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
5b80dfeab267e29ee13c243c45fb994c8683c290
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/order/heyting/boundary.lean
192f0371025d86a59f2414cafbb13a66eca13c29
[ "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,488
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import order.boolean_algebra /-! # Co-Heyting boundary The boundary of an element of a co-Heyting algebra is the intersection of its Heyting negation with itself. The boundary in the co-Heyting algebra of closed sets coincides with the topological boundary. ## Main declarations * `coheyting.boundary`: Co-Heyting boundary. `coheyting.boundary a = a ⊓ ¬a` ## Notation `∂ a` is notation for `coheyting.boundary a` in locale `heyting`. -/ variables {α : Type*} namespace coheyting variables [coheyting_algebra α] {a b : α} /-- The boundary of an element of a co-Heyting algebra is the intersection of its Heyting negation with itself. Note that this is always `⊥` for a boolean algebra. -/ def boundary (a : α) : α := a ⊓ ¬a localized "prefix `∂ `:120 := coheyting.boundary" in heyting lemma inf_hnot_self (a : α) : a ⊓ ¬a = ∂ a := rfl lemma boundary_le : ∂ a ≤ a := inf_le_left lemma boundary_le_hnot : ∂ a ≤ ¬a := inf_le_right @[simp] lemma boundary_bot : ∂ (⊥ : α) = ⊥ := bot_inf_eq @[simp] lemma boundary_top : ∂ (⊤ : α) = ⊥ := by rw [boundary, hnot_top, inf_bot_eq] lemma boundary_hnot_le (a : α) : ∂ ¬a ≤ ∂ a := inf_comm.trans_le $ inf_le_inf_right _ hnot_hnot_le @[simp] lemma boundary_hnot_hnot (a : α) : ∂ ¬¬a = ∂ ¬a := by simp_rw [boundary, hnot_hnot_hnot, inf_comm] @[simp] lemma hnot_boundary (a : α) : ¬ ∂ a = ⊤ := by rw [boundary, hnot_inf_distrib, sup_hnot_self] /-- **Leibniz rule** for the co-Heyting boundary. -/ lemma boundary_inf (a b : α) : ∂ (a ⊓ b) = ∂ a ⊓ b ⊔ a ⊓ ∂ b := by { unfold boundary, rw [hnot_inf_distrib, inf_sup_left, inf_right_comm, ←inf_assoc] } lemma boundary_inf_le : ∂ (a ⊓ b) ≤ ∂ a ⊔ ∂ b := (boundary_inf _ _).trans_le $ sup_le_sup inf_le_left inf_le_right lemma boundary_sup_le : ∂ (a ⊔ b) ≤ ∂ a ⊔ ∂ b := begin rw [boundary, inf_sup_right], exact sup_le_sup (inf_le_inf_left _ $ hnot_anti le_sup_left) (inf_le_inf_left _ $ hnot_anti le_sup_right), end /- The intuitionistic version of `coheyting.boundary_le_boundary_sup_sup_boundary_inf_left`. Either proof can be obtained from the other using the equivalence of Heyting algebras and intuitionistic logic and duality between Heyting and co-Heyting algebras. It is crucial that the following proof be intuitionistic. -/ example (a b : Prop) : ((a ∧ b) ∨ ¬(a ∧ b)) ∧ ((a ∨ b) ∨ ¬ (a ∨ b)) → a ∨ ¬ a := begin rintro ⟨⟨ha, hb⟩ | hnab, (ha | hb) | hnab⟩; try { exact or.inl ha }, { exact or.inr (λ ha, hnab ⟨ha, hb⟩) }, { exact or.inr (λ ha, hnab $ or.inl ha) } end lemma boundary_le_boundary_sup_sup_boundary_inf_left : ∂ a ≤ ∂ (a ⊔ b) ⊔ ∂ (a ⊓ b) := begin simp only [boundary, sup_inf_left, sup_inf_right, sup_right_idem, le_inf_iff, sup_assoc, @sup_comm _ _ _ a], refine ⟨⟨⟨_, _⟩, _⟩, ⟨_, _⟩, _⟩; try { exact le_sup_of_le_left inf_le_left }; refine inf_le_of_right_le _, { rw [hnot_le_iff_codisjoint_right, codisjoint_left_comm], exact codisjoint_hnot_left }, { refine le_sup_of_le_right _, rw hnot_le_iff_codisjoint_right, exact codisjoint_hnot_right.mono_right (hnot_anti inf_le_left) } end lemma boundary_le_boundary_sup_sup_boundary_inf_right : ∂ b ≤ ∂ (a ⊔ b) ⊔ ∂ (a ⊓ b) := by { rw [@sup_comm _ _ a, inf_comm], exact boundary_le_boundary_sup_sup_boundary_inf_left } lemma boundary_sup_sup_boundary_inf (a b : α) : ∂ (a ⊔ b) ⊔ ∂ (a ⊓ b) = ∂ a ⊔ ∂ b := le_antisymm (sup_le boundary_sup_le boundary_inf_le) $ sup_le boundary_le_boundary_sup_sup_boundary_inf_left boundary_le_boundary_sup_sup_boundary_inf_right @[simp] lemma boundary_idem (a : α) : ∂ ∂ a = ∂ a := by rw [boundary, hnot_boundary, inf_top_eq] lemma hnot_hnot_sup_boundary (a : α) : ¬¬a ⊔ ∂ a = a := by { rw [boundary, sup_inf_left, hnot_sup_self, inf_top_eq, sup_eq_right], exact hnot_hnot_le } lemma hnot_eq_top_iff_exists_boundary : ¬a = ⊤ ↔ ∃ b, ∂ b = a := ⟨λ h, ⟨a, by rw [boundary, h, inf_top_eq]⟩, by { rintro ⟨b, rfl⟩, exact hnot_boundary _ }⟩ end coheyting open_locale heyting section boolean_algebra variables [boolean_algebra α] @[simp] lemma coheyting.boundary_eq_bot (a : α) : ∂ a = ⊥ := inf_compl_eq_bot end boolean_algebra
f87b8ea328d69e8c09a00ba30a6db71b4884312c
f4bff2062c030df03d65e8b69c88f79b63a359d8
/src/game/sup_inf/infSumSets.lean
53f3c1fec805b239076bd99b85c0e0a86c2184f6
[ "Apache-2.0" ]
permissive
adastra7470/real-number-game
776606961f52db0eb824555ed2f8e16f92216ea3
f9dcb7d9255a79b57e62038228a23346c2dc301b
refs/heads/master
1,669,221,575,893
1,594,669,800,000
1,594,669,800,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,399
lean
import game.sup_inf.supSumSets namespace xena -- hide /- # Chapter 3 : Sup and Inf ## Level 6 This level, very similar to the previous, showcases the infimum. -/ def sum_of_sets (A : set ℝ) (B : set ℝ) := { x : ℝ | ∃ y ∈ A, ∃ z ∈ B, x = y + z} /- Lemma If $A$ and $B$ are sets of reals, then $$ \textrm{inf} (A + B) = \textrm{inf} (A) + \textrm{inf}(B)$$ -/ lemma inf_sum_of_sets (A : set ℝ) (B : set ℝ) (h1A : A.nonempty) (h1B : B.nonempty) (h2A : bdd_below A) (h2B : bdd_below B) (a : ℝ) (b : ℝ) : (is_glb A a) ∧ (is_glb B b) → is_glb (sum_of_sets A B) (a + b) := begin intro h, cases h with hA hB, split, -- prove that (a+b) is a lower bound intros x h0, cases h0 with y h1, cases h1 with yA h2, cases h2 with z h3, cases h3 with zB hx, --have H11A := hA.right, have H11B := hB.right, have H12A := hA.left, have H12B := hB.left, have H13A := H12A yA, have H13B := H12B zB, linarith, -- now prove (a+b) is the greatest lower bound intros L hL, -- L is another lower bound of (A+B) have H1 : ∀ x ∈ A, (L - x) ∈ lower_bounds B, { intros x hx y hy, suffices : L ≤ x + y, by linarith, exact hL ⟨x, hx, y, hy, rfl⟩, }, have H2 : L - b ∈ lower_bounds A, { intros x hx, suffices : L - x ≤ b, by linarith, exact hB.2 (H1 x hx), }, linarith [hA.2 H2], done end end xena -- hide
b965eacdb25cd171c14561ed177aa29e0b96bf7f
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/big_operators/basic.lean
7db65eb99e4cc81d9a2bffe8de38ce1f5015e19c
[ "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
59,610
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 data.finset.fold import data.equiv.mul_add import tactic.abel /-! # Big operators In this file we define products and sums indexed by finite sets (specifically, `finset`). ## Notation We introduce the following notation, localized in `big_operators`. To enable the notation, use `open_locale big_operators`. Let `s` be a `finset α`, and `f : α → β` a function. * `∏ x in s, f x` is notation for `finset.prod s f` (assuming `β` is a `comm_monoid`) * `∑ x in s, f x` is notation for `finset.sum s f` (assuming `β` is an `add_comm_monoid`) * `∏ x, f x` is notation for `finset.prod finset.univ f` (assuming `α` is a `fintype` and `β` is a `comm_monoid`) * `∑ x, f x` is notation for `finset.sum finset.univ f` (assuming `α` is a `fintype` and `β` is an `add_comm_monoid`) ## Implementation Notes The first arguments in all definitions and lemmas is the codomain of the function of the big operator. This is necessary for the heuristic in `@[to_additive]`. See the documentation of `to_additive.attr` for more information. -/ universes u v w variables {β : Type u} {α : Type v} {γ : Type w} namespace finset /-- `∏ x in s, f x` is the product of `f x` as `x` ranges over the elements of the finite set `s`. -/ @[to_additive "`∑ x in s, f` is the sum of `f x` as `x` ranges over the elements of the finite set `s`."] protected def prod [comm_monoid β] (s : finset α) (f : α → β) : β := (s.1.map f).prod @[simp, to_additive] lemma prod_mk [comm_monoid β] (s : multiset α) (hs : s.nodup) (f : α → β) : (⟨s, hs⟩ : finset α).prod f = (s.map f).prod := rfl end finset /-- There is no established mathematical convention for the operator precedence of big operators like `∏` and `∑`. We will have to make a choice. Online discussions, such as https://math.stackexchange.com/q/185538/30839 seem to suggest that `∏` and `∑` should have the same precedence, and that this should be somewhere between `*` and `+`. The latter have precedence levels `70` and `65` respectively, and we therefore choose the level `67`. In practice, this means that parentheses should be placed as follows: ```lean ∑ k in K, (a k + b k) = ∑ k in K, a k + ∑ k in K, b k → ∏ k in K, a k * b k = (∏ k in K, a k) * (∏ k in K, b k) ``` (Example taken from page 490 of Knuth's *Concrete Mathematics*.) -/ library_note "operator precedence of big operators" localized "notation `∑` binders `, ` r:(scoped:67 f, finset.sum finset.univ f) := r" in big_operators localized "notation `∏` binders `, ` r:(scoped:67 f, finset.prod finset.univ f) := r" in big_operators localized "notation `∑` binders ` in ` s `, ` r:(scoped:67 f, finset.sum s f) := r" in big_operators localized "notation `∏` binders ` in ` s `, ` r:(scoped:67 f, finset.prod s f) := r" in big_operators open_locale big_operators namespace finset variables {s s₁ s₂ : finset α} {a : α} {f g : α → β} @[to_additive] lemma prod_eq_multiset_prod [comm_monoid β] (s : finset α) (f : α → β) : ∏ x in s, f x = (s.1.map f).prod := rfl @[to_additive] theorem prod_eq_fold [comm_monoid β] (s : finset α) (f : α → β) : ∏ x in s, f x = s.fold (*) 1 f := rfl @[simp] lemma sum_multiset_singleton (s : finset α) : s.sum (λ x, x ::ₘ 0) = s.val := by simp [sum_eq_multiset_sum] end finset @[to_additive] lemma monoid_hom.map_prod [comm_monoid β] [comm_monoid γ] (g : β →* γ) (f : α → β) (s : finset α) : g (∏ x in s, f x) = ∏ x in s, g (f x) := by simp only [finset.prod_eq_multiset_prod, g.map_multiset_prod, multiset.map_map] @[to_additive] lemma mul_equiv.map_prod [comm_monoid β] [comm_monoid γ] (g : β ≃* γ) (f : α → β) (s : finset α) : g (∏ x in s, f x) = ∏ x in s, g (f x) := g.to_monoid_hom.map_prod f s lemma ring_hom.map_list_prod [semiring β] [semiring γ] (f : β →+* γ) (l : list β) : f l.prod = (l.map f).prod := f.to_monoid_hom.map_list_prod l lemma ring_hom.map_list_sum [non_assoc_semiring β] [non_assoc_semiring γ] (f : β →+* γ) (l : list β) : f l.sum = (l.map f).sum := f.to_add_monoid_hom.map_list_sum l lemma ring_hom.map_multiset_prod [comm_semiring β] [comm_semiring γ] (f : β →+* γ) (s : multiset β) : f s.prod = (s.map f).prod := f.to_monoid_hom.map_multiset_prod s lemma ring_hom.map_multiset_sum [non_assoc_semiring β] [non_assoc_semiring γ] (f : β →+* γ) (s : multiset β) : f s.sum = (s.map f).sum := f.to_add_monoid_hom.map_multiset_sum s lemma ring_hom.map_prod [comm_semiring β] [comm_semiring γ] (g : β →+* γ) (f : α → β) (s : finset α) : g (∏ x in s, f x) = ∏ x in s, g (f x) := g.to_monoid_hom.map_prod f s lemma ring_hom.map_sum [non_assoc_semiring β] [non_assoc_semiring γ] (g : β →+* γ) (f : α → β) (s : finset α) : g (∑ x in s, f x) = ∑ x in s, g (f x) := g.to_add_monoid_hom.map_sum f s @[to_additive] lemma monoid_hom.coe_prod [mul_one_class β] [comm_monoid γ] (f : α → β →* γ) (s : finset α) : ⇑(∏ x in s, f x) = ∏ x in s, f x := (monoid_hom.coe_fn β γ).map_prod _ _ -- See also `finset.prod_apply`, with the same conclusion -- but with the weaker hypothesis `f : α → β → γ`. @[simp, to_additive] lemma monoid_hom.finset_prod_apply [mul_one_class β] [comm_monoid γ] (f : α → β →* γ) (s : finset α) (b : β) : (∏ x in s, f x) b = ∏ x in s, f x b := (monoid_hom.eval b).map_prod _ _ variables {s s₁ s₂ : finset α} {a : α} {f g : α → β} namespace finset section comm_monoid variables [comm_monoid β] @[simp, to_additive] lemma prod_empty {f : α → β} : (∏ x in (∅:finset α), f x) = 1 := rfl @[simp, to_additive] lemma prod_insert [decidable_eq α] : a ∉ s → (∏ x in (insert a s), f x) = f a * ∏ x in s, f x := fold_insert /-- The product of `f` over `insert a s` is the same as the product over `s`, as long as `a` is in `s` or `f a = 1`. -/ @[simp, to_additive "The sum of `f` over `insert a s` is the same as the sum over `s`, as long as `a` is in `s` or `f a = 0`."] lemma prod_insert_of_eq_one_if_not_mem [decidable_eq α] (h : a ∉ s → f a = 1) : ∏ x in insert a s, f x = ∏ x in s, f x := begin by_cases hm : a ∈ s, { simp_rw insert_eq_of_mem hm }, { rw [prod_insert hm, h hm, one_mul] }, end /-- The product of `f` over `insert a s` is the same as the product over `s`, as long as `f a = 1`. -/ @[simp, to_additive "The sum of `f` over `insert a s` is the same as the sum over `s`, as long as `f a = 0`."] lemma prod_insert_one [decidable_eq α] (h : f a = 1) : ∏ x in insert a s, f x = ∏ x in s, f x := prod_insert_of_eq_one_if_not_mem (λ _, h) @[simp, to_additive] lemma prod_singleton : (∏ x in (singleton a), f x) = f a := eq.trans fold_singleton $ mul_one _ @[to_additive] lemma prod_pair [decidable_eq α] {a b : α} (h : a ≠ b) : (∏ x in ({a, b} : finset α), f x) = f a * f b := by rw [prod_insert (not_mem_singleton.2 h), prod_singleton] @[simp, priority 1100] lemma prod_const_one : (∏ x in s, (1 : β)) = 1 := by simp only [finset.prod, multiset.map_const, multiset.prod_repeat, one_pow] @[simp, priority 1100] lemma sum_const_zero {β α} {s : finset α} [add_comm_monoid β] : (∑ x in s, (0 : β)) = 0 := @prod_const_one (multiplicative β) _ _ _ attribute [to_additive] prod_const_one @[simp, to_additive] lemma prod_image [decidable_eq α] {s : finset γ} {g : γ → α} : (∀x∈s, ∀y∈s, g x = g y → x = y) → (∏ x in (s.image g), f x) = ∏ x in s, f (g x) := fold_image @[simp, to_additive] lemma prod_map (s : finset α) (e : α ↪ γ) (f : γ → β) : (∏ x in (s.map e), f x) = ∏ x in s, f (e x) := by rw [finset.prod, finset.map_val, multiset.map_map]; refl @[congr, to_additive] lemma prod_congr (h : s₁ = s₂) : (∀x∈s₂, f x = g x) → s₁.prod f = s₂.prod g := by rw [h]; exact fold_congr attribute [congr] finset.sum_congr @[to_additive] lemma prod_union_inter [decidable_eq α] : (∏ x in (s₁ ∪ s₂), f x) * (∏ x in (s₁ ∩ s₂), f x) = (∏ x in s₁, f x) * (∏ x in s₂, f x) := fold_union_inter @[to_additive] lemma prod_union [decidable_eq α] (h : disjoint s₁ s₂) : (∏ x in (s₁ ∪ s₂), f x) = (∏ x in s₁, f x) * (∏ x in s₂, f x) := by rw [←prod_union_inter, (disjoint_iff_inter_eq_empty.mp h)]; exact (mul_one _).symm end comm_monoid end finset section open finset variables [fintype α] [decidable_eq α] [comm_monoid β] @[to_additive] lemma is_compl.prod_mul_prod {s t : finset α} (h : is_compl s t) (f : α → β) : (∏ i in s, f i) * (∏ i in t, f i) = ∏ i, f i := (finset.prod_union h.disjoint).symm.trans $ by rw [← finset.sup_eq_union, h.sup_eq_top]; refl end namespace finset section comm_monoid variables [comm_monoid β] @[to_additive] lemma prod_mul_prod_compl [fintype α] [decidable_eq α] (s : finset α) (f : α → β) : (∏ i in s, f i) * (∏ i in sᶜ, f i) = ∏ i, f i := is_compl_compl.prod_mul_prod f @[to_additive] lemma prod_compl_mul_prod [fintype α] [decidable_eq α] (s : finset α) (f : α → β) : (∏ i in sᶜ, f i) * (∏ i in s, f i) = ∏ i, f i := is_compl_compl.symm.prod_mul_prod f @[to_additive] lemma prod_sdiff [decidable_eq α] (h : s₁ ⊆ s₂) : (∏ x in (s₂ \ s₁), f x) * (∏ x in s₁, f x) = (∏ x in s₂, f x) := by rw [←prod_union sdiff_disjoint, sdiff_union_of_subset h] @[simp, to_additive] lemma prod_sum_elim [decidable_eq (α ⊕ γ)] (s : finset α) (t : finset γ) (f : α → β) (g : γ → β) : ∏ x in s.map function.embedding.inl ∪ t.map function.embedding.inr, sum.elim f g x = (∏ x in s, f x) * (∏ x in t, g x) := begin rw [prod_union, prod_map, prod_map], { simp only [sum.elim_inl, function.embedding.inl_apply, function.embedding.inr_apply, sum.elim_inr] }, { simp only [disjoint_left, finset.mem_map, finset.mem_map], rintros _ ⟨i, hi, rfl⟩ ⟨j, hj, H⟩, cases H } end @[to_additive] lemma prod_bUnion [decidable_eq α] {s : finset γ} {t : γ → finset α} : (∀ x ∈ s, ∀ y ∈ s, x ≠ y → disjoint (t x) (t y)) → (∏ x in (s.bUnion t), f x) = ∏ x in s, ∏ i in t x, f i := by haveI := classical.dec_eq γ; exact finset.induction_on s (λ _, by simp only [bUnion_empty, prod_empty]) (assume x s hxs ih hd, have hd' : ∀x∈s, ∀y∈s, x ≠ y → disjoint (t x) (t y), from assume _ hx _ hy, hd _ (mem_insert_of_mem hx) _ (mem_insert_of_mem hy), have ∀y∈s, x ≠ y, from assume _ hy h, by rw [←h] at hy; contradiction, have ∀y∈s, disjoint (t x) (t y), from assume _ hy, hd _ (mem_insert_self _ _) _ (mem_insert_of_mem hy) (this _ hy), have disjoint (t x) (finset.bUnion s t), from (disjoint_bUnion_right _ _ _).mpr this, by simp only [bUnion_insert, prod_insert hxs, prod_union this, ih hd']) @[to_additive] lemma prod_product {s : finset γ} {t : finset α} {f : γ×α → β} : (∏ x in s.product t, f x) = ∏ x in s, ∏ y in t, f (x, y) := begin haveI := classical.dec_eq α, haveI := classical.dec_eq γ, rw [product_eq_bUnion, prod_bUnion], { congr, funext, exact prod_image (λ _ _ _ _ H, (prod.mk.inj H).2) }, simp only [disjoint_iff_ne, mem_image], rintros _ _ _ _ h ⟨_, _⟩ ⟨_, _, ⟨_, _⟩⟩ ⟨_, _⟩ ⟨_, _, ⟨_, _⟩⟩ _, apply h, cc end /-- An uncurried version of `finset.prod_product`. -/ @[to_additive "An uncurried version of `finset.sum_product`"] lemma prod_product' {s : finset γ} {t : finset α} {f : γ → α → β} : (∏ x in s.product t, f x.1 x.2) = ∏ x in s, ∏ y in t, f x y := prod_product /-- Product over a sigma type equals the product of fiberwise products. For rewriting in the reverse direction, use `finset.prod_sigma'`. -/ @[to_additive "Sum over a sigma type equals the sum of fiberwise sums. For rewriting in the reverse direction, use `finset.sum_sigma'`"] lemma prod_sigma {σ : α → Type*} (s : finset α) (t : Πa, finset (σ a)) (f : sigma σ → β) : (∏ x in s.sigma t, f x) = ∏ a in s, ∏ s in (t a), f ⟨a, s⟩ := by classical; calc (∏ x in s.sigma t, f x) = ∏ x in s.bUnion (λa, (t a).map (function.embedding.sigma_mk a)), f x : by rw sigma_eq_bUnion ... = ∏ a in s, ∏ x in (t a).map (function.embedding.sigma_mk a), f x : prod_bUnion $ assume a₁ ha a₂ ha₂ h x hx, by { simp only [inf_eq_inter, mem_inter, mem_map, function.embedding.sigma_mk_apply] at hx, rcases hx with ⟨⟨y, hy, rfl⟩, ⟨z, hz, hz'⟩⟩, cc } ... = ∏ a in s, ∏ s in t a, f ⟨a, s⟩ : prod_congr rfl $ λ _ _, prod_map _ _ _ @[to_additive] lemma prod_sigma' {σ : α → Type*} (s : finset α) (t : Πa, finset (σ a)) (f : Πa, σ a → β) : (∏ a in s, ∏ s in (t a), f a s) = ∏ x in s.sigma t, f x.1 x.2 := eq.symm $ prod_sigma s t (λ x, f x.1 x.2) @[to_additive] lemma prod_fiberwise_of_maps_to [decidable_eq γ] {s : finset α} {t : finset γ} {g : α → γ} (h : ∀ x ∈ s, g x ∈ t) (f : α → β) : (∏ y in t, ∏ x in s.filter (λ x, g x = y), f x) = ∏ x in s, f x := begin letI := classical.dec_eq α, rw [← bUnion_filter_eq_of_maps_to h] {occs := occurrences.pos [2]}, refine (prod_bUnion $ λ x' hx y' hy hne, _).symm, rw [disjoint_filter], rintros x hx rfl, exact hne end @[to_additive] lemma prod_image' [decidable_eq α] {s : finset γ} {g : γ → α} (h : γ → β) (eq : ∀c∈s, f (g c) = ∏ x in s.filter (λc', g c' = g c), h x) : (∏ x in s.image g, f x) = ∏ x in s, h x := calc (∏ x in s.image g, f x) = ∏ x in s.image g, ∏ x in s.filter (λ c', g c' = x), h x : prod_congr rfl $ λ x hx, let ⟨c, hcs, hc⟩ := mem_image.1 hx in hc ▸ (eq c hcs) ... = ∏ x in s, h x : prod_fiberwise_of_maps_to (λ x, mem_image_of_mem g) _ @[to_additive] lemma prod_mul_distrib : ∏ x in s, (f x * g x) = (∏ x in s, f x) * (∏ x in s, g x) := eq.trans (by rw one_mul; refl) fold_op_distrib @[to_additive] lemma prod_comm {s : finset γ} {t : finset α} {f : γ → α → β} : (∏ x in s, ∏ y in t, f x y) = (∏ y in t, ∏ x in s, f x y) := begin classical, apply finset.induction_on s, { simp only [prod_empty, prod_const_one] }, { intros _ _ H ih, simp only [prod_insert H, prod_mul_distrib, ih] } end @[to_additive] lemma prod_hom [comm_monoid γ] (s : finset α) {f : α → β} (g : β → γ) [is_monoid_hom g] : (∏ x in s, g (f x)) = g (∏ x in s, f x) := ((monoid_hom.of g).map_prod f s).symm @[to_additive] lemma prod_hom_rel [comm_monoid γ] {r : β → γ → Prop} {f : α → β} {g : α → γ} {s : finset α} (h₁ : r 1 1) (h₂ : ∀a b c, r b c → r (f a * b) (g a * c)) : r (∏ x in s, f x) (∏ x in s, g x) := by { delta finset.prod, apply multiset.prod_hom_rel; assumption } @[to_additive] lemma prod_subset (h : s₁ ⊆ s₂) (hf : ∀ x ∈ s₂, x ∉ s₁ → f x = 1) : (∏ x in s₁, f x) = ∏ x in s₂, f x := by haveI := classical.dec_eq α; exact have ∏ x in s₂ \ s₁, f x = ∏ x in s₂ \ s₁, 1, from prod_congr rfl $ by simpa only [mem_sdiff, and_imp], by rw [←prod_sdiff h]; simp only [this, prod_const_one, one_mul] @[to_additive] lemma prod_filter_of_ne {p : α → Prop} [decidable_pred p] (hp : ∀ x ∈ s, f x ≠ 1 → p x) : (∏ x in (s.filter p), f x) = (∏ x in s, f x) := prod_subset (filter_subset _ _) $ λ x, by { classical, rw [not_imp_comm, mem_filter], exact λ h₁ h₂, ⟨h₁, hp _ h₁ h₂⟩ } -- If we use `[decidable_eq β]` here, some rewrites fail because they find a wrong `decidable` -- instance first; `{∀x, decidable (f x ≠ 1)}` doesn't work with `rw ← prod_filter_ne_one` @[to_additive] lemma prod_filter_ne_one [∀ x, decidable (f x ≠ 1)] : (∏ x in (s.filter $ λx, f x ≠ 1), f x) = (∏ x in s, f x) := prod_filter_of_ne $ λ _ _, id @[to_additive] lemma prod_filter (p : α → Prop) [decidable_pred p] (f : α → β) : (∏ a in s.filter p, f a) = (∏ a in s, if p a then f a else 1) := calc (∏ a in s.filter p, f a) = ∏ a in s.filter p, if p a then f a else 1 : prod_congr rfl (assume a h, by rw [if_pos (mem_filter.1 h).2]) ... = ∏ a in s, if p a then f a else 1 : begin refine prod_subset (filter_subset _ s) (assume x hs h, _), rw [mem_filter, not_and] at h, exact if_neg (h hs) end @[to_additive] lemma prod_eq_single_of_mem {s : finset α} {f : α → β} (a : α) (h : a ∈ s) (h₀ : ∀ b ∈ s, b ≠ a → f b = 1) : (∏ x in s, f x) = f a := begin haveI := classical.dec_eq α, calc (∏ x in s, f x) = ∏ x in {a}, f x : begin refine (prod_subset _ _).symm, { intros _ H, rwa mem_singleton.1 H }, { simpa only [mem_singleton] } end ... = f a : prod_singleton end @[to_additive] lemma prod_eq_single {s : finset α} {f : α → β} (a : α) (h₀ : ∀b∈s, b ≠ a → f b = 1) (h₁ : a ∉ s → f a = 1) : (∏ x in s, f x) = f a := by haveI := classical.dec_eq α; from classical.by_cases (assume : a ∈ s, prod_eq_single_of_mem a this h₀) (assume : a ∉ s, (prod_congr rfl $ λ b hb, h₀ b hb $ by rintro rfl; cc).trans $ prod_const_one.trans (h₁ this).symm) @[to_additive] lemma prod_eq_mul_of_mem {s : finset α} {f : α → β} (a b : α) (ha : a ∈ s) (hb : b ∈ s) (hn : a ≠ b) (h₀ : ∀ c ∈ s, c ≠ a ∧ c ≠ b → f c = 1) : (∏ x in s, f x) = (f a) * (f b) := begin haveI := classical.dec_eq α; let s' := ({a, b} : finset α), have hu : s' ⊆ s, { refine insert_subset.mpr _, apply and.intro ha, apply singleton_subset_iff.mpr hb }, have hf : ∀ c ∈ s, c ∉ s' → f c = 1, { intros c hc hcs, apply h₀ c hc, apply not_or_distrib.mp, intro hab, apply hcs, apply mem_insert.mpr, rw mem_singleton, exact hab }, rw ←prod_subset hu hf, exact finset.prod_pair hn end @[to_additive] lemma prod_eq_mul {s : finset α} {f : α → β} (a b : α) (hn : a ≠ b) (h₀ : ∀ c ∈ s, c ≠ a ∧ c ≠ b → f c = 1) (ha : a ∉ s → f a = 1) (hb : b ∉ s → f b = 1) : (∏ x in s, f x) = (f a) * (f b) := begin haveI := classical.dec_eq α; by_cases h₁ : a ∈ s; by_cases h₂ : b ∈ s, { exact prod_eq_mul_of_mem a b h₁ h₂ hn h₀ }, { rw [hb h₂, mul_one], apply prod_eq_single_of_mem a h₁, exact λ c hc hca, h₀ c hc ⟨hca, ne_of_mem_of_not_mem hc h₂⟩ }, { rw [ha h₁, one_mul], apply prod_eq_single_of_mem b h₂, exact λ c hc hcb, h₀ c hc ⟨ne_of_mem_of_not_mem hc h₁, hcb⟩ }, { rw [ha h₁, hb h₂, mul_one], exact trans (prod_congr rfl (λ c hc, h₀ c hc ⟨ne_of_mem_of_not_mem hc h₁, ne_of_mem_of_not_mem hc h₂⟩)) prod_const_one } end @[to_additive] lemma prod_attach {f : α → β} : (∏ x in s.attach, f x) = (∏ x in s, f x) := by haveI := classical.dec_eq α; exact calc (∏ x in s.attach, f x.val) = (∏ x in (s.attach).image subtype.val, f x) : by rw [prod_image]; exact assume x _ y _, subtype.eq ... = _ : by rw [attach_image_val] /-- A product over `s.subtype p` equals one over `s.filter p`. -/ @[simp, to_additive "A sum over `s.subtype p` equals one over `s.filter p`."] lemma prod_subtype_eq_prod_filter (f : α → β) {p : α → Prop} [decidable_pred p] : ∏ x in s.subtype p, f x = ∏ x in s.filter p, f x := begin conv_lhs { erw ←prod_map (s.subtype p) (function.embedding.subtype _) f }, exact prod_congr (subtype_map _) (λ x hx, rfl) end /-- If all elements of a `finset` satisfy the predicate `p`, a product over `s.subtype p` equals that product over `s`. -/ @[to_additive "If all elements of a `finset` satisfy the predicate `p`, a sum over `s.subtype p` equals that sum over `s`."] lemma prod_subtype_of_mem (f : α → β) {p : α → Prop} [decidable_pred p] (h : ∀ x ∈ s, p x) : ∏ x in s.subtype p, f x = ∏ x in s, f x := by simp_rw [prod_subtype_eq_prod_filter, filter_true_of_mem h] /-- A product of a function over a `finset` in a subtype equals a product in the main type of a function that agrees with the first function on that `finset`. -/ @[to_additive "A sum of a function over a `finset` in a subtype equals a sum in the main type of a function that agrees with the first function on that `finset`."] lemma prod_subtype_map_embedding {p : α → Prop} {s : finset {x // p x}} {f : {x // p x} → β} {g : α → β} (h : ∀ x : {x // p x}, x ∈ s → g x = f x) : ∏ x in s.map (function.embedding.subtype _), g x = ∏ x in s, f x := begin rw finset.prod_map, exact finset.prod_congr rfl h end @[to_additive] lemma prod_finset_coe (f : α → β) (s : finset α) : ∏ (i : (s : set α)), f i = ∏ i in s, f i := prod_attach @[to_additive] lemma prod_subtype {p : α → Prop} {F : fintype (subtype p)} (s : finset α) (h : ∀ x, x ∈ s ↔ p x) (f : α → β) : ∏ a in s, f a = ∏ a : subtype p, f a := have (∈ s) = p, from set.ext h, by { substI p, rw [←prod_finset_coe], congr } @[to_additive] lemma prod_eq_one {f : α → β} {s : finset α} (h : ∀x∈s, f x = 1) : (∏ x in s, f x) = 1 := calc (∏ x in s, f x) = ∏ x in s, 1 : finset.prod_congr rfl h ... = 1 : finset.prod_const_one @[to_additive] lemma prod_apply_dite {s : finset α} {p : α → Prop} {hp : decidable_pred p} (f : Π (x : α), p x → γ) (g : Π (x : α), ¬p x → γ) (h : γ → β) : (∏ x in s, h (if hx : p x then f x hx else g x hx)) = (∏ x in (s.filter p).attach, h (f x.1 (mem_filter.mp x.2).2)) * (∏ x in (s.filter (λ x, ¬ p x)).attach, h (g x.1 (mem_filter.mp x.2).2)) := by letI := classical.dec_eq α; exact calc ∏ x in s, h (if hx : p x then f x hx else g x hx) = ∏ x in s.filter p ∪ s.filter (λ x, ¬ p x), h (if hx : p x then f x hx else g x hx) : by rw [filter_union_filter_neg_eq] ... = (∏ x in s.filter p, h (if hx : p x then f x hx else g x hx)) * (∏ x in s.filter (λ x, ¬ p x), h (if hx : p x then f x hx else g x hx)) : prod_union (by simp [disjoint_right] {contextual := tt}) ... = (∏ x in (s.filter p).attach, h (if hx : p x.1 then f x.1 hx else g x.1 hx)) * (∏ x in (s.filter (λ x, ¬ p x)).attach, h (if hx : p x.1 then f x.1 hx else g x.1 hx)) : congr_arg2 _ prod_attach.symm prod_attach.symm ... = (∏ x in (s.filter p).attach, h (f x.1 (mem_filter.mp x.2).2)) * (∏ x in (s.filter (λ x, ¬ p x)).attach, h (g x.1 (mem_filter.mp x.2).2)) : congr_arg2 _ (prod_congr rfl (λ x hx, congr_arg h (dif_pos (mem_filter.mp x.2).2))) (prod_congr rfl (λ x hx, congr_arg h (dif_neg (mem_filter.mp x.2).2))) @[to_additive] lemma prod_apply_ite {s : finset α} {p : α → Prop} {hp : decidable_pred p} (f g : α → γ) (h : γ → β) : (∏ x in s, h (if p x then f x else g x)) = (∏ x in s.filter p, h (f x)) * (∏ x in s.filter (λ x, ¬ p x), h (g x)) := trans (prod_apply_dite _ _ _) (congr_arg2 _ (@prod_attach _ _ _ _ (h ∘ f)) (@prod_attach _ _ _ _ (h ∘ g))) @[to_additive] lemma prod_dite {s : finset α} {p : α → Prop} {hp : decidable_pred p} (f : Π (x : α), p x → β) (g : Π (x : α), ¬p x → β) : (∏ x in s, if hx : p x then f x hx else g x hx) = (∏ x in (s.filter p).attach, f x.1 (mem_filter.mp x.2).2) * (∏ x in (s.filter (λ x, ¬ p x)).attach, g x.1 (mem_filter.mp x.2).2) := by simp [prod_apply_dite _ _ (λ x, x)] @[to_additive] lemma prod_ite {s : finset α} {p : α → Prop} {hp : decidable_pred p} (f g : α → β) : (∏ x in s, if p x then f x else g x) = (∏ x in s.filter p, f x) * (∏ x in s.filter (λ x, ¬ p x), g x) := by simp [prod_apply_ite _ _ (λ x, x)] @[to_additive] lemma prod_ite_of_false {p : α → Prop} {hp : decidable_pred p} (f g : α → β) (h : ∀ x ∈ s, ¬p x) : (∏ x in s, if p x then f x else g x) = (∏ x in s, g x) := by { rw prod_ite, simp [filter_false_of_mem h, filter_true_of_mem h] } @[to_additive] lemma prod_ite_of_true {p : α → Prop} {hp : decidable_pred p} (f g : α → β) (h : ∀ x ∈ s, p x) : (∏ x in s, if p x then f x else g x) = (∏ x in s, f x) := by { simp_rw ←(ite_not (p _)), apply prod_ite_of_false, simpa } @[to_additive] lemma prod_apply_ite_of_false {p : α → Prop} {hp : decidable_pred p} (f g : α → γ) (k : γ → β) (h : ∀ x ∈ s, ¬p x) : (∏ x in s, k (if p x then f x else g x)) = (∏ x in s, k (g x)) := by { simp_rw apply_ite k, exact prod_ite_of_false _ _ h } @[to_additive] lemma prod_apply_ite_of_true {p : α → Prop} {hp : decidable_pred p} (f g : α → γ) (k : γ → β) (h : ∀ x ∈ s, p x) : (∏ x in s, k (if p x then f x else g x)) = (∏ x in s, k (f x)) := by { simp_rw apply_ite k, exact prod_ite_of_true _ _ h } @[to_additive] lemma prod_extend_by_one [decidable_eq α] (s : finset α) (f : α → β) : ∏ i in s, (if i ∈ s then f i else 1) = ∏ i in s, f i := prod_congr rfl $ λ i hi, if_pos hi @[simp, to_additive] lemma prod_dite_eq [decidable_eq α] (s : finset α) (a : α) (b : Π x : α, a = x → β) : (∏ x in s, (if h : a = x then b x h else 1)) = ite (a ∈ s) (b a rfl) 1 := begin split_ifs with h, { rw [finset.prod_eq_single a, dif_pos rfl], { intros, rw dif_neg, cc }, { cc } }, { rw finset.prod_eq_one, intros, rw dif_neg, intro, cc } end @[simp, to_additive] lemma prod_dite_eq' [decidable_eq α] (s : finset α) (a : α) (b : Π x : α, x = a → β) : (∏ x in s, (if h : x = a then b x h else 1)) = ite (a ∈ s) (b a rfl) 1 := begin split_ifs with h, { rw [finset.prod_eq_single a, dif_pos rfl], { intros, rw dif_neg, cc }, { cc } }, { rw finset.prod_eq_one, intros, rw dif_neg, intro, cc } end @[simp, to_additive] lemma prod_ite_eq [decidable_eq α] (s : finset α) (a : α) (b : α → β) : (∏ x in s, (ite (a = x) (b x) 1)) = ite (a ∈ s) (b a) 1 := prod_dite_eq s a (λ x _, b x) /-- When a product is taken over a conditional whose condition is an equality test on the index and whose alternative is 1, then the product's value is either the term at that index or `1`. The difference with `prod_ite_eq` is that the arguments to `eq` are swapped. -/ @[simp, to_additive] lemma prod_ite_eq' [decidable_eq α] (s : finset α) (a : α) (b : α → β) : (∏ x in s, (ite (x = a) (b x) 1)) = ite (a ∈ s) (b a) 1 := prod_dite_eq' s a (λ x _, b x) @[to_additive] lemma prod_ite_index (p : Prop) [decidable p] (s t : finset α) (f : α → β) : (∏ x in if p then s else t, f x) = if p then ∏ x in s, f x else ∏ x in t, f x := apply_ite (λ s, ∏ x in s, f x) _ _ _ @[simp, to_additive] lemma prod_dite_irrel (p : Prop) [decidable p] (s : finset α) (f : p → α → β) (g : ¬p → α → β): (∏ x in s, if h : p then f h x else g h x) = if h : p then ∏ x in s, f h x else ∏ x in s, g h x := by { split_ifs with h; refl } @[simp] lemma sum_pi_single' {ι M : Type*} [decidable_eq ι] [add_comm_monoid M] (i : ι) (x : M) (s : finset ι) : ∑ j in s, pi.single i x j = if i ∈ s then x else 0 := sum_dite_eq' _ _ _ @[simp] lemma sum_pi_single {ι : Type*} {M : ι → Type*} [decidable_eq ι] [Π i, add_comm_monoid (M i)] (i : ι) (f : Π i, M i) (s : finset ι) : ∑ j in s, pi.single j (f j) i = if i ∈ s then f i else 0 := sum_dite_eq _ _ _ /-- Reorder a product. The difference with `prod_bij'` is that the bijection is specified as a surjective injection, rather than by an inverse function. -/ @[to_additive " Reorder a sum. The difference with `sum_bij'` is that the bijection is specified as a surjective injection, rather than by an inverse function. "] lemma prod_bij {s : finset α} {t : finset γ} {f : α → β} {g : γ → β} (i : Πa∈s, γ) (hi : ∀a ha, i a ha ∈ t) (h : ∀a ha, f a = g (i a ha)) (i_inj : ∀a₁ a₂ ha₁ ha₂, i a₁ ha₁ = i a₂ ha₂ → a₁ = a₂) (i_surj : ∀b∈t, ∃a ha, b = i a ha) : (∏ x in s, f x) = (∏ x in t, g x) := congr_arg multiset.prod (multiset.map_eq_map_of_bij_of_nodup f g s.2 t.2 i hi h i_inj i_surj) /-- Reorder a product. The difference with `prod_bij` is that the bijection is specified with an inverse, rather than as a surjective injection. -/ @[to_additive " Reorder a sum. The difference with `sum_bij` is that the bijection is specified with an inverse, rather than as a surjective injection. "] lemma prod_bij' {s : finset α} {t : finset γ} {f : α → β} {g : γ → β} (i : Πa∈s, γ) (hi : ∀a ha, i a ha ∈ t) (h : ∀a ha, f a = g (i a ha)) (j : Πa∈t, α) (hj : ∀a ha, j a ha ∈ s) (left_inv : ∀ a ha, j (i a ha) (hi a ha) = a) (right_inv : ∀ a ha, i (j a ha) (hj a ha) = a) : (∏ x in s, f x) = (∏ x in t, g x) := begin refine prod_bij i hi h _ _, {intros a1 a2 h1 h2 eq, rw [←left_inv a1 h1, ←left_inv a2 h2], cc,}, {intros b hb, use j b hb, use hj b hb, exact (right_inv b hb).symm,}, end @[to_additive] lemma prod_bij_ne_one {s : finset α} {t : finset γ} {f : α → β} {g : γ → β} (i : Πa∈s, f a ≠ 1 → γ) (hi : ∀a h₁ h₂, i a h₁ h₂ ∈ t) (i_inj : ∀a₁ a₂ h₁₁ h₁₂ h₂₁ h₂₂, i a₁ h₁₁ h₁₂ = i a₂ h₂₁ h₂₂ → a₁ = a₂) (i_surj : ∀b∈t, g b ≠ 1 → ∃a h₁ h₂, b = i a h₁ h₂) (h : ∀a h₁ h₂, f a = g (i a h₁ h₂)) : (∏ x in s, f x) = (∏ x in t, g x) := by classical; exact calc (∏ x in s, f x) = ∏ x in (s.filter $ λx, f x ≠ 1), f x : prod_filter_ne_one.symm ... = ∏ x in (t.filter $ λx, g x ≠ 1), g x : prod_bij (assume a ha, i a (mem_filter.mp ha).1 (mem_filter.mp ha).2) (assume a ha, (mem_filter.mp ha).elim $ λh₁ h₂, mem_filter.mpr ⟨hi a h₁ h₂, λ hg, h₂ (hg ▸ h a h₁ h₂)⟩) (assume a ha, (mem_filter.mp ha).elim $ h a) (assume a₁ a₂ ha₁ ha₂, (mem_filter.mp ha₁).elim $ λ ha₁₁ ha₁₂, (mem_filter.mp ha₂).elim $ λ ha₂₁ ha₂₂, i_inj a₁ a₂ _ _ _ _) (assume b hb, (mem_filter.mp hb).elim $ λh₁ h₂, let ⟨a, ha₁, ha₂, eq⟩ := i_surj b h₁ h₂ in ⟨a, mem_filter.mpr ⟨ha₁, ha₂⟩, eq⟩) ... = (∏ x in t, g x) : prod_filter_ne_one @[to_additive] lemma nonempty_of_prod_ne_one (h : (∏ x in s, f x) ≠ 1) : s.nonempty := s.eq_empty_or_nonempty.elim (λ H, false.elim $ h $ H.symm ▸ prod_empty) id @[to_additive] lemma exists_ne_one_of_prod_ne_one (h : (∏ x in s, f x) ≠ 1) : ∃a∈s, f a ≠ 1 := begin classical, rw ← prod_filter_ne_one at h, rcases nonempty_of_prod_ne_one h with ⟨x, hx⟩, exact ⟨x, (mem_filter.1 hx).1, (mem_filter.1 hx).2⟩ end @[to_additive] lemma prod_subset_one_on_sdiff [decidable_eq α] (h : s₁ ⊆ s₂) (hg : ∀ x ∈ (s₂ \ s₁), g x = 1) (hfg : ∀ x ∈ s₁, f x = g x) : ∏ i in s₁, f i = ∏ i in s₂, g i := begin rw [← prod_sdiff h, prod_eq_one hg, one_mul], exact prod_congr rfl hfg end @[to_additive] lemma prod_range_succ_comm (f : ℕ → β) (n : ℕ) : ∏ x in range (n + 1), f x = f n * ∏ x in range n, f x := by rw [range_succ, prod_insert not_mem_range_self] @[to_additive] lemma prod_range_succ (f : ℕ → β) (n : ℕ) : ∏ x in range (n + 1), f x = (∏ x in range n, f x) * f n := by simp only [mul_comm, prod_range_succ_comm] @[to_additive] lemma prod_range_succ' (f : ℕ → β) : ∀ n : ℕ, (∏ k in range (n + 1), f k) = (∏ k in range n, f (k+1)) * f 0 | 0 := prod_range_succ _ _ | (n + 1) := by rw [prod_range_succ _ n, mul_right_comm, ← prod_range_succ', prod_range_succ] @[to_additive] lemma eventually_constant_prod {u : ℕ → β} {N : ℕ} (hu : ∀ n ≥ N, u n = 1) {n : ℕ} (hn : N ≤ n) : ∏ k in range (n + 1), u k = ∏ k in range (N + 1), u k := begin obtain ⟨m, rfl : n = N + m⟩ := le_iff_exists_add.mp hn, clear hn, induction m with m hm, { simp }, erw [prod_range_succ, hm], simp [hu] end @[to_additive] lemma prod_range_add (f : ℕ → β) (n m : ℕ) : ∏ x in range (n + m), f x = (∏ x in range n, f x) * (∏ x in range m, f (n + x)) := begin induction m with m hm, { simp }, { rw [nat.add_succ, prod_range_succ, hm, prod_range_succ, mul_assoc], }, end @[to_additive] lemma prod_range_zero (f : ℕ → β) : ∏ k in range 0, f k = 1 := by rw [range_zero, prod_empty] @[to_additive sum_range_one] lemma prod_range_one (f : ℕ → β) : ∏ k in range 1, f k = f 0 := by { rw [range_one], apply @prod_singleton β ℕ 0 f } open multiset lemma prod_multiset_map_count [decidable_eq α] (s : multiset α) {M : Type*} [comm_monoid M] (f : α → M) : (s.map f).prod = ∏ m in s.to_finset, (f m) ^ (s.count m) := begin apply s.induction_on, { simp only [prod_const_one, count_zero, prod_zero, pow_zero, map_zero] }, intros a s ih, simp only [prod_cons, map_cons, to_finset_cons, ih], by_cases has : a ∈ s.to_finset, { rw [insert_eq_of_mem has, ← insert_erase has, prod_insert (not_mem_erase _ _), prod_insert (not_mem_erase _ _), ← mul_assoc, count_cons_self, pow_succ], congr' 1, refine prod_congr rfl (λ x hx, _), rw [count_cons_of_ne (ne_of_mem_erase hx)] }, rw [prod_insert has, count_cons_self, count_eq_zero_of_not_mem (mt mem_to_finset.2 has), pow_one], congr' 1, refine prod_congr rfl (λ x hx, _), rw count_cons_of_ne, rintro rfl, exact has hx end lemma sum_multiset_map_count [decidable_eq α] (s : multiset α) {M : Type*} [add_comm_monoid M] (f : α → M) : (s.map f).sum = ∑ m in s.to_finset, s.count m • f m := @prod_multiset_map_count _ _ _ (multiplicative M) _ f attribute [to_additive] prod_multiset_map_count lemma prod_multiset_count [decidable_eq α] [comm_monoid α] (s : multiset α) : s.prod = ∏ m in s.to_finset, m ^ (s.count m) := by { convert prod_multiset_map_count s id, rw map_id } lemma sum_multiset_count [decidable_eq α] [add_comm_monoid α] (s : multiset α) : s.sum = ∑ m in s.to_finset, s.count m • m := @prod_multiset_count (multiplicative α) _ _ s attribute [to_additive] prod_multiset_count /-- To prove a property of a product, it suffices to prove that the property is multiplicative and holds on factors. -/ @[to_additive "To prove a property of a sum, it suffices to prove that the property is additive and holds on summands."] lemma prod_induction {M : Type*} [comm_monoid M] (f : α → M) (p : M → Prop) (p_mul : ∀ a b, p a → p b → p (a * b)) (p_one : p 1) (p_s : ∀ x ∈ s, p $ f x) : p $ ∏ x in s, f x := multiset.prod_induction _ _ p_mul p_one (multiset.forall_mem_map_iff.mpr p_s) /-- To prove a property of a product, it suffices to prove that the property is multiplicative and holds on factors. -/ @[to_additive "To prove a property of a sum, it suffices to prove that the property is additive and holds on summands."] lemma prod_induction_nonempty {M : Type*} [comm_monoid M] (f : α → M) (p : M → Prop) (p_mul : ∀ a b, p a → p b → p (a * b)) (hs_nonempty : s.nonempty) (p_s : ∀ x ∈ s, p $ f x) : p $ ∏ x in s, f x := multiset.prod_induction_nonempty p p_mul (by simp [nonempty_iff_ne_empty.mp hs_nonempty]) (multiset.forall_mem_map_iff.mpr p_s) /-- For any product along `{0, ..., n-1}` of a commutative-monoid-valued function, we can verify that it's equal to a different function just by checking ratios of adjacent terms. This is a multiplicative discrete analogue of the fundamental theorem of calculus. -/ lemma prod_range_induction {M : Type*} [comm_monoid M] (f s : ℕ → M) (h0 : s 0 = 1) (h : ∀ n, s (n + 1) = s n * f n) (n : ℕ) : ∏ k in finset.range n, f k = s n := begin induction n with k hk, { simp only [h0, finset.prod_range_zero] }, { simp only [hk, finset.prod_range_succ, h, mul_comm] } end /-- For any sum along `{0, ..., n-1}` of a commutative-monoid-valued function, we can verify that it's equal to a different function just by checking differences of adjacent terms. This is a discrete analogue of the fundamental theorem of calculus. -/ lemma sum_range_induction {M : Type*} [add_comm_monoid M] (f s : ℕ → M) (h0 : s 0 = 0) (h : ∀ n, s (n + 1) = s n + f n) (n : ℕ) : ∑ k in finset.range n, f k = s n := @prod_range_induction (multiplicative M) _ f s h0 h n /-- A telescoping sum along `{0, ..., n-1}` of an additive commutative group valued function reduces to the difference of the last and first terms.-/ lemma sum_range_sub {G : Type*} [add_comm_group G] (f : ℕ → G) (n : ℕ) : ∑ i in range n, (f (i+1) - f i) = f n - f 0 := by { apply sum_range_induction; abel, simp } lemma sum_range_sub' {G : Type*} [add_comm_group G] (f : ℕ → G) (n : ℕ) : ∑ i in range n, (f i - f (i+1)) = f 0 - f n := by { apply sum_range_induction; abel, simp } /-- A telescoping product along `{0, ..., n-1}` of a commutative group valued function reduces to the ratio of the last and first factors.-/ @[to_additive] lemma prod_range_div {M : Type*} [comm_group M] (f : ℕ → M) (n : ℕ) : ∏ i in range n, (f (i+1) * (f i)⁻¹) = f n * (f 0)⁻¹ := by simpa only [← div_eq_mul_inv] using @sum_range_sub (additive M) _ f n @[to_additive] lemma prod_range_div' {M : Type*} [comm_group M] (f : ℕ → M) (n : ℕ) : ∏ i in range n, (f i * (f (i+1))⁻¹) = (f 0) * (f n)⁻¹ := by simpa only [← div_eq_mul_inv] using @sum_range_sub' (additive M) _ f n /-- A telescoping sum along `{0, ..., n-1}` of an `ℕ`-valued function reduces to the difference of the last and first terms when the function we are summing is monotone. -/ lemma sum_range_sub_of_monotone {f : ℕ → ℕ} (h : monotone f) (n : ℕ) : ∑ i in range n, (f (i+1) - f i) = f n - f 0 := begin refine sum_range_induction _ _ (nat.sub_self _) (λ n, _) _, have h₁ : f n ≤ f (n+1) := h (nat.le_succ _), have h₂ : f 0 ≤ f n := h (nat.zero_le _), rw [←nat.sub_add_comm h₂, nat.add_sub_cancel' h₁], end @[simp] lemma prod_const (b : β) : (∏ x in s, b) = b ^ s.card := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (λ a s has ih, by rw [prod_insert has, card_insert_of_not_mem has, pow_succ, ih]) lemma pow_eq_prod_const (b : β) : ∀ n, b ^ n = ∏ k in range n, b | 0 := by simp | (n+1) := by simp lemma prod_pow (s : finset α) (n : ℕ) (f : α → β) : ∏ x in s, f x ^ n = (∏ x in s, f x) ^ n := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (by simp [mul_pow] {contextual := tt}) @[to_additive] lemma prod_flip {n : ℕ} (f : ℕ → β) : ∏ r in range (n + 1), f (n - r) = ∏ k in range (n + 1), f k := begin induction n with n ih, { rw [prod_range_one, prod_range_one] }, { rw [prod_range_succ', prod_range_succ _ (nat.succ n)], simp [← ih] } end @[to_additive] lemma prod_involution {s : finset α} {f : α → β} : ∀ (g : Π a ∈ s, α) (h : ∀ a ha, f a * f (g a ha) = 1) (g_ne : ∀ a ha, f a ≠ 1 → g a ha ≠ a) (g_mem : ∀ a ha, g a ha ∈ s) (g_inv : ∀ a ha, g (g a ha) (g_mem a ha) = a), (∏ x in s, f x) = 1 := by haveI := classical.dec_eq α; haveI := classical.dec_eq β; exact finset.strong_induction_on s (λ s ih g h g_ne g_mem g_inv, s.eq_empty_or_nonempty.elim (λ hs, hs.symm ▸ rfl) (λ ⟨x, hx⟩, have hmem : ∀ y ∈ (s.erase x).erase (g x hx), y ∈ s, from λ y hy, (mem_of_mem_erase (mem_of_mem_erase hy)), have g_inj : ∀ {x hx y hy}, g x hx = g y hy → x = y, from λ x hx y hy h, by rw [← g_inv x hx, ← g_inv y hy]; simp [h], have ih': ∏ y in erase (erase s x) (g x hx), f y = (1 : β) := ih ((s.erase x).erase (g x hx)) ⟨subset.trans (erase_subset _ _) (erase_subset _ _), λ h, not_mem_erase (g x hx) (s.erase x) (h (g_mem x hx))⟩ (λ y hy, g y (hmem y hy)) (λ y hy, h y (hmem y hy)) (λ y hy, g_ne y (hmem y hy)) (λ y hy, mem_erase.2 ⟨λ (h : g y _ = g x hx), by simpa [g_inj h] using hy, mem_erase.2 ⟨λ (h : g y _ = x), have y = g x hx, from g_inv y (hmem y hy) ▸ by simp [h], by simpa [this] using hy, g_mem y (hmem y hy)⟩⟩) (λ y hy, g_inv y (hmem y hy)), if hx1 : f x = 1 then ih' ▸ eq.symm (prod_subset hmem (λ y hy hy₁, have y = x ∨ y = g x hx, by simp [hy] at hy₁; tauto, this.elim (λ hy, hy.symm ▸ hx1) (λ hy, h x hx ▸ hy ▸ hx1.symm ▸ (one_mul _).symm))) else by rw [← insert_erase hx, prod_insert (not_mem_erase _ _), ← insert_erase (mem_erase.2 ⟨g_ne x hx hx1, g_mem x hx⟩), prod_insert (not_mem_erase _ _), ih', mul_one, h x hx])) /-- The product of the composition of functions `f` and `g`, is the product over `b ∈ s.image g` of `f b` to the power of the cardinality of the fibre of `b` -/ lemma prod_comp [decidable_eq γ] {s : finset α} (f : γ → β) (g : α → γ) : ∏ a in s, f (g a) = ∏ b in s.image g, f b ^ (s.filter (λ a, g a = b)).card := calc ∏ a in s, f (g a) = ∏ x in (s.image g).sigma (λ b : γ, s.filter (λ a, g a = b)), f (g x.2) : prod_bij (λ a ha, ⟨g a, a⟩) (by simp; tauto) (λ _ _, rfl) (by simp) (by finish) ... = ∏ b in s.image g, ∏ a in s.filter (λ a, g a = b), f (g a) : prod_sigma _ _ _ ... = ∏ b in s.image g, ∏ a in s.filter (λ a, g a = b), f b : prod_congr rfl (λ b hb, prod_congr rfl (by simp {contextual := tt})) ... = ∏ b in s.image g, f b ^ (s.filter (λ a, g a = b)).card : prod_congr rfl (λ _ _, prod_const _) @[to_additive] lemma prod_piecewise [decidable_eq α] (s t : finset α) (f g : α → β) : (∏ x in s, (t.piecewise f g) x) = (∏ x in s ∩ t, f x) * (∏ x in s \ t, g x) := by { rw [piecewise, prod_ite, filter_mem_eq_inter, ← sdiff_eq_filter], } @[to_additive] lemma prod_inter_mul_prod_diff [decidable_eq α] (s t : finset α) (f : α → β) : (∏ x in s ∩ t, f x) * (∏ x in s \ t, f x) = (∏ x in s, f x) := by { convert (s.prod_piecewise t f f).symm, simp [finset.piecewise] } @[to_additive] lemma prod_eq_mul_prod_diff_singleton [decidable_eq α] {s : finset α} {i : α} (h : i ∈ s) (f : α → β) : ∏ x in s, f x = f i * ∏ x in s \ {i}, f x := by { convert (s.prod_inter_mul_prod_diff {i} f).symm, simp [h] } @[to_additive] lemma prod_eq_prod_diff_singleton_mul [decidable_eq α] {s : finset α} {i : α} (h : i ∈ s) (f : α → β) : ∏ x in s, f x = (∏ x in s \ {i}, f x) * f i := by { rw [prod_eq_mul_prod_diff_singleton h, mul_comm] } @[to_additive] lemma _root_.fintype.prod_eq_mul_prod_compl [decidable_eq α] [fintype α] (a : α) (f : α → β) : ∏ i, f i = (f a) * ∏ i in {a}ᶜ, f i := prod_eq_mul_prod_diff_singleton (mem_univ a) f @[to_additive] lemma _root_.fintype.prod_eq_prod_compl_mul [decidable_eq α] [fintype α] (a : α) (f : α → β) : ∏ i, f i = (∏ i in {a}ᶜ, f i) * f a := prod_eq_prod_diff_singleton_mul (mem_univ a) f /-- A product can be partitioned into a product of products, each equivalent under a setoid. -/ @[to_additive "A sum can be partitioned into a sum of sums, each equivalent under a setoid."] lemma prod_partition (R : setoid α) [decidable_rel R.r] : (∏ x in s, f x) = ∏ xbar in s.image quotient.mk, ∏ y in s.filter (λ y, ⟦y⟧ = xbar), f y := begin refine (finset.prod_image' f (λ x hx, _)).symm, refl, end /-- If we can partition a product into subsets that cancel out, then the whole product cancels. -/ @[to_additive "If we can partition a sum into subsets that cancel out, then the whole sum cancels."] lemma prod_cancels_of_partition_cancels (R : setoid α) [decidable_rel R.r] (h : ∀ x ∈ s, (∏ a in s.filter (λ y, y ≈ x), f a) = 1) : (∏ x in s, f x) = 1 := begin rw [prod_partition R, ←finset.prod_eq_one], intros xbar xbar_in_s, obtain ⟨x, x_in_s, xbar_eq_x⟩ := mem_image.mp xbar_in_s, rw [←xbar_eq_x, filter_congr (λ y _, @quotient.eq _ R y x)], apply h x x_in_s, end @[to_additive] lemma prod_update_of_not_mem [decidable_eq α] {s : finset α} {i : α} (h : i ∉ s) (f : α → β) (b : β) : (∏ x in s, function.update f i b x) = (∏ x in s, f x) := begin apply prod_congr rfl (λj hj, _), have : j ≠ i, by { assume eq, rw eq at hj, exact h hj }, simp [this] end lemma prod_update_of_mem [decidable_eq α] {s : finset α} {i : α} (h : i ∈ s) (f : α → β) (b : β) : (∏ x in s, function.update f i b x) = b * (∏ x in s \ (singleton i), f x) := by { rw [update_eq_piecewise, prod_piecewise], simp [h] } /-- If a product of a `finset` of size at most 1 has a given value, so do the terms in that product. -/ @[to_additive eq_of_card_le_one_of_sum_eq "If a sum of a `finset` of size at most 1 has a given value, so do the terms in that sum."] lemma eq_of_card_le_one_of_prod_eq {s : finset α} (hc : s.card ≤ 1) {f : α → β} {b : β} (h : ∏ x in s, f x = b) : ∀ x ∈ s, f x = b := begin intros x hx, by_cases hc0 : s.card = 0, { exact false.elim (card_ne_zero_of_mem hx hc0) }, { have h1 : s.card = 1 := le_antisymm hc (nat.one_le_of_lt (nat.pos_of_ne_zero hc0)), rw card_eq_one at h1, cases h1 with x2 hx2, rw [hx2, mem_singleton] at hx, simp_rw hx2 at h, rw hx, rw prod_singleton at h, exact h } end /-- If a function applied at a point is 1, a product is unchanged by removing that point, if present, from a `finset`. -/ @[to_additive "If a function applied at a point is 0, a sum is unchanged by removing that point, if present, from a `finset`."] lemma prod_erase [decidable_eq α] (s : finset α) {f : α → β} {a : α} (h : f a = 1) : ∏ x in s.erase a, f x = ∏ x in s, f x := begin rw ←sdiff_singleton_eq_erase, refine prod_subset (sdiff_subset _ _) (λ x hx hnx, _), rw sdiff_singleton_eq_erase at hnx, rwa eq_of_mem_of_not_mem_erase hx hnx end /-- If a product is 1 and the function is 1 except possibly at one point, it is 1 everywhere on the `finset`. -/ @[to_additive "If a sum is 0 and the function is 0 except possibly at one point, it is 0 everywhere on the `finset`."] lemma eq_one_of_prod_eq_one {s : finset α} {f : α → β} {a : α} (hp : ∏ x in s, f x = 1) (h1 : ∀ x ∈ s, x ≠ a → f x = 1) : ∀ x ∈ s, f x = 1 := begin intros x hx, classical, by_cases h : x = a, { rw h, rw h at hx, rw [←prod_subset (singleton_subset_iff.2 hx) (λ t ht ha, h1 t ht (not_mem_singleton.1 ha)), prod_singleton] at hp, exact hp }, { exact h1 x hx h } end lemma prod_pow_boole [decidable_eq α] (s : finset α) (f : α → β) (a : α) : (∏ x in s, (f x)^(ite (a = x) 1 0)) = ite (a ∈ s) (f a) 1 := by simp end comm_monoid /-- If `f = g = h` everywhere but at `i`, where `f i = g i + h i`, then the product of `f` over `s` is the sum of the products of `g` and `h`. -/ lemma prod_add_prod_eq [comm_semiring β] {s : finset α} {i : α} {f g h : α → β} (hi : i ∈ s) (h1 : g i + h i = f i) (h2 : ∀ j ∈ s, j ≠ i → g j = f j) (h3 : ∀ j ∈ s, j ≠ i → h j = f j) : ∏ i in s, g i + ∏ i in s, h i = ∏ i in s, f i := by { classical, simp_rw [prod_eq_mul_prod_diff_singleton hi, ← h1, right_distrib], congr' 2; apply prod_congr rfl; simpa } lemma sum_update_of_mem [add_comm_monoid β] [decidable_eq α] {s : finset α} {i : α} (h : i ∈ s) (f : α → β) (b : β) : (∑ x in s, function.update f i b x) = b + (∑ x in s \ (singleton i), f x) := by { rw [update_eq_piecewise, sum_piecewise], simp [h] } attribute [to_additive] prod_update_of_mem lemma sum_nsmul [add_comm_monoid β] (s : finset α) (n : ℕ) (f : α → β) : (∑ x in s, n • (f x)) = n • ((∑ x in s, f x)) := @prod_pow (multiplicative β) _ _ _ _ _ attribute [to_additive sum_nsmul] prod_pow @[simp] lemma sum_const [add_comm_monoid β] (b : β) : (∑ x in s, b) = s.card • b := @prod_const (multiplicative β) _ _ _ _ attribute [to_additive] prod_const lemma card_eq_sum_ones (s : finset α) : s.card = ∑ _ in s, 1 := by simp lemma sum_const_nat {m : ℕ} {f : α → ℕ} (h₁ : ∀x ∈ s, f x = m) : (∑ x in s, f x) = card s * m := begin rw [← nat.nsmul_eq_mul, ← sum_const], apply sum_congr rfl h₁ end @[simp] lemma sum_boole {s : finset α} {p : α → Prop} [non_assoc_semiring β] {hp : decidable_pred p} : (∑ x in s, if p x then (1 : β) else (0 : β)) = (s.filter p).card := by simp [sum_ite] @[norm_cast] lemma sum_nat_cast [add_comm_monoid β] [has_one β] (s : finset α) (f : α → ℕ) : ↑(∑ x in s, f x : ℕ) = (∑ x in s, (f x : β)) := (nat.cast_add_monoid_hom β).map_sum f s @[norm_cast] lemma sum_int_cast [add_comm_group β] [has_one β] (s : finset α) (f : α → ℤ) : ↑(∑ x in s, f x : ℤ) = (∑ x in s, (f x : β)) := (int.cast_add_hom β).map_sum f s lemma sum_comp [add_comm_monoid β] [decidable_eq γ] {s : finset α} (f : γ → β) (g : α → γ) : ∑ a in s, f (g a) = ∑ b in s.image g, (s.filter (λ a, g a = b)).card • (f b) := @prod_comp (multiplicative β) _ _ _ _ _ _ _ attribute [to_additive "The sum of the composition of functions `f` and `g`, is the sum over `b ∈ s.image g` of `f b` times of the cardinality of the fibre of `b`"] prod_comp lemma eq_sum_range_sub [add_comm_group β] (f : ℕ → β) (n : ℕ) : f n = f 0 + ∑ i in range n, (f (i+1) - f i) := by { rw finset.sum_range_sub, abel } lemma eq_sum_range_sub' [add_comm_group β] (f : ℕ → β) (n : ℕ) : f n = ∑ i in range (n + 1), if i = 0 then f 0 else f i - f (i - 1) := begin conv_lhs { rw [finset.eq_sum_range_sub f] }, simp [finset.sum_range_succ', add_comm] end section opposite open opposite /-- Moving to the opposite additive commutative monoid commutes with summing. -/ @[simp] lemma op_sum [add_comm_monoid β] {s : finset α} (f : α → β) : op (∑ x in s, f x) = ∑ x in s, op (f x) := (op_add_equiv : β ≃+ βᵒᵖ).map_sum _ _ @[simp] lemma unop_sum [add_comm_monoid β] {s : finset α} (f : α → βᵒᵖ) : unop (∑ x in s, f x) = ∑ x in s, unop (f x) := (op_add_equiv : β ≃+ βᵒᵖ).symm.map_sum _ _ end opposite section comm_group variables [comm_group β] @[simp, to_additive] lemma prod_inv_distrib : (∏ x in s, (f x)⁻¹) = (∏ x in s, f x)⁻¹ := s.prod_hom has_inv.inv end comm_group @[simp] theorem card_sigma {σ : α → Type*} (s : finset α) (t : Π a, finset (σ a)) : card (s.sigma t) = ∑ a in s, card (t a) := multiset.card_sigma _ _ lemma card_bUnion [decidable_eq β] {s : finset α} {t : α → finset β} (h : ∀ x ∈ s, ∀ y ∈ s, x ≠ y → disjoint (t x) (t y)) : (s.bUnion t).card = ∑ u in s, card (t u) := calc (s.bUnion t).card = ∑ i in s.bUnion t, 1 : by simp ... = ∑ a in s, ∑ i in t a, 1 : finset.sum_bUnion h ... = ∑ u in s, card (t u) : by simp lemma card_bUnion_le [decidable_eq β] {s : finset α} {t : α → finset β} : (s.bUnion t).card ≤ ∑ a in s, (t a).card := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (λ a s has ih, calc ((insert a s).bUnion t).card ≤ (t a).card + (s.bUnion t).card : by rw bUnion_insert; exact finset.card_union_le _ _ ... ≤ ∑ a in insert a s, card (t a) : by rw sum_insert has; exact add_le_add_left ih _) theorem card_eq_sum_card_fiberwise [decidable_eq β] {f : α → β} {s : finset α} {t : finset β} (H : ∀ x ∈ s, f x ∈ t) : s.card = ∑ a in t, (s.filter (λ x, f x = a)).card := by simp only [card_eq_sum_ones, sum_fiberwise_of_maps_to H] theorem card_eq_sum_card_image [decidable_eq β] (f : α → β) (s : finset α) : s.card = ∑ a in s.image f, (s.filter (λ x, f x = a)).card := card_eq_sum_card_fiberwise (λ _, mem_image_of_mem _) lemma gsmul_sum [add_comm_group β] {f : α → β} {s : finset α} (z : ℤ) : gsmul z (∑ a in s, f a) = ∑ a in s, gsmul z (f a) := (s.sum_hom (gsmul z)).symm @[simp] lemma sum_sub_distrib [add_comm_group β] : ∑ x in s, (f x - g x) = (∑ x in s, f x) - (∑ x in s, g x) := by simpa only [sub_eq_add_neg] using sum_add_distrib.trans (congr_arg _ sum_neg_distrib) section prod_eq_zero variables [comm_monoid_with_zero β] lemma prod_eq_zero (ha : a ∈ s) (h : f a = 0) : (∏ x in s, f x) = 0 := by haveI := classical.dec_eq α; calc (∏ x in s, f x) = ∏ x in insert a (erase s a), f x : by rw insert_erase ha ... = 0 : by rw [prod_insert (not_mem_erase _ _), h, zero_mul] lemma prod_boole {s : finset α} {p : α → Prop} [decidable_pred p] : ∏ i in s, ite (p i) (1 : β) (0 : β) = ite (∀ i ∈ s, p i) 1 0 := begin split_ifs, { apply prod_eq_one, intros i hi, rw if_pos (h i hi) }, { push_neg at h, rcases h with ⟨i, hi, hq⟩, apply prod_eq_zero hi, rw [if_neg hq] }, end variables [nontrivial β] [no_zero_divisors β] lemma prod_eq_zero_iff : (∏ x in s, f x) = 0 ↔ (∃a∈s, f a = 0) := begin classical, apply finset.induction_on s, exact ⟨not.elim one_ne_zero, λ ⟨_, H, _⟩, H.elim⟩, assume a s ha ih, rw [prod_insert ha, mul_eq_zero, bex_def, exists_mem_insert, ih, ← bex_def] end theorem prod_ne_zero_iff : (∏ x in s, f x) ≠ 0 ↔ (∀ a ∈ s, f a ≠ 0) := by { rw [ne, prod_eq_zero_iff], push_neg } end prod_eq_zero section comm_group_with_zero variables [comm_group_with_zero β] @[simp] lemma prod_inv_distrib' : (∏ x in s, (f x)⁻¹) = (∏ x in s, f x)⁻¹ := begin classical, by_cases h : ∃ x ∈ s, f x = 0, { simpa [prod_eq_zero_iff.mpr h, prod_eq_zero_iff] using h }, { push_neg at h, have h' := prod_ne_zero_iff.mpr h, have hf : ∀ x ∈ s, (f x)⁻¹ * f x = 1 := λ x hx, inv_mul_cancel (h x hx), apply mul_right_cancel' h', simp [h, h', ← finset.prod_mul_distrib, prod_congr rfl hf] } end end comm_group_with_zero end finset namespace fintype open finset /-- `fintype.prod_bijective` is a variant of `finset.prod_bij` that accepts `function.bijective`. See `function.bijective.prod_comp` for a version without `h`. -/ @[to_additive "`fintype.sum_equiv` is a variant of `finset.sum_bij` that accepts `function.bijective`. See `function.bijective.sum_comp` for a version without `h`. "] lemma prod_bijective {α β M : Type*} [fintype α] [fintype β] [comm_monoid M] (e : α → β) (he : function.bijective e) (f : α → M) (g : β → M) (h : ∀ x, f x = g (e x)) : ∏ x : α, f x = ∏ x : β, g x := prod_bij (λ x _, e x) (λ x _, mem_univ (e x)) (λ x _, h x) (λ x x' _ _ h, he.injective h) (λ y _, (he.surjective y).imp $ λ a h, ⟨mem_univ _, h.symm⟩) /-- `fintype.prod_equiv` is a specialization of `finset.prod_bij` that automatically fills in most arguments. See `equiv.prod_comp` for a version without `h`. -/ @[to_additive "`fintype.sum_equiv` is a specialization of `finset.sum_bij` that automatically fills in most arguments. See `equiv.sum_comp` for a version without `h`. "] lemma prod_equiv {α β M : Type*} [fintype α] [fintype β] [comm_monoid M] (e : α ≃ β) (f : α → M) (g : β → M) (h : ∀ x, f x = g (e x)) : ∏ x : α, f x = ∏ x : β, g x := prod_bijective e e.bijective f g h @[to_additive] lemma prod_finset_coe [comm_monoid β] : ∏ (i : (s : set α)), f i = ∏ i in s, f i := (finset.prod_subtype s (λ _, iff.rfl) f).symm end fintype namespace list @[to_additive] lemma prod_to_finset {M : Type*} [decidable_eq α] [comm_monoid M] (f : α → M) : ∀ {l : list α} (hl : l.nodup), l.to_finset.prod f = (l.map f).prod | [] _ := by simp | (a :: l) hl := let ⟨not_mem, hl⟩ := list.nodup_cons.mp hl in by simp [finset.prod_insert (mt list.mem_to_finset.mp not_mem), prod_to_finset hl] end list namespace multiset lemma abs_sum_le_sum_abs [linear_ordered_add_comm_group α] {s : multiset α} : abs s.sum ≤ (s.map abs).sum := le_sum_of_subadditive _ abs_zero abs_add s variables [decidable_eq α] @[simp] lemma to_finset_sum_count_eq (s : multiset α) : (∑ a in s.to_finset, s.count a) = s.card := multiset.induction_on s rfl (assume a s ih, calc (∑ x in to_finset (a ::ₘ s), count x (a ::ₘ s)) = ∑ x in to_finset (a ::ₘ s), ((if x = a then 1 else 0) + count x s) : finset.sum_congr rfl $ λ _ _, by split_ifs; [simp only [h, count_cons_self, nat.one_add], simp only [count_cons_of_ne h, zero_add]] ... = card (a ::ₘ s) : begin by_cases a ∈ s.to_finset, { have : ∑ x in s.to_finset, ite (x = a) 1 0 = ∑ x in {a}, ite (x = a) 1 0, { rw [finset.sum_ite_eq', if_pos h, finset.sum_singleton, if_pos rfl], }, rw [to_finset_cons, finset.insert_eq_of_mem h, finset.sum_add_distrib, ih, this, finset.sum_singleton, if_pos rfl, add_comm, card_cons] }, { have ha : a ∉ s, by rwa mem_to_finset at h, have : ∑ x in to_finset s, ite (x = a) 1 0 = ∑ x in to_finset s, 0, from finset.sum_congr rfl (λ x hx, if_neg $ by rintro rfl; cc), rw [to_finset_cons, finset.sum_insert h, if_pos rfl, finset.sum_add_distrib, this, finset.sum_const_zero, ih, count_eq_zero_of_not_mem ha, zero_add, add_comm, card_cons] } end) lemma count_sum' {s : finset β} {a : α} {f : β → multiset α} : count a (∑ x in s, f x) = ∑ x in s, count a (f x) := by { dunfold finset.sum, rw count_sum } @[simp] lemma to_finset_sum_count_nsmul_eq (s : multiset α) : (∑ a in s.to_finset, s.count a • (a ::ₘ 0)) = s := begin apply ext', intro b, rw count_sum', have h : count b s = count b (count b s • (b ::ₘ 0)), { rw [singleton_coe, count_nsmul, ← singleton_coe, count_singleton, mul_one] }, rw h, clear h, apply finset.sum_eq_single b, { intros c h hcb, rw count_nsmul, convert mul_zero (count c s), apply count_eq_zero.mpr, exact finset.not_mem_singleton.mpr (ne.symm hcb) }, { intro hb, rw [count_eq_zero_of_not_mem (mt mem_to_finset.2 hb), count_nsmul, zero_mul]} end theorem exists_smul_of_dvd_count (s : multiset α) {k : ℕ} (h : ∀ (a : α), k ∣ multiset.count a s) : ∃ (u : multiset α), s = k • u := begin use ∑ a in s.to_finset, (s.count a / k) • (a ::ₘ 0), have h₂ : ∑ (x : α) in s.to_finset, k • (count x s / k) • (x ::ₘ 0) = ∑ (x : α) in s.to_finset, count x s • (x ::ₘ 0), { refine congr_arg s.to_finset.sum _, apply funext, intro x, rw [← mul_nsmul, nat.mul_div_cancel' (h x)] }, rw [← finset.sum_nsmul, h₂, to_finset_sum_count_nsmul_eq] end end multiset @[simp, norm_cast] lemma nat.coe_prod {R : Type*} [comm_semiring R] (f : α → ℕ) (s : finset α) : (↑∏ i in s, f i : R) = ∏ i in s, f i := (nat.cast_ring_hom R).map_prod _ _ @[simp, norm_cast] lemma int.coe_prod {R : Type*} [comm_ring R] (f : α → ℤ) (s : finset α) : (↑∏ i in s, f i : R) = ∏ i in s, f i := (int.cast_ring_hom R).map_prod _ _ @[simp, norm_cast] lemma units.coe_prod {M : Type*} [comm_monoid M] (f : α → units M) (s : finset α) : (↑∏ i in s, f i : M) = ∏ i in s, f i := (units.coe_hom M).map_prod _ _ lemma nat_abs_sum_le {ι : Type*} (s : finset ι) (f : ι → ℤ) : (∑ i in s, f i).nat_abs ≤ ∑ i in s, (f i).nat_abs := begin classical, apply finset.induction_on s, { simp only [finset.sum_empty, int.nat_abs_zero] }, { intros i s his IH, simp only [his, finset.sum_insert, not_false_iff], exact (int.nat_abs_add_le _ _).trans (add_le_add le_rfl IH) } end
0204600202c8cef4729c7bf964f9563d73a4f54a
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/data/nat/pow.lean
f306cab9f46bb7bbabd9c1e3c3d4e63e665e0beb
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,664
lean
/- Copyright (c) 2014 Floris van Doorn (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad, Mario Carneiro -/ import data.nat.basic import algebra.group_power.order /-! # `nat.pow` Results on the power operation on natural numbers. -/ namespace nat /-! ### `pow` -/ -- This is redundant with `canonically_ordered_comm_semiring.pow_le_pow_of_le_left`, -- but `canonically_ordered_comm_semiring` is not such an obvious abstraction, and also quite long. -- So, we leave a version in the `nat` namespace as well. -- (The global `pow_le_pow_of_le_left` needs an extra hypothesis `0 ≤ x`.) protected theorem pow_le_pow_of_le_left {x y : ℕ} (H : x ≤ y) : ∀ i : ℕ, x^i ≤ y^i := canonically_ordered_comm_semiring.pow_le_pow_of_le_left H theorem pow_le_pow_of_le_right {x : ℕ} (H : x > 0) {i : ℕ} : ∀ {j}, i ≤ j → x^i ≤ x^j | 0 h := by rw nat.eq_zero_of_le_zero h; apply le_refl | (succ j) h := h.lt_or_eq_dec.elim (λhl, by rw [pow_succ', ← nat.mul_one (x^i)]; exact nat.mul_le_mul (pow_le_pow_of_le_right $ le_of_lt_succ hl) H) (λe, by rw e; refl) theorem pow_lt_pow_of_lt_left {x y : ℕ} (H : x < y) {i} (h : 0 < i) : x^i < y^i := begin cases i with i, { exact absurd h (nat.not_lt_zero _) }, rw [pow_succ', pow_succ'], exact nat.mul_lt_mul' (nat.pow_le_pow_of_le_left (le_of_lt H) _) H (pow_pos (lt_of_le_of_lt (zero_le _) H) _) end theorem pow_lt_pow_of_lt_right {x : ℕ} (H : x > 1) {i j : ℕ} (h : i < j) : x^i < x^j := begin have xpos := lt_of_succ_lt H, refine lt_of_lt_of_le _ (pow_le_pow_of_le_right xpos h), rw [← nat.mul_one (x^i), pow_succ'], exact nat.mul_lt_mul_of_pos_left H (pow_pos xpos _) end -- TODO: Generalize? lemma pow_lt_pow_succ {p : ℕ} (h : 1 < p) (n : ℕ) : p^n < p^(n+1) := suffices 1*p^n < p*p^n, by simpa [pow_succ], nat.mul_lt_mul_of_pos_right h (pow_pos (lt_of_succ_lt h) n) lemma lt_pow_self {p : ℕ} (h : 1 < p) : ∀ n : ℕ, n < p ^ n | 0 := by simp [zero_lt_one] | (n+1) := calc n + 1 < p^n + 1 : nat.add_lt_add_right (lt_pow_self _) _ ... ≤ p ^ (n+1) : pow_lt_pow_succ h _ lemma lt_two_pow (n : ℕ) : n < 2^n := lt_pow_self dec_trivial n lemma one_le_pow (n m : ℕ) (h : 0 < m) : 1 ≤ m^n := by { rw ←one_pow n, exact nat.pow_le_pow_of_le_left h n } lemma one_le_pow' (n m : ℕ) : 1 ≤ (m+1)^n := one_le_pow n (m+1) (succ_pos m) lemma one_le_two_pow (n : ℕ) : 1 ≤ 2^n := one_le_pow n 2 dec_trivial lemma one_lt_pow (n m : ℕ) (h₀ : 0 < n) (h₁ : 1 < m) : 1 < m^n := by { rw ←one_pow n, exact pow_lt_pow_of_lt_left h₁ h₀ } lemma one_lt_pow' (n m : ℕ) : 1 < (m+2)^(n+1) := one_lt_pow (n+1) (m+2) (succ_pos n) (nat.lt_of_sub_eq_succ rfl) lemma one_lt_two_pow (n : ℕ) (h₀ : 0 < n) : 1 < 2^n := one_lt_pow n 2 h₀ dec_trivial lemma one_lt_two_pow' (n : ℕ) : 1 < 2^(n+1) := one_lt_pow (n+1) 2 (succ_pos n) dec_trivial lemma pow_right_strict_mono {x : ℕ} (k : 2 ≤ x) : strict_mono (λ (n : ℕ), x^n) := λ _ _, pow_lt_pow_of_lt_right k lemma pow_le_iff_le_right {x m n : ℕ} (k : 2 ≤ x) : x^m ≤ x^n ↔ m ≤ n := strict_mono.le_iff_le (pow_right_strict_mono k) lemma pow_lt_iff_lt_right {x m n : ℕ} (k : 2 ≤ x) : x^m < x^n ↔ m < n := strict_mono.lt_iff_lt (pow_right_strict_mono k) lemma pow_right_injective {x : ℕ} (k : 2 ≤ x) : function.injective (λ (n : ℕ), x^n) := strict_mono.injective (pow_right_strict_mono k) lemma pow_left_strict_mono {m : ℕ} (k : 1 ≤ m) : strict_mono (λ (x : ℕ), x^m) := λ _ _ h, pow_lt_pow_of_lt_left h k lemma mul_lt_mul_pow_succ {n a q : ℕ} (a0 : 0 < a) (q1 : 1 < q) : n * q < a * q ^ (n + 1) := begin rw [pow_succ', ← mul_assoc, mul_lt_mul_right (zero_lt_one.trans q1)], exact lt_mul_of_one_le_of_lt (nat.succ_le_iff.mpr a0) (nat.lt_pow_self q1 n), end end nat lemma strict_mono.nat_pow {n : ℕ} (hn : 1 ≤ n) {f : ℕ → ℕ} (hf : strict_mono f) : strict_mono (λ m, (f m) ^ n) := (nat.pow_left_strict_mono hn).comp hf namespace nat lemma pow_le_iff_le_left {m x y : ℕ} (k : 1 ≤ m) : x^m ≤ y^m ↔ x ≤ y := strict_mono.le_iff_le (pow_left_strict_mono k) lemma pow_lt_iff_lt_left {m x y : ℕ} (k : 1 ≤ m) : x^m < y^m ↔ x < y := strict_mono.lt_iff_lt (pow_left_strict_mono k) lemma pow_left_injective {m : ℕ} (k : 1 ≤ m) : function.injective (λ (x : ℕ), x^m) := strict_mono.injective (pow_left_strict_mono k) theorem sq_sub_sq (a b : ℕ) : a ^ 2 - b ^ 2 = (a + b) * (a - b) := by { rw [sq, sq], exact nat.mul_self_sub_mul_self_eq a b } alias nat.sq_sub_sq ← nat.pow_two_sub_pow_two /-! ### `pow` and `mod` / `dvd` -/ theorem mod_pow_succ {b : ℕ} (b_pos : 0 < b) (w m : ℕ) : m % (b^succ w) = b * (m/b % b^w) + m % b := begin apply nat.strong_induction_on m, clear m, intros p IH, cases lt_or_ge p (b^succ w) with h₁ h₁, -- base case: p < b^succ w { have h₂ : p / b < b^w, { rw [div_lt_iff_lt_mul p _ b_pos], simpa [pow_succ'] using h₁ }, rw [mod_eq_of_lt h₁, mod_eq_of_lt h₂], simp [div_add_mod] }, -- step: p ≥ b^succ w { -- Generate condition for induction hypothesis have h₂ : p - b^succ w < p, { apply nat.sub_lt_of_pos_le _ _ (pow_pos b_pos _) h₁ }, -- Apply induction rw [mod_eq_sub_mod h₁, IH _ h₂], -- Normalize goal and h1 simp only [pow_succ], simp only [ge, pow_succ] at h₁, -- Pull subtraction outside mod and div rw [sub_mul_mod _ _ _ h₁, sub_mul_div _ _ _ h₁], -- Cancel subtraction inside mod b^w have p_b_ge : b^w ≤ p / b, { rw [le_div_iff_mul_le _ _ b_pos, mul_comm], exact h₁ }, rw [eq.symm (mod_eq_sub_mod p_b_ge)] } end lemma pow_dvd_pow_iff_pow_le_pow {k l : ℕ} : Π {x : ℕ} (w : 0 < x), x^k ∣ x^l ↔ x^k ≤ x^l | (x+1) w := begin split, { intro a, exact le_of_dvd (pow_pos (succ_pos x) l) a, }, { intro a, cases x with x, { simp only [one_pow], }, { have le := (pow_le_iff_le_right (nat.le_add_left _ _)).mp a, use (x+2)^(l-k), rw [←pow_add, add_comm k, nat.sub_add_cancel le], } } end /-- If `1 < x`, then `x^k` divides `x^l` if and only if `k` is at most `l`. -/ lemma pow_dvd_pow_iff_le_right {x k l : ℕ} (w : 1 < x) : x^k ∣ x^l ↔ k ≤ l := by rw [pow_dvd_pow_iff_pow_le_pow (lt_of_succ_lt w), pow_le_iff_le_right w] lemma pow_dvd_pow_iff_le_right' {b k l : ℕ} : (b+2)^k ∣ (b+2)^l ↔ k ≤ l := pow_dvd_pow_iff_le_right (nat.lt_of_sub_eq_succ rfl) lemma not_pos_pow_dvd : ∀ {p k : ℕ} (hp : 1 < p) (hk : 1 < k), ¬ p^k ∣ p | (succ p) (succ k) hp hk h := have succ p * (succ p)^k ∣ succ p * 1, by simpa [pow_succ] using h, have (succ p) ^ k ∣ 1, from dvd_of_mul_dvd_mul_left (succ_pos _) this, have he : (succ p) ^ k = 1, from eq_one_of_dvd_one this, have k < (succ p) ^ k, from lt_pow_self hp k, have k < 1, by rwa [he] at this, have k = 0, from nat.eq_zero_of_le_zero $ le_of_lt_succ this, have 1 < 1, by rwa [this] at hk, absurd this dec_trivial lemma pow_dvd_of_le_of_pow_dvd {p m n k : ℕ} (hmn : m ≤ n) (hdiv : p ^ n ∣ k) : p ^ m ∣ k := have p ^ m ∣ p ^ n, from pow_dvd_pow _ hmn, dvd_trans this hdiv lemma dvd_of_pow_dvd {p k m : ℕ} (hk : 1 ≤ k) (hpk : p^k ∣ m) : p ∣ m := by rw ←pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk lemma pow_div {x m n : ℕ} (h : n ≤ m) (hx : 0 < x) : x ^ m / x ^ n = x ^ (m - n) := by rw [nat.div_eq_iff_eq_mul_left (pow_pos hx n) (pow_dvd_pow _ h), pow_sub_mul_pow _ h] /-! ### `shiftl` and `shiftr` -/ lemma shiftl_eq_mul_pow (m) : ∀ n, shiftl m n = m * 2 ^ n | 0 := (nat.mul_one _).symm | (k+1) := show bit0 (shiftl m k) = m * (2 * 2 ^ k), by rw [bit0_val, shiftl_eq_mul_pow, mul_left_comm, mul_comm 2] lemma shiftl'_tt_eq_mul_pow (m) : ∀ n, shiftl' tt m n + 1 = (m + 1) * 2 ^ n | 0 := by simp [shiftl, shiftl', pow_zero, nat.one_mul] | (k+1) := begin change bit1 (shiftl' tt m k) + 1 = (m + 1) * (2 * 2 ^ k), rw bit1_val, change 2 * (shiftl' tt m k + 1) = _, rw [shiftl'_tt_eq_mul_pow, mul_left_comm, mul_comm 2], end lemma one_shiftl (n) : shiftl 1 n = 2 ^ n := (shiftl_eq_mul_pow _ _).trans (nat.one_mul _) @[simp] lemma zero_shiftl (n) : shiftl 0 n = 0 := (shiftl_eq_mul_pow _ _).trans (nat.zero_mul _) lemma shiftr_eq_div_pow (m) : ∀ n, shiftr m n = m / 2 ^ n | 0 := (nat.div_one _).symm | (k+1) := (congr_arg div2 (shiftr_eq_div_pow k)).trans $ by rw [div2_val, nat.div_div_eq_div_mul, mul_comm]; refl @[simp] lemma zero_shiftr (n) : shiftr 0 n = 0 := (shiftr_eq_div_pow _ _).trans (nat.zero_div _) theorem shiftl'_ne_zero_left (b) {m} (h : m ≠ 0) (n) : shiftl' b m n ≠ 0 := by induction n; simp [shiftl', bit_ne_zero, *] theorem shiftl'_tt_ne_zero (m) : ∀ {n} (h : n ≠ 0), shiftl' tt m n ≠ 0 | 0 h := absurd rfl h | (succ n) _ := nat.bit1_ne_zero _ /-! ### `size` -/ @[simp] theorem size_zero : size 0 = 0 := by simp [size] @[simp] theorem size_bit {b n} (h : bit b n ≠ 0) : size (bit b n) = succ (size n) := begin rw size, conv { to_lhs, rw [binary_rec], simp [h] }, rw div2_bit, end @[simp] theorem size_bit0 {n} (h : n ≠ 0) : size (bit0 n) = succ (size n) := @size_bit ff n (nat.bit0_ne_zero h) @[simp] theorem size_bit1 (n) : size (bit1 n) = succ (size n) := @size_bit tt n (nat.bit1_ne_zero n) @[simp] theorem size_one : size 1 = 1 := show size (bit1 0) = 1, by rw [size_bit1, size_zero] @[simp] theorem size_shiftl' {b m n} (h : shiftl' b m n ≠ 0) : size (shiftl' b m n) = size m + n := begin induction n with n IH; simp [shiftl'] at h ⊢, rw [size_bit h, nat.add_succ], by_cases s0 : shiftl' b m n = 0; [skip, rw [IH s0]], rw s0 at h ⊢, cases b, {exact absurd rfl h}, have : shiftl' tt m n + 1 = 1 := congr_arg (+1) s0, rw [shiftl'_tt_eq_mul_pow] at this, have m0 := succ.inj (eq_one_of_dvd_one ⟨_, this.symm⟩), subst m0, simp at this, have : n = 0 := nat.eq_zero_of_le_zero (le_of_not_gt $ λ hn, ne_of_gt (pow_lt_pow_of_lt_right dec_trivial hn) this), subst n, refl end @[simp] theorem size_shiftl {m} (h : m ≠ 0) (n) : size (shiftl m n) = size m + n := size_shiftl' (shiftl'_ne_zero_left _ h _) theorem lt_size_self (n : ℕ) : n < 2^size n := begin rw [← one_shiftl], have : ∀ {n}, n = 0 → n < shiftl 1 (size n), { simp }, apply binary_rec _ _ n, {apply this rfl}, intros b n IH, by_cases bit b n = 0, {apply this h}, rw [size_bit h, shiftl_succ], exact bit_lt_bit0 _ IH end theorem size_le {m n : ℕ} : size m ≤ n ↔ m < 2^n := ⟨λ h, lt_of_lt_of_le (lt_size_self _) (pow_le_pow_of_le_right dec_trivial h), begin rw [← one_shiftl], revert n, apply binary_rec _ _ m, { intros n h, simp }, { intros b m IH n h, by_cases e : bit b m = 0, { simp [e] }, rw [size_bit e], cases n with n, { exact e.elim (nat.eq_zero_of_le_zero (le_of_lt_succ h)) }, { apply succ_le_succ (IH _), apply lt_imp_lt_of_le_imp_le (λ h', bit0_le_bit _ h') h } } end⟩ theorem lt_size {m n : ℕ} : m < size n ↔ 2^m ≤ n := by rw [← not_lt, decidable.iff_not_comm, not_lt, size_le] theorem size_pos {n : ℕ} : 0 < size n ↔ 0 < n := by rw lt_size; refl theorem size_eq_zero {n : ℕ} : size n = 0 ↔ n = 0 := by have := @size_pos n; simp [pos_iff_ne_zero] at this; exact decidable.not_iff_not.1 this theorem size_pow {n : ℕ} : size (2^n) = n+1 := le_antisymm (size_le.2 $ pow_lt_pow_of_lt_right dec_trivial (lt_succ_self _)) (lt_size.2 $ le_refl _) theorem size_le_size {m n : ℕ} (h : m ≤ n) : size m ≤ size n := size_le.2 $ lt_of_le_of_lt h (lt_size_self _) end nat
6fae0ba3db6bd76648b384c4d69f969c23c6be1f
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/argNameIfMacroScopes.lean
0b414141a7134d1e9db9ac0774c5d7c5e268bf9c
[ "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
68
lean
def g : Nat → Nat := fun _ => 0 def f (x : Nat) : Nat := g _
e8ae9c77003c75d770109616dd02cab97715e2ee
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/data/nat/choose/dvd.lean
4a2403863bc0e0a0470de39805118980fb7e40d5
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,588
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Patrick Stevens -/ import data.nat.choose.basic import data.nat.prime /-! # Divisibility properties of binomial coefficients -/ namespace nat open_locale nat namespace prime lemma dvd_choose_add {p a b : ℕ} (hap : a < p) (hbp : b < p) (h : p ≤ a + b) (hp : prime p) : p ∣ choose (a + b) a := have h₁ : p ∣ (a + b)!, from hp.dvd_factorial.2 h, have h₂ : ¬p ∣ a!, from mt hp.dvd_factorial.1 (not_le_of_gt hap), have h₃ : ¬p ∣ b!, from mt hp.dvd_factorial.1 (not_le_of_gt hbp), by rw [← choose_mul_factorial_mul_factorial (le.intro rfl), mul_assoc, hp.dvd_mul, hp.dvd_mul, nat.add_sub_cancel_left a b] at h₁; exact h₁.resolve_right (not_or_distrib.2 ⟨h₂, h₃⟩) lemma dvd_choose_self {p k : ℕ} (hk : 0 < k) (hkp : k < p) (hp : prime p) : p ∣ choose p k := begin have r : k + (p - k) = p, by rw [← nat.add_sub_assoc (nat.le_of_lt hkp) k, nat.add_sub_cancel_left], have e : p ∣ choose (k + (p - k)) k, by exact dvd_choose_add hkp (nat.sub_lt (hk.trans hkp) hk) (by rw r) hp, rwa r at e, end end prime lemma cast_choose {α : Type*} [field α] [char_zero α] {a b : ℕ} (hab : a ≤ b) : (b.choose a : α) = b! / (a! * (b - a)!) := begin rw [eq_comm, div_eq_iff], norm_cast, rw [←mul_assoc, choose_mul_factorial_mul_factorial hab], { exact mul_ne_zero (nat.cast_ne_zero.2 $ factorial_ne_zero _) (nat.cast_ne_zero.2 $ factorial_ne_zero _) } end end nat
3de7474f24e191aa92a6c127d9a0a5aeee7857b7
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/PrettyPrinter/Formatter.lean
50c1757f0c7de5d4ab7e1a7132fc6e3b01e6d2e8
[ "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
23,372
lean
/- Copyright (c) 2020 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Lean.CoreM import Lean.Parser.Extension import Lean.KeyedDeclsAttribute import Lean.ParserCompiler.Attribute import Lean.PrettyPrinter.Basic /-! The formatter turns a `Syntax` tree into a `Format` object, inserting both mandatory whitespace (to separate adjacent tokens) as well as "pretty" optional whitespace. The basic approach works much like the parenthesizer: A right-to-left traversal over the syntax tree, driven by parser-specific handlers registered via attributes. The traversal is right-to-left so that when emitting a token, we already know the text following it and can decide whether or not whitespace between the two is necessary. -/ namespace Lean namespace PrettyPrinter namespace Formatter structure Context where options : Options table : Parser.TokenTable structure State where stxTrav : Syntax.Traverser -- Textual content of `stack` up to the first whitespace (not enclosed in an escaped ident). We assume that the textual -- content of `stack` is modified only by `pushText` and `pushLine`, so `leadWord` is adjusted there accordingly. leadWord : String := "" -- Whether the generated format begins with the result of an ungrouped category formatter. isUngrouped : Bool := false -- Whether the resulting format must be grouped when used in a category formatter. -- If the flag is set to false, then categoryParser omits the fill+nest operation. mustBeGrouped : Bool := true -- Stack of generated Format objects, analogous to the Syntax stack in the parser. -- Note, however, that the stack is reversed because of the right-to-left traversal. stack : Array Format := #[] end Formatter abbrev FormatterM := ReaderT Formatter.Context $ StateRefT Formatter.State CoreM @[inline] def FormatterM.orElse {α} (p₁ : FormatterM α) (p₂ : Unit → FormatterM α) : FormatterM α := do let s ← get catchInternalId backtrackExceptionId p₁ (fun _ => do set s; p₂ ()) instance {α} : OrElse (FormatterM α) := ⟨FormatterM.orElse⟩ abbrev Formatter := FormatterM Unit unsafe def mkFormatterAttribute : IO (KeyedDeclsAttribute Formatter) := KeyedDeclsAttribute.init { builtinName := `builtinFormatter, name := `formatter, descr := "Register a formatter for a parser. [formatter k] registers a declaration of type `Lean.PrettyPrinter.Formatter` for the `SyntaxNodeKind` `k`.", valueTypeName := `Lean.PrettyPrinter.Formatter, evalKey := fun builtin stx => do let env ← getEnv let id ← Attribute.Builtin.getId stx -- `isValidSyntaxNodeKind` is updated only in the next stage for new `[builtin*Parser]`s, but we try to -- synthesize a formatter for it immediately, so we just check for a declaration in this case if (builtin && (env.find? id).isSome) || Parser.isValidSyntaxNodeKind env id then pure id else throwError "invalid [formatter] argument, unknown syntax kind '{id}'" } `Lean.PrettyPrinter.formatterAttribute @[builtinInit mkFormatterAttribute] opaque formatterAttribute : KeyedDeclsAttribute Formatter unsafe def mkCombinatorFormatterAttribute : IO ParserCompiler.CombinatorAttribute := ParserCompiler.registerCombinatorAttribute `combinatorFormatter "Register a formatter for a parser combinator. [combinatorFormatter c] registers a declaration of type `Lean.PrettyPrinter.Formatter` for the `Parser` declaration `c`. Note that, unlike with [formatter], this is not a node kind since combinators usually do not introduce their own node kinds. The tagged declaration may optionally accept parameters corresponding to (a prefix of) those of `c`, where `Parser` is replaced with `Formatter` in the parameter types." @[builtinInit mkCombinatorFormatterAttribute] opaque combinatorFormatterAttribute : ParserCompiler.CombinatorAttribute namespace Formatter open Lean.Core open Lean.Parser def throwBacktrack {α} : FormatterM α := throw $ Exception.internal backtrackExceptionId instance : Syntax.MonadTraverser FormatterM := ⟨{ get := State.stxTrav <$> get, set := fun t => modify (fun st => { st with stxTrav := t }), modifyGet := fun f => modifyGet (fun st => let (a, t) := f st.stxTrav; (a, { st with stxTrav := t })) }⟩ open Syntax.MonadTraverser def getStack : FormatterM (Array Format) := do let st ← get pure st.stack def getStackSize : FormatterM Nat := do let stack ← getStack; pure stack.size def setStack (stack : Array Format) : FormatterM Unit := modify fun st => { st with stack := stack } private def push (f : Format) : FormatterM Unit := modify fun st => { st with stack := st.stack.push f, isUngrouped := false } def pushWhitespace (f : Format) : FormatterM Unit := do push f modify fun st => { st with leadWord := "", isUngrouped := false } def pushLine : FormatterM Unit := pushWhitespace Format.line /-- Execute `x` at the right-most child of the current node, if any, then advance to the left. -/ def visitArgs (x : FormatterM Unit) : FormatterM Unit := do let stx ← getCur if stx.getArgs.size > 0 then goDown (stx.getArgs.size - 1) *> x <* goUp goLeft /-- Execute `x`, pass array of generated Format objects to `fn`, and push result. -/ def fold (fn : Array Format → Format) (x : FormatterM Unit) : FormatterM Unit := do let sp ← getStackSize x let stack ← getStack let f := fn $ stack.extract sp stack.size setStack $ (stack.shrink sp).push f /-- Execute `x` and concatenate generated Format objects. -/ def concat (x : FormatterM Unit) : FormatterM Unit := do fold (Array.foldl (fun acc f => if acc.isNil then f else f ++ acc) Format.nil) x def indent (x : Formatter) (indent : Option Int := none) : Formatter := do concat x let ctx ← read let indent := indent.getD $ Std.Format.getIndent ctx.options modify fun st => { st with stack := st.stack.modify (st.stack.size - 1) (Format.nest indent) } def fill (x : Formatter) : Formatter := do concat x modify fun st => { st with stack := st.stack.modify (st.stack.size - 1) Format.fill isUngrouped := false } def group (x : Formatter) : Formatter := do concat x modify fun st => { st with stack := st.stack.modify (st.stack.size - 1) Format.group isUngrouped := false } /-- If `pos?` has a position, run `x` and tag its results with that position, if they are not already tagged. Otherwise just run `x`. -/ def withMaybeTag (pos? : Option String.Pos) (x : FormatterM Unit) : Formatter := do if let some p := pos? then concat x modify fun st => { st with stack := st.stack.modify (st.stack.size - 1) fun fmt => if fmt matches Format.tag .. then fmt else Format.tag p.byteIdx fmt } else x @[combinatorFormatter Lean.Parser.orelse] def orelse.formatter (p1 p2 : Formatter) : Formatter := -- HACK: We have no (immediate) information on which side of the orelse could have produced the current node, so try -- them in turn. Uses the syntax traverser non-linearly! p1 <|> p2 -- `mkAntiquot` is quite complex, so we'd rather have its formatter synthesized below the actual parser definition. -- Note that there is a mutual recursion -- `categoryParser -> mkAntiquot -> termParser -> categoryParser`, so we need to introduce an indirection somewhere -- anyway. @[extern "lean_mk_antiquot_formatter"] opaque mkAntiquot.formatter' (name : String) (kind : SyntaxNodeKind) (anonymous := true) (isPseudoKind := false) : Formatter -- break up big mutual recursion @[extern "lean_pretty_printer_formatter_interpret_parser_descr"] opaque interpretParserDescr' : ParserDescr → CoreM Formatter private def SourceInfo.getExprPos? : SourceInfo → Option String.Pos | SourceInfo.synthetic pos _ => pos | _ => none private def getExprPos? : Syntax → Option String.Pos | Syntax.node info _ _ => SourceInfo.getExprPos? info | Syntax.atom info _ => SourceInfo.getExprPos? info | Syntax.ident info _ _ _ => SourceInfo.getExprPos? info | Syntax.missing => none unsafe def formatterForKindUnsafe (k : SyntaxNodeKind) : Formatter := do if k == `missing then push "<missing>" goLeft else let stx ← getCur let f ← runForNodeKind formatterAttribute k interpretParserDescr' withMaybeTag (getExprPos? stx) f @[implementedBy formatterForKindUnsafe] opaque formatterForKind (k : SyntaxNodeKind) : Formatter @[combinatorFormatter Lean.Parser.withAntiquot] def withAntiquot.formatter (antiP p : Formatter) : Formatter := -- TODO: could be optimized using `isAntiquot` (which would have to be moved), but I'd rather -- fix the backtracking hack outright. orelse.formatter antiP p @[combinatorFormatter Lean.Parser.withAntiquotSuffixSplice] def withAntiquotSuffixSplice.formatter (_ : SyntaxNodeKind) (p suffix : Formatter) : Formatter := do if (← getCur).isAntiquotSuffixSplice then visitArgs <| suffix *> p else p @[combinatorFormatter Lean.Parser.tokenWithAntiquot] def tokenWithAntiquot.formatter (p : Formatter) : Formatter := do if (← getCur).isTokenAntiquot then visitArgs p else p def categoryFormatterCore (cat : Name) : Formatter := do modify fun st => { st with mustBeGrouped := true, isUngrouped := false } let stx ← getCur trace[PrettyPrinter.format] "formatting {indentD (format stx)}" if stx.getKind == `choice then visitArgs do -- format only last choice -- TODO: We could use elaborator data here to format the chosen child when available formatterForKind (← getCur).getKind else if cat == `rawStx then withAntiquot.formatter (mkAntiquot.formatter' cat.toString cat (isPseudoKind := true)) (push stx.formatStx *> goLeft) else withAntiquot.formatter (mkAntiquot.formatter' cat.toString cat (isPseudoKind := true)) (formatterForKind stx.getKind) modify fun st => { st with mustBeGrouped := true, isUngrouped := !st.mustBeGrouped } @[combinatorFormatter Lean.Parser.categoryParser] def categoryParser.formatter (cat : Name) : Formatter := do concat <| categoryFormatterCore cat unless (← get).isUngrouped do let indent := Std.Format.getIndent (← read).options modify fun st => { st with stack := st.stack.modify (st.stack.size - 1) fun fmt => fmt.nest indent |>.fill } def categoryFormatter (cat : Name) : Formatter := fill <| indent <| categoryFormatterCore cat @[combinatorFormatter Lean.Parser.categoryParserOfStack] def categoryParserOfStack.formatter (offset : Nat) : Formatter := do let st ← get let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset) categoryParser.formatter stx.getId @[combinatorFormatter Lean.Parser.parserOfStack] def parserOfStack.formatter (offset : Nat) (_prec : Nat := 0) : Formatter := do let st ← get let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset) formatterForKind stx.getKind @[combinatorFormatter Lean.Parser.error] def error.formatter (_msg : String) : Formatter := pure () @[combinatorFormatter Lean.Parser.errorAtSavedPos] def errorAtSavedPos.formatter (_msg : String) (_delta : Bool) : Formatter := pure () @[combinatorFormatter Lean.Parser.atomic] def atomic.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.lookahead] def lookahead.formatter (_ : Formatter) : Formatter := pure () @[combinatorFormatter Lean.Parser.notFollowedBy] def notFollowedBy.formatter (_ : Formatter) : Formatter := pure () @[combinatorFormatter Lean.Parser.andthen] def andthen.formatter (p1 p2 : Formatter) : Formatter := p2 *> p1 def checkKind (k : SyntaxNodeKind) : FormatterM Unit := do let stx ← getCur if k != stx.getKind then trace[PrettyPrinter.format.backtrack] "unexpected node kind '{stx.getKind}', expected '{k}'" throwBacktrack @[combinatorFormatter Lean.Parser.node] def node.formatter (k : SyntaxNodeKind) (p : Formatter) : Formatter := do checkKind k; visitArgs p @[combinatorFormatter Lean.Parser.trailingNode] def trailingNode.formatter (k : SyntaxNodeKind) (_ _ : Nat) (p : Formatter) : Formatter := do checkKind k visitArgs do p; -- leading term, not actually produced by `p` categoryParser.formatter `foo def parseToken (s : String) : FormatterM ParserState := -- include comment tokens, e.g. when formatting `- -0` return (Parser.andthenFn Parser.whitespace (Parser.tokenFn [])) { input := s, fileName := "", fileMap := FileMap.ofString "", prec := 0, env := ← getEnv, options := ← getOptions, tokens := (← read).table } (Parser.mkParserState s) def pushToken (info : SourceInfo) (tk : String) : FormatterM Unit := do match info with | SourceInfo.original _ _ ss _ => -- preserve non-whitespace content (i.e. comments) let ss' := ss.trim if !ss'.isEmpty then let ws := { ss with startPos := ss'.stopPos } if ws.contains '\n' then push s!"\n{ss'}" else push s!" {ss'}" modify fun st => { st with leadWord := "" } | _ => pure () let st ← get -- If there is no space between `tk` and the next word, see if we would parse more than `tk` as a single token if st.leadWord != "" && tk.trimRight == tk then let tk' := tk.trimLeft let t ← parseToken $ tk' ++ st.leadWord if t.pos <= tk'.endPos then -- stopped within `tk` => use it as is, extend `leadWord` if not prefixed by whitespace push tk modify fun st => { st with leadWord := if tk.trimLeft == tk then tk ++ st.leadWord else "" } else -- stopped after `tk` => add space push $ tk ++ " " modify fun st => { st with leadWord := if tk.trimLeft == tk then tk else "" } else -- already separated => use `tk` as is if st.leadWord == "" then push tk.trimRight else if tk.endsWith " " then pushLine push tk.trimRight else push tk -- preserve special whitespace for tokens like ":=\n" modify fun st => { st with leadWord := if tk.trimLeft == tk then tk else "" } match info with | SourceInfo.original ss _ _ _ => -- preserve non-whitespace content (i.e. comments) let ss' := ss.trim if !ss'.isEmpty then let ws := { ss with startPos := ss'.stopPos } if ws.contains '\n' then do -- Indentation is automatically increased when entering a category, but comments should be aligned -- with the actual token, so dedent indent (push s!"{ss'}\n") (some ((0:Int) - Std.Format.getIndent (← getOptions))) else pushLine push ss'.toString modify fun st => { st with leadWord := "" } | _ => pure () @[combinatorFormatter Lean.Parser.symbolNoAntiquot] def symbolNoAntiquot.formatter (sym : String) : Formatter := do let stx ← getCur if stx.isToken sym then do let (Syntax.atom info _) ← pure stx | unreachable! withMaybeTag (getExprPos? stx) (pushToken info sym) goLeft else do trace[PrettyPrinter.format.backtrack] "unexpected syntax '{format stx}', expected symbol '{sym}'" throwBacktrack @[combinatorFormatter Lean.Parser.nonReservedSymbolNoAntiquot] def nonReservedSymbolNoAntiquot.formatter := symbolNoAntiquot.formatter @[combinatorFormatter Lean.Parser.rawCh] def rawCh.formatter (ch : Char) := symbolNoAntiquot.formatter ch.toString @[combinatorFormatter Lean.Parser.unicodeSymbolNoAntiquot] def unicodeSymbolNoAntiquot.formatter (sym asciiSym : String) : Formatter := do let Syntax.atom info val ← getCur | throwError m!"not an atom: {← getCur}" if val == sym.trim then pushToken info sym else pushToken info asciiSym; goLeft @[combinatorFormatter Lean.Parser.identNoAntiquot] def identNoAntiquot.formatter : Formatter := do checkKind identKind let stx@(Syntax.ident info _ id _) ← getCur | throwError m!"not an ident: {← getCur}" let id := id.simpMacroScopes withMaybeTag (getExprPos? stx) (pushToken info id.toString) goLeft @[combinatorFormatter Lean.Parser.rawIdentNoAntiquot] def rawIdentNoAntiquot.formatter : Formatter := do checkKind identKind let Syntax.ident info _ id _ ← getCur | throwError m!"not an ident: {← getCur}" pushToken info id.toString goLeft @[combinatorFormatter Lean.Parser.identEq] def identEq.formatter (_id : Name) := rawIdentNoAntiquot.formatter def visitAtom (k : SyntaxNodeKind) : Formatter := do let stx ← getCur if k != Name.anonymous then checkKind k let Syntax.atom info val ← pure $ stx.ifNode (fun n => n.getArg 0) (fun _ => stx) | throwError m!"not an atom: {stx}" pushToken info val goLeft @[combinatorFormatter Lean.Parser.charLitNoAntiquot] def charLitNoAntiquot.formatter := visitAtom charLitKind @[combinatorFormatter Lean.Parser.strLitNoAntiquot] def strLitNoAntiquot.formatter := visitAtom strLitKind @[combinatorFormatter Lean.Parser.nameLitNoAntiquot] def nameLitNoAntiquot.formatter := visitAtom nameLitKind @[combinatorFormatter Lean.Parser.numLitNoAntiquot] def numLitNoAntiquot.formatter := visitAtom numLitKind @[combinatorFormatter Lean.Parser.scientificLitNoAntiquot] def scientificLitNoAntiquot.formatter := visitAtom scientificLitKind @[combinatorFormatter Lean.Parser.fieldIdx] def fieldIdx.formatter := visitAtom fieldIdxKind @[combinatorFormatter Lean.Parser.manyNoAntiquot] def manyNoAntiquot.formatter (p : Formatter) : Formatter := do let stx ← getCur visitArgs $ stx.getArgs.size.forM fun _ => p @[combinatorFormatter Lean.Parser.many1NoAntiquot] def many1NoAntiquot.formatter (p : Formatter) : Formatter := manyNoAntiquot.formatter p @[combinatorFormatter Lean.Parser.optionalNoAntiquot] def optionalNoAntiquot.formatter (p : Formatter) : Formatter := visitArgs p @[combinatorFormatter Lean.Parser.many1Unbox] def many1Unbox.formatter (p : Formatter) : Formatter := do let stx ← getCur if stx.getKind == nullKind then do manyNoAntiquot.formatter p else p @[combinatorFormatter Lean.Parser.sepByNoAntiquot] def sepByNoAntiquot.formatter (p pSep : Formatter) : Formatter := do let stx ← getCur visitArgs <| (List.range stx.getArgs.size).reverse.forM fun i => if i % 2 == 0 then p else pSep @[combinatorFormatter Lean.Parser.sepBy1NoAntiquot] def sepBy1NoAntiquot.formatter := sepByNoAntiquot.formatter @[combinatorFormatter Lean.Parser.withPosition] def withPosition.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.withPositionAfterLinebreak] def withPositionAfterLinebreak.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.withoutPosition] def withoutPosition.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.withForbidden] def withForbidden.formatter (_tk : Token) (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.withoutForbidden] def withoutForbidden.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.withoutInfo] def withoutInfo.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.setExpected] def setExpected.formatter (_expected : List String) (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.incQuotDepth] def incQuotDepth.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.decQuotDepth] def decQuotDepth.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.suppressInsideQuot] def suppressInsideQuot.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.evalInsideQuot] def evalInsideQuot.formatter (_declName : Name) (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.checkWsBefore] def checkWsBefore.formatter : Formatter := do let st ← get if st.leadWord != "" then pushLine @[combinatorFormatter Lean.Parser.checkPrec] def checkPrec.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkLhsPrec] def checkLhsPrec.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.setLhsPrec] def setLhsPrec.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkStackTop] def checkStackTop.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkNoWsBefore] def checkNoWsBefore.formatter : Formatter := -- prevent automatic whitespace insertion modify fun st => { st with leadWord := "" } @[combinatorFormatter Lean.Parser.checkLinebreakBefore] def checkLinebreakBefore.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkTailWs] def checkTailWs.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkColGe] def checkColGe.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkColGt] def checkColGt.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkLineEq] def checkLineEq.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.eoi] def eoi.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkNoImmediateColon] def checkNoImmediateColon.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.skip] def skip.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.pushNone] def pushNone.formatter : Formatter := goLeft @[combinatorFormatter Lean.Parser.withOpenDecl] def withOpenDecl.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.withOpen] def withOpen.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.interpolatedStr] def interpolatedStr.formatter (p : Formatter) : Formatter := do visitArgs $ (← getCur).getArgs.reverse.forM fun chunk => match chunk.isLit? interpolatedStrLitKind with | some str => push str *> goLeft | none => p @[combinatorFormatter Lean.Parser.dbgTraceState] def dbgTraceState.formatter (_label : String) (p : Formatter) : Formatter := p @[combinatorFormatter ite, macroInline] def ite {_ : Type} (c : Prop) [Decidable c] (t e : Formatter) : Formatter := if c then t else e abbrev FormatterAliasValue := AliasValue Formatter builtin_initialize formatterAliasesRef : IO.Ref (NameMap FormatterAliasValue) ← IO.mkRef {} def registerAlias (aliasName : Name) (v : FormatterAliasValue) : IO Unit := do Parser.registerAliasCore formatterAliasesRef aliasName v instance : Coe Formatter FormatterAliasValue := { coe := AliasValue.const } instance : Coe (Formatter → Formatter) FormatterAliasValue := { coe := AliasValue.unary } instance : Coe (Formatter → Formatter → Formatter) FormatterAliasValue := { coe := AliasValue.binary } end Formatter open Formatter def format (formatter : Formatter) (stx : Syntax) : CoreM Format := do trace[PrettyPrinter.format.input] "{Std.format stx}" let options ← getOptions let table ← Parser.builtinTokenTable.get catchInternalId backtrackExceptionId (do let (_, st) ← (concat formatter { table := table, options := options }).run { stxTrav := Syntax.Traverser.fromSyntax stx }; pure $ Format.fill $ st.stack.get! 0) (fun _ => throwError "format: uncaught backtrack exception") def formatCategory (cat : Name) := format <| categoryFormatter cat def formatTerm := formatCategory `term def formatTactic := formatCategory `tactic def formatCommand := formatCategory `command builtin_initialize registerTraceClass `PrettyPrinter.format; end PrettyPrinter end Lean
06d2e1f349c8c26319d8d32d5f55b53b4062a1fa
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Data/LOption.lean
409752518b3865ead899116e8b85d695f7ce0428
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
800
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 -/ universe u namespace Lean inductive LOption (α : Type u) where | none : LOption α | some : α → LOption α | undef : LOption α deriving Inhabited, BEq instance [ToString α] : ToString (LOption α) where toString | LOption.none => "none" | LOption.undef => "undef" | LOption.some a => "(some " ++ toString a ++ ")" end Lean def Option.toLOption {α : Type u} : Option α → Lean.LOption α | none => Lean.LOption.none | some a => Lean.LOption.some a @[inline] def toLOptionM {α} {m : Type → Type} [Monad m] (x : m (Option α)) : m (Lean.LOption α) := do let b ← x pure b.toLOption
2e5df5169ee1515ebc4fadac7951f4e9e85e99b6
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/field_theory/is_alg_closed/spectrum.lean
8747b3ccb009f3bd7ff264afb02fcc9bd4a34d88
[ "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
7,683
lean
/- Copyright (c) 2021 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import algebra.algebra.spectrum import field_theory.is_alg_closed.basic /-! # Spectrum mapping theorem > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file develops proves the spectral mapping theorem for polynomials over algebraically closed fields. In particular, if `a` is an element of an `𝕜`-algebra `A` where `𝕜` is a field, and `p : 𝕜[X]` is a polynomial, then the spectrum of `polynomial.aeval a p` contains the image of the spectrum of `a` under `(λ k, polynomial.eval k p)`. When `𝕜` is algebraically closed, these are in fact equal (assuming either that the spectrum of `a` is nonempty or the polynomial has positive degree), which is the **spectral mapping theorem**. In addition, this file contains the fact that every element of a finite dimensional nontrivial algebra over an algebraically closed field has nonempty spectrum. In particular, this is used in `module.End.exists_eigenvalue` to show that every linear map from a vector space to itself has an eigenvalue. ## Main statements * `spectrum.subset_polynomial_aeval`, `spectrum.map_polynomial_aeval_of_degree_pos`, `spectrum.map_polynomial_aeval_of_nonempty`: variations on the **spectral mapping theorem**. * `spectrum.nonempty_of_is_alg_closed_of_finite_dimensional`: the spectrum is nonempty for any element of a nontrivial finite dimensional algebra over an algebraically closed field. ## Notations * `σ a` : `spectrum R a` of `a : A` -/ namespace spectrum open set polynomial open_locale pointwise polynomial universes u v section scalar_ring variables {R : Type u} {A : Type v} variables [comm_ring R] [ring A] [algebra R A] local notation `σ` := spectrum R local notation `↑ₐ` := algebra_map R A lemma exists_mem_of_not_is_unit_aeval_prod [is_domain R] {p : R[X]} {a : A} (hp : p ≠ 0) (h : ¬is_unit (aeval a (multiset.map (λ (x : R), X - C x) p.roots).prod)) : ∃ k : R, k ∈ σ a ∧ eval k p = 0 := begin rw [←multiset.prod_to_list, alg_hom.map_list_prod] at h, replace h := mt list.prod_is_unit h, simp only [not_forall, exists_prop, aeval_C, multiset.mem_to_list, list.mem_map, aeval_X, exists_exists_and_eq_and, multiset.mem_map, alg_hom.map_sub] at h, rcases h with ⟨r, r_mem, r_nu⟩, exact ⟨r, by rwa [mem_iff, ←is_unit.sub_iff], by rwa [←is_root.def, ←mem_roots hp]⟩ end end scalar_ring section scalar_field variables {𝕜 : Type u} {A : Type v} variables [field 𝕜] [ring A] [algebra 𝕜 A] local notation `σ` := spectrum 𝕜 local notation `↑ₐ` := algebra_map 𝕜 A open polynomial /-- Half of the spectral mapping theorem for polynomials. We prove it separately because it holds over any field, whereas `spectrum.map_polynomial_aeval_of_degree_pos` and `spectrum.map_polynomial_aeval_of_nonempty` need the field to be algebraically closed. -/ theorem subset_polynomial_aeval (a : A) (p : 𝕜[X]) : (λ k, eval k p) '' (σ a) ⊆ σ (aeval a p) := begin rintros _ ⟨k, hk, rfl⟩, let q := C (eval k p) - p, have hroot : is_root q k, by simp only [eval_C, eval_sub, sub_self, is_root.def], rw [←mul_div_eq_iff_is_root, ←neg_mul_neg, neg_sub] at hroot, have aeval_q_eq : ↑ₐ(eval k p) - aeval a p = aeval a q, by simp only [aeval_C, alg_hom.map_sub, sub_left_inj], rw [mem_iff, aeval_q_eq, ←hroot, aeval_mul], have hcomm := (commute.all (C k - X) (- (q / (X - C k)))).map (aeval a), apply mt (λ h, (hcomm.is_unit_mul_iff.mp h).1), simpa only [aeval_X, aeval_C, alg_hom.map_sub] using hk, end /-- The *spectral mapping theorem* for polynomials. Note: the assumption `degree p > 0` is necessary in case `σ a = ∅`, for then the left-hand side is `∅` and the right-hand side, assuming `[nontrivial A]`, is `{k}` where `p = polynomial.C k`. -/ theorem map_polynomial_aeval_of_degree_pos [is_alg_closed 𝕜] (a : A) (p : 𝕜[X]) (hdeg : 0 < degree p) : σ (aeval a p) = (λ k, eval k p) '' (σ a) := begin /- handle the easy direction via `spectrum.subset_polynomial_aeval` -/ refine set.eq_of_subset_of_subset (λ k hk, _) (subset_polynomial_aeval a p), /- write `C k - p` product of linear factors and a constant; show `C k - p ≠ 0`. -/ have hprod := eq_prod_roots_of_splits_id (is_alg_closed.splits (C k - p)), have h_ne : C k - p ≠ 0, from ne_zero_of_degree_gt (by rwa [degree_sub_eq_right_of_degree_lt (lt_of_le_of_lt degree_C_le hdeg)]), have lead_ne := leading_coeff_ne_zero.mpr h_ne, have lead_unit := (units.map (↑ₐ).to_monoid_hom (units.mk0 _ lead_ne)).is_unit, /- leading coefficient is a unit so product of linear factors is not a unit; apply `exists_mem_of_not_is_unit_aeval_prod`. -/ have p_a_eq : aeval a (C k - p) = ↑ₐk - aeval a p, by simp only [aeval_C, alg_hom.map_sub, sub_left_inj], rw [mem_iff, ←p_a_eq, hprod, aeval_mul, ((commute.all _ _).map (aeval a)).is_unit_mul_iff, aeval_C] at hk, replace hk := exists_mem_of_not_is_unit_aeval_prod h_ne (not_and.mp hk lead_unit), rcases hk with ⟨r, r_mem, r_ev⟩, exact ⟨r, r_mem, symm (by simpa [eval_sub, eval_C, sub_eq_zero] using r_ev)⟩, end /-- In this version of the spectral mapping theorem, we assume the spectrum is nonempty instead of assuming the degree of the polynomial is positive. -/ theorem map_polynomial_aeval_of_nonempty [is_alg_closed 𝕜] (a : A) (p : 𝕜[X]) (hnon : (σ a).nonempty) : σ (aeval a p) = (λ k, eval k p) '' (σ a) := begin nontriviality A, refine or.elim (le_or_gt (degree p) 0) (λ h, _) (map_polynomial_aeval_of_degree_pos a p), { rw eq_C_of_degree_le_zero h, simp only [set.image_congr, eval_C, aeval_C, scalar_eq, set.nonempty.image_const hnon] }, end /-- A specialization of `spectrum.subset_polynomial_aeval` to monic monomials for convenience. -/ lemma pow_image_subset (a : A) (n : ℕ) : (λ x, x ^ n) '' (σ a) ⊆ σ (a ^ n) := by simpa only [eval_pow, eval_X, aeval_X_pow] using subset_polynomial_aeval a (X ^ n : 𝕜[X]) /-- A specialization of `spectrum.map_polynomial_aeval_of_nonempty` to monic monomials for convenience. -/ lemma map_pow_of_pos [is_alg_closed 𝕜] (a : A) {n : ℕ} (hn : 0 < n) : σ (a ^ n) = (λ x, x ^ n) '' (σ a) := by simpa only [aeval_X_pow, eval_pow, eval_X] using map_polynomial_aeval_of_degree_pos a (X ^ n : 𝕜[X]) (by { rw_mod_cast degree_X_pow, exact hn }) /-- A specialization of `spectrum.map_polynomial_aeval_of_nonempty` to monic monomials for convenience. -/ lemma map_pow_of_nonempty [is_alg_closed 𝕜] {a : A} (ha : (σ a).nonempty) (n : ℕ) : σ (a ^ n) = (λ x, x ^ n) '' (σ a) := by simpa only [aeval_X_pow, eval_pow, eval_X] using map_polynomial_aeval_of_nonempty a (X ^ n) ha variable (𝕜) /-- Every element `a` in a nontrivial finite-dimensional algebra `A` over an algebraically closed field `𝕜` has non-empty spectrum. -/ -- We will use this both to show eigenvalues exist, and to prove Schur's lemma. lemma nonempty_of_is_alg_closed_of_finite_dimensional [is_alg_closed 𝕜] [nontrivial A] [I : finite_dimensional 𝕜 A] (a : A) : (σ a).nonempty := begin obtain ⟨p, ⟨h_mon, h_eval_p⟩⟩ := is_integral_of_noetherian (is_noetherian.iff_fg.2 I) a, have nu : ¬ is_unit (aeval a p), { rw [←aeval_def] at h_eval_p, rw h_eval_p, simp, }, rw [eq_prod_roots_of_monic_of_splits_id h_mon (is_alg_closed.splits p)] at nu, obtain ⟨k, hk, _⟩ := exists_mem_of_not_is_unit_aeval_prod (monic.ne_zero h_mon) nu, exact ⟨k, hk⟩ end end scalar_field end spectrum
dae906dc0c513e19f02fe88fa6555aac041d9549
c777c32c8e484e195053731103c5e52af26a25d1
/src/measure_theory/function/strongly_measurable/basic.lean
610bde59e6b99fd8746a3cb3d6b264df033c58e6
[ "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
88,964
lean
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne, Sébastien Gouëzel -/ import analysis.normed_space.bounded_linear_maps import topology.metric_space.metrizable import measure_theory.integral.lebesgue import measure_theory.function.simple_func_dense /-! # Strongly measurable and finitely strongly measurable functions A function `f` is said to be strongly measurable if `f` is the sequential limit of simple functions. It is said to be finitely strongly measurable with respect to a measure `μ` if the supports of those simple functions have finite measure. We also provide almost everywhere versions of these notions. Almost everywhere strongly measurable functions form the largest class of functions that can be integrated using the Bochner integral. If the target space has a second countable topology, strongly measurable and measurable are equivalent. If the measure is sigma-finite, strongly measurable and finitely strongly measurable are equivalent. The main property of finitely strongly measurable functions is `fin_strongly_measurable.exists_set_sigma_finite`: there exists a measurable set `t` such that the function is supported on `t` and `μ.restrict t` is sigma-finite. As a consequence, we can prove some results for those functions as if the measure was sigma-finite. ## Main definitions * `strongly_measurable f`: `f : α → β` is the limit of a sequence `fs : ℕ → simple_func α β`. * `fin_strongly_measurable f μ`: `f : α → β` is the limit of a sequence `fs : ℕ → simple_func α β` such that for all `n ∈ ℕ`, the measure of the support of `fs n` is finite. * `ae_strongly_measurable f μ`: `f` is almost everywhere equal to a `strongly_measurable` function. * `ae_fin_strongly_measurable f μ`: `f` is almost everywhere equal to a `fin_strongly_measurable` function. * `ae_fin_strongly_measurable.sigma_finite_set`: a measurable set `t` such that `f =ᵐ[μ.restrict tᶜ] 0` and `μ.restrict t` is sigma-finite. ## Main statements * `ae_fin_strongly_measurable.exists_set_sigma_finite`: there exists a measurable set `t` such that `f =ᵐ[μ.restrict tᶜ] 0` and `μ.restrict t` is sigma-finite. We provide a solid API for strongly measurable functions, and for almost everywhere strongly measurable functions, as a basis for the Bochner integral. ## References * Hytönen, Tuomas, Jan Van Neerven, Mark Veraar, and Lutz Weis. Analysis in Banach spaces. Springer, 2016. -/ open measure_theory filter topological_space function set measure_theory.measure open_locale ennreal topology measure_theory nnreal big_operators /-- The typeclass `second_countable_topology_either α β` registers the fact that at least one of the two spaces has second countable topology. This is the right assumption to ensure that continuous maps from `α` to `β` are strongly measurable. -/ class second_countable_topology_either (α β : Type*) [topological_space α] [topological_space β] : Prop := (out : second_countable_topology α ∨ second_countable_topology β) @[priority 100] instance second_countable_topology_either_of_left (α β : Type*) [topological_space α] [topological_space β] [second_countable_topology α] : second_countable_topology_either α β := { out := or.inl (by apply_instance) } @[priority 100] instance second_countable_topology_either_of_right (α β : Type*) [topological_space α] [topological_space β] [second_countable_topology β] : second_countable_topology_either α β := { out := or.inr (by apply_instance) } variables {α β γ ι : Type*} [countable ι] namespace measure_theory local infixr ` →ₛ `:25 := simple_func section definitions variable [topological_space β] /-- A function is `strongly_measurable` if it is the limit of simple functions. -/ def strongly_measurable [measurable_space α] (f : α → β) : Prop := ∃ fs : ℕ → α →ₛ β, ∀ x, tendsto (λ n, fs n x) at_top (𝓝 (f x)) localized "notation (name := strongly_measurable_of) `strongly_measurable[` m `]` := @measure_theory.strongly_measurable _ _ _ m" in measure_theory /-- A function is `fin_strongly_measurable` with respect to a measure if it is the limit of simple functions with support with finite measure. -/ def fin_strongly_measurable [has_zero β] {m0 : measurable_space α} (f : α → β) (μ : measure α) : Prop := ∃ fs : ℕ → α →ₛ β, (∀ n, μ (support (fs n)) < ∞) ∧ (∀ x, tendsto (λ n, fs n x) at_top (𝓝 (f x))) /-- A function is `ae_strongly_measurable` with respect to a measure `μ` if it is almost everywhere equal to the limit of a sequence of simple functions. -/ def ae_strongly_measurable {m0 : measurable_space α} (f : α → β) (μ : measure α) : Prop := ∃ g, strongly_measurable g ∧ f =ᵐ[μ] g /-- A function is `ae_fin_strongly_measurable` with respect to a measure if it is almost everywhere equal to the limit of a sequence of simple functions with support with finite measure. -/ def ae_fin_strongly_measurable [has_zero β] {m0 : measurable_space α} (f : α → β) (μ : measure α) : Prop := ∃ g, fin_strongly_measurable g μ ∧ f =ᵐ[μ] g end definitions open_locale measure_theory /-! ## Strongly measurable functions -/ protected lemma strongly_measurable.ae_strongly_measurable {α β} {m0 : measurable_space α} [topological_space β] {f : α → β} {μ : measure α} (hf : strongly_measurable f) : ae_strongly_measurable f μ := ⟨f, hf, eventually_eq.refl _ _⟩ @[simp] lemma subsingleton.strongly_measurable {α β} [measurable_space α] [topological_space β] [subsingleton β] (f : α → β) : strongly_measurable f := begin let f_sf : α →ₛ β := ⟨f, λ x, _, set.subsingleton.finite set.subsingleton_of_subsingleton⟩, { exact ⟨λ n, f_sf, λ x, tendsto_const_nhds⟩, }, { have h_univ : f ⁻¹' {x} = set.univ, by { ext1 y, simp, }, rw h_univ, exact measurable_set.univ, }, end lemma simple_func.strongly_measurable {α β} {m : measurable_space α} [topological_space β] (f : α →ₛ β) : strongly_measurable f := ⟨λ _, f, λ x, tendsto_const_nhds⟩ lemma strongly_measurable_of_is_empty [is_empty α] {m : measurable_space α} [topological_space β] (f : α → β) : strongly_measurable f := ⟨λ n, simple_func.of_is_empty, is_empty_elim⟩ lemma strongly_measurable_const {α β} {m : measurable_space α} [topological_space β] {b : β} : strongly_measurable (λ a : α, b) := ⟨λ n, simple_func.const α b, λ a, tendsto_const_nhds⟩ @[to_additive] lemma strongly_measurable_one {α β} {m : measurable_space α} [topological_space β] [has_one β] : strongly_measurable (1 : α → β) := @strongly_measurable_const _ _ _ _ 1 /-- A version of `strongly_measurable_const` that assumes `f x = f y` for all `x, y`. This version works for functions between empty types. -/ lemma strongly_measurable_const' {α β} {m : measurable_space α} [topological_space β] {f : α → β} (hf : ∀ x y, f x = f y) : strongly_measurable f := begin casesI is_empty_or_nonempty α, { exact strongly_measurable_of_is_empty f }, { convert strongly_measurable_const, exact funext (λ x, hf x h.some) } end @[simp] lemma subsingleton.strongly_measurable' {α β} [measurable_space α] [topological_space β] [subsingleton α] (f : α → β) : strongly_measurable f := strongly_measurable_const' (λ x y, by rw subsingleton.elim x y) namespace strongly_measurable variables {f g : α → β} section basic_properties_in_any_topological_space variables [topological_space β] /-- A sequence of simple functions such that `∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x))`. That property is given by `strongly_measurable.tendsto_approx`. -/ protected noncomputable def approx {m : measurable_space α} (hf : strongly_measurable f) : ℕ → α →ₛ β := hf.some protected lemma tendsto_approx {m : measurable_space α} (hf : strongly_measurable f) : ∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x)) := hf.some_spec /-- Similar to `strongly_measurable.approx`, but enforces that the norm of every function in the sequence is less than `c` everywhere. If `‖f x‖ ≤ c` this sequence of simple functions verifies `tendsto (λ n, hf.approx_bounded n x) at_top (𝓝 (f x))`. -/ noncomputable def approx_bounded {m : measurable_space α} [has_norm β] [has_smul ℝ β] (hf : strongly_measurable f) (c : ℝ) : ℕ → simple_func α β := λ n, (hf.approx n).map (λ x, (min 1 (c / ‖x‖)) • x) lemma tendsto_approx_bounded_of_norm_le {β} {f : α → β} [normed_add_comm_group β] [normed_space ℝ β] {m : measurable_space α} (hf : strongly_measurable[m] f) {c : ℝ} {x : α} (hfx : ‖f x‖ ≤ c) : tendsto (λ n, hf.approx_bounded c n x) at_top (𝓝 (f x)) := begin have h_tendsto := hf.tendsto_approx x, simp only [strongly_measurable.approx_bounded, simple_func.coe_map, function.comp_app], by_cases hfx0 : ‖f x‖ = 0, { rw norm_eq_zero at hfx0, rw hfx0 at h_tendsto ⊢, have h_tendsto_norm : tendsto (λ n, ‖hf.approx n x‖) at_top (𝓝 0), { convert h_tendsto.norm, rw norm_zero, }, refine squeeze_zero_norm (λ n, _) h_tendsto_norm, calc ‖min 1 (c / ‖hf.approx n x‖) • hf.approx n x‖ = ‖min 1 (c / ‖hf.approx n x‖)‖ * ‖hf.approx n x‖ : norm_smul _ _ ... ≤ ‖(1 : ℝ)‖ * ‖hf.approx n x‖ : begin refine mul_le_mul_of_nonneg_right _ (norm_nonneg _), rw [norm_one, real.norm_of_nonneg], { exact min_le_left _ _, }, { exact le_min zero_le_one (div_nonneg ((norm_nonneg _).trans hfx) (norm_nonneg _)), }, end ... = ‖hf.approx n x‖ : by rw [norm_one, one_mul], }, rw ← one_smul ℝ (f x), refine tendsto.smul _ h_tendsto, have : min 1 (c / ‖f x‖) = 1, { rw [min_eq_left_iff, one_le_div (lt_of_le_of_ne (norm_nonneg _) (ne.symm hfx0))], exact hfx, }, nth_rewrite 0 this.symm, refine tendsto.min tendsto_const_nhds _, refine tendsto.div tendsto_const_nhds h_tendsto.norm hfx0, end lemma tendsto_approx_bounded_ae {β} {f : α → β} [normed_add_comm_group β] [normed_space ℝ β] {m m0 : measurable_space α} {μ : measure α} (hf : strongly_measurable[m] f) {c : ℝ} (hf_bound : ∀ᵐ x ∂μ, ‖f x‖ ≤ c) : ∀ᵐ x ∂μ, tendsto (λ n, hf.approx_bounded c n x) at_top (𝓝 (f x)) := by filter_upwards [hf_bound] with x hfx using tendsto_approx_bounded_of_norm_le hf hfx lemma norm_approx_bounded_le {β} {f : α → β} [seminormed_add_comm_group β] [normed_space ℝ β] {m : measurable_space α} {c : ℝ} (hf : strongly_measurable[m] f) (hc : 0 ≤ c) (n : ℕ) (x : α) : ‖hf.approx_bounded c n x‖ ≤ c := begin simp only [strongly_measurable.approx_bounded, simple_func.coe_map, function.comp_app], refine (norm_smul_le _ _).trans _, by_cases h0 : ‖hf.approx n x‖ = 0, { simp only [h0, div_zero, min_eq_right, zero_le_one, norm_zero, mul_zero], exact hc, }, cases le_total (‖hf.approx n x‖) c, { rw min_eq_left _, { simpa only [norm_one, one_mul] using h, }, { rwa one_le_div (lt_of_le_of_ne (norm_nonneg _) (ne.symm h0)), }, }, { rw min_eq_right _, { rw [norm_div, norm_norm, mul_comm, mul_div, div_eq_mul_inv, mul_comm, ← mul_assoc, inv_mul_cancel h0, one_mul, real.norm_of_nonneg hc], }, { rwa div_le_one (lt_of_le_of_ne (norm_nonneg _) (ne.symm h0)), }, }, end lemma _root_.strongly_measurable_bot_iff [nonempty β] [t2_space β] : strongly_measurable[⊥] f ↔ ∃ c, f = λ _, c := begin casesI is_empty_or_nonempty α with hα hα, { simp only [subsingleton.strongly_measurable', eq_iff_true_of_subsingleton, exists_const], }, refine ⟨λ hf, _, λ hf_eq, _⟩, { refine ⟨f hα.some, _⟩, let fs := hf.approx, have h_fs_tendsto : ∀ x, tendsto (λ n, fs n x) at_top (𝓝 (f x)) := hf.tendsto_approx, have : ∀ n, ∃ c, ∀ x, fs n x = c := λ n, simple_func.simple_func_bot (fs n), let cs := λ n, (this n).some, have h_cs_eq : ∀ n, ⇑(fs n) = (λ x, cs n) := λ n, funext (this n).some_spec, simp_rw h_cs_eq at h_fs_tendsto, have h_tendsto : tendsto cs at_top (𝓝 (f hα.some)) := h_fs_tendsto hα.some, ext1 x, exact tendsto_nhds_unique (h_fs_tendsto x) h_tendsto, }, { obtain ⟨c, rfl⟩ := hf_eq, exact strongly_measurable_const, }, end end basic_properties_in_any_topological_space lemma fin_strongly_measurable_of_set_sigma_finite [topological_space β] [has_zero β] {m : measurable_space α} {μ : measure α} (hf_meas : strongly_measurable f) {t : set α} (ht : measurable_set t) (hft_zero : ∀ x ∈ tᶜ, f x = 0) (htμ : sigma_finite (μ.restrict t)) : fin_strongly_measurable f μ := begin haveI : sigma_finite (μ.restrict t) := htμ, let S := spanning_sets (μ.restrict t), have hS_meas : ∀ n, measurable_set (S n), from measurable_spanning_sets (μ.restrict t), let f_approx := hf_meas.approx, let fs := λ n, simple_func.restrict (f_approx n) (S n ∩ t), have h_fs_t_compl : ∀ n, ∀ x ∉ t, fs n x = 0, { intros n x hxt, rw simple_func.restrict_apply _ ((hS_meas n).inter ht), refine set.indicator_of_not_mem _ _, simp [hxt], }, refine ⟨fs, _, λ x, _⟩, { simp_rw simple_func.support_eq, refine λ n, (measure_bUnion_finset_le _ _).trans_lt _, refine ennreal.sum_lt_top_iff.mpr (λ y hy, _), rw simple_func.restrict_preimage_singleton _ ((hS_meas n).inter ht), swap, { rw finset.mem_filter at hy, exact hy.2, }, refine (measure_mono (set.inter_subset_left _ _)).trans_lt _, have h_lt_top := measure_spanning_sets_lt_top (μ.restrict t) n, rwa measure.restrict_apply' ht at h_lt_top, }, { by_cases hxt : x ∈ t, swap, { rw [funext (λ n, h_fs_t_compl n x hxt), hft_zero x hxt], exact tendsto_const_nhds, }, have h : tendsto (λ n, (f_approx n) x) at_top (𝓝 (f x)), from hf_meas.tendsto_approx x, obtain ⟨n₁, hn₁⟩ : ∃ n, ∀ m, n ≤ m → fs m x = f_approx m x, { obtain ⟨n, hn⟩ : ∃ n, ∀ m, n ≤ m → x ∈ S m ∩ t, { rsuffices ⟨n, hn⟩ : ∃ n, ∀ m, n ≤ m → x ∈ S m, { exact ⟨n, λ m hnm, set.mem_inter (hn m hnm) hxt⟩, }, rsuffices ⟨n, hn⟩ : ∃ n, x ∈ S n, { exact ⟨n, λ m hnm, monotone_spanning_sets (μ.restrict t) hnm hn⟩, }, rw [← set.mem_Union, Union_spanning_sets (μ.restrict t)], trivial, }, refine ⟨n, λ m hnm, _⟩, simp_rw [fs, simple_func.restrict_apply _ ((hS_meas m).inter ht), set.indicator_of_mem (hn m hnm)], }, rw tendsto_at_top' at h ⊢, intros s hs, obtain ⟨n₂, hn₂⟩ := h s hs, refine ⟨max n₁ n₂, λ m hm, _⟩, rw hn₁ m ((le_max_left _ _).trans hm.le), exact hn₂ m ((le_max_right _ _).trans hm.le), }, end /-- If the measure is sigma-finite, all strongly measurable functions are `fin_strongly_measurable`. -/ protected lemma fin_strongly_measurable [topological_space β] [has_zero β] {m0 : measurable_space α} (hf : strongly_measurable f) (μ : measure α) [sigma_finite μ] : fin_strongly_measurable f μ := hf.fin_strongly_measurable_of_set_sigma_finite measurable_set.univ (by simp) (by rwa measure.restrict_univ) /-- A strongly measurable function is measurable. -/ protected lemma measurable {m : measurable_space α} [topological_space β] [pseudo_metrizable_space β] [measurable_space β] [borel_space β] (hf : strongly_measurable f) : measurable f := measurable_of_tendsto_metrizable (λ n, (hf.approx n).measurable) (tendsto_pi_nhds.mpr hf.tendsto_approx) /-- A strongly measurable function is almost everywhere measurable. -/ protected lemma ae_measurable {m : measurable_space α} [topological_space β] [pseudo_metrizable_space β] [measurable_space β] [borel_space β] {μ : measure α} (hf : strongly_measurable f) : ae_measurable f μ := hf.measurable.ae_measurable lemma _root_.continuous.comp_strongly_measurable {m : measurable_space α} [topological_space β] [topological_space γ] {g : β → γ} {f : α → β} (hg : continuous g) (hf : strongly_measurable f) : strongly_measurable (λ x, g (f x)) := ⟨λ n, simple_func.map g (hf.approx n), λ x, (hg.tendsto _).comp (hf.tendsto_approx x)⟩ @[to_additive] lemma measurable_set_mul_support {m : measurable_space α} [has_one β] [topological_space β] [metrizable_space β] (hf : strongly_measurable f) : measurable_set (mul_support f) := by { borelize β, exact measurable_set_mul_support hf.measurable } protected lemma mono {m m' : measurable_space α} [topological_space β] (hf : strongly_measurable[m'] f) (h_mono : m' ≤ m) : strongly_measurable[m] f := begin let f_approx : ℕ → @simple_func α m β := λ n, { to_fun := hf.approx n, measurable_set_fiber' := λ x, h_mono _ (simple_func.measurable_set_fiber' _ x), finite_range' := simple_func.finite_range (hf.approx n) }, exact ⟨f_approx, hf.tendsto_approx⟩, end protected lemma prod_mk {m : measurable_space α} [topological_space β] [topological_space γ] {f : α → β} {g : α → γ} (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (λ x, (f x, g x)) := begin refine ⟨λ n, simple_func.pair (hf.approx n) (hg.approx n), λ x, _⟩, rw nhds_prod_eq, exact tendsto.prod_mk (hf.tendsto_approx x) (hg.tendsto_approx x), end lemma comp_measurable [topological_space β] {m : measurable_space α} {m' : measurable_space γ} {f : α → β} {g : γ → α} (hf : strongly_measurable f) (hg : measurable g) : strongly_measurable (f ∘ g) := ⟨λ n, simple_func.comp (hf.approx n) g hg, λ x, hf.tendsto_approx (g x)⟩ lemma of_uncurry_left [topological_space β] {mα : measurable_space α} {mγ : measurable_space γ} {f : α → γ → β} (hf : strongly_measurable (uncurry f)) {x : α} : strongly_measurable (f x) := hf.comp_measurable measurable_prod_mk_left lemma of_uncurry_right [topological_space β] {mα : measurable_space α} {mγ : measurable_space γ} {f : α → γ → β} (hf : strongly_measurable (uncurry f)) {y : γ} : strongly_measurable (λ x, f x y) := hf.comp_measurable measurable_prod_mk_right section arithmetic variables {mα : measurable_space α} [topological_space β] include mα @[to_additive] protected lemma mul [has_mul β] [has_continuous_mul β] (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (f * g) := ⟨λ n, hf.approx n * hg.approx n, λ x, (hf.tendsto_approx x).mul (hg.tendsto_approx x)⟩ @[to_additive] lemma mul_const [has_mul β] [has_continuous_mul β] (hf : strongly_measurable f) (c : β) : strongly_measurable (λ x, f x * c) := hf.mul strongly_measurable_const @[to_additive] lemma const_mul [has_mul β] [has_continuous_mul β] (hf : strongly_measurable f) (c : β) : strongly_measurable (λ x, c * f x) := strongly_measurable_const.mul hf @[to_additive] protected lemma inv [group β] [topological_group β] (hf : strongly_measurable f) : strongly_measurable f⁻¹ := ⟨λ n, (hf.approx n)⁻¹, λ x, (hf.tendsto_approx x).inv⟩ @[to_additive] protected lemma div [has_div β] [has_continuous_div β] (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (f / g) := ⟨λ n, hf.approx n / hg.approx n, λ x, (hf.tendsto_approx x).div' (hg.tendsto_approx x)⟩ @[to_additive] protected lemma smul {𝕜} [topological_space 𝕜] [has_smul 𝕜 β] [has_continuous_smul 𝕜 β] {f : α → 𝕜} {g : α → β} (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (λ x, f x • g x) := continuous_smul.comp_strongly_measurable (hf.prod_mk hg) protected lemma const_smul {𝕜} [has_smul 𝕜 β] [has_continuous_const_smul 𝕜 β] (hf : strongly_measurable f) (c : 𝕜) : strongly_measurable (c • f) := ⟨λ n, c • (hf.approx n), λ x, (hf.tendsto_approx x).const_smul c⟩ protected lemma const_smul' {𝕜} [has_smul 𝕜 β] [has_continuous_const_smul 𝕜 β] (hf : strongly_measurable f) (c : 𝕜) : strongly_measurable (λ x, c • (f x)) := hf.const_smul c @[to_additive] protected lemma smul_const {𝕜} [topological_space 𝕜] [has_smul 𝕜 β] [has_continuous_smul 𝕜 β] {f : α → 𝕜} (hf : strongly_measurable f) (c : β) : strongly_measurable (λ x, f x • c) := continuous_smul.comp_strongly_measurable (hf.prod_mk strongly_measurable_const) end arithmetic section mul_action variables [topological_space β] {G : Type*} [group G] [mul_action G β] [has_continuous_const_smul G β] lemma _root_.strongly_measurable_const_smul_iff {m : measurable_space α} (c : G) : strongly_measurable (λ x, c • f x) ↔ strongly_measurable f := ⟨λ h, by simpa only [inv_smul_smul] using h.const_smul' c⁻¹, λ h, h.const_smul c⟩ variables {G₀ : Type*} [group_with_zero G₀] [mul_action G₀ β] [has_continuous_const_smul G₀ β] lemma _root_.strongly_measurable_const_smul_iff₀ {m : measurable_space α} {c : G₀} (hc : c ≠ 0) : strongly_measurable (λ x, c • f x) ↔ strongly_measurable f := begin refine ⟨λ h, _, λ h, h.const_smul c⟩, convert h.const_smul' c⁻¹, simp [smul_smul, inv_mul_cancel hc] end end mul_action section order variables [measurable_space α] [topological_space β] open filter open_locale filter protected lemma sup [has_sup β] [has_continuous_sup β] (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (f ⊔ g) := ⟨λ n, hf.approx n ⊔ hg.approx n, λ x, (hf.tendsto_approx x).sup_right_nhds (hg.tendsto_approx x)⟩ protected lemma inf [has_inf β] [has_continuous_inf β] (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (f ⊓ g) := ⟨λ n, hf.approx n ⊓ hg.approx n, λ x, (hf.tendsto_approx x).inf_right_nhds (hg.tendsto_approx x)⟩ end order /-! ### Big operators: `∏` and `∑` -/ section monoid variables {M : Type*} [monoid M] [topological_space M] [has_continuous_mul M] {m : measurable_space α} include m @[to_additive] lemma _root_.list.strongly_measurable_prod' (l : list (α → M)) (hl : ∀ f ∈ l, strongly_measurable f) : strongly_measurable l.prod := begin induction l with f l ihl, { exact strongly_measurable_one }, rw [list.forall_mem_cons] at hl, rw [list.prod_cons], exact hl.1.mul (ihl hl.2) end @[to_additive] lemma _root_.list.strongly_measurable_prod (l : list (α → M)) (hl : ∀ f ∈ l, strongly_measurable f) : strongly_measurable (λ x, (l.map (λ f : α → M, f x)).prod) := by simpa only [← pi.list_prod_apply] using l.strongly_measurable_prod' hl end monoid section comm_monoid variables {M : Type*} [comm_monoid M] [topological_space M] [has_continuous_mul M] {m : measurable_space α} include m @[to_additive] lemma _root_.multiset.strongly_measurable_prod' (l : multiset (α → M)) (hl : ∀ f ∈ l, strongly_measurable f) : strongly_measurable l.prod := by { rcases l with ⟨l⟩, simpa using l.strongly_measurable_prod' (by simpa using hl) } @[to_additive] lemma _root_.multiset.strongly_measurable_prod (s : multiset (α → M)) (hs : ∀ f ∈ s, strongly_measurable f) : strongly_measurable (λ x, (s.map (λ f : α → M, f x)).prod) := by simpa only [← pi.multiset_prod_apply] using s.strongly_measurable_prod' hs @[to_additive] lemma _root_.finset.strongly_measurable_prod' {ι : Type*} {f : ι → α → M} (s : finset ι) (hf : ∀i ∈ s, strongly_measurable (f i)) : strongly_measurable (∏ i in s, f i) := finset.prod_induction _ _ (λ a b ha hb, ha.mul hb) (@strongly_measurable_one α M _ _ _) hf @[to_additive] lemma _root_.finset.strongly_measurable_prod {ι : Type*} {f : ι → α → M} (s : finset ι) (hf : ∀i ∈ s, strongly_measurable (f i)) : strongly_measurable (λ a, ∏ i in s, f i a) := by simpa only [← finset.prod_apply] using s.strongly_measurable_prod' hf end comm_monoid /-- The range of a strongly measurable function is separable. -/ lemma is_separable_range {m : measurable_space α} [topological_space β] (hf : strongly_measurable f) : topological_space.is_separable (range f) := begin have : is_separable (closure (⋃ n, range (hf.approx n))) := (is_separable_Union (λ n, (simple_func.finite_range (hf.approx n)).is_separable)).closure, apply this.mono, rintros _ ⟨x, rfl⟩, apply mem_closure_of_tendsto (hf.tendsto_approx x), apply eventually_of_forall (λ n, _), apply mem_Union_of_mem n, exact mem_range_self _ end lemma separable_space_range_union_singleton {m : measurable_space α} [topological_space β] [pseudo_metrizable_space β] (hf : strongly_measurable f) {b : β} : separable_space (range f ∪ {b} : set β) := begin letI := pseudo_metrizable_space_pseudo_metric β, exact (hf.is_separable_range.union (finite_singleton _).is_separable).separable_space end section second_countable_strongly_measurable variables {mα : measurable_space α} [measurable_space β] include mα /-- In a space with second countable topology, measurable implies strongly measurable. -/ lemma _root_.measurable.strongly_measurable [topological_space β] [pseudo_metrizable_space β] [second_countable_topology β] [opens_measurable_space β] (hf : measurable f) : strongly_measurable f := begin letI := pseudo_metrizable_space_pseudo_metric β, rcases is_empty_or_nonempty β; resetI, { exact subsingleton.strongly_measurable f, }, { inhabit β, exact ⟨simple_func.approx_on f hf set.univ default (set.mem_univ _), λ x, simple_func.tendsto_approx_on hf (set.mem_univ _) (by simp)⟩, }, end /-- In a space with second countable topology, strongly measurable and measurable are equivalent. -/ lemma _root_.strongly_measurable_iff_measurable [topological_space β] [metrizable_space β] [borel_space β] [second_countable_topology β] : strongly_measurable f ↔ measurable f := ⟨λ h, h.measurable, λ h, measurable.strongly_measurable h⟩ lemma _root_.strongly_measurable_id [topological_space α] [pseudo_metrizable_space α] [opens_measurable_space α] [second_countable_topology α] : strongly_measurable (id : α → α) := measurable_id.strongly_measurable end second_countable_strongly_measurable /-- A function is strongly measurable if and only if it is measurable and has separable range. -/ theorem _root_.strongly_measurable_iff_measurable_separable {m : measurable_space α} [topological_space β] [pseudo_metrizable_space β] [measurable_space β] [borel_space β] : strongly_measurable f ↔ (measurable f ∧ is_separable (range f)) := begin refine ⟨λ H, ⟨H.measurable, H.is_separable_range⟩, _⟩, rintros ⟨H, H'⟩, letI := pseudo_metrizable_space_pseudo_metric β, let g := cod_restrict f (closure (range f)) (λ x, subset_closure (mem_range_self x)), have fg : f = (coe : closure (range f) → β) ∘ g, by { ext x, refl }, have T : measurable_embedding (coe : closure (range f) → β), { apply closed_embedding.measurable_embedding, exact closed_embedding_subtype_coe is_closed_closure }, have g_meas : measurable g, { rw fg at H, exact T.measurable_comp_iff.1 H }, haveI : second_countable_topology (closure (range f)), { suffices : separable_space (closure (range f)), by exactI uniform_space.second_countable_of_separable _, exact (is_separable.closure H').separable_space }, have g_smeas : strongly_measurable g := measurable.strongly_measurable g_meas, rw fg, exact continuous_subtype_coe.comp_strongly_measurable g_smeas, end /-- A continuous function is strongly measurable when either the source space or the target space is second-countable. -/ lemma _root_.continuous.strongly_measurable [measurable_space α] [topological_space α] [opens_measurable_space α] {β : Type*} [topological_space β] [pseudo_metrizable_space β] [h : second_countable_topology_either α β] {f : α → β} (hf : continuous f) : strongly_measurable f := begin borelize β, casesI h.out, { rw strongly_measurable_iff_measurable_separable, refine ⟨hf.measurable, _⟩, rw ← image_univ, exact (is_separable_of_separable_space univ).image hf }, { exact hf.measurable.strongly_measurable } end /-- If `g` is a topological embedding, then `f` is strongly measurable iff `g ∘ f` is. -/ lemma _root_.embedding.comp_strongly_measurable_iff {m : measurable_space α} [topological_space β] [pseudo_metrizable_space β] [topological_space γ] [pseudo_metrizable_space γ] {g : β → γ} {f : α → β} (hg : embedding g) : strongly_measurable (λ x, g (f x)) ↔ strongly_measurable f := begin letI := pseudo_metrizable_space_pseudo_metric γ, borelize [β, γ], refine ⟨λ H, strongly_measurable_iff_measurable_separable.2 ⟨_, _⟩, λ H, hg.continuous.comp_strongly_measurable H⟩, { let G : β → range g := cod_restrict g (range g) mem_range_self, have hG : closed_embedding G := { closed_range := begin convert is_closed_univ, apply eq_univ_of_forall, rintros ⟨-, ⟨x, rfl⟩⟩, exact mem_range_self x end, .. hg.cod_restrict _ _ }, have : measurable (G ∘ f) := measurable.subtype_mk H.measurable, exact hG.measurable_embedding.measurable_comp_iff.1 this }, { have : is_separable (g ⁻¹' (range (g ∘ f))) := hg.is_separable_preimage H.is_separable_range, convert this, ext x, simp [hg.inj.eq_iff] } end /-- A sequential limit of strongly measurable functions is strongly measurable. -/ lemma _root_.strongly_measurable_of_tendsto {ι : Type*} {m : measurable_space α} [topological_space β] [pseudo_metrizable_space β] (u : filter ι) [ne_bot u] [is_countably_generated u] {f : ι → α → β} {g : α → β} (hf : ∀ i, strongly_measurable (f i)) (lim : tendsto f u (𝓝 g)) : strongly_measurable g := begin borelize β, refine strongly_measurable_iff_measurable_separable.2 ⟨_, _⟩, { exact measurable_of_tendsto_metrizable' u (λ i, (hf i).measurable) lim }, { rcases u.exists_seq_tendsto with ⟨v, hv⟩, have : is_separable (closure (⋃ i, range (f (v i)))) := (is_separable_Union (λ i, (hf (v i)).is_separable_range)).closure, apply this.mono, rintros _ ⟨x, rfl⟩, rw [tendsto_pi_nhds] at lim, apply mem_closure_of_tendsto ((lim x).comp hv), apply eventually_of_forall (λ n, _), apply mem_Union_of_mem n, exact mem_range_self _ } end protected lemma piecewise {m : measurable_space α} [topological_space β] {s : set α} {_ : decidable_pred (∈ s)} (hs : measurable_set s) (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (set.piecewise s f g) := begin refine ⟨λ n, simple_func.piecewise s hs (hf.approx n) (hg.approx n), λ x, _⟩, by_cases hx : x ∈ s, { simpa [hx] using hf.tendsto_approx x }, { simpa [hx] using hg.tendsto_approx x }, end /-- this is slightly different from `strongly_measurable.piecewise`. It can be used to show `strongly_measurable (ite (x=0) 0 1)` by `exact strongly_measurable.ite (measurable_set_singleton 0) strongly_measurable_const strongly_measurable_const`, but replacing `strongly_measurable.ite` by `strongly_measurable.piecewise` in that example proof does not work. -/ protected lemma ite {m : measurable_space α} [topological_space β] {p : α → Prop} {_ : decidable_pred p} (hp : measurable_set {a : α | p a}) (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (λ x, ite (p x) (f x) (g x)) := strongly_measurable.piecewise hp hf hg lemma _root_.strongly_measurable_of_strongly_measurable_union_cover {m : measurable_space α} [topological_space β] {f : α → β} (s t : set α) (hs : measurable_set s) (ht : measurable_set t) (h : univ ⊆ s ∪ t) (hc : strongly_measurable (λ a : s, f a)) (hd : strongly_measurable (λ a : t, f a)) : strongly_measurable f := begin classical, let f : ℕ → α →ₛ β := λ n, { to_fun := λ x, if hx : x ∈ s then hc.approx n ⟨x, hx⟩ else hd.approx n ⟨x, by simpa [hx] using h (mem_univ x)⟩, measurable_set_fiber' := begin assume x, convert (hs.subtype_image ((hc.approx n).measurable_set_fiber x)).union ((ht.subtype_image ((hd.approx n).measurable_set_fiber x)).diff hs), ext1 y, simp only [mem_union, mem_preimage, mem_singleton_iff, mem_image, set_coe.exists, subtype.coe_mk, exists_and_distrib_right, exists_eq_right, mem_diff], by_cases hy : y ∈ s, { rw dif_pos hy, simp only [hy, exists_true_left, not_true, and_false, or_false]}, { rw dif_neg hy, have A : y ∈ t, by simpa [hy] using h (mem_univ y), simp only [A, hy, false_or, is_empty.exists_iff, not_false_iff, and_true, exists_true_left] } end, finite_range' := begin apply ((hc.approx n).finite_range.union (hd.approx n).finite_range).subset, rintros - ⟨y, rfl⟩, dsimp, by_cases hy : y ∈ s, { left, rw dif_pos hy, exact mem_range_self _ }, { right, rw dif_neg hy, exact mem_range_self _ } end }, refine ⟨f, λ y, _⟩, by_cases hy : y ∈ s, { convert hc.tendsto_approx ⟨y, hy⟩ using 1, ext1 n, simp only [dif_pos hy, simple_func.apply_mk] }, { have A : y ∈ t, by simpa [hy] using h (mem_univ y), convert hd.tendsto_approx ⟨y, A⟩ using 1, ext1 n, simp only [dif_neg hy, simple_func.apply_mk] } end lemma _root_.strongly_measurable_of_restrict_of_restrict_compl {m : measurable_space α} [topological_space β] {f : α → β} {s : set α} (hs : measurable_set s) (h₁ : strongly_measurable (s.restrict f)) (h₂ : strongly_measurable (sᶜ.restrict f)) : strongly_measurable f := strongly_measurable_of_strongly_measurable_union_cover s sᶜ hs hs.compl (union_compl_self s).ge h₁ h₂ protected lemma indicator {m : measurable_space α} [topological_space β] [has_zero β] (hf : strongly_measurable f) {s : set α} (hs : measurable_set s) : strongly_measurable (s.indicator f) := hf.piecewise hs strongly_measurable_const protected lemma dist {m : measurable_space α} {β : Type*} [pseudo_metric_space β] {f g : α → β} (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (λ x, dist (f x) (g x)) := continuous_dist.comp_strongly_measurable (hf.prod_mk hg) protected lemma norm {m : measurable_space α} {β : Type*} [seminormed_add_comm_group β] {f : α → β} (hf : strongly_measurable f) : strongly_measurable (λ x, ‖f x‖) := continuous_norm.comp_strongly_measurable hf protected lemma nnnorm {m : measurable_space α} {β : Type*} [seminormed_add_comm_group β] {f : α → β} (hf : strongly_measurable f) : strongly_measurable (λ x, ‖f x‖₊) := continuous_nnnorm.comp_strongly_measurable hf protected lemma ennnorm {m : measurable_space α} {β : Type*} [seminormed_add_comm_group β] {f : α → β} (hf : strongly_measurable f) : measurable (λ a, (‖f a‖₊ : ℝ≥0∞)) := (ennreal.continuous_coe.comp_strongly_measurable hf.nnnorm).measurable protected lemma real_to_nnreal {m : measurable_space α} {f : α → ℝ} (hf : strongly_measurable f) : strongly_measurable (λ x, (f x).to_nnreal) := continuous_real_to_nnreal.comp_strongly_measurable hf lemma _root_.measurable_embedding.strongly_measurable_extend {f : α → β} {g : α → γ} {g' : γ → β} {mα : measurable_space α} {mγ : measurable_space γ} [topological_space β] (hg : measurable_embedding g) (hf : strongly_measurable f) (hg' : strongly_measurable g') : strongly_measurable (function.extend g f g') := begin refine ⟨λ n, simple_func.extend (hf.approx n) g hg (hg'.approx n), _⟩, assume x, by_cases hx : ∃ y, g y = x, { rcases hx with ⟨y, rfl⟩, simpa only [simple_func.extend_apply, hg.injective, injective.extend_apply] using hf.tendsto_approx y }, { simpa only [hx, simple_func.extend_apply', not_false_iff, extend_apply'] using hg'.tendsto_approx x } end lemma _root_.measurable_embedding.exists_strongly_measurable_extend {f : α → β} {g : α → γ} {mα : measurable_space α} {mγ : measurable_space γ} [topological_space β] (hg : measurable_embedding g) (hf : strongly_measurable f) (hne : γ → nonempty β) : ∃ f' : γ → β, strongly_measurable f' ∧ f' ∘ g = f := ⟨function.extend g f (λ x, classical.choice (hne x)), hg.strongly_measurable_extend hf (strongly_measurable_const' $ λ _ _, rfl), funext $ λ x, hg.injective.extend_apply _ _ _⟩ lemma measurable_set_eq_fun {m : measurable_space α} {E} [topological_space E] [metrizable_space E] {f g : α → E} (hf : strongly_measurable f) (hg : strongly_measurable g) : measurable_set {x | f x = g x} := begin borelize E × E, exact (hf.prod_mk hg).measurable is_closed_diagonal.measurable_set end lemma measurable_set_lt {m : measurable_space α} [topological_space β] [linear_order β] [order_closed_topology β] [pseudo_metrizable_space β] {f g : α → β} (hf : strongly_measurable f) (hg : strongly_measurable g) : measurable_set {a | f a < g a} := begin borelize β × β, exact (hf.prod_mk hg).measurable is_open_lt_prod.measurable_set end lemma measurable_set_le {m : measurable_space α} [topological_space β] [preorder β] [order_closed_topology β] [pseudo_metrizable_space β] {f g : α → β} (hf : strongly_measurable f) (hg : strongly_measurable g) : measurable_set {a | f a ≤ g a} := begin borelize β × β, exact (hf.prod_mk hg).measurable is_closed_le_prod.measurable_set end lemma strongly_measurable_in_set {m : measurable_space α} [topological_space β] [has_zero β] {s : set α} {f : α → β} (hs : measurable_set s) (hf : strongly_measurable f) (hf_zero : ∀ x ∉ s, f x = 0) : ∃ fs : ℕ → α →ₛ β, (∀ x, tendsto (λ n, fs n x) at_top (𝓝 (f x))) ∧ (∀ (x ∉ s) n, fs n x = 0) := begin let g_seq_s : ℕ → @simple_func α m β := λ n, (hf.approx n).restrict s, have hg_eq : ∀ x ∈ s, ∀ n, g_seq_s n x = hf.approx n x, { intros x hx n, rw [simple_func.coe_restrict _ hs, set.indicator_of_mem hx], }, have hg_zero : ∀ x ∉ s, ∀ n, g_seq_s n x = 0, { intros x hx n, rw [simple_func.coe_restrict _ hs, set.indicator_of_not_mem hx], }, refine ⟨g_seq_s, λ x, _, hg_zero⟩, by_cases hx : x ∈ s, { simp_rw hg_eq x hx, exact hf.tendsto_approx x, }, { simp_rw [hg_zero x hx, hf_zero x hx], exact tendsto_const_nhds, }, end /-- If the restriction to a set `s` of a σ-algebra `m` is included in the restriction to `s` of another σ-algebra `m₂` (hypothesis `hs`), the set `s` is `m` measurable and a function `f` supported on `s` is `m`-strongly-measurable, then `f` is also `m₂`-strongly-measurable. -/ lemma strongly_measurable_of_measurable_space_le_on {α E} {m m₂ : measurable_space α} [topological_space E] [has_zero E] {s : set α} {f : α → E} (hs_m : measurable_set[m] s) (hs : ∀ t, measurable_set[m] (s ∩ t) → measurable_set[m₂] (s ∩ t)) (hf : strongly_measurable[m] f) (hf_zero : ∀ x ∉ s, f x = 0) : strongly_measurable[m₂] f := begin have hs_m₂ : measurable_set[m₂] s, { rw ← set.inter_univ s, refine hs set.univ _, rwa [set.inter_univ], }, obtain ⟨g_seq_s, hg_seq_tendsto, hg_seq_zero⟩ := strongly_measurable_in_set hs_m hf hf_zero, let g_seq_s₂ : ℕ → @simple_func α m₂ E := λ n, { to_fun := g_seq_s n, measurable_set_fiber' := λ x, begin rw [← set.inter_univ ((g_seq_s n) ⁻¹' {x}), ← set.union_compl_self s, set.inter_union_distrib_left, set.inter_comm ((g_seq_s n) ⁻¹' {x})], refine measurable_set.union (hs _ (hs_m.inter _)) _, { exact @simple_func.measurable_set_fiber _ _ m _ _, }, by_cases hx : x = 0, { suffices : (g_seq_s n) ⁻¹' {x} ∩ sᶜ = sᶜ, by { rw this, exact hs_m₂.compl, }, ext1 y, rw [hx, set.mem_inter_iff, set.mem_preimage, set.mem_singleton_iff], exact ⟨λ h, h.2, λ h, ⟨hg_seq_zero y h n, h⟩⟩, }, { suffices : (g_seq_s n) ⁻¹' {x} ∩ sᶜ = ∅, by { rw this, exact measurable_set.empty, }, ext1 y, simp only [mem_inter_iff, mem_preimage, mem_singleton_iff, mem_compl_iff, mem_empty_iff_false, iff_false, not_and, not_not_mem], refine imp_of_not_imp_not _ _ (λ hys, _), rw hg_seq_zero y hys n, exact ne.symm hx, }, end, finite_range' := @simple_func.finite_range _ _ m (g_seq_s n), }, have hg_eq : ∀ x n, g_seq_s₂ n x = g_seq_s n x := λ x n, rfl, refine ⟨g_seq_s₂, λ x, _⟩, simp_rw hg_eq, exact hg_seq_tendsto x, end /-- If a function `f` is strongly measurable w.r.t. a sub-σ-algebra `m` and the measure is σ-finite on `m`, then there exists spanning measurable sets with finite measure on which `f` has bounded norm. In particular, `f` is integrable on each of those sets. -/ lemma exists_spanning_measurable_set_norm_le [seminormed_add_comm_group β] {m m0 : measurable_space α} (hm : m ≤ m0) (hf : strongly_measurable[m] f) (μ : measure α) [sigma_finite (μ.trim hm)] : ∃ s : ℕ → set α, (∀ n, measurable_set[m] (s n) ∧ μ (s n) < ∞ ∧ ∀ x ∈ s n, ‖f x‖ ≤ n) ∧ (⋃ i, s i) = set.univ := begin let sigma_finite_sets := spanning_sets (μ.trim hm), let norm_sets := λ (n : ℕ), {x | ‖f x‖ ≤ n}, have norm_sets_spanning : (⋃ n, norm_sets n) = set.univ, { ext1 x, simp only [set.mem_Union, set.mem_set_of_eq, set.mem_univ, iff_true], exact ⟨⌈‖f x‖⌉₊, nat.le_ceil (‖f x‖)⟩, }, let sets := λ n, sigma_finite_sets n ∩ norm_sets n, have h_meas : ∀ n, measurable_set[m] (sets n), { refine λ n, measurable_set.inter _ _, { exact measurable_spanning_sets (μ.trim hm) n, }, { exact hf.norm.measurable_set_le strongly_measurable_const, }, }, have h_finite : ∀ n, μ (sets n) < ∞, { refine λ n, (measure_mono (set.inter_subset_left _ _)).trans_lt _, exact (le_trim hm).trans_lt (measure_spanning_sets_lt_top (μ.trim hm) n), }, refine ⟨sets, λ n, ⟨h_meas n, h_finite n, _⟩, _⟩, { exact λ x hx, hx.2, }, { have : (⋃ i, sigma_finite_sets i ∩ norm_sets i) = (⋃ i, sigma_finite_sets i) ∩ (⋃ i, norm_sets i), { refine set.Union_inter_of_monotone (monotone_spanning_sets (μ.trim hm)) (λ i j hij x, _), simp only [norm_sets, set.mem_set_of_eq], refine λ hif, hif.trans _, exact_mod_cast hij, }, rw [this, norm_sets_spanning, Union_spanning_sets (μ.trim hm), set.inter_univ], }, end end strongly_measurable /-! ## Finitely strongly measurable functions -/ lemma fin_strongly_measurable_zero {α β} {m : measurable_space α} {μ : measure α} [has_zero β] [topological_space β] : fin_strongly_measurable (0 : α → β) μ := ⟨0, by simp only [pi.zero_apply, simple_func.coe_zero, support_zero', measure_empty, with_top.zero_lt_top, forall_const], λ n, tendsto_const_nhds⟩ namespace fin_strongly_measurable variables {m0 : measurable_space α} {μ : measure α} {f g : α → β} lemma ae_fin_strongly_measurable [has_zero β] [topological_space β] (hf : fin_strongly_measurable f μ) : ae_fin_strongly_measurable f μ := ⟨f, hf, ae_eq_refl f⟩ section sequence variables [has_zero β] [topological_space β] (hf : fin_strongly_measurable f μ) /-- A sequence of simple functions such that `∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x))` and `∀ n, μ (support (hf.approx n)) < ∞`. These properties are given by `fin_strongly_measurable.tendsto_approx` and `fin_strongly_measurable.fin_support_approx`. -/ protected noncomputable def approx : ℕ → α →ₛ β := hf.some protected lemma fin_support_approx : ∀ n, μ (support (hf.approx n)) < ∞ := hf.some_spec.1 protected lemma tendsto_approx : ∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x)) := hf.some_spec.2 end sequence protected lemma strongly_measurable [has_zero β] [topological_space β] (hf : fin_strongly_measurable f μ) : strongly_measurable f := ⟨hf.approx, hf.tendsto_approx⟩ lemma exists_set_sigma_finite [has_zero β] [topological_space β] [t2_space β] (hf : fin_strongly_measurable f μ) : ∃ t, measurable_set t ∧ (∀ x ∈ tᶜ, f x = 0) ∧ sigma_finite (μ.restrict t) := begin rcases hf with ⟨fs, hT_lt_top, h_approx⟩, let T := λ n, support (fs n), have hT_meas : ∀ n, measurable_set (T n), from λ n, simple_func.measurable_set_support (fs n), let t := ⋃ n, T n, refine ⟨t, measurable_set.Union hT_meas, _, _⟩, { have h_fs_zero : ∀ n, ∀ x ∈ tᶜ, fs n x = 0, { intros n x hxt, rw [set.mem_compl_iff, set.mem_Union, not_exists] at hxt, simpa using (hxt n), }, refine λ x hxt, tendsto_nhds_unique (h_approx x) _, rw funext (λ n, h_fs_zero n x hxt), exact tendsto_const_nhds, }, { refine ⟨⟨⟨λ n, tᶜ ∪ T n, λ n, trivial, λ n, _, _⟩⟩⟩, { rw [measure.restrict_apply' (measurable_set.Union hT_meas), set.union_inter_distrib_right, set.compl_inter_self t, set.empty_union], exact (measure_mono (set.inter_subset_left _ _)).trans_lt (hT_lt_top n), }, { rw ← set.union_Union tᶜ T, exact set.compl_union_self _ } } end /-- A finitely strongly measurable function is measurable. -/ protected lemma measurable [has_zero β] [topological_space β] [pseudo_metrizable_space β] [measurable_space β] [borel_space β] (hf : fin_strongly_measurable f μ) : measurable f := hf.strongly_measurable.measurable section arithmetic variables [topological_space β] protected lemma mul [monoid_with_zero β] [has_continuous_mul β] (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f * g) μ := begin refine ⟨λ n, hf.approx n * hg.approx n, _, λ x, (hf.tendsto_approx x).mul (hg.tendsto_approx x)⟩, intro n, exact (measure_mono (support_mul_subset_left _ _)).trans_lt (hf.fin_support_approx n), end protected lemma add [add_monoid β] [has_continuous_add β] (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f + g) μ := ⟨λ n, hf.approx n + hg.approx n, λ n, (measure_mono (function.support_add _ _)).trans_lt ((measure_union_le _ _).trans_lt (ennreal.add_lt_top.mpr ⟨hf.fin_support_approx n, hg.fin_support_approx n⟩)), λ x, (hf.tendsto_approx x).add (hg.tendsto_approx x)⟩ protected lemma neg [add_group β] [topological_add_group β] (hf : fin_strongly_measurable f μ) : fin_strongly_measurable (-f) μ := begin refine ⟨λ n, -hf.approx n, λ n, _, λ x, (hf.tendsto_approx x).neg⟩, suffices : μ (function.support (λ x, - (hf.approx n) x)) < ∞, by convert this, rw function.support_neg (hf.approx n), exact hf.fin_support_approx n, end protected lemma sub [add_group β] [has_continuous_sub β] (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f - g) μ := ⟨λ n, hf.approx n - hg.approx n, λ n, (measure_mono (function.support_sub _ _)).trans_lt ((measure_union_le _ _).trans_lt (ennreal.add_lt_top.mpr ⟨hf.fin_support_approx n, hg.fin_support_approx n⟩)), λ x, (hf.tendsto_approx x).sub (hg.tendsto_approx x)⟩ protected lemma const_smul {𝕜} [topological_space 𝕜] [add_monoid β] [monoid 𝕜] [distrib_mul_action 𝕜 β] [has_continuous_smul 𝕜 β] (hf : fin_strongly_measurable f μ) (c : 𝕜) : fin_strongly_measurable (c • f) μ := begin refine ⟨λ n, c • (hf.approx n), λ n, _, λ x, (hf.tendsto_approx x).const_smul c⟩, rw simple_func.coe_smul, refine (measure_mono (support_smul_subset_right c _)).trans_lt (hf.fin_support_approx n), end end arithmetic section order variables [topological_space β] [has_zero β] protected lemma sup [semilattice_sup β] [has_continuous_sup β] (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f ⊔ g) μ := begin refine ⟨λ n, hf.approx n ⊔ hg.approx n, λ n, _, λ x, (hf.tendsto_approx x).sup_right_nhds (hg.tendsto_approx x)⟩, refine (measure_mono (support_sup _ _)).trans_lt _, exact measure_union_lt_top_iff.mpr ⟨hf.fin_support_approx n, hg.fin_support_approx n⟩, end protected lemma inf [semilattice_inf β] [has_continuous_inf β] (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f ⊓ g) μ := begin refine ⟨λ n, hf.approx n ⊓ hg.approx n, λ n, _, λ x, (hf.tendsto_approx x).inf_right_nhds (hg.tendsto_approx x)⟩, refine (measure_mono (support_inf _ _)).trans_lt _, exact measure_union_lt_top_iff.mpr ⟨hf.fin_support_approx n, hg.fin_support_approx n⟩, end end order end fin_strongly_measurable lemma fin_strongly_measurable_iff_strongly_measurable_and_exists_set_sigma_finite {α β} {f : α → β} [topological_space β] [t2_space β] [has_zero β] {m : measurable_space α} {μ : measure α} : fin_strongly_measurable f μ ↔ (strongly_measurable f ∧ (∃ t, measurable_set t ∧ (∀ x ∈ tᶜ, f x = 0) ∧ sigma_finite (μ.restrict t))) := ⟨λ hf, ⟨hf.strongly_measurable, hf.exists_set_sigma_finite⟩, λ hf, hf.1.fin_strongly_measurable_of_set_sigma_finite hf.2.some_spec.1 hf.2.some_spec.2.1 hf.2.some_spec.2.2⟩ lemma ae_fin_strongly_measurable_zero {α β} {m : measurable_space α} (μ : measure α) [has_zero β] [topological_space β] : ae_fin_strongly_measurable (0 : α → β) μ := ⟨0, fin_strongly_measurable_zero, eventually_eq.rfl⟩ /-! ## Almost everywhere strongly measurable functions -/ lemma ae_strongly_measurable_const {α β} {m : measurable_space α} {μ : measure α} [topological_space β] {b : β} : ae_strongly_measurable (λ a : α, b) μ := strongly_measurable_const.ae_strongly_measurable @[to_additive] lemma ae_strongly_measurable_one {α β} {m : measurable_space α} {μ : measure α} [topological_space β] [has_one β] : ae_strongly_measurable (1 : α → β) μ := strongly_measurable_one.ae_strongly_measurable @[simp] lemma subsingleton.ae_strongly_measurable {m : measurable_space α} [topological_space β] [subsingleton β] {μ : measure α} (f : α → β) : ae_strongly_measurable f μ := (subsingleton.strongly_measurable f).ae_strongly_measurable @[simp] lemma subsingleton.ae_strongly_measurable' {m : measurable_space α} [topological_space β] [subsingleton α] {μ : measure α} (f : α → β) : ae_strongly_measurable f μ := (subsingleton.strongly_measurable' f).ae_strongly_measurable @[simp] lemma ae_strongly_measurable_zero_measure [measurable_space α] [topological_space β] (f : α → β) : ae_strongly_measurable f (0 : measure α) := begin nontriviality α, inhabit α, exact ⟨λ x, f default, strongly_measurable_const, rfl⟩ end lemma simple_func.ae_strongly_measurable {m : measurable_space α} {μ : measure α} [topological_space β] (f : α →ₛ β) : ae_strongly_measurable f μ := f.strongly_measurable.ae_strongly_measurable namespace ae_strongly_measurable variables {m : measurable_space α} {μ : measure α} [topological_space β] [topological_space γ] {f g : α → β} section mk /-- A `strongly_measurable` function such that `f =ᵐ[μ] hf.mk f`. See lemmas `strongly_measurable_mk` and `ae_eq_mk`. -/ protected noncomputable def mk (f : α → β) (hf : ae_strongly_measurable f μ) : α → β := hf.some lemma strongly_measurable_mk (hf : ae_strongly_measurable f μ) : strongly_measurable (hf.mk f) := hf.some_spec.1 lemma measurable_mk [pseudo_metrizable_space β] [measurable_space β] [borel_space β] (hf : ae_strongly_measurable f μ) : measurable (hf.mk f) := hf.strongly_measurable_mk.measurable lemma ae_eq_mk (hf : ae_strongly_measurable f μ) : f =ᵐ[μ] hf.mk f := hf.some_spec.2 protected lemma ae_measurable {β} [measurable_space β] [topological_space β] [pseudo_metrizable_space β] [borel_space β] {f : α → β} (hf : ae_strongly_measurable f μ) : ae_measurable f μ := ⟨hf.mk f, hf.strongly_measurable_mk.measurable, hf.ae_eq_mk⟩ end mk lemma congr (hf : ae_strongly_measurable f μ) (h : f =ᵐ[μ] g) : ae_strongly_measurable g μ := ⟨hf.mk f, hf.strongly_measurable_mk, h.symm.trans hf.ae_eq_mk⟩ lemma _root_.ae_strongly_measurable_congr (h : f =ᵐ[μ] g) : ae_strongly_measurable f μ ↔ ae_strongly_measurable g μ := ⟨λ hf, hf.congr h, λ hg, hg.congr h.symm⟩ lemma mono_measure {ν : measure α} (hf : ae_strongly_measurable f μ) (h : ν ≤ μ) : ae_strongly_measurable f ν := ⟨hf.mk f, hf.strongly_measurable_mk, eventually.filter_mono (ae_mono h) hf.ae_eq_mk⟩ protected lemma mono' {ν : measure α} (h : ae_strongly_measurable f μ) (h' : ν ≪ μ) : ae_strongly_measurable f ν := ⟨h.mk f, h.strongly_measurable_mk, h' h.ae_eq_mk⟩ lemma mono_set {s t} (h : s ⊆ t) (ht : ae_strongly_measurable f (μ.restrict t)) : ae_strongly_measurable f (μ.restrict s) := ht.mono_measure (restrict_mono h le_rfl) protected lemma restrict (hfm : ae_strongly_measurable f μ) {s} : ae_strongly_measurable f (μ.restrict s) := hfm.mono_measure measure.restrict_le_self lemma ae_mem_imp_eq_mk {s} (h : ae_strongly_measurable f (μ.restrict s)) : ∀ᵐ x ∂μ, x ∈ s → f x = h.mk f x := ae_imp_of_ae_restrict h.ae_eq_mk /-- The composition of a continuous function and an ae strongly measurable function is ae strongly measurable. -/ lemma _root_.continuous.comp_ae_strongly_measurable {g : β → γ} {f : α → β} (hg : continuous g) (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, g (f x)) μ := ⟨_, hg.comp_strongly_measurable hf.strongly_measurable_mk, eventually_eq.fun_comp hf.ae_eq_mk g⟩ /-- A continuous function from `α` to `β` is ae strongly measurable when one of the two spaces is second countable. -/ lemma _root_.continuous.ae_strongly_measurable [topological_space α] [opens_measurable_space α] [pseudo_metrizable_space β] [second_countable_topology_either α β] (hf : continuous f) : ae_strongly_measurable f μ := hf.strongly_measurable.ae_strongly_measurable protected lemma prod_mk {f : α → β} {g : α → γ} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (λ x, (f x, g x)) μ := ⟨λ x, (hf.mk f x, hg.mk g x), hf.strongly_measurable_mk.prod_mk hg.strongly_measurable_mk, hf.ae_eq_mk.prod_mk hg.ae_eq_mk⟩ /-- In a space with second countable topology, measurable implies ae strongly measurable. -/ lemma _root_.measurable.ae_strongly_measurable {m : measurable_space α} {μ : measure α} [measurable_space β] [pseudo_metrizable_space β] [second_countable_topology β] [opens_measurable_space β] (hf : measurable f) : ae_strongly_measurable f μ := hf.strongly_measurable.ae_strongly_measurable section arithmetic @[to_additive] protected lemma mul [has_mul β] [has_continuous_mul β] (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (f * g) μ := ⟨hf.mk f * hg.mk g, hf.strongly_measurable_mk.mul hg.strongly_measurable_mk, hf.ae_eq_mk.mul hg.ae_eq_mk⟩ @[to_additive] protected lemma mul_const [has_mul β] [has_continuous_mul β] (hf : ae_strongly_measurable f μ) (c : β) : ae_strongly_measurable (λ x, f x * c) μ := hf.mul ae_strongly_measurable_const @[to_additive] protected lemma const_mul [has_mul β] [has_continuous_mul β] (hf : ae_strongly_measurable f μ) (c : β) : ae_strongly_measurable (λ x, c * f x) μ := ae_strongly_measurable_const.mul hf @[to_additive] protected lemma inv [group β] [topological_group β] (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (f⁻¹) μ := ⟨(hf.mk f)⁻¹, hf.strongly_measurable_mk.inv, hf.ae_eq_mk.inv⟩ @[to_additive] protected lemma div [group β] [topological_group β] (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (f / g) μ := ⟨hf.mk f / hg.mk g, hf.strongly_measurable_mk.div hg.strongly_measurable_mk, hf.ae_eq_mk.div hg.ae_eq_mk⟩ @[to_additive] protected lemma smul {𝕜} [topological_space 𝕜] [has_smul 𝕜 β] [has_continuous_smul 𝕜 β] {f : α → 𝕜} {g : α → β} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (λ x, f x • g x) μ := continuous_smul.comp_ae_strongly_measurable (hf.prod_mk hg) protected lemma const_smul {𝕜} [has_smul 𝕜 β] [has_continuous_const_smul 𝕜 β] (hf : ae_strongly_measurable f μ) (c : 𝕜) : ae_strongly_measurable (c • f) μ := ⟨c • hf.mk f, hf.strongly_measurable_mk.const_smul c, hf.ae_eq_mk.const_smul c⟩ protected lemma const_smul' {𝕜} [has_smul 𝕜 β] [has_continuous_const_smul 𝕜 β] (hf : ae_strongly_measurable f μ) (c : 𝕜) : ae_strongly_measurable (λ x, c • (f x)) μ := hf.const_smul c @[to_additive] protected lemma smul_const {𝕜} [topological_space 𝕜] [has_smul 𝕜 β] [has_continuous_smul 𝕜 β] {f : α → 𝕜} (hf : ae_strongly_measurable f μ) (c : β) : ae_strongly_measurable (λ x, f x • c) μ := continuous_smul.comp_ae_strongly_measurable (hf.prod_mk ae_strongly_measurable_const) end arithmetic section order protected lemma sup [semilattice_sup β] [has_continuous_sup β] (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (f ⊔ g) μ := ⟨hf.mk f ⊔ hg.mk g, hf.strongly_measurable_mk.sup hg.strongly_measurable_mk, hf.ae_eq_mk.sup hg.ae_eq_mk⟩ protected lemma inf [semilattice_inf β] [has_continuous_inf β] (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (f ⊓ g) μ := ⟨hf.mk f ⊓ hg.mk g, hf.strongly_measurable_mk.inf hg.strongly_measurable_mk, hf.ae_eq_mk.inf hg.ae_eq_mk⟩ end order /-! ### Big operators: `∏` and `∑` -/ section monoid variables {M : Type*} [monoid M] [topological_space M] [has_continuous_mul M] @[to_additive] lemma _root_.list.ae_strongly_measurable_prod' (l : list (α → M)) (hl : ∀ f ∈ l, ae_strongly_measurable f μ) : ae_strongly_measurable l.prod μ := begin induction l with f l ihl, { exact ae_strongly_measurable_one }, rw [list.forall_mem_cons] at hl, rw [list.prod_cons], exact hl.1.mul (ihl hl.2) end @[to_additive] lemma _root_.list.ae_strongly_measurable_prod (l : list (α → M)) (hl : ∀ f ∈ l, ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, (l.map (λ f : α → M, f x)).prod) μ := by simpa only [← pi.list_prod_apply] using l.ae_strongly_measurable_prod' hl end monoid section comm_monoid variables {M : Type*} [comm_monoid M] [topological_space M] [has_continuous_mul M] @[to_additive] lemma _root_.multiset.ae_strongly_measurable_prod' (l : multiset (α → M)) (hl : ∀ f ∈ l, ae_strongly_measurable f μ) : ae_strongly_measurable l.prod μ := by { rcases l with ⟨l⟩, simpa using l.ae_strongly_measurable_prod' (by simpa using hl) } @[to_additive] lemma _root_.multiset.ae_strongly_measurable_prod (s : multiset (α → M)) (hs : ∀ f ∈ s, ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, (s.map (λ f : α → M, f x)).prod) μ := by simpa only [← pi.multiset_prod_apply] using s.ae_strongly_measurable_prod' hs @[to_additive] lemma _root_.finset.ae_strongly_measurable_prod' {ι : Type*} {f : ι → α → M} (s : finset ι) (hf : ∀i ∈ s, ae_strongly_measurable (f i) μ) : ae_strongly_measurable (∏ i in s, f i) μ := multiset.ae_strongly_measurable_prod' _ $ λ g hg, let ⟨i, hi, hg⟩ := multiset.mem_map.1 hg in (hg ▸ hf _ hi) @[to_additive] lemma _root_.finset.ae_strongly_measurable_prod {ι : Type*} {f : ι → α → M} (s : finset ι) (hf : ∀i ∈ s, ae_strongly_measurable (f i) μ) : ae_strongly_measurable (λ a, ∏ i in s, f i a) μ := by simpa only [← finset.prod_apply] using s.ae_strongly_measurable_prod' hf end comm_monoid section second_countable_ae_strongly_measurable variables [measurable_space β] /-- In a space with second countable topology, measurable implies strongly measurable. -/ lemma _root_.ae_measurable.ae_strongly_measurable [pseudo_metrizable_space β] [opens_measurable_space β] [second_countable_topology β] (hf : ae_measurable f μ) : ae_strongly_measurable f μ := ⟨hf.mk f, hf.measurable_mk.strongly_measurable, hf.ae_eq_mk⟩ lemma _root_.ae_strongly_measurable_id {α : Type*} [topological_space α] [pseudo_metrizable_space α] {m : measurable_space α} [opens_measurable_space α] [second_countable_topology α] {μ : measure α} : ae_strongly_measurable (id : α → α) μ := ae_measurable_id.ae_strongly_measurable /-- In a space with second countable topology, strongly measurable and measurable are equivalent. -/ lemma _root_.ae_strongly_measurable_iff_ae_measurable [pseudo_metrizable_space β] [borel_space β] [second_countable_topology β] : ae_strongly_measurable f μ ↔ ae_measurable f μ := ⟨λ h, h.ae_measurable, λ h, h.ae_strongly_measurable⟩ end second_countable_ae_strongly_measurable protected lemma dist {β : Type*} [pseudo_metric_space β] {f g : α → β} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (λ x, dist (f x) (g x)) μ := continuous_dist.comp_ae_strongly_measurable (hf.prod_mk hg) protected lemma norm {β : Type*} [seminormed_add_comm_group β] {f : α → β} (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, ‖f x‖) μ := continuous_norm.comp_ae_strongly_measurable hf protected lemma nnnorm {β : Type*} [seminormed_add_comm_group β] {f : α → β} (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, ‖f x‖₊) μ := continuous_nnnorm.comp_ae_strongly_measurable hf protected lemma ennnorm {β : Type*} [seminormed_add_comm_group β] {f : α → β} (hf : ae_strongly_measurable f μ) : ae_measurable (λ a, (‖f a‖₊ : ℝ≥0∞)) μ := (ennreal.continuous_coe.comp_ae_strongly_measurable hf.nnnorm).ae_measurable protected lemma edist {β : Type*} [seminormed_add_comm_group β] {f g : α → β} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_measurable (λ a, edist (f a) (g a)) μ := (continuous_edist.comp_ae_strongly_measurable (hf.prod_mk hg)).ae_measurable protected lemma real_to_nnreal {f : α → ℝ} (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, (f x).to_nnreal) μ := continuous_real_to_nnreal.comp_ae_strongly_measurable hf lemma _root_.ae_strongly_measurable_indicator_iff [has_zero β] {s : set α} (hs : measurable_set s) : ae_strongly_measurable (indicator s f) μ ↔ ae_strongly_measurable f (μ.restrict s) := begin split, { intro h, exact (h.mono_measure measure.restrict_le_self).congr (indicator_ae_eq_restrict hs) }, { intro h, refine ⟨indicator s (h.mk f), h.strongly_measurable_mk.indicator hs, _⟩, have A : s.indicator f =ᵐ[μ.restrict s] s.indicator (h.mk f) := (indicator_ae_eq_restrict hs).trans (h.ae_eq_mk.trans $ (indicator_ae_eq_restrict hs).symm), have B : s.indicator f =ᵐ[μ.restrict sᶜ] s.indicator (h.mk f) := (indicator_ae_eq_restrict_compl hs).trans (indicator_ae_eq_restrict_compl hs).symm, exact ae_of_ae_restrict_of_ae_restrict_compl _ A B }, end protected lemma indicator [has_zero β] (hfm : ae_strongly_measurable f μ) {s : set α} (hs : measurable_set s) : ae_strongly_measurable (s.indicator f) μ := (ae_strongly_measurable_indicator_iff hs).mpr hfm.restrict lemma null_measurable_set_eq_fun {E} [topological_space E] [metrizable_space E] {f g : α → E} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : null_measurable_set {x | f x = g x} μ := begin apply (hf.strongly_measurable_mk.measurable_set_eq_fun hg.strongly_measurable_mk) .null_measurable_set.congr, filter_upwards [hf.ae_eq_mk, hg.ae_eq_mk] with x hfx hgx, change (hf.mk f x = hg.mk g x) = (f x = g x), simp only [hfx, hgx] end lemma null_measurable_set_lt [linear_order β] [order_closed_topology β] [pseudo_metrizable_space β] {f g : α → β} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : null_measurable_set {a | f a < g a} μ := begin apply (hf.strongly_measurable_mk.measurable_set_lt hg.strongly_measurable_mk) .null_measurable_set.congr, filter_upwards [hf.ae_eq_mk, hg.ae_eq_mk] with x hfx hgx, change (hf.mk f x < hg.mk g x) = (f x < g x), simp only [hfx, hgx] end lemma null_measurable_set_le [preorder β] [order_closed_topology β] [pseudo_metrizable_space β] {f g : α → β} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : null_measurable_set {a | f a ≤ g a} μ := begin apply (hf.strongly_measurable_mk.measurable_set_le hg.strongly_measurable_mk) .null_measurable_set.congr, filter_upwards [hf.ae_eq_mk, hg.ae_eq_mk] with x hfx hgx, change (hf.mk f x ≤ hg.mk g x) = (f x ≤ g x), simp only [hfx, hgx] end lemma _root_.ae_strongly_measurable_of_ae_strongly_measurable_trim {α} {m m0 : measurable_space α} {μ : measure α} (hm : m ≤ m0) {f : α → β} (hf : ae_strongly_measurable f (μ.trim hm)) : ae_strongly_measurable f μ := ⟨hf.mk f, strongly_measurable.mono hf.strongly_measurable_mk hm, ae_eq_of_ae_eq_trim hf.ae_eq_mk⟩ lemma comp_ae_measurable {γ : Type*} {mγ : measurable_space γ} {mα : measurable_space α} {f : γ → α} {μ : measure γ} (hg : ae_strongly_measurable g (measure.map f μ)) (hf : ae_measurable f μ) : ae_strongly_measurable (g ∘ f) μ := ⟨(hg.mk g) ∘ hf.mk f, hg.strongly_measurable_mk.comp_measurable hf.measurable_mk, (ae_eq_comp hf hg.ae_eq_mk).trans ((hf.ae_eq_mk).fun_comp (hg.mk g))⟩ lemma comp_measurable {γ : Type*} {mγ : measurable_space γ} {mα : measurable_space α} {f : γ → α} {μ : measure γ} (hg : ae_strongly_measurable g (measure.map f μ)) (hf : measurable f) : ae_strongly_measurable (g ∘ f) μ := hg.comp_ae_measurable hf.ae_measurable lemma comp_quasi_measure_preserving {γ : Type*} {mγ : measurable_space γ} {mα : measurable_space α} {f : γ → α} {μ : measure γ} {ν : measure α} (hg : ae_strongly_measurable g ν) (hf : quasi_measure_preserving f μ ν) : ae_strongly_measurable (g ∘ f) μ := (hg.mono' hf.absolutely_continuous).comp_measurable hf.measurable lemma is_separable_ae_range (hf : ae_strongly_measurable f μ) : ∃ (t : set β), is_separable t ∧ ∀ᵐ x ∂μ, f x ∈ t := begin refine ⟨range (hf.mk f), hf.strongly_measurable_mk.is_separable_range, _⟩, filter_upwards [hf.ae_eq_mk] with x hx, simp [hx] end /-- A function is almost everywhere strongly measurable if and only if it is almost everywhere measurable, and up to a zero measure set its range is contained in a separable set. -/ theorem _root_.ae_strongly_measurable_iff_ae_measurable_separable [pseudo_metrizable_space β] [measurable_space β] [borel_space β] : ae_strongly_measurable f μ ↔ (ae_measurable f μ ∧ ∃ (t : set β), is_separable t ∧ ∀ᵐ x ∂μ, f x ∈ t) := begin refine ⟨λ H, ⟨H.ae_measurable, H.is_separable_ae_range⟩, _⟩, rintros ⟨H, ⟨t, t_sep, ht⟩⟩, rcases eq_empty_or_nonempty t with rfl|h₀, { simp only [mem_empty_iff_false, eventually_false_iff_eq_bot, ae_eq_bot] at ht, rw ht, exact ae_strongly_measurable_zero_measure f }, { obtain ⟨g, g_meas, gt, fg⟩ : ∃ (g : α → β), measurable g ∧ range g ⊆ t ∧ f =ᵐ[μ] g := H.exists_ae_eq_range_subset ht h₀, refine ⟨g, _, fg⟩, exact strongly_measurable_iff_measurable_separable.2 ⟨g_meas, t_sep.mono gt⟩ } end lemma _root_.measurable_embedding.ae_strongly_measurable_map_iff {γ : Type*} {mγ : measurable_space γ} {mα : measurable_space α} {f : γ → α} {μ : measure γ} (hf : measurable_embedding f) {g : α → β} : ae_strongly_measurable g (measure.map f μ) ↔ ae_strongly_measurable (g ∘ f) μ := begin refine ⟨λ H, H.comp_measurable hf.measurable, _⟩, rintro ⟨g₁, hgm₁, heq⟩, rcases hf.exists_strongly_measurable_extend hgm₁ (λ x, ⟨g x⟩) with ⟨g₂, hgm₂, rfl⟩, exact ⟨g₂, hgm₂, hf.ae_map_iff.2 heq⟩ end lemma _root_.embedding.ae_strongly_measurable_comp_iff [pseudo_metrizable_space β] [pseudo_metrizable_space γ] {g : β → γ} {f : α → β} (hg : embedding g) : ae_strongly_measurable (λ x, g (f x)) μ ↔ ae_strongly_measurable f μ := begin letI := pseudo_metrizable_space_pseudo_metric γ, borelize [β, γ], refine ⟨λ H, ae_strongly_measurable_iff_ae_measurable_separable.2 ⟨_, _⟩, λ H, hg.continuous.comp_ae_strongly_measurable H⟩, { let G : β → range g := cod_restrict g (range g) mem_range_self, have hG : closed_embedding G := { closed_range := begin convert is_closed_univ, apply eq_univ_of_forall, rintros ⟨-, ⟨x, rfl⟩⟩, exact mem_range_self x end, .. hg.cod_restrict _ _ }, have : ae_measurable (G ∘ f) μ := ae_measurable.subtype_mk H.ae_measurable, exact hG.measurable_embedding.ae_measurable_comp_iff.1 this }, { rcases (ae_strongly_measurable_iff_ae_measurable_separable.1 H).2 with ⟨t, ht, h't⟩, exact ⟨g⁻¹' t, hg.is_separable_preimage ht, h't⟩ } end lemma _root_.measure_theory.measure_preserving.ae_strongly_measurable_comp_iff {β : Type*} {f : α → β} {mα : measurable_space α} {μa : measure α} {mβ : measurable_space β} {μb : measure β} (hf : measure_preserving f μa μb) (h₂ : measurable_embedding f) {g : β → γ} : ae_strongly_measurable (g ∘ f) μa ↔ ae_strongly_measurable g μb := by rw [← hf.map_eq, h₂.ae_strongly_measurable_map_iff] /-- An almost everywhere sequential limit of almost everywhere strongly measurable functions is almost everywhere strongly measurable. -/ lemma _root_.ae_strongly_measurable_of_tendsto_ae {ι : Type*} [pseudo_metrizable_space β] (u : filter ι) [ne_bot u] [is_countably_generated u] {f : ι → α → β} {g : α → β} (hf : ∀ i, ae_strongly_measurable (f i) μ) (lim : ∀ᵐ x ∂μ, tendsto (λ n, f n x) u (𝓝 (g x))) : ae_strongly_measurable g μ := begin borelize β, refine ae_strongly_measurable_iff_ae_measurable_separable.2 ⟨_, _⟩, { exact ae_measurable_of_tendsto_metrizable_ae _ (λ n, (hf n).ae_measurable) lim }, { rcases u.exists_seq_tendsto with ⟨v, hv⟩, have : ∀ (n : ℕ), ∃ (t : set β), is_separable t ∧ f (v n) ⁻¹' t ∈ μ.ae := λ n, (ae_strongly_measurable_iff_ae_measurable_separable.1 (hf (v n))).2, choose t t_sep ht using this, refine ⟨closure (⋃ i, (t i)), (is_separable_Union (λ i, (t_sep i))).closure, _⟩, filter_upwards [ae_all_iff.2 ht, lim] with x hx h'x, apply mem_closure_of_tendsto ((h'x).comp hv), apply eventually_of_forall (λ n, _), apply mem_Union_of_mem n, exact hx n } end /-- If a sequence of almost everywhere strongly measurable functions converges almost everywhere, one can select a strongly measurable function as the almost everywhere limit. -/ lemma _root_.exists_strongly_measurable_limit_of_tendsto_ae [pseudo_metrizable_space β] {f : ℕ → α → β} (hf : ∀ n, ae_strongly_measurable (f n) μ) (h_ae_tendsto : ∀ᵐ x ∂μ, ∃ l : β, tendsto (λ n, f n x) at_top (𝓝 l)) : ∃ (f_lim : α → β) (hf_lim_meas : strongly_measurable f_lim), ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x)) := begin borelize β, obtain ⟨g, g_meas, hg⟩ : ∃ (g : α → β) (g_meas : measurable g), ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (𝓝 (g x)) := measurable_limit_of_tendsto_metrizable_ae (λ n, (hf n).ae_measurable) h_ae_tendsto, have Hg : ae_strongly_measurable g μ := ae_strongly_measurable_of_tendsto_ae _ hf hg, refine ⟨Hg.mk g, Hg.strongly_measurable_mk, _⟩, filter_upwards [hg, Hg.ae_eq_mk] with x hx h'x, rwa h'x at hx, end lemma sum_measure [pseudo_metrizable_space β] {m : measurable_space α} {μ : ι → measure α} (h : ∀ i, ae_strongly_measurable f (μ i)) : ae_strongly_measurable f (measure.sum μ) := begin borelize β, refine ae_strongly_measurable_iff_ae_measurable_separable.2 ⟨ae_measurable.sum_measure (λ i, (h i).ae_measurable), _⟩, have A : ∀ (i : ι), ∃ (t : set β), is_separable t ∧ f ⁻¹' t ∈ (μ i).ae := λ i, (ae_strongly_measurable_iff_ae_measurable_separable.1 (h i)).2, choose t t_sep ht using A, refine ⟨(⋃ i, t i), is_separable_Union t_sep, _⟩, simp only [measure.ae_sum_eq, mem_Union, eventually_supr], assume i, filter_upwards [ht i] with x hx, exact ⟨i, hx⟩ end @[simp] lemma _root_.ae_strongly_measurable_sum_measure_iff [pseudo_metrizable_space β] {m : measurable_space α} {μ : ι → measure α} : ae_strongly_measurable f (sum μ) ↔ ∀ i, ae_strongly_measurable f (μ i) := ⟨λ h i, h.mono_measure (measure.le_sum _ _), sum_measure⟩ @[simp] lemma _root_.ae_strongly_measurable_add_measure_iff [pseudo_metrizable_space β] {ν : measure α} : ae_strongly_measurable f (μ + ν) ↔ ae_strongly_measurable f μ ∧ ae_strongly_measurable f ν := by { rw [← sum_cond, ae_strongly_measurable_sum_measure_iff, bool.forall_bool, and.comm], refl } lemma add_measure [pseudo_metrizable_space β] {ν : measure α} {f : α → β} (hμ : ae_strongly_measurable f μ) (hν : ae_strongly_measurable f ν) : ae_strongly_measurable f (μ + ν) := ae_strongly_measurable_add_measure_iff.2 ⟨hμ, hν⟩ protected lemma Union [pseudo_metrizable_space β] {s : ι → set α} (h : ∀ i, ae_strongly_measurable f (μ.restrict (s i))) : ae_strongly_measurable f (μ.restrict (⋃ i, s i)) := (sum_measure h).mono_measure $ restrict_Union_le @[simp] lemma _root_.ae_strongly_measurable_Union_iff [pseudo_metrizable_space β] {s : ι → set α} : ae_strongly_measurable f (μ.restrict (⋃ i, s i)) ↔ ∀ i, ae_strongly_measurable f (μ.restrict (s i)) := ⟨λ h i, h.mono_measure $ restrict_mono (subset_Union _ _) le_rfl, ae_strongly_measurable.Union⟩ @[simp] lemma _root_.ae_strongly_measurable_union_iff [pseudo_metrizable_space β] {s t : set α} : ae_strongly_measurable f (μ.restrict (s ∪ t)) ↔ ae_strongly_measurable f (μ.restrict s) ∧ ae_strongly_measurable f (μ.restrict t) := by simp only [union_eq_Union, ae_strongly_measurable_Union_iff, bool.forall_bool, cond, and.comm] lemma ae_strongly_measurable_uIoc_iff [linear_order α] [pseudo_metrizable_space β] {f : α → β} {a b : α} : ae_strongly_measurable f (μ.restrict $ uIoc a b) ↔ ae_strongly_measurable f (μ.restrict $ Ioc a b) ∧ ae_strongly_measurable f (μ.restrict $ Ioc b a) := by rw [uIoc_eq_union, ae_strongly_measurable_union_iff] lemma smul_measure {R : Type*} [monoid R] [distrib_mul_action R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] (h : ae_strongly_measurable f μ) (c : R) : ae_strongly_measurable f (c • μ) := ⟨h.mk f, h.strongly_measurable_mk, ae_smul_measure h.ae_eq_mk c⟩ section normed_space variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] [complete_space 𝕜] variables {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] lemma _root_.ae_strongly_measurable_smul_const_iff {f : α → 𝕜} {c : E} (hc : c ≠ 0) : ae_strongly_measurable (λ x, f x • c) μ ↔ ae_strongly_measurable f μ := (closed_embedding_smul_left hc).to_embedding.ae_strongly_measurable_comp_iff end normed_space section mul_action variables {G : Type*} [group G] [mul_action G β] [has_continuous_const_smul G β] lemma _root_.ae_strongly_measurable_const_smul_iff (c : G) : ae_strongly_measurable (λ x, c • f x) μ ↔ ae_strongly_measurable f μ := ⟨λ h, by simpa only [inv_smul_smul] using h.const_smul' c⁻¹, λ h, h.const_smul c⟩ variables {G₀ : Type*} [group_with_zero G₀] [mul_action G₀ β] [has_continuous_const_smul G₀ β] lemma _root_.ae_strongly_measurable_const_smul_iff₀ {c : G₀} (hc : c ≠ 0) : ae_strongly_measurable (λ x, c • f x) μ ↔ ae_strongly_measurable f μ := begin refine ⟨λ h, _, λ h, h.const_smul c⟩, convert h.const_smul' c⁻¹, simp [smul_smul, inv_mul_cancel hc] end end mul_action section continuous_linear_map_nontrivially_normed_field variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] variables {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] variables {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] variables {G : Type*} [normed_add_comm_group G] [normed_space 𝕜 G] lemma _root_.strongly_measurable.apply_continuous_linear_map {m : measurable_space α} {φ : α → F →L[𝕜] E} (hφ : strongly_measurable φ) (v : F) : strongly_measurable (λ a, φ a v) := (continuous_linear_map.apply 𝕜 E v).continuous.comp_strongly_measurable hφ lemma apply_continuous_linear_map {φ : α → F →L[𝕜] E} (hφ : ae_strongly_measurable φ μ) (v : F) : ae_strongly_measurable (λ a, φ a v) μ := (continuous_linear_map.apply 𝕜 E v).continuous.comp_ae_strongly_measurable hφ lemma _root_.continuous_linear_map.ae_strongly_measurable_comp₂ (L : E →L[𝕜] F →L[𝕜] G) {f : α → E} {g : α → F} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (λ x, L (f x) (g x)) μ := L.continuous₂.comp_ae_strongly_measurable $ hf.prod_mk hg end continuous_linear_map_nontrivially_normed_field lemma _root_.ae_strongly_measurable_with_density_iff {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] {f : α → ℝ≥0} (hf : measurable f) {g : α → E} : ae_strongly_measurable g (μ.with_density (λ x, (f x : ℝ≥0∞))) ↔ ae_strongly_measurable (λ x, (f x : ℝ) • g x) μ := begin split, { rintros ⟨g', g'meas, hg'⟩, have A : measurable_set {x : α | f x ≠ 0} := (hf (measurable_set_singleton 0)).compl, refine ⟨λ x, (f x : ℝ) • g' x, hf.coe_nnreal_real.strongly_measurable.smul g'meas, _⟩, apply @ae_of_ae_restrict_of_ae_restrict_compl _ _ _ {x | f x ≠ 0}, { rw [eventually_eq, ae_with_density_iff hf.coe_nnreal_ennreal] at hg', rw ae_restrict_iff' A, filter_upwards [hg'] with a ha h'a, have : (f a : ℝ≥0∞) ≠ 0, by simpa only [ne.def, ennreal.coe_eq_zero] using h'a, rw ha this }, { filter_upwards [ae_restrict_mem A.compl] with x hx, simp only [not_not, mem_set_of_eq, mem_compl_iff] at hx, simp [hx] } }, { rintros ⟨g', g'meas, hg'⟩, refine ⟨λ x, (f x : ℝ)⁻¹ • g' x, hf.coe_nnreal_real.inv.strongly_measurable.smul g'meas, _⟩, rw [eventually_eq, ae_with_density_iff hf.coe_nnreal_ennreal], filter_upwards [hg'] with x hx h'x, rw [← hx, smul_smul, _root_.inv_mul_cancel, one_smul], simp only [ne.def, ennreal.coe_eq_zero] at h'x, simpa only [nnreal.coe_eq_zero, ne.def] using h'x } end end ae_strongly_measurable /-! ## Almost everywhere finitely strongly measurable functions -/ namespace ae_fin_strongly_measurable variables {m : measurable_space α} {μ : measure α} [topological_space β] {f g : α → β} section mk variables [has_zero β] /-- A `fin_strongly_measurable` function such that `f =ᵐ[μ] hf.mk f`. See lemmas `fin_strongly_measurable_mk` and `ae_eq_mk`. -/ protected noncomputable def mk (f : α → β) (hf : ae_fin_strongly_measurable f μ) : α → β := hf.some lemma fin_strongly_measurable_mk (hf : ae_fin_strongly_measurable f μ) : fin_strongly_measurable (hf.mk f) μ := hf.some_spec.1 lemma ae_eq_mk (hf : ae_fin_strongly_measurable f μ) : f =ᵐ[μ] hf.mk f := hf.some_spec.2 protected lemma ae_measurable {β} [has_zero β] [measurable_space β] [topological_space β] [pseudo_metrizable_space β] [borel_space β] {f : α → β} (hf : ae_fin_strongly_measurable f μ) : ae_measurable f μ := ⟨hf.mk f, hf.fin_strongly_measurable_mk.measurable, hf.ae_eq_mk⟩ end mk section arithmetic protected lemma mul [monoid_with_zero β] [has_continuous_mul β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f * g) μ := ⟨hf.mk f * hg.mk g, hf.fin_strongly_measurable_mk.mul hg.fin_strongly_measurable_mk, hf.ae_eq_mk.mul hg.ae_eq_mk⟩ protected lemma add [add_monoid β] [has_continuous_add β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f + g) μ := ⟨hf.mk f + hg.mk g, hf.fin_strongly_measurable_mk.add hg.fin_strongly_measurable_mk, hf.ae_eq_mk.add hg.ae_eq_mk⟩ protected lemma neg [add_group β] [topological_add_group β] (hf : ae_fin_strongly_measurable f μ) : ae_fin_strongly_measurable (-f) μ := ⟨-hf.mk f, hf.fin_strongly_measurable_mk.neg, hf.ae_eq_mk.neg⟩ protected lemma sub [add_group β] [has_continuous_sub β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f - g) μ := ⟨hf.mk f - hg.mk g, hf.fin_strongly_measurable_mk.sub hg.fin_strongly_measurable_mk, hf.ae_eq_mk.sub hg.ae_eq_mk⟩ protected lemma const_smul {𝕜} [topological_space 𝕜] [add_monoid β] [monoid 𝕜] [distrib_mul_action 𝕜 β] [has_continuous_smul 𝕜 β] (hf : ae_fin_strongly_measurable f μ) (c : 𝕜) : ae_fin_strongly_measurable (c • f) μ := ⟨c • hf.mk f, hf.fin_strongly_measurable_mk.const_smul c, hf.ae_eq_mk.const_smul c⟩ end arithmetic section order variables [has_zero β] protected lemma sup [semilattice_sup β] [has_continuous_sup β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f ⊔ g) μ := ⟨hf.mk f ⊔ hg.mk g, hf.fin_strongly_measurable_mk.sup hg.fin_strongly_measurable_mk, hf.ae_eq_mk.sup hg.ae_eq_mk⟩ protected lemma inf [semilattice_inf β] [has_continuous_inf β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f ⊓ g) μ := ⟨hf.mk f ⊓ hg.mk g, hf.fin_strongly_measurable_mk.inf hg.fin_strongly_measurable_mk, hf.ae_eq_mk.inf hg.ae_eq_mk⟩ end order variables [has_zero β] [t2_space β] lemma exists_set_sigma_finite (hf : ae_fin_strongly_measurable f μ) : ∃ t, measurable_set t ∧ f =ᵐ[μ.restrict tᶜ] 0 ∧ sigma_finite (μ.restrict t) := begin rcases hf with ⟨g, hg, hfg⟩, obtain ⟨t, ht, hgt_zero, htμ⟩ := hg.exists_set_sigma_finite, refine ⟨t, ht, _, htμ⟩, refine eventually_eq.trans (ae_restrict_of_ae hfg) _, rw [eventually_eq, ae_restrict_iff' ht.compl], exact eventually_of_forall hgt_zero, end /-- A measurable set `t` such that `f =ᵐ[μ.restrict tᶜ] 0` and `sigma_finite (μ.restrict t)`. -/ def sigma_finite_set (hf : ae_fin_strongly_measurable f μ) : set α := hf.exists_set_sigma_finite.some protected lemma measurable_set (hf : ae_fin_strongly_measurable f μ) : measurable_set hf.sigma_finite_set := hf.exists_set_sigma_finite.some_spec.1 lemma ae_eq_zero_compl (hf : ae_fin_strongly_measurable f μ) : f =ᵐ[μ.restrict hf.sigma_finite_setᶜ] 0 := hf.exists_set_sigma_finite.some_spec.2.1 instance sigma_finite_restrict (hf : ae_fin_strongly_measurable f μ) : sigma_finite (μ.restrict hf.sigma_finite_set) := hf.exists_set_sigma_finite.some_spec.2.2 end ae_fin_strongly_measurable section second_countable_topology variables {G : Type*} {p : ℝ≥0∞} {m m0 : measurable_space α} {μ : measure α} [seminormed_add_comm_group G] [measurable_space G] [borel_space G] [second_countable_topology G] {f : α → G} /-- In a space with second countable topology and a sigma-finite measure, `fin_strongly_measurable` and `measurable` are equivalent. -/ lemma fin_strongly_measurable_iff_measurable {m0 : measurable_space α} (μ : measure α) [sigma_finite μ] : fin_strongly_measurable f μ ↔ measurable f := ⟨λ h, h.measurable, λ h, (measurable.strongly_measurable h).fin_strongly_measurable μ⟩ /-- In a space with second countable topology and a sigma-finite measure, `ae_fin_strongly_measurable` and `ae_measurable` are equivalent. -/ lemma ae_fin_strongly_measurable_iff_ae_measurable {m0 : measurable_space α} (μ : measure α) [sigma_finite μ] : ae_fin_strongly_measurable f μ ↔ ae_measurable f μ := by simp_rw [ae_fin_strongly_measurable, ae_measurable, fin_strongly_measurable_iff_measurable] end second_countable_topology lemma measurable_uncurry_of_continuous_of_measurable {α β ι : Type*} [topological_space ι] [metrizable_space ι] [measurable_space ι] [second_countable_topology ι] [opens_measurable_space ι] {mβ : measurable_space β} [topological_space β] [pseudo_metrizable_space β] [borel_space β] {m : measurable_space α} {u : ι → α → β} (hu_cont : ∀ x, continuous (λ i, u i x)) (h : ∀ i, measurable (u i)) : measurable (function.uncurry u) := begin obtain ⟨t_sf, ht_sf⟩ : ∃ t : ℕ → simple_func ι ι, ∀ j x, tendsto (λ n, u (t n j) x) at_top (𝓝 $ u j x), { have h_str_meas : strongly_measurable (id : ι → ι), from strongly_measurable_id, refine ⟨h_str_meas.approx, λ j x, _⟩, exact ((hu_cont x).tendsto j).comp (h_str_meas.tendsto_approx j), }, let U := λ (n : ℕ) (p : ι × α), u (t_sf n p.fst) p.snd, have h_tendsto : tendsto U at_top (𝓝 (λ p, u p.fst p.snd)), { rw tendsto_pi_nhds, exact λ p, ht_sf p.fst p.snd, }, refine measurable_of_tendsto_metrizable (λ n, _) h_tendsto, have h_meas : measurable (λ (p : (t_sf n).range × α), u ↑p.fst p.snd), { have : (λ (p : ↥((t_sf n).range) × α), u ↑(p.fst) p.snd) = (λ (p : α × ((t_sf n).range)), u ↑(p.snd) p.fst) ∘ prod.swap := rfl, rw [this, @measurable_swap_iff α ↥((t_sf n).range) β m], exact measurable_from_prod_countable (λ j, h j), }, have : (λ p : ι × α, u (t_sf n p.fst) p.snd) = (λ p : ↥(t_sf n).range × α, u p.fst p.snd) ∘ (λ p : ι × α, (⟨t_sf n p.fst, simple_func.mem_range_self _ _⟩, p.snd)) := rfl, simp_rw [U, this], refine h_meas.comp (measurable.prod_mk _ measurable_snd), exact ((t_sf n).measurable.comp measurable_fst).subtype_mk, end lemma strongly_measurable_uncurry_of_continuous_of_strongly_measurable {α β ι : Type*} [topological_space ι] [metrizable_space ι] [measurable_space ι] [second_countable_topology ι] [opens_measurable_space ι] [topological_space β] [pseudo_metrizable_space β] [measurable_space α] {u : ι → α → β} (hu_cont : ∀ x, continuous (λ i, u i x)) (h : ∀ i, strongly_measurable (u i)) : strongly_measurable (function.uncurry u) := begin borelize β, obtain ⟨t_sf, ht_sf⟩ : ∃ t : ℕ → simple_func ι ι, ∀ j x, tendsto (λ n, u (t n j) x) at_top (𝓝 $ u j x), { have h_str_meas : strongly_measurable (id : ι → ι), from strongly_measurable_id, refine ⟨h_str_meas.approx, λ j x, _⟩, exact ((hu_cont x).tendsto j).comp (h_str_meas.tendsto_approx j), }, let U := λ (n : ℕ) (p : ι × α), u (t_sf n p.fst) p.snd, have h_tendsto : tendsto U at_top (𝓝 (λ p, u p.fst p.snd)), { rw tendsto_pi_nhds, exact λ p, ht_sf p.fst p.snd, }, refine strongly_measurable_of_tendsto _ (λ n, _) h_tendsto, have h_str_meas : strongly_measurable (λ (p : (t_sf n).range × α), u ↑p.fst p.snd), { refine strongly_measurable_iff_measurable_separable.2 ⟨_, _⟩, { have : (λ (p : ↥((t_sf n).range) × α), u ↑(p.fst) p.snd) = (λ (p : α × ((t_sf n).range)), u ↑(p.snd) p.fst) ∘ prod.swap := rfl, rw [this, measurable_swap_iff], exact measurable_from_prod_countable (λ j, (h j).measurable), }, { have : is_separable (⋃ (i : (t_sf n).range), range (u i)) := is_separable_Union (λ i, (h i).is_separable_range), apply this.mono, rintros _ ⟨⟨i, x⟩, rfl⟩, simp only [mem_Union, mem_range], exact ⟨i, x, rfl⟩ } }, have : (λ p : ι × α, u (t_sf n p.fst) p.snd) = (λ p : ↥(t_sf n).range × α, u p.fst p.snd) ∘ (λ p : ι × α, (⟨t_sf n p.fst, simple_func.mem_range_self _ _⟩, p.snd)) := rfl, simp_rw [U, this], refine h_str_meas.comp_measurable (measurable.prod_mk _ measurable_snd), exact ((t_sf n).measurable.comp measurable_fst).subtype_mk, end end measure_theory -- Guard against import creep assert_not_exists inner_product_space
353681d5308ffc40bdb79f84c6c1fa936fe89f4b
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/linear_algebra/adic_completion.lean
9ac176a2b4f816cd46ebc5ea77df02cd3b753bb4
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
8,556
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 linear_algebra.smodeq import ring_theory.ideal.operations /-! # Completion of a module with respect to an ideal. In this file we define the notions of Hausdorff, precomplete, and complete for an `R`-module `M` with respect to an ideal `I`: ## Main definitions - `is_Hausdorff I M`: this says that the intersection of `I^n M` is `0`. - `is_precomplete I M`: this says that every Cauchy sequence converges. - `is_adic_complete I M`: this says that `M` is Hausdorff and precomplete. - `Hausdorffification I M`: this is the universal Hausdorff module with a map from `M`. - `completion I M`: if `I` is finitely generated, then this is the universal complete module (TODO) with a map from `M`. This map is injective iff `M` is Hausdorff and surjective iff `M` is precomplete. -/ open submodule variables {R : Type*} [comm_ring R] (I : ideal R) variables (M : Type*) [add_comm_group M] [module R M] variables {N : Type*} [add_comm_group N] [module R N] /-- A module `M` is Hausdorff with respect to an ideal `I` if `⋂ I^n M = 0`. -/ class is_Hausdorff : Prop := (haus' : ∀ x : M, (∀ n : ℕ, x ≡ 0 [SMOD (I ^ n • ⊤ : submodule R M)]) → x = 0) /-- A module `M` is precomplete with respect to an ideal `I` if every Cauchy sequence converges. -/ class is_precomplete : Prop := (prec' : ∀ f : ℕ → M, (∀ {m n}, m ≤ n → f m ≡ f n [SMOD (I ^ m • ⊤ : submodule R M)]) → ∃ L : M, ∀ n, f n ≡ L [SMOD (I ^ n • ⊤ : submodule R M)]) /-- A module `M` is `I`-adically complete if it is Hausdorff and precomplete. -/ class is_adic_complete extends is_Hausdorff I M, is_precomplete I M : Prop variables {I M} theorem is_Hausdorff.haus (h : is_Hausdorff I M) : ∀ x : M, (∀ n : ℕ, x ≡ 0 [SMOD (I ^ n • ⊤ : submodule R M)]) → x = 0 := is_Hausdorff.haus' theorem is_Hausdorff_iff : is_Hausdorff I M ↔ ∀ x : M, (∀ n : ℕ, x ≡ 0 [SMOD (I ^ n • ⊤ : submodule R M)]) → x = 0 := ⟨is_Hausdorff.haus, λ h, ⟨h⟩⟩ theorem is_precomplete.prec (h : is_precomplete I M) {f : ℕ → M} : (∀ {m n}, m ≤ n → f m ≡ f n [SMOD (I ^ m • ⊤ : submodule R M)]) → ∃ L : M, ∀ n, f n ≡ L [SMOD (I ^ n • ⊤ : submodule R M)] := is_precomplete.prec' _ theorem is_precomplete_iff : is_precomplete I M ↔ ∀ f : ℕ → M, (∀ {m n}, m ≤ n → f m ≡ f n [SMOD (I ^ m • ⊤ : submodule R M)]) → ∃ L : M, ∀ n, f n ≡ L [SMOD (I ^ n • ⊤ : submodule R M)] := ⟨λ h, h.1, λ h, ⟨h⟩⟩ variables (I M) /-- The Hausdorffification of a module with respect to an ideal. -/ @[reducible] def Hausdorffification : Type* := (⨅ n : ℕ, I ^ n • ⊤ : submodule R M).quotient /-- The completion of a module with respect to an ideal. This is not necessarily Hausdorff. In fact, this is only complete if the ideal is finitely generated. -/ def adic_completion : submodule R (Π n : ℕ, (I ^ n • ⊤ : submodule R M).quotient) := { carrier := { f | ∀ {m n} (h : m ≤ n), liftq _ (mkq _) (by { rw ker_mkq, exact smul_mono (ideal.pow_le_pow h) (le_refl _) }) (f n) = f m }, zero_mem' := λ m n hmn, by rw [pi.zero_apply, pi.zero_apply, linear_map.map_zero], add_mem' := λ f g hf hg m n hmn, by rw [pi.add_apply, pi.add_apply, linear_map.map_add, hf hmn, hg hmn], smul_mem' := λ c f hf m n hmn, by rw [pi.smul_apply, pi.smul_apply, linear_map.map_smul, hf hmn] } namespace is_Hausdorff instance bot : is_Hausdorff (⊥ : ideal R) M := ⟨λ x hx, by simpa only [pow_one ⊥, bot_smul, smodeq.bot] using hx 1⟩ variables {M} protected theorem subsingleton (h : is_Hausdorff (⊤ : ideal R) M) : subsingleton M := ⟨λ x y, eq_of_sub_eq_zero $ h.haus (x - y) $ λ n, by { rw [ideal.top_pow, top_smul], exact smodeq.top }⟩ variables (M) @[priority 100] instance of_subsingleton [subsingleton M] : is_Hausdorff I M := ⟨λ x _, subsingleton.elim _ _⟩ variables {I M} theorem infi_pow_smul (h : is_Hausdorff I M) : (⨅ n : ℕ, I ^ n • ⊤ : submodule R M) = ⊥ := eq_bot_iff.2 $ λ x hx, (mem_bot _).2 $ h.haus x $ λ n, smodeq.zero.2 $ (mem_infi $ λ n : ℕ, I ^ n • ⊤).1 hx n end is_Hausdorff namespace Hausdorffification /-- The canonical linear map to the Hausdorffification. -/ def of : M →ₗ[R] Hausdorffification I M := mkq _ variables {I M} @[elab_as_eliminator] lemma induction_on {C : Hausdorffification I M → Prop} (x : Hausdorffification I M) (ih : ∀ x, C (of I M x)) : C x := quotient.induction_on' x ih variables (I M) instance : is_Hausdorff I (Hausdorffification I M) := ⟨λ x, quotient.induction_on' x $ λ x hx, (quotient.mk_eq_zero _).2 $ (mem_infi _).2 $ λ n, begin have := comap_map_mkq (⨅ n : ℕ, I ^ n • ⊤ : submodule R M) (I ^ n • ⊤), simp only [sup_of_le_right (infi_le (λ n, (I ^ n • ⊤ : submodule R M)) n)] at this, rw [← this, map_smul'', mem_comap, map_top, range_mkq, ← smodeq.zero], exact hx n end⟩ variables {M} [h : is_Hausdorff I N] include h /-- universal property of Hausdorffification: any linear map to a Hausdorff module extends to a unique map from the Hausdorffification. -/ def lift (f : M →ₗ[R] N) : Hausdorffification I M →ₗ[R] N := liftq _ f $ map_le_iff_le_comap.1 $ h.infi_pow_smul ▸ le_infi (λ n, le_trans (map_mono $ infi_le _ n) $ by { rw map_smul'', exact smul_mono (le_refl _) le_top }) theorem lift_of (f : M →ₗ[R] N) (x : M) : lift I f (of I M x) = f x := rfl theorem lift_comp_of (f : M →ₗ[R] N) : (lift I f).comp (of I M) = f := linear_map.ext $ λ _, rfl /-- Uniqueness of lift. -/ theorem lift_eq (f : M →ₗ[R] N) (g : Hausdorffification I M →ₗ[R] N) (hg : g.comp (of I M) = f) : g = lift I f := linear_map.ext $ λ x, induction_on x $ λ x, by rw [lift_of, ← hg, linear_map.comp_apply] end Hausdorffification namespace is_precomplete instance bot : is_precomplete (⊥ : ideal R) M := begin refine ⟨λ f hf, ⟨f 1, λ n, _⟩⟩, cases n, { rw [pow_zero, ideal.one_eq_top, top_smul], exact smodeq.top }, specialize hf (nat.le_add_left 1 n), rw [pow_one, bot_smul, smodeq.bot] at hf, rw hf end instance top : is_precomplete (⊤ : ideal R) M := ⟨λ f hf, ⟨0, λ n, by { rw [ideal.top_pow, top_smul], exact smodeq.top }⟩⟩ @[priority 100] instance of_subsingleton [subsingleton M] : is_precomplete I M := ⟨λ f hf, ⟨0, λ n, by rw subsingleton.elim (f n) 0⟩⟩ end is_precomplete namespace adic_completion /-- The canonical linear map to the completion. -/ def of : M →ₗ[R] adic_completion I M := { to_fun := λ x, ⟨λ n, mkq _ x, λ m n hmn, rfl⟩, map_add' := λ x y, rfl, map_smul' := λ c x, rfl } @[simp] lemma of_apply (x : M) (n : ℕ) : (of I M x).1 n = mkq _ x := rfl /-- Linearly evaluating a sequence in the completion at a given input. -/ def eval (n : ℕ) : adic_completion I M →ₗ[R] (I ^ n • ⊤ : submodule R M).quotient := { to_fun := λ f, f.1 n, map_add' := λ f g, rfl, map_smul' := λ c f, rfl } @[simp] lemma coe_eval (n : ℕ) : (eval I M n : adic_completion I M → (I ^ n • ⊤ : submodule R M).quotient) = λ f, f.1 n := rfl lemma eval_apply (n : ℕ) (f : adic_completion I M) : eval I M n f = f.1 n := rfl lemma eval_of (n : ℕ) (x : M) : eval I M n (of I M x) = mkq _ x := rfl @[simp] lemma eval_comp_of (n : ℕ) : (eval I M n).comp (of I M) = mkq _ := rfl @[simp] lemma range_eval (n : ℕ) : (eval I M n).range = ⊤ := linear_map.range_eq_top.2 $ λ x, quotient.induction_on' x $ λ x, ⟨of I M x, rfl⟩ variables {I M} @[ext] lemma ext {x y : adic_completion I M} (h : ∀ n, eval I M n x = eval I M n y) : x = y := subtype.eq $ funext h variables (I M) instance : is_Hausdorff I (adic_completion I M) := ⟨λ x hx, ext $ λ n, smul_induction_on (smodeq.zero.1 $ hx n) (λ r hr x _, ((eval I M n).map_smul r x).symm ▸ quotient.induction_on' (eval I M n x) (λ x, smodeq.zero.2 $ smul_mem_smul hr mem_top)) rfl (λ _ _ ih1 ih2, by rw [linear_map.map_add, ih1, ih2, linear_map.map_zero, add_zero]) (λ c _ ih, by rw [linear_map.map_smul, ih, linear_map.map_zero, smul_zero])⟩ end adic_completion namespace is_adic_complete instance bot : is_adic_complete (⊥ : ideal R) M := {} protected theorem subsingleton (h : is_adic_complete (⊤ : ideal R) M) : subsingleton M := h.1.subsingleton @[priority 100] instance of_subsingleton [subsingleton M] : is_adic_complete I M := {} end is_adic_complete
34ea21859b18abc16df027c7062b663a58b5c2f4
5756a081670ba9c1d1d3fca7bd47cb4e31beae66
/Mathport/Binary/Number.lean
bb8ecae8200ee8b45e51292737f8f2f81077a0bf
[ "Apache-2.0" ]
permissive
leanprover-community/mathport
2c9bdc8292168febf59799efdc5451dbf0450d4a
13051f68064f7638970d39a8fecaede68ffbf9e1
refs/heads/master
1,693,841,364,079
1,693,813,111,000
1,693,813,111,000
379,357,010
27
10
Apache-2.0
1,691,309,132,000
1,624,384,521,000
Lean
UTF-8
Lean
false
false
2,375
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Daniel Selsam These functions mimic the ones in lean3/src/library/num.cpp -/ import Lean import Mathport.Binary.Basic import Mathlib.Init.ZeroOne namespace Mathport.Binary open Lean partial def isConcreteNat? (e : Expr) : Option Nat := do if e.isConstOf ``Nat.zero then some 0 else if e.isAppOfArity ``Nat.succ 1 then let n ← isConcreteNat? e.appArg! some (n+1) else none structure NumInfo where number : Nat level : Level type : Expr hasZero? : Option Expr := none hasOne? : Option Expr := none hasAdd? : Option Expr := none deriving Inhabited partial def isNumber? (e : Expr) : Option NumInfo := do if e.isAppOfArity ``Zero.zero 2 then pure { number := 0, level := e.getAppFn.constLevels!.head!, type := e.getArg! 0 hasZero? := e.getArg! 1 } else if e.isAppOfArity ``One.one 2 then pure { number := 1, level := e.getAppFn.constLevels!.head!, type := e.getArg! 0, hasOne? := e.getArg! 1 } else if e.isAppOfArity ``bit0 3 then -- TODO: may need to check if these instances are def-eq -- (I am hoping that mathlib does not produce terms in which they are not) let info ← isNumber? $ e.getArg! 2 pure { info with number := info.number * 2, hasAdd? := info.hasAdd? <|> e.getArg! 1 } else if e.isAppOfArity ``bit1 4 then let info ← isNumber? $ e.getArg! 3 pure { info with number := info.number * 2 + 1, hasAdd? := info.hasAdd? <|> e.getArg! 2 } else none def translateNumbers (e : Expr) : MetaM Expr := Meta.transform e (pre := core) where core e : MetaM TransformStep := pure <| match isConcreteNat? e with | some n => TransformStep.done $ mkNatLit n | none => match isNumber? e with | none => .continue | some { number := n, level, type, .. } => -- TODO: we will want to avoid wrapping "normal" Nat numbers -- (current workaround is for the OfNat instances to `no_index` the numbers) let inst := mkAppN (mkConst ``OfNat.mk [level]) #[type, mkRawNatLit n, e] TransformStep.done $ mkAppN (mkConst ``OfNat.ofNat [level]) #[type, mkRawNatLit n, inst] end Mathport.Binary
8e644f19f6b403d081452a80e8b7f9280607c001
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/geometry/manifold/bump_function.lean
425e72abc444ad7185ff6e772c40e1e8ecaa3152
[ "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
14,165
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.calculus.specific_functions import geometry.manifold.cont_mdiff /-! # Smooth bump functions on a smooth manifold In this file we define `smooth_bump_function I c` to be a bundled smooth "bump" function centered at `c`. It is a structure that consists of two real numbers `0 < r < R` with small enough `R`. We define a coercion to function for this type, and for `f : smooth_bump_function I c`, the function `⇑f` written in the extended chart at `c` has the following properties: * `f x = 1` in the closed euclidean ball of radius `f.r` centered at `c`; * `f x = 0` outside of the euclidean ball of radius `f.R` centered at `c`; * `0 ≤ f x ≤ 1` for all `x`. The actual statements involve (pre)images under `ext_chart_at I f` and are given as lemmas in the `smooth_bump_function` namespace. ## Tags manifold, smooth bump function -/ universes uE uF uH uM variables {E : Type uE} [normed_add_comm_group E] [normed_space ℝ E] [finite_dimensional ℝ E] {H : Type uH} [topological_space H] (I : model_with_corners ℝ E H) {M : Type uM} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] open function filter finite_dimensional set open_locale topological_space manifold classical filter big_operators noncomputable theory /-! ### Smooth bump function In this section we define a structure for a bundled smooth bump function and prove its properties. -/ /-- Given a smooth manifold modelled on a finite dimensional space `E`, `f : smooth_bump_function I M` is a smooth function on `M` such that in the extended chart `e` at `f.c`: * `f x = 1` in the closed euclidean ball of radius `f.r` centered at `f.c`; * `f x = 0` outside of the euclidean ball of radius `f.R` centered at `f.c`; * `0 ≤ f x ≤ 1` for all `x`. The structure contains data required to construct a function with these properties. The function is available as `⇑f` or `f x`. Formal statements of the properties listed above involve some (pre)images under `ext_chart_at I f.c` and are given as lemmas in the `smooth_bump_function` namespace. -/ structure smooth_bump_function (c : M) extends cont_diff_bump (ext_chart_at I c c) := (closed_ball_subset : (euclidean.closed_ball (ext_chart_at I c c) R) ∩ range I ⊆ (ext_chart_at I c).target) variable {M} namespace smooth_bump_function open euclidean (renaming dist -> eudist) variables {c : M} (f : smooth_bump_function I c) {x : M} {I} /-- The function defined by `f : smooth_bump_function c`. Use automatic coercion to function instead. -/ def to_fun : M → ℝ := indicator (chart_at H c).source (f.to_cont_diff_bump ∘ ext_chart_at I c) instance : has_coe_to_fun (smooth_bump_function I c) (λ _, M → ℝ) := ⟨to_fun⟩ lemma coe_def : ⇑f = indicator (chart_at H c).source (f.to_cont_diff_bump ∘ ext_chart_at I c) := rfl lemma R_pos : 0 < f.R := f.to_cont_diff_bump.R_pos lemma ball_subset : ball (ext_chart_at I c c) f.R ∩ range I ⊆ (ext_chart_at I c).target := subset.trans (inter_subset_inter_left _ ball_subset_closed_ball) f.closed_ball_subset lemma eq_on_source : eq_on f (f.to_cont_diff_bump ∘ ext_chart_at I c) (chart_at H c).source := eq_on_indicator lemma eventually_eq_of_mem_source (hx : x ∈ (chart_at H c).source) : f =ᶠ[𝓝 x] f.to_cont_diff_bump ∘ ext_chart_at I c := f.eq_on_source.eventually_eq_of_mem $ is_open.mem_nhds (chart_at H c).open_source hx lemma one_of_dist_le (hs : x ∈ (chart_at H c).source) (hd : eudist (ext_chart_at I c x) (ext_chart_at I c c) ≤ f.r) : f x = 1 := by simp only [f.eq_on_source hs, (∘), f.to_cont_diff_bump.one_of_mem_closed_ball hd] lemma support_eq_inter_preimage : support f = (chart_at H c).source ∩ (ext_chart_at I c ⁻¹' ball (ext_chart_at I c c) f.R) := by rw [coe_def, support_indicator, (∘), support_comp_eq_preimage, ← ext_chart_at_source I, ← (ext_chart_at I c).symm_image_target_inter_eq', ← (ext_chart_at I c).symm_image_target_inter_eq', f.to_cont_diff_bump.support_eq] lemma is_open_support : is_open (support f) := by { rw support_eq_inter_preimage, exact is_open_ext_chart_at_preimage I c is_open_ball } lemma support_eq_symm_image : support f = (ext_chart_at I c).symm '' (ball (ext_chart_at I c c) f.R ∩ range I) := begin rw [f.support_eq_inter_preimage, ← ext_chart_at_source I, ← (ext_chart_at I c).symm_image_target_inter_eq', inter_comm], congr' 1 with y, exact and.congr_right_iff.2 (λ hy, ⟨λ h, ext_chart_at_target_subset_range _ _ h, λ h, f.ball_subset ⟨hy, h⟩⟩) end lemma support_subset_source : support f ⊆ (chart_at H c).source := by { rw [f.support_eq_inter_preimage, ← ext_chart_at_source I], exact inter_subset_left _ _ } lemma image_eq_inter_preimage_of_subset_support {s : set M} (hs : s ⊆ support f) : ext_chart_at I c '' s = closed_ball (ext_chart_at I c c) f.R ∩ range I ∩ (ext_chart_at I c).symm ⁻¹' s := begin rw [support_eq_inter_preimage, subset_inter_iff, ← ext_chart_at_source I, ← image_subset_iff] at hs, cases hs with hse hsf, apply subset.antisymm, { refine subset_inter (subset_inter (subset.trans hsf ball_subset_closed_ball) _) _, { rintro _ ⟨x, -, rfl⟩, exact mem_range_self _ }, { rw [(ext_chart_at I c).image_eq_target_inter_inv_preimage hse], exact inter_subset_right _ _ } }, { refine subset.trans (inter_subset_inter_left _ f.closed_ball_subset) _, rw [(ext_chart_at I c).image_eq_target_inter_inv_preimage hse] } end lemma mem_Icc : f x ∈ Icc (0 : ℝ) 1 := begin have : f x = 0 ∨ f x = _, from indicator_eq_zero_or_self _ _ _, cases this; rw this, exacts [left_mem_Icc.2 zero_le_one, ⟨f.to_cont_diff_bump.nonneg, f.to_cont_diff_bump.le_one⟩] end lemma nonneg : 0 ≤ f x := f.mem_Icc.1 lemma le_one : f x ≤ 1 := f.mem_Icc.2 lemma eventually_eq_one_of_dist_lt (hs : x ∈ (chart_at H c).source) (hd : eudist (ext_chart_at I c x) (ext_chart_at I c c) < f.r) : f =ᶠ[𝓝 x] 1 := begin filter_upwards [is_open.mem_nhds (is_open_ext_chart_at_preimage I c is_open_ball) ⟨hs, hd⟩], rintro z ⟨hzs, hzd : _ < _⟩, exact f.one_of_dist_le hzs hzd.le end lemma eventually_eq_one : f =ᶠ[𝓝 c] 1 := f.eventually_eq_one_of_dist_lt (mem_chart_source _ _) $ by { rw [euclidean.dist, dist_self], exact f.r_pos } @[simp] lemma eq_one : f c = 1 := f.eventually_eq_one.eq_of_nhds lemma support_mem_nhds : support f ∈ 𝓝 c := f.eventually_eq_one.mono $ λ x hx, by { rw hx, exact one_ne_zero } lemma tsupport_mem_nhds : tsupport f ∈ 𝓝 c := mem_of_superset f.support_mem_nhds subset_closure lemma c_mem_support : c ∈ support f := mem_of_mem_nhds f.support_mem_nhds lemma nonempty_support : (support f).nonempty := ⟨c, f.c_mem_support⟩ lemma compact_symm_image_closed_ball : is_compact ((ext_chart_at I c).symm '' (closed_ball (ext_chart_at I c c) f.R ∩ range I)) := (euclidean.is_compact_closed_ball.inter_right I.closed_range).image_of_continuous_on $ (continuous_on_ext_chart_at_symm _ _).mono f.closed_ball_subset /-- Given a smooth bump function `f : smooth_bump_function I c`, the closed ball of radius `f.R` is known to include the support of `f`. These closed balls (in the model normed space `E`) intersected with `set.range I` form a basis of `𝓝[range I] (ext_chart_at I c c)`. -/ lemma nhds_within_range_basis : (𝓝[range I] (ext_chart_at I c c)).has_basis (λ f : smooth_bump_function I c, true) (λ f, closed_ball (ext_chart_at I c c) f.R ∩ range I) := begin refine ((nhds_within_has_basis euclidean.nhds_basis_closed_ball _).restrict_subset (ext_chart_at_target_mem_nhds_within _ _)).to_has_basis' _ _, { rintro R ⟨hR0, hsub⟩, exact ⟨⟨⟨⟨R / 2, R, half_pos hR0, half_lt_self hR0⟩⟩, hsub⟩, trivial, subset.rfl⟩ }, { exact λ f _, inter_mem (mem_nhds_within_of_mem_nhds $ closed_ball_mem_nhds f.R_pos) self_mem_nhds_within } end lemma is_closed_image_of_is_closed {s : set M} (hsc : is_closed s) (hs : s ⊆ support f) : is_closed (ext_chart_at I c '' s) := begin rw f.image_eq_inter_preimage_of_subset_support hs, refine continuous_on.preimage_closed_of_closed ((continuous_on_ext_chart_at_symm _ _).mono f.closed_ball_subset) _ hsc, exact is_closed.inter is_closed_closed_ball I.closed_range end /-- If `f` is a smooth bump function and `s` closed subset of the support of `f` (i.e., of the open ball of radius `f.R`), then there exists `0 < r < f.R` such that `s` is a subset of the open ball of radius `r`. Formally, `s ⊆ e.source ∩ e ⁻¹' (ball (e c) r)`, where `e = ext_chart_at I c`. -/ lemma exists_r_pos_lt_subset_ball {s : set M} (hsc : is_closed s) (hs : s ⊆ support f) : ∃ r (hr : r ∈ Ioo 0 f.R), s ⊆ (chart_at H c).source ∩ ext_chart_at I c ⁻¹' (ball (ext_chart_at I c c) r) := begin set e := ext_chart_at I c, have : is_closed (e '' s) := f.is_closed_image_of_is_closed hsc hs, rw [support_eq_inter_preimage, subset_inter_iff, ← image_subset_iff] at hs, rcases euclidean.exists_pos_lt_subset_ball f.R_pos this hs.2 with ⟨r, hrR, hr⟩, exact ⟨r, hrR, subset_inter hs.1 (image_subset_iff.1 hr)⟩ end /-- Replace `r` with another value in the interval `(0, f.R)`. -/ def update_r (r : ℝ) (hr : r ∈ Ioo 0 f.R) : smooth_bump_function I c := ⟨⟨⟨r, f.R, hr.1, hr.2⟩⟩, f.closed_ball_subset⟩ @[simp] lemma update_r_R {r : ℝ} (hr : r ∈ Ioo 0 f.R) : (f.update_r r hr).R = f.R := rfl @[simp] lemma update_r_r {r : ℝ} (hr : r ∈ Ioo 0 f.R) : (f.update_r r hr).r = r := rfl @[simp] lemma support_update_r {r : ℝ} (hr : r ∈ Ioo 0 f.R) : support (f.update_r r hr) = support f := by simp only [support_eq_inter_preimage, update_r_R] instance : inhabited (smooth_bump_function I c) := classical.inhabited_of_nonempty nhds_within_range_basis.nonempty variables [t2_space M] lemma is_closed_symm_image_closed_ball : is_closed ((ext_chart_at I c).symm '' (closed_ball (ext_chart_at I c c) f.R ∩ range I)) := f.compact_symm_image_closed_ball.is_closed lemma tsupport_subset_symm_image_closed_ball : tsupport f ⊆ (ext_chart_at I c).symm '' (closed_ball (ext_chart_at I c c) f.R ∩ range I) := begin rw [tsupport, support_eq_symm_image], exact closure_minimal (image_subset _ $ inter_subset_inter_left _ ball_subset_closed_ball) f.is_closed_symm_image_closed_ball end lemma tsupport_subset_ext_chart_at_source : tsupport f ⊆ (ext_chart_at I c).source := calc tsupport f ⊆ (ext_chart_at I c).symm '' (closed_ball (ext_chart_at I c c) f.R ∩ range I) : f.tsupport_subset_symm_image_closed_ball ... ⊆ (ext_chart_at I c).symm '' (ext_chart_at I c).target : image_subset _ f.closed_ball_subset ... = (ext_chart_at I c).source : (ext_chart_at I c).symm_image_target_eq_source lemma tsupport_subset_chart_at_source : tsupport f ⊆ (chart_at H c).source := by simpa only [ext_chart_at_source] using f.tsupport_subset_ext_chart_at_source protected lemma has_compact_support : has_compact_support f := is_compact_of_is_closed_subset f.compact_symm_image_closed_ball is_closed_closure f.tsupport_subset_symm_image_closed_ball variables (I c) /-- The closures of supports of smooth bump functions centered at `c` form a basis of `𝓝 c`. In other words, each of these closures is a neighborhood of `c` and each neighborhood of `c` includes `tsupport f` for some `f : smooth_bump_function I c`. -/ lemma nhds_basis_tsupport : (𝓝 c).has_basis (λ f : smooth_bump_function I c, true) (λ f, tsupport f) := begin have : (𝓝 c).has_basis (λ f : smooth_bump_function I c, true) (λ f, (ext_chart_at I c).symm '' (closed_ball (ext_chart_at I c c) f.R ∩ range I)), { rw [← map_ext_chart_at_symm_nhds_within_range I c], exact nhds_within_range_basis.map _ }, refine this.to_has_basis' (λ f hf, ⟨f, trivial, f.tsupport_subset_symm_image_closed_ball⟩) (λ f _, f.tsupport_mem_nhds), end variable {c} /-- Given `s ∈ 𝓝 c`, the supports of smooth bump functions `f : smooth_bump_function I c` such that `tsupport f ⊆ s` form a basis of `𝓝 c`. In other words, each of these supports is a neighborhood of `c` and each neighborhood of `c` includes `support f` for some `f : smooth_bump_function I c` such that `tsupport f ⊆ s`. -/ lemma nhds_basis_support {s : set M} (hs : s ∈ 𝓝 c) : (𝓝 c).has_basis (λ f : smooth_bump_function I c, tsupport f ⊆ s) (λ f, support f) := ((nhds_basis_tsupport I c).restrict_subset hs).to_has_basis' (λ f hf, ⟨f, hf.2, subset_closure⟩) (λ f hf, f.support_mem_nhds) variables [smooth_manifold_with_corners I M] {I} /-- A smooth bump function is infinitely smooth. -/ protected lemma smooth : smooth I 𝓘(ℝ) f := begin refine cont_mdiff_of_support (λ x hx, _), have : x ∈ (chart_at H c).source := f.tsupport_subset_chart_at_source hx, refine cont_mdiff_at.congr_of_eventually_eq _ (f.eq_on_source.eventually_eq_of_mem $ is_open.mem_nhds (chart_at _ _).open_source this), exact f.to_cont_diff_bump.cont_diff_at.cont_mdiff_at.comp _ (cont_mdiff_at_ext_chart_at' this) end protected lemma smooth_at {x} : smooth_at I 𝓘(ℝ) f x := f.smooth.smooth_at protected lemma continuous : continuous f := f.smooth.continuous /-- If `f : smooth_bump_function I c` is a smooth bump function and `g : M → G` is a function smooth on the source of the chart at `c`, then `f • g` is smooth on the whole manifold. -/ lemma smooth_smul {G} [normed_add_comm_group G] [normed_space ℝ G] {g : M → G} (hg : smooth_on I 𝓘(ℝ, G) g (chart_at H c).source) : smooth I 𝓘(ℝ, G) (λ x, f x • g x) := begin apply cont_mdiff_of_support (λ x hx, _), have : x ∈ (chart_at H c).source, calc x ∈ tsupport (λ x, f x • g x) : hx ... ⊆ tsupport f : tsupport_smul_subset_left _ _ ... ⊆ (chart_at _ c).source : f.tsupport_subset_chart_at_source, exact f.smooth_at.smul ((hg _ this).cont_mdiff_at $ is_open.mem_nhds (chart_at _ _).open_source this) end end smooth_bump_function
1e5dbc8fd117fa11c62894d696922dac8b503442
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/asymptotics_auto.lean
65ad010d2064187762332dc8f197c1cd96aa74db
[]
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
86,270
lean
/- Copyright (c) 2019 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.normed_space.basic import Mathlib.topology.local_homeomorph import Mathlib.PostPort universes u_1 u_3 u_4 u_7 u_6 u_2 u_5 u_8 u_11 u_9 u_13 namespace Mathlib /-! # Asymptotics We introduce these relations: * `is_O_with c f g l` : "f is big O of g along l with constant c"; * `is_O f g l` : "f is big O of g along l"; * `is_o f g l` : "f is little o of g along l". Here `l` is any filter on the domain of `f` and `g`, which are assumed to be the same. The codomains of `f` and `g` do not need to be the same; all that is needed that there is a norm associated with these types, and it is the norm that is compared asymptotically. The relation `is_O_with c` is introduced to factor out common algebraic arguments in the proofs of similar properties of `is_O` and `is_o`. Usually proofs outside of this file should use `is_O` instead. Often the ranges of `f` and `g` will be the real numbers, in which case the norm is the absolute value. In general, we have `is_O f g l ↔ is_O (λ x, ∥f x∥) (λ x, ∥g x∥) l`, and similarly for `is_o`. But our setup allows us to use the notions e.g. with functions to the integers, rationals, complex numbers, or any normed vector space without mentioning the norm explicitly. If `f` and `g` are functions to a normed field like the reals or complex numbers and `g` is always nonzero, we have `is_o f g l ↔ tendsto (λ x, f x / (g x)) l (𝓝 0)`. In fact, the right-to-left direction holds without the hypothesis on `g`, and in the other direction it suffices to assume that `f` is zero wherever `g` is. (This generalization is useful in defining the Fréchet derivative.) -/ namespace asymptotics /-! ### Definitions -/ /-- This version of the Landau notation `is_O_with C f g l` where `f` and `g` are two functions on a type `α` and `l` is a filter on `α`, means that eventually for `l`, `∥f∥` is bounded by `C * ∥g∥`. In other words, `∥f∥ / ∥g∥` is eventually bounded by `C`, modulo division by zero issues that are avoided by this definition. Probably you want to use `is_O` instead of this relation. -/ def is_O_with {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] (c : ℝ) (f : α → E) (g : α → F) (l : filter α) := filter.eventually (fun (x : α) => norm (f x) ≤ c * norm (g x)) l /-- Definition of `is_O_with`. We record it in a lemma as we will set `is_O_with` to be irreducible at the end of this file. -/ theorem is_O_with_iff {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c : ℝ} {f : α → E} {g : α → F} {l : filter α} : is_O_with c f g l ↔ filter.eventually (fun (x : α) => norm (f x) ≤ c * norm (g x)) l := iff.rfl theorem is_O_with.of_bound {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c : ℝ} {f : α → E} {g : α → F} {l : filter α} (h : filter.eventually (fun (x : α) => norm (f x) ≤ c * norm (g x)) l) : is_O_with c f g l := h /-- The Landau notation `is_O f g l` where `f` and `g` are two functions on a type `α` and `l` is a filter on `α`, means that eventually for `l`, `∥f∥` is bounded by a constant multiple of `∥g∥`. In other words, `∥f∥ / ∥g∥` is eventually bounded, modulo division by zero issues that are avoided by this definition. -/ def is_O {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] (f : α → E) (g : α → F) (l : filter α) := ∃ (c : ℝ), is_O_with c f g l /-- Definition of `is_O` in terms of `is_O_with`. We record it in a lemma as we will set `is_O` to be irreducible at the end of this file. -/ theorem is_O_iff_is_O_with {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {l : filter α} : is_O f g l ↔ ∃ (c : ℝ), is_O_with c f g l := iff.rfl /-- Definition of `is_O` in terms of filters. We record it in a lemma as we will set `is_O` to be irreducible at the end of this file. -/ theorem is_O_iff {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {l : filter α} : is_O f g l ↔ ∃ (c : ℝ), filter.eventually (fun (x : α) => norm (f x) ≤ c * norm (g x)) l := iff.rfl theorem is_O.of_bound {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] (c : ℝ) {f : α → E} {g : α → F} {l : filter α} (h : filter.eventually (fun (x : α) => norm (f x) ≤ c * norm (g x)) l) : is_O f g l := Exists.intro c h /-- The Landau notation `is_o f g l` where `f` and `g` are two functions on a type `α` and `l` is a filter on `α`, means that eventually for `l`, `∥f∥` is bounded by an arbitrarily small constant multiple of `∥g∥`. In other words, `∥f∥ / ∥g∥` tends to `0` along `l`, modulo division by zero issues that are avoided by this definition. -/ def is_o {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] (f : α → E) (g : α → F) (l : filter α) := ∀ {c : ℝ}, 0 < c → is_O_with c f g l /-- Definition of `is_o` in terms of `is_O_with`. We record it in a lemma as we will set `is_o` to be irreducible at the end of this file. -/ theorem is_o_iff_forall_is_O_with {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {l : filter α} : is_o f g l ↔ ∀ {c : ℝ}, 0 < c → is_O_with c f g l := iff.rfl /-- Definition of `is_o` in terms of filters. We record it in a lemma as we will set `is_o` to be irreducible at the end of this file. -/ theorem is_o_iff {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {l : filter α} : is_o f g l ↔ ∀ {c : ℝ}, 0 < c → filter.eventually (fun (x : α) => norm (f x) ≤ c * norm (g x)) l := iff.rfl theorem is_o.def {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {l : filter α} (h : is_o f g l) {c : ℝ} (hc : 0 < c) : filter.eventually (fun (x : α) => norm (f x) ≤ c * norm (g x)) l := h hc theorem is_o.def' {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {l : filter α} (h : is_o f g l) {c : ℝ} (hc : 0 < c) : is_O_with c f g l := h hc /-! ### Conversions -/ theorem is_O_with.is_O {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c : ℝ} {f : α → E} {g : α → F} {l : filter α} (h : is_O_with c f g l) : is_O f g l := Exists.intro c h theorem is_o.is_O_with {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {l : filter α} (hgf : is_o f g l) : is_O_with 1 f g l := hgf zero_lt_one theorem is_o.is_O {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {l : filter α} (hgf : is_o f g l) : is_O f g l := is_O_with.is_O (is_o.is_O_with hgf) theorem is_O_with.weaken {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {c : ℝ} {c' : ℝ} {f : α → E} {g' : α → F'} {l : filter α} (h : is_O_with c f g' l) (hc : c ≤ c') : is_O_with c' f g' l := filter.mem_sets_of_superset h fun (x : α) (hx : x ∈ set_of fun (x : α) => (fun (x : α) => norm (f x) ≤ c * norm (g' x)) x) => le_trans hx (mul_le_mul_of_nonneg_right hc (norm_nonneg (g' x))) theorem is_O_with.exists_pos {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {c : ℝ} {f : α → E} {g' : α → F'} {l : filter α} (h : is_O_with c f g' l) : ∃ (c' : ℝ), ∃ (H : 0 < c'), is_O_with c' f g' l := Exists.intro (max c 1) (Exists.intro (lt_of_lt_of_le zero_lt_one (le_max_right c 1)) (is_O_with.weaken h (le_max_left c 1))) theorem is_O.exists_pos {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {f : α → E} {g' : α → F'} {l : filter α} (h : is_O f g' l) : ∃ (c : ℝ), ∃ (H : 0 < c), is_O_with c f g' l := sorry theorem is_O_with.exists_nonneg {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {c : ℝ} {f : α → E} {g' : α → F'} {l : filter α} (h : is_O_with c f g' l) : ∃ (c' : ℝ), ∃ (H : 0 ≤ c'), is_O_with c' f g' l := sorry theorem is_O.exists_nonneg {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {f : α → E} {g' : α → F'} {l : filter α} (h : is_O f g' l) : ∃ (c : ℝ), ∃ (H : 0 ≤ c), is_O_with c f g' l := sorry /-! ### Subsingleton -/ theorem is_o_of_subsingleton {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {g' : α → F'} {l : filter α} [subsingleton E'] : is_o f' g' l := sorry theorem is_O_of_subsingleton {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {g' : α → F'} {l : filter α} [subsingleton E'] : is_O f' g' l := is_o.is_O is_o_of_subsingleton /-! ### Congruence -/ theorem is_O_with_congr {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c₁ : ℝ} {c₂ : ℝ} {f₁ : α → E} {f₂ : α → E} {g₁ : α → F} {g₂ : α → F} {l : filter α} (hc : c₁ = c₂) (hf : filter.eventually_eq l f₁ f₂) (hg : filter.eventually_eq l g₁ g₂) : is_O_with c₁ f₁ g₁ l ↔ is_O_with c₂ f₂ g₂ l := sorry theorem is_O_with.congr' {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c₁ : ℝ} {c₂ : ℝ} {f₁ : α → E} {f₂ : α → E} {g₁ : α → F} {g₂ : α → F} {l : filter α} (hc : c₁ = c₂) (hf : filter.eventually_eq l f₁ f₂) (hg : filter.eventually_eq l g₁ g₂) : is_O_with c₁ f₁ g₁ l → is_O_with c₂ f₂ g₂ l := iff.mp (is_O_with_congr hc hf hg) theorem is_O_with.congr {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c₁ : ℝ} {c₂ : ℝ} {f₁ : α → E} {f₂ : α → E} {g₁ : α → F} {g₂ : α → F} {l : filter α} (hc : c₁ = c₂) (hf : ∀ (x : α), f₁ x = f₂ x) (hg : ∀ (x : α), g₁ x = g₂ x) : is_O_with c₁ f₁ g₁ l → is_O_with c₂ f₂ g₂ l := fun (h : is_O_with c₁ f₁ g₁ l) => is_O_with.congr' hc (filter.univ_mem_sets' hf) (filter.univ_mem_sets' hg) h theorem is_O_with.congr_left {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c : ℝ} {g : α → F} {f₁ : α → E} {f₂ : α → E} {l : filter α} (hf : ∀ (x : α), f₁ x = f₂ x) : is_O_with c f₁ g l → is_O_with c f₂ g l := is_O_with.congr rfl hf fun (_x : α) => rfl theorem is_O_with.congr_right {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c : ℝ} {f : α → E} {g₁ : α → F} {g₂ : α → F} {l : filter α} (hg : ∀ (x : α), g₁ x = g₂ x) : is_O_with c f g₁ l → is_O_with c f g₂ l := is_O_with.congr rfl (fun (_x : α) => rfl) hg theorem is_O_with.congr_const {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {c₁ : ℝ} {c₂ : ℝ} {l : filter α} (hc : c₁ = c₂) : is_O_with c₁ f g l → is_O_with c₂ f g l := is_O_with.congr hc (fun (_x : α) => rfl) fun (_x : α) => rfl theorem is_O_congr {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f₁ : α → E} {f₂ : α → E} {g₁ : α → F} {g₂ : α → F} {l : filter α} (hf : filter.eventually_eq l f₁ f₂) (hg : filter.eventually_eq l g₁ g₂) : is_O f₁ g₁ l ↔ is_O f₂ g₂ l := exists_congr fun (c : ℝ) => is_O_with_congr rfl hf hg theorem is_O.congr' {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f₁ : α → E} {f₂ : α → E} {g₁ : α → F} {g₂ : α → F} {l : filter α} (hf : filter.eventually_eq l f₁ f₂) (hg : filter.eventually_eq l g₁ g₂) : is_O f₁ g₁ l → is_O f₂ g₂ l := iff.mp (is_O_congr hf hg) theorem is_O.congr {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f₁ : α → E} {f₂ : α → E} {g₁ : α → F} {g₂ : α → F} {l : filter α} (hf : ∀ (x : α), f₁ x = f₂ x) (hg : ∀ (x : α), g₁ x = g₂ x) : is_O f₁ g₁ l → is_O f₂ g₂ l := fun (h : is_O f₁ g₁ l) => is_O.congr' (filter.univ_mem_sets' hf) (filter.univ_mem_sets' hg) h theorem is_O.congr_left {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {g : α → F} {f₁ : α → E} {f₂ : α → E} {l : filter α} (hf : ∀ (x : α), f₁ x = f₂ x) : is_O f₁ g l → is_O f₂ g l := is_O.congr hf fun (_x : α) => rfl theorem is_O.congr_right {α : Type u_1} {E : Type u_3} [has_norm E] {f : α → E} {g₁ : α → E} {g₂ : α → E} {l : filter α} (hg : ∀ (x : α), g₁ x = g₂ x) : is_O f g₁ l → is_O f g₂ l := is_O.congr (fun (_x : α) => rfl) hg theorem is_o_congr {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f₁ : α → E} {f₂ : α → E} {g₁ : α → F} {g₂ : α → F} {l : filter α} (hf : filter.eventually_eq l f₁ f₂) (hg : filter.eventually_eq l g₁ g₂) : is_o f₁ g₁ l ↔ is_o f₂ g₂ l := ball_congr fun (c : ℝ) (hc : 0 < c) => is_O_with_congr (Eq.refl c) hf hg theorem is_o.congr' {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f₁ : α → E} {f₂ : α → E} {g₁ : α → F} {g₂ : α → F} {l : filter α} (hf : filter.eventually_eq l f₁ f₂) (hg : filter.eventually_eq l g₁ g₂) : is_o f₁ g₁ l → is_o f₂ g₂ l := iff.mp (is_o_congr hf hg) theorem is_o.congr {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f₁ : α → E} {f₂ : α → E} {g₁ : α → F} {g₂ : α → F} {l : filter α} (hf : ∀ (x : α), f₁ x = f₂ x) (hg : ∀ (x : α), g₁ x = g₂ x) : is_o f₁ g₁ l → is_o f₂ g₂ l := fun (h : is_o f₁ g₁ l) => is_o.congr' (filter.univ_mem_sets' hf) (filter.univ_mem_sets' hg) h theorem is_o.congr_left {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {g : α → F} {f₁ : α → E} {f₂ : α → E} {l : filter α} (hf : ∀ (x : α), f₁ x = f₂ x) : is_o f₁ g l → is_o f₂ g l := is_o.congr hf fun (_x : α) => rfl theorem is_o.congr_right {α : Type u_1} {E : Type u_3} [has_norm E] {f : α → E} {g₁ : α → E} {g₂ : α → E} {l : filter α} (hg : ∀ (x : α), g₁ x = g₂ x) : is_o f g₁ l → is_o f g₂ l := is_o.congr (fun (_x : α) => rfl) hg /-! ### Filter operations and transitivity -/ theorem is_O_with.comp_tendsto {α : Type u_1} {β : Type u_2} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c : ℝ} {f : α → E} {g : α → F} {l : filter α} (hcfg : is_O_with c f g l) {k : β → α} {l' : filter β} (hk : filter.tendsto k l' l) : is_O_with c (f ∘ k) (g ∘ k) l' := hk hcfg theorem is_O.comp_tendsto {α : Type u_1} {β : Type u_2} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {l : filter α} (hfg : is_O f g l) {k : β → α} {l' : filter β} (hk : filter.tendsto k l' l) : is_O (f ∘ k) (g ∘ k) l' := Exists.imp (fun (c : ℝ) (h : is_O_with c f g l) => is_O_with.comp_tendsto h hk) hfg theorem is_o.comp_tendsto {α : Type u_1} {β : Type u_2} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {l : filter α} (hfg : is_o f g l) {k : β → α} {l' : filter β} (hk : filter.tendsto k l' l) : is_o (f ∘ k) (g ∘ k) l' := fun (c : ℝ) (cpos : 0 < c) => is_O_with.comp_tendsto (hfg cpos) hk @[simp] theorem is_O_with_map {α : Type u_1} {β : Type u_2} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c : ℝ} {f : α → E} {g : α → F} {k : β → α} {l : filter β} : is_O_with c f g (filter.map k l) ↔ is_O_with c (f ∘ k) (g ∘ k) l := filter.mem_map @[simp] theorem is_O_map {α : Type u_1} {β : Type u_2} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {k : β → α} {l : filter β} : is_O f g (filter.map k l) ↔ is_O (f ∘ k) (g ∘ k) l := sorry @[simp] theorem is_o_map {α : Type u_1} {β : Type u_2} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {k : β → α} {l : filter β} : is_o f g (filter.map k l) ↔ is_o (f ∘ k) (g ∘ k) l := sorry theorem is_O_with.mono {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c : ℝ} {f : α → E} {g : α → F} {l : filter α} {l' : filter α} (h : is_O_with c f g l') (hl : l ≤ l') : is_O_with c f g l := hl h theorem is_O.mono {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {l : filter α} {l' : filter α} (h : is_O f g l') (hl : l ≤ l') : is_O f g l := Exists.imp (fun (c : ℝ) (h : is_O_with c f g l') => is_O_with.mono h hl) h theorem is_o.mono {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {l : filter α} {l' : filter α} (h : is_o f g l') (hl : l ≤ l') : is_o f g l := fun (c : ℝ) (cpos : 0 < c) => is_O_with.mono (h cpos) hl theorem is_O_with.trans {α : Type u_1} {E : Type u_3} {F : Type u_4} {G : Type u_5} [has_norm E] [has_norm F] [has_norm G] {c : ℝ} {c' : ℝ} {f : α → E} {g : α → F} {k : α → G} {l : filter α} (hfg : is_O_with c f g l) (hgk : is_O_with c' g k l) (hc : 0 ≤ c) : is_O_with (c * c') f k l := sorry theorem is_O.trans {α : Type u_1} {E : Type u_3} {G : Type u_5} {F' : Type u_7} [has_norm E] [has_norm G] [normed_group F'] {f : α → E} {k : α → G} {g' : α → F'} {l : filter α} (hfg : is_O f g' l) (hgk : is_O g' k l) : is_O f k l := sorry theorem is_o.trans_is_O_with {α : Type u_1} {E : Type u_3} {F : Type u_4} {G : Type u_5} [has_norm E] [has_norm F] [has_norm G] {c : ℝ} {f : α → E} {g : α → F} {k : α → G} {l : filter α} (hfg : is_o f g l) (hgk : is_O_with c g k l) (hc : 0 < c) : is_o f k l := sorry theorem is_o.trans_is_O {α : Type u_1} {E : Type u_3} {F : Type u_4} {G' : Type u_8} [has_norm E] [has_norm F] [normed_group G'] {f : α → E} {g : α → F} {k' : α → G'} {l : filter α} (hfg : is_o f g l) (hgk : is_O g k' l) : is_o f k' l := sorry theorem is_O_with.trans_is_o {α : Type u_1} {E : Type u_3} {F : Type u_4} {G : Type u_5} [has_norm E] [has_norm F] [has_norm G] {c : ℝ} {f : α → E} {g : α → F} {k : α → G} {l : filter α} (hfg : is_O_with c f g l) (hgk : is_o g k l) (hc : 0 < c) : is_o f k l := sorry theorem is_O.trans_is_o {α : Type u_1} {E : Type u_3} {G : Type u_5} {F' : Type u_7} [has_norm E] [has_norm G] [normed_group F'] {f : α → E} {k : α → G} {g' : α → F'} {l : filter α} (hfg : is_O f g' l) (hgk : is_o g' k l) : is_o f k l := sorry theorem is_o.trans {α : Type u_1} {E : Type u_3} {F : Type u_4} {G' : Type u_8} [has_norm E] [has_norm F] [normed_group G'] {f : α → E} {g : α → F} {k' : α → G'} {l : filter α} (hfg : is_o f g l) (hgk : is_o g k' l) : is_o f k' l := is_o.trans_is_O hfg (is_o.is_O hgk) theorem is_o.trans' {α : Type u_1} {E : Type u_3} {G : Type u_5} {F' : Type u_7} [has_norm E] [has_norm G] [normed_group F'] {f : α → E} {k : α → G} {g' : α → F'} {l : filter α} (hfg : is_o f g' l) (hgk : is_o g' k l) : is_o f k l := is_O.trans_is_o (is_o.is_O hfg) hgk theorem is_O_with_of_le' {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c : ℝ} {f : α → E} {g : α → F} (l : filter α) (hfg : ∀ (x : α), norm (f x) ≤ c * norm (g x)) : is_O_with c f g l := filter.univ_mem_sets' hfg theorem is_O_with_of_le {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} (l : filter α) (hfg : ∀ (x : α), norm (f x) ≤ norm (g x)) : is_O_with 1 f g l := is_O_with_of_le' l fun (x : α) => eq.mpr (id (Eq._oldrec (Eq.refl (norm (f x) ≤ 1 * norm (g x))) (one_mul (norm (g x))))) (hfg x) theorem is_O_of_le' {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c : ℝ} {f : α → E} {g : α → F} (l : filter α) (hfg : ∀ (x : α), norm (f x) ≤ c * norm (g x)) : is_O f g l := is_O_with.is_O (is_O_with_of_le' l hfg) theorem is_O_of_le {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} (l : filter α) (hfg : ∀ (x : α), norm (f x) ≤ norm (g x)) : is_O f g l := is_O_with.is_O (is_O_with_of_le l hfg) theorem is_O_with_refl {α : Type u_1} {E : Type u_3} [has_norm E] (f : α → E) (l : filter α) : is_O_with 1 f f l := is_O_with_of_le l fun (_x : α) => le_refl (norm (f _x)) theorem is_O_refl {α : Type u_1} {E : Type u_3} [has_norm E] (f : α → E) (l : filter α) : is_O f f l := is_O_with.is_O (is_O_with_refl f l) theorem is_O_with.trans_le {α : Type u_1} {E : Type u_3} {F : Type u_4} {G : Type u_5} [has_norm E] [has_norm F] [has_norm G] {c : ℝ} {f : α → E} {g : α → F} {k : α → G} {l : filter α} (hfg : is_O_with c f g l) (hgk : ∀ (x : α), norm (g x) ≤ norm (k x)) (hc : 0 ≤ c) : is_O_with c f k l := is_O_with.congr_const (mul_one c) (is_O_with.trans hfg (is_O_with_of_le l hgk) hc) theorem is_O.trans_le {α : Type u_1} {E : Type u_3} {G : Type u_5} {F' : Type u_7} [has_norm E] [has_norm G] [normed_group F'] {f : α → E} {k : α → G} {g' : α → F'} {l : filter α} (hfg : is_O f g' l) (hgk : ∀ (x : α), norm (g' x) ≤ norm (k x)) : is_O f k l := is_O.trans hfg (is_O_of_le l hgk) theorem is_o.trans_le {α : Type u_1} {E : Type u_3} {F : Type u_4} {G : Type u_5} [has_norm E] [has_norm F] [has_norm G] {f : α → E} {g : α → F} {k : α → G} {l : filter α} (hfg : is_o f g l) (hgk : ∀ (x : α), norm (g x) ≤ norm (k x)) : is_o f k l := is_o.trans_is_O_with hfg (is_O_with_of_le l hgk) zero_lt_one @[simp] theorem is_O_with_bot {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] (c : ℝ) (f : α → E) (g : α → F) : is_O_with c f g ⊥ := trivial @[simp] theorem is_O_bot {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] (f : α → E) (g : α → F) : is_O f g ⊥ := is_O_with.is_O (is_O_with_bot 1 f g) @[simp] theorem is_o_bot {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] (f : α → E) (g : α → F) : is_o f g ⊥ := fun (c : ℝ) (_x : 0 < c) => is_O_with_bot c f g theorem is_O_with.join {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c : ℝ} {f : α → E} {g : α → F} {l : filter α} {l' : filter α} (h : is_O_with c f g l) (h' : is_O_with c f g l') : is_O_with c f g (l ⊔ l') := iff.mpr filter.mem_sup_sets { left := h, right := h' } theorem is_O_with.join' {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {c : ℝ} {c' : ℝ} {f : α → E} {g' : α → F'} {l : filter α} {l' : filter α} (h : is_O_with c f g' l) (h' : is_O_with c' f g' l') : is_O_with (max c c') f g' (l ⊔ l') := iff.mpr filter.mem_sup_sets { left := is_O_with.weaken h (le_max_left c c'), right := is_O_with.weaken h' (le_max_right c c') } theorem is_O.join {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {f : α → E} {g' : α → F'} {l : filter α} {l' : filter α} (h : is_O f g' l) (h' : is_O f g' l') : is_O f g' (l ⊔ l') := sorry theorem is_o.join {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {l : filter α} {l' : filter α} (h : is_o f g l) (h' : is_o f g l') : is_o f g (l ⊔ l') := fun (c : ℝ) (cpos : 0 < c) => is_O_with.join (h cpos) (h' cpos) /-! ### Simplification : norm -/ @[simp] theorem is_O_with_norm_right {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {c : ℝ} {f : α → E} {g' : α → F'} {l : filter α} : is_O_with c f (fun (x : α) => norm (g' x)) l ↔ is_O_with c f g' l := sorry theorem is_O_with.norm_right {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {c : ℝ} {f : α → E} {g' : α → F'} {l : filter α} : is_O_with c f g' l → is_O_with c f (fun (x : α) => norm (g' x)) l := iff.mpr is_O_with_norm_right @[simp] theorem is_O_norm_right {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {f : α → E} {g' : α → F'} {l : filter α} : is_O f (fun (x : α) => norm (g' x)) l ↔ is_O f g' l := exists_congr fun (_x : ℝ) => is_O_with_norm_right theorem is_O.norm_right {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {f : α → E} {g' : α → F'} {l : filter α} : is_O f g' l → is_O f (fun (x : α) => norm (g' x)) l := iff.mpr is_O_norm_right @[simp] theorem is_o_norm_right {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {f : α → E} {g' : α → F'} {l : filter α} : is_o f (fun (x : α) => norm (g' x)) l ↔ is_o f g' l := forall_congr fun (_x : ℝ) => forall_congr fun (_x_1 : 0 < _x) => is_O_with_norm_right theorem is_o.norm_right {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {f : α → E} {g' : α → F'} {l : filter α} : is_o f g' l → is_o f (fun (x : α) => norm (g' x)) l := iff.mpr is_o_norm_right @[simp] theorem is_O_with_norm_left {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {c : ℝ} {g : α → F} {f' : α → E'} {l : filter α} : is_O_with c (fun (x : α) => norm (f' x)) g l ↔ is_O_with c f' g l := sorry theorem is_O_with.of_norm_left {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {c : ℝ} {g : α → F} {f' : α → E'} {l : filter α} : is_O_with c (fun (x : α) => norm (f' x)) g l → is_O_with c f' g l := iff.mp is_O_with_norm_left @[simp] theorem is_O_norm_left {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {f' : α → E'} {l : filter α} : is_O (fun (x : α) => norm (f' x)) g l ↔ is_O f' g l := exists_congr fun (_x : ℝ) => is_O_with_norm_left theorem is_O.of_norm_left {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {f' : α → E'} {l : filter α} : is_O (fun (x : α) => norm (f' x)) g l → is_O f' g l := iff.mp is_O_norm_left @[simp] theorem is_o_norm_left {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {f' : α → E'} {l : filter α} : is_o (fun (x : α) => norm (f' x)) g l ↔ is_o f' g l := forall_congr fun (_x : ℝ) => forall_congr fun (_x_1 : 0 < _x) => is_O_with_norm_left theorem is_o.of_norm_left {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {f' : α → E'} {l : filter α} : is_o (fun (x : α) => norm (f' x)) g l → is_o f' g l := iff.mp is_o_norm_left theorem is_O_with_norm_norm {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {c : ℝ} {f' : α → E'} {g' : α → F'} {l : filter α} : is_O_with c (fun (x : α) => norm (f' x)) (fun (x : α) => norm (g' x)) l ↔ is_O_with c f' g' l := iff.trans is_O_with_norm_left is_O_with_norm_right theorem is_O_with.of_norm_norm {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {c : ℝ} {f' : α → E'} {g' : α → F'} {l : filter α} : is_O_with c (fun (x : α) => norm (f' x)) (fun (x : α) => norm (g' x)) l → is_O_with c f' g' l := iff.mp is_O_with_norm_norm theorem is_O_norm_norm {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {g' : α → F'} {l : filter α} : is_O (fun (x : α) => norm (f' x)) (fun (x : α) => norm (g' x)) l ↔ is_O f' g' l := iff.trans is_O_norm_left is_O_norm_right theorem is_O.of_norm_norm {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {g' : α → F'} {l : filter α} : is_O (fun (x : α) => norm (f' x)) (fun (x : α) => norm (g' x)) l → is_O f' g' l := iff.mp is_O_norm_norm theorem is_o_norm_norm {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {g' : α → F'} {l : filter α} : is_o (fun (x : α) => norm (f' x)) (fun (x : α) => norm (g' x)) l ↔ is_o f' g' l := iff.trans is_o_norm_left is_o_norm_right theorem is_o.norm_norm {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {g' : α → F'} {l : filter α} : is_o f' g' l → is_o (fun (x : α) => norm (f' x)) (fun (x : α) => norm (g' x)) l := iff.mpr is_o_norm_norm /-! ### Simplification: negate -/ @[simp] theorem is_O_with_neg_right {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {c : ℝ} {f : α → E} {g' : α → F'} {l : filter α} : is_O_with c f (fun (x : α) => -g' x) l ↔ is_O_with c f g' l := sorry theorem is_O_with.neg_right {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {c : ℝ} {f : α → E} {g' : α → F'} {l : filter α} : is_O_with c f g' l → is_O_with c f (fun (x : α) => -g' x) l := iff.mpr is_O_with_neg_right @[simp] theorem is_O_neg_right {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {f : α → E} {g' : α → F'} {l : filter α} : is_O f (fun (x : α) => -g' x) l ↔ is_O f g' l := exists_congr fun (_x : ℝ) => is_O_with_neg_right theorem is_O.of_neg_right {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {f : α → E} {g' : α → F'} {l : filter α} : is_O f (fun (x : α) => -g' x) l → is_O f g' l := iff.mp is_O_neg_right @[simp] theorem is_o_neg_right {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {f : α → E} {g' : α → F'} {l : filter α} : is_o f (fun (x : α) => -g' x) l ↔ is_o f g' l := forall_congr fun (_x : ℝ) => forall_congr fun (_x_1 : 0 < _x) => is_O_with_neg_right theorem is_o.of_neg_right {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {f : α → E} {g' : α → F'} {l : filter α} : is_o f (fun (x : α) => -g' x) l → is_o f g' l := iff.mp is_o_neg_right @[simp] theorem is_O_with_neg_left {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {c : ℝ} {g : α → F} {f' : α → E'} {l : filter α} : is_O_with c (fun (x : α) => -f' x) g l ↔ is_O_with c f' g l := sorry theorem is_O_with.neg_left {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {c : ℝ} {g : α → F} {f' : α → E'} {l : filter α} : is_O_with c f' g l → is_O_with c (fun (x : α) => -f' x) g l := iff.mpr is_O_with_neg_left @[simp] theorem is_O_neg_left {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {f' : α → E'} {l : filter α} : is_O (fun (x : α) => -f' x) g l ↔ is_O f' g l := exists_congr fun (_x : ℝ) => is_O_with_neg_left theorem is_O.neg_left {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {f' : α → E'} {l : filter α} : is_O f' g l → is_O (fun (x : α) => -f' x) g l := iff.mpr is_O_neg_left @[simp] theorem is_o_neg_left {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {f' : α → E'} {l : filter α} : is_o (fun (x : α) => -f' x) g l ↔ is_o f' g l := forall_congr fun (_x : ℝ) => forall_congr fun (_x_1 : 0 < _x) => is_O_with_neg_left theorem is_o.neg_left {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {f' : α → E'} {l : filter α} : is_o f' g l → is_o (fun (x : α) => -f' x) g l := iff.mpr is_o_neg_left /-! ### Product of functions (right) -/ theorem is_O_with_fst_prod {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {g' : α → F'} {l : filter α} : is_O_with 1 f' (fun (x : α) => (f' x, g' x)) l := is_O_with_of_le l fun (x : α) => le_max_left (norm (f' x)) (norm (prod.snd (f' x, g' x))) theorem is_O_with_snd_prod {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {g' : α → F'} {l : filter α} : is_O_with 1 g' (fun (x : α) => (f' x, g' x)) l := is_O_with_of_le l fun (x : α) => le_max_right (norm (prod.fst (f' x, g' x))) (norm (g' x)) theorem is_O_fst_prod {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {g' : α → F'} {l : filter α} : is_O f' (fun (x : α) => (f' x, g' x)) l := is_O_with.is_O is_O_with_fst_prod theorem is_O_snd_prod {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {g' : α → F'} {l : filter α} : is_O g' (fun (x : α) => (f' x, g' x)) l := is_O_with.is_O is_O_with_snd_prod theorem is_O_fst_prod' {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {l : filter α} {f' : α → E' × F'} : is_O (fun (x : α) => prod.fst (f' x)) f' l := is_O_fst_prod theorem is_O_snd_prod' {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {l : filter α} {f' : α → E' × F'} : is_O (fun (x : α) => prod.snd (f' x)) f' l := is_O_snd_prod theorem is_O_with.prod_rightl {α : Type u_1} {E : Type u_3} {F' : Type u_7} {G' : Type u_8} [has_norm E] [normed_group F'] [normed_group G'] {c : ℝ} {f : α → E} {g' : α → F'} (k' : α → G') {l : filter α} (h : is_O_with c f g' l) (hc : 0 ≤ c) : is_O_with c f (fun (x : α) => (g' x, k' x)) l := is_O_with.congr_const (mul_one c) (is_O_with.trans h is_O_with_fst_prod hc) theorem is_O.prod_rightl {α : Type u_1} {E : Type u_3} {F' : Type u_7} {G' : Type u_8} [has_norm E] [normed_group F'] [normed_group G'] {f : α → E} {g' : α → F'} (k' : α → G') {l : filter α} (h : is_O f g' l) : is_O f (fun (x : α) => (g' x, k' x)) l := sorry theorem is_o.prod_rightl {α : Type u_1} {E : Type u_3} {F' : Type u_7} {G' : Type u_8} [has_norm E] [normed_group F'] [normed_group G'] {f : α → E} {g' : α → F'} (k' : α → G') {l : filter α} (h : is_o f g' l) : is_o f (fun (x : α) => (g' x, k' x)) l := fun (c : ℝ) (cpos : 0 < c) => is_O_with.prod_rightl k' (h cpos) (le_of_lt cpos) theorem is_O_with.prod_rightr {α : Type u_1} {E : Type u_3} {E' : Type u_6} {F' : Type u_7} [has_norm E] [normed_group E'] [normed_group F'] {c : ℝ} {f : α → E} (f' : α → E') {g' : α → F'} {l : filter α} (h : is_O_with c f g' l) (hc : 0 ≤ c) : is_O_with c f (fun (x : α) => (f' x, g' x)) l := is_O_with.congr_const (mul_one c) (is_O_with.trans h is_O_with_snd_prod hc) theorem is_O.prod_rightr {α : Type u_1} {E : Type u_3} {E' : Type u_6} {F' : Type u_7} [has_norm E] [normed_group E'] [normed_group F'] {f : α → E} (f' : α → E') {g' : α → F'} {l : filter α} (h : is_O f g' l) : is_O f (fun (x : α) => (f' x, g' x)) l := sorry theorem is_o.prod_rightr {α : Type u_1} {E : Type u_3} {E' : Type u_6} {F' : Type u_7} [has_norm E] [normed_group E'] [normed_group F'] {f : α → E} (f' : α → E') {g' : α → F'} {l : filter α} (h : is_o f g' l) : is_o f (fun (x : α) => (f' x, g' x)) l := fun (c : ℝ) (cpos : 0 < c) => is_O_with.prod_rightr f' (h cpos) (le_of_lt cpos) theorem is_O_with.prod_left_same {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {G' : Type u_8} [normed_group E'] [normed_group F'] [normed_group G'] {c : ℝ} {f' : α → E'} {g' : α → F'} {k' : α → G'} {l : filter α} (hf : is_O_with c f' k' l) (hg : is_O_with c g' k' l) : is_O_with c (fun (x : α) => (f' x, g' x)) k' l := filter.mp_sets hg (filter.mp_sets hf (filter.univ_mem_sets' (id fun (x : α) => max_le))) theorem is_O_with.prod_left {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {G' : Type u_8} [normed_group E'] [normed_group F'] [normed_group G'] {c : ℝ} {c' : ℝ} {f' : α → E'} {g' : α → F'} {k' : α → G'} {l : filter α} (hf : is_O_with c f' k' l) (hg : is_O_with c' g' k' l) : is_O_with (max c c') (fun (x : α) => (f' x, g' x)) k' l := is_O_with.prod_left_same (is_O_with.weaken hf (le_max_left c c')) (is_O_with.weaken hg (le_max_right c c')) theorem is_O_with.prod_left_fst {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {G' : Type u_8} [normed_group E'] [normed_group F'] [normed_group G'] {c : ℝ} {f' : α → E'} {g' : α → F'} {k' : α → G'} {l : filter α} (h : is_O_with c (fun (x : α) => (f' x, g' x)) k' l) : is_O_with c f' k' l := is_O_with.congr_const (one_mul c) (is_O_with.trans is_O_with_fst_prod h zero_le_one) theorem is_O_with.prod_left_snd {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {G' : Type u_8} [normed_group E'] [normed_group F'] [normed_group G'] {c : ℝ} {f' : α → E'} {g' : α → F'} {k' : α → G'} {l : filter α} (h : is_O_with c (fun (x : α) => (f' x, g' x)) k' l) : is_O_with c g' k' l := is_O_with.congr_const (one_mul c) (is_O_with.trans is_O_with_snd_prod h zero_le_one) theorem is_O_with_prod_left {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {G' : Type u_8} [normed_group E'] [normed_group F'] [normed_group G'] {c : ℝ} {f' : α → E'} {g' : α → F'} {k' : α → G'} {l : filter α} : is_O_with c (fun (x : α) => (f' x, g' x)) k' l ↔ is_O_with c f' k' l ∧ is_O_with c g' k' l := sorry theorem is_O.prod_left {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {G' : Type u_8} [normed_group E'] [normed_group F'] [normed_group G'] {f' : α → E'} {g' : α → F'} {k' : α → G'} {l : filter α} (hf : is_O f' k' l) (hg : is_O g' k' l) : is_O (fun (x : α) => (f' x, g' x)) k' l := sorry theorem is_O.prod_left_fst {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {G' : Type u_8} [normed_group E'] [normed_group F'] [normed_group G'] {f' : α → E'} {g' : α → F'} {k' : α → G'} {l : filter α} (h : is_O (fun (x : α) => (f' x, g' x)) k' l) : is_O f' k' l := is_O.trans is_O_fst_prod h theorem is_O.prod_left_snd {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {G' : Type u_8} [normed_group E'] [normed_group F'] [normed_group G'] {f' : α → E'} {g' : α → F'} {k' : α → G'} {l : filter α} (h : is_O (fun (x : α) => (f' x, g' x)) k' l) : is_O g' k' l := is_O.trans is_O_snd_prod h @[simp] theorem is_O_prod_left {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {G' : Type u_8} [normed_group E'] [normed_group F'] [normed_group G'] {f' : α → E'} {g' : α → F'} {k' : α → G'} {l : filter α} : is_O (fun (x : α) => (f' x, g' x)) k' l ↔ is_O f' k' l ∧ is_O g' k' l := sorry theorem is_o.prod_left {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {G' : Type u_8} [normed_group E'] [normed_group F'] [normed_group G'] {f' : α → E'} {g' : α → F'} {k' : α → G'} {l : filter α} (hf : is_o f' k' l) (hg : is_o g' k' l) : is_o (fun (x : α) => (f' x, g' x)) k' l := fun (c : ℝ) (hc : 0 < c) => is_O_with.prod_left_same (hf hc) (hg hc) theorem is_o.prod_left_fst {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {G' : Type u_8} [normed_group E'] [normed_group F'] [normed_group G'] {f' : α → E'} {g' : α → F'} {k' : α → G'} {l : filter α} (h : is_o (fun (x : α) => (f' x, g' x)) k' l) : is_o f' k' l := is_O.trans_is_o is_O_fst_prod h theorem is_o.prod_left_snd {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {G' : Type u_8} [normed_group E'] [normed_group F'] [normed_group G'] {f' : α → E'} {g' : α → F'} {k' : α → G'} {l : filter α} (h : is_o (fun (x : α) => (f' x, g' x)) k' l) : is_o g' k' l := is_O.trans_is_o is_O_snd_prod h @[simp] theorem is_o_prod_left {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {G' : Type u_8} [normed_group E'] [normed_group F'] [normed_group G'] {f' : α → E'} {g' : α → F'} {k' : α → G'} {l : filter α} : is_o (fun (x : α) => (f' x, g' x)) k' l ↔ is_o f' k' l ∧ is_o g' k' l := sorry theorem is_O_with.eq_zero_imp {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {c : ℝ} {f' : α → E'} {g' : α → F'} {l : filter α} (h : is_O_with c f' g' l) : filter.eventually (fun (x : α) => g' x = 0 → f' x = 0) l := sorry theorem is_O.eq_zero_imp {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {g' : α → F'} {l : filter α} (h : is_O f' g' l) : filter.eventually (fun (x : α) => g' x = 0 → f' x = 0) l := sorry /-! ### Addition and subtraction -/ theorem is_O_with.add {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {c₁ : ℝ} {c₂ : ℝ} {f₁ : α → E'} {f₂ : α → E'} (h₁ : is_O_with c₁ f₁ g l) (h₂ : is_O_with c₂ f₂ g l) : is_O_with (c₁ + c₂) (fun (x : α) => f₁ x + f₂ x) g l := sorry theorem is_O.add {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} : is_O f₁ g l → is_O f₂ g l → is_O (fun (x : α) => f₁ x + f₂ x) g l := sorry theorem is_o.add {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} (h₁ : is_o f₁ g l) (h₂ : is_o f₂ g l) : is_o (fun (x : α) => f₁ x + f₂ x) g l := fun (c : ℝ) (cpos : 0 < c) => is_O_with.congr_const (add_halves c) (is_O_with.add (h₁ (half_pos cpos)) (h₂ (half_pos cpos))) theorem is_o.add_add {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {l : filter α} {f₁ : α → E'} {f₂ : α → E'} {g₁ : α → F'} {g₂ : α → F'} (h₁ : is_o f₁ g₁ l) (h₂ : is_o f₂ g₂ l) : is_o (fun (x : α) => f₁ x + f₂ x) (fun (x : α) => norm (g₁ x) + norm (g₂ x)) l := sorry theorem is_O.add_is_o {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} (h₁ : is_O f₁ g l) (h₂ : is_o f₂ g l) : is_O (fun (x : α) => f₁ x + f₂ x) g l := is_O.add h₁ (is_o.is_O h₂) theorem is_o.add_is_O {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} (h₁ : is_o f₁ g l) (h₂ : is_O f₂ g l) : is_O (fun (x : α) => f₁ x + f₂ x) g l := is_O.add (is_o.is_O h₁) h₂ theorem is_O_with.add_is_o {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {c₁ : ℝ} {c₂ : ℝ} {f₁ : α → E'} {f₂ : α → E'} (h₁ : is_O_with c₁ f₁ g l) (h₂ : is_o f₂ g l) (hc : c₁ < c₂) : is_O_with c₂ (fun (x : α) => f₁ x + f₂ x) g l := is_O_with.congr_const (add_sub_cancel'_right c₁ c₂) (is_O_with.add h₁ (h₂ (iff.mpr sub_pos hc))) theorem is_o.add_is_O_with {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {c₁ : ℝ} {c₂ : ℝ} {f₁ : α → E'} {f₂ : α → E'} (h₁ : is_o f₁ g l) (h₂ : is_O_with c₁ f₂ g l) (hc : c₁ < c₂) : is_O_with c₂ (fun (x : α) => f₁ x + f₂ x) g l := is_O_with.congr_left (fun (_x : α) => add_comm (f₂ _x) (f₁ _x)) (is_O_with.add_is_o h₂ h₁ hc) theorem is_O_with.sub {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {c₁ : ℝ} {c₂ : ℝ} {f₁ : α → E'} {f₂ : α → E'} (h₁ : is_O_with c₁ f₁ g l) (h₂ : is_O_with c₂ f₂ g l) : is_O_with (c₁ + c₂) (fun (x : α) => f₁ x - f₂ x) g l := sorry theorem is_O_with.sub_is_o {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {c₁ : ℝ} {c₂ : ℝ} {f₁ : α → E'} {f₂ : α → E'} (h₁ : is_O_with c₁ f₁ g l) (h₂ : is_o f₂ g l) (hc : c₁ < c₂) : is_O_with c₂ (fun (x : α) => f₁ x - f₂ x) g l := sorry theorem is_O.sub {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} (h₁ : is_O f₁ g l) (h₂ : is_O f₂ g l) : is_O (fun (x : α) => f₁ x - f₂ x) g l := sorry theorem is_o.sub {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} (h₁ : is_o f₁ g l) (h₂ : is_o f₂ g l) : is_o (fun (x : α) => f₁ x - f₂ x) g l := sorry /-! ### Lemmas about `is_O (f₁ - f₂) g l` / `is_o (f₁ - f₂) g l` treated as a binary relation -/ theorem is_O_with.symm {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {c : ℝ} {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} (h : is_O_with c (fun (x : α) => f₁ x - f₂ x) g l) : is_O_with c (fun (x : α) => f₂ x - f₁ x) g l := is_O_with.congr_left (fun (x : α) => neg_sub (f₁ x) (f₂ x)) (is_O_with.neg_left h) theorem is_O_with_comm {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {c : ℝ} {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} : is_O_with c (fun (x : α) => f₁ x - f₂ x) g l ↔ is_O_with c (fun (x : α) => f₂ x - f₁ x) g l := { mp := is_O_with.symm, mpr := is_O_with.symm } theorem is_O.symm {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} (h : is_O (fun (x : α) => f₁ x - f₂ x) g l) : is_O (fun (x : α) => f₂ x - f₁ x) g l := is_O.congr_left (fun (x : α) => neg_sub (f₁ x) (f₂ x)) (is_O.neg_left h) theorem is_O_comm {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} : is_O (fun (x : α) => f₁ x - f₂ x) g l ↔ is_O (fun (x : α) => f₂ x - f₁ x) g l := { mp := is_O.symm, mpr := is_O.symm } theorem is_o.symm {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} (h : is_o (fun (x : α) => f₁ x - f₂ x) g l) : is_o (fun (x : α) => f₂ x - f₁ x) g l := sorry theorem is_o_comm {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} : is_o (fun (x : α) => f₁ x - f₂ x) g l ↔ is_o (fun (x : α) => f₂ x - f₁ x) g l := { mp := is_o.symm, mpr := is_o.symm } theorem is_O_with.triangle {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {c : ℝ} {c' : ℝ} {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} {f₃ : α → E'} (h₁ : is_O_with c (fun (x : α) => f₁ x - f₂ x) g l) (h₂ : is_O_with c' (fun (x : α) => f₂ x - f₃ x) g l) : is_O_with (c + c') (fun (x : α) => f₁ x - f₃ x) g l := is_O_with.congr_left (fun (x : α) => sub_add_sub_cancel (f₁ x) (f₂ x) (f₃ x)) (is_O_with.add h₁ h₂) theorem is_O.triangle {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} {f₃ : α → E'} (h₁ : is_O (fun (x : α) => f₁ x - f₂ x) g l) (h₂ : is_O (fun (x : α) => f₂ x - f₃ x) g l) : is_O (fun (x : α) => f₁ x - f₃ x) g l := is_O.congr_left (fun (x : α) => sub_add_sub_cancel (f₁ x) (f₂ x) (f₃ x)) (is_O.add h₁ h₂) theorem is_o.triangle {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} {f₃ : α → E'} (h₁ : is_o (fun (x : α) => f₁ x - f₂ x) g l) (h₂ : is_o (fun (x : α) => f₂ x - f₃ x) g l) : is_o (fun (x : α) => f₁ x - f₃ x) g l := is_o.congr_left (fun (x : α) => sub_add_sub_cancel (f₁ x) (f₂ x) (f₃ x)) (is_o.add h₁ h₂) theorem is_O.congr_of_sub {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} (h : is_O (fun (x : α) => f₁ x - f₂ x) g l) : is_O f₁ g l ↔ is_O f₂ g l := { mp := fun (h' : is_O f₁ g l) => is_O.congr_left (fun (x : α) => sub_sub_cancel (f₁ x) (f₂ x)) (is_O.sub h' h), mpr := fun (h' : is_O f₂ g l) => is_O.congr_left (fun (x : α) => sub_add_cancel (f₁ x) (f₂ x)) (is_O.add h h') } theorem is_o.congr_of_sub {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} (h : is_o (fun (x : α) => f₁ x - f₂ x) g l) : is_o f₁ g l ↔ is_o f₂ g l := { mp := fun (h' : is_o f₁ g l) => is_o.congr_left (fun (x : α) => sub_sub_cancel (f₁ x) (f₂ x)) (is_o.sub h' h), mpr := fun (h' : is_o f₂ g l) => is_o.congr_left (fun (x : α) => sub_add_cancel (f₁ x) (f₂ x)) (is_o.add h h') } /-! ### Zero, one, and other constants -/ theorem is_o_zero {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] (g' : α → F') (l : filter α) : is_o (fun (x : α) => 0) g' l := sorry theorem is_O_with_zero {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {c : ℝ} (g' : α → F') (l : filter α) (hc : 0 ≤ c) : is_O_with c (fun (x : α) => 0) g' l := sorry theorem is_O_with_zero' {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] (g : α → F) (l : filter α) : is_O_with 0 (fun (x : α) => 0) g l := sorry theorem is_O_zero {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] (g : α → F) (l : filter α) : is_O (fun (x : α) => 0) g l := Exists.intro 0 (is_O_with_zero' g l) theorem is_O_refl_left {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} (g' : α → F') (l : filter α) : is_O (fun (x : α) => f' x - f' x) g' l := is_O.congr_left (fun (x : α) => Eq.symm (sub_self (f' x))) (is_O_zero g' l) theorem is_o_refl_left {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} (g' : α → F') (l : filter α) : is_o (fun (x : α) => f' x - f' x) g' l := is_o.congr_left (fun (x : α) => Eq.symm (sub_self (f' x))) (is_o_zero g' l) @[simp] theorem is_O_with_zero_right_iff {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {c : ℝ} {f' : α → E'} {l : filter α} : is_O_with c f' (fun (x : α) => 0) l ↔ filter.eventually (fun (x : α) => f' x = 0) l := sorry @[simp] theorem is_O_zero_right_iff {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {l : filter α} : is_O f' (fun (x : α) => 0) l ↔ filter.eventually (fun (x : α) => f' x = 0) l := sorry @[simp] theorem is_o_zero_right_iff {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {l : filter α} : is_o f' (fun (x : α) => 0) l ↔ filter.eventually (fun (x : α) => f' x = 0) l := { mp := fun (h : is_o f' (fun (x : α) => 0) l) => iff.mp is_O_zero_right_iff (is_o.is_O h), mpr := fun (h : filter.eventually (fun (x : α) => f' x = 0) l) (c : ℝ) (hc : 0 < c) => iff.mpr is_O_with_zero_right_iff h } theorem is_O_with_const_const {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] (c : E) {c' : F'} (hc' : c' ≠ 0) (l : filter α) : is_O_with (norm c / norm c') (fun (x : α) => c) (fun (x : α) => c') l := sorry theorem is_O_const_const {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] (c : E) {c' : F'} (hc' : c' ≠ 0) (l : filter α) : is_O (fun (x : α) => c) (fun (x : α) => c') l := is_O_with.is_O (is_O_with_const_const c hc' l) @[simp] theorem is_O_with_top {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c : ℝ} {f : α → E} {g : α → F} : is_O_with c f g ⊤ ↔ ∀ (x : α), norm (f x) ≤ c * norm (g x) := iff.rfl @[simp] theorem is_O_top {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} : is_O f g ⊤ ↔ ∃ (C : ℝ), ∀ (x : α), norm (f x) ≤ C * norm (g x) := iff.rfl @[simp] theorem is_o_top {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {g' : α → F'} : is_o f' g' ⊤ ↔ ∀ (x : α), f' x = 0 := sorry @[simp] theorem is_O_with_principal {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {c : ℝ} {f : α → E} {g : α → F} {s : set α} : is_O_with c f g (filter.principal s) ↔ ∀ (x : α), x ∈ s → norm (f x) ≤ c * norm (g x) := iff.rfl theorem is_O_principal {α : Type u_1} {E : Type u_3} {F : Type u_4} [has_norm E] [has_norm F] {f : α → E} {g : α → F} {s : set α} : is_O f g (filter.principal s) ↔ ∃ (c : ℝ), ∀ (x : α), x ∈ s → norm (f x) ≤ c * norm (g x) := iff.rfl theorem is_O_with_const_one {α : Type u_1} {E : Type u_3} {𝕜 : Type u_11} [has_norm E] [normed_field 𝕜] (c : E) (l : filter α) : is_O_with (norm c) (fun (x : α) => c) (fun (x : α) => 1) l := sorry theorem is_O_const_one {α : Type u_1} {E : Type u_3} {𝕜 : Type u_11} [has_norm E] [normed_field 𝕜] (c : E) (l : filter α) : is_O (fun (x : α) => c) (fun (x : α) => 1) l := is_O_with.is_O (is_O_with_const_one c l) theorem is_o_const_iff_is_o_one {α : Type u_1} {E : Type u_3} {F' : Type u_7} (𝕜 : Type u_11) [has_norm E] [normed_group F'] [normed_field 𝕜] {f : α → E} {l : filter α} {c : F'} (hc : c ≠ 0) : is_o f (fun (x : α) => c) l ↔ is_o f (fun (x : α) => 1) l := { mp := fun (h : is_o f (fun (x : α) => c) l) => is_o.trans_is_O h (is_O_const_one c l), mpr := fun (h : is_o f (fun (x : α) => 1) l) => is_o.trans_is_O h (is_O_const_const 1 hc l) } theorem is_o_const_iff {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {l : filter α} {c : F'} (hc : c ≠ 0) : is_o f' (fun (x : α) => c) l ↔ filter.tendsto f' l (nhds 0) := sorry theorem is_o_id_const {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {c : F'} (hc : c ≠ 0) : is_o (fun (x : E') => x) (fun (x : E') => c) (nhds 0) := iff.mpr (is_o_const_iff hc) (continuous.tendsto continuous_id 0) theorem is_O_const_of_tendsto {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {l : filter α} {y : E'} (h : filter.tendsto f' l (nhds y)) {c : F'} (hc : c ≠ 0) : is_O f' (fun (x : α) => c) l := sorry theorem is_o_one_iff {α : Type u_1} {E' : Type u_6} (𝕜 : Type u_11) [normed_group E'] [normed_field 𝕜] {f' : α → E'} {l : filter α} : is_o f' (fun (x : α) => 1) l ↔ filter.tendsto f' l (nhds 0) := is_o_const_iff one_ne_zero theorem is_O_one_of_tendsto {α : Type u_1} {E' : Type u_6} (𝕜 : Type u_11) [normed_group E'] [normed_field 𝕜] {f' : α → E'} {l : filter α} {y : E'} (h : filter.tendsto f' l (nhds y)) : is_O f' (fun (x : α) => 1) l := is_O_const_of_tendsto h one_ne_zero theorem is_O.trans_tendsto_nhds {α : Type u_1} {E : Type u_3} {F' : Type u_7} (𝕜 : Type u_11) [has_norm E] [normed_group F'] [normed_field 𝕜] {f : α → E} {g' : α → F'} {l : filter α} (hfg : is_O f g' l) {y : F'} (hg : filter.tendsto g' l (nhds y)) : is_O f (fun (x : α) => 1) l := is_O.trans hfg (is_O_one_of_tendsto 𝕜 hg) theorem is_O.trans_tendsto {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {g' : α → F'} {l : filter α} (hfg : is_O f' g' l) (hg : filter.tendsto g' l (nhds 0)) : filter.tendsto f' l (nhds 0) := iff.mp (is_o_one_iff ℝ) (is_O.trans_is_o hfg (iff.mpr (is_o_one_iff ℝ) hg)) theorem is_o.trans_tendsto {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {g' : α → F'} {l : filter α} (hfg : is_o f' g' l) (hg : filter.tendsto g' l (nhds 0)) : filter.tendsto f' l (nhds 0) := is_O.trans_tendsto (is_o.is_O hfg) hg /-! ### Multiplication by a constant -/ theorem is_O_with_const_mul_self {α : Type u_1} {R : Type u_9} [normed_ring R] (c : R) (f : α → R) (l : filter α) : is_O_with (norm c) (fun (x : α) => c * f x) f l := is_O_with_of_le' l fun (x : α) => norm_mul_le c (f x) theorem is_O_const_mul_self {α : Type u_1} {R : Type u_9} [normed_ring R] (c : R) (f : α → R) (l : filter α) : is_O (fun (x : α) => c * f x) f l := is_O_with.is_O (is_O_with_const_mul_self c f l) theorem is_O_with.const_mul_left {α : Type u_1} {F : Type u_4} {R : Type u_9} [has_norm F] [normed_ring R] {c : ℝ} {g : α → F} {l : filter α} {f : α → R} (h : is_O_with c f g l) (c' : R) : is_O_with (norm c' * c) (fun (x : α) => c' * f x) g l := is_O_with.trans (is_O_with_const_mul_self c' f l) h (norm_nonneg c') theorem is_O.const_mul_left {α : Type u_1} {F : Type u_4} {R : Type u_9} [has_norm F] [normed_ring R] {g : α → F} {l : filter α} {f : α → R} (h : is_O f g l) (c' : R) : is_O (fun (x : α) => c' * f x) g l := sorry theorem is_O_with_self_const_mul' {α : Type u_1} {R : Type u_9} [normed_ring R] (u : units R) (f : α → R) (l : filter α) : is_O_with (norm ↑(u⁻¹)) f (fun (x : α) => ↑u * f x) l := is_O_with.congr_left (fun (x : α) => units.inv_mul_cancel_left u (f x)) (is_O_with_const_mul_self (↑(u⁻¹)) (fun (x : α) => ↑u * f x) l) theorem is_O_with_self_const_mul {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] (c : 𝕜) (hc : c ≠ 0) (f : α → 𝕜) (l : filter α) : is_O_with (norm c⁻¹) f (fun (x : α) => c * f x) l := is_O_with.congr_const (normed_field.norm_inv c) (is_O_with_self_const_mul' (units.mk0 c hc) f l) theorem is_O_self_const_mul' {α : Type u_1} {R : Type u_9} [normed_ring R] {c : R} (hc : is_unit c) (f : α → R) (l : filter α) : is_O f (fun (x : α) => c * f x) l := sorry theorem is_O_self_const_mul {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] (c : 𝕜) (hc : c ≠ 0) (f : α → 𝕜) (l : filter α) : is_O f (fun (x : α) => c * f x) l := is_O_self_const_mul' (is_unit.mk0 c hc) f l theorem is_O_const_mul_left_iff' {α : Type u_1} {F : Type u_4} {R : Type u_9} [has_norm F] [normed_ring R] {g : α → F} {l : filter α} {f : α → R} {c : R} (hc : is_unit c) : is_O (fun (x : α) => c * f x) g l ↔ is_O f g l := { mp := is_O.trans (is_O_self_const_mul' hc f l), mpr := fun (h : is_O f g l) => is_O.const_mul_left h c } theorem is_O_const_mul_left_iff {α : Type u_1} {F : Type u_4} {𝕜 : Type u_11} [has_norm F] [normed_field 𝕜] {g : α → F} {l : filter α} {f : α → 𝕜} {c : 𝕜} (hc : c ≠ 0) : is_O (fun (x : α) => c * f x) g l ↔ is_O f g l := is_O_const_mul_left_iff' (is_unit.mk0 c hc) theorem is_o.const_mul_left {α : Type u_1} {F : Type u_4} {R : Type u_9} [has_norm F] [normed_ring R] {g : α → F} {l : filter α} {f : α → R} (h : is_o f g l) (c : R) : is_o (fun (x : α) => c * f x) g l := is_O.trans_is_o (is_O_const_mul_self c f l) h theorem is_o_const_mul_left_iff' {α : Type u_1} {F : Type u_4} {R : Type u_9} [has_norm F] [normed_ring R] {g : α → F} {l : filter α} {f : α → R} {c : R} (hc : is_unit c) : is_o (fun (x : α) => c * f x) g l ↔ is_o f g l := { mp := is_O.trans_is_o (is_O_self_const_mul' hc f l), mpr := fun (h : is_o f g l) => is_o.const_mul_left h c } theorem is_o_const_mul_left_iff {α : Type u_1} {F : Type u_4} {𝕜 : Type u_11} [has_norm F] [normed_field 𝕜] {g : α → F} {l : filter α} {f : α → 𝕜} {c : 𝕜} (hc : c ≠ 0) : is_o (fun (x : α) => c * f x) g l ↔ is_o f g l := is_o_const_mul_left_iff' (is_unit.mk0 c hc) theorem is_O_with.of_const_mul_right {α : Type u_1} {E : Type u_3} {R : Type u_9} [has_norm E] [normed_ring R] {c' : ℝ} {f : α → E} {l : filter α} {g : α → R} {c : R} (hc' : 0 ≤ c') (h : is_O_with c' f (fun (x : α) => c * g x) l) : is_O_with (c' * norm c) f g l := is_O_with.trans h (is_O_with_const_mul_self c g l) hc' theorem is_O.of_const_mul_right {α : Type u_1} {E : Type u_3} {R : Type u_9} [has_norm E] [normed_ring R] {f : α → E} {l : filter α} {g : α → R} {c : R} (h : is_O f (fun (x : α) => c * g x) l) : is_O f g l := sorry theorem is_O_with.const_mul_right' {α : Type u_1} {E : Type u_3} {R : Type u_9} [has_norm E] [normed_ring R] {f : α → E} {l : filter α} {g : α → R} {u : units R} {c' : ℝ} (hc' : 0 ≤ c') (h : is_O_with c' f g l) : is_O_with (c' * norm ↑(u⁻¹)) f (fun (x : α) => ↑u * g x) l := is_O_with.trans h (is_O_with_self_const_mul' u g l) hc' theorem is_O_with.const_mul_right {α : Type u_1} {E : Type u_3} {𝕜 : Type u_11} [has_norm E] [normed_field 𝕜] {f : α → E} {l : filter α} {g : α → 𝕜} {c : 𝕜} (hc : c ≠ 0) {c' : ℝ} (hc' : 0 ≤ c') (h : is_O_with c' f g l) : is_O_with (c' * (norm c⁻¹)) f (fun (x : α) => c * g x) l := is_O_with.trans h (is_O_with_self_const_mul c hc g l) hc' theorem is_O.const_mul_right' {α : Type u_1} {E : Type u_3} {R : Type u_9} [has_norm E] [normed_ring R] {f : α → E} {l : filter α} {g : α → R} {c : R} (hc : is_unit c) (h : is_O f g l) : is_O f (fun (x : α) => c * g x) l := is_O.trans h (is_O_self_const_mul' hc g l) theorem is_O.const_mul_right {α : Type u_1} {E : Type u_3} {𝕜 : Type u_11} [has_norm E] [normed_field 𝕜] {f : α → E} {l : filter α} {g : α → 𝕜} {c : 𝕜} (hc : c ≠ 0) (h : is_O f g l) : is_O f (fun (x : α) => c * g x) l := is_O.const_mul_right' (is_unit.mk0 c hc) h theorem is_O_const_mul_right_iff' {α : Type u_1} {E : Type u_3} {R : Type u_9} [has_norm E] [normed_ring R] {f : α → E} {l : filter α} {g : α → R} {c : R} (hc : is_unit c) : is_O f (fun (x : α) => c * g x) l ↔ is_O f g l := { mp := fun (h : is_O f (fun (x : α) => c * g x) l) => is_O.of_const_mul_right h, mpr := fun (h : is_O f g l) => is_O.const_mul_right' hc h } theorem is_O_const_mul_right_iff {α : Type u_1} {E : Type u_3} {𝕜 : Type u_11} [has_norm E] [normed_field 𝕜] {f : α → E} {l : filter α} {g : α → 𝕜} {c : 𝕜} (hc : c ≠ 0) : is_O f (fun (x : α) => c * g x) l ↔ is_O f g l := is_O_const_mul_right_iff' (is_unit.mk0 c hc) theorem is_o.of_const_mul_right {α : Type u_1} {E : Type u_3} {R : Type u_9} [has_norm E] [normed_ring R] {f : α → E} {l : filter α} {g : α → R} {c : R} (h : is_o f (fun (x : α) => c * g x) l) : is_o f g l := is_o.trans_is_O h (is_O_const_mul_self c g l) theorem is_o.const_mul_right' {α : Type u_1} {E : Type u_3} {R : Type u_9} [has_norm E] [normed_ring R] {f : α → E} {l : filter α} {g : α → R} {c : R} (hc : is_unit c) (h : is_o f g l) : is_o f (fun (x : α) => c * g x) l := is_o.trans_is_O h (is_O_self_const_mul' hc g l) theorem is_o.const_mul_right {α : Type u_1} {E : Type u_3} {𝕜 : Type u_11} [has_norm E] [normed_field 𝕜] {f : α → E} {l : filter α} {g : α → 𝕜} {c : 𝕜} (hc : c ≠ 0) (h : is_o f g l) : is_o f (fun (x : α) => c * g x) l := is_o.const_mul_right' (is_unit.mk0 c hc) h theorem is_o_const_mul_right_iff' {α : Type u_1} {E : Type u_3} {R : Type u_9} [has_norm E] [normed_ring R] {f : α → E} {l : filter α} {g : α → R} {c : R} (hc : is_unit c) : is_o f (fun (x : α) => c * g x) l ↔ is_o f g l := { mp := fun (h : is_o f (fun (x : α) => c * g x) l) => is_o.of_const_mul_right h, mpr := fun (h : is_o f g l) => is_o.const_mul_right' hc h } theorem is_o_const_mul_right_iff {α : Type u_1} {E : Type u_3} {𝕜 : Type u_11} [has_norm E] [normed_field 𝕜] {f : α → E} {l : filter α} {g : α → 𝕜} {c : 𝕜} (hc : c ≠ 0) : is_o f (fun (x : α) => c * g x) l ↔ is_o f g l := is_o_const_mul_right_iff' (is_unit.mk0 c hc) /-! ### Multiplication -/ theorem is_O_with.mul {α : Type u_1} {R : Type u_9} {𝕜 : Type u_11} [normed_ring R] [normed_field 𝕜] {l : filter α} {f₁ : α → R} {f₂ : α → R} {g₁ : α → 𝕜} {g₂ : α → 𝕜} {c₁ : ℝ} {c₂ : ℝ} (h₁ : is_O_with c₁ f₁ g₁ l) (h₂ : is_O_with c₂ f₂ g₂ l) : is_O_with (c₁ * c₂) (fun (x : α) => f₁ x * f₂ x) (fun (x : α) => g₁ x * g₂ x) l := sorry theorem is_O.mul {α : Type u_1} {R : Type u_9} {𝕜 : Type u_11} [normed_ring R] [normed_field 𝕜] {l : filter α} {f₁ : α → R} {f₂ : α → R} {g₁ : α → 𝕜} {g₂ : α → 𝕜} (h₁ : is_O f₁ g₁ l) (h₂ : is_O f₂ g₂ l) : is_O (fun (x : α) => f₁ x * f₂ x) (fun (x : α) => g₁ x * g₂ x) l := sorry theorem is_O.mul_is_o {α : Type u_1} {R : Type u_9} {𝕜 : Type u_11} [normed_ring R] [normed_field 𝕜] {l : filter α} {f₁ : α → R} {f₂ : α → R} {g₁ : α → 𝕜} {g₂ : α → 𝕜} (h₁ : is_O f₁ g₁ l) (h₂ : is_o f₂ g₂ l) : is_o (fun (x : α) => f₁ x * f₂ x) (fun (x : α) => g₁ x * g₂ x) l := sorry theorem is_o.mul_is_O {α : Type u_1} {R : Type u_9} {𝕜 : Type u_11} [normed_ring R] [normed_field 𝕜] {l : filter α} {f₁ : α → R} {f₂ : α → R} {g₁ : α → 𝕜} {g₂ : α → 𝕜} (h₁ : is_o f₁ g₁ l) (h₂ : is_O f₂ g₂ l) : is_o (fun (x : α) => f₁ x * f₂ x) (fun (x : α) => g₁ x * g₂ x) l := sorry theorem is_o.mul {α : Type u_1} {R : Type u_9} {𝕜 : Type u_11} [normed_ring R] [normed_field 𝕜] {l : filter α} {f₁ : α → R} {f₂ : α → R} {g₁ : α → 𝕜} {g₂ : α → 𝕜} (h₁ : is_o f₁ g₁ l) (h₂ : is_o f₂ g₂ l) : is_o (fun (x : α) => f₁ x * f₂ x) (fun (x : α) => g₁ x * g₂ x) l := is_o.mul_is_O h₁ (is_o.is_O h₂) theorem is_O_with.pow' {α : Type u_1} {R : Type u_9} {𝕜 : Type u_11} [normed_ring R] [normed_field 𝕜] {c : ℝ} {l : filter α} {f : α → R} {g : α → 𝕜} (h : is_O_with c f g l) (n : ℕ) : is_O_with (nat.cases_on n (norm 1) fun (n : ℕ) => c ^ (n + 1)) (fun (x : α) => f x ^ n) (fun (x : α) => g x ^ n) l := sorry theorem is_O_with.pow {α : Type u_1} {R : Type u_9} {𝕜 : Type u_11} [normed_ring R] [normed_field 𝕜] {c : ℝ} {l : filter α} [norm_one_class R] {f : α → R} {g : α → 𝕜} (h : is_O_with c f g l) (n : ℕ) : is_O_with (c ^ n) (fun (x : α) => f x ^ n) (fun (x : α) => g x ^ n) l := sorry theorem is_O.pow {α : Type u_1} {R : Type u_9} {𝕜 : Type u_11} [normed_ring R] [normed_field 𝕜] {l : filter α} {f : α → R} {g : α → 𝕜} (h : is_O f g l) (n : ℕ) : is_O (fun (x : α) => f x ^ n) (fun (x : α) => g x ^ n) l := sorry theorem is_o.pow {α : Type u_1} {R : Type u_9} {𝕜 : Type u_11} [normed_ring R] [normed_field 𝕜] {l : filter α} {f : α → R} {g : α → 𝕜} (h : is_o f g l) {n : ℕ} (hn : 0 < n) : is_o (fun (x : α) => f x ^ n) (fun (x : α) => g x ^ n) l := sorry /-! ### Scalar multiplication -/ theorem is_O_with.const_smul_left {α : Type u_1} {F : Type u_4} {E' : Type u_6} {𝕜 : Type u_11} [has_norm F] [normed_group E'] [normed_field 𝕜] {c : ℝ} {g : α → F} {f' : α → E'} {l : filter α} [normed_space 𝕜 E'] (h : is_O_with c f' g l) (c' : 𝕜) : is_O_with (norm c' * c) (fun (x : α) => c' • f' x) g l := sorry theorem is_O_const_smul_left_iff {α : Type u_1} {F : Type u_4} {E' : Type u_6} {𝕜 : Type u_11} [has_norm F] [normed_group E'] [normed_field 𝕜] {g : α → F} {f' : α → E'} {l : filter α} [normed_space 𝕜 E'] {c : 𝕜} (hc : c ≠ 0) : is_O (fun (x : α) => c • f' x) g l ↔ is_O f' g l := sorry theorem is_o_const_smul_left {α : Type u_1} {F : Type u_4} {E' : Type u_6} {𝕜 : Type u_11} [has_norm F] [normed_group E'] [normed_field 𝕜] {g : α → F} {f' : α → E'} {l : filter α} [normed_space 𝕜 E'] (h : is_o f' g l) (c : 𝕜) : is_o (fun (x : α) => c • f' x) g l := is_o.of_norm_left (is_o.congr_left (fun (x : α) => Eq.symm (norm_smul c (f' x))) (is_o.const_mul_left (is_o.norm_left h) (norm c))) theorem is_o_const_smul_left_iff {α : Type u_1} {F : Type u_4} {E' : Type u_6} {𝕜 : Type u_11} [has_norm F] [normed_group E'] [normed_field 𝕜] {g : α → F} {f' : α → E'} {l : filter α} [normed_space 𝕜 E'] {c : 𝕜} (hc : c ≠ 0) : is_o (fun (x : α) => c • f' x) g l ↔ is_o f' g l := sorry theorem is_O_const_smul_right {α : Type u_1} {E : Type u_3} {E' : Type u_6} {𝕜 : Type u_11} [has_norm E] [normed_group E'] [normed_field 𝕜] {f : α → E} {f' : α → E'} {l : filter α} [normed_space 𝕜 E'] {c : 𝕜} (hc : c ≠ 0) : is_O f (fun (x : α) => c • f' x) l ↔ is_O f f' l := sorry theorem is_o_const_smul_right {α : Type u_1} {E : Type u_3} {E' : Type u_6} {𝕜 : Type u_11} [has_norm E] [normed_group E'] [normed_field 𝕜] {f : α → E} {f' : α → E'} {l : filter α} [normed_space 𝕜 E'] {c : 𝕜} (hc : c ≠ 0) : is_o f (fun (x : α) => c • f' x) l ↔ is_o f f' l := sorry theorem is_O_with.smul {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {𝕜 : Type u_11} [normed_group E'] [normed_group F'] [normed_field 𝕜] {c : ℝ} {c' : ℝ} {f' : α → E'} {g' : α → F'} {l : filter α} [normed_space 𝕜 E'] [normed_space 𝕜 F'] {k₁ : α → 𝕜} {k₂ : α → 𝕜} (h₁ : is_O_with c k₁ k₂ l) (h₂ : is_O_with c' f' g' l) : is_O_with (c * c') (fun (x : α) => k₁ x • f' x) (fun (x : α) => k₂ x • g' x) l := sorry theorem is_O.smul {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {𝕜 : Type u_11} [normed_group E'] [normed_group F'] [normed_field 𝕜] {f' : α → E'} {g' : α → F'} {l : filter α} [normed_space 𝕜 E'] [normed_space 𝕜 F'] {k₁ : α → 𝕜} {k₂ : α → 𝕜} (h₁ : is_O k₁ k₂ l) (h₂ : is_O f' g' l) : is_O (fun (x : α) => k₁ x • f' x) (fun (x : α) => k₂ x • g' x) l := sorry theorem is_O.smul_is_o {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {𝕜 : Type u_11} [normed_group E'] [normed_group F'] [normed_field 𝕜] {f' : α → E'} {g' : α → F'} {l : filter α} [normed_space 𝕜 E'] [normed_space 𝕜 F'] {k₁ : α → 𝕜} {k₂ : α → 𝕜} (h₁ : is_O k₁ k₂ l) (h₂ : is_o f' g' l) : is_o (fun (x : α) => k₁ x • f' x) (fun (x : α) => k₂ x • g' x) l := sorry theorem is_o.smul_is_O {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {𝕜 : Type u_11} [normed_group E'] [normed_group F'] [normed_field 𝕜] {f' : α → E'} {g' : α → F'} {l : filter α} [normed_space 𝕜 E'] [normed_space 𝕜 F'] {k₁ : α → 𝕜} {k₂ : α → 𝕜} (h₁ : is_o k₁ k₂ l) (h₂ : is_O f' g' l) : is_o (fun (x : α) => k₁ x • f' x) (fun (x : α) => k₂ x • g' x) l := sorry theorem is_o.smul {α : Type u_1} {E' : Type u_6} {F' : Type u_7} {𝕜 : Type u_11} [normed_group E'] [normed_group F'] [normed_field 𝕜] {f' : α → E'} {g' : α → F'} {l : filter α} [normed_space 𝕜 E'] [normed_space 𝕜 F'] {k₁ : α → 𝕜} {k₂ : α → 𝕜} (h₁ : is_o k₁ k₂ l) (h₂ : is_o f' g' l) : is_o (fun (x : α) => k₁ x • f' x) (fun (x : α) => k₂ x • g' x) l := sorry /-! ### Sum -/ theorem is_O_with.sum {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {ι : Type u_13} {A : ι → α → E'} {C : ι → ℝ} {s : finset ι} (h : ∀ (i : ι), i ∈ s → is_O_with (C i) (A i) g l) : is_O_with (finset.sum s fun (i : ι) => C i) (fun (x : α) => finset.sum s fun (i : ι) => A i x) g l := sorry theorem is_O.sum {α : Type u_1} {F : Type u_4} {E' : Type u_6} [has_norm F] [normed_group E'] {g : α → F} {l : filter α} {ι : Type u_13} {A : ι → α → E'} {s : finset ι} (h : ∀ (i : ι), i ∈ s → is_O (A i) g l) : is_O (fun (x : α) => finset.sum s fun (i : ι) => A i x) g l := sorry theorem is_o.sum {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {g' : α → F'} {l : filter α} {ι : Type u_13} {A : ι → α → E'} {s : finset ι} (h : ∀ (i : ι), i ∈ s → is_o (A i) g' l) : is_o (fun (x : α) => finset.sum s fun (i : ι) => A i x) g' l := sorry /-! ### Relation between `f = o(g)` and `f / g → 0` -/ theorem is_o.tendsto_0 {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] {f : α → 𝕜} {g : α → 𝕜} {l : filter α} (h : is_o f g l) : filter.tendsto (fun (x : α) => f x / g x) l (nhds 0) := sorry theorem is_o_iff_tendsto' {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] {f : α → 𝕜} {g : α → 𝕜} {l : filter α} (hgf : filter.eventually (fun (x : α) => g x = 0 → f x = 0) l) : is_o f g l ↔ filter.tendsto (fun (x : α) => f x / g x) l (nhds 0) := sorry theorem is_o_iff_tendsto {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] {f : α → 𝕜} {g : α → 𝕜} {l : filter α} (hgf : ∀ (x : α), g x = 0 → f x = 0) : is_o f g l ↔ filter.tendsto (fun (x : α) => f x / g x) l (nhds 0) := { mp := fun (h : is_o f g l) => is_o.tendsto_0 h, mpr := iff.mpr (is_o_iff_tendsto' (filter.eventually_of_forall hgf)) } theorem is_o_of_tendsto' {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] {f : α → 𝕜} {g : α → 𝕜} {l : filter α} (hgf : filter.eventually (fun (x : α) => g x = 0 → f x = 0) l) : filter.tendsto (fun (x : α) => f x / g x) l (nhds 0) → is_o f g l := iff.mpr (is_o_iff_tendsto' hgf) theorem is_o_of_tendsto {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] {f : α → 𝕜} {g : α → 𝕜} {l : filter α} (hgf : ∀ (x : α), g x = 0 → f x = 0) : filter.tendsto (fun (x : α) => f x / g x) l (nhds 0) → is_o f g l := iff.mpr (is_o_iff_tendsto hgf) /-! ### Eventually (u / v) * v = u If `u` and `v` are linked by an `is_O_with` relation, then we eventually have `(u / v) * v = u`, even if `v` vanishes. -/ theorem is_O_with.eventually_mul_div_cancel {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] {c : ℝ} {l : filter α} {u : α → 𝕜} {v : α → 𝕜} (h : is_O_with c u v l) : filter.eventually_eq l (u / v * v) u := sorry /-- If `u = O(v)` along `l`, then `(u / v) * v = u` eventually at `l`. -/ theorem is_O.eventually_mul_div_cancel {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] {l : filter α} {u : α → 𝕜} {v : α → 𝕜} (h : is_O u v l) : filter.eventually_eq l (u / v * v) u := sorry /-- If `u = o(v)` along `l`, then `(u / v) * v = u` eventually at `l`. -/ theorem is_o.eventually_mul_div_cancel {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] {l : filter α} {u : α → 𝕜} {v : α → 𝕜} (h : is_o u v l) : filter.eventually_eq l (u / v * v) u := is_O_with.eventually_mul_div_cancel (h zero_lt_one) /-! ### Equivalent definitions of the form `∃ φ, u =ᶠ[l] φ * v` in a `normed_field`. -/ /-- If `∥φ∥` is eventually bounded by `c`, and `u =ᶠ[l] φ * v`, then we have `is_O_with c u v l`. This does not require any assumptions on `c`, which is why we keep this version along with `is_O_with_iff_exists_eq_mul`. -/ theorem is_O_with_of_eq_mul {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] {c : ℝ} {l : filter α} {u : α → 𝕜} {v : α → 𝕜} (φ : α → 𝕜) (hφ : filter.eventually (fun (x : α) => norm (φ x) ≤ c) l) (h : filter.eventually_eq l u (φ * v)) : is_O_with c u v l := sorry theorem is_O_with_iff_exists_eq_mul {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] {c : ℝ} {l : filter α} {u : α → 𝕜} {v : α → 𝕜} (hc : 0 ≤ c) : is_O_with c u v l ↔ ∃ (φ : α → 𝕜), ∃ (hφ : filter.eventually (fun (x : α) => norm (φ x) ≤ c) l), filter.eventually_eq l u (φ * v) := sorry theorem is_O_with.exists_eq_mul {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] {c : ℝ} {l : filter α} {u : α → 𝕜} {v : α → 𝕜} (h : is_O_with c u v l) (hc : 0 ≤ c) : ∃ (φ : α → 𝕜), ∃ (hφ : filter.eventually (fun (x : α) => norm (φ x) ≤ c) l), filter.eventually_eq l u (φ * v) := iff.mp (is_O_with_iff_exists_eq_mul hc) h theorem is_O_iff_exists_eq_mul {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] {l : filter α} {u : α → 𝕜} {v : α → 𝕜} : is_O u v l ↔ ∃ (φ : α → 𝕜), ∃ (hφ : filter.is_bounded_under LessEq l (norm ∘ φ)), filter.eventually_eq l u (φ * v) := sorry theorem is_O.exists_eq_mul {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] {l : filter α} {u : α → 𝕜} {v : α → 𝕜} : is_O u v l → ∃ (φ : α → 𝕜), ∃ (hφ : filter.is_bounded_under LessEq l (norm ∘ φ)), filter.eventually_eq l u (φ * v) := iff.mp is_O_iff_exists_eq_mul theorem is_o_iff_exists_eq_mul {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] {l : filter α} {u : α → 𝕜} {v : α → 𝕜} : is_o u v l ↔ ∃ (φ : α → 𝕜), ∃ (hφ : filter.tendsto φ l (nhds 0)), filter.eventually_eq l u (φ * v) := sorry theorem is_o.exists_eq_mul {α : Type u_1} {𝕜 : Type u_11} [normed_field 𝕜] {l : filter α} {u : α → 𝕜} {v : α → 𝕜} : is_o u v l → ∃ (φ : α → 𝕜), ∃ (hφ : filter.tendsto φ l (nhds 0)), filter.eventually_eq l u (φ * v) := iff.mp is_o_iff_exists_eq_mul /-! ### Miscellanous lemmas -/ theorem is_o_pow_pow {𝕜 : Type u_11} [normed_field 𝕜] {m : ℕ} {n : ℕ} (h : m < n) : is_o (fun (x : 𝕜) => x ^ n) (fun (x : 𝕜) => x ^ m) (nhds 0) := sorry theorem is_o_norm_pow_norm_pow {E' : Type u_6} [normed_group E'] {m : ℕ} {n : ℕ} (h : m < n) : is_o (fun (x : E') => norm x ^ n) (fun (x : E') => norm x ^ m) (nhds 0) := is_o.comp_tendsto (is_o_pow_pow h) tendsto_norm_zero theorem is_o_pow_id {𝕜 : Type u_11} [normed_field 𝕜] {n : ℕ} (h : 1 < n) : is_o (fun (x : 𝕜) => x ^ n) (fun (x : 𝕜) => x) (nhds 0) := sorry theorem is_o_norm_pow_id {E' : Type u_6} [normed_group E'] {n : ℕ} (h : 1 < n) : is_o (fun (x : E') => norm x ^ n) (fun (x : E') => x) (nhds 0) := sorry theorem is_O_with.right_le_sub_of_lt_1 {α : Type u_1} {E' : Type u_6} [normed_group E'] {c : ℝ} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} (h : is_O_with c f₁ f₂ l) (hc : c < 1) : is_O_with (1 / (1 - c)) f₂ (fun (x : α) => f₂ x - f₁ x) l := sorry theorem is_O_with.right_le_add_of_lt_1 {α : Type u_1} {E' : Type u_6} [normed_group E'] {c : ℝ} {l : filter α} {f₁ : α → E'} {f₂ : α → E'} (h : is_O_with c f₁ f₂ l) (hc : c < 1) : is_O_with (1 / (1 - c)) f₂ (fun (x : α) => f₁ x + f₂ x) l := sorry theorem is_o.right_is_O_sub {α : Type u_1} {E' : Type u_6} [normed_group E'] {l : filter α} {f₁ : α → E'} {f₂ : α → E'} (h : is_o f₁ f₂ l) : is_O f₂ (fun (x : α) => f₂ x - f₁ x) l := is_O_with.is_O (is_O_with.right_le_sub_of_lt_1 (is_o.def' h one_half_pos) one_half_lt_one) theorem is_o.right_is_O_add {α : Type u_1} {E' : Type u_6} [normed_group E'] {l : filter α} {f₁ : α → E'} {f₂ : α → E'} (h : is_o f₁ f₂ l) : is_O f₂ (fun (x : α) => f₁ x + f₂ x) l := is_O_with.is_O (is_O_with.right_le_add_of_lt_1 (is_o.def' h one_half_pos) one_half_lt_one) /-- If `f x = O(g x)` along `cofinite`, then there exists a positive constant `C` such that `∥f x∥ ≤ C * ∥g x∥` whenever `g x ≠ 0`. -/ theorem bound_of_is_O_cofinite {α : Type u_1} {E : Type u_3} {F' : Type u_7} [has_norm E] [normed_group F'] {f : α → E} {g' : α → F'} (h : is_O f g' filter.cofinite) : ∃ (C : ℝ), ∃ (H : C > 0), ∀ {x : α}, g' x ≠ 0 → norm (f x) ≤ C * norm (g' x) := sorry theorem is_O_cofinite_iff {α : Type u_1} {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f' : α → E'} {g' : α → F'} (h : ∀ (x : α), g' x = 0 → f' x = 0) : is_O f' g' filter.cofinite ↔ ∃ (C : ℝ), ∀ (x : α), norm (f' x) ≤ C * norm (g' x) := sorry theorem bound_of_is_O_nat_at_top {E : Type u_3} {E' : Type u_6} [has_norm E] [normed_group E'] {f : ℕ → E} {g' : ℕ → E'} (h : is_O f g' filter.at_top) : ∃ (C : ℝ), ∃ (H : C > 0), ∀ {x : ℕ}, g' x ≠ 0 → norm (f x) ≤ C * norm (g' x) := sorry theorem is_O_nat_at_top_iff {E' : Type u_6} {F' : Type u_7} [normed_group E'] [normed_group F'] {f : ℕ → E'} {g : ℕ → F'} (h : ∀ (x : ℕ), g x = 0 → f x = 0) : is_O f g filter.at_top ↔ ∃ (C : ℝ), ∀ (x : ℕ), norm (f x) ≤ C * norm (g x) := sorry theorem is_O_one_nat_at_top_iff {E' : Type u_6} [normed_group E'] {f : ℕ → E'} : is_O f (fun (n : ℕ) => 1) filter.at_top ↔ ∃ (C : ℝ), ∀ (n : ℕ), norm (f n) ≤ C := sorry end asymptotics theorem summable_of_is_O {ι : Type u_1} {E : Type u_2} [normed_group E] [complete_space E] {f : ι → E} (g : ι → ℝ) (hg : summable g) (h : asymptotics.is_O f g filter.cofinite) : summable f := sorry theorem summable_of_is_O_nat {E : Type u_1} [normed_group E] [complete_space E] {f : ℕ → E} (g : ℕ → ℝ) (hg : summable g) (h : asymptotics.is_O f g filter.at_top) : summable f := summable_of_is_O g hg (Eq.symm nat.cofinite_eq_at_top ▸ h) namespace local_homeomorph /-- Transfer `is_O_with` over a `local_homeomorph`. -/ theorem is_O_with_congr {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {E : Type u_3} [has_norm E] {F : Type u_4} [has_norm F] (e : local_homeomorph α β) {b : β} (hb : b ∈ local_equiv.target (to_local_equiv e)) {f : β → E} {g : β → F} {C : ℝ} : asymptotics.is_O_with C f g (nhds b) ↔ asymptotics.is_O_with C (f ∘ ⇑e) (g ∘ ⇑e) (nhds (coe_fn (local_homeomorph.symm e) b)) := sorry /-- Transfer `is_O` over a `local_homeomorph`. -/ theorem is_O_congr {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {E : Type u_3} [has_norm E] {F : Type u_4} [has_norm F] (e : local_homeomorph α β) {b : β} (hb : b ∈ local_equiv.target (to_local_equiv e)) {f : β → E} {g : β → F} : asymptotics.is_O f g (nhds b) ↔ asymptotics.is_O (f ∘ ⇑e) (g ∘ ⇑e) (nhds (coe_fn (local_homeomorph.symm e) b)) := exists_congr fun (C : ℝ) => is_O_with_congr e hb /-- Transfer `is_o` over a `local_homeomorph`. -/ theorem is_o_congr {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {E : Type u_3} [has_norm E] {F : Type u_4} [has_norm F] (e : local_homeomorph α β) {b : β} (hb : b ∈ local_equiv.target (to_local_equiv e)) {f : β → E} {g : β → F} : asymptotics.is_o f g (nhds b) ↔ asymptotics.is_o (f ∘ ⇑e) (g ∘ ⇑e) (nhds (coe_fn (local_homeomorph.symm e) b)) := forall_congr fun (c : ℝ) => forall_congr fun (hc : 0 < c) => is_O_with_congr e hb end local_homeomorph namespace homeomorph /-- Transfer `is_O_with` over a `homeomorph`. -/ theorem is_O_with_congr {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {E : Type u_3} [has_norm E] {F : Type u_4} [has_norm F] (e : α ≃ₜ β) {b : β} {f : β → E} {g : β → F} {C : ℝ} : asymptotics.is_O_with C f g (nhds b) ↔ asymptotics.is_O_with C (f ∘ ⇑e) (g ∘ ⇑e) (nhds (coe_fn (homeomorph.symm e) b)) := local_homeomorph.is_O_with_congr (to_local_homeomorph e) trivial /-- Transfer `is_O` over a `homeomorph`. -/ theorem is_O_congr {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {E : Type u_3} [has_norm E] {F : Type u_4} [has_norm F] (e : α ≃ₜ β) {b : β} {f : β → E} {g : β → F} : asymptotics.is_O f g (nhds b) ↔ asymptotics.is_O (f ∘ ⇑e) (g ∘ ⇑e) (nhds (coe_fn (homeomorph.symm e) b)) := exists_congr fun (C : ℝ) => is_O_with_congr e /-- Transfer `is_o` over a `homeomorph`. -/ theorem is_o_congr {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {E : Type u_3} [has_norm E] {F : Type u_4} [has_norm F] (e : α ≃ₜ β) {b : β} {f : β → E} {g : β → F} : asymptotics.is_o f g (nhds b) ↔ asymptotics.is_o (f ∘ ⇑e) (g ∘ ⇑e) (nhds (coe_fn (homeomorph.symm e) b)) := forall_congr fun (c : ℝ) => forall_congr fun (hc : 0 < c) => is_O_with_congr e end Mathlib
bf789bbcff2ebbee0e92f9a150113c3421c7af49
83bd3c3824dd952c0fef702bace6c34c78226af8
/library/system/io.lean
00cda83107ed6234a87938f78ae5b514c76cee58
[ "Apache-2.0" ]
permissive
heruix/lean
cb0767295dc8cdfa2892601f04d3954606e81e03
39270fd46f49fecb30649f5ec527da7bbd4cdb13
refs/heads/master
1,611,580,745,403
1,519,814,958,000
1,519,843,130,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,480
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Luke Nelson, Jared Roesch and Leonardo de Moura -/ import system.io_interface /- The following constants have a builtin implementation -/ constant io_core : Type → Type → Type /- Auxiliary definition used in the builtin implementation of monad_io_random_impl -/ def io_rand_nat : std_gen → nat → nat → nat × std_gen := rand_nat @[instance] constant monad_io_impl : monad_io io_core @[instance] constant monad_io_terminal_impl : monad_io_terminal io_core @[instance] constant monad_io_file_system_impl : monad_io_file_system io_core @[instance] constant monad_io_environment_impl : monad_io_environment io_core @[instance] constant monad_io_process_impl : monad_io_process io_core @[instance] constant monad_io_random_impl : monad_io_random io_core instance io_core_is_monad (e : Type) : monad (io_core e) := monad_io_is_monad io_core e instance io_core_is_monad_fail : monad_fail (io_core io.error) := monad_io_is_monad_fail io_core instance io_core_is_alternative : alternative (io_core io.error) := monad_io_is_alternative io_core @[reducible] def io (α : Type) := io_core io.error α namespace io /- Remark: the following definitions can be generalized and defined for any (m : Type -> Type -> Type) that implements the required type classes. However, the generalized versions are very inconvenient to use, (example: `#eval io.put_str "hello world"` does not work because we don't have enough information to infer `m`.). -/ def iterate {e α} (a : α) (f : α → io_core e (option α)) : io_core e α := monad_io.iterate e α a f def forever {e} (a : io_core e unit) : io_core e unit := iterate () $ λ _, a >> return (some ()) -- TODO(Leo): delete after we merge #1881 def catch {e₁ e₂ α} (a : io_core e₁ α) (b : e₁ → io_core e₂ α) : io_core e₂ α := monad_io.catch e₁ e₂ α a b def finally {α e} (a : io_core e α) (cleanup : io_core e unit) : io_core e α := do res ← catch (sum.inr <$> a) (return ∘ sum.inl), cleanup, match res with | sum.inr res := return res | sum.inl error := monad_io.fail _ _ _ error end protected def fail {α : Type} (s : string) : io α := monad_io.fail io_core _ _ (io.error.other s) def put_str : string → io unit := monad_io_terminal.put_str io_core def put_str_ln (s : string) : io unit := put_str s >> put_str "\n" def get_line : io string := monad_io_terminal.get_line io_core def cmdline_args : io (list string) := return (monad_io_terminal.cmdline_args io_core) def print {α} [has_to_string α] (s : α) : io unit := put_str ∘ to_string $ s def print_ln {α} [has_to_string α] (s : α) : io unit := print s >> put_str "\n" def handle : Type := monad_io.handle io_core def mk_file_handle (s : string) (m : mode) (bin : bool := ff) : io handle := monad_io_file_system.mk_file_handle io_core s m bin def stdin : io handle := monad_io_file_system.stdin io_core def stderr : io handle := monad_io_file_system.stderr io_core def stdout : io handle := monad_io_file_system.stdout io_core namespace env def get (env_var : string) : io (option string) := monad_io_environment.get_env io_core env_var /-- get the current working directory -/ def get_cwd : io string := monad_io_environment.get_cwd io_core /-- set the current working directory -/ def set_cwd (cwd : string) : io unit := monad_io_environment.set_cwd io_core cwd end env namespace fs def is_eof : handle → io bool := monad_io_file_system.is_eof def flush : handle → io unit := monad_io_file_system.flush def close : handle → io unit := monad_io_file_system.close def read : handle → nat → io char_buffer := monad_io_file_system.read def write : handle → char_buffer → io unit := monad_io_file_system.write def get_char (h : handle) : io char := do b ← read h 1, if h : b.size = 1 then return $ b.read ⟨0, h.symm ▸ zero_lt_one⟩ else io.fail "get_char failed" def get_line : handle → io char_buffer := monad_io_file_system.get_line def put_char (h : handle) (c : char) : io unit := write h (mk_buffer.push_back c) def put_str (h : handle) (s : string) : io unit := write h (mk_buffer.append_string s) def put_str_ln (h : handle) (s : string) : io unit := put_str h s >> put_str h "\n" def read_to_end (h : handle) : io char_buffer := iterate mk_buffer $ λ r, do done ← is_eof h, if done then return none else do c ← read h 1024, return $ some (r ++ c) def read_file (s : string) (bin := ff) : io char_buffer := do h ← mk_file_handle s io.mode.read bin, read_to_end h end fs namespace proc def child : Type := monad_io_process.child io_core def child.stdin : child → handle := monad_io_process.stdin def child.stdout : child → handle := monad_io_process.stdout def child.stderr : child → handle := monad_io_process.stderr def spawn (p : io.process.spawn_args) : io child := monad_io_process.spawn io_core p def wait (c : child) : io nat := monad_io_process.wait c end proc def set_rand_gen : std_gen → io unit := monad_io_random.set_rand_gen io_core def rand (lo : nat := std_range.1) (hi : nat := std_range.2) : io nat := monad_io_random.rand io_core lo hi end io meta constant format.print_using : format → options → io unit meta definition format.print (fmt : format) : io unit := format.print_using fmt options.mk meta definition pp_using {α : Type} [has_to_format α] (a : α) (o : options) : io unit := format.print_using (to_fmt a) o meta definition pp {α : Type} [has_to_format α] (a : α) : io unit := format.print (to_fmt a) /-- Run the external process specified by `args`. The process will run to completion with its output captured by a pipe, and read into `string` which is then returned. -/ def io.cmd (args : io.process.spawn_args) : io string := do child ← io.proc.spawn { stdout := io.process.stdio.piped, ..args }, buf ← io.fs.read_to_end child.stdout, io.fs.close child.stdout, exitv ← io.proc.wait child, when (exitv ≠ 0) $ io.fail $ "process exited with status " ++ repr exitv, return buf.to_string /-- This is the "back door" into the `io` monad, allowing IO computation to be performed during tactic execution. For this to be safe, the IO computation should be ideally free of side effects and independent of its environment. This primitive is used to invoke external tools (e.g., SAT and SMT solvers) from a tactic. IMPORTANT: this primitive can be used to implement `unsafe_perform_io {α : Type} : io α → option α` or `unsafe_perform_io {α : Type} [inhabited α] : io α → α`. This can be accomplished by executing the resulting tactic using an empty `tactic_state` (we have `tactic_state.mk_empty`). If `unsafe_perform_io` is defined, and used to perform side-effects, users need to take the following precautions: - Use `@[noinline]` attribute in any function to invokes `tactic.unsafe_perform_io`. Reason: if the call is inlined, the IO may be performed more than once. - Set `set_option compiler.cse false` before any function that invokes `tactic.unsafe_perform_io`. This option disables common subexpression elimination. Common subexpression elimination might combine two side effects that were meant to be separate. TODO[Leo]: add `[noinline]` attribute and option `compiler.cse`. -/ meta constant tactic.unsafe_run_io {α : Type} : io α → tactic α
37785ad8b93baa45b7751222be92d27e42c864b1
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/t5.lean
904967a8130d3edf225186df8c9d4e095cc48cf4
[ "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
330
lean
constant N : Type.{1} namespace foo constant N : Type.{2} namespace tst constant N : Type.{3} #print raw N end tst end foo #print raw N namespace foo #print raw N namespace tst #print raw N N -> N section variable N : Type.{4} -- Shadow previous ones. #print raw N end end tst end foo
e0cf57cc938925361f1c1532cf5e57b83ee5ad24
46125763b4dbf50619e8846a1371029346f4c3db
/src/algebra/group/hom.lean
2353d1f0bca7caa3e081313dff9f26d3c10f299e
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
8,719
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Kevin Buzzard, Scott Morrison, Johan Commelin, Chris Hughes, Johannes Hölzl, Yury Kudryashov -/ import algebra.group.to_additive algebra.group.basic /-! # monoid and group homomorphisms This file defines the bundled structures for monoid and group homomorphisms. Namely, we define `monoid_hom` (resp., `add_monoid_hom`) to be bundled homomorphisms between multiplicative (resp., additive) monoids or groups. We also define coercion to a function, and usual operations: composition, identity homomorphism, pointwise multiplication and pointwise inversion. ## Notations * `→*` for bundled monoid homs (also use for group homs) * `→+` for bundled add_monoid homs (also use for add_group homs) ## implementation notes There's a coercion from bundled homs to fun, and the canonical notation is to use the bundled hom as a function via this coercion. There is no `group_hom` -- the idea is that `monoid_hom` is used. The constructor for `monoid_hom` needs a proof of `map_one` as well as `map_mul`; a separate constructor `monoid_hom.mk'` will construct group homs (i.e. monoid homs between groups) given only a proof that multiplication is preserved, Implicit `{}` brackets are often used instead of type class `[]` brackets. This is done when the instances can be inferred because they are implicit arguments to the type `monoid_hom`. When they can be inferred from the type it is faster to use this method than to use type class inference. Historically this file also included definitions of unbundled homomorphism classes; they were deprecated and moved to `deprecated/group`. ## Tags monoid_hom, add_monoid_hom -/ variables {M : Type*} {N : Type*} {P : Type*} -- monoids {G : Type*} {H : Type*} -- groups /-- Bundled add_monoid homomorphisms; use this for bundled add_group homomorphisms too. -/ structure add_monoid_hom (M : Type*) (N : Type*) [add_monoid M] [add_monoid N] := (to_fun : M → N) (map_zero' : to_fun 0 = 0) (map_add' : ∀ x y, to_fun (x + y) = to_fun x + to_fun y) infixr ` →+ `:25 := add_monoid_hom /-- Bundled monoid homomorphisms; use this for bundled group homomorphisms too. -/ @[to_additive add_monoid_hom] structure monoid_hom (M : Type*) (N : Type*) [monoid M] [monoid N] := (to_fun : M → N) (map_one' : to_fun 1 = 1) (map_mul' : ∀ x y, to_fun (x * y) = to_fun x * to_fun y) infixr ` →* `:25 := monoid_hom @[to_additive] instance {M : Type*} {N : Type*} {mM : monoid M} {mN : monoid N} : has_coe_to_fun (M →* N) := ⟨_, monoid_hom.to_fun⟩ namespace monoid_hom variables {mM : monoid M} {mN : monoid N} {mP : monoid P} variables [group G] [comm_group H] include mM mN @[simp, to_additive] lemma coe_mk (f : M → N) (h1 hmul) : ⇑(monoid_hom.mk f h1 hmul) = f := rfl @[to_additive] lemma coe_inj ⦃f g : M →* N⦄ (h : (f : M → N) = g) : f = g := by cases f; cases g; cases h; refl @[ext, to_additive] lemma ext ⦃f g : M →* N⦄ (h : ∀ x, f x = g x) : f = g := coe_inj (funext h) @[to_additive] lemma ext_iff {f g : M →* N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ /-- If f is a monoid homomorphism then f 1 = 1. -/ @[simp, to_additive] lemma map_one (f : M →* N) : f 1 = 1 := f.map_one' /-- If f is a monoid homomorphism then f (a * b) = f a * f b. -/ @[simp, to_additive] lemma map_mul (f : M →* N) (a b : M) : f (a * b) = f a * f b := f.map_mul' a b omit mN mM /-- The identity map from a monoid to itself. -/ @[to_additive] def id (M : Type*) [monoid M] : M →* M := { to_fun := id, map_one' := rfl, map_mul' := λ _ _, rfl } include mM mN mP /-- Composition of monoid morphisms is a monoid morphism. -/ @[to_additive] def comp (hnp : N →* P) (hmn : M →* N) : M →* P := { to_fun := hnp ∘ hmn, map_one' := by simp, map_mul' := by simp } @[simp, to_additive] lemma comp_apply (g : N →* P) (f : M →* N) (x : M) : g.comp f x = g (f x) := rfl /-- Composition of monoid homomorphisms is associative. -/ @[to_additive] lemma comp_assoc {Q : Type*} [monoid Q] (f : M →* N) (g : N →* P) (h : P →* Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl /-- Given a monoid homomorphism `f : M →* N` and a set `S ⊆ M` such that `f` maps elements of `S` to invertible elements of `N`, any monoid homomorphism `g : N →* P` maps elements of `f(S)` to invertible elements of `P`. -/ @[to_additive "Given an add_monoid homomorphism `f : M →+ N` and a set `S ⊆ M` such that `f` maps elements of `S` to invertible elements of `N`, any add_monoid homomorphism `g : N →+ P` maps elements of `f(S)` to invertible elements of `P`."] lemma exists_inv_of_comp_exists_inv {S : set M} {f : M →* N} (hf : ∀ s ∈ S, ∃ b, f s * b = 1) (g : N →* P) (s ∈ S) : ∃ x : P, g.comp f s * x = 1 := let ⟨c, hc⟩ := hf s H in ⟨g c, show g _ * _ = _, by rw [←g.map_mul, hc, g.map_one]⟩ @[to_additive] lemma cancel_right {g₁ g₂ : N →* P} {f : M →* N} (hf : function.surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, monoid_hom.ext $ (forall_iff_forall_surj hf).1 (ext_iff.1 h), λ h, h ▸ rfl⟩ @[to_additive] lemma cancel_left {g : N →* P} {f₁ f₂ : M →* N} (hg : function.injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, monoid_hom.ext $ λ x, hg $ by rw [← comp_apply, h, comp_apply], λ h, h ▸ rfl⟩ omit mP variables [mM] [mN] @[to_additive] protected def one : M →* N := { to_fun := λ _, 1, map_one' := rfl, map_mul' := λ _ _, (one_mul 1).symm } @[to_additive] instance : has_one (M →* N) := ⟨monoid_hom.one⟩ @[to_additive] instance : inhabited (M →* N) := ⟨1⟩ omit mM mN /-- The product of two monoid morphisms is a monoid morphism if the target is commutative. -/ @[to_additive] protected def mul {M N} {mM : monoid M} [comm_monoid N] (f g : M →* N) : M →* N := { to_fun := λ m, f m * g m, map_one' := show f 1 * g 1 = 1, by simp, map_mul' := begin intros, show f (x * y) * g (x * y) = f x * g x * (f y * g y), rw [f.map_mul, g.map_mul, ←mul_assoc, ←mul_assoc, mul_right_comm (f x)], end } @[to_additive] instance {M N} {mM : monoid M} [comm_monoid N] : has_mul (M →* N) := ⟨monoid_hom.mul⟩ /-- (M →* N) is a comm_monoid if N is commutative. -/ @[to_additive add_comm_monoid] instance {M N} [monoid M] [comm_monoid N] : comm_monoid (M →* N) := { mul := (*), mul_assoc := by intros; ext; apply mul_assoc, one := 1, one_mul := by intros; ext; apply one_mul, mul_one := by intros; ext; apply mul_one, mul_comm := by intros; ext; apply mul_comm } /-- Group homomorphisms preserve inverse. -/ @[simp, to_additive] theorem map_inv {G H} [group G] [group H] (f : G →* H) (g : G) : f g⁻¹ = (f g)⁻¹ := eq_inv_of_mul_eq_one $ by rw [←f.map_mul, inv_mul_self, f.map_one] /-- Group homomorphisms preserve division. -/ @[simp, to_additive] theorem map_mul_inv {G H} [group G] [group H] (f : G →* H) (g h : G) : f (g * h⁻¹) = (f g) * (f h)⁻¹ := by rw [f.map_mul, f.map_inv] /-- A group homomorphism is injective iff its kernel is trivial. -/ @[to_additive] lemma injective_iff {G H} [group G] [group H] (f : G →* H) : function.injective f ↔ (∀ a, f a = 1 → a = 1) := ⟨λ h _, by rw ← f.map_one; exact @h _ _, λ h x y hxy, by rw [← inv_inv (f x), inv_eq_iff_mul_eq_one, ← f.map_inv, ← f.map_mul] at hxy; simpa using inv_eq_of_mul_eq_one (h _ hxy)⟩ include mM /-- Makes a group homomomorphism from a proof that the map preserves multiplication. -/ @[to_additive] def mk' (f : M → G) (map_mul : ∀ a b : M, f (a * b) = f a * f b) : M →* G := { to_fun := f, map_mul' := map_mul, map_one' := mul_self_iff_eq_one.1 $ by rw [←map_mul, mul_one] } omit mM /-- The inverse of a monoid homomorphism is a monoid homomorphism if the target is a commutative group.-/ @[to_additive] protected def inv {M G} {mM : monoid M} [comm_group G] (f : M →* G) : M →* G := mk' (λ g, (f g)⁻¹) $ λ a b, by rw [←mul_inv, f.map_mul] @[to_additive] instance {M G} [monoid M] [comm_group G] : has_inv (M →* G) := ⟨monoid_hom.inv⟩ /-- (M →* G) is a comm_group if G is a comm_group -/ @[to_additive add_comm_group] instance {M G} [monoid M] [comm_group G] : comm_group (M →* G) := { inv := has_inv.inv, mul_left_inv := by intros; ext; apply mul_left_inv, ..monoid_hom.comm_monoid } end monoid_hom /-- Additive group homomorphisms preserve subtraction. -/ @[simp] theorem add_monoid_hom.map_sub {G H} [add_group G] [add_group H] (f : G →+ H) (g h : G) : f (g - h) = (f g) - (f h) := f.map_add_neg g h
96388db9f6b3c695ebfc2fe46e150c0b1d3b0a6e
cabd1ea95170493667c024ef2045eb86d981b133
/test/super_examples.lean
b7456221084fda1197c98f7a533d473a0a8abb2f
[]
no_license
semorrison/super
31db4b5aa5ef4c2313dc5803b8c79a95f809815b
0c6c03ba9c7470f801eb4d055294f424ff090308
refs/heads/master
1,630,272,140,541
1,511,054,739,000
1,511,054,756,000
114,317,807
0
0
null
1,513,304,776,000
1,513,304,775,000
null
UTF-8
Lean
false
false
2,767
lean
import super open tactic def prime (n : ℕ) := ∀d, d ∣ n → d = 1 ∨ d = n axiom nat_mul_cancel_one (m n : ℕ) : m = m * n → n = 1 example {m n : ℕ} : prime (m * n) → m = 1 ∨ n = 1 := by super with dvd_refl dvd_mul_of_dvd_left nat_mul_cancel_one example : nat.zero ≠ nat.succ nat.zero := by super example (x y : ℕ) : nat.succ x = nat.succ y → x = y := by super example (i) (a b c : i) : [a,b,c] = [b,c,a] -> a = b ∧ b = c := by super definition is_positive (n : ℕ) := n > 0 example (n : ℕ) : n > 0 ↔ is_positive n := by super example (m n : ℕ) : 0 + m = 0 + n → m = n := by super with nat.zero_add example : ∀x y : ℕ, x + y = y + x := begin intros, induction x, have h : nat.zero = 0 := rfl, super with nat.add_zero nat.zero_add, super with nat.add_succ nat.succ_add end example (i) [inhabited i] : nonempty i := by super example (i) [nonempty i] : ¬(inhabited i → false) := by super example : nonempty ℕ := by super example : ¬(inhabited ℕ → false) := by super example {a b} : ¬(b ∨ ¬a) ∨ (a → b) := by super example {a} : a ∨ ¬a := by super example {a} : (a ∧ a) ∨ (¬a ∧ ¬a) := by super example (i) (c : i) (p : i → Prop) (f : i → i) : p c → (∀x, p x → p (f x)) → p (f (f (f c))) := by super example (i) (p : i → Prop) : ∀x, p x → ∃x, p x := by super example (i) [nonempty i] (p : i → i → Prop) : (∀x y, p x y) → ∃x, ∀z, p x z := by super example (i) [nonempty i] (p : i → Prop) : (∀x, p x) → ¬¬∀x, p x := by super -- Requires non-empty domain. example {i} [nonempty i] (p : i → Prop) : (∀x y, p x ∨ p y) → ∃x y, p x ∧ p y := by super example (i) (a b : i) (p : i → Prop) (H : a = b) : p b → p a := by super example (i) (a b : i) (p : i → Prop) (H : a = b) : p a → p b := by super example (i) (a b : i) (p : i → Prop) (H : a = b) : p b = p a := by super example (i) (c : i) (p : i → Prop) (f g : i → i) : p c → (∀x, p x → p (f x)) → (∀x, p x → f x = g x) → f (f c) = g (g c) := by super example (i) (p q : i → i → Prop) (a b c d : i) : (∀x y z, p x y ∧ p y z → p x z) → (∀x y z, q x y ∧ q y z → q x z) → (∀x y, q x y → q y x) → (∀x y, p x y ∨ q x y) → p a b ∨ q c d := by super -- This example from Davis-Putnam actually requires a non-empty domain example (i) [nonempty i] (f g : i → i → Prop) : ∃x y, ∀z, (f x y → f y z ∧ f z z) ∧ (f x y ∧ g x y → g x z ∧ g z z) := by super example (person) [nonempty person] (drinks : person → Prop) : ∃canary, drinks canary → ∀other, drinks other := by super example {p q : ℕ → Prop} {r} : (∀x y, p x ∧ q y ∧ r) -> ∀x, (p x ∧ r ∧ q x) := by super
0542999c9bbaefc03ff35ce0cdb228869b6b64e2
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/monoid_algebra/basic.lean
b0df6344a58e8b21f4ecdc108ad17f3652d65c1c
[ "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
66,406
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury G. Kudryashov, Scott Morrison -/ import algebra.algebra.equiv import algebra.big_operators.finsupp import algebra.hom.non_unital_alg import algebra.module.big_operators import linear_algebra.finsupp /-! # Monoid algebras > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. When the domain of a `finsupp` has a multiplicative or additive structure, we can define a convolution product. To mathematicians this structure is known as the "monoid algebra", i.e. the finite formal linear combinations over a given semiring of elements of the monoid. The "group ring" ℤ[G] or the "group algebra" k[G] are typical uses. In fact the construction of the "monoid algebra" makes sense when `G` is not even a monoid, but merely a magma, i.e., when `G` carries a multiplication which is not required to satisfy any conditions at all. In this case the construction yields a not-necessarily-unital, not-necessarily-associative algebra but it is still adjoint to the forgetful functor from such algebras to magmas, and we prove this as `monoid_algebra.lift_magma`. In this file we define `monoid_algebra k G := G →₀ k`, and `add_monoid_algebra k G` in the same way, and then define the convolution product on these. When the domain is additive, this is used to define polynomials: ``` polynomial α := add_monoid_algebra ℕ α mv_polynomial σ α := add_monoid_algebra (σ →₀ ℕ) α ``` When the domain is multiplicative, e.g. a group, this will be used to define the group ring. ## Implementation note Unfortunately because additive and multiplicative structures both appear in both cases, it doesn't appear to be possible to make much use of `to_additive`, and we just settle for saying everything twice. Similarly, I attempted to just define `add_monoid_algebra k G := monoid_algebra k (multiplicative G)`, but the definitional equality `multiplicative G = G` leaks through everywhere, and seems impossible to use. -/ noncomputable theory open_locale big_operators open finset finsupp universes u₁ u₂ u₃ variables (k : Type u₁) (G : Type u₂) {R : Type*} /-! ### Multiplicative monoids -/ section variables [semiring k] /-- The monoid algebra over a semiring `k` generated by the monoid `G`. It is the type of finite formal `k`-linear combinations of terms of `G`, endowed with the convolution product. -/ @[derive [inhabited, add_comm_monoid]] def monoid_algebra : Type (max u₁ u₂) := G →₀ k instance : has_coe_to_fun (monoid_algebra k G) (λ _, G → k) := finsupp.has_coe_to_fun end namespace monoid_algebra variables {k G} section variables [semiring k] [non_unital_non_assoc_semiring R] /-- A non-commutative version of `monoid_algebra.lift`: given a additive homomorphism `f : k →+ R` and a homomorphism `g : G → R`, returns the additive homomorphism from `monoid_algebra k G` such that `lift_nc f g (single a b) = f b * g a`. If `f` is a ring homomorphism and the range of either `f` or `g` is in center of `R`, then the result is a ring homomorphism. If `R` is a `k`-algebra and `f = algebra_map k R`, then the result is an algebra homomorphism called `monoid_algebra.lift`. -/ def lift_nc (f : k →+ R) (g : G → R) : monoid_algebra k G →+ R := lift_add_hom (λ x : G, (add_monoid_hom.mul_right (g x)).comp f) @[simp] lemma lift_nc_single (f : k →+ R) (g : G → R) (a : G) (b : k) : lift_nc f g (single a b) = f b * g a := lift_add_hom_apply_single _ _ _ end section has_mul variables [semiring k] [has_mul G] /-- The product of `f g : monoid_algebra k G` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x * y = a`. (Think of the group ring of a group.) -/ instance : has_mul (monoid_algebra k G) := ⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ * a₂) (b₁ * b₂)⟩ lemma mul_def {f g : monoid_algebra k G} : f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ * a₂) (b₁ * b₂)) := rfl instance : non_unital_non_assoc_semiring (monoid_algebra k G) := { zero := 0, mul := (*), add := (+), left_distrib := assume f g h, by haveI := classical.dec_eq G; simp only [mul_def, sum_add_index, mul_add, mul_zero, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add], right_distrib := assume f g h, by haveI := classical.dec_eq G; simp only [mul_def, sum_add_index, add_mul, zero_mul, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add], zero_mul := assume f, by simp only [mul_def, sum_zero_index], mul_zero := assume f, by simp only [mul_def, sum_zero_index, sum_zero], .. finsupp.add_comm_monoid } variables [semiring R] lemma lift_nc_mul {g_hom : Type*} [mul_hom_class g_hom G R] (f : k →+* R) (g : g_hom) (a b : monoid_algebra k G) (h_comm : ∀ {x y}, y ∈ a.support → commute (f (b x)) (g y)) : lift_nc (f : k →+ R) g (a * b) = lift_nc (f : k →+ R) g a * lift_nc (f : k →+ R) g b := begin conv_rhs { rw [← sum_single a, ← sum_single b] }, simp_rw [mul_def, (lift_nc _ g).map_finsupp_sum, lift_nc_single, finsupp.sum_mul, finsupp.mul_sum], refine finset.sum_congr rfl (λ y hy, finset.sum_congr rfl (λ x hx, _)), simp [mul_assoc, (h_comm hy).left_comm] end end has_mul section semigroup variables [semiring k] [semigroup G] [semiring R] instance : non_unital_semiring (monoid_algebra k G) := { zero := 0, mul := (*), add := (+), mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, add_assoc, mul_assoc, zero_mul, mul_zero, sum_zero, sum_add], .. monoid_algebra.non_unital_non_assoc_semiring} end semigroup section has_one variables [non_assoc_semiring R] [semiring k] [has_one G] /-- The unit of the multiplication is `single 1 1`, i.e. the function that is `1` at `1` and zero elsewhere. -/ instance : has_one (monoid_algebra k G) := ⟨single 1 1⟩ lemma one_def : (1 : monoid_algebra k G) = single 1 1 := rfl @[simp] lemma lift_nc_one {g_hom : Type*} [one_hom_class g_hom G R] (f : k →+* R) (g : g_hom) : lift_nc (f : k →+ R) g 1 = 1 := by simp [one_def] end has_one section mul_one_class variables [semiring k] [mul_one_class G] instance : non_assoc_semiring (monoid_algebra k G) := { one := 1, mul := (*), zero := 0, add := (+), nat_cast := λ n, single 1 n, nat_cast_zero := by simp [nat.cast], nat_cast_succ := λ _, by simp [nat.cast]; refl, one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, zero_mul, single_zero, sum_zero, zero_add, one_mul, sum_single], mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, mul_zero, single_zero, sum_zero, add_zero, mul_one, sum_single], ..monoid_algebra.non_unital_non_assoc_semiring } lemma nat_cast_def (n : ℕ) : (n : monoid_algebra k G) = single 1 n := rfl end mul_one_class /-! #### Semiring structure -/ section semiring variables [semiring k] [monoid G] instance : semiring (monoid_algebra k G) := { one := 1, mul := (*), zero := 0, add := (+), .. monoid_algebra.non_unital_semiring, .. monoid_algebra.non_assoc_semiring } variables [semiring R] /-- `lift_nc` as a `ring_hom`, for when `f x` and `g y` commute -/ def lift_nc_ring_hom (f : k →+* R) (g : G →* R) (h_comm : ∀ x y, commute (f x) (g y)) : monoid_algebra k G →+* R := { to_fun := lift_nc (f : k →+ R) g, map_one' := lift_nc_one _ _, map_mul' := λ a b, lift_nc_mul _ _ _ _ $ λ _ _ _, h_comm _ _, ..(lift_nc (f : k →+ R) g)} end semiring instance [comm_semiring k] [comm_semigroup G] : non_unital_comm_semiring (monoid_algebra k G) := { mul_comm := assume f g, begin simp only [mul_def, finsupp.sum, mul_comm], rw [finset.sum_comm], simp only [mul_comm] end, .. monoid_algebra.non_unital_semiring } instance [semiring k] [nontrivial k] [nonempty G]: nontrivial (monoid_algebra k G) := finsupp.nontrivial /-! #### Derived instances -/ section derived_instances instance [comm_semiring k] [comm_monoid G] : comm_semiring (monoid_algebra k G) := { .. monoid_algebra.non_unital_comm_semiring, .. monoid_algebra.semiring } instance [semiring k] [subsingleton k] : unique (monoid_algebra k G) := finsupp.unique_of_right instance [ring k] : add_comm_group (monoid_algebra k G) := finsupp.add_comm_group instance [ring k] [has_mul G] : non_unital_non_assoc_ring (monoid_algebra k G) := { .. monoid_algebra.add_comm_group, .. monoid_algebra.non_unital_non_assoc_semiring } instance [ring k] [semigroup G] : non_unital_ring (monoid_algebra k G) := { .. monoid_algebra.add_comm_group, .. monoid_algebra.non_unital_semiring } instance [ring k] [mul_one_class G] : non_assoc_ring (monoid_algebra k G) := { int_cast := λ z, single 1 (z : k), int_cast_of_nat := λ n, by simpa, int_cast_neg_succ_of_nat := λ n, by simpa, .. monoid_algebra.add_comm_group, .. monoid_algebra.non_assoc_semiring } lemma int_cast_def [ring k] [mul_one_class G] (z : ℤ) : (z : monoid_algebra k G) = single 1 z := rfl instance [ring k] [monoid G] : ring (monoid_algebra k G) := { .. monoid_algebra.non_assoc_ring, .. monoid_algebra.semiring } instance [comm_ring k] [comm_semigroup G] : non_unital_comm_ring (monoid_algebra k G) := { .. monoid_algebra.non_unital_comm_semiring, .. monoid_algebra.non_unital_ring } instance [comm_ring k] [comm_monoid G] : comm_ring (monoid_algebra k G) := { .. monoid_algebra.non_unital_comm_ring, .. monoid_algebra.ring } variables {S : Type*} instance [semiring k] [smul_zero_class R k] : smul_zero_class R (monoid_algebra k G) := finsupp.smul_zero_class instance [semiring k] [distrib_smul R k] : distrib_smul R (monoid_algebra k G) := finsupp.distrib_smul _ _ instance [monoid R] [semiring k] [distrib_mul_action R k] : distrib_mul_action R (monoid_algebra k G) := finsupp.distrib_mul_action G k instance [semiring R] [semiring k] [module R k] : module R (monoid_algebra k G) := finsupp.module G k instance [monoid R] [semiring k] [distrib_mul_action R k] [has_faithful_smul R k] [nonempty G] : has_faithful_smul R (monoid_algebra k G) := finsupp.has_faithful_smul instance [semiring k] [smul_zero_class R k] [smul_zero_class S k] [has_smul R S] [is_scalar_tower R S k] : is_scalar_tower R S (monoid_algebra k G) := finsupp.is_scalar_tower G k instance [monoid R] [monoid S] [semiring k] [distrib_mul_action R k] [distrib_mul_action S k] [smul_comm_class R S k] : smul_comm_class R S (monoid_algebra k G) := finsupp.smul_comm_class G k instance [monoid R] [semiring k] [distrib_mul_action R k] [distrib_mul_action Rᵐᵒᵖ k] [is_central_scalar R k] : is_central_scalar R (monoid_algebra k G) := finsupp.is_central_scalar G k /-- This is not an instance as it conflicts with `monoid_algebra.distrib_mul_action` when `G = kˣ`. -/ def comap_distrib_mul_action_self [group G] [semiring k] : distrib_mul_action G (monoid_algebra k G) := finsupp.comap_distrib_mul_action end derived_instances section misc_theorems variables [semiring k] local attribute [reducible] monoid_algebra lemma mul_apply [decidable_eq G] [has_mul G] (f g : monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, if a₁ * a₂ = x then b₁ * b₂ else 0) := begin rw [mul_def], simp only [finsupp.sum_apply, single_apply], end lemma mul_apply_antidiagonal [has_mul G] (f g : monoid_algebra k G) (x : G) (s : finset (G × G)) (hs : ∀ {p : G × G}, p ∈ s ↔ p.1 * p.2 = x) : (f * g) x = ∑ p in s, (f p.1 * g p.2) := by classical; exact let F : G × G → k := λ p, if p.1 * p.2 = x then f p.1 * g p.2 else 0 in calc (f * g) x = (∑ a₁ in f.support, ∑ a₂ in g.support, F (a₁, a₂)) : mul_apply f g x ... = ∑ p in f.support ×ˢ g.support, F p : finset.sum_product.symm ... = ∑ p in (f.support ×ˢ g.support).filter (λ p : G × G, p.1 * p.2 = x), f p.1 * g p.2 : (finset.sum_filter _ _).symm ... = ∑ p in s.filter (λ p : G × G, p.1 ∈ f.support ∧ p.2 ∈ g.support), f p.1 * g p.2 : sum_congr (by { ext, simp only [mem_filter, mem_product, hs, and_comm] }) (λ _ _, rfl) ... = ∑ p in s, f p.1 * g p.2 : sum_subset (filter_subset _ _) $ λ p hps hp, begin simp only [mem_filter, mem_support_iff, not_and, not_not] at hp ⊢, by_cases h1 : f p.1 = 0, { rw [h1, zero_mul] }, { rw [hp hps h1, mul_zero] } end @[simp] lemma single_mul_single [has_mul G] {a₁ a₂ : G} {b₁ b₂ : k} : (single a₁ b₁ : monoid_algebra k G) * single a₂ b₂ = single (a₁ * a₂) (b₁ * b₂) := (sum_single_index (by simp only [zero_mul, single_zero, sum_zero])).trans (sum_single_index (by rw [mul_zero, single_zero])) @[simp] lemma single_pow [monoid G] {a : G} {b : k} : ∀ n : ℕ, (single a b : monoid_algebra k G)^n = single (a^n) (b ^ n) | 0 := by { simp only [pow_zero], refl } | (n+1) := by simp only [pow_succ, single_pow n, single_mul_single] section /-- Like `finsupp.map_domain_zero`, but for the `1` we define in this file -/ @[simp] lemma map_domain_one {α : Type*} {β : Type*} {α₂ : Type*} [semiring β] [has_one α] [has_one α₂] {F : Type*} [one_hom_class F α α₂] (f : F) : (map_domain f (1 : monoid_algebra β α) : monoid_algebra β α₂) = (1 : monoid_algebra β α₂) := by simp_rw [one_def, map_domain_single, map_one] /-- Like `finsupp.map_domain_add`, but for the convolutive multiplication we define in this file -/ lemma map_domain_mul {α : Type*} {β : Type*} {α₂ : Type*} [semiring β] [has_mul α] [has_mul α₂] {F : Type*} [mul_hom_class F α α₂] (f : F) (x y : monoid_algebra β α) : (map_domain f (x * y : monoid_algebra β α) : monoid_algebra β α₂) = (map_domain f x * map_domain f y : monoid_algebra β α₂) := begin simp_rw [mul_def, map_domain_sum, map_domain_single, map_mul], rw finsupp.sum_map_domain_index, { congr, ext a b, rw finsupp.sum_map_domain_index, { simp }, { simp [mul_add] } }, { simp }, { simp [add_mul] } end variables (k G) /-- The embedding of a magma into its magma algebra. -/ @[simps] def of_magma [has_mul G] : G →ₙ* (monoid_algebra k G) := { to_fun := λ a, single a 1, map_mul' := λ a b, by simp only [mul_def, mul_one, sum_single_index, single_eq_zero, mul_zero], } /-- The embedding of a unital magma into its magma algebra. -/ @[simps] def of [mul_one_class G] : G →* monoid_algebra k G := { to_fun := λ a, single a 1, map_one' := rfl, .. of_magma k G } end lemma smul_of [mul_one_class G] (g : G) (r : k) : r • (of k G g) = single g r := by simp lemma of_injective [mul_one_class G] [nontrivial k] : function.injective (of k G) := λ a b h, by simpa using (single_eq_single_iff _ _ _ _).mp h /-- `finsupp.single` as a `monoid_hom` from the product type into the monoid algebra. Note the order of the elements of the product are reversed compared to the arguments of `finsupp.single`. -/ @[simps] def single_hom [mul_one_class G] : k × G →* monoid_algebra k G := { to_fun := λ a, single a.2 a.1, map_one' := rfl, map_mul' := λ a b, single_mul_single.symm } lemma mul_single_apply_aux [has_mul G] (f : monoid_algebra k G) {r : k} {x y z : G} (H : ∀ a, a * x = z ↔ a = y) : (f * single x r) z = f y * r := by classical; exact have A : ∀ a₁ b₁, (single x r).sum (λ a₂ b₂, ite (a₁ * a₂ = z) (b₁ * b₂) 0) = ite (a₁ * x = z) (b₁ * r) 0, from λ a₁ b₁, sum_single_index $ by simp, calc (f * single x r) z = sum f (λ a b, if (a = y) then (b * r) else 0) : by simp only [mul_apply, A, H] ... = if y ∈ f.support then f y * r else 0 : f.support.sum_ite_eq' _ _ ... = f y * r : by split_ifs with h; simp at h; simp [h] lemma mul_single_one_apply [mul_one_class G] (f : monoid_algebra k G) (r : k) (x : G) : (f * single 1 r) x = f x * r := f.mul_single_apply_aux $ λ a, by rw [mul_one] lemma mul_single_apply_of_not_exists_mul [has_mul G] (r : k) {g g' : G} (x : monoid_algebra k G) (h : ¬∃ d, g' = d * g): (x * finsupp.single g r : monoid_algebra k G) g' = 0 := begin classical, rw [mul_apply, finsupp.sum_comm, finsupp.sum_single_index], swap, { simp_rw [finsupp.sum, mul_zero, if_t_t, finset.sum_const_zero] }, { apply finset.sum_eq_zero, simp_rw ite_eq_right_iff, rintros g'' hg'' rfl, exfalso, exact h ⟨_, rfl⟩ } end lemma single_mul_apply_aux [has_mul G] (f : monoid_algebra k G) {r : k} {x y z : G} (H : ∀ a, x * a = y ↔ a = z) : (single x r * f) y = r * f z := by classical; exact ( have f.sum (λ a b, ite (x * a = y) (0 * b) 0) = 0, by simp, calc (single x r * f) y = sum f (λ a b, ite (x * a = y) (r * b) 0) : (mul_apply _ _ _).trans $ sum_single_index (by exact this) ... = f.sum (λ a b, ite (a = z) (r * b) 0) : by simp only [H] ... = if z ∈ f.support then (r * f z) else 0 : f.support.sum_ite_eq' _ _ ... = _ : by split_ifs with h; simp at h; simp [h]) lemma single_one_mul_apply [mul_one_class G] (f : monoid_algebra k G) (r : k) (x : G) : (single 1 r * f) x = r * f x := f.single_mul_apply_aux $ λ a, by rw [one_mul] lemma single_mul_apply_of_not_exists_mul [has_mul G] (r : k) {g g' : G} (x : monoid_algebra k G) (h : ¬∃ d, g' = g * d): (finsupp.single g r * x : monoid_algebra k G) g' = 0 := begin classical, rw [mul_apply, finsupp.sum_single_index], swap, { simp_rw [finsupp.sum, zero_mul, if_t_t, finset.sum_const_zero] }, { apply finset.sum_eq_zero, simp_rw ite_eq_right_iff, rintros g'' hg'' rfl, exfalso, exact h ⟨_, rfl⟩ }, end lemma lift_nc_smul [mul_one_class G] {R : Type*} [semiring R] (f : k →+* R) (g : G →* R) (c : k) (φ : monoid_algebra k G) : lift_nc (f : k →+ R) g (c • φ) = f c * lift_nc (f : k →+ R) g φ := begin suffices : (lift_nc ↑f g).comp (smul_add_hom k (monoid_algebra k G) c) = (add_monoid_hom.mul_left (f c)).comp (lift_nc ↑f g), from add_monoid_hom.congr_fun this φ, ext a b, simp [mul_assoc] end end misc_theorems /-! #### Non-unital, non-associative algebra structure -/ section non_unital_non_assoc_algebra variables (k) [semiring k] [distrib_smul R k] [has_mul G] instance is_scalar_tower_self [is_scalar_tower R k k] : is_scalar_tower R (monoid_algebra k G) (monoid_algebra k G) := ⟨λ t a b, begin ext m, classical, simp only [mul_apply, finsupp.smul_sum, smul_ite, smul_mul_assoc, sum_smul_index', zero_mul, if_t_t, implies_true_iff, eq_self_iff_true, sum_zero, coe_smul, smul_eq_mul, pi.smul_apply, smul_zero], end⟩ /-- Note that if `k` is a `comm_semiring` then we have `smul_comm_class k k k` and so we can take `R = k` in the below. In other words, if the coefficients are commutative amongst themselves, they also commute with the algebra multiplication. -/ instance smul_comm_class_self [smul_comm_class R k k] : smul_comm_class R (monoid_algebra k G) (monoid_algebra k G) := ⟨λ t a b, begin classical, ext m, simp only [mul_apply, finsupp.sum, finset.smul_sum, smul_ite, mul_smul_comm, sum_smul_index', implies_true_iff, eq_self_iff_true, coe_smul, ite_eq_right_iff, smul_eq_mul, pi.smul_apply, mul_zero, smul_zero], end⟩ instance smul_comm_class_symm_self [smul_comm_class k R k] : smul_comm_class (monoid_algebra k G) R (monoid_algebra k G) := ⟨λ t a b, by { haveI := smul_comm_class.symm k R k, rw ← smul_comm, } ⟩ variables {A : Type u₃} [non_unital_non_assoc_semiring A] /-- A non_unital `k`-algebra homomorphism from `monoid_algebra k G` is uniquely defined by its values on the functions `single a 1`. -/ lemma non_unital_alg_hom_ext [distrib_mul_action k A] {φ₁ φ₂ : monoid_algebra k G →ₙₐ[k] A} (h : ∀ x, φ₁ (single x 1) = φ₂ (single x 1)) : φ₁ = φ₂ := non_unital_alg_hom.to_distrib_mul_action_hom_injective $ finsupp.distrib_mul_action_hom_ext' $ λ a, distrib_mul_action_hom.ext_ring (h a) /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma non_unital_alg_hom_ext' [distrib_mul_action k A] {φ₁ φ₂ : monoid_algebra k G →ₙₐ[k] A} (h : φ₁.to_mul_hom.comp (of_magma k G) = φ₂.to_mul_hom.comp (of_magma k G)) : φ₁ = φ₂ := non_unital_alg_hom_ext k $ mul_hom.congr_fun h /-- The functor `G ↦ monoid_algebra k G`, from the category of magmas to the category of non-unital, non-associative algebras over `k` is adjoint to the forgetful functor in the other direction. -/ @[simps] def lift_magma [module k A] [is_scalar_tower k A A] [smul_comm_class k A A] : (G →ₙ* A) ≃ (monoid_algebra k G →ₙₐ[k] A) := { to_fun := λ f, { to_fun := λ a, a.sum (λ m t, t • f m), map_smul' := λ t' a, begin rw [finsupp.smul_sum, sum_smul_index'], { simp_rw smul_assoc, }, { intros m, exact zero_smul k (f m), }, end, map_mul' := λ a₁ a₂, begin let g : G → k → A := λ m t, t • f m, have h₁ : ∀ m, g m 0 = 0, { intros, exact zero_smul k (f m), }, have h₂ : ∀ m (t₁ t₂ : k), g m (t₁ + t₂) = g m t₁ + g m t₂, { intros, rw ← add_smul, }, simp_rw [finsupp.mul_sum, finsupp.sum_mul, smul_mul_smul, ← f.map_mul, mul_def, sum_comm a₂ a₁, sum_sum_index h₁ h₂, sum_single_index (h₁ _)], end, .. lift_add_hom (λ x, (smul_add_hom k A).flip (f x)) }, inv_fun := λ F, F.to_mul_hom.comp (of_magma k G), left_inv := λ f, by { ext m, simp only [non_unital_alg_hom.coe_mk, of_magma_apply, non_unital_alg_hom.to_mul_hom_eq_coe, sum_single_index, function.comp_app, one_smul, zero_smul, mul_hom.coe_comp, non_unital_alg_hom.coe_to_mul_hom], }, right_inv := λ F, by { ext m, simp only [non_unital_alg_hom.coe_mk, of_magma_apply, non_unital_alg_hom.to_mul_hom_eq_coe, sum_single_index, function.comp_app, one_smul, zero_smul, mul_hom.coe_comp, non_unital_alg_hom.coe_to_mul_hom], }, } end non_unital_non_assoc_algebra /-! #### Algebra structure -/ section algebra local attribute [reducible] monoid_algebra lemma single_one_comm [comm_semiring k] [mul_one_class G] (r : k) (f : monoid_algebra k G) : single 1 r * f = f * single 1 r := by { ext, rw [single_one_mul_apply, mul_single_one_apply, mul_comm] } /-- `finsupp.single 1` as a `ring_hom` -/ @[simps] def single_one_ring_hom [semiring k] [mul_one_class G] : k →+* monoid_algebra k G := { map_one' := rfl, map_mul' := λ x y, by rw [single_add_hom, single_mul_single, one_mul], ..finsupp.single_add_hom 1} /-- If `f : G → H` is a multiplicative homomorphism between two monoids, then `finsupp.map_domain f` is a ring homomorphism between their monoid algebras. -/ @[simps] def map_domain_ring_hom (k : Type*) {H F : Type*} [semiring k] [monoid G] [monoid H] [monoid_hom_class F G H] (f : F) : monoid_algebra k G →+* monoid_algebra k H := { map_one' := map_domain_one f, map_mul' := λ x y, map_domain_mul f x y, ..(finsupp.map_domain.add_monoid_hom f : monoid_algebra k G →+ monoid_algebra k H) } /-- If two ring homomorphisms from `monoid_algebra k G` are equal on all `single a 1` and `single 1 b`, then they are equal. -/ lemma ring_hom_ext {R} [semiring k] [mul_one_class G] [semiring R] {f g : monoid_algebra k G →+* R} (h₁ : ∀ b, f (single 1 b) = g (single 1 b)) (h_of : ∀ a, f (single a 1) = g (single a 1)) : f = g := ring_hom.coe_add_monoid_hom_injective $ add_hom_ext $ λ a b, by rw [← one_mul a, ← mul_one b, ← single_mul_single, f.coe_add_monoid_hom, g.coe_add_monoid_hom, f.map_mul, g.map_mul, h₁, h_of] /-- If two ring homomorphisms from `monoid_algebra k G` are equal on all `single a 1` and `single 1 b`, then they are equal. See note [partially-applied ext lemmas]. -/ @[ext] lemma ring_hom_ext' {R} [semiring k] [mul_one_class G] [semiring R] {f g : monoid_algebra k G →+* R} (h₁ : f.comp single_one_ring_hom = g.comp single_one_ring_hom) (h_of : (f : monoid_algebra k G →* R).comp (of k G) = (g : monoid_algebra k G →* R).comp (of k G)) : f = g := ring_hom_ext (ring_hom.congr_fun h₁) (monoid_hom.congr_fun h_of) /-- The instance `algebra k (monoid_algebra A G)` whenever we have `algebra k A`. In particular this provides the instance `algebra k (monoid_algebra k G)`. -/ instance {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] : algebra k (monoid_algebra A G) := { smul_def' := λ r a, by { ext, simp [single_one_mul_apply, algebra.smul_def, pi.smul_apply], }, commutes' := λ r f, by { ext, simp [single_one_mul_apply, mul_single_one_apply, algebra.commutes], }, ..single_one_ring_hom.comp (algebra_map k A) } /-- `finsupp.single 1` as a `alg_hom` -/ @[simps] def single_one_alg_hom {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] : A →ₐ[k] monoid_algebra A G := { commutes' := λ r, by { ext, simp, refl, }, ..single_one_ring_hom} @[simp] lemma coe_algebra_map {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] : ⇑(algebra_map k (monoid_algebra A G)) = single 1 ∘ (algebra_map k A) := rfl lemma single_eq_algebra_map_mul_of [comm_semiring k] [monoid G] (a : G) (b : k) : single a b = algebra_map k (monoid_algebra k G) b * of k G a := by simp lemma single_algebra_map_eq_algebra_map_mul_of {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] (a : G) (b : k) : single a (algebra_map k A b) = algebra_map k (monoid_algebra A G) b * of A G a := by simp lemma induction_on [semiring k] [monoid G] {p : monoid_algebra k G → Prop} (f : monoid_algebra k G) (hM : ∀ g, p (of k G g)) (hadd : ∀ f g : monoid_algebra k G, p f → p g → p (f + g)) (hsmul : ∀ (r : k) f, p f → p (r • f)) : p f := begin refine finsupp.induction_linear f _ (λ f g hf hg, hadd f g hf hg) (λ g r, _), { simpa using hsmul 0 (of k G 1) (hM 1) }, { convert hsmul r (of k G g) (hM g), simp only [mul_one, smul_single', of_apply] }, end end algebra section lift variables {k G} [comm_semiring k] [monoid G] variables {A : Type u₃} [semiring A] [algebra k A] {B : Type*} [semiring B] [algebra k B] /-- `lift_nc_ring_hom` as a `alg_hom`, for when `f` is an `alg_hom` -/ def lift_nc_alg_hom (f : A →ₐ[k] B) (g : G →* B) (h_comm : ∀ x y, commute (f x) (g y)) : monoid_algebra A G →ₐ[k] B := { to_fun := lift_nc_ring_hom (f : A →+* B) g h_comm, commutes' := by simp [lift_nc_ring_hom], ..(lift_nc_ring_hom (f : A →+* B) g h_comm)} /-- A `k`-algebra homomorphism from `monoid_algebra k G` is uniquely defined by its values on the functions `single a 1`. -/ lemma alg_hom_ext ⦃φ₁ φ₂ : monoid_algebra k G →ₐ[k] A⦄ (h : ∀ x, φ₁ (single x 1) = φ₂ (single x 1)) : φ₁ = φ₂ := alg_hom.to_linear_map_injective $ finsupp.lhom_ext' $ λ a, linear_map.ext_ring (h a) /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma alg_hom_ext' ⦃φ₁ φ₂ : monoid_algebra k G →ₐ[k] A⦄ (h : (φ₁ : monoid_algebra k G →* A).comp (of k G) = (φ₂ : monoid_algebra k G →* A).comp (of k G)) : φ₁ = φ₂ := alg_hom_ext $ monoid_hom.congr_fun h variables (k G A) /-- Any monoid homomorphism `G →* A` can be lifted to an algebra homomorphism `monoid_algebra k G →ₐ[k] A`. -/ def lift : (G →* A) ≃ (monoid_algebra k G →ₐ[k] A) := { inv_fun := λ f, (f : monoid_algebra k G →* A).comp (of k G), to_fun := λ F, lift_nc_alg_hom (algebra.of_id k A) F $ λ _ _, algebra.commutes _ _, left_inv := λ f, by { ext, simp [lift_nc_alg_hom, lift_nc_ring_hom] }, right_inv := λ F, by { ext, simp [lift_nc_alg_hom, lift_nc_ring_hom] } } variables {k G A} lemma lift_apply' (F : G →* A) (f : monoid_algebra k G) : lift k G A F f = f.sum (λ a b, (algebra_map k A b) * F a) := rfl lemma lift_apply (F : G →* A) (f : monoid_algebra k G) : lift k G A F f = f.sum (λ a b, b • F a) := by simp only [lift_apply', algebra.smul_def] lemma lift_def (F : G →* A) : ⇑(lift k G A F) = lift_nc ((algebra_map k A : k →+* A) : k →+ A) F := rfl @[simp] lemma lift_symm_apply (F : monoid_algebra k G →ₐ[k] A) (x : G) : (lift k G A).symm F x = F (single x 1) := rfl lemma lift_of (F : G →* A) (x) : lift k G A F (of k G x) = F x := by rw [of_apply, ← lift_symm_apply, equiv.symm_apply_apply] @[simp] lemma lift_single (F : G →* A) (a b) : lift k G A F (single a b) = b • F a := by rw [lift_def, lift_nc_single, algebra.smul_def, ring_hom.coe_add_monoid_hom] lemma lift_unique' (F : monoid_algebra k G →ₐ[k] A) : F = lift k G A ((F : monoid_algebra k G →* A).comp (of k G)) := ((lift k G A).apply_symm_apply F).symm /-- Decomposition of a `k`-algebra homomorphism from `monoid_algebra k G` by its values on `F (single a 1)`. -/ lemma lift_unique (F : monoid_algebra k G →ₐ[k] A) (f : monoid_algebra k G) : F f = f.sum (λ a b, b • F (single a 1)) := by conv_lhs { rw lift_unique' F, simp [lift_apply] } /-- If `f : G → H` is a homomorphism between two magmas, then `finsupp.map_domain f` is a non-unital algebra homomorphism between their magma algebras. -/ @[simps] def map_domain_non_unital_alg_hom (k A : Type*) [comm_semiring k] [semiring A] [algebra k A] {G H F : Type*} [has_mul G] [has_mul H] [mul_hom_class F G H] (f : F) : monoid_algebra A G →ₙₐ[k] monoid_algebra A H := { map_mul' := λ x y, map_domain_mul f x y, map_smul' := λ r x, map_domain_smul r x, ..(finsupp.map_domain.add_monoid_hom f : monoid_algebra A G →+ monoid_algebra A H) } lemma map_domain_algebra_map (k A : Type*) {H F : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid H] [monoid_hom_class F G H] (f : F) (r : k) : map_domain f (algebra_map k (monoid_algebra A G) r) = algebra_map k (monoid_algebra A H) r := by simp only [coe_algebra_map, map_domain_single, map_one] /-- If `f : G → H` is a multiplicative homomorphism between two monoids, then `finsupp.map_domain f` is an algebra homomorphism between their monoid algebras. -/ @[simps] def map_domain_alg_hom (k A : Type*) [comm_semiring k] [semiring A] [algebra k A] {H F : Type*} [monoid H] [monoid_hom_class F G H] (f : F) : monoid_algebra A G →ₐ[k] monoid_algebra A H := { commutes' := map_domain_algebra_map k A f, ..map_domain_ring_hom A f} end lift section local attribute [reducible] monoid_algebra variables (k) /-- When `V` is a `k[G]`-module, multiplication by a group element `g` is a `k`-linear map. -/ def group_smul.linear_map [monoid G] [comm_semiring k] (V : Type u₃) [add_comm_monoid V] [module k V] [module (monoid_algebra k G) V] [is_scalar_tower k (monoid_algebra k G) V] (g : G) : V →ₗ[k] V := { to_fun := λ v, (single g (1 : k) • v : V), map_add' := λ x y, smul_add (single g (1 : k)) x y, map_smul' := λ c x, smul_algebra_smul_comm _ _ _ } @[simp] lemma group_smul.linear_map_apply [monoid G] [comm_semiring k] (V : Type u₃) [add_comm_monoid V] [module k V] [module (monoid_algebra k G) V] [is_scalar_tower k (monoid_algebra k G) V] (g : G) (v : V) : (group_smul.linear_map k V g) v = (single g (1 : k) • v : V) := rfl section variables {k} variables [monoid G] [comm_semiring k] {V W : Type u₃} [add_comm_monoid V] [module k V] [module (monoid_algebra k G) V] [is_scalar_tower k (monoid_algebra k G) V] [add_comm_monoid W] [module k W] [module (monoid_algebra k G) W] [is_scalar_tower k (monoid_algebra k G) W] (f : V →ₗ[k] W) (h : ∀ (g : G) (v : V), f (single g (1 : k) • v : V) = (single g (1 : k) • (f v) : W)) include h /-- Build a `k[G]`-linear map from a `k`-linear map and evidence that it is `G`-equivariant. -/ def equivariant_of_linear_of_comm : V →ₗ[monoid_algebra k G] W := { to_fun := f, map_add' := λ v v', by simp, map_smul' := λ c v, begin apply finsupp.induction c, { simp, }, { intros g r c' nm nz w, dsimp at *, simp only [add_smul, f.map_add, w, add_left_inj, single_eq_algebra_map_mul_of, ← smul_smul], erw [algebra_map_smul (monoid_algebra k G) r, algebra_map_smul (monoid_algebra k G) r, f.map_smul, h g v, of_apply], all_goals { apply_instance } } end, } @[simp] lemma equivariant_of_linear_of_comm_apply (v : V) : (equivariant_of_linear_of_comm f h) v = f v := rfl end end section universe ui variable {ι : Type ui} local attribute [reducible] monoid_algebra lemma prod_single [comm_semiring k] [comm_monoid G] {s : finset ι} {a : ι → G} {b : ι → k} : (∏ i in s, single (a i) (b i)) = single (∏ i in s, a i) (∏ i in s, b i) := finset.cons_induction_on s rfl $ λ a s has ih, by rw [prod_cons has, ih, single_mul_single, prod_cons has, prod_cons has] end section -- We now prove some additional statements that hold for group algebras. variables [semiring k] [group G] local attribute [reducible] monoid_algebra @[simp] lemma mul_single_apply (f : monoid_algebra k G) (r : k) (x y : G) : (f * single x r) y = f (y * x⁻¹) * r := f.mul_single_apply_aux $ λ a, eq_mul_inv_iff_mul_eq.symm @[simp] lemma single_mul_apply (r : k) (x : G) (f : monoid_algebra k G) (y : G) : (single x r * f) y = r * f (x⁻¹ * y) := f.single_mul_apply_aux $ λ z, eq_inv_mul_iff_mul_eq.symm lemma mul_apply_left (f g : monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λ a b, b * (g (a⁻¹ * x))) := calc (f * g) x = sum f (λ a b, (single a b * g) x) : by rw [← finsupp.sum_apply, ← finsupp.sum_mul, f.sum_single] ... = _ : by simp only [single_mul_apply, finsupp.sum] -- If we'd assumed `comm_semiring`, we could deduce this from `mul_apply_left`. lemma mul_apply_right (f g : monoid_algebra k G) (x : G) : (f * g) x = (g.sum $ λa b, (f (x * a⁻¹)) * b) := calc (f * g) x = sum g (λ a b, (f * single a b) x) : by rw [← finsupp.sum_apply, ← finsupp.mul_sum, g.sum_single] ... = _ : by simp only [mul_single_apply, finsupp.sum] end section opposite open finsupp mul_opposite variables [semiring k] /-- The opposite of an `monoid_algebra R I` equivalent as a ring to the `monoid_algebra Rᵐᵒᵖ Iᵐᵒᵖ` over the opposite ring, taking elements to their opposite. -/ @[simps {simp_rhs := tt}] protected noncomputable def op_ring_equiv [monoid G] : (monoid_algebra k G)ᵐᵒᵖ ≃+* monoid_algebra kᵐᵒᵖ Gᵐᵒᵖ := { map_mul' := begin dsimp only [add_equiv.to_fun_eq_coe, ←add_equiv.coe_to_add_monoid_hom], rw add_monoid_hom.map_mul_iff, ext i₁ r₁ i₂ r₂ : 6, simp end, ..op_add_equiv.symm.trans $ (finsupp.map_range.add_equiv (op_add_equiv : k ≃+ kᵐᵒᵖ)).trans $ finsupp.dom_congr op_equiv } @[simp] lemma op_ring_equiv_single [monoid G] (r : k) (x : G) : monoid_algebra.op_ring_equiv (op (single x r)) = single (op x) (op r) := by simp @[simp] lemma op_ring_equiv_symm_single [monoid G] (r : kᵐᵒᵖ) (x : Gᵐᵒᵖ) : monoid_algebra.op_ring_equiv.symm (single x r) = op (single x.unop r.unop) := by simp end opposite section submodule variables {k G} [comm_semiring k] [monoid G] variables {V : Type*} [add_comm_monoid V] variables [module k V] [module (monoid_algebra k G) V] [is_scalar_tower k (monoid_algebra k G) V] /-- A submodule over `k` which is stable under scalar multiplication by elements of `G` is a submodule over `monoid_algebra k G` -/ def submodule_of_smul_mem (W : submodule k V) (h : ∀ (g : G) (v : V), v ∈ W → (of k G g) • v ∈ W) : submodule (monoid_algebra k G) V := { carrier := W, zero_mem' := W.zero_mem', add_mem' := λ _ _, W.add_mem', smul_mem' := begin intros f v hv, rw [←finsupp.sum_single f, finsupp.sum, finset.sum_smul], simp_rw [←smul_of, smul_assoc], exact submodule.sum_smul_mem W _ (λ g _, h g v hv) end } end submodule end monoid_algebra /-! ### Additive monoids -/ section variables [semiring k] /-- The monoid algebra over a semiring `k` generated by the additive monoid `G`. It is the type of finite formal `k`-linear combinations of terms of `G`, endowed with the convolution product. -/ @[derive [inhabited, add_comm_monoid]] def add_monoid_algebra := G →₀ k instance : has_coe_to_fun (add_monoid_algebra k G) (λ _, G → k) := finsupp.has_coe_to_fun end namespace add_monoid_algebra variables {k G} section variables [semiring k] [non_unital_non_assoc_semiring R] /-- A non-commutative version of `add_monoid_algebra.lift`: given a additive homomorphism `f : k →+ R` and a map `g : multiplicative G → R`, returns the additive homomorphism from `add_monoid_algebra k G` such that `lift_nc f g (single a b) = f b * g a`. If `f` is a ring homomorphism and the range of either `f` or `g` is in center of `R`, then the result is a ring homomorphism. If `R` is a `k`-algebra and `f = algebra_map k R`, then the result is an algebra homomorphism called `add_monoid_algebra.lift`. -/ def lift_nc (f : k →+ R) (g : multiplicative G → R) : add_monoid_algebra k G →+ R := lift_add_hom (λ x : G, (add_monoid_hom.mul_right (g $ multiplicative.of_add x)).comp f) @[simp] lemma lift_nc_single (f : k →+ R) (g : multiplicative G → R) (a : G) (b : k) : lift_nc f g (single a b) = f b * g (multiplicative.of_add a) := lift_add_hom_apply_single _ _ _ end section has_mul variables [semiring k] [has_add G] /-- The product of `f g : add_monoid_algebra k G` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x + y = a`. (Think of the product of multivariate polynomials where `α` is the additive monoid of monomial exponents.) -/ instance : has_mul (add_monoid_algebra k G) := ⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)⟩ lemma mul_def {f g : add_monoid_algebra k G} : f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)) := rfl instance : non_unital_non_assoc_semiring (add_monoid_algebra k G) := { zero := 0, mul := (*), add := (+), left_distrib := assume f g h, by haveI := classical.dec_eq G; simp only [mul_def, sum_add_index, mul_add, mul_zero, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add], right_distrib := assume f g h, by haveI := classical.dec_eq G; simp only [mul_def, sum_add_index, add_mul, mul_zero, zero_mul, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add], zero_mul := assume f, by simp only [mul_def, sum_zero_index], mul_zero := assume f, by simp only [mul_def, sum_zero_index, sum_zero], nsmul := λ n f, n • f, nsmul_zero' := by { intros, ext, simp [-nsmul_eq_mul, add_smul] }, nsmul_succ' := by { intros, ext, simp [-nsmul_eq_mul, nat.succ_eq_one_add, add_smul] }, .. finsupp.add_comm_monoid } variables [semiring R] lemma lift_nc_mul {g_hom : Type*} [mul_hom_class g_hom (multiplicative G) R] (f : k →+* R) (g : g_hom) (a b : add_monoid_algebra k G) (h_comm : ∀ {x y}, y ∈ a.support → commute (f (b x)) (g $ multiplicative.of_add y)) : lift_nc (f : k →+ R) g (a * b) = lift_nc (f : k →+ R) g a * lift_nc (f : k →+ R) g b := (monoid_algebra.lift_nc_mul f g _ _ @h_comm : _) end has_mul section has_one variables [semiring k] [has_zero G] [non_assoc_semiring R] /-- The unit of the multiplication is `single 1 1`, i.e. the function that is `1` at `0` and zero elsewhere. -/ instance : has_one (add_monoid_algebra k G) := ⟨single 0 1⟩ lemma one_def : (1 : add_monoid_algebra k G) = single 0 1 := rfl @[simp] lemma lift_nc_one {g_hom : Type*} [one_hom_class g_hom (multiplicative G) R] (f : k →+* R) (g : g_hom) : lift_nc (f : k →+ R) g 1 = 1 := (monoid_algebra.lift_nc_one f g : _) end has_one section semigroup variables [semiring k] [add_semigroup G] instance : non_unital_semiring (add_monoid_algebra k G) := { zero := 0, mul := (*), add := (+), mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, add_assoc, mul_assoc, zero_mul, mul_zero, sum_zero, sum_add], .. add_monoid_algebra.non_unital_non_assoc_semiring } end semigroup section mul_one_class variables [semiring k] [add_zero_class G] instance : non_assoc_semiring (add_monoid_algebra k G) := { one := 1, mul := (*), zero := 0, add := (+), nat_cast := λ n, single 0 n, nat_cast_zero := by simp [nat.cast], nat_cast_succ := λ _, by simp [nat.cast]; refl, one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, zero_mul, single_zero, sum_zero, zero_add, one_mul, sum_single], mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, mul_zero, single_zero, sum_zero, add_zero, mul_one, sum_single], .. add_monoid_algebra.non_unital_non_assoc_semiring } lemma nat_cast_def (n : ℕ) : (n : add_monoid_algebra k G) = single 0 n := rfl end mul_one_class /-! #### Semiring structure -/ section semiring instance {R : Type*} [semiring k] [smul_zero_class R k] : smul_zero_class R (add_monoid_algebra k G) := finsupp.smul_zero_class variables [semiring k] [add_monoid G] instance : semiring (add_monoid_algebra k G) := { one := 1, mul := (*), zero := 0, add := (+), .. add_monoid_algebra.non_unital_semiring, .. add_monoid_algebra.non_assoc_semiring, } variables [semiring R] /-- `lift_nc` as a `ring_hom`, for when `f` and `g` commute -/ def lift_nc_ring_hom (f : k →+* R) (g : multiplicative G →* R) (h_comm : ∀ x y, commute (f x) (g y)) : add_monoid_algebra k G →+* R := { to_fun := lift_nc (f : k →+ R) g, map_one' := lift_nc_one _ _, map_mul' := λ a b, lift_nc_mul _ _ _ _ $ λ _ _ _, h_comm _ _, ..(lift_nc (f : k →+ R) g)} end semiring instance [comm_semiring k] [add_comm_semigroup G] : non_unital_comm_semiring (add_monoid_algebra k G) := { mul_comm := @mul_comm (monoid_algebra k $ multiplicative G) _, .. add_monoid_algebra.non_unital_semiring } instance [semiring k] [nontrivial k] [nonempty G] : nontrivial (add_monoid_algebra k G) := finsupp.nontrivial /-! #### Derived instances -/ section derived_instances instance [comm_semiring k] [add_comm_monoid G] : comm_semiring (add_monoid_algebra k G) := { .. add_monoid_algebra.non_unital_comm_semiring, .. add_monoid_algebra.semiring } instance [semiring k] [subsingleton k] : unique (add_monoid_algebra k G) := finsupp.unique_of_right instance [ring k] : add_comm_group (add_monoid_algebra k G) := finsupp.add_comm_group instance [ring k] [has_add G] : non_unital_non_assoc_ring (add_monoid_algebra k G) := { .. add_monoid_algebra.add_comm_group, .. add_monoid_algebra.non_unital_non_assoc_semiring } instance [ring k] [add_semigroup G] : non_unital_ring (add_monoid_algebra k G) := { .. add_monoid_algebra.add_comm_group, .. add_monoid_algebra.non_unital_semiring } instance [ring k] [add_zero_class G] : non_assoc_ring (add_monoid_algebra k G) := { int_cast := λ z, single 0 (z : k), int_cast_of_nat := λ n, by simpa, int_cast_neg_succ_of_nat := λ n, by simpa, .. add_monoid_algebra.add_comm_group, .. add_monoid_algebra.non_assoc_semiring } lemma int_cast_def [ring k] [add_zero_class G] (z : ℤ) : (z : add_monoid_algebra k G) = single 0 z := rfl instance [ring k] [add_monoid G] : ring (add_monoid_algebra k G) := { .. add_monoid_algebra.non_assoc_ring, .. add_monoid_algebra.semiring } instance [comm_ring k] [add_comm_semigroup G] : non_unital_comm_ring (add_monoid_algebra k G) := { .. add_monoid_algebra.non_unital_comm_semiring, .. add_monoid_algebra.non_unital_ring } instance [comm_ring k] [add_comm_monoid G] : comm_ring (add_monoid_algebra k G) := { .. add_monoid_algebra.non_unital_comm_ring, .. add_monoid_algebra.ring } variables {S : Type*} instance [semiring k] [distrib_smul R k] : distrib_smul R (add_monoid_algebra k G) := finsupp.distrib_smul G k instance [monoid R] [semiring k] [distrib_mul_action R k] : distrib_mul_action R (add_monoid_algebra k G) := finsupp.distrib_mul_action G k instance [semiring k] [smul_zero_class R k] [has_faithful_smul R k] [nonempty G] : has_faithful_smul R (add_monoid_algebra k G) := finsupp.has_faithful_smul instance [semiring R] [semiring k] [module R k] : module R (add_monoid_algebra k G) := finsupp.module G k instance [semiring k] [smul_zero_class R k] [smul_zero_class S k] [has_smul R S] [is_scalar_tower R S k] : is_scalar_tower R S (add_monoid_algebra k G) := finsupp.is_scalar_tower G k instance [semiring k] [smul_zero_class R k] [smul_zero_class S k] [smul_comm_class R S k] : smul_comm_class R S (add_monoid_algebra k G) := finsupp.smul_comm_class G k instance [semiring k] [smul_zero_class R k] [smul_zero_class Rᵐᵒᵖ k] [is_central_scalar R k] : is_central_scalar R (add_monoid_algebra k G) := finsupp.is_central_scalar G k /-! It is hard to state the equivalent of `distrib_mul_action G (add_monoid_algebra k G)` because we've never discussed actions of additive groups. -/ end derived_instances . section misc_theorems variables [semiring k] lemma mul_apply [decidable_eq G] [has_add G] (f g : add_monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, if a₁ + a₂ = x then b₁ * b₂ else 0) := @monoid_algebra.mul_apply k (multiplicative G) _ _ _ _ _ _ lemma mul_apply_antidiagonal [has_add G] (f g : add_monoid_algebra k G) (x : G) (s : finset (G × G)) (hs : ∀ {p : G × G}, p ∈ s ↔ p.1 + p.2 = x) : (f * g) x = ∑ p in s, (f p.1 * g p.2) := @monoid_algebra.mul_apply_antidiagonal k (multiplicative G) _ _ _ _ _ s @hs lemma single_mul_single [has_add G] {a₁ a₂ : G} {b₁ b₂ : k} : (single a₁ b₁ * single a₂ b₂ : add_monoid_algebra k G) = single (a₁ + a₂) (b₁ * b₂) := @monoid_algebra.single_mul_single k (multiplicative G) _ _ _ _ _ _ -- This should be a `@[simp]` lemma, but the simp_nf linter times out if we add this. -- Probably the correct fix is to make a `[add_]monoid_algebra.single` with the correct type, -- instead of relying on `finsupp.single`. lemma single_pow [add_monoid G] {a : G} {b : k} : ∀ n : ℕ, ((single a b)^n : add_monoid_algebra k G) = single (n • a) (b ^ n) | 0 := by { simp only [pow_zero, zero_nsmul], refl } | (n+1) := by rw [pow_succ, pow_succ, single_pow n, single_mul_single, add_comm, add_nsmul, one_nsmul] /-- Like `finsupp.map_domain_zero`, but for the `1` we define in this file -/ @[simp] lemma map_domain_one {α : Type*} {β : Type*} {α₂ : Type*} [semiring β] [has_zero α] [has_zero α₂] {F : Type*} [zero_hom_class F α α₂] (f : F) : (map_domain f (1 : add_monoid_algebra β α) : add_monoid_algebra β α₂) = (1 : add_monoid_algebra β α₂) := by simp_rw [one_def, map_domain_single, map_zero] /-- Like `finsupp.map_domain_add`, but for the convolutive multiplication we define in this file -/ lemma map_domain_mul {α : Type*} {β : Type*} {α₂ : Type*} [semiring β] [has_add α] [has_add α₂] {F : Type*} [add_hom_class F α α₂] (f : F) (x y : add_monoid_algebra β α) : (map_domain f (x * y : add_monoid_algebra β α) : add_monoid_algebra β α₂) = (map_domain f x * map_domain f y : add_monoid_algebra β α₂) := begin simp_rw [mul_def, map_domain_sum, map_domain_single, map_add], rw finsupp.sum_map_domain_index, { congr, ext a b, rw finsupp.sum_map_domain_index, { simp }, { simp [mul_add] } }, { simp }, { simp [add_mul] } end section variables (k G) /-- The embedding of an additive magma into its additive magma algebra. -/ @[simps] def of_magma [has_add G] : multiplicative G →ₙ* add_monoid_algebra k G := { to_fun := λ a, single a 1, map_mul' := λ a b, by simpa only [mul_def, mul_one, sum_single_index, single_eq_zero, mul_zero], } /-- Embedding of a magma with zero into its magma algebra. -/ def of [add_zero_class G] : multiplicative G →* add_monoid_algebra k G := { to_fun := λ a, single a 1, map_one' := rfl, .. of_magma k G } /-- Embedding of a magma with zero `G`, into its magma algebra, having `G` as source. -/ def of' : G → add_monoid_algebra k G := λ a, single a 1 end @[simp] lemma of_apply [add_zero_class G] (a : multiplicative G) : of k G a = single a.to_add 1 := rfl @[simp] lemma of'_apply (a : G) : of' k G a = single a 1 := rfl lemma of'_eq_of [add_zero_class G] (a : G) : of' k G a = of k G a := rfl lemma of_injective [nontrivial k] [add_zero_class G] : function.injective (of k G) := λ a b h, by simpa using (single_eq_single_iff _ _ _ _).mp h /-- `finsupp.single` as a `monoid_hom` from the product type into the additive monoid algebra. Note the order of the elements of the product are reversed compared to the arguments of `finsupp.single`. -/ @[simps] def single_hom [add_zero_class G] : k × multiplicative G →* add_monoid_algebra k G := { to_fun := λ a, single a.2.to_add a.1, map_one' := rfl, map_mul' := λ a b, single_mul_single.symm } lemma mul_single_apply_aux [has_add G] (f : add_monoid_algebra k G) (r : k) (x y z : G) (H : ∀ a, a + x = z ↔ a = y) : (f * single x r) z = f y * r := @monoid_algebra.mul_single_apply_aux k (multiplicative G) _ _ _ _ _ _ _ H lemma mul_single_zero_apply [add_zero_class G] (f : add_monoid_algebra k G) (r : k) (x : G) : (f * single 0 r) x = f x * r := f.mul_single_apply_aux r _ _ _ $ λ a, by rw [add_zero] lemma mul_single_apply_of_not_exists_add [has_add G] (r : k) {g g' : G} (x : add_monoid_algebra k G) (h : ¬∃ d, g' = d + g): (x * finsupp.single g r : add_monoid_algebra k G) g' = 0 := @monoid_algebra.mul_single_apply_of_not_exists_mul k (multiplicative G) _ _ _ _ _ _ h lemma single_mul_apply_aux [has_add G] (f : add_monoid_algebra k G) (r : k) (x y z : G) (H : ∀ a, x + a = y ↔ a = z) : (single x r * f : add_monoid_algebra k G) y = r * f z := @monoid_algebra.single_mul_apply_aux k (multiplicative G) _ _ _ _ _ _ _ H lemma single_zero_mul_apply [add_zero_class G] (f : add_monoid_algebra k G) (r : k) (x : G) : (single 0 r * f : add_monoid_algebra k G) x = r * f x := f.single_mul_apply_aux r _ _ _ $ λ a, by rw [zero_add] lemma single_mul_apply_of_not_exists_add [has_add G] (r : k) {g g' : G} (x : add_monoid_algebra k G) (h : ¬∃ d, g' = g + d): (finsupp.single g r * x : add_monoid_algebra k G) g' = 0 := @monoid_algebra.single_mul_apply_of_not_exists_mul k (multiplicative G) _ _ _ _ _ _ h lemma mul_single_apply [add_group G] (f : add_monoid_algebra k G) (r : k) (x y : G) : (f * single x r) y = f (y - x) * r := (sub_eq_add_neg y x).symm ▸ @monoid_algebra.mul_single_apply k (multiplicative G) _ _ _ _ _ _ lemma single_mul_apply [add_group G] (r : k) (x : G) (f : add_monoid_algebra k G) (y : G) : (single x r * f : add_monoid_algebra k G) y = r * f (- x + y) := @monoid_algebra.single_mul_apply k (multiplicative G) _ _ _ _ _ _ lemma lift_nc_smul {R : Type*} [add_zero_class G] [semiring R] (f : k →+* R) (g : multiplicative G →* R) (c : k) (φ : monoid_algebra k G) : lift_nc (f : k →+ R) g (c • φ) = f c * lift_nc (f : k →+ R) g φ := @monoid_algebra.lift_nc_smul k (multiplicative G) _ _ _ _ f g c φ lemma induction_on [add_monoid G] {p : add_monoid_algebra k G → Prop} (f : add_monoid_algebra k G) (hM : ∀ g, p (of k G (multiplicative.of_add g))) (hadd : ∀ f g : add_monoid_algebra k G, p f → p g → p (f + g)) (hsmul : ∀ (r : k) f, p f → p (r • f)) : p f := begin refine finsupp.induction_linear f _ (λ f g hf hg, hadd f g hf hg) (λ g r, _), { simpa using hsmul 0 (of k G (multiplicative.of_add 0)) (hM 0) }, { convert hsmul r (of k G (multiplicative.of_add g)) (hM g), simp only [mul_one, to_add_of_add, smul_single', of_apply] }, end /-- If `f : G → H` is an additive homomorphism between two additive monoids, then `finsupp.map_domain f` is a ring homomorphism between their add monoid algebras. -/ @[simps] def map_domain_ring_hom (k : Type*) [semiring k] {H F : Type*} [add_monoid G] [add_monoid H] [add_monoid_hom_class F G H] (f : F) : add_monoid_algebra k G →+* add_monoid_algebra k H := { map_one' := map_domain_one f, map_mul' := λ x y, map_domain_mul f x y, ..(finsupp.map_domain.add_monoid_hom f : monoid_algebra k G →+ monoid_algebra k H) } end misc_theorems end add_monoid_algebra /-! #### Conversions between `add_monoid_algebra` and `monoid_algebra` We have not defined `add_monoid_algebra k G = monoid_algebra k (multiplicative G)` because historically this caused problems; since the changes that have made `nsmul` definitional, this would be possible, but for now we just contruct the ring isomorphisms using `ring_equiv.refl _`. -/ /-- The equivalence between `add_monoid_algebra` and `monoid_algebra` in terms of `multiplicative` -/ protected def add_monoid_algebra.to_multiplicative [semiring k] [has_add G] : add_monoid_algebra k G ≃+* monoid_algebra k (multiplicative G) := { to_fun := equiv_map_domain multiplicative.of_add, map_mul' := λ x y, begin repeat {rw equiv_map_domain_eq_map_domain}, dsimp [multiplicative.of_add], convert monoid_algebra.map_domain_mul (mul_hom.id (multiplicative G)) _ _, end, ..finsupp.dom_congr multiplicative.of_add } /-- The equivalence between `monoid_algebra` and `add_monoid_algebra` in terms of `additive` -/ protected def monoid_algebra.to_additive [semiring k] [has_mul G] : monoid_algebra k G ≃+* add_monoid_algebra k (additive G) := { to_fun := equiv_map_domain additive.of_mul, map_mul' := λ x y, begin repeat {rw equiv_map_domain_eq_map_domain}, dsimp [additive.of_mul], convert monoid_algebra.map_domain_mul (mul_hom.id G) _ _, end, ..finsupp.dom_congr additive.of_mul } namespace add_monoid_algebra variables {k G} /-! #### Non-unital, non-associative algebra structure -/ section non_unital_non_assoc_algebra variables (k) [semiring k] [distrib_smul R k] [has_add G] instance is_scalar_tower_self [is_scalar_tower R k k] : is_scalar_tower R (add_monoid_algebra k G) (add_monoid_algebra k G) := @monoid_algebra.is_scalar_tower_self k (multiplicative G) R _ _ _ _ /-- Note that if `k` is a `comm_semiring` then we have `smul_comm_class k k k` and so we can take `R = k` in the below. In other words, if the coefficients are commutative amongst themselves, they also commute with the algebra multiplication. -/ instance smul_comm_class_self [smul_comm_class R k k] : smul_comm_class R (add_monoid_algebra k G) (add_monoid_algebra k G) := @monoid_algebra.smul_comm_class_self k (multiplicative G) R _ _ _ _ instance smul_comm_class_symm_self [smul_comm_class k R k] : smul_comm_class (add_monoid_algebra k G) R (add_monoid_algebra k G) := @monoid_algebra.smul_comm_class_symm_self k (multiplicative G) R _ _ _ _ variables {A : Type u₃} [non_unital_non_assoc_semiring A] /-- A non_unital `k`-algebra homomorphism from `add_monoid_algebra k G` is uniquely defined by its values on the functions `single a 1`. -/ lemma non_unital_alg_hom_ext [distrib_mul_action k A] {φ₁ φ₂ : add_monoid_algebra k G →ₙₐ[k] A} (h : ∀ x, φ₁ (single x 1) = φ₂ (single x 1)) : φ₁ = φ₂ := @monoid_algebra.non_unital_alg_hom_ext k (multiplicative G) _ _ _ _ _ φ₁ φ₂ h /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma non_unital_alg_hom_ext' [distrib_mul_action k A] {φ₁ φ₂ : add_monoid_algebra k G →ₙₐ[k] A} (h : φ₁.to_mul_hom.comp (of_magma k G) = φ₂.to_mul_hom.comp (of_magma k G)) : φ₁ = φ₂ := @monoid_algebra.non_unital_alg_hom_ext' k (multiplicative G) _ _ _ _ _ φ₁ φ₂ h /-- The functor `G ↦ add_monoid_algebra k G`, from the category of magmas to the category of non-unital, non-associative algebras over `k` is adjoint to the forgetful functor in the other direction. -/ @[simps] def lift_magma [module k A] [is_scalar_tower k A A] [smul_comm_class k A A] : (multiplicative G →ₙ* A) ≃ (add_monoid_algebra k G →ₙₐ[k] A) := { to_fun := λ f, { to_fun := λ a, sum a (λ m t, t • f (multiplicative.of_add m)), .. (monoid_algebra.lift_magma k f : _)}, inv_fun := λ F, F.to_mul_hom.comp (of_magma k G), .. (monoid_algebra.lift_magma k : (multiplicative G →ₙ* A) ≃ (_ →ₙₐ[k] A)) } end non_unital_non_assoc_algebra /-! #### Algebra structure -/ section algebra local attribute [reducible] add_monoid_algebra /-- `finsupp.single 0` as a `ring_hom` -/ @[simps] def single_zero_ring_hom [semiring k] [add_monoid G] : k →+* add_monoid_algebra k G := { map_one' := rfl, map_mul' := λ x y, by rw [single_add_hom, single_mul_single, zero_add], ..finsupp.single_add_hom 0} /-- If two ring homomorphisms from `add_monoid_algebra k G` are equal on all `single a 1` and `single 0 b`, then they are equal. -/ lemma ring_hom_ext {R} [semiring k] [add_monoid G] [semiring R] {f g : add_monoid_algebra k G →+* R} (h₀ : ∀ b, f (single 0 b) = g (single 0 b)) (h_of : ∀ a, f (single a 1) = g (single a 1)) : f = g := @monoid_algebra.ring_hom_ext k (multiplicative G) R _ _ _ _ _ h₀ h_of /-- If two ring homomorphisms from `add_monoid_algebra k G` are equal on all `single a 1` and `single 0 b`, then they are equal. See note [partially-applied ext lemmas]. -/ @[ext] lemma ring_hom_ext' {R} [semiring k] [add_monoid G] [semiring R] {f g : add_monoid_algebra k G →+* R} (h₁ : f.comp single_zero_ring_hom = g.comp single_zero_ring_hom) (h_of : (f : add_monoid_algebra k G →* R).comp (of k G) = (g : add_monoid_algebra k G →* R).comp (of k G)) : f = g := ring_hom_ext (ring_hom.congr_fun h₁) (monoid_hom.congr_fun h_of) section opposite open finsupp mul_opposite variables [semiring k] /-- The opposite of an `add_monoid_algebra R I` is ring equivalent to the `add_monoid_algebra Rᵐᵒᵖ I` over the opposite ring, taking elements to their opposite. -/ @[simps {simp_rhs := tt}] protected noncomputable def op_ring_equiv [add_comm_monoid G] : (add_monoid_algebra k G)ᵐᵒᵖ ≃+* add_monoid_algebra kᵐᵒᵖ G := { map_mul' := begin dsimp only [add_equiv.to_fun_eq_coe, ←add_equiv.coe_to_add_monoid_hom], rw add_monoid_hom.map_mul_iff, ext i r i' r' : 6, dsimp, simp only [map_range_single, single_mul_single, ←op_mul, add_comm] end, ..mul_opposite.op_add_equiv.symm.trans (finsupp.map_range.add_equiv (mul_opposite.op_add_equiv : k ≃+ kᵐᵒᵖ))} @[simp] lemma op_ring_equiv_single [add_comm_monoid G] (r : k) (x : G) : add_monoid_algebra.op_ring_equiv (op (single x r)) = single x (op r) := by simp @[simp] lemma op_ring_equiv_symm_single [add_comm_monoid G] (r : kᵐᵒᵖ) (x : Gᵐᵒᵖ) : add_monoid_algebra.op_ring_equiv.symm (single x r) = op (single x r.unop) := by simp end opposite /-- The instance `algebra R (add_monoid_algebra k G)` whenever we have `algebra R k`. In particular this provides the instance `algebra k (add_monoid_algebra k G)`. -/ instance [comm_semiring R] [semiring k] [algebra R k] [add_monoid G] : algebra R (add_monoid_algebra k G) := { smul_def' := λ r a, by { ext, simp [single_zero_mul_apply, algebra.smul_def, pi.smul_apply], }, commutes' := λ r f, by { ext, simp [single_zero_mul_apply, mul_single_zero_apply, algebra.commutes], }, ..single_zero_ring_hom.comp (algebra_map R k) } /-- `finsupp.single 0` as a `alg_hom` -/ @[simps] def single_zero_alg_hom [comm_semiring R] [semiring k] [algebra R k] [add_monoid G] : k →ₐ[R] add_monoid_algebra k G := { commutes' := λ r, by { ext, simp, refl, }, ..single_zero_ring_hom} @[simp] lemma coe_algebra_map [comm_semiring R] [semiring k] [algebra R k] [add_monoid G] : (algebra_map R (add_monoid_algebra k G) : R → add_monoid_algebra k G) = single 0 ∘ (algebra_map R k) := rfl end algebra section lift variables {k G} [comm_semiring k] [add_monoid G] variables {A : Type u₃} [semiring A] [algebra k A] {B : Type*} [semiring B] [algebra k B] /-- `lift_nc_ring_hom` as a `alg_hom`, for when `f` is an `alg_hom` -/ def lift_nc_alg_hom (f : A →ₐ[k] B) (g : multiplicative G →* B) (h_comm : ∀ x y, commute (f x) (g y)) : add_monoid_algebra A G →ₐ[k] B := { to_fun := lift_nc_ring_hom (f : A →+* B) g h_comm, commutes' := by simp [lift_nc_ring_hom], ..(lift_nc_ring_hom (f : A →+* B) g h_comm)} /-- A `k`-algebra homomorphism from `monoid_algebra k G` is uniquely defined by its values on the functions `single a 1`. -/ lemma alg_hom_ext ⦃φ₁ φ₂ : add_monoid_algebra k G →ₐ[k] A⦄ (h : ∀ x, φ₁ (single x 1) = φ₂ (single x 1)) : φ₁ = φ₂ := @monoid_algebra.alg_hom_ext k (multiplicative G) _ _ _ _ _ _ _ h /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma alg_hom_ext' ⦃φ₁ φ₂ : add_monoid_algebra k G →ₐ[k] A⦄ (h : (φ₁ : add_monoid_algebra k G →* A).comp (of k G) = (φ₂ : add_monoid_algebra k G →* A).comp (of k G)) : φ₁ = φ₂ := alg_hom_ext $ monoid_hom.congr_fun h variables (k G A) /-- Any monoid homomorphism `G →* A` can be lifted to an algebra homomorphism `monoid_algebra k G →ₐ[k] A`. -/ def lift : (multiplicative G →* A) ≃ (add_monoid_algebra k G →ₐ[k] A) := { inv_fun := λ f, (f : add_monoid_algebra k G →* A).comp (of k G), to_fun := λ F, { to_fun := lift_nc_alg_hom (algebra.of_id k A) F $ λ _ _, algebra.commutes _ _, .. @monoid_algebra.lift k (multiplicative G) _ _ A _ _ F}, .. @monoid_algebra.lift k (multiplicative G) _ _ A _ _ } variables {k G A} lemma lift_apply' (F : multiplicative G →* A) (f : monoid_algebra k G) : lift k G A F f = f.sum (λ a b, (algebra_map k A b) * F (multiplicative.of_add a)) := rfl lemma lift_apply (F : multiplicative G →* A) (f : monoid_algebra k G) : lift k G A F f = f.sum (λ a b, b • F (multiplicative.of_add a)) := by simp only [lift_apply', algebra.smul_def] lemma lift_def (F : multiplicative G →* A) : ⇑(lift k G A F) = lift_nc ((algebra_map k A : k →+* A) : k →+ A) F := rfl @[simp] lemma lift_symm_apply (F : add_monoid_algebra k G →ₐ[k] A) (x : multiplicative G) : (lift k G A).symm F x = F (single x.to_add 1) := rfl lemma lift_of (F : multiplicative G →* A) (x : multiplicative G) : lift k G A F (of k G x) = F x := by rw [of_apply, ← lift_symm_apply, equiv.symm_apply_apply] @[simp] lemma lift_single (F : multiplicative G →* A) (a b) : lift k G A F (single a b) = b • F (multiplicative.of_add a) := by rw [lift_def, lift_nc_single, algebra.smul_def, ring_hom.coe_add_monoid_hom] lemma lift_unique' (F : add_monoid_algebra k G →ₐ[k] A) : F = lift k G A ((F : add_monoid_algebra k G →* A).comp (of k G)) := ((lift k G A).apply_symm_apply F).symm /-- Decomposition of a `k`-algebra homomorphism from `monoid_algebra k G` by its values on `F (single a 1)`. -/ lemma lift_unique (F : add_monoid_algebra k G →ₐ[k] A) (f : monoid_algebra k G) : F f = f.sum (λ a b, b • F (single a 1)) := by conv_lhs { rw lift_unique' F, simp [lift_apply] } lemma alg_hom_ext_iff {φ₁ φ₂ : add_monoid_algebra k G →ₐ[k] A} : (∀ x, φ₁ (finsupp.single x 1) = φ₂ (finsupp.single x 1)) ↔ φ₁ = φ₂ := ⟨λ h, alg_hom_ext h, by rintro rfl _; refl⟩ end lift section local attribute [reducible] add_monoid_algebra universe ui variable {ι : Type ui} lemma prod_single [comm_semiring k] [add_comm_monoid G] {s : finset ι} {a : ι → G} {b : ι → k} : (∏ i in s, single (a i) (b i)) = single (∑ i in s, a i) (∏ i in s, b i) := finset.cons_induction_on s rfl $ λ a s has ih, by rw [prod_cons has, ih, single_mul_single, sum_cons has, prod_cons has] end lemma map_domain_algebra_map {A H F : Type*} [comm_semiring k] [semiring A] [algebra k A] [add_monoid G] [add_monoid H] [add_monoid_hom_class F G H] (f : F) (r : k) : map_domain f (algebra_map k (add_monoid_algebra A G) r) = algebra_map k (add_monoid_algebra A H) r := by simp only [function.comp_app, map_domain_single, add_monoid_algebra.coe_algebra_map, map_zero] /-- If `f : G → H` is a homomorphism between two additive magmas, then `finsupp.map_domain f` is a non-unital algebra homomorphism between their additive magma algebras. -/ @[simps] def map_domain_non_unital_alg_hom (k A : Type*) [comm_semiring k] [semiring A] [algebra k A] {G H F : Type*} [has_add G] [has_add H] [add_hom_class F G H] (f : F) : add_monoid_algebra A G →ₙₐ[k] add_monoid_algebra A H := { map_mul' := λ x y, map_domain_mul f x y, map_smul' := λ r x, map_domain_smul r x, ..(finsupp.map_domain.add_monoid_hom f : monoid_algebra A G →+ monoid_algebra A H) } /-- If `f : G → H` is an additive homomorphism between two additive monoids, then `finsupp.map_domain f` is an algebra homomorphism between their add monoid algebras. -/ @[simps] def map_domain_alg_hom (k A : Type*) [comm_semiring k] [semiring A] [algebra k A] [add_monoid G] {H F : Type*} [add_monoid H] [add_monoid_hom_class F G H] (f : F) : add_monoid_algebra A G →ₐ[k] add_monoid_algebra A H := { commutes' := map_domain_algebra_map f, ..map_domain_ring_hom A f} end add_monoid_algebra variables [comm_semiring R] (k G) /-- The algebra equivalence between `add_monoid_algebra` and `monoid_algebra` in terms of `multiplicative`. -/ def add_monoid_algebra.to_multiplicative_alg_equiv [semiring k] [algebra R k] [add_monoid G] : add_monoid_algebra k G ≃ₐ[R] monoid_algebra k (multiplicative G) := { commutes' := λ r, by simp [add_monoid_algebra.to_multiplicative], ..add_monoid_algebra.to_multiplicative k G } /-- The algebra equivalence between `monoid_algebra` and `add_monoid_algebra` in terms of `additive`. -/ def monoid_algebra.to_additive_alg_equiv [semiring k] [algebra R k] [monoid G] : monoid_algebra k G ≃ₐ[R] add_monoid_algebra k (additive G) := { commutes' := λ r, by simp [monoid_algebra.to_additive], ..monoid_algebra.to_additive k G }
8dc471a0c2e28cf656e5ece9d9f6b977a76817a7
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/order/ideal.lean
78cc1626d0bc213bd5c634f2cadf897374ffa2dd
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,484
lean
/- Copyright (c) 2020 David Wärn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Wärn -/ import order.basic import data.equiv.encodable.basic import order.atoms /-! # Order ideals, cofinal sets, and the Rasiowa–Sikorski lemma ## Main definitions Throughout this file, `P` is at least a preorder, but some sections require more structure, such as a bottom element, a top element, or a join-semilattice structure. - `order.ideal P`: the type of nonempty, upward directed, and downward closed subsets of `P`. Dual to the notion of a filter on a preorder. - `order.is_ideal P`: a predicate for when a `set P` is an ideal. - `order.ideal.principal p`: the principal ideal generated by `p : P`. - `order.ideal.is_proper P`: a predicate for proper ideals. Dual to the notion of a proper filter. - `order.ideal.is_maximal`: a predicate for maximal ideals. Dual to the notion of an ultrafilter. - `ideal_inter_nonempty P`: a predicate for when the intersection of any two ideals of `P` is nonempty. - `order.cofinal P`: the type of subsets of `P` containing arbitrarily large elements. Dual to the notion of 'dense set' used in forcing. - `order.ideal_of_cofinals p 𝒟`, where `p : P`, and `𝒟` is a countable family of cofinal subsets of P: an ideal in `P` which contains `p` and intersects every set in `𝒟`. (This a form of the Rasiowa–Sikorski lemma.) ## References - <https://en.wikipedia.org/wiki/Ideal_(order_theory)> - <https://en.wikipedia.org/wiki/Cofinal_(mathematics)> - <https://en.wikipedia.org/wiki/Rasiowa%E2%80%93Sikorski_lemma> Note that for the Rasiowa–Sikorski lemma, Wikipedia uses the opposite ordering on `P`, in line with most presentations of forcing. ## Tags ideal, cofinal, dense, countable, generic -/ namespace order variables {P : Type*} /-- An ideal on a preorder `P` is a subset of `P` that is - nonempty - upward directed (any pair of elements in the ideal has an upper bound in the ideal) - downward closed (any element less than an element of the ideal is in the ideal). -/ structure ideal (P) [preorder P] := (carrier : set P) (nonempty : carrier.nonempty) (directed : directed_on (≤) carrier) (mem_of_le : ∀ {x y : P}, x ≤ y → y ∈ carrier → x ∈ carrier) /-- A subset of a preorder `P` is an ideal if it is - nonempty - upward directed (any pair of elements in the ideal has an upper bound in the ideal) - downward closed (any element less than an element of the ideal is in the ideal). -/ @[mk_iff] structure is_ideal {P} [preorder P] (I : set P) : Prop := (nonempty : I.nonempty) (directed : directed_on (≤) I) (mem_of_le : ∀ {x y : P}, x ≤ y → y ∈ I → x ∈ I) /-- Create an element of type `order.ideal` from a set satisfying the predicate `order.is_ideal`. -/ def is_ideal.to_ideal [preorder P] {I : set P} (h : is_ideal I) : ideal P := ⟨I, h.1, h.2, h.3⟩ /-- A preorder `P` has the `ideal_inter_nonempty` property if the intersection of any two ideals is nonempty. Most importantly, a `semilattice_sup` preorder with this property satisfies that its ideal poset is a lattice. -/ class ideal_inter_nonempty (P) [preorder P] : Prop := (inter_nonempty : ∀ (I J : ideal P), (I.carrier ∩ J.carrier).nonempty) lemma inter_nonempty [preorder P] [ideal_inter_nonempty P] : ∀ (I J : ideal P), (I.carrier ∩ J.carrier).nonempty := ideal_inter_nonempty.inter_nonempty namespace ideal section preorder variables [preorder P] {x y : P} {I J : ideal P} /-- The smallest ideal containing a given element. -/ def principal (p : P) : ideal P := { carrier := { x | x ≤ p }, nonempty := ⟨p, le_refl _⟩, directed := λ x hx y hy, ⟨p, le_refl _, hx, hy⟩, mem_of_le := λ x y hxy hy, le_trans hxy hy, } instance [inhabited P] : inhabited (ideal P) := ⟨ideal.principal $ default P⟩ /-- An ideal of `P` can be viewed as a subset of `P`. -/ instance : has_coe (ideal P) (set P) := ⟨carrier⟩ /-- For the notation `x ∈ I`. -/ instance : has_mem P (ideal P) := ⟨λ x I, x ∈ (I : set P)⟩ @[simp] lemma mem_coe : x ∈ (I : set P) ↔ x ∈ I := iff_of_eq rfl @[simp] lemma mem_principal : x ∈ principal y ↔ x ≤ y := by refl /-- Two ideals are equal when their underlying sets are equal. -/ @[ext] lemma ext : ∀ (I J : ideal P), (I : set P) = J → I = J | ⟨_, _, _, _⟩ ⟨_, _, _, _⟩ rfl := rfl @[simp, norm_cast] lemma ext_set_eq {I J : ideal P} : (I : set P) = J ↔ I = J := ⟨by ext, congr_arg _⟩ lemma ext'_iff {I J : ideal P} : I = J ↔ (I : set P) = J := ext_set_eq.symm lemma is_ideal (I : ideal P) : is_ideal (I : set P) := ⟨I.2, I.3, I.4⟩ /-- The partial ordering by subset inclusion, inherited from `set P`. -/ instance : partial_order (ideal P) := partial_order.lift coe ext @[trans] lemma mem_of_mem_of_le : x ∈ I → I ≤ J → x ∈ J := @set.mem_of_mem_of_subset P x I J @[simp] lemma principal_le_iff : principal x ≤ I ↔ x ∈ I := ⟨λ (h : ∀ {y}, y ≤ x → y ∈ I), h (le_refl x), λ h_mem y (h_le : y ≤ x), I.mem_of_le h_le h_mem⟩ lemma mem_compl_of_ge {x y : P} : x ≤ y → x ∈ (I : set P)ᶜ → y ∈ (I : set P)ᶜ := λ h, mt (I.mem_of_le h) /-- A proper ideal is one that is not the whole set. Note that the whole set might not be an ideal. -/ @[mk_iff] class is_proper (I : ideal P) : Prop := (ne_univ : (I : set P) ≠ set.univ) lemma is_proper_of_not_mem {I : ideal P} {p : P} (nmem : p ∉ I) : is_proper I := ⟨λ hp, begin change p ∉ ↑I at nmem, rw hp at nmem, exact nmem (set.mem_univ p), end⟩ /-- An ideal is maximal if it is maximal in the collection of proper ideals. Note that we cannot use the `is_coatom` class because `P` might not have a `top` element. -/ @[mk_iff] class is_maximal (I : ideal P) extends is_proper I : Prop := (maximal_proper : ∀ ⦃J : ideal P⦄, I < J → J.carrier = set.univ) end preorder section order_bot variables [order_bot P] {I : ideal P} /-- A specific witness of `I.nonempty` when `P` has a bottom element. -/ @[simp] lemma bot_mem : ⊥ ∈ I := I.mem_of_le bot_le I.nonempty.some_mem /-- There is a bottom ideal when `P` has a bottom element. -/ instance : order_bot (ideal P) := { bot := principal ⊥, bot_le := by simp, .. ideal.partial_order } end order_bot section order_top variables [order_top P] /-- There is a top ideal when `P` has a top element. -/ instance : order_top (ideal P) := { top := principal ⊤, le_top := λ I x h, le_top, .. ideal.partial_order } @[simp] lemma top_carrier : (⊤ : ideal P).carrier = set.univ := set.univ_subset_iff.1 (λ p _, le_top) @[simp] lemma top_coe : ((⊤ : ideal P) : set P) = set.univ := top_carrier lemma top_of_mem_top {I : ideal P} (mem_top : ⊤ ∈ I) : I = ⊤ := begin ext, change x ∈ I.carrier ↔ x ∈ (⊤ : ideal P).carrier, split, { simp [top_carrier] }, { exact λ _, I.mem_of_le le_top mem_top } end lemma is_proper_of_ne_top {I : ideal P} (ne_top : I ≠ ⊤) : is_proper I := is_proper_of_not_mem (λ h, ne_top (top_of_mem_top h)) lemma is_proper.ne_top {I : ideal P} (hI : is_proper I) : I ≠ ⊤ := begin intro h, rw [ext'_iff, top_coe] at h, apply hI.ne_univ, assumption, end lemma _root_.is_coatom.is_proper {I : ideal P} (hI : is_coatom I) : is_proper I := is_proper_of_ne_top hI.1 lemma is_proper_iff_ne_top {I : ideal P} : is_proper I ↔ I ≠ ⊤ := ⟨λ h, h.ne_top, λ h, is_proper_of_ne_top h⟩ lemma is_maximal.is_coatom {I : ideal P} (h : is_maximal I) : is_coatom I := ⟨is_maximal.to_is_proper.ne_top, λ _ _, by { rw [ext'_iff, top_coe], exact is_maximal.maximal_proper ‹_› }⟩ lemma is_maximal.is_coatom' {I : ideal P} [is_maximal I] : is_coatom I := is_maximal.is_coatom ‹_› lemma _root_.is_coatom.is_maximal {I : ideal P} (hI : is_coatom I) : is_maximal I := { maximal_proper := λ _ _, by simp [hI.2 _ ‹_›], ..is_coatom.is_proper ‹_› } lemma is_maximal_iff_is_coatom {I : ideal P} : is_maximal I ↔ is_coatom I := ⟨λ h, h.is_coatom, λ h, h.is_maximal⟩ end order_top section semilattice_sup variables [semilattice_sup P] {x y : P} {I : ideal P} /-- A specific witness of `I.directed` when `P` has joins. -/ lemma sup_mem (x y ∈ I) : x ⊔ y ∈ I := let ⟨z, h_mem, hx, hy⟩ := I.directed x ‹_› y ‹_› in I.mem_of_le (sup_le hx hy) h_mem @[simp] lemma sup_mem_iff : x ⊔ y ∈ I ↔ x ∈ I ∧ y ∈ I := ⟨λ h, ⟨I.mem_of_le le_sup_left h, I.mem_of_le le_sup_right h⟩, λ h, sup_mem x y h.left h.right⟩ end semilattice_sup section semilattice_sup_ideal_inter_nonempty variables [semilattice_sup P] [ideal_inter_nonempty P] {x : P} {I J K : ideal P} /-- The intersection of two ideals is an ideal, when it is nonempty and `P` has joins. -/ def inf (I J : ideal P) : ideal P := { carrier := I ∩ J, nonempty := inter_nonempty I J, directed := λ x ⟨_, _⟩ y ⟨_, _⟩, ⟨x ⊔ y, ⟨sup_mem x y ‹_› ‹_›, sup_mem x y ‹_› ‹_›⟩, by simp⟩, mem_of_le := λ x y h ⟨_, _⟩, ⟨mem_of_le I h ‹_›, mem_of_le J h ‹_›⟩ } /-- There is a smallest ideal containing two ideals, when their intersection is nonempty and `P` has joins. -/ def sup (I J : ideal P) : ideal P := { carrier := {x | ∃ (i ∈ I) (j ∈ J), x ≤ i ⊔ j}, nonempty := by { cases inter_nonempty I J, exact ⟨w, w, h.1, w, h.2, le_sup_left⟩ }, directed := λ x ⟨xi, _, xj, _, _⟩ y ⟨yi, _, yj, _, _⟩, ⟨x ⊔ y, ⟨xi ⊔ yi, sup_mem xi yi ‹_› ‹_›, xj ⊔ yj, sup_mem xj yj ‹_› ‹_›, sup_le (calc x ≤ xi ⊔ xj : ‹_› ... ≤ (xi ⊔ yi) ⊔ (xj ⊔ yj) : sup_le_sup le_sup_left le_sup_left) (calc y ≤ yi ⊔ yj : ‹_› ... ≤ (xi ⊔ yi) ⊔ (xj ⊔ yj) : sup_le_sup le_sup_right le_sup_right)⟩, le_sup_left, le_sup_right⟩, mem_of_le := λ x y _ ⟨yi, _, yj, _, _⟩, ⟨yi, ‹_›, yj, ‹_›, le_trans ‹x ≤ y› ‹_›⟩ } lemma sup_le : I ≤ K → J ≤ K → sup I J ≤ K := λ hIK hJK x ⟨i, hiI, j, hjJ, hxij⟩, K.mem_of_le hxij $ sup_mem i j (mem_of_mem_of_le hiI hIK) (mem_of_mem_of_le hjJ hJK) instance : lattice (ideal P) := { sup := sup, le_sup_left := λ I J (i ∈ I), by { cases nonempty J, exact ⟨i, ‹_›, w, ‹_›, le_sup_left⟩ }, le_sup_right := λ I J (j ∈ J), by { cases nonempty I, exact ⟨w, ‹_›, j, ‹_›, le_sup_right⟩ }, sup_le := @sup_le _ _ _, inf := inf, inf_le_left := λ I J, set.inter_subset_left I J, inf_le_right := λ I J, set.inter_subset_right I J, le_inf := λ I J K, set.subset_inter, .. ideal.partial_order } @[simp] lemma mem_inf : x ∈ I ⊓ J ↔ x ∈ I ∧ x ∈ J := iff_of_eq rfl @[simp] lemma mem_sup : x ∈ I ⊔ J ↔ ∃ (i ∈ I) (j ∈ J), x ≤ i ⊔ j := iff_of_eq rfl lemma lt_sup_principal_of_not_mem (hx : x ∉ I) : I < I ⊔ principal x := begin apply lt_of_le_of_ne le_sup_left, intro h, simp at h, exact hx h end end semilattice_sup_ideal_inter_nonempty section semilattice_sup_bot variables [semilattice_sup_bot P] @[priority 100] instance semilattice_sup_bot.ideal_inter_nonempty : ideal_inter_nonempty P := { inter_nonempty := λ _ _, ⟨⊥, ⟨bot_mem, bot_mem⟩⟩ } end semilattice_sup_bot section semilattice_inf variable [semilattice_inf P] @[priority 100] instance semilattice_inf.ideal_inter_nonempty : ideal_inter_nonempty P := { inter_nonempty := λ I J, begin cases I.nonempty with i _, cases J.nonempty with j _, exact ⟨i ⊓ j, I.mem_of_le inf_le_left ‹_›, J.mem_of_le inf_le_right ‹_›⟩ end } end semilattice_inf section distrib_lattice variables [distrib_lattice P] variables {I J : ideal P} lemma eq_sup_of_le_sup {x i j: P} (hi : i ∈ I) (hj : j ∈ J) (hx : x ≤ i ⊔ j): ∃ (i' ∈ I) (j' ∈ J), x = i' ⊔ j' := begin refine ⟨x ⊓ i, I.mem_of_le inf_le_right hi, x ⊓ j, J.mem_of_le inf_le_right hj, _⟩, calc x = x ⊓ (i ⊔ j) : left_eq_inf.mpr hx ... = (x ⊓ i) ⊔ (x ⊓ j) : inf_sup_left, end lemma coe_sup_eq : ↑(I ⊔ J) = {x | ∃ i ∈ I, ∃ j ∈ J, x = i ⊔ j} := begin ext, rw [mem_coe, mem_sup], exact ⟨λ ⟨_, _, _, _, _⟩, eq_sup_of_le_sup ‹_› ‹_› ‹_›, λ ⟨i, _, j, _, _⟩, ⟨i, ‹_›, j, ‹_›, le_of_eq ‹_›⟩⟩ end end distrib_lattice end ideal /-- For a preorder `P`, `cofinal P` is the type of subsets of `P` containing arbitrarily large elements. They are the dense sets in the topology whose open sets are terminal segments. -/ structure cofinal (P) [preorder P] := (carrier : set P) (mem_gt : ∀ x : P, ∃ y ∈ carrier, x ≤ y) namespace cofinal variables [preorder P] instance : inhabited (cofinal P) := ⟨{ carrier := set.univ, mem_gt := λ x, ⟨x, trivial, le_refl _⟩ }⟩ instance : has_mem P (cofinal P) := ⟨λ x D, x ∈ D.carrier⟩ variables (D : cofinal P) (x : P) /-- A (noncomputable) element of a cofinal set lying above a given element. -/ noncomputable def above : P := classical.some $ D.mem_gt x lemma above_mem : D.above x ∈ D := exists.elim (classical.some_spec $ D.mem_gt x) $ λ a _, a lemma le_above : x ≤ D.above x := exists.elim (classical.some_spec $ D.mem_gt x) $ λ _ b, b end cofinal section ideal_of_cofinals variables [preorder P] (p : P) {ι : Type*} [encodable ι] (𝒟 : ι → cofinal P) /-- Given a starting point, and a countable family of cofinal sets, this is an increasing sequence that intersects each cofinal set. -/ noncomputable def sequence_of_cofinals : ℕ → P | 0 := p | (n+1) := match encodable.decode ι n with | none := sequence_of_cofinals n | some i := (𝒟 i).above (sequence_of_cofinals n) end lemma sequence_of_cofinals.monotone : monotone (sequence_of_cofinals p 𝒟) := by { apply monotone_nat_of_le_succ, intros n, dunfold sequence_of_cofinals, cases encodable.decode ι n, { refl }, { apply cofinal.le_above }, } lemma sequence_of_cofinals.encode_mem (i : ι) : sequence_of_cofinals p 𝒟 (encodable.encode i + 1) ∈ 𝒟 i := by { dunfold sequence_of_cofinals, rw encodable.encodek, apply cofinal.above_mem, } /-- Given an element `p : P` and a family `𝒟` of cofinal subsets of a preorder `P`, indexed by a countable type, `ideal_of_cofinals p 𝒟` is an ideal in `P` which - contains `p`, according to `mem_ideal_of_cofinals p 𝒟`, and - intersects every set in `𝒟`, according to `cofinal_meets_ideal_of_cofinals p 𝒟`. This proves the Rasiowa–Sikorski lemma. -/ def ideal_of_cofinals : ideal P := { carrier := { x : P | ∃ n, x ≤ sequence_of_cofinals p 𝒟 n }, nonempty := ⟨p, 0, le_refl _⟩, directed := λ x ⟨n, hn⟩ y ⟨m, hm⟩, ⟨_, ⟨max n m, le_refl _⟩, le_trans hn $ sequence_of_cofinals.monotone p 𝒟 (le_max_left _ _), le_trans hm $ sequence_of_cofinals.monotone p 𝒟 (le_max_right _ _) ⟩, mem_of_le := λ x y hxy ⟨n, hn⟩, ⟨n, le_trans hxy hn⟩, } lemma mem_ideal_of_cofinals : p ∈ ideal_of_cofinals p 𝒟 := ⟨0, le_refl _⟩ /-- `ideal_of_cofinals p 𝒟` is `𝒟`-generic. -/ lemma cofinal_meets_ideal_of_cofinals (i : ι) : ∃ x : P, x ∈ 𝒟 i ∧ x ∈ ideal_of_cofinals p 𝒟 := ⟨_, sequence_of_cofinals.encode_mem p 𝒟 i, _, le_refl _⟩ end ideal_of_cofinals end order
271e1bb6d6f824646b446901ce8338656f67bd01
08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4
/src/Lean/LocalContext.lean
21352f5b798cc67077d80a0f1dfa7ca112962fe3
[ "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
18,942
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.PersistentArray import Lean.Expr import Lean.Hygiene namespace Lean /-- Whether a local declaration should be found by type class search, tactics, etc. and shown in the goal display. -/ inductive LocalDeclKind /-- Participates fully in type class search, tactics, and is shown even if inaccessible. For example: the `x` in `fun x => _` has the default kind. -/ | default /-- Invisible to type class search or tactics, and hidden in the goal display. This kind is used for temporary variables in macros. For example: `return (← foo) + bar` expands to `foo >>= fun __tmp => pure (__tmp + bar)`, where `__tmp` has the `implDetail` kind. -/ | implDetail /-- Auxiliary local declaration for recursive calls. The behavior is similar to `implDetail`. For example: `def foo (n : Nat) : Nat := _` adds the local declaration `foo : Nat → Nat` to allow recursive calls. This declaration has the `auxDecl` kind. -/ | auxDecl deriving Inhabited, Repr, DecidableEq, Hashable /-- A declaration for a LocalContext. This is used to register which free variables are in scope. Each declaration comes with - `index` the position of the decl in the local context - `fvarId` the unique id of the free variables - `userName` the pretty-printable name of the variable - `type` the type. A `cdecl` is a local variable, a `ldecl` is a let-bound free variable with a `value : Expr`. -/ inductive LocalDecl where | cdecl (index : Nat) (fvarId : FVarId) (userName : Name) (type : Expr) (bi : BinderInfo) (kind : LocalDeclKind) | ldecl (index : Nat) (fvarId : FVarId) (userName : Name) (type : Expr) (value : Expr) (nonDep : Bool) (kind : LocalDeclKind) deriving Inhabited @[export lean_mk_local_decl] def mkLocalDeclEx (index : Nat) (fvarId : FVarId) (userName : Name) (type : Expr) (bi : BinderInfo) : LocalDecl := .cdecl index fvarId userName type bi .default @[export lean_mk_let_decl] def mkLetDeclEx (index : Nat) (fvarId : FVarId) (userName : Name) (type : Expr) (val : Expr) : LocalDecl := .ldecl index fvarId userName type val false .default @[export lean_local_decl_binder_info] def LocalDecl.binderInfoEx : LocalDecl → BinderInfo | .cdecl _ _ _ _ bi _ => bi | _ => BinderInfo.default namespace LocalDecl def isLet : LocalDecl → Bool | cdecl .. => false | ldecl .. => true def index : LocalDecl → Nat | cdecl (index := i) .. => i | ldecl (index := i) .. => i def setIndex : LocalDecl → Nat → LocalDecl | cdecl _ id n t bi k, idx => cdecl idx id n t bi k | ldecl _ id n t v nd k, idx => ldecl idx id n t v nd k def fvarId : LocalDecl → FVarId | cdecl (fvarId := id) .. => id | ldecl (fvarId := id) .. => id def userName : LocalDecl → Name | cdecl (userName := n) .. => n | ldecl (userName := n) .. => n def type : LocalDecl → Expr | cdecl (type := t) .. => t | ldecl (type := t) .. => t def setType : LocalDecl → Expr → LocalDecl | cdecl idx id n _ bi k, t => cdecl idx id n t bi k | ldecl idx id n _ v nd k, t => ldecl idx id n t v nd k def binderInfo : LocalDecl → BinderInfo | cdecl (bi := bi) .. => bi | ldecl .. => BinderInfo.default def kind : LocalDecl → LocalDeclKind | cdecl .. | ldecl .. => ‹_› def isAuxDecl (d : LocalDecl) : Bool := d.kind = .auxDecl /-- Is the local declaration an implementation-detail hypothesis (including auxiliary declarations)? -/ def isImplementationDetail (d : LocalDecl) : Bool := d.kind != .default def value? : LocalDecl → Option Expr | cdecl .. => none | ldecl (value := v) .. => some v def value : LocalDecl → Expr | cdecl .. => panic! "let declaration expected" | ldecl (value := v) .. => v def hasValue : LocalDecl → Bool | cdecl .. => false | ldecl .. => true def setValue : LocalDecl → Expr → LocalDecl | ldecl idx id n t _ nd k, v => ldecl idx id n t v nd k | d, _ => d def setUserName : LocalDecl → Name → LocalDecl | cdecl index id _ type bi k, userName => cdecl index id userName type bi k | ldecl index id _ type val nd k, userName => ldecl index id userName type val nd k def setBinderInfo : LocalDecl → BinderInfo → LocalDecl | cdecl index id n type _ k, bi => cdecl index id n type bi k | ldecl .., _ => panic! "unexpected let declaration" def toExpr (decl : LocalDecl) : Expr := mkFVar decl.fvarId def hasExprMVar : LocalDecl → Bool | cdecl (type := t) .. => t.hasExprMVar | ldecl (type := t) (value := v) .. => t.hasExprMVar || v.hasExprMVar end LocalDecl /-- A LocalContext is an ordered set of local variable declarations. It is used to store the free variables (also known as local constants) that are in scope. When inspecting a goal or expected type in the infoview, the local context is all of the variables above the `⊢` symbol. -/ structure LocalContext where fvarIdToDecl : PersistentHashMap FVarId LocalDecl := {} decls : PersistentArray (Option LocalDecl) := {} deriving Inhabited namespace LocalContext @[export lean_mk_empty_local_ctx] def mkEmpty : Unit → LocalContext := fun _ => {} def empty : LocalContext := {} @[export lean_local_ctx_is_empty] def isEmpty (lctx : LocalContext) : Bool := lctx.fvarIdToDecl.isEmpty /-- Low level API for creating local declarations. It is used to implement actions in the monads `Elab` and `Tactic`. It should not be used directly since the argument `(fvarId : FVarId)` is assumed to be unique. You can create a unique fvarId with `mkFreshFVarId`. -/ def mkLocalDecl (lctx : LocalContext) (fvarId : FVarId) (userName : Name) (type : Expr) (bi : BinderInfo := BinderInfo.default) (kind : LocalDeclKind := .default) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => let idx := decls.size let decl := LocalDecl.cdecl idx fvarId userName type bi kind { fvarIdToDecl := map.insert fvarId decl, decls := decls.push decl } @[export lean_local_ctx_mk_local_decl] private def mkLocalDeclExported (lctx : LocalContext) (fvarId : FVarId) (userName : Name) (type : Expr) (bi : BinderInfo) (kind : LocalDeclKind := .default) : LocalContext := mkLocalDecl lctx fvarId userName type bi kind /-- Low level API for let declarations. Do not use directly.-/ def mkLetDecl (lctx : LocalContext) (fvarId : FVarId) (userName : Name) (type : Expr) (value : Expr) (nonDep := false) (kind : LocalDeclKind := default) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => let idx := decls.size let decl := LocalDecl.ldecl idx fvarId userName type value nonDep kind { fvarIdToDecl := map.insert fvarId decl, decls := decls.push decl } @[export lean_local_ctx_mk_let_decl] private def mkLetDeclExported (lctx : LocalContext) (fvarId : FVarId) (userName : Name) (type : Expr) (value : Expr) (nonDep : Bool) : LocalContext := mkLetDecl lctx fvarId userName type value nonDep /-- Low level API for adding a local declaration. Do not use directly. -/ def addDecl (lctx : LocalContext) (newDecl : LocalDecl) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => let idx := decls.size let newDecl := newDecl.setIndex idx { fvarIdToDecl := map.insert newDecl.fvarId newDecl, decls := decls.push newDecl } @[export lean_local_ctx_find] def find? (lctx : LocalContext) (fvarId : FVarId) : Option LocalDecl := lctx.fvarIdToDecl.find? fvarId def findFVar? (lctx : LocalContext) (e : Expr) : Option LocalDecl := lctx.find? e.fvarId! def get! (lctx : LocalContext) (fvarId : FVarId) : LocalDecl := match lctx.find? fvarId with | some d => d | none => panic! "unknown free variable" /-- Gets the declaration for expression `e` in the local context. If `e` is not a free variable or not present then panics. -/ def getFVar! (lctx : LocalContext) (e : Expr) : LocalDecl := lctx.get! e.fvarId! def contains (lctx : LocalContext) (fvarId : FVarId) : Bool := lctx.fvarIdToDecl.contains fvarId /-- Returns true when the lctx contains the free variable `e`. Panics if `e` is not an fvar. -/ def containsFVar (lctx : LocalContext) (e : Expr) : Bool := lctx.contains e.fvarId! def getFVarIds (lctx : LocalContext) : Array FVarId := lctx.decls.foldl (init := #[]) fun r decl? => match decl? with | some decl => r.push decl.fvarId | none => r /-- Return all of the free variables in the given context. -/ def getFVars (lctx : LocalContext) : Array Expr := lctx.getFVarIds.map mkFVar private partial def popTailNoneAux (a : PArray (Option LocalDecl)) : PArray (Option LocalDecl) := if a.size == 0 then a else match a.get! (a.size - 1) with | none => popTailNoneAux a.pop | some _ => a @[export lean_local_ctx_erase] def erase (lctx : LocalContext) (fvarId : FVarId) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => match map.find? fvarId with | none => lctx | some decl => { fvarIdToDecl := map.erase fvarId, decls := popTailNoneAux (decls.set decl.index none) } def pop (lctx : LocalContext): LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => if decls.size == 0 then lctx else match decls.get! (decls.size - 1) with | none => lctx -- unreachable | some decl => { fvarIdToDecl := map.erase decl.fvarId, decls := popTailNoneAux decls.pop } def findFromUserName? (lctx : LocalContext) (userName : Name) : Option LocalDecl := lctx.decls.findSomeRev? fun decl => match decl with | none => none | some decl => if decl.userName == userName then some decl else none def usesUserName (lctx : LocalContext) (userName : Name) : Bool := (lctx.findFromUserName? userName).isSome private partial def getUnusedNameAux (lctx : LocalContext) (suggestion : Name) (i : Nat) : Name × Nat := let curr := suggestion.appendIndexAfter i if lctx.usesUserName curr then getUnusedNameAux lctx suggestion (i + 1) else (curr, i + 1) def getUnusedName (lctx : LocalContext) (suggestion : Name) : Name := let suggestion := suggestion.eraseMacroScopes if lctx.usesUserName suggestion then (getUnusedNameAux lctx suggestion 1).1 else suggestion def lastDecl (lctx : LocalContext) : Option LocalDecl := lctx.decls.get! (lctx.decls.size - 1) def setUserName (lctx : LocalContext) (fvarId : FVarId) (userName : Name) : LocalContext := let decl := lctx.get! fvarId let decl := decl.setUserName userName { fvarIdToDecl := lctx.fvarIdToDecl.insert decl.fvarId decl, decls := lctx.decls.set decl.index decl } def renameUserName (lctx : LocalContext) (fromName : Name) (toName : Name) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => match lctx.findFromUserName? fromName with | none => lctx | some decl => let decl := decl.setUserName toName; { fvarIdToDecl := map.insert decl.fvarId decl, decls := decls.set decl.index decl } /-- Low-level function for updating the local context. Assumptions about `f`, the resulting nested expressions must be definitionally equal to their original values, the `index` nor `fvarId` are modified. -/ @[inline] def modifyLocalDecl (lctx : LocalContext) (fvarId : FVarId) (f : LocalDecl → LocalDecl) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => match lctx.find? fvarId with | none => lctx | some decl => let decl := f decl { fvarIdToDecl := map.insert decl.fvarId decl decls := decls.set decl.index decl } def setBinderInfo (lctx : LocalContext) (fvarId : FVarId) (bi : BinderInfo) : LocalContext := modifyLocalDecl lctx fvarId fun decl => decl.setBinderInfo bi @[export lean_local_ctx_num_indices] def numIndices (lctx : LocalContext) : Nat := lctx.decls.size def getAt? (lctx : LocalContext) (i : Nat) : Option LocalDecl := lctx.decls.get! i @[specialize] def foldlM [Monad m] (lctx : LocalContext) (f : β → LocalDecl → m β) (init : β) (start : Nat := 0) : m β := lctx.decls.foldlM (init := init) (start := start) fun b decl => match decl with | none => pure b | some decl => f b decl @[specialize] def foldrM [Monad m] (lctx : LocalContext) (f : LocalDecl → β → m β) (init : β) : m β := lctx.decls.foldrM (init := init) fun decl b => match decl with | none => pure b | some decl => f decl b @[specialize] def forM [Monad m] (lctx : LocalContext) (f : LocalDecl → m PUnit) : m PUnit := lctx.decls.forM fun decl => match decl with | none => pure PUnit.unit | some decl => f decl @[specialize] def findDeclM? [Monad m] (lctx : LocalContext) (f : LocalDecl → m (Option β)) : m (Option β) := lctx.decls.findSomeM? fun decl => match decl with | none => pure none | some decl => f decl @[specialize] def findDeclRevM? [Monad m] (lctx : LocalContext) (f : LocalDecl → m (Option β)) : m (Option β) := lctx.decls.findSomeRevM? fun decl => match decl with | none => pure none | some decl => f decl instance : ForIn m LocalContext LocalDecl where forIn lctx init f := lctx.decls.forIn init fun d? b => match d? with | none => return ForInStep.yield b | some d => f d b @[inline] def foldl (lctx : LocalContext) (f : β → LocalDecl → β) (init : β) (start : Nat := 0) : β := Id.run <| lctx.foldlM f init start @[inline] def foldr (lctx : LocalContext) (f : LocalDecl → β → β) (init : β) : β := Id.run <| lctx.foldrM f init def size (lctx : LocalContext) : Nat := lctx.foldl (fun n _ => n+1) 0 @[inline] def findDecl? (lctx : LocalContext) (f : LocalDecl → Option β) : Option β := Id.run <| lctx.findDeclM? f @[inline] def findDeclRev? (lctx : LocalContext) (f : LocalDecl → Option β) : Option β := Id.run <| lctx.findDeclRevM? f partial def isSubPrefixOfAux (a₁ a₂ : PArray (Option LocalDecl)) (exceptFVars : Array Expr) (i j : Nat) : Bool := if i < a₁.size then match a₁[i]! with | none => isSubPrefixOfAux a₁ a₂ exceptFVars (i+1) j | some decl₁ => if exceptFVars.any fun fvar => fvar.fvarId! == decl₁.fvarId then isSubPrefixOfAux a₁ a₂ exceptFVars (i+1) j else if j < a₂.size then match a₂[j]! with | none => isSubPrefixOfAux a₁ a₂ exceptFVars i (j+1) | some decl₂ => if decl₁.fvarId == decl₂.fvarId then isSubPrefixOfAux a₁ a₂ exceptFVars (i+1) (j+1) else isSubPrefixOfAux a₁ a₂ exceptFVars i (j+1) else false else true /-- Given `lctx₁ - exceptFVars` of the form `(x_1 : A_1) ... (x_n : A_n)`, then return true iff there is a local context `B_1* (x_1 : A_1) ... B_n* (x_n : A_n)` which is a prefix of `lctx₂` where `B_i`'s are (possibly empty) sequences of local declarations. -/ def isSubPrefixOf (lctx₁ lctx₂ : LocalContext) (exceptFVars : Array Expr := #[]) : Bool := isSubPrefixOfAux lctx₁.decls lctx₂.decls exceptFVars 0 0 @[inline] def mkBinding (isLambda : Bool) (lctx : LocalContext) (xs : Array Expr) (b : Expr) : Expr := let b := b.abstract xs xs.size.foldRev (init := b) fun i b => let x := xs[i]! match lctx.findFVar? x with | some (.cdecl _ _ n ty bi _) => let ty := ty.abstractRange i xs; if isLambda then Lean.mkLambda n bi ty b else Lean.mkForall n bi ty b | some (.ldecl _ _ n ty val nonDep _) => if b.hasLooseBVar 0 then let ty := ty.abstractRange i xs let val := val.abstractRange i xs mkLet n ty val b nonDep else b.lowerLooseBVars 1 1 | none => panic! "unknown free variable" /-- Creates the expression `fun x₁ .. xₙ => b` for free variables `xs = #[x₁, .., xₙ]`, suitably abstracting `b` and the types for each of the `xᵢ`. -/ def mkLambda (lctx : LocalContext) (xs : Array Expr) (b : Expr) : Expr := mkBinding true lctx xs b /-- Creates the expression `(x₁:α₁) → .. → (xₙ:αₙ) → b` for free variables `xs = #[x₁, .., xₙ]`, suitably abstracting `b` and the types for each of the `xᵢ`, `αᵢ`. -/ def mkForall (lctx : LocalContext) (xs : Array Expr) (b : Expr) : Expr := mkBinding false lctx xs b @[inline] def anyM [Monad m] (lctx : LocalContext) (p : LocalDecl → m Bool) : m Bool := lctx.decls.anyM fun d => match d with | some decl => p decl | none => pure false @[inline] def allM [Monad m] (lctx : LocalContext) (p : LocalDecl → m Bool) : m Bool := lctx.decls.allM fun d => match d with | some decl => p decl | none => pure true /-- Return `true` if `lctx` contains a local declaration satisfying `p`. -/ @[inline] def any (lctx : LocalContext) (p : LocalDecl → Bool) : Bool := Id.run <| lctx.anyM p /-- Return `true` if all declarations in `lctx` satisfy `p`. -/ @[inline] def all (lctx : LocalContext) (p : LocalDecl → Bool) : Bool := Id.run <| lctx.allM p /-- If option `pp.sanitizeNames` is set to `true`, add tombstone to shadowed local declaration names and ones contains macroscopes. -/ def sanitizeNames (lctx : LocalContext) : StateM NameSanitizerState LocalContext := do let st ← get if !getSanitizeNames st.options then pure lctx else StateT.run' (s := ({} : NameSet)) <| lctx.decls.size.foldRevM (init := lctx) fun i lctx => do match lctx.decls[i]! with | none => pure lctx | some decl => if decl.userName.hasMacroScopes || (← get).contains decl.userName then do modify fun s => s.insert decl.userName let userNameNew ← liftM <| sanitizeName decl.userName pure <| lctx.setUserName decl.fvarId userNameNew else modify fun s => s.insert decl.userName pure lctx end LocalContext /-- Class used to denote that `m` has a local context. -/ class MonadLCtx (m : Type → Type) where getLCtx : m LocalContext export MonadLCtx (getLCtx) instance [MonadLift m n] [MonadLCtx m] : MonadLCtx n where getLCtx := liftM (getLCtx : m _) def LocalDecl.replaceFVarId (fvarId : FVarId) (e : Expr) (d : LocalDecl) : LocalDecl := if d.fvarId == fvarId then d else match d with | .cdecl idx id n type bi k => .cdecl idx id n (type.replaceFVarId fvarId e) bi k | .ldecl idx id n type val nonDep k => .ldecl idx id n (type.replaceFVarId fvarId e) (val.replaceFVarId fvarId e) nonDep k def LocalContext.replaceFVarId (fvarId : FVarId) (e : Expr) (lctx : LocalContext) : LocalContext := let lctx := lctx.erase fvarId { fvarIdToDecl := lctx.fvarIdToDecl.map (·.replaceFVarId fvarId e) decls := lctx.decls.map fun localDecl? => localDecl?.map (·.replaceFVarId fvarId e) } end Lean
e29b71dc87ff3a7f2961982ec7159e0523a119a5
680b0d1592ce164979dab866b232f6fa743f2cc8
/library/data/list/set.lean
aa3a0e2d85dffb8dce604d8a86854d1dc51b7504
[ "Apache-2.0" ]
permissive
syohex/lean
657428ab520f8277fc18cf04bea2ad200dbae782
081ad1212b686780f3ff8a6d0e5f8a1d29a7d8bc
refs/heads/master
1,611,274,838,635
1,452,668,188,000
1,452,668,188,000
49,562,028
0
0
null
1,452,675,604,000
1,452,675,602,000
null
UTF-8
Lean
false
false
37,033
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 Set-like operations on lists -/ import data.list.basic data.list.comb open nat function decidable helper_tactics eq.ops namespace list section erase variable {A : Type} variable [H : decidable_eq A] include H definition erase (a : A) : list A → list A | [] := [] | (b::l) := match H a b with | inl e := l | inr n := b :: erase l end lemma erase_nil (a : A) : erase a [] = [] := rfl lemma erase_cons_head (a : A) (l : list A) : erase a (a :: l) = l := show match H a a with | inl e := l | inr n := a :: erase a l end = l, by rewrite decidable_eq_inl_refl lemma erase_cons_tail {a b : A} (l : list A) : a ≠ b → erase a (b::l) = b :: erase a l := assume h : a ≠ b, show match H a b with | inl e := l | inr n₁ := b :: erase a l end = b :: erase a l, by rewrite (decidable_eq_inr_neg h) lemma length_erase_of_mem {a : A} : ∀ {l}, a ∈ l → length (erase a l) = pred (length l) | [] h := rfl | [x] h := by rewrite [mem_singleton h, erase_cons_head] | (x::y::xs) h := by_cases (suppose a = x, by rewrite [this, erase_cons_head]) (suppose a ≠ x, assert ainyxs : a ∈ y::xs, from or_resolve_right h this, by rewrite [erase_cons_tail _ this, *length_cons, length_erase_of_mem ainyxs]) lemma length_erase_of_not_mem {a : A} : ∀ {l}, a ∉ l → length (erase a l) = length l | [] h := rfl | (x::xs) h := assert anex : a ≠ x, from λ aeqx : a = x, absurd (or.inl aeqx) h, assert aninxs : a ∉ xs, from λ ainxs : a ∈ xs, absurd (or.inr ainxs) h, by rewrite [erase_cons_tail _ anex, length_cons, length_erase_of_not_mem aninxs] lemma erase_append_left {a : A} : ∀ {l₁} (l₂), a ∈ l₁ → erase a (l₁++l₂) = erase a l₁ ++ l₂ | [] l₂ h := absurd h !not_mem_nil | (x::xs) l₂ h := by_cases (λ aeqx : a = x, by rewrite [aeqx, append_cons, *erase_cons_head]) (λ anex : a ≠ x, assert ainxs : a ∈ xs, from mem_of_ne_of_mem anex h, by rewrite [append_cons, *erase_cons_tail _ anex, erase_append_left l₂ ainxs]) lemma erase_append_right {a : A} : ∀ {l₁} (l₂), a ∉ l₁ → erase a (l₁++l₂) = l₁ ++ erase a l₂ | [] l₂ h := rfl | (x::xs) l₂ h := by_cases (λ aeqx : a = x, by rewrite aeqx at h; exact (absurd !mem_cons h)) (λ anex : a ≠ x, assert nainxs : a ∉ xs, from not_mem_of_not_mem_cons h, by rewrite [append_cons, *erase_cons_tail _ anex, erase_append_right l₂ nainxs]) lemma erase_sub (a : A) : ∀ l, erase a l ⊆ l | [] := λ x xine, xine | (x::xs) := λ y xine, by_cases (λ aeqx : a = x, by rewrite [aeqx at xine, erase_cons_head at xine]; exact (or.inr xine)) (λ anex : a ≠ x, assert yinxe : y ∈ x :: erase a xs, by rewrite [erase_cons_tail _ anex at xine]; exact xine, assert subxs : erase a xs ⊆ xs, from erase_sub xs, by_cases (λ yeqx : y = x, by rewrite yeqx; apply mem_cons) (λ ynex : y ≠ x, assert yine : y ∈ erase a xs, from mem_of_ne_of_mem ynex yinxe, assert yinxs : y ∈ xs, from subxs yine, or.inr yinxs)) theorem mem_erase_of_ne_of_mem {a b : A} : ∀ {l : list A}, a ≠ b → a ∈ l → a ∈ erase b l | [] n i := absurd i !not_mem_nil | (c::l) n i := by_cases (λ beqc : b = c, assert ainl : a ∈ l, from or.elim (eq_or_mem_of_mem_cons i) (λ aeqc : a = c, absurd aeqc (beqc ▸ n)) (λ ainl : a ∈ l, ainl), by rewrite [beqc, erase_cons_head]; exact ainl) (λ bnec : b ≠ c, by_cases (λ aeqc : a = c, assert aux : a ∈ c :: erase b l, by rewrite [aeqc]; exact !mem_cons, by rewrite [erase_cons_tail _ bnec]; exact aux) (λ anec : a ≠ c, have ainl : a ∈ l, from mem_of_ne_of_mem anec i, have ainel : a ∈ erase b l, from mem_erase_of_ne_of_mem n ainl, assert aux : a ∈ c :: erase b l, from mem_cons_of_mem _ ainel, by rewrite [erase_cons_tail _ bnec]; exact aux)) -- theorem mem_of_mem_erase {a b : A} : ∀ {l}, a ∈ erase b l → a ∈ l | [] i := absurd i !not_mem_nil | (c::l) i := by_cases (λ beqc : b = c, by rewrite [beqc at i, erase_cons_head at i]; exact (mem_cons_of_mem _ i)) (λ bnec : b ≠ c, have i₁ : a ∈ c :: erase b l, by rewrite [erase_cons_tail _ bnec at i]; exact i, or.elim (eq_or_mem_of_mem_cons i₁) (λ aeqc : a = c, by rewrite [aeqc]; exact !mem_cons) (λ ainel : a ∈ erase b l, have ainl : a ∈ l, from mem_of_mem_erase ainel, mem_cons_of_mem _ ainl)) theorem all_erase_of_all {p : A → Prop} (a : A) : ∀ {l}, all l p → all (erase a l) p | [] h := by rewrite [erase_nil]; exact h | (b::l) h := assert h₁ : all l p, from all_of_all_cons h, have h₂ : all (erase a l) p, from all_erase_of_all h₁, have pb : p b, from of_all_cons h, assert h₃ : all (b :: erase a l) p, from all_cons_of_all pb h₂, by_cases (λ aeqb : a = b, by rewrite [aeqb, erase_cons_head]; exact h₁) (λ aneb : a ≠ b, by rewrite [erase_cons_tail _ aneb]; exact h₃) end erase /- disjoint -/ section disjoint variable {A : Type} definition disjoint (l₁ l₂ : list A) : Prop := ∀ ⦃a⦄, (a ∈ l₁ → a ∈ l₂ → false) lemma disjoint_left {l₁ l₂ : list A} : disjoint l₁ l₂ → ∀ {a}, a ∈ l₁ → a ∉ l₂ := λ d a, d a lemma disjoint_right {l₁ l₂ : list A} : disjoint l₁ l₂ → ∀ {a}, a ∈ l₂ → a ∉ l₁ := λ d a i₂ i₁, d a i₁ i₂ lemma disjoint.comm {l₁ l₂ : list A} : disjoint l₁ l₂ → disjoint l₂ l₁ := λ d a i₂ i₁, d a i₁ i₂ lemma disjoint_of_disjoint_cons_left {a : A} {l₁ l₂} : disjoint (a::l₁) l₂ → disjoint l₁ l₂ := λ d x xinl₁, disjoint_left d (or.inr xinl₁) lemma disjoint_of_disjoint_cons_right {a : A} {l₁ l₂} : disjoint l₁ (a::l₂) → disjoint l₁ l₂ := λ d, disjoint.comm (disjoint_of_disjoint_cons_left (disjoint.comm d)) lemma disjoint_nil_left (l : list A) : disjoint [] l := λ a ab, absurd ab !not_mem_nil lemma disjoint_nil_right (l : list A) : disjoint l [] := disjoint.comm (disjoint_nil_left l) lemma disjoint_cons_of_not_mem_of_disjoint {a : A} {l₁ l₂} : a ∉ l₂ → disjoint l₁ l₂ → disjoint (a::l₁) l₂ := λ nainl₂ d x (xinal₁ : x ∈ a::l₁), or.elim (eq_or_mem_of_mem_cons xinal₁) (λ xeqa : x = a, xeqa⁻¹ ▸ nainl₂) (λ xinl₁ : x ∈ l₁, disjoint_left d xinl₁) lemma disjoint_of_disjoint_append_left_left : ∀ {l₁ l₂ l : list A}, disjoint (l₁++l₂) l → disjoint l₁ l | [] l₂ l d := disjoint_nil_left l | (x::xs) l₂ l d := have nxinl : x ∉ l, from disjoint_left d !mem_cons, have d₁ : disjoint (xs++l₂) l, from disjoint_of_disjoint_cons_left d, have d₂ : disjoint xs l, from disjoint_of_disjoint_append_left_left d₁, disjoint_cons_of_not_mem_of_disjoint nxinl d₂ lemma disjoint_of_disjoint_append_left_right : ∀ {l₁ l₂ l : list A}, disjoint (l₁++l₂) l → disjoint l₂ l | [] l₂ l d := d | (x::xs) l₂ l d := have d₁ : disjoint (xs++l₂) l, from disjoint_of_disjoint_cons_left d, disjoint_of_disjoint_append_left_right d₁ lemma disjoint_of_disjoint_append_right_left : ∀ {l₁ l₂ l : list A}, disjoint l (l₁++l₂) → disjoint l l₁ := λ l₁ l₂ l d, disjoint.comm (disjoint_of_disjoint_append_left_left (disjoint.comm d)) lemma disjoint_of_disjoint_append_right_right : ∀ {l₁ l₂ l : list A}, disjoint l (l₁++l₂) → disjoint l l₂ := λ l₁ l₂ l d, disjoint.comm (disjoint_of_disjoint_append_left_right (disjoint.comm d)) end disjoint /- no duplicates predicate -/ inductive nodup {A : Type} : list A → Prop := | ndnil : nodup [] | ndcons : ∀ {a l}, a ∉ l → nodup l → nodup (a::l) section nodup open nodup variables {A B : Type} theorem nodup_nil : @nodup A [] := ndnil theorem nodup_cons {a : A} {l : list A} : a ∉ l → nodup l → nodup (a::l) := λ i n, ndcons i n theorem nodup_singleton (a : A) : nodup [a] := nodup_cons !not_mem_nil nodup_nil theorem nodup_of_nodup_cons : ∀ {a : A} {l : list A}, nodup (a::l) → nodup l | a xs (ndcons i n) := n theorem not_mem_of_nodup_cons : ∀ {a : A} {l : list A}, nodup (a::l) → a ∉ l | a xs (ndcons i n) := i theorem not_nodup_cons_of_mem {a : A} {l : list A} : a ∈ l → ¬ nodup (a :: l) := λ ainl d, absurd ainl (not_mem_of_nodup_cons d) theorem not_nodup_cons_of_not_nodup {a : A} {l : list A} : ¬ nodup l → ¬ nodup (a :: l) := λ nd d, absurd (nodup_of_nodup_cons d) nd theorem nodup_of_nodup_append_left : ∀ {l₁ l₂ : list A}, nodup (l₁++l₂) → nodup l₁ | [] l₂ n := nodup_nil | (x::xs) l₂ n := have ndxs : nodup xs, from nodup_of_nodup_append_left (nodup_of_nodup_cons n), have nxinxsl₂ : x ∉ xs++l₂, from not_mem_of_nodup_cons n, have nxinxs : x ∉ xs, from not_mem_of_not_mem_append_left nxinxsl₂, nodup_cons nxinxs ndxs theorem nodup_of_nodup_append_right : ∀ {l₁ l₂ : list A}, nodup (l₁++l₂) → nodup l₂ | [] l₂ n := n | (x::xs) l₂ n := nodup_of_nodup_append_right (nodup_of_nodup_cons n) theorem disjoint_of_nodup_append : ∀ {l₁ l₂ : list A}, nodup (l₁++l₂) → disjoint l₁ l₂ | [] l₂ d := disjoint_nil_left l₂ | (x::xs) l₂ d := have nodup (x::(xs++l₂)), from d, have x ∉ xs++l₂, from not_mem_of_nodup_cons this, have nxinl₂ : x ∉ l₂, from not_mem_of_not_mem_append_right this, take a, suppose a ∈ x::xs, or.elim (eq_or_mem_of_mem_cons this) (suppose a = x, this⁻¹ ▸ nxinl₂) (suppose ainxs : a ∈ xs, have nodup (x::(xs++l₂)), from d, have nodup (xs++l₂), from nodup_of_nodup_cons this, have disjoint xs l₂, from disjoint_of_nodup_append this, disjoint_left this ainxs) theorem nodup_append_of_nodup_of_nodup_of_disjoint : ∀ {l₁ l₂ : list A}, nodup l₁ → nodup l₂ → disjoint l₁ l₂ → nodup (l₁++l₂) | [] l₂ d₁ d₂ dsj := by rewrite [append_nil_left]; exact d₂ | (x::xs) l₂ d₁ d₂ dsj := have ndxs : nodup xs, from nodup_of_nodup_cons d₁, have disjoint xs l₂, from disjoint_of_disjoint_cons_left dsj, have ndxsl₂ : nodup (xs++l₂), from nodup_append_of_nodup_of_nodup_of_disjoint ndxs d₂ this, have nxinxs : x ∉ xs, from not_mem_of_nodup_cons d₁, have x ∉ l₂, from disjoint_left dsj !mem_cons, have x ∉ xs++l₂, from not_mem_append nxinxs this, nodup_cons this ndxsl₂ theorem nodup_app_comm {l₁ l₂ : list A} (d : nodup (l₁++l₂)) : nodup (l₂++l₁) := have d₁ : nodup l₁, from nodup_of_nodup_append_left d, have d₂ : nodup l₂, from nodup_of_nodup_append_right d, have dsj : disjoint l₁ l₂, from disjoint_of_nodup_append d, nodup_append_of_nodup_of_nodup_of_disjoint d₂ d₁ (disjoint.comm dsj) theorem nodup_head {a : A} {l₁ l₂ : list A} (d : nodup (l₁++(a::l₂))) : nodup (a::(l₁++l₂)) := have d₁ : nodup (a::(l₂++l₁)), from nodup_app_comm d, have d₂ : nodup (l₂++l₁), from nodup_of_nodup_cons d₁, have d₃ : nodup (l₁++l₂), from nodup_app_comm d₂, have nain : a ∉ l₂++l₁, from not_mem_of_nodup_cons d₁, have nain₂ : a ∉ l₂, from not_mem_of_not_mem_append_left nain, have nain₁ : a ∉ l₁, from not_mem_of_not_mem_append_right nain, nodup_cons (not_mem_append nain₁ nain₂) d₃ theorem nodup_middle {a : A} {l₁ l₂ : list A} (d : nodup (a::(l₁++l₂))) : nodup (l₁++(a::l₂)) := have d₁ : nodup (l₁++l₂), from nodup_of_nodup_cons d, have nain : a ∉ l₁++l₂, from not_mem_of_nodup_cons d, have disj : disjoint l₁ l₂, from disjoint_of_nodup_append d₁, have d₂ : nodup l₁, from nodup_of_nodup_append_left d₁, have d₃ : nodup l₂, from nodup_of_nodup_append_right d₁, have nain₂ : a ∉ l₂, from not_mem_of_not_mem_append_right nain, have nain₁ : a ∉ l₁, from not_mem_of_not_mem_append_left nain, have d₄ : nodup (a::l₂), from nodup_cons nain₂ d₃, have disj₂ : disjoint l₁ (a::l₂), from disjoint.comm (disjoint_cons_of_not_mem_of_disjoint nain₁ (disjoint.comm disj)), nodup_append_of_nodup_of_nodup_of_disjoint d₂ d₄ disj₂ theorem nodup_map {f : A → B} (inj : injective f) : ∀ {l : list A}, nodup l → nodup (map f l) | [] n := begin rewrite [map_nil], apply nodup_nil end | (x::xs) n := assert nxinxs : x ∉ xs, from not_mem_of_nodup_cons n, assert ndxs : nodup xs, from nodup_of_nodup_cons n, assert ndmfxs : nodup (map f xs), from nodup_map ndxs, assert nfxinm : f x ∉ map f xs, from λ ab : f x ∈ map f xs, obtain (y : A) (yinxs : y ∈ xs) (fyfx : f y = f x), from exists_of_mem_map ab, assert yeqx : y = x, from inj fyfx, by subst y; contradiction, nodup_cons nfxinm ndmfxs theorem nodup_erase_of_nodup [decidable_eq A] (a : A) : ∀ {l}, nodup l → nodup (erase a l) | [] n := nodup_nil | (b::l) n := by_cases (λ aeqb : a = b, by rewrite [aeqb, erase_cons_head]; exact (nodup_of_nodup_cons n)) (λ aneb : a ≠ b, have nbinl : b ∉ l, from not_mem_of_nodup_cons n, have ndl : nodup l, from nodup_of_nodup_cons n, have ndeal : nodup (erase a l), from nodup_erase_of_nodup ndl, have nbineal : b ∉ erase a l, from λ i, absurd (erase_sub _ _ i) nbinl, assert aux : nodup (b :: erase a l), from nodup_cons nbineal ndeal, by rewrite [erase_cons_tail _ aneb]; exact aux) theorem mem_erase_of_nodup [decidable_eq A] (a : A) : ∀ {l}, nodup l → a ∉ erase a l | [] n := !not_mem_nil | (b::l) n := have ndl : nodup l, from nodup_of_nodup_cons n, have naineal : a ∉ erase a l, from mem_erase_of_nodup ndl, assert nbinl : b ∉ l, from not_mem_of_nodup_cons n, by_cases (λ aeqb : a = b, by rewrite [aeqb, erase_cons_head]; exact nbinl) (λ aneb : a ≠ b, assert aux : a ∉ b :: erase a l, from assume ainbeal : a ∈ b :: erase a l, or.elim (eq_or_mem_of_mem_cons ainbeal) (λ aeqb : a = b, absurd aeqb aneb) (λ aineal : a ∈ erase a l, absurd aineal naineal), by rewrite [erase_cons_tail _ aneb]; exact aux) definition erase_dup [decidable_eq A] : list A → list A | [] := [] | (x :: xs) := if x ∈ xs then erase_dup xs else x :: erase_dup xs theorem erase_dup_nil [decidable_eq A] : erase_dup [] = ([] : list A) theorem erase_dup_cons_of_mem [decidable_eq A] {a : A} {l : list A} : a ∈ l → erase_dup (a::l) = erase_dup l := assume ainl, calc erase_dup (a::l) = if a ∈ l then erase_dup l else a :: erase_dup l : rfl ... = erase_dup l : if_pos ainl theorem erase_dup_cons_of_not_mem [decidable_eq A] {a : A} {l : list A} : a ∉ l → erase_dup (a::l) = a :: erase_dup l := assume nainl, calc erase_dup (a::l) = if a ∈ l then erase_dup l else a :: erase_dup l : rfl ... = a :: erase_dup l : if_neg nainl theorem mem_erase_dup [decidable_eq A] {a : A} : ∀ {l}, a ∈ l → a ∈ erase_dup l | [] h := absurd h !not_mem_nil | (b::l) h := by_cases (λ binl : b ∈ l, or.elim (eq_or_mem_of_mem_cons h) (λ aeqb : a = b, by rewrite [erase_dup_cons_of_mem binl, -aeqb at binl]; exact (mem_erase_dup binl)) (λ ainl : a ∈ l, by rewrite [erase_dup_cons_of_mem binl]; exact (mem_erase_dup ainl))) (λ nbinl : b ∉ l, or.elim (eq_or_mem_of_mem_cons h) (λ aeqb : a = b, by rewrite [erase_dup_cons_of_not_mem nbinl, aeqb]; exact !mem_cons) (λ ainl : a ∈ l, by rewrite [erase_dup_cons_of_not_mem nbinl]; exact (or.inr (mem_erase_dup ainl)))) theorem mem_of_mem_erase_dup [decidable_eq A] {a : A} : ∀ {l}, a ∈ erase_dup l → a ∈ l | [] h := by rewrite [erase_dup_nil at h]; exact h | (b::l) h := by_cases (λ binl : b ∈ l, have h₁ : a ∈ erase_dup l, by rewrite [erase_dup_cons_of_mem binl at h]; exact h, or.inr (mem_of_mem_erase_dup h₁)) (λ nbinl : b ∉ l, have h₁ : a ∈ b :: erase_dup l, by rewrite [erase_dup_cons_of_not_mem nbinl at h]; exact h, or.elim (eq_or_mem_of_mem_cons h₁) (λ aeqb : a = b, by rewrite aeqb; exact !mem_cons) (λ ainel : a ∈ erase_dup l, or.inr (mem_of_mem_erase_dup ainel))) theorem erase_dup_sub [decidable_eq A] (l : list A) : erase_dup l ⊆ l := λ a i, mem_of_mem_erase_dup i theorem sub_erase_dup [decidable_eq A] (l : list A) : l ⊆ erase_dup l := λ a i, mem_erase_dup i theorem nodup_erase_dup [decidable_eq A] : ∀ l : list A, nodup (erase_dup l) | [] := by rewrite erase_dup_nil; exact nodup_nil | (a::l) := by_cases (λ ainl : a ∈ l, by rewrite [erase_dup_cons_of_mem ainl]; exact (nodup_erase_dup l)) (λ nainl : a ∉ l, assert r : nodup (erase_dup l), from nodup_erase_dup l, assert nin : a ∉ erase_dup l, from assume ab : a ∈ erase_dup l, absurd (mem_of_mem_erase_dup ab) nainl, by rewrite [erase_dup_cons_of_not_mem nainl]; exact (nodup_cons nin r)) theorem erase_dup_eq_of_nodup [decidable_eq A] : ∀ {l : list A}, nodup l → erase_dup l = l | [] d := rfl | (a::l) d := assert nainl : a ∉ l, from not_mem_of_nodup_cons d, assert dl : nodup l, from nodup_of_nodup_cons d, by rewrite [erase_dup_cons_of_not_mem nainl, erase_dup_eq_of_nodup dl] definition decidable_nodup [instance] [decidable_eq A] : ∀ (l : list A), decidable (nodup l) | [] := inl nodup_nil | (a::l) := match decidable_mem a l with | inl p := inr (not_nodup_cons_of_mem p) | inr n := match decidable_nodup l with | inl nd := inl (nodup_cons n nd) | inr d := inr (not_nodup_cons_of_not_nodup d) end end theorem nodup_product : ∀ {l₁ : list A} {l₂ : list B}, nodup l₁ → nodup l₂ → nodup (product l₁ l₂) | [] l₂ n₁ n₂ := nodup_nil | (a::l₁) l₂ n₁ n₂ := have nainl₁ : a ∉ l₁, from not_mem_of_nodup_cons n₁, have n₃ : nodup l₁, from nodup_of_nodup_cons n₁, have n₄ : nodup (product l₁ l₂), from nodup_product n₃ n₂, have dgen : ∀ l, nodup l → nodup (map (λ b, (a, b)) l) | [] h := nodup_nil | (x::l) h := have dl : nodup l, from nodup_of_nodup_cons h, have dm : nodup (map (λ b, (a, b)) l), from dgen l dl, have nxin : x ∉ l, from not_mem_of_nodup_cons h, have npin : (a, x) ∉ map (λ b, (a, b)) l, from assume pin, absurd (mem_of_mem_map_pair₁ pin) nxin, nodup_cons npin dm, have dm : nodup (map (λ b, (a, b)) l₂), from dgen l₂ n₂, have dsj : disjoint (map (λ b, (a, b)) l₂) (product l₁ l₂), from λ p, match p with | (a₁, b₁) := λ (i₁ : (a₁, b₁) ∈ map (λ b, (a, b)) l₂) (i₂ : (a₁, b₁) ∈ product l₁ l₂), have a₁inl₁ : a₁ ∈ l₁, from mem_of_mem_product_left i₂, have a₁eqa : a₁ = a, from eq_of_mem_map_pair₁ i₁, absurd (a₁eqa ▸ a₁inl₁) nainl₁ end, nodup_append_of_nodup_of_nodup_of_disjoint dm n₄ dsj theorem nodup_filter (p : A → Prop) [decidable_pred p] : ∀ {l : list A}, nodup l → nodup (filter p l) | [] nd := nodup_nil | (a::l) nd := have nainl : a ∉ l, from not_mem_of_nodup_cons nd, have ndl : nodup l, from nodup_of_nodup_cons nd, assert ndf : nodup (filter p l), from nodup_filter ndl, assert nainf : a ∉ filter p l, from assume ainf, absurd (mem_of_mem_filter ainf) nainl, by_cases (λ pa : p a, by rewrite [filter_cons_of_pos _ pa]; exact (nodup_cons nainf ndf)) (λ npa : ¬ p a, by rewrite [filter_cons_of_neg _ npa]; exact ndf) lemma dmap_nodup_of_dinj {p : A → Prop} [h : decidable_pred p] {f : Π a, p a → B} (Pdi : dinj p f): ∀ {l : list A}, nodup l → nodup (dmap p f l) | [] := take P, nodup.ndnil | (a::l) := take Pnodup, decidable.rec_on (h a) (λ Pa, begin rewrite [dmap_cons_of_pos Pa], apply nodup_cons, apply (not_mem_dmap_of_dinj_of_not_mem Pdi Pa), exact not_mem_of_nodup_cons Pnodup, exact dmap_nodup_of_dinj (nodup_of_nodup_cons Pnodup) end) (λ nPa, begin rewrite [dmap_cons_of_neg nPa], exact dmap_nodup_of_dinj (nodup_of_nodup_cons Pnodup) end) end nodup /- upto -/ definition upto : nat → list nat | 0 := [] | (n+1) := n :: upto n theorem upto_nil : upto 0 = nil theorem upto_succ (n : nat) : upto (succ n) = n :: upto n theorem length_upto : ∀ n, length (upto n) = n | 0 := rfl | (succ n) := by rewrite [upto_succ, length_cons, length_upto] theorem upto_less : ∀ n, all (upto n) (λ i, i < n) | 0 := trivial | (succ n) := have alln : all (upto n) (λ i, i < n), from upto_less n, all_cons_of_all (lt.base n) (all_implies alln (λ x h, lt.step h)) theorem nodup_upto : ∀ n, nodup (upto n) | 0 := nodup_nil | (n+1) := have d : nodup (upto n), from nodup_upto n, have n : n ∉ upto n, from assume i : n ∈ upto n, absurd (of_mem_of_all i (upto_less n)) (nat.lt_irrefl n), nodup_cons n d theorem lt_of_mem_upto {n i : nat} : i ∈ upto n → i < n := assume i, of_mem_of_all i (upto_less n) theorem mem_upto_succ_of_mem_upto {n i : nat} : i ∈ upto n → i ∈ upto (succ n) := assume i, mem_cons_of_mem _ i theorem mem_upto_of_lt : ∀ {n i : nat}, i < n → i ∈ upto n | 0 i h := absurd h !not_lt_zero | (succ n) i h := begin cases h with m h', { rewrite upto_succ, apply mem_cons}, { exact mem_upto_succ_of_mem_upto (mem_upto_of_lt h')} end lemma upto_step : ∀ {n : nat}, upto (succ n) = (map succ (upto n))++[0] | 0 := rfl | (succ n) := begin rewrite [upto_succ n, map_cons, append_cons, -upto_step] end /- union -/ section union variable {A : Type} variable [H : decidable_eq A] include H definition union : list A → list A → list A | [] l₂ := l₂ | (a::l₁) l₂ := if a ∈ l₂ then union l₁ l₂ else a :: union l₁ l₂ theorem nil_union (l : list A) : union [] l = l theorem union_cons_of_mem {a : A} {l₂} : ∀ (l₁), a ∈ l₂ → union (a::l₁) l₂ = union l₁ l₂ := take l₁, assume ainl₂, calc union (a::l₁) l₂ = if a ∈ l₂ then union l₁ l₂ else a :: union l₁ l₂ : rfl ... = union l₁ l₂ : if_pos ainl₂ theorem union_cons_of_not_mem {a : A} {l₂} : ∀ (l₁), a ∉ l₂ → union (a::l₁) l₂ = a :: union l₁ l₂ := take l₁, assume nainl₂, calc union (a::l₁) l₂ = if a ∈ l₂ then union l₁ l₂ else a :: union l₁ l₂ : rfl ... = a :: union l₁ l₂ : if_neg nainl₂ theorem union_nil : ∀ (l : list A), union l [] = l | [] := !nil_union | (a::l) := by rewrite [union_cons_of_not_mem _ !not_mem_nil, union_nil] theorem mem_or_mem_of_mem_union : ∀ {l₁ l₂} {a : A}, a ∈ union l₁ l₂ → a ∈ l₁ ∨ a ∈ l₂ | [] l₂ a ainl₂ := by rewrite nil_union at ainl₂; exact (or.inr (ainl₂)) | (b::l₁) l₂ a ainbl₁l₂ := by_cases (λ binl₂ : b ∈ l₂, have ainl₁l₂ : a ∈ union l₁ l₂, by rewrite [union_cons_of_mem l₁ binl₂ at ainbl₁l₂]; exact ainbl₁l₂, or.elim (mem_or_mem_of_mem_union ainl₁l₂) (λ ainl₁, or.inl (mem_cons_of_mem _ ainl₁)) (λ ainl₂, or.inr ainl₂)) (λ nbinl₂ : b ∉ l₂, have ainb_l₁l₂ : a ∈ b :: union l₁ l₂, by rewrite [union_cons_of_not_mem l₁ nbinl₂ at ainbl₁l₂]; exact ainbl₁l₂, or.elim (eq_or_mem_of_mem_cons ainb_l₁l₂) (λ aeqb, by rewrite aeqb; exact (or.inl !mem_cons)) (λ ainl₁l₂, or.elim (mem_or_mem_of_mem_union ainl₁l₂) (λ ainl₁, or.inl (mem_cons_of_mem _ ainl₁)) (λ ainl₂, or.inr ainl₂))) theorem mem_union_right {a : A} : ∀ (l₁) {l₂}, a ∈ l₂ → a ∈ union l₁ l₂ | [] l₂ h := by rewrite nil_union; exact h | (b::l₁) l₂ h := by_cases (λ binl₂ : b ∈ l₂, by rewrite [union_cons_of_mem _ binl₂]; exact (mem_union_right _ h)) (λ nbinl₂ : b ∉ l₂, by rewrite [union_cons_of_not_mem _ nbinl₂]; exact (mem_cons_of_mem _ (mem_union_right _ h))) theorem mem_union_left {a : A} : ∀ {l₁} (l₂), a ∈ l₁ → a ∈ union l₁ l₂ | [] l₂ h := absurd h !not_mem_nil | (b::l₁) l₂ h := by_cases (λ binl₂ : b ∈ l₂, or.elim (eq_or_mem_of_mem_cons h) (λ aeqb : a = b, by rewrite [union_cons_of_mem l₁ binl₂, -aeqb at binl₂]; exact (mem_union_right _ binl₂)) (λ ainl₁ : a ∈ l₁, by rewrite [union_cons_of_mem l₁ binl₂]; exact (mem_union_left _ ainl₁))) (λ nbinl₂ : b ∉ l₂, or.elim (eq_or_mem_of_mem_cons h) (λ aeqb : a = b, by rewrite [union_cons_of_not_mem l₁ nbinl₂, aeqb]; exact !mem_cons) (λ ainl₁ : a ∈ l₁, by rewrite [union_cons_of_not_mem l₁ nbinl₂]; exact (mem_cons_of_mem _ (mem_union_left _ ainl₁)))) theorem mem_union_cons (a : A) (l₁ : list A) (l₂ : list A) : a ∈ union (a::l₁) l₂ := by_cases (λ ainl₂ : a ∈ l₂, mem_union_right _ ainl₂) (λ nainl₂ : a ∉ l₂, by rewrite [union_cons_of_not_mem _ nainl₂]; exact !mem_cons) theorem nodup_union_of_nodup_of_nodup : ∀ {l₁ l₂ : list A}, nodup l₁ → nodup l₂ → nodup (union l₁ l₂) | [] l₂ n₁ nl₂ := by rewrite nil_union; exact nl₂ | (a::l₁) l₂ nal₁ nl₂ := assert nl₁ : nodup l₁, from nodup_of_nodup_cons nal₁, assert nl₁l₂ : nodup (union l₁ l₂), from nodup_union_of_nodup_of_nodup nl₁ nl₂, by_cases (λ ainl₂ : a ∈ l₂, by rewrite [union_cons_of_mem l₁ ainl₂]; exact nl₁l₂) (λ nainl₂ : a ∉ l₂, have nainl₁ : a ∉ l₁, from not_mem_of_nodup_cons nal₁, assert nainl₁l₂ : a ∉ union l₁ l₂, from assume ainl₁l₂ : a ∈ union l₁ l₂, or.elim (mem_or_mem_of_mem_union ainl₁l₂) (λ ainl₁, absurd ainl₁ nainl₁) (λ ainl₂, absurd ainl₂ nainl₂), by rewrite [union_cons_of_not_mem l₁ nainl₂]; exact (nodup_cons nainl₁l₂ nl₁l₂)) theorem union_eq_append : ∀ {l₁ l₂ : list A}, disjoint l₁ l₂ → union l₁ l₂ = append l₁ l₂ | [] l₂ d := rfl | (a::l₁) l₂ d := assert nainl₂ : a ∉ l₂, from disjoint_left d !mem_cons, assert d₁ : disjoint l₁ l₂, from disjoint_of_disjoint_cons_left d, by rewrite [union_cons_of_not_mem _ nainl₂, append_cons, union_eq_append d₁] theorem all_union {p : A → Prop} : ∀ {l₁ l₂ : list A}, all l₁ p → all l₂ p → all (union l₁ l₂) p | [] l₂ h₁ h₂ := h₂ | (a::l₁) l₂ h₁ h₂ := have h₁' : all l₁ p, from all_of_all_cons h₁, have pa : p a, from of_all_cons h₁, assert au : all (union l₁ l₂) p, from all_union h₁' h₂, assert au' : all (a :: union l₁ l₂) p, from all_cons_of_all pa au, by_cases (λ ainl₂ : a ∈ l₂, by rewrite [union_cons_of_mem _ ainl₂]; exact au) (λ nainl₂ : a ∉ l₂, by rewrite [union_cons_of_not_mem _ nainl₂]; exact au') theorem all_of_all_union_left {p : A → Prop} : ∀ {l₁ l₂ : list A}, all (union l₁ l₂) p → all l₁ p | [] l₂ h := trivial | (a::l₁) l₂ h := have ain : a ∈ union (a::l₁) l₂, from !mem_union_cons, have pa : p a, from of_mem_of_all ain h, by_cases (λ ainl₂ : a ∈ l₂, have al₁l₂ : all (union l₁ l₂) p, by rewrite [union_cons_of_mem _ ainl₂ at h]; exact h, have al₁ : all l₁ p, from all_of_all_union_left al₁l₂, all_cons_of_all pa al₁) (λ nainl₂ : a ∉ l₂, have aal₁l₂ : all (a::union l₁ l₂) p, by rewrite [union_cons_of_not_mem _ nainl₂ at h]; exact h, have al₁l₂ : all (union l₁ l₂) p, from all_of_all_cons aal₁l₂, have al₁ : all l₁ p, from all_of_all_union_left al₁l₂, all_cons_of_all pa al₁) theorem all_of_all_union_right {p : A → Prop} : ∀ {l₁ l₂ : list A}, all (union l₁ l₂) p → all l₂ p | [] l₂ h := by rewrite [nil_union at h]; exact h | (a::l₁) l₂ h := by_cases (λ ainl₂ : a ∈ l₂, by rewrite [union_cons_of_mem _ ainl₂ at h]; exact (all_of_all_union_right h)) (λ nainl₂ : a ∉ l₂, have h₁ : all (a :: union l₁ l₂) p, by rewrite [union_cons_of_not_mem _ nainl₂ at h]; exact h, all_of_all_union_right (all_of_all_cons h₁)) variable {B : Type} theorem foldl_union_of_disjoint (f : B → A → B) (b : B) {l₁ l₂ : list A} (d : disjoint l₁ l₂) : foldl f b (union l₁ l₂) = foldl f (foldl f b l₁) l₂ := by rewrite [union_eq_append d, foldl_append] theorem foldr_union_of_dijoint (f : A → B → B) (b : B) {l₁ l₂ : list A} (d : disjoint l₁ l₂) : foldr f b (union l₁ l₂) = foldr f (foldr f b l₂) l₁ := by rewrite [union_eq_append d, foldr_append] end union /- insert -/ section insert variable {A : Type} variable [H : decidable_eq A] include H definition insert (a : A) (l : list A) : list A := if a ∈ l then l else a::l theorem insert_eq_of_mem {a : A} {l : list A} : a ∈ l → insert a l = l := assume ainl, if_pos ainl theorem insert_eq_of_not_mem {a : A} {l : list A} : a ∉ l → insert a l = a::l := assume nainl, if_neg nainl theorem mem_insert (a : A) (l : list A) : a ∈ insert a l := by_cases (λ ainl : a ∈ l, by rewrite [insert_eq_of_mem ainl]; exact ainl) (λ nainl : a ∉ l, by rewrite [insert_eq_of_not_mem nainl]; exact !mem_cons) theorem mem_insert_of_mem {a : A} (b : A) {l : list A} : a ∈ l → a ∈ insert b l := assume ainl, by_cases (λ binl : b ∈ l, by rewrite [insert_eq_of_mem binl]; exact ainl) (λ nbinl : b ∉ l, by rewrite [insert_eq_of_not_mem nbinl]; exact (mem_cons_of_mem _ ainl)) theorem eq_or_mem_of_mem_insert {x a : A} {l : list A} (H : x ∈ insert a l) : x = a ∨ x ∈ l := decidable.by_cases (assume H3: a ∈ l, or.inr (insert_eq_of_mem H3 ▸ H)) (assume H3: a ∉ l, have H4: x ∈ a :: l, from insert_eq_of_not_mem H3 ▸ H, iff.mp !mem_cons_iff H4) theorem mem_insert_iff (x a : A) (l : list A) : x ∈ insert a l ↔ x = a ∨ x ∈ l := iff.intro (!eq_or_mem_of_mem_insert) (assume H, or.elim H (assume H' : x = a, H'⁻¹ ▸ !mem_insert) (assume H' : x ∈ l, !mem_insert_of_mem H')) theorem nodup_insert (a : A) {l : list A} : nodup l → nodup (insert a l) := assume n, by_cases (λ ainl : a ∈ l, by rewrite [insert_eq_of_mem ainl]; exact n) (λ nainl : a ∉ l, by rewrite [insert_eq_of_not_mem nainl]; exact (nodup_cons nainl n)) theorem length_insert_of_mem {a : A} {l : list A} : a ∈ l → length (insert a l) = length l := assume ainl, by rewrite [insert_eq_of_mem ainl] theorem length_insert_of_not_mem {a : A} {l : list A} : a ∉ l → length (insert a l) = length l + 1 := assume nainl, by rewrite [insert_eq_of_not_mem nainl] theorem all_insert_of_all {p : A → Prop} {a : A} {l} : p a → all l p → all (insert a l) p := assume h₁ h₂, by_cases (λ ainl : a ∈ l, by rewrite [insert_eq_of_mem ainl]; exact h₂) (λ nainl : a ∉ l, by rewrite [insert_eq_of_not_mem nainl]; exact (all_cons_of_all h₁ h₂)) end insert /- inter -/ section inter variable {A : Type} variable [H : decidable_eq A] include H definition inter : list A → list A → list A | [] l₂ := [] | (a::l₁) l₂ := if a ∈ l₂ then a :: inter l₁ l₂ else inter l₁ l₂ theorem inter_nil (l : list A) : inter [] l = [] theorem inter_cons_of_mem {a : A} (l₁ : list A) {l₂} : a ∈ l₂ → inter (a::l₁) l₂ = a :: inter l₁ l₂ := assume i, if_pos i theorem inter_cons_of_not_mem {a : A} (l₁ : list A) {l₂} : a ∉ l₂ → inter (a::l₁) l₂ = inter l₁ l₂ := assume i, if_neg i theorem mem_of_mem_inter_left : ∀ {l₁ l₂} {a : A}, a ∈ inter l₁ l₂ → a ∈ l₁ | [] l₂ a i := absurd i !not_mem_nil | (b::l₁) l₂ a i := by_cases (λ binl₂ : b ∈ l₂, have aux : a ∈ b :: inter l₁ l₂, by rewrite [inter_cons_of_mem _ binl₂ at i]; exact i, or.elim (eq_or_mem_of_mem_cons aux) (λ aeqb : a = b, by rewrite [aeqb]; exact !mem_cons) (λ aini, mem_cons_of_mem _ (mem_of_mem_inter_left aini))) (λ nbinl₂ : b ∉ l₂, have ainl₁ : a ∈ l₁, by rewrite [inter_cons_of_not_mem _ nbinl₂ at i]; exact (mem_of_mem_inter_left i), mem_cons_of_mem _ ainl₁) theorem mem_of_mem_inter_right : ∀ {l₁ l₂} {a : A}, a ∈ inter l₁ l₂ → a ∈ l₂ | [] l₂ a i := absurd i !not_mem_nil | (b::l₁) l₂ a i := by_cases (λ binl₂ : b ∈ l₂, have aux : a ∈ b :: inter l₁ l₂, by rewrite [inter_cons_of_mem _ binl₂ at i]; exact i, or.elim (eq_or_mem_of_mem_cons aux) (λ aeqb : a = b, by rewrite [aeqb]; exact binl₂) (λ aini : a ∈ inter l₁ l₂, mem_of_mem_inter_right aini)) (λ nbinl₂ : b ∉ l₂, by rewrite [inter_cons_of_not_mem _ nbinl₂ at i]; exact (mem_of_mem_inter_right i)) theorem mem_inter_of_mem_of_mem : ∀ {l₁ l₂} {a : A}, a ∈ l₁ → a ∈ l₂ → a ∈ inter l₁ l₂ | [] l₂ a i₁ i₂ := absurd i₁ !not_mem_nil | (b::l₁) l₂ a i₁ i₂ := by_cases (λ binl₂ : b ∈ l₂, or.elim (eq_or_mem_of_mem_cons i₁) (λ aeqb : a = b, by rewrite [inter_cons_of_mem _ binl₂, aeqb]; exact !mem_cons) (λ ainl₁ : a ∈ l₁, by rewrite [inter_cons_of_mem _ binl₂]; apply mem_cons_of_mem; exact (mem_inter_of_mem_of_mem ainl₁ i₂))) (λ nbinl₂ : b ∉ l₂, or.elim (eq_or_mem_of_mem_cons i₁) (λ aeqb : a = b, absurd (aeqb ▸ i₂) nbinl₂) (λ ainl₁ : a ∈ l₁, by rewrite [inter_cons_of_not_mem _ nbinl₂]; exact (mem_inter_of_mem_of_mem ainl₁ i₂))) theorem nodup_inter_of_nodup : ∀ {l₁ : list A} (l₂), nodup l₁ → nodup (inter l₁ l₂) | [] l₂ d := nodup_nil | (a::l₁) l₂ d := have d₁ : nodup l₁, from nodup_of_nodup_cons d, assert d₂ : nodup (inter l₁ l₂), from nodup_inter_of_nodup _ d₁, have nainl₁ : a ∉ l₁, from not_mem_of_nodup_cons d, assert naini : a ∉ inter l₁ l₂, from λ i, absurd (mem_of_mem_inter_left i) nainl₁, by_cases (λ ainl₂ : a ∈ l₂, by rewrite [inter_cons_of_mem _ ainl₂]; exact (nodup_cons naini d₂)) (λ nainl₂ : a ∉ l₂, by rewrite [inter_cons_of_not_mem _ nainl₂]; exact d₂) theorem inter_eq_nil_of_disjoint : ∀ {l₁ l₂ : list A}, disjoint l₁ l₂ → inter l₁ l₂ = [] | [] l₂ d := rfl | (a::l₁) l₂ d := assert aux_eq : inter l₁ l₂ = [], from inter_eq_nil_of_disjoint (disjoint_of_disjoint_cons_left d), assert nainl₂ : a ∉ l₂, from disjoint_left d !mem_cons, by rewrite [inter_cons_of_not_mem _ nainl₂, aux_eq] theorem all_inter_of_all_left {p : A → Prop} : ∀ {l₁} (l₂), all l₁ p → all (inter l₁ l₂) p | [] l₂ h := trivial | (a::l₁) l₂ h := have h₁ : all l₁ p, from all_of_all_cons h, assert h₂ : all (inter l₁ l₂) p, from all_inter_of_all_left _ h₁, have pa : p a, from of_all_cons h, assert h₃ : all (a :: inter l₁ l₂) p, from all_cons_of_all pa h₂, by_cases (λ ainl₂ : a ∈ l₂, by rewrite [inter_cons_of_mem _ ainl₂]; exact h₃) (λ nainl₂ : a ∉ l₂, by rewrite [inter_cons_of_not_mem _ nainl₂]; exact h₂) theorem all_inter_of_all_right {p : A → Prop} : ∀ (l₁) {l₂}, all l₂ p → all (inter l₁ l₂) p | [] l₂ h := trivial | (a::l₁) l₂ h := assert h₁ : all (inter l₁ l₂) p, from all_inter_of_all_right _ h, by_cases (λ ainl₂ : a ∈ l₂, have pa : p a, from of_mem_of_all ainl₂ h, assert h₂ : all (a :: inter l₁ l₂) p, from all_cons_of_all pa h₁, by rewrite [inter_cons_of_mem _ ainl₂]; exact h₂) (λ nainl₂ : a ∉ l₂, by rewrite [inter_cons_of_not_mem _ nainl₂]; exact h₁) end inter end list
f4090988f11e7e9e31ad189853af4f3dc1a78d2e
367134ba5a65885e863bdc4507601606690974c1
/src/order/bounds.lean
059c773fe90a79997d44815bf886e465dfcf5914
[ "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
30,515
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov -/ import data.set.intervals.basic import algebra.ordered_group /-! # Upper / lower bounds In this file we define: * `upper_bounds`, `lower_bounds` : the set of upper bounds (resp., lower bounds) of a set; * `bdd_above s`, `bdd_below s` : the set `s` is bounded above (resp., below), i.e., the set of upper (resp., lower) bounds of `s` is nonempty; * `is_least s a`, `is_greatest s a` : `a` is a least (resp., greatest) element of `s`; for a partial order, it is unique if exists; * `is_lub s a`, `is_glb s a` : `a` is a least upper bound (resp., a greatest lower bound) of `s`; for a partial order, it is unique if exists. We also prove various lemmas about monotonicity, behaviour under `∪`, `∩`, `insert`, and provide formulas for `∅`, `univ`, and intervals. -/ open set universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} section variables [preorder α] [preorder β] {s t : set α} {a b : α} /-! ### Definitions -/ /-- The set of upper bounds of a set. -/ def upper_bounds (s : set α) : set α := { x | ∀ ⦃a⦄, a ∈ s → a ≤ x } /-- The set of lower bounds of a set. -/ def lower_bounds (s : set α) : set α := { x | ∀ ⦃a⦄, a ∈ s → x ≤ a } /-- A set is bounded above if there exists an upper bound. -/ def bdd_above (s : set α) := (upper_bounds s).nonempty /-- A set is bounded below if there exists a lower bound. -/ def bdd_below (s : set α) := (lower_bounds s).nonempty /-- `a` is a least element of a set `s`; for a partial order, it is unique if exists. -/ def is_least (s : set α) (a : α) : Prop := a ∈ s ∧ a ∈ lower_bounds s /-- `a` is a greatest element of a set `s`; for a partial order, it is unique if exists -/ def is_greatest (s : set α) (a : α) : Prop := a ∈ s ∧ a ∈ upper_bounds s /-- `a` is a least upper bound of a set `s`; for a partial order, it is unique if exists. -/ def is_lub (s : set α) : α → Prop := is_least (upper_bounds s) /-- `a` is a greatest lower bound of a set `s`; for a partial order, it is unique if exists. -/ def is_glb (s : set α) : α → Prop := is_greatest (lower_bounds s) lemma mem_upper_bounds : a ∈ upper_bounds s ↔ ∀ x ∈ s, x ≤ a := iff.rfl lemma mem_lower_bounds : a ∈ lower_bounds s ↔ ∀ x ∈ s, a ≤ x := iff.rfl /-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` such that `x` is not greater than or equal to `y`. This version only assumes `preorder` structure and uses `¬(y ≤ x)`. A version for linear orders is called `not_bdd_above_iff`. -/ lemma not_bdd_above_iff' : ¬bdd_above s ↔ ∀ x, ∃ y ∈ s, ¬(y ≤ x) := by simp [bdd_above, upper_bounds, set.nonempty] /-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` such that `x` is not less than or equal to `y`. This version only assumes `preorder` structure and uses `¬(x ≤ y)`. A version for linear orders is called `not_bdd_below_iff`. -/ lemma not_bdd_below_iff' : ¬bdd_below s ↔ ∀ x, ∃ y ∈ s, ¬(x ≤ y) := @not_bdd_above_iff' (order_dual α) _ _ /-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` that is greater than `x`. A version for preorders is called `not_bdd_above_iff'`. -/ lemma not_bdd_above_iff {α : Type*} [linear_order α] {s : set α} : ¬bdd_above s ↔ ∀ x, ∃ y ∈ s, x < y := by simp only [not_bdd_above_iff', not_le] /-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` that is less than `x`. A version for preorders is called `not_bdd_below_iff'`. -/ lemma not_bdd_below_iff {α : Type*} [linear_order α] {s : set α} : ¬bdd_below s ↔ ∀ x, ∃ y ∈ s, y < x := @not_bdd_above_iff (order_dual α) _ _ /-! ### Monotonicity -/ lemma upper_bounds_mono_set ⦃s t : set α⦄ (hst : s ⊆ t) : upper_bounds t ⊆ upper_bounds s := λ b hb x h, hb $ hst h lemma lower_bounds_mono_set ⦃s t : set α⦄ (hst : s ⊆ t) : lower_bounds t ⊆ lower_bounds s := λ b hb x h, hb $ hst h lemma upper_bounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : a ∈ upper_bounds s → b ∈ upper_bounds s := λ ha x h, le_trans (ha h) hab lemma lower_bounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : b ∈ lower_bounds s → a ∈ lower_bounds s := λ hb x h, le_trans hab (hb h) lemma upper_bounds_mono ⦃s t : set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) : a ∈ upper_bounds t → b ∈ upper_bounds s := λ ha, upper_bounds_mono_set hst $ upper_bounds_mono_mem hab ha lemma lower_bounds_mono ⦃s t : set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) : b ∈ lower_bounds t → a ∈ lower_bounds s := λ hb, lower_bounds_mono_set hst $ lower_bounds_mono_mem hab hb /-- If `s ⊆ t` and `t` is bounded above, then so is `s`. -/ lemma bdd_above.mono ⦃s t : set α⦄ (h : s ⊆ t) : bdd_above t → bdd_above s := nonempty.mono $ upper_bounds_mono_set h /-- If `s ⊆ t` and `t` is bounded below, then so is `s`. -/ lemma bdd_below.mono ⦃s t : set α⦄ (h : s ⊆ t) : bdd_below t → bdd_below s := nonempty.mono $ lower_bounds_mono_set h /-- If `a` is a least upper bound for sets `s` and `p`, then it is a least upper bound for any set `t`, `s ⊆ t ⊆ p`. -/ lemma is_lub.of_subset_of_superset {s t p : set α} (hs : is_lub s a) (hp : is_lub p a) (hst : s ⊆ t) (htp : t ⊆ p) : is_lub t a := ⟨upper_bounds_mono_set htp hp.1, lower_bounds_mono_set (upper_bounds_mono_set hst) hs.2⟩ /-- If `a` is a greatest lower bound for sets `s` and `p`, then it is a greater lower bound for any set `t`, `s ⊆ t ⊆ p`. -/ lemma is_glb.of_subset_of_superset {s t p : set α} (hs : is_glb s a) (hp : is_glb p a) (hst : s ⊆ t) (htp : t ⊆ p) : is_glb t a := @is_lub.of_subset_of_superset (order_dual α) _ a s t p hs hp hst htp lemma is_least.mono (ha : is_least s a) (hb : is_least t b) (hst : s ⊆ t) : b ≤ a := hb.2 (hst ha.1) lemma is_greatest.mono (ha : is_greatest s a) (hb : is_greatest t b) (hst : s ⊆ t) : a ≤ b := hb.2 (hst ha.1) lemma is_lub.mono (ha : is_lub s a) (hb : is_lub t b) (hst : s ⊆ t) : a ≤ b := hb.mono ha $ upper_bounds_mono_set hst lemma is_glb.mono (ha : is_glb s a) (hb : is_glb t b) (hst : s ⊆ t) : b ≤ a := hb.mono ha $ lower_bounds_mono_set hst /-! ### Conversions -/ lemma is_least.is_glb (h : is_least s a) : is_glb s a := ⟨h.2, λ b hb, hb h.1⟩ lemma is_greatest.is_lub (h : is_greatest s a) : is_lub s a := ⟨h.2, λ b hb, hb h.1⟩ lemma is_lub.upper_bounds_eq (h : is_lub s a) : upper_bounds s = Ici a := set.ext $ λ b, ⟨λ hb, h.2 hb, λ hb, upper_bounds_mono_mem hb h.1⟩ lemma is_glb.lower_bounds_eq (h : is_glb s a) : lower_bounds s = Iic a := @is_lub.upper_bounds_eq (order_dual α) _ _ _ h lemma is_least.lower_bounds_eq (h : is_least s a) : lower_bounds s = Iic a := h.is_glb.lower_bounds_eq lemma is_greatest.upper_bounds_eq (h : is_greatest s a) : upper_bounds s = Ici a := h.is_lub.upper_bounds_eq lemma is_lub_le_iff (h : is_lub s a) : a ≤ b ↔ b ∈ upper_bounds s := by { rw h.upper_bounds_eq, refl } lemma le_is_glb_iff (h : is_glb s a) : b ≤ a ↔ b ∈ lower_bounds s := by { rw h.lower_bounds_eq, refl } /-- If `s` has a least upper bound, then it is bounded above. -/ lemma is_lub.bdd_above (h : is_lub s a) : bdd_above s := ⟨a, h.1⟩ /-- If `s` has a greatest lower bound, then it is bounded below. -/ lemma is_glb.bdd_below (h : is_glb s a) : bdd_below s := ⟨a, h.1⟩ /-- If `s` has a greatest element, then it is bounded above. -/ lemma is_greatest.bdd_above (h : is_greatest s a) : bdd_above s := ⟨a, h.2⟩ /-- If `s` has a least element, then it is bounded below. -/ lemma is_least.bdd_below (h : is_least s a) : bdd_below s := ⟨a, h.2⟩ lemma is_least.nonempty (h : is_least s a) : s.nonempty := ⟨a, h.1⟩ lemma is_greatest.nonempty (h : is_greatest s a) : s.nonempty := ⟨a, h.1⟩ /-! ### Union and intersection -/ @[simp] lemma upper_bounds_union : upper_bounds (s ∪ t) = upper_bounds s ∩ upper_bounds t := subset.antisymm (λ b hb, ⟨λ x hx, hb (or.inl hx), λ x hx, hb (or.inr hx)⟩) (λ b hb x hx, hx.elim (λ hs, hb.1 hs) (λ ht, hb.2 ht)) @[simp] lemma lower_bounds_union : lower_bounds (s ∪ t) = lower_bounds s ∩ lower_bounds t := @upper_bounds_union (order_dual α) _ s t lemma union_upper_bounds_subset_upper_bounds_inter : upper_bounds s ∪ upper_bounds t ⊆ upper_bounds (s ∩ t) := union_subset (upper_bounds_mono_set $ inter_subset_left _ _) (upper_bounds_mono_set $ inter_subset_right _ _) lemma union_lower_bounds_subset_lower_bounds_inter : lower_bounds s ∪ lower_bounds t ⊆ lower_bounds (s ∩ t) := @union_upper_bounds_subset_upper_bounds_inter (order_dual α) _ s t lemma is_least_union_iff {a : α} {s t : set α} : is_least (s ∪ t) a ↔ (is_least s a ∧ a ∈ lower_bounds t ∨ a ∈ lower_bounds s ∧ is_least t a) := by simp [is_least, lower_bounds_union, or_and_distrib_right, and_comm (a ∈ t), and_assoc] lemma is_greatest_union_iff : is_greatest (s ∪ t) a ↔ (is_greatest s a ∧ a ∈ upper_bounds t ∨ a ∈ upper_bounds s ∧ is_greatest t a) := @is_least_union_iff (order_dual α) _ a s t /-- If `s` is bounded, then so is `s ∩ t` -/ lemma bdd_above.inter_of_left (h : bdd_above s) : bdd_above (s ∩ t) := h.mono $ inter_subset_left s t /-- If `t` is bounded, then so is `s ∩ t` -/ lemma bdd_above.inter_of_right (h : bdd_above t) : bdd_above (s ∩ t) := h.mono $ inter_subset_right s t /-- If `s` is bounded, then so is `s ∩ t` -/ lemma bdd_below.inter_of_left (h : bdd_below s) : bdd_below (s ∩ t) := h.mono $ inter_subset_left s t /-- If `t` is bounded, then so is `s ∩ t` -/ lemma bdd_below.inter_of_right (h : bdd_below t) : bdd_below (s ∩ t) := h.mono $ inter_subset_right s t /-- If `s` and `t` are bounded above sets in a `semilattice_sup`, then so is `s ∪ t`. -/ lemma bdd_above.union [semilattice_sup γ] {s t : set γ} : bdd_above s → bdd_above t → bdd_above (s ∪ t) := begin rintros ⟨bs, hs⟩ ⟨bt, ht⟩, use bs ⊔ bt, rw upper_bounds_union, exact ⟨upper_bounds_mono_mem le_sup_left hs, upper_bounds_mono_mem le_sup_right ht⟩ end /-- The union of two sets is bounded above if and only if each of the sets is. -/ lemma bdd_above_union [semilattice_sup γ] {s t : set γ} : bdd_above (s ∪ t) ↔ bdd_above s ∧ bdd_above t := ⟨λ h, ⟨h.mono $ subset_union_left s t, h.mono $ subset_union_right s t⟩, λ h, h.1.union h.2⟩ lemma bdd_below.union [semilattice_inf γ] {s t : set γ} : bdd_below s → bdd_below t → bdd_below (s ∪ t) := @bdd_above.union (order_dual γ) _ s t /--The union of two sets is bounded above if and only if each of the sets is.-/ lemma bdd_below_union [semilattice_inf γ] {s t : set γ} : bdd_below (s ∪ t) ↔ bdd_below s ∧ bdd_below t := @bdd_above_union (order_dual γ) _ s t /-- If `a` is the least upper bound of `s` and `b` is the least upper bound of `t`, then `a ⊔ b` is the least upper bound of `s ∪ t`. -/ lemma is_lub.union [semilattice_sup γ] {a b : γ} {s t : set γ} (hs : is_lub s a) (ht : is_lub t b) : is_lub (s ∪ t) (a ⊔ b) := ⟨assume c h, h.cases_on (λ h, le_sup_left_of_le $ hs.left h) (λ h, le_sup_right_of_le $ ht.left h), assume c hc, sup_le (hs.right $ assume d hd, hc $ or.inl hd) (ht.right $ assume d hd, hc $ or.inr hd)⟩ /-- If `a` is the greatest lower bound of `s` and `b` is the greatest lower bound of `t`, then `a ⊓ b` is the greatest lower bound of `s ∪ t`. -/ lemma is_glb.union [semilattice_inf γ] {a₁ a₂ : γ} {s t : set γ} (hs : is_glb s a₁) (ht : is_glb t a₂) : is_glb (s ∪ t) (a₁ ⊓ a₂) := @is_lub.union (order_dual γ) _ _ _ _ _ hs ht /-- If `a` is the least element of `s` and `b` is the least element of `t`, then `min a b` is the least element of `s ∪ t`. -/ lemma is_least.union [linear_order γ] {a b : γ} {s t : set γ} (ha : is_least s a) (hb : is_least t b) : is_least (s ∪ t) (min a b) := ⟨by cases (le_total a b) with h h; simp [h, ha.1, hb.1], (ha.is_glb.union hb.is_glb).1⟩ /-- If `a` is the greatest element of `s` and `b` is the greatest element of `t`, then `max a b` is the greatest element of `s ∪ t`. -/ lemma is_greatest.union [linear_order γ] {a b : γ} {s t : set γ} (ha : is_greatest s a) (hb : is_greatest t b) : is_greatest (s ∪ t) (max a b) := ⟨by cases (le_total a b) with h h; simp [h, ha.1, hb.1], (ha.is_lub.union hb.is_lub).1⟩ lemma is_lub.inter_Ici_of_mem [linear_order γ] {s : set γ} {a b : γ} (ha : is_lub s a) (hb : b ∈ s) : is_lub (s ∩ Ici b) a := ⟨λ x hx, ha.1 hx.1, λ c hc, have hbc : b ≤ c, from hc ⟨hb, le_rfl⟩, ha.2 $ λ x hx, (le_total x b).elim (λ hxb, hxb.trans hbc) $ λ hbx, hc ⟨hx, hbx⟩⟩ lemma is_glb.inter_Iic_of_mem [linear_order γ] {s : set γ} {a b : γ} (ha : is_glb s a) (hb : b ∈ s) : is_glb (s ∩ Iic b) a := @is_lub.inter_Ici_of_mem (order_dual γ) _ _ _ _ ha hb /-! ### Specific sets #### Unbounded intervals -/ lemma is_least_Ici : is_least (Ici a) a := ⟨left_mem_Ici, λ x, id⟩ lemma is_greatest_Iic : is_greatest (Iic a) a := ⟨right_mem_Iic, λ x, id⟩ lemma is_lub_Iic : is_lub (Iic a) a := is_greatest_Iic.is_lub lemma is_glb_Ici : is_glb (Ici a) a := is_least_Ici.is_glb lemma upper_bounds_Iic : upper_bounds (Iic a) = Ici a := is_lub_Iic.upper_bounds_eq lemma lower_bounds_Ici : lower_bounds (Ici a) = Iic a := is_glb_Ici.lower_bounds_eq lemma bdd_above_Iic : bdd_above (Iic a) := is_lub_Iic.bdd_above lemma bdd_below_Ici : bdd_below (Ici a) := is_glb_Ici.bdd_below lemma bdd_above_Iio : bdd_above (Iio a) := ⟨a, λ x hx, le_of_lt hx⟩ lemma bdd_below_Ioi : bdd_below (Ioi a) := ⟨a, λ x hx, le_of_lt hx⟩ section variables [linear_order γ] [densely_ordered γ] lemma is_lub_Iio {a : γ} : is_lub (Iio a) a := ⟨λ x hx, le_of_lt hx, λ y hy, le_of_forall_ge_of_dense hy⟩ lemma is_glb_Ioi {a : γ} : is_glb (Ioi a) a := @is_lub_Iio (order_dual γ) _ _ a lemma upper_bounds_Iio {a : γ} : upper_bounds (Iio a) = Ici a := is_lub_Iio.upper_bounds_eq lemma lower_bounds_Ioi {a : γ} : lower_bounds (Ioi a) = Iic a := is_glb_Ioi.lower_bounds_eq end /-! #### Singleton -/ lemma is_greatest_singleton : is_greatest {a} a := ⟨mem_singleton a, λ x hx, le_of_eq $ eq_of_mem_singleton hx⟩ lemma is_least_singleton : is_least {a} a := @is_greatest_singleton (order_dual α) _ a lemma is_lub_singleton : is_lub {a} a := is_greatest_singleton.is_lub lemma is_glb_singleton : is_glb {a} a := is_least_singleton.is_glb lemma bdd_above_singleton : bdd_above ({a} : set α) := is_lub_singleton.bdd_above lemma bdd_below_singleton : bdd_below ({a} : set α) := is_glb_singleton.bdd_below @[simp] lemma upper_bounds_singleton : upper_bounds {a} = Ici a := is_lub_singleton.upper_bounds_eq @[simp] lemma lower_bounds_singleton : lower_bounds {a} = Iic a := is_glb_singleton.lower_bounds_eq /-! #### Bounded intervals -/ lemma bdd_above_Icc : bdd_above (Icc a b) := ⟨b, λ _, and.right⟩ lemma bdd_below_Icc : bdd_below (Icc a b) := ⟨a, λ _, and.left⟩ lemma bdd_above_Ico : bdd_above (Ico a b) := bdd_above_Icc.mono Ico_subset_Icc_self lemma bdd_below_Ico : bdd_below (Ico a b) := bdd_below_Icc.mono Ico_subset_Icc_self lemma bdd_above_Ioc : bdd_above (Ioc a b) := bdd_above_Icc.mono Ioc_subset_Icc_self lemma bdd_below_Ioc : bdd_below (Ioc a b) := bdd_below_Icc.mono Ioc_subset_Icc_self lemma bdd_above_Ioo : bdd_above (Ioo a b) := bdd_above_Icc.mono Ioo_subset_Icc_self lemma bdd_below_Ioo : bdd_below (Ioo a b) := bdd_below_Icc.mono Ioo_subset_Icc_self lemma is_greatest_Icc (h : a ≤ b) : is_greatest (Icc a b) b := ⟨right_mem_Icc.2 h, λ x, and.right⟩ lemma is_lub_Icc (h : a ≤ b) : is_lub (Icc a b) b := (is_greatest_Icc h).is_lub lemma upper_bounds_Icc (h : a ≤ b) : upper_bounds (Icc a b) = Ici b := (is_lub_Icc h).upper_bounds_eq lemma is_least_Icc (h : a ≤ b) : is_least (Icc a b) a := ⟨left_mem_Icc.2 h, λ x, and.left⟩ lemma is_glb_Icc (h : a ≤ b) : is_glb (Icc a b) a := (is_least_Icc h).is_glb lemma lower_bounds_Icc (h : a ≤ b) : lower_bounds (Icc a b) = Iic a := (is_glb_Icc h).lower_bounds_eq lemma is_greatest_Ioc (h : a < b) : is_greatest (Ioc a b) b := ⟨right_mem_Ioc.2 h, λ x, and.right⟩ lemma is_lub_Ioc (h : a < b) : is_lub (Ioc a b) b := (is_greatest_Ioc h).is_lub lemma upper_bounds_Ioc (h : a < b) : upper_bounds (Ioc a b) = Ici b := (is_lub_Ioc h).upper_bounds_eq lemma is_least_Ico (h : a < b) : is_least (Ico a b) a := ⟨left_mem_Ico.2 h, λ x, and.left⟩ lemma is_glb_Ico (h : a < b) : is_glb (Ico a b) a := (is_least_Ico h).is_glb lemma lower_bounds_Ico (h : a < b) : lower_bounds (Ico a b) = Iic a := (is_glb_Ico h).lower_bounds_eq section variables [linear_order γ] [densely_ordered γ] lemma is_glb_Ioo {a b : γ} (hab : a < b) : is_glb (Ioo a b) a := begin refine ⟨λx hx, le_of_lt hx.1, λy hy, le_of_not_lt $ λ h, _⟩, have : a < min b y, by { rw lt_min_iff, exact ⟨hab, h⟩ }, rcases exists_between this with ⟨z, az, zy⟩, rw lt_min_iff at zy, exact lt_irrefl _ (lt_of_le_of_lt (hy ⟨az, zy.1⟩) zy.2) end lemma lower_bounds_Ioo {a b : γ} (hab : a < b) : lower_bounds (Ioo a b) = Iic a := (is_glb_Ioo hab).lower_bounds_eq lemma is_glb_Ioc {a b : γ} (hab : a < b) : is_glb (Ioc a b) a := (is_glb_Ioo hab).of_subset_of_superset (is_glb_Icc $ le_of_lt hab) Ioo_subset_Ioc_self Ioc_subset_Icc_self lemma lower_bound_Ioc {a b : γ} (hab : a < b) : lower_bounds (Ioc a b) = Iic a := (is_glb_Ioc hab).lower_bounds_eq lemma is_lub_Ioo {a b : γ} (hab : a < b) : is_lub (Ioo a b) b := by simpa only [dual_Ioo] using @is_glb_Ioo (order_dual γ) _ _ b a hab lemma upper_bounds_Ioo {a b : γ} (hab : a < b) : upper_bounds (Ioo a b) = Ici b := (is_lub_Ioo hab).upper_bounds_eq lemma is_lub_Ico {a b : γ} (hab : a < b) : is_lub (Ico a b) b := by simpa only [dual_Ioc] using @is_glb_Ioc (order_dual γ) _ _ b a hab lemma upper_bounds_Ico {a b : γ} (hab : a < b) : upper_bounds (Ico a b) = Ici b := (is_lub_Ico hab).upper_bounds_eq end lemma bdd_below_iff_subset_Ici : bdd_below s ↔ ∃ a, s ⊆ Ici a := iff.rfl lemma bdd_above_iff_subset_Iic : bdd_above s ↔ ∃ a, s ⊆ Iic a := iff.rfl lemma bdd_below_bdd_above_iff_subset_Icc : bdd_below s ∧ bdd_above s ↔ ∃ a b, s ⊆ Icc a b := by simp only [Ici_inter_Iic.symm, subset_inter_iff, bdd_below_iff_subset_Ici, bdd_above_iff_subset_Iic, exists_and_distrib_left, exists_and_distrib_right] /-! ### Univ -/ lemma order_top.upper_bounds_univ [order_top γ] : upper_bounds (univ : set γ) = {⊤} := set.ext $ λ b, iff.trans ⟨λ hb, top_unique $ hb trivial, λ hb x hx, hb.symm ▸ le_top⟩ mem_singleton_iff.symm lemma is_greatest_univ [order_top γ] : is_greatest (univ : set γ) ⊤ := by simp only [is_greatest, order_top.upper_bounds_univ, mem_univ, mem_singleton, true_and] lemma is_lub_univ [order_top γ] : is_lub (univ : set γ) ⊤ := is_greatest_univ.is_lub lemma order_bot.lower_bounds_univ [order_bot γ] : lower_bounds (univ : set γ) = {⊥} := @order_top.upper_bounds_univ (order_dual γ) _ lemma is_least_univ [order_bot γ] : is_least (univ : set γ) ⊥ := @is_greatest_univ (order_dual γ) _ lemma is_glb_univ [order_bot γ] : is_glb (univ : set γ) ⊥ := is_least_univ.is_glb lemma no_top_order.upper_bounds_univ [no_top_order α] : upper_bounds (univ : set α) = ∅ := eq_empty_of_subset_empty $ λ b hb, let ⟨x, hx⟩ := no_top b in not_le_of_lt hx (hb trivial) lemma no_bot_order.lower_bounds_univ [no_bot_order α] : lower_bounds (univ : set α) = ∅ := @no_top_order.upper_bounds_univ (order_dual α) _ _ /-! ### Empty set -/ @[simp] lemma upper_bounds_empty : upper_bounds (∅ : set α) = univ := by simp only [upper_bounds, eq_univ_iff_forall, mem_set_of_eq, ball_empty_iff, forall_true_iff] @[simp] lemma lower_bounds_empty : lower_bounds (∅ : set α) = univ := @upper_bounds_empty (order_dual α) _ @[simp] lemma bdd_above_empty [nonempty α] : bdd_above (∅ : set α) := by simp only [bdd_above, upper_bounds_empty, univ_nonempty] @[simp] lemma bdd_below_empty [nonempty α] : bdd_below (∅ : set α) := by simp only [bdd_below, lower_bounds_empty, univ_nonempty] lemma is_glb_empty [order_top γ] : is_glb ∅ (⊤:γ) := by simp only [is_glb, lower_bounds_empty, is_greatest_univ] lemma is_lub_empty [order_bot γ] : is_lub ∅ (⊥:γ) := @is_glb_empty (order_dual γ) _ lemma is_lub.nonempty [no_bot_order α] (hs : is_lub s a) : s.nonempty := let ⟨a', ha'⟩ := no_bot a in ne_empty_iff_nonempty.1 $ assume h, have a ≤ a', from hs.right $ by simp only [h, upper_bounds_empty], not_le_of_lt ha' this lemma is_glb.nonempty [no_top_order α] (hs : is_glb s a) : s.nonempty := @is_lub.nonempty (order_dual α) _ _ _ _ hs lemma nonempty_of_not_bdd_above [ha : nonempty α] (h : ¬bdd_above s) : s.nonempty := nonempty.elim ha $ λ x, (not_bdd_above_iff'.1 h x).imp $ λ a ha, ha.fst lemma nonempty_of_not_bdd_below [ha : nonempty α] (h : ¬bdd_below s) : s.nonempty := @nonempty_of_not_bdd_above (order_dual α) _ _ _ h /-! ### insert -/ /-- Adding a point to a set preserves its boundedness above. -/ @[simp] lemma bdd_above_insert [semilattice_sup γ] (a : γ) {s : set γ} : bdd_above (insert a s) ↔ bdd_above s := by simp only [insert_eq, bdd_above_union, bdd_above_singleton, true_and] lemma bdd_above.insert [semilattice_sup γ] (a : γ) {s : set γ} (hs : bdd_above s) : bdd_above (insert a s) := (bdd_above_insert a).2 hs /--Adding a point to a set preserves its boundedness below.-/ @[simp] lemma bdd_below_insert [semilattice_inf γ] (a : γ) {s : set γ} : bdd_below (insert a s) ↔ bdd_below s := by simp only [insert_eq, bdd_below_union, bdd_below_singleton, true_and] lemma bdd_below.insert [semilattice_inf γ] (a : γ) {s : set γ} (hs : bdd_below s) : bdd_below (insert a s) := (bdd_below_insert a).2 hs lemma is_lub.insert [semilattice_sup γ] (a) {b} {s : set γ} (hs : is_lub s b) : is_lub (insert a s) (a ⊔ b) := by { rw insert_eq, exact is_lub_singleton.union hs } lemma is_glb.insert [semilattice_inf γ] (a) {b} {s : set γ} (hs : is_glb s b) : is_glb (insert a s) (a ⊓ b) := by { rw insert_eq, exact is_glb_singleton.union hs } lemma is_greatest.insert [linear_order γ] (a) {b} {s : set γ} (hs : is_greatest s b) : is_greatest (insert a s) (max a b) := by { rw insert_eq, exact is_greatest_singleton.union hs } lemma is_least.insert [linear_order γ] (a) {b} {s : set γ} (hs : is_least s b) : is_least (insert a s) (min a b) := by { rw insert_eq, exact is_least_singleton.union hs } @[simp] lemma upper_bounds_insert (a : α) (s : set α) : upper_bounds (insert a s) = Ici a ∩ upper_bounds s := by rw [insert_eq, upper_bounds_union, upper_bounds_singleton] @[simp] lemma lower_bounds_insert (a : α) (s : set α) : lower_bounds (insert a s) = Iic a ∩ lower_bounds s := by rw [insert_eq, lower_bounds_union, lower_bounds_singleton] /-- When there is a global maximum, every set is bounded above. -/ @[simp] protected lemma order_top.bdd_above [order_top γ] (s : set γ) : bdd_above s := ⟨⊤, assume a ha, order_top.le_top a⟩ /-- When there is a global minimum, every set is bounded below. -/ @[simp] protected lemma order_bot.bdd_below [order_bot γ] (s : set γ) : bdd_below s := ⟨⊥, assume a ha, order_bot.bot_le a⟩ /-! ### Pair -/ lemma is_lub_pair [semilattice_sup γ] {a b : γ} : is_lub {a, b} (a ⊔ b) := is_lub_singleton.insert _ lemma is_glb_pair [semilattice_inf γ] {a b : γ} : is_glb {a, b} (a ⊓ b) := is_glb_singleton.insert _ lemma is_least_pair [linear_order γ] {a b : γ} : is_least {a, b} (min a b) := is_least_singleton.insert _ lemma is_greatest_pair [linear_order γ] {a b : γ} : is_greatest {a, b} (max a b) := is_greatest_singleton.insert _ end /-! ### (In)equalities with the least upper bound and the greatest lower bound -/ section preorder variables [preorder α] {s : set α} {a b : α} lemma lower_bounds_le_upper_bounds (ha : a ∈ lower_bounds s) (hb : b ∈ upper_bounds s) : s.nonempty → a ≤ b | ⟨c, hc⟩ := le_trans (ha hc) (hb hc) lemma is_glb_le_is_lub (ha : is_glb s a) (hb : is_lub s b) (hs : s.nonempty) : a ≤ b := lower_bounds_le_upper_bounds ha.1 hb.1 hs lemma is_lub_lt_iff (ha : is_lub s a) : a < b ↔ ∃ c ∈ upper_bounds s, c < b := ⟨λ hb, ⟨a, ha.1, hb⟩, λ ⟨c, hcs, hcb⟩, lt_of_le_of_lt (ha.2 hcs) hcb⟩ lemma lt_is_glb_iff (ha : is_glb s a) : b < a ↔ ∃ c ∈ lower_bounds s, b < c := @is_lub_lt_iff (order_dual α) _ s _ _ ha lemma le_of_is_lub_le_is_glb {x y} (ha : is_glb s a) (hb : is_lub s b) (hab : b ≤ a) (hx : x ∈ s) (hy : y ∈ s) : x ≤ y := calc x ≤ b : hb.1 hx ... ≤ a : hab ... ≤ y : ha.1 hy end preorder section partial_order variables [partial_order α] {s : set α} {a b : α} lemma is_least.unique (Ha : is_least s a) (Hb : is_least s b) : a = b := le_antisymm (Ha.right Hb.left) (Hb.right Ha.left) lemma is_least.is_least_iff_eq (Ha : is_least s a) : is_least s b ↔ a = b := iff.intro Ha.unique (assume h, h ▸ Ha) lemma is_greatest.unique (Ha : is_greatest s a) (Hb : is_greatest s b) : a = b := le_antisymm (Hb.right Ha.left) (Ha.right Hb.left) lemma is_greatest.is_greatest_iff_eq (Ha : is_greatest s a) : is_greatest s b ↔ a = b := iff.intro Ha.unique (assume h, h ▸ Ha) lemma is_lub.unique (Ha : is_lub s a) (Hb : is_lub s b) : a = b := Ha.unique Hb lemma is_glb.unique (Ha : is_glb s a) (Hb : is_glb s b) : a = b := Ha.unique Hb lemma set.subsingleton_of_is_lub_le_is_glb (Ha : is_glb s a) (Hb : is_lub s b) (hab : b ≤ a) : s.subsingleton := λ x hx y hy, le_antisymm (le_of_is_lub_le_is_glb Ha Hb hab hx hy) (le_of_is_lub_le_is_glb Ha Hb hab hy hx) lemma is_glb_lt_is_lub_of_ne (Ha : is_glb s a) (Hb : is_lub s b) {x y} (Hx : x ∈ s) (Hy : y ∈ s) (Hxy : x ≠ y) : a < b := lt_iff_le_not_le.2 ⟨lower_bounds_le_upper_bounds Ha.1 Hb.1 ⟨x, Hx⟩, λ hab, Hxy $ set.subsingleton_of_is_lub_le_is_glb Ha Hb hab Hx Hy⟩ end partial_order section linear_order variables [linear_order α] {s : set α} {a b : α} lemma lt_is_lub_iff (h : is_lub s a) : b < a ↔ ∃ c ∈ s, b < c := by simp only [← not_le, is_lub_le_iff h, mem_upper_bounds, not_forall] lemma is_glb_lt_iff (h : is_glb s a) : a < b ↔ ∃ c ∈ s, c < b := @lt_is_lub_iff (order_dual α) _ _ _ _ h lemma is_lub.exists_between (h : is_lub s a) (hb : b < a) : ∃ c ∈ s, b < c ∧ c ≤ a := let ⟨c, hcs, hbc⟩ := (lt_is_lub_iff h).1 hb in ⟨c, hcs, hbc, h.1 hcs⟩ lemma is_lub.exists_between' (h : is_lub s a) (h' : a ∉ s) (hb : b < a) : ∃ c ∈ s, b < c ∧ c < a := let ⟨c, hcs, hbc, hca⟩ := h.exists_between hb in ⟨c, hcs, hbc, hca.lt_of_ne $ λ hac, h' $ hac ▸ hcs⟩ lemma is_glb.exists_between (h : is_glb s a) (hb : a < b) : ∃ c ∈ s, a ≤ c ∧ c < b := let ⟨c, hcs, hbc⟩ := (is_glb_lt_iff h).1 hb in ⟨c, hcs, h.1 hcs, hbc⟩ lemma is_glb.exists_between' (h : is_glb s a) (h' : a ∉ s) (hb : a < b) : ∃ c ∈ s, a < c ∧ c < b := let ⟨c, hcs, hac, hcb⟩ := h.exists_between hb in ⟨c, hcs, hac.lt_of_ne $ λ hac, h' $ hac.symm ▸ hcs, hcb⟩ end linear_order /-! ### Least upper bound and the greatest lower bound in linear ordered additive commutative groups -/ section linear_ordered_add_comm_group variables [linear_ordered_add_comm_group α] {s : set α} {a ε : α} lemma is_glb.exists_between_self_add (h : is_glb s a) (hε : 0 < ε) : ∃ b ∈ s, a ≤ b ∧ b < a + ε := h.exists_between $ lt_add_of_pos_right _ hε lemma is_glb.exists_between_self_add' (h : is_glb s a) (h₂ : a ∉ s) (hε : 0 < ε) : ∃ b ∈ s, a < b ∧ b < a + ε := h.exists_between' h₂ $ lt_add_of_pos_right _ hε lemma is_lub.exists_between_sub_self (h : is_lub s a) (hε : 0 < ε) : ∃ b ∈ s, a - ε < b ∧ b ≤ a := h.exists_between $ sub_lt_self _ hε lemma is_lub.exists_between_sub_self' (h : is_lub s a) (h₂ : a ∉ s) (hε : 0 < ε) : ∃ b ∈ s, a - ε < b ∧ b < a := h.exists_between' h₂ $ sub_lt_self _ hε end linear_ordered_add_comm_group /-! ### Images of upper/lower bounds under monotone functions -/ namespace monotone variables [preorder α] [preorder β] {f : α → β} (Hf : monotone f) {a : α} {s : set α} lemma mem_upper_bounds_image (Ha : a ∈ upper_bounds s) : f a ∈ upper_bounds (f '' s) := ball_image_of_ball (assume x H, Hf (Ha ‹x ∈ s›)) lemma mem_lower_bounds_image (Ha : a ∈ lower_bounds s) : f a ∈ lower_bounds (f '' s) := ball_image_of_ball (assume x H, Hf (Ha ‹x ∈ s›)) /-- The image under a monotone function of a set which is bounded above is bounded above. -/ lemma map_bdd_above (hf : monotone f) : bdd_above s → bdd_above (f '' s) | ⟨C, hC⟩ := ⟨f C, hf.mem_upper_bounds_image hC⟩ /-- The image under a monotone function of a set which is bounded below is bounded below. -/ lemma map_bdd_below (hf : monotone f) : bdd_below s → bdd_below (f '' s) | ⟨C, hC⟩ := ⟨f C, hf.mem_lower_bounds_image hC⟩ /-- A monotone map sends a least element of a set to a least element of its image. -/ lemma map_is_least (Ha : is_least s a) : is_least (f '' s) (f a) := ⟨mem_image_of_mem _ Ha.1, Hf.mem_lower_bounds_image Ha.2⟩ /-- A monotone map sends a greatest element of a set to a greatest element of its image. -/ lemma map_is_greatest (Ha : is_greatest s a) : is_greatest (f '' s) (f a) := ⟨mem_image_of_mem _ Ha.1, Hf.mem_upper_bounds_image Ha.2⟩ lemma is_lub_image_le (Ha : is_lub s a) {b : β} (Hb : is_lub (f '' s) b) : b ≤ f a := Hb.2 (Hf.mem_upper_bounds_image Ha.1) lemma le_is_glb_image (Ha : is_glb s a) {b : β} (Hb : is_glb (f '' s) b) : f a ≤ b := Hb.2 (Hf.mem_lower_bounds_image Ha.1) end monotone lemma is_glb.of_image [preorder α] [preorder β] {f : α → β} (hf : ∀ {x y}, f x ≤ f y ↔ x ≤ y) {s : set α} {x : α} (hx : is_glb (f '' s) (f x)) : is_glb s x := ⟨λ y hy, hf.1 $ hx.1 $ mem_image_of_mem _ hy, λ y hy, hf.1 $ hx.2 $ monotone.mem_lower_bounds_image (λ x y, hf.2) hy⟩ lemma is_lub.of_image [preorder α] [preorder β] {f : α → β} (hf : ∀ {x y}, f x ≤ f y ↔ x ≤ y) {s : set α} {x : α} (hx : is_lub (f '' s) (f x)) : is_lub s x := @is_glb.of_image (order_dual α) (order_dual β) _ _ f (λ x y, hf) _ _ hx
d81a2a56413900cca71843dcb17ec2f4e95b11a8
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/archive/imo/imo1959_q1.lean
fe34d1b143489a79ef9643db3bf342c32d7eccc1
[ "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,068
lean
/- Copyright (c) 2020 Kevin Lacker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Lacker -/ import tactic.ring import data.nat.prime /-! # IMO 1959 Q1 > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Prove that the fraction `(21n+4)/(14n+3)` is irreducible for every natural number `n`. Since Lean doesn't have a concept of "irreducible fractions" per se, we just formalize this as saying the numerator and denominator are relatively prime. -/ open nat namespace imo1959_q1 lemma calculation (n k : ℕ) (h1 : k ∣ 21 * n + 4) (h2 : k ∣ 14 * n + 3) : k ∣ 1 := have h3 : k ∣ 2 * (21 * n + 4), from h1.mul_left 2, have h4 : k ∣ 3 * (14 * n + 3), from h2.mul_left 3, have h5 : 3 * (14 * n + 3) = 2 * (21 * n + 4) + 1, by ring, (nat.dvd_add_right h3).mp (h5 ▸ h4) end imo1959_q1 open imo1959_q1 theorem imo1959_q1 : ∀ n : ℕ, coprime (21 * n + 4) (14 * n + 3) := assume n, coprime_of_dvd' $ λ k hp h1 h2, calculation n k h1 h2
c09cc283ae5ed506cf097094c6bd0075380028d7
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/tactic5.lean
41387388e6e49bab9257572b4693502ad2959e9e
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
219
lean
import logic open tactic (renaming id->id_tac) definition id {A : Type} (a : A) := a infixl `;`:15 := tactic.and_then theorem tst {A B : Prop} (H1 : A) (H2 : B) : id A := by (unfold id; state); assumption check tst
2cac38228cbb27ee1e2ff22bb343b078381a4301
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/int/parity.lean
491d39711cf3216ec69fce0d79cb83b8c7feb4c6
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
3,232
lean
/- Copyright (c) 2019 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad The `even` predicate on the integers. -/ import data.int.modeq import data.nat.parity namespace int @[simp] theorem mod_two_ne_one {n : int} : ¬ n % 2 = 1 ↔ n % 2 = 0 := by cases mod_two_eq_zero_or_one n with h h; simp [h] local attribute [simp] -- euclidean_domain.mod_eq_zero uses (2 ∣ n) as normal form theorem mod_two_ne_zero {n : int} : ¬ n % 2 = 0 ↔ n % 2 = 1 := by cases mod_two_eq_zero_or_one n with h h; simp [h] /-- An integer `n` is `even` if `2 | n`. -/ def even (n : int) : Prop := 2 ∣ n @[simp] theorem even_coe_nat (n : nat) : even n ↔ nat.even n := have ∀ m, 2 * to_nat m = to_nat (2 * m), from λ m, by cases m; refl, ⟨λ ⟨m, hm⟩, ⟨to_nat m, by rw [this, ←to_nat_coe_nat n, hm]⟩, λ ⟨m, hm⟩, ⟨m, by simp [hm]⟩⟩ theorem even_iff {n : int} : even n ↔ n % 2 = 0 := ⟨λ ⟨m, hm⟩, by simp [hm], λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by simp [h])⟩⟩ lemma not_even_iff {n : ℤ} : ¬ even n ↔ n % 2 = 1 := by rw [even_iff, mod_two_ne_zero] @[simp] theorem two_dvd_ne_zero {n : int} : ¬ 2 ∣ n ↔ n % 2 = 1 := not_even_iff instance : decidable_pred even := λ n, decidable_of_decidable_of_iff (by apply_instance) even_iff.symm @[simp] theorem even_zero : even (0 : int) := ⟨0, dec_trivial⟩ @[simp] theorem not_even_one : ¬ even (1 : int) := by rw even_iff; apply one_ne_zero @[simp] theorem even_bit0 (n : int) : even (bit0 n) := ⟨n, by rw [bit0, two_mul]⟩ @[parity_simps] theorem even_add {m n : int} : even (m + n) ↔ (even m ↔ even n) := begin cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂; simp [even_iff, h₁, h₂, -euclidean_domain.mod_eq_zero], { exact @modeq.modeq_add _ _ 0 _ 0 h₁ h₂ }, { exact @modeq.modeq_add _ _ 0 _ 1 h₁ h₂ }, { exact @modeq.modeq_add _ _ 1 _ 0 h₁ h₂ }, exact @modeq.modeq_add _ _ 1 _ 1 h₁ h₂ end @[parity_simps] theorem even_neg {n : ℤ} : even (-n) ↔ even n := by simp [even] @[simp] theorem not_even_bit1 (n : int) : ¬ even (bit1 n) := by simp [bit1] with parity_simps @[parity_simps] theorem even_sub {m n : int} : even (m - n) ↔ (even m ↔ even n) := by simp [sub_eq_add_neg] with parity_simps @[parity_simps] theorem even_mul {m n : int} : even (m * n) ↔ even m ∨ even n := begin cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂; simp [even_iff, h₁, h₂, -euclidean_domain.mod_eq_zero], { exact @modeq.modeq_mul _ _ 0 _ 0 h₁ h₂ }, { exact @modeq.modeq_mul _ _ 0 _ 1 h₁ h₂ }, { exact @modeq.modeq_mul _ _ 1 _ 0 h₁ h₂ }, exact @modeq.modeq_mul _ _ 1 _ 1 h₁ h₂ end @[parity_simps] theorem even_pow {m : int} {n : nat} : even (m^n) ↔ even m ∧ n ≠ 0 := by { induction n with n ih; simp [*, even_mul, pow_succ], tauto } -- Here are examples of how `parity_simps` can be used with `int`. example (m n : int) (h : even m) : ¬ even (n + 3) ↔ even (m^2 + m + n) := by simp [*, (dec_trivial : ¬ 2 = 0)] with parity_simps example : ¬ even (25394535 : int) := by simp end int
9f89f6e45458cb928f36ee77fb0546c75553ccea
94d8c57c0a90adc01592a2509d65a360540671a9
/M1F/2017-18/Example_Sheet_03/solutions.lean
6d4ab0dd239a72e467788f124355708b910ca72e
[]
no_license
TudorTitan/xena
ee58125263b84ca8787ea46778e7b4838c7b3057
389b9c40c43b26139722c88763f4d04d85467e0f
refs/heads/master
1,628,408,454,654
1,510,265,443,000
1,510,265,443,000
110,169,835
0
0
null
1,510,264,043,000
1,510,264,043,000
null
UTF-8
Lean
false
false
11,241
lean
import xenalib.M1Fstuff algebra.group_power xenalib.square_root -- Need to start off with some fake reals to do Q1,2 constant fake_reals : Type @[instance] constant fake_reals_comm_ring : comm_ring fake_reals @[instance] constant fake_reals_have_lt : has_lt fake_reals -- we define a<=b to mean a<b or a=b. Axiom 3 says that at most one occurs. @[instance] noncomputable definition fake_reals_have_le : has_le fake_reals := ⟨λ a b, (a<b) ∨ (a=b)⟩ axiom A1 {a b t : fake_reals} : a < b → a+t < b+t axiom A2 {a b c : fake_reals} : a < b → b < c → a < c axiom A3 {a b : fake_reals} : (a < b ∨ a = b ∨ b < a) ∧ (a < b → ¬ (a = b)) ∧ (a < b → ¬ (b < a)) ∧ (a = b → ¬ (b < a)) axiom A4 {a b : fake_reals} : a > 0 → b > 0 → (a*b) > 0 axiom A0 : (0 : fake_reals) ≠ (1 : fake_reals) theorem one_pos : (1:fake_reals) > 0 := begin cases (@A3 0 1).left with H1pos H1nonpos, assumption, cases H1nonpos with H1zero H1neg, exfalso, exact A0 H1zero, have H : (1:fake_reals) + (-1) < 0 + (-1), exact A1 H1neg, rw [add_neg_self,zero_add] at H, have H2 : (-1:fake_reals) * (-1) > 0, exact A4 H H, rw [←neg_eq_neg_one_mul] at H2, -- exact H2, -- oops rw [neg_neg] at H2, exact H2, end section M1F_Sheet03 -- set_option pp.all true theorem Q1 : ∀ x y : fake_reals, 0<x ∧ 0<y → 0<(x+y) := begin intros x y Hxy, have H : y < x+y := calc y = 0 + y : by simp [zero_add] ... < x+y : A1 Hxy.left, exact A2 Hxy.right H, end theorem n_pos : ∀ n : ℕ, n ≠ 0 → (n : fake_reals) > 0 := begin intro n, cases n with m, by norm_num, induction m with p Hp, intro,rw [nat.cast_one], exact one_pos, have H0 : nat.succ p ≠ 0, intro H, exact nat.no_confusion H, have H1 : ↑(nat.succ p) > (0:fake_reals), exact Hp H0, intro H,clear Hp H0 H, rw [nat.succ_eq_add_one,nat.cast_add,nat.cast_one], exact Q1 (nat.succ p) 1 ⟨H1,one_pos⟩, end -- a) We proved in lectures that if x > y and c > 0 then cx > cy. Deduce from this that the -- product of a positive number and a negative number is negative. -- #check @lt_of_sub_pos theorem mul_pos_lt_of_lt {x y c : fake_reals} : (y < x) → (0 < c) → c*y < c*x := begin intros Hx_gt_y Hc_gt_zero, have Hsub_gt_zero : 0 < (x-y), rw [sub_eq_add_neg,←(add_neg_self y)], exact A1 Hx_gt_y, -- is apply OK to finish a goal? have H2 : c*(x-y) > 0 := A4 Hc_gt_zero Hsub_gt_zero, rw [mul_sub,sub_eq_add_neg] at H2, have H3 : c*x + -(c*y)+c*y>0+c*y := A1 H2, rwa [zero_add,add_assoc,neg_add_self,add_zero] at H3, end theorem Q2a {x y : fake_reals} : (x > 0) → (y < 0) → x * y < 0 := begin intros Hx_gt_0 Hy_lt_0, have H : x*0 > x*y := mul_pos_lt_of_lt Hy_lt_0 Hx_gt_0, rwa [mul_zero] at H, end theorem neg_pos_of_neg' {x : fake_reals} : x < 0 → -x > 0 := begin intro Hx_neg, have H : x + (-x) < 0 + (-x) := A1 Hx_neg, rwa [add_neg_self,zero_add] at H, end theorem neg_eq_neg_one_mul' {x : fake_reals} : -x = (-1)*x := neg_eq_neg_one_mul x theorem neg_one_squared : (-1:fake_reals)*(-1)=1 := calc (-1:fake_reals)*(-1)=-(-1) : eq.symm (@neg_eq_neg_one_mul' (-1)) ... = 1 : neg_neg 1 theorem pos_eq_neg_mul_neg {x y : fake_reals} : x < 0 → y < 0 → x * y > 0 := begin intros Hxneg Hyneg, have Hneg_x_pos : -x > 0 := neg_pos_of_neg' Hxneg, have Hneg_y_pos : -y > 0 := neg_pos_of_neg' Hyneg, exact calc x * y = -x * -y : by rw [neg_eq_neg_one_mul',@neg_eq_neg_one_mul' y,←mul_one (x * y),←neg_one_squared];simp ... > 0 : A4 Hneg_x_pos Hneg_y_pos, end theorem Q2b {x y : fake_reals} : x < 0 → y < 0 → x * y > 0 := pos_eq_neg_mul_neg theorem zero_not_pos : ¬ ((0:fake_reals) < 0) := (@A3 0 0).right.right.right (rfl) theorem fake_reals_integral_domain {x y : fake_reals} : x * y = 0 → x = 0 ∨ y = 0 := begin intro Hxy_zero, cases (@A3 0 x).left with Hx_pos Hx_nonpos, cases (@A3 0 y).left with Hy_pos Hy_nonpos, exfalso, exact zero_not_pos (calc 0 < x * y : A4 Hx_pos Hy_pos ... = 0 : Hxy_zero), cases Hy_nonpos with Hy_0 Hy_neg, right,exact eq.symm Hy_0, exfalso, apply zero_not_pos, exact calc 0 = x*y : eq.symm Hxy_zero ... <0 : Q2a Hx_pos Hy_neg, cases Hx_nonpos with Hx_0 Hx_neg, left,exact eq.symm Hx_0, cases (@A3 0 y).left with Hy_pos Hy_nonpos, exfalso, apply zero_not_pos, exact calc 0=x*y : eq.symm Hxy_zero ... = y*x : by rw[mul_comm] ... <0 : Q2a Hy_pos Hx_neg, cases Hy_nonpos with Hy_0 Hy_neg, right,exact eq.symm Hy_0, exfalso, apply zero_not_pos, exact calc 0 < x * y : Q2b Hx_neg Hy_neg ... = 0 : Hxy_zero, end theorem Q2c : ∀ x y : fake_reals, x * y = 0 → x = 0 ∨ y = 0 := @fake_reals_integral_domain end M1F_Sheet03 axiom A5 : ∀ x : fake_reals, x > 0 → ∃ y : fake_reals, y > 0 ∧ y*y=x ∧ ∀ z : fake_reals, z > 0 ∧ z*z=x → z=y section M1F_Sheet03 theorem Q2d : ∀ x : fake_reals, x > 0 → ∃ z1 z2 : fake_reals, z1*z1=x ∧ z2*z2=x ∧ ∀ z : fake_reals, z*z=x → z=z1 ∨ z=z2 := begin intros x Hx_pos, have H : ∃ (y : fake_reals), y > 0 ∧ y * y = x ∧ ∀ (z : fake_reals), z > 0 ∧ z * z = x → z = y, exact A5 x Hx_pos, cases H with y Hy, existsi y, existsi -y, split, exact Hy.right.left, split, rw [neg_mul_neg], exact Hy.right.left, intro z, intro Hz2, have Hz2_eq_y2 : z*z = y*y := eq.symm Hy.right.left ▸ Hz2, have Hcases : (0 < z ∨ 0 = z ∨ z < 0) := (@A3 0 z).left, cases Hcases with Hz_neg Hz_nonneg, left, exact Hy.right.right z ⟨Hz_neg,Hz2⟩, cases Hz_nonneg with Hz_0 Hz_pos, exfalso, rw [←Hz_0,mul_zero] at Hz2, rw [←Hz2] at Hx_pos, revert Hx_pos, exact zero_not_pos, right, apply eq.symm, apply neg_eq_iff_neg_eq.1, apply Hy.right.right, split, have H : z + (-z) < 0 + (-z) := A1 Hz_pos, rwa [add_neg_self,zero_add] at H, simp [Hz2], end end M1F_Sheet03 axiom A6 : ∀ n : ℕ, n > 0 → ∀ x : fake_reals, x > 0 → ∃ y : fake_reals, y > 0 ∧ y ^ n = x ∧ ∀ z : fake_reals, z > 0 ∧ z ^ n = x → z = y section M1F_Sheet03 -- some definitions before Q3a theorem three_not_zero : (3:ℕ) ≠ 0 := by norm_num theorem two_not_zero : (2:ℕ) ≠ 0 := by norm_num -- set_option pp.notation false theorem pow_pos_of_pos (x : fake_reals) (n : ℕ) : (0 < x) → (0 < n) → 0 < x^n := begin intros Hx_pos Hn_pos, cases n with m, revert Hn_pos,norm_num, clear Hn_pos, induction m with p Hp, simp [Hx_pos], exact A4 Hx_pos Hp, end theorem pow_lt_of_lt (x y : fake_reals) (n : ℕ) : (0 < x) → (x < y) → (0 < n) → x ^ n < y^n := begin intros Hx_pos Hx_lt_y Hn_pos, cases n with m, exfalso, revert Hn_pos,norm_num, induction m with p Hp, simp [Hx_lt_y], have H : x^ nat.succ p < y^nat.succ p := Hp (nat.zero_lt_succ p), clear Hp Hn_pos, change x ^ nat.succ (nat.succ p) with x * (x^nat.succ p), change y ^ nat.succ (nat.succ p) with y * (y^nat.succ p), have H1: x * (x ^ nat.succ p) < y * (x ^ nat.succ p) := calc x * (x ^ nat.succ p) = (x ^ nat.succ p) * x : by rw [mul_comm] ... < (x ^ nat.succ p) * y : mul_pos_lt_of_lt Hx_lt_y (pow_pos_of_pos x (nat.succ p) Hx_pos (nat.zero_lt_succ p)) ... = y * (x ^ nat.succ p) : by rw [mul_comm], have H2 : y * (x ^ nat.succ p) < y * (y ^ nat.succ p) := mul_pos_lt_of_lt H (A2 Hx_pos Hx_lt_y), exact A2 H1 H2 end def n:ℕ := 1000000000000 def t3_stuff := A6 3000000000000 (by norm_num) ↑3 (n_pos 3 (three_not_zero)) def t2_stuff := A6 2000000000000 (by norm_num) ↑2 (n_pos 2 (two_not_zero)) noncomputable def t2 := classical.some t2_stuff noncomputable def t3 := classical.some t3_stuff def t2_facts := classical.some_spec t2_stuff def t3_facts := classical.some_spec t3_stuff theorem Q3a : t3 > t2 := -- these are both fake reals. begin have H3 : t3 ^ (6*n) = ↑9, change 6 with 2*3, rw [mul_assoc,mul_comm,pow_mul], have H3trill : 3*n=3000000000000 := by change n with 1000000000000;norm_num, have Htemp : t3 ^ (3*n) = ↑3, rw [H3trill], exact t3_facts.right.left, rw [Htemp], norm_num, have H2 : t2 ^ (6*n) = ↑8, change 6 with 3*2, rw [mul_assoc,mul_comm,pow_mul], have H2trill : 2*n=2000000000000 := by change n with 1000000000000;norm_num, have Htemp : t2 ^ (2*n) = ↑2, rw [H2trill], exact t2_facts.right.left, rw [Htemp], norm_num, have Hlt : t2 ^ (6*n) < t3 ^ (6*n), rw [H2,H3], change 8 with 0+8, change 9 with 1+8, rw [nat.cast_add,nat.cast_add,nat.cast_zero,nat.cast_one], exact A1 one_pos, clear H2 H3, have Hneq : ¬ (t2^(6*n) = t3^(6*n)), exact A3.right.left Hlt, have Hngt : ¬ (t2^(6*n) > t3^(6*n)), exact A3.right.right.left Hlt, cases (@A3 t2 t3).left with Hlt Hge, exact Hlt, exfalso, cases Hge with Heq Hgt, rw Heq at Hneq, apply Hneq, trivial, apply Hngt, apply pow_lt_of_lt, exact t3_facts.left, exact Hgt, apply mul_pos, norm_num, change n with 1000000000000, norm_num, end -- I've done the next two parts with integers, on the basis that -- inequality on the reals extends inequality on the integers. -- ambiguous overload for power ^ symbol :-( -- Could mean nat.pow or pow_nat. -- Here's something that's in core lean for nat.pow -- and we need for pow_nat. theorem pow_lt_pow_of_lt {x i j : ℕ} : x > 1 → i < j → pow_nat x i < pow_nat x j := begin rw [←nat.pow_eq_pow_nat,←nat.pow_eq_pow_nat], intro H, exact nat.pow_lt_pow_of_lt_right H, end theorem Q3b : pow_nat 10000 100 < pow_nat 100 10000 := begin have H : 10000 = pow_nat 100 2, { norm_num }, have H2 : pow_nat 10000 100=pow_nat (pow_nat 100 2) 100, rw [H], rw [H2,←pow_mul], have H3 : 2*100 = 200, { norm_num}, rw [H3], apply pow_lt_pow_of_lt, { norm_num }, norm_num, end theorem Q3ci : pow_nat (pow_nat 2 11) 2 = pow_nat 2 22 := begin rw [←pow_mul], change 11*2 with 22, trivial, end theorem Q3cii : pow_nat (pow_nat 2 (pow_nat 2 21)) 2 = pow_nat 2 (pow_nat 2 22) := begin rw [←pow_mul], suffices H : pow_nat 2 22 = (pow_nat 2 21) * 2, rw [H], change 22 with 21+1, rw [pow_add], change pow_nat 2 1 with 2, trivial, end -- set_option pp.notation false #print sub_neg_of_lt #check @lt_of_sub_neg universe u #print ordered_comm_group theorem lt_iff_sub_neg : ∀ {α : Type u} [ordered_comm_group α] {a b : α}, a - b < 0 ↔ a < b := begin admit end theorem Q4 : { x : ℝ | x ≠ 0 ∧ 3*x + 1/x < 4 } = {x : ℝ | x<0 ∨ ( ((1:ℝ)/3)<x ∧ x<1) } := begin apply funext, intro x, apply propext, -- unfolded goal is x ≠ 0 ∧ 3 * x + 1 / x < 4 ↔ x < 0 ∨ 1 / 3 < x ∧ x < 1 -- I should now prove 3x+1/x-4 = (3x^2-1-4x)/x=(3x-1)(x-1)/x have Hkey : x ≠ 0 → 3*x+1/x-4 = (3*x-1)*(x-1)/x, have Htemp : (4:ℝ)=(3:ℝ)+(1:ℝ) := by norm_num, rw [Htemp], intro Hx_ne_0, apply eq_div_of_mul_eq _ _ Hx_ne_0, simp [Hx_ne_0,mul_add,add_mul], /- is this useful? have Hx_squared_pos : 0 < x*x, cases lt_or_gt_of_ne H.left with Hx_lt_0 Hx_gt_0, exact mul_pos_of_neg_of_neg Hx_lt_0 Hx_lt_0, exact mul_pos Hx_gt_0 Hx_gt_0, -/ have sub_neg_iff_lt [] rw [sub_neg_of_lt], split, intro Hleft, have Hx_ne_0 := Hleft.left, have Hx_eq := Hleft.right, clear Hleft, have Hx_eq2 := sub_neg_of_lt Hx_eq, rw [Hkey Hx_ne_0] at Hx_eq2, repeat {admit}, end end M1F_Sheet03
33ea081d1561f2a375ce7e8f6a85622a77de4ad8
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/src/Lean/Elab/PreDefinition.lean
eb4b66cd932acb92fbd92daa1791069148733cfd
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
279
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.Elab.PreDefinition.Basic import Lean.Elab.PreDefinition.Structural import Lean.Elab.PreDefinition.Main
26023eb153d07a0ca042ab89a6abe5bd0774cb8a
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/t13.lean
70c7ac41136f6bc5015fbcd9a684def7c88027d7
[ "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
329
lean
prelude constant A : Type.{1} constant f : A → A → A constant g : A → A → A precedence `+` : 65 infixl + := f infixl + := g constant a : A constant b : A #print raw a+b -- + is overloaded #check fun (h : A → A → A) (infixl + := h), -- Like local declarations, local notation "shadows" global one. a+b
fe97b46801ef5d51e8c8cc5087ef9929b8edb642
36938939954e91f23dec66a02728db08a7acfcf9
/lean/deps/galois_stdlib/src/galois/data/list/with_mem.lean
b8bc30fab0912d03eb6d406be594c897cef193f2
[ "Apache-2.0" ]
permissive
pnwamk/reopt-vcg
f8b56dd0279392a5e1c6aee721be8138e6b558d3
c9f9f185fbefc25c36c4b506bbc85fd1a03c3b6d
refs/heads/master
1,631,145,017,772
1,593,549,019,000
1,593,549,143,000
254,191,418
0
0
null
1,586,377,077,000
1,586,377,077,000
null
UTF-8
Lean
false
false
3,034
lean
namespace list def map_with_mem {α} {β: Sort _} : Π(l:list α), (Π(x:α), x ∈ l → β) → list β | [] f := [] | (h::r) f := f h (or.inl rfl) :: map_with_mem r (λ(x:α) (p:x ∈ r), f x (or.inr p)) /- Elements in a list have a smaller size than the list. -/ theorem mem_sizeof {α} [s : has_sizeof α] {x : α} {l : list α} (p : x ∈ l) : sizeof x < l.sizeof := begin induction l, case nil { exact false.elim p, }, case cons : h r ind { simp [has_mem.mem, list.mem] at p, simp [list.sizeof, nat.succ_add], apply nat.succ_le_succ, cases p, case or.inl : x_eq_h { simp [x_eq_h], apply nat.le_add_left, }, case or.inr : x_in_r { transitivity, exact le_of_lt (ind x_in_r), apply nat.le_add_right, }, }, end --- A decidable test for lists using a test for elements that -- includes proof only list elements are queried. def has_dec_eq_with_mem {α} : Π (k l : list α) (p : Π{a : α}, a ∈ k → Π{b:α}, b ∈ l → decidable (a = b)), decidable (k = l) | [] [] p := is_true rfl | (a::as) [] p := is_false (λ h, list.no_confusion h) | [] (b::bs) p := is_false (λ h, list.no_confusion h) | (a::as) (b::bs) p := let ma : a ∈ (a::as) := or.inl rfl in let mb : b ∈ (b::bs) := or.inl rfl in match p ma mb with | is_true hab := let q (x:α) (xm:x ∈ as) (y:α) (ym:y ∈ bs) := p (or.inr xm) (or.inr ym) in match has_dec_eq_with_mem as bs q with | is_true habs := is_true (eq.subst hab (eq.subst habs rfl)) | is_false nabs := is_false (λ h, list.no_confusion h (λ _ habs, absurd habs nabs)) end | is_false nab := is_false (λ h, list.no_confusion h (λ hab _, absurd hab nab)) end /- Predicate that returns true if two lists have same length and all pairswise elements are equivalent. -/ inductive holds_pairwise {α} {β} (P : α → β → Prop) : list α -> list β -> Prop | nil {} : holds_pairwise [] [] | cons {} {x:α} {xr : list α} {y:β} {yr:list β} : P x y → holds_pairwise xr yr → holds_pairwise (x::xr) (y::yr) namespace holds_pairwise open decidable /- Decide the holds pairwise relation. -/ def decide_with_mem {α} {β} (r : α → β → Prop) : Π(x:list α) (y:list β), (Π(a:α), a ∈ x → Pi(b:β), b ∈ y → decidable (r a b)) → decidable (holds_pairwise r x y) | [] [] _ := is_true nil | [] (yh::yr) _ := is_false (by { intro p, cases p, }) | (xh::xr) [] _ := is_false (by { intro p, cases p, }) | (xh::xr) (yh::yr) p := match p xh (or.inl rfl) yh (or.inl rfl) with | is_true ph := let q := λa am b bm, p a (or.inr am) b (or.inr bm) in match decide_with_mem xr yr q with | is_true pr := is_true (cons ph pr) | is_false npr := is_false begin intro p, cases p with x yr y yr ph pr, exact npr pr, end end | is_false nph := is_false begin intro p, cases p with x yr y yr ph pr, exact nph ph, end end end holds_pairwise end list
1b6ee06944b0db0c88441d1e5240fc191be5f1ae
b3fced0f3ff82d577384fe81653e47df68bb2fa1
/src/category_theory/comma.lean
fdb2933d659bc8657182b1ae099ad0b9cc53f824
[ "Apache-2.0" ]
permissive
ratmice/mathlib
93b251ef5df08b6fd55074650ff47fdcc41a4c75
3a948a6a4cd5968d60e15ed914b1ad2f4423af8d
refs/heads/master
1,599,240,104,318
1,572,981,183,000
1,572,981,183,000
219,830,178
0
0
Apache-2.0
1,572,980,897,000
1,572,980,896,000
null
UTF-8
Lean
false
false
11,340
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johan Commelin -/ import category_theory.isomorphism import category_theory.punit namespace category_theory universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation variables {A : Type u₁} [𝒜 : category.{v₁} A] variables {B : Type u₂} [ℬ : category.{v₂} B] variables {T : Type u₃} [𝒯 : category.{v₃} T] include 𝒜 ℬ 𝒯 structure comma (L : A ⥤ T) (R : B ⥤ T) : Type (max u₁ u₂ v₃) := (left : A . obviously) (right : B . obviously) (hom : L.obj left ⟶ R.obj right) variables {L : A ⥤ T} {R : B ⥤ T} structure comma_morphism (X Y : comma L R) := (left : X.left ⟶ Y.left . obviously) (right : X.right ⟶ Y.right . obviously) (w' : L.map left ≫ Y.hom = X.hom ≫ R.map right . obviously) restate_axiom comma_morphism.w' attribute [simp] comma_morphism.w namespace comma_morphism @[extensionality] lemma ext {X Y : comma L R} {f g : comma_morphism X Y} (l : f.left = g.left) (r : f.right = g.right) : f = g := begin cases f, cases g, congr; assumption end end comma_morphism instance comma_category : category (comma L R) := { hom := comma_morphism, id := λ X, { left := 𝟙 X.left, right := 𝟙 X.right }, comp := λ X Y Z f g, { left := f.left ≫ g.left, right := f.right ≫ g.right, w' := begin rw [functor.map_comp, category.assoc, g.w, ←category.assoc, f.w, functor.map_comp, category.assoc], end }} namespace comma section variables {X Y Z : comma L R} {f : X ⟶ Y} {g : Y ⟶ Z} @[simp] lemma comp_left : (f ≫ g).left = f.left ≫ g.left := rfl @[simp] lemma comp_right : (f ≫ g).right = f.right ≫ g.right := rfl end variables (L) (R) def fst : comma L R ⥤ A := { obj := λ X, X.left, map := λ _ _ f, f.left } def snd : comma L R ⥤ B := { obj := λ X, X.right, map := λ _ _ f, f.right } @[simp] lemma fst_obj {X : comma L R} : (fst L R).obj X = X.left := rfl @[simp] lemma snd_obj {X : comma L R} : (snd L R).obj X = X.right := rfl @[simp] lemma fst_map {X Y : comma L R} {f : X ⟶ Y} : (fst L R).map f = f.left := rfl @[simp] lemma snd_map {X Y : comma L R} {f : X ⟶ Y} : (snd L R).map f = f.right := rfl def nat_trans : fst L R ⋙ L ⟶ snd L R ⋙ R := { app := λ X, X.hom } section variables {L₁ L₂ L₃ : A ⥤ T} {R₁ R₂ R₃ : B ⥤ T} def map_left (l : L₁ ⟶ L₂) : comma L₂ R ⥤ comma L₁ R := { obj := λ X, { left := X.left, right := X.right, hom := l.app X.left ≫ X.hom }, map := λ X Y f, { left := f.left, right := f.right, w' := by tidy; rw [←category.assoc, l.naturality f.left, category.assoc]; tidy } } section variables {X Y : comma L₂ R} {f : X ⟶ Y} {l : L₁ ⟶ L₂} @[simp] lemma map_left_obj_left : ((map_left R l).obj X).left = X.left := rfl @[simp] lemma map_left_obj_right : ((map_left R l).obj X).right = X.right := rfl @[simp] lemma map_left_obj_hom : ((map_left R l).obj X).hom = l.app X.left ≫ X.hom := rfl @[simp] lemma map_left_map_left : ((map_left R l).map f).left = f.left := rfl @[simp] lemma map_left_map_right : ((map_left R l).map f).right = f.right := rfl end def map_left_id : map_left R (𝟙 L) ≅ 𝟭 _ := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L R} @[simp] lemma map_left_id_hom_app_left : (((map_left_id L R).hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_id_hom_app_right : (((map_left_id L R).hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_left_id_inv_app_left : (((map_left_id L R).inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_id_inv_app_right : (((map_left_id L R).inv).app X).right = 𝟙 (X.right) := rfl end def map_left_comp (l : L₁ ⟶ L₂) (l' : L₂ ⟶ L₃) : (map_left R (l ≫ l')) ≅ (map_left R l') ⋙ (map_left R l) := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L₃ R} {l : L₁ ⟶ L₂} {l' : L₂ ⟶ L₃} @[simp] lemma map_left_comp_hom_app_left : (((map_left_comp R l l').hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_comp_hom_app_right : (((map_left_comp R l l').hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_left_comp_inv_app_left : (((map_left_comp R l l').inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_comp_inv_app_right : (((map_left_comp R l l').inv).app X).right = 𝟙 (X.right) := rfl end def map_right (r : R₁ ⟶ R₂) : comma L R₁ ⥤ comma L R₂ := { obj := λ X, { left := X.left, right := X.right, hom := X.hom ≫ r.app X.right }, map := λ X Y f, { left := f.left, right := f.right, w' := by tidy; rw [←r.naturality f.right, ←category.assoc]; tidy } } section variables {X Y : comma L R₁} {f : X ⟶ Y} {r : R₁ ⟶ R₂} @[simp] lemma map_right_obj_left : ((map_right L r).obj X).left = X.left := rfl @[simp] lemma map_right_obj_right : ((map_right L r).obj X).right = X.right := rfl @[simp] lemma map_right_obj_hom : ((map_right L r).obj X).hom = X.hom ≫ r.app X.right := rfl @[simp] lemma map_right_map_left : ((map_right L r).map f).left = f.left := rfl @[simp] lemma map_right_map_right : ((map_right L r).map f).right = f.right := rfl end def map_right_id : map_right L (𝟙 R) ≅ 𝟭 _ := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L R} @[simp] lemma map_right_id_hom_app_left : (((map_right_id L R).hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_id_hom_app_right : (((map_right_id L R).hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_right_id_inv_app_left : (((map_right_id L R).inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_id_inv_app_right : (((map_right_id L R).inv).app X).right = 𝟙 (X.right) := rfl end def map_right_comp (r : R₁ ⟶ R₂) (r' : R₂ ⟶ R₃) : (map_right L (r ≫ r')) ≅ (map_right L r) ⋙ (map_right L r') := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L R₁} {r : R₁ ⟶ R₂} {r' : R₂ ⟶ R₃} @[simp] lemma map_right_comp_hom_app_left : (((map_right_comp L r r').hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_comp_hom_app_right : (((map_right_comp L r r').hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_right_comp_inv_app_left : (((map_right_comp L r r').inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_comp_inv_app_right : (((map_right_comp L r r').inv).app X).right = 𝟙 (X.right) := rfl end end end comma omit 𝒜 ℬ @[derive category] def over (X : T) := comma.{v₃ 0 v₃} (𝟭 T) (functor.of.obj X) namespace over variables {X : T} @[extensionality] lemma over_morphism.ext {X : T} {U V : over X} {f g : U ⟶ V} (h : f.left = g.left) : f = g := by tidy @[simp] lemma over_right (U : over X) : U.right = punit.star := by tidy @[simp] lemma over_morphism_right {U V : over X} (f : U ⟶ V) : f.right = 𝟙 punit.star := by tidy @[simp] lemma id_left (U : over X) : comma_morphism.left (𝟙 U) = 𝟙 U.left := rfl @[simp] lemma comp_left (a b c : over X) (f : a ⟶ b) (g : b ⟶ c) : (f ≫ g).left = f.left ≫ g.left := rfl @[simp, reassoc] lemma w {A B : over X} (f : A ⟶ B) : f.left ≫ B.hom = A.hom := by have := f.w; tidy def mk {X Y : T} (f : Y ⟶ X) : over X := { left := Y, hom := f } @[simp] lemma mk_left {X Y : T} (f : Y ⟶ X) : (mk f).left = Y := rfl @[simp] lemma mk_hom {X Y : T} (f : Y ⟶ X) : (mk f).hom = f := rfl def hom_mk {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom . obviously) : U ⟶ V := { left := f } @[simp] lemma hom_mk_left {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom) : (hom_mk f).left = f := rfl def forget : (over X) ⥤ T := comma.fst _ _ @[simp] lemma forget_obj {U : over X} : forget.obj U = U.left := rfl @[simp] lemma forget_map {U V : over X} {f : U ⟶ V} : forget.map f = f.left := rfl def map {Y : T} (f : X ⟶ Y) : over X ⥤ over Y := comma.map_right _ $ functor.of.map f section variables {Y : T} {f : X ⟶ Y} {U V : over X} {g : U ⟶ V} @[simp] lemma map_obj_left : ((map f).obj U).left = U.left := rfl @[simp] lemma map_obj_hom : ((map f).obj U).hom = U.hom ≫ f := rfl @[simp] lemma map_map_left : ((map f).map g).left = g.left := rfl end section variables {D : Type u₃} [𝒟 : category.{v₃} D] include 𝒟 def post (F : T ⥤ D) : over X ⥤ over (F.obj X) := { obj := λ Y, mk $ F.map Y.hom, map := λ Y₁ Y₂ f, { left := F.map f.left, w' := by tidy; erw [← F.map_comp, w] } } end end over @[derive category] def under (X : T) := comma.{0 v₃ v₃} (functor.of.obj X) (𝟭 T) namespace under variables {X : T} @[extensionality] lemma under_morphism.ext {X : T} {U V : under X} {f g : U ⟶ V} (h : f.right = g.right) : f = g := by tidy @[simp] lemma under_left (U : under X) : U.left = punit.star := by tidy @[simp] lemma under_morphism_left {U V : under X} (f : U ⟶ V) : f.left = 𝟙 punit.star := by tidy @[simp] lemma id_right (U : under X) : comma_morphism.right (𝟙 U) = 𝟙 U.right := rfl @[simp] lemma comp_right (a b c : under X) (f : a ⟶ b) (g : b ⟶ c) : (f ≫ g).right = f.right ≫ g.right := rfl @[simp] lemma w {A B : under X} (f : A ⟶ B) : A.hom ≫ f.right = B.hom := by have := f.w; tidy def mk {X Y : T} (f : X ⟶ Y) : under X := { right := Y, hom := f } @[simp] lemma mk_right {X Y : T} (f : X ⟶ Y) : (mk f).right = Y := rfl @[simp] lemma mk_hom {X Y : T} (f : X ⟶ Y) : (mk f).hom = f := rfl def hom_mk {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom . obviously) : U ⟶ V := { right := f } @[simp] lemma hom_mk_right {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom) : (hom_mk f).right = f := rfl def forget : (under X) ⥤ T := comma.snd _ _ @[simp] lemma forget_obj {U : under X} : forget.obj U = U.right := rfl @[simp] lemma forget_map {U V : under X} {f : U ⟶ V} : forget.map f = f.right := rfl def map {Y : T} (f : X ⟶ Y) : under Y ⥤ under X := comma.map_left _ $ functor.of.map f section variables {Y : T} {f : X ⟶ Y} {U V : under Y} {g : U ⟶ V} @[simp] lemma map_obj_right : ((map f).obj U).right = U.right := rfl @[simp] lemma map_obj_hom : ((map f).obj U).hom = f ≫ U.hom := rfl @[simp] lemma map_map_right : ((map f).map g).right = g.right := rfl end section variables {D : Type u₃} [𝒟 : category.{v₃} D] include 𝒟 def post {X : T} (F : T ⥤ D) : under X ⥤ under (F.obj X) := { obj := λ Y, mk $ F.map Y.hom, map := λ Y₁ Y₂ f, { right := F.map f.right, w' := by tidy; erw [← F.map_comp, w] } } end end under end category_theory
5d1fd488d1ea2c2bb6248de23627d114ee1a96ba
88fb7558b0636ec6b181f2a548ac11ad3919f8a5
/library/init/meta/pexpr.lean
91c171ae7f6e49e1088699cfef9468b12c0020c7
[ "Apache-2.0" ]
permissive
moritayasuaki/lean
9f666c323cb6fa1f31ac597d777914aed41e3b7a
ae96ebf6ee953088c235ff7ae0e8c95066ba8001
refs/heads/master
1,611,135,440,814
1,493,852,869,000
1,493,852,869,000
90,269,903
0
0
null
1,493,906,291,000
1,493,906,291,000
null
UTF-8
Lean
false
false
1,365
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.expr universe u /- Quoted expressions. They can be converted into expressions by using a tactic. -/ meta constant pexpr : Type protected meta constant pexpr.of_expr : expr → pexpr protected meta constant pexpr.subst : pexpr → pexpr → pexpr /- Low level primitives for accessing internal representation. -/ protected meta constant pexpr.to_raw_expr : pexpr → expr protected meta constant pexpr.of_raw_expr : expr → pexpr meta constant pexpr.mk_placeholder : pexpr meta constant pexpr.pos : pexpr → option pos meta constant pexpr.mk_quote_macro : pexpr → pexpr meta constant pexpr.mk_prenum_macro : nat → pexpr meta constant pexpr.mk_string_macro : string → pexpr meta constant pexpr.mk_field_macro : pexpr → name → pexpr meta constant pexpr.mk_explicit : pexpr → pexpr meta constant pexpr.to_string : pexpr → string meta instance : has_to_string pexpr := ⟨pexpr.to_string⟩ meta class has_to_pexpr (α : Type u) := (to_pexpr : α → pexpr) meta def to_pexpr {α : Type u} [has_to_pexpr α] : α → pexpr := has_to_pexpr.to_pexpr meta instance : has_to_pexpr pexpr := ⟨id⟩ meta instance : has_to_pexpr expr := ⟨pexpr.of_expr⟩
696386fae5e54b2cc83854cd791ce29dcd5ba355
737dc4b96c97368cb66b925eeea3ab633ec3d702
/tests/lean/matchOfNatIssue.lean
e5814d7a69171b06ac3f92ad7d1605c4da608d20
[ "Apache-2.0" ]
permissive
Bioye97/lean4
1ace34638efd9913dc5991443777b01a08983289
bc3900cbb9adda83eed7e6affeaade7cfd07716d
refs/heads/master
1,690,589,820,211
1,631,051,000,000
1,631,067,598,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
490
lean
example (x : Int) (h : x = 2) : Int.div 2 1 = x := by simp [Int.div] traceState simp [h] example (n : Nat) : Int.div (Int.ofNat n) (Int.ofNat 0) = Int.ofNat (n / 0) := by simp [Int.div] example (n : Nat) : Int.div (Int.ofNat n) 0 = Int.ofNat (n / 0) := by simp [Int.div] example (n : Nat) : Int.mul (Int.ofNat n) (Int.ofNat 0) = Int.ofNat (n * 0) := by simp [Int.mul] example (n : Nat) : Int.mul (Int.ofNat n) 0 = Int.ofNat (n * 0) := by simp [Int.mul] traceState rfl
d85faf04a4298d5465790ccfa063c1d7538720f0
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/topology/continuous_function/basic.lean
62656ae578074e392452a2a53a7ce8a89b73397e
[ "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
10,682
lean
/- Copyright © 2020 Nicolò Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nicolò Cavalleri -/ import data.set.Union_lift import topology.subset_properties import topology.tactic import topology.algebra.ordered.proj_Icc /-! # Continuous bundled map In this file we define the type `continuous_map` of continuous bundled maps. -/ /-- Bundled continuous maps. -/ @[protect_proj] structure continuous_map (α : Type*) (β : Type*) [topological_space α] [topological_space β] := (to_fun : α → β) (continuous_to_fun : continuous to_fun . tactic.interactive.continuity') notation `C(` α `, ` β `)` := continuous_map α β namespace continuous_map attribute [continuity] continuous_map.continuous_to_fun variables {α : Type*} {β : Type*} {γ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] instance : has_coe_to_fun (C(α, β)) := ⟨_, continuous_map.to_fun⟩ @[simp] lemma to_fun_eq_coe {f : C(α, β)} : f.to_fun = (f : α → β) := rfl variables {α β} {f g : continuous_map α β} @[continuity] protected lemma continuous (f : C(α, β)) : continuous f := f.continuous_to_fun @[continuity] lemma continuous_set_coe (s : set C(α, β)) (f : s) : continuous f := by { cases f, dsimp, continuity, } protected lemma continuous_at (f : C(α, β)) (x : α) : continuous_at f x := f.continuous.continuous_at protected lemma continuous_within_at (f : C(α, β)) (s : set α) (x : α) : continuous_within_at f s x := f.continuous.continuous_within_at protected lemma congr_fun {f g : C(α, β)} (H : f = g) (x : α) : f x = g x := H ▸ rfl protected lemma congr_arg (f : C(α, β)) {x y : α} (h : x = y) : f x = f y := h ▸ rfl @[ext] theorem ext (H : ∀ x, f x = g x) : f = g := by cases f; cases g; congr'; exact funext H lemma ext_iff : f = g ↔ ∀ x, f x = g x := ⟨continuous_map.congr_fun, ext⟩ instance [inhabited β] : inhabited C(α, β) := ⟨{ to_fun := λ _, default _, }⟩ lemma coe_inj ⦃f g : C(α, β)⦄ (h : (f : α → β) = g) : f = g := by cases f; cases g; cases h; refl @[simp] lemma coe_mk (f : α → β) (h : continuous f) : ⇑(⟨f, h⟩ : continuous_map α β) = f := rfl section variables (α β) /-- The continuous functions from `α` to `β` are the same as the plain functions when `α` is discrete. -/ @[simps] def equiv_fn_of_discrete [discrete_topology α] : C(α, β) ≃ (α → β) := ⟨(λ f, f), (λ f, ⟨f, continuous_of_discrete_topology⟩), λ f, by { ext, refl, }, λ f, by { ext, refl, }⟩ end /-- The identity as a continuous map. -/ def id : C(α, α) := ⟨id⟩ @[simp] lemma id_coe : (id : α → α) = _root_.id := rfl lemma id_apply (a : α) : id a = a := rfl /-- The composition of continuous maps, as a continuous map. -/ def comp (f : C(β, γ)) (g : C(α, β)) : C(α, γ) := ⟨f ∘ g⟩ @[simp] lemma comp_coe (f : C(β, γ)) (g : C(α, β)) : (comp f g : α → γ) = f ∘ g := rfl lemma comp_apply (f : C(β, γ)) (g : C(α, β)) (a : α) : comp f g a = f (g a) := rfl /-- Constant map as a continuous map -/ def const (b : β) : C(α, β) := ⟨λ x, b⟩ @[simp] lemma const_coe (b : β) : (const b : α → β) = (λ x, b) := rfl lemma const_apply (b : β) (a : α) : const b a = b := rfl instance [nonempty α] [nontrivial β] : nontrivial C(α, β) := { exists_pair_ne := begin obtain ⟨b₁, b₂, hb⟩ := exists_pair_ne β, refine ⟨const b₁, const b₂, _⟩, contrapose! hb, inhabit α, change const b₁ (default α) = const b₂ (default α), simp [hb] end } section variables [linear_ordered_add_comm_group β] [order_topology β] /-- The pointwise absolute value of a continuous function as a continuous function. -/ def abs (f : C(α, β)) : C(α, β) := { to_fun := λ x, |f x|, } @[priority 100] -- see Note [lower instance priority] instance : has_abs C(α, β) := ⟨λf, abs f⟩ @[simp] lemma abs_apply (f : C(α, β)) (x : α) : |f| x = |f x| := rfl end /-! We now set up the partial order and lattice structure (given by pointwise min and max) on continuous functions. -/ section lattice instance partial_order [partial_order β] : partial_order C(α, β) := partial_order.lift (λ f, f.to_fun) (by tidy) lemma le_def [partial_order β] {f g : C(α, β)} : f ≤ g ↔ ∀ a, f a ≤ g a := pi.le_def lemma lt_def [partial_order β] {f g : C(α, β)} : f < g ↔ (∀ a, f a ≤ g a) ∧ (∃ a, f a < g a) := pi.lt_def instance has_sup [linear_order β] [order_closed_topology β] : has_sup C(α, β) := { sup := λ f g, { to_fun := λ a, max (f a) (g a), } } @[simp, norm_cast] lemma sup_coe [linear_order β] [order_closed_topology β] (f g : C(α, β)) : ((f ⊔ g : C(α, β)) : α → β) = (f ⊔ g : α → β) := rfl @[simp] lemma sup_apply [linear_order β] [order_closed_topology β] (f g : C(α, β)) (a : α) : (f ⊔ g) a = max (f a) (g a) := rfl instance [linear_order β] [order_closed_topology β] : semilattice_sup C(α, β) := { le_sup_left := λ f g, le_def.mpr (by simp [le_refl]), le_sup_right := λ f g, le_def.mpr (by simp [le_refl]), sup_le := λ f₁ f₂ g w₁ w₂, le_def.mpr (λ a, by simp [le_def.mp w₁ a, le_def.mp w₂ a]), ..continuous_map.partial_order, ..continuous_map.has_sup, } instance has_inf [linear_order β] [order_closed_topology β] : has_inf C(α, β) := { inf := λ f g, { to_fun := λ a, min (f a) (g a), } } @[simp, norm_cast] lemma inf_coe [linear_order β] [order_closed_topology β] (f g : C(α, β)) : ((f ⊓ g : C(α, β)) : α → β) = (f ⊓ g : α → β) := rfl @[simp] lemma inf_apply [linear_order β] [order_closed_topology β] (f g : C(α, β)) (a : α) : (f ⊓ g) a = min (f a) (g a) := rfl instance [linear_order β] [order_closed_topology β] : semilattice_inf C(α, β) := { inf_le_left := λ f g, le_def.mpr (by simp [le_refl]), inf_le_right := λ f g, le_def.mpr (by simp [le_refl]), le_inf := λ f₁ f₂ g w₁ w₂, le_def.mpr (λ a, by simp [le_def.mp w₁ a, le_def.mp w₂ a]), ..continuous_map.partial_order, ..continuous_map.has_inf, } instance [linear_order β] [order_closed_topology β] : lattice C(α, β) := { ..continuous_map.semilattice_inf, ..continuous_map.semilattice_sup } -- TODO transfer this lattice structure to `bounded_continuous_function` section sup' variables [linear_order γ] [order_closed_topology γ] lemma sup'_apply {ι : Type*} {s : finset ι} (H : s.nonempty) (f : ι → C(β, γ)) (b : β) : s.sup' H f b = s.sup' H (λ a, f a b) := finset.comp_sup'_eq_sup'_comp H (λ f : C(β, γ), f b) (λ i j, rfl) @[simp, norm_cast] lemma sup'_coe {ι : Type*} {s : finset ι} (H : s.nonempty) (f : ι → C(β, γ)) : ((s.sup' H f : C(β, γ)) : ι → β) = s.sup' H (λ a, (f a : β → γ)) := by { ext, simp [sup'_apply], } end sup' section inf' variables [linear_order γ] [order_closed_topology γ] lemma inf'_apply {ι : Type*} {s : finset ι} (H : s.nonempty) (f : ι → C(β, γ)) (b : β) : s.inf' H f b = s.inf' H (λ a, f a b) := @sup'_apply _ (order_dual γ) _ _ _ _ _ _ H f b @[simp, norm_cast] lemma inf'_coe {ι : Type*} {s : finset ι} (H : s.nonempty) (f : ι → C(β, γ)) : ((s.inf' H f : C(β, γ)) : ι → β) = s.inf' H (λ a, (f a : β → γ)) := @sup'_coe _ (order_dual γ) _ _ _ _ _ _ H f end inf' end lattice section restrict variables (s : set α) /-- The restriction of a continuous function `α → β` to a subset `s` of `α`. -/ def restrict (f : C(α, β)) : C(s, β) := ⟨f ∘ coe⟩ @[simp] lemma coe_restrict (f : C(α, β)) : ⇑(f.restrict s) = f ∘ coe := rfl end restrict section extend variables [linear_order α] [order_topology α] {a b : α} (h : a ≤ b) /-- Extend a continuous function `f : C(set.Icc a b, β)` to a function `f : C(α, β)`. -/ def Icc_extend (f : C(set.Icc a b, β)) : C(α, β) := ⟨set.Icc_extend h f⟩ @[simp] lemma coe_Icc_extend (f : C(set.Icc a b, β)) : ((Icc_extend h f : C(α, β)) : α → β) = set.Icc_extend h f := rfl end extend section gluing variables {ι : Type*} (S : ι → set α) (φ : Π i : ι, C(S i, β)) (hφ : ∀ i j (x : α) (hxi : x ∈ S i) (hxj : x ∈ S j), φ i ⟨x, hxi⟩ = φ j ⟨x, hxj⟩) (hS : ∀ x : α, ∃ i, S i ∈ nhds x) include hφ hS /-- A family `φ i` of continuous maps `C(S i, β)`, where the domains `S i` contain a neighbourhood of each point in `α` and the functions `φ i` agree pairwise on intersections, can be glued to construct a continuous map in `C(α, β)`. -/ noncomputable def lift_cover : C(α, β) := begin have H : (⋃ i, S i) = set.univ, { rw set.eq_univ_iff_forall, intros x, rw set.mem_Union, obtain ⟨i, hi⟩ := hS x, exact ⟨i, mem_of_mem_nhds hi⟩ }, refine ⟨set.lift_cover S (λ i, φ i) hφ H, continuous_subtype_nhds_cover hS _⟩, intros i, convert (φ i).continuous, ext x, exact set.lift_cover_coe x, end variables {S φ hφ hS} @[simp] lemma lift_cover_coe {i : ι} (x : S i) : lift_cover S φ hφ hS x = φ i x := set.lift_cover_coe _ @[simp] lemma lift_cover_restrict {i : ι} : (lift_cover S φ hφ hS).restrict (S i) = φ i := ext $ lift_cover_coe omit hφ hS variables (A : set (set α)) (F : Π (s : set α) (hi : s ∈ A), C(s, β)) (hF : ∀ s (hs : s ∈ A) t (ht : t ∈ A) (x : α) (hxi : x ∈ s) (hxj : x ∈ t), F s hs ⟨x, hxi⟩ = F t ht ⟨x, hxj⟩) (hA : ∀ x : α, ∃ i ∈ A, i ∈ nhds x) include hF hA /-- A family `F s` of continuous maps `C(s, β)`, where (1) the domains `s` are taken from a set `A` of sets in `α` which contain a neighbourhood of each point in `α` and (2) the functions `F s` agree pairwise on intersections, can be glued to construct a continuous map in `C(α, β)`. -/ noncomputable def lift_cover' : C(α, β) := begin let S : A → set α := coe, let F : Π i : A, C(i, β) := λ i, F i i.prop, refine lift_cover S F (λ i j, hF i i.prop j j.prop) _, intros x, obtain ⟨s, hs, hsx⟩ := hA x, exact ⟨⟨s, hs⟩, hsx⟩ end variables {A F hF hA} @[simp] lemma lift_cover_coe' {s : set α} {hs : s ∈ A} (x : s) : lift_cover' A F hF hA x = F s hs x := let x' : (coe : A → set α) ⟨s, hs⟩ := x in lift_cover_coe x' @[simp] lemma lift_cover_restrict' {s : set α} {hs : s ∈ A} : (lift_cover' A F hF hA).restrict s = F s hs := ext $ lift_cover_coe' end gluing end continuous_map /-- The forward direction of a homeomorphism, as a bundled continuous map. -/ @[simps] def homeomorph.to_continuous_map {α β : Type*} [topological_space α] [topological_space β] (e : α ≃ₜ β) : C(α, β) := ⟨e⟩
11eb65774fa8bdc159fc1c5292300ecc5bd2b8f7
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/order/antichain.lean
0e62ef2616deb336acd4a333a8c55799ae0db51e
[ "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
12,939
lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import data.set.pairwise.basic import order.bounds.basic import order.directed import order.hom.set /-! # Antichains > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines antichains. An antichain is a set where any two distinct elements are not related. If the relation is `(≤)`, this corresponds to incomparability and usual order antichains. If the relation is `G.adj` for `G : simple_graph α`, this corresponds to independent sets of `G`. ## Definitions * `is_antichain r s`: Any two elements of `s : set α` are unrelated by `r : α → α → Prop`. * `is_strong_antichain r s`: Any two elements of `s : set α` are not related by `r : α → α → Prop` to a common element. * `is_antichain.mk r s`: Turns `s` into an antichain by keeping only the "maximal" elements. -/ open function set section general variables {α β : Type*} {r r₁ r₂ : α → α → Prop} {r' : β → β → Prop} {s t : set α} {a b : α} protected lemma symmetric.compl (h : symmetric r) : symmetric rᶜ := λ x y hr hr', hr $ h hr' /-- An antichain is a set such that no two distinct elements are related. -/ def is_antichain (r : α → α → Prop) (s : set α) : Prop := s.pairwise rᶜ namespace is_antichain protected lemma subset (hs : is_antichain r s) (h : t ⊆ s) : is_antichain r t := hs.mono h lemma mono (hs : is_antichain r₁ s) (h : r₂ ≤ r₁) : is_antichain r₂ s := hs.mono' $ compl_le_compl h lemma mono_on (hs : is_antichain r₁ s) (h : s.pairwise (λ ⦃a b⦄, r₂ a b → r₁ a b)) : is_antichain r₂ s := hs.imp_on $ h.imp $ λ a b h h₁ h₂, h₁ $ h h₂ protected lemma eq (hs : is_antichain r s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) (h : r a b) : a = b := hs.eq ha hb $ not_not_intro h protected lemma eq' (hs : is_antichain r s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) (h : r b a) : a = b := (hs.eq hb ha h).symm protected lemma is_antisymm (h : is_antichain r univ) : is_antisymm α r := ⟨λ a b ha _, h.eq trivial trivial ha⟩ protected lemma subsingleton [is_trichotomous α r] (h : is_antichain r s) : s.subsingleton := begin rintro a ha b hb, obtain hab | hab | hab := trichotomous_of r a b, { exact h.eq ha hb hab }, { exact hab }, { exact h.eq' ha hb hab } end protected lemma flip (hs : is_antichain r s) : is_antichain (flip r) s := λ a ha b hb h, hs hb ha h.symm lemma swap (hs : is_antichain r s) : is_antichain (swap r) s := hs.flip lemma image (hs : is_antichain r s) (f : α → β) (h : ∀ ⦃a b⦄, r' (f a) (f b) → r a b) : is_antichain r' (f '' s) := begin rintro _ ⟨b, hb, rfl⟩ _ ⟨c, hc, rfl⟩ hbc hr, exact hs hb hc (ne_of_apply_ne _ hbc) (h hr), end lemma preimage (hs : is_antichain r s) {f : β → α} (hf : injective f) (h : ∀ ⦃a b⦄, r' a b → r (f a) (f b)) : is_antichain r' (f ⁻¹' s) := λ b hb c hc hbc hr, hs hb hc (hf.ne hbc) $ h hr lemma _root_.is_antichain_insert : is_antichain r (insert a s) ↔ is_antichain r s ∧ ∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ r a b ∧ ¬ r b a := set.pairwise_insert protected lemma insert (hs : is_antichain r s) (hl : ∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ r b a) (hr : ∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ r a b) : is_antichain r (insert a s) := is_antichain_insert.2 ⟨hs, λ b hb hab, ⟨hr hb hab, hl hb hab⟩⟩ lemma _root_.is_antichain_insert_of_symmetric (hr : symmetric r) : is_antichain r (insert a s) ↔ is_antichain r s ∧ ∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ r a b := pairwise_insert_of_symmetric hr.compl lemma insert_of_symmetric (hs : is_antichain r s) (hr : symmetric r) (h : ∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ r a b) : is_antichain r (insert a s) := (is_antichain_insert_of_symmetric hr).2 ⟨hs, h⟩ lemma image_rel_embedding (hs : is_antichain r s) (φ : r ↪r r') : is_antichain r' (φ '' s) := begin intros b hb b' hb' h₁ h₂, rw set.mem_image at hb hb', obtain ⟨⟨a,has,rfl⟩,⟨a',has',rfl⟩⟩ := ⟨hb,hb'⟩, exact hs has has' (λ haa', h₁ (haa'.subst (by refl))) (φ.map_rel_iff.mp h₂), end lemma preimage_rel_embedding {t : set β} (ht : is_antichain r' t) (φ : r ↪r r') : is_antichain r (φ ⁻¹' t) := λ a ha a' ha' hne hle, ht ha ha' (λ h, hne (φ.injective h)) (φ.map_rel_iff.mpr hle) lemma image_rel_iso (hs : is_antichain r s) (φ : r ≃r r') : is_antichain r' (φ '' s) := hs.image_rel_embedding φ lemma preimage_rel_iso {t : set β} (hs : is_antichain r' t) (φ : r ≃r r') : is_antichain r (φ ⁻¹' t) := hs.preimage_rel_embedding φ lemma image_rel_embedding_iff {φ : r ↪r r'} : is_antichain r' (φ '' s) ↔ is_antichain r s := ⟨λ h, (φ.injective.preimage_image s).subst (h.preimage_rel_embedding φ), λ h, h.image_rel_embedding φ⟩ lemma image_rel_iso_iff {φ : r ≃r r'} : is_antichain r' (φ '' s) ↔ is_antichain r s := @image_rel_embedding_iff _ _ _ _ _ (φ : r ↪r r') lemma image_embedding [has_le α] [has_le β] (hs : is_antichain (≤) s) (φ : α ↪o β) : is_antichain (≤) (φ '' s) := image_rel_embedding hs _ lemma preimage_embedding [has_le α] [has_le β] {t : set β} (ht : is_antichain (≤) t) (φ : α ↪o β) : is_antichain (≤) (φ ⁻¹' t) := preimage_rel_embedding ht _ lemma image_embedding_iff [has_le α] [has_le β] {φ : α ↪o β} : is_antichain (≤) (φ '' s) ↔ is_antichain (≤) s := image_rel_embedding_iff lemma image_iso [has_le α] [has_le β] (hs : is_antichain (≤) s) (φ : α ≃o β) : is_antichain (≤) (φ '' s) := image_rel_embedding hs _ lemma image_iso_iff [has_le α] [has_le β] {φ : α ≃o β} : is_antichain (≤) (φ '' s) ↔ is_antichain (≤) s := image_rel_embedding_iff lemma preimage_iso [has_le α] [has_le β] {t : set β} (ht : is_antichain (≤) t) (φ : α ≃o β) : is_antichain (≤) (φ ⁻¹' t) := preimage_rel_embedding ht _ lemma preimage_iso_iff [has_le α] [has_le β] {t : set β} {φ : α ≃o β} : is_antichain (≤) (φ ⁻¹' t) ↔ is_antichain (≤) t := ⟨λ h, (φ.image_preimage t).subst (h.image_iso φ), λ h, h.preimage_iso _⟩ lemma to_dual [has_le α] (hs : is_antichain (≤) s) : @is_antichain αᵒᵈ (≤) s := λ a ha b hb hab, hs hb ha hab.symm lemma to_dual_iff [has_le α] : is_antichain (≤) s ↔ @is_antichain αᵒᵈ (≤) s := ⟨to_dual, to_dual⟩ lemma image_compl [boolean_algebra α] (hs : is_antichain (≤) s) : is_antichain (≤) (compl '' s) := (hs.image_embedding (order_iso.compl α).to_order_embedding).flip lemma preimage_compl [boolean_algebra α] (hs : is_antichain (≤) s) : is_antichain (≤) (compl ⁻¹' s) := λ a ha a' ha' hne hle, hs ha' ha (λ h, hne (compl_inj_iff.mp h.symm)) (compl_le_compl hle) end is_antichain lemma is_antichain_singleton (a : α) (r : α → α → Prop) : is_antichain r {a} := pairwise_singleton _ _ lemma set.subsingleton.is_antichain (hs : s.subsingleton) (r : α → α → Prop) : is_antichain r s := hs.pairwise _ section preorder variables [preorder α] lemma is_antichain.not_lt (hs : is_antichain (≤) s) (ha : a ∈ s) (hb : b ∈ s) : ¬ a < b := λ h, hs ha hb h.ne h.le lemma is_antichain_and_least_iff : is_antichain (≤) s ∧ is_least s a ↔ s = {a} := ⟨λ h, eq_singleton_iff_unique_mem.2 ⟨h.2.1, λ b hb, h.1.eq' hb h.2.1 (h.2.2 hb)⟩, by { rintro rfl, exact ⟨is_antichain_singleton _ _, is_least_singleton⟩ }⟩ lemma is_antichain_and_greatest_iff : is_antichain (≤) s ∧ is_greatest s a ↔ s = {a} := ⟨λ h, eq_singleton_iff_unique_mem.2 ⟨h.2.1, λ b hb, h.1.eq hb h.2.1 (h.2.2 hb)⟩, by { rintro rfl, exact ⟨is_antichain_singleton _ _, is_greatest_singleton⟩ }⟩ lemma is_antichain.least_iff (hs : is_antichain (≤) s) : is_least s a ↔ s = {a} := (and_iff_right hs).symm.trans is_antichain_and_least_iff lemma is_antichain.greatest_iff (hs : is_antichain (≤) s) : is_greatest s a ↔ s = {a} := (and_iff_right hs).symm.trans is_antichain_and_greatest_iff lemma is_least.antichain_iff (hs : is_least s a) : is_antichain (≤) s ↔ s = {a} := (and_iff_left hs).symm.trans is_antichain_and_least_iff lemma is_greatest.antichain_iff (hs : is_greatest s a) : is_antichain (≤) s ↔ s = {a} := (and_iff_left hs).symm.trans is_antichain_and_greatest_iff lemma is_antichain.bot_mem_iff [order_bot α] (hs : is_antichain (≤) s) : ⊥ ∈ s ↔ s = {⊥} := is_least_bot_iff.symm.trans hs.least_iff lemma is_antichain.top_mem_iff [order_top α] (hs : is_antichain (≤) s) : ⊤ ∈ s ↔ s = {⊤} := is_greatest_top_iff.symm.trans hs.greatest_iff end preorder section partial_order variables [partial_order α] lemma is_antichain_iff_forall_not_lt : is_antichain (≤) s ↔ ∀ ⦃a⦄, a ∈ s → ∀ ⦃b⦄, b ∈ s → ¬ a < b := ⟨λ hs a ha b, hs.not_lt ha, λ hs a ha b hb h h', hs ha hb $ h'.lt_of_ne h⟩ end partial_order /-! ### Strong antichains -/ /-- A strong (upward) antichain is a set such that no two distinct elements are related to a common element. -/ def is_strong_antichain (r : α → α → Prop) (s : set α) : Prop := s.pairwise $ λ a b, ∀ c, ¬ r a c ∨ ¬ r b c namespace is_strong_antichain protected lemma subset (hs : is_strong_antichain r s) (h : t ⊆ s) : is_strong_antichain r t := hs.mono h lemma mono (hs : is_strong_antichain r₁ s) (h : r₂ ≤ r₁) : is_strong_antichain r₂ s := hs.mono' $ λ a b hab c, (hab c).imp (compl_le_compl h _ _) (compl_le_compl h _ _) lemma eq (hs : is_strong_antichain r s) {a b c : α} (ha : a ∈ s) (hb : b ∈ s) (hac : r a c) (hbc : r b c) : a = b := hs.eq ha hb $ λ h, false.elim $ (h c).elim (not_not_intro hac) (not_not_intro hbc) protected lemma is_antichain [is_refl α r] (h : is_strong_antichain r s) : is_antichain r s := h.imp $ λ a b hab, (hab b).resolve_right (not_not_intro $ refl _) protected lemma subsingleton [is_directed α r] (h : is_strong_antichain r s) : s.subsingleton := λ a ha b hb, let ⟨c, hac, hbc⟩ := directed_of r a b in h.eq ha hb hac hbc protected lemma flip [is_symm α r] (hs : is_strong_antichain r s) : is_strong_antichain (flip r) s := λ a ha b hb h c, (hs ha hb h c).imp (mt $ symm_of r) (mt $ symm_of r) lemma swap [is_symm α r] (hs : is_strong_antichain r s) : is_strong_antichain (swap r) s := hs.flip lemma image (hs : is_strong_antichain r s) {f : α → β} (hf : surjective f) (h : ∀ a b, r' (f a) (f b) → r a b) : is_strong_antichain r' (f '' s) := begin rintro _ ⟨a, ha, rfl⟩ _ ⟨b, hb, rfl⟩ hab c, obtain ⟨c, rfl⟩ := hf c, exact (hs ha hb (ne_of_apply_ne _ hab) _).imp (mt $ h _ _) (mt $ h _ _), end lemma preimage (hs : is_strong_antichain r s) {f : β → α} (hf : injective f) (h : ∀ a b, r' a b → r (f a) (f b)) : is_strong_antichain r' (f ⁻¹' s) := λ a ha b hb hab c, (hs ha hb (hf.ne hab) _).imp (mt $ h _ _) (mt $ h _ _) lemma _root_.is_strong_antichain_insert : is_strong_antichain r (insert a s) ↔ is_strong_antichain r s ∧ ∀ ⦃b⦄, b ∈ s → a ≠ b → ∀ c, ¬ r a c ∨ ¬ r b c := set.pairwise_insert_of_symmetric $ λ a b h c, (h c).symm protected lemma insert (hs : is_strong_antichain r s) (h : ∀ ⦃b⦄, b ∈ s → a ≠ b → ∀ c, ¬ r a c ∨ ¬ r b c) : is_strong_antichain r (insert a s) := is_strong_antichain_insert.2 ⟨hs, h⟩ end is_strong_antichain lemma set.subsingleton.is_strong_antichain (hs : s.subsingleton) (r : α → α → Prop) : is_strong_antichain r s := hs.pairwise _ end general /-! ### Weak antichains -/ section pi variables {ι : Type*} {α : ι → Type*} [Π i, preorder (α i)] {s t : set (Π i, α i)} {a b c : Π i, α i} local infix ` ≺ `:50 := strong_lt /-- A weak antichain in `Π i, α i` is a set such that no two distinct elements are strongly less than each other. -/ def is_weak_antichain (s : set (Π i, α i)) : Prop := is_antichain (≺) s namespace is_weak_antichain protected lemma subset (hs : is_weak_antichain s) : t ⊆ s → is_weak_antichain t := hs.subset protected lemma eq (hs : is_weak_antichain s) : a ∈ s → b ∈ s → a ≺ b → a = b := hs.eq protected lemma insert (hs : is_weak_antichain s) : (∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ b ≺ a) → (∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ a ≺ b) → is_weak_antichain (insert a s) := hs.insert end is_weak_antichain lemma is_weak_antichain_insert : is_weak_antichain (insert a s) ↔ is_weak_antichain s ∧ ∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ a ≺ b ∧ ¬ b ≺ a := is_antichain_insert protected lemma is_antichain.is_weak_antichain (hs : is_antichain (≤) s) : is_weak_antichain s := hs.mono $ λ a b, le_of_strong_lt lemma set.subsingleton.is_weak_antichain (hs : s.subsingleton) : is_weak_antichain s := hs.is_antichain _ end pi
4ba60e5a139646382ef442d289e419956fea4302
aa5a655c05e5359a70646b7154e7cac59f0b4132
/stage0/src/Lean/Meta/ExprDefEq.lean
adf674039737811a1d765f3f827838e3bb6d94c6
[ "Apache-2.0" ]
permissive
lambdaxymox/lean4
ae943c960a42247e06eff25c35338268d07454cb
278d47c77270664ef29715faab467feac8a0f446
refs/heads/master
1,677,891,867,340
1,612,500,005,000
1,612,500,005,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
55,013
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.ProjFns import Lean.Meta.WHNF import Lean.Meta.InferType import Lean.Meta.FunInfo import Lean.Meta.LevelDefEq import Lean.Meta.Check import Lean.Meta.Offset import Lean.Meta.ForEachExpr import Lean.Meta.UnificationHint namespace Lean.Meta /-- Try to solve `a := (fun x => t) =?= b` by eta-expanding `b`. Remark: eta-reduction is not a good alternative even in a system without universe cumulativity like Lean. Example: ``` (fun x : A => f ?m) =?= f ``` The left-hand side of the constraint above it not eta-reduced because `?m` is a metavariable. -/ private def isDefEqEta (a b : Expr) : MetaM Bool := do if a.isLambda && !b.isLambda then let bType ← inferType b let bType ← whnfD bType match bType with | Expr.forallE n d _ c => let b' := mkLambda n c.binderInfo d (mkApp b (mkBVar 0)) commitWhen <| Meta.isExprDefEqAux a b' | _ => pure false else pure false /-- Support for `Lean.reduceBool` and `Lean.reduceNat` -/ def isDefEqNative (s t : Expr) : MetaM LBool := do let isDefEq (s t) : MetaM LBool := toLBoolM <| Meta.isExprDefEqAux s t let s? ← reduceNative? s let t? ← reduceNative? t match s?, t? with | some s, some t => isDefEq s t | some s, none => isDefEq s t | none, some t => isDefEq s t | none, none => pure LBool.undef /-- Support for reducing Nat basic operations. -/ def isDefEqNat (s t : Expr) : MetaM LBool := do let isDefEq (s t) : MetaM LBool := toLBoolM <| Meta.isExprDefEqAux s t if s.hasFVar || s.hasMVar || t.hasFVar || t.hasMVar then pure LBool.undef else let s? ← reduceNat? s let t? ← reduceNat? t match s?, t? with | some s, some t => isDefEq s t | some s, none => isDefEq s t | none, some t => isDefEq s t | none, none => pure LBool.undef /-- Support for constraints of the form `("..." =?= String.mk cs)` -/ def isDefEqStringLit (s t : Expr) : MetaM LBool := do let isDefEq (s t) : MetaM LBool := toLBoolM <| Meta.isExprDefEqAux s t if s.isStringLit && t.isAppOf `String.mk then isDefEq (toCtorIfLit s) t else if s.isAppOf `String.mk && t.isStringLit then isDefEq s (toCtorIfLit t) else pure LBool.undef /-- Return `true` if `e` is of the form `fun (x_1 ... x_n) => ?m x_1 ... x_n)`, and `?m` is unassigned. Remark: `n` may be 0. -/ def isEtaUnassignedMVar (e : Expr) : MetaM Bool := do match e.etaExpanded? with | some (Expr.mvar mvarId _) => if (← isReadOnlyOrSyntheticOpaqueExprMVar mvarId) then pure false else if (← isExprMVarAssigned mvarId) then pure false else pure true | _ => pure false /- First pass for `isDefEqArgs`. We unify explicit arguments, *and* easy cases Here, we say a case is easy if it is of the form ?m =?= t or t =?= ?m where `?m` is unassigned. These easy cases are not just an optimization. When `?m` is a function, by assigning it to t, we make sure a unification constraint (in the explicit part) ``` ?m t =?= f s ``` is not higher-order. We also handle the eta-expanded cases: ``` fun x₁ ... xₙ => ?m x₁ ... xₙ =?= t t =?= fun x₁ ... xₙ => ?m x₁ ... xₙ This is important because type inference often produces eta-expanded terms, and without this extra case, we could introduce counter intuitive behavior. Pre: `paramInfo.size <= args₁.size = args₂.size` -/ private partial def isDefEqArgsFirstPass (paramInfo : Array ParamInfo) (args₁ args₂ : Array Expr) : MetaM (Option (Array Nat)) := do let rec loop (i : Nat) (postponed : Array Nat) := do if h : i < paramInfo.size then let info := paramInfo.get ⟨i, h⟩ let a₁ := args₁[i] let a₂ := args₂[i] if info.implicit || info.instImplicit then if (← isEtaUnassignedMVar a₁ <||> isEtaUnassignedMVar a₂) then if (← Meta.isExprDefEqAux a₁ a₂) then loop (i+1) postponed else pure none else loop (i+1) (postponed.push i) else if (← Meta.isExprDefEqAux a₁ a₂) then loop (i+1) postponed else pure none else pure (some postponed) loop 0 #[] @[specialize] private def trySynthPending (e : Expr) : MetaM Bool := do let mvarId? ← getStuckMVar? e match mvarId? with | some mvarId => Meta.synthPending mvarId | none => pure false private partial def isDefEqArgs (f : Expr) (args₁ args₂ : Array Expr) : MetaM Bool := if h : args₁.size = args₂.size then do let finfo ← getFunInfoNArgs f args₁.size let (some postponed) ← isDefEqArgsFirstPass finfo.paramInfo args₁ args₂ | pure false let rec processOtherArgs (i : Nat) : MetaM Bool := do if h₁ : i < args₁.size then let a₁ := args₁.get ⟨i, h₁⟩ let a₂ := args₂.get ⟨i, Eq.subst h h₁⟩ if (← Meta.isExprDefEqAux a₁ a₂) then processOtherArgs (i+1) else pure false else pure true if (← processOtherArgs finfo.paramInfo.size) then postponed.allM fun i => do /- Second pass: unify implicit arguments. In the second pass, we make sure we are unfolding at least non reducible definitions (default setting). -/ let a₁ := args₁[i] let a₂ := args₂[i] let info := finfo.paramInfo[i] if info.instImplicit then discard <| trySynthPending a₁ discard <| trySynthPending a₂ withAtLeastTransparency TransparencyMode.default <| Meta.isExprDefEqAux a₁ a₂ else pure false else pure false /-- Check whether the types of the free variables at `fvars` are definitionally equal to the types at `ds₂`. Pre: `fvars.size == ds₂.size` This method also updates the set of local instances, and invokes the continuation `k` with the updated set. We can't use `withNewLocalInstances` because the `isDeq fvarType d₂` may use local instances. -/ @[specialize] partial def isDefEqBindingDomain (fvars : Array Expr) (ds₂ : Array Expr) (k : MetaM Bool) : MetaM Bool := let rec loop (i : Nat) := do if h : i < fvars.size then do let fvar := fvars.get ⟨i, h⟩ let fvarDecl ← getFVarLocalDecl fvar let fvarType := fvarDecl.type let d₂ := ds₂[i] if (← Meta.isExprDefEqAux fvarType d₂) then match (← isClass? fvarType) with | some className => withNewLocalInstance className fvar <| loop (i+1) | none => loop (i+1) else pure false else k loop 0 /- Auxiliary function for `isDefEqBinding` for handling binders `forall/fun`. It accumulates the new free variables in `fvars`, and declare them at `lctx`. We use the domain types of `e₁` to create the new free variables. We store the domain types of `e₂` at `ds₂`. -/ private partial def isDefEqBindingAux (lctx : LocalContext) (fvars : Array Expr) (e₁ e₂ : Expr) (ds₂ : Array Expr) : MetaM Bool := let process (n : Name) (d₁ d₂ b₁ b₂ : Expr) : MetaM Bool := do let d₁ := d₁.instantiateRev fvars let d₂ := d₂.instantiateRev fvars let fvarId ← mkFreshId let lctx := lctx.mkLocalDecl fvarId n d₁ let fvars := fvars.push (mkFVar fvarId) isDefEqBindingAux lctx fvars b₁ b₂ (ds₂.push d₂) match e₁, e₂ with | Expr.forallE n d₁ b₁ _, Expr.forallE _ d₂ b₂ _ => process n d₁ d₂ b₁ b₂ | Expr.lam n d₁ b₁ _, Expr.lam _ d₂ b₂ _ => process n d₁ d₂ b₁ b₂ | _, _ => withReader (fun ctx => { ctx with lctx := lctx }) do isDefEqBindingDomain fvars ds₂ do Meta.isExprDefEqAux (e₁.instantiateRev fvars) (e₂.instantiateRev fvars) @[inline] private def isDefEqBinding (a b : Expr) : MetaM Bool := do let lctx ← getLCtx isDefEqBindingAux lctx #[] a b #[] private def checkTypesAndAssign (mvar : Expr) (v : Expr) : MetaM Bool := traceCtx `Meta.isDefEq.assign.checkTypes do if !mvar.isMVar then trace[Meta.isDefEq.assign.final]! "metavariable expected at {mvar} := {v}" return false else -- must check whether types are definitionally equal or not, before assigning and returning true let mvarType ← inferType mvar let vType ← inferType v if (← withTransparency TransparencyMode.default <| Meta.isExprDefEqAux mvarType vType) then trace[Meta.isDefEq.assign.final]! "{mvar} := {v}" assignExprMVar mvar.mvarId! v pure true else trace[Meta.isDefEq.assign.typeMismatch]! "{mvar} : {mvarType} := {v} : {vType}" pure false /-- Auxiliary method for solving constraints of the form `?m xs := v`. It creates a lambda using `mkLambdaFVars ys v`, where `ys` is a superset of `xs`. `ys` is often equal to `xs`. It is a bigger when there are let-declaration dependencies in `xs`. For example, suppose we have `xs` of the form `#[a, c]` where ``` a : Nat b : Nat := f a c : b = a ``` In this scenario, the type of `?m` is `(x1 : Nat) -> (x2 : f x1 = x1) -> C[x1, x2]`, and type of `v` is `C[a, c]`. Note that, `?m a c` is type correct since `f a = a` is definitionally equal to the type of `c : b = a`, and the type of `?m a c` is equal to the type of `v`. Note that `fun xs => v` is the term `fun (x1 : Nat) (x2 : b = x1) => v` which has type `(x1 : Nat) -> (x2 : b = x1) -> C[x1, x2]` which is not definitionally equal to the type of `?m`, and may not even be type correct. The issue here is that we are not capturing the `let`-declarations. This method collects let-declarations `y` occurring between `xs[0]` and `xs.back` s.t. some `x` in `xs` depends on `y`. `ys` is the `xs` with these extra let-declarations included. In the example above, `ys` is `#[a, b, c]`, and `mkLambdaFVars ys v` produces `fun a => let b := f a; fun (c : b = a) => v` which has a type definitionally equal to the type of `?m`. Recall that the method `checkAssignment` ensures `v` does not contain offending `let`-declarations. This method assumes that for any `xs[i]` and `xs[j]` where `i < j`, we have that `index of xs[i]` < `index of xs[j]`. where the index is the position in the local context. -/ private partial def mkLambdaFVarsWithLetDeps (xs : Array Expr) (v : Expr) : MetaM (Option Expr) := do if not (← hasLetDeclsInBetween) then mkLambdaFVars xs v else let ys ← addLetDeps trace[Meta.debug]! "ys: {ys}, v: {v}" mkLambdaFVars ys v where /- Return true if there are let-declarions between `xs[0]` and `xs[xs.size-1]`. We use it a quick-check to avoid the more expensive collection procedure. -/ hasLetDeclsInBetween : MetaM Bool := do let check (lctx : LocalContext) : Bool := do let start := lctx.getFVar! xs[0] |>.index let stop := lctx.getFVar! xs.back |>.index for i in [start+1:stop] do match lctx.getAt! i with | some localDecl => if localDecl.isLet then return true | _ => pure () return false if xs.size <= 1 then pure false else check (← getLCtx) /- Traverse `e` and stores in the state `NameHashSet` any let-declaration with index greater than `(← read)`. The context `Nat` is the position of `xs[0]` in the local context. -/ collectLetDeclsFrom (e : Expr) : ReaderT Nat (StateRefT NameHashSet MetaM) Unit := do let rec visit (e : Expr) : MonadCacheT Expr Unit (ReaderT Nat (StateRefT NameHashSet MetaM)) Unit := checkCache e fun _ => do match e with | Expr.forallE _ d b _ => visit d; visit b | Expr.lam _ d b _ => visit d; visit b | Expr.letE _ t v b _ => visit t; visit v; visit b | Expr.app f a _ => visit f; visit a | Expr.mdata _ b _ => visit b | Expr.proj _ _ b _ => visit b | Expr.fvar fvarId _ => let localDecl ← getLocalDecl fvarId if localDecl.isLet && localDecl.index > (← read) then modify fun s => s.insert localDecl.fvarId | _ => pure () visit (← instantiateMVars e) |>.run /- Auxiliary definition for traversing all declarations between `xs[0]` ... `xs.back` backwards. The `Nat` argument is the current position in the local context being visited, and it is less than or equal to the position of `xs.back` in the local context. The `Nat` context `(← read)` is the position of `xs[0]` in the local context. -/ collectLetDepsAux : Nat → ReaderT Nat (StateRefT NameHashSet MetaM) Unit | 0 => return () | i+1 => do if i+1 == (← read) then return () else match (← getLCtx).getAt! (i+1) with | none => collectLetDepsAux i | some localDecl => if (← get).contains localDecl.fvarId then collectLetDeclsFrom localDecl.type match localDecl.value? with | some val => collectLetDeclsFrom val | _ => pure () collectLetDepsAux i /- Computes the set `ys`. It is a set of `FVarId`s, -/ collectLetDeps : MetaM NameHashSet := do let lctx ← getLCtx let start := lctx.getFVar! xs[0] |>.index let stop := lctx.getFVar! xs.back |>.index let s := xs.foldl (init := {}) fun s x => s.insert x.fvarId! let (_, s) ← collectLetDepsAux stop |>.run start |>.run s return s /- Computes the array `ys` containing let-decls between `xs[0]` and `xs.back` that some `x` in `xs` depends on. -/ addLetDeps : MetaM (Array Expr) := do let lctx ← getLCtx let s ← collectLetDeps /- Convert `s` into the the array `ys` -/ let start := lctx.getFVar! xs[0] |>.index let stop := lctx.getFVar! xs.back |>.index let mut ys := #[] for i in [start:stop+1] do match lctx.getAt! i with | none => pure () | some localDecl => if s.contains localDecl.fvarId then ys := ys.push localDecl.toExpr return ys /- Each metavariable is declared in a particular local context. We use the notation `C |- ?m : t` to denote a metavariable `?m` that was declared at the local context `C` with type `t` (see `MetavarDecl`). We also use `?m@C` as a shorthand for `C |- ?m : t` where `t` is the type of `?m`. The following method process the unification constraint ?m@C a₁ ... aₙ =?= t We say the unification constraint is a pattern IFF 1) `a₁ ... aₙ` are pairwise distinct free variables that are ​*not*​ let-variables. 2) `a₁ ... aₙ` are not in `C` 3) `t` only contains free variables in `C` and/or `{a₁, ..., aₙ}` 4) For every metavariable `?m'@C'` occurring in `t`, `C'` is a subprefix of `C` 5) `?m` does not occur in `t` Claim: we don't have to check free variable declarations. That is, if `t` contains a reference to `x : A := v`, we don't need to check `v`. Reason: The reference to `x` is a free variable, and it must be in `C` (by 1 and 3). If `x` is in `C`, then any metavariable occurring in `v` must have been defined in a strict subprefix of `C`. So, condition 4 and 5 are satisfied. If the conditions above have been satisfied, then the solution for the unification constrain is ?m := fun a₁ ... aₙ => t Now, we consider some workarounds/approximations. A1) Suppose `t` contains a reference to `x : A := v` and `x` is not in `C` (failed condition 3) (precise) solution: unfold `x` in `t`. A2) Suppose some `aᵢ` is in `C` (failed condition 2) (approximated) solution (when `config.ctxApprox` is set to true) : ignore condition and also use ?m := fun a₁ ... aₙ => t Here is an example where this approximation fails: Given `C` containing `a : nat`, consider the following two constraints ?m@C a =?= a ?m@C b =?= a If we use the approximation in the first constraint, we get ?m := fun x => x when we apply this solution to the second one we get a failure. IMPORTANT: When applying this approximation we need to make sure the abstracted term `fun a₁ ... aₙ => t` is type correct. The check can only be skipped in the pattern case described above. Consider the following example. Given the local context (α : Type) (a : α) we try to solve ?m α =?= @id α a If we use the approximation above we obtain: ?m := (fun α' => @id α' a) which is a type incorrect term. `a` has type `α` but it is expected to have type `α'`. The problem occurs because the right hand side contains a free variable `a` that depends on the free variable `α` being abstracted. Note that this dependency cannot occur in patterns. We can address this by type checking the term after abstraction. This is not a significant performance bottleneck because this case doesn't happen very often in practice (262 times when compiling stdlib on Jan 2018). The second example is trickier, but it also occurs less frequently (8 times when compiling stdlib on Jan 2018, and all occurrences were at Init/Control when we define monads and auxiliary combinators for them). We considered three options for the addressing the issue on the second example: A3) `a₁ ... aₙ` are not pairwise distinct (failed condition 1). In Lean3, we would try to approximate this case using an approach similar to A2. However, this approximation complicates the code, and is never used in the Lean3 stdlib and mathlib. A4) `t` contains a metavariable `?m'@C'` where `C'` is not a subprefix of `C`. If `?m'` is assigned, we substitute. If not, we create an auxiliary metavariable with a smaller scope. Actually, we let `elimMVarDeps` at `MetavarContext.lean` to perform this step. A5) If some `aᵢ` is not a free variable, then we use first-order unification (if `config.foApprox` is set to true) ?m a_1 ... a_i a_{i+1} ... a_{i+k} =?= f b_1 ... b_k reduces to ?M a_1 ... a_i =?= f a_{i+1} =?= b_1 ... a_{i+k} =?= b_k A6) If (m =?= v) is of the form ?m a_1 ... a_n =?= ?m b_1 ... b_k then we use first-order unification (if `config.foApprox` is set to true) A7) When `foApprox`, we may use another approximation (`constApprox`) for solving constraints of the form ``` ?m s₁ ... sₙ =?= t ``` where `s₁ ... sₙ` are arbitrary terms. We solve them by assigning the constant function to `?m`. ``` ?m := fun _ ... _ => t ``` In general, this approximation may produce bad solutions, and may prevent coercions from being tried. For example, consider the term `pure (x > 0)` with inferred type `?m Prop` and expected type `IO Bool`. In this situation, the elaborator generates the unification constraint ``` ?m Prop =?= IO Bool ``` It is not a higher-order pattern, nor first-order approximation is applicable. However, constant approximation produces the bogus solution `?m := fun _ => IO Bool`, and prevents the system from using the coercion from the decidable proposition `x > 0` to `Bool`. On the other hand, the constant approximation is desirable for elaborating the term ``` let f (x : _) := pure "hello"; f () ``` with expected type `IO String`. In this example, the following unification contraint is generated. ``` ?m () String =?= IO String ``` It is not a higher-order pattern, first-order approximation reduces it to ``` ?m () =?= IO ``` which fails to be solved. However, constant approximation solves it by assigning ``` ?m := fun _ => IO ``` Note that `f`s type is `(x : ?α) -> ?m x String`. The metavariable `?m` may depend on `x`. If `constApprox` is set to true, we use constant approximation. Otherwise, we use a heuristic to decide whether we should apply it or not. The heuristic is based on observing where the constraints above come from. In the first example, the constraint `?m Prop =?= IO Bool` come from polymorphic method where `?m` is expected to be a **function** of type `Type -> Type`. In the second example, the first argument of `?m` is used to model a **potential** dependency on `x`. By using constant approximation here, we are just saying the type of `f` does **not** depend on `x`. We claim this is a reasonable approximation in practice. Moreover, it is expected by any functional programmer used to non-dependently type languages (e.g., Haskell). We distinguish the two cases above by using the field `numScopeArgs` at `MetavarDecl`. This fiels tracks how many metavariable arguments are representing dependencies. -/ def mkAuxMVar (lctx : LocalContext) (localInsts : LocalInstances) (type : Expr) (numScopeArgs : Nat := 0) : MetaM Expr := do mkFreshExprMVarAt lctx localInsts type MetavarKind.natural Name.anonymous numScopeArgs namespace CheckAssignment builtin_initialize checkAssignmentExceptionId : InternalExceptionId ← registerInternalExceptionId `checkAssignment builtin_initialize outOfScopeExceptionId : InternalExceptionId ← registerInternalExceptionId `outOfScope structure State where cache : ExprStructMap Expr := {} structure Context where mvarId : MVarId mvarDecl : MetavarDecl fvars : Array Expr hasCtxLocals : Bool rhs : Expr abbrev CheckAssignmentM := ReaderT Context $ StateRefT State MetaM def throwCheckAssignmentFailure {α} : CheckAssignmentM α := throw <| Exception.internal checkAssignmentExceptionId def throwOutOfScopeFVar {α} : CheckAssignmentM α := throw <| Exception.internal outOfScopeExceptionId private def findCached? (e : Expr) : CheckAssignmentM (Option Expr) := do return (← get).cache.find? e private def cache (e r : Expr) : CheckAssignmentM Unit := do modify fun s => { s with cache := s.cache.insert e r } instance : MonadCache Expr Expr CheckAssignmentM where findCached? := findCached? cache := cache @[inline] private def visit (f : Expr → CheckAssignmentM Expr) (e : Expr) : CheckAssignmentM Expr := if !e.hasExprMVar && !e.hasFVar then pure e else checkCache e (fun _ => f e) private def addAssignmentInfo (msg : MessageData) : CheckAssignmentM MessageData := do let ctx ← read return m!"{msg} @ {mkMVar ctx.mvarId} {ctx.fvars} := {ctx.rhs}" @[inline] def run (x : CheckAssignmentM Expr) (mvarId : MVarId) (fvars : Array Expr) (hasCtxLocals : Bool) (v : Expr) : MetaM (Option Expr) := do let mvarDecl ← getMVarDecl mvarId let ctx := { mvarId := mvarId, mvarDecl := mvarDecl, fvars := fvars, hasCtxLocals := hasCtxLocals, rhs := v : Context } let x : CheckAssignmentM (Option Expr) := catchInternalIds [outOfScopeExceptionId, checkAssignmentExceptionId] (do let e ← x; return some e) (fun _ => pure none) x.run ctx |>.run' {} mutual partial def checkFVar (fvar : Expr) : CheckAssignmentM Expr := do let ctxMeta ← readThe Meta.Context let ctx ← read if ctx.mvarDecl.lctx.containsFVar fvar then pure fvar else let lctx := ctxMeta.lctx match lctx.findFVar? fvar with | some (LocalDecl.ldecl (value := v) ..) => visit check v | _ => if ctx.fvars.contains fvar then pure fvar else traceM `Meta.isDefEq.assign.outOfScopeFVar do addAssignmentInfo fvar throwOutOfScopeFVar partial def checkMVar (mvar : Expr) : CheckAssignmentM Expr := do let mvarId := mvar.mvarId! let ctx ← read let mctx ← getMCtx if mvarId == ctx.mvarId then traceM `Meta.isDefEq.assign.occursCheck <| addAssignmentInfo "occurs check failed" throwCheckAssignmentFailure else match mctx.getExprAssignment? mvarId with | some v => check v | none => match mctx.findDecl? mvarId with | none => throwUnknownMVar mvarId | some mvarDecl => if ctx.hasCtxLocals then throwCheckAssignmentFailure -- It is not a pattern, then we fail and fall back to FO unification else if mvarDecl.lctx.isSubPrefixOf ctx.mvarDecl.lctx ctx.fvars then /- The local context of `mvar` - free variables being abstracted is a subprefix of the metavariable being assigned. We "substract" variables being abstracted because we use `elimMVarDeps` -/ pure mvar else if mvarDecl.depth != mctx.depth || mvarDecl.kind.isSyntheticOpaque then traceM `Meta.isDefEq.assign.readOnlyMVarWithBiggerLCtx <| addAssignmentInfo (mkMVar mvarId) throwCheckAssignmentFailure else let ctxMeta ← readThe Meta.Context if ctxMeta.config.ctxApprox && ctx.mvarDecl.lctx.isSubPrefixOf mvarDecl.lctx then /- Create an auxiliary metavariable with a smaller context and "checked" type. Note that `mvarType` may be different from `mvarDecl.type`. Example: `mvarType` contains a metavariable that we also need to reduce the context. We remove from `ctx.mvarDecl.lctx` any variable that is not in `mvarDecl.lctx` or in `ctx.fvars`. We don't need to remove the ones in `ctx.fvars` because `elimMVarDeps` will take care of them. First, we collect `toErase` the variables that need to be erased. Notat that if a variable is `ctx.fvars`, but it depends on variable at `toErase`, we must also erase it. -/ let toErase := mvarDecl.lctx.foldl (init := #[]) fun toErase localDecl => if ctx.mvarDecl.lctx.contains localDecl.fvarId then toErase else if ctx.fvars.any fun fvar => fvar.fvarId! == localDecl.fvarId then if mctx.findLocalDeclDependsOn localDecl fun fvarId => toErase.contains fvarId then -- localDecl depends on a variable that will be erased. So, we must add it to `toErase` too toErase.push localDecl.fvarId else toErase else toErase.push localDecl.fvarId let lctx := toErase.foldl (init := mvarDecl.lctx) fun lctx toEraseFVar => lctx.erase toEraseFVar /- Compute new set of local instances. -/ let localInsts := mvarDecl.localInstances.filter fun localInst => toErase.contains localInst.fvar.fvarId! let mvarType ← check mvarDecl.type let newMVar ← mkAuxMVar lctx localInsts mvarType mvarDecl.numScopeArgs modifyThe Meta.State fun s => { s with mctx := s.mctx.assignExpr mvarId newMVar } pure newMVar else traceM `Meta.isDefEq.assign.readOnlyMVarWithBiggerLCtx <| addAssignmentInfo (mkMVar mvarId) throwCheckAssignmentFailure /- Auxiliary function used to "fix" subterms of the form `?m x_1 ... x_n` where `x_i`s are free variables, and one of them is out-of-scope. See `Expr.app` case at `check`. If `ctxApprox` is true, then we solve this case by creating a fresh metavariable ?n with the correct scope, an assigning `?m := fun _ ... _ => ?n` -/ partial def assignToConstFun (mvar : Expr) (numArgs : Nat) (newMVar : Expr) : MetaM Bool := do let mvarType ← inferType mvar forallBoundedTelescope mvarType numArgs fun xs _ => do if xs.size != numArgs then pure false else let some v ← mkLambdaFVarsWithLetDeps xs newMVar | return false match (← checkAssignmentAux mvar.mvarId! #[] false v) with | some v => checkTypesAndAssign mvar v | none => return false -- See checkAssignment partial def checkAssignmentAux (mvarId : MVarId) (fvars : Array Expr) (hasCtxLocals : Bool) (v : Expr) : MetaM (Option Expr) := do run (check v) mvarId fvars hasCtxLocals v partial def check (e : Expr) : CheckAssignmentM Expr := do match e with | Expr.mdata _ b _ => return e.updateMData! (← visit check b) | Expr.proj _ _ s _ => return e.updateProj! (← visit check s) | Expr.lam _ d b _ => return e.updateLambdaE! (← visit check d) (← visit check b) | Expr.forallE _ d b _ => return e.updateForallE! (← visit check d) (← visit check b) | Expr.letE _ t v b _ => return e.updateLet! (← visit check t) (← visit check v) (← visit check b) | Expr.bvar .. => return e | Expr.sort .. => return e | Expr.const .. => return e | Expr.lit .. => return e | Expr.fvar .. => visit checkFVar e | Expr.mvar .. => visit checkMVar e | Expr.app .. => e.withApp fun f args => do let ctxMeta ← readThe Meta.Context if f.isMVar && ctxMeta.config.ctxApprox && args.all Expr.isFVar then let f ← visit checkMVar f catchInternalId outOfScopeExceptionId (do let args ← args.mapM (visit check) return mkAppN f args) (fun ex => do if (← f.isMVar <&&> isDelayedAssigned f.mvarId!) then throw ex else let eType ← inferType e let mvarType ← check eType /- Create an auxiliary metavariable with a smaller context and "checked" type, assign `?f := fun _ => ?newMVar` Note that `mvarType` may be different from `eType`. -/ let ctx ← read let newMVar ← mkAuxMVar ctx.mvarDecl.lctx ctx.mvarDecl.localInstances mvarType if (← assignToConstFun f args.size newMVar) then pure newMVar else throw ex) else let f ← visit check f let args ← args.mapM (visit check) return mkAppN f args end end CheckAssignment namespace CheckAssignmentQuick partial def check (hasCtxLocals ctxApprox : Bool) (mctx : MetavarContext) (lctx : LocalContext) (mvarDecl : MetavarDecl) (mvarId : MVarId) (fvars : Array Expr) (e : Expr) : Bool := let rec visit (e : Expr) : Bool := if !e.hasExprMVar && !e.hasFVar then true else match e with | Expr.mdata _ b _ => visit b | Expr.proj _ _ s _ => visit s | Expr.app f a _ => visit f && visit a | Expr.lam _ d b _ => visit d && visit b | Expr.forallE _ d b _ => visit d && visit b | Expr.letE _ t v b _ => visit t && visit v && visit b | Expr.bvar .. => true | Expr.sort .. => true | Expr.const .. => true | Expr.lit .. => true | Expr.fvar fvarId .. => if mvarDecl.lctx.contains fvarId then true else match lctx.find? fvarId with | some (LocalDecl.ldecl (value := v) ..) => false -- need expensive CheckAssignment.check | _ => if fvars.any fun x => x.fvarId! == fvarId then true else false -- We could throw an exception here, but we would have to use ExceptM. So, we let CheckAssignment.check do it | Expr.mvar mvarId' _ => match mctx.getExprAssignment? mvarId' with | some _ => false -- use CheckAssignment.check to instantiate | none => if mvarId' == mvarId then false -- occurs check failed, use CheckAssignment.check to throw exception else match mctx.findDecl? mvarId' with | none => false | some mvarDecl' => if hasCtxLocals then false -- use CheckAssignment.check else if mvarDecl'.lctx.isSubPrefixOf mvarDecl.lctx fvars then true else false -- use CheckAssignment.check visit e end CheckAssignmentQuick /-- Auxiliary function for handling constraints of the form `?m a₁ ... aₙ =?= v`. It will check whether we can perform the assignment ``` ?m := fun fvars => v ``` The result is `none` if the assignment can't be performed. The result is `some newV` where `newV` is a possibly updated `v`. This method may need to unfold let-declarations. -/ def checkAssignment (mvarId : MVarId) (fvars : Array Expr) (v : Expr) : MetaM (Option Expr) := do /- Check whether `mvarId` occurs in the type of `fvars` or not. If it does, return `none` to prevent us from creating the cyclic assignment `?m := fun fvars => v` -/ for fvar in fvars do unless (← occursCheck mvarId (← inferType fvar)) do return none if !v.hasExprMVar && !v.hasFVar then pure (some v) else let mvarDecl ← getMVarDecl mvarId let hasCtxLocals := fvars.any fun fvar => mvarDecl.lctx.containsFVar fvar let ctx ← read let mctx ← getMCtx if CheckAssignmentQuick.check hasCtxLocals ctx.config.ctxApprox mctx ctx.lctx mvarDecl mvarId fvars v then pure (some v) else let v ← instantiateMVars v CheckAssignment.checkAssignmentAux mvarId fvars hasCtxLocals v private def processAssignmentFOApproxAux (mvar : Expr) (args : Array Expr) (v : Expr) : MetaM Bool := match v with | Expr.app f a _ => if args.isEmpty then pure false else Meta.isExprDefEqAux args.back a <&&> Meta.isExprDefEqAux (mkAppRange mvar 0 (args.size - 1) args) f | _ => pure false /- Auxiliary method for applying first-order unification. It is an approximation. Remark: this method is trying to solve the unification constraint: ?m a₁ ... aₙ =?= v It is uses processAssignmentFOApproxAux, if it fails, it tries to unfold `v`. We have added support for unfolding here because we want to be able to solve unification problems such as ?m Unit =?= ITactic where `ITactic` is defined as def ITactic := Tactic Unit -/ private partial def processAssignmentFOApprox (mvar : Expr) (args : Array Expr) (v : Expr) : MetaM Bool := let rec loop (v : Expr) := do let cfg ← getConfig if !cfg.foApprox then pure false else trace[Meta.isDefEq.foApprox]! "{mvar} {args} := {v}" let v := v.headBeta if (← commitWhen <| processAssignmentFOApproxAux mvar args v) then pure true else match (← unfoldDefinition? v) with | none => pure false | some v => loop v loop v private partial def simpAssignmentArgAux : Expr → MetaM Expr | Expr.mdata _ e _ => simpAssignmentArgAux e | e@(Expr.fvar fvarId _) => do let decl ← getLocalDecl fvarId match decl.value? with | some value => simpAssignmentArgAux value | _ => pure e | e => pure e /- Auxiliary procedure for processing `?m a₁ ... aₙ =?= v`. We apply it to each `aᵢ`. It instantiates assigned metavariables if `aᵢ` is of the form `f[?n] b₁ ... bₘ`, and then removes metadata, and zeta-expand let-decls. -/ private def simpAssignmentArg (arg : Expr) : MetaM Expr := do let arg ← if arg.getAppFn.hasExprMVar then instantiateMVars arg else pure arg simpAssignmentArgAux arg /- Assign `mvar := fun a_1 ... a_{numArgs} => v`. We use it at `processConstApprox` and `isDefEqMVarSelf` -/ private def assignConst (mvar : Expr) (numArgs : Nat) (v : Expr) : MetaM Bool := do let mvarDecl ← getMVarDecl mvar.mvarId! forallBoundedTelescope mvarDecl.type numArgs fun xs _ => do if xs.size != numArgs then pure false else let some v ← mkLambdaFVarsWithLetDeps xs v | pure false match (← checkAssignment mvar.mvarId! #[] v) with | none => pure false | some v => trace[Meta.isDefEq.constApprox]! "{mvar} := {v}" checkTypesAndAssign mvar v private def processConstApprox (mvar : Expr) (numArgs : Nat) (v : Expr) : MetaM Bool := do let cfg ← getConfig let mvarId := mvar.mvarId! let mvarDecl ← getMVarDecl mvarId if mvarDecl.numScopeArgs == numArgs || cfg.constApprox then assignConst mvar numArgs v else pure false /-- Tries to solve `?m a₁ ... aₙ =?= v` by assigning `?m`. It assumes `?m` is unassigned. -/ private partial def processAssignment (mvarApp : Expr) (v : Expr) : MetaM Bool := traceCtx `Meta.isDefEq.assign do trace[Meta.isDefEq.assign]! "{mvarApp} := {v}" let mvar := mvarApp.getAppFn let mvarDecl ← getMVarDecl mvar.mvarId! let rec process (i : Nat) (args : Array Expr) (v : Expr) := do let cfg ← getConfig let useFOApprox (args : Array Expr) : MetaM Bool := processAssignmentFOApprox mvar args v <||> processConstApprox mvar args.size v if h : i < args.size then let arg := args.get ⟨i, h⟩ let arg ← simpAssignmentArg arg let args := args.set ⟨i, h⟩ arg match arg with | Expr.fvar fvarId _ => if args[0:i].any fun prevArg => prevArg == arg then useFOApprox args else if mvarDecl.lctx.contains fvarId && !cfg.quasiPatternApprox then useFOApprox args else process (i+1) args v | _ => useFOApprox args else let v ← instantiateMVars v -- enforce A4 if v.getAppFn == mvar then -- using A6 useFOApprox args else let mvarId := mvar.mvarId! match (← checkAssignment mvarId args v) with | none => useFOApprox args | some v => do trace[Meta.isDefEq.assign.beforeMkLambda]! "{mvar} {args} := {v}" let some v ← mkLambdaFVarsWithLetDeps args v | return false if args.any (fun arg => mvarDecl.lctx.containsFVar arg) then /- We need to type check `v` because abstraction using `mkLambdaFVars` may have produced a type incorrect term. See discussion at A2 -/ if (← isTypeCorrect v) then checkTypesAndAssign mvar v else trace[Meta.isDefEq.assign.typeError]! "{mvar} := {v}" useFOApprox args else checkTypesAndAssign mvar v process 0 mvarApp.getAppArgs v /-- Similar to processAssignment, but if it fails, compute v's whnf and try again. This helps to solve constraints such as `?m =?= { α := ?m, ... }.α` Note this is not perfect solution since we still fail occurs check for constraints such as ```lean ?m =?= List { α := ?m, β := Nat }.β ``` -/ private def processAssignment' (mvarApp : Expr) (v : Expr) : MetaM Bool := do if (← processAssignment mvarApp v) then return true else let vNew ← whnf v if vNew != v then if mvarApp == vNew then return true else processAssignment mvarApp vNew else return false private def isDeltaCandidate? (t : Expr) : MetaM (Option ConstantInfo) := match t.getAppFn with | Expr.const c _ _ => getConst? c | _ => pure none /-- Auxiliary method for isDefEqDelta -/ private def isListLevelDefEq (us vs : List Level) : MetaM LBool := toLBoolM <| isListLevelDefEqAux us vs /-- Auxiliary method for isDefEqDelta -/ private def isDefEqLeft (fn : Name) (t s : Expr) : MetaM LBool := do trace[Meta.isDefEq.delta.unfoldLeft]! fn toLBoolM <| Meta.isExprDefEqAux t s /-- Auxiliary method for isDefEqDelta -/ private def isDefEqRight (fn : Name) (t s : Expr) : MetaM LBool := do trace[Meta.isDefEq.delta.unfoldRight]! fn toLBoolM <| Meta.isExprDefEqAux t s /-- Auxiliary method for isDefEqDelta -/ private def isDefEqLeftRight (fn : Name) (t s : Expr) : MetaM LBool := do trace[Meta.isDefEq.delta.unfoldLeftRight]! fn toLBoolM <| Meta.isExprDefEqAux t s /-- Try to solve `f a₁ ... aₙ =?= f b₁ ... bₙ` by solving `a₁ =?= b₁, ..., aₙ =?= bₙ`. Auxiliary method for isDefEqDelta -/ private def tryHeuristic (t s : Expr) : MetaM Bool := let tFn := t.getAppFn let sFn := s.getAppFn traceCtx `Meta.isDefEq.delta do commitWhen do let b ← isDefEqArgs tFn t.getAppArgs s.getAppArgs <&&> isListLevelDefEqAux tFn.constLevels! sFn.constLevels! unless b do trace[Meta.isDefEq.delta]! "heuristic failed {t} =?= {s}" pure b /-- Auxiliary method for isDefEqDelta -/ private abbrev unfold {α} (e : Expr) (failK : MetaM α) (successK : Expr → MetaM α) : MetaM α := do match (← unfoldDefinition? e) with | some e => successK e | none => failK /-- Auxiliary method for isDefEqDelta -/ private def unfoldBothDefEq (fn : Name) (t s : Expr) : MetaM LBool := do match t, s with | Expr.const _ ls₁ _, Expr.const _ ls₂ _ => isListLevelDefEq ls₁ ls₂ | Expr.app _ _ _, Expr.app _ _ _ => if (← tryHeuristic t s) then pure LBool.true else unfold t (unfold s (pure LBool.false) (fun s => isDefEqRight fn t s)) (fun t => unfold s (isDefEqLeft fn t s) (fun s => isDefEqLeftRight fn t s)) | _, _ => pure LBool.false private def sameHeadSymbol (t s : Expr) : Bool := match t.getAppFn, s.getAppFn with | Expr.const c₁ _ _, Expr.const c₂ _ _ => true | _, _ => false /-- - If headSymbol (unfold t) == headSymbol s, then unfold t - If headSymbol (unfold s) == headSymbol t, then unfold s - Otherwise unfold t and s if possible. Auxiliary method for isDefEqDelta -/ private def unfoldComparingHeadsDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool := unfold t (unfold s (pure LBool.undef) -- `t` and `s` failed to be unfolded (fun s => isDefEqRight sInfo.name t s)) (fun tNew => if sameHeadSymbol tNew s then isDefEqLeft tInfo.name tNew s else unfold s (isDefEqLeft tInfo.name tNew s) (fun sNew => if sameHeadSymbol t sNew then isDefEqRight sInfo.name t sNew else isDefEqLeftRight tInfo.name tNew sNew)) /-- If `t` and `s` do not contain metavariables, then use kernel definitional equality heuristics. Otherwise, use `unfoldComparingHeadsDefEq`. Auxiliary method for isDefEqDelta -/ private def unfoldDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool := if !t.hasExprMVar && !s.hasExprMVar then /- If `t` and `s` do not contain metavariables, we simulate strategy used in the kernel. -/ if tInfo.hints.lt sInfo.hints then unfold t (unfoldComparingHeadsDefEq tInfo sInfo t s) fun t => isDefEqLeft tInfo.name t s else if sInfo.hints.lt tInfo.hints then unfold s (unfoldComparingHeadsDefEq tInfo sInfo t s) fun s => isDefEqRight sInfo.name t s else unfoldComparingHeadsDefEq tInfo sInfo t s else unfoldComparingHeadsDefEq tInfo sInfo t s /-- When `TransparencyMode` is set to `default` or `all`. If `t` is reducible and `s` is not ==> `isDefEqLeft (unfold t) s` If `s` is reducible and `t` is not ==> `isDefEqRight t (unfold s)` Otherwise, use `unfoldDefEq` Auxiliary method for isDefEqDelta -/ private def unfoldReducibeDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool := do if (← shouldReduceReducibleOnly) then unfoldDefEq tInfo sInfo t s else let tReducible ← isReducible tInfo.name let sReducible ← isReducible sInfo.name if tReducible && !sReducible then unfold t (unfoldDefEq tInfo sInfo t s) fun t => isDefEqLeft tInfo.name t s else if !tReducible && sReducible then unfold s (unfoldDefEq tInfo sInfo t s) fun s => isDefEqRight sInfo.name t s else unfoldDefEq tInfo sInfo t s /-- If `t` is a projection function application and `s` is not ==> `isDefEqRight t (unfold s)` If `s` is a projection function application and `t` is not ==> `isDefEqRight (unfold t) s` Otherwise, use `unfoldReducibeDefEq` Auxiliary method for isDefEqDelta -/ private def unfoldNonProjFnDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool := do let tProj? ← isProjectionFn tInfo.name let sProj? ← isProjectionFn sInfo.name if tProj? && !sProj? then unfold s (unfoldDefEq tInfo sInfo t s) fun s => isDefEqRight sInfo.name t s else if !tProj? && sProj? then unfold t (unfoldDefEq tInfo sInfo t s) fun t => isDefEqLeft tInfo.name t s else unfoldReducibeDefEq tInfo sInfo t s /-- isDefEq by lazy delta reduction. This method implements many different heuristics: 1- If only `t` can be unfolded => then unfold `t` and continue 2- If only `s` can be unfolded => then unfold `s` and continue 3- If `t` and `s` can be unfolded and they have the same head symbol, then a) First try to solve unification by unifying arguments. b) If it fails, unfold both and continue. Implemented by `unfoldBothDefEq` 4- If `t` is a projection function application and `s` is not => then unfold `s` and continue. 5- If `s` is a projection function application and `t` is not => then unfold `t` and continue. Remark: 4&5 are implemented by `unfoldNonProjFnDefEq` 6- If `t` is reducible and `s` is not => then unfold `t` and continue. 7- If `s` is reducible and `t` is not => then unfold `s` and continue Remark: 6&7 are implemented by `unfoldReducibeDefEq` 8- If `t` and `s` do not contain metavariables, then use heuristic used in the Kernel. Implemented by `unfoldDefEq` 9- If `headSymbol (unfold t) == headSymbol s`, then unfold t and continue. 10- If `headSymbol (unfold s) == headSymbol t`, then unfold s 11- Otherwise, unfold `t` and `s` and continue. Remark: 9&10&11 are implemented by `unfoldComparingHeadsDefEq` -/ private def isDefEqDelta (t s : Expr) : MetaM LBool := do let tInfo? ← isDeltaCandidate? t.getAppFn let sInfo? ← isDeltaCandidate? s.getAppFn match tInfo?, sInfo? with | none, none => pure LBool.undef | some tInfo, none => unfold t (pure LBool.undef) fun t => isDefEqLeft tInfo.name t s | none, some sInfo => unfold s (pure LBool.undef) fun s => isDefEqRight sInfo.name t s | some tInfo, some sInfo => if tInfo.name == sInfo.name then unfoldBothDefEq tInfo.name t s else unfoldNonProjFnDefEq tInfo sInfo t s private def isAssigned : Expr → MetaM Bool | Expr.mvar mvarId _ => isExprMVarAssigned mvarId | _ => pure false private def isDelayedAssignedHead (tFn : Expr) (t : Expr) : MetaM Bool := do match tFn with | Expr.mvar mvarId _ => if (← isDelayedAssigned mvarId) then let tNew ← instantiateMVars t return tNew != t else pure false | _ => pure false private def isSynthetic : Expr → MetaM Bool | Expr.mvar mvarId _ => do let mvarDecl ← getMVarDecl mvarId match mvarDecl.kind with | MetavarKind.synthetic => pure true | MetavarKind.syntheticOpaque => pure true | MetavarKind.natural => pure false | _ => pure false private def isAssignable : Expr → MetaM Bool | Expr.mvar mvarId _ => do let b ← isReadOnlyOrSyntheticOpaqueExprMVar mvarId; pure (!b) | _ => pure false private def etaEq (t s : Expr) : Bool := match t.etaExpanded? with | some t => t == s | none => false private def isLetFVar (fvarId : FVarId) : MetaM Bool := do let decl ← getLocalDecl fvarId pure decl.isLet private def isDefEqProofIrrel (t s : Expr) : MetaM LBool := do let status ← isProofQuick t match status with | LBool.false => pure LBool.undef | LBool.true => let tType ← inferType t let sType ← inferType s toLBoolM <| Meta.isExprDefEqAux tType sType | LBool.undef => let tType ← inferType t if (← isProp tType) then let sType ← inferType s toLBoolM <| Meta.isExprDefEqAux tType sType else pure LBool.undef /- Try to solve constraint of the form `?m args₁ =?= ?m args₂`. - First try to unify `args₁` and `args₂`, and return true if successful - Otherwise, try to assign `?m` to a constant function of the form `fun x_1 ... x_n => ?n` where `?n` is a fresh metavariable. See `processConstApprox`. -/ private def isDefEqMVarSelf (mvar : Expr) (args₁ args₂ : Array Expr) : MetaM Bool := do if args₁.size != args₂.size then pure false else if (← isDefEqArgs mvar args₁ args₂) then pure true else if !(← isAssignable mvar) then pure false else let cfg ← getConfig let mvarId := mvar.mvarId! let mvarDecl ← getMVarDecl mvarId if mvarDecl.numScopeArgs == args₁.size || cfg.constApprox then let type ← inferType (mkAppN mvar args₁) let auxMVar ← mkAuxMVar mvarDecl.lctx mvarDecl.localInstances type assignConst mvar args₁.size auxMVar else pure false /- Remove unnecessary let-decls -/ private def consumeLet : Expr → Expr | e@(Expr.letE _ _ _ b _) => if b.hasLooseBVars then e else consumeLet b | e => e mutual private partial def isDefEqQuick (t s : Expr) : MetaM LBool := let t := consumeLet t let s := consumeLet s match t, s with | Expr.lit l₁ _, Expr.lit l₂ _ => return (l₁ == l₂).toLBool | Expr.sort u _, Expr.sort v _ => toLBoolM <| isLevelDefEqAux u v | Expr.lam .., Expr.lam .. => if t == s then pure LBool.true else toLBoolM <| isDefEqBinding t s | Expr.forallE .., Expr.forallE .. => if t == s then pure LBool.true else toLBoolM <| isDefEqBinding t s | Expr.mdata _ t _, s => isDefEqQuick t s | t, Expr.mdata _ s _ => isDefEqQuick t s | Expr.fvar fvarId₁ _, Expr.fvar fvarId₂ _ => do if (← isLetFVar fvarId₁ <||> isLetFVar fvarId₂) then pure LBool.undef else if fvarId₁ == fvarId₂ then pure LBool.true else isDefEqProofIrrel t s | t, s => isDefEqQuickOther t s private partial def isDefEqQuickOther (t s : Expr) : MetaM LBool := do if t == s then pure LBool.true else if etaEq t s || etaEq s t then pure LBool.true -- t =?= (fun xs => t xs) else let tFn := t.getAppFn let sFn := s.getAppFn if !tFn.isMVar && !sFn.isMVar then pure LBool.undef else if (← isAssigned tFn) then let t ← instantiateMVars t isDefEqQuick t s else if (← isAssigned sFn) then let s ← instantiateMVars s isDefEqQuick t s else if (← isDelayedAssignedHead tFn t) then let t ← instantiateMVars t isDefEqQuick t s else if (← isDelayedAssignedHead sFn s) then let s ← instantiateMVars s isDefEqQuick t s else if (← isSynthetic tFn <&&> trySynthPending tFn) then let t ← instantiateMVars t isDefEqQuick t s else if (← isSynthetic sFn <&&> trySynthPending sFn) then let s ← instantiateMVars s isDefEqQuick t s else if tFn.isMVar && sFn.isMVar && tFn == sFn then Bool.toLBool <$> isDefEqMVarSelf tFn t.getAppArgs s.getAppArgs else let tAssign? ← isAssignable tFn let sAssign? ← isAssignable sFn let assignableMsg (b : Bool) := if b then "[assignable]" else "[nonassignable]" trace[Meta.isDefEq]! "{t} {assignableMsg tAssign?} =?= {s} {assignableMsg sAssign?}" if tAssign? && !sAssign? then toLBoolM <| processAssignment' t s else if !tAssign? && sAssign? then toLBoolM <| processAssignment' s t else if !tAssign? && !sAssign? then if tFn.isMVar || sFn.isMVar then let ctx ← read if ctx.config.isDefEqStuckEx then do trace[Meta.isDefEq.stuck]! "{t} =?= {s}" Meta.throwIsDefEqStuck else pure LBool.false else pure LBool.undef else isDefEqQuickMVarMVar t s -- Both `t` and `s` are terms of the form `?m ...` private partial def isDefEqQuickMVarMVar (t s : Expr) : MetaM LBool := do let tFn := t.getAppFn let sFn := s.getAppFn let tMVarDecl ← getMVarDecl tFn.mvarId! let sMVarDecl ← getMVarDecl sFn.mvarId! if s.isMVar && !t.isMVar then /- Solve `?m t =?= ?n` by trying first `?n := ?m t`. Reason: this assignment is precise. -/ if (← commitWhen (processAssignment s t)) then pure LBool.true else toLBoolM <| processAssignment t s else if (← commitWhen (processAssignment t s)) then pure LBool.true else toLBoolM <| processAssignment s t end @[inline] def whenUndefDo (x : MetaM LBool) (k : MetaM Bool) : MetaM Bool := do let status ← x match status with | LBool.true => pure true | LBool.false => pure false | LBool.undef => k @[specialize] private def unstuckMVar (e : Expr) (successK : Expr → MetaM Bool) (failK : MetaM Bool): MetaM Bool := do match (← getStuckMVar? e) with | some mvarId => trace[Meta.isDefEq.stuckMVar]! "found stuck MVar {mkMVar mvarId} : {← inferType (mkMVar mvarId)}" if (← Meta.synthPending mvarId) then let e ← instantiateMVars e successK e else failK | none => failK private def isDefEqOnFailure (t s : Expr) : MetaM Bool := unstuckMVar t (fun t => Meta.isExprDefEqAux t s) <| unstuckMVar s (fun s => Meta.isExprDefEqAux t s) <| tryUnificationHints t s <||> tryUnificationHints s t private def isDefEqProj : Expr → Expr → MetaM Bool | Expr.proj _ i t _, Expr.proj _ j s _ => pure (i == j) <&&> Meta.isExprDefEqAux t s | _, _ => pure false partial def isExprDefEqAuxImpl (t : Expr) (s : Expr) : MetaM Bool := do trace[Meta.isDefEq.step]! "{t} =?= {s}" checkMaxHeartbeats "isDefEq" withNestedTraces do whenUndefDo (isDefEqQuick t s) do whenUndefDo (isDefEqProofIrrel t s) do let t' ← whnfCore t let s' ← whnfCore s if t != t' || s != s' then isExprDefEqAuxImpl t' s' else do if (← (isDefEqEta t s <||> isDefEqEta s t)) then pure true else if (← isDefEqProj t s) then pure true else whenUndefDo (isDefEqNative t s) do whenUndefDo (isDefEqNat t s) do whenUndefDo (isDefEqOffset t s) do whenUndefDo (isDefEqDelta t s) do if t.isConst && s.isConst then if t.constName! == s.constName! then isListLevelDefEqAux t.constLevels! s.constLevels! else pure false else if t.isApp && s.isApp then let tFn := t.getAppFn if (← commitWhen (Meta.isExprDefEqAux tFn s.getAppFn <&&> isDefEqArgs tFn t.getAppArgs s.getAppArgs)) then pure true else isDefEqOnFailure t s else whenUndefDo (isDefEqStringLit t s) do isDefEqOnFailure t s builtin_initialize isExprDefEqAuxRef.set isExprDefEqAuxImpl builtin_initialize registerTraceClass `Meta.isDefEq registerTraceClass `Meta.isDefEq.foApprox registerTraceClass `Meta.isDefEq.constApprox registerTraceClass `Meta.isDefEq.delta registerTraceClass `Meta.isDefEq.step registerTraceClass `Meta.isDefEq.assign end Lean.Meta
42daf7c464963e8cf7ed7cbcecbf17cfe56e14dc
618003631150032a5676f229d13a079ac875ff77
/src/init_/data/int/basic.lean
b7ddd977142006e5f6a93e885730f1b702796971
[ "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
1,656
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad Instances, copied from the core library by Johan Commelin -/ import algebra.ring instance : comm_ring int := { add := int.add, add_assoc := int.add_assoc, zero := int.zero, zero_add := int.zero_add, add_zero := int.add_zero, neg := int.neg, add_left_neg := int.add_left_neg, add_comm := int.add_comm, mul := int.mul, mul_assoc := int.mul_assoc, one := int.one, one_mul := int.one_mul, mul_one := int.mul_one, left_distrib := int.distrib_left, right_distrib := int.distrib_right, mul_comm := int.mul_comm } /- Extra instances to short-circuit type class resolution -/ -- instance : has_sub int := by apply_instance -- This is in core instance : add_comm_monoid int := by apply_instance instance : add_monoid int := by apply_instance instance : monoid int := by apply_instance instance : comm_monoid int := by apply_instance instance : comm_semigroup int := by apply_instance instance : semigroup int := by apply_instance instance : add_comm_semigroup int := by apply_instance instance : add_semigroup int := by apply_instance instance : comm_semiring int := by apply_instance instance : semiring int := by apply_instance instance : ring int := by apply_instance instance : distrib int := by apply_instance instance : nonzero ℤ := { zero_ne_one := int.zero_ne_one }
a5c3a6e763c13ddd2dbddce87163f34cbe4da6da
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/vm_eval_crash.lean
34f90d1da4c2654c31d6b81a67a2fa6ca0024bf0
[ "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
11
lean
vm_eval 10
ee57d1c34e345d87e9007d301f68664e55ec32b1
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/size_of1.lean
454e12d6c0891a852ac6edec3b301bac17ea3e8a
[ "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
203
lean
open tactic example : sizeof [tt, tt] < sizeof [tt, ff, tt] := dec_trivial example : sizeof [tt, tt] = sizeof [ff, ff] := dec_trivial example : sizeof (3:nat) < sizeof ([3] : list nat) := dec_trivial
520c7503d4793c8b7bcc6c368955e3815c19a015
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/examples/lean/ex4.lean
adc9f50e3b387fc53aaac86cefbdb9a5bd9af419
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
163
lean
import Int import tactic set_option simplifier::unfold true -- allow the simplifier to unfold definitions definition a : Nat := 10 theorem T1 : a > 0 := (by simp)
efd503dcac70f0bf27383df6cecae91a023ce032
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/tests/lean/mvar2.lean
482cb8a7669ed1fe780c31e26243c739d0cc058b
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,430
lean
import Lean.MetavarContext open Lean def check (b : Bool) : IO Unit := unless b (throw $ IO.userError "error") def f := mkConst `f [] def g := mkConst `g [] def a := mkConst `a [] def b := mkConst `b [] def c := mkConst `c [] def b0 := mkBVar 0 def b1 := mkBVar 1 def b2 := mkBVar 2 def u := mkLevelParam `u def typeE := mkSort levelOne def natE := mkConst `Nat [] def boolE := mkConst `Bool [] def vecE := mkConst `Vec [levelZero] def α := mkFVar `α def x := mkFVar `x def y := mkFVar `y def z := mkFVar `z def w := mkFVar `w def m1 := mkMVar `m1 def m2 := mkMVar `m2 def m3 := mkMVar `m3 def bi := BinderInfo.default def arrow (d b : Expr) := mkForall `_ bi d b def lctx1 : LocalContext := {} def lctx2 := lctx1.mkLocalDecl `α `α typeE def lctx3 := lctx2.mkLocalDecl `x `x m1 def lctx4 := lctx3.mkLocalDecl `y `y (arrow natE (mkAppN m3 #[α, x])) def mctx1 : MetavarContext := {} def mctx2 := mctx1.addExprMVarDecl `m1 `m1 lctx1 #[] typeE def mctx3 := mctx2.addExprMVarDecl `m2 `m2 lctx3 #[] natE def mctx4 := mctx3.addExprMVarDecl `m3 `m3 lctx1 #[] (arrow typeE (arrow natE natE)) def mctx5 := mctx4.assignDelayed `m3 lctx3 #[α, x] m2 def mctx6 := mctx5.assignExpr `m2 (arrow α α) def mctx7 := mctx6.assignExpr `m1 natE def t2 := lctx4.mkLambda #[α, x, y] $ mkAppN f #[mkAppN m3 #[α, x], x] #eval check (!t2.hasFVar) #eval t2 #eval (mctx6.instantiateMVars t2).1 #eval (mctx7.instantiateMVars t2).1
55ee463f44f1a171693ba49acb3aa2f911dbbfcd
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/dynamics/circle/rotation_number/translation_number_auto.lean
4fad633bd2da0ef19dca2f9d221f04482f9b446f
[]
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
35,568
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.specific_limits import Mathlib.order.iterate import Mathlib.order.semiconj_Sup import Mathlib.algebra.iterate_hom import Mathlib.PostPort universes l u_1 namespace Mathlib /-! # Translation number of a monotone real map that commutes with `x ↦ x + 1` Let `f : ℝ → ℝ` be a monotone map such that `f (x + 1) = f x + 1` for all `x`. Then the limit $$ \tau(f)=\lim_{n\to\infty}{f^n(x)-x}{n} $$ exists and does not depend on `x`. This number is called the *translation number* of `f`. Different authors use different notation for this number: `τ`, `ρ`, `rot`, etc In this file we define a structure `circle_deg1_lift` for bundled maps with these properties, define translation number of `f : circle_deg1_lift`, prove some estimates relating `f^n(x)-x` to `τ(f)`. In case of a continuous map `f` we also prove that `f` admits a point `x` such that `f^n(x)=x+m` if and only if `τ(f)=m/n`. Maps of this type naturally appear as lifts of orientation preserving circle homeomorphisms. More precisely, let `f` be an orientation preserving homeomorphism of the circle $S^1=ℝ/ℤ$, and consider a real number `a` such that `⟦a⟧ = f 0`, where `⟦⟧` means the natural projection `ℝ → ℝ/ℤ`. Then there exists a unique continuous function `F : ℝ → ℝ` such that `F 0 = a` and `⟦F x⟧ = f ⟦x⟧` for all `x` (this fact is not formalized yet). This function is strictly monotone, continuous, and satisfies `F (x + 1) = F x + 1`. The number `⟦τ F⟧ : ℝ / ℤ` is called the *rotation number* of `f`. It does not depend on the choice of `a`. ## Main definitions * `circle_deg1_lift`: a monotone map `f : ℝ → ℝ` such that `f (x + 1) = f x + 1` for all `x`; the type `circle_deg1_lift` is equipped with `lattice` and `monoid` structures; the multiplication is given by composition: `(f * g) x = f (g x)`. * `circle_deg1_lift.translation_number`: translation number of `f : circle_deg1_lift`. ## Main statements We prove the following properties of `circle_deg1_lift.translation_number`. * `circle_deg1_lift.translation_number_eq_of_dist_bounded`: if the distance between `(f^n) 0` and `(g^n) 0` is bounded from above uniformly in `n : ℕ`, then `f` and `g` have equal translation numbers. * `circle_deg1_lift.translation_number_eq_of_semiconj_by`: if two `circle_deg1_lift` maps `f`, `g` are semiconjugate by a `circle_deg1_lift` map, then `τ f = τ g`. * `circle_deg1_lift.translation_number_units_inv`: if `f` is an invertible `circle_deg1_lift` map (equivalently, `f` is a lift of an orientation-preserving circle homeomorphism), then the translation number of `f⁻¹` is the negative of the translation number of `f`. * `circle_deg1_lift.translation_number_mul_of_commute`: if `f` and `g` commute, then `τ (f * g) = τ f + τ g`. * `circle_deg1_lift.translation_number_eq_rat_iff`: the translation number of `f` is equal to a rational number `m / n` if and only if `(f^n) x = x + m` for some `x`. * `circle_deg1_lift.semiconj_of_bijective_of_translation_number_eq`: if `f` and `g` are two bijective `circle_deg1_lift` maps and their translation numbers are equal, then these maps are semiconjugate to each other. * `circle_deg1_lift.semiconj_of_group_action_of_forall_translation_number_eq`: let `f₁` and `f₂` be two actions of a group `G` on the circle by degree 1 maps (formally, `f₁` and `f₂` are two homomorphisms from `G →* circle_deg1_lift`). If the translation numbers of `f₁ g` and `f₂ g` are equal to each other for all `g : G`, then these two actions are semiconjugate by some `F : circle_deg1_lift`. This is a version of Proposition 5.4 from [Étienne Ghys, Groupes d'homeomorphismes du cercle et cohomologie bornee][ghys87:groupes]. ## Notation We use a local notation `τ` for the translation number of `f : circle_deg1_lift`. ## Implementation notes We define the translation number of `f : circle_deg1_lift` to be the limit of the sequence `(f ^ (2 ^ n)) 0 / (2 ^ n)`, then prove that `((f ^ n) x - x) / n` tends to this number for any `x`. This way it is much easier to prove that the limit exists and basic properties of the limit. We define translation number for a wider class of maps `f : ℝ → ℝ` instead of lifts of orientation preserving circle homeomorphisms for two reasons: * non-strictly monotone circle self-maps with discontinuities naturally appear as Poincaré maps for some flows on the two-torus (e.g., one can take a constant flow and glue in a few Cherry cells); * definition and some basic properties still work for this class. ## References * [Étienne Ghys, Groupes d'homeomorphismes du cercle et cohomologie bornee][ghys87:groupes] ## TODO Here are some short-term goals. * Introduce a structure or a typeclass for lifts of circle homeomorphisms. We use `units circle_deg1_lift` for now, but it's better to have a dedicated type (or a typeclass?). * Prove that the `semiconj_by` relation on circle homeomorphisms is an equivalence relation. * Introduce `conditionally_complete_lattice` structure, use it in the proof of `circle_deg1_lift.semiconj_of_group_action_of_forall_translation_number_eq`. * Prove that the orbits of the irrational rotation are dense in the circle. Deduce that a homeomorphism with an irrational rotation is semiconjugate to the corresponding irrational translation by a continuous `circle_deg1_lift`. ## Tags circle homeomorphism, rotation number -/ /-! ### Definition and monoid structure -/ /-- A lift of a monotone degree one map `S¹ → S¹`. -/ structure circle_deg1_lift where to_fun : ℝ → ℝ monotone' : monotone to_fun map_add_one' : ∀ (x : ℝ), to_fun (x + 1) = to_fun x + 1 namespace circle_deg1_lift protected instance has_coe_to_fun : has_coe_to_fun circle_deg1_lift := has_coe_to_fun.mk (fun (_x : circle_deg1_lift) => ℝ → ℝ) to_fun @[simp] theorem coe_mk (f : ℝ → ℝ) (h₁ : monotone f) (h₂ : ∀ (x : ℝ), f (x + 1) = f x + 1) : ⇑(mk f h₁ h₂) = f := rfl protected theorem monotone (f : circle_deg1_lift) : monotone ⇑f := monotone' f theorem mono (f : circle_deg1_lift) {x : ℝ} {y : ℝ} (h : x ≤ y) : coe_fn f x ≤ coe_fn f y := circle_deg1_lift.monotone f h theorem strict_mono_iff_injective (f : circle_deg1_lift) : strict_mono ⇑f ↔ function.injective ⇑f := monotone.strict_mono_iff_injective (circle_deg1_lift.monotone f) @[simp] theorem map_add_one (f : circle_deg1_lift) (x : ℝ) : coe_fn f (x + 1) = coe_fn f x + 1 := map_add_one' f @[simp] theorem map_one_add (f : circle_deg1_lift) (x : ℝ) : coe_fn f (1 + x) = 1 + coe_fn f x := sorry theorem coe_inj {f : circle_deg1_lift} {g : circle_deg1_lift} : ⇑f = ⇑g → f = g := sorry theorem ext {f : circle_deg1_lift} {g : circle_deg1_lift} (h : ∀ (x : ℝ), coe_fn f x = coe_fn g x) : f = g := coe_inj (funext h) theorem ext_iff {f : circle_deg1_lift} {g : circle_deg1_lift} : f = g ↔ ∀ (x : ℝ), coe_fn f x = coe_fn g x := { mp := fun (h : f = g) (x : ℝ) => h ▸ rfl, mpr := fun (h : ∀ (x : ℝ), coe_fn f x = coe_fn g x) => ext h } protected instance monoid : monoid circle_deg1_lift := monoid.mk (fun (f g : circle_deg1_lift) => mk (⇑f ∘ ⇑g) sorry sorry) sorry (mk id monotone_id sorry) sorry sorry protected instance inhabited : Inhabited circle_deg1_lift := { default := 1 } @[simp] theorem coe_mul (f : circle_deg1_lift) (g : circle_deg1_lift) : ⇑(f * g) = ⇑f ∘ ⇑g := rfl theorem mul_apply (f : circle_deg1_lift) (g : circle_deg1_lift) (x : ℝ) : coe_fn (f * g) x = coe_fn f (coe_fn g x) := rfl @[simp] theorem coe_one : ⇑1 = id := rfl protected instance units_has_coe_to_fun : has_coe_to_fun (units circle_deg1_lift) := has_coe_to_fun.mk (fun (_x : units circle_deg1_lift) => ℝ → ℝ) fun (f : units circle_deg1_lift) => ⇑↑f @[simp] theorem units_coe (f : units circle_deg1_lift) : ⇑↑f = ⇑f := rfl @[simp] theorem units_inv_apply_apply (f : units circle_deg1_lift) (x : ℝ) : coe_fn (f⁻¹) (coe_fn f x) = x := sorry @[simp] theorem units_apply_inv_apply (f : units circle_deg1_lift) (x : ℝ) : coe_fn f (coe_fn (f⁻¹) x) = x := sorry /-- If a lift of a circle map is bijective, then it is an order automorphism of the line. -/ def to_order_iso : units circle_deg1_lift →* ℝ ≃o ℝ := monoid_hom.mk (fun (f : units circle_deg1_lift) => rel_iso.mk (equiv.mk (⇑f) (⇑(f⁻¹)) (units_inv_apply_apply f) (units_apply_inv_apply f)) sorry) sorry sorry @[simp] theorem coe_to_order_iso (f : units circle_deg1_lift) : ⇑(coe_fn to_order_iso f) = ⇑f := rfl @[simp] theorem coe_to_order_iso_symm (f : units circle_deg1_lift) : ⇑(order_iso.symm (coe_fn to_order_iso f)) = ⇑(f⁻¹) := rfl @[simp] theorem coe_to_order_iso_inv (f : units circle_deg1_lift) : ⇑(coe_fn to_order_iso f⁻¹) = ⇑(f⁻¹) := rfl theorem is_unit_iff_bijective {f : circle_deg1_lift} : is_unit f ↔ function.bijective ⇑f := sorry theorem coe_pow (f : circle_deg1_lift) (n : ℕ) : ⇑(f ^ n) = nat.iterate (⇑f) n := sorry theorem semiconj_by_iff_semiconj {f : circle_deg1_lift} {g₁ : circle_deg1_lift} {g₂ : circle_deg1_lift} : semiconj_by f g₁ g₂ ↔ function.semiconj ⇑f ⇑g₁ ⇑g₂ := ext_iff theorem commute_iff_commute {f : circle_deg1_lift} {g : circle_deg1_lift} : commute f g ↔ function.commute ⇑f ⇑g := ext_iff /-! ### Translate by a constant -/ /-- The map `y ↦ x + y` as a `circle_deg1_lift`. More precisely, we define a homomorphism from `multiplicative ℝ` to `units circle_deg1_lift`, so the translation by `x` is `translation (multiplicative.of_add x)`. -/ def translate : multiplicative ℝ →* units circle_deg1_lift := monoid_hom.comp (units.map (monoid_hom.mk (fun (x : multiplicative ℝ) => mk (fun (y : ℝ) => coe_fn multiplicative.to_add x + y) sorry sorry) sorry sorry)) (mul_equiv.to_monoid_hom to_units) @[simp] theorem translate_apply (x : ℝ) (y : ℝ) : coe_fn (coe_fn translate (coe_fn multiplicative.of_add x)) y = x + y := rfl @[simp] theorem translate_inv_apply (x : ℝ) (y : ℝ) : coe_fn (coe_fn translate (coe_fn multiplicative.of_add x)⁻¹) y = -x + y := rfl @[simp] theorem translate_gpow (x : ℝ) (n : ℤ) : coe_fn translate (coe_fn multiplicative.of_add x) ^ n = coe_fn translate (coe_fn multiplicative.of_add (↑n * x)) := sorry @[simp] theorem translate_pow (x : ℝ) (n : ℕ) : coe_fn translate (coe_fn multiplicative.of_add x) ^ n = coe_fn translate (coe_fn multiplicative.of_add (↑n * x)) := translate_gpow x ↑n @[simp] theorem translate_iterate (x : ℝ) (n : ℕ) : nat.iterate (⇑(coe_fn translate (coe_fn multiplicative.of_add x))) n = ⇑(coe_fn translate (coe_fn multiplicative.of_add (↑n * x))) := sorry /-! ### Commutativity with integer translations In this section we prove that `f` commutes with translations by an integer number. First we formulate these statements (for a natural or an integer number, addition on the left or on the right, addition or subtraction) using `function.commute`, then reformulate as `simp` lemmas `map_int_add` etc. -/ theorem commute_nat_add (f : circle_deg1_lift) (n : ℕ) : function.commute (⇑f) (Add.add ↑n) := sorry theorem commute_add_nat (f : circle_deg1_lift) (n : ℕ) : function.commute ⇑f fun (x : ℝ) => x + ↑n := sorry theorem commute_sub_nat (f : circle_deg1_lift) (n : ℕ) : function.commute ⇑f fun (x : ℝ) => x - ↑n := sorry theorem commute_add_int (f : circle_deg1_lift) (n : ℤ) : function.commute ⇑f fun (x : ℝ) => x + ↑n := sorry theorem commute_int_add (f : circle_deg1_lift) (n : ℤ) : function.commute (⇑f) (Add.add ↑n) := sorry theorem commute_sub_int (f : circle_deg1_lift) (n : ℤ) : function.commute ⇑f fun (x : ℝ) => x - ↑n := sorry @[simp] theorem map_int_add (f : circle_deg1_lift) (m : ℤ) (x : ℝ) : coe_fn f (↑m + x) = ↑m + coe_fn f x := commute_int_add f m x @[simp] theorem map_add_int (f : circle_deg1_lift) (x : ℝ) (m : ℤ) : coe_fn f (x + ↑m) = coe_fn f x + ↑m := commute_add_int f m x @[simp] theorem map_sub_int (f : circle_deg1_lift) (x : ℝ) (n : ℤ) : coe_fn f (x - ↑n) = coe_fn f x - ↑n := commute_sub_int f n x @[simp] theorem map_add_nat (f : circle_deg1_lift) (x : ℝ) (n : ℕ) : coe_fn f (x + ↑n) = coe_fn f x + ↑n := map_add_int f x ↑n @[simp] theorem map_nat_add (f : circle_deg1_lift) (n : ℕ) (x : ℝ) : coe_fn f (↑n + x) = ↑n + coe_fn f x := map_int_add f (↑n) x @[simp] theorem map_sub_nat (f : circle_deg1_lift) (x : ℝ) (n : ℕ) : coe_fn f (x - ↑n) = coe_fn f x - ↑n := map_sub_int f x ↑n theorem map_int_of_map_zero (f : circle_deg1_lift) (n : ℤ) : coe_fn f ↑n = coe_fn f 0 + ↑n := eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn f ↑n = coe_fn f 0 + ↑n)) (Eq.symm (map_add_int f 0 n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn f ↑n = coe_fn f (0 + ↑n))) (zero_add ↑n))) (Eq.refl (coe_fn f ↑n))) @[simp] theorem map_fract_sub_fract_eq (f : circle_deg1_lift) (x : ℝ) : coe_fn f (fract x) - fract x = coe_fn f x - x := sorry /-! ### Pointwise order on circle maps -/ /-- Monotone circle maps form a lattice with respect to the pointwise order -/ protected instance lattice : lattice circle_deg1_lift := lattice.mk (fun (f g : circle_deg1_lift) => mk (fun (x : ℝ) => max (coe_fn f x) (coe_fn g x)) sorry sorry) (fun (f g : circle_deg1_lift) => ∀ (x : ℝ), coe_fn f x ≤ coe_fn g x) (semilattice_sup.lt._default fun (f g : circle_deg1_lift) => ∀ (x : ℝ), coe_fn f x ≤ coe_fn g x) sorry sorry sorry sorry sorry sorry (fun (f g : circle_deg1_lift) => mk (fun (x : ℝ) => min (coe_fn f x) (coe_fn g x)) sorry sorry) sorry sorry sorry @[simp] theorem sup_apply (f : circle_deg1_lift) (g : circle_deg1_lift) (x : ℝ) : coe_fn (f ⊔ g) x = max (coe_fn f x) (coe_fn g x) := rfl @[simp] theorem inf_apply (f : circle_deg1_lift) (g : circle_deg1_lift) (x : ℝ) : coe_fn (f ⊓ g) x = min (coe_fn f x) (coe_fn g x) := rfl theorem iterate_monotone (n : ℕ) : monotone fun (f : circle_deg1_lift) => nat.iterate (⇑f) n := fun (f g : circle_deg1_lift) (h : f ≤ g) => monotone.iterate_le_of_le (circle_deg1_lift.monotone f) h n theorem iterate_mono {f : circle_deg1_lift} {g : circle_deg1_lift} (h : f ≤ g) (n : ℕ) : nat.iterate (⇑f) n ≤ nat.iterate (⇑g) n := iterate_monotone n h theorem pow_mono {f : circle_deg1_lift} {g : circle_deg1_lift} (h : f ≤ g) (n : ℕ) : f ^ n ≤ g ^ n := sorry theorem pow_monotone (n : ℕ) : monotone fun (f : circle_deg1_lift) => f ^ n := fun (f g : circle_deg1_lift) (h : f ≤ g) => pow_mono h n /-! ### Estimates on `(f * g) 0` We prove the estimates `f 0 + ⌊g 0⌋ ≤ f (g 0) ≤ f 0 + ⌈g 0⌉` and some corollaries with added/removed floors and ceils. We also prove that for two semiconjugate maps `g₁`, `g₂`, the distance between `g₁ 0` and `g₂ 0` is less than two. -/ theorem map_le_of_map_zero (f : circle_deg1_lift) (x : ℝ) : coe_fn f x ≤ coe_fn f 0 + ↑(ceil x) := trans_rel_left LessEq (circle_deg1_lift.monotone f (le_ceil x)) (map_int_of_map_zero f (ceil x)) theorem map_map_zero_le (f : circle_deg1_lift) (g : circle_deg1_lift) : coe_fn f (coe_fn g 0) ≤ coe_fn f 0 + ↑(ceil (coe_fn g 0)) := map_le_of_map_zero f (coe_fn g 0) theorem floor_map_map_zero_le (f : circle_deg1_lift) (g : circle_deg1_lift) : floor (coe_fn f (coe_fn g 0)) ≤ floor (coe_fn f 0) + ceil (coe_fn g 0) := trans_rel_left LessEq (floor_mono (map_map_zero_le f g)) (floor_add_int (coe_fn f 0) (ceil (coe_fn g 0))) theorem ceil_map_map_zero_le (f : circle_deg1_lift) (g : circle_deg1_lift) : ceil (coe_fn f (coe_fn g 0)) ≤ ceil (coe_fn f 0) + ceil (coe_fn g 0) := trans_rel_left LessEq (ceil_mono (map_map_zero_le f g)) (ceil_add_int (coe_fn f 0) (ceil (coe_fn g 0))) theorem map_map_zero_lt (f : circle_deg1_lift) (g : circle_deg1_lift) : coe_fn f (coe_fn g 0) < coe_fn f 0 + coe_fn g 0 + 1 := trans_rel_left Less (lt_of_le_of_lt (map_map_zero_le f g) (add_lt_add_left (ceil_lt_add_one (coe_fn g 0)) (coe_fn f 0))) (Eq.symm (add_assoc (coe_fn f 0) (coe_fn g 0) 1)) theorem le_map_of_map_zero (f : circle_deg1_lift) (x : ℝ) : coe_fn f 0 + ↑(floor x) ≤ coe_fn f x := trans_rel_right LessEq (Eq.symm (map_int_of_map_zero f (floor x))) (circle_deg1_lift.monotone f (floor_le x)) theorem le_map_map_zero (f : circle_deg1_lift) (g : circle_deg1_lift) : coe_fn f 0 + ↑(floor (coe_fn g 0)) ≤ coe_fn f (coe_fn g 0) := le_map_of_map_zero f (coe_fn g 0) theorem le_floor_map_map_zero (f : circle_deg1_lift) (g : circle_deg1_lift) : floor (coe_fn f 0) + floor (coe_fn g 0) ≤ floor (coe_fn f (coe_fn g 0)) := trans_rel_right LessEq (Eq.symm (floor_add_int (coe_fn f 0) (floor (coe_fn g 0)))) (floor_mono (le_map_map_zero f g)) theorem le_ceil_map_map_zero (f : circle_deg1_lift) (g : circle_deg1_lift) : ceil (coe_fn f 0) + floor (coe_fn g 0) ≤ ceil (coe_fn (f * g) 0) := trans_rel_right LessEq (Eq.symm (ceil_add_int (coe_fn f 0) (floor (coe_fn g 0)))) (ceil_mono (le_map_map_zero f g)) theorem lt_map_map_zero (f : circle_deg1_lift) (g : circle_deg1_lift) : coe_fn f 0 + coe_fn g 0 - 1 < coe_fn f (coe_fn g 0) := sorry theorem dist_map_map_zero_lt (f : circle_deg1_lift) (g : circle_deg1_lift) : dist (coe_fn f 0 + coe_fn g 0) (coe_fn f (coe_fn g 0)) < 1 := sorry theorem dist_map_zero_lt_of_semiconj {f : circle_deg1_lift} {g₁ : circle_deg1_lift} {g₂ : circle_deg1_lift} (h : function.semiconj ⇑f ⇑g₁ ⇑g₂) : dist (coe_fn g₁ 0) (coe_fn g₂ 0) < bit0 1 := sorry theorem dist_map_zero_lt_of_semiconj_by {f : circle_deg1_lift} {g₁ : circle_deg1_lift} {g₂ : circle_deg1_lift} (h : semiconj_by f g₁ g₂) : dist (coe_fn g₁ 0) (coe_fn g₂ 0) < bit0 1 := dist_map_zero_lt_of_semiconj (iff.mp semiconj_by_iff_semiconj h) /-! ### Limits at infinities and continuity -/ protected theorem tendsto_at_bot (f : circle_deg1_lift) : filter.tendsto (⇑f) filter.at_bot filter.at_bot := sorry protected theorem tendsto_at_top (f : circle_deg1_lift) : filter.tendsto (⇑f) filter.at_top filter.at_top := sorry theorem continuous_iff_surjective (f : circle_deg1_lift) : continuous ⇑f ↔ function.surjective ⇑f := sorry /-! ### Estimates on `(f^n) x` If we know that `f x` is `≤`/`<`/`≥`/`>`/`=` to `x + m`, then we have a similar estimate on `f^[n] x` and `x + n * m`. For `≤`, `≥`, and `=` we formulate both `of` (implication) and `iff` versions because implications work for `n = 0`. For `<` and `>` we formulate only `iff` versions. -/ theorem iterate_le_of_map_le_add_int (f : circle_deg1_lift) {x : ℝ} {m : ℤ} (h : coe_fn f x ≤ x + ↑m) (n : ℕ) : nat.iterate (⇑f) n x ≤ x + ↑n * ↑m := sorry theorem le_iterate_of_add_int_le_map (f : circle_deg1_lift) {x : ℝ} {m : ℤ} (h : x + ↑m ≤ coe_fn f x) (n : ℕ) : x + ↑n * ↑m ≤ nat.iterate (⇑f) n x := sorry theorem iterate_eq_of_map_eq_add_int (f : circle_deg1_lift) {x : ℝ} {m : ℤ} (h : coe_fn f x = x + ↑m) (n : ℕ) : nat.iterate (⇑f) n x = x + ↑n * ↑m := sorry theorem iterate_pos_le_iff (f : circle_deg1_lift) {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : nat.iterate (⇑f) n x ≤ x + ↑n * ↑m ↔ coe_fn f x ≤ x + ↑m := sorry theorem iterate_pos_lt_iff (f : circle_deg1_lift) {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : nat.iterate (⇑f) n x < x + ↑n * ↑m ↔ coe_fn f x < x + ↑m := sorry theorem iterate_pos_eq_iff (f : circle_deg1_lift) {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : nat.iterate (⇑f) n x = x + ↑n * ↑m ↔ coe_fn f x = x + ↑m := sorry theorem le_iterate_pos_iff (f : circle_deg1_lift) {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : x + ↑n * ↑m ≤ nat.iterate (⇑f) n x ↔ x + ↑m ≤ coe_fn f x := sorry theorem lt_iterate_pos_iff (f : circle_deg1_lift) {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : x + ↑n * ↑m < nat.iterate (⇑f) n x ↔ x + ↑m < coe_fn f x := sorry theorem mul_floor_map_zero_le_floor_iterate_zero (f : circle_deg1_lift) (n : ℕ) : ↑n * floor (coe_fn f 0) ≤ floor (nat.iterate (⇑f) n 0) := sorry /-! ### Definition of translation number -/ /-- An auxiliary sequence used to define the translation number. -/ def transnum_aux_seq (f : circle_deg1_lift) (n : ℕ) : ℝ := coe_fn (f ^ bit0 1 ^ n) 0 / bit0 1 ^ n /-- The translation number of a `circle_deg1_lift`, $τ(f)=\lim_{n→∞}\frac{f^n(x)-x}{n}$. We use an auxiliary sequence `\frac{f^{2^n}(0)}{2^n}` to define `τ(f)` because some proofs are simpler this way. -/ def translation_number (f : circle_deg1_lift) : ℝ := lim filter.at_top (transnum_aux_seq f) -- TODO: choose two different symbols for `circle_deg1_lift.translation_number` and the future -- `circle_mono_homeo.rotation_number`, then make them `localized notation`s theorem transnum_aux_seq_def (f : circle_deg1_lift) : transnum_aux_seq f = fun (n : ℕ) => coe_fn (f ^ bit0 1 ^ n) 0 / bit0 1 ^ n := rfl theorem translation_number_eq_of_tendsto_aux (f : circle_deg1_lift) {τ' : ℝ} (h : filter.tendsto (transnum_aux_seq f) filter.at_top (nhds τ')) : translation_number f = τ' := filter.tendsto.lim_eq h theorem translation_number_eq_of_tendsto₀ (f : circle_deg1_lift) {τ' : ℝ} (h : filter.tendsto (fun (n : ℕ) => nat.iterate (⇑f) n 0 / ↑n) filter.at_top (nhds τ')) : translation_number f = τ' := sorry theorem translation_number_eq_of_tendsto₀' (f : circle_deg1_lift) {τ' : ℝ} (h : filter.tendsto (fun (n : ℕ) => nat.iterate (⇑f) (n + 1) 0 / (↑n + 1)) filter.at_top (nhds τ')) : translation_number f = τ' := translation_number_eq_of_tendsto₀ f (iff.mp (filter.tendsto_add_at_top_iff_nat 1) h) theorem transnum_aux_seq_zero (f : circle_deg1_lift) : transnum_aux_seq f 0 = coe_fn f 0 := sorry theorem transnum_aux_seq_dist_lt (f : circle_deg1_lift) (n : ℕ) : dist (transnum_aux_seq f n) (transnum_aux_seq f (n + 1)) < 1 / bit0 1 / bit0 1 ^ n := sorry theorem tendsto_translation_number_aux (f : circle_deg1_lift) : filter.tendsto (transnum_aux_seq f) filter.at_top (nhds (translation_number f)) := cauchy_seq.tendsto_lim (cauchy_seq_of_le_geometric_two 1 fun (n : ℕ) => le_of_lt (transnum_aux_seq_dist_lt f n)) theorem dist_map_zero_translation_number_le (f : circle_deg1_lift) : dist (coe_fn f 0) (translation_number f) ≤ 1 := transnum_aux_seq_zero f ▸ dist_le_of_le_geometric_two_of_tendsto₀ 1 (fun (n : ℕ) => le_of_lt (transnum_aux_seq_dist_lt f n)) (tendsto_translation_number_aux f) theorem tendsto_translation_number_of_dist_bounded_aux (f : circle_deg1_lift) (x : ℕ → ℝ) (C : ℝ) (H : ∀ (n : ℕ), dist (coe_fn (f ^ n) 0) (x n) ≤ C) : filter.tendsto (fun (n : ℕ) => x (bit0 1 ^ n) / bit0 1 ^ n) filter.at_top (nhds (translation_number f)) := sorry theorem translation_number_eq_of_dist_bounded {f : circle_deg1_lift} {g : circle_deg1_lift} (C : ℝ) (H : ∀ (n : ℕ), dist (coe_fn (f ^ n) 0) (coe_fn (g ^ n) 0) ≤ C) : translation_number f = translation_number g := Eq.symm (translation_number_eq_of_tendsto_aux g (tendsto_translation_number_of_dist_bounded_aux f (fun (n : ℕ) => coe_fn (g ^ n) 0) C H)) @[simp] theorem translation_number_one : translation_number 1 = 0 := sorry theorem translation_number_eq_of_semiconj_by {f : circle_deg1_lift} {g₁ : circle_deg1_lift} {g₂ : circle_deg1_lift} (H : semiconj_by f g₁ g₂) : translation_number g₁ = translation_number g₂ := translation_number_eq_of_dist_bounded (bit0 1) fun (n : ℕ) => le_of_lt (dist_map_zero_lt_of_semiconj_by (semiconj_by.pow_right H n)) theorem translation_number_eq_of_semiconj {f : circle_deg1_lift} {g₁ : circle_deg1_lift} {g₂ : circle_deg1_lift} (H : function.semiconj ⇑f ⇑g₁ ⇑g₂) : translation_number g₁ = translation_number g₂ := translation_number_eq_of_semiconj_by (iff.mpr semiconj_by_iff_semiconj H) theorem translation_number_mul_of_commute {f : circle_deg1_lift} {g : circle_deg1_lift} (h : commute f g) : translation_number (f * g) = translation_number f + translation_number g := sorry @[simp] theorem translation_number_units_inv (f : units circle_deg1_lift) : translation_number ↑(f⁻¹) = -translation_number ↑f := sorry @[simp] theorem translation_number_pow (f : circle_deg1_lift) (n : ℕ) : translation_number (f ^ n) = ↑n * translation_number f := sorry @[simp] theorem translation_number_gpow (f : units circle_deg1_lift) (n : ℤ) : translation_number ↑(f ^ n) = ↑n * translation_number ↑f := sorry @[simp] theorem translation_number_conj_eq (f : units circle_deg1_lift) (g : circle_deg1_lift) : translation_number (↑f * g * ↑(f⁻¹)) = translation_number g := Eq.symm (translation_number_eq_of_semiconj_by (units.mk_semiconj_by f g)) @[simp] theorem translation_number_conj_eq' (f : units circle_deg1_lift) (g : circle_deg1_lift) : translation_number (↑(f⁻¹) * g * ↑f) = translation_number g := translation_number_conj_eq (f⁻¹) g theorem dist_pow_map_zero_mul_translation_number_le (f : circle_deg1_lift) (n : ℕ) : dist (coe_fn (f ^ n) 0) (↑n * translation_number f) ≤ 1 := translation_number_pow f n ▸ dist_map_zero_translation_number_le (f ^ n) theorem tendsto_translation_number₀' (f : circle_deg1_lift) : filter.tendsto (fun (n : ℕ) => coe_fn (f ^ (n + 1)) 0 / (↑n + 1)) filter.at_top (nhds (translation_number f)) := sorry theorem tendsto_translation_number₀ (f : circle_deg1_lift) : filter.tendsto (fun (n : ℕ) => coe_fn (f ^ n) 0 / ↑n) filter.at_top (nhds (translation_number f)) := iff.mp (filter.tendsto_add_at_top_iff_nat 1) (tendsto_translation_number₀' f) /-- For any `x : ℝ` the sequence $\frac{f^n(x)-x}{n}$ tends to the translation number of `f`. In particular, this limit does not depend on `x`. -/ theorem tendsto_translation_number (f : circle_deg1_lift) (x : ℝ) : filter.tendsto (fun (n : ℕ) => (coe_fn (f ^ n) x - x) / ↑n) filter.at_top (nhds (translation_number f)) := sorry theorem tendsto_translation_number' (f : circle_deg1_lift) (x : ℝ) : filter.tendsto (fun (n : ℕ) => (coe_fn (f ^ (n + 1)) x - x) / (↑n + 1)) filter.at_top (nhds (translation_number f)) := iff.mpr (filter.tendsto_add_at_top_iff_nat 1) (tendsto_translation_number f x) theorem translation_number_mono : monotone translation_number := fun (f g : circle_deg1_lift) (h : f ≤ g) => le_of_tendsto_of_tendsto' (tendsto_translation_number₀ f) (tendsto_translation_number₀ g) fun (n : ℕ) => div_le_div_of_le_of_nonneg (pow_mono h n 0) (nat.cast_nonneg n) theorem translation_number_translate (x : ℝ) : translation_number ↑(coe_fn translate (coe_fn multiplicative.of_add x)) = x := sorry theorem translation_number_le_of_le_add (f : circle_deg1_lift) {z : ℝ} (hz : ∀ (x : ℝ), coe_fn f x ≤ x + z) : translation_number f ≤ z := translation_number_translate z ▸ translation_number_mono fun (x : ℝ) => trans_rel_left LessEq (hz x) (add_comm x z) theorem le_translation_number_of_add_le (f : circle_deg1_lift) {z : ℝ} (hz : ∀ (x : ℝ), x + z ≤ coe_fn f x) : z ≤ translation_number f := sorry theorem translation_number_le_of_le_add_int (f : circle_deg1_lift) {x : ℝ} {m : ℤ} (h : coe_fn f x ≤ x + ↑m) : translation_number f ≤ ↑m := sorry theorem translation_number_le_of_le_add_nat (f : circle_deg1_lift) {x : ℝ} {m : ℕ} (h : coe_fn f x ≤ x + ↑m) : translation_number f ≤ ↑m := translation_number_le_of_le_add_int f h theorem le_translation_number_of_add_int_le (f : circle_deg1_lift) {x : ℝ} {m : ℤ} (h : x + ↑m ≤ coe_fn f x) : ↑m ≤ translation_number f := sorry theorem le_translation_number_of_add_nat_le (f : circle_deg1_lift) {x : ℝ} {m : ℕ} (h : x + ↑m ≤ coe_fn f x) : ↑m ≤ translation_number f := le_translation_number_of_add_int_le f h /-- If `f x - x` is an integer number `m` for some point `x`, then `τ f = m`. On the circle this means that a map with a fixed point has rotation number zero. -/ theorem translation_number_of_eq_add_int (f : circle_deg1_lift) {x : ℝ} {m : ℤ} (h : coe_fn f x = x + ↑m) : translation_number f = ↑m := le_antisymm (translation_number_le_of_le_add_int f (le_of_eq h)) (le_translation_number_of_add_int_le f (le_of_eq (Eq.symm h))) theorem floor_sub_le_translation_number (f : circle_deg1_lift) (x : ℝ) : ↑(floor (coe_fn f x - x)) ≤ translation_number f := le_translation_number_of_add_int_le f (iff.mp le_sub_iff_add_le' (floor_le (coe_fn f x - x))) theorem translation_number_le_ceil_sub (f : circle_deg1_lift) (x : ℝ) : translation_number f ≤ ↑(ceil (coe_fn f x - x)) := translation_number_le_of_le_add_int f (iff.mp sub_le_iff_le_add' (le_ceil (coe_fn f x - x))) theorem map_lt_of_translation_number_lt_int (f : circle_deg1_lift) {n : ℤ} (h : translation_number f < ↑n) (x : ℝ) : coe_fn f x < x + ↑n := iff.mp not_le (mt (le_translation_number_of_add_int_le f) (iff.mpr not_le h)) theorem map_lt_of_translation_number_lt_nat (f : circle_deg1_lift) {n : ℕ} (h : translation_number f < ↑n) (x : ℝ) : coe_fn f x < x + ↑n := map_lt_of_translation_number_lt_int f h x theorem map_lt_add_floor_translation_number_add_one (f : circle_deg1_lift) (x : ℝ) : coe_fn f x < x + ↑(floor (translation_number f)) + 1 := sorry theorem map_lt_add_translation_number_add_one (f : circle_deg1_lift) (x : ℝ) : coe_fn f x < x + translation_number f + 1 := lt_of_lt_of_le (map_lt_add_floor_translation_number_add_one f x) (add_le_add (id (add_le_add (le_refl x) (id (floor_le (translation_number f))))) (le_refl 1)) theorem lt_map_of_int_lt_translation_number (f : circle_deg1_lift) {n : ℤ} (h : ↑n < translation_number f) (x : ℝ) : x + ↑n < coe_fn f x := iff.mp not_le (mt (translation_number_le_of_le_add_int f) (iff.mpr not_le h)) theorem lt_map_of_nat_lt_translation_number (f : circle_deg1_lift) {n : ℕ} (h : ↑n < translation_number f) (x : ℝ) : x + ↑n < coe_fn f x := lt_map_of_int_lt_translation_number f h x /-- If `f^n x - x`, `n > 0`, is an integer number `m` for some point `x`, then `τ f = m / n`. On the circle this means that a map with a periodic orbit has a rational rotation number. -/ theorem translation_number_of_map_pow_eq_add_int (f : circle_deg1_lift) {x : ℝ} {n : ℕ} {m : ℤ} (h : coe_fn (f ^ n) x = x + ↑m) (hn : 0 < n) : translation_number f = ↑m / ↑n := sorry /-- If a predicate depends only on `f x - x` and holds for all `0 ≤ x ≤ 1`, then it holds for all `x`. -/ theorem forall_map_sub_of_Icc (f : circle_deg1_lift) (P : ℝ → Prop) (h : ∀ (x : ℝ), x ∈ set.Icc 0 1 → P (coe_fn f x - x)) (x : ℝ) : P (coe_fn f x - x) := map_fract_sub_fract_eq f x ▸ h (fract x) { left := fract_nonneg x, right := le_of_lt (fract_lt_one x) } theorem translation_number_lt_of_forall_lt_add (f : circle_deg1_lift) (hf : continuous ⇑f) {z : ℝ} (hz : ∀ (x : ℝ), coe_fn f x < x + z) : translation_number f < z := sorry theorem lt_translation_number_of_forall_add_lt (f : circle_deg1_lift) (hf : continuous ⇑f) {z : ℝ} (hz : ∀ (x : ℝ), x + z < coe_fn f x) : z < translation_number f := sorry /-- If `f` is a continuous monotone map `ℝ → ℝ`, `f (x + 1) = f x + 1`, then there exists `x` such that `f x = x + τ f`. -/ theorem exists_eq_add_translation_number (f : circle_deg1_lift) (hf : continuous ⇑f) : ∃ (x : ℝ), coe_fn f x = x + translation_number f := sorry theorem translation_number_eq_int_iff (f : circle_deg1_lift) (hf : continuous ⇑f) {m : ℤ} : translation_number f = ↑m ↔ ∃ (x : ℝ), coe_fn f x = x + ↑m := sorry theorem continuous_pow (f : circle_deg1_lift) (hf : continuous ⇑f) (n : ℕ) : continuous ⇑(f ^ n) := eq.mpr (id (Eq._oldrec (Eq.refl (continuous ⇑(f ^ n))) (coe_pow f n))) (continuous.iterate hf n) theorem translation_number_eq_rat_iff (f : circle_deg1_lift) (hf : continuous ⇑f) {m : ℤ} {n : ℕ} (hn : 0 < n) : translation_number f = ↑m / ↑n ↔ ∃ (x : ℝ), coe_fn (f ^ n) x = x + ↑m := sorry /-- Consider two actions `f₁ f₂ : G →* circle_deg1_lift` of a group on the real line by lifts of orientation preserving circle homeomorphisms. Suppose that for each `g : G` the homeomorphisms `f₁ g` and `f₂ g` have equal rotation numbers. Then there exists `F : circle_deg1_lift` such that `F * f₁ g = f₂ g * F` for all `g : G`. This is a version of Proposition 5.4 from [Étienne Ghys, Groupes d'homeomorphismes du cercle et cohomologie bornee][ghys87:groupes]. -/ theorem semiconj_of_group_action_of_forall_translation_number_eq {G : Type u_1} [group G] (f₁ : G →* circle_deg1_lift) (f₂ : G →* circle_deg1_lift) (h : ∀ (g : G), translation_number (coe_fn f₁ g) = translation_number (coe_fn f₂ g)) : ∃ (F : circle_deg1_lift), ∀ (g : G), function.semiconj ⇑F ⇑(coe_fn f₁ g) ⇑(coe_fn f₂ g) := sorry /-- If two lifts of circle homeomorphisms have the same translation number, then they are semiconjugate by a `circle_deg1_lift`. This version uses arguments `f₁ f₂ : units circle_deg1_lift` to assume that `f₁` and `f₂` are homeomorphisms. -/ theorem units_semiconj_of_translation_number_eq {f₁ : units circle_deg1_lift} {f₂ : units circle_deg1_lift} (h : translation_number ↑f₁ = translation_number ↑f₂) : ∃ (F : circle_deg1_lift), function.semiconj ⇑F ⇑f₁ ⇑f₂ := sorry /-- If two lifts of circle homeomorphisms have the same translation number, then they are semiconjugate by a `circle_deg1_lift`. This version uses assumptions `is_unit f₁` and `is_unit f₂` to assume that `f₁` and `f₂` are homeomorphisms. -/ theorem semiconj_of_is_unit_of_translation_number_eq {f₁ : circle_deg1_lift} {f₂ : circle_deg1_lift} (h₁ : is_unit f₁) (h₂ : is_unit f₂) (h : translation_number f₁ = translation_number f₂) : ∃ (F : circle_deg1_lift), function.semiconj ⇑F ⇑f₁ ⇑f₂ := sorry /-- If two lifts of circle homeomorphisms have the same translation number, then they are semiconjugate by a `circle_deg1_lift`. This version uses assumptions `bijective f₁` and `bijective f₂` to assume that `f₁` and `f₂` are homeomorphisms. -/ theorem semiconj_of_bijective_of_translation_number_eq {f₁ : circle_deg1_lift} {f₂ : circle_deg1_lift} (h₁ : function.bijective ⇑f₁) (h₂ : function.bijective ⇑f₂) (h : translation_number f₁ = translation_number f₂) : ∃ (F : circle_deg1_lift), function.semiconj ⇑F ⇑f₁ ⇑f₂ := semiconj_of_is_unit_of_translation_number_eq (iff.mpr is_unit_iff_bijective h₁) (iff.mpr is_unit_iff_bijective h₂) h end Mathlib
15e2983451411c605edbce3aa6899e2fb7c41a37
130c49f47783503e462c16b2eff31933442be6ff
/src/Lean/PrettyPrinter/Parenthesizer.lean
f59de8e8e99cf03dcc088c5494d9f1cd041954e2
[ "Apache-2.0" ]
permissive
Hazel-Brown/lean4
8aa5860e282435ffc30dcdfccd34006c59d1d39c
79e6732fc6bbf5af831b76f310f9c488d44e7a16
refs/heads/master
1,689,218,208,951
1,629,736,869,000
1,629,736,896,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
29,581
lean
/- Copyright (c) 2020 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Lean.CoreM import Lean.KeyedDeclsAttribute import Lean.Parser.Extension import Lean.ParserCompiler.Attribute import Lean.PrettyPrinter.Basic /-! The parenthesizer inserts parentheses into a `Syntax` object where syntactically necessary, usually as an intermediary step between the delaborator and the formatter. While the delaborator outputs structurally well-formed syntax trees that can be re-elaborated without post-processing, this tree structure is lost in the formatter and thus needs to be preserved by proper insertion of parentheses. # The abstract problem & solution The Lean 4 grammar is unstructured and extensible with arbitrary new parsers, so in general it is undecidable whether parentheses are necessary or even allowed at any point in the syntax tree. Parentheses for different categories, e.g. terms and levels, might not even have the same structure. In this module, we focus on the correct parenthesization of parsers defined via `Lean.Parser.prattParser`, which includes both aforementioned built-in categories. Custom parenthesizers can be added for new node kinds, but the data collected in the implementation below might not be appropriate for other parenthesization strategies. Usages of a parser defined via `prattParser` in general have the form `p prec`, where `prec` is the minimum precedence or binding power. Recall that a Pratt parser greedily runs a leading parser with precedence at least `prec` (otherwise it fails) followed by zero or more trailing parsers with precedence at least `prec`; the precedence of a parser is encoded in the call to `leadingNode/trailingNode`, respectively. Thus we should parenthesize a syntax node `stx` supposedly produced by `p prec` if 1. the leading/any trailing parser involved in `stx` has precedence < `prec` (because without parentheses, `p prec` would not produce all of `stx`), or 2. the trailing parser parsing the input to *the right of* `stx`, if any, has precedence >= `prec` (because without parentheses, `p prec` would have parsed it as well and made it a part of `stx`). We also check that the two parsers are from the same syntax category. Note that in case 2, it is also sufficient to parenthesize a *parent* node as long as the offending parser is still to the right of that node. For example, imagine the tree structure of `(f $ fun x => x) y` without parentheses. We need to insert *some* parentheses between `x` and `y` since the lambda body is parsed with precedence 0, while the identifier parser for `y` has precedence `maxPrec`. But we need to parenthesize the `$` node anyway since the precedence of its RHS (0) again is smaller than that of `y`. So it's better to only parenthesize the outer node than ending up with `(f $ (fun x => x)) y`. # Implementation We transform the syntax tree and collect the necessary precedence information for that in a single traversal. The traversal is right-to-left to cover case 2. More specifically, for every Pratt parser call, we store as monadic state the precedence of the left-most trailing parser and the minimum precedence of any parser (`contPrec`/`minPrec`) in this call, if any, and the precedence of the nested trailing Pratt parser call (`trailPrec`), if any. If `stP` is the state resulting from the traversal of a Pratt parser call `p prec`, and `st` is the state of the surrounding call, we parenthesize if `prec > stP.minPrec` (case 1) or if `stP.trailPrec <= st.contPrec` (case 2). The traversal can be customized for each `[*Parser]` parser declaration `c` (more specifically, each `SyntaxNodeKind` `c`) using the `[parenthesizer c]` attribute. Otherwise, a default parenthesizer will be synthesized from the used parser combinators by recursively replacing them with declarations tagged as `[combinatorParenthesizer]` for the respective combinator. If a called function does not have a registered combinator parenthesizer and is not reducible, the synthesizer fails. This happens mostly at the `Parser.mk` decl, which is irreducible, when some parser primitive has not been handled yet. The traversal over the `Syntax` object is complicated by the fact that a parser does not produce exactly one syntax node, but an arbitrary (but constant, for each parser) amount that it pushes on top of the parser stack. This amount can even be zero for parsers such as `checkWsBefore`. Thus we cannot simply pass and return a `Syntax` object to and from `visit`. Instead, we use a `Syntax.Traverser` that allows arbitrary movement and modification inside the syntax tree. Our traversal invariant is that a parser interpreter should stop at the syntax object to the *left* of all syntax objects its parser produced, except when it is already at the left-most child. This special case is not an issue in practice since if there is another parser to the left that produced zero nodes in this case, it should always do so, so there is no danger of the left-most child being processed multiple times. Ultimately, most parenthesizers are implemented via three primitives that do all the actual syntax traversal: `maybeParenthesize mkParen prec x` runs `x` and afterwards transforms it with `mkParen` if the above condition for `p prec` is fulfilled. `visitToken` advances to the preceding sibling and is used on atoms. `visitArgs x` executes `x` on the last child of the current node and then advances to the preceding sibling (of the original current node). -/ namespace Lean namespace PrettyPrinter namespace Parenthesizer structure Context where -- We need to store this `categoryParser` argument to deal with the implicit Pratt parser call in `trailingNode.parenthesizer`. cat : Name := Name.anonymous structure State where stxTrav : Syntax.Traverser --- precedence and category of the current left-most trailing parser, if any; see module doc for details contPrec : Option Nat := none contCat : Name := Name.anonymous -- current minimum precedence in this Pratt parser call, if any; see module doc for details minPrec : Option Nat := none -- precedence and category of the trailing Pratt parser call if any; see module doc for details trailPrec : Option Nat := none trailCat : Name := Name.anonymous -- true iff we have already visited a token on this parser level; used for detecting trailing parsers visitedToken : Bool := false end Parenthesizer abbrev ParenthesizerM := ReaderT Parenthesizer.Context $ StateRefT Parenthesizer.State CoreM abbrev Parenthesizer := ParenthesizerM Unit @[inline] def ParenthesizerM.orelse {α} (p₁ p₂ : ParenthesizerM α) : ParenthesizerM α := do let s ← get catchInternalId backtrackExceptionId p₁ (fun _ => do set s; p₂) instance {α} : OrElse (ParenthesizerM α) := ⟨ParenthesizerM.orelse⟩ unsafe def mkParenthesizerAttribute : IO (KeyedDeclsAttribute Parenthesizer) := KeyedDeclsAttribute.init { builtinName := `builtinParenthesizer, name := `parenthesizer, descr := "Register a parenthesizer for a parser. [parenthesizer k] registers a declaration of type `Lean.PrettyPrinter.Parenthesizer` for the `SyntaxNodeKind` `k`.", valueTypeName := `Lean.PrettyPrinter.Parenthesizer, evalKey := fun builtin stx => do let env ← getEnv let id ← Attribute.Builtin.getId stx -- `isValidSyntaxNodeKind` is updated only in the next stage for new `[builtin*Parser]`s, but we try to -- synthesize a parenthesizer for it immediately, so we just check for a declaration in this case if (builtin && (env.find? id).isSome) || Parser.isValidSyntaxNodeKind env id then pure id else throwError "invalid [parenthesizer] argument, unknown syntax kind '{id}'" } `Lean.PrettyPrinter.parenthesizerAttribute @[builtinInit mkParenthesizerAttribute] constant parenthesizerAttribute : KeyedDeclsAttribute Parenthesizer abbrev CategoryParenthesizer := forall (prec : Nat), Parenthesizer unsafe def mkCategoryParenthesizerAttribute : IO (KeyedDeclsAttribute CategoryParenthesizer) := KeyedDeclsAttribute.init { builtinName := `builtinCategoryParenthesizer, name := `categoryParenthesizer, descr := "Register a parenthesizer for a syntax category. [categoryParenthesizer cat] registers a declaration of type `Lean.PrettyPrinter.CategoryParenthesizer` for the category `cat`, which is used when parenthesizing calls of `categoryParser cat prec`. Implementations should call `maybeParenthesize` with the precedence and `cat`. If no category parenthesizer is registered, the category will never be parenthesized, but still be traversed for parenthesizing nested categories.", valueTypeName := `Lean.PrettyPrinter.CategoryParenthesizer, evalKey := fun _ stx => do let env ← getEnv let id ← Attribute.Builtin.getId stx if Parser.isParserCategory env id then pure id else throwError "invalid [categoryParenthesizer] argument, unknown parser category '{toString id}'" } `Lean.PrettyPrinter.categoryParenthesizerAttribute @[builtinInit mkCategoryParenthesizerAttribute] constant categoryParenthesizerAttribute : KeyedDeclsAttribute CategoryParenthesizer unsafe def mkCombinatorParenthesizerAttribute : IO ParserCompiler.CombinatorAttribute := ParserCompiler.registerCombinatorAttribute `combinatorParenthesizer "Register a parenthesizer for a parser combinator. [combinatorParenthesizer c] registers a declaration of type `Lean.PrettyPrinter.Parenthesizer` for the `Parser` declaration `c`. Note that, unlike with [parenthesizer], this is not a node kind since combinators usually do not introduce their own node kinds. The tagged declaration may optionally accept parameters corresponding to (a prefix of) those of `c`, where `Parser` is replaced with `Parenthesizer` in the parameter types." @[builtinInit mkCombinatorParenthesizerAttribute] constant combinatorParenthesizerAttribute : ParserCompiler.CombinatorAttribute namespace Parenthesizer open Lean.Core open Std.Format def throwBacktrack {α} : ParenthesizerM α := throw $ Exception.internal backtrackExceptionId instance : Syntax.MonadTraverser ParenthesizerM := ⟨{ get := State.stxTrav <$> get, set := fun t => modify (fun st => { st with stxTrav := t }), modifyGet := fun f => modifyGet (fun st => let (a, t) := f st.stxTrav; (a, { st with stxTrav := t })) }⟩ open Syntax.MonadTraverser def addPrecCheck (prec : Nat) : ParenthesizerM Unit := modify fun st => { st with contPrec := Nat.min (st.contPrec.getD prec) prec, minPrec := Nat.min (st.minPrec.getD prec) prec } /-- Execute `x` at the right-most child of the current node, if any, then advance to the left. -/ def visitArgs (x : ParenthesizerM Unit) : ParenthesizerM Unit := do let stx ← getCur if stx.getArgs.size > 0 then goDown (stx.getArgs.size - 1) *> x <* goUp goLeft -- Macro scopes in the parenthesizer output are ultimately ignored by the pretty printer, -- so give a trivial implementation. instance : MonadQuotation ParenthesizerM := { getCurrMacroScope := pure arbitrary, getMainModule := pure arbitrary, withFreshMacroScope := fun x => x, } /-- Run `x` and parenthesize the result using `mkParen` if necessary. If `canJuxtapose` is false, we assume the category does not have a token-less juxtaposition syntax a la function application and deactivate rule 2. -/ def maybeParenthesize (cat : Name) (canJuxtapose : Bool) (mkParen : Syntax → Syntax) (prec : Nat) (x : ParenthesizerM Unit) : ParenthesizerM Unit := do let stx ← getCur let idx ← getIdx let st ← get -- reset precs for the recursive call set { stxTrav := st.stxTrav : State } trace[PrettyPrinter.parenthesize] "parenthesizing (cont := {(st.contPrec, st.contCat)}){indentD (format stx)}" x let { minPrec := some minPrec, trailPrec := trailPrec, trailCat := trailCat, .. } ← get | trace[PrettyPrinter.parenthesize] "visited a syntax tree without precedences?!{line ++ format stx}" trace[PrettyPrinter.parenthesize] (m!"...precedences are {prec} >? {minPrec}" ++ if canJuxtapose then m!", {(trailPrec, trailCat)} <=? {(st.contPrec, st.contCat)}" else "") -- Should we parenthesize? if (prec > minPrec || canJuxtapose && match trailPrec, st.contPrec with | some trailPrec, some contPrec => trailCat == st.contCat && trailPrec <= contPrec | _, _ => false) then -- The recursive `visit` call, by the invariant, has moved to the preceding node. In order to parenthesize -- the original node, we must first move to the right, except if we already were at the left-most child in the first -- place. if idx > 0 then goRight let mut stx ← getCur -- Move leading/trailing whitespace of `stx` outside of parentheses if let SourceInfo.original _ pos trail endPos := stx.getHeadInfo then stx := stx.setHeadInfo (SourceInfo.original "".toSubstring pos trail endPos) if let SourceInfo.original lead pos _ endPos := stx.getTailInfo then stx := stx.setTailInfo (SourceInfo.original lead pos "".toSubstring endPos) let mut stx' := mkParen stx if let SourceInfo.original lead pos _ endPos := stx.getHeadInfo then stx' := stx'.setHeadInfo (SourceInfo.original lead pos "".toSubstring endPos) if let SourceInfo.original _ pos trail endPos := stx.getTailInfo then stx' := stx'.setTailInfo (SourceInfo.original "".toSubstring pos trail endPos) trace[PrettyPrinter.parenthesize] "parenthesized: {stx'.formatStx none}" setCur stx' goLeft -- after parenthesization, there is no more trailing parser modify (fun st => { st with contPrec := Parser.maxPrec, contCat := cat, trailPrec := none }) let { trailPrec := trailPrec, .. } ← get -- If we already had a token at this level, keep the trailing parser. Otherwise, use the minimum of -- `prec` and `trailPrec`. if st.visitedToken then modify fun stP => { stP with trailPrec := st.trailPrec, trailCat := st.trailCat } else let trailPrec := match trailPrec with | some trailPrec => Nat.min trailPrec prec | _ => prec modify fun stP => { stP with trailPrec := trailPrec, trailCat := cat } modify fun stP => { stP with minPrec := st.minPrec } /-- Adjust state and advance. -/ def visitToken : Parenthesizer := do modify fun st => { st with contPrec := none, contCat := Name.anonymous, visitedToken := true } goLeft @[combinatorParenthesizer Lean.Parser.orelse] def orelse.parenthesizer (p1 p2 : Parenthesizer) : Parenthesizer := do let st ← get -- HACK: We have no (immediate) information on which side of the orelse could have produced the current node, so try -- them in turn. Uses the syntax traverser non-linearly! p1 <|> p2 -- `mkAntiquot` is quite complex, so we'd rather have its parenthesizer synthesized below the actual parser definition. -- Note that there is a mutual recursion -- `categoryParser -> mkAntiquot -> termParser -> categoryParser`, so we need to introduce an indirection somewhere -- anyway. @[extern 8 "lean_mk_antiquot_parenthesizer"] constant mkAntiquot.parenthesizer' (name : String) (kind : Option SyntaxNodeKind) (anonymous := true) : Parenthesizer @[inline] def liftCoreM {α} (x : CoreM α) : ParenthesizerM α := liftM x -- break up big mutual recursion @[extern "lean_pretty_printer_parenthesizer_interpret_parser_descr"] constant interpretParserDescr' : ParserDescr → CoreM Parenthesizer unsafe def parenthesizerForKindUnsafe (k : SyntaxNodeKind) : Parenthesizer := do if k == `missing then pure () else let p ← runForNodeKind parenthesizerAttribute k interpretParserDescr' p @[implementedBy parenthesizerForKindUnsafe] constant parenthesizerForKind (k : SyntaxNodeKind) : Parenthesizer @[combinatorParenthesizer Lean.Parser.withAntiquot] def withAntiquot.parenthesizer (antiP p : Parenthesizer) : Parenthesizer := -- TODO: could be optimized using `isAntiquot` (which would have to be moved), but I'd rather -- fix the backtracking hack outright. orelse.parenthesizer antiP p @[combinatorParenthesizer Lean.Parser.withAntiquotSuffixSplice] def withAntiquotSuffixSplice.parenthesizer (k : SyntaxNodeKind) (p suffix : Parenthesizer) : Parenthesizer := do if (← getCur).isAntiquotSuffixSplice then visitArgs <| suffix *> p else p @[combinatorParenthesizer Lean.Parser.tokenWithAntiquot] def tokenWithAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do if (← getCur).isTokenAntiquot then visitArgs p else p def parenthesizeCategoryCore (cat : Name) (prec : Nat) : Parenthesizer := withReader (fun ctx => { ctx with cat := cat }) do let stx ← getCur if stx.getKind == `choice then visitArgs $ stx.getArgs.size.forM fun _ => do let stx ← getCur parenthesizerForKind stx.getKind else withAntiquot.parenthesizer (mkAntiquot.parenthesizer' cat.toString none) (parenthesizerForKind stx.getKind) modify fun st => { st with contCat := cat } @[combinatorParenthesizer Lean.Parser.categoryParser] def categoryParser.parenthesizer (cat : Name) (prec : Nat) : Parenthesizer := do let env ← getEnv match categoryParenthesizerAttribute.getValues env cat with | p::_ => p prec -- Fall back to the generic parenthesizer. -- In this case this node will never be parenthesized since we don't know which parentheses to use. | _ => parenthesizeCategoryCore cat prec @[combinatorParenthesizer Lean.Parser.categoryParserOfStack] def categoryParserOfStack.parenthesizer (offset : Nat) (prec : Nat) : Parenthesizer := do let st ← get let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset) categoryParser.parenthesizer stx.getId prec @[combinatorParenthesizer Lean.Parser.parserOfStack] def parserOfStack.parenthesizer (offset : Nat) (prec : Nat := 0) : Parenthesizer := do let st ← get let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset) parenthesizerForKind stx.getKind @[builtinCategoryParenthesizer term] def term.parenthesizer : CategoryParenthesizer | prec => do let stx ← getCur -- this can happen at `termParser <|> many1 commandParser` in `Term.stxQuot` if stx.getKind == nullKind then throwBacktrack else do maybeParenthesize `term true (fun stx => Unhygienic.run `(($stx))) prec $ parenthesizeCategoryCore `term prec @[builtinCategoryParenthesizer tactic] def tactic.parenthesizer : CategoryParenthesizer | prec => do maybeParenthesize `tactic false (fun stx => Unhygienic.run `(tactic|($stx))) prec $ parenthesizeCategoryCore `tactic prec @[builtinCategoryParenthesizer level] def level.parenthesizer : CategoryParenthesizer | prec => do maybeParenthesize `level false (fun stx => Unhygienic.run `(level|($stx))) prec $ parenthesizeCategoryCore `level prec @[combinatorParenthesizer Lean.Parser.error] def error.parenthesizer (msg : String) : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.errorAtSavedPos] def errorAtSavedPos.parenthesizer (msg : String) (delta : Bool) : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.atomic] def atomic.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.lookahead] def lookahead.parenthesizer (p : Parenthesizer) : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.notFollowedBy] def notFollowedBy.parenthesizer (p : Parenthesizer) : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.andthen] def andthen.parenthesizer (p1 p2 : Parenthesizer) : Parenthesizer := p2 *> p1 @[combinatorParenthesizer Lean.Parser.node] def node.parenthesizer (k : SyntaxNodeKind) (p : Parenthesizer) : Parenthesizer := do let stx ← getCur if k != stx.getKind then trace[PrettyPrinter.parenthesize.backtrack] "unexpected node kind '{stx.getKind}', expected '{k}'" -- HACK; see `orelse.parenthesizer` throwBacktrack visitArgs p @[combinatorParenthesizer Lean.Parser.checkPrec] def checkPrec.parenthesizer (prec : Nat) : Parenthesizer := addPrecCheck prec @[combinatorParenthesizer Lean.Parser.leadingNode] def leadingNode.parenthesizer (k : SyntaxNodeKind) (prec : Nat) (p : Parenthesizer) : Parenthesizer := do node.parenthesizer k p addPrecCheck prec -- Limit `cont` precedence to `maxPrec-1`. -- This is because `maxPrec-1` is the precedence of function application, which is the only way to turn a leading parser -- into a trailing one. modify fun st => { st with contPrec := Nat.min (Parser.maxPrec-1) prec } @[combinatorParenthesizer Lean.Parser.trailingNode] def trailingNode.parenthesizer (k : SyntaxNodeKind) (prec lhsPrec : Nat) (p : Parenthesizer) : Parenthesizer := do let stx ← getCur if k != stx.getKind then trace[PrettyPrinter.parenthesize.backtrack] "unexpected node kind '{stx.getKind}', expected '{k}'" -- HACK; see `orelse.parenthesizer` throwBacktrack visitArgs do p addPrecCheck prec let ctx ← read modify fun st => { st with contCat := ctx.cat } -- After visiting the nodes actually produced by the parser passed to `trailingNode`, we are positioned on the -- left-most child, which is the term injected by `trailingNode` in place of the recursion. Left recursion is not an -- issue for the parenthesizer, so we can think of this child being produced by `termParser lhsPrec`, or whichever Pratt -- parser is calling us. categoryParser.parenthesizer ctx.cat lhsPrec @[combinatorParenthesizer Lean.Parser.rawCh] def rawCh.parenthesizer (ch : Char) := visitToken @[combinatorParenthesizer Lean.Parser.symbolNoAntiquot] def symbolNoAntiquot.parenthesizer (sym : String) := visitToken @[combinatorParenthesizer Lean.Parser.unicodeSymbolNoAntiquot] def unicodeSymbolNoAntiquot.parenthesizer (sym asciiSym : String) := visitToken @[combinatorParenthesizer Lean.Parser.identNoAntiquot] def identNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.rawIdentNoAntiquot] def rawIdentNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.identEq] def identEq.parenthesizer (id : Name) := visitToken @[combinatorParenthesizer Lean.Parser.nonReservedSymbolNoAntiquot] def nonReservedSymbolNoAntiquot.parenthesizer (sym : String) (includeIdent : Bool) := visitToken @[combinatorParenthesizer Lean.Parser.charLitNoAntiquot] def charLitNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.strLitNoAntiquot] def strLitNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.nameLitNoAntiquot] def nameLitNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.numLitNoAntiquot] def numLitNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.scientificLitNoAntiquot] def scientificLitNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.fieldIdx] def fieldIdx.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.manyNoAntiquot] def manyNoAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do let stx ← getCur visitArgs $ stx.getArgs.size.forM fun _ => p @[combinatorParenthesizer Lean.Parser.many1NoAntiquot] def many1NoAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do manyNoAntiquot.parenthesizer p @[combinatorParenthesizer Lean.Parser.many1Unbox] def many1Unbox.parenthesizer (p : Parenthesizer) : Parenthesizer := do let stx ← getCur if stx.getKind == nullKind then manyNoAntiquot.parenthesizer p else p @[combinatorParenthesizer Lean.Parser.optionalNoAntiquot] def optionalNoAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do visitArgs p @[combinatorParenthesizer Lean.Parser.sepByNoAntiquot] def sepByNoAntiquot.parenthesizer (p pSep : Parenthesizer) : Parenthesizer := do let stx ← getCur visitArgs $ (List.range stx.getArgs.size).reverse.forM $ fun i => if i % 2 == 0 then p else pSep @[combinatorParenthesizer Lean.Parser.sepBy1NoAntiquot] def sepBy1NoAntiquot.parenthesizer := sepByNoAntiquot.parenthesizer @[combinatorParenthesizer Lean.Parser.withPosition] def withPosition.parenthesizer (p : Parenthesizer) : Parenthesizer := do -- We assume the formatter will indent syntax sufficiently such that parenthesizing a `withPosition` node is never necessary modify fun st => { st with contPrec := none } p @[combinatorParenthesizer Lean.Parser.withoutPosition] def withoutPosition.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.withForbidden] def withForbidden.parenthesizer (tk : Parser.Token) (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.withoutForbidden] def withoutForbidden.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.withoutInfo] def withoutInfo.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.setExpected] def setExpected.parenthesizer (expected : List String) (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.incQuotDepth] def incQuotDepth.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.decQuotDepth] def decQuotDepth.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.suppressInsideQuot] def suppressInsideQuot.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.evalInsideQuot] def evalInsideQuot.parenthesizer (declName : Name) (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.checkStackTop] def checkStackTop.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkWsBefore] def checkWsBefore.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkNoWsBefore] def checkNoWsBefore.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkLinebreakBefore] def checkLinebreakBefore.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkTailWs] def checkTailWs.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkColGe] def checkColGe.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkColGt] def checkColGt.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkLineEq] def checkLineEq.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.eoi] def eoi.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.notFollowedByCategoryToken] def notFollowedByCategoryToken.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkNoImmediateColon] def checkNoImmediateColon.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkInsideQuot] def checkInsideQuot.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkOutsideQuot] def checkOutsideQuot.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.skip] def skip.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.pushNone] def pushNone.parenthesizer : Parenthesizer := goLeft @[combinatorParenthesizer Lean.Parser.withOpenDecl] def withOpenDecl.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.withOpen] def withOpen.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.interpolatedStr] def interpolatedStr.parenthesizer (p : Parenthesizer) : Parenthesizer := do visitArgs $ (← getCur).getArgs.reverse.forM fun chunk => if chunk.isOfKind interpolatedStrLitKind then goLeft else p @[combinatorParenthesizer Lean.Parser.dbgTraceState] def dbgTraceState.parenthesizer (label : String) (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer ite, macroInline] def ite {α : Type} (c : Prop) [h : Decidable c] (t e : Parenthesizer) : Parenthesizer := if c then t else e open Parser abbrev ParenthesizerAliasValue := AliasValue Parenthesizer builtin_initialize parenthesizerAliasesRef : IO.Ref (NameMap ParenthesizerAliasValue) ← IO.mkRef {} def registerAlias (aliasName : Name) (v : ParenthesizerAliasValue) : IO Unit := do Parser.registerAliasCore parenthesizerAliasesRef aliasName v instance : Coe Parenthesizer ParenthesizerAliasValue := { coe := AliasValue.const } instance : Coe (Parenthesizer → Parenthesizer) ParenthesizerAliasValue := { coe := AliasValue.unary } instance : Coe (Parenthesizer → Parenthesizer → Parenthesizer) ParenthesizerAliasValue := { coe := AliasValue.binary } end Parenthesizer open Parenthesizer /-- Add necessary parentheses in `stx` parsed by `parser`. -/ def parenthesize (parenthesizer : Parenthesizer) (stx : Syntax) : CoreM Syntax := do trace[PrettyPrinter.parenthesize.input] "{format stx}" catchInternalId backtrackExceptionId (do let (_, st) ← (parenthesizer {}).run { stxTrav := Syntax.Traverser.fromSyntax stx } pure st.stxTrav.cur) (fun _ => throwError "parenthesize: uncaught backtrack exception") def parenthesizeTerm := parenthesize $ categoryParser.parenthesizer `term 0 def parenthesizeCommand := parenthesize $ categoryParser.parenthesizer `command 0 builtin_initialize registerTraceClass `PrettyPrinter.parenthesize end PrettyPrinter end Lean
382f0e85ed29319a593c7e27ec5a0c56dca0d9c5
367134ba5a65885e863bdc4507601606690974c1
/src/field_theory/primitive_element.lean
27cdb797622bbe626a545a1e267cbae17aa64532
[ "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
7,812
lean
/- Copyright (c) 2020 Thomas Browning and Patrick Lutz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning and Patrick Lutz -/ import field_theory.adjoin import field_theory.separable /-! # Primitive Element Theorem In this file we prove the primitive element theorem. ## Main results - `exists_primitive_element`: a finite separable extension `E / F` has a primitive element, i.e. there is an `α : E` such that `F⟮α⟯ = (⊤ : subalgebra F E)`. ## Implementation notes In declaration names, `primitive_element` abbreviates `adjoin_simple_eq_top`: it stands for the statement `F⟮α⟯ = (⊤ : subalgebra F E)`. We did not add an extra declaration `is_primitive_element F α := F⟮α⟯ = (⊤ : subalgebra F E)` because this requires more unfolding without much obvious benefit. ## Tags primitive element, separable field extension, separable extension, intermediate field, adjoin, exists_adjoin_simple_eq_top -/ noncomputable theory open_locale classical open finite_dimensional polynomial intermediate_field namespace field section primitive_element_finite variables (F : Type*) [field F] (E : Type*) [field E] [algebra F E] /-! ### Primitive element theorem for finite fields -/ /-- Primitive element theorem assuming E is finite. -/ lemma exists_primitive_element_of_fintype_top [fintype E] : ∃ α : E, F⟮α⟯ = ⊤ := begin obtain ⟨α, hα⟩ := is_cyclic.exists_generator (units E), use α, apply eq_top_iff.mpr, rintros x -, by_cases hx : x = 0, { rw hx, exact F⟮α.val⟯.zero_mem }, { obtain ⟨n, hn⟩ := set.mem_range.mp (hα (units.mk0 x hx)), rw (show x = α^n, by { norm_cast, rw [hn, units.coe_mk0] }), exact pow_mem F⟮↑α⟯ (mem_adjoin_simple_self F ↑α) n, }, end /-- Primitive element theorem for finite dimensional extension of a finite field. -/ theorem exists_primitive_element_of_fintype_bot [fintype F] [finite_dimensional F E] : ∃ α : E, F⟮α⟯ = ⊤ := begin haveI : fintype E := fintype_of_fintype F E, exact exists_primitive_element_of_fintype_top F E, end end primitive_element_finite /-! ### Primitive element theorem for infinite fields -/ section primitive_element_inf variables {F : Type*} [field F] [infinite F] {E : Type*} [field E] (ϕ : F →+* E) (α β : E) lemma primitive_element_inf_aux_exists_c (f g : polynomial F) : ∃ c : F, ∀ (α' ∈ (f.map ϕ).roots) (β' ∈ (g.map ϕ).roots), -(α' - α)/(β' - β) ≠ ϕ c := begin let sf := (f.map ϕ).roots, let sg := (g.map ϕ).roots, let s := (sf.bind (λ α', sg.map (λ β', -(α' - α) / (β' - β)))).to_finset, let s' := s.preimage ϕ (λ x hx y hy h, ϕ.injective h), obtain ⟨c, hc⟩ := infinite.exists_not_mem_finset s', simp_rw [finset.mem_preimage, multiset.mem_to_finset, multiset.mem_bind, multiset.mem_map] at hc, push_neg at hc, exact ⟨c, hc⟩, end variables [algebra F E] -- This is the heart of the proof of the primitive element theorem. It shows that if `F` is -- infinite and `α` and `β` are separable over `F` then `F⟮α, β⟯` is generated by a single element. lemma primitive_element_inf_aux (F_sep : is_separable F E) : ∃ γ : E, F⟮α, β⟯ = F⟮γ⟯ := begin have hα := F_sep.is_integral α, have hβ := F_sep.is_integral β, let f := minpoly F α, let g := minpoly F β, let ιFE := algebra_map F E, let ιEE' := algebra_map E (splitting_field (g.map ιFE)), obtain ⟨c, hc⟩ := primitive_element_inf_aux_exists_c (ιEE'.comp ιFE) (ιEE' α) (ιEE' β) f g, let γ := α + c • β, suffices β_in_Fγ : β ∈ F⟮γ⟯, { use γ, apply le_antisymm, { rw adjoin_le_iff, have α_in_Fγ : α ∈ F⟮γ⟯, { rw ← add_sub_cancel α (c • β), exact F⟮γ⟯.sub_mem (mem_adjoin_simple_self F γ) (F⟮γ⟯.to_subalgebra.smul_mem β_in_Fγ c)}, exact λ x hx, by cases hx; cases hx; cases hx; assumption }, { rw adjoin_le_iff, change {γ} ⊆ _, rw set.singleton_subset_iff, have α_in_Fαβ : α ∈ F⟮α, β⟯ := subset_adjoin F {α, β} (set.mem_insert α {β}), have β_in_Fαβ : β ∈ F⟮α, β⟯ := subset_adjoin F {α, β} (set.mem_insert_of_mem α rfl), exact F⟮α,β⟯.add_mem α_in_Fαβ (F⟮α, β⟯.smul_mem β_in_Fαβ) } }, let p := euclidean_domain.gcd ((f.map (algebra_map F F⟮γ⟯)).comp (C (adjoin_simple.gen F γ) - (C ↑c * X))) (g.map (algebra_map F F⟮γ⟯)), let h := euclidean_domain.gcd ((f.map ιFE).comp (C γ - (C (ιFE c) * X))) (g.map ιFE), have map_g_ne_zero : g.map ιFE ≠ 0 := map_ne_zero (minpoly.ne_zero hβ), have h_ne_zero : h ≠ 0 := mt euclidean_domain.gcd_eq_zero_iff.mp (not_and.mpr (λ _, map_g_ne_zero)), suffices p_linear : p.map (algebra_map F⟮γ⟯ E) = (C h.leading_coeff) * (X - C β), { have finale : β = algebra_map F⟮γ⟯ E (-p.coeff 0 / p.coeff 1), { rw [ring_hom.map_div, ring_hom.map_neg, ←coeff_map, ←coeff_map, p_linear], simp [mul_sub, coeff_C, mul_div_cancel_left β (mt leading_coeff_eq_zero.mp h_ne_zero)] }, rw finale, exact subtype.mem (-p.coeff 0 / p.coeff 1) }, have h_sep : h.separable := separable_gcd_right _ (separable.map (F_sep.separable β)), have h_root : h.eval β = 0, { apply eval_gcd_eq_zero, { rw [eval_comp, eval_sub, eval_mul, eval_C, eval_C, eval_X, eval_map, ←aeval_def, ←algebra.smul_def, add_sub_cancel, minpoly.aeval] }, { rw [eval_map, ←aeval_def, minpoly.aeval] } }, have h_splits : splits ιEE' h := splits_of_splits_gcd_right ιEE' map_g_ne_zero (splitting_field.splits _), have h_roots : ∀ x ∈ (h.map ιEE').roots, x = ιEE' β, { intros x hx, rw mem_roots_map h_ne_zero at hx, specialize hc ((ιEE' γ) - (ιEE' (ιFE c)) * x) (begin have f_root := root_left_of_root_gcd hx, rw [eval₂_comp, eval₂_sub, eval₂_mul,eval₂_C, eval₂_C, eval₂_X, eval₂_map] at f_root, exact (mem_roots_map (minpoly.ne_zero hα)).mpr f_root, end), specialize hc x (begin rw [mem_roots_map (minpoly.ne_zero hβ), ←eval₂_map], exact root_right_of_root_gcd hx, end), by_contradiction a, apply hc, apply (div_eq_iff (sub_ne_zero.mpr a)).mpr, simp only [algebra.smul_def, ring_hom.map_add, ring_hom.map_mul, ring_hom.comp_apply], ring }, rw ← eq_X_sub_C_of_separable_of_root_eq h_ne_zero h_sep h_root h_splits h_roots, transitivity euclidean_domain.gcd (_ : polynomial E) (_ : polynomial E), { dsimp only [p], convert (gcd_map (algebra_map F⟮γ⟯ E)).symm }, { simpa [map_comp, map_map, ←is_scalar_tower.algebra_map_eq, h] }, end end primitive_element_inf variables {F : Type*} [field F] {E : Type*} [field E] [algebra F E] /-- Primitive element theorem: a finite separable field extension `E` of `F` has a primitive element, i.e. there is an `α ∈ E` such that `F⟮α⟯ = (⊤ : subalgebra F E)`.-/ theorem exists_primitive_element [finite_dimensional F E] (F_sep : is_separable F E) : ∃ α : E, F⟮α⟯ = ⊤ := begin by_cases F_finite : nonempty (fintype F), { exact nonempty.elim F_finite (λ h, by haveI := h; exact exists_primitive_element_of_fintype_bot F E) }, { let P : intermediate_field F E → Prop := λ K, ∃ α : E, F⟮α⟯ = K, have base : P ⊥ := ⟨0, adjoin_zero⟩, have ih : ∀ (K : intermediate_field F E) (x : E), P K → P ↑K⟮x⟯, { intros K β hK, cases hK with α hK, rw [←hK, adjoin_simple_adjoin_simple], haveI : infinite F := not_nonempty_fintype.mp F_finite, cases primitive_element_inf_aux α β F_sep with γ hγ, exact ⟨γ, hγ.symm⟩ }, exact induction_on_adjoin P base ih ⊤ }, end end field
134c59a514753a21b24a1d08c2cee15ca3d0d0cf
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/rename_var.lean
5e5f3280dfcb5d54359bb3efda50d4085692efbd
[ "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
2,772
lean
/- Copyright (c) 2019 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import tactic.interactive /-! # Rename bound variable tactic This files defines a tactic `rename_var` whose main purpose is to teach renaming of bound variables. * `rename_var old new` renames all bound variables named `old` to `new` in the goal. * `rename_var old new at h` does the same in hypothesis `h`. ```lean example (P : ℕ → ℕ → Prop) (h : ∀ n, ∃ m, P n m) : ∀ l, ∃ m, P l m := begin rename_var n q at h, -- h is now ∀ (q : ℕ), ∃ (m : ℕ), P q m, rename_var m n, -- goal is now ∀ (l : ℕ), ∃ (n : ℕ), P k n, exact h -- Lean does not care about those bound variable names end ``` ## Tags teaching, tactic -/ open expr /-- Rename bound variable `old` to `new` in an `expr`-/ meta def expr.rename_var (old new : name) : expr → expr | (pi n bi t b) := (pi (if n = old then new else n) bi (expr.rename_var t) (expr.rename_var b)) | (lam n bi t b) := (lam (if n = old then new else n) bi (expr.rename_var t) (expr.rename_var b)) | (app t b) := (app (expr.rename_var t) (expr.rename_var b)) | e := e namespace tactic /-- Rename bound variable `old` to `new` in goal -/ meta def rename_var_at_goal (old new : name) : tactic unit := do old_tgt ← target, tactic.change (expr.rename_var old new old_tgt) /-- Rename bound variable `old` to `new` in assumption `h` -/ meta def rename_var_at_hyp (old new : name) (e : expr) : tactic unit := do old_e ← infer_type e, tactic.change_core (expr.rename_var old new old_e) (some e) end tactic namespace tactic.interactive open tactic setup_tactic_parser /-- `rename_var old new` renames all bound variables named `old` to `new` in the goal. `rename_var old new at h` does the same in hypothesis `h`. -/ meta def rename_var (old : parse ident) (new : parse ident) (l : parse location) : tactic unit := l.apply (rename_var_at_hyp old new) (rename_var_at_goal old new) end tactic.interactive /-- `rename_var old new` renames all bound variables named `old` to `new` in the goal. `rename_var old new at h` does the same in hypothesis `h`. This is meant for teaching bound variables only. Such a renaming should never be relevant to Lean. ```lean example (P : ℕ → ℕ → Prop) (h : ∀ n, ∃ m, P n m) : ∀ l, ∃ m, P l m := begin rename_var n q at h, -- h is now ∀ (q : ℕ), ∃ (m : ℕ), P q m, rename_var m n, -- goal is now ∀ (l : ℕ), ∃ (n : ℕ), P k n, exact h -- Lean does not care about those bound variable names end ``` -/ add_tactic_doc { name := "rename_var", category := doc_category.tactic, decl_names := [`tactic.interactive.rename_var], tags := ["renaming"] }
a1627755872d35ef480c75f9e23fd04be1c6166f
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/elab14.lean
5a0eb25e279414d1c67fbb516cdc74e916a14ac0
[ "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
87
lean
set_option pp.all true universe variables u check λ (A B : Type u) (a : A) (b : B), a
46ad0edb06d6cdc5b10efc50621a881f3e67b2be
9dc8cecdf3c4634764a18254e94d43da07142918
/src/order/filter/pointwise.lean
0b3e90b529f1c9a1854b21933c7d9d3b49d41d9c
[ "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
35,605
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yaël Dillies -/ import data.set.pointwise import order.filter.n_ary import order.filter.ultrafilter /-! # Pointwise operations on filters This file defines pointwise operations on filters. This is useful because usual algebraic operations distribute over pointwise operations. For example, * `(f₁ * f₂).map m = f₁.map m * f₂.map m` * `𝓝 (x * y) = 𝓝 x * 𝓝 y` ## Main declarations * `0` (`filter.has_zero`): Pure filter at `0 : α`, or alternatively principal filter at `0 : set α`. * `1` (`filter.has_one`): Pure filter at `1 : α`, or alternatively principal filter at `1 : set α`. * `f + g` (`filter.has_add`): Addition, filter generated by all `s + t` where `s ∈ f` and `t ∈ g`. * `f * g` (`filter.has_mul`): Multiplication, filter generated by all `s * t` where `s ∈ f` and `t ∈ g`. * `-f` (`filter.has_neg`): Negation, filter of all `-s` where `s ∈ f`. * `f⁻¹` (`filter.has_inv`): Inversion, filter of all `s⁻¹` where `s ∈ f`. * `f - g` (`filter.has_sub`): Subtraction, filter generated by all `s - t` where `s ∈ f` and `t ∈ g`. * `f / g` (`filter.has_div`): Division, filter generated by all `s / t` where `s ∈ f` and `t ∈ g`. * `f +ᵥ g` (`filter.has_vadd`): Scalar addition, filter generated by all `s +ᵥ t` where `s ∈ f` and `t ∈ g`. * `f -ᵥ g` (`filter.has_vsub`): Scalar subtraction, filter generated by all `s -ᵥ t` where `s ∈ f` and `t ∈ g`. * `f • g` (`filter.has_smul`): Scalar multiplication, filter generated by all `s • t` where `s ∈ f` and `t ∈ g`. * `a +ᵥ f` (`filter.has_vadd_filter`): Translation, filter of all `a +ᵥ s` where `s ∈ f`. * `a • f` (`filter.has_smul_filter`): Scaling, filter of all `a • s` where `s ∈ f`. For `α` a semigroup/monoid, `filter α` is a semigroup/monoid. As an unfortunate side effect, this means that `n • f`, where `n : ℕ`, is ambiguous between pointwise scaling and repeated pointwise addition. See note [pointwise nat action]. ## Implementation notes We put all instances in the locale `pointwise`, so that these instances are not available by default. Note that we do not mark them as reducible (as argued by note [reducible non-instances]) since we expect the locale to be open whenever the instances are actually used (and making the instances reducible changes the behavior of `simp`. ## Tags filter multiplication, filter addition, pointwise addition, pointwise multiplication, -/ open function set open_locale filter pointwise variables {F α β γ δ ε : Type*} namespace filter /-! ### `0`/`1` as filters -/ section has_one variables [has_one α] {f : filter α} {s : set α} /-- `1 : filter α` is defined as the filter of sets containing `1 : α` in locale `pointwise`. -/ @[to_additive "`0 : filter α` is defined as the filter of sets containing `0 : α` in locale `pointwise`."] protected def has_one : has_one (filter α) := ⟨pure 1⟩ localized "attribute [instance] filter.has_one filter.has_zero" in pointwise @[simp, to_additive] lemma mem_one : s ∈ (1 : filter α) ↔ (1 : α) ∈ s := mem_pure @[to_additive] lemma one_mem_one : (1 : set α) ∈ (1 : filter α) := mem_pure.2 one_mem_one @[simp, to_additive] lemma pure_one : pure 1 = (1 : filter α) := rfl @[simp, to_additive] lemma principal_one : 𝓟 1 = (1 : filter α) := principal_singleton _ @[to_additive] lemma one_ne_bot : (1 : filter α).ne_bot := filter.pure_ne_bot @[simp, to_additive] protected lemma map_one' (f : α → β) : (1 : filter α).map f = pure (f 1) := rfl @[simp, to_additive] lemma le_one_iff : f ≤ 1 ↔ (1 : set α) ∈ f := le_pure_iff @[to_additive] protected lemma ne_bot.le_one_iff (h : f.ne_bot) : f ≤ 1 ↔ f = 1 := h.le_pure_iff @[simp, to_additive] lemma eventually_one {p : α → Prop} : (∀ᶠ x in 1, p x) ↔ p 1 := eventually_pure @[simp, to_additive] lemma tendsto_one {a : filter β} {f : β → α} : tendsto f a 1 ↔ ∀ᶠ x in a, f x = 1 := tendsto_pure /-- `pure` as a `one_hom`. -/ @[to_additive "`pure` as a `zero_hom`."] def pure_one_hom : one_hom α (filter α) := ⟨pure, pure_one⟩ @[simp, to_additive] lemma coe_pure_one_hom : (pure_one_hom : α → filter α) = pure := rfl @[simp, to_additive] lemma pure_one_hom_apply (a : α) : pure_one_hom a = pure a := rfl variables [has_one β] @[simp, to_additive] protected lemma map_one [one_hom_class F α β] (φ : F) : map φ 1 = 1 := by rw [filter.map_one', map_one, pure_one] end has_one /-! ### Filter negation/inversion -/ section has_inv variables [has_inv α] {f g : filter α} {s : set α} {a : α} /-- The inverse of a filter is the pointwise preimage under `⁻¹` of its sets. -/ @[to_additive "The negation of a filter is the pointwise preimage under `-` of its sets."] instance : has_inv (filter α) := ⟨map has_inv.inv⟩ @[simp, to_additive] protected lemma map_inv : f.map has_inv.inv = f⁻¹ := rfl @[to_additive] lemma mem_inv : s ∈ f⁻¹ ↔ has_inv.inv ⁻¹' s ∈ f := iff.rfl @[to_additive] protected lemma inv_le_inv (hf : f ≤ g) : f⁻¹ ≤ g⁻¹ := map_mono hf @[simp, to_additive] lemma inv_pure : (pure a : filter α)⁻¹ = pure a⁻¹ := rfl @[simp, to_additive] lemma inv_eq_bot_iff : f⁻¹ = ⊥ ↔ f = ⊥ := map_eq_bot_iff @[simp, to_additive] lemma ne_bot_inv_iff : f⁻¹.ne_bot ↔ ne_bot f := map_ne_bot_iff _ @[to_additive] lemma ne_bot.inv : f.ne_bot → f⁻¹.ne_bot := λ h, h.map _ end has_inv section has_involutive_inv variables [has_involutive_inv α] {f : filter α} {s : set α} @[to_additive] lemma inv_mem_inv (hs : s ∈ f) : s⁻¹ ∈ f⁻¹ := by rwa [mem_inv, inv_preimage, inv_inv] /-- Inversion is involutive on `filter α` if it is on `α`. -/ @[to_additive "Negation is involutive on `filter α` if it is on `α`."] protected def has_involutive_inv : has_involutive_inv (filter α) := { inv_inv := λ f, map_map.trans $ by rw [inv_involutive.comp_self, map_id], ..filter.has_inv } end has_involutive_inv /-! ### Filter addition/multiplication -/ section has_mul variables [has_mul α] [has_mul β] {f f₁ f₂ g g₁ g₂ h : filter α} {s t : set α} {a b : α} /-- The filter `f * g` is generated by `{s * t | s ∈ f, t ∈ g}` in locale `pointwise`. -/ @[to_additive "The filter `f + g` is generated by `{s + t | s ∈ f, t ∈ g}` in locale `pointwise`."] protected def has_mul : has_mul (filter α) := /- This is defeq to `map₂ (*) f g`, but the hypothesis unfolds to `t₁ * t₂ ⊆ s` rather than all the way to `set.image2 (*) t₁ t₂ ⊆ s`. -/ ⟨λ f g, { sets := {s | ∃ t₁ t₂, t₁ ∈ f ∧ t₂ ∈ g ∧ t₁ * t₂ ⊆ s}, ..map₂ (*) f g }⟩ localized "attribute [instance] filter.has_mul filter.has_add" in pointwise @[simp, to_additive] lemma map₂_mul : map₂ (*) f g = f * g := rfl @[to_additive] lemma mem_mul : s ∈ f * g ↔ ∃ t₁ t₂, t₁ ∈ f ∧ t₂ ∈ g ∧ t₁ * t₂ ⊆ s := iff.rfl @[to_additive] lemma mul_mem_mul : s ∈ f → t ∈ g → s * t ∈ f * g := image2_mem_map₂ @[simp, to_additive] lemma bot_mul : ⊥ * g = ⊥ := map₂_bot_left @[simp, to_additive] lemma mul_bot : f * ⊥ = ⊥ := map₂_bot_right @[simp, to_additive] lemma mul_eq_bot_iff : f * g = ⊥ ↔ f = ⊥ ∨ g = ⊥ := map₂_eq_bot_iff @[simp, to_additive] lemma mul_ne_bot_iff : (f * g).ne_bot ↔ f.ne_bot ∧ g.ne_bot := map₂_ne_bot_iff @[to_additive] lemma ne_bot.mul : ne_bot f → ne_bot g → ne_bot (f * g) := ne_bot.map₂ @[to_additive] lemma ne_bot.of_mul_left : (f * g).ne_bot → f.ne_bot := ne_bot.of_map₂_left @[to_additive] lemma ne_bot.of_mul_right : (f * g).ne_bot → g.ne_bot := ne_bot.of_map₂_right @[simp, to_additive] lemma pure_mul : pure a * g = g.map ((*) a) := map₂_pure_left @[simp, to_additive] lemma mul_pure : f * pure b = f.map (* b) := map₂_pure_right @[simp, to_additive] lemma pure_mul_pure : (pure a : filter α) * pure b = pure (a * b) := map₂_pure @[simp, to_additive] lemma le_mul_iff : h ≤ f * g ↔ ∀ ⦃s⦄, s ∈ f → ∀ ⦃t⦄, t ∈ g → s * t ∈ h := le_map₂_iff @[to_additive] instance covariant_mul : covariant_class (filter α) (filter α) (*) (≤) := ⟨λ f g h, map₂_mono_left⟩ @[to_additive] instance covariant_swap_mul : covariant_class (filter α) (filter α) (swap (*)) (≤) := ⟨λ f g h, map₂_mono_right⟩ @[to_additive] protected lemma map_mul [mul_hom_class F α β] (m : F) : (f₁ * f₂).map m = f₁.map m * f₂.map m := map_map₂_distrib $ map_mul m /-- `pure` operation as a `mul_hom`. -/ @[to_additive "The singleton operation as an `add_hom`."] def pure_mul_hom : α →ₙ* filter α := ⟨pure, λ a b, pure_mul_pure.symm⟩ @[simp, to_additive] lemma coe_pure_mul_hom : (pure_mul_hom : α → filter α) = pure := rfl @[simp, to_additive] lemma pure_mul_hom_apply (a : α) : pure_mul_hom a = pure a := rfl end has_mul /-! ### Filter subtraction/division -/ section div variables [has_div α] {f f₁ f₂ g g₁ g₂ h : filter α} {s t : set α} {a b : α} /-- The filter `f / g` is generated by `{s / t | s ∈ f, t ∈ g}` in locale `pointwise`. -/ @[to_additive "The filter `f - g` is generated by `{s - t | s ∈ f, t ∈ g}` in locale `pointwise`."] protected def has_div : has_div (filter α) := /- This is defeq to `map₂ (/) f g`, but the hypothesis unfolds to `t₁ / t₂ ⊆ s` rather than all the way to `set.image2 (/) t₁ t₂ ⊆ s`. -/ ⟨λ f g, { sets := {s | ∃ t₁ t₂, t₁ ∈ f ∧ t₂ ∈ g ∧ t₁ / t₂ ⊆ s}, ..map₂ (/) f g }⟩ localized "attribute [instance] filter.has_div filter.has_sub" in pointwise @[simp, to_additive] lemma map₂_div : map₂ (/) f g = f / g := rfl @[to_additive] lemma mem_div : s ∈ f / g ↔ ∃ t₁ t₂, t₁ ∈ f ∧ t₂ ∈ g ∧ t₁ / t₂ ⊆ s := iff.rfl @[to_additive] lemma div_mem_div : s ∈ f → t ∈ g → s / t ∈ f / g := image2_mem_map₂ @[simp, to_additive] lemma bot_div : ⊥ / g = ⊥ := map₂_bot_left @[simp, to_additive] lemma div_bot : f / ⊥ = ⊥ := map₂_bot_right @[simp, to_additive] lemma div_eq_bot_iff : f / g = ⊥ ↔ f = ⊥ ∨ g = ⊥ := map₂_eq_bot_iff @[simp, to_additive] lemma div_ne_bot_iff : (f / g).ne_bot ↔ f.ne_bot ∧ g.ne_bot := map₂_ne_bot_iff @[to_additive] lemma ne_bot.div : ne_bot f → ne_bot g → ne_bot (f / g) := ne_bot.map₂ @[to_additive] lemma ne_bot.of_div_left : (f / g).ne_bot → f.ne_bot := ne_bot.of_map₂_left @[to_additive] lemma ne_bot.of_div_right : (f / g).ne_bot → g.ne_bot := ne_bot.of_map₂_right @[simp, to_additive] lemma pure_div : pure a / g = g.map ((/) a) := map₂_pure_left @[simp, to_additive] lemma div_pure : f / pure b = f.map (/ b) := map₂_pure_right @[simp, to_additive] lemma pure_div_pure : (pure a : filter α) / pure b = pure (a / b) := map₂_pure @[to_additive] protected lemma div_le_div : f₁ ≤ f₂ → g₁ ≤ g₂ → f₁ / g₁ ≤ f₂ / g₂ := map₂_mono @[to_additive] protected lemma div_le_div_left : g₁ ≤ g₂ → f / g₁ ≤ f / g₂ := map₂_mono_left @[to_additive] protected lemma div_le_div_right : f₁ ≤ f₂ → f₁ / g ≤ f₂ / g := map₂_mono_right @[simp, to_additive] protected lemma le_div_iff : h ≤ f / g ↔ ∀ ⦃s⦄, s ∈ f → ∀ ⦃t⦄, t ∈ g → s / t ∈ h := le_map₂_iff @[to_additive] instance covariant_div : covariant_class (filter α) (filter α) (/) (≤) := ⟨λ f g h, map₂_mono_left⟩ @[to_additive] instance covariant_swap_div : covariant_class (filter α) (filter α) (swap (/)) (≤) := ⟨λ f g h, map₂_mono_right⟩ end div open_locale pointwise /-- Repeated pointwise addition (not the same as pointwise repeated addition!) of a `filter`. See Note [pointwise nat action].-/ protected def has_nsmul [has_zero α] [has_add α] : has_smul ℕ (filter α) := ⟨nsmul_rec⟩ /-- Repeated pointwise multiplication (not the same as pointwise repeated multiplication!) of a `filter`. See Note [pointwise nat action]. -/ @[to_additive] protected def has_npow [has_one α] [has_mul α] : has_pow (filter α) ℕ := ⟨λ s n, npow_rec n s⟩ /-- Repeated pointwise addition/subtraction (not the same as pointwise repeated addition/subtraction!) of a `filter`. See Note [pointwise nat action]. -/ protected def has_zsmul [has_zero α] [has_add α] [has_neg α] : has_smul ℤ (filter α) := ⟨zsmul_rec⟩ /-- Repeated pointwise multiplication/division (not the same as pointwise repeated multiplication/division!) of a `filter`. See Note [pointwise nat action]. -/ @[to_additive] protected def has_zpow [has_one α] [has_mul α] [has_inv α] : has_pow (filter α) ℤ := ⟨λ s n, zpow_rec n s⟩ localized "attribute [instance] filter.has_nsmul filter.has_npow filter.has_zsmul filter.has_zpow" in pointwise /-- `filter α` is a `semigroup` under pointwise operations if `α` is.-/ @[to_additive "`filter α` is an `add_semigroup` under pointwise operations if `α` is."] protected def semigroup [semigroup α] : semigroup (filter α) := { mul := (*), mul_assoc := λ f g h, map₂_assoc mul_assoc } /-- `filter α` is a `comm_semigroup` under pointwise operations if `α` is. -/ @[to_additive "`filter α` is an `add_comm_semigroup` under pointwise operations if `α` is."] protected def comm_semigroup [comm_semigroup α] : comm_semigroup (filter α) := { mul_comm := λ f g, map₂_comm mul_comm, ..filter.semigroup } section mul_one_class variables [mul_one_class α] [mul_one_class β] /-- `filter α` is a `mul_one_class` under pointwise operations if `α` is. -/ @[to_additive "`filter α` is an `add_zero_class` under pointwise operations if `α` is."] protected def mul_one_class : mul_one_class (filter α) := { one := 1, mul := (*), one_mul := λ f, by simp only [←pure_one, ←map₂_mul, map₂_pure_left, one_mul, map_id'], mul_one := λ f, by simp only [←pure_one, ←map₂_mul, map₂_pure_right, mul_one, map_id'] } localized "attribute [instance] filter.semigroup filter.add_semigroup filter.comm_semigroup filter.add_comm_semigroup filter.mul_one_class filter.add_zero_class" in pointwise /-- If `φ : α →* β` then `map_monoid_hom φ` is the monoid homomorphism `filter α →* filter β` induced by `map φ`. -/ @[to_additive "If `φ : α →+ β` then `map_add_monoid_hom φ` is the monoid homomorphism `filter α →+ filter β` induced by `map φ`."] def map_monoid_hom [monoid_hom_class F α β] (φ : F) : filter α →* filter β := { to_fun := map φ, map_one' := filter.map_one φ, map_mul' := λ _ _, filter.map_mul φ } -- The other direction does not hold in general @[to_additive] lemma comap_mul_comap_le [mul_hom_class F α β] (m : F) {f g : filter β} : f.comap m * g.comap m ≤ (f * g).comap m := λ s ⟨t, ⟨t₁, t₂, ht₁, ht₂, t₁t₂⟩, mt⟩, ⟨m ⁻¹' t₁, m ⁻¹' t₂, ⟨t₁, ht₁, subset.rfl⟩, ⟨t₂, ht₂, subset.rfl⟩, (preimage_mul_preimage_subset _).trans $ (preimage_mono t₁t₂).trans mt⟩ @[to_additive] lemma tendsto.mul_mul [mul_hom_class F α β] (m : F) {f₁ g₁ : filter α} {f₂ g₂ : filter β} : tendsto m f₁ f₂ → tendsto m g₁ g₂ → tendsto m (f₁ * g₁) (f₂ * g₂) := λ hf hg, (filter.map_mul m).trans_le $ mul_le_mul' hf hg /-- `pure` as a `monoid_hom`. -/ @[to_additive "`pure` as an `add_monoid_hom`."] def pure_monoid_hom : α →* filter α := { ..pure_mul_hom, ..pure_one_hom } @[simp, to_additive] lemma coe_pure_monoid_hom : (pure_monoid_hom : α → filter α) = pure := rfl @[simp, to_additive] lemma pure_monoid_hom_apply (a : α) : pure_monoid_hom a = pure a := rfl end mul_one_class section monoid variables [monoid α] {f g : filter α} {s : set α} {a : α} {m n : ℕ} /-- `filter α` is a `monoid` under pointwise operations if `α` is. -/ @[to_additive "`filter α` is an `add_monoid` under pointwise operations if `α` is."] protected def monoid : monoid (filter α) := { ..filter.mul_one_class, ..filter.semigroup, ..filter.has_npow } localized "attribute [instance] filter.monoid filter.add_monoid" in pointwise @[to_additive] lemma pow_mem_pow (hs : s ∈ f) : ∀ n : ℕ, s ^ n ∈ f ^ n | 0 := by { rw pow_zero, exact one_mem_one } | (n + 1) := by { rw pow_succ, exact mul_mem_mul hs (pow_mem_pow _) } @[simp, to_additive nsmul_bot] lemma bot_pow {n : ℕ} (hn : n ≠ 0) : (⊥ : filter α) ^ n = ⊥ := by rw [←tsub_add_cancel_of_le (nat.succ_le_of_lt $ nat.pos_of_ne_zero hn), pow_succ, bot_mul] @[to_additive] lemma mul_top_of_one_le (hf : 1 ≤ f) : f * ⊤ = ⊤ := begin refine top_le_iff.1 (λ s, _), simp only [mem_mul, mem_top, exists_and_distrib_left, exists_eq_left], rintro ⟨t, ht, hs⟩, rwa [mul_univ_of_one_mem (mem_one.1 $ hf ht), univ_subset_iff] at hs, end @[to_additive] lemma top_mul_of_one_le (hf : 1 ≤ f) : ⊤ * f = ⊤ := begin refine top_le_iff.1 (λ s, _), simp only [mem_mul, mem_top, exists_and_distrib_left, exists_eq_left], rintro ⟨t, ht, hs⟩, rwa [univ_mul_of_one_mem (mem_one.1 $ hf ht), univ_subset_iff] at hs, end @[simp, to_additive] lemma top_mul_top : (⊤ : filter α) * ⊤ = ⊤ := mul_top_of_one_le le_top --TODO: `to_additive` trips up on the `1 : ℕ` used in the pattern-matching. lemma nsmul_top {α : Type*} [add_monoid α] : ∀ {n : ℕ}, n ≠ 0 → n • (⊤ : filter α) = ⊤ | 0 := λ h, (h rfl).elim | 1 := λ _, one_nsmul _ | (n + 2) := λ _, by { rw [succ_nsmul, nsmul_top n.succ_ne_zero, top_add_top] } @[to_additive nsmul_top] lemma top_pow : ∀ {n : ℕ}, n ≠ 0 → (⊤ : filter α) ^ n = ⊤ | 0 := λ h, (h rfl).elim | 1 := λ _, pow_one _ | (n + 2) := λ _, by { rw [pow_succ, top_pow n.succ_ne_zero, top_mul_top] } @[to_additive] protected lemma _root_.is_unit.filter : is_unit a → is_unit (pure a : filter α) := is_unit.map (pure_monoid_hom : α →* filter α) end monoid /-- `filter α` is a `comm_monoid` under pointwise operations if `α` is. -/ @[to_additive "`filter α` is an `add_comm_monoid` under pointwise operations if `α` is."] protected def comm_monoid [comm_monoid α] : comm_monoid (filter α) := { ..filter.mul_one_class, ..filter.comm_semigroup } open_locale pointwise section division_monoid variables [division_monoid α] {f g : filter α} @[to_additive] protected lemma mul_eq_one_iff : f * g = 1 ↔ ∃ a b, f = pure a ∧ g = pure b ∧ a * b = 1 := begin refine ⟨λ hfg, _, _⟩, { obtain ⟨t₁, t₂, h₁, h₂, h⟩ : (1 : set α) ∈ f * g := hfg.symm.subst one_mem_one, have hfg : (f * g).ne_bot := hfg.symm.subst one_ne_bot, rw [(hfg.nonempty_of_mem $ mul_mem_mul h₁ h₂).subset_one_iff, set.mul_eq_one_iff] at h, obtain ⟨a, b, rfl, rfl, h⟩ := h, refine ⟨a, b, _, _, h⟩, { rwa [←hfg.of_mul_left.le_pure_iff, le_pure_iff] }, { rwa [←hfg.of_mul_right.le_pure_iff, le_pure_iff] } }, { rintro ⟨a, b, rfl, rfl, h⟩, rw [pure_mul_pure, h, pure_one] } end /-- `filter α` is a division monoid under pointwise operations if `α` is. -/ @[to_additive subtraction_monoid "`filter α` is a subtraction monoid under pointwise operations if `α` is."] protected def division_monoid : division_monoid (filter α) := { mul_inv_rev := λ s t, map_map₂_antidistrib mul_inv_rev, inv_eq_of_mul := λ s t h, begin obtain ⟨a, b, rfl, rfl, hab⟩ := filter.mul_eq_one_iff.1 h, rw [inv_pure, inv_eq_of_mul_eq_one_right hab], end, div_eq_mul_inv := λ f g, map_map₂_distrib_right div_eq_mul_inv, ..filter.monoid, ..filter.has_involutive_inv, ..filter.has_div, ..filter.has_zpow } @[to_additive] lemma is_unit_iff : is_unit f ↔ ∃ a, f = pure a ∧ is_unit a := begin split, { rintro ⟨u, rfl⟩, obtain ⟨a, b, ha, hb, h⟩ := filter.mul_eq_one_iff.1 u.mul_inv, refine ⟨a, ha, ⟨a, b, h, pure_injective _⟩, rfl⟩, rw [←pure_mul_pure, ←ha, ←hb], exact u.inv_mul }, { rintro ⟨a, rfl, ha⟩, exact ha.filter } end end division_monoid /-- `filter α` is a commutative division monoid under pointwise operations if `α` is. -/ @[to_additive subtraction_comm_monoid "`filter α` is a commutative subtraction monoid under pointwise operations if `α` is."] protected def division_comm_monoid [division_comm_monoid α] : division_comm_monoid (filter α) := { ..filter.division_monoid, ..filter.comm_semigroup } /-- `filter α` has distributive negation if `α` has. -/ protected def has_distrib_neg [has_mul α] [has_distrib_neg α] : has_distrib_neg (filter α) := { neg_mul := λ _ _, map₂_map_left_comm neg_mul, mul_neg := λ _ _, map_map₂_right_comm mul_neg, ..filter.has_involutive_neg } localized "attribute [instance] filter.comm_monoid filter.add_comm_monoid filter.division_monoid filter.subtraction_monoid filter.division_comm_monoid filter.subtraction_comm_monoid filter.has_distrib_neg" in pointwise section distrib variables [distrib α] {f g h : filter α} /-! Note that `filter α` is not a `distrib` because `f * g + f * h` has cross terms that `f * (g + h)` lacks. -/ lemma mul_add_subset : f * (g + h) ≤ f * g + f * h := map₂_distrib_le_left mul_add lemma add_mul_subset : (f + g) * h ≤ f * h + g * h := map₂_distrib_le_right add_mul end distrib section mul_zero_class variables [mul_zero_class α] {f g : filter α} /-! Note that `filter` is not a `mul_zero_class` because `0 * ⊥ ≠ 0`. -/ lemma ne_bot.mul_zero_nonneg (hf : f.ne_bot) : 0 ≤ f * 0 := le_mul_iff.2 $ λ t₁ h₁ t₂ h₂, let ⟨a, ha⟩ := hf.nonempty_of_mem h₁ in ⟨_, _, ha, h₂, mul_zero _⟩ lemma ne_bot.zero_mul_nonneg (hg : g.ne_bot) : 0 ≤ 0 * g := le_mul_iff.2 $ λ t₁ h₁ t₂ h₂, let ⟨b, hb⟩ := hg.nonempty_of_mem h₂ in ⟨_, _, h₁, hb, zero_mul _⟩ end mul_zero_class section group variables [group α] [division_monoid β] [monoid_hom_class F α β] (m : F) {f g f₁ g₁ : filter α} {f₂ g₂ : filter β} /-! Note that `filter α` is not a group because `f / f ≠ 1` in general -/ @[simp, to_additive] protected lemma one_le_div_iff : 1 ≤ f / g ↔ ¬ disjoint f g := begin refine ⟨λ h hfg, _, _⟩, { obtain ⟨s, hs, t, ht, hst⟩ := hfg (mem_bot : ∅ ∈ ⊥), exact set.one_mem_div_iff.1 (h $ div_mem_div hs ht) (disjoint_iff.2 hst.symm) }, { rintro h s ⟨t₁, t₂, h₁, h₂, hs⟩, exact hs (set.one_mem_div_iff.2 $ λ ht, h $ disjoint_of_disjoint_of_mem ht h₁ h₂) } end @[to_additive] lemma not_one_le_div_iff : ¬ 1 ≤ f / g ↔ disjoint f g := filter.one_le_div_iff.not_left @[to_additive] lemma ne_bot.one_le_div (h : f.ne_bot) : 1 ≤ f / f := begin rintro s ⟨t₁, t₂, h₁, h₂, hs⟩, obtain ⟨a, ha₁, ha₂⟩ := set.not_disjoint_iff.1 (h.not_disjoint h₁ h₂), rw [mem_one, ←div_self' a], exact hs (set.div_mem_div ha₁ ha₂), end @[to_additive] lemma is_unit_pure (a : α) : is_unit (pure a : filter α) := (group.is_unit a).filter @[simp] lemma is_unit_iff_singleton : is_unit f ↔ ∃ a, f = pure a := by simp only [is_unit_iff, group.is_unit, and_true] include β @[to_additive] lemma map_inv' : f⁻¹.map m = (f.map m)⁻¹ := semiconj.filter_map (map_inv m) f @[to_additive] lemma tendsto.inv_inv : tendsto m f₁ f₂ → tendsto m f₁⁻¹ f₂⁻¹ := λ hf, (filter.map_inv' m).trans_le $ filter.inv_le_inv hf @[to_additive] protected lemma map_div : (f / g).map m = f.map m / g.map m := map_map₂_distrib $ map_div m @[to_additive] lemma tendsto.div_div : tendsto m f₁ f₂ → tendsto m g₁ g₂ → tendsto m (f₁ / g₁) (f₂ / g₂) := λ hf hg, (filter.map_div m).trans_le $ filter.div_le_div hf hg end group open_locale pointwise section group_with_zero variables [group_with_zero α] {f g : filter α} lemma ne_bot.div_zero_nonneg (hf : f.ne_bot) : 0 ≤ f / 0 := filter.le_div_iff.2 $ λ t₁ h₁ t₂ h₂, let ⟨a, ha⟩ := hf.nonempty_of_mem h₁ in ⟨_, _, ha, h₂, div_zero _⟩ lemma ne_bot.zero_div_nonneg (hg : g.ne_bot) : 0 ≤ 0 / g := filter.le_div_iff.2 $ λ t₁ h₁ t₂ h₂, let ⟨b, hb⟩ := hg.nonempty_of_mem h₂ in ⟨_, _, h₁, hb, zero_div _⟩ end group_with_zero /-! ### Scalar addition/multiplication of filters -/ section smul variables [has_smul α β] {f f₁ f₂ : filter α} {g g₁ g₂ h : filter β} {s : set α} {t : set β} {a : α} {b : β} /-- The filter `f • g` is generated by `{s • t | s ∈ f, t ∈ g}` in locale `pointwise`. -/ @[to_additive filter.has_vadd "The filter `f +ᵥ g` is generated by `{s +ᵥ t | s ∈ f, t ∈ g}` in locale `pointwise`."] protected def has_smul : has_smul (filter α) (filter β) := /- This is defeq to `map₂ (•) f g`, but the hypothesis unfolds to `t₁ • t₂ ⊆ s` rather than all the way to `set.image2 (•) t₁ t₂ ⊆ s`. -/ ⟨λ f g, { sets := {s | ∃ t₁ t₂, t₁ ∈ f ∧ t₂ ∈ g ∧ t₁ • t₂ ⊆ s}, ..map₂ (•) f g }⟩ localized "attribute [instance] filter.has_smul filter.has_vadd" in pointwise @[simp, to_additive] lemma map₂_smul : map₂ (•) f g = f • g := rfl @[to_additive] lemma mem_smul : t ∈ f • g ↔ ∃ t₁ t₂, t₁ ∈ f ∧ t₂ ∈ g ∧ t₁ • t₂ ⊆ t := iff.rfl @[to_additive] lemma smul_mem_smul : s ∈ f → t ∈ g → s • t ∈ f • g := image2_mem_map₂ @[simp, to_additive] lemma bot_smul : (⊥ : filter α) • g = ⊥ := map₂_bot_left @[simp, to_additive] lemma smul_bot : f • (⊥ : filter β) = ⊥ := map₂_bot_right @[simp, to_additive] lemma smul_eq_bot_iff : f • g = ⊥ ↔ f = ⊥ ∨ g = ⊥ := map₂_eq_bot_iff @[simp, to_additive] lemma smul_ne_bot_iff : (f • g).ne_bot ↔ f.ne_bot ∧ g.ne_bot := map₂_ne_bot_iff @[to_additive] lemma ne_bot.smul : ne_bot f → ne_bot g → ne_bot (f • g) := ne_bot.map₂ @[to_additive] lemma ne_bot.of_smul_left : (f • g).ne_bot → f.ne_bot := ne_bot.of_map₂_left @[to_additive] lemma ne_bot.of_smul_right : (f • g).ne_bot → g.ne_bot := ne_bot.of_map₂_right @[simp, to_additive] lemma pure_smul : (pure a : filter α) • g = g.map ((•) a) := map₂_pure_left @[simp, to_additive] lemma smul_pure : f • pure b = f.map (• b) := map₂_pure_right @[simp, to_additive] lemma pure_smul_pure : (pure a : filter α) • (pure b : filter β) = pure (a • b) := map₂_pure @[to_additive] lemma smul_le_smul : f₁ ≤ f₂ → g₁ ≤ g₂ → f₁ • g₁ ≤ f₂ • g₂ := map₂_mono @[to_additive] lemma smul_le_smul_left : g₁ ≤ g₂ → f • g₁ ≤ f • g₂ := map₂_mono_left @[to_additive] lemma smul_le_smul_right : f₁ ≤ f₂ → f₁ • g ≤ f₂ • g := map₂_mono_right @[simp, to_additive] lemma le_smul_iff : h ≤ f • g ↔ ∀ ⦃s⦄, s ∈ f → ∀ ⦃t⦄, t ∈ g → s • t ∈ h := le_map₂_iff @[to_additive] instance covariant_smul : covariant_class (filter α) (filter β) (•) (≤) := ⟨λ f g h, map₂_mono_left⟩ end smul /-! ### Scalar subtraction of filters -/ section vsub variables [has_vsub α β] {f f₁ f₂ g g₁ g₂ : filter β} {h : filter α} {s t : set β} {a b : β} include α /-- The filter `f -ᵥ g` is generated by `{s -ᵥ t | s ∈ f, t ∈ g}` in locale `pointwise`. -/ protected def has_vsub : has_vsub (filter α) (filter β) := /- This is defeq to `map₂ (-ᵥ) f g`, but the hypothesis unfolds to `t₁ -ᵥ t₂ ⊆ s` rather than all the way to `set.image2 (-ᵥ) t₁ t₂ ⊆ s`. -/ ⟨λ f g, { sets := {s | ∃ t₁ t₂, t₁ ∈ f ∧ t₂ ∈ g ∧ t₁ -ᵥ t₂ ⊆ s}, ..map₂ (-ᵥ) f g }⟩ localized "attribute [instance] filter.has_vsub" in pointwise @[simp] lemma map₂_vsub : map₂ (-ᵥ) f g = f -ᵥ g := rfl lemma mem_vsub {s : set α} : s ∈ f -ᵥ g ↔ ∃ t₁ t₂, t₁ ∈ f ∧ t₂ ∈ g ∧ t₁ -ᵥ t₂ ⊆ s := iff.rfl lemma vsub_mem_vsub : s ∈ f → t ∈ g → s -ᵥ t ∈ f -ᵥ g := image2_mem_map₂ @[simp] lemma bot_vsub : (⊥ : filter β) -ᵥ g = ⊥ := map₂_bot_left @[simp] lemma vsub_bot : f -ᵥ (⊥ : filter β) = ⊥ := map₂_bot_right @[simp] lemma vsub_eq_bot_iff : f -ᵥ g = ⊥ ↔ f = ⊥ ∨ g = ⊥ := map₂_eq_bot_iff @[simp] lemma vsub_ne_bot_iff : (f -ᵥ g : filter α).ne_bot ↔ f.ne_bot ∧ g.ne_bot := map₂_ne_bot_iff lemma ne_bot.vsub : ne_bot f → ne_bot g → ne_bot (f -ᵥ g) := ne_bot.map₂ lemma ne_bot.of_vsub_left : (f -ᵥ g : filter α).ne_bot → f.ne_bot := ne_bot.of_map₂_left lemma ne_bot.of_vsub_right : (f -ᵥ g : filter α).ne_bot → g.ne_bot := ne_bot.of_map₂_right @[simp] lemma pure_vsub : (pure a : filter β) -ᵥ g = g.map ((-ᵥ) a) := map₂_pure_left @[simp] lemma vsub_pure : f -ᵥ pure b = f.map (-ᵥ b) := map₂_pure_right @[simp] lemma pure_vsub_pure : (pure a : filter β) -ᵥ pure b = (pure (a -ᵥ b) : filter α) := map₂_pure lemma vsub_le_vsub : f₁ ≤ f₂ → g₁ ≤ g₂ → f₁ -ᵥ g₁ ≤ f₂ -ᵥ g₂ := map₂_mono lemma vsub_le_vsub_left : g₁ ≤ g₂ → f -ᵥ g₁ ≤ f -ᵥ g₂ := map₂_mono_left lemma vsub_le_vsub_right : f₁ ≤ f₂ → f₁ -ᵥ g ≤ f₂ -ᵥ g := map₂_mono_right @[simp] lemma le_vsub_iff : h ≤ f -ᵥ g ↔ ∀ ⦃s⦄, s ∈ f → ∀ ⦃t⦄, t ∈ g → s -ᵥ t ∈ h := le_map₂_iff end vsub /-! ### Translation/scaling of filters -/ section smul variables [has_smul α β] {f f₁ f₂ : filter β} {s : set β} {a : α} /-- `a • f` is the map of `f` under `a •` in locale `pointwise`. -/ @[to_additive filter.has_vadd_filter "`a +ᵥ f` is the map of `f` under `a +ᵥ` in locale `pointwise`."] protected def has_smul_filter : has_smul α (filter β) := ⟨λ a, map ((•) a)⟩ localized "attribute [instance] filter.has_smul_filter filter.has_vadd_filter" in pointwise @[simp, to_additive] lemma map_smul : map (λ b, a • b) f = a • f := rfl @[to_additive] lemma mem_smul_filter : s ∈ a • f ↔ (•) a ⁻¹' s ∈ f := iff.rfl @[to_additive] lemma smul_set_mem_smul_filter : s ∈ f → a • s ∈ a • f := image_mem_map @[simp, to_additive] lemma smul_filter_bot : a • (⊥ : filter β) = ⊥ := map_bot @[simp, to_additive] lemma smul_filter_eq_bot_iff : a • f = ⊥ ↔ f = ⊥ := map_eq_bot_iff @[simp, to_additive] lemma smul_filter_ne_bot_iff : (a • f).ne_bot ↔ f.ne_bot := map_ne_bot_iff _ @[to_additive] lemma ne_bot.smul_filter : f.ne_bot → (a • f).ne_bot := λ h, h.map _ @[to_additive] lemma ne_bot.of_smul_filter : (a • f).ne_bot → f.ne_bot := ne_bot.of_map @[to_additive] lemma smul_filter_le_smul_filter (hf : f₁ ≤ f₂) : a • f₁ ≤ a • f₂ := map_mono hf @[to_additive] instance covariant_smul_filter : covariant_class α (filter β) (•) (≤) := ⟨λ f, map_mono⟩ end smul open_locale pointwise @[to_additive] instance smul_comm_class_filter [has_smul α γ] [has_smul β γ] [smul_comm_class α β γ] : smul_comm_class α β (filter γ) := ⟨λ _ _ _, map_comm (funext $ smul_comm _ _) _⟩ @[to_additive] instance smul_comm_class_filter' [has_smul α γ] [has_smul β γ] [smul_comm_class α β γ] : smul_comm_class α (filter β) (filter γ) := ⟨λ a f g, map_map₂_distrib_right $ smul_comm a⟩ @[to_additive] instance smul_comm_class_filter'' [has_smul α γ] [has_smul β γ] [smul_comm_class α β γ] : smul_comm_class (filter α) β (filter γ) := by haveI := smul_comm_class.symm α β γ; exact smul_comm_class.symm _ _ _ @[to_additive] instance smul_comm_class [has_smul α γ] [has_smul β γ] [smul_comm_class α β γ] : smul_comm_class (filter α) (filter β) (filter γ) := ⟨λ f g h, map₂_left_comm smul_comm⟩ @[to_additive] instance is_scalar_tower [has_smul α β] [has_smul α γ] [has_smul β γ] [is_scalar_tower α β γ] : is_scalar_tower α β (filter γ) := ⟨λ a b f, by simp only [←map_smul, map_map, smul_assoc]⟩ @[to_additive] instance is_scalar_tower' [has_smul α β] [has_smul α γ] [has_smul β γ] [is_scalar_tower α β γ] : is_scalar_tower α (filter β) (filter γ) := ⟨λ a f g, by { refine (map_map₂_distrib_left $ λ _ _, _).symm, exact (smul_assoc a _ _).symm }⟩ @[to_additive] instance is_scalar_tower'' [has_smul α β] [has_smul α γ] [has_smul β γ] [is_scalar_tower α β γ] : is_scalar_tower (filter α) (filter β) (filter γ) := ⟨λ f g h, map₂_assoc smul_assoc⟩ instance is_central_scalar [has_smul α β] [has_smul αᵐᵒᵖ β] [is_central_scalar α β] : is_central_scalar α (filter β) := ⟨λ a f, congr_arg (λ m, map m f) $ by exact funext (λ _, op_smul_eq_smul _ _)⟩ /-- A multiplicative action of a monoid `α` on a type `β` gives a multiplicative action of `filter α` on `filter β`. -/ @[to_additive "An additive action of an additive monoid `α` on a type `β` gives an additive action of `filter α` on `filter β`"] protected def mul_action [monoid α] [mul_action α β] : mul_action (filter α) (filter β) := { one_smul := λ f, map₂_pure_left.trans $ by simp_rw [one_smul, map_id'], mul_smul := λ f g h, map₂_assoc mul_smul } /-- A multiplicative action of a monoid on a type `β` gives a multiplicative action on `filter β`. -/ @[to_additive "An additive action of an additive monoid on a type `β` gives an additive action on `filter β`."] protected def mul_action_filter [monoid α] [mul_action α β] : mul_action α (filter β) := { mul_smul := λ a b f, by simp only [←map_smul, map_map, function.comp, ←mul_smul], one_smul := λ f, by simp only [←map_smul, one_smul, map_id'] } localized "attribute [instance] filter.mul_action filter.add_action filter.mul_action_filter filter.add_action_filter" in pointwise /-- A distributive multiplicative action of a monoid on an additive monoid `β` gives a distributive multiplicative action on `filter β`. -/ protected def distrib_mul_action_filter [monoid α] [add_monoid β] [distrib_mul_action α β] : distrib_mul_action α (filter β) := { smul_add := λ _ _ _, map_map₂_distrib $ smul_add _, smul_zero := λ _, (map_pure _ _).trans $ by rw [smul_zero, pure_zero] } /-- A multiplicative action of a monoid on a monoid `β` gives a multiplicative action on `set β`. -/ protected def mul_distrib_mul_action_filter [monoid α] [monoid β] [mul_distrib_mul_action α β] : mul_distrib_mul_action α (set β) := { smul_mul := λ _ _ _, image_image2_distrib $ smul_mul' _, smul_one := λ _, image_singleton.trans $ by rw [smul_one, singleton_one] } localized "attribute [instance] filter.distrib_mul_action_filter filter.mul_distrib_mul_action_filter" in pointwise section smul_with_zero variables [has_zero α] [has_zero β] [smul_with_zero α β] {f : filter α} {g : filter β} /-! Note that we have neither `smul_with_zero α (filter β)` nor `smul_with_zero (filter α) (filter β)` because `0 * ⊥ ≠ 0`. -/ lemma ne_bot.smul_zero_nonneg (hf : f.ne_bot) : 0 ≤ f • (0 : filter β) := le_smul_iff.2 $ λ t₁ h₁ t₂ h₂, let ⟨a, ha⟩ := hf.nonempty_of_mem h₁ in ⟨_, _, ha, h₂, smul_zero' _ _⟩ lemma ne_bot.zero_smul_nonneg (hg : g.ne_bot) : 0 ≤ (0 : filter α) • g := le_smul_iff.2 $ λ t₁ h₁ t₂ h₂, let ⟨b, hb⟩ := hg.nonempty_of_mem h₂ in ⟨_, _, h₁, hb, zero_smul _ _⟩ lemma zero_smul_filter_nonpos : (0 : α) • g ≤ 0 := begin refine λ s hs, mem_smul_filter.2 _, convert univ_mem, refine eq_univ_iff_forall.2 (λ a, _), rwa [mem_preimage, zero_smul], end lemma zero_smul_filter (hg : g.ne_bot) : (0 : α) • g = 0 := zero_smul_filter_nonpos.antisymm $ le_map_iff.2 $ λ s hs, begin simp_rw [set.image_eta, zero_smul, (hg.nonempty_of_mem hs).image_const], exact zero_mem_zero, end end smul_with_zero end filter
5746d4167beea76077f302ca47952a38c89ade36
9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e
/src/data/function_transfer.lean
008d2a44d5d97747c877911984ef43ce76eea781
[]
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
1,132
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland The formation of function types `α → β` is functorial with respect to equivalences of `α` and `β`. This should be placed in some more general context. -/ import data.equiv.basic open equiv universes u v w x def function_equiv_of_equiv {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} (f : α ≃ γ) (g : β ≃ δ) : (α → β) ≃ (γ → δ) := { to_fun := λ (u : α → β), g ∘ u ∘ f.inv_fun, inv_fun := λ (v : γ → δ), g.inv_fun ∘ v ∘ f, left_inv := begin unfold function.left_inverse, intro u, ext a, simp, by calc g.inv_fun (g.to_fun (u (f.inv_fun (f.to_fun a)))) = u (f.inv_fun (f.to_fun a)) : g.left_inv _ ... = u a : congr_arg u (f.left_inv a), end, right_inv := begin unfold function.right_inverse, intro v, ext c, simp, by calc g.to_fun (g.inv_fun (v (f.to_fun (f.inv_fun c)))) = v (f.to_fun (f.inv_fun c)) : g.right_inv _ ... = v c : congr_arg v (f.right_inv c) end }
d1c815cefe68cdf75f02a9cdb4a9a3e5159cea4d
4727251e0cd73359b15b664c3170e5d754078599
/src/number_theory/cyclotomic/basic.lean
df2ab5d3a0fb8f55292336df07503b3948ea1470
[ "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
27,045
lean
/- Copyright (c) 2021 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import ring_theory.polynomial.cyclotomic.basic import number_theory.number_field import algebra.char_p.algebra import field_theory.galois import analysis.complex.polynomial /-! # Cyclotomic extensions Let `A` and `B` be commutative rings with `algebra A B`. For `S : set ℕ+`, we define a class `is_cyclotomic_extension S A B` expressing the fact that `B` is obtained from `A` by adding `n`-th primitive roots of unity, for all `n ∈ S`. ## Main definitions * `is_cyclotomic_extension S A B` : means that `B` is obtained from `A` by adding `n`-th primitive roots of unity, for all `n ∈ S`. * `cyclotomic_field`: given `n : ℕ+` and a field `K`, we define `cyclotomic n K` as the splitting field of `cyclotomic n K`. If `n` is nonzero in `K`, it has the instance `is_cyclotomic_extension {n} K (cyclotomic_field n K)`. * `cyclotomic_ring` : if `A` is a domain with fraction field `K` and `n : ℕ+`, we define `cyclotomic_ring n A K` as the `A`-subalgebra of `cyclotomic_field n K` generated by the roots of `X ^ n - 1`. If `n` is nonzero in `A`, it has the instance `is_cyclotomic_extension {n} A (cyclotomic_ring n A K)`. ## Main results * `is_cyclotomic_extension.trans` : if `is_cyclotomic_extension S A B` and `is_cyclotomic_extension T B C`, then `is_cyclotomic_extension (S ∪ T) A C`. * `is_cyclotomic_extension.union_right` : given `is_cyclotomic_extension (S ∪ T) A B`, then `is_cyclotomic_extension T (adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 }) B`. * `is_cyclotomic_extension.union_right` : given `is_cyclotomic_extension T A B` and `S ⊆ T`, then `is_cyclotomic_extension S A (adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 })`. * `is_cyclotomic_extension.finite` : if `S` is finite and `is_cyclotomic_extension S A B`, then `B` is a finite `A`-algebra. * `is_cyclotomic_extension.number_field` : a finite cyclotomic extension of a number field is a number field. * `is_cyclotomic_extension.splitting_field_X_pow_sub_one` : if `is_cyclotomic_extension {n} K L` and `ne_zero ((n : ℕ) : K)`, then `L` is the splitting field of `X ^ n - 1`. * `is_cyclotomic_extension.splitting_field_cyclotomic` : if `is_cyclotomic_extension {n} K L` and `ne_zero ((n : ℕ) : K)`, then `L` is the splitting field of `cyclotomic n K`. ## Implementation details Our definition of `is_cyclotomic_extension` is very general, to allow rings of any characteristic and infinite extensions, but it will mainly be used in the case `S = {n}` with `(n : A) ≠ 0` (and for integral domains). All results are in the `is_cyclotomic_extension` namespace. Note that some results, for example `is_cyclotomic_extension.trans`, `is_cyclotomic_extension.finite`, `is_cyclotomic_extension.number_field`, `is_cyclotomic_extension.finite_dimensional`, `is_cyclotomic_extension.is_galois` and `cyclotomic_field.algebra_base` are lemmas, but they can be made local instances. Some of them are included in the `cyclotomic` locale. -/ open polynomial algebra finite_dimensional module set open_locale big_operators universes u v w z variables (n : ℕ+) (S T : set ℕ+) (A : Type u) (B : Type v) (K : Type w) (L : Type z) variables [comm_ring A] [comm_ring B] [algebra A B] variables [field K] [field L] [algebra K L] noncomputable theory /-- Given an `A`-algebra `B` and `S : set ℕ+`, we define `is_cyclotomic_extension S A B` requiring that `cyclotomic a A` has a root in `B` for all `a ∈ S` and that `B` is generated over `A` by the roots of `X ^ n - 1`. -/ @[mk_iff] class is_cyclotomic_extension : Prop := (exists_root {a : ℕ+} (ha : a ∈ S) : ∃ r : B, aeval r (cyclotomic a A) = 0) (adjoin_roots : ∀ (x : B), x ∈ adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 }) namespace is_cyclotomic_extension section basic /-- A reformulation of `is_cyclotomic_extension` that uses `⊤`. -/ lemma iff_adjoin_eq_top : is_cyclotomic_extension S A B ↔ (∀ (a : ℕ+), a ∈ S → ∃ r : B, aeval r (cyclotomic a A) = 0) ∧ (adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 } = ⊤) := ⟨λ h, ⟨h.exists_root, algebra.eq_top_iff.2 h.adjoin_roots⟩, λ h, ⟨h.1, algebra.eq_top_iff.1 h.2⟩⟩ /-- A reformulation of `is_cyclotomic_extension` in the case `S` is a singleton. -/ lemma iff_singleton : is_cyclotomic_extension {n} A B ↔ (∃ r : B, aeval r (cyclotomic n A) = 0) ∧ (∀ x, x ∈ adjoin A { b : B | b ^ (n : ℕ) = 1 }) := by simp [is_cyclotomic_extension_iff] /-- If `is_cyclotomic_extension ∅ A B`, then `A = B`. -/ lemma empty [h : is_cyclotomic_extension ∅ A B] : (⊥ : subalgebra A B) = ⊤ := by simpa [algebra.eq_top_iff, is_cyclotomic_extension_iff] using h /-- If `is_cyclotomic_extension {1} A B`, then `A = B`. -/ lemma singleton_one [h : is_cyclotomic_extension {1} A B] : (⊥ : subalgebra A B) = ⊤ := algebra.eq_top_iff.2 (λ x, by simpa [adjoin_singleton_one] using ((is_cyclotomic_extension_iff _ _ _).1 h).2 x) /-- Transitivity of cyclotomic extensions. -/ lemma trans (C : Type w) [comm_ring C] [algebra A C] [algebra B C] [is_scalar_tower A B C] [hS : is_cyclotomic_extension S A B] [hT : is_cyclotomic_extension T B C] : is_cyclotomic_extension (S ∪ T) A C := begin refine ⟨λ n hn, _, λ x, _⟩, { cases hn, { obtain ⟨b, hb⟩ := ((is_cyclotomic_extension_iff _ _ _).1 hS).1 hn, refine ⟨algebra_map B C b, _⟩, replace hb := congr_arg (algebra_map B C) hb, rw [aeval_def, eval₂_eq_eval_map, map_cyclotomic, ring_hom.map_zero, ← eval₂_at_apply, eval₂_eq_eval_map, map_cyclotomic] at hb, rwa [aeval_def, eval₂_eq_eval_map, map_cyclotomic] }, { obtain ⟨c, hc⟩ := ((is_cyclotomic_extension_iff _ _ _).1 hT).1 hn, refine ⟨c, _⟩, rw [aeval_def, eval₂_eq_eval_map, map_cyclotomic] at hc, rwa [aeval_def, eval₂_eq_eval_map, map_cyclotomic] } }, { refine adjoin_induction (((is_cyclotomic_extension_iff _ _ _).1 hT).2 x) (λ c ⟨n, hn⟩, subset_adjoin ⟨n, or.inr hn.1, hn.2⟩) (λ b, _) (λ x y hx hy, subalgebra.add_mem _ hx hy) (λ x y hx hy, subalgebra.mul_mem _ hx hy), { let f := is_scalar_tower.to_alg_hom A B C, have hb : f b ∈ (adjoin A { b : B | ∃ (a : ℕ+), a ∈ S ∧ b ^ (a : ℕ) = 1 }).map f := ⟨b, ((is_cyclotomic_extension_iff _ _ _).1 hS).2 b, rfl⟩, rw [is_scalar_tower.to_alg_hom_apply, ← adjoin_image] at hb, refine adjoin_mono (λ y hy, _) hb, obtain ⟨b₁, ⟨⟨n, hn⟩, h₁⟩⟩ := hy, exact ⟨n, ⟨mem_union_left T hn.1, by rw [← h₁, ← alg_hom.map_pow, hn.2, alg_hom.map_one]⟩⟩ } } end /-- If `B` is a cyclotomic extension of `A` given by roots of unity of order in `S ∪ T`, then `B` is a cyclotomic extension of `adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 } ` given by roots of unity of order in `T`. -/ lemma union_right [h : is_cyclotomic_extension (S ∪ T) A B] : is_cyclotomic_extension T (adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 }) B := begin have : { b : B | ∃ (n : ℕ+), n ∈ S ∪ T ∧ b ^ (n : ℕ) = 1 } = { b : B | ∃ (n : ℕ+), n ∈ S ∧ b ^ (n : ℕ) = 1 } ∪ { b : B | ∃ (n : ℕ+), n ∈ T ∧ b ^ (n : ℕ) = 1 }, { refine le_antisymm (λ x hx, _) (λ x hx, _), { rcases hx with ⟨n, hn₁ | hn₂, hnpow⟩, { left, exact ⟨n, hn₁, hnpow⟩ }, { right, exact ⟨n, hn₂, hnpow⟩ } }, { rcases hx with ⟨n, hn⟩ | ⟨n, hn⟩, { exact ⟨n, or.inl hn.1, hn.2⟩ }, { exact ⟨n, or.inr hn.1, hn.2⟩ } } }, refine ⟨λ n hn, _, λ b, _⟩, { obtain ⟨b, hb⟩ := ((is_cyclotomic_extension_iff _ _ _).1 h).1 (mem_union_right S hn), refine ⟨b, _⟩, rw [aeval_def, eval₂_eq_eval_map, map_cyclotomic] at hb, rwa [aeval_def, eval₂_eq_eval_map, map_cyclotomic] }, { replace h := ((is_cyclotomic_extension_iff _ _ _).1 h).2 b, rwa [this, adjoin_union_eq_adjoin_adjoin, subalgebra.mem_restrict_scalars] at h } end /-- If `B` is a cyclotomic extension of `A` given by roots of unity of order in `T` and `S ⊆ T`, then `adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 }` is a cyclotomic extension of `B` given by roots of unity of order in `S`. -/ lemma union_left [h : is_cyclotomic_extension T A B] (hS : S ⊆ T) : is_cyclotomic_extension S A (adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 }) := begin refine ⟨λ n hn, _, λ b, _⟩, { obtain ⟨b, hb⟩ := ((is_cyclotomic_extension_iff _ _ _).1 h).1 (hS hn), refine ⟨⟨b, subset_adjoin ⟨n, hn, _⟩⟩, _⟩, { rw [aeval_def, eval₂_eq_eval_map, map_cyclotomic, ← is_root.def] at hb, suffices : (X ^ (n : ℕ) - 1).is_root b, { simpa [sub_eq_zero] using this }, exact hb.dvd (cyclotomic.dvd_X_pow_sub_one _ _) }, rwa [← subalgebra.coe_eq_zero, aeval_subalgebra_coe, subtype.coe_mk] }, { convert mem_top, rw [← adjoin_adjoin_coe_preimage, preimage_set_of_eq], norm_cast, } end end basic section fintype lemma finite_of_singleton [is_domain B] [h : is_cyclotomic_extension {n} A B] : finite A B := begin classical, rw [module.finite_def, ← top_to_submodule, ← ((iff_adjoin_eq_top _ _ _).1 h).2], refine fg_adjoin_of_finite _ (λ b hb, _), { simp only [mem_singleton_iff, exists_eq_left], have : {b : B | b ^ (n : ℕ) = 1} = (nth_roots n (1 : B)).to_finset := set.ext (λ x, ⟨λ h, by simpa using h, λ h, by simpa using h⟩), rw [this], exact (nth_roots ↑n 1).to_finset.finite_to_set }, { simp only [mem_singleton_iff, exists_eq_left, mem_set_of_eq] at hb, refine ⟨X ^ (n : ℕ) - 1, ⟨monic_X_pow_sub_C _ n.pos.ne.symm, by simp [hb]⟩⟩ } end /-- If `S` is finite and `is_cyclotomic_extension S A B`, then `B` is a finite `A`-algebra. -/ lemma finite [is_domain B] [h₁ : fintype S] [h₂ : is_cyclotomic_extension S A B] : finite A B := begin unfreezingI {revert h₂ A B}, refine set.finite.induction_on (set.finite.intro h₁) (λ A B, _) (λ n S hn hS H A B, _), { introsI _ _ _ _ _, refine module.finite_def.2 ⟨({1} : finset B), _⟩, simp [← top_to_submodule, ← empty, to_submodule_bot] }, { introsI _ _ _ _ h, haveI : is_cyclotomic_extension S A (adjoin A { b : B | ∃ (n : ℕ+), n ∈ S ∧ b ^ (n : ℕ) = 1 }) := union_left _ (insert n S) _ _ (subset_insert n S), haveI := H A (adjoin A { b : B | ∃ (n : ℕ+), n ∈ S ∧ b ^ (n : ℕ) = 1 }), haveI : finite (adjoin A { b : B | ∃ (n : ℕ+), n ∈ S ∧ b ^ (n : ℕ) = 1 }) B, { rw [← union_singleton] at h, letI := @union_right S {n} A B _ _ _ h, exact finite_of_singleton n _ _ }, exact finite.trans (adjoin A { b : B | ∃ (n : ℕ+), n ∈ S ∧ b ^ (n : ℕ) = 1 }) _ } end /-- A cyclotomic finite extension of a number field is a number field. -/ lemma number_field [h : number_field K] [fintype S] [is_cyclotomic_extension S K L] : number_field L := { to_char_zero := char_zero_of_injective_algebra_map (algebra_map K L).injective, to_finite_dimensional := @finite.trans _ K L _ _ _ _ (@algebra_rat L _ (char_zero_of_injective_algebra_map (algebra_map K L).injective)) _ _ h.to_finite_dimensional (finite S K L) } localized "attribute [instance] is_cyclotomic_extension.number_field" in cyclotomic /-- A finite cyclotomic extension of an integral noetherian domain is integral -/ lemma integral [is_domain B] [is_noetherian_ring A] [fintype S] [is_cyclotomic_extension S A B] : algebra.is_integral A B := is_integral_of_noetherian $ is_noetherian_of_fg_of_noetherian' $ (finite S A B).out /-- If `S` is finite and `is_cyclotomic_extension S K A`, then `finite_dimensional K A`. -/ lemma finite_dimensional (C : Type z) [fintype S] [comm_ring C] [algebra K C] [is_domain C] [is_cyclotomic_extension S K C] : finite_dimensional K C := finite S K C localized "attribute [instance] is_cyclotomic_extension.finite_dimensional" in cyclotomic end fintype section variables {A B} lemma adjoin_roots_cyclotomic_eq_adjoin_nth_roots [decidable_eq B] [is_domain B] {ζ : B} (hζ : is_primitive_root ζ n) : adjoin A ↑((map (algebra_map A B) (cyclotomic n A)).roots.to_finset) = adjoin A {b : B | ∃ (a : ℕ+), a ∈ ({n} : set ℕ+) ∧ b ^ (a : ℕ) = 1} := begin simp only [mem_singleton_iff, exists_eq_left, map_cyclotomic], refine le_antisymm (adjoin_mono (λ x hx, _)) (adjoin_le (λ x hx, _)), { simp only [multiset.mem_to_finset, finset.mem_coe, map_cyclotomic, mem_roots (cyclotomic_ne_zero n B)] at hx, simp only [mem_singleton_iff, exists_eq_left, mem_set_of_eq], rw is_root_of_unity_iff n.pos, exact ⟨n, nat.mem_divisors_self n n.ne_zero, hx⟩ }, { simp only [mem_singleton_iff, exists_eq_left, mem_set_of_eq] at hx, obtain ⟨i, hin, rfl⟩ := hζ.eq_pow_of_pow_eq_one hx n.pos, refine set_like.mem_coe.2 (subalgebra.pow_mem _ (subset_adjoin _) _), rwa [finset.mem_coe, multiset.mem_to_finset, mem_roots $ cyclotomic_ne_zero n B], exact hζ.is_root_cyclotomic n.pos } end lemma adjoin_roots_cyclotomic_eq_adjoin_root_cyclotomic [decidable_eq B] [is_domain B] (ζ : B) (hζ : is_primitive_root ζ n) : adjoin A (((map (algebra_map A B) (cyclotomic n A)).roots.to_finset) : set B) = adjoin A ({ζ}) := begin refine le_antisymm (adjoin_le (λ x hx, _)) (adjoin_mono (λ x hx, _)), { suffices hx : x ^ ↑n = 1, obtain ⟨i, hin, rfl⟩ := hζ.eq_pow_of_pow_eq_one hx n.pos, exact set_like.mem_coe.2 (subalgebra.pow_mem _ (subset_adjoin $ mem_singleton ζ) _), rw is_root_of_unity_iff n.pos, refine ⟨n, nat.mem_divisors_self n n.ne_zero, _⟩, rwa [finset.mem_coe, multiset.mem_to_finset, map_cyclotomic, mem_roots $ cyclotomic_ne_zero n B] at hx }, { simp only [mem_singleton_iff, exists_eq_left, mem_set_of_eq] at hx, simpa only [hx, multiset.mem_to_finset, finset.mem_coe, map_cyclotomic, mem_roots (cyclotomic_ne_zero n B)] using hζ.is_root_cyclotomic n.pos } end lemma adjoin_primitive_root_eq_top [is_domain B] [h : is_cyclotomic_extension {n} A B] (ζ : B) (hζ : is_primitive_root ζ n) : adjoin A ({ζ} : set B) = ⊤ := begin classical, rw ←adjoin_roots_cyclotomic_eq_adjoin_root_cyclotomic n ζ hζ, rw adjoin_roots_cyclotomic_eq_adjoin_nth_roots n hζ, exact ((iff_adjoin_eq_top {n} A B).mp h).2, end variable (A) lemma _root_.is_primitive_root.adjoin_is_cyclotomic_extension [is_domain B] {ζ : B} {n : ℕ+} (h : is_primitive_root ζ n) : is_cyclotomic_extension {n} A (adjoin A ({ζ} : set B)) := { exists_root := λ i hi, begin rw [set.mem_singleton_iff] at hi, refine ⟨⟨ζ, subset_adjoin $ set.mem_singleton ζ⟩, _⟩, replace h := h.is_root_cyclotomic n.pos, rw [is_root.def, ← map_cyclotomic _ (algebra_map A B), eval_map, ← aeval_def, ← hi] at h, rwa [← subalgebra.coe_eq_zero, aeval_subalgebra_coe, subtype.coe_mk] end, adjoin_roots := λ x, begin refine adjoin_induction' (λ b hb, _) (λ a, _) (λ b₁ b₂ hb₁ hb₂, _) (λ b₁ b₂ hb₁ hb₂, _) x, { rw [set.mem_singleton_iff] at hb, refine subset_adjoin _, simp only [mem_singleton_iff, exists_eq_left, mem_set_of_eq, hb], rw [← subalgebra.coe_eq_one, subalgebra.coe_pow, set_like.coe_mk], exact ((is_primitive_root.iff_def ζ n).1 h).1 }, { exact subalgebra.algebra_map_mem _ _ }, { exact subalgebra.add_mem _ hb₁ hb₂ }, { exact subalgebra.mul_mem _ hb₁ hb₂ } end } end section field variable [ne_zero ((n : ℕ) : K)] /-- A cyclotomic extension splits `X ^ n - 1` if `n ∈ S` and `ne_zero (n : K)`.-/ lemma splits_X_pow_sub_one [H : is_cyclotomic_extension S K L] (hS : n ∈ S) : splits (algebra_map K L) (X ^ (n : ℕ) - 1) := begin rw [← splits_id_iff_splits, polynomial.map_sub, polynomial.map_one, polynomial.map_pow, polynomial.map_X], obtain ⟨z, hz⟩ := ((is_cyclotomic_extension_iff _ _ _).1 H).1 hS, rw [aeval_def, eval₂_eq_eval_map, map_cyclotomic] at hz, haveI := ne_zero.of_no_zero_smul_divisors K L n, exact X_pow_sub_one_splits (is_root_cyclotomic_iff.1 hz), end /-- A cyclotomic extension splits `cyclotomic n K` if `n ∈ S` and `ne_zero (n : K)`.-/ lemma splits_cyclotomic [is_cyclotomic_extension S K L] (hS : n ∈ S) : splits (algebra_map K L) (cyclotomic n K) := begin refine splits_of_splits_of_dvd _ (X_pow_sub_C_ne_zero n.pos _) (splits_X_pow_sub_one n S K L hS) _, use (∏ (i : ℕ) in (n : ℕ).proper_divisors, polynomial.cyclotomic i K), rw [(eq_cyclotomic_iff n.pos _).1 rfl, ring_hom.map_one], end section singleton variables [is_cyclotomic_extension {n} K L] /-- If `is_cyclotomic_extension {n} K L` and `ne_zero ((n : ℕ) : K)`, then `L` is the splitting field of `X ^ n - 1`. -/ lemma splitting_field_X_pow_sub_one : is_splitting_field K L (X ^ (n : ℕ) - 1) := { splits := splits_X_pow_sub_one n {n} K L (mem_singleton n), adjoin_roots := begin rw [← ((iff_adjoin_eq_top {n} K L).1 infer_instance).2], congr, refine set.ext (λ x, _), simp only [polynomial.map_pow, mem_singleton_iff, multiset.mem_to_finset, exists_eq_left, mem_set_of_eq, polynomial.map_X, polynomial.map_one, finset.mem_coe, polynomial.map_sub], rwa [← ring_hom.map_one C, mem_roots (@X_pow_sub_C_ne_zero _ _ (field.to_nontrivial L) _ n.pos _), is_root.def, eval_sub, eval_pow, eval_C, eval_X, sub_eq_zero] end } localized "attribute [instance] is_cyclotomic_extension.splitting_field_X_pow_sub_one" in cyclotomic include n lemma is_galois : is_galois K L := begin letI := splitting_field_X_pow_sub_one n K L, exact is_galois.of_separable_splitting_field (X_pow_sub_one_separable_iff.2 (ne_zero.ne _ : ((n : ℕ) : K) ≠ 0)), end localized "attribute [instance] is_cyclotomic_extension.is_galois" in cyclotomic /-- If `is_cyclotomic_extension {n} K L` and `ne_zero ((n : ℕ) : K)`, then `L` is the splitting field of `cyclotomic n K`. -/ lemma splitting_field_cyclotomic : is_splitting_field K L (cyclotomic n K) := { splits := splits_cyclotomic n {n} K L (mem_singleton n), adjoin_roots := begin rw [← ((iff_adjoin_eq_top {n} K L).1 infer_instance).2], letI := classical.dec_eq L, obtain ⟨ζ, hζ⟩ := @is_cyclotomic_extension.exists_root {n} K L _ _ _ _ _ (mem_singleton n), haveI : ne_zero ((n : ℕ) : L) := ne_zero.nat_of_injective (algebra_map K L).injective, rw [aeval_def, eval₂_eq_eval_map, map_cyclotomic, ← is_root.def, is_root_cyclotomic_iff] at hζ, refine adjoin_roots_cyclotomic_eq_adjoin_nth_roots n hζ end } localized "attribute [instance] is_cyclotomic_extension.splitting_field_cyclotomic" in cyclotomic end singleton end field end is_cyclotomic_extension section cyclotomic_field /-- Given `n : ℕ+` and a field `K`, we define `cyclotomic_field n K` as the splitting field of `cyclotomic n K`. If `n` is nonzero in `K`, it has the instance `is_cyclotomic_extension {n} K (cyclotomic_field n K)`. -/ @[derive [field, algebra K, inhabited]] def cyclotomic_field : Type w := (cyclotomic n K).splitting_field namespace cyclotomic_field instance is_cyclotomic_extension [ne_zero ((n : ℕ) : K)] : is_cyclotomic_extension {n} K (cyclotomic_field n K) := { exists_root := λ a han, begin rw mem_singleton_iff at han, subst a, exact exists_root_of_splits _ (splitting_field.splits _) (degree_cyclotomic_pos n K (n.pos)).ne' end, adjoin_roots := begin rw [←algebra.eq_top_iff, ←splitting_field.adjoin_roots, eq_comm], letI := classical.dec_eq (cyclotomic_field n K), obtain ⟨ζ, hζ⟩ := exists_root_of_splits _ (splitting_field.splits (cyclotomic n K)) (degree_cyclotomic_pos n _ n.pos).ne', haveI : ne_zero ((n : ℕ) : (cyclotomic_field n K)) := ne_zero.nat_of_injective (algebra_map K _).injective, rw [eval₂_eq_eval_map, map_cyclotomic, ← is_root.def, is_root_cyclotomic_iff] at hζ, exact is_cyclotomic_extension.adjoin_roots_cyclotomic_eq_adjoin_nth_roots n hζ, end } end cyclotomic_field end cyclotomic_field section is_domain variables [is_domain A] [algebra A K] [is_fraction_ring A K] section cyclotomic_ring /-- If `K` is the fraction field of `A`, the `A`-algebra structure on `cyclotomic_field n K`. This is not an instance since it causes diamonds when `A = ℤ`. -/ @[nolint unused_arguments] def cyclotomic_field.algebra_base : algebra A (cyclotomic_field n K) := ((algebra_map K (cyclotomic_field n K)).comp (algebra_map A K)).to_algebra local attribute [instance] cyclotomic_field.algebra_base instance cyclotomic_field.no_zero_smul_divisors : no_zero_smul_divisors A (cyclotomic_field n K) := no_zero_smul_divisors.of_algebra_map_injective $ function.injective.comp (no_zero_smul_divisors.algebra_map_injective _ _) $ is_fraction_ring.injective A K /-- If `A` is a domain with fraction field `K` and `n : ℕ+`, we define `cyclotomic_ring n A K` as the `A`-subalgebra of `cyclotomic_field n K` generated by the roots of `X ^ n - 1`. If `n` is nonzero in `A`, it has the instance `is_cyclotomic_extension {n} A (cyclotomic_ring n A K)`. -/ @[derive [comm_ring, is_domain, inhabited]] def cyclotomic_ring : Type w := adjoin A { b : (cyclotomic_field n K) | b ^ (n : ℕ) = 1 } namespace cyclotomic_ring /-- The `A`-algebra structure on `cyclotomic_ring n A K`. This is not an instance since it causes diamonds when `A = ℤ`. -/ def algebra_base : algebra A (cyclotomic_ring n A K) := (adjoin A _).algebra local attribute [instance] cyclotomic_ring.algebra_base instance : no_zero_smul_divisors A (cyclotomic_ring n A K) := (adjoin A _).no_zero_smul_divisors_bot lemma algebra_base_injective : function.injective $ algebra_map A (cyclotomic_ring n A K) := no_zero_smul_divisors.algebra_map_injective _ _ instance : algebra (cyclotomic_ring n A K) (cyclotomic_field n K) := (adjoin A _).to_algebra lemma adjoin_algebra_injective : function.injective $ algebra_map (cyclotomic_ring n A K) (cyclotomic_field n K) := subtype.val_injective instance : no_zero_smul_divisors (cyclotomic_ring n A K) (cyclotomic_field n K) := no_zero_smul_divisors.of_algebra_map_injective (adjoin_algebra_injective n A K) instance : is_scalar_tower A (cyclotomic_ring n A K) (cyclotomic_field n K) := is_scalar_tower.subalgebra' _ _ _ _ instance is_cyclotomic_extension [ne_zero ((n : ℕ) : A)] : is_cyclotomic_extension {n} A (cyclotomic_ring n A K) := { exists_root := λ a han, begin rw mem_singleton_iff at han, subst a, haveI := ne_zero.of_no_zero_smul_divisors A K n, haveI := ne_zero.of_no_zero_smul_divisors A (cyclotomic_ring n A K) n, haveI := ne_zero.of_no_zero_smul_divisors A (cyclotomic_field n K) n, obtain ⟨μ, hμ⟩ := let h := (cyclotomic_field.is_cyclotomic_extension n K).exists_root in h $ mem_singleton n, refine ⟨⟨μ, subset_adjoin _⟩, _⟩, { apply (is_root_of_unity_iff n.pos (cyclotomic_field n K)).mpr, refine ⟨n, nat.mem_divisors_self _ n.ne_zero, _⟩, rwa [aeval_def, eval₂_eq_eval_map, map_cyclotomic] at hμ }, simp_rw [aeval_def, eval₂_eq_eval_map, map_cyclotomic, ←is_root.def, is_root_cyclotomic_iff] at hμ ⊢, rwa ←is_primitive_root.map_iff_of_injective (adjoin_algebra_injective n A K) end, adjoin_roots := λ x, begin refine adjoin_induction' (λ y hy, _) (λ a, _) (λ y z hy hz, _) (λ y z hy hz, _) x, { refine subset_adjoin _, simp only [mem_singleton_iff, exists_eq_left, mem_set_of_eq], rwa [← subalgebra.coe_eq_one, subalgebra.coe_pow, subtype.coe_mk] }, { exact subalgebra.algebra_map_mem _ a }, { exact subalgebra.add_mem _ hy hz }, { exact subalgebra.mul_mem _ hy hz }, end } instance [ne_zero ((n : ℕ) : A)] : is_fraction_ring (cyclotomic_ring n A K) (cyclotomic_field n K) := { map_units := λ ⟨x, hx⟩, begin rw is_unit_iff_ne_zero, apply map_ne_zero_of_mem_non_zero_divisors, apply adjoin_algebra_injective, exact hx end, surj := λ x, begin letI : ne_zero ((n : ℕ) : K) := ne_zero.nat_of_injective (is_fraction_ring.injective A K), refine algebra.adjoin_induction (((is_cyclotomic_extension.iff_singleton n K _).1 (cyclotomic_field.is_cyclotomic_extension n K)).2 x) (λ y hy, _) (λ k, _) _ _, { exact ⟨⟨⟨y, subset_adjoin hy⟩, 1⟩, by simpa⟩ }, { have : is_localization (non_zero_divisors A) K := infer_instance, replace := this.surj, obtain ⟨⟨z, w⟩, hw⟩ := this k, refine ⟨⟨algebra_map A _ z, algebra_map A _ w, map_mem_non_zero_divisors _ (algebra_base_injective n A K) w.2⟩, _⟩, letI : is_scalar_tower A K (cyclotomic_field n K) := is_scalar_tower.of_algebra_map_eq (congr_fun rfl), rw [set_like.coe_mk, ← is_scalar_tower.algebra_map_apply, ← is_scalar_tower.algebra_map_apply, @is_scalar_tower.algebra_map_apply A K _ _ _ _ _ (_root_.cyclotomic_field.algebra n K) _ _ w, ← ring_hom.map_mul, hw, ← is_scalar_tower.algebra_map_apply] }, { rintro y z ⟨a, ha⟩ ⟨b, hb⟩, refine ⟨⟨a.1 * b.2 + b.1 * a.2, a.2 * b.2, mul_mem_non_zero_divisors.2 ⟨a.2.2, b.2.2⟩⟩, _⟩, rw [set_like.coe_mk, ring_hom.map_mul, add_mul, ← mul_assoc, ha, mul_comm ((algebra_map _ _) ↑a.2), ← mul_assoc, hb], simp }, { rintro y z ⟨a, ha⟩ ⟨b, hb⟩, refine ⟨⟨a.1 * b.1, a.2 * b.2, mul_mem_non_zero_divisors.2 ⟨a.2.2, b.2.2⟩⟩, _⟩, rw [set_like.coe_mk, ring_hom.map_mul, mul_comm ((algebra_map _ _) ↑a.2), mul_assoc, ← mul_assoc z, hb, ← mul_comm ((algebra_map _ _) ↑a.2), ← mul_assoc, ha], simp } end, eq_iff_exists := λ x y, ⟨λ h, ⟨1, by rw adjoin_algebra_injective n A K h⟩, λ ⟨c, hc⟩, by rw mul_right_cancel₀ (non_zero_divisors.ne_zero c.prop) hc⟩ } lemma eq_adjoin_primitive_root {μ : (cyclotomic_field n K)} (h : is_primitive_root μ n) : cyclotomic_ring n A K = adjoin A ({μ} : set ((cyclotomic_field n K))) := begin letI := classical.prop_decidable, rw [←is_cyclotomic_extension.adjoin_roots_cyclotomic_eq_adjoin_root_cyclotomic n μ h, is_cyclotomic_extension.adjoin_roots_cyclotomic_eq_adjoin_nth_roots n h], simp [cyclotomic_ring] end end cyclotomic_ring end cyclotomic_ring end is_domain /-- Algebraically closed fields are cyclotomic extensions over themselves. -/ lemma is_alg_closed.is_cyclotomic_extension (K) [field K] [is_alg_closed K] (S) : is_cyclotomic_extension S K K := ⟨λ a _, is_alg_closed.exists_aeval_eq_zero _ _ (degree_cyclotomic_pos _ _ a.pos).ne', algebra.eq_top_iff.mp $ subsingleton.elim _ _ ⟩ instance : ∀ S, is_cyclotomic_extension S ℂ ℂ := is_alg_closed.is_cyclotomic_extension ℂ
2771b379a8e48e04a27784bf71a6d89fbba2809b
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/689.lean
d11d5f4fad5aef36a1adade81675abedf55d2020
[ "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
107
lean
example (x : Nat) : True := match h:x with section end structure A where h : Nat := 1 h2 : h ≠ 0 := by
155296b0660a8d45ba40afbd64c91677ff58742c
43390109ab88557e6090f3245c47479c123ee500
/src/M3P14/Problem_sheets/sheet2.lean
aed647e8698ddc2bb5e9bbafe70ac2be337abc42
[ "Apache-2.0" ]
permissive
Ja1941/xena-UROP-2018
41f0956519f94d56b8bf6834a8d39473f4923200
b111fb87f343cf79eca3b886f99ee15c1dd9884b
refs/heads/master
1,662,355,955,139
1,590,577,325,000
1,590,577,325,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,459
lean
import data.nat.prime import data.nat.modeq data.int.modeq import analysis.real import tactic.norm_num import algebra.group_power import data.int.basic import M3P14.order import chris_hughes_various.zmod import M3P14.lqr import M3P14.phi open nat -- Questions: -- Compute 210 449 and 605/617 using quadratic reciprocity. -- 449 and 617 are both prime). -- TODO: how to prove it using quadratic reciprocity? local attribute [instance] decidable_prime_1 theorem ls_3_449 (oddprime_449 : (prime 449 ∧ 449 ≠ 2)) : legendre_sym 3 oddprime_449 = -1 := begin have oddprime_3 : prime 3 ∧ 3 ≠ 2, from ⟨prime_three, by norm_num⟩, have b2: (legendre_sym 3 oddprime_449) = (legendre_sym 449 oddprime_3) * 1, from eq.subst (show (-1:ℤ)^(((3-1)/2)*((449-1)/2)) = 1, by norm_num) (law_of_quadratic_reciprocity' oddprime_3 oddprime_449), rw b2, simp, rw legendre_sym_refl 449 2 oddprime_3, rw LQR_supplementary_2, norm_num, unfold int.modeq, norm_num, end theorem ls_5_449 (H : prime 449 ∧ 449 ≠ 2) : legendre_sym 5 H = 1 := begin have prime_5 : prime 5 ∧ 5 ≠ 2, from ⟨dec_trivial, by norm_num⟩, rw (show (legendre_sym 5 H) = (legendre_sym 449 prime_5) * (-1)^(((5-1)/2)*((449-1)/2)), from law_of_quadratic_reciprocity' _ _), norm_num, have : 449 ≡ -1 [ZMOD 5], by norm_num [int.modeq], rw [legendre_sym_refl _ _ prime_5 this, legendre_neg_one], norm_num, end theorem ls_7_449 (H : prime 449 ∧ 449 ≠ 2) : legendre_sym 7 H = 1 := begin have prime_7 : prime 7 ∧ 7 ≠ 2, from ⟨dec_trivial, by norm_num⟩, rw (show (legendre_sym 7 H) = (legendre_sym 449 prime_7) * (-1)^(((7-1)/2)*((449-1)/2)), from law_of_quadratic_reciprocity' _ _), norm_num, have : 449 ≡ 1 [ZMOD 7], by unfold int.modeq; norm_num, rw [legendre_sym_refl _ _ prime_7 this, legendre_one], end theorem ls_5_617 (H : prime 617 ∧ 617 ≠ 2) : legendre_sym 5 H = -1 := begin have prime_5 : prime 5 ∧ 5 ≠ 2, from ⟨dec_trivial, by norm_num⟩, rw (show (legendre_sym 5 H) = (legendre_sym 617 prime_5) * (-1)^(((5-1)/2)*((617-1)/2)), from law_of_quadratic_reciprocity' _ _), norm_num, have : 617 ≡ 2 [ZMOD 5], by unfold int.modeq; norm_num, rw [legendre_sym_refl _ _ prime_5 this, LQR_supplementary_2], norm_num, end theorem ls_11_617 (H : prime 617 ∧ 617 ≠ 2) : legendre_sym 11 H = 1 := begin have prime_11 : prime 11 ∧ 11 ≠ 2, from ⟨dec_trivial, by norm_num⟩, rw (show (legendre_sym 11 H) = (legendre_sym 617 prime_11) * (-1)^(((11-1)/2)*((617-1)/2)), from law_of_quadratic_reciprocity' _ _), norm_num, have : 617 ≡ 1 [ZMOD 11], by unfold int.modeq; norm_num, rw [legendre_sym_refl _ _ prime_11 this, legendre_one], end theorem q1 (H1 : prime 449 ∧ 449 ≠ 2) (H2 : prime 617 ∧ 617 ≠ 2) : ((legendre_sym 210 H1) = (-1 : ℤ)) ∧ ((legendre_sym 605 H2) = (-1: ℤ) ) := begin split, have h : (-1 : ℤ)^((449^2 -1)/8) = 1, by norm_num, have h13: legendre_sym 2 H1 = (-1:ℤ)^((449^2-1)/8), from LQR_supplementary_2 H1, have a1: legendre_sym 2 H1 = 1, from eq.trans h13 h, have eq_210 : (210 : ℤ) = (2 : ℤ) * (3 : ℤ) * (5 : ℤ) * (7 : ℤ), by norm_num, rw [eq_210, legendre_sym_mul, legendre_sym_mul, legendre_sym_mul, ls_7_449 H1, ls_5_449 H1, ls_3_449 H1, a1], norm_num, have eq_605 : (605 : ℤ) = (5 : ℤ) * (11 : ℤ) * (11 : ℤ), by norm_num, rw [eq_605, legendre_sym_mul, legendre_sym_mul,ls_5_617, ls_11_617], norm_num, end -- Find all 6 primitive roots modulo 19. theorem q2a : ∃ A : set ℕ, ∀ x : ℕ, primitive_root x 19 ↔ x ∈ A := sorry -- Show that if n is odd and a is a primitive root mod n, then a is a primitive root mod 2n if a is odd, and a + n is a primitive root mod 2n if a is even. -- [HINT: Φ(2n) = Φ(n) when n is odd.] theorem q2b {a n : ℕ} (n_odd : gcd 2 n = 1) (hp : primitive_root a n) : (gcd 2 a = 1 → primitive_root a (2*n)) ∧ (gcd 2 a = 2 → primitive_root (a + n) (2*n)) := begin unfold primitive_root at hp, split, have p1: coprime a n, from hp.1, have p2: order_of a n = phi n, from hp.2, have p3: phi (2*n) = phi n, rw [phi_mul 2 n n_odd, phi_p 2 (prime_two), one_mul], { intro h, unfold primitive_root, split, have h1: coprime 2 a, by {unfold coprime, exact h}, by {unfold coprime, unfold coprime at p1, rw [@coprime.gcd_mul_left_cancel_right 2 a n h1, p1],}, rw p3, sorry, }, { intro h, unfold primitive_root, split, sorry, sorry, } end -- Let p be a prime and let a be a primitive root mod p. -- Show that a is also a primitive root mod p² if, and only if, a^p−1 is not congruent to 1 mod p². -- [HINT: what is the order of a mod p? What does this say about the order of a mod p²?] --set_option pp.proofs true theorem q3 {a p : ℕ} (hp : prime p) (hq : primitive_root a p) : primitive_root a (p^2) ↔ ¬(a^(p-1) ≡ 1 [MOD (p^2)]) := begin apply iff.intro, { intro j1, unfold primitive_root at hq j1, have j2: order_of a (p^2) = p*(p-1), rw [j1.2, power_p_phi p 2 hp, nat.pow_one], assume j3, have j4: order_of a (p^2) ∣ (p-1), from order_div a (p^2) (p-1) j1.1 j3, have j5: p*(p-1) ∣ (p-1), rwa j2 at j4, have j6: ¬ p*(p-1) ∣ (p-1), from mt (le_of_dvd (nat.sub_pos_of_lt hp.gt_one)) (not_le_of_gt ((lt_mul_iff_one_lt_left (nat.sub_pos_of_lt hp.gt_one)).2 hp.gt_one)), from absurd j5 j6, }, { unfold prime at hp, unfold primitive_root at hq, unfold primitive_root, intro h, have j1: coprime a (p^2), { unfold coprime, have h1: gcd a (p*p) = gcd a p, from @coprime.gcd_mul_right_cancel_right p a p hq.1.symm, have h2: gcd a (p*p) = 1, from eq.subst hq.1 h1, have h3: p^2 = p*p, norm_num, from eq.subst h3.symm h2, }, have j2: phi (p^2) = p*(p-1), rw [power_p_phi p 2 hp, nat.pow_one], have j3: order_of a (p^2) ∣ phi (p^2), from order_div_phi_n a (p^2) j1, have j4: order_of a (p^2) ∣ p*(p-1), from eq.subst j2 j3, have j5: phi (p^1) = 1*(p-1), rw [power_p_phi p 1 hp, nat.pow_zero], have j6: phi p = 1*(p-1), rw [←j5, nat.pow_one], have j7: order_of a p = p-1, rw [hq.2, j6, one_mul], have j8: a ^ order_of a (p^2) ≡ 1 [MOD (p^2)], from pow_order_eq_one a (p^2) j1, have j9: ↑(p^2) ∣ ↑(a ^ order_of a (p^2)) - ↑1, from nat.modeq.dvd_of_modeq j8.symm, --have j17: ↑(a ^ order_of a (p^2)) - ↑1 = ↑(a ^ order_of a (p ^ 2) - 1), from sorry, --have j18: ↑(p^2) ∣ ↑(a ^ order_of a (p ^ 2) - 1), from eq.subst j17 j16, have j10: (p^2) ∣ a ^ order_of a (p^2) - 1, from sorry, have j11: p ∣ a ^ order_of a (p^2) - 1, { have h1: p^2 = p*p, norm_num, have h2: p*p ∣ a ^ order_of a (p*p) - 1, from eq.subst h1 j10, have h3: p ∣ p*p, from sorry, from dvd_trans h3 h2, }, have j12: a ^ order_of a (p^2) ≡ 1 [MOD p], from sorry, have j13: order_of a p ∣ order_of a (p^2), from order_div a p (order_of a (p^2)) hq.1 j12, have j14: p-1 ∣ order_of a (p^2), from eq.subst j7 j13, have j15: order_of a (p^2) = p*(p-1), from sorry, have j16: order_of a (p^2) = phi (p^2), rw[j15, ←j2], from ⟨j1, j16⟩, } end -- Let p be a prime, and let a be an integer not divisible by p. -- Show that the equation x^d ≡ a (mod p) has a solution if, and only if, a^(p−1/(d,p−1)) ≡ 1 (mod p). -- Show further that if this is the case then this equation has (d, p − 1) solutions mod p. -- [HINT: what happens when you fix a primitive root g mod p, and take the discrete log of the equation x^d ≡ a (mod p)?] theorem q4 (p a x d : ℕ) (hp : prime p) : x^d ≡ a [MOD p] ↔ a^(p-1/(gcd d (p-1))) ≡ 1 [MOD p] := sorry -- Let p be an odd prime different from 7. -- Show that 7 is a square mod p if, and only if, p is congruent to 1, 3, 9, 19, 25 or 27 modulo 28. -- [HINT: use quadratic reciprocity to relate 7/p to p/7.] theorem q5 (p x : ℕ) (hp: prime p) (hq: p ≠ 7) : x^2 ≡ 7 [MOD p] ↔ (p ≡ 1 [MOD 28] ∨ p ≡ 3 [MOD 28] ∨ p ≡ 9 [MOD 28] ∨ p ≡ 19 [MOD 28] ∨ p ≡ 25 [MOD 28] ∨ p ≡ 25 [MOD 28]) := sorry -- Let n and m be relatively prime. Show that every element of (ℤ/nmℤ)^x has order dividing the least common multiple of Φ(n) and Φ(m). --theorem q6a {n m : ℕ} (hp : gcd m n = 1) : := sorry -- Show that if n and m are relatively prime, then ℤ/nmℤ has a primitive root if, and only if, both ℤ/nℤ and ℤ/mℤ have primitive roots, and (Φ(n), Φ(m)) = 1. -- When can this happen? --theorem q6b (n m : ℕ) (hp : gcd m n = 1) := sorry -- Suppose a is a primitive root modulo n. Show that a^d is also a primitive root modulo n for all d such that (d, Φ(n)) = 1. -- [Hint: show that there exists k such that (a^d)^k is equal to a.] theorem q7 {a n : ℕ} (hp : primitive_root a n) : ∀ d : ℕ, gcd d (phi n) = 1 → primitive_root (a^d) n := sorry -- Show that if p is a prime congruent to ±1 mod 24 then none of 2, 3, 4, 6is a primitive root modulo p. -- [Hint: show that 2 and 3 are squares mod p.] theorem q8 (p : ℕ) (hp : prime p) : (p ≡ 1 [ZMOD 24] ∨ p ≡ -1 [ZMOD 24]) := sorry
153698defa096ea637026254acf8667c5f1552b3
618003631150032a5676f229d13a079ac875ff77
/src/algebra/category/CommRing/colimits.lean
49e31486aeaeb58f5c4484986a1f1b24a480eb8c
[ "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
12,653
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.category.CommRing.basic /-! # The category of commutative rings has all colimits. This file uses a "pre-automated" approach, just as for `Mon/colimits.lean`. It is a very uniform approach, that conceivably could be synthesised directly by a tactic that analyses the shape of `comm_ring` and `ring_hom`. -/ universes u v open category_theory open category_theory.limits -- [ROBOT VOICE]: -- You should pretend for now that this file was automatically generated. -- It follows the same template as colimits in Mon. /- `#print comm_ring` says: structure comm_ring : Type u → Type u fields: comm_ring.zero : Π (α : Type u) [c : comm_ring α], α comm_ring.one : Π (α : Type u) [c : comm_ring α], α comm_ring.neg : Π {α : Type u} [c : comm_ring α], α → α comm_ring.add : Π {α : Type u} [c : comm_ring α], α → α → α comm_ring.mul : Π {α : Type u} [c : comm_ring α], α → α → α comm_ring.zero_add : ∀ {α : Type u} [c : comm_ring α] (a : α), 0 + a = a comm_ring.add_zero : ∀ {α : Type u} [c : comm_ring α] (a : α), a + 0 = a comm_ring.one_mul : ∀ {α : Type u} [c : comm_ring α] (a : α), 1 * a = a comm_ring.mul_one : ∀ {α : Type u} [c : comm_ring α] (a : α), a * 1 = a comm_ring.add_left_neg : ∀ {α : Type u} [c : comm_ring α] (a : α), -a + a = 0 comm_ring.add_comm : ∀ {α : Type u} [c : comm_ring α] (a b : α), a + b = b + a comm_ring.mul_comm : ∀ {α : Type u} [c : comm_ring α] (a b : α), a * b = b * a comm_ring.add_assoc : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), a + b + c_1 = a + (b + c_1) comm_ring.mul_assoc : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), a * b * c_1 = a * (b * c_1) comm_ring.left_distrib : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), a * (b + c_1) = a * b + a * c_1 comm_ring.right_distrib : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), (a + b) * c_1 = a * c_1 + b * c_1 -/ namespace CommRing.colimits /-! We build the colimit of a diagram in `Mon` by constructing the free monoid on the disjoint union of all the monoids in the diagram, then taking the quotient by the monoid laws within each monoid, and the identifications given by the morphisms in the diagram. -/ variables {J : Type v} [small_category J] (F : J ⥤ CommRing.{v}) /-- An inductive type representing all commutative ring expressions (without relations) on a collection of types indexed by the objects of `J`. -/ inductive prequotient -- There's always `of` | of : Π (j : J) (x : F.obj j), prequotient -- Then one generator for each operation | zero : prequotient | one : prequotient | neg : prequotient → prequotient | add : prequotient → prequotient → prequotient | mul : prequotient → prequotient → prequotient instance : inhabited (prequotient F) := ⟨prequotient.zero⟩ open prequotient /-- The relation on `prequotient` saying when two expressions are equal because of the commutative ring laws, or because one element is mapped to another by a morphism in the diagram. -/ inductive relation : prequotient F → prequotient F → Prop -- Make it an equivalence relation: | refl : Π (x), relation x x | symm : Π (x y) (h : relation x y), relation y x | trans : Π (x y z) (h : relation x y) (k : relation y z), relation x z -- There's always a `map` relation | map : Π (j j' : J) (f : j ⟶ j') (x : F.obj j), relation (of j' (F.map f x)) (of j x) -- Then one relation per operation, describing the interaction with `of` | zero : Π (j), relation (of j 0) zero | one : Π (j), relation (of j 1) one | neg : Π (j) (x : F.obj j), relation (of j (-x)) (neg (of j x)) | add : Π (j) (x y : F.obj j), relation (of j (x + y)) (add (of j x) (of j y)) | mul : Π (j) (x y : F.obj j), relation (of j (x * y)) (mul (of j x) (of j y)) -- Then one relation per argument of each operation | neg_1 : Π (x x') (r : relation x x'), relation (neg x) (neg x') | add_1 : Π (x x' y) (r : relation x x'), relation (add x y) (add x' y) | add_2 : Π (x y y') (r : relation y y'), relation (add x y) (add x y') | mul_1 : Π (x x' y) (r : relation x x'), relation (mul x y) (mul x' y) | mul_2 : Π (x y y') (r : relation y y'), relation (mul x y) (mul x y') -- And one relation per axiom | zero_add : Π (x), relation (add zero x) x | add_zero : Π (x), relation (add x zero) x | one_mul : Π (x), relation (mul one x) x | mul_one : Π (x), relation (mul x one) x | add_left_neg : Π (x), relation (add (neg x) x) zero | add_comm : Π (x y), relation (add x y) (add y x) | mul_comm : Π (x y), relation (mul x y) (mul y x) | add_assoc : Π (x y z), relation (add (add x y) z) (add x (add y z)) | mul_assoc : Π (x y z), relation (mul (mul x y) z) (mul x (mul y z)) | left_distrib : Π (x y z), relation (mul x (add y z)) (add (mul x y) (mul x z)) | right_distrib : Π (x y z), relation (mul (add x y) z) (add (mul x z) (mul y z)) /-- The setoid corresponding to commutative expressions modulo monoid relations and identifications. -/ def colimit_setoid : setoid (prequotient F) := { r := relation F, iseqv := ⟨relation.refl, relation.symm, relation.trans⟩ } attribute [instance] colimit_setoid /-- The underlying type of the colimit of a diagram in `CommRing`. -/ @[derive inhabited] def colimit_type : Type v := quotient (colimit_setoid F) instance : comm_ring (colimit_type F) := { zero := begin exact quot.mk _ zero end, one := begin exact quot.mk _ one end, neg := begin fapply @quot.lift, { intro x, exact quot.mk _ (neg x) }, { intros x x' r, apply quot.sound, exact relation.neg_1 _ _ r }, end, add := begin fapply @quot.lift _ _ ((colimit_type F) → (colimit_type F)), { intro x, fapply @quot.lift, { intro y, exact quot.mk _ (add x y) }, { intros y y' r, apply quot.sound, exact relation.add_2 _ _ _ r } }, { intros x x' r, funext y, induction y, dsimp, apply quot.sound, { exact relation.add_1 _ _ _ r }, { refl } }, end, mul := begin fapply @quot.lift _ _ ((colimit_type F) → (colimit_type F)), { intro x, fapply @quot.lift, { intro y, exact quot.mk _ (mul x y) }, { intros y y' r, apply quot.sound, exact relation.mul_2 _ _ _ r } }, { intros x x' r, funext y, induction y, dsimp, apply quot.sound, { exact relation.mul_1 _ _ _ r }, { refl } }, end, zero_add := λ x, begin induction x, dsimp, apply quot.sound, apply relation.zero_add, refl, end, add_zero := λ x, begin induction x, dsimp, apply quot.sound, apply relation.add_zero, refl, end, one_mul := λ x, begin induction x, dsimp, apply quot.sound, apply relation.one_mul, refl, end, mul_one := λ x, begin induction x, dsimp, apply quot.sound, apply relation.mul_one, refl, end, add_left_neg := λ x, begin induction x, dsimp, apply quot.sound, apply relation.add_left_neg, refl, end, add_comm := λ x y, begin induction x, induction y, dsimp, apply quot.sound, apply relation.add_comm, refl, refl, end, mul_comm := λ x y, begin induction x, induction y, dsimp, apply quot.sound, apply relation.mul_comm, refl, refl, end, add_assoc := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.add_assoc, refl, refl, refl, end, mul_assoc := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.mul_assoc, refl, refl, refl, end, left_distrib := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.left_distrib, refl, refl, refl, end, right_distrib := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.right_distrib, refl, refl, refl, end, } @[simp] lemma quot_zero : quot.mk setoid.r zero = (0 : colimit_type F) := rfl @[simp] lemma quot_one : quot.mk setoid.r one = (1 : colimit_type F) := rfl @[simp] lemma quot_neg (x) : quot.mk setoid.r (neg x) = (-(quot.mk setoid.r x) : colimit_type F) := rfl @[simp] lemma quot_add (x y) : quot.mk setoid.r (add x y) = ((quot.mk setoid.r x) + (quot.mk setoid.r y) : colimit_type F) := rfl @[simp] lemma quot_mul (x y) : quot.mk setoid.r (mul x y) = ((quot.mk setoid.r x) * (quot.mk setoid.r y) : colimit_type F) := rfl /-- The bundled commutative ring giving the colimit of a diagram. -/ def colimit : CommRing := CommRing.of (colimit_type F) /-- The function from a given commutative ring in the diagram to the colimit commutative ring. -/ def cocone_fun (j : J) (x : F.obj j) : colimit_type F := quot.mk _ (of j x) /-- The ring homomorphism from a given commutative ring in the diagram to the colimit commutative ring. -/ def cocone_morphism (j : J) : F.obj j ⟶ colimit F := { to_fun := cocone_fun F j, map_one' := by apply quot.sound; apply relation.one, map_mul' := by intros; apply quot.sound; apply relation.mul, map_zero' := by apply quot.sound; apply relation.zero, map_add' := by intros; apply quot.sound; apply relation.add } @[simp] lemma cocone_naturality {j j' : J} (f : j ⟶ j') : F.map f ≫ (cocone_morphism F j') = cocone_morphism F j := begin ext, apply quot.sound, apply relation.map, end @[simp] lemma cocone_naturality_components (j j' : J) (f : j ⟶ j') (x : F.obj j): (cocone_morphism F j') (F.map f x) = (cocone_morphism F j) x := by { rw ←cocone_naturality F f, refl } /-- The cocone over the proposed colimit commutative ring. -/ def colimit_cocone : cocone F := { X := colimit F, ι := { app := cocone_morphism F } }. /-- The function from the free commutative ring on the diagram to the cone point of any other cocone. -/ @[simp] def desc_fun_lift (s : cocone F) : prequotient F → s.X | (of j x) := (s.ι.app j) x | zero := 0 | one := 1 | (neg x) := -(desc_fun_lift x) | (add x y) := desc_fun_lift x + desc_fun_lift y | (mul x y) := desc_fun_lift x * desc_fun_lift y /-- The function from the colimit commutative ring to the cone point of any other cocone. -/ def desc_fun (s : cocone F) : colimit_type F → s.X := begin fapply quot.lift, { exact desc_fun_lift F s }, { intros x y r, induction r; try { dsimp }, -- refl { refl }, -- symm { exact r_ih.symm }, -- trans { exact eq.trans r_ih_h r_ih_k }, -- map { simp, }, -- zero { simp, }, -- one { simp, }, -- neg { simp, }, -- add { simp, }, -- mul { simp, }, -- neg_1 { rw r_ih, }, -- add_1 { rw r_ih, }, -- add_2 { rw r_ih, }, -- mul_1 { rw r_ih, }, -- mul_2 { rw r_ih, }, -- zero_add { rw zero_add, }, -- add_zero { rw add_zero, }, -- one_mul { rw one_mul, }, -- mul_one { rw mul_one, }, -- add_left_neg { rw add_left_neg, }, -- add_comm { rw add_comm, }, -- mul_comm { rw mul_comm, }, -- add_assoc { rw add_assoc, }, -- mul_assoc { rw mul_assoc, }, -- left_distrib { rw left_distrib, }, -- right_distrib { rw right_distrib, }, } end /-- The ring homomorphism from the colimit commutative ring to the cone point of any other cocone. -/ @[simps] def desc_morphism (s : cocone F) : colimit F ⟶ s.X := { to_fun := desc_fun F s, map_one' := rfl, map_zero' := rfl, map_add' := λ x y, by { induction x; induction y; refl }, map_mul' := λ x y, by { induction x; induction y; refl }, } /-- Evidence that the proposed colimit is the colimit. -/ def colimit_is_colimit : is_colimit (colimit_cocone F) := { desc := λ s, desc_morphism F s, uniq' := λ s m w, begin ext, induction x, induction x, { have w' := congr_fun (congr_arg (λ f : F.obj x_j ⟶ s.X, (f : F.obj x_j → s.X)) (w x_j)) x_x, erw w', refl, }, { simp, }, { simp, }, { simp *, }, { simp *, }, { simp *, }, refl end }. instance has_colimits_CommRing : has_colimits.{v} CommRing.{v} := { has_colimits_of_shape := λ J 𝒥, { has_colimit := λ F, by exactI { cocone := colimit_cocone F, is_colimit := colimit_is_colimit F } } } end CommRing.colimits
ff864734d2263140742cba3b510b37f37d12f6ec
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/order/hom/monoid.lean
5d9ab16c0c6dee7743826243a7b85abef66e9b1e
[ "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
19,132
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import algebra.hom.group import algebra.order.with_zero import order.hom.basic /-! # Ordered monoid and group homomorphisms This file defines morphisms between (additive) ordered monoids. ## Types of morphisms * `order_add_monoid_hom`: Ordered additive monoid homomorphisms. * `order_monoid_hom`: Ordered monoid homomorphisms. * `order_monoid_with_zero_hom`: Ordered monoid with zero homomorphisms. ## Typeclasses * `order_add_monoid_hom_class` * `order_monoid_hom_class` * `order_monoid_with_zero_hom_class` ## Notation * `→+o`: Bundled ordered additive monoid homs. Also use for additive groups homs. * `→*o`: Bundled ordered monoid homs. Also use for groups homs. * `→*₀o`: Bundled ordered monoid with zero homs. Also use for groups with zero homs. ## Implementation notes There's a coercion from bundled homs to fun, and the canonical notation is to use the bundled hom as a function via this coercion. There is no `order_group_hom` -- the idea is that `order_monoid_hom` is used. The constructor for `order_monoid_hom` needs a proof of `map_one` as well as `map_mul`; a separate constructor `order_monoid_hom.mk'` will construct ordered group homs (i.e. ordered monoid homs between ordered groups) given only a proof that multiplication is preserved, Implicit `{}` brackets are often used instead of type class `[]` brackets. This is done when the instances can be inferred because they are implicit arguments to the type `order_monoid_hom`. When they can be inferred from the type it is faster to use this method than to use type class inference. ## Tags ordered monoid, ordered group, monoid with zero -/ open function variables {F α β γ δ : Type*} section add_monoid /-- `α →+o β` is the type of monotone functions `α → β` that preserve the `ordered_add_comm_monoid` structure. `order_add_monoid_hom` is also used for ordered group homomorphisms. When possible, instead of parametrizing results over `(f : α →+o β)`, you should parametrize over `(F : Type*) [order_add_monoid_hom_class F α β] (f : F)`. When you extend this structure, make sure to extend `order_add_monoid_hom_class`. -/ structure order_add_monoid_hom (α β : Type*) [preorder α] [preorder β] [add_zero_class α] [add_zero_class β] extends α →+ β := (monotone' : monotone to_fun) infixr ` →+o `:25 := order_add_monoid_hom /-- `order_add_monoid_hom_class F α β` states that `F` is a type of ordered monoid homomorphisms. You should also extend this typeclass when you extend `order_add_monoid_hom`. -/ class order_add_monoid_hom_class (F : Type*) (α β : out_param $ Type*) [preorder α] [preorder β] [add_zero_class α] [add_zero_class β] extends add_monoid_hom_class F α β := (monotone (f : F) : monotone f) -- Instances and lemmas are defined below through `@[to_additive]`. end add_monoid section monoid variables [preorder α] [preorder β] [mul_one_class α] [mul_one_class β] /-- `α →*o β` is the type of functions `α → β` that preserve the `ordered_comm_monoid` structure. `order_monoid_hom` is also used for ordered group homomorphisms. When possible, instead of parametrizing results over `(f : α →*o β)`, you should parametrize over `(F : Type*) [order_monoid_hom_class F α β] (f : F)`. When you extend this structure, make sure to extend `order_monoid_hom_class`. -/ @[to_additive] structure order_monoid_hom (α β : Type*) [preorder α] [preorder β] [mul_one_class α] [mul_one_class β] extends α →* β := (monotone' : monotone to_fun) infixr ` →*o `:25 := order_monoid_hom /-- `order_monoid_hom_class F α β` states that `F` is a type of ordered monoid homomorphisms. You should also extend this typeclass when you extend `order_monoid_hom`. -/ @[to_additive] class order_monoid_hom_class (F : Type*) (α β : out_param $ Type*) [preorder α] [preorder β] [mul_one_class α] [mul_one_class β] extends monoid_hom_class F α β := (monotone (f : F) : monotone f) @[priority 100, to_additive] -- See note [lower instance priority] instance order_monoid_hom_class.to_order_hom_class [order_monoid_hom_class F α β] : order_hom_class F α β := { map_rel := order_monoid_hom_class.monotone } @[to_additive] instance [order_monoid_hom_class F α β] : has_coe_t F (α →*o β) := ⟨λ f, { to_fun := f, map_one' := map_one f, map_mul' := map_mul f, monotone' := order_monoid_hom_class.monotone _ }⟩ end monoid section monoid_with_zero variables [preorder α] [preorder β] [mul_zero_one_class α] [mul_zero_one_class β] /-- `order_monoid_with_zero_hom α β` is the type of functions `α → β` that preserve the `monoid_with_zero` structure. `order_monoid_with_zero_hom` is also used for group homomorphisms. When possible, instead of parametrizing results over `(f : α →+ β)`, you should parametrize over `(F : Type*) [order_monoid_with_zero_hom_class F α β] (f : F)`. When you extend this structure, make sure to extend `order_monoid_with_zero_hom_class`. -/ structure order_monoid_with_zero_hom (α β : Type*) [preorder α] [preorder β] [mul_zero_one_class α] [mul_zero_one_class β] extends α →*₀ β := (monotone' : monotone to_fun) infixr ` →*₀o `:25 := order_monoid_with_zero_hom /-- `order_monoid_with_zero_hom_class F α β` states that `F` is a type of ordered monoid with zero homomorphisms. You should also extend this typeclass when you extend `order_monoid_with_zero_hom`. -/ class order_monoid_with_zero_hom_class (F : Type*) (α β : out_param $ Type*) [preorder α] [preorder β] [mul_zero_one_class α] [mul_zero_one_class β] extends monoid_with_zero_hom_class F α β := (monotone (f : F) : monotone f) @[priority 100] -- See note [lower instance priority] instance order_monoid_with_zero_hom_class.to_order_monoid_hom_class [order_monoid_with_zero_hom_class F α β] : order_monoid_hom_class F α β := { .. ‹order_monoid_with_zero_hom_class F α β› } instance [order_monoid_with_zero_hom_class F α β] : has_coe_t F (α →*₀o β) := ⟨λ f, { to_fun := f, map_one' := map_one f, map_zero' := map_zero f, map_mul' := map_mul f, monotone' := order_monoid_with_zero_hom_class.monotone _ }⟩ end monoid_with_zero section ordered_add_comm_monoid variables [ordered_add_comm_monoid α] [ordered_add_comm_monoid β] [order_add_monoid_hom_class F α β] (f : F) {a : α} include β lemma map_nonneg (ha : 0 ≤ a) : 0 ≤ f a := by { rw ←map_zero f, exact order_hom_class.mono _ ha } lemma map_nonpos (ha : a ≤ 0) : f a ≤ 0 := by { rw ←map_zero f, exact order_hom_class.mono _ ha } end ordered_add_comm_monoid namespace order_monoid_hom section preorder variables [preorder α] [preorder β] [preorder γ] [preorder δ] [mul_one_class α] [mul_one_class β] [mul_one_class γ] [mul_one_class δ] {f g : α →*o β} @[to_additive] instance : order_monoid_hom_class (α →*o β) α β := { coe := λ f, f.to_fun, coe_injective' := λ f g h, by { obtain ⟨⟨_, _⟩, _⟩ := f, obtain ⟨⟨_, _⟩, _⟩ := g, congr' }, map_mul := λ f, f.map_mul', map_one := λ f, f.map_one', monotone := λ f, f.monotone' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ @[to_additive "Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly."] instance : has_coe_to_fun (α →*o β) (λ _, α → β) := fun_like.has_coe_to_fun -- Other lemmas should be accessed through the `fun_like` API @[ext, to_additive] lemma ext (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h @[to_additive] lemma to_fun_eq_coe (f : α →*o β) : f.to_fun = (f : α → β) := rfl @[simp, to_additive] lemma coe_mk (f : α →* β) (h) : (order_monoid_hom.mk f h : α → β) = f := rfl @[simp, to_additive] lemma mk_coe (f : α →*o β) (h) : order_monoid_hom.mk (f : α →* β) h = f := by { ext, refl } /-- Reinterpret an ordered monoid homomorphism as an order homomorphism. -/ @[to_additive "Reinterpret an ordered additive monoid homomorphism as an order homomorphism."] def to_order_hom (f : α →*o β) : α →o β := { ..f } @[simp, to_additive] lemma coe_monoid_hom (f : α →*o β) : ((f : α →* β) : α → β) = f := rfl @[simp, to_additive] lemma coe_order_hom (f : α →*o β) : ((f : α →o β) : α → β) = f := rfl @[to_additive] lemma to_monoid_hom_injective : injective (to_monoid_hom : _ → α →* β) := λ f g h, ext $ by convert fun_like.ext_iff.1 h @[to_additive] lemma to_order_hom_injective : injective (to_order_hom : _ → α →o β) := λ f g h, ext $ by convert fun_like.ext_iff.1 h /-- Copy of an `order_monoid_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ @[to_additive "Copy of an `order_monoid_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities."] protected def copy (f : α →*o β) (f' : α → β) (h : f' = f) : α →*o β := { to_fun := f', monotone' := h.symm.subst f.monotone', ..f.to_monoid_hom.copy f' $ by exact h } variables (α) /-- The identity map as an ordered monoid homomorphism. -/ @[to_additive "The identity map as an ordered additive monoid homomorphism."] protected def id : α →*o α := { ..monoid_hom.id α, ..order_hom.id } @[simp, to_additive] lemma coe_id : ⇑(order_monoid_hom.id α) = id := rfl @[to_additive] instance : inhabited (α →*o α) := ⟨order_monoid_hom.id α⟩ variables {α} /-- Composition of `order_monoid_hom`s as an `order_monoid_hom`. -/ @[to_additive "Composition of `order_add_monoid_hom`s as an `order_add_monoid_hom`"] def comp (f : β →*o γ) (g : α →*o β) : α →*o γ := { ..f.to_monoid_hom.comp (g : α →* β), ..f.to_order_hom.comp (g : α →o β) } @[simp, to_additive] lemma coe_comp (f : β →*o γ) (g : α →*o β) : (f.comp g : α → γ) = f ∘ g := rfl @[simp, to_additive] lemma comp_apply (f : β →*o γ) (g : α →*o β) (a : α) : (f.comp g) a = f (g a) := rfl @[simp, to_additive] lemma coe_comp_monoid_hom (f : β →*o γ) (g : α →*o β) : (f.comp g : α →* γ) = (f : β →* γ).comp g := rfl @[simp, to_additive] lemma coe_comp_order_hom (f : β →*o γ) (g : α →*o β) : (f.comp g : α →o γ) = (f : β →o γ).comp g := rfl @[simp, to_additive] lemma comp_assoc (f : γ →*o δ) (g : β →*o γ) (h : α →*o β) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp, to_additive] lemma comp_id (f : α →*o β) : f.comp (order_monoid_hom.id α) = f := ext $ λ a, rfl @[simp, to_additive] lemma id_comp (f : α →*o β) : (order_monoid_hom.id β).comp f = f := ext $ λ a, rfl @[to_additive] lemma cancel_right {g₁ g₂ : β →*o γ} {f : α →*o β} (hf : function.surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩ @[to_additive] lemma cancel_left {g : β →*o γ} {f₁ f₂ : α →*o β} (hg : function.injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, ext $ λ a, hg $ by rw [←comp_apply, h, comp_apply], congr_arg _⟩ /-- `1` is the homomorphism sending all elements to `1`. -/ @[to_additive "`1` is the homomorphism sending all elements to `1`."] instance : has_one (α →*o β) := ⟨{ monotone' := monotone_const, ..(1 : α →* β) }⟩ @[simp, to_additive] lemma coe_one : ⇑(1 : α →*o β) = 1 := rfl @[simp, to_additive] lemma one_apply (a : α) : (1 : α →*o β) a = 1 := rfl @[simp, to_additive] lemma one_comp (f : α →*o β) : (1 : β →*o γ).comp f = 1 := rfl @[simp, to_additive] lemma comp_one (f : β →*o γ) : f.comp (1 : α →*o β) = 1 := by { ext, exact map_one f } end preorder section mul variables [ordered_comm_monoid α] [ordered_comm_monoid β] [ordered_comm_monoid γ] /-- For two ordered monoid morphisms `f` and `g`, their product is the ordered monoid morphism sending `a` to `f a * g a`. -/ @[to_additive "For two ordered additive monoid morphisms `f` and `g`, their product is the ordered additive monoid morphism sending `a` to `f a + g a`."] instance : has_mul (α →*o β) := ⟨λ f g, { monotone' := f.monotone'.mul' g.monotone', ..(f * g : α →* β) }⟩ @[simp, to_additive] lemma coe_mul (f g : α →*o β) : ⇑(f * g) = f * g := rfl @[simp, to_additive] lemma mul_apply (f g : α →*o β) (a : α) : (f * g) a = f a * g a := rfl @[to_additive] lemma mul_comp (g₁ g₂ : β →*o γ) (f : α →*o β) : (g₁ * g₂).comp f = g₁.comp f * g₂.comp f := rfl @[to_additive] lemma comp_mul (g : β →*o γ) (f₁ f₂ : α →*o β) : g.comp (f₁ * f₂) = g.comp f₁ * g.comp f₂ := by { ext, exact map_mul g _ _ } end mul section ordered_comm_monoid variables {hα : ordered_comm_monoid α} {hβ : ordered_comm_monoid β} include hα hβ @[simp, to_additive] lemma to_monoid_hom_eq_coe (f : α →*o β) : f.to_monoid_hom = f := by { ext, refl } @[simp, to_additive] lemma to_order_hom_eq_coe (f : α →*o β) : f.to_order_hom = f := rfl end ordered_comm_monoid section ordered_comm_group variables {hα : ordered_comm_group α} {hβ : ordered_comm_group β} include hα hβ /-- Makes an ordered group homomorphism from a proof that the map preserves multiplication. -/ @[to_additive "Makes an ordered additive group homomorphism from a proof that the map preserves addition.", simps {fully_applied := ff}] def mk' (f : α → β) (hf : monotone f) (map_mul : ∀ a b : α, f (a * b) = f a * f b) : α →*o β := { monotone' := hf, ..monoid_hom.mk' f map_mul } end ordered_comm_group end order_monoid_hom namespace order_monoid_with_zero_hom section preorder variables [preorder α] [preorder β] [preorder γ] [preorder δ] [mul_zero_one_class α] [mul_zero_one_class β] [mul_zero_one_class γ] [mul_zero_one_class δ] {f g : α →*₀o β} instance : order_monoid_with_zero_hom_class (α →*₀o β) α β := { coe := λ f, f.to_fun, coe_injective' := λ f g h, by { obtain ⟨⟨_, _⟩, _⟩ := f, obtain ⟨⟨_, _⟩, _⟩ := g, congr' }, map_mul := λ f, f.map_mul', map_one := λ f, f.map_one', map_zero := λ f, f.map_zero', monotone := λ f, f.monotone' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (α →*₀o β) (λ _, α → β) := fun_like.has_coe_to_fun -- Other lemmas should be accessed through the `fun_like` API @[ext] lemma ext (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h lemma to_fun_eq_coe (f : α →*₀o β) : f.to_fun = (f : α → β) := rfl @[simp] lemma coe_mk (f : α →*₀ β) (h) : (order_monoid_with_zero_hom.mk f h : α → β) = f := rfl @[simp] lemma mk_coe (f : α →*₀o β) (h) : order_monoid_with_zero_hom.mk (f : α →*₀ β) h = f := by { ext, refl } /-- Reinterpret an ordered monoid with zero homomorphism as an order monoid homomorphism. -/ def to_order_monoid_hom (f : α →*₀o β) : α →*o β := { ..f } @[simp] lemma coe_monoid_with_zero_hom (f : α →*₀o β) : ⇑(f : α →*₀ β) = f := rfl @[simp] lemma coe_order_monoid_hom (f : α →*₀o β) : ⇑(f : α →*o β) = f := rfl lemma to_order_monoid_hom_injective : injective (to_order_monoid_hom : _ → α →*o β) := λ f g h, ext $ by convert fun_like.ext_iff.1 h lemma to_monoid_with_zero_hom_injective : injective (to_monoid_with_zero_hom : _ → α →*₀ β) := λ f g h, ext $ by convert fun_like.ext_iff.1 h /-- Copy of an `order_monoid_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : α →*o β) (f' : α → β) (h : f' = f) : α →*o β := { to_fun := f', monotone' := h.symm.subst f.monotone', ..f.to_monoid_hom.copy f' (by exact h) } variables (α) /-- The identity map as an ordered monoid with zero homomorphism. -/ protected def id : α →*₀o α := { ..monoid_with_zero_hom.id α, ..order_hom.id } @[simp] lemma coe_id : ⇑(order_monoid_with_zero_hom.id α) = id := rfl instance : inhabited (α →*₀o α) := ⟨order_monoid_with_zero_hom.id α⟩ variables {α} /-- Composition of `order_monoid_with_zero_hom`s as an `order_monoid_with_zero_hom`. -/ def comp (f : β →*₀o γ) (g : α →*₀o β) : α →*₀o γ := { ..f.to_monoid_with_zero_hom.comp (g : α →*₀ β), ..f.to_order_monoid_hom.comp (g : α →*o β) } @[simp] lemma coe_comp (f : β →*₀o γ) (g : α →*₀o β) : (f.comp g : α → γ) = f ∘ g := rfl @[simp] lemma comp_apply (f : β →*₀o γ) (g : α →*₀o β) (a : α) : (f.comp g) a = f (g a) := rfl @[simp] lemma coe_comp_monoid_with_zero_hom (f : β →*₀o γ) (g : α →*₀o β) : (f.comp g : α →*₀ γ) = (f : β →*₀ γ).comp g := rfl @[simp] lemma coe_comp_order_monoid_hom (f : β →*₀o γ) (g : α →*₀o β) : (f.comp g : α →*o γ) = (f : β →*o γ).comp g := rfl @[simp] lemma comp_assoc (f : γ →*₀o δ) (g : β →*₀o γ) (h : α →*₀o β) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] lemma comp_id (f : α →*₀o β) : f.comp (order_monoid_with_zero_hom.id α) = f := ext $ λ a, rfl @[simp] lemma id_comp (f : α →*₀o β) : (order_monoid_with_zero_hom.id β).comp f = f := ext $ λ a, rfl lemma cancel_right {g₁ g₂ : β →*₀o γ} {f : α →*₀o β} (hf : function.surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩ lemma cancel_left {g : β →*₀o γ} {f₁ f₂ : α →*₀o β} (hg : function.injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, ext $ λ a, hg $ by rw [←comp_apply, h, comp_apply], congr_arg _⟩ end preorder section mul variables [linear_ordered_comm_monoid_with_zero α] [linear_ordered_comm_monoid_with_zero β] [linear_ordered_comm_monoid_with_zero γ] /-- For two ordered monoid morphisms `f` and `g`, their product is the ordered monoid morphism sending `a` to `f a * g a`. -/ instance : has_mul (α →*₀o β) := ⟨λ f g, { monotone' := f.monotone'.mul' g.monotone', ..(f * g : α →*₀ β) }⟩ @[simp] lemma coe_mul (f g : α →*₀o β) : ⇑(f * g) = f * g := rfl @[simp] lemma mul_apply (f g : α →*₀o β) (a : α) : (f * g) a = f a * g a := rfl lemma mul_comp (g₁ g₂ : β →*₀o γ) (f : α →*₀o β) : (g₁ * g₂).comp f = g₁.comp f * g₂.comp f := rfl lemma comp_mul (g : β →*₀o γ) (f₁ f₂ : α →*₀o β) : g.comp (f₁ * f₂) = g.comp f₁ * g.comp f₂ := ext $ λ _, map_mul g _ _ end mul section linear_ordered_comm_monoid_with_zero variables {hα : preorder α} {hα' : mul_zero_one_class α} {hβ : preorder β} {hβ' : mul_zero_one_class β} include hα hα' hβ hβ' @[simp] lemma to_monoid_with_zero_hom_eq_coe (f : α →*₀o β) : f.to_monoid_with_zero_hom = f := by { ext, refl } @[simp] lemma to_order_monoid_hom_eq_coe (f : α →*₀o β) : f.to_order_monoid_hom = f := rfl end linear_ordered_comm_monoid_with_zero end order_monoid_with_zero_hom
b41d9a8443b8f3c6844a35372fb42ff51fc542c8
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/algebra/lie/ideal_operations.lean
296c587941a10bd70242b9a4acd4ed1e383fa42d
[ "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
11,544
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.lie.submodule /-! # Ideal operations for Lie algebras Given a Lie module `M` over a Lie algebra `L`, there is a natural action of the Lie ideals of `L` on the Lie submodules of `M`. In the special case that `M = L` with the adjoint action, this provides a pairing of Lie ideals which is especially important. For example, it can be used to define solvability / nilpotency of a Lie algebra via the derived / lower-central series. ## Main definitions * `lie_submodule.has_bracket` * `lie_submodule.lie_ideal_oper_eq_linear_span` * `lie_ideal.map_bracket_le` * `lie_ideal.comap_bracket_le` ## Notation Given a Lie module `M` over a Lie algebra `L`, together with a Lie submodule `N ⊆ M` and a Lie ideal `I ⊆ L`, we introduce the notation `⁅I, N⁆` for the Lie submodule of `M` corresponding to the action defined in this file. ## Tags lie algebra, ideal operation -/ universes u v w w₁ w₂ namespace lie_submodule variables {R : Type u} {L : Type v} {M : Type w} variables [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] variables [lie_ring_module L M] [lie_module R L M] variables (N N' : lie_submodule R L M) (I J : lie_ideal R L) section lie_ideal_operations /-- Given a Lie module `M` over a Lie algebra `L`, the set of Lie ideals of `L` acts on the set of submodules of `M`. -/ instance has_bracket : has_bracket (lie_ideal R L) (lie_submodule R L M) := ⟨λ I N, lie_span R L { m | ∃ (x : I) (n : N), ⁅(x : L), (n : M)⁆ = m }⟩ lemma lie_ideal_oper_eq_span : ⁅I, N⁆ = lie_span R L { m | ∃ (x : I) (n : N), ⁅(x : L), (n : M)⁆ = m } := rfl /-- See also `lie_submodule.lie_ideal_oper_eq_tensor_map_range`. -/ lemma lie_ideal_oper_eq_linear_span : (↑⁅I, N⁆ : submodule R M) = submodule.span R { m | ∃ (x : I) (n : N), ⁅(x : L), (n : M)⁆ = m } := begin apply le_antisymm, { let s := {m : M | ∃ (x : ↥I) (n : ↥N), ⁅(x : L), (n : M)⁆ = m}, have aux : ∀ (y : L) (m' ∈ submodule.span R s), ⁅y, m'⁆ ∈ submodule.span R s, { intros y m' hm', apply submodule.span_induction hm', { rintros m'' ⟨x, n, hm''⟩, rw [← hm'', leibniz_lie], refine submodule.add_mem _ _ _; apply submodule.subset_span, { use [⟨⁅y, ↑x⁆, I.lie_mem x.property⟩, n], refl, }, { use [x, ⟨⁅y, ↑n⁆, N.lie_mem n.property⟩], refl, }, }, { simp only [lie_zero, submodule.zero_mem], }, { intros m₁ m₂ hm₁ hm₂, rw lie_add, exact submodule.add_mem _ hm₁ hm₂, }, { intros t m'' hm'', rw lie_smul, exact submodule.smul_mem _ t hm'', }, }, change _ ≤ ↑({ lie_mem := aux, ..submodule.span R s } : lie_submodule R L M), rw [coe_submodule_le_coe_submodule, lie_ideal_oper_eq_span, lie_span_le], exact submodule.subset_span, }, { rw lie_ideal_oper_eq_span, apply submodule_span_le_lie_span, }, end lemma lie_coe_mem_lie (x : I) (m : N) : ⁅(x : L), (m : M)⁆ ∈ ⁅I, N⁆ := by { rw lie_ideal_oper_eq_span, apply subset_lie_span, use [x, m], } lemma lie_mem_lie {x : L} {m : M} (hx : x ∈ I) (hm : m ∈ N) : ⁅x, m⁆ ∈ ⁅I, N⁆ := N.lie_coe_mem_lie I ⟨x, hx⟩ ⟨m, hm⟩ lemma lie_comm : ⁅I, J⁆ = ⁅J, I⁆ := begin suffices : ∀ (I J : lie_ideal R L), ⁅I, J⁆ ≤ ⁅J, I⁆, { exact le_antisymm (this I J) (this J I), }, clear I J, intros I J, rw [lie_ideal_oper_eq_span, lie_span_le], rintros x ⟨y, z, h⟩, rw ← h, rw [← lie_skew, ← lie_neg, ← submodule.coe_neg], apply lie_coe_mem_lie, end lemma lie_le_right : ⁅I, N⁆ ≤ N := begin rw [lie_ideal_oper_eq_span, lie_span_le], rintros m ⟨x, n, hn⟩, rw ← hn, exact N.lie_mem n.property, end lemma lie_le_left : ⁅I, J⁆ ≤ I := by { rw lie_comm, exact lie_le_right I J, } lemma lie_le_inf : ⁅I, J⁆ ≤ I ⊓ J := by { rw le_inf_iff, exact ⟨lie_le_left I J, lie_le_right J I⟩, } @[simp] lemma lie_bot : ⁅I, (⊥ : lie_submodule R L M)⁆ = ⊥ := by { rw eq_bot_iff, apply lie_le_right, } @[simp] lemma bot_lie : ⁅(⊥ : lie_ideal R L), N⁆ = ⊥ := begin suffices : ⁅(⊥ : lie_ideal R L), N⁆ ≤ ⊥, { exact le_bot_iff.mp this, }, rw [lie_ideal_oper_eq_span, lie_span_le], rintros m ⟨⟨x, hx⟩, n, hn⟩, rw ← hn, change x ∈ (⊥ : lie_ideal R L) at hx, rw mem_bot at hx, simp [hx], end lemma mono_lie (h₁ : I ≤ J) (h₂ : N ≤ N') : ⁅I, N⁆ ≤ ⁅J, N'⁆ := begin intros m h, rw [lie_ideal_oper_eq_span, mem_lie_span] at h, rw [lie_ideal_oper_eq_span, mem_lie_span], intros N hN, apply h, rintros m' ⟨⟨x, hx⟩, ⟨n, hn⟩, hm⟩, rw ← hm, apply hN, use [⟨x, h₁ hx⟩, ⟨n, h₂ hn⟩], refl, end lemma mono_lie_left (h : I ≤ J) : ⁅I, N⁆ ≤ ⁅J, N⁆ := mono_lie _ _ _ _ h (le_refl N) lemma mono_lie_right (h : N ≤ N') : ⁅I, N⁆ ≤ ⁅I, N'⁆ := mono_lie _ _ _ _ (le_refl I) h @[simp] lemma lie_sup : ⁅I, N ⊔ N'⁆ = ⁅I, N⁆ ⊔ ⁅I, N'⁆ := begin have h : ⁅I, N⁆ ⊔ ⁅I, N'⁆ ≤ ⁅I, N ⊔ N'⁆, { rw sup_le_iff, split; apply mono_lie_right; [exact le_sup_left, exact le_sup_right], }, suffices : ⁅I, N ⊔ N'⁆ ≤ ⁅I, N⁆ ⊔ ⁅I, N'⁆, { exact le_antisymm this h, }, clear h, rw [lie_ideal_oper_eq_span, lie_span_le], rintros m ⟨x, ⟨n, hn⟩, h⟩, erw lie_submodule.mem_sup, erw lie_submodule.mem_sup at hn, rcases hn with ⟨n₁, hn₁, n₂, hn₂, hn'⟩, use ⁅(x : L), (⟨n₁, hn₁⟩ : N)⁆, split, { apply lie_coe_mem_lie, }, use ⁅(x : L), (⟨n₂, hn₂⟩ : N')⁆, split, { apply lie_coe_mem_lie, }, simp [← h, ← hn'], end @[simp] lemma sup_lie : ⁅I ⊔ J, N⁆ = ⁅I, N⁆ ⊔ ⁅J, N⁆ := begin have h : ⁅I, N⁆ ⊔ ⁅J, N⁆ ≤ ⁅I ⊔ J, N⁆, { rw sup_le_iff, split; apply mono_lie_left; [exact le_sup_left, exact le_sup_right], }, suffices : ⁅I ⊔ J, N⁆ ≤ ⁅I, N⁆ ⊔ ⁅J, N⁆, { exact le_antisymm this h, }, clear h, rw [lie_ideal_oper_eq_span, lie_span_le], rintros m ⟨⟨x, hx⟩, n, h⟩, erw lie_submodule.mem_sup, erw lie_submodule.mem_sup at hx, rcases hx with ⟨x₁, hx₁, x₂, hx₂, hx'⟩, use ⁅((⟨x₁, hx₁⟩ : I) : L), (n : N)⁆, split, { apply lie_coe_mem_lie, }, use ⁅((⟨x₂, hx₂⟩ : J) : L), (n : N)⁆, split, { apply lie_coe_mem_lie, }, simp [← h, ← hx'], end @[simp] lemma lie_inf : ⁅I, N ⊓ N'⁆ ≤ ⁅I, N⁆ ⊓ ⁅I, N'⁆ := by { rw le_inf_iff, split; apply mono_lie_right; [exact inf_le_left, exact inf_le_right], } @[simp] lemma inf_lie : ⁅I ⊓ J, N⁆ ≤ ⁅I, N⁆ ⊓ ⁅J, N⁆ := by { rw le_inf_iff, split; apply mono_lie_left; [exact inf_le_left, exact inf_le_right], } end lie_ideal_operations end lie_submodule namespace lie_ideal open lie_algebra variables {R : Type u} {L : Type v} {L' : Type w₂} variables [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] variables (f : L →ₗ⁅R⁆ L') (I : lie_ideal R L) (J : lie_ideal R L') /-- Note that the inequality can be strict; e.g., the inclusion of an Abelian subalgebra of a simple algebra. -/ lemma map_bracket_le {I₁ I₂ : lie_ideal R L} : map f ⁅I₁, I₂⁆ ≤ ⁅map f I₁, map f I₂⁆ := begin rw map_le_iff_le_comap, erw lie_submodule.lie_span_le, intros x hx, obtain ⟨⟨y₁, hy₁⟩, ⟨y₂, hy₂⟩, hx⟩ := hx, rw ← hx, let fy₁ : ↥(map f I₁) := ⟨f y₁, mem_map hy₁⟩, let fy₂ : ↥(map f I₂) := ⟨f y₂, mem_map hy₂⟩, change _ ∈ comap f ⁅map f I₁, map f I₂⁆, simp only [submodule.coe_mk, mem_comap, lie_hom.map_lie], exact lie_submodule.lie_coe_mem_lie _ _ fy₁ fy₂, end lemma map_bracket_eq {I₁ I₂ : lie_ideal R L} (h : function.surjective f) : map f ⁅I₁, I₂⁆ = ⁅map f I₁, map f I₂⁆ := begin suffices : ⁅map f I₁, map f I₂⁆ ≤ map f ⁅I₁, I₂⁆, { exact le_antisymm (map_bracket_le f) this, }, rw [← lie_submodule.coe_submodule_le_coe_submodule, coe_map_of_surjective h, lie_submodule.lie_ideal_oper_eq_linear_span, lie_submodule.lie_ideal_oper_eq_linear_span, linear_map.map_span], apply submodule.span_mono, rintros x ⟨⟨z₁, h₁⟩, ⟨z₂, h₂⟩, rfl⟩, obtain ⟨y₁, rfl⟩ := mem_map_of_surjective h h₁, obtain ⟨y₂, rfl⟩ := mem_map_of_surjective h h₂, use [⁅(y₁ : L), (y₂ : L)⁆, y₁, y₂], apply f.map_lie, end lemma comap_bracket_le {J₁ J₂ : lie_ideal R L'} : ⁅comap f J₁, comap f J₂⁆ ≤ comap f ⁅J₁, J₂⁆ := begin rw ← map_le_iff_le_comap, exact le_trans (map_bracket_le f) (lie_submodule.mono_lie _ _ _ _ map_comap_le map_comap_le), end variables {f} lemma map_comap_incl {I₁ I₂ : lie_ideal R L} : map I₁.incl (comap I₁.incl I₂) = I₁ ⊓ I₂ := by { conv_rhs { rw ← I₁.incl_ideal_range, }, rw ← map_comap_eq, exact I₁.incl_is_ideal_morphism, } lemma comap_bracket_eq {J₁ J₂ : lie_ideal R L'} (h : f.is_ideal_morphism) : comap f ⁅f.ideal_range ⊓ J₁, f.ideal_range ⊓ J₂⁆ = ⁅comap f J₁, comap f J₂⁆ ⊔ f.ker := begin rw [← lie_submodule.coe_to_submodule_eq_iff, comap_coe_submodule, lie_submodule.sup_coe_to_submodule, f.ker_coe_submodule, ← submodule.comap_map_eq, lie_submodule.lie_ideal_oper_eq_linear_span, lie_submodule.lie_ideal_oper_eq_linear_span, linear_map.map_span], congr, simp only [lie_hom.coe_to_linear_map, set.mem_set_of_eq], ext y, split, { rintros ⟨⟨x₁, hx₁⟩, ⟨x₂, hx₂⟩, hy⟩, rw ← hy, erw [lie_submodule.mem_inf, f.mem_ideal_range_iff h] at hx₁ hx₂, obtain ⟨⟨z₁, hz₁⟩, hz₁'⟩ := hx₁, rw ← hz₁ at hz₁', obtain ⟨⟨z₂, hz₂⟩, hz₂'⟩ := hx₂, rw ← hz₂ at hz₂', use [⁅z₁, z₂⁆, ⟨z₁, hz₁'⟩, ⟨z₂, hz₂'⟩, rfl], simp only [hz₁, hz₂, submodule.coe_mk, lie_hom.map_lie], }, { rintros ⟨x, ⟨⟨z₁, hz₁⟩, ⟨z₂, hz₂⟩, hx⟩, hy⟩, rw [← hy, ← hx], have hz₁' : f z₁ ∈ f.ideal_range ⊓ J₁, { rw lie_submodule.mem_inf, exact ⟨f.mem_ideal_range, hz₁⟩, }, have hz₂' : f z₂ ∈ f.ideal_range ⊓ J₂, { rw lie_submodule.mem_inf, exact ⟨f.mem_ideal_range, hz₂⟩, }, use [⟨f z₁, hz₁'⟩, ⟨f z₂, hz₂'⟩], simp only [submodule.coe_mk, lie_hom.map_lie], }, end lemma map_comap_bracket_eq {J₁ J₂ : lie_ideal R L'} (h : f.is_ideal_morphism) : map f ⁅comap f J₁, comap f J₂⁆ = ⁅f.ideal_range ⊓ J₁, f.ideal_range ⊓ J₂⁆ := by { rw [← map_sup_ker_eq_map, ← comap_bracket_eq h, map_comap_eq h, inf_eq_right], exact le_trans (lie_submodule.lie_le_left _ _) inf_le_left, } lemma comap_bracket_incl {I₁ I₂ : lie_ideal R L} : ⁅comap I.incl I₁, comap I.incl I₂⁆ = comap I.incl ⁅I ⊓ I₁, I ⊓ I₂⁆ := begin conv_rhs { congr, skip, rw ← I.incl_ideal_range, }, rw comap_bracket_eq, simp only [ker_incl, sup_bot_eq], exact I.incl_is_ideal_morphism, end /-- This is a very useful result; it allows us to use the fact that inclusion distributes over the Lie bracket operation on ideals, subject to the conditions shown. -/ lemma comap_bracket_incl_of_le {I₁ I₂ : lie_ideal R L} (h₁ : I₁ ≤ I) (h₂ : I₂ ≤ I) : ⁅comap I.incl I₁, comap I.incl I₂⁆ = comap I.incl ⁅I₁, I₂⁆ := by { rw comap_bracket_incl, rw ← inf_eq_right at h₁ h₂, rw [h₁, h₂], } end lie_ideal
6c3e9a5a861d3fc8afc0c946e54eb2f677988c84
36938939954e91f23dec66a02728db08a7acfcf9
/lean4/deps/x86_semantics/src/X86Semantics/BufferMap.lean
23b400245d456cefe953319435d5d6d761d67ae4
[]
no_license
pnwamk/reopt-vcg
f8b56dd0279392a5e1c6aee721be8138e6b558d3
c9f9f185fbefc25c36c4b506bbc85fd1a03c3b6d
refs/heads/master
1,631,145,017,772
1,593,549,019,000
1,593,549,143,000
254,191,418
0
0
null
1,586,377,077,000
1,586,377,077,000
null
UTF-8
Lean
false
false
2,146
lean
/- A map from indexes onto buffers, specialised to bytes -/ structure buffer_map.entry.{u} (k : Type u) : Type u := (start : k) (value : ByteArray) -- distance here is essentially subtraction. distance k k' < 0 iff k < k' structure buffer_map.{u} (k : Type u) (distance : k -> k -> Int) := (entries : List (buffer_map.entry k)) namespace buffer_map section universe u variables {k : Type u} {distance : k -> k -> Int} /- construction -/ def empty : buffer_map k distance := buffer_map.mk distance [] /- lookup -/ def in_entry (key : k) (e : entry k) : Prop := distance key e.start ≥ 0 ∧ Int.natAbs (distance key e.start) < e.value.size def entry_idx (key : k) (e : entry k) : Option (Fin e.value.size) := if H : distance key e.start ≥ 0 ∧ Int.natAbs (distance key e.start) < e.value.size then some (Fin.mk _ H.right) else none protected def lookup' : List (buffer_map.entry k) -> k -> Option UInt8 | [], _ => none | (e :: m), key => match @entry_idx k distance key e with | none => lookup' m key | (some idx) => some (e.value.get! idx.val) def lookup (m : buffer_map k distance) := @buffer_map.lookup' k distance m.entries protected def lookup_buffer' : List (buffer_map.entry k) -> k -> Option (k × ByteArray) | [], _ => none | (e :: m), key => match @entry_idx k distance key e with | none => lookup_buffer' m key | (some idx) => some (e.start, e.value) def lookup_buffer (m : buffer_map k distance) := @buffer_map.lookup_buffer' k distance m.entries /- insertion -/ -- FIXME: add overlap check def insert (m : buffer_map k distance) (start : k) (value : ByteArray) : buffer_map k distance := buffer_map.mk distance ({ start := start, value := value } :: m.entries) end end buffer_map section universes u variables {k : Type u} {distance : k -> k -> Int} [HasRepr k] instance : HasRepr (buffer_map.entry k) := ⟨fun e => "( [" ++ repr e.start ++ " ..+ " ++ repr e.value.size ++ "]" /-" -> " ++ has_repr.repr e.value -/ ++ ")"⟩ instance : HasRepr (buffer_map k distance) := ⟨fun m => repr m.entries ⟩ end
abf77a6d4366439b6ec3dbea47721ef09c6382aa
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/W/cardinal.lean
849d41a3194fc31c1b2169d730e9664fd45b2b7a
[ "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
2,830
lean
/- Copyright (c) 2021 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import set_theory.cardinal_ordinal import data.W.basic /-! # Cardinality of W-types This file proves some theorems about the cardinality of W-types. The main result is `cardinal_mk_le_max_omega_of_fintype` which says that if for any `a : α`, `β a` is finite, then the cardinality of `W_type β` is at most the maximum of the cardinality of `α` and `cardinal.omega`. This can be used to prove theorems about the cardinality of algebraic constructions such as polynomials. There is a surjection from a `W_type` to `mv_polynomial` for example, and this surjection can be used to put an upper bound on the cardinality of `mv_polynomial`. ## Tags W, W type, cardinal, first order -/ universe u variables {α : Type u} {β : α → Type u} noncomputable theory namespace W_type open_locale cardinal open cardinal lemma cardinal_mk_eq_sum : #(W_type β) = sum (λ a : α, #(W_type β) ^ #(β a)) := begin simp only [cardinal.power_def, ← cardinal.mk_sigma], exact mk_congr (equiv_sigma β) end /-- `#(W_type β)` is the least cardinal `κ` such that `sum (λ a : α, κ ^ #(β a)) ≤ κ` -/ lemma cardinal_mk_le_of_le {κ : cardinal.{u}} (hκ : sum (λ a : α, κ ^ #(β a)) ≤ κ) : #(W_type β) ≤ κ := begin induction κ using cardinal.induction_on with γ, simp only [cardinal.power_def, ← cardinal.mk_sigma, cardinal.le_def] at hκ, cases hκ, exact cardinal.mk_le_of_injective (elim_injective _ hκ.1 hκ.2) end /-- If, for any `a : α`, `β a` is finite, then the cardinality of `W_type β` is at most the maximum of the cardinality of `α` and `ω` -/ lemma cardinal_mk_le_max_omega_of_fintype [Π a, fintype (β a)] : #(W_type β) ≤ max (#α) ω := (is_empty_or_nonempty α).elim (begin introI h, rw [cardinal.mk_eq_zero (W_type β)], exact zero_le _ end) $ λ hn, let m := max (#α) ω in cardinal_mk_le_of_le $ calc cardinal.sum (λ a : α, m ^ #(β a)) ≤ #α * cardinal.sup.{u u} (λ a : α, m ^ cardinal.mk (β a)) : cardinal.sum_le_sup _ ... ≤ m * cardinal.sup.{u u} (λ a : α, m ^ #(β a)) : mul_le_mul' (le_max_left _ _) le_rfl ... = m : mul_eq_left.{u} (le_max_right _ _) (cardinal.sup_le.2 (λ i, begin cases lt_omega.1 (lt_omega_iff_fintype.2 ⟨show fintype (β i), by apply_instance⟩) with n hn, rw [hn], exact power_nat_le (le_max_right _ _) end)) (pos_iff_ne_zero.1 (succ_le.1 begin rw [succ_zero], obtain ⟨a⟩ : nonempty α, from hn, refine le_trans _ (le_sup _ a), rw [← @power_zero m], exact power_le_power_left (pos_iff_ne_zero.1 (lt_of_lt_of_le omega_pos (le_max_right _ _))) (zero_le _) end)) end W_type
27eca4dbfc6524c9067e3841330748f66283ce68
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/group/semiconj.lean
b47b881c6bcd948dd797091a29c8175e0e688f9a
[ "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
7,690
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov Some proofs and docs came from `algebra/commute` (c) Neil Strickland -/ import algebra.group.units /-! # Semiconjugate elements of a semigroup ## Main definitions We say that `x` is semiconjugate to `y` by `a` (`semiconj_by a x y`), if `a * x = y * a`. In this file we provide operations on `semiconj_by _ _ _`. In the names of these operations, we treat `a` as the “left” argument, and both `x` and `y` as “right” arguments. This way most names in this file agree with the names of the corresponding lemmas for `commute a b = semiconj_by a b b`. As a side effect, some lemmas have only `_right` version. Lean does not immediately recognise these terms as equations, so for rewriting we need syntax like `rw [(h.pow_right 5).eq]` rather than just `rw [h.pow_right 5]`. This file provides only basic operations (`mul_left`, `mul_right`, `inv_right` etc). Other operations (`pow_right`, field inverse etc) are in the files that define corresponding notions. -/ universes u v variables {G : Type*} /-- `x` is semiconjugate to `y` by `a`, if `a * x = y * a`. -/ @[to_additive add_semiconj_by "`x` is additive semiconjugate to `y` by `a` if `a + x = y + a`"] def semiconj_by {M : Type u} [has_mul M] (a x y : M) : Prop := a * x = y * a namespace semiconj_by /-- Equality behind `semiconj_by a x y`; useful for rewriting. -/ @[to_additive "Equality behind `add_semiconj_by a x y`; useful for rewriting."] protected lemma eq {S : Type u} [has_mul S] {a x y : S} (h : semiconj_by a x y) : a * x = y * a := h section semigroup variables {S : Type u} [semigroup S] {a b x y z x' y' : S} /-- If `a` semiconjugates `x` to `y` and `x'` to `y'`, then it semiconjugates `x * x'` to `y * y'`. -/ @[simp, to_additive "If `a` semiconjugates `x` to `y` and `x'` to `y'`, then it semiconjugates `x + x'` to `y + y'`."] lemma mul_right (h : semiconj_by a x y) (h' : semiconj_by a x' y') : semiconj_by a (x * x') (y * y') := by unfold semiconj_by; assoc_rw [h.eq, h'.eq] /-- If both `a` and `b` semiconjugate `x` to `y`, then so does `a * b`. -/ @[to_additive "If both `a` and `b` semiconjugate `x` to `y`, then so does `a + b`."] lemma mul_left (ha : semiconj_by a y z) (hb : semiconj_by b x y) : semiconj_by (a * b) x z := by unfold semiconj_by; assoc_rw [hb.eq, ha.eq, mul_assoc] /-- The relation “there exists an element that semiconjugates `a` to `b`” on a semigroup is transitive. -/ @[to_additive "The relation “there exists an element that semiconjugates `a` to `b`” on an additive semigroup is transitive."] protected lemma transitive : transitive (λ a b : S, ∃ c, semiconj_by c a b) := λ a b c ⟨x, hx⟩ ⟨y, hy⟩, ⟨y * x, hy.mul_left hx⟩ end semigroup section mul_one_class variables {M : Type u} [mul_one_class M] /-- Any element semiconjugates `1` to `1`. -/ @[simp, to_additive "Any element additively semiconjugates `0` to `0`."] lemma one_right (a : M) : semiconj_by a 1 1 := by rw [semiconj_by, mul_one, one_mul] /-- One semiconjugates any element to itself. -/ @[simp, to_additive "Zero additively semiconjugates any element to itself."] lemma one_left (x : M) : semiconj_by 1 x x := eq.symm $ one_right x /-- The relation “there exists an element that semiconjugates `a` to `b`” on a monoid (or, more generally, on ` mul_one_class` type) is reflexive. -/ @[to_additive "The relation “there exists an element that semiconjugates `a` to `b`” on an additive monoid (or, more generally, on a `add_zero_class` type) is reflexive."] protected lemma reflexive : reflexive (λ a b : M, ∃ c, semiconj_by c a b) := λ a, ⟨1, one_left a⟩ end mul_one_class section monoid variables {M : Type u} [monoid M] /-- If `a` semiconjugates a unit `x` to a unit `y`, then it semiconjugates `x⁻¹` to `y⁻¹`. -/ @[to_additive "If `a` semiconjugates an additive unit `x` to an additive unit `y`, then it semiconjugates `-x` to `-y`."] lemma units_inv_right {a : M} {x y : Mˣ} (h : semiconj_by a x y) : semiconj_by a ↑x⁻¹ ↑y⁻¹ := calc a * ↑x⁻¹ = ↑y⁻¹ * (y * a) * ↑x⁻¹ : by rw [units.inv_mul_cancel_left] ... = ↑y⁻¹ * a : by rw [← h.eq, mul_assoc, units.mul_inv_cancel_right] @[simp, to_additive] lemma units_inv_right_iff {a : M} {x y : Mˣ} : semiconj_by a ↑x⁻¹ ↑y⁻¹ ↔ semiconj_by a x y := ⟨units_inv_right, units_inv_right⟩ /-- If a unit `a` semiconjugates `x` to `y`, then `a⁻¹` semiconjugates `y` to `x`. -/ @[to_additive "If an additive unit `a` semiconjugates `x` to `y`, then `-a` semiconjugates `y` to `x`."] lemma units_inv_symm_left {a : Mˣ} {x y : M} (h : semiconj_by ↑a x y) : semiconj_by ↑a⁻¹ y x := calc ↑a⁻¹ * y = ↑a⁻¹ * (y * a * ↑a⁻¹) : by rw [units.mul_inv_cancel_right] ... = x * ↑a⁻¹ : by rw [← h.eq, ← mul_assoc, units.inv_mul_cancel_left] @[simp, to_additive] lemma units_inv_symm_left_iff {a : Mˣ} {x y : M} : semiconj_by ↑a⁻¹ y x ↔ semiconj_by ↑a x y := ⟨units_inv_symm_left, units_inv_symm_left⟩ @[to_additive] theorem units_coe {a x y : Mˣ} (h : semiconj_by a x y) : semiconj_by (a : M) x y := congr_arg units.val h @[to_additive] theorem units_of_coe {a x y : Mˣ} (h : semiconj_by (a : M) x y) : semiconj_by a x y := units.ext h @[simp, to_additive] theorem units_coe_iff {a x y : Mˣ} : semiconj_by (a : M) x y ↔ semiconj_by a x y := ⟨units_of_coe, units_coe⟩ @[simp, to_additive] lemma pow_right {a x y : M} (h : semiconj_by a x y) (n : ℕ) : semiconj_by a (x^n) (y^n) := begin induction n with n ih, { rw [pow_zero, pow_zero], exact semiconj_by.one_right _ }, { rw [pow_succ, pow_succ], exact h.mul_right ih } end end monoid section division_monoid variables [division_monoid G] {a x y : G} @[simp, to_additive] lemma inv_inv_symm_iff : semiconj_by a⁻¹ x⁻¹ y⁻¹ ↔ semiconj_by a y x := inv_involutive.injective.eq_iff.symm.trans $ by simp_rw [mul_inv_rev, inv_inv, eq_comm, semiconj_by] @[to_additive] lemma inv_inv_symm : semiconj_by a x y → semiconj_by a⁻¹ y⁻¹ x⁻¹ := inv_inv_symm_iff.2 end division_monoid section group variables [group G] {a x y : G} @[simp, to_additive] lemma inv_right_iff : semiconj_by a x⁻¹ y⁻¹ ↔ semiconj_by a x y := @units_inv_right_iff G _ a ⟨x, x⁻¹, mul_inv_self x, inv_mul_self x⟩ ⟨y, y⁻¹, mul_inv_self y, inv_mul_self y⟩ @[to_additive] lemma inv_right : semiconj_by a x y → semiconj_by a x⁻¹ y⁻¹ := inv_right_iff.2 @[simp, to_additive] lemma inv_symm_left_iff : semiconj_by a⁻¹ y x ↔ semiconj_by a x y := @units_inv_symm_left_iff G _ ⟨a, a⁻¹, mul_inv_self a, inv_mul_self a⟩ _ _ @[to_additive] lemma inv_symm_left : semiconj_by a x y → semiconj_by a⁻¹ y x := inv_symm_left_iff.2 /-- `a` semiconjugates `x` to `a * x * a⁻¹`. -/ @[to_additive "`a` semiconjugates `x` to `a + x + -a`."] lemma conj_mk (a x : G) : semiconj_by a x (a * x * a⁻¹) := by unfold semiconj_by; rw [mul_assoc, inv_mul_self, mul_one] end group end semiconj_by @[simp, to_additive add_semiconj_by_iff_eq] lemma semiconj_by_iff_eq {M : Type u} [cancel_comm_monoid M] {a x y : M} : semiconj_by a x y ↔ x = y := ⟨λ h, mul_left_cancel (h.trans (mul_comm _ _)), λ h, by rw [h, semiconj_by, mul_comm] ⟩ /-- `a` semiconjugates `x` to `a * x * a⁻¹`. -/ @[to_additive "`a` semiconjugates `x` to `a + x + -a`."] lemma units.mk_semiconj_by {M : Type u} [monoid M] (u : Mˣ) (x : M) : semiconj_by ↑u x (u * x * ↑u⁻¹) := by unfold semiconj_by; rw [units.inv_mul_cancel_right]
2eda50cc3770fa1de1717b767c86e255a44cd482
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/convex/slope.lean
dd5cc0fac92730d0e8e23b4ba1f9a60f20e96fc5
[ "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
10,406
lean
/- Copyright (c) 2021 Yury Kudriashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudriashov, Malo Jaffré -/ import analysis.convex.function /-! # Slopes of convex functions This file relates convexity/concavity of functions in a linearly ordered field and the monotonicity of their slopes. The main use is to show convexity/concavity from monotonicity of the derivative. -/ variables {𝕜 : Type*} [linear_ordered_field 𝕜] {s : set 𝕜} {f : 𝕜 → 𝕜} /-- If `f : 𝕜 → 𝕜` is convex, then for any three points `x < y < z` the slope of the secant line of `f` on `[x, y]` is less than the slope of the secant line of `f` on `[x, z]`. -/ lemma convex_on.slope_mono_adjacent (hf : convex_on 𝕜 s f) {x y z : 𝕜} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f y - f x) / (y - x) ≤ (f z - f y) / (z - y) := begin have hxz := hxy.trans hyz, rw ←sub_pos at hxy hxz hyz, suffices : f y / (y - x) + f y / (z - y) ≤ f x / (y - x) + f z / (z - y), { ring_nf at this ⊢, linarith }, set a := (z - y) / (z - x), set b := (y - x) / (z - x), have hy : a • x + b • z = y, by { field_simp, rw div_eq_iff; [ring, linarith] }, have key, from hf.2 hx hz (show 0 ≤ a, by apply div_nonneg; linarith) (show 0 ≤ b, by apply div_nonneg; linarith) (show a + b = 1, by { field_simp, rw div_eq_iff; [ring, linarith] }), rw hy at key, replace key := mul_le_mul_of_nonneg_left key hxz.le, field_simp [hxy.ne', hyz.ne', hxz.ne', mul_comm (z - x) _] at key ⊢, rw div_le_div_right, { linarith }, { nlinarith } end /-- If `f : 𝕜 → 𝕜` is concave, then for any three points `x < y < z` the slope of the secant line of `f` on `[x, y]` is greater than the slope of the secant line of `f` on `[x, z]`. -/ lemma concave_on.slope_anti_adjacent (hf : concave_on 𝕜 s f) {x y z : 𝕜} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f z - f y) / (z - y) ≤ (f y - f x) / (y - x) := begin rw [←neg_le_neg_iff, ←neg_sub_neg (f x), ←neg_sub_neg (f y)], simp_rw [←pi.neg_apply, ←neg_div, neg_sub], exact convex_on.slope_mono_adjacent hf.neg hx hz hxy hyz, end /-- If `f : 𝕜 → 𝕜` is strictly convex, then for any three points `x < y < z` the slope of the secant line of `f` on `[x, y]` is strictly less than the slope of the secant line of `f` on `[x, z]`. -/ lemma strict_convex_on.slope_strict_mono_adjacent (hf : strict_convex_on 𝕜 s f) {x y z : 𝕜} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f y - f x) / (y - x) < (f z - f y) / (z - y) := begin have hxz := hxy.trans hyz, have hxz' := hxz.ne, rw ←sub_pos at hxy hxz hyz, suffices : f y / (y - x) + f y / (z - y) < f x / (y - x) + f z / (z - y), { ring_nf at this ⊢, linarith }, set a := (z - y) / (z - x), set b := (y - x) / (z - x), have hy : a • x + b • z = y, by { field_simp, rw div_eq_iff; [ring, linarith] }, have key, from hf.2 hx hz hxz' (div_pos hyz hxz) (div_pos hxy hxz) (show a + b = 1, by { field_simp, rw div_eq_iff; [ring, linarith] }), rw hy at key, replace key := mul_lt_mul_of_pos_left key hxz, field_simp [hxy.ne', hyz.ne', hxz.ne', mul_comm (z - x) _] at key ⊢, rw div_lt_div_right, { linarith }, { nlinarith } end /-- If `f : 𝕜 → 𝕜` is strictly concave, then for any three points `x < y < z` the slope of the secant line of `f` on `[x, y]` is strictly greater than the slope of the secant line of `f` on `[x, z]`. -/ lemma strict_concave_on.slope_anti_adjacent (hf : strict_concave_on 𝕜 s f) {x y z : 𝕜} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f z - f y) / (z - y) < (f y - f x) / (y - x) := begin rw [←neg_lt_neg_iff, ←neg_sub_neg (f x), ←neg_sub_neg (f y)], simp_rw [←pi.neg_apply, ←neg_div, neg_sub], exact strict_convex_on.slope_strict_mono_adjacent hf.neg hx hz hxy hyz, end /-- If for any three points `x < y < z`, the slope of the secant line of `f : 𝕜 → 𝕜` on `[x, y]` is less than the slope of the secant line of `f` on `[x, z]`, then `f` is convex. -/ lemma convex_on_of_slope_mono_adjacent (hs : convex 𝕜 s) (hf : ∀ {x y z : 𝕜}, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) ≤ (f z - f y) / (z - y)) : convex_on 𝕜 s f := linear_order.convex_on_of_lt hs begin assume x z hx hz hxz a b ha hb hab, let y := a * x + b * z, have hxy : x < y, { rw [← one_mul x, ← hab, add_mul], exact add_lt_add_left ((mul_lt_mul_left hb).2 hxz) _ }, have hyz : y < z, { rw [← one_mul z, ← hab, add_mul], exact add_lt_add_right ((mul_lt_mul_left ha).2 hxz) _ }, have : (f y - f x) * (z - y) ≤ (f z - f y) * (y - x), from (div_le_div_iff (sub_pos.2 hxy) (sub_pos.2 hyz)).1 (hf hx hz hxy hyz), have hxz : 0 < z - x, from sub_pos.2 (hxy.trans hyz), have ha : (z - y) / (z - x) = a, { rw [eq_comm, ← sub_eq_iff_eq_add'] at hab, simp_rw [div_eq_iff hxz.ne', y, ←hab], ring }, have hb : (y - x) / (z - x) = b, { rw [eq_comm, ← sub_eq_iff_eq_add] at hab, simp_rw [div_eq_iff hxz.ne', y, ←hab], ring }, rwa [sub_mul, sub_mul, sub_le_iff_le_add', ← add_sub_assoc, le_sub_iff_add_le, ← mul_add, sub_add_sub_cancel, ← le_div_iff hxz, add_div, mul_div_assoc, mul_div_assoc, mul_comm (f x), mul_comm (f z), ha, hb] at this, end /-- If for any three points `x < y < z`, the slope of the secant line of `f : 𝕜 → 𝕜` on `[x, y]` is greater than the slope of the secant line of `f` on `[x, z]`, then `f` is concave. -/ lemma concave_on_of_slope_anti_adjacent (hs : convex 𝕜 s) (hf : ∀ {x y z : 𝕜}, x ∈ s → z ∈ s → x < y → y < z → (f z - f y) / (z - y) ≤ (f y - f x) / (y - x)) : concave_on 𝕜 s f := begin rw ←neg_convex_on_iff, refine convex_on_of_slope_mono_adjacent hs (λ x y z hx hz hxy hyz, _), rw ←neg_le_neg_iff, simp_rw [←neg_div, neg_sub, pi.neg_apply, neg_sub_neg], exact hf hx hz hxy hyz, end /-- If for any three points `x < y < z`, the slope of the secant line of `f : 𝕜 → 𝕜` on `[x, y]` is strictly less than the slope of the secant line of `f` on `[x, z]`, then `f` is strictly convex. -/ lemma strict_convex_on_of_slope_strict_mono_adjacent (hs : convex 𝕜 s) (hf : ∀ {x y z : 𝕜}, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) < (f z - f y) / (z - y)) : strict_convex_on 𝕜 s f := linear_order.strict_convex_on_of_lt hs begin assume x z hx hz hxz a b ha hb hab, let y := a * x + b * z, have hxy : x < y, { rw [← one_mul x, ← hab, add_mul], exact add_lt_add_left ((mul_lt_mul_left hb).2 hxz) _ }, have hyz : y < z, { rw [← one_mul z, ← hab, add_mul], exact add_lt_add_right ((mul_lt_mul_left ha).2 hxz) _ }, have : (f y - f x) * (z - y) < (f z - f y) * (y - x), from (div_lt_div_iff (sub_pos.2 hxy) (sub_pos.2 hyz)).1 (hf hx hz hxy hyz), have hxz : 0 < z - x, from sub_pos.2 (hxy.trans hyz), have ha : (z - y) / (z - x) = a, { rw [eq_comm, ← sub_eq_iff_eq_add'] at hab, simp_rw [div_eq_iff hxz.ne', y, ←hab], ring }, have hb : (y - x) / (z - x) = b, { rw [eq_comm, ← sub_eq_iff_eq_add] at hab, simp_rw [div_eq_iff hxz.ne', y, ←hab], ring }, rwa [sub_mul, sub_mul, sub_lt_iff_lt_add', ← add_sub_assoc, lt_sub_iff_add_lt, ← mul_add, sub_add_sub_cancel, ← lt_div_iff hxz, add_div, mul_div_assoc, mul_div_assoc, mul_comm (f x), mul_comm (f z), ha, hb] at this, end /-- If for any three points `x < y < z`, the slope of the secant line of `f : 𝕜 → 𝕜` on `[x, y]` is strictly greater than the slope of the secant line of `f` on `[x, z]`, then `f` is strictly concave. -/ lemma strict_concave_on_of_slope_strict_anti_adjacent (hs : convex 𝕜 s) (hf : ∀ {x y z : 𝕜}, x ∈ s → z ∈ s → x < y → y < z → (f z - f y) / (z - y) < (f y - f x) / (y - x)) : strict_concave_on 𝕜 s f := begin rw ←neg_strict_convex_on_iff, refine strict_convex_on_of_slope_strict_mono_adjacent hs (λ x y z hx hz hxy hyz, _), rw ←neg_lt_neg_iff, simp_rw [←neg_div, neg_sub, pi.neg_apply, neg_sub_neg], exact hf hx hz hxy hyz, end /-- A function `f : 𝕜 → 𝕜` is convex iff for any three points `x < y < z` the slope of the secant line of `f` on `[x, y]` is less than the slope of the secant line of `f` on `[x, z]`. -/ lemma convex_on_iff_slope_mono_adjacent : convex_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x y z : 𝕜⦄, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) ≤ (f z - f y) / (z - y) := ⟨λ h, ⟨h.1, λ x y z, h.slope_mono_adjacent⟩, λ h, convex_on_of_slope_mono_adjacent h.1 h.2⟩ /-- A function `f : 𝕜 → 𝕜` is concave iff for any three points `x < y < z` the slope of the secant line of `f` on `[x, y]` is greater than the slope of the secant line of `f` on `[x, z]`. -/ lemma concave_on_iff_slope_anti_adjacent : concave_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x y z : 𝕜⦄, x ∈ s → z ∈ s → x < y → y < z → (f z - f y) / (z - y) ≤ (f y - f x) / (y - x) := ⟨λ h, ⟨h.1, λ x y z, h.slope_anti_adjacent⟩, λ h, concave_on_of_slope_anti_adjacent h.1 h.2⟩ /-- A function `f : 𝕜 → 𝕜` is strictly convex iff for any three points `x < y < z` the slope of the secant line of `f` on `[x, y]` is strictly less than the slope of the secant line of `f` on `[x, z]`. -/ lemma strict_convex_on_iff_slope_strict_mono_adjacent : strict_convex_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x y z : 𝕜⦄, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) < (f z - f y) / (z - y) := ⟨λ h, ⟨h.1, λ x y z, h.slope_strict_mono_adjacent⟩, λ h, strict_convex_on_of_slope_strict_mono_adjacent h.1 h.2⟩ /-- A function `f : 𝕜 → 𝕜` is strictly concave iff for any three points `x < y < z` the slope of the secant line of `f` on `[x, y]` is strictly greater than the slope of the secant line of `f` on `[x, z]`. -/ lemma strict_concave_on_iff_slope_strict_anti_adjacent : strict_concave_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x y z : 𝕜⦄, x ∈ s → z ∈ s → x < y → y < z → (f z - f y) / (z - y) < (f y - f x) / (y - x) := ⟨λ h, ⟨h.1, λ x y z, h.slope_anti_adjacent⟩, λ h, strict_concave_on_of_slope_strict_anti_adjacent h.1 h.2⟩
d0598257802082c67a937fc806b246c6dec55d3c
28be2ab6091504b6ba250b367205fb94d50ab284
/src/game/world3/level7.lean
b7021c143f634beb8dc14012278839f27f78dec2
[ "Apache-2.0" ]
permissive
postmasters/natural_number_game
87304ac22e5e1c5ac2382d6e523d6914dd67a92d
38a7adcdfdb18c49c87b37831736c8f15300d821
refs/heads/master
1,649,856,819,031
1,586,444,676,000
1,586,444,676,000
255,006,061
0
0
Apache-2.0
1,586,664,599,000
1,586,664,598,000
null
UTF-8
Lean
false
false
1,932
lean
import game.world3.level6 -- hide namespace mynat -- hide /- # Multiplication World ## Level 7: `add_mul` We proved `mul_add` already, but because we don't have commutativity yet we also need to prove `add_mul`. We have a bunch of tools now, so this won't be too hard. You know what -- you can do this one by induction on any of the variables. Try them all! Which works best? If you can't face doing all the commutativity and associativity, remember the high-powered `simp` tactic mentioned at the bottom of Addition World level 6, which will solve any puzzle which needs only commutativity and associativity. If your goal looks like `a+(b+c)=c+b+a` or something, don't mess around doing it explicitly with `add_comm` and `add_assoc`, just try `simp`. -/ /- Lemma Addition is distributive over multiplication. In other words, for all natural numbers $a$, $b$ and $t$, we have $$ (a + b) \times t = at + bt. $$ -/ lemma add_mul (a b t : mynat) : (a + b) * t = a * t + b * t := begin [nat_num_game] induction b with d hd, { rw zero_mul, rw add_zero, rw add_zero, refl }, { rw add_succ, rw succ_mul, rw hd, rw succ_mul, rw add_assoc, refl } end /- A mathematician would now say that you have proved that the natural numbers are a semiring. This sounds like a respectable result. -/ def right_distrib := add_mul -- alternative name def collectible_05 : semiring mynat := by structure_helper /- Lean would add that you have also proved that they are a `distrib`. However this concept has no mathematical name at all -- this says something about the regard with which mathematicians hold this collectible. This is an artefact of the set-up of collectibles in Lean. You consider politely declining Lean's offer of a `distrib` collectible. You are dreaming of the big collectible at the end of power world. -/ example : distrib mynat := by structure_helper -- end mynat -- hide
4fad580c6f58d7d0d32fb07e14b88ff384585b7b
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/essentially_small.lean
cb4fa7f70fd7751ae1e53c891b1e6f90090c2753
[ "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
7,620
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 logic.small.basic import category_theory.category.ulift import category_theory.skeletal /-! # Essentially small categories. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. A category given by `(C : Type u) [category.{v} C]` is `w`-essentially small if there exists a `small_model C : Type w` equipped with `[small_category (small_model C)]`. A category is `w`-locally small if every hom type is `w`-small. The main theorem here is that a category is `w`-essentially small iff the type `skeleton C` is `w`-small, and `C` is `w`-locally small. -/ universes w v v' u u' open category_theory variables (C : Type u) [category.{v} C] namespace category_theory /-- A category is `essentially_small.{w}` if there exists an equivalence to some `S : Type w` with `[small_category S]`. -/ class essentially_small (C : Type u) [category.{v} C] : Prop := (equiv_small_category : ∃ (S : Type w) (_ : small_category S), by exactI nonempty (C ≌ S)) /-- Constructor for `essentially_small C` from an explicit small category witness. -/ lemma essentially_small.mk' {C : Type u} [category.{v} C] {S : Type w} [small_category S] (e : C ≌ S) : essentially_small.{w} C := ⟨⟨S, _, ⟨e⟩⟩⟩ /-- An arbitrarily chosen small model for an essentially small category. -/ @[nolint has_nonempty_instance] def small_model (C : Type u) [category.{v} C] [essentially_small.{w} C] : Type w := classical.some (@essentially_small.equiv_small_category C _ _) noncomputable instance small_category_small_model (C : Type u) [category.{v} C] [essentially_small.{w} C] : small_category (small_model C) := classical.some (classical.some_spec (@essentially_small.equiv_small_category C _ _)) /-- The (noncomputable) categorical equivalence between an essentially small category and its small model. -/ noncomputable def equiv_small_model (C : Type u) [category.{v} C] [essentially_small.{w} C] : C ≌ small_model C := nonempty.some (classical.some_spec (classical.some_spec (@essentially_small.equiv_small_category C _ _))) lemma essentially_small_congr {C : Type u} [category.{v} C] {D : Type u'} [category.{v'} D] (e : C ≌ D) : essentially_small.{w} C ↔ essentially_small.{w} D := begin fsplit, { rintro ⟨S, 𝒮, ⟨f⟩⟩, resetI, exact essentially_small.mk' (e.symm.trans f), }, { rintro ⟨S, 𝒮, ⟨f⟩⟩, resetI, exact essentially_small.mk' (e.trans f), }, end lemma discrete.essentially_small_of_small {α : Type u} [small.{w} α] : essentially_small.{w} (discrete α) := ⟨⟨discrete (shrink α), ⟨infer_instance, ⟨discrete.equivalence (equiv_shrink _)⟩⟩⟩⟩ lemma essentially_small_self : essentially_small.{max w v u} C := essentially_small.mk' (as_small.equiv : C ≌ as_small.{w} C) /-- A category is `w`-locally small if every hom set is `w`-small. See `shrink_homs C` for a category instance where every hom set has been replaced by a small model. -/ class locally_small (C : Type u) [category.{v} C] : Prop := (hom_small : ∀ X Y : C, small.{w} (X ⟶ Y) . tactic.apply_instance) instance (C : Type u) [category.{v} C] [locally_small.{w} C] (X Y : C) : small (X ⟶ Y) := locally_small.hom_small X Y lemma locally_small_congr {C : Type u} [category.{v} C] {D : Type u'} [category.{v'} D] (e : C ≌ D) : locally_small.{w} C ↔ locally_small.{w} D := begin fsplit, { rintro ⟨L⟩, fsplit, intros X Y, specialize L (e.inverse.obj X) (e.inverse.obj Y), refine (small_congr _).mpr L, exact equiv_of_fully_faithful e.inverse, }, { rintro ⟨L⟩, fsplit, intros X Y, specialize L (e.functor.obj X) (e.functor.obj Y), refine (small_congr _).mpr L, exact equiv_of_fully_faithful e.functor, }, end @[priority 100] instance locally_small_self (C : Type u) [category.{v} C] : locally_small.{v} C := {} @[priority 100] instance locally_small_of_essentially_small (C : Type u) [category.{v} C] [essentially_small.{w} C] : locally_small.{w} C := (locally_small_congr (equiv_small_model C)).mpr (category_theory.locally_small_self _) /-- We define a type alias `shrink_homs C` for `C`. When we have `locally_small.{w} C`, we'll put a `category.{w}` instance on `shrink_homs C`. -/ @[nolint has_nonempty_instance] def shrink_homs (C : Type u) := C namespace shrink_homs section variables {C' : Type*} -- a fresh variable with no category instance attached /-- Help the typechecker by explicitly translating from `C` to `shrink_homs C`. -/ def to_shrink_homs {C' : Type*} (X : C') : shrink_homs C' := X /-- Help the typechecker by explicitly translating from `shrink_homs C` to `C`. -/ def from_shrink_homs {C' : Type*} (X : shrink_homs C') : C' := X @[simp] lemma to_from (X : C') : from_shrink_homs (to_shrink_homs X) = X := rfl @[simp] lemma from_to (X : shrink_homs C') : to_shrink_homs (from_shrink_homs X) = X := rfl end variables (C) [locally_small.{w} C] @[simps] noncomputable instance : category.{w} (shrink_homs C) := { hom := λ X Y, shrink (from_shrink_homs X ⟶ from_shrink_homs Y), id := λ X, equiv_shrink _ (𝟙 (from_shrink_homs X)), comp := λ X Y Z f g, equiv_shrink _ (((equiv_shrink _).symm f) ≫ ((equiv_shrink _).symm g)), }. /-- Implementation of `shrink_homs.equivalence`. -/ @[simps] noncomputable def functor : C ⥤ shrink_homs C := { obj := λ X, to_shrink_homs X, map := λ X Y f, equiv_shrink (X ⟶ Y) f, } /-- Implementation of `shrink_homs.equivalence`. -/ @[simps] noncomputable def inverse : shrink_homs C ⥤ C := { obj := λ X, from_shrink_homs X, map := λ X Y f, (equiv_shrink (from_shrink_homs X ⟶ from_shrink_homs Y)).symm f, } /-- The categorical equivalence between `C` and `shrink_homs C`, when `C` is locally small. -/ @[simps] noncomputable def equivalence : C ≌ shrink_homs C := equivalence.mk (functor C) (inverse C) (nat_iso.of_components (λ X, iso.refl X) (by tidy)) (nat_iso.of_components (λ X, iso.refl X) (by tidy)) end shrink_homs /-- A category is essentially small if and only if the underlying type of its skeleton (i.e. the "set" of isomorphism classes) is small, and it is locally small. -/ theorem essentially_small_iff (C : Type u) [category.{v} C] : essentially_small.{w} C ↔ small.{w} (skeleton C) ∧ locally_small.{w} C := begin -- This theorem is the only bit of real work in this file. fsplit, { intro h, fsplit, { rcases h with ⟨S, 𝒮, ⟨e⟩⟩, resetI, refine ⟨⟨skeleton S, ⟨_⟩⟩⟩, exact e.skeleton_equiv, }, { resetI, apply_instance, }, }, { rintro ⟨⟨S, ⟨e⟩⟩, L⟩, resetI, let e' := (shrink_homs.equivalence C).skeleton_equiv.symm, refine ⟨⟨S, _, ⟨_⟩⟩⟩, apply induced_category.category (e'.trans e).symm, refine (shrink_homs.equivalence C).trans ((skeleton_equivalence _).symm.trans ((induced_functor (e'.trans e).symm).as_equivalence.symm)), }, end /-- Any thin category is locally small. -/ @[priority 100] instance locally_small_of_thin {C : Type u} [category.{v} C] [quiver.is_thin C] : locally_small.{w} C := {} /-- A thin category is essentially small if and only if the underlying type of its skeleton is small. -/ theorem essentially_small_iff_of_thin {C : Type u} [category.{v} C] [quiver.is_thin C] : essentially_small.{w} C ↔ small.{w} (skeleton C) := by simp [essentially_small_iff, category_theory.locally_small_of_thin] end category_theory
e55fe60f9ae8c3afa768261c8a9bc802bb742c6e
ee8cdbabf07f77e7be63a449b8483ce308d37218
/lean/src/valid/mathd-numbertheory-136.lean
6b577e0f8bbea43f3477fda59eaa2af14deecc75
[ "MIT", "Apache-2.0" ]
permissive
zeta1999/miniF2F
6d66c75d1c18152e224d07d5eed57624f731d4b7
c1ba9629559c5273c92ec226894baa0c1ce27861
refs/heads/main
1,681,897,460,642
1,620,646,361,000
1,620,646,361,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
292
lean
/- Copyright (c) 2021 OpenAI. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kunhao Zheng -/ import tactic.gptf import data.nat.basic import data.real.basic example (n : ℕ) (h₀ : 123 * n + 17 = 39500) : n = 321 := begin linarith, end
ce368d8a9886b2d4415f70251b08a1e8ea057c11
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Modifiers.lean
3048be1280219fcf5f7b7dc9e40d191a7e0974a0
[ "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
2,039
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.Environment namespace Lean builtin_initialize protectedExt : TagDeclarationExtension ← mkTagDeclarationExtension `protected @[export lean_add_protected] def addProtected (env : Environment) (n : Name) : Environment := protectedExt.tag env n @[export lean_is_protected] def isProtected (env : Environment) (n : Name) : Bool := protectedExt.isTagged env n /-! # Private name support. Suppose the user marks as declaration `n` as private. Then, we create the name: `_private.<module_name>.0 ++ n`. We say `_private.<module_name>.0` is the "private prefix" We assume that `n` is a valid user name and does not contain `Name.num` constructors. Thus, we can easily convert from private internal name to the user given name. -/ def privateHeader : Name := `_private def mkPrivateName (env : Environment) (n : Name) : Name := Name.mkNum (privateHeader ++ env.mainModule) 0 ++ n def isPrivateName : Name → Bool | n@(.str p _) => n == privateHeader || isPrivateName p | .num p _ => isPrivateName p | _ => false @[export lean_is_private_name] def isPrivateNameExport (n : Name) : Bool := isPrivateName n private def privateToUserNameAux : Name → Name | .str p s => Name.mkStr (privateToUserNameAux p) s | _ => Name.anonymous @[export lean_private_to_user_name] def privateToUserName? (n : Name) : Option Name := if isPrivateName n then privateToUserNameAux n else none def isPrivateNameFromImportedModule (env : Environment) (n : Name) : Bool := match privateToUserName? n with | some userName => mkPrivateName env userName != n | _ => false private def privatePrefixAux : Name → Name | .str p _ => privatePrefixAux p | n => n @[export lean_private_prefix] def privatePrefix? (n : Name) : Option Name := if isPrivateName n then privatePrefixAux n else none end Lean
98f3158f6156f7f7041f5f941a57a26327c98b53
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/char_p/exp_char.lean
a17a5b34cea7674f5252a6a50e1014e4acd347b0
[ "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
4,139
lean
/- Copyright (c) 2021 Jakob Scholbach. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob Scholbach -/ import algebra.char_p.basic import algebra.char_zero import data.nat.prime /-! # Exponential characteristic This file defines the exponential characteristic and establishes a few basic results relating it to the (ordinary characteristic). The definition is stated for a semiring, but the actual results are for nontrivial rings (as far as exponential characteristic one is concerned), respectively a ring without zero-divisors (for prime characteristic). ## Main results - `exp_char`: the definition of exponential characteristic - `exp_char_is_prime_or_one`: the exponential characteristic is a prime or one - `char_eq_exp_char_iff`: the characteristic equals the exponential characteristic iff the characteristic is prime ## Tags exponential characteristic, characteristic -/ universe u variables (R : Type u) section semiring variables [semiring R] /-- The definition of the exponential characteristic of a semiring. -/ class inductive exp_char (R : Type u) [semiring R] : ℕ → Prop | zero [char_zero R] : exp_char 1 | prime {q : ℕ} (hprime : q.prime) [hchar : char_p R q] : exp_char q /-- The exponential characteristic is one if the characteristic is zero. -/ lemma exp_char_one_of_char_zero (q : ℕ) [hp : char_p R 0] [hq : exp_char R q] : q = 1 := begin casesI hq with q hq_one hq_prime, { refl }, { exact false.elim (lt_irrefl _ ((hp.eq R hq_hchar).symm ▸ hq_prime : (0 : ℕ).prime).pos) } end /-- The characteristic equals the exponential characteristic iff the former is prime. -/ theorem char_eq_exp_char_iff (p q : ℕ) [hp : char_p R p] [hq : exp_char R q] : p = q ↔ p.prime := begin casesI hq with q hq_one hq_prime, { split, { unfreezingI {rintro rfl}, exact false.elim (one_ne_zero (hp.eq R (char_p.of_char_zero R))) }, { intro pprime, rw (char_p.eq R hp infer_instance : p = 0) at pprime, exact false.elim (nat.not_prime_zero pprime) } }, { split, { intro hpq, rw hpq, exact hq_prime, }, { intro _, exact char_p.eq R hp hq_hchar } }, end section nontrivial variables [nontrivial R] /-- The exponential characteristic is one if the characteristic is zero. -/ lemma char_zero_of_exp_char_one (p : ℕ) [hp : char_p R p] [hq : exp_char R 1] : p = 0 := begin casesI hq, { exact char_p.eq R hp infer_instance, }, { exact false.elim (char_p.char_ne_one R 1 rfl), } end /-- The exponential characteristic is one if the characteristic is zero. -/ @[priority 100] -- see Note [lower instance priority] instance char_zero_of_exp_char_one' [hq : exp_char R 1] : char_zero R := begin casesI hq, { assumption, }, { exact false.elim (char_p.char_ne_one R 1 rfl), } end /-- The exponential characteristic is one iff the characteristic is zero. -/ theorem exp_char_one_iff_char_zero (p q : ℕ) [char_p R p] [exp_char R q] : q = 1 ↔ p = 0 := begin split, { unfreezingI {rintro rfl}, exact char_zero_of_exp_char_one R p, }, { unfreezingI {rintro rfl}, exact exp_char_one_of_char_zero R q, } end section no_zero_divisors variable [no_zero_divisors R] /-- A helper lemma: the characteristic is prime if it is non-zero. -/ lemma char_prime_of_ne_zero {p : ℕ} [hp : char_p R p] (p_ne_zero : p ≠ 0) : nat.prime p := begin cases char_p.char_is_prime_or_zero R p with h h, { exact h, }, { contradiction, } end /-- The exponential characteristic is a prime number or one. -/ theorem exp_char_is_prime_or_one (q : ℕ) [hq : exp_char R q] : nat.prime q ∨ q = 1 := or_iff_not_imp_right.mpr $ λ h, begin casesI char_p.exists R with p hp, have p_ne_zero : p ≠ 0, { intro p_zero, haveI : char_p R 0 := by { rwa ←p_zero }, have : q = 1 := exp_char_one_of_char_zero R q, contradiction, }, have p_eq_q : p = q := (char_eq_exp_char_iff R p q).mpr (char_prime_of_ne_zero R p_ne_zero), cases char_p.char_is_prime_or_zero R p with pprime, { rwa p_eq_q at pprime }, { contradiction }, end end no_zero_divisors end nontrivial end semiring
6ae9e204964597dcccb41a94ff0b0f5da80324c2
2ee768fce4d0783536570385ee340d97e2411436
/src/q_prop.lean
54bd36f024a3097243cb294bf753d5b61e070dce
[]
no_license
agjftucker/exists-unique
835cf0c0ec622d49dbceb84be37db62e6ab8a6d0
1fba6035a08347f424c765d8a1c56b1a15c8abbe
refs/heads/master
1,683,445,592,336
1,622,649,822,000
1,622,649,822,000
309,485,697
2
0
null
null
null
null
UTF-8
Lean
false
false
3,075
lean
import q_def local prefix `𝒫`:100 := λ {α : Type} (s : finset α), {t // t ≤ s} section variables {α : Type*} [semilattice_sup_bot α] /-- To derive our conclusions about `q r` we require of relation `r` certain abstract properties. Let `r b` denote the set comprising those elements `c` for which `r b c`. Then `r` is a `support_rel` if each `r b` forms a `semilattice_sup_bot` and if `r` is nondecreasing, i.e. `d ≤ c` implies `r d ⊆ r c`. -/ class support_rel {α β} [preorder α] [semilattice_sup_bot β] (r : α → β → Prop) : Prop := (bottom : ∀ (b : α), r b ⊥) (sup : ∀ (b c : β) (d : α), r d b → r d c → r d (b ⊔ c)) (mono : ∀ (c d : α), d ≤ c → ∀ (b : β), r d b → r c b) variables {r : α → α → Prop} [ht : support_rel r] include ht lemma r_self_of_q {b : α} : q r b → r b b := begin intro hqb, cases hqb with _ c hlt hr hqc, { exact support_rel.bottom ⊥, }, { exact support_rel.mono b c (le_of_lt hlt) b hr, }, end lemma r_joins_of_q {b c d : α} : r d c → q r b → r (b ⊔ d) (b ⊔ c) := begin intros hr hq, apply support_rel.sup, { exact support_rel.mono (b ⊔ d) b le_sup_left b (r_self_of_q hq), }, { exact support_rel.mono (b ⊔ d) d le_sup_right c hr, }, end lemma q_sup_of_foreach {b c : α} : q r b → q r c → q r (b ⊔ c) := begin intros hb hc, induction hc with d e hed hr hq hq', { rwa sup_bot_eq, }, { cases lt_or_eq_of_le (sup_le_sup_left (le_of_lt hed) b) with hlt he, { exact q.succ hlt (r_joins_of_q hr hb) hq', }, { rwa he at hq', }, }, end lemma q_sup_of_forall (s : finset α) : (∀ b ∈ s, q r b) → q r (s.sup id) := begin apply finset.sup_induction, apply q.base, apply q_sup_of_foreach, end lemma exists_ge_term_of_q {a : α} : q r a → ∀ c < a, q r c → ∃ b, c ≤ b ∧ b < a ∧ q r b ∧ r b a := begin suffices : ∀ d ≤ a, q r d → ∀ c < a, q r c → ∃ b, c ≤ b ∧ b < a ∧ q r b ∧ r b (c ⊔ d), { intros hqa c hca hqc, specialize this a (le_refl a) hqa c hca hqc, rwa sup_of_le_right (le_of_lt hca) at this, }, intros d hle hqd c hca hqc, induction hqd with e f hfe hr hqf h₄ h₅, { rw sup_bot_eq, use [c, le_refl c, hca, hqc, r_self_of_q hqc], }, { have hfa := le_trans (le_of_lt hfe) hle, cases lt_or_eq_of_le (sup_le (le_of_lt hca) hfa) with hlt he, { use [c ⊔ f, le_sup_left, hlt, q_sup_of_foreach hqc hqf], apply r_joins_of_q hr hqc, }, { rcases h₄ hfa with ⟨b, hb₁, hb₂, hb₃, hb₄⟩, use [b, hb₁, hb₂, hb₃], suffices : c ⊔ f = c ⊔ e, { rwa this at hb₄, }, apply le_antisymm, { exact sup_le_sup_left (le_of_lt hfe) c, }, { rw he, exact sup_le (le_of_lt hca) hle, }, }, }, end end namespace si variables {β : Type} [decidable_eq β] {A : finset β} variables {r : 𝒫 A → 𝒫 A → Prop} [decidable_rel r] [ht : support_rel r] include ht lemma q_φ : q r (si.φ r) := begin apply q_sup_of_forall, intros B hB, rw finset.mem_filter at hB, exact hB.2, end end si
b0bf1fcd13815d7cd94baeafddb373fec70ea394
9845f9502cdf644282e3a34b74fa995fc33d6752
/REDUNDANCY[WEB].lean
910a16b660f4e7951fd04200275720c765ff198a
[]
no_license
Robilsu/Theorem-of-Branch-Redundancy
cc430d761dade4bb723c88a2a8709b7e47081171
918803bc047f2340e0f01d470939d077f7acefce
refs/heads/main
1,691,744,940,519
1,632,346,370,000
1,632,346,370,000
365,813,983
0
0
null
null
null
null
UTF-8
Lean
false
false
88,561
lean
/- To run this project, load the contents of this file at: -- https://leanprover-community.github.io/lean-web-editor -- You can either copy-and-paste it or use the load function. -- The Lean web editor will take up to some minutes to load fully and process the code. -- Do not worry! The waiting is normal and happens only once (when the code is loaded). -/ reserve prefix `#` : 9999 -- #_ reserve infix ` >> ` : 1000 -- _ >> _ /- Useful Properties -/ namespace use universes u v /- Proofs from L∃∀N's library regarding negation, conjunction, and disjunction -/ lemma Not_Or {a b : Prop} : ¬ a → ¬ b → ¬ (a ∨ b) | hna hnb (or.inl ha) := absurd ha hna | hna hnb (or.inr hb) := absurd hb hnb lemma Not_Or_iff_And_Not (p q) [d₁ : decidable p] [d₂ : decidable q] : ¬ (p ∨ q) ↔ ¬ p ∧ ¬ q := iff.intro (λ h, match d₁ with | is_true h₁ := false.elim $ h (or.inl h₁) | is_false h₁ := match d₂ with | is_true h₂ := false.elim $ h (or.inr h₂) | is_false h₂ := ⟨h₁, h₂⟩ end end) (λ ⟨np, nq⟩ h, or.elim h np nq) /- Proving the distributive property of "IF-THEN-ELSE" -/ lemma ITE_Distribution {α : Type u}{β : Type v} {IF : Prop}{THEN ELSE : α} [decIF : decidable IF] : ∀(f : α → β), --------------------------- ( f (ite IF THEN ELSE) = ite IF (f THEN) (f ELSE) ) := begin with_cases { by_cases IFs : IF }, case pos { simp [IFs] }, case neg { simp [IFs] } end /- Useful properties regarding Lists -/ lemma Index_LT_Length_of_Mem {α : Type u} [eqα : decidable_eq α] : ∀{a : α}{l : list α}, ( a ∈ l ) → --------------------------- ( list.index_of a l < list.length l ) | a [] := begin assume a_mem, simp [has_mem.mem, list.mem] at a_mem, from false.elim a_mem end | a (HEAD::TAIL) := begin assume a_mem, simp [has_mem.mem, list.mem] at a_mem, simp [list.index_of, list.find_index, list.length], with_cases { by_cases a_eq_head : a = HEAD }, case pos { simp [a_eq_head], have one_le_succ, from nat.add_le_add_left (nat.zero_le (list.length TAIL)) 1, rewrite [nat.add_zero] at one_le_succ, have zero_lt_succ, from nat.lt_of_lt_of_le (nat.zero_lt_one) one_le_succ, rewrite [nat.add_comm] at zero_lt_succ, from nat.lt_of_lt_of_le nat.zero_lt_one zero_lt_succ }, case neg { simp [a_eq_head] at ⊢ a_mem, rewrite [nat.succ_eq_add_one, nat.add_comm], have loop, from Index_LT_Length_of_Mem a_mem, have lt_loop, from nat.add_lt_add_left loop 1, rewrite [nat.add_comm 1 (TAIL.length)] at lt_loop, from lt_loop } end lemma Mem_Remove_of_Mem_of_Not_Mem {α : Type u} [eqα : decidable_eq α] : ∀{nth : α}{l1 l2 : list α}, ( nth ∉ l1 ) → ( nth ∈ l2 ) → --------------------------- ( ∀(a : α), a ∈ l1 → a ∈ l2 → a ∈ list.remove_nth l2 (list.index_of nth l2) ) | nth l1 [] := begin assume nth_not_l1 nth_mem_l2, simp [has_mem.mem, list.mem] at nth_mem_l2, from false.elim nth_mem_l2 end | nth l1 (HEAD2::TAIL2) := begin assume nth_not_l1 nth_mem_l2, simp [list.index_of, list.find_index], with_cases { by_cases nth_eq_head2 : nth = HEAD2 }, case pos { simp [nth_eq_head2] at ⊢ nth_not_l1, simp [list.index_of, list.find_index, list.remove_nth], have case_loop : ∀(a : α), a ∈ l1 → a ∈ (HEAD2::TAIL2) → a ∈ TAIL2, from begin assume loop loop_mem_l1 loop_mem_l2, simp [has_mem.mem, list.mem] at loop_mem_l2, cases loop_mem_l2 with loop_eq_head2 loop_mem_tail2, case or.inl { rewrite [loop_eq_head2] at loop_mem_l1, from absurd loop_mem_l1 nth_not_l1 }, case or.inr { from loop_mem_tail2 } end, from case_loop }, case neg { simp [nth_eq_head2] at ⊢ nth_mem_l2, simp [list.remove_nth], have case_loop : ∀(a : α), a ∈ l1 → a ∈ (HEAD2::TAIL2) → a = HEAD2 ∨ a ∈ list.remove_nth TAIL2 (list.find_index (eq nth) TAIL2), from begin assume loop loop_mem_l1 loop_mem_l2, simp [has_mem.mem, list.mem] at loop_mem_l2, cases loop_mem_l2 with loop_eq_head2 loop_mem_tail2, case or.inl { simp [loop_eq_head2] }, case or.inr { apply (or.intro_right), from Mem_Remove_of_Mem_of_Not_Mem nth_not_l1 nth_mem_l2 loop loop_mem_l1 loop_mem_tail2 } end, from case_loop } end lemma Subset_Self {α : Type u} : ∀(l : list α), --------------------------- ( ∀(a : α), ( a ∈ l ) → ( a ∈ l ) ) | l := begin have subset_self : ∀(a : α), a ∈ l → a ∈ l, from begin assume a a_mem_l, from a_mem_l end, from subset_self end /- Useful properties regarding Natural Numbers (Must be in Order) -/ lemma LT_Add_of_Pos_of_LE : ∀{a b c : ℕ}, ( 0 < a ) → ( b ≤ c ) → --------------------------------- b < a + c | a b c := begin assume a_pos b_le_c, have one_le_a, from nat.succ_le_of_lt a_pos, have b_le_ac, from nat.add_le_add one_le_a b_le_c, rewrite [nat.add_comm] at b_le_ac, from nat.lt_of_succ_le b_le_ac end lemma LT_Add_of_Pos_of_LT : ∀{a b c : ℕ}, ( 0 < a ) → ( b < c ) → --------------------------------- b < a + c | a b c := begin assume a_pos b_lt_c, from LT_Add_of_Pos_of_LE a_pos (nat.le_of_lt b_lt_c) end lemma LT_Add_of_LE_of_Pos : ∀{a b c : ℕ}, ( b ≤ c ) → ( 0 < a ) → --------------------------------- b < c + a | a b c := begin assume b_le_c a_pos, have one_le_a, from nat.succ_le_of_lt a_pos, have b_le_ca, from nat.add_le_add b_le_c one_le_a, from nat.lt_of_succ_le b_le_ca end lemma LT_Add_of_LT_of_Pos : ∀{a b c : ℕ}, ( b < c ) → ( 0 < a ) → --------------------------------- b < c + a | a b c := begin assume b_lt_c a_pos, from LT_Add_of_LE_of_Pos (nat.le_of_lt b_lt_c) a_pos end lemma EQ_of_LT_ONE_of_GE : ∀{a b : ℕ}, ( a ≥ b ) → ( a < 1 + b ) → --------------------------------- ( a = b ) | 0 0 := begin assume a_ge_b a_lt_oneb, from eq.refl 0 end | (a+1) 0 := begin assume a_ge_b a_lt_oneb, rewrite [nat.add_comm] at a_lt_oneb, have zero_lt_a, from nat.lt_of_add_lt_add_left a_lt_oneb, have not_zero_lt_a, from nat.not_lt_zero a, from absurd zero_lt_a not_zero_lt_a end | 0 (b+1) := begin assume a_ge_b a_lt_oneb, have zero_le_b, from nat.zero_le b, have b_le_oneb, from le_trans a_ge_b zero_le_b, rewrite [nat.add_one] at b_le_oneb, from absurd b_le_oneb (nat.not_succ_le_self b) end | (a+1) (b+1) := begin assume a_ge_b a_lt_oneb, have loop_ge : a ≥ b , from nat.le_of_succ_le_succ a_ge_b, rewrite [nat.add_comm] at a_lt_oneb, have loop_lt, from nat.lt_of_add_lt_add_left a_lt_oneb, rewrite [nat.add_comm] at loop_lt, rewrite [EQ_of_LT_ONE_of_GE loop_ge loop_lt] end lemma LE_Mul_Left_of_Pos : ∀{a : ℕ}, ( 0 < a ) → --------------------------------- ( ∀(b : ℕ), b ≤ a * b ) | a := begin assume zero_lt_a b, induction b, case nat.zero { simp [nat.mul_zero] }, case nat.succ { have one_le_a, from nat.succ_le_of_lt zero_lt_a, rewrite [nat.succ_eq_add_one, nat.mul_comm a, nat.add_comm, nat.right_distrib, nat.one_mul, nat.mul_comm], from nat.add_le_add one_le_a b_ih } end /- Useful properties regarding Natural Numbers -/ lemma Add_GE_Add : ∀{a b c d : ℕ}, ( a ≥ b ) → ( c ≥ d ) → --------------------------------- ( a + c ≥ b + d ) | a b c d := begin assume a_ge_b c_ge_d, from nat.add_le_add a_ge_b c_ge_d end lemma Add_Left_EQ_of_EQ : ∀{a b : ℕ}, ( a = b ) → --------------------------------- ( ∀(c : ℕ), c + a = c + b ) | a b := begin assume a_eq_b c, rewrite [a_eq_b] end lemma Add_Right_EQ_of_EQ : ∀{a b : ℕ}, ( a = b ) → --------------------------------- ( ∀(c : ℕ), a + c = b + c ) | a b := begin assume a_eq_b c, rewrite [a_eq_b] end lemma EQ_or_SUCC_LE_iff_LE : ∀{a b : ℕ}, --------------------------- ( a ≤ b ) ↔ ( (a = b) ∨ (a+1 ≤ b) ) := begin have case_to : ∀{a b : ℕ}, a ≤ b → (a = b) ∨ (a+1 ≤ b), from begin assume a b le_a_b, from nat.eq_or_lt_of_le le_a_b end, have case_from : ∀{a b : ℕ}, (a = b) ∨ (a+1 ≤ b) → a ≤ b, from begin assume a b eq_or_succ_le_a_b, cases eq_or_succ_le_a_b with eq_a_b succ_le_a_b, case or.inl { simp [eq_a_b] }, case or.inr { from nat.le_of_succ_le succ_le_a_b } end, assume a b, from iff.intro case_to case_from end lemma EQ_PRED_of_SUCC_EQ : -- Notice that the other way does not hold true, since 0 ≠ 0 - 1 ∀{a b : ℕ}, ( a+1 = b ) → --------------------------- ( a = b-1 ) | a b := begin assume eq_succ_a_b, rewrite [←eq_succ_a_b, eq_comm], from nat.add_sub_cancel a 1 end lemma GE_Cancel_Right : ∀{a b c : ℕ}, ( a ≥ b + c ) → --------------------------------- ( a ≥ b ) | a b c := begin assume a_ge_bc, have bc_ge_b : b + c ≥ b, from begin have b_le_bc, from nat.add_le_add_left (nat.zero_le c) b, rewrite [nat.add_zero] at b_le_bc, from b_le_bc end, from ge_trans a_ge_bc bc_ge_b end lemma GE_iff_Not_LT : ∀{a b : ℕ}, --------------------------- ( a ≥ b ) ↔ ( ¬( a < b ) ) := begin have case_to : ∀{m n : ℕ}, ( m ≥ n ) → ( ¬( m < n ) ), from begin assume m n m_ge_n, from not_lt_of_ge m_ge_n end, have case_from : ∀{m n : ℕ}, ( ¬( m < n ) ) → ( m ≥ n ), from begin assume m n not_m_lt_n, have eq_or_lt, from nat.eq_or_lt_of_not_lt not_m_lt_n, cases eq_or_lt with m_eq_n m_lt_n, case or.inl { apply nat.le_of_eq, from (iff.elim_right eq_comm) m_eq_n }, case or.inr { apply nat.le_of_lt, from m_lt_n }, end, assume a b, from iff.intro case_to case_from end lemma LE_Add_Left : ∀{a b : ℕ}, ( a ≤ b ) → --------------------------------- ( ∀(c : ℕ), ( a ≤ c + b ) ) | a b := begin assume a_le_b c, from le_trans a_le_b (nat.le_add_left b c) end lemma LE_Add_Right : ∀{a b : ℕ}, ( a ≤ b ) → --------------------------------- ( ∀(c : ℕ), ( a ≤ b + c ) ) | a b := begin assume a_le_b c, from le_trans a_le_b (nat.le_add_right b c) end lemma LE_Mul_of_LE : ∀{a b : ℕ}, ∀(c : ℕ), ( a ≤ b ) → --------------------------- ( a * c ≤ b * c ) | a b 0 := begin assume a_le_b, simp [nat.mul_zero] end | a b (c+1) := begin assume a_le_b, rewrite [←nat.mul_comm (c+1), nat.right_distrib, nat.one_mul, nat.mul_comm c], rewrite [←nat.mul_comm (c+1), nat.right_distrib, nat.one_mul, nat.mul_comm c], have loop, from LE_Mul_of_LE c a_le_b, from nat.add_le_add loop a_le_b end lemma LT_Add_Left : ∀{a b : ℕ}, ( a < b ) → --------------------------------- ( ∀(c : ℕ), ( a < c + b ) ) | a b := begin assume a_lt_b c, with_cases {by_cases c_eq_zero : c = 0 }, case pos { simp [c_eq_zero], rewrite [nat.add_comm, nat.add_zero], from a_lt_b }, case neg { rewrite [eq_comm] at c_eq_zero, have c_pos, from lt_of_le_of_ne (nat.zero_le c) (c_eq_zero), from LT_Add_of_Pos_of_LT c_pos a_lt_b } end lemma LT_Add_Right : ∀{a b : ℕ}, ( a < b ) → --------------------------------- ( ∀(c : ℕ), ( a < b + c ) ) | a b := begin assume a_lt_b c, with_cases {by_cases c_eq_zero : c = 0 }, case pos { simp [c_eq_zero], rewrite [nat.add_zero], from a_lt_b }, case neg { rewrite [eq_comm] at c_eq_zero, have c_pos, from lt_of_le_of_ne (nat.zero_le c) (c_eq_zero), from LT_Add_of_LT_of_Pos a_lt_b c_pos } end lemma LT_ZERO_iff_LE_ONE : ∀{a : ℕ}, --------------------------- ( 0 < a ) ↔ ( 1 ≤ a ) := begin have case_to : ∀{a : ℕ}, ( 0 < a ) → ( 1 ≤ a ), from begin assume a zero_lt_a, from nat.le_of_lt_succ (nat.succ_lt_succ zero_lt_a) end, have case_from : ∀{a : ℕ}, ( 1 ≤ a ) → ( 0 < a ), from begin assume a one_le_a, from nat.lt_of_lt_of_le nat.zero_lt_one one_le_a end, assume a, from iff.intro case_to case_from end lemma Not_EQ_of_SUCC_EQ_SUB : ∀{a b c : ℕ}, ( c+1 = a-b ) → --------------------------------- ¬ ( b = a ) | a b c := begin assume succ_eq_sub eq_a_b, rewrite [eq_a_b] at succ_eq_sub, rewrite [nat.sub_self] at succ_eq_sub, simp [nat.add_one_ne_zero] at succ_eq_sub, from succ_eq_sub end lemma Not_SUCC_LE_of_ZERO_EQ_SUB : ∀{a b : ℕ}, ( 0 = a-b ) → --------------------------------- ¬ ( b+1 ≤ a ) | a b := begin assume zero_eq_sub succ_le, rewrite [eq_comm] at zero_eq_sub, have le_a_b, from nat.le_of_sub_eq_zero zero_eq_sub, have le_succ_b_b, from nat.le_trans succ_le le_a_b, simp [nat.not_succ_le_self] at le_succ_b_b, from le_succ_b_b end lemma Sub_Add_Cancel : ∀{a b : ℕ}, ( b ≤ a ) → --------------------------------- ( a - b + b = a ) | a b := begin assume b_le_a, from nat.sub_add_cancel b_le_a end lemma ZERO_LT_Mul_iff_AND_ZERO_LT : ∀{a b : ℕ}, --------------------------------- ( 0 < a * b ) ↔ ( 0 < a ∧ 0 < b ) := begin have case_to : ∀{a b : ℕ}, ( 0 < a * b ) → ( 0 < a ∧ 0 < b ), from begin assume a b zero_lt_ab, with_cases {by_cases a_eq_zero : a = 0 }, case pos { rewrite [a_eq_zero, nat.zero_mul] at zero_lt_ab, from false.elim (nat.not_lt_zero 0 zero_lt_ab) }, case neg { with_cases {by_cases b_eq_zero : b = 0 }, case pos { rewrite [b_eq_zero, nat.mul_zero] at zero_lt_ab, from false.elim (nat.not_lt_zero 0 zero_lt_ab) }, case neg { have zero_le_a, from nat.lt_of_le_and_ne (nat.zero_le a), have zero_le_b, from nat.lt_of_le_and_ne (nat.zero_le b), rewrite [ne.def, eq_comm] at zero_le_a zero_le_b, from and.intro (zero_le_a a_eq_zero) (zero_le_b b_eq_zero) }, } end, have case_from : ∀{a b : ℕ}, ( 0 < a ∧ 0 < b ) → ( 0 < a * b ), from begin assume a b zero_lt_a_and_b, cases zero_lt_a_and_b with zero_lt_a zero_lt_b, have zero_lt_ab, from nat.mul_lt_mul_of_pos_left zero_lt_b zero_lt_a, rewrite [nat.mul_zero] at zero_lt_ab, from zero_lt_ab end, assume a b, from iff.intro case_to case_from end lemma ZERO_LT_of_Not_EQ_ZERO : ∀{a : ℕ}, ¬ ( a = 0 ) → --------------------------------- ( 0 < a ) | 0 := begin assume not_refl_zero, from absurd (eq.refl 0) not_refl_zero end | (a+1) := begin assume not_succ_eq_zero, rewrite [nat.add_comm], from nat.zero_lt_one_add a end end use /- Auxiliary Definitions -/ namespace aux universes u /- Methods for determining the size of nested lists -/ -- The order of the following definitions is important class has_size (α : Type u) := (size : α → ℕ) instance is_default_size (α : Type u) : has_size α := ⟨ (λx, 1) ⟩ -- First the size method checks if it has been given a list, if not it goes to its default case def size {α : Type u} [hcα : has_size α] : α → ℕ := has_size.size def list_size {α : Type u} [hcα : has_size α] : list α → ℕ | [] := 0 | (h::l) := size h + list_size l -- Writing [hcα : has_size α] means that α ay have either is_list_size or is_default_size -- If [hcα : has_size α] is omitted, than α must have is_default_size instance is_list_size (α : Type u) [hcα : has_size α] : has_size (list α) := ⟨list_size⟩ /- Properties about list size -/ lemma Deafault_Size_EQ_Length {α : Type} : ∀{l : list α}, --------------------------- ( list_size l = list.length l ) | [] := by simp [list.length, list_size] | (HEAD::TAIL) := begin simp [list.length, list_size], simp [size, has_size.size], rewrite [Deafault_Size_EQ_Length], from nat.add_comm 1 (list.length TAIL) end lemma ZERO_LT_Length_of_LT_Size {α : Type} [hcα : has_size α] : ∀{l : list α}{num : ℕ}, ( num < list_size l ) → --------------------------- ( 0 < list.length l ) | [] num := begin assume num_lt_size, simp [list_size] at num_lt_size, from absurd num_lt_size (nat.not_lt_zero num) end | (HEAD::TAIL) num := begin assume num_lt_size, simp [list.length, nat.add_comm], from nat.zero_lt_one_add (list.length TAIL) end end aux /- Auxiliary Definitions (Leaf) -/ namespace a_leaf universes u /- Combinatory theorem about nested lists -/ theorem Main_Combinatory_Nested {α : Type u} [hsα : aux.has_size α] : ∀{num : ℕ}{ll : list (list α)}, ( (list.length ll) * num < aux.list_size ll ) → --------------------------------- ( ∃(l : list α), l ∈ ll ∧ num < aux.list_size l ) | num [] := begin assume mul_lt_ll, simp [aux.list_size] at mul_lt_ll, rewrite [nat.mul_comm, nat.mul_zero] at mul_lt_ll, from false.elim (nat.not_lt_zero 0 mul_lt_ll) end | num (HEAD::TAIL) := begin assume mul_lt_ll, simp [aux.list_size] at mul_lt_ll, with_cases { by_cases num_lt_head : num < aux.size HEAD }, case pos { --If HEAD is big apply exists.intro HEAD, simp [has_mem.mem, list.mem], from num_lt_head }, case neg { --If HEAD isn't big with_cases { by_cases mul_lt_tail : (list.length TAIL) * num < aux.list_size TAIL }, case pos { --If TAIL is big have main_combinatory, from Main_Combinatory_Nested mul_lt_tail, cases main_combinatory with la main_combinatory, cases main_combinatory with mem_la_tail num_lt_la, apply exists.intro la, simp [has_mem.mem, list.mem] at ⊢ mem_la_tail, simp [mem_la_tail], from num_lt_la }, case neg { --If TAIL isn't big rewrite [nat.right_distrib (list.length TAIL) 1 num, nat.one_mul] at mul_lt_ll, rewrite [←use.GE_iff_Not_LT] at num_lt_head, rewrite [←use.GE_iff_Not_LT] at mul_lt_tail, have not_mul_lt_ll, from use.Add_GE_Add mul_lt_tail num_lt_head, rewrite [use.GE_iff_Not_LT, nat.add_comm (aux.list_size TAIL)] at not_mul_lt_ll, from absurd mul_lt_ll not_mul_lt_ll } } end end a_leaf /- Auxiliary Definitions (Branch) -/ namespace a_branch universes u /- Methods for counting the number diferent elements in a list -/ def list_set {α : Type u} [eqα : decidable_eq α] : list α → list α | [] := [] | (HEAD::TAIL) := if HEAD ∈ TAIL then list_set TAIL else HEAD :: list_set TAIL def list_filter {α : Type u} [eqα : decidable_eq α] : α → list α → list α | _ [] := [] | a (HEAD::TAIL) := if a = HEAD then HEAD :: list_filter a TAIL else list_filter a TAIL def list_delete {α : Type u} [eqα : decidable_eq α] : α → list α → list α | _ [] := [] | a (HEAD::TAIL) := if a = HEAD then list_delete a TAIL else HEAD :: list_delete a TAIL /- Properties about list set, filter, and delete (Used outside this namespace) -/ lemma Length_Set_LE_Length_of_Subset {α : Type u} [eqα : decidable_eq α] : ∀{l1 l2 : list α}, ( ∀(a : α), ( a ∈ l1 ) → ( a ∈ l2 ) ) → --------------------------- ( list.length (list_set l1) ≤ list.length l2 ) | [] l2 := begin assume case_mem, simp [list_set], from nat.zero_le (list.length l2) end | (HEAD1::TAIL1) l2 := begin assume case_mem, have head1_mem_l2 : HEAD1 ∈ l2, from begin apply (case_mem HEAD1), simp [has_mem.mem, list.mem] end, have case_mem_tail1 : ∀ (a : α), a ∈ TAIL1 → a ∈ l2, from begin assume loop loop_mem_tail1, simp [has_mem.mem, list.mem] at case_mem, apply (case_mem loop), simp [has_mem.mem, list.mem] at loop_mem_tail1, simp [loop_mem_tail1] end, simp [list_set], with_cases { by_cases head1_mem_tail1 : HEAD1 ∈ TAIL1 }, -- case pos { simp [head1_mem_tail1], from Length_Set_LE_Length_of_Subset case_mem_tail1 }, case neg { simp [head1_mem_tail1], have case_loop : ∀ (a : α), a ∈ TAIL1 → a ∈ list.remove_nth l2 (list.index_of HEAD1 l2), from begin assume loop loop_mem_tail1, have loop_mem_l2, from case_mem_tail1 loop loop_mem_tail1, have case_remove, from use.Mem_Remove_of_Mem_of_Not_Mem head1_mem_tail1 head1_mem_l2, from case_remove loop loop_mem_tail1 loop_mem_l2 end, have loop, from Length_Set_LE_Length_of_Subset case_loop, have case_index, from use.Index_LT_Length_of_Mem head1_mem_l2, rewrite [list.length_remove_nth l2 (list.index_of HEAD1 l2) case_index] at loop, have one_le_l2, from nat.lt_of_le_of_lt (nat.zero_le (list.index_of HEAD1 l2)) case_index, rewrite [use.LT_ZERO_iff_LE_ONE] at one_le_l2, rewrite [←use.Sub_Add_Cancel one_le_l2], from nat.add_le_add loop (nat.le_refl 1) } end /- Properties about list set, filter, and delete (Used only within this namespace) -/ lemma Mem_of_Mem_Delete {α : Type u} [eqα : decidable_eq α] : ∀{a1 a2 : α}{l : list α}, ( a1 ∈ (list_delete a2 l) ) → --------------------------- ( a1 ∈ l ) | a1 a2 [] := begin assume a1_mem_del, simp [list_delete, has_mem.mem, list.mem] at a1_mem_del, from false.elim a1_mem_del end | a1 a2 (HEAD::TAIL) := begin assume a1_mem_del, simp [list_delete, has_mem.mem, list.mem] at ⊢ a1_mem_del, with_cases { by_cases a2_eq_head : a2 = HEAD }, case pos { -- If HEAD = a2 simp [a2_eq_head] at a1_mem_del, have loop, from Mem_of_Mem_Delete a1_mem_del, simp [has_mem.mem, list.mem] at ⊢ loop, simp [loop] }, case neg { -- If HEAD ≠ a2 simp [a2_eq_head, list.mem] at a1_mem_del, cases a1_mem_del with a1_eq_head a1_mem_del, case or.inl { simp [a1_eq_head] }, case or.inr { have loop, from Mem_of_Mem_Delete a1_mem_del, simp [has_mem.mem, list.mem] at ⊢ loop, simp [loop] } } end lemma Size_Filter_Delete {α : Type u} [eqα : decidable_eq α] : ∀(a : α)(l : list α), --------------------------- ( aux.list_size (list_filter a (list_delete a l)) = 0 ) | a [] := by simp [list_delete, list_filter, aux.list_size] | a (HEAD::TAIL) := begin with_cases { by_cases a_eq_head : a = HEAD }, case pos { -- If a = HEAD simp [a_eq_head, list_delete], from Size_Filter_Delete HEAD TAIL }, case neg { -- If a ≠ HEAD simp [a_eq_head, list_delete, list_filter], from Size_Filter_Delete a TAIL } end lemma Size_Delete_LE_Size {α : Type u} [eqα : decidable_eq α] : ∀(a1 a2 : α)(l : list α), --------------------------- ( aux.list_size (list_filter a1 (list_delete a2 l)) ≤ aux.list_size (list_filter a1 l) ) | a1 a2 [] := by simp [list_delete, list_filter] | a1 a2 (HEAD::TAIL) := begin simp [list_delete], with_cases { by_cases a2_eq_head : a2 = HEAD }, case pos { -- If a2 = HEAD simp [a2_eq_head, list_filter], with_cases { by_cases a1_eq_head : a1 = HEAD }, case pos { -- If a1 = HEAD simp [a1_eq_head, aux.list_size], from use.LE_Add_Left (Size_Delete_LE_Size HEAD HEAD TAIL) (aux.size HEAD) }, case neg { -- If a1 ≠ HEAD simp [a1_eq_head, aux.list_size], from Size_Delete_LE_Size a1 HEAD TAIL } }, case neg { -- If a2 ≠ HEAD simp [a2_eq_head, list_filter], with_cases { by_cases a1_eq_head : a1 = HEAD }, case pos { -- If a1 = HEAD simp [a1_eq_head, aux.list_size], from nat.add_le_add (nat.le_refl (aux.size HEAD)) (Size_Delete_LE_Size HEAD a2 TAIL) }, case neg { -- If a1 ≠ HEAD simp [a1_eq_head, aux.list_size], from Size_Delete_LE_Size a1 a2 TAIL } } end lemma Size_Filter_Delete_EQ_Size {α : Type u} [eqα : decidable_eq α] : ∀(a : α)(l : list α), --------------------------- ( aux.list_size (list_filter a l) + aux.list_size (list_delete a l) = aux.list_size l ) | a [] := by simp [list_delete, list_filter, aux.list_size] | a (HEAD::TAIL) := begin simp [list_filter, list_delete], with_cases { by_cases a_eq_head : a = HEAD }, case pos { -- If a = HEAD simp [a_eq_head, aux.list_size], rewrite [nat.add_assoc], have loop, from Size_Filter_Delete_EQ_Size HEAD TAIL, from use.Add_Left_EQ_of_EQ loop (aux.size HEAD) }, case neg { -- If a ≠ HEAD simp [a_eq_head, aux.list_size], rewrite [←nat.add_assoc], rewrite [nat.add_comm (aux.list_size (list_filter a TAIL)) (aux.size HEAD)], rewrite [nat.add_assoc], have loop, from Size_Filter_Delete_EQ_Size a TAIL, from use.Add_Left_EQ_of_EQ loop (aux.size HEAD) } end /- Properties about list set, filter, and delete (Must be left in order) -/ lemma Case_Not_Mem_Del {α : Type u} [eqα : decidable_eq α] : ∀{a1 a2 : α}{l : list α}, ( a2 ≠ a1 ) → ( a1 ∉ l ) → --------------------------- ( a1 ∉ list_delete a2 l ) | a1 a2 [] := by simp [list_delete] | a1 a2 (HEAD::TAIL) := begin assume a1_ne_a2 a1_not_mem, simp [has_mem.mem, list.mem] at a1_not_mem, rewrite [use.Not_Or_iff_And_Not] at a1_not_mem, cases a1_not_mem with a1_ne_head a1_not_mem_tail, -- with_cases { by_cases a2_eq_head : a2 = HEAD }, case pos { -- If a2 = HEAD simp [a2_eq_head, list_delete] at ⊢ a1_ne_a2, rewrite [eq_comm] at a1_ne_head, from Case_Not_Mem_Del a1_ne_head a1_not_mem_tail }, case neg { -- If a2 ≠ HEAD simp [a2_eq_head, list_delete], from use.Not_Or a1_ne_head (Case_Not_Mem_Del a1_ne_a2 a1_not_mem_tail) } end lemma Case_Mem_Del {α : Type u} [eqα : decidable_eq α] : ∀{a1 a2 : α}{l : list α}, ( a2 ≠ a1 ) → ( a1 ∈ l ) → --------------------------- ( a1 ∈ list_delete a2 l ) | a1 a2 [] := begin assume a1_ne_a2 a1_mem, simp [list_delete] at a1_mem, from false.elim a1_mem end | a1 a2 (HEAD::TAIL) := begin assume a1_ne_a2 a1_mem, simp [has_mem.mem, list.mem] at a1_mem, -- with_cases { by_cases a2_eq_head : a2 = HEAD }, case pos { -- If a2 = HEAD simp [a2_eq_head, list_delete] at ⊢ a1_ne_a2, cases a1_mem with a1_eq_head a1_mem_tail, case or.inl { rewrite [eq_comm] at a1_eq_head, from absurd a1_eq_head a1_ne_a2 }, case or.inr { from Case_Mem_Del a1_ne_a2 a1_mem_tail } }, case neg { -- If a2 ≠ HEAD simp [a2_eq_head, list_delete], cases a1_mem with a1_eq_head a1_mem_tail, case or.inl { simp [a1_eq_head] }, case or.inr { simp [Case_Mem_Del a1_ne_a2 a1_mem_tail] } } end lemma Lenght_EQ_Lenght_Set_Delete {α : Type u} [eqα : decidable_eq α] : ∀(a : α)(l : list α), --------------------------- ( list.length (list_set (a::l)) = 1 + list.length (list_set (list_delete a l)) ) | a [] := by simp [list_delete, list_set] | a (HEAD::TAIL) := begin have loop_head, from Lenght_EQ_Lenght_Set_Delete HEAD TAIL, have loop_a, from Lenght_EQ_Lenght_Set_Delete a TAIL, simp [list_delete] at ⊢ loop_head loop_a, with_cases { by_cases a_eq_head : a = HEAD }, case pos { -- If a = HEAD simp [a_eq_head, list_set] at ⊢ loop_head, with_cases { by_cases head_mem_tail : HEAD ∈ TAIL }, case pos { -- If HEAD ∈ TAIL simp [head_mem_tail, list.length] at ⊢ loop_head, from loop_head }, case neg { -- If HEAD ∉ TAIL simp [head_mem_tail, list.length] at ⊢ loop_head, from loop_head } }, case neg { -- If a ≠ HEAD simp [a_eq_head, list_set] at ⊢ loop_a, with_cases { by_cases a_mem_tail : a ∈ TAIL }, case pos { -- If a ∈ TAIL simp [a_mem_tail, list.length] at ⊢ loop_a, with_cases { by_cases head_mem_tail : HEAD ∈ TAIL }, case pos { -- If HEAD ∈ TAIL simp [head_mem_tail, list.length] at ⊢ loop_a, simp [Case_Mem_Del a_eq_head head_mem_tail], from loop_a }, case neg { -- If HEAD ∉ TAIL simp [head_mem_tail, list.length] at ⊢ loop_a, simp [Case_Not_Mem_Del a_eq_head head_mem_tail], rewrite [nat.add_comm (list.length (list_set TAIL)) 1], rewrite [nat.add_comm] at loop_a, from use.Add_Left_EQ_of_EQ loop_a 1 } }, case neg { -- If a ∉ TAIL simp [a_mem_tail, list.length] at ⊢ loop_a, with_cases { by_cases head_mem_tail : HEAD ∈ TAIL }, case pos { -- If HEAD ∈ TAIL simp [head_mem_tail, list.length] at ⊢ loop_a, simp [Case_Mem_Del a_eq_head head_mem_tail], from loop_a }, case neg { -- If HEAD ∉ TAIL simp [head_mem_tail, list.length] at ⊢ loop_a, simp [Case_Not_Mem_Del a_eq_head head_mem_tail], rewrite [nat.add_comm 1 (list.length (list_set (list_delete a TAIL)))] at loop_a, rewrite [nat.add_comm (list.length (list_set TAIL) + 1) 1], from use.Add_Left_EQ_of_EQ loop_a 1 } } } end /- Combinatory theorem about lists (Filter) -/ theorem Main_Combinatory_Filter {α : Type} [eqα : decidable_eq α] : ∀(num : ℕ)(l : list α), ( list.length (list_set l) * num < aux.list_size l ) → --------------------------------- ( ∃(a : α), a ∈ l ∧ num < aux.list_size (list_filter a l) ) | num [] := begin assume mul_lt_l, simp [aux.list_size] at mul_lt_l, from false.elim (nat.not_lt_zero (list.length (list_set []) * num) mul_lt_l) end | num (HEAD::TAIL) := begin assume mul_lt_l, with_cases { by_cases num_lt_head : num < 1 + aux.list_size (list_filter HEAD TAIL) }, case pos { -- If list_filter HEAD (HEAD::TAIL) is big apply exists.intro HEAD, simp [list_filter, aux.list_size, has_mem.mem, list.mem, aux.size, aux.has_size.size], from num_lt_head }, case neg { -- If list_filter HEAD (HEAD::TAIL) isn't big with_cases { by_cases mul_lt_del : list.length (list_set (list_delete HEAD TAIL)) * num < aux.list_size (list_delete HEAD TAIL) }, case pos { -- If list_delete HEAD (HEAD::TAIL) is big have main_combinatory : ∃(a : α), a ∈ list_delete HEAD TAIL ∧ num < aux.list_size (list_filter a (list_delete HEAD TAIL)), from Main_Combinatory_Filter num (list_delete HEAD TAIL) mul_lt_del, -- sorry, cases main_combinatory with a main_combinatory, cases main_combinatory with a_mem_del num_lt_a, apply exists.intro a, simp [has_mem.mem, list.mem] at ⊢ a_mem_del, simp [a_mem_del, list_filter], with_cases { by_cases a_eq_head : a = HEAD }, case pos { -- If a = HEAD rewrite [a_eq_head] at num_lt_a a_mem_del, rewrite [Size_Filter_Delete HEAD TAIL] at num_lt_a, from absurd num_lt_a (nat.not_lt_zero num) }, case neg { -- If a ≠ HEAD simp [a_eq_head], have a_mem_tail, from Mem_of_Mem_Delete a_mem_del, simp [has_mem.mem] at a_mem_tail, simp [a_mem_tail], from nat.lt_of_lt_of_le num_lt_a (Size_Delete_LE_Size a HEAD TAIL) } }, case neg { --If list_delete HEAD (HEAD::TAIL) isn't big rewrite [Lenght_EQ_Lenght_Set_Delete, nat.right_distrib, nat.one_mul] at mul_lt_l, simp [list_delete, aux.list_size, aux.size, aux.has_size.size] at mul_lt_l, rewrite [←use.GE_iff_Not_LT] at mul_lt_del, rewrite [←use.GE_iff_Not_LT] at num_lt_head, have mul_ge_l, from use.Add_GE_Add num_lt_head mul_lt_del, rewrite [nat.add_assoc, Size_Filter_Delete_EQ_Size] at mul_ge_l, rewrite [use.GE_iff_Not_LT] at mul_ge_l, -- This proof is the same as Main_Combinatory_Nested from absurd mul_lt_l mul_ge_l } } end using_well_founded -- So that the recursion is explicitly done over the list-type { rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ t, list.sizeof t.snd)⟩] } -- L∃∀N gives us a warning that the recursion at Main_Combinatory_Filter is not a well founded relation -- @has_well_founded.r (Σ' (num : ℕ), list α) -- (@has_well_founded.mk (Σ' (num : ℕ), list α) -- (@measure (Σ' (num : ℕ), list α) -- (λ (t : Σ' (num : ℕ), list α), @list.sizeof α (default_has_sizeof α) t.snd)) -- _) -- The nested exception message contains the failure state for the decreasing tactic: -- ⊢ (list_delete HEAD TAIL).sizeof < 1 + 0 + TAIL.sizeof -- However, we show this target is true for all type-instances -- Proving that, despite the warning, the recursion used in Main_Combinatory_Filter is well-founded lemma Well_Founded_Delete {α : Type u} [eqα : decidable_eq α] : ∀(head : α)(tail : list α), --------------------------------- ( list.sizeof (list_delete head tail) < 1 + 0 + list.sizeof tail ) | head [] := begin rewrite [nat.add_zero], simp [list_delete], simp [list.sizeof], have del_lt_sizeof, from nat.add_lt_add_left nat.zero_lt_one 1, rewrite [nat.add_zero] at del_lt_sizeof, from del_lt_sizeof end | head (HEAD::TAIL) := begin rewrite [nat.add_zero], simp [list_delete], with_cases { by_cases eq_head : head = HEAD }, case pos { -- If head = HEAD simp [eq_head], simp [list.sizeof], have loop_0, from Well_Founded_Delete HEAD TAIL, have loop_1, from use.LT_Add_Right loop_0 (sizeof HEAD), rewrite [nat.add_assoc] at loop_1, from use.LT_Add_Left loop_1 1 }, case neg { -- If head ≠ HEAD simp [eq_head], simp [list.sizeof], have loop_0, from Well_Founded_Delete head TAIL, have loop_1, from nat.add_lt_add_left loop_0 (sizeof HEAD), rewrite [←nat.add_assoc] at loop_1, rewrite [nat.add_comm (sizeof HEAD) 1] at loop_1, have loop_2, from nat.add_lt_add_left loop_1 1, rewrite [←nat.add_assoc] at loop_2, from loop_2 } end end a_branch /- Type Definitions and Properties -/ namespace types /- Inductive Types: Formula -/ inductive formula | atom (SBL : ℕ) : formula | implication (ANT CON : formula) : formula export formula (atom implication) notation #SBL := formula.atom SBL notation ANT >> CON := formula.implication ANT CON /- Inductive Types: Proof-Graph -/ inductive node_option | leaf : node_option | node : node_option export node_option (leaf node) inductive tree -- Top Formulas and Hypotheses | leaf (LVL : ℕ) (FML : formula) : tree -- Nodes resulting from →Introduction | u_tree (LVL : ℕ) (FML : formula) (UNI : tree) : tree -- Nodes resulting from →Elimination | lr_tree (LVL : ℕ) (FML : formula) (MNR MJR : tree) : tree export tree (leaf u_tree lr_tree) inductive branch -- The First Node of the Branch (Always a leaf when a Proof-Branch) | leaf (BRC : branch) (OPT : node_option) (LVL : ℕ) (FML : formula) : branch -- Nodes resulting from →Introduction | u_branch (BRC : branch) (OPT : node_option) (LVL : ℕ) (FML : formula) : branch -- Nodes resulting from →Elimination, connected to their Minor Premise | l_branch (BRC : branch) (OPT : node_option) (LVL : ℕ) (FML : formula) : branch -- Nodes resulting from →Elimination, connected to their Major Premise | r_branch (BRC : branch) (OPT : node_option) (LVL : ℕ) (FML : formula) : branch -- Pointer to the Ending of the Branch (Always the Root of the Proof-Tree before Pruning) | root : branch export branch (leaf u_branch l_branch r_branch root) /- Instances of Decidability (Equality): Formula -/ instance formula.decidable_eq : ∀(f1 f2 : formula), --------------------------------- decidable (f1 = f2) -- ATOM: | (atom SBL1) (atom SBL2) := begin simp [eq.decidable], from @nat.decidable_eq SBL1 SBL2 end | (atom _) (implication _ _) := begin simp [eq.decidable], from is_false not_false end -- IMPLICATION: | (implication _ _) (atom _) := begin simp [eq.decidable], from is_false not_false end | (implication ANT1 CON1) (implication ANT2 CON2) := begin simp [eq.decidable], from @and.decidable (ANT1 = ANT2) (CON1 = CON2) (@formula.decidable_eq ANT1 ANT2) (@formula.decidable_eq CON1 CON2) end /- Instances of Decidability (Equality): Proof-Graph -/ instance node_option.decidable_eq : ∀(opt1 opt2 : node_option), --------------------------------- decidable (opt1 = opt2) -- LEAF: | leaf leaf := begin simp [eq.decidable], from is_true trivial end | leaf node := begin simp [eq.decidable], from is_false not_false end -- NODE: | node leaf := begin simp [eq.decidable], from is_false not_false end | node node := begin simp [eq.decidable], from is_true trivial end instance tree.decidable_eq : ∀(t1 t2 : tree), --------------------------------- decidable (t1 = t2) -- LEAF: | (leaf LVL1 FML1) (leaf LVL2 FML2) := begin simp [eq.decidable], from @and.decidable (LVL1 = LVL2) (FML1 = FML2) (@nat.decidable_eq LVL1 LVL2) (@formula.decidable_eq FML1 FML2) end | (leaf _ _) (u_tree _ _ _) := begin simp [eq.decidable], from is_false not_false end | (leaf _ _) (lr_tree _ _ _ _) := begin simp [eq.decidable], from is_false not_false end -- U_Tree: | (u_tree _ _ UNI1) (leaf _ _) := begin simp [eq.decidable], from is_false not_false end | (u_tree LVL1 FML1 UNI1) (u_tree LVL2 FML2 UNI2) := begin simp [eq.decidable], have dec_and1, from @and.decidable (FML1 = FML2) (UNI1 = UNI2) (@formula.decidable_eq FML1 FML2) (@tree.decidable_eq UNI1 UNI2), from @and.decidable (LVL1 = LVL2) (FML1 = FML2 ∧ UNI1 = UNI2) (@nat.decidable_eq LVL1 LVL2) dec_and1 end | (u_tree _ _ _) (lr_tree _ _ _ _) := begin simp [eq.decidable], from is_false not_false end -- LR_Tree: | (lr_tree _ _ MNR1 MJR1) (leaf _ _) := begin simp [eq.decidable], from is_false not_false end | (lr_tree _ _ MNR1 MJR1) (u_tree _ _ _) := begin simp [eq.decidable], from is_false not_false end | (lr_tree LVL1 FML1 MNR1 MJR1) (lr_tree LVL2 FML2 MNR2 MJR2) := begin simp [eq.decidable], have dec_and1, from @and.decidable (MNR1 = MNR2) (MJR1 = MJR2) (@tree.decidable_eq MNR1 MNR2) (@tree.decidable_eq MJR1 MJR2), have dec_and2, from @and.decidable (FML1 = FML2) (MNR1 = MNR2 ∧ MJR1 = MJR2) (@formula.decidable_eq FML1 FML2) dec_and1, from @and.decidable (LVL1 = LVL2) (FML1 = FML2 ∧ MNR1 = MNR2 ∧ MJR1 = MJR2) (@nat.decidable_eq LVL1 LVL2) dec_and2 end instance branch.decidable_eq : ∀(p1 p2 : branch), --------------------------------- decidable (p1 = p2) -- LEAF: | (leaf BRC1 OPT1 LVL1 FML1) (leaf BRC2 OPT2 LVL2 FML2) := begin simp [eq.decidable], have dec_and1, from @and.decidable (LVL1 = LVL2) (FML1 = FML2) (@nat.decidable_eq LVL1 LVL2) (@formula.decidable_eq FML1 FML2), have dec_and2, from @and.decidable (OPT1 = OPT2) (LVL1 = LVL2 ∧ FML1 = FML2) (@node_option.decidable_eq OPT1 OPT2) dec_and1, from @and.decidable (BRC1 = BRC2) (OPT1 = OPT2 ∧ LVL1 = LVL2 ∧ FML1 = FML2) (@branch.decidable_eq BRC1 BRC2) dec_and2 end | (leaf _ _ _ _) (u_branch _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | (leaf _ _ _ _) (l_branch _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | (leaf _ _ _ _) (r_branch _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | (leaf _ _ _ _) root := begin simp [eq.decidable], from is_false not_false end -- U_Path: | (u_branch _ _ _ _) (leaf _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | (u_branch BRC1 OPT1 LVL1 FML1) (u_branch BRC2 OPT2 LVL2 FML2) := begin simp [eq.decidable], have dec_and1, from @and.decidable (LVL1 = LVL2) (FML1 = FML2) (@nat.decidable_eq LVL1 LVL2) (@formula.decidable_eq FML1 FML2), have dec_and2, from @and.decidable (OPT1 = OPT2) (LVL1 = LVL2 ∧ FML1 = FML2) (@node_option.decidable_eq OPT1 OPT2) dec_and1, from @and.decidable (BRC1 = BRC2) (OPT1 = OPT2 ∧ LVL1 = LVL2 ∧ FML1 = FML2) (@branch.decidable_eq BRC1 BRC2) dec_and2 end | (u_branch _ _ _ _) (l_branch _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | (u_branch _ _ _ _) (r_branch _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | (u_branch _ _ _ _) root := begin simp [eq.decidable], from is_false not_false end -- L_Path: | (l_branch _ _ _ _) (leaf _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | (l_branch _ _ _ _) (u_branch _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | (l_branch BRC1 OPT1 LVL1 FML1) (l_branch BRC2 OPT2 LVL2 FML2) := begin simp [eq.decidable], have dec_and1, from @and.decidable (LVL1 = LVL2) (FML1 = FML2) (@nat.decidable_eq LVL1 LVL2) (@formula.decidable_eq FML1 FML2), have dec_and2, from @and.decidable (OPT1 = OPT2) (LVL1 = LVL2 ∧ FML1 = FML2) (@node_option.decidable_eq OPT1 OPT2) dec_and1, from @and.decidable (BRC1 = BRC2) (OPT1 = OPT2 ∧ LVL1 = LVL2 ∧ FML1 = FML2) (@branch.decidable_eq BRC1 BRC2) dec_and2 end | (l_branch _ _ _ _) (r_branch _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | (l_branch _ _ _ _) root := begin simp [eq.decidable], from is_false not_false end -- R_Path: | (r_branch _ _ _ _) (leaf _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | (r_branch _ _ _ _) (u_branch _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | (r_branch _ _ _ _) (l_branch _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | (r_branch BRC1 OPT1 LVL1 FML1) (r_branch BRC2 OPT2 LVL2 FML2) := begin simp [eq.decidable], have dec_and1, from @and.decidable (LVL1 = LVL2) (FML1 = FML2) (@nat.decidable_eq LVL1 LVL2) (@formula.decidable_eq FML1 FML2), have dec_and2, from @and.decidable (OPT1 = OPT2) (LVL1 = LVL2 ∧ FML1 = FML2) (@node_option.decidable_eq OPT1 OPT2) dec_and1, from @and.decidable (BRC1 = BRC2) (OPT1 = OPT2 ∧ LVL1 = LVL2 ∧ FML1 = FML2) (@branch.decidable_eq BRC1 BRC2) dec_and2 end | (r_branch _ _ _ _) root := begin simp [eq.decidable], from is_false not_false end -- ROOT: | root (leaf _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | root (u_branch _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | root (l_branch _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | root (r_branch _ _ _ _) := begin simp [eq.decidable], from is_false not_false end | root root := begin simp [eq.decidable], from is_true trivial end /- Get Methods: Formula -/ def is_atomic : formula → Prop | (atom _) := true | (implication _ _) := false instance formula.is_atomic : ∀(f : formula), --------------------------------- decidable (is_atomic f) | (atom _) := begin simp [is_atomic], from is_true trivial end | (implication _ _) := begin simp [is_atomic], from is_false not_false end def not_atomic : formula → Prop | (atom _) := false | (implication _ _) := true instance formula.not_atomic : ∀(f : formula), --------------------------------- decidable (not_atomic f) | (atom _) := begin simp [not_atomic], from is_false not_false end | (implication _ _) := begin simp [not_atomic], from is_true trivial end def get_antecedent : formula → formula | (atom SBL) := #0 -- NULL | (implication ANT _) := ANT def get_consequent : formula → formula | (atom SBL) := #0 -- NULL | (implication _ CON) := CON /- Get Methods: Proof-Graph -/ def not_root : branch → Prop | (leaf _ _ _ _) := true | (u_branch _ _ _ _) := true | (l_branch _ _ _ _) := true | (r_branch _ _ _ _) := true | root := false instance branch.not_root : ∀(b : branch), --------------------------------- decidable (not_root b) | (leaf _ _ _ _) := begin simp [not_root], from is_true trivial end | (u_branch _ _ _ _) := begin simp [not_root], from is_true trivial end | (l_branch _ _ _ _) := begin simp [not_root], from is_true trivial end | (r_branch _ _ _ _) := begin simp [not_root], from is_true trivial end | root := begin simp [not_root], from is_false not_false end def get_option : branch → node_option | (leaf _ OPT _ _) := OPT | (u_branch _ OPT _ _) := OPT | (l_branch _ OPT _ _) := OPT | (r_branch _ OPT _ _) := OPT | root := node -- NULL def get_level : branch → ℕ | (leaf _ _ LVL _) := LVL | (u_branch _ _ LVL _) := LVL | (l_branch _ _ LVL _) := LVL | (r_branch _ _ LVL _) := LVL | root := 0 -- NULL def get_formula : branch → formula | (leaf _ _ _ FML) := FML | (u_branch _ _ _ FML) := FML | (l_branch _ _ _ FML) := FML | (r_branch _ _ _ FML) := FML | root := #0 -- NULL /- Numbber of Levels and Formulas -/ def list_levels : tree → list (ℕ) | (leaf LVL _) := [LVL] | (u_tree LVL _ UNI) := if LVL ∈ list_levels UNI then list_levels UNI else [LVL] ++ list_levels UNI | (lr_tree LVL _ MNR MJR) := if LVL ∈ (list_levels MNR ∩ list_levels MJR) then (list_levels MNR ∩ list_levels MJR) else [LVL] ++ (list_levels MNR ∩ list_levels MJR) def list_formulas : tree → list (formula) | (leaf _ FML) := [FML] | (u_tree _ FML UNI) := if FML ∈ list_formulas UNI then list_formulas UNI else [FML] ++ list_formulas UNI | (lr_tree _ FML MNR MJR) := if FML ∈ (list_formulas MNR ∩ list_formulas MJR) then (list_formulas MNR ∩ list_formulas MJR) else [FML] ++ (list_formulas MNR ∩ list_formulas MJR) def count_levels : tree → ℕ | t := list.length (list_levels t) def count_formulas : tree → ℕ | t := list.length (list_formulas t) /- For this proof, trees are viewed as lists of nodes(which are also trees) -/ -- The lists are arranged in an orderly fashion, splitting the tree by types of node, levels, and formulas -- The lists return a branch from the original tree's root to a node given by the by type/level/formula def loop_tree_option_level_formula : branch → tree → node_option → ℕ → formula → list (branch) -- Matched node_option with the type of tree constructor | BRC (leaf LVL FML) leaf lvl fml := if LVL=lvl ∧ FML=fml then [leaf BRC leaf LVL FML] else [] | BRC (u_tree LVL FML UNI) node lvl fml := if LVL=lvl ∧ FML=fml then [leaf BRC node LVL FML] else loop_tree_option_level_formula (u_branch BRC node LVL FML) UNI node lvl fml | BRC (lr_tree LVL FML MNR MJR) node lvl fml := if LVL=lvl ∧ FML=fml then [leaf BRC node LVL FML] else loop_tree_option_level_formula (l_branch BRC node LVL FML) MNR node lvl fml ++ loop_tree_option_level_formula (r_branch BRC node LVL FML) MJR node lvl fml -- Mismatched node_option with the type of tree constructor | BRC (leaf LVL FML) node lvl fml := [] | BRC (u_tree LVL FML UNI) leaf lvl fml := loop_tree_option_level_formula (u_branch BRC node LVL FML) UNI leaf lvl fml | BRC (lr_tree LVL FML MNR MJR) leaf lvl fml := loop_tree_option_level_formula (l_branch BRC node LVL FML) MNR leaf lvl fml ++ loop_tree_option_level_formula (r_branch BRC node LVL FML) MJR leaf lvl fml -- Returns all the nodes in the tree, with a given constructor, at a given level, and with a specific formula def list_tree_option_level_formula : tree → node_option → ℕ → formula → list (branch) | t opt lvl fml := loop_tree_option_level_formula root t opt lvl fml -- Returns all the nodes in the tree, with a given constructor, and at a given level def loop_tree_option_level : tree → node_option → ℕ → list (formula) → list (list (branch)) | t opt lvl [] := [] | t opt lvl (HEAD::TAIL) := [list_tree_option_level_formula t opt lvl HEAD] ++ loop_tree_option_level t opt lvl TAIL def list_tree_option_level : tree → node_option → ℕ → list (list (branch)) | t opt lvl := loop_tree_option_level t opt lvl (list_formulas t) -- Returns all the nodes in the tree, with a given constructor def loop_tree_option :tree → node_option → list (ℕ) → list (list (list (branch))) | t opt [] := [] | t opt (HEAD::TAIL) := [list_tree_option_level t opt HEAD] ++ loop_tree_option t opt TAIL def list_tree_option : tree → node_option → list (list (list (branch))) | t opt := loop_tree_option t opt (list_levels t) -- Returns all the nodes in the tree def list_tree : tree → list (list (list (list (branch)))) | t := [list_tree_option t leaf] ++ [list_tree_option t node] /- Prooving the correctness of the list_tree_option_level_formula method -/ lemma Loop_Level_Formula_Option : ∀(mem b : branch)(t : tree)(opt : node_option)(lvl : ℕ)(fml : formula), ( mem ∈ loop_tree_option_level_formula b t opt lvl fml ) → --------------------------- ( (get_option mem = opt) ∧ (get_level mem = lvl) ∧ (get_formula mem = fml) ) -- When mem and opt match | mem BRC (leaf LVL FML) leaf lvl fml := begin assume mem_node, -- Simplifying ∈ at the condition(mem_node) simp [loop_tree_option_level_formula] at mem_node, simp [has_mem.mem] at mem_node, simp [use.ITE_Distribution (list.mem mem)] at mem_node, simp [list.mem] at mem_node, -- Cases over (LVL=lvl ∧ FML=fml) with_cases { by_cases and_eq : (LVL=lvl ∧ FML=fml) }, case pos { simp [and_eq] at mem_node, simp [mem_node, get_option, get_level, get_formula] }, -- The case where ¬(LVL=lvl ∧ FML=fml) is impossible, since b ∉ [] case neg { simp [and_eq] at mem_node, from false.elim mem_node } end | mem BRC (u_tree LVL FML UNI) node lvl fml := begin assume mem_node, -- Simplifying ∈ at the condition(mem_node) simp [loop_tree_option_level_formula] at mem_node, simp [has_mem.mem] at mem_node, simp [use.ITE_Distribution (list.mem mem)] at mem_node, simp [list.mem] at mem_node, -- Cases over (LVL=lvl ∧ FML=fml) with_cases { by_cases and_eq : (LVL=lvl ∧ FML=fml) }, case pos { simp [and_eq] at mem_node, simp [mem_node, get_option, get_level, get_formula] }, -- The case where ¬(LVL=lvl ∧ FML=fml) continues up the single child case neg { simp [and_eq] at mem_node, from Loop_Level_Formula_Option mem (u_branch BRC node LVL FML) UNI node lvl fml mem_node } end | mem BRC (lr_tree LVL FML MNR MJR) node lvl fml := begin assume mem_node, -- Simplifying ∈ at the condition(mem_node) simp [loop_tree_option_level_formula] at mem_node, simp [has_mem.mem] at mem_node, simp [use.ITE_Distribution (list.mem mem)] at mem_node, simp [list.mem] at mem_node, -- Cases over (LVL=lvl ∧ FML=fml) with_cases { by_cases and_eq : (LVL=lvl ∧ FML=fml) }, case pos { simp [and_eq] at mem_node, simp [mem_node, get_option, get_level, get_formula] }, -- The case where ¬(LVL=lvl ∧ FML=fml) continues up both children case neg { simp [and_eq] at mem_node, -- Using the relation between append[++] and or[∨] regarding item membership over lists have or_mem_node, from (iff.elim_left list.mem_append) mem_node, cases or_mem_node with mem_node mem_node, case or.inl { from Loop_Level_Formula_Option mem (l_branch BRC node LVL FML) MNR node lvl fml mem_node }, case or.inr { from Loop_Level_Formula_Option mem (r_branch BRC node LVL FML) MJR node lvl fml mem_node } } end -- When mem and opt don't match | mem BRC (leaf LVL FML) node lvl fml := begin assume mem_node, simp [loop_tree_option_level_formula] at mem_node, from false.elim mem_node end | mem BRC (u_tree LVL FML UNI) leaf lvl fml := begin assume mem_node, simp [loop_tree_option_level_formula] at mem_node, from Loop_Level_Formula_Option mem (u_branch BRC node LVL FML) UNI leaf lvl fml mem_node end | mem BRC (lr_tree LVL FML MNR MJR) leaf lvl fml := begin simp [loop_tree_option_level_formula], assume mem_node, cases mem_node, case or.inl { from Loop_Level_Formula_Option mem (l_branch BRC node LVL FML) MNR leaf lvl fml mem_node }, case or.inr { from Loop_Level_Formula_Option mem (r_branch BRC node LVL FML) MJR leaf lvl fml mem_node } end lemma From_Level_Formula_Option : ∀(mem : branch)(t : tree)(opt : node_option)(lvl : ℕ)(fml : formula), ( mem ∈ list_tree_option_level_formula t opt lvl fml ) → --------------------------- ( (get_option mem = opt) ∧ (get_level mem = lvl) ∧ (get_formula mem = fml) ) | mem t opt lvl fml := begin assume mem_node, simp [list_tree_option_level_formula] at mem_node, from Loop_Level_Formula_Option mem root t opt lvl fml mem_node end end types export types.formula (atom implication) export types.node_option (leaf node) export types.tree (leaf u_tree lr_tree) export types.branch (leaf u_branch l_branch r_branch root) /- Proof: Leaf Redundancy -/ namespace p_leaf /- Restrictions Regarding Proof-Trees -/ -- Trees must be finite, meaning that every node must have a path ending on a leaf def is_finite : types.tree → Prop | t := ∀(lvl : ℕ), --------------------------- ( aux.list_size (types.list_tree_option_level t node lvl) ≤ aux.list_size (types.list_tree_option t leaf) ) lemma Case_Option : ∀(t : types.tree), ∀(lll : list (list (list (types.branch)))), ( lll ∈ (types.list_tree t ) ) → --------------------------- ( ∃(opt : types.node_option), lll = types.list_tree_option t opt ) | t lll := begin assume or_eq, simp [types.list_tree] at or_eq, -- ∨-Elimination cases or_eq with eq_leaf eq_node, case or.inl { from exists.intro leaf eq_leaf }, case or.inr { from exists.intro node eq_node } end lemma Length_Tree : ∀(t : types.tree), --------------------------- ( 2 = list.length (types.list_tree t) ) | t := by simp [types.list_tree] lemma Tree_to_Option : ∀(t : types.tree), ∀{num : ℕ}, ( 2 * num < aux.list_size (types.list_tree t) ) → --------------------------- ( ∃(opt : types.node_option), num < aux.list_size (types.list_tree_option t opt) ) | t n := begin assume big_tree, -- Combinatory Property rewrite [Length_Tree t] at big_tree, have main_combinatory, from a_leaf.Main_Combinatory_Nested big_tree, cases main_combinatory with list_opt main_combinatory, cases main_combinatory with list_mem main_combinatory, -- Substitution have case_opt, from Case_Option t list_opt list_mem, cases case_opt with opt case_opt, rewrite [case_opt] at main_combinatory, -- ∃-Introduction from exists.intro opt main_combinatory end lemma Case_Level : ∀(t : types.tree)(opt : types.node_option)(loop : list (ℕ)), ∀(ll : list (list (types.branch))), ( ll ∈ types.loop_tree_option t opt loop ) → --------------------------- ( ∃(lvl : ℕ), lvl ∈ loop ∧ ll = types.list_tree_option_level t opt lvl ) | t opt [] ll := begin assume ll_mem, simp [types.loop_tree_option] at ll_mem, from false.elim ll_mem end | t opt (HEAD::TAIL) ll := begin simp [types.loop_tree_option], assume or_eq_mem, cases or_eq_mem with eq_head list_mem, case or.inl { apply exists.intro HEAD, simp [eq.refl HEAD], from eq_head }, case or.inr { have case_inductive, from Case_Level t opt TAIL ll list_mem, cases case_inductive with lvl case_inductive, apply exists.intro lvl, cases case_inductive with mem_lvl eq_lllt, simp [mem_lvl], from eq_lllt } end lemma Length_Tree_Option : ∀(t : types.tree)(opt : types.node_option), --------------------------- ( types.count_levels t = list.length (types.list_tree_option t opt) ) | t opt := begin simp [types.list_tree_option, types.count_levels], induction (types.list_levels t), case list.nil { simp [types.loop_tree_option] }, case list.cons { simp [types.loop_tree_option, ih] } end lemma Option_to_Level : ∀(t : types.tree)(opt : types.node_option), ∀{num : ℕ}, ( (types.count_levels t) * num < aux.list_size (types.list_tree_option t opt) ) → --------------------------- ( ∃(lvl : ℕ), lvl ∈ types.list_levels t ∧ num < aux.list_size (types.list_tree_option_level t opt lvl) ) | t opt num := begin assume big_option, -- Combinatory Property rewrite [Length_Tree_Option t opt] at big_option, have main_combinatory, from a_leaf.Main_Combinatory_Nested big_option, cases main_combinatory with list_lvl main_combinatory, cases main_combinatory with list_mem main_combinatory, -- Substitution have case_level, from Case_Level t opt (types.list_levels t) list_lvl list_mem, cases case_level with lvl case_level, cases case_level with mem_lvl case_level, rewrite [case_level] at main_combinatory, -- ∃-Introduction and ∧-Introduction apply exists.intro lvl, from and.intro mem_lvl main_combinatory end lemma Case_Formula : ∀(t : types.tree)(opt : types.node_option)(lvl : ℕ)(loop : list (types.formula)), ∀(l : list (types.branch)), ( l ∈ types.loop_tree_option_level t opt lvl loop ) → --------------------------- ( ∃(fml : types.formula), fml ∈ loop ∧ l = types.list_tree_option_level_formula t opt lvl fml ) | t opt lvl [] ll := begin assume l_mem, simp [types.loop_tree_option_level] at l_mem, from false.elim l_mem end | t opt lvl (HEAD::TAIL) ll := begin simp [types.loop_tree_option_level], assume or_eq_mem, cases or_eq_mem with eq_head list_mem, case or.inl { apply exists.intro HEAD, simp [eq.refl HEAD], from eq_head }, case or.inr { have case_inductive, from Case_Formula t opt lvl TAIL ll list_mem, cases case_inductive with fml case_inductive, apply exists.intro fml, cases case_inductive with mem_fml eq_llt, simp [mem_fml], from eq_llt } end lemma Length_Tree_Option_Level: ∀(t : types.tree)(opt : types.node_option)(lvl : ℕ), --------------------------- ( types.count_formulas t = list.length (types.list_tree_option_level t opt lvl) ) | t opt lvl := begin simp [types.list_tree_option_level, types.count_formulas], induction (types.list_formulas t), case list.nil { simp [types.loop_tree_option_level] }, case list.cons { simp [types.loop_tree_option_level, ih] } end lemma Level_to_Formula : ∀(t : types.tree)(opt : types.node_option)(lvl : ℕ), ∀{num : ℕ}, ( (types.count_formulas t) * num < aux.list_size (types.list_tree_option_level t opt lvl) ) → --------------------------- ( ∃(fml : types.formula), fml ∈ types.list_formulas t ∧ num < aux.list_size (types.list_tree_option_level_formula t opt lvl fml) ) | t opt lvl num := begin assume big_option_level, -- Combinatory Property rewrite [Length_Tree_Option_Level t opt lvl] at big_option_level, have main_combinatory, from a_leaf.Main_Combinatory_Nested big_option_level, cases main_combinatory with list_fml main_combinatory, cases main_combinatory with list_mem main_combinatory, -- Substitution have case_fml, from Case_Formula t opt lvl (types.list_formulas t) list_fml list_mem, cases case_fml with fml case_fml, cases case_fml with mem_fml case_fml, rewrite [case_fml] at main_combinatory, -- ∃-Introduction and ∧-Introduction apply exists.intro fml, from and.intro mem_fml main_combinatory end lemma Option_to_Leaf : ∀(t : types.tree)(opt : types.node_option), ∀{num : ℕ}, ( is_finite t ) → ( (types.count_levels t) * num < aux.list_size (types.list_tree_option t opt) ) → --------------------------- ( num < aux.list_size (types.list_tree_option t leaf) ) | t leaf num := begin assume finite big_option, have levels_pos, from aux.ZERO_LT_Length_of_LT_Size big_option, rewrite [←Length_Tree_Option t leaf] at levels_pos, have le_mul, from use.LE_Mul_Left_of_Pos levels_pos num, from nat.lt_of_le_of_lt le_mul big_option end | t node num := begin assume finite big_option, have big_lrnode_level, from Option_to_Level t node big_option, cases big_lrnode_level with lvl big_lrnode_level, cases big_lrnode_level with mem_lvl big_lrnode, from nat.lt_of_lt_of_le big_lrnode (finite lvl) end theorem Main_Theorem_Leaves : ∀(t : types.tree)(mul num : ℕ), ( is_finite t ) → ( mul = (2 * (types.count_levels t * (types.count_levels t * (types.count_formulas t * (types.count_formulas t * num))))) ) → ( mul < aux.list_size (types.list_tree t) ) → --------------------------- ( ∃(lvl : ℕ)(fml : types.formula), lvl ∈ types.list_levels t ∧ fml ∈ types.list_formulas t ∧ (types.count_formulas t) * num < aux.list_size (types.list_tree_option_level_formula t leaf lvl fml) ) | t mul num := begin assume finite mul_constant big_tree, rewrite [mul_constant] at big_tree, -- Find a redundant type-constructor within the tree: have big_option, from Tree_to_Option t big_tree, cases big_option with opt big_option, -- Since the tree is finite, there must be a redundant number of leaves within the tree: have big_leaf, from Option_to_Leaf t opt finite big_option, -- Find a redundant level within the repeating leaf-constructor: have big_leaf_level, from Option_to_Level t leaf big_leaf, cases big_leaf_level with lvl big_leaf_level, cases big_leaf_level with mem_lvl big_leaf_level, apply exists.intro lvl, simp [mem_lvl], -- Find a redundant formula within the repeating leaf-constructor/level: have big_leaf_level_formula, from Level_to_Formula t leaf lvl big_leaf_level, cases big_leaf_level_formula with fml big_leaf_level_formula, cases big_leaf_level_formula with mem_fml big_leaf_level_formula, apply exists.intro fml, simp [mem_fml], from big_leaf_level_formula end end p_leaf /- Proof: Leaf Redundancy Implies Proof-Branch Redundancy -/ namespace p_branch /- Methods Regarding Proof-Branchs (Prop) -/ -- Proofs and Derivations in Natural Deduction can be seen as Proof-Trees -- Proofs have no open formulas, while Derivations may have open formulas -- In a Normal Proof-Tree, every Leaf is part of a Proof-Branch -- A Proof-Branch can be obtained by pruning the Branch which connects its Leaf to the proof's conclusion -- This pruning is done top-down, from the Leaf to either the minor premise of an →ELIMINATION or the proof's conclusion -- Along the pruning, if a visited node in the unpruned Branch is the result of an: -- →ELIMINATION from its major premise: visited node = CON; visited premise = _ >> CON; the Proof-Branch continues -- →ELIMINATION from its minor premise: visited node = some formula; the Proof-Branch ends at the visited premise -- →INTRODUCTION: -- In a Normal Proof-Tree, an →INTRODUCTION can only be followed by an: -- →INTRODUCTION: visited node = _ >> CON; visited premise = CON; the Proof-Branch continues -- →ELIMINATION from its minor premise: visited node = some formula; the Proof-Branch ends at the visited premise -- The proof's conclusion: the Proof-Branch ends at the proof's conclusion -- But no matter where this →INTRODUCTION sequence ends, the formula where it ends determines the sequence -- Every Proof-Branch of a Normal Proof-Tree is either: -- Composed of an →ELIMINATION (from its major premise) part followed by an →INTRODUCTION part -- Composed of an →ELIMINATION (from its major premise) only -- Composed of an →INTRODUCTION only -- A Leaf that is the minor premise of an →ELIMINATION -- In an Atomicaly Expanded Normal Proof-Tree: -- If there is an →ELIMINATION part, it ends in a atomic formula -- If there is an →INTRODUCTION part, it begins in a atomic formula -- That is to say, in an Atomicaly Expanded Normal Proof-Tree: -- The Proof-Branch's Leaf determines completely the →ELIMINATION part -- The Proof-Branch's Ending Formula completely determines the →INTRODUCTION part -- Loops over the →INTRODUCTION part def loop_check_intro : ℕ → types.formula → types.branch → Prop | lvl1 fml1 (u_branch BRC OPT LVL0 FML0) := ( OPT = node ) ∧ ( lvl1 = LVL0+1 ) ∧ ( types.not_atomic FML0 ∧ types.get_consequent FML0 = fml1 ) ∧ loop_check_intro LVL0 FML0 BRC | lvl1 fml1 (l_branch BRC OPT LVL0 FML0) := ( OPT = node ) ∧ ( lvl1 = LVL0+1 ) | lvl1 fml1 root := ( lvl1 = 1 ) | _ _ _ := false -- Loops over the →ELIMINATION part and verifies if the minimal formula is atomic def loop_check_elim : ℕ → types.formula → types.branch → Prop | lvl1 fml1 (u_branch BRC OPT LVL0 FML0) := ( OPT = node ) ∧ ( lvl1 = LVL0+1 ) ∧ ( types.is_atomic fml1 ∧ types.not_atomic FML0 ∧ types.get_consequent FML0 = fml1 ) ∧ loop_check_intro LVL0 FML0 BRC | lvl1 fml1 (l_branch BRC OPT LVL0 FML0) := ( OPT = node ) ∧ ( lvl1 = LVL0+1 ) ∧ ( types.is_atomic fml1 ) | lvl1 fml1 (r_branch BRC OPT LVL0 FML0) := ( OPT = node ) ∧ ( lvl1 = LVL0+1 ) ∧ ( types.not_atomic fml1 ∧ types.get_consequent fml1 = FML0 ) ∧ loop_check_elim LVL0 FML0 BRC | lvl1 fml1 root := ( lvl1 = 1 ) ∧ ( types.is_atomic fml1 ) | _ _ _ := false -- Verifies the type of branch (→ELIMINATION→INTRODUCTION; →ELIMINATION; →INTRODUCTION; Leaf) def check_leaf : ℕ → types.formula → types.branch → Prop | lvl1 fml1 (u_branch BRC OPT LVL0 FML0) := ( OPT = node ) ∧ ( lvl1 = LVL0+1 ) ∧ ( types.is_atomic fml1 ∧ types.not_atomic FML0 ∧ types.get_consequent FML0 = fml1 ) ∧ loop_check_intro LVL0 FML0 BRC | lvl1 fml1 (l_branch BRC OPT LVL0 FML0) := ( OPT = node ) ∧ ( lvl1 = LVL0+1 ) | lvl1 fml1 (r_branch BRC OPT LVL0 FML0) := ( OPT = node ) ∧ ( lvl1 = LVL0+1 ) ∧ ( types.not_atomic fml1 ∧ types.get_consequent fml1 = FML0 ) ∧ loop_check_elim LVL0 FML0 BRC | _ _ _ := false -- To check if a regular Branch, ending at the Proof-Tree's conclusion, can be pruned into a Proof-Branch def check_proof_branch : types.branch → Prop | (leaf BRC leaf LVL0 FML0) := check_leaf LVL0 FML0 BRC | _ := false -- Using check_proof_branch as an if-clause demands a proof of decidability for each method instance types.branch.loop_check_intro : ∀(lvl1 : ℕ)(fml1 : types.formula)(b : types.branch), --------------------------------- decidable (loop_check_intro lvl1 fml1 b) | _ _ (leaf _ _ _ _) := begin simp [loop_check_intro], from is_false not_false end | lvl1 fml1 (u_branch BRC OPT LVL0 FML0) := begin simp [loop_check_intro], have dec_and1, from @and.decidable (types.not_atomic FML0) (types.get_consequent FML0 = fml1) (@types.formula.not_atomic FML0) (@types.formula.decidable_eq (types.get_consequent FML0) fml1), have dec_and2, from @and.decidable (types.not_atomic FML0 ∧ types.get_consequent FML0 = fml1) (loop_check_intro LVL0 FML0 BRC) dec_and1 (@types.branch.loop_check_intro LVL0 FML0 BRC), have dec_and3, from @and.decidable (lvl1 = LVL0+1) ((types.not_atomic FML0 ∧ types.get_consequent FML0 = fml1) ∧ loop_check_intro LVL0 FML0 BRC) (@nat.decidable_eq lvl1 (LVL0+1)) dec_and2, from @and.decidable (OPT = node) (lvl1 = LVL0+1 ∧ (types.not_atomic FML0 ∧ types.get_consequent FML0 = fml1) ∧ loop_check_intro LVL0 FML0 BRC) (@types.node_option.decidable_eq OPT node) dec_and3 end | lvl1 fml1 (l_branch BRC OPT LVL0 FML0) := begin simp [loop_check_intro], from @and.decidable (OPT = node) (lvl1 = LVL0 + 1) (@types.node_option.decidable_eq OPT node) (@nat.decidable_eq lvl1 (LVL0+1)) end | _ _ (r_branch _ _ _ _) := begin simp [loop_check_intro], from is_false not_false end | lvl1 fml1 root := begin simp [loop_check_intro], from @nat.decidable_eq lvl1 1 end instance types.branch.loop_check_elim : ∀(lvl1 : ℕ)(fml1 : types.formula)(b : types.branch), --------------------------------- decidable (loop_check_elim lvl1 fml1 b) | _ _ (leaf _ _ _ _) := begin simp [loop_check_elim], from is_false not_false end | lvl1 fml1 (u_branch BRC OPT LVL0 FML0) := begin simp [loop_check_elim], have dec_and1, from @and.decidable (types.not_atomic FML0) (types.get_consequent FML0 = fml1) (@types.formula.not_atomic FML0) (@types.formula.decidable_eq (types.get_consequent FML0) fml1), have dec_and2, from @and.decidable (types.is_atomic fml1) (types.not_atomic FML0 ∧ types.get_consequent FML0 = fml1) (@types.formula.is_atomic fml1) dec_and1, have dec_and3, from @and.decidable (types.is_atomic fml1 ∧ types.not_atomic FML0 ∧ types.get_consequent FML0 = fml1) (loop_check_intro LVL0 FML0 BRC) dec_and2 (@types.branch.loop_check_intro LVL0 FML0 BRC), have dec_and4, from @and.decidable (lvl1 = LVL0+1) ((types.is_atomic fml1 ∧ types.not_atomic FML0 ∧ types.get_consequent FML0 = fml1) ∧ loop_check_intro LVL0 FML0 BRC) (@nat.decidable_eq lvl1 (LVL0+1)) dec_and3, from @and.decidable (OPT = node) (lvl1 = LVL0+1 ∧ (types.is_atomic fml1 ∧ types.not_atomic FML0 ∧ types.get_consequent FML0 = fml1) ∧ loop_check_intro LVL0 FML0 BRC) (@types.node_option.decidable_eq OPT node) dec_and4 end | lvl1 fml1 (l_branch BRC OPT LVL0 FML0) := begin simp [loop_check_elim], have dec_and1, from @and.decidable (lvl1 = LVL0 + 1) (types.is_atomic fml1) (@nat.decidable_eq lvl1 (LVL0+1)) (@types.formula.is_atomic fml1), from @and.decidable (OPT = node) (lvl1 = LVL0 + 1 ∧ types.is_atomic fml1) (@types.node_option.decidable_eq OPT node) dec_and1 end | lvl1 fml1 (r_branch BRC OPT LVL0 FML0) := begin simp [loop_check_elim], have dec_and1, from @and.decidable (types.not_atomic fml1) (types.get_consequent fml1 = FML0) (@types.formula.not_atomic fml1) (@types.formula.decidable_eq (types.get_consequent fml1) FML0), have dec_and2, from @and.decidable (types.not_atomic fml1 ∧ types.get_consequent fml1 = FML0) (loop_check_elim LVL0 FML0 BRC) dec_and1 (@types.branch.loop_check_elim LVL0 FML0 BRC), have dec_and3, from @and.decidable (lvl1 = LVL0+1) ((types.not_atomic fml1 ∧ types.get_consequent fml1 = FML0) ∧ loop_check_elim LVL0 FML0 BRC) (@nat.decidable_eq lvl1 (LVL0+1)) dec_and2, from @and.decidable (OPT = node) (lvl1 = LVL0+1 ∧ (types.not_atomic fml1 ∧ types.get_consequent fml1 = FML0) ∧ loop_check_elim LVL0 FML0 BRC) (@types.node_option.decidable_eq OPT node) dec_and3 end | lvl1 fml1 root := begin simp [loop_check_elim], from @and.decidable (lvl1 = 1) (types.is_atomic fml1) (@nat.decidable_eq lvl1 1) (@types.formula.is_atomic fml1) end instance types.branch.check_leaf : ∀(lvl1 : ℕ)(fml1 : types.formula)(b : types.branch), --------------------------------- decidable (check_leaf lvl1 fml1 b) | _ _ (leaf _ _ _ _) := begin simp [check_leaf], from is_false not_false end | lvl1 fml1 (u_branch BRC OPT LVL0 FML0) := begin simp [check_leaf], have dec_and1, from @and.decidable (types.not_atomic FML0) (types.get_consequent FML0 = fml1) (@types.formula.not_atomic FML0) (@types.formula.decidable_eq (types.get_consequent FML0) fml1), have dec_and2, from @and.decidable (types.is_atomic fml1) (types.not_atomic FML0 ∧ types.get_consequent FML0 = fml1) (@types.formula.is_atomic fml1) dec_and1, have dec_and3, from @and.decidable (types.is_atomic fml1 ∧ types.not_atomic FML0 ∧ types.get_consequent FML0 = fml1) (loop_check_intro LVL0 FML0 BRC) dec_and2 (@types.branch.loop_check_intro LVL0 FML0 BRC), have dec_and4, from @and.decidable (lvl1 = LVL0+1) ((types.is_atomic fml1 ∧ types.not_atomic FML0 ∧ types.get_consequent FML0 = fml1) ∧ loop_check_intro LVL0 FML0 BRC) (@nat.decidable_eq lvl1 (LVL0+1)) dec_and3, from @and.decidable (OPT = node) (lvl1 = LVL0+1 ∧ (types.is_atomic fml1 ∧ types.not_atomic FML0 ∧ types.get_consequent FML0 = fml1) ∧ loop_check_intro LVL0 FML0 BRC) (@types.node_option.decidable_eq OPT node) dec_and4 end | lvl1 fml1 (l_branch BRC OPT LVL0 FML0) := begin simp [check_leaf], from @and.decidable (OPT = node) (lvl1 = LVL0 + 1) (@types.node_option.decidable_eq OPT node) (@nat.decidable_eq lvl1 (LVL0+1)) end | lvl1 fml1 (r_branch BRC OPT LVL0 FML0) := begin simp [check_leaf], have dec_and1, from @and.decidable (types.not_atomic fml1) (types.get_consequent fml1 = FML0) (@types.formula.not_atomic fml1) (@types.formula.decidable_eq (types.get_consequent fml1) FML0), have dec_and2, from @and.decidable (types.not_atomic fml1 ∧ types.get_consequent fml1 = FML0) (loop_check_elim LVL0 FML0 BRC) dec_and1 (@types.branch.loop_check_elim LVL0 FML0 BRC), have dec_and3, from @and.decidable (lvl1 = LVL0+1) ((types.not_atomic fml1 ∧ types.get_consequent fml1 = FML0) ∧ loop_check_elim LVL0 FML0 BRC) (@nat.decidable_eq lvl1 (LVL0+1)) dec_and2, from @and.decidable (OPT = node) (lvl1 = LVL0+1 ∧ (types.not_atomic fml1 ∧ types.get_consequent fml1 = FML0) ∧ loop_check_elim LVL0 FML0 BRC) (@types.node_option.decidable_eq OPT node) dec_and3 end | _ _ root := begin simp [check_leaf], from is_false not_false end instance types.branch.check_proof_branch : ∀(b : types.branch), --------------------------------- decidable (check_proof_branch b) | (leaf BRC leaf LVL0 FML0) := begin simp [check_proof_branch], from @types.branch.check_leaf LVL0 FML0 BRC end | (leaf _ node _ _) := begin simp [check_proof_branch], from is_false not_false end | (u_branch _ _ _ _) := begin simp [check_proof_branch], from is_false not_false end | (l_branch _ _ _ _) := begin simp [check_proof_branch], from is_false not_false end | (r_branch _ _ _ _) := begin simp [check_proof_branch], from is_false not_false end | root := begin simp [check_proof_branch], from is_false not_false end /- Methods Regarding Proof-Branchs (Formulas) -/ -- Finally, the pruning, per se def loop_prune_intro : ℕ → types.formula → types.branch → types.formula | lvl1 fml1 (u_branch BRC OPT LVL0 FML0) := if loop_check_intro lvl1 fml1 (u_branch BRC OPT LVL0 FML0) then loop_prune_intro LVL0 FML0 BRC else #0 --false | lvl1 fml1 (l_branch BRC OPT LVL0 FML0) := if loop_check_intro lvl1 fml1 (l_branch BRC OPT LVL0 FML0) then fml1 else #0 --false | lvl1 fml1 root := if loop_check_intro lvl1 fml1 root then fml1 else #0 --false | _ _ _ := #0 --false def loop_prune_elim : ℕ → types.formula → types.branch → types.formula | lvl1 fml1 (u_branch BRC OPT LVL0 FML0) := if loop_check_elim lvl1 fml1 (u_branch BRC OPT LVL0 FML0) then loop_prune_intro LVL0 FML0 BRC else #0 --false | lvl1 fml1 (l_branch BRC OPT LVL0 FML0) := if loop_check_elim lvl1 fml1 (l_branch BRC OPT LVL0 FML0) then fml1 else #0 --false | lvl1 fml1 (r_branch BRC OPT LVL0 FML0) := if loop_check_elim lvl1 fml1 (r_branch BRC OPT LVL0 FML0) then loop_prune_elim LVL0 FML0 BRC else #0 --false | lvl1 fml1 root := if loop_check_elim lvl1 fml1 root then fml1 else #0 --false | _ _ _ := #0 --false def prune_leaf : ℕ → types.formula → types.branch → types.formula | lvl1 fml1 (u_branch BRC OPT LVL0 FML0) := if check_leaf lvl1 fml1 (u_branch BRC OPT LVL0 FML0) then loop_prune_intro LVL0 FML0 BRC else #0 --false | lvl1 fml1 (l_branch BRC OPT LVL0 FML0) := if check_leaf lvl1 fml1 (l_branch BRC OPT LVL0 FML0) then fml1 else #0 --false | lvl1 fml1 (r_branch BRC OPT LVL0 FML0) := if check_leaf lvl1 fml1 (r_branch BRC OPT LVL0 FML0) then loop_prune_elim LVL0 FML0 BRC else #0 --false | _ _ _ := #0 --false def prune_proof_branch : types.branch → types.formula | (leaf BRC leaf LVL0 FML0) := prune_leaf LVL0 FML0 BRC | _ := #0 --false def prune_branch : types.branch → (types.formula) | (leaf BRC leaf LVL FML) := #0 | _ := #0 --false -- Defining a method to prune all the branches in a list of branches def prune_list_branch : list (types.branch) → list (types.formula) | [] := [] | (HEAD::TAIL) := if check_proof_branch HEAD then [prune_branch HEAD] ++ prune_list_branch TAIL else prune_list_branch TAIL -- def list_proof_branch_root : types.tree → types.node_option → ℕ → types.formula→ types.formula → list (types.formula) | t opt lvl1 fml1 fml0 := a_branch.list_filter fml0 (prune_list_branch (types.list_tree_option_level_formula t leaf lvl1 fml1)) /- Restrictions Regarding Proof-Branchs -/ -- In a Normal Proof-Tree, every Leaf is part of a Proof-Branch def is_normal_check : types.tree → Prop | t := ∀(lvl1 : ℕ)(fml1 : types.formula)(b : types.branch), ( b ∈ (types.list_tree_option_level_formula t leaf lvl1 fml1) ) → --------------------------- ( check_proof_branch b ) def is_normal_mem : types.tree → Prop | t := ∀(lvl1 : ℕ)(fml1 fml0 : types.formula), ( fml0 ∈ prune_list_branch (types.list_tree_option_level_formula t leaf lvl1 fml1) ) → --------------------------- ( fml0 ∈ types.list_formulas t ) lemma Case_Size : ∀{t : types.tree}{lvl1 : ℕ}{fml1 : types.formula}, ∀{l : list (types.branch)}, ( is_normal_check t ) → ( ∀(b : types.branch), b ∈ l → b ∈ types.list_tree_option_level_formula t leaf lvl1 fml1 ) → --------------------------- ( aux.list_size l = aux.list_size (prune_list_branch l) ) | t lvl1 fml1 [] := begin assume normal_check subset, simp [prune_list_branch, aux.list_size] end | t lvl1 fml1 (HEAD::TAIL) := begin assume normal_check subset, have head_mem : HEAD ∈ types.list_tree_option_level_formula t leaf lvl1 fml1, from begin apply (subset HEAD), simp [has_mem.mem, list.mem] end, have tail_subset : ∀(b : types.branch), b ∈ TAIL → b ∈ types.list_tree_option_level_formula t leaf lvl1 fml1, from begin assume loop loop_mem_tail, apply (subset loop), simp [has_mem.mem, list.mem] at ⊢ loop_mem_tail, simp [loop_mem_tail] end, simp [prune_list_branch], rewrite [use.ITE_Distribution (aux.list_size)], simp [aux.list_size, aux.size, aux.has_size.size], have check_head, from normal_check lvl1 fml1 HEAD head_mem, simp [implies, has_mem.mem, list.mem] at check_head, simp [check_head], rewrite [Case_Size normal_check tail_subset] end lemma Normal_Size : ∀(t : types.tree)(lvl1 : ℕ)(fml1 : types.formula), ( is_normal_check t ) → --------------------------- ( aux.list_size (types.list_tree_option_level_formula t leaf lvl1 fml1) = aux.list_size (prune_list_branch (types.list_tree_option_level_formula t leaf lvl1 fml1)) ) | t lvl1 fml1 := begin assume normal_check, from Case_Size normal_check (use.Subset_Self (types.list_tree_option_level_formula t leaf lvl1 fml1)) end lemma Case_Set : ∀(t : types.tree)(lvl1 : ℕ)(fml1 : types.formula)(n : ℕ), ( is_normal_mem t ) → --------------------------- ( list.length (a_branch.list_set (prune_list_branch (types.list_tree_option_level_formula t leaf lvl1 fml1))) * n ≤ list.length (types.list_formulas t) * n ) | t lvl1 fml1 n := begin assume normal_mem, have set_le_formulas, from a_branch.Length_Set_LE_Length_of_Subset (normal_mem lvl1 fml1), from use.LE_Mul_of_LE n set_le_formulas end theorem Main_Theorem_Branchs : ∀(t : types.tree)(lvl1 : ℕ)(fml1 : types.formula)(num : ℕ), ( is_normal_check t ) → ( is_normal_mem t ) → ( (types.count_formulas t) * num < aux.list_size (types.list_tree_option_level_formula t leaf lvl1 fml1) ) → --------------------------- ( ∃(fml0 : types.formula), fml0 ∈ prune_list_branch (types.list_tree_option_level_formula t leaf lvl1 fml1) ∧ num < aux.list_size (list_proof_branch_root t leaf lvl1 fml1 fml0) ) | t lvl1 fml1 num := begin assume normal_check normal_mem big_leaf_level_formula, simp [list_proof_branch_root, aux.list_size] at ⊢ big_leaf_level_formula, simp [types.count_formulas, nat.mul_assoc] at big_leaf_level_formula, rewrite [Normal_Size t lvl1 fml1 normal_check] at big_leaf_level_formula, -- have main_combinatory, from a_branch.Main_Combinatory_Filter num (prune_list_branch (types.list_tree_option_level_formula t leaf lvl1 fml1)), -- have case_set, from Case_Set t lvl1 fml1 num normal_mem, have big_branch, from main_combinatory (nat.lt_of_le_of_lt case_set big_leaf_level_formula), cases big_branch with fml0 big_branch, cases big_branch with mem_fml0 big_branch, apply exists.intro fml0, simp [mem_fml0], from big_branch end end p_branch /- Proof: Theorem of Proof-Branch Redundancy -/ theorem Proof_Branch_Redundancy : ∀(t : types.tree)(mul num : ℕ), ( p_leaf.is_finite t ) → ( p_branch.is_normal_check t ) → ( p_branch.is_normal_mem t ) → ( mul = (2 * (types.count_levels t * (types.count_levels t * (types.count_formulas t * (types.count_formulas t * num))))) ) → ( mul < aux.list_size (types.list_tree t) ) → --------------------------- ( ∃(lvl1 : ℕ)(fml1 fml0 : types.formula), lvl1 ∈ types.list_levels t ∧ fml1 ∈ types.list_formulas t ∧ fml0 ∈ p_branch.prune_list_branch (types.list_tree_option_level_formula t leaf lvl1 fml1) ∧ num < aux.list_size (p_branch.list_proof_branch_root t leaf lvl1 fml1 fml0) ) | t mul num := begin assume finite normal_check normal_mem mul_constant big_tree, -- Find a repeating leaf-constructor/level/formula: have big_leaf_level_formula, from p_leaf.Main_Theorem_Leaves t mul num finite mul_constant big_tree, cases big_leaf_level_formula with lvl1 big_leaf_level_formula, cases big_leaf_level_formula with fml1 big_leaf_level_formula, cases big_leaf_level_formula with mem_lvl1 big_leaf_level_formula, cases big_leaf_level_formula with mem_fml1 big_leaf_level_formula, apply exists.intro lvl1, simp [mem_lvl1], apply exists.intro fml1, simp [mem_fml1], -- Find a redundant proof-branch from the repeating leaf-constructor/level/formula: have big_branch, from p_branch.Main_Theorem_Branchs t lvl1 fml1 num normal_check normal_mem big_leaf_level_formula, cases big_branch with fml0 big_branch, cases big_branch with mem_fml0 big_branch, apply exists.intro fml0, simp [mem_fml0], from big_branch end
029d81dc0d4efc82e67a6a5a54479f8c7b4f5115
94e33a31faa76775069b071adea97e86e218a8ee
/src/analysis/calculus/implicit.lean
ee25f945f903b85ac95bc41db77a44eeb007b2c0
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
20,145
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.calculus.inverse import analysis.normed_space.complemented /-! # Implicit function theorem We prove three versions of the implicit function theorem. First we define a structure `implicit_function_data` that holds arguments for the most general version of the implicit function theorem, see `implicit_function_data.implicit_function` and `implicit_function_data.to_implicit_function`. This version allows a user to choose a specific implicit function but provides only a little convenience over the inverse function theorem. Then we define `implicit_function_of_complemented`: implicit function defined by `f (g z y) = z`, where `f : E → F` is a function strictly differentiable at `a` such that its derivative `f'` is surjective and has a `complemented` kernel. Finally, if the codomain of `f` is a finite dimensional space, then we can automatically prove that the kernel of `f'` is complemented, hence the only assumptions are `has_strict_fderiv_at` and `f'.range = ⊤`. This version is named `implicit_function`. ## TODO * Add a version for a function `f : E × F → G` such that $$\frac{\partial f}{\partial y}$$ is invertible. * Add a version for `f : 𝕜 × 𝕜 → 𝕜` proving `has_strict_deriv_at` and `deriv φ = ...`. * Prove that in a real vector space the implicit function has the same smoothness as the original one. * If the original function is differentiable in a neighborhood, then the implicit function is differentiable in a neighborhood as well. Current setup only proves differentiability at one point for the implicit function constructed in this file (as opposed to an unspecified implicit function). One of the ways to overcome this difficulty is to use uniqueness of the implicit function in the general version of the theorem. Another way is to prove that *any* implicit function satisfying some predicate is strictly differentiable. ## Tags implicit function, inverse function -/ noncomputable theory open_locale topological_space open filter open continuous_linear_map (fst snd subtype_val smul_right ker_prod) open continuous_linear_equiv (of_bijective) /-! ### General version Consider two functions `f : E → F` and `g : E → G` and a point `a` such that * both functions are strictly differentiable at `a`; * the derivatives are surjective; * the kernels of the derivatives are complementary subspaces of `E`. Note that the map `x ↦ (f x, g x)` has a bijective derivative, hence it is a local homeomorphism between `E` and `F × G`. We use this fact to define a function `φ : F → G → E` (see `implicit_function_data.implicit_function`) such that for `(y, z)` close enough to `(f a, g a)` we have `f (φ y z) = y` and `g (φ y z) = z`. We also prove a formula for $$\frac{\partial\varphi}{\partial z}.$$ Though this statement is almost symmetric with respect to `F`, `G`, we interpret it in the following way. Consider a family of surfaces `{x | f x = y}`, `y ∈ 𝓝 (f a)`. Each of these surfaces is parametrized by `φ y`. There are many ways to choose a (differentiable) function `φ` such that `f (φ y z) = y` but the extra condition `g (φ y z) = z` allows a user to select one of these functions. If we imagine that the level surfaces `f = const` form a local horizontal foliation, then the choice of `g` fixes a transverse foliation `g = const`, and `φ` is the inverse function of the projection of `{x | f x = y}` along this transverse foliation. This version of the theorem is used to prove the other versions and can be used if a user needs to have a complete control over the choice of the implicit function. -/ /-- Data for the general version of the implicit function theorem. It holds two functions `f : E → F` and `g : E → G` (named `left_fun` and `right_fun`) and a point `a` (named `pt`) such that * both functions are strictly differentiable at `a`; * the derivatives are surjective; * the kernels of the derivatives are complementary subspaces of `E`. -/ @[nolint has_inhabited_instance] structure implicit_function_data (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] [complete_space E] (F : Type*) [normed_group F] [normed_space 𝕜 F] [complete_space F] (G : Type*) [normed_group G] [normed_space 𝕜 G] [complete_space G] := (left_fun : E → F) (left_deriv : E →L[𝕜] F) (right_fun : E → G) (right_deriv : E →L[𝕜] G) (pt : E) (left_has_deriv : has_strict_fderiv_at left_fun left_deriv pt) (right_has_deriv : has_strict_fderiv_at right_fun right_deriv pt) (left_range : left_deriv.range = ⊤) (right_range : right_deriv.range = ⊤) (is_compl_ker : is_compl left_deriv.ker right_deriv.ker) namespace implicit_function_data variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type*} [normed_group F] [normed_space 𝕜 F] [complete_space F] {G : Type*} [normed_group G] [normed_space 𝕜 G] [complete_space G] (φ : implicit_function_data 𝕜 E F G) /-- The function given by `x ↦ (left_fun x, right_fun x)`. -/ def prod_fun (x : E) : F × G := (φ.left_fun x, φ.right_fun x) @[simp] lemma prod_fun_apply (x : E) : φ.prod_fun x = (φ.left_fun x, φ.right_fun x) := rfl protected lemma has_strict_fderiv_at : has_strict_fderiv_at φ.prod_fun (φ.left_deriv.equiv_prod_of_surjective_of_is_compl φ.right_deriv φ.left_range φ.right_range φ.is_compl_ker : E →L[𝕜] F × G) φ.pt := φ.left_has_deriv.prod φ.right_has_deriv /-- Implicit function theorem. If `f : E → F` and `g : E → G` are two maps strictly differentiable at `a`, their derivatives `f'`, `g'` are surjective, and the kernels of these derivatives are complementary subspaces of `E`, then `x ↦ (f x, g x)` defines a local homeomorphism between `E` and `F × G`. In particular, `{x | f x = f a}` is locally homeomorphic to `G`. -/ def to_local_homeomorph : local_homeomorph E (F × G) := φ.has_strict_fderiv_at.to_local_homeomorph _ /-- Implicit function theorem. If `f : E → F` and `g : E → G` are two maps strictly differentiable at `a`, their derivatives `f'`, `g'` are surjective, and the kernels of these derivatives are complementary subspaces of `E`, then `implicit_function_of_is_compl_ker` is the unique (germ of a) map `φ : F → G → E` such that `f (φ y z) = y` and `g (φ y z) = z`. -/ def implicit_function : F → G → E := function.curry $ φ.to_local_homeomorph.symm @[simp] lemma to_local_homeomorph_coe : ⇑(φ.to_local_homeomorph) = φ.prod_fun := rfl lemma to_local_homeomorph_apply (x : E) : φ.to_local_homeomorph x = (φ.left_fun x, φ.right_fun x) := rfl lemma pt_mem_to_local_homeomorph_source : φ.pt ∈ φ.to_local_homeomorph.source := φ.has_strict_fderiv_at.mem_to_local_homeomorph_source lemma map_pt_mem_to_local_homeomorph_target : (φ.left_fun φ.pt, φ.right_fun φ.pt) ∈ φ.to_local_homeomorph.target := φ.to_local_homeomorph.map_source $ φ.pt_mem_to_local_homeomorph_source lemma prod_map_implicit_function : ∀ᶠ (p : F × G) in 𝓝 (φ.prod_fun φ.pt), φ.prod_fun (φ.implicit_function p.1 p.2) = p := φ.has_strict_fderiv_at.eventually_right_inverse.mono $ λ ⟨z, y⟩ h, h lemma left_map_implicit_function : ∀ᶠ (p : F × G) in 𝓝 (φ.prod_fun φ.pt), φ.left_fun (φ.implicit_function p.1 p.2) = p.1 := φ.prod_map_implicit_function.mono $ λ z, congr_arg prod.fst lemma right_map_implicit_function : ∀ᶠ (p : F × G) in 𝓝 (φ.prod_fun φ.pt), φ.right_fun (φ.implicit_function p.1 p.2) = p.2 := φ.prod_map_implicit_function.mono $ λ z, congr_arg prod.snd lemma implicit_function_apply_image : ∀ᶠ x in 𝓝 φ.pt, φ.implicit_function (φ.left_fun x) (φ.right_fun x) = x := φ.has_strict_fderiv_at.eventually_left_inverse lemma map_nhds_eq : map φ.left_fun (𝓝 φ.pt) = 𝓝 (φ.left_fun φ.pt) := show map (prod.fst ∘ φ.prod_fun) (𝓝 φ.pt) = 𝓝 (φ.prod_fun φ.pt).1, by rw [← map_map, φ.has_strict_fderiv_at.map_nhds_eq_of_equiv, map_fst_nhds] lemma implicit_function_has_strict_fderiv_at (g'inv : G →L[𝕜] E) (hg'inv : φ.right_deriv.comp g'inv = continuous_linear_map.id 𝕜 G) (hg'invf : φ.left_deriv.comp g'inv = 0) : has_strict_fderiv_at (φ.implicit_function (φ.left_fun φ.pt)) g'inv (φ.right_fun φ.pt) := begin have := φ.has_strict_fderiv_at.to_local_inverse, simp only [prod_fun] at this, convert this.comp (φ.right_fun φ.pt) ((has_strict_fderiv_at_const _ _).prod (has_strict_fderiv_at_id _)), simp only [continuous_linear_map.ext_iff, continuous_linear_map.coe_comp', function.comp_app] at hg'inv hg'invf ⊢, simp [continuous_linear_equiv.eq_symm_apply, *] end end implicit_function_data namespace has_strict_fderiv_at section complemented /-! ### Case of a complemented kernel In this section we prove the following version of the implicit function theorem. Consider a map `f : E → F` and a point `a : E` such that `f` is strictly differentiable at `a`, its derivative `f'` is surjective and the kernel of `f'` is a complemented subspace of `E` (i.e., it has a closed complementary subspace). Then there exists a function `φ : F → ker f' → E` such that for `(y, z)` close to `(f a, 0)` we have `f (φ y z) = y` and the derivative of `φ (f a)` at zero is the embedding `ker f' → E`. Note that a map with these properties is not unique. E.g., different choices of a subspace complementary to `ker f'` lead to different maps `φ`. -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type*} [normed_group F] [normed_space 𝕜 F] [complete_space F] {f : E → F} {f' : E →L[𝕜] F} {a : E} section defs variables (f f') /-- Data used to apply the generic implicit function theorem to the case of a strictly differentiable map such that its derivative is surjective and has a complemented kernel. -/ @[simp] def implicit_function_data_of_complemented (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) (hker : f'.ker.closed_complemented) : implicit_function_data 𝕜 E F f'.ker := { left_fun := f, left_deriv := f', right_fun := λ x, classical.some hker (x - a), right_deriv := classical.some hker, pt := a, left_has_deriv := hf, right_has_deriv := (classical.some hker).has_strict_fderiv_at.comp a ((has_strict_fderiv_at_id a).sub_const a), left_range := hf', right_range := linear_map.range_eq_of_proj (classical.some_spec hker), is_compl_ker := linear_map.is_compl_of_proj (classical.some_spec hker) } /-- A local homeomorphism between `E` and `F × f'.ker` sending level surfaces of `f` to vertical subspaces. -/ def implicit_to_local_homeomorph_of_complemented (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) (hker : f'.ker.closed_complemented) : local_homeomorph E (F × f'.ker) := (implicit_function_data_of_complemented f f' hf hf' hker).to_local_homeomorph /-- Implicit function `g` defined by `f (g z y) = z`. -/ def implicit_function_of_complemented (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) (hker : f'.ker.closed_complemented) : F → f'.ker → E := (implicit_function_data_of_complemented f f' hf hf' hker).implicit_function end defs @[simp] lemma implicit_to_local_homeomorph_of_complemented_fst (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) (hker : f'.ker.closed_complemented) (x : E) : (hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker x).fst = f x := rfl lemma implicit_to_local_homeomorph_of_complemented_apply (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) (hker : f'.ker.closed_complemented) (y : E) : hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker y = (f y, classical.some hker (y - a)) := rfl @[simp] lemma implicit_to_local_homeomorph_of_complemented_apply_ker (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) (hker : f'.ker.closed_complemented) (y : f'.ker) : hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker (y + a) = (f (y + a), y) := by simp only [implicit_to_local_homeomorph_of_complemented_apply, add_sub_cancel, classical.some_spec hker] @[simp] lemma implicit_to_local_homeomorph_of_complemented_self (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) (hker : f'.ker.closed_complemented) : hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker a = (f a, 0) := by simp [hf.implicit_to_local_homeomorph_of_complemented_apply] lemma mem_implicit_to_local_homeomorph_of_complemented_source (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) (hker : f'.ker.closed_complemented) : a ∈ (hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker).source := mem_to_local_homeomorph_source _ lemma mem_implicit_to_local_homeomorph_of_complemented_target (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) (hker : f'.ker.closed_complemented) : (f a, (0 : f'.ker)) ∈ (hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker).target := by simpa only [implicit_to_local_homeomorph_of_complemented_self] using ((hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker).map_source $ (hf.mem_implicit_to_local_homeomorph_of_complemented_source hf' hker)) /-- `implicit_function_of_complemented` sends `(z, y)` to a point in `f ⁻¹' z`. -/ lemma map_implicit_function_of_complemented_eq (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) (hker : f'.ker.closed_complemented) : ∀ᶠ (p : F × f'.ker) in 𝓝 (f a, 0), f (hf.implicit_function_of_complemented f f' hf' hker p.1 p.2) = p.1 := ((hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker).eventually_right_inverse $ hf.mem_implicit_to_local_homeomorph_of_complemented_target hf' hker).mono $ λ ⟨z, y⟩ h, congr_arg prod.fst h /-- Any point in some neighborhood of `a` can be represented as `implicit_function` of some point. -/ lemma eq_implicit_function_of_complemented (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) (hker : f'.ker.closed_complemented) : ∀ᶠ x in 𝓝 a, hf.implicit_function_of_complemented f f' hf' hker (f x) (hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker x).snd = x := (implicit_function_data_of_complemented f f' hf hf' hker).implicit_function_apply_image @[simp] lemma implicit_function_of_complemented_apply_image (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) (hker : f'.ker.closed_complemented) : hf.implicit_function_of_complemented f f' hf' hker (f a) 0 = a := begin convert (hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker).left_inv (hf.mem_implicit_to_local_homeomorph_of_complemented_source hf' hker), exact congr_arg prod.snd (hf.implicit_to_local_homeomorph_of_complemented_self hf' hker).symm end lemma to_implicit_function_of_complemented (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) (hker : f'.ker.closed_complemented) : has_strict_fderiv_at (hf.implicit_function_of_complemented f f' hf' hker (f a)) (subtype_val f'.ker) 0 := by convert (implicit_function_data_of_complemented f f' hf hf' hker).implicit_function_has_strict_fderiv_at (subtype_val f'.ker) _ _; [skip, ext, ext]; simp [classical.some_spec hker] end complemented /-! ### Finite dimensional case In this section we prove the following version of the implicit function theorem. Consider a map `f : E → F` from a Banach normed space to a finite dimensional space. Take a point `a : E` such that `f` is strictly differentiable at `a` and its derivative `f'` is surjective. Then there exists a function `φ : F → ker f' → E` such that for `(y, z)` close to `(f a, 0)` we have `f (φ y z) = y` and the derivative of `φ (f a)` at zero is the embedding `ker f' → E`. This version deduces that `ker f'` is a complemented subspace from the fact that `F` is a finite dimensional space, then applies the previous version. Note that a map with these properties is not unique. E.g., different choices of a subspace complementary to `ker f'` lead to different maps `φ`. -/ section finite_dimensional variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type*} [normed_group F] [normed_space 𝕜 F] [finite_dimensional 𝕜 F] (f : E → F) (f' : E →L[𝕜] F) {a : E} /-- Given a map `f : E → F` to a finite dimensional space with a surjective derivative `f'`, returns a local homeomorphism between `E` and `F × ker f'`. -/ def implicit_to_local_homeomorph (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) : local_homeomorph E (F × f'.ker) := by haveI := finite_dimensional.complete 𝕜 F; exact hf.implicit_to_local_homeomorph_of_complemented f f' hf' f'.ker_closed_complemented_of_finite_dimensional_range /-- Implicit function `g` defined by `f (g z y) = z`. -/ def implicit_function (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) : F → f'.ker → E := function.curry $ (hf.implicit_to_local_homeomorph f f' hf').symm variables {f f'} @[simp] lemma implicit_to_local_homeomorph_fst (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) (x : E) : (hf.implicit_to_local_homeomorph f f' hf' x).fst = f x := rfl @[simp] lemma implicit_to_local_homeomorph_apply_ker (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) (y : f'.ker) : hf.implicit_to_local_homeomorph f f' hf' (y + a) = (f (y + a), y) := by apply implicit_to_local_homeomorph_of_complemented_apply_ker @[simp] lemma implicit_to_local_homeomorph_self (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) : hf.implicit_to_local_homeomorph f f' hf' a = (f a, 0) := by apply implicit_to_local_homeomorph_of_complemented_self lemma mem_implicit_to_local_homeomorph_source (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) : a ∈ (hf.implicit_to_local_homeomorph f f' hf').source := mem_to_local_homeomorph_source _ lemma mem_implicit_to_local_homeomorph_target (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) : (f a, (0 : f'.ker)) ∈ (hf.implicit_to_local_homeomorph f f' hf').target := by apply mem_implicit_to_local_homeomorph_of_complemented_target lemma tendsto_implicit_function (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) {α : Type*} {l : filter α} {g₁ : α → F} {g₂ : α → f'.ker} (h₁ : tendsto g₁ l (𝓝 $ f a)) (h₂ : tendsto g₂ l (𝓝 0)) : tendsto (λ t, hf.implicit_function f f' hf' (g₁ t) (g₂ t)) l (𝓝 a) := begin refine ((hf.implicit_to_local_homeomorph f f' hf').tendsto_symm (hf.mem_implicit_to_local_homeomorph_source hf')).comp _, rw [implicit_to_local_homeomorph_self], exact h₁.prod_mk_nhds h₂ end alias tendsto_implicit_function ← _root_.filter.tendsto.implicit_function /-- `implicit_function` sends `(z, y)` to a point in `f ⁻¹' z`. -/ lemma map_implicit_function_eq (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) : ∀ᶠ (p : F × f'.ker) in 𝓝 (f a, 0), f (hf.implicit_function f f' hf' p.1 p.2) = p.1 := by apply map_implicit_function_of_complemented_eq @[simp] lemma implicit_function_apply_image (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) : hf.implicit_function f f' hf' (f a) 0 = a := by apply implicit_function_of_complemented_apply_image /-- Any point in some neighborhood of `a` can be represented as `implicit_function` of some point. -/ lemma eq_implicit_function (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) : ∀ᶠ x in 𝓝 a, hf.implicit_function f f' hf' (f x) (hf.implicit_to_local_homeomorph f f' hf' x).snd = x := by apply eq_implicit_function_of_complemented lemma to_implicit_function (hf : has_strict_fderiv_at f f' a) (hf' : f'.range = ⊤) : has_strict_fderiv_at (hf.implicit_function f f' hf' (f a)) (subtype_val f'.ker) 0 := by apply to_implicit_function_of_complemented end finite_dimensional end has_strict_fderiv_at
3c7f9a6d24b55322ec7a25d31ffee5e46a49e2e5
9028d228ac200bbefe3a711342514dd4e4458bff
/src/category_theory/limits/shapes/terminal.lean
222fd218aae03742cd433fc1d9ec2b602bacb3d9
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,079
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Bhavik Mehta -/ import category_theory.pempty import category_theory.limits.limits /-! # Initial and terminal objects in a category. ## References * [Stacks: Initial and final objects](https://stacks.math.columbia.edu/tag/002B) -/ noncomputable theory universes v u open category_theory namespace category_theory.limits variables {C : Type u} [category.{v} C] /-- Construct a cone for the empty diagram given an object. -/ @[simps] def as_empty_cone (X : C) : cone (functor.empty C) := { X := X, π := by tidy } /-- Construct a cocone for the empty diagram given an object. -/ @[simps] def as_empty_cocone (X : C) : cocone (functor.empty C) := { X := X, ι := by tidy } /-- `X` is terminal if the cone it induces on the empty diagram is limiting. -/ abbreviation is_terminal (X : C) := is_limit (as_empty_cone X) /-- `X` is initial if the cocone it induces on the empty diagram is colimiting. -/ abbreviation is_initial (X : C) := is_colimit (as_empty_cocone X) /-- Give the morphism to a terminal object from any other. -/ def is_terminal.from {X : C} (t : is_terminal X) (Y : C) : Y ⟶ X := t.lift (as_empty_cone Y) /-- Any two morphisms to a terminal object are equal. -/ lemma is_terminal.hom_ext {X Y : C} (t : is_terminal X) (f g : Y ⟶ X) : f = g := t.hom_ext (by tidy) /-- Give the morphism from an initial object to any other. -/ def is_initial.to {X : C} (t : is_initial X) (Y : C) : X ⟶ Y := t.desc (as_empty_cocone Y) /-- Any two morphisms from an initial object are equal. -/ lemma is_initial.hom_ext {X Y : C} (t : is_initial X) (f g : X ⟶ Y) : f = g := t.hom_ext (by tidy) /-- Any morphism from a terminal object is mono. -/ lemma is_terminal.mono_from {X Y : C} (t : is_terminal X) (f : X ⟶ Y) : mono f := ⟨λ Z g h eq, t.hom_ext _ _⟩ /-- Any morphism to an initial object is epi. -/ lemma is_initial.epi_to {X Y : C} (t : is_initial X) (f : Y ⟶ X) : epi f := ⟨λ Z g h eq, t.hom_ext _ _⟩ variable (C) /-- A category has a terminal object if it has a limit over the empty diagram. Use `has_terminal_of_unique` to construct instances. -/ abbreviation has_terminal := has_limits_of_shape (discrete pempty) C /-- A category has an initial object if it has a colimit over the empty diagram. Use `has_initial_of_unique` to construct instances. -/ abbreviation has_initial := has_colimits_of_shape (discrete pempty) C /-- An arbitrary choice of terminal object, if one exists. You can use the notation `⊤_ C`. This object is characterized by having a unique morphism from any object. -/ abbreviation terminal [has_terminal C] : C := limit (functor.empty C) /-- An arbitrary choice of initial object, if one exists. You can use the notation `⊥_ C`. This object is characterized by having a unique morphism to any object. -/ abbreviation initial [has_initial C] : C := colimit (functor.empty C) notation `⊤_` C:20 := terminal C notation `⊥_` C:20 := initial C section variables {C} /-- We can more explicitly show that a category has a terminal object by specifying the object, and showing there is a unique morphism to it from any other object. -/ lemma has_terminal_of_unique (X : C) [h : Π Y : C, unique (Y ⟶ X)] : has_terminal C := { has_limit := λ F, has_limit.mk { cone := { X := X, π := { app := pempty.rec _ } }, is_limit := { lift := λ s, (h s.X).default } } } /-- We can more explicitly show that a category has an initial object by specifying the object, and showing there is a unique morphism from it to any other object. -/ lemma has_initial_of_unique (X : C) [h : Π Y : C, unique (X ⟶ Y)] : has_initial C := { has_colimit := λ F, has_colimit.mk { cocone := { X := X, ι := { app := pempty.rec _ } }, is_colimit := { desc := λ s, (h s.X).default } } } /-- The map from an object to the terminal object. -/ abbreviation terminal.from [has_terminal C] (P : C) : P ⟶ ⊤_ C := limit.lift (functor.empty C) (as_empty_cone P) /-- The map to an object from the initial object. -/ abbreviation initial.to [has_initial C] (P : C) : ⊥_ C ⟶ P := colimit.desc (functor.empty C) (as_empty_cocone P) instance unique_to_terminal [has_terminal C] (P : C) : unique (P ⟶ ⊤_ C) := { default := terminal.from P, uniq := λ m, by { apply limit.hom_ext, rintro ⟨⟩ } } instance unique_from_initial [has_initial C] (P : C) : unique (⊥_ C ⟶ P) := { default := initial.to P, uniq := λ m, by { apply colimit.hom_ext, rintro ⟨⟩ } } /-- A terminal object is terminal. -/ def terminal_is_terminal [has_terminal C] : is_terminal (⊤_ C) := { lift := λ s, terminal.from _ } /-- An initial object is initial. -/ def initial_is_initial [has_initial C] : is_initial (⊥_ C) := { desc := λ s, initial.to _ } /-- Any morphism from a terminal object is mono. -/ instance terminal.mono_from {Y : C} [has_terminal C] (f : ⊤_ C ⟶ Y) : mono f := is_terminal.mono_from terminal_is_terminal _ /-- Any morphism to an initial object is epi. -/ instance initial.epi_to {Y : C} [has_initial C] (f : Y ⟶ ⊥_ C) : epi f := is_initial.epi_to initial_is_initial _ /-- From a functor `F : J ⥤ C`, given an initial object of `J`, construct a cone for `J`. In `limit_of_diagram_initial` we show it is a limit cone. -/ @[simps] def cone_of_diagram_initial {J : Type v} [small_category J] {X : J} (tX : is_initial X) (F : J ⥤ C) : cone F := { X := F.obj X, π := { app := λ j, F.map (tX.to j), naturality' := λ j j' k, begin dsimp, rw [← F.map_comp, category.id_comp, tX.hom_ext (tX.to j ≫ k) (tX.to j')], end } } /-- From a functor `F : J ⥤ C`, given an initial object of `J`, show the cone `cone_of_diagram_initial` is a limit. -/ def limit_of_diagram_initial {J : Type v} [small_category J] {X : J} (tX : is_initial X) (F : J ⥤ C) : is_limit (cone_of_diagram_initial tX F) := { lift := λ s, s.π.app X, uniq' := λ s m w, begin rw [← w X, cone_of_diagram_initial_π_app, tX.hom_ext (tX.to X) (𝟙 _)], dsimp, simp -- See note [dsimp, simp] end} -- This is reducible to allow usage of lemmas about `cone_point_unique_up_to_iso`. /-- For a functor `F : J ⥤ C`, if `J` has an initial object then the image of it is isomorphic to the limit of `F`. -/ @[reducible] def limit_of_initial {J : Type v} [small_category J] (F : J ⥤ C) [has_initial J] [has_limit F] : limit F ≅ F.obj (⊥_ J) := is_limit.cone_point_unique_up_to_iso (limit.is_limit _) (limit_of_diagram_initial initial_is_initial F) /-- From a functor `F : J ⥤ C`, given a terminal object of `J`, construct a cocone for `J`. In `colimit_of_diagram_terminal` we show it is a colimit cocone. -/ @[simps] def cocone_of_diagram_terminal {J : Type v} [small_category J] {X : J} (tX : is_terminal X) (F : J ⥤ C) : cocone F := { X := F.obj X, ι := { app := λ j, F.map (tX.from j), naturality' := λ j j' k, begin dsimp, rw [← F.map_comp, category.comp_id, tX.hom_ext (k ≫ tX.from j') (tX.from j)], end } } /-- From a functor `F : J ⥤ C`, given a terminal object of `J`, show the cocone `cocone_of_diagram_terminal` is a colimit. -/ def colimit_of_diagram_terminal {J : Type v} [small_category J] {X : J} (tX : is_terminal X) (F : J ⥤ C) : is_colimit (cocone_of_diagram_terminal tX F) := { desc := λ s, s.ι.app X, uniq' := λ s m w, by { rw [← w X, cocone_of_diagram_terminal_ι_app, tX.hom_ext (tX.from X) (𝟙 _)], simp } } -- This is reducible to allow usage of lemmas about `cocone_point_unique_up_to_iso`. /-- For a functor `F : J ⥤ C`, if `J` has a terminal object then the image of it is isomorphic to the colimit of `F`. -/ @[reducible] def colimit_of_terminal {J : Type v} [small_category J] (F : J ⥤ C) [has_terminal J] [has_colimit F] : colimit F ≅ F.obj (⊤_ J) := is_colimit.cocone_point_unique_up_to_iso (colimit.is_colimit _) (colimit_of_diagram_terminal terminal_is_terminal F) end end category_theory.limits
de23f3d6260d66cc392148e01c77fe98a1ab9f2f
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/uni_var_bug.lean
38a0052c9ec658e4f54d7a52d64e5c72f9d483e4
[ "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
309
lean
import logic.eq section universe variable u variables {A B : Type.{u}} theorem foo (H : A = B) : A = B := H theorem bar {C D : Type} (H : C = D) : C = D := foo H end universe variable u variables {A B : Type.{u}} theorem foo2 (H : A = B) : A = B := H theorem bar2 {C D : Type} (H : C = D) : C = D := foo2 H
3c0b5e00cfde3aa9374f31373b9d46b2e2f21fcd
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/order/initial_seg.lean
8b0a9fdc8aa33e2c71448002bb8bbf0d06dee011
[ "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
18,110
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Floris van Doorn -/ import order.rel_iso.set import order.well_founded /-! # Initial and principal segments This file defines initial and principal segments. ## Main definitions * `initial_seg r s`: type of order embeddings of `r` into `s` for which the range is an initial segment (i.e., if `b` belongs to the range, then any `b' < b` also belongs to the range). It is denoted by `r ≼i s`. * `principal_seg r s`: Type of order embeddings of `r` into `s` for which the range is a principal segment, i.e., an interval of the form `(-∞, top)` for some element `top`. It is denoted by `r ≺i s`. ## Notations These notations belong to the `initial_seg` locale. * `r ≼i s`: the type of initial segment embeddings of `r` into `s`. * `r ≺i s`: the type of principal segment embeddings of `r` into `s`. -/ /-! ### Initial segments Order embeddings whose range is an initial segment of `s` (i.e., if `b` belongs to the range, then any `b' < b` also belongs to the range). The type of these embeddings from `r` to `s` is called `initial_seg r s`, and denoted by `r ≼i s`. -/ variables {α : Type*} {β : Type*} {γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} open function /-- If `r` is a relation on `α` and `s` in a relation on `β`, then `f : r ≼i s` is an order embedding whose range is an initial segment. That is, whenever `b < f a` in `β` then `b` is in the range of `f`. -/ structure initial_seg {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends r ↪r s := (init : ∀ a b, s b (to_rel_embedding a) → ∃ a', to_rel_embedding a' = b) localized "infix (name := initial_seg) ` ≼i `:25 := initial_seg" in initial_seg namespace initial_seg instance : has_coe (r ≼i s) (r ↪r s) := ⟨initial_seg.to_rel_embedding⟩ instance : has_coe_to_fun (r ≼i s) (λ _, α → β) := ⟨λ f x, (f : r ↪r s) x⟩ @[simp] theorem coe_fn_mk (f : r ↪r s) (o) : (@initial_seg.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_rel_embedding (f : r ≼i s) : (f.to_rel_embedding : α → β) = f := rfl @[simp] theorem coe_coe_fn (f : r ≼i s) : ((f : r ↪r s) : α → β) = f := rfl theorem init' (f : r ≼i s) {a : α} {b : β} : s b (f a) → ∃ a', f a' = b := f.init _ _ theorem init_iff (f : r ≼i s) {a : α} {b : β} : s b (f a) ↔ ∃ a', f a' = b ∧ r a' a := ⟨λ h, let ⟨a', e⟩ := f.init' h in ⟨a', e, (f : r ↪r s).map_rel_iff.1 (e.symm ▸ h)⟩, λ ⟨a', e, h⟩, e ▸ (f : r ↪r s).map_rel_iff.2 h⟩ /-- An order isomorphism is an initial segment -/ def of_iso (f : r ≃r s) : r ≼i s := ⟨f, λ a b h, ⟨f.symm b, rel_iso.apply_symm_apply f _⟩⟩ /-- The identity function shows that `≼i` is reflexive -/ @[refl] protected def refl (r : α → α → Prop) : r ≼i r := ⟨rel_embedding.refl _, λ a b h, ⟨_, rfl⟩⟩ instance (r : α → α → Prop) : inhabited (r ≼i r) := ⟨initial_seg.refl r⟩ /-- Composition of functions shows that `≼i` is transitive -/ @[trans] protected def trans (f : r ≼i s) (g : s ≼i t) : r ≼i t := ⟨f.1.trans g.1, λ a c h, begin simp at h ⊢, rcases g.2 _ _ h with ⟨b, rfl⟩, have h := g.1.map_rel_iff.1 h, rcases f.2 _ _ h with ⟨a', rfl⟩, exact ⟨a', rfl⟩ end⟩ @[simp] theorem refl_apply (x : α) : initial_seg.refl r x = x := rfl @[simp] theorem trans_apply (f : r ≼i s) (g : s ≼i t) (a : α) : (f.trans g) a = g (f a) := rfl theorem unique_of_trichotomous_of_irrefl [is_trichotomous β s] [is_irrefl β s] : well_founded r → subsingleton (r ≼i s) | ⟨h⟩ := ⟨λ f g, begin suffices : (f : α → β) = g, { cases f, cases g, congr, exact rel_embedding.coe_fn_injective this }, funext a, have := h a, induction this with a H IH, refine extensional_of_trichotomous_of_irrefl s (λ x, ⟨λ h, _, λ h, _⟩), { rcases f.init_iff.1 h with ⟨y, rfl, h'⟩, rw IH _ h', exact (g : r ↪r s).map_rel_iff.2 h' }, { rcases g.init_iff.1 h with ⟨y, rfl, h'⟩, rw ← IH _ h', exact (f : r ↪r s).map_rel_iff.2 h' } end⟩ instance [is_well_order β s] : subsingleton (r ≼i s) := ⟨λ a, @subsingleton.elim _ (unique_of_trichotomous_of_irrefl (@rel_embedding.well_founded _ _ r s a is_well_founded.wf)) a⟩ protected theorem eq [is_well_order β s] (f g : r ≼i s) (a) : f a = g a := by rw subsingleton.elim f g theorem antisymm.aux [is_well_order α r] (f : r ≼i s) (g : s ≼i r) : left_inverse g f := initial_seg.eq (f.trans g) (initial_seg.refl _) /-- If we have order embeddings between `α` and `β` whose images are initial segments, and `β` is a well-order then `α` and `β` are order-isomorphic. -/ def antisymm [is_well_order β s] (f : r ≼i s) (g : s ≼i r) : r ≃r s := by haveI := f.to_rel_embedding.is_well_order; exact ⟨⟨f, g, antisymm.aux f g, antisymm.aux g f⟩, λ _ _, f.map_rel_iff'⟩ @[simp] theorem antisymm_to_fun [is_well_order β s] (f : r ≼i s) (g : s ≼i r) : (antisymm f g : α → β) = f := rfl @[simp] theorem antisymm_symm [is_well_order α r] [is_well_order β s] (f : r ≼i s) (g : s ≼i r) : (antisymm f g).symm = antisymm g f := rel_iso.coe_fn_injective rfl theorem eq_or_principal [is_well_order β s] (f : r ≼i s) : surjective f ∨ ∃ b, ∀ x, s x b ↔ ∃ y, f y = x := or_iff_not_imp_right.2 $ λ h b, acc.rec_on (is_well_founded.wf.apply b : acc s b) $ λ x H IH, not_forall_not.1 $ λ hn, h ⟨x, λ y, ⟨(IH _), λ ⟨a, e⟩, by rw ← e; exact (trichotomous _ _).resolve_right (not_or (hn a) (λ hl, not_exists.2 hn (f.init' hl)))⟩⟩ /-- Restrict the codomain of an initial segment -/ def cod_restrict (p : set β) (f : r ≼i s) (H : ∀ a, f a ∈ p) : r ≼i subrel s p := ⟨rel_embedding.cod_restrict p f H, λ a ⟨b, m⟩ (h : s b (f a)), let ⟨a', e⟩ := f.init' h in ⟨a', by clear _let_match; subst e; refl⟩⟩ @[simp] theorem cod_restrict_apply (p) (f : r ≼i s) (H a) : cod_restrict p f H a = ⟨f a, H a⟩ := rfl /-- Initial segment from an empty type. -/ def of_is_empty (r : α → α → Prop) (s : β → β → Prop) [is_empty α] : r ≼i s := ⟨rel_embedding.of_is_empty r s, is_empty_elim⟩ /-- Initial segment embedding of an order `r` into the disjoint union of `r` and `s`. -/ def le_add (r : α → α → Prop) (s : β → β → Prop) : r ≼i sum.lex r s := ⟨⟨⟨sum.inl, λ _ _, sum.inl.inj⟩, λ a b, sum.lex_inl_inl⟩, λ a b, by cases b; [exact λ _, ⟨_, rfl⟩, exact false.elim ∘ sum.lex_inr_inl]⟩ @[simp] theorem le_add_apply (r : α → α → Prop) (s : β → β → Prop) (a) : le_add r s a = sum.inl a := rfl end initial_seg /-! ### Principal segments Order embeddings whose range is a principal segment of `s` (i.e., an interval of the form `(-∞, top)` for some element `top` of `β`). The type of these embeddings from `r` to `s` is called `principal_seg r s`, and denoted by `r ≺i s`. Principal segments are in particular initial segments. -/ /-- If `r` is a relation on `α` and `s` in a relation on `β`, then `f : r ≺i s` is an order embedding whose range is an open interval `(-∞, top)` for some element `top` of `β`. Such order embeddings are called principal segments -/ @[nolint has_nonempty_instance] structure principal_seg {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends r ↪r s := (top : β) (down' : ∀ b, s b top ↔ ∃ a, to_rel_embedding a = b) localized "infix (name := principal_seg) ` ≺i `:25 := principal_seg" in initial_seg namespace principal_seg instance : has_coe (r ≺i s) (r ↪r s) := ⟨principal_seg.to_rel_embedding⟩ instance : has_coe_to_fun (r ≺i s) (λ _, α → β) := ⟨λ f, f⟩ @[simp] theorem coe_fn_mk (f : r ↪r s) (t o) : (@principal_seg.mk _ _ r s f t o : α → β) = f := rfl @[simp] theorem coe_fn_to_rel_embedding (f : r ≺i s) : (f.to_rel_embedding : α → β) = f := rfl @[simp] theorem coe_coe_fn (f : r ≺i s) : ((f : r ↪r s) : α → β) = f := rfl theorem down (f : r ≺i s) : ∀ {b : β}, s b f.top ↔ ∃ a, f a = b := f.down' theorem lt_top (f : r ≺i s) (a : α) : s (f a) f.top := f.down.2 ⟨_, rfl⟩ theorem init [is_trans β s] (f : r ≺i s) {a : α} {b : β} (h : s b (f a)) : ∃ a', f a' = b := f.down.1 $ trans h $ f.lt_top _ /-- A principal segment is in particular an initial segment. -/ instance has_coe_initial_seg [is_trans β s] : has_coe (r ≺i s) (r ≼i s) := ⟨λ f, ⟨f.to_rel_embedding, λ a b, f.init⟩⟩ theorem coe_coe_fn' [is_trans β s] (f : r ≺i s) : ((f : r ≼i s) : α → β) = f := rfl theorem init_iff [is_trans β s] (f : r ≺i s) {a : α} {b : β} : s b (f a) ↔ ∃ a', f a' = b ∧ r a' a := @initial_seg.init_iff α β r s f a b theorem irrefl {r : α → α → Prop} [is_well_order α r] (f : r ≺i r) : false := begin have := f.lt_top f.top, rw [show f f.top = f.top, from initial_seg.eq ↑f (initial_seg.refl r) f.top] at this, exact irrefl _ this end instance (r : α → α → Prop) [is_well_order α r] : is_empty (r ≺i r) := ⟨λ f, f.irrefl⟩ /-- Composition of a principal segment with an initial segment, as a principal segment -/ def lt_le (f : r ≺i s) (g : s ≼i t) : r ≺i t := ⟨@rel_embedding.trans _ _ _ r s t f g, g f.top, λ a, by simp only [g.init_iff, f.down', exists_and_distrib_left.symm, exists_swap, rel_embedding.trans_apply, exists_eq_right']; refl⟩ @[simp] theorem lt_le_apply (f : r ≺i s) (g : s ≼i t) (a : α) : (f.lt_le g) a = g (f a) := rel_embedding.trans_apply _ _ _ @[simp] theorem lt_le_top (f : r ≺i s) (g : s ≼i t) : (f.lt_le g).top = g f.top := rfl /-- Composition of two principal segments as a principal segment -/ @[trans] protected def trans [is_trans γ t] (f : r ≺i s) (g : s ≺i t) : r ≺i t := lt_le f g @[simp] theorem trans_apply [is_trans γ t] (f : r ≺i s) (g : s ≺i t) (a : α) : (f.trans g) a = g (f a) := lt_le_apply _ _ _ @[simp] theorem trans_top [is_trans γ t] (f : r ≺i s) (g : s ≺i t) : (f.trans g).top = g f.top := rfl /-- Composition of an order isomorphism with a principal segment, as a principal segment -/ def equiv_lt (f : r ≃r s) (g : s ≺i t) : r ≺i t := ⟨@rel_embedding.trans _ _ _ r s t f g, g.top, λ c, suffices (∃ (a : β), g a = c) ↔ ∃ (a : α), g (f a) = c, by simpa [g.down], ⟨λ ⟨b, h⟩, ⟨f.symm b, by simp only [h, rel_iso.apply_symm_apply, rel_iso.coe_coe_fn]⟩, λ ⟨a, h⟩, ⟨f a, h⟩⟩⟩ /-- Composition of a principal segment with an order isomorphism, as a principal segment -/ def lt_equiv {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} (f : principal_seg r s) (g : s ≃r t) : principal_seg r t := ⟨@rel_embedding.trans _ _ _ r s t f g, g f.top, begin intro x, rw [← g.apply_symm_apply x, g.map_rel_iff, f.down', exists_congr], intro y, exact ⟨congr_arg g, λ h, g.to_equiv.bijective.1 h⟩ end⟩ @[simp] theorem equiv_lt_apply (f : r ≃r s) (g : s ≺i t) (a : α) : (equiv_lt f g) a = g (f a) := rel_embedding.trans_apply _ _ _ @[simp] theorem equiv_lt_top (f : r ≃r s) (g : s ≺i t) : (equiv_lt f g).top = g.top := rfl /-- Given a well order `s`, there is a most one principal segment embedding of `r` into `s`. -/ instance [is_well_order β s] : subsingleton (r ≺i s) := ⟨λ f g, begin have ef : (f : α → β) = g, { show ((f : r ≼i s) : α → β) = g, rw @subsingleton.elim _ _ (f : r ≼i s) g, refl }, have et : f.top = g.top, { refine extensional_of_trichotomous_of_irrefl s (λ x, _), simp only [f.down, g.down, ef, coe_fn_to_rel_embedding] }, cases f, cases g, have := rel_embedding.coe_fn_injective ef; congr' end⟩ theorem top_eq [is_well_order γ t] (e : r ≃r s) (f : r ≺i t) (g : s ≺i t) : f.top = g.top := by rw subsingleton.elim f (principal_seg.equiv_lt e g); refl lemma top_lt_top {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} [is_well_order γ t] (f : principal_seg r s) (g : principal_seg s t) (h : principal_seg r t) : t h.top g.top := by { rw [subsingleton.elim h (f.trans g)], apply principal_seg.lt_top } /-- Any element of a well order yields a principal segment -/ def of_element {α : Type*} (r : α → α → Prop) (a : α) : subrel r {b | r b a} ≺i r := ⟨subrel.rel_embedding _ _, a, λ b, ⟨λ h, ⟨⟨_, h⟩, rfl⟩, λ ⟨⟨_, h⟩, rfl⟩, h⟩⟩ @[simp] theorem of_element_apply {α : Type*} (r : α → α → Prop) (a : α) (b) : of_element r a b = b.1 := rfl @[simp] theorem of_element_top {α : Type*} (r : α → α → Prop) (a : α) : (of_element r a).top = a := rfl /-- Restrict the codomain of a principal segment -/ def cod_restrict (p : set β) (f : r ≺i s) (H : ∀ a, f a ∈ p) (H₂ : f.top ∈ p) : r ≺i subrel s p := ⟨rel_embedding.cod_restrict p f H, ⟨f.top, H₂⟩, λ ⟨b, h⟩, f.down.trans $ exists_congr $ λ a, show (⟨f a, H a⟩ : p).1 = _ ↔ _, from ⟨subtype.eq, congr_arg _⟩⟩ @[simp] theorem cod_restrict_apply (p) (f : r ≺i s) (H H₂ a) : cod_restrict p f H H₂ a = ⟨f a, H a⟩ := rfl @[simp] theorem cod_restrict_top (p) (f : r ≺i s) (H H₂) : (cod_restrict p f H H₂).top = ⟨f.top, H₂⟩ := rfl /-- Principal segment from an empty type into a type with a minimal element. -/ def of_is_empty (r : α → α → Prop) [is_empty α] {b : β} (H : ∀ b', ¬ s b' b) : r ≺i s := { top := b, down' := by simp [H], ..rel_embedding.of_is_empty r s } @[simp] theorem of_is_empty_top (r : α → α → Prop) [is_empty α] {b : β} (H : ∀ b', ¬ s b' b) : (of_is_empty r H).top = b := rfl /-- Principal segment from the empty relation on `pempty` to the empty relation on `punit`. -/ @[reducible] def pempty_to_punit : @empty_relation pempty ≺i @empty_relation punit := @of_is_empty _ _ empty_relation _ _ punit.star $ λ x, not_false end principal_seg /-! ### Properties of initial and principal segments -/ /-- To an initial segment taking values in a well order, one can associate either a principal segment (if the range is not everything, hence one can take as top the minimum of the complement of the range) or an order isomorphism (if the range is everything). -/ noncomputable def initial_seg.lt_or_eq [is_well_order β s] (f : r ≼i s) : (r ≺i s) ⊕ (r ≃r s) := begin by_cases h : surjective f, { exact sum.inr (rel_iso.of_surjective f h) }, { have h' : _, from (initial_seg.eq_or_principal f).resolve_left h, exact sum.inl ⟨f, classical.some h', classical.some_spec h'⟩ } end theorem initial_seg.lt_or_eq_apply_left [is_well_order β s] (f : r ≼i s) (g : r ≺i s) (a : α) : g a = f a := @initial_seg.eq α β r s _ g f a theorem initial_seg.lt_or_eq_apply_right [is_well_order β s] (f : r ≼i s) (g : r ≃r s) (a : α) : g a = f a := initial_seg.eq (initial_seg.of_iso g) f a /-- Composition of an initial segment taking values in a well order and a principal segment. -/ noncomputable def initial_seg.le_lt [is_well_order β s] [is_trans γ t] (f : r ≼i s) (g : s ≺i t) : r ≺i t := match f.lt_or_eq with | sum.inl f' := f'.trans g | sum.inr f' := principal_seg.equiv_lt f' g end @[simp] theorem initial_seg.le_lt_apply [is_well_order β s] [is_trans γ t] (f : r ≼i s) (g : s ≺i t) (a : α) : (f.le_lt g) a = g (f a) := begin delta initial_seg.le_lt, cases h : f.lt_or_eq with f' f', { simp only [principal_seg.trans_apply, f.lt_or_eq_apply_left] }, { simp only [principal_seg.equiv_lt_apply, f.lt_or_eq_apply_right] } end namespace rel_embedding /-- Given an order embedding into a well order, collapse the order embedding by filling the gaps, to obtain an initial segment. Here, we construct the collapsed order embedding pointwise, but the proof of the fact that it is an initial segment will be given in `collapse`. -/ noncomputable def collapse_F [is_well_order β s] (f : r ↪r s) : Π a, {b // ¬ s (f a) b} := (rel_embedding.well_founded f $ is_well_founded.wf).fix $ λ a IH, begin let S := {b | ∀ a h, s (IH a h).1 b}, have : f a ∈ S, from λ a' h, ((trichotomous _ _) .resolve_left $ λ h', (IH a' h).2 $ trans (f.map_rel_iff.2 h) h') .resolve_left $ λ h', (IH a' h).2 $ h' ▸ f.map_rel_iff.2 h, exact ⟨is_well_founded.wf.min S ⟨_, this⟩, is_well_founded.wf.not_lt_min _ _ this⟩ end theorem collapse_F.lt [is_well_order β s] (f : r ↪r s) {a : α} : ∀ {a'}, r a' a → s (collapse_F f a').1 (collapse_F f a).1 := show (collapse_F f a).1 ∈ {b | ∀ a' (h : r a' a), s (collapse_F f a').1 b}, begin unfold collapse_F, rw well_founded.fix_eq, apply well_founded.min_mem _ _ end theorem collapse_F.not_lt [is_well_order β s] (f : r ↪r s) (a : α) {b} (h : ∀ a' (h : r a' a), s (collapse_F f a').1 b) : ¬ s b (collapse_F f a).1 := begin unfold collapse_F, rw well_founded.fix_eq, exact well_founded.not_lt_min _ _ _ (show b ∈ {b | ∀ a' (h : r a' a), s (collapse_F f a').1 b}, from h) end /-- Construct an initial segment from an order embedding into a well order, by collapsing it to fill the gaps. -/ noncomputable def collapse [is_well_order β s] (f : r ↪r s) : r ≼i s := by haveI := rel_embedding.is_well_order f; exact ⟨rel_embedding.of_monotone (λ a, (collapse_F f a).1) (λ a b, collapse_F.lt f), λ a b, acc.rec_on (is_well_founded.wf.apply b : acc s b) (λ b H IH a h, begin let S := {a | ¬ s (collapse_F f a).1 b}, have : S.nonempty := ⟨_, asymm h⟩, existsi (is_well_founded.wf : well_founded r).min S this, refine ((@trichotomous _ s _ _ _).resolve_left _).resolve_right _, { exact (is_well_founded.wf : well_founded r).min_mem S this }, { refine collapse_F.not_lt f _ (λ a' h', _), by_contradiction hn, exact is_well_founded.wf.not_lt_min S this hn h' } end) a⟩ theorem collapse_apply [is_well_order β s] (f : r ↪r s) (a) : collapse f a = (collapse_F f a).1 := rfl end rel_embedding
d1a6bb2ab81d2b81a5635ebac413e0ea5e651927
fcf3ffa92a3847189ca669cb18b34ef6b2ec2859
/src/world3/level8.lean
e9d68c94e3c66d271e5655f9c536dc90f56cab45
[ "Apache-2.0" ]
permissive
nomoid/lean-proofs
4a80a97888699dee42b092b7b959b22d9aa0c066
b9f03a24623d1a1d111d6c2bbf53c617e2596d6a
refs/heads/master
1,674,955,317,080
1,607,475,706,000
1,607,475,706,000
314,104,281
0
0
null
null
null
null
UTF-8
Lean
false
false
364
lean
import mynat.definition import mynat.mul import world3.level1 import world3.level6 namespace mynat lemma mul_comm (a b : mynat) : a * b = b * a := begin [nat_num_game] induction b with d hd, { rw mul_zero, rw zero_mul, refl, }, { rw mul_succ, rw succ_mul, rw hd, refl, }, end end mynat
a4b4d2e4ec7e1700e6b953f9dadcb74e70ff7678
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/group_theory/group_action/big_operators.lean
3c799badb1f36162b02116fce9fcd19ab31581fb
[ "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,827
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import algebra.big_operators.basic import data.finset.basic import data.multiset.basic import group_theory.group_action.defs /-! # Lemmas about group actions on big operators > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Note that analogous lemmas for `module`s like `finset.sum_smul` appear in other files. -/ variables {α β γ : Type*} open_locale big_operators section variables [add_monoid β] [distrib_smul α β] lemma list.smul_sum {r : α} {l : list β} : r • l.sum = (l.map ((•) r)).sum := (distrib_smul.to_add_monoid_hom β r).map_list_sum l end section variables [monoid α] [monoid β] [mul_distrib_mul_action α β] lemma list.smul_prod {r : α} {l : list β} : r • l.prod = (l.map ((•) r)).prod := (mul_distrib_mul_action.to_monoid_hom β r).map_list_prod l end section variables [add_comm_monoid β] [distrib_smul α β] lemma multiset.smul_sum {r : α} {s : multiset β} : r • s.sum = (s.map ((•) r)).sum := (distrib_smul.to_add_monoid_hom β r).map_multiset_sum s lemma finset.smul_sum {r : α} {f : γ → β} {s : finset γ} : r • ∑ x in s, f x = ∑ x in s, r • f x := (distrib_smul.to_add_monoid_hom β r).map_sum f s end section variables [monoid α] [comm_monoid β] [mul_distrib_mul_action α β] lemma multiset.smul_prod {r : α} {s : multiset β} : r • s.prod = (s.map ((•) r)).prod := (mul_distrib_mul_action.to_monoid_hom β r).map_multiset_prod s lemma finset.smul_prod {r : α} {f : γ → β} {s : finset γ} : r • ∏ x in s, f x = ∏ x in s, r • f x := (mul_distrib_mul_action.to_monoid_hom β r).map_prod f s end
bcc493eba5b93f8f829b73b11a648c2b8e1fd007
47181b4ef986292573c77e09fcb116584d37ea8a
/src/valuations/basic.lean
56a0200a0c74b1c7e13fb558ca3a386b37e752e4
[ "MIT" ]
permissive
RaitoBezarius/berkovich-spaces
87662a2bdb0ac0beed26e3338b221e3f12107b78
0a49f75a599bcb20333ec86b301f84411f04f7cf
refs/heads/main
1,690,520,666,912
1,629,328,012,000
1,629,328,012,000
332,238,095
4
0
MIT
1,629,312,085,000
1,611,414,506,000
Lean
UTF-8
Lean
false
false
9,292
lean
import data.nat.enat import data.real.basic import data.real.cau_seq import data.nat.choose.basic import algebra.group_power.basic import ring_theory.ideal.basic import ring_theory.principal_ideal_domain import ring_theory.unique_factorization_domain import analysis.special_functions.pow import topology.basic import for_mathlib.associated import for_mathlib.multiplicity import for_mathlib.principal_ideal_domain open is_absolute_value open_locale classical big_operators noncomputable theory set_option old_structure_cmd true def padic_val {α} [integral_domain α] [is_principal_ideal_ring α] [normalization_monoid α] (p: α) [p_prime: fact (prime p)] (a: α): ℕ := if a ≠ 0 then multiset.count (normalize p) (unique_factorization_monoid.factors a) else 0 lemma padic_val_mul {α} [integral_domain α] [is_principal_ideal_ring α] [normalization_monoid α] (p: α) [p_prime: fact (prime p)] (a b: α) (ha: a ≠ 0) (hb: b ≠ 0): padic_val p (a * b) = padic_val p a + padic_val p b := begin unfold padic_val, simp [ha, hb], end lemma padic_val_add {α} [integral_domain α] [is_principal_ideal_ring α] [normalization_monoid α] (p: α) [p_prime: fact (prime p)] {a b: α} (add_ne_zero: a + b ≠ 0): padic_val p (a + b) ≥ min (padic_val p a) (padic_val p b) := begin unfold padic_val, exact if ha: a = 0 then by { simp [ha], } else if hb: b = 0 then by { simp [hb], } else by { have h: multiplicity p (a + b) ≥ min (multiplicity p a) (multiplicity p b), from multiplicity.min_le_multiplicity_add, have p₀ := λ a: α, λ p: a ≠ 0, unique_factorization_monoid.multiplicity_eq_count_factors (fact_iff.1 p_prime).irreducible p, rw p₀ _ add_ne_zero at h, rw p₀ _ ha at h, rw p₀ _ hb at h, finish, }, end lemma padic_val_primes {α} [integral_domain α] [is_principal_ideal_ring α] [normalization_monoid α] (p: α) [p_prime: fact (prime p)]: ∀ q: α, prime q → padic_val p q = if associated q p then 1 else 0 := begin intros q q_prime, unfold padic_val, simp only [q_prime.left, if_true, if_false, ne.def, not_false_iff], rw ← enat.coe_inj, rw eq.symm (unique_factorization_monoid.multiplicity_eq_count_factors (fact_iff.1 p_prime).irreducible q_prime.1), exact if hpq: associated q p then by simp [hpq, multiplicity.eq_of_associated hpq, multiplicity.multiplicity_self (fact_iff.1 p_prime).not_unit (fact_iff.1 p_prime).ne_zero] else by simp [hpq, multiplicity.multiplicity_eq_zero_of_not_dvd (λ h, hpq (primes_associated_of_dvd (fact_iff.1 p_prime) q_prime h).symm)] end lemma padic_val_units {α} [integral_domain α] [is_principal_ideal_ring α] [normalization_monoid α] (p: α) [p_prime: fact (prime p)]: ∀ u: units α, padic_val p u = 0 := begin intro u, unfold padic_val, simp only [u.ne_zero, if_true, ne.def, not_false_iff], rw ← enat.coe_inj, rw eq.symm (unique_factorization_monoid.multiplicity_eq_count_factors (fact_iff.1 p_prime).irreducible u.ne_zero), exact multiplicity.unit_right (fact_iff.1 p_prime).not_unit u, end def padic_abv {α} [integral_domain α] [is_principal_ideal_ring α] [normalization_monoid α] (base: ℝ) (base_pos: 0 < base) (base_lt_one: base < 1) (p: α) [p_prime: fact (prime p)]: α → ℝ := λ a: α, if a = 0 then 0 else base ^ padic_val p a instance padic_abv_is_absolute_value {α} [integral_domain α] [is_principal_ideal_ring α] [normalization_monoid α] (base: ℝ) (base_pos: 0 < base) (base_lt_one: base < 1) (p: α) [p_prime: fact (prime p)] : is_absolute_value (padic_abv base base_pos base_lt_one p) := { abv_nonneg := by { intro a, unfold padic_abv, split_ifs, refl, apply pow_nonneg, exact le_of_lt base_pos, }, abv_eq_zero := by { intro a, unfold padic_abv, split_ifs, tauto, finish [pow_ne_zero (padic_val p a) (ne.symm $ ne_of_lt base_pos)], }, abv_add := by { intros x y, unfold padic_abv, have pow_x_pos := pow_pos base_pos (padic_val p x), have pow_y_pos := pow_pos base_pos (padic_val p y), exact if hx: x = 0 then by { simp [hx], } else if hy: y = 0 then by { simp [hy], } else if hxy: x + y = 0 then by { simp [hx, hy, hxy], linarith only [pow_x_pos, pow_y_pos], } else by { suffices p₁: real.rpow base (padic_val p (x + y)) ≤ max (real.rpow base (padic_val p x)) (real.rpow base (padic_val p y)), { have p₂ := max_le_add_of_nonneg (le_of_lt pow_x_pos) (le_of_lt pow_y_pos), repeat { rw [real.rpow_eq_pow, real.rpow_nat_cast] at p₁, }, simp [hx, hy, hxy, le_trans p₁ p₂], }, set c := base⁻¹ with c_def, obtain ⟨ one_lt_c, base_rw, c_nonneg ⟩: 1 < c ∧ base = c⁻¹ ∧ 0 ≤ c, { rw c_def, rw inv_inv', exact ⟨ one_lt_inv base_pos base_lt_one, rfl, inv_nonneg.2 $ le_of_lt base_pos ⟩, }, have pow_c_mono: monotone (pow c), from λ a b h, real.rpow_le_rpow_of_exponent_le (le_of_lt one_lt_c) h, have p₀ := (int.neg_le_neg $ int.coe_nat_le.2 $ padic_val_add p hxy), repeat { rw base_rw, }, repeat { rw real.rpow_eq_pow, }, repeat { rw real.inv_rpow c_nonneg, }, repeat { rw ← real.rpow_neg c_nonneg, }, rw ← monotone.map_max pow_c_mono, rw ← ge_iff_le at p₀, rw max_neg_neg, rw ← monotone.map_min, { apply pow_c_mono, exact_mod_cast p₀, }, exact nat.mono_cast, }, }, abv_mul := by { intros x y, unfold padic_abv, exact if hx: x = 0 then by simp [hx] else if hy: y = 0 then by simp [hy] else by { simp [hx, hy, padic_val_mul p x y, pow_add _ _ _], }, }, } lemma padic_abv_primes {α} [integral_domain α] [is_principal_ideal_ring α] [normalization_monoid α] (base: ℝ) (base_pos: 0 < base) (base_lt_one: base < 1) (p: α) [p_prime: fact (prime p)]: ∀ q: α, prime q → padic_abv base base_pos base_lt_one p q = if associated q p then base else 1 := begin intros q q_prime, unfold padic_abv, rw padic_val_primes p q q_prime, simp [q_prime.1], end lemma padic_abv_bounded {α} [integral_domain α] [is_principal_ideal_ring α] [normalization_monoid α] (base: ℝ) (base_pos: 0 < base) (base_lt_one: base < 1) (p: α) [p_prime: fact (prime p)]: ∀ a: α, padic_abv base base_pos base_lt_one p a ≤ 1 := begin set abv := (padic_abv base base_pos base_lt_one p) with abv_def, intro a, refine wf_dvd_monoid.induction_on_irreducible a ((abv_zero abv).symm ▸ zero_le_one) _ _, { rintros _ ⟨ u, rfl ⟩, rw abv_def, unfold padic_abv, simp [padic_val_units p u], }, { intros a q ha hq abva_le_one, rw abv_mul abv, convert mul_le_mul _ abva_le_one (abv_nonneg abv a) (zero_le_one), rw one_mul, rw principal_ideal_ring.irreducible_iff_prime at hq, rw [abv_def, padic_abv_primes base base_pos base_lt_one p q hq], by_cases hpq: associated q p; simp [hpq, if_true, le_of_lt base_lt_one], }, end def sample_padic_abv {α} [integral_domain α] [is_principal_ideal_ring α] [normalization_monoid α] (p: α) [p_prime: fact (prime p)]: α → ℝ := padic_abv (1/2) one_half_pos one_half_lt_one p instance sample_padic_abv_is_absolute_value {α} [integral_domain α] [is_principal_ideal_ring α] [normalization_monoid α] (p: α) [p_prime: fact (prime p)]: is_absolute_value (sample_padic_abv p) := padic_abv_is_absolute_value (1/2) one_half_pos one_half_lt_one p lemma sample_padic_abv_on_primes {α} [integral_domain α] [is_principal_ideal_ring α] [normalization_monoid α] (p: α) [p_prime: fact (prime p)]: ∀ q: α, prime q → sample_padic_abv p q = if associated q p then 1/2 else 1 := padic_abv_primes (1/2) one_half_pos one_half_lt_one p lemma sample_padic_abv_bounded {α} [integral_domain α] [is_principal_ideal_ring α] [normalization_monoid α] (p: α) [p_prime: fact (prime p)]: ∀ a: α, sample_padic_abv p a ≤ 1 := padic_abv_bounded (1/2) one_half_pos one_half_lt_one p def hom_of_abv {α} [linear_ordered_field α] {β} [ring β] [nontrivial β] (abv: β → α) [is_absolute_value abv]: monoid_with_zero_hom β α := { to_fun := abv, map_zero' := abv_zero abv, map_one' := abv_one abv, map_mul' := abv_mul abv, } def hom_of_equiv {α} [comm_ring α] (φ: monoid_with_zero_hom α ℝ) (a: ℝ) (a_pos: 0 < a) (nonneg: ∀ a: α, 0 ≤ φ a): monoid_with_zero_hom α ℝ := { to_fun := (λ r, (φ r) ^ a), map_zero' := by { rw [← monoid_with_zero_hom.to_fun_eq_coe, φ.map_zero'], exact real.zero_rpow (ne.symm $ ne_of_lt a_pos), }, map_one' := by { rw [← monoid_with_zero_hom.to_fun_eq_coe, φ.map_one'], exact real.one_rpow a, }, map_mul' := by { intros x y, rw [← monoid_with_zero_hom.to_fun_eq_coe, φ.map_mul'], have p: ∀ a: α, 0 ≤ φ.to_fun a := nonneg, rw real.mul_rpow (p x) (p y), }, }
760790c4a3f9604ecdafd1eaca8f7e3f21e65d40
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/impByNameResolution.lean
04085509239cae87196d6422d17e83af2e11d8a3
[ "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
103
lean
-- namespace Foo def f (x : Nat) : Nat := x + 1 @[implemented_by f] opaque g : Nat → Nat end Foo
b55947a28bfb088f55458754bbd8c567f36bc109
dc253be9829b840f15d96d986e0c13520b085033
/spectrum/trunc.hlean
c58b3f92d6e2c1407cae95fe8d17f2d7220f8c8c
[ "Apache-2.0" ]
permissive
cmu-phil/Spectral
4ce68e5c1ef2a812ffda5260e9f09f41b85ae0ea
3b078f5f1de251637decf04bd3fc8aa01930a6b3
refs/heads/master
1,685,119,195,535
1,684,169,772,000
1,684,169,772,000
46,450,197
42
13
null
1,505,516,767,000
1,447,883,921,000
Lean
UTF-8
Lean
false
false
11,244
hlean
/- Copyright (c) 2017 Floris van Doorn and Ulrik Buchholtz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Ulrik Buchholtz Truncatedness and truncation of spectra -/ import .basic open int trunc eq is_trunc lift unit pointed equiv is_equiv algebra EM trunc_index namespace spectrum /- interactions of ptrunc / trunc with maxm2 -/ definition ptrunc_maxm2_change_int {k l : ℤ} (p : k = l) (X : Type*) : ptrunc (maxm2 k) X ≃* ptrunc (maxm2 l) X := ptrunc_change_index (ap maxm2 p) X definition is_trunc_maxm2_change_int {k l : ℤ} (X : pType) (p : k = l) : is_trunc (maxm2 k) X → is_trunc (maxm2 l) X := by induction p; exact id definition is_trunc_maxm2_loop {k : ℤ} {A : Type*} (H : is_trunc (maxm2 (k+1)) A) : is_trunc (maxm2 k) (Ω A) := begin induction k with k k, apply is_trunc_loop, exact H, apply is_contr_loop, cases k with k, { exact H }, { apply is_trunc_succ, apply is_trunc_succ, exact H } end definition loop_ptrunc_maxm2_pequiv {k : ℤ} {l : ℕ₋₂} (p : maxm2 (k+1) = l) (X : Type*) : Ω (ptrunc l X) ≃* ptrunc (maxm2 k) (Ω X) := begin induction p, induction k with k k, { exact loop_ptrunc_pequiv k X }, { refine pequiv_of_is_contr _ _ _ !is_trunc_trunc, apply is_contr_loop, cases k with k, { change is_set (trunc 0 X), apply _ }, { change is_set (trunc -2 X), apply _ }} end definition loop_ptrunc_maxm2_pequiv_ptrunc_elim' {k : ℤ} {l : ℕ₋₂} (p : maxm2 (k+1) = l) {A B : Type*} (f : A →* B) {H : is_trunc l B} : Ω→ (ptrunc.elim l f) ∘* (loop_ptrunc_maxm2_pequiv p A)⁻¹ᵉ* ~* @ptrunc.elim (maxm2 k) _ _ (is_trunc_maxm2_loop (is_trunc_of_eq p⁻¹ H)) (Ω→ f) := begin induction p, induction k with k k, { refine pwhisker_right _ (ap1_phomotopy _) ⬝* @(ap1_ptrunc_elim k f) H, apply ptrunc_elim_phomotopy2, reflexivity }, { apply phomotopy_of_is_contr_cod_pmap, exact is_trunc_maxm2_loop H } end definition loop_ptrunc_maxm2_pequiv_ptrunc_elim {k : ℤ} {l : ℕ₋₂} (p : maxm2 (k+1) = l) {A B : Type*} (f : A →* B) {H1 : is_trunc ((maxm2 k).+1) B } {H2 : is_trunc l B} : Ω→ (ptrunc.elim l f) ∘* (loop_ptrunc_maxm2_pequiv p A)⁻¹ᵉ* ~* ptrunc.elim (maxm2 k) (Ω→ f) := begin induction p, induction k with k k: esimp at H1, { refine pwhisker_right _ (ap1_phomotopy _) ⬝* ap1_ptrunc_elim k f, apply ptrunc_elim_phomotopy2, reflexivity }, { apply phomotopy_of_is_contr_cod } end definition loop_ptrunc_maxm2_pequiv_ptr {k : ℤ} {l : ℕ₋₂} (p : maxm2 (k+1) = l) (A : Type*) : Ω→ (ptr l A) ~* (loop_ptrunc_maxm2_pequiv p A)⁻¹ᵉ* ∘* ptr (maxm2 k) (Ω A) := begin induction p, induction k with k k, { exact ap1_ptr k A }, { apply phomotopy_pinv_left_of_phomotopy, apply phomotopy_of_is_contr_cod_pmap, apply is_trunc_trunc } end definition is_trunc_of_is_trunc_maxm2 (k : ℤ) (X : Type) : is_trunc (maxm2 k) X → is_trunc (max0 k) X := λ H, @is_trunc_of_le X _ _ (maxm2_le_maxm0 k) H definition ptrunc_maxm2_pred {n m : ℤ} (A : Type*) (p : n - 1 = m) : ptrunc (maxm2 m) A ≃* ptrunc (trunc_index.pred (maxm2 n)) A := begin cases n with n, cases n with n, apply pequiv_of_is_contr, induction p, apply is_trunc_trunc, apply is_contr_ptrunc_minus_one, exact ptrunc_change_index (ap maxm2 (p⁻¹ ⬝ !add_sub_cancel)) A, exact ptrunc_change_index (ap maxm2 p⁻¹) A end definition ptrunc_maxm2_pred_nat {n : ℕ} {m l : ℤ} (A : Type*) (p : nat.succ n = l) (q : pred l = m) (r : maxm2 m = trunc_index.pred (maxm2 (nat.succ n))) : @ptrunc_maxm2_pred (nat.succ n) m A (ap pred p ⬝ q) ~* ptrunc_change_index r A := begin have ap maxm2 ((ap pred p ⬝ q)⁻¹ ⬝ add_sub_cancel n 1) = r, from !is_set.elim, induction this, reflexivity end /- truncatedness of spectra -/ definition is_strunc [reducible] (k : ℤ) (E : spectrum) : Type := Π (n : ℤ), is_trunc (maxm2 (k + n)) (E n) definition is_strunc_change_int {k l : ℤ} (E : spectrum) (p : k = l) : is_strunc k E → is_strunc l E := begin induction p, exact id end definition is_strunc_of_le {k l : ℤ} (E : spectrum) (H : k ≤ l) : is_strunc k E → is_strunc l E := begin intro T, intro n, exact is_trunc_of_le (E n) (maxm2_monotone (algebra.add_le_add_right H n)) _ end definition is_strunc_pequiv_closed {k : ℤ} {E F : spectrum} (H : Πn, E n ≃* F n) (H2 : is_strunc k E) : is_strunc k F := λn, is_trunc_equiv_closed (maxm2 (k + n)) (H n) _ definition is_strunc_sunit (n : ℤ) : is_strunc n sunit := begin intro k, apply is_trunc_lift, apply is_trunc_unit end definition is_contr_of_is_strunc (n : ℤ) {m : ℤ} (E : spectrum) (H : is_strunc n E) (Hmn : m < -n) : is_contr (E m) := begin refine transport (λn, is_trunc n (E m)) _ (H m), apply maxm2_eq_minus_two, exact lt_of_lt_of_le (add_lt_add_left Hmn n) (le_of_eq !add.right_inv) end open option definition is_strunc_add_point_spectrum {X : Type} {Y : X → spectrum} {s₀ : ℤ} (H : Πx, is_strunc s₀ (Y x)) : Π(x : X₊), is_strunc s₀ (add_point_spectrum Y x) | (some x) := proof H x qed | none := begin intro k, apply is_trunc_lift, apply is_trunc_unit end definition is_strunc_EM_spectrum (G : AbGroup) : is_strunc 0 (EM_spectrum G) := begin intro n, induction n with n n, { -- case ≥ 0 apply is_trunc_maxm2_change_int (EM G n) (zero_add n)⁻¹, apply is_trunc_EM }, { change is_contr (EM_spectrum G (-[1+n])), induction n with n IH, { -- case = -1 apply is_contr_loop, exact is_trunc_EM G 0 }, { -- case < -1 apply is_trunc_loop, apply is_trunc_succ, exact IH }} end definition trivial_shomotopy_group_of_is_strunc (E : spectrum) {n k : ℤ} (K : is_strunc n E) (H : n < k) : is_contr (πₛ[k] E) := let m := n + (2 - k) in have I : m < 2, from calc m = (2 - k) + n : int.add_comm n (2 - k) ... < (2 - k) + k : add_lt_add_left H (2 - k) ... = 2 : sub_add_cancel 2 k, @trivial_homotopy_group_of_is_trunc (E (2 - k)) (max0 m) 2 (is_trunc_of_is_trunc_maxm2 m (E (2 - k)) (K (2 - k))) (nat.succ_le_succ (max0_le_of_le (le_sub_one_of_lt I))) /- truncation of spectra -/ definition strunc [constructor] (k : ℤ) (E : spectrum) : spectrum := spectrum.MK (λ(n : ℤ), ptrunc (maxm2 (k + n)) (E n)) (λ(n : ℤ), ptrunc_pequiv_ptrunc (maxm2 (k + n)) (equiv_glue E n) ⬝e* (loop_ptrunc_maxm2_pequiv (ap maxm2 (add.assoc k n 1)) (E (n+1)))⁻¹ᵉ*) definition strunc_change_int [constructor] {k l : ℤ} (E : spectrum) (p : k = l) : strunc k E →ₛ strunc l E := begin induction p, reflexivity end definition is_strunc_strunc (k : ℤ) (E : spectrum) : is_strunc k (strunc k E) := λ n, is_trunc_trunc (maxm2 (k + n)) (E n) definition is_strunc_strunc_pred (X : spectrum) (k : ℤ) : is_strunc k (strunc (k - 1) X) := λn, @(is_trunc_of_le _ (maxm2_monotone (add_le_add_right (sub_one_le k) n))) !is_strunc_strunc definition is_strunc_strunc_of_is_strunc (k : ℤ) {l : ℤ} {E : spectrum} (H : is_strunc l E) : is_strunc l (strunc k E) := λ n, !is_trunc_trunc_of_is_trunc definition str [constructor] (k : ℤ) (E : spectrum) : E →ₛ strunc k E := smap.mk (λ n, ptr (maxm2 (k + n)) (E n)) abstract begin intro n, apply psquare_of_phomotopy, refine !passoc ⬝* pwhisker_left _ !ptr_natural ⬝* _, refine !passoc⁻¹* ⬝* pwhisker_right _ !loop_ptrunc_maxm2_pequiv_ptr⁻¹*, end end definition strunc_elim [constructor] {k : ℤ} {E F : spectrum} (f : E →ₛ F) (H : is_strunc k F) : strunc k E →ₛ F := smap.mk (λn, ptrunc.elim (maxm2 (k + n)) (f n)) abstract begin intro n, apply psquare_of_phomotopy, symmetry, refine !passoc⁻¹* ⬝* pwhisker_right _ !loop_ptrunc_maxm2_pequiv_ptrunc_elim' ⬝* _, refine @(ptrunc_elim_ptrunc_functor _ _ _) _ ⬝* _, refine _ ⬝* @(ptrunc_elim_pcompose _ _ _) _ _, apply is_trunc_maxm2_loop, refine is_trunc_of_eq _ (H (n+1)), exact ap maxm2 (add.assoc k n 1)⁻¹, apply ptrunc_elim_phomotopy2, apply phomotopy_of_psquare, apply ptranspose, apply smap.glue_square end end definition strunc_functor [constructor] (k : ℤ) {E F : spectrum} (f : E →ₛ F) : strunc k E →ₛ strunc k F := strunc_elim (str k F ∘ₛ f) (is_strunc_strunc k F) /- truncated spectra -/ structure truncspectrum (n : ℤ) := (carrier : spectrum) (struct : is_strunc n carrier) notation n `-spectrum` := truncspectrum n attribute truncspectrum.carrier [coercion] definition genspectrum_of_truncspectrum [coercion] (n : ℤ) : n-spectrum → gen_spectrum +ℤ := λ E, truncspectrum.carrier E /- Comment (by Floris): I think we should really not bundle truncated spectra up, unless we are interested in the type of truncated spectra (e.g. when proving n-spectrum ≃ ...). Properties of truncated a spectrum should just be stated with two assumptions (X : spectrum) (H : is_strunc n X) -/ /- truncatedness of spi and sp_cotensor assuming the domain has a level of connectedness -/ section open is_conn definition is_conn_maxm1_of_maxm2 (A : Type*) (n : ℤ) : is_conn (maxm2 n) A → is_conn (maxm1m1 n).+1 A := begin intro H, induction n with n n, { exact H }, { exact is_conn_minus_one A (tr pt) } end definition is_trunc_maxm2_of_maxm1 (A : Type*) (n : ℤ) : is_trunc (maxm1m1 n).+1 A → is_trunc (maxm2 n) A := begin intro H, induction n with n n, { exact H}, { apply is_contr_of_merely_prop, { exact H }, { exact tr pt } } end variables (A : Type*) (n : ℤ) [H : is_conn (maxm2 n) A] include H definition is_trunc_maxm2_ppi (k l : ℤ) (H3 : l ≤ n+1+k) (P : A → Type*) (H2 : Πa, is_trunc (maxm2 l) (P a)) : is_trunc (maxm2 k) (Π*(a : A), P a) := is_trunc_maxm2_of_maxm1 (Π*(a : A), P a) k (@is_trunc_ppi_of_is_conn A (maxm1m1 n) (is_conn_maxm1_of_maxm2 A n H) (maxm1m1 k) _ (le.trans (maxm2_monotone H3) (maxm2_le n k)) P H2) definition is_strunc_spi_of_is_conn (k l : ℤ) (H3 : l ≤ n+1+k) (P : A → spectrum) (H2 : Πa, is_strunc l (P a)) : is_strunc k (spi A P) := begin intro m, unfold spi, exact is_trunc_maxm2_ppi A n (k+m) _ (le.trans (add_le_add_right H3 _) (le_of_eq (add.assoc (n+1) k m))) (λ a, P a m) (λa, H2 a m) end end definition is_strunc_spi_of_le {A : Type*} (k n : ℤ) (H : n ≤ k) (P : A → spectrum) (H2 : Πa, is_strunc n (P a)) : is_strunc k (spi A P) := begin assert K : n ≤ -[1+ 0] + 1 + k, { krewrite (int.zero_add k), exact H }, { exact @is_strunc_spi_of_is_conn A (-[1+ 0]) (is_conn.is_conn_minus_two A) k _ K P H2 } end definition is_strunc_spi {A : Type*} (n : ℤ) (P : A → spectrum) (H : Πa, is_strunc n (P a)) : is_strunc n (spi A P) := is_strunc_spi_of_le n n !le.refl P H definition is_strunc_sp_cotensor (n : ℤ) (A : Type*) {Y : spectrum} (H : is_strunc n Y) : is_strunc n (sp_cotensor A Y) := is_strunc_pequiv_closed (λn, !pppi_pequiv_ppmap) (is_strunc_spi n (λa, Y) (λa, H)) definition is_strunc_sp_ucotensor (n : ℤ) (A : Type) {Y : spectrum} (H : is_strunc n Y) : is_strunc n (sp_ucotensor A Y) := λk, !pi.is_trunc_arrow end spectrum
e597cfbe2db3026b21fa5f86cb8227c9e5a2e038
6df8d5ae3acf20ad0d7f0247d2cee1957ef96df1
/notes/ConceptsSoFar(9.17.19).lean
a3fe2cd0e1b088c575c4d309242dc5962779dbe0
[]
no_license
derekjohnsonva/CS2102
8ed45daa6658e6121bac0f6691eac6147d08246d
b3f507d4be824a2511838a1054d04fc9aef3304c
refs/heads/master
1,648,529,162,527
1,578,851,859,000
1,578,851,859,000
233,433,207
0
0
null
null
null
null
UTF-8
Lean
false
false
525
lean
/- Concepts So Far Terms Literal Data: Nat, String, Bool Function: Lambda abstraction Identifier: name for a value Application: ? Values: The evaluation of a term creates a value Definitions: binding of an identifier to a set of terms. Evaluations Types Values have types Terms have the types of the values to which they reduce Types we have Seen Data Types: nat, string, bool Function Types: lots -/
02e3aea9aa060aee0ec0746ab07af20d9ad7111f
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/469.lean
00610e8d7612939766d941693900408dd63abcda
[ "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
112
lean
notation "(+)" => HAdd.hAdd #check ((+) : Nat -> Nat -> Nat) #check ((+) 2 : Nat -> Nat) #check ((+) 2 3 : Nat)
e691a8b163299f4d2f25f0ea1de4ee3de30a4b1c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/sigma/basic_auto.lean
d3fb4e7d7b639ffbe97e926fb021af0f2f57586f
[]
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
1,107
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad, Floris van Doorn -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.logic import Mathlib.Lean3Lib.init.wf universes u v namespace Mathlib theorem ex_of_psig {α : Type u} {p : α → Prop} : (psigma fun (x : α) => p x) → ∃ (x : α), p x := fun (ᾰ : psigma fun (x : α) => p x) => psigma.cases_on ᾰ fun (ᾰ_fst : α) (ᾰ_snd : p ᾰ_fst) => idRhs (∃ (x : α), p x) (Exists.intro ᾰ_fst ᾰ_snd) protected theorem sigma.eq {α : Type u} {β : α → Type v} {p₁ : sigma fun (a : α) => β a} {p₂ : sigma fun (a : α) => β a} (h₁ : sigma.fst p₁ = sigma.fst p₂) : eq.rec_on h₁ (sigma.snd p₁) = sigma.snd p₂ → p₁ = p₂ := sorry protected theorem psigma.eq {α : Sort u} {β : α → Sort v} {p₁ : psigma β} {p₂ : psigma β} (h₁ : psigma.fst p₁ = psigma.fst p₂) : eq.rec_on h₁ (psigma.snd p₁) = psigma.snd p₂ → p₁ = p₂ := sorry end Mathlib
aa30041846717a6fd7edce5872bb25c172588ce0
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/data/polynomial/ring_division.lean
c626f5d33c862645ea9f59eb4074fcc9fb9b4508
[ "Apache-2.0" ]
permissive
anthony2698/mathlib
03cd69fe5c280b0916f6df2d07c614c8e1efe890
407615e05814e98b24b2ff322b14e8e3eb5e5d67
refs/heads/master
1,678,792,774,873
1,614,371,563,000
1,614,371,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
27,150
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker, Johan Commelin -/ import data.polynomial.basic import data.polynomial.div import data.polynomial.algebra_map import data.set.finite /-! # Theory of univariate polynomials This file starts looking like the ring theory of $ R[X] $ -/ noncomputable theory local attribute [instance, priority 100] classical.prop_decidable open finset namespace polynomial universes u v w z variables {R : Type u} {S : Type v} {T : Type w} {A : Type z} {a b : R} {n : ℕ} section comm_ring variables [comm_ring R] {p q : polynomial R} variables [comm_ring S] lemma nat_degree_pos_of_aeval_root [algebra R S] {p : polynomial R} (hp : p ≠ 0) {z : S} (hz : aeval z p = 0) (inj : ∀ (x : R), algebra_map R S x = 0 → x = 0) : 0 < p.nat_degree := nat_degree_pos_of_eval₂_root hp (algebra_map R S) hz inj lemma degree_pos_of_aeval_root [algebra R S] {p : polynomial R} (hp : p ≠ 0) {z : S} (hz : aeval z p = 0) (inj : ∀ (x : R), algebra_map R S x = 0 → x = 0) : 0 < p.degree := nat_degree_pos_iff_degree_pos.mp (nat_degree_pos_of_aeval_root hp hz inj) lemma aeval_mod_by_monic_eq_self_of_root [algebra R S] {p q : polynomial R} (hq : q.monic) {x : S} (hx : aeval x q = 0) : aeval x (p %ₘ q) = aeval x p := eval₂_mod_by_monic_eq_self_of_root hq hx end comm_ring section integral_domain variables [integral_domain R] {p q : polynomial R} instance : integral_domain (polynomial R) := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h, begin have : leading_coeff 0 = leading_coeff a * leading_coeff b := h ▸ leading_coeff_mul a b, rw [leading_coeff_zero, eq_comm] at this, erw [← leading_coeff_eq_zero, ← leading_coeff_eq_zero], exact eq_zero_or_eq_zero_of_mul_eq_zero this end, ..polynomial.nontrivial, ..polynomial.comm_ring } lemma nat_degree_mul (hp : p ≠ 0) (hq : q ≠ 0) : nat_degree (p * q) = nat_degree p + nat_degree q := by rw [← with_bot.coe_eq_coe, ← degree_eq_nat_degree (mul_ne_zero hp hq), with_bot.coe_add, ← degree_eq_nat_degree hp, ← degree_eq_nat_degree hq, degree_mul] lemma nat_trailing_degree_mul (hp : p ≠ 0) (hq : q ≠ 0) : (p * q).nat_trailing_degree = p.nat_trailing_degree + q.nat_trailing_degree := begin simp only [←nat.sub_eq_of_eq_add (nat_degree_eq_reverse_nat_degree_add_nat_trailing_degree _)], rw [reverse_mul_of_domain, nat_degree_mul hp hq, nat_degree_mul (mt reverse_eq_zero.mp hp) (mt reverse_eq_zero.mp hq), reverse_nat_degree, reverse_nat_degree, ←nat.sub_sub, nat.add_comm, nat.add_sub_assoc (nat.sub_le _ _), add_comm, nat.add_sub_assoc (nat.sub_le _ _)], end @[simp] lemma nat_degree_pow (p : polynomial R) (n : ℕ) : nat_degree (p ^ n) = n * nat_degree p := if hp0 : p = 0 then if hn0 : n = 0 then by simp [hp0, hn0] else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp else nat_degree_pow' (by rw [← leading_coeff_pow, ne.def, leading_coeff_eq_zero]; exact pow_ne_zero _ hp0) lemma root_mul : is_root (p * q) a ↔ is_root p a ∨ is_root q a := by simp_rw [is_root, eval_mul, mul_eq_zero] lemma root_or_root_of_root_mul (h : is_root (p * q) a) : is_root p a ∨ is_root q a := root_mul.1 h lemma degree_le_mul_left (p : polynomial R) (hq : q ≠ 0) : degree p ≤ degree (p * q) := if hp : p = 0 then by simp only [hp, zero_mul, le_refl] else by rw [degree_mul, degree_eq_nat_degree hp, degree_eq_nat_degree hq]; exact with_bot.coe_le_coe.2 (nat.le_add_right _ _) theorem nat_degree_le_of_dvd {p q : polynomial R} (h1 : p ∣ q) (h2 : q ≠ 0) : p.nat_degree ≤ q.nat_degree := begin rcases h1 with ⟨q, rfl⟩, rw mul_ne_zero_iff at h2, rw [nat_degree_mul h2.1 h2.2], exact nat.le_add_right _ _ end section roots open multiset local attribute [reducible] with_zero lemma degree_eq_zero_of_is_unit (h : is_unit p) : degree p = 0 := let ⟨q, hq⟩ := is_unit_iff_dvd_one.1 h in have hp0 : p ≠ 0, from λ hp0, by simpa [hp0] using hq, have hq0 : q ≠ 0, from λ hp0, by simpa [hp0] using hq, have nat_degree (1 : polynomial R) = nat_degree (p * q), from congr_arg _ hq, by rw [nat_degree_one, nat_degree_mul hp0 hq0, eq_comm, _root_.add_eq_zero_iff, ← with_bot.coe_eq_coe, ← degree_eq_nat_degree hp0] at this; exact this.1 @[simp] lemma degree_coe_units (u : units (polynomial R)) : degree (u : polynomial R) = 0 := degree_eq_zero_of_is_unit ⟨u, rfl⟩ theorem prime_X_sub_C {r : R} : prime (X - C r) := ⟨X_sub_C_ne_zero r, not_is_unit_X_sub_C, λ _ _, by { simp_rw [dvd_iff_is_root, is_root.def, eval_mul, mul_eq_zero], exact id }⟩ theorem prime_X : prime (X : polynomial R) := by { convert (prime_X_sub_C : prime (X - C 0 : polynomial R)), simp } lemma prime_of_degree_eq_one_of_monic (hp1 : degree p = 1) (hm : monic p) : prime p := have p = X - C (- p.coeff 0), by simpa [hm.leading_coeff] using eq_X_add_C_of_degree_eq_one hp1, this.symm ▸ prime_X_sub_C theorem irreducible_X_sub_C (r : R) : irreducible (X - C r) := irreducible_of_prime prime_X_sub_C theorem irreducible_X : irreducible (X : polynomial R) := irreducible_of_prime prime_X lemma irreducible_of_degree_eq_one_of_monic (hp1 : degree p = 1) (hm : monic p) : irreducible p := irreducible_of_prime (prime_of_degree_eq_one_of_monic hp1 hm) theorem eq_of_monic_of_associated (hp : p.monic) (hq : q.monic) (hpq : associated p q) : p = q := begin obtain ⟨u, hu⟩ := hpq, unfold monic at hp hq, rw eq_C_of_degree_le_zero (le_of_eq $ degree_coe_units _) at hu, rw [← hu, leading_coeff_mul, hp, one_mul, leading_coeff_C] at hq, rwa [hq, C_1, mul_one] at hu end @[simp] lemma root_multiplicity_zero {x : R} : root_multiplicity x 0 = 0 := dif_pos rfl lemma root_multiplicity_eq_zero {p : polynomial R} {x : R} (h : ¬ is_root p x) : root_multiplicity x p = 0 := begin rw root_multiplicity_eq_multiplicity, split_ifs, { refl }, rw [← enat.coe_inj, enat.coe_get, multiplicity.multiplicity_eq_zero_of_not_dvd, enat.coe_zero], intro hdvd, exact h (dvd_iff_is_root.mp hdvd) end lemma root_multiplicity_pos {p : polynomial R} (hp : p ≠ 0) {x : R} : 0 < root_multiplicity x p ↔ is_root p x := begin rw [← dvd_iff_is_root, root_multiplicity_eq_multiplicity, dif_neg hp, ← enat.coe_lt_coe, enat.coe_get], exact multiplicity.dvd_iff_multiplicity_pos end lemma root_multiplicity_mul {p q : polynomial R} {x : R} (hpq : p * q ≠ 0) : root_multiplicity x (p * q) = root_multiplicity x p + root_multiplicity x q := begin have hp : p ≠ 0 := left_ne_zero_of_mul hpq, have hq : q ≠ 0 := right_ne_zero_of_mul hpq, rw [root_multiplicity_eq_multiplicity (p * q), dif_neg hpq, root_multiplicity_eq_multiplicity p, dif_neg hp, root_multiplicity_eq_multiplicity q, dif_neg hq, @multiplicity.mul' _ _ _ (X - C x) _ _ prime_X_sub_C], end lemma root_multiplicity_X_sub_C_self {x : R} : root_multiplicity x (X - C x) = 1 := by rw [root_multiplicity_eq_multiplicity, dif_neg (X_sub_C_ne_zero x), multiplicity.get_multiplicity_self] lemma root_multiplicity_X_sub_C {x y : R} : root_multiplicity x (X - C y) = if x = y then 1 else 0 := begin split_ifs with hxy, { rw hxy, exact root_multiplicity_X_sub_C_self }, exact root_multiplicity_eq_zero (mt root_X_sub_C.mp (ne.symm hxy)) end /-- The multiplicity of `a` as root of `(X - a) ^ n` is `n`. -/ lemma root_multiplicity_X_sub_C_pow (a : R) (n : ℕ) : root_multiplicity a ((X - C a) ^ n) = n := begin induction n with n hn, { refine root_multiplicity_eq_zero _, simp only [eval_one, is_root.def, not_false_iff, one_ne_zero, pow_zero] }, have hzero := (ne_zero_of_monic (monic_pow (monic_X_sub_C a) n.succ)), rw pow_succ (X - C a) n at hzero ⊢, simp only [root_multiplicity_mul hzero, root_multiplicity_X_sub_C_self, hn, nat.one_add] end /-- If `(X - a) ^ n` divides a polynomial `p` then the multiplicity of `a` as root of `p` is at least `n`. -/ lemma root_multiplicity_of_dvd {p : polynomial R} {a : R} {n : ℕ} (hzero : p ≠ 0) (h : (X - C a) ^ n ∣ p) : n ≤ root_multiplicity a p := begin obtain ⟨q, hq⟩ := exists_eq_mul_right_of_dvd h, rw hq at hzero, simp only [hq, root_multiplicity_mul hzero, root_multiplicity_X_sub_C_pow, ge_iff_le, _root_.zero_le, le_add_iff_nonneg_right], end /-- The multiplicity of `p + q` is at least the minimum of the multiplicities. -/ lemma root_multiplicity_add {p q : polynomial R} (a : R) (hzero : p + q ≠ 0) : min (root_multiplicity a p) (root_multiplicity a q) ≤ root_multiplicity a (p + q) := begin refine root_multiplicity_of_dvd hzero _, have hdivp : (X - C a) ^ root_multiplicity a p ∣ p := pow_root_multiplicity_dvd p a, have hdivq : (X - C a) ^ root_multiplicity a q ∣ q := pow_root_multiplicity_dvd q a, exact min_pow_dvd_add hdivp hdivq end lemma exists_multiset_roots : ∀ {p : polynomial R} (hp : p ≠ 0), ∃ s : multiset R, (s.card : with_bot ℕ) ≤ degree p ∧ ∀ a, s.count a = root_multiplicity a p | p := λ hp, by haveI := classical.prop_decidable (∃ x, is_root p x); exact if h : ∃ x, is_root p x then let ⟨x, hx⟩ := h in have hpd : 0 < degree p := degree_pos_of_root hp hx, have hd0 : p /ₘ (X - C x) ≠ 0 := λ h, by rw [← mul_div_by_monic_eq_iff_is_root.2 hx, h, mul_zero] at hp; exact hp rfl, have wf : degree (p /ₘ _) < degree p := degree_div_by_monic_lt _ (monic_X_sub_C x) hp ((degree_X_sub_C x).symm ▸ dec_trivial), let ⟨t, htd, htr⟩ := @exists_multiset_roots (p /ₘ (X - C x)) hd0 in have hdeg : degree (X - C x) ≤ degree p := begin rw [degree_X_sub_C, degree_eq_nat_degree hp], rw degree_eq_nat_degree hp at hpd, exact with_bot.coe_le_coe.2 (with_bot.coe_lt_coe.1 hpd) end, have hdiv0 : p /ₘ (X - C x) ≠ 0 := mt (div_by_monic_eq_zero_iff (monic_X_sub_C x) (ne_zero_of_monic (monic_X_sub_C x))).1 $ not_lt.2 hdeg, ⟨x ::ₘ t, calc (card (x ::ₘ t) : with_bot ℕ) = t.card + 1 : by exact_mod_cast card_cons _ _ ... ≤ degree p : by rw [← degree_add_div_by_monic (monic_X_sub_C x) hdeg, degree_X_sub_C, add_comm]; exact add_le_add (le_refl (1 : with_bot ℕ)) htd, begin assume a, conv_rhs { rw ← mul_div_by_monic_eq_iff_is_root.mpr hx }, rw [root_multiplicity_mul (mul_ne_zero (X_sub_C_ne_zero _) hdiv0), root_multiplicity_X_sub_C, ← htr a], split_ifs with ha, { rw [ha, count_cons_self, nat.succ_eq_add_one, add_comm] }, { rw [count_cons_of_ne ha, zero_add] }, end⟩ else ⟨0, (degree_eq_nat_degree hp).symm ▸ with_bot.coe_le_coe.2 (nat.zero_le _), by { intro a, rw [count_zero, root_multiplicity_eq_zero (not_exists.mp h a)] }⟩ using_well_founded {dec_tac := tactic.assumption} /-- `roots p` noncomputably gives a multiset containing all the roots of `p`, including their multiplicities. -/ noncomputable def roots (p : polynomial R) : multiset R := if h : p = 0 then ∅ else classical.some (exists_multiset_roots h) @[simp] lemma roots_zero : (0 : polynomial R).roots = 0 := dif_pos rfl lemma card_roots (hp0 : p ≠ 0) : ((roots p).card : with_bot ℕ) ≤ degree p := begin unfold roots, rw dif_neg hp0, exact (classical.some_spec (exists_multiset_roots hp0)).1 end lemma card_roots' {p : polynomial R} (hp0 : p ≠ 0) : p.roots.card ≤ nat_degree p := with_bot.coe_le_coe.1 (le_trans (card_roots hp0) (le_of_eq $ degree_eq_nat_degree hp0)) lemma card_roots_sub_C {p : polynomial R} {a : R} (hp0 : 0 < degree p) : ((p - C a).roots.card : with_bot ℕ) ≤ degree p := calc ((p - C a).roots.card : with_bot ℕ) ≤ degree (p - C a) : card_roots $ mt sub_eq_zero.1 $ λ h, not_le_of_gt hp0 $ h.symm ▸ degree_C_le ... = degree p : by rw [sub_eq_add_neg, ← C_neg]; exact degree_add_C hp0 lemma card_roots_sub_C' {p : polynomial R} {a : R} (hp0 : 0 < degree p) : (p - C a).roots.card ≤ nat_degree p := with_bot.coe_le_coe.1 (le_trans (card_roots_sub_C hp0) (le_of_eq $ degree_eq_nat_degree (λ h, by simp [*, lt_irrefl] at *))) @[simp] lemma count_roots (hp : p ≠ 0) : p.roots.count a = root_multiplicity a p := by { rw [roots, dif_neg hp], exact (classical.some_spec (exists_multiset_roots hp)).2 a } @[simp] lemma mem_roots (hp : p ≠ 0) : a ∈ p.roots ↔ is_root p a := by rw [← count_pos, count_roots hp, root_multiplicity_pos hp] lemma eq_zero_of_infinite_is_root (p : polynomial R) (h : set.infinite {x | is_root p x}) : p = 0 := begin by_contradiction hp, apply h, convert p.roots.to_finset.finite_to_set using 1, ext1 r, simp only [mem_roots hp, multiset.mem_to_finset, set.mem_set_of_eq, finset.mem_coe] end lemma eq_of_infinite_eval_eq {R : Type*} [integral_domain R] (p q : polynomial R) (h : set.infinite {x | eval x p = eval x q}) : p = q := begin rw [← sub_eq_zero], apply eq_zero_of_infinite_is_root, simpa only [is_root, eval_sub, sub_eq_zero] end lemma roots_mul {p q : polynomial R} (hpq : p * q ≠ 0) : (p * q).roots = p.roots + q.roots := multiset.ext.mpr $ λ r, by rw [count_add, count_roots hpq, count_roots (left_ne_zero_of_mul hpq), count_roots (right_ne_zero_of_mul hpq), root_multiplicity_mul hpq] @[simp] lemma mem_roots_sub_C {p : polynomial R} {a x : R} (hp0 : 0 < degree p) : x ∈ (p - C a).roots ↔ p.eval x = a := (mem_roots (show p - C a ≠ 0, from mt sub_eq_zero.1 $ λ h, not_le_of_gt hp0 $ h.symm ▸ degree_C_le)).trans (by rw [is_root.def, eval_sub, eval_C, sub_eq_zero]) @[simp] lemma roots_X_sub_C (r : R) : roots (X - C r) = r ::ₘ 0 := begin ext s, rw [count_roots (X_sub_C_ne_zero r), root_multiplicity_X_sub_C], split_ifs with h, { rw [h, count_singleton] }, { rw [count_cons_of_ne h, count_zero] } end @[simp] lemma roots_C (x : R) : (C x).roots = 0 := if H : x = 0 then by rw [H, C_0, roots_zero] else multiset.ext.mpr $ λ r, have h : C x ≠ 0, from λ h, H $ C_inj.1 $ h.symm ▸ C_0.symm, have not_root : ¬ is_root (C x) r := mt (λ (h : eval r (C x) = 0), trans eval_C.symm h) H, by rw [count_roots h, count_zero, root_multiplicity_eq_zero not_root] @[simp] lemma roots_one : (1 : polynomial R).roots = ∅ := roots_C 1 lemma roots_list_prod (L : list (polynomial R)) : (∀ p ∈ L, (p : _) ≠ 0) → L.prod.roots = (L : multiset (polynomial R)).bind roots := list.rec_on L (λ _, roots_one) $ λ hd tl ih H, begin rw list.forall_mem_cons at H, rw [list.prod_cons, roots_mul (mul_ne_zero H.1 $ list.prod_ne_zero H.2), ← multiset.cons_coe, multiset.cons_bind, ih H.2] end lemma roots_multiset_prod (m : multiset (polynomial R)) : (∀ p ∈ m, (p : _) ≠ 0) → m.prod.roots = m.bind roots := multiset.induction_on m (λ _, roots_one) $ λ hd tl ih H, begin rw multiset.forall_mem_cons at H, rw [multiset.prod_cons, roots_mul (mul_ne_zero H.1 $ multiset.prod_ne_zero H.2), multiset.cons_bind, ih H.2] end lemma roots_prod {ι : Type*} (f : ι → polynomial R) (s : finset ι) : s.prod f ≠ 0 → (s.prod f).roots = s.val.bind (λ i, roots (f i)) := begin refine s.induction_on _ _, { intros, exact roots_one }, intros i s hi ih ne_zero, rw prod_insert hi at ⊢ ne_zero, rw [roots_mul ne_zero, ih (right_ne_zero_of_mul ne_zero), insert_val, ndinsert_of_not_mem hi, cons_bind] end lemma roots_prod_X_sub_C (s : finset R) : (s.prod (λ a, X - C a)).roots = s.val := (roots_prod (λ a, X - C a) s (prod_ne_zero_iff.mpr (λ a _, X_sub_C_ne_zero a))).trans (by simp_rw [roots_X_sub_C, bind_cons, bind_zero, add_zero, multiset.map_id']) lemma card_roots_X_pow_sub_C {n : ℕ} (hn : 0 < n) (a : R) : (roots ((X : polynomial R) ^ n - C a)).card ≤ n := with_bot.coe_le_coe.1 $ calc ((roots ((X : polynomial R) ^ n - C a)).card : with_bot ℕ) ≤ degree ((X : polynomial R) ^ n - C a) : card_roots (X_pow_sub_C_ne_zero hn a) ... = n : degree_X_pow_sub_C hn a section nth_roots /-- `nth_roots n a` noncomputably returns the solutions to `x ^ n = a`-/ def nth_roots (n : ℕ) (a : R) : multiset R := roots ((X : polynomial R) ^ n - C a) @[simp] lemma mem_nth_roots {n : ℕ} (hn : 0 < n) {a x : R} : x ∈ nth_roots n a ↔ x ^ n = a := by rw [nth_roots, mem_roots (X_pow_sub_C_ne_zero hn a), is_root.def, eval_sub, eval_C, eval_pow, eval_X, sub_eq_zero_iff_eq] @[simp] lemma nth_roots_zero (r : R) : nth_roots 0 r = 0 := by simp only [empty_eq_zero, pow_zero, nth_roots, ← C_1, ← C_sub, roots_C] lemma card_nth_roots (n : ℕ) (a : R) : (nth_roots n a).card ≤ n := if hn : n = 0 then if h : (X : polynomial R) ^ n - C a = 0 then by simp only [nat.zero_le, nth_roots, roots, h, dif_pos rfl, empty_eq_zero, card_zero] else with_bot.coe_le_coe.1 (le_trans (card_roots h) (by rw [hn, pow_zero, ← C_1, ← @is_ring_hom.map_sub _ _ _ _ (@C R _)]; exact degree_C_le)) else by rw [← with_bot.coe_le_coe, ← degree_X_pow_sub_C (nat.pos_of_ne_zero hn) a]; exact card_roots (X_pow_sub_C_ne_zero (nat.pos_of_ne_zero hn) a) /-- The multiset `nth_roots ↑n (1 : R)` as a finset. -/ def nth_roots_finset (n : ℕ) (R : Type*) [integral_domain R] : finset R := multiset.to_finset (nth_roots n (1 : R)) @[simp] lemma mem_nth_roots_finset {n : ℕ} (h : 0 < n) {x : R} : x ∈ nth_roots_finset n R ↔ x ^ (n : ℕ) = 1 := by rw [nth_roots_finset, mem_to_finset, mem_nth_roots h] end nth_roots lemma coeff_comp_degree_mul_degree (hqd0 : nat_degree q ≠ 0) : coeff (p.comp q) (nat_degree p * nat_degree q) = leading_coeff p * leading_coeff q ^ nat_degree p := if hp0 : p = 0 then by simp [hp0] else calc coeff (p.comp q) (nat_degree p * nat_degree q) = p.sum (λ n a, coeff (C a * q ^ n) (nat_degree p * nat_degree q)) : by rw [comp, eval₂, coeff_sum] ... = coeff (C (leading_coeff p) * q ^ nat_degree p) (nat_degree p * nat_degree q) : finset.sum_eq_single _ begin assume b hbs hbp, have hq0 : q ≠ 0, from λ hq0, hqd0 (by rw [hq0, nat_degree_zero]), have : coeff p b ≠ 0, by rwa finsupp.mem_support_iff at hbs, refine coeff_eq_zero_of_degree_lt _, erw [degree_mul, degree_C this, degree_pow, zero_add, degree_eq_nat_degree hq0, ← with_bot.coe_nsmul, nsmul_eq_mul, with_bot.coe_lt_coe, nat.cast_id, mul_lt_mul_right (pos_iff_ne_zero.mpr hqd0)], exact lt_of_le_of_ne (le_nat_degree_of_ne_zero this) hbp, end begin intro h, contrapose! hp0, rw finsupp.mem_support_iff at h, push_neg at h, rwa ← leading_coeff_eq_zero, end ... = _ : have coeff (q ^ nat_degree p) (nat_degree p * nat_degree q) = leading_coeff (q ^ nat_degree p), by rw [leading_coeff, nat_degree_pow], by rw [coeff_C_mul, this, leading_coeff_pow] lemma nat_degree_comp : nat_degree (p.comp q) = nat_degree p * nat_degree q := le_antisymm nat_degree_comp_le (if hp0 : p = 0 then by rw [hp0, zero_comp, nat_degree_zero, zero_mul] else if hqd0 : nat_degree q = 0 then have degree q ≤ 0, by rw [← with_bot.coe_zero, ← hqd0]; exact degree_le_nat_degree, by rw [eq_C_of_degree_le_zero this]; simp else le_nat_degree_of_ne_zero $ have hq0 : q ≠ 0, from λ hq0, hqd0 $ by rw [hq0, nat_degree_zero], calc coeff (p.comp q) (nat_degree p * nat_degree q) = leading_coeff p * leading_coeff q ^ nat_degree p : coeff_comp_degree_mul_degree hqd0 ... ≠ 0 : mul_ne_zero (mt leading_coeff_eq_zero.1 hp0) (pow_ne_zero _ (mt leading_coeff_eq_zero.1 hq0))) lemma leading_coeff_comp (hq : nat_degree q ≠ 0) : leading_coeff (p.comp q) = leading_coeff p * leading_coeff q ^ nat_degree p := by rw [← coeff_comp_degree_mul_degree hq, ← nat_degree_comp]; refl lemma units_coeff_zero_smul (c : units (polynomial R)) (p : polynomial R) : (c : polynomial R).coeff 0 • p = c * p := by rw [←polynomial.C_mul', ←polynomial.eq_C_of_degree_eq_zero (degree_coe_units c)] @[simp] lemma nat_degree_coe_units (u : units (polynomial R)) : nat_degree (u : polynomial R) = 0 := nat_degree_eq_of_degree_eq_some (degree_coe_units u) lemma comp_eq_zero_iff : p.comp q = 0 ↔ p = 0 ∨ (p.eval (q.coeff 0) = 0 ∧ q = C (q.coeff 0)) := begin split, { intro h, have key : p.nat_degree = 0 ∨ q.nat_degree = 0, { rw [←mul_eq_zero, ←nat_degree_comp, h, nat_degree_zero] }, replace key := or.imp eq_C_of_nat_degree_eq_zero eq_C_of_nat_degree_eq_zero key, cases key, { rw [key, C_comp] at h, exact or.inl (key.trans h) }, { rw [key, comp_C, C_eq_zero] at h, exact or.inr ⟨h, key⟩ }, }, { exact λ h, or.rec (λ h, by rw [h, zero_comp]) (λ h, by rw [h.2, comp_C, h.1, C_0]) h }, end lemma zero_of_eval_zero [infinite R] (p : polynomial R) (h : ∀ x, p.eval x = 0) : p = 0 := by classical; by_contradiction hp; exact infinite.not_fintype ⟨p.roots.to_finset, λ x, multiset.mem_to_finset.mpr ((mem_roots hp).mpr (h _))⟩ lemma funext [infinite R] {p q : polynomial R} (ext : ∀ r : R, p.eval r = q.eval r) : p = q := begin rw ← sub_eq_zero, apply zero_of_eval_zero, intro x, rw [eval_sub, sub_eq_zero, ext], end /-- The set of distinct roots of `p` in `E`. If you have a non-separable polynomial, use `polynomial.roots` for the multiset where multiple roots have the appropriate multiplicity. -/ def root_set (p : polynomial R) (S) [integral_domain S] [algebra R S] : set S := (p.map (algebra_map R S)).roots.to_finset lemma root_set_def (p : polynomial R) (S) [integral_domain S] [algebra R S] : p.root_set S = (p.map (algebra_map R S)).roots.to_finset := rfl @[simp] lemma root_set_zero (S) [integral_domain S] [algebra R S] : (0 : polynomial R).root_set S = ∅ := by rw [root_set_def, polynomial.map_zero, roots_zero, to_finset_zero, finset.coe_empty] end roots theorem is_unit_iff {f : polynomial R} : is_unit f ↔ ∃ r : R, is_unit r ∧ C r = f := ⟨λ hf, ⟨f.coeff 0, is_unit_C.1 $ eq_C_of_degree_eq_zero (degree_eq_zero_of_is_unit hf) ▸ hf, (eq_C_of_degree_eq_zero (degree_eq_zero_of_is_unit hf)).symm⟩, λ ⟨r, hr, hrf⟩, hrf ▸ is_unit_C.2 hr⟩ lemma coeff_coe_units_zero_ne_zero (u : units (polynomial R)) : coeff (u : polynomial R) 0 ≠ 0 := begin conv in (0) {rw [← nat_degree_coe_units u]}, rw [← leading_coeff, ne.def, leading_coeff_eq_zero], exact units.ne_zero _ end lemma degree_eq_degree_of_associated (h : associated p q) : degree p = degree q := let ⟨u, hu⟩ := h in by simp [hu.symm] lemma degree_eq_one_of_irreducible_of_root (hi : irreducible p) {x : R} (hx : is_root p x) : degree p = 1 := let ⟨g, hg⟩ := dvd_iff_is_root.2 hx in have is_unit (X - C x) ∨ is_unit g, from hi.is_unit_or_is_unit hg, this.elim (λ h, have h₁ : degree (X - C x) = 1, from degree_X_sub_C x, have h₂ : degree (X - C x) = 0, from degree_eq_zero_of_is_unit h, by rw h₁ at h₂; exact absurd h₂ dec_trivial) (λ hgu, by rw [hg, degree_mul, degree_X_sub_C, degree_eq_zero_of_is_unit hgu, add_zero]) /-- Division by a monic polynomial doesn't change the leading coefficient. -/ lemma leading_coeff_div_by_monic_of_monic {R : Type u} [integral_domain R] {p q : polynomial R} (hmonic : q.monic) (hdegree : q.degree ≤ p.degree) : (p /ₘ q).leading_coeff = p.leading_coeff := begin have hp := mod_by_monic_add_div p hmonic, have hzero : (p /ₘ q) ≠ 0, { intro h, exact not_lt_of_le hdegree ((div_by_monic_eq_zero_iff hmonic (monic.ne_zero hmonic)).1 h) }, have deglt : (p %ₘ q).degree < (q * (p /ₘ q)).degree, { rw degree_mul, refine lt_of_lt_of_le (degree_mod_by_monic_lt p hmonic (monic.ne_zero hmonic)) _, rw [degree_eq_nat_degree (monic.ne_zero hmonic), degree_eq_nat_degree hzero], norm_cast, simp only [zero_le, le_add_iff_nonneg_right] }, have hrew := (leading_coeff_add_of_degree_lt deglt), rw leading_coeff_mul q (p /ₘ q) at hrew, simp only [hmonic, one_mul, monic.leading_coeff] at hrew, nth_rewrite 1 ← hp, exact hrew.symm end lemma eq_of_monic_of_dvd_of_nat_degree_le (hp : p.monic) (hq : q.monic) (hdiv : p ∣ q) (hdeg : q.nat_degree ≤ p.nat_degree) : q = p := begin obtain ⟨r, hr⟩ := hdiv, have rzero : r ≠ 0, { intro h, simpa [h, monic.ne_zero hq] using hr }, rw [hr, nat_degree_mul (monic.ne_zero hp) rzero] at hdeg, have hdegeq : p.nat_degree + r.nat_degree = p.nat_degree, { suffices hdegle : p.nat_degree ≤ p.nat_degree + r.nat_degree, { exact le_antisymm hdeg hdegle }, exact nat.le.intro rfl }, replace hdegeq := eq_C_of_nat_degree_eq_zero (((@add_right_inj _ _ p.nat_degree) _ 0).1 hdegeq), suffices hlead : 1 = r.leading_coeff, { have hcoeff := leading_coeff_C (r.coeff 0), rw [← hdegeq, ← hlead] at hcoeff, rw [← hcoeff, C_1] at hdegeq, rwa [hdegeq, mul_one] at hr }, have hprod : q.leading_coeff = p.leading_coeff * r.leading_coeff, { simp only [hr, leading_coeff_mul] }, rwa [monic.leading_coeff hp, monic.leading_coeff hq, one_mul] at hprod end end integral_domain section variables [semiring R] [integral_domain S] (φ : R →+* S) lemma is_unit_of_is_unit_leading_coeff_of_is_unit_map (f : polynomial R) (hf : is_unit (leading_coeff f)) (H : is_unit (map φ f)) : is_unit f := begin have dz := degree_eq_zero_of_is_unit H, rw degree_map_eq_of_leading_coeff_ne_zero at dz, { rw eq_C_of_degree_eq_zero dz, apply is_unit.map', convert hf, rw (degree_eq_iff_nat_degree_eq _).1 dz, rintro rfl, simpa using H, }, { intro h, have u : is_unit (φ f.leading_coeff) := is_unit.map' _ hf, rw h at u, simpa using u, } end end section variables [integral_domain R] [integral_domain S] (φ : R →+* S) /-- A polynomial over an integral domain `R` is irreducible if it is monic and irreducible after mapping into an integral domain `S`. A special case of this lemma is that a polynomial over `ℤ` is irreducible if it is monic and irreducible over `ℤ/pℤ` for some prime `p`. -/ lemma monic.irreducible_of_irreducible_map (f : polynomial R) (h_mon : monic f) (h_irr : irreducible (map φ f)) : irreducible f := begin fsplit, { intro h, exact h_irr.not_unit (is_unit.map (monoid_hom.of (map φ)) h), }, { intros a b h, have q := (leading_coeff_mul a b).symm, rw ←h at q, dsimp [monic] at h_mon, rw h_mon at q, have au : is_unit a.leading_coeff := is_unit_of_mul_eq_one _ _ q, rw mul_comm at q, have bu : is_unit b.leading_coeff := is_unit_of_mul_eq_one _ _ q, clear q h_mon, have h' := congr_arg (map φ) h, simp only [map_mul] at h', cases h_irr.is_unit_or_is_unit h' with w w, { left, exact is_unit_of_is_unit_leading_coeff_of_is_unit_map _ _ au w, }, { right, exact is_unit_of_is_unit_leading_coeff_of_is_unit_map _ _ bu w, }, } end end end polynomial namespace is_integral_domain variables {R : Type*} [comm_ring R] /-- Lift evidence that `is_integral_domain R` to `is_integral_domain (polynomial R)`. -/ lemma polynomial (h : is_integral_domain R) : is_integral_domain (polynomial R) := @integral_domain.to_is_integral_domain _ (@polynomial.integral_domain _ (h.to_integral_domain _)) end is_integral_domain
c361e7d4cb8fc5945892a07b122287a6928bea82
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/topology/path_connected.lean
dec86d838698913f0ab28891687ef6551659bd59
[ "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
33,406
lean
/- Copyright (c) 2020 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import topology.unit_interval import topology.algebra.ordered.proj_Icc import topology.continuous_function.basic /-! # Path connectedness ## Main definitions In the file the unit interval `[0, 1]` in `ℝ` is denoted by `I`, and `X` is a topological space. * `path (x y : X)` is the type of paths from `x` to `y`, i.e., continuous maps from `I` to `X` mapping `0` to `x` and `1` to `y`. * `path.map` is the image of a path under a continuous map. * `joined (x y : X)` means there is a path between `x` and `y`. * `joined.some_path (h : joined x y)` selects some path between two points `x` and `y`. * `path_component (x : X)` is the set of points joined to `x`. * `path_connected_space X` is a predicate class asserting that `X` is non-empty and every two points of `X` are joined. Then there are corresponding relative notions for `F : set X`. * `joined_in F (x y : X)` means there is a path `γ` joining `x` to `y` with values in `F`. * `joined_in.some_path (h : joined_in F x y)` selects a path from `x` to `y` inside `F`. * `path_component_in F (x : X)` is the set of points joined to `x` in `F`. * `is_path_connected F` asserts that `F` is non-empty and every two points of `F` are joined in `F`. * `loc_path_connected_space X` is a predicate class asserting that `X` is locally path-connected: each point has a basis of path-connected neighborhoods (we do *not* ask these to be open). ## Main theorems * `joined` and `joined_in F` are transitive relations. One can link the absolute and relative version in two directions, using `(univ : set X)` or the subtype `↥F`. * `path_connected_space_iff_univ : path_connected_space X ↔ is_path_connected (univ : set X)` * `is_path_connected_iff_path_connected_space : is_path_connected F ↔ path_connected_space ↥F` For locally path connected spaces, we have * `path_connected_space_iff_connected_space : path_connected_space X ↔ connected_space X` * `is_connected_iff_is_path_connected (U_op : is_open U) : is_path_connected U ↔ is_connected U` ## Implementation notes By default, all paths have `I` as their source and `X` as their target, but there is an operation `set.Icc_extend` that will extend any continuous map `γ : I → X` into a continuous map `Icc_extend zero_le_one γ : ℝ → X` that is constant before `0` and after `1`. This is used to define `path.extend` that turns `γ : path x y` into a continuous map `γ.extend : ℝ → X` whose restriction to `I` is the original `γ`, and is equal to `x` on `(-∞, 0]` and to `y` on `[1, +∞)`. -/ noncomputable theory open_locale classical topological_space filter unit_interval open filter set function unit_interval variables {X : Type*} [topological_space X] {x y z : X} {ι : Type*} /-! ### Paths -/ /-- Continuous path connecting two points `x` and `y` in a topological space -/ @[nolint has_inhabited_instance] structure path (x y : X) extends C(I, X) := (source' : to_fun 0 = x) (target' : to_fun 1 = y) instance : has_coe_to_fun (path x y) := ⟨_, λ p, p.to_fun⟩ @[ext] protected lemma path.ext {X : Type*} [topological_space X] {x y : X} : ∀ {γ₁ γ₂ : path x y}, (γ₁ : I → X) = γ₂ → γ₁ = γ₂ | ⟨⟨x, h11⟩, h12, h13⟩ ⟨⟨.(x), h21⟩, h22, h23⟩ rfl := rfl namespace path @[simp] lemma coe_mk (f : I → X) (h₁ h₂ h₃) : ⇑(mk ⟨f, h₁⟩ h₂ h₃ : path x y) = f := rfl variable (γ : path x y) @[continuity] protected lemma continuous : continuous γ := γ.continuous_to_fun @[simp] protected lemma source : γ 0 = x := γ.source' @[simp] protected lemma target : γ 1 = y := γ.target' /-- Any function `φ : Π (a : α), path (x a) (y a)` can be seen as a function `α × I → X`. -/ instance has_uncurry_path {X α : Type*} [topological_space X] {x y : α → X} : has_uncurry (Π (a : α), path (x a) (y a)) (α × I) X := ⟨λ φ p, φ p.1 p.2⟩ /-- The constant path from a point to itself -/ @[refl] def refl (x : X) : path x x := { to_fun := λ t, x, continuous_to_fun := continuous_const, source' := rfl, target' := rfl } @[simp] lemma refl_range {X : Type*} [topological_space X] {a : X} : range (path.refl a) = {a} := by simp [path.refl, has_coe_to_fun.coe, coe_fn] /-- The reverse of a path from `x` to `y`, as a path from `y` to `x` -/ @[symm] def symm (γ : path x y) : path y x := { to_fun := γ ∘ σ, continuous_to_fun := by continuity, source' := by simpa [-path.target] using γ.target, target' := by simpa [-path.source] using γ.source } @[simp] lemma refl_symm {X : Type*} [topological_space X] {a : X} : (path.refl a).symm = path.refl a := by { ext, refl } @[simp] lemma symm_range {X : Type*} [topological_space X] {a b : X} (γ : path a b) : range γ.symm = range γ := begin ext x, simp only [mem_range, path.symm, has_coe_to_fun.coe, coe_fn, unit_interval.symm, set_coe.exists, comp_app, subtype.coe_mk, subtype.val_eq_coe], split; rintros ⟨y, hy, hxy⟩; refine ⟨1-y, mem_iff_one_sub_mem.mp hy, _⟩; convert hxy, simp end /-- A continuous map extending a path to `ℝ`, constant before `0` and after `1`. -/ def extend : ℝ → X := Icc_extend zero_le_one γ lemma continuous_extend : continuous γ.extend := γ.continuous.Icc_extend @[simp] lemma extend_extends {X : Type*} [topological_space X] {a b : X} (γ : path a b) {t : ℝ} (ht : t ∈ (Icc 0 1 : set ℝ)) : γ.extend t = γ ⟨t, ht⟩ := Icc_extend_of_mem _ γ ht lemma extend_zero : γ.extend 0 = x := by simp lemma extend_one : γ.extend 1 = y := by simp @[simp] lemma extend_extends' {X : Type*} [topological_space X] {a b : X} (γ : path a b) (t : (Icc 0 1 : set ℝ)) : γ.extend t = γ t := Icc_extend_coe _ γ t @[simp] lemma extend_range {X : Type*} [topological_space X] {a b : X} (γ : path a b) : range γ.extend = range γ := Icc_extend_range _ γ lemma extend_of_le_zero {X : Type*} [topological_space X] {a b : X} (γ : path a b) {t : ℝ} (ht : t ≤ 0) : γ.extend t = a := (Icc_extend_of_le_left _ _ ht).trans γ.source lemma extend_of_one_le {X : Type*} [topological_space X] {a b : X} (γ : path a b) {t : ℝ} (ht : 1 ≤ t) : γ.extend t = b := (Icc_extend_of_right_le _ _ ht).trans γ.target @[simp] lemma refl_extend {X : Type*} [topological_space X] {a : X} : (path.refl a).extend = λ _, a := rfl /-- The path obtained from a map defined on `ℝ` by restriction to the unit interval. -/ def of_line {f : ℝ → X} (hf : continuous_on f I) (h₀ : f 0 = x) (h₁ : f 1 = y) : path x y := { to_fun := f ∘ coe, continuous_to_fun := hf.comp_continuous continuous_subtype_coe subtype.prop, source' := h₀, target' := h₁ } lemma of_line_mem {f : ℝ → X} (hf : continuous_on f I) (h₀ : f 0 = x) (h₁ : f 1 = y) : ∀ t, of_line hf h₀ h₁ t ∈ f '' I := λ ⟨t, t_in⟩, ⟨t, t_in, rfl⟩ local attribute [simp] Iic_def /-- Concatenation of two paths from `x` to `y` and from `y` to `z`, putting the first path on `[0, 1/2]` and the second one on `[1/2, 1]`. -/ @[trans] def trans (γ : path x y) (γ' : path y z) : path x z := { to_fun := (λ t : ℝ, if t ≤ 1/2 then γ.extend (2*t) else γ'.extend (2*t-1)) ∘ coe, continuous_to_fun := begin refine (continuous.if_le _ _ continuous_id continuous_const (by norm_num)).comp continuous_subtype_coe, -- TODO: the following are provable by `continuity` but it is too slow exacts [γ.continuous_extend.comp (continuous_const.mul continuous_id), γ'.continuous_extend.comp ((continuous_const.mul continuous_id).sub continuous_const)] end, source' := by norm_num, target' := by norm_num } @[simp] lemma refl_trans_refl {X : Type*} [topological_space X] {a : X} : (path.refl a).trans (path.refl a) = path.refl a := begin ext, simp only [path.trans, if_t_t, one_div, path.refl_extend], refl end lemma trans_range {X : Type*} [topological_space X] {a b c : X} (γ₁ : path a b) (γ₂ : path b c) : range (γ₁.trans γ₂) = range γ₁ ∪ range γ₂ := begin rw path.trans, apply eq_of_subset_of_subset, { rintros x ⟨⟨t, ht0, ht1⟩, hxt⟩, by_cases h : t ≤ 1/2, { left, use [2*t, ⟨by linarith, by linarith⟩], rw ← γ₁.extend_extends, unfold_coes at hxt, simp only [h, comp_app, if_true] at hxt, exact hxt }, { right, use [2*t-1, ⟨by linarith, by linarith⟩], rw ← γ₂.extend_extends, unfold_coes at hxt, simp only [h, comp_app, if_false] at hxt, exact hxt } }, { rintros x (⟨⟨t, ht0, ht1⟩, hxt⟩ | ⟨⟨t, ht0, ht1⟩, hxt⟩), { use ⟨t/2, ⟨by linarith, by linarith⟩⟩, unfold_coes, have : t/2 ≤ 1/2 := by linarith, simp only [this, comp_app, if_true], ring_nf, rwa γ₁.extend_extends }, { by_cases h : t = 0, { use ⟨1/2, ⟨by linarith, by linarith⟩⟩, unfold_coes, simp only [h, comp_app, if_true, le_refl, mul_one_div_cancel (@two_ne_zero ℝ _ _)], rw γ₁.extend_one, rwa [← γ₂.extend_extends, h, γ₂.extend_zero] at hxt }, { use ⟨(t+1)/2, ⟨by linarith, by linarith⟩⟩, unfold_coes, change t ≠ 0 at h, have ht0 := lt_of_le_of_ne ht0 h.symm, have : ¬ (t+1)/2 ≤ 1/2 := by {rw not_le, linarith}, simp only [comp_app, if_false, this], ring_nf, rwa γ₂.extend_extends } } } end /-- Image of a path from `x` to `y` by a continuous map -/ def map (γ : path x y) {Y : Type*} [topological_space Y] {f : X → Y} (h : continuous f) : path (f x) (f y) := { to_fun := f ∘ γ, continuous_to_fun := by continuity, source' := by simp, target' := by simp } @[simp] lemma map_coe (γ : path x y) {Y : Type*} [topological_space Y] {f : X → Y} (h : continuous f) : (γ.map h : I → Y) = f ∘ γ := by { ext t, refl } /-- Casting a path from `x` to `y` to a path from `x'` to `y'` when `x' = x` and `y' = y` -/ def cast (γ : path x y) {x' y'} (hx : x' = x) (hy : y' = y) : path x' y' := { to_fun := γ, continuous_to_fun := γ.continuous, source' := by simp [hx], target' := by simp [hy] } @[simp] lemma symm_cast {X : Type*} [topological_space X] {a₁ a₂ b₁ b₂ : X} (γ : path a₂ b₂) (ha : a₁ = a₂) (hb : b₁ = b₂) : (γ.cast ha hb).symm = (γ.symm).cast hb ha := rfl @[simp] lemma trans_cast {X : Type*} [topological_space X] {a₁ a₂ b₁ b₂ c₁ c₂ : X} (γ : path a₂ b₂) (γ' : path b₂ c₂) (ha : a₁ = a₂) (hb : b₁ = b₂) (hc : c₁ = c₂) : (γ.cast ha hb).trans (γ'.cast hb hc) = (γ.trans γ').cast ha hc := rfl @[simp] lemma cast_coe (γ : path x y) {x' y'} (hx : x' = x) (hy : y' = y) : (γ.cast hx hy : I → X) = γ := rfl lemma symm_continuous_family {X ι : Type*} [topological_space X] [topological_space ι] {a b : ι → X} (γ : Π (t : ι), path (a t) (b t)) (h : continuous ↿γ) : continuous ↿(λ t, (γ t).symm) := h.comp (continuous_id.prod_map continuous_symm) lemma continuous_uncurry_extend_of_continuous_family {X ι : Type*} [topological_space X] [topological_space ι] {a b : ι → X} (γ : Π (t : ι), path (a t) (b t)) (h : continuous ↿γ) : continuous ↿(λ t, (γ t).extend) := h.comp (continuous_id.prod_map continuous_proj_Icc) lemma trans_continuous_family {X ι : Type*} [topological_space X] [topological_space ι] {a b c : ι → X} (γ₁ : Π (t : ι), path (a t) (b t)) (h₁ : continuous ↿γ₁) (γ₂ : Π (t : ι), path (b t) (c t)) (h₂ : continuous ↿γ₂) : continuous ↿(λ t, (γ₁ t).trans (γ₂ t)) := begin have h₁' := path.continuous_uncurry_extend_of_continuous_family γ₁ h₁, have h₂' := path.continuous_uncurry_extend_of_continuous_family γ₂ h₂, simp only [has_uncurry.uncurry, has_coe_to_fun.coe, coe_fn, path.trans, (∘)], refine continuous.if_le _ _ (continuous_subtype_coe.comp continuous_snd) continuous_const _, { change continuous ((λ p : ι × ℝ, (γ₁ p.1).extend p.2) ∘ (prod.map id (λ x, 2*x : I → ℝ))), exact h₁'.comp (continuous_id.prod_map $ continuous_const.mul continuous_subtype_coe) }, { change continuous ((λ p : ι × ℝ, (γ₂ p.1).extend p.2) ∘ (prod.map id (λ x, 2*x - 1 : I → ℝ))), exact h₂'.comp (continuous_id.prod_map $ (continuous_const.mul continuous_subtype_coe).sub continuous_const) }, { rintros st hst, simp [hst, mul_inv_cancel (@two_ne_zero ℝ _ _)] } end /-! #### Truncating a path -/ /-- `γ.truncate t₀ t₁` is the path which follows the path `γ` on the time interval `[t₀, t₁]` and stays still otherwise. -/ def truncate {X : Type*} [topological_space X] {a b : X} (γ : path a b) (t₀ t₁ : ℝ) : path (γ.extend $ min t₀ t₁) (γ.extend t₁) := { to_fun := λ s, γ.extend (min (max s t₀) t₁), continuous_to_fun := γ.continuous_extend.comp ((continuous_subtype_coe.max continuous_const).min continuous_const), source' := begin unfold min max, dsimp only, norm_cast, split_ifs with h₁ h₂ h₃ h₄, { simp [γ.extend_of_le_zero h₁] }, { congr, linarith }, { have h₄ : t₁ ≤ 0 := le_of_lt (by simpa using h₂), simp [γ.extend_of_le_zero h₄, γ.extend_of_le_zero h₁] }, all_goals { refl } end, target' := begin unfold min max, dsimp only, norm_cast, split_ifs with h₁ h₂ h₃, { simp [γ.extend_of_one_le h₂] }, { refl }, { have h₄ : 1 ≤ t₀ := le_of_lt (by simpa using h₁), simp [γ.extend_of_one_le h₄, γ.extend_of_one_le (h₄.trans h₃)] }, { refl } end } /-- `γ.truncate_of_le t₀ t₁ h`, where `h : t₀ ≤ t₁` is `γ.truncate t₀ t₁` casted as a path from `γ.extend t₀` to `γ.extend t₁`. -/ def truncate_of_le {X : Type*} [topological_space X] {a b : X} (γ : path a b) {t₀ t₁ : ℝ} (h : t₀ ≤ t₁) : path (γ.extend t₀) (γ.extend t₁) := (γ.truncate t₀ t₁).cast (by rw min_eq_left h) rfl lemma truncate_range {X : Type*} [topological_space X] {a b : X} (γ : path a b) {t₀ t₁ : ℝ} : range (γ.truncate t₀ t₁) ⊆ range γ := begin rw ← γ.extend_range, simp only [range_subset_iff, set_coe.exists, set_coe.forall], intros x hx, simp only [has_coe_to_fun.coe, coe_fn, path.truncate, mem_range_self] end /-- For a path `γ`, `γ.truncate` gives a "continuous family of paths", by which we mean the uncurried function which maps `(t₀, t₁, s)` to `γ.truncate t₀ t₁ s` is continuous. -/ lemma truncate_continuous_family {X : Type*} [topological_space X] {a b : X} (γ : path a b) : continuous (λ x, γ.truncate x.1 x.2.1 x.2.2 : ℝ × ℝ × I → X) := γ.continuous_extend.comp (((continuous_subtype_coe.comp (continuous_snd.comp continuous_snd)).max continuous_fst).min (continuous_fst.comp continuous_snd)) /- TODO : When `continuity` gets quicker, change the proof back to : `begin` `simp only [has_coe_to_fun.coe, coe_fn, path.truncate],` `continuity,` `exact continuous_subtype_coe` `end` -/ lemma truncate_const_continuous_family {X : Type*} [topological_space X] {a b : X} (γ : path a b) (t : ℝ) : continuous ↿(γ.truncate t) := have key : continuous (λ x, (t, x) : ℝ × I → ℝ × ℝ × I) := continuous_const.prod_mk continuous_id, by convert γ.truncate_continuous_family.comp key @[simp] lemma truncate_self {X : Type*} [topological_space X] {a b : X} (γ : path a b) (t : ℝ) : γ.truncate t t = (path.refl $ γ.extend t).cast (by rw min_self) rfl := begin ext x, rw cast_coe, simp only [truncate, has_coe_to_fun.coe, coe_fn, refl, min, max], split_ifs with h₁ h₂; congr, linarith end @[simp] lemma truncate_zero_zero {X : Type*} [topological_space X] {a b : X} (γ : path a b) : γ.truncate 0 0 = (path.refl a).cast (by rw [min_self, γ.extend_zero]) γ.extend_zero := by convert γ.truncate_self 0; exact γ.extend_zero.symm @[simp] lemma truncate_one_one {X : Type*} [topological_space X] {a b : X} (γ : path a b) : γ.truncate 1 1 = (path.refl b).cast (by rw [min_self, γ.extend_one]) γ.extend_one := by convert γ.truncate_self 1; exact γ.extend_one.symm @[simp] lemma truncate_zero_one {X : Type*} [topological_space X] {a b : X} (γ : path a b) : γ.truncate 0 1 = γ.cast (by simp [zero_le_one, extend_zero]) (by simp) := begin ext x, rw cast_coe, have : ↑x ∈ (Icc 0 1 : set ℝ) := x.2, rw [truncate, coe_mk, max_eq_left this.1, min_eq_left this.2, extend_extends'] end end path /-! ### Being joined by a path -/ /-- The relation "being joined by a path". This is an equivalence relation. -/ def joined (x y : X) : Prop := nonempty (path x y) @[refl] lemma joined.refl (x : X) : joined x x := ⟨path.refl x⟩ /-- When two points are joined, choose some path from `x` to `y`. -/ def joined.some_path (h : joined x y) : path x y := nonempty.some h @[symm] lemma joined.symm {x y : X} (h : joined x y) : joined y x := ⟨h.some_path.symm⟩ @[trans] lemma joined.trans {x y z : X} (hxy : joined x y) (hyz : joined y z) : joined x z := ⟨hxy.some_path.trans hyz.some_path⟩ variables (X) /-- The setoid corresponding the equivalence relation of being joined by a continuous path. -/ def path_setoid : setoid X := { r := joined, iseqv := mk_equivalence _ joined.refl (λ x y, joined.symm) (λ x y z, joined.trans) } /-- The quotient type of points of a topological space modulo being joined by a continuous path. -/ def zeroth_homotopy := quotient (path_setoid X) instance : inhabited (zeroth_homotopy ℝ) := ⟨@quotient.mk ℝ (path_setoid ℝ) 0⟩ variables {X} /-! ### Being joined by a path inside a set -/ /-- The relation "being joined by a path in `F`". Not quite an equivalence relation since it's not reflexive for points that do not belong to `F`. -/ def joined_in (F : set X) (x y : X) : Prop := ∃ γ : path x y, ∀ t, γ t ∈ F variables {F : set X} lemma joined_in.mem (h : joined_in F x y) : x ∈ F ∧ y ∈ F := begin rcases h with ⟨γ, γ_in⟩, have : γ 0 ∈ F ∧ γ 1 ∈ F, by { split; apply γ_in }, simpa using this end lemma joined_in.source_mem (h : joined_in F x y) : x ∈ F := h.mem.1 lemma joined_in.target_mem (h : joined_in F x y) : y ∈ F := h.mem.2 /-- When `x` and `y` are joined in `F`, choose a path from `x` to `y` inside `F` -/ def joined_in.some_path (h : joined_in F x y) : path x y := classical.some h lemma joined_in.some_path_mem (h : joined_in F x y) (t : I) : h.some_path t ∈ F := classical.some_spec h t /-- If `x` and `y` are joined in the set `F`, then they are joined in the subtype `F`. -/ lemma joined_in.joined_subtype (h : joined_in F x y) : joined (⟨x, h.source_mem⟩ : F) (⟨y, h.target_mem⟩ : F) := ⟨{ to_fun := λ t, ⟨h.some_path t, h.some_path_mem t⟩, continuous_to_fun := by continuity, source' := by simp, target' := by simp }⟩ lemma joined_in.of_line {f : ℝ → X} (hf : continuous_on f I) (h₀ : f 0 = x) (h₁ : f 1 = y) (hF : f '' I ⊆ F) : joined_in F x y := ⟨path.of_line hf h₀ h₁, λ t, hF $ path.of_line_mem hf h₀ h₁ t⟩ lemma joined_in.joined (h : joined_in F x y) : joined x y := ⟨h.some_path⟩ lemma joined_in_iff_joined (x_in : x ∈ F) (y_in : y ∈ F) : joined_in F x y ↔ joined (⟨x, x_in⟩ : F) (⟨y, y_in⟩ : F) := ⟨λ h, h.joined_subtype, λ h, ⟨h.some_path.map continuous_subtype_coe, by simp⟩⟩ @[simp] lemma joined_in_univ : joined_in univ x y ↔ joined x y := by simp [joined_in, joined, exists_true_iff_nonempty] lemma joined_in.mono {U V : set X} (h : joined_in U x y) (hUV : U ⊆ V) : joined_in V x y := ⟨h.some_path, λ t, hUV (h.some_path_mem t)⟩ lemma joined_in.refl (h : x ∈ F) : joined_in F x x := ⟨path.refl x, λ t, h⟩ @[symm] lemma joined_in.symm (h : joined_in F x y) : joined_in F y x := begin cases h.mem with hx hy, simp [joined_in_iff_joined, *] at *, exact h.symm end lemma joined_in.trans (hxy : joined_in F x y) (hyz : joined_in F y z) : joined_in F x z := begin cases hxy.mem with hx hy, cases hyz.mem with hx hy, simp [joined_in_iff_joined, *] at *, exact hxy.trans hyz end /-! ### Path component -/ /-- The path component of `x` is the set of points that can be joined to `x`. -/ def path_component (x : X) := {y | joined x y} @[simp] lemma mem_path_component_self (x : X) : x ∈ path_component x := joined.refl x @[simp] lemma path_component.nonempty (x : X) : (path_component x).nonempty := ⟨x, mem_path_component_self x⟩ lemma mem_path_component_of_mem (h : x ∈ path_component y) : y ∈ path_component x := joined.symm h lemma path_component_symm : x ∈ path_component y ↔ y ∈ path_component x := ⟨λ h, mem_path_component_of_mem h, λ h, mem_path_component_of_mem h⟩ lemma path_component_congr (h : x ∈ path_component y) : path_component x = path_component y := begin ext z, split, { intro h', rw path_component_symm, exact (h.trans h').symm }, { intro h', rw path_component_symm at h' ⊢, exact h'.trans h }, end lemma path_component_subset_component (x : X) : path_component x ⊆ connected_component x := λ y h, (is_connected_range h.some_path.continuous).subset_connected_component ⟨0, by simp⟩ ⟨1, by simp⟩ /-- The path component of `x` in `F` is the set of points that can be joined to `x` in `F`. -/ def path_component_in (x : X) (F : set X) := {y | joined_in F x y} @[simp] lemma path_component_in_univ (x : X) : path_component_in x univ = path_component x := by simp [path_component_in, path_component, joined_in, joined, exists_true_iff_nonempty] lemma joined.mem_path_component (hyz : joined y z) (hxy : y ∈ path_component x) : z ∈ path_component x := hxy.trans hyz /-! ### Path connected sets -/ /-- A set `F` is path connected if it contains a point that can be joined to all other in `F`. -/ def is_path_connected (F : set X) : Prop := ∃ x ∈ F, ∀ {y}, y ∈ F → joined_in F x y lemma is_path_connected_iff_eq : is_path_connected F ↔ ∃ x ∈ F, path_component_in x F = F := begin split ; rintros ⟨x, x_in, h⟩ ; use [x, x_in], { ext y, exact ⟨λ hy, hy.mem.2, h⟩ }, { intros y y_in, rwa ← h at y_in }, end lemma is_path_connected.joined_in (h : is_path_connected F) : ∀ x y ∈ F, joined_in F x y := λ x y x_in y_in, let ⟨b, b_in, hb⟩ := h in (hb x_in).symm.trans (hb y_in) lemma is_path_connected_iff : is_path_connected F ↔ F.nonempty ∧ ∀ x y ∈ F, joined_in F x y := ⟨λ h, ⟨let ⟨b, b_in, hb⟩ := h in ⟨b, b_in⟩, h.joined_in⟩, λ ⟨⟨b, b_in⟩, h⟩, ⟨b, b_in, λ x x_in, h b x b_in x_in⟩⟩ lemma is_path_connected.image {Y : Type*} [topological_space Y] (hF : is_path_connected F) {f : X → Y} (hf : continuous f) : is_path_connected (f '' F) := begin rcases hF with ⟨x, x_in, hx⟩, use [f x, mem_image_of_mem f x_in], rintros _ ⟨y, y_in, rfl⟩, exact ⟨(hx y_in).some_path.map hf, λ t, ⟨_, (hx y_in).some_path_mem t, rfl⟩⟩, end lemma is_path_connected.mem_path_component (h : is_path_connected F) (x_in : x ∈ F) (y_in : y ∈ F) : y ∈ path_component x := (h.joined_in x y x_in y_in).joined lemma is_path_connected.subset_path_component (h : is_path_connected F) (x_in : x ∈ F) : F ⊆ path_component x := λ y y_in, h.mem_path_component x_in y_in lemma is_path_connected.union {U V : set X} (hU : is_path_connected U) (hV : is_path_connected V) (hUV : (U ∩ V).nonempty) : is_path_connected (U ∪ V) := begin rcases hUV with ⟨x, xU, xV⟩, use [x, or.inl xU], rintros y (yU | yV), { exact (hU.joined_in x y xU yU).mono (subset_union_left U V) }, { exact (hV.joined_in x y xV yV).mono (subset_union_right U V) }, end /-- If a set `W` is path-connected, then it is also path-connected when seen as a set in a smaller ambient type `U` (when `U` contains `W`). -/ lemma is_path_connected.preimage_coe {U W : set X} (hW : is_path_connected W) (hWU : W ⊆ U) : is_path_connected ((coe : U → X) ⁻¹' W) := begin rcases hW with ⟨x, x_in, hx⟩, use [⟨x, hWU x_in⟩, by simp [x_in]], rintros ⟨y, hyU⟩ hyW, exact ⟨(hx hyW).joined_subtype.some_path.map (continuous_inclusion hWU), by simp⟩ end lemma is_path_connected.exists_path_through_family {X : Type*} [topological_space X] {n : ℕ} {s : set X} (h : is_path_connected s) (p : fin (n+1) → X) (hp : ∀ i, p i ∈ s) : ∃ γ : path (p 0) (p n), (range γ ⊆ s) ∧ (∀ i, p i ∈ range γ) := begin let p' : ℕ → X := λ k, if h : k < n+1 then p ⟨k, h⟩ else p ⟨0, n.zero_lt_succ⟩, obtain ⟨γ, hγ⟩ : ∃ (γ : path (p' 0) (p' n)), (∀ i ≤ n, p' i ∈ range γ) ∧ range γ ⊆ s, { have hp' : ∀ i ≤ n, p' i ∈ s, { intros i hi, simp [p', nat.lt_succ_of_le hi, hp] }, clear_value p', clear hp p, induction n with n hn, { use path.refl (p' 0), { split, { rintros i hi, rw nat.le_zero_iff.mp hi, exact ⟨0, rfl⟩ }, { rw range_subset_iff, rintros x, exact hp' 0 (le_refl _) } } }, { rcases hn (λ i hi, hp' i $ nat.le_succ_of_le hi) with ⟨γ₀, hγ₀⟩, rcases h.joined_in (p' n) (p' $ n+1) (hp' n n.le_succ) (hp' (n+1) $ le_refl _) with ⟨γ₁, hγ₁⟩, let γ : path (p' 0) (p' $ n+1) := γ₀.trans γ₁, use γ, have range_eq : range γ = range γ₀ ∪ range γ₁ := γ₀.trans_range γ₁, split, { rintros i hi, by_cases hi' : i ≤ n, { rw range_eq, left, exact hγ₀.1 i hi' }, { rw [not_le, ← nat.succ_le_iff] at hi', have : i = n.succ := by linarith, rw this, use 1, exact γ.target } }, { rw range_eq, apply union_subset hγ₀.2, rw range_subset_iff, exact hγ₁ } } }, have hpp' : ∀ k < n+1, p k = p' k, { intros k hk, simp only [p', hk, dif_pos], congr, ext, rw fin.coe_coe_of_lt hk, norm_cast }, use γ.cast (hpp' 0 n.zero_lt_succ) (hpp' n n.lt_succ_self), simp only [γ.cast_coe], refine and.intro hγ.2 _, rintros ⟨i, hi⟩, convert hγ.1 i (nat.le_of_lt_succ hi), rw ← hpp' i hi, congr, ext, rw fin.coe_coe_of_lt hi, norm_cast end lemma is_path_connected.exists_path_through_family' {X : Type*} [topological_space X] {n : ℕ} {s : set X} (h : is_path_connected s) (p : fin (n+1) → X) (hp : ∀ i, p i ∈ s) : ∃ (γ : path (p 0) (p n)) (t : fin (n + 1) → I), (∀ t, γ t ∈ s) ∧ ∀ i, γ (t i) = p i := begin rcases h.exists_path_through_family p hp with ⟨γ, hγ⟩, rcases hγ with ⟨h₁, h₂⟩, simp only [range, mem_set_of_eq] at h₂, rw range_subset_iff at h₁, choose! t ht using h₂, exact ⟨γ, t, h₁, ht⟩ end /-! ### Path connected spaces -/ /-- A topological space is path-connected if it is non-empty and every two points can be joined by a continuous path. -/ class path_connected_space (X : Type*) [topological_space X] : Prop := (nonempty : nonempty X) (joined : ∀ x y : X, joined x y) attribute [instance, priority 50] path_connected_space.nonempty lemma path_connected_space_iff_zeroth_homotopy : path_connected_space X ↔ nonempty (zeroth_homotopy X) ∧ subsingleton (zeroth_homotopy X) := begin letI := path_setoid X, split, { introI h, refine ⟨(nonempty_quotient_iff _).mpr h.1, ⟨_⟩⟩, rintros ⟨x⟩ ⟨y⟩, exact quotient.sound (path_connected_space.joined x y) }, { unfold zeroth_homotopy, rintros ⟨h, h'⟩, resetI, exact ⟨(nonempty_quotient_iff _).mp h, λ x y, quotient.exact $ subsingleton.elim ⟦x⟧ ⟦y⟧⟩ }, end namespace path_connected_space variables [path_connected_space X] /-- Use path-connectedness to build a path between two points. -/ def some_path (x y : X) : path x y := nonempty.some (joined x y) end path_connected_space lemma is_path_connected_iff_path_connected_space : is_path_connected F ↔ path_connected_space F := begin rw is_path_connected_iff, split, { rintro ⟨⟨x, x_in⟩, h⟩, refine ⟨⟨⟨x, x_in⟩⟩, _⟩, rintros ⟨y, y_in⟩ ⟨z, z_in⟩, have H := h y z y_in z_in, rwa joined_in_iff_joined y_in z_in at H }, { rintros ⟨⟨x, x_in⟩, H⟩, refine ⟨⟨x, x_in⟩, λ y z y_in z_in, _⟩, rw joined_in_iff_joined y_in z_in, apply H } end lemma path_connected_space_iff_univ : path_connected_space X ↔ is_path_connected (univ : set X) := begin split, { introI h, inhabit X, refine ⟨default X, mem_univ _, _⟩, simpa using path_connected_space.joined (default X) }, { intro h, have h' := h.joined_in, cases h with x h, exact ⟨⟨x⟩, by simpa using h'⟩ }, end lemma path_connected_space_iff_eq : path_connected_space X ↔ ∃ x : X, path_component x = univ := by simp [path_connected_space_iff_univ, is_path_connected_iff_eq] @[priority 100] -- see Note [lower instance priority] instance path_connected_space.connected_space [path_connected_space X] : connected_space X := begin rw connected_space_iff_connected_component, rcases is_path_connected_iff_eq.mp (path_connected_space_iff_univ.mp ‹_›) with ⟨x, x_in, hx⟩, use x, rw ← univ_subset_iff, exact (by simpa using hx : path_component x = univ) ▸ path_component_subset_component x end namespace path_connected_space variables [path_connected_space X] lemma exists_path_through_family {n : ℕ} (p : fin (n+1) → X) : ∃ γ : path (p 0) (p n), (∀ i, p i ∈ range γ) := begin have : is_path_connected (univ : set X) := path_connected_space_iff_univ.mp (by apply_instance), rcases this.exists_path_through_family p (λ i, true.intro) with ⟨γ, -, h⟩, exact ⟨γ, h⟩ end lemma exists_path_through_family' {n : ℕ} (p : fin (n+1) → X) : ∃ (γ : path (p 0) (p n)) (t : fin (n + 1) → I), ∀ i, γ (t i) = p i := begin have : is_path_connected (univ : set X) := path_connected_space_iff_univ.mp (by apply_instance), rcases this.exists_path_through_family' p (λ i, true.intro) with ⟨γ, t, -, h⟩, exact ⟨γ, t, h⟩ end end path_connected_space /-! ### Locally path connected spaces -/ /-- A topological space is locally path connected, at every point, path connected neighborhoods form a neighborhood basis. -/ class loc_path_connected_space (X : Type*) [topological_space X] : Prop := (path_connected_basis : ∀ x : X, (𝓝 x).has_basis (λ s : set X, s ∈ 𝓝 x ∧ is_path_connected s) id) export loc_path_connected_space (path_connected_basis) lemma loc_path_connected_of_bases {p : ι → Prop} {s : X → ι → set X} (h : ∀ x, (𝓝 x).has_basis p (s x)) (h' : ∀ x i, p i → is_path_connected (s x i)) : loc_path_connected_space X := begin constructor, intro x, apply (h x).to_has_basis, { intros i pi, exact ⟨s x i, ⟨(h x).mem_of_mem pi, h' x i pi⟩, by refl⟩ }, { rintros U ⟨U_in, hU⟩, rcases (h x).mem_iff.mp U_in with ⟨i, pi, hi⟩, tauto } end lemma path_connected_space_iff_connected_space [loc_path_connected_space X] : path_connected_space X ↔ connected_space X := begin split, { introI h, apply_instance }, { introI hX, inhabit X, let x₀ := default X, rw path_connected_space_iff_eq, use x₀, refine eq_univ_of_nonempty_clopen (by simp) ⟨_, _⟩, { rw is_open_iff_mem_nhds, intros y y_in, rcases (path_connected_basis y).ex_mem with ⟨U, ⟨U_in, hU⟩⟩, apply mem_of_superset U_in, rw ← path_component_congr y_in, exact hU.subset_path_component (mem_of_mem_nhds U_in) }, { rw is_closed_iff_nhds, intros y H, rcases (path_connected_basis y).ex_mem with ⟨U, ⟨U_in, hU⟩⟩, rcases H U U_in with ⟨z, hz, hz'⟩, exact ((hU.joined_in z y hz $ mem_of_mem_nhds U_in).joined.mem_path_component hz') } }, end lemma path_connected_subset_basis [loc_path_connected_space X] {U : set X} (h : is_open U) (hx : x ∈ U) : (𝓝 x).has_basis (λ s : set X, s ∈ 𝓝 x ∧ is_path_connected s ∧ s ⊆ U) id := (path_connected_basis x).has_basis_self_subset (is_open.mem_nhds h hx) lemma loc_path_connected_of_is_open [loc_path_connected_space X] {U : set X} (h : is_open U) : loc_path_connected_space U := ⟨begin rintros ⟨x, x_in⟩, rw nhds_subtype_eq_comap, constructor, intros V, rw (has_basis.comap (coe : U → X) (path_connected_subset_basis h x_in)).mem_iff, split, { rintros ⟨W, ⟨W_in, hW, hWU⟩, hWV⟩, exact ⟨coe ⁻¹' W, ⟨⟨preimage_mem_comap W_in, hW.preimage_coe hWU⟩, hWV⟩⟩ }, { rintros ⟨W, ⟨W_in, hW⟩, hWV⟩, refine ⟨coe '' W, ⟨filter.image_coe_mem_of_mem_comap (is_open.mem_nhds h x_in) W_in, hW.image continuous_subtype_coe, subtype.coe_image_subset U W⟩, _⟩, rintros x ⟨y, ⟨y_in, hy⟩⟩, rw ← subtype.coe_injective hy, tauto }, end⟩ lemma is_open.is_connected_iff_is_path_connected [loc_path_connected_space X] {U : set X} (U_op : is_open U) : is_path_connected U ↔ is_connected U := begin rw [is_connected_iff_connected_space, is_path_connected_iff_path_connected_space], haveI := loc_path_connected_of_is_open U_op, exact path_connected_space_iff_connected_space end
1cd9e2a037cf2f47652a5c5cf1b4455a0749f53a
4727251e0cd73359b15b664c3170e5d754078599
/src/order/succ_pred/relation.lean
15a145eeb1af0b34e4ecd9dd5534680101ff283f
[ "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
6,675
lean
/- Copyright (c) 2022 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import order.succ_pred.basic /-! # Relations on types with a `succ_order` This file contains properties about relations on types with a `succ_order` and their closure operations (like the transitive closure). -/ open function order relation set section partial_succ variables {α : Type*} [partial_order α] [succ_order α] [is_succ_archimedean α] /-- For `n ≤ m`, `(n, m)` is in the reflexive-transitive closure of `~` if `i ~ succ i` for all `i` between `n` and `m`. -/ lemma refl_trans_gen_of_succ_of_le (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ico n m, r i (succ i)) (hnm : n ≤ m) : refl_trans_gen r n m := begin revert h, refine succ.rec _ _ hnm, { intros h, exact refl_trans_gen.refl }, { intros m hnm ih h, have : refl_trans_gen r n m := ih (λ i hi, h i ⟨hi.1, hi.2.trans_le $ le_succ m⟩), cases (le_succ m).eq_or_lt with hm hm, { rwa [← hm] }, exact this.tail (h m ⟨hnm, hm⟩) } end /-- For `m ≤ n`, `(n, m)` is in the reflexive-transitive closure of `~` if `succ i ~ i` for all `i` between `n` and `m`. -/ lemma refl_trans_gen_of_succ_of_ge (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ico m n, r (succ i) i) (hmn : m ≤ n) : refl_trans_gen r n m := by { rw [← refl_trans_gen_swap], exact refl_trans_gen_of_succ_of_le (swap r) h hmn } /-- For `n < m`, `(n, m)` is in the transitive closure of a relation `~` if `i ~ succ i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_succ_of_lt (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ico n m, r i (succ i)) (hnm : n < m) : trans_gen r n m := (refl_trans_gen_iff_eq_or_trans_gen.mp $ refl_trans_gen_of_succ_of_le r h hnm.le).resolve_left hnm.ne' /-- For `m < n`, `(n, m)` is in the transitive closure of a relation `~` if `succ i ~ i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_succ_of_gt (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ico m n, r (succ i) i) (hmn : m < n) : trans_gen r n m := (refl_trans_gen_iff_eq_or_trans_gen.mp $ refl_trans_gen_of_succ_of_ge r h hmn.le).resolve_left hmn.ne end partial_succ section linear_succ variables {α : Type*} [linear_order α] [succ_order α] [is_succ_archimedean α] /-- `(n, m)` is in the reflexive-transitive closure of `~` if `i ~ succ i` and `succ i ~ i` for all `i` between `n` and `m`. -/ lemma refl_trans_gen_of_succ (r : α → α → Prop) {n m : α} (h1 : ∀ i ∈ Ico n m, r i (succ i)) (h2 : ∀ i ∈ Ico m n, r (succ i) i) : refl_trans_gen r n m := (le_total n m).elim (refl_trans_gen_of_succ_of_le r h1) $ refl_trans_gen_of_succ_of_ge r h2 /-- For `n ≠ m`,`(n, m)` is in the transitive closure of a relation `~` if `i ~ succ i` and `succ i ~ i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_succ_of_ne (r : α → α → Prop) {n m : α} (h1 : ∀ i ∈ Ico n m, r i (succ i)) (h2 : ∀ i ∈ Ico m n, r (succ i) i) (hnm : n ≠ m) : trans_gen r n m := (refl_trans_gen_iff_eq_or_trans_gen.mp (refl_trans_gen_of_succ r h1 h2)).resolve_left hnm.symm /-- `(n, m)` is in the transitive closure of a reflexive relation `~` if `i ~ succ i` and `succ i ~ i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_succ_of_reflexive (r : α → α → Prop) {n m : α} (hr : reflexive r) (h1 : ∀ i ∈ Ico n m, r i (succ i)) (h2 : ∀ i ∈ Ico m n, r (succ i) i) : trans_gen r n m := begin rcases eq_or_ne m n with rfl|hmn, { exact trans_gen.single (hr m) }, exact trans_gen_of_succ_of_ne r h1 h2 hmn.symm end end linear_succ section partial_pred variables {α : Type*} [partial_order α] [pred_order α] [is_pred_archimedean α] /-- For `m ≤ n`, `(n, m)` is in the reflexive-transitive closure of `~` if `i ~ pred i` for all `i` between `n` and `m`. -/ lemma refl_trans_gen_of_pred_of_ge (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ioc m n, r i (pred i)) (hnm : m ≤ n) : refl_trans_gen r n m := @refl_trans_gen_of_succ_of_le αᵒᵈ _ _ _ r n m (λ x hx, h x ⟨hx.2, hx.1⟩) hnm /-- For `n ≤ m`, `(n, m)` is in the reflexive-transitive closure of `~` if `pred i ~ i` for all `i` between `n` and `m`. -/ lemma refl_trans_gen_of_pred_of_le (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ioc n m, r (pred i) i) (hmn : n ≤ m) : refl_trans_gen r n m := @refl_trans_gen_of_succ_of_ge αᵒᵈ _ _ _ r n m (λ x hx, h x ⟨hx.2, hx.1⟩) hmn /-- For `m < n`, `(n, m)` is in the transitive closure of a relation `~` for `n ≠ m` if `i ~ pred i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_pred_of_gt (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ioc m n, r i (pred i)) (hnm : m < n) : trans_gen r n m := @trans_gen_of_succ_of_lt αᵒᵈ _ _ _ r _ _ (λ x hx, h x ⟨hx.2, hx.1⟩) hnm /-- For `n < m`, `(n, m)` is in the transitive closure of a relation `~` for `n ≠ m` if `pred i ~ i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_pred_of_lt (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ioc n m, r (pred i) i) (hmn : n < m) : trans_gen r n m := @trans_gen_of_succ_of_gt αᵒᵈ _ _ _ r _ _ (λ x hx, h x ⟨hx.2, hx.1⟩) hmn end partial_pred section linear_pred variables {α : Type*} [linear_order α] [pred_order α] [is_pred_archimedean α] /-- `(n, m)` is in the reflexive-transitive closure of `~` if `i ~ pred i` and `pred i ~ i` for all `i` between `n` and `m`. -/ lemma refl_trans_gen_of_pred (r : α → α → Prop) {n m : α} (h1 : ∀ i ∈ Ioc m n, r i (pred i)) (h2 : ∀ i ∈ Ioc n m, r (pred i) i) : refl_trans_gen r n m := @refl_trans_gen_of_succ αᵒᵈ _ _ _ r n m (λ x hx, h1 x ⟨hx.2, hx.1⟩) (λ x hx, h2 x ⟨hx.2, hx.1⟩) /-- For `n ≠ m`, `(n, m)` is in the transitive closure of a relation `~` if `i ~ pred i` and `pred i ~ i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_pred_of_ne (r : α → α → Prop) {n m : α} (h1 : ∀ i ∈ Ioc m n, r i (pred i)) (h2 : ∀ i ∈ Ioc n m, r (pred i) i) (hnm : n ≠ m) : trans_gen r n m := @trans_gen_of_succ_of_ne αᵒᵈ _ _ _ r n m (λ x hx, h1 x ⟨hx.2, hx.1⟩) (λ x hx, h2 x ⟨hx.2, hx.1⟩) hnm /-- `(n, m)` is in the transitive closure of a reflexive relation `~` if `i ~ pred i` and `pred i ~ i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_pred_of_reflexive (r : α → α → Prop) {n m : α} (hr : reflexive r) (h1 : ∀ i ∈ Ioc m n, r i (pred i)) (h2 : ∀ i ∈ Ioc n m, r (pred i) i) : trans_gen r n m := @trans_gen_of_succ_of_reflexive αᵒᵈ _ _ _ r n m hr (λ x hx, h1 x ⟨hx.2, hx.1⟩) (λ x hx, h2 x ⟨hx.2, hx.1⟩) end linear_pred
387bf2684651e29263a0f01e52c0647026607e53
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/library/data/sigma.lean
2a287c6c7dbe95e993d1f4be124a0000bdce7d80
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
3,034
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad, Floris van Doorn Sigma types, aka dependent sum. -/ import logic.cast open inhabited sigma.ops override eq.ops namespace sigma universe variables u v variables {A A' : Type.{u}} {B : A → Type.{v}} {B' : A' → Type.{v}} definition unpack {C : (Σa, B a) → Type} {u : Σa, B a} (H : C ⟨u.1 , u.2⟩) : C u := destruct u (λx y H, H) H theorem dpair_heq {a : A} {a' : A'} {b : B a} {b' : B' a'} (HB : B == B') (Ha : a == a') (Hb : b == b') : ⟨a, b⟩ == ⟨a', b'⟩ := hcongr_arg4 @mk (type_eq_of_heq Ha) HB Ha Hb protected theorem heq {p : Σa : A, B a} {p' : Σa' : A', B' a'} (HB : B == B') : ∀(H₁ : p.1 == p'.1) (H₂ : p.2 == p'.2), p == p' := destruct p (take a₁ b₁, destruct p' (take a₂ b₂ H₁ H₂, dpair_heq HB H₁ H₂)) protected definition is_inhabited [instance] [H₁ : inhabited A] [H₂ : inhabited (B (default A))] : inhabited (sigma B) := inhabited.destruct H₁ (λa, inhabited.destruct H₂ (λb, inhabited.mk ⟨default A, b⟩)) theorem eq_rec_dpair_commute {C : Πa, B a → Type} {a a' : A} (H : a = a') (b : B a) (c : C a b) : eq.rec_on H ⟨b, c⟩ = ⟨eq.rec_on H b, eq.rec_on (dcongr_arg2 C H rfl) c⟩ := eq.drec_on H (dpair_eq rfl (!eq.rec_on_id⁻¹)) variables {C : Πa, B a → Type} {D : Πa b, C a b → Type} definition dtrip (a : A) (b : B a) (c : C a b) := ⟨a, b, c⟩ definition dquad (a : A) (b : B a) (c : C a b) (d : D a b c) := ⟨a, b, c, d⟩ definition pr1' [reducible] (x : Σ a, B a) := x.1 definition pr2' [reducible] (x : Σ a b, C a b) := x.2.1 definition pr3 [reducible] (x : Σ a b, C a b) := x.2.2 definition pr3' [reducible] (x : Σ a b c, D a b c) := x.2.2.1 definition pr4 [reducible] (x : Σ a b c, D a b c) := x.2.2.2 theorem dtrip_eq {a₁ a₂ : A} {b₁ : B a₁} {b₂ : B a₂} {c₁ : C a₁ b₁} {c₂ : C a₂ b₂} (H₁ : a₁ = a₂) (H₂ : eq.rec_on H₁ b₁ = b₂) (H₃ : cast (dcongr_arg2 C H₁ H₂) c₁ = c₂) : ⟨a₁, b₁, c₁⟩ = ⟨a₂, b₂, c₂⟩ := dcongr_arg3 dtrip H₁ H₂ H₃ theorem ndtrip_eq {A B : Type} {C : A → B → Type} {a₁ a₂ : A} {b₁ b₂ : B} {c₁ : C a₁ b₁} {c₂ : C a₂ b₂} (H₁ : a₁ = a₂) (H₂ : b₁ = b₂) (H₃ : cast (congr_arg2 C H₁ H₂) c₁ = c₂) : ⟨a₁, b₁, c₁⟩ = ⟨a₂, b₂, c₂⟩ := hdcongr_arg3 dtrip H₁ (heq_of_eq H₂) H₃ theorem ndtrip_equal {A B : Type} {C : A → B → Type} {p₁ p₂ : Σa b, C a b} : ∀(H₁ : pr1 p₁ = pr1 p₂) (H₂ : pr2' p₁ = pr2' p₂) (H₃ : eq.rec_on (congr_arg2 C H₁ H₂) (pr3 p₁) = pr3 p₂), p₁ = p₂ := destruct p₁ (take a₁ q₁, destruct q₁ (take b₁ c₁, destruct p₂ (take a₂ q₂, destruct q₂ (take b₂ c₂ H₁ H₂ H₃, ndtrip_eq H₁ H₂ H₃)))) end sigma
111d4334159120b2874ff548527ebf41ba412a4b
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/data/padics/padic_norm.lean
d61e5cf19d0765f44c7e44a77f47ef564a23a229
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
17,017
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 data.rat algebra.gcd_domain algebra.field_power import ring_theory.multiplicity tactic.ring import data.real.cau_seq import tactic.norm_cast /-! # p-adic norm This file defines the p-adic valuation and 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 (prime p) as a type class argument. ## References * [F. Q. Gouêva, *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 -/ universe u open nat attribute [class] nat.prime local infix `/.`:70 := rat.mk open multiplicity /-- For `p ≠ 1`, the p-adic valuation of an integer `z ≠ 0` is the largest natural number `n` such that p^n divides z. `padic_val_rat` defines the valuation of a rational `q` to be the valuation of `q.num` minus the valuation of `q.denom`. If `q = 0` or `p = 1`, then `padic_val_rat p q` defaults to 0. -/ def padic_val_rat (p : ℕ) (q : ℚ) : ℤ := if h : q ≠ 0 ∧ p ≠ 1 then (multiplicity (p : ℤ) q.num).get (multiplicity.finite_int_iff.2 ⟨h.2, rat.num_ne_zero_of_ne_zero h.1⟩) - (multiplicity (p : ℤ) q.denom).get (multiplicity.finite_int_iff.2 ⟨h.2, by exact_mod_cast rat.denom_ne_zero _⟩) else 0 /-- A simplification of the definition of `padic_val_rat p q` when `q ≠ 0` and `p` is prime. -/ lemma padic_val_rat_def (p : ℕ) [hp : p.prime] {q : ℚ} (hq : q ≠ 0) : padic_val_rat p q = (multiplicity (p : ℤ) q.num).get (finite_int_iff.2 ⟨hp.ne_one, rat.num_ne_zero_of_ne_zero hq⟩) - (multiplicity (p : ℤ) q.denom).get (finite_int_iff.2 ⟨hp.ne_one, by exact_mod_cast rat.denom_ne_zero _⟩) := dif_pos ⟨hq, hp.ne_one⟩ namespace padic_val_rat open multiplicity section padic_val_rat variables {p : ℕ} /-- `padic_val_rat p q` is symmetric in `q`. -/ @[simp] protected lemma neg (q : ℚ) : padic_val_rat p (-q) = padic_val_rat p q := begin unfold padic_val_rat, split_ifs, { simp [-add_comm]; refl }, { exfalso, simp * at * }, { exfalso, simp * at * }, { refl } end /-- `padic_val_rat p 1` is 0 for any `p`. -/ @[simp] protected lemma one : padic_val_rat p 1 = 0 := by unfold padic_val_rat; split_ifs; simp * /-- For `p ≠ 0, p ≠ 1, `padic_val_rat p p` is 1. -/ @[simp] lemma padic_val_rat_self (hp : 1 < p) : padic_val_rat p p = 1 := by unfold padic_val_rat; split_ifs; simp [*, nat.one_lt_iff_ne_zero_and_ne_one] at * /-- The p-adic value of an integer `z ≠ 0` is the multiplicity of `p` in `z`. -/ lemma padic_val_rat_of_int (z : ℤ) (hp : p ≠ 1) (hz : z ≠ 0) : padic_val_rat p (z : ℚ) = (multiplicity (p : ℤ) z).get (finite_int_iff.2 ⟨hp, hz⟩) := by rw [padic_val_rat, dif_pos]; simp *; refl end padic_val_rat section padic_val_rat open multiplicity variables (p : ℕ) [p_prime : nat.prime p] include p_prime /-- The multiplicity of `p : ℕ` in `a : ℤ` is finite exactly when `a ≠ 0`. -/ lemma finite_int_prime_iff {p : ℕ} [p_prime : p.prime] {a : ℤ} : finite (p : ℤ) a ↔ a ≠ 0 := by simp [finite_int_iff, ne.symm (ne_of_lt (p_prime.gt_one))] /-- A rewrite lemma for `padic_val_rat p q` when `q` is expressed in terms of `rat.mk`. -/ protected lemma defn {q : ℚ} {n d : ℤ} (hqz : q ≠ 0) (qdf : q = n /. d) : padic_val_rat p q = (multiplicity (p : ℤ) n).get (finite_int_iff.2 ⟨ne.symm $ ne_of_lt p_prime.gt_one, λ hn, by simp * at *⟩) - (multiplicity (p : ℤ) d).get (finite_int_iff.2 ⟨ne.symm $ ne_of_lt p_prime.gt_one, λ hd, by simp * at *⟩) := have hn : n ≠ 0, from rat.mk_num_ne_zero_of_ne_zero hqz qdf, have hd : d ≠ 0, from rat.mk_denom_ne_zero_of_ne_zero hqz qdf, let ⟨c, hc1, hc2⟩ := rat.num_denom_mk hn hd qdf in by rw [padic_val_rat, dif_pos]; simp [hc1, hc2, multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime), (ne.symm (ne_of_lt p_prime.gt_one)), hqz] /-- A rewrite lemma for `padic_val_rat p (q * r)` with conditions `q ≠ 0`, `r ≠ 0`. -/ protected lemma mul {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) : padic_val_rat p (q * r) = padic_val_rat p q + padic_val_rat p r := have q*r = (q.num * r.num) /. (↑q.denom * ↑r.denom), by rw_mod_cast rat.mul_num_denom, have hq' : q.num /. q.denom ≠ 0, by rw ← rat.num_denom q; exact hq, have hr' : r.num /. r.denom ≠ 0, by rw ← rat.num_denom r; exact hr, have hp' : _root_.prime (p : ℤ), from nat.prime_iff_prime_int.1 p_prime, begin rw [padic_val_rat.defn p (mul_ne_zero hq hr) this], conv_rhs { rw [rat.num_denom q, padic_val_rat.defn p hq', rat.num_denom r, padic_val_rat.defn p hr'] }, rw [multiplicity.mul' hp', multiplicity.mul' hp']; simp end /-- A rewrite lemma for `padic_val_rat p (q^k) with condition `q ≠ 0`. -/ protected lemma pow {q : ℚ} (hq : q ≠ 0) {k : ℕ} : padic_val_rat p (q ^ k) = k * padic_val_rat p q := by induction k; simp [*, padic_val_rat.mul _ hq (pow_ne_zero _ hq), _root_.pow_succ, add_mul] /-- A rewrite lemma for `padic_val_rat p (q⁻¹)` with condition `q ≠ 0`. -/ protected lemma inv {q : ℚ} (hq : q ≠ 0) : padic_val_rat p (q⁻¹) = -padic_val_rat p q := by rw [eq_neg_iff_add_eq_zero, ← padic_val_rat.mul p (inv_ne_zero hq) hq, inv_mul_cancel hq, padic_val_rat.one] /-- A rewrite lemma for `padic_val_rat p (q / r)` with conditions `q ≠ 0`, `r ≠ 0`. -/ protected lemma div {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) : padic_val_rat p (q / r) = padic_val_rat p q - padic_val_rat p r := by rw [div_eq_mul_inv, padic_val_rat.mul p hq (inv_ne_zero hr), padic_val_rat.inv p hr, sub_eq_add_neg] /-- A condition for `padic_val_rat p (n₁ / d₁) ≤ padic_val_rat p (n₂ / d₂), in terms of divisibility by `p^n`. -/ lemma padic_val_rat_le_padic_val_rat_iff {n₁ n₂ d₁ d₂ : ℤ} (hn₁ : n₁ ≠ 0) (hn₂ : n₂ ≠ 0) (hd₁ : d₁ ≠ 0) (hd₂ : d₂ ≠ 0) : padic_val_rat p (n₁ /. d₁) ≤ padic_val_rat p (n₂ /. d₂) ↔ ∀ (n : ℕ), ↑p ^ n ∣ n₁ * d₂ → ↑p ^ n ∣ n₂ * d₁ := have hf1 : finite (p : ℤ) (n₁ * d₂), from finite_int_prime_iff.2 (mul_ne_zero hn₁ hd₂), have hf2 : finite (p : ℤ) (n₂ * d₁), from finite_int_prime_iff.2 (mul_ne_zero hn₂ hd₁), by conv { to_lhs, rw [padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hn₁ hd₁) rfl, padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hn₂ hd₂) rfl, sub_le_iff_le_add', ← add_sub_assoc, le_sub_iff_add_le], norm_cast, rw [← multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime) hf1, add_comm, ← multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime) hf2, enat.get_le_get, multiplicity_le_multiplicity_iff] } /-- Sufficient conditions to show that the p-adic valuation of `q` is less than or equal to the p-adic vlauation of `q + r`. -/ theorem le_padic_val_rat_add_of_le {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) (hqr : q + r ≠ 0) (h : padic_val_rat p q ≤ padic_val_rat p r) : padic_val_rat p q ≤ padic_val_rat p (q + r) := have hqn : q.num ≠ 0, from rat.num_ne_zero_of_ne_zero hq, have hqd : (q.denom : ℤ) ≠ 0, by exact_mod_cast rat.denom_ne_zero _, have hrn : r.num ≠ 0, from rat.num_ne_zero_of_ne_zero hr, have hrd : (r.denom : ℤ) ≠ 0, by exact_mod_cast rat.denom_ne_zero _, have hqdv : q.num /. q.denom ≠ 0, from rat.mk_ne_zero_of_ne_zero hqn hqd, have hrdv : r.num /. r.denom ≠ 0, from rat.mk_ne_zero_of_ne_zero hrn hrd, have hqreq : q + r = (((q.num * r.denom + q.denom * r.num : ℤ)) /. (↑q.denom * ↑r.denom : ℤ)), from rat.add_num_denom _ _, have hqrd : q.num * ↑(r.denom) + ↑(q.denom) * r.num ≠ 0, from rat.mk_num_ne_zero_of_ne_zero hqr hqreq, begin conv_lhs { rw rat.num_denom q }, rw [hqreq, padic_val_rat_le_padic_val_rat_iff p hqn hqrd hqd (mul_ne_zero hqd hrd), ← multiplicity_le_multiplicity_iff, mul_left_comm, multiplicity.mul (nat.prime_iff_prime_int.1 p_prime), add_mul], rw [rat.num_denom q, rat.num_denom r, padic_val_rat_le_padic_val_rat_iff p hqn hrn hqd hrd, ← multiplicity_le_multiplicity_iff] at h, calc _ ≤ min (multiplicity ↑p (q.num * ↑(r.denom) * ↑(q.denom))) (multiplicity ↑p (↑(q.denom) * r.num * ↑(q.denom))) : (le_min (by rw [@multiplicity.mul _ _ _ _ (_ * _) _ (nat.prime_iff_prime_int.1 p_prime), add_comm]) (by rw [mul_assoc, @multiplicity.mul _ _ _ _ (q.denom : ℤ) (_ * _) (nat.prime_iff_prime_int.1 p_prime)]; exact add_le_add_left' h)) ... ≤ _ : min_le_multiplicity_add end /-- The minimum of the valuations of `q` and `r` is less than or equal to the valuation of `q + r`. -/ theorem min_le_padic_val_rat_add {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) (hqr : q + r ≠ 0) : min (padic_val_rat p q) (padic_val_rat p r) ≤ padic_val_rat p (q + r) := (le_total (padic_val_rat p q) (padic_val_rat p r)).elim (λ h, by rw [min_eq_left h]; exact le_padic_val_rat_add_of_le _ hq hr hqr h) (λ h, by rw [min_eq_right h, add_comm]; exact le_padic_val_rat_add_of_le _ hr hq (by rwa add_comm) h) end padic_val_rat end padic_val_rat /-- 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 section 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_fpow_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 : ℚ) : padic_norm p q ≥ 0 := if hq : q = 0 then by simp [hq] else begin unfold padic_norm; split_ifs, apply fpow_nonneg_of_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 image of `padic_norm p` is {0} ∪ {p^(-n) | n ∈ ℤ}. -/ protected theorem image {q : ℚ} (hq : q ≠ 0) : ∃ n : ℤ, padic_norm p q = p ^ (-n) := ⟨ (padic_val_rat p q), by simp [padic_norm, hq] ⟩ variable [hp : 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_fpow_of_nonzero p hq, apply fpow_ne_zero_of_ne_zero, exact_mod_cast ne_of_gt hp.pos end /-- `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, hp.gt_one] /-- 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 fpow_ne_zero_of_ne_zero, exact_mod_cast hp.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 [prime.ne_zero hp], by simp [padic_norm, *, padic_val_rat.mul, fpow_add this] /-- 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] else begin unfold padic_norm, rw [if_neg _], { refine fpow_le_one_of_nonpos _ _, { exact_mod_cast le_of_lt hp.gt_one, }, { rw [padic_val_rat_of_int _ hp.ne_one hz, 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 fpow_le_of_le, { exact_mod_cast le_of_lt hp.gt_one }, { 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 := le_total (padic_val_rat p q) (padic_val_rat p r) using [q r], exact nonarchimedean_aux p 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 p ... ≤ padic_norm p q + padic_norm p r : max_le_add_of_nonneg (padic_norm.nonneg p _) (padic_norm.nonneg p _) /-- 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 p 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 hle := le_total (padic_norm p r) (padic_norm p q) using [q r], have hlt : padic_norm p r < padic_norm p q, from lt_of_le_of_ne hle hne.symm, 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 p ... = 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 p }, { rw max_eq_left_of_lt hlt, assumption } 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 p, abv_eq_zero := begin intros, constructor; intro, { apply zero_of_padic_norm_eq_zero p, assumption }, { simp [*] } end, abv_add := padic_norm.triangle_ineq p, abv_mul := padic_norm.mul p } /-- If `p^n` divides an integer `z`, then the p-adic norm of `z` is at most `p^(-n)`. -/ lemma le_of_dvd {n : ℕ} {z : ℤ} (hd : ↑(p^n) ∣ z) : padic_norm p z ≤ ↑p ^ (-n : ℤ) := begin unfold padic_norm, split_ifs with hz hz, { apply fpow_nonneg_of_nonneg, exact_mod_cast le_of_lt hp.pos }, { apply fpow_le_of_le, exact_mod_cast le_of_lt hp.gt_one, apply neg_le_neg, rw padic_val_rat_of_int _ hp.ne_one _, { norm_cast, rw [← enat.coe_le_coe, enat.coe_get], apply multiplicity.le_multiplicity_of_pow_dvd, exact_mod_cast hd }, { exact_mod_cast hz }} end end padic_norm end padic_norm
6f1c572e752ff4621843867823a37ff728a8ea94
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/linear_algebra/multilinear.lean
92c547b6d03beb771de6551727c2ccabeb2008ee
[ "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
23,444
lean
/- Copyright (c) 2020 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 linear_algebra.basic /-! # Multilinear maps We define multilinear maps as maps from `Π(i : ι), M₁ i` to `M₂` which are linear in each coordinate. Here, `M₁ i` and `M₂` are modules over a ring `R`, and `ι` is an arbitrary type (although some statements will require it to be a fintype). This space, denoted by `multilinear_map R M₁ M₂`, inherits a module structure by pointwise addition and multiplication. ## Main definitions * `multilinear_map R M₁ M₂` is the space of multilinear maps from `Π(i : ι), M₁ i` to `M₂`. * `f.map_smul` is the multiplicativity of the multilinear map `f` along each coordinate. * `f.map_add` is the additivity of the multilinear map `f` along each coordinate. * `f.map_smul_univ` expresses the multiplicativity of `f` over all coordinates at the same time, writing `f (λi, c i • m i)` as `univ.prod c • f m`. * `f.map_add_univ` expresses the additivity of `f` over all coordinates at the same time, writing `f (m + m')` as the sum over all subsets `s` of `ι` of `f (s.piecewise m m')`. We also register isomorphisms corresponding to currying or uncurrying variables, transforming a multilinear function `f` on `n+1` variables into a linear function taking values in multilinear functions in `n` variables, and into a multilinear function in `n` variables taking values in linear functions. These operations are called `f.curry_left` and `f.curry_right` respectively (with inverses `f.uncurry_left` and `f.uncurry_right`). These operations induce linear equivalences between spaces of multilinear functions in `n+1` variables and spaces of linear functions into multilinear functions in `n` variables (resp. multilinear functions in `n` variables taking values in linear functions), called respectively `multilinear_curry_left_equiv` and `multilinear_curry_right_equiv`. ## Implementation notes Expressing that a map is linear along the `i`-th coordinate when all other coordinates are fixed can be done in two (equivalent) different ways: * fixing a vector `m : Π(j : ι - i), M₁ j.val`, and then choosing separately the `i`-th coordinate * fixing a vector `m : Πj, M₁ j`, and then modifying its `i`-th coordinate The second way is more artificial as the value of `m` at `i` is not relevant, but it has the advantage of avoiding subtype inclusion issues. This is the definition we use, based on `function.update` that allows to change the value of `m` at `i`. -/ open function fin set universes u v v' v₁ v₂ v₃ w u' variables {R : Type u} {ι : Type u'} {n : ℕ} {M : fin n.succ → Type v} {M₁ : ι → Type v₁} {M₂ : Type v₂} {M₃ : Type v₃} {M' : Type v'} [decidable_eq ι] /-- Multilinear maps over the ring `R`, from `Πi, M₁ i` to `M₂` where `M₁ i` and `M₂` are modules over `R`. -/ structure multilinear_map (R : Type u) {ι : Type u'} (M₁ : ι → Type v) (M₂ : Type w) [decidable_eq ι] [ring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [∀i, module R (M₁ i)] [module R M₂] := (to_fun : (Πi, M₁ i) → M₂) (add : ∀(m : Πi, M₁ i) (i : ι) (x y : M₁ i), to_fun (update m i (x + y)) = to_fun (update m i x) + to_fun (update m i y)) (smul : ∀(m : Πi, M₁ i) (i : ι) (c : R) (x : M₁ i), to_fun (update m i (c • x)) = c • to_fun (update m i x)) namespace multilinear_map section ring variables [ring R] [∀i, add_comm_group (M i)] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [add_comm_group M₃] [add_comm_group M'] [∀i, module R (M i)] [∀i, module R (M₁ i)] [module R M₂] [module R M₃] [module R M'] (f f' : multilinear_map R M₁ M₂) instance : has_coe_to_fun (multilinear_map R M₁ M₂) := ⟨_, to_fun⟩ @[ext] theorem ext {f f' : multilinear_map R M₁ M₂} (H : ∀ x, f x = f' x) : f = f' := by cases f; cases f'; congr'; exact funext H @[simp] lemma map_add (m : Πi, M₁ i) (i : ι) (x y : M₁ i) : f (update m i (x + y)) = f (update m i x) + f (update m i y) := f.add m i x y @[simp] lemma map_smul (m : Πi, M₁ i) (i : ι) (c : R) (x : M₁ i) : f (update m i (c • x)) = c • f (update m i x) := f.smul m i c x @[simp] lemma map_sub (m : Πi, M₁ i) (i : ι) (x y : M₁ i) : f (update m i (x - y)) = f (update m i x) - f (update m i y) := by { simp only [map_add, add_left_inj, sub_eq_add_neg, (neg_one_smul R y).symm, map_smul], simp } lemma map_coord_zero {m : Πi, M₁ i} (i : ι) (h : m i = 0) : f m = 0 := begin have : (0 : R) • (0 : M₁ i) = 0, by simp, rw [← update_eq_self i m, h, ← this, f.map_smul, zero_smul] end @[simp] lemma map_zero [nonempty ι] : f 0 = 0 := begin obtain ⟨i, _⟩ : ∃i:ι, i ∈ set.univ := set.exists_mem_of_nonempty ι, exact map_coord_zero f i rfl end instance : has_add (multilinear_map R M₁ M₂) := ⟨λf f', ⟨λx, f x + f' x, λm i x y, by simp [add_left_comm], λm i c x, by simp [smul_add]⟩⟩ @[simp] lemma add_apply (m : Πi, M₁ i) : (f + f') m = f m + f' m := rfl instance : has_neg (multilinear_map R M₁ M₂) := ⟨λ f, ⟨λ m, - f m, λm i x y, by simp [add_comm], λm i c x, by simp⟩⟩ @[simp] lemma neg_apply (m : Πi, M₁ i) : (-f) m = - (f m) := rfl instance : has_zero (multilinear_map R M₁ M₂) := ⟨⟨λ _, 0, λm i x y, by simp, λm i c x, by simp⟩⟩ instance : inhabited (multilinear_map R M₁ M₂) := ⟨0⟩ @[simp] lemma zero_apply (m : Πi, M₁ i) : (0 : multilinear_map R M₁ M₂) m = 0 := rfl instance : add_comm_group (multilinear_map R M₁ M₂) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext; simp [add_comm, add_left_comm] /-- If `f` is a multilinear map, then `f.to_linear_map m i` is the linear map obtained by fixing all coordinates but `i` equal to those of `m`, and varying the `i`-th coordinate. -/ def to_linear_map (m : Πi, M₁ i) (i : ι) : M₁ i →ₗ[R] M₂ := { to_fun := λx, f (update m i x), add := λx y, by simp, smul := λc x, by simp } /-- The cartesian product of two multilinear maps, as a multilinear map. -/ def prod (f : multilinear_map R M₁ M₂) (g : multilinear_map R M₁ M₃) : multilinear_map R M₁ (M₂ × M₃) := { to_fun := λ m, (f m, g m), add := λ m i x y, by simp, smul := λ m i c x, by simp } /-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the additivity of a multilinear map along the first variable. -/ lemma cons_add (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (x y : M 0) : f (cons (x+y) m) = f (cons x m) + f (cons y m) := by rw [← update_cons_zero x m (x+y), f.map_add, update_cons_zero, update_cons_zero] /-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the multiplicativity of a multilinear map along the first variable. -/ lemma cons_smul (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (c : R) (x : M 0) : f (cons (c • x) m) = c • f (cons x m) := by rw [← update_cons_zero x m (c • x), f.map_smul, update_cons_zero] /-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `snoc`, one can express directly the additivity of a multilinear map along the first variable. -/ lemma snoc_add (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.cast_succ) (x y : M (last n)) : f (snoc m (x+y)) = f (snoc m x) + f (snoc m y) := by rw [← update_snoc_last x m (x+y), f.map_add, update_snoc_last, update_snoc_last] /-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the multiplicativity of a multilinear map along the first variable. -/ lemma snoc_smul (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.cast_succ) (c : R) (x : M (last n)) : f (snoc m (c • x)) = c • f (snoc m x) := by rw [← update_snoc_last x m (c • x), f.map_smul, update_snoc_last] /-- If `g` is multilinear and `f` is linear, then `g (f m₁, ..., f mₙ)` is again a multilinear function, that we call `g.comp_linear_map f`. -/ def comp_linear_map (g : multilinear_map R (λ (i : ι), M₂) M₃) (f : M' →ₗ[R] M₂) : multilinear_map R (λ (i : ι), M') M₃ := { to_fun := λ m, g (f ∘ m), add := λ m i x y, by simp [comp_update], smul := λ m i c x, by simp [comp_update] } end ring section comm_ring variables [comm_ring R] [∀i, add_comm_group (M₁ i)] [∀i, add_comm_group (M i)] [add_comm_group M₂] [∀i, module R (M i)] [∀i, module R (M₁ i)] [module R M₂] (f f' : multilinear_map R M₁ M₂) /-- If one multiplies by `c i` the coordinates in a finset `s`, then the image under a multilinear map is multiplied by `s.prod c`. This is mainly an auxiliary statement to prove the result when `s = univ`, given in `map_smul_univ`, although it can be useful in its own right as it does not require the index set `ι` to be finite. -/ lemma map_piecewise_smul (c : ι → R) (m : Πi, M₁ i) (s : finset ι) : f (s.piecewise (λi, c i • m i) m) = s.prod c • f m := begin refine s.induction_on (by simp) _, assume j s j_not_mem_s Hrec, have A : function.update (s.piecewise (λi, c i • m i) m) j (m j) = s.piecewise (λi, c i • m i) m, { ext i, by_cases h : i = j, { rw h, simp [j_not_mem_s] }, { simp [h] } }, rw [s.piecewise_insert, f.map_smul, A, Hrec], simp [j_not_mem_s, mul_smul] end /-- Multiplicativity of a multilinear map along all coordinates at the same time, writing `f (λi, c i • m i)` as `univ.prod c • f m`. -/ lemma map_smul_univ [fintype ι] (c : ι → R) (m : Πi, M₁ i) : f (λi, c i • m i) = finset.univ.prod c • f m := by simpa using map_piecewise_smul f c m finset.univ /-- If one adds to a vector `m'` another vector `m`, but only for coordinates in a finset `t`, then the image under a multilinear map `f` is the sum of `f (s.piecewise m m')` along all subsets `s` of `t`. This is mainly an auxiliary statement to prove the result when `t = univ`, given in `map_add_univ`, although it can be useful in its own right as it does not require the index set `ι` to be finite.-/ lemma map_piecewise_add (m m' : Πi, M₁ i) (t : finset ι) : f (t.piecewise (m + m') m') = t.powerset.sum (λs, f (s.piecewise m m')) := begin revert m', refine finset.induction_on t (by simp) _, assume i t hit Hrec m', have A : (insert i t).piecewise (m + m') m' = update (t.piecewise (m + m') m') i (m i + m' i) := t.piecewise_insert _ _ _, have B : update (t.piecewise (m + m') m') i (m' i) = t.piecewise (m + m') m', { ext j, by_cases h : j = i, { rw h, simp [hit] }, { simp [h] } }, let m'' := update m' i (m i), have C : update (t.piecewise (m + m') m') i (m i) = t.piecewise (m + m'') m'', { ext j, by_cases h : j = i, { rw h, simp [m'', hit] }, { by_cases h' : j ∈ t; simp [h, hit, m'', h'] } }, rw [A, f.map_add, B, C, finset.sum_powerset_insert hit, Hrec, Hrec, add_comm], congr' 1, apply finset.sum_congr rfl (λs hs, _), have : (insert i s).piecewise m m' = s.piecewise m m'', { ext j, by_cases h : j = i, { rw h, simp [m'', finset.not_mem_of_mem_powerset_of_not_mem hs hit] }, { by_cases h' : j ∈ s; simp [h, m'', h'] } }, rw this end /-- Additivity of a multilinear map along all coordinates at the same time, writing `f (m + m')` as the sum of `f (s.piecewise m m')` over all sets `s`. -/ lemma map_add_univ [fintype ι] (m m' : Πi, M₁ i) : f (m + m') = (finset.univ : finset (finset ι)).sum (λs, f (s.piecewise m m')) := by simpa using f.map_piecewise_add m m' finset.univ instance : has_scalar R (multilinear_map R M₁ M₂) := ⟨λ c f, ⟨λ m, c • f m, λm i x y, by simp [smul_add], λl i x d, by simp [smul_smul, mul_comm]⟩⟩ @[simp] lemma smul_apply (c : R) (m : Πi, M₁ i) : (c • f) m = c • f m := rfl /-- The space of multilinear maps is a module over `R`, for the pointwise addition and scalar multiplication. -/ instance : module R (multilinear_map R M₁ M₂) := module.of_core $ by refine { smul := (•), ..}; intros; ext; simp [smul_add, add_smul, smul_smul] variables (R ι) /-- The canonical multilinear map on `R^ι` when `ι` is finite, associating to `m` the product of all the `m i` (multiplied by a fixed reference element `z` in the target module) -/ protected def mk_pi_ring [fintype ι] (z : M₂) : multilinear_map R (λ(i : ι), R) M₂ := { to_fun := λm, finset.univ.prod m • z, add := λ m i x y, by simp [finset.prod_update_of_mem, add_mul, add_smul], smul := λ m i c x, by { rw [smul_eq_mul], simp [finset.prod_update_of_mem, smul_smul, mul_assoc] } } variables {R ι} @[simp] lemma mk_pi_ring_apply [fintype ι] (z : M₂) (m : ι → R) : (multilinear_map.mk_pi_ring R ι z : (ι → R) → M₂) m = finset.univ.prod m • z := rfl lemma mk_pi_ring_apply_one_eq_self [fintype ι] (f : multilinear_map R (λ(i : ι), R) M₂) : multilinear_map.mk_pi_ring R ι (f (λi, 1)) = f := begin ext m, have : m = (λi, m i • 1), by { ext j, simp }, conv_rhs { rw [this, f.map_smul_univ] }, refl end variables (R ι M₂) /-- When `ι` is finite, multilinear maps on `R^ι` with values in `M₂` are in bijection with `M₂`, as such a multilinear map is completely determined by its value on the constant vector made of ones. We register this bijection as a linear equivalence in `multilinear_map.pi_ring_equiv`. -/ protected def pi_ring_equiv [fintype ι] : M₂ ≃ₗ[R] (multilinear_map R (λ(i : ι), R) M₂) := { to_fun := λ z, multilinear_map.mk_pi_ring R ι z, inv_fun := λ f, f (λi, 1), add := λ z z', by { ext m, simp [smul_add] }, smul := λ c z, by { ext m, simp [smul_smul, mul_comm] }, left_inv := λ z, by simp, right_inv := λ f, f.mk_pi_ring_apply_one_eq_self } end comm_ring end multilinear_map namespace linear_map variables [ring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [add_comm_group M₃] [∀i, module R (M₁ i)] [module R M₂] [module R M₃] /-- Composing a multilinear map with a linear map gives again a multilinear map. -/ def comp_multilinear_map (g : M₂ →ₗ[R] M₃) (f : multilinear_map R M₁ M₂) : multilinear_map R M₁ M₃ := { to_fun := λ m, g (f m), add := λ m i x y, by simp, smul := λ m i c x, by simp } end linear_map section currying /-! ### Currying We associate to a multilinear map in `n+1` variables (i.e., based on `fin n.succ`) two curried functions, named `f.curry_left` (which is a linear map on `E 0` taking values in multilinear maps in `n` variables) and `f.curry_right` (wich is a multilinear map in `n` variables taking values in linear maps on `E 0`). In both constructions, the variable that is singled out is `0`, to take advantage of the operations `cons` and `tail` on `fin n`. The inverse operations are called `uncurry_left` and `uncurry_right`. We also register linear equiv versions of these correspondences, in `multilinear_curry_left_equiv` and `multilinear_curry_right_equiv`. -/ open multilinear_map variables {R M M₂} [comm_ring R] [∀i, add_comm_group (M i)] [add_comm_group M'] [add_comm_group M₂] [∀i, module R (M i)] [module R M'] [module R M₂] /-! #### Left currying -/ /-- Given a linear map `f` from `M 0` to multilinear maps on `n` variables, construct the corresponding multilinear map on `n+1` variables obtained by concatenating the variables, given by `m ↦ f (m 0) (tail m)`-/ def linear_map.uncurry_left (f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) : multilinear_map R M M₂ := { to_fun := λm, f (m 0) (tail m), add := λm i x y, begin by_cases h : i = 0, { revert x y, rw h, assume x y, rw [update_same, update_same, update_same, f.map_add, add_apply, tail_update_zero, tail_update_zero, tail_update_zero] }, { rw [update_noteq (ne.symm h), update_noteq (ne.symm h), update_noteq (ne.symm h)], revert x y, rw ← succ_pred i h, assume x y, rw [tail_update_succ, map_add, tail_update_succ, tail_update_succ] } end, smul := λm i c x, begin by_cases h : i = 0, { revert x, rw h, assume x, rw [update_same, update_same, tail_update_zero, tail_update_zero, ← smul_apply, f.map_smul] }, { rw [update_noteq (ne.symm h), update_noteq (ne.symm h)], revert x, rw ← succ_pred i h, assume x, rw [tail_update_succ, tail_update_succ, map_smul] } end } @[simp] lemma linear_map.uncurry_left_apply (f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) (m : Πi, M i) : f.uncurry_left m = f (m 0) (tail m) := rfl /-- Given a multilinear map `f` in `n+1` variables, split the first variable to obtain a linear map into multilinear maps in `n` variables, given by `x ↦ (m ↦ f (cons x m))`. -/ def multilinear_map.curry_left (f : multilinear_map R M M₂) : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂) := { to_fun := λx, { to_fun := λm, f (cons x m), add := λm i y y', by simp, smul := λm i y c, by simp }, add := λx y, by { ext m, exact cons_add f m x y }, smul := λc x, by { ext m, exact cons_smul f m c x } } @[simp] lemma multilinear_map.curry_left_apply (f : multilinear_map R M M₂) (x : M 0) (m : Π(i : fin n), M i.succ) : f.curry_left x m = f (cons x m) := rfl @[simp] lemma linear_map.curry_uncurry_left (f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) : f.uncurry_left.curry_left = f := begin ext m x, simp only [tail_cons, linear_map.uncurry_left_apply, multilinear_map.curry_left_apply], rw cons_zero end @[simp] lemma multilinear_map.uncurry_curry_left (f : multilinear_map R M M₂) : f.curry_left.uncurry_left = f := by { ext m, simp } variables (R M M₂) /-- The space of multilinear maps on `Π(i : fin (n+1)), M i` is canonically isomorphic to the space of linear maps from `M 0` to the space of multilinear maps on `Π(i : fin n), M i.succ `, by separating the first variable. We register this isomorphism as a linear isomorphism in `multilinear_curry_left_equiv R M M₂`. The direct and inverse maps are given by `f.uncurry_left` and `f.curry_left`. Use these unless you need the full framework of linear equivs. -/ def multilinear_curry_left_equiv : (M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) ≃ₗ[R] (multilinear_map R M M₂) := { to_fun := linear_map.uncurry_left, add := λf₁ f₂, by { ext m, refl }, smul := λc f, by { ext m, refl }, inv_fun := multilinear_map.curry_left, left_inv := linear_map.curry_uncurry_left, right_inv := multilinear_map.uncurry_curry_left } variables {R M M₂} /-! #### Right currying -/ /-- Given a multilinear map `f` in `n` variables to the space of linear maps from `M (last n)` to `M₂`, construct the corresponding multilinear map on `n+1` variables obtained by concatenating the variables, given by `m ↦ f (init m) (m (last n))`-/ def multilinear_map.uncurry_right (f : (multilinear_map R (λ(i : fin n), M i.cast_succ) (M (last n) →ₗ[R] M₂))) : multilinear_map R M M₂ := { to_fun := λm, f (init m) (m (last n)), add := λm i x y, begin by_cases h : i.val < n, { have : last n ≠ i := ne.symm (ne_of_lt h), rw [update_noteq this, update_noteq this, update_noteq this], revert x y, rw [(cast_succ_cast_lt i h).symm], assume x y, rw [init_update_cast_succ, map_add, init_update_cast_succ, init_update_cast_succ, linear_map.add_apply] }, { revert x y, rw eq_last_of_not_lt h, assume x y, rw [init_update_last, init_update_last, init_update_last, update_same, update_same, update_same, linear_map.map_add] } end, smul := λm i c x, begin by_cases h : i.val < n, { have : last n ≠ i := ne.symm (ne_of_lt h), rw [update_noteq this, update_noteq this], revert x, rw [(cast_succ_cast_lt i h).symm], assume x, rw [init_update_cast_succ, init_update_cast_succ, map_smul, linear_map.smul_apply] }, { revert x, rw eq_last_of_not_lt h, assume x, rw [update_same, update_same, init_update_last, init_update_last, linear_map.map_smul] } end } @[simp] lemma multilinear_map.uncurry_right_apply (f : (multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂))) (m : Πi, M i) : f.uncurry_right m = f (init m) (m (last n)) := rfl /-- Given a multilinear map `f` in `n+1` variables, split the last variable to obtain a multilinear map in `n` variables taking values in linear maps from `M (last n)` to `M₂`, given by `m ↦ (x ↦ f (snoc m x))`. -/ def multilinear_map.curry_right (f : multilinear_map R M M₂) : multilinear_map R (λ(i : fin n), M (fin.cast_succ i)) ((M (last n)) →ₗ[R] M₂) := { to_fun := λm, { to_fun := λx, f (snoc m x), add := λx y, by rw f.snoc_add, smul := λc x, by rw f.snoc_smul }, add := λm i x y, begin ext z, change f (snoc (update m i (x + y)) z) = f (snoc (update m i x) z) + f (snoc (update m i y) z), rw [snoc_update, snoc_update, snoc_update, f.map_add] end, smul := λm i c x, begin ext z, change f (snoc (update m i (c • x)) z) = c • f (snoc (update m i x) z), rw [snoc_update, snoc_update, f.map_smul] end } @[simp] lemma multilinear_map.curry_right_apply (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.cast_succ) (x : M (last n)) : f.curry_right m x = f (snoc m x) := rfl @[simp] lemma multilinear_map.curry_uncurry_right (f : (multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂))) : f.uncurry_right.curry_right = f := begin ext m x, simp only [snoc_last, multilinear_map.curry_right_apply, multilinear_map.uncurry_right_apply], rw init_snoc end @[simp] lemma multilinear_map.uncurry_curry_right (f : multilinear_map R M M₂) : f.curry_right.uncurry_right = f := by { ext m, simp } variables (R M M₂) /-- The space of multilinear maps on `Π(i : fin (n+1)), M i` is canonically isomorphic to the space of linear maps from the space of multilinear maps on `Π(i : fin n), M i.cast_succ` to the space of linear maps on `M (last n)`, by separating the last variable. We register this isomorphism as a linear isomorphism in `multilinear_curry_right_equiv R M M₂`. The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these unless you need the full framework of linear equivs. -/ def multilinear_curry_right_equiv : (multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂)) ≃ₗ[R] (multilinear_map R M M₂) := { to_fun := multilinear_map.uncurry_right, add := λf₁ f₂, by { ext m, refl }, smul := λc f, by { ext m, rw [smul_apply], refl }, inv_fun := multilinear_map.curry_right, left_inv := multilinear_map.curry_uncurry_right, right_inv := multilinear_map.uncurry_curry_right } end currying
0f5bfa6990d70548765021f30fe306bac90582b4
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/data/polynomial/monic.lean
e51b2507b3bbcd7aa4f03f979ea1dfaac9f21290
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,782
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.reverse import algebra.associated /-! # Theory of monic polynomials We give several tools for proving that polynomials are monic, e.g. `monic_mul`, `monic_map`. -/ noncomputable theory open finset open_locale big_operators classical namespace polynomial universes u v y variables {R : Type u} {S : Type v} {a b : R} {m n : ℕ} {ι : Type y} section semiring variables [semiring R] {p q r : polynomial R} lemma monic.as_sum {p : polynomial R} (hp : p.monic) : p = X^(p.nat_degree) + (∑ i in range p.nat_degree, C (p.coeff i) * X^i) := begin conv_lhs { rw [p.as_sum_range_C_mul_X_pow, sum_range_succ_comm] }, suffices : C (p.coeff p.nat_degree) = 1, { rw [this, one_mul] }, exact congr_arg C hp end lemma ne_zero_of_monic_of_zero_ne_one (hp : monic p) (h : (0 : R) ≠ 1) : p ≠ 0 := mt (congr_arg leading_coeff) $ by rw [monic.def.1 hp, leading_coeff_zero]; cc lemma ne_zero_of_ne_zero_of_monic (hp : p ≠ 0) (hq : monic q) : q ≠ 0 := begin intro h, rw [h, monic.def, leading_coeff_zero] at hq, rw [← mul_one p, ← C_1, ← hq, C_0, mul_zero] at hp, exact hp rfl end lemma monic_map [semiring S] (f : R →+* S) (hp : monic p) : monic (p.map f) := if h : (0 : S) = 1 then by haveI := subsingleton_of_zero_eq_one h; exact subsingleton.elim _ _ else have f (leading_coeff p) ≠ 0, by rwa [show _ = _, from hp, f.map_one, ne.def, eq_comm], by begin rw [monic, leading_coeff, coeff_map], suffices : p.coeff (map f p).nat_degree = 1, simp [this], suffices : (map f p).nat_degree = p.nat_degree, rw this, exact hp, rwa nat_degree_eq_of_degree_eq (degree_map_eq_of_leading_coeff_ne_zero f _) end lemma monic_C_mul_of_mul_leading_coeff_eq_one [nontrivial R] {b : R} (hp : b * p.leading_coeff = 1) : monic (C b * p) := by rw [monic, leading_coeff_mul' _]; simp [leading_coeff_C b, hp] lemma monic_mul_C_of_leading_coeff_mul_eq_one [nontrivial R] {b : R} (hp : p.leading_coeff * b = 1) : monic (p * C b) := by rw [monic, leading_coeff_mul' _]; simp [leading_coeff_C b, hp] theorem monic_of_degree_le (n : ℕ) (H1 : degree p ≤ n) (H2 : coeff p n = 1) : monic p := decidable.by_cases (assume H : degree p < n, eq_of_zero_eq_one (H2 ▸ (coeff_eq_zero_of_degree_lt H).symm) _ _) (assume H : ¬degree p < n, by rwa [monic, leading_coeff, nat_degree, (lt_or_eq_of_le H1).resolve_left H]) theorem monic_X_pow_add {n : ℕ} (H : degree p ≤ n) : monic (X ^ (n+1) + p) := have H1 : degree p < n+1, from lt_of_le_of_lt H (with_bot.coe_lt_coe.2 (nat.lt_succ_self n)), monic_of_degree_le (n+1) (le_trans (degree_add_le _ _) (max_le (degree_X_pow_le _) (le_of_lt H1))) (by rw [coeff_add, coeff_X_pow, if_pos rfl, coeff_eq_zero_of_degree_lt H1, add_zero]) theorem monic_X_add_C (x : R) : monic (X + C x) := pow_one (X : polynomial R) ▸ monic_X_pow_add degree_C_le lemma monic_mul (hp : monic p) (hq : monic q) : monic (p * q) := if h0 : (0 : R) = 1 then by haveI := subsingleton_of_zero_eq_one h0; exact subsingleton.elim _ _ else have leading_coeff p * leading_coeff q ≠ 0, by simp [monic.def.1 hp, monic.def.1 hq, ne.symm h0], by rw [monic.def, leading_coeff_mul' this, monic.def.1 hp, monic.def.1 hq, one_mul] lemma monic_pow (hp : monic p) : ∀ (n : ℕ), monic (p ^ n) | 0 := monic_one | (n+1) := by { rw pow_succ, exact monic_mul hp (monic_pow n) } lemma monic_add_of_left {p q : polynomial R} (hp : monic p) (hpq : degree q < degree p) : monic (p + q) := by rwa [monic, add_comm, leading_coeff_add_of_degree_lt hpq] lemma monic_add_of_right {p q : polynomial R} (hq : monic q) (hpq : degree p < degree q) : monic (p + q) := by rwa [monic, leading_coeff_add_of_degree_lt hpq] namespace monic @[simp] lemma nat_degree_eq_zero_iff_eq_one {p : polynomial R} (hp : p.monic) : p.nat_degree = 0 ↔ p = 1 := begin split; intro h, swap, { rw h, exact nat_degree_one }, have : p = C (p.coeff 0), { rw ← polynomial.degree_le_zero_iff, rwa polynomial.nat_degree_eq_zero_iff_degree_le_zero at h }, rw this, convert C_1, rw ← h, apply hp, end @[simp] lemma degree_le_zero_iff_eq_one {p : polynomial R} (hp : p.monic) : p.degree ≤ 0 ↔ p = 1 := by rw [←hp.nat_degree_eq_zero_iff_eq_one, nat_degree_eq_zero_iff_degree_le_zero] lemma nat_degree_mul {p q : polynomial R} (hp : p.monic) (hq : q.monic) : (p * q).nat_degree = p.nat_degree + q.nat_degree := begin nontriviality R, apply nat_degree_mul', simp [hp.leading_coeff, hq.leading_coeff] end lemma degree_mul_comm {p : polynomial R} (hp : p.monic) (q : polynomial R) : (p * q).degree = (q * p).degree := begin by_cases h : q = 0, { simp [h] }, rw [degree_mul', hp.degree_mul], { exact add_comm _ _ }, { rwa [hp.leading_coeff, one_mul, leading_coeff_ne_zero] } end lemma nat_degree_mul' {p q : polynomial R} (hp : p.monic) (hq : q ≠ 0) : (p * q).nat_degree = p.nat_degree + q.nat_degree := begin rw [nat_degree_mul', add_comm], simpa [hp.leading_coeff, leading_coeff_ne_zero] end lemma nat_degree_mul_comm {p : polynomial R} (hp : p.monic) (q : polynomial R) : (p * q).nat_degree = (q * p).nat_degree := begin by_cases h : q = 0, { simp [h] }, rw [hp.nat_degree_mul' h, polynomial.nat_degree_mul', add_comm], simpa [hp.leading_coeff, leading_coeff_ne_zero] end lemma next_coeff_mul {p q : polynomial R} (hp : monic p) (hq : monic q) : next_coeff (p * q) = next_coeff p + next_coeff q := begin nontriviality, simp only [← coeff_one_reverse], rw reverse_mul; simp [coeff_mul, nat.antidiagonal, hp.leading_coeff, hq.leading_coeff, add_comm] end end monic end semiring section comm_semiring variables [comm_semiring R] {p : polynomial R} lemma monic_multiset_prod_of_monic (t : multiset ι) (f : ι → polynomial R) (ht : ∀ i ∈ t, monic (f i)) : monic (t.map f).prod := begin revert ht, refine t.induction_on _ _, { simp }, intros a t ih ht, rw [multiset.map_cons, multiset.prod_cons], exact monic_mul (ht _ (multiset.mem_cons_self _ _)) (ih (λ _ hi, ht _ (multiset.mem_cons_of_mem hi))) end lemma monic_prod_of_monic (s : finset ι) (f : ι → polynomial R) (hs : ∀ i ∈ s, monic (f i)) : monic (∏ i in s, f i) := monic_multiset_prod_of_monic s.1 f hs lemma is_unit_C {x : R} : is_unit (C x) ↔ is_unit x := begin rw [is_unit_iff_dvd_one, is_unit_iff_dvd_one], split, { rintros ⟨g, hg⟩, replace hg := congr_arg (eval 0) hg, rw [eval_one, eval_mul, eval_C] at hg, exact ⟨g.eval 0, hg⟩ }, { rintros ⟨y, hy⟩, exact ⟨C y, by rw [← C_mul, ← hy, C_1]⟩ } end lemma eq_one_of_is_unit_of_monic (hm : monic p) (hpu : is_unit p) : p = 1 := have degree p ≤ 0, from calc degree p ≤ degree (1 : polynomial R) : let ⟨u, hu⟩ := is_unit_iff_dvd_one.1 hpu in if hu0 : u = 0 then begin rw [hu0, mul_zero] at hu, rw [← mul_one p, hu, mul_zero], simp end else have p.leading_coeff * u.leading_coeff ≠ 0, by rw [hm.leading_coeff, one_mul, ne.def, leading_coeff_eq_zero]; exact hu0, by rw [hu, degree_mul' this]; exact le_add_of_nonneg_right (degree_nonneg_iff_ne_zero.2 hu0) ... ≤ 0 : degree_one_le, by rw [eq_C_of_degree_le_zero this, ← nat_degree_eq_zero_iff_degree_le_zero.2 this, ← leading_coeff, hm.leading_coeff, C_1] lemma monic.next_coeff_multiset_prod (t : multiset ι) (f : ι → polynomial R) (h : ∀ i ∈ t, monic (f i)) : next_coeff (t.map f).prod = (t.map (λ i, next_coeff (f i))).sum := begin revert h, refine multiset.induction_on t _ (λ a t ih ht, _), { simp only [multiset.not_mem_zero, forall_prop_of_true, forall_prop_of_false, multiset.map_zero, multiset.prod_zero, multiset.sum_zero, not_false_iff, forall_true_iff], rw ← C_1, rw next_coeff_C_eq_zero }, { rw [multiset.map_cons, multiset.prod_cons, multiset.map_cons, multiset.sum_cons, monic.next_coeff_mul, ih], exacts [λ i hi, ht i (multiset.mem_cons_of_mem hi), ht a (multiset.mem_cons_self _ _), monic_multiset_prod_of_monic _ _ (λ b bs, ht _ (multiset.mem_cons_of_mem bs))] } end lemma monic.next_coeff_prod (s : finset ι) (f : ι → polynomial R) (h : ∀ i ∈ s, monic (f i)) : next_coeff (∏ i in s, f i) = ∑ i in s, next_coeff (f i) := monic.next_coeff_multiset_prod s.1 f h end comm_semiring section ring variables [ring R] {p : polynomial R} theorem monic_X_sub_C (x : R) : monic (X - C x) := by simpa only [sub_eq_add_neg, C_neg] using monic_X_add_C (-x) theorem monic_X_pow_sub {n : ℕ} (H : degree p ≤ n) : monic (X ^ (n+1) - p) := by simpa [sub_eq_add_neg] using monic_X_pow_add (show degree (-p) ≤ n, by rwa ←degree_neg p at H) /-- `X ^ n - a` is monic. -/ lemma monic_X_pow_sub_C {R : Type u} [ring R] (a : R) {n : ℕ} (h : n ≠ 0) : (X ^ n - C a).monic := begin obtain ⟨k, hk⟩ := nat.exists_eq_succ_of_ne_zero h, convert monic_X_pow_sub _, exact le_trans degree_C_le nat.with_bot.coe_nonneg, end lemma monic_sub_of_left {p q : polynomial R} (hp : monic p) (hpq : degree q < degree p) : monic (p - q) := by { rw sub_eq_add_neg, apply monic_add_of_left hp, rwa degree_neg } lemma monic_sub_of_right {p q : polynomial R} (hq : q.leading_coeff = -1) (hpq : degree p < degree q) : monic (p - q) := have (-q).coeff (-q).nat_degree = 1 := by rw [nat_degree_neg, coeff_neg, show q.coeff q.nat_degree = -1, from hq, neg_neg], by { rw sub_eq_add_neg, apply monic_add_of_right this, rwa degree_neg } section injective open function variables [semiring S] {f : R →+* S} (hf : injective f) include hf lemma degree_map_eq_of_injective (p : polynomial R) : degree (p.map f) = degree p := if h : p = 0 then by simp [h] else degree_map_eq_of_leading_coeff_ne_zero _ (by rw [← f.map_zero]; exact mt hf.eq_iff.1 (mt leading_coeff_eq_zero.1 h)) lemma degree_map' (p : polynomial R) : degree (p.map f) = degree p := p.degree_map_eq_of_injective hf lemma nat_degree_map' (p : polynomial R) : nat_degree (p.map f) = nat_degree p := nat_degree_eq_of_degree_eq (degree_map' hf p) lemma leading_coeff_map' (p : polynomial R) : leading_coeff (p.map f) = f (leading_coeff p) := begin unfold leading_coeff, rw [coeff_map, nat_degree_map' hf p], end lemma next_coeff_map (p : polynomial R) : (p.map f).next_coeff = f p.next_coeff := begin unfold next_coeff, rw nat_degree_map' hf, split_ifs; simp end lemma leading_coeff_of_injective (p : polynomial R) : leading_coeff (p.map f) = f (leading_coeff p) := begin delta leading_coeff, rw [coeff_map f, nat_degree_map' hf p] end lemma monic_of_injective {p : polynomial R} (hp : (p.map f).monic) : p.monic := begin apply hf, rw [← leading_coeff_of_injective hf, hp.leading_coeff, f.map_one] end end injective end ring section nonzero_semiring variables [semiring R] [nontrivial R] {p q : polynomial R} @[simp] lemma not_monic_zero : ¬monic (0 : polynomial R) := by simpa only [monic, leading_coeff_zero] using (zero_ne_one : (0 : R) ≠ 1) lemma ne_zero_of_monic (h : monic p) : p ≠ 0 := λ h₁, @not_monic_zero R _ _ (h₁ ▸ h) end nonzero_semiring section not_zero_divisor -- TODO: using gh-8537, rephrase lemmas that involve commutation around `*` using the op-ring -- TODO: using gh-8561, rephrase using regular elements variables [semiring R] {p : polynomial R} lemma monic.mul_left_ne_zero (hp : monic p) {q : polynomial R} (hq : q ≠ 0) : q * p ≠ 0 := begin by_cases h : p = 1, { simpa [h] }, rw [ne.def, ←degree_eq_bot, hp.degree_mul, with_bot.add_eq_bot, not_or_distrib, degree_eq_bot], refine ⟨hq, _⟩, rw [←hp.degree_le_zero_iff_eq_one, not_le] at h, refine (lt_trans _ h).ne', simp end lemma monic.mul_right_ne_zero (hp : monic p) {q : polynomial R} (hq : q ≠ 0) : p * q ≠ 0 := begin by_cases h : p = 1, { simpa [h] }, rw [ne.def, ←degree_eq_bot, hp.degree_mul_comm, hp.degree_mul, with_bot.add_eq_bot, not_or_distrib, degree_eq_bot], refine ⟨hq, _⟩, rw [←hp.degree_le_zero_iff_eq_one, not_le] at h, refine (lt_trans _ h).ne', simp end lemma monic.mul_nat_degree_lt_iff (h : monic p) {q : polynomial R} : (p * q).nat_degree < p.nat_degree ↔ p ≠ 1 ∧ q = 0 := begin by_cases hq : q = 0, { suffices : 0 < p.nat_degree ↔ p.nat_degree ≠ 0, { simpa [hq, ←h.nat_degree_eq_zero_iff_eq_one] }, exact ⟨λ h, h.ne', λ h, lt_of_le_of_ne (nat.zero_le _) h.symm ⟩ }, { simp [h.nat_degree_mul', hq] } end lemma monic.mul_right_eq_zero_iff (h : monic p) {q : polynomial R} : p * q = 0 ↔ q = 0 := begin by_cases hq : q = 0; simp [h.mul_right_ne_zero, hq] end lemma monic.mul_left_eq_zero_iff (h : monic p) {q : polynomial R} : q * p = 0 ↔ q = 0 := begin by_cases hq : q = 0; simp [h.mul_left_ne_zero, hq] end lemma degree_smul_of_non_zero_divisor {S : Type*} [monoid S] [distrib_mul_action S R] (p : polynomial R) (k : S) (h : ∀ (x : R), k • x = 0 → x = 0) : (k • p).degree = p.degree := begin refine le_antisymm _ _, { rw degree_le_iff_coeff_zero, intros m hm, rw degree_lt_iff_coeff_zero at hm, simp [hm m le_rfl] }, { rw degree_le_iff_coeff_zero, intros m hm, rw degree_lt_iff_coeff_zero at hm, refine h _ _, simpa using hm m le_rfl }, end lemma nat_degree_smul_of_non_zero_divisor {S : Type*} [monoid S] [distrib_mul_action S R] (p : polynomial R) (k : S) (h : ∀ (x : R), k • x = 0 → x = 0) : (k • p).nat_degree = p.nat_degree := begin by_cases hp : p = 0, { simp [hp] }, rw [←with_bot.coe_eq_coe, ←degree_eq_nat_degree hp, ←degree_eq_nat_degree, degree_smul_of_non_zero_divisor _ _ h], contrapose! hp, rw polynomial.ext_iff at hp ⊢, simp only [coeff_smul, coeff_zero] at hp, intro n, simp [h _ (hp n)] end lemma leading_coeff_smul_of_non_zero_divisor {S : Type*} [monoid S] [distrib_mul_action S R] (p : polynomial R) (k : S) (h : ∀ (x : R), k • x = 0 → x = 0) : (k • p).leading_coeff = k • p.leading_coeff := by rw [leading_coeff, leading_coeff, coeff_smul, nat_degree_smul_of_non_zero_divisor _ _ h] lemma monic_of_is_unit_leading_coeff_inv_smul (h : is_unit p.leading_coeff) : monic (h.unit⁻¹ • p) := begin rw [monic.def, leading_coeff_smul_of_non_zero_divisor, units.smul_def], { obtain ⟨k, hk⟩ := h, simp only [←hk, smul_eq_mul, ←units.coe_mul, units.coe_eq_one, inv_mul_eq_iff_eq_mul], simp [units.ext_iff, is_unit.unit_spec] }, { simp [smul_eq_zero_iff_eq] } end lemma is_unit_leading_coeff_mul_right_eq_zero_iff (h : is_unit p.leading_coeff) {q : polynomial R} : p * q = 0 ↔ q = 0 := begin split, { intro hp, rw ←smul_eq_zero_iff_eq (h.unit)⁻¹ at hp, { have : (h.unit)⁻¹ • (p * q) = ((h.unit)⁻¹ • p) * q, { ext, simp only [units.smul_def, coeff_smul, coeff_mul, smul_eq_mul, mul_sum], refine sum_congr rfl (λ x hx, _), rw ←mul_assoc }, rwa [this, monic.mul_right_eq_zero_iff] at hp, exact monic_of_is_unit_leading_coeff_inv_smul _ } }, { rintro rfl, simp } end lemma is_unit_leading_coeff_mul_left_eq_zero_iff (h : is_unit p.leading_coeff) {q : polynomial R} : q * p = 0 ↔ q = 0 := begin split, { intro hp, replace hp := congr_arg (* C ↑(h.unit)⁻¹) hp, simp only [zero_mul] at hp, rwa [mul_assoc, monic.mul_left_eq_zero_iff] at hp, nontriviality, refine monic_mul_C_of_leading_coeff_mul_eq_one _, simp [units.mul_inv_eq_iff_eq_mul, is_unit.unit_spec] }, { rintro rfl, rw zero_mul } end end not_zero_divisor end polynomial
4352c2c81b14a693dc0c89a77a2bf416bcc278a4
a1179fa077c09acc49e4fbc8d67084ba89ac4f4c
/tutorials/src/solutions/all.lean
e71eae63e6ab9d13833f751597c6b717ff837dcf
[]
no_license
Seeram/Lean-proof-assistant
11ca0ca0e0446bacdd1773c4c481a3653b2f1074
e672d46e0e5f39d8de2933ad4f4cac095ca6094f
refs/heads/master
1,682,754,224,366
1,620,959,431,000
1,620,959,431,000
299,000,950
0
1
null
1,620,680,462,000
1,601,200,258,000
Lean
UTF-8
Lean
false
false
283
lean
import 07bis_abstract_negations import 07_first_negations import 04_exists import 06_sub_sequences import 05_sequence_limits import 02_iff_if_and import 08_limits_negation import 09_limits_final import 01_equality_rewriting import 03_forall_or import tuto_lib import 00_first_proofs
eb39bdb1f9537047e3a035f50f012e9d5e1d3741
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/dfinsupp/multiset.lean
3d1da56331209ffbf2ac8f7f7b2081cc0dfc774c
[ "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
2,953
lean
/- Copyright (c) 2022 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import data.dfinsupp.order /-! # Equivalence between `multiset` and `ℕ`-valued finitely supported functions This defines `dfinsupp.to_multiset` the equivalence between `Π₀ a : α, ℕ` and `multiset α`, along with `multiset.to_dfinsupp` the reverse equivalence. Note that this provides a computable alternative to `finsupp.to_multiset`. -/ variables {α : Type*} {β : α → Type*} namespace dfinsupp /-- Non-dependent special case of `dfinsupp.add_zero_class` to help typeclass search. -/ instance add_zero_class' {β} [add_zero_class β] : add_zero_class (Π₀ a : α, β) := @dfinsupp.add_zero_class α (λ _, β) _ variables [decidable_eq α] /-- A computable version of `finsupp.to_multiset`. -/ def to_multiset : (Π₀ a : α, ℕ) →+ multiset α := dfinsupp.sum_add_hom (λ a : α, multiset.repeat_add_monoid_hom a) @[simp] lemma to_multiset_single (a : α) (n : ℕ) : to_multiset (dfinsupp.single a n) = multiset.repeat a n := dfinsupp.sum_add_hom_single _ _ _ end dfinsupp namespace multiset variables [decidable_eq α] /-- A computable version of `multiset.to_finsupp` -/ def to_dfinsupp : multiset α →+ Π₀ a : α, ℕ := { to_fun := λ s, { to_fun := λ n, s.count n, support' := trunc.mk ⟨s, λ i, (em (i ∈ s)).imp_right multiset.count_eq_zero_of_not_mem⟩ }, map_zero' := rfl, map_add' := λ s t, dfinsupp.ext $ λ _, multiset.count_add _ _ _ } @[simp] lemma to_dfinsupp_apply (s : multiset α) (a : α) : s.to_dfinsupp a = s.count a := rfl @[simp] lemma to_dfinsupp_support (s : multiset α) : s.to_dfinsupp.support = s.to_finset := (finset.filter_eq_self _).mpr (λ x hx, count_ne_zero.mpr $ multiset.mem_to_finset.1 hx) @[simp] lemma to_dfinsupp_repeat (a : α) (n : ℕ) : to_dfinsupp (multiset.repeat a n) = dfinsupp.single a n := begin ext i, dsimp [to_dfinsupp], simp [count_repeat, eq_comm], end @[simp] lemma to_dfinsupp_singleton (a : α) : to_dfinsupp {a} = dfinsupp.single a 1 := by rw [←repeat_one, to_dfinsupp_repeat] /-- `multiset.to_dfinsupp` as an `add_equiv`. -/ @[simps apply symm_apply] def equiv_dfinsupp : multiset α ≃+ Π₀ a : α, ℕ := add_monoid_hom.to_add_equiv multiset.to_dfinsupp dfinsupp.to_multiset (by { ext x : 1, simp }) (by { refine @dfinsupp.add_hom_ext α (λ _, ℕ) _ _ _ _ _ _ (λ i hi, _), simp, }) @[simp] lemma to_dfinsupp_to_multiset (s : multiset α) : s.to_dfinsupp.to_multiset = s := equiv_dfinsupp.symm_apply_apply s @[simp] lemma to_dfinsupp_le_to_dfinsupp (s t : multiset α) : to_dfinsupp s ≤ to_dfinsupp t ↔ s ≤ t := by simp [multiset.le_iff_count, dfinsupp.le_def] end multiset @[simp] lemma dfinsupp.to_multiset_to_dfinsupp [decidable_eq α] (f : Π₀ a : α, ℕ) : f.to_multiset.to_dfinsupp = f := multiset.equiv_dfinsupp.apply_symm_apply f
37ac84fd2e46f80051a1ccf00f728e693999c0b8
9c1ad797ec8a5eddb37d34806c543602d9a6bf70
/enriched/enriched_category.lean
b9d2553d5c0b7368e4e7a55ad28ca08f08f8515c
[]
no_license
timjb/lean-category-theory
816eefc3a0582c22c05f4ee1c57ed04e57c0982f
12916cce261d08bb8740bc85e0175b75fb2a60f4
refs/heads/master
1,611,078,926,765
1,492,080,000,000
1,492,080,000,000
88,348,246
0
0
null
1,492,262,499,000
1,492,262,498,000
null
UTF-8
Lean
false
false
2,390
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import ..monoidal_categories.monoidal_category namespace tqft.categories.enriched open tqft.categories open tqft.categories.monoidal_category structure {u v w} EnrichedCategory { V: Category.{v w} } ( m : MonoidalStructure V ) := (Obj : Type u) (Hom : Obj → Obj → V.Obj) (compose : Π { X Y Z : Obj }, V.Hom (m.tensorObjects (Hom X Y) (Hom Y Z)) (Hom X Z)) (identity : Π X : Obj, V.Hom m.tensor_unit (Hom X X)) (left_identity : ∀ { X Y : Obj }, V.compose (m.left_unitor.inverse.components (Hom X Y)) (V.compose (m.tensorMorphisms (identity X) (V.identity (Hom X Y))) compose ) = V.identity (Hom X Y) ) (right_identity : ∀ { X Y : Obj }, V.compose (m.right_unitor.inverse.components (Hom X Y)) (V.compose (m.tensorMorphisms (V.identity (Hom X Y)) (identity Y)) compose ) = V.identity (Hom X Y) ) (associativity : ∀ { W X Y Z : Obj }, V.compose (m.tensorMorphisms compose (V.identity (Hom Y Z))) compose = V.compose (m.associator (Hom W X) (Hom X Y) (Hom Y Z)) (V.compose (m.tensorMorphisms (V.identity (Hom W X)) compose) compose) ) attribute [simp,ematch] EnrichedCategory.left_identity attribute [simp,ematch] EnrichedCategory.right_identity attribute [ematch] EnrichedCategory.associativity instance EnrichedCategory_to_Hom { V : Category } { m : MonoidalStructure V } : has_coe_to_fun (EnrichedCategory m) := { F := λ C, C.Obj → C.Obj → V.Obj, coe := EnrichedCategory.Hom } structure Functor { V : Category } { m : MonoidalStructure V } ( C D : EnrichedCategory m ) := ( onObjects : C.Obj → D.Obj ) ( onMorphisms : ∀ { X Y : C.Obj }, V.Hom (C X Y) (D (onObjects X) (onObjects Y)) ) ( identities : ∀ X : C.Obj, V.compose (C.identity X) onMorphisms = D.identity (onObjects X) ) ( functoriality : ∀ { X Y Z : C.Obj }, V.compose C.compose (@onMorphisms X Z) = V.compose (m.tensorMorphisms (@onMorphisms X Y) (@onMorphisms Y Z)) D.compose ) attribute [simp,ematch] Functor.identities attribute [simp,ematch] Functor.functoriality -- PROJECT natural transformations don't always exist; you need various limits! end tqft.categories.enriched
c053a2d497bcb1a13e7e9ae3c3c4358adcb6e9b8
4727251e0cd73359b15b664c3170e5d754078599
/src/ring_theory/graded_algebra/homogeneous_localization.lean
cae0fdc5645392fc99f048f7a42db026624dee80
[ "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
19,804
lean
/- Copyright (c) 2022 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jujian Zhang, Eric Wieser -/ import ring_theory.localization.at_prime import ring_theory.graded_algebra.basic /-! # Homogeneous Localization ## Notation - `ι` is a commutative monoid; - `R` is a commutative semiring; - `A` is a commutative ring and an `R`-algebra; - `𝒜 : ι → submodule R A` is the grading of `A`; - `x : ideal A` is a prime ideal. ## Main definitions and results This file constructs the subring of `Aₓ` where the numerator and denominator have the same grading, i.e. `{a/b ∈ Aₓ | ∃ (i : ι), a ∈ 𝒜ᵢ ∧ b ∈ 𝒜ᵢ}`. * `homogeneous_localization.num_denom_same_deg`: a structure with a numerator and denominator field where they are required to have the same grading. However `num_denom_same_deg 𝒜 x` cannot have a ring structure for many reasons, for example if `c` is a `num_denom_same_deg`, then generally, `c + (-c)` is not necessarily `0` for degree reasons --- `0` is considered to have grade zero (see `deg_zero`) but `c + (-c)` has the same degree as `c`. To circumvent this, we quotient `num_denom_same_deg 𝒜 x` by the kernel of `c ↦ c.num / c.denom`. * `homogeneous_localization.num_denom_same_deg.embedding` : for `x : prime ideal of A` and any `c : num_denom_same_deg 𝒜 x`, or equivalent a numerator and a denominator of the same degree, we get an element `c.num / c.denom` of `Aₓ`. * `homogeneous_localization`: `num_denom_same_deg 𝒜 x` quotiented by kernel of `embedding 𝒜 x`. * `homogeneous_localization.val`: if `f : homogeneous_localization 𝒜 x`, then `f.val` is an element of `Aₓ`. In another word, one can view `homogeneous_localization 𝒜 x` as a subring of `Aₓ` through `homogeneous_localization.val`. * `homogeneous_localization.num`: if `f : homogeneous_localization 𝒜 x`, then `f.num : A` is the numerator of `f`. * `homogeneous_localization.num`: if `f : homogeneous_localization 𝒜 x`, then `f.denom : A` is the denominator of `f`. * `homogeneous_localization.deg`: if `f : homogeneous_localization 𝒜 x`, then `f.deg : ι` is the degree of `f` such that `f.num ∈ 𝒜 f.deg` and `f.denom ∈ 𝒜 f.deg` (see `homogeneous_localization.num_mem` and `homogeneous_localization.denom_mem`). * `homogeneous_localization.num_mem`: if `f : homogeneous_localization 𝒜 x`, then `f.num_mem` is a proof that `f.num ∈ f.deg`. * `homogeneous_localization.denom_mem`: if `f : homogeneous_localization 𝒜 x`, then `f.denom_mem` is a proof that `f.denom ∈ f.deg`. * `homogeneous_localization.eq_num_div_denom`: if `f : homogeneous_localization 𝒜 x`, then `f.val : Aₓ` is equal to `f.num / f.denom`. * `homogeneous_localization.local_ring`: `homogeneous_localization 𝒜 x` is a local ring. ## References * [Robin Hartshorne, *Algebraic Geometry*][Har77] -/ noncomputable theory open_locale direct_sum big_operators pointwise open direct_sum set_like variables {ι R A: Type*} variables [add_comm_monoid ι] [decidable_eq ι] variables [comm_ring R] [comm_ring A] [algebra R A] variables (𝒜 : ι → submodule R A) [graded_algebra 𝒜] variables (x : ideal A) [ideal.is_prime x] local notation `at ` x := localization.at_prime x namespace homogeneous_localization section /-- Let `x` be a prime ideal, then `num_denom_same_deg 𝒜 x` is a structure with a numerator and a denominator with same grading such that the denominator is not contained in `x`. -/ @[nolint has_inhabited_instance] structure num_denom_same_deg := (deg : ι) (num denom : 𝒜 deg) (denom_not_mem : (denom : A) ∉ x) end namespace num_denom_same_deg open set_like.graded_monoid submodule variable {𝒜} @[ext] lemma ext {c1 c2 : num_denom_same_deg 𝒜 x} (hdeg : c1.deg = c2.deg) (hnum : (c1.num : A) = c2.num) (hdenom : (c1.denom : A) = c2.denom) : c1 = c2 := begin rcases c1 with ⟨i1, ⟨n1, hn1⟩, ⟨d1, hd1⟩, h1⟩, rcases c2 with ⟨i2, ⟨n2, hn2⟩, ⟨d2, hd2⟩, h2⟩, dsimp only [subtype.coe_mk] at *, simp only, exact ⟨hdeg, by subst hdeg; subst hnum, by subst hdeg; subst hdenom⟩, end instance : has_one (num_denom_same_deg 𝒜 x) := { one := { deg := 0, num := ⟨1, one_mem⟩, denom := ⟨1, one_mem⟩, denom_not_mem := λ r, (infer_instance : x.is_prime).ne_top $ x.eq_top_iff_one.mpr r } } @[simp] lemma deg_one : (1 : num_denom_same_deg 𝒜 x).deg = 0 := rfl @[simp] lemma num_one : ((1 : num_denom_same_deg 𝒜 x).num : A) = 1 := rfl @[simp] lemma denom_one : ((1 : num_denom_same_deg 𝒜 x).denom : A) = 1 := rfl instance : has_zero (num_denom_same_deg 𝒜 x) := { zero := ⟨0, 0, ⟨1, one_mem⟩, λ r, (infer_instance : x.is_prime).ne_top $ x.eq_top_iff_one.mpr r⟩ } @[simp] lemma deg_zero : (0 : num_denom_same_deg 𝒜 x).deg = 0 := rfl @[simp] lemma num_zero : (0 : num_denom_same_deg 𝒜 x).num = 0 := rfl @[simp] lemma denom_zero : ((0 : num_denom_same_deg 𝒜 x).denom : A) = 1 := rfl instance : has_mul (num_denom_same_deg 𝒜 x) := { mul := λ p q, { deg := p.deg + q.deg, num := ⟨p.num * q.num, mul_mem p.num.prop q.num.prop⟩, denom := ⟨p.denom * q.denom, mul_mem p.denom.prop q.denom.prop⟩, denom_not_mem := λ r, or.elim ((infer_instance : x.is_prime).mem_or_mem r) p.denom_not_mem q.denom_not_mem } } @[simp] lemma deg_mul (c1 c2 : num_denom_same_deg 𝒜 x) : (c1 * c2).deg = c1.deg + c2.deg := rfl @[simp] lemma num_mul (c1 c2 : num_denom_same_deg 𝒜 x) : ((c1 * c2).num : A) = c1.num * c2.num := rfl @[simp] lemma denom_mul (c1 c2 : num_denom_same_deg 𝒜 x) : ((c1 * c2).denom : A) = c1.denom * c2.denom := rfl instance : has_add (num_denom_same_deg 𝒜 x) := { add := λ c1 c2, { deg := c1.deg + c2.deg, num := ⟨c1.denom * c2.num + c2.denom * c1.num, add_mem (mul_mem c1.denom.2 c2.num.2) (add_comm c2.deg c1.deg ▸ mul_mem c2.denom.2 c1.num.2)⟩, denom := ⟨c1.denom * c2.denom, mul_mem c1.denom.2 c2.denom.2⟩, denom_not_mem := λ r, or.elim ((infer_instance : x.is_prime).mem_or_mem r) c1.denom_not_mem c2.denom_not_mem } } @[simp] lemma deg_add (c1 c2 : num_denom_same_deg 𝒜 x) : (c1 + c2).deg = c1.deg + c2.deg := rfl @[simp] lemma num_add (c1 c2 : num_denom_same_deg 𝒜 x) : ((c1 + c2).num : A) = c1.denom * c2.num + c2.denom * c1.num := rfl @[simp] lemma denom_add (c1 c2 : num_denom_same_deg 𝒜 x) : ((c1 + c2).denom : A) = c1.denom * c2.denom := rfl instance : has_neg (num_denom_same_deg 𝒜 x) := { neg := λ c, ⟨c.deg, ⟨-c.num, neg_mem c.num.2⟩, c.denom, c.denom_not_mem⟩ } @[simp] lemma deg_neg (c : num_denom_same_deg 𝒜 x) : (-c).deg = c.deg := rfl @[simp] lemma num_neg (c : num_denom_same_deg 𝒜 x) : ((-c).num : A) = -c.num := rfl @[simp] lemma denom_neg (c : num_denom_same_deg 𝒜 x) : ((-c).denom : A) = c.denom := rfl instance : comm_monoid (num_denom_same_deg 𝒜 x) := { one := 1, mul := (*), mul_assoc := λ c1 c2 c3, ext _ (add_assoc _ _ _) (mul_assoc _ _ _) (mul_assoc _ _ _), one_mul := λ c, ext _ (zero_add _) (one_mul _) (one_mul _), mul_one := λ c, ext _ (add_zero _) (mul_one _) (mul_one _), mul_comm := λ c1 c2, ext _ (add_comm _ _) (mul_comm _ _) (mul_comm _ _) } instance : has_pow (num_denom_same_deg 𝒜 x) ℕ := { pow := λ c n, ⟨n • c.deg, ⟨c.num ^ n, pow_mem n c.num.2⟩, ⟨c.denom ^ n, pow_mem n c.denom.2⟩, begin cases n, { simp only [pow_zero], exact λ r, (infer_instance : x.is_prime).ne_top $ (ideal.eq_top_iff_one _).mpr r, }, { exact λ r, c.denom_not_mem $ ((infer_instance : x.is_prime).pow_mem_iff_mem n.succ (nat.zero_lt_succ _)).mp r } end⟩ } @[simp] lemma deg_pow (c : num_denom_same_deg 𝒜 x) (n : ℕ) : (c ^ n).deg = n • c.deg := rfl @[simp] lemma num_pow (c : num_denom_same_deg 𝒜 x) (n : ℕ) : ((c ^ n).num : A) = c.num ^ n := rfl @[simp] lemma denom_pow (c : num_denom_same_deg 𝒜 x) (n : ℕ) : ((c ^ n).denom : A) = c.denom ^ n := rfl section has_scalar variables {α : Type*} [has_scalar α R] [has_scalar α A] [is_scalar_tower α R A] instance : has_scalar α (num_denom_same_deg 𝒜 x) := { smul := λ m c, ⟨c.deg, m • c.num, c.denom, c.denom_not_mem⟩ } @[simp] lemma deg_smul (c : num_denom_same_deg 𝒜 x) (m : α) : (m • c).deg = c.deg := rfl @[simp] lemma num_smul (c : num_denom_same_deg 𝒜 x) (m : α) : ((m • c).num : A) = m • c.num := rfl @[simp] lemma denom_smul (c : num_denom_same_deg 𝒜 x) (m : α) : ((m • c).denom : A) = c.denom := rfl end has_scalar variable (𝒜) /-- For `x : prime ideal of A` and any `p : num_denom_same_deg 𝒜 x`, or equivalent a numerator and a denominator of the same degree, we get an element `p.num / p.denom` of `Aₓ`. -/ def embedding (p : num_denom_same_deg 𝒜 x) : at x := localization.mk p.num ⟨p.denom, p.denom_not_mem⟩ end num_denom_same_deg end homogeneous_localization /-- For `x : prime ideal of A`, `homogeneous_localization 𝒜 x` is `num_denom_same_deg 𝒜 x` modulo the kernel of `embedding 𝒜 x`. This is essentially the subring of `Aₓ` where the numerator and denominator share the same grading. -/ @[nolint has_inhabited_instance] def homogeneous_localization : Type* := quotient (setoid.ker $ homogeneous_localization.num_denom_same_deg.embedding 𝒜 x) namespace homogeneous_localization open homogeneous_localization homogeneous_localization.num_denom_same_deg variables {𝒜} {x} /-- View an element of `homogeneous_localization 𝒜 x` as an element of `Aₓ` by forgetting that the numerator and denominator are of the same grading. -/ def val (y : homogeneous_localization 𝒜 x) : at x := quotient.lift_on' y (num_denom_same_deg.embedding 𝒜 x) $ λ _ _, id @[simp] lemma val_mk' (i : num_denom_same_deg 𝒜 x) : val (quotient.mk' i) = localization.mk i.num ⟨i.denom, i.denom_not_mem⟩ := rfl variable (x) lemma val_injective : function.injective (@homogeneous_localization.val _ _ _ _ _ _ _ _ 𝒜 _ x _) := λ a b, quotient.rec_on_subsingleton₂' a b $ λ a b h, quotient.sound' h instance has_pow : has_pow (homogeneous_localization 𝒜 x) ℕ := { pow := λ z n, (quotient.map' (^ n) (λ c1 c2 (h : localization.mk _ _ = localization.mk _ _), begin change localization.mk _ _ = localization.mk _ _, simp only [num_pow, denom_pow], convert congr_arg (λ z, z ^ n) h; erw localization.mk_pow; refl, end) : homogeneous_localization 𝒜 x → homogeneous_localization 𝒜 x) z } section has_scalar variables {α : Type*} [has_scalar α R] [has_scalar α A] [is_scalar_tower α R A] variables [is_scalar_tower α A A] instance : has_scalar α (homogeneous_localization 𝒜 x) := { smul := λ m, quotient.map' ((•) m) (λ c1 c2 (h : localization.mk _ _ = localization.mk _ _), begin change localization.mk _ _ = localization.mk _ _, simp only [num_smul, denom_smul], convert congr_arg (λ z : at x, m • z) h; rw localization.smul_mk; refl, end) } @[simp] lemma smul_val (y : homogeneous_localization 𝒜 x) (n : α) : (n • y).val = n • y.val := begin induction y using quotient.induction_on, unfold homogeneous_localization.val has_scalar.smul, simp only [quotient.lift_on₂'_mk, quotient.lift_on'_mk], change localization.mk _ _ = n • localization.mk _ _, dsimp only, rw localization.smul_mk, congr' 1, end end has_scalar instance : has_neg (homogeneous_localization 𝒜 x) := { neg := quotient.map' has_neg.neg (λ c1 c2 (h : localization.mk _ _ = localization.mk _ _), begin change localization.mk _ _ = localization.mk _ _, simp only [num_neg, denom_neg, ←localization.neg_mk], exact congr_arg (λ c, -c) h end) } instance : has_add (homogeneous_localization 𝒜 x) := { add := quotient.map₂' (+) (λ c1 c2 (h : localization.mk _ _ = localization.mk _ _) c3 c4 (h' : localization.mk _ _ = localization.mk _ _), begin change localization.mk _ _ = localization.mk _ _, simp only [num_add, denom_add, ←localization.add_mk], convert congr_arg2 (+) h h'; erw [localization.add_mk]; refl end) } instance : has_sub (homogeneous_localization 𝒜 x) := { sub := λ z1 z2, z1 + (-z2) } instance : has_mul (homogeneous_localization 𝒜 x) := { mul := quotient.map₂' (*) (λ c1 c2 (h : localization.mk _ _ = localization.mk _ _) c3 c4 (h' : localization.mk _ _ = localization.mk _ _), begin change localization.mk _ _ = localization.mk _ _, simp only [num_mul, denom_mul], convert congr_arg2 (*) h h'; erw [localization.mk_mul]; refl, end) } instance : has_one (homogeneous_localization 𝒜 x) := { one := quotient.mk' 1 } instance : has_zero (homogeneous_localization 𝒜 x) := { zero := quotient.mk' 0 } lemma zero_eq : (0 : homogeneous_localization 𝒜 x) = quotient.mk' 0 := rfl lemma one_eq : (1 : homogeneous_localization 𝒜 x) = quotient.mk' 1 := rfl variable {x} lemma zero_val : (0 : homogeneous_localization 𝒜 x).val = 0 := localization.mk_zero _ lemma one_val : (1 : homogeneous_localization 𝒜 x).val = 1 := localization.mk_one @[simp] lemma add_val (y1 y2 : homogeneous_localization 𝒜 x) : (y1 + y2).val = y1.val + y2.val := begin induction y1 using quotient.induction_on, induction y2 using quotient.induction_on, unfold homogeneous_localization.val has_add.add, simp only [quotient.lift_on₂'_mk, quotient.lift_on'_mk], change localization.mk _ _ = localization.mk _ _ + localization.mk _ _, dsimp only, rw [localization.add_mk], refl end @[simp] lemma mul_val (y1 y2 : homogeneous_localization 𝒜 x) : (y1 * y2).val = y1.val * y2.val := begin induction y1 using quotient.induction_on, induction y2 using quotient.induction_on, unfold homogeneous_localization.val has_mul.mul, simp only [quotient.lift_on₂'_mk, quotient.lift_on'_mk], change localization.mk _ _ = localization.mk _ _ * localization.mk _ _, dsimp only, rw [localization.mk_mul], refl, end @[simp] lemma neg_val (y : homogeneous_localization 𝒜 x) : (-y).val = -y.val := begin induction y using quotient.induction_on, unfold homogeneous_localization.val has_neg.neg, simp only [quotient.lift_on₂'_mk, quotient.lift_on'_mk], change localization.mk _ _ = - localization.mk _ _, dsimp only, rw [localization.neg_mk], refl, end @[simp] lemma sub_val (y1 y2 : homogeneous_localization 𝒜 x) : (y1 - y2).val = y1.val - y2.val := by rw [show y1 - y2 = y1 + (-y2), from rfl, add_val, neg_val]; refl @[simp] lemma pow_val (y : homogeneous_localization 𝒜 x) (n : ℕ) : (y ^ n).val = y.val ^ n := begin induction y using quotient.induction_on, unfold homogeneous_localization.val has_pow.pow, simp only [quotient.lift_on₂'_mk, quotient.lift_on'_mk], change localization.mk _ _ = (localization.mk _ _) ^ n, rw localization.mk_pow, dsimp only, congr' 1, end instance : comm_ring (homogeneous_localization 𝒜 x) := (homogeneous_localization.val_injective x).comm_ring _ zero_val one_val add_val mul_val neg_val sub_val (λ z n, smul_val x z n) (λ z n, smul_val x z n) pow_val end homogeneous_localization namespace homogeneous_localization open homogeneous_localization homogeneous_localization.num_denom_same_deg variables {𝒜} {x} /-- numerator of an element in `homogeneous_localization x`-/ def num (f : homogeneous_localization 𝒜 x) : A := (quotient.out' f).num /-- denominator of an element in `homogeneous_localization x`-/ def denom (f : homogeneous_localization 𝒜 x) : A := (quotient.out' f).denom /-- For an element in `homogeneous_localization x`, degree is the natural number `i` such that `𝒜 i` contains both numerator and denominator. -/ def deg (f : homogeneous_localization 𝒜 x) : ι := (quotient.out' f).deg lemma denom_not_mem (f : homogeneous_localization 𝒜 x) : f.denom ∉ x := (quotient.out' f).denom_not_mem lemma num_mem (f : homogeneous_localization 𝒜 x) : f.num ∈ 𝒜 f.deg := (quotient.out' f).num.2 lemma denom_mem (f : homogeneous_localization 𝒜 x) : f.denom ∈ 𝒜 f.deg := (quotient.out' f).denom.2 lemma eq_num_div_denom (f : homogeneous_localization 𝒜 x) : f.val = localization.mk f.num ⟨f.denom, f.denom_not_mem⟩ := begin have := (quotient.out_eq' f), apply_fun homogeneous_localization.val at this, rw ← this, unfold homogeneous_localization.val, simp only [quotient.lift_on'_mk'], refl, end lemma ext_iff_val (f g : homogeneous_localization 𝒜 x) : f = g ↔ f.val = g.val := { mp := λ h, h ▸ rfl, mpr := λ h, begin induction f using quotient.induction_on, induction g using quotient.induction_on, rw quotient.eq, unfold homogeneous_localization.val at h, simpa only [quotient.lift_on'_mk] using h, end } lemma is_unit_iff_is_unit_val (f : homogeneous_localization 𝒜 x) : is_unit f.val ↔ is_unit f := ⟨λ h1, begin rcases h1 with ⟨⟨a, b, eq0, eq1⟩, (eq2 : a = f.val)⟩, rw eq2 at eq0 eq1, clear' a eq2, induction b using localization.induction_on with data, rcases data with ⟨a, ⟨b, hb⟩⟩, dsimp only at eq0 eq1, have b_f_denom_not_mem : b * f.denom ∈ x.prime_compl := λ r, or.elim (ideal.is_prime.mem_or_mem infer_instance r) (λ r2, hb r2) (λ r2, f.denom_not_mem r2), rw [f.eq_num_div_denom, localization.mk_mul, show (⟨b, hb⟩ : x.prime_compl) * ⟨f.denom, _⟩ = ⟨b * f.denom, _⟩, from rfl, show (1 : at x) = localization.mk 1 1, by erw localization.mk_self 1, localization.mk_eq_mk', is_localization.eq] at eq1, rcases eq1 with ⟨⟨c, hc⟩, eq1⟩, simp only [← subtype.val_eq_coe] at eq1, change a * f.num * 1 * c = _ at eq1, simp only [one_mul, mul_one] at eq1, have mem1 : a * f.num * c ∈ x.prime_compl := eq1.symm ▸ λ r, or.elim (ideal.is_prime.mem_or_mem infer_instance r) (by tauto)(by tauto), have mem2 : f.num ∉ x, { contrapose! mem1, erw [not_not], exact ideal.mul_mem_right _ _ (ideal.mul_mem_left _ _ mem1), }, refine ⟨⟨f, quotient.mk' ⟨f.deg, ⟨f.denom, f.denom_mem⟩, ⟨f.num, f.num_mem⟩, mem2⟩, _, _⟩, rfl⟩; simp only [ext_iff_val, mul_val, val_mk', ← subtype.val_eq_coe, f.eq_num_div_denom, localization.mk_mul, one_val]; convert localization.mk_self _; simpa only [mul_comm] end, λ ⟨⟨_, b, eq1, eq2⟩, rfl⟩, begin simp only [ext_iff_val, mul_val, one_val] at eq1 eq2, exact ⟨⟨f.val, b.val, eq1, eq2⟩, rfl⟩ end⟩ instance : nontrivial (homogeneous_localization 𝒜 x) := ⟨⟨0, 1, λ r, by simpa [ext_iff_val, zero_val, one_val, zero_ne_one] using r⟩⟩ instance : local_ring (homogeneous_localization 𝒜 x) := local_ring.of_is_unit_or_is_unit_one_sub_self $ λ a, begin simp only [← is_unit_iff_is_unit_val, sub_val, one_val], induction a using quotient.induction_on', simp only [homogeneous_localization.val_mk', ← subtype.val_eq_coe], by_cases mem1 : a.num.1 ∈ x, { right, have : a.denom.1 - a.num.1 ∈ x.prime_compl := λ h, a.denom_not_mem ((sub_add_cancel a.denom.val a.num.val) ▸ ideal.add_mem _ h mem1 : a.denom.1 ∈ x), apply is_unit_of_mul_eq_one _ (localization.mk a.denom.1 ⟨a.denom.1 - a.num.1, this⟩), simp only [sub_mul, localization.mk_mul, one_mul, localization.sub_mk, ← subtype.val_eq_coe, submonoid.coe_mul], convert localization.mk_self _, simp only [← subtype.val_eq_coe, submonoid.coe_mul], ring, }, { left, change _ ∈ x.prime_compl at mem1, apply is_unit_of_mul_eq_one _ (localization.mk a.denom.1 ⟨a.num.1, mem1⟩), rw [localization.mk_mul], convert localization.mk_self _, simpa only [mul_comm], }, end end homogeneous_localization
90a9a72d84c3ed39f817c4a1a3f6fb4338e012b2
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/ring/basic.lean
9b60f938c03a93d6ba5b13b81a809dad96bbb0f0
[ "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,777
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, Floris van Doorn, Yury Kudryashov, Neil Strickland -/ import algebra.ring.defs import algebra.hom.group import algebra.opposites /-! # Semirings and rings > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file gives lemmas about semirings, rings and domains. This is analogous to `algebra.group.basic`, the difference being that the former is about `+` and `*` separately, while the present file is about their interaction. For the definitions of semirings and rings see `algebra.ring.defs`. -/ universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {R : Type x} open function namespace add_hom /-- Left multiplication by an element of a type with distributive multiplication is an `add_hom`. -/ @[simps { fully_applied := ff}] def mul_left {R : Type*} [distrib R] (r : R) : add_hom R R := ⟨(*) r, mul_add r⟩ /-- Left multiplication by an element of a type with distributive multiplication is an `add_hom`. -/ @[simps { fully_applied := ff}] def mul_right {R : Type*} [distrib R] (r : R) : add_hom R R := ⟨λ a, a * r, λ _ _, add_mul _ _ r⟩ end add_hom section add_hom_class variables {F : Type*} [non_assoc_semiring α] [non_assoc_semiring β] [add_hom_class F α β] /-- Additive homomorphisms preserve `bit0`. -/ @[simp] lemma map_bit0 (f : F) (a : α) : (f (bit0 a) : β) = bit0 (f a) := map_add _ _ _ end add_hom_class namespace add_monoid_hom /-- Left multiplication by an element of a (semi)ring is an `add_monoid_hom` -/ def mul_left {R : Type*} [non_unital_non_assoc_semiring R] (r : R) : R →+ R := { to_fun := (*) r, map_zero' := mul_zero r, map_add' := mul_add r } @[simp] lemma coe_mul_left {R : Type*} [non_unital_non_assoc_semiring R] (r : R) : ⇑(mul_left r) = (*) r := rfl /-- Right multiplication by an element of a (semi)ring is an `add_monoid_hom` -/ def mul_right {R : Type*} [non_unital_non_assoc_semiring R] (r : R) : R →+ R := { to_fun := λ a, a * r, map_zero' := zero_mul r, map_add' := λ _ _, add_mul _ _ r } @[simp] lemma coe_mul_right {R : Type*} [non_unital_non_assoc_semiring R] (r : R) : ⇑(mul_right r) = (* r) := rfl lemma mul_right_apply {R : Type*} [non_unital_non_assoc_semiring R] (a r : R) : mul_right r a = a * r := rfl end add_monoid_hom section has_distrib_neg section has_mul variables [has_mul α] [has_distrib_neg α] open mul_opposite instance : has_distrib_neg αᵐᵒᵖ := { neg_mul := λ _ _, unop_injective $ mul_neg _ _, mul_neg := λ _ _, unop_injective $ neg_mul _ _, ..mul_opposite.has_involutive_neg _ } end has_mul section group variables [group α] [has_distrib_neg α] @[simp] lemma inv_neg' (a : α) : (- a)⁻¹ = - a⁻¹ := by rw [eq_comm, eq_inv_iff_mul_eq_one, neg_mul, mul_neg,neg_neg, mul_left_inv] end group end has_distrib_neg section non_unital_comm_ring variables [non_unital_comm_ring α] {a b c : α} local attribute [simp] add_assoc add_comm add_left_comm mul_comm /-- Vieta's formula for a quadratic equation, relating the coefficients of the polynomial with its roots. This particular version states that if we have a root `x` of a monic quadratic polynomial, then there is another root `y` such that `x + y` is negative the `a_1` coefficient and `x * y` is the `a_0` coefficient. -/ lemma Vieta_formula_quadratic {b c x : α} (h : x * x - b * x + c = 0) : ∃ y : α, y * y - b * y + c = 0 ∧ x + y = b ∧ x * y = c := begin have : c = x * (b - x) := (eq_neg_of_add_eq_zero_right h).trans (by simp [mul_sub, mul_comm]), refine ⟨b - x, _, by simp, by rw this⟩, rw [this, sub_add, ← sub_mul, sub_self] end end non_unital_comm_ring lemma succ_ne_self [non_assoc_ring α] [nontrivial α] (a : α) : a + 1 ≠ a := λ h, one_ne_zero ((add_right_inj a).mp (by simp [h])) lemma pred_ne_self [non_assoc_ring α] [nontrivial α] (a : α) : a - 1 ≠ a := λ h, one_ne_zero (neg_injective ((add_right_inj a).mp (by simpa [sub_eq_add_neg] using h))) section no_zero_divisors variable (α) lemma is_left_cancel_mul_zero.to_no_zero_divisors [ring α] [is_left_cancel_mul_zero α] : no_zero_divisors α := begin refine ⟨λ x y h, _⟩, by_cases hx : x = 0, { left, exact hx }, { right, rw [← sub_zero (x * y), ← mul_zero x, ← mul_sub] at h, convert (is_left_cancel_mul_zero.mul_left_cancel_of_ne_zero) hx h, rw [sub_zero] } end lemma is_right_cancel_mul_zero.to_no_zero_divisors [ring α] [is_right_cancel_mul_zero α] : no_zero_divisors α := begin refine ⟨λ x y h, _⟩, by_cases hy : y = 0, { right, exact hy }, { left, rw [← sub_zero (x * y), ← zero_mul y, ← sub_mul] at h, convert (is_right_cancel_mul_zero.mul_right_cancel_of_ne_zero) hy h, rw [sub_zero] } end @[priority 100] instance no_zero_divisors.to_is_cancel_mul_zero [ring α] [no_zero_divisors α] : is_cancel_mul_zero α := { mul_left_cancel_of_ne_zero := λ a b c ha h, begin rw [← sub_eq_zero, ← mul_sub] at h, exact sub_eq_zero.1 ((eq_zero_or_eq_zero_of_mul_eq_zero h).resolve_left ha) end, mul_right_cancel_of_ne_zero := λ a b c hb h, begin rw [← sub_eq_zero, ← sub_mul] at h, exact sub_eq_zero.1 ((eq_zero_or_eq_zero_of_mul_eq_zero h).resolve_right hb) end } lemma no_zero_divisors.to_is_domain [ring α] [h : nontrivial α] [no_zero_divisors α] : is_domain α := { .. no_zero_divisors.to_is_cancel_mul_zero α, .. h } @[priority 100] instance is_domain.to_no_zero_divisors [ring α] [is_domain α] : no_zero_divisors α := is_right_cancel_mul_zero.to_no_zero_divisors α end no_zero_divisors
81a7bca984fa058f0cade0e10dbf0aca894518e0
f3849be5d845a1cb97680f0bbbe03b85518312f0
/leanpkg/leanpkg/manifest.lean
1073926d01dddd0f433877bfbd7addb37db815b3
[ "Apache-2.0" ]
permissive
bjoeris/lean
0ed95125d762b17bfcb54dad1f9721f953f92eeb
4e496b78d5e73545fa4f9a807155113d8e6b0561
refs/heads/master
1,611,251,218,281
1,495,337,658,000
1,495,337,658,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,161
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Gabriel Ebner -/ import leanpkg.toml system.io namespace leanpkg inductive source | path (dir_name : string) : source | git (url rev : string) : source namespace source def from_toml (v : toml.value) : option source := (do toml.value.str dir_name ← v.lookup "path" | none, return $ path dir_name) <|> (do toml.value.str url ← v.lookup "git" | none, toml.value.str rev ← (v.lookup "rev") | none, return $ git url rev) <|> (do toml.value.str url ← v.lookup "git" | none, return $ git url "master") def to_toml : ∀ (s : source), toml.value | (path dir_name) := toml.value.table [("path", toml.value.str dir_name)] | (git url "master") := toml.value.table [("git", toml.value.str url)] | (git url rev) := toml.value.table [("git", toml.value.str url), ("rev", toml.value.str rev)] instance : has_to_string source := ⟨λ s, s.to_toml.to_string⟩ end source structure dependency := (name : string) (src : source) namespace dependency instance : has_to_string dependency := ⟨λ d, d.name ++ " = " ++ to_string d.src⟩ end dependency structure manifest := (name : string) (version : string) (path : option string) (dependencies : list dependency) namespace manifest def effective_path (m : manifest) : list string := [match m.path with some p := p | none := "." end] def from_toml (t : toml.value) : option manifest := do pkg ← t.lookup "package", toml.value.str n ← pkg.lookup "name" | none, toml.value.str ver ← pkg.lookup "version" | none, path ← match pkg.lookup "path" with | some (toml.value.str path) := some (some path) | none := some none | _ := none end, toml.value.table deps ← t.lookup "dependencies" <|> some (toml.value.table []) | none, deps ← monad.for deps (λ ⟨n, src⟩, do src ← source.from_toml src, return $ dependency.mk n src), return { name := n, version := ver, path := path, dependencies := deps } def to_toml (d : manifest) : toml.value := let pkg := [("name", toml.value.str d.name), ("version", toml.value.str d.version)], pkg := match d.path with some p := pkg ++ [("path", toml.value.str p)] | none := pkg end, deps := toml.value.table $ d.dependencies.for $ λ dep, (dep.name, dep.src.to_toml) in toml.value.table [("package", toml.value.table pkg), ("dependencies", deps)] instance : has_to_string manifest := ⟨λ d, d.to_toml.to_string⟩ def from_string (s : string) : option manifest := match parser.run_string toml.File s with | sum.inr toml := from_toml toml | sum.inl _ := none end def from_file [io.interface] (fn : string) : io manifest := do cnts ← io.fs.read_file fn, toml ← (match parser.run toml.File cnts with | sum.inl err := io.fail $ "toml parse error in " ++ fn ++ "\n\n" ++ err | sum.inr res := return res end), some manifest ← return (from_toml toml) | io.fail ("cannot read manifest from " ++ fn ++ "\n\n" ++ toml.to_string), return manifest end manifest def leanpkg_toml_fn := "leanpkg.toml" end leanpkg
04573425e326d12132ecc2f1af1374ed2d2fb4f3
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/number_theory/divisors.lean
539e0e764b49d7403b1ae25a5bac7a4f1b5fd930
[ "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
16,514
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 algebra.big_operators.order import data.nat.interval import data.nat.prime /-! # Divisor finsets This file defines sets of divisors of a natural number. This is particularly useful as background for defining Dirichlet convolution. ## Main Definitions Let `n : ℕ`. All of the following definitions are in the `nat` namespace: * `divisors n` is the `finset` of natural numbers that divide `n`. * `proper_divisors n` is the `finset` of natural numbers that divide `n`, other than `n`. * `divisors_antidiagonal n` is the `finset` of pairs `(x,y)` such that `x * y = n`. * `perfect n` is true when `n` is positive and the sum of `proper_divisors n` is `n`. ## Implementation details * `divisors 0`, `proper_divisors 0`, and `divisors_antidiagonal 0` are defined to be `∅`. ## Tags divisors, perfect numbers -/ open_locale classical open_locale big_operators open finset namespace nat variable (n : ℕ) /-- `divisors n` is the `finset` of divisors of `n`. As a special case, `divisors 0 = ∅`. -/ def divisors : finset ℕ := finset.filter (λ x : ℕ, x ∣ n) (finset.Ico 1 (n + 1)) /-- `proper_divisors n` is the `finset` of divisors of `n`, other than `n`. As a special case, `proper_divisors 0 = ∅`. -/ def proper_divisors : finset ℕ := finset.filter (λ x : ℕ, x ∣ n) (finset.Ico 1 n) /-- `divisors_antidiagonal n` is the `finset` of pairs `(x,y)` such that `x * y = n`. As a special case, `divisors_antidiagonal 0 = ∅`. -/ def divisors_antidiagonal : finset (ℕ × ℕ) := (Ico 1 (n + 1) ×ˢ Ico 1 (n + 1)).filter (λ x, x.fst * x.snd = n) variable {n} @[simp] lemma filter_dvd_eq_divisors (h : n ≠ 0) : (finset.range n.succ).filter (∣ n) = n.divisors := begin ext, simp only [divisors, mem_filter, mem_range, mem_Ico, and.congr_left_iff, iff_and_self], exact λ ha _, succ_le_iff.mpr (pos_of_dvd_of_pos ha h.bot_lt), end @[simp] lemma filter_dvd_eq_proper_divisors (h : n ≠ 0) : (finset.range n).filter (∣ n) = n.proper_divisors := begin ext, simp only [proper_divisors, mem_filter, mem_range, mem_Ico, and.congr_left_iff, iff_and_self], exact λ ha _, succ_le_iff.mpr (pos_of_dvd_of_pos ha h.bot_lt), end lemma proper_divisors.not_self_mem : ¬ n ∈ proper_divisors n := by simp [proper_divisors] @[simp] lemma mem_proper_divisors {m : ℕ} : n ∈ proper_divisors m ↔ n ∣ m ∧ n < m := begin rcases eq_or_ne m 0 with rfl | hm, { simp [proper_divisors] }, simp only [and_comm, ←filter_dvd_eq_proper_divisors hm, mem_filter, mem_range], end lemma divisors_eq_proper_divisors_insert_self_of_pos (h : 0 < n): divisors n = has_insert.insert n (proper_divisors n) := by rw [divisors, proper_divisors, Ico_succ_right_eq_insert_Ico h, finset.filter_insert, if_pos (dvd_refl n)] @[simp] lemma mem_divisors {m : ℕ} : n ∈ divisors m ↔ (n ∣ m ∧ m ≠ 0) := begin rcases eq_or_ne m 0 with rfl | hm, { simp [divisors] }, simp only [hm, ne.def, not_false_iff, and_true, ←filter_dvd_eq_divisors hm, mem_filter, mem_range, and_iff_right_iff_imp, lt_succ_iff], exact le_of_dvd hm.bot_lt, end lemma one_mem_divisors : 1 ∈ divisors n ↔ n ≠ 0 := by simp lemma mem_divisors_self (n : ℕ) (h : n ≠ 0) : n ∈ n.divisors := mem_divisors.2 ⟨dvd_rfl, h⟩ lemma dvd_of_mem_divisors {m : ℕ} (h : n ∈ divisors m) : n ∣ m := begin cases m, { apply dvd_zero }, { simp [mem_divisors.1 h], } end @[simp] lemma mem_divisors_antidiagonal {x : ℕ × ℕ} : x ∈ divisors_antidiagonal n ↔ x.fst * x.snd = n ∧ n ≠ 0 := begin simp only [divisors_antidiagonal, finset.mem_Ico, ne.def, finset.mem_filter, finset.mem_product], rw and_comm, apply and_congr_right, rintro rfl, split; intro h, { contrapose! h, simp [h], }, { rw [nat.lt_add_one_iff, nat.lt_add_one_iff], rw [mul_eq_zero, decidable.not_or_iff_and_not] at h, simp only [succ_le_of_lt (nat.pos_of_ne_zero h.1), succ_le_of_lt (nat.pos_of_ne_zero h.2), true_and], exact ⟨le_mul_of_pos_right (nat.pos_of_ne_zero h.2), le_mul_of_pos_left (nat.pos_of_ne_zero h.1)⟩ } end variable {n} lemma divisor_le {m : ℕ}: n ∈ divisors m → n ≤ m := begin cases m, { simp }, simp only [mem_divisors, m.succ_ne_zero, and_true, ne.def, not_false_iff], exact nat.le_of_dvd (nat.succ_pos m), end lemma divisors_subset_of_dvd {m : ℕ} (hzero : n ≠ 0) (h : m ∣ n) : divisors m ⊆ divisors n := finset.subset_iff.2 $ λ x hx, nat.mem_divisors.mpr (⟨(nat.mem_divisors.mp hx).1.trans h, hzero⟩) lemma divisors_subset_proper_divisors {m : ℕ} (hzero : n ≠ 0) (h : m ∣ n) (hdiff : m ≠ n) : divisors m ⊆ proper_divisors n := begin apply finset.subset_iff.2, intros x hx, exact nat.mem_proper_divisors.2 (⟨(nat.mem_divisors.1 hx).1.trans h, lt_of_le_of_lt (divisor_le hx) (lt_of_le_of_ne (divisor_le (nat.mem_divisors.2 ⟨h, hzero⟩)) hdiff)⟩) end @[simp] lemma divisors_zero : divisors 0 = ∅ := by { ext, simp } @[simp] lemma proper_divisors_zero : proper_divisors 0 = ∅ := by { ext, simp } lemma proper_divisors_subset_divisors : proper_divisors n ⊆ divisors n := filter_subset_filter _ $ Ico_subset_Ico_right n.le_succ @[simp] lemma divisors_one : divisors 1 = {1} := by { ext, simp } @[simp] lemma proper_divisors_one : proper_divisors 1 = ∅ := by rw [proper_divisors, Ico_self, filter_empty] lemma pos_of_mem_divisors {m : ℕ} (h : m ∈ n.divisors) : 0 < m := begin cases m, { rw [mem_divisors, zero_dvd_iff] at h, cases h.2 h.1 }, apply nat.succ_pos, end lemma pos_of_mem_proper_divisors {m : ℕ} (h : m ∈ n.proper_divisors) : 0 < m := pos_of_mem_divisors (proper_divisors_subset_divisors h) lemma one_mem_proper_divisors_iff_one_lt : 1 ∈ n.proper_divisors ↔ 1 < n := by rw [mem_proper_divisors, and_iff_right (one_dvd _)] @[simp] lemma divisors_antidiagonal_zero : divisors_antidiagonal 0 = ∅ := by { ext, simp } @[simp] lemma divisors_antidiagonal_one : divisors_antidiagonal 1 = {(1,1)} := by { ext, simp [nat.mul_eq_one_iff, prod.ext_iff], } lemma swap_mem_divisors_antidiagonal {x : ℕ × ℕ} (h : x ∈ divisors_antidiagonal n) : x.swap ∈ divisors_antidiagonal n := begin rw [mem_divisors_antidiagonal, mul_comm] at h, simp [h.1, h.2], end lemma fst_mem_divisors_of_mem_antidiagonal {x : ℕ × ℕ} (h : x ∈ divisors_antidiagonal n) : x.fst ∈ divisors n := begin rw mem_divisors_antidiagonal at h, simp [dvd.intro _ h.1, h.2], end lemma snd_mem_divisors_of_mem_antidiagonal {x : ℕ × ℕ} (h : x ∈ divisors_antidiagonal n) : x.snd ∈ divisors n := begin rw mem_divisors_antidiagonal at h, simp [dvd.intro_left _ h.1, h.2], end @[simp] lemma map_swap_divisors_antidiagonal : (divisors_antidiagonal n).map ⟨prod.swap, prod.swap_right_inverse.injective⟩ = divisors_antidiagonal n := begin ext, simp only [exists_prop, mem_divisors_antidiagonal, finset.mem_map, function.embedding.coe_fn_mk, ne.def, prod.swap_prod_mk, prod.exists], split, { rintros ⟨x, y, ⟨⟨rfl, h⟩, rfl⟩⟩, simp [mul_comm, h], }, { rintros ⟨rfl, h⟩, use [a.snd, a.fst], rw mul_comm, simp [h] } end @[simp] lemma image_fst_divisors_antidiagonal : (divisors_antidiagonal n).image prod.fst = divisors n := by { ext, simp [has_dvd.dvd, @eq_comm _ n (_ * _)] } @[simp] lemma image_snd_divisors_antidiagonal : (divisors_antidiagonal n).image prod.snd = divisors n := begin rw [←map_swap_divisors_antidiagonal, map_eq_image, image_image], exact image_fst_divisors_antidiagonal end lemma map_div_right_divisors : n.divisors.map ⟨λ d, (d, n/d), λ p₁ p₂, congr_arg prod.fst⟩ = n.divisors_antidiagonal := begin obtain rfl | hn := decidable.eq_or_ne n 0, { simp }, ext ⟨d, nd⟩, simp only [and_true, finset.mem_map, exists_eq_left, ne.def, hn, not_false_iff, mem_divisors_antidiagonal, function.embedding.coe_fn_mk, mem_divisors], split, { rintro ⟨a, ⟨k, rfl⟩, h⟩, obtain ⟨rfl, rfl⟩ := prod.mk.inj_iff.1 h, have := (mul_ne_zero_iff.1 hn).1, rw nat.mul_div_cancel_left _ (zero_lt_iff.mpr this), }, { rintro rfl, refine ⟨d, dvd_mul_right _ _, _⟩, have := (mul_ne_zero_iff.1 hn).1, rw nat.mul_div_cancel_left _ (zero_lt_iff.mpr this) } end lemma map_div_left_divisors : n.divisors.map ⟨λ d, (n/d, d), λ p₁ p₂, congr_arg prod.snd⟩ = n.divisors_antidiagonal := begin apply finset.map_injective ⟨prod.swap, prod.swap_right_inverse.injective⟩, rw [map_swap_divisors_antidiagonal, ←map_div_right_divisors, finset.map_map], refl, end lemma sum_divisors_eq_sum_proper_divisors_add_self : ∑ i in divisors n, i = ∑ i in proper_divisors n, i + n := begin cases n, { simp }, { rw [divisors_eq_proper_divisors_insert_self_of_pos (nat.succ_pos _), finset.sum_insert (proper_divisors.not_self_mem), add_comm] } end /-- `n : ℕ` is perfect if and only the sum of the proper divisors of `n` is `n` and `n` is positive. -/ def perfect (n : ℕ) : Prop := (∑ i in proper_divisors n, i = n) ∧ 0 < n theorem perfect_iff_sum_proper_divisors (h : 0 < n) : perfect n ↔ ∑ i in proper_divisors n, i = n := and_iff_left h theorem perfect_iff_sum_divisors_eq_two_mul (h : 0 < n) : perfect n ↔ ∑ i in divisors n, i = 2 * n := begin rw [perfect_iff_sum_proper_divisors h, sum_divisors_eq_sum_proper_divisors_add_self, two_mul], split; intro h, { rw h }, { apply add_right_cancel h } end lemma mem_divisors_prime_pow {p : ℕ} (pp : p.prime) (k : ℕ) {x : ℕ} : x ∈ divisors (p ^ k) ↔ ∃ (j : ℕ) (H : j ≤ k), x = p ^ j := by rw [mem_divisors, nat.dvd_prime_pow pp, and_iff_left (ne_of_gt (pow_pos pp.pos k))] lemma prime.divisors {p : ℕ} (pp : p.prime) : divisors p = {1, p} := begin ext, rw [mem_divisors, dvd_prime pp, and_iff_left pp.ne_zero, finset.mem_insert, finset.mem_singleton] end lemma prime.proper_divisors {p : ℕ} (pp : p.prime) : proper_divisors p = {1} := by rw [← erase_insert (proper_divisors.not_self_mem), ← divisors_eq_proper_divisors_insert_self_of_pos pp.pos, pp.divisors, pair_comm, erase_insert (λ con, pp.ne_one (mem_singleton.1 con))] lemma divisors_prime_pow {p : ℕ} (pp : p.prime) (k : ℕ) : divisors (p ^ k) = (finset.range (k + 1)).map ⟨pow p, pow_right_injective pp.two_le⟩ := by { ext, simp [mem_divisors_prime_pow, pp, nat.lt_succ_iff, @eq_comm _ a] } lemma eq_proper_divisors_of_subset_of_sum_eq_sum {s : finset ℕ} (hsub : s ⊆ n.proper_divisors) : ∑ x in s, x = ∑ x in n.proper_divisors, x → s = n.proper_divisors := begin cases n, { rw [proper_divisors_zero, subset_empty] at hsub, simp [hsub] }, classical, rw [← sum_sdiff hsub], intros h, apply subset.antisymm hsub, rw [← sdiff_eq_empty_iff_subset], contrapose h, rw [← ne.def, ← nonempty_iff_ne_empty] at h, apply ne_of_lt, rw [← zero_add (∑ x in s, x), ← add_assoc, add_zero], apply add_lt_add_right, have hlt := sum_lt_sum_of_nonempty h (λ x hx, pos_of_mem_proper_divisors (sdiff_subset _ _ hx)), simp only [sum_const_zero] at hlt, apply hlt end lemma sum_proper_divisors_dvd (h : ∑ x in n.proper_divisors, x ∣ n) : (∑ x in n.proper_divisors, x = 1) ∨ (∑ x in n.proper_divisors, x = n) := begin cases n, { simp }, cases n, { contrapose! h, simp, }, rw or_iff_not_imp_right, intro ne_n, have hlt : ∑ x in n.succ.succ.proper_divisors, x < n.succ.succ := lt_of_le_of_ne (nat.le_of_dvd (nat.succ_pos _) h) ne_n, symmetry, rw [← mem_singleton, eq_proper_divisors_of_subset_of_sum_eq_sum (singleton_subset_iff.2 (mem_proper_divisors.2 ⟨h, hlt⟩)) sum_singleton, mem_proper_divisors], refine ⟨one_dvd _, nat.succ_lt_succ (nat.succ_pos _)⟩, end @[simp, to_additive] lemma prime.prod_proper_divisors {α : Type*} [comm_monoid α] {p : ℕ} {f : ℕ → α} (h : p.prime) : ∏ x in p.proper_divisors, f x = f 1 := by simp [h.proper_divisors] @[simp, to_additive] lemma prime.prod_divisors {α : Type*} [comm_monoid α] {p : ℕ} {f : ℕ → α} (h : p.prime) : ∏ x in p.divisors, f x = f p * f 1 := by rw [divisors_eq_proper_divisors_insert_self_of_pos h.pos, prod_insert proper_divisors.not_self_mem, h.prod_proper_divisors] lemma proper_divisors_eq_singleton_one_iff_prime : n.proper_divisors = {1} ↔ n.prime := ⟨λ h, begin have h1 := mem_singleton.2 rfl, rw [← h, mem_proper_divisors] at h1, refine nat.prime_def_lt''.mpr ⟨h1.2, λ m hdvd, _⟩, rw [← mem_singleton, ← h, mem_proper_divisors], have hle := nat.le_of_dvd (lt_trans (nat.succ_pos _) h1.2) hdvd, exact or.imp_left (λ hlt, ⟨hdvd, hlt⟩) hle.lt_or_eq end, prime.proper_divisors⟩ lemma sum_proper_divisors_eq_one_iff_prime : ∑ x in n.proper_divisors, x = 1 ↔ n.prime := begin cases n, { simp [nat.not_prime_zero] }, cases n, { simp [nat.not_prime_one] }, rw [← proper_divisors_eq_singleton_one_iff_prime], refine ⟨λ h, _, λ h, h.symm ▸ sum_singleton⟩, rw [@eq_comm (finset ℕ) _ _], apply eq_proper_divisors_of_subset_of_sum_eq_sum (singleton_subset_iff.2 (one_mem_proper_divisors_iff_one_lt.2 (succ_lt_succ (nat.succ_pos _)))) (eq.trans sum_singleton h.symm) end lemma mem_proper_divisors_prime_pow {p : ℕ} (pp : p.prime) (k : ℕ) {x : ℕ} : x ∈ proper_divisors (p ^ k) ↔ ∃ (j : ℕ) (H : j < k), x = p ^ j := begin rw [mem_proper_divisors, nat.dvd_prime_pow pp, ← exists_and_distrib_right], simp only [exists_prop, and_assoc], apply exists_congr, intro a, split; intro h, { rcases h with ⟨h_left, rfl, h_right⟩, rwa pow_lt_pow_iff pp.one_lt at h_right, simpa, }, { rcases h with ⟨h_left, rfl⟩, rwa pow_lt_pow_iff pp.one_lt, simp [h_left, le_of_lt], }, end lemma proper_divisors_prime_pow {p : ℕ} (pp : p.prime) (k : ℕ) : proper_divisors (p ^ k) = (finset.range k).map ⟨pow p, pow_right_injective pp.two_le⟩ := by { ext, simp [mem_proper_divisors_prime_pow, pp, nat.lt_succ_iff, @eq_comm _ a], } @[simp, to_additive] lemma prod_proper_divisors_prime_pow {α : Type*} [comm_monoid α] {k p : ℕ} {f : ℕ → α} (h : p.prime) : ∏ x in (p ^ k).proper_divisors, f x = ∏ x in range k, f (p ^ x) := by simp [h, proper_divisors_prime_pow] @[simp, to_additive sum_divisors_prime_pow] lemma prod_divisors_prime_pow {α : Type*} [comm_monoid α] {k p : ℕ} {f : ℕ → α} (h : p.prime) : ∏ x in (p ^ k).divisors, f x = ∏ x in range (k + 1), f (p ^ x) := by simp [h, divisors_prime_pow] @[to_additive] lemma prod_divisors_antidiagonal {M : Type*} [comm_monoid M] (f : ℕ → ℕ → M) {n : ℕ} : ∏ i in n.divisors_antidiagonal, f i.1 i.2 = ∏ i in n.divisors, f i (n / i) := begin rw [←map_div_right_divisors, finset.prod_map], refl, end @[to_additive] lemma prod_divisors_antidiagonal' {M : Type*} [comm_monoid M] (f : ℕ → ℕ → M) {n : ℕ} : ∏ i in n.divisors_antidiagonal, f i.1 i.2 = ∏ i in n.divisors, f (n / i) i := begin rw [←map_swap_divisors_antidiagonal, finset.prod_map], exact prod_divisors_antidiagonal (λ i j, f j i), end /-- The factors of `n` are the prime divisors -/ lemma prime_divisors_eq_to_filter_divisors_prime (n : ℕ) : n.factors.to_finset = (divisors n).filter prime := begin rcases n.eq_zero_or_pos with rfl | hn, { simp }, { ext q, simpa [hn, hn.ne', mem_factors] using and_comm (prime q) (q ∣ n) } end @[simp] lemma image_div_divisors_eq_divisors (n : ℕ) : image (λ (x : ℕ), n / x) n.divisors = n.divisors := begin by_cases hn : n = 0, { simp [hn] }, ext, split, { rw mem_image, rintros ⟨x, hx1, hx2⟩, rw mem_divisors at *, refine ⟨_,hn⟩, rw ←hx2, exact div_dvd_of_dvd hx1.1 }, { rw [mem_divisors, mem_image], rintros ⟨h1, -⟩, exact ⟨n/a, mem_divisors.mpr ⟨div_dvd_of_dvd h1, hn⟩, nat.div_div_self h1 hn⟩ }, end @[simp, to_additive sum_div_divisors] lemma prod_div_divisors {α : Type*} [comm_monoid α] (n : ℕ) (f : ℕ → α) : ∏ d in n.divisors, f (n/d) = n.divisors.prod f := begin by_cases hn : n = 0, { simp [hn] }, rw ←prod_image, { exact prod_congr (image_div_divisors_eq_divisors n) (by simp) }, { intros x hx y hy h, rw mem_divisors at hx hy, exact (div_eq_iff_eq_of_dvd_dvd hn hx.1 hy.1).mp h } end end nat
c58ec954518a43530d3572752411a0e97f2f25ce
a4673261e60b025e2c8c825dfa4ab9108246c32e
/tests/lean/run/coeSort1.lean
0b68a869debb18a0a8c114a9a21062731e5e1d89
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
273
lean
universes u def Below (n : Nat) : Nat → Prop := (· < n) def f {n : Nat} (v : Subtype (Below n)) : Nat := v + 1 instance pred2subtype {α : Type u} : CoeSort (α → Prop) (Type u) := CoeSort.mk (fun p => Subtype p) def g {n : Nat} (v : Below n) : Nat := v.val + 1
9371de2f956c780fa6d270cca3c0c796a65ad117
4727251e0cd73359b15b664c3170e5d754078599
/src/data/polynomial/degree/card_pow_degree.lean
d5838fe31283fe049e78926610fd73cbecb5db95
[ "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
3,676
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 algebra.order.euclidean_absolute_value import data.polynomial.field_division /-! # Absolute value on polynomials over a finite field. Let `Fq` be a finite field of cardinality `q`, then the map sending a polynomial `p` to `q ^ degree p` (where `q ^ degree 0 = 0`) is an absolute value. ## Main definitions * `polynomial.card_pow_degree` is an absolute value on `𝔽_q[t]`, the ring of polynomials over a finite field of cardinality `q`, mapping a polynomial `p` to `q ^ degree p` (where `q ^ degree 0 = 0`) ## Main results * `polynomial.card_pow_degree_is_euclidean`: `card_pow_degree` respects the Euclidean domain structure on the ring of polynomials -/ namespace polynomial variables {Fq : Type*} [field Fq] [fintype Fq] open absolute_value open_locale classical polynomial /-- `card_pow_degree` is the absolute value on `𝔽_q[t]` sending `f` to `q ^ degree f`. `card_pow_degree 0` is defined to be `0`. -/ noncomputable def card_pow_degree : absolute_value Fq[X] ℤ := have card_pos : 0 < fintype.card Fq := fintype.card_pos_iff.mpr infer_instance, have pow_pos : ∀ n, 0 < (fintype.card Fq : ℤ) ^ n := λ n, pow_pos (int.coe_nat_pos.mpr card_pos) n, { to_fun := λ p, if p = 0 then 0 else fintype.card Fq ^ p.nat_degree, nonneg' := λ p, by { dsimp, split_ifs, { refl }, exact pow_nonneg (int.coe_zero_le _) _ }, eq_zero' := λ p, ite_eq_left_iff.trans $ ⟨λ h, by { contrapose! h, exact ⟨h, (pow_pos _).ne'⟩ }, absurd⟩, add_le' := λ p q, begin by_cases hp : p = 0, { simp [hp] }, by_cases hq : q = 0, { simp [hq] }, by_cases hpq : p + q = 0, { simp only [hpq, hp, hq, eq_self_iff_true, if_true, if_false], exact add_nonneg (pow_pos _).le (pow_pos _).le }, simp only [hpq, hp, hq, if_false], refine le_trans (pow_le_pow (by linarith) (polynomial.nat_degree_add_le _ _)) _, refine le_trans (le_max_iff.mpr _) (max_le_add_of_nonneg (pow_nonneg (by linarith) _) (pow_nonneg (by linarith) _)), exact (max_choice p.nat_degree q.nat_degree).imp (λ h, by rw [h]) (λ h, by rw [h]) end, map_mul' := λ p q, begin by_cases hp : p = 0, { simp [hp] }, by_cases hq : q = 0, { simp [hq] }, have hpq : p * q ≠ 0 := mul_ne_zero hp hq, simp only [hpq, hp, hq, eq_self_iff_true, if_true, if_false, polynomial.nat_degree_mul hp hq, pow_add], end } lemma card_pow_degree_apply (p : Fq[X]) : card_pow_degree p = if p = 0 then 0 else fintype.card Fq ^ nat_degree p := rfl @[simp] lemma card_pow_degree_zero : card_pow_degree (0 : Fq[X]) = 0 := if_pos rfl @[simp] lemma card_pow_degree_nonzero (p : Fq[X]) (hp : p ≠ 0) : card_pow_degree p = fintype.card Fq ^ p.nat_degree := if_neg hp lemma card_pow_degree_is_euclidean : is_euclidean (card_pow_degree : absolute_value Fq[X] ℤ) := have card_pos : 0 < fintype.card Fq := fintype.card_pos_iff.mpr infer_instance, have pow_pos : ∀ n, 0 < (fintype.card Fq : ℤ) ^ n := λ n, pow_pos (int.coe_nat_pos.mpr card_pos) n, { map_lt_map_iff' := λ p q, begin simp only [euclidean_domain.r, card_pow_degree_apply], split_ifs with hp hq hq, { simp only [hp, hq, lt_self_iff_false] }, { simp only [hp, hq, degree_zero, ne.def, bot_lt_iff_ne_bot, degree_eq_bot, pow_pos, not_false_iff] }, { simp only [hp, hq, degree_zero, not_lt_bot, (pow_pos _).not_lt] }, { rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq, with_bot.coe_lt_coe, pow_lt_pow_iff], exact_mod_cast @fintype.one_lt_card Fq _ _ }, end } end polynomial
a16e7e9e50c970ed0964342e29d38cb84e3bf229
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/theories/analysis/sqrt.lean
3631344d924f0cde7024c0f369b4c29a9eb65334
[ "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
3,197
lean
/- Copyright (c) 2015 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Jeremy Avigad The square root function. -/ import .ivt open analysis real classical topology noncomputable theory private definition sqr_lb (x : ℝ) : ℝ := 0 private theorem sqr_lb_is_lb (x : ℝ) (H : x ≥ 0) : (sqr_lb x) * (sqr_lb x) ≤ x := by rewrite [↑sqr_lb, zero_mul]; assumption private definition sqr_ub (x : ℝ) : ℝ := x + 1 private theorem sqr_ub_is_ub (x : ℝ) (H : x ≥ 0) : (sqr_ub x) * (sqr_ub x) ≥ x := begin rewrite [↑sqr_ub, left_distrib, mul_one, right_distrib, one_mul, {x + 1}add.comm, -*add.assoc], apply le_add_of_nonneg_left, repeat apply add_nonneg, apply mul_nonneg, repeat assumption, apply zero_le_one end private theorem lb_le_ub (x : ℝ) (H : x ≥ 0) : sqr_lb x ≤ sqr_ub x := begin rewrite [↑sqr_lb, ↑sqr_ub], apply add_nonneg, assumption, apply zero_le_one end private lemma sqr_cts : continuous (λ x : ℝ, x * x) := continuous_mul_of_continuous continuous_id continuous_id definition sqrt (x : ℝ) : ℝ := if H : x ≥ 0 then some (intermediate_value_incr_weak sqr_cts (lb_le_ub x H) (sqr_lb_is_lb x H) (sqr_ub_is_ub x H)) else 0 private theorem sqrt_spec {x : ℝ} (H : x ≥ 0) : sqrt x * sqrt x = x ∧ sqrt x ≥ 0 := begin rewrite [↑sqrt, dif_pos H], note Hs := some_spec (intermediate_value_incr_weak sqr_cts (lb_le_ub x H) (sqr_lb_is_lb x H) (sqr_ub_is_ub x H)), cases Hs with Hs1 Hs2, cases Hs2 with Hs2a Hs2b, exact and.intro Hs2b Hs1 end theorem sqrt_mul_self {x : ℝ} (H : x ≥ 0) : sqrt x * sqrt x = x := and.left (sqrt_spec H) theorem sqrt_nonneg (x : ℝ) : sqrt x ≥ 0 := if H : x ≥ 0 then and.right (sqrt_spec H) else by rewrite [↑sqrt, dif_neg H]; exact le.refl 0 theorem sqrt_squared {x : ℝ} (H : x ≥ 0) : (sqrt x)^2 = x := by krewrite [pow_two, sqrt_mul_self H] theorem sqrt_zero : sqrt (0 : ℝ) = 0 := have sqrt 0 * sqrt 0 = 0, from sqrt_mul_self !le.refl, or.elim (eq_zero_or_eq_zero_of_mul_eq_zero this) (λ H, H) (λ H, H) theorem sqrt_squared_of_nonneg {x : ℝ} (H : x ≥ 0) : sqrt (x^2) = x := have sqrt (x^2)^2 = x^2, from sqrt_squared (squared_nonneg x), eq_of_squared_eq_squared_of_nonneg (sqrt_nonneg (x^2)) H this theorem sqrt_squared' (x : ℝ) : sqrt (x^2) = abs x := have x^2 = (abs x)^2, by krewrite [+pow_two, -abs_mul, abs_mul_self], using this, by rewrite [this, sqrt_squared_of_nonneg (abs_nonneg x)] theorem sqrt_mul {x y : ℝ} (Hx : x ≥ 0) (Hy : y ≥ 0) : sqrt (x * y) = sqrt x * sqrt y := have (sqrt (x * y))^2 = (sqrt x * sqrt y)^2, from calc (sqrt (x * y))^2 = x * y : by rewrite [sqrt_squared (mul_nonneg Hx Hy)] ... = (sqrt x)^2 * (sqrt y)^2 : by rewrite [sqrt_squared Hx, sqrt_squared Hy] ... = (sqrt x * sqrt y)^2 : by krewrite [*pow_two]; rewrite [*mul.assoc, mul.left_comm (sqrt y)], eq_of_squared_eq_squared_of_nonneg !sqrt_nonneg (mul_nonneg !sqrt_nonneg !sqrt_nonneg) this
643890a34ef80b9a7a0fce34ff06ca154b883bee
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/analysis/normed_space/finite_dimension.lean
3aae442367bab67b0250f05754f5c75478d8b7d0
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
12,692
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 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 open_locale classical -- To get a reasonable compile time for `continuous_equiv_fun_basis`, typeclass inference needs -- to be guided. local attribute [instance, priority 10000] pi.module normed_space.to_module submodule.add_comm_group submodule.module linear_map.finite_dimensional_range Pi.complete nondiscrete_normed_field.to_normed_field /-- 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, finset.sum finset.univ (λ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 (equiv_fun_basis hξ) := begin unfreezeI, induction hn : fintype.card ι with n IH generalizing ι E, { apply linear_map.continuous_of_bound _ 0 (λx, _), have : equiv_fun_basis hξ x = 0, by { ext i, exact (fintype.card_eq_zero_iff.1 hn i).elim }, change ∥equiv_fun_basis hξ x∥ ≤ 0 * ∥x∥, rw this, simp [norm_nonneg] }, { haveI : finite_dimensional 𝕜 E := of_finite_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 (equiv_fun_basis b_basis).symm.to_equiv, { have : fintype.card b = n, by { rw ← s_dim, exact (findim_eq_card_basis b_basis).symm }, have : continuous (equiv_fun_basis b_basis) := IH (subtype.val : b → s) b_basis this, exact (equiv_fun_basis b_basis).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 is_closed_of_is_complete this }, -- 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), ∥equiv_fun_basis hξ x i∥ ≤ C * ∥x∥, { assume i, let f : E →ₗ[𝕜] 𝕜 := (linear_map.proj i).comp (equiv_fun_basis hξ), 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 := finset.sum finset.univ C0, 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 (equiv_fun_basis b_basis) := continuous_equiv_fun_basis _ b_basis, have B : continuous (f.comp ((equiv_fun_basis b_basis).symm : (b → 𝕜) →ₗ[𝕜] E)) := linear_map.continuous_on_pi _, have : continuous ((f.comp ((equiv_fun_basis b_basis).symm : (b → 𝕜) →ₗ[𝕜] E)) ∘ (equiv_fun_basis b_basis)) := B.comp A, convert this, ext x, dsimp, rw linear_equiv.symm_apply_apply end /-- 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 } /-- 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 (equiv_fun_basis b_basis).symm := linear_equiv.uniform_embedding _ (linear_map.continuous_of_finite_dimensional _) (linear_map.continuous_of_finite_dimensional _), change uniform_embedding (equiv_fun_basis b_basis).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) := is_closed_of_is_complete s.complete_of_finite_dimensional 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⟩ 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 := equiv_fun_basis b_basis, 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), 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
f83f7e469b81168c6e5aa6c02543a6684dc91632
9028d228ac200bbefe3a711342514dd4e4458bff
/src/logic/nontrivial.lean
35d95464ce89806a844730af76b775f457b4134e
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,104
lean
/- Copyright (c) 2020 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 logic.unique import logic.function.basic /-! # Nontrivial types A type is *nontrivial* if it contains at least two elements. This is useful in particular for rings (where it is equivalent to the fact that zero is different from one) and for vector spaces (where it is equivalent to the fact that the dimension is positive). We introduce a typeclass `nontrivial` formalizing this property. -/ variables {α : Type*} {β : Type*} open_locale classical /-- Predicate typeclass for expressing that a type is not reduced to a single element. In rings, this is equivalent to `0 ≠ 1`. In vector spaces, this is equivalent to positive dimension. -/ class nontrivial (α : Type*) : Prop := (exists_pair_ne : ∃ (x y : α), x ≠ y) lemma nontrivial_iff : nontrivial α ↔ ∃ (x y : α), x ≠ y := ⟨λ h, h.exists_pair_ne, λ h, ⟨h⟩⟩ lemma exists_pair_ne (α : Type*) [nontrivial α] : ∃ (x y : α), x ≠ y := nontrivial.exists_pair_ne lemma exists_ne [nontrivial α] (x : α) : ∃ y, y ≠ x := begin rcases exists_pair_ne α with ⟨y, y', h⟩, by_cases hx : x = y, { rw ← hx at h, exact ⟨y', h.symm⟩ }, { exact ⟨y, ne.symm hx⟩ } end -- `x` and `y` are explicit here, as they are often needed to guide typechecking of `h`. lemma nontrivial_of_ne (x y : α) (h : x ≠ y) : nontrivial α := ⟨⟨x, y, h⟩⟩ -- `x` and `y` are explicit here, as they are often needed to guide typechecking of `h`. lemma nontrivial_of_lt [preorder α] (x y : α) (h : x < y) : nontrivial α := ⟨⟨x, y, ne_of_lt h⟩⟩ @[priority 100] -- see Note [lower instance priority] instance nontrivial.to_nonempty [nontrivial α] : nonempty α := let ⟨x, _⟩ := exists_pair_ne α in ⟨x⟩ /-- An inhabited type is either nontrivial, or has a unique element. -/ noncomputable def nontrivial_psum_unique (α : Type*) [inhabited α] : psum (nontrivial α) (unique α) := if h : nontrivial α then psum.inl h else psum.inr { default := default α, uniq := λ (x : α), begin change x = default α, contrapose! h, use [x, default α] end } lemma subsingleton_iff : subsingleton α ↔ ∀ (x y : α), x = y := ⟨by { introsI h, exact subsingleton.elim }, λ h, ⟨h⟩⟩ lemma not_nontrivial_iff_subsingleton : ¬(nontrivial α) ↔ subsingleton α := by { rw [nontrivial_iff, subsingleton_iff], push_neg, refl } lemma not_subsingleton (α) [h : nontrivial α] : ¬subsingleton α := let ⟨⟨x, y, hxy⟩⟩ := h in λ ⟨h'⟩, hxy $ h' x y /-- A type is either a subsingleton or nontrivial. -/ lemma subsingleton_or_nontrivial (α : Type*) : subsingleton α ∨ nontrivial α := by { rw [← not_nontrivial_iff_subsingleton, or_comm], exact classical.em _ } lemma false_of_nontrivial_of_subsingleton (α : Type*) [nontrivial α] [subsingleton α] : false := let ⟨x, y, h⟩ := exists_pair_ne α in h $ subsingleton.elim x y instance nontrivial_prod_left [nontrivial α] [nonempty β] : nontrivial (α × β) := begin inhabit β, rcases exists_pair_ne α with ⟨x, y, h⟩, use [(x, default β), (y, default β)], contrapose! h, exact congr_arg prod.fst h end instance nontrivial_prod_right [nontrivial α] [nonempty β] : nontrivial (β × α) := begin inhabit β, rcases exists_pair_ne α with ⟨x, y, h⟩, use [(default β, x), (default β, y)], contrapose! h, exact congr_arg prod.snd h end instance option.nontrivial [nonempty α] : nontrivial (option α) := by { inhabit α, use [none, some (default α)] } instance function.nontrivial [nonempty α] [nontrivial β] : nontrivial (α → β) := begin rcases exists_pair_ne β with ⟨x, y, h⟩, use [λ _, x, λ _, y], contrapose! h, inhabit α, exact congr_fun h (default α) end /-- Pushforward a `nontrivial` instance along an injective function. -/ protected lemma function.injective.nontrivial [nontrivial α] {f : α → β} (hf : function.injective f) : nontrivial β := let ⟨x, y, h⟩ := exists_pair_ne α in ⟨⟨f x, f y, hf.ne h⟩⟩ /-- Pullback a `nontrivial` instance along a surjective function. -/ protected lemma function.surjective.nontrivial [nontrivial β] {f : α → β} (hf : function.surjective f) : nontrivial α := begin rcases exists_pair_ne β with ⟨x, y, h⟩, rcases hf x with ⟨x', hx'⟩, rcases hf y with ⟨y', hy'⟩, have : x' ≠ y', by { contrapose! h, rw [← hx', ← hy', h] }, exact ⟨⟨x', y', this⟩⟩ end /-- An injective function from a nontrivial type has an argument at which it does not take a given value. -/ protected lemma function.injective.exists_ne [nontrivial α] {f : α → β} (hf : function.injective f) (y : β) : ∃ x, f x ≠ y := begin rcases exists_pair_ne α with ⟨x₁, x₂, hx⟩, by_cases h : f x₂ = y, { exact ⟨x₁, (hf.ne_iff' h).2 hx⟩ }, { exact ⟨x₂, h⟩ } end namespace tactic /-- Tries to generate a `nontrivial α` instance by performing case analysis on `subsingleton_or_nontrivial α`, attempting to discharge the subsingleton branch using `le_of_eq` and `subsingleton.elim`. -/ meta def nontriviality_by_elim (α : expr) : tactic unit := do alternative ← to_expr ``(subsingleton_or_nontrivial %%α), n ← get_unused_name "_inst", tactic.cases alternative [n, n], `[{ resetI, try { apply le_of_eq }, apply subsingleton.elim, }] <|> fail format!"Could not prove goal assuming `subsingleton {α}`", reset_instance_cache /-- Tries to generate a `nontrivial α` instance using `nontrivial_of_ne` or `nontrivial_of_lt` and local hypotheses. -/ meta def nontriviality_by_assumption (α : expr) : tactic unit := do n ← get_unused_name "_inst", to_expr ``(nontrivial %%α) >>= assert n, apply_instance <|> `[solve_by_elim [nontrivial_of_ne, nontrivial_of_lt]], reset_instance_cache end tactic namespace tactic.interactive open tactic setup_tactic_parser /-- Attempts to generate a `nontrivial α` hypothesis. The tactic first looks for an instance using `apply_instance`. If the goal is an (in)equality, the type `α` is inferred from the goal. Otherwise, the type needs to be specified in the tactic invocation, as `nontriviality α`. The `nontriviality` tactic will first look for strict inequalities amongst the hypotheses, and use these to derive the `nontrivial` instance directly. Otherwise, it will perform a case split on `subsingleton α ∨ nontrivial α`, and attempt to discharge the `subsingleton` goal. ``` example {R : Type} [ordered_ring R] {a : R} (h : 0 < a) : 0 < a := begin nontriviality, -- There is now a `nontrivial R` hypothesis available. assumption, end ``` ``` example {R : Type} [comm_ring R] {r s : R} : r * s = s * r := begin nontriviality, -- There is now a `nontrivial R` hypothesis available. apply mul_comm, end ``` ``` example {R : Type} [ordered_ring R] {a : R} (h : 0 < a) : (2 : ℕ) ∣ 4 := begin nontriviality R, -- there is now a `nontrivial R` hypothesis available. dec_trivial end ``` -/ meta def nontriviality (t : parse parser.pexpr?) : tactic unit := do α ← match t with | some α := to_expr α | none := (do t ← mk_mvar, e ← to_expr ``(@eq %%t _ _), target >>= unify e, return t) <|> (do t ← mk_mvar, e ← to_expr ``(@has_le.le %%t _ _ _), target >>= unify e, return t) <|> (do t ← mk_mvar, e ← to_expr ``(@ne %%t _ _), target >>= unify e, return t) <|> (do t ← mk_mvar, e ← to_expr ``(@has_lt.lt %%t _ _ _), target >>= unify e, return t) <|> fail "The goal is not an (in)equality, so you'll need to specify the desired `nontrivial α` instance by invoking `nontriviality α`." end, nontriviality_by_assumption α <|> nontriviality_by_elim α add_tactic_doc { name := "nontriviality", category := doc_category.tactic, decl_names := [`tactic.interactive.nontriviality], tags := ["logic", "typeclass"] } end tactic.interactive
f6122116b9e18749a7c206a7affa4e0e3710b4e8
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/src/Lean/Data/Format.lean
7d3ae256b4e676fe55805b9affd5b7d66cd26055
[ "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
10,642
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ import Lean.Data.Options universes u v namespace Lean namespace Format inductive FlattenBehavior | allOrNone | fill namespace FlattenBehavior instance : BEq FlattenBehavior := ⟨fun b₁ b₂ => match b₁, b₂ with | allOrNone, allOrNone => true | fill, fill => true | _, _ => false⟩ end FlattenBehavior end Format open Format inductive Format | nil : Format | line : Format | text : String → Format | nest (indent : Int) : Format → Format | append : Format → Format → Format | group : Format → (behavior : FlattenBehavior := FlattenBehavior.allOrNone) → Format namespace Format def fill (f : Format) : Format := group f (behavior := FlattenBehavior.fill) @[export lean_format_append] protected def appendEx (a b : Format) : Format := append a b @[export lean_format_group] protected def groupEx : Format → Format := group instance : Append Format := ⟨Format.append⟩ instance : Coe String Format := ⟨text⟩ instance : Inhabited Format := ⟨nil⟩ def join (xs : List Format) : Format := xs.foldl Append.append "" def isNil : Format → Bool | nil => true | _ => false private structure SpaceResult := (foundLine : Bool := false) (foundFlattenedHardLine : Bool := false) (space : Nat := 0) instance : Inhabited SpaceResult := ⟨{}⟩ @[inline] private def merge (w : Nat) (r₁ : SpaceResult) (r₂ : Nat → SpaceResult) : SpaceResult := if r₁.space > w || r₁.foundLine then r₁ else let r₂ := r₂ (w - r₁.space); { r₂ with space := r₁.space + r₂.space } private def spaceUptoLine : Format → Bool → Nat → SpaceResult | nil, flatten, w => {} | line, flatten, w => if flatten then { space := 1 } else { foundLine := true } | text s, flatten, w => let p := s.posOf '\n'; let off := s.offsetOfPos p; { foundLine := p != s.bsize, foundFlattenedHardLine := flatten && p != s.bsize, space := off } | append f₁ f₂, flatten, w => merge w (spaceUptoLine f₁ flatten w) (spaceUptoLine f₂ flatten) | nest _ f, flatten, w => spaceUptoLine f flatten w | group f _, _, w => spaceUptoLine f true w private structure WorkItem := (f : Format) (indent : Int) private structure WorkGroup := (flatten : Bool) (flb : FlattenBehavior) (items : List WorkItem) private partial def spaceUptoLine' : List WorkGroup → Nat → SpaceResult | [], w => {} | { items := [], .. }::gs, w => spaceUptoLine' gs w | g@{ items := i::is, .. }::gs, w => merge w (spaceUptoLine i.f g.flatten w) (spaceUptoLine' ({ g with items := is }::gs)) private structure State := (out : String := "") (column : Nat := 0) private def pushGroup (flb : FlattenBehavior) (items : List WorkItem) (gs : List WorkGroup) (w : Nat) : StateM State (List WorkGroup) := do let k := (← get).column -- Flatten group if it + the remainder (gs) fits in the remaining space. For `fill`, measure only up to the next (ungrouped) line break. let g := { flatten := flb == FlattenBehavior.allOrNone, flb := flb, items := items : WorkGroup } let r := spaceUptoLine' [g] (w-k) let r' := merge (w-k) r (spaceUptoLine' gs); -- Prevent flattening if any item contains a hard line break, except within `fill` if it is ungrouped (=> unflattened) return { g with flatten := !r.foundFlattenedHardLine && r'.space <= w-k }::gs private def pushOutput (s : String) : StateM State Unit := modify fun st => { st with out := st.out ++ s, column := st.column + s.length } private def pushNewline (indent : Nat) : StateM State Unit := modify fun st => { st with out := st.out ++ "\n".pushn ' ' indent, column := indent } private partial def be (w : Nat) : List WorkGroup → StateM State Unit | [] => pure () | { items := [], .. }::gs => be w gs | g@{ items := i::is, .. }::gs => do let gs' (is' : List WorkItem) := { g with items := is' }::gs; match i.f with | nil => be w (gs' is) | append f₁ f₂ => be w (gs' ({ i with f := f₁ }::{ i with f := f₂ }::is)) | nest n f => be w (gs' ({ i with f := f, indent := i.indent + n }::is)) | text s => let p := s.posOf '\n' if p == s.bsize then pushOutput s be w (gs' is) else pushOutput (s.extract 0 p) pushNewline i.indent.toNat let is := { i with f := s.extract (s.next p) s.bsize }::is -- after a hard line break, re-evaluate whether to flatten the remaining group pushGroup g.flb is gs w >>= be w | line => match g.flb with | FlattenBehavior.allOrNone => if g.flatten then -- flatten line = text " " pushOutput " " be w (gs' is) else pushNewline i.indent.toNat be w (gs' is) | FlattenBehavior.fill => let breakHere := do pushNewline i.indent.toNat -- make new `fill` group and recurse pushGroup FlattenBehavior.fill is gs w >>= be w -- if preceding fill item fit in a single line, try to fit next one too if g.flatten then let gs'@(g'::_) ← pushGroup FlattenBehavior.fill is gs (w - " ".length) | unreachable! if g'.flatten then pushOutput " "; be w gs' -- TODO: use `return` else breakHere else breakHere | group f flb => if g.flatten then -- flatten (group f) = flatten f be w (gs' ({ i with f := f }::is)) else pushGroup flb [{ i with f := f }] (gs' is) w >>= be w @[inline] def bracket (l : String) (f : Format) (r : String) : Format := group (nest l.length $ l ++ f ++ r) @[inline] def paren (f : Format) : Format := bracket "(" f ")" @[inline] def sbracket (f : Format) : Format := bracket "[" f "]" def defIndent := 2 def defUnicode := true def defWidth := 120 def getWidth (o : Options) : Nat := o.get `format.width defWidth def getIndent (o : Options) : Nat := o.get `format.indent defIndent def getUnicode (o : Options) : Bool := o.get `format.unicode defUnicode builtin_initialize registerOption `format.indent { defValue := defIndent, group := "format", descr := "indentation" } registerOption `format.unicode { defValue := defUnicode, group := "format", descr := "unicode characters" } registerOption `format.width { defValue := defWidth, group := "format", descr := "line width" } @[export lean_format_pretty] def prettyAux (f : Format) (w : Nat := defWidth) : String := let (_, st) := be w [{ flb := FlattenBehavior.allOrNone, flatten := false, items := [{ f := f, indent := 0 }] }] {}; st.out def pretty (f : Format) (o : Options := {}) : String := prettyAux f (getWidth o) end Format open Lean.Format class ToFormat (α : Type u) := (format : α → Format) export Lean.ToFormat (format) def fmt {α : Type u} [ToFormat α] : α → Format := format instance {α : Type u} [ToString α] : ToFormat α := ⟨text ∘ toString⟩ -- note: must take precendence over the above instance to avoid premature formatting instance : ToFormat Format := ⟨id⟩ instance : ToFormat String := ⟨Format.text⟩ def Format.joinSep {α : Type u} [ToFormat α] : List α → Format → Format | [], sep => nil | [a], sep => format a | a::as, sep => format a ++ sep ++ joinSep as sep def Format.prefixJoin {α : Type u} [ToFormat α] (pre : Format) : List α → Format | [] => nil | a::as => pre ++ format a ++ prefixJoin pre as def Format.joinSuffix {α : Type u} [ToFormat α] : List α → Format → Format | [], suffix => nil | a::as, suffix => format a ++ suffix ++ joinSuffix as suffix def List.format {α : Type u} [ToFormat α] : List α → Format | [] => "[]" | xs => sbracket $ Format.joinSep xs ("," ++ line) instance {α : Type u} [ToFormat α] : ToFormat (List α) := ⟨List.format⟩ instance {α : Type u} [ToFormat α] : ToFormat (Array α) := ⟨fun a => "#" ++ fmt a.toList⟩ def Option.format {α : Type u} [ToFormat α] : Option α → Format | none => "none" | some a => "some " ++ fmt a instance {α : Type u} [ToFormat α] : ToFormat (Option α) := ⟨Option.format⟩ instance {α : Type u} {β : Type v} [ToFormat α] [ToFormat β] : ToFormat (Prod α β) := ⟨fun ⟨a, b⟩ => paren $ format a ++ "," ++ line ++ format b ⟩ def Format.joinArraySep {α : Type u} [ToFormat α] (as : Array α) (sep : Format) : Format := do let mut r := nil let mut i := 0 for a in as do if i > 0 then r := r ++ sep ++ format a else r := r ++ format a i := i + 1 return r instance : ToFormat Nat := ⟨fun n => toString n⟩ instance : ToFormat UInt16 := ⟨fun n => toString n⟩ instance : ToFormat UInt32 := ⟨fun n => toString n⟩ instance : ToFormat UInt64 := ⟨fun n => toString n⟩ instance : ToFormat USize := ⟨fun n => toString n⟩ instance : ToFormat Name := ⟨fun n => n.toString⟩ protected def Format.repr : Format → Format | nil => "Format.nil" | line => "Format.line" | text s => paren $ "Format.text" ++ line ++ repr s | nest n f => paren $ "Format.nest" ++ line ++ repr n ++ line ++ Format.repr f | append f₁ f₂ => paren $ "Format.append " ++ line ++ Format.repr f₁ ++ line ++ Format.repr f₂ | group f FlattenBehavior.allOrNone => paren $ "Format.group" ++ line ++ Format.repr f | group f FlattenBehavior.fill => paren $ "Format.fill" ++ line ++ Format.repr f instance : ToString Format := ⟨Format.pretty⟩ instance : Repr Format := ⟨Format.pretty ∘ Format.repr⟩ def formatDataValue : DataValue → Format | DataValue.ofString v => format (repr v) | DataValue.ofBool v => format v | DataValue.ofName v => "`" ++ format v | DataValue.ofNat v => format v | DataValue.ofInt v => format v instance : ToFormat DataValue := ⟨formatDataValue⟩ def formatEntry : Name × DataValue → Format | (n, v) => format n ++ " := " ++ format v instance : ToFormat (Name × DataValue) := ⟨formatEntry⟩ def formatKVMap (m : KVMap) : Format := sbracket (Format.joinSep m.entries ", ") instance : ToFormat KVMap := ⟨formatKVMap⟩ end Lean def String.toFormat (s : String) : Lean.Format := Lean.Format.joinSep (s.splitOn "\n") Lean.Format.line
e83852c1a5a33e75864cea2dab9e57017c532796
c5b07d17b3c9fb19e4b302465d237fd1d988c14f
/src/isos/id.lean
9871fbd10f547417ca0efc85cc75a48363acdc2e
[ "MIT" ]
permissive
skaslev/papers
acaec61602b28c33d6115e53913b2002136aa29b
f15b379f3c43bbd0a37ac7bb75f4278f7e901389
refs/heads/master
1,665,505,770,318
1,660,378,602,000
1,660,378,602,000
14,101,547
0
1
MIT
1,595,414,522,000
1,383,542,702,000
Lean
UTF-8
Lean
false
false
420
lean
import isos.fseq_ogf namespace id -- x = Σ n:ℕ, δ(1,n) xⁿ def ogf_iso {A} : A ≃ ogf (delta 1) A := fseq.id_iso⁻¹ ⋆ fseq.ogf_iso instance : has_ogf id := ⟨delta 1, @ogf_iso⟩ -- x = 0 + 1 x def linear {A} : A ≃ 0 ⊕ 1 × A := iso.mul_one_left ⋆ iso.add_zero_left -- x = x¹ def one_iso {A} : A ≃ 1 → A := ⟨λ x y, x, λ x, x (), λ x, rfl, λ x, funext (λ y, punit.rec rfl y)⟩ end id
3f17d317f891697b416705bb464d143a476f1d81
c062f1c97fdef9ac746f08754e7d766fd6789aa9
/data/list/basic.lean
e8645a9b99852fff263e44bde8a6e3b184244010
[]
no_license
emberian/library_dev
00c7a985b21bdebe912f4127a363f2874e1e7555
f3abd7db0238edc18a397540e361a1da2f51503c
refs/heads/master
1,624,153,474,804
1,490,147,180,000
1,490,147,180,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,437
lean
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn Basic properties of lists. -/ import ...logic.basic ..nat.basic open function nat namespace list universe variable uu variable {α : Type uu} /- theorems -/ @[simp] lemma cons_ne_nil (a : α) (l : list α) : a::l ≠ [] := begin intro, contradiction end lemma head_eq_of_cons_eq {α : Type} {h₁ h₂ : α} {t₁ t₂ : list α} : (h₁::t₁) = (h₂::t₂) → h₁ = h₂ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pheq) lemma tail_eq_of_cons_eq {α : Type} {h₁ h₂ : α} {t₁ t₂ : list α} : (h₁::t₁) = (h₂::t₂) → t₁ = t₂ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pteq) lemma cons_inj {α : Type} {a : α} : injective (cons a) := take l₁ l₂, assume Pe, tail_eq_of_cons_eq Pe /- append -/ -- TODO(Jeremy): append_nil in the lean library should be nil_append attribute [simp] cons_append nil_append @[simp] theorem append.assoc (s t u : list α) : s ++ t ++ u = s ++ (t ++ u) := begin induction s with a s ih, reflexivity, simp [ih] end /- length -/ attribute [simp] length_cons attribute [simp] length_append /- concat -/ @[simp] theorem concat_nil (a : α) : concat [] a = [a] := rfl @[simp] theorem concat_cons (a b : α) (l : list α) : concat (a::l) b = a::(concat l b) := rfl @[simp] theorem concat_ne_nil (a : α) (l : list α) : concat l a ≠ [] := begin induction l, repeat { intro h, contradiction } end attribute [simp] length_concat @[simp] theorem concat_append (a : α) (l₁ l₂ : list α) : concat l₁ a ++ l₂ = l₁ ++ a :: l₂ := begin induction l₁ with b l₁ ih, simp, simp [ih] end theorem append_concat (a : α) (l₁ l₂ : list α) : l₁ ++ concat l₂ a = concat (l₁ ++ l₂) a := begin induction l₂ with b l₂ ih, repeat { simp } end /- last -/ @[simp] lemma last_singleton (a : α) (h : [a] ≠ []) : last [a] h = a := rfl @[simp] lemma last_cons_cons (a₁ a₂ : α) (l : list α) (h : a₁::a₂::l ≠ []) : last (a₁::a₂::l) h = last (a₂::l) (cons_ne_nil a₂ l) := rfl theorem last_congr {l₁ l₂ : list α} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) : last l₁ h₁ = last l₂ h₂ := by subst l₁ /- head and tail -/ @[simp] theorem head_cons [h : inhabited α] (a : α) (l : list α) : head (a::l) = a := rfl @[simp] theorem tail_nil : tail (@nil α) = [] := rfl @[simp] theorem tail_cons (a : α) (l : list α) : tail (a::l) = l := rfl /- list membership -/ attribute [simp] mem_nil_iff mem_cons_self mem_cons_iff /- find -/ section find variable [decidable_eq α] @[simp] theorem find_nil (a : α) : find a [] = 0 := rfl theorem find_cons (a b : α) (l : list α) : find a (b::l) = if a = b then 0 else succ (find a l) := rfl @[simp] theorem find_cons_of_eq {a b : α} (l : list α) : a = b → find a (b::l) = 0 := assume e, if_pos e @[simp] theorem find_cons_of_ne {a b : α} (l : list α) : a ≠ b → find a (b::l) = succ (find a l) := assume n, if_neg n @[simp] theorem find_of_not_mem {l : list α} {a : α} : ¬a ∈ l → find a l = length l := list.rec_on l (suppose ¬a ∈ [], rfl) (take b l, assume ih : ¬a ∈ l → find a l = length l, suppose ¬a ∈ b::l, have ¬a = b ∧ ¬a ∈ l, begin rw [mem_cons_iff, not_or_iff] at this, exact this end, show find a (b::l) = length (b::l), begin rw [find_cons, if_neg this^.left, ih this^.right], reflexivity end) lemma find_le_length {a : α} {l : list α} : find a l ≤ length l := list.rec_on l (by simp) (take b l, assume ih : find a l ≤ length l, show find a (b::l) ≤ length (b::l), from decidable.by_cases (suppose a = b, begin simp [this, find_cons_of_eq l (eq.refl b)], apply zero_le end) (suppose a ≠ b, begin simp [this, find_cons_of_ne l this], apply succ_le_succ ih end)) lemma not_mem_of_find_eq_length : ∀ {a : α} {l : list α}, find a l = length l → a ∉ l | a [] := by simp | a (b::l) := begin note h := decidable.em (a = b), cases h with aeqb aneb, { rw [find_cons_of_eq l aeqb, length_cons], intros, contradiction }, rw [find_cons_of_ne l aneb, length_cons, mem_cons_iff, not_or_iff], intro h, split, assumption, exact not_mem_of_find_eq_length (nat.succ_inj h) end lemma find_lt_length {a} {l : list α} (al : a ∈ l) : find a l < length l := begin apply lt_of_le_of_ne, apply find_le_length, apply not.intro, intro Peq, exact absurd al (not_mem_of_find_eq_length Peq) end end find /- nth element -/ section nth attribute [simp] nth_succ theorem nth_eq_some : ∀ {l : list α} {n : nat}, n < length l → { a : α // nth l n = some a} | ([] : list α) n h := absurd h (not_lt_zero _) | (a::l) 0 h := ⟨a, rfl⟩ | (a::l) (succ n) h := have n < length l, from lt_of_succ_lt_succ h, subtype.rec_on (nth_eq_some this) (take b : α, take hb : nth l n = some b, show { b : α // nth (a::l) (succ n) = some b }, from ⟨b, by rw [nth_succ, hb]⟩) theorem find_nth [decidable_eq α] {a : α} : ∀ {l : list α}, a ∈ l → nth l (find a l) = some a | [] ain := absurd ain (not_mem_nil _) | (b::l) ainbl := decidable.by_cases (λ aeqb : a = b, by rw [find_cons_of_eq _ aeqb]; simp [nth, aeqb]) (λ aneb : a ≠ b, or.elim (eq_or_mem_of_mem_cons ainbl) (λ aeqb : a = b, absurd aeqb aneb) (λ ainl : a ∈ l, by rewrite [find_cons_of_ne _ aneb, nth_succ, find_nth ainl])) definition inth [h : inhabited α] (l : list α) (n : nat) : α := match (nth l n) with | (some a) := a | none := arbitrary α end theorem inth_zero [inhabited α] (a : α) (l : list α) : inth (a :: l) 0 = a := rfl theorem inth_succ [inhabited α] (a : α) (l : list α) (n : nat) : inth (a::l) (n+1) = inth l n := rfl end nth section ith definition ith : Π (l : list α) (i : nat), i < length l → α | nil i h := absurd h (not_lt_zero i) | (a::ains) 0 h := a | (a::ains) (succ i) h := ith ains i (lt_of_succ_lt_succ h) @[simp] lemma ith_zero (a : α) (l : list α) (h : 0 < length (a::l)) : ith (a::l) 0 h = a := rfl @[simp] lemma ith_succ (a : α) (l : list α) (i : nat) (h : succ i < length (a::l)) : ith (a::l) (succ i) h = ith l i (lt_of_succ_lt_succ h) := rfl end ith section taken @[simp] lemma taken_zero : ∀ (l : list α), taken 0 l = [] := begin intros, reflexivity end @[simp] lemma taken_nil : ∀ n, taken n [] = ([] : list α) | 0 := rfl | (n+1) := rfl lemma taken_cons : ∀ n (a : α) (l : list α), taken (succ n) (a::l) = a :: taken n l := begin intros, reflexivity end lemma taken_all : ∀ (l : list α), taken (length l) l = l | [] := rfl | (a::l) := begin change a :: (taken (length l) l) = a :: l, rw taken_all end lemma taken_all_of_ge : ∀ {n} {l : list α}, n ≥ length l → taken n l = l | 0 [] h := rfl | 0 (a::l) h := absurd h (not_le_of_gt (zero_lt_succ _)) | (n+1) [] h := rfl | (n+1) (a::l) h := begin change a :: taken n l = a :: l, rw [taken_all_of_ge (le_of_succ_le_succ h)] end -- TODO(Jeremy): restore when we have min /- lemma taken_taken : ∀ (n m) (l : list α), taken n (taken m l) = taken (min n m) l | n 0 l := sorry -- by rewrite [min_zero, taken_zero, taken_nil] | 0 m l := sorry -- by rewrite [zero_min] | (succ n) (succ m) nil := sorry -- by rewrite [*taken_nil] | (succ n) (succ m) (a::l) := sorry -- by rewrite [*taken_cons, taken_taken, min_succ_succ] -/ lemma length_taken_le : ∀ (n) (l : list α), length (taken n l) ≤ n | 0 l := begin rw [taken_zero], reflexivity end | (succ n) (a::l) := begin rw [taken_cons, length_cons], apply succ_le_succ, apply length_taken_le end | (succ n) [] := begin simp [taken, length], apply zero_le end -- TODO(Jeremy): restore when we have min /- lemma length_taken_eq : ∀ (n) (l : list α), length (taken n l) = min n (length l) | 0 l := sorry -- by rewrite [taken_zero, zero_min] | (succ n) (a::l) := sorry -- by rewrite [taken_cons, *length_cons, *add_one, min_succ_succ, length_taken_eq] | (succ n) [] := sorry -- by rewrite [taken_nil] -/ end taken -- TODO(Jeremy): restore when we have nat.sub /- section dropn -- 'dropn n l' drops the first 'n' elements of 'l' theorem length_dropn : ∀ (i : ℕ) (l : list α), length (dropn i l) = length l - i | 0 l := rfl | (succ i) [] := calc length (dropn (succ i) []) = 0 - succ i : sorry -- by rewrite (nat.zero_sub (succ i)) | (succ i) (x::l) := calc length (dropn (succ i) (x::l)) = length (dropn i l) : by reflexivity ... = length l - i : length_dropn i l ... = succ (length l) - succ i : sorry -- by rewrite (succ_sub_succ (length l) i) end dropn -/ section count variable [decidable_eq α] definition count (a : α) : list α → nat | [] := 0 | (x::xs) := if a = x then succ (count xs) else count xs @[simp] lemma count_nil (a : α) : count a [] = 0 := rfl lemma count_cons (a b : α) (l : list α) : count a (b :: l) = if a = b then succ (count a l) else count a l := rfl lemma count_cons' (a b : α) (l : list α) : count a (b :: l) = count a l + (if a = b then 1 else 0) := decidable.by_cases (suppose a = b, begin rw [count_cons, if_pos this, if_pos this] end) (suppose a ≠ b, begin rw [count_cons, if_neg this, if_neg this], reflexivity end) @[simp] lemma count_cons_self (a : α) (l : list α) : count a (a::l) = succ (count a l) := if_pos rfl @[simp] lemma count_cons_of_ne {a b : α} (h : a ≠ b) (l : list α) : count a (b::l) = count a l := if_neg h lemma count_cons_ge_count (a b : α) (l : list α) : count a (b :: l) ≥ count a l := decidable.by_cases (suppose a = b, begin subst b, rewrite count_cons_self, apply le_succ end) (suppose a ≠ b, begin rw (count_cons_of_ne this), apply le_refl end) -- TODO(Jeremy): without the reflexivity, this yields the goal "1 = 1". the first is from has_one, -- the second is succ 0. Make sure the simplifier can eventually handle this. lemma count_singleton (a : α) : count a [a] = 1 := by simp @[simp] lemma count_append (a : α) : ∀ l₁ l₂, count a (l₁ ++ l₂) = count a l₁ + count a l₂ | [] l₂ := begin rw [nil_append, count_nil, zero_add] end | (b::l₁) l₂ := decidable.by_cases (suppose a = b, by rw [-this, cons_append, count_cons_self, count_cons_self, succ_add, count_append]) (suppose a ≠ b, by rw [cons_append, count_cons_of_ne this, count_cons_of_ne this, count_append]) @[simp] lemma count_concat (a : α) (l : list α) : count a (concat l a) = succ (count a l) := by rw [concat_eq_append, count_append, count_singleton] lemma mem_of_count_pos : ∀ {a : α} {l : list α}, count a l > 0 → a ∈ l | a [] h := absurd h (lt_irrefl _) | a (b::l) h := decidable.by_cases (suppose a = b, begin subst b, apply mem_cons_self end) (suppose a ≠ b, have count a l > 0, begin rw [count_cons_of_ne this] at h, exact h end, have a ∈ l, from mem_of_count_pos this, show a ∈ b::l, from mem_cons_of_mem _ this) lemma count_pos_of_mem : ∀ {a : α} {l : list α}, a ∈ l → count a l > 0 | a [] h := absurd h (not_mem_nil _) | a (b::l) h := or.elim h (suppose a = b, begin subst b, rw count_cons_self, apply zero_lt_succ end) (suppose a ∈ l, calc count a (b::l) ≥ count a l : count_cons_ge_count _ _ _ ... > 0 : count_pos_of_mem this) lemma mem_iff_count_pos (a : α) (l : list α) : a ∈ l ↔ count a l > 0 := iff.intro count_pos_of_mem mem_of_count_pos @[simp] lemma count_eq_zero_of_not_mem {a : α} {l : list α} (h : a ∉ l) : count a l = 0 := have ∀ n, count a l = n → count a l = 0, begin intro n, cases n, { intro this, exact this }, intro this, exact absurd (mem_of_count_pos (begin rw this, exact dec_trivial end)) h end, this (count a l) rfl lemma not_mem_of_count_eq_zero {a : α} {l : list α} (h : count a l = 0) : a ∉ l := suppose a ∈ l, have count a l > 0, from count_pos_of_mem this, show false, begin rw h at this, exact nat.not_lt_zero _ this end end count end list
e37a6cec8982766b2d40bfc044c8b90ab6a8b1cf
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/hott/types/unit.hlean
dc121d685078b3fffb17ee2e609d71565cda77e3
[ "Apache-2.0" ]
permissive
YHVHvx/lean
732bf0fb7a298cd7fe0f15d82f8e248c11db49e9
038369533e0136dd395dc252084d3c1853accbf2
refs/heads/master
1,610,701,080,210
1,449,128,595,000
1,449,128,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,154
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 Theorems about the unit type -/ import algebra.group open equiv option eq namespace unit protected definition eta : Π(u : unit), ⋆ = u | eta ⋆ := idp definition unit_equiv_option_empty : unit ≃ option empty := begin fapply equiv.MK, { intro u, exact none}, { intro e, exact star}, { intro e, cases e, reflexivity, contradiction}, { intro u, cases u, reflexivity}, end definition unit_imp_equiv (A : Type) : (unit → A) ≃ A := begin fapply equiv.MK, { intro f, exact f star}, { intro a u, exact a}, { intro a, reflexivity}, { intro f, apply eq_of_homotopy, intro u, cases u, reflexivity}, end end unit open unit is_trunc namespace algebra definition trivial_group [constructor] : group unit := group.mk (λx y, star) _ (λx y z, idp) star (unit.rec idp) (unit.rec idp) (λx, star) (λx, idp) definition Trivial_group [constructor] : Group := Group.mk _ trivial_group notation `G0` := Trivial_group end algebra