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
5e0acaeade07b52cc82fe98e7b158f82bb549460
4727251e0cd73359b15b664c3170e5d754078599
/src/order/semiconj_Sup.lean
12f70d69ab22b35384ae5d1f03dccd1f548345fe
[ "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,000
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import algebra.hom.equiv import logic.function.conjugate import order.conditionally_complete_lattice import order.ord_continuous /-! # Semiconjugate by `Sup` In this file we prove two facts about semiconjugate (families of) functions. First, if an order isomorphism `fa : α → α` is semiconjugate to an order embedding `fb : β → β` by `g : α → β`, then `fb` is semiconjugate to `fa` by `y ↦ Sup {x | g x ≤ y}`, see `semiconj.symm_adjoint`. Second, consider two actions `f₁ f₂ : G → α → α` of a group on a complete lattice by order isomorphisms. Then the map `x ↦ ⨆ g : G, (f₁ g)⁻¹ (f₂ g x)` semiconjugates each `f₁ g'` to `f₂ g'`, see `function.Sup_div_semiconj`. In the case of a conditionally complete lattice, a similar statement holds true under an additional assumption that each set `{(f₁ g)⁻¹ (f₂ g x) | g : G}` is bounded above, see `function.cSup_div_semiconj`. The lemmas come from [Étienne Ghys, Groupes d'homeomorphismes du cercle et cohomologie bornee][ghys87:groupes], Proposition 2.1 and 5.4 respectively. In the paper they are formulated for homeomorphisms of the circle, so in order to apply results from this file one has to lift these homeomorphisms to the real line first. -/ variables {α β γ : Type*} open set /-- We say that `g : β → α` is an order right adjoint function for `f : α → β` if it sends each `y` to a least upper bound for `{x | f x ≤ y}`. If `α` is a partial order, and `f : α → β` has a right adjoint, then this right adjoint is unique. -/ def is_order_right_adjoint [preorder α] [preorder β] (f : α → β) (g : β → α) := ∀ y, is_lub {x | f x ≤ y} (g y) lemma is_order_right_adjoint_Sup [complete_lattice α] [preorder β] (f : α → β) : is_order_right_adjoint f (λ y, Sup {x | f x ≤ y}) := λ y, is_lub_Sup _ lemma is_order_right_adjoint_cSup [conditionally_complete_lattice α] [preorder β] (f : α → β) (hne : ∀ y, ∃ x, f x ≤ y) (hbdd : ∀ y, bdd_above {x | f x ≤ y}) : is_order_right_adjoint f (λ y, Sup {x | f x ≤ y}) := λ y, is_lub_cSup (hne y) (hbdd y) namespace is_order_right_adjoint protected lemma unique [partial_order α] [preorder β] {f : α → β} {g₁ g₂ : β → α} (h₁ : is_order_right_adjoint f g₁) (h₂ : is_order_right_adjoint f g₂) : g₁ = g₂ := funext $ λ y, (h₁ y).unique (h₂ y) lemma right_mono [preorder α] [preorder β] {f : α → β} {g : β → α} (h : is_order_right_adjoint f g) : monotone g := λ y₁ y₂ hy, (h y₁).mono (h y₂) $ λ x hx, le_trans hx hy lemma order_iso_comp [preorder α] [preorder β] [preorder γ] {f : α → β} {g : β → α} (h : is_order_right_adjoint f g) (e : β ≃o γ) : is_order_right_adjoint (e ∘ f) (g ∘ e.symm) := λ y, by simpa [e.le_symm_apply] using h (e.symm y) lemma comp_order_iso [preorder α] [preorder β] [preorder γ] {f : α → β} {g : β → α} (h : is_order_right_adjoint f g) (e : γ ≃o α) : is_order_right_adjoint (f ∘ e) (e.symm ∘ g) := begin intro y, change is_lub (e ⁻¹' {x | f x ≤ y}) (e.symm (g y)), rw [e.is_lub_preimage, e.apply_symm_apply], exact h y end end is_order_right_adjoint namespace function /-- If an order automorphism `fa` is semiconjugate to an order embedding `fb` by a function `g` and `g'` is an order right adjoint of `g` (i.e. `g' y = Sup {x | f x ≤ y}`), then `fb` is semiconjugate to `fa` by `g'`. This is a version of Proposition 2.1 from [Étienne Ghys, Groupes d'homeomorphismes du cercle et cohomologie bornee][ghys87:groupes]. -/ lemma semiconj.symm_adjoint [partial_order α] [preorder β] {fa : α ≃o α} {fb : β ↪o β} {g : α → β} (h : function.semiconj g fa fb) {g' : β → α} (hg' : is_order_right_adjoint g g') : function.semiconj g' fb fa := begin refine λ y, (hg' _).unique _, rw [← fa.surjective.image_preimage {x | g x ≤ fb y}, preimage_set_of_eq], simp only [h.eq, fb.le_iff_le, fa.left_ord_continuous (hg' _)] end variable {G : Type*} lemma semiconj_of_is_lub [partial_order α] [group G] (f₁ f₂ : G →* (α ≃o α)) {h : α → α} (H : ∀ x, is_lub (range (λ g', (f₁ g')⁻¹ (f₂ g' x))) (h x)) (g : G) : function.semiconj h (f₂ g) (f₁ g) := begin refine λ y, (H _).unique _, have := (f₁ g).left_ord_continuous (H y), rw [← range_comp, ← (equiv.mul_right g).surjective.range_comp _] at this, simpa [(∘)] using this end /-- Consider two actions `f₁ f₂ : G → α → α` of a group on a complete lattice by order isomorphisms. Then the map `x ↦ ⨆ g : G, (f₁ g)⁻¹ (f₂ g x)` semiconjugates each `f₁ g'` to `f₂ g'`. This is a version of Proposition 5.4 from [Étienne Ghys, Groupes d'homeomorphismes du cercle et cohomologie bornee][ghys87:groupes]. -/ lemma Sup_div_semiconj [complete_lattice α] [group G] (f₁ f₂ : G →* (α ≃o α)) (g : G) : function.semiconj (λ x, ⨆ g' : G, (f₁ g')⁻¹ (f₂ g' x)) (f₂ g) (f₁ g) := semiconj_of_is_lub f₁ f₂ (λ x, is_lub_supr) _ /-- Consider two actions `f₁ f₂ : G → α → α` of a group on a conditionally complete lattice by order isomorphisms. Suppose that each set $s(x)=\{f_1(g)^{-1} (f_2(g)(x)) | g \in G\}$ is bounded above. Then the map `x ↦ Sup s(x)` semiconjugates each `f₁ g'` to `f₂ g'`. This is a version of Proposition 5.4 from [Étienne Ghys, Groupes d'homeomorphismes du cercle et cohomologie bornee][ghys87:groupes]. -/ lemma cSup_div_semiconj [conditionally_complete_lattice α] [group G] (f₁ f₂ : G →* (α ≃o α)) (hbdd : ∀ x, bdd_above (range $ λ g, (f₁ g)⁻¹ (f₂ g x))) (g : G) : function.semiconj (λ x, ⨆ g' : G, (f₁ g')⁻¹ (f₂ g' x)) (f₂ g) (f₁ g) := semiconj_of_is_lub f₁ f₂ (λ x, is_lub_cSup (range_nonempty _) (hbdd x)) _ end function
105cbb7da5bf5e46bc600f2039e5bf14be8119fb
367134ba5a65885e863bdc4507601606690974c1
/src/category_theory/limits/shapes/finite_limits.lean
d8fcfa5f72c946ad7a855b90dc2ee25639e0510d
[ "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
6,818
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 data.fintype.basic import category_theory.fin_category import category_theory.limits.shapes.products import category_theory.limits.shapes.equalizers import category_theory.limits.shapes.pullbacks /-! # Categories with finite limits. A typeclass for categories with all finite (co)limits. -/ universes v u open category_theory namespace category_theory.limits variables (C : Type u) [category.{v} C] /-- A category has all finite limits if every functor `J ⥤ C` with a `fin_category J` instance has a limit. This is often called 'finitely complete'. -/ -- We can't just made this an `abbreviation` -- because of https://github.com/leanprover-community/lean/issues/429 def has_finite_limits : Prop := Π (J : Type v) [𝒥 : small_category J] [@fin_category J 𝒥], @has_limits_of_shape J 𝒥 C _ attribute [class] has_finite_limits @[priority 100] instance has_limits_of_shape_of_has_finite_limits (J : Type v) [small_category J] [fin_category J] [has_finite_limits C] : has_limits_of_shape J C := ‹has_finite_limits C› J /-- If `C` has all limits, it has finite limits. -/ lemma has_finite_limits_of_has_limits [has_limits C] : has_finite_limits C := λ J 𝒥₁ 𝒥₂, by apply_instance /-- A category has all finite colimits if every functor `J ⥤ C` with a `fin_category J` instance has a colimit. This is often called 'finitely cocomplete'. -/ def has_finite_colimits : Prop := Π (J : Type v) [𝒥 : small_category J] [@fin_category J 𝒥], @has_colimits_of_shape J 𝒥 C _ attribute [class] has_finite_colimits @[priority 100] instance has_colimits_of_shape_of_has_finite_colimits (J : Type v) [small_category J] [fin_category J] [has_finite_colimits C] : has_colimits_of_shape J C := ‹has_finite_colimits C› J /-- If `C` has all colimits, it has finite colimits. -/ lemma has_finite_colimits_of_has_colimits [has_colimits C] : has_finite_colimits C := λ J 𝒥₁ 𝒥₂, by apply_instance section open walking_parallel_pair walking_parallel_pair_hom instance fintype_walking_parallel_pair : fintype walking_parallel_pair := { elems := [walking_parallel_pair.zero, walking_parallel_pair.one].to_finset, complete := λ x, by { cases x; simp } } local attribute [tidy] tactic.case_bash instance (j j' : walking_parallel_pair) : fintype (walking_parallel_pair_hom j j') := { elems := walking_parallel_pair.rec_on j (walking_parallel_pair.rec_on j' [walking_parallel_pair_hom.id zero].to_finset [left, right].to_finset) (walking_parallel_pair.rec_on j' ∅ [walking_parallel_pair_hom.id one].to_finset), complete := by tidy } end instance : fin_category walking_parallel_pair := { } /-- Equalizers are finite limits, so if `C` has all finite limits, it also has all equalizers -/ example [has_finite_limits C] : has_equalizers C := by apply_instance /-- Coequalizers are finite colimits, of if `C` has all finite colimits, it also has all coequalizers -/ example [has_finite_colimits C] : has_coequalizers C := by apply_instance variables {J : Type v} local attribute [tidy] tactic.case_bash namespace wide_pullback_shape instance fintype_obj [fintype J] : fintype (wide_pullback_shape J) := by { rw wide_pullback_shape, apply_instance } instance fintype_hom [decidable_eq J] (j j' : wide_pullback_shape J) : fintype (j ⟶ j') := { elems := begin cases j', { cases j, { exact {hom.id none} }, { exact {hom.term j} } }, { by_cases some j' = j, { rw h, exact {hom.id j} }, { exact ∅ } } end, complete := by tidy } end wide_pullback_shape namespace wide_pushout_shape instance fintype_obj [fintype J] : fintype (wide_pushout_shape J) := by { rw wide_pushout_shape, apply_instance } instance fintype_hom [decidable_eq J] (j j' : wide_pushout_shape J) : fintype (j ⟶ j') := { elems := begin cases j, { cases j', { exact {hom.id none} }, { exact {hom.init j'} } }, { by_cases some j = j', { rw h, exact {hom.id j'} }, { exact ∅ } } end, complete := by tidy } end wide_pushout_shape instance fin_category_wide_pullback [decidable_eq J] [fintype J] : fin_category (wide_pullback_shape J) := { fintype_hom := wide_pullback_shape.fintype_hom } instance fin_category_wide_pushout [decidable_eq J] [fintype J] : fin_category (wide_pushout_shape J) := { fintype_hom := wide_pushout_shape.fintype_hom } /-- `has_finite_wide_pullbacks` represents a choice of wide pullback for every finite collection of morphisms -/ -- We can't just made this an `abbreviation` -- because of https://github.com/leanprover-community/lean/issues/429 def has_finite_wide_pullbacks : Prop := Π (J : Type v) [decidable_eq J] [fintype J], has_limits_of_shape (wide_pullback_shape J) C attribute [class] has_finite_wide_pullbacks instance has_limits_of_shape_wide_pullback_shape (J : Type v) [fintype J] [has_finite_wide_pullbacks C] : has_limits_of_shape (wide_pullback_shape J) C := by { classical, exact ‹has_finite_wide_pullbacks C› J } /-- `has_finite_wide_pushouts` represents a choice of wide pushout for every finite collection of morphisms -/ def has_finite_wide_pushouts : Prop := Π (J : Type v) [decidable_eq J] [fintype J], has_colimits_of_shape (wide_pushout_shape J) C attribute [class] has_finite_wide_pushouts instance has_colimits_of_shape_wide_pushout_shape (J : Type v) [fintype J] [has_finite_wide_pushouts C] : has_colimits_of_shape (wide_pushout_shape J) C := by { classical, exact ‹has_finite_wide_pushouts C› J } /-- Finite wide pullbacks are finite limits, so if `C` has all finite limits, it also has finite wide pullbacks -/ lemma has_finite_wide_pullbacks_of_has_finite_limits [has_finite_limits C] : has_finite_wide_pullbacks C := λ J _ _, by exactI limits.has_limits_of_shape_of_has_finite_limits _ _ /-- Finite wide pushouts are finite colimits, so if `C` has all finite colimits, it also has finite wide pushouts -/ lemma has_finite_wide_pushouts_of_has_finite_limits [has_finite_colimits C] : has_finite_wide_pushouts C := λ J _ _, by exactI limits.has_colimits_of_shape_of_has_finite_colimits _ _ instance fintype_walking_pair : fintype walking_pair := { elems := {walking_pair.left, walking_pair.right}, complete := λ x, by { cases x; simp } } /-- Pullbacks are finite limits, so if `C` has all finite limits, it also has all pullbacks -/ example [has_finite_wide_pullbacks C] : has_pullbacks C := by apply_instance /-- Pushouts are finite colimits, so if `C` has all finite colimits, it also has all pushouts -/ example [has_finite_wide_pushouts C] : has_pushouts C := by apply_instance end category_theory.limits
fe03a94713fa38811201080fc29ab5d367781f18
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/linear_algebra/smodeq.lean
1eb72d7ae3d5739d47cabc7954f176c42c7a65ed
[ "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
2,930
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 data.polynomial.eval import ring_theory.ideal.quotient /-! # modular equivalence for submodule -/ open submodule variables {R : Type*} [ring R] variables {M : Type*} [add_comm_group M] [module R M] (U U₁ U₂ : submodule R M) variables {x x₁ x₂ y y₁ y₂ z z₁ z₂ : M} variables {N : Type*} [add_comm_group N] [module R N] (V V₁ V₂ : submodule R N) /-- A predicate saying two elements of a module are equivalent modulo a submodule. -/ def smodeq (x y : M) : Prop := (submodule.quotient.mk x : U.quotient) = submodule.quotient.mk y notation x ` ≡ `:50 y ` [SMOD `:50 N `]`:0 := smodeq N x y variables {U U₁ U₂} protected lemma smodeq.def : x ≡ y [SMOD U] ↔ (submodule.quotient.mk x : U.quotient) = submodule.quotient.mk y := iff.rfl namespace smodeq lemma sub_mem : x ≡ y [SMOD U] ↔ x - y ∈ U := by rw [smodeq.def, submodule.quotient.eq] @[simp] theorem top : x ≡ y [SMOD (⊤ : submodule R M)] := (submodule.quotient.eq ⊤).2 mem_top @[simp] theorem bot : x ≡ y [SMOD (⊥ : submodule R M)] ↔ x = y := by rw [smodeq.def, submodule.quotient.eq, mem_bot, sub_eq_zero] @[mono] theorem mono (HU : U₁ ≤ U₂) (hxy : x ≡ y [SMOD U₁]) : x ≡ y [SMOD U₂] := (submodule.quotient.eq U₂).2 $ HU $ (submodule.quotient.eq U₁).1 hxy @[refl] theorem refl : x ≡ x [SMOD U] := eq.refl _ @[symm] theorem symm (hxy : x ≡ y [SMOD U]) : y ≡ x [SMOD U] := hxy.symm @[trans] theorem trans (hxy : x ≡ y [SMOD U]) (hyz : y ≡ z [SMOD U]) : x ≡ z [SMOD U] := hxy.trans hyz theorem add (hxy₁ : x₁ ≡ y₁ [SMOD U]) (hxy₂ : x₂ ≡ y₂ [SMOD U]) : x₁ + x₂ ≡ y₁ + y₂ [SMOD U] := by { rw smodeq.def at hxy₁ hxy₂ ⊢, simp_rw [quotient.mk_add, hxy₁, hxy₂] } theorem smul (hxy : x ≡ y [SMOD U]) (c : R) : c • x ≡ c • y [SMOD U] := by { rw smodeq.def at hxy ⊢, simp_rw [quotient.mk_smul, hxy] } theorem zero : x ≡ 0 [SMOD U] ↔ x ∈ U := by rw [smodeq.def, submodule.quotient.eq, sub_zero] theorem map (hxy : x ≡ y [SMOD U]) (f : M →ₗ[R] N) : f x ≡ f y [SMOD U.map f] := (submodule.quotient.eq _).2 $ f.map_sub x y ▸ mem_map_of_mem $ (submodule.quotient.eq _).1 hxy theorem comap {f : M →ₗ[R] N} (hxy : f x ≡ f y [SMOD V]) : x ≡ y [SMOD V.comap f] := (submodule.quotient.eq _).2 $ show f (x - y) ∈ V, from (f.map_sub x y).symm ▸ (submodule.quotient.eq _).1 hxy lemma eval {R : Type*} [comm_ring R] {I : ideal R} {x y : R} (h : x ≡ y [SMOD I]) (f : polynomial R) : f.eval x ≡ f.eval y [SMOD I] := begin rw [smodeq.def] at h ⊢, show ideal.quotient.mk I (f.eval x) = ideal.quotient.mk I (f.eval y), change ideal.quotient.mk I x = ideal.quotient.mk I y at h, rw [← polynomial.eval₂_at_apply, ← polynomial.eval₂_at_apply, h], end end smodeq
2c11dfa5f0468f011873df75c95f6c2d29cc2102
ec040be767d27b10d2f864ddcfdf756aeb7a9a0a
/src/assignments/assignment_6/assignment_6.lean
df033e8ad17d596bb3798cdd028d8f404e6c929e
[]
no_license
RoboticPanda77/complogic-s21
b26a9680dfb98ac650e40539296c0cafc86f5cb4
93c5bcc0139c0926cc261075f50a8b1ead6aa40c
refs/heads/master
1,682,196,614,558
1,620,625,035,000
1,620,625,035,000
337,230,148
0
0
null
1,620,625,036,000
1,612,824,240,000
Lean
UTF-8
Lean
false
false
8,903
lean
import ...inClassNotes.typeclasses.algebra import ...inClassNotes.typeclasses.functor set_option old_structure_cmd true /- Copy this file to where you want to work on it and then adjust the imports accordingly. Work through the file following directions as indicated. Turn in your completed file on Collab. -/ universe u open alg /- 1. We've imported our definitions from our class on basic algebraic structures, such as monoid and group. Go learn what an algebraic *ring* is, define a typeclass that expresses its definition, and define an instance that expresses the claim that the integers (ℤ or *int* in Lean) is a ring. You may "stub out" the required proofs with *sorry*. -/ @[class] structure ring (α : Type u) extends add_comm_group α, mul_monoid α:= (mul_distrib_left : ∀ (a b c : α), mul_groupoid.mul a (add_groupoid.add b c) = add_groupoid.add (mul_groupoid.mul a b) (mul_groupoid.mul a c)) (mul_distrib_right : ∀ (a b c : α), mul_groupoid.mul (add_groupoid.add b c) a = add_groupoid.add (mul_groupoid.mul b a) (mul_groupoid.mul c a)) instance int_is_ring : ring ℤ := _ /- 2. Go learn what an algebraic *field* is, then define a typeclass to formalize its definition, and finally define two instances that express the claims that the rational numbers (ℚ) and the real numbers (ℝ) are both fields. Again you may (and should) stub out the proof fields in your instances using sorry. -/ /- Assert: lecture from relevant class posted in time to do the homework -/ /- 3. Graduate students required. Undergrads extra credit. Go figure out what an algebraic module is and write a typeclass to specify it formally. Create an instance to implement the typeclass for the integers (ℤ not ℕ). Stub out the proofs. In lieu of a formal proof, present a *brief informal* (in English) argument to convince your instructor that the integers really do form a module under the usual arithmetic operators. -/ /- 4. The set of (our representations of) natural numbers is defined inductively. Here's how they are defined, copied straight from Lean's library. inductive nat | zero : nat | succ (n : nat) : nat Complete the following function definitions for natural number addition, multiplication, and exponentiation. Write your own functions here without using Lean's implementations (i.e., don't use nat.mul, *, etc). You may not use + except as a shorthand for using the nat.succ constructor to add one. If you need to do addition of something other than one, use your own add function. Similarly, if you need to multiply, using your mul function. -/ def add : nat → nat → nat | 0 m := m | (n' + 1) m := add n' (nat.succ(m)) def mul : nat → nat → nat | 0 m := 0 | (n' + 1) m := add m (mul n' m) -- first arg raised to second def exp : nat → nat → nat | n 0 := 1 | n (m'+1) := mul n (exp n m') #eval exp 2 10 -- expect 1024 /- 5. Many computations can be expressed as compositions of map and fold (also sometimes called reduce). For example, you can compute the length of a list by mapping each element to the number, 1, and by the folding the list under natural number addition. A slightly more interesting function counts the number of elements in a list that satisfy some predicate (specified by a boolean-returning function). A. Write a function, mul_map_reduce, that takes (1) a function, f : α → β, where β must be a monoid; and (2) a list, l, of objects of type α; and that then uses f to map l to a list of objects of a type, β, and that then does a fold on the list to reduce it to a final value of type β. Be sure to use a typeclass instance in specifying the type of your function to ensure that the only types that can serve as values of β have monoid structures. Use both our mul_monoid_foldr and fmap functions to implement your solution. -/ open alg def mul_map_reduce {α β : Type} [mul_monoid β] (f : α → β) (l : list α) : β := mul_monoid_foldr (fmap f l) /- B. Complete the given application of mul_map_reduce with a lambda expression to compute the product of the non-zero values in the list [1,0,2,0,3,0,4]. -/ #eval mul_map_reduce (λ n : nat, if n = 0 then 1 else n) [1,0,2,0,3,0,4] -- expect 24 /- 6. Here you practice with type families. A. Define a family of types, each of which is index by two natural numbers, such that each type is inhabited if and only if the two natural numbers are equal. You may call your type family nat_eql. Use implicit args when it makes the use of your type family easier. -/ inductive nat_eql: nat → nat → Type | zeros_equal : nat_eql 0 0 | n_succ_m_succ_equal : Π {n m : nat}, (nat_eql n m) → nat_eql (n+1) (m+1) /- B. Now either complete the following programs or argue informally (and briefly) why that won't be possible. -/ open nat_eql def eq_0_0 : nat_eql 0 0 := zeros_equal def eq_0_1 : nat_eql 0 1 := _ -- impossible: 0 does not equal 1 def eq_1_1 : nat_eql 1 1 := n_succ_m_succ_equal zeros_equal def eq_2_2 : nat_eql 2 2 := n_succ_m_succ_equal (n_succ_m_succ_equal zeros_equal) /- C. The apply tactic in Lean's tactic language let's you build the term you need by applying an already defined function. Moreover, you can leave holes (underscores) for the arguments and those holes then become subgoals. In this way, using tactics allows you to build a solution using interactive, top-down, type-guided, aka structured, programming! Show that eq_2_2 is inhabited using Lean's tactic language. We get you started. Hint: remember the "exact" tactic. Hint: Think *top down*. Write a single, simple expression that provides a complete solution *except* for the holes that remain to be filled. Then recursively "fill the holes". Continue until you're done. Voila! -/ def eq_10_10 : nat_eql 10 10 := begin apply n_succ_m_succ_equal, apply n_succ_m_succ_equal, apply n_succ_m_succ_equal, apply n_succ_m_succ_equal, apply n_succ_m_succ_equal, apply n_succ_m_succ_equal, apply n_succ_m_succ_equal, apply n_succ_m_succ_equal, apply n_succ_m_succ_equal, apply n_succ_m_succ_equal, apply zeros_equal end /- In Lean, "repeat" is a tactic that takes another tactic as an argument (enclosed in curly braces), applies it repeatedly until it fails, and leaves you in the resulting tactic state. Use the repeat tactic to show that "nat_eql 500 500" is inhabited. If you get a deterministic timeout, pick smaller numbers, such as 100 and 100. It's ok with us. -/ def eq_500_500 : nat_eql 500 500 := begin repeat {apply n_succ_m_succ_equal}, apply zeros_equal end #reduce eq_500_500 /- 7. Typeclasses and instances are used in Lean to implement *coercions*, also known as type casts. As in Java, C++, and many other languages, coercions are automatically applied conversions of values of one type, α, to values of another type, β, so that that values of type α can be used where values of type β are needed. For example, in many languages you may use an integer wherever a Boolean value is expected. The conversion is typically from zero to false and from any non-zero value to true. Here's the has_coe (has coercion) typeclass as defined in Lean's libraries. As you can see, a coercion is really just a function, coe, from one type to another, associated with the pair of those two types. class has_coe (a : Sort u) (b : Sort v) := (coe : a → b) A. We provide a simple function, needs_bool, that takes a bool value and just returns it. Your job is to allow this function to be applied to any nat value by defining a new coercion from nat to bool. First define a function, say nat_to_bool, that converts any nat, n, to a bool, by the rule that zero goes to false and any other nat goes to tt. Then define an instance of the has_coe typeclass to enable coercions from nat to bool. You should call it nat_to_bool_coe. When you're done the test cases below should work. -/ def nat_to_bool : nat → bool := λ n, match n with | 0 := ff | _ := tt end instance nat_to_bool_coe : has_coe nat bool := ⟨ nat_to_bool ⟩ def needs_bool : bool → bool := λ b, b -- Test cases #eval needs_bool (1:nat) -- expect tt #eval needs_bool (0:nat) -- expect ff /- Not only are coercions, when available, applied automatically, but, with certain limitations, Lean can also chain them automatically. Define a second coercion called string_to_nat_coe, from string to nat, that will coerce any string to its length as a nat (using the string.length function). When you're done, you should be able to apply the needs_bool function to any string, where the empty string returns ff and non-empty, tt. -/ instance string_to_nat_coe : has_coe string nat := ⟨ string.length ⟩ -- Test cases #eval needs_bool "Hello" -- expect tt #eval needs_bool "" -- expect ff /- Do you see how the coercions are being chained, aka, composed, automatically? -/ -- Good job!
920f4ea4a2fd73346937a5332b155c2281a1f6f0
5fbbd711f9bfc21ee168f46a4be146603ece8835
/lean/natural_number_game/addition/5.lean
39eecc03603c451656f175808efd3317ddc508c7
[ "LicenseRef-scancode-warranty-disclaimer" ]
no_license
goedel-gang/maths
22596f71e3fde9c088e59931f128a3b5efb73a2c
a20a6f6a8ce800427afd595c598a5ad43da1408d
refs/heads/master
1,623,055,941,960
1,621,599,441,000
1,621,599,441,000
169,335,840
0
0
null
null
null
null
UTF-8
Lean
false
false
117
lean
theorem succ_eq_add_one (n : mynat) : succ n = n + 1 := begin rw one_eq_succ_zero, rwa [add_succ, add_zero], end
7f68a26353dfea5f0d7af68e1253bf14b8dd4254
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/algebra/module/ordered.lean
86d71a79a8e073ec761e0e142222cabe11e515a1
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
3,658
lean
/- Copyright (c) 2020 Frédéric Dupuis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Frédéric Dupuis -/ import algebra.module.basic import algebra.ordered_group /-! # Ordered semimodules In this file we define * `ordered_semimodule R M` : an ordered additive commutative monoid `M` is an `ordered_semimodule` over an `ordered_semiring` `R` if the scalar product respects the order relation on the monoid and on the ring. There is a correspondence between this structure and convex cones, which is proven in `analysis/convex/cone.lean`. ## Implementation notes * We choose to define `ordered_semimodule` so that it extends `semimodule` only, as is done for semimodules itself. * To get ordered modules and ordered vector spaces, it suffices to the replace the `order_add_comm_monoid` and the `ordered_semiring` as desired. ## References * https://en.wikipedia.org/wiki/Ordered_vector_space ## Tags ordered semimodule, ordered module, ordered vector space -/ set_option default_priority 100 -- see Note [default priority] /-- An ordered semimodule is an ordered additive commutative monoid with a partial order in which the scalar multiplication is compatible with the order. -/ @[protect_proj, ancestor semimodule] class ordered_semimodule (R β : Type*) [ordered_semiring R] [ordered_add_comm_monoid β] extends semimodule R β := (smul_lt_smul_of_pos : ∀ {a b : β}, ∀ {c : R}, a < b → 0 < c → c • a < c • b) (lt_of_smul_lt_smul_of_nonneg : ∀ {a b : β}, ∀ {c : R}, c • a < c • b → 0 ≤ c → a < b) section ordered_semimodule variables {R β : Type*} [ordered_semiring R] [ordered_add_comm_monoid β] [ordered_semimodule R β] {a b : β} {c : R} lemma smul_lt_smul_of_pos : a < b → 0 < c → c • a < c • b := ordered_semimodule.smul_lt_smul_of_pos lemma smul_le_smul_of_nonneg (h₁ : a ≤ b) (h₂ : 0 ≤ c) : c • a ≤ c • b := begin by_cases H₁ : c = 0, { simp [H₁, zero_smul] }, { by_cases H₂ : a = b, { rw H₂ }, { exact le_of_lt (smul_lt_smul_of_pos (lt_of_le_of_ne h₁ H₂) (lt_of_le_of_ne h₂ (ne.symm H₁))), } } end end ordered_semimodule section instances variables {R : Type*} [linear_ordered_ring R] instance linear_ordered_ring.to_ordered_semimodule : ordered_semimodule R R := { smul_lt_smul_of_pos := ordered_semiring.mul_lt_mul_of_pos_left, lt_of_smul_lt_smul_of_nonneg := λ _ _ _, lt_of_mul_lt_mul_left } end instances section order_dual variables {R β : Type*} instance [semiring R] [ordered_add_comm_monoid β] [semimodule R β] : has_scalar R (order_dual β) := { smul := @has_scalar.smul R β _ } instance [semiring R] [ordered_add_comm_monoid β] [semimodule R β] : mul_action R (order_dual β) := { one_smul := @mul_action.one_smul R β _ _, mul_smul := @mul_action.mul_smul R β _ _ } instance [semiring R] [ordered_add_comm_monoid β] [semimodule R β] : distrib_mul_action R (order_dual β) := { smul_add := @distrib_mul_action.smul_add R β _ _ _, smul_zero := @distrib_mul_action.smul_zero R β _ _ _ } instance [semiring R] [ordered_add_comm_monoid β] [semimodule R β] : semimodule R (order_dual β) := { add_smul := @semimodule.add_smul R β _ _ _, zero_smul := @semimodule.zero_smul R β _ _ _ } instance [ordered_semiring R] [ordered_add_comm_monoid β] [ordered_semimodule R β] : ordered_semimodule R (order_dual β) := { smul_lt_smul_of_pos := λ a b, @ordered_semimodule.smul_lt_smul_of_pos R β _ _ _ b a, lt_of_smul_lt_smul_of_nonneg := λ a b, @ordered_semimodule.lt_of_smul_lt_smul_of_nonneg R β _ _ _ b a } end order_dual
5ce19d78c745880ce358d63751e24899355479d0
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/big_operators/norm_num.lean
f410d99c47343c1da8db7a72bba7fffdc1a9a0e7
[ "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
14,540
lean
/- Copyright (c) 2022 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import algebra.big_operators.basic import tactic.norm_num /-! ### `norm_num` plugin for big operators This `norm_num` plugin provides support for computing sums and products of lists, multisets and finsets. Example goals this plugin can solve: * `∑ i in finset.range 10, (i^2 : ℕ) = 285` * `Π i in {1, 4, 9, 16}, nat.sqrt i = 24` * `([1, 2, 1, 3]).sum = 7` ## Implementation notes The tactic works by first converting the expression denoting the collection (list, multiset, finset) to a list of expressions denoting each element. For finsets, this involves erasing duplicate elements (the tactic fails if equality or disequality cannot be determined). After rewriting the big operator to a product/sum of lists, we evaluate this using `norm_num` itself to handle multiplication/addition. Finally, we package up the result using some congruence lemmas. -/ open tactic namespace tactic.norm_num /-- Use `norm_num` to decide equality between two expressions. If the decision procedure succeeds, the `bool` value indicates whether the expressions are equal, and the `expr` is a proof of (dis)equality. This procedure is partial: it will fail in cases where `norm_num` can't reduce either side to a rational numeral. -/ meta def decide_eq (l r : expr) : tactic (bool × expr) := do (l', l'_pf) ← or_refl_conv norm_num.derive l, (r', r'_pf) ← or_refl_conv norm_num.derive r, n₁ ← l'.to_rat, n₂ ← r'.to_rat, c ← infer_type l' >>= mk_instance_cache, if n₁ = n₂ then do pf ← i_to_expr ``(eq.trans %%l'_pf $ eq.symm %%r'_pf), pure (tt, pf) else do (_, p) ← norm_num.prove_ne c l' r' n₁ n₂, pure (ff, p) lemma list.not_mem_cons {α : Type*} {x y : α} {ys : list α} (h₁ : x ≠ y) (h₂ : x ∉ ys) : x ∉ y :: ys := λ h, ((list.mem_cons_iff _ _ _).mp h).elim h₁ h₂ /-- Use a decision procedure for the equality of list elements to decide list membership. If the decision procedure succeeds, the `bool` value indicates whether the expressions are equal, and the `expr` is a proof of (dis)equality. This procedure is partial iff its parameter `decide_eq` is partial. -/ meta def list.decide_mem (decide_eq : expr → expr → tactic (bool × expr)) : expr → list expr → tactic (bool × expr) | x [] := do pf ← i_to_expr ``(list.not_mem_nil %%x), pure (ff, pf) | x (y :: ys) := do (is_head, head_pf) ← decide_eq x y, if is_head then do pf ← i_to_expr ``((list.mem_cons_iff %%x %%y _).mpr (or.inl %%head_pf)), pure (tt, pf) else do (mem_tail, tail_pf) ← list.decide_mem x ys, if mem_tail then do pf ← i_to_expr ``((list.mem_cons_iff %%x %%y _).mpr (or.inr %%tail_pf)), pure (tt, pf) else do pf ← i_to_expr ``(list.not_mem_cons %%head_pf %%tail_pf), pure (ff, pf) lemma finset.insert_eq_coe_list_of_mem {α : Type*} [decidable_eq α] (x : α) (xs : finset α) {xs' : list α} (h : x ∈ xs') (nd_xs : xs'.nodup) (hxs' : xs = finset.mk ↑xs' (multiset.coe_nodup.mpr nd_xs)) : insert x xs = finset.mk ↑xs' (multiset.coe_nodup.mpr nd_xs) := have h : x ∈ xs, by simpa [hxs'] using h, by rw [finset.insert_eq_of_mem h, hxs'] lemma finset.insert_eq_coe_list_cons {α : Type*} [decidable_eq α] (x : α) (xs : finset α) {xs' : list α} (h : x ∉ xs') (nd_xs : xs'.nodup) (nd_xxs : (x :: xs').nodup) (hxs' : xs = finset.mk ↑xs' (multiset.coe_nodup.mpr nd_xs)) : insert x xs = finset.mk ↑(x :: xs') (multiset.coe_nodup.mpr nd_xxs) := have h : x ∉ xs, by simpa [hxs'] using h, by { rw [← finset.val_inj, finset.insert_val_of_not_mem h, hxs'], simp only [multiset.cons_coe] } /-- Convert an expression denoting a finset to a list of elements, a proof that this list is equal to the original finset, and a proof that the list contains no duplicates. We return a list rather than a finset, so we can more easily iterate over it (without having to prove that our tactics are independent of the order of iteration, which is in general not true). `decide_eq` is a (partial) decision procedure for determining whether two elements of the finset are equal, for example to parse `{2, 1, 2}` into `[2, 1]`. -/ meta def eval_finset (decide_eq : expr → expr → tactic (bool × expr)) : expr → tactic (list expr × expr × expr) | e@`(has_emptyc.emptyc) := do eq ← mk_eq_refl e, nd ← i_to_expr ``(list.nodup_nil), pure ([], eq, nd) | e@`(has_singleton.singleton %%x) := do eq ← mk_eq_refl e, nd ← i_to_expr ``(list.nodup_singleton %%x), pure ([x], eq, nd) | `(@@has_insert.insert (@@finset.has_insert %%dec) %%x %%xs) := do (exs, xs_eq, xs_nd) ← eval_finset xs, (is_mem, mem_pf) ← list.decide_mem decide_eq x exs, if is_mem then do pf ← i_to_expr ``(finset.insert_eq_coe_list_of_mem %%x %%xs %%mem_pf %%xs_nd %%xs_eq), pure (exs, pf, xs_nd) else do nd ← i_to_expr ``(list.nodup_cons.mpr ⟨%%mem_pf, %%xs_nd⟩), pf ← i_to_expr ``(finset.insert_eq_coe_list_cons %%x %%xs %%mem_pf %%xs_nd %%nd %%xs_eq), pure (x :: exs, pf, nd) | `(@@finset.univ %%ft) := do -- Convert the fintype instance expression `ft` to a list of its elements. -- Unfold it to the `fintype.mk` constructor and a list of arguments. `fintype.mk ← get_app_fn_const_whnf ft | fail (to_fmt "Unknown fintype expression" ++ format.line ++ to_fmt ft), [_, args, _] ← get_app_args_whnf ft | fail (to_fmt "Expected 3 arguments to `fintype.mk`"), eval_finset args | e@`(finset.range %%en) := do n ← expr.to_nat en, eis ← (list.range n).mmap (λ i, expr.of_nat `(ℕ) i), eq ← mk_eq_refl e, nd ← i_to_expr ``(list.nodup_range %%en), pure (eis, eq, nd) | e@`(finset.fin_range %%en) := do n ← expr.to_nat en, eis ← (list.fin_range n).mmap (λ i, expr.of_nat `(fin %%en) i), eq ← mk_eq_refl e, nd ← i_to_expr ``(list.nodup_fin_range %%en), pure (eis, eq, nd) | e := fail (to_fmt "Unknown finset expression" ++ format.line ++ to_fmt e) lemma list.map_cons_congr {α β : Type*} (f : α → β) {x : α} {xs : list α} {fx : β} {fxs : list β} (h₁ : f x = fx) (h₂ : xs.map f = fxs) : (x :: xs).map f = fx :: fxs := by rw [list.map_cons, h₁, h₂] /-- Apply `ef : α → β` to all elements of the list, constructing an equality proof. -/ meta def eval_list_map (ef : expr) : list expr → tactic (list expr × expr) | [] := do eq ← i_to_expr ``(list.map_nil %%ef), pure ([], eq) | (x :: xs) := do (fx, fx_eq) ← or_refl_conv norm_num.derive (expr.app ef x), (fxs, fxs_eq) ← eval_list_map xs, eq ← i_to_expr ``(list.map_cons_congr %%ef %%fx_eq %%fxs_eq), pure (fx :: fxs, eq) lemma multiset.cons_congr {α : Type*} (x : α) {xs : multiset α} {xs' : list α} (xs_eq : (xs' : multiset α) = xs) : (list.cons x xs' : multiset α) = x ::ₘ xs := by rw [← xs_eq]; refl lemma multiset.map_congr {α β : Type*} (f : α → β) {xs : multiset α} {xs' : list α} {ys : list β} (xs_eq : xs = (xs' : multiset α)) (ys_eq : xs'.map f = ys) : xs.map f = (ys : multiset β) := by rw [← ys_eq, ← multiset.coe_map, xs_eq] /-- Convert an expression denoting a multiset to a list of elements. We return a list rather than a finset, so we can more easily iterate over it (without having to prove that our tactics are independent of the order of iteration, which is in general not true). -/ meta def eval_multiset : expr → tactic (list expr × expr) | e@`(@has_zero.zero (multiset _) _) := do eq ← mk_eq_refl e, pure ([], eq) | e@`(has_emptyc.emptyc) := do eq ← mk_eq_refl e, pure ([], eq) | e@`(has_singleton.singleton %%x) := do eq ← mk_eq_refl e, pure ([x], eq) | e@`(multiset.cons %%x %%xs) := do (xs, xs_eq) ← eval_multiset xs, eq ← i_to_expr ``(multiset.cons_congr %%x %%xs_eq), pure (x :: xs, eq) | e@`(@@has_insert.insert multiset.has_insert %%x %%xs) := do (xs, xs_eq) ← eval_multiset xs, eq ← i_to_expr ``(multiset.cons_congr %%x %%xs_eq), pure (x :: xs, eq) | e@`(multiset.range %%en) := do n ← expr.to_nat en, eis ← (list.range n).mmap (λ i, expr.of_nat `(ℕ) i), eq ← mk_eq_refl e, pure (eis, eq) | `(@multiset.map %%α %%β %%ef %%exs) := do (xs, xs_eq) ← eval_multiset exs, (ys, ys_eq) ← eval_list_map ef xs, eq ← i_to_expr ``(multiset.map_congr %%ef %%xs_eq %%ys_eq), pure (ys, eq) | e := fail (to_fmt "Unknown multiset expression" ++ format.line ++ to_fmt e) lemma list.cons_congr {α : Type*} (x : α) {xs : list α} {xs' : list α} (xs_eq : xs' = xs) : x :: xs' = x :: xs := by rw xs_eq lemma list.map_congr {α β : Type*} (f : α → β) {xs xs' : list α} {ys : list β} (xs_eq : xs = xs') (ys_eq : xs'.map f = ys) : xs.map f = ys := by rw [← ys_eq, xs_eq] /-- Convert an expression denoting a list to a list of elements. -/ meta def eval_list : expr → tactic (list expr × expr) | e@`(list.nil) := do eq ← mk_eq_refl e, pure ([], eq) | e@`(list.cons %%x %%xs) := do (xs, xs_eq) ← eval_list xs, eq ← i_to_expr ``(list.cons_congr %%x %%xs_eq), pure (x :: xs, eq) | e@`(list.range %%en) := do n ← expr.to_nat en, eis ← (list.range n).mmap (λ i, expr.of_nat `(ℕ) i), eq ← mk_eq_refl e, pure (eis, eq) | `(@list.map %%α %%β %%ef %%exs) := do (xs, xs_eq) ← eval_list exs, (ys, ys_eq) ← eval_list_map ef xs, eq ← i_to_expr ``(list.map_congr %%ef %%xs_eq %%ys_eq), pure (ys, eq) | e := fail (to_fmt "Unknown list expression" ++ format.line ++ to_fmt e) @[to_additive] lemma list.prod_cons_congr {α : Type*} [monoid α] (xs : list α) (x y z : α) (his : xs.prod = y) (hi : x * y = z) : (x :: xs).prod = z := by rw [list.prod_cons, his, hi] /-- Evaluate `list.prod %%xs`, producing the evaluated expression and an equality proof. -/ meta def list.prove_prod (α : expr) : list expr → tactic (expr × expr) | [] := do result ← expr.of_nat α 1, proof ← i_to_expr ``(@list.prod_nil %%α _), pure (result, proof) | (x :: xs) := do eval_xs ← list.prove_prod xs, xxs ← i_to_expr ``(%%x * %%eval_xs.1), eval_xxs ← or_refl_conv norm_num.derive xxs, exs ← expr.of_list α xs, proof ← i_to_expr ``(list.prod_cons_congr %%exs%%x %%eval_xs.1 %%eval_xxs.1 %%eval_xs.2 %%eval_xxs.2), pure (eval_xxs.1, proof) /-- Evaluate `list.sum %%xs`, sumucing the evaluated expression and an equality proof. -/ meta def list.prove_sum (α : expr) : list expr → tactic (expr × expr) | [] := do result ← expr.of_nat α 0, proof ← i_to_expr ``(@list.sum_nil %%α _), pure (result, proof) | (x :: xs) := do eval_xs ← list.prove_sum xs, xxs ← i_to_expr ``(%%x + %%eval_xs.1), eval_xxs ← or_refl_conv norm_num.derive xxs, exs ← expr.of_list α xs, proof ← i_to_expr ``(list.sum_cons_congr %%exs%%x %%eval_xs.1 %%eval_xxs.1 %%eval_xs.2 %%eval_xxs.2), pure (eval_xxs.1, proof) @[to_additive] lemma list.prod_congr {α : Type*} [monoid α] {xs xs' : list α} {z : α} (h₁ : xs = xs') (h₂ : xs'.prod = z) : xs.prod = z := by cc @[to_additive] lemma multiset.prod_congr {α : Type*} [comm_monoid α] {xs : multiset α} {xs' : list α} {z : α} (h₁ : xs = (xs' : multiset α)) (h₂ : xs'.prod = z) : xs.prod = z := by rw [← h₂, ← multiset.coe_prod, h₁] /-- Evaluate `(%%xs.map (%%ef : %%α → %%β)).prod`, producing the evaluated expression and an equality proof. -/ meta def list.prove_prod_map (β ef : expr) (xs : list expr) : tactic (expr × expr) := do (fxs, fxs_eq) ← eval_list_map ef xs, (prod, prod_eq) ← list.prove_prod β fxs, eq ← i_to_expr ``(list.prod_congr %%fxs_eq %%prod_eq), pure (prod, eq) /-- Evaluate `(%%xs.map (%%ef : %%α → %%β)).sum`, producing the evaluated expression and an equality proof. -/ meta def list.prove_sum_map (β ef : expr) (xs : list expr) : tactic (expr × expr) := do (fxs, fxs_eq) ← eval_list_map ef xs, (sum, sum_eq) ← list.prove_sum β fxs, eq ← i_to_expr ``(list.sum_congr %%fxs_eq %%sum_eq), pure (sum, eq) @[to_additive] lemma finset.eval_prod_of_list {β α : Type*} [comm_monoid β] (s : finset α) (f : α → β) {is : list α} (his : is.nodup) (hs : finset.mk ↑is (multiset.coe_nodup.mpr his) = s) {x : β} (hx : (is.map f).prod = x) : s.prod f = x := by rw [← hs, finset.prod_mk, multiset.coe_map, multiset.coe_prod, hx] /-- `norm_num` plugin for evaluating big operators: * `list.prod` * `list.sum` * `multiset.prod` * `multiset.sum` * `finset.prod` * `finset.sum` -/ @[norm_num] meta def eval_big_operators : expr → tactic (expr × expr) | `(@list.prod %%α %%inst1 %%inst2 %%exs) := tactic.trace_error "Internal error in `tactic.norm_num.eval_big_operators`:" $ do (xs, list_eq) ← eval_list exs, (result, sum_eq) ← list.prove_prod α xs, pf ← i_to_expr ``(list.prod_congr %%list_eq %%sum_eq), pure (result, pf) | `(@list.sum %%α %%inst1 %%inst2 %%exs) := tactic.trace_error "Internal error in `tactic.norm_num.eval_big_operators`:" $ do (xs, list_eq) ← eval_list exs, (result, sum_eq) ← list.prove_sum α xs, pf ← i_to_expr ``(list.sum_congr %%list_eq %%sum_eq), pure (result, pf) | `(@multiset.prod %%α %%inst %%exs) := tactic.trace_error "Internal error in `tactic.norm_num.eval_big_operators`:" $ do (xs, list_eq) ← eval_multiset exs, (result, sum_eq) ← list.prove_prod α xs, pf ← i_to_expr ``(multiset.prod_congr %%list_eq %%sum_eq), pure (result, pf) | `(@multiset.sum %%α %%inst %%exs) := tactic.trace_error "Internal error in `tactic.norm_num.eval_big_operators`:" $ do (xs, list_eq) ← eval_multiset exs, (result, sum_eq) ← list.prove_sum α xs, pf ← i_to_expr ``(multiset.sum_congr %%list_eq %%sum_eq), pure (result, pf) | `(@finset.prod %%β %%α %%inst %%es %%ef) := tactic.trace_error "Internal error in `tactic.norm_num.eval_big_operators`:" $ do (xs, list_eq, nodup) ← eval_finset decide_eq es, (result, sum_eq) ← list.prove_prod_map β ef xs, pf ← i_to_expr ``(finset.eval_prod_of_list %%es %%ef %%nodup %%list_eq %%sum_eq), pure (result, pf) | `(@finset.sum %%β %%α %%inst %%es %%ef) := tactic.trace_error "Internal error in `tactic.norm_num.eval_big_operators`:" $ do (xs, list_eq, nodup) ← eval_finset decide_eq es, (result, sum_eq) ← list.prove_sum_map β ef xs, pf ← i_to_expr ``(finset.eval_sum_of_list %%es %%ef %%nodup %%list_eq %%sum_eq), pure (result, pf) | _ := failed end tactic.norm_num
69cb12ed4a0a2ac84d7e063e8b9e76603285a571
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/algebra/category/Group/colimits.lean
00f9f3f287b831f8120e8dd05cf03e44cfc16ca8
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,176
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.Group.preadditive import group_theory.quotient_group import category_theory.limits.concrete_category import category_theory.limits.shapes.kernels import category_theory.limits.shapes.concrete_category /-! # The category of additive commutative groups 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 `add_comm_group` and `monoid_hom`. TODO: In fact, in `AddCommGroup` there is a much nicer model of colimits as quotients of finitely supported functions, and we really should implement this as well (or instead). -/ 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. namespace AddCommGroup.colimits /-! We build the colimit of a diagram in `AddCommGroup` by constructing the free group on the disjoint union of all the abelian groups in the diagram, then taking the quotient by the abelian group laws within each abelian group, and the identifications given by the morphisms in the diagram. -/ variables {J : Type v} [small_category J] (F : J ⥤ AddCommGroup.{v}) /-- An inductive type representing all group 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 | neg : prequotient → prequotient | add : prequotient → prequotient → prequotient instance : inhabited (prequotient F) := ⟨prequotient.zero⟩ open prequotient /-- The relation on `prequotient` saying when two expressions are equal because of the abelian group 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 | 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)) -- 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') -- And one relation per axiom | zero_add : Π (x), relation (add zero x) x | add_zero : Π (x), relation (add x zero) x | add_left_neg : Π (x), relation (add (neg x) x) zero | add_comm : Π (x y), relation (add x y) (add y x) | add_assoc : Π (x y z), relation (add (add x y) z) (add x (add y z)) /-- The setoid corresponding to group expressions modulo abelian group 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 `AddCommGroup`. -/ @[derive inhabited] def colimit_type : Type v := quotient (colimit_setoid F) instance : add_comm_group (colimit_type F) := { zero := begin exact quot.mk _ zero 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, 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, 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, add_assoc := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.add_assoc, refl, refl, refl, end, } @[simp] lemma quot_zero : quot.mk setoid.r zero = (0 : 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 /-- The bundled abelian group giving the colimit of a diagram. -/ def colimit : AddCommGroup := AddCommGroup.of (colimit_type F) /-- The function from a given abelian group in the diagram to the colimit abelian group. -/ def cocone_fun (j : J) (x : F.obj j) : colimit_type F := quot.mk _ (of j x) /-- The group homomorphism from a given abelian group in the diagram to the colimit abelian group. -/ def cocone_morphism (j : J) : F.obj j ⟶ colimit F := { to_fun := cocone_fun F j, 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 abelian group. -/ def colimit_cocone : cocone F := { X := colimit F, ι := { app := cocone_morphism F } }. /-- The function from the free abelian group 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 | (neg x) := -(desc_fun_lift x) | (add x y) := desc_fun_lift x + desc_fun_lift y /-- The function from the colimit abelian group 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, }, -- neg { simp, }, -- add { simp, }, -- neg_1 { rw r_ih, }, -- add_1 { rw r_ih, }, -- add_2 { rw r_ih, }, -- zero_add { rw zero_add, }, -- add_zero { rw add_zero, }, -- add_left_neg { rw add_left_neg, }, -- add_comm { rw add_comm, }, -- add_assoc { rw add_assoc, }, } end /-- The group homomorphism from the colimit abelian group to the cone point of any other cocone. -/ def desc_morphism (s : cocone F) : colimit F ⟶ s.X := { to_fun := desc_fun F s, map_zero' := rfl, map_add' := λ x y, by { induction x; induction y; refl }, } /-- Evidence that the proposed colimit is the colimit. -/ def colimit_cocone_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 *, }, refl end }. instance has_colimits_AddCommGroup : has_colimits AddCommGroup := { has_colimits_of_shape := λ J 𝒥, by exactI { has_colimit := λ F, has_colimit.mk { cocone := colimit_cocone F, is_colimit := colimit_cocone_is_colimit F } } } end AddCommGroup.colimits namespace AddCommGroup open quotient_add_group /-- The categorical cokernel of a morphism in `AddCommGroup` agrees with the usual group-theoretical quotient. -/ noncomputable def cokernel_iso_quotient {G H : AddCommGroup} (f : G ⟶ H) : cokernel f ≅ AddCommGroup.of (quotient (add_monoid_hom.range f)) := { hom := cokernel.desc f (mk' _) (by { ext, apply quotient.sound, fsplit, exact -x, simp, }), inv := add_monoid_hom.of (quotient_add_group.lift _ (cokernel.π f) (by tidy)), } end AddCommGroup
67dd98c0a7df820e1e4881d43e342c74cee8d9de
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/normed_space/bounded_linear_maps.lean
67d91463d48ba71ec0cc584dfee803fceef14d1c
[ "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
25,716
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl -/ import analysis.normed_space.multilinear import analysis.normed_space.units import analysis.asymptotics.asymptotics /-! # Bounded linear maps This file defines a class stating that a map between normed vector spaces is (bi)linear and continuous. Instead of asking for continuity, the definition takes the equivalent condition (because the space is normed) that `∥f x∥` is bounded by a multiple of `∥x∥`. Hence the "bounded" in the name refers to `∥f x∥/∥x∥` rather than `∥f x∥` itself. ## Main definitions * `is_bounded_linear_map`: Class stating that a map `f : E → F` is linear and has `∥f x∥` bounded by a multiple of `∥x∥`. * `is_bounded_bilinear_map`: Class stating that a map `f : E × F → G` is bilinear and continuous, but through the simpler to provide statement that `∥f (x, y)∥` is bounded by a multiple of `∥x∥ * ∥y∥` * `is_bounded_bilinear_map.linear_deriv`: Derivative of a continuous bilinear map as a linear map. * `is_bounded_bilinear_map.deriv`: Derivative of a continuous bilinear map as a continuous linear map. The proof that it is indeed the derivative is `is_bounded_bilinear_map.has_fderiv_at` in `analysis.calculus.fderiv`. ## Main theorems * `is_bounded_bilinear_map.continuous`: A bounded bilinear map is continuous. * `continuous_linear_equiv.is_open`: The continuous linear equivalences are an open subset of the set of continuous linear maps between a pair of Banach spaces. Placed in this file because its proof uses `is_bounded_bilinear_map.continuous`. ## Notes The main use of this file is `is_bounded_bilinear_map`. The file `analysis.normed_space.multilinear` already expounds the theory of multilinear maps, but the `2`-variables case is sufficiently simpler to currently deserve its own treatment. `is_bounded_linear_map` is effectively an unbundled version of `continuous_linear_map` (defined in `topology.algebra.module.basic`, theory over normed spaces developed in `analysis.normed_space.operator_norm`), albeit the name disparity. A bundled `continuous_linear_map` is to be preferred over a `is_bounded_linear_map` hypothesis. Historical artifact, really. -/ noncomputable theory open_locale classical big_operators topological_space open filter (tendsto) metric continuous_linear_map variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] {G : Type*} [normed_add_comm_group G] [normed_space 𝕜 G] /-- A function `f` satisfies `is_bounded_linear_map 𝕜 f` if it is linear and satisfies the inequality `∥f x∥ ≤ M * ∥x∥` for some positive constant `M`. -/ structure is_bounded_linear_map (𝕜 : Type*) [normed_field 𝕜] {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] (f : E → F) extends is_linear_map 𝕜 f : Prop := (bound : ∃ M, 0 < M ∧ ∀ x : E, ∥f x∥ ≤ M * ∥x∥) lemma is_linear_map.with_bound {f : E → F} (hf : is_linear_map 𝕜 f) (M : ℝ) (h : ∀ x : E, ∥f x∥ ≤ M * ∥x∥) : is_bounded_linear_map 𝕜 f := ⟨ hf, classical.by_cases (assume : M ≤ 0, ⟨1, zero_lt_one, λ x, (h x).trans $ mul_le_mul_of_nonneg_right (this.trans zero_le_one) (norm_nonneg x)⟩) (assume : ¬ M ≤ 0, ⟨M, lt_of_not_ge this, h⟩)⟩ /-- A continuous linear map satisfies `is_bounded_linear_map` -/ lemma continuous_linear_map.is_bounded_linear_map (f : E →L[𝕜] F) : is_bounded_linear_map 𝕜 f := { bound := f.bound, ..f.to_linear_map.is_linear } namespace is_bounded_linear_map /-- Construct a linear map from a function `f` satisfying `is_bounded_linear_map 𝕜 f`. -/ def to_linear_map (f : E → F) (h : is_bounded_linear_map 𝕜 f) : E →ₗ[𝕜] F := (is_linear_map.mk' _ h.to_is_linear_map) /-- Construct a continuous linear map from is_bounded_linear_map -/ def to_continuous_linear_map {f : E → F} (hf : is_bounded_linear_map 𝕜 f) : E →L[𝕜] F := { cont := let ⟨C, Cpos, hC⟩ := hf.bound in add_monoid_hom_class.continuous_of_bound (to_linear_map f hf) C hC, ..to_linear_map f hf} lemma zero : is_bounded_linear_map 𝕜 (λ (x:E), (0:F)) := (0 : E →ₗ[𝕜] F).is_linear.with_bound 0 $ by simp [le_refl] lemma id : is_bounded_linear_map 𝕜 (λ (x:E), x) := linear_map.id.is_linear.with_bound 1 $ by simp [le_refl] lemma fst : is_bounded_linear_map 𝕜 (λ x : E × F, x.1) := begin refine (linear_map.fst 𝕜 E F).is_linear.with_bound 1 (λ x, _), rw one_mul, exact le_max_left _ _ end lemma snd : is_bounded_linear_map 𝕜 (λ x : E × F, x.2) := begin refine (linear_map.snd 𝕜 E F).is_linear.with_bound 1 (λ x, _), rw one_mul, exact le_max_right _ _ end variables {f g : E → F} lemma smul (c : 𝕜) (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (c • f) := let ⟨hlf, M, hMp, hM⟩ := hf in (c • hlf.mk' f).is_linear.with_bound (∥c∥ * M) $ λ x, calc ∥c • f x∥ = ∥c∥ * ∥f x∥ : norm_smul c (f x) ... ≤ ∥c∥ * (M * ∥x∥) : mul_le_mul_of_nonneg_left (hM _) (norm_nonneg _) ... = (∥c∥ * M) * ∥x∥ : (mul_assoc _ _ _).symm lemma neg (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (λ e, -f e) := begin rw show (λ e, -f e) = (λ e, (-1 : 𝕜) • f e), { funext, simp }, exact smul (-1) hf end lemma add (hf : is_bounded_linear_map 𝕜 f) (hg : is_bounded_linear_map 𝕜 g) : is_bounded_linear_map 𝕜 (λ e, f e + g e) := let ⟨hlf, Mf, hMfp, hMf⟩ := hf in let ⟨hlg, Mg, hMgp, hMg⟩ := hg in (hlf.mk' _ + hlg.mk' _).is_linear.with_bound (Mf + Mg) $ λ x, calc ∥f x + g x∥ ≤ Mf * ∥x∥ + Mg * ∥x∥ : norm_add_le_of_le (hMf x) (hMg x) ... ≤ (Mf + Mg) * ∥x∥ : by rw add_mul lemma sub (hf : is_bounded_linear_map 𝕜 f) (hg : is_bounded_linear_map 𝕜 g) : is_bounded_linear_map 𝕜 (λ e, f e - g e) := by simpa [sub_eq_add_neg] using add hf (neg hg) lemma comp {g : F → G} (hg : is_bounded_linear_map 𝕜 g) (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (g ∘ f) := (hg.to_continuous_linear_map.comp hf.to_continuous_linear_map).is_bounded_linear_map protected lemma tendsto (x : E) (hf : is_bounded_linear_map 𝕜 f) : tendsto f (𝓝 x) (𝓝 (f x)) := let ⟨hf, M, hMp, hM⟩ := hf in tendsto_iff_norm_tendsto_zero.2 $ squeeze_zero (λ e, norm_nonneg _) (λ e, calc ∥f e - f x∥ = ∥hf.mk' f (e - x)∥ : by rw (hf.mk' _).map_sub e x; refl ... ≤ M * ∥e - x∥ : hM (e - x)) (suffices tendsto (λ (e : E), M * ∥e - x∥) (𝓝 x) (𝓝 (M * 0)), by simpa, tendsto_const_nhds.mul (tendsto_norm_sub_self _)) lemma continuous (hf : is_bounded_linear_map 𝕜 f) : continuous f := continuous_iff_continuous_at.2 $ λ _, hf.tendsto _ lemma lim_zero_bounded_linear_map (hf : is_bounded_linear_map 𝕜 f) : tendsto f (𝓝 0) (𝓝 0) := (hf.1.mk' _).map_zero ▸ continuous_iff_continuous_at.1 hf.continuous 0 section open asymptotics filter theorem is_O_id {f : E → F} (h : is_bounded_linear_map 𝕜 f) (l : filter E) : f =O[l] (λ x, x) := let ⟨M, hMp, hM⟩ := h.bound in is_O.of_bound _ (mem_of_superset univ_mem (λ x _, hM x)) theorem is_O_comp {E : Type*} {g : F → G} (hg : is_bounded_linear_map 𝕜 g) {f : E → F} (l : filter E) : (λ x', g (f x')) =O[l] f := (hg.is_O_id ⊤).comp_tendsto le_top theorem is_O_sub {f : E → F} (h : is_bounded_linear_map 𝕜 f) (l : filter E) (x : E) : (λ x', f (x' - x)) =O[l] (λ x', x' - x) := is_O_comp h l end end is_bounded_linear_map section variables {ι : Type*} [decidable_eq ι] [fintype ι] /-- Taking the cartesian product of two continuous multilinear maps is a bounded linear operation. -/ lemma is_bounded_linear_map_prod_multilinear {E : ι → Type*} [∀ i, normed_add_comm_group (E i)] [∀ i, normed_space 𝕜 (E i)] : is_bounded_linear_map 𝕜 (λ p : (continuous_multilinear_map 𝕜 E F) × (continuous_multilinear_map 𝕜 E G), p.1.prod p.2) := { map_add := λ p₁ p₂, by { ext1 m, refl }, map_smul := λ c p, by { ext1 m, refl }, bound := ⟨1, zero_lt_one, λ p, begin rw one_mul, apply continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λ m, _), rw [continuous_multilinear_map.prod_apply, norm_prod_le_iff], split, { exact (p.1.le_op_norm m).trans (mul_le_mul_of_nonneg_right (norm_fst_le p) (finset.prod_nonneg (λ i hi, norm_nonneg _))) }, { exact (p.2.le_op_norm m).trans (mul_le_mul_of_nonneg_right (norm_snd_le p) (finset.prod_nonneg (λ i hi, norm_nonneg _))) }, end⟩ } /-- Given a fixed continuous linear map `g`, associating to a continuous multilinear map `f` the continuous multilinear map `f (g m₁, ..., g mₙ)` is a bounded linear operation. -/ lemma is_bounded_linear_map_continuous_multilinear_map_comp_linear (g : G →L[𝕜] E) : is_bounded_linear_map 𝕜 (λ f : continuous_multilinear_map 𝕜 (λ (i : ι), E) F, f.comp_continuous_linear_map (λ _, g)) := begin refine is_linear_map.with_bound ⟨λ f₁ f₂, by { ext m, refl }, λ c f, by { ext m, refl }⟩ (∥g∥ ^ (fintype.card ι)) (λ f, _), apply continuous_multilinear_map.op_norm_le_bound _ _ (λ m, _), { apply_rules [mul_nonneg, pow_nonneg, norm_nonneg] }, calc ∥f (g ∘ m)∥ ≤ ∥f∥ * ∏ i, ∥g (m i)∥ : f.le_op_norm _ ... ≤ ∥f∥ * ∏ i, (∥g∥ * ∥m i∥) : begin apply mul_le_mul_of_nonneg_left _ (norm_nonneg _), exact finset.prod_le_prod (λ i hi, norm_nonneg _) (λ i hi, g.le_op_norm _) end ... = ∥g∥ ^ fintype.card ι * ∥f∥ * ∏ i, ∥m i∥ : by { simp [finset.prod_mul_distrib, finset.card_univ], ring } end end section bilinear_map namespace continuous_linear_map /-! We prove some computation rules for continuous (semi-)bilinear maps in their first argument. If `f` is a continuuous bilinear map, to use the corresponding rules for the second argument, use `(f _).map_add` and similar. We have to assume that `F` and `G` are normed spaces in this section, to use `continuous_linear_map.to_normed_add_comm_group`, but we don't need to assume this for the first argument of `f`. -/ variables {R : Type*} variables {𝕜₂ 𝕜' : Type*} [nontrivially_normed_field 𝕜'] [nontrivially_normed_field 𝕜₂] variables {M : Type*} [topological_space M] variables {σ₁₂ : 𝕜 →+* 𝕜₂} [ring_hom_isometric σ₁₂] variables {G' : Type*} [normed_add_comm_group G'] [normed_space 𝕜₂ G'] [normed_space 𝕜' G'] variables [smul_comm_class 𝕜₂ 𝕜' G'] section semiring variables [semiring R] [add_comm_monoid M] [module R M] {ρ₁₂ : R →+* 𝕜'} lemma map_add₂ (f : M →SL[ρ₁₂] F →SL[σ₁₂] G') (x x' : M) (y : F) : f (x + x') y = f x y + f x' y := by rw [f.map_add, add_apply] lemma map_zero₂ (f : M →SL[ρ₁₂] F →SL[σ₁₂] G') (y : F) : f 0 y = 0 := by rw [f.map_zero, zero_apply] lemma map_smulₛₗ₂ (f : M →SL[ρ₁₂] F →SL[σ₁₂] G') (c : R) (x : M) (y : F) : f (c • x) y = ρ₁₂ c • f x y := by rw [f.map_smulₛₗ, smul_apply] end semiring section ring variables [ring R] [add_comm_group M] [module R M] {ρ₁₂ : R →+* 𝕜'} lemma map_sub₂ (f : M →SL[ρ₁₂] F →SL[σ₁₂] G') (x x' : M) (y : F) : f (x - x') y = f x y - f x' y := by rw [f.map_sub, sub_apply] lemma map_neg₂ (f : M →SL[ρ₁₂] F →SL[σ₁₂] G') (x : M) (y : F) : f (- x) y = - f x y := by rw [f.map_neg, neg_apply] end ring lemma map_smul₂ (f : E →L[𝕜] F →L[𝕜] G) (c : 𝕜) (x : E) (y : F) : f (c • x) y = c • f x y := by rw [f.map_smul, smul_apply] end continuous_linear_map variable (𝕜) /-- A map `f : E × F → G` satisfies `is_bounded_bilinear_map 𝕜 f` if it is bilinear and continuous. -/ structure is_bounded_bilinear_map (f : E × F → G) : Prop := (add_left : ∀ (x₁ x₂ : E) (y : F), f (x₁ + x₂, y) = f (x₁, y) + f (x₂, y)) (smul_left : ∀ (c : 𝕜) (x : E) (y : F), f (c • x, y) = c • f (x, y)) (add_right : ∀ (x : E) (y₁ y₂ : F), f (x, y₁ + y₂) = f (x, y₁) + f (x, y₂)) (smul_right : ∀ (c : 𝕜) (x : E) (y : F), f (x, c • y) = c • f (x,y)) (bound : ∃ C > 0, ∀ (x : E) (y : F), ∥f (x, y)∥ ≤ C * ∥x∥ * ∥y∥) variable {𝕜} variable {f : E × F → G} lemma continuous_linear_map.is_bounded_bilinear_map (f : E →L[𝕜] F →L[𝕜] G) : is_bounded_bilinear_map 𝕜 (λ x : E × F, f x.1 x.2) := { add_left := f.map_add₂, smul_left := f.map_smul₂, add_right := λ x, (f x).map_add, smul_right := λ c x, (f x).map_smul c, bound := ⟨max ∥f∥ 1, zero_lt_one.trans_le (le_max_right _ _), λ x y, (f.le_op_norm₂ x y).trans $ by apply_rules [mul_le_mul_of_nonneg_right, norm_nonneg, le_max_left]⟩ } protected lemma is_bounded_bilinear_map.is_O (h : is_bounded_bilinear_map 𝕜 f) : f =O[⊤] (λ p : E × F, ∥p.1∥ * ∥p.2∥) := let ⟨C, Cpos, hC⟩ := h.bound in asymptotics.is_O.of_bound _ $ filter.eventually_of_forall $ λ ⟨x, y⟩, by simpa [mul_assoc] using hC x y lemma is_bounded_bilinear_map.is_O_comp {α : Type*} (H : is_bounded_bilinear_map 𝕜 f) {g : α → E} {h : α → F} {l : filter α} : (λ x, f (g x, h x)) =O[l] (λ x, ∥g x∥ * ∥h x∥) := H.is_O.comp_tendsto le_top protected lemma is_bounded_bilinear_map.is_O' (h : is_bounded_bilinear_map 𝕜 f) : f =O[⊤] (λ p : E × F, ∥p∥ * ∥p∥) := h.is_O.trans (asymptotics.is_O_fst_prod'.norm_norm.mul asymptotics.is_O_snd_prod'.norm_norm) lemma is_bounded_bilinear_map.map_sub_left (h : is_bounded_bilinear_map 𝕜 f) {x y : E} {z : F} : f (x - y, z) = f (x, z) - f(y, z) := calc f (x - y, z) = f (x + (-1 : 𝕜) • y, z) : by simp [sub_eq_add_neg] ... = f (x, z) + (-1 : 𝕜) • f (y, z) : by simp only [h.add_left, h.smul_left] ... = f (x, z) - f (y, z) : by simp [sub_eq_add_neg] lemma is_bounded_bilinear_map.map_sub_right (h : is_bounded_bilinear_map 𝕜 f) {x : E} {y z : F} : f (x, y - z) = f (x, y) - f (x, z) := calc f (x, y - z) = f (x, y + (-1 : 𝕜) • z) : by simp [sub_eq_add_neg] ... = f (x, y) + (-1 : 𝕜) • f (x, z) : by simp only [h.add_right, h.smul_right] ... = f (x, y) - f (x, z) : by simp [sub_eq_add_neg] /-- Useful to use together with `continuous.comp₂`. -/ lemma is_bounded_bilinear_map.continuous (h : is_bounded_bilinear_map 𝕜 f) : continuous f := begin have one_ne : (1:ℝ) ≠ 0 := by simp, obtain ⟨C, (Cpos : 0 < C), hC⟩ := h.bound, rw continuous_iff_continuous_at, intros x, have H : ∀ (a:E) (b:F), ∥f (a, b)∥ ≤ C * ∥∥a∥ * ∥b∥∥, { intros a b, simpa [mul_assoc] using hC a b }, have h₁ : (λ e : E × F, f (e.1 - x.1, e.2)) =o[𝓝 x] (λ e, (1:ℝ)), { refine (asymptotics.is_O_of_le' (𝓝 x) (λ e, H (e.1 - x.1) e.2)).trans_is_o _, rw asymptotics.is_o_const_iff one_ne, convert ((continuous_fst.sub continuous_const).norm.mul continuous_snd.norm).continuous_at, { simp }, apply_instance }, have h₂ : (λ e : E × F, f (x.1, e.2 - x.2)) =o[𝓝 x] (λ e, (1:ℝ)), { refine (asymptotics.is_O_of_le' (𝓝 x) (λ e, H x.1 (e.2 - x.2))).trans_is_o _, rw asymptotics.is_o_const_iff one_ne, convert (continuous_const.mul (continuous_snd.sub continuous_const).norm).continuous_at, { simp }, apply_instance }, have := h₁.add h₂, rw asymptotics.is_o_const_iff one_ne at this, change tendsto _ _ _, convert this.add_const (f x), { ext e, simp [h.map_sub_left, h.map_sub_right], }, { simp } end lemma is_bounded_bilinear_map.continuous_left (h : is_bounded_bilinear_map 𝕜 f) {e₂ : F} : continuous (λe₁, f (e₁, e₂)) := h.continuous.comp (continuous_id.prod_mk continuous_const) lemma is_bounded_bilinear_map.continuous_right (h : is_bounded_bilinear_map 𝕜 f) {e₁ : E} : continuous (λe₂, f (e₁, e₂)) := h.continuous.comp (continuous_const.prod_mk continuous_id) /-- Useful to use together with `continuous.comp₂`. -/ lemma continuous_linear_map.continuous₂ (f : E →L[𝕜] F →L[𝕜] G) : continuous (function.uncurry (λ x y, f x y)) := f.is_bounded_bilinear_map.continuous lemma is_bounded_bilinear_map.is_bounded_linear_map_left (h : is_bounded_bilinear_map 𝕜 f) (y : F) : is_bounded_linear_map 𝕜 (λ x, f (x, y)) := { map_add := λ x x', h.add_left _ _ _, map_smul := λ c x, h.smul_left _ _ _, bound := begin rcases h.bound with ⟨C, C_pos, hC⟩, refine ⟨C * (∥y∥ + 1), mul_pos C_pos (lt_of_lt_of_le (zero_lt_one) (by simp)), λ x, _⟩, have : ∥y∥ ≤ ∥y∥ + 1, by simp [zero_le_one], calc ∥f (x, y)∥ ≤ C * ∥x∥ * ∥y∥ : hC x y ... ≤ C * ∥x∥ * (∥y∥ + 1) : by apply_rules [norm_nonneg, mul_le_mul_of_nonneg_left, le_of_lt C_pos, mul_nonneg] ... = C * (∥y∥ + 1) * ∥x∥ : by ring end } lemma is_bounded_bilinear_map.is_bounded_linear_map_right (h : is_bounded_bilinear_map 𝕜 f) (x : E) : is_bounded_linear_map 𝕜 (λ y, f (x, y)) := { map_add := λ y y', h.add_right _ _ _, map_smul := λ c y, h.smul_right _ _ _, bound := begin rcases h.bound with ⟨C, C_pos, hC⟩, refine ⟨C * (∥x∥ + 1), mul_pos C_pos (lt_of_lt_of_le (zero_lt_one) (by simp)), λ y, _⟩, have : ∥x∥ ≤ ∥x∥ + 1, by simp [zero_le_one], calc ∥f (x, y)∥ ≤ C * ∥x∥ * ∥y∥ : hC x y ... ≤ C * (∥x∥ + 1) * ∥y∥ : by apply_rules [mul_le_mul_of_nonneg_right, norm_nonneg, mul_le_mul_of_nonneg_left, le_of_lt C_pos] end } lemma is_bounded_bilinear_map_smul {𝕜' : Type*} [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜' × E), p.1 • p.2) := (lsmul 𝕜 𝕜' : 𝕜' →L[𝕜] E →L[𝕜] E).is_bounded_bilinear_map lemma is_bounded_bilinear_map_mul : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜 × 𝕜), p.1 * p.2) := by simp_rw ← smul_eq_mul; exact is_bounded_bilinear_map_smul lemma is_bounded_bilinear_map_comp : is_bounded_bilinear_map 𝕜 (λ (p : (F →L[𝕜] G) × (E →L[𝕜] F)), p.1.comp p.2) := (compL 𝕜 E F G).is_bounded_bilinear_map lemma continuous_linear_map.is_bounded_linear_map_comp_left (g : F →L[𝕜] G) : is_bounded_linear_map 𝕜 (λ (f : E →L[𝕜] F), continuous_linear_map.comp g f) := is_bounded_bilinear_map_comp.is_bounded_linear_map_right _ lemma continuous_linear_map.is_bounded_linear_map_comp_right (f : E →L[𝕜] F) : is_bounded_linear_map 𝕜 (λ (g : F →L[𝕜] G), continuous_linear_map.comp g f) := is_bounded_bilinear_map_comp.is_bounded_linear_map_left _ lemma is_bounded_bilinear_map_apply : is_bounded_bilinear_map 𝕜 (λ p : (E →L[𝕜] F) × E, p.1 p.2) := (continuous_linear_map.flip (apply 𝕜 F : E →L[𝕜] (E →L[𝕜] F) →L[𝕜] F)).is_bounded_bilinear_map /-- The function `continuous_linear_map.smul_right`, associating to a continuous linear map `f : E → 𝕜` and a scalar `c : F` the tensor product `f ⊗ c` as a continuous linear map from `E` to `F`, is a bounded bilinear map. -/ lemma is_bounded_bilinear_map_smul_right : is_bounded_bilinear_map 𝕜 (λ p, (continuous_linear_map.smul_right : (E →L[𝕜] 𝕜) → F → (E →L[𝕜] F)) p.1 p.2) := (smul_rightL 𝕜 E F).is_bounded_bilinear_map /-- The composition of a continuous linear map with a continuous multilinear map is a bounded bilinear operation. -/ lemma is_bounded_bilinear_map_comp_multilinear {ι : Type*} {E : ι → Type*} [decidable_eq ι] [fintype ι] [∀ i, normed_add_comm_group (E i)] [∀ i, normed_space 𝕜 (E i)] : is_bounded_bilinear_map 𝕜 (λ p : (F →L[𝕜] G) × (continuous_multilinear_map 𝕜 E F), p.1.comp_continuous_multilinear_map p.2) := (comp_continuous_multilinear_mapL 𝕜 E F G).is_bounded_bilinear_map /-- Definition of the derivative of a bilinear map `f`, given at a point `p` by `q ↦ f(p.1, q.2) + f(q.1, p.2)` as in the standard formula for the derivative of a product. We define this function here as a linear map `E × F →ₗ[𝕜] G`, then `is_bounded_bilinear_map.deriv` strengthens it to a continuous linear map `E × F →L[𝕜] G`. ``. -/ def is_bounded_bilinear_map.linear_deriv (h : is_bounded_bilinear_map 𝕜 f) (p : E × F) : E × F →ₗ[𝕜] G := { to_fun := λ q, f (p.1, q.2) + f (q.1, p.2), map_add' := λ q₁ q₂, begin change f (p.1, q₁.2 + q₂.2) + f (q₁.1 + q₂.1, p.2) = f (p.1, q₁.2) + f (q₁.1, p.2) + (f (p.1, q₂.2) + f (q₂.1, p.2)), simp [h.add_left, h.add_right], abel end, map_smul' := λ c q, begin change f (p.1, c • q.2) + f (c • q.1, p.2) = c • (f (p.1, q.2) + f (q.1, p.2)), simp [h.smul_left, h.smul_right, smul_add] end } /-- The derivative of a bounded bilinear map at a point `p : E × F`, as a continuous linear map from `E × F` to `G`. The statement that this is indeed the derivative of `f` is `is_bounded_bilinear_map.has_fderiv_at` in `analysis.calculus.fderiv`. -/ def is_bounded_bilinear_map.deriv (h : is_bounded_bilinear_map 𝕜 f) (p : E × F) : E × F →L[𝕜] G := (h.linear_deriv p).mk_continuous_of_exists_bound $ begin rcases h.bound with ⟨C, Cpos, hC⟩, refine ⟨C * ∥p.1∥ + C * ∥p.2∥, λ q, _⟩, calc ∥f (p.1, q.2) + f (q.1, p.2)∥ ≤ C * ∥p.1∥ * ∥q.2∥ + C * ∥q.1∥ * ∥p.2∥ : norm_add_le_of_le (hC _ _) (hC _ _) ... ≤ C * ∥p.1∥ * ∥q∥ + C * ∥q∥ * ∥p.2∥ : begin apply add_le_add, exact mul_le_mul_of_nonneg_left (le_max_right _ _) (mul_nonneg (le_of_lt Cpos) (norm_nonneg _)), apply mul_le_mul_of_nonneg_right _ (norm_nonneg _), exact mul_le_mul_of_nonneg_left (le_max_left _ _) (le_of_lt Cpos), end ... = (C * ∥p.1∥ + C * ∥p.2∥) * ∥q∥ : by ring end @[simp] lemma is_bounded_bilinear_map_deriv_coe (h : is_bounded_bilinear_map 𝕜 f) (p q : E × F) : h.deriv p q = f (p.1, q.2) + f (q.1, p.2) := rfl variables (𝕜) /-- The function `lmul_left_right : 𝕜' × 𝕜' → (𝕜' →L[𝕜] 𝕜')` is a bounded bilinear map. -/ lemma continuous_linear_map.lmul_left_right_is_bounded_bilinear (𝕜' : Type*) [normed_ring 𝕜'] [normed_algebra 𝕜 𝕜'] : is_bounded_bilinear_map 𝕜 (λ p : 𝕜' × 𝕜', continuous_linear_map.lmul_left_right 𝕜 𝕜' p.1 p.2) := (continuous_linear_map.lmul_left_right 𝕜 𝕜').is_bounded_bilinear_map variables {𝕜} /-- Given a bounded bilinear map `f`, the map associating to a point `p` the derivative of `f` at `p` is itself a bounded linear map. -/ lemma is_bounded_bilinear_map.is_bounded_linear_map_deriv (h : is_bounded_bilinear_map 𝕜 f) : is_bounded_linear_map 𝕜 (λ p : E × F, h.deriv p) := begin rcases h.bound with ⟨C, Cpos : 0 < C, hC⟩, refine is_linear_map.with_bound ⟨λ p₁ p₂, _, λ c p, _⟩ (C + C) (λ p, _), { ext; simp [h.add_left, h.add_right]; abel }, { ext; simp [h.smul_left, h.smul_right, smul_add] }, { refine continuous_linear_map.op_norm_le_bound _ (mul_nonneg (add_nonneg Cpos.le Cpos.le) (norm_nonneg _)) (λ q, _), calc ∥f (p.1, q.2) + f (q.1, p.2)∥ ≤ C * ∥p.1∥ * ∥q.2∥ + C * ∥q.1∥ * ∥p.2∥ : norm_add_le_of_le (hC _ _) (hC _ _) ... ≤ C * ∥p∥ * ∥q∥ + C * ∥q∥ * ∥p∥ : by apply_rules [add_le_add, mul_le_mul, norm_nonneg, Cpos.le, le_refl, le_max_left, le_max_right, mul_nonneg] ... = (C + C) * ∥p∥ * ∥q∥ : by ring }, end end bilinear_map lemma continuous.clm_comp {X} [topological_space X] {g : X → F →L[𝕜] G} {f : X → E →L[𝕜] F} (hg : continuous g) (hf : continuous f) : continuous (λ x, (g x).comp (f x)) := (compL 𝕜 E F G).continuous₂.comp₂ hg hf lemma continuous_on.clm_comp {X} [topological_space X] {g : X → F →L[𝕜] G} {f : X → E →L[𝕜] F} {s : set X} (hg : continuous_on g s) (hf : continuous_on f s) : continuous_on (λ x, (g x).comp (f x)) s := (compL 𝕜 E F G).continuous₂.comp_continuous_on (hg.prod hf) namespace continuous_linear_equiv open set /-! ### The set of continuous linear equivalences between two Banach spaces is open In this section we establish that the set of continuous linear equivalences between two Banach spaces is an open subset of the space of linear maps between them. -/ protected lemma is_open [complete_space E] : is_open (range (coe : (E ≃L[𝕜] F) → (E →L[𝕜] F))) := begin rw [is_open_iff_mem_nhds, forall_range_iff], refine λ e, is_open.mem_nhds _ (mem_range_self _), let O : (E →L[𝕜] F) → (E →L[𝕜] E) := λ f, (e.symm : F →L[𝕜] E).comp f, have h_O : continuous O := is_bounded_bilinear_map_comp.continuous_right, convert units.is_open.preimage h_O using 1, ext f', split, { rintros ⟨e', rfl⟩, exact ⟨(e'.trans e.symm).to_unit, rfl⟩ }, { rintros ⟨w, hw⟩, use (units_equiv 𝕜 E w).trans e, ext x, simp [coe_fn_coe_base' w, hw] } end protected lemma nhds [complete_space E] (e : E ≃L[𝕜] F) : (range (coe : (E ≃L[𝕜] F) → (E →L[𝕜] F))) ∈ 𝓝 (e : E →L[𝕜] F) := is_open.mem_nhds continuous_linear_equiv.is_open (by simp) end continuous_linear_equiv
beea5ed7e239cf4b5ec2dd96a441566af6613360
367134ba5a65885e863bdc4507601606690974c1
/src/ring_theory/witt_vector/is_poly.lean
c58dd8590414a5dba595ac240a6fedcead8af025
[ "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
23,415
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Robert Y. Lewis -/ import algebra.ring.ulift import ring_theory.witt_vector.basic import data.mv_polynomial.funext /-! # The `is_poly` predicate `witt_vector.is_poly` is a (type-valued) predicate on functions `f : Π R, 𝕎 R → 𝕎 R`. It asserts that there is a family of polynomials `φ : ℕ → mv_polynomial ℕ ℤ`, such that the `n`th coefficient of `f x` is equal to `φ n` evaluated on the coefficients of `x`. Many operations on Witt vectors satisfy this predicate (or an analogue for higher arity functions). We say that such a function `f` is a *polynomial function*. The power of satisfying this predicate comes from `is_poly.ext`. It shows that if `φ` and `ψ` witness that `f` and `g` are polynomial functions, then `f = g` not merely when `φ = ψ`, but in fact it suffices to prove ``` ∀ n, bind₁ φ (witt_polynomial p _ n) = bind₁ ψ (witt_polynomial p _ n) ``` (in other words, when evaluating the Witt polynomials on `φ` and `ψ`, we get the same values) which will then imply `φ = ψ` and hence `f = g`. Even though this sufficient condition looks somewhat intimidating, it is rather pleasant to check in practice; more so than direct checking of `φ = ψ`. In practice, we apply this technique to show that the composition of `witt_vector.frobenius` and `witt_vector.verschiebung` is equal to multiplication by `p`. ## Main declarations * `witt_vector.is_poly`, `witt_vector.is_poly₂`: two predicates that assert that a unary/binary function on Witt vectors is polynomial in the coefficients of the input values. * `witt_vector.is_poly.ext`, `witt_vector.is_poly₂.ext`: two polynomial functions are equal if their families of polynomials are equal after evaluating the Witt polynmials on them. * `witt_vector.is_poly.comp` (+ many variants) show that unary/binary compositions of polynomial functions are polynomial. * `witt_vector.id_is_poly`, `witt_vector.neg_is_poly`, `witt_vector.add_is_poly₂`, `witt_vector.mul_is_poly₂`: several well-known operations are polynomial functions (for Verschiebung, Frobenius, and multiplication by `p`, see their respective files). ## On higher arity analogues Ideally, there should be a predicate `is_polyₙ` for functions of higher arity, together with `is_polyₙ.comp` that shows how such functions compose. Since mathlib does not have a library on composition of higher arity functions, we have only implemented the unary and binary variants so far. Nullary functions (a.k.a. constants) are treated as constant functions and fall under the unary case. ## Tactics There are important metaprograms defined in this file: the tactics `ghost_simp` and `ghost_calc` and the attributes `@[is_poly]` and `@[ghost_simps]`. These are used in combination to discharge proofs of identities between polynomial functions. Any atomic proof of `is_poly` or `is_poly₂` (i.e. not taking additional `is_poly` arguments) should be tagged as `@[is_poly]`. Any lemma doing "ring equation rewriting" with polynomial functions should be tagged `@[ghost_simps]`, e.g. ```lean @[ghost_simps] lemma bind₁_frobenius_poly_witt_polynomial (n : ℕ) : bind₁ (frobenius_poly p) (witt_polynomial p ℤ n) = (witt_polynomial p ℤ (n+1)) ``` Proofs of identities between polynomial functions will often follow the pattern ```lean begin ghost_calc _, <minor preprocessing>, ghost_simp end ``` ## References * [Hazewinkel, *Witt Vectors*][Haze09] * [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21] -/ /- ### Simplification tactics `ghost_simp` is used later in the development for certain simplifications. We define it here so it is a shared import. -/ mk_simp_attribute ghost_simps "Simplification rules for ghost equations" namespace tactic namespace interactive setup_tactic_parser /-- A macro for a common simplification when rewriting with ghost component equations. -/ meta def ghost_simp (lems : parse simp_arg_list) : tactic unit := do tactic.try tactic.intro1, simp none none tt (lems ++ [simp_arg_type.symm_expr ``(sub_eq_add_neg)]) [`ghost_simps] (loc.ns [none]) /-- `ghost_calc` is a tactic for proving identities between polynomial functions. Typically, when faced with a goal like ```lean ∀ (x y : 𝕎 R), verschiebung (x * frobenius y) = verschiebung x * y ``` you can 1. call `ghost_calc` 2. do a small amount of manual work -- maybe nothing, maybe `rintro`, etc 3. call `ghost_simp` and this will close the goal. `ghost_calc` cannot detect whether you are dealing with unary or binary polynomial functions. You must give it arguments to determine this. If you are proving a universally quantified goal like the above, call `ghost_calc _ _`. If the variables are introduced already, call `ghost_calc x y`. In the unary case, use `ghost_calc _` or `ghost_calc x`. `ghost_calc` is a light wrapper around type class inference. All it does is apply the appropriate extensionality lemma and try to infer the resulting goals. This is subtle and Lean's elaborator doesn't like it because of the HO unification involved, so it is easier (and prettier) to put it in a tactic script. -/ meta def ghost_calc (ids' : parse ident_*) : tactic unit := do ids ← ids'.mmap $ λ n, get_local n <|> tactic.intro n, `(@eq (witt_vector _ %%R) _ _) ← target, match ids with | [x] := refine ```(is_poly.ext _ _ _ _ %%x) | [x, y] := refine ```(is_poly₂.ext _ _ _ _ %%x %%y) | _ := fail "ghost_calc takes one or two arguments" end, nm ← match R with | expr.local_const _ nm _ _ := return nm | _ := get_unused_name `R end, iterate_exactly 2 apply_instance, unfreezingI (tactic.clear' tt [R]), introsI $ [nm, nm<.>"_inst"] ++ ids', skip end interactive end tactic namespace witt_vector universe variable u variables {p : ℕ} {R S : Type u} {σ idx : Type*} [hp : fact p.prime] [comm_ring R] [comm_ring S] local notation `𝕎` := witt_vector p -- type as `\bbW` open mv_polynomial open function (uncurry) include hp variables (p) noncomputable theory /-! ### The `is_poly` predicate -/ lemma poly_eq_of_witt_polynomial_bind_eq' (f g : ℕ → mv_polynomial (idx × ℕ) ℤ) (h : ∀ n, bind₁ f (witt_polynomial p _ n) = bind₁ g (witt_polynomial p _ n)) : f = g := begin ext1 n, apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective, rw ← function.funext_iff at h, replace h := congr_arg (λ fam, bind₁ (mv_polynomial.map (int.cast_ring_hom ℚ) ∘ fam) (X_in_terms_of_W p ℚ n)) h, simpa only [function.comp, map_bind₁, map_witt_polynomial, ← bind₁_bind₁, bind₁_witt_polynomial_X_in_terms_of_W, bind₁_X_right] using h end lemma poly_eq_of_witt_polynomial_bind_eq (f g : ℕ → mv_polynomial ℕ ℤ) (h : ∀ n, bind₁ f (witt_polynomial p _ n) = bind₁ g (witt_polynomial p _ n)) : f = g := begin ext1 n, apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective, rw ← function.funext_iff at h, replace h := congr_arg (λ fam, bind₁ (mv_polynomial.map (int.cast_ring_hom ℚ) ∘ fam) (X_in_terms_of_W p ℚ n)) h, simpa only [function.comp, map_bind₁, map_witt_polynomial, ← bind₁_bind₁, bind₁_witt_polynomial_X_in_terms_of_W, bind₁_X_right] using h end omit hp -- Ideally, we would generalise this to n-ary functions -- But we don't have a good theory of n-ary compositions in mathlib /-- A function `f : Π R, 𝕎 R → 𝕎 R` that maps Witt vectors to Witt vectors over arbitrary base rings is said to be *polynomial* if there is a family of polynomials `φₙ` over `ℤ` such that the `n`th coefficient of `f x` is given by evaluating `φₙ` at the coefficients of `x`. See also `witt_vector.is_poly₂` for the binary variant. The `ghost_calc` tactic treats `is_poly` as a type class, and the `@[is_poly]` attribute derives certain specialized composition instances for declarations of type `is_poly f`. For the most part, users are not expected to treat `is_poly` as a class. -/ class is_poly (f : Π ⦃R⦄ [comm_ring R], witt_vector p R → 𝕎 R) : Prop := mk' :: (poly : ∃ φ : ℕ → mv_polynomial ℕ ℤ, ∀ ⦃R⦄ [comm_ring R] (x : 𝕎 R), by exactI (f x).coeff = λ n, aeval x.coeff (φ n)) /-- The identity function on Witt vectors is a polynomial function. -/ instance id_is_poly : is_poly p (λ _ _, id) := ⟨⟨X, by { introsI, simp only [aeval_X, id] }⟩⟩ instance id_is_poly_i' : is_poly p (λ _ _ a, a) := witt_vector.id_is_poly _ namespace is_poly instance : inhabited (is_poly p (λ _ _, id)) := ⟨witt_vector.id_is_poly p⟩ variables {p} include hp lemma ext {f g} (hf : is_poly p f) (hg : is_poly p g) (h : ∀ (R : Type u) [_Rcr : comm_ring R] (x : 𝕎 R) (n : ℕ), by exactI ghost_component n (f x) = ghost_component n (g x)) : ∀ (R : Type u) [_Rcr : comm_ring R] (x : 𝕎 R), by exactI f x = g x := begin unfreezingI { obtain ⟨φ, hf⟩ := hf, obtain ⟨ψ, hg⟩ := hg }, intros, ext n, rw [hf, hg, poly_eq_of_witt_polynomial_bind_eq p φ ψ], intro k, apply mv_polynomial.funext, intro x, simp only [hom_bind₁], specialize h (ulift ℤ) (mk p $ λ i, ⟨x i⟩) k, simp only [ghost_component_apply, aeval_eq_eval₂_hom] at h, apply (ulift.ring_equiv.{0 u}).symm.injective, simp only [map_eval₂_hom], convert h, all_goals { funext i, rw [← ring_equiv.coe_to_ring_hom], simp only [hf, hg, mv_polynomial.eval, map_eval₂_hom], apply eval₂_hom_congr (ring_hom.ext_int _ _) _ rfl, ext1, apply eval₂_hom_congr (ring_hom.ext_int _ _) _ rfl, simp only [coeff_mk], refl } end omit hp /-- The composition of polynomial functions is polynomial. -/ lemma comp {g f} (hg : is_poly p g) (hf : is_poly p f) : is_poly p (λ R _Rcr, @g R _Rcr ∘ @f R _Rcr) := begin unfreezingI { obtain ⟨φ, hf⟩ := hf, obtain ⟨ψ, hg⟩ := hg }, use (λ n, bind₁ φ (ψ n)), intros, simp only [aeval_bind₁, function.comp, hg, hf] end end is_poly /-- A binary function `f : Π R, 𝕎 R → 𝕎 R → 𝕎 R` on Witt vectors is said to be *polynomial* if there is a family of polynomials `φₙ` over `ℤ` such that the `n`th coefficient of `f x y` is given by evaluating `φₙ` at the coefficients of `x` and `y`. See also `witt_vector.is_poly` for the unary variant. The `ghost_calc` tactic treats `is_poly₂` as a type class, and the `@[is_poly]` attribute derives certain specialized composition instances for declarations of type `is_poly₂ f`. For the most part, users are not expected to treat `is_poly₂` as a class. -/ class is_poly₂ (f : Π ⦃R⦄ [comm_ring R], witt_vector p R → 𝕎 R → 𝕎 R) : Prop := mk' :: (poly : ∃ φ : ℕ → mv_polynomial (fin 2 × ℕ) ℤ, ∀ ⦃R⦄ [comm_ring R] (x y : 𝕎 R), by exactI (f x y).coeff = λ n, peval (φ n) ![x.coeff, y.coeff]) variable {p} /-- The composition of polynomial functions is polynomial. -/ lemma is_poly₂.comp {h f g} (hh : is_poly₂ p h) (hf : is_poly p f) (hg : is_poly p g) : is_poly₂ p (λ R _Rcr x y, by exactI h (f x) (g y)) := begin unfreezingI { obtain ⟨φ, hf⟩ := hf, obtain ⟨ψ, hg⟩ := hg, obtain ⟨χ, hh⟩ := hh }, refine ⟨⟨(λ n, bind₁ (uncurry $ ![λ k, rename (prod.mk (0 : fin 2)) (φ k), λ k, rename (prod.mk (1 : fin 2)) (ψ k)]) (χ n)), _⟩⟩, intros, funext n, simp only [peval, aeval_bind₁, function.comp, hh, hf, hg, uncurry], apply eval₂_hom_congr rfl _ rfl, ext ⟨i, n⟩, fin_cases i; simp only [aeval_eq_eval₂_hom, eval₂_hom_rename, function.comp, matrix.cons_val_zero, matrix.head_cons, matrix.cons_val_one], end /-- The composition of a polynomial function with a binary polynomial function is polynomial. -/ lemma is_poly.comp₂ {g f} (hg : is_poly p g) (hf : is_poly₂ p f) : is_poly₂ p (λ R _Rcr x y, by exactI g (f x y)) := begin unfreezingI { obtain ⟨φ, hf⟩ := hf, obtain ⟨ψ, hg⟩ := hg }, use (λ n, bind₁ φ (ψ n)), intros, simp only [peval, aeval_bind₁, function.comp, hg, hf] end /-- The diagonal `λ x, f x x` of a polynomial function `f` is polynomial. -/ lemma is_poly₂.diag {f} (hf : is_poly₂ p f) : is_poly p (λ R _Rcr x, by exactI f x x) := begin unfreezingI {obtain ⟨φ, hf⟩ := hf}, refine ⟨⟨λ n, bind₁ (uncurry ![X, X]) (φ n), _⟩⟩, intros, funext n, simp only [hf, peval, uncurry, aeval_bind₁], apply eval₂_hom_congr rfl _ rfl, ext ⟨i, k⟩, fin_cases i; simp only [matrix.head_cons, aeval_X, matrix.cons_val_zero, matrix.cons_val_one], end namespace tactic open tactic /-! ### The `@[is_poly]` attribute This attribute is used to derive specialized composition instances for `is_poly` and `is_poly₂` declarations. -/ /-- If `n` is the name of a lemma with opened type `∀ vars, is_poly p _`, `mk_poly_comp_lemmas n vars p` adds composition instances to the environment `n.comp_i` and `n.comp₂_i`. -/ meta def mk_poly_comp_lemmas (n : name) (vars : list expr) (p : expr) : tactic unit := do c ← mk_const n, let appd := vars.foldl expr.app c, tgt_bod ← to_expr ``(λ f [hf : is_poly %%p f], is_poly.comp %%appd hf) >>= replace_univ_metas_with_univ_params, tgt_bod ← lambdas vars tgt_bod, tgt_tp ← infer_type tgt_bod, let nm := n <.> "comp_i", add_decl $ mk_definition nm tgt_tp.collect_univ_params tgt_tp tgt_bod, set_attribute `instance nm, tgt_bod ← to_expr ``(λ f [hf : is_poly₂ %%p f], is_poly.comp₂ %%appd hf) >>= replace_univ_metas_with_univ_params, tgt_bod ← lambdas vars tgt_bod, tgt_tp ← infer_type tgt_bod, let nm := n <.> "comp₂_i", add_decl $ mk_definition nm tgt_tp.collect_univ_params tgt_tp tgt_bod, set_attribute `instance nm /-- If `n` is the name of a lemma with opened type `∀ vars, is_poly₂ p _`, `mk_poly₂_comp_lemmas n vars p` adds composition instances to the environment `n.comp₂_i` and `n.comp_diag`. -/ meta def mk_poly₂_comp_lemmas (n : name) (vars : list expr) (p : expr) : tactic unit := do c ← mk_const n, let appd := vars.foldl expr.app c, tgt_bod ← to_expr ``(λ {f g} [hf : is_poly %%p f] [hg : is_poly %%p g], is_poly₂.comp %%appd hf hg) >>= replace_univ_metas_with_univ_params, tgt_bod ← lambdas vars tgt_bod, tgt_tp ← infer_type tgt_bod >>= simp_lemmas.mk.dsimplify, let nm := n <.> "comp₂_i", add_decl $ mk_definition nm tgt_tp.collect_univ_params tgt_tp tgt_bod, set_attribute `instance nm, tgt_bod ← to_expr ``(λ {f g} [hf : is_poly %%p f] [hg : is_poly %%p g], (is_poly₂.comp %%appd hf hg).diag) >>= replace_univ_metas_with_univ_params, tgt_bod ← lambdas vars tgt_bod, tgt_tp ← infer_type tgt_bod >>= simp_lemmas.mk.dsimplify, let nm := n <.> "comp_diag", add_decl $ mk_definition nm tgt_tp.collect_univ_params tgt_tp tgt_bod, set_attribute `instance nm /-- The `after_set` function for `@[is_poly]`. Calls `mk_poly(₂)_comp_lemmas`. -/ meta def mk_comp_lemmas (n : name) : tactic unit := do d ← get_decl n, (vars, tp) ← open_pis d.type, match tp with | `(is_poly %%p _) := mk_poly_comp_lemmas n vars p | `(is_poly₂ %%p _) := mk_poly₂_comp_lemmas n vars p | _ := fail "@[is_poly] should only be applied to terms of type `is_poly _ _` or `is_poly₂ _ _`" end /-- `@[is_poly]` is applied to lemmas of the form `is_poly f φ` or `is_poly₂ f φ`. These lemmas should *not* be tagged as instances, and only atomic `is_poly` defs should be tagged: composition lemmas should not. Roughly speaking, lemmas that take `is_poly` proofs as arguments should not be tagged. Type class inference struggles with function composition, and the higher order unification problems involved in inferring `is_poly` proofs are complex. The standard style writing these proofs by hand doesn't work very well. Instead, we construct the type class hierarchy "under the hood", with limited forms of composition. Applying `@[is_poly]` to a lemma creates a number of instances. Roughly, if the tagged lemma is a proof of `is_poly f φ`, the instances added have the form ```lean ∀ g ψ, [is_poly g ψ] → is_poly (f ∘ g) _ ``` Since `f` is fixed in this instance, it restricts the HO unification needed when the instance is applied. Composition lemmas relating `is_poly` with `is_poly₂` are also added. `id_is_poly` is an atomic instance. The user-written lemmas are not instances. Users should be able to assemble `is_poly` proofs by hand "as normal" if the tactic fails. -/ @[user_attribute] meta def is_poly_attr : user_attribute := { name := `is_poly, descr := "Lemmas with this attribute describe the polynomial structure of functions", after_set := some $ λ n _ _, mk_comp_lemmas n } end tactic include hp /-! ### `is_poly` instances These are not declared as instances at the top level, but the `@[is_poly]` attribute adds instances based on each one. Users are expected to use the non-instance versions manually. -/ /-- The additive negation is a polynomial function on Witt vectors. -/ @[is_poly] lemma neg_is_poly : is_poly p (λ R _, by exactI @has_neg.neg (𝕎 R) _) := ⟨⟨λ n, rename prod.snd (witt_neg p n), begin introsI, funext n, rw [neg_coeff, aeval_eq_eval₂_hom, eval₂_hom_rename], apply eval₂_hom_congr rfl _ rfl, ext ⟨i, k⟩, fin_cases i, refl, end⟩⟩ section zero_one /- To avoid a theory of 0-ary functions (a.k.a. constants) we model them as constant unary functions. -/ /-- The function that is constantly zero on Witt vectors is a polynomial function. -/ instance zero_is_poly : is_poly p (λ _ _ _, by exactI 0) := ⟨⟨0, by { introsI, funext n, simp only [pi.zero_apply, alg_hom.map_zero, zero_coeff] }⟩⟩ @[simp] lemma bind₁_zero_witt_polynomial (n : ℕ) : bind₁ (0 : ℕ → mv_polynomial ℕ R) (witt_polynomial p R n) = 0 := by rw [← aeval_eq_bind₁, aeval_zero, constant_coeff_witt_polynomial, ring_hom.map_zero] omit hp /-- The coefficients of `1 : 𝕎 R` as polynomials. -/ def one_poly (n : ℕ) : mv_polynomial ℕ ℤ := if n = 0 then 1 else 0 include hp @[simp] lemma bind₁_one_poly_witt_polynomial (n : ℕ) : bind₁ one_poly (witt_polynomial p ℤ n) = 1 := begin rw [witt_polynomial_eq_sum_C_mul_X_pow, alg_hom.map_sum, finset.sum_eq_single 0], { simp only [one_poly, one_pow, one_mul, alg_hom.map_pow, C_1, pow_zero, bind₁_X_right, if_true, eq_self_iff_true], }, { intros i hi hi0, simp only [one_poly, if_neg hi0, zero_pow (pow_pos (nat.prime.pos hp) _), mul_zero, alg_hom.map_pow, bind₁_X_right, alg_hom.map_mul], }, { rw finset.mem_range, dec_trivial } end /-- The function that is constantly one on Witt vectors is a polynomial function. -/ instance one_is_poly : is_poly p (λ _ _ _, by exactI 1) := ⟨⟨one_poly, begin introsI, funext n, cases n, { simp only [one_poly, if_true, eq_self_iff_true, one_coeff_zero, alg_hom.map_one], }, { simp only [one_poly, nat.succ_pos', one_coeff_eq_of_pos, if_neg n.succ_ne_zero, alg_hom.map_zero] } end⟩⟩ end zero_one omit hp /-- Addition of Witt vectors is a polynomial function. -/ @[is_poly] lemma add_is_poly₂ [fact p.prime] : is_poly₂ p (λ _ _, by exactI (+)) := ⟨⟨witt_add p, by { introsI, dunfold witt_vector.has_add, simp [eval] }⟩⟩ /-- Multiplication of Witt vectors is a polynomial function. -/ @[is_poly] lemma mul_is_poly₂ [fact p.prime] : is_poly₂ p (λ _ _, by exactI (*)) := ⟨⟨witt_mul p, by { introsI, dunfold witt_vector.has_mul, simp [eval] }⟩⟩ include hp -- unfortunately this is not universe polymorphic, merely because `f` isn't lemma is_poly.map {f} (hf : is_poly p f) (g : R →+* S) (x : 𝕎 R) : map g (f x) = f (map g x) := begin -- this could be turned into a tactic “macro” (taking `hf` as parameter) -- so that applications do not have to worry about the universe issue -- see `is_poly₂.map` for a slightly more general proof strategy unfreezingI {obtain ⟨φ, hf⟩ := hf}, ext n, simp only [map_coeff, hf, map_aeval], apply eval₂_hom_congr (ring_hom.ext_int _ _) _ rfl, simp only [map_coeff] end namespace is_poly₂ omit hp instance [fact p.prime] : inhabited (is_poly₂ p _) := ⟨add_is_poly₂⟩ variables {p} /-- The composition of a binary polynomial function with a unary polynomial function in the first argument is polynomial. -/ lemma comp_left {g f} (hg : is_poly₂ p g) (hf : is_poly p f) : is_poly₂ p (λ R _Rcr x y, by exactI g (f x) y) := hg.comp hf (witt_vector.id_is_poly _) /-- The composition of a binary polynomial function with a unary polynomial function in the second argument is polynomial. -/ lemma comp_right {g f} (hg : is_poly₂ p g) (hf : is_poly p f) : is_poly₂ p (λ R _Rcr x y, by exactI g x (f y)) := hg.comp (witt_vector.id_is_poly p) hf include hp lemma ext {f g} (hf : is_poly₂ p f) (hg : is_poly₂ p g) (h : ∀ (R : Type u) [_Rcr : comm_ring R] (x y : 𝕎 R) (n : ℕ), by exactI ghost_component n (f x y) = ghost_component n (g x y)) : ∀ (R) [_Rcr : comm_ring R] (x y : 𝕎 R), by exactI f x y = g x y := begin unfreezingI { obtain ⟨φ, hf⟩ := hf, obtain ⟨ψ, hg⟩ := hg }, intros, ext n, rw [hf, hg, poly_eq_of_witt_polynomial_bind_eq' p φ ψ], clear x y, intro k, apply mv_polynomial.funext, intro x, simp only [hom_bind₁], specialize h (ulift ℤ) (mk p $ λ i, ⟨x (0, i)⟩) (mk p $ λ i, ⟨x (1, i)⟩) k, simp only [ghost_component_apply, aeval_eq_eval₂_hom] at h, apply (ulift.ring_equiv.{0 u}).symm.injective, simp only [map_eval₂_hom], convert h; clear h, all_goals { funext i, rw [← ring_equiv.coe_to_ring_hom], simp only [hf, hg, mv_polynomial.eval, map_eval₂_hom], apply eval₂_hom_congr (ring_hom.ext_int _ _) _ rfl, ext1, apply eval₂_hom_congr (ring_hom.ext_int _ _) _ rfl, ext ⟨b, _⟩, fin_cases b; simp only [coeff_mk, uncurry]; refl } end -- unfortunately this is not universe polymorphic, merely because `f` isn't lemma map {f} (hf : is_poly₂ p f) (g : R →+* S) (x y : 𝕎 R) : map g (f x y) = f (map g x) (map g y) := begin -- this could be turned into a tactic “macro” (taking `hf` as parameter) -- so that applications do not have to worry about the universe issue unfreezingI {obtain ⟨φ, hf⟩ := hf}, ext n, simp only [map_coeff, hf, map_aeval, peval, uncurry], apply eval₂_hom_congr (ring_hom.ext_int _ _) _ rfl, try { ext ⟨i, k⟩, fin_cases i }, all_goals { simp only [map_coeff, matrix.cons_val_zero, matrix.head_cons, matrix.cons_val_one] }, end end is_poly₂ attribute [ghost_simps] alg_hom.map_zero alg_hom.map_one alg_hom.map_add alg_hom.map_mul alg_hom.map_sub alg_hom.map_neg alg_hom.id_apply alg_hom.map_nat_cast ring_hom.map_zero ring_hom.map_one ring_hom.map_mul ring_hom.map_add ring_hom.map_sub ring_hom.map_neg ring_hom.id_apply ring_hom.map_nat_cast mul_add add_mul add_zero zero_add mul_one one_mul mul_zero zero_mul nat.succ_ne_zero nat.add_sub_cancel nat.succ_eq_add_one if_true eq_self_iff_true if_false forall_true_iff forall_2_true_iff forall_3_true_iff end witt_vector
21d0c58d44577dfe0bbf57f6a7fb730545ca60ad
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/src/Init/Control/Lawful.lean
d74e421dcb8e1502be6886e6be15a8eb834ded19
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,357
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich, Leonardo de Moura -/ prelude import Init.SimpLemmas import Init.Control.Except import Init.Control.StateRef open Function @[simp] theorem monadLift_self [Monad m] (x : m α) : monadLift x = x := rfl class LawfulFunctor (f : Type u → Type v) [Functor f] : Prop where map_const : (Functor.mapConst : α → f β → f α) = Functor.map ∘ const β id_map (x : f α) : id <$> x = x comp_map (g : α → β) (h : β → γ) (x : f α) : (h ∘ g) <$> x = h <$> g <$> x export LawfulFunctor (map_const id_map comp_map) attribute [simp] id_map @[simp] theorem id_map' [Functor m] [LawfulFunctor m] (x : m α) : (fun a => a) <$> x = x := id_map x class LawfulApplicative (f : Type u → Type v) [Applicative f] extends LawfulFunctor f : Prop where seqLeft_eq (x : f α) (y : f β) : x <* y = const β <$> x <*> y seqRight_eq (x : f α) (y : f β) : x *> y = const α id <$> x <*> y pure_seq (g : α → β) (x : f α) : pure g <*> x = g <$> x map_pure (g : α → β) (x : α) : g <$> (pure x : f α) = pure (g x) seq_pure (g : f (α → β)) (x : α) : g <*> pure x = (fun h => h x) <$> g seq_assoc (x : f α) (g : f (α → β)) (h : f (β → γ)) : h <*> (g <*> x) = ((. ∘ .) <$> h) <*> g <*> x comp_map g h x := by repeat rw [← pure_seq] simp [seq_assoc, map_pure, seq_pure] export LawfulApplicative (seqLeft_eq seqRight_eq pure_seq map_pure seq_pure seq_assoc) attribute [simp] map_pure seq_pure @[simp] theorem pure_id_seq [Applicative f] [LawfulApplicative f] (x : f α) : pure id <*> x = x := by simp [pure_seq] class LawfulMonad (m : Type u → Type v) [Monad m] extends LawfulApplicative m : Prop where bind_pure_comp (f : α → β) (x : m α) : x >>= pure ∘ f = f <$> x bind_map (f : m (α → (β : Type u))) (x : m α) : f >>= (. <$> x) = f <*> x pure_bind (x : α) (f : α → m β) : pure x >>= f = f x bind_assoc (x : m α) (f : α → m β) (g : β → m γ) : x >>= f >>= g = x >>= fun x => f x >>= g map_pure g x := by rw [← bind_pure_comp, pure_bind] seq_pure g x := by rw [← bind_map]; simp [map_pure, bind_pure_comp] seq_assoc x g h := by -- TODO: support for applying `symm` at `simp` arguments let bind_pure_comp_symm {α β : Type u} (f : α → β) (x : m α) : f <$> x = x >>= pure ∘ f := by rw [bind_pure_comp] let bind_map_symm {α β : Type u} (f : m (α → (β : Type u))) (x : m α) : f <*> x = f >>= (. <$> x) := by rw [bind_map] simp[bind_pure_comp_symm, bind_map_symm, bind_assoc, pure_bind] export LawfulMonad (bind_pure_comp bind_map pure_bind bind_assoc) attribute [simp] pure_bind bind_assoc @[simp] theorem bind_pure [Monad m] [LawfulMonad m] (x : m α) : x >>= pure = x := by show x >>= pure ∘ id = x rw [bind_pure_comp, id_map] theorem map_eq_pure_bind [Monad m] [LawfulMonad m] (f : α → β) (x : m α) : f <$> x = x >>= fun a => pure (f a) := by rw [← bind_pure_comp] theorem seq_eq_bind_map {α β : Type u} [Monad m] [LawfulMonad m] (f : m (α → β)) (x : m α) : f <*> x = f >>= (. <$> x) := by rw [← bind_map] theorem bind_congr [Bind m] {x : m α} {f g : α → m β} (h : ∀ a, f a = g a) : x >>= f = x >>= g := by simp [funext h] @[simp] theorem bind_pure_unit [Monad m] [LawfulMonad m] {x : m PUnit} : (x >>= fun _ => pure ⟨⟩) = x := by have (x >>= fun _ => pure ⟨⟩) = (x >>= pure) by apply bind_congr; intro u cases u; simp rw [bind_pure] at this assumption theorem map_congr [Functor m] {x : m α} {f g : α → β} (h : ∀ a, f a = g a) : (f <$> x : m β) = g <$> x := by simp [funext h] theorem seq_eq_bind {α β : Type u} [Monad m] [LawfulMonad m] (mf : m (α → β)) (x : m α) : mf <*> x = mf >>= fun f => f <$> x := by rw [bind_map] theorem seqRight_eq_bind [Monad m] [LawfulMonad m] (x : m α) (y : m β) : x *> y = x >>= fun _ => y := by rw [seqRight_eq]; simp [map_eq_pure_bind, seq_eq_bind_map] theorem seqLeft_eq_bind [Monad m] [LawfulMonad m] (x : m α) (y : m β) : x <* y = x >>= fun a => y >>= fun _ => pure a := by rw [seqLeft_eq]; simp [map_eq_pure_bind, seq_eq_bind_map] /- Id -/ namespace Id @[simp] theorem map_eq (x : Id α) (f : α → β) : f <$> x = f x := rfl @[simp] theorem bind_eq (x : Id α) (f : α → id β) : x >>= f = f x := rfl @[simp] theorem pure_eq (a : α) : (pure a : Id α) = a := rfl instance : LawfulMonad Id := by refine! { .. } <;> intros <;> rfl end Id /- ExceptT -/ namespace ExceptT theorem ext [Monad m] {x y : ExceptT ε m α} (h : x.run = y.run) : x = y := by simp [run] at h assumption @[simp] theorem run_pure [Monad m] : run (pure x : ExceptT ε m α) = pure (Except.ok x) := rfl @[simp] theorem run_lift [Monad m] : run (ExceptT.lift x : ExceptT ε m α) = Except.ok <$> x := rfl @[simp] theorem run_throw [Monad m] : run (throw e : ExceptT ε m β) = pure (Except.error e) := rfl @[simp] theorem run_bind_lift [Monad m] [LawfulMonad m] (x : m α) (f : α → ExceptT ε m β) : run (ExceptT.lift x >>= f : ExceptT ε m β) = x >>= fun a => run (f a) := by simp[ExceptT.run, ExceptT.lift, bind, ExceptT.bind, ExceptT.mk, ExceptT.bindCont, map_eq_pure_bind] @[simp] theorem bind_throw [Monad m] [LawfulMonad m] (f : α → ExceptT ε m β) : (throw e >>= f) = throw e := by simp [throw, throwThe, MonadExceptOf.throw, bind, ExceptT.bind, ExceptT.bindCont, ExceptT.mk] theorem run_bind [Monad m] (x : ExceptT ε m α) : run (x >>= f : ExceptT ε m β) = run x >>= fun | Except.ok x => run (f x) | Except.error e => pure (Except.error e) := rfl @[simp] theorem lift_pure [Monad m] [LawfulMonad m] (a : α) : ExceptT.lift (pure a) = (pure a : ExceptT ε m α) := by simp [ExceptT.lift, pure, ExceptT.pure] @[simp] theorem run_map [Monad m] [LawfulMonad m] (f : α → β) (x : ExceptT ε m α) : (f <$> x).run = Except.map f <$> x.run := by simp [Functor.map, ExceptT.map, map_eq_pure_bind] apply bind_congr intro a; cases a <;> simp [Except.map] protected theorem seq_eq {α β ε : Type u} [Monad m] (mf : ExceptT ε m (α → β)) (x : ExceptT ε m α) : mf <*> x = mf >>= fun f => f <$> x := rfl protected theorem bind_pure_comp [Monad m] [LawfulMonad m] (f : α → β) (x : ExceptT ε m α) : x >>= pure ∘ f = f <$> x := by intros; rfl protected theorem seqLeft_eq {α β ε : Type u} {m : Type u → Type v} [Monad m] [LawfulMonad m] (x : ExceptT ε m α) (y : ExceptT ε m β) : x <* y = const β <$> x <*> y := by show (x >>= fun a => y >>= fun _ => pure a) = (const (α := α) β <$> x) >>= fun f => f <$> y rw [← ExceptT.bind_pure_comp] apply ext simp [run_bind] apply bind_congr intro | Except.error _ => simp | Except.ok _ => simp [map_eq_pure_bind]; apply bind_congr; intro b; cases b <;> simp [comp, Except.map, const] protected theorem seqRight_eq [Monad m] [LawfulMonad m] (x : ExceptT ε m α) (y : ExceptT ε m β) : x *> y = const α id <$> x <*> y := by show (x >>= fun _ => y) = (const α id <$> x) >>= fun f => f <$> y rw [← ExceptT.bind_pure_comp] apply ext simp [run_bind] apply bind_congr intro a; cases a <;> simp instance [Monad m] [LawfulMonad m] : LawfulMonad (ExceptT ε m) where id_map := by intros; apply ext; simp map_const := by intros; rfl seqLeft_eq := ExceptT.seqLeft_eq seqRight_eq := ExceptT.seqRight_eq pure_seq := by intros; apply ext; simp [ExceptT.seq_eq, run_bind] bind_pure_comp := ExceptT.bind_pure_comp bind_map := by intros; rfl pure_bind := by intros; apply ext; simp [run_bind] bind_assoc := by intros; apply ext; simp [run_bind]; apply bind_congr; intro a; cases a <;> simp end ExceptT /- ReaderT -/ namespace ReaderT theorem ext [Monad m] {x y : ReaderT ρ m α} (h : ∀ ctx, x.run ctx = y.run ctx) : x = y := by simp [run] at h exact funext h @[simp] theorem run_pure [Monad m] (a : α) (ctx : ρ) : (pure a : ReaderT ρ m α).run ctx = pure a := rfl @[simp] theorem run_bind [Monad m] (x : ReaderT ρ m α) (f : α → ReaderT ρ m β) (ctx : ρ) : (x >>= f).run ctx = x.run ctx >>= λ a => (f a).run ctx := rfl @[simp] theorem run_map [Monad m] (f : α → β) (x : ReaderT ρ m α) (ctx : ρ) : (f <$> x).run ctx = f <$> x.run ctx := rfl @[simp] theorem run_monadLift [MonadLiftT n m] (x : n α) (ctx : ρ) : (monadLift x : ReaderT ρ m α).run ctx = (monadLift x : m α) := rfl @[simp] theorem run_monadMap [Monad m] [MonadFunctor n m] (f : {β : Type u} → n β → n β) (x : ReaderT ρ m α) (ctx : ρ) : (monadMap @f x : ReaderT ρ m α).run ctx = monadMap @f (x.run ctx) := rfl @[simp] theorem run_read [Monad m] (ctx : ρ) : (ReaderT.read : ReaderT ρ m ρ).run ctx = pure ctx := rfl @[simp] theorem run_seq {α β : Type u} [Monad m] [LawfulMonad m] (f : ReaderT ρ m (α → β)) (x : ReaderT ρ m α) (ctx : ρ) : (f <*> x).run ctx = (f.run ctx <*> x.run ctx) := by rw [seq_eq_bind (m := m)]; rfl @[simp] theorem run_seqRight [Monad m] [LawfulMonad m] (x : ReaderT ρ m α) (y : ReaderT ρ m β) (ctx : ρ) : (x *> y).run ctx = (x.run ctx *> y.run ctx) := by rw [seqRight_eq_bind (m := m)]; rfl @[simp] theorem run_seqLeft [Monad m] [LawfulMonad m] (x : ReaderT ρ m α) (y : ReaderT ρ m β) (ctx : ρ) : (x <* y).run ctx = (x.run ctx <* y.run ctx) := by rw [seqLeft_eq_bind (m := m)]; rfl instance [Monad m] [LawfulMonad m] : LawfulMonad (ReaderT ρ m) where id_map := by intros; apply ext; intros; simp map_const := by intros; rfl seqLeft_eq := by intros; apply ext; intros; simp; apply LawfulApplicative.seqLeft_eq seqRight_eq := by intros; apply ext; intros; simp; apply LawfulApplicative.seqRight_eq pure_seq := by intros; apply ext; intros; simp; apply LawfulApplicative.pure_seq bind_pure_comp := by intros; apply ext; intros; simp; apply LawfulMonad.bind_pure_comp bind_map := by intros; rfl pure_bind := by intros; apply ext; intros; simp bind_assoc := by intros; apply ext; intros; simp end ReaderT /- StateRefT -/ instance [Monad m] [LawfulMonad m] : LawfulMonad (StateRefT' ω σ m) := inferInstanceAs (LawfulMonad (ReaderT (ST.Ref ω σ) m)) /- StateT -/ namespace StateT theorem ext {x y : StateT σ m α} (h : ∀ s, x.run s = y.run s) : x = y := funext h @[simp] theorem run'_eq [Monad m] (x : StateT σ m α) (s : σ) : run' x s = (·.1) <$> run x s := rfl @[simp] theorem run_pure [Monad m] (a : α) (s : σ) : (pure a : StateT σ m α).run s = pure (a, s) := rfl @[simp] theorem run_bind [Monad m] (x : StateT σ m α) (f : α → StateT σ m β) (s : σ) : (x >>= f).run s = x.run s >>= λ p => (f p.1).run p.2 := by simp [bind, StateT.bind, run] apply bind_congr intro p; cases p; rfl @[simp] theorem run_map {α β σ : Type u} [Monad m] [LawfulMonad m] (f : α → β) (x : StateT σ m α) (s : σ) : (f <$> x).run s = (fun (p : α × σ) => (f p.1, p.2)) <$> x.run s := by simp [Functor.map, StateT.map, run, map_eq_pure_bind] apply bind_congr intro p; cases p; rfl @[simp] theorem run_get [Monad m] (s : σ) : (get : StateT σ m σ).run s = pure (s, s) := rfl @[simp] theorem run_set [Monad m] (s s' : σ) : (set s' : StateT σ m PUnit).run s = pure (⟨⟩, s') := rfl @[simp] theorem run_modify [Monad m] (f : σ → σ) (s : σ) : (modify f : StateT σ m PUnit).run s = pure (⟨⟩, f s) := rfl @[simp] theorem run_modifyGet [Monad m] (f : σ → α × σ) (s : σ) : (modifyGet f : StateT σ m α).run s = pure ((f s).1, (f s).2) := by simp [modifyGet, MonadStateOf.modifyGet, StateT.modifyGet, run]; cases f s <;> rfl @[simp] theorem run_lift {α σ : Type u} [Monad m] (x : m α) (s : σ) : (StateT.lift x : StateT σ m α).run s = x >>= fun a => pure (a, s) := rfl @[simp] theorem run_bind_lift {α σ : Type u} [Monad m] [LawfulMonad m] (x : m α) (f : α → StateT σ m β) (s : σ) : (StateT.lift x >>= f).run s = x >>= fun a => (f a).run s := by simp [StateT.lift, StateT.run, bind, StateT.bind] @[simp] theorem run_monadLift {α σ : Type u} [Monad m] [MonadLiftT n m] (x : n α) (s : σ) : (monadLift x : StateT σ m α).run s = (monadLift x : m α) >>= fun a => pure (a, s) := rfl @[simp] theorem run_monadMap [Monad m] [MonadFunctor n m] (f : {β : Type u} → n β → n β) (x : StateT σ m α) (s : σ) : (monadMap @f x : StateT σ m α).run s = monadMap @f (x.run s) := rfl @[simp] theorem run_seq {α β σ : Type u} [Monad m] [LawfulMonad m] (f : StateT σ m (α → β)) (x : StateT σ m α) (s : σ) : (f <*> x).run s = (f.run s >>= fun fs => (fun (p : α × σ) => (fs.1 p.1, p.2)) <$> x.run fs.2) := by show (f >>= fun g => g <$> x).run s = _ simp @[simp] theorem run_seqRight [Monad m] [LawfulMonad m] (x : StateT σ m α) (y : StateT σ m β) (s : σ) : (x *> y).run s = (x.run s >>= fun p => y.run p.2) := by show (x >>= fun _ => y).run s = _ simp @[simp] theorem run_seqLeft {α β σ : Type u} [Monad m] [LawfulMonad m] (x : StateT σ m α) (y : StateT σ m β) (s : σ) : (x <* y).run s = (x.run s >>= fun p => y.run p.2 >>= fun p' => pure (p.1, p'.2)) := by show (x >>= fun a => y >>= fun _ => pure a).run s = _ simp theorem seqRight_eq [Monad m] [LawfulMonad m] (x : StateT σ m α) (y : StateT σ m β) : x *> y = const α id <$> x <*> y := by apply ext; intro s simp [map_eq_pure_bind] apply bind_congr; intro p; cases p simp [Prod.ext] theorem seqLeft_eq [Monad m] [LawfulMonad m] (x : StateT σ m α) (y : StateT σ m β) : x <* y = const β <$> x <*> y := by apply ext; intro s simp [map_eq_pure_bind] instance [Monad m] [LawfulMonad m] : LawfulMonad (StateT σ m) where id_map := by intros; apply ext; intros; simp[Prod.ext] map_const := by intros; rfl seqLeft_eq := seqLeft_eq seqRight_eq := seqRight_eq pure_seq := by intros; apply ext; intros; simp bind_pure_comp := by intros; apply ext; intros; simp; apply LawfulMonad.bind_pure_comp bind_map := by intros; rfl pure_bind := by intros; apply ext; intros; simp bind_assoc := by intros; apply ext; intros; simp end StateT
a302b36fcce74add959ca37d220d139e6a41a655
958488bc7f3c2044206e0358e56d7690b6ae696c
/lean/tutorials/tutorial7.lean
b04fddbe18298cc64e3a1c8d474e31fe02ce3398
[]
no_license
possientis/Prog
a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4
d4b3debc37610a88e0dac3ac5914903604fd1d1f
refs/heads/master
1,692,263,717,723
1,691,757,179,000
1,691,757,179,000
40,361,602
3
0
null
1,679,896,438,000
1,438,953,859,000
Coq
UTF-8
Lean
false
false
1,972
lean
import tuto_lib import data.int.parity example : false → 0 = 1 := begin intros H₁, exfalso, assumption end example : ∀ (x : ℝ), ¬ x < x := begin intros x H₁, rw lt_iff_le_and_ne at H₁, cases H₁ with H₁ H₂, clear H₁, change x = x → false at H₂, apply H₂, refl end open int example : ∀ (n : ℤ), even n → ¬ even n → 0 = 1 := begin intros n H₁ H₂, exfalso, apply H₂, assumption end example : ∀ (P Q:Prop), (P ∨ Q) → ¬(P ∧ Q) → (¬ P ↔ Q) := begin intros P Q H₁ H₂, split; intros H₃, { cases H₁ with H₁ H₄, { exfalso, apply H₃, assumption}, { assumption }}, { intros H₄, apply H₂, split; assumption } end example : ∀ (u : ℕ → ℝ) (l l' : ℝ), seq_limit u l → seq_limit u l' → l = l' := begin intros u l l' H₁ H₂, by_contradiction H₃, change l ≠ l' at H₃, -- superfluous have H₄ : |l - l'| > 0, {apply abs_pos.mpr, apply sub_ne_zero_of_ne, assumption }, specialize H₁ (|l - l'|/4) (by linarith), cases H₁ with N₁ H₁, specialize H₂ (|l - l'|/4) (by linarith), cases H₂ with N₂ H₂, let N := max N₁ N₂, specialize H₁ N (by apply le_max_left), specialize H₂ N (by apply le_max_right), have H₅ : |l - l'| < |l - l'|, calc |l - l'| = |(l - u N) + (u N - l')| : by ring ... ≤ |l - u N| + |u N - l'| : by apply abs_add ... = |u N - l| + |u N - l'| : by rw abs_sub ... ≤ |l - l'|/4 + |u N - l'| : by linarith ... ≤ |l - l'|/4 + |l - l'|/4 : by linarith ... = |l - l'|/2 : by ring ... < |l -l'| : by linarith, linarith end example : ∀ (P Q : Prop), (¬Q → ¬P) → P → Q := begin intros P Q H₁ H₂, by_contradiction H₃, apply H₁; assumption end example : ∀ (P Q : Prop), (¬Q → ¬P) → P → Q := begin intros P Q H₁, contrapose, assumption end
4d1caf5b5c48331629e2ccf97878ac9498230074
94637389e03c919023691dcd05bd4411b1034aa5
/src/inClassNotes/00_whatIsALanguage.lean
b655a62e684f2bc0f08a09de3c4bd56450a72639
[]
no_license
kevinsullivan/complogic-s21
7c4eef2105abad899e46502270d9829d913e8afc
99039501b770248c8ceb39890be5dfe129dc1082
refs/heads/master
1,682,985,669,944
1,621,126,241,000
1,621,126,241,000
335,706,272
0
38
null
1,618,325,669,000
1,612,374,118,000
Lean
UTF-8
Lean
false
false
1,516
lean
inductive Syntax : Type | I | II | III | IV | V inductive Semantics : Type | one | two | three | four | five #reduce Semantics.one -- Qualified access to that namespace open Semantics -- Open namespaces open Syntax #reduce one -- Now defined in current namespace /- Here's an informal definition of our desired semantics. I --> one II --> two III --> three IV --> four V --> five -/ -- We can formalize semantics as a function /- A little bit of Lean -/ -- Literal expressions -- Variable expressions -- Application expressions #reduce 1 -- literal def x := 1 -- variable #reduce x def my_id : nat → nat := (λ n, n) -- lambda expression, literal -- Ident Type Value -- type inference in use #reduce (my_id 1) #reduce (my_id 4) -- #reduce (my_id "Hello, Lean!") def my_id' : ℕ → ℕ -- by cases | n := n def my_id'' (n : nat) : nat := -- C style syntax n #reduce my_id #reduce my_id'' -- End of "A little bit of Lean" -- Another aside def my_add (n m : ℕ) := nat.add n m -- n + m #reduce my_add 2 3 def my_add' : ℕ → (ℕ → ℕ) := λ n, λ m, n + m def k := my_add' 5 #reduce k 6 /- Our semantics! -/ def my_eval : Syntax → Semantics | I := one | II := two | III := three | IV := four | V := five #reduce my_eval II -- computational: def c_semantics_fun : input -> c-program -> output -- logical: def c_semantics_pred : input -> c-program -> output -> Prop
d753188d8cf4f106948e205a4b4e6edc8cb735b4
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/data/dfinsupp/order.lean
e5f390ebc8a6d48e3ca45338141cdbbf6ec496cc
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
7,937
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.dfinsupp.basic /-! # Pointwise order on finitely supported dependent functions This file lifts order structures on the `α i` to `Π₀ i, α i`. ## Main declarations * `dfinsupp.order_embedding_to_fun`: The order embedding from finitely supported dependent functions to functions. ## TODO Add `is_well_order (Π₀ i, α i) (<)`. -/ open_locale big_operators open finset variables {ι : Type*} {α : ι → Type*} namespace dfinsupp /-! ### Order structures -/ section has_zero variables (α) [Π i, has_zero (α i)] section has_le variables [Π i, has_le (α i)] instance : has_le (Π₀ i, α i) := ⟨λ f g, ∀ i, f i ≤ g i⟩ variables {α} lemma le_def {f g : Π₀ i, α i} : f ≤ g ↔ ∀ i, f i ≤ g i := iff.rfl /-- The order on `dfinsupp`s over a partial order embeds into the order on functions -/ def order_embedding_to_fun : (Π₀ i, α i) ↪o Π i, α i := { to_fun := coe_fn, inj' := coe_fn_injective, map_rel_iff' := λ a b, (@le_def _ _ _ _ a b).symm } @[simp] lemma order_embedding_to_fun_apply {f : Π₀ i, α i} {i : ι} : order_embedding_to_fun f i = f i := rfl end has_le section preorder variables [Π i, preorder (α i)] instance : preorder (Π₀ i, α i) := { le_refl := λ f i, le_rfl, le_trans := λ f g h hfg hgh i, (hfg i).trans (hgh i), .. dfinsupp.has_le α } lemma coe_fn_mono : monotone (coe_fn : (Π₀ i, α i) → Π i, α i) := λ f g, le_def.1 end preorder instance [Π i, partial_order (α i)] : partial_order (Π₀ i, α i) := { le_antisymm := λ f g hfg hgf, ext $ λ i, (hfg i).antisymm (hgf i), .. dfinsupp.preorder α} instance [Π i, semilattice_inf (α i)] : semilattice_inf (Π₀ i, α i) := { inf := zip_with (λ _, (⊓)) (λ _, inf_idem), inf_le_left := λ f g i, by { rw zip_with_apply, exact inf_le_left }, inf_le_right := λ f g i, by { rw zip_with_apply, exact inf_le_right }, le_inf := λ f g h hf hg i, by { rw zip_with_apply, exact le_inf (hf i) (hg i) }, ..dfinsupp.partial_order α } @[simp] lemma inf_apply [Π i, semilattice_inf (α i)] (f g : Π₀ i, α i) (i : ι) : (f ⊓ g) i = f i ⊓ g i := zip_with_apply _ _ _ _ _ instance [Π i, semilattice_sup (α i)] : semilattice_sup (Π₀ i, α i) := { sup := zip_with (λ _, (⊔)) (λ _, sup_idem), le_sup_left := λ f g i, by { rw zip_with_apply, exact le_sup_left }, le_sup_right := λ f g i, by { rw zip_with_apply, exact le_sup_right }, sup_le := λ f g h hf hg i, by { rw zip_with_apply, exact sup_le (hf i) (hg i) }, ..dfinsupp.partial_order α } @[simp] lemma sup_apply [Π i, semilattice_sup (α i)] (f g : Π₀ i, α i) (i : ι) : (f ⊔ g) i = f i ⊔ g i := zip_with_apply _ _ _ _ _ instance lattice [Π i, lattice (α i)] : lattice (Π₀ i, α i) := { .. dfinsupp.semilattice_inf α, .. dfinsupp.semilattice_sup α } end has_zero /-! ### Algebraic order structures -/ instance (α : ι → Type*) [Π i, ordered_add_comm_monoid (α i)] : ordered_add_comm_monoid (Π₀ i, α i) := { add_le_add_left := λ a b h c i, by { rw [add_apply, add_apply], exact add_le_add_left (h i) (c i) }, .. dfinsupp.add_comm_monoid, .. dfinsupp.partial_order α } instance (α : ι → Type*) [Π i, ordered_cancel_add_comm_monoid (α i)] : ordered_cancel_add_comm_monoid (Π₀ i, α i) := { le_of_add_le_add_left := λ f g h H i, begin specialize H i, rw [add_apply, add_apply] at H, exact le_of_add_le_add_left H, end, add_left_cancel := λ f g h H, ext $ λ i, begin refine add_left_cancel _, exact f i, rw [←add_apply, ←add_apply, H], end, .. dfinsupp.ordered_add_comm_monoid α } instance [Π i, ordered_add_comm_monoid (α i)] [Π i, contravariant_class (α i) (α i) (+) (≤)] : contravariant_class (Π₀ i, α i) (Π₀ i, α i) (+) (≤) := ⟨λ f g h H i, by { specialize H i, rw [add_apply, add_apply] at H, exact le_of_add_le_add_left H }⟩ section canonically_ordered_add_monoid variables (α) [Π i, canonically_ordered_add_monoid (α i)] instance : order_bot (Π₀ i, α i) := { bot := 0, bot_le := by simp only [le_def, coe_zero, pi.zero_apply, implies_true_iff, zero_le] } variables {α} protected lemma bot_eq_zero : (⊥ : Π₀ i, α i) = 0 := rfl @[simp] lemma add_eq_zero_iff (f g : Π₀ i, α i) : f + g = 0 ↔ f = 0 ∧ g = 0 := by simp [ext_iff, forall_and_distrib] section le variables [decidable_eq ι] [Π i (x : α i), decidable (x ≠ 0)] {f g : Π₀ i, α i} {s : finset ι} lemma le_iff' (hf : f.support ⊆ s) : f ≤ g ↔ ∀ i ∈ s, f i ≤ g i := ⟨λ h s hs, h s, λ h s, if H : s ∈ f.support then h s (hf H) else (not_mem_support_iff.1 H).symm ▸ zero_le (g s)⟩ lemma le_iff : f ≤ g ↔ ∀ i ∈ f.support, f i ≤ g i := le_iff' $ subset.refl _ variables (α) instance decidable_le [Π i, decidable_rel (@has_le.le (α i) _)] : decidable_rel (@has_le.le (Π₀ i, α i) _) := λ f g, decidable_of_iff _ le_iff.symm variables {α} @[simp] lemma single_le_iff {i : ι} {a : α i} : single i a ≤ f ↔ a ≤ f i := (le_iff' support_single_subset).trans $ by simp end le variables (α) [Π i, has_sub (α i)] [Π i, has_ordered_sub (α i)] {f g : Π₀ i, α i} {i : ι} {a b : α i} /-- This is called `tsub` for truncated subtraction, to distinguish it with subtraction in an additive group. -/ instance tsub : has_sub (Π₀ i, α i) := ⟨zip_with (λ i m n, m - n) (λ i, tsub_self 0)⟩ variables {α} lemma tsub_apply (f g : Π₀ i, α i) (i : ι) : (f - g) i = f i - g i := zip_with_apply _ _ _ _ _ @[simp] lemma coe_tsub (f g : Π₀ i, α i) : ⇑(f - g) = f - g := by { ext i, exact tsub_apply f g i } variables (α) instance : has_ordered_sub (Π₀ i, α i) := ⟨λ n m k, forall_congr $ λ i, by { rw [add_apply, tsub_apply], exact tsub_le_iff_right }⟩ instance : canonically_ordered_add_monoid (Π₀ i, α i) := { exists_add_of_le := λ f g h, ⟨g - f, by { ext i, rw [add_apply, tsub_apply], exact (add_tsub_cancel_of_le $ h i).symm }⟩, le_self_add := λ f g i, by { rw add_apply, exact le_self_add }, .. dfinsupp.order_bot α, .. dfinsupp.ordered_add_comm_monoid α } variables {α} [decidable_eq ι] @[simp] lemma single_tsub : single i (a - b) = single i a - single i b := begin ext j, obtain rfl | h := eq_or_ne i j, { rw [tsub_apply, single_eq_same, single_eq_same, single_eq_same] }, { rw [tsub_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h, tsub_self] } end variables [Π i (x : α i), decidable (x ≠ 0)] lemma support_tsub : (f - g).support ⊆ f.support := by simp only [subset_iff, tsub_eq_zero_iff_le, mem_support_iff, ne.def, coe_tsub, pi.sub_apply, not_imp_not, zero_le, implies_true_iff] {contextual := tt} lemma subset_support_tsub : f.support \ g.support ⊆ (f - g).support := by simp [subset_iff] {contextual := tt} end canonically_ordered_add_monoid section canonically_linear_ordered_add_monoid variables [Π i, canonically_linear_ordered_add_monoid (α i)] [decidable_eq ι] {f g : Π₀ i, α i} @[simp] lemma support_inf : (f ⊓ g).support = f.support ∩ g.support := begin ext, simp only [inf_apply, mem_support_iff, ne.def, finset.mem_union, finset.mem_filter, finset.mem_inter], simp only [inf_eq_min, ←nonpos_iff_eq_zero, min_le_iff, not_or_distrib], end @[simp] lemma support_sup : (f ⊔ g).support = f.support ∪ g.support := begin ext, simp only [finset.mem_union, mem_support_iff, sup_apply, ne.def, ←bot_eq_zero], rw [_root_.sup_eq_bot_iff, not_and_distrib], end lemma disjoint_iff : disjoint f g ↔ disjoint f.support g.support := begin rw [disjoint_iff, disjoint_iff, dfinsupp.bot_eq_zero, ← dfinsupp.support_eq_empty, dfinsupp.support_inf], refl, end end canonically_linear_ordered_add_monoid end dfinsupp
8ce63c98bb494d76fff7a0e0bbc72ed4ed1ebff7
2c1ae42b7676ac4a573a53b902feb68be1ea98f7
/src/Interact.lean
29529de9efd1f855244bfa274d2fac7a8bc7a820
[]
no_license
rodrigogribeiro/lean-tutorial
67d9793fdfba1ddd418ec5c29c30564ff3f4987a
564254d582181d08eedf241781ab5a4c9ce8d832
refs/heads/master
1,610,252,605,463
1,447,618,793,000
1,447,618,793,000
43,224,821
0
0
null
null
null
null
UTF-8
Lean
false
false
205
lean
import data.nat import standard algebra.ring algebra.ordered_ring open nat algebra check eq check @eq check eq.symm check @eq.symm print notation + check and.comm check @add.comm check @succ_ne_zero
8e90b9ea7c9f480f97fe68f8b16401aae5362e18
64874bd1010548c7f5a6e3e8902efa63baaff785
/hott/algebra/precategory/iso.hlean
d9ad03a847f173dc09c50d911f7bfe3cf42e50b3
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,365
hlean
-- Copyright (c) 2014 Jakob von Raumer. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Floris van Doorn, Jakob von Raumer import .basic .morphism types.sigma open eq precategory sigma sigma.ops equiv is_equiv function truncation open prod namespace morphism variables {ob : Type} [C : precategory ob] include C variables {a b c : ob} {g : b ⟶ c} {f : a ⟶ b} {h : b ⟶ a} -- "is_iso f" is equivalent to a certain sigma type protected definition sigma_char (f : hom a b) : (Σ (g : hom b a), (g ∘ f = id) × (f ∘ g = id)) ≃ is_iso f := begin fapply (equiv.mk), intro S, apply is_iso.mk, exact (pr₁ S.2), exact (pr₂ S.2), fapply adjointify, intro H, apply (is_iso.rec_on H), intros (g, η, ε), exact (sigma.mk g (pair η ε)), intro H, apply (is_iso.rec_on H), intros (g, η, ε), apply idp, intro S, apply (sigma.rec_on S), intros (g, ηε), apply (prod.rec_on ηε), intros (η, ε), apply idp, end -- The structure for isomorphism can be characterized up to equivalence -- by a sigma type. definition sigma_is_iso_equiv ⦃a b : ob⦄ : (Σ (f : hom a b), is_iso f) ≃ (a ≅ b) := begin fapply (equiv.mk), intro S, apply isomorphic.mk, apply (S.2), fapply adjointify, intro p, apply (isomorphic.rec_on p), intros (f, H), exact (sigma.mk f H), intro p, apply (isomorphic.rec_on p), intros (f, H), apply idp, intro S, apply (sigma.rec_on S), intros (f, H), apply idp, end -- The statement "f is an isomorphism" is a mere proposition definition is_hprop_of_is_iso : is_hset (is_iso f) := begin apply trunc_equiv, apply (equiv.to_is_equiv (!sigma_char)), apply trunc_sigma, apply (!homH), intro g, apply trunc_prod, repeat (apply succ_is_trunc; apply trunc_succ; apply (!homH)), end -- The type of isomorphisms between two objects is a set definition is_hset_iso : is_hset (a ≅ b) := begin apply trunc_equiv, apply (equiv.to_is_equiv (!sigma_is_iso_equiv)), apply trunc_sigma, apply homH, intro f, apply is_hprop_of_is_iso, end -- In a precategory, equal objects are isomorphic definition iso_of_path (p : a = b) : isomorphic a b := eq.rec_on p (isomorphic.mk id) end morphism
dd00fd1f21dac9ea6683fa45bc142eac5213e08b
4727251e0cd73359b15b664c3170e5d754078599
/src/measure_theory/integral/average.lean
7bb6581827581c06c21401d7ed4fd16dcb4a1cfb
[ "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
7,997
lean
/- Copyright (c) 2022 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import measure_theory.integral.set_integral /-! # Integral average of a function In this file we define `measure_theory.average μ f` (notation: `⨍ x, f x ∂μ`) to be the average value of `f` with respect to measure `μ`. It is defined as `∫ x, f x ∂((μ univ)⁻¹ • μ)`, so it is equal to zero if `f` is not integrable or if `μ` is an infinite measure. If `μ` is a probability measure, then the average of any function is equal to its integral. For the average on a set, we use `⨍ x in s, f x ∂μ` (notation for `⨍ x, f x ∂(μ.restrict s)`). For average w.r.t. the volume, one can omit `∂volume`. ## Implementation notes The average is defined as an integral over `(μ univ)⁻¹ • μ` so that all theorems about Bochner integrals work for the average without modifications. For theorems that require integrability of a function, we provide a convenience lemma `measure_theory.integrable.to_average`. ## Tags integral, center mass, average value -/ open measure_theory measure_theory.measure metric set filter topological_space function open_locale topological_space big_operators ennreal convex variables {α E F : Type*} {m0 : measurable_space α} [normed_group E] [normed_space ℝ E] [complete_space E] [normed_group F] [normed_space ℝ F] [complete_space F] {μ : measure α} {s : set E} /-! ### Average value of a function w.r.t. a measure The average value of a function `f` w.r.t. a measure `μ` (notation: `⨍ x, f x ∂μ`) is defined as `(μ univ).to_real⁻¹ • ∫ x, f x ∂μ`, so it is equal to zero if `f` is not integrable or if `μ` is an infinite measure. If `μ` is a probability measure, then the average of any function is equal to its integral. -/ namespace measure_theory variable (μ) include m0 /-- Average value of a function `f` w.r.t. a measure `μ`, notation: `⨍ x, f x ∂μ`. It is defined as `(μ univ).to_real⁻¹ • ∫ x, f x ∂μ`, so it is equal to zero if `f` is not integrable or if `μ` is an infinite measure. If `μ` is a probability measure, then the average of any function is equal to its integral. For the average on a set, use `⨍ x in s, f x ∂μ` (defined as `⨍ x, f x ∂(μ.restrict s)`). For average w.r.t. the volume, one can omit `∂volume`. -/ noncomputable def average (f : α → E) := ∫ x, f x ∂((μ univ)⁻¹ • μ) notation `⨍` binders `, ` r:(scoped:60 f, f) ` ∂` μ:70 := average μ r notation `⨍` binders `, ` r:(scoped:60 f, average volume f) := r notation `⨍` binders ` in ` s `, ` r:(scoped:60 f, f) ` ∂` μ:70 := average (measure.restrict μ s) r notation `⨍` binders ` in ` s `, ` r:(scoped:60 f, average (measure.restrict volume s) f) := r @[simp] lemma average_zero : ⨍ x, (0 : E) ∂μ = 0 := by rw [average, integral_zero] @[simp] lemma average_zero_measure (f : α → E) : ⨍ x, f x ∂(0 : measure α) = 0 := by rw [average, smul_zero, integral_zero_measure] @[simp] lemma average_neg (f : α → E) : ⨍ x, -f x ∂μ = -⨍ x, f x ∂μ := integral_neg f lemma average_def (f : α → E) : ⨍ x, f x ∂μ = ∫ x, f x ∂((μ univ)⁻¹ • μ) := rfl lemma average_def' (f : α → E) : ⨍ x, f x ∂μ = (μ univ).to_real⁻¹ • ∫ x, f x ∂μ := by rw [average_def, integral_smul_measure, ennreal.to_real_inv] lemma average_eq_integral [is_probability_measure μ] (f : α → E) : ⨍ x, f x ∂μ = ∫ x, f x ∂μ := by rw [average, measure_univ, ennreal.inv_one, one_smul] @[simp] lemma measure_smul_average [is_finite_measure μ] (f : α → E) : (μ univ).to_real • ⨍ x, f x ∂μ = ∫ x, f x ∂μ := begin cases eq_or_ne μ 0 with hμ hμ, { rw [hμ, integral_zero_measure, average_zero_measure, smul_zero] }, { rw [average_def', smul_inv_smul₀], refine (ennreal.to_real_pos _ $ measure_ne_top _ _).ne', rwa [ne.def, measure_univ_eq_zero] } end lemma set_average_eq (f : α → E) (s : set α) : ⨍ x in s, f x ∂μ = (μ s).to_real⁻¹ • ∫ x in s, f x ∂μ := by rw [average_def', restrict_apply_univ] variable {μ} lemma average_congr {f g : α → E} (h : f =ᵐ[μ] g) : ⨍ x, f x ∂μ = ⨍ x, g x ∂μ := by simp only [average_def', integral_congr_ae h] lemma average_add_measure [is_finite_measure μ] {ν : measure α} [is_finite_measure ν] {f : α → E} (hμ : integrable f μ) (hν : integrable f ν) : ⨍ x, f x ∂(μ + ν) = ((μ univ).to_real / ((μ univ).to_real + (ν univ).to_real)) • ⨍ x, f x ∂μ + ((ν univ).to_real / ((μ univ).to_real + (ν univ).to_real)) • ⨍ x, f x ∂ν := begin simp only [div_eq_inv_mul, mul_smul, measure_smul_average, ← smul_add, ← integral_add_measure hμ hν, ← ennreal.to_real_add (measure_ne_top μ _) (measure_ne_top ν _)], rw [average_def', measure.add_apply] end lemma average_pair {f : α → E} {g : α → F} (hfi : integrable f μ) (hgi : integrable g μ) : ⨍ x, (f x, g x) ∂μ = (⨍ x, f x ∂μ, ⨍ x, g x ∂μ) := integral_pair hfi.to_average hgi.to_average lemma measure_smul_set_average (f : α → E) {s : set α} (h : μ s ≠ ∞) : (μ s).to_real • ⨍ x in s, f x ∂μ = ∫ x in s, f x ∂μ := by { haveI := fact.mk h.lt_top, rw [← measure_smul_average, restrict_apply_univ] } lemma average_union {f : α → E} {s t : set α} (hd : ae_disjoint μ s t) (ht : null_measurable_set t μ) (hsμ : μ s ≠ ∞) (htμ : μ t ≠ ∞) (hfs : integrable_on f s μ) (hft : integrable_on f t μ) : ⨍ x in s ∪ t, f x ∂μ = ((μ s).to_real / ((μ s).to_real + (μ t).to_real)) • ⨍ x in s, f x ∂μ + ((μ t).to_real / ((μ s).to_real + (μ t).to_real)) • ⨍ x in t, f x ∂μ := begin haveI := fact.mk hsμ.lt_top, haveI := fact.mk htμ.lt_top, rw [restrict_union₀ hd ht, average_add_measure hfs hft, restrict_apply_univ, restrict_apply_univ] end lemma average_union_mem_open_segment {f : α → E} {s t : set α} (hd : ae_disjoint μ s t) (ht : null_measurable_set t μ) (hs₀ : μ s ≠ 0) (ht₀ : μ t ≠ 0) (hsμ : μ s ≠ ∞) (htμ : μ t ≠ ∞) (hfs : integrable_on f s μ) (hft : integrable_on f t μ) : ⨍ x in s ∪ t, f x ∂μ ∈ open_segment ℝ (⨍ x in s, f x ∂μ) (⨍ x in t, f x ∂μ) := begin replace hs₀ : 0 < (μ s).to_real, from ennreal.to_real_pos hs₀ hsμ, replace ht₀ : 0 < (μ t).to_real, from ennreal.to_real_pos ht₀ htμ, refine mem_open_segment_iff_div.mpr ⟨(μ s).to_real, (μ t).to_real, hs₀, ht₀, (average_union hd ht hsμ htμ hfs hft).symm⟩ end lemma average_union_mem_segment {f : α → E} {s t : set α} (hd : ae_disjoint μ s t) (ht : null_measurable_set t μ) (hsμ : μ s ≠ ∞) (htμ : μ t ≠ ∞) (hfs : integrable_on f s μ) (hft : integrable_on f t μ) : ⨍ x in s ∪ t, f x ∂μ ∈ [⨍ x in s, f x ∂μ -[ℝ] ⨍ x in t, f x ∂μ] := begin by_cases hse : μ s = 0, { rw ← ae_eq_empty at hse, rw [restrict_congr_set (hse.union eventually_eq.rfl), empty_union], exact right_mem_segment _ _ _ }, { refine mem_segment_iff_div.mpr ⟨(μ s).to_real, (μ t).to_real, ennreal.to_real_nonneg, ennreal.to_real_nonneg, _, (average_union hd ht hsμ htμ hfs hft).symm⟩, calc 0 < (μ s).to_real : ennreal.to_real_pos hse hsμ ... ≤ _ : le_add_of_nonneg_right ennreal.to_real_nonneg } end lemma average_mem_open_segment_compl_self [is_finite_measure μ] {f : α → E} {s : set α} (hs : null_measurable_set s μ) (hs₀ : μ s ≠ 0) (hsc₀ : μ sᶜ ≠ 0) (hfi : integrable f μ) : ⨍ x, f x ∂μ ∈ open_segment ℝ (⨍ x in s, f x ∂μ) (⨍ x in sᶜ, f x ∂μ) := by simpa only [union_compl_self, restrict_univ] using average_union_mem_open_segment ae_disjoint_compl_right hs.compl hs₀ hsc₀ (measure_ne_top _ _) (measure_ne_top _ _) hfi.integrable_on hfi.integrable_on end measure_theory
326d3e10932af330eb8bb58110e7f80a1a4ef873
59a4b050600ed7b3d5826a8478db0a9bdc190252
/src/category_theory/idempotent_completion.lean
93b65550192a782bc8de63afe44249dd8653f84f
[]
no_license
rwbarton/lean-category-theory
f720268d800b62a25d69842ca7b5d27822f00652
00df814d463406b7a13a56f5dcda67758ba1b419
refs/heads/master
1,585,366,296,767
1,536,151,349,000
1,536,151,349,000
147,652,096
0
0
null
1,536,226,960,000
1,536,226,960,000
null
UTF-8
Lean
false
false
4,222
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 category_theory.equivalence namespace category_theory universes u u₁ u₂ structure Idempotent (C : Type (u+1)) [large_category C] := (object : C) (idempotent : object ⟶ object) (witness' : idempotent ≫ idempotent = idempotent . obviously) restate_axiom Idempotent.witness' attribute [simp,search] Idempotent.witness local attribute [search] subtype.property variables {C : Type (u+1)} [large_category C] namespace Idempotent structure morphism (X Y : Idempotent C) := (morphism : X.object ⟶ Y.object) (left' : X.idempotent ≫ morphism = morphism . obviously) (right' : morphism ≫ Y.idempotent = morphism . obviously) restate_axiom morphism.left' restate_axiom morphism.right' attribute [simp,search] morphism.left morphism.right @[extensionality] lemma ext {X Y : Idempotent C} (f g : morphism X Y) (w : f.morphism = g.morphism) : f = g := begin induction f, induction g, tidy end end Idempotent instance IdempotentCompletion : large_category (Idempotent C) := { hom := Idempotent.morphism, id := λ X, ⟨ X.idempotent ⟩, comp := λ X Y Z f g, ⟨ f.morphism ≫ g.morphism ⟩ } namespace IdempotentCompletion def functor_to_completion (C : Type (u+1)) [large_category C] : C ⥤ (Idempotent C) := { obj := λ X, { object := X, idempotent := 𝟙 X }, map' := λ _ _ f, { morphism := f } } -- -- PROJECT -- def IdempotentCompletion_functorial (C : Type u) [category C] (D : Type u) [category D] : Functor (Functor C D) (Functor (Idempotent C) (Idempotent D)) := { -- FIXME -- lemma embedding' (C : Type (u+1)) [large_category C] : embedding (functor_to_completion C) := by obviously variable {D : Type (u₂+1)} variable [large_category D] def restrict_Functor_from (F : (Idempotent C) ⥤ D) : C ⥤ D := (functor_to_completion C) ⋙ F @[simp] private lemma double_idempotent_morphism_left (X Y : Idempotent (Idempotent C)) (f : X ⟶ Y) : (X.idempotent).morphism ≫ (f.morphism).morphism = (f.morphism).morphism := congr_arg Idempotent.morphism.morphism f.left @[simp] private lemma double_idempotent_morphism_right (X Y : Idempotent (Idempotent C)) (f : X ⟶ Y) : (f.morphism).morphism ≫ (Y.idempotent).morphism = (f.morphism).morphism := congr_arg Idempotent.morphism.morphism f.right private def idempotent_functor (C : Type (u+1)) [large_category C] : (Idempotent (Idempotent C)) ⥤ (Idempotent C) := { obj := λ X, ⟨ X.object.object, X.idempotent.morphism, congr_arg Idempotent.morphism.morphism X.witness ⟩, -- PROJECT think about automation here map' := λ X Y f, ⟨ f.morphism.morphism, by obviously ⟩ } private def idempotent_inverse (C : Type (u+1)) [large_category C] : (Idempotent C) ⥤ (Idempotent (Idempotent C)) := { obj := λ X, ⟨ X, ⟨ X.idempotent, by obviously ⟩, by obviously ⟩, map' := λ X Y f, ⟨ f, by obviously ⟩ } -- PROJECT prove these lemmas about idempotent completion -- lemma IdempotentCompletion_idempotent (C : Type u) [category C] : -- Equivalence (IdempotentCompletion (IdempotentCompletion C)) (IdempotentCompletion C) := -- { -- functor := IdempotentCompletion_idempotent_functor C, -- inverse := IdempotentCompletion_idempotent_inverse C, -- isomorphism_1 := begin tidy, exact C.identity _, tidy, induction f_2, tidy, end, -- PROJECT very slow?? -- isomorphism_2 := sorry --} def extend_Functor_to_completion (F : C ⥤ (Idempotent D)) : (Idempotent C) ⥤ (Idempotent D) := { obj := λ X, { object := (F X.object).object, idempotent := (F.map X.idempotent).morphism }, map' := λ X Y f, { morphism := (F.map f.morphism).morphism } } -- lemma Functor_from_IdempotentCompletion_determined_by_restriction -- {C D : Category} (F : Functor (IdempotentCompletion C) (IdempotentCompletion D)) : -- NaturalIsomorphism (extend_Functor_to_IdempotentCompletion (restrict_Functor_from_IdempotentCompletion F)) F := -- sorry -- PROJECT idempotent completion left adjoint to the forgetful functor from categories to semicategories? end IdempotentCompletion end category_theory
a04e7c8d8c4fe50e9ede726d9db73fee0a46bfb8
097294e9b80f0d9893ac160b9c7219aa135b51b9
/assignments/hw8/associativity_and_precedence.lean
22c9299a17b11c2d134c8e537fe60ae2849f8dc2
[]
no_license
AbigailCastro17/CS2102-Discrete-Math
cf296251be9418ce90206f5e66bde9163e21abf9
d741e4d2d6a9b2e0c8380e51706218b8f608cee4
refs/heads/main
1,682,891,087,358
1,621,401,341,000
1,621,401,341,000
368,749,959
0
0
null
null
null
null
UTF-8
Lean
false
false
2,917
lean
import .propositional_logic_syntax_and_semantics open pExp #check 2 + 3 + 4 #check (2 + 3) + 4 #check 2 + (3 + 4) /- Motivation: Can't overlad →. First attempt at a solution: overload > to mean pImp, as follows. notation e1 > e2 := pImp e1 e2 Problem: > is a reserved notation in Lean. We can overload it but we cannot change either is precedence or its associativity. The notation is thus non-standard and confusing. -/ def P := pVar (var.mk 0) def Q := pVar (var.mk 1) def R := pVar (var.mk 2) -- associativity is wrong #check P > Q > R #check P > (Q > R) #check (P > Q) > R -- associativity is wrong #check P ∧ Q > Q > R #check (P ∧ Q) > (Q > R) #check P ∧ (Q > Q) > R -- precedence is wrong, too #check P ∨ Q > Q > R #check (P ∨ Q) > (Q > R) #check P ∨ (Q > Q) > R #check P ∧ Q > Q > R #check (P ∧ Q) > (Q > R) #check P ∧ (Q > Q) > R #check P > Q ↔ Q > P #check (P > Q) ↔ (Q > P) /- Solution: Define our own infix operator with appropriate precedence (also called) binding strength. First, let's see where the other reserved operators that we're using (such as ∧ and ∨) get their binding strengths: from one of Lean's libraries. Here's what appears there (some details omitted). /- /- Logical operations and relations -/ reserve prefix `¬`:40 reserve prefix `~`:40 reserve infixr ` ∧ `:35 reserve infixr ` /\ `:35 reserve infixr ` \/ `:30 reserve infixr ` ∨ `:30 reserve infix ` <-> `:20 reserve infix ` ↔ `:20 reserve infix ` = `:50 reserve infix ` == `:50 reserve infix ` ≠ `:50 reserve infix ` ≈ `:50 reserve infix ` ~ `:50 reserve infix ` ≡ `:50 reserve infixl ` ⬝ `:75 reserve infixr ` ▸ `:75 reserve infixr ` ▹ `:75 /- arithmetic operations -/ reserve infixl ` + `:65 reserve infixl ` - `:65 reserve infixl ` * `:70 reserve infixl ` / `:70 reserve infixl ` % `:70 reserve prefix `-`:100 reserve infixr ` ^ `:80 reserve infixr ` ∘ `:90 -- input with \comp reserve infix ` <= `:50 reserve infix ` ≤ `:50 reserve infix ` < `:50 reserve infix ` >= `:50 reserve infix ` ≥ `:50 reserve infix ` > `:50 /- boolean operations -/ reserve infixl ` && `:70 reserve infixl ` || `:65 -/ -/ -- Here's our new notation infixr ` >> ` : 30 := pImp -- associativity is correct #check P >> Q >> R #check P >> (Q >> R) #check (P >> Q) >> R -- precedence is correct #check P ∧ Q >> Q >> R #check (P ∧ Q) >> (Q >> R) #check P ∧ (Q >> Q) >> R #check P ∨ Q >> Q >> R #check P ∨ Q >> (Q >> R) #check P ∨ (Q >> (Q >> R)) --uh oh, another bug! /- What is wrong? How to fix it? -/ #check (P ∨ Q) >> (Q >> R) #check (P ∨ Q) >> Q >> R axioms X Y Z : Prop #check X ∨ Y → Y → Z #check X ∨ Y → (Y → Z) #check (P ∨ Q) >> Q >> R #check P ∨ (Q >> Q) >> R #check P ∧ Q >> Q >> R #check (P ∧ Q) >> (Q >> R) #check P ∧ (Q >> Q) >> R #check P >> Q ↔ Q >> P #check (P >> Q) ↔ (Q >> P)
feb81c695014f3225ed2ae93b376d79941386815
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/continued_fractions/computation/approximations.lean
15194e3e7af1fcfc234f304525d9ca11c9e7c182
[ "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
29,768
lean
/- Copyright (c) 2020 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import algebra.continued_fractions.computation.correctness_terminating import data.nat.fib import tactic.solve_by_elim /-! # Approximations for Continued Fraction Computations (`generalized_continued_fraction.of`) ## Summary This file contains useful approximations for the values involved in the continued fractions computation `generalized_continued_fraction.of`. In particular, we derive the so-called *determinant formula* for `generalized_continued_fraction.of`: `Aₙ * Bₙ₊₁ - Bₙ * Aₙ₊₁ = (-1)^(n + 1)`. Moreover, we derive some upper bounds for the error term when computing a continued fraction up a given position, i.e. bounds for the term `|v - (generalized_continued_fraction.of v).convergents n|`. The derived bounds will show us that the error term indeed gets smaller. As a corollary, we will be able to show that `(generalized_continued_fraction.of v).convergents` converges to `v` in `algebra.continued_fractions.computation.approximation_corollaries`. ## Main Theorems - `generalized_continued_fraction.of_part_num_eq_one`: shows that all partial numerators `aᵢ` are equal to one. - `generalized_continued_fraction.exists_int_eq_of_part_denom`: shows that all partial denominators `bᵢ` correspond to an integer. - `generalized_continued_fraction.one_le_of_nth_part_denom`: shows that `1 ≤ bᵢ`. - `generalized_continued_fraction.succ_nth_fib_le_of_nth_denom`: shows that the `n`th denominator `Bₙ` is greater than or equal to the `n + 1`th fibonacci number `nat.fib (n + 1)`. - `generalized_continued_fraction.le_of_succ_nth_denom`: shows that `bₙ * Bₙ ≤ Bₙ₊₁`, where `bₙ` is the `n`th partial denominator of the continued fraction. - `generalized_continued_fraction.abs_sub_convergents_le`: shows that `|v - Aₙ / Bₙ| ≤ 1 / (Bₙ * Bₙ₊₁)`, where `Aₙ` is the nth partial numerator. ## References - [*Hardy, GH and Wright, EM and Heath-Brown, Roger and Silverman, Joseph*][hardy2008introduction] - https://en.wikipedia.org/wiki/Generalized_continued_fraction#The_determinant_formula -/ namespace generalized_continued_fraction open generalized_continued_fraction (of) int variables {K : Type*} {v : K} {n : ℕ} [linear_ordered_field K] [floor_ring K] namespace int_fract_pair /-! We begin with some lemmas about the stream of `int_fract_pair`s, which presumably are not of great interest for the end user. -/ /-- Shows that the fractional parts of the stream are in `[0,1)`. -/ lemma nth_stream_fr_nonneg_lt_one {ifp_n : int_fract_pair K} (nth_stream_eq : int_fract_pair.stream v n = some ifp_n) : 0 ≤ ifp_n.fr ∧ ifp_n.fr < 1 := begin cases n, case nat.zero { have : int_fract_pair.of v = ifp_n, by injection nth_stream_eq, rw [←this, int_fract_pair.of], exact ⟨fract_nonneg _, fract_lt_one _⟩ }, case nat.succ { rcases (succ_nth_stream_eq_some_iff.elim_left nth_stream_eq) with ⟨_, _, _, ifp_of_eq_ifp_n⟩, rw [←ifp_of_eq_ifp_n, int_fract_pair.of], exact ⟨fract_nonneg _, fract_lt_one _⟩ } end /-- Shows that the fractional parts of the stream are nonnegative. -/ lemma nth_stream_fr_nonneg {ifp_n : int_fract_pair K} (nth_stream_eq : int_fract_pair.stream v n = some ifp_n) : 0 ≤ ifp_n.fr := (nth_stream_fr_nonneg_lt_one nth_stream_eq).left /-- Shows that the fractional parts of the stream are smaller than one. -/ lemma nth_stream_fr_lt_one {ifp_n : int_fract_pair K} (nth_stream_eq : int_fract_pair.stream v n = some ifp_n) : ifp_n.fr < 1 := (nth_stream_fr_nonneg_lt_one nth_stream_eq).right /-- Shows that the integer parts of the stream are at least one. -/ lemma one_le_succ_nth_stream_b {ifp_succ_n : int_fract_pair K} (succ_nth_stream_eq : int_fract_pair.stream v (n + 1) = some ifp_succ_n) : 1 ≤ ifp_succ_n.b := begin obtain ⟨ifp_n, nth_stream_eq, stream_nth_fr_ne_zero, ⟨-⟩⟩ : ∃ ifp_n, int_fract_pair.stream v n = some ifp_n ∧ ifp_n.fr ≠ 0 ∧ int_fract_pair.of ifp_n.fr⁻¹ = ifp_succ_n, from succ_nth_stream_eq_some_iff.elim_left succ_nth_stream_eq, suffices : 1 ≤ ifp_n.fr⁻¹, { rw_mod_cast [le_floor], assumption }, suffices : ifp_n.fr ≤ 1, { have h : 0 < ifp_n.fr, from lt_of_le_of_ne (nth_stream_fr_nonneg nth_stream_eq) stream_nth_fr_ne_zero.symm, apply one_le_inv h this }, simp only [le_of_lt (nth_stream_fr_lt_one nth_stream_eq)] end /-- Shows that the `n + 1`th integer part `bₙ₊₁` of the stream is smaller or equal than the inverse of the `n`th fractional part `frₙ` of the stream. This result is straight-forward as `bₙ₊₁` is defined as the floor of `1 / frₙ` -/ lemma succ_nth_stream_b_le_nth_stream_fr_inv {ifp_n ifp_succ_n : int_fract_pair K} (nth_stream_eq : int_fract_pair.stream v n = some ifp_n) (succ_nth_stream_eq : int_fract_pair.stream v (n + 1) = some ifp_succ_n) : (ifp_succ_n.b : K) ≤ ifp_n.fr⁻¹ := begin suffices : (⌊ifp_n.fr⁻¹⌋ : K) ≤ ifp_n.fr⁻¹, { cases ifp_n with _ ifp_n_fr, have : ifp_n_fr ≠ 0, { intro h, simpa [h, int_fract_pair.stream, nth_stream_eq] using succ_nth_stream_eq }, have : int_fract_pair.of ifp_n_fr⁻¹ = ifp_succ_n, { simpa [this, int_fract_pair.stream, nth_stream_eq, option.coe_def] using succ_nth_stream_eq }, rwa ←this }, exact (floor_le ifp_n.fr⁻¹) end end int_fract_pair /-! Next we translate above results about the stream of `int_fract_pair`s to the computed continued fraction `generalized_continued_fraction.of`. -/ /-- Shows that the integer parts of the continued fraction are at least one. -/ lemma of_one_le_nth_part_denom {b : K} (nth_part_denom_eq : (of v).partial_denominators.nth n = some b) : 1 ≤ b := begin obtain ⟨gp_n, nth_s_eq, ⟨-⟩⟩ : ∃ gp_n, (of v).s.nth n = some gp_n ∧ gp_n.b = b, from exists_s_b_of_part_denom nth_part_denom_eq, obtain ⟨ifp_n, succ_nth_stream_eq, ifp_n_b_eq_gp_n_b⟩ : ∃ ifp, int_fract_pair.stream v (n + 1) = some ifp ∧ (ifp.b : K) = gp_n.b, from int_fract_pair.exists_succ_nth_stream_of_gcf_of_nth_eq_some nth_s_eq, rw [←ifp_n_b_eq_gp_n_b], exact_mod_cast (int_fract_pair.one_le_succ_nth_stream_b succ_nth_stream_eq) end /-- Shows that the partial numerators `aᵢ` of the continued fraction are equal to one and the partial denominators `bᵢ` correspond to integers. -/ lemma of_part_num_eq_one_and_exists_int_part_denom_eq {gp : generalized_continued_fraction.pair K} (nth_s_eq : (of v).s.nth n = some gp) : gp.a = 1 ∧ ∃ (z : ℤ), gp.b = (z : K) := begin obtain ⟨ifp, stream_succ_nth_eq, -⟩ : ∃ ifp, int_fract_pair.stream v (n + 1) = some ifp ∧ _, from int_fract_pair.exists_succ_nth_stream_of_gcf_of_nth_eq_some nth_s_eq, have : gp = ⟨1, ifp.b⟩, by { have : (of v).s.nth n = some ⟨1, ifp.b⟩, from nth_of_eq_some_of_succ_nth_int_fract_pair_stream stream_succ_nth_eq, have : some gp = some ⟨1, ifp.b⟩, by rwa nth_s_eq at this, injection this }, simp [this], end /-- Shows that the partial numerators `aᵢ` are equal to one. -/ lemma of_part_num_eq_one {a : K} (nth_part_num_eq : (of v).partial_numerators.nth n = some a) : a = 1 := begin obtain ⟨gp, nth_s_eq, gp_a_eq_a_n⟩ : ∃ gp, (of v).s.nth n = some gp ∧ gp.a = a, from exists_s_a_of_part_num nth_part_num_eq, have : gp.a = 1, from (of_part_num_eq_one_and_exists_int_part_denom_eq nth_s_eq).left, rwa gp_a_eq_a_n at this end /-- Shows that the partial denominators `bᵢ` correspond to an integer. -/ lemma exists_int_eq_of_part_denom {b : K} (nth_part_denom_eq : (of v).partial_denominators.nth n = some b) : ∃ (z : ℤ), b = (z : K) := begin obtain ⟨gp, nth_s_eq, gp_b_eq_b_n⟩ : ∃ gp, (of v).s.nth n = some gp ∧ gp.b = b, from exists_s_b_of_part_denom nth_part_denom_eq, have : ∃ (z : ℤ), gp.b = (z : K), from (of_part_num_eq_one_and_exists_int_part_denom_eq nth_s_eq).right, rwa gp_b_eq_b_n at this end /-! One of our next goals is to show that `bₙ * Bₙ ≤ Bₙ₊₁`. For this, we first show that the partial denominators `Bₙ` are bounded from below by the fibonacci sequence `nat.fib`. This then implies that `0 ≤ Bₙ` and hence `Bₙ₊₂ = bₙ₊₁ * Bₙ₊₁ + Bₙ ≥ bₙ₊₁ * Bₙ₊₁ + 0 = bₙ₊₁ * Bₙ₊₁`. -/ -- open `nat` as we will make use of fibonacci numbers. open nat lemma fib_le_of_continuants_aux_b : (n ≤ 1 ∨ ¬(of v).terminated_at (n - 2)) → (fib n : K) ≤ ((of v).continuants_aux n).b := nat.strong_induction_on n begin clear n, assume n IH hyp, rcases n with _|_|n, { simp [fib_add_two, continuants_aux] }, -- case n = 0 { simp [fib_add_two, continuants_aux] }, -- case n = 1 { let g := of v, -- case 2 ≤ n have : ¬(n + 2 ≤ 1), by linarith, have not_terminated_at_n : ¬g.terminated_at n, from or.resolve_left hyp this, obtain ⟨gp, s_ppred_nth_eq⟩ : ∃ gp, g.s.nth n = some gp, from option.ne_none_iff_exists'.mp not_terminated_at_n, set pconts := g.continuants_aux (n + 1) with pconts_eq, set ppconts := g.continuants_aux n with ppconts_eq, -- use the recurrence of continuants_aux suffices : (fib n : K) + fib (n + 1) ≤ gp.a * ppconts.b + gp.b * pconts.b, by simpa [fib_add_two, add_comm, (continuants_aux_recurrence s_ppred_nth_eq ppconts_eq pconts_eq)], -- make use of the fact that gp.a = 1 suffices : (fib n : K) + fib (n + 1) ≤ ppconts.b + gp.b * pconts.b, by simpa [(of_part_num_eq_one $ part_num_eq_s_a s_ppred_nth_eq)], have not_terminated_at_pred_n : ¬g.terminated_at (n - 1), from mt (terminated_stable $ nat.sub_le n 1) not_terminated_at_n, have not_terminated_at_ppred_n : ¬terminated_at g (n - 2), from mt (terminated_stable (n - 1).pred_le) not_terminated_at_pred_n, -- use the IH to get the inequalities for `pconts` and `ppconts` have : (fib (n + 1) : K) ≤ pconts.b, from IH _ (nat.lt.base $ n + 1) (or.inr not_terminated_at_pred_n), have ppred_nth_fib_le_ppconts_B : (fib n : K) ≤ ppconts.b, from IH n (lt_trans (nat.lt.base n) $ nat.lt.base $ n + 1) (or.inr not_terminated_at_ppred_n), suffices : (fib (n + 1) : K) ≤ gp.b * pconts.b, solve_by_elim [add_le_add ppred_nth_fib_le_ppconts_B], -- finally use the fact that 1 ≤ gp.b to solve the goal suffices : 1 * (fib (n + 1) : K) ≤ gp.b * pconts.b, by rwa [one_mul] at this, have one_le_gp_b : (1 : K) ≤ gp.b, from of_one_le_nth_part_denom (part_denom_eq_s_b s_ppred_nth_eq), have : (0 : K) ≤ fib (n + 1), by exact_mod_cast (fib (n + 1)).zero_le, have : (0 : K) ≤ gp.b, from le_trans zero_le_one one_le_gp_b, mono } end /-- Shows that the `n`th denominator is greater than or equal to the `n + 1`th fibonacci number, that is `nat.fib (n + 1) ≤ Bₙ`. -/ lemma succ_nth_fib_le_of_nth_denom (hyp: n = 0 ∨ ¬(of v).terminated_at (n - 1)) : (fib (n + 1) : K) ≤ (of v).denominators n := begin rw [denom_eq_conts_b, nth_cont_eq_succ_nth_cont_aux], have : (n + 1) ≤ 1 ∨ ¬(of v).terminated_at (n - 1), by { cases n, case nat.zero : { exact (or.inl $ le_refl 1) }, case nat.succ : { exact or.inr (or.resolve_left hyp n.succ_ne_zero) } }, exact (fib_le_of_continuants_aux_b this) end /-! As a simple consequence, we can now derive that all denominators are nonnegative. -/ lemma zero_le_of_continuants_aux_b : 0 ≤ ((of v).continuants_aux n).b := begin let g := of v, induction n with n IH, case nat.zero: { refl }, case nat.succ: { cases (decidable.em $ g.terminated_at (n - 1)) with terminated not_terminated, { cases n, -- terminating case { simp [zero_le_one] }, { have : g.continuants_aux (n + 2) = g.continuants_aux (n + 1), from continuants_aux_stable_step_of_terminated terminated, simp only [this, IH] } }, { calc -- non-terminating case (0 : K) ≤ fib (n + 1) : by exact_mod_cast (n + 1).fib.zero_le ... ≤ ((of v).continuants_aux (n + 1)).b : fib_le_of_continuants_aux_b (or.inr not_terminated) } } end /-- Shows that all denominators are nonnegative. -/ lemma zero_le_of_denom : 0 ≤ (of v).denominators n := by { rw [denom_eq_conts_b, nth_cont_eq_succ_nth_cont_aux], exact zero_le_of_continuants_aux_b } lemma le_of_succ_succ_nth_continuants_aux_b {b : K} (nth_part_denom_eq : (of v).partial_denominators.nth n = some b) : b * ((of v).continuants_aux $ n + 1).b ≤ ((of v).continuants_aux $ n + 2).b := begin set g := of v with g_eq, obtain ⟨gp_n, nth_s_eq, gpnb_eq_b⟩ : ∃ gp_n, g.s.nth n = some gp_n ∧ gp_n.b = b, from exists_s_b_of_part_denom nth_part_denom_eq, subst gpnb_eq_b, let conts := g.continuants_aux (n + 2), set pconts := g.continuants_aux (n + 1) with pconts_eq, set ppconts := g.continuants_aux n with ppconts_eq, have h1 : 0 ≤ ppconts.b, from zero_le_of_continuants_aux_b, have h2 : gp_n.b * pconts.b ≤ ppconts.b + gp_n.b * pconts.b, { solve_by_elim [le_add_of_nonneg_of_le, le_refl] }, -- use the recurrence of continuants_aux and the fact that gp_n.a = 1 simp [h1, h2, of_part_num_eq_one (part_num_eq_s_a nth_s_eq), generalized_continued_fraction.continuants_aux_recurrence nth_s_eq ppconts_eq pconts_eq], end /-- Shows that `bₙ * Bₙ ≤ Bₙ₊₁`, where `bₙ` is the `n`th partial denominator and `Bₙ₊₁` and `Bₙ` are the `n + 1`th and `n`th denominator of the continued fraction. -/ theorem le_of_succ_nth_denom {b : K} (nth_part_denom_eq : (of v).partial_denominators.nth n = some b) : b * (of v).denominators n ≤ (of v).denominators (n + 1) := begin rw [denom_eq_conts_b, nth_cont_eq_succ_nth_cont_aux], exact (le_of_succ_succ_nth_continuants_aux_b nth_part_denom_eq) end /-- Shows that the sequence of denominators is monotone, that is `Bₙ ≤ Bₙ₊₁`. -/ theorem of_denom_mono : (of v).denominators n ≤ (of v).denominators (n + 1) := begin let g := of v, cases (decidable.em $ g.partial_denominators.terminated_at n) with terminated not_terminated, { have : g.partial_denominators.nth n = none, by rwa seq.terminated_at at terminated, have : g.terminated_at n, from terminated_at_iff_part_denom_none.elim_right (by rwa seq.terminated_at at terminated), have : g.denominators (n + 1) = g.denominators n, from denominators_stable_of_terminated n.le_succ this, rw this }, { obtain ⟨b, nth_part_denom_eq⟩ : ∃ b, g.partial_denominators.nth n = some b, from option.ne_none_iff_exists'.mp not_terminated, have : 1 ≤ b, from of_one_le_nth_part_denom nth_part_denom_eq, calc g.denominators n ≤ b * g.denominators n : by simpa using (mul_le_mul_of_nonneg_right this zero_le_of_denom) ... ≤ g.denominators (n + 1) : le_of_succ_nth_denom nth_part_denom_eq } end section determinant /-! ### Determinant Formula Next we prove the so-called *determinant formula* for `generalized_continued_fraction.of`: `Aₙ * Bₙ₊₁ - Bₙ * Aₙ₊₁ = (-1)^(n + 1)`. -/ lemma determinant_aux (hyp: n = 0 ∨ ¬(of v).terminated_at (n - 1)) : ((of v).continuants_aux n).a * ((of v).continuants_aux (n + 1)).b - ((of v).continuants_aux n).b * ((of v).continuants_aux (n + 1)).a = (-1)^n := begin induction n with n IH, case nat.zero { simp [continuants_aux] }, case nat.succ { -- set up some shorthand notation let g := of v, let conts := continuants_aux g (n + 2), set pred_conts := continuants_aux g (n + 1) with pred_conts_eq, set ppred_conts := continuants_aux g n with ppred_conts_eq, let pA := pred_conts.a, let pB := pred_conts.b, let ppA := ppred_conts.a, let ppB := ppred_conts.b, -- let's change the goal to something more readable change pA * conts.b - pB * conts.a = (-1)^(n + 1), have not_terminated_at_n : ¬terminated_at g n, from or.resolve_left hyp n.succ_ne_zero, obtain ⟨gp, s_nth_eq⟩ : ∃ gp, g.s.nth n = some gp, from option.ne_none_iff_exists'.elim_left not_terminated_at_n, -- unfold the recurrence relation for `conts` once and simplify to derive the following suffices : pA * (ppB + gp.b * pB) - pB * (ppA + gp.b * pA) = (-1)^(n + 1), by { simp only [conts, (continuants_aux_recurrence s_nth_eq ppred_conts_eq pred_conts_eq)], have gp_a_eq_one : gp.a = 1, from of_part_num_eq_one (part_num_eq_s_a s_nth_eq), rw [gp_a_eq_one, this.symm], ring }, suffices : pA * ppB - pB * ppA = (-1)^(n + 1), calc pA * (ppB + gp.b * pB) - pB * (ppA + gp.b * pA) = pA * ppB + pA * gp.b * pB - pB * ppA - pB * gp.b * pA : by ring ... = pA * ppB - pB * ppA : by ring ... = (-1)^(n + 1) : by assumption, suffices : ppA * pB - ppB * pA = (-1)^n, by { have pow_succ_n : (-1 : K)^(n + 1) = (-1) * (-1)^n, from pow_succ (-1) n, rw [pow_succ_n, ←this], ring }, exact (IH $ or.inr $ mt (terminated_stable $ n.sub_le 1) not_terminated_at_n) } end /-- The determinant formula `Aₙ * Bₙ₊₁ - Bₙ * Aₙ₊₁ = (-1)^(n + 1)` -/ lemma determinant (not_terminated_at_n : ¬(of v).terminated_at n) : (of v).numerators n * (of v).denominators (n + 1) - (of v).denominators n * (of v).numerators (n + 1) = (-1)^(n + 1) := (determinant_aux $ or.inr $ not_terminated_at_n) end determinant section error_term /-! ### Approximation of Error Term Next we derive some approximations for the error term when computing a continued fraction up a given position, i.e. bounds for the term `|v - (generalized_continued_fraction.of v).convergents n|`. -/ /-- This lemma follows from the finite correctness proof, the determinant equality, and by simplifying the difference. -/ lemma sub_convergents_eq {ifp : int_fract_pair K} (stream_nth_eq : int_fract_pair.stream v n = some ifp) : let g := of v in let B := (g.continuants_aux (n + 1)).b in let pB := (g.continuants_aux n).b in v - g.convergents n = if ifp.fr = 0 then 0 else (-1)^n / (B * (ifp.fr⁻¹ * B + pB)) := begin -- set up some shorthand notation let g := of v, let conts := g.continuants_aux (n + 1), let pred_conts := g.continuants_aux n, have g_finite_correctness : v = generalized_continued_fraction.comp_exact_value pred_conts conts ifp.fr, from comp_exact_value_correctness_of_stream_eq_some stream_nth_eq, cases decidable.em (ifp.fr = 0) with ifp_fr_eq_zero ifp_fr_ne_zero, { suffices : v - g.convergents n = 0, by simpa [ifp_fr_eq_zero], replace g_finite_correctness : v = g.convergents n, by simpa [generalized_continued_fraction.comp_exact_value, ifp_fr_eq_zero] using g_finite_correctness, exact (sub_eq_zero.elim_right g_finite_correctness) }, { -- more shorthand notation let A := conts.a, let B := conts.b, let pA := pred_conts.a, let pB := pred_conts.b, -- first, let's simplify the goal as `ifp.fr ≠ 0` suffices : v - A / B = (-1)^n / (B * (ifp.fr⁻¹ * B + pB)), by simpa [ifp_fr_ne_zero], -- now we can unfold `g.comp_exact_value` to derive the following equality for `v` replace g_finite_correctness : v = (pA + ifp.fr⁻¹ * A) / (pB + ifp.fr⁻¹ * B), by simpa [generalized_continued_fraction.comp_exact_value, ifp_fr_ne_zero, next_continuants, next_numerator, next_denominator, add_comm] using g_finite_correctness, -- let's rewrite this equality for `v` in our goal suffices : (pA + ifp.fr⁻¹ * A) / (pB + ifp.fr⁻¹ * B) - A / B = (-1)^n / (B * (ifp.fr⁻¹ * B + pB)), by rwa g_finite_correctness, -- To continue, we need use the determinant equality. So let's derive the needed hypothesis. have n_eq_zero_or_not_terminated_at_pred_n : n = 0 ∨ ¬g.terminated_at (n - 1), by { cases n with n', { simp }, { have : int_fract_pair.stream v (n' + 1) ≠ none, by simp [stream_nth_eq], have : ¬g.terminated_at n', from (not_iff_not_of_iff of_terminated_at_n_iff_succ_nth_int_fract_pair_stream_eq_none) .elim_right this, exact (or.inr this) } }, have determinant_eq : pA * B - pB * A = (-1)^n, from determinant_aux n_eq_zero_or_not_terminated_at_pred_n, -- now all we got to do is to rewrite this equality in our goal and re-arrange terms; -- however, for this, we first have to derive quite a few tedious inequalities. have pB_ineq : (fib n : K) ≤ pB, by { have : n ≤ 1 ∨ ¬g.terminated_at (n - 2), by { cases n_eq_zero_or_not_terminated_at_pred_n with n_eq_zero not_terminated_at_pred_n, { simp [n_eq_zero] }, { exact (or.inr $ mt (terminated_stable (n - 1).pred_le) not_terminated_at_pred_n) } }, exact (fib_le_of_continuants_aux_b this) }, have B_ineq : (fib (n + 1) : K) ≤ B, by { have : n + 1 ≤ 1 ∨ ¬g.terminated_at (n + 1 - 2), by { cases n_eq_zero_or_not_terminated_at_pred_n with n_eq_zero not_terminated_at_pred_n, { simp [n_eq_zero, le_refl] }, { exact (or.inr not_terminated_at_pred_n) } }, exact (fib_le_of_continuants_aux_b this) }, have zero_lt_B : 0 < B, { have : 1 ≤ B, from le_trans (by exact_mod_cast fib_pos (lt_of_le_of_ne n.succ.zero_le n.succ_ne_zero.symm)) B_ineq, exact (lt_of_lt_of_le zero_lt_one this) }, have zero_ne_B : 0 ≠ B, from ne_of_lt zero_lt_B, have : 0 ≠ pB + ifp.fr⁻¹ * B, by { have : (0 : K) ≤ fib n, by exact_mod_cast (fib n).zero_le, -- 0 ≤ fib n ≤ pB have zero_le_pB : 0 ≤ pB, from le_trans this pB_ineq, have : 0 < ifp.fr⁻¹, by { suffices : 0 < ifp.fr, by rwa inv_pos, have : 0 ≤ ifp.fr, from int_fract_pair.nth_stream_fr_nonneg stream_nth_eq, change ifp.fr ≠ 0 at ifp_fr_ne_zero, exact lt_of_le_of_ne this ifp_fr_ne_zero.symm }, have : 0 < ifp.fr⁻¹ * B, from mul_pos this zero_lt_B, have : 0 < pB + ifp.fr⁻¹ * B, from add_pos_of_nonneg_of_pos zero_le_pB this, exact (ne_of_lt this) }, -- finally, let's do the rewriting calc (pA + ifp.fr⁻¹ * A) / (pB + ifp.fr⁻¹ * B) - A / B = ((pA + ifp.fr⁻¹ * A) * B - (pB + ifp.fr⁻¹ * B) * A) / ((pB + ifp.fr⁻¹ * B) * B) : by rw (div_sub_div _ _ this.symm zero_ne_B.symm) ... = (pA * B + ifp.fr⁻¹ * A * B - (pB * A + ifp.fr⁻¹ * B * A)) / _ : by repeat { rw [add_mul] } ... = (pA * B - pB * A) / ((pB + ifp.fr⁻¹ * B) * B) : by ring ... = (-1)^n / ((pB + ifp.fr⁻¹ * B) * B) : by rw determinant_eq ... = (-1)^n / (B * (ifp.fr⁻¹ * B + pB)) : by ac_refl } end /-- Shows that `|v - Aₙ / Bₙ| ≤ 1 / (Bₙ * Bₙ₊₁)` -/ theorem abs_sub_convergents_le (not_terminated_at_n : ¬(of v).terminated_at n) : |v - (of v).convergents n| ≤ 1 / (((of v).denominators n) * ((of v).denominators $ n + 1)) := begin -- shorthand notation let g := of v, let nextConts := g.continuants_aux (n + 2), set conts := continuants_aux g (n + 1) with conts_eq, set pred_conts := continuants_aux g n with pred_conts_eq, -- change the goal to something more readable change |v - convergents g n| ≤ 1 / (conts.b * nextConts.b), obtain ⟨gp, s_nth_eq⟩ : ∃ gp, g.s.nth n = some gp, from option.ne_none_iff_exists'.elim_left not_terminated_at_n, have gp_a_eq_one : gp.a = 1, from of_part_num_eq_one (part_num_eq_s_a s_nth_eq), -- unfold the recurrence relation for `nextConts.b` have nextConts_b_eq : nextConts.b = pred_conts.b + gp.b * conts.b, by simp [nextConts, (continuants_aux_recurrence s_nth_eq pred_conts_eq conts_eq), gp_a_eq_one, pred_conts_eq.symm, conts_eq.symm, add_comm], let denom := conts.b * (pred_conts.b + gp.b * conts.b), suffices : |v - g.convergents n| ≤ 1 / denom, by { rw [nextConts_b_eq], congr' 1 }, obtain ⟨ifp_succ_n, succ_nth_stream_eq, ifp_succ_n_b_eq_gp_b⟩ : ∃ ifp_succ_n, int_fract_pair.stream v (n + 1) = some ifp_succ_n ∧ (ifp_succ_n.b : K) = gp.b, from int_fract_pair.exists_succ_nth_stream_of_gcf_of_nth_eq_some s_nth_eq, obtain ⟨ifp_n, stream_nth_eq, stream_nth_fr_ne_zero, if_of_eq_ifp_succ_n⟩ : ∃ ifp_n, int_fract_pair.stream v n = some ifp_n ∧ ifp_n.fr ≠ 0 ∧ int_fract_pair.of ifp_n.fr⁻¹ = ifp_succ_n, from int_fract_pair.succ_nth_stream_eq_some_iff.elim_left succ_nth_stream_eq, let denom' := conts.b * (pred_conts.b + ifp_n.fr⁻¹ * conts.b), -- now we can use `sub_convergents_eq` to simplify our goal suffices : |(-1)^n / denom'| ≤ 1 / denom, by { have : v - g.convergents n = (-1)^n / denom', by { -- apply `sub_convergens_eq` and simplify the result have tmp, from sub_convergents_eq stream_nth_eq, delta at tmp, simp only [stream_nth_fr_ne_zero, conts_eq.symm, pred_conts_eq.symm] at tmp, rw tmp, simp only [denom'], ring_nf }, rwa this }, -- derive some tedious inequalities that we need to rewrite our goal have nextConts_b_ineq : (fib (n + 2) : K) ≤ (pred_conts.b + gp.b * conts.b), by { have : (fib (n + 2) : K) ≤ nextConts.b, from fib_le_of_continuants_aux_b (or.inr not_terminated_at_n), rwa [nextConts_b_eq] at this }, have conts_b_ineq : (fib (n + 1) : K) ≤ conts.b, by { have : ¬g.terminated_at (n - 1), from mt (terminated_stable n.pred_le) not_terminated_at_n, exact (fib_le_of_continuants_aux_b $ or.inr this) }, have zero_lt_conts_b : 0 < conts.b, by { have : (0 : K) < fib (n + 1), by exact_mod_cast (fib_pos (lt_of_le_of_ne n.succ.zero_le n.succ_ne_zero.symm)), exact (lt_of_lt_of_le this conts_b_ineq) }, -- `denom'` is positive, so we can remove `|⬝|` from our goal suffices : 1 / denom' ≤ 1 / denom, by { have : |(-1)^n / denom'| = 1 / denom', by { suffices : 1 / |denom'| = 1 / denom', by rwa [abs_div, (abs_neg_one_pow n)], have : 0 < denom', by { have : 0 ≤ pred_conts.b, by { have : (fib n : K) ≤ pred_conts.b, by { have : ¬g.terminated_at (n - 2), from mt (terminated_stable (n.sub_le 2)) not_terminated_at_n, exact (fib_le_of_continuants_aux_b $ or.inr this) }, exact le_trans (by exact_mod_cast (fib n).zero_le) this }, have : 0 < ifp_n.fr⁻¹, by { have zero_le_ifp_n_fract : 0 ≤ ifp_n.fr, from int_fract_pair.nth_stream_fr_nonneg stream_nth_eq, exact inv_pos.elim_right (lt_of_le_of_ne zero_le_ifp_n_fract stream_nth_fr_ne_zero.symm) }, any_goals { repeat { apply mul_pos <|> apply add_pos_of_nonneg_of_pos } }; assumption }, rwa (abs_of_pos this) }, rwa this }, suffices : 0 < denom ∧ denom ≤ denom', from div_le_div_of_le_left zero_le_one this.left this.right, split, { have : 0 < pred_conts.b + gp.b * conts.b, from lt_of_lt_of_le (by exact_mod_cast (fib_pos (lt_of_le_of_ne n.succ.succ.zero_le n.succ.succ_ne_zero.symm))) nextConts_b_ineq, solve_by_elim [mul_pos] }, { -- we can cancel multiplication by `conts.b` and addition with `pred_conts.b` suffices : gp.b * conts.b ≤ ifp_n.fr⁻¹ * conts.b, from ((mul_le_mul_left zero_lt_conts_b).elim_right $ (add_le_add_iff_left pred_conts.b).elim_right this), suffices : (ifp_succ_n.b : K) * conts.b ≤ ifp_n.fr⁻¹ * conts.b, by rwa [←ifp_succ_n_b_eq_gp_b], have : (ifp_succ_n.b : K) ≤ ifp_n.fr⁻¹, from int_fract_pair.succ_nth_stream_b_le_nth_stream_fr_inv stream_nth_eq succ_nth_stream_eq, have : 0 ≤ conts.b, from le_of_lt zero_lt_conts_b, mono } end /-- Shows that `|v - Aₙ / Bₙ| ≤ 1 / (bₙ * Bₙ * Bₙ)`. This bound is worse than the one shown in `gcf.abs_sub_convergents_le`, but sometimes it is easier to apply and sufficient for one's use case. -/ lemma abs_sub_convergents_le' {b : K} (nth_part_denom_eq : (of v).partial_denominators.nth n = some b) : |v - (of v).convergents n| ≤ 1 / (b * ((of v).denominators n) * ((of v).denominators n)) := begin let g := of v, let B := g.denominators n, let nB := g.denominators (n + 1), have not_terminated_at_n : ¬g.terminated_at n, by { have : g.partial_denominators.nth n ≠ none, by simp [nth_part_denom_eq], exact (not_iff_not_of_iff terminated_at_iff_part_denom_none).elim_right this }, suffices : 1 / (B * nB) ≤ (1 : K) / (b * B * B), by { have : |v - g.convergents n| ≤ 1 / (B * nB), from abs_sub_convergents_le not_terminated_at_n, transitivity; assumption }, -- derive some inequalities needed to show the claim have zero_lt_B : 0 < B, by { have : (fib (n + 1) : K) ≤ B, from succ_nth_fib_le_of_nth_denom (or.inr $ mt (terminated_stable n.pred_le) not_terminated_at_n), exact (lt_of_lt_of_le (by exact_mod_cast (fib_pos (lt_of_le_of_ne n.succ.zero_le n.succ_ne_zero.symm))) this) }, have denoms_ineq : b * B * B ≤ B * nB, by { have : b * B ≤ nB, from le_of_succ_nth_denom nth_part_denom_eq, rwa [(mul_comm B nB), (mul_le_mul_right zero_lt_B)] }, have : (0 : K) < b * B * B, by { have : 0 < b, from lt_of_lt_of_le zero_lt_one (of_one_le_nth_part_denom nth_part_denom_eq), any_goals { repeat { apply mul_pos } }; assumption }, exact (div_le_div_of_le_left zero_le_one this denoms_ineq) end end error_term end generalized_continued_fraction
e3654ae99556d97e44344f7a62165b3b2b2cc958
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/pi/lex.lean
ebdda5aaa60c3efdaac3e7bcc096cb2c1f567cc2
[ "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
5,193
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import order.well_founded import algebra.group.pi import order.min_max /-! # Lexicographic order on Pi types This file defines the lexicographic order for Pi types. `a` is less than `b` if `a i = b i` for all `i` up to some point `k`, and `a k < b k`. ## Notation * `Πₗ i, α i`: Pi type equipped with the lexicographic order. Type synonym of `Π i, α i`. ## See also Related files are: * `data.finset.colex`: Colexicographic order on finite sets. * `data.list.lex`: Lexicographic order on lists. * `data.sigma.order`: Lexicographic order on `Σₗ i, α i`. * `data.psigma.order`: Lexicographic order on `Σₗ' i, α i`. * `data.prod.lex`: Lexicographic order on `α × β`. -/ variables {ι : Type*} {β : ι → Type*} (r : ι → ι → Prop) (s : Π {i}, β i → β i → Prop) namespace pi instance {α : Type*} : Π [inhabited α], inhabited (lex α) := id /-- The lexicographic relation on `Π i : ι, β i`, where `ι` is ordered by `r`, and each `β i` is ordered by `s`. -/ protected def lex (x y : Π i, β i) : Prop := ∃ i, (∀ j, r j i → x j = y j) ∧ s (x i) (y i) /- This unfortunately results in a type that isn't delta-reduced, so we keep the notation out of the basic API, just in case -/ notation `Πₗ` binders `, ` r:(scoped p, lex (Π i, p i)) := r @[simp] lemma to_lex_apply (x : Π i, β i) (i : ι) : to_lex x i = x i := rfl @[simp] lemma of_lex_apply (x : lex (Π i, β i)) (i : ι) : of_lex x i = x i := rfl lemma is_trichotomous_lex [∀ i, is_trichotomous (β i) s] (wf : well_founded r) : is_trichotomous (Π i, β i) (pi.lex r @s) := { trichotomous := λ a b, begin cases eq_or_ne a b with hab hab, { exact or.inr (or.inl hab) }, { rw function.ne_iff at hab, let i := wf.min _ hab, have hri : ∀ j, r j i → a j = b j, { intro j, rw ← not_imp_not, exact λ h', wf.not_lt_min _ _ h' }, have hne : a i ≠ b i, from wf.min_mem _ hab, cases trichotomous_of s (a i) (b i) with hi hi, exacts [or.inl ⟨i, hri, hi⟩, or.inr $ or.inr $ ⟨i, λ j hj, (hri j hj).symm, hi.resolve_left hne⟩] }, end } instance [has_lt ι] [Π a, has_lt (β a)] : has_lt (lex (Π i, β i)) := ⟨pi.lex (<) (λ _, (<))⟩ instance lex.is_strict_order [linear_order ι] [∀ a, partial_order (β a)] : is_strict_order (lex (Π i, β i)) (<) := { irrefl := λ a ⟨k, hk₁, hk₂⟩, lt_irrefl (a k) hk₂, trans := begin rintro a b c ⟨N₁, lt_N₁, a_lt_b⟩ ⟨N₂, lt_N₂, b_lt_c⟩, rcases lt_trichotomy N₁ N₂ with (H|rfl|H), exacts [⟨N₁, λ j hj, (lt_N₁ _ hj).trans (lt_N₂ _ $ hj.trans H), lt_N₂ _ H ▸ a_lt_b⟩, ⟨N₁, λ j hj, (lt_N₁ _ hj).trans (lt_N₂ _ hj), a_lt_b.trans b_lt_c⟩, ⟨N₂, λ j hj, (lt_N₁ _ (hj.trans H)).trans (lt_N₂ _ hj), (lt_N₁ _ H).symm ▸ b_lt_c⟩] end } instance [linear_order ι] [Π a, partial_order (β a)] : partial_order (lex (Π i, β i)) := partial_order_of_SO (<) /-- `Πₗ i, α i` is a linear order if the original order is well-founded. -/ noncomputable instance [linear_order ι] [is_well_order ι (<)] [∀ a, linear_order (β a)] : linear_order (lex (Π i, β i)) := @linear_order_of_STO (Πₗ i, β i) (<) { to_is_trichotomous := is_trichotomous_lex _ _ is_well_founded.wf } (classical.dec_rel _) lemma lex.le_of_forall_le [linear_order ι] [is_well_order ι (<)] [Π a, linear_order (β a)] {a b : lex (Π i, β i)} (h : ∀ i, a i ≤ b i) : a ≤ b := le_of_not_lt (λ ⟨i, hi⟩, (h i).not_lt hi.2) lemma lex.le_of_of_lex_le [linear_order ι] [is_well_order ι (<)] [Π a, linear_order (β a)] {a b : lex (Π i, β i)} (h : of_lex a ≤ of_lex b) : a ≤ b := lex.le_of_forall_le h lemma to_lex_monotone [linear_order ι] [is_well_order ι (<)] [Π a, linear_order (β a)] : monotone (@to_lex (Π i, β i)) := λ _ _, lex.le_of_forall_le instance [linear_order ι] [is_well_order ι (<)] [Π a, linear_order (β a)] [Π a, order_bot (β a)] : order_bot (lex (Π a, β a)) := { bot := to_lex ⊥, bot_le := λ f, lex.le_of_of_lex_le bot_le } instance [linear_order ι] [is_well_order ι (<)] [Π a, linear_order (β a)] [Π a, order_top (β a)] : order_top (lex (Π a, β a)) := { top := to_lex ⊤, le_top := λ f, lex.le_of_of_lex_le le_top } instance [linear_order ι] [is_well_order ι (<)] [Π a, linear_order (β a)] [Π a, bounded_order (β a)] : bounded_order (lex (Π a, β a)) := { .. pi.lex.order_bot, .. pi.lex.order_top } --we might want the analog of `pi.ordered_cancel_comm_monoid` as well in the future @[to_additive] instance lex.ordered_comm_group [linear_order ι] [∀ a, ordered_comm_group (β a)] : ordered_comm_group (lex (Π i, β i)) := { mul_le_mul_left := λ x y hxy z, hxy.elim (λ hxyz, hxyz ▸ le_rfl) (λ ⟨i, hi⟩, or.inr ⟨i, λ j hji, show z j * x j = z j * y j, by rw hi.1 j hji, mul_lt_mul_left' hi.2 _⟩), ..pi.lex.partial_order, ..pi.comm_group } end pi
45cecf19769d0aec9732dd403b946e89b4c0c30a
48ee71c79a2d430812d2a0c56d69480c8e22fd71
/InsideOut/Example.lean
593feea665d7918f1f1ceea83a43b5c35a57acbd
[ "MIT" ]
permissive
intsuc/inside-out
a49d08c04f992bdc28607345facd63597db28cab
5f14d511b8cab8703611d071027311f9967ec3a8
refs/heads/main
1,691,829,769,552
1,633,103,533,000
1,633,103,533,000
411,958,950
0
0
null
null
null
null
UTF-8
Lean
false
false
1,791
lean
import InsideOut.Elaborate notation Γ " | " Ψ " ⊢ " e => inferOutsideIn Γ Ψ e #eval IO.println "-- ok" #eval [] | [] ⊢ exp ff #eval [] | [] ⊢ exp abs x ⇒ #x ∷ bool #eval [] | [] ⊢ exp let f ∷ bool ⇒ bool ≔ abs x ⇒ #x; abs x ⇒ #f ◁ #x #eval [] | [] ⊢ exp let f ∷ bool ⇒ bool ⇒ bool ≔ abs x ⇒ abs y ⇒ #x; abs x ⇒ #f ◁ #x #eval [] | [] ⊢ exp let f ∷ bool ⇒ bool ⇒ bool ≔ abs x ⇒ abs y ⇒ #y; abs x ⇒ #f ◁ #x #eval [] | [] ⊢ exp let f ∷ bool ⇒ bool ⇒ bool ≔ abs x ⇒ abs y ⇒ #x; abs x ⇒ abs y ⇒ #f ◁ #x ◁ #y #eval [] | [] ⊢ exp (abs x ⇒ #x) ◁ ff #eval [] | [] ⊢ exp (abs x ⇒ ff) ◁ ff #eval [] | [] ⊢ exp (iff ff then abs x ⇒ #x else abs x ⇒ #x) ◁ ff #eval [] | [] ⊢ exp let f ∷ bool ⇒ bool ⇒ bool ≔ abs x ⇒ abs y ⇒ #x; (abs x ⇒ #f) ◁ ff #eval [] | [] ⊢ exp let f ∷ bool ⇒ bool ⇒ bool ≔ abs x ⇒ abs y ⇒ #x; (abs x ⇒ abs y ⇒ #f) ◁ ff ◁ ff #eval [] | [] ⊢ exp let f ∷ bool ⇒ bool ⇒ bool ≔ abs x ⇒ abs y ⇒ #x; (abs x ⇒ abs y ⇒ #f ◁ #y) ◁ ff #eval [] | [] ⊢ exp (abs x ⇒ ff) ∷ bool ⇒ ? #eval [] | [] ⊢ exp (abs x ⇒ #x) ∷ bool ⇒ ? #eval [] | [] ⊢ exp (abs x ⇒ #x) ∷ ? ⇒ bool #eval IO.println "-- error" #eval [] | [] ⊢ exp #x #eval [] | [] ⊢ exp iff abs x ⇒ #x then ff else ff #eval [] | [] ⊢ exp abs x ⇒ #x #eval [] | [] ⊢ exp abs x ⇒ ff #eval [] | [] ⊢ exp let f ∷ bool ⇒ bool ⇒ bool ≔ abs x ⇒ abs y ⇒ #x; abs x ⇒ #f ◁ ff #eval [] | [] ⊢ exp let f ∷ bool ⇒ bool ⇒ bool ≔ abs x ⇒ abs y ⇒ #x; abs x ⇒ abs y ⇒ #f ◁ #x ◁ #x #eval [] | [] ⊢ exp (abs x ⇒ #x) ∷ ? ⇒ ? -- TODO: should be error
f96e57f46961678ecd7f2572e90651e5e19eea18
d6124c8dbe5661dcc5b8c9da0a56fbf1f0480ad6
/Papyrus/FFI.lean
7f5e804267033bb9d36b4d7905195d79b8f4ec36
[ "Apache-2.0" ]
permissive
xubaiw/lean4-papyrus
c3fbbf8ba162eb5f210155ae4e20feb2d32c8182
02e82973a5badda26fc0f9fd15b3d37e2eb309e0
refs/heads/master
1,691,425,756,824
1,632,122,825,000
1,632,123,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,645
lean
namespace Papyrus /-- An object which defines the kind of an external pointer. -/ constant ExternalPtrClass : Type := Unit /-- A Lean object which contains an external pointer. -/ constant ExternalPtr : ExternalPtrClass → Type → Type := fun _ _ => Unit /-- The external class of LoosePtr. -/ axiom LoosePtr.class : ExternalPtrClass noncomputable instance : Inhabited ExternalPtrClass := ⟨LoosePtr.class⟩ /-- An external pointer with no memory management. -/ abbrev LoosePtr (α) := ExternalPtr Papyrus.LoosePtr.class α /-- The external class of LoosePtr. -/ axiom OwnedPtr.class : ExternalPtrClass /-- A external pointer that is deleted upon being garbage collected (i.e., it is owned by Lean). -/ abbrev OwnedPtr (α) := ExternalPtr Papyrus.OwnedPtr.class α /-- The actual implementation of `LinkedPtr`. -/ structure LinkedPtrImpl (k : ExternalPtrClass) (σ) (α) where link : σ this : ExternalPtr k α /-- A external pointer whose lifetime is linked to some other external object that should not be deleted until this one is garbage collected by Lean. It as an opaque definition to prevent access to the internal `ExternalPtr`, which could create memory mismanagement (e.g., if a reference to it is kept without keeping a reference to the `LinkedPtr`). -/ constant LinkedPtr (k : ExternalPtrClass) (σ : Type) (α : Type) : Type := LinkedPtrImpl k σ α /-- A linked external pointer that should not be managed by Lean. -/ abbrev LinkedLoosePtr := LinkedPtr LoosePtr.class /-- A linked external pointer that can be deleted independently by Lean. -/ abbrev LinkedOwnedPtr := LinkedPtr OwnedPtr.class
e7ce7d897cb074dc71fd6c464a53556b9801c087
4efff1f47634ff19e2f786deadd394270a59ecd2
/test/simps.lean
f81e6fb6def5aaed32dabde69792ef50906c46db
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
19,931
lean
import tactic.simps -- set_option trace.simps.verbose true -- set_option trace.app_builder true open function tactic expr structure equiv (α : Sort*) (β : Sort*) := (to_fun : α → β) (inv_fun : β → α) (left_inv : left_inverse inv_fun to_fun) (right_inv : right_inverse inv_fun to_fun) local infix ` ≃ `:25 := equiv /- Since `prod` and `pprod` are a special case for `@[simps]`, we define a new structure to test the basic functionality.-/ structure my_prod (α β : Type*) := (fst : α) (snd : β) def myprod.map {α α' β β'} (f : α → α') (g : β → β') (x : my_prod α β) : my_prod α' β' := ⟨f x.1, g x.2⟩ namespace foo @[simps] protected def rfl {α} : α ≃ α := ⟨id, λ x, x, λ x, rfl, λ x, rfl⟩ /- simps adds declarations -/ run_cmd do e ← get_env, e.get `foo.rfl_to_fun, e.get `foo.rfl_inv_fun, success_if_fail (e.get `foo.rfl_left_inv), success_if_fail (e.get `foo.rfl_right_inv) example (n : ℕ) : foo.rfl.to_fun n = n := by rw [foo.rfl_to_fun, id] example (n : ℕ) : foo.rfl.inv_fun n = n := by rw [foo.rfl_inv_fun] /- the declarations are simp-lemmas -/ @[simps] def foo : ℕ × ℤ := (1, 2) example : foo.1 = 1 := by simp example : foo.2 = 2 := by simp example : foo.1 = 1 := by { dsimp, refl } -- check that dsimp also unfolds example : foo.2 = 2 := by { dsimp, refl } example {α} (x : α) : foo.rfl.to_fun x = x := by simp example {α} (x : α) : foo.rfl.inv_fun x = x := by simp example {α} (x : α) : foo.rfl.to_fun = @id α := by { success_if_fail {simp}, refl } /- check some failures -/ def bar1 : ℕ := 1 -- type is not a structure def bar2 : ℕ × ℤ := prod.map (λ x, x + 2) (λ y, y - 3) (3, 4) -- value is not a constructor noncomputable def bar3 {α} : α ≃ α := classical.choice ⟨foo.rfl⟩ run_cmd do success_if_fail_with_msg (simps_tac `foo.bar1) "Invalid `simps` attribute. Target is not a structure", success_if_fail_with_msg (simps_tac `foo.bar2) "Invalid `simps` attribute. The body is not a constructor application: prod.map (λ (x : ℕ), x + 2) (λ (y : ℤ), y - 3) (3, 4) Possible solution: add option {rhs_md := semireducible}.", success_if_fail_with_msg (simps_tac `foo.bar3) "Invalid `simps` attribute. The body is not a constructor application: classical.choice bar3._proof_1 Possible solution: add option {rhs_md := semireducible}.", e ← get_env, let nm := `foo.bar1, d ← e.get nm, let lhs : expr := const d.to_name (d.univ_params.map level.param), simps_add_projections e nm "" d.type lhs d.value [] d.univ_params ff {} [] /- test `rhs_md` option -/ def rfl2 {α} : α ≃ α := foo.rfl run_cmd success_if_fail (simps_tac `foo.rfl2) attribute [simps {rhs_md := semireducible}] foo.rfl2 /- test `fully_applied` option -/ @[simps {fully_applied := ff}] def rfl3 {α} : α ≃ α := ⟨id, λ x, x, λ x, rfl, λ x, rfl⟩ end foo /- we reduce the type when applying [simps] -/ def my_equiv := equiv @[simps] def baz : my_equiv ℕ ℕ := ⟨id, λ x, x, λ x, rfl, λ x, rfl⟩ /- test name clashes -/ def name_clash_fst := 1 def name_clash_snd := 1 def name_clash_snd_2 := 1 @[simps] def name_clash := (2, 3) run_cmd do e ← get_env, e.get `name_clash_fst_2, e.get `name_clash_snd_3 /- check projections for nested structures -/ namespace count_nested @[simps {attrs := [`simp, `norm]}] def nested1 : my_prod ℕ $ my_prod ℤ ℕ := ⟨2, -1, 1⟩ @[simps {attrs := []}] def nested2 : ℕ × my_prod ℕ ℕ := ⟨2, myprod.map nat.succ nat.pred ⟨1, 2⟩⟩ end count_nested run_cmd do e ← get_env, e.get `count_nested.nested1_fst, e.get `count_nested.nested1_snd_fst, e.get `count_nested.nested1_snd_snd, e.get `count_nested.nested2_fst, e.get `count_nested.nested2_snd, is_simp_lemma `count_nested.nested1_fst >>= λ b, guard b, -- simp attribute is global is_simp_lemma `count_nested.nested2_fst >>= λ b, guard $ ¬b, --lemmas_only doesn't add simp lemma guard $ 7 = e.fold 0 -- there are no other lemmas generated (λ d n, n + if d.to_name.components.init.ilast = `count_nested then 1 else 0) -- testing with arguments @[simps] def bar {α : Type*} (n m : ℕ) : ℕ × ℤ := ⟨n - m, n + m⟩ structure equiv_plus_data (α β) extends α ≃ β := (P : (α → β) → Prop) (data : P to_fun) structure automorphism_plus_data α extends α ⊕ α ≃ α ⊕ α := (P : (α ⊕ α → α ⊕ α) → Prop) (data : P to_fun) (extra : bool → my_prod ℕ ℕ) @[simps] def refl_with_data {α} : equiv_plus_data α α := { P := λ f, f = id, data := rfl, ..foo.rfl } @[simps] def refl_with_data' {α} : equiv_plus_data α α := { P := λ f, f = id, data := rfl, to_equiv := foo.rfl } /- test whether eta expansions are reduced correctly -/ @[simps] def test {α} : automorphism_plus_data α := { P := λ f, f = id, data := rfl, extra := λ b, ⟨(⟨3, 5⟩ : my_prod _ _).1, (⟨3, 5⟩ : my_prod _ _).2⟩, ..foo.rfl } /- test whether this is indeed rejected as a valid eta expansion -/ @[simps] def test_sneaky {α} : automorphism_plus_data α := { P := λ f, f = id, data := rfl, extra := λ b, ⟨(3,5).1,(3,5).2⟩, ..foo.rfl } run_cmd do e ← get_env, e.get `refl_with_data_to_equiv, e.get `refl_with_data'_to_equiv, e.get `test_extra, e.get `test_sneaky_extra_fst, success_if_fail (e.get `refl_with_data_to_equiv_to_fun), success_if_fail (e.get `refl_with_data'_to_equiv_to_fun), success_if_fail (e.get `test_extra_fst), success_if_fail (e.get `test_sneaky_extra) structure partially_applied_str := (data : ℕ → my_prod ℕ ℕ) /- if we have a partially applied constructor, we treat it as if it were eta-expanded -/ @[simps] def partially_applied_term : partially_applied_str := ⟨my_prod.mk 3⟩ run_cmd do e ← get_env, e.get `partially_applied_term_data_fst, e.get `partially_applied_term_data_snd structure very_partially_applied_str := (data : ∀β, ℕ → β → my_prod ℕ β) /- if we have a partially applied constructor, we treat it as if it were eta-expanded -/ @[simps] -- def very_partially_applied_term : very_partially_applied_str := ⟨@my_prod.mk ℕ⟩ def very_partially_applied_term : very_partially_applied_str := ⟨λ x y z, my_prod.mk y z⟩ run_cmd do e ← get_env, e.get `very_partially_applied_term_data_fst, e.get `very_partially_applied_term_data_snd @[simps] def let1 : ℕ × ℤ := let n := 3 in ⟨n + 4, 5⟩ @[simps] def let2 : ℕ × ℤ := let n := 3, m := 4 in let k := 5 in ⟨n + m, k⟩ @[simps] def let3 : ℕ → ℕ × ℤ := λ n, let m := 4, k := 5 in ⟨n + m, k⟩ @[simps] def let4 : ℕ → ℕ × ℤ := let m := 4, k := 5 in λ n, ⟨n + m, k⟩ run_cmd do e ← get_env, e.get `let1_fst, e.get `let2_fst, e.get `let3_fst, e.get `let4_fst, e.get `let1_snd, e.get `let2_snd, e.get `let3_snd, e.get `let4_snd namespace specify @[simps fst] def specify1 : ℕ × ℕ × ℕ := (1, 2, 3) @[simps snd] def specify2 : ℕ × ℕ × ℕ := (1, 2, 3) @[simps snd_fst] def specify3 : ℕ × ℕ × ℕ := (1, 2, 3) @[simps snd snd_snd snd_snd] def specify4 : ℕ × ℕ × ℕ := (1, 2, 3) -- last argument is ignored @[simps] def specify5 : ℕ × ℕ × ℕ := (1, prod.map (λ x, x) (λ y, y) (2, 3)) end specify run_cmd do e ← get_env, e.get `specify.specify1_fst, e.get `specify.specify2_snd, e.get `specify.specify3_snd_fst, e.get `specify.specify4_snd_snd, e.get `specify.specify4_snd, e.get `specify.specify5_fst, e.get `specify.specify5_snd, guard $ 12 = e.fold 0 -- there are no other lemmas generated (λ d n, n + if d.to_name.components.init.ilast = `specify then 1 else 0), success_if_fail_with_msg (simps_tac `specify.specify1 {} ["fst_fst"]) "Invalid simp-lemma specify.specify1_fst_fst. Projection fst doesn't exist, because target is not a structure.", success_if_fail_with_msg (simps_tac `specify.specify1 {} ["foo_fst"]) "Invalid simp-lemma specify.specify1_foo_fst. Projection foo doesn't exist.", success_if_fail_with_msg (simps_tac `specify.specify1 {} ["snd_bar"]) "Invalid simp-lemma specify.specify1_snd_bar. Projection bar doesn't exist.", success_if_fail_with_msg (simps_tac `specify.specify5 {} ["snd_snd"]) "Invalid simp-lemma specify.specify5_snd_snd. The given definition is not a constructor application: prod.map (λ (x : ℕ), x) (λ (y : ℕ), y) (2, 3) Possible solution: add option {rhs_md := semireducible}." /- We also eta-reduce if we explicitly specify the projection. -/ attribute [simps extra] test run_cmd do e ← get_env, d1 ← e.get `test_extra, d2 ← e.get `test_extra_2, guard $ d1.type =ₐ d2.type, skip /- check short_name option -/ @[simps {short_name := tt}] def short_name1 : my_prod ℕ ℕ × my_prod ℕ ℕ := ⟨⟨1, 2⟩, 3, 4⟩ run_cmd do e ← get_env, e.get `short_name1_fst, e.get `short_name1_fst_2, e.get `short_name1_snd, e.get `short_name1_snd_2 /- check simp_rhs option -/ @[simps {simp_rhs := tt}] def equiv.trans {α β γ} (f : α ≃ β) (g : β ≃ γ) : α ≃ γ := ⟨g.to_fun ∘ f.to_fun, f.inv_fun ∘ g.inv_fun, by { intro x, simp [equiv.left_inv _ _] }, by { intro x, simp [equiv.right_inv _ _] }⟩ example {α β γ : Type} (f : α ≃ β) (g : β ≃ γ) (x : α) : (f.trans g).to_fun x = (f.trans g).to_fun x := begin dsimp only [equiv.trans_to_fun], guard_target g.to_fun (f.to_fun x) = g.to_fun (f.to_fun x), refl, end local attribute [simp] nat.zero_add nat.one_mul nat.mul_one @[simps {simp_rhs := tt}] def my_nat_equiv : ℕ ≃ ℕ := ⟨λ n, 0 + n, λ n, 1 * n * 1, by { intro n, simp }, by { intro n, simp }⟩ run_cmd success_if_fail (has_attribute `_refl_lemma `my_nat_equiv_to_fun) >> has_attribute `_refl_lemma `equiv.trans_to_fun example (n : ℕ) : my_nat_equiv.to_fun (my_nat_equiv.to_fun $ my_nat_equiv.inv_fun n) = n := by { success_if_fail { refl }, simp only [my_nat_equiv_to_fun, my_nat_equiv_inv_fun] } @[simps {simp_rhs := tt}] def succeed_without_simplification_possible : ℕ ≃ ℕ := ⟨λ n, n, λ n, n, by { intro n, refl }, by { intro n, refl }⟩ /- test that we don't recursively take projections of `prod` and `pprod` -/ @[simps] def pprod_equiv_prod : pprod ℕ ℕ ≃ ℕ × ℕ := { to_fun := λ x, ⟨x.1, x.2⟩, inv_fun := λ x, ⟨x.1, x.2⟩, left_inv := λ ⟨x, y⟩, rfl, right_inv := λ ⟨x, y⟩, rfl } run_cmd do e ← get_env, e.get `pprod_equiv_prod_to_fun, e.get `pprod_equiv_prod_inv_fun attribute [simps to_fun_fst inv_fun_snd] pprod_equiv_prod run_cmd do e ← get_env, e.get `pprod_equiv_prod_to_fun_fst, e.get `pprod_equiv_prod_inv_fun_snd /- Tests with universe levels -/ universe variables v u class has_hom (obj : Type u) : Type (max u (v+1)) := (hom : obj → obj → Type v) infixr ` ⟶ `:10 := has_hom.hom -- type as \h section prio set_option default_priority 100 class category_struct (obj : Type u) extends has_hom.{v} obj : Type (max u (v+1)) := (id : Π X : obj, hom X X) (comp : Π {X Y Z : obj}, (X ⟶ Y) → (Y ⟶ Z) → (X ⟶ Z)) end prio notation `𝟙` := category_struct.id -- type as \b1 infixr ` ≫ `:80 := category_struct.comp -- type as \gg @[simps] instance types : category_struct (Type u) := { hom := λ a b, (a → b), id := λ a, id, comp := λ _ _ _ f g, g ∘ f } example (X : Type u) : (X ⟶ X) = (X → X) := by simp example (X : Type u) : 𝟙 X = (λ x, x) := by { funext, simp } example (X Y Z : Type u) (f : X ⟶ Y) (g : Y ⟶ Z) : f ≫ g = g ∘ f := by { funext, simp } namespace coercing structure foo_str := (c : Type) (x : c) instance : has_coe_to_sort foo_str := ⟨_, foo_str.c⟩ @[simps] def foo : foo_str := ⟨ℕ, 3⟩ @[simps] def foo2 : foo_str := ⟨ℕ, 34⟩ example : ↥foo = ℕ := by simp only [foo_c] example : foo.x = (3 : ℕ) := by simp only [foo_x] structure voo_str (n : ℕ) := (c : Type) (x : c) instance has_coe_voo_str (n : ℕ) : has_coe_to_sort (voo_str n) := ⟨_, voo_str.c⟩ @[simps] def voo : voo_str 7 := ⟨ℕ, 3⟩ @[simps] def voo2 : voo_str 4 := ⟨ℕ, 34⟩ example : ↥voo = ℕ := by simp only [voo_c] example : voo.x = (3 : ℕ) := by simp only [voo_x] structure equiv2 (α : Sort*) (β : Sort*) := (to_fun : α → β) (inv_fun : β → α) (left_inv : left_inverse inv_fun to_fun) (right_inv : right_inverse inv_fun to_fun) instance {α β} : has_coe_to_fun $ equiv2 α β := ⟨_, equiv2.to_fun⟩ @[simps] protected def rfl2 {α} : equiv2 α α := ⟨λ x, x, λ x, x, λ x, rfl, λ x, rfl⟩ example {α} (x : α) : coercing.rfl2 x = x := by rw [coercing.rfl2_to_fun] example {α} (x : α) : coercing.rfl2 x = x := by simp example {α} (x : α) : coercing.rfl2.inv_fun x = x := by simp @[simps] protected def equiv2.symm {α β} (f : equiv2 α β) : equiv2 β α := ⟨f.inv_fun, f, f.right_inv, f.left_inv⟩ @[simps] protected def equiv2.symm2 {α β} (f : equiv2 α β) : equiv2 β α := ⟨f.inv_fun, f.to_fun, f.right_inv, f.left_inv⟩ /- we can use the `md` attribute to not unfold the `has_coe_to_fun` attribute, so that `@[simps]` doesn't recognize that the type of `⇑f` is still a function type. -/ @[simps {type_md := reducible}] protected def equiv2.symm3 {α β} (f : equiv2 α β) : equiv2 β α := ⟨f.inv_fun, f, f.right_inv, f.left_inv⟩ example {α β} (f : equiv2 α β) (y : β) : f.symm y = f.inv_fun y := by simp example {α β} (f : equiv2 α β) (x : α) : f.symm.inv_fun x = f x := by simp example {α β} (f : equiv2 α β) : f.symm.inv_fun = f := by { success_if_fail {simp}, refl } example {α β} (f : equiv2 α β) : f.symm3.inv_fun = f := by simp section set_option old_structure_cmd true class semigroup (G : Type u) extends has_mul G := (mul_assoc : ∀ a b c : G, a * b * c = a * (b * c)) end @[simps] instance {α β} [semigroup α] [semigroup β] : semigroup (α × β) := { mul := λ x y, (x.1 * y.1, x.2 * y.2), mul_assoc := by { intros, simp only [semigroup.mul_assoc], refl } } example {α β} [semigroup α] [semigroup β] (x y : α × β) : x * y = (x.1 * y.1, x.2 * y.2) := by simp example {α β} [semigroup α] [semigroup β] (x y : α × β) : (x * y).1 = x.1 * y.1 := by simp structure Semigroup := (G : Type*) (op : G → G → G) (infix * := op) (op_assoc : ∀ (x y z : G), (x * y) * z = x * (y * z)) namespace Group instance : has_coe_to_sort Semigroup := ⟨_, Semigroup.G⟩ instance (G : Semigroup) : has_mul G := ⟨G.op⟩ @[simps] def prod_Semigroup (G H : Semigroup) : Semigroup := { G := G × H, op := λ x y, (x.1 * y.1, x.2 * y.2), op_assoc := by { intros, dsimp [Group.has_mul], simp [Semigroup.op_assoc] }} end Group section set_option old_structure_cmd true class extending_stuff (G : Type u) extends has_mul G, has_zero G, has_neg G, has_subset G := (new_axiom : ∀ x : G, x * - 0 ⊆ - x) end @[simps] def bar : extending_stuff ℕ := { mul := (*), zero := 0, neg := nat.succ, subset := λ x y, true, new_axiom := λ x, trivial } section local attribute [instance] bar example (x : ℕ) : x * - 0 ⊆ - x := by simp end class new_extending_stuff (G : Type u) extends has_mul G, has_zero G, has_neg G, has_subset G := (new_axiom : ∀ x : G, x * - 0 ⊆ - x) @[simps] def new_bar : new_extending_stuff ℕ := { mul := (*), zero := 0, neg := nat.succ, subset := λ x y, true, new_axiom := λ x, trivial } section local attribute [instance] new_bar example (x : ℕ) : x * - 0 ⊆ - x := by simp end end coercing namespace manual_coercion structure equiv (α : Sort*) (β : Sort*) := (to_fun : α → β) (inv_fun : β → α) local infix ` ≃ `:25 := manual_coercion.equiv variables {α β γ : Sort*} instance : has_coe_to_fun $ α ≃ β := ⟨_, equiv.to_fun⟩ def equiv.symm (e : α ≃ β) : β ≃ α := ⟨e.inv_fun, e.to_fun⟩ /-- See Note [custom simps projection] -/ def equiv.simps.inv_fun (e : α ≃ β) : β → α := e.symm /-- Composition of equivalences `e₁ : α ≃ β` and `e₂ : β ≃ γ`. -/ @[simps {simp_rhs := tt}] protected def equiv.trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ := ⟨e₂ ∘ e₁, e₁.symm ∘ e₂.symm⟩ end manual_coercion namespace failty_manual_coercion variables {α β γ : Sort*} structure equiv (α : Sort*) (β : Sort*) := (to_fun : α → β) (inv_fun : β → α) local infix ` ≃ `:25 := failty_manual_coercion.equiv /-- See Note [custom simps projection] -/ noncomputable def equiv.simps.inv_fun (e : α ≃ β) : β → α := classical.choice ⟨e.inv_fun⟩ run_cmd do e ← get_env, success_if_fail (simps_get_raw_projections e `faulty_manual_coercion.equiv) end failty_manual_coercion namespace manual_initialize /- defining a manual coercion. This should be made more easily. -/ variables {α β γ : Sort*} structure equiv (α : Sort*) (β : Sort*) := (to_fun : α → β) (inv_fun : β → α) local infix ` ≃ `:25 := manual_initialize.equiv instance : has_coe_to_fun $ α ≃ β := ⟨_, equiv.to_fun⟩ def equiv.symm (e : α ≃ β) : β ≃ α := ⟨e.inv_fun, e.to_fun⟩ /-- See Note [custom simps projection] -/ -- test: intentionally using different unvierse levels for equiv.symm than for equiv def equiv.simps.inv_fun (e : α ≃ β) : β → α := e.symm initialize_simps_projections equiv run_cmd has_attribute `_simps_str `manual_initialize.equiv /-- Composition of equivalences `e₁ : α ≃ β` and `e₂ : β ≃ γ`. -/ @[simps {simp_rhs := tt}] protected def equiv.trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ := ⟨e₂ ∘ e₁, e₁.symm ∘ e₂.symm⟩ end manual_initialize -- test transparency setting structure set_plus (α : Type) := (s : set α) (x : α) (h : x ∈ s) @[simps] def nat_set_plus : set_plus ℕ := ⟨set.univ, 1, trivial⟩ example : nat_set_plus.s = set.univ := begin dsimp only [nat_set_plus_s], guard_target @set.univ ℕ = set.univ, refl end @[simps {type_md := semireducible}] def nat_set_plus2 : set_plus ℕ := ⟨set.univ, 1, trivial⟩ example : nat_set_plus2.s = set.univ := begin success_if_fail { dsimp only [nat_set_plus2_s] }, refl end @[simps {rhs_md := semireducible}] def nat_set_plus3 : set_plus ℕ := nat_set_plus example : nat_set_plus3.s = set.univ := begin dsimp only [nat_set_plus3_s], guard_target @set.univ ℕ = set.univ, refl end namespace nested_non_fully_applied structure equiv (α : Sort*) (β : Sort*) := (to_fun : α → β) (inv_fun : β → α) local infix ` ≃ `:25 := nested_non_fully_applied.equiv variables {α β γ : Sort*} @[simps] def equiv.symm (e : α ≃ β) : β ≃ α := ⟨e.inv_fun, e.to_fun⟩ @[simps {rhs_md := semireducible, fully_applied := ff}] def equiv.symm2 : (α ≃ β) ≃ (β ≃ α) := ⟨equiv.symm, equiv.symm⟩ example (e : α ≃ β) : (equiv.symm2.inv_fun e).to_fun = e.inv_fun := begin dsimp only [equiv.symm2_inv_fun_to_fun], guard_target e.inv_fun = e.inv_fun, refl end /- do not prematurely unfold `equiv.symm`, unless necessary -/ @[simps to_fun to_fun_to_fun {rhs_md := semireducible}] def equiv.symm3 : (α ≃ β) ≃ (β ≃ α) := equiv.symm2 example (e : α ≃ β) (y : β) : (equiv.symm3.to_fun e).to_fun y = e.inv_fun y ∧ (equiv.symm3.to_fun e).to_fun y = e.inv_fun y := begin split, { dsimp only [equiv.symm3_to_fun], guard_target e.symm.to_fun y = e.inv_fun y, refl }, { dsimp only [equiv.symm3_to_fun_to_fun], guard_target e.inv_fun y = e.inv_fun y, refl } end end nested_non_fully_applied /- fail if you add an attribute with a parameter. -/ run_cmd success_if_fail $ simps_tac `foo.rfl { attrs := [`higher_order] } -- test that type classes which are props work class prop_class (n : ℕ) : Prop := (has_true : true) instance has_prop_class (n : ℕ) : prop_class n := ⟨trivial⟩ structure needs_prop_class (n : ℕ) [prop_class n] := (t : true) @[simps] def test_prop_class : needs_prop_class 1 := { t := trivial }
400d57c13dde878fc9e1dba1f56b15846ff232b1
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/meta/rb_map.lean
9ec7d05d674bcc1369e178ef3a7522c3a0932ff1
[]
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
434
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.ordering.basic import Mathlib.Lean3Lib.init.function import Mathlib.Lean3Lib.init.meta.name import Mathlib.Lean3Lib.init.meta.format import Mathlib.Lean3Lib.init.control.monad namespace Mathlib
28a81ac02d7f9941f29053d2f69ccefbbc068a50
4fa161becb8ce7378a709f5992a594764699e268
/src/data/matrix/basic.lean
696cab201a950648fcfc42313fa9b24317351767
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
19,717
lean
/- Copyright (c) 2018 Ellen Arlt. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ellen Arlt, Blair Shi, Sean Leather, Mario Carneiro, Johan Commelin -/ import algebra.pi_instances /-! # Matrices -/ universes u v w open_locale big_operators @[nolint unused_arguments] def matrix (m n : Type u) [fintype m] [fintype n] (α : Type v) : Type (max u v) := m → n → α namespace matrix variables {l m n o : Type u} [fintype l] [fintype m] [fintype n] [fintype o] variables {α : Type v} section ext variables {M N : matrix m n α} theorem ext_iff : (∀ i j, M i j = N i j) ↔ M = N := ⟨λ h, funext $ λ i, funext $ h i, λ h, by simp [h]⟩ @[ext] theorem ext : (∀ i j, M i j = N i j) → M = N := ext_iff.mp end ext def transpose (M : matrix m n α) : matrix n m α | x y := M y x localized "postfix `ᵀ`:1500 := matrix.transpose" in matrix def col (w : m → α) : matrix m punit α | x y := w x def row (v : n → α) : matrix punit n α | x y := v y instance [inhabited α] : inhabited (matrix m n α) := pi.inhabited _ instance [has_add α] : has_add (matrix m n α) := pi.has_add instance [add_semigroup α] : add_semigroup (matrix m n α) := pi.add_semigroup instance [add_comm_semigroup α] : add_comm_semigroup (matrix m n α) := pi.add_comm_semigroup instance [has_zero α] : has_zero (matrix m n α) := pi.has_zero instance [add_monoid α] : add_monoid (matrix m n α) := pi.add_monoid instance [add_comm_monoid α] : add_comm_monoid (matrix m n α) := pi.add_comm_monoid instance [has_neg α] : has_neg (matrix m n α) := pi.has_neg instance [add_group α] : add_group (matrix m n α) := pi.add_group instance [add_comm_group α] : add_comm_group (matrix m n α) := pi.add_comm_group @[simp] theorem zero_val [has_zero α] (i j) : (0 : matrix m n α) i j = 0 := rfl @[simp] theorem neg_val [has_neg α] (M : matrix m n α) (i j) : (- M) i j = - M i j := rfl @[simp] theorem add_val [has_add α] (M N : matrix m n α) (i j) : (M + N) i j = M i j + N i j := rfl section diagonal variables [decidable_eq n] /-- `diagonal d` is the square matrix such that `(diagonal d) i i = d i` and `(diagonal d) i j = 0` if `i ≠ j`. -/ def diagonal [has_zero α] (d : n → α) : matrix n n α := λ i j, if i = j then d i else 0 @[simp] theorem diagonal_val_eq [has_zero α] {d : n → α} (i : n) : (diagonal d) i i = d i := by simp [diagonal] @[simp] theorem diagonal_val_ne [has_zero α] {d : n → α} {i j : n} (h : i ≠ j) : (diagonal d) i j = 0 := by simp [diagonal, h] theorem diagonal_val_ne' [has_zero α] {d : n → α} {i j : n} (h : j ≠ i) : (diagonal d) i j = 0 := diagonal_val_ne h.symm @[simp] theorem diagonal_zero [has_zero α] : (diagonal (λ _, 0) : matrix n n α) = 0 := by simp [diagonal]; refl @[simp] lemma diagonal_transpose [has_zero α] (v : n → α) : (diagonal v)ᵀ = diagonal v := begin ext i j, by_cases h : i = j, { simp [h, transpose] }, { simp [h, transpose, diagonal_val_ne' h] } end @[simp] theorem diagonal_add [add_monoid α] (d₁ d₂ : n → α) : diagonal d₁ + diagonal d₂ = diagonal (λ i, d₁ i + d₂ i) := by ext i j; by_cases h : i = j; simp [h] section one variables [has_zero α] [has_one α] instance : has_one (matrix n n α) := ⟨diagonal (λ _, 1)⟩ @[simp] theorem diagonal_one : (diagonal (λ _, 1) : matrix n n α) = 1 := rfl theorem one_val {i j} : (1 : matrix n n α) i j = if i = j then 1 else 0 := rfl @[simp] theorem one_val_eq (i) : (1 : matrix n n α) i i = 1 := diagonal_val_eq i @[simp] theorem one_val_ne {i j} : i ≠ j → (1 : matrix n n α) i j = 0 := diagonal_val_ne theorem one_val_ne' {i j} : j ≠ i → (1 : matrix n n α) i j = 0 := diagonal_val_ne' end one section numeral @[simp] lemma bit0_val [has_add α] (M : matrix m m α) (i : m) (j : m) : (bit0 M) i j = bit0 (M i j) := rfl variables [add_monoid α] [has_one α] lemma bit1_val (M : matrix n n α) (i : n) (j : n) : (bit1 M) i j = if i = j then bit1 (M i j) else bit0 (M i j) := by dsimp [bit1]; by_cases h : i = j; simp [h] @[simp] lemma bit1_val_eq (M : matrix n n α) (i : n) : (bit1 M) i i = bit1 (M i i) := by simp [bit1_val] @[simp] lemma bit1_val_ne (M : matrix n n α) {i j : n} (h : i ≠ j) : (bit1 M) i j = bit0 (M i j) := by simp [bit1_val, h] end numeral end diagonal section dot_product /-- `dot_product v w` is the sum of the entrywise products `v i * w i` -/ def dot_product [has_mul α] [add_comm_monoid α] (v w : m → α) : α := ∑ i, v i * w i lemma dot_product_assoc [semiring α] (u : m → α) (v : m → n → α) (w : n → α) : dot_product (λ j, dot_product u (λ i, v i j)) w = dot_product u (λ i, dot_product (v i) w) := by simpa [dot_product, finset.mul_sum, finset.sum_mul, mul_assoc] using finset.sum_comm lemma dot_product_comm [comm_semiring α] (v w : m → α) : dot_product v w = dot_product w v := by simp_rw [dot_product, mul_comm] @[simp] lemma dot_product_punit [add_comm_monoid α] [has_mul α] (v w : punit → α) : dot_product v w = v ⟨⟩ * w ⟨⟩ := by simp [dot_product] @[simp] lemma dot_product_zero [semiring α] (v : m → α) : dot_product v 0 = 0 := by simp [dot_product] @[simp] lemma dot_product_zero' [semiring α] (v : m → α) : dot_product v (λ _, 0) = 0 := dot_product_zero v @[simp] lemma zero_dot_product [semiring α] (v : m → α) : dot_product 0 v = 0 := by simp [dot_product] @[simp] lemma zero_dot_product' [semiring α] (v : m → α) : dot_product (λ _, (0 : α)) v = 0 := zero_dot_product v @[simp] lemma add_dot_product [semiring α] (u v w : m → α) : dot_product (u + v) w = dot_product u w + dot_product v w := by simp [dot_product, add_mul, finset.sum_add_distrib] @[simp] lemma dot_product_add [semiring α] (u v w : m → α) : dot_product u (v + w) = dot_product u v + dot_product u w := by simp [dot_product, mul_add, finset.sum_add_distrib] @[simp] lemma diagonal_dot_product [decidable_eq m] [semiring α] (v w : m → α) (i : m) : dot_product (diagonal v i) w = v i * w i := have ∀ j ≠ i, diagonal v i j * w j = 0 := λ j hij, by simp [diagonal_val_ne' hij], by convert finset.sum_eq_single i (λ j _, this j) _; simp @[simp] lemma dot_product_diagonal [decidable_eq m] [semiring α] (v w : m → α) (i : m) : dot_product v (diagonal w i) = v i * w i := have ∀ j ≠ i, v j * diagonal w i j = 0 := λ j hij, by simp [diagonal_val_ne' hij], by convert finset.sum_eq_single i (λ j _, this j) _; simp @[simp] lemma dot_product_diagonal' [decidable_eq m] [semiring α] (v w : m → α) (i : m) : dot_product v (λ j, diagonal w j i) = v i * w i := have ∀ j ≠ i, v j * diagonal w j i = 0 := λ j hij, by simp [diagonal_val_ne hij], by convert finset.sum_eq_single i (λ j _, this j) _; simp @[simp] lemma neg_dot_product [ring α] (v w : m → α) : dot_product (-v) w = - dot_product v w := by simp [dot_product] @[simp] lemma dot_product_neg [ring α] (v w : m → α) : dot_product v (-w) = - dot_product v w := by simp [dot_product] @[simp] lemma smul_dot_product [semiring α] (x : α) (v w : m → α) : dot_product (x • v) w = x * dot_product v w := by simp [dot_product, finset.mul_sum, mul_assoc] @[simp] lemma dot_product_smul [comm_semiring α] (x : α) (v w : m → α) : dot_product v (x • w) = x * dot_product v w := by simp [dot_product, finset.mul_sum, mul_assoc, mul_comm, mul_left_comm] end dot_product protected def mul [has_mul α] [add_comm_monoid α] (M : matrix l m α) (N : matrix m n α) : matrix l n α := λ i k, dot_product (λ j, M i j) (λ j, N j k) localized "infixl ` ⬝ `:75 := matrix.mul" in matrix theorem mul_val [has_mul α] [add_comm_monoid α] {M : matrix l m α} {N : matrix m n α} {i k} : (M ⬝ N) i k = ∑ j, M i j * N j k := rfl instance [has_mul α] [add_comm_monoid α] : has_mul (matrix n n α) := ⟨matrix.mul⟩ @[simp] theorem mul_eq_mul [has_mul α] [add_comm_monoid α] (M N : matrix n n α) : M * N = M ⬝ N := rfl theorem mul_val' [has_mul α] [add_comm_monoid α] {M N : matrix n n α} {i k} : (M ⬝ N) i k = dot_product (λ j, M i j) (λ j, N j k) := rfl section semigroup variables [semiring α] protected theorem mul_assoc (L : matrix l m α) (M : matrix m n α) (N : matrix n o α) : (L ⬝ M) ⬝ N = L ⬝ (M ⬝ N) := by { ext, apply dot_product_assoc } instance : semigroup (matrix n n α) := { mul_assoc := matrix.mul_assoc, ..matrix.has_mul } end semigroup @[simp] theorem diagonal_neg [decidable_eq n] [add_group α] (d : n → α) : -diagonal d = diagonal (λ i, -d i) := by ext i j; by_cases i = j; simp [h] section semiring variables [semiring α] @[simp] protected theorem mul_zero (M : matrix m n α) : M ⬝ (0 : matrix n o α) = 0 := by { ext i j, apply dot_product_zero } @[simp] protected theorem zero_mul (M : matrix m n α) : (0 : matrix l m α) ⬝ M = 0 := by { ext i j, apply zero_dot_product } protected theorem mul_add (L : matrix m n α) (M N : matrix n o α) : L ⬝ (M + N) = L ⬝ M + L ⬝ N := by { ext i j, apply dot_product_add } protected theorem add_mul (L M : matrix l m α) (N : matrix m n α) : (L + M) ⬝ N = L ⬝ N + M ⬝ N := by { ext i j, apply add_dot_product } @[simp] theorem diagonal_mul [decidable_eq m] (d : m → α) (M : matrix m n α) (i j) : (diagonal d).mul M i j = d i * M i j := diagonal_dot_product _ _ _ @[simp] theorem mul_diagonal [decidable_eq n] (d : n → α) (M : matrix m n α) (i j) : (M ⬝ diagonal d) i j = M i j * d j := by { rw ← diagonal_transpose, apply dot_product_diagonal } @[simp] protected theorem one_mul [decidable_eq m] (M : matrix m n α) : (1 : matrix m m α) ⬝ M = M := by ext i j; rw [← diagonal_one, diagonal_mul, one_mul] @[simp] protected theorem mul_one [decidable_eq n] (M : matrix m n α) : M ⬝ (1 : matrix n n α) = M := by ext i j; rw [← diagonal_one, mul_diagonal, mul_one] instance [decidable_eq n] : monoid (matrix n n α) := { one_mul := matrix.one_mul, mul_one := matrix.mul_one, ..matrix.has_one, ..matrix.semigroup } instance [decidable_eq n] : semiring (matrix n n α) := { mul_zero := matrix.mul_zero, zero_mul := matrix.zero_mul, left_distrib := matrix.mul_add, right_distrib := matrix.add_mul, ..matrix.add_comm_monoid, ..matrix.monoid } @[simp] theorem diagonal_mul_diagonal [decidable_eq n] (d₁ d₂ : n → α) : (diagonal d₁) ⬝ (diagonal d₂) = diagonal (λ i, d₁ i * d₂ i) := by ext i j; by_cases i = j; simp [h] theorem diagonal_mul_diagonal' [decidable_eq n] (d₁ d₂ : n → α) : diagonal d₁ * diagonal d₂ = diagonal (λ i, d₁ i * d₂ i) := diagonal_mul_diagonal _ _ lemma is_add_monoid_hom_mul_left (M : matrix l m α) : is_add_monoid_hom (λ x : matrix m n α, M ⬝ x) := { to_is_add_hom := ⟨matrix.mul_add _⟩, map_zero := matrix.mul_zero _ } lemma is_add_monoid_hom_mul_right (M : matrix m n α) : is_add_monoid_hom (λ x : matrix l m α, x ⬝ M) := { to_is_add_hom := ⟨λ _ _, matrix.add_mul _ _ _⟩, map_zero := matrix.zero_mul _ } protected lemma sum_mul {β : Type*} (s : finset β) (f : β → matrix l m α) (M : matrix m n α) : (∑ a in s, f a) ⬝ M = ∑ a in s, f a ⬝ M := (@finset.sum_hom _ _ _ _ _ s f (λ x, x ⬝ M) /- This line does not type-check without `id` and `: _`. Lean did not recognize that two different `add_monoid` instances were def-eq -/ (id (@is_add_monoid_hom_mul_right l _ _ _ _ _ _ _ M) : _)).symm protected lemma mul_sum {β : Type*} (s : finset β) (f : β → matrix m n α) (M : matrix l m α) : M ⬝ ∑ a in s, f a = ∑ a in s, M ⬝ f a := (@finset.sum_hom _ _ _ _ _ s f (λ x, M ⬝ x) /- This line does not type-check without `id` and `: _`. Lean did not recognize that two different `add_monoid` instances were def-eq -/ (id (@is_add_monoid_hom_mul_left _ _ n _ _ _ _ _ M) : _)).symm @[simp] lemma row_mul_col_val (v w : m → α) (i j) : (row v ⬝ col w) i j = dot_product v w := rfl end semiring section ring variables [ring α] @[simp] theorem neg_mul (M : matrix m n α) (N : matrix n o α) : (-M) ⬝ N = -(M ⬝ N) := by { ext, apply neg_dot_product } @[simp] theorem mul_neg (M : matrix m n α) (N : matrix n o α) : M ⬝ (-N) = -(M ⬝ N) := by { ext, apply dot_product_neg } end ring instance [decidable_eq n] [ring α] : ring (matrix n n α) := { ..matrix.semiring, ..matrix.add_comm_group } instance [semiring α] : has_scalar α (matrix m n α) := pi.has_scalar instance {β : Type w} [semiring α] [add_comm_monoid β] [semimodule α β] : semimodule α (matrix m n β) := pi.semimodule _ _ _ @[simp] lemma smul_val [semiring α] (a : α) (A : matrix m n α) (i : m) (j : n) : (a • A) i j = a * A i j := rfl section comm_semiring variables [comm_semiring α] lemma smul_eq_diagonal_mul [decidable_eq m] (M : matrix m n α) (a : α) : a • M = diagonal (λ _, a) ⬝ M := by { ext, simp } lemma smul_eq_mul_diagonal [decidable_eq n] (M : matrix m n α) (a : α) : a • M = M ⬝ diagonal (λ _, a) := by { ext, simp [mul_comm] } @[simp] lemma mul_smul (M : matrix m n α) (a : α) (N : matrix n l α) : M ⬝ (a • N) = a • M ⬝ N := by { ext, apply dot_product_smul } @[simp] lemma smul_mul (M : matrix m n α) (a : α) (N : matrix n l α) : (a • M) ⬝ N = a • M ⬝ N := by { ext, apply smul_dot_product } end comm_semiring section semiring variables [semiring α] def vec_mul_vec (w : m → α) (v : n → α) : matrix m n α | x y := w x * v y def mul_vec (M : matrix m n α) (v : n → α) : m → α | i := dot_product (λ j, M i j) v def vec_mul (v : m → α) (M : matrix m n α) : n → α | j := dot_product v (λ i, M i j) instance mul_vec.is_add_monoid_hom_left (v : n → α) : is_add_monoid_hom (λM:matrix m n α, mul_vec M v) := { map_zero := by ext; simp [mul_vec]; refl, map_add := begin intros x y, ext m, apply add_dot_product end } lemma mul_vec_diagonal [decidable_eq m] (v w : m → α) (x : m) : mul_vec (diagonal v) w x = v x * w x := diagonal_dot_product v w x lemma vec_mul_diagonal [decidable_eq m] (v w : m → α) (x : m) : vec_mul v (diagonal w) x = v x * w x := dot_product_diagonal' v w x @[simp] lemma mul_vec_one [decidable_eq m] (v : m → α) : mul_vec 1 v = v := by { ext, rw [←diagonal_one, mul_vec_diagonal, one_mul] } @[simp] lemma vec_mul_one [decidable_eq m] (v : m → α) : vec_mul v 1 = v := by { ext, rw [←diagonal_one, vec_mul_diagonal, mul_one] } @[simp] lemma mul_vec_zero (A : matrix m n α) : mul_vec A 0 = 0 := by { ext, simp [mul_vec] } @[simp] lemma vec_mul_zero (A : matrix m n α) : vec_mul 0 A = 0 := by { ext, simp [vec_mul] } @[simp] lemma vec_mul_vec_mul (v : m → α) (M : matrix m n α) (N : matrix n o α) : vec_mul (vec_mul v M) N = vec_mul v (M ⬝ N) := by { ext, apply dot_product_assoc } @[simp] lemma mul_vec_mul_vec (v : o → α) (M : matrix m n α) (N : matrix n o α) : mul_vec M (mul_vec N v) = mul_vec (M ⬝ N) v := by { ext, symmetry, apply dot_product_assoc } lemma vec_mul_vec_eq (w : m → α) (v : n → α) : vec_mul_vec w v = (col w) ⬝ (row v) := by { ext i j, simp [vec_mul_vec, mul_val], refl } end semiring section ring variables [ring α] lemma neg_vec_mul (v : m → α) (A : matrix m n α) : vec_mul (-v) A = - vec_mul v A := by { ext, apply neg_dot_product } lemma vec_mul_neg (v : m → α) (A : matrix m n α) : vec_mul v (-A) = - vec_mul v A := by { ext, apply dot_product_neg } lemma neg_mul_vec (v : n → α) (A : matrix m n α) : mul_vec (-A) v = - mul_vec A v := by { ext, apply neg_dot_product } lemma mul_vec_neg (v : n → α) (A : matrix m n α) : mul_vec A (-v) = - mul_vec A v := by { ext, apply dot_product_neg } end ring section transpose open_locale matrix /-- Tell `simp` what the entries are in a transposed matrix. Compare with `mul_val`, `diagonal_val_eq`, etc. -/ @[simp] lemma transpose_val (M : matrix m n α) (i j) : M.transpose j i = M i j := rfl @[simp] lemma transpose_transpose (M : matrix m n α) : Mᵀᵀ = M := by ext; refl @[simp] lemma transpose_zero [has_zero α] : (0 : matrix m n α)ᵀ = 0 := by ext i j; refl @[simp] lemma transpose_one [decidable_eq n] [has_zero α] [has_one α] : (1 : matrix n n α)ᵀ = 1 := begin ext i j, unfold has_one.one transpose, by_cases i = j, { simp only [h, diagonal_val_eq] }, { simp only [diagonal_val_ne h, diagonal_val_ne (λ p, h (symm p))] } end @[simp] lemma transpose_add [has_add α] (M : matrix m n α) (N : matrix m n α) : (M + N)ᵀ = Mᵀ + Nᵀ := by { ext i j, simp } @[simp] lemma transpose_mul [comm_ring α] (M : matrix m n α) (N : matrix n l α) : (M ⬝ N)ᵀ = Nᵀ ⬝ Mᵀ := begin ext i j, apply dot_product_comm end @[simp] lemma transpose_smul [comm_ring α] (c : α)(M : matrix m n α) : (c • M)ᵀ = c • Mᵀ := by { ext i j, refl } @[simp] lemma transpose_neg [comm_ring α] (M : matrix m n α) : (- M)ᵀ = - Mᵀ := by ext i j; refl end transpose def minor (A : matrix m n α) (row : l → m) (col : o → n) : matrix l o α := λ i j, A (row i) (col j) @[reducible] def sub_left {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin l) α := minor A id (fin.cast_add r) @[reducible] def sub_right {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin r) α := minor A id (fin.nat_add l) @[reducible] def sub_up {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin u) (fin n) α := minor A (fin.cast_add d) id @[reducible] def sub_down {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin d) (fin n) α := minor A (fin.nat_add u) id @[reducible] def sub_up_right {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin u) (fin r) α := sub_up (sub_right A) @[reducible] def sub_down_right {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin d) (fin r) α := sub_down (sub_right A) @[reducible] def sub_up_left {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin u) (fin (l)) α := sub_up (sub_left A) @[reducible] def sub_down_left {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin d) (fin (l)) α := sub_down (sub_left A) section row_col /-! ### `row_col` section Simplification lemmas for `matrix.row` and `matrix.col`. -/ open_locale matrix @[simp] lemma col_add [semiring α] (v w : m → α) : col (v + w) = col v + col w := by { ext, refl } @[simp] lemma col_smul [semiring α] (x : α) (v : m → α) : col (x • v) = x • col v := by { ext, refl } @[simp] lemma row_add [semiring α] (v w : m → α) : row (v + w) = row v + row w := by { ext, refl } @[simp] lemma row_smul [semiring α] (x : α) (v : m → α) : row (x • v) = x • row v := by { ext, refl } @[simp] lemma col_val (v : m → α) (i j) : matrix.col v i j = v i := rfl @[simp] lemma row_val (v : m → α) (i j) : matrix.row v i j = v j := rfl @[simp] lemma transpose_col (v : m → α) : (matrix.col v).transpose = matrix.row v := by {ext, refl} @[simp] lemma transpose_row (v : m → α) : (matrix.row v).transpose = matrix.col v := by {ext, refl} lemma row_vec_mul [semiring α] (M : matrix m n α) (v : m → α) : matrix.row (matrix.vec_mul v M) = matrix.row v ⬝ M := by {ext, refl} lemma col_vec_mul [semiring α] (M : matrix m n α) (v : m → α) : matrix.col (matrix.vec_mul v M) = (matrix.row v ⬝ M)ᵀ := by {ext, refl} lemma col_mul_vec [semiring α] (M : matrix m n α) (v : n → α) : matrix.col (matrix.mul_vec M v) = M ⬝ matrix.col v := by {ext, refl} lemma row_mul_vec [semiring α] (M : matrix m n α) (v : n → α) : matrix.row (matrix.mul_vec M v) = (M ⬝ matrix.col v)ᵀ := by {ext, refl} end row_col end matrix
ab75343734d5264769477962b27b3ba50d88a35f
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/tests/lean/interactive/coe.lean
a305ab32d71f541dad1a62080b5e8d8dd363eb36
[ "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
161
lean
import data.int open int protected theorem has_decidable_eq [instance] : decidable_eq ℤ := take (a b : ℤ), _ constant n : nat constant i : int check n + i
b0c4b67fca3f56eeeb20a6f2b081fbb3e5d39711
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Leanpkg/Manifest.lean
e993564abb374f1a26b684b3c1c10f638249baa3
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
2,848
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner, Sebastian Ullrich -/ import Leanpkg.Toml import Leanpkg.LeanVersion open System namespace Leanpkg inductive Source where | path (dir : System.FilePath) : Source | git (url rev : String) (branch : Option String) : Source namespace Source def fromToml (v : Toml.Value) : OptionM Source := (do let Toml.Value.str dir ← v.lookup "path" | none path ⟨dir⟩) <|> (do let Toml.Value.str url ← v.lookup "git" | none let Toml.Value.str rev ← v.lookup "rev" | none match v.lookup "branch" with | none => git url rev none | some (Toml.Value.str branch) => git url rev (some branch) | _ => none) def toToml : Source → Toml.Value | path dir => Toml.Value.table [("path", Toml.Value.str dir.toString)] | git url rev none => Toml.Value.table [("git", Toml.Value.str url), ("rev", Toml.Value.str rev)] | git url rev (some branch) => Toml.Value.table [("git", Toml.Value.str url), ("branch", Toml.Value.str branch), ("rev", Toml.Value.str rev)] end Source structure Dependency where name : String src : Source structure Manifest where name : String version : String leanVersion : String := leanVersionString timeout : Option Nat := none path : Option FilePath := none dependencies : List Dependency := [] namespace Manifest def effectivePath (m : Manifest) : FilePath := m.path.getD ⟨"."⟩ def fromToml (t : Toml.Value) : Option Manifest := OptionM.run do let pkg ← t.lookup "package" let Toml.Value.str n ← pkg.lookup "name" | none let Toml.Value.str ver ← pkg.lookup "version" | none let leanVer ← match pkg.lookup "lean_version" with | some (Toml.Value.str leanVer) => some leanVer | none => some leanVersionString | _ => none let tm ← match pkg.lookup "timeout" with | some (Toml.Value.nat timeout) => some (some timeout) | none => some none | _ => none let path ← match pkg.lookup "path" with | some (Toml.Value.str path) => some (some ⟨path⟩) | none => some none | _ => none let Toml.Value.table deps ← t.lookup "dependencies" <|> some (Toml.Value.table []) | none let deps ← deps.mapM fun ⟨n, src⟩ => do Dependency.mk n (← Source.fromToml src) return { name := n, version := ver, leanVersion := leanVer, path := path, dependencies := deps, timeout := tm } def fromFile (fn : System.FilePath) : IO Manifest := do let cnts ← IO.FS.readFile fn let toml ← Toml.parse cnts let some manifest ← pure (fromToml toml) | throw <| IO.userError s!"cannot read manifest from {fn}" manifest end Manifest def leanpkgTomlFn : System.FilePath := ⟨"leanpkg.toml"⟩ end Leanpkg
5e37adeda6a3aeba15d6fc8d2ea9ae45175d0b0e
3d2a7f1582fe5bae4d0bdc2fe86e997521239a65
/misc/family.lean
1d2b301815a09eed113db333bafc92b038fb287e
[]
no_license
own-pt/common-sense-lean
e4fa643ae010459de3d1bf673be7cbc7062563c9
f672210aecb4172f5bae265e43e6867397e13b1c
refs/heads/master
1,622,065,660,261
1,589,487,533,000
1,589,487,533,000
254,167,782
3
2
null
1,589,487,535,000
1,586,370,214,000
Lean
UTF-8
Lean
false
false
1,834
lean
/- From adam's serie: https://www.youtube.com/watch?v=SkruxPmN0kk https://www.youtube.com/watch?v=nvHatVfiLPU -/ constant U : Type -- sumo axioms constants SetOrClass Set Class Object Entity : U constant ins : U → U → Prop constant subclass : U → U → Prop constant element : U → U → Prop constant birthplace : U → U → Prop constant subProcess : U → U → Prop -- problem axioms constants John Mary Stanley Jimmy Jane Human SetJohnMaryStan SetJohnMaryJimmy OshkoshHuman OshkoshWisconsin FeymanLecture Lecture Thanking FeymansThankingBohr GeographicLocation : U axiom john_human : ins John Human axiom mary_human : ins Mary Human axiom stanley_human : ins Stanley Human axiom jimmy_human : ins Jimmy Human axiom jane_ohuman : ins Jane OshkoshHuman axiom oshkoshwis_geo : ins OshkoshWisconsin GeographicLocation axiom john_mary_stan : ins SetJohnMaryStan Set axiom john_el_jms : element John SetJohnMaryStan axiom mary_el_jms : element Mary SetJohnMaryStan axiom stanley_el_jms : element Stanley SetJohnMaryStan axiom john_mary_jimmy : ins SetJohnMaryJimmy Set axiom john_el_jmj : element John SetJohnMaryJimmy axiom mary_el_jmj : element Mary SetJohnMaryJimmy axiom jimmy_jmj : element Jimmy SetJohnMaryJimmy -- https://en.wikipedia.org/wiki/Closed-world_assumption axiom jimmy_not_jms : ¬ element Jimmy SetJohnMaryStan axiom oshkoshHuman_cl : subclass OshkoshHuman Human axiom oshkoshHuman_ax : ∀ x : U, ins x OshkoshHuman → birthplace x OshkoshWisconsin lemma sets_not_equal : ¬ SetJohnMaryStan = SetJohnMaryJimmy := begin intro a, have h₁, from jimmy_not_jms, rw a at h₁, exact (h₁ jimmy_jmj), end lemma birthplace_jane : birthplace Jane OshkoshWisconsin := begin have h₁, from oshkoshHuman_ax Jane, exact (h₁ jane_ohuman), end
66cb0882b22a165026efde409f9e017904b724b3
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/int/basic.lean
04a85597753029bb9f8ef7598a5fbb41eaa14572
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
52,151
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad The integers, with addition, multiplication, and subtraction. -/ import data.nat.basic algebra.char_zero algebra.order_functions data.list.range open nat namespace int instance : inhabited ℤ := ⟨int.zero⟩ @[simp] lemma default_eq_zero : default ℤ = 0 := rfl meta instance : has_to_format ℤ := ⟨λ z, to_string z⟩ meta instance : has_reflect ℤ := by tactic.mk_has_reflect_instance attribute [simp] int.coe_nat_add int.coe_nat_mul int.coe_nat_zero int.coe_nat_one int.coe_nat_succ attribute [simp] int.of_nat_eq_coe int.bodd @[simp] theorem add_def {a b : ℤ} : int.add a b = a + b := rfl @[simp] theorem mul_def {a b : ℤ} : int.mul a b = a * b := rfl @[simp] theorem coe_nat_mul_neg_succ (m n : ℕ) : (m : ℤ) * -[1+ n] = -(m * succ n) := rfl @[simp] theorem neg_succ_mul_coe_nat (m n : ℕ) : -[1+ m] * n = -(succ m * n) := rfl @[simp] theorem neg_succ_mul_neg_succ (m n : ℕ) : -[1+ m] * -[1+ n] = succ m * succ n := rfl @[simp, elim_cast] theorem coe_nat_le {m n : ℕ} : (↑m : ℤ) ≤ ↑n ↔ m ≤ n := coe_nat_le_coe_nat_iff m n @[simp, elim_cast] theorem coe_nat_lt {m n : ℕ} : (↑m : ℤ) < ↑n ↔ m < n := coe_nat_lt_coe_nat_iff m n @[simp, elim_cast] theorem coe_nat_inj' {m n : ℕ} : (↑m : ℤ) = ↑n ↔ m = n := int.coe_nat_eq_coe_nat_iff m n @[simp] theorem coe_nat_pos {n : ℕ} : (0 : ℤ) < n ↔ 0 < n := by rw [← int.coe_nat_zero, coe_nat_lt] @[simp] theorem coe_nat_eq_zero {n : ℕ} : (n : ℤ) = 0 ↔ n = 0 := by rw [← int.coe_nat_zero, coe_nat_inj'] theorem coe_nat_ne_zero {n : ℕ} : (n : ℤ) ≠ 0 ↔ n ≠ 0 := not_congr coe_nat_eq_zero lemma coe_nat_nonneg (n : ℕ) : 0 ≤ (n : ℤ) := coe_nat_le.2 (nat.zero_le _) lemma coe_nat_ne_zero_iff_pos {n : ℕ} : (n : ℤ) ≠ 0 ↔ 0 < n := ⟨λ h, nat.pos_of_ne_zero (coe_nat_ne_zero.1 h), λ h, (ne_of_lt (coe_nat_lt.2 h)).symm⟩ lemma coe_nat_succ_pos (n : ℕ) : 0 < (n.succ : ℤ) := int.coe_nat_pos.2 (succ_pos n) @[simp, elim_cast] theorem coe_nat_abs (n : ℕ) : abs (n : ℤ) = n := abs_of_nonneg (coe_nat_nonneg n) /- succ and pred -/ /-- Immediate successor of an integer: `succ n = n + 1` -/ def succ (a : ℤ) := a + 1 /-- Immediate predecessor of an integer: `pred n = n - 1` -/ def pred (a : ℤ) := a - 1 theorem nat_succ_eq_int_succ (n : ℕ) : (nat.succ n : ℤ) = int.succ n := rfl theorem pred_succ (a : ℤ) : pred (succ a) = a := add_sub_cancel _ _ theorem succ_pred (a : ℤ) : succ (pred a) = a := sub_add_cancel _ _ theorem neg_succ (a : ℤ) : -succ a = pred (-a) := neg_add _ _ theorem succ_neg_succ (a : ℤ) : succ (-succ a) = -a := by rw [neg_succ, succ_pred] theorem neg_pred (a : ℤ) : -pred a = succ (-a) := by rw [eq_neg_of_eq_neg (neg_succ (-a)).symm, neg_neg] theorem pred_neg_pred (a : ℤ) : pred (-pred a) = -a := by rw [neg_pred, pred_succ] theorem pred_nat_succ (n : ℕ) : pred (nat.succ n) = n := pred_succ n theorem neg_nat_succ (n : ℕ) : -(nat.succ n : ℤ) = pred (-n) := neg_succ n theorem succ_neg_nat_succ (n : ℕ) : succ (-nat.succ n) = -n := succ_neg_succ n theorem lt_succ_self (a : ℤ) : a < succ a := lt_add_of_pos_right _ zero_lt_one theorem pred_self_lt (a : ℤ) : pred a < a := sub_lt_self _ zero_lt_one theorem add_one_le_iff {a b : ℤ} : a + 1 ≤ b ↔ a < b := iff.rfl theorem lt_add_one_iff {a b : ℤ} : a < b + 1 ↔ a ≤ b := @add_le_add_iff_right _ _ a b 1 theorem sub_one_lt_iff {a b : ℤ} : a - 1 < b ↔ a ≤ b := sub_lt_iff_lt_add.trans lt_add_one_iff theorem le_sub_one_iff {a b : ℤ} : a ≤ b - 1 ↔ a < b := le_sub_iff_add_le @[elab_as_eliminator] protected lemma induction_on {p : ℤ → Prop} (i : ℤ) (hz : p 0) (hp : ∀i : ℕ, p i → p (i + 1)) (hn : ∀i : ℕ, p (-i) → p (-i - 1)) : p i := begin induction i, { induction i, { exact hz }, { exact hp _ i_ih } }, { have : ∀n:ℕ, p (- n), { intro n, induction n, { simp [hz] }, { convert hn _ n_ih using 1, simp [sub_eq_neg_add] } }, exact this (i + 1) } end protected def induction_on' {C : ℤ → Sort*} (z : ℤ) (b : ℤ) : C b → (∀ k, b ≤ k → C k → C (k + 1)) → (∀ k ≤ b, C k → C (k - 1)) → C z := λ H0 Hs Hp, begin rw ←sub_add_cancel z b, induction (z - b), { induction a with n ih, { rwa [of_nat_zero, zero_add] }, rw [of_nat_succ, add_assoc, add_comm 1 b, ←add_assoc], exact Hs _ (le_add_of_nonneg_left (of_nat_nonneg _)) ih }, { induction a with n ih, { rw [neg_succ_of_nat_eq, ←of_nat_eq_coe, of_nat_zero, zero_add, neg_add_eq_sub], exact Hp _ (le_refl _) H0 }, { rw [neg_succ_of_nat_coe', nat.succ_eq_add_one, ←neg_succ_of_nat_coe, sub_add_eq_add_sub], exact Hp _ (le_of_lt (add_lt_of_neg_of_le (neg_succ_lt_zero _) (le_refl _))) ih } } end /- nat abs -/ attribute [simp] nat_abs nat_abs_of_nat nat_abs_zero nat_abs_one theorem nat_abs_add_le (a b : ℤ) : nat_abs (a + b) ≤ nat_abs a + nat_abs b := begin have : ∀ (a b : ℕ), nat_abs (sub_nat_nat a (nat.succ b)) ≤ nat.succ (a + b), { refine (λ a b : ℕ, sub_nat_nat_elim a b.succ (λ m n i, n = b.succ → nat_abs i ≤ (m + b).succ) _ _ rfl); intros i n e, { subst e, rw [add_comm _ i, add_assoc], exact nat.le_add_right i (b.succ + b).succ }, { apply succ_le_succ, rw [← succ_inj e, ← add_assoc, add_comm], apply nat.le_add_right } }, cases a; cases b with b b; simp [nat_abs, nat.succ_add]; try {refl}; [skip, rw add_comm a b]; apply this end theorem nat_abs_neg_of_nat (n : ℕ) : nat_abs (neg_of_nat n) = n := by cases n; refl theorem nat_abs_mul (a b : ℤ) : nat_abs (a * b) = (nat_abs a) * (nat_abs b) := by cases a; cases b; simp only [(*), int.mul, nat_abs_neg_of_nat, eq_self_iff_true, int.nat_abs] @[simp] lemma nat_abs_mul_self' (a : ℤ) : (nat_abs a * nat_abs a : ℤ) = a * a := by rw [← int.coe_nat_mul, nat_abs_mul_self] theorem neg_succ_of_nat_eq' (m : ℕ) : -[1+ m] = -m - 1 := by simp [neg_succ_of_nat_eq, sub_eq_neg_add] lemma nat_abs_ne_zero_of_ne_zero {z : ℤ} (hz : z ≠ 0) : z.nat_abs ≠ 0 := λ h, hz $ int.eq_zero_of_nat_abs_eq_zero h @[simp] lemma nat_abs_eq_zero {a : ℤ} : a.nat_abs = 0 ↔ a = 0 := ⟨int.eq_zero_of_nat_abs_eq_zero, λ h, h.symm ▸ rfl⟩ /- / -/ @[simp] theorem of_nat_div (m n : ℕ) : of_nat (m / n) = (of_nat m) / (of_nat n) := rfl @[simp, move_cast] theorem coe_nat_div (m n : ℕ) : ((m / n : ℕ) : ℤ) = m / n := rfl theorem neg_succ_of_nat_div (m : ℕ) {b : ℤ} (H : 0 < b) : -[1+m] / b = -(m / b + 1) := match b, eq_succ_of_zero_lt H with ._, ⟨n, rfl⟩ := rfl end @[simp] protected theorem div_neg : ∀ (a b : ℤ), a / -b = -(a / b) | (m : ℕ) 0 := show of_nat (m / 0) = -(m / 0 : ℕ), by rw nat.div_zero; refl | (m : ℕ) (n+1:ℕ) := rfl | 0 -[1+ n] := rfl | (m+1:ℕ) -[1+ n] := (neg_neg _).symm | -[1+ m] 0 := rfl | -[1+ m] (n+1:ℕ) := rfl | -[1+ m] -[1+ n] := rfl theorem div_of_neg_of_pos {a b : ℤ} (Ha : a < 0) (Hb : 0 < b) : a / b = -((-a - 1) / b + 1) := match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := by change (- -[1+ m] : ℤ) with (m+1 : ℤ); rw add_sub_cancel; refl end protected theorem div_nonneg {a b : ℤ} (Ha : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a / b := match a, b, eq_coe_of_zero_le Ha, eq_coe_of_zero_le Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := coe_zero_le _ end protected theorem div_nonpos {a b : ℤ} (Ha : 0 ≤ a) (Hb : b ≤ 0) : a / b ≤ 0 := nonpos_of_neg_nonneg $ by rw [← int.div_neg]; exact int.div_nonneg Ha (neg_nonneg_of_nonpos Hb) theorem div_neg' {a b : ℤ} (Ha : a < 0) (Hb : 0 < b) : a / b < 0 := match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := neg_succ_lt_zero _ end -- Will be generalized to Euclidean domains. protected theorem zero_div : ∀ (b : ℤ), 0 / b = 0 | 0 := rfl | (n+1:ℕ) := rfl | -[1+ n] := rfl local attribute [simp] -- Will be generalized to Euclidean domains. protected theorem div_zero : ∀ (a : ℤ), a / 0 = 0 | 0 := rfl | (n+1:ℕ) := rfl | -[1+ n] := rfl @[simp] protected theorem div_one : ∀ (a : ℤ), a / 1 = a | 0 := rfl | (n+1:ℕ) := congr_arg of_nat (nat.div_one _) | -[1+ n] := congr_arg neg_succ_of_nat (nat.div_one _) theorem div_eq_zero_of_lt {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < b) : a / b = 0 := match a, b, eq_coe_of_zero_le H1, eq_succ_of_zero_lt (lt_of_le_of_lt H1 H2), H2 with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 := congr_arg of_nat $ nat.div_eq_of_lt $ lt_of_coe_nat_lt_coe_nat H2 end theorem div_eq_zero_of_lt_abs {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < abs b) : a / b = 0 := match b, abs b, abs_eq_nat_abs b, H2 with | (n : ℕ), ._, rfl, H2 := div_eq_zero_of_lt H1 H2 | -[1+ n], ._, rfl, H2 := neg_inj $ by rw [← int.div_neg]; exact div_eq_zero_of_lt H1 H2 end protected theorem add_mul_div_right (a b : ℤ) {c : ℤ} (H : c ≠ 0) : (a + b * c) / c = a / c + b := have ∀ {k n : ℕ} {a : ℤ}, (a + n * k.succ) / k.succ = a / k.succ + n, from λ k n a, match a with | (m : ℕ) := congr_arg of_nat $ nat.add_mul_div_right _ _ k.succ_pos | -[1+ m] := show ((n * k.succ:ℕ) - m.succ : ℤ) / k.succ = n - (m / k.succ + 1 : ℕ), begin cases lt_or_ge m (n*k.succ) with h h, { rw [← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.div_lt_iff_lt_mul _ _ k.succ_pos).2 h)], apply congr_arg of_nat, rw [mul_comm, nat.mul_sub_div], rwa mul_comm }, { change (↑(n * nat.succ k) - (m + 1) : ℤ) / ↑(nat.succ k) = ↑n - ((m / nat.succ k : ℕ) + 1), rw [← sub_sub, ← sub_sub, ← neg_sub (m:ℤ), ← neg_sub _ (n:ℤ), ← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.le_div_iff_mul_le _ _ k.succ_pos).2 h), ← neg_succ_of_nat_coe', ← neg_succ_of_nat_coe'], { apply congr_arg neg_succ_of_nat, rw [mul_comm, nat.sub_mul_div], rwa mul_comm } } end end, have ∀ {a b c : ℤ}, 0 < c → (a + b * c) / c = a / c + b, from λ a b c H, match c, eq_succ_of_zero_lt H, b with | ._, ⟨k, rfl⟩, (n : ℕ) := this | ._, ⟨k, rfl⟩, -[1+ n] := show (a - n.succ * k.succ) / k.succ = (a / k.succ) - n.succ, from eq_sub_of_add_eq $ by rw [← this, sub_add_cancel] end, match lt_trichotomy c 0 with | or.inl hlt := neg_inj $ by rw [← int.div_neg, neg_add, ← int.div_neg, ← neg_mul_neg]; apply this (neg_pos_of_neg hlt) | or.inr (or.inl heq) := absurd heq H | or.inr (or.inr hgt) := this hgt end protected theorem add_mul_div_left (a : ℤ) {b : ℤ} (c : ℤ) (H : b ≠ 0) : (a + b * c) / b = a / b + c := by rw [mul_comm, int.add_mul_div_right _ _ H] @[simp] protected theorem mul_div_cancel (a : ℤ) {b : ℤ} (H : b ≠ 0) : a * b / b = a := by have := int.add_mul_div_right 0 a H; rwa [zero_add, int.zero_div, zero_add] at this @[simp] protected theorem mul_div_cancel_left {a : ℤ} (b : ℤ) (H : a ≠ 0) : a * b / a = b := by rw [mul_comm, int.mul_div_cancel _ H] @[simp] protected theorem div_self {a : ℤ} (H : a ≠ 0) : a / a = 1 := by have := int.mul_div_cancel 1 H; rwa one_mul at this /- mod -/ theorem of_nat_mod (m n : nat) : (m % n : ℤ) = of_nat (m % n) := rfl @[simp] theorem coe_nat_mod (m n : ℕ) : (↑(m % n) : ℤ) = ↑m % ↑n := rfl theorem neg_succ_of_nat_mod (m : ℕ) {b : ℤ} (bpos : 0 < b) : -[1+m] % b = b - 1 - m % b := by rw [sub_sub, add_comm]; exact match b, eq_succ_of_zero_lt bpos with ._, ⟨n, rfl⟩ := rfl end @[simp] theorem mod_neg : ∀ (a b : ℤ), a % -b = a % b | (m : ℕ) n := @congr_arg ℕ ℤ _ _ (λ i, ↑(m % i)) (nat_abs_neg _) | -[1+ m] n := @congr_arg ℕ ℤ _ _ (λ i, sub_nat_nat i (nat.succ (m % i))) (nat_abs_neg _) @[simp] theorem mod_abs (a b : ℤ) : a % (abs b) = a % b := abs_by_cases (λ i, a % i = a % b) rfl (mod_neg _ _) local attribute [simp] -- Will be generalized to Euclidean domains. theorem zero_mod (b : ℤ) : 0 % b = 0 := congr_arg of_nat $ nat.zero_mod _ local attribute [simp] -- Will be generalized to Euclidean domains. theorem mod_zero : ∀ (a : ℤ), a % 0 = a | (m : ℕ) := congr_arg of_nat $ nat.mod_zero _ | -[1+ m] := congr_arg neg_succ_of_nat $ nat.mod_zero _ local attribute [simp] -- Will be generalized to Euclidean domains. theorem mod_one : ∀ (a : ℤ), a % 1 = 0 | (m : ℕ) := congr_arg of_nat $ nat.mod_one _ | -[1+ m] := show (1 - (m % 1).succ : ℤ) = 0, by rw nat.mod_one; refl theorem mod_eq_of_lt {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < b) : a % b = a := match a, b, eq_coe_of_zero_le H1, eq_coe_of_zero_le (le_trans H1 (le_of_lt H2)), H2 with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 := congr_arg of_nat $ nat.mod_eq_of_lt (lt_of_coe_nat_lt_coe_nat H2) end theorem mod_nonneg : ∀ (a : ℤ) {b : ℤ}, b ≠ 0 → 0 ≤ a % b | (m : ℕ) n H := coe_zero_le _ | -[1+ m] n H := sub_nonneg_of_le $ coe_nat_le_coe_nat_of_le $ nat.mod_lt _ (nat_abs_pos_of_ne_zero H) theorem mod_lt_of_pos (a : ℤ) {b : ℤ} (H : 0 < b) : a % b < b := match a, b, eq_succ_of_zero_lt H with | (m : ℕ), ._, ⟨n, rfl⟩ := coe_nat_lt_coe_nat_of_lt (nat.mod_lt _ (nat.succ_pos _)) | -[1+ m], ._, ⟨n, rfl⟩ := sub_lt_self _ (coe_nat_lt_coe_nat_of_lt $ nat.succ_pos _) end theorem mod_lt (a : ℤ) {b : ℤ} (H : b ≠ 0) : a % b < abs b := by rw [← mod_abs]; exact mod_lt_of_pos _ (abs_pos_of_ne_zero H) theorem mod_add_div_aux (m n : ℕ) : (n - (m % n + 1) - (n * (m / n) + n) : ℤ) = -[1+ m] := begin rw [← sub_sub, neg_succ_of_nat_coe, sub_sub (n:ℤ)], apply eq_neg_of_eq_neg, rw [neg_sub, sub_sub_self, add_right_comm], exact @congr_arg ℕ ℤ _ _ (λi, (i + 1 : ℤ)) (nat.mod_add_div _ _).symm end theorem mod_add_div : ∀ (a b : ℤ), a % b + b * (a / b) = a | (m : ℕ) 0 := congr_arg of_nat (nat.mod_add_div _ _) | (m : ℕ) (n+1:ℕ) := congr_arg of_nat (nat.mod_add_div _ _) | 0 -[1+ n] := rfl | (m+1:ℕ) -[1+ n] := show (_ + -(n+1) * -((m + 1) / (n + 1) : ℕ) : ℤ) = _, by rw [neg_mul_neg]; exact congr_arg of_nat (nat.mod_add_div _ _) | -[1+ m] 0 := by rw [mod_zero, int.div_zero]; refl | -[1+ m] (n+1:ℕ) := mod_add_div_aux m n.succ | -[1+ m] -[1+ n] := mod_add_div_aux m n.succ theorem mod_def (a b : ℤ) : a % b = a - b * (a / b) := eq_sub_of_add_eq (mod_add_div _ _) @[simp] theorem add_mul_mod_self {a b c : ℤ} : (a + b * c) % c = a % c := if cz : c = 0 then by rw [cz, mul_zero, add_zero] else by rw [mod_def, mod_def, int.add_mul_div_right _ _ cz, mul_add, mul_comm, add_sub_add_right_eq_sub] @[simp] theorem add_mul_mod_self_left (a b c : ℤ) : (a + b * c) % b = a % b := by rw [mul_comm, add_mul_mod_self] @[simp] theorem add_mod_self {a b : ℤ} : (a + b) % b = a % b := by have := add_mul_mod_self_left a b 1; rwa mul_one at this @[simp] theorem add_mod_self_left {a b : ℤ} : (a + b) % a = b % a := by rw [add_comm, add_mod_self] @[simp] theorem mod_add_mod (m n k : ℤ) : (m % n + k) % n = (m + k) % n := by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm; rwa [add_right_comm, mod_add_div] at this @[simp] theorem add_mod_mod (m n k : ℤ) : (m + n % k) % k = (m + n) % k := by rw [add_comm, mod_add_mod, add_comm] theorem add_mod_eq_add_mod_right {m n k : ℤ} (i : ℤ) (H : m % n = k % n) : (m + i) % n = (k + i) % n := by rw [← mod_add_mod, ← mod_add_mod k, H] theorem add_mod_eq_add_mod_left {m n k : ℤ} (i : ℤ) (H : m % n = k % n) : (i + m) % n = (i + k) % n := by rw [add_comm, add_mod_eq_add_mod_right _ H, add_comm] theorem mod_add_cancel_right {m n k : ℤ} (i) : (m + i) % n = (k + i) % n ↔ m % n = k % n := ⟨λ H, by have := add_mod_eq_add_mod_right (-i) H; rwa [add_neg_cancel_right, add_neg_cancel_right] at this, add_mod_eq_add_mod_right _⟩ theorem mod_add_cancel_left {m n k i : ℤ} : (i + m) % n = (i + k) % n ↔ m % n = k % n := by rw [add_comm, add_comm i, mod_add_cancel_right] theorem mod_sub_cancel_right {m n k : ℤ} (i) : (m - i) % n = (k - i) % n ↔ m % n = k % n := mod_add_cancel_right _ theorem mod_eq_mod_iff_mod_sub_eq_zero {m n k : ℤ} : m % n = k % n ↔ (m - k) % n = 0 := (mod_sub_cancel_right k).symm.trans $ by simp @[simp] theorem mul_mod_left (a b : ℤ) : (a * b) % b = 0 := by rw [← zero_add (a * b), add_mul_mod_self, zero_mod] @[simp] theorem mul_mod_right (a b : ℤ) : (a * b) % a = 0 := by rw [mul_comm, mul_mod_left] local attribute [simp] -- Will be generalized to Euclidean domains. theorem mod_self {a : ℤ} : a % a = 0 := by have := mul_mod_left 1 a; rwa one_mul at this @[simp] theorem mod_mod_of_dvd (n : int) {m k : int} (h : m ∣ k) : n % k % m = n % m := begin conv { to_rhs, rw ←mod_add_div n k }, rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left] end @[simp] theorem mod_mod (a b : ℤ) : a % b % b = a % b := by conv {to_rhs, rw [← mod_add_div a b, add_mul_mod_self_left]} /- properties of / and % -/ @[simp] theorem mul_div_mul_of_pos {a : ℤ} (b c : ℤ) (H : 0 < a) : a * b / (a * c) = b / c := suffices ∀ (m k : ℕ) (b : ℤ), (m.succ * b / (m.succ * k) : ℤ) = b / k, from match a, eq_succ_of_zero_lt H, c, eq_coe_or_neg c with | ._, ⟨m, rfl⟩, ._, ⟨k, or.inl rfl⟩ := this _ _ _ | ._, ⟨m, rfl⟩, ._, ⟨k, or.inr rfl⟩ := by rw [← neg_mul_eq_mul_neg, int.div_neg, int.div_neg]; apply congr_arg has_neg.neg; apply this end, λ m k b, match b, k with | (n : ℕ), k := congr_arg of_nat (nat.mul_div_mul _ _ m.succ_pos) | -[1+ n], 0 := by rw [int.coe_nat_zero, mul_zero, int.div_zero, int.div_zero] | -[1+ n], k+1 := congr_arg neg_succ_of_nat $ show (m.succ * n + m) / (m.succ * k.succ) = n / k.succ, begin apply nat.div_eq_of_lt_le, { refine le_trans _ (nat.le_add_right _ _), rw [← nat.mul_div_mul _ _ m.succ_pos], apply nat.div_mul_le_self }, { change m.succ * n.succ ≤ _, rw [mul_left_comm], apply nat.mul_le_mul_left, apply (nat.div_lt_iff_lt_mul _ _ k.succ_pos).1, apply nat.lt_succ_self } end end @[simp] theorem mul_div_mul_of_pos_left (a : ℤ) {b : ℤ} (c : ℤ) (H : 0 < b) : a * b / (c * b) = a / c := by rw [mul_comm, mul_comm c, mul_div_mul_of_pos _ _ H] @[simp] theorem mul_mod_mul_of_pos {a : ℤ} (b c : ℤ) (H : 0 < a) : a * b % (a * c) = a * (b % c) := by rw [mod_def, mod_def, mul_div_mul_of_pos _ _ H, mul_sub_left_distrib, mul_assoc] theorem lt_div_add_one_mul_self (a : ℤ) {b : ℤ} (H : 0 < b) : a < (a / b + 1) * b := by rw [add_mul, one_mul, mul_comm]; apply lt_add_of_sub_left_lt; rw [← mod_def]; apply mod_lt_of_pos _ H theorem abs_div_le_abs : ∀ (a b : ℤ), abs (a / b) ≤ abs a := suffices ∀ (a : ℤ) (n : ℕ), abs (a / n) ≤ abs a, from λ a b, match b, eq_coe_or_neg b with | ._, ⟨n, or.inl rfl⟩ := this _ _ | ._, ⟨n, or.inr rfl⟩ := by rw [int.div_neg, abs_neg]; apply this end, λ a n, by rw [abs_eq_nat_abs, abs_eq_nat_abs]; exact coe_nat_le_coe_nat_of_le (match a, n with | (m : ℕ), n := nat.div_le_self _ _ | -[1+ m], 0 := nat.zero_le _ | -[1+ m], n+1 := nat.succ_le_succ (nat.div_le_self _ _) end) theorem div_le_self {a : ℤ} (b : ℤ) (Ha : 0 ≤ a) : a / b ≤ a := by have := le_trans (le_abs_self _) (abs_div_le_abs a b); rwa [abs_of_nonneg Ha] at this theorem mul_div_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : b * (a / b) = a := by have := mod_add_div a b; rwa [H, zero_add] at this theorem div_mul_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : a / b * b = a := by rw [mul_comm, mul_div_cancel_of_mod_eq_zero H] lemma mod_two_eq_zero_or_one (n : ℤ) : n % 2 = 0 ∨ n % 2 = 1 := have h : n % 2 < 2 := abs_of_nonneg (show 0 ≤ (2 : ℤ), from dec_trivial) ▸ int.mod_lt _ dec_trivial, have h₁ : 0 ≤ n % 2 := int.mod_nonneg _ dec_trivial, match (n % 2), h, h₁ with | (0 : ℕ) := λ _ _, or.inl rfl | (1 : ℕ) := λ _ _, or.inr rfl | (k + 2 : ℕ) := λ h _, absurd h dec_trivial | -[1+ a] := λ _ h₁, absurd h₁ dec_trivial end /- dvd -/ @[elim_cast] theorem coe_nat_dvd {m n : ℕ} : (↑m : ℤ) ∣ ↑n ↔ m ∣ n := ⟨λ ⟨a, ae⟩, m.eq_zero_or_pos.elim (λm0, by simp [m0] at ae; simp [ae, m0]) (λm0l, by { cases eq_coe_of_zero_le (@nonneg_of_mul_nonneg_left ℤ _ m a (by simp [ae.symm]) (by simpa using m0l)) with k e, subst a, exact ⟨k, int.coe_nat_inj ae⟩ }), λ ⟨k, e⟩, dvd.intro k $ by rw [e, int.coe_nat_mul]⟩ theorem coe_nat_dvd_left {n : ℕ} {z : ℤ} : (↑n : ℤ) ∣ z ↔ n ∣ z.nat_abs := by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd] theorem coe_nat_dvd_right {n : ℕ} {z : ℤ} : z ∣ (↑n : ℤ) ↔ z.nat_abs ∣ n := by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd] theorem dvd_antisymm {a b : ℤ} (H1 : 0 ≤ a) (H2 : 0 ≤ b) : a ∣ b → b ∣ a → a = b := begin rw [← abs_of_nonneg H1, ← abs_of_nonneg H2, abs_eq_nat_abs, abs_eq_nat_abs], rw [coe_nat_dvd, coe_nat_dvd, coe_nat_inj'], apply nat.dvd_antisymm end theorem dvd_of_mod_eq_zero {a b : ℤ} (H : b % a = 0) : a ∣ b := ⟨b / a, (mul_div_cancel_of_mod_eq_zero H).symm⟩ theorem mod_eq_zero_of_dvd : ∀ {a b : ℤ}, a ∣ b → b % a = 0 | a ._ ⟨c, rfl⟩ := mul_mod_right _ _ theorem dvd_iff_mod_eq_zero (a b : ℤ) : a ∣ b ↔ b % a = 0 := ⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩ theorem nat_abs_dvd {a b : ℤ} : (a.nat_abs : ℤ) ∣ b ↔ a ∣ b := (nat_abs_eq a).elim (λ e, by rw ← e) (λ e, by rw [← neg_dvd_iff_dvd, ← e]) theorem dvd_nat_abs {a b : ℤ} : a ∣ b.nat_abs ↔ a ∣ b := (nat_abs_eq b).elim (λ e, by rw ← e) (λ e, by rw [← dvd_neg_iff_dvd, ← e]) instance decidable_dvd : @decidable_rel ℤ (∣) := assume a n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm protected theorem div_mul_cancel {a b : ℤ} (H : b ∣ a) : a / b * b = a := div_mul_cancel_of_mod_eq_zero (mod_eq_zero_of_dvd H) protected theorem mul_div_cancel' {a b : ℤ} (H : a ∣ b) : a * (b / a) = b := by rw [mul_comm, int.div_mul_cancel H] protected theorem mul_div_assoc (a : ℤ) : ∀ {b c : ℤ}, c ∣ b → (a * b) / c = a * (b / c) | ._ c ⟨d, rfl⟩ := if cz : c = 0 then by simp [cz] else by rw [mul_left_comm, int.mul_div_cancel_left _ cz, int.mul_div_cancel_left _ cz] theorem div_dvd_div : ∀ {a b c : ℤ} (H1 : a ∣ b) (H2 : b ∣ c), b / a ∣ c / a | a ._ ._ ⟨b, rfl⟩ ⟨c, rfl⟩ := if az : a = 0 then by simp [az] else by rw [int.mul_div_cancel_left _ az, mul_assoc, int.mul_div_cancel_left _ az]; apply dvd_mul_right protected theorem eq_mul_of_div_eq_right {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) : a = b * c := by rw [← H2, int.mul_div_cancel' H1] protected theorem div_eq_of_eq_mul_right {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = b * c) : a / b = c := by rw [H2, int.mul_div_cancel_left _ H1] protected theorem div_eq_iff_eq_mul_right {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) : a / b = c ↔ a = b * c := ⟨int.eq_mul_of_div_eq_right H', int.div_eq_of_eq_mul_right H⟩ protected theorem div_eq_iff_eq_mul_left {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) : a / b = c ↔ a = c * b := by rw mul_comm; exact int.div_eq_iff_eq_mul_right H H' protected theorem eq_mul_of_div_eq_left {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) : a = c * b := by rw [mul_comm, int.eq_mul_of_div_eq_right H1 H2] protected theorem div_eq_of_eq_mul_left {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = c * b) : a / b = c := int.div_eq_of_eq_mul_right H1 (by rw [mul_comm, H2]) theorem neg_div_of_dvd : ∀ {a b : ℤ} (H : b ∣ a), -a / b = -(a / b) | ._ b ⟨c, rfl⟩ := if bz : b = 0 then by simp [bz] else by rw [neg_mul_eq_mul_neg, int.mul_div_cancel_left _ bz, int.mul_div_cancel_left _ bz] lemma add_div_of_dvd {a b c : ℤ} : c ∣ a → c ∣ b → (a + b) / c = a / c + b / c := begin intros h1 h2, by_cases h3 : c = 0, { rw [h3, zero_dvd_iff] at *, rw [h1, h2, h3], refl }, { apply eq_of_mul_eq_mul_right h3, rw add_mul, repeat {rw [int.div_mul_cancel]}; try {apply dvd_add}; assumption } end theorem div_sign : ∀ a b, a / sign b = a * sign b | a (n+1:ℕ) := by unfold sign; simp | a 0 := by simp [sign] | a -[1+ n] := by simp [sign] @[simp] theorem sign_mul : ∀ a b, sign (a * b) = sign a * sign b | a 0 := by simp | 0 b := by simp | (m+1:ℕ) (n+1:ℕ) := rfl | (m+1:ℕ) -[1+ n] := rfl | -[1+ m] (n+1:ℕ) := rfl | -[1+ m] -[1+ n] := rfl protected theorem sign_eq_div_abs (a : ℤ) : sign a = a / (abs a) := if az : a = 0 then by simp [az] else (int.div_eq_of_eq_mul_left (mt eq_zero_of_abs_eq_zero az) (sign_mul_abs _).symm).symm theorem mul_sign : ∀ (i : ℤ), i * sign i = nat_abs i | (n+1:ℕ) := mul_one _ | 0 := mul_zero _ | -[1+ n] := mul_neg_one _ theorem le_of_dvd {a b : ℤ} (bpos : 0 < b) (H : a ∣ b) : a ≤ b := match a, b, eq_succ_of_zero_lt bpos, H with | (m : ℕ), ._, ⟨n, rfl⟩, H := coe_nat_le_coe_nat_of_le $ nat.le_of_dvd n.succ_pos $ coe_nat_dvd.1 H | -[1+ m], ._, ⟨n, rfl⟩, _ := le_trans (le_of_lt $ neg_succ_lt_zero _) (coe_zero_le _) end theorem eq_one_of_dvd_one {a : ℤ} (H : 0 ≤ a) (H' : a ∣ 1) : a = 1 := match a, eq_coe_of_zero_le H, H' with | ._, ⟨n, rfl⟩, H' := congr_arg coe $ nat.eq_one_of_dvd_one $ coe_nat_dvd.1 H' end theorem eq_one_of_mul_eq_one_right {a b : ℤ} (H : 0 ≤ a) (H' : a * b = 1) : a = 1 := eq_one_of_dvd_one H ⟨b, H'.symm⟩ theorem eq_one_of_mul_eq_one_left {a b : ℤ} (H : 0 ≤ b) (H' : a * b = 1) : b = 1 := eq_one_of_mul_eq_one_right H (by rw [mul_comm, H']) lemma of_nat_dvd_of_dvd_nat_abs {a : ℕ} : ∀ {z : ℤ} (haz : a ∣ z.nat_abs), ↑a ∣ z | (int.of_nat _) haz := int.coe_nat_dvd.2 haz | -[1+k] haz := begin change ↑a ∣ -(k+1 : ℤ), apply dvd_neg_of_dvd, apply int.coe_nat_dvd.2, exact haz end lemma dvd_nat_abs_of_of_nat_dvd {a : ℕ} : ∀ {z : ℤ} (haz : ↑a ∣ z), a ∣ z.nat_abs | (int.of_nat _) haz := int.coe_nat_dvd.1 (int.dvd_nat_abs.2 haz) | -[1+k] haz := have haz' : (↑a:ℤ) ∣ (↑(k+1):ℤ), from dvd_of_dvd_neg haz, int.coe_nat_dvd.1 haz' lemma pow_dvd_of_le_of_pow_dvd {p m n : ℕ} {k : ℤ} (hmn : m ≤ n) (hdiv : ↑(p ^ n) ∣ k) : ↑(p ^ m) ∣ k := begin induction k, { apply int.coe_nat_dvd.2, apply pow_dvd_of_le_of_pow_dvd hmn, apply int.coe_nat_dvd.1 hdiv }, { change -[1+k] with -(↑(k+1) : ℤ), apply dvd_neg_of_dvd, apply int.coe_nat_dvd.2, apply pow_dvd_of_le_of_pow_dvd hmn, apply int.coe_nat_dvd.1, apply dvd_of_dvd_neg, exact hdiv } end lemma dvd_of_pow_dvd {p k : ℕ} {m : ℤ} (hk : 1 ≤ k) (hpk : ↑(p^k) ∣ m) : ↑p ∣ m := by rw ←nat.pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk /- / and ordering -/ protected theorem div_mul_le (a : ℤ) {b : ℤ} (H : b ≠ 0) : a / b * b ≤ a := le_of_sub_nonneg $ by rw [mul_comm, ← mod_def]; apply mod_nonneg _ H protected theorem div_le_of_le_mul {a b c : ℤ} (H : 0 < c) (H' : a ≤ b * c) : a / c ≤ b := le_of_mul_le_mul_right (le_trans (int.div_mul_le _ (ne_of_gt H)) H') H protected theorem mul_lt_of_lt_div {a b c : ℤ} (H : 0 < c) (H3 : a < b / c) : a * c < b := lt_of_not_ge $ mt (int.div_le_of_le_mul H) (not_le_of_gt H3) protected theorem mul_le_of_le_div {a b c : ℤ} (H1 : 0 < c) (H2 : a ≤ b / c) : a * c ≤ b := le_trans (mul_le_mul_of_nonneg_right H2 (le_of_lt H1)) (int.div_mul_le _ (ne_of_gt H1)) protected theorem le_div_of_mul_le {a b c : ℤ} (H1 : 0 < c) (H2 : a * c ≤ b) : a ≤ b / c := le_of_lt_add_one $ lt_of_mul_lt_mul_right (lt_of_le_of_lt H2 (lt_div_add_one_mul_self _ H1)) (le_of_lt H1) protected theorem le_div_iff_mul_le {a b c : ℤ} (H : 0 < c) : a ≤ b / c ↔ a * c ≤ b := ⟨int.mul_le_of_le_div H, int.le_div_of_mul_le H⟩ protected theorem div_le_div {a b c : ℤ} (H : 0 < c) (H' : a ≤ b) : a / c ≤ b / c := int.le_div_of_mul_le H (le_trans (int.div_mul_le _ (ne_of_gt H)) H') protected theorem div_lt_of_lt_mul {a b c : ℤ} (H : 0 < c) (H' : a < b * c) : a / c < b := lt_of_not_ge $ mt (int.mul_le_of_le_div H) (not_le_of_gt H') protected theorem lt_mul_of_div_lt {a b c : ℤ} (H1 : 0 < c) (H2 : a / c < b) : a < b * c := lt_of_not_ge $ mt (int.le_div_of_mul_le H1) (not_le_of_gt H2) protected theorem div_lt_iff_lt_mul {a b c : ℤ} (H : 0 < c) : a / c < b ↔ a < b * c := ⟨int.lt_mul_of_div_lt H, int.div_lt_of_lt_mul H⟩ protected theorem le_mul_of_div_le {a b c : ℤ} (H1 : 0 ≤ b) (H2 : b ∣ a) (H3 : a / b ≤ c) : a ≤ c * b := by rw [← int.div_mul_cancel H2]; exact mul_le_mul_of_nonneg_right H3 H1 protected theorem lt_div_of_mul_lt {a b c : ℤ} (H1 : 0 ≤ b) (H2 : b ∣ c) (H3 : a * b < c) : a < c / b := lt_of_not_ge $ mt (int.le_mul_of_div_le H1 H2) (not_le_of_gt H3) protected theorem lt_div_iff_mul_lt {a b : ℤ} (c : ℤ) (H : 0 < c) (H' : c ∣ b) : a < b / c ↔ a * c < b := ⟨int.mul_lt_of_lt_div H, int.lt_div_of_mul_lt (le_of_lt H) H'⟩ theorem div_pos_of_pos_of_dvd {a b : ℤ} (H1 : 0 < a) (H2 : 0 ≤ b) (H3 : b ∣ a) : 0 < a / b := int.lt_div_of_mul_lt H2 H3 (by rwa zero_mul) theorem div_eq_div_of_mul_eq_mul {a b c d : ℤ} (H2 : d ∣ c) (H3 : b ≠ 0) (H4 : d ≠ 0) (H5 : a * d = b * c) : a / b = c / d := int.div_eq_of_eq_mul_right H3 $ by rw [← int.mul_div_assoc _ H2]; exact (int.div_eq_of_eq_mul_left H4 H5.symm).symm theorem eq_mul_div_of_mul_eq_mul_of_dvd_left {a b c d : ℤ} (hb : b ≠ 0) (hbc : b ∣ c) (h : b * a = c * d) : a = c / b * d := begin cases hbc with k hk, subst hk, rw int.mul_div_cancel_left, rw mul_assoc at h, apply _root_.eq_of_mul_eq_mul_left _ h, repeat {assumption} end theorem of_nat_add_neg_succ_of_nat_of_lt {m n : ℕ} (h : m < n.succ) : of_nat m + -[1+n] = -[1+ n - m] := begin change sub_nat_nat _ _ = _, have h' : n.succ - m = (n - m).succ, apply succ_sub, apply le_of_lt_succ h, simp [*, sub_nat_nat] end theorem of_nat_add_neg_succ_of_nat_of_ge {m n : ℕ} (h : n.succ ≤ m) : of_nat m + -[1+n] = of_nat (m - n.succ) := begin change sub_nat_nat _ _ = _, have h' : n.succ - m = 0, apply sub_eq_zero_of_le h, simp [*, sub_nat_nat] end @[simp] theorem neg_add_neg (m n : ℕ) : -[1+m] + -[1+n] = -[1+nat.succ(m+n)] := rfl /- to_nat -/ theorem to_nat_eq_max : ∀ (a : ℤ), (to_nat a : ℤ) = max a 0 | (n : ℕ) := (max_eq_left (coe_zero_le n)).symm | -[1+ n] := (max_eq_right (le_of_lt (neg_succ_lt_zero n))).symm @[simp] theorem to_nat_of_nonneg {a : ℤ} (h : 0 ≤ a) : (to_nat a : ℤ) = a := by rw [to_nat_eq_max, max_eq_left h] @[simp] lemma to_nat_sub_of_le (a b : ℤ) (h : b ≤ a) : (to_nat (a + -b) : ℤ) = a + - b := int.to_nat_of_nonneg (sub_nonneg_of_le h) @[simp] theorem to_nat_coe_nat (n : ℕ) : to_nat ↑n = n := rfl theorem le_to_nat (a : ℤ) : a ≤ to_nat a := by rw [to_nat_eq_max]; apply le_max_left @[simp] theorem to_nat_le {a : ℤ} {n : ℕ} : to_nat a ≤ n ↔ a ≤ n := by rw [(coe_nat_le_coe_nat_iff _ _).symm, to_nat_eq_max, max_le_iff]; exact and_iff_left (coe_zero_le _) @[simp] theorem lt_to_nat {n : ℕ} {a : ℤ} : n < to_nat a ↔ (n : ℤ) < a := le_iff_le_iff_lt_iff_lt.1 to_nat_le theorem to_nat_le_to_nat {a b : ℤ} (h : a ≤ b) : to_nat a ≤ to_nat b := by rw to_nat_le; exact le_trans h (le_to_nat b) theorem to_nat_lt_to_nat {a b : ℤ} (hb : 0 < b) : to_nat a < to_nat b ↔ a < b := ⟨λ h, begin cases a, exact lt_to_nat.1 h, exact lt_trans (neg_succ_of_nat_lt_zero a) hb, end, λ h, begin rw lt_to_nat, cases a, exact h, exact hb end⟩ theorem lt_of_to_nat_lt {a b : ℤ} (h : to_nat a < to_nat b) : a < b := (to_nat_lt_to_nat $ lt_to_nat.1 $ lt_of_le_of_lt (nat.zero_le _) h).1 h def to_nat' : ℤ → option ℕ | (n : ℕ) := some n | -[1+ n] := none theorem mem_to_nat' : ∀ (a : ℤ) (n : ℕ), n ∈ to_nat' a ↔ a = n | (m : ℕ) n := option.some_inj.trans coe_nat_inj'.symm | -[1+ m] n := by split; intro h; cases h /- units -/ @[simp] theorem units_nat_abs (u : units ℤ) : nat_abs u = 1 := units.ext_iff.1 $ nat.units_eq_one ⟨nat_abs u, nat_abs ↑u⁻¹, by rw [← nat_abs_mul, units.mul_inv]; refl, by rw [← nat_abs_mul, units.inv_mul]; refl⟩ theorem units_eq_one_or (u : units ℤ) : u = 1 ∨ u = -1 := by simpa [units.ext_iff, units_nat_abs] using nat_abs_eq u lemma units_inv_eq_self (u : units ℤ) : u⁻¹ = u := (units_eq_one_or u).elim (λ h, h.symm ▸ rfl) (λ h, h.symm ▸ rfl) /- bitwise ops -/ @[simp] lemma bodd_zero : bodd 0 = ff := rfl @[simp] lemma bodd_one : bodd 1 = tt := rfl @[simp] lemma bodd_two : bodd 2 = ff := rfl @[simp, elim_cast] lemma bodd_coe (n : ℕ) : int.bodd n = nat.bodd n := rfl @[simp] lemma bodd_sub_nat_nat (m n : ℕ) : bodd (sub_nat_nat m n) = bxor m.bodd n.bodd := by apply sub_nat_nat_elim m n (λ m n i, bodd i = bxor m.bodd n.bodd); intros; simp; cases i.bodd; simp @[simp] lemma bodd_neg_of_nat (n : ℕ) : bodd (neg_of_nat n) = n.bodd := by cases n; simp; refl @[simp] lemma bodd_neg (n : ℤ) : bodd (-n) = bodd n := by cases n; simp [has_neg.neg, int.coe_nat_eq, int.neg, bodd, -of_nat_eq_coe] @[simp] lemma bodd_add (m n : ℤ) : bodd (m + n) = bxor (bodd m) (bodd n) := by cases m with m m; cases n with n n; unfold has_add.add; simp [int.add, -of_nat_eq_coe, bool.bxor_comm] @[simp] lemma bodd_mul (m n : ℤ) : bodd (m * n) = bodd m && bodd n := by cases m with m m; cases n with n n; unfold has_mul.mul; simp [int.mul, -of_nat_eq_coe, bool.bxor_comm] theorem bodd_add_div2 : ∀ n, cond (bodd n) 1 0 + 2 * div2 n = n | (n : ℕ) := by rw [show (cond (bodd n) 1 0 : ℤ) = (cond (bodd n) 1 0 : ℕ), by cases bodd n; refl]; exact congr_arg of_nat n.bodd_add_div2 | -[1+ n] := begin refine eq.trans _ (congr_arg neg_succ_of_nat n.bodd_add_div2), dsimp [bodd], cases nat.bodd n; dsimp [cond, bnot, div2, int.mul], { change -[1+ 2 * nat.div2 n] = _, rw zero_add }, { rw [zero_add, add_comm], refl } end theorem div2_val : ∀ n, div2 n = n / 2 | (n : ℕ) := congr_arg of_nat n.div2_val | -[1+ n] := congr_arg neg_succ_of_nat n.div2_val lemma bit0_val (n : ℤ) : bit0 n = 2 * n := (two_mul _).symm lemma bit1_val (n : ℤ) : bit1 n = 2 * n + 1 := congr_arg (+(1:ℤ)) (bit0_val _) lemma bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by { cases b, apply (bit0_val n).trans (add_zero _).symm, apply bit1_val } lemma bit_decomp (n : ℤ) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans $ (add_comm _ _).trans $ bodd_add_div2 _ def {u} bit_cases_on {C : ℤ → Sort u} (n) (h : ∀ b n, C (bit b n)) : C n := by rw [← bit_decomp n]; apply h @[simp] lemma bit_zero : bit ff 0 = 0 := rfl @[simp] lemma bit_coe_nat (b) (n : ℕ) : bit b n = nat.bit b n := by rw [bit_val, nat.bit_val]; cases b; refl @[simp] lemma bit_neg_succ (b) (n : ℕ) : bit b -[1+ n] = -[1+ nat.bit (bnot b) n] := by rw [bit_val, nat.bit_val]; cases b; refl @[simp] lemma bodd_bit (b n) : bodd (bit b n) = b := by rw bit_val; simp; cases b; cases bodd n; refl @[simp] lemma div2_bit (b n) : div2 (bit b n) = n := begin rw [bit_val, div2_val, add_comm, int.add_mul_div_left, (_ : (_/2:ℤ) = 0), zero_add], cases b, all_goals {exact dec_trivial} end @[simp] lemma test_bit_zero (b) : ∀ n, test_bit (bit b n) 0 = b | (n : ℕ) := by rw [bit_coe_nat]; apply nat.test_bit_zero | -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_zero]; clear test_bit_zero; cases b; refl @[simp] lemma test_bit_succ (m b) : ∀ n, test_bit (bit b n) (nat.succ m) = test_bit n m | (n : ℕ) := by rw [bit_coe_nat]; apply nat.test_bit_succ | -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_succ] private meta def bitwise_tac : tactic unit := `[ funext m, funext n, cases m with m m; cases n with n n; try {refl}, all_goals { apply congr_arg of_nat <|> apply congr_arg neg_succ_of_nat, try {dsimp [nat.land, nat.ldiff, nat.lor]}, try {rw [ show nat.bitwise (λ a b, a && bnot b) n m = nat.bitwise (λ a b, b && bnot a) m n, from congr_fun (congr_fun (@nat.bitwise_swap (λ a b, b && bnot a) rfl) n) m]}, apply congr_arg (λ f, nat.bitwise f m n), funext a, funext b, cases a; cases b; refl }, all_goals {unfold nat.land nat.ldiff nat.lor} ] theorem bitwise_or : bitwise bor = lor := by bitwise_tac theorem bitwise_and : bitwise band = land := by bitwise_tac theorem bitwise_diff : bitwise (λ a b, a && bnot b) = ldiff := by bitwise_tac theorem bitwise_xor : bitwise bxor = lxor := by bitwise_tac @[simp] lemma bitwise_bit (f : bool → bool → bool) (a m b n) : bitwise f (bit a m) (bit b n) = bit (f a b) (bitwise f m n) := begin cases m with m m; cases n with n n; repeat { rw [← int.coe_nat_eq] <|> rw bit_coe_nat <|> rw bit_neg_succ }; unfold bitwise nat_bitwise bnot; [ induction h : f ff ff, induction h : f ff tt, induction h : f tt ff, induction h : f tt tt ], all_goals { unfold cond, rw nat.bitwise_bit, repeat { rw bit_coe_nat <|> rw bit_neg_succ <|> rw bnot_bnot } }, all_goals { unfold bnot {fail_if_unchanged := ff}; rw h; refl } end @[simp] lemma lor_bit (a m b n) : lor (bit a m) (bit b n) = bit (a || b) (lor m n) := by rw [← bitwise_or, bitwise_bit] @[simp] lemma land_bit (a m b n) : land (bit a m) (bit b n) = bit (a && b) (land m n) := by rw [← bitwise_and, bitwise_bit] @[simp] lemma ldiff_bit (a m b n) : ldiff (bit a m) (bit b n) = bit (a && bnot b) (ldiff m n) := by rw [← bitwise_diff, bitwise_bit] @[simp] lemma lxor_bit (a m b n) : lxor (bit a m) (bit b n) = bit (bxor a b) (lxor m n) := by rw [← bitwise_xor, bitwise_bit] @[simp] lemma lnot_bit (b) : ∀ n, lnot (bit b n) = bit (bnot b) (lnot n) | (n : ℕ) := by simp [lnot] | -[1+ n] := by simp [lnot] @[simp] lemma test_bit_bitwise (f : bool → bool → bool) (m n k) : test_bit (bitwise f m n) k = f (test_bit m k) (test_bit n k) := begin induction k with k IH generalizing m n; apply bit_cases_on m; intros a m'; apply bit_cases_on n; intros b n'; rw bitwise_bit, { simp [test_bit_zero] }, { simp [test_bit_succ, IH] } end @[simp] lemma test_bit_lor (m n k) : test_bit (lor m n) k = test_bit m k || test_bit n k := by rw [← bitwise_or, test_bit_bitwise] @[simp] lemma test_bit_land (m n k) : test_bit (land m n) k = test_bit m k && test_bit n k := by rw [← bitwise_and, test_bit_bitwise] @[simp] lemma test_bit_ldiff (m n k) : test_bit (ldiff m n) k = test_bit m k && bnot (test_bit n k) := by rw [← bitwise_diff, test_bit_bitwise] @[simp] lemma test_bit_lxor (m n k) : test_bit (lxor m n) k = bxor (test_bit m k) (test_bit n k) := by rw [← bitwise_xor, test_bit_bitwise] @[simp] lemma test_bit_lnot : ∀ n k, test_bit (lnot n) k = bnot (test_bit n k) | (n : ℕ) k := by simp [lnot, test_bit] | -[1+ n] k := by simp [lnot, test_bit] lemma shiftl_add : ∀ (m : ℤ) (n : ℕ) (k : ℤ), shiftl m (n + k) = shiftl (shiftl m n) k | (m : ℕ) n (k:ℕ) := congr_arg of_nat (nat.shiftl_add _ _ _) | -[1+ m] n (k:ℕ) := congr_arg neg_succ_of_nat (nat.shiftl'_add _ _ _ _) | (m : ℕ) n -[1+k] := sub_nat_nat_elim n k.succ (λ n k i, shiftl ↑m i = nat.shiftr (nat.shiftl m n) k) (λ i n, congr_arg coe $ by rw [← nat.shiftl_sub, nat.add_sub_cancel_left]; apply nat.le_add_right) (λ i n, congr_arg coe $ by rw [add_assoc, nat.shiftr_add, ← nat.shiftl_sub, nat.sub_self]; refl) | -[1+ m] n -[1+k] := sub_nat_nat_elim n k.succ (λ n k i, shiftl -[1+ m] i = -[1+ nat.shiftr (nat.shiftl' tt m n) k]) (λ i n, congr_arg neg_succ_of_nat $ by rw [← nat.shiftl'_sub, nat.add_sub_cancel_left]; apply nat.le_add_right) (λ i n, congr_arg neg_succ_of_nat $ by rw [add_assoc, nat.shiftr_add, ← nat.shiftl'_sub, nat.sub_self]; refl) lemma shiftl_sub (m : ℤ) (n : ℕ) (k : ℤ) : shiftl m (n - k) = shiftr (shiftl m n) k := shiftl_add _ _ _ @[simp] lemma shiftl_neg (m n : ℤ) : shiftl m (-n) = shiftr m n := rfl @[simp] lemma shiftr_neg (m n : ℤ) : shiftr m (-n) = shiftl m n := by rw [← shiftl_neg, neg_neg] @[simp] lemma shiftl_coe_nat (m n : ℕ) : shiftl m n = nat.shiftl m n := rfl @[simp] lemma shiftr_coe_nat (m n : ℕ) : shiftr m n = nat.shiftr m n := by cases n; refl @[simp] lemma shiftl_neg_succ (m n : ℕ) : shiftl -[1+ m] n = -[1+ nat.shiftl' tt m n] := rfl @[simp] lemma shiftr_neg_succ (m n : ℕ) : shiftr -[1+ m] n = -[1+ nat.shiftr m n] := by cases n; refl lemma shiftr_add : ∀ (m : ℤ) (n k : ℕ), shiftr m (n + k) = shiftr (shiftr m n) k | (m : ℕ) n k := by rw [shiftr_coe_nat, shiftr_coe_nat, ← int.coe_nat_add, shiftr_coe_nat, nat.shiftr_add] | -[1+ m] n k := by rw [shiftr_neg_succ, shiftr_neg_succ, ← int.coe_nat_add, shiftr_neg_succ, nat.shiftr_add] lemma shiftl_eq_mul_pow : ∀ (m : ℤ) (n : ℕ), shiftl m n = m * ↑(2 ^ n) | (m : ℕ) n := congr_arg coe (nat.shiftl_eq_mul_pow _ _) | -[1+ m] n := @congr_arg ℕ ℤ _ _ (λi, -i) (nat.shiftl'_tt_eq_mul_pow _ _) lemma shiftr_eq_div_pow : ∀ (m : ℤ) (n : ℕ), shiftr m n = m / ↑(2 ^ n) | (m : ℕ) n := by rw shiftr_coe_nat; exact congr_arg coe (nat.shiftr_eq_div_pow _ _) | -[1+ m] n := begin rw [shiftr_neg_succ, neg_succ_of_nat_div, nat.shiftr_eq_div_pow], refl, exact coe_nat_lt_coe_nat_of_lt (nat.pos_pow_of_pos _ dec_trivial) end lemma one_shiftl (n : ℕ) : shiftl 1 n = (2 ^ n : ℕ) := congr_arg coe (nat.one_shiftl _) @[simp] lemma zero_shiftl : ∀ n : ℤ, shiftl 0 n = 0 | (n : ℕ) := congr_arg coe (nat.zero_shiftl _) | -[1+ n] := congr_arg coe (nat.zero_shiftr _) @[simp] lemma zero_shiftr (n) : shiftr 0 n = 0 := zero_shiftl _ /- Least upper bound property for integers -/ section classical open_locale classical theorem exists_least_of_bdd {P : ℤ → Prop} (Hbdd : ∃ b : ℤ, ∀ z : ℤ, P z → b ≤ z) (Hinh : ∃ z : ℤ, P z) : ∃ lb : ℤ, P lb ∧ (∀ z : ℤ, P z → lb ≤ z) := let ⟨b, Hb⟩ := Hbdd in have EX : ∃ n : ℕ, P (b + n), from let ⟨elt, Helt⟩ := Hinh in match elt, le.dest (Hb _ Helt), Helt with | ._, ⟨n, rfl⟩, Hn := ⟨n, Hn⟩ end, ⟨b + (nat.find EX : ℤ), nat.find_spec EX, λ z h, match z, le.dest (Hb _ h), h with | ._, ⟨n, rfl⟩, h := add_le_add_left (int.coe_nat_le.2 $ nat.find_min' _ h) _ end⟩ theorem exists_greatest_of_bdd {P : ℤ → Prop} (Hbdd : ∃ b : ℤ, ∀ z : ℤ, P z → z ≤ b) (Hinh : ∃ z : ℤ, P z) : ∃ ub : ℤ, P ub ∧ (∀ z : ℤ, P z → z ≤ ub) := have Hbdd' : ∃ (b : ℤ), ∀ (z : ℤ), P (-z) → b ≤ z, from let ⟨b, Hb⟩ := Hbdd in ⟨-b, λ z h, neg_le.1 (Hb _ h)⟩, have Hinh' : ∃ z : ℤ, P (-z), from let ⟨elt, Helt⟩ := Hinh in ⟨-elt, by rw [neg_neg]; exact Helt⟩, let ⟨lb, Plb, al⟩ := exists_least_of_bdd Hbdd' Hinh' in ⟨-lb, Plb, λ z h, le_neg.1 $ al _ $ by rwa neg_neg⟩ end classical /- cast (injection into groups with one) -/ -- We use the int.has_coe instance for the simp-normal form. -- Increase the priority so that it is used preferentially. attribute [priority 1001] int.has_coe @[simp] theorem nat_cast_eq_coe_nat : ∀ n, @coe ℕ ℤ (@coe_to_lift _ _ (@coe_base _ _ nat.cast_coe)) n = @coe ℕ ℤ (@coe_to_lift _ _ (@coe_base _ _ int.has_coe)) n | 0 := rfl | (n+1) := congr_arg (+(1:ℤ)) (nat_cast_eq_coe_nat n) section cast variables {α : Type*} section variables [has_zero α] [has_one α] [has_add α] [has_neg α] /-- Canonical homomorphism from the integers to any ring(-like) structure `α` -/ protected def cast : ℤ → α | (n : ℕ) := n | -[1+ n] := -(n+1) @[priority 10] instance cast_coe : has_coe ℤ α := ⟨int.cast⟩ @[simp, squash_cast] theorem cast_zero : ((0 : ℤ) : α) = 0 := rfl theorem cast_of_nat (n : ℕ) : (of_nat n : α) = n := rfl @[simp, squash_cast] theorem cast_coe_nat (n : ℕ) : ((n : ℤ) : α) = n := rfl theorem cast_coe_nat' (n : ℕ) : (@coe ℕ ℤ (@coe_to_lift _ _ (@coe_base _ _ nat.cast_coe)) n : α) = n := by simp @[simp, move_cast] theorem cast_neg_succ_of_nat (n : ℕ) : (-[1+ n] : α) = -(n + 1) := rfl end @[simp, squash_cast] theorem cast_one [add_monoid α] [has_one α] [has_neg α] : ((1 : ℤ) : α) = 1 := nat.cast_one @[simp, move_cast] theorem cast_sub_nat_nat [add_group α] [has_one α] (m n) : ((int.sub_nat_nat m n : ℤ) : α) = m - n := begin unfold sub_nat_nat, cases e : n - m, { simp [sub_nat_nat, e, nat.le_of_sub_eq_zero e] }, { rw [sub_nat_nat, cast_neg_succ_of_nat, ← nat.cast_succ, ← e, nat.cast_sub $ _root_.le_of_lt $ nat.lt_of_sub_eq_succ e, neg_sub] }, end @[simp, move_cast] theorem cast_neg_of_nat [add_group α] [has_one α] : ∀ n, ((neg_of_nat n : ℤ) : α) = -n | 0 := neg_zero.symm | (n+1) := rfl @[simp, move_cast] theorem cast_add [add_group α] [has_one α] : ∀ m n, ((m + n : ℤ) : α) = m + n | (m : ℕ) (n : ℕ) := nat.cast_add _ _ | (m : ℕ) -[1+ n] := cast_sub_nat_nat _ _ | -[1+ m] (n : ℕ) := (cast_sub_nat_nat _ _).trans $ sub_eq_of_eq_add $ show (n:α) = -(m+1) + n + (m+1), by rw [add_assoc, ← cast_succ, ← nat.cast_add, add_comm, nat.cast_add, cast_succ, neg_add_cancel_left] | -[1+ m] -[1+ n] := show -((m + n + 1 + 1 : ℕ) : α) = -(m + 1) + -(n + 1), begin rw [← neg_add_rev, ← nat.cast_add_one, ← nat.cast_add_one, ← nat.cast_add], apply congr_arg (λ x:ℕ, -(x:α)), ac_refl end @[simp, move_cast] theorem cast_neg [add_group α] [has_one α] : ∀ n, ((-n : ℤ) : α) = -n | (n : ℕ) := cast_neg_of_nat _ | -[1+ n] := (neg_neg _).symm @[move_cast] theorem cast_sub [add_group α] [has_one α] (m n) : ((m - n : ℤ) : α) = m - n := by simp [sub_eq_add_neg] @[simp] theorem cast_eq_zero [add_group α] [has_one α] [char_zero α] {n : ℤ} : (n : α) = 0 ↔ n = 0 := ⟨λ h, begin cases n, { exact congr_arg coe (nat.cast_eq_zero.1 h) }, { rw [cast_neg_succ_of_nat, neg_eq_zero, ← cast_succ, nat.cast_eq_zero] at h, contradiction } end, λ h, by rw [h, cast_zero]⟩ @[simp, elim_cast] theorem cast_inj [add_group α] [has_one α] [char_zero α] {m n : ℤ} : (m : α) = n ↔ m = n := by rw [← sub_eq_zero, ← cast_sub, cast_eq_zero, sub_eq_zero] theorem cast_injective [add_group α] [has_one α] [char_zero α] : function.injective (coe : ℤ → α) | m n := cast_inj.1 theorem cast_ne_zero [add_group α] [has_one α] [char_zero α] {n : ℤ} : (n : α) ≠ 0 ↔ n ≠ 0 := not_congr cast_eq_zero @[simp, move_cast] theorem cast_mul [ring α] : ∀ m n, ((m * n : ℤ) : α) = m * n | (m : ℕ) (n : ℕ) := nat.cast_mul _ _ | (m : ℕ) -[1+ n] := (cast_neg_of_nat _).trans $ show (-(m * (n + 1) : ℕ) : α) = m * -(n + 1), by rw [nat.cast_mul, nat.cast_add_one, neg_mul_eq_mul_neg] | -[1+ m] (n : ℕ) := (cast_neg_of_nat _).trans $ show (-((m + 1) * n : ℕ) : α) = -(m + 1) * n, by rw [nat.cast_mul, nat.cast_add_one, neg_mul_eq_neg_mul] | -[1+ m] -[1+ n] := show (((m + 1) * (n + 1) : ℕ) : α) = -(m + 1) * -(n + 1), by rw [nat.cast_mul, nat.cast_add_one, nat.cast_add_one, neg_mul_neg] /-- `coe : ℤ → α` as a `ring_hom`. -/ def cast_ring_hom (α : Type*) [ring α] : ℤ →+* α := ⟨coe, cast_one, cast_mul, cast_zero, cast_add⟩ @[simp] lemma coe_cast_ring_hom [ring α] : ⇑(cast_ring_hom α) = coe := rfl theorem mul_cast_comm [ring α] (a : α) (n : ℤ) : a * n = n * a := by cases n; simp [nat.mul_cast_comm, left_distrib, right_distrib, *] @[simp, squash_cast, move_cast] theorem coe_nat_bit0 (n : ℕ) : (↑(bit0 n) : ℤ) = bit0 ↑n := by {unfold bit0, simp} @[simp, squash_cast, move_cast] theorem coe_nat_bit1 (n : ℕ) : (↑(bit1 n) : ℤ) = bit1 ↑n := by {unfold bit1, unfold bit0, simp} @[simp, squash_cast, move_cast] theorem cast_bit0 [ring α] (n : ℤ) : ((bit0 n : ℤ) : α) = bit0 n := cast_add _ _ @[simp, squash_cast, move_cast] theorem cast_bit1 [ring α] (n : ℤ) : ((bit1 n : ℤ) : α) = bit1 n := by rw [bit1, cast_add, cast_one, cast_bit0]; refl lemma cast_two [ring α] : ((2 : ℤ) : α) = 2 := by simp theorem cast_nonneg [linear_ordered_ring α] : ∀ {n : ℤ}, (0 : α) ≤ n ↔ 0 ≤ n | (n : ℕ) := by simp | -[1+ n] := by simpa [not_le_of_gt (neg_succ_lt_zero n)] using show -(n:α) < 1, from lt_of_le_of_lt (by simp) zero_lt_one @[simp, elim_cast] theorem cast_le [linear_ordered_ring α] {m n : ℤ} : (m : α) ≤ n ↔ m ≤ n := by rw [← sub_nonneg, ← cast_sub, cast_nonneg, sub_nonneg] @[simp, elim_cast] theorem cast_lt [linear_ordered_ring α] {m n : ℤ} : (m : α) < n ↔ m < n := by simpa [-cast_le] using not_congr (@cast_le α _ n m) @[simp] theorem cast_nonpos [linear_ordered_ring α] {n : ℤ} : (n : α) ≤ 0 ↔ n ≤ 0 := by rw [← cast_zero, cast_le] @[simp] theorem cast_pos [linear_ordered_ring α] {n : ℤ} : (0 : α) < n ↔ 0 < n := by rw [← cast_zero, cast_lt] @[simp] theorem cast_lt_zero [linear_ordered_ring α] {n : ℤ} : (n : α) < 0 ↔ n < 0 := by rw [← cast_zero, cast_lt] theorem eq_cast [add_group α] [has_one α] (f : ℤ → α) (H1 : f 1 = 1) (Hadd : ∀ x y, f (x + y) = f x + f y) (n : ℤ) : f n = n := begin have H : ∀ (n : ℕ), f n = n := nat.eq_cast' (λ n, f n) H1 (λ x y, Hadd x y), cases n, {apply H}, apply eq_neg_of_add_eq_zero, rw [← nat.cast_zero, ← H 0, int.coe_nat_zero, ← show -[1+ n] + (↑n + 1) = 0, from neg_add_self (↑n+1), Hadd, show f (n+1) = n+1, from H (n+1)] end @[simp, squash_cast] theorem cast_id (n : ℤ) : ↑n = n := (eq_cast id rfl (λ _ _, rfl) n).symm @[simp, move_cast] theorem cast_min [decidable_linear_ordered_comm_ring α] {a b : ℤ} : (↑(min a b) : α) = min a b := by by_cases a ≤ b; simp [h, min] @[simp, move_cast] theorem cast_max [decidable_linear_ordered_comm_ring α] {a b : ℤ} : (↑(max a b) : α) = max a b := by by_cases a ≤ b; simp [h, max] @[simp, move_cast] theorem cast_abs [decidable_linear_ordered_comm_ring α] {q : ℤ} : ((abs q : ℤ) : α) = abs q := by simp [abs] end cast section decidable def range (m n : ℤ) : list ℤ := (list.range (to_nat (n-m))).map $ λ r, m+r theorem mem_range_iff {m n r : ℤ} : r ∈ range m n ↔ m ≤ r ∧ r < n := ⟨λ H, let ⟨s, h1, h2⟩ := list.mem_map.1 H in h2 ▸ ⟨le_add_of_nonneg_right trivial, add_lt_of_lt_sub_left $ match n-m, h1 with | (k:ℕ), h1 := by rwa [list.mem_range, to_nat_coe_nat, ← coe_nat_lt] at h1 end⟩, λ ⟨h1, h2⟩, list.mem_map.2 ⟨to_nat (r-m), list.mem_range.2 $ by rw [← coe_nat_lt, to_nat_of_nonneg (sub_nonneg_of_le h1), to_nat_of_nonneg (sub_nonneg_of_le (le_of_lt (lt_of_le_of_lt h1 h2)))]; exact sub_lt_sub_right h2 _, show m + _ = _, by rw [to_nat_of_nonneg (sub_nonneg_of_le h1), add_sub_cancel'_right]⟩⟩ instance decidable_le_lt (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m ≤ r → r < n → P r) := decidable_of_iff (∀ r ∈ range m n, P r) $ by simp only [mem_range_iff, and_imp] instance decidable_le_le (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m ≤ r → r ≤ n → P r) := decidable_of_iff (∀ r ∈ range m (n+1), P r) $ by simp only [mem_range_iff, and_imp, lt_add_one_iff] instance decidable_lt_lt (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m < r → r < n → P r) := int.decidable_le_lt P _ _ instance decidable_lt_le (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m < r → r ≤ n → P r) := int.decidable_le_le P _ _ end decidable end int namespace ring_hom variables {α : Type*} {β : Type*} {rα : ring α} {rβ : ring β} include rα @[simp] lemma eq_int_cast (f : ℤ →+* α) (n : ℤ) : f n = n := int.eq_cast f f.map_one f.map_add n lemma eq_int_cast' (f : ℤ →+* α) : f = int.cast_ring_hom α := ring_hom.ext $ int.eq_cast f f.map_one f.map_add include rβ @[simp] lemma map_int_cast (f : α →+* β) (n : ℤ) : f n = n := (f.comp (int.cast_ring_hom α)).eq_int_cast n end ring_hom
8b7b4fb52f5f0f64792f07f25e7179910416e22c
43390109ab88557e6090f3245c47479c123ee500
/src/Topology/Material/topological_sequences.lean
8a47038b47e23301dc1514ef154d81e1aff1e917
[ "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
16,737
lean
/- Copyright (c) 2018 Rohan Mitta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rohan Mitta -/ import analysis.metric_space import analysis.topology.topological_space import order.filter import tactic.norm_num noncomputable theory local attribute [instance] classical.prop_decidable --Patrick's Lemmas variables {α : Type*} {β : Type*} open filter lemma tendsto_nhds_iff [metric_space α] (u : β → α) (f : filter β) (a : α) : tendsto u f (nhds a) ↔ ∀ ε > 0, ∃ s ∈ f.sets, ∀ {n}, n ∈ s → dist (u n) a < ε := ⟨λ H ε εpos, ⟨u ⁻¹' ball a ε, ⟨H $ ball_mem_nhds a εpos, λ n h, h⟩⟩, λ H s s_nhd, let ⟨ε, εpos, sub⟩ := mem_nhds_iff_metric.1 s_nhd in let ⟨N, ⟨N_in, H'⟩⟩ := H ε εpos in f.sets_of_superset N_in (λ b b_in, sub $ H' b_in)⟩ lemma seq_tendsto_iff [metric_space α] (u : ℕ → α) (a : α) : tendsto u at_top (nhds a) ↔ ∀ ε > 0, ∃ (N : ℕ), ∀ {n}, n ≥ N → dist (u n) a < ε := ⟨λ H ε εpos, mem_at_top_sets.1 $ mem_map.2 $ H (ball_mem_nhds _ εpos), λ H s s_nhd, let ⟨ε, εpos, sub⟩ := mem_nhds_iff_metric.1 s_nhd in let ⟨N, H'⟩ := H ε εpos in mem_at_top_sets.2 ⟨N, λ n nN, sub $ mem_ball.2 $ H' nN⟩⟩ --Sutherland Exercise 6.26 (as setup for prop 17.6) theorem lim_sequence_of_mem_closure {α : Type*} [metric_space α] {Y : set α} {a : α} (H : a ∈ closure Y) : ∃ (f : ℕ → α) (H1 : ∀ (n : ℕ), f n ∈ Y), filter.tendsto f at_top (nhds a) := begin let ball_n := λ (n : ℕ), ball a ((1 : ℝ)/n), have H1 : ∀ (n : ℕ), nonempty {x : α | x ∈ (ball_n (n+1)) ∩ Y}, { intro n, apply @nonempty_of_exists _ (λ _,true), have H3 := set.exists_mem_of_ne_empty ((mem_closure_iff_nhds.1 H) (ball_n (n+1)) (ball_mem_nhds _ _)), { cases H3 with xn Hxn, existsi (⟨xn, Hxn⟩ : ↥{x : α | x ∈ ball_n (n+1) ∩ Y}), trivial }, apply div_pos, exact zero_lt_one, rw ← nat.cast_zero, apply nat.cast_lt.2, apply zero_lt_iff_ne_zero.2, apply nat.succ_ne_zero }, have sequence := λ (n : ℕ), classical.choice (H1 n), let sequencevals := λ (n : ℕ), (sequence n).val, existsi sequencevals, have H1 : ∀ (n : ℕ), sequencevals n ∈ Y, { show ∀ (n : ℕ), (sequence n).val ∈ Y, let sequenceprops := λ (n : ℕ), ((sequence n).property).2, exact sequenceprops }, existsi H1, rw tendsto_nhds_iff _ _ _, intros ep Hep, let nat_one_over_ep := int.nat_abs (ceil (1/ep)), existsi [{n : ℕ | nat_one_over_ep ≤ n}, _], swap, { rw filter.mem_at_top_sets, existsi nat_one_over_ep, exact λ b Hb, Hb }, intros n Hn, show dist (sequence n).val a < ep, have : dist (sequence n).val a < (1 / ↑(n+1)) := (sequence n).property.1, apply lt.trans this, rw one_div_eq_inv, have H3: 0 < (↑(n + 1) : ℝ), { rw ← nat.cast_zero, rw (@nat.cast_lt ℝ _ 0 (n+1)), exact zero_lt_iff_ne_zero.2 (nat.succ_ne_zero n) }, rw (@inv_lt ℝ _ ↑(n + 1) ep) H3 Hep, dsimp at Hn, rw ← one_div_eq_inv, have H4 := nat.lt_succ_of_le Hn, rw [nat.succ_eq_add_one, ← @nat.cast_lt ℝ _ nat_one_over_ep (n+1)] at H4, exact lt_of_le_of_lt (le_trans (le_ceil (1 / ep)) ((@int.cast_le ℝ _ _ _).2 (@int.le_nat_abs ⌈1 / ep⌉))) H4, end --We think here of sequences as functions (f : ℕ → α) def metric_space.seq_cauchy [metric_space α] (u : ℕ → α) : Prop := cauchy (filter.map u at_top) lemma metric_space.seq_cauchy_of_mathematician [metric_space α] (u : ℕ → α) : metric_space.seq_cauchy u ↔ ∀ ε > 0, ∃ (N : ℕ), ∀ {n m}, n ≥ N → m ≥ N → dist (u n) (u m) < ε := begin split, { intros H ε Hε, unfold metric_space.seq_cauchy at H, rw cauchy_of_metric at H, rcases H.2 ε Hε with ⟨t, Ht, Ht2⟩, rw [mem_map, mem_at_top_sets] at Ht, cases Ht with N HN, existsi N, intros n m Hn Hm, exact Ht2 (u n) (u m) (HN n Hn) (HN m Hm) }, intro H, unfold metric_space.seq_cauchy, rw cauchy_of_metric, apply and.intro _, { intros ε Hε, cases H ε Hε with N HN, existsi u '' {x : ℕ | N ≤ x}, existsi _, swap, { rw [mem_map, mem_at_top_sets], existsi N, intros b Hb, rw [set.mem_set_of_eq, set.mem_image], existsi b, exact ⟨Hb, rfl⟩ }, intros x y Hx Hy, rw set.mem_image at Hx, rw set.mem_image at Hy, cases Hx with n Hn, cases Hy with m Hm, have := HN Hn.1 Hm.1, rw Hn.2 at this, rw Hm.2 at this, assumption }, exact map_ne_bot at_top_ne_bot, end def metric_space.seq_tendsto [metric_space α] (u : ℕ → α) (a : α) : Prop := ∀ ε > 0, ∃ (N : ℕ), ∀ {n}, n ≥ N → dist (u n) a < ε lemma metric_space.unique_limit_seq [metric_space α] (u : ℕ → α) (a b : α) (Ha : metric_space.seq_tendsto u a) (Hb : metric_space.seq_tendsto u b) : a = b := begin unfold metric_space.seq_tendsto at Ha, unfold metric_space.seq_tendsto at Hb, apply metric_space.eq_of_dist_eq_zero, by_contradiction Hnab, cases @dist_nonneg _ _ a b, swap, cc, cases Ha ((dist a b)/2) (div_pos h (by norm_num)) with N Ha1, cases Hb ((dist a b)/2) (div_pos h (by norm_num)) with M Hb1, let k := max N M, have Ha2 : dist (u k) a < dist a b / 2:= Ha1 (le_max_left N M), have Hb2 : dist (u k) b < dist a b / 2:= Hb1 (le_max_right N M), rw dist_comm at Ha2, have := add_lt_add Ha2 Hb2, have this2 := dist_triangle a (u k) b, have this3 := lt_of_le_of_lt this2 this, rw [← two_mul, mul_div_cancel' (dist a b) two_ne_zero] at this3, exact lt_irrefl (dist a b) this3, end lemma metric_space.cauchy_of_convergent [metric_space α] (u : ℕ → α) (H : ∃ (a : α), metric_space.seq_tendsto u a) : metric_space.seq_cauchy u := begin rw metric_space.seq_cauchy_of_mathematician, cases H with a Ha, intros ε Hε, unfold metric_space.seq_tendsto at Ha, cases Ha (ε / 2) (div_pos Hε (by norm_num)) with N HN, existsi N, intros n m Hn Hm, have dist_m := HN Hm, rw dist_comm at dist_m, have := add_lt_add (HN Hn) (dist_m), rw [← two_mul (ε / 2), mul_div_cancel' ε two_ne_zero] at this, exact lt_of_le_of_lt (dist_triangle (u n) a (u m)) this, end lemma subtype.seq_cauchy [metric_space α] {Y : set α} (u : ℕ → α) (H1 : ∀ (n : ℕ), u n ∈ Y) : metric_space.seq_cauchy u ↔ metric_space.seq_cauchy (λ (n : ℕ), (⟨u n, H1 n⟩ : Y)) := by rw metric_space.seq_cauchy_of_mathematician; rw metric_space.seq_cauchy_of_mathematician; refl lemma subtype.seq_tendsto [metric_space α] {Y : set α} (u : ℕ → α) (H1 : ∀ (n : ℕ), u n ∈ Y) {a : α} (H2 : a ∈ Y) : metric_space.seq_tendsto u a ↔ metric_space.seq_tendsto (λ (n : ℕ), (⟨u n, H1 n⟩ : Y)) ⟨a, H2⟩ := by refl theorem metric_space.convergent_of_cauchy_of_complete [metric_space α] (u : ℕ → α) [complete_space α] (H : metric_space.seq_cauchy u) : ∃ (x : α), metric_space.seq_tendsto u x := let ⟨a, Ha⟩ := (complete_space.complete H) in ⟨a, by change tendsto u at_top (nhds a) at Ha; exact (seq_tendsto_iff u a).1 Ha⟩ --Proposition 17.6 theorem closed_of_complete_subspace_of_metric {α : Type*} [metric_space α] (Y : set α) [complete_space Y] : is_closed Y := begin rw ← closure_eq_iff_is_closed, apply set.eq_of_subset_of_subset, { intros x Hx, rcases lim_sequence_of_mem_closure Hx with ⟨sequence, Hxn, Hsequence⟩, rw seq_tendsto_iff at Hsequence, have Ha := metric_space.convergent_of_cauchy_of_complete (λ (n : ℕ), (⟨sequence n, Hxn n⟩ : Y)) ((subtype.seq_cauchy sequence Hxn).1 (metric_space.cauchy_of_convergent sequence ⟨x, Hsequence⟩)), cases Ha with a Ha, change metric_space.seq_tendsto (λ (n : ℕ), (⟨sequence n, Hxn n⟩ : Y)) a at Ha, cases a with a ha, rw ← subtype.seq_tendsto at Ha, change metric_space.seq_tendsto sequence x at Hsequence, change metric_space.seq_tendsto sequence a at Ha, have H4 := metric_space.unique_limit_seq sequence x a Hsequence Ha, rw H4, exact ha }, exact subset_closure, end --Lemma for following lemma --Showing the filter definition of complete is equivalent to the sequences defintion for a metric space lemma complete_iff_seq_complete {α : Type*} [metric_space α] : complete_space α ↔ ( ∀ (f : ℕ → α), cauchy (filter.map f at_top) → (∃ (a : α), tendsto f at_top (nhds a))) := begin split, intros H f Hf, exact (@complete_space.complete _ _ H _ Hf), intro H, split, intros filt Hfilt, rw cauchy_of_metric at Hfilt, have this1 : ∀ n, 0 < (↑(n + 1) : ℝ) := by intro n; rw ← nat.cast_zero; rw (@nat.cast_lt ℝ _ 0 (n+1)); exact zero_lt_iff_ne_zero.2 (nat.succ_ne_zero n), have this2 := λ (n : ℕ), (@div_pos ℝ _ 1 (n+1) (nat.cast_lt.2 (@zero_lt_one ℕ _)) (this1 n)), have this3 := λ n, (Hfilt.2 ((1 : ℝ)/(n+1 : ℕ))) (this2 n), have this4 := classical.axiom_of_choice (this3), cases this4 with f Hf, dsimp at f, dsimp at Hf, cases (classical.axiom_of_choice Hf) with Hf1 Hf2, dsimp at Hf1, dsimp at Hf2, have H3 : ∀ n, (f n) ≠ ∅, intro n, by_contradiction, rw not_not at a, have H2 := Hf1 n, rw a at H2, have H1 := empty_in_sets_eq_bot.1 H2, cc, have H4 : ∀ n, nonempty (f n), intro n, cases set.exists_mem_of_ne_empty (H3 n) with x Hx, constructor, exact ⟨x, Hx⟩, have seq_prop_better : ∀ (n : ℕ), ∃ (S : set α), (∀ (m : ℕ), m ≤ n → (S ⊆ (f m))) ∧ S ∈ filt.sets, intro n, induction n with N HN, existsi (f 0), exact ⟨λ n Hn, by rw (le_zero_iff_eq.1 Hn); exact (set.subset.refl (f 0)), Hf1 0⟩, cases HN with S0 HS0, existsi S0 ∩ (f (N+1)), refine ⟨_,inter_mem_sets HS0.2 (Hf1 (N+1))⟩, intro n, by_cases (n ≤ N), exact λ _ x Hx, (HS0.1 n h) Hx.1, rw not_le at h, intro Hn, rw (le_antisymm Hn (nat.succ_le_of_lt h)), intros x Hx, exact Hx.2, have seq_prop2 := classical.axiom_of_choice seq_prop_better, dsimp at seq_prop2, cases seq_prop2 with seqsets Hseqsets, have Hnonempty : ∀ n, nonempty (seqsets n), intro n, have Hnotempty : seqsets n ≠ ∅, by_contradiction, rw not_not at a, have := Hseqsets n, rw a at this, have := empty_in_sets_eq_bot.1 this.2, cc, cases set.exists_mem_of_ne_empty Hnotempty, constructor, exact ⟨w,h⟩, have seq := λ (n : ℕ), classical.choice (Hnonempty n), have FGI : cauchy (map (λ (n : ℕ), (seq n).val) at_top), rw cauchy_of_metric, apply and.intro (map_ne_bot at_top_ne_bot), intros ε Hε, existsi (f (int.nat_abs (ceil ((1:ℝ)/ε)))), have Hnext : f (int.nat_abs ⌈1 / ε⌉) ∈ (map (λ (n : ℕ), (seq n).val) at_top).sets, rw mem_map, rw mem_at_top_sets, existsi (int.nat_abs ⌈1 / ε⌉), intros m Hm, exact (Hseqsets m).1 (int.nat_abs ⌈1 / ε⌉) Hm (seq m).property, existsi Hnext, intros x y Hx Hy, have exciting := Hf2 (int.nat_abs ⌈1 / ε⌉) x y Hx Hy, have Hnext3 : 1/ε > 0, apply (mul_lt_mul_right Hε).1, rw one_div_eq_inv, rw zero_mul, rw inv_mul_cancel (ne.symm (ne_of_lt Hε)), exact zero_lt_one, have Hnext2 : ceil (1/ε) ≥ 0, exact le_of_lt (ceil_pos.2 Hnext3), have Hnext : 1 / (↑(int.nat_abs ⌈1 / ε⌉) + 1) < ε, rw [← int.cast_coe_nat (int.nat_abs _), int.nat_abs_of_nonneg Hnext2], have Hfornext4 := lt_add_one (↑⌈1 / ε⌉ : ℝ), have Hnext4 : (1 : ℝ) / (↑⌈1 / ε⌉ + 1) < 1 / (↑⌈1 / ε⌉), exact one_div_lt_one_div_of_lt (int.cast_lt.2 (ceil_pos.2 Hnext3)) Hfornext4, apply lt_of_lt_of_le Hnext4, have Hnext5 := le_ceil (1/ε), have Hnext6 := one_div_le_one_div_of_le Hnext3 Hnext5, apply le_trans Hnext6, simp, exact lt_trans exciting Hnext, exact ⟨0⟩, have := H (λ n, (seq n).1) FGI, cases this with a Ha, existsi a, unfold tendsto at Ha, intros S HS, rcases mem_nhds_sets_iff.1 HS with ⟨S1, HS1, H2S1⟩, rcases is_open_metric.1 H2S1.1 a H2S1.2 with ⟨ε, Hε, Hballε⟩, have Hepover2 : ε/2 > 0 := div_pos Hε (by norm_num), cases (seq_tendsto_iff (λ (n : ℕ), (seq n).val) a).1 Ha (ε/2) Hepover2 with N1 HN1, let N := max N1 (int.nat_abs ⌈2 / ε⌉), have HS3 : f N ⊆ ball a ε, intros x Hx, rw mem_ball, dsimp at HN1, have distance1 := Hf2 N x (seq N).val Hx ((Hseqsets N).1 N (le_refl N) (seq N).property), have lt_εover2 : 1 / (↑N + 1) < ε / 2, have le_somth := le_max_right N1 (int.nat_abs ⌈2 / ε⌉), have twoovereplt : 2 / ε < ↑(nat.succ N), apply lt_of_le_of_lt (le_ceil (2/ε)), have N_ge_ceil : int.nat_abs ⌈2 / ε⌉ ≤ N, exact le_max_right _ _, have N_ge_ceil2 : ((int.nat_abs ⌈2 / ε⌉) : ℝ) ≤ ↑N := nat.cast_le.2 N_ge_ceil, have zero_le_ceiltwooverep : (0 : ℤ) ≤ ⌈2 / ε⌉, have zero_lt_twooverep : 0 < 2/ε, have := div_div_eq_mul_div 1 2 ε, simp at this, rw ← this at Hepover2, exact inv_pos'.1 Hepover2, apply int.cast_le.1, refine le_of_lt (lt_of_lt_of_le zero_lt_twooverep _), exact le_ceil _, have := int.nat_abs_of_nonneg zero_le_ceiltwooverep, change (((int.nat_abs ⌈2 / ε⌉) : ℤ) : ℝ) ≤ _ at N_ge_ceil2, rw this at N_ge_ceil2, refine lt_of_le_of_lt N_ge_ceil2 _, simp [zero_lt_one], have := (inv_lt_inv (nat.cast_lt.2 (nat.zero_lt_succ N) : (0 : ℝ) < _) (div_pos (by norm_num) Hε : (2 : ℝ)/ε > 0)).2 twoovereplt, rw inv_eq_one_div at this, rw inv_eq_one_div at this, rw div_div_eq_mul_div at this, rw one_mul at this, exact this, have distance2 := HN1 (le_max_left N1 (int.nat_abs ⌈2 / ε⌉)), have distance3 := dist_triangle x (seq N).val a, have distance4 := add_lt_add (lt_trans distance1 lt_εover2) distance2, have distance5 := lt_of_le_of_lt distance3 distance4, rw add_halves at distance5, exact distance5, apply mem_sets_of_superset (Hf1 N), exact set.subset.trans (set.subset.trans HS3 Hballε) HS1, end --Proposition 17.7 theorem complete_of_closed_subspace_of_complete {α : Type*} [metric_space α] [complete_space α] (Y : set α) (HY : is_closed Y) : complete_space Y := begin rw complete_iff_seq_complete, intros f Hf, rw complete_iff_seq_complete at _inst_2, have : metric_space.seq_cauchy f := Hf, have this2 : f = (λ (n : ℕ), (⟨(f n).val, (f n).property⟩ : Y)), { simp }, rw this2 at this, cases _inst_2 (λ n, (f n).val) ((subtype.seq_cauchy _ _).2 this) with a Ha, have H2 : a ∈ Y, { apply mem_of_closed_of_tendsto at_top_ne_bot Ha HY, rw mem_at_top_sets, existsi 0, exact λ n _, (f n).property }, existsi (⟨a, H2⟩ : Y), have H3 := subtype.seq_tendsto (λ n, (f n).val) (λ n, (f n).property) H2, simp at H3, rw seq_tendsto_iff, apply H3.1, exact (seq_tendsto_iff (λ (n : ℕ), (f n).val) a).1 Ha end def subseq {α : Type*} (f : ℕ → α) (u : ℕ → α) := ∃ (map : ℕ → ℕ), u = f ∘ map ∧ tendsto map at_top at_top --tendsto map at_top at_top is the same as being a subsequence, but we don't require strict increasingness --Prop 17.10 theorem convergent_of_cauchy_of_subseq_convergent {α : Type*} [metric_space α] {f : ℕ → α} (H : cauchy (filter.map f at_top)) {sub : ℕ → α} (H1 : subseq sub f) {x : α} (H2 : metric_space.seq_tendsto sub x) : metric_space.seq_tendsto f x := begin unfold subseq at H1, cases H1 with map Hmap, rw Hmap.1, unfold tendsto at Hmap, unfold metric_space.seq_tendsto, rw ← seq_tendsto_iff, unfold metric_space.seq_tendsto at H2, rw ← seq_tendsto_iff at H2, apply tendsto.comp, { exact tendsto_id }, apply tendsto.comp, { exact Hmap.2 }, exact H2, end theorem convergent_of_cauchy_of_subseq_convergent' {α : Type*} [metric_space α] {f : ℕ → α} (H : cauchy (filter.map f at_top)) {sub : ℕ → α} (H1 : subseq sub f) {x : α} (H2 : metric_space.seq_tendsto sub x) : metric_space.seq_tendsto f x := begin cases H1 with map Hmap, rw Hmap.1, unfold tendsto at Hmap, unfold metric_space.seq_tendsto, rw ← seq_tendsto_iff, unfold metric_space.seq_tendsto at H2, rw ← seq_tendsto_iff at H2, exact tendsto.comp tendsto_id (tendsto.comp Hmap.2 H2), end
d186f11a7c338a7221a86e2e811c63a7bac19943
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/linear_algebra/basic.lean
75b5ff14f46e1164eb79d7edf4569eba61a120d5
[ "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
86,249
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Kevin Buzzard, Yury Kudryashov, Frédéric Dupuis, Heather Macbeth -/ import algebra.big_operators.pi import algebra.module.hom import algebra.module.prod import algebra.module.submodule.lattice import data.dfinsupp.basic import data.finsupp.basic /-! # Linear algebra > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the basics of linear algebra. It sets up the "categorical/lattice structure" of modules over a ring, submodules, and linear maps. Many of the relevant definitions, including `module`, `submodule`, and `linear_map`, are found in `src/algebra/module`. ## Main definitions * Many constructors for (semi)linear maps * The kernel `ker` and range `range` of a linear map are submodules of the domain and codomain respectively. See `linear_algebra.span` for the span of a set (as a submodule), and `linear_algebra.quotient` for quotients by submodules. ## Main theorems See `linear_algebra.isomorphisms` for Noether's three isomorphism theorems for modules. ## Notations * We continue to use the notations `M →ₛₗ[σ] M₂` and `M →ₗ[R] M₂` for the type of semilinear (resp. linear) maps from `M` to `M₂` over the ring homomorphism `σ` (resp. over the ring `R`). ## Implementation notes We note that, when constructing linear maps, it is convenient to use operations defined on bundled maps (`linear_map.prod`, `linear_map.coprod`, arithmetic operations like `+`) instead of defining a function and proving it is linear. ## TODO * Parts of this file have not yet been generalized to semilinear maps ## Tags linear algebra, vector space, module -/ open function open_locale big_operators pointwise variables {R : Type*} {R₁ : Type*} {R₂ : Type*} {R₃ : Type*} {R₄ : Type*} variables {S : Type*} variables {K : Type*} {K₂ : Type*} variables {M : Type*} {M' : Type*} {M₁ : Type*} {M₂ : Type*} {M₃ : Type*} {M₄ : Type*} variables {N : Type*} {N₂ : Type*} variables {ι : Type*} variables {V : Type*} {V₂ : Type*} namespace finsupp lemma smul_sum {α : Type*} {β : Type*} {R : Type*} {M : Type*} [has_zero β] [add_comm_monoid M] [distrib_smul R M] {v : α →₀ β} {c : R} {h : α → β → M} : c • (v.sum h) = v.sum (λa b, c • h a b) := finset.smul_sum @[simp] lemma sum_smul_index_linear_map' {α : Type*} {R : Type*} {M : Type*} {M₂ : Type*} [semiring R] [add_comm_monoid M] [module R M] [add_comm_monoid M₂] [module R M₂] {v : α →₀ M} {c : R} {h : α → M →ₗ[R] M₂} : (c • v).sum (λ a, h a) = c • (v.sum (λ a, h a)) := begin rw [finsupp.sum_smul_index', finsupp.smul_sum], { simp only [map_smul], }, { intro i, exact (h i).map_zero }, end variables (α : Type*) [finite α] variables (R M) [add_comm_monoid M] [semiring R] [module R M] /-- Given `finite α`, `linear_equiv_fun_on_finite R` is the natural `R`-linear equivalence between `α →₀ β` and `α → β`. -/ @[simps apply] noncomputable def linear_equiv_fun_on_finite : (α →₀ M) ≃ₗ[R] (α → M) := { to_fun := coe_fn, map_add' := λ f g, rfl, map_smul' := λ c f, rfl, .. equiv_fun_on_finite } @[simp] lemma linear_equiv_fun_on_finite_single [decidable_eq α] (x : α) (m : M) : (linear_equiv_fun_on_finite R M α) (single x m) = pi.single x m := equiv_fun_on_finite_single x m @[simp] lemma linear_equiv_fun_on_finite_symm_single [decidable_eq α] (x : α) (m : M) : (linear_equiv_fun_on_finite R M α).symm (pi.single x m) = single x m := equiv_fun_on_finite_symm_single x m @[simp] lemma linear_equiv_fun_on_finite_symm_coe (f : α →₀ M) : (linear_equiv_fun_on_finite R M α).symm f = f := (linear_equiv_fun_on_finite R M α).symm_apply_apply f /-- If `α` has a unique term, then the type of finitely supported functions `α →₀ M` is `R`-linearly equivalent to `M`. -/ noncomputable def linear_equiv.finsupp_unique (α : Type*) [unique α] : (α →₀ M) ≃ₗ[R] M := { map_add' := λ x y, rfl, map_smul' := λ r x, rfl, ..finsupp.equiv_fun_on_finite.trans (equiv.fun_unique α M) } variables {R M α} @[simp] lemma linear_equiv.finsupp_unique_apply (α : Type*) [unique α] (f : α →₀ M) : linear_equiv.finsupp_unique R M α f = f default := rfl @[simp] lemma linear_equiv.finsupp_unique_symm_apply {α : Type*} [unique α] (m : M) : (linear_equiv.finsupp_unique R M α).symm m = finsupp.single default m := by ext; simp [linear_equiv.finsupp_unique] end finsupp /-- decomposing `x : ι → R` as a sum along the canonical basis -/ lemma pi_eq_sum_univ {ι : Type*} [fintype ι] [decidable_eq ι] {R : Type*} [semiring R] (x : ι → R) : x = ∑ i, x i • (λj, if i = j then 1 else 0) := by { ext, simp } /-! ### Properties of linear maps -/ namespace linear_map section add_comm_monoid variables [semiring R] [semiring R₂] [semiring R₃] [semiring R₄] variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] variables [add_comm_monoid M₃] [add_comm_monoid M₄] variables [module R M] [module R M₁] [module R₂ M₂] [module R₃ M₃] [module R₄ M₄] variables {σ₁₂ : R →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₃₄ : R₃ →+* R₄} variables {σ₁₃ : R →+* R₃} {σ₂₄ : R₂ →+* R₄} {σ₁₄ : R →+* R₄} variables [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] [ring_hom_comp_triple σ₂₃ σ₃₄ σ₂₄] variables [ring_hom_comp_triple σ₁₃ σ₃₄ σ₁₄] [ring_hom_comp_triple σ₁₂ σ₂₄ σ₁₄] variables (f : M →ₛₗ[σ₁₂] M₂) (g : M₂ →ₛₗ[σ₂₃] M₃) include R R₂ @[simp] lemma map_sum {ι : Type*} {t : finset ι} {g : ι → M} : f (∑ i in t, g i) = (∑ i in t, f (g i)) := f.to_add_monoid_hom.map_sum _ _ theorem comp_assoc (h : M₃ →ₛₗ[σ₃₄] M₄) : ((h.comp g : M₂ →ₛₗ[σ₂₄] M₄).comp f : M →ₛₗ[σ₁₄] M₄) = h.comp (g.comp f : M →ₛₗ[σ₁₃] M₃) := rfl omit R R₂ /-- The restriction of a linear map `f : M → M₂` to a submodule `p ⊆ M` gives a linear map `p → M₂`. -/ def dom_restrict (f : M →ₛₗ[σ₁₂] M₂) (p : submodule R M) : p →ₛₗ[σ₁₂] M₂ := f.comp p.subtype @[simp] lemma dom_restrict_apply (f : M →ₛₗ[σ₁₂] M₂) (p : submodule R M) (x : p) : f.dom_restrict p x = f x := rfl /-- A linear map `f : M₂ → M` whose values lie in a submodule `p ⊆ M` can be restricted to a linear map M₂ → p. -/ def cod_restrict (p : submodule R₂ M₂) (f : M →ₛₗ[σ₁₂] M₂) (h : ∀c, f c ∈ p) : M →ₛₗ[σ₁₂] p := by refine {to_fun := λc, ⟨f c, h c⟩, ..}; intros; apply set_coe.ext; simp @[simp] theorem cod_restrict_apply (p : submodule R₂ M₂) (f : M →ₛₗ[σ₁₂] M₂) {h} (x : M) : (cod_restrict p f h x : M₂) = f x := rfl @[simp] lemma comp_cod_restrict (p : submodule R₃ M₃) (h : ∀b, g b ∈ p) : ((cod_restrict p g h).comp f : M →ₛₗ[σ₁₃] p) = cod_restrict p (g.comp f) (assume b, h _) := ext $ assume b, rfl @[simp] lemma subtype_comp_cod_restrict (p : submodule R₂ M₂) (h : ∀b, f b ∈ p) : p.subtype.comp (cod_restrict p f h) = f := ext $ assume b, rfl /-- Restrict domain and codomain of a linear map. -/ def restrict (f : M →ₗ[R] M₁) {p : submodule R M} {q : submodule R M₁} (hf : ∀ x ∈ p, f x ∈ q) : p →ₗ[R] q := (f.dom_restrict p).cod_restrict q $ set_like.forall.2 hf @[simp] lemma restrict_coe_apply (f : M →ₗ[R] M₁) {p : submodule R M} {q : submodule R M₁} (hf : ∀ x ∈ p, f x ∈ q) (x : p) : ↑(f.restrict hf x) = f x := rfl lemma restrict_apply {f : M →ₗ[R] M₁} {p : submodule R M} {q : submodule R M₁} (hf : ∀ x ∈ p, f x ∈ q) (x : p) : f.restrict hf x = ⟨f x, hf x.1 x.2⟩ := rfl lemma subtype_comp_restrict {f : M →ₗ[R] M₁} {p : submodule R M} {q : submodule R M₁} (hf : ∀ x ∈ p, f x ∈ q) : q.subtype.comp (f.restrict hf) = f.dom_restrict p := rfl lemma restrict_eq_cod_restrict_dom_restrict {f : M →ₗ[R] M₁} {p : submodule R M} {q : submodule R M₁} (hf : ∀ x ∈ p, f x ∈ q) : f.restrict hf = (f.dom_restrict p).cod_restrict q (λ x, hf x.1 x.2) := rfl lemma restrict_eq_dom_restrict_cod_restrict {f : M →ₗ[R] M₁} {p : submodule R M} {q : submodule R M₁} (hf : ∀ x, f x ∈ q) : f.restrict (λ x _, hf x) = (f.cod_restrict q hf).dom_restrict p := rfl instance unique_of_left [subsingleton M] : unique (M →ₛₗ[σ₁₂] M₂) := { uniq := λ f, ext $ λ x, by rw [subsingleton.elim x 0, map_zero, map_zero], .. linear_map.inhabited } instance unique_of_right [subsingleton M₂] : unique (M →ₛₗ[σ₁₂] M₂) := coe_injective.unique /-- Evaluation of a `σ₁₂`-linear map at a fixed `a`, as an `add_monoid_hom`. -/ def eval_add_monoid_hom (a : M) : (M →ₛₗ[σ₁₂] M₂) →+ M₂ := { to_fun := λ f, f a, map_add' := λ f g, linear_map.add_apply f g a, map_zero' := rfl } /-- `linear_map.to_add_monoid_hom` promoted to an `add_monoid_hom` -/ def to_add_monoid_hom' : (M →ₛₗ[σ₁₂] M₂) →+ (M →+ M₂) := { to_fun := to_add_monoid_hom, map_zero' := by ext; refl, map_add' := by intros; ext; refl } lemma sum_apply (t : finset ι) (f : ι → M →ₛₗ[σ₁₂] M₂) (b : M) : (∑ d in t, f d) b = ∑ d in t, f d b := add_monoid_hom.map_sum ((add_monoid_hom.eval b).comp to_add_monoid_hom') f _ section smul_right variables [semiring S] [module R S] [module S M] [is_scalar_tower R S M] /-- When `f` is an `R`-linear map taking values in `S`, then `λb, f b • x` is an `R`-linear map. -/ def smul_right (f : M₁ →ₗ[R] S) (x : M) : M₁ →ₗ[R] M := { to_fun := λb, f b • x, map_add' := λ x y, by rw [f.map_add, add_smul], map_smul' := λ b y, by dsimp; rw [map_smul, smul_assoc] } @[simp] theorem coe_smul_right (f : M₁ →ₗ[R] S) (x : M) : (smul_right f x : M₁ → M) = λ c, f c • x := rfl theorem smul_right_apply (f : M₁ →ₗ[R] S) (x : M) (c : M₁) : smul_right f x c = f c • x := rfl end smul_right instance [nontrivial M] : nontrivial (module.End R M) := begin obtain ⟨m, ne⟩ := (nontrivial_iff_exists_ne (0 : M)).mp infer_instance, exact nontrivial_of_ne 1 0 (λ p, ne (linear_map.congr_fun p m)), end @[simp, norm_cast] lemma coe_fn_sum {ι : Type*} (t : finset ι) (f : ι → M →ₛₗ[σ₁₂] M₂) : ⇑(∑ i in t, f i) = ∑ i in t, (f i : M → M₂) := add_monoid_hom.map_sum ⟨@to_fun R R₂ _ _ σ₁₂ M M₂ _ _ _ _, rfl, λ x y, rfl⟩ _ _ @[simp] lemma pow_apply (f : M →ₗ[R] M) (n : ℕ) (m : M) : (f^n) m = (f^[n] m) := begin induction n with n ih, { refl, }, { simp only [function.comp_app, function.iterate_succ, linear_map.mul_apply, pow_succ, ih], exact (function.commute.iterate_self _ _ m).symm, }, end lemma pow_map_zero_of_le {f : module.End R M} {m : M} {k l : ℕ} (hk : k ≤ l) (hm : (f^k) m = 0) : (f^l) m = 0 := by rw [← tsub_add_cancel_of_le hk, pow_add, mul_apply, hm, map_zero] lemma commute_pow_left_of_commute {f : M →ₛₗ[σ₁₂] M₂} {g : module.End R M} {g₂ : module.End R₂ M₂} (h : g₂.comp f = f.comp g) (k : ℕ) : (g₂^k).comp f = f.comp (g^k) := begin induction k with k ih, { simpa only [pow_zero], }, { rw [pow_succ, pow_succ, linear_map.mul_eq_comp, linear_map.comp_assoc, ih, ← linear_map.comp_assoc, h, linear_map.comp_assoc, linear_map.mul_eq_comp], }, end lemma submodule_pow_eq_zero_of_pow_eq_zero {N : submodule R M} {g : module.End R N} {G : module.End R M} (h : G.comp N.subtype = N.subtype.comp g) {k : ℕ} (hG : G^k = 0) : g^k = 0 := begin ext m, have hg : N.subtype.comp (g^k) m = 0, { rw [← commute_pow_left_of_commute h, hG, zero_comp, zero_apply], }, simp only [submodule.subtype_apply, comp_app, submodule.coe_eq_zero, coe_comp] at hg, rw [hg, linear_map.zero_apply], end lemma coe_pow (f : M →ₗ[R] M) (n : ℕ) : ⇑(f^n) = (f^[n]) := by { ext m, apply pow_apply, } @[simp] lemma id_pow (n : ℕ) : (id : M →ₗ[R] M)^n = id := one_pow n section variables {f' : M →ₗ[R] M} lemma iterate_succ (n : ℕ) : (f' ^ (n + 1)) = comp (f' ^ n) f' := by rw [pow_succ', mul_eq_comp] lemma iterate_surjective (h : surjective f') : ∀ n : ℕ, surjective ⇑(f' ^ n) | 0 := surjective_id | (n + 1) := by { rw [iterate_succ], exact surjective.comp (iterate_surjective n) h, } lemma iterate_injective (h : injective f') : ∀ n : ℕ, injective ⇑(f' ^ n) | 0 := injective_id | (n + 1) := by { rw [iterate_succ], exact injective.comp (iterate_injective n) h, } lemma iterate_bijective (h : bijective f') : ∀ n : ℕ, bijective ⇑(f' ^ n) | 0 := bijective_id | (n + 1) := by { rw [iterate_succ], exact bijective.comp (iterate_bijective n) h, } lemma injective_of_iterate_injective {n : ℕ} (hn : n ≠ 0) (h : injective ⇑(f' ^ n)) : injective f' := begin rw [← nat.succ_pred_eq_of_pos (pos_iff_ne_zero.mpr hn), iterate_succ, coe_comp] at h, exact injective.of_comp h, end lemma surjective_of_iterate_surjective {n : ℕ} (hn : n ≠ 0) (h : surjective ⇑(f' ^ n)) : surjective f' := begin rw [← nat.succ_pred_eq_of_pos (pos_iff_ne_zero.mpr hn), nat.succ_eq_add_one, add_comm, pow_add] at h, exact surjective.of_comp h, end lemma pow_apply_mem_of_forall_mem {p : submodule R M} (n : ℕ) (h : ∀ x ∈ p, f' x ∈ p) (x : M) (hx : x ∈ p) : (f'^n) x ∈ p := begin induction n with n ih generalizing x, { simpa, }, simpa only [iterate_succ, coe_comp, function.comp_app, restrict_apply] using ih _ (h _ hx), end lemma pow_restrict {p : submodule R M} (n : ℕ) (h : ∀ x ∈ p, f' x ∈ p) (h' := pow_apply_mem_of_forall_mem n h) : (f'.restrict h)^n = (f'^n).restrict h' := begin induction n with n ih; ext, { simp [restrict_apply], }, { simp [restrict_apply, linear_map.iterate_succ, -linear_map.pow_apply, ih], }, end end /-- A linear map `f` applied to `x : ι → R` can be computed using the image under `f` of elements of the canonical basis. -/ lemma pi_apply_eq_sum_univ [fintype ι] [decidable_eq ι] (f : (ι → R) →ₗ[R] M) (x : ι → R) : f x = ∑ i, x i • (f (λj, if i = j then 1 else 0)) := begin conv_lhs { rw [pi_eq_sum_univ x, f.map_sum] }, apply finset.sum_congr rfl (λl hl, _), rw map_smul end end add_comm_monoid section module variables [semiring R] [semiring S] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [module R M] [module R M₂] [module R M₃] [module S M₂] [module S M₃] [smul_comm_class R S M₂] [smul_comm_class R S M₃] (f : M →ₗ[R] M₂) variable (S) /-- Applying a linear map at `v : M`, seen as `S`-linear map from `M →ₗ[R] M₂` to `M₂`. See `linear_map.applyₗ` for a version where `S = R`. -/ @[simps] def applyₗ' : M →+ (M →ₗ[R] M₂) →ₗ[S] M₂ := { to_fun := λ v, { to_fun := λ f, f v, map_add' := λ f g, f.add_apply g v, map_smul' := λ x f, f.smul_apply x v }, map_zero' := linear_map.ext $ λ f, f.map_zero, map_add' := λ x y, linear_map.ext $ λ f, f.map_add _ _ } section variables (R M) /-- The equivalence between R-linear maps from `R` to `M`, and points of `M` itself. This says that the forgetful functor from `R`-modules to types is representable, by `R`. This as an `S`-linear equivalence, under the assumption that `S` acts on `M` commuting with `R`. When `R` is commutative, we can take this to be the usual action with `S = R`. Otherwise, `S = ℕ` shows that the equivalence is additive. See note [bundled maps over different rings]. -/ @[simps] def ring_lmap_equiv_self [module S M] [smul_comm_class R S M] : (R →ₗ[R] M) ≃ₗ[S] M := { to_fun := λ f, f 1, inv_fun := smul_right (1 : R →ₗ[R] R), left_inv := λ f, by { ext, simp }, right_inv := λ x, by simp, .. applyₗ' S (1 : R) } end end module section comm_semiring variables [comm_semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [module R M] [module R M₂] [module R M₃] variables (f g : M →ₗ[R] M₂) include R /-- Composition by `f : M₂ → M₃` is a linear map from the space of linear maps `M → M₂` to the space of linear maps `M₂ → M₃`. -/ def comp_right (f : M₂ →ₗ[R] M₃) : (M →ₗ[R] M₂) →ₗ[R] (M →ₗ[R] M₃) := { to_fun := f.comp, map_add' := λ _ _, linear_map.ext $ λ _, map_add f _ _, map_smul' := λ _ _, linear_map.ext $ λ _, map_smul f _ _ } @[simp] lemma comp_right_apply (f : M₂ →ₗ[R] M₃) (g : M →ₗ[R] M₂) : comp_right f g = f.comp g := rfl /-- Applying a linear map at `v : M`, seen as a linear map from `M →ₗ[R] M₂` to `M₂`. See also `linear_map.applyₗ'` for a version that works with two different semirings. This is the `linear_map` version of `add_monoid_hom.eval`. -/ @[simps] def applyₗ : M →ₗ[R] (M →ₗ[R] M₂) →ₗ[R] M₂ := { to_fun := λ v, { to_fun := λ f, f v, ..applyₗ' R v }, map_smul' := λ x y, linear_map.ext $ λ f, map_smul f _ _, ..applyₗ' R } /-- Alternative version of `dom_restrict` as a linear map. -/ def dom_restrict' (p : submodule R M) : (M →ₗ[R] M₂) →ₗ[R] (p →ₗ[R] M₂) := { to_fun := λ φ, φ.dom_restrict p, map_add' := by simp [linear_map.ext_iff], map_smul' := by simp [linear_map.ext_iff] } @[simp] lemma dom_restrict'_apply (f : M →ₗ[R] M₂) (p : submodule R M) (x : p) : dom_restrict' p f x = f x := rfl /-- The family of linear maps `M₂ → M` parameterised by `f ∈ M₂ → R`, `x ∈ M`, is linear in `f`, `x`. -/ def smul_rightₗ : (M₂ →ₗ[R] R) →ₗ[R] M →ₗ[R] M₂ →ₗ[R] M := { to_fun := λ f, { to_fun := linear_map.smul_right f, map_add' := λ m m', by { ext, apply smul_add, }, map_smul' := λ c m, by { ext, apply smul_comm, } }, map_add' := λ f f', by { ext, apply add_smul, }, map_smul' := λ c f, by { ext, apply mul_smul, } } @[simp] lemma smul_rightₗ_apply (f : M₂ →ₗ[R] R) (x : M) (c : M₂) : (smul_rightₗ : (M₂ →ₗ[R] R) →ₗ[R] M →ₗ[R] M₂ →ₗ[R] M) f x c = (f c) • x := rfl end comm_semiring end linear_map /-- The `R`-linear equivalence between additive morphisms `A →+ B` and `ℕ`-linear morphisms `A →ₗ[ℕ] B`. -/ @[simps] def add_monoid_hom_lequiv_nat {A B : Type*} (R : Type*) [semiring R] [add_comm_monoid A] [add_comm_monoid B] [module R B] : (A →+ B) ≃ₗ[R] (A →ₗ[ℕ] B) := { to_fun := add_monoid_hom.to_nat_linear_map, inv_fun := linear_map.to_add_monoid_hom, map_add' := by { intros, ext, refl }, map_smul' := by { intros, ext, refl }, left_inv := by { intros f, ext, refl }, right_inv := by { intros f, ext, refl } } /-- The `R`-linear equivalence between additive morphisms `A →+ B` and `ℤ`-linear morphisms `A →ₗ[ℤ] B`. -/ @[simps] def add_monoid_hom_lequiv_int {A B : Type*} (R : Type*) [semiring R] [add_comm_group A] [add_comm_group B] [module R B] : (A →+ B) ≃ₗ[R] (A →ₗ[ℤ] B) := { to_fun := add_monoid_hom.to_int_linear_map, inv_fun := linear_map.to_add_monoid_hom, map_add' := by { intros, ext, refl }, map_smul' := by { intros, ext, refl }, left_inv := by { intros f, ext, refl }, right_inv := by { intros f, ext, refl } } /-! ### Properties of submodules -/ namespace submodule section add_comm_monoid variables [semiring R] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M'] variables [module R M] [module R M'] [module R₂ M₂] [module R₃ M₃] variables {σ₁₂ : R →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R →+* R₃} variables {σ₂₁ : R₂ →+* R} variables [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] variables [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] variables (p p' : submodule R M) (q q' : submodule R₂ M₂) variables (q₁ q₁' : submodule R M') variables {r : R} {x y : M} open set variables {p p'} /-- If two submodules `p` and `p'` satisfy `p ⊆ p'`, then `of_le p p'` is the linear map version of this inclusion. -/ def of_le (h : p ≤ p') : p →ₗ[R] p' := p.subtype.cod_restrict p' $ λ ⟨x, hx⟩, h hx @[simp] theorem coe_of_le (h : p ≤ p') (x : p) : (of_le h x : M) = x := rfl theorem of_le_apply (h : p ≤ p') (x : p) : of_le h x = ⟨x, h x.2⟩ := rfl theorem of_le_injective (h : p ≤ p') : function.injective (of_le h) := λ x y h, subtype.val_injective (subtype.mk.inj h) variables (p p') lemma subtype_comp_of_le (p q : submodule R M) (h : p ≤ q) : q.subtype.comp (of_le h) = p.subtype := by { ext ⟨b, hb⟩, refl } variables (R) @[simp] lemma subsingleton_iff : subsingleton (submodule R M) ↔ subsingleton M := have h : subsingleton (submodule R M) ↔ subsingleton (add_submonoid M), { rw [←subsingleton_iff_bot_eq_top, ←subsingleton_iff_bot_eq_top], convert to_add_submonoid_eq.symm; refl, }, h.trans add_submonoid.subsingleton_iff @[simp] lemma nontrivial_iff : nontrivial (submodule R M) ↔ nontrivial M := not_iff_not.mp ( (not_nontrivial_iff_subsingleton.trans $ subsingleton_iff R).trans not_nontrivial_iff_subsingleton.symm) variables {R} instance [subsingleton M] : unique (submodule R M) := ⟨⟨⊥⟩, λ a, @subsingleton.elim _ ((subsingleton_iff R).mpr ‹_›) a _⟩ instance unique' [subsingleton R] : unique (submodule R M) := by haveI := module.subsingleton R M; apply_instance instance [nontrivial M] : nontrivial (submodule R M) := (nontrivial_iff R).mpr ‹_› theorem mem_right_iff_eq_zero_of_disjoint {p p' : submodule R M} (h : disjoint p p') {x : p} : (x:M) ∈ p' ↔ x = 0 := ⟨λ hx, coe_eq_zero.1 $ disjoint_def.1 h x x.2 hx, λ h, h.symm ▸ p'.zero_mem⟩ theorem mem_left_iff_eq_zero_of_disjoint {p p' : submodule R M} (h : disjoint p p') {x : p'} : (x:M) ∈ p ↔ x = 0 := ⟨λ hx, coe_eq_zero.1 $ disjoint_def.1 h x hx x.2, λ h, h.symm ▸ p.zero_mem⟩ section variables [ring_hom_surjective σ₁₂] {F : Type*} [sc : semilinear_map_class F σ₁₂ M M₂] include sc /-- The pushforward of a submodule `p ⊆ M` by `f : M → M₂` -/ def map (f : F) (p : submodule R M) : submodule R₂ M₂ := { carrier := f '' p, smul_mem' := begin rintro c x ⟨y, hy, rfl⟩, obtain ⟨a, rfl⟩ := σ₁₂.is_surjective c, exact ⟨_, p.smul_mem a hy, map_smulₛₗ f _ _⟩, end, .. p.to_add_submonoid.map f } @[simp] lemma map_coe (f : F) (p : submodule R M) : (map f p : set M₂) = f '' p := rfl omit sc lemma map_to_add_submonoid (f : M →ₛₗ[σ₁₂] M₂) (p : submodule R M) : (p.map f).to_add_submonoid = p.to_add_submonoid.map (f : M →+ M₂) := set_like.coe_injective rfl lemma map_to_add_submonoid' (f : M →ₛₗ[σ₁₂] M₂) (p : submodule R M) : (p.map f).to_add_submonoid = p.to_add_submonoid.map f := set_like.coe_injective rfl include sc @[simp] lemma mem_map {f : F} {p : submodule R M} {x : M₂} : x ∈ map f p ↔ ∃ y, y ∈ p ∧ f y = x := iff.rfl theorem mem_map_of_mem {f : F} {p : submodule R M} {r} (h : r ∈ p) : f r ∈ map f p := set.mem_image_of_mem _ h lemma apply_coe_mem_map (f : F) {p : submodule R M} (r : p) : f r ∈ map f p := mem_map_of_mem r.prop omit sc @[simp] lemma map_id : map (linear_map.id : M →ₗ[R] M) p = p := submodule.ext $ λ a, by simp lemma map_comp [ring_hom_surjective σ₂₃] [ring_hom_surjective σ₁₃] (f : M →ₛₗ[σ₁₂] M₂) (g : M₂ →ₛₗ[σ₂₃] M₃) (p : submodule R M) : map (g.comp f : M →ₛₗ[σ₁₃] M₃) p = map g (map f p) := set_like.coe_injective $ by simp only [← image_comp, map_coe, linear_map.coe_comp, comp_app] include sc lemma map_mono {f : F} {p p' : submodule R M} : p ≤ p' → map f p ≤ map f p' := image_subset _ omit sc @[simp] lemma map_zero : map (0 : M →ₛₗ[σ₁₂] M₂) p = ⊥ := have ∃ (x : M), x ∈ p := ⟨0, p.zero_mem⟩, ext $ by simp [this, eq_comm] lemma map_add_le (f g : M →ₛₗ[σ₁₂] M₂) : map (f + g) p ≤ map f p ⊔ map g p := begin rintros x ⟨m, hm, rfl⟩, exact add_mem_sup (mem_map_of_mem hm) (mem_map_of_mem hm), end lemma range_map_nonempty (N : submodule R M) : (set.range (λ ϕ, submodule.map ϕ N : (M →ₛₗ[σ₁₂] M₂) → submodule R₂ M₂)).nonempty := ⟨_, set.mem_range.mpr ⟨0, rfl⟩⟩ end variables {F : Type*} [sc : semilinear_map_class F σ₁₂ M M₂] include σ₂₁ sc /-- The pushforward of a submodule by an injective linear map is linearly equivalent to the original submodule. See also `linear_equiv.submodule_map` for a computable version when `f` has an explicit inverse. -/ noncomputable def equiv_map_of_injective (f : F) (i : injective f) (p : submodule R M) : p ≃ₛₗ[σ₁₂] p.map f := { map_add' := by { intros, simp only [coe_add, map_add, equiv.to_fun_as_coe, equiv.set.image_apply], refl }, map_smul' := by { intros, simp only [coe_smul_of_tower, map_smulₛₗ, equiv.to_fun_as_coe, equiv.set.image_apply], refl }, ..(equiv.set.image f p i) } @[simp] lemma coe_equiv_map_of_injective_apply (f : F) (i : injective f) (p : submodule R M) (x : p) : (equiv_map_of_injective f i p x : M₂) = f x := rfl omit σ₂₁ /-- The pullback of a submodule `p ⊆ M₂` along `f : M → M₂` -/ def comap (f : F) (p : submodule R₂ M₂) : submodule R M := { carrier := f ⁻¹' p, smul_mem' := λ a x h, by simp [p.smul_mem _ h], .. p.to_add_submonoid.comap f } @[simp] lemma comap_coe (f : F) (p : submodule R₂ M₂) : (comap f p : set M) = f ⁻¹' p := rfl @[simp] lemma mem_comap {f : F} {p : submodule R₂ M₂} : x ∈ comap f p ↔ f x ∈ p := iff.rfl omit sc @[simp] lemma comap_id : comap (linear_map.id : M →ₗ[R] M) p = p := set_like.coe_injective rfl lemma comap_comp (f : M →ₛₗ[σ₁₂] M₂) (g : M₂ →ₛₗ[σ₂₃] M₃) (p : submodule R₃ M₃) : comap (g.comp f : M →ₛₗ[σ₁₃] M₃) p = comap f (comap g p) := rfl include sc lemma comap_mono {f : F} {q q' : submodule R₂ M₂} : q ≤ q' → comap f q ≤ comap f q' := preimage_mono omit sc lemma le_comap_pow_of_le_comap (p : submodule R M) {f : M →ₗ[R] M} (h : p ≤ p.comap f) (k : ℕ) : p ≤ p.comap (f^k) := begin induction k with k ih, { simp [linear_map.one_eq_id], }, { simp [linear_map.iterate_succ, comap_comp, h.trans (comap_mono ih)], }, end section variables [ring_hom_surjective σ₁₂] include sc lemma map_le_iff_le_comap {f : F} {p : submodule R M} {q : submodule R₂ M₂} : map f p ≤ q ↔ p ≤ comap f q := image_subset_iff lemma gc_map_comap (f : F) : galois_connection (map f) (comap f) | p q := map_le_iff_le_comap @[simp] lemma map_bot (f : F) : map f ⊥ = ⊥ := (gc_map_comap f).l_bot @[simp] lemma map_sup (f : F) : map f (p ⊔ p') = map f p ⊔ map f p' := (gc_map_comap f : galois_connection (map f) (comap f)).l_sup @[simp] lemma map_supr {ι : Sort*} (f : F) (p : ι → submodule R M) : map f (⨆i, p i) = (⨆i, map f (p i)) := (gc_map_comap f : galois_connection (map f) (comap f)).l_supr end include sc @[simp] lemma comap_top (f : F) : comap f ⊤ = ⊤ := rfl @[simp] lemma comap_inf (f : F) : comap f (q ⊓ q') = comap f q ⊓ comap f q' := rfl @[simp] lemma comap_infi [ring_hom_surjective σ₁₂] {ι : Sort*} (f : F) (p : ι → submodule R₂ M₂) : comap f (⨅i, p i) = (⨅i, comap f (p i)) := (gc_map_comap f : galois_connection (map f) (comap f)).u_infi omit sc @[simp] lemma comap_zero : comap (0 : M →ₛₗ[σ₁₂] M₂) q = ⊤ := ext $ by simp include sc lemma map_comap_le [ring_hom_surjective σ₁₂] (f : F) (q : submodule R₂ M₂) : map f (comap f q) ≤ q := (gc_map_comap f).l_u_le _ lemma le_comap_map [ring_hom_surjective σ₁₂] (f : F) (p : submodule R M) : p ≤ comap f (map f p) := (gc_map_comap f).le_u_l _ section galois_insertion variables {f : F} (hf : surjective f) variables [ring_hom_surjective σ₁₂] include hf /-- `map f` and `comap f` form a `galois_insertion` when `f` is surjective. -/ def gi_map_comap : galois_insertion (map f) (comap f) := (gc_map_comap f).to_galois_insertion (λ S x hx, begin rcases hf x with ⟨y, rfl⟩, simp only [mem_map, mem_comap], exact ⟨y, hx, rfl⟩ end) lemma map_comap_eq_of_surjective (p : submodule R₂ M₂) : (p.comap f).map f = p := (gi_map_comap hf).l_u_eq _ lemma map_surjective_of_surjective : function.surjective (map f) := (gi_map_comap hf).l_surjective lemma comap_injective_of_surjective : function.injective (comap f) := (gi_map_comap hf).u_injective lemma map_sup_comap_of_surjective (p q : submodule R₂ M₂) : (p.comap f ⊔ q.comap f).map f = p ⊔ q := (gi_map_comap hf).l_sup_u _ _ lemma map_supr_comap_of_sujective {ι : Sort*} (S : ι → submodule R₂ M₂) : (⨆ i, (S i).comap f).map f = supr S := (gi_map_comap hf).l_supr_u _ lemma map_inf_comap_of_surjective (p q : submodule R₂ M₂) : (p.comap f ⊓ q.comap f).map f = p ⊓ q := (gi_map_comap hf).l_inf_u _ _ lemma map_infi_comap_of_surjective {ι : Sort*} (S : ι → submodule R₂ M₂) : (⨅ i, (S i).comap f).map f = infi S := (gi_map_comap hf).l_infi_u _ lemma comap_le_comap_iff_of_surjective (p q : submodule R₂ M₂) : p.comap f ≤ q.comap f ↔ p ≤ q := (gi_map_comap hf).u_le_u_iff lemma comap_strict_mono_of_surjective : strict_mono (comap f) := (gi_map_comap hf).strict_mono_u end galois_insertion section galois_coinsertion variables [ring_hom_surjective σ₁₂] {f : F} (hf : injective f) include hf /-- `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. -/ def gci_map_comap : galois_coinsertion (map f) (comap f) := (gc_map_comap f).to_galois_coinsertion (λ S x, by simp [mem_comap, mem_map, hf.eq_iff]) lemma comap_map_eq_of_injective (p : submodule R M) : (p.map f).comap f = p := (gci_map_comap hf).u_l_eq _ lemma comap_surjective_of_injective : function.surjective (comap f) := (gci_map_comap hf).u_surjective lemma map_injective_of_injective : function.injective (map f) := (gci_map_comap hf).l_injective lemma comap_inf_map_of_injective (p q : submodule R M) : (p.map f ⊓ q.map f).comap f = p ⊓ q := (gci_map_comap hf).u_inf_l _ _ lemma comap_infi_map_of_injective {ι : Sort*} (S : ι → submodule R M) : (⨅ i, (S i).map f).comap f = infi S := (gci_map_comap hf).u_infi_l _ lemma comap_sup_map_of_injective (p q : submodule R M) : (p.map f ⊔ q.map f).comap f = p ⊔ q := (gci_map_comap hf).u_sup_l _ _ lemma comap_supr_map_of_injective {ι : Sort*} (S : ι → submodule R M) : (⨆ i, (S i).map f).comap f = supr S := (gci_map_comap hf).u_supr_l _ lemma map_le_map_iff_of_injective (p q : submodule R M) : p.map f ≤ q.map f ↔ p ≤ q := (gci_map_comap hf).l_le_l_iff lemma map_strict_mono_of_injective : strict_mono (map f) := (gci_map_comap hf).strict_mono_l end galois_coinsertion section order_iso omit sc include σ₁₂ σ₂₁ variables [semilinear_equiv_class F σ₁₂ M M₂] /-- A linear isomorphism induces an order isomorphism of submodules. -/ @[simps symm_apply apply] def order_iso_map_comap (f : F) : submodule R M ≃o submodule R₂ M₂ := { to_fun := map f, inv_fun := comap f, left_inv := comap_map_eq_of_injective $ equiv_like.injective f, right_inv := map_comap_eq_of_surjective $ equiv_like.surjective f, map_rel_iff' := map_le_map_iff_of_injective $ equiv_like.injective f } end order_iso --TODO(Mario): is there a way to prove this from order properties? lemma map_inf_eq_map_inf_comap [ring_hom_surjective σ₁₂] {f : F} {p : submodule R M} {p' : submodule R₂ M₂} : map f p ⊓ p' = map f (p ⊓ comap f p') := le_antisymm (by rintro _ ⟨⟨x, h₁, rfl⟩, h₂⟩; exact ⟨_, ⟨h₁, h₂⟩, rfl⟩) (le_inf (map_mono inf_le_left) (map_le_iff_le_comap.2 inf_le_right)) omit sc lemma map_comap_subtype : map p.subtype (comap p.subtype p') = p ⊓ p' := ext $ λ x, ⟨by rintro ⟨⟨_, h₁⟩, h₂, rfl⟩; exact ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨⟨_, h₁⟩, h₂, rfl⟩⟩ lemma eq_zero_of_bot_submodule : ∀(b : (⊥ : submodule R M)), b = 0 | ⟨b', hb⟩ := subtype.eq $ show b' = 0, from (mem_bot R).1 hb /-- The infimum of a family of invariant submodule of an endomorphism is also an invariant submodule. -/ lemma _root_.linear_map.infi_invariant {σ : R →+* R} [ring_hom_surjective σ] {ι : Sort*} (f : M →ₛₗ[σ] M) {p : ι → submodule R M} (hf : ∀ i, ∀ v ∈ (p i), f v ∈ p i) : ∀ v ∈ infi p, f v ∈ infi p := begin have : ∀ i, (p i).map f ≤ p i, { rintros i - ⟨v, hv, rfl⟩, exact hf i v hv }, suffices : (infi p).map f ≤ infi p, { exact λ v hv, this ⟨v, hv, rfl⟩, }, exact le_infi (λ i, (submodule.map_mono (infi_le p i)).trans (this i)), end end add_comm_monoid section add_comm_group variables [ring R] [add_comm_group M] [module R M] (p : submodule R M) variables [add_comm_group M₂] [module R M₂] -- See `neg_coe_set` lemma neg_coe : -(p : set M) = p := set.ext $ λ x, p.neg_mem_iff @[simp] protected lemma map_neg (f : M →ₗ[R] M₂) : map (-f) p = map f p := ext $ λ y, ⟨λ ⟨x, hx, hy⟩, hy ▸ ⟨-x, show -x ∈ p, from neg_mem hx, map_neg f x⟩, λ ⟨x, hx, hy⟩, hy ▸ ⟨-x, show -x ∈ p, from neg_mem hx, (map_neg (-f) _).trans (neg_neg (f x))⟩⟩ end add_comm_group end submodule namespace submodule variables [field K] variables [add_comm_group V] [module K V] variables [add_comm_group V₂] [module K V₂] lemma comap_smul (f : V →ₗ[K] V₂) (p : submodule K V₂) (a : K) (h : a ≠ 0) : p.comap (a • f) = p.comap f := by ext b; simp only [submodule.mem_comap, p.smul_mem_iff h, linear_map.smul_apply] lemma map_smul (f : V →ₗ[K] V₂) (p : submodule K V) (a : K) (h : a ≠ 0) : p.map (a • f) = p.map f := le_antisymm begin rw [map_le_iff_le_comap, comap_smul f _ a h, ← map_le_iff_le_comap], exact le_rfl end begin rw [map_le_iff_le_comap, ← comap_smul f _ a h, ← map_le_iff_le_comap], exact le_rfl end lemma comap_smul' (f : V →ₗ[K] V₂) (p : submodule K V₂) (a : K) : p.comap (a • f) = (⨅ h : a ≠ 0, p.comap f) := by classical; by_cases a = 0; simp [h, comap_smul] lemma map_smul' (f : V →ₗ[K] V₂) (p : submodule K V) (a : K) : p.map (a • f) = (⨆ h : a ≠ 0, p.map f) := by classical; by_cases a = 0; simp [h, map_smul] end submodule /-! ### Properties of linear maps -/ namespace linear_map section add_comm_monoid variables [semiring R] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables {σ₁₂ : R →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R →+* R₃} variables [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] variables [module R M] [module R₂ M₂] [module R₃ M₃] include R open submodule section finsupp variables {γ : Type*} [has_zero γ] @[simp] lemma map_finsupp_sum (f : M →ₛₗ[σ₁₂] M₂) {t : ι →₀ γ} {g : ι → γ → M} : f (t.sum g) = t.sum (λ i d, f (g i d)) := f.map_sum lemma coe_finsupp_sum (t : ι →₀ γ) (g : ι → γ → M →ₛₗ[σ₁₂] M₂) : ⇑(t.sum g) = t.sum (λ i d, g i d) := coe_fn_sum _ _ @[simp] lemma finsupp_sum_apply (t : ι →₀ γ) (g : ι → γ → M →ₛₗ[σ₁₂] M₂) (b : M) : (t.sum g) b = t.sum (λ i d, g i d b) := sum_apply _ _ _ end finsupp section dfinsupp open dfinsupp variables {γ : ι → Type*} [decidable_eq ι] section sum variables [Π i, has_zero (γ i)] [Π i (x : γ i), decidable (x ≠ 0)] @[simp] lemma map_dfinsupp_sum (f : M →ₛₗ[σ₁₂] M₂) {t : Π₀ i, γ i} {g : Π i, γ i → M} : f (t.sum g) = t.sum (λ i d, f (g i d)) := f.map_sum lemma coe_dfinsupp_sum (t : Π₀ i, γ i) (g : Π i, γ i → M →ₛₗ[σ₁₂] M₂) : ⇑(t.sum g) = t.sum (λ i d, g i d) := coe_fn_sum _ _ @[simp] lemma dfinsupp_sum_apply (t : Π₀ i, γ i) (g : Π i, γ i → M →ₛₗ[σ₁₂] M₂) (b : M) : (t.sum g) b = t.sum (λ i d, g i d b) := sum_apply _ _ _ end sum section sum_add_hom variables [Π i, add_zero_class (γ i)] @[simp] lemma map_dfinsupp_sum_add_hom (f : M →ₛₗ[σ₁₂] M₂) {t : Π₀ i, γ i} {g : Π i, γ i →+ M} : f (sum_add_hom g t) = sum_add_hom (λ i, f.to_add_monoid_hom.comp (g i)) t := f.to_add_monoid_hom.map_dfinsupp_sum_add_hom _ _ end sum_add_hom end dfinsupp variables {σ₂₁ : R₂ →+* R} {τ₁₂ : R →+* R₂} {τ₂₃ : R₂ →+* R₃} {τ₁₃ : R →+* R₃} variables [ring_hom_comp_triple τ₁₂ τ₂₃ τ₁₃] theorem map_cod_restrict [ring_hom_surjective σ₂₁] (p : submodule R M) (f : M₂ →ₛₗ[σ₂₁] M) (h p') : submodule.map (cod_restrict p f h) p' = comap p.subtype (p'.map f) := submodule.ext $ λ ⟨x, hx⟩, by simp [subtype.ext_iff_val] theorem comap_cod_restrict (p : submodule R M) (f : M₂ →ₛₗ[σ₂₁] M) (hf p') : submodule.comap (cod_restrict p f hf) p' = submodule.comap f (map p.subtype p') := submodule.ext $ λ x, ⟨λ h, ⟨⟨_, hf x⟩, h, rfl⟩, by rintro ⟨⟨_, _⟩, h, ⟨⟩⟩; exact h⟩ section variables {F : Type*} [sc : semilinear_map_class F τ₁₂ M M₂] include sc /-- The range of a linear map `f : M → M₂` is a submodule of `M₂`. See Note [range copy pattern]. -/ def range [ring_hom_surjective τ₁₂] (f : F) : submodule R₂ M₂ := (map f ⊤).copy (set.range f) set.image_univ.symm theorem range_coe [ring_hom_surjective τ₁₂] (f : F) : (range f : set M₂) = set.range f := rfl omit sc lemma range_to_add_submonoid [ring_hom_surjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : f.range.to_add_submonoid = f.to_add_monoid_hom.mrange := rfl include sc @[simp] theorem mem_range [ring_hom_surjective τ₁₂] {f : F} {x} : x ∈ range f ↔ ∃ y, f y = x := iff.rfl lemma range_eq_map [ring_hom_surjective τ₁₂] (f : F) : range f = map f ⊤ := by { ext, simp } theorem mem_range_self [ring_hom_surjective τ₁₂] (f : F) (x : M) : f x ∈ range f := ⟨x, rfl⟩ omit sc @[simp] theorem range_id : range (linear_map.id : M →ₗ[R] M) = ⊤ := set_like.coe_injective set.range_id theorem range_comp [ring_hom_surjective τ₁₂] [ring_hom_surjective τ₂₃] [ring_hom_surjective τ₁₃] (f : M →ₛₗ[τ₁₂] M₂) (g : M₂ →ₛₗ[τ₂₃] M₃) : range (g.comp f : M →ₛₗ[τ₁₃] M₃) = map g (range f) := set_like.coe_injective (set.range_comp g f) theorem range_comp_le_range [ring_hom_surjective τ₂₃] [ring_hom_surjective τ₁₃] (f : M →ₛₗ[τ₁₂] M₂) (g : M₂ →ₛₗ[τ₂₃] M₃) : range (g.comp f : M →ₛₗ[τ₁₃] M₃) ≤ range g := set_like.coe_mono (set.range_comp_subset_range f g) include sc theorem range_eq_top [ring_hom_surjective τ₁₂] {f : F} : range f = ⊤ ↔ surjective f := by rw [set_like.ext'_iff, range_coe, top_coe, set.range_iff_surjective] lemma range_le_iff_comap [ring_hom_surjective τ₁₂] {f : F} {p : submodule R₂ M₂} : range f ≤ p ↔ comap f p = ⊤ := by rw [range_eq_map, map_le_iff_le_comap, eq_top_iff] lemma map_le_range [ring_hom_surjective τ₁₂] {f : F} {p : submodule R M} : map f p ≤ range f := set_like.coe_mono (set.image_subset_range f p) omit sc @[simp] lemma range_neg {R : Type*} {R₂ : Type*} {M : Type*} {M₂ : Type*} [semiring R] [ring R₂] [add_comm_monoid M] [add_comm_group M₂] [module R M] [module R₂ M₂] {τ₁₂ : R →+* R₂} [ring_hom_surjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : (-f).range = f.range := begin change ((-linear_map.id : M₂ →ₗ[R₂] M₂).comp f).range = _, rw [range_comp, submodule.map_neg, submodule.map_id], end /-- A linear map version of `add_monoid_hom.eq_locus` -/ def eq_locus (f g : M →ₛₗ[τ₁₂] M₂) : submodule R M := { carrier := {x | f x = g x}, smul_mem' := λ r x (hx : _ = _), show _ = _, by simpa only [linear_map.map_smulₛₗ] using congr_arg ((•) (τ₁₂ r)) hx, .. f.to_add_monoid_hom.eq_mlocus g.to_add_monoid_hom } @[simp] lemma mem_eq_locus {x : M} {f g : M →ₛₗ[τ₁₂] M₂} : x ∈ f.eq_locus g ↔ f x = g x := iff.rfl lemma eq_locus_to_add_submonoid (f g : M →ₛₗ[τ₁₂] M₂) : (f.eq_locus g).to_add_submonoid = (f : M →+ M₂).eq_mlocus g := rfl @[simp] lemma eq_locus_same (f : M →ₛₗ[τ₁₂] M₂) : f.eq_locus f = ⊤ := set_like.ext $ λ _, eq_self_iff_true _ end /-- The decreasing sequence of submodules consisting of the ranges of the iterates of a linear map. -/ @[simps] def iterate_range (f : M →ₗ[R] M) : ℕ →o (submodule R M)ᵒᵈ := ⟨λ n, (f ^ n).range, λ n m w x h, begin obtain ⟨c, rfl⟩ := le_iff_exists_add.mp w, rw linear_map.mem_range at h, obtain ⟨m, rfl⟩ := h, rw linear_map.mem_range, use (f ^ c) m, rw [pow_add, linear_map.mul_apply], end⟩ /-- Restrict the codomain of a linear map `f` to `f.range`. This is the bundled version of `set.range_factorization`. -/ @[reducible] def range_restrict [ring_hom_surjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : M →ₛₗ[τ₁₂] f.range := f.cod_restrict f.range f.mem_range_self /-- The range of a linear map is finite if the domain is finite. Note: this instance can form a diamond with `subtype.fintype` in the presence of `fintype M₂`. -/ instance fintype_range [fintype M] [decidable_eq M₂] [ring_hom_surjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : fintype (range f) := set.fintype_range f variables {F : Type*} [sc : semilinear_map_class F τ₁₂ M M₂] include sc /-- The kernel of a linear map `f : M → M₂` is defined to be `comap f ⊥`. This is equivalent to the set of `x : M` such that `f x = 0`. The kernel is a submodule of `M`. -/ def ker (f : F) : submodule R M := comap f ⊥ @[simp] theorem mem_ker {f : F} {y} : y ∈ ker f ↔ f y = 0 := mem_bot R₂ omit sc @[simp] theorem ker_id : ker (linear_map.id : M →ₗ[R] M) = ⊥ := rfl include sc @[simp] theorem map_coe_ker (f : F) (x : ker f) : f x = 0 := mem_ker.1 x.2 omit sc lemma ker_to_add_submonoid (f : M →ₛₗ[τ₁₂] M₂) : f.ker.to_add_submonoid = f.to_add_monoid_hom.mker := rfl lemma comp_ker_subtype (f : M →ₛₗ[τ₁₂] M₂) : f.comp f.ker.subtype = 0 := linear_map.ext $ λ x, suffices f x = 0, by simp [this], mem_ker.1 x.2 theorem ker_comp (f : M →ₛₗ[τ₁₂] M₂) (g : M₂ →ₛₗ[τ₂₃] M₃) : ker (g.comp f : M →ₛₗ[τ₁₃] M₃) = comap f (ker g) := rfl theorem ker_le_ker_comp (f : M →ₛₗ[τ₁₂] M₂) (g : M₂ →ₛₗ[τ₂₃] M₃) : ker f ≤ ker (g.comp f : M →ₛₗ[τ₁₃] M₃) := by rw ker_comp; exact comap_mono bot_le include sc theorem disjoint_ker {f : F} {p : submodule R M} : disjoint p (ker f) ↔ ∀ x ∈ p, f x = 0 → x = 0 := by simp [disjoint_def] theorem ker_eq_bot' {f : F} : ker f = ⊥ ↔ (∀ m, f m = 0 → m = 0) := by simpa [disjoint_iff_inf_le] using @disjoint_ker _ _ _ _ _ _ _ _ _ _ _ _ _ f ⊤ omit sc theorem ker_eq_bot_of_inverse {τ₂₁ : R₂ →+* R} [ring_hom_inv_pair τ₁₂ τ₂₁] {f : M →ₛₗ[τ₁₂] M₂} {g : M₂ →ₛₗ[τ₂₁] M} (h : (g.comp f : M →ₗ[R] M) = id) : ker f = ⊥ := ker_eq_bot'.2 $ λ m hm, by rw [← id_apply m, ← h, comp_apply, hm, g.map_zero] include sc lemma le_ker_iff_map [ring_hom_surjective τ₁₂] {f : F} {p : submodule R M} : p ≤ ker f ↔ map f p = ⊥ := by rw [ker, eq_bot_iff, map_le_iff_le_comap] omit sc lemma ker_cod_restrict {τ₂₁ : R₂ →+* R} (p : submodule R M) (f : M₂ →ₛₗ[τ₂₁] M) (hf) : ker (cod_restrict p f hf) = ker f := by rw [ker, comap_cod_restrict, submodule.map_bot]; refl lemma range_cod_restrict {τ₂₁ : R₂ →+* R} [ring_hom_surjective τ₂₁] (p : submodule R M) (f : M₂ →ₛₗ[τ₂₁] M) (hf) : range (cod_restrict p f hf) = comap p.subtype f.range := by simpa only [range_eq_map] using map_cod_restrict _ _ _ _ lemma ker_restrict [add_comm_monoid M₁] [module R M₁] {p : submodule R M} {q : submodule R M₁} {f : M →ₗ[R] M₁} (hf : ∀ x : M, x ∈ p → f x ∈ q) : ker (f.restrict hf) = (f.dom_restrict p).ker := by rw [restrict_eq_cod_restrict_dom_restrict, ker_cod_restrict] include sc lemma _root_.submodule.map_comap_eq [ring_hom_surjective τ₁₂] (f : F) (q : submodule R₂ M₂) : map f (comap f q) = range f ⊓ q := le_antisymm (le_inf map_le_range (map_comap_le _ _)) $ by rintro _ ⟨⟨x, _, rfl⟩, hx⟩; exact ⟨x, hx, rfl⟩ lemma _root_.submodule.map_comap_eq_self [ring_hom_surjective τ₁₂] {f : F} {q : submodule R₂ M₂} (h : q ≤ range f) : map f (comap f q) = q := by rwa [submodule.map_comap_eq, inf_eq_right] omit sc @[simp] theorem ker_zero : ker (0 : M →ₛₗ[τ₁₂] M₂) = ⊤ := eq_top_iff'.2 $ λ x, by simp @[simp] theorem range_zero [ring_hom_surjective τ₁₂] : range (0 : M →ₛₗ[τ₁₂] M₂) = ⊥ := by simpa only [range_eq_map] using submodule.map_zero _ theorem ker_eq_top {f : M →ₛₗ[τ₁₂] M₂} : ker f = ⊤ ↔ f = 0 := ⟨λ h, ext $ λ x, mem_ker.1 $ h.symm ▸ trivial, λ h, h.symm ▸ ker_zero⟩ section variables [ring_hom_surjective τ₁₂] lemma range_le_bot_iff (f : M →ₛₗ[τ₁₂] M₂) : range f ≤ ⊥ ↔ f = 0 := by rw [range_le_iff_comap]; exact ker_eq_top theorem range_eq_bot {f : M →ₛₗ[τ₁₂] M₂} : range f = ⊥ ↔ f = 0 := by rw [← range_le_bot_iff, le_bot_iff] lemma range_le_ker_iff {f : M →ₛₗ[τ₁₂] M₂} {g : M₂ →ₛₗ[τ₂₃] M₃} : range f ≤ ker g ↔ (g.comp f : M →ₛₗ[τ₁₃] M₃) = 0 := ⟨λ h, ker_eq_top.1 $ eq_top_iff'.2 $ λ x, h $ ⟨_, rfl⟩, λ h x hx, mem_ker.2 $ exists.elim hx $ λ y hy, by rw [←hy, ←comp_apply, h, zero_apply]⟩ include sc theorem comap_le_comap_iff {f : F} (hf : range f = ⊤) {p p'} : comap f p ≤ comap f p' ↔ p ≤ p' := ⟨λ H x hx, by rcases range_eq_top.1 hf x with ⟨y, hy, rfl⟩; exact H hx, comap_mono⟩ theorem comap_injective {f : F} (hf : range f = ⊤) : injective (comap f) := λ p p' h, le_antisymm ((comap_le_comap_iff hf).1 (le_of_eq h)) ((comap_le_comap_iff hf).1 (ge_of_eq h)) end include sc theorem ker_eq_bot_of_injective {f : F} (hf : injective f) : ker f = ⊥ := begin have : disjoint ⊤ (ker f), by { rw [disjoint_ker, ← map_zero f], exact λ x hx H, hf H }, simpa [disjoint_iff_inf_le] end omit sc /-- The increasing sequence of submodules consisting of the kernels of the iterates of a linear map. -/ @[simps] def iterate_ker (f : M →ₗ[R] M) : ℕ →o submodule R M := ⟨λ n, (f ^ n).ker, λ n m w x h, begin obtain ⟨c, rfl⟩ := le_iff_exists_add.mp w, rw linear_map.mem_ker at h, rw [linear_map.mem_ker, add_comm, pow_add, linear_map.mul_apply, h, linear_map.map_zero], end⟩ end add_comm_monoid section ring variables [ring R] [ring R₂] [ring R₃] variables [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R₂ M₂] [module R₃ M₃] variables {τ₁₂ : R →+* R₂} {τ₂₃ : R₂ →+* R₃} {τ₁₃ : R →+* R₃} variables [ring_hom_comp_triple τ₁₂ τ₂₃ τ₁₃] variables {F : Type*} [sc : semilinear_map_class F τ₁₂ M M₂] variables {f : F} include R open submodule lemma range_to_add_subgroup [ring_hom_surjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : f.range.to_add_subgroup = f.to_add_monoid_hom.range := rfl lemma ker_to_add_subgroup (f : M →ₛₗ[τ₁₂] M₂) : f.ker.to_add_subgroup = f.to_add_monoid_hom.ker := rfl lemma eq_locus_eq_ker_sub (f g : M →ₛₗ[τ₁₂] M₂) : f.eq_locus g = (f - g).ker := set_like.ext $ λ v, sub_eq_zero.symm include sc theorem sub_mem_ker_iff {x y} : x - y ∈ ker f ↔ f x = f y := by rw [mem_ker, map_sub, sub_eq_zero] theorem disjoint_ker' {p : submodule R M} : disjoint p (ker f) ↔ ∀ x y ∈ p, f x = f y → x = y := disjoint_ker.trans ⟨λ H x hx y hy h, eq_of_sub_eq_zero $ H _ (sub_mem hx hy) (by simp [h]), λ H x h₁ h₂, H x h₁ 0 (zero_mem _) (by simpa using h₂)⟩ theorem inj_on_of_disjoint_ker {p : submodule R M} {s : set M} (h : s ⊆ p) (hd : disjoint p (ker f)) : set.inj_on f s := λ x hx y hy, disjoint_ker'.1 hd _ (h hx) _ (h hy) variables (F) theorem _root_.linear_map_class.ker_eq_bot : ker f = ⊥ ↔ injective f := by simpa [disjoint_iff_inf_le] using @disjoint_ker' _ _ _ _ _ _ _ _ _ _ _ _ _ f ⊤ variables {F} omit sc theorem ker_eq_bot {f : M →ₛₗ[τ₁₂] M₂} : ker f = ⊥ ↔ injective f := linear_map_class.ker_eq_bot _ include sc lemma ker_le_iff [ring_hom_surjective τ₁₂] {p : submodule R M} : ker f ≤ p ↔ ∃ (y ∈ range f), f ⁻¹' {y} ⊆ p := begin split, { intros h, use 0, rw [← set_like.mem_coe, range_coe], exact ⟨⟨0, map_zero f⟩, h⟩, }, { rintros ⟨y, h₁, h₂⟩, rw set_like.le_def, intros z hz, simp only [mem_ker, set_like.mem_coe] at hz, rw [← set_like.mem_coe, range_coe, set.mem_range] at h₁, obtain ⟨x, hx⟩ := h₁, have hx' : x ∈ p, { exact h₂ hx, }, have hxz : z + x ∈ p, { apply h₂, simp [hx, hz], }, suffices : z + x - x ∈ p, { simpa only [this, add_sub_cancel], }, exact p.sub_mem hxz hx', }, end omit sc end ring section field variables [field K] [field K₂] variables [add_comm_group V] [module K V] variables [add_comm_group V₂] [module K V₂] lemma ker_smul (f : V →ₗ[K] V₂) (a : K) (h : a ≠ 0) : ker (a • f) = ker f := submodule.comap_smul f _ a h lemma ker_smul' (f : V →ₗ[K] V₂) (a : K) : ker (a • f) = ⨅(h : a ≠ 0), ker f := submodule.comap_smul' f _ a lemma range_smul (f : V →ₗ[K] V₂) (a : K) (h : a ≠ 0) : range (a • f) = range f := by simpa only [range_eq_map] using submodule.map_smul f _ a h lemma range_smul' (f : V →ₗ[K] V₂) (a : K) : range (a • f) = ⨆(h : a ≠ 0), range f := by simpa only [range_eq_map] using submodule.map_smul' f _ a end field end linear_map namespace is_linear_map lemma is_linear_map_add [semiring R] [add_comm_monoid M] [module R M] : is_linear_map R (λ (x : M × M), x.1 + x.2) := begin apply is_linear_map.mk, { intros x y, simp only [prod.fst_add, prod.snd_add], cc }, { intros x y, simp [smul_add] } end lemma is_linear_map_sub {R M : Type*} [semiring R] [add_comm_group M] [module R M]: is_linear_map R (λ (x : M × M), x.1 - x.2) := begin apply is_linear_map.mk, { intros x y, simp [add_comm, add_left_comm, sub_eq_add_neg] }, { intros x y, simp [smul_sub] } end end is_linear_map namespace submodule section add_comm_monoid variables [semiring R] [semiring R₂] [add_comm_monoid M] [add_comm_monoid M₂] variables [module R M] [module R₂ M₂] variables (p p' : submodule R M) (q : submodule R₂ M₂) variables {τ₁₂ : R →+* R₂} variables {F : Type*} [sc : semilinear_map_class F τ₁₂ M M₂] open linear_map include sc @[simp] theorem map_top [ring_hom_surjective τ₁₂] (f : F) : map f ⊤ = range f := (range_eq_map f).symm @[simp] theorem comap_bot (f : F) : comap f ⊥ = ker f := rfl omit sc @[simp] theorem ker_subtype : p.subtype.ker = ⊥ := ker_eq_bot_of_injective $ λ x y, subtype.ext_val @[simp] theorem range_subtype : p.subtype.range = p := by simpa using map_comap_subtype p ⊤ lemma map_subtype_le (p' : submodule R p) : map p.subtype p' ≤ p := by simpa using (map_le_range : map p.subtype p' ≤ p.subtype.range) /-- Under the canonical linear map from a submodule `p` to the ambient space `M`, the image of the maximal submodule of `p` is just `p `. -/ @[simp] lemma map_subtype_top : map p.subtype (⊤ : submodule R p) = p := by simp @[simp] lemma comap_subtype_eq_top {p p' : submodule R M} : comap p.subtype p' = ⊤ ↔ p ≤ p' := eq_top_iff.trans $ map_le_iff_le_comap.symm.trans $ by rw [map_subtype_top] @[simp] lemma comap_subtype_self : comap p.subtype p = ⊤ := comap_subtype_eq_top.2 le_rfl @[simp] theorem ker_of_le (p p' : submodule R M) (h : p ≤ p') : (of_le h).ker = ⊥ := by rw [of_le, ker_cod_restrict, ker_subtype] lemma range_of_le (p q : submodule R M) (h : p ≤ q) : (of_le h).range = comap q.subtype p := by rw [← map_top, of_le, linear_map.map_cod_restrict, map_top, range_subtype] @[simp] lemma map_subtype_range_of_le {p p' : submodule R M} (h : p ≤ p') : map p'.subtype (of_le h).range = p := by simp [range_of_le, map_comap_eq, h] lemma disjoint_iff_comap_eq_bot {p q : submodule R M} : disjoint p q ↔ comap p.subtype q = ⊥ := by rw [←(map_injective_of_injective (show injective p.subtype, from subtype.coe_injective)).eq_iff, map_comap_subtype, map_bot, disjoint_iff] /-- If `N ⊆ M` then submodules of `N` are the same as submodules of `M` contained in `N` -/ def map_subtype.rel_iso : submodule R p ≃o {p' : submodule R M // p' ≤ p} := { to_fun := λ p', ⟨map p.subtype p', map_subtype_le p _⟩, inv_fun := λ q, comap p.subtype q, left_inv := λ p', comap_map_eq_of_injective subtype.coe_injective p', right_inv := λ ⟨q, hq⟩, subtype.ext_val $ by simp [map_comap_subtype p, inf_of_le_right hq], map_rel_iff' := λ p₁ p₂, subtype.coe_le_coe.symm.trans begin dsimp, rw [map_le_iff_le_comap, comap_map_eq_of_injective (show injective p.subtype, from subtype.coe_injective) p₂], end } /-- If `p ⊆ M` is a submodule, the ordering of submodules of `p` is embedded in the ordering of submodules of `M`. -/ def map_subtype.order_embedding : submodule R p ↪o submodule R M := (rel_iso.to_rel_embedding $ map_subtype.rel_iso p).trans (subtype.rel_embedding _ _) @[simp] lemma map_subtype_embedding_eq (p' : submodule R p) : map_subtype.order_embedding p p' = map p.subtype p' := rfl end add_comm_monoid end submodule namespace linear_map section semiring variables [semiring R] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [module R M] [module R₂ M₂] [module R₃ M₃] variables {τ₁₂ : R →+* R₂} {τ₂₃ : R₂ →+* R₃} {τ₁₃ : R →+* R₃} variables [ring_hom_comp_triple τ₁₂ τ₂₃ τ₁₃] /-- A monomorphism is injective. -/ lemma ker_eq_bot_of_cancel {f : M →ₛₗ[τ₁₂] M₂} (h : ∀ (u v : f.ker →ₗ[R] M), f.comp u = f.comp v → u = v) : f.ker = ⊥ := begin have h₁ : f.comp (0 : f.ker →ₗ[R] M) = 0 := comp_zero _, rw [←submodule.range_subtype f.ker, ←h 0 f.ker.subtype (eq.trans h₁ (comp_ker_subtype f).symm)], exact range_zero end lemma range_comp_of_range_eq_top [ring_hom_surjective τ₁₂] [ring_hom_surjective τ₂₃] [ring_hom_surjective τ₁₃] {f : M →ₛₗ[τ₁₂] M₂} (g : M₂ →ₛₗ[τ₂₃] M₃) (hf : range f = ⊤) : range (g.comp f : M →ₛₗ[τ₁₃] M₃) = range g := by rw [range_comp, hf, submodule.map_top] lemma ker_comp_of_ker_eq_bot (f : M →ₛₗ[τ₁₂] M₂) {g : M₂ →ₛₗ[τ₂₃] M₃} (hg : ker g = ⊥) : ker (g.comp f : M →ₛₗ[τ₁₃] M₃) = ker f := by rw [ker_comp, hg, submodule.comap_bot] section image /-- If `O` is a submodule of `M`, and `Φ : O →ₗ M'` is a linear map, then `(ϕ : O →ₗ M').submodule_image N` is `ϕ(N)` as a submodule of `M'` -/ def submodule_image {M' : Type*} [add_comm_monoid M'] [module R M'] {O : submodule R M} (ϕ : O →ₗ[R] M') (N : submodule R M) : submodule R M' := (N.comap O.subtype).map ϕ @[simp] lemma mem_submodule_image {M' : Type*} [add_comm_monoid M'] [module R M'] {O : submodule R M} {ϕ : O →ₗ[R] M'} {N : submodule R M} {x : M'} : x ∈ ϕ.submodule_image N ↔ ∃ y (yO : y ∈ O) (yN : y ∈ N), ϕ ⟨y, yO⟩ = x := begin refine submodule.mem_map.trans ⟨_, _⟩; simp_rw submodule.mem_comap, { rintro ⟨⟨y, yO⟩, (yN : y ∈ N), h⟩, exact ⟨y, yO, yN, h⟩ }, { rintro ⟨y, yO, yN, h⟩, exact ⟨⟨y, yO⟩, yN, h⟩ } end lemma mem_submodule_image_of_le {M' : Type*} [add_comm_monoid M'] [module R M'] {O : submodule R M} {ϕ : O →ₗ[R] M'} {N : submodule R M} (hNO : N ≤ O) {x : M'} : x ∈ ϕ.submodule_image N ↔ ∃ y (yN : y ∈ N), ϕ ⟨y, hNO yN⟩ = x := begin refine mem_submodule_image.trans ⟨_, _⟩, { rintro ⟨y, yO, yN, h⟩, exact ⟨y, yN, h⟩ }, { rintro ⟨y, yN, h⟩, exact ⟨y, hNO yN, yN, h⟩ } end lemma submodule_image_apply_of_le {M' : Type*} [add_comm_group M'] [module R M'] {O : submodule R M} (ϕ : O →ₗ[R] M') (N : submodule R M) (hNO : N ≤ O) : ϕ.submodule_image N = (ϕ.comp (submodule.of_le hNO)).range := by rw [submodule_image, range_comp, submodule.range_of_le] end image end semiring end linear_map @[simp] lemma linear_map.range_range_restrict [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module R M₂] (f : M →ₗ[R] M₂) : f.range_restrict.range = ⊤ := by simp [f.range_cod_restrict _] @[simp] lemma linear_map.ker_range_restrict [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module R M₂] (f : M →ₗ[R] M₂) : f.range_restrict.ker = f.ker := linear_map.ker_cod_restrict _ _ _ /-! ### Linear equivalences -/ namespace linear_equiv section add_comm_monoid section subsingleton variables [semiring R] [semiring R₂] variables [add_comm_monoid M] [add_comm_monoid M₂] variables [module R M] [module R₂ M₂] variables {σ₁₂ : R →+* R₂} {σ₂₁ : R₂ →+* R} variables [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] include σ₂₁ section module variables [subsingleton M] [subsingleton M₂] /-- Between two zero modules, the zero map is an equivalence. -/ instance : has_zero (M ≃ₛₗ[σ₁₂] M₂) := ⟨{ to_fun := 0, inv_fun := 0, right_inv := λ x, subsingleton.elim _ _, left_inv := λ x, subsingleton.elim _ _, ..(0 : M →ₛₗ[σ₁₂] M₂)}⟩ omit σ₂₁ -- Even though these are implied by `subsingleton.elim` via the `unique` instance below, they're -- nice to have as `rfl`-lemmas for `dsimp`. include σ₂₁ @[simp] lemma zero_symm : (0 : M ≃ₛₗ[σ₁₂] M₂).symm = 0 := rfl @[simp] lemma coe_zero : ⇑(0 : M ≃ₛₗ[σ₁₂] M₂) = 0 := rfl lemma zero_apply (x : M) : (0 : M ≃ₛₗ[σ₁₂] M₂) x = 0 := rfl /-- Between two zero modules, the zero map is the only equivalence. -/ instance : unique (M ≃ₛₗ[σ₁₂] M₂) := { uniq := λ f, to_linear_map_injective (subsingleton.elim _ _), default := 0 } omit σ₂₁ end module instance unique_of_subsingleton [subsingleton R] [subsingleton R₂] : unique (M ≃ₛₗ[σ₁₂] M₂) := by { haveI := module.subsingleton R M, haveI := module.subsingleton R₂ M₂, apply_instance } end subsingleton section variables [semiring R] [semiring R₂] [semiring R₃] [semiring R₄] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] variables {module_M : module R M} {module_M₂ : module R₂ M₂} variables {σ₁₂ : R →+* R₂} {σ₂₁ : R₂ →+* R} variables {re₁₂ : ring_hom_inv_pair σ₁₂ σ₂₁} {re₂₁ : ring_hom_inv_pair σ₂₁ σ₁₂} variables (e e' : M ≃ₛₗ[σ₁₂] M₂) @[simp] lemma map_sum {s : finset ι} (u : ι → M) : e (∑ i in s, u i) = ∑ i in s, e (u i) := e.to_linear_map.map_sum lemma map_eq_comap {p : submodule R M} : (p.map (e : M →ₛₗ[σ₁₂] M₂) : submodule R₂ M₂) = p.comap (e.symm : M₂ →ₛₗ[σ₂₁] M) := set_like.coe_injective $ by simp [e.image_eq_preimage] /-- A linear equivalence of two modules restricts to a linear equivalence from any submodule `p` of the domain onto the image of that submodule. This is the linear version of `add_equiv.submonoid_map` and `add_equiv.subgroup_map`. This is `linear_equiv.of_submodule'` but with `map` on the right instead of `comap` on the left. -/ def submodule_map (p : submodule R M) : p ≃ₛₗ[σ₁₂] ↥(p.map (e : M →ₛₗ[σ₁₂] M₂) : submodule R₂ M₂) := { inv_fun := λ y, ⟨(e.symm : M₂ →ₛₗ[σ₂₁] M) y, by { rcases y with ⟨y', hy⟩, rw submodule.mem_map at hy, rcases hy with ⟨x, hx, hxy⟩, subst hxy, simp only [symm_apply_apply, submodule.coe_mk, coe_coe, hx], }⟩, left_inv := λ x, by simp only [linear_map.dom_restrict_apply, linear_map.cod_restrict_apply, linear_map.to_fun_eq_coe, linear_equiv.coe_coe, linear_equiv.symm_apply_apply, set_like.eta], right_inv := λ y, by { apply set_coe.ext, simp only [linear_map.dom_restrict_apply, linear_map.cod_restrict_apply, linear_map.to_fun_eq_coe, linear_equiv.coe_coe, set_like.coe_mk, linear_equiv.apply_symm_apply] }, ..((e : M →ₛₗ[σ₁₂] M₂).dom_restrict p).cod_restrict (p.map (e : M →ₛₗ[σ₁₂] M₂)) (λ x, ⟨x, by simp only [linear_map.dom_restrict_apply, eq_self_iff_true, and_true, set_like.coe_mem, set_like.mem_coe]⟩) } include σ₂₁ @[simp] lemma submodule_map_apply (p : submodule R M) (x : p) : ↑(e.submodule_map p x) = e x := rfl @[simp] lemma submodule_map_symm_apply (p : submodule R M) (x : (p.map (e : M →ₛₗ[σ₁₂] M₂) : submodule R₂ M₂)) : ↑((e.submodule_map p).symm x) = e.symm x := rfl omit σ₂₁ end section finsupp variables {γ : Type*} variables [semiring R] [semiring R₂] variables [add_comm_monoid M] [add_comm_monoid M₂] variables [module R M] [module R₂ M₂] [has_zero γ] variables {τ₁₂ : R →+* R₂} {τ₂₁ : R₂ →+* R} variables [ring_hom_inv_pair τ₁₂ τ₂₁] [ring_hom_inv_pair τ₂₁ τ₁₂] include τ₂₁ @[simp] lemma map_finsupp_sum (f : M ≃ₛₗ[τ₁₂] M₂) {t : ι →₀ γ} {g : ι → γ → M} : f (t.sum g) = t.sum (λ i d, f (g i d)) := f.map_sum _ omit τ₂₁ end finsupp section dfinsupp open dfinsupp variables [semiring R] [semiring R₂] variables [add_comm_monoid M] [add_comm_monoid M₂] variables [module R M] [module R₂ M₂] variables {τ₁₂ : R →+* R₂} {τ₂₁ : R₂ →+* R} variables [ring_hom_inv_pair τ₁₂ τ₂₁] [ring_hom_inv_pair τ₂₁ τ₁₂] variables {γ : ι → Type*} [decidable_eq ι] include τ₂₁ @[simp] lemma map_dfinsupp_sum [Π i, has_zero (γ i)] [Π i (x : γ i), decidable (x ≠ 0)] (f : M ≃ₛₗ[τ₁₂] M₂) (t : Π₀ i, γ i) (g : Π i, γ i → M) : f (t.sum g) = t.sum (λ i d, f (g i d)) := f.map_sum _ @[simp] lemma map_dfinsupp_sum_add_hom [Π i, add_zero_class (γ i)] (f : M ≃ₛₗ[τ₁₂] M₂) (t : Π₀ i, γ i) (g : Π i, γ i →+ M) : f (sum_add_hom g t) = sum_add_hom (λ i, f.to_add_equiv.to_add_monoid_hom.comp (g i)) t := f.to_add_equiv.map_dfinsupp_sum_add_hom _ _ end dfinsupp section uncurry variables [semiring R] [semiring R₂] [semiring R₃] [semiring R₄] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] variables (V V₂ R) /-- Linear equivalence between a curried and uncurried function. Differs from `tensor_product.curry`. -/ protected def curry : (V × V₂ → R) ≃ₗ[R] (V → V₂ → R) := { map_add' := λ _ _, by { ext, refl }, map_smul' := λ _ _, by { ext, refl }, .. equiv.curry _ _ _ } @[simp] lemma coe_curry : ⇑(linear_equiv.curry R V V₂) = curry := rfl @[simp] lemma coe_curry_symm : ⇑(linear_equiv.curry R V V₂).symm = uncurry := rfl end uncurry section variables [semiring R] [semiring R₂] [semiring R₃] [semiring R₄] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] variables {module_M : module R M} {module_M₂ : module R₂ M₂} {module_M₃ : module R₃ M₃} variables {σ₁₂ : R →+* R₂} {σ₂₁ : R₂ →+* R} variables {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R →+* R₃} [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] variables {σ₃₂ : R₃ →+* R₂} variables {re₁₂ : ring_hom_inv_pair σ₁₂ σ₂₁} {re₂₁ : ring_hom_inv_pair σ₂₁ σ₁₂} variables {re₂₃ : ring_hom_inv_pair σ₂₃ σ₃₂} {re₃₂ : ring_hom_inv_pair σ₃₂ σ₂₃} variables (f : M →ₛₗ[σ₁₂] M₂) (g : M₂ →ₛₗ[σ₂₁] M) (e : M ≃ₛₗ[σ₁₂] M₂) (h : M₂ →ₛₗ[σ₂₃] M₃) variables (e'' : M₂ ≃ₛₗ[σ₂₃] M₃) variables (p q : submodule R M) /-- Linear equivalence between two equal submodules. -/ def of_eq (h : p = q) : p ≃ₗ[R] q := { map_smul' := λ _ _, rfl, map_add' := λ _ _, rfl, .. equiv.set.of_eq (congr_arg _ h) } variables {p q} @[simp] lemma coe_of_eq_apply (h : p = q) (x : p) : (of_eq p q h x : M) = x := rfl @[simp] lemma of_eq_symm (h : p = q) : (of_eq p q h).symm = of_eq q p h.symm := rfl @[simp] lemma of_eq_rfl : of_eq p p rfl = linear_equiv.refl R p := by ext; refl include σ₂₁ /-- A linear equivalence which maps a submodule of one module onto another, restricts to a linear equivalence of the two submodules. -/ def of_submodules (p : submodule R M) (q : submodule R₂ M₂) (h : p.map (e : M →ₛₗ[σ₁₂] M₂) = q) : p ≃ₛₗ[σ₁₂] q := (e.submodule_map p).trans (linear_equiv.of_eq _ _ h) @[simp] lemma of_submodules_apply {p : submodule R M} {q : submodule R₂ M₂} (h : p.map ↑e = q) (x : p) : ↑(e.of_submodules p q h x) = e x := rfl @[simp] lemma of_submodules_symm_apply {p : submodule R M} {q : submodule R₂ M₂} (h : p.map ↑e = q) (x : q) : ↑((e.of_submodules p q h).symm x) = e.symm x := rfl include re₁₂ re₂₁ /-- A linear equivalence of two modules restricts to a linear equivalence from the preimage of any submodule to that submodule. This is `linear_equiv.of_submodule` but with `comap` on the left instead of `map` on the right. -/ def of_submodule' [module R M] [module R₂ M₂] (f : M ≃ₛₗ[σ₁₂] M₂) (U : submodule R₂ M₂) : U.comap (f : M →ₛₗ[σ₁₂] M₂) ≃ₛₗ[σ₁₂] U := (f.symm.of_submodules _ _ f.symm.map_eq_comap).symm lemma of_submodule'_to_linear_map [module R M] [module R₂ M₂] (f : M ≃ₛₗ[σ₁₂] M₂) (U : submodule R₂ M₂) : (f.of_submodule' U).to_linear_map = (f.to_linear_map.dom_restrict _).cod_restrict _ subtype.prop := by { ext, refl } @[simp] lemma of_submodule'_apply [module R M] [module R₂ M₂] (f : M ≃ₛₗ[σ₁₂] M₂) (U : submodule R₂ M₂) (x : U.comap (f : M →ₛₗ[σ₁₂] M₂)) : (f.of_submodule' U x : M₂) = f (x : M) := rfl @[simp] lemma of_submodule'_symm_apply [module R M] [module R₂ M₂] (f : M ≃ₛₗ[σ₁₂] M₂) (U : submodule R₂ M₂) (x : U) : ((f.of_submodule' U).symm x : M) = f.symm (x : M₂) := rfl variable (p) omit σ₂₁ re₁₂ re₂₁ /-- The top submodule of `M` is linearly equivalent to `M`. -/ def of_top (h : p = ⊤) : p ≃ₗ[R] M := { inv_fun := λ x, ⟨x, h.symm ▸ trivial⟩, left_inv := λ ⟨x, h⟩, rfl, right_inv := λ x, rfl, .. p.subtype } @[simp] theorem of_top_apply {h} (x : p) : of_top p h x = x := rfl @[simp] theorem coe_of_top_symm_apply {h} (x : M) : ((of_top p h).symm x : M) = x := rfl theorem of_top_symm_apply {h} (x : M) : (of_top p h).symm x = ⟨x, h.symm ▸ trivial⟩ := rfl include σ₂₁ re₁₂ re₂₁ /-- If a linear map has an inverse, it is a linear equivalence. -/ def of_linear (h₁ : f.comp g = linear_map.id) (h₂ : g.comp f = linear_map.id) : M ≃ₛₗ[σ₁₂] M₂ := { inv_fun := g, left_inv := linear_map.ext_iff.1 h₂, right_inv := linear_map.ext_iff.1 h₁, ..f } omit σ₂₁ re₁₂ re₂₁ include σ₂₁ re₁₂ re₂₁ @[simp] theorem of_linear_apply {h₁ h₂} (x : M) : of_linear f g h₁ h₂ x = f x := rfl omit σ₂₁ re₁₂ re₂₁ include σ₂₁ re₁₂ re₂₁ @[simp] theorem of_linear_symm_apply {h₁ h₂} (x : M₂) : (of_linear f g h₁ h₂).symm x = g x := rfl omit σ₂₁ re₁₂ re₂₁ @[simp] protected theorem range : (e : M →ₛₗ[σ₁₂] M₂).range = ⊤ := linear_map.range_eq_top.2 e.to_equiv.surjective include σ₂₁ re₁₂ re₂₁ @[simp] protected theorem _root_.linear_equiv_class.range [module R M] [module R₂ M₂] {F : Type*} [semilinear_equiv_class F σ₁₂ M M₂] (e : F) : linear_map.range e = ⊤ := linear_map.range_eq_top.2 (equiv_like.surjective e) lemma eq_bot_of_equiv [module R₂ M₂] (e : p ≃ₛₗ[σ₁₂] (⊥ : submodule R₂ M₂)) : p = ⊥ := begin refine bot_unique (set_like.le_def.2 $ assume b hb, (submodule.mem_bot R).2 _), rw [← p.mk_eq_zero hb, ← e.map_eq_zero_iff], apply submodule.eq_zero_of_bot_submodule end omit σ₂₁ re₁₂ re₂₁ @[simp] protected theorem ker : (e : M →ₛₗ[σ₁₂] M₂).ker = ⊥ := linear_map.ker_eq_bot_of_injective e.to_equiv.injective @[simp] theorem range_comp [ring_hom_surjective σ₁₂] [ring_hom_surjective σ₂₃] [ring_hom_surjective σ₁₃] : (h.comp (e : M →ₛₗ[σ₁₂] M₂) : M →ₛₗ[σ₁₃] M₃).range = h.range := linear_map.range_comp_of_range_eq_top _ e.range include module_M @[simp] theorem ker_comp (l : M →ₛₗ[σ₁₂] M₂) : (((e'' : M₂ →ₛₗ[σ₂₃] M₃).comp l : M →ₛₗ[σ₁₃] M₃) : M →ₛₗ[σ₁₃] M₃).ker = l.ker := linear_map.ker_comp_of_ker_eq_bot _ e''.ker omit module_M variables {f g} include σ₂₁ /-- An linear map `f : M →ₗ[R] M₂` with a left-inverse `g : M₂ →ₗ[R] M` defines a linear equivalence between `M` and `f.range`. This is a computable alternative to `linear_equiv.of_injective`, and a bidirectional version of `linear_map.range_restrict`. -/ def of_left_inverse [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] {g : M₂ → M} (h : function.left_inverse g f) : M ≃ₛₗ[σ₁₂] f.range := { to_fun := f.range_restrict, inv_fun := g ∘ f.range.subtype, left_inv := h, right_inv := λ x, subtype.ext $ let ⟨x', hx'⟩ := linear_map.mem_range.mp x.prop in show f (g x) = x, by rw [←hx', h x'], .. f.range_restrict } omit σ₂₁ @[simp] lemma of_left_inverse_apply [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] (h : function.left_inverse g f) (x : M) : ↑(of_left_inverse h x) = f x := rfl include σ₂₁ @[simp] lemma of_left_inverse_symm_apply [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] (h : function.left_inverse g f) (x : f.range) : (of_left_inverse h).symm x = g x := rfl omit σ₂₁ variables (f) /-- An `injective` linear map `f : M →ₗ[R] M₂` defines a linear equivalence between `M` and `f.range`. See also `linear_map.of_left_inverse`. -/ noncomputable def of_injective [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] (h : injective f) : M ≃ₛₗ[σ₁₂] f.range := of_left_inverse $ classical.some_spec h.has_left_inverse @[simp] theorem of_injective_apply [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] {h : injective f} (x : M) : ↑(of_injective f h x) = f x := rfl /-- A bijective linear map is a linear equivalence. -/ noncomputable def of_bijective [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] (hf : bijective f) : M ≃ₛₗ[σ₁₂] M₂ := (of_injective f hf.injective).trans (of_top _ $ linear_map.range_eq_top.2 hf.surjective) @[simp] theorem of_bijective_apply [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] {hf} (x : M) : of_bijective f hf x = f x := rfl end end add_comm_monoid section add_comm_group variables [semiring R] [semiring R₂] [semiring R₃] [semiring R₄] variables [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] [add_comm_group M₄] variables {module_M : module R M} {module_M₂ : module R₂ M₂} variables {module_M₃ : module R₃ M₃} {module_M₄ : module R₄ M₄} variables {σ₁₂ : R →+* R₂} {σ₃₄ : R₃ →+* R₄} variables {σ₂₁ : R₂ →+* R} {σ₄₃ : R₄ →+* R₃} variables {re₁₂ : ring_hom_inv_pair σ₁₂ σ₂₁} {re₂₁ : ring_hom_inv_pair σ₂₁ σ₁₂} variables {re₃₄ : ring_hom_inv_pair σ₃₄ σ₄₃} {re₄₃ : ring_hom_inv_pair σ₄₃ σ₃₄} variables (e e₁ : M ≃ₛₗ[σ₁₂] M₂) (e₂ : M₃ ≃ₛₗ[σ₃₄] M₄) @[simp] theorem map_neg (a : M) : e (-a) = -e a := e.to_linear_map.map_neg a @[simp] theorem map_sub (a b : M) : e (a - b) = e a - e b := e.to_linear_map.map_sub a b end add_comm_group section neg variables (R) [semiring R] [add_comm_group M] [module R M] /-- `x ↦ -x` as a `linear_equiv` -/ def neg : M ≃ₗ[R] M := { .. equiv.neg M, .. (-linear_map.id : M →ₗ[R] M) } variable {R} @[simp] lemma coe_neg : ⇑(neg R : M ≃ₗ[R] M) = -id := rfl lemma neg_apply (x : M) : neg R x = -x := by simp @[simp] lemma symm_neg : (neg R : M ≃ₗ[R] M).symm = neg R := rfl end neg section comm_semiring variables [comm_semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [module R M] [module R M₂] [module R M₃] open _root_.linear_map /-- Multiplying by a unit `a` of the ring `R` is a linear equivalence. -/ def smul_of_unit (a : Rˣ) : M ≃ₗ[R] M := distrib_mul_action.to_linear_equiv R M a /-- A linear isomorphism between the domains and codomains of two spaces of linear maps gives a linear isomorphism between the two function spaces. -/ def arrow_congr {R M₁ M₂ M₂₁ M₂₂ : Sort*} [comm_semiring R] [add_comm_monoid M₁] [add_comm_monoid M₂] [add_comm_monoid M₂₁] [add_comm_monoid M₂₂] [module R M₁] [module R M₂] [module R M₂₁] [module R M₂₂] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : M₂₁ ≃ₗ[R] M₂₂) : (M₁ →ₗ[R] M₂₁) ≃ₗ[R] (M₂ →ₗ[R] M₂₂) := { to_fun := λ f : M₁ →ₗ[R] M₂₁, (e₂ : M₂₁ →ₗ[R] M₂₂).comp $ f.comp (e₁.symm : M₂ →ₗ[R] M₁), inv_fun := λ f, (e₂.symm : M₂₂ →ₗ[R] M₂₁).comp $ f.comp (e₁ : M₁ →ₗ[R] M₂), left_inv := λ f, by { ext x, simp only [symm_apply_apply, comp_app, coe_comp, coe_coe]}, right_inv := λ f, by { ext x, simp only [comp_app, apply_symm_apply, coe_comp, coe_coe]}, map_add' := λ f g, by { ext x, simp only [map_add, add_apply, comp_app, coe_comp, coe_coe]}, map_smul' := λ c f, by { ext x, simp only [smul_apply, comp_app, coe_comp, map_smulₛₗ e₂, coe_coe]} } @[simp] lemma arrow_congr_apply {R M₁ M₂ M₂₁ M₂₂ : Sort*} [comm_semiring R] [add_comm_monoid M₁] [add_comm_monoid M₂] [add_comm_monoid M₂₁] [add_comm_monoid M₂₂] [module R M₁] [module R M₂] [module R M₂₁] [module R M₂₂] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : M₂₁ ≃ₗ[R] M₂₂) (f : M₁ →ₗ[R] M₂₁) (x : M₂) : arrow_congr e₁ e₂ f x = e₂ (f (e₁.symm x)) := rfl @[simp] lemma arrow_congr_symm_apply {R M₁ M₂ M₂₁ M₂₂ : Sort*} [comm_semiring R] [add_comm_monoid M₁] [add_comm_monoid M₂] [add_comm_monoid M₂₁] [add_comm_monoid M₂₂] [module R M₁] [module R M₂] [module R M₂₁] [module R M₂₂] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : M₂₁ ≃ₗ[R] M₂₂) (f : M₂ →ₗ[R] M₂₂) (x : M₁) : (arrow_congr e₁ e₂).symm f x = e₂.symm (f (e₁ x)) := rfl lemma arrow_congr_comp {N N₂ N₃ : Sort*} [add_comm_monoid N] [add_comm_monoid N₂] [add_comm_monoid N₃] [module R N] [module R N₂] [module R N₃] (e₁ : M ≃ₗ[R] N) (e₂ : M₂ ≃ₗ[R] N₂) (e₃ : M₃ ≃ₗ[R] N₃) (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : arrow_congr e₁ e₃ (g.comp f) = (arrow_congr e₂ e₃ g).comp (arrow_congr e₁ e₂ f) := by { ext, simp only [symm_apply_apply, arrow_congr_apply, linear_map.comp_apply], } lemma arrow_congr_trans {M₁ M₂ M₃ N₁ N₂ N₃ : Sort*} [add_comm_monoid M₁] [module R M₁] [add_comm_monoid M₂] [module R M₂] [add_comm_monoid M₃] [module R M₃] [add_comm_monoid N₁] [module R N₁] [add_comm_monoid N₂] [module R N₂] [add_comm_monoid N₃] [module R N₃] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : N₁ ≃ₗ[R] N₂) (e₃ : M₂ ≃ₗ[R] M₃) (e₄ : N₂ ≃ₗ[R] N₃) : (arrow_congr e₁ e₂).trans (arrow_congr e₃ e₄) = arrow_congr (e₁.trans e₃) (e₂.trans e₄) := rfl /-- If `M₂` and `M₃` are linearly isomorphic then the two spaces of linear maps from `M` into `M₂` and `M` into `M₃` are linearly isomorphic. -/ def congr_right (f : M₂ ≃ₗ[R] M₃) : (M →ₗ[R] M₂) ≃ₗ[R] (M →ₗ[R] M₃) := arrow_congr (linear_equiv.refl R M) f /-- If `M` and `M₂` are linearly isomorphic then the two spaces of linear maps from `M` and `M₂` to themselves are linearly isomorphic. -/ def conj (e : M ≃ₗ[R] M₂) : (module.End R M) ≃ₗ[R] (module.End R M₂) := arrow_congr e e lemma conj_apply (e : M ≃ₗ[R] M₂) (f : module.End R M) : e.conj f = ((↑e : M →ₗ[R] M₂).comp f).comp (e.symm : M₂ →ₗ[R] M) := rfl lemma conj_apply_apply (e : M ≃ₗ[R] M₂) (f : module.End R M) (x : M₂) : e.conj f x = e (f (e.symm x)) := rfl lemma symm_conj_apply (e : M ≃ₗ[R] M₂) (f : module.End R M₂) : e.symm.conj f = ((↑e.symm : M₂ →ₗ[R] M).comp f).comp (e : M →ₗ[R] M₂) := rfl lemma conj_comp (e : M ≃ₗ[R] M₂) (f g : module.End R M) : e.conj (g.comp f) = (e.conj g).comp (e.conj f) := arrow_congr_comp e e e f g lemma conj_trans (e₁ : M ≃ₗ[R] M₂) (e₂ : M₂ ≃ₗ[R] M₃) : e₁.conj.trans e₂.conj = (e₁.trans e₂).conj := by { ext f x, refl, } @[simp] lemma conj_id (e : M ≃ₗ[R] M₂) : e.conj linear_map.id = linear_map.id := by { ext, simp [conj_apply], } end comm_semiring section field variables [field K] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module K M] [module K M₂] [module K M₃] variables (K) (M) open _root_.linear_map /-- Multiplying by a nonzero element `a` of the field `K` is a linear equivalence. -/ @[simps] def smul_of_ne_zero (a : K) (ha : a ≠ 0) : M ≃ₗ[K] M := smul_of_unit $ units.mk0 a ha end field end linear_equiv namespace submodule section module variables [semiring R] [add_comm_monoid M] [module R M] /-- Given `p` a submodule of the module `M` and `q` a submodule of `p`, `p.equiv_subtype_map q` is the natural `linear_equiv` between `q` and `q.map p.subtype`. -/ def equiv_subtype_map (p : submodule R M) (q : submodule R p) : q ≃ₗ[R] q.map p.subtype := { inv_fun := begin rintro ⟨x, hx⟩, refine ⟨⟨x, _⟩, _⟩; rcases hx with ⟨⟨_, h⟩, _, rfl⟩; assumption end, left_inv := λ ⟨⟨_, _⟩, _⟩, rfl, right_inv := λ ⟨x, ⟨_, h⟩, _, rfl⟩, rfl, .. (p.subtype.dom_restrict q).cod_restrict _ begin rintro ⟨x, hx⟩, refine ⟨x, hx, rfl⟩, end } @[simp] lemma equiv_subtype_map_apply {p : submodule R M} {q : submodule R p} (x : q) : (p.equiv_subtype_map q x : M) = p.subtype.dom_restrict q x := rfl @[simp] lemma equiv_subtype_map_symm_apply {p : submodule R M} {q : submodule R p} (x : q.map p.subtype) : ((p.equiv_subtype_map q).symm x : M) = x := by { cases x, refl } /-- If `s ≤ t`, then we can view `s` as a submodule of `t` by taking the comap of `t.subtype`. -/ @[simps] def comap_subtype_equiv_of_le {p q : submodule R M} (hpq : p ≤ q) : comap q.subtype p ≃ₗ[R] p := { to_fun := λ x, ⟨x, x.2⟩, inv_fun := λ x, ⟨⟨x, hpq x.2⟩, x.2⟩, left_inv := λ x, by simp only [coe_mk, set_like.eta, coe_coe], right_inv := λ x, by simp only [subtype.coe_mk, set_like.eta, coe_coe], map_add' := λ x y, rfl, map_smul' := λ c x, rfl } end module end submodule namespace submodule variables [comm_semiring R] [comm_semiring R₂] variables [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module R₂ M₂] variables [add_comm_monoid N] [add_comm_monoid N₂] [module R N] [module R N₂] variables {τ₁₂ : R →+* R₂} {τ₂₁ : R₂ →+* R} variables [ring_hom_inv_pair τ₁₂ τ₂₁] [ring_hom_inv_pair τ₂₁ τ₁₂] variables (p : submodule R M) (q : submodule R₂ M₂) variables (pₗ : submodule R N) (qₗ : submodule R N₂) include τ₂₁ @[simp] lemma mem_map_equiv {e : M ≃ₛₗ[τ₁₂] M₂} {x : M₂} : x ∈ p.map (e : M →ₛₗ[τ₁₂] M₂) ↔ e.symm x ∈ p := begin rw submodule.mem_map, split, { rintros ⟨y, hy, hx⟩, simp [←hx, hy], }, { intros hx, refine ⟨e.symm x, hx, by simp⟩, }, end omit τ₂₁ lemma map_equiv_eq_comap_symm (e : M ≃ₛₗ[τ₁₂] M₂) (K : submodule R M) : K.map (e : M →ₛₗ[τ₁₂] M₂) = K.comap (e.symm : M₂ →ₛₗ[τ₂₁] M) := submodule.ext (λ _, by rw [mem_map_equiv, mem_comap, linear_equiv.coe_coe]) lemma comap_equiv_eq_map_symm (e : M ≃ₛₗ[τ₁₂] M₂) (K : submodule R₂ M₂) : K.comap (e : M →ₛₗ[τ₁₂] M₂) = K.map (e.symm : M₂ →ₛₗ[τ₂₁] M) := (map_equiv_eq_comap_symm e.symm K).symm variables {p} include τ₂₁ lemma map_symm_eq_iff (e : M ≃ₛₗ[τ₁₂] M₂) {K : submodule R₂ M₂} : K.map e.symm = p ↔ p.map e = K := begin split; rintro rfl, { calc map e (map e.symm K) = comap e.symm (map e.symm K) : map_equiv_eq_comap_symm _ _ ... = K : comap_map_eq_of_injective e.symm.injective _ }, { calc map e.symm (map e p) = comap e (map e p) : (comap_equiv_eq_map_symm _ _).symm ... = p : comap_map_eq_of_injective e.injective _ }, end lemma order_iso_map_comap_apply' (e : M ≃ₛₗ[τ₁₂] M₂) (p : submodule R M) : order_iso_map_comap e p = comap e.symm p := p.map_equiv_eq_comap_symm _ lemma order_iso_map_comap_symm_apply' (e : M ≃ₛₗ[τ₁₂] M₂) (p : submodule R₂ M₂) : (order_iso_map_comap e).symm p = map e.symm p := p.comap_equiv_eq_map_symm _ omit τ₂₁ lemma comap_le_comap_smul (fₗ : N →ₗ[R] N₂) (c : R) : comap fₗ qₗ ≤ comap (c • fₗ) qₗ := begin rw set_like.le_def, intros m h, change c • (fₗ m) ∈ qₗ, change fₗ m ∈ qₗ at h, apply qₗ.smul_mem _ h, end lemma inf_comap_le_comap_add (f₁ f₂ : M →ₛₗ[τ₁₂] M₂) : comap f₁ q ⊓ comap f₂ q ≤ comap (f₁ + f₂) q := begin rw set_like.le_def, intros m h, change f₁ m + f₂ m ∈ q, change f₁ m ∈ q ∧ f₂ m ∈ q at h, apply q.add_mem h.1 h.2, end /-- Given modules `M`, `M₂` over a commutative ring, together with submodules `p ⊆ M`, `q ⊆ M₂`, the set of maps $\{f ∈ Hom(M, M₂) | f(p) ⊆ q \}$ is a submodule of `Hom(M, M₂)`. -/ def compatible_maps : submodule R (N →ₗ[R] N₂) := { carrier := {fₗ | pₗ ≤ comap fₗ qₗ}, zero_mem' := by { change pₗ ≤ comap (0 : N →ₗ[R] N₂) qₗ, rw comap_zero, refine le_top, }, add_mem' := λ f₁ f₂ h₁ h₂, by { apply le_trans _ (inf_comap_le_comap_add qₗ f₁ f₂), rw le_inf_iff, exact ⟨h₁, h₂⟩, }, smul_mem' := λ c fₗ h, le_trans h (comap_le_comap_smul qₗ fₗ c), } end submodule namespace equiv variables [semiring R] [add_comm_monoid M] [module R M] [add_comm_monoid M₂] [module R M₂] /-- An equivalence whose underlying function is linear is a linear equivalence. -/ def to_linear_equiv (e : M ≃ M₂) (h : is_linear_map R (e : M → M₂)) : M ≃ₗ[R] M₂ := { .. e, .. h.mk' e} end equiv section fun_left variables (R M) [semiring R] [add_comm_monoid M] [module R M] variables {m n p : Type*} namespace linear_map /-- Given an `R`-module `M` and a function `m → n` between arbitrary types, construct a linear map `(n → M) →ₗ[R] (m → M)` -/ def fun_left (f : m → n) : (n → M) →ₗ[R] (m → M) := { to_fun := (∘ f), map_add' := λ _ _, rfl, map_smul' := λ _ _, rfl } @[simp] theorem fun_left_apply (f : m → n) (g : n → M) (i : m) : fun_left R M f g i = g (f i) := rfl @[simp] theorem fun_left_id (g : n → M) : fun_left R M _root_.id g = g := rfl theorem fun_left_comp (f₁ : n → p) (f₂ : m → n) : fun_left R M (f₁ ∘ f₂) = (fun_left R M f₂).comp (fun_left R M f₁) := rfl theorem fun_left_surjective_of_injective (f : m → n) (hf : injective f) : surjective (fun_left R M f) := begin classical, intro g, refine ⟨λ x, if h : ∃ y, f y = x then g h.some else 0, _⟩, { ext, dsimp only [fun_left_apply], split_ifs with w, { congr, exact hf w.some_spec, }, { simpa only [not_true, exists_apply_eq_apply] using w } }, end theorem fun_left_injective_of_surjective (f : m → n) (hf : surjective f) : injective (fun_left R M f) := begin obtain ⟨g, hg⟩ := hf.has_right_inverse, suffices : left_inverse (fun_left R M g) (fun_left R M f), { exact this.injective }, intro x, rw [←linear_map.comp_apply, ← fun_left_comp, hg.id, fun_left_id], end end linear_map namespace linear_equiv open _root_.linear_map /-- Given an `R`-module `M` and an equivalence `m ≃ n` between arbitrary types, construct a linear equivalence `(n → M) ≃ₗ[R] (m → M)` -/ def fun_congr_left (e : m ≃ n) : (n → M) ≃ₗ[R] (m → M) := linear_equiv.of_linear (fun_left R M e) (fun_left R M e.symm) (linear_map.ext $ λ x, funext $ λ i, by rw [id_apply, ← fun_left_comp, equiv.symm_comp_self, fun_left_id]) (linear_map.ext $ λ x, funext $ λ i, by rw [id_apply, ← fun_left_comp, equiv.self_comp_symm, fun_left_id]) @[simp] theorem fun_congr_left_apply (e : m ≃ n) (x : n → M) : fun_congr_left R M e x = fun_left R M e x := rfl @[simp] theorem fun_congr_left_id : fun_congr_left R M (equiv.refl n) = linear_equiv.refl R (n → M) := rfl @[simp] theorem fun_congr_left_comp (e₁ : m ≃ n) (e₂ : n ≃ p) : fun_congr_left R M (equiv.trans e₁ e₂) = linear_equiv.trans (fun_congr_left R M e₂) (fun_congr_left R M e₁) := rfl @[simp] lemma fun_congr_left_symm (e : m ≃ n) : (fun_congr_left R M e).symm = fun_congr_left R M e.symm := rfl end linear_equiv end fun_left
fa55ed51dd38ecc2ed893ba42b1afdd8cc59a80f
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/lean/run/expr1.lean
dc2200750843803ac5a36f3ef34b3fac2ee0c3dd
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
4,151
lean
import Init.Lean.Expr open Lean def tst1 : IO Unit := do let f := mkConst `f []; let a := mkConst `a []; let b := mkConst `b []; let t := mkAppN f #[a, b, b]; let as₁ := t.getAppArgs; let as₂ := t.getAppRevArgs; IO.println as₁; IO.println as₂; unless (as₁.reverse == as₂) $ throw $ IO.userError "failed"; pure () #eval tst1 def tst2 : IO Unit := do let l1 := mkLevelMax (mkLevelParam `a) (mkLevelParam `b); let l2 := mkLevelMax (mkLevelParam `b) (mkLevelParam `a); IO.println l1; IO.println l2; unless (Level.isEquiv l1 l2) $ throw $ IO.userError "not equiv"; pure () #eval tst2 def tst3 : IO Unit := do let f := mkConst `f []; let a := mkConst `a []; let b := mkConst `b []; let c := mkConst `c []; let t := mkAppN f #[a, b, c]; IO.println $ t.getArg! 0; IO.println $ t.getArg! 1; IO.println $ t.getArg! 2; pure () #eval tst3 def tst4 : IO Unit := do let f := mkConst `f []; let a := mkConst `a []; let b := mkConst `b []; let x0 := mkBVar 0; let x1 := mkBVar 1; let t1 := mkAppN f #[a, b]; let t2 := mkAppN f #[a, x0]; let t3 := mkLambda `x BinderInfo.default (mkSort levelZero) (mkAppN f #[a, x0]); let t4 := mkLambda `x BinderInfo.default (mkSort levelZero) (mkAppN f #[a, x1]); unless (!t1.hasLooseBVar 0) $ throw $ IO.userError "failed-1"; unless (t2.hasLooseBVar 0) $ throw $ IO.userError "failed-2"; unless (!t3.hasLooseBVar 0) $ throw $ IO.userError "failed-3"; unless (t4.hasLooseBVar 0) $ throw $ IO.userError "failed-4"; unless (!t4.hasLooseBVar 1) $ throw $ IO.userError "failed-5"; unless (!t2.hasLooseBVar 1) $ throw $ IO.userError "failed-6"; pure () #eval tst4 def tst5 : IO Unit := do let f := mkConst `f []; let a := mkConst `a []; let nat := mkConst `Nat []; let x0 := mkBVar 0; let x1 := mkBVar 1; let x2 := mkBVar 2; let t := mkLambda `x BinderInfo.default nat (mkApp f x0); IO.println t.etaExpanded?; unless (t.etaExpanded? == some f) $ throw $ IO.userError "failed-1"; let t := mkLambda `x BinderInfo.default nat (mkApp f x1); unless (t.etaExpanded? == none) $ throw $ IO.userError "failed-2"; let t := mkLambda `x BinderInfo.default nat (mkAppN f #[a, x0]); unless (t.etaExpanded? == some (mkApp f a)) $ throw $ IO.userError "failed-3"; let t := mkLambda `x BinderInfo.default nat (mkAppN f #[x0, x0]); unless (t.etaExpanded? == none) $ throw $ IO.userError "failed-4"; let t := mkLambda `x BinderInfo.default nat (mkLambda `y BinderInfo.default nat (mkApp f x0)); unless (t.etaExpanded? == none) $ throw $ IO.userError "failed-5"; let t := mkLambda `x BinderInfo.default nat (mkLambda `y BinderInfo.default nat (mkAppN f #[x1, x0])); IO.println t; unless (t.etaExpanded? == some f) $ throw $ IO.userError "failed-6"; let t := mkLambda `x BinderInfo.default nat (mkLambda `y BinderInfo.default nat (mkLambda `z BinderInfo.default nat (mkAppN f #[x2, x1, x0]))); IO.println t; unless (t.etaExpanded? == some f) $ throw $ IO.userError "failed-7"; let t := mkLambda `x BinderInfo.default nat (mkLambda `y BinderInfo.default nat (mkLambda `z BinderInfo.default nat (mkAppN f #[a, x2, x1, x0]))); IO.println t; unless (t.etaExpanded? == some (mkApp f a)) $ throw $ IO.userError "failed-8"; IO.println t.etaExpanded?; let t := mkApp f a; unless (t.etaExpanded? == some (mkApp f a)) $ throw $ IO.userError "failed-9"; pure () #eval tst5 def tst6 : IO Unit := do let x1 := mkBVar 0; let x2 := mkBVar 1; let t1 := mkApp2 (mkConst `f) x1 x2; let t2 := mkForall `x BinderInfo.default (mkConst `Nat) t1; IO.println (t1.liftLooseBVars 0 1); IO.println (t2.liftLooseBVars 0 1); let t3 := (t2.liftLooseBVars 0 1).lowerLooseBVars 1 1; IO.println $ t3; unless (t2 == t3) $ throw $ IO.userError "failed-1"; pure () #eval tst6 def tst7 : IO Unit := do let x := mkFVar `x; let y := mkFVar `y; let f := mkConst `f; let t := mkAppN f #[x, y, mkNatLit 2]; let t := t.abstract #[x, y]; let t := t.instantiateRev #[mkNatLit 0, mkNatLit 1]; IO.println t #eval tst7
c4690174acbf565d8e426d5e074e87332cedcdd8
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/hott/init/pathover.hlean
fc24d191bdbe502a811c10da9c0d6cd8f5733816
[ "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
12,981
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Basic theorems about pathovers -/ prelude import .path .equiv open equiv is_equiv equiv.ops function variables {A A' : Type} {B B' : A → Type} {B'' : A' → Type} {C : Π⦃a⦄, B a → Type} {a a₂ a₃ a₄ : A} {p p' : a = a₂} {p₂ : a₂ = a₃} {p₃ : a₃ = a₄} {p₁₃ : a = a₃} {b b' : B a} {b₂ b₂' : B a₂} {b₃ : B a₃} {b₄ : B a₄} {c : C b} {c₂ : C b₂} namespace eq inductive pathover.{l} (B : A → Type.{l}) (b : B a) : Π{a₂ : A}, a = a₂ → B a₂ → Type.{l} := idpatho : pathover B b (refl a) b notation b ` =[`:50 p:0 `] `:0 b₂:50 := pathover _ b p b₂ definition idpo [reducible] [constructor] : b =[refl a] b := pathover.idpatho b /- equivalences with equality using transport -/ definition pathover_of_tr_eq [unfold 5 8] (r : p ▸ b = b₂) : b =[p] b₂ := by cases p; cases r; constructor definition pathover_of_eq_tr [unfold 5 8] (r : b = p⁻¹ ▸ b₂) : b =[p] b₂ := by cases p; cases r; constructor definition tr_eq_of_pathover [unfold 8] (r : b =[p] b₂) : p ▸ b = b₂ := by cases r; reflexivity definition eq_tr_of_pathover [unfold 8] (r : b =[p] b₂) : b = p⁻¹ ▸ b₂ := by cases r; reflexivity definition pathover_equiv_tr_eq [constructor] (p : a = a₂) (b : B a) (b₂ : B a₂) : (b =[p] b₂) ≃ (p ▸ b = b₂) := begin fapply equiv.MK, { exact tr_eq_of_pathover}, { exact pathover_of_tr_eq}, { intro r, cases p, cases r, apply idp}, { intro r, cases r, apply idp}, end definition pathover_equiv_eq_tr [constructor] (p : a = a₂) (b : B a) (b₂ : B a₂) : (b =[p] b₂) ≃ (b = p⁻¹ ▸ b₂) := begin fapply equiv.MK, { exact eq_tr_of_pathover}, { exact pathover_of_eq_tr}, { intro r, cases p, cases r, apply idp}, { intro r, cases r, apply idp}, end definition pathover_tr [unfold 5] (p : a = a₂) (b : B a) : b =[p] p ▸ b := by cases p;constructor definition tr_pathover [unfold 5] (p : a = a₂) (b : B a₂) : p⁻¹ ▸ b =[p] b := by cases p;constructor definition concato [unfold 12] (r : b =[p] b₂) (r₂ : b₂ =[p₂] b₃) : b =[p ⬝ p₂] b₃ := pathover.rec_on r₂ r definition inverseo [unfold 8] (r : b =[p] b₂) : b₂ =[p⁻¹] b := pathover.rec_on r idpo definition apdo [unfold 6] (f : Πa, B a) (p : a = a₂) : f a =[p] f a₂ := eq.rec_on p idpo definition concato_eq [unfold 10] (r : b =[p] b₂) (q : b₂ = b₂') : b =[p] b₂' := eq.rec_on q r definition eq_concato [unfold 9] (q : b = b') (r : b' =[p] b₂) : b =[p] b₂ := by induction q;exact r definition change_path [unfold 9] (q : p = p') (r : b =[p] b₂) : b =[p'] b₂ := q ▸ r -- infix ` ⬝ ` := concato infix ` ⬝o `:72 := concato infix ` ⬝op `:73 := concato_eq infix ` ⬝po `:73 := eq_concato -- postfix `⁻¹` := inverseo postfix `⁻¹ᵒ`:(max+10) := inverseo definition pathover_cancel_right (q : b =[p ⬝ p₂] b₃) (r : b₃ =[p₂⁻¹] b₂) : b =[p] b₂ := change_path !con_inv_cancel_right (q ⬝o r) definition pathover_cancel_right' (q : b =[p₁₃ ⬝ p₂⁻¹] b₂) (r : b₂ =[p₂] b₃) : b =[p₁₃] b₃ := change_path !inv_con_cancel_right (q ⬝o r) definition pathover_cancel_left (q : b₂ =[p⁻¹] b) (r : b =[p ⬝ p₂] b₃) : b₂ =[p₂] b₃ := change_path !inv_con_cancel_left (q ⬝o r) definition pathover_cancel_left' (q : b =[p] b₂) (r : b₂ =[p⁻¹ ⬝ p₁₃] b₃) : b =[p₁₃] b₃ := change_path !con_inv_cancel_left (q ⬝o r) /- Some of the theorems analogous to theorems for = in init.path -/ definition cono_idpo (r : b =[p] b₂) : r ⬝o idpo =[con_idp p] r := pathover.rec_on r idpo definition idpo_cono (r : b =[p] b₂) : idpo ⬝o r =[idp_con p] r := pathover.rec_on r idpo definition cono.assoc' (r : b =[p] b₂) (r₂ : b₂ =[p₂] b₃) (r₃ : b₃ =[p₃] b₄) : r ⬝o (r₂ ⬝o r₃) =[!con.assoc'] (r ⬝o r₂) ⬝o r₃ := pathover.rec_on r₃ (pathover.rec_on r₂ (pathover.rec_on r idpo)) definition cono.assoc (r : b =[p] b₂) (r₂ : b₂ =[p₂] b₃) (r₃ : b₃ =[p₃] b₄) : (r ⬝o r₂) ⬝o r₃ =[!con.assoc] r ⬝o (r₂ ⬝o r₃) := pathover.rec_on r₃ (pathover.rec_on r₂ (pathover.rec_on r idpo)) definition cono.right_inv (r : b =[p] b₂) : r ⬝o r⁻¹ᵒ =[!con.right_inv] idpo := pathover.rec_on r idpo definition cono.left_inv (r : b =[p] b₂) : r⁻¹ᵒ ⬝o r =[!con.left_inv] idpo := pathover.rec_on r idpo definition eq_of_pathover {a' a₂' : A'} (q : a' =[p] a₂') : a' = a₂' := by cases q;reflexivity definition pathover_of_eq [unfold 5 8] {a' a₂' : A'} (q : a' = a₂') : a' =[p] a₂' := by cases p;cases q;constructor definition pathover_constant [constructor] (p : a = a₂) (a' a₂' : A') : a' =[p] a₂' ≃ a' = a₂' := begin fapply equiv.MK, { exact eq_of_pathover}, { exact pathover_of_eq}, { intro r, cases p, cases r, reflexivity}, { intro r, cases r, reflexivity}, end definition eq_of_pathover_idp [unfold 6] {b' : B a} (q : b =[idpath a] b') : b = b' := tr_eq_of_pathover q --should B be explicit in the next two definitions? definition pathover_idp_of_eq [unfold 6] {b' : B a} (q : b = b') : b =[idpath a] b' := pathover_of_tr_eq q definition pathover_idp [constructor] (b : B a) (b' : B a) : b =[idpath a] b' ≃ b = b' := equiv.MK eq_of_pathover_idp (pathover_idp_of_eq) (to_right_inv !pathover_equiv_tr_eq) (to_left_inv !pathover_equiv_tr_eq) -- definition pathover_idp (b : B a) (b' : B a) : b =[idpath a] b' ≃ b = b' := -- pathover_equiv_tr_eq idp b b' -- definition eq_of_pathover_idp [reducible] {b' : B a} (q : b =[idpath a] b') : b = b' := -- to_fun !pathover_idp q -- definition pathover_idp_of_eq [reducible] {b' : B a} (q : b = b') : b =[idpath a] b' := -- to_inv !pathover_idp q definition idp_rec_on [recursor] {P : Π⦃b₂ : B a⦄, b =[idpath a] b₂ → Type} {b₂ : B a} (r : b =[idpath a] b₂) (H : P idpo) : P r := have H2 : P (pathover_idp_of_eq (eq_of_pathover_idp r)), from eq.rec_on (eq_of_pathover_idp r) H, proof left_inv !pathover_idp r ▸ H2 qed definition rec_on_right [recursor] {P : Π⦃b₂ : B a₂⦄, b =[p] b₂ → Type} {b₂ : B a₂} (r : b =[p] b₂) (H : P !pathover_tr) : P r := by cases r; exact H definition rec_on_left [recursor] {P : Π⦃b : B a⦄, b =[p] b₂ → Type} {b : B a} (r : b =[p] b₂) (H : P !tr_pathover) : P r := by cases r; exact H --pathover with fibration B' ∘ f definition pathover_ap [unfold 10] (B' : A' → Type) (f : A → A') {p : a = a₂} {b : B' (f a)} {b₂ : B' (f a₂)} (q : b =[p] b₂) : b =[ap f p] b₂ := by cases q; constructor definition pathover_of_pathover_ap (B' : A' → Type) (f : A → A') {p : a = a₂} {b : B' (f a)} {b₂ : B' (f a₂)} (q : b =[ap f p] b₂) : b =[p] b₂ := by cases p; apply (idp_rec_on q); apply idpo definition pathover_compose [constructor] (B' : A' → Type) (f : A → A') (p : a = a₂) (b : B' (f a)) (b₂ : B' (f a₂)) : b =[p] b₂ ≃ b =[ap f p] b₂ := begin fapply equiv.MK, { exact pathover_ap B' f}, { exact pathover_of_pathover_ap B' f}, { intro q, cases p, esimp, apply (idp_rec_on q), apply idp}, { intro q, cases q, reflexivity}, end definition apdo_con (f : Πa, B a) (p : a = a₂) (q : a₂ = a₃) : apdo f (p ⬝ q) = apdo f p ⬝o apdo f q := by cases p; cases q; reflexivity definition apdo_inv (f : Πa, B a) (p : a = a₂) : apdo f p⁻¹ = (apdo f p)⁻¹ᵒ := by cases p; reflexivity definition apdo_eq_pathover_of_eq_ap (f : A → A') (p : a = a₂) : apdo f p = pathover_of_eq (ap f p) := eq.rec_on p idp definition pathover_of_pathover_tr (q : b =[p ⬝ p₂] p₂ ▸ b₂) : b =[p] b₂ := pathover_cancel_right q !pathover_tr⁻¹ᵒ definition pathover_tr_of_pathover (q : b =[p₁₃ ⬝ p₂⁻¹] b₂) : b =[p₁₃] p₂ ▸ b₂ := pathover_cancel_right' q !pathover_tr definition pathover_of_tr_pathover (q : p ▸ b =[p⁻¹ ⬝ p₁₃] b₃) : b =[p₁₃] b₃ := pathover_cancel_left' !pathover_tr q definition tr_pathover_of_pathover (q : b =[p ⬝ p₂] b₃) : p ▸ b =[p₂] b₃ := pathover_cancel_left !pathover_tr⁻¹ᵒ q definition pathover_tr_of_eq (q : b = b') : b =[p] p ▸ b' := by cases q;apply pathover_tr definition tr_pathover_of_eq (q : b₂ = b₂') : p⁻¹ ▸ b₂ =[p] b₂' := by cases q;apply tr_pathover variable (C) definition transporto (r : b =[p] b₂) (c : C b) : C b₂ := by induction r;exact c infix ` ▸o `:75 := transporto _ definition fn_tro_eq_tro_fn (C' : Π ⦃a : A⦄, B a → Type) (q : b =[p] b₂) (f : Π(b : B a), C b → C' b) (c : C b) : f b (q ▸o c) = (q ▸o (f b c)) := by induction q;reflexivity variable {C} definition apo {f : A → A'} (g : Πa, B a → B'' (f a)) (q : b =[p] b₂) : g a b =[p] g a₂ b₂ := by induction q; constructor definition apo011 [unfold 10] (f : Πa, B a → A') (Ha : a = a₂) (Hb : b =[Ha] b₂) : f a b = f a₂ b₂ := by cases Hb; reflexivity definition apo0111 (f : Πa b, C b → A') (Ha : a = a₂) (Hb : b =[Ha] b₂) (Hc : c =[apo011 C Ha Hb] c₂) : f a b c = f a₂ b₂ c₂ := by cases Hb; apply (idp_rec_on Hc); apply idp definition apod11 {f : Πb, C b} {g : Πb₂, C b₂} (r : f =[p] g) {b : B a} {b₂ : B a₂} (q : b =[p] b₂) : f b =[apo011 C p q] g b₂ := by cases r; apply (idp_rec_on q); constructor definition apdo10 {f : Πb, C b} {g : Πb₂, C b₂} (r : f =[p] g) (b : B a) : f b =[apo011 C p !pathover_tr] g (p ▸ b) := by cases r; constructor definition apo10 [unfold 9] {f : B a → B' a} {g : B a₂ → B' a₂} (r : f =[p] g) (b : B a) : f b =[p] g (p ▸ b) := by cases r; constructor definition apo10_constant_right [unfold 9] {f : B a → A'} {g : B a₂ → A'} (r : f =[p] g) (b : B a) : f b = g (p ▸ b) := by cases r; constructor definition apo10_constant_left [unfold 9] {f : A' → B a} {g : A' → B a₂} (r : f =[p] g) (a' : A') : f a' =[p] g a' := by cases r; constructor definition apo11 {f : B a → B' a} {g : B a₂ → B' a₂} (r : f =[p] g) (q : b =[p] b₂) : f b =[p] g b₂ := by induction q; exact apo10 r b definition apdo_compose1 (g : Πa, B a → B' a) (f : Πa, B a) (p : a = a₂) : apdo (g ∘' f) p = apo g (apdo f p) := by induction p; reflexivity definition apdo_compose2 (g : Πa', B'' a') (f : A → A') (p : a = a₂) : apdo (λa, g (f a)) p = pathover_of_pathover_ap B'' f (apdo g (ap f p)) := by induction p; reflexivity definition cono.right_inv_eq (q : b = b') : concato_eq (pathover_idp_of_eq q) q⁻¹ = (idpo : b =[refl a] b) := by induction q;constructor definition cono.right_inv_eq' (q : b = b') : eq_concato q (pathover_idp_of_eq q⁻¹) = (idpo : b =[refl a] b) := by induction q;constructor definition cono.left_inv_eq (q : b = b') : concato_eq (pathover_idp_of_eq q⁻¹) q = (idpo : b' =[refl a] b') := by induction q;constructor definition cono.left_inv_eq' (q : b = b') : eq_concato q⁻¹ (pathover_idp_of_eq q) = (idpo : b' =[refl a] b') := by induction q;constructor definition pathover_of_fn_pathover_fn (f : Π{a}, B a ≃ B' a) (r : f b =[p] f b₂) : b =[p] b₂ := (left_inv f b)⁻¹ ⬝po apo (λa, f⁻¹ᵉ) r ⬝op left_inv f b₂ definition change_path_of_pathover (s : p = p') (r : b =[p] b₂) (r' : b =[p'] b₂) (q : r =[s] r') : change_path s r = r' := by induction s; eapply idp_rec_on q; reflexivity definition pathover_of_change_path (s : p = p') (r : b =[p] b₂) (r' : b =[p'] b₂) (q : change_path s r = r') : r =[s] r' := by induction s; induction q; constructor definition pathover_pathover_path [constructor] (s : p = p') (r : b =[p] b₂) (r' : b =[p'] b₂) : (r =[s] r') ≃ change_path s r = r' := begin fapply equiv.MK, { apply change_path_of_pathover}, { apply pathover_of_change_path}, { intro q, induction s, induction q, reflexivity}, { intro q, induction s, eapply idp_rec_on q, reflexivity}, end definition inverseo2 [unfold 10] {r r' : b =[p] b₂} (s : r = r') : r⁻¹ᵒ = r'⁻¹ᵒ := by induction s; reflexivity definition concato2 [unfold 15 16] {r r' : b =[p] b₂} {r₂ r₂' : b₂ =[p₂] b₃} (s : r = r') (s₂ : r₂ = r₂') : r ⬝o r₂ = r' ⬝o r₂' := by induction s; induction s₂; reflexivity infixl ` ◾o `:75 := concato2 postfix [parsing_only] `⁻²ᵒ`:(max+10) := inverseo2 --this notation is abusive, should we use it? end eq
359b2e699eec2734013c0336e58e3fb5abd667ab
f3849be5d845a1cb97680f0bbbe03b85518312f0
/library/init/meta/smt/smt_tactic.lean
9c5128f5fd1b41a52d54a9d7531c40b003e92f07
[ "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
15,276
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.category import init.meta.simp_tactic import init.meta.smt.congruence_closure import init.meta.smt.ematch universe u run_cmd mk_simp_attr `pre_smt run_cmd mk_hinst_lemma_attr_set `ematch [] [`ematch_lhs] /-- Configuration for the smt tactic preprocessor. The preprocessor is applied whenever a new hypothesis is introduced. - simp_attr: is the attribute name for the simplification lemmas that are used during the preprocessing step. - max_steps: it is the maximum number of steps performed by the simplifier. - zeta: if tt, then zeta reduction (i.e., unfolding let-expressions) is used during preprocessing. -/ structure smt_pre_config := (simp_attr : name := `pre_smt) (max_steps : nat := 1000000) (zeta : bool := ff) /-- Configuration for the smt_state object. - em_attr: is the attribute name for the hinst_lemmas that are used for ematching -/ structure smt_config := (cc_cfg : cc_config := {}) (em_cfg : ematch_config := {}) (pre_cfg : smt_pre_config := {}) (em_attr : name := `ematch) meta def smt_config.set_classical (c : smt_config) (b : bool) : smt_config := {c with cc_cfg := { (c.cc_cfg) with em := b}} meta constant smt_goal : Type meta def smt_state := list smt_goal meta constant smt_state.mk : smt_config → tactic smt_state meta constant smt_state.to_format : smt_state → tactic_state → format /-- Return tt iff classical excluded middle was enabled at smt_state.mk -/ meta constant smt_state.classical : smt_state → bool meta def smt_tactic := state_t smt_state tactic meta instance : has_append smt_state := list.has_append meta instance : monad smt_tactic := state_t.monad _ _ /- We don't use the default state_t lift operation because only tactics that do not change hypotheses can be automatically lifted to smt_tactic. -/ meta constant tactic_to_smt_tactic (α : Type) : tactic α → smt_tactic α meta instance : monad.has_monad_lift tactic smt_tactic := ⟨tactic_to_smt_tactic⟩ meta instance (α : Type) : has_coe (tactic α) (smt_tactic α) := ⟨monad.monad_lift⟩ meta def smt_tactic_orelse {α : Type} (t₁ t₂ : smt_tactic α) : smt_tactic α := λ ss ts, result.cases_on (t₁ ss ts) result.success (λ e₁ ref₁ s', result.cases_on (t₂ ss ts) result.success result.exception) meta instance : monad_fail smt_tactic := { smt_tactic.monad with fail := λ α s, (tactic.fail (to_fmt s) : smt_tactic α) } meta instance : alternative smt_tactic := { smt_tactic.monad with failure := λ α, @tactic.failed α, orelse := @smt_tactic_orelse } namespace smt_tactic open tactic (transparency) meta constant intros : smt_tactic unit meta constant intron : nat → smt_tactic unit meta constant intro_lst : list name → smt_tactic unit /-- Try to close main goal by using equalities implied by the congruence closure module. -/ meta constant close : smt_tactic unit /-- Produce new facts using heuristic lemma instantiation based on E-matching. This tactic tries to match patterns from lemmas in the main goal with terms in the main goal. The set of lemmas is populated with theorems tagged with the attribute specified at smt_config.em_attr, and lemmas added using tactics such as `smt_tactic.add_lemmas`. The current set of lemmas can be retrieved using the tactic `smt_tactic.get_lemmas`. Remark: the given predicate is applied to every new instance. The instance is only added to the state if the predicate returns tt. -/ meta constant ematch_core : (expr → bool) → smt_tactic unit /-- Produce new facts using heuristic lemma instantiation based on E-matching. This tactic tries to match patterns from the given lemmas with terms in the main goal. -/ meta constant ematch_using : hinst_lemmas → smt_tactic unit meta constant mk_ematch_eqn_lemmas_for_core : transparency → name → smt_tactic hinst_lemmas meta constant to_cc_state : smt_tactic cc_state meta constant to_em_state : smt_tactic ematch_state meta constant get_config : smt_tactic smt_config /-- Preprocess the given term using the same simplifications rules used when we introduce a new hypothesis. The result is pair containing the resulting term and a proof that it is equal to the given one. -/ meta constant preprocess : expr → smt_tactic (expr × expr) meta constant get_lemmas : smt_tactic hinst_lemmas meta constant set_lemmas : hinst_lemmas → smt_tactic unit meta constant add_lemmas : hinst_lemmas → smt_tactic unit meta def add_ematch_lemma_core (md : transparency) (as_simp : bool) (e : expr) : smt_tactic unit := do h ← hinst_lemma.mk_core md e as_simp, add_lemmas (mk_hinst_singleton h) meta def add_ematch_lemma_from_decl_core (md : transparency) (as_simp : bool) (n : name) : smt_tactic unit := do h ← hinst_lemma.mk_from_decl_core md n as_simp, add_lemmas (mk_hinst_singleton h) meta def add_ematch_eqn_lemmas_for_core (md : transparency) (n : name) : smt_tactic unit := do hs ← mk_ematch_eqn_lemmas_for_core md n, add_lemmas hs meta def ematch : smt_tactic unit := ematch_core (λ _, tt) meta def failed {α} : smt_tactic α := tactic.failed meta def fail {α : Type} {β : Type u} [has_to_format β] (msg : β) : smt_tactic α := tactic.fail msg meta def try {α : Type} (t : smt_tactic α) : smt_tactic unit := λ ss ts, result.cases_on (t ss ts) (λ ⟨a, new_ss⟩, result.success ((), new_ss)) (λ e ref s', result.success ((), ss) ts) /- (repeat_at_most n t): repeat the given tactic at most n times or until t fails -/ meta def repeat_at_most : nat → smt_tactic unit → smt_tactic unit | 0 t := return () | (n+1) t := (do t, repeat_at_most n t) <|> return () /-- (repeat_exactly n t) : execute t n times -/ meta def repeat_exactly : nat → smt_tactic unit → smt_tactic unit | 0 t := return () | (n+1) t := do t, repeat_exactly n t meta def repeat : smt_tactic unit → smt_tactic unit := repeat_at_most 100000 meta def eblast : smt_tactic unit := repeat (ematch >> try close) open tactic protected meta def read : smt_tactic (smt_state × tactic_state) := do s₁ ← state_t.read, s₂ ← tactic.read, return (s₁, s₂) protected meta def write : smt_state × tactic_state → smt_tactic unit := λ ⟨ss, ts⟩ _ _, result.success ((), ss) ts private meta def mk_smt_goals_for (cfg : smt_config) : list expr → list smt_goal → list expr → tactic (list smt_goal × list expr) | [] sr tr := return (sr.reverse, tr.reverse) | (tg::tgs) sr tr := do tactic.set_goals [tg], [new_sg] ← smt_state.mk cfg | tactic.failed, [new_tg] ← get_goals | tactic.failed, mk_smt_goals_for tgs (new_sg::sr) (new_tg::tr) /- See slift -/ meta def slift_aux {α : Type} (t : tactic α) (cfg : smt_config) : smt_tactic α := λ ss, do _::sgs ← return ss | tactic.fail "slift tactic failed, there no smt goals to be solved", tg::tgs ← tactic.get_goals | tactic.failed, tactic.set_goals [tg], a ← t, new_tgs ← tactic.get_goals, (new_sgs, new_tgs) ← mk_smt_goals_for cfg new_tgs [] [], tactic.set_goals (new_tgs ++ tgs), return (a, new_sgs ++ sgs) /-- This lift operation will restart the SMT state. It is useful for using tactics that change the set of hypotheses. -/ meta def slift {α : Type} (t : tactic α) : smt_tactic α := get_config >>= slift_aux t meta def trace_state : smt_tactic unit := do (s₁, s₂) ← smt_tactic.read, trace (smt_state.to_format s₁ s₂) meta def trace {α : Type} [has_to_tactic_format α] (a : α) : smt_tactic unit := tactic.trace a meta def to_expr (q : pexpr) (allow_mvars := tt) : smt_tactic expr := tactic.to_expr q allow_mvars meta def classical : smt_tactic bool := do s ← state_t.read, return s.classical meta def num_goals : smt_tactic nat := λ ss, return (ss.length, ss) /- Low level primitives for managing set of goals -/ meta def get_goals : smt_tactic (list smt_goal × list expr) := do (g₁, _) ← smt_tactic.read, g₂ ← tactic.get_goals, return (g₁, g₂) meta def set_goals : list smt_goal → list expr → smt_tactic unit := λ g₁ g₂ ss, tactic.set_goals g₂ >> return ((), g₁) private meta def all_goals_core (tac : smt_tactic unit) : list smt_goal → list expr → list smt_goal → list expr → smt_tactic unit | [] ts acs act := set_goals acs (ts ++ act) | (s :: ss) [] acs act := fail "ill-formed smt_state" | (s :: ss) (t :: ts) acs act := do set_goals [s] [t], tac, (new_ss, new_ts) ← get_goals, all_goals_core ss ts (acs ++ new_ss) (act ++ new_ts) /- Apply the given tactic to all goals. -/ meta def all_goals (tac : smt_tactic unit) : smt_tactic unit := do (ss, ts) ← get_goals, all_goals_core tac ss ts [] [] /- LCF-style AND_THEN tactic. It applies tac1, and if succeed applies tac2 to each subgoal produced by tac1 -/ meta def seq (tac1 : smt_tactic unit) (tac2 : smt_tactic unit) : smt_tactic unit := do (s::ss, t::ts) ← get_goals, set_goals [s] [t], tac1, all_goals tac2, (new_ss, new_ts) ← get_goals, set_goals (new_ss ++ ss) (new_ts ++ ts) meta instance : has_andthen (smt_tactic unit) := ⟨seq⟩ meta def focus1 {α} (tac : smt_tactic α) : smt_tactic α := do (s::ss, t::ts) ← get_goals, match ss with | [] := tac | _ := do set_goals [s] [t], a ← tac, (ss', ts') ← get_goals, set_goals (ss' ++ ss) (ts' ++ ts), return a end meta def solve1 (tac : smt_tactic unit) : smt_tactic unit := do (ss, gs) ← get_goals, match ss, gs with | [], _ := fail "solve1 tactic failed, there isn't any goal left to focus" | _, [] := fail "solve1 tactic failed, there isn't any smt goal left to focus" | s::ss, g::gs := do set_goals [s] [g], tac, (ss', gs') ← get_goals, match ss', gs' with | [], [] := set_goals ss gs | _, _ := fail "solve1 tactic failed, focused goal has not been solved" end end meta def swap : smt_tactic unit := do (ss, ts) ← get_goals, match ss, ts with | (s₁ :: s₂ :: ss), (t₁ :: t₂ :: ts) := set_goals (s₂ :: s₁ :: ss) (t₂ :: t₁ :: ts) | _, _ := failed end /-- Add a new goal for t, and the hypothesis (h : t) in the current goal. -/ meta def assert (h : name) (t : expr) : smt_tactic unit := tactic.assert_core h t >> swap >> intros >> swap >> try close /-- Add the hypothesis (h : t) in the current goal if v has type t. -/ meta def assertv (h : name) (t : expr) (v : expr) : smt_tactic unit := tactic.assertv_core h t v >> intros >> return () /-- Add a new goal for t, and the hypothesis (h : t := ?M) in the current goal. -/ meta def define (h : name) (t : expr) : smt_tactic unit := tactic.define_core h t >> swap >> intros >> swap >> try close /-- Add the hypothesis (h : t := v) in the current goal if v has type t. -/ meta def definev (h : name) (t : expr) (v : expr) : smt_tactic unit := tactic.definev_core h t v >> intros >> return () /-- Add (h : t := pr) to the current goal -/ meta def pose (h : name) (t : option expr := none) (pr : expr) : smt_tactic unit := match t with | none := do t ← infer_type pr, definev h t pr | some t := definev h t pr end /-- Add (h : t) to the current goal, given a proof (pr : t) -/ meta def note (h : name) (t : option expr := none) (pr : expr) : smt_tactic unit := match t with | none := do t ← infer_type pr, assertv h t pr | some t := assertv h t pr end meta def destruct (e : expr) : smt_tactic unit := smt_tactic.seq (tactic.destruct e) smt_tactic.intros meta def by_cases (e : expr) : smt_tactic unit := do c ← classical, if c then destruct (expr.app (expr.const `classical.em []) e) else do dec_e ← (mk_app `decidable [e] <|> fail "by_cases smt_tactic failed, type is not a proposition"), inst ← (mk_instance dec_e <|> fail "by_cases smt_tactic failed, type of given expression is not decidable"), em ← mk_app `decidable.em [e, inst], destruct em meta def by_contradiction : smt_tactic unit := do t ← target, c ← classical, if t.is_false then skip else if c then do apply (expr.app (expr.const `classical.by_contradiction []) t), intros else do dec_t ← (mk_app `decidable [t] <|> fail "by_contradiction smt_tactic failed, target is not a proposition"), inst ← (mk_instance dec_t <|> fail "by_contradiction smt_tactic failed, target is not decidable"), a ← mk_mapp `decidable.by_contradiction [some t, some inst], apply a, intros /- Return a proof for e, if 'e' is a known fact in the main goal. -/ meta def proof_for (e : expr) : smt_tactic expr := do cc ← to_cc_state, cc.proof_for e /- Return a refutation for e (i.e., a proof for (not e)), if 'e' has been refuted in the main goal. -/ meta def refutation_for (e : expr) : smt_tactic expr := do cc ← to_cc_state, cc.refutation_for e meta def get_facts : smt_tactic (list expr) := do cc ← to_cc_state, return $ cc.eqc_of expr.mk_true meta def get_refuted_facts : smt_tactic (list expr) := do cc ← to_cc_state, return $ cc.eqc_of expr.mk_false meta def add_ematch_lemma : expr → smt_tactic unit := add_ematch_lemma_core reducible ff meta def add_ematch_lhs_lemma : expr → smt_tactic unit := add_ematch_lemma_core reducible tt meta def add_ematch_lemma_from_decl : name → smt_tactic unit := add_ematch_lemma_from_decl_core reducible ff meta def add_ematch_lhs_lemma_from_decl : name → smt_tactic unit := add_ematch_lemma_from_decl_core reducible ff meta def add_ematch_eqn_lemmas_for : name → smt_tactic unit := add_ematch_eqn_lemmas_for_core reducible meta def add_lemmas_from_facts_core : list expr → smt_tactic unit | [] := return () | (f::fs) := do try (is_prop f >> guard (f.is_pi && bnot (f.is_arrow)) >> proof_for f >>= add_ematch_lemma_core reducible ff), add_lemmas_from_facts_core fs meta def add_lemmas_from_facts : smt_tactic unit := get_facts >>= add_lemmas_from_facts_core meta def induction (e : expr) (ids : list name := []) (rec : option name := none) : smt_tactic unit := slift (tactic.induction e ids rec >> return ()) -- pass on the information? meta def when (c : Prop) [decidable c] (tac : smt_tactic unit) : smt_tactic unit := if c then tac else skip meta def when_tracing (n : name) (tac : smt_tactic unit) : smt_tactic unit := when (is_trace_enabled_for n = tt) tac end smt_tactic open smt_tactic meta def using_smt {α} (t : smt_tactic α) (cfg : smt_config := {}) : tactic α := do ss ← smt_state.mk cfg, (a, _) ← (do a ← t, repeat close, return a) ss, return a meta def using_smt_with {α} (cfg : smt_config) (t : smt_tactic α) : tactic α := using_smt t cfg
e6ca083381d57816cbb7c7c73e59500ebba9a9d0
976d2334b51721ddc405deb2e1754016d454286e
/src/caltech ma6/scratch.lean
f51bcb97a3c3f83d160513ca2bd1820e76be8655
[]
no_license
kbuzzard/lean-at-MC2020
11bb6ac9ec38a6caace9d5d9a1705d6794d9f477
1f7ca65a7ba5cc17eb49f525c02dc6b0e65d6543
refs/heads/master
1,668,496,422,317
1,594,131,838,000
1,594,131,838,000
277,877,735
0
0
null
1,594,142,006,000
1,594,142,005,000
null
UTF-8
Lean
false
false
1,049
lean
lemma p_imp_not_n : ∀ (G : pimpartial), p_position G → ¬ (n_position G) | (mk 0 M) := begin intro hpG, rw zero_unique, exact zero_not_n_position end | (mk (nat.succ n) M) := begin intro hpG, intro hnG, unfold p_position at hpG, unfold n_position at hnG, cases hnG with i hnG', have hpG' := hpG i, cases hpG' with j hpG'', have hnG'' := hnG' j, -- For recursion have h1 : (((mk (nat.succ n) M).move i).move j).subsequent ((mk (nat.succ n) M).move i), from subsequent.from_move ((mk (nat.succ n) M).move i) j, have h2 : ((mk (nat.succ n) M).move i).subsequent (mk (nat.succ n) M), from subsequent.from_move (mk (nat.succ n) M) i, have hwf : (((mk (nat.succ n) M).move i).move j).subsequent (mk n.succ M), from subsequent.from_trans (((mk n.succ M).move i).move j) ((mk n.succ M).move i) (mk n.succ M) h1 h2, exact p_imp_not_n (((mk (nat.succ n) M).move i).move j) hpG'' hnG'', end using_well_founded {dec_tac := tactic.assumption}
0d56a378d0e0fdfa7c2043029e595cc4f42103a1
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/order/category/NonemptyFinLinOrd.lean
8f385285cff46c56250f5a41b4c4d3d709e72b23
[ "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
2,053
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.fintype.sort import data.fin import order.category.LinearOrder /-! # Nonempty finite linear orders Nonempty finite linear orders form the index category for simplicial objects. -/ universe variables u v open category_theory set_option old_structure_cmd true /-- A typeclass for nonempty finite linear orders. -/ class nonempty_fin_lin_ord (α : Type*) extends fintype α, linear_order α, order_bot α, order_top α. instance punit.nonempty_fin_lin_ord : nonempty_fin_lin_ord punit := begin refine_struct { .. punit.linear_ordered_cancel_add_comm_monoid, .. punit.fintype }; { intros, exact punit.star <|> exact dec_trivial } end section open_locale classical instance fin.nonempty_fin_lin_ord (n : ℕ) : nonempty_fin_lin_ord (fin (n+1)) := { top := fin.last n, le_top := fin.le_last, bot := 0, bot_le := fin.zero_le, .. fin.fintype _, .. fin.linear_order } end instance ulift.nonempty_fin_lin_ord (α : Type u) [nonempty_fin_lin_ord α] : nonempty_fin_lin_ord (ulift.{v} α) := { top := ulift.up ⊤, bot := ulift.up ⊥, le_top := λ ⟨a⟩, show a ≤ ⊤, from le_top, bot_le := λ ⟨a⟩, show ⊥ ≤ a, from bot_le, .. linear_order.lift equiv.ulift (equiv.injective _), .. ulift.fintype _ } /-- The category of nonempty finite linear orders. -/ def NonemptyFinLinOrd := bundled nonempty_fin_lin_ord namespace NonemptyFinLinOrd instance : bundled_hom.parent_projection @nonempty_fin_lin_ord.to_linear_order := ⟨⟩ attribute [derive [has_coe_to_sort, large_category, concrete_category]] NonemptyFinLinOrd /-- Construct a bundled NonemptyFinLinOrd from the underlying type and typeclass. -/ def of (α : Type*) [nonempty_fin_lin_ord α] : NonemptyFinLinOrd := bundled.of α instance : inhabited NonemptyFinLinOrd := ⟨of punit⟩ instance (α : NonemptyFinLinOrd) : nonempty_fin_lin_ord α := α.str end NonemptyFinLinOrd
acd88d350ddd4ab3c4a347227c9d95d02ab40e90
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/topology/algebra/module.lean
e814b264105abc0f9df6c62e6779b2a158843bf3
[ "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
69,808
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jan-David Salchow, Sébastien Gouëzel, Jean Lo, Yury Kudryashov, Frédéric Dupuis, Heather Macbeth -/ import topology.algebra.ring import topology.algebra.mul_action import topology.uniform_space.uniform_embedding import algebra.algebra.basic import linear_algebra.projection import linear_algebra.pi /-! # Theory of topological modules and continuous linear maps. We use the class `has_continuous_smul` for topological (semi) modules and topological vector spaces. In this file we define continuous linear maps, as linear maps between topological modules which are continuous. The set of continuous linear maps between the topological `R`-modules `M` and `M₂` is denoted by `M →L[R] M₂`. Continuous linear equivalences are denoted by `M ≃L[R] M₂`. -/ open filter open_locale topological_space big_operators filter universes u v w u' section variables {R : Type*} {M : Type*} [ring R] [topological_space R] [topological_space M] [add_comm_group M] [module R M] lemma has_continuous_smul.of_nhds_zero [topological_ring R] [topological_add_group M] (hmul : tendsto (λ p : R × M, p.1 • p.2) (𝓝 0 ×ᶠ (𝓝 0)) (𝓝 0)) (hmulleft : ∀ m : M, tendsto (λ a : R, a • m) (𝓝 0) (𝓝 0)) (hmulright : ∀ a : R, tendsto (λ m : M, a • m) (𝓝 0) (𝓝 0)) : has_continuous_smul R M := ⟨begin rw continuous_iff_continuous_at, rintros ⟨a₀, m₀⟩, have key : ∀ p : R × M, p.1 • p.2 = a₀ • m₀ + ((p.1 - a₀) • m₀ + a₀ • (p.2 - m₀) + (p.1 - a₀) • (p.2 - m₀)), { rintro ⟨a, m⟩, simp [sub_smul, smul_sub], abel }, rw funext key, clear key, refine tendsto_const_nhds.add (tendsto.add (tendsto.add _ _) _), { rw [sub_self, zero_smul], apply (hmulleft m₀).comp, rw [show (λ p : R × M, p.1 - a₀) = (λ a, a - a₀) ∘ prod.fst, by {ext, refl }, nhds_prod_eq], have : tendsto (λ a, a - a₀) (𝓝 a₀) (𝓝 0), { rw ← sub_self a₀, exact tendsto_id.sub tendsto_const_nhds }, exact this.comp tendsto_fst }, { rw [sub_self, smul_zero], apply (hmulright a₀).comp, rw [show (λ p : R × M, p.2 - m₀) = (λ m, m - m₀) ∘ prod.snd, by {ext, refl }, nhds_prod_eq], have : tendsto (λ m, m - m₀) (𝓝 m₀) (𝓝 0), { rw ← sub_self m₀, exact tendsto_id.sub tendsto_const_nhds }, exact this.comp tendsto_snd }, { rw [sub_self, zero_smul, nhds_prod_eq, show (λ p : R × M, (p.fst - a₀) • (p.snd - m₀)) = (λ p : R × M, p.1 • p.2) ∘ (prod.map (λ a, a - a₀) (λ m, m - m₀)), by { ext, refl }], apply hmul.comp (tendsto.prod_map _ _); { rw ← sub_self , exact tendsto_id.sub tendsto_const_nhds } }, end⟩ end section variables {R : Type*} {M : Type*} [ring R] [topological_space R] [topological_space M] [add_comm_group M] [has_continuous_add M] [module R M] [has_continuous_smul R M] /-- If `M` is a topological module over `R` and `0` is a limit of invertible elements of `R`, then `⊤` is the only submodule of `M` with a nonempty interior. This is the case, e.g., if `R` is a nondiscrete normed field. -/ lemma submodule.eq_top_of_nonempty_interior' [ne_bot (𝓝[{x : R | is_unit x}] 0)] (s : submodule R M) (hs : (interior (s:set M)).nonempty) : s = ⊤ := begin rcases hs with ⟨y, hy⟩, refine (submodule.eq_top_iff'.2 $ λ x, _), rw [mem_interior_iff_mem_nhds] at hy, have : tendsto (λ c:R, y + c • x) (𝓝[{x : R | is_unit x}] 0) (𝓝 (y + (0:R) • x)), from tendsto_const_nhds.add ((tendsto_nhds_within_of_tendsto_nhds tendsto_id).smul tendsto_const_nhds), rw [zero_smul, add_zero] at this, rcases nonempty_of_mem (inter_mem (mem_map.1 (this hy)) self_mem_nhds_within) with ⟨_, hu, u, rfl⟩, have hy' : y ∈ ↑s := mem_of_mem_nhds hy, exact (s.smul_mem_iff' _).1 ((s.add_mem_iff_right hy').1 hu) end variables (R M) /-- Let `R` be a topological ring such that zero is not an isolated point (e.g., a nondiscrete normed field, see `normed_field.punctured_nhds_ne_bot`). Let `M` be a nontrivial module over `R` such that `c • x = 0` implies `c = 0 ∨ x = 0`. Then `M` has no isolated points. We formulate this using `ne_bot (𝓝[{x}ᶜ] x)`. This lemma is not an instance because Lean would need to find `[has_continuous_smul ?m_1 M]` with unknown `?m_1`. We register this as an instance for `R = ℝ` in `real.punctured_nhds_module_ne_bot`. One can also use `haveI := module.punctured_nhds_ne_bot R M` in a proof. -/ lemma module.punctured_nhds_ne_bot [nontrivial M] [ne_bot (𝓝[{0}ᶜ] (0 : R))] [no_zero_smul_divisors R M] (x : M) : ne_bot (𝓝[{x}ᶜ] x) := begin rcases exists_ne (0 : M) with ⟨y, hy⟩, suffices : tendsto (λ c : R, x + c • y) (𝓝[{0}ᶜ] 0) (𝓝[{x}ᶜ] x), from this.ne_bot, refine tendsto.inf _ (tendsto_principal_principal.2 $ _), { convert tendsto_const_nhds.add ((@tendsto_id R _).smul_const y), rw [zero_smul, add_zero] }, { intros c hc, simpa [hy] using hc } end end section closure variables {R : Type u} {M : Type v} [semiring R] [topological_space R] [topological_space M] [add_comm_monoid M] [module R M] [has_continuous_smul R M] lemma submodule.closure_smul_self_subset (s : submodule R M) : (λ p : R × M, p.1 • p.2) '' ((set.univ : set R).prod (closure (s : set M))) ⊆ closure (s : set M) := calc (λ p : R × M, p.1 • p.2) '' ((set.univ : set R).prod (closure (s : set M))) = (λ p : R × M, p.1 • p.2) '' (closure ((set.univ : set R).prod s)) : by simp [closure_prod_eq] ... ⊆ closure ((λ p : R × M, p.1 • p.2) '' ((set.univ : set R).prod s)) : image_closure_subset_closure_image continuous_smul ... = closure s : begin congr, ext x, refine ⟨_, λ hx, ⟨⟨1, x⟩, ⟨set.mem_univ _, hx⟩, one_smul R _⟩⟩, rintros ⟨⟨c, y⟩, ⟨hc, hy⟩, rfl⟩, simp [s.smul_mem c hy] end lemma submodule.closure_smul_self_eq (s : submodule R M) : (λ p : R × M, p.1 • p.2) '' ((set.univ : set R).prod (closure (s : set M))) = closure (s : set M) := set.subset.antisymm s.closure_smul_self_subset (λ x hx, ⟨⟨1, x⟩, ⟨set.mem_univ _, hx⟩, one_smul R _⟩) variables [has_continuous_add M] /-- The (topological-space) closure of a submodule of a topological `R`-module `M` is itself a submodule. -/ def submodule.topological_closure (s : submodule R M) : submodule R M := { carrier := closure (s : set M), smul_mem' := λ c x hx, s.closure_smul_self_subset ⟨⟨c, x⟩, ⟨set.mem_univ _, hx⟩, rfl⟩, ..s.to_add_submonoid.topological_closure } @[simp] lemma submodule.topological_closure_coe (s : submodule R M) : (s.topological_closure : set M) = closure (s : set M) := rfl instance submodule.topological_closure_has_continuous_smul (s : submodule R M) : has_continuous_smul R (s.topological_closure) := { continuous_smul := begin apply continuous_induced_rng, change continuous (λ p : R × s.topological_closure, p.1 • (p.2 : M)), continuity, end, ..s.to_add_submonoid.topological_closure_has_continuous_add } lemma submodule.submodule_topological_closure (s : submodule R M) : s ≤ s.topological_closure := subset_closure lemma submodule.is_closed_topological_closure (s : submodule R M) : is_closed (s.topological_closure : set M) := by convert is_closed_closure lemma submodule.topological_closure_minimal (s : submodule R M) {t : submodule R M} (h : s ≤ t) (ht : is_closed (t : set M)) : s.topological_closure ≤ t := closure_minimal h ht lemma submodule.topological_closure_mono {s : submodule R M} {t : submodule R M} (h : s ≤ t) : s.topological_closure ≤ t.topological_closure := s.topological_closure_minimal (h.trans t.submodule_topological_closure) t.is_closed_topological_closure end closure /-- Continuous linear maps between modules. We only put the type classes that are necessary for the definition, although in applications `M` and `M₂` will be topological modules over the topological ring `R`. -/ structure continuous_linear_map {R : Type*} {S : Type*} [semiring R] [semiring S] (σ : R →+* S) (M : Type*) [topological_space M] [add_comm_monoid M] (M₂ : Type*) [topological_space M₂] [add_comm_monoid M₂] [module R M] [module S M₂] extends M →ₛₗ[σ] M₂ := (cont : continuous to_fun . tactic.interactive.continuity') notation M ` →SL[`:25 σ `] ` M₂ := continuous_linear_map σ M M₂ notation M ` →L[`:25 R `] ` M₂ := continuous_linear_map (ring_hom.id R) M M₂ /-- Continuous linear equivalences between modules. We only put the type classes that are necessary for the definition, although in applications `M` and `M₂` will be topological modules over the topological ring `R`. -/ @[nolint has_inhabited_instance] structure continuous_linear_equiv {R : Type*} {S : Type*} [semiring R] [semiring S] (σ : R →+* S) {σ' : S →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] (M : Type*) [topological_space M] [add_comm_monoid M] (M₂ : Type*) [topological_space M₂] [add_comm_monoid M₂] [module R M] [module S M₂] extends M ≃ₛₗ[σ] M₂ := (continuous_to_fun : continuous to_fun . tactic.interactive.continuity') (continuous_inv_fun : continuous inv_fun . tactic.interactive.continuity') notation M ` ≃SL[`:50 σ `] ` M₂ := continuous_linear_equiv σ M M₂ notation M ` ≃L[`:50 R `] ` M₂ := continuous_linear_equiv (ring_hom.id R) M M₂ namespace continuous_linear_map section semiring /-! ### Properties that hold for non-necessarily commutative semirings. -/ variables {R₁ : Type*} [semiring R₁] {R₂ : Type*} [semiring R₂] {R₃ : Type*} [semiring R₃] {σ₁₂ : R₁ →+* R₂} {σ₂₃ : R₂ →+* R₃} {M₁ : Type*} [topological_space M₁] [add_comm_monoid M₁] {M₂ : Type*} [topological_space M₂] [add_comm_monoid M₂] {M₃ : Type*} [topological_space M₃] [add_comm_monoid M₃] {M₄ : Type*} [topological_space M₄] [add_comm_monoid M₄] [module R₁ M₁] [module R₂ M₂] [module R₃ M₃] /-- Coerce continuous linear maps to linear maps. -/ instance : has_coe (M₁ →SL[σ₁₂] M₂) (M₁ →ₛₗ[σ₁₂] M₂) := ⟨to_linear_map⟩ -- make the coercion the preferred form @[simp] lemma to_linear_map_eq_coe (f : M₁ →SL[σ₁₂] M₂) : f.to_linear_map = f := rfl /-- Coerce continuous linear maps to functions. -/ -- see Note [function coercion] instance to_fun : has_coe_to_fun $ M₁ →SL[σ₁₂] M₂ := ⟨λ _, M₁ → M₂, λ f, f⟩ @[simp] lemma coe_mk (f : M₁ →ₛₗ[σ₁₂] M₂) (h) : (mk f h : M₁ →ₛₗ[σ₁₂] M₂) = f := rfl @[simp] lemma coe_mk' (f : M₁ →ₛₗ[σ₁₂] M₂) (h) : (mk f h : M₁ → M₂) = f := rfl @[continuity] protected lemma continuous (f : M₁ →SL[σ₁₂] M₂) : continuous f := f.2 theorem coe_injective : function.injective (coe : (M₁ →SL[σ₁₂] M₂) → (M₁ →ₛₗ[σ₁₂] M₂)) := by { intros f g H, cases f, cases g, congr' } @[simp, norm_cast] lemma coe_inj {f g : M₁ →SL[σ₁₂] M₂} : (f : M₁ →ₛₗ[σ₁₂] M₂) = g ↔ f = g := coe_injective.eq_iff theorem coe_fn_injective : @function.injective (M₁ →SL[σ₁₂] M₂) (M₁ → M₂) coe_fn := linear_map.coe_injective.comp coe_injective @[ext] theorem ext {f g : M₁ →SL[σ₁₂] M₂} (h : ∀ x, f x = g x) : f = g := coe_fn_injective $ funext h theorem ext_iff {f g : M₁ →SL[σ₁₂] M₂} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, by rw h, by ext⟩ variables (f g : M₁ →SL[σ₁₂] M₂) (c : R₁) (h : M₂ →SL[σ₂₃] M₃) (x y z : M₁) -- make some straightforward lemmas available to `simp`. @[simp] lemma map_zero : f (0 : M₁) = 0 := (to_linear_map _).map_zero @[simp] lemma map_add : f (x + y) = f x + f y := (to_linear_map _).map_add _ _ @[simp] lemma map_smulₛₗ : f (c • x) = (σ₁₂ c) • f x := (to_linear_map _).map_smulₛₗ _ _ @[simp] lemma map_smul [module R₁ M₂] (f : M₁ →L[R₁] M₂)(c : R₁) (x : M₁) : f (c • x) = c • f x := by simp only [ring_hom.id_apply, map_smulₛₗ] @[simp, priority 900] lemma map_smul_of_tower {R S : Type*} [semiring S] [has_scalar R M₁] [module S M₁] [has_scalar R M₂] [module S M₂] [linear_map.compatible_smul M₁ M₂ R S] (f : M₁ →L[S] M₂) (c : R) (x : M₁) : f (c • x) = c • f x := linear_map.compatible_smul.map_smul f c x lemma map_sum {ι : Type*} (s : finset ι) (g : ι → M₁) : f (∑ i in s, g i) = ∑ i in s, f (g i) := f.to_linear_map.map_sum @[simp, norm_cast] lemma coe_coe : ((f : M₁ →ₛₗ[σ₁₂] M₂) : (M₁ → M₂)) = (f : M₁ → M₂) := rfl @[ext] theorem ext_ring [topological_space R₁] {f g : R₁ →L[R₁] M₁} (h : f 1 = g 1) : f = g := coe_inj.1 $ linear_map.ext_ring h theorem ext_ring_iff [topological_space R₁] {f g : R₁ →L[R₁] M₁} : f = g ↔ f 1 = g 1 := ⟨λ h, h ▸ rfl, ext_ring⟩ /-- If two continuous linear maps are equal on a set `s`, then they are equal on the closure of the `submodule.span` of this set. -/ lemma eq_on_closure_span [t2_space M₂] {s : set M₁} {f g : M₁ →SL[σ₁₂] M₂} (h : set.eq_on f g s) : set.eq_on f g (closure (submodule.span R₁ s : set M₁)) := (linear_map.eq_on_span' h).closure f.continuous g.continuous /-- If the submodule generated by a set `s` is dense in the ambient module, then two continuous linear maps equal on `s` are equal. -/ lemma ext_on [t2_space M₂] {s : set M₁} (hs : dense (submodule.span R₁ s : set M₁)) {f g : M₁ →SL[σ₁₂] M₂} (h : set.eq_on f g s) : f = g := ext $ λ x, eq_on_closure_span h (hs x) /-- Under a continuous linear map, the image of the `topological_closure` of a submodule is contained in the `topological_closure` of its image. -/ lemma _root_.submodule.topological_closure_map [ring_hom_surjective σ₁₂] [topological_space R₁] [topological_space R₂] [has_continuous_smul R₁ M₁] [has_continuous_add M₁] [has_continuous_smul R₂ M₂] [has_continuous_add M₂] (f : M₁ →SL[σ₁₂] M₂) (s : submodule R₁ M₁) : (s.topological_closure.map (f : M₁ →ₛₗ[σ₁₂] M₂)) ≤ (s.map (f : M₁ →ₛₗ[σ₁₂] M₂)).topological_closure := image_closure_subset_closure_image f.continuous /-- Under a dense continuous linear map, a submodule whose `topological_closure` is `⊤` is sent to another such submodule. That is, the image of a dense set under a map with dense range is dense. -/ lemma _root_.dense_range.topological_closure_map_submodule [ring_hom_surjective σ₁₂] [topological_space R₁] [topological_space R₂] [has_continuous_smul R₁ M₁] [has_continuous_add M₁] [has_continuous_smul R₂ M₂] [has_continuous_add M₂] {f : M₁ →SL[σ₁₂] M₂} (hf' : dense_range f) {s : submodule R₁ M₁} (hs : s.topological_closure = ⊤) : (s.map (f : M₁ →ₛₗ[σ₁₂] M₂)).topological_closure = ⊤ := begin rw set_like.ext'_iff at hs ⊢, simp only [submodule.topological_closure_coe, submodule.top_coe, ← dense_iff_closure_eq] at hs ⊢, exact hf'.dense_image f.continuous hs end /-- The continuous map that is constantly zero. -/ instance: has_zero (M₁ →SL[σ₁₂] M₂) := ⟨⟨0, continuous_zero⟩⟩ instance : inhabited (M₁ →SL[σ₁₂] M₂) := ⟨0⟩ @[simp] lemma default_def : default (M₁ →SL[σ₁₂] M₂) = 0 := rfl @[simp] lemma zero_apply : (0 : M₁ →SL[σ₁₂] M₂) x = 0 := rfl @[simp, norm_cast] lemma coe_zero : ((0 : M₁ →SL[σ₁₂] M₂) : M₁ →ₛₗ[σ₁₂] M₂) = 0 := rfl /- no simp attribute on the next line as simp does not always simplify `0 x` to `0` when `0` is the zero function, while it does for the zero continuous linear map, and this is the most important property we care about. -/ @[norm_cast] lemma coe_zero' : ((0 : M₁ →SL[σ₁₂] M₂) : M₁ → M₂) = 0 := rfl instance unique_of_left [subsingleton M₁] : unique (M₁ →SL[σ₁₂] M₂) := coe_injective.unique instance unique_of_right [subsingleton M₂] : unique (M₁ →SL[σ₁₂] M₂) := coe_injective.unique section variables (R₁ M₁) /-- the identity map as a continuous linear map. -/ def id : M₁ →L[R₁] M₁ := ⟨linear_map.id, continuous_id⟩ end instance : has_one (M₁ →L[R₁] M₁) := ⟨id R₁ M₁⟩ lemma one_def : (1 : M₁ →L[R₁] M₁) = id R₁ M₁ := rfl lemma id_apply : id R₁ M₁ x = x := rfl @[simp, norm_cast] lemma coe_id : (id R₁ M₁ : M₁ →ₗ[R₁] M₁) = linear_map.id := rfl @[simp, norm_cast] lemma coe_id' : (id R₁ M₁ : M₁ → M₁) = _root_.id := rfl @[simp, norm_cast] lemma coe_eq_id {f : M₁ →L[R₁] M₁} : (f : M₁ →ₗ[R₁] M₁) = linear_map.id ↔ f = id _ _ := by rw [← coe_id, coe_inj] @[simp] lemma one_apply : (1 : M₁ →L[R₁] M₁) x = x := rfl section add variables [has_continuous_add M₂] instance : has_add (M₁ →SL[σ₁₂] M₂) := ⟨λ f g, ⟨f + g, f.2.add g.2⟩⟩ lemma continuous_nsmul (n : ℕ) : continuous (λ (x : M₂), n • x) := begin induction n with n ih, { simp [continuous_const] }, { simp [nat.succ_eq_add_one, add_smul], exact ih.add continuous_id } end @[continuity] lemma continuous.nsmul {α : Type*} [topological_space α] {n : ℕ} {f : α → M₂} (hf : continuous f) : continuous (λ (x : α), n • (f x)) := (continuous_nsmul n).comp hf @[simp] lemma add_apply : (f + g) x = f x + g x := rfl @[simp, norm_cast] lemma coe_add : (((f + g) : M₁ →SL[σ₁₂] M₂) : M₁ →ₛₗ[σ₁₂] M₂) = f + g := rfl @[norm_cast] lemma coe_add' : (((f + g) : M₁ →SL[σ₁₂] M₂) : M₁ → M₂) = (f : M₁ → M₂) + g := rfl instance : add_comm_monoid (M₁ →SL[σ₁₂] M₂) := { zero := (0 : M₁ →SL[σ₁₂] M₂), add := (+), zero_add := by intros; ext; apply_rules [zero_add, add_assoc, add_zero, add_left_neg, add_comm], add_zero := by intros; ext; apply_rules [zero_add, add_assoc, add_zero, add_left_neg, add_comm], add_comm := by intros; ext; apply_rules [zero_add, add_assoc, add_zero, add_left_neg, add_comm], add_assoc := by intros; ext; apply_rules [zero_add, add_assoc, add_zero, add_left_neg, add_comm], nsmul := λ n f, { to_fun := λ x, n • (f x), map_add' := by simp, map_smul' := by simp [smul_comm n] }, nsmul_zero' := λ f, by { ext, simp }, nsmul_succ' := λ n f, by { ext, simp [nat.succ_eq_one_add, add_smul] } } @[simp, norm_cast] lemma coe_sum {ι : Type*} (t : finset ι) (f : ι → M₁ →SL[σ₁₂] M₂) : ↑(∑ d in t, f d) = (∑ d in t, f d : M₁ →ₛₗ[σ₁₂] M₂) := (add_monoid_hom.mk (coe : (M₁ →SL[σ₁₂] M₂) → (M₁ →ₛₗ[σ₁₂] M₂)) rfl (λ _ _, rfl)).map_sum _ _ @[simp, norm_cast] lemma coe_sum' {ι : Type*} (t : finset ι) (f : ι → M₁ →SL[σ₁₂] M₂) : ⇑(∑ d in t, f d) = ∑ d in t, f d := by simp only [← coe_coe, coe_sum, linear_map.coe_fn_sum] lemma sum_apply {ι : Type*} (t : finset ι) (f : ι → M₁ →SL[σ₁₂] M₂) (b : M₁) : (∑ d in t, f d) b = ∑ d in t, f d b := by simp only [coe_sum', finset.sum_apply] end add variables {σ₁₃ : R₁ →+* R₃} [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] /-- Composition of bounded linear maps. -/ def comp (g : M₂ →SL[σ₂₃] M₃) (f : M₁ →SL[σ₁₂] M₂) : M₁ →SL[σ₁₃] M₃ := ⟨(g : M₂ →ₛₗ[σ₂₃] M₃).comp ↑f, g.2.comp f.2⟩ infixr ` ∘L `:80 := @continuous_linear_map.comp _ _ _ _ _ _ (ring_hom.id _) (ring_hom.id _) _ _ _ _ _ _ _ _ _ _ _ _ (ring_hom.id _) ring_hom_comp_triple.ids @[simp, norm_cast] lemma coe_comp : ((h.comp f) : (M₁ →ₛₗ[σ₁₃] M₃)) = (h : M₂ →ₛₗ[σ₂₃] M₃).comp (f : M₁ →ₛₗ[σ₁₂] M₂) := rfl include σ₁₃ @[simp, norm_cast] lemma coe_comp' : ((h.comp f) : (M₁ → M₃)) = (h : M₂ → M₃) ∘ f := rfl lemma comp_apply (g : M₂ →SL[σ₂₃] M₃) (f : M₁ →SL[σ₁₂] M₂) (x : M₁) : (g.comp f) x = g (f x) := rfl omit σ₁₃ @[simp] theorem comp_id : f.comp (id R₁ M₁) = f := ext $ λ x, rfl @[simp] theorem id_comp : (id R₂ M₂).comp f = f := ext $ λ x, rfl include σ₁₃ @[simp] theorem comp_zero (g : M₂ →SL[σ₂₃] M₃) : g.comp (0 : M₁ →SL[σ₁₂] M₂) = 0 := by { ext, simp } @[simp] theorem zero_comp : (0 : M₂ →SL[σ₂₃] M₃).comp f = 0 := by { ext, simp } @[simp] lemma comp_add [has_continuous_add M₂] [has_continuous_add M₃] (g : M₂ →SL[σ₂₃] M₃) (f₁ f₂ : M₁ →SL[σ₁₂] M₂) : g.comp (f₁ + f₂) = g.comp f₁ + g.comp f₂ := by { ext, simp } @[simp] lemma add_comp [has_continuous_add M₃] (g₁ g₂ : M₂ →SL[σ₂₃] M₃) (f : M₁ →SL[σ₁₂] M₂) : (g₁ + g₂).comp f = g₁.comp f + g₂.comp f := by { ext, simp } omit σ₁₃ theorem comp_assoc {R₄ : Type*} [semiring R₄] [module R₄ M₄] {σ₁₄ : R₁ →+* R₄} {σ₂₄ : R₂ →+* R₄} {σ₃₄ : R₃ →+* R₄} [ring_hom_comp_triple σ₁₃ σ₃₄ σ₁₄] [ring_hom_comp_triple σ₂₃ σ₃₄ σ₂₄] [ring_hom_comp_triple σ₁₂ σ₂₄ σ₁₄] (h : M₃ →SL[σ₃₄] M₄) (g : M₂ →SL[σ₂₃] M₃) (f : M₁ →SL[σ₁₂] M₂) : (h.comp g).comp f = h.comp (g.comp f) := rfl instance : has_mul (M₁ →L[R₁] M₁) := ⟨comp⟩ lemma mul_def (f g : M₁ →L[R₁] M₁) : f * g = f.comp g := rfl @[simp] lemma coe_mul (f g : M₁ →L[R₁] M₁) : ⇑(f * g) = f ∘ g := rfl lemma mul_apply (f g : M₁ →L[R₁] M₁) (x : M₁) : (f * g) x = f (g x) := rfl /-- The cartesian product of two bounded linear maps, as a bounded linear map. -/ protected def prod [module R₁ M₂] [module R₁ M₃] (f₁ : M₁ →L[R₁] M₂) (f₂ : M₁ →L[R₁] M₃) : M₁ →L[R₁] (M₂ × M₃) := ⟨(f₁ : M₁ →ₗ[R₁] M₂).prod f₂, f₁.2.prod_mk f₂.2⟩ @[simp, norm_cast] lemma coe_prod [module R₁ M₂] [module R₁ M₃] (f₁ : M₁ →L[R₁] M₂) (f₂ : M₁ →L[R₁] M₃) : (f₁.prod f₂ : M₁ →ₗ[R₁] M₂ × M₃) = linear_map.prod f₁ f₂ := rfl @[simp, norm_cast] lemma prod_apply [module R₁ M₂] [module R₁ M₃] (f₁ : M₁ →L[R₁] M₂) (f₂ : M₁ →L[R₁] M₃) (x : M₁) : f₁.prod f₂ x = (f₁ x, f₂ x) := rfl section variables (R₁ M₁ M₂) /-- The left injection into a product is a continuous linear map. -/ def inl [module R₁ M₂] : M₁ →L[R₁] M₁ × M₂ := (id R₁ M₁).prod 0 /-- The right injection into a product is a continuous linear map. -/ def inr [module R₁ M₂] : M₂ →L[R₁] M₁ × M₂ := (0 : M₂ →L[R₁] M₁).prod (id R₁ M₂) end @[simp] lemma inl_apply [module R₁ M₂] (x : M₁) : inl R₁ M₁ M₂ x = (x, 0) := rfl @[simp] lemma inr_apply [module R₁ M₂] (x : M₂) : inr R₁ M₁ M₂ x = (0, x) := rfl @[simp, norm_cast] lemma coe_inl [module R₁ M₂] : (inl R₁ M₁ M₂ : M₁ →ₗ[R₁] M₁ × M₂) = linear_map.inl R₁ M₁ M₂ := rfl @[simp, norm_cast] lemma coe_inr [module R₁ M₂] : (inr R₁ M₁ M₂ : M₂ →ₗ[R₁] M₁ × M₂) = linear_map.inr R₁ M₁ M₂ := rfl /-- Kernel of a continuous linear map. -/ def ker (f : M₁ →SL[σ₁₂] M₂) : submodule R₁ M₁ := (f : M₁ →ₛₗ[σ₁₂] M₂).ker @[norm_cast] lemma ker_coe : (f : M₁ →ₛₗ[σ₁₂] M₂).ker = f.ker := rfl @[simp] lemma mem_ker {f : M₁ →SL[σ₁₂] M₂} {x} : x ∈ f.ker ↔ f x = 0 := linear_map.mem_ker lemma is_closed_ker [t1_space M₂] : is_closed (f.ker : set M₁) := continuous_iff_is_closed.1 f.cont _ is_closed_singleton @[simp] lemma apply_ker (x : f.ker) : f x = 0 := mem_ker.1 x.2 lemma is_complete_ker {M' : Type*} [uniform_space M'] [complete_space M'] [add_comm_monoid M'] [module R₁ M'] [t1_space M₂] (f : M' →SL[σ₁₂] M₂) : is_complete (f.ker : set M') := f.is_closed_ker.is_complete instance complete_space_ker {M' : Type*} [uniform_space M'] [complete_space M'] [add_comm_monoid M'] [module R₁ M'] [t1_space M₂] (f : M' →SL[σ₁₂] M₂) : complete_space f.ker := f.is_closed_ker.complete_space_coe @[simp] lemma ker_prod [module R₁ M₂] [module R₁ M₃] (f : M₁ →L[R₁] M₂) (g : M₁ →L[R₁] M₃) : ker (f.prod g) = ker f ⊓ ker g := linear_map.ker_prod f g /-- Range of a continuous linear map. -/ def range [ring_hom_surjective σ₁₂] (f : M₁ →SL[σ₁₂] M₂) : submodule R₂ M₂ := (f : M₁ →ₛₗ[σ₁₂] M₂).range lemma range_coe [ring_hom_surjective σ₁₂] : (f.range : set M₂) = set.range f := linear_map.range_coe _ lemma mem_range [ring_hom_surjective σ₁₂] {f : M₁ →SL[σ₁₂] M₂} {y} : y ∈ f.range ↔ ∃ x, f x = y := linear_map.mem_range lemma mem_range_self [ring_hom_surjective σ₁₂] (f : M₁ →SL[σ₁₂] M₂) (x : M₁) : f x ∈ f.range := mem_range.2 ⟨x, rfl⟩ lemma range_prod_le [module R₁ M₂] [module R₁ M₃] (f : M₁ →L[R₁] M₂) (g : M₁ →L[R₁] M₃) : range (f.prod g) ≤ (range f).prod (range g) := (f : M₁ →ₗ[R₁] M₂).range_prod_le g /-- Restrict codomain of a continuous linear map. -/ def cod_restrict (f : M₁ →SL[σ₁₂] M₂) (p : submodule R₂ M₂) (h : ∀ x, f x ∈ p) : M₁ →SL[σ₁₂] p := { cont := continuous_subtype_mk h f.continuous, to_linear_map := (f : M₁ →ₛₗ[σ₁₂] M₂).cod_restrict p h} @[norm_cast] lemma coe_cod_restrict (f : M₁ →SL[σ₁₂] M₂) (p : submodule R₂ M₂) (h : ∀ x, f x ∈ p) : (f.cod_restrict p h : M₁ →ₛₗ[σ₁₂] p) = (f : M₁ →ₛₗ[σ₁₂] M₂).cod_restrict p h := rfl @[simp] lemma coe_cod_restrict_apply (f : M₁ →SL[σ₁₂] M₂) (p : submodule R₂ M₂) (h : ∀ x, f x ∈ p) (x) : (f.cod_restrict p h x : M₂) = f x := rfl @[simp] lemma ker_cod_restrict (f : M₁ →SL[σ₁₂] M₂) (p : submodule R₂ M₂) (h : ∀ x, f x ∈ p) : ker (f.cod_restrict p h) = ker f := (f : M₁ →ₛₗ[σ₁₂] M₂).ker_cod_restrict p h /-- Embedding of a submodule into the ambient space as a continuous linear map. -/ def subtype_val (p : submodule R₁ M₁) : p →L[R₁] M₁ := { cont := continuous_subtype_val, to_linear_map := p.subtype } @[simp, norm_cast] lemma coe_subtype_val (p : submodule R₁ M₁) : (subtype_val p : p →ₗ[R₁] M₁) = p.subtype := rfl @[simp, norm_cast] lemma subtype_val_apply (p : submodule R₁ M₁) (x : p) : (subtype_val p : p → M₁) x = x := rfl variables (R₁ M₁ M₂) /-- `prod.fst` as a `continuous_linear_map`. -/ def fst [module R₁ M₂] : M₁ × M₂ →L[R₁] M₁ := { cont := continuous_fst, to_linear_map := linear_map.fst R₁ M₁ M₂ } /-- `prod.snd` as a `continuous_linear_map`. -/ def snd [module R₁ M₂] : M₁ × M₂ →L[R₁] M₂ := { cont := continuous_snd, to_linear_map := linear_map.snd R₁ M₁ M₂ } variables {R₁ M₁ M₂} @[simp, norm_cast] lemma coe_fst [module R₁ M₂] : (fst R₁ M₁ M₂ : M₁ × M₂ →ₗ[R₁] M₁) = linear_map.fst R₁ M₁ M₂ := rfl @[simp, norm_cast] lemma coe_fst' [module R₁ M₂] : (fst R₁ M₁ M₂ : M₁ × M₂ → M₁) = prod.fst := rfl @[simp, norm_cast] lemma coe_snd [module R₁ M₂] : (snd R₁ M₁ M₂ : M₁ × M₂ →ₗ[R₁] M₂) = linear_map.snd R₁ M₁ M₂ := rfl @[simp, norm_cast] lemma coe_snd' [module R₁ M₂] : (snd R₁ M₁ M₂ : M₁ × M₂ → M₂) = prod.snd := rfl @[simp] lemma fst_prod_snd [module R₁ M₂] : (fst R₁ M₁ M₂).prod (snd R₁ M₁ M₂) = id R₁ (M₁ × M₂) := ext $ λ ⟨x, y⟩, rfl @[simp] lemma fst_comp_prod [module R₁ M₂] [module R₁ M₃] (f : M₁ →L[R₁] M₂) (g : M₁ →L[R₁] M₃) : (fst R₁ M₂ M₃).comp (f.prod g) = f := ext $ λ x, rfl @[simp] lemma snd_comp_prod [module R₁ M₂] [module R₁ M₃] (f : M₁ →L[R₁] M₂) (g : M₁ →L[R₁] M₃) : (snd R₁ M₂ M₃).comp (f.prod g) = g := ext $ λ x, rfl /-- `prod.map` of two continuous linear maps. -/ def prod_map [module R₁ M₂] [module R₁ M₃] [module R₁ M₄] (f₁ : M₁ →L[R₁] M₂) (f₂ : M₃ →L[R₁] M₄) : (M₁ × M₃) →L[R₁] (M₂ × M₄) := (f₁.comp (fst R₁ M₁ M₃)).prod (f₂.comp (snd R₁ M₁ M₃)) @[simp, norm_cast] lemma coe_prod_map [module R₁ M₂] [module R₁ M₃] [module R₁ M₄] (f₁ : M₁ →L[R₁] M₂) (f₂ : M₃ →L[R₁] M₄) : (f₁.prod_map f₂ : (M₁ × M₃) →ₗ[R₁] (M₂ × M₄)) = ((f₁ : M₁ →ₗ[R₁] M₂).prod_map (f₂ : M₃ →ₗ[R₁] M₄)) := rfl @[simp, norm_cast] lemma coe_prod_map' [module R₁ M₂] [module R₁ M₃] [module R₁ M₄] (f₁ : M₁ →L[R₁] M₂) (f₂ : M₃ →L[R₁] M₄) : ⇑(f₁.prod_map f₂) = prod.map f₁ f₂ := rfl /-- The continuous linear map given by `(x, y) ↦ f₁ x + f₂ y`. -/ def coprod [module R₁ M₂] [module R₁ M₃] [has_continuous_add M₃] (f₁ : M₁ →L[R₁] M₃) (f₂ : M₂ →L[R₁] M₃) : (M₁ × M₂) →L[R₁] M₃ := ⟨linear_map.coprod f₁ f₂, (f₁.cont.comp continuous_fst).add (f₂.cont.comp continuous_snd)⟩ @[norm_cast, simp] lemma coe_coprod [module R₁ M₂] [module R₁ M₃] [has_continuous_add M₃] (f₁ : M₁ →L[R₁] M₃) (f₂ : M₂ →L[R₁] M₃) : (f₁.coprod f₂ : (M₁ × M₂) →ₗ[R₁] M₃) = linear_map.coprod f₁ f₂ := rfl @[simp] lemma coprod_apply [module R₁ M₂] [module R₁ M₃] [has_continuous_add M₃] (f₁ : M₁ →L[R₁] M₃) (f₂ : M₂ →L[R₁] M₃) (x) : f₁.coprod f₂ x = f₁ x.1 + f₂ x.2 := rfl lemma range_coprod [module R₁ M₂] [module R₁ M₃] [has_continuous_add M₃] (f₁ : M₁ →L[R₁] M₃) (f₂ : M₂ →L[R₁] M₃) : (f₁.coprod f₂).range = f₁.range ⊔ f₂.range := linear_map.range_coprod _ _ section variables {R S : Type*} [semiring R] [semiring S] [module R M₁] [module R M₂] [module R S] [module S M₂] [is_scalar_tower R S M₂] [topological_space S] [has_continuous_smul S M₂] /-- The linear map `λ x, c x • f`. Associates to a scalar-valued linear map and an element of `M₂` the `M₂`-valued linear map obtained by multiplying the two (a.k.a. tensoring by `M₂`). See also `continuous_linear_map.smul_rightₗ` and `continuous_linear_map.smul_rightL`. -/ def smul_right (c : M₁ →L[R] S) (f : M₂) : M₁ →L[R] M₂ := { cont := c.2.smul continuous_const, ..c.to_linear_map.smul_right f } @[simp] lemma smul_right_apply {c : M₁ →L[R] S} {f : M₂} {x : M₁} : (smul_right c f : M₁ → M₂) x = c x • f := rfl end variables [module R₁ M₂] [topological_space R₁] [has_continuous_smul R₁ M₂] @[simp] lemma smul_right_one_one (c : R₁ →L[R₁] M₂) : smul_right (1 : R₁ →L[R₁] R₁) (c 1) = c := by ext; simp [← continuous_linear_map.map_smul_of_tower] @[simp] lemma smul_right_one_eq_iff {f f' : M₂} : smul_right (1 : R₁ →L[R₁] R₁) f = smul_right (1 : R₁ →L[R₁] R₁) f' ↔ f = f' := by simp only [ext_ring_iff, smul_right_apply, one_apply, one_smul] lemma smul_right_comp [has_continuous_mul R₁] {x : M₂} {c : R₁} : (smul_right (1 : R₁ →L[R₁] R₁) x).comp (smul_right (1 : R₁ →L[R₁] R₁) c) = smul_right (1 : R₁ →L[R₁] R₁) (c • x) := by { ext, simp [mul_smul] } end semiring section pi variables {R : Type*} [semiring R] {M : Type*} [topological_space M] [add_comm_monoid M] [module R M] {M₂ : Type*} [topological_space M₂] [add_comm_monoid M₂] [module R M₂] {ι : Type*} {φ : ι → Type*} [∀i, topological_space (φ i)] [∀i, add_comm_monoid (φ i)] [∀i, module R (φ i)] /-- `pi` construction for continuous linear functions. From a family of continuous linear functions it produces a continuous linear function into a family of topological modules. -/ def pi (f : Πi, M →L[R] φ i) : M →L[R] (Πi, φ i) := ⟨linear_map.pi (λ i, f i), continuous_pi (λ i, (f i).continuous)⟩ @[simp] lemma coe_pi' (f : Π i, M →L[R] φ i) : ⇑(pi f) = λ c i, f i c := rfl @[simp] lemma coe_pi (f : Π i, M →L[R] φ i) : (pi f : M →ₗ[R] Π i, φ i) = linear_map.pi (λ i, f i) := rfl lemma pi_apply (f : Πi, M →L[R] φ i) (c : M) (i : ι) : pi f c i = f i c := rfl lemma pi_eq_zero (f : Πi, M →L[R] φ i) : pi f = 0 ↔ (∀i, f i = 0) := by { simp only [ext_iff, pi_apply, function.funext_iff], exact forall_swap } lemma pi_zero : pi (λi, 0 : Πi, M →L[R] φ i) = 0 := ext $ λ _, rfl lemma pi_comp (f : Πi, M →L[R] φ i) (g : M₂ →L[R] M) : (pi f).comp g = pi (λi, (f i).comp g) := rfl /-- The projections from a family of topological modules are continuous linear maps. -/ def proj (i : ι) : (Πi, φ i) →L[R] φ i := ⟨linear_map.proj i, continuous_apply _⟩ @[simp] lemma proj_apply (i : ι) (b : Πi, φ i) : (proj i : (Πi, φ i) →L[R] φ i) b = b i := rfl lemma proj_pi (f : Πi, M₂ →L[R] φ i) (i : ι) : (proj i).comp (pi f) = f i := ext $ assume c, rfl lemma infi_ker_proj : (⨅i, ker (proj i) : submodule R (Πi, φ i)) = ⊥ := linear_map.infi_ker_proj variables (R φ) /-- If `I` and `J` are complementary index sets, the product of the kernels of the `J`th projections of `φ` is linearly equivalent to the product over `I`. -/ def infi_ker_proj_equiv {I J : set ι} [decidable_pred (λi, i ∈ I)] (hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) : (⨅i ∈ J, ker (proj i) : submodule R (Πi, φ i)) ≃L[R] (Πi:I, φ i) := ⟨ linear_map.infi_ker_proj_equiv R φ hd hu, continuous_pi (λ i, begin have := @continuous_subtype_coe _ _ (λ x, x ∈ (⨅i ∈ J, ker (proj i) : submodule R (Πi, φ i))), have := continuous.comp (by exact continuous_apply i) this, exact this end), continuous_subtype_mk _ (continuous_pi (λ i, begin dsimp, split_ifs; [apply continuous_apply, exact continuous_zero] end)) ⟩ end pi section ring variables {R : Type*} [ring R] {R₂ : Type*} [ring R₂] {M : Type*} [topological_space M] [add_comm_group M] {M₂ : Type*} [topological_space M₂] [add_comm_group M₂] {M₃ : Type*} [topological_space M₃] [add_comm_group M₃] {M₄ : Type*} [topological_space M₄] [add_comm_group M₄] [module R M] [module R₂ M₂] {σ₁₂ : R →+* R₂} section variables (f g : M →SL[σ₁₂] M₂) (x y : M) @[simp] lemma map_neg : f (-x) = - (f x) := (to_linear_map _).map_neg _ @[simp] lemma map_sub : f (x - y) = f x - f y := (to_linear_map _).map_sub _ _ @[simp] lemma sub_apply' (x : M) : ((f : M →ₛₗ[σ₁₂] M₂) - g) x = f x - g x := rfl end section variables [module R M₂] [module R M₃] [module R M₄] variables (c : R) (f g : M →L[R] M₂) (h : M₂ →L[R] M₃) (x y z : M) lemma range_prod_eq {f : M →L[R] M₂} {g : M →L[R] M₃} (h : ker f ⊔ ker g = ⊤) : range (f.prod g) = (range f).prod (range g) := linear_map.range_prod_eq h lemma ker_prod_ker_le_ker_coprod [has_continuous_add M₃] (f : M →L[R] M₃) (g : M₂ →L[R] M₃) : (ker f).prod (ker g) ≤ ker (f.coprod g) := linear_map.ker_prod_ker_le_ker_coprod f.to_linear_map g.to_linear_map lemma ker_coprod_of_disjoint_range [has_continuous_add M₃] (f : M →L[R] M₃) (g : M₂ →L[R] M₃) (hd : disjoint f.range g.range) : ker (f.coprod g) = (ker f).prod (ker g) := linear_map.ker_coprod_of_disjoint_range f.to_linear_map g.to_linear_map hd end section variables [topological_add_group M₂] variables (f g : M →SL[σ₁₂] M₂) (x y : M) instance : has_neg (M →SL[σ₁₂] M₂) := ⟨λ f, ⟨-f, f.2.neg⟩⟩ @[simp] lemma neg_apply : (-f) x = - (f x) := rfl @[simp, norm_cast] lemma coe_neg : (((-f) : M →SL[σ₁₂] M₂) : M →ₛₗ[σ₁₂] M₂) = -(f : M →ₛₗ[σ₁₂] M₂) := rfl @[norm_cast] lemma coe_neg' : (((-f) : M →SL[σ₁₂] M₂) : M → M₂) = -(f : M → M₂) := rfl instance : has_sub (M →SL[σ₁₂] M₂) := ⟨λ f g, ⟨f - g, f.2.sub g.2⟩⟩ lemma continuous_gsmul : ∀ (n : ℤ), continuous (λ (x : M₂), n • x) | (n : ℕ) := by { simp only [gsmul_coe_nat], exact continuous_nsmul _ } | -[1+ n] := by { simp only [gsmul_neg_succ_of_nat], exact (continuous_nsmul _).neg } @[continuity] lemma continuous.gsmul {α : Type*} [topological_space α] {n : ℤ} {f : α → M₂} (hf : continuous f) : continuous (λ (x : α), n • (f x)) := (continuous_gsmul n).comp hf instance : add_comm_group (M →SL[σ₁₂] M₂) := by refine { zero := 0, add := (+), neg := has_neg.neg, sub := has_sub.sub, sub_eq_add_neg := _, nsmul := λ n f, { to_fun := λ x, n • (f x), map_add' := by simp, map_smul' := by simp [smul_comm n] }, gsmul := λ n f, { to_fun := λ x, n • (f x), map_add' := by simp, map_smul' := by simp [smul_comm n] }, gsmul_zero' := λ f, by { ext, simp }, gsmul_succ' := λ n f, by { ext, simp [add_smul, add_comm] }, gsmul_neg' := λ n f, by { ext, simp [nat.succ_eq_add_one, add_smul] }, .. continuous_linear_map.add_comm_monoid, .. }; intros; ext; apply_rules [zero_add, add_assoc, add_zero, add_left_neg, add_comm, sub_eq_add_neg] lemma sub_apply (x : M) : (f - g) x = f x - g x := rfl @[simp, norm_cast] lemma coe_sub : (((f - g) : M →SL[σ₁₂] M₂) : M →ₛₗ[σ₁₂] M₂) = f - g := rfl @[simp, norm_cast] lemma coe_sub' : (((f - g) : M →SL[σ₁₂] M₂) : M → M₂) = (f : M → M₂) - g := rfl end instance [topological_add_group M] : ring (M →L[R] M) := { mul := (*), one := 1, mul_one := λ _, ext $ λ _, rfl, one_mul := λ _, ext $ λ _, rfl, mul_assoc := λ _ _ _, ext $ λ _, rfl, left_distrib := λ _ _ _, ext $ λ _, map_add _ _ _, right_distrib := λ _ _ _, ext $ λ _, linear_map.add_apply _ _ _, ..continuous_linear_map.add_comm_group } lemma smul_right_one_pow [topological_space R] [topological_ring R] (c : R) (n : ℕ) : (smul_right (1 : R →L[R] R) c)^n = smul_right (1 : R →L[R] R) (c^n) := begin induction n with n ihn, { ext, simp }, { rw [pow_succ, ihn, mul_def, smul_right_comp, smul_eq_mul, pow_succ'] } end section variables {σ₂₁ : R₂ →+* R} [ring_hom_inv_pair σ₁₂ σ₂₁] /-- Given a right inverse `f₂ : M₂ →L[R] M` to `f₁ : M →L[R] M₂`, `proj_ker_of_right_inverse f₁ f₂ h` is the projection `M →L[R] f₁.ker` along `f₂.range`. -/ def proj_ker_of_right_inverse [topological_add_group M] (f₁ : M →SL[σ₁₂] M₂) (f₂ : M₂ →SL[σ₂₁] M) (h : function.right_inverse f₂ f₁) : M →L[R] f₁.ker := (id R M - f₂.comp f₁).cod_restrict f₁.ker $ λ x, by simp [h (f₁ x)] @[simp] lemma coe_proj_ker_of_right_inverse_apply [topological_add_group M] (f₁ : M →SL[σ₁₂] M₂) (f₂ : M₂ →SL[σ₂₁] M) (h : function.right_inverse f₂ f₁) (x : M) : (f₁.proj_ker_of_right_inverse f₂ h x : M) = x - f₂ (f₁ x) := rfl @[simp] lemma proj_ker_of_right_inverse_apply_idem [topological_add_group M] (f₁ : M →SL[σ₁₂] M₂) (f₂ : M₂ →SL[σ₂₁] M) (h : function.right_inverse f₂ f₁) (x : f₁.ker) : f₁.proj_ker_of_right_inverse f₂ h x = x := subtype.ext_iff_val.2 $ by simp @[simp] lemma proj_ker_of_right_inverse_comp_inv [topological_add_group M] (f₁ : M →SL[σ₁₂] M₂) (f₂ : M₂ →SL[σ₂₁] M) (h : function.right_inverse f₂ f₁) (y : M₂) : f₁.proj_ker_of_right_inverse f₂ h (f₂ y) = 0 := subtype.ext_iff_val.2 $ by simp [h y] end end ring section smul variables {R S : Type*} [semiring R] [semiring S] [topological_space S] {M : Type*} [topological_space M] [add_comm_monoid M] [module R M] {M₂ : Type*} [topological_space M₂] [add_comm_monoid M₂] [module R M₂] {M₃ : Type*} [topological_space M₃] [add_comm_monoid M₃] [module R M₃] [module S M₃] [smul_comm_class R S M₃] [has_continuous_smul S M₃] instance : has_scalar S (M →L[R] M₃) := ⟨λ c f, ⟨c • f, (continuous_const.smul f.2 : continuous (λ x, c • f x))⟩⟩ variables (c : S) (h : M₂ →L[R] M₃) (f g : M →L[R] M₂) (x y z : M) @[simp] lemma smul_comp : (c • h).comp f = c • (h.comp f) := rfl variables [module S M₂] [has_continuous_smul S M₂] [smul_comm_class R S M₂] lemma smul_apply : (c • f) x = c • (f x) := rfl @[simp, norm_cast] lemma coe_smul : (((c • f) : M →L[R] M₂) : M →ₗ[R] M₂) = c • f := rfl @[simp, norm_cast] lemma coe_smul' : (((c • f) : M →L[R] M₂) : M → M₂) = c • f := rfl @[simp] lemma comp_smul [linear_map.compatible_smul M₂ M₃ S R] : h.comp (c • f) = c • (h.comp f) := by { ext x, exact h.map_smul_of_tower c (f x) } /-- `continuous_linear_map.prod` as an `equiv`. -/ @[simps apply] def prod_equiv : ((M →L[R] M₂) × (M →L[R] M₃)) ≃ (M →L[R] M₂ × M₃) := { to_fun := λ f, f.1.prod f.2, inv_fun := λ f, ⟨(fst _ _ _).comp f, (snd _ _ _).comp f⟩, left_inv := λ f, by ext; refl, right_inv := λ f, by ext; refl } lemma prod_ext_iff {f g : M × M₂ →L[R] M₃} : f = g ↔ f.comp (inl _ _ _) = g.comp (inl _ _ _) ∧ f.comp (inr _ _ _) = g.comp (inr _ _ _) := by { simp only [← coe_inj, linear_map.prod_ext_iff], refl } @[ext] lemma prod_ext {f g : M × M₂ →L[R] M₃} (hl : f.comp (inl _ _ _) = g.comp (inl _ _ _)) (hr : f.comp (inr _ _ _) = g.comp (inr _ _ _)) : f = g := prod_ext_iff.2 ⟨hl, hr⟩ variables [has_continuous_add M₂] instance : module S (M →L[R] M₂) := { smul_zero := λ _, ext $ λ _, smul_zero _, zero_smul := λ _, ext $ λ _, zero_smul _ _, one_smul := λ _, ext $ λ _, by exact one_smul _ _, mul_smul := λ _ _ _, ext $ λ _, mul_smul _ _ _, add_smul := λ _ _ _, ext $ λ _, add_smul _ _ _, smul_add := λ _ _ _, ext $ λ _, smul_add _ _ _ } variables (S) [has_continuous_add M₃] /-- `continuous_linear_map.prod` as a `linear_equiv`. -/ @[simps apply] def prodₗ : ((M →L[R] M₂) × (M →L[R] M₃)) ≃ₗ[S] (M →L[R] M₂ × M₃) := { map_add' := λ f g, rfl, map_smul' := λ c f, rfl, .. prod_equiv } end smul section smul_rightₗ variables {R S T M M₂ : Type*} [ring R] [ring S] [ring T] [module R S] [add_comm_group M₂] [module R M₂] [module S M₂] [is_scalar_tower R S M₂] [topological_space S] [topological_space M₂] [has_continuous_smul S M₂] [topological_space M] [add_comm_group M] [module R M] [topological_add_group M₂] [topological_space T] [module T M₂] [has_continuous_smul T M₂] [smul_comm_class R T M₂] [smul_comm_class S T M₂] /-- Given `c : E →L[𝕜] 𝕜`, `c.smul_rightₗ` is the linear map from `F` to `E →L[𝕜] F` sending `f` to `λ e, c e • f`. See also `continuous_linear_map.smul_rightL`. -/ def smul_rightₗ (c : M →L[R] S) : M₂ →ₗ[T] (M →L[R] M₂) := { to_fun := c.smul_right, map_add' := λ x y, by { ext e, apply smul_add }, map_smul' := λ a x, by { ext e, dsimp, apply smul_comm } } @[simp] lemma coe_smul_rightₗ (c : M →L[R] S) : ⇑(smul_rightₗ c : M₂ →ₗ[T] (M →L[R] M₂)) = c.smul_right := rfl end smul_rightₗ section comm_ring variables {R : Type*} [comm_ring R] [topological_space R] {M : Type*} [topological_space M] [add_comm_group M] {M₂ : Type*} [topological_space M₂] [add_comm_group M₂] {M₃ : Type*} [topological_space M₃] [add_comm_group M₃] [module R M] [module R M₂] [module R M₃] [has_continuous_smul R M₃] variables [topological_add_group M₂] [has_continuous_smul R M₂] instance : algebra R (M₂ →L[R] M₂) := algebra.of_module smul_comp (λ _ _ _, comp_smul _ _ _) end comm_ring section restrict_scalars variables {A M M₂ : Type*} [ring A] [add_comm_group M] [add_comm_group M₂] [module A M] [module A M₂] [topological_space M] [topological_space M₂] (R : Type*) [ring R] [module R M] [module R M₂] [linear_map.compatible_smul M M₂ R A] /-- If `A` is an `R`-algebra, then a continuous `A`-linear map can be interpreted as a continuous `R`-linear map. We assume `linear_map.compatible_smul M M₂ R A` to match assumptions of `linear_map.map_smul_of_tower`. -/ def restrict_scalars (f : M →L[A] M₂) : M →L[R] M₂ := ⟨(f : M →ₗ[A] M₂).restrict_scalars R, f.continuous⟩ variable {R} @[simp, norm_cast] lemma coe_restrict_scalars (f : M →L[A] M₂) : (f.restrict_scalars R : M →ₗ[R] M₂) = (f : M →ₗ[A] M₂).restrict_scalars R := rfl @[simp] lemma coe_restrict_scalars' (f : M →L[A] M₂) : ⇑(f.restrict_scalars R) = f := rfl @[simp] lemma restrict_scalars_zero : (0 : M →L[A] M₂).restrict_scalars R = 0 := rfl section variable [topological_add_group M₂] @[simp] lemma restrict_scalars_add (f g : M →L[A] M₂) : (f + g).restrict_scalars R = f.restrict_scalars R + g.restrict_scalars R := rfl @[simp] lemma restrict_scalars_neg (f : M →L[A] M₂) : (-f).restrict_scalars R = -f.restrict_scalars R := rfl end variables {S : Type*} [ring S] [topological_space S] [module S M₂] [has_continuous_smul S M₂] [smul_comm_class A S M₂] [smul_comm_class R S M₂] @[simp] lemma restrict_scalars_smul (c : S) (f : M →L[A] M₂) : (c • f).restrict_scalars R = c • f.restrict_scalars R := rfl variables (A M M₂ R S) [topological_add_group M₂] /-- `continuous_linear_map.restrict_scalars` as a `linear_map`. See also `continuous_linear_map.restrict_scalarsL`. -/ def restrict_scalarsₗ : (M →L[A] M₂) →ₗ[S] (M →L[R] M₂) := { to_fun := restrict_scalars R, map_add' := restrict_scalars_add, map_smul' := restrict_scalars_smul } variables {A M M₂ R S} @[simp] lemma coe_restrict_scalarsₗ : ⇑(restrict_scalarsₗ A M M₂ R S) = restrict_scalars R := rfl end restrict_scalars end continuous_linear_map namespace continuous_linear_equiv section add_comm_monoid variables {R₁ : Type*} [semiring R₁] {R₂ : Type*} [semiring R₂] {R₃ : Type*} [semiring R₃] {M₁ : Type*} [topological_space M₁] [add_comm_monoid M₁] {M₂ : Type*} [topological_space M₂] [add_comm_monoid M₂] {M₃ : Type*} [topological_space M₃] [add_comm_monoid M₃] {M₄ : Type*} [topological_space M₄] [add_comm_monoid M₄] [module R₁ M₁] [module R₂ M₂] [module R₃ M₃] {σ₁₂ : R₁ →+* R₂} {σ₂₁ : R₂ →+* R₁} [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] {σ₂₃ : R₂ →+* R₃} {σ₃₂ : R₃ →+* R₂} [ring_hom_inv_pair σ₂₃ σ₃₂] [ring_hom_inv_pair σ₃₂ σ₂₃] {σ₁₃ : R₁ →+* R₃} {σ₃₁ : R₃ →+* R₁} [ring_hom_inv_pair σ₁₃ σ₃₁] [ring_hom_inv_pair σ₃₁ σ₁₃] [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] [ring_hom_comp_triple σ₃₂ σ₂₁ σ₃₁] include σ₂₁ /-- A continuous linear equivalence induces a continuous linear map. -/ def to_continuous_linear_map (e : M₁ ≃SL[σ₁₂] M₂) : M₁ →SL[σ₁₂] M₂ := { cont := e.continuous_to_fun, ..e.to_linear_equiv.to_linear_map } /-- Coerce continuous linear equivs to continuous linear maps. -/ instance : has_coe (M₁ ≃SL[σ₁₂] M₂) (M₁ →SL[σ₁₂] M₂) := ⟨to_continuous_linear_map⟩ /-- Coerce continuous linear equivs to maps. -/ -- see Note [function coercion] instance : has_coe_to_fun (M₁ ≃SL[σ₁₂] M₂) := ⟨λ _, M₁ → M₂, λ f, f⟩ @[simp] theorem coe_def_rev (e : M₁ ≃SL[σ₁₂] M₂) : e.to_continuous_linear_map = e := rfl theorem coe_apply (e : M₁ ≃SL[σ₁₂] M₂) (b : M₁) : (e : M₁ →SL[σ₁₂] M₂) b = e b := rfl @[simp] lemma coe_to_linear_equiv (f : M₁ ≃SL[σ₁₂] M₂) : ⇑f.to_linear_equiv = f := rfl @[simp, norm_cast] lemma coe_coe (e : M₁ ≃SL[σ₁₂] M₂) : ((e : M₁ →SL[σ₁₂] M₂) : M₁ → M₂) = e := rfl lemma to_linear_equiv_injective : function.injective (to_linear_equiv : (M₁ ≃SL[σ₁₂] M₂) → (M₁ ≃ₛₗ[σ₁₂] M₂)) | ⟨e, _, _⟩ ⟨e', _, _⟩ rfl := rfl @[ext] lemma ext {f g : M₁ ≃SL[σ₁₂] M₂} (h : (f : M₁ → M₂) = g) : f = g := to_linear_equiv_injective $ linear_equiv.ext $ congr_fun h lemma coe_injective : function.injective (coe : (M₁ ≃SL[σ₁₂] M₂) → (M₁ →SL[σ₁₂] M₂)) := λ e e' h, ext $ funext $ continuous_linear_map.ext_iff.1 h @[simp, norm_cast] lemma coe_inj {e e' : M₁ ≃SL[σ₁₂] M₂} : (e : M₁ →SL[σ₁₂] M₂) = e' ↔ e = e' := coe_injective.eq_iff /-- A continuous linear equivalence induces a homeomorphism. -/ def to_homeomorph (e : M₁ ≃SL[σ₁₂] M₂) : M₁ ≃ₜ M₂ := { to_equiv := e.to_linear_equiv.to_equiv, ..e } @[simp] lemma coe_to_homeomorph (e : M₁ ≃SL[σ₁₂] M₂) : ⇑e.to_homeomorph = e := rfl lemma image_closure (e : M₁ ≃SL[σ₁₂] M₂) (s : set M₁) : e '' closure s = closure (e '' s) := e.to_homeomorph.image_closure s lemma preimage_closure (e : M₁ ≃SL[σ₁₂] M₂) (s : set M₂) : e ⁻¹' closure s = closure (e ⁻¹' s) := e.to_homeomorph.preimage_closure s @[simp] lemma is_closed_image (e : M₁ ≃SL[σ₁₂] M₂) {s : set M₁} : is_closed (e '' s) ↔ is_closed s := e.to_homeomorph.is_closed_image lemma map_nhds_eq (e : M₁ ≃SL[σ₁₂] M₂) (x : M₁) : map e (𝓝 x) = 𝓝 (e x) := e.to_homeomorph.map_nhds_eq x -- Make some straightforward lemmas available to `simp`. @[simp] lemma map_zero (e : M₁ ≃SL[σ₁₂] M₂) : e (0 : M₁) = 0 := (e : M₁ →SL[σ₁₂] M₂).map_zero @[simp] lemma map_add (e : M₁ ≃SL[σ₁₂] M₂) (x y : M₁) : e (x + y) = e x + e y := (e : M₁ →SL[σ₁₂] M₂).map_add x y @[simp] lemma map_smulₛₗ (e : M₁ ≃SL[σ₁₂] M₂) (c : R₁) (x : M₁) : e (c • x) = σ₁₂ c • (e x) := (e : M₁ →SL[σ₁₂] M₂).map_smulₛₗ c x omit σ₂₁ @[simp] lemma map_smul [module R₁ M₂] (e : M₁ ≃L[R₁] M₂) (c : R₁) (x : M₁) : e (c • x) = c • (e x) := (e : M₁ →L[R₁] M₂).map_smul c x include σ₂₁ @[simp] lemma map_eq_zero_iff (e : M₁ ≃SL[σ₁₂] M₂) {x : M₁} : e x = 0 ↔ x = 0 := e.to_linear_equiv.map_eq_zero_iff attribute [continuity] continuous_linear_equiv.continuous_to_fun continuous_linear_equiv.continuous_inv_fun @[continuity] protected lemma continuous (e : M₁ ≃SL[σ₁₂] M₂) : continuous (e : M₁ → M₂) := e.continuous_to_fun protected lemma continuous_on (e : M₁ ≃SL[σ₁₂] M₂) {s : set M₁} : continuous_on (e : M₁ → M₂) s := e.continuous.continuous_on protected lemma continuous_at (e : M₁ ≃SL[σ₁₂] M₂) {x : M₁} : continuous_at (e : M₁ → M₂) x := e.continuous.continuous_at protected lemma continuous_within_at (e : M₁ ≃SL[σ₁₂] M₂) {s : set M₁} {x : M₁} : continuous_within_at (e : M₁ → M₂) s x := e.continuous.continuous_within_at lemma comp_continuous_on_iff {α : Type*} [topological_space α] (e : M₁ ≃SL[σ₁₂] M₂) {f : α → M₁} {s : set α} : continuous_on (e ∘ f) s ↔ continuous_on f s := e.to_homeomorph.comp_continuous_on_iff _ _ lemma comp_continuous_iff {α : Type*} [topological_space α] (e : M₁ ≃SL[σ₁₂] M₂) {f : α → M₁} : continuous (e ∘ f) ↔ continuous f := e.to_homeomorph.comp_continuous_iff omit σ₂₁ /-- An extensionality lemma for `R ≃L[R] M`. -/ lemma ext₁ [topological_space R₁] {f g : R₁ ≃L[R₁] M₁} (h : f 1 = g 1) : f = g := ext $ funext $ λ x, mul_one x ▸ by rw [← smul_eq_mul, map_smul, h, map_smul] section variables (R₁ M₁) /-- The identity map as a continuous linear equivalence. -/ @[refl] protected def refl : M₁ ≃L[R₁] M₁ := { continuous_to_fun := continuous_id, continuous_inv_fun := continuous_id, .. linear_equiv.refl R₁ M₁ } end @[simp, norm_cast] lemma coe_refl : (continuous_linear_equiv.refl R₁ M₁ : M₁ →L[R₁] M₁) = continuous_linear_map.id R₁ M₁ := rfl @[simp, norm_cast] lemma coe_refl' : (continuous_linear_equiv.refl R₁ M₁ : M₁ → M₁) = id := rfl /-- The inverse of a continuous linear equivalence as a continuous linear equivalence-/ @[symm] protected def symm (e : M₁ ≃SL[σ₁₂] M₂) : M₂ ≃SL[σ₂₁] M₁ := { continuous_to_fun := e.continuous_inv_fun, continuous_inv_fun := e.continuous_to_fun, .. e.to_linear_equiv.symm } include σ₂₁ @[simp] lemma symm_to_linear_equiv (e : M₁ ≃SL[σ₁₂] M₂) : e.symm.to_linear_equiv = e.to_linear_equiv.symm := by { ext, refl } @[simp] lemma symm_to_homeomorph (e : M₁ ≃SL[σ₁₂] M₂) : e.to_homeomorph.symm = e.symm.to_homeomorph := rfl lemma symm_map_nhds_eq (e : M₁ ≃SL[σ₁₂] M₂) (x : M₁) : map e.symm (𝓝 (e x)) = 𝓝 x := e.to_homeomorph.symm_map_nhds_eq x omit σ₂₁ include σ₂₁ σ₃₂ σ₃₁ /-- The composition of two continuous linear equivalences as a continuous linear equivalence. -/ @[trans] protected def trans (e₁ : M₁ ≃SL[σ₁₂] M₂) (e₂ : M₂ ≃SL[σ₂₃] M₃) : M₁ ≃SL[σ₁₃] M₃ := { continuous_to_fun := e₂.continuous_to_fun.comp e₁.continuous_to_fun, continuous_inv_fun := e₁.continuous_inv_fun.comp e₂.continuous_inv_fun, .. e₁.to_linear_equiv.trans e₂.to_linear_equiv } include σ₁₃ @[simp] lemma trans_to_linear_equiv (e₁ : M₁ ≃SL[σ₁₂] M₂) (e₂ : M₂ ≃SL[σ₂₃] M₃) : (e₁.trans e₂).to_linear_equiv = e₁.to_linear_equiv.trans e₂.to_linear_equiv := by { ext, refl } omit σ₁₃ σ₂₁ σ₃₂ σ₃₁ /-- Product of two continuous linear equivalences. The map comes from `equiv.prod_congr`. -/ def prod [module R₁ M₂] [module R₁ M₃] [module R₁ M₄] (e : M₁ ≃L[R₁] M₂) (e' : M₃ ≃L[R₁] M₄) : (M₁ × M₃) ≃L[R₁] (M₂ × M₄) := { continuous_to_fun := e.continuous_to_fun.prod_map e'.continuous_to_fun, continuous_inv_fun := e.continuous_inv_fun.prod_map e'.continuous_inv_fun, .. e.to_linear_equiv.prod e'.to_linear_equiv } @[simp, norm_cast] lemma prod_apply [module R₁ M₂] [module R₁ M₃] [module R₁ M₄] (e : M₁ ≃L[R₁] M₂) (e' : M₃ ≃L[R₁] M₄) (x) : e.prod e' x = (e x.1, e' x.2) := rfl @[simp, norm_cast] lemma coe_prod [module R₁ M₂] [module R₁ M₃] [module R₁ M₄] (e : M₁ ≃L[R₁] M₂) (e' : M₃ ≃L[R₁] M₄) : (e.prod e' : (M₁ × M₃) →L[R₁] (M₂ × M₄)) = (e : M₁ →L[R₁] M₂).prod_map (e' : M₃ →L[R₁] M₄) := rfl include σ₂₁ theorem bijective (e : M₁ ≃SL[σ₁₂] M₂) : function.bijective e := e.to_linear_equiv.to_equiv.bijective theorem injective (e : M₁ ≃SL[σ₁₂] M₂) : function.injective e := e.to_linear_equiv.to_equiv.injective theorem surjective (e : M₁ ≃SL[σ₁₂] M₂) : function.surjective e := e.to_linear_equiv.to_equiv.surjective include σ₃₂ σ₃₁ σ₁₃ @[simp] theorem trans_apply (e₁ : M₁ ≃SL[σ₁₂] M₂) (e₂ : M₂ ≃SL[σ₂₃] M₃) (c : M₁) : (e₁.trans e₂) c = e₂ (e₁ c) := rfl omit σ₃₂ σ₃₁ σ₁₃ @[simp] theorem apply_symm_apply (e : M₁ ≃SL[σ₁₂] M₂) (c : M₂) : e (e.symm c) = c := e.1.right_inv c @[simp] theorem symm_apply_apply (e : M₁ ≃SL[σ₁₂] M₂) (b : M₁) : e.symm (e b) = b := e.1.left_inv b include σ₁₂ σ₂₃ σ₁₃ σ₃₁ @[simp] theorem symm_trans_apply (e₁ : M₂ ≃SL[σ₂₁] M₁) (e₂ : M₃ ≃SL[σ₃₂] M₂) (c : M₁) : (e₂.trans e₁).symm c = e₂.symm (e₁.symm c) := rfl omit σ₁₂ σ₂₃ σ₁₃ σ₃₁ @[simp] theorem symm_image_image (e : M₁ ≃SL[σ₁₂] M₂) (s : set M₁) : e.symm '' (e '' s) = s := e.to_linear_equiv.to_equiv.symm_image_image s @[simp] theorem image_symm_image (e : M₁ ≃SL[σ₁₂] M₂) (s : set M₂) : e '' (e.symm '' s) = s := e.symm.symm_image_image s include σ₃₂ σ₃₁ @[simp, norm_cast] lemma comp_coe (f : M₁ ≃SL[σ₁₂] M₂) (f' : M₂ ≃SL[σ₂₃] M₃) : (f' : M₂ →SL[σ₂₃] M₃).comp (f : M₁ →SL[σ₁₂] M₂) = (f.trans f' : M₁ →SL[σ₁₃] M₃) := rfl omit σ₃₂ σ₃₁ σ₂₁ @[simp] theorem coe_comp_coe_symm (e : M₁ ≃SL[σ₁₂] M₂) : (e : M₁ →SL[σ₁₂] M₂).comp (e.symm : M₂ →SL[σ₂₁] M₁) = continuous_linear_map.id R₂ M₂ := continuous_linear_map.ext e.apply_symm_apply @[simp] theorem coe_symm_comp_coe (e : M₁ ≃SL[σ₁₂] M₂) : (e.symm : M₂ →SL[σ₂₁] M₁).comp (e : M₁ →SL[σ₁₂] M₂) = continuous_linear_map.id R₁ M₁ := continuous_linear_map.ext e.symm_apply_apply include σ₂₁ @[simp] lemma symm_comp_self (e : M₁ ≃SL[σ₁₂] M₂) : (e.symm : M₂ → M₁) ∘ (e : M₁ → M₂) = id := by{ ext x, exact symm_apply_apply e x } @[simp] lemma self_comp_symm (e : M₁ ≃SL[σ₁₂] M₂) : (e : M₁ → M₂) ∘ (e.symm : M₂ → M₁) = id := by{ ext x, exact apply_symm_apply e x } @[simp] theorem symm_symm (e : M₁ ≃SL[σ₁₂] M₂) : e.symm.symm = e := by { ext x, refl } omit σ₂₁ @[simp] lemma refl_symm : (continuous_linear_equiv.refl R₁ M₁).symm = continuous_linear_equiv.refl R₁ M₁ := rfl include σ₂₁ theorem symm_symm_apply (e : M₁ ≃SL[σ₁₂] M₂) (x : M₁) : e.symm.symm x = e x := rfl lemma symm_apply_eq (e : M₁ ≃SL[σ₁₂] M₂) {x y} : e.symm x = y ↔ x = e y := e.to_linear_equiv.symm_apply_eq lemma eq_symm_apply (e : M₁ ≃SL[σ₁₂] M₂) {x y} : y = e.symm x ↔ e y = x := e.to_linear_equiv.eq_symm_apply protected lemma image_eq_preimage (e : M₁ ≃SL[σ₁₂] M₂) (s : set M₁) : e '' s = e.symm ⁻¹' s := e.to_linear_equiv.to_equiv.image_eq_preimage s protected lemma image_symm_eq_preimage (e : M₁ ≃SL[σ₁₂] M₂) (s : set M₂) : e.symm '' s = e ⁻¹' s := by rw [e.symm.image_eq_preimage, e.symm_symm] omit σ₂₁ /-- Create a `continuous_linear_equiv` from two `continuous_linear_map`s that are inverse of each other. -/ def equiv_of_inverse (f₁ : M₁ →SL[σ₁₂] M₂) (f₂ : M₂ →SL[σ₂₁] M₁) (h₁ : function.left_inverse f₂ f₁) (h₂ : function.right_inverse f₂ f₁) : M₁ ≃SL[σ₁₂] M₂ := { to_fun := f₁, continuous_to_fun := f₁.continuous, inv_fun := f₂, continuous_inv_fun := f₂.continuous, left_inv := h₁, right_inv := h₂, .. f₁ } include σ₂₁ @[simp] lemma equiv_of_inverse_apply (f₁ : M₁ →SL[σ₁₂] M₂) (f₂ h₁ h₂ x) : equiv_of_inverse f₁ f₂ h₁ h₂ x = f₁ x := rfl @[simp] lemma symm_equiv_of_inverse (f₁ : M₁ →SL[σ₁₂] M₂) (f₂ h₁ h₂) : (equiv_of_inverse f₁ f₂ h₁ h₂).symm = equiv_of_inverse f₂ f₁ h₂ h₁ := rfl omit σ₂₁ variable (M₁) /-- The continuous linear equivalences from `M` to itself form a group under composition. -/ instance automorphism_group : group (M₁ ≃L[R₁] M₁) := { mul := λ f g, g.trans f, one := continuous_linear_equiv.refl R₁ M₁, inv := λ f, f.symm, mul_assoc := λ f g h, by {ext, refl}, mul_one := λ f, by {ext, refl}, one_mul := λ f, by {ext, refl}, mul_left_inv := λ f, by {ext, exact f.left_inv x} } end add_comm_monoid section add_comm_group variables {R : Type*} [semiring R] {M : Type*} [topological_space M] [add_comm_group M] {M₂ : Type*} [topological_space M₂] [add_comm_group M₂] {M₃ : Type*} [topological_space M₃] [add_comm_group M₃] {M₄ : Type*} [topological_space M₄] [add_comm_group M₄] [module R M] [module R M₂] [module R M₃] [module R M₄] variables [topological_add_group M₄] /-- Equivalence given by a block lower diagonal matrix. `e` and `e'` are diagonal square blocks, and `f` is a rectangular block below the diagonal. -/ def skew_prod (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (f : M →L[R] M₄) : (M × M₃) ≃L[R] M₂ × M₄ := { continuous_to_fun := (e.continuous_to_fun.comp continuous_fst).prod_mk ((e'.continuous_to_fun.comp continuous_snd).add $ f.continuous.comp continuous_fst), continuous_inv_fun := (e.continuous_inv_fun.comp continuous_fst).prod_mk (e'.continuous_inv_fun.comp $ continuous_snd.sub $ f.continuous.comp $ e.continuous_inv_fun.comp continuous_fst), .. e.to_linear_equiv.skew_prod e'.to_linear_equiv ↑f } @[simp] lemma skew_prod_apply (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (f : M →L[R] M₄) (x) : e.skew_prod e' f x = (e x.1, e' x.2 + f x.1) := rfl @[simp] lemma skew_prod_symm_apply (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (f : M →L[R] M₄) (x) : (e.skew_prod e' f).symm x = (e.symm x.1, e'.symm (x.2 - f (e.symm x.1))) := rfl end add_comm_group section ring variables {R : Type*} [ring R] {R₂ : Type*} [ring R₂] {M : Type*} [topological_space M] [add_comm_group M] [module R M] {M₂ : Type*} [topological_space M₂] [add_comm_group M₂] [module R₂ M₂] variables {σ₁₂ : R →+* R₂} {σ₂₁ : R₂ →+* R} [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] include σ₂₁ @[simp] lemma map_sub (e : M ≃SL[σ₁₂] M₂) (x y : M) : e (x - y) = e x - e y := (e : M →SL[σ₁₂] M₂).map_sub x y @[simp] lemma map_neg (e : M ≃SL[σ₁₂] M₂) (x : M) : e (-x) = -e x := (e : M →SL[σ₁₂] M₂).map_neg x omit σ₂₁ section /-! The next theorems cover the identification between `M ≃L[𝕜] M`and the group of units of the ring `M →L[R] M`. -/ variables [topological_add_group M] /-- An invertible continuous linear map `f` determines a continuous equivalence from `M` to itself. -/ def of_unit (f : units (M →L[R] M)) : (M ≃L[R] M) := { to_linear_equiv := { to_fun := f.val, map_add' := by simp, map_smul' := by simp, inv_fun := f.inv, left_inv := λ x, show (f.inv * f.val) x = x, by {rw f.inv_val, simp}, right_inv := λ x, show (f.val * f.inv) x = x, by {rw f.val_inv, simp}, }, continuous_to_fun := f.val.continuous, continuous_inv_fun := f.inv.continuous } /-- A continuous equivalence from `M` to itself determines an invertible continuous linear map. -/ def to_unit (f : (M ≃L[R] M)) : units (M →L[R] M) := { val := f, inv := f.symm, val_inv := by {ext, simp}, inv_val := by {ext, simp} } variables (R M) /-- The units of the algebra of continuous `R`-linear endomorphisms of `M` is multiplicatively equivalent to the type of continuous linear equivalences between `M` and itself. -/ def units_equiv : units (M →L[R] M) ≃* (M ≃L[R] M) := { to_fun := of_unit, inv_fun := to_unit, left_inv := λ f, by {ext, refl}, right_inv := λ f, by {ext, refl}, map_mul' := λ x y, by {ext, refl} } @[simp] lemma units_equiv_apply (f : units (M →L[R] M)) (x : M) : units_equiv R M f x = f x := rfl end section variables (R) [topological_space R] [has_continuous_mul R] /-- Continuous linear equivalences `R ≃L[R] R` are enumerated by `units R`. -/ def units_equiv_aut : units R ≃ (R ≃L[R] R) := { to_fun := λ u, equiv_of_inverse (continuous_linear_map.smul_right (1 : R →L[R] R) ↑u) (continuous_linear_map.smul_right (1 : R →L[R] R) ↑u⁻¹) (λ x, by simp) (λ x, by simp), inv_fun := λ e, ⟨e 1, e.symm 1, by rw [← smul_eq_mul, ← map_smul, smul_eq_mul, mul_one, symm_apply_apply], by rw [← smul_eq_mul, ← map_smul, smul_eq_mul, mul_one, apply_symm_apply]⟩, left_inv := λ u, units.ext $ by simp, right_inv := λ e, ext₁ $ by simp } variable {R} @[simp] lemma units_equiv_aut_apply (u : units R) (x : R) : units_equiv_aut R u x = x * u := rfl @[simp] lemma units_equiv_aut_apply_symm (u : units R) (x : R) : (units_equiv_aut R u).symm x = x * ↑u⁻¹ := rfl @[simp] lemma units_equiv_aut_symm_apply (e : R ≃L[R] R) : ↑((units_equiv_aut R).symm e) = e 1 := rfl end variables [module R M₂] [topological_add_group M] open continuous_linear_map (id fst snd subtype_val mem_ker) /-- A pair of continuous linear maps such that `f₁ ∘ f₂ = id` generates a continuous linear equivalence `e` between `M` and `M₂ × f₁.ker` such that `(e x).2 = x` for `x ∈ f₁.ker`, `(e x).1 = f₁ x`, and `(e (f₂ y)).2 = 0`. The map is given by `e x = (f₁ x, x - f₂ (f₁ x))`. -/ def equiv_of_right_inverse (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) : M ≃L[R] M₂ × f₁.ker := equiv_of_inverse (f₁.prod (f₁.proj_ker_of_right_inverse f₂ h)) (f₂.coprod (subtype_val f₁.ker)) (λ x, by simp) (λ ⟨x, y⟩, by simp [h x]) @[simp] lemma fst_equiv_of_right_inverse (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) (x : M) : (equiv_of_right_inverse f₁ f₂ h x).1 = f₁ x := rfl @[simp] lemma snd_equiv_of_right_inverse (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) (x : M) : ((equiv_of_right_inverse f₁ f₂ h x).2 : M) = x - f₂ (f₁ x) := rfl @[simp] lemma equiv_of_right_inverse_symm_apply (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) (y : M₂ × f₁.ker) : (equiv_of_right_inverse f₁ f₂ h).symm y = f₂ y.1 + y.2 := rfl end ring end continuous_linear_equiv namespace continuous_linear_map open_locale classical variables {R : Type*} {M : Type*} {M₂ : Type*} [topological_space M] [topological_space M₂] section variables [semiring R] variables [add_comm_monoid M₂] [module R M₂] variables [add_comm_monoid M] [module R M] /-- Introduce a function `inverse` from `M →L[R] M₂` to `M₂ →L[R] M`, which sends `f` to `f.symm` if `f` is a continuous linear equivalence and to `0` otherwise. This definition is somewhat ad hoc, but one needs a fully (rather than partially) defined inverse function for some purposes, including for calculus. -/ noncomputable def inverse : (M →L[R] M₂) → (M₂ →L[R] M) := λ f, if h : ∃ (e : M ≃L[R] M₂), (e : M →L[R] M₂) = f then ((classical.some h).symm : M₂ →L[R] M) else 0 /-- By definition, if `f` is invertible then `inverse f = f.symm`. -/ @[simp] lemma inverse_equiv (e : M ≃L[R] M₂) : inverse (e : M →L[R] M₂) = e.symm := begin have h : ∃ (e' : M ≃L[R] M₂), (e' : M →L[R] M₂) = ↑e := ⟨e, rfl⟩, simp only [inverse, dif_pos h], congr, exact_mod_cast (classical.some_spec h) end /-- By definition, if `f` is not invertible then `inverse f = 0`. -/ @[simp] lemma inverse_non_equiv (f : M →L[R] M₂) (h : ¬∃ (e' : M ≃L[R] M₂), ↑e' = f) : inverse f = 0 := dif_neg h end section variables [ring R] variables [add_comm_group M] [topological_add_group M] [module R M] variables [add_comm_group M₂] [module R M₂] @[simp] lemma ring_inverse_equiv (e : M ≃L[R] M) : ring.inverse ↑e = inverse (e : M →L[R] M) := begin suffices : ring.inverse ((((continuous_linear_equiv.units_equiv _ _).symm e) : M →L[R] M)) = inverse ↑e, { convert this }, simp, refl, end /-- The function `continuous_linear_equiv.inverse` can be written in terms of `ring.inverse` for the ring of self-maps of the domain. -/ lemma to_ring_inverse (e : M ≃L[R] M₂) (f : M →L[R] M₂) : inverse f = (ring.inverse ((e.symm : (M₂ →L[R] M)).comp f)) ∘L ↑e.symm := begin by_cases h₁ : ∃ (e' : M ≃L[R] M₂), ↑e' = f, { obtain ⟨e', he'⟩ := h₁, rw ← he', change _ = (ring.inverse ↑(e'.trans e.symm)) ∘L ↑e.symm, ext, simp }, { suffices : ¬is_unit ((e.symm : M₂ →L[R] M).comp f), { simp [this, h₁] }, revert h₁, contrapose!, rintros ⟨F, hF⟩, use (continuous_linear_equiv.units_equiv _ _ F).trans e, ext, simp [hF] } end lemma ring_inverse_eq_map_inverse : ring.inverse = @inverse R M M _ _ _ _ _ _ _ := begin ext, simp [to_ring_inverse (continuous_linear_equiv.refl R M)], end end end continuous_linear_map namespace submodule variables {R : Type*} [ring R] {M : Type*} [topological_space M] [add_comm_group M] [module R M] {M₂ : Type*} [topological_space M₂] [add_comm_group M₂] [module R M₂] open continuous_linear_map /-- A submodule `p` is called *complemented* if there exists a continuous projection `M →ₗ[R] p`. -/ def closed_complemented (p : submodule R M) : Prop := ∃ f : M →L[R] p, ∀ x : p, f x = x lemma closed_complemented.has_closed_complement {p : submodule R M} [t1_space p] (h : closed_complemented p) : ∃ (q : submodule R M) (hq : is_closed (q : set M)), is_compl p q := exists.elim h $ λ f hf, ⟨f.ker, f.is_closed_ker, linear_map.is_compl_of_proj hf⟩ protected lemma closed_complemented.is_closed [topological_add_group M] [t1_space M] {p : submodule R M} (h : closed_complemented p) : is_closed (p : set M) := begin rcases h with ⟨f, hf⟩, have : ker (id R M - (subtype_val p).comp f) = p := linear_map.ker_id_sub_eq_of_proj hf, exact this ▸ (is_closed_ker _) end @[simp] lemma closed_complemented_bot : closed_complemented (⊥ : submodule R M) := ⟨0, λ x, by simp only [zero_apply, eq_zero_of_bot_submodule x]⟩ @[simp] lemma closed_complemented_top : closed_complemented (⊤ : submodule R M) := ⟨(id R M).cod_restrict ⊤ (λ x, trivial), λ x, subtype.ext_iff_val.2 $ by simp⟩ end submodule lemma continuous_linear_map.closed_complemented_ker_of_right_inverse {R : Type*} [ring R] {M : Type*} [topological_space M] [add_comm_group M] {M₂ : Type*} [topological_space M₂] [add_comm_group M₂] [module R M] [module R M₂] [topological_add_group M] (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) : f₁.ker.closed_complemented := ⟨f₁.proj_ker_of_right_inverse f₂ h, f₁.proj_ker_of_right_inverse_apply_idem f₂ h⟩
8a35b9cfc530d92798f39153889a4f1f7e8ec6a9
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/lean/run/concatElim.lean
958941bc31b01de44092b2bd54748dea56152014
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
2,700
lean
universes u def concat {α} : List α → α → List α | [], a => [a] | x::xs, a => x :: concat xs a def last {α} : (xs : List α) → xs ≠ [] → α | [], h => by contradiction | [a], h => a | _::a::as, h => last (a::as) (fun h => by injection h) def dropLast {α} : List α → List α | [] => [] | [a] => [] | a::as => a :: dropLast as variable {α} theorem concatEq (xs : List α) (h : xs ≠ []) : concat (dropLast xs) (last xs h) = xs := by match xs, h with | [], h => contradiction | [x], h => rfl | x₁::x₂::xs, h => simp [concat, last, concatEq (x₂::xs) List.noConfusion] theorem lengthCons {α} (x : α) (xs : List α) : (x::xs).length = xs.length + 1 := let rec aux (a : α) (xs : List α) : (n : Nat) → (a::xs).lengthAux n = xs.lengthAux n + 1 := match xs with | [] => fun _ => rfl | x::xs => fun n => aux a xs (n+1) aux x xs 0 theorem eqNilOfLengthZero {α} : (xs : List α) → xs.length = 0 → xs = [] | [], h => rfl | x::xs, h => by rw [lengthCons] at h; contradiction theorem dropLastLen {α} (xs : List α) : (n : Nat) → xs.length = n+1 → (dropLast xs).length = n := by match xs with | [] => intros; contradiction | [a] => intro n h have 1 = n + 1 from h have 0 = n by injection this; assumption subst this rfl | x₁::x₂::xs => intro n h cases n with | zero => simp [lengthCons] at h injection h with h injection h | succ n => have (x₁ :: x₂ :: xs).length = xs.length + 2 by simp [lengthCons] have xs.length = n by rw [this] at h; injection h with h; injection h with h; assumption simp [dropLast, lengthCons, dropLastLen (x₂::xs) xs.length (lengthCons ..), this] @[inline] def concatElim {α} (motive : List α → Sort u) (base : Unit → motive []) (ind : (xs : List α) → (a : α) → motive xs → motive (concat xs a)) (xs : List α) : motive xs := let rec @[specialize] aux : (n : Nat) → (xs : List α) → xs.length = n → motive xs | 0, xs, h => by have aux := eqNilOfLengthZero _ h subst aux apply base () | n+1, xs, h => by have notNil : xs ≠ [] by intro h1; subst h1; injection h let ih := aux n (dropLast xs) (dropLastLen _ _ h) let aux := ind (dropLast xs) (last xs notNil) ih rw [concatEq] at aux exact aux aux xs.length xs rfl -- The generated code is tail recursive def test (xs : List Nat) : IO Unit := concatElim (motive := fun _ => IO Unit) (fun _ => pure ()) (fun xs x r => do IO.println s!"step xs: {xs} x: {x}"; r) xs #eval test [1, 2, 3, 4]
2b32c710ff44c78b6054e19f7e5eeeb42a940150
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/field_theory/ax_grothendieck.lean
691f6f9a276b4ae062cdca7da22a713fc671b33f
[ "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
3,217
lean
/- Copyright (c) 2023 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.mv_polynomial.basic import ring_theory.algebraic import data.fintype.card /-! # Ax-Grothendieck for algebraic extensions of `zmod p` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file proves that if `R` is an algebraic extension of a finite field, then any injective polynomial map `R^n -> R^n` is also surjective. This proof is required for the true Ax-Grothendieck theorem, which proves the same result for any algebraically closed field of characteristic zero. ## TODO The proof of the theorem for characteristic zero is not in mathlib, but it is at https://github.com/Jlh18/ModelTheoryInLean8 -/ noncomputable theory open mv_polynomial finset function /-- Any injective polynomial map over an algebraic extension of a finite field is surjective. -/ lemma ax_grothendieck_of_locally_finite {ι K R : Type*} [field K] [finite K] [comm_ring R] [finite ι] [algebra K R] (alg : algebra.is_algebraic K R) (ps : ι → mv_polynomial ι R) (hinj : injective (λ v i, eval v (ps i))) : surjective (λ v i, eval v (ps i)) := begin have is_int : ∀ x : R, is_integral K x, from λ x, is_algebraic_iff_is_integral.1 (alg x), classical, intros v, casesI nonempty_fintype ι, /- `s` is the set of all coefficients of the polynomial, as well as all of the coordinates of `v`, the point I am trying to find the preimage of. -/ let s : finset R := finset.bUnion (univ : finset ι) (λ i, (ps i).support.image (λ x, coeff x (ps i))) ∪ (univ : finset ι).image v, have hv : ∀ i, v i ∈ algebra.adjoin K (s : set R), from λ j, algebra.subset_adjoin (mem_union_right _ (mem_image.2 ⟨j, mem_univ _, rfl⟩)), have hs₁ : ∀ (i : ι) (k : ι →₀ ℕ), k ∈ (ps i).support → coeff k (ps i) ∈ algebra.adjoin K (s : set R), from λ i k hk, algebra.subset_adjoin (mem_union_left _ (mem_bUnion.2 ⟨i, mem_univ _, mem_image_of_mem _ hk⟩)), have hs : ∀ i, mv_polynomial.eval v (ps i) ∈ algebra.adjoin K (s : set R), from λ i, eval_mem (hs₁ _) hv, letI := is_noetherian_adjoin_finset s (λ x _, is_int x), letI := module.is_noetherian.finite K (algebra.adjoin K (s : set R)), letI : finite (algebra.adjoin K (s : set R)) := finite_dimensional.finite_of_finite K (algebra.adjoin K (s : set R)), /- The restriction of the polynomial map, `ps`, to the subalgebra generated by `s` -/ let res : (ι → algebra.adjoin K (s : set R)) → (ι → algebra.adjoin K (s : set R)) := λ x i, ⟨eval (λ j : ι, (x j : R)) (ps i), eval_mem (hs₁ _) (λ i, (x i).2)⟩, have hres_inj : injective res, { intros x y hxy, ext i, simp only [res, subtype.ext_iff, funext_iff] at hxy, exact congr_fun (hinj (funext hxy)) i }, have hres_surj : surjective res, from finite.injective_iff_surjective.1 hres_inj, cases hres_surj (λ i, ⟨v i, hv i⟩) with w hw, use λ i, w i, simpa only [res, subtype.ext_iff, funext_iff] using hw, end
1771283cf8492a8c7d653e891c046951041bea46
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/algebra/field.lean
3764d0a56b597d9d984ee6fea3fcb12b2c80877c
[ "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
5,672
lean
/- Copyright (c) 2021 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Scott Morrison -/ import topology.algebra.ring import topology.algebra.group_with_zero import topology.local_extr /-! # Topological fields A topological division ring is a topological ring whose inversion function is continuous at every non-zero element. -/ namespace topological_ring open topological_space function variables (R : Type*) [semiring R] variables [topological_space R] /-- The induced topology on units of a topological semiring. This is not a global instance since other topologies could be relevant. Instead there is a class `induced_units` asserting that something equivalent to this construction holds. -/ def topological_space_units : topological_space Rˣ := induced (coe : Rˣ → R) ‹_› /-- Asserts the topology on units is the induced topology. Note: this is not always the correct topology. Another good candidate is the subspace topology of $R \times R$, with the units embedded via $u \mapsto (u, u^{-1})$. These topologies are not (propositionally) equal in general. -/ class induced_units [t : topological_space $ Rˣ] : Prop := (top_eq : t = induced (coe : Rˣ → R) ‹_›) variables [topological_space $ Rˣ] lemma units_topology_eq [induced_units R] : ‹topological_space Rˣ› = induced (coe : Rˣ → R) ‹_› := induced_units.top_eq lemma induced_units.continuous_coe [induced_units R] : continuous (coe : Rˣ → R) := (units_topology_eq R).symm ▸ continuous_induced_dom lemma units_embedding [induced_units R] : embedding (coe : Rˣ → R) := { induced := units_topology_eq R, inj := λ x y h, units.ext h } instance top_monoid_units [topological_semiring R] [induced_units R] : has_continuous_mul Rˣ := ⟨begin let mulR := (λ (p : R × R), p.1*p.2), let mulRx := (λ (p : Rˣ × Rˣ), p.1*p.2), have key : coe ∘ mulRx = mulR ∘ (λ p, (p.1.val, p.2.val)), from rfl, rw [continuous_iff_le_induced, units_topology_eq R, prod_induced_induced, induced_compose, key, ← induced_compose], apply induced_mono, rw ← continuous_iff_le_induced, exact continuous_mul, end⟩ end topological_ring variables {K : Type*} [division_ring K] [topological_space K] /-- Left-multiplication by a nonzero element of a topological division ring is proper, i.e., inverse images of compact sets are compact. -/ lemma filter.tendsto_cocompact_mul_left₀ [has_continuous_mul K] {a : K} (ha : a ≠ 0) : filter.tendsto (λ x : K, a * x) (filter.cocompact K) (filter.cocompact K) := filter.tendsto_cocompact_mul_left (inv_mul_cancel ha) /-- Right-multiplication by a nonzero element of a topological division ring is proper, i.e., inverse images of compact sets are compact. -/ lemma filter.tendsto_cocompact_mul_right₀ [has_continuous_mul K] {a : K} (ha : a ≠ 0) : filter.tendsto (λ x : K, x * a) (filter.cocompact K) (filter.cocompact K) := filter.tendsto_cocompact_mul_right (mul_inv_cancel ha) variables (K) /-- A topological division ring is a division ring with a topology where all operations are continuous, including inversion. -/ class topological_division_ring extends topological_ring K, has_continuous_inv₀ K : Prop namespace topological_division_ring open filter set /-! In this section, we show that units of a topological division ring endowed with the induced topology form a topological group. These are not global instances because one could want another topology on units. To turn on this feature, use: ```lean local attribute [instance] topological_semiring.topological_space_units topological_division_ring.units_top_group ``` -/ local attribute [instance] topological_ring.topological_space_units @[priority 100] instance induced_units : topological_ring.induced_units K := ⟨rfl⟩ variables [topological_division_ring K] lemma units_top_group : topological_group Kˣ := { continuous_inv := begin rw continuous_iff_continuous_at, intros x, rw [continuous_at, nhds_induced, nhds_induced, tendsto_iff_comap, ←function.semiconj.filter_comap units.coe_inv _], apply comap_mono, rw [← tendsto_iff_comap, units.coe_inv], exact continuous_at_inv₀ x.ne_zero end, ..topological_ring.top_monoid_units K} local attribute [instance] units_top_group lemma continuous_units_inv : continuous (λ x : Kˣ, (↑(x⁻¹) : K)) := (topological_ring.induced_units.continuous_coe K).comp continuous_inv end topological_division_ring section affine_homeomorph /-! This section is about affine homeomorphisms from a topological field `𝕜` to itself. Technically it does not require `𝕜` to be a topological field, a topological ring that happens to be a field is enough. -/ variables {𝕜 : Type*} [field 𝕜] [topological_space 𝕜] [topological_ring 𝕜] /-- The map `λ x, a * x + b`, as a homeomorphism from `𝕜` (a topological field) to itself, when `a ≠ 0`. -/ @[simps] def affine_homeomorph (a b : 𝕜) (h : a ≠ 0) : 𝕜 ≃ₜ 𝕜 := { to_fun := λ x, a * x + b, inv_fun := λ y, (y - b) / a, left_inv := λ x, by { simp only [add_sub_cancel], exact mul_div_cancel_left x h, }, right_inv := λ y, by { simp [mul_div_cancel' _ h], }, } end affine_homeomorph section local_extr variables {α β : Type*} [topological_space α] [linear_ordered_semifield β] {a : α} open_locale topological_space lemma is_local_min.inv {f : α → β} {a : α} (h1 : is_local_min f a) (h2 : ∀ᶠ z in 𝓝 a, 0 < f z) : is_local_max f⁻¹ a := by filter_upwards [h1, h2] with z h3 h4 using (inv_le_inv h4 h2.self_of_nhds).mpr h3 end local_extr
cdc03a356f6d7d1c6dc659c23cf50ea632fe7580
f083c4ed5d443659f3ed9b43b1ca5bb037ddeb58
/linear_algebra/prod_module.lean
e96bf9e99974373cb2061a9e5ec15b73417abe84
[ "Apache-2.0" ]
permissive
semorrison/mathlib
1be6f11086e0d24180fec4b9696d3ec58b439d10
20b4143976dad48e664c4847b75a85237dca0a89
refs/heads/master
1,583,799,212,170
1,535,634,130,000
1,535,730,505,000
129,076,205
0
0
Apache-2.0
1,551,697,998,000
1,523,442,265,000
Lean
UTF-8
Lean
false
false
11,172
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl Semigroup, monoid, group and module structures on product spaces. -/ import data.prod linear_algebra.basic open set function universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {p q : α × β} namespace prod section variables [has_mul α] [has_mul β] instance : has_mul (α × β) := ⟨λp q, (p.1 * q.1, p.2 * q.2)⟩ @[simp] lemma fst_mul : (p * q).1 = p.1 * q.1 := rfl @[simp] lemma snd_mul : (p * q).2 = p.2 * q.2 := rfl end section variables [has_one α] [has_one β] instance : has_one (α × β) := ⟨(1, 1)⟩ @[simp] lemma fst_one : (1 : α × β).1 = 1 := rfl @[simp] lemma snd_one : (1 : α × β).2 = 1 := rfl end section variables [has_inv α] [has_inv β] instance : has_inv (α × β) := ⟨λp, (p.1⁻¹, p.2⁻¹)⟩ @[simp] lemma fst_inv : (p⁻¹).1 = (p.1)⁻¹ := rfl @[simp] lemma snd_inv : (p⁻¹).2 = (p.2)⁻¹ := rfl end instance [semigroup α] [semigroup β] : semigroup (α × β) := { mul_assoc := assume a b c, mk.inj_iff.mpr ⟨mul_assoc _ _ _, mul_assoc _ _ _⟩, .. prod.has_mul } instance [monoid α] [monoid β] : monoid (α × β) := { one_mul := assume a, prod.rec_on a $ λa b, mk.inj_iff.mpr ⟨one_mul _, one_mul _⟩, mul_one := assume a, prod.rec_on a $ λa b, mk.inj_iff.mpr ⟨mul_one _, mul_one _⟩, .. prod.semigroup, .. prod.has_one } instance [group α] [group β] : group (α × β) := { mul_left_inv := assume a, mk.inj_iff.mpr ⟨mul_left_inv _, mul_left_inv _⟩, .. prod.monoid, .. prod.has_inv } instance [comm_semigroup α] [comm_semigroup β] : comm_semigroup (α × β) := { mul_comm := assume a b, mk.inj_iff.mpr ⟨mul_comm _ _, mul_comm _ _⟩, .. prod.semigroup } instance [comm_monoid α] [comm_monoid β] : comm_monoid (α × β) := { mul_comm := assume a b, mk.inj_iff.mpr ⟨mul_comm _ _, mul_comm _ _⟩, .. prod.monoid } instance [comm_group α] [comm_group β] : comm_group (α × β) := { mul_comm := assume a b, mk.inj_iff.mpr ⟨mul_comm _ _, mul_comm _ _⟩, .. prod.group } lemma fst_prod [comm_monoid α] [comm_monoid β] {t : finset γ} {f : γ → α × β} : (t.prod f).1 = t.prod (λc, (f c).1) := (finset.prod_hom prod.fst (show (1:α×β).1 = 1, from rfl) $ assume x y, rfl).symm lemma snd_prod [comm_monoid α] [comm_monoid β] {t : finset γ} {f : γ → α × β} : (t.prod f).2 = t.prod (λc, (f c).2) := (finset.prod_hom prod.snd (show (1:α×β).2 = 1, from rfl) $ assume x y, rfl).symm section variables [has_add α] [has_add β] instance : has_add (α × β) := ⟨λp q, (p.1 + q.1, p.2 + q.2)⟩ attribute [to_additive prod.has_add] prod.has_mul attribute [to_additive prod.fst_add] prod.fst_mul attribute [to_additive prod.snd_add] prod.snd_mul attribute [simp] fst_add snd_add end section variables [has_zero α] [has_zero β] instance : has_zero (α × β) := ⟨(0, 0)⟩ attribute [to_additive prod.has_zero] prod.has_one attribute [to_additive prod.fst_zero] prod.fst_one attribute [to_additive prod.snd_zero] prod.snd_one attribute [simp] fst_zero snd_zero end section variables [has_neg α] [has_neg β] instance : has_neg (α × β) := ⟨λp, (- p.1, - p.2)⟩ attribute [to_additive prod.has_neg] prod.has_inv attribute [to_additive prod.fst_neg] prod.fst_inv attribute [to_additive prod.snd_neg] prod.snd_inv attribute [simp] fst_neg snd_neg end instance [add_semigroup α] [add_semigroup β] : add_semigroup (α × β) := { add_assoc := assume a b c, mk.inj_iff.mpr ⟨add_assoc _ _ _, add_assoc _ _ _⟩, .. prod.has_add } attribute [to_additive prod.add_semigroup] prod.semigroup instance [add_monoid α] [add_monoid β] : add_monoid (α × β) := { zero_add := assume ⟨a, b⟩, mk.inj_iff.mpr ⟨zero_add _, zero_add _⟩, add_zero := assume ⟨a, b⟩, mk.inj_iff.mpr ⟨add_zero _, add_zero _⟩, .. prod.add_semigroup, .. prod.has_zero } attribute [to_additive prod.add_monoid] prod.monoid instance [add_group α] [add_group β] : add_group (α × β) := { add_left_neg := assume a, mk.inj_iff.mpr ⟨add_left_neg _, add_left_neg _⟩, .. prod.add_monoid, .. prod.has_neg } attribute [to_additive prod.add_group] prod.group instance [add_comm_semigroup α] [add_comm_semigroup β] : add_comm_semigroup (α × β) := { add_comm := assume a b, mk.inj_iff.mpr ⟨add_comm _ _, add_comm _ _⟩, .. prod.add_semigroup } attribute [to_additive prod.add_comm_semigroup] prod.comm_semigroup instance [add_comm_monoid α] [add_comm_monoid β] : add_comm_monoid (α × β) := { add_comm := assume a b, mk.inj_iff.mpr ⟨add_comm _ _, add_comm _ _⟩, .. prod.add_monoid } attribute [to_additive prod.add_comm_monoid] prod.comm_monoid instance [add_comm_group α] [add_comm_group β] : add_comm_group (α × β) := { add_comm := assume a b, mk.inj_iff.mpr ⟨add_comm _ _, add_comm _ _⟩, .. prod.add_group } attribute [to_additive prod.add_comm_group] prod.comm_group attribute [to_additive prod.fst_sum] prod.fst_prod attribute [to_additive prod.snd_sum] prod.snd_prod /- TODO: this fils with a code generation error attribute [to_additive add_semigroup.add] semigroup.mul attribute [to_additive add_monoid.one] monoid.one -- code generation error: unexpected macro found attribute [to_additive add_monoid.add] monoid.mul attribute [to_additive add_monoid.add_assoc] monoid.mul_assoc attribute [to_additive prod.add_semigroup._proof_1] prod.semigroup._proof_1 attribute [to_additive prod.add_semigroup] prod.semigroup attribute [instance] prod.add_semigroup attribute [to_additive prod.add_monoid._proof_1] prod.monoid._proof_1 attribute [to_additive prod.add_monoid._proof_2] prod.monoid._proof_2 attribute [to_additive prod.add_monoid._proof_3] prod.monoid._proof_3 attribute [to_additive prod.add_monoid] prod.monoid attribute [to_additive prod.add_group._proof_1] prod.group._proof_1 attribute [to_additive prod.add_group] prod.group -/ /-- Left injection function for the inner product From a vector space (and also group and module) perspective the product is the same as the sum of two vector spaces. `inl` and `inr` provide the corresponding injection functions. -/ def inl [has_zero β] (a : α) : α × β := (a, 0) /-- Right injection function for the inner product -/ def inr [has_zero α] (b : β) : α × β := (0, b) lemma injective_inl [has_zero β] : injective (inl : α → α × β) := assume x y h, (prod.mk.inj_iff.mp h).1 lemma injective_inr [has_zero α] : injective (inr : β → α × β) := assume x y h, (prod.mk.inj_iff.mp h).2 @[simp] lemma inl_eq_inl [has_zero β] {a₁ a₂ : α} : (inl a₁ : α × β) = inl a₂ ↔ a₁ = a₂ := iff.intro (assume h, injective_inl h) (assume h, h ▸ rfl) @[simp] lemma inr_eq_inr [has_zero α] {b₁ b₂ : β} : (inr b₁ : α × β) = inr b₂ ↔ b₁ = b₂ := iff.intro (assume h, injective_inr h) (assume h, h ▸ rfl) @[simp] lemma inl_eq_inr [has_zero α] [has_zero β] {a : α} {b : β} : (inl a = inr b) ↔ (a = 0 ∧ b = 0) := by constructor; simp [inl, inr] {contextual := tt} @[simp] lemma inr_eq_inl [has_zero α] [has_zero β] {a : α} {b : β} : (inr b = inl a) ↔ (a = 0 ∧ b = 0) := by constructor; simp [inl, inr] {contextual := tt} @[simp] lemma fst_inl [has_zero β] (a : α) : (inl a : α × β).1 = a := rfl @[simp] lemma snd_inl [has_zero β] (a : α) : (inl a : α × β).2 = 0 := rfl @[simp] lemma fst_inr [has_zero α] (b : β) : (inr b : α × β).1 = 0 := rfl @[simp] lemma snd_inr [has_zero α] (b : β) : (inr b : α × β).2 = b := rfl instance [has_scalar α β] [has_scalar α γ] : has_scalar α (β × γ) := ⟨λa p, (a • p.1, a • p.2)⟩ section module variables [ring α] [module α β] [module α γ] [module α δ] include α instance : module α (β × γ) := { smul_add := assume a p₁ p₂, mk.inj_iff.mpr ⟨smul_add, smul_add⟩, add_smul := assume a p₁ p₂, mk.inj_iff.mpr ⟨add_smul, add_smul⟩, mul_smul := assume a₁ a₂ p, mk.inj_iff.mpr ⟨mul_smul, mul_smul⟩, one_smul := assume ⟨b, c⟩, mk.inj_iff.mpr ⟨one_smul, one_smul⟩, .. prod.has_scalar } lemma is_linear_map_prod_fst : is_linear_map (prod.fst : β × γ → β) := ⟨assume x y, rfl, assume x y, rfl⟩ lemma is_linear_map_prod_snd : is_linear_map (prod.snd : β × γ → γ) := ⟨assume x y, rfl, assume x y, rfl⟩ lemma is_linear_map_prod_mk {f : δ → β} {g : δ → γ} (hf : is_linear_map f) (hg : is_linear_map g): is_linear_map (λd, (f d, g d)) := ⟨assume x y, mk.inj_iff.mpr ⟨hf.add _ _, hg.add _ _⟩, assume x y, mk.inj_iff.mpr ⟨hf.smul _ _, hg.smul _ _⟩⟩ lemma is_linear_map_prod_inl : is_linear_map (prod.inl : β → β × γ) := is_linear_map_prod_mk is_linear_map.id is_linear_map.map_zero lemma is_linear_map_prod_inr : is_linear_map (prod.inr : γ → β × γ) := is_linear_map_prod_mk is_linear_map.map_zero is_linear_map.id instance {s : set β} {t : set γ} [is_submodule s] [is_submodule t] : is_submodule (set.prod s t) := @is_submodule.inter_submodule _ _ _ _ _ _ (is_submodule.preimage is_linear_map_prod_fst) (is_submodule.preimage is_linear_map_prod_snd) lemma span_prod {s : set β} {t : set γ} : span (set.prod s t) ⊆ set.prod (span s) (span t) := span_minimal prod.is_submodule (set.prod_mono subset_span subset_span) lemma span_inl_union_inr {s : set β} {t : set γ} : span (inl '' s ∪ inr '' t) = set.prod (span s) (span t) := span_eq prod.is_submodule (set.union_subset (set.image_subset_iff.mpr $ assume b hbs, ⟨subset_span hbs, is_submodule.zero⟩) (set.image_subset_iff.mpr $ assume c hct, ⟨is_submodule.zero, subset_span hct⟩)) (assume ⟨b, c⟩ ⟨hb, hc⟩, begin simp [span_union, span_image_of_linear_map is_linear_map_prod_inl, span_image_of_linear_map is_linear_map_prod_inr], exact ⟨b, 0, ⟨b, hb, rfl⟩, 0, c, ⟨c, hc, rfl⟩, mk.inj_iff.mpr ⟨(add_zero _).symm, (zero_add _).symm⟩⟩ end) lemma linear_independent_inl_union_inr {s : set β} {t : set γ} (hs : linear_independent s) (ht : linear_independent t) : linear_independent (inl '' s ∪ inr '' t) := linear_independent_union (hs.image is_linear_map_prod_inl $ assume a ha b hb eq, injective_inl eq) (ht.image is_linear_map_prod_inr $ assume a ha b hb eq, injective_inr eq) (subset.antisymm (by rw [span_image_of_linear_map is_linear_map_prod_inl, span_image_of_linear_map is_linear_map_prod_inr]; exact assume ⟨b, c⟩ ⟨⟨b', hb', beq⟩, ⟨c', hc', ceq⟩⟩, have c = 0, from (prod.mk.inj beq).2.symm, have b = 0, from (prod.mk.inj ceq).1.symm, by simp *; refl) (by simp [is_submodule.zero])) lemma is_basis_inl_union_inr {s : set β} {t : set γ} (hs : is_basis s) (ht : is_basis t) : is_basis (inl '' s ∪ inr '' t) := ⟨linear_independent_inl_union_inr hs.1 ht.1, by rw [span_inl_union_inr]; exact assume ⟨b, c⟩, ⟨hs.2 b, ht.2 c⟩⟩ end module instance [field α] [vector_space α β] [vector_space α γ] : vector_space α (β × γ) := {..prod.module} end prod
2aa439177bcc5f92f276a1aecc13f74e266be62f
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Elab/Tactic/ElabTerm.lean
ece241580fe6f72fef4db6eb59a5e02a160bc18a
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,695
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.Meta.CollectMVars import Lean.Meta.Tactic.Apply import Lean.Meta.Tactic.Assert import Lean.Elab.Tactic.Basic import Lean.Elab.SyntheticMVars namespace Lean namespace Elab namespace Tactic open Meta /- `elabTerm` for Tactics and basic tactics that use it. -/ def elabTerm (stx : Syntax) (expectedType? : Option Expr) (mayPostpone := false) : TacticM Expr := withRef stx $ liftTermElabM $ adaptReader (fun (ctx : Term.Context) => { ctx with errToSorry := false }) $ do e ← Term.elabTerm stx expectedType?; Term.synthesizeSyntheticMVars mayPostpone; instantiateMVars e def elabTermEnsuringType (stx : Syntax) (expectedType? : Option Expr) (mayPostpone := false) : TacticM Expr := do e ← elabTerm stx expectedType? mayPostpone; ensureHasType expectedType? e @[builtinTactic «exact»] def evalExact : Tactic := fun stx => match_syntax stx with | `(tactic| exact $e) => do (g, gs) ← getMainGoal; withMVarContext g $ do { decl ← getMVarDecl g; val ← elabTermEnsuringType e decl.type; ensureHasNoMVars val; assignExprMVar g val }; setGoals gs | _ => throwUnsupportedSyntax @[builtinMacro Lean.Parser.Tactic.admit] def expandAdmit : Macro := fun _ => `(tactic| exact sorry) def elabTermWithHoles (stx : Syntax) (expectedType? : Option Expr) (tagSuffix : Name) (allowNaturalHoles := false) : TacticM (Expr × List MVarId) := do val ← elabTermEnsuringType stx expectedType?; newMVarIds ← getMVarsNoDelayed val; newMVarIds ← if allowNaturalHoles then pure newMVarIds.toList else do { naturalMVarIds ← newMVarIds.filterM fun mvarId => do { mvarDecl ← getMVarDecl mvarId; pure mvarDecl.kind.isNatural }; syntheticMVarIds ← newMVarIds.filterM fun mvarId => do { mvarDecl ← getMVarDecl mvarId; pure $ !mvarDecl.kind.isNatural }; liftM $ Term.logUnassignedUsingErrorInfos naturalMVarIds; pure syntheticMVarIds.toList }; tag ← getMainTag; tagUntaggedGoals tag tagSuffix newMVarIds; pure (val, newMVarIds) /- If `allowNaturalHoles == true`, then we allow the resultant expression to contain unassigned "natural" metavariables. Recall that "natutal" metavariables are created for explicit holes `_` and implicit arguments. They are meant to be filled by typing constraints. "Synthetic" metavariables are meant to be filled by tactics and are usually created using the synthetic hole notation `?<hole-name>`. -/ def refineCore (stx : Syntax) (tagSuffix : Name) (allowNaturalHoles : Bool) : TacticM Unit := do (g, gs) ← getMainGoal; withMVarContext g do decl ← getMVarDecl g; (val, gs') ← elabTermWithHoles stx decl.type tagSuffix allowNaturalHoles; assignExprMVar g val; setGoals (gs ++ gs') @[builtinTactic «refine»] def evalRefine : Tactic := fun stx => match_syntax stx with | `(tactic| refine $e) => refineCore e `refine false | _ => throwUnsupportedSyntax @[builtinTactic «refine!»] def evalRefineBang : Tactic := fun stx => match_syntax stx with | `(tactic| refine! $e) => refineCore e `refine true | _ => throwUnsupportedSyntax @[builtinTactic Lean.Parser.Tactic.apply] def evalApply : Tactic := fun stx => match_syntax stx with | `(tactic| apply $e) => do (g, gs) ← getMainGoal; gs' ← withMVarContext g $ do { decl ← getMVarDecl g; val ← elabTerm e none true; gs' ← liftMetaM $ Meta.apply g val; liftTermElabM $ Term.synthesizeSyntheticMVarsNoPostponing; pure gs' }; -- TODO: handle optParam and autoParam setGoals (gs' ++ gs) | _ => throwUnsupportedSyntax /-- Elaborate `stx`. If it a free variable, return it. Otherwise, assert it, and return the free variable. Note that, the main goal is updated when `Meta.assert` is used in the second case. -/ def elabAsFVar (stx : Syntax) (userName? : Option Name := none) : TacticM FVarId := do (mvarId, others) ← getMainGoal; withMVarContext mvarId $ do e ← elabTerm stx none; match e with | Expr.fvar fvarId _ => pure fvarId | _ => do type ← inferType e; let intro (userName : Name) (preserveBinderNames : Bool) : TacticM FVarId := do { (fvarId, mvarId) ← liftMetaM do { mvarId ← Meta.assert mvarId userName type e; Meta.intro1Core mvarId preserveBinderNames }; setGoals $ mvarId::others; pure fvarId }; match userName? with | none => intro `h false | some userName => intro userName true end Tactic end Elab end Lean
cc679b3dd6f6eacada4378cc1c19558b40e46da5
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/playground/add_zeros.lean
7803e94bf4fb0d3f1f80ddeeeeb258d2d800c602
[ "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
364
lean
def add (n m : Nat) : Nat := n + m @[simp] theorem addZero x : add 0 x = x := by simp [add] syntax "bigAdd0Seq! " num : term macro_rules | `(bigAdd0Seq! $n) => let n := n.toNat if n == 0 then `(0) else `(add 0 (bigAdd0Seq! $(Lean.quote (n - 1)))) set_option maxRecDepth 10000 theorem ex : bigAdd0Seq! 20 = 0 := by simp #print ex
8d4c49b94bda7b51f319321cf959e91515fab198
4727251e0cd73359b15b664c3170e5d754078599
/src/data/finset/pi.lean
cf2fe7e7c48bcd6400a8f746fedae75f325bfa33
[ "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
4,411
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import data.finset.basic import data.multiset.pi /-! # The cartesian product of finsets -/ namespace finset open multiset /-! ### pi -/ section pi variables {α : Type*} /-- The empty dependent product function, defined on the empty set. The assumption `a ∈ ∅` is never satisfied. -/ def pi.empty (β : α → Sort*) (a : α) (h : a ∈ (∅ : finset α)) : β a := multiset.pi.empty β a h variables {δ : α → Type*} [decidable_eq α] /-- Given a finset `s` of `α` and for all `a : α` a finset `t a` of `δ a`, then one can define the finset `s.pi t` of all functions defined on elements of `s` taking values in `t a` for `a ∈ s`. Note that the elements of `s.pi t` are only partially defined, on `s`. -/ def pi (s : finset α) (t : Πa, finset (δ a)) : finset (Πa∈s, δ a) := ⟨s.1.pi (λ a, (t a).1), s.nodup.pi $ λ a _, (t a).nodup⟩ @[simp] lemma pi_val (s : finset α) (t : Πa, finset (δ a)) : (s.pi t).1 = s.1.pi (λ a, (t a).1) := rfl @[simp] lemma mem_pi {s : finset α} {t : Πa, finset (δ a)} {f : Πa∈s, δ a} : f ∈ s.pi t ↔ (∀a (h : a ∈ s), f a h ∈ t a) := mem_pi _ _ _ /-- Given a function `f` defined on a finset `s`, define a new function on the finset `s ∪ {a}`, equal to `f` on `s` and sending `a` to a given value `b`. This function is denoted `s.pi.cons a b f`. If `a` already belongs to `s`, the new function takes the value `b` at `a` anyway. -/ def pi.cons (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (a' : α) (h : a' ∈ insert a s) : δ a' := multiset.pi.cons s.1 a b f _ (multiset.mem_cons.2 $ mem_insert.symm.2 h) @[simp] lemma pi.cons_same (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (h : a ∈ insert a s) : pi.cons s a b f a h = b := multiset.pi.cons_same _ lemma pi.cons_ne {s : finset α} {a a' : α} {b : δ a} {f : Πa, a ∈ s → δ a} {h : a' ∈ insert a s} (ha : a ≠ a') : pi.cons s a b f a' h = f a' ((mem_insert.1 h).resolve_left ha.symm) := multiset.pi.cons_ne _ _ lemma pi_cons_injective {a : α} {b : δ a} {s : finset α} (hs : a ∉ s) : function.injective (pi.cons s a b) := assume e₁ e₂ eq, @multiset.pi_cons_injective α _ δ a b s.1 hs _ _ $ funext $ assume e, funext $ assume h, have pi.cons s a b e₁ e (by simpa only [multiset.mem_cons, mem_insert] using h) = pi.cons s a b e₂ e (by simpa only [multiset.mem_cons, mem_insert] using h), { rw [eq] }, this @[simp] lemma pi_empty {t : Πa:α, finset (δ a)} : pi (∅ : finset α) t = singleton (pi.empty δ) := rfl @[simp] lemma pi_insert [∀a, decidable_eq (δ a)] {s : finset α} {t : Πa:α, finset (δ a)} {a : α} (ha : a ∉ s) : pi (insert a s) t = (t a).bUnion (λb, (pi s t).image (pi.cons s a b)) := begin apply eq_of_veq, rw ← (pi (insert a s) t).2.dedup, refine (λ s' (h : s' = a ::ₘ s.1), (_ : dedup (multiset.pi s' (λ a, (t a).1)) = dedup ((t a).1.bind $ λ b, dedup $ (multiset.pi s.1 (λ (a : α), (t a).val)).map $ λ f a' h', multiset.pi.cons s.1 a b f a' (h ▸ h')))) _ (insert_val_of_not_mem ha), subst s', rw pi_cons, congr, funext b, exact ((pi s t).nodup.map $ multiset.pi_cons_injective ha).dedup.symm, end lemma pi_singletons {β : Type*} (s : finset α) (f : α → β) : s.pi (λ a, ({f a} : finset β)) = {λ a _, f a} := begin rw eq_singleton_iff_unique_mem, split, { simp }, intros a ha, ext i hi, rw [mem_pi] at ha, simpa using ha i hi, end lemma pi_const_singleton {β : Type*} (s : finset α) (i : β) : s.pi (λ _, ({i} : finset β)) = {λ _ _, i} := pi_singletons s (λ _, i) lemma pi_subset {s : finset α} (t₁ t₂ : Πa, finset (δ a)) (h : ∀ a ∈ s, t₁ a ⊆ t₂ a) : s.pi t₁ ⊆ s.pi t₂ := λ g hg, mem_pi.2 $ λ a ha, h a ha (mem_pi.mp hg a ha) lemma pi_disjoint_of_disjoint {δ : α → Type*} [∀a, decidable_eq (δ a)] {s : finset α} [decidable_eq (Πa∈s, δ a)] (t₁ t₂ : Πa, finset (δ a)) {a : α} (ha : a ∈ s) (h : disjoint (t₁ a) (t₂ a)) : disjoint (s.pi t₁) (s.pi t₂) := disjoint_iff_ne.2 $ λ f₁ hf₁ f₂ hf₂ eq₁₂, disjoint_iff_ne.1 h (f₁ a ha) (mem_pi.mp hf₁ a ha) (f₂ a ha) (mem_pi.mp hf₂ a ha) $ congr_fun (congr_fun eq₁₂ a) ha end pi end finset
e9b57b9afef780b568c321b229dc553fbba43fac
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/03_Propositions_and_Proofs.org.7.lean
ef81c5900511f75a0fb681bdaa4e8ed5cf1eda1a
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
132
lean
/- page 36 -/ import standard constants p q : Prop -- BEGIN theorem t1 : p → q → p := assume Hp : p, assume Hq : q, Hp -- END
df374b5f601013d796a5779256bbfa12d027421a
1446f520c1db37e157b631385707cc28a17a595e
/stage0/src/Init/System/IO.lean
92dffbaa5cfb1ed8eb8874e7e11079f27bd2e228
[ "Apache-2.0" ]
permissive
bdbabiak/lean4
cab06b8a2606d99a168dd279efdd404edb4e825a
3f4d0d78b2ce3ef541cb643bbe21496bd6b057ac
refs/heads/master
1,615,045,275,530
1,583,793,696,000
1,583,793,696,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,434
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, Leonardo de Moura, Sebastian Ullrich -/ prelude import Init.Control.EState import Init.Data.String.Basic import Init.Data.ByteArray import Init.System.IOError import Init.System.FilePath /-- Like https://hackage.haskell.org/package/ghc-Prim-0.5.2.0/docs/GHC-Prim.html#t:RealWorld. Makes sure we never reorder `IO` operations. TODO: mark opaque -/ def IO.RealWorld : Type := Unit /- TODO(Leo): mark it as an opaque definition. Reason: prevent functions defined in other modules from accessing `IO.RealWorld`. We don't want action such as ``` def getWorld : IO (IO.RealWorld) := get ``` -/ def EIO (ε : Type) : Type → Type := EStateM ε IO.RealWorld @[inline] def EIO.adaptExcept {α ε ε'} (f : ε → ε') (x : EIO ε α) : EIO ε' α := EStateM.adaptExcept f x @[inline] def EIO.catchExceptions {α ε} (x : EIO ε α) (h : ε → EIO Empty α) : EIO Empty α := fun s => match x s with | EStateM.Result.ok a s => EStateM.Result.ok a s | EStateM.Result.error ex s => h ex s instance (ε : Type) : Monad (EIO ε) := inferInstanceAs (Monad (EStateM ε IO.RealWorld)) instance (ε : Type) : MonadExcept ε (EIO ε) := inferInstanceAs (MonadExcept ε (EStateM ε IO.RealWorld)) instance (α ε : Type) : HasOrelse (EIO ε α) := ⟨MonadExcept.orelse⟩ instance {ε : Type} {α : Type} [Inhabited ε] : Inhabited (EIO ε α) := inferInstanceAs (Inhabited (EStateM ε IO.RealWorld α)) abbrev IO : Type → Type := EIO IO.Error section /- After we inline `EState.run'`, the closed term `((), ())` is generated, where the second `()` represents the "initial world". We don't want to cache this closed term. So, we disable the "extract closed terms" optimization. -/ set_option compiler.extract_closed false @[inline] unsafe def unsafeIO {α : Type} (fn : IO α) : Except IO.Error α := match fn.run () with | EStateM.Result.ok a _ => Except.ok a | EStateM.Result.error e _ => Except.error e end @[extern "lean_io_timeit"] constant timeit {α : Type} (msg : @& String) (fn : IO α) : IO α := arbitrary _ @[extern "lean_io_allocprof"] constant allocprof {α : Type} (msg : @& String) (fn : IO α) : IO α := arbitrary _ /- Programs can execute IO actions during initialization that occurs before the `main` function is executed. The attribute `[init <action>]` specifies which IO action is executed to set the value of an opaque constant. The action `initializing` returns `true` iff it is invoked during initialization. -/ @[extern "lean_io_initializing"] constant IO.initializing : IO Bool := arbitrary _ abbrev MonadIO (m : Type → Type) := HasMonadLiftT IO m namespace IO def ofExcept {ε α : Type} [HasToString ε] (e : Except ε α) : IO α := match e with | Except.ok a => pure a | Except.error e => throw (IO.userError (toString e)) def lazyPure {α : Type} (fn : Unit → α) : IO α := pure (fn ()) inductive FS.Mode | read | write | readWrite | append constant FS.Handle : Type := Unit namespace Prim open FS @[specialize] partial def iterate {α β : Type} : α → (α → IO (Sum α β)) → IO β | a, f => do v ← f a; match v with | Sum.inl a => iterate a f | Sum.inr b => pure b -- @[export lean_fopen_flags] def fopenFlags (m : FS.Mode) (b : Bool) : String := let mode := match m with | FS.Mode.read => "r" | FS.Mode.write => "w" | FS.Mode.readWrite => "r+" | FS.Mode.append => "a" ; let bin := if b then "b" else "t"; mode ++ bin @[extern "lean_io_prim_put_str"] constant putStr (s: @& String) : IO Unit := arbitrary _ @[extern "lean_io_prim_read_text_file"] constant readTextFile (s : @& String) : IO String := arbitrary _ @[extern "lean_io_prim_get_line"] constant getLine : IO String := arbitrary _ @[extern "lean_io_prim_handle_mk"] constant Handle.mk (s : @& String) (mode : @& String) : IO Handle := arbitrary _ @[extern "lean_io_prim_handle_is_eof"] constant Handle.isEof (h : @& Handle) : IO Bool := arbitrary _ @[extern "lean_io_prim_handle_flush"] constant Handle.flush (h : @& Handle) : IO Unit := arbitrary _ -- TODO: replace `String` with byte buffer @[extern "lean_io_prim_handle_read"] constant Handle.read (h : @& Handle) (bytes : USize) : IO ByteArray := arbitrary _ @[extern "lean_io_prim_handle_write"] constant Handle.write (h : @& Handle) (buffer : @& ByteArray) : IO Unit := arbitrary _ @[extern "lean_io_prim_handle_read_byte"] constant Handle.getByte (h : @& Handle) : IO UInt8 := arbitrary _ @[extern "lean_io_prim_handle_write_byte"] constant Handle.putByte (h : @& Handle) (c : UInt8) : IO Unit := arbitrary _ @[extern "lean_io_prim_handle_get_line"] constant Handle.getLine (h : @& Handle) : IO String := arbitrary _ @[extern "lean_io_prim_handle_put_str"] constant Handle.putStr (h : @& Handle) (s : @& String) : IO Unit := arbitrary _ @[extern "lean_io_getenv"] constant getEnv (var : @& String) : IO (Option String) := arbitrary _ @[extern "lean_io_realpath"] constant realPath (fname : String) : IO String := arbitrary _ @[extern "lean_io_is_dir"] constant isDir (fname : @& String) : IO Bool := arbitrary _ @[extern "lean_io_file_exists"] constant fileExists (fname : @& String) : IO Bool := arbitrary _ @[extern "lean_io_app_dir"] constant appPath : IO String := arbitrary _ @[inline] def liftIO {m : Type → Type} {α : Type} [MonadIO m] (x : IO α) : m α := monadLift x end Prim section variables {m : Type → Type} [Monad m] [MonadIO m] private def putStr : String → m Unit := Prim.liftIO ∘ Prim.putStr def print {α} [HasToString α] (s : α) : m Unit := putStr ∘ toString $ s def println {α} [HasToString α] (s : α) : m Unit := print s *> putStr "\n" def readTextFile : String → m String := Prim.liftIO ∘ Prim.readTextFile def getEnv : String → m (Option String) := Prim.liftIO ∘ Prim.getEnv def realPath : String → m String := Prim.liftIO ∘ Prim.realPath def isDir : String → m Bool := Prim.liftIO ∘ Prim.isDir def fileExists : String → m Bool := Prim.liftIO ∘ Prim.fileExists def appPath : m String := Prim.liftIO Prim.appPath def appDir : m String := do p ← appPath; realPath (System.FilePath.dirName p) end namespace FS variables {m : Type → Type} [Monad m] [MonadIO m] def Handle.mk (s : String) (Mode : Mode) (bin : Bool := false) : m Handle := Prim.liftIO (Prim.Handle.mk s (Prim.fopenFlags Mode bin)) @[inline] def withFile {α} (fn : String) (m : Mode) (f : Handle → IO α) : IO α := Handle.mk fn m >>= f /-- returns whether the end of the file has been reached while reading a file. `h.isEof` returns true /after/ the first attempt at reading past the end of `h`. Once `h.isEof` is true, the reading `h` raises `IO.Error.eof`. -/ def Handle.isEof : Handle → m Bool := Prim.liftIO ∘ Prim.Handle.isEof def Handle.flush : Handle → m Unit := Prim.liftIO ∘ Prim.Handle.flush def Handle.read (h : Handle) (bytes : Nat) : m ByteArray := Prim.liftIO (Prim.Handle.read h (USize.ofNat bytes)) def Handle.write (h : Handle) (s : ByteArray) : m Unit := Prim.liftIO (Prim.Handle.write h s) def Handle.getByte (h : Handle) : m UInt8 := Prim.liftIO (Prim.Handle.getByte h) def Handle.putByte (h : Handle) (b : UInt8) : m Unit := Prim.liftIO (Prim.Handle.putByte h b) def Handle.getLine : Handle → m String := Prim.liftIO ∘ Prim.Handle.getLine def Handle.putStr (h : Handle) (s : String) : m Unit := Prim.liftIO $ Prim.Handle.putStr h s def Handle.putStrLn (h : Handle) (s : String) : m Unit := h.putStr s *> h.putStr "\n" def Handle.readToEnd (h : Handle) : m String := Prim.liftIO $ Prim.iterate "" $ fun r => do done ← h.isEof; if done then pure (Sum.inr r) -- stop else do -- HACK: use less efficient `getLine` while `read` is broken c ← h.getLine; pure $ Sum.inl (r ++ c) -- continue def readFile (fname : String) (bin := false) : m String := do h ← Handle.mk fname Mode.read bin; r ← h.readToEnd; pure r end FS -- constant stdin : IO FS.Handle -- constant stderr : IO FS.Handle -- constant stdout : IO FS.Handle /- namespace Proc def child : Type := MonadIOProcess.child ioCore def child.stdin : child → Handle := MonadIOProcess.stdin def child.stdout : child → Handle := MonadIOProcess.stdout def child.stderr : child → Handle := MonadIOProcess.stderr def spawn (p : IO.process.spawnArgs) : IO child := MonadIOProcess.spawn ioCore p def wait (c : child) : IO Nat := MonadIOProcess.wait c end Proc -/ structure AccessRight := (read write execution : Bool := false) def AccessRight.flags (acc : AccessRight) : UInt32 := let r : UInt32 := if acc.read then 0x4 else 0; let w : UInt32 := if acc.write then 0x2 else 0; let x : UInt32 := if acc.execution then 0x1 else 0; r.lor $ w.lor x structure FileRight := (user group other : AccessRight := { }) def FileRight.flags (acc : FileRight) : UInt32 := let u : UInt32 := acc.user.flags.shiftLeft 6; let g : UInt32 := acc.group.flags.shiftLeft 3; let o : UInt32 := acc.other.flags; u.lor $ g.lor o @[extern "lean_chmod"] constant Prim.setAccessRights (filename : @& String) (mode : UInt32) : IO Unit := arbitrary _ def setAccessRights (filename : String) (mode : FileRight) : IO Unit := Prim.setAccessRights filename mode.flags /- References -/ constant RefPointed (α : Type) : PointedType := arbitrary _ def Ref (α : Type) : Type := (RefPointed α).type instance (α : Type) : Inhabited (Ref α) := ⟨(RefPointed α).val⟩ namespace Prim @[extern "lean_io_mk_ref"] constant mkRef {α : Type} (a : α) : IO (Ref α) := arbitrary _ @[extern "lean_io_ref_get"] constant Ref.get {α : Type} (r : @& Ref α) : IO α := arbitrary _ @[extern "lean_io_ref_set"] constant Ref.set {α : Type} (r : @& Ref α) (a : α) : IO Unit := arbitrary _ @[extern "lean_io_ref_swap"] constant Ref.swap {α : Type} (r : @& Ref α) (a : α) : IO α := arbitrary _ @[extern "lean_io_ref_reset"] constant Ref.reset {α : Type} (r : @& Ref α) : IO Unit := arbitrary _ @[extern "lean_io_ref_ptr_eq"] constant Ref.ptrEq {α : Type} (r1 r2 : @& Ref α) : IO Bool := arbitrary _ end Prim section variables {m : Type → Type} [Monad m] [MonadIO m] @[inline] def mkRef {α : Type} (a : α) : m (Ref α) := Prim.liftIO (Prim.mkRef a) @[inline] def Ref.get {α : Type} (r : Ref α) : m α := Prim.liftIO (Prim.Ref.get r) @[inline] def Ref.set {α : Type} (r : Ref α) (a : α) : m Unit := Prim.liftIO (Prim.Ref.set r a) @[inline] def Ref.swap {α : Type} (r : Ref α) (a : α) : m α := Prim.liftIO (Prim.Ref.swap r a) @[inline] def Ref.reset {α : Type} (r : Ref α) : m Unit := Prim.liftIO (Prim.Ref.reset r) @[inline] def Ref.ptrEq {α : Type} (r1 r2 : Ref α) : m Bool := Prim.liftIO (Prim.Ref.ptrEq r1 r2) @[inline] def Ref.modify {α : Type} (r : Ref α) (f : α → α) : m Unit := do v ← r.get; r.reset; r.set (f v) end end IO universe u namespace Lean /-- Typeclass used for presenting the output of an `#eval` command. -/ class HasEval (α : Type u) := (eval : α → IO Unit) instance HasRepr.HasEval {α : Type u} [HasRepr α] : HasEval α := ⟨fun a => IO.println (repr a)⟩ instance IO.HasEval {α : Type} [HasEval α] : HasEval (IO α) := ⟨fun x => do a ← x; HasEval.eval a⟩ -- special case: do not print `()` instance IOUnit.HasEval : HasEval (IO Unit) := ⟨fun x => x⟩ end Lean
8220d3ffe3ec91f86832d9b788769f0e7b08d8b9
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/07_Induction_and_Recursion.org.19.lean
802746a14e2fcde2367c2600624d55eb065112b6
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
236
lean
/- page 107 -/ import standard variables {A B : Type} inductive image_of (f : A → B) : B → Type := imf : Π a, image_of f (f a) open image_of definition inv {f : A → B} : Π b, image_of f b → A | inv ⌞f a⌟ (imf f a) := a
f2fe43afd94610d4b9b1127d5f3d8b9e11e6c978
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/lean/run/unif_issue2.lean
4aa245c35915f9f1b22724d509d7196ae743ae56
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
332
lean
import Init.Lean new_frontend open Lean #check @StateT.run Nat Id Nat (let f := fun (x : Unit) => pure 0; f ()) 0 #check Id.run $ StateT.run (let x := fun _ => pure 0; x ()) 0 #check @StateT.run Nat Id (List Nat) (let x := fun _ => pure [0]; x ()) 0 #check @Unhygienic.run Syntax $ let x := fun _ => pure Syntax.missing; x ()
d3df0014a357579e122d32318156672565b78918
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/sets_functions_and_relations/unnamed_976.lean
a3969cc3fb6df137e53d7af1f40adacfa4b94ea8
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
502
lean
import data.set.lattice open set function -- BEGIN variables {α β I : Type*} variable f : α → β variable A : I → set α variable B : I → set β example : f '' (⋃ i, A i) = ⋃ i, f '' A i := sorry example : f '' (⋂ i, A i) ⊆ ⋂ i, f '' A i := sorry example (i : I) (injf : injective f) : (⋂ i, f '' A i) ⊆ f '' (⋂ i, A i) := sorry example : f ⁻¹' (⋃ i, B i) = ⋃ i, f ⁻¹' (B i) := sorry example : f ⁻¹' (⋂ i, B i) = ⋂ i, f ⁻¹' (B i) := sorry -- END
46f591e712945dc5e8e86be563af09d8f8ebe82c
a4673261e60b025e2c8c825dfa4ab9108246c32e
/stage0/src/Lean/Server/Snapshots.lean
524d75a022930c0c8d2b1c730d73e7e54556a81b
[ "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
4,400
lean
/- Copyright (c) 2020 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki -/ import Init.System.IO import Lean.Elab.Import import Lean.Elab.Command /-! One can think of this module as being a partial reimplementation of Lean.Elab.Frontend which also stores a snapshot of the world after each command. Importantly, we allow (re)starting compilation from any snapshot/position in the file for interactive editing purposes. -/ namespace Lean namespace Server namespace Snapshots open Elab /-- The data associated with a snapshot is different depending on whether it was produced from the header or from a command. -/ inductive SnapshotData := | headerData : Environment → MessageLog → Options → SnapshotData | cmdData : Command.State → SnapshotData /-- What Lean knows about the world after the header and each command. -/ structure Snapshot := /- Where the command which produced this snapshot begins. Note that neighbouring snapshots are *not* necessarily attached beginning-to-end, since inputs outside the grammar advance the parser but do not produce snapshots. -/ (beginPos : String.Pos) (mpState : Parser.ModuleParserState) (data : SnapshotData) namespace Snapshot def endPos (s : Snapshot) : String.Pos := s.mpState.pos def env : Snapshot → Environment | ⟨_, _, SnapshotData.headerData env_ _ _⟩ => env_ | ⟨_, _, SnapshotData.cmdData cmdState⟩ => cmdState.env def msgLog : Snapshot → MessageLog | ⟨_, _, SnapshotData.headerData _ msgLog_ _⟩ => msgLog_ | ⟨_, _, SnapshotData.cmdData cmdState⟩ => cmdState.messages def toCmdState : Snapshot → Command.State | ⟨_, _, SnapshotData.headerData env msgLog opts⟩ => Command.mkState env msgLog opts | ⟨_, _, SnapshotData.cmdData cmdState⟩ => cmdState end Snapshot -- TODO(WN): fns here should probably take inputCtx and live -- in some SnapshotsM := ReaderT Context (EIO Empty) def compileHeader (contents : String) (opts : Options := {}) : IO Snapshot := do let inputCtx := Parser.mkInputContext contents "<input>" let (headerStx, headerParserState, msgLog) ← Parser.parseHeader inputCtx let (headerEnv, msgLog) ← Elab.processHeader headerStx opts msgLog inputCtx pure { beginPos := 0, mpState := headerParserState, data := SnapshotData.headerData headerEnv msgLog opts } private def ioErrorFromEmpty (ex : Empty) : IO.Error := nomatch ex /-- Compiles the next command occurring after the given snapshot. If there is no next command (file ended), returns messages produced through the file. -/ -- NOTE: This code is really very similar to Elab.Frontend. But generalizing it -- over "store snapshots"/"don't store snapshots" would likely result in confusing -- isServer? conditionals and not be worth it due to how short it is. def compileNextCmd (contents : String) (snap : Snapshot) : IO (Sum Snapshot MessageLog) := do let inputCtx := Parser.mkInputContext contents "<input>"; let (cmdStx, cmdParserState, msgLog) := Parser.parseCommand snap.env inputCtx snap.mpState snap.msgLog; let cmdPos := cmdStx.getHeadInfo.get!.pos.get!; -- TODO(WN): always `some`? if Parser.isEOI cmdStx || Parser.isExitCommand cmdStx then pure $ Sum.inr msgLog else let cmdStateRef ← IO.mkRef { snap.toCmdState with messages := msgLog } let cmdCtx : Elab.Command.Context := { cmdPos := snap.endPos, fileName := inputCtx.fileName, fileMap := inputCtx.fileMap } EIO.toIO ioErrorFromEmpty $ Elab.Command.catchExceptions (Elab.Command.elabCommand cmdStx) cmdCtx cmdStateRef let postCmdState ← cmdStateRef.get let postCmdSnap : Snapshot := { beginPos := cmdPos, mpState := cmdParserState, data := SnapshotData.cmdData postCmdState } pure $ Sum.inl postCmdSnap /-- Compiles all commands after the given snapshot. Returns them as a list, together with the final message log. -/ partial def compileCmdsAfter (contents : String) (snap : Snapshot) : IO (List Snapshot × MessageLog) := do let cmdOut ← compileNextCmd contents snap match cmdOut with | Sum.inl snap => do let (snaps, msgLog) ← compileCmdsAfter contents snap pure $ (snap :: snaps, msgLog) | Sum.inr msgLog => pure ([], msgLog) end Snapshots end Server end Lean
5c8d74ba79c59c0f29c4f654bf5780dc59fa7243
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/field_theory/algebraic_closure.lean
37863eaabe810d88582136ba41c6702ad34bfdee
[ "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
15,957
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.direct_limit import field_theory.splitting_field /-! # Algebraic Closure In this file we define the typeclass for algebraically closed fields and algebraic closures. We also construct an algebraic closure for any field. ## Main Definitions - `is_alg_closed k` is the typeclass saying `k` is an algebraically closed field, i.e. every polynomial in `k` splits. - `is_alg_closure k K` is the typeclass saying `K` is an algebraic closure of `k`. - `algebraic_closure k` is an algebraic closure of `k` (in the same universe). It is constructed by taking the polynomial ring generated by indeterminates `x_f` corresponding to monic irreducible polynomials `f` with coefficients in `k`, and quotienting out by a maximal ideal containing every `f(x_f)`, and then repeating this step countably many times. See Exercise 1.13 in Atiyah--Macdonald. ## TODO Show that any algebraic extension embeds into any algebraically closed extension (via Zorn's lemma). ## Tags algebraic closure, algebraically closed -/ universes u v w noncomputable theory open_locale classical big_operators open polynomial variables (k : Type u) [field k] /-- Typeclass for algebraically closed fields. To show `polynomial.splits p f` for an arbitrary ring homomorphism `f`, see `is_alg_closed.splits_codomain` and `is_alg_closed.splits_domain`. -/ class is_alg_closed : Prop := (splits : ∀ p : polynomial k, p.splits $ ring_hom.id k) /-- Every polynomial splits in the field extension `f : K →+* k` if `k` is algebraically closed. See also `is_alg_closed.splits_domain` for the case where `K` is algebraically closed. -/ theorem is_alg_closed.splits_codomain {k K : Type*} [field k] [is_alg_closed k] [field K] {f : K →+* k} (p : polynomial K) : p.splits f := by { convert is_alg_closed.splits (p.map f), simp [splits_map_iff] } /-- Every polynomial splits in the field extension `f : K →+* k` if `K` is algebraically closed. See also `is_alg_closed.splits_codomain` for the case where `k` is algebraically closed. -/ theorem is_alg_closed.splits_domain {k K : Type*} [field k] [is_alg_closed k] [field K] {f : k →+* K} (p : polynomial k) : p.splits f := polynomial.splits_of_splits_id _ $ is_alg_closed.splits _ namespace is_alg_closed theorem exists_root [is_alg_closed k] (p : polynomial k) (hp : p.degree ≠ 0) : ∃ x, is_root p x := exists_root_of_splits _ (is_alg_closed.splits p) hp theorem of_exists_root (H : ∀ p : polynomial k, p.monic → irreducible p → ∃ x, p.eval x = 0) : is_alg_closed k := ⟨λ p, or.inr $ λ q hq hqp, have irreducible (q * C (leading_coeff q)⁻¹), by { rw ← coe_norm_unit_of_ne_zero hq.ne_zero, exact (associated_normalize _).irreducible hq }, let ⟨x, hx⟩ := H (q * C (leading_coeff q)⁻¹) (monic_mul_leading_coeff_inv hq.ne_zero) this in degree_mul_leading_coeff_inv q hq.ne_zero ▸ degree_eq_one_of_irreducible_of_root this hx⟩ lemma degree_eq_one_of_irreducible [is_alg_closed k] {p : polynomial k} (h_nz : p ≠ 0) (hp : irreducible p) : p.degree = 1 := degree_eq_one_of_irreducible_of_splits h_nz hp (is_alg_closed.splits_codomain _) lemma algebra_map_surjective_of_is_integral {k K : Type*} [field k] [domain K] [hk : is_alg_closed k] [algebra k K] (hf : algebra.is_integral k K) : function.surjective (algebra_map k K) := begin refine λ x, ⟨-((minpoly k x).coeff 0), _⟩, have hq : (minpoly k x).leading_coeff = 1 := minpoly.monic (hf x), have h : (minpoly k x).degree = 1 := degree_eq_one_of_irreducible k (minpoly.ne_zero (hf x)) (minpoly.irreducible (hf x)), have : (aeval x (minpoly k x)) = 0 := minpoly.aeval k x, rw [eq_X_add_C_of_degree_eq_one h, hq, C_1, one_mul, aeval_add, aeval_X, aeval_C, add_eq_zero_iff_eq_neg] at this, exact (ring_hom.map_neg (algebra_map k K) ((minpoly k x).coeff 0)).symm ▸ this.symm, end lemma algebra_map_surjective_of_is_integral' {k K : Type*} [field k] [integral_domain K] [hk : is_alg_closed k] (f : k →+* K) (hf : f.is_integral) : function.surjective f := @algebra_map_surjective_of_is_integral k K _ _ _ f.to_algebra hf lemma algebra_map_surjective_of_is_algebraic {k K : Type*} [field k] [domain K] [hk : is_alg_closed k] [algebra k K] (hf : algebra.is_algebraic k K) : function.surjective (algebra_map k K) := algebra_map_surjective_of_is_integral ((is_algebraic_iff_is_integral' k).mp hf) end is_alg_closed /-- Typeclass for an extension being an algebraic closure. -/ class is_alg_closure (K : Type v) [field K] [algebra k K] : Prop := (alg_closed : is_alg_closed K) (algebraic : algebra.is_algebraic k K) theorem is_alg_closure_iff (K : Type v) [field K] [algebra k K] : is_alg_closure k K ↔ is_alg_closed K ∧ algebra.is_algebraic k K := ⟨λ h, ⟨h.1, h.2⟩, λ h, ⟨h.1, h.2⟩⟩ namespace algebraic_closure open mv_polynomial /-- The subtype of monic irreducible polynomials -/ @[reducible] def monic_irreducible : Type u := { f : polynomial k // monic f ∧ irreducible f } /-- Sends a monic irreducible polynomial `f` to `f(x_f)` where `x_f` is a formal indeterminate. -/ def eval_X_self (f : monic_irreducible k) : mv_polynomial (monic_irreducible k) k := polynomial.eval₂ mv_polynomial.C (X f) f /-- The span of `f(x_f)` across monic irreducible polynomials `f` where `x_f` is an indeterminate. -/ def span_eval : ideal (mv_polynomial (monic_irreducible k) k) := ideal.span $ set.range $ eval_X_self k /-- Given a finset of monic irreducible polynomials, construct an algebra homomorphism to the splitting field of the product of the polynomials sending each indeterminate `x_f` represented by the polynomial `f` in the finset to a root of `f`. -/ def to_splitting_field (s : finset (monic_irreducible k)) : mv_polynomial (monic_irreducible k) k →ₐ[k] splitting_field (∏ x in s, x : polynomial k) := mv_polynomial.aeval $ λ f, if hf : f ∈ s then root_of_splits _ ((splits_prod_iff _ $ λ (j : monic_irreducible k) _, j.2.2.ne_zero).1 (splitting_field.splits _) f hf) (mt is_unit_iff_degree_eq_zero.2 f.2.2.not_unit) else 37 theorem to_splitting_field_eval_X_self {s : finset (monic_irreducible k)} {f} (hf : f ∈ s) : to_splitting_field k s (eval_X_self k f) = 0 := by { rw [to_splitting_field, eval_X_self, ← alg_hom.coe_to_ring_hom, hom_eval₂, alg_hom.coe_to_ring_hom, mv_polynomial.aeval_X, dif_pos hf, ← algebra_map_eq, alg_hom.comp_algebra_map], exact map_root_of_splits _ _ _ } theorem span_eval_ne_top : span_eval k ≠ ⊤ := begin rw [ideal.ne_top_iff_one, span_eval, ideal.span, ← set.image_univ, finsupp.mem_span_image_iff_total], rintros ⟨v, _, hv⟩, replace hv := congr_arg (to_splitting_field k v.support) hv, rw [alg_hom.map_one, finsupp.total_apply, finsupp.sum, alg_hom.map_sum, finset.sum_eq_zero] at hv, { exact zero_ne_one hv }, intros j hj, rw [smul_eq_mul, alg_hom.map_mul, to_splitting_field_eval_X_self k hj, mul_zero] end /-- A random maximal ideal that contains `span_eval k` -/ def max_ideal : ideal (mv_polynomial (monic_irreducible k) k) := classical.some $ ideal.exists_le_maximal _ $ span_eval_ne_top k instance max_ideal.is_maximal : (max_ideal k).is_maximal := (classical.some_spec $ ideal.exists_le_maximal _ $ span_eval_ne_top k).1 theorem le_max_ideal : span_eval k ≤ max_ideal k := (classical.some_spec $ ideal.exists_le_maximal _ $ span_eval_ne_top k).2 /-- The first step of constructing `algebraic_closure`: adjoin a root of all monic polynomials -/ def adjoin_monic : Type u := (max_ideal k).quotient instance adjoin_monic.field : field (adjoin_monic k) := ideal.quotient.field _ instance adjoin_monic.inhabited : inhabited (adjoin_monic k) := ⟨37⟩ /-- The canonical ring homomorphism to `adjoin_monic k`. -/ def to_adjoin_monic : k →+* adjoin_monic k := (ideal.quotient.mk _).comp C instance adjoin_monic.algebra : algebra k (adjoin_monic k) := (to_adjoin_monic k).to_algebra theorem adjoin_monic.algebra_map : algebra_map k (adjoin_monic k) = (ideal.quotient.mk _).comp C := rfl theorem adjoin_monic.is_integral (z : adjoin_monic k) : is_integral k z := let ⟨p, hp⟩ := ideal.quotient.mk_surjective z in hp ▸ mv_polynomial.induction_on p (λ x, is_integral_algebra_map) (λ p q, is_integral_add) (λ p f ih, @is_integral_mul _ _ _ _ _ _ (ideal.quotient.mk _ _) ih ⟨f, f.2.1, by { erw [adjoin_monic.algebra_map, ← hom_eval₂, ideal.quotient.eq_zero_iff_mem], exact le_max_ideal k (ideal.subset_span ⟨f, rfl⟩) }⟩) theorem adjoin_monic.exists_root {f : polynomial k} (hfm : f.monic) (hfi : irreducible f) : ∃ x : adjoin_monic k, f.eval₂ (to_adjoin_monic k) x = 0 := ⟨ideal.quotient.mk _ $ X (⟨f, hfm, hfi⟩ : monic_irreducible k), by { rw [to_adjoin_monic, ← hom_eval₂, ideal.quotient.eq_zero_iff_mem], exact le_max_ideal k (ideal.subset_span $ ⟨_, rfl⟩) }⟩ /-- The `n`th step of constructing `algebraic_closure`, together with its `field` instance. -/ def step_aux (n : ℕ) : Σ α : Type u, field α := nat.rec_on n ⟨k, infer_instance⟩ $ λ n ih, ⟨@adjoin_monic ih.1 ih.2, @adjoin_monic.field ih.1 ih.2⟩ /-- The `n`th step of constructing `algebraic_closure`. -/ def step (n : ℕ) : Type u := (step_aux k n).1 instance step.field (n : ℕ) : field (step k n) := (step_aux k n).2 instance step.inhabited (n) : inhabited (step k n) := ⟨37⟩ /-- The canonical inclusion to the `0`th step. -/ def to_step_zero : k →+* step k 0 := ring_hom.id k /-- The canonical ring homomorphism to the next step. -/ def to_step_succ (n : ℕ) : step k n →+* step k (n + 1) := @to_adjoin_monic (step k n) (step.field k n) instance step.algebra_succ (n) : algebra (step k n) (step k (n + 1)) := (to_step_succ k n).to_algebra theorem to_step_succ.exists_root {n} {f : polynomial (step k n)} (hfm : f.monic) (hfi : irreducible f) : ∃ x : step k (n + 1), f.eval₂ (to_step_succ k n) x = 0 := @adjoin_monic.exists_root _ (step.field k n) _ hfm hfi /-- The canonical ring homomorphism to a step with a greater index. -/ def to_step_of_le (m n : ℕ) (h : m ≤ n) : step k m →+* step k n := { to_fun := nat.le_rec_on h (λ n, to_step_succ k n), map_one' := begin induction h with n h ih, { exact nat.le_rec_on_self 1 }, rw [nat.le_rec_on_succ h, ih, ring_hom.map_one] end, map_mul' := λ x y, begin induction h with n h ih, { simp_rw nat.le_rec_on_self }, simp_rw [nat.le_rec_on_succ h, ih, ring_hom.map_mul] end, map_zero' := begin induction h with n h ih, { exact nat.le_rec_on_self 0 }, rw [nat.le_rec_on_succ h, ih, ring_hom.map_zero] end, map_add' := λ x y, begin induction h with n h ih, { simp_rw nat.le_rec_on_self }, simp_rw [nat.le_rec_on_succ h, ih, ring_hom.map_add] end } @[simp] lemma coe_to_step_of_le (m n : ℕ) (h : m ≤ n) : (to_step_of_le k m n h : step k m → step k n) = nat.le_rec_on h (λ n, to_step_succ k n) := rfl instance step.algebra (n) : algebra k (step k n) := (to_step_of_le k 0 n n.zero_le).to_algebra instance step.scalar_tower (n) : is_scalar_tower k (step k n) (step k (n + 1)) := is_scalar_tower.of_algebra_map_eq $ λ z, @nat.le_rec_on_succ (step k) 0 n n.zero_le (n + 1).zero_le (λ n, to_step_succ k n) z theorem step.is_integral (n) : ∀ z : step k n, is_integral k z := nat.rec_on n (λ z, is_integral_algebra_map) $ λ n ih z, is_integral_trans ih _ (adjoin_monic.is_integral (step k n) z : _) instance to_step_of_le.directed_system : directed_system (step k) (λ i j h, to_step_of_le k i j h) := ⟨λ i x h, nat.le_rec_on_self x, λ i₁ i₂ i₃ h₁₂ h₂₃ x, (nat.le_rec_on_trans h₁₂ h₂₃ x).symm⟩ end algebraic_closure /-- The canonical algebraic closure of a field, the direct limit of adding roots to the field for each polynomial over the field. -/ def algebraic_closure : Type u := ring.direct_limit (algebraic_closure.step k) (λ i j h, algebraic_closure.to_step_of_le k i j h) namespace algebraic_closure instance : field (algebraic_closure k) := field.direct_limit.field _ _ instance : inhabited (algebraic_closure k) := ⟨37⟩ /-- The canonical ring embedding from the `n`th step to the algebraic closure. -/ def of_step (n : ℕ) : step k n →+* algebraic_closure k := ring.direct_limit.of _ _ _ instance algebra_of_step (n) : algebra (step k n) (algebraic_closure k) := (of_step k n).to_algebra theorem of_step_succ (n : ℕ) : (of_step k (n + 1)).comp (to_step_succ k n) = of_step k n := ring_hom.ext $ λ x, show ring.direct_limit.of (step k) (λ i j h, to_step_of_le k i j h) _ _ = _, by { convert ring.direct_limit.of_f n.le_succ x, ext x, exact (nat.le_rec_on_succ' x).symm } theorem exists_of_step (z : algebraic_closure k) : ∃ n x, of_step k n x = z := ring.direct_limit.exists_of z -- slow theorem exists_root {f : polynomial (algebraic_closure k)} (hfm : f.monic) (hfi : irreducible f) : ∃ x : algebraic_closure k, f.eval x = 0 := begin have : ∃ n p, polynomial.map (of_step k n) p = f, { convert ring.direct_limit.polynomial.exists_of f }, unfreezingI { obtain ⟨n, p, rfl⟩ := this }, rw monic_map_iff at hfm, have := hfm.irreducible_of_irreducible_map (of_step k n) p hfi, obtain ⟨x, hx⟩ := to_step_succ.exists_root k hfm this, refine ⟨of_step k (n + 1) x, _⟩, rw [← of_step_succ k n, eval_map, ← hom_eval₂, hx, ring_hom.map_zero] end instance : is_alg_closed (algebraic_closure k) := is_alg_closed.of_exists_root _ $ λ f, exists_root k instance {R : Type*} [comm_semiring R] [alg : algebra R k] : algebra R (algebraic_closure k) := ((of_step k 0).comp (@algebra_map _ _ _ _ alg)).to_algebra lemma algebra_map_def {R : Type*} [comm_semiring R] [alg : algebra R k] : algebra_map R (algebraic_closure k) = ((of_step k 0 : k →+* _).comp (@algebra_map _ _ _ _ alg)) := rfl instance {R S : Type*} [comm_semiring R] [comm_semiring S] [algebra R S] [algebra S k] [algebra R k] [is_scalar_tower R S k] : is_scalar_tower R S (algebraic_closure k) := is_scalar_tower.of_algebra_map_eq (λ x, ring_hom.congr_arg _ (is_scalar_tower.algebra_map_apply R S k x : _)) /-- Canonical algebra embedding from the `n`th step to the algebraic closure. -/ def of_step_hom (n) : step k n →ₐ[k] algebraic_closure k := { commutes' := λ x, ring.direct_limit.of_f n.zero_le x, .. of_step k n } theorem is_algebraic : algebra.is_algebraic k (algebraic_closure k) := λ z, (is_algebraic_iff_is_integral _).2 $ let ⟨n, x, hx⟩ := exists_of_step k z in hx ▸ is_integral_alg_hom (of_step_hom k n) (step.is_integral k n x) instance : is_alg_closure k (algebraic_closure k) := ⟨algebraic_closure.is_alg_closed k, is_algebraic k⟩ end algebraic_closure /-- Every element `f` in a nontrivial finite-dimensional algebra `A` over an algebraically closed field `K` has non-empty spectrum: that is, there is some `c : K` so `f - c • 1` is not invertible. -/ -- We will use this both to show eigenvalues exist, and to prove Schur's lemma. lemma exists_spectrum_of_is_alg_closed_of_finite_dimensional (𝕜 : Type*) [field 𝕜] [is_alg_closed 𝕜] {A : Type*} [nontrivial A] [ring A] [algebra 𝕜 A] [I : finite_dimensional 𝕜 A] (f : A) : ∃ c : 𝕜, ¬ is_unit (f - algebra_map 𝕜 A c) := begin obtain ⟨p, ⟨h_mon, h_eval_p⟩⟩ := is_integral_of_noetherian I f, have nu : ¬ is_unit (aeval f 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), ←multiset.prod_to_list, alg_hom.map_list_prod] at nu, replace nu := mt list.prod_is_unit nu, 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 nu, exact ⟨nu.some, nu.some_spec.2⟩, end
5642d43250508a6b1d4474a8888921f5750dbb5a
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/topology/continuous_function/bounded.lean
b9f6fd9cc9e54f291f132afedf00ab6836565860
[ "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
43,825
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Mario Carneiro, Yury Kudryashov, Heather Macbeth -/ import analysis.normed_space.operator_norm import topology.continuous_function.algebra /-! # Bounded continuous functions The type of bounded continuous functions taking values in a metric space, with the uniform distance. -/ noncomputable theory open_locale topological_space classical nnreal open set filter metric universes u v w variables {α : Type u} {β : Type v} {γ : Type w} /-- The type of bounded continuous functions from a topological space to a metric space -/ structure bounded_continuous_function (α : Type u) (β : Type v) [topological_space α] [metric_space β] extends continuous_map α β : Type (max u v) := (bounded' : ∃C, ∀x y:α, dist (to_fun x) (to_fun y) ≤ C) localized "infixr ` →ᵇ `:25 := bounded_continuous_function" in bounded_continuous_function namespace bounded_continuous_function section basics variables [topological_space α] [metric_space β] [metric_space γ] variables {f g : α →ᵇ β} {x : α} {C : ℝ} instance : has_coe_to_fun (α →ᵇ β) (λ _, α → β) := ⟨λ f, f.to_fun⟩ @[simp] lemma coe_to_continuous_fun (f : α →ᵇ β) : (f.to_continuous_map : α → β) = f := rfl /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def simps.apply (h : α →ᵇ β) : α → β := h initialize_simps_projections bounded_continuous_function (to_continuous_map_to_fun → apply) protected lemma bounded (f : α →ᵇ β) : ∃C, ∀ x y : α, dist (f x) (f y) ≤ C := f.bounded' @[continuity] protected lemma continuous (f : α →ᵇ β) : continuous f := f.to_continuous_map.continuous @[ext] lemma ext (H : ∀x, f x = g x) : f = g := by { cases f, cases g, congr, ext, exact H x, } lemma ext_iff : f = g ↔ ∀ x, f x = g x := ⟨λ h, λ x, h ▸ rfl, ext⟩ lemma bounded_range : bounded (range f) := bounded_range_iff.2 f.bounded lemma eq_of_empty [is_empty α] (f g : α →ᵇ β) : f = g := ext $ is_empty.elim ‹_› /-- A continuous function with an explicit bound is a bounded continuous function. -/ def mk_of_bound (f : C(α, β)) (C : ℝ) (h : ∀ x y : α, dist (f x) (f y) ≤ C) : α →ᵇ β := ⟨f, ⟨C, h⟩⟩ @[simp] lemma mk_of_bound_coe {f} {C} {h} : (mk_of_bound f C h : α → β) = (f : α → β) := rfl /-- A continuous function on a compact space is automatically a bounded continuous function. -/ def mk_of_compact [compact_space α] (f : C(α, β)) : α →ᵇ β := ⟨f, bounded_range_iff.1 (is_compact_range f.continuous).bounded⟩ @[simp] lemma mk_of_compact_apply [compact_space α] (f : C(α, β)) (a : α) : mk_of_compact f a = f a := rfl /-- If a function is bounded on a discrete space, it is automatically continuous, and therefore gives rise to an element of the type of bounded continuous functions -/ @[simps] def mk_of_discrete [discrete_topology α] (f : α → β) (C : ℝ) (h : ∀ x y : α, dist (f x) (f y) ≤ C) : α →ᵇ β := ⟨⟨f, continuous_of_discrete_topology⟩, ⟨C, h⟩⟩ section variables (α β) /-- The map forgetting that a bounded continuous function is bounded. -/ def forget_boundedness : (α →ᵇ β) → C(α, β) := λ f, f.1 @[simp] lemma forget_boundedness_coe (f : α →ᵇ β) : (forget_boundedness α β f : α → β) = f := rfl end /-- The uniform distance between two bounded continuous functions -/ instance : has_dist (α →ᵇ β) := ⟨λf g, Inf {C | 0 ≤ C ∧ ∀ x : α, dist (f x) (g x) ≤ C}⟩ lemma dist_eq : dist f g = Inf {C | 0 ≤ C ∧ ∀ x : α, dist (f x) (g x) ≤ C} := rfl lemma dist_set_exists : ∃ C, 0 ≤ C ∧ ∀ x : α, dist (f x) (g x) ≤ C := begin refine if h : nonempty α then _ else ⟨0, le_refl _, λ x, h.elim ⟨x⟩⟩, cases h with x, rcases f.bounded with ⟨Cf, hCf : ∀ x y, dist (f x) (f y) ≤ Cf⟩, rcases g.bounded with ⟨Cg, hCg : ∀ x y, dist (g x) (g y) ≤ Cg⟩, let C := max 0 (dist (f x) (g x) + (Cf + Cg)), refine ⟨C, le_max_left _ _, λ y, _⟩, calc dist (f y) (g y) ≤ dist (f x) (g x) + (dist (f x) (f y) + dist (g x) (g y)) : dist_triangle4_left _ _ _ _ ... ≤ dist (f x) (g x) + (Cf + Cg) : by mono* ... ≤ C : le_max_right _ _ end /-- The pointwise distance is controlled by the distance between functions, by definition. -/ lemma dist_coe_le_dist (x : α) : dist (f x) (g x) ≤ dist f g := le_cInf dist_set_exists $ λb hb, hb.2 x /- This lemma will be needed in the proof of the metric space instance, but it will become useless afterwards as it will be superseded by the general result that the distance is nonnegative in metric spaces. -/ private lemma dist_nonneg' : 0 ≤ dist f g := le_cInf dist_set_exists (λ C, and.left) /-- The distance between two functions is controlled by the supremum of the pointwise distances -/ lemma dist_le (C0 : (0 : ℝ) ≤ C) : dist f g ≤ C ↔ ∀x:α, dist (f x) (g x) ≤ C := ⟨λ h x, le_trans (dist_coe_le_dist x) h, λ H, cInf_le ⟨0, λ C, and.left⟩ ⟨C0, H⟩⟩ lemma dist_le_iff_of_nonempty [nonempty α] : dist f g ≤ C ↔ ∀ x, dist (f x) (g x) ≤ C := ⟨λ h x, le_trans (dist_coe_le_dist x) h, λ w, (dist_le (le_trans dist_nonneg (w (nonempty.some ‹_›)))).mpr w⟩ lemma dist_lt_of_nonempty_compact [nonempty α] [compact_space α] (w : ∀x:α, dist (f x) (g x) < C) : dist f g < C := begin have c : continuous (λ x, dist (f x) (g x)), { continuity, }, obtain ⟨x, -, le⟩ := is_compact.exists_forall_ge compact_univ set.univ_nonempty (continuous.continuous_on c), exact lt_of_le_of_lt (dist_le_iff_of_nonempty.mpr (λ y, le y trivial)) (w x), end lemma dist_lt_iff_of_compact [compact_space α] (C0 : (0 : ℝ) < C) : dist f g < C ↔ ∀x:α, dist (f x) (g x) < C := begin fsplit, { intros w x, exact lt_of_le_of_lt (dist_coe_le_dist x) w, }, { by_cases h : nonempty α, { resetI, exact dist_lt_of_nonempty_compact, }, { rintro -, convert C0, apply le_antisymm _ dist_nonneg', rw [dist_eq], exact cInf_le ⟨0, λ C, and.left⟩ ⟨le_refl _, λ x, false.elim (h (nonempty.intro x))⟩, }, }, end lemma dist_lt_iff_of_nonempty_compact [nonempty α] [compact_space α] : dist f g < C ↔ ∀x:α, dist (f x) (g x) < C := ⟨λ w x, lt_of_le_of_lt (dist_coe_le_dist x) w, dist_lt_of_nonempty_compact⟩ /-- The type of bounded continuous functions, with the uniform distance, is a metric space. -/ instance : metric_space (α →ᵇ β) := { dist_self := λ f, le_antisymm ((dist_le (le_refl _)).2 $ λ x, by simp) dist_nonneg', eq_of_dist_eq_zero := λ f g hfg, by ext x; exact eq_of_dist_eq_zero (le_antisymm (hfg ▸ dist_coe_le_dist _) dist_nonneg), dist_comm := λ f g, by simp [dist_eq, dist_comm], dist_triangle := λ f g h, (dist_le (add_nonneg dist_nonneg' dist_nonneg')).2 $ λ x, le_trans (dist_triangle _ _ _) (add_le_add (dist_coe_le_dist _) (dist_coe_le_dist _)) } /-- On an empty space, bounded continuous functions are at distance 0 -/ lemma dist_zero_of_empty [is_empty α] : dist f g = 0 := dist_eq_zero.2 (eq_of_empty f g) variables (α) {β} /-- Constant as a continuous bounded function. -/ @[simps {fully_applied := ff}] def const (b : β) : α →ᵇ β := ⟨continuous_map.const b, 0, by simp [le_refl]⟩ variable {α} lemma const_apply' (a : α) (b : β) : (const α b : α → β) a = b := rfl /-- If the target space is inhabited, so is the space of bounded continuous functions -/ instance [inhabited β] : inhabited (α →ᵇ β) := ⟨const α (default β)⟩ lemma lipschitz_evalx (x : α) : lipschitz_with 1 (λ f : α →ᵇ β, f x) := lipschitz_with.mk_one $ λ f g, dist_coe_le_dist x theorem uniform_continuous_coe : @uniform_continuous (α →ᵇ β) (α → β) _ _ coe_fn := uniform_continuous_pi.2 $ λ x, (lipschitz_evalx x).uniform_continuous lemma continuous_coe : continuous (λ (f : α →ᵇ β) x, f x) := uniform_continuous.continuous uniform_continuous_coe /-- When `x` is fixed, `(f : α →ᵇ β) ↦ f x` is continuous -/ @[continuity] theorem continuous_evalx {x : α} : continuous (λ f : α →ᵇ β, f x) := (continuous_apply x).comp continuous_coe /-- The evaluation map is continuous, as a joint function of `u` and `x` -/ @[continuity] theorem continuous_eval : continuous (λ p : (α →ᵇ β) × α, p.1 p.2) := continuous_prod_of_continuous_lipschitz _ 1 (λ f, f.continuous) $ lipschitz_evalx /-- Bounded continuous functions taking values in a complete space form a complete space. -/ instance [complete_space β] : complete_space (α →ᵇ β) := complete_of_cauchy_seq_tendsto $ λ (f : ℕ → α →ᵇ β) (hf : cauchy_seq f), begin /- We have to show that `f n` converges to a bounded continuous function. For this, we prove pointwise convergence to define the limit, then check it is a continuous bounded function, and then check the norm convergence. -/ rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, b0, b_bound, b_lim⟩, have f_bdd := λx n m N hn hm, le_trans (dist_coe_le_dist x) (b_bound n m N hn hm), have fx_cau : ∀x, cauchy_seq (λn, f n x) := λx, cauchy_seq_iff_le_tendsto_0.2 ⟨b, b0, f_bdd x, b_lim⟩, choose F hF using λx, cauchy_seq_tendsto_of_complete (fx_cau x), /- F : α → β, hF : ∀ (x : α), tendsto (λ (n : ℕ), f n x) at_top (𝓝 (F x)) `F` is the desired limit function. Check that it is uniformly approximated by `f N` -/ have fF_bdd : ∀x N, dist (f N x) (F x) ≤ b N := λ x N, le_of_tendsto (tendsto_const_nhds.dist (hF x)) (filter.eventually_at_top.2 ⟨N, λn hn, f_bdd x N n N (le_refl N) hn⟩), refine ⟨⟨⟨F, _⟩, _⟩, _⟩, { /- Check that `F` is continuous, as a uniform limit of continuous functions -/ have : tendsto_uniformly (λn x, f n x) F at_top, { refine metric.tendsto_uniformly_iff.2 (λ ε ε0, _), refine ((tendsto_order.1 b_lim).2 ε ε0).mono (λ n hn x, _), rw dist_comm, exact lt_of_le_of_lt (fF_bdd x n) hn }, exact this.continuous (λN, (f N).continuous) }, { /- Check that `F` is bounded -/ rcases (f 0).bounded with ⟨C, hC⟩, refine ⟨C + (b 0 + b 0), λ x y, _⟩, calc dist (F x) (F y) ≤ dist (f 0 x) (f 0 y) + (dist (f 0 x) (F x) + dist (f 0 y) (F y)) : dist_triangle4_left _ _ _ _ ... ≤ C + (b 0 + b 0) : by mono* }, { /- Check that `F` is close to `f N` in distance terms -/ refine tendsto_iff_dist_tendsto_zero.2 (squeeze_zero (λ _, dist_nonneg) _ b_lim), exact λ N, (dist_le (b0 _)).2 (λx, fF_bdd x N) } end /-- Composition of a bounded continuous function and a continuous function. -/ @[simps { fully_applied := ff }] def comp_continuous {δ : Type*} [topological_space δ] (f : α →ᵇ β) (g : C(δ, α)) : δ →ᵇ β := { to_continuous_map := f.1.comp g, bounded' := f.bounded'.imp (λ C hC x y, hC _ _) } lemma lipschitz_comp_continuous {δ : Type*} [topological_space δ] (g : C(δ, α)) : lipschitz_with 1 (λ f : α →ᵇ β, f.comp_continuous g) := lipschitz_with.mk_one $ λ f₁ f₂, (dist_le dist_nonneg).2 $ λ x, dist_coe_le_dist (g x) lemma continuous_comp_continuous {δ : Type*} [topological_space δ] (g : C(δ, α)) : continuous (λ f : α →ᵇ β, f.comp_continuous g) := (lipschitz_comp_continuous g).continuous /-- Restrict a bounded continuous function to a set. -/ @[simps apply { fully_applied := ff }] def restrict (f : α →ᵇ β) (s : set α) : s →ᵇ β := f.comp_continuous (continuous_map.id.restrict s) /-- Composition (in the target) of a bounded continuous function with a Lipschitz map again gives a bounded continuous function -/ def comp (G : β → γ) {C : ℝ≥0} (H : lipschitz_with C G) (f : α →ᵇ β) : α →ᵇ γ := ⟨⟨λx, G (f x), H.continuous.comp f.continuous⟩, let ⟨D, hD⟩ := f.bounded in ⟨max C 0 * D, λ x y, calc dist (G (f x)) (G (f y)) ≤ C * dist (f x) (f y) : H.dist_le_mul _ _ ... ≤ max C 0 * dist (f x) (f y) : mul_le_mul_of_nonneg_right (le_max_left C 0) dist_nonneg ... ≤ max C 0 * D : mul_le_mul_of_nonneg_left (hD _ _) (le_max_right C 0)⟩⟩ /-- The composition operator (in the target) with a Lipschitz map is Lipschitz -/ lemma lipschitz_comp {G : β → γ} {C : ℝ≥0} (H : lipschitz_with C G) : lipschitz_with C (comp G H : (α →ᵇ β) → α →ᵇ γ) := lipschitz_with.of_dist_le_mul $ λ f g, (dist_le (mul_nonneg C.2 dist_nonneg)).2 $ λ x, calc dist (G (f x)) (G (g x)) ≤ C * dist (f x) (g x) : H.dist_le_mul _ _ ... ≤ C * dist f g : mul_le_mul_of_nonneg_left (dist_coe_le_dist _) C.2 /-- The composition operator (in the target) with a Lipschitz map is uniformly continuous -/ lemma uniform_continuous_comp {G : β → γ} {C : ℝ≥0} (H : lipschitz_with C G) : uniform_continuous (comp G H : (α →ᵇ β) → α →ᵇ γ) := (lipschitz_comp H).uniform_continuous /-- The composition operator (in the target) with a Lipschitz map is continuous -/ lemma continuous_comp {G : β → γ} {C : ℝ≥0} (H : lipschitz_with C G) : continuous (comp G H : (α →ᵇ β) → α →ᵇ γ) := (lipschitz_comp H).continuous /-- Restriction (in the target) of a bounded continuous function taking values in a subset -/ def cod_restrict (s : set β) (f : α →ᵇ β) (H : ∀x, f x ∈ s) : α →ᵇ s := ⟨⟨s.cod_restrict f H, continuous_subtype_mk _ f.continuous⟩, f.bounded⟩ end basics section arzela_ascoli variables [topological_space α] [compact_space α] [metric_space β] variables {f g : α →ᵇ β} {x : α} {C : ℝ} /- Arzela-Ascoli theorem asserts that, on a compact space, a set of functions sharing a common modulus of continuity and taking values in a compact set forms a compact subset for the topology of uniform convergence. In this section, we prove this theorem and several useful variations around it. -/ /-- First version, with pointwise equicontinuity and range in a compact space -/ theorem arzela_ascoli₁ [compact_space β] (A : set (α →ᵇ β)) (closed : is_closed A) (H : ∀ (x:α) (ε > 0), ∃U ∈ 𝓝 x, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε) : is_compact A := begin refine compact_of_totally_bounded_is_closed _ closed, refine totally_bounded_of_finite_discretization (λ ε ε0, _), rcases exists_between ε0 with ⟨ε₁, ε₁0, εε₁⟩, let ε₂ := ε₁/2/2, /- We have to find a finite discretization of `u`, i.e., finite information that is sufficient to reconstruct `u` up to ε. This information will be provided by the values of `u` on a sufficiently dense set tα, slightly translated to fit in a finite ε₂-dense set tβ in the image. Such sets exist by compactness of the source and range. Then, to check that these data determine the function up to ε, one uses the control on the modulus of continuity to extend the closeness on tα to closeness everywhere. -/ have ε₂0 : ε₂ > 0 := half_pos (half_pos ε₁0), have : ∀x:α, ∃U, x ∈ U ∧ is_open U ∧ ∀ (y z ∈ U) {f : α →ᵇ β}, f ∈ A → dist (f y) (f z) < ε₂ := λ x, let ⟨U, nhdsU, hU⟩ := H x _ ε₂0, ⟨V, VU, openV, xV⟩ := _root_.mem_nhds_iff.1 nhdsU in ⟨V, xV, openV, λy z hy hz f hf, hU y z (VU hy) (VU hz) f hf⟩, choose U hU using this, /- For all x, the set hU x is an open set containing x on which the elements of A fluctuate by at most ε₂. We extract finitely many of these sets that cover the whole space, by compactness -/ rcases compact_univ.elim_finite_subcover_image (λx _, (hU x).2.1) (λx hx, mem_bUnion (mem_univ _) (hU x).1) with ⟨tα, _, ⟨_⟩, htα⟩, /- tα : set α, htα : univ ⊆ ⋃x ∈ tα, U x -/ rcases @finite_cover_balls_of_compact β _ _ compact_univ _ ε₂0 with ⟨tβ, _, ⟨_⟩, htβ⟩, resetI, /- tβ : set β, htβ : univ ⊆ ⋃y ∈ tβ, ball y ε₂ -/ /- Associate to every point `y` in the space a nearby point `F y` in tβ -/ choose F hF using λy, show ∃z∈tβ, dist y z < ε₂, by simpa using htβ (mem_univ y), /- F : β → β, hF : ∀ (y : β), F y ∈ tβ ∧ dist y (F y) < ε₂ -/ /- Associate to every function a discrete approximation, mapping each point in `tα` to a point in `tβ` close to its true image by the function. -/ refine ⟨tα → tβ, by apply_instance, λ f a, ⟨F (f a), (hF (f a)).1⟩, _⟩, rintro ⟨f, hf⟩ ⟨g, hg⟩ f_eq_g, /- If two functions have the same approximation, then they are within distance ε -/ refine lt_of_le_of_lt ((dist_le $ le_of_lt ε₁0).2 (λ x, _)) εε₁, obtain ⟨x', x'tα, hx'⟩ : ∃x' ∈ tα, x ∈ U x' := mem_bUnion_iff.1 (htα (mem_univ x)), calc dist (f x) (g x) ≤ dist (f x) (f x') + dist (g x) (g x') + dist (f x') (g x') : dist_triangle4_right _ _ _ _ ... ≤ ε₂ + ε₂ + ε₁/2 : le_of_lt (add_lt_add (add_lt_add _ _) _) ... = ε₁ : by rw [add_halves, add_halves], { exact (hU x').2.2 _ _ hx' ((hU x').1) hf }, { exact (hU x').2.2 _ _ hx' ((hU x').1) hg }, { have F_f_g : F (f x') = F (g x') := (congr_arg (λ f:tα → tβ, (f ⟨x', x'tα⟩ : β)) f_eq_g : _), calc dist (f x') (g x') ≤ dist (f x') (F (f x')) + dist (g x') (F (f x')) : dist_triangle_right _ _ _ ... = dist (f x') (F (f x')) + dist (g x') (F (g x')) : by rw F_f_g ... < ε₂ + ε₂ : add_lt_add (hF (f x')).2 (hF (g x')).2 ... = ε₁/2 : add_halves _ } end /-- Second version, with pointwise equicontinuity and range in a compact subset -/ theorem arzela_ascoli₂ (s : set β) (hs : is_compact s) (A : set (α →ᵇ β)) (closed : is_closed A) (in_s : ∀(f : α →ᵇ β) (x : α), f ∈ A → f x ∈ s) (H : ∀(x:α) (ε > 0), ∃U ∈ 𝓝 x, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε) : is_compact A := /- This version is deduced from the previous one by restricting to the compact type in the target, using compactness there and then lifting everything to the original space. -/ begin have M : lipschitz_with 1 coe := lipschitz_with.subtype_coe s, let F : (α →ᵇ s) → α →ᵇ β := comp coe M, refine compact_of_is_closed_subset ((_ : is_compact (F ⁻¹' A)).image (continuous_comp M)) closed (λ f hf, _), { haveI : compact_space s := is_compact_iff_compact_space.1 hs, refine arzela_ascoli₁ _ (continuous_iff_is_closed.1 (continuous_comp M) _ closed) (λ x ε ε0, bex.imp_right (λ U U_nhds hU y z hy hz f hf, _) (H x ε ε0)), calc dist (f y) (f z) = dist (F f y) (F f z) : rfl ... < ε : hU y z hy hz (F f) hf }, { let g := cod_restrict s f (λx, in_s f x hf), rw [show f = F g, by ext; refl] at hf ⊢, exact ⟨g, hf, rfl⟩ } end /-- Third (main) version, with pointwise equicontinuity and range in a compact subset, but without closedness. The closure is then compact -/ theorem arzela_ascoli (s : set β) (hs : is_compact s) (A : set (α →ᵇ β)) (in_s : ∀(f : α →ᵇ β) (x : α), f ∈ A → f x ∈ s) (H : ∀(x:α) (ε > 0), ∃U ∈ 𝓝 x, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε) : is_compact (closure A) := /- This version is deduced from the previous one by checking that the closure of A, in addition to being closed, still satisfies the properties of compact range and equicontinuity -/ arzela_ascoli₂ s hs (closure A) is_closed_closure (λ f x hf, (mem_of_closed' hs.is_closed).2 $ λ ε ε0, let ⟨g, gA, dist_fg⟩ := metric.mem_closure_iff.1 hf ε ε0 in ⟨g x, in_s g x gA, lt_of_le_of_lt (dist_coe_le_dist _) dist_fg⟩) (λ x ε ε0, show ∃ U ∈ 𝓝 x, ∀ y z ∈ U, ∀ (f : α →ᵇ β), f ∈ closure A → dist (f y) (f z) < ε, begin refine bex.imp_right (λ U U_set hU y z hy hz f hf, _) (H x (ε/2) (half_pos ε0)), rcases metric.mem_closure_iff.1 hf (ε/2/2) (half_pos (half_pos ε0)) with ⟨g, gA, dist_fg⟩, replace dist_fg := λ x, lt_of_le_of_lt (dist_coe_le_dist x) dist_fg, calc dist (f y) (f z) ≤ dist (f y) (g y) + dist (f z) (g z) + dist (g y) (g z) : dist_triangle4_right _ _ _ _ ... < ε/2/2 + ε/2/2 + ε/2 : add_lt_add (add_lt_add (dist_fg y) (dist_fg z)) (hU y z hy hz g gA) ... = ε : by rw [add_halves, add_halves] end) /- To apply the previous theorems, one needs to check the equicontinuity. An important instance is when the source space is a metric space, and there is a fixed modulus of continuity for all the functions in the set A -/ lemma equicontinuous_of_continuity_modulus {α : Type u} [metric_space α] (b : ℝ → ℝ) (b_lim : tendsto b (𝓝 0) (𝓝 0)) (A : set (α →ᵇ β)) (H : ∀(x y:α) (f : α →ᵇ β), f ∈ A → dist (f x) (f y) ≤ b (dist x y)) (x:α) (ε : ℝ) (ε0 : 0 < ε) : ∃U ∈ 𝓝 x, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε := begin rcases tendsto_nhds_nhds.1 b_lim ε ε0 with ⟨δ, δ0, hδ⟩, refine ⟨ball x (δ/2), ball_mem_nhds x (half_pos δ0), λ y z hy hz f hf, _⟩, have : dist y z < δ := calc dist y z ≤ dist y x + dist z x : dist_triangle_right _ _ _ ... < δ/2 + δ/2 : add_lt_add hy hz ... = δ : add_halves _, calc dist (f y) (f z) ≤ b (dist y z) : H y z f hf ... ≤ |b (dist y z)| : le_abs_self _ ... = dist (b (dist y z)) 0 : by simp [real.dist_eq] ... < ε : hδ (by simpa [real.dist_eq] using this), end end arzela_ascoli section has_lipschitz_add /- In this section, if `β` is an `add_monoid` whose addition operation is Lipschitz, then we show that the space of bounded continuous functions from `α` to `β` inherits a topological `add_monoid` structure, by using pointwise operations and checking that they are compatible with the uniform distance. Implementation note: The material in this section could have been written for `has_lipschitz_mul` and transported by `@[to_additive]`. We choose not to do this because this causes a few lemma names (for example, `coe_mul`) to conflict with later lemma names for normed rings; this is only a trivial inconvenience, but in any case there are no obvious applications of the multiplicative version. -/ variables [topological_space α] [metric_space β] [add_monoid β] instance : has_zero (α →ᵇ β) := ⟨const α 0⟩ @[simp] lemma coe_zero : ((0 : α →ᵇ β) : α → β) = 0 := rfl lemma forall_coe_zero_iff_zero (f : α →ᵇ β) : (∀x, f x = 0) ↔ f = 0 := (@ext_iff _ _ _ _ f 0).symm @[simp] lemma zero_comp_continuous [topological_space γ] (f : C(γ, α)) : (0 : α →ᵇ β).comp_continuous f = 0 := rfl variables [has_lipschitz_add β] variables (f g : α →ᵇ β) {x : α} {C : ℝ} /-- The pointwise sum of two bounded continuous functions is again bounded continuous. -/ instance : has_add (α →ᵇ β) := { add := λ f g, bounded_continuous_function.mk_of_bound (f.to_continuous_map + g.to_continuous_map) (↑(has_lipschitz_add.C β) * max (classical.some f.bounded) (classical.some g.bounded)) begin intros x y, refine le_trans (lipschitz_with_lipschitz_const_add ⟨f x, g x⟩ ⟨f y, g y⟩) _, rw prod.dist_eq, refine mul_le_mul_of_nonneg_left _ (has_lipschitz_add.C β).coe_nonneg, apply max_le_max, exact classical.some_spec f.bounded x y, exact classical.some_spec g.bounded x y, end } @[simp] lemma coe_add : ⇑(f + g) = f + g := rfl lemma add_apply : (f + g) x = f x + g x := rfl lemma add_comp_continuous [topological_space γ] (h : C(γ, α)) : (g + f).comp_continuous h = g.comp_continuous h + f.comp_continuous h := rfl instance : add_monoid (α →ᵇ β) := { add_assoc := assume f g h, by ext; simp [add_assoc], zero_add := assume f, by ext; simp, add_zero := assume f, by ext; simp, .. bounded_continuous_function.has_add, .. bounded_continuous_function.has_zero } instance : has_lipschitz_add (α →ᵇ β) := { lipschitz_add := ⟨has_lipschitz_add.C β, begin have C_nonneg := (has_lipschitz_add.C β).coe_nonneg, rw lipschitz_with_iff_dist_le_mul, rintros ⟨f₁, g₁⟩ ⟨f₂, g₂⟩, rw dist_le (mul_nonneg C_nonneg dist_nonneg), intros x, refine le_trans (lipschitz_with_lipschitz_const_add ⟨f₁ x, g₁ x⟩ ⟨f₂ x, g₂ x⟩) _, refine mul_le_mul_of_nonneg_left _ C_nonneg, apply max_le_max; exact dist_coe_le_dist x, end⟩ } /-- Coercion of a `normed_group_hom` is an `add_monoid_hom`. Similar to `add_monoid_hom.coe_fn` -/ @[simps] def coe_fn_add_hom : (α →ᵇ β) →+ (α → β) := { to_fun := coe_fn, map_zero' := coe_zero, map_add' := coe_add } variables (α β) /-- The additive map forgetting that a bounded continuous function is bounded. -/ @[simps] def forget_boundedness_add_hom : (α →ᵇ β) →+ C(α, β) := { to_fun := forget_boundedness α β, map_zero' := by { ext, simp, }, map_add' := by { intros, ext, simp, }, } end has_lipschitz_add section comm_has_lipschitz_add variables [topological_space α] [metric_space β] [add_comm_monoid β] [has_lipschitz_add β] @[to_additive] instance : add_comm_monoid (α →ᵇ β) := { add_comm := assume f g, by ext; simp [add_comm], .. bounded_continuous_function.add_monoid } open_locale big_operators @[simp] lemma coe_sum {ι : Type*} (s : finset ι) (f : ι → (α →ᵇ β)) : ⇑(∑ i in s, f i) = (∑ i in s, (f i : α → β)) := (@coe_fn_add_hom α β _ _ _ _).map_sum f s lemma sum_apply {ι : Type*} (s : finset ι) (f : ι → (α →ᵇ β)) (a : α) : (∑ i in s, f i) a = (∑ i in s, f i a) := by simp end comm_has_lipschitz_add section normed_group /- In this section, if β is a normed group, then we show that the space of bounded continuous functions from α to β inherits a normed group structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables [topological_space α] [normed_group β] variables (f g : α →ᵇ β) {x : α} {C : ℝ} instance : has_norm (α →ᵇ β) := ⟨λu, dist u 0⟩ lemma norm_def : ∥f∥ = dist f 0 := rfl /-- The norm of a bounded continuous function is the supremum of `∥f x∥`. We use `Inf` to ensure that the definition works if `α` has no elements. -/ lemma norm_eq (f : α →ᵇ β) : ∥f∥ = Inf {C : ℝ | 0 ≤ C ∧ ∀ (x : α), ∥f x∥ ≤ C} := by simp [norm_def, bounded_continuous_function.dist_eq] /-- When the domain is non-empty, we do not need the `0 ≤ C` condition in the formula for ∥f∥ as an `Inf`. -/ lemma norm_eq_of_nonempty [h : nonempty α] : ∥f∥ = Inf {C : ℝ | ∀ (x : α), ∥f x∥ ≤ C} := begin unfreezingI { obtain ⟨a⟩ := h, }, rw norm_eq, congr, ext, simp only [and_iff_right_iff_imp], exact λ h', le_trans (norm_nonneg (f a)) (h' a), end @[simp] lemma norm_eq_zero_of_empty [h : is_empty α] : ∥f∥ = 0 := dist_zero_of_empty lemma norm_coe_le_norm (x : α) : ∥f x∥ ≤ ∥f∥ := calc ∥f x∥ = dist (f x) ((0 : α →ᵇ β) x) : by simp [dist_zero_right] ... ≤ ∥f∥ : dist_coe_le_dist _ lemma dist_le_two_norm' {f : γ → β} {C : ℝ} (hC : ∀ x, ∥f x∥ ≤ C) (x y : γ) : dist (f x) (f y) ≤ 2 * C := calc dist (f x) (f y) ≤ ∥f x∥ + ∥f y∥ : dist_le_norm_add_norm _ _ ... ≤ C + C : add_le_add (hC x) (hC y) ... = 2 * C : (two_mul _).symm /-- Distance between the images of any two points is at most twice the norm of the function. -/ lemma dist_le_two_norm (x y : α) : dist (f x) (f y) ≤ 2 * ∥f∥ := dist_le_two_norm' f.norm_coe_le_norm x y variable {f} /-- The norm of a function is controlled by the supremum of the pointwise norms -/ lemma norm_le (C0 : (0 : ℝ) ≤ C) : ∥f∥ ≤ C ↔ ∀x:α, ∥f x∥ ≤ C := by simpa using @dist_le _ _ _ _ f 0 _ C0 lemma norm_le_of_nonempty [nonempty α] {f : α →ᵇ β} {M : ℝ} : ∥f∥ ≤ M ↔ ∀ x, ∥f x∥ ≤ M := begin simp_rw [norm_def, ←dist_zero_right], exact dist_le_iff_of_nonempty, end lemma norm_lt_iff_of_compact [compact_space α] {f : α →ᵇ β} {M : ℝ} (M0 : 0 < M) : ∥f∥ < M ↔ ∀ x, ∥f x∥ < M := begin simp_rw [norm_def, ←dist_zero_right], exact dist_lt_iff_of_compact M0, end lemma norm_lt_iff_of_nonempty_compact [nonempty α] [compact_space α] {f : α →ᵇ β} {M : ℝ} : ∥f∥ < M ↔ ∀ x, ∥f x∥ < M := begin simp_rw [norm_def, ←dist_zero_right], exact dist_lt_iff_of_nonempty_compact, end variable (f) /-- Norm of `const α b` is less than or equal to `∥b∥`. If `α` is nonempty, then it is equal to `∥b∥`. -/ lemma norm_const_le (b : β) : ∥const α b∥ ≤ ∥b∥ := (norm_le (norm_nonneg b)).2 $ λ x, le_refl _ @[simp] lemma norm_const_eq [h : nonempty α] (b : β) : ∥const α b∥ = ∥b∥ := le_antisymm (norm_const_le b) $ h.elim $ λ x, (const α b).norm_coe_le_norm x /-- Constructing a bounded continuous function from a uniformly bounded continuous function taking values in a normed group. -/ def of_normed_group {α : Type u} {β : Type v} [topological_space α] [normed_group β] (f : α → β) (Hf : continuous f) (C : ℝ) (H : ∀x, ∥f x∥ ≤ C) : α →ᵇ β := ⟨⟨λn, f n, Hf⟩, ⟨_, dist_le_two_norm' H⟩⟩ @[simp] lemma coe_of_normed_group {α : Type u} {β : Type v} [topological_space α] [normed_group β] (f : α → β) (Hf : continuous f) (C : ℝ) (H : ∀x, ∥f x∥ ≤ C) : (of_normed_group f Hf C H : α → β) = f := rfl lemma norm_of_normed_group_le {f : α → β} (hfc : continuous f) {C : ℝ} (hC : 0 ≤ C) (hfC : ∀ x, ∥f x∥ ≤ C) : ∥of_normed_group f hfc C hfC∥ ≤ C := (norm_le hC).2 hfC /-- Constructing a bounded continuous function from a uniformly bounded function on a discrete space, taking values in a normed group -/ def of_normed_group_discrete {α : Type u} {β : Type v} [topological_space α] [discrete_topology α] [normed_group β] (f : α → β) (C : ℝ) (H : ∀x, norm (f x) ≤ C) : α →ᵇ β := of_normed_group f continuous_of_discrete_topology C H @[simp] lemma coe_of_normed_group_discrete {α : Type u} {β : Type v} [topological_space α] [discrete_topology α] [normed_group β] (f : α → β) (C : ℝ) (H : ∀x, ∥f x∥ ≤ C) : (of_normed_group_discrete f C H : α → β) = f := rfl /-- Taking the pointwise norm of a bounded continuous function with values in a `normed_group`, yields a bounded continuous function with values in ℝ. -/ def norm_comp : α →ᵇ ℝ := of_normed_group (norm ∘ f) (by continuity) ∥f∥ (λ x, by simp only [f.norm_coe_le_norm, norm_norm]) @[simp] lemma coe_norm_comp : (f.norm_comp : α → ℝ) = norm ∘ f := rfl @[simp] lemma norm_norm_comp : ∥f.norm_comp∥ = ∥f∥ := by simp only [norm_eq, coe_norm_comp, norm_norm] lemma bdd_above_range_norm_comp : bdd_above $ set.range $ norm ∘ f := (real.bounded_iff_bdd_below_bdd_above.mp $ @bounded_range _ _ _ _ f.norm_comp).2 lemma norm_eq_supr_norm : ∥f∥ = ⨆ x : α, ∥f x∥ := begin casesI is_empty_or_nonempty α with hα _, { suffices : range (norm ∘ f) = ∅, { rw [f.norm_eq_zero_of_empty, supr, this, real.Sup_empty], }, simp only [hα, range_eq_empty, not_nonempty_iff], }, { rw [norm_eq_of_nonempty, supr, ← cInf_upper_bounds_eq_cSup f.bdd_above_range_norm_comp (range_nonempty _)], congr, ext, simp only [forall_apply_eq_imp_iff', mem_range, exists_imp_distrib], }, end /-- The pointwise opposite of a bounded continuous function is again bounded continuous. -/ instance : has_neg (α →ᵇ β) := ⟨λf, of_normed_group (-f) f.continuous.neg ∥f∥ $ λ x, trans_rel_right _ (norm_neg _) (f.norm_coe_le_norm x)⟩ /-- The pointwise difference of two bounded continuous functions is again bounded continuous. -/ instance : has_sub (α →ᵇ β) := ⟨λf g, of_normed_group (f - g) (f.continuous.sub g.continuous) (∥f∥ + ∥g∥) $ λ x, by { simp only [sub_eq_add_neg], exact le_trans (norm_add_le _ _) (add_le_add (f.norm_coe_le_norm x) $ trans_rel_right _ (norm_neg _) (g.norm_coe_le_norm x)) }⟩ @[simp] lemma coe_neg : ⇑(-f) = -f := rfl lemma neg_apply : (-f) x = -f x := rfl instance : add_comm_group (α →ᵇ β) := { add_left_neg := assume f, by ext; simp, add_comm := assume f g, by ext; simp [add_comm], sub_eq_add_neg := assume f g, by { ext, apply sub_eq_add_neg }, ..bounded_continuous_function.add_monoid, ..bounded_continuous_function.has_neg, ..bounded_continuous_function.has_sub } @[simp] lemma coe_sub : ⇑(f - g) = f - g := rfl lemma sub_apply : (f - g) x = f x - g x := rfl instance : normed_group (α →ᵇ β) := { dist_eq := λ f g, by simp only [norm_eq, dist_eq, dist_eq_norm, sub_apply] } lemma abs_diff_coe_le_dist : ∥f x - g x∥ ≤ dist f g := by { rw dist_eq_norm, exact (f - g).norm_coe_le_norm x } lemma coe_le_coe_add_dist {f g : α →ᵇ ℝ} : f x ≤ g x + dist f g := sub_le_iff_le_add'.1 $ (abs_le.1 $ @dist_coe_le_dist _ _ _ _ f g x).2 end normed_group section has_bounded_smul /-! ### `has_bounded_smul` (in particular, topological module) structure In this section, if `β` is a metric space and a `𝕜`-module whose addition and scalar multiplication are compatible with the metric structure, then we show that the space of bounded continuous functions from `α` to `β` inherits a so-called `has_bounded_smul` structure (in particular, a `has_continuous_mul` structure, which is the mathlib formulation of being a topological module), by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables {𝕜 : Type*} [metric_space 𝕜] [semiring 𝕜] variables [topological_space α] [metric_space β] [add_comm_monoid β] [module 𝕜 β] [has_bounded_smul 𝕜 β] variables {f g : α →ᵇ β} {x : α} {C : ℝ} instance : has_scalar 𝕜 (α →ᵇ β) := ⟨λ c f, bounded_continuous_function.mk_of_bound (c • f.to_continuous_map) (dist c 0 * (classical.some f.bounded)) begin intros x y, refine (dist_smul_pair c (f x) (f y)).trans _, refine mul_le_mul_of_nonneg_left _ dist_nonneg, exact classical.some_spec f.bounded x y end ⟩ @[simp] lemma coe_smul (c : 𝕜) (f : α →ᵇ β) : ⇑(c • f) = λ x, c • (f x) := rfl lemma smul_apply (c : 𝕜) (f : α →ᵇ β) (x : α) : (c • f) x = c • f x := rfl instance : has_bounded_smul 𝕜 (α →ᵇ β) := { dist_smul_pair' := λ c f₁ f₂, begin rw dist_le (mul_nonneg dist_nonneg dist_nonneg), intros x, refine (dist_smul_pair c (f₁ x) (f₂ x)).trans _, exact mul_le_mul_of_nonneg_left (dist_coe_le_dist x) dist_nonneg end, dist_pair_smul' := λ c₁ c₂ f, begin rw dist_le (mul_nonneg dist_nonneg dist_nonneg), intros x, refine (dist_pair_smul c₁ c₂ (f x)).trans _, convert mul_le_mul_of_nonneg_left (dist_coe_le_dist x) dist_nonneg, simp end } variables [has_lipschitz_add β] instance : module 𝕜 (α →ᵇ β) := { smul := (•), smul_add := λ c f g, ext $ λ x, smul_add c (f x) (g x), add_smul := λ c₁ c₂ f, ext $ λ x, add_smul c₁ c₂ (f x), mul_smul := λ c₁ c₂ f, ext $ λ x, mul_smul c₁ c₂ (f x), one_smul := λ f, ext $ λ x, one_smul 𝕜 (f x), smul_zero := λ c, ext $ λ x, smul_zero c, zero_smul := λ f, ext $ λ x, zero_smul 𝕜 (f x), .. bounded_continuous_function.add_comm_monoid } variables (𝕜) /-- The evaluation at a point, as a continuous linear map from `α →ᵇ β` to `β`. -/ def eval_clm (x : α) : (α →ᵇ β) →L[𝕜] β := { to_fun := λ f, f x, map_add' := λ f g, by simp only [pi.add_apply, coe_add], map_smul' := λ c f, by simp only [coe_smul, ring_hom.id_apply] } @[simp] lemma eval_clm_apply (x : α) (f : α →ᵇ β) : eval_clm 𝕜 x f = f x := rfl variables (α β) /-- The linear map forgetting that a bounded continuous function is bounded. -/ @[simps] def forget_boundedness_linear_map : (α →ᵇ β) →ₗ[𝕜] C(α, β) := { to_fun := forget_boundedness α β, map_smul' := by { intros, ext, simp, }, map_add' := by { intros, ext, simp, }, } end has_bounded_smul section normed_space /-! ### Normed space structure In this section, if `β` is a normed space, then we show that the space of bounded continuous functions from `α` to `β` inherits a normed space structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables {𝕜 : Type*} variables [topological_space α] [normed_group β] variables {f g : α →ᵇ β} {x : α} {C : ℝ} instance [normed_field 𝕜] [normed_space 𝕜 β] : normed_space 𝕜 (α →ᵇ β) := ⟨λ c f, begin refine norm_of_normed_group_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _, exact (λ x, trans_rel_right _ (norm_smul _ _) (mul_le_mul_of_nonneg_left (f.norm_coe_le_norm _) (norm_nonneg _))) end⟩ variables [nondiscrete_normed_field 𝕜] [normed_space 𝕜 β] variables [normed_group γ] [normed_space 𝕜 γ] variables (α) -- TODO does this work in the `has_bounded_smul` setting, too? /-- Postcomposition of bounded continuous functions into a normed module by a continuous linear map is a continuous linear map. Upgraded version of `continuous_linear_map.comp_left_continuous`, similar to `linear_map.comp_left`. -/ protected def _root_.continuous_linear_map.comp_left_continuous_bounded (g : β →L[𝕜] γ) : (α →ᵇ β) →L[𝕜] (α →ᵇ γ) := linear_map.mk_continuous { to_fun := λ f, of_normed_group (g ∘ f) (g.continuous.comp f.continuous) (∥g∥ * ∥f∥) (λ x, (g.le_op_norm_of_le (f.norm_coe_le_norm x))), map_add' := λ f g, by ext; simp, map_smul' := λ c f, by ext; simp } ∥g∥ (λ f, norm_of_normed_group_le _ (mul_nonneg (norm_nonneg g) (norm_nonneg f)) _) @[simp] lemma _root_.continuous_linear_map.comp_left_continuous_bounded_apply (g : β →L[𝕜] γ) (f : α →ᵇ β) (x : α) : (g.comp_left_continuous_bounded α f) x = g (f x) := rfl end normed_space section normed_ring /-! ### Normed ring structure In this section, if `R` is a normed ring, then we show that the space of bounded continuous functions from `α` to `R` inherits a normed ring structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables [topological_space α] {R : Type*} [normed_ring R] instance : ring (α →ᵇ R) := { one := const α 1, mul := λ f g, of_normed_group (f * g) (f.continuous.mul g.continuous) (∥f∥ * ∥g∥) $ λ x, le_trans (normed_ring.norm_mul (f x) (g x)) $ mul_le_mul (f.norm_coe_le_norm x) (g.norm_coe_le_norm x) (norm_nonneg _) (norm_nonneg _), one_mul := λ f, ext $ λ x, one_mul (f x), mul_one := λ f, ext $ λ x, mul_one (f x), mul_assoc := λ f₁ f₂ f₃, ext $ λ x, mul_assoc _ _ _, left_distrib := λ f₁ f₂ f₃, ext $ λ x, left_distrib _ _ _, right_distrib := λ f₁ f₂ f₃, ext $ λ x, right_distrib _ _ _, .. bounded_continuous_function.add_comm_group } @[simp] lemma coe_mul (f g : α →ᵇ R) : ⇑(f * g) = f * g := rfl lemma mul_apply (f g : α →ᵇ R) (x : α) : (f * g) x = f x * g x := rfl instance : normed_ring (α →ᵇ R) := { norm_mul := λ f g, norm_of_normed_group_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _, .. bounded_continuous_function.normed_group } end normed_ring section normed_comm_ring /-! ### Normed commutative ring structure In this section, if `R` is a normed commutative ring, then we show that the space of bounded continuous functions from `α` to `R` inherits a normed commutative ring structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables [topological_space α] {R : Type*} [normed_comm_ring R] instance : comm_ring (α →ᵇ R) := { mul_comm := λ f₁ f₂, ext $ λ x, mul_comm _ _, .. bounded_continuous_function.ring } instance : normed_comm_ring (α →ᵇ R) := { .. bounded_continuous_function.comm_ring, .. bounded_continuous_function.normed_group } end normed_comm_ring section normed_algebra /-! ### Normed algebra structure In this section, if `γ` is a normed algebra, then we show that the space of bounded continuous functions from `α` to `γ` inherits a normed algebra structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables {𝕜 : Type*} [normed_field 𝕜] variables [topological_space α] [normed_group β] [normed_space 𝕜 β] variables [normed_ring γ] [normed_algebra 𝕜 γ] variables {f g : α →ᵇ γ} {x : α} {c : 𝕜} /-- `bounded_continuous_function.const` as a `ring_hom`. -/ def C : 𝕜 →+* (α →ᵇ γ) := { to_fun := λ (c : 𝕜), const α ((algebra_map 𝕜 γ) c), map_one' := ext $ λ x, (algebra_map 𝕜 γ).map_one, map_mul' := λ c₁ c₂, ext $ λ x, (algebra_map 𝕜 γ).map_mul _ _, map_zero' := ext $ λ x, (algebra_map 𝕜 γ).map_zero, map_add' := λ c₁ c₂, ext $ λ x, (algebra_map 𝕜 γ).map_add _ _ } instance : algebra 𝕜 (α →ᵇ γ) := { to_ring_hom := C, commutes' := λ c f, ext $ λ x, algebra.commutes' _ _, smul_def' := λ c f, ext $ λ x, algebra.smul_def' _ _, ..bounded_continuous_function.module, ..bounded_continuous_function.ring } @[simp] lemma algebra_map_apply (k : 𝕜) (a : α) : algebra_map 𝕜 (α →ᵇ γ) k a = k • 1 := by { rw algebra.algebra_map_eq_smul_one, refl, } instance [nonempty α] : normed_algebra 𝕜 (α →ᵇ γ) := { norm_algebra_map_eq := λ c, begin calc ∥ (algebra_map 𝕜 (α →ᵇ γ)).to_fun c∥ = ∥(algebra_map 𝕜 γ) c∥ : _ ... = ∥c∥ : norm_algebra_map_eq _ _, apply norm_const_eq ((algebra_map 𝕜 γ) c), assumption, end, ..bounded_continuous_function.algebra } /-! ### Structure as normed module over scalar functions If `β` is a normed `𝕜`-space, then we show that the space of bounded continuous functions from `α` to `β` is naturally a module over the algebra of bounded continuous functions from `α` to `𝕜`. -/ instance has_scalar' : has_scalar (α →ᵇ 𝕜) (α →ᵇ β) := ⟨λ (f : α →ᵇ 𝕜) (g : α →ᵇ β), of_normed_group (λ x, (f x) • (g x)) (f.continuous.smul g.continuous) (∥f∥ * ∥g∥) (λ x, calc ∥f x • g x∥ ≤ ∥f x∥ * ∥g x∥ : normed_space.norm_smul_le _ _ ... ≤ ∥f∥ * ∥g∥ : mul_le_mul (f.norm_coe_le_norm _) (g.norm_coe_le_norm _) (norm_nonneg _) (norm_nonneg _)) ⟩ instance module' : module (α →ᵇ 𝕜) (α →ᵇ β) := module.of_core $ { smul := (•), smul_add := λ c f₁ f₂, ext $ λ x, smul_add _ _ _, add_smul := λ c₁ c₂ f, ext $ λ x, add_smul _ _ _, mul_smul := λ c₁ c₂ f, ext $ λ x, mul_smul _ _ _, one_smul := λ f, ext $ λ x, one_smul 𝕜 (f x) } lemma norm_smul_le (f : α →ᵇ 𝕜) (g : α →ᵇ β) : ∥f • g∥ ≤ ∥f∥ * ∥g∥ := norm_of_normed_group_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _ /- TODO: When `normed_module` has been added to `normed_space.basic`, the above facts show that the space of bounded continuous functions from `α` to `β` is naturally a normed module over the algebra of bounded continuous functions from `α` to `𝕜`. -/ end normed_algebra end bounded_continuous_function
578e1fd6b212793aa8b4719305c77d21a0188af5
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/analysis/calculus/formal_multilinear_series.lean
d08591e5e5873c936270dc493fc0ae9376a6e1a8
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
6,978
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.multilinear /-! # Formal multilinear series In this file we define `formal_multilinear_series 𝕜 E F` to be a family of `n`-multilinear maps for all `n`, designed to model the sequence of derivatives of a function. In other files we use this notion to define `C^n` functions (called `cont_diff` in `mathlib`) and analytic functions. ## Notations We use the notation `E [×n]→L[𝕜] F` for the space of continuous multilinear maps on `E^n` with values in `F`. This is the space in which the `n`-th derivative of a function from `E` to `F` lives. ## Tags multilinear, formal series -/ noncomputable theory open set fin open_locale topological_space variables {𝕜 𝕜' E F G : Type*} section variables [comm_ring 𝕜] [add_comm_group E] [module 𝕜 E] [topological_space E] [topological_add_group E] [has_continuous_const_smul 𝕜 E] [add_comm_group F] [module 𝕜 F] [topological_space F] [topological_add_group F] [has_continuous_const_smul 𝕜 F] [add_comm_group G] [module 𝕜 G] [topological_space G] [topological_add_group G] [has_continuous_const_smul 𝕜 G] /-- A formal multilinear series over a field `𝕜`, from `E` to `F`, is given by a family of multilinear maps from `E^n` to `F` for all `n`. -/ @[derive add_comm_group, nolint unused_arguments] def formal_multilinear_series (𝕜 : Type*) (E : Type*) (F : Type*) [ring 𝕜] [add_comm_group E] [module 𝕜 E] [topological_space E] [topological_add_group E] [has_continuous_const_smul 𝕜 E] [add_comm_group F] [module 𝕜 F] [topological_space F] [topological_add_group F] [has_continuous_const_smul 𝕜 F] := Π (n : ℕ), (E [×n]→L[𝕜] F) instance : inhabited (formal_multilinear_series 𝕜 E F) := ⟨0⟩ section module /- `derive` is not able to find the module structure, probably because Lean is confused by the dependent types. We register it explicitly. -/ instance : module 𝕜 (formal_multilinear_series 𝕜 E F) := begin letI : Π n, module 𝕜 (continuous_multilinear_map 𝕜 (λ (i : fin n), E) F) := λ n, by apply_instance, refine pi.module _ _ _, end end module namespace formal_multilinear_series /-- Killing the zeroth coefficient in a formal multilinear series -/ def remove_zero (p : formal_multilinear_series 𝕜 E F) : formal_multilinear_series 𝕜 E F | 0 := 0 | (n + 1) := p (n + 1) @[simp] lemma remove_zero_coeff_zero (p : formal_multilinear_series 𝕜 E F) : p.remove_zero 0 = 0 := rfl @[simp] lemma remove_zero_coeff_succ (p : formal_multilinear_series 𝕜 E F) (n : ℕ) : p.remove_zero (n+1) = p (n+1) := rfl lemma remove_zero_of_pos (p : formal_multilinear_series 𝕜 E F) {n : ℕ} (h : 0 < n) : p.remove_zero n = p n := by { rw ← nat.succ_pred_eq_of_pos h, refl } /-- Convenience congruence lemma stating in a dependent setting that, if the arguments to a formal multilinear series are equal, then the values are also equal. -/ lemma congr (p : formal_multilinear_series 𝕜 E F) {m n : ℕ} {v : fin m → E} {w : fin n → E} (h1 : m = n) (h2 : ∀ (i : ℕ) (him : i < m) (hin : i < n), v ⟨i, him⟩ = w ⟨i, hin⟩) : p m v = p n w := by { cases h1, congr' with ⟨i, hi⟩, exact h2 i hi hi } /-- Composing each term `pₙ` in a formal multilinear series with `(u, ..., u)` where `u` is a fixed continuous linear map, gives a new formal multilinear series `p.comp_continuous_linear_map u`. -/ def comp_continuous_linear_map (p : formal_multilinear_series 𝕜 F G) (u : E →L[𝕜] F) : formal_multilinear_series 𝕜 E G := λ n, (p n).comp_continuous_linear_map (λ (i : fin n), u) @[simp] lemma comp_continuous_linear_map_apply (p : formal_multilinear_series 𝕜 F G) (u : E →L[𝕜] F) (n : ℕ) (v : fin n → E) : (p.comp_continuous_linear_map u) n v = p n (u ∘ v) := rfl variables (𝕜) [comm_ring 𝕜'] [has_smul 𝕜 𝕜'] variables [module 𝕜' E] [has_continuous_const_smul 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] variables [module 𝕜' F] [has_continuous_const_smul 𝕜' F] [is_scalar_tower 𝕜 𝕜' F] /-- Reinterpret a formal `𝕜'`-multilinear series as a formal `𝕜`-multilinear series. -/ @[simp] protected def restrict_scalars (p : formal_multilinear_series 𝕜' E F) : formal_multilinear_series 𝕜 E F := λ n, (p n).restrict_scalars 𝕜 end formal_multilinear_series end namespace formal_multilinear_series variables [nontrivially_normed_field 𝕜] [normed_add_comm_group E] [normed_space 𝕜 E] [normed_add_comm_group F] [normed_space 𝕜 F] [normed_add_comm_group G] [normed_space 𝕜 G] variables (p : formal_multilinear_series 𝕜 E F) /-- Forgetting the zeroth term in a formal multilinear series, and interpreting the following terms as multilinear maps into `E →L[𝕜] F`. If `p` corresponds to the Taylor series of a function, then `p.shift` is the Taylor series of the derivative of the function. -/ def shift : formal_multilinear_series 𝕜 E (E →L[𝕜] F) := λn, (p n.succ).curry_right /-- Adding a zeroth term to a formal multilinear series taking values in `E →L[𝕜] F`. This corresponds to starting from a Taylor series for the derivative of a function, and building a Taylor series for the function itself. -/ def unshift (q : formal_multilinear_series 𝕜 E (E →L[𝕜] F)) (z : F) : formal_multilinear_series 𝕜 E F | 0 := (continuous_multilinear_curry_fin0 𝕜 E F).symm z | (n + 1) := continuous_multilinear_curry_right_equiv' 𝕜 n E F (q n) end formal_multilinear_series namespace continuous_linear_map variables [comm_ring 𝕜] [add_comm_group E] [module 𝕜 E] [topological_space E] [topological_add_group E] [has_continuous_const_smul 𝕜 E] [add_comm_group F] [module 𝕜 F] [topological_space F] [topological_add_group F] [has_continuous_const_smul 𝕜 F] [add_comm_group G] [module 𝕜 G] [topological_space G] [topological_add_group G] [has_continuous_const_smul 𝕜 G] /-- Composing each term `pₙ` in a formal multilinear series with a continuous linear map `f` on the left gives a new formal multilinear series `f.comp_formal_multilinear_series p` whose general term is `f ∘ pₙ`. -/ def comp_formal_multilinear_series (f : F →L[𝕜] G) (p : formal_multilinear_series 𝕜 E F) : formal_multilinear_series 𝕜 E G := λ n, f.comp_continuous_multilinear_map (p n) @[simp] lemma comp_formal_multilinear_series_apply (f : F →L[𝕜] G) (p : formal_multilinear_series 𝕜 E F) (n : ℕ) : (f.comp_formal_multilinear_series p) n = f.comp_continuous_multilinear_map (p n) := rfl lemma comp_formal_multilinear_series_apply' (f : F →L[𝕜] G) (p : formal_multilinear_series 𝕜 E F) (n : ℕ) (v : fin n → E) : (f.comp_formal_multilinear_series p) n v = f (p n v) := rfl end continuous_linear_map
fce19301b65158aed60030c7c62cd3261714aa1e
94e33a31faa76775069b071adea97e86e218a8ee
/src/data/real/nnreal.lean
dd5d8cac4c6bf1ab5a827c8d49cdc0707910f0df
[ "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
34,671
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.big_operators.ring import data.real.pointwise import algebra.indicator_function import algebra.algebra.basic import algebra.order.module import algebra.order.nonneg /-! # Nonnegative real numbers In this file we define `nnreal` (notation: `ℝ≥0`) to be the type of non-negative real numbers, a.k.a. the interval `[0, ∞)`. We also define the following operations and structures on `ℝ≥0`: * the order on `ℝ≥0` is the restriction of the order on `ℝ`; these relations define a conditionally complete linear order with a bottom element, `conditionally_complete_linear_order_bot`; * `a + b` and `a * b` are the restrictions of addition and multiplication of real numbers to `ℝ≥0`; these operations together with `0 = ⟨0, _⟩` and `1 = ⟨1, _⟩` turn `ℝ≥0` into a conditionally complete linear ordered archimedean commutative semifield; we have no typeclass for this in `mathlib` yet, so we define the following instances instead: - `linear_ordered_semiring ℝ≥0`; - `ordered_comm_semiring ℝ≥0`; - `canonically_ordered_comm_semiring ℝ≥0`; - `linear_ordered_comm_group_with_zero ℝ≥0`; - `canonically_linear_ordered_add_monoid ℝ≥0`; - `archimedean ℝ≥0`; - `conditionally_complete_linear_order_bot ℝ≥0`. These instances are derived from corresponding instances about the type `{x : α // 0 ≤ x}` in an appropriate ordered field/ring/group/monoid `α`. See `algebra/order/nonneg`. * `real.to_nnreal x` is defined as `⟨max x 0, _⟩`, i.e. `↑(real.to_nnreal x) = x` when `0 ≤ x` and `↑(real.to_nnreal x) = 0` otherwise. We also define an instance `can_lift ℝ ℝ≥0`. This instance can be used by the `lift` tactic to replace `x : ℝ` and `hx : 0 ≤ x` in the proof context with `x : ℝ≥0` while replacing all occurences of `x` with `↑x`. This tactic also works for a function `f : α → ℝ` with a hypothesis `hf : ∀ x, 0 ≤ f x`. ## Notations This file defines `ℝ≥0` as a localized notation for `nnreal`. ## TODO `semifield` instance -/ open_locale classical big_operators /-- Nonnegative real numbers. -/ @[derive [ ordered_semiring, comm_monoid_with_zero, -- to ensure these instance are computable floor_semiring, semilattice_inf, densely_ordered, order_bot, canonically_linear_ordered_add_monoid, linear_ordered_comm_group_with_zero, archimedean, linear_ordered_semiring, ordered_comm_semiring, canonically_ordered_comm_semiring, has_sub, has_ordered_sub, has_div, inhabited]] def nnreal := {r : ℝ // 0 ≤ r} localized "notation ` ℝ≥0 ` := nnreal" in nnreal namespace nnreal instance : has_coe ℝ≥0 ℝ := ⟨subtype.val⟩ /- Simp lemma to put back `n.val` into the normal form given by the coercion. -/ @[simp] lemma val_eq_coe (n : ℝ≥0) : n.val = n := rfl instance : can_lift ℝ ℝ≥0 := subtype.can_lift _ protected lemma eq {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) → n = m := subtype.eq protected lemma eq_iff {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) ↔ n = m := iff.intro nnreal.eq (congr_arg coe) lemma ne_iff {x y : ℝ≥0} : (x : ℝ) ≠ (y : ℝ) ↔ x ≠ y := not_iff_not_of_iff $ nnreal.eq_iff protected lemma «forall» {p : ℝ≥0 → Prop} : (∀ x : ℝ≥0, p x) ↔ ∀ (x : ℝ) (hx : 0 ≤ x), p ⟨x, hx⟩ := subtype.forall protected lemma «exists» {p : ℝ≥0 → Prop} : (∃ x : ℝ≥0, p x) ↔ ∃ (x : ℝ) (hx : 0 ≤ x), p ⟨x, hx⟩ := subtype.exists /-- Reinterpret a real number `r` as a non-negative real number. Returns `0` if `r < 0`. -/ noncomputable def _root_.real.to_nnreal (r : ℝ) : ℝ≥0 := ⟨max r 0, le_max_right _ _⟩ lemma _root_.real.coe_to_nnreal (r : ℝ) (hr : 0 ≤ r) : (real.to_nnreal r : ℝ) = r := max_eq_left hr lemma _root_.real.le_coe_to_nnreal (r : ℝ) : r ≤ real.to_nnreal r := le_max_left r 0 lemma coe_nonneg (r : ℝ≥0) : (0 : ℝ) ≤ r := r.2 @[norm_cast] theorem coe_mk (a : ℝ) (ha) : ((⟨a, ha⟩ : ℝ≥0) : ℝ) = a := rfl example : has_zero ℝ≥0 := by apply_instance example : has_one ℝ≥0 := by apply_instance example : has_add ℝ≥0 := by apply_instance noncomputable example : has_sub ℝ≥0 := by apply_instance example : has_mul ℝ≥0 := by apply_instance noncomputable example : has_inv ℝ≥0 := by apply_instance noncomputable example : has_div ℝ≥0 := by apply_instance example : has_le ℝ≥0 := by apply_instance example : has_bot ℝ≥0 := by apply_instance example : inhabited ℝ≥0 := by apply_instance example : nontrivial ℝ≥0 := by apply_instance protected lemma coe_injective : function.injective (coe : ℝ≥0 → ℝ) := subtype.coe_injective @[simp, norm_cast] protected lemma coe_eq {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) = r₂ ↔ r₁ = r₂ := nnreal.coe_injective.eq_iff protected lemma coe_zero : ((0 : ℝ≥0) : ℝ) = 0 := rfl protected lemma coe_one : ((1 : ℝ≥0) : ℝ) = 1 := rfl protected lemma coe_add (r₁ r₂ : ℝ≥0) : ((r₁ + r₂ : ℝ≥0) : ℝ) = r₁ + r₂ := rfl protected lemma coe_mul (r₁ r₂ : ℝ≥0) : ((r₁ * r₂ : ℝ≥0) : ℝ) = r₁ * r₂ := rfl protected lemma coe_inv (r : ℝ≥0) : ((r⁻¹ : ℝ≥0) : ℝ) = r⁻¹ := rfl protected lemma coe_div (r₁ r₂ : ℝ≥0) : ((r₁ / r₂ : ℝ≥0) : ℝ) = r₁ / r₂ := rfl @[simp, norm_cast] protected lemma coe_bit0 (r : ℝ≥0) : ((bit0 r : ℝ≥0) : ℝ) = bit0 r := rfl @[simp, norm_cast] protected lemma coe_bit1 (r : ℝ≥0) : ((bit1 r : ℝ≥0) : ℝ) = bit1 r := rfl protected lemma coe_two : ((2 : ℝ≥0) : ℝ) = 2 := rfl @[simp, norm_cast] protected lemma coe_sub {r₁ r₂ : ℝ≥0} (h : r₂ ≤ r₁) : ((r₁ - r₂ : ℝ≥0) : ℝ) = r₁ - r₂ := max_eq_left $ le_sub.2 $ by simp [show (r₂ : ℝ) ≤ r₁, from h] -- TODO: setup semifield! @[simp, norm_cast] protected lemma coe_eq_zero (r : ℝ≥0) : ↑r = (0 : ℝ) ↔ r = 0 := by rw [← nnreal.coe_zero, nnreal.coe_eq] @[simp, norm_cast] protected lemma coe_eq_one (r : ℝ≥0) : ↑r = (1 : ℝ) ↔ r = 1 := by rw [← nnreal.coe_one, nnreal.coe_eq] lemma coe_ne_zero {r : ℝ≥0} : (r : ℝ) ≠ 0 ↔ r ≠ 0 := by norm_cast example : comm_semiring ℝ≥0 := by apply_instance /-- Coercion `ℝ≥0 → ℝ` as a `ring_hom`. -/ def to_real_hom : ℝ≥0 →+* ℝ := ⟨coe, nnreal.coe_one, nnreal.coe_mul, nnreal.coe_zero, nnreal.coe_add⟩ @[simp] lemma coe_to_real_hom : ⇑to_real_hom = coe := rfl section actions /-- A `mul_action` over `ℝ` restricts to a `mul_action` over `ℝ≥0`. -/ instance {M : Type*} [mul_action ℝ M] : mul_action ℝ≥0 M := mul_action.comp_hom M to_real_hom.to_monoid_hom lemma smul_def {M : Type*} [mul_action ℝ M] (c : ℝ≥0) (x : M) : c • x = (c : ℝ) • x := rfl instance {M N : Type*} [mul_action ℝ M] [mul_action ℝ N] [has_smul M N] [is_scalar_tower ℝ M N] : is_scalar_tower ℝ≥0 M N := { smul_assoc := λ r, (smul_assoc (r : ℝ) : _)} instance smul_comm_class_left {M N : Type*} [mul_action ℝ N] [has_smul M N] [smul_comm_class ℝ M N] : smul_comm_class ℝ≥0 M N := { smul_comm := λ r, (smul_comm (r : ℝ) : _)} instance smul_comm_class_right {M N : Type*} [mul_action ℝ N] [has_smul M N] [smul_comm_class M ℝ N] : smul_comm_class M ℝ≥0 N := { smul_comm := λ m r, (smul_comm m (r : ℝ) : _)} /-- A `distrib_mul_action` over `ℝ` restricts to a `distrib_mul_action` over `ℝ≥0`. -/ instance {M : Type*} [add_monoid M] [distrib_mul_action ℝ M] : distrib_mul_action ℝ≥0 M := distrib_mul_action.comp_hom M to_real_hom.to_monoid_hom /-- A `module` over `ℝ` restricts to a `module` over `ℝ≥0`. -/ instance {M : Type*} [add_comm_monoid M] [module ℝ M] : module ℝ≥0 M := module.comp_hom M to_real_hom /-- An `algebra` over `ℝ` restricts to an `algebra` over `ℝ≥0`. -/ instance {A : Type*} [semiring A] [algebra ℝ A] : algebra ℝ≥0 A := { smul := (•), commutes' := λ r x, by simp [algebra.commutes], smul_def' := λ r x, by simp [←algebra.smul_def (r : ℝ) x, smul_def], to_ring_hom := ((algebra_map ℝ A).comp (to_real_hom : ℝ≥0 →+* ℝ)) } -- verify that the above produces instances we might care about example : algebra ℝ≥0 ℝ := by apply_instance example : distrib_mul_action ℝ≥0ˣ ℝ := by apply_instance end actions example : monoid_with_zero ℝ≥0 := by apply_instance example : comm_monoid_with_zero ℝ≥0 := by apply_instance noncomputable example : comm_group_with_zero ℝ≥0 := by apply_instance @[simp, norm_cast] lemma coe_indicator {α} (s : set α) (f : α → ℝ≥0) (a : α) : ((s.indicator f a : ℝ≥0) : ℝ) = s.indicator (λ x, f x) a := (to_real_hom : ℝ≥0 →+ ℝ).map_indicator _ _ _ @[simp, norm_cast] lemma coe_pow (r : ℝ≥0) (n : ℕ) : ((r^n : ℝ≥0) : ℝ) = r^n := to_real_hom.map_pow r n @[simp, norm_cast] lemma coe_zpow (r : ℝ≥0) (n : ℤ) : ((r^n : ℝ≥0) : ℝ) = r^n := by cases n; simp @[norm_cast] lemma coe_list_sum (l : list ℝ≥0) : ((l.sum : ℝ≥0) : ℝ) = (l.map coe).sum := to_real_hom.map_list_sum l @[norm_cast] lemma coe_list_prod (l : list ℝ≥0) : ((l.prod : ℝ≥0) : ℝ) = (l.map coe).prod := to_real_hom.map_list_prod l @[norm_cast] lemma coe_multiset_sum (s : multiset ℝ≥0) : ((s.sum : ℝ≥0) : ℝ) = (s.map coe).sum := to_real_hom.map_multiset_sum s @[norm_cast] lemma coe_multiset_prod (s : multiset ℝ≥0) : ((s.prod : ℝ≥0) : ℝ) = (s.map coe).prod := to_real_hom.map_multiset_prod s @[norm_cast] lemma coe_sum {α} {s : finset α} {f : α → ℝ≥0} : ↑(∑ a in s, f a) = ∑ a in s, (f a : ℝ) := to_real_hom.map_sum _ _ lemma _root_.real.to_nnreal_sum_of_nonneg {α} {s : finset α} {f : α → ℝ} (hf : ∀ a, a ∈ s → 0 ≤ f a) : real.to_nnreal (∑ a in s, f a) = ∑ a in s, real.to_nnreal (f a) := begin rw [←nnreal.coe_eq, nnreal.coe_sum, real.coe_to_nnreal _ (finset.sum_nonneg hf)], exact finset.sum_congr rfl (λ x hxs, by rw real.coe_to_nnreal _ (hf x hxs)), end @[norm_cast] lemma coe_prod {α} {s : finset α} {f : α → ℝ≥0} : ↑(∏ a in s, f a) = ∏ a in s, (f a : ℝ) := to_real_hom.map_prod _ _ lemma _root_.real.to_nnreal_prod_of_nonneg {α} {s : finset α} {f : α → ℝ} (hf : ∀ a, a ∈ s → 0 ≤ f a) : real.to_nnreal (∏ a in s, f a) = ∏ a in s, real.to_nnreal (f a) := begin rw [←nnreal.coe_eq, nnreal.coe_prod, real.coe_to_nnreal _ (finset.prod_nonneg hf)], exact finset.prod_congr rfl (λ x hxs, by rw real.coe_to_nnreal _ (hf x hxs)), end lemma nsmul_coe (r : ℝ≥0) (n : ℕ) : ↑(n • r) = n • (r:ℝ) := by norm_cast @[simp, norm_cast] protected lemma coe_nat_cast (n : ℕ) : (↑(↑n : ℝ≥0) : ℝ) = n := map_nat_cast to_real_hom n noncomputable example : linear_order ℝ≥0 := by apply_instance @[simp, norm_cast] protected lemma coe_le_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) ≤ r₂ ↔ r₁ ≤ r₂ := iff.rfl @[simp, norm_cast] protected lemma coe_lt_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) < r₂ ↔ r₁ < r₂ := iff.rfl @[simp, norm_cast] protected lemma coe_pos {r : ℝ≥0} : (0 : ℝ) < r ↔ 0 < r := iff.rfl protected lemma coe_mono : monotone (coe : ℝ≥0 → ℝ) := λ _ _, nnreal.coe_le_coe.2 protected lemma _root_.real.to_nnreal_mono : monotone real.to_nnreal := λ x y h, max_le_max h (le_refl 0) @[simp] lemma _root_.real.to_nnreal_coe {r : ℝ≥0} : real.to_nnreal r = r := nnreal.eq $ max_eq_left r.2 @[simp] lemma mk_coe_nat (n : ℕ) : @eq ℝ≥0 (⟨(n : ℝ), n.cast_nonneg⟩ : ℝ≥0) n := nnreal.eq (nnreal.coe_nat_cast n).symm @[simp] lemma to_nnreal_coe_nat (n : ℕ) : real.to_nnreal n = n := nnreal.eq $ by simp [real.coe_to_nnreal] /-- `real.to_nnreal` and `coe : ℝ≥0 → ℝ` form a Galois insertion. -/ noncomputable def gi : galois_insertion real.to_nnreal coe := galois_insertion.monotone_intro nnreal.coe_mono real.to_nnreal_mono real.le_coe_to_nnreal (λ _, real.to_nnreal_coe) -- note that anything involving the (decidability of the) linear order, including `⊔`/`⊓` (min, max) -- will be noncomputable, everything else should not be. example : order_bot ℝ≥0 := by apply_instance example : partial_order ℝ≥0 := by apply_instance noncomputable example : canonically_linear_ordered_add_monoid ℝ≥0 := by apply_instance noncomputable example : linear_ordered_add_comm_monoid ℝ≥0 := by apply_instance noncomputable example : distrib_lattice ℝ≥0 := by apply_instance noncomputable example : semilattice_inf ℝ≥0 := by apply_instance noncomputable example : semilattice_sup ℝ≥0 := by apply_instance noncomputable example : linear_ordered_semiring ℝ≥0 := by apply_instance example : ordered_comm_semiring ℝ≥0 := by apply_instance noncomputable example : linear_ordered_comm_monoid ℝ≥0 := by apply_instance noncomputable example : linear_ordered_comm_monoid_with_zero ℝ≥0 := by apply_instance noncomputable example : linear_ordered_comm_group_with_zero ℝ≥0 := by apply_instance example : canonically_ordered_comm_semiring ℝ≥0 := by apply_instance example : densely_ordered ℝ≥0 := by apply_instance example : no_max_order ℝ≥0 := by apply_instance /-- If `a` is a nonnegative real number, then the closed interval `[0, a]` in `ℝ` is order isomorphic to the interval `set.Iic a`. -/ @[simps apply_coe_coe] def order_iso_Icc_zero_coe (a : ℝ≥0) : set.Icc (0 : ℝ) a ≃o set.Iic a := { to_equiv := equiv.set.sep (set.Ici 0) (λ x, x ≤ a), map_rel_iff' := λ x y, iff.rfl } @[simp] lemma order_iso_Icc_zero_coe_symm_apply_coe (a : ℝ≥0) (b : set.Iic a) : ((order_iso_Icc_zero_coe a).symm b : ℝ) = b := rfl -- note we need the `@` to make the `has_mem.mem` have a sensible type lemma coe_image {s : set ℝ≥0} : coe '' s = {x : ℝ | ∃ h : 0 ≤ x, @has_mem.mem (ℝ≥0) _ _ ⟨x, h⟩ s} := subtype.coe_image lemma bdd_above_coe {s : set ℝ≥0} : bdd_above ((coe : ℝ≥0 → ℝ) '' s) ↔ bdd_above s := iff.intro (assume ⟨b, hb⟩, ⟨real.to_nnreal b, assume ⟨y, hy⟩ hys, show y ≤ max b 0, from le_max_of_le_left $ hb $ set.mem_image_of_mem _ hys⟩) (assume ⟨b, hb⟩, ⟨b, assume y ⟨x, hx, eq⟩, eq ▸ hb hx⟩) lemma bdd_below_coe (s : set ℝ≥0) : bdd_below ((coe : ℝ≥0 → ℝ) '' s) := ⟨0, assume r ⟨q, _, eq⟩, eq ▸ q.2⟩ noncomputable instance : conditionally_complete_linear_order_bot ℝ≥0 := nonneg.conditionally_complete_linear_order_bot real.Sup_empty.le @[norm_cast] lemma coe_Sup (s : set ℝ≥0) : (↑(Sup s) : ℝ) = Sup ((coe : ℝ≥0 → ℝ) '' s) := eq.symm $ @subset_Sup_of_within ℝ (set.Ici 0) _ ⟨(0 : ℝ≥0)⟩ s $ real.Sup_nonneg _ $ λ y ⟨x, _, hy⟩, hy ▸ x.2 @[norm_cast] lemma coe_supr {ι : Sort*} (s : ι → ℝ≥0) : (↑(⨆ i, s i) : ℝ) = ⨆ i, (s i) := by rw [supr, supr, coe_Sup, set.range_comp] @[norm_cast] lemma coe_Inf (s : set ℝ≥0) : (↑(Inf s) : ℝ) = Inf ((coe : ℝ≥0 → ℝ) '' s) := eq.symm $ @subset_Inf_of_within ℝ (set.Ici 0) _ ⟨(0 : ℝ≥0)⟩ s $ real.Inf_nonneg _ $ λ y ⟨x, _, hy⟩, hy ▸ x.2 @[simp] lemma Inf_empty : Inf (∅ : set ℝ≥0) = 0 := by rw [← nnreal.coe_eq_zero, coe_Inf, set.image_empty, real.Inf_empty] @[norm_cast] lemma coe_infi {ι : Sort*} (s : ι → ℝ≥0) : (↑(⨅ i, s i) : ℝ) = ⨅ i, (s i) := by rw [infi, infi, coe_Inf, set.range_comp] lemma le_infi_add_infi {ι ι' : Sort*} [nonempty ι] [nonempty ι'] {f : ι → ℝ≥0} {g : ι' → ℝ≥0} {a : ℝ≥0} (h : ∀ i j, a ≤ f i + g j) : a ≤ (⨅ i, f i) + ⨅ j, g j := begin rw [← nnreal.coe_le_coe, nnreal.coe_add, coe_infi, coe_infi], exact le_cinfi_add_cinfi h end example : archimedean ℝ≥0 := by apply_instance -- TODO: why are these three instances necessary? why aren't they inferred? instance covariant_add : covariant_class ℝ≥0 ℝ≥0 (+) (≤) := ordered_add_comm_monoid.to_covariant_class_left ℝ≥0 instance contravariant_add : contravariant_class ℝ≥0 ℝ≥0 (+) (<) := ordered_cancel_add_comm_monoid.to_contravariant_class_left ℝ≥0 instance covariant_mul : covariant_class ℝ≥0 ℝ≥0 (*) (≤) := ordered_comm_monoid.to_covariant_class_left ℝ≥0 lemma le_of_forall_pos_le_add {a b : ℝ≥0} (h : ∀ε, 0 < ε → a ≤ b + ε) : a ≤ b := le_of_forall_le_of_dense $ assume x hxb, begin rcases le_iff_exists_add.1 (le_of_lt hxb) with ⟨ε, rfl⟩, exact h _ ((lt_add_iff_pos_right b).1 hxb) end lemma lt_iff_exists_rat_btwn (a b : ℝ≥0) : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < real.to_nnreal q ∧ real.to_nnreal q < b) := iff.intro (assume (h : (↑a:ℝ) < (↑b:ℝ)), let ⟨q, haq, hqb⟩ := exists_rat_btwn h in have 0 ≤ (q : ℝ), from le_trans a.2 $ le_of_lt haq, ⟨q, rat.cast_nonneg.1 this, by simp [real.coe_to_nnreal _ this, nnreal.coe_lt_coe.symm, haq, hqb]⟩) (assume ⟨q, _, haq, hqb⟩, lt_trans haq hqb) lemma bot_eq_zero : (⊥ : ℝ≥0) = 0 := rfl lemma mul_sup (a b c : ℝ≥0) : a * (b ⊔ c) = (a * b) ⊔ (a * c) := mul_max_of_nonneg _ _ $ zero_le a lemma sup_mul (a b c : ℝ≥0) : (a ⊔ b) * c = (a * c) ⊔ (b * c) := max_mul_of_nonneg _ _ $ zero_le c lemma mul_finset_sup {α} (r : ℝ≥0) (s : finset α) (f : α → ℝ≥0) : r * s.sup f = s.sup (λ a, r * f a) := (finset.comp_sup_eq_sup_comp _ (nnreal.mul_sup r) (mul_zero r)) lemma finset_sup_mul {α} (s : finset α) (f : α → ℝ≥0) (r : ℝ≥0) : s.sup f * r = s.sup (λ a, f a * r) := (finset.comp_sup_eq_sup_comp (* r) (λ x y, nnreal.sup_mul x y r) (zero_mul r)) lemma finset_sup_div {α} {f : α → ℝ≥0} {s : finset α} (r : ℝ≥0) : s.sup f / r = s.sup (λ a, f a / r) := by simp only [div_eq_inv_mul, mul_finset_sup] @[simp, norm_cast] lemma coe_max (x y : ℝ≥0) : ((max x y : ℝ≥0) : ℝ) = max (x : ℝ) (y : ℝ) := nnreal.coe_mono.map_max @[simp, norm_cast] lemma coe_min (x y : ℝ≥0) : ((min x y : ℝ≥0) : ℝ) = min (x : ℝ) (y : ℝ) := nnreal.coe_mono.map_min @[simp] lemma zero_le_coe {q : ℝ≥0} : 0 ≤ (q : ℝ) := q.2 end nnreal namespace real section to_nnreal @[simp] lemma to_nnreal_zero : real.to_nnreal 0 = 0 := by simp [real.to_nnreal]; refl @[simp] lemma to_nnreal_one : real.to_nnreal 1 = 1 := by simp [real.to_nnreal, max_eq_left (zero_le_one : (0 :ℝ) ≤ 1)]; refl @[simp] lemma to_nnreal_pos {r : ℝ} : 0 < real.to_nnreal r ↔ 0 < r := by simp [real.to_nnreal, nnreal.coe_lt_coe.symm, lt_irrefl] @[simp] lemma to_nnreal_eq_zero {r : ℝ} : real.to_nnreal r = 0 ↔ r ≤ 0 := by simpa [-to_nnreal_pos] using (not_iff_not.2 (@to_nnreal_pos r)) lemma to_nnreal_of_nonpos {r : ℝ} : r ≤ 0 → real.to_nnreal r = 0 := to_nnreal_eq_zero.2 @[simp] lemma coe_to_nnreal' (r : ℝ) : (real.to_nnreal r : ℝ) = max r 0 := rfl @[simp] lemma to_nnreal_le_to_nnreal_iff {r p : ℝ} (hp : 0 ≤ p) : real.to_nnreal r ≤ real.to_nnreal p ↔ r ≤ p := by simp [nnreal.coe_le_coe.symm, real.to_nnreal, hp] @[simp] lemma to_nnreal_lt_to_nnreal_iff' {r p : ℝ} : real.to_nnreal r < real.to_nnreal p ↔ r < p ∧ 0 < p := nnreal.coe_lt_coe.symm.trans max_lt_max_left_iff lemma to_nnreal_lt_to_nnreal_iff {r p : ℝ} (h : 0 < p) : real.to_nnreal r < real.to_nnreal p ↔ r < p := to_nnreal_lt_to_nnreal_iff'.trans (and_iff_left h) lemma to_nnreal_lt_to_nnreal_iff_of_nonneg {r p : ℝ} (hr : 0 ≤ r) : real.to_nnreal r < real.to_nnreal p ↔ r < p := to_nnreal_lt_to_nnreal_iff'.trans ⟨and.left, λ h, ⟨h, lt_of_le_of_lt hr h⟩⟩ @[simp] lemma to_nnreal_add {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : real.to_nnreal (r + p) = real.to_nnreal r + real.to_nnreal p := nnreal.eq $ by simp [real.to_nnreal, hr, hp, add_nonneg] lemma to_nnreal_add_to_nnreal {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : real.to_nnreal r + real.to_nnreal p = real.to_nnreal (r + p) := (real.to_nnreal_add hr hp).symm lemma to_nnreal_le_to_nnreal {r p : ℝ} (h : r ≤ p) : real.to_nnreal r ≤ real.to_nnreal p := real.to_nnreal_mono h lemma to_nnreal_add_le {r p : ℝ} : real.to_nnreal (r + p) ≤ real.to_nnreal r + real.to_nnreal p := nnreal.coe_le_coe.1 $ max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) nnreal.zero_le_coe lemma to_nnreal_le_iff_le_coe {r : ℝ} {p : ℝ≥0} : real.to_nnreal r ≤ p ↔ r ≤ ↑p := nnreal.gi.gc r p lemma le_to_nnreal_iff_coe_le {r : ℝ≥0} {p : ℝ} (hp : 0 ≤ p) : r ≤ real.to_nnreal p ↔ ↑r ≤ p := by rw [← nnreal.coe_le_coe, real.coe_to_nnreal p hp] lemma le_to_nnreal_iff_coe_le' {r : ℝ≥0} {p : ℝ} (hr : 0 < r) : r ≤ real.to_nnreal p ↔ ↑r ≤ p := (le_or_lt 0 p).elim le_to_nnreal_iff_coe_le $ λ hp, by simp only [(hp.trans_le r.coe_nonneg).not_le, to_nnreal_eq_zero.2 hp.le, hr.not_le] lemma to_nnreal_lt_iff_lt_coe {r : ℝ} {p : ℝ≥0} (ha : 0 ≤ r) : real.to_nnreal r < p ↔ r < ↑p := by rw [← nnreal.coe_lt_coe, real.coe_to_nnreal r ha] lemma lt_to_nnreal_iff_coe_lt {r : ℝ≥0} {p : ℝ} : r < real.to_nnreal p ↔ ↑r < p := begin cases le_total 0 p, { rw [← nnreal.coe_lt_coe, real.coe_to_nnreal p h] }, { rw [to_nnreal_eq_zero.2 h], split, { intro, have := not_lt_of_le (zero_le r), contradiction }, { intro rp, have : ¬(p ≤ 0) := not_le_of_lt (lt_of_le_of_lt (nnreal.coe_nonneg _) rp), contradiction } } end @[simp] lemma to_nnreal_bit0 (r : ℝ) : real.to_nnreal (bit0 r) = bit0 (real.to_nnreal r) := begin cases le_total r 0 with hr hr, { rw [to_nnreal_of_nonpos hr, to_nnreal_of_nonpos, bit0_zero], exact add_nonpos hr hr }, { exact to_nnreal_add hr hr } end @[simp] lemma to_nnreal_bit1 {r : ℝ} (hr : 0 ≤ r) : real.to_nnreal (bit1 r) = bit1 (real.to_nnreal r) := (real.to_nnreal_add (by simp [hr]) zero_le_one).trans (by simp [bit1]) end to_nnreal end real open real namespace nnreal section mul lemma mul_eq_mul_left {a b c : ℝ≥0} (h : a ≠ 0) : (a * b = a * c ↔ b = c) := begin rw [← nnreal.eq_iff, ← nnreal.eq_iff, nnreal.coe_mul, nnreal.coe_mul], split, { exact mul_left_cancel₀ (mt (@nnreal.eq_iff a 0).1 h) }, { assume h, rw [h] } end lemma _root_.real.to_nnreal_mul {p q : ℝ} (hp : 0 ≤ p) : real.to_nnreal (p * q) = real.to_nnreal p * real.to_nnreal q := begin cases le_total 0 q with hq hq, { apply nnreal.eq, simp [real.to_nnreal, hp, hq, max_eq_left, mul_nonneg] }, { have hpq := mul_nonpos_of_nonneg_of_nonpos hp hq, rw [to_nnreal_eq_zero.2 hq, to_nnreal_eq_zero.2 hpq, mul_zero] } end end mul section pow lemma pow_antitone_exp {a : ℝ≥0} (m n : ℕ) (mn : m ≤ n) (a1 : a ≤ 1) : a ^ n ≤ a ^ m := pow_le_pow_of_le_one (zero_le a) a1 mn lemma exists_pow_lt_of_lt_one {a b : ℝ≥0} (ha : 0 < a) (hb : b < 1) : ∃ n : ℕ, b ^ n < a := by simpa only [← coe_pow, nnreal.coe_lt_coe] using exists_pow_lt_of_lt_one (nnreal.coe_pos.2 ha) (nnreal.coe_lt_coe.2 hb) lemma exists_mem_Ico_zpow {x : ℝ≥0} {y : ℝ≥0} (hx : x ≠ 0) (hy : 1 < y) : ∃ n : ℤ, x ∈ set.Ico (y ^ n) (y ^ (n + 1)) := begin obtain ⟨n, hn, h'n⟩ : ∃ n : ℤ, (y : ℝ) ^ n ≤ x ∧ (x : ℝ) < y ^ (n + 1) := exists_mem_Ico_zpow (bot_lt_iff_ne_bot.mpr hx) hy, rw ← nnreal.coe_zpow at hn h'n, exact ⟨n, hn, h'n⟩, end lemma exists_mem_Ioc_zpow {x : ℝ≥0} {y : ℝ≥0} (hx : x ≠ 0) (hy : 1 < y) : ∃ n : ℤ, x ∈ set.Ioc (y ^ n) (y ^ (n + 1)) := begin obtain ⟨n, hn, h'n⟩ : ∃ n : ℤ, (y : ℝ) ^ n < x ∧ (x : ℝ) ≤ y ^ (n + 1) := exists_mem_Ioc_zpow (bot_lt_iff_ne_bot.mpr hx) hy, rw ← nnreal.coe_zpow at hn h'n, exact ⟨n, hn, h'n⟩, end end pow section sub /-! ### Lemmas about subtraction In this section we provide a few lemmas about subtraction that do not fit well into any other typeclass. For lemmas about subtraction and addition see lemmas about `has_ordered_sub` in the file `algebra.order.sub`. See also `mul_tsub` and `tsub_mul`. -/ lemma sub_def {r p : ℝ≥0} : r - p = real.to_nnreal (r - p) := rfl lemma coe_sub_def {r p : ℝ≥0} : ↑(r - p) = max (r - p : ℝ) 0 := rfl noncomputable example : has_ordered_sub ℝ≥0 := by apply_instance lemma sub_div (a b c : ℝ≥0) : (a - b) / c = a / c - b / c := by simp only [div_eq_mul_inv, tsub_mul] end sub section inv lemma sum_div {ι} (s : finset ι) (f : ι → ℝ≥0) (b : ℝ≥0) : (∑ i in s, f i) / b = ∑ i in s, (f i / b) := by simp only [div_eq_mul_inv, finset.sum_mul] @[simp] lemma inv_pos {r : ℝ≥0} : 0 < r⁻¹ ↔ 0 < r := by simp [pos_iff_ne_zero] lemma div_pos {r p : ℝ≥0} (hr : 0 < r) (hp : 0 < p) : 0 < r / p := by simpa only [div_eq_mul_inv] using mul_pos hr (inv_pos.2 hp) lemma div_self_le (r : ℝ≥0) : r / r ≤ 1 := div_self_le_one (r : ℝ) @[simp] lemma inv_le {r p : ℝ≥0} (h : r ≠ 0) : r⁻¹ ≤ p ↔ 1 ≤ r * p := by rw [← mul_le_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h] lemma inv_le_of_le_mul {r p : ℝ≥0} (h : 1 ≤ r * p) : r⁻¹ ≤ p := by by_cases r = 0; simp [*, inv_le] @[simp] lemma le_inv_iff_mul_le {r p : ℝ≥0} (h : p ≠ 0) : (r ≤ p⁻¹ ↔ r * p ≤ 1) := by rw [← mul_le_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] @[simp] lemma lt_inv_iff_mul_lt {r p : ℝ≥0} (h : p ≠ 0) : (r < p⁻¹ ↔ r * p < 1) := by rw [← mul_lt_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] lemma mul_le_iff_le_inv {a b r : ℝ≥0} (hr : r ≠ 0) : r * a ≤ b ↔ a ≤ r⁻¹ * b := have 0 < r, from lt_of_le_of_ne (zero_le r) hr.symm, by rw [← @mul_le_mul_left _ _ a _ r this, ← mul_assoc, mul_inv_cancel hr, one_mul] lemma le_div_iff_mul_le {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ a * r ≤ b := by rw [div_eq_inv_mul, ← mul_le_iff_le_inv hr, mul_comm] lemma div_le_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a / r ≤ b ↔ a ≤ b * r := @div_le_iff ℝ _ a r b $ pos_iff_ne_zero.2 hr lemma div_le_iff' {a b r : ℝ≥0} (hr : r ≠ 0) : a / r ≤ b ↔ a ≤ r * b := @div_le_iff' ℝ _ a r b $ pos_iff_ne_zero.2 hr lemma div_le_of_le_mul {a b c : ℝ≥0} (h : a ≤ b * c) : a / c ≤ b := if h0 : c = 0 then by simp [h0] else (div_le_iff h0).2 h lemma div_le_of_le_mul' {a b c : ℝ≥0} (h : a ≤ b * c) : a / b ≤ c := div_le_of_le_mul $ mul_comm b c ▸ h lemma le_div_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ a * r ≤ b := @le_div_iff ℝ _ a b r $ pos_iff_ne_zero.2 hr lemma le_div_iff' {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ r * a ≤ b := @le_div_iff' ℝ _ a b r $ pos_iff_ne_zero.2 hr lemma div_lt_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a / r < b ↔ a < b * r := lt_iff_lt_of_le_iff_le (le_div_iff hr) lemma div_lt_iff' {a b r : ℝ≥0} (hr : r ≠ 0) : a / r < b ↔ a < r * b := lt_iff_lt_of_le_iff_le (le_div_iff' hr) lemma lt_div_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a < b / r ↔ a * r < b := lt_iff_lt_of_le_iff_le (div_le_iff hr) lemma lt_div_iff' {a b r : ℝ≥0} (hr : r ≠ 0) : a < b / r ↔ r * a < b := lt_iff_lt_of_le_iff_le (div_le_iff' hr) lemma mul_lt_of_lt_div {a b r : ℝ≥0} (h : a < b / r) : a * r < b := begin refine (lt_div_iff $ λ hr, false.elim _).1 h, subst r, simpa using h end lemma div_le_div_left_of_le {a b c : ℝ≥0} (b0 : 0 < b) (c0 : 0 < c) (cb : c ≤ b) : a / b ≤ a / c := begin by_cases a0 : a = 0, { rw [a0, zero_div, zero_div] }, { cases a with a ha, replace a0 : 0 < a := lt_of_le_of_ne ha (ne_of_lt (zero_lt_iff.mpr a0)), exact (div_le_div_left a0 b0 c0).mpr cb } end lemma div_le_div_left {a b c : ℝ≥0} (a0 : 0 < a) (b0 : 0 < b) (c0 : 0 < c) : a / b ≤ a / c ↔ c ≤ b := by rw [nnreal.div_le_iff b0.ne.symm, div_mul_eq_mul_div, nnreal.le_div_iff_mul_le c0.ne.symm, mul_le_mul_left a0] lemma le_of_forall_lt_one_mul_le {x y : ℝ≥0} (h : ∀a<1, a * x ≤ y) : x ≤ y := le_of_forall_ge_of_dense $ assume a ha, have hx : x ≠ 0 := pos_iff_ne_zero.1 (lt_of_le_of_lt (zero_le _) ha), have hx' : x⁻¹ ≠ 0, by rwa [(≠), inv_eq_zero], have a * x⁻¹ < 1, by rwa [← lt_inv_iff_mul_lt hx', inv_inv], have (a * x⁻¹) * x ≤ y, from h _ this, by rwa [mul_assoc, inv_mul_cancel hx, mul_one] at this lemma div_add_div_same (a b c : ℝ≥0) : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma half_pos {a : ℝ≥0} (h : 0 < a) : 0 < a / 2 := div_pos h zero_lt_two lemma add_halves (a : ℝ≥0) : a / 2 + a / 2 = a := nnreal.eq (add_halves a) lemma half_le_self (a : ℝ≥0) : a / 2 ≤ a := nnreal.coe_le_coe.mp $ half_le_self a.coe_nonneg lemma half_lt_self {a : ℝ≥0} (h : a ≠ 0) : a / 2 < a := by rw [← nnreal.coe_lt_coe, nnreal.coe_div]; exact half_lt_self (bot_lt_iff_ne_bot.2 h) lemma two_inv_lt_one : (2⁻¹:ℝ≥0) < 1 := by simpa using half_lt_self zero_ne_one.symm lemma div_lt_one_of_lt {a b : ℝ≥0} (h : a < b) : a / b < 1 := begin rwa [div_lt_iff, one_mul], exact ne_of_gt (lt_of_le_of_lt (zero_le _) h) end @[field_simps] lemma div_add_div (a : ℝ≥0) {b : ℝ≥0} (c : ℝ≥0) {d : ℝ≥0} (hb : b ≠ 0) (hd : d ≠ 0) : a / b + c / d = (a * d + b * c) / (b * d) := begin rw ← nnreal.eq_iff, simp only [nnreal.coe_add, nnreal.coe_div, nnreal.coe_mul], exact div_add_div _ _ (coe_ne_zero.2 hb) (coe_ne_zero.2 hd) end @[field_simps] lemma add_div' (a b c : ℝ≥0) (hc : c ≠ 0) : b + a / c = (b * c + a) / c := by simpa using div_add_div b a one_ne_zero hc @[field_simps] lemma div_add' (a b c : ℝ≥0) (hc : c ≠ 0) : a / c + b = (a + b * c) / c := by rwa [add_comm, add_div', add_comm] lemma _root_.real.to_nnreal_inv {x : ℝ} : real.to_nnreal x⁻¹ = (real.to_nnreal x)⁻¹ := begin by_cases hx : 0 ≤ x, { nth_rewrite 0 ← real.coe_to_nnreal x hx, rw [←nnreal.coe_inv, real.to_nnreal_coe], }, { have hx' := le_of_not_ge hx, rw [to_nnreal_eq_zero.mpr hx', inv_zero, to_nnreal_eq_zero.mpr (inv_nonpos.mpr hx')], }, end lemma _root_.real.to_nnreal_div {x y : ℝ} (hx : 0 ≤ x) : real.to_nnreal (x / y) = real.to_nnreal x / real.to_nnreal y := by rw [div_eq_mul_inv, div_eq_mul_inv, ← real.to_nnreal_inv, ← real.to_nnreal_mul hx] lemma _root_.real.to_nnreal_div' {x y : ℝ} (hy : 0 ≤ y) : real.to_nnreal (x / y) = real.to_nnreal x / real.to_nnreal y := by rw [div_eq_inv_mul, div_eq_inv_mul, real.to_nnreal_mul (inv_nonneg.2 hy), real.to_nnreal_inv] lemma inv_lt_one_iff {x : ℝ≥0} (hx : x ≠ 0) : x⁻¹ < 1 ↔ 1 < x := by rwa [← one_div, div_lt_iff hx, one_mul] lemma inv_lt_one {x : ℝ≥0} (hx : 1 < x) : x⁻¹ < 1 := (inv_lt_one_iff (zero_lt_one.trans hx).ne').2 hx lemma zpow_pos {x : ℝ≥0} (hx : x ≠ 0) (n : ℤ) : 0 < x ^ n := begin cases n, { simp [pow_pos hx.bot_lt _] }, { simp [pow_pos hx.bot_lt _] } end lemma inv_lt_inv_iff {x y : ℝ≥0} (hx : x ≠ 0) (hy : y ≠ 0) : y⁻¹ < x⁻¹ ↔ x < y := by rw [← one_div, div_lt_iff hy, ← div_eq_inv_mul, lt_div_iff hx, one_mul] lemma inv_lt_inv {x y : ℝ≥0} (hx : x ≠ 0) (h : x < y) : y⁻¹ < x⁻¹ := (inv_lt_inv_iff hx ((bot_le.trans_lt h).ne')).2 h end inv @[simp] lemma abs_eq (x : ℝ≥0) : |(x : ℝ)| = x := abs_of_nonneg x.property section csupr open set variables {ι : Sort*} {f : ι → ℝ≥0} lemma le_to_nnreal_of_coe_le {x : ℝ≥0} {y : ℝ} (h : ↑x ≤ y) : x ≤ y.to_nnreal := (le_to_nnreal_iff_coe_le $ x.2.trans h).2 h lemma Sup_of_not_bdd_above {s : set ℝ≥0} (hs : ¬bdd_above s) : has_Sup.Sup s = 0 := begin rw [← bdd_above_coe] at hs, rw [← nnreal.coe_eq, coe_Sup], exact Sup_of_not_bdd_above hs, end lemma supr_of_not_bdd_above (hf : ¬ bdd_above (range f)) : (⨆ i, f i) = 0 := Sup_of_not_bdd_above hf lemma infi_empty [is_empty ι] (f : ι → ℝ≥0) : (⨅ i, f i) = 0 := by { rw [← nnreal.coe_eq, coe_infi], exact real.cinfi_empty _, } @[simp] lemma infi_const_zero {α : Sort*} : (⨅ i : α, (0 : ℝ≥0)) = 0 := by { rw [← nnreal.coe_eq, coe_infi], exact real.cinfi_const_zero, } lemma infi_mul (f : ι → ℝ≥0) (a : ℝ≥0) : infi f * a = ⨅ i, f i * a := begin rw [← nnreal.coe_eq, nnreal.coe_mul, coe_infi, coe_infi], exact real.infi_mul_of_nonneg (nnreal.coe_nonneg _) _, end lemma mul_infi (f : ι → ℝ≥0) (a : ℝ≥0) : a * infi f = ⨅ i, a * f i := by simpa only [mul_comm] using infi_mul f a lemma mul_supr (f : ι → ℝ≥0) (a : ℝ≥0) : a * (⨆ i, f i) = ⨆ i, a * f i := begin rw [← nnreal.coe_eq, nnreal.coe_mul, nnreal.coe_supr, nnreal.coe_supr], exact real.mul_supr_of_nonneg (nnreal.coe_nonneg _) _, end lemma supr_mul (f : ι → ℝ≥0) (a : ℝ≥0) : (⨆ i, f i) * a = ⨆ i, f i * a := by { rw [mul_comm, mul_supr], simp_rw [mul_comm] } lemma supr_div (f : ι → ℝ≥0) (a : ℝ≥0) : (⨆ i, f i) / a = ⨆ i, f i / a := by simp only [div_eq_mul_inv, supr_mul] variable [nonempty ι] lemma le_mul_infi {a : ℝ≥0} {g : ℝ≥0} {h : ι → ℝ≥0} (H : ∀ j, a ≤ g * h j) : a ≤ g * infi h := by { rw [mul_infi], exact le_cinfi H } lemma mul_supr_le {a : ℝ≥0} {g : ℝ≥0} {h : ι → ℝ≥0} (H : ∀ j, g * h j ≤ a) : g * supr h ≤ a := by { rw [mul_supr], exact csupr_le H } lemma le_infi_mul {a : ℝ≥0} {g : ι → ℝ≥0} {h : ℝ≥0} (H : ∀ i, a ≤ g i * h) : a ≤ infi g * h := by { rw infi_mul, exact le_cinfi H } lemma supr_mul_le {a : ℝ≥0} {g : ι → ℝ≥0} {h : ℝ≥0} (H : ∀ i, g i * h ≤ a) : supr g * h ≤ a := by { rw supr_mul, exact csupr_le H } lemma le_infi_mul_infi {a : ℝ≥0} {g h : ι → ℝ≥0} (H : ∀ i j, a ≤ g i * h j) : a ≤ infi g * infi h := le_infi_mul $ λ i, le_mul_infi $ H i lemma supr_mul_supr_le {a : ℝ≥0} {g h : ι → ℝ≥0} (H : ∀ i j, g i * h j ≤ a) : supr g * supr h ≤ a := supr_mul_le $ λ i, mul_supr_le $ H _ end csupr end nnreal namespace real /-- The absolute value on `ℝ` as a map to `ℝ≥0`. -/ @[pp_nodot] noncomputable def nnabs : ℝ →*₀ ℝ≥0 := { to_fun := λ x, ⟨|x|, abs_nonneg x⟩, map_zero' := by { ext, simp }, map_one' := by { ext, simp }, map_mul' := λ x y, by { ext, simp [abs_mul] } } @[norm_cast, simp] lemma coe_nnabs (x : ℝ) : (nnabs x : ℝ) = |x| := rfl @[simp] lemma nnabs_of_nonneg {x : ℝ} (h : 0 ≤ x) : nnabs x = to_nnreal x := by { ext, simp [coe_to_nnreal x h, abs_of_nonneg h] } lemma coe_to_nnreal_le (x : ℝ) : (to_nnreal x : ℝ) ≤ |x| := max_le (le_abs_self _) (abs_nonneg _) lemma cast_nat_abs_eq_nnabs_cast (n : ℤ) : (n.nat_abs : ℝ≥0) = nnabs n := by { ext, rw [nnreal.coe_nat_cast, int.cast_nat_abs, real.coe_nnabs] } end real
814c27148a8b199bbd26a860937f151207c3356a
8cb37a089cdb4af3af9d8bf1002b417e407a8e9e
/tests/lean/run/has_sizeof_indices.lean
dd73833b03418161bbb3048bb3cf96b8e5578627
[ "Apache-2.0" ]
permissive
kbuzzard/lean
ae3c3db4bb462d750dbf7419b28bafb3ec983ef7
ed1788fd674bb8991acffc8fca585ec746711928
refs/heads/master
1,620,983,366,617
1,618,937,600,000
1,618,937,600,000
359,886,396
1
0
Apache-2.0
1,618,936,987,000
1,618,936,987,000
null
UTF-8
Lean
false
false
460
lean
universes u inductive foo : nat → Type | baz (n : nat) : foo n → foo (nat.succ n) lemma foo.size (α β : Type u) (n a : ℕ) : has_sizeof (foo a) := by tactic.mk_has_sizeof_instance inductive bla : nat → bool → Type | mk : bla 0 ff | baz (n : nat) : bla n ff → bla (nat.succ n) tt | boo (n : nat) : bla n tt → bla (nat.succ n) ff lemma bla.size (α β : Type u) (a : ℕ) (t : bool) : has_sizeof (bla a t) := by tactic.mk_has_sizeof_instance
8e9e4b1ba11ca07f01ee36aad36c7e59ad88a836
d642a6b1261b2cbe691e53561ac777b924751b63
/src/topology/uniform_space/uniform_embedding.lean
e87f24e53f4934ba79d59d34c289170c33d3247e
[ "Apache-2.0" ]
permissive
cipher1024/mathlib
fee56b9954e969721715e45fea8bcb95f9dc03fe
d077887141000fefa5a264e30fa57520e9f03522
refs/heads/master
1,651,806,490,504
1,573,508,694,000
1,573,508,694,000
107,216,176
0
0
Apache-2.0
1,647,363,136,000
1,508,213,014,000
Lean
UTF-8
Lean
false
false
19,039
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, Sébastien Gouëzel, Patrick Massot Uniform embeddings of uniform spaces. Extension of uniform continuous functions. -/ import topology.uniform_space.cauchy topology.uniform_space.separation import topology.dense_embedding open filter topological_space lattice set classical open_locale classical open_locale uniformity section variables {α : Type*} {β : Type*} {γ : Type*} [uniform_space α] [uniform_space β] [uniform_space γ] universe u structure uniform_inducing (f : α → β) : Prop := (comap_uniformity : comap (λx:α×α, (f x.1, f x.2)) (𝓤 β) = 𝓤 α) lemma uniform_inducing.mk' {f : α → β} (h : ∀ s, s ∈ 𝓤 α ↔ ∃ t ∈ 𝓤 β, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s) : uniform_inducing f := ⟨by simp [eq_comm, filter.ext_iff, subset_def, h]⟩ lemma uniform_inducing.comp {g : β → γ} (hg : uniform_inducing g) {f : α → β} (hf : uniform_inducing f) : uniform_inducing (g ∘ f) := ⟨ by rw [show (λ (x : α × α), ((g ∘ f) x.1, (g ∘ f) x.2)) = (λ y : β × β, (g y.1, g y.2)) ∘ (λ x : α × α, (f x.1, f x.2)), by ext ; simp, ← filter.comap_comap_comp, hg.1, hf.1]⟩ structure uniform_embedding (f : α → β) extends uniform_inducing f : Prop := (inj : function.injective f) lemma uniform_embedding.comp {g : β → γ} (hg : uniform_embedding g) {f : α → β} (hf : uniform_embedding f) : uniform_embedding (g ∘ f) := { inj := function.injective_comp hg.inj hf.inj, ..hg.to_uniform_inducing.comp hf.to_uniform_inducing } theorem uniform_embedding_def {f : α → β} : uniform_embedding f ↔ function.injective f ∧ ∀ s, s ∈ 𝓤 α ↔ ∃ t ∈ 𝓤 β, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s := begin split, { rintro ⟨⟨h⟩, h'⟩, rw [eq_comm, filter.ext_iff] at h, simp [*, subset_def] }, { rintro ⟨h, h'⟩, refine uniform_embedding.mk ⟨_⟩ h, rw [eq_comm, filter.ext_iff], simp [*, subset_def] } end theorem uniform_embedding_def' {f : α → β} : uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧ ∀ s, s ∈ 𝓤 α → ∃ t ∈ 𝓤 β, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s := by simp [uniform_embedding_def, uniform_continuous_def]; exact ⟨λ ⟨I, H⟩, ⟨I, λ s su, (H _).2 ⟨s, su, λ x y, id⟩, λ s, (H s).1⟩, λ ⟨I, H₁, H₂⟩, ⟨I, λ s, ⟨H₂ s, λ ⟨t, tu, h⟩, sets_of_superset _ (H₁ t tu) (λ ⟨a, b⟩, h a b)⟩⟩⟩ lemma uniform_inducing.uniform_continuous {f : α → β} (hf : uniform_inducing f) : uniform_continuous f := by simp [uniform_continuous, hf.comap_uniformity.symm, tendsto_comap] lemma uniform_inducing.uniform_continuous_iff {f : α → β} {g : β → γ} (hg : uniform_inducing g) : uniform_continuous f ↔ uniform_continuous (g ∘ f) := by simp [uniform_continuous, tendsto]; rw [← hg.comap_uniformity, ← map_le_iff_le_comap, filter.map_map] lemma uniform_inducing.inducing {f : α → β} (h : uniform_inducing f) : inducing f := begin refine ⟨eq_of_nhds_eq_nhds $ assume a, _ ⟩, rw [nhds_induced, nhds_eq_uniformity, nhds_eq_uniformity, ← h.comap_uniformity, comap_lift'_eq, comap_lift'_eq2]; { refl <|> exact monotone_preimage } end lemma uniform_inducing.prod {α' : Type*} {β' : Type*} [uniform_space α'] [uniform_space β'] {e₁ : α → α'} {e₂ : β → β'} (h₁ : uniform_inducing e₁) (h₂ : uniform_inducing e₂) : uniform_inducing (λp:α×β, (e₁ p.1, e₂ p.2)) := ⟨by simp [(∘), uniformity_prod, h₁.comap_uniformity.symm, h₂.comap_uniformity.symm, comap_inf, comap_comap_comp]⟩ lemma uniform_inducing.dense_inducing {f : α → β} (h : uniform_inducing f) (hd : dense_range f) : dense_inducing f := { dense := hd, induced := h.inducing.induced } lemma uniform_embedding.embedding {f : α → β} (h : uniform_embedding f) : embedding f := { induced := h.to_uniform_inducing.inducing.induced, inj := h.inj } lemma uniform_embedding.dense_embedding {f : α → β} (h : uniform_embedding f) (hd : dense_range f) : dense_embedding f := { dense := hd, inj := h.inj, induced := h.embedding.induced } lemma closure_image_mem_nhds_of_uniform_inducing {s : set (α×α)} {e : α → β} (b : β) (he₁ : uniform_inducing e) (he₂ : dense_inducing e) (hs : s ∈ 𝓤 α) : ∃a, closure (e '' {a' | (a, a') ∈ s}) ∈ nhds b := have s ∈ comap (λp:α×α, (e p.1, e p.2)) (𝓤 β), from he₁.comap_uniformity.symm ▸ hs, let ⟨t₁, ht₁u, ht₁⟩ := this in have ht₁ : ∀p:α×α, (e p.1, e p.2) ∈ t₁ → p ∈ s, from ht₁, let ⟨t₂, ht₂u, ht₂s, ht₂c⟩ := comp_symm_of_uniformity ht₁u in let ⟨t, htu, hts, htc⟩ := comp_symm_of_uniformity ht₂u in have preimage e {b' | (b, b') ∈ t₂} ∈ comap e (nhds b), from preimage_mem_comap $ mem_nhds_left b ht₂u, let ⟨a, (ha : (b, e a) ∈ t₂)⟩ := inhabited_of_mem_sets (he₂.comap_nhds_neq_bot) this in have ∀b' (s' : set (β × β)), (b, b') ∈ t → s' ∈ 𝓤 β → {y : β | (b', y) ∈ s'} ∩ e '' {a' : α | (a, a') ∈ s} ≠ ∅, from assume b' s' hb' hs', have preimage e {b'' | (b', b'') ∈ s' ∩ t} ∈ comap e (nhds b'), from preimage_mem_comap $ mem_nhds_left b' $ inter_mem_sets hs' htu, let ⟨a₂, ha₂s', ha₂t⟩ := inhabited_of_mem_sets (he₂.comap_nhds_neq_bot) this in have (e a, e a₂) ∈ t₁, from ht₂c $ prod_mk_mem_comp_rel (ht₂s ha) $ htc $ prod_mk_mem_comp_rel hb' ha₂t, have e a₂ ∈ {b'':β | (b', b'') ∈ s'} ∩ e '' {a' | (a, a') ∈ s}, from ⟨ha₂s', mem_image_of_mem _ $ ht₁ (a, a₂) this⟩, ne_empty_of_mem this, have ∀b', (b, b') ∈ t → nhds b' ⊓ principal (e '' {a' | (a, a') ∈ s}) ≠ ⊥, begin intros b' hb', rw [nhds_eq_uniformity, lift'_inf_principal_eq, lift'_neq_bot_iff], exact assume s, this b' s hb', exact monotone_inter monotone_preimage monotone_const end, have ∀b', (b, b') ∈ t → b' ∈ closure (e '' {a' | (a, a') ∈ s}), from assume b' hb', by rw [closure_eq_nhds]; exact this b' hb', ⟨a, (nhds b).sets_of_superset (mem_nhds_left b htu) this⟩ lemma uniform_embedding_subtype_emb (p : α → Prop) {e : α → β} (ue : uniform_embedding e) (de : dense_embedding e) : uniform_embedding (dense_embedding.subtype_emb p e) := { comap_uniformity := by simp [comap_comap_comp, (∘), dense_embedding.subtype_emb, uniformity_subtype, ue.comap_uniformity.symm], inj := (de.subtype p).inj } lemma uniform_embedding.prod {α' : Type*} {β' : Type*} [uniform_space α'] [uniform_space β'] {e₁ : α → α'} {e₂ : β → β'} (h₁ : uniform_embedding e₁) (h₂ : uniform_embedding e₂) : uniform_embedding (λp:α×β, (e₁ p.1, e₂ p.2)) := { inj := function.injective_prod h₁.inj h₂.inj, ..h₁.to_uniform_inducing.prod h₂.to_uniform_inducing } /-- A set is complete iff its image under a uniform embedding is complete. -/ lemma is_complete_image_iff {m : α → β} {s : set α} (hm : uniform_embedding m) : is_complete (m '' s) ↔ is_complete s := begin refine ⟨λ c f hf fs, _, λ c f hf fs, _⟩, { let f' := map m f, have cf' : cauchy f' := cauchy_map hm.to_uniform_inducing.uniform_continuous hf, have f's : f' ≤ principal (m '' s), { simp only [filter.le_principal_iff, set.mem_image, filter.mem_map], exact mem_sets_of_superset (filter.le_principal_iff.1 fs) (λx hx, ⟨x, hx, rfl⟩) }, rcases c f' cf' f's with ⟨y, yms, hy⟩, rcases mem_image_iff_bex.1 yms with ⟨x, xs, rfl⟩, rw [map_le_iff_le_comap, ← nhds_induced, ← (uniform_embedding.embedding hm).induced] at hy, exact ⟨x, xs, hy⟩ }, { rw filter.le_principal_iff at fs, let f' := comap m f, have cf' : cauchy f', { have : comap m f ≠ ⊥, { refine comap_neq_bot (λt ht, _), have A : t ∩ m '' s ∈ f := filter.inter_mem_sets ht fs, have : t ∩ m '' s ≠ ∅, { by_contradiction h, simp only [not_not, ne.def] at h, simpa [h, empty_in_sets_eq_bot, hf.1] using A }, rcases ne_empty_iff_exists_mem.1 this with ⟨x, ⟨xt, xms⟩⟩, rcases mem_image_iff_bex.1 xms with ⟨y, ys, yx⟩, rw ← yx at xt, exact ⟨y, xt⟩ }, apply cauchy_comap _ hf this, simp only [hm.comap_uniformity, le_refl] }, have : f' ≤ principal s := by simp [f']; exact ⟨m '' s, by simpa using fs, by simp [preimage_image_eq s hm.inj]⟩, rcases c f' cf' this with ⟨x, xs, hx⟩, existsi [m x, mem_image_of_mem m xs], rw [(uniform_embedding.embedding hm).induced, nhds_induced] at hx, calc f = map m f' : (map_comap $ filter.mem_sets_of_superset fs $ image_subset_range _ _).symm ... ≤ map m (comap m (nhds (m x))) : map_mono hx ... ≤ nhds (m x) : map_comap_le } end lemma complete_space_extension {m : β → α} (hm : uniform_inducing m) (dense : dense_range m) (h : ∀f:filter β, cauchy f → ∃x:α, map m f ≤ nhds x) : complete_space α := ⟨assume (f : filter α), assume hf : cauchy f, let p : set (α × α) → set α → set α := λs t, {y : α| ∃x:α, x ∈ t ∧ (x, y) ∈ s}, g := (𝓤 α).lift (λs, f.lift' (p s)) in have mp₀ : monotone p, from assume a b h t s ⟨x, xs, xa⟩, ⟨x, xs, h xa⟩, have mp₁ : ∀{s}, monotone (p s), from assume s a b h x ⟨y, ya, yxs⟩, ⟨y, h ya, yxs⟩, have f ≤ g, from le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht, le_principal_iff.mpr $ mem_sets_of_superset ht $ assume x hx, ⟨x, hx, refl_mem_uniformity hs⟩, have g ≠ ⊥, from neq_bot_of_le_neq_bot hf.left this, have comap m g ≠ ⊥, from comap_neq_bot $ assume t ht, let ⟨t', ht', ht_mem⟩ := (mem_lift_sets $ monotone_lift' monotone_const mp₀).mp ht in let ⟨t'', ht'', ht'_sub⟩ := (mem_lift'_sets mp₁).mp ht_mem in let ⟨x, (hx : x ∈ t'')⟩ := inhabited_of_mem_sets hf.left ht'' in have h₀ : nhds x ⊓ principal (range m) ≠ ⊥, by simpa [dense_range, closure_eq_nhds] using dense x, have h₁ : {y | (x, y) ∈ t'} ∈ nhds x ⊓ principal (range m), from @mem_inf_sets_of_left α (nhds x) (principal (range m)) _ $ mem_nhds_left x ht', have h₂ : range m ∈ nhds x ⊓ principal (range m), from @mem_inf_sets_of_right α (nhds x) (principal (range m)) _ $ subset.refl _, have {y | (x, y) ∈ t'} ∩ range m ∈ nhds x ⊓ principal (range m), from @inter_mem_sets α (nhds x ⊓ principal (range m)) _ _ h₁ h₂, let ⟨y, xyt', b, b_eq⟩ := inhabited_of_mem_sets h₀ this in ⟨b, b_eq.symm ▸ ht'_sub ⟨x, hx, xyt'⟩⟩, have cauchy g, from ⟨‹g ≠ ⊥›, assume s hs, let ⟨s₁, hs₁, (comp_s₁ : comp_rel s₁ s₁ ⊆ s)⟩ := comp_mem_uniformity_sets hs, ⟨s₂, hs₂, (comp_s₂ : comp_rel s₂ s₂ ⊆ s₁)⟩ := comp_mem_uniformity_sets hs₁, ⟨t, ht, (prod_t : set.prod t t ⊆ s₂)⟩ := mem_prod_same_iff.mp (hf.right hs₂) in have hg₁ : p (preimage prod.swap s₁) t ∈ g, from mem_lift (symm_le_uniformity hs₁) $ @mem_lift' α α f _ t ht, have hg₂ : p s₂ t ∈ g, from mem_lift hs₂ $ @mem_lift' α α f _ t ht, have hg : set.prod (p (preimage prod.swap s₁) t) (p s₂ t) ∈ filter.prod g g, from @prod_mem_prod α α _ _ g g hg₁ hg₂, (filter.prod g g).sets_of_superset hg (assume ⟨a, b⟩ ⟨⟨c₁, c₁t, hc₁⟩, ⟨c₂, c₂t, hc₂⟩⟩, have (c₁, c₂) ∈ set.prod t t, from ⟨c₁t, c₂t⟩, comp_s₁ $ prod_mk_mem_comp_rel hc₁ $ comp_s₂ $ prod_mk_mem_comp_rel (prod_t this) hc₂)⟩, have cauchy (filter.comap m g), from cauchy_comap (le_of_eq hm.comap_uniformity) ‹cauchy g› (by assumption), let ⟨x, (hx : map m (filter.comap m g) ≤ nhds x)⟩ := h _ this in have map m (filter.comap m g) ⊓ nhds x ≠ ⊥, from (le_nhds_iff_adhp_of_cauchy (cauchy_map hm.uniform_continuous this)).mp hx, have g ⊓ nhds x ≠ ⊥, from neq_bot_of_le_neq_bot this (inf_le_inf (assume s hs, ⟨s, hs, subset.refl _⟩) (le_refl _)), ⟨x, calc f ≤ g : by assumption ... ≤ nhds x : le_nhds_of_cauchy_adhp ‹cauchy g› this⟩⟩ lemma totally_bounded_preimage {f : α → β} {s : set β} (hf : uniform_embedding f) (hs : totally_bounded s) : totally_bounded (f ⁻¹' s) := λ t ht, begin rw ← hf.comap_uniformity at ht, rcases mem_comap_sets.2 ht with ⟨t', ht', ts⟩, rcases totally_bounded_iff_subset.1 (totally_bounded_subset (image_preimage_subset f s) hs) _ ht' with ⟨c, cs, hfc, hct⟩, refine ⟨f ⁻¹' c, finite_preimage (inj_on_of_injective _ hf.inj) hfc, λ x h, _⟩, have := hct (mem_image_of_mem f h), simp at this ⊢, rcases this with ⟨z, zc, zt⟩, rcases cs zc with ⟨y, yc, rfl⟩, exact ⟨y, zc, ts (by exact zt)⟩ end end lemma uniform_embedding_comap {α : Type*} {β : Type*} {f : α → β} [u : uniform_space β] (hf : function.injective f) : @uniform_embedding α β (uniform_space.comap f u) u f := @uniform_embedding.mk _ _ (uniform_space.comap f u) _ _ (@uniform_inducing.mk _ _ (uniform_space.comap f u) _ _ rfl) hf section uniform_extension variables {α : Type*} {β : Type*} {γ : Type*} [uniform_space α] [uniform_space β] [uniform_space γ] {e : β → α} (h_e : uniform_inducing e) (h_dense : dense_range e) {f : β → γ} (h_f : uniform_continuous f) local notation `ψ` := (h_e.dense_inducing h_dense).extend f lemma uniformly_extend_exists [complete_space γ] (a : α) : ∃c, tendsto f (comap e (nhds a)) (nhds c) := let de := (h_e.dense_inducing h_dense) in have cauchy (nhds a), from cauchy_nhds, have cauchy (comap e (nhds a)), from cauchy_comap (le_of_eq h_e.comap_uniformity) this de.comap_nhds_neq_bot, have cauchy (map f (comap e (nhds a))), from cauchy_map h_f this, complete_space.complete this lemma uniform_extend_subtype [complete_space γ] {p : α → Prop} {e : α → β} {f : α → γ} {b : β} {s : set α} (hf : uniform_continuous (λx:subtype p, f x.val)) (he : uniform_embedding e) (hd : ∀x:β, x ∈ closure (range e)) (hb : closure (e '' s) ∈ nhds b) (hs : is_closed s) (hp : ∀x∈s, p x) : ∃c, tendsto f (comap e (nhds b)) (nhds c) := have de : dense_embedding e, from he.dense_embedding hd, have de' : dense_embedding (dense_embedding.subtype_emb p e), by exact de.subtype p, have ue' : uniform_embedding (dense_embedding.subtype_emb p e), from uniform_embedding_subtype_emb _ he de, have b ∈ closure (e '' {x | p x}), from (closure_mono $ mono_image $ hp) (mem_of_nhds hb), let ⟨c, (hc : tendsto (f ∘ subtype.val) (comap (dense_embedding.subtype_emb p e) (nhds ⟨b, this⟩)) (nhds c))⟩ := uniformly_extend_exists ue'.to_uniform_inducing de'.dense hf _ in begin rw [nhds_subtype_eq_comap] at hc, simp [comap_comap_comp] at hc, change (tendsto (f ∘ @subtype.val α p) (comap (e ∘ @subtype.val α p) (nhds b)) (nhds c)) at hc, rw [←comap_comap_comp, tendsto_comap'_iff] at hc, exact ⟨c, hc⟩, exact ⟨_, hb, assume x, begin change e x ∈ (closure (e '' s)) → x ∈ range subtype.val, rw [←closure_induced, closure_eq_nhds, mem_set_of_eq, (≠), nhds_induced, ← de.to_dense_inducing.nhds_eq_comap], change x ∈ {x | nhds x ⊓ principal s ≠ ⊥} → x ∈ range subtype.val, rw [←closure_eq_nhds, closure_eq_of_is_closed hs], exact assume hxs, ⟨⟨x, hp x hxs⟩, rfl⟩, exact de.inj end⟩ end variables [separated γ] lemma uniformly_extend_of_ind (b : β) : ψ (e b) = f b := dense_inducing.extend_e_eq _ b (continuous_iff_continuous_at.1 h_f.continuous b) include h_f lemma uniformly_extend_spec [complete_space γ] (a : α) : tendsto f (comap e (nhds a)) (nhds (ψ a)) := let de := (h_e.dense_inducing h_dense) in begin by_cases ha : a ∈ range e, { rcases ha with ⟨b, rfl⟩, rw [uniformly_extend_of_ind _ _ h_f, ← de.nhds_eq_comap], exact h_f.continuous.tendsto _ }, { simp only [dense_inducing.extend, dif_neg ha], exact (@lim_spec _ _ (id _) _ $ uniformly_extend_exists h_e h_dense h_f _) } end lemma uniform_continuous_uniformly_extend [cγ : complete_space γ] : uniform_continuous ψ := assume d hd, let ⟨s, hs, hs_comp⟩ := (mem_lift'_sets $ monotone_comp_rel monotone_id $ monotone_comp_rel monotone_id monotone_id).mp (comp_le_uniformity3 hd) in have h_pnt : ∀{a m}, m ∈ nhds a → ∃c, c ∈ f '' preimage e m ∧ (c, ψ a) ∈ s ∧ (ψ a, c) ∈ s, from assume a m hm, have nb : map f (comap e (nhds a)) ≠ ⊥, from map_ne_bot (h_e.dense_inducing h_dense).comap_nhds_neq_bot, have (f '' preimage e m) ∩ ({c | (c, ψ a) ∈ s } ∩ {c | (ψ a, c) ∈ s }) ∈ map f (comap e (nhds a)), from inter_mem_sets (image_mem_map $ preimage_mem_comap $ hm) (uniformly_extend_spec h_e h_dense h_f _ (inter_mem_sets (mem_nhds_right _ hs) (mem_nhds_left _ hs))), inhabited_of_mem_sets nb this, have preimage (λp:β×β, (f p.1, f p.2)) s ∈ 𝓤 β, from h_f hs, have preimage (λp:β×β, (f p.1, f p.2)) s ∈ comap (λx:β×β, (e x.1, e x.2)) (𝓤 α), by rwa [h_e.comap_uniformity.symm] at this, let ⟨t, ht, ts⟩ := this in show preimage (λp:(α×α), (ψ p.1, ψ p.2)) d ∈ 𝓤 α, from (𝓤 α).sets_of_superset (interior_mem_uniformity ht) $ assume ⟨x₁, x₂⟩ hx_t, have nhds (x₁, x₂) ≤ principal (interior t), from is_open_iff_nhds.mp is_open_interior (x₁, x₂) hx_t, have interior t ∈ filter.prod (nhds x₁) (nhds x₂), by rwa [nhds_prod_eq, le_principal_iff] at this, let ⟨m₁, hm₁, m₂, hm₂, (hm : set.prod m₁ m₂ ⊆ interior t)⟩ := mem_prod_iff.mp this in let ⟨a, ha₁, _, ha₂⟩ := h_pnt hm₁ in let ⟨b, hb₁, hb₂, _⟩ := h_pnt hm₂ in have set.prod (preimage e m₁) (preimage e m₂) ⊆ preimage (λp:(β×β), (f p.1, f p.2)) s, from calc _ ⊆ preimage (λp:(β×β), (e p.1, e p.2)) (interior t) : preimage_mono hm ... ⊆ preimage (λp:(β×β), (e p.1, e p.2)) t : preimage_mono interior_subset ... ⊆ preimage (λp:(β×β), (f p.1, f p.2)) s : ts, have set.prod (f '' preimage e m₁) (f '' preimage e m₂) ⊆ s, from calc set.prod (f '' preimage e m₁) (f '' preimage e m₂) = (λp:(β×β), (f p.1, f p.2)) '' (set.prod (preimage e m₁) (preimage e m₂)) : prod_image_image_eq ... ⊆ (λp:(β×β), (f p.1, f p.2)) '' preimage (λp:(β×β), (f p.1, f p.2)) s : mono_image this ... ⊆ s : image_subset_iff.mpr $ subset.refl _, have (a, b) ∈ s, from @this (a, b) ⟨ha₁, hb₁⟩, hs_comp $ show (ψ x₁, ψ x₂) ∈ comp_rel s (comp_rel s s), from ⟨a, ha₂, ⟨b, this, hb₂⟩⟩ end uniform_extension
fa1f26ebd20ee93b8fe0dd23b554432704f7002a
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/homology/homological_complex.lean
58afd16d775a31fe29d6503f27287d8d462370b9
[ "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
31,658
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Scott Morrison -/ import algebra.homology.complex_shape import category_theory.subobject.limits import category_theory.graded_object /-! # Homological complexes. A `homological_complex V c` with a "shape" controlled by `c : complex_shape ι` has chain groups `X i` (objects in `V`) indexed by `i : ι`, and a differential `d i j` whenever `c.rel i j`. We in fact ask for differentials `d i j` for all `i j : ι`, but have a field `shape'` requiring that these are zero when not allowed by `c`. This avoids a lot of dependent type theory hell! The composite of any two differentials `d i j ≫ d j k` must be zero. We provide `chain_complex V α` for `α`-indexed chain complexes in which `d i j ≠ 0` only if `j + 1 = i`, and similarly `cochain_complex V α`, with `i = j + 1`. There is a category structure, where morphisms are chain maps. For `C : homological_complex V c`, we define `C.X_next i`, which is either `C.X j` for some arbitrarily chosen `j` such that `c.r i j`, or the zero object if there is no such `j`. Similarly we have `C.X_prev j`. Defined in terms of these we have `C.d_from i : C.X i ⟶ C.X_next i` and `C.d_to j : C.X_prev j ⟶ C.X j`, which are either defined as `C.d i j`, or zero, as needed. -/ universes v u open category_theory category_theory.limits variables {ι : Type*} variables (V : Type u) [category.{v} V] [has_zero_morphisms V] /-- A `homological_complex V c` with a "shape" controlled by `c : complex_shape ι` has chain groups `X i` (objects in `V`) indexed by `i : ι`, and a differential `d i j` whenever `c.rel i j`. We in fact ask for differentials `d i j` for all `i j : ι`, but have a field `shape'` requiring that these are zero when not allowed by `c`. This avoids a lot of dependent type theory hell! The composite of any two differentials `d i j ≫ d j k` must be zero. -/ structure homological_complex (c : complex_shape ι) := (X : ι → V) (d : Π i j, X i ⟶ X j) (shape' : ∀ i j, ¬ c.rel i j → d i j = 0 . obviously) (d_comp_d' : ∀ i j k, c.rel i j → c.rel j k → d i j ≫ d j k = 0 . obviously) namespace homological_complex restate_axiom shape' attribute [simp] shape variables {V} {c : complex_shape ι} @[simp, reassoc] lemma d_comp_d (C : homological_complex V c) (i j k : ι) : C.d i j ≫ C.d j k = 0 := begin by_cases hij : c.rel i j, { by_cases hjk : c.rel j k, { exact C.d_comp_d' i j k hij hjk }, { rw [C.shape j k hjk, comp_zero] } }, { rw [C.shape i j hij, zero_comp] } end end homological_complex /-- An `α`-indexed chain complex is a `homological_complex` in which `d i j ≠ 0` only if `j + 1 = i`. -/ abbreviation chain_complex (α : Type*) [add_right_cancel_semigroup α] [has_one α] : Type* := homological_complex V (complex_shape.down α) /-- An `α`-indexed cochain complex is a `homological_complex` in which `d i j ≠ 0` only if `i + 1 = j`. -/ abbreviation cochain_complex (α : Type*) [add_right_cancel_semigroup α] [has_one α] : Type* := homological_complex V (complex_shape.up α) namespace chain_complex @[simp] lemma prev (α : Type*) [add_right_cancel_semigroup α] [has_one α] (i : α) : (complex_shape.down α).prev i = some ⟨i+1, rfl⟩ := option.choice_eq _ @[simp] lemma next (α : Type*) [add_group α] [has_one α] (i : α) : (complex_shape.down α).next i = some ⟨i-1, sub_add_cancel i 1⟩ := option.choice_eq _ @[simp] lemma next_nat_zero : (complex_shape.down ℕ).next 0 = none := @option.choice_eq_none _ ⟨by rintro ⟨j, ⟨⟩⟩⟩ @[simp] lemma next_nat_succ (i : ℕ) : (complex_shape.down ℕ).next (i+1) = some ⟨i, rfl⟩ := option.choice_eq _ end chain_complex namespace cochain_complex @[simp] lemma prev (α : Type*) [add_group α] [has_one α] (i : α) : (complex_shape.up α).prev i = some ⟨i-1, sub_add_cancel i 1⟩ := option.choice_eq _ @[simp] lemma next (α : Type*) [add_right_cancel_semigroup α] [has_one α] (i : α) : (complex_shape.up α).next i = some ⟨i+1, rfl⟩ := option.choice_eq _ @[simp] lemma prev_nat_zero : (complex_shape.up ℕ).prev 0 = none := @option.choice_eq_none _ ⟨by rintro ⟨j, ⟨⟩⟩⟩ @[simp] lemma prev_nat_succ (i : ℕ) : (complex_shape.up ℕ).prev (i+1) = some ⟨i, rfl⟩ := option.choice_eq _ end cochain_complex namespace homological_complex variables {V} {c : complex_shape ι} (C : homological_complex V c) /-- A morphism of homological complexes consists of maps between the chain groups, commuting with the differentials. -/ @[ext] structure hom (A B : homological_complex V c) := (f : ∀ i, A.X i ⟶ B.X i) (comm' : ∀ i j, c.rel i j → f i ≫ B.d i j = A.d i j ≫ f j . obviously) @[simp, reassoc] lemma hom.comm {A B : homological_complex V c} (f : A.hom B) (i j : ι) : f.f i ≫ B.d i j = A.d i j ≫ f.f j := begin by_cases hij : c.rel i j, { exact f.comm' i j hij }, rw [A.shape i j hij, B.shape i j hij, comp_zero, zero_comp], end instance (A B : homological_complex V c) : inhabited (hom A B) := ⟨{ f := λ i, 0 }⟩ /-- Identity chain map. -/ def id (A : homological_complex V c) : hom A A := { f := λ _, 𝟙 _ } /-- Composition of chain maps. -/ def comp (A B C : homological_complex V c) (φ : hom A B) (ψ : hom B C) : hom A C := { f := λ i, φ.f i ≫ ψ.f i } section local attribute [simp] id comp instance : category (homological_complex V c) := { hom := hom, id := id, comp := comp } end @[simp] lemma id_f (C : homological_complex V c) (i : ι) : hom.f (𝟙 C) i = 𝟙 (C.X i) := rfl @[simp] lemma comp_f {C₁ C₂ C₃ : homological_complex V c} (f : C₁ ⟶ C₂) (g : C₂ ⟶ C₃) (i : ι) : (f ≫ g).f i = f.f i ≫ g.f i := rfl -- We'll use this later to show that `homological_complex V c` is preadditive when `V` is. lemma hom_f_injective {C₁ C₂ : homological_complex V c} : function.injective (λ f : hom C₁ C₂, f.f) := by tidy instance : has_zero_morphisms (homological_complex V c) := { has_zero := λ C D, ⟨{ f := λ i, 0 }⟩ } @[simp] lemma zero_apply (C D : homological_complex V c) (i : ι) : (0 : C ⟶ D).f i = 0 := rfl open_locale zero_object instance [has_zero_object V] : has_zero_object (homological_complex V c) := { zero := { X := λ i, 0, d := λ i j, 0 }, unique_from := λ C, ⟨⟨0⟩, λ f, by ext⟩, unique_to := λ C, ⟨⟨0⟩, λ f, by ext⟩ } instance [has_zero_object V] : inhabited (homological_complex V c) := ⟨0⟩ lemma congr_hom {C D : homological_complex V c} {f g : C ⟶ D} (w : f = g) (i : ι) : f.f i = g.f i := congr_fun (congr_arg hom.f w) i section variables (V c) /-- The functor picking out the `i`-th object of a complex. -/ @[simps] def eval (i : ι) : homological_complex V c ⥤ V := { obj := λ C, C.X i, map := λ C D f, f.f i, } /-- The functor forgetting the differential in a complex, obtaining a graded object. -/ @[simps] def forget : homological_complex V c ⥤ graded_object ι V := { obj := λ C, C.X, map := λ _ _ f, f.f } /-- Forgetting the differentials than picking out the `i`-th object is the same as just picking out the `i`-th object. -/ @[simps] def forget_eval (i : ι) : forget V c ⋙ graded_object.eval i ≅ eval V c i := nat_iso.of_components (λ X, iso.refl _) (by tidy) end open_locale classical noncomputable theory /-- If `C.d i j` and `C.d i j'` are both allowed, then we must have `j = j'`, and so the differentials only differ by an `eq_to_hom`. -/ @[simp] lemma d_comp_eq_to_hom {i j j' : ι} (rij : c.rel i j) (rij' : c.rel i j') : C.d i j' ≫ eq_to_hom (congr_arg C.X (c.next_eq rij' rij)) = C.d i j := begin have P : ∀ h : j' = j, C.d i j' ≫ eq_to_hom (congr_arg C.X h) = C.d i j, { rintro rfl, simp }, apply P, end /-- If `C.d i j` and `C.d i' j` are both allowed, then we must have `i = i'`, and so the differentials only differ by an `eq_to_hom`. -/ @[simp] lemma eq_to_hom_comp_d {i i' j : ι} (rij : c.rel i j) (rij' : c.rel i' j) : eq_to_hom (congr_arg C.X (c.prev_eq rij rij')) ≫ C.d i' j = C.d i j := begin have P : ∀ h : i = i', eq_to_hom (congr_arg C.X h) ≫ C.d i' j = C.d i j, { rintro rfl, simp }, apply P, end lemma kernel_eq_kernel [has_kernels V] {i j j' : ι} (r : c.rel i j) (r' : c.rel i j') : kernel_subobject (C.d i j) = kernel_subobject (C.d i j') := begin rw ←d_comp_eq_to_hom C r r', apply kernel_subobject_comp_mono, end lemma image_eq_image [has_images V] [has_equalizers V] {i i' j : ι} (r : c.rel i j) (r' : c.rel i' j) : image_subobject (C.d i j) = image_subobject (C.d i' j) := begin rw ←eq_to_hom_comp_d C r r', apply image_subobject_iso_comp, end section variables [has_zero_object V] open_locale zero_object /-- Either `C.X i`, if there is some `i` with `c.rel i j`, or the zero object. -/ def X_prev (j : ι) : V := match c.prev j with | none := 0 | (some ⟨i,_⟩) := C.X i end /-- If `c.rel i j`, then `C.X_prev j` is isomorphic to `C.X i`. -/ def X_prev_iso {i j : ι} (r : c.rel i j) : C.X_prev j ≅ C.X i := eq_to_iso begin dsimp [X_prev], rw c.prev_eq_some r, refl, end /-- If there is no `i` so `c.rel i j`, then `C.X_prev j` is isomorphic to `0`. -/ def X_prev_iso_zero {j : ι} (h : c.prev j = none) : C.X_prev j ≅ 0 := eq_to_iso begin dsimp [X_prev], rw h, refl, end /-- Either `C.X j`, if there is some `j` with `c.rel i j`, or the zero object. -/ def X_next (i : ι) : V := match c.next i with | none := 0 | (some ⟨j,_⟩) := C.X j end /-- If `c.rel i j`, then `C.X_next i` is isomorphic to `C.X j`. -/ def X_next_iso {i j : ι} (r : c.rel i j) : C.X_next i ≅ C.X j := eq_to_iso begin dsimp [X_next], rw c.next_eq_some r, refl, end /-- If there is no `j` so `c.rel i j`, then `C.X_next i` is isomorphic to `0`. -/ def X_next_iso_zero {i : ι} (h : c.next i = none) : C.X_next i ≅ 0 := eq_to_iso begin dsimp [X_next], rw h, refl, end /-- The differential mapping into `C.X j`, or zero if there isn't one. -/ def d_to (j : ι) : C.X_prev j ⟶ C.X j := match c.prev j with | none := (0 : C.X_prev j ⟶ C.X j) | (some ⟨i, w⟩) := (C.X_prev_iso w).hom ≫ C.d i j end /-- The differential mapping out of `C.X i`, or zero if there isn't one. -/ def d_from (i : ι) : C.X i ⟶ C.X_next i := match c.next i with | none := (0 : C.X i ⟶ C.X_next i) | (some ⟨j, w⟩) := C.d i j ≫ (C.X_next_iso w).inv end lemma d_to_eq {i j : ι} (r : c.rel i j) : C.d_to j = (C.X_prev_iso r).hom ≫ C.d i j := begin dsimp [d_to, X_prev_iso], rw c.prev_eq_some r, refl, end @[simp] lemma d_to_eq_zero {j : ι} (h : c.prev j = none) : C.d_to j = 0 := begin dsimp [d_to], rw h, refl, end lemma d_from_eq {i j : ι} (r : c.rel i j) : C.d_from i = C.d i j ≫ (C.X_next_iso r).inv := begin dsimp [d_from, X_next_iso], rw c.next_eq_some r, refl, end @[simp] lemma d_from_eq_zero {i : ι} (h : c.next i = none) : C.d_from i = 0 := begin dsimp [d_from], rw h, refl, end @[simp, reassoc] lemma X_prev_iso_comp_d_to {i j : ι} (r : c.rel i j) : (C.X_prev_iso r).inv ≫ C.d_to j = C.d i j := by simp [C.d_to_eq r] @[simp, reassoc] lemma X_prev_iso_zero_comp_d_to {j : ι} (h : c.prev j = none) : (C.X_prev_iso_zero h).inv ≫ C.d_to j = 0 := by simp [h] @[simp, reassoc] lemma d_from_comp_X_next_iso {i j : ι} (r : c.rel i j) : C.d_from i ≫ (C.X_next_iso r).hom = C.d i j := by simp [C.d_from_eq r] @[simp, reassoc] lemma d_from_comp_X_next_iso_zero {i : ι} (h : c.next i = none) : C.d_from i ≫ (C.X_next_iso_zero h).hom = 0 := by simp [h] @[simp] lemma d_to_comp_d_from (j : ι) : C.d_to j ≫ C.d_from j = 0 := begin rcases h₁ : c.next j with _ | ⟨k,w₁⟩, { rw [d_from_eq_zero _ h₁], simp }, { rw [d_from_eq _ w₁], rcases h₂ : c.prev j with _ | ⟨i,w₂⟩, { rw [d_to_eq_zero _ h₂], simp }, { rw [d_to_eq _ w₂], simp } } end lemma kernel_from_eq_kernel [has_kernels V] {i j : ι} (r : c.rel i j) : kernel_subobject (C.d_from i) = kernel_subobject (C.d i j) := begin rw C.d_from_eq r, apply kernel_subobject_comp_mono, end lemma image_to_eq_image [has_images V] [has_equalizers V] {i j : ι} (r : c.rel i j) : image_subobject (C.d_to j) = image_subobject (C.d i j) := begin rw C.d_to_eq r, apply image_subobject_iso_comp, end end namespace hom variables {C₁ C₂ C₃ : homological_complex V c} /-- The `i`-th component of an isomorphism of chain complexes. -/ @[simps] def iso_app (f : C₁ ≅ C₂) (i : ι) : C₁.X i ≅ C₂.X i := (eval V c i).map_iso f /-- Construct an isomorphism of chain complexes from isomorphism of the objects which commute with the differentials. -/ @[simps] def iso_of_components (f : Π i, C₁.X i ≅ C₂.X i) (hf : ∀ i j, c.rel i j → (f i).hom ≫ C₂.d i j = C₁.d i j ≫ (f j).hom) : C₁ ≅ C₂ := { hom := { f := λ i, (f i).hom, comm' := hf }, inv := { f := λ i, (f i).inv, comm' := λ i j hij, calc (f i).inv ≫ C₁.d i j = (f i).inv ≫ (C₁.d i j ≫ (f j).hom) ≫ (f j).inv : by simp ... = (f i).inv ≫ ((f i).hom ≫ C₂.d i j) ≫ (f j).inv : by rw hf i j hij ... = C₂.d i j ≫ (f j).inv : by simp }, hom_inv_id' := by { ext i, exact (f i).hom_inv_id }, inv_hom_id' := by { ext i, exact (f i).inv_hom_id } } @[simp] lemma iso_of_components_app (f : Π i, C₁.X i ≅ C₂.X i) (hf : ∀ i j, c.rel i j → (f i).hom ≫ C₂.d i j = C₁.d i j ≫ (f j).hom) (i : ι) : iso_app (iso_of_components f hf) i = f i := by { ext, simp, } variables [has_zero_object V] open_locale zero_object /-! Lemmas relating chain maps and `d_to`/`d_from`. -/ /-- `f.prev j` is `f.f i` if there is some `r i j`, and zero otherwise. -/ def prev (f : hom C₁ C₂) (j : ι) : C₁.X_prev j ⟶ C₂.X_prev j := match c.prev j with | none := 0 | some ⟨i,w⟩ := (C₁.X_prev_iso w).hom ≫ f.f i ≫ (C₂.X_prev_iso w).inv end lemma prev_eq (f : hom C₁ C₂) {i j : ι} (w : c.rel i j) : f.prev j = (C₁.X_prev_iso w).hom ≫ f.f i ≫ (C₂.X_prev_iso w).inv := begin dsimp [prev], rw c.prev_eq_some w, refl, end /-- `f.next i` is `f.f j` if there is some `r i j`, and zero otherwise. -/ def next (f : hom C₁ C₂) (i : ι) : C₁.X_next i ⟶ C₂.X_next i := match c.next i with | none := 0 | some ⟨j,w⟩ := (C₁.X_next_iso w).hom ≫ f.f j ≫ (C₂.X_next_iso w).inv end lemma next_eq (f : hom C₁ C₂) {i j : ι} (w : c.rel i j) : f.next i = (C₁.X_next_iso w).hom ≫ f.f j ≫ (C₂.X_next_iso w).inv := begin dsimp [next], rw c.next_eq_some w, refl, end @[simp, reassoc] lemma comm_from (f : hom C₁ C₂) (i : ι) : f.f i ≫ C₂.d_from i = C₁.d_from i ≫ f.next i := begin rcases h : c.next i with _ | ⟨j,w⟩, { simp [h] }, { simp [d_from_eq _ w, next_eq _ w] } end @[simp, reassoc] lemma comm_to (f : hom C₁ C₂) (j : ι) : f.prev j ≫ C₂.d_to j = C₁.d_to j ≫ f.f j := begin rcases h : c.prev j with _ | ⟨j,w⟩, { simp [h] }, { simp [d_to_eq _ w, prev_eq _ w] } end /-- A morphism of chain complexes induces a morphism of arrows of the differentials out of each object. -/ def sq_from (f : hom C₁ C₂) (i : ι) : arrow.mk (C₁.d_from i) ⟶ arrow.mk (C₂.d_from i) := arrow.hom_mk (f.comm_from i) @[simp] lemma sq_from_left (f : hom C₁ C₂) (i : ι) : (f.sq_from i).left = f.f i := rfl @[simp] lemma sq_from_right (f : hom C₁ C₂) (i : ι) : (f.sq_from i).right = f.next i := rfl @[simp] lemma sq_from_id (C₁ : homological_complex V c) (i : ι) : sq_from (𝟙 C₁) i = 𝟙 _ := begin rcases h : c.next i with _ | ⟨j,w⟩, { ext, { refl }, { dsimp, simp only [next, h], symmetry, apply zero_of_target_iso_zero, exact X_next_iso_zero _ h } }, { ext, refl, dsimp, simp [next, h] } end @[simp] lemma sq_from_comp (f : C₁ ⟶ C₂) (g : C₂ ⟶ C₃) (i : ι) : sq_from (f ≫ g) i = sq_from f i ≫ sq_from g i := begin rcases h : c.next i with _ | ⟨j,w⟩, { ext, { refl }, { dsimp, simp only [next, h], symmetry, apply zero_of_target_iso_zero, exact X_next_iso_zero _ h } }, { ext, refl, dsimp, simp [next, h] } end /-- A morphism of chain complexes induces a morphism of arrows of the differentials into each object. -/ def sq_to (f : hom C₁ C₂) (j : ι) : arrow.mk (C₁.d_to j) ⟶ arrow.mk (C₂.d_to j) := arrow.hom_mk (f.comm_to j) @[simp] lemma sq_to_left (f : hom C₁ C₂) (j : ι) : (f.sq_to j).left = f.prev j := rfl @[simp] lemma sq_to_right (f : hom C₁ C₂) (j : ι) : (f.sq_to j).right = f.f j := rfl end hom end homological_complex namespace chain_complex section of variables {V} {α : Type*} [add_right_cancel_semigroup α] [has_one α] [decidable_eq α] /-- Construct an `α`-indexed chain complex from a dependently-typed differential. -/ def of (X : α → V) (d : Π n, X (n+1) ⟶ X n) (sq : ∀ n, d (n+1) ≫ d n = 0) : chain_complex V α := { X := X, d := λ i j, if h : i = j + 1 then eq_to_hom (by subst h) ≫ d j else 0, shape' := λ i j w, by rw dif_neg (ne.symm w), d_comp_d' := λ i j k hij hjk, begin dsimp at hij hjk, substs hij hjk, simp only [category.id_comp, dif_pos rfl, eq_to_hom_refl], exact sq k, end } variables (X : α → V) (d : Π n, X (n+1) ⟶ X n) (sq : ∀ n, d (n+1) ≫ d n = 0) @[simp] lemma of_X (n : α) : (of X d sq).X n = X n := rfl @[simp] lemma of_d (j : α) : (of X d sq).d (j+1) j = d j := by { dsimp [of], rw [if_pos rfl, category.id_comp] } lemma of_d_ne {i j : α} (h : i ≠ j + 1) : (of X d sq).d i j = 0 := by { dsimp [of], rw [dif_neg h], } end of section of_hom variables {V} {α : Type*} [add_right_cancel_semigroup α] [has_one α] [decidable_eq α] variables (X : α → V) (d_X : Π n, X (n+1) ⟶ X n) (sq_X : ∀ n, d_X (n+1) ≫ d_X n = 0) (Y : α → V) (d_Y : Π n, Y (n+1) ⟶ Y n) (sq_Y : ∀ n, d_Y (n+1) ≫ d_Y n = 0) /-- A constructor for chain maps between `α`-indexed chain complexes built using `chain_complex.of`, from a dependently typed collection of morphisms. -/ @[simps] def of_hom (f : Π i : α, X i ⟶ Y i) (comm : ∀ i : α, f (i+1) ≫ d_Y i = d_X i ≫ f i) : of X d_X sq_X ⟶ of Y d_Y sq_Y := { f := f, comm' := λ n m, begin by_cases h : n = m + 1, { subst h, simpa using comm m, }, { rw [of_d_ne X _ _ h, of_d_ne Y _ _ h], simp } end } end of_hom section mk /-- Auxiliary structure for setting up the recursion in `mk`. This is purely an implementation detail: for some reason just using the dependent 6-tuple directly results in `mk_aux` taking much longer (well over the `-T100000` limit) to elaborate. -/ @[nolint has_inhabited_instance] structure mk_struct := (X₀ X₁ X₂ : V) (d₀ : X₁ ⟶ X₀) (d₁ : X₂ ⟶ X₁) (s : d₁ ≫ d₀ = 0) variables {V} /-- Flatten to a tuple. -/ def mk_struct.flat (t : mk_struct V) : Σ' (X₀ X₁ X₂ : V) (d₀ : X₁ ⟶ X₀) (d₁ : X₂ ⟶ X₁), d₁ ≫ d₀ = 0 := ⟨t.X₀, t.X₁, t.X₂, t.d₀, t.d₁, t.s⟩ variables (X₀ X₁ X₂ : V) (d₀ : X₁ ⟶ X₀) (d₁ : X₂ ⟶ X₁) (s : d₁ ≫ d₀ = 0) (succ : Π (t : Σ' (X₀ X₁ X₂ : V) (d₀ : X₁ ⟶ X₀) (d₁ : X₂ ⟶ X₁), d₁ ≫ d₀ = 0), Σ' (X₃ : V) (d₂ : X₃ ⟶ t.2.2.1), d₂ ≫ t.2.2.2.2.1 = 0) /-- Auxiliary definition for `mk`. -/ def mk_aux : Π n : ℕ, mk_struct V | 0 := ⟨X₀, X₁, X₂, d₀, d₁, s⟩ | (n+1) := let p := mk_aux n in ⟨p.X₁, p.X₂, (succ p.flat).1, p.d₁, (succ p.flat).2.1, (succ p.flat).2.2⟩ /-- A inductive constructor for `ℕ`-indexed chain complexes. You provide explicitly the first two differentials, then a function which takes two differentials and the fact they compose to zero, and returns the next object, its differential, and the fact it composes appropiately to zero. See also `mk'`, which only sees the previous differential in the inductive step. -/ def mk : chain_complex V ℕ := of (λ n, (mk_aux X₀ X₁ X₂ d₀ d₁ s succ n).X₀) (λ n, (mk_aux X₀ X₁ X₂ d₀ d₁ s succ n).d₀) (λ n, (mk_aux X₀ X₁ X₂ d₀ d₁ s succ n).s) @[simp] lemma mk_X_0 : (mk X₀ X₁ X₂ d₀ d₁ s succ).X 0 = X₀ := rfl @[simp] lemma mk_X_1 : (mk X₀ X₁ X₂ d₀ d₁ s succ).X 1 = X₁ := rfl @[simp] lemma mk_X_2 : (mk X₀ X₁ X₂ d₀ d₁ s succ).X 2 = X₂ := rfl @[simp] lemma mk_d_1_0 : (mk X₀ X₁ X₂ d₀ d₁ s succ).d 1 0 = d₀ := by { change ite (1 = 0 + 1) (𝟙 X₁ ≫ d₀) 0 = d₀, rw [if_pos rfl, category.id_comp] } @[simp] lemma mk_d_2_0 : (mk X₀ X₁ X₂ d₀ d₁ s succ).d 2 1 = d₁ := by { change ite (2 = 1 + 1) (𝟙 X₂ ≫ d₁) 0 = d₁, rw [if_pos rfl, category.id_comp] } -- TODO simp lemmas for the inductive steps? It's not entirely clear that they are needed. /-- A simpler inductive constructor for `ℕ`-indexed chain complexes. You provide explicitly the first differential, then a function which takes a differential, and returns the next object, its differential, and the fact it composes appropriately to zero. -/ def mk' (X₀ X₁ : V) (d : X₁ ⟶ X₀) (succ' : Π (t : Σ (X₀ X₁ : V), X₁ ⟶ X₀), Σ' (X₂ : V) (d : X₂ ⟶ t.2.1), d ≫ t.2.2 = 0) : chain_complex V ℕ := mk X₀ X₁ (succ' ⟨X₀, X₁, d⟩).1 d (succ' ⟨X₀, X₁, d⟩).2.1 (succ' ⟨X₀, X₁, d⟩).2.2 (λ t, succ' ⟨t.2.1, t.2.2.1, t.2.2.2.2.1⟩) variables (succ' : Π (t : Σ (X₀ X₁ : V), X₁ ⟶ X₀), Σ' (X₂ : V) (d : X₂ ⟶ t.2.1), d ≫ t.2.2 = 0) @[simp] lemma mk'_X_0 : (mk' X₀ X₁ d₀ succ').X 0 = X₀ := rfl @[simp] lemma mk'_X_1 : (mk' X₀ X₁ d₀ succ').X 1 = X₁ := rfl @[simp] lemma mk'_d_1_0 : (mk' X₀ X₁ d₀ succ').d 1 0 = d₀ := by { change ite (1 = 0 + 1) (𝟙 X₁ ≫ d₀) 0 = d₀, rw [if_pos rfl, category.id_comp] } -- TODO simp lemmas for the inductive steps? It's not entirely clear that they are needed. end mk section mk_hom variables {V} (P Q : chain_complex V ℕ) (zero : P.X 0 ⟶ Q.X 0) (one : P.X 1 ⟶ Q.X 1) (one_zero_comm : one ≫ Q.d 1 0 = P.d 1 0 ≫ zero) (succ : ∀ (n : ℕ) (p : Σ' (f : P.X n ⟶ Q.X n) (f' : P.X (n+1) ⟶ Q.X (n+1)), f' ≫ Q.d (n+1) n = P.d (n+1) n ≫ f), Σ' f'' : P.X (n+2) ⟶ Q.X (n+2), f'' ≫ Q.d (n+2) (n+1) = P.d (n+2) (n+1) ≫ p.2.1) /-- An auxiliary construction for `mk_hom`. Here we build by induction a family of commutative squares, but don't require at the type level that these successive commutative squares actually agree. They do in fact agree, and we then capture that at the type level (i.e. by constructing a chain map) in `mk_hom`. -/ def mk_hom_aux : Π n, Σ' (f : P.X n ⟶ Q.X n) (f' : P.X (n+1) ⟶ Q.X (n+1)), f' ≫ Q.d (n+1) n = P.d (n+1) n ≫ f | 0 := ⟨zero, one, one_zero_comm⟩ | (n+1) := ⟨(mk_hom_aux n).2.1, (succ n (mk_hom_aux n)).1, (succ n (mk_hom_aux n)).2⟩ /-- A constructor for chain maps between `ℕ`-indexed chain complexes, working by induction on commutative squares. You need to provide the components of the chain map in degrees 0 and 1, show that these form a commutative square, and then give a construction of each component, and the fact that it forms a commutative square with the previous component, using as an inductive hypothesis the data (and commutativity) of the previous two components. -/ def mk_hom : P ⟶ Q := { f := λ n, (mk_hom_aux P Q zero one one_zero_comm succ n).1, comm' := λ n m, begin rintro (rfl : m + 1 = n), exact (mk_hom_aux P Q zero one one_zero_comm succ m).2.2, end } @[simp] lemma mk_hom_f_0 : (mk_hom P Q zero one one_zero_comm succ).f 0 = zero := rfl @[simp] lemma mk_hom_f_1 : (mk_hom P Q zero one one_zero_comm succ).f 1 = one := rfl @[simp] lemma mk_hom_f_succ_succ (n : ℕ) : (mk_hom P Q zero one one_zero_comm succ).f (n+2) = (succ n ⟨(mk_hom P Q zero one one_zero_comm succ).f n, (mk_hom P Q zero one one_zero_comm succ).f (n+1), (mk_hom P Q zero one one_zero_comm succ).comm (n+1) n⟩).1 := begin dsimp [mk_hom, mk_hom_aux], induction n; congr, end end mk_hom end chain_complex namespace cochain_complex section of variables {V} {α : Type*} [add_right_cancel_semigroup α] [has_one α] [decidable_eq α] /-- Construct an `α`-indexed cochain complex from a dependently-typed differential. -/ def of (X : α → V) (d : Π n, X n ⟶ X (n+1)) (sq : ∀ n, d n ≫ d (n+1) = 0) : cochain_complex V α := { X := X, d := λ i j, if h : i + 1 = j then d _ ≫ eq_to_hom (by subst h) else 0, shape' := λ i j w, by {rw dif_neg, exact w}, d_comp_d' := λ i j k, begin split_ifs with h h' h', { substs h h', simp [sq] }, all_goals { simp }, end } variables (X : α → V) (d : Π n, X n ⟶ X (n+1)) (sq : ∀ n, d n ≫ d (n+1) = 0) @[simp] lemma of_X (n : α) : (of X d sq).X n = X n := rfl @[simp] lemma of_d (j : α) : (of X d sq).d j (j+1) = d j := by { dsimp [of], rw [if_pos rfl, category.comp_id] } lemma of_d_ne {i j : α} (h : i + 1 ≠ j) : (of X d sq).d i j = 0 := by { dsimp [of], rw [dif_neg h] } end of section of_hom variables {V} {α : Type*} [add_right_cancel_semigroup α] [has_one α] [decidable_eq α] variables (X : α → V) (d_X : Π n, X n ⟶ X (n+1)) (sq_X : ∀ n, d_X n ≫ d_X (n+1) = 0) (Y : α → V) (d_Y : Π n, Y n ⟶ Y (n+1)) (sq_Y : ∀ n, d_Y n ≫ d_Y (n+1) = 0) /-- A constructor for chain maps between `α`-indexed cochain complexes built using `cochain_complex.of`, from a dependently typed collection of morphisms. -/ @[simps] def of_hom (f : Π i : α, X i ⟶ Y i) (comm : ∀ i : α, f i ≫ d_Y i = d_X i ≫ f (i+1)) : of X d_X sq_X ⟶ of Y d_Y sq_Y := { f := f, comm' := λ n m, begin by_cases h : n + 1 = m, { subst h, simpa using comm n }, { rw [of_d_ne X _ _ h, of_d_ne Y _ _ h], simp } end } end of_hom section mk /-- Auxiliary structure for setting up the recursion in `mk`. This is purely an implementation detail: for some reason just using the dependent 6-tuple directly results in `mk_aux` taking much longer (well over the `-T100000` limit) to elaborate. -/ @[nolint has_inhabited_instance] structure mk_struct := (X₀ X₁ X₂ : V) (d₀ : X₀ ⟶ X₁) (d₁ : X₁ ⟶ X₂) (s : d₀ ≫ d₁ = 0) variables {V} /-- Flatten to a tuple. -/ def mk_struct.flat (t : mk_struct V) : Σ' (X₀ X₁ X₂ : V) (d₀ : X₀ ⟶ X₁) (d₁ : X₁ ⟶ X₂), d₀ ≫ d₁ = 0 := ⟨t.X₀, t.X₁, t.X₂, t.d₀, t.d₁, t.s⟩ variables (X₀ X₁ X₂ : V) (d₀ : X₀ ⟶ X₁) (d₁ : X₁ ⟶ X₂) (s : d₀ ≫ d₁ = 0) (succ : Π (t : Σ' (X₀ X₁ X₂ : V) (d₀ : X₀ ⟶ X₁) (d₁ : X₁ ⟶ X₂), d₀ ≫ d₁ = 0), Σ' (X₃ : V) (d₂ : t.2.2.1 ⟶ X₃), t.2.2.2.2.1 ≫ d₂ = 0) /-- Auxiliary definition for `mk`. -/ def mk_aux : Π n : ℕ, mk_struct V | 0 := ⟨X₀, X₁, X₂, d₀, d₁, s⟩ | (n+1) := let p := mk_aux n in ⟨p.X₁, p.X₂, (succ p.flat).1, p.d₁, (succ p.flat).2.1, (succ p.flat).2.2⟩ /-- A inductive constructor for `ℕ`-indexed cochain complexes. You provide explicitly the first two differentials, then a function which takes two differentials and the fact they compose to zero, and returns the next object, its differential, and the fact it composes appropiately to zero. See also `mk'`, which only sees the previous differential in the inductive step. -/ def mk : cochain_complex V ℕ := of (λ n, (mk_aux X₀ X₁ X₂ d₀ d₁ s succ n).X₀) (λ n, (mk_aux X₀ X₁ X₂ d₀ d₁ s succ n).d₀) (λ n, (mk_aux X₀ X₁ X₂ d₀ d₁ s succ n).s) @[simp] lemma mk_X_0 : (mk X₀ X₁ X₂ d₀ d₁ s succ).X 0 = X₀ := rfl @[simp] lemma mk_X_1 : (mk X₀ X₁ X₂ d₀ d₁ s succ).X 1 = X₁ := rfl @[simp] lemma mk_X_2 : (mk X₀ X₁ X₂ d₀ d₁ s succ).X 2 = X₂ := rfl @[simp] lemma mk_d_1_0 : (mk X₀ X₁ X₂ d₀ d₁ s succ).d 0 1 = d₀ := by { change ite (1 = 0 + 1) (d₀ ≫ 𝟙 X₁) 0 = d₀, rw [if_pos rfl, category.comp_id] } @[simp] lemma mk_d_2_0 : (mk X₀ X₁ X₂ d₀ d₁ s succ).d 1 2 = d₁ := by { change ite (2 = 1 + 1) (d₁ ≫ 𝟙 X₂) 0 = d₁, rw [if_pos rfl, category.comp_id] } -- TODO simp lemmas for the inductive steps? It's not entirely clear that they are needed. /-- A simpler inductive constructor for `ℕ`-indexed cochain complexes. You provide explicitly the first differential, then a function which takes a differential, and returns the next object, its differential, and the fact it composes appropriately to zero. -/ def mk' (X₀ X₁ : V) (d : X₀ ⟶ X₁) (succ' : Π (t : Σ (X₀ X₁ : V), X₀ ⟶ X₁), Σ' (X₂ : V) (d : t.2.1 ⟶ X₂), t.2.2 ≫ d = 0) : cochain_complex V ℕ := mk X₀ X₁ (succ' ⟨X₀, X₁, d⟩).1 d (succ' ⟨X₀, X₁, d⟩).2.1 (succ' ⟨X₀, X₁, d⟩).2.2 (λ t, succ' ⟨t.2.1, t.2.2.1, t.2.2.2.2.1⟩) variables (succ' : Π (t : Σ (X₀ X₁ : V), X₀ ⟶ X₁), Σ' (X₂ : V) (d : t.2.1 ⟶ X₂), t.2.2 ≫ d = 0) @[simp] lemma mk'_X_0 : (mk' X₀ X₁ d₀ succ').X 0 = X₀ := rfl @[simp] lemma mk'_X_1 : (mk' X₀ X₁ d₀ succ').X 1 = X₁ := rfl @[simp] lemma mk'_d_1_0 : (mk' X₀ X₁ d₀ succ').d 0 1 = d₀ := by { change ite (1 = 0 + 1) (d₀ ≫ 𝟙 X₁) 0 = d₀, rw [if_pos rfl, category.comp_id] } -- TODO simp lemmas for the inductive steps? It's not entirely clear that they are needed. end mk section mk_hom variables {V} (P Q : cochain_complex V ℕ) (zero : P.X 0 ⟶ Q.X 0) (one : P.X 1 ⟶ Q.X 1) (one_zero_comm : zero ≫ Q.d 0 1 = P.d 0 1 ≫ one) (succ : ∀ (n : ℕ) (p : Σ' (f : P.X n ⟶ Q.X n) (f' : P.X (n+1) ⟶ Q.X (n+1)), f ≫ Q.d n (n+1) = P.d n (n+1) ≫ f'), Σ' f'' : P.X (n+2) ⟶ Q.X (n+2), p.2.1 ≫ Q.d (n+1) (n+2) = P.d (n+1) (n+2) ≫ f'') /-- An auxiliary construction for `mk_hom`. Here we build by induction a family of commutative squares, but don't require at the type level that these successive commutative squares actually agree. They do in fact agree, and we then capture that at the type level (i.e. by constructing a chain map) in `mk_hom`. -/ def mk_hom_aux : Π n, Σ' (f : P.X n ⟶ Q.X n) (f' : P.X (n+1) ⟶ Q.X (n+1)), f ≫ Q.d n (n+1) = P.d n (n+1) ≫ f' | 0 := ⟨zero, one, one_zero_comm⟩ | (n+1) := ⟨(mk_hom_aux n).2.1, (succ n (mk_hom_aux n)).1, (succ n (mk_hom_aux n)).2⟩ /-- A constructor for chain maps between `ℕ`-indexed cochain complexes, working by induction on commutative squares. You need to provide the components of the chain map in degrees 0 and 1, show that these form a commutative square, and then give a construction of each component, and the fact that it forms a commutative square with the previous component, using as an inductive hypothesis the data (and commutativity) of the previous two components. -/ def mk_hom : P ⟶ Q := { f := λ n, (mk_hom_aux P Q zero one one_zero_comm succ n).1, comm' := λ n m, begin rintro (rfl : n + 1 = m), exact (mk_hom_aux P Q zero one one_zero_comm succ n).2.2, end } @[simp] lemma mk_hom_f_0 : (mk_hom P Q zero one one_zero_comm succ).f 0 = zero := rfl @[simp] lemma mk_hom_f_1 : (mk_hom P Q zero one one_zero_comm succ).f 1 = one := rfl @[simp] lemma mk_hom_f_succ_succ (n : ℕ) : (mk_hom P Q zero one one_zero_comm succ).f (n+2) = (succ n ⟨(mk_hom P Q zero one one_zero_comm succ).f n, (mk_hom P Q zero one one_zero_comm succ).f (n+1), (mk_hom P Q zero one one_zero_comm succ).comm n (n+1)⟩).1 := begin dsimp [mk_hom, mk_hom_aux], induction n; congr, end end mk_hom end cochain_complex
672d681aa95666ba24a4d63c00f625032bb7f689
88fb7558b0636ec6b181f2a548ac11ad3919f8a5
/tests/lean/run/begin_end_do.lean
c2b1f024acc900ead2378192d403f6a3b5f3c1d1
[ "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
265
lean
open tactic example (a b c : nat) (p : nat → Prop) (f : nat → nat → nat) : p (f (f a a) (f b c)) := begin do { [x, y, z] ← match_target_subexpr `(λ x y z, f x (f y z)) | failed, trace x, trace y, trace z, trace "------------"}, exact sorry end
f6e9f673d328e36d0bfd52eb4f1d9df315bd027d
78630e908e9624a892e24ebdd21260720d29cf55
/src/logic_propositional/prop_21.lean
4a328b5259ca7a4167537e0053008c7001596718
[ "CC0-1.0" ]
permissive
tomasz-lisowski/lean-logic-examples
84e612466776be0a16c23a0439ff8ef6114ddbe1
2b2ccd467b49c3989bf6c92ec0358a8d6ee68c5d
refs/heads/master
1,683,334,199,431
1,621,938,305,000
1,621,938,305,000
365,041,573
1
0
null
null
null
null
UTF-8
Lean
false
false
457
lean
namespace prop_21 /- An example of a proof without use of classical rules. -/ variable A : Prop theorem prop_21 : ¬ (A ↔ ¬ A) := assume h1: A ↔ ¬ A, have h2: A → (A → false), from h1.mp, have h3: (A → false) → A, from h1.mpr, have h4: A → false, from (assume h5: A, (h2 h5) h5), have h6: A, from h3 h4, show false, from h4 h6 /- No classical logic used so should print `no axioms`. -/ #print axioms prop_21 -- end namespace end prop_21
caeaa134b414efccf290ce9022d2f92aeed71d80
05e1338b54d08e2cb29dc6a094c10ecbedb7a6a9
/src/PR.lean
4688f44bfac235de979c9f5b3e7bc04517b8dec4
[]
no_license
ChrisHughes24/jordan_hoelder
91783fd9b34a424451a976bb683bd43c7e955a13
41ba0efabeded8655399420a0dfe107eb5acf664
refs/heads/master
1,690,778,999,662
1,632,495,314,000
1,632,495,314,000
398,859,868
0
0
null
null
null
null
UTF-8
Lean
false
false
44,166
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 order.lattice import data.fin import data.set_like.basic import data.list.sort import data.equiv.fin import data.equiv.functor /-! # Jordan Hölder Theorem This file proves the Jordan Hölder theorem for a `jordan_hoelder_lattice`, a class also defined in this file. Examples of `jordan_hoelder_lattice` include `subgroup G` if `G` is a group, and `submodule R M` if `M` is an `R`-module. Using this approach the theorem need not be proved seperately for both groups and modules, the proof in this file can be applied to both. ## Main definitions The main definitions in this file are `jordan_hoelder_lattice` and `composition_series`, and the relation `equivalent` on `composition_series` A `jordan_hoelder_lattice` is the class for which the Jordan Hölder theorem is proved. A Jordan Hölder class is a lattice equipped with a notion of maximality, `is_maximal`, and a notion of isoorphism of pairs `iso`. In the example of subgroups of a group, `is_maximal H K` means that `H` is a maximal normal subgroup of `K`, and `iso (H₁, K₁) (H₂, K₂)` means that the quotient `H₁ / K₁` is isomorphic to the quotient `H₂ / K₂`. `iso` be symmetric and transitive and must satisfy the second isomorphism theorem `iso (H, H ⊔ K) (H ⊓ K, K)`. A `composition_series X` is a finite nonempty series of elements of the lattice `X` such that each element is maximal inside the next. The length of a `composition_series X` is one less than the number of elements in the series. Note that there is no stipulation that a series start from the bottom of the lattice and finish at the top. For a composition series `s`, `s.top` is the largest element of the series, and `s.bot` is the least element. Two `composition_series X`, `s₁` and `s₂` are equivalent if there is a bijection `e : fin s₁.length ≃ fin s₂.length` such that for any `i`, `iso (s₁ i) (s₁ i.succ) (s₂ (e i), s₂ (e i.succ))` ## Main theorems The main theorem is `jordan_hoelder`, which says that if two composition series have the same least element and the same largest element, then they are `equivalent`. -/ universe u open set /-- A `jordan_hoelder_lattice` is the class for which the Jordan Hölder theorem is proved. A Jordan Hölder class is a lattice equipped with a notion of maximality, `is_maximal`, and a notion of isoorphism of pairs `iso`. In the example of subgroups of a group, `is_maximal H K` means that `H` is a maximal normal subgroup of `K`, and `iso (H₁, K₁) (H₂, K₂)` means that the quotient `H₁ / K₁` is isomorphic to the quotient `H₂ / K₂`. `iso` be symmetric and transitive and must satisfy the second isomorphism theorem `iso (H, H ⊔ K) (H ⊓ K, K)`. Examples include `subgroup G` if `G` is a group, and `submodule R M` if `M` is an `R`-module -/ class jordan_hoelder_lattice (X : Type u) [lattice X] := (is_maximal : X → X → Prop) (lt_of_is_maximal : ∀ {x y}, is_maximal x y → x < y) (sup_eq_of_is_maximal : ∀ {x y z}, is_maximal x z → is_maximal y z → x ≠ y → x ⊔ y = z) (is_maximal_inf_left_of_is_maximal_sup : ∀ {x y}, is_maximal x (x ⊔ y) → is_maximal y (x ⊔ y) → is_maximal (x ⊓ y) x) (iso : (X × X) → (X × X) → Prop) (iso_symm : ∀ {x y}, iso x y → iso y x) (iso_trans : ∀ {x y z}, iso x y → iso y z → iso x z) (second_iso : ∀ {x y}, is_maximal x (x ⊔ y) → iso (x, x ⊔ y) (x ⊓ y, y)) namespace jordan_hoelder_lattice variables {X : Type u} [lattice X] [jordan_hoelder_lattice X] lemma is_maximal_inf_right_of_is_maximal_sup {x y : X} (hxz : is_maximal x (x ⊔ y)) (hyz : is_maximal y (x ⊔ y)) : is_maximal (x ⊓ y) y := begin rw [inf_comm], rw [sup_comm] at hxz hyz, exact is_maximal_inf_left_of_is_maximal_sup hyz hxz end lemma is_maximal_of_eq_inf (x b : X) {a y : X} (ha : x ⊓ y = a) (hxy : x ≠ y) (hxb : is_maximal x b) (hyb : is_maximal y b) : is_maximal a y := begin have hb : x ⊔ y = b, from sup_eq_of_is_maximal hxb hyb hxy, substs a b, exact is_maximal_inf_right_of_is_maximal_sup hxb hyb end lemma second_iso_of_eq {x y a b: X} (hm : is_maximal x a) (ha : x ⊔ y = a) (hb : x ⊓ y = b) : iso (x, a) (b, y) := by substs a b; exact second_iso hm lemma iso_refl_of_is_maximal {x y : X} (h : is_maximal x y) : iso (x, y) (x, y) := second_iso_of_eq h (sup_eq_right.2 (le_of_lt (lt_of_is_maximal h))) (inf_eq_left.2 (le_of_lt (lt_of_is_maximal h))) end jordan_hoelder_lattice open jordan_hoelder_lattice attribute [symm] iso_symm attribute [trans] iso_trans /-- A `composition_series X` is a finite nonempty series of elements of a `jordan_hoelder_lattice` such that each element is maximal inside the next. The length of a `composition_series X` is one less than the number of elements in the series. Note that there is no stipulation that a series start from the bottom of the lattice and finish at the top. For a composition series `s`, `s.top` is the largest element of the series, and `s.bot` is the least element. -/ structure composition_series (X : Type u) [lattice X] [jordan_hoelder_lattice X] : Type u := (length : ℕ) (series : fin (length + 1) → X) (step' : ∀ i : fin length, is_maximal (series i.cast_succ) (series i.succ)) namespace composition_series variables {X : Type u} [lattice X] [jordan_hoelder_lattice X] instance : has_coe_to_fun (composition_series X) := { F := _, coe := composition_series.series } variables {X} lemma step (s : composition_series X) : ∀ i : fin s.length, is_maximal (s i.cast_succ) (s i.succ) := s.step' @[simp] lemma coe_fn_mk (length : ℕ) (series step) : (@composition_series.mk X _ _ length series step : fin length.succ → X) = series := rfl theorem lt_succ (s : composition_series X) (i : fin s.length) : s i.cast_succ < s i.succ := lt_of_is_maximal (s.step _) protected theorem strict_mono (s : composition_series X) : strict_mono s := fin.strict_mono_iff_lt_succ.2 (λ i h, s.lt_succ ⟨i, nat.lt_of_succ_lt_succ h⟩) protected theorem injective (s : composition_series X) : function.injective s := s.strict_mono.injective @[simp] protected theorem inj (s : composition_series X) {i j : fin s.length.succ} : s i = s j ↔ i = j := s.injective.eq_iff instance : has_mem X (composition_series X) := ⟨λ x s, x ∈ set.range s⟩ lemma mem_def {x : X} {s : composition_series X} : x ∈ s ↔ x ∈ set.range s := iff.rfl lemma total {s : composition_series X} {x y : X} (hx : x ∈ s) (hy : y ∈ s) : x ≤ y ∨ y ≤ x := begin rcases set.mem_range.1 hx with ⟨i, rfl⟩, rcases set.mem_range.1 hy with ⟨j, rfl⟩, rw [s.strict_mono.le_iff_le, s.strict_mono.le_iff_le], exact le_total i j end /-- The ordered `list X` of elements of a `composition_series X`. -/ def to_list (s : composition_series X) : list X := list.of_fn s /-- Two `composition_series` are equal if they are the same length and have the same `i`th element for every `i` -/ lemma ext_fun {s₁ s₂ : composition_series X} (hl : s₁.length = s₂.length) (h : ∀ i, s₁ i = s₂ (fin.cast (congr_arg nat.succ hl) i)) : s₁ = s₂ := begin cases s₁, cases s₂, dsimp at *, subst hl, simpa [function.funext_iff] using h end @[simp] lemma length_to_list (s : composition_series X) : s.to_list.length = s.length + 1 := by rw [to_list, list.length_of_fn] lemma to_list_ne_nil (s : composition_series X) : s.to_list ≠ [] := by rw [← list.length_pos_iff_ne_nil, length_to_list]; exact nat.succ_pos _ lemma to_list_injective : function.injective (@composition_series.to_list X _ _) := λ s₁ s₂ (h : list.of_fn s₁ = list.of_fn s₂), have h₁ : s₁.length = s₂.length, from nat.succ_injective ((list.length_of_fn s₁).symm.trans $ (congr_arg list.length h).trans $ list.length_of_fn s₂), have h₂ : ∀ i : fin s₁.length.succ, (s₁ i) = s₂ (fin.cast (congr_arg nat.succ h₁) i), begin assume i, rw [← list.nth_le_of_fn s₁ i, ← list.nth_le_of_fn s₂], simp [h] end, begin cases s₁, cases s₂, dsimp at *, subst h₁, simp only [heq_iff_eq, eq_self_iff_true, true_and], simp only [fin.cast_refl] at h₂, exact funext h₂ end lemma chain'_to_list (s : composition_series X) : list.chain' is_maximal s.to_list := list.chain'_iff_nth_le.2 begin assume i hi, simp only [to_list, list.nth_le_of_fn'], rw [length_to_list] at hi, exact s.step ⟨i, hi⟩ end lemma to_list_sorted (s : composition_series X) : s.to_list.sorted (<) := list.pairwise_iff_nth_le.2 (λ i j hi hij, begin dsimp [to_list], rw [list.nth_le_of_fn', list.nth_le_of_fn'], exact s.strict_mono hij end) lemma to_list_nodup (s : composition_series X) : s.to_list.nodup := list.nodup_iff_nth_le_inj.2 (λ i j hi hj, begin delta to_list, rw [list.nth_le_of_fn', list.nth_le_of_fn', s.injective.eq_iff, fin.ext_iff, fin.coe_mk, fin.coe_mk], exact id end) @[simp] lemma mem_to_list {s : composition_series X} {x : X} : x ∈ s.to_list ↔ x ∈ s := by rw [to_list, list.mem_of_fn, mem_def] /-- Make a `composition_series X` from the ordered list of its elements. -/ def of_list (l : list X) (hl : l ≠ []) (hc : list.chain' is_maximal l) : composition_series X := { length := l.length - 1, series := λ i, l.nth_le i begin conv_rhs { rw ← nat.sub_add_cancel (list.length_pos_of_ne_nil hl) }, exact i.2 end, step' := λ ⟨i, hi⟩, list.chain'_iff_nth_le.1 hc i hi } lemma length_of_list (l : list X) (hl : l ≠ []) (hc : list.chain' is_maximal l) : (of_list l hl hc).length = l.length - 1 := rfl lemma of_list_to_list (s : composition_series X) : of_list s.to_list s.to_list_ne_nil s.chain'_to_list = s := begin refine ext_fun _ _, { rw [length_of_list, length_to_list, nat.succ_sub_one] }, { rintros ⟨i, hi⟩, dsimp [of_list, to_list], rw [list.nth_le_of_fn'] } end @[simp] lemma of_list_to_list' (s : composition_series X) (hs : s.to_list ≠ []) (hc : list.chain' is_maximal s.to_list) : of_list s.to_list s.to_list_ne_nil s.chain'_to_list = s := of_list_to_list s @[simp] lemma to_list_of_list (l : list X) (hl : l ≠ []) (hc : list.chain' is_maximal l) : to_list (of_list l hl hc) = l := begin refine list.ext_le _ _, { rw [length_to_list, length_of_list, nat.sub_add_cancel (list.length_pos_of_ne_nil hl)] }, { assume i hi hi', dsimp [of_list, to_list], rw [list.nth_le_of_fn'], refl } end /-- Two `composition_series` are equal if they have the same elements. See also `ext_fun`. -/ @[ext] lemma ext {s₁ s₂ : composition_series X} (h : ∀ x, x ∈ s₁ ↔ x ∈ s₂) : s₁ = s₂ := to_list_injective $ list.eq_of_perm_of_sorted (by classical; exact list.perm_of_nodup_nodup_to_finset_eq s₁.to_list_nodup s₂.to_list_nodup (finset.ext $ by simp *)) s₁.to_list_sorted s₂.to_list_sorted /-- The largest element of a `composition_series` -/ def top (s : composition_series X) : X := s (fin.last _) lemma top_mem (s : composition_series X) : s.top ∈ s := mem_def.2 (set.mem_range.2 ⟨fin.last _, rfl⟩) @[simp] lemma le_top {s : composition_series X} (i : fin (s.length + 1)) : s i ≤ s.top := s.strict_mono.monotone (fin.le_last _) lemma le_top_of_mem {s : composition_series X} {x : X} (hx : x ∈ s) : x ≤ s.top := let ⟨i, hi⟩ := set.mem_range.2 hx in hi ▸ le_top _ /-- The smallest element of a `composition_series` -/ def bot (s : composition_series X) : X := s 0 lemma bot_mem (s : composition_series X) : s.bot ∈ s := mem_def.2 (set.mem_range.2 ⟨0, rfl⟩) @[simp] lemma bot_le {s : composition_series X} (i : fin (s.length + 1)) : s.bot ≤ s i := s.strict_mono.monotone (fin.zero_le _) lemma bot_le_of_mem {s : composition_series X} {x : X} (hx : x ∈ s) : s.bot ≤ x := let ⟨i, hi⟩ := set.mem_range.2 hx in hi ▸ bot_le _ instance : set_like (composition_series X) X := { coe := λ s, set.range s, coe_injective' := λ s₁ s₂ h, ext $ λ x, begin dsimp at h, rw [mem_def, mem_def, h] end } lemma length_pos_of_mem_ne {s : composition_series X} {x y : X} (hx : x ∈ s) (hy : y ∈ s) (hxy : x ≠ y) : 0 < s.length := let ⟨i, hi⟩ := hx, ⟨j, hj⟩ := hy in have hij : i ≠ j, from mt s.inj.2 $ λ h, hxy (hi ▸ hj ▸ h), hij.lt_or_lt.elim (λ hij, (lt_of_le_of_lt (zero_le i) (lt_of_lt_of_le hij (nat.le_of_lt_succ j.2)))) (λ hji, (lt_of_le_of_lt (zero_le j) (lt_of_lt_of_le hji (nat.le_of_lt_succ i.2)))) lemma forall_mem_eq_of_length_eq_zero {s : composition_series X} (hs : s.length = 0) {x y} (hx : x ∈ s) (hy : y ∈ s) : x = y := by_contradiction (λ hxy, pos_iff_ne_zero.1 (length_pos_of_mem_ne hx hy hxy) hs) /-- `s.take i` returns the inital segment of `s` up to and including the `i`th term. -/ @[simps] def take (s : composition_series X) (i : fin (s.length + 1)) : composition_series X := { length := i, series := λ j, s (fin.cast_le (nat.succ_le_of_lt i.2) j), step' := λ j, by simpa using s.step (fin.cast_le (nat.le_of_lt_succ i.2) j) } @[simp] lemma top_take (s : composition_series X) (i : fin (s.length + 1)) : (s.take i).top = s i := begin cases i, simp [top, fin.ext_iff], end @[simp] lemma bot_take (s : composition_series X) (i : fin (s.length + 1)) : (s.take i).bot = s.bot := rfl /-- `s.drop i` returns the final segment of `s` starting at and including the `i`th term. -/ @[simps] def drop (s : composition_series X) (i : fin (s.length + 1)) : composition_series X := { length := s.length - i, series := have (i : ℕ) + (s.length - i + 1) = s.length + 1, by rw [← add_assoc, nat.add_sub_cancel' (nat.le_of_lt_succ i.is_lt)], λ j, s (fin.cast this (fin.cast_add_right i j)), step' := λ j, begin have := s.step (fin.cast (nat.add_sub_cancel' (nat.le_of_lt_succ i.is_lt)) (fin.cast_add_right i j)), cases j, simpa end } @[simp] lemma top_drop (s : composition_series X) (i : fin (s.length + 1)) : (s.drop i).top = s.top := begin cases i with _ hi, simp [top, fin.ext_iff, nat.add_sub_cancel' (nat.le_of_lt_succ hi)], end @[simp] lemma bot_drop (s : composition_series X) (i : fin (s.length + 1)) : (s.drop i).bot = s i := begin cases i, simp [bot, fin.ext_iff], end /-- Remove the largest element from a `composition_series`. If the series `s` has length zero, then `s.erase_top = s` -/ @[simps] def erase_top (s : composition_series X) : composition_series X := { length := s.length - 1, series := λ i, s ⟨i, lt_of_lt_of_le i.2 (nat.succ_le_succ (nat.sub_le_self _ _))⟩, step' := λ i, begin have := s.step ⟨i, lt_of_lt_of_le i.2 (nat.sub_le_self _ _)⟩, cases i, exact this end } lemma top_erase_top (s : composition_series X) : s.erase_top.top = s ⟨s.length - 1, lt_of_le_of_lt (nat.sub_le_self _ _) (nat.lt_succ_self _)⟩ := show s _ = s _, from congr_arg s begin ext, simp only [erase_top_length, fin.coe_last, fin.coe_cast_succ, fin.coe_of_nat_eq_mod, fin.coe_mk, coe_coe] end lemma erase_top_top_le (s : composition_series X) : s.erase_top.top ≤ s.top := by simp [erase_top, top, s.strict_mono.le_iff_le, fin.le_iff_coe_le_coe, nat.sub_le_self] @[simp] lemma bot_erase_top (s : composition_series X) : s.erase_top.bot = s.bot := rfl lemma mem_erase_top_of_ne_of_mem {s : composition_series X} {x : X} (hx : x ≠ s.top) (hxs : x ∈ s) : x ∈ s.erase_top := begin { rcases hxs with ⟨i, rfl⟩, have hi : (i : ℕ) < (s.length - 1).succ, { conv_rhs { rw [← nat.succ_sub (length_pos_of_mem_ne ⟨i, rfl⟩ s.top_mem hx), nat.succ_sub_one] }, exact lt_of_le_of_ne (nat.le_of_lt_succ i.2) (by simpa [top, s.inj, fin.ext_iff] using hx) }, refine ⟨i.cast_succ, _⟩, simp [fin.ext_iff, nat.mod_eq_of_lt hi] } end lemma erase_top_le (s : composition_series X) : s.erase_top ≤ s := begin rintros x ⟨i, rfl⟩, simp [mem_def, erase_top], end lemma mem_erase_top {s : composition_series X} {x : X} (h : 0 < s.length) : x ∈ s.erase_top ↔ x ≠ s.top ∧ x ∈ s := begin simp only [mem_def], dsimp only [erase_top, coe_fn_mk], split, { rintros ⟨i, rfl⟩, have hi : (i : ℕ) < s.length, { conv_rhs { rw [← nat.succ_sub_one s.length, nat.succ_sub h] }, exact i.2 }, simp [top, fin.ext_iff, (ne_of_lt hi)] }, { intro h, exact mem_erase_top_of_ne_of_mem h.1 h.2 } end lemma lt_top_of_mem_erase_top {s : composition_series X} {x : X} (h : 0 < s.length) (hx : x ∈ s.erase_top) : x < s.top := lt_of_le_of_ne (le_top_of_mem ((mem_erase_top h).1 hx).2) ((mem_erase_top h).1 hx).1 lemma is_maximal_erase_top_top {s : composition_series X} (h : 0 < s.length) : is_maximal s.erase_top.top s.top := have s.length - 1 + 1 = s.length, by conv_rhs { rw [← nat.succ_sub_one s.length] }; rw nat.succ_sub h, begin rw [top_erase_top, top], convert s.step ⟨s.length - 1, nat.sub_lt h zero_lt_one⟩; ext; simp [this] end /-- Remove the smallest element from a `composition_series`. If the series `s` has length zero, then `s.erase_bot = s` -/ @[simps] def erase_bot (s : composition_series X) : composition_series X := { length := s.length - 1, series := λ i, if h0s : 0 < s.length then s (fin.succ ⟨i, (by conv_rhs {rw [← nat.sub_add_cancel h0s]}; exact i.is_lt)⟩) else s.top, step' := λ i, begin have := s.step (⟨i + 1, nat.add_lt_of_lt_sub_right i.2⟩), have h0s : 0 < s.length, from lt_of_lt_of_le (fin.pos_iff_nonempty.2 ⟨i⟩) (nat.sub_le_self _ _), cases i, simpa [h0s] end } lemma bot_erase_bot (s : composition_series X) (h0s : 0 < s.length): s.erase_bot.bot = s ⟨1, nat.succ_lt_succ h0s⟩ := by simp [erase_bot, bot, h0s] lemma le_erase_bot_bot (s : composition_series X) : s.bot ≤ s.erase_bot.bot := by simp [erase_bot, bot, fin.le_iff_coe_le_coe, nat.sub_le_self]; split_ifs; simp [s.strict_mono.le_iff_le, fin.le_iff_coe_le_coe] @[simp] lemma top_erase_bot (s : composition_series X) : s.erase_bot.top = s.top := begin simp [erase_bot, top], split_ifs, { simp [fin.ext_iff, nat.sub_add_cancel h] }, { refl } end lemma mem_erase_bot_of_ne_of_mem {s : composition_series X} {x : X} (hx : x ≠ s.bot) (hxs : x ∈ s) : x ∈ s.erase_bot := begin rcases hxs with ⟨i, rfl⟩, have hi0 : (0 : ℕ) < i, from nat.pos_of_ne_zero (by simpa [bot, fin.ext_iff] using hx), have hi : (i - 1 : ℕ) < (s.length - 1).succ, { conv_rhs { rw [← nat.succ_sub (length_pos_of_mem_ne ⟨i, rfl⟩ s.bot_mem hx), nat.succ_sub_one] }, exact (nat.sub_lt_right_iff_lt_add hi0).2 i.2 }, refine ⟨⟨i - 1, hi⟩, _⟩, dsimp [erase_bot], split_ifs, { simp [fin.ext_iff, nat.sub_add_cancel hi0] }, { cases i, simp [*, bot, top, fin.ext_iff] at *, simp * at *} end lemma erase_bot_le (s : composition_series X) : s.erase_top ≤ s := begin rintros x ⟨i, rfl⟩, simp [mem_def], end lemma mem_erase_bot {s : composition_series X} {x : X} (h0s : 0 < s.length) : x ∈ s.erase_bot ↔ x ≠ s.bot ∧ x ∈ s := begin simp only [mem_def], dsimp only [erase_bot, coe_fn_mk], split, { rintros ⟨i, rfl⟩, have hi : (i : ℕ) < s.length, { conv_rhs { rw [← nat.succ_sub_one s.length, nat.succ_sub h0s] }, exact i.2 }, simp [bot, fin.ext_iff, (ne_of_lt hi), h0s] }, { intro h, exact mem_erase_bot_of_ne_of_mem h.1 h.2 } end lemma bot_lt_of_mem_erase_bot {s : composition_series X} {x : X} (h : 0 < s.length) (hx : x ∈ s.erase_bot) : s.bot < x := lt_of_le_of_ne (bot_le_of_mem ((mem_erase_bot h).1 hx).2) ((mem_erase_bot h).1 hx).1.symm lemma is_maximal_bot_erase_bot {s : composition_series X} (h : 0 < s.length) : is_maximal s.bot s.erase_bot.bot := have s.length - 1 + 1 = s.length, by conv_rhs { rw [← nat.succ_sub_one s.length] }; rw nat.succ_sub h, begin rw [bot_erase_bot s h, bot], convert s.step ⟨0, h⟩, end /-- If `s.top` and `x` are both maximal inside `y`, and `¬ s i ≤ x`, then `x ⊓ s i` is maximal inside `s i` -/ lemma is_maximal_inf_series' {s : composition_series X} {x y : X} {i : fin (s.length + 1)} (hy : is_maximal x y) (ht : is_maximal s.top y): ¬ s i ≤ x → is_maximal (x ⊓ s i) (s i) := begin refine fin.reverse_induction _ _ i, { intro hb, refine is_maximal_of_eq_inf _ _ rfl _ hy ht, { rintro rfl, exact hb (le_refl _) } }, { intros i ih hb, refine is_maximal_of_eq_inf (x ⊓ s i.succ) _ _ _ (ih _) (s.step i), { rw [inf_assoc, inf_eq_right.2 (le_of_lt (lt_of_is_maximal (s.step _))), inf_comm] }, { assume h, exact hb (h ▸ inf_le_left) }, { assume h, exact hb (le_trans (s.strict_mono.monotone (le_of_lt i.cast_succ_lt_succ)) h) } } end /-- If `x` is maximal inside `s j`, and `¬ s i ≤ x` and `i ≤ j`, then `x ⊓ s i` is maximal inside `s i` -/ lemma is_maximal_inf_series {s : composition_series X} {x : X} {i j : fin (s.length + 1)} (hij : i ≤ j) (hm : is_maximal x (s j)) (hb : ¬ s i ≤ x) : is_maximal (x ⊓ s i) (s i) := begin have : ∀ (t : composition_series X) (i : fin (t.length + 1)), is_maximal x t.top → ¬ t i ≤ x → is_maximal (x ⊓ t i) (t i), { intros t i hm, refine fin.last_cases _ _ i, { assume h, erw [inf_eq_left.2 (le_of_lt (lt_of_is_maximal hm))], exact hm }, { intros i hb, have h0t : 0 < t.length, from fin.pos_iff_nonempty.2 ⟨i⟩, have := @is_maximal_inf_series' _ _ _ _ _ _ ⟨i, by rw [erase_top_length, nat.sub_add_cancel h0t]; exact i.2⟩ hm (is_maximal_erase_top_top h0t) hb, exact this } }, let i' : fin ((s.take j).length + 1) := ⟨i, nat.lt_succ_of_le hij⟩, simpa using this (s.take j) i' (by simpa using hm) (by simpa using hb) end lemma append_cast_add_aux {s₁ s₂ : composition_series X} (h : s₁ (fin.last _) = s₂ 0) (i : fin s₁.length) : fin.append (nat.add_succ _ _).symm (s₁ ∘ fin.cast_succ) s₂ (fin.cast_add s₂.length i).cast_succ = s₁ i.cast_succ := by { cases i, simp [fin.append, *] } lemma append_succ_cast_add_aux {s₁ s₂ : composition_series X} (h : s₁ (fin.last _) = s₂ 0) (i : fin s₁.length) : fin.append (nat.add_succ _ _).symm (s₁ ∘ fin.cast_succ) s₂ (fin.cast_add s₂.length i).succ = s₁ i.succ := begin cases i with i hi, simp only [fin.append, hi, fin.succ_mk, function.comp_app, fin.cast_succ_mk, fin.coe_mk, fin.cast_add_mk], split_ifs, { refl }, { have : i + 1 = s₁.length, from le_antisymm hi (le_of_not_gt h_1), calc s₂ ⟨i + 1 - s₁.length, by simp [this]⟩ = s₂ 0 : congr_arg s₂ (by simp [fin.ext_iff, this]) ... = s₁ (fin.last _) : h.symm ... = _ : congr_arg s₁ (by simp [fin.ext_iff, this]) } end lemma append_cast_add_right_aux {s₁ s₂ : composition_series X} (h : s₁ (fin.last _) = s₂ 0) (i : fin s₂.length) : fin.append (nat.add_succ _ _).symm (s₁ ∘ fin.cast_succ) s₂ (fin.cast_add_right s₁.length i).cast_succ = s₂ i.cast_succ := by { cases i, simp [fin.append, *] } lemma append_succ_cast_add_right_aux {s₁ s₂ : composition_series X} (h : s₁ (fin.last _) = s₂ 0) (i : fin s₂.length) : fin.append (nat.add_succ _ _).symm (s₁ ∘ fin.cast_succ) s₂ (fin.cast_add_right s₁.length i).succ = s₂ i.succ := begin cases i with i hi, simp [fin.append, add_assoc] end /-- Append two composition series `s₁` and `s₂` such that the least element of `s₁` is the maximum element of `s₂`. -/ @[simps length] def append (s₁ s₂ : composition_series X) (h : s₁.top = s₂.bot) : composition_series X := { length := s₁.length + s₂.length, series := fin.append (nat.add_succ _ _).symm (s₁ ∘ fin.cast_succ) s₂, step' := λ i, begin refine fin.add_cases _ _ i, { intro i, rw [append_succ_cast_add_aux h, append_cast_add_aux h], exact s₁.step i }, { intro i, rw [append_cast_add_right_aux h, append_succ_cast_add_right_aux h], exact s₂.step i } end } @[simp] lemma append_cast_add {s₁ s₂ : composition_series X} (h : s₁.top = s₂.bot) (i : fin s₁.length) : append s₁ s₂ h (fin.cast_add s₂.length i).cast_succ = s₁ i.cast_succ := append_cast_add_aux h i @[simp] lemma append_succ_cast_add {s₁ s₂ : composition_series X} (h : s₁.top = s₂.bot) (i : fin s₁.length) : append s₁ s₂ h (fin.cast_add s₂.length i).succ = s₁ i.succ := append_succ_cast_add_aux h i @[simp] lemma append_cast_add_right {s₁ s₂ : composition_series X} (h : s₁.top = s₂.bot) (i : fin s₂.length) : append s₁ s₂ h (fin.cast_add_right s₁.length i).cast_succ = s₂ i.cast_succ := append_cast_add_right_aux h i @[simp] lemma append_succ_cast_add_right {s₁ s₂ : composition_series X} (h : s₁.top = s₂.bot) (i : fin s₂.length) : append s₁ s₂ h (fin.cast_add_right s₁.length i).succ = s₂ i.succ := append_succ_cast_add_right_aux h i @[simp] lemma top_append {s₁ s₂ : composition_series X} (h : s₁.top = s₂.bot) : (append s₁ s₂ h).top = s₂.top := by simp [top, append, fin.append, fin.ext_iff] @[simp] lemma bot_append {s₁ s₂ : composition_series X} (h : s₁.top = s₂.bot) : (append s₁ s₂ h).bot = s₁.bot := have s₁.length = 0 → s₁ 0 = s₁.top := λ h, forall_mem_eq_of_length_eq_zero h s₁.bot_mem s₁.top_mem, by revert this; simp [bot, append, fin.append, fin.ext_iff, h] { contextual := tt } lemma take_append_drop (s : composition_series X) (i : fin (s.length + 1)) : (s.take i).append (s.drop i) (by simp) = s := begin refine ext_fun _ _, { simp [nat.add_sub_cancel' (nat.le_of_lt_succ i.is_lt)] }, { intro j, refine fin.last_cases _ _ j, { rw [← top, top_append], simp [top, fin.ext_iff] }, { assume j, refine fin.add_cases _ _ j, { intro j, simp [fin.ext_iff] }, { intro j, simp [fin.ext_iff] } } } end /-- Add an element to the top of a `composition_series` -/ @[simps length] def snoc (s : composition_series X) (x : X) (hsat : is_maximal s.top x) : composition_series X := { length := s.length + 1, series := fin.snoc s x, step' := λ i, begin refine fin.last_cases _ _ i, { rwa [fin.snoc_cast_succ, fin.succ_last, fin.snoc_last, ← top] }, { intro i, rw [fin.snoc_cast_succ, ← fin.cast_succ_fin_succ, fin.snoc_cast_succ], exact s.step _ } end } @[simp] lemma top_snoc (s : composition_series X) (x : X) (hsat : is_maximal s.top x) : (snoc s x hsat).top = x := fin.snoc_last _ _ @[simp] lemma snoc_last (s : composition_series X) (x : X) (hsat : is_maximal s.top x) : snoc s x hsat (fin.last (s.length + 1)) = x := fin.snoc_last _ _ @[simp] lemma snoc_cast_succ (s : composition_series X) (x : X) (hsat : is_maximal s.top x) (i : fin (s.length + 1)) : snoc s x hsat (i.cast_succ) = s i := fin.snoc_cast_succ _ _ _ @[simp] lemma bot_snoc (s : composition_series X) (x : X) (hsat : is_maximal s.top x) : (snoc s x hsat).bot = s.bot := by rw [bot, bot, ← fin.cast_succ_zero, snoc_cast_succ] lemma mem_snoc {s : composition_series X} {x y: X} {hsat : is_maximal s.top x} : y ∈ snoc s x hsat ↔ y ∈ s ∨ y = x := begin simp only [snoc, mem_def], split, { rintros ⟨i, rfl⟩, refine fin.last_cases _ (λ i, _) i, { right, simp }, { left, simp } }, { intro h, rcases h with ⟨i, rfl⟩ | rfl, { use i.cast_succ, simp }, { use (fin.last _), simp } } end lemma eq_snoc_erase_top {s : composition_series X} (h : 0 < s.length) : s = snoc (erase_top s) s.top (is_maximal_erase_top_top h) := begin ext x, simp [mem_snoc, mem_erase_top h], by_cases h : x = s.top; simp [*, s.top_mem] end @[simp] lemma snoc_erase_top_top {s : composition_series X} (h : is_maximal s.erase_top.top s.top) : s.erase_top.snoc s.top h = s := have h : 0 < s.length, from nat.pos_of_ne_zero begin assume hs, refine ne_of_gt (lt_of_is_maximal h) _, simp [top, fin.ext_iff, hs] end, (eq_snoc_erase_top h).symm /-- Add an element to the bottom of a `composition_series` -/ @[simps length] def cons (s : composition_series X) (x : X) (hsat : is_maximal x s.bot) : composition_series X := { length := s.length + 1, series := fin.cons x s, step' := λ i, begin refine fin.cases _ _ i, { rwa [fin.cons_succ, fin.cast_succ_zero, fin.cons_zero, ← bot] }, { intro i, rw [fin.cons_succ, fin.cast_succ_fin_succ, fin.cons_succ], exact s.step _ } end } @[simp] lemma bot_cons (s : composition_series X) (x : X) (hsat : is_maximal x s.bot) : (cons s x hsat).bot = x := by simp [bot, cons] @[simp] lemma cons_zero (s : composition_series X) (x : X) (hsat : is_maximal x s.bot) : cons s x hsat 0 = x := bot_cons _ _ _ @[simp] lemma cons_succ (s : composition_series X) (x : X) (hsat : is_maximal x s.bot) (i : fin (s.length + 1)) : cons s x hsat (i.succ) = s i := fin.cons_succ _ _ _ @[simp] lemma top_cons (s : composition_series X) (x : X) (hsat : is_maximal x s.bot) : (cons s x hsat).top = s.top := begin rw [top], dsimp only [cons_length], rw [← fin.succ_last, cons_succ, top] end lemma mem_cons {s : composition_series X} {x y: X} {hsat : is_maximal x s.bot} : y ∈ cons s x hsat ↔ y ∈ s ∨ y = x := begin simp only [snoc, mem_def], split, { rintros ⟨i, rfl⟩, refine fin.cases _ (λ i, _) i, { right, simp }, { left, simp } }, { intro h, rcases h with ⟨i, rfl⟩ | rfl, { use i.succ, simp }, { use 0, simp } } end lemma eq_cons_erase_bot {s : composition_series X} (h : 0 < s.length) : s = cons (erase_bot s) s.bot (is_maximal_bot_erase_bot h) := begin ext x, simp [mem_cons, mem_erase_bot h], by_cases h : x = s.bot; simp [*, s.bot_mem] end @[simp] lemma cons_erase_bot_bot {s : composition_series X} (h : is_maximal s.bot s.erase_bot.bot) : s.erase_bot.cons s.bot h = s := have h : 0 < s.length, from nat.pos_of_ne_zero begin assume hs, refine ne_of_gt (lt_of_is_maximal h) _, simp [bot, fin.ext_iff, hs, erase_bot, top] end, (eq_cons_erase_bot h).symm /-- For a composition series `s` and an element `x`, return the series whose `i`th term is `x ⊓ s i` -/ @[simps] def infinum (s : composition_series X) (x : X) (hm : is_maximal x s.top) (hb : ¬ s.bot ≤ x) : composition_series X := { length := s.length, series := λ i, x ⊓ s i, step' := λ i, begin generalize hj : i.succ = j, revert i, refine fin.last_cases _ _ j, { intros i hj, rw [top] at hm, rw [inf_eq_left.2 (le_of_lt (lt_of_is_maximal hm))], refine is_maximal_of_eq_inf _ _ inf_comm _ (s.step i) (hj.symm ▸ hm), rintro rfl, exact hb (bot_le _) }, { intros j i hj, rw [← hj], exact is_maximal_of_eq_inf (s i.cast_succ) (s i.succ) (by rw [inf_comm, inf_assoc, inf_eq_right.2 (le_of_lt (s.strict_mono (i.cast_succ_lt_succ)))]) (λ h, hb (le_trans (bot_le i.cast_succ) (h.symm ▸ inf_le_left))) (s.step i) (is_maximal_inf_series (fin.le_last _) hm (λ h, hb (le_trans (s.strict_mono.monotone (fin.zero_le _)) h))) } end } @[simp] lemma infinum_top (s : composition_series X) (x : X) (hm : is_maximal x s.top) (hb : ¬ s.bot ≤ x) : (s.infinum x hm hb).top = x := begin dsimp [top], simpa [top] using le_of_lt (lt_of_is_maximal hm) end lemma iso_inf_mem_top {s : composition_series X} {x : X} {i : fin (s.length + 1)} (hm : is_maximal x s.top) : ∀ (hb : ¬s i ≤ x), iso (x ⊓ s i, s i) (x, s.top) := begin refine fin.reverse_induction _ _ i, { intro hb, rw [← top, inf_eq_left.2], { exact iso_refl_of_is_maximal hm }, { exact le_of_lt (lt_of_is_maximal hm) } }, { intros i ih hb, have : ¬ s i.succ ≤ x, from λ h, hb $ le_trans (le_of_lt (s.strict_mono i.cast_succ_lt_succ)) h, refine iso_trans _ (ih this), exact iso_symm (second_iso_of_eq (is_maximal_inf_series (fin.le_last _) hm this) (sup_eq_of_is_maximal (is_maximal_inf_series (fin.le_last _) hm this) (s.step i) (λ h, hb (h ▸ inf_le_left))) (by rw [inf_assoc, inf_eq_right.2 (le_of_lt (s.strict_mono i.cast_succ_lt_succ))])) } end /-- Two `composition_series X`, `s₁` and `s₂` are equivalent if there is a bijection `e : fin s₁.length ≃ fin s₂.length` such that for any `i`, `iso (s₁ i) (s₁ i.succ) (s₂ (e i), s₂ (e i.succ))` -/ def equivalent (s₁ s₂ : composition_series X) : Prop := ∃ f : fin s₁.length ≃ fin s₂.length, ∀ i : fin s₁.length, iso (s₁ i.cast_succ, s₁ i.succ) (s₂ (f i).cast_succ, s₂ (f i).succ) namespace equivalent @[refl] lemma refl (s : composition_series X) : equivalent s s := ⟨equiv.refl _, λ _, iso_refl_of_is_maximal (s.step _)⟩ @[symm] lemma symm {s₁ s₂ : composition_series X} (h : equivalent s₁ s₂) : equivalent s₂ s₁ := ⟨h.some.symm, λ i, iso_symm (by simpa using h.some_spec (h.some.symm i))⟩ @[trans] lemma trans {s₁ s₂ s₃ : composition_series X} (h₁ : equivalent s₁ s₂) (h₂ : equivalent s₂ s₃) : equivalent s₁ s₃ := ⟨h₁.some.trans h₂.some, λ i, iso_trans (h₁.some_spec i) (h₂.some_spec (h₁.some i))⟩ lemma append {s₁ s₂ t₁ t₂ : composition_series X} (hs : s₁.top = s₂.bot) (ht : t₁.top = t₂.bot) (h₁ : equivalent s₁ t₁) (h₂ : equivalent s₂ t₂) : equivalent (append s₁ s₂ hs) (append t₁ t₂ ht) := let e : fin (s₁.length + s₂.length) ≃ fin (t₁.length + t₂.length) := calc fin (s₁.length + s₂.length) ≃ fin s₁.length ⊕ fin s₂.length : fin_sum_fin_equiv.symm ... ≃ fin t₁.length ⊕ fin t₂.length : equiv.sum_congr h₁.some h₂.some ... ≃ fin (t₁.length + t₂.length) : fin_sum_fin_equiv in ⟨e, begin assume i, refine fin.add_cases _ _ i, { assume i, simpa [top, bot] using h₁.some_spec i }, { assume i, simpa [top, bot] using h₂.some_spec i } end⟩ protected lemma snoc {s₁ s₂ : composition_series X} {x₁ x₂ : X} {hsat₁ : is_maximal s₁.top x₁} {hsat₂ : is_maximal s₂.top x₂} (hequiv : equivalent s₁ s₂) (htop : iso (s₁.top, x₁) (s₂.top, x₂)) : equivalent (s₁.snoc x₁ hsat₁) (s₂.snoc x₂ hsat₂) := let e : fin s₁.length.succ ≃ fin s₂.length.succ := calc fin (s₁.length + 1) ≃ option (fin s₁.length) : fin_succ_equiv_last ... ≃ option (fin s₂.length) : functor.map_equiv option hequiv.some ... ≃ fin (s₂.length + 1) : fin_succ_equiv_last.symm in ⟨e, λ i, begin refine fin.last_cases _ _ i, { simpa [top] using htop }, { assume i, simpa [fin.succ_cast_succ] using hequiv.some_spec i } end⟩ protected lemma cons {s₁ s₂ : composition_series X} {x₁ x₂ : X} {hsat₁ : is_maximal x₁ s₁.bot} {hsat₂ : is_maximal x₂ s₂.bot} (hequiv : equivalent s₁ s₂) (hbot : iso (x₁, s₁.bot) (x₂, s₂.bot)) : equivalent (s₁.cons x₁ hsat₁) (s₂.cons x₂ hsat₂) := let e : fin s₁.length.succ ≃ fin s₂.length.succ := calc fin (s₁.length + 1) ≃ option (fin s₁.length) : fin_succ_equiv _ ... ≃ option (fin s₂.length) : functor.map_equiv option hequiv.some ... ≃ fin (s₂.length + 1) : (fin_succ_equiv _).symm in ⟨e, λ i, begin refine fin.cases _ _ i, { simpa [bot] using hbot }, { assume i, simpa [← fin.succ_cast_succ] using hequiv.some_spec i } end⟩ lemma cons_snoc {s₁ s₂ : composition_series X} {x₁ x₂ : X} {hsat₁ : is_maximal x₁ s₁.bot} {hsat₂ : is_maximal s₂.top x₂} (hequiv : equivalent s₁ s₂) (hbottop : iso (x₁, s₁.bot) (s₂.top, x₂)) : equivalent (s₁.cons x₁ hsat₁) (s₂.snoc x₂ hsat₂) := let e : fin s₁.length.succ ≃ fin s₂.length.succ := calc fin (s₁.length + 1) ≃ option (fin s₁.length) : fin_succ_equiv _ ... ≃ option (fin s₂.length) : functor.map_equiv option hequiv.some ... ≃ fin (s₂.length + 1) : fin_succ_equiv_last.symm in ⟨e, λ i, begin refine fin.cases _ _ i, { simpa [bot] using hbottop }, { assume i, simp [← fin.succ_cast_succ], simpa [fin.succ_cast_succ] using hequiv.some_spec i } end⟩ lemma infinum (s : composition_series X) (x : X) (hm : is_maximal x s.top) (hb : ¬ s.bot ≤ x) : equivalent (s.infinum x hm hb) s := ⟨equiv.refl _, λ i, begin simp, refine iso_symm (second_iso_of_eq _ _ _), { simpa using s.step i }, { refine sup_eq_of_is_maximal _ _ _, { simpa using s.step i }, { exact is_maximal_inf_series (fin.le_last _) (by simpa) (λ h, hb (le_trans (bot_le _) h)) }, { intro h, refine hb (le_trans (bot_le i.cast_succ) _), simp * at * } }, { simp [inf_left_comm, inf_eq_left.2 (le_of_lt (s.strict_mono (fin.cast_succ_lt_succ i)))] } end⟩ lemma snoc_infinum_cons (s : composition_series X) (x : X) (hm : is_maximal x s.top) (hb : ¬ s.bot ≤ x) : ((s.infinum x hm hb).snoc s.top (by simpa)).equivalent (s.cons (x ⊓ s.bot) (is_maximal_inf_series (fin.zero_le _) hm hb)) := equivalent.symm $ equivalent.cons_snoc (equivalent.infinum _ _ _ _).symm (by simpa [bot] using iso_inf_mem_top hm hb) lemma length_eq {s₁ s₂ : composition_series X} (h : equivalent s₁ s₂) : s₁.length = s₂.length := by simpa using fintype.card_congr h.some lemma snoc_snoc_swap {s : composition_series X} {x₁ x₂ y₁ y₂ : X} {hsat₁ : is_maximal s.top x₁} {hsat₂ : is_maximal s.top x₂} {hsaty₁ : is_maximal (snoc s x₁ hsat₁).top y₁} {hsaty₂ : is_maximal (snoc s x₂ hsat₂).top y₂} (hr₁ : iso (s.top, x₁) (x₂, y₂)) (hr₂ : iso (x₁, y₁) (s.top, x₂)) : equivalent (snoc (snoc s x₁ hsat₁) y₁ hsaty₁) (snoc (snoc s x₂ hsat₂) y₂ hsaty₂) := let e : fin (s.length + 1 + 1) ≃ fin (s.length + 1 + 1) := equiv.swap (fin.last _) (fin.cast_succ (fin.last _)) in have h1 : ∀ {i : fin s.length}, i.cast_succ.cast_succ ≠ (fin.last _).cast_succ, from λ _, ne_of_lt (by simp [fin.cast_succ_lt_last]), have h2 : ∀ {i : fin s.length}, i.cast_succ.cast_succ ≠ (fin.last _), from λ _, ne_of_lt (by simp [fin.cast_succ_lt_last]), ⟨e, begin intro i, dsimp only [e], refine fin.last_cases _ (λ i, _) i, { erw [equiv.swap_apply_left, snoc_cast_succ, snoc_last, fin.succ_last, snoc_last, snoc_cast_succ, snoc_cast_succ, fin.succ_cast_succ, snoc_cast_succ, fin.succ_last, snoc_last], exact hr₂ }, { refine fin.last_cases _ (λ i, _) i, { erw [equiv.swap_apply_right, snoc_cast_succ, snoc_cast_succ, snoc_cast_succ, fin.succ_cast_succ, snoc_cast_succ, fin.succ_last, snoc_last, snoc_last, fin.succ_last, snoc_last], exact hr₁ }, { erw [equiv.swap_apply_of_ne_of_ne h2 h1, snoc_cast_succ, snoc_cast_succ, snoc_cast_succ, snoc_cast_succ, fin.succ_cast_succ, snoc_cast_succ, fin.succ_cast_succ, snoc_cast_succ, snoc_cast_succ, snoc_cast_succ], exact iso_refl_of_is_maximal (s.step i) } } end⟩ end equivalent lemma length_eq_zero_of_bot_eq_bot_of_top_eq_top_of_length_eq_zero {s₁ s₂ : composition_series X} (hb : s₁.bot = s₂.bot) (ht : s₁.top = s₂.top) (hs₁ : s₁.length = 0) : s₂.length = 0 := begin have : s₁.bot = s₁.top, from congr_arg s₁ (fin.ext (by simp [hs₁])), have : (fin.last s₂.length) = (0 : fin s₂.length.succ), from s₂.injective (hb.symm.trans (this.trans ht)).symm, simpa [fin.ext_iff] end lemma length_pos_of_bot_eq_bot_of_top_eq_top_of_length_pos {s₁ s₂ : composition_series X} (hb : s₁.bot = s₂.bot) (ht : s₁.top = s₂.top) : 0 < s₁.length → 0 < s₂.length := not_imp_not.1 begin simp only [pos_iff_ne_zero, ne.def, not_iff_not, not_not], exact length_eq_zero_of_bot_eq_bot_of_top_eq_top_of_length_eq_zero hb.symm ht.symm end lemma eq_of_bot_eq_bot_of_top_eq_top_of_length_eq_zero {s₁ s₂ : composition_series X} (hb : s₁.bot = s₂.bot) (ht : s₁.top = s₂.top) (hs₁0 : s₁.length = 0) : s₁ = s₂ := have ∀ x, x ∈ s₁ ↔ x = s₁.top, from λ x, ⟨λ hx, forall_mem_eq_of_length_eq_zero hs₁0 hx s₁.top_mem, λ hx, hx.symm ▸ s₁.top_mem⟩, have ∀ x, x ∈ s₂ ↔ x = s₂.top, from λ x, ⟨λ hx, forall_mem_eq_of_length_eq_zero (length_eq_zero_of_bot_eq_bot_of_top_eq_top_of_length_eq_zero hb ht hs₁0) hx s₂.top_mem, λ hx, hx.symm ▸ s₂.top_mem⟩, by { ext, simp * } /-- Given a `composition_series`, `s`, and an element `x` such that `x` is maximal inside `s.top` there is a series, `t`, such that `t.top = x`, `t.bot = s.bot` and `snoc t s.top _` is equivalent to `s`. -/ lemma exists_top_eq_snoc_equivalant (s : composition_series X) (x : X) (hm : is_maximal x s.top) (hb : s.bot ≤ x) : ∃ t : composition_series X, t.bot = s.bot ∧ t.length + 1 = s.length ∧ ∃ htx : t.top = x, equivalent s (snoc t s.top (htx.symm ▸ hm)) := begin induction hn : s.length with n ih generalizing s x, { exact (ne_of_gt (lt_of_le_of_lt hb (lt_of_is_maximal hm)) (forall_mem_eq_of_length_eq_zero hn s.top_mem s.bot_mem)).elim }, { have h0s : 0 < s.length, from hn.symm ▸ nat.succ_pos _, by_cases hetx : s.erase_top.top = x, { use s.erase_top, simp [← hetx, hn] }, { have imxs : is_maximal (x ⊓ s.erase_top.top) s.erase_top.top, from is_maximal_of_eq_inf x s.top rfl (ne.symm hetx) hm (is_maximal_erase_top_top h0s), have := ih _ _ imxs (le_inf (by simpa) (le_top_of_mem s.erase_top.bot_mem)) (by simp [hn]), rcases this with ⟨t, htb, htl, htt, hteqv⟩, have hmtx : is_maximal t.top x, from is_maximal_of_eq_inf s.erase_top.top s.top (by rw [inf_comm, htt]) hetx (is_maximal_erase_top_top h0s) hm, use snoc t x hmtx, refine ⟨by simp [htb], by simp [htl], by simp, _⟩, have : s.equivalent ((snoc t s.erase_top.top (htt.symm ▸ imxs)).snoc s.top (by simpa using is_maximal_erase_top_top h0s)), { conv_lhs { rw eq_snoc_erase_top h0s }, exact equivalent.snoc hteqv (by simpa using iso_refl_of_is_maximal (is_maximal_erase_top_top h0s)) }, refine this.trans _, refine equivalent.snoc_snoc_swap _ _, { exact iso_symm (second_iso_of_eq hm (sup_eq_of_is_maximal hm (is_maximal_erase_top_top h0s) (ne.symm hetx)) htt.symm) }, { exact second_iso_of_eq (is_maximal_erase_top_top h0s) (sup_eq_of_is_maximal (is_maximal_erase_top_top h0s) hm hetx) (by rw [inf_comm, htt]) } } } end /-- The **Jordan-Hölder** theorem, stated for any `jordan_hoelder_lattice`. If two composition series start and finish at the same place, they are equivalent. -/ theorem jordan_hoelder (s₁ s₂ : composition_series X) (hb : s₁.bot = s₂.bot) (ht : s₁.top = s₂.top) : equivalent s₁ s₂ := begin induction hle : s₁.length with n ih generalizing s₁ s₂, { rw [eq_of_bot_eq_bot_of_top_eq_top_of_length_eq_zero hb ht hle] }, { have h0s₂ : 0 < s₂.length, from length_pos_of_bot_eq_bot_of_top_eq_top_of_length_pos hb ht (hle.symm ▸ nat.succ_pos _), rcases exists_top_eq_snoc_equivalant s₁ s₂.erase_top.top (ht.symm ▸ is_maximal_erase_top_top h0s₂) (hb.symm ▸ s₂.bot_erase_top ▸ bot_le_of_mem (top_mem _)) with ⟨t, htb, htl, htt, hteq⟩, have := ih t s₂.erase_top (by simp [htb, ← hb]) htt (nat.succ_inj'.1 (htl.trans hle)), refine hteq.trans _, conv_rhs { rw [eq_snoc_erase_top h0s₂] }, simp only [ht], exact equivalent.snoc this (by simp [htt, iso_refl_of_is_maximal (is_maximal_erase_top_top h0s₂)]) } end end composition_series
09c585bc09bbdf17d3ffe26ff5432666097a8aa3
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/is_true.lean
0614c69e667e99113d5350984da17e028548515b
[ "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
248
lean
import data.nat open nat example : is_true (2 = (2:nat)) := trivial example : is_false (3 = (2:nat)) := trivial example : is_true (2 < (3:nat)) := trivial example : is_true (3 ∣ (9:nat)) := trivial example : is_false (3 ∣ (7:nat)) := trivial
c37032e69bb95ff600a6dedf21742a55c54b1f04
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/congr.lean
128b24d53e494414fcc1a466de00b3fd375edc3b
[ "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
8,822
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import tactic.lint import tactic.ext /-! # Congruence and related tactics This file contains the tactic `congr'`, which is an extension of `congr`, and various tactics using `congr'` internally. `congr'` has some advantages over `congr`: * It turns `↔` to equalities, before trying another congr lemma * You can write `congr' n` to give the maximal depth of recursive applications. This is useful if `congr` breaks down the goal to aggressively, and the resulting goals are false. * You can write `congr' with ...` to do `congr', ext ...` in a single tactic. Other tactics in this file: * `rcongr`: repeatedly apply `congr'` and `ext.` * `convert`: like `exact`, but produces an equality goal if the type doesn't match. * `convert_to`: changes the goal, if you prove an equality between the old goal and the new goal. * `ac_change`: like `convert_to`, but uses `ac_refl` to discharge the goals. -/ open tactic setup_tactic_parser namespace tactic /-- Apply the constant `iff_of_eq` to the goal. -/ meta def apply_iff_congr_core : tactic unit := applyc ``iff_of_eq /-- The main part of the body for the loop in `congr'`. This will try to replace a goal `f x = f y` with `x = y`. Also has support for `==` and `↔`. -/ meta def congr_core' : tactic unit := do tgt ← target, apply_eq_congr_core tgt <|> apply_heq_congr_core <|> apply_iff_congr_core <|> fail "congr tactic failed" /-- The main function in `convert_to`. Changes the goal to `r` and a proof obligation that the goal is equal to `r`. -/ meta def convert_to_core (r : pexpr) : tactic unit := do tgt ← target, h ← to_expr ``(_ : %%tgt = %%r), rewrite_target h, swap /-- Attempts to prove the goal by proof irrelevance, but avoids unifying universe metavariables to do so. -/ meta def by_proof_irrel : tactic unit := do tgt ← target, @expr.const tt n [level.zero] ← pure tgt.get_app_fn, if n = ``eq then `[apply proof_irrel] else if n = ``heq then `[apply proof_irrel_heq] else failed /-- Same as the `congr` tactic, but takes an optional argument which gives the depth of recursive applications. * This is useful when `congr` is too aggressive in breaking down the goal. * For example, given `⊢ f (g (x + y)) = f (g (y + x))`, `congr'` produces the goals `⊢ x = y` and `⊢ y = x`, while `congr' 2` produces the intended `⊢ x + y = y + x`. * If, at any point, a subgoal matches a hypothesis then the subgoal will be closed. -/ meta def congr' : option ℕ → tactic unit | o := focus1 $ assumption <|> reflexivity transparency.none <|> by_proof_irrel <|> (guard (o ≠ some 0) >> congr_core' >> all_goals' (try (congr' (nat.pred <$> o)))) <|> reflexivity namespace interactive /-- Same as the `congr` tactic, but takes an optional argument which gives the depth of recursive applications. * This is useful when `congr` is too aggressive in breaking down the goal. * For example, given `⊢ f (g (x + y)) = f (g (y + x))`, `congr'` produces the goals `⊢ x = y` and `⊢ y = x`, while `congr' 2` produces the intended `⊢ x + y = y + x`. * If, at any point, a subgoal matches a hypothesis then the subgoal will be closed. * You can use `congr' with p (: n)?` to call `ext p (: n)?` to all subgoals generated by `congr'`. For example, if the goal is `⊢ f '' s = g '' s` then `congr' with x` generates the goal `x : α ⊢ f x = g x`. -/ meta def congr' (n : parse (with_desc "n" small_nat)?) : parse (tk "with" *> prod.mk <$> rintro_patt_parse_hi* <*> (tk ":" *> small_nat)?)? → tactic unit | none := tactic.congr' n | (some ⟨p, m⟩) := focus1 (tactic.congr' n >> all_goals' (tactic.ext p.join m $> ())) /-- Repeatedly and apply `congr'` and `ext`, using the given patterns as arguments for `ext`. There are two ways this tactic stops: * `congr'` fails (makes no progress), after having already applied `ext`. * `congr'` canceled out the last usage of `ext`. In this case, the state is reverted to before the `congr'` was applied. For example, when the goal is ```lean ⊢ (λ x, f x + 3) '' s = (λ x, g x + 3) '' s ``` then `rcongr x` produces the goal ```lean x : α ⊢ f x = g x ``` This gives the same result as `congr', ext x, congr'`. In contrast, `congr'` would produce ```lean ⊢ (λ x, f x + 3) = (λ x, g x + 3) ``` and `congr' with x` (or `congr', ext x`) would produce ```lean x : α ⊢ f x + 3 = g x + 3 ``` -/ meta def rcongr : parse (list.join <$> rintro_patt_parse_hi*) → tactic unit | ps := do t ← target, qs ← try_core (tactic.ext ps none), some () ← try_core (tactic.congr' none >> (done <|> do s ← target, guard $ ¬ s =ₐ t)) | skip, done <|> rcongr (qs.get_or_else ps) add_tactic_doc { name := "congr'", category := doc_category.tactic, decl_names := [`tactic.interactive.congr', `tactic.interactive.congr, `tactic.interactive.rcongr], tags := ["congruence"], inherit_description_from := `tactic.interactive.congr' } /-- The `exact e` and `refine e` tactics require a term `e` whose type is definitionally equal to the goal. `convert e` is similar to `refine e`, but the type of `e` is not required to exactly match the goal. Instead, new goals are created for differences between the type of `e` and the goal. For example, in the proof state ```lean n : ℕ, e : prime (2 * n + 1) ⊢ prime (n + n + 1) ``` the tactic `convert e` will change the goal to ```lean ⊢ n + n = 2 * n ``` In this example, the new goal can be solved using `ring`. The `convert` tactic applies congruence lemmas eagerly before reducing, therefore it can fail in cases where `exact` succeeds: ```lean def p (n : ℕ) := true example (h : p 0) : p 1 := by exact h -- succeeds example (h : p 0) : p 1 := by convert h -- fails, with leftover goal `1 = 0` ``` If `x y : t`, and an instance `subsingleton t` is in scope, then any goals of the form `x = y` are solved automatically. The syntax `convert ← e` will reverse the direction of the new goals (producing `⊢ 2 * n = n + n` in this example). Internally, `convert e` works by creating a new goal asserting that the goal equals the type of `e`, then simplifying it using `congr'`. The syntax `convert e using n` can be used to control the depth of matching (like `congr' n`). In the example, `convert e using 1` would produce a new goal `⊢ n + n + 1 = 2 * n + 1`. -/ meta def convert (sym : parse (with_desc "←" (tk "<-")?)) (r : parse texpr) (n : parse (tk "using" *> small_nat)?) : tactic unit := do tgt ← target, u ← infer_type tgt, r ← i_to_expr ``(%%r : (_ : %%u)), src ← infer_type r, src ← simp_lemmas.mk.dsimplify [] src {fail_if_unchanged := ff}, v ← to_expr (if sym.is_some then ``(%%src = %%tgt) else ``(%%tgt = %%src)) tt ff >>= mk_meta_var, (if sym.is_some then mk_eq_mp v r else mk_eq_mpr v r) >>= tactic.exact, gs ← get_goals, set_goals [v], try (tactic.congr' n), gs' ← get_goals, set_goals $ gs' ++ gs add_tactic_doc { name := "convert", category := doc_category.tactic, decl_names := [`tactic.interactive.convert], tags := ["congruence"] } /-- `convert_to g using n` attempts to change the current goal to `g`, but unlike `change`, it will generate equality proof obligations using `congr' n` to resolve discrepancies. `convert_to g` defaults to using `congr' 1`. `convert_to` is similar to `convert`, but `convert_to` takes a type (the desired subgoal) while `convert` takes a proof term. That is, `convert_to g using n` is equivalent to `convert (_ : g) using n`. -/ meta def convert_to (r : parse texpr) (n : parse (tk "using" *> small_nat)?) : tactic unit := match n with | none := convert_to_core r >> `[congr' 1] | (some 0) := convert_to_core r | (some o) := convert_to_core r >> tactic.congr' o end /-- `ac_change g using n` is `convert_to g using n` followed by `ac_refl`. It is useful for rearranging/reassociating e.g. sums: ```lean example (a b c d e f g N : ℕ) : (a + b) + (c + d) + (e + f) + g ≤ N := begin ac_change a + d + e + f + c + g + b ≤ _, -- ⊢ a + d + e + f + c + g + b ≤ N end ``` ## Related tactic: `move_add` In the case in which the expression to be changed is a sum of terms, tactic `tactive.interactive.move_add` can also be useful. -/ meta def ac_change (r : parse texpr) (n : parse (tk "using" *> small_nat)?) : tactic unit := convert_to r n; try ac_refl add_tactic_doc { name := "convert_to", category := doc_category.tactic, decl_names := [`tactic.interactive.convert_to, `tactic.interactive.ac_change], tags := ["congruence"], inherit_description_from := `tactic.interactive.convert_to } end interactive end tactic
9675966bac9c773b872a25dfb0ec2d93d05bb345
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/topology/algebra/open_subgroup.lean
f52e502c5a076aeea3b3234cde1c707809d94e3c
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,359
lean
/- Copyright (c) 2019 Johan Commelin All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import order.filter.lift import topology.opens import topology.algebra.ring /-! # Open subgroups of a topological groups This files builds the lattice `open_subgroup G` of open subgroups in a topological group `G`, and its additive version `open_add_subgroup`. This lattice has a top element, the subgroup of all elements, but no bottom element in general. The trivial subgroup which is the natural candidate bottom has no reason to be open (this happens only in discrete groups). Note that this notion is especially relevant in a non-archimedean context, for instance for `p`-adic groups. ## Main declarations * `open_subgroup.is_closed`: An open subgroup is automatically closed. * `subgroup.is_open_mono`: A subgroup containing an open subgroup is open. There are also versions for additive groups, submodules and ideals. * `open_subgroup.comap`: Open subgroups can be pulled back by a continuous group morphism. ## TODO * Prove that the identity component of a locally path connected group is an open subgroup. Up to now this file is really geared towards non-archimedean algebra, not Lie groups. -/ open topological_space open_locale topological_space /-- The type of open subgroups of a topological additive group. -/ @[ancestor add_subgroup] structure open_add_subgroup (G : Type*) [add_group G] [topological_space G] extends add_subgroup G := (is_open' : is_open carrier) /-- The type of open subgroups of a topological group. -/ @[ancestor subgroup, to_additive] structure open_subgroup (G : Type*) [group G] [topological_space G] extends subgroup G := (is_open' : is_open carrier) /-- Reinterpret an `open_subgroup` as a `subgroup`. -/ add_decl_doc open_subgroup.to_subgroup /-- Reinterpret an `open_add_subgroup` as an `add_subgroup`. -/ add_decl_doc open_add_subgroup.to_add_subgroup -- Tell Lean that `open_add_subgroup` is a namespace namespace open_add_subgroup end open_add_subgroup namespace open_subgroup open function topological_space variables {G : Type*} [group G] [topological_space G] variables {U V : open_subgroup G} {g : G} @[to_additive] instance has_coe_set : has_coe_t (open_subgroup G) (set G) := ⟨λ U, U.1⟩ @[to_additive] instance : has_mem G (open_subgroup G) := ⟨λ g U, g ∈ (U : set G)⟩ @[to_additive] instance has_coe_subgroup : has_coe_t (open_subgroup G) (subgroup G) := ⟨to_subgroup⟩ @[to_additive] instance has_coe_opens : has_coe_t (open_subgroup G) (opens G) := ⟨λ U, ⟨U, U.is_open'⟩⟩ @[simp, norm_cast, to_additive] lemma mem_coe : g ∈ (U : set G) ↔ g ∈ U := iff.rfl @[simp, norm_cast, to_additive] lemma mem_coe_opens : g ∈ (U : opens G) ↔ g ∈ U := iff.rfl @[simp, norm_cast, to_additive] lemma mem_coe_subgroup : g ∈ (U : subgroup G) ↔ g ∈ U := iff.rfl @[to_additive] lemma coe_injective : injective (coe : open_subgroup G → set G) := by { rintros ⟨⟨⟩⟩ ⟨⟨⟩⟩ ⟨h⟩, congr, } @[ext, to_additive] lemma ext (h : ∀ x, x ∈ U ↔ x ∈ V) : (U = V) := coe_injective $ set.ext h @[to_additive] lemma ext_iff : (U = V) ↔ (∀ x, x ∈ U ↔ x ∈ V) := ⟨λ h x, h ▸ iff.rfl, ext⟩ variable (U) @[to_additive] protected lemma is_open : is_open (U : set G) := U.is_open' @[to_additive] protected lemma one_mem : (1 : G) ∈ U := U.one_mem' @[to_additive] protected lemma inv_mem {g : G} (h : g ∈ U) : g⁻¹ ∈ U := U.inv_mem' h @[to_additive] protected lemma mul_mem {g₁ g₂ : G} (h₁ : g₁ ∈ U) (h₂ : g₂ ∈ U) : g₁ * g₂ ∈ U := U.mul_mem' h₁ h₂ @[to_additive] lemma mem_nhds_one : (U : set G) ∈ 𝓝 (1 : G) := is_open.mem_nhds U.is_open U.one_mem variable {U} @[to_additive] instance : has_top (open_subgroup G) := ⟨{ is_open' := is_open_univ, .. (⊤ : subgroup G) }⟩ @[to_additive] instance : inhabited (open_subgroup G) := ⟨⊤⟩ @[to_additive] lemma is_closed [has_continuous_mul G] (U : open_subgroup G) : is_closed (U : set G) := begin apply is_open_compl_iff.1, refine is_open_iff_forall_mem_open.2 (λ x hx, ⟨(λ y, y * x⁻¹) ⁻¹' U, _, _, _⟩), { intros u hux, simp only [set.mem_preimage, set.mem_compl_iff, mem_coe] at hux hx ⊢, refine mt (λ hu, _) hx, convert U.mul_mem (U.inv_mem hux) hu, simp }, { exact U.is_open.preimage (continuous_mul_right _) }, { simp [U.one_mem] } end section variables {H : Type*} [group H] [topological_space H] /-- The product of two open subgroups as an open subgroup of the product group. -/ @[to_additive "The product of two open subgroups as an open subgroup of the product group."] def prod (U : open_subgroup G) (V : open_subgroup H) : open_subgroup (G × H) := { carrier := (U : set G).prod (V : set H), is_open' := U.is_open.prod V.is_open, .. (U : subgroup G).prod (V : subgroup H) } end @[to_additive] instance : partial_order (open_subgroup G) := { le := λ U V, ∀ ⦃x⦄, x ∈ U → x ∈ V, .. partial_order.lift (coe : open_subgroup G → set G) coe_injective } @[to_additive] instance : semilattice_inf_top (open_subgroup G) := { inf := λ U V, { is_open' := is_open.inter U.is_open V.is_open, .. (U : subgroup G) ⊓ V }, inf_le_left := λ U V, set.inter_subset_left _ _, inf_le_right := λ U V, set.inter_subset_right _ _, le_inf := λ U V W hV hW, set.subset_inter hV hW, top := ⊤, le_top := λ U, set.subset_univ _, ..open_subgroup.partial_order } @[simp, norm_cast, to_additive] lemma coe_inf : (↑(U ⊓ V) : set G) = (U : set G) ∩ V := rfl @[simp, norm_cast, to_additive] lemma coe_subset : (U : set G) ⊆ V ↔ U ≤ V := iff.rfl @[simp, norm_cast, to_additive] lemma coe_subgroup_le : (U : subgroup G) ≤ (V : subgroup G) ↔ U ≤ V := iff.rfl variables {N : Type*} [group N] [topological_space N] /-- The preimage of an `open_subgroup` along a continuous `monoid` homomorphism is an `open_subgroup`. -/ @[to_additive "The preimage of an `open_add_subgroup` along a continuous `add_monoid` homomorphism is an `open_add_subgroup`."] def comap (f : G →* N) (hf : continuous f) (H : open_subgroup N) : open_subgroup G := { is_open' := H.is_open.preimage hf, .. (H : subgroup N).comap f } @[simp, to_additive] lemma coe_comap (H : open_subgroup N) (f : G →* N) (hf : continuous f) : (H.comap f hf : set G) = f ⁻¹' H := rfl @[simp, to_additive] lemma mem_comap {H : open_subgroup N} {f : G →* N} {hf : continuous f} {x : G} : x ∈ H.comap f hf ↔ f x ∈ H := iff.rfl @[to_additive] lemma comap_comap {P : Type*} [group P] [topological_space P] (K : open_subgroup P) (f₂ : N →* P) (hf₂ : continuous f₂) (f₁ : G →* N) (hf₁ : continuous f₁) : (K.comap f₂ hf₂).comap f₁ hf₁ = K.comap (f₂.comp f₁) (hf₂.comp hf₁) := rfl end open_subgroup namespace subgroup variables {G : Type*} [group G] [topological_space G] [has_continuous_mul G] (H : subgroup G) @[to_additive] lemma is_open_of_mem_nhds {g : G} (hg : (H : set G) ∈ 𝓝 g) : is_open (H : set G) := begin simp only [is_open_iff_mem_nhds, set_like.mem_coe] at hg ⊢, intros x hx, have : filter.tendsto (λ y, y * (x⁻¹ * g)) (𝓝 x) (𝓝 $ x * (x⁻¹ * g)) := (continuous_id.mul continuous_const).tendsto _, rw [mul_inv_cancel_left] at this, have := filter.mem_map'.1 (this hg), replace hg : g ∈ H := set_like.mem_coe.1 (mem_of_mem_nhds hg), simp only [set_like.mem_coe, H.mul_mem_cancel_right (H.mul_mem (H.inv_mem hx) hg)] at this, exact this end @[to_additive] lemma is_open_of_open_subgroup {U : open_subgroup G} (h : U.1 ≤ H) : is_open (H : set G) := H.is_open_of_mem_nhds (filter.mem_of_superset U.mem_nhds_one h) @[to_additive] lemma is_open_mono {H₁ H₂ : subgroup G} (h : H₁ ≤ H₂) (h₁ : is_open (H₁ :set G)) : is_open (H₂ : set G) := @is_open_of_open_subgroup _ _ _ _ H₂ { is_open' := h₁, .. H₁ } h end subgroup namespace open_subgroup variables {G : Type*} [group G] [topological_space G] [has_continuous_mul G] @[to_additive] instance : semilattice_sup_top (open_subgroup G) := { sup := λ U V, { is_open' := show is_open (((U : subgroup G) ⊔ V : subgroup G) : set G), from subgroup.is_open_mono le_sup_left U.is_open, .. ((U : subgroup G) ⊔ V) }, le_sup_left := λ U V, coe_subgroup_le.1 le_sup_left, le_sup_right := λ U V, coe_subgroup_le.1 le_sup_right, sup_le := λ U V W hU hV, coe_subgroup_le.1 (sup_le hU hV), ..open_subgroup.semilattice_inf_top } @[to_additive] instance : lattice (open_subgroup G) := { ..open_subgroup.semilattice_sup_top, ..open_subgroup.semilattice_inf_top } end open_subgroup namespace submodule open open_add_subgroup variables {R : Type*} {M : Type*} [comm_ring R] variables [add_comm_group M] [topological_space M] [topological_add_group M] [module R M] lemma is_open_mono {U P : submodule R M} (h : U ≤ P) (hU : is_open (U : set M)) : is_open (P : set M) := @add_subgroup.is_open_mono M _ _ _ U.to_add_subgroup P.to_add_subgroup h hU end submodule namespace ideal variables {R : Type*} [comm_ring R] variables [topological_space R] [topological_ring R] lemma is_open_of_open_subideal {U I : ideal R} (h : U ≤ I) (hU : is_open (U : set R)) : is_open (I : set R) := submodule.is_open_mono h hU end ideal
895af1e0790965aeb70b780fff3dd38e3bae321f
63abd62053d479eae5abf4951554e1064a4c45b4
/src/measure_theory/interval_integral.lean
c2492ce0ee51be7e720ea8f565bdb65df7f62876
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
65,015
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 measure_theory.set_integral import measure_theory.lebesgue_measure import analysis.calculus.deriv /-! # Integral over an interval In this file we define `∫ x in a..b, f x ∂μ` to be `∫ x in Ioc a b, f x ∂μ` if `a ≤ b` and `-∫ x in Ioc b a, f x ∂μ` if `b ≤ a`. We prove a few simple properties and many versions of the first part of the [fundamental theorem of calculus](https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus). Recall that it states that the function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(δu, δv) ↦ δv • f b - δu • f a` at `(a, b)` provided that `f` is continuous at `a` and `b`. ## Main statements ### FTC-1 for Lebesgue measure We prove several versions of FTC-1, all in the `interval_integral` namespace. Many of them follow the naming scheme `integral_has(_strict?)_(f?)deriv(_within?)_at(_of_tendsto_ae?)(_right|_left?)`. They formulate FTC in terms of `has(_strict?)_(f?)deriv(_within?)_at`. Let us explain the meaning of each part of the name: * `_strict` means that the theorem is about strict differentiability; * `f` means that the theorem is about differentiability in both endpoints; incompatible with `_right|_left`; * `_within` means that the theorem is about one-sided derivatives, see below for details; * `_of_tendsto_ae` means that instead of continuity the theorem assumes that `f` has a finite limit almost surely as `x` tends to `a` and/or `b`; * `_right` or `_left` mean that the theorem is about differentiability in the right (resp., left) endpoint. We also reformulate these theorems in terms of `(f?)deriv(_within?)`. These theorems are named `(f?)deriv(_within?)_integral(_of_tendsto_ae?)(_right|_left?)` with the same meaning of parts of the name. ### One-sided derivatives Theorem `integral_has_fderiv_within_at_of_tendsto_ae` states that `(u, v) ↦ ∫ x in u..v, f x` has a derivative `(δu, δv) ↦ δv • cb - δu • ca` within the set `s × t` at `(a, b)` provided that `f` tends to `ca` (resp., `cb`) almost surely at `la` (resp., `lb`), where possible values of `s`, `t`, and corresponding filters `la`, `lb` are given in the following table. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[Iic a] a` | `Iic b` | `𝓝[Iic b] b` | | `Ici a` | `𝓝[Ioi a] a` | `Ici b` | `𝓝[Ioi b] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | We use a typeclass `FTC_filter` to make Lean automatically find `la`/`lb` based on `s`/`t`. This way we can formulate one theorem instead of `16` (or `8` if we leave only non-trivial ones not covered by `integral_has_deriv_within_at_of_tendsto_ae_(left|right)` and `integral_has_fderiv_at_of_tendsto_ae`). Similarly, `integral_has_deriv_within_at_of_tendsto_ae_right` works for both one-sided derivatives using the same typeclass to find an appropriate filter. ### FTC for a locally finite measure Before proving FTC for the Lebesgue measure, we prove a few statements that can be seen as FTC for any measure. The most general of them, `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae`, states the following. Let `(la, la')` be an `FTC_filter` pair of filters around `a` (i.e., `FTC_filter a la la'`) and let `(lb, lb')` be an `FTC_filter` pair of filters around `b`. If `f` has finite limits `ca` and `cb` almost surely at `la'` and `lb'`, respectively, then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ + o(∥∫ x in ua..va, (1:ℝ) ∂μ∥ + ∥∫ x in ub..vb, (1:ℝ) ∂μ∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. ## Implementation notes ### Avoiding `if`, `min`, and `max` In order to avoid `if`s in the definition, we define `interval_integrable f μ a b` as `integrable_on f (Ioc a b) μ ∧ integrable_on f (Ioc b a) μ`. For any `a`, `b` one of these intervals is empty and the other coincides with `Ioc (min a b) (max a b)`. Similarly, we define `∫ x in a..b, f x ∂μ` to be `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ`. Again, for any `a`, `b` one of these integrals is zero, and the other gives the expected result. This way some properties can be translated from integrals over sets without dealing with the cases `a ≤ b` and `b ≤ a` separately. ### Choice of the interval We use integral over `Ioc (min a b) (max a b)` instead of one of the other three possible intervals with the same endpoints for two reasons: * this way `∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ` holds whenever `f` is integrable on each interval; in particular, it works even if the measure `μ` has an atom at `b`; this rules out `Ioo` and `Icc` intervals; * with this definition for a probability measure `μ`, the integral `∫ x in a..b, 1 ∂μ` equals the difference $F_μ(b)-F_μ(a)$, where $F_μ(a)=μ(-∞, a]$ is the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) of `μ`. ### `FTC_filter` class As explained above, many theorems in this file rely on the typeclass `FTC_filter (a : α) (l l' : filter α)` to avoid code duplication. This typeclass combines four assumptions: - `pure a ≤ l`; - `l' ≤ 𝓝 a`; - `l'` has a basis of measurable sets; - if `u n` and `v n` tend to `l`, then for any `s ∈ l'`, `Ioc (u n) (v n)` is eventually included in `s`. This typeclass has exactly four “real” instances: `(a, pure a, ⊥)`, `(a, 𝓝[Ici a] a, 𝓝[Ioi a] a)`, `(a, 𝓝[Iic a] a, 𝓝[Iic a] a)`, `(a, 𝓝 a, 𝓝 a)`, and two instances that are equal to the first and last “real” instances: `(a, 𝓝[{a}] a, ⊥)` and `(a, 𝓝[univ] a, 𝓝[univ] a)`. While the difference between `Ici a` and `Ioi a` doesn't matter for theorems about Lebesgue measure, it becomes important in the versions of FTC about any locally finite measure if this measure has an atom at one of the endpoints. ## Tags integral, fundamental theorem of calculus -/ noncomputable theory open topological_space (second_countable_topology) open measure_theory set classical filter open_locale classical topological_space filter variables {α β 𝕜 E F : Type*} [linear_order α] [measurable_space α] [measurable_space E] [normed_group E] /-! ### Integrability at an interval -/ /-- A function `f` is called *interval integrable* with respect to a measure `μ` on an unordered interval `a..b` if it is integrable on both intervals `(a, b]` and `(b, a]`. One of these intervals is always empty, so this property is equivalent to `f` being integrable on `(min a b, max a b]`. -/ def interval_integrable (f : α → E) (μ : measure α) (a b : α) := integrable_on f (Ioc a b) μ ∧ integrable_on f (Ioc b a) μ lemma measure_theory.integrable.interval_integrable {f : α → E} {μ : measure α} (hf : integrable f μ) {a b : α} : interval_integrable f μ a b := ⟨hf.integrable_on, hf.integrable_on⟩ namespace interval_integrable section variables {f : α → E} {a b c : α} {μ : measure α} @[symm] lemma symm (h : interval_integrable f μ a b) : interval_integrable f μ b a := h.symm @[refl] lemma refl (hf : measurable f) : interval_integrable f μ a a := by split; simp [hf] @[trans] lemma trans (hab : interval_integrable f μ a b) (hbc : interval_integrable f μ b c) : interval_integrable f μ a c := ⟨(hab.1.union hbc.1).mono_set Ioc_subset_Ioc_union_Ioc, (hbc.2.union hab.2).mono_set Ioc_subset_Ioc_union_Ioc⟩ lemma neg [borel_space E] (h : interval_integrable f μ a b) : interval_integrable (-f) μ a b := ⟨h.1.neg, h.2.neg⟩ protected lemma measurable (h : interval_integrable f μ a b) : measurable f := h.1.measurable end variables [borel_space E] {f g : α → E} {a b : α} {μ : measure α} lemma smul [normed_field 𝕜] [normed_space 𝕜 E] {f : α → E} {a b : α} {μ : measure α} (h : interval_integrable f μ a b) (r : 𝕜) : interval_integrable (r • f) μ a b := ⟨h.1.smul r, h.2.smul r⟩ lemma add [second_countable_topology E] (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : interval_integrable (f + g) μ a b := ⟨hf.1.add hg.1, hf.2.add hg.2⟩ lemma sub [second_countable_topology E] (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : interval_integrable (f - g) μ a b := ⟨hf.1.sub hg.1, hf.2.sub hg.2⟩ end interval_integrable /-- Let `l'` be a measurably generated filter; let `l` be a of filter such that each `s ∈ l'` eventually includes `Ioc u v` as both `u` and `v` tend to `l`. Let `μ` be a measure finite at `l'`. Suppose that `f : α → E` has a finite limit at `l' ⊓ μ.ae`. Then `f` is interval integrable on `u..v` provided that both `u` and `v` tend to `l`. Typeclass instances allow Lean to find `l'` based on `l` but not vice versa, so `apply tendsto.eventually_interval_integrable_ae` will generate goals `filter α` and `tendsto_Ixx_class Ioc ?m_1 l'`. -/ lemma filter.tendsto.eventually_interval_integrable_ae {f : α → E} (hfm : measurable f) {μ : measure α} {l l' : filter α} [tendsto_Ixx_class Ioc l l'] [is_measurably_generated l'] (hμ : μ.finite_at_filter l') {c : E} (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) {u v : β → α} {lt : filter β} (hu : tendsto u lt l) (hv : tendsto v lt l) : ∀ᶠ t in lt, interval_integrable f μ (u t) (v t) := have _ := (hf.integrable_at_filter_ae hfm hμ).eventually, ((hu.Ioc hv).eventually this).and $ (hv.Ioc hu).eventually this /-- Let `l'` be a measurably generated filter; let `l` be a of filter such that each `s ∈ l'` eventually includes `Ioc u v` as both `u` and `v` tend to `l`. Let `μ` be a measure finite at `l'`. Suppose that `f : α → E` has a finite limit at `l`. Then `f` is interval integrable on `u..v` provided that both `u` and `v` tend to `l`. Typeclass instances allow Lean to find `l'` based on `l` but not vice versa, so `apply tendsto.eventually_interval_integrable_ae` will generate goals `filter α` and `tendsto_Ixx_class Ioc ?m_1 l'`. -/ lemma filter.tendsto.eventually_interval_integrable {f : α → E} (hfm : measurable f) {μ : measure α} {l l' : filter α} [tendsto_Ixx_class Ioc l l'] [is_measurably_generated l'] (hμ : μ.finite_at_filter l') {c : E} (hf : tendsto f l' (𝓝 c)) {u v : β → α} {lt : filter β} (hu : tendsto u lt l) (hv : tendsto v lt l) : ∀ᶠ t in lt, interval_integrable f μ (u t) (v t) := (hf.mono_left inf_le_left).eventually_interval_integrable_ae hfm hμ hu hv /-! ### Interval integral: definition and basic properties In this section we define `∫ x in a..b, f x ∂μ` as `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ` and prove some basic properties. -/ variables [second_countable_topology E] [complete_space E] [normed_space ℝ E] [borel_space E] /-- The interval integral `∫ x in a..b, f x ∂μ` is defined as `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ`. If `a ≤ b`, then it equals `∫ x in Ioc a b, f x ∂μ`, otherwise it equals `-∫ x in Ioc b a, f x ∂μ`. -/ def interval_integral (f : α → E) (a b : α) (μ : measure α) := ∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ notation `∫` binders ` in ` a `..` b `, ` r:(scoped:60 f, f) ` ∂` μ:70 := interval_integral r a b μ notation `∫` binders ` in ` a `..` b `, ` r:(scoped:60 f, interval_integral f a b volume) := r namespace interval_integral section variables {a b c d : α} {f g : α → E} {μ : measure α} @[simp] lemma integral_zero : ∫ x in a..b, (0 : E) ∂μ = 0 := by simp [interval_integral] lemma integral_of_le (h : a ≤ b) : ∫ x in a..b, f x ∂μ = ∫ x in Ioc a b, f x ∂μ := by simp [interval_integral, h] @[simp] lemma integral_same : ∫ x in a..a, f x ∂μ = 0 := sub_self _ lemma integral_symm (a b) : ∫ x in b..a, f x ∂μ = -∫ x in a..b, f x ∂μ := by simp only [interval_integral, neg_sub] lemma integral_of_ge (h : b ≤ a) : ∫ x in a..b, f x ∂μ = -∫ x in Ioc b a, f x ∂μ := by simp only [integral_symm b, integral_of_le h] lemma integral_cases (f : α → E) (a b) : ∫ x in a..b, f x ∂μ ∈ ({∫ x in Ioc (min a b) (max a b), f x ∂μ, -∫ x in Ioc (min a b) (max a b), f x ∂μ} : set E) := (le_total a b).imp (λ h, by simp [h, integral_of_le]) (λ h, by simp [h, integral_of_ge]) lemma integral_non_measurable {f : α → E} {a b} (hf : ¬measurable f) : ∫ x in a..b, f x ∂μ = 0 := by rw [interval_integral, integral_non_measurable hf, integral_non_measurable hf, sub_zero] lemma norm_integral_eq_norm_integral_Ioc : ∥∫ x in a..b, f x ∂μ∥ = ∥∫ x in Ioc (min a b) (max a b), f x ∂μ∥ := (integral_cases f a b).elim (congr_arg _) (λ h, (congr_arg _ h).trans (norm_neg _)) lemma norm_integral_le_integral_norm_Ioc : ∥∫ x in a..b, f x ∂μ∥ ≤ ∫ x in Ioc (min a b) (max a b), ∥f x∥ ∂μ := calc ∥∫ x in a..b, f x ∂μ∥ = ∥∫ x in Ioc (min a b) (max a b), f x ∂μ∥ : norm_integral_eq_norm_integral_Ioc ... ≤ ∫ x in Ioc (min a b) (max a b), ∥f x∥ ∂μ : norm_integral_le_integral_norm f lemma norm_integral_le_abs_integral_norm : ∥∫ x in a..b, f x ∂μ∥ ≤ abs (∫ x in a..b, ∥f x∥ ∂μ) := begin simp only [← real.norm_eq_abs, norm_integral_eq_norm_integral_Ioc], exact le_trans (norm_integral_le_integral_norm _) (le_abs_self _) end lemma norm_integral_le_of_norm_le_const_ae {a b C : ℝ} {f : ℝ → E} (h : ∀ᵐ x, x ∈ Ioc (min a b) (max a b) → ∥f x∥ ≤ C) : ∥∫ x in a..b, f x∥ ≤ C * abs (b - a) := begin rw [norm_integral_eq_norm_integral_Ioc], convert norm_set_integral_le_of_norm_le_const_ae'' _ is_measurable_Ioc h, { rw [real.volume_Ioc, max_sub_min_eq_abs, ennreal.to_real_of_real (abs_nonneg _)] }, { simp only [real.volume_Ioc, ennreal.of_real_lt_top] }, end lemma norm_integral_le_of_norm_le_const {a b C : ℝ} {f : ℝ → E} (h : ∀ x ∈ Ioc (min a b) (max a b), ∥f x∥ ≤ C) : ∥∫ x in a..b, f x∥ ≤ C * abs (b - a) := norm_integral_le_of_norm_le_const_ae $ eventually_of_forall h lemma integral_add (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : ∫ x in a..b, f x + g x ∂μ = ∫ x in a..b, f x ∂μ + ∫ x in a..b, g x ∂μ := by { simp only [interval_integral, integral_add hf.1 hg.1, integral_add hf.2 hg.2], abel } @[simp] lemma integral_neg : ∫ x in a..b, -f x ∂μ = -∫ x in a..b, f x ∂μ := by { simp only [interval_integral, integral_neg], abel } lemma integral_sub (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : ∫ x in a..b, f x - g x ∂μ = ∫ x in a..b, f x ∂μ - ∫ x in a..b, g x ∂μ := (integral_add hf hg.neg).trans $ congr_arg _ integral_neg lemma integral_smul (r : ℝ) : ∫ x in a..b, r • f x ∂μ = r • ∫ x in a..b, f x ∂μ := by simp only [interval_integral, integral_smul, smul_sub] lemma integral_const' (c : E) : ∫ x in a..b, c ∂μ = ((μ $ Ioc a b).to_real - (μ $ Ioc b a).to_real) • c := by simp only [interval_integral, set_integral_const, sub_smul] lemma integral_const {a b : ℝ} (c : E) : (∫ (x : ℝ) in a..b, c) = (b - a) • c := by simp only [integral_const', real.volume_Ioc, ennreal.to_real_of_real', ← neg_sub b, max_zero_sub_eq_self] lemma integral_smul_measure (c : ennreal) : ∫ x in a..b, f x ∂(c • μ) = c.to_real • ∫ x in a..b, f x ∂μ := by simp only [interval_integral, measure.restrict_smul, integral_smul_measure, smul_sub] lemma integral_comp_add_right (a b c : ℝ) (f : ℝ → E) (hfm : measurable f) : ∫ x in a..b, f (x + c) = ∫ x in a+c..b+c, f x := calc ∫ x in a..b, f (x + c) = ∫ x in a+c..b+c, f x ∂(measure.map (λ x, x + c) volume) : by simp only [interval_integral, set_integral_map is_measurable_Ioc hfm (measurable_add_right _), preimage_add_const_Ioc, add_sub_cancel] ... = ∫ x in a+c..b+c, f x : by rw [real.map_volume_add_right] lemma integral_comp_mul_right {c : ℝ} (hc : 0 < c) (a b : ℝ) (f : ℝ → E) (hfm : measurable f) : ∫ x in a..b, f (x * c) = c⁻¹ • ∫ x in a*c..b*c, f x := begin conv_rhs { rw [← real.smul_map_volume_mul_right (ne_of_gt hc)] }, rw [integral_smul_measure], simp only [interval_integral, set_integral_map is_measurable_Ioc hfm (measurable_mul_right _), hc, preimage_mul_const_Ioc, mul_div_cancel _ (ne_of_gt hc), abs_of_pos, ennreal.to_real_of_real (le_of_lt hc), inv_smul_smul' (ne_of_gt hc)] end lemma integral_comp_neg (a b : ℝ) (f : ℝ → E) (hfm : measurable f) : ∫ x in a..b, f (-x) = ∫ x in -b..-a, f x := begin conv_rhs { rw ← real.map_volume_neg }, simp only [interval_integral, set_integral_map is_measurable_Ioc hfm measurable_neg, neg_preimage, preimage_neg_Ioc, neg_neg, restrict_congr_set Ico_ae_eq_Ioc] end /-! ### Integral is an additive function of the interval In this section we prove that `∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ` as well as a few other identities trivially equivalent to this one. We also prove that `∫ x in a..b, f x ∂μ = ∫ x, f x ∂μ` provided that `support f ⊆ Ioc a b`. -/ variables [topological_space α] [opens_measurable_space α] section order_closed_topology variables [order_closed_topology α] lemma integral_add_adjacent_intervals_cancel (hab : interval_integrable f μ a b) (hbc : interval_integrable f μ b c) : ∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ + ∫ x in c..a, f x ∂μ = 0 := begin have hac := hab.trans hbc, simp only [interval_integral, ← add_sub_comm, sub_eq_zero], iterate 4 { rw ← integral_union }, { suffices : Ioc a b ∪ Ioc b c ∪ Ioc c a = Ioc b a ∪ Ioc c b ∪ Ioc a c, by rw this, rw [Ioc_union_Ioc_union_Ioc_cycle, union_right_comm, Ioc_union_Ioc_union_Ioc_cycle, min_left_comm, max_left_comm] }, all_goals { simp [*, is_measurable.union, is_measurable_Ioc, Ioc_disjoint_Ioc_same, Ioc_disjoint_Ioc_same.symm, hab.1, hab.2, hbc.1, hbc.2, hac.1, hac.2] } end lemma integral_add_adjacent_intervals (hab : interval_integrable f μ a b) (hbc : interval_integrable f μ b c) : ∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ := by rw [← add_neg_eq_zero, ← integral_symm, integral_add_adjacent_intervals_cancel hab hbc] lemma integral_interval_sub_left (hab : interval_integrable f μ a b) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ - ∫ x in a..c, f x ∂μ = ∫ x in c..b, f x ∂μ := sub_eq_of_eq_add' $ eq.symm $ integral_add_adjacent_intervals hac (hac.symm.trans hab) lemma integral_interval_add_interval_comm (hab : interval_integrable f μ a b) (hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ + ∫ x in c..d, f x ∂μ = ∫ x in a..d, f x ∂μ + ∫ x in c..b, f x ∂μ := by rw [← integral_add_adjacent_intervals hac hcd, add_assoc, add_left_comm, integral_add_adjacent_intervals hac (hac.symm.trans hab), add_comm] lemma integral_interval_sub_interval_comm (hab : interval_integrable f μ a b) (hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ - ∫ x in c..d, f x ∂μ = ∫ x in a..c, f x ∂μ - ∫ x in b..d, f x ∂μ := by simp only [sub_eq_add_neg, ← integral_symm, integral_interval_add_interval_comm hab hcd.symm (hac.trans hcd)] lemma integral_interval_sub_interval_comm' (hab : interval_integrable f μ a b) (hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ - ∫ x in c..d, f x ∂μ = ∫ x in d..b, f x ∂μ - ∫ x in c..a, f x ∂μ := by { rw [integral_interval_sub_interval_comm hab hcd hac, integral_symm b d, integral_symm a c, sub_neg_eq_add, sub_eq_neg_add], } lemma integral_Iic_sub_Iic (ha : integrable_on f (Iic a) μ) (hb : integrable_on f (Iic b) μ) : ∫ x in Iic b, f x ∂μ - ∫ x in Iic a, f x ∂μ = ∫ x in a..b, f x ∂μ := begin wlog hab : a ≤ b using [a b] tactic.skip, { rw [sub_eq_iff_eq_add', integral_of_le hab, ← integral_union (Iic_disjoint_Ioc (le_refl _)), Iic_union_Ioc_eq_Iic hab], exacts [is_measurable_Iic, is_measurable_Ioc, ha, hb.mono_set (λ _, and.right)] }, { intros ha hb, rw [integral_symm, ← this hb ha, neg_sub] } end /-- If `μ` is a finite measure then `∫ x in a..b, c ∂μ = (μ (Iic b) - μ (Iic a)) • c`. -/ lemma integral_const_of_cdf [finite_measure μ] (c : E) : ∫ x in a..b, c ∂μ = ((μ (Iic b)).to_real - (μ (Iic a)).to_real) • c := begin simp only [sub_smul, ← set_integral_const], refine (integral_Iic_sub_Iic _ _).symm; simp only [integrable_on_const, measure_lt_top, or_true] end lemma integral_eq_integral_of_support_subset {f : α → E} {a b} (h : function.support f ⊆ Ioc a b) : ∫ x in a..b, f x ∂μ = ∫ x, f x ∂μ := begin by_cases hfm : measurable f, { cases le_total a b with hab hab, { rw [integral_of_le hab, ← integral_indicator hfm is_measurable_Ioc, indicator_of_support_subset h] }, { rw [Ioc_eq_empty hab, subset_empty_iff, function.support_eq_empty_iff] at h, simp [h] } }, { rw [integral_non_measurable hfm, measure_theory.integral_non_measurable hfm] }, end end order_closed_topology end lemma integral_eq_zero_iff_of_le_of_nonneg_ae {f : ℝ → ℝ} {a b : ℝ} (hab : a ≤ b) (hf : 0 ≤ᵐ[volume.restrict (Ioc a b)] f) (hfi : interval_integrable f volume a b) : ∫ x in a..b, f x = 0 ↔ f =ᵐ[volume.restrict (Ioc a b)] 0 := by rw [integral_of_le hab, integral_eq_zero_iff_of_nonneg_ae hf hfi.1] lemma integral_eq_zero_iff_of_nonneg_ae {f : ℝ → ℝ} {a b : ℝ} (hf : 0 ≤ᵐ[volume.restrict (Ioc a b ∪ Ioc b a)] f) (hfi : interval_integrable f volume a b) : ∫ x in a..b, f x = 0 ↔ f =ᵐ[volume.restrict (Ioc a b ∪ Ioc b a)] 0 := begin cases le_total a b with hab hab; simp only [Ioc_eq_empty hab, empty_union, union_empty] at *, { exact integral_eq_zero_iff_of_le_of_nonneg_ae hab hf hfi }, { rw [integral_symm, neg_eq_zero], exact integral_eq_zero_iff_of_le_of_nonneg_ae hab hf hfi.symm } end lemma integral_pos_iff_support_of_nonneg_ae' {f : ℝ → ℝ} {a b : ℝ} (hf : 0 ≤ᵐ[volume.restrict (Ioc a b ∪ Ioc b a)] f) (hfi : interval_integrable f volume a b) : 0 < ∫ x in a..b, f x ↔ a < b ∧ 0 < volume (function.support f ∩ Ioc a b) := begin cases le_total a b with hab hab, { simp only [integral_of_le hab, Ioc_eq_empty hab, union_empty] at hf ⊢, symmetry, rw [set_integral_pos_iff_support_of_nonneg_ae hf hfi.1, and_iff_right_iff_imp], contrapose!, intro h, simp [Ioc_eq_empty h] }, { rw [Ioc_eq_empty hab, empty_union] at hf, simp [integral_of_ge hab, Ioc_eq_empty hab, integral_nonneg_of_ae hf] } end lemma integral_pos_iff_support_of_nonneg_ae {f : ℝ → ℝ} {a b : ℝ} (hf : 0 ≤ᵐ[volume] f) (hfi : interval_integrable f volume a b) : 0 < ∫ x in a..b, f x ↔ a < b ∧ 0 < volume (function.support f ∩ Ioc a b) := integral_pos_iff_support_of_nonneg_ae' (ae_mono measure.restrict_le_self hf) hfi /-! ### Fundamental theorem of calculus, part 1, for any measure In this section we prove a few lemmas that can be seen as versions of FTC-1 for interval integral w.r.t. any measure. Many theorems are formulated for one or two pairs of filters related by `FTC_filter a l l'`. This typeclass has exactly four “real” instances: `(a, pure a, ⊥)`, `(a, 𝓝[Ici a] a, 𝓝[Ioi a] a)`, `(a, 𝓝[Iic a] a, 𝓝[Iic a] a)`, `(a, 𝓝 a, 𝓝 a)`, and two instances that are equal to the first and last “real” instances: `(a, 𝓝[{a}] a, ⊥)` and `(a, 𝓝[univ] a, 𝓝[univ] a)`. We use this approach to avoid repeating arguments in many very similar cases. Lean can automatically find both `a` and `l'` based on `l`. The most general theorem `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae` can be seen as a generalization of lemma `integral_has_strict_fderiv_at` below which states strict differentiability of `∫ x in u..v, f x` in `(u, v)` at `(a, b)` for a measurable function `f` that is integrable on `a..b` and is continuous at `a` and `b`. The lemma is generalized in three directions: first, `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae` deals with any locally finite measure `μ`; second, it works for one-sided limits/derivatives; third, it assumes only that `f` has finite limits almost surely at `a` and `b`. Namely, let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s around `a`; let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has finite limits `ca` and `cb` at `la' ⊓ μ.ae` and `lb' ⊓ μ.ae`, respectively. Then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ + o(∥∫ x in ua..va, (1:ℝ) ∂μ∥ + ∥∫ x in ub..vb, (1:ℝ) ∂μ∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. This theorem is formulated with integral of constants instead of measures in the right hand sides for two reasons: first, this way we avoid `min`/`max` in the statements; second, often it is possible to write better `simp` lemmas for these integrals, see `integral_const` and `integral_const_of_cdf`. In the next subsection we apply this theorem to prove various theorems about differentiability of the integral w.r.t. Lebesgue measure. -/ /-- An auxiliary typeclass for the Fundamental theorem of calculus, part 1. It is used to formulate theorems that work simultaneously for left and right one-sided derivatives of `∫ x in u..v, f x`. There are four instances: `(a, pure a, ⊥)`, `(a, 𝓝[Ici a], 𝓝[Ioi a])`, `(a, 𝓝[Iic a], 𝓝[Iic a])`, and `(a, 𝓝 a, 𝓝 a)`. -/ class FTC_filter {β : Type*} [linear_order β] [measurable_space β] [topological_space β] (a : out_param β) (outer : filter β) (inner : out_param $ filter β) extends tendsto_Ixx_class Ioc outer inner : Prop := (pure_le : pure a ≤ outer) (le_nhds : inner ≤ 𝓝 a) [meas_gen : is_measurably_generated inner] /- The `dangerous_instance` linter doesn't take `out_param`s into account, so it thinks that `FTC_filter.to_tendsto_Ixx_class` is dangerous. Disable this linter using `nolint`. -/ attribute [nolint dangerous_instance] FTC_filter.to_tendsto_Ixx_class namespace FTC_filter variables [linear_order β] [measurable_space β] [topological_space β] instance pure (a : β) : FTC_filter a (pure a) ⊥ := { pure_le := le_refl _, le_nhds := bot_le } instance nhds_within_singleton (a : β) : FTC_filter a (𝓝[{a}] a) ⊥ := by { rw [nhds_within, principal_singleton, inf_eq_right.2 (pure_le_nhds a)], apply_instance } lemma finite_at_inner {a : β} (l : filter β) {l'} [h : FTC_filter a l l'] {μ : measure β} [locally_finite_measure μ] : μ.finite_at_filter l' := (μ.finite_at_nhds a).filter_mono h.le_nhds variables [opens_measurable_space β] [order_topology β] instance nhds (a : β) : FTC_filter a (𝓝 a) (𝓝 a) := { pure_le := pure_le_nhds a, le_nhds := le_refl _ } instance nhds_univ (a : β) : FTC_filter a (𝓝[univ] a) (𝓝 a) := by { rw nhds_within_univ, apply_instance } instance nhds_left (a : β) : FTC_filter a (𝓝[Iic a] a) (𝓝[Iic a] a) := { pure_le := pure_le_nhds_within right_mem_Iic, le_nhds := inf_le_left } instance nhds_right (a : β) : FTC_filter a (𝓝[Ici a] a) (𝓝[Ioi a] a) := { pure_le := pure_le_nhds_within left_mem_Ici, le_nhds := inf_le_left } end FTC_filter open asymptotics section variables {f : α → E} {a b : α} {c ca cb : E} {l l' la la' lb lb' : filter α} {lt : filter β} {μ : measure α} {u v ua va ub vb : β → α} /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, where `μ` is a measure finite at `l'`, then `∫ x in u..v, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, 1 ∂μ)` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae` for a version assuming `[FTC_filter a l l']` and `[locally_finite_measure μ]`. If `l` is one of `𝓝[Ici a] a`, `𝓝[Iic a] a`, `𝓝 a`, then it's easier to apply the non-primed version. The primed version also works, e.g., for `l = l' = at_top`. We use integrals of constants instead of measures because this way it is easier to formulate a statement that works in both cases `u ≤ v` and `v ≤ u`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae' [is_measurably_generated l'] [tendsto_Ixx_class Ioc l l'] (hfm : measurable f) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) : is_o (λ t, ∫ x in u t..v t, f x ∂μ - ∫ x in u t..v t, c ∂μ) (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) lt := begin have A := (hf.integral_sub_linear_is_o_ae hfm hl).comp_tendsto (hu.Ioc hv), have B := (hf.integral_sub_linear_is_o_ae hfm hl).comp_tendsto (hv.Ioc hu), simp only [integral_const'], convert (A.trans_le _).sub (B.trans_le _), { ext t, simp_rw [(∘), interval_integral, sub_smul], abel }, all_goals { intro t, cases le_total (u t) (v t) with huv huv; simp [huv] } end /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`. If `f` has a finite limit `c` at `l ⊓ μ.ae`, where `μ` is a measure finite at `l`, then `∫ x in u..v, f x ∂μ = μ (Ioc u v) • c + o(μ(Ioc u v))` as both `u` and `v` tend to `l` so that `u ≤ v`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_le` for a version assuming `[FTC_filter a l l']` and `[locally_finite_measure μ]`. If `l` is one of `𝓝[Ici a] a`, `𝓝[Iic a] a`, `𝓝 a`, then it's easier to apply the non-primed version. The primed version also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' [is_measurably_generated l'] [tendsto_Ixx_class Ioc l l'] (hfm : measurable f) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : u ≤ᶠ[lt] v) : is_o (λ t, ∫ x in u t..v t, f x ∂μ - (μ (Ioc (u t) (v t))).to_real • c) (λ t, (μ $ Ioc (u t) (v t)).to_real) lt := (measure_integral_sub_linear_is_o_of_tendsto_ae' hfm hf hl hu hv).congr' (huv.mono $ λ x hx, by simp [integral_const', hx]) (huv.mono $ λ x hx, by simp [integral_const', hx]) /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`. If `f` has a finite limit `c` at `l ⊓ μ.ae`, where `μ` is a measure finite at `l`, then `∫ x in u..v, f x ∂μ = -μ (Ioc v u) • c + o(μ(Ioc v u))` as both `u` and `v` tend to `l` so that `v ≤ u`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge` for a version assuming `[FTC_filter a l l']` and `[locally_finite_measure μ]`. If `l` is one of `𝓝[Ici a] a`, `𝓝[Iic a] a`, `𝓝 a`, then it's easier to apply the non-primed version. The primed version also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge' [is_measurably_generated l'] [tendsto_Ixx_class Ioc l l'] (hfm : measurable f) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : v ≤ᶠ[lt] u) : is_o (λ t, ∫ x in u t..v t, f x ∂μ + (μ (Ioc (v t) (u t))).to_real • c) (λ t, (μ $ Ioc (v t) (u t)).to_real) lt := (measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' hfm hf hl hv hu huv).neg_left.congr_left $ λ t, by simp [integral_symm (u t), add_comm] variables [topological_space α] section variables [locally_finite_measure μ] [FTC_filter a l l'] include a local attribute [instance] FTC_filter.meas_gen /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then `∫ x in u..v, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, 1 ∂μ)` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae'` for a version that also works, e.g., for `l = l' = at_top`. We use integrals of constants instead of measures because this way it is easier to formulate a statement that works in both cases `u ≤ v` and `v ≤ u`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae (hfm : measurable f) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) : is_o (λ t, ∫ x in u t..v t, f x ∂μ - ∫ x in u t..v t, c ∂μ) (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) lt := measure_integral_sub_linear_is_o_of_tendsto_ae' hfm hf (FTC_filter.finite_at_inner l) hu hv /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then `∫ x in u..v, f x ∂μ = μ (Ioc u v) • c + o(μ(Ioc u v))` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_le'` for a version that also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_le (hfm : measurable f) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : u ≤ᶠ[lt] v) : is_o (λ t, ∫ x in u t..v t, f x ∂μ - (μ (Ioc (u t) (v t))).to_real • c) (λ t, (μ $ Ioc (u t) (v t)).to_real) lt := measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' hfm hf (FTC_filter.finite_at_inner l) hu hv huv /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then `∫ x in u..v, f x ∂μ = -μ (Ioc v u) • c + o(μ(Ioc v u))` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge'` for a version that also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge (hfm : measurable f) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : v ≤ᶠ[lt] u) : is_o (λ t, ∫ x in u t..v t, f x ∂μ + (μ (Ioc (v t) (u t))).to_real • c) (λ t, (μ $ Ioc (v t) (u t)).to_real) lt := measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge' hfm hf (FTC_filter.finite_at_inner l) hu hv huv end variables [order_topology α] [borel_space α] local attribute [instance] FTC_filter.meas_gen variables [FTC_filter a la la'] [FTC_filter b lb lb'] [locally_finite_measure μ] /-- Fundamental theorem of calculus-1, strict derivative in both limits for a locally finite measure. Let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s around `a`; let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has finite limits `ca` and `cb` at `la' ⊓ μ.ae` and `lb' ⊓ μ.ae`, respectively. Then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ + o(∥∫ x in ua..va, (1:ℝ) ∂μ∥ + ∥∫ x in ub..vb, (1:ℝ) ∂μ∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. -/ lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae (hab : interval_integrable f μ a b) (ha_lim : tendsto f (la' ⊓ μ.ae) (𝓝 ca)) (hb_lim : tendsto f (lb' ⊓ μ.ae) (𝓝 cb)) (hua : tendsto ua lt la) (hva : tendsto va lt la) (hub : tendsto ub lt lb) (hvb : tendsto vb lt lb) : is_o (λ t, (∫ x in va t..vb t, f x ∂μ) - (∫ x in ua t..ub t, f x ∂μ) - (∫ x in ub t..vb t, cb ∂μ - ∫ x in ua t..va t, ca ∂μ)) (λ t, ∥∫ x in ua t..va t, (1:ℝ) ∂μ∥ + ∥∫ x in ub t..vb t, (1:ℝ) ∂μ∥) lt := begin refine ((measure_integral_sub_linear_is_o_of_tendsto_ae hab.measurable ha_lim hua hva).neg_left.add_add (measure_integral_sub_linear_is_o_of_tendsto_ae hab.measurable hb_lim hub hvb)).congr' _ (eventually_eq.refl _ _), have A : ∀ᶠ t in lt, interval_integrable f μ (ua t) (va t) := ha_lim.eventually_interval_integrable_ae hab.measurable (FTC_filter.finite_at_inner la) hua hva, have A' : ∀ᶠ t in lt, interval_integrable f μ a (ua t) := ha_lim.eventually_interval_integrable_ae hab.measurable (FTC_filter.finite_at_inner la) (tendsto_const_pure.mono_right FTC_filter.pure_le) hua, have B : ∀ᶠ t in lt, interval_integrable f μ (ub t) (vb t) := hb_lim.eventually_interval_integrable_ae hab.measurable (FTC_filter.finite_at_inner lb) hub hvb, have B' : ∀ᶠ t in lt, interval_integrable f μ b (ub t) := hb_lim.eventually_interval_integrable_ae hab.measurable (FTC_filter.finite_at_inner lb) (tendsto_const_pure.mono_right FTC_filter.pure_le) hub, filter_upwards [A, A', B, B'], intros t ua_va a_ua ub_vb b_ub, rw [← integral_interval_sub_interval_comm'], { dsimp only [], abel }, exacts [ub_vb, ua_va, b_ub.symm.trans $ hab.symm.trans a_ua] end /-- Fundamental theorem of calculus-1, strict derivative in right endpoint for a locally finite measure. Let `f` be a measurable function integrable on `a..b`. Let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has a finite limit `c` at `lb' ⊓ μ.ae`. Then `∫ x in a..v, f x ∂μ - ∫ x in a..u, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, (1:ℝ) ∂μ)` as `u` and `v` tend to `lb`. -/ lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right (hab : interval_integrable f μ a b) (hf : tendsto f (lb' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt lb) (hv : tendsto v lt lb) : is_o (λ t, ∫ x in a..v t, f x ∂μ - ∫ x in a..u t, f x ∂μ - ∫ x in u t..v t, c ∂μ) (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) lt := by simpa using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab ((tendsto_bot : tendsto _ ⊥ (𝓝 0)).mono_left inf_le_left) hf (tendsto_const_pure : tendsto _ _ (pure a)) tendsto_const_pure hu hv /-- Fundamental theorem of calculus-1, strict derivative in left endpoint for a locally finite measure. Let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s around `a`. Suppose that `f` has a finite limit `c` at `la' ⊓ μ.ae`. Then `∫ x in v..b, f x ∂μ - ∫ x in u..b, f x ∂μ = -∫ x in u..v, c ∂μ + o(∫ x in u..v, (1:ℝ) ∂μ)` as `u` and `v` tend to `la`. -/ lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left (hab : interval_integrable f μ a b) (hf : tendsto f (la' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt la) (hv : tendsto v lt la) : is_o (λ t, ∫ x in v t..b, f x ∂μ - ∫ x in u t..b, f x ∂μ + ∫ x in u t..v t, c ∂μ) (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) lt := by simpa using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab hf ((tendsto_bot : tendsto _ ⊥ (𝓝 0)).mono_left inf_le_left) hu hv (tendsto_const_pure : tendsto _ _ (pure b)) tendsto_const_pure end /-! ### Fundamental theorem of calculus-1 for Lebesgue measure In this section we restate theorems from the previous section for Lebesgue measure. In particular, we prove that `∫ x in u..v, f x` is strictly differentiable in `(u, v)` at `(a, b)` provided that `f` is integrable on `a..b` and is continuous at `a` and `b`. -/ variables {f : ℝ → E} {c ca cb : E} {l l' la la' lb lb' : filter ℝ} {lt : filter β} {a b z : ℝ} {u v ua ub va vb : β → ℝ} [FTC_filter a la la'] [FTC_filter b lb lb'] /-! #### Auxiliary `is_o` statements In this section we prove several lemmas that can be interpreted as strict differentiability of `(u, v) ↦ ∫ x in u..v, f x ∂μ` in `u` and/or `v` at a filter. The statements use `is_o` because we have no definition of `has_strict_(f)deriv_at_filter` in the library. -/ /-- Fundamental theorem of calculus-1, local version. If `f` has a finite limit `c` almost surely at `l'`, where `(l, l')` is an `FTC_filter` pair around `a`, then `∫ x in u..v, f x ∂μ = (v - u) • c + o (v - u)` as both `u` and `v` tend to `l`. -/ lemma integral_sub_linear_is_o_of_tendsto_ae [FTC_filter a l l'] (hfm : measurable f) (hf : tendsto f (l' ⊓ volume.ae) (𝓝 c)) {u v : β → ℝ} (hu : tendsto u lt l) (hv : tendsto v lt l) : is_o (λ t, (∫ x in u t..v t, f x) - (v t - u t) • c) (v - u) lt := by simpa [integral_const] using measure_integral_sub_linear_is_o_of_tendsto_ae hfm hf hu hv /-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints. If `f` is a measurable function integrable on `a..b`, `(la, la')` is an `FTC_filter` pair around `a`, and `(lb, lb')` is an `FTC_filter` pair around `b`, and `f` has finite limits `ca` and `cb` almost surely at `la'` and `lb'`, respectively, then `(∫ x in va..vb, f x) - ∫ x in ua..ub, f x = (vb - ub) • cb - (va - ua) • ca + o(∥va - ua∥ + ∥vb - ub∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. This lemma could've been formulated using `has_strict_fderiv_at_filter` if we had this definition. -/ lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae (hab : interval_integrable f volume a b) (ha_lim : tendsto f (la' ⊓ volume.ae) (𝓝 ca)) (hb_lim : tendsto f (lb' ⊓ volume.ae) (𝓝 cb)) (hua : tendsto ua lt la) (hva : tendsto va lt la) (hub : tendsto ub lt lb) (hvb : tendsto vb lt lb) : is_o (λ t, (∫ x in va t..vb t, f x) - (∫ x in ua t..ub t, f x) - ((vb t - ub t) • cb - (va t - ua t) • ca)) (λ t, ∥va t - ua t∥ + ∥vb t - ub t∥) lt := by simpa [integral_const] using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab ha_lim hb_lim hua hva hub hvb /-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints. If `f` is a measurable function integrable on `a..b`, `(lb, lb')` is an `FTC_filter` pair around `b`, and `f` has a finite limit `c` almost surely at `lb'`, then `(∫ x in a..v, f x) - ∫ x in a..u, f x = (v - u) • c + o(∥v - u∥)` as `u` and `v` tend to `lb`. This lemma could've been formulated using `has_strict_deriv_at_filter` if we had this definition. -/ lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right (hab : interval_integrable f volume a b) (hf : tendsto f (lb' ⊓ volume.ae) (𝓝 c)) (hu : tendsto u lt lb) (hv : tendsto v lt lb) : is_o (λ t, (∫ x in a..v t, f x) - (∫ x in a..u t, f x) - (v t - u t) • c) (v - u) lt := by simpa only [integral_const, smul_eq_mul, mul_one] using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hab hf hu hv /-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints. If `f` is a measurable function integrable on `a..b`, `(la, la')` is an `FTC_filter` pair around `a`, and `f` has a finite limit `c` almost surely at `la'`, then `(∫ x in v..b, f x) - ∫ x in u..b, f x = -(v - u) • c + o(∥v - u∥)` as `u` and `v` tend to `la`. This lemma could've been formulated using `has_strict_deriv_at_filter` if we had this definition. -/ lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left (hab : interval_integrable f volume a b) (hf : tendsto f (la' ⊓ volume.ae) (𝓝 c)) (hu : tendsto u lt la) (hv : tendsto v lt la) : is_o (λ t, (∫ x in v t..b, f x) - (∫ x in u t..b, f x) + (v t - u t) • c) (v - u) lt := by simpa only [integral_const, smul_eq_mul, mul_one] using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left hab hf hu hv open continuous_linear_map (fst snd smul_right sub_apply smul_right_apply coe_fst' coe_snd' map_sub) /-! #### Strict differentiability In this section we prove that for a measurable function `f` integrable on `a..b`, * `integral_has_strict_fderiv_at_of_tendsto_ae`: the function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability provided that `f` tends to `ca` and `cb` almost surely as `x` tendsto to `a` and `b`, respectively; * `integral_has_strict_fderiv_at`: the function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • f b - u • f a` at `(a, b)` in the sense of strict differentiability provided that `f` is continuous at `a` and `b`; * `integral_has_strict_deriv_at_of_tendsto_ae_right`: the function `u ↦ ∫ x in a..u, f x` has derivative `c` at `b` in the sense of strict differentiability provided that `f` tends to `c` almost surely as `x` tends to `b`; * `integral_has_strict_deriv_at_right`: the function `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b` in the sense of strict differentiability provided that `f` is continuous at `b`; * `integral_has_strict_deriv_at_of_tendsto_ae_left`: the function `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a` in the sense of strict differentiability provided that `f` tends to `c` almost surely as `x` tends to `a`; * `integral_has_strict_deriv_at_left`: the function `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a` in the sense of strict differentiability provided that `f` is continuous at `a`. -/ /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability. -/ lemma integral_has_strict_fderiv_at_of_tendsto_ae (hf : interval_integrable f volume a b) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) : has_strict_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (a, b) := begin have := integral_sub_integral_sub_linear_is_o_of_tendsto_ae hf ha hb ((continuous_fst.comp continuous_snd).tendsto ((a, b), (a, b))) ((continuous_fst.comp continuous_fst).tendsto ((a, b), (a, b))) ((continuous_snd.comp continuous_snd).tendsto ((a, b), (a, b))) ((continuous_snd.comp continuous_fst).tendsto ((a, b), (a, b))), refine (this.congr_left _).trans_is_O _, { intro x, simp [sub_smul] }, { exact is_O_fst_prod.norm_left.add is_O_snd_prod.norm_left } end /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a` and `b`, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability. -/ lemma integral_has_strict_fderiv_at (hf : interval_integrable f volume a b) (ha : continuous_at f a) (hb : continuous_at f b) : has_strict_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (a, b) := integral_has_strict_fderiv_at_of_tendsto_ae hf (ha.mono_left inf_le_left) (hb.mono_left inf_le_left) /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `c` at `b` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : has_strict_deriv_at (λ u, ∫ x in a..u, f x) c b := integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hf hb continuous_at_snd continuous_at_fst /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_right (hf : interval_integrable f volume a b) (hb : continuous_at f b) : has_strict_deriv_at (λ u, ∫ x in a..u, f x) (f b) b := integral_has_strict_deriv_at_of_tendsto_ae_right hf (hb.mono_left inf_le_left) /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_of_tendsto_ae_left (hf : interval_integrable f volume a b) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : has_strict_deriv_at (λ u, ∫ x in u..b, f x) (-c) a := by simpa only [← integral_symm] using (integral_has_strict_deriv_at_of_tendsto_ae_right hf.symm ha).neg /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_left (hf : interval_integrable f volume a b) (ha : continuous_at f a) : has_strict_deriv_at (λ u, ∫ x in u..b, f x) (-f a) a := by simpa only [← integral_symm] using (integral_has_strict_deriv_at_right hf.symm ha).neg /-! #### Fréchet differentiability In this subsection we restate results from the previous subsection in terms of `has_fderiv_at`, `has_deriv_at`, `fderiv`, and `deriv`. -/ /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)`. -/ lemma integral_has_fderiv_at_of_tendsto_ae (hf : interval_integrable f volume a b) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) : has_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (a, b) := (integral_has_strict_fderiv_at_of_tendsto_ae hf ha hb).has_fderiv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a` and `b`, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)`. -/ lemma integral_has_fderiv_at (hf : interval_integrable f volume a b) (ha : continuous_at f a) (hb : continuous_at f b) : has_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (a, b) := (integral_has_strict_fderiv_at hf ha hb).has_fderiv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `fderiv` derivative of `(u, v) ↦ ∫ x in u..v, f x` at `(a, b)` equals `(u, v) ↦ v • cb - u • ca`. -/ lemma fderiv_integral_of_tendsto_ae (hf : interval_integrable f volume a b) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) : fderiv ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (a, b) = (snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca := (integral_has_fderiv_at_of_tendsto_ae hf ha hb).fderiv /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a` and `b`, then `fderiv` derivative of `(u, v) ↦ ∫ x in u..v, f x` at `(a, b)` equals `(u, v) ↦ v • cb - u • ca`. -/ lemma fderiv_integral (hf : interval_integrable f volume a b) (ha : continuous_at f a) (hb : continuous_at f b) : fderiv ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (a, b) = (snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a) := (integral_has_fderiv_at hf ha hb).fderiv /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `c` at `b`. -/ lemma integral_has_deriv_at_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : has_deriv_at (λ u, ∫ x in a..u, f x) c b := (integral_has_strict_deriv_at_of_tendsto_ae_right hf hb).has_deriv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b`. -/ lemma integral_has_deriv_at_right (hf : interval_integrable f volume a b) (hb : continuous_at f b) : has_deriv_at (λ u, ∫ x in a..u, f x) (f b) b := (integral_has_strict_deriv_at_right hf hb).has_deriv_at /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` has a finite limit `c` almost surely at `b`, then the derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `c`. -/ lemma deriv_integral_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : deriv (λ u, ∫ x in a..u, f x) b = c := (integral_has_deriv_at_of_tendsto_ae_right hf hb).deriv /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `b`, then the derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `f b`. -/ lemma deriv_integral_right (hf : interval_integrable f volume a b) (hb : continuous_at f b) : deriv (λ u, ∫ x in a..u, f x) b = f b := (integral_has_deriv_at_right hf hb).deriv /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a`. -/ lemma integral_has_deriv_at_of_tendsto_ae_left (hf : interval_integrable f volume a b) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : has_deriv_at (λ u, ∫ x in u..b, f x) (-c) a := (integral_has_strict_deriv_at_of_tendsto_ae_left hf ha).has_deriv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a`. -/ lemma integral_has_deriv_at_left (hf : interval_integrable f volume a b) (ha : continuous_at f a) : has_deriv_at (λ u, ∫ x in u..b, f x) (-f a) a := (integral_has_strict_deriv_at_left hf ha).has_deriv_at /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` has a finite limit `c` almost surely at `a`, then the derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-c`. -/ lemma deriv_integral_of_tendsto_ae_left (hf : interval_integrable f volume a b) (hb : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : deriv (λ u, ∫ x in u..b, f x) a = -c := (integral_has_deriv_at_of_tendsto_ae_left hf hb).deriv /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a`, then the derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-f a`. -/ lemma deriv_integral_left (hf : interval_integrable f volume a b) (hb : continuous_at f a) : deriv (λ u, ∫ x in u..b, f x) a = -f a := (integral_has_deriv_at_left hf hb).deriv /-! #### One-sided derivatives -/ /-- Let `f` be a measurable function integrable on `a..b`. The function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` within `s × t` at `(a, b)`, where `s ∈ {Iic a, {a}, Ici a, univ}` and `t ∈ {Iic b, {b}, Ici b, univ}` provided that `f` tends to `ca` and `cb` almost surely at the filters `la` and `lb` from the following table. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[Iic a] a` | `Iic b` | `𝓝[Iic b] b` | | `Ici a` | `𝓝[Ioi a] a` | `Ici b` | `𝓝[Ioi b] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | -/ lemma integral_has_fderiv_within_at_of_tendsto_ae (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb] (ha : tendsto f (la ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (lb ⊓ volume.ae) (𝓝 cb)) : has_fderiv_within_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (s.prod t) (a, b) := begin rw [has_fderiv_within_at, nhds_within_prod_eq], have := integral_sub_integral_sub_linear_is_o_of_tendsto_ae hf ha hb (tendsto_const_pure.mono_right FTC_filter.pure_le : tendsto _ _ (𝓝[s] a)) tendsto_fst (tendsto_const_pure.mono_right FTC_filter.pure_le : tendsto _ _ (𝓝[t] b)) tendsto_snd, refine (this.congr_left _).trans_is_O _, { intro x, simp [sub_smul] }, { exact is_O_fst_prod.norm_left.add is_O_snd_prod.norm_left } end /-- Let `f` be a measurable function integrable on `a..b`. The function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • f b - u • f a` within `s × t` at `(a, b)`, where `s ∈ {Iic a, {a}, Ici a, univ}` and `t ∈ {Iic b, {b}, Ici b, univ}` provided that `f` tends to `f a` and `f b` at the filters `la` and `lb` from the following table. In most cases this assumption is definitionally equal `continuous_at f _` or `continuous_within_at f _ _`. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[Iic a] a` | `Iic b` | `𝓝[Iic b] b` | | `Ici a` | `𝓝[Ioi a] a` | `Ici b` | `𝓝[Ioi b] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | -/ lemma integral_has_fderiv_within_at (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb] (ha : tendsto f la (𝓝 $ f a)) (hb : tendsto f lb (𝓝 $ f b)) : has_fderiv_within_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (s.prod t) (a, b) := integral_has_fderiv_within_at_of_tendsto_ae hf (ha.mono_left inf_le_left) (hb.mono_left inf_le_left) /-- An auxiliary tactic closing goals `unique_diff_within_at ℝ s a` where `s ∈ {Iic a, Ici a, univ}`. -/ meta def unique_diff_within_at_Ici_Iic_univ : tactic unit := `[apply_rules [unique_diff_on.unique_diff_within_at, unique_diff_on_Ici, unique_diff_on_Iic, left_mem_Ici, right_mem_Iic, unique_diff_within_at_univ]] /-- Let `f` be a measurable function integrable on `a..b`. Choose `s ∈ {Iic a, Ici a, univ}` and `t ∈ {Iic b, Ici b, univ}`. Suppose that `f` tends to `ca` and `cb` almost surely at the filters `la` and `lb` from the table below. Then `fderiv_within ℝ (λ p, ∫ x in p.1..p.2, f x) (s.prod t)` is equal to `(u, v) ↦ u • cb - v • ca`. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[Iic a] a` | `Iic b` | `𝓝[Iic b] b` | | `Ici a` | `𝓝[Ioi a] a` | `Ici b` | `𝓝[Ioi b] b` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | -/ lemma fderiv_within_integral_of_tendsto_ae (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb] (ha : tendsto f (la ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (lb ⊓ volume.ae) (𝓝 cb)) (hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) (ht : unique_diff_within_at ℝ t b . unique_diff_within_at_Ici_Iic_univ) : fderiv_within ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (s.prod t) (a, b) = ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) := (integral_has_fderiv_within_at_of_tendsto_ae hf ha hb).fderiv_within $ hs.prod ht /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `b` from the right or from the left, then `u ↦ ∫ x in a..u, f x` has right (resp., left) derivative `c` at `b`. -/ lemma integral_has_deriv_within_at_of_tendsto_ae_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hb : tendsto f (𝓝[t] b ⊓ volume.ae) (𝓝 c)) : has_deriv_within_at (λ u, ∫ x in a..u, f x) c s b := integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hf hb (tendsto_const_pure.mono_right FTC_filter.pure_le) tendsto_id /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous from the left or from the right at `b`, then `u ↦ ∫ x in a..u, f x` has left (resp., right) derivative `f b` at `b`. -/ lemma integral_has_deriv_within_at_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hb : continuous_within_at f t b) : has_deriv_within_at (λ u, ∫ x in a..u, f x) (f b) s b := integral_has_deriv_within_at_of_tendsto_ae_right hf (hb.mono_left inf_le_left) /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `b` from the right or from the left, then the right (resp., left) derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `c`. -/ lemma deriv_within_integral_of_tendsto_ae_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hb : tendsto f (𝓝[t] b ⊓ volume.ae) (𝓝 c)) (hs : unique_diff_within_at ℝ s b . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in a..u, f x) s b = c := (integral_has_deriv_within_at_of_tendsto_ae_right hf hb).deriv_within hs /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous on the right or on the left at `b`, then the right (resp., left) derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `f b`. -/ lemma deriv_within_integral_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hb : continuous_within_at f t b) (hs : unique_diff_within_at ℝ s b . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in a..u, f x) s b = f b := (integral_has_deriv_within_at_right hf hb).deriv_within hs /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `a` from the right or from the left, then `u ↦ ∫ x in u..b, f x` has right (resp., left) derivative `-c` at `a`. -/ lemma integral_has_deriv_within_at_of_tendsto_ae_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (ha : tendsto f (𝓝[t] a ⊓ volume.ae) (𝓝 c)) : has_deriv_within_at (λ u, ∫ x in u..b, f x) (-c) s a := by { simp only [integral_symm b], exact (integral_has_deriv_within_at_of_tendsto_ae_right hf.symm ha).neg } /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous from the left or from the right at `a`, then `u ↦ ∫ x in u..b, f x` has left (resp., right) derivative `-f a` at `a`. -/ lemma integral_has_deriv_within_at_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (ha : continuous_within_at f t a) : has_deriv_within_at (λ u, ∫ x in u..b, f x) (-f a) s a := integral_has_deriv_within_at_of_tendsto_ae_left hf (ha.mono_left inf_le_left) /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `a` from the right or from the left, then the right (resp., left) derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-c`. -/ lemma deriv_within_integral_of_tendsto_ae_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (ha : tendsto f (𝓝[t] a ⊓ volume.ae) (𝓝 c)) (hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in u..b, f x) s a = -c := (integral_has_deriv_within_at_of_tendsto_ae_left hf ha).deriv_within hs /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous on the right or on the left at `a`, then the right (resp., left) derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-f a`. -/ lemma deriv_within_integral_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (ha : continuous_within_at f t a) (hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in u..b, f x) s a = -f a := (integral_has_deriv_within_at_left hf ha).deriv_within hs end interval_integral
1c3645cbdc5ca5de19d4e10a6de23d75611283e7
07f5f86b00fed90a419ccda4298d8b795a68f657
/library/init/data/nat/lemmas.lean
8b77c4eea73731e15a7c6c6490eaeba5722c86fa
[ "Apache-2.0" ]
permissive
VBaratham/lean
8ec5c3167b4835cfbcd7f25e2173d61ad9416b3a
450ca5834c1c35318e4b47d553bb9820c1b3eee7
refs/heads/master
1,629,649,471,814
1,512,060,373,000
1,512,060,469,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
50,359
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad -/ prelude import init.data.nat.basic init.data.nat.div init.data.nat.pow init.meta init.algebra.functions universes u namespace nat attribute [pre_smt] nat_zero_eq_zero protected lemma zero_add : ∀ n : ℕ, 0 + n = n | 0 := rfl | (n+1) := congr_arg succ (zero_add n) lemma succ_add : ∀ n m : ℕ, (succ n) + m = succ (n + m) | n 0 := rfl | n (m+1) := congr_arg succ (succ_add n m) lemma add_succ (n m : ℕ) : n + succ m = succ (n + m) := rfl protected lemma add_zero (n : ℕ) : n + 0 = n := rfl lemma add_one (n : ℕ) : n + 1 = succ n := rfl lemma succ_eq_add_one (n : ℕ) : succ n = n + 1 := rfl protected lemma add_comm : ∀ n m : ℕ, n + m = m + n | n 0 := eq.symm (nat.zero_add n) | n (m+1) := suffices succ (n + m) = succ (m + n), from eq.symm (succ_add m n) ▸ this, congr_arg succ (add_comm n m) protected lemma add_assoc : ∀ n m k : ℕ, (n + m) + k = n + (m + k) | n m 0 := rfl | n m (succ k) := by rw [add_succ, add_succ, add_assoc] protected lemma add_left_comm : ∀ (n m k : ℕ), n + (m + k) = m + (n + k) := left_comm nat.add nat.add_comm nat.add_assoc protected lemma add_left_cancel : ∀ {n m k : ℕ}, n + m = n + k → m = k | 0 m k := by simp [nat.zero_add] {contextual := tt} | (succ n) m k := λ h, have n+m = n+k, by simp [succ_add] at h; injection h, add_left_cancel this protected lemma add_right_cancel {n m k : ℕ} (h : n + m = k + m) : n = k := have m + n = m + k, by rwa [nat.add_comm n m, nat.add_comm k m] at h, nat.add_left_cancel this lemma succ_ne_zero (n : ℕ) : succ n ≠ 0 := assume h, nat.no_confusion h lemma succ_ne_self : ∀ n : ℕ, succ n ≠ n | 0 h := absurd h (nat.succ_ne_zero 0) | (n+1) h := succ_ne_self n (nat.no_confusion h (λ h, h)) protected lemma one_ne_zero : 1 ≠ (0 : ℕ) := assume h, nat.no_confusion h protected lemma zero_ne_one : 0 ≠ (1 : ℕ) := assume h, nat.no_confusion h instance : zero_ne_one_class ℕ := { zero := 0, one := 1, zero_ne_one := nat.zero_ne_one } lemma eq_zero_of_add_eq_zero_right : ∀ {n m : ℕ}, n + m = 0 → n = 0 | 0 m := by simp [nat.zero_add] | (n+1) m := λ h, begin exfalso, rw [add_one, succ_add] at h, apply succ_ne_zero _ h end lemma eq_zero_of_add_eq_zero_left {n m : ℕ} (h : n + m = 0) : m = 0 := @eq_zero_of_add_eq_zero_right m n (nat.add_comm n m ▸ h) @[simp] lemma pred_zero : pred 0 = 0 := rfl @[simp] lemma pred_succ (n : ℕ) : pred (succ n) = n := rfl protected lemma mul_zero (n : ℕ) : n * 0 = 0 := rfl lemma mul_succ (n m : ℕ) : n * succ m = n * m + n := rfl protected theorem zero_mul : ∀ (n : ℕ), 0 * n = 0 | 0 := rfl | (succ n) := by rw [mul_succ, zero_mul] private meta def sort_add := `[simp [nat.add_assoc, nat.add_comm, nat.add_left_comm]] lemma succ_mul : ∀ (n m : ℕ), (succ n) * m = (n * m) + m | n 0 := rfl | n (succ m) := begin simp [mul_succ, add_succ, succ_mul n m], sort_add end protected lemma right_distrib : ∀ (n m k : ℕ), (n + m) * k = n * k + m * k | n m 0 := rfl | n m (succ k) := begin simp [mul_succ, right_distrib n m k], sort_add end protected lemma left_distrib : ∀ (n m k : ℕ), n * (m + k) = n * m + n * k | 0 m k := by simp [nat.zero_mul] | (succ n) m k := begin simp [succ_mul, left_distrib n m k], sort_add end protected lemma mul_comm : ∀ (n m : ℕ), n * m = m * n | n 0 := by rw [nat.zero_mul, nat.mul_zero] | n (succ m) := by simp [mul_succ, succ_mul, mul_comm n m] protected lemma mul_assoc : ∀ (n m k : ℕ), (n * m) * k = n * (m * k) | n m 0 := rfl | n m (succ k) := by simp [mul_succ, nat.left_distrib, mul_assoc n m k] protected lemma mul_one : ∀ (n : ℕ), n * 1 = n := nat.zero_add protected lemma one_mul (n : ℕ) : 1 * n = n := by rw [nat.mul_comm, nat.mul_one] instance : comm_semiring nat := {add := nat.add, add_assoc := nat.add_assoc, zero := nat.zero, zero_add := nat.zero_add, add_zero := nat.add_zero, add_comm := nat.add_comm, mul := nat.mul, mul_assoc := nat.mul_assoc, one := nat.succ nat.zero, one_mul := nat.one_mul, mul_one := nat.mul_one, left_distrib := nat.left_distrib, right_distrib := nat.right_distrib, zero_mul := nat.zero_mul, mul_zero := nat.mul_zero, mul_comm := nat.mul_comm} /- properties of inequality -/ protected lemma le_of_eq {n m : ℕ} (p : n = m) : n ≤ m := p ▸ less_than_or_equal.refl n lemma le_succ_of_le {n m : ℕ} (h : n ≤ m) : n ≤ succ m := nat.le_trans h (le_succ m) lemma le_of_succ_le {n m : ℕ} (h : succ n ≤ m) : n ≤ m := nat.le_trans (le_succ n) h protected lemma le_of_lt {n m : ℕ} (h : n < m) : n ≤ m := le_of_succ_le h def lt.step {n m : ℕ} : n < m → n < succ m := less_than_or_equal.step lemma eq_zero_or_pos (n : ℕ) : n = 0 ∨ n > 0 := by {cases n, exact or.inl rfl, exact or.inr (succ_pos _)} protected lemma pos_of_ne_zero {n : nat} : n ≠ 0 → n > 0 := or.resolve_left (eq_zero_or_pos n) protected lemma lt_trans {n m k : ℕ} (h₁ : n < m) : m < k → n < k := nat.le_trans (less_than_or_equal.step h₁) protected lemma lt_of_le_of_lt {n m k : ℕ} (h₁ : n ≤ m) : m < k → n < k := nat.le_trans (succ_le_succ h₁) def lt.base (n : ℕ) : n < succ n := nat.le_refl (succ n) lemma lt_succ_self (n : ℕ) : n < succ n := lt.base n protected lemma le_antisymm {n m : ℕ} (h₁ : n ≤ m) : m ≤ n → n = m := less_than_or_equal.cases_on h₁ (λ a, rfl) (λ a b c, absurd (nat.lt_of_le_of_lt b c) (nat.lt_irrefl n)) protected lemma lt_or_ge : ∀ (a b : ℕ), a < b ∨ a ≥ b | a 0 := or.inr (zero_le a) | a (b+1) := match lt_or_ge a b with | or.inl h := or.inl (le_succ_of_le h) | or.inr h := match nat.eq_or_lt_of_le h with | or.inl h1 := or.inl (h1 ▸ lt_succ_self b) | or.inr h1 := or.inr h1 end end protected lemma le_total {m n : ℕ} : m ≤ n ∨ n ≤ m := or.imp_left nat.le_of_lt (nat.lt_or_ge m n) protected lemma lt_of_le_and_ne {m n : ℕ} (h1 : m ≤ n) : m ≠ n → m < n := or.resolve_right (or.swap (nat.eq_or_lt_of_le h1)) protected lemma lt_iff_le_not_le {m n : ℕ} : m < n ↔ (m ≤ n ∧ ¬ n ≤ m) := ⟨λ hmn, ⟨nat.le_of_lt hmn, λ hnm, nat.lt_irrefl _ (nat.lt_of_le_of_lt hnm hmn)⟩, λ ⟨hmn, hnm⟩, nat.lt_of_le_and_ne hmn (λ heq, hnm (heq ▸ nat.le_refl _))⟩ instance : linear_order ℕ := { le := nat.less_than_or_equal, le_refl := @nat.le_refl, le_trans := @nat.le_trans, le_antisymm := @nat.le_antisymm, le_total := @nat.le_total, lt := nat.lt, lt_iff_le_not_le := @nat.lt_iff_le_not_le } lemma eq_zero_of_le_zero {n : nat} (h : n ≤ 0) : n = 0 := le_antisymm h (zero_le _) lemma succ_lt_succ {a b : ℕ} : a < b → succ a < succ b := succ_le_succ lemma lt_of_succ_lt {a b : ℕ} : succ a < b → a < b := le_of_succ_le lemma lt_of_succ_lt_succ {a b : ℕ} : succ a < succ b → a < b := le_of_succ_le_succ lemma pred_lt_pred : ∀ {n m : ℕ}, n ≠ 0 → n < m → pred n < pred m | 0 _ h₁ h := absurd rfl h₁ | _ 0 h₁ h := absurd h (not_lt_zero _) | (succ n) (succ m) _ h := lt_of_succ_lt_succ h lemma lt_of_succ_le {a b : ℕ} (h : succ a ≤ b) : a < b := h lemma succ_le_of_lt {a b : ℕ} (h : a < b) : succ a ≤ b := h lemma le_add_right : ∀ (n k : ℕ), n ≤ n + k | n 0 := nat.le_refl n | n (k+1) := le_succ_of_le (le_add_right n k) lemma le_add_left (n m : ℕ): n ≤ m + n := nat.add_comm n m ▸ le_add_right n m lemma le.dest : ∀ {n m : ℕ}, n ≤ m → ∃ k, n + k = m | n ._ (less_than_or_equal.refl ._) := ⟨0, rfl⟩ | n ._ (@less_than_or_equal.step ._ m h) := match le.dest h with | ⟨w, hw⟩ := ⟨succ w, hw ▸ add_succ n w⟩ end lemma le.intro {n m k : ℕ} (h : n + k = m) : n ≤ m := h ▸ le_add_right n k protected lemma add_le_add_left {n m : ℕ} (h : n ≤ m) (k : ℕ) : k + n ≤ k + m := match le.dest h with | ⟨w, hw⟩ := @le.intro _ _ w begin rw [nat.add_assoc, hw] end end protected lemma add_le_add_right {n m : ℕ} (h : n ≤ m) (k : ℕ) : n + k ≤ m + k := begin rw [nat.add_comm n k, nat.add_comm m k], apply nat.add_le_add_left h end protected lemma le_of_add_le_add_left {k n m : ℕ} (h : k + n ≤ k + m) : n ≤ m := match le.dest h with | ⟨w, hw⟩ := @le.intro _ _ w begin rw [nat.add_assoc] at hw, apply nat.add_left_cancel hw end end protected lemma le_of_add_le_add_right {k n m : ℕ} : n + k ≤ m + k → n ≤ m := begin rw [nat.add_comm _ k, nat.add_comm _ k], apply nat.le_of_add_le_add_left end protected lemma add_le_add_iff_le_right (k n m : ℕ) : n + k ≤ m + k ↔ n ≤ m := ⟨ nat.le_of_add_le_add_right , assume h, nat.add_le_add_right h _ ⟩ protected theorem lt_of_add_lt_add_left {k n m : ℕ} (h : k + n < k + m) : n < m := let h' := nat.le_of_lt h in nat.lt_of_le_and_ne (nat.le_of_add_le_add_left h') (λ heq, nat.lt_irrefl (k + m) begin rw heq at h, assumption end) protected lemma add_lt_add_left {n m : ℕ} (h : n < m) (k : ℕ) : k + n < k + m := lt_of_succ_le (add_succ k n ▸ nat.add_le_add_left (succ_le_of_lt h) k) protected lemma add_lt_add_right {n m : ℕ} (h : n < m) (k : ℕ) : n + k < m + k := nat.add_comm k m ▸ nat.add_comm k n ▸ nat.add_lt_add_left h k protected lemma lt_add_of_pos_right {n k : ℕ} (h : k > 0) : n < n + k := nat.add_lt_add_left h n protected lemma lt_add_of_pos_left {n k : ℕ} (h : k > 0) : n < k + n := by rw add_comm; exact nat.lt_add_of_pos_right h protected lemma zero_lt_one : 0 < (1:nat) := zero_lt_succ 0 lemma mul_le_mul_left {n m : ℕ} (k : ℕ) (h : n ≤ m) : k * n ≤ k * m := match le.dest h with | ⟨l, hl⟩ := have k * n + k * l = k * m, by rw [← left_distrib, hl], le.intro this end lemma mul_le_mul_right {n m : ℕ} (k : ℕ) (h : n ≤ m) : n * k ≤ m * k := mul_comm k m ▸ mul_comm k n ▸ mul_le_mul_left k h protected lemma mul_lt_mul_of_pos_left {n m k : ℕ} (h : n < m) (hk : k > 0) : k * n < k * m := nat.lt_of_lt_of_le (nat.lt_add_of_pos_right hk) (mul_succ k n ▸ nat.mul_le_mul_left k (succ_le_of_lt h)) protected lemma mul_lt_mul_of_pos_right {n m k : ℕ} (h : n < m) (hk : k > 0) : n * k < m * k := mul_comm k m ▸ mul_comm k n ▸ nat.mul_lt_mul_of_pos_left h hk instance : decidable_linear_ordered_semiring nat := { add_left_cancel := @nat.add_left_cancel, add_right_cancel := @nat.add_right_cancel, lt := nat.lt, le := nat.le, le_refl := nat.le_refl, le_trans := @nat.le_trans, le_antisymm := @nat.le_antisymm, le_total := @nat.le_total, lt_iff_le_not_le := @lt_iff_le_not_le _ _, add_le_add_left := @nat.add_le_add_left, le_of_add_le_add_left := @nat.le_of_add_le_add_left, zero_lt_one := zero_lt_succ 0, mul_le_mul_of_nonneg_left := assume a b c h₁ h₂, nat.mul_le_mul_left c h₁, mul_le_mul_of_nonneg_right := assume a b c h₁ h₂, nat.mul_le_mul_right c h₁, mul_lt_mul_of_pos_left := @nat.mul_lt_mul_of_pos_left, mul_lt_mul_of_pos_right := @nat.mul_lt_mul_of_pos_right, decidable_lt := nat.decidable_lt, decidable_le := nat.decidable_le, decidable_eq := nat.decidable_eq, ..nat.comm_semiring } -- all the fields are already included in the decidable_linear_ordered_semiring instance instance : decidable_linear_ordered_cancel_comm_monoid ℕ := { add_left_cancel := @nat.add_left_cancel, ..nat.decidable_linear_ordered_semiring } lemma le_of_lt_succ {m n : nat} : m < succ n → m ≤ n := le_of_succ_le_succ theorem eq_of_mul_eq_mul_left {m k n : ℕ} (Hn : n > 0) (H : n * m = n * k) : m = k := le_antisymm (le_of_mul_le_mul_left (le_of_eq H) Hn) (le_of_mul_le_mul_left (le_of_eq H.symm) Hn) theorem eq_of_mul_eq_mul_right {n m k : ℕ} (Hm : m > 0) (H : n * m = k * m) : n = k := by rw [mul_comm n m, mul_comm k m] at H; exact eq_of_mul_eq_mul_left Hm H /- sub properties -/ @[simp] protected lemma zero_sub : ∀ a : ℕ, 0 - a = 0 | 0 := rfl | (a+1) := congr_arg pred (zero_sub a) lemma sub_lt_succ (a b : ℕ) : a - b < succ a := lt_succ_of_le (sub_le a b) protected theorem sub_le_sub_right {n m : ℕ} (h : n ≤ m) : ∀ k, n - k ≤ m - k | 0 := h | (succ z) := pred_le_pred (sub_le_sub_right z) /- bit0/bit1 properties -/ protected lemma bit0_succ_eq (n : ℕ) : bit0 (succ n) = succ (succ (bit0 n)) := show succ (succ n + n) = succ (succ (n + n)), from congr_arg succ (succ_add n n) protected lemma bit1_eq_succ_bit0 (n : ℕ) : bit1 n = succ (bit0 n) := rfl protected lemma bit1_succ_eq (n : ℕ) : bit1 (succ n) = succ (succ (bit1 n)) := eq.trans (nat.bit1_eq_succ_bit0 (succ n)) (congr_arg succ (nat.bit0_succ_eq n)) protected lemma bit0_ne_zero : ∀ {n : ℕ}, n ≠ 0 → bit0 n ≠ 0 | 0 h := absurd rfl h | (n+1) h := succ_ne_zero _ protected lemma bit1_ne_zero (n : ℕ) : bit1 n ≠ 0 := show succ (n + n) ≠ 0, from succ_ne_zero (n + n) protected lemma bit1_ne_one : ∀ {n : ℕ}, n ≠ 0 → bit1 n ≠ 1 | 0 h h1 := absurd rfl h | (n+1) h h1 := nat.no_confusion h1 (λ h2, absurd h2 (succ_ne_zero _)) protected lemma bit0_ne_one : ∀ n : ℕ, bit0 n ≠ 1 | 0 h := absurd h (ne.symm nat.one_ne_zero) | (n+1) h := have h1 : succ (succ (n + n)) = 1, from succ_add n n ▸ h, nat.no_confusion h1 (λ h2, absurd h2 (succ_ne_zero (n + n))) protected lemma add_self_ne_one : ∀ (n : ℕ), n + n ≠ 1 | 0 h := nat.no_confusion h | (n+1) h := have h1 : succ (succ (n + n)) = 1, from succ_add n n ▸ h, nat.no_confusion h1 (λ h2, absurd h2 (nat.succ_ne_zero (n + n))) protected lemma bit1_ne_bit0 : ∀ (n m : ℕ), bit1 n ≠ bit0 m | 0 m h := absurd h (ne.symm (nat.add_self_ne_one m)) | (n+1) 0 h := have h1 : succ (bit0 (succ n)) = 0, from h, absurd h1 (nat.succ_ne_zero _) | (n+1) (m+1) h := have h1 : succ (succ (bit1 n)) = succ (succ (bit0 m)), from nat.bit0_succ_eq m ▸ nat.bit1_succ_eq n ▸ h, have h2 : bit1 n = bit0 m, from nat.no_confusion h1 (λ h2', nat.no_confusion h2' (λ h2'', h2'')), absurd h2 (bit1_ne_bit0 n m) protected lemma bit0_ne_bit1 : ∀ (n m : ℕ), bit0 n ≠ bit1 m := λ n m : nat, ne.symm (nat.bit1_ne_bit0 m n) protected lemma bit0_inj : ∀ {n m : ℕ}, bit0 n = bit0 m → n = m | 0 0 h := rfl | 0 (m+1) h := by contradiction | (n+1) 0 h := by contradiction | (n+1) (m+1) h := have succ (succ (n + n)) = succ (succ (m + m)), begin unfold bit0 at h, simp [add_one, add_succ, succ_add] at h, exact h end, have n + n = m + m, by repeat {injection this with this}, have n = m, from bit0_inj this, by rw this protected lemma bit1_inj : ∀ {n m : ℕ}, bit1 n = bit1 m → n = m := λ n m h, have succ (bit0 n) = succ (bit0 m), begin simp [nat.bit1_eq_succ_bit0] at h, assumption end, have bit0 n = bit0 m, by injection this, nat.bit0_inj this protected lemma bit0_ne {n m : ℕ} : n ≠ m → bit0 n ≠ bit0 m := λ h₁ h₂, absurd (nat.bit0_inj h₂) h₁ protected lemma bit1_ne {n m : ℕ} : n ≠ m → bit1 n ≠ bit1 m := λ h₁ h₂, absurd (nat.bit1_inj h₂) h₁ protected lemma zero_ne_bit0 {n : ℕ} : n ≠ 0 → 0 ≠ bit0 n := λ h, ne.symm (nat.bit0_ne_zero h) protected lemma zero_ne_bit1 (n : ℕ) : 0 ≠ bit1 n := ne.symm (nat.bit1_ne_zero n) protected lemma one_ne_bit0 (n : ℕ) : 1 ≠ bit0 n := ne.symm (nat.bit0_ne_one n) protected lemma one_ne_bit1 {n : ℕ} : n ≠ 0 → 1 ≠ bit1 n := λ h, ne.symm (nat.bit1_ne_one h) protected lemma zero_lt_bit1 (n : nat) : 0 < bit1 n := zero_lt_succ _ protected lemma zero_lt_bit0 : ∀ {n : nat}, n ≠ 0 → 0 < bit0 n | 0 h := by contradiction | (succ n) h := begin rw nat.bit0_succ_eq, apply zero_lt_succ end protected lemma one_lt_bit1 : ∀ {n : nat}, n ≠ 0 → 1 < bit1 n | 0 h := by contradiction | (succ n) h := begin rw nat.bit1_succ_eq, apply succ_lt_succ, apply zero_lt_succ end protected lemma one_lt_bit0 : ∀ {n : nat}, n ≠ 0 → 1 < bit0 n | 0 h := by contradiction | (succ n) h := begin rw nat.bit0_succ_eq, apply succ_lt_succ, apply zero_lt_succ end protected lemma bit0_lt {n m : nat} (h : n < m) : bit0 n < bit0 m := add_lt_add h h protected lemma bit1_lt {n m : nat} (h : n < m) : bit1 n < bit1 m := succ_lt_succ (add_lt_add h h) protected lemma bit0_lt_bit1 {n m : nat} (h : n ≤ m) : bit0 n < bit1 m := lt_succ_of_le (add_le_add h h) protected lemma bit1_lt_bit0 : ∀ {n m : nat}, n < m → bit1 n < bit0 m | n 0 h := absurd h (not_lt_zero _) | n (succ m) h := have n ≤ m, from le_of_lt_succ h, have succ (n + n) ≤ succ (m + m), from succ_le_succ (add_le_add this this), have succ (n + n) ≤ succ m + m, {rw succ_add, assumption}, show succ (n + n) < succ (succ m + m), from lt_succ_of_le this protected lemma one_le_bit1 (n : ℕ) : 1 ≤ bit1 n := show 1 ≤ succ (bit0 n), from succ_le_succ (zero_le (bit0 n)) protected lemma one_le_bit0 : ∀ (n : ℕ), n ≠ 0 → 1 ≤ bit0 n | 0 h := absurd rfl h | (n+1) h := suffices 1 ≤ succ (succ (bit0 n)), from eq.symm (nat.bit0_succ_eq n) ▸ this, succ_le_succ (zero_le (succ (bit0 n))) /- Extra instances to short-circuit type class resolution -/ instance : add_comm_monoid nat := by apply_instance instance : add_monoid nat := by apply_instance instance : monoid nat := by apply_instance instance : comm_monoid nat := by apply_instance instance : comm_semigroup nat := by apply_instance instance : semigroup nat := by apply_instance instance : add_comm_semigroup nat := by apply_instance instance : add_semigroup nat := by apply_instance instance : distrib nat := by apply_instance instance : semiring nat := by apply_instance instance : ordered_semiring nat := by apply_instance /- subtraction -/ @[simp] protected theorem sub_zero (n : ℕ) : n - 0 = n := rfl theorem sub_succ (n m : ℕ) : n - succ m = pred (n - m) := rfl theorem succ_sub_succ (n m : ℕ) : succ n - succ m = n - m := succ_sub_succ_eq_sub n m protected theorem sub_self : ∀ (n : ℕ), n - n = 0 | 0 := by rw nat.sub_zero | (succ n) := by rw [succ_sub_succ, sub_self n] /- TODO(Leo): remove the following ematch annotations as soon as we have arithmetic theory in the smt_stactic -/ @[ematch_lhs] protected theorem add_sub_add_right : ∀ (n k m : ℕ), (n + k) - (m + k) = n - m | n 0 m := by rw [add_zero, add_zero] | n (succ k) m := by rw [add_succ, add_succ, succ_sub_succ, add_sub_add_right n k m] @[ematch_lhs] protected theorem add_sub_add_left (k n m : ℕ) : (k + n) - (k + m) = n - m := by rw [add_comm k n, add_comm k m, nat.add_sub_add_right] @[ematch_lhs] protected theorem add_sub_cancel (n m : ℕ) : n + m - m = n := suffices n + m - (0 + m) = n, from by rwa [zero_add] at this, by rw [nat.add_sub_add_right, nat.sub_zero] @[ematch_lhs] protected theorem add_sub_cancel_left (n m : ℕ) : n + m - n = m := show n + m - (n + 0) = m, from by rw [nat.add_sub_add_left, nat.sub_zero] protected theorem sub_sub : ∀ (n m k : ℕ), n - m - k = n - (m + k) | n m 0 := by rw [add_zero, nat.sub_zero] | n m (succ k) := by rw [add_succ, nat.sub_succ, nat.sub_succ, sub_sub n m k] theorem le_of_le_of_sub_le_sub_right {n m k : ℕ} (h₀ : k ≤ m) (h₁ : n - k ≤ m - k) : n ≤ m := begin revert k m, induction n with n ; intros k m h₀ h₁, { apply zero_le }, { cases k with k, { apply h₁ }, cases m with m, { cases not_succ_le_zero _ h₀ }, { simp [succ_sub_succ] at h₁, apply succ_le_succ, apply ih_1 _ h₁, apply le_of_succ_le_succ h₀ }, } end protected theorem sub_le_sub_right_iff (n m k : ℕ) (h : k ≤ m) : n - k ≤ m - k ↔ n ≤ m := ⟨ le_of_le_of_sub_le_sub_right h , assume h, nat.sub_le_sub_right h k ⟩ theorem sub_self_add (n m : ℕ) : n - (n + m) = 0 := show (n + 0) - (n + m) = 0, from by rw [nat.add_sub_add_left, nat.zero_sub] theorem add_le_to_le_sub (x : ℕ) {y k : ℕ} (h : k ≤ y) : x + k ≤ y ↔ x ≤ y - k := by rw [← nat.add_sub_cancel x k, nat.sub_le_sub_right_iff _ _ _ h, nat.add_sub_cancel] lemma sub_lt_of_pos_le (a b : ℕ) (h₀ : 0 < a) (h₁ : a ≤ b) : b - a < b := begin apply sub_lt _ h₀, apply lt_of_lt_of_le h₀ h₁ end theorem sub_one (n : ℕ) : n - 1 = pred n := rfl theorem succ_sub_one (n : ℕ) : succ n - 1 = n := rfl theorem succ_pred_eq_of_pos : ∀ {n : ℕ}, n > 0 → succ (pred n) = n | 0 h := absurd h (lt_irrefl 0) | (succ k) h := rfl theorem sub_eq_zero_of_le {n m : ℕ} (h : n ≤ m) : n - m = 0 := exists.elim (nat.le.dest h) (assume k, assume hk : n + k = m, by rw [← hk, sub_self_add]) protected theorem le_of_sub_eq_zero : ∀{n m : ℕ}, n - m = 0 → n ≤ m | n 0 H := begin rw [nat.sub_zero] at H, simp [H] end | 0 (m+1) H := zero_le _ | (n+1) (m+1) H := add_le_add_right (le_of_sub_eq_zero begin simp [nat.add_sub_add_right] at H, exact H end) _ protected theorem sub_eq_zero_iff_le {n m : ℕ} : n - m = 0 ↔ n ≤ m := ⟨nat.le_of_sub_eq_zero, nat.sub_eq_zero_of_le⟩ theorem add_sub_of_le {n m : ℕ} (h : n ≤ m) : n + (m - n) = m := exists.elim (nat.le.dest h) (assume k, assume hk : n + k = m, by rw [← hk, nat.add_sub_cancel_left]) protected theorem sub_add_cancel {n m : ℕ} (h : n ≥ m) : n - m + m = n := by rw [add_comm, add_sub_of_le h] protected theorem add_sub_assoc {m k : ℕ} (h : k ≤ m) (n : ℕ) : n + m - k = n + (m - k) := exists.elim (nat.le.dest h) (assume l, assume hl : k + l = m, by rw [← hl, nat.add_sub_cancel_left, add_comm k, ← add_assoc, nat.add_sub_cancel]) protected lemma sub_eq_iff_eq_add {a b c : ℕ} (ab : b ≤ a) : a - b = c ↔ a = c + b := ⟨assume c_eq, begin rw [c_eq.symm, nat.sub_add_cancel ab] end, assume a_eq, begin rw [a_eq, nat.add_sub_cancel] end⟩ protected lemma lt_of_sub_eq_succ {m n l : ℕ} (H : m - n = nat.succ l) : n < m := lt_of_not_ge (assume (H' : n ≥ m), begin simp [nat.sub_eq_zero_of_le H'] at H, contradiction end) @[simp] lemma zero_min (a : ℕ) : min 0 a = 0 := min_eq_left (zero_le a) @[simp] lemma min_zero (a : ℕ) : min a 0 = 0 := min_eq_right (zero_le a) -- Distribute succ over min theorem min_succ_succ (x y : ℕ) : min (succ x) (succ y) = succ (min x y) := have f : x ≤ y → min (succ x) (succ y) = succ (min x y), from λp, calc min (succ x) (succ y) = succ x : if_pos (succ_le_succ p) ... = succ (min x y) : congr_arg succ (eq.symm (if_pos p)), have g : ¬ (x ≤ y) → min (succ x) (succ y) = succ (min x y), from λp, calc min (succ x) (succ y) = succ y : if_neg (λeq, p (pred_le_pred eq)) ... = succ (min x y) : congr_arg succ (eq.symm (if_neg p)), decidable.by_cases f g theorem sub_eq_sub_min (n m : ℕ) : n - m = n - min n m := if h : n ≥ m then by rewrite [min_eq_right h] else by rewrite [sub_eq_zero_of_le (le_of_not_ge h), min_eq_left (le_of_not_ge h), nat.sub_self] @[simp] theorem sub_add_min_cancel (n m : ℕ) : n - m + min n m = n := by rw [sub_eq_sub_min, nat.sub_add_cancel (min_le_left n m)] /- TODO(Leo): sub + inequalities -/ protected def strong_rec_on {p : nat → Sort u} (n : nat) (h : ∀ n, (∀ m, m < n → p m) → p n) : p n := suffices ∀ n m, m < n → p m, from this (succ n) n (lt_succ_self _), begin intros n, induction n with n ih, {intros m h₁, exact absurd h₁ (not_lt_zero _)}, {intros m h₁, apply or.by_cases (lt_or_eq_of_le (le_of_lt_succ h₁)), {intros, apply ih, assumption}, {intros, subst m, apply h _ ih}} end protected lemma strong_induction_on {p : nat → Prop} (n : nat) (h : ∀ n, (∀ m, m < n → p m) → p n) : p n := nat.strong_rec_on n h protected lemma case_strong_induction_on {p : nat → Prop} (a : nat) (hz : p 0) (hi : ∀ n, (∀ m, m ≤ n → p m) → p (succ n)) : p a := nat.strong_induction_on a $ λ n, match n with | 0 := λ _, hz | (n+1) := λ h₁, hi n (λ m h₂, h₁ _ (lt_succ_of_le h₂)) end /- mod -/ lemma mod_def (x y : nat) : x % y = if 0 < y ∧ y ≤ x then (x - y) % y else x := by have h := mod_def_aux x y; rwa [dif_eq_if] at h @[simp] lemma mod_zero (a : nat) : a % 0 = a := begin rw mod_def, have h : ¬ (0 < 0 ∧ 0 ≤ a), simp [lt_irrefl], simp [if_neg, h] end lemma mod_eq_of_lt {a b : nat} (h : a < b) : a % b = a := begin rw mod_def, have h' : ¬(0 < b ∧ b ≤ a), simp [not_le_of_gt h], simp [if_neg, h'] end @[simp] lemma zero_mod (b : nat) : 0 % b = 0 := begin rw mod_def, have h : ¬(0 < b ∧ b ≤ 0), {intro hn, cases hn with l r, exact absurd (lt_of_lt_of_le l r) (lt_irrefl 0)}, simp [if_neg, h] end lemma mod_eq_sub_mod {a b : nat} (h : a ≥ b) : a % b = (a - b) % b := or.elim (eq_zero_or_pos b) (λb0, by rw [b0, nat.sub_zero]) (λh₂, by rw [mod_def, if_pos (and.intro h₂ h)]) lemma mod_lt (x : nat) {y : nat} (h : y > 0) : x % y < y := begin induction x using nat.case_strong_induction_on with x ih, {rw zero_mod, assumption}, {apply or.elim (decidable.em (succ x < y)), {intro h₁, rwa [mod_eq_of_lt h₁]}, {intro h₁, have h₁ : succ x % y = (succ x - y) % y, {exact mod_eq_sub_mod (le_of_not_gt h₁)}, have this : succ x - y ≤ x, {exact le_of_lt_succ (sub_lt (succ_pos x) h)}, have h₂ : (succ x - y) % y < y, {exact ih _ this}, rwa [← h₁] at h₂}} end @[simp] theorem mod_self (n : nat) : n % n = 0 := by rw [mod_eq_sub_mod (le_refl _), nat.sub_self, zero_mod] @[simp] lemma mod_one (n : ℕ) : n % 1 = 0 := have n % 1 < 1, from (mod_lt n) (succ_pos 0), eq_zero_of_le_zero (le_of_lt_succ this) lemma mod_two_eq_zero_or_one (n : ℕ) : n % 2 = 0 ∨ n % 2 = 1 := match n % 2, @nat.mod_lt n 2 dec_trivial with | 0, _ := or.inl rfl | 1, _ := or.inr rfl | k+2, h := absurd h dec_trivial end /- div & mod -/ lemma div_def (x y : nat) : x / y = if 0 < y ∧ y ≤ x then (x - y) / y + 1 else 0 := by have h := div_def_aux x y; rwa dif_eq_if at h lemma mod_add_div (m k : ℕ) : m % k + k * (m / k) = m := begin apply nat.strong_induction_on m, clear m, intros m IH, cases decidable.em (0 < k ∧ k ≤ m) with h h', -- 0 < k ∧ k ≤ m { have h' : m - k < m, { apply nat.sub_lt _ h.left, apply lt_of_lt_of_le h.left h.right }, rw [div_def, mod_def, if_pos h, if_pos h], simp [left_distrib, IH _ h'], rw [← nat.add_sub_assoc h.right, nat.add_sub_cancel_left] }, -- ¬ (0 < k ∧ k ≤ m) { rw [div_def, mod_def, if_neg h', if_neg h'], simp }, end /- div -/ @[simp] protected lemma div_one (n : ℕ) : n / 1 = n := have n % 1 + 1 * (n / 1) = n, from mod_add_div _ _, by simp [mod_one] at this; assumption @[simp] protected lemma div_zero (n : ℕ) : n / 0 = 0 := begin rw [div_def], simp [lt_irrefl] end @[simp] protected lemma zero_div (b : ℕ) : 0 / b = 0 := eq.trans (div_def 0 b) $ if_neg (and.rec not_le_of_gt) protected lemma div_le_of_le_mul {m n : ℕ} : ∀ {k}, m ≤ k * n → m / k ≤ n | 0 h := by simp [nat.div_zero]; apply zero_le | (succ k) h := suffices succ k * (m / succ k) ≤ succ k * n, from le_of_mul_le_mul_left this (zero_lt_succ _), calc succ k * (m / succ k) ≤ m % succ k + succ k * (m / succ k) : le_add_left _ _ ... = m : by rw mod_add_div ... ≤ succ k * n : h protected lemma div_le_self : ∀ (m n : ℕ), m / n ≤ m | m 0 := by simp [nat.div_zero]; apply zero_le | m (succ n) := have m ≤ succ n * m, from calc m = 1 * m : by simp ... ≤ succ n * m : mul_le_mul_right _ (succ_le_succ (zero_le _)), nat.div_le_of_le_mul this lemma div_eq_sub_div {a b : nat} (h₁ : b > 0) (h₂ : a ≥ b) : a / b = (a - b) / b + 1 := begin rw [div_def a, if_pos], split ; assumption end lemma div_eq_of_lt {a b : ℕ} (h₀ : a < b) : a / b = 0 := begin rw [div_def a, if_neg], intro h₁, apply not_le_of_gt h₀ h₁.right end -- this is a Galois connection -- f x ≤ y ↔ x ≤ g y -- with -- f x = x * k -- g y = y / k theorem le_div_iff_mul_le (x y : ℕ) {k : ℕ} (Hk : k > 0) : x ≤ y / k ↔ x * k ≤ y := begin -- Hk is needed because, despite div being made total, y / 0 := 0 -- x * 0 ≤ y ↔ x ≤ y / 0 -- ↔ 0 ≤ y ↔ x ≤ 0 -- ↔ true ↔ x = 0 -- ↔ x = 0 revert x, apply nat.strong_induction_on y _, clear y, intros y IH x, cases lt_or_ge y k with h h, -- base case: y < k { rw [div_eq_of_lt h], cases x with x, { simp [zero_mul, zero_le] }, { simp [succ_mul, not_succ_le_zero], apply not_le_of_gt, apply lt_of_lt_of_le h, apply le_add_right } }, -- step: k ≤ y { rw [div_eq_sub_div Hk h], cases x with x, { simp [zero_mul, zero_le] }, { have Hlt : y - k < y, { apply sub_lt_of_pos_le ; assumption }, rw [ ← add_one , nat.add_le_add_iff_le_right , IH (y - k) Hlt x , add_one , succ_mul, add_le_to_le_sub _ h ] } } end theorem div_lt_iff_lt_mul (x y : ℕ) {k : ℕ} (Hk : k > 0) : x / k < y ↔ x < y * k := begin simp [lt_iff_not_ge], apply not_iff_not_of_iff, apply le_div_iff_mul_le _ _ Hk end def iterate {α : Type} (op : α → α) : ℕ → α → α | 0 a := a | (succ k) a := iterate k (op a) notation f`^[`n`]` := iterate f n /- successor and predecessor -/ theorem add_one_ne_zero (n : ℕ) : n + 1 ≠ 0 := succ_ne_zero _ theorem eq_zero_or_eq_succ_pred (n : ℕ) : n = 0 ∨ n = succ (pred n) := by cases n; simp theorem exists_eq_succ_of_ne_zero {n : ℕ} (H : n ≠ 0) : ∃k : ℕ, n = succ k := ⟨_, (eq_zero_or_eq_succ_pred _).resolve_left H⟩ theorem succ_inj {n m : ℕ} (H : succ n = succ m) : n = m := nat.succ.inj_arrow H id theorem discriminate {B : Sort u} {n : ℕ} (H1: n = 0 → B) (H2 : ∀m, n = succ m → B) : B := by ginduction n with h; [exact H1 h, exact H2 _ h] theorem one_succ_zero : 1 = succ 0 := rfl theorem two_step_induction {P : ℕ → Sort u} (H1 : P 0) (H2 : P 1) (H3 : ∀ (n : ℕ) (IH1 : P n) (IH2 : P (succ n)), P (succ (succ n))) : Π (a : ℕ), P a | 0 := H1 | 1 := H2 | (succ (succ n)) := H3 _ (two_step_induction _) (two_step_induction _) theorem sub_induction {P : ℕ → ℕ → Sort u} (H1 : ∀m, P 0 m) (H2 : ∀n, P (succ n) 0) (H3 : ∀n m, P n m → P (succ n) (succ m)) : Π (n m : ℕ), P n m | 0 m := H1 _ | (succ n) 0 := H2 _ | (succ n) (succ m) := H3 _ _ (sub_induction n m) /- addition -/ theorem succ_add_eq_succ_add (n m : ℕ) : succ n + m = n + succ m := by simp [succ_add, add_succ] theorem one_add (n : ℕ) : 1 + n = succ n := by simp protected theorem add_right_comm : ∀ (n m k : ℕ), n + m + k = n + k + m := right_comm nat.add nat.add_comm nat.add_assoc theorem eq_zero_of_add_eq_zero {n m : ℕ} (H : n + m = 0) : n = 0 ∧ m = 0 := ⟨nat.eq_zero_of_add_eq_zero_right H, nat.eq_zero_of_add_eq_zero_left H⟩ theorem eq_zero_of_mul_eq_zero : ∀ {n m : ℕ}, n * m = 0 → n = 0 ∨ m = 0 | 0 m := λ h, or.inl rfl | (succ n) m := begin rw succ_mul, intro h, exact or.inr (eq_zero_of_add_eq_zero_left h) end /- properties of inequality -/ theorem le_succ_of_pred_le {n m : ℕ} : pred n ≤ m → n ≤ succ m := nat.cases_on n less_than_or_equal.step (λ a, succ_le_succ) theorem le_lt_antisymm {n m : ℕ} (h₁ : n ≤ m) (h₂ : m < n) : false := nat.lt_irrefl n (nat.lt_of_le_of_lt h₁ h₂) theorem lt_le_antisymm {n m : ℕ} (h₁ : n < m) (h₂ : m ≤ n) : false := le_lt_antisymm h₂ h₁ protected theorem nat.lt_asymm {n m : ℕ} (h₁ : n < m) : ¬ m < n := le_lt_antisymm (nat.le_of_lt h₁) protected def lt_ge_by_cases {a b : ℕ} {C : Sort u} (h₁ : a < b → C) (h₂ : a ≥ b → C) : C := decidable.by_cases h₁ (λ h, h₂ (or.elim (nat.lt_or_ge a b) (λ a, absurd a h) (λ a, a))) protected def lt_by_cases {a b : ℕ} {C : Sort u} (h₁ : a < b → C) (h₂ : a = b → C) (h₃ : b < a → C) : C := nat.lt_ge_by_cases h₁ (λ h₁, nat.lt_ge_by_cases h₃ (λ h, h₂ (nat.le_antisymm h h₁))) protected theorem lt_trichotomy (a b : ℕ) : a < b ∨ a = b ∨ b < a := nat.lt_by_cases (λ h, or.inl h) (λ h, or.inr (or.inl h)) (λ h, or.inr (or.inr h)) protected theorem eq_or_lt_of_not_lt {a b : ℕ} (hnlt : ¬ a < b) : a = b ∨ b < a := (nat.lt_trichotomy a b).resolve_left hnlt theorem lt_succ_of_lt {a b : nat} (h : a < b) : a < succ b := le_succ_of_le h def one_pos := nat.zero_lt_one theorem mul_self_le_mul_self {n m : ℕ} (h : n ≤ m) : n * n ≤ m * m := mul_le_mul h h (zero_le _) (zero_le _) theorem mul_self_lt_mul_self : Π {n m : ℕ}, n < m → n * n < m * m | 0 m h := mul_pos h h | (succ n) m h := mul_lt_mul h (le_of_lt h) (succ_pos _) (zero_le _) theorem mul_self_le_mul_self_iff {n m : ℕ} : n ≤ m ↔ n * n ≤ m * m := ⟨mul_self_le_mul_self, λh, decidable.by_contradiction $ λhn, not_lt_of_ge h $ mul_self_lt_mul_self $ lt_of_not_ge hn⟩ theorem mul_self_lt_mul_self_iff {n m : ℕ} : n < m ↔ n * n < m * m := iff.trans (lt_iff_not_ge _ _) $ iff.trans (not_iff_not_of_iff mul_self_le_mul_self_iff) $ iff.symm (lt_iff_not_ge _ _) theorem le_mul_self : Π (n : ℕ), n ≤ n * n | 0 := le_refl _ | (n+1) := let t := mul_le_mul_left (n+1) (succ_pos n) in by simp at t; exact t /- subtraction -/ protected theorem sub_le_sub_left {n m : ℕ} (k) (h : n ≤ m) : k - m ≤ k - n := by induction h; [refl, exact le_trans (pred_le _) ih_1] theorem succ_sub_sub_succ (n m k : ℕ) : succ n - m - succ k = n - m - k := by rw [nat.sub_sub, nat.sub_sub, add_succ, succ_sub_succ] protected theorem sub.right_comm (m n k : ℕ) : m - n - k = m - k - n := by rw [nat.sub_sub, nat.sub_sub, add_comm] theorem mul_pred_left : ∀ (n m : ℕ), pred n * m = n * m - m | 0 m := by simp [nat.zero_sub, pred_zero, zero_mul] | (succ n) m := by rw [pred_succ, succ_mul, nat.add_sub_cancel] theorem mul_pred_right (n m : ℕ) : n * pred m = n * m - n := by rw [mul_comm, mul_pred_left, mul_comm] protected theorem mul_sub_right_distrib : ∀ (n m k : ℕ), (n - m) * k = n * k - m * k | n 0 k := by simp [nat.sub_zero] | n (succ m) k := by rw [nat.sub_succ, mul_pred_left, mul_sub_right_distrib, succ_mul, nat.sub_sub] protected theorem mul_sub_left_distrib (n m k : ℕ) : n * (m - k) = n * m - n * k := by rw [mul_comm, nat.mul_sub_right_distrib, mul_comm m n, mul_comm n k] protected theorem mul_self_sub_mul_self_eq (a b : nat) : a * a - b * b = (a + b) * (a - b) := by rw [nat.mul_sub_left_distrib, right_distrib, right_distrib, mul_comm b a, add_comm (a*a) (a*b), nat.add_sub_add_left] theorem succ_mul_succ_eq (a b : nat) : succ a * succ b = a*b + a + b + 1 := begin rw [← add_one, ← add_one], simp [right_distrib, left_distrib] end theorem succ_sub {m n : ℕ} (h : m ≥ n) : succ m - n = succ (m - n) := exists.elim (nat.le.dest h) (assume k, assume hk : n + k = m, by rw [← hk, nat.add_sub_cancel_left, ← add_succ, nat.add_sub_cancel_left]) protected theorem sub_pos_of_lt {m n : ℕ} (h : m < n) : n - m > 0 := have 0 + m < n - m + m, begin rw [zero_add, nat.sub_add_cancel (le_of_lt h)], exact h end, lt_of_add_lt_add_right this protected theorem sub_sub_self {n m : ℕ} (h : m ≤ n) : n - (n - m) = m := (nat.sub_eq_iff_eq_add (nat.sub_le _ _)).2 (eq.symm (add_sub_of_le h)) protected theorem sub_add_comm {n m k : ℕ} (h : k ≤ n) : n + m - k = n - k + m := (nat.sub_eq_iff_eq_add (nat.le_trans h (nat.le_add_right _ _))).2 (by rwa [nat.add_right_comm, nat.sub_add_cancel]) theorem sub_one_sub_lt {n i} (h : i < n) : n - 1 - i < n := begin rw nat.sub_sub, apply nat.sub_lt, apply lt_of_lt_of_le (nat.zero_lt_succ _) h, rw add_comm, apply nat.zero_lt_succ end theorem pred_inj : ∀ {a b : nat}, a > 0 → b > 0 → nat.pred a = nat.pred b → a = b | (succ a) (succ b) ha hb h := have a = b, from h, by rw this | (succ a) 0 ha hb h := absurd hb (lt_irrefl _) | 0 (succ b) ha hb h := absurd ha (lt_irrefl _) | 0 0 ha hb h := rfl /- find -/ section find parameter {p : ℕ → Prop} private def lbp (m n : ℕ) : Prop := m = n + 1 ∧ ∀ k ≤ n, ¬p k parameters [decidable_pred p] (H : ∃n, p n) private def wf_lbp : well_founded lbp := ⟨let ⟨n, pn⟩ := H in suffices ∀m k, n ≤ k + m → acc lbp k, from λa, this _ _ (nat.le_add_left _ _), λm, nat.rec_on m (λk kn, ⟨_, λy r, match y, r with ._, ⟨rfl, a⟩ := absurd pn (a _ kn) end⟩) (λm IH k kn, ⟨_, λy r, match y, r with ._, ⟨rfl, a⟩ := IH _ (by rw nat.add_right_comm; exact kn) end⟩)⟩ protected def find_x : {n // p n ∧ ∀m < n, ¬p m} := @well_founded.fix _ (λk, (∀n < k, ¬p n) → {n // p n ∧ ∀m < n, ¬p m}) lbp wf_lbp (λm IH al, if pm : p m then ⟨m, pm, al⟩ else have ∀ n ≤ m, ¬p n, from λn h, or.elim (lt_or_eq_of_le h) (al n) (λe, by rw e; exact pm), IH _ ⟨rfl, this⟩ (λn h, this n $ nat.le_of_succ_le_succ h)) 0 (λn h, absurd h (nat.not_lt_zero _)) protected def find : ℕ := nat.find_x.1 protected theorem find_spec : p nat.find := nat.find_x.2.left protected theorem find_min : ∀ {m : ℕ}, m < nat.find → ¬p m := nat.find_x.2.right protected theorem find_min' {m : ℕ} (h : p m) : nat.find ≤ m := le_of_not_gt (λ l, find_min l h) end find /- mod -/ theorem mod_le (x y : ℕ) : x % y ≤ x := or.elim (lt_or_ge x y) (λxlty, by rw mod_eq_of_lt xlty; refl) (λylex, or.elim (eq_zero_or_pos y) (λy0, by rw [y0, mod_zero]; refl) (λypos, le_trans (le_of_lt (mod_lt _ ypos)) ylex)) @[simp] theorem add_mod_right (x z : ℕ) : (x + z) % z = x % z := by rw [mod_eq_sub_mod (nat.le_add_left _ _), nat.add_sub_cancel] @[simp] theorem add_mod_left (x z : ℕ) : (x + z) % x = z % x := by rw [add_comm, add_mod_right] @[simp] theorem add_mul_mod_self_left (x y z : ℕ) : (x + y * z) % y = x % y := by {induction z with z ih, simp, rw[mul_succ, ← add_assoc, add_mod_right, ih]} @[simp] theorem add_mul_mod_self_right (x y z : ℕ) : (x + y * z) % z = x % z := by rw [mul_comm, add_mul_mod_self_left] @[simp] theorem mul_mod_right (m n : ℕ) : (m * n) % m = 0 := by rw [← zero_add (m*n), add_mul_mod_self_left, zero_mod] @[simp] theorem mul_mod_left (m n : ℕ) : (m * n) % n = 0 := by rw [mul_comm, mul_mod_right] theorem mul_mod_mul_left (z x y : ℕ) : (z * x) % (z * y) = z * (x % y) := if y0 : y = 0 then by rw [y0, mul_zero, mod_zero, mod_zero] else if z0 : z = 0 then by rw [z0, zero_mul, zero_mul, zero_mul, mod_zero] else x.strong_induction_on $ λn IH, have y0 : y > 0, from nat.pos_of_ne_zero y0, have z0 : z > 0, from nat.pos_of_ne_zero z0, or.elim (le_or_gt y n) (λyn, by rw [ mod_eq_sub_mod yn, mod_eq_sub_mod (mul_le_mul_left z yn), ← nat.mul_sub_left_distrib]; exact IH _ (sub_lt (lt_of_lt_of_le y0 yn) y0)) (λyn, by rw [mod_eq_of_lt yn, mod_eq_of_lt (mul_lt_mul_of_pos_left yn z0)]) theorem mul_mod_mul_right (z x y : ℕ) : (x * z) % (y * z) = (x % y) * z := by rw [mul_comm x z, mul_comm y z, mul_comm (x % y) z]; apply mul_mod_mul_left theorem cond_to_bool_mod_two (x : ℕ) [d : decidable (x % 2 = 1)] : cond (@to_bool (x % 2 = 1) d) 1 0 = x % 2 := begin cases d with h h ; unfold decidable.to_bool cond, { cases mod_two_eq_zero_or_one x with h' h', rw h', cases h h' }, { rw h }, end theorem sub_mul_mod (x k n : ℕ) (h₁ : n*k ≤ x) : (x - n*k) % n = x % n := begin induction k with k, { simp }, { have h₂ : n * k ≤ x, { rw [mul_succ] at h₁, apply nat.le_trans _ h₁, apply le_add_right _ n }, have h₄ : x - n * k ≥ n, { apply @nat.le_of_add_le_add_right (n*k), rw [nat.sub_add_cancel h₂], simp [mul_succ] at h₁, simp [h₁] }, rw [mul_succ, ← nat.sub_sub, ← mod_eq_sub_mod h₄, ih_1 h₂] } end /- div -/ theorem sub_mul_div (x n p : ℕ) (h₁ : n*p ≤ x) : (x - n*p) / n = x / n - p := begin cases eq_zero_or_pos n with h₀ h₀, { rw [h₀, nat.div_zero, nat.div_zero, nat.zero_sub] }, { induction p with p, { simp }, { have h₂ : n*p ≤ x, { transitivity, { apply nat.mul_le_mul_left, apply le_succ }, { apply h₁ } }, have h₃ : x - n * p ≥ n, { apply le_of_add_le_add_right, rw [nat.sub_add_cancel h₂, add_comm], rw [mul_succ] at h₁, apply h₁ }, rw [sub_succ, ← ih_1 h₂], rw [@div_eq_sub_div (x - n*p) _ h₀ h₃], simp [add_one, pred_succ, mul_succ, nat.sub_sub] } } end theorem div_mul_le_self : ∀ (m n : ℕ), m / n * n ≤ m | m 0 := by simp; apply zero_le | m (succ n) := (le_div_iff_mul_le _ _ (nat.succ_pos _)).1 (le_refl _) @[simp] theorem add_div_right (x : ℕ) {z : ℕ} (H : z > 0) : (x + z) / z = succ (x / z) := by rw [div_eq_sub_div H (nat.le_add_left _ _), nat.add_sub_cancel] @[simp] theorem add_div_left (x : ℕ) {z : ℕ} (H : z > 0) : (z + x) / z = succ (x / z) := by rw [add_comm, add_div_right x H] @[simp] theorem mul_div_right (n : ℕ) {m : ℕ} (H : m > 0) : m * n / m = n := by {induction n; simp [*, mul_succ, -mul_comm]} @[simp] theorem mul_div_left (m : ℕ) {n : ℕ} (H : n > 0) : m * n / n = m := by rw [mul_comm, mul_div_right _ H] protected theorem div_self {n : ℕ} (H : n > 0) : n / n = 1 := let t := add_div_right 0 H in by rwa [zero_add, nat.zero_div] at t theorem add_mul_div_left (x z : ℕ) {y : ℕ} (H : y > 0) : (x + y * z) / y = x / y + z := by {induction z with z ih, simp, rw [mul_succ, ← add_assoc, add_div_right _ H, ih]} theorem add_mul_div_right (x y : ℕ) {z : ℕ} (H : z > 0) : (x + y * z) / z = x / z + y := by rw [mul_comm, add_mul_div_left _ _ H] protected theorem mul_div_cancel (m : ℕ) {n : ℕ} (H : n > 0) : m * n / n = m := let t := add_mul_div_right 0 m H in by rwa [zero_add, nat.zero_div, zero_add] at t protected theorem mul_div_cancel_left (m : ℕ) {n : ℕ} (H : n > 0) : n * m / n = m := by rw [mul_comm, nat.mul_div_cancel _ H] protected theorem div_eq_of_eq_mul_left {m n k : ℕ} (H1 : n > 0) (H2 : m = k * n) : m / n = k := by rw [H2, nat.mul_div_cancel _ H1] protected theorem div_eq_of_eq_mul_right {m n k : ℕ} (H1 : n > 0) (H2 : m = n * k) : m / n = k := by rw [H2, nat.mul_div_cancel_left _ H1] protected theorem div_eq_of_lt_le {m n k : ℕ} (lo : k * n ≤ m) (hi : m < succ k * n) : m / n = k := have npos : n > 0, from (eq_zero_or_pos _).resolve_left $ λ hn, by rw [hn, mul_zero] at hi lo; exact absurd lo (not_le_of_gt hi), le_antisymm (le_of_lt_succ ((nat.div_lt_iff_lt_mul _ _ npos).2 hi)) ((nat.le_div_iff_mul_le _ _ npos).2 lo) theorem mul_sub_div (x n p : ℕ) (h₁ : x < n*p) : (n * p - succ x) / n = p - succ (x / n) := begin have npos : n > 0 := (eq_zero_or_pos _).resolve_left (λ n0, by rw [n0, zero_mul] at h₁; exact not_lt_zero _ h₁), apply nat.div_eq_of_lt_le, { rw [nat.mul_sub_right_distrib, mul_comm], apply nat.sub_le_sub_left, exact (div_lt_iff_lt_mul _ _ npos).1 (lt_succ_self _) }, { change succ (pred (n * p - x)) ≤ (succ (pred (p - x / n))) * n, rw [succ_pred_eq_of_pos (nat.sub_pos_of_lt h₁), succ_pred_eq_of_pos (nat.sub_pos_of_lt _)], { rw [nat.mul_sub_right_distrib, mul_comm], apply nat.sub_le_sub_left, apply div_mul_le_self }, { apply (div_lt_iff_lt_mul _ _ npos).2, rwa mul_comm } } end protected theorem div_div_eq_div_mul (m n k : ℕ) : m / n / k = m / (n * k) := begin cases eq_zero_or_pos k with k0 kpos, {rw [k0, mul_zero, nat.div_zero, nat.div_zero]}, cases eq_zero_or_pos n with n0 npos, {rw [n0, zero_mul, nat.div_zero, nat.zero_div]}, apply le_antisymm, { apply (le_div_iff_mul_le _ _ (mul_pos npos kpos)).2, rw [mul_comm n k, ← mul_assoc], apply (le_div_iff_mul_le _ _ npos).1, apply (le_div_iff_mul_le _ _ kpos).1, refl }, { apply (le_div_iff_mul_le _ _ kpos).2, apply (le_div_iff_mul_le _ _ npos).2, rw [mul_assoc, mul_comm n k], apply (le_div_iff_mul_le _ _ (mul_pos kpos npos)).1, refl } end protected theorem mul_div_mul {m : ℕ} (n k : ℕ) (H : m > 0) : m * n / (m * k) = n / k := by rw [← nat.div_div_eq_div_mul, nat.mul_div_cancel_left _ H] /- dvd -/ protected theorem dvd_add_iff_right {k m n : ℕ} (h : k ∣ m) : k ∣ n ↔ k ∣ m + n := ⟨dvd_add h, dvd.elim h $ λd hd, match m, hd with | ._, rfl := λh₂, dvd.elim h₂ $ λe he, ⟨e - d, by rw [nat.mul_sub_left_distrib, ← he, nat.add_sub_cancel_left]⟩ end⟩ protected theorem dvd_add_iff_left {k m n : ℕ} (h : k ∣ n) : k ∣ m ↔ k ∣ m + n := by rw add_comm; exact nat.dvd_add_iff_right h theorem dvd_sub {k m n : ℕ} (H : n ≤ m) (h₁ : k ∣ m) (h₂ : k ∣ n) : k ∣ m - n := (nat.dvd_add_iff_left h₂).2 $ by rw nat.sub_add_cancel H; exact h₁ theorem dvd_mod_iff {k m n : ℕ} (h : k ∣ n) : k ∣ m % n ↔ k ∣ m := let t := @nat.dvd_add_iff_left _ (m % n) _ (dvd_trans h (dvd_mul_right n (m / n))) in by rwa mod_add_div at t theorem le_of_dvd {m n : ℕ} (h : n > 0) : m ∣ n → m ≤ n := λ⟨k, e⟩, by { revert h, rw e, refine k.cases_on _ _, exact λhn, absurd hn (lt_irrefl _), exact λk _, let t := mul_le_mul_left m (succ_pos k) in by rwa mul_one at t } theorem dvd_antisymm : Π {m n : ℕ}, m ∣ n → n ∣ m → m = n | m 0 h₁ h₂ := eq_zero_of_zero_dvd h₂ | 0 n h₁ h₂ := (eq_zero_of_zero_dvd h₁).symm | (succ m) (succ n) h₁ h₂ := le_antisymm (le_of_dvd (succ_pos _) h₁) (le_of_dvd (succ_pos _) h₂) theorem pos_of_dvd_of_pos {m n : ℕ} (H1 : m ∣ n) (H2 : n > 0) : m > 0 := nat.pos_of_ne_zero $ λm0, by rw m0 at H1; rw eq_zero_of_zero_dvd H1 at H2; exact lt_irrefl _ H2 theorem eq_one_of_dvd_one {n : ℕ} (H : n ∣ 1) : n = 1 := le_antisymm (le_of_dvd dec_trivial H) (pos_of_dvd_of_pos H dec_trivial) theorem dvd_of_mod_eq_zero {m n : ℕ} (H : n % m = 0) : m ∣ n := dvd.intro (n / m) $ let t := mod_add_div n m in by simp [H] at t; exact t theorem mod_eq_zero_of_dvd {m n : ℕ} (H : m ∣ n) : n % m = 0 := dvd.elim H (λ z H1, by rw [H1, mul_mod_right]) theorem dvd_iff_mod_eq_zero (m n : ℕ) : m ∣ n ↔ n % m = 0 := ⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩ instance decidable_dvd : @decidable_rel ℕ (∣) := λm n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm protected theorem mul_div_cancel' {m n : ℕ} (H : n ∣ m) : n * (m / n) = m := let t := mod_add_div m n in by rwa [mod_eq_zero_of_dvd H, zero_add] at t protected theorem div_mul_cancel {m n : ℕ} (H : n ∣ m) : m / n * n = m := by rw [mul_comm, nat.mul_div_cancel' H] protected theorem mul_div_assoc (m : ℕ) {n k : ℕ} (H : k ∣ n) : m * n / k = m * (n / k) := or.elim (eq_zero_or_pos k) (λh, by rw [h, nat.div_zero, nat.div_zero, mul_zero]) (λh, have m * n / k = m * (n / k * k) / k, by rw nat.div_mul_cancel H, by rw[this, ← mul_assoc, nat.mul_div_cancel _ h]) theorem dvd_of_mul_dvd_mul_left {m n k : ℕ} (kpos : k > 0) (H : k * m ∣ k * n) : m ∣ n := dvd.elim H (λl H1, by rw mul_assoc at H1; exact ⟨_, eq_of_mul_eq_mul_left kpos H1⟩) theorem dvd_of_mul_dvd_mul_right {m n k : ℕ} (kpos : k > 0) (H : m * k ∣ n * k) : m ∣ n := by rw [mul_comm m k, mul_comm n k] at H; exact dvd_of_mul_dvd_mul_left kpos H /- pow -/ @[simp] theorem pow_one (b : ℕ) : b^1 = b := by simp [pow_succ] theorem pow_le_pow_of_le_left {x y : ℕ} (H : x ≤ y) : ∀ i, x^i ≤ y^i | 0 := le_refl _ | (succ i) := mul_le_mul (pow_le_pow_of_le_left i) H (zero_le _) (zero_le _) theorem pow_le_pow_of_le_right {x : ℕ} (H : x > 0) {i} : ∀ {j}, i ≤ j → x^i ≤ x^j | 0 h := by rw eq_zero_of_le_zero h; apply le_refl | (succ j) h := (lt_or_eq_of_le h).elim (λhl, by rw [pow_succ, ← mul_one (x^i)]; exact mul_le_mul (pow_le_pow_of_le_right $ le_of_lt_succ hl) H (zero_le _) (zero_le _)) (λe, by rw e; refl) theorem pos_pow_of_pos {b : ℕ} (n : ℕ) (h : 0 < b) : 0 < b^n := pow_le_pow_of_le_right h (zero_le _) theorem zero_pow {n : ℕ} (h : 0 < n) : 0^n = 0 := by rw [← succ_pred_eq_of_pos h, pow_succ, mul_zero] theorem pow_lt_pow_of_lt_left {x y : ℕ} (H : x < y) {i} (h : i > 0) : x^i < y^i := begin cases i with i, { exact absurd h (not_lt_zero _) }, rw [pow_succ, pow_succ], exact mul_lt_mul' (pow_le_pow_of_le_left (le_of_lt H) _) H (zero_le _) (pos_pow_of_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 [← mul_one (x^i), pow_succ], exact nat.mul_lt_mul_of_pos_left H (pos_pow_of_pos _ xpos) end /- mod / div / pow -/ theorem mod_pow_succ {b : ℕ} (b_pos : b > 0) (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], simp [pow_succ] at h₁, simp [h₁] }, rw [mod_eq_of_lt h₁, mod_eq_of_lt h₂], simp [mod_add_div] }, -- step: p ≥ b^succ w { -- Generate condiition for induction principal have h₂ : p - b^succ w < p, { apply sub_lt_of_pos_le _ _ (pos_pow_of_pos _ b_pos) h₁ }, -- Apply induction rw [mod_eq_sub_mod h₁, IH _ h₂], -- Normalize goal and h1 simp [pow_succ], simp [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], simp [h₁] }, rw [eq.symm (mod_eq_sub_mod p_b_ge)] } end end nat
6d2968939d654d6e18be2669e90a7f32538d2ee7
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/tests/lean/run/alias1.lean
1ceed6318607127d4011445ed1a57c2a733251e6
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
783
lean
import logic namespace N1 constant num : Type.{1} constant foo : num → num → num end N1 namespace N2 constant val : Type.{1} constant foo : val → val → val end N2 open N1 open N2 constants a b : num constants x y : val check foo a b check foo x y constant f : num → val coercion f check foo a x check foo x y check foo x a check foo a b theorem T1 : foo a b = N1.foo a b := eq.refl _ definition aux1 := foo a b -- System elaborated it to N1.foo a b #erase_cache T2 theorem T2 : aux1 = N1.foo a b := eq.refl _ open N1 definition aux2 := foo a b -- Now N1 is in the end of the queue, this is elaborated to N2.foo (f a) (f b) check aux2 theorem T3 : aux2 = N2.foo (f a) (f b) := eq.refl aux2 check foo a b theorem T4 : foo a b = N2.foo a b := eq.refl _
5cf958b18c3c3bc993b74f5e68a34e33f6fd80cc
a7dd8b83f933e72c40845fd168dde330f050b1c9
/src/algebraic_geometry/presheafed_space.lean
a68d0803a1ca73fab0ebf9e0bb6eb53552467300
[ "Apache-2.0" ]
permissive
NeilStrickland/mathlib
10420e92ee5cb7aba1163c9a01dea2f04652ed67
3efbd6f6dff0fb9b0946849b43b39948560a1ffe
refs/heads/master
1,589,043,046,346
1,558,938,706,000
1,558,938,706,000
181,285,984
0
0
Apache-2.0
1,568,941,848,000
1,555,233,833,000
Lean
UTF-8
Lean
false
false
5,429
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 category_theory.instances.Top.presheaf universes v u open category_theory open category_theory.instances open category_theory.instances.Top open topological_space variables (C : Type u) [𝒞 : category.{v+1} C] include 𝒞 namespace algebraic_geometry structure PresheafedSpace := (to_Top : Top.{v}) (𝒪 : to_Top.presheaf C) variables {C} namespace PresheafedSpace instance : has_coe_to_sort (PresheafedSpace.{v} C) := { S := Type v, coe := λ F, F.to_Top.α } instance (X : PresheafedSpace.{v} C) : topological_space X := X.to_Top.str structure hom (X Y : PresheafedSpace.{v} C) := (f : X.to_Top ⟶ Y.to_Top) (c : Y.𝒪 ⟶ f _* X.𝒪) @[extensionality] lemma ext {X Y : PresheafedSpace.{v} C} (α β : hom X Y) (w : α.f = β.f) (h : α.c ≫ (whisker_right (nat_trans.op (opens.map_iso _ _ w).inv) X.𝒪) = β.c) : α = β := begin cases α, cases β, dsimp [presheaf.pushforward] at *, tidy, -- TODO including `injections` would make tidy work earlier. end . def id (X : PresheafedSpace.{v} C) : hom X X := { f := 𝟙 X.to_Top, c := ((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id _).hom) _) } def comp (X Y Z : PresheafedSpace.{v} C) (α : hom X Y) (β : hom Y Z) : hom X Z := { f := α.f ≫ β.f, c := β.c ≫ (whisker_left (opens.map β.f).op α.c) } variables (C) section local attribute [simp] id comp presheaf.pushforward instance category_of_PresheafedSpaces : category (PresheafedSpace.{v} C) := { hom := hom, id := id, comp := comp, -- I'm still grumpy about these proofs. -- The obstacle here is the mysterious need to use `erw` for some `simp` lemmas. -- If we could avoid that, locally adding `op_induction` to `tidy` would discharge these. comp_id' := λ X Y f, begin ext U, { op_induction U, cases U, dsimp, simp, }, { dsimp, simp } end, id_comp' := λ X Y f, begin ext U, { op_induction U, cases U, dsimp, simp only [category.assoc], -- This should be done by `simp`, but unfortunately isn't. erw [category_theory.functor.map_id], simp, }, { simp } end, assoc' := λ W X Y Z f g h, begin ext U, { op_induction U, cases U, dsimp, simp only [category.assoc], -- This should be done by `simp`, but unfortunately isn't. erw [category_theory.functor.map_id], simp, }, { refl } end } end . variables {C} instance {X Y : PresheafedSpace.{v} C} : has_coe (X ⟶ Y) (X.to_Top ⟶ Y.to_Top) := { coe := λ α, α.f } @[simp] lemma id_f (X : PresheafedSpace.{v} C) : ((𝟙 X) : X ⟶ X).f = 𝟙 X.to_Top := rfl @[simp] lemma comp_f {X Y Z : PresheafedSpace.{v} C} (α : X ⟶ Y) (β : Y ⟶ Z) : (α ≫ β).f = α.f ≫ β.f := rfl @[simp] lemma comp_coe {X Y Z : PresheafedSpace.{v} C} (α : X ⟶ Y) (β : Y ⟶ Z) : ((α ≫ β : X ⟶ Z) : X.to_Top ⟶ Z.to_Top) = (α : X.to_Top ⟶ Y.to_Top) ≫ (β : Y.to_Top ⟶ Z.to_Top) := rfl -- We don't mark these as simp lemmas, because the innards are pretty unsightly. lemma id_c (X : PresheafedSpace.{v} C) : ((𝟙 X) : X ⟶ X).c = (((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id _).hom) _)) := rfl lemma comp_c {X Y Z : PresheafedSpace.{v} C} (α : X ⟶ Y) (β : Y ⟶ Z) : (α ≫ β).c = (β.c ≫ (whisker_left (opens.map β.f).op α.c)) := rfl def forget : PresheafedSpace.{v} C ⥤ Top := { obj := λ X, X.to_Top, map := λ X Y f, f } end PresheafedSpace end algebraic_geometry open algebraic_geometry variables {C} namespace category_theory variables {D : Type u} [𝒟 : category.{v+1} D] include 𝒟 local attribute [simp] PresheafedSpace.id_c PresheafedSpace.comp_c presheaf.pushforward namespace functor def map_presheaf (F : C ⥤ D) : PresheafedSpace.{v} C ⥤ PresheafedSpace.{v} D := { obj := λ X, { to_Top := X.to_Top, 𝒪 := X.𝒪 ⋙ F }, map := λ X Y f, { f := f.f, c := whisker_right f.c F } }. @[simp] lemma map_presheaf_obj_X (F : C ⥤ D) (X : PresheafedSpace.{v} C) : (F.map_presheaf.obj X).to_Top = X.to_Top := rfl @[simp] lemma map_presheaf_obj_𝒪 (F : C ⥤ D) (X : PresheafedSpace.{v} C) : (F.map_presheaf.obj X).𝒪 = X.𝒪 ⋙ F := rfl @[simp] lemma map_presheaf_map_f (F : C ⥤ D) {X Y : PresheafedSpace.{v} C} (f : X ⟶ Y) : (F.map_presheaf.map f).f = f := rfl @[simp] lemma map_presheaf_map_c (F : C ⥤ D) {X Y : PresheafedSpace.{v} C} (f : X ⟶ Y) : (F.map_presheaf.map f).c = whisker_right f.c F := rfl end functor namespace nat_trans def on_presheaf {F G : C ⥤ D} (α : F ⟶ G) : G.map_presheaf ⟶ F.map_presheaf := { app := λ X, { f := 𝟙 _, c := whisker_left X.𝒪 α ≫ ((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id _).hom) _) }, naturality' := λ X Y f, begin ext U, { op_induction U, cases U, dsimp, simp only [functor.map_id, category.id_comp, category.comp_id, category.assoc], -- This should be done by `simp`, but unfortunately isn't. erw category_theory.functor.map_id, erw category_theory.functor.map_id, simp only [category.comp_id], exact (α.naturality _).symm, }, { refl, } end }. end nat_trans end category_theory
b9408a7b77e23ce9373472fd2bec62699fdb9c38
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/algebraic_geometry/structure_sheaf.lean
ac589bb6a5a3d459cef14172fa23dc307be357ee
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
12,789
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Scott Morrison -/ import algebraic_geometry.prime_spectrum import algebra.category.CommRing.limits import topology.sheaves.local_predicate import topology.sheaves.forget import ring_theory.localization import ring_theory.subring /-! # The structure sheaf on `prime_spectrum R`. We define the structure sheaf on `Top.of (prime_spectrum R)`, for a commutative ring `R`. We define this as a subsheaf of the sheaf of dependent functions into the localizations, cut out by the condition that the function must be locally equal to a ratio of elements of `R`. Because the condition "is equal to a fraction" passes to smaller open subsets, the subset of functions satisfying this condition is automatically a subpresheaf. Because the condition "is locally equal to a fraction" is local, it is also a subsheaf. (It may be helpful to refer back to `topology.sheaves.sheaf_of_functions`, where we show that dependent functions into any type family form a sheaf, and also `topology.sheaves.local_predicate`, where we characterise the predicates which pick out sub-presheaves and sub-sheaves of these sheaves.) We also set up the ring structure, obtaining `structure_sheaf R : sheaf CommRing (Top.of (prime_spectrum R))`. -/ universe u noncomputable theory variables (R : Type u) [comm_ring R] open Top open topological_space open category_theory open opposite namespace algebraic_geometry /-- $Spec R$, just as a topological space. -/ def Spec.Top : Top := Top.of (prime_spectrum R) namespace structure_sheaf /-- The type family over `prime_spectrum R` consisting of the localization over each point. -/ @[derive [comm_ring, local_ring]] def localizations (P : Spec.Top R) : Type u := localization.at_prime P.as_ideal instance (P : Spec.Top R) : inhabited (localizations R P) := ⟨(localization.of _).to_map 1⟩ variables {R} /-- The predicate saying that a dependent function on an open `U` is realised as a fixed fraction `r / s` in each of the stalks (which are localizations at various prime ideals). -/ def is_fraction {U : opens (Spec.Top R)} (f : Π x : U, localizations R x) : Prop := ∃ (r s : R), ∀ x : U, ¬ (s ∈ x.1.as_ideal) ∧ f x * (localization.of _).to_map s = (localization.of _).to_map r variables (R) /-- The predicate `is_fraction` is "prelocal", in the sense that if it holds on `U` it holds on any open subset `V` of `U`. -/ def is_fraction_prelocal : prelocal_predicate (localizations R) := { pred := λ U f, is_fraction f, res := by { rintro V U i f ⟨r, s, w⟩, exact ⟨r, s, λ x, w (i x)⟩ } } /-- We will define the structure sheaf as the subsheaf of all dependent functions in `Π x : U, localizations R x` consisting of those functions which can locally be expressed as a ratio of (the images in the localization of) elements of `R`. Quoting Hartshorne: For an open set $$U ⊆ Spec A$$, we define $$𝒪(U)$$ to be the set of functions $$s : U → ⨆_{𝔭 ∈ U} A_𝔭$$, such that $s(𝔭) ∈ A_𝔭$$ for each $$𝔭$$, and such that $$s$$ is locally a quotient of elements of $$A$$: to be precise, we require that for each $$𝔭 ∈ U$$, there is a neighborhood $$V$$ of $$𝔭$$, contained in $$U$$, and elements $$a, f ∈ A$$, such that for each $$𝔮 ∈ V, f ∉ 𝔮$$, and $$s(𝔮) = a/f$$ in $$A_𝔮$$. Now Hartshorne had the disadvantage of not knowing about dependent functions, so we replace his circumlocution about functions into a disjoint union with `Π x : U, localizations x`. -/ def is_locally_fraction : local_predicate (localizations R) := (is_fraction_prelocal R).sheafify @[simp] lemma is_locally_fraction_pred {U : opens (Spec.Top R)} (f : Π x : U, localizations R x) : (is_locally_fraction R).pred f = ∀ x : U, ∃ (V) (m : x.1 ∈ V) (i : V ⟶ U), ∃ (r s : R), ∀ y : V, ¬ (s ∈ y.1.as_ideal) ∧ f (i y : U) * (localization.of _).to_map s = (localization.of _).to_map r := rfl /-- The functions satisfying `is_locally_fraction` form a submonoid. -/ def sections_subring (U : (opens (Spec.Top R))ᵒᵖ) : subring (Π x : unop U, localizations R x) := { carrier := { f | (is_locally_fraction R).pred f }, zero_mem' := begin refine λ x, ⟨unop U, x.2, 𝟙 _, 0, 1, λ y, ⟨_, _⟩⟩, { rw ←ideal.ne_top_iff_one, exact y.1.is_prime.1, }, { simp, }, end, one_mem' := begin refine λ x, ⟨unop U, x.2, 𝟙 _, 1, 1, λ y, ⟨_, _⟩⟩, { rw ←ideal.ne_top_iff_one, exact y.1.is_prime.1, }, { simp, }, end, add_mem' := begin intros a b ha hb x, rcases ha x with ⟨Va, ma, ia, ra, sa, wa⟩, rcases hb x with ⟨Vb, mb, ib, rb, sb, wb⟩, refine ⟨Va ⊓ Vb, ⟨ma, mb⟩, opens.inf_le_left _ _ ≫ ia, ra * sb + rb * sa, sa * sb, _⟩, intro y, rcases wa (opens.inf_le_left _ _ y) with ⟨nma, wa⟩, rcases wb (opens.inf_le_right _ _ y) with ⟨nmb, wb⟩, fsplit, { intro H, cases y.1.is_prime.mem_or_mem H; contradiction, }, { simp only [add_mul, ring_hom.map_add, pi.add_apply, ring_hom.map_mul], erw [←wa, ←wb], simp only [mul_assoc], congr' 2, rw [mul_comm], refl, } end, neg_mem' := begin intros a ha x, rcases ha x with ⟨V, m, i, r, s, w⟩, refine ⟨V, m, i, -r, s, _⟩, intro y, rcases w y with ⟨nm, w⟩, fsplit, { exact nm, }, { simp only [ring_hom.map_neg, pi.neg_apply], erw [←w], simp only [neg_mul_eq_neg_mul_symm], } end, mul_mem' := begin intros a b ha hb x, rcases ha x with ⟨Va, ma, ia, ra, sa, wa⟩, rcases hb x with ⟨Vb, mb, ib, rb, sb, wb⟩, refine ⟨Va ⊓ Vb, ⟨ma, mb⟩, opens.inf_le_left _ _ ≫ ia, ra * rb, sa * sb, _⟩, intro y, rcases wa (opens.inf_le_left _ _ y) with ⟨nma, wa⟩, rcases wb (opens.inf_le_right _ _ y) with ⟨nmb, wb⟩, fsplit, { intro H, cases y.1.is_prime.mem_or_mem H; contradiction, }, { simp only [pi.mul_apply, ring_hom.map_mul], erw [←wa, ←wb], simp only [mul_left_comm, mul_assoc, mul_comm], refl, } end, } end structure_sheaf open structure_sheaf /-- The structure sheaf (valued in `Type`, not yet `CommRing`) is the subsheaf consisting of functions satisfying `is_locally_fraction`. -/ def structure_sheaf_in_Type : sheaf (Type u) (Spec.Top R):= subsheaf_to_Types (is_locally_fraction R) instance comm_ring_structure_sheaf_in_Type_obj (U : (opens (Spec.Top R))ᵒᵖ) : comm_ring ((structure_sheaf_in_Type R).presheaf.obj U) := (sections_subring R U).to_comm_ring open prime_spectrum /-- The `stalk_to_fiber` map for the structure sheaf is surjective. (In fact, an isomorphism, as constructed below in `stalk_iso_Type`.) -/ lemma structure_sheaf_stalk_to_fiber_surjective (x : Top.of (prime_spectrum R)) : function.surjective (stalk_to_fiber (is_locally_fraction R) x) := begin apply stalk_to_fiber_surjective, intro t, obtain ⟨r, ⟨s, hs⟩, rfl⟩ := (localization.of _).mk'_surjective t, exact ⟨⟨⟨basic_open s, basic_open_open⟩, hs⟩, λ y, (localization.of _).mk' r ⟨s, y.2⟩, ⟨prelocal_predicate.sheafify_of ⟨r, s, λ y, ⟨y.2, localization_map.mk'_spec _ _ _⟩⟩, rfl⟩⟩, end /-- The `stalk_to_fiber` map for the structure sheaf is injective. (In fact, an isomorphism, as constructed below in `stalk_iso_Type`.) The proof here follows the argument in Hartshorne's Algebraic Geometry, Proposition II.2.2. -/ lemma structure_sheaf_stalk_to_fiber_injective (x : Top.of (prime_spectrum R)) : function.injective (stalk_to_fiber (is_locally_fraction R) x) := begin apply stalk_to_fiber_injective, intros U V fU hU fV hV e, rcases hU ⟨x, U.2⟩ with ⟨U', mU, iU, ⟨a, b, wU⟩⟩, rcases hV ⟨x, V.2⟩ with ⟨V', mV, iV, ⟨c, d, wV⟩⟩, have wUx := (wU ⟨x, mU⟩).2, dsimp at wUx, have wVx := (wV ⟨x, mV⟩).2, dsimp at wVx, have e' := congr_arg (λ z, z * ((localization.of _).to_map (b * d))) e, dsimp at e', simp only [←mul_assoc, ring_hom.map_mul] at e', rw [mul_right_comm (fV _)] at e', erw [wUx, wVx] at e', simp only [←ring_hom.map_mul] at e', have := @localization_map.mk'_eq_iff_eq _ _ _ _ _ (localization.of (as_ideal x).prime_compl) a c ⟨b, (wU ⟨x, mU⟩).1⟩ ⟨d, (wV ⟨x, mV⟩).1⟩, dsimp at this, rw ←this at e', rw localization_map.eq at e', rcases e' with ⟨⟨h, hh⟩, e''⟩, dsimp at e'', let Wb : opens _ := ⟨basic_open b, basic_open_open⟩, let Wd : opens _ := ⟨basic_open d, basic_open_open⟩, let Wh : opens _ := ⟨basic_open h, basic_open_open⟩, use ((Wb ⊓ Wd) ⊓ Wh) ⊓ (U' ⊓ V'), refine ⟨⟨⟨(wU ⟨x, mU⟩).1, (wV ⟨x, mV⟩).1⟩, hh⟩, ⟨mU, mV⟩⟩, refine ⟨_, _, _⟩, change _ ⟶ U.val, exact (opens.inf_le_right _ _) ≫ (opens.inf_le_left _ _) ≫ iU, change _ ⟶ V.val, exact (opens.inf_le_right _ _) ≫ (opens.inf_le_right _ _) ≫ iV, intro w, dsimp, have wU' := (wU ⟨w.1, w.2.2.1⟩).2, dsimp at wU', have wV' := (wV ⟨w.1, w.2.2.2⟩).2, dsimp at wV', -- We need to prove `fU w = fV w`. -- First we show that is suffices to prove `fU w * b * d * h = fV w * b * d * h`. -- Then we calculate (at w) as follows: -- fU w * b * d * h -- = a * d * h : wU' -- ... = c * b * h : e'' -- ... = fV w * d * b * h : wV' have u : is_unit ((localization.of (as_ideal w.1).prime_compl).to_map (b * d * h)), { simp only [ring_hom.map_mul], apply is_unit.mul, apply is_unit.mul, exact (localization.of (as_ideal w.1).prime_compl).map_units ⟨b, (wU ⟨w, w.2.2.1⟩).1⟩, exact (localization.of (as_ideal w.1).prime_compl).map_units ⟨d, (wV ⟨w, w.2.2.2⟩).1⟩, exact (localization.of (as_ideal w.1).prime_compl).map_units ⟨h, w.2.1.2⟩, }, apply (is_unit.mul_left_inj u).1, conv_rhs { rw [mul_comm b d] }, simp only [ring_hom.map_mul, ←mul_assoc], erw [wU', wV'], dsimp, simp only [←ring_hom.map_mul, ←mul_assoc], rw e'', end /-- The structure presheaf, valued in `CommRing`, constructed by dressing up the `Type` valued structure presheaf. -/ @[simps] def structure_presheaf_in_CommRing : presheaf CommRing (Spec.Top R) := { obj := λ U, CommRing.of ((structure_sheaf_in_Type R).presheaf.obj U), map := λ U V i, { to_fun := ((structure_sheaf_in_Type R).presheaf.map i), map_zero' := rfl, map_add' := λ x y, rfl, map_one' := rfl, map_mul' := λ x y, rfl, }, } /-- Some glue, verifying that that structure presheaf valued in `CommRing` agrees with the `Type` valued structure presheaf. -/ def structure_presheaf_comp_forget : structure_presheaf_in_CommRing R ⋙ (forget CommRing) ≅ (structure_sheaf_in_Type R).presheaf := nat_iso.of_components (λ U, iso.refl _) (by tidy) /-- The structure sheaf on $Spec R$, valued in `CommRing`. This is provided as a bundled `SheafedSpace` as `Spec.SheafedSpace R` later. -/ def structure_sheaf : sheaf CommRing (Spec.Top R) := { presheaf := structure_presheaf_in_CommRing R, sheaf_condition := -- We check the sheaf condition under `forget CommRing`. (sheaf_condition_equiv_sheaf_condition_comp _ _).symm (sheaf_condition_equiv_of_iso (structure_presheaf_comp_forget R).symm (structure_sheaf_in_Type R).sheaf_condition), } /-- The stalk at `x` is equivalent (just as a type) to the localization at `x`. -/ def stalk_iso_Type (x : prime_spectrum R) : (structure_sheaf_in_Type R).presheaf.stalk x ≅ localization.at_prime x.as_ideal := (equiv.of_bijective _ ⟨structure_sheaf_stalk_to_fiber_injective R x, structure_sheaf_stalk_to_fiber_surjective R x⟩).to_iso -- PROJECT: Improve this to an isomorphism of rings. /- /-- TODO: this should follow easily from `forget CommRing` preserving filtered colimits. -/ def compare_stalks (x : prime_spectrum R) : (forget CommRing).obj ((structure_sheaf R).presheaf.stalk x) ≅ (structure_sheaf_in_Type R).presheaf.stalk x := sorry /-- TODO: -/ def stalk_to_fiber_ring_hom (x : prime_spectrum R) : (structure_sheaf R).presheaf.stalk x ⟶ CommRing.of (localization.at_prime x.as_ideal) := { to_fun := (compare_stalks R x).hom ≫ stalk_to_fiber (is_locally_fraction_local R) x, map_zero' := sorry, map_add' := sorry, map_one' := sorry, map_mul' := sorry } /-- The stalk at `x` is equivalent, as a commutative ring, to the localization at `x`. -/ def stalk_iso (x : prime_spectrum R) : (structure_sheaf R).presheaf.stalk x ≅ CommRing.of (localization.at_prime x.as_ideal) := ({ ..stalk_to_fiber_ring_hom R x, ..(compare_stalks R x ≪≫ stalk_iso_Type R x).to_equiv } : _ ≃+* _).to_CommRing_iso -/ end algebraic_geometry
6d2b3c91a135553391086b5fd61a9dcc9d7905ba
54d7e71c3616d331b2ec3845d31deb08f3ff1dea
/library/init/meta/smt/rsimp.lean
c836d247e5c4e4346abfbd00d1124e1a985bb8bb
[ "Apache-2.0" ]
permissive
pachugupta/lean
6f3305c4292288311cc4ab4550060b17d49ffb1d
0d02136a09ac4cf27b5c88361750e38e1f485a1a
refs/heads/master
1,611,110,653,606
1,493,130,117,000
1,493,167,649,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,555
lean
/- Copyright (c) 2017 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.smt.smt_tactic init.meta.fun_info init.meta.rb_map open tactic private meta def add_lemma (m : transparency) (h : name) (hs : hinst_lemmas) : tactic hinst_lemmas := (do h ← hinst_lemma.mk_from_decl_core m h tt, return $ hs.add h) <|> return hs private meta def to_hinst_lemmas (m : transparency) (ex : name_set) : list name → hinst_lemmas → tactic hinst_lemmas | [] hs := return hs | (n::ns) hs := if ex.contains n then to_hinst_lemmas ns hs else let add n := add_lemma m n hs >>= to_hinst_lemmas ns in do eqns ← tactic.get_eqn_lemmas_for tt n, match eqns with | [] := add n | _ := mcond (is_prop_decl n) (add n) (to_hinst_lemmas eqns hs >>= to_hinst_lemmas ns) end /-- Create a rsimp attribute named `attr_name`, the attribute declaration is named `attr_decl_name`. The cached hinst_lemmas structure is built using the lemmas marked with simp attribute `simp_attr_name`, but *not* marked with `ex_attr_name`. We say `ex_attr_name` is the "exception set". It is useful for excluding lemmas in `simp_attr_name` which are not good or redundant for ematching. -/ meta def mk_hinst_lemma_attr_from_simp_attr (attr_decl_name attr_name : name) (simp_attr_name : name) (ex_attr_name : name) : command := do let t := ```(caching_user_attribute hinst_lemmas), v ← to_expr ``({name := %%(quote attr_name), descr := "hinst_lemma attribute derived from '" ++ to_string %%(quote simp_attr_name) ++ "'", mk_cache := λ ns, let aux := %%(quote simp_attr_name) in let ex_attr := %%(quote ex_attr_name) in do { hs ← to_hinst_lemmas reducible mk_name_set ns hinst_lemmas.mk, ss ← attribute.get_instances aux, ex ← get_name_set_for_attr ex_attr, to_hinst_lemmas reducible ex ss hs }, dependencies := [`reducibility, %%(quote simp_attr_name)]} : caching_user_attribute hinst_lemmas), add_decl (declaration.defn attr_decl_name [] t v reducibility_hints.abbrev ff), attribute.register attr_decl_name run_cmd mk_name_set_attr `no_rsimp run_cmd mk_hinst_lemma_attr_from_simp_attr `rsimp_attr `rsimp `simp `no_rsimp /- The following lemmas are not needed by rsimp, and they actually hurt performance since they generate a lot of instances. -/ attribute [no_rsimp] id.def ne.def not_true not_false_iff ne_self_iff_false eq_self_iff_true heq_self_iff_true iff_not_self not_iff_self true_iff_false false_iff_true and.comm and.assoc and.left_comm and_true true_and and_false false_and not_and_self and_not_self and_self or.comm or.assoc or.left_comm or_true true_or or_false false_or or_self iff_true true_iff iff_false false_iff iff_self implies_true_iff false_implies_iff if_t_t if_true if_false namespace rsimp meta def is_value_like : expr → bool | e := if ¬ e.is_app then ff else let fn := e.get_app_fn in if ¬ fn.is_constant then ff else let nargs := e.get_app_num_args, fname := fn.const_name in if fname = `zero ∧ nargs = 2 then tt else if fname = `one ∧ nargs = 2 then tt else if fname = `bit0 ∧ nargs = 3 then is_value_like e.app_arg else if fname = `bit1 ∧ nargs = 4 then is_value_like e.app_arg else if fname = `char.of_nat ∧ nargs = 1 then is_value_like e.app_arg else ff /-- Return the size of term by considering only explicit arguments. -/ meta def explicit_size : expr → tactic nat | e := if ¬ e.is_app then return 1 else if is_value_like e then return 1 else fold_explicit_args e 1 (λ n arg, do r ← explicit_size arg, return $ r + n) /-- Choose smallest element (with respect to explicit_size) in `e`s equivalence class. -/ meta def choose (ccs : cc_state) (e : expr) : tactic expr := do sz ← explicit_size e, p ← ccs.mfold_eqc e (e, sz) $ λ p e', if p.2 = 1 then return p else do { sz' ← explicit_size e', if sz' < p.2 then return (e', sz') else return p }, return p.1 meta def repr_map := expr_map expr meta def mk_repr_map := expr_map.mk expr meta def to_repr_map (ccs : cc_state) : tactic repr_map := ccs.roots.mfoldl (λ S e, do r ← choose ccs e, return $ S.insert e r) mk_repr_map meta def rsimplify (ccs : cc_state) (e : expr) (m : option repr_map := none) : tactic (expr × expr) := do m ← match m with | none := to_repr_map ccs | some m := return m end, r ← simplify_top_down () (λ _ t, do root ← return $ ccs.root t, new_t ← m.find root, guard (¬ new_t =ₐ t), prf ← ccs.eqv_proof t new_t, return ((), new_t, prf)) e, return r.2 structure config := (attr_name := `rsimp_attr) (max_rounds := 8) open smt_tactic meta def collect_implied_eqs (cfg : config := {}) (extra := hinst_lemmas.mk) : tactic cc_state := do focus1 $ using_smt_with {em_attr := cfg.attr_name} $ do add_lemmas_from_facts, add_lemmas extra, repeat_at_most cfg.max_rounds (ematch >> try smt_tactic.close), (now >> return cc_state.mk) <|> to_cc_state meta def rsimplify_goal (ccs : cc_state) (m : option repr_map := none) : tactic unit := do t ← target, (new_t, pr) ← rsimplify ccs t m, try (replace_target new_t pr) meta def rsimplify_at (ccs : cc_state) (h : expr) (m : option repr_map := none) : tactic unit := do when (expr.is_local_constant h = ff) (tactic.fail "tactic rsimplify_at failed, the given expression is not a hypothesis"), htype ← infer_type h, (new_htype, heq) ← rsimplify ccs htype m, try $ do assert (expr.local_pp_name h) new_htype, mk_eq_mp heq h >>= exact, try $ clear h end rsimp open rsimp namespace tactic meta def rsimp (cfg : config := {}) (extra := hinst_lemmas.mk) : tactic unit := do ccs ← collect_implied_eqs cfg extra, try $ rsimplify_goal ccs meta def rsimp_at (h : expr) (cfg : config := {}) (extra := hinst_lemmas.mk) : tactic unit := do ccs ← collect_implied_eqs cfg extra, try $ rsimplify_at ccs h namespace interactive /- TODO(Leo): allow user to provide extra lemmas manually -/ meta def rsimp : tactic unit := tactic.rsimp end interactive end tactic
109260f85caa1665498a85b21fcaa14f0a14151b
0dbd5f7001f62ee8d54ed48bada66bfeaf55e550
/src/data/nat/exactly_divides_old.lean
a2eedf8ee2fabe0d897902ee2f9102311fc17459
[]
no_license
rwbarton/lean-elementary-number-theory
667203b08501792eef48217759539f6c1e2da25a
fabef0737fd2486e3f24f9e04652db4c182d5425
refs/heads/master
1,670,605,651,029
1,599,565,470,000
1,599,565,470,000
293,792,043
2
0
null
null
null
null
UTF-8
Lean
false
false
2,179
lean
/- lemma divides_p_times (r : ℕ) (n : ℕ) : p^r ∣ n ↔ p^(r+1) ∣ (p * n) := calc p^r ∣ n ↔ (p * p^r) ∣ (p * n) : (nat.mul_dvd_mul_iff_left (gt_zero hp)).symm ... = (p^r * p ∣ p * n) : by rw mul_comm lemma exactly_divides_p_times (r : ℕ) (n : ℕ) : p^r ∣∣ n ↔ p^(r+1) ∣∣ (p * n) := have eq : (p * n) / p^(r+1) = n / p^r, from calc (p * n) / p^(r+1) = (p * n) / (p^r * p) : rfl ... = (p * n) / (p * p^r) : by rw mul_comm (p^r) p ... = (p * n) / p / p^r : by rw nat.div_div_eq_div_mul ... = n / p^r : by rw (nat.mul_div_cancel_left n (gt_zero hp)), -- : congr_arg (λ m, m / p^r) (nat.mul_div_cancel_left n gt_zero), and_congr (divides_p_times hp r n) (by rw eq) lemma exactly_divides' {r : ℕ} {n : ℕ} : p^r ∣∣ n → (∀ i, p^i ∣ n ↔ i ≤ r) := begin intros prn i, apply iff.intro, { intro pin, apply (le_or_gt _ _).resolve_right, intro i_gt_r, have := calc p^r * p = p^(r+1) : rfl ... ∣ p^i : pow_dvd_pow p i_gt_r ... ∣ n : pin, exact absurd (dvd_div_of_mul_dvd (pos_pow_of_pos r (gt_zero hp)) this) prn.right, }, { intro i_le_r, exact dvd.trans (pow_dvd_pow p i_le_r) (and.left prn) } end -/ /- lemma exactly_divides_mul (r s a b : ℕ) : p^r ∣∣ a → p^s ∣∣ b → p^(r+s) ∣∣ a*b := begin intros pra psb, split, { rw nat.pow_add, exact mul_dvd_mul pra.left psb.left }, let a' := a / p^r, have ha : p^r * a' = a := nat.mul_div_cancel' pra.left, let b' := b / p^s, have hb : p^s * b' = b := nat.mul_div_cancel' psb.left, have : (a * b) / (p^(r+s)) = (a / p^r) * (b / p^s) := calc (a * b) / (p^(r+s)) = ((p^r * a') * (p^s * b')) / (p^r * p^s) : by rw [ha, hb, pow_add] ... = ((p^r * p^s) * (a' * b')) / (p^r * p^s) : by ac_refl ... = a' * b' : by rw nat.mul_div_cancel_left _ (mul_pos (pos_pow_of_pos r (gt_zero hp)) (pos_pow_of_pos s (gt_zero hp))) ... = (a / p^r) * (b / p^s) : rfl, rw this, exact prime.not_dvd_mul hp pra.right psb.right end -/
c7023fa8ba42ec0a79fce8b3e6a7e98be4d7d1b4
9dc8cecdf3c4634764a18254e94d43da07142918
/src/ring_theory/polynomial/bernstein.lean
3fc5d067c27d408b9eadd7e7bdf5633d643b1507
[ "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
15,845
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import data.polynomial.derivative import data.nat.choose.sum import ring_theory.polynomial.pochhammer import data.polynomial.algebra_map import linear_algebra.linear_independent import data.mv_polynomial.pderiv /-! # Bernstein polynomials The definition of the Bernstein polynomials ``` bernstein_polynomial (R : Type*) [comm_ring R] (n ν : ℕ) : R[X] := (choose n ν) * X^ν * (1 - X)^(n - ν) ``` and the fact that for `ν : fin (n+1)` these are linearly independent over `ℚ`. We prove the basic identities * `(finset.range (n + 1)).sum (λ ν, bernstein_polynomial R n ν) = 1` * `(finset.range (n + 1)).sum (λ ν, ν • bernstein_polynomial R n ν) = n • X` * `(finset.range (n + 1)).sum (λ ν, (ν * (ν-1)) • bernstein_polynomial R n ν) = (n * (n-1)) • X^2` ## Notes See also `analysis.special_functions.bernstein`, which defines the Bernstein approximations of a continuous function `f : C([0,1], ℝ)`, and shows that these converge uniformly to `f`. -/ noncomputable theory open nat (choose) open polynomial (X) open_locale big_operators polynomial variables (R : Type*) [comm_ring R] /-- `bernstein_polynomial R n ν` is `(choose n ν) * X^ν * (1 - X)^(n - ν)`. Although the coefficients are integers, it is convenient to work over an arbitrary commutative ring. -/ def bernstein_polynomial (n ν : ℕ) : R[X] := choose n ν * X^ν * (1 - X)^(n - ν) example : bernstein_polynomial ℤ 3 2 = 3 * X^2 - 3 * X^3 := begin norm_num [bernstein_polynomial, choose], ring, end namespace bernstein_polynomial lemma eq_zero_of_lt {n ν : ℕ} (h : n < ν) : bernstein_polynomial R n ν = 0 := by simp [bernstein_polynomial, nat.choose_eq_zero_of_lt h] section variables {R} {S : Type*} [comm_ring S] @[simp] lemma map (f : R →+* S) (n ν : ℕ) : (bernstein_polynomial R n ν).map f = bernstein_polynomial S n ν := by simp [bernstein_polynomial] end lemma flip (n ν : ℕ) (h : ν ≤ n) : (bernstein_polynomial R n ν).comp (1-X) = bernstein_polynomial R n (n-ν) := begin dsimp [bernstein_polynomial], simp [h, tsub_tsub_assoc, mul_right_comm], end lemma flip' (n ν : ℕ) (h : ν ≤ n) : bernstein_polynomial R n ν = (bernstein_polynomial R n (n-ν)).comp (1-X) := begin rw [←flip _ _ _ h, polynomial.comp_assoc], simp, end lemma eval_at_0 (n ν : ℕ) : (bernstein_polynomial R n ν).eval 0 = if ν = 0 then 1 else 0 := begin dsimp [bernstein_polynomial], split_ifs, { subst h, simp, }, { simp [zero_pow (nat.pos_of_ne_zero h)], }, end lemma eval_at_1 (n ν : ℕ) : (bernstein_polynomial R n ν).eval 1 = if ν = n then 1 else 0 := begin dsimp [bernstein_polynomial], split_ifs, { subst h, simp, }, { obtain w | w := (n - ν).eq_zero_or_pos, { simp [nat.choose_eq_zero_of_lt ((tsub_eq_zero_iff_le.mp w).lt_of_ne (ne.symm h))] }, { simp [zero_pow w] } }, end. lemma derivative_succ_aux (n ν : ℕ) : (bernstein_polynomial R (n+1) (ν+1)).derivative = (n+1) * (bernstein_polynomial R n ν - bernstein_polynomial R n (ν + 1)) := begin dsimp [bernstein_polynomial], suffices : ↑((n + 1).choose (ν + 1)) * ((↑ν + 1) * X ^ ν) * (1 - X) ^ (n - ν) -(↑((n + 1).choose (ν + 1)) * X ^ (ν + 1) * (↑(n - ν) * (1 - X) ^ (n - ν - 1))) = (↑n + 1) * (↑(n.choose ν) * X ^ ν * (1 - X) ^ (n - ν) - ↑(n.choose (ν + 1)) * X ^ (ν + 1) * (1 - X) ^ (n - (ν + 1))), { simpa [polynomial.derivative_pow, ←sub_eq_add_neg], }, conv_rhs { rw mul_sub, }, -- We'll prove the two terms match up separately. refine congr (congr_arg has_sub.sub _) _, { simp only [←mul_assoc], refine congr (congr_arg (*) (congr (congr_arg (*) _) rfl)) rfl, -- Now it's just about binomial coefficients exact_mod_cast congr_arg (λ m : ℕ, (m : R[X])) (nat.succ_mul_choose_eq n ν).symm, }, { rw [← tsub_add_eq_tsub_tsub, ← mul_assoc, ← mul_assoc], congr' 1, rw mul_comm , rw [←mul_assoc,←mul_assoc], congr' 1, norm_cast, congr' 1, convert (nat.choose_mul_succ_eq n (ν + 1)).symm using 1, { convert mul_comm _ _ using 2, simp, }, { apply mul_comm, }, }, end lemma derivative_succ (n ν : ℕ) : (bernstein_polynomial R n (ν+1)).derivative = n * (bernstein_polynomial R (n-1) ν - bernstein_polynomial R (n-1) (ν+1)) := begin cases n, { simp [bernstein_polynomial], }, { rw nat.cast_succ, apply derivative_succ_aux, } end lemma derivative_zero (n : ℕ) : (bernstein_polynomial R n 0).derivative = -n * bernstein_polynomial R (n-1) 0 := begin dsimp [bernstein_polynomial], simp [polynomial.derivative_pow], end lemma iterate_derivative_at_0_eq_zero_of_lt (n : ℕ) {ν k : ℕ} : k < ν → (polynomial.derivative^[k] (bernstein_polynomial R n ν)).eval 0 = 0 := begin cases ν, { rintro ⟨⟩, }, { rw nat.lt_succ_iff, induction k with k ih generalizing n ν, { simp [eval_at_0], }, { simp only [derivative_succ, int.coe_nat_eq_zero, mul_eq_zero, function.comp_app, function.iterate_succ, polynomial.iterate_derivative_sub, polynomial.iterate_derivative_nat_cast_mul, polynomial.eval_mul, polynomial.eval_nat_cast, polynomial.eval_sub], intro h, apply mul_eq_zero_of_right, rw [ih _ _ (nat.le_of_succ_le h), sub_zero], convert ih _ _ (nat.pred_le_pred h), exact (nat.succ_pred_eq_of_pos (k.succ_pos.trans_le h)).symm } }, end @[simp] lemma iterate_derivative_succ_at_0_eq_zero (n ν : ℕ) : (polynomial.derivative^[ν] (bernstein_polynomial R n (ν+1))).eval 0 = 0 := iterate_derivative_at_0_eq_zero_of_lt R n (lt_add_one ν) open polynomial @[simp] lemma iterate_derivative_at_0 (n ν : ℕ) : (polynomial.derivative^[ν] (bernstein_polynomial R n ν)).eval 0 = (pochhammer R ν).eval (n - (ν - 1) : ℕ) := begin by_cases h : ν ≤ n, { induction ν with ν ih generalizing n h, { simp [eval_at_0], }, { have h' : ν ≤ n-1 := le_tsub_of_add_le_right h, simp only [derivative_succ, ih (n-1) h', iterate_derivative_succ_at_0_eq_zero, nat.succ_sub_succ_eq_sub, tsub_zero, sub_zero, iterate_derivative_sub, iterate_derivative_nat_cast_mul, eval_one, eval_mul, eval_add, eval_sub, eval_X, eval_comp, eval_nat_cast, function.comp_app, function.iterate_succ, pochhammer_succ_left], obtain rfl | h'' := ν.eq_zero_or_pos, { simp }, { have : n - 1 - (ν - 1) = n - ν, { rw ←nat.succ_le_iff at h'', rw [← tsub_add_eq_tsub_tsub, add_comm, tsub_add_cancel_of_le h''] }, rw [this, pochhammer_eval_succ], rw_mod_cast tsub_add_cancel_of_le (h'.trans n.pred_le) } } }, { simp only [not_le] at h, rw [tsub_eq_zero_iff_le.mpr (nat.le_pred_of_lt h), eq_zero_of_lt R h], simp [pos_iff_ne_zero.mp (pos_of_gt h)] }, end lemma iterate_derivative_at_0_ne_zero [char_zero R] (n ν : ℕ) (h : ν ≤ n) : (polynomial.derivative^[ν] (bernstein_polynomial R n ν)).eval 0 ≠ 0 := begin simp only [int.coe_nat_eq_zero, bernstein_polynomial.iterate_derivative_at_0, ne.def, nat.cast_eq_zero], simp only [←pochhammer_eval_cast], norm_cast, apply ne_of_gt, obtain rfl|h' := nat.eq_zero_or_pos ν, { simp, }, { rw ← nat.succ_pred_eq_of_pos h' at h, exact pochhammer_pos _ _ (tsub_pos_of_lt (nat.lt_of_succ_le h)) } end /-! Rather than redoing the work of evaluating the derivatives at 1, we use the symmetry of the Bernstein polynomials. -/ lemma iterate_derivative_at_1_eq_zero_of_lt (n : ℕ) {ν k : ℕ} : k < n - ν → (polynomial.derivative^[k] (bernstein_polynomial R n ν)).eval 1 = 0 := begin intro w, rw flip' _ _ _ (tsub_pos_iff_lt.mp (pos_of_gt w)).le, simp [polynomial.eval_comp, iterate_derivative_at_0_eq_zero_of_lt R n w], end @[simp] lemma iterate_derivative_at_1 (n ν : ℕ) (h : ν ≤ n) : (polynomial.derivative^[n-ν] (bernstein_polynomial R n ν)).eval 1 = (-1)^(n-ν) * (pochhammer R (n - ν)).eval (ν + 1) := begin rw flip' _ _ _ h, simp [polynomial.eval_comp, h], obtain rfl | h' := h.eq_or_lt, { simp, }, { congr, norm_cast, rw [← tsub_add_eq_tsub_tsub, tsub_tsub_cancel_of_le (nat.succ_le_iff.mpr h')] }, end lemma iterate_derivative_at_1_ne_zero [char_zero R] (n ν : ℕ) (h : ν ≤ n) : (polynomial.derivative^[n-ν] (bernstein_polynomial R n ν)).eval 1 ≠ 0 := begin rw [bernstein_polynomial.iterate_derivative_at_1 _ _ _ h, ne.def, neg_one_pow_mul_eq_zero_iff, ←nat.cast_succ, ←pochhammer_eval_cast, ←nat.cast_zero, nat.cast_inj], exact (pochhammer_pos _ _ (nat.succ_pos ν)).ne', end open submodule lemma linear_independent_aux (n k : ℕ) (h : k ≤ n + 1): linear_independent ℚ (λ ν : fin k, bernstein_polynomial ℚ n ν) := begin induction k with k ih, { apply linear_independent_empty_type, }, { apply linear_independent_fin_succ'.mpr, fsplit, { exact ih (le_of_lt h), }, { -- The actual work! -- We show that the (n-k)-th derivative at 1 doesn't vanish, -- but vanishes for everything in the span. clear ih, simp only [nat.succ_eq_add_one, add_le_add_iff_right] at h, simp only [fin.coe_last, fin.init_def], dsimp, apply not_mem_span_of_apply_not_mem_span_image ((@polynomial.derivative ℚ _)^(n-k)), simp only [not_exists, not_and, submodule.mem_map, submodule.span_image], intros p m, apply_fun (polynomial.eval (1 : ℚ)), simp only [linear_map.pow_apply], -- The right hand side is nonzero, -- so it will suffice to show the left hand side is always zero. suffices : (polynomial.derivative^[n-k] p).eval 1 = 0, { rw [this], exact (iterate_derivative_at_1_ne_zero ℚ n k h).symm, }, apply span_induction m, { simp, rintro ⟨a, w⟩, simp only [fin.coe_mk], rw [iterate_derivative_at_1_eq_zero_of_lt ℚ n ((tsub_lt_tsub_iff_left_of_le h).mpr w)] }, { simp, }, { intros x y hx hy, simp [hx, hy], }, { intros a x h, simp [h], }, }, }, end /-- The Bernstein polynomials are linearly independent. We prove by induction that the collection of `bernstein_polynomial n ν` for `ν = 0, ..., k` are linearly independent. The inductive step relies on the observation that the `(n-k)`-th derivative, evaluated at 1, annihilates `bernstein_polynomial n ν` for `ν < k`, but has a nonzero value at `ν = k`. -/ lemma linear_independent (n : ℕ) : linear_independent ℚ (λ ν : fin (n+1), bernstein_polynomial ℚ n ν) := linear_independent_aux n (n+1) le_rfl lemma sum (n : ℕ) : ∑ ν in finset.range (n + 1), bernstein_polynomial R n ν = 1 := calc ∑ ν in finset.range (n + 1), bernstein_polynomial R n ν = (X + (1 - X)) ^ n : by { rw add_pow, simp only [bernstein_polynomial, mul_comm, mul_assoc, mul_left_comm] } ... = 1 : by simp open polynomial open mv_polynomial lemma sum_smul (n : ℕ) : ∑ ν in finset.range (n + 1), ν • bernstein_polynomial R n ν = n • X := begin -- We calculate the `x`-derivative of `(x+y)^n`, evaluated at `y=(1-x)`, -- either directly or by using the binomial theorem. -- We'll work in `mv_polynomial bool R`. let x : mv_polynomial bool R := mv_polynomial.X tt, let y : mv_polynomial bool R := mv_polynomial.X ff, have pderiv_tt_x : pderiv tt x = 1, { simp [x], }, have pderiv_tt_y : pderiv tt y = 0, { simp [pderiv_X, y], }, let e : bool → R[X] := λ i, cond i X (1-X), -- Start with `(x+y)^n = (x+y)^n`, -- take the `x`-derivative, evaluate at `x=X, y=1-X`, and multiply by `X`: have h : (x+y)^n = (x+y)^n := rfl, apply_fun (pderiv tt) at h, apply_fun (aeval e) at h, apply_fun (λ p, p * X) at h, -- On the left hand side we'll use the binomial theorem, then simplify. -- We first prepare a tedious rewrite: have w : ∀ k : ℕ, ↑k * polynomial.X ^ (k - 1) * (1 - polynomial.X) ^ (n - k) * ↑(n.choose k) * polynomial.X = k • bernstein_polynomial R n k, { rintro (_|k), { simp, }, { dsimp [bernstein_polynomial], simp only [←nat_cast_mul, nat.succ_eq_add_one, nat.add_succ_sub_one, add_zero, pow_succ], push_cast, ring, }, }, conv at h { to_lhs, rw [add_pow, (pderiv tt).map_sum, (mv_polynomial.aeval e).map_sum, finset.sum_mul], -- Step inside the sum: apply_congr, skip, simp [pderiv_mul, pderiv_tt_x, pderiv_tt_y, e, w], }, -- On the right hand side, we'll just simplify. conv at h { to_rhs, rw [(pderiv tt).leibniz_pow, (pderiv tt).map_add, pderiv_tt_x, pderiv_tt_y], simp [e] }, simpa using h, end lemma sum_mul_smul (n : ℕ) : ∑ ν in finset.range (n + 1), (ν * (ν-1)) • bernstein_polynomial R n ν = (n * (n-1)) • X^2 := begin -- We calculate the second `x`-derivative of `(x+y)^n`, evaluated at `y=(1-x)`, -- either directly or by using the binomial theorem. -- We'll work in `mv_polynomial bool R`. let x : mv_polynomial bool R := mv_polynomial.X tt, let y : mv_polynomial bool R := mv_polynomial.X ff, have pderiv_tt_x : pderiv tt x = 1, { simp [x], }, have pderiv_tt_y : pderiv tt y = 0, { simp [pderiv_X, y], }, let e : bool → R[X] := λ i, cond i X (1-X), -- Start with `(x+y)^n = (x+y)^n`, -- take the second `x`-derivative, evaluate at `x=X, y=1-X`, and multiply by `X`: have h : (x+y)^n = (x+y)^n := rfl, apply_fun (pderiv tt) at h, apply_fun (pderiv tt) at h, apply_fun (aeval e) at h, apply_fun (λ p, p * X^2) at h, -- On the left hand side we'll use the binomial theorem, then simplify. -- We first prepare a tedious rewrite: have w : ∀ k : ℕ, ↑k * (↑(k-1) * polynomial.X ^ (k - 1 - 1)) * (1 - polynomial.X) ^ (n - k) * ↑(n.choose k) * polynomial.X^2 = (k * (k-1)) • bernstein_polynomial R n k, { rintro (_|k), { simp, }, { rcases k with (_|k), { simp, }, { dsimp [bernstein_polynomial], simp only [←nat_cast_mul, nat.succ_eq_add_one, nat.add_succ_sub_one, add_zero, pow_succ], push_cast, ring, }, }, }, conv at h { to_lhs, rw [add_pow, (pderiv tt).map_sum, (pderiv tt).map_sum, (mv_polynomial.aeval e).map_sum, finset.sum_mul], -- Step inside the sum: apply_congr, skip, simp [pderiv_mul, pderiv_tt_x, pderiv_tt_y, e, w] }, -- On the right hand side, we'll just simplify. conv at h { to_rhs, simp only [pderiv_one, pderiv_mul, (pderiv _).leibniz_pow, (pderiv _).map_coe_nat, (pderiv tt).map_add, pderiv_tt_x, pderiv_tt_y], simp [e, smul_smul] }, simpa using h, end /-- A certain linear combination of the previous three identities, which we'll want later. -/ lemma variance (n : ℕ) : ∑ ν in finset.range (n+1), (n • polynomial.X - ν)^2 * bernstein_polynomial R n ν = n • polynomial.X * (1 - polynomial.X) := begin have p : (finset.range (n+1)).sum (λ ν, (ν * (ν-1)) • bernstein_polynomial R n ν) + (1 - (2 * n) • polynomial.X) * (finset.range (n+1)).sum (λ ν, ν • bernstein_polynomial R n ν) + (n^2 • X^2) * (finset.range (n+1)).sum (λ ν, bernstein_polynomial R n ν) = _ := rfl, conv at p { to_lhs, rw [finset.mul_sum, finset.mul_sum, ←finset.sum_add_distrib, ←finset.sum_add_distrib], simp only [←nat_cast_mul], simp only [←mul_assoc], simp only [←add_mul], }, conv at p { to_rhs, rw [sum, sum_smul, sum_mul_smul, ←nat_cast_mul], }, calc _ = _ : finset.sum_congr rfl (λ k m, _) ... = _ : p ... = _ : _, { congr' 1, simp only [←nat_cast_mul] with push_cast, cases k; { simp, ring, }, }, { simp only [←nat_cast_mul] with push_cast, cases n, { simp, }, { simp, ring, }, }, end end bernstein_polynomial
d59faefcac063230ca0272b0486037713ec98b87
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/finset/image.lean
e41fa0925240ec6190d1ab56d83527b8afac6a7d
[ "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
27,108
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro -/ import algebra.hom.embedding import data.fin.basic import data.finset.basic import data.int.order.basic /-! # Image and map operations on finite sets > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Thie file provides the finite analog of `set.image`, along with some other similar functions. Note there are two ways to take the image over a finset; via `finset.image` which applies the function then removes duplicates (requiring `decidable_eq`), or via `finset.map` which exploits injectivity of the function to avoid needing to deduplicate. Choosing between these is similar to choosing between `insert` and `finset.cons`, or between `finset.union` and `finset.disj_union`. ## Main definitions * `finset.image`: Given a function `f : α → β`, `s.image f` is the image finset in `β`. * `finset.map`: Given an embedding `f : α ↪ β`, `s.map f` is the image finset in `β`. * `finset.subtype`: `s.subtype p` is the the finset of `subtype p` whose elements belong to `s`. * `finset.fin`:`s.fin n` is the finset of all elements of `s` less than `n`. -/ variables {α β γ : Type*} open multiset open function namespace finset /-! ### map -/ section map open function /-- When `f` is an embedding of `α` in `β` and `s` is a finset in `α`, then `s.map f` is the image finset in `β`. The embedding condition guarantees that there are no duplicates in the image. -/ def map (f : α ↪ β) (s : finset α) : finset β := ⟨s.1.map f, s.2.map f.2⟩ @[simp] theorem map_val (f : α ↪ β) (s : finset α) : (map f s).1 = s.1.map f := rfl @[simp] theorem map_empty (f : α ↪ β) : (∅ : finset α).map f = ∅ := rfl variables {f : α ↪ β} {s : finset α} @[simp] theorem mem_map {b : β} : b ∈ s.map f ↔ ∃ a ∈ s, f a = b := mem_map.trans $ by simp only [exists_prop]; refl @[simp] lemma mem_map_equiv {f : α ≃ β} {b : β} : b ∈ s.map f.to_embedding ↔ f.symm b ∈ s := by { rw mem_map, exact ⟨by { rintro ⟨a, H, rfl⟩, simpa }, λ h, ⟨_, h, by simp⟩⟩ } lemma mem_map' (f : α ↪ β) {a} {s : finset α} : f a ∈ s.map f ↔ a ∈ s := mem_map_of_injective f.2 lemma mem_map_of_mem (f : α ↪ β) {a} {s : finset α} : a ∈ s → f a ∈ s.map f := (mem_map' _).2 lemma forall_mem_map {f : α ↪ β} {s : finset α} {p : Π a, a ∈ s.map f → Prop} : (∀ y ∈ s.map f, p y H) ↔ ∀ x ∈ s, p (f x) (mem_map_of_mem _ H) := ⟨λ h y hy, h (f y) (mem_map_of_mem _ hy), λ h x hx, by { obtain ⟨y, hy, rfl⟩ := mem_map.1 hx, exact h _ hy }⟩ lemma apply_coe_mem_map (f : α ↪ β) (s : finset α) (x : s) : f x ∈ s.map f := mem_map_of_mem f x.prop @[simp, norm_cast] theorem coe_map (f : α ↪ β) (s : finset α) : (s.map f : set β) = f '' s := set.ext $ λ x, mem_map.trans set.mem_image_iff_bex.symm theorem coe_map_subset_range (f : α ↪ β) (s : finset α) : (s.map f : set β) ⊆ set.range f := calc ↑(s.map f) = f '' s : coe_map f s ... ⊆ set.range f : set.image_subset_range f ↑s /-- If the only elements outside `s` are those left fixed by `σ`, then mapping by `σ` has no effect. -/ lemma map_perm {σ : equiv.perm α} (hs : {a | σ a ≠ a} ⊆ s) : s.map (σ : α ↪ α) = s := coe_injective $ (coe_map _ _).trans $ set.image_perm hs theorem map_to_finset [decidable_eq α] [decidable_eq β] {s : multiset α} : s.to_finset.map f = (s.map f).to_finset := ext $ λ _, by simp only [mem_map, multiset.mem_map, exists_prop, multiset.mem_to_finset] @[simp] theorem map_refl : s.map (embedding.refl _) = s := ext $ λ _, by simpa only [mem_map, exists_prop] using exists_eq_right @[simp] theorem map_cast_heq {α β} (h : α = β) (s : finset α) : s.map (equiv.cast h).to_embedding == s := by { subst h, simp } theorem map_map (f : α ↪ β) (g : β ↪ γ) (s : finset α) : (s.map f).map g = s.map (f.trans g) := eq_of_veq $ by simp only [map_val, multiset.map_map]; refl lemma map_comm {β'} {f : β ↪ γ} {g : α ↪ β} {f' : α ↪ β'} {g' : β' ↪ γ} (h_comm : ∀ a, f (g a) = g' (f' a)) : (s.map g).map f = (s.map f').map g' := by simp_rw [map_map, embedding.trans, function.comp, h_comm] lemma _root_.function.semiconj.finset_map {f : α ↪ β} {ga : α ↪ α} {gb : β ↪ β} (h : function.semiconj f ga gb) : function.semiconj (map f) (map ga) (map gb) := λ s, map_comm h lemma _root_.function.commute.finset_map {f g : α ↪ α} (h : function.commute f g) : function.commute (map f) (map g) := h.finset_map @[simp] theorem map_subset_map {s₁ s₂ : finset α} : s₁.map f ⊆ s₂.map f ↔ s₁ ⊆ s₂ := ⟨λ h x xs, (mem_map' _).1 $ h $ (mem_map' f).2 xs, λ h, by simp [subset_def, map_subset_map h]⟩ /-- Associate to an embedding `f` from `α` to `β` the order embedding that maps a finset to its image under `f`. -/ def map_embedding (f : α ↪ β) : finset α ↪o finset β := order_embedding.of_map_le_iff (map f) (λ _ _, map_subset_map) @[simp] theorem map_inj {s₁ s₂ : finset α} : s₁.map f = s₂.map f ↔ s₁ = s₂ := (map_embedding f).injective.eq_iff lemma map_injective (f : α ↪ β) : injective (map f) := (map_embedding f).injective @[simp] theorem map_embedding_apply : map_embedding f s = map f s := rfl lemma filter_map {p : β → Prop} [decidable_pred p] : (s.map f).filter p = (s.filter (p ∘ f)).map f := eq_of_veq (map_filter _ _ _) lemma map_filter {f : α ≃ β} {p : α → Prop} [decidable_pred p] : (s.filter p).map f.to_embedding = (s.map f.to_embedding).filter (p ∘ f.symm) := by simp only [filter_map, function.comp, equiv.to_embedding_apply, equiv.symm_apply_apply] @[simp] lemma disjoint_map {s t : finset α} (f : α ↪ β) : disjoint (s.map f) (t.map f) ↔ disjoint s t := begin simp only [disjoint_iff_ne, mem_map, exists_prop, exists_imp_distrib, and_imp], refine ⟨λ h a ha b hb hab, h _ _ ha rfl _ _ hb rfl $ congr_arg _ hab, _⟩, rintro h _ a ha rfl _ b hb rfl, exact f.injective.ne (h _ ha _ hb), end theorem map_disj_union {f : α ↪ β} (s₁ s₂ : finset α) (h) (h' := (disjoint_map _).mpr h) : (s₁.disj_union s₂ h).map f = (s₁.map f).disj_union (s₂.map f) h' := eq_of_veq $ multiset.map_add _ _ _ /-- A version of `finset.map_disj_union` for writing in the other direction. -/ theorem map_disj_union' {f : α ↪ β} (s₁ s₂ : finset α) (h') (h := (disjoint_map _).mp h') : (s₁.disj_union s₂ h).map f = (s₁.map f).disj_union (s₂.map f) h' := map_disj_union _ _ _ theorem map_union [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).map f = s₁.map f ∪ s₂.map f := coe_injective $ by simp only [coe_map, coe_union, set.image_union] theorem map_inter [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∩ s₂).map f = s₁.map f ∩ s₂.map f := coe_injective $ by simp only [coe_map, coe_inter, set.image_inter f.injective] @[simp] theorem map_singleton (f : α ↪ β) (a : α) : map f {a} = {f a} := coe_injective $ by simp only [coe_map, coe_singleton, set.image_singleton] @[simp] lemma map_insert [decidable_eq α] [decidable_eq β] (f : α ↪ β) (a : α) (s : finset α) : (insert a s).map f = insert (f a) (s.map f) := by simp only [insert_eq, map_union, map_singleton] @[simp] lemma map_cons (f : α ↪ β) (a : α) (s : finset α) (ha : a ∉ s) : (cons a s ha).map f = cons (f a) (s.map f) (by simpa using ha) := eq_of_veq $ multiset.map_cons f a s.val @[simp] theorem map_eq_empty : s.map f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_map_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ @[simp] lemma map_nonempty : (s.map f).nonempty ↔ s.nonempty := by rw [nonempty_iff_ne_empty, nonempty_iff_ne_empty, ne.def, map_eq_empty] alias map_nonempty ↔ _ nonempty.map lemma attach_map_val {s : finset α} : s.attach.map (embedding.subtype _) = s := eq_of_veq $ by rw [map_val, attach_val]; exact attach_map_val _ lemma disjoint_range_add_left_embedding (a b : ℕ) : disjoint (range a) (map (add_left_embedding a) (range b)) := begin refine disjoint_iff_inf_le.mpr _, intros k hk, simp only [exists_prop, mem_range, inf_eq_inter, mem_map, add_left_embedding_apply, mem_inter] at hk, obtain ⟨a, haQ, ha⟩ := hk.2, simpa [← ha] using hk.1, end lemma disjoint_range_add_right_embedding (a b : ℕ) : disjoint (range a) (map (add_right_embedding a) (range b)) := begin refine disjoint_iff_inf_le.mpr _, intros k hk, simp only [exists_prop, mem_range, inf_eq_inter, mem_map, add_left_embedding_apply, mem_inter] at hk, obtain ⟨a, haQ, ha⟩ := hk.2, simpa [← ha] using hk.1, end theorem map_disj_Union {f : α ↪ β} {s : finset α} {t : β → finset γ} {h} : (s.map f).disj_Union t h = s.disj_Union (λa, t (f a)) (λ a ha b hb hab, h (mem_map_of_mem _ ha) (mem_map_of_mem _ hb) (f.injective.ne hab)) := eq_of_veq $ multiset.bind_map _ _ _ theorem disj_Union_map {s : finset α} {t : α → finset β} {f : β ↪ γ} {h} : (s.disj_Union t h).map f = s.disj_Union (λa, (t a).map f) (λ a ha b hb hab, disjoint_left.mpr $ λ x hxa hxb, begin obtain ⟨xa, hfa, rfl⟩ := mem_map.mp hxa, obtain ⟨xb, hfb, hfab⟩ := mem_map.mp hxb, obtain rfl := f.injective hfab, exact disjoint_left.mp (h ha hb hab) hfa hfb, end) := eq_of_veq $ multiset.map_bind _ _ _ end map lemma range_add_one' (n : ℕ) : range (n + 1) = insert 0 ((range n).map ⟨λi, i + 1, assume i j, nat.succ.inj⟩) := by ext (⟨⟩ | ⟨n⟩); simp [nat.succ_eq_add_one, nat.zero_lt_succ n] /-! ### image -/ section image variables [decidable_eq β] /-- `image f s` is the forward image of `s` under `f`. -/ def image (f : α → β) (s : finset α) : finset β := (s.1.map f).to_finset @[simp] theorem image_val (f : α → β) (s : finset α) : (image f s).1 = (s.1.map f).dedup := rfl @[simp] theorem image_empty (f : α → β) : (∅ : finset α).image f = ∅ := rfl variables {f g : α → β} {s : finset α} {t : finset β} {a : α} {b c : β} @[simp] lemma mem_image : b ∈ s.image f ↔ ∃ a ∈ s, f a = b := by simp only [mem_def, image_val, mem_dedup, multiset.mem_map, exists_prop] lemma mem_image_of_mem (f : α → β) {a} (h : a ∈ s) : f a ∈ s.image f := mem_image.2 ⟨_, h, rfl⟩ lemma forall_image {p : β → Prop} : (∀ b ∈ s.image f, p b) ↔ ∀ a ∈ s, p (f a) := by simp only [mem_image, forall_exists_index, forall_apply_eq_imp_iff₂] @[simp] lemma mem_image_const : c ∈ s.image (const α b) ↔ s.nonempty ∧ b = c := by { rw mem_image, simp only [exists_prop, const_apply, exists_and_distrib_right], refl } lemma mem_image_const_self : b ∈ s.image (const α b) ↔ s.nonempty := mem_image_const.trans $ and_iff_left rfl instance can_lift (c) (p) [can_lift β α c p] : can_lift (finset β) (finset α) (image c) (λ s, ∀ x ∈ s, p x) := { prf := begin rintro ⟨⟨l⟩, hd : l.nodup⟩ hl, lift l to list α using hl, exact ⟨⟨l, hd.of_map _⟩, ext $ λ a, by simp⟩, end } lemma image_congr (h : (s : set α).eq_on f g) : finset.image f s = finset.image g s := by { ext, simp_rw mem_image, exact bex_congr (λ x hx, by rw h hx) } lemma _root_.function.injective.mem_finset_image (hf : injective f) : f a ∈ s.image f ↔ a ∈ s := begin refine ⟨λ h, _, finset.mem_image_of_mem f⟩, obtain ⟨y, hy, heq⟩ := mem_image.1 h, exact hf heq ▸ hy, end lemma filter_mem_image_eq_image (f : α → β) (s : finset α) (t : finset β) (h : ∀ x ∈ s, f x ∈ t) : t.filter (λ y, y ∈ s.image f) = s.image f := by { ext, rw [mem_filter, mem_image], simp only [and_imp, exists_prop, and_iff_right_iff_imp, exists_imp_distrib], rintros x xel rfl, exact h _ xel } lemma fiber_nonempty_iff_mem_image (f : α → β) (s : finset α) (y : β) : (s.filter (λ x, f x = y)).nonempty ↔ y ∈ s.image f := by simp [finset.nonempty] @[simp, norm_cast] lemma coe_image {f : α → β} : ↑(s.image f) = f '' ↑s := set.ext $ λ _, mem_image.trans set.mem_image_iff_bex.symm protected lemma nonempty.image (h : s.nonempty) (f : α → β) : (s.image f).nonempty := let ⟨a, ha⟩ := h in ⟨f a, mem_image_of_mem f ha⟩ @[simp] lemma nonempty.image_iff (f : α → β) : (s.image f).nonempty ↔ s.nonempty := ⟨λ ⟨y, hy⟩, let ⟨x, hx, _⟩ := mem_image.mp hy in ⟨x, hx⟩, λ h, h.image f⟩ theorem image_to_finset [decidable_eq α] {s : multiset α} : s.to_finset.image f = (s.map f).to_finset := ext $ λ _, by simp only [mem_image, multiset.mem_to_finset, exists_prop, multiset.mem_map] lemma image_val_of_inj_on (H : set.inj_on f s) : (image f s).1 = s.1.map f := (s.2.map_on H).dedup @[simp] lemma image_id [decidable_eq α] : s.image id = s := ext $ λ _, by simp only [mem_image, exists_prop, id, exists_eq_right] @[simp] theorem image_id' [decidable_eq α] : s.image (λ x, x) = s := image_id theorem image_image [decidable_eq γ] {g : β → γ} : (s.image f).image g = s.image (g ∘ f) := eq_of_veq $ by simp only [image_val, dedup_map_dedup_eq, multiset.map_map] lemma image_comm {β'} [decidable_eq β'] [decidable_eq γ] {f : β → γ} {g : α → β} {f' : α → β'} {g' : β' → γ} (h_comm : ∀ a, f (g a) = g' (f' a)) : (s.image g).image f = (s.image f').image g' := by simp_rw [image_image, comp, h_comm] lemma _root_.function.semiconj.finset_image [decidable_eq α] {f : α → β} {ga : α → α} {gb : β → β} (h : function.semiconj f ga gb) : function.semiconj (image f) (image ga) (image gb) := λ s, image_comm h lemma _root_.function.commute.finset_image [decidable_eq α] {f g : α → α} (h : function.commute f g) : function.commute (image f) (image g) := h.finset_image theorem image_subset_image {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.image f ⊆ s₂.image f := by simp only [subset_def, image_val, subset_dedup', dedup_subset', multiset.map_subset_map h] lemma image_subset_iff : s.image f ⊆ t ↔ ∀ x ∈ s, f x ∈ t := calc s.image f ⊆ t ↔ f '' ↑s ⊆ ↑t : by norm_cast ... ↔ _ : set.image_subset_iff theorem image_mono (f : α → β) : monotone (finset.image f) := λ _ _, image_subset_image lemma image_subset_image_iff {t : finset α} (hf : injective f) : s.image f ⊆ t.image f ↔ s ⊆ t := by { simp_rw ←coe_subset, push_cast, exact set.image_subset_image_iff hf } theorem coe_image_subset_range : ↑(s.image f) ⊆ set.range f := calc ↑(s.image f) = f '' ↑s : coe_image ... ⊆ set.range f : set.image_subset_range f ↑s theorem image_filter {p : β → Prop} [decidable_pred p] : (s.image f).filter p = (s.filter (p ∘ f)).image f := ext $ λ b, by simp only [mem_filter, mem_image, exists_prop]; exact ⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, ⟨h1, h2⟩, rfl⟩, by rintro ⟨x, ⟨h1, h2⟩, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩ theorem image_union [decidable_eq α] {f : α → β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).image f = s₁.image f ∪ s₂.image f := ext $ λ _, by simp only [mem_image, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib] lemma image_inter_subset [decidable_eq α] (f : α → β) (s t : finset α) : (s ∩ t).image f ⊆ s.image f ∩ t.image f := subset_inter (image_subset_image $ inter_subset_left _ _) $ image_subset_image $ inter_subset_right _ _ lemma image_inter_of_inj_on [decidable_eq α] {f : α → β} (s t : finset α) (hf : set.inj_on f (s ∪ t)) : (s ∩ t).image f = s.image f ∩ t.image f := coe_injective $ by { push_cast, exact set.image_inter_on (λ a ha b hb, hf (or.inr ha) $ or.inl hb) } lemma image_inter [decidable_eq α] (s₁ s₂ : finset α) (hf : injective f) : (s₁ ∩ s₂).image f = s₁.image f ∩ s₂.image f := image_inter_of_inj_on _ _ $ hf.inj_on _ @[simp] theorem image_singleton (f : α → β) (a : α) : image f {a} = {f a} := ext $ λ x, by simpa only [mem_image, exists_prop, mem_singleton, exists_eq_left] using eq_comm @[simp] theorem image_insert [decidable_eq α] (f : α → β) (a : α) (s : finset α) : (insert a s).image f = insert (f a) (s.image f) := by simp only [insert_eq, image_singleton, image_union] lemma erase_image_subset_image_erase [decidable_eq α] (f : α → β) (s : finset α) (a : α) : (s.image f).erase (f a) ⊆ (s.erase a).image f := begin simp only [subset_iff, and_imp, exists_prop, mem_image, exists_imp_distrib, mem_erase], rintro b hb x hx rfl, exact ⟨_, ⟨ne_of_apply_ne f hb, hx⟩, rfl⟩, end @[simp] lemma image_erase [decidable_eq α] {f : α → β} (hf : injective f) (s : finset α) (a : α) : (s.erase a).image f = (s.image f).erase (f a) := begin refine (erase_image_subset_image_erase _ _ _).antisymm' (λ b, _), simp only [mem_image, exists_prop, mem_erase], rintro ⟨a', ⟨haa', ha'⟩, rfl⟩, exact ⟨hf.ne haa', a', ha', rfl⟩, end @[simp] theorem image_eq_empty : s.image f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_image_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ lemma image_sdiff [decidable_eq α] {f : α → β} (s t : finset α) (hf : injective f) : (s \ t).image f = s.image f \ t.image f := coe_injective $ by { push_cast, exact set.image_diff hf _ _ } lemma image_symm_diff [decidable_eq α] {f : α → β} (s t : finset α) (hf : injective f) : (s ∆ t).image f = s.image f ∆ t.image f := coe_injective $ by { push_cast, exact set.image_symm_diff hf _ _ } @[simp] lemma _root_.disjoint.of_image_finset {s t : finset α} {f : α → β} (h : disjoint (s.image f) (t.image f)) : disjoint s t := disjoint_iff_ne.2 $ λ a ha b hb, ne_of_apply_ne f $ h.forall_ne_finset (mem_image_of_mem _ ha) (mem_image_of_mem _ hb) lemma mem_range_iff_mem_finset_range_of_mod_eq' [decidable_eq α] {f : ℕ → α} {a : α} {n : ℕ} (hn : 0 < n) (h : ∀ i, f (i % n) = f i) : a ∈ set.range f ↔ a ∈ (finset.range n).image (λi, f i) := begin split, { rintros ⟨i, hi⟩, simp only [mem_image, exists_prop, mem_range], exact ⟨i % n, nat.mod_lt i hn, (rfl.congr hi).mp (h i)⟩ }, { rintro h, simp only [mem_image, exists_prop, set.mem_range, mem_range] at *, rcases h with ⟨i, hi, ha⟩, exact ⟨i, ha⟩ } end lemma mem_range_iff_mem_finset_range_of_mod_eq [decidable_eq α] {f : ℤ → α} {a : α} {n : ℕ} (hn : 0 < n) (h : ∀ i, f (i % n) = f i) : a ∈ set.range f ↔ a ∈ (finset.range n).image (λi, f i) := suffices (∃ i, f (i % n) = a) ↔ ∃ i, i < n ∧ f ↑i = a, by simpa [h], have hn' : 0 < (n : ℤ), from int.coe_nat_lt.mpr hn, iff.intro (assume ⟨i, hi⟩, have 0 ≤ i % ↑n, from int.mod_nonneg _ (ne_of_gt hn'), ⟨int.to_nat (i % n), by rw [←int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos i hn', hi⟩⟩) (assume ⟨i, hi, ha⟩, ⟨i, by rw [int.mod_eq_of_lt (int.coe_zero_le _) (int.coe_nat_lt_coe_nat_of_lt hi), ha]⟩) lemma range_add (a b : ℕ) : range (a + b) = range a ∪ (range b).map (add_left_embedding a) := by { rw [←val_inj, union_val], exact multiset.range_add_eq_union a b } @[simp] lemma attach_image_val [decidable_eq α] {s : finset α} : s.attach.image subtype.val = s := eq_of_veq $ by rw [image_val, attach_val, multiset.attach_map_val, dedup_eq_self] @[simp] lemma attach_image_coe [decidable_eq α] {s : finset α} : s.attach.image coe = s := finset.attach_image_val @[simp] lemma attach_insert [decidable_eq α] {a : α} {s : finset α} : attach (insert a s) = insert (⟨a, mem_insert_self a s⟩ : {x // x ∈ insert a s}) ((attach s).image (λx, ⟨x.1, mem_insert_of_mem x.2⟩)) := ext $ λ ⟨x, hx⟩, ⟨or.cases_on (mem_insert.1 hx) (λ h : x = a, λ _, mem_insert.2 $ or.inl $ subtype.eq h) (λ h : x ∈ s, λ _, mem_insert_of_mem $ mem_image.2 $ ⟨⟨x, h⟩, mem_attach _ _, subtype.eq rfl⟩), λ _, finset.mem_attach _ _⟩ theorem map_eq_image (f : α ↪ β) (s : finset α) : s.map f = s.image f := eq_of_veq (s.map f).2.dedup.symm @[simp] lemma disjoint_image {s t : finset α} {f : α → β} (hf : injective f) : disjoint (s.image f) (t.image f) ↔ disjoint s t := by convert disjoint_map ⟨_, hf⟩; simp [map_eq_image] lemma image_const {s : finset α} (h : s.nonempty) (b : β) : s.image (λa, b) = singleton b := ext $ assume b', by simp only [mem_image, exists_prop, exists_and_distrib_right, h.bex, true_and, mem_singleton, eq_comm] @[simp] lemma map_erase [decidable_eq α] (f : α ↪ β) (s : finset α) (a : α) : (s.erase a).map f = (s.map f).erase (f a) := by { simp_rw map_eq_image, exact s.image_erase f.2 a } theorem image_bUnion [decidable_eq γ] {f : α → β} {s : finset α} {t : β → finset γ} : (s.image f).bUnion t = s.bUnion (λa, t (f a)) := by haveI := classical.dec_eq α; exact finset.induction_on s rfl (λ a s has ih, by simp only [image_insert, bUnion_insert, ih]) theorem bUnion_image [decidable_eq γ] {s : finset α} {t : α → finset β} {f : β → γ} : (s.bUnion t).image f = s.bUnion (λa, (t a).image f) := by haveI := classical.dec_eq α; exact finset.induction_on s rfl (λ a s has ih, by simp only [bUnion_insert, image_union, ih]) lemma image_bUnion_filter_eq [decidable_eq α] (s : finset β) (g : β → α) : (s.image g).bUnion (λa, s.filter $ (λc, g c = a)) = s := bUnion_filter_eq_of_maps_to (λ x, mem_image_of_mem g) lemma bUnion_singleton {f : α → β} : s.bUnion (λa, {f a}) = s.image f := ext $ λ x, by simp only [mem_bUnion, mem_image, mem_singleton, eq_comm] end image /-! ### Subtype -/ section subtype /-- Given a finset `s` and a predicate `p`, `s.subtype p` is the finset of `subtype p` whose elements belong to `s`. -/ protected def subtype {α} (p : α → Prop) [decidable_pred p] (s : finset α) : finset (subtype p) := (s.filter p).attach.map ⟨λ x, ⟨x.1, (finset.mem_filter.1 x.2).2⟩, λ x y H, subtype.eq $ subtype.mk.inj H⟩ @[simp] lemma mem_subtype {p : α → Prop} [decidable_pred p] {s : finset α} : ∀ {a : subtype p}, a ∈ s.subtype p ↔ (a : α) ∈ s | ⟨a, ha⟩ := by simp [finset.subtype, ha] lemma subtype_eq_empty {p : α → Prop} [decidable_pred p] {s : finset α} : s.subtype p = ∅ ↔ ∀ x, p x → x ∉ s := by simp [ext_iff, subtype.forall, subtype.coe_mk]; refl @[mono] lemma subtype_mono {p : α → Prop} [decidable_pred p] : monotone (finset.subtype p) := λ s t h x hx, mem_subtype.2 $ h $ mem_subtype.1 hx /-- `s.subtype p` converts back to `s.filter p` with `embedding.subtype`. -/ @[simp] lemma subtype_map (p : α → Prop) [decidable_pred p] {s : finset α} : (s.subtype p).map (embedding.subtype _) = s.filter p := begin ext x, simp [and_comm _ (_ = _), @and.left_comm _ (_ = _), and_comm (p x) (x ∈ s)] end /-- If all elements of a `finset` satisfy the predicate `p`, `s.subtype p` converts back to `s` with `embedding.subtype`. -/ lemma subtype_map_of_mem {p : α → Prop} [decidable_pred p] {s : finset α} (h : ∀ x ∈ s, p x) : (s.subtype p).map (embedding.subtype _) = s := by rw [subtype_map, filter_true_of_mem h] /-- If a `finset` of a subtype is converted to the main type with `embedding.subtype`, all elements of the result have the property of the subtype. -/ lemma property_of_mem_map_subtype {p : α → Prop} (s : finset {x // p x}) {a : α} (h : a ∈ s.map (embedding.subtype _)) : p a := begin rcases mem_map.1 h with ⟨x, hx, rfl⟩, exact x.2 end /-- If a `finset` of a subtype is converted to the main type with `embedding.subtype`, the result does not contain any value that does not satisfy the property of the subtype. -/ lemma not_mem_map_subtype_of_not_property {p : α → Prop} (s : finset {x // p x}) {a : α} (h : ¬ p a) : a ∉ (s.map (embedding.subtype _)) := mt s.property_of_mem_map_subtype h /-- If a `finset` of a subtype is converted to the main type with `embedding.subtype`, the result is a subset of the set giving the subtype. -/ lemma map_subtype_subset {t : set α} (s : finset t) : ↑(s.map (embedding.subtype _)) ⊆ t := begin intros a ha, rw mem_coe at ha, convert property_of_mem_map_subtype s ha end end subtype /-! ### Fin -/ /-- Given a finset `s` of natural numbers and a bound `n`, `s.fin n` is the finset of all elements of `s` less than `n`. -/ protected def fin (n : ℕ) (s : finset ℕ) : finset (fin n) := (s.subtype _).map fin.equiv_subtype.symm.to_embedding @[simp] lemma mem_fin {n} {s : finset ℕ} : ∀ a : fin n, a ∈ s.fin n ↔ (a : ℕ) ∈ s | ⟨a, ha⟩ := by simp [finset.fin] @[mono] lemma fin_mono {n} : monotone (finset.fin n) := λ s t h x, by simpa using @h x @[simp] lemma fin_map {n} {s : finset ℕ} : (s.fin n).map fin.coe_embedding = s.filter (< n) := by simp [finset.fin, finset.map_map] lemma subset_image_iff [decidable_eq β] {s : set α} {t : finset β} {f : α → β}: ↑t ⊆ f '' s ↔ ∃ s' : finset α, ↑s' ⊆ s ∧ s'.image f = t := begin split, swap, { rintro ⟨t, ht, rfl⟩, rw [coe_image], exact set.image_subset f ht }, intro h, letI : can_lift β s (f ∘ coe) (λ y, y ∈ f '' s) := ⟨λ y ⟨x, hxt, hy⟩, ⟨⟨x, hxt⟩, hy⟩⟩, lift t to finset s using h, refine ⟨t.map (embedding.subtype _), map_subtype_subset _, _⟩, ext y, simp end lemma range_sdiff_zero {n : ℕ} : range (n + 1) \ {0} = (range n).image nat.succ := begin induction n with k hk, { simp }, nth_rewrite 1 range_succ, rw [range_succ, image_insert, ←hk, insert_sdiff_of_not_mem], simp end end finset lemma _root_.multiset.to_finset_map [decidable_eq α] [decidable_eq β] (f : α → β) (m : multiset α) : (m.map f).to_finset = m.to_finset.image f := finset.val_inj.1 (multiset.dedup_map_dedup_eq _ _).symm namespace equiv /-- Given an equivalence `α` to `β`, produce an equivalence between `finset α` and `finset β`. -/ protected def finset_congr (e : α ≃ β) : finset α ≃ finset β := { to_fun := λ s, s.map e.to_embedding, inv_fun := λ s, s.map e.symm.to_embedding, left_inv := λ s, by simp [finset.map_map], right_inv := λ s, by simp [finset.map_map] } @[simp] lemma finset_congr_apply (e : α ≃ β) (s : finset α) : e.finset_congr s = s.map e.to_embedding := rfl @[simp] lemma finset_congr_refl : (equiv.refl α).finset_congr = equiv.refl _ := by { ext, simp } @[simp] lemma finset_congr_symm (e : α ≃ β) : e.finset_congr.symm = e.symm.finset_congr := rfl @[simp] lemma finset_congr_trans (e : α ≃ β) (e' : β ≃ γ) : e.finset_congr.trans (e'.finset_congr) = (e.trans e').finset_congr := by { ext, simp [-finset.mem_map, -equiv.trans_to_embedding] } lemma finset_congr_to_embedding (e : α ≃ β) : e.finset_congr.to_embedding = (finset.map_embedding e.to_embedding).to_embedding := rfl end equiv
54c0dad4e6afc80bd7a67be5da1c293a8b9d887b
b70447c014d9e71cf619ebc9f539b262c19c2e0b
/hott/hit/trunc.hlean
992b527d0c2ca33bbc388df4590168d942586341
[ "Apache-2.0" ]
permissive
ia0/lean2
c20d8da69657f94b1d161f9590a4c635f8dc87f3
d86284da630acb78fa5dc3b0b106153c50ffccd0
refs/heads/master
1,611,399,322,751
1,495,751,007,000
1,495,751,007,000
93,104,167
0
0
null
1,496,355,488,000
1,496,355,487,000
null
UTF-8
Lean
false
false
5,689
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 n-truncation of types. Ported from Coq HoTT -/ /- The hit n-truncation is primitive, declared in init.hit. -/ import types.sigma types.pointed open is_trunc eq equiv is_equiv function prod sum sigma namespace trunc protected definition elim {n : trunc_index} {A : Type} {P : Type} [Pt : is_trunc n P] (H : A → P) : trunc n A → P := trunc.rec H protected definition elim_on {n : trunc_index} {A : Type} {P : Type} (aa : trunc n A) [Pt : is_trunc n P] (H : A → P) : P := trunc.elim H aa end trunc attribute trunc.elim_on [unfold 4] attribute trunc.rec [recursor 6] attribute trunc.elim [recursor 6] [unfold 6] namespace trunc variables {X Y Z : Type} {P : X → Type} (n : trunc_index) (A B : Type) local attribute is_trunc_eq [instance] variables {A n} definition untrunc_of_is_trunc [reducible] [unfold 4] [H : is_trunc n A] : trunc n A → A := trunc.rec id variables (A n) definition is_equiv_tr [instance] [constructor] [H : is_trunc n A] : is_equiv (@tr n A) := adjointify _ (untrunc_of_is_trunc) (λaa, trunc.rec_on aa (λa, idp)) (λa, idp) definition trunc_equiv [constructor] [H : is_trunc n A] : trunc n A ≃ A := (equiv.mk tr _)⁻¹ᵉ definition is_trunc_of_is_equiv_tr [H : is_equiv (@tr n A)] : is_trunc n A := is_trunc_is_equiv_closed n (@tr n _)⁻¹ /- Functoriality -/ definition trunc_functor [unfold 5] (f : X → Y) : trunc n X → trunc n Y := λxx, trunc.rec_on xx (λx, tr (f x)) definition trunc_functor_compose [unfold 7] (f : X → Y) (g : Y → Z) : trunc_functor n (g ∘ f) ~ trunc_functor n g ∘ trunc_functor n f := λxx, trunc.rec_on xx (λx, idp) definition trunc_functor_id : trunc_functor n (@id A) ~ id := λxx, trunc.rec_on xx (λx, idp) definition trunc_functor_cast {X Y : Type} (n : ℕ₋₂) (p : X = Y) : trunc_functor n (cast p) ~ cast (ap (trunc n) p) := begin intro x, induction x with x, esimp, exact fn_tr_eq_tr_fn p (λy, tr) x ⬝ !tr_compose end definition is_equiv_trunc_functor [constructor] (f : X → Y) [H : is_equiv f] : is_equiv (trunc_functor n f) := adjointify _ (trunc_functor n f⁻¹) (λyy, trunc.rec_on yy (λy, ap tr !right_inv)) (λxx, trunc.rec_on xx (λx, ap tr !left_inv)) definition trunc_homotopy {f g : X → Y} (p : f ~ g) : trunc_functor n f ~ trunc_functor n g := λxx, trunc.rec_on xx (λx, ap tr (p x)) section definition trunc_equiv_trunc [constructor] (f : X ≃ Y) : trunc n X ≃ trunc n Y := equiv.mk _ (is_equiv_trunc_functor n f) end section open prod.ops definition trunc_prod_equiv [constructor] : trunc n (X × Y) ≃ trunc n X × trunc n Y := begin fapply equiv.MK, {exact (λpp, trunc.rec_on pp (λp, (tr p.1, tr p.2)))}, {intro p, cases p with xx yy, apply (trunc.rec_on xx), intro x, apply (trunc.rec_on yy), intro y, exact (tr (x,y))}, {intro p, cases p with xx yy, apply (trunc.rec_on xx), intro x, apply (trunc.rec_on yy), intro y, apply idp}, {intro pp, apply (trunc.rec_on pp), intro p, cases p, apply idp} end end /- Propositional truncation -/ definition ttrunc [constructor] (n : ℕ₋₂) (X : Type) : n-Type := trunctype.mk (trunc n X) _ -- should this live in Prop? definition merely [reducible] [constructor] (A : Type) : Prop := ttrunc -1 A notation `||`:max A `||`:0 := merely A notation `∥`:max A `∥`:0 := merely A definition Exists [reducible] [constructor] (P : X → Type) : Prop := ∥ sigma P ∥ definition or [reducible] [constructor] (A B : Type) : Prop := ∥ A ⊎ B ∥ notation `exists` binders `,` r:(scoped P, Exists P) := r notation `∃` binders `,` r:(scoped P, Exists P) := r notation A ` \/ ` B := or A B notation A ∨ B := or A B definition merely.intro [reducible] [constructor] (a : A) : ∥ A ∥ := tr a definition exists.intro [reducible] [constructor] (x : X) (p : P x) : ∃x, P x := tr ⟨x, p⟩ definition or.intro_left [reducible] [constructor] (x : X) : X ∨ Y := tr (inl x) definition or.intro_right [reducible] [constructor] (y : Y) : X ∨ Y := tr (inr y) definition is_contr_of_merely_prop [H : is_prop A] (aa : merely A) : is_contr A := is_contr_of_inhabited_prop (trunc.rec_on aa id) section open sigma.ops definition trunc_sigma_equiv [constructor] : trunc n (Σ x, P x) ≃ trunc n (Σ x, trunc n (P x)) := equiv.MK (λpp, trunc.rec_on pp (λp, tr ⟨p.1, tr p.2⟩)) (λpp, trunc.rec_on pp (λp, trunc.rec_on p.2 (λb, tr ⟨p.1, b⟩))) (λpp, trunc.rec_on pp (λp, sigma.rec_on p (λa bb, trunc.rec_on bb (λb, by esimp)))) (λpp, trunc.rec_on pp (λp, sigma.rec_on p (λa b, by esimp))) definition trunc_sigma_equiv_of_is_trunc [H : is_trunc n X] : trunc n (Σ x, P x) ≃ Σ x, trunc n (P x) := calc trunc n (Σ x, P x) ≃ trunc n (Σ x, trunc n (P x)) : trunc_sigma_equiv ... ≃ Σ x, trunc n (P x) : !trunc_equiv end /- the (non-dependent) universal property -/ definition trunc_arrow_equiv [constructor] [H : is_trunc n B] : (trunc n A → B) ≃ (A → B) := begin fapply equiv.MK, { intro g a, exact g (tr a)}, { intro f x, exact trunc.rec_on x f}, { intro f, apply eq_of_homotopy, intro a, reflexivity}, { intro g, apply eq_of_homotopy, intro x, exact trunc.rec_on x (λa, idp)}, end end trunc
b5193a43a056674af92cbf21e8fed645a4c2ecdb
54518a41e0f0c03b53f961e37a3ac2fed5cfeaa9
/src/set_theory.lean
f57436f63a15309cd8bc1c4d9cfce8b3158084af
[ "Apache-2.0" ]
permissive
cipher1024/flypitch
9110cbfc99aa2195fe0a903fffc7034cdb00e87c
357cd9cc344d7b6ea0c1f5d7232956b9ddb39359
refs/heads/master
1,598,733,974,743
1,572,308,681,000
1,572,992,816,000
218,170,521
0
0
Apache-2.0
1,572,308,724,000
1,572,308,723,000
null
UTF-8
Lean
false
false
34,997
lean
/- Copyright (c) 2019 The Flypitch Project. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jesse Han, Floris van Doorn -/ import .to_mathlib /- note: in comment above cofinality, change sentence with + `∀ a, ∃ b ∈ S, ¬(b > a)`. It is defined for all ordinals, but -/ universe variables u v w w' noncomputable theory open ordinal set section delta_system variables {ι : Type w} {ι' : Type w'} {α : Type u} {β : Type v} {A : ι → set α} def is_delta_system (A : ι → set α) := ∃(root : set α), ∀{{x y}}, x ≠ y → A x ∩ A y = root open cardinal def root_subset (hι : 2 ≤ mk ι) {root : set α} (x : ι) (h : ∀{{x y}}, x ≠ y → A x ∩ A y = root) : root ⊆ A x := begin cases (two_le_iff' x).mp hι with y hy, rw [←h hy], apply inter_subset_left end def finite_root (hι : 2 ≤ mk ι) {root : set α} (h2A : ∀(x : ι), finite (A x)) (h : ∀{{x y}}, x ≠ y → A x ∩ A y = root) : finite root := begin rcases two_le_iff.mp hι with ⟨t, u, htu⟩, rw [←h htu], exact finite_subset (h2A t) (inter_subset_left _ _) end open function lemma is_delta_system_preimage (f : β → α) (h : is_delta_system A) : is_delta_system (preimage f ∘ A) := begin cases h with r hr, use f ⁻¹' r, rintro x y hxy, rw [←preimage_inter], apply congr_arg (preimage f), apply hr hxy end lemma is_delta_system_image {f : α → β} (hf : injective f) : is_delta_system (image f ∘ A) ↔ is_delta_system A := begin split, { intro h, have := is_delta_system_preimage f h, convert this, apply funext, intro x, dsimp, rw [preimage_image_eq _ hf] }, rintro ⟨r, hr⟩, use f '' r, rintro x y hxy, rw [image_inter hf], apply congr_arg (image f), apply hr hxy end lemma is_delta_system_preimage_iff {f : β → α} (hf : injective f) (hf₂ : ∀i, A i ⊆ range f) : is_delta_system (preimage f ∘ A) ↔ is_delta_system A := begin split, rintro ⟨r, hr⟩, use f '' r, intros i j hij, rw [←hr hij, ←image_inter hf, image_preimage_eq_of_subset (hf₂ i), image_preimage_eq_of_subset (hf₂ j)], apply is_delta_system_preimage end lemma is_delta_system_precompose (f : ι' → ι) (hf : injective f) (h : is_delta_system A) : is_delta_system (A ∘ f) := by { cases h with r hr, use r, intros x y hxy, exact hr (λ hxy', hxy $ hf hxy') } lemma is_delta_system_precompose_iff (f : ι' ≃ ι) : is_delta_system A ↔ is_delta_system (A ∘ f) := begin use is_delta_system_precompose f f.bijective.1, intro h, convert is_delta_system_precompose f.symm f.symm.bijective.1 h, apply funext, intro i, simp end end delta_system namespace subrel -- instance subrel.is_well_order' {α : Type u} (r : α → α → Prop) [is_well_order α r] -- (p : α → Prop) : is_well_order {x // p x} (subrel r p) := -- subrel.is_well_order r p end subrel namespace delta_system open cardinal ordinal set open function lemma delta_system_lemma_2 {κ : cardinal} (hκ : cardinal.omega ≤ κ) {θ : Type u} (θr : θ → θ → Prop) [θwo : is_well_order θ θr] (hκθ : κ < mk θ) (hθ : is_regular $ mk θ) (θtype_eq : ord (mk θ) = type θr) (hθ_le : ∀(β < mk θ), β ^< κ < mk θ) {ρ : Type u} (ρr : ρ → ρ → Prop) [ρwo : is_well_order ρ ρr] (hρ : mk ρ < κ) {ι : Type u} {A : ι → set θ} (h2A : ∀i, ρr ≃o subrel θr (A i)) (h3A : unbounded θr (⋃i, A i)) : ∃(t : set ι), mk t = mk θ ∧ is_delta_system (restrict A t) := begin let nr : ι → ρ → θ := λ i ξ, (h2A i ξ).val, let good : ρ → Prop := λ ξ, unbounded θr (range $ λ i, nr i ξ), have : ∃ξ : ρ, good ξ, { apply unbounded_of_unbounded_Union θr (λ ξ, range $ λ i, nr i ξ), { rw [Union_range_eq_Union], exact h3A, intro i, exact (h2A i).to_equiv.bijective.2 }, { rw [←cof_type, ←θtype_eq, hθ.2], refine lt_trans hρ hκθ }}, let ξ₀ : ρ := ρwo.wf.min good (ne_empty_iff_exists_mem.mpr this), let α₀ : ordinal := sup.{u u} (λ o : {x // ρr x ξ₀}, sup.{u u} $ λ x : ι, ordinal.succ $ typein θr $ nr x o.1), have hα₀ : α₀ < type θr, { rw [←θtype_eq], apply sup_lt_ord_of_is_regular _ hθ, { refine lt_of_le_of_lt _ (lt_trans hρ hκθ), rw [card_typein, ←card_type ρr], apply card_le_card, apply le_of_lt, apply typein_lt_type }, rintro ⟨ξ, hξ⟩, refine lt_of_le_of_lt (sup_succ _) _, apply (ord_is_limit hθ.1).2, apply lt_of_not_ge, intro h, apply ρwo.wf.not_lt_min _ _ _ hξ, apply unbounded_range_of_sup_ge, dsimp, rw [←θtype_eq], exact h }, let pick' : ∀(μ : θ), ∀(pick : ∀y, θr y μ → ι), ordinal := λ μ pick, max α₀ $ sup.{u u} (λ x : ρ × {x // θr x μ}, typein θr $ nr (pick x.2.val x.2.2) x.1), have pick'_lt : ∀(μ : θ) (pick : ∀y, θr y μ → ι), pick' μ pick < type θr, { intros μ pick, apply max_lt hα₀, rw [←θtype_eq], apply sup_lt_ord_of_is_regular _ hθ, { apply @mul_lt_of_lt (mk ρ) (mk {x // θr x μ}) _ hθ.1 (lt_trans hρ hκθ), rw [←ord_lt_ord, θtype_eq], apply lt_of_le_of_lt (ord_le_type (subrel θr {x | θr x μ})), apply typein_lt_type }, rintro ⟨x, y, hy⟩, rw [θtype_eq], apply typein_lt_type }, have : ∀(x : θ), ∃i : ι, θr x (nr i ξ₀), { intro x, have : good ξ₀ := ρwo.wf.min_mem good _, have θr_unbounded : ∀(x : θ), ∃y, θr x y, { intro y, apply has_succ_of_is_limit, rw [←θtype_eq], exact ord_is_limit hθ.1 }, cases θr_unbounded x with y hy, rcases this y with ⟨z, ⟨i, rfl⟩, hz⟩, use i, rcases trichotomous_of θr (nr i ξ₀) y with hw | rfl | hw, exfalso, exact hz hw, exact hy, exact trans hy hw }, let pick : θ → ι := θwo.wf.fix (λ μ pick, classical.some $ this $ enum θr (pick' μ pick) (pick'_lt μ pick)), have lt_pick : ∀(μ : θ), θr (enum θr (pick' μ (λ y _, pick y)) (pick'_lt μ (λ y _, pick y))) (nr (pick μ) ξ₀), { intro μ, dsimp [pick], rw [θwo.wf.fix_eq _ μ], apply classical.some_spec (this _) }, have pick_lt_pick : ∀{μ ν : θ} (h : θr ν μ) (η : ρ), θr (nr (pick ν) η) (nr (pick μ) ξ₀), { intros, apply trans_trichotomous_left _ (lt_pick μ), rw [←typein_le_typein, typein_enum], refine le_trans _ (le_max_right _ _), refine le_trans _ (le_sup _ ⟨η, ν, h⟩), refl }, let sub_α₀ : set θ := typein θr ⁻¹' {c | c < α₀}, have h1sub_α₀ : mk ↥sub_α₀ = α₀.card, { rw [←cardinal.lift_inj.{_ u+1}, mk_preimage_of_injective_of_subset_range_lift], rw [mk_initial_seg, cardinal.lift_lift], exact injective_typein θr, intros o ho, rcases typein_surj θr (lt_trans ho hα₀) with ⟨_, rfl⟩, apply mem_range_self }, have h2A2' : ∀{x y : θ}, θr x y → A (pick x) ∩ A (pick y) ⊆ sub_α₀, { rintros x y hxy z ⟨hzx, hzy⟩, let η := typein (subrel θr $ A $ pick y) ⟨z, hzy⟩, have η_def : z = (enum (subrel θr $ A $ pick y) η (typein_lt_type _ _)).val, {rw [enum_typein]}, cases lt_or_ge η (typein ρr ξ₀) with h h, { rw [mem_preimage, mem_set_of_eq], refine lt_of_lt_of_le _ (ordinal.le_sup _ _), { refine ⟨enum ρr η (lt_trans h (typein_lt_type _ _)), _⟩, rw [←typein_lt_typein ρr, typein_enum], exact h }, refine lt_of_lt_of_le _ (ordinal.le_sup _ (pick y)), convert lt_succ_self (typein θr z), rw [η_def], dsimp [nr], congr' 1, apply order_iso_enum' (h2A _) }, exfalso, have η_lt : η < type ρr, { convert typein_lt_type (subrel θr $ A $ pick y) ⟨z, hzy⟩ using 1, apply quotient.sound, exact ⟨h2A (pick y)⟩ }, have : ¬ρr (enum ρr η η_lt) ξ₀, { rw [←typein_le_typein, typein_enum], exact h }, apply this, rw [(h2A (pick y)).ord], dsimp only [subrel, order.preimage], convert pick_lt_pick hxy ((h2A (pick x)).symm ⟨z, hzx⟩) using 1, transitivity z, { rw [η_def], congr' 1, apply order_iso_enum (h2A _) }, transitivity subtype.val ⟨z, hzx⟩, refl, congr' 1, symmetry, apply equiv.right_inv }, have h1A2 : mk (range pick) = mk θ, { have increasing_pick : ∀{{x y : θ}}, θr x y → θr (nr (pick x) ξ₀) (nr (pick y) ξ₀), { intros x y hxy, apply pick_lt_pick hxy ξ₀ }, have injective_pick : injective pick, { intros x y hx, apply injective_of_increasing _ _ _ increasing_pick, dsimp only, congr' 1, exact hx }, rw [mk_range_eq], apply injective_pick }, have h2A2 : ∀(x y : range pick), x ≠ y → A x ∩ A y ⊆ sub_α₀, { rintro ⟨_, ⟨x, rfl⟩⟩ ⟨_, ⟨y, rfl⟩⟩ hxy, rcases trichotomous_of θr x y with h | h | h, apply h2A2' h, exfalso, apply hxy, rw h, rw [inter_comm], apply h2A2' h }, have hsub_α₀ : mk ↥sub_α₀ < mk θ, { rw [h1sub_α₀, ←ord_lt_ord, θtype_eq], refine lt_of_le_of_lt (ord_card_le _) hα₀ }, let codomain := {s : set θ // s ⊆ sub_α₀ ∧ mk s ≤ mk ρ}, have hcodomain : ∀(x : range pick), A x ∩ sub_α₀ ⊆ sub_α₀ ∧ mk (↥(A x ∩ sub_α₀)) ≤ mk ρ, { have α₀_lt_pick : ∀(μ : θ), θr (enum θr α₀ hα₀) (nr (pick μ) ξ₀), { intro μ, apply trans_trichotomous_left _ (lt_pick μ), rw enum_le_enum, apply le_max_left }, rintro ⟨s, hs⟩, refine ⟨inter_subset_right _ _, _⟩, rcases hs with ⟨μ, rfl⟩, dsimp, transitivity mk {x : A (pick μ) // typein θr x.1 < α₀}, { apply le_of_eq, apply mk_sep }, let f := (h2A (pick μ)).to_equiv, rw [mk_subtype_of_equiv _ f.symm], transitivity mk { x : ρ // ρr x ξ₀}, { apply mk_subtype_mono, intros x hx, rw [(h2A (pick μ)).ord], dsimp only [subrel, order.preimage], refine trans _ (α₀_lt_pick μ), rw [←enum_typein' θr (f x).val, @enum_lt _ θr], exact hx }, transitivity (typein ρr ξ₀).card, { rw [typein, card_type], refl }, rw [←card_type ρr], apply card_le_card, apply le_of_lt, apply typein_lt_type }, let f : range pick → codomain := λx, ⟨A x.1 ∩ sub_α₀, hcodomain x⟩, have : ∃r (t : set ι), r ⊆ sub_α₀ ∧ t ⊆ range pick ∧ mk t = mk θ ∧ ∀{{x}}, x ∈ t → A x ∩ sub_α₀ = r, { have h1 : cardinal.omega ≤ mk (range pick), { rw [h1A2], exact hθ.1 }, have h2 : mk codomain < cof (ord $ mk $ range pick), { rw [h1A2, hθ.2], apply lt_of_le_of_lt (mk_bounded_subset_le _ _), refine lt_of_le_of_lt (le_powerlt hρ) (hθ_le _ _), apply max_lt hsub_α₀ (lt_of_le_of_lt hκ hκθ) }, cases infinite_pigeonhole f h1 h2 with r' hr', refine ⟨r'.val, subtype.val '' (f ⁻¹' {r'}), _, _, _, _⟩, { rintro x hx, exact r'.2.1 hx }, { rintro _ ⟨⟨x, hx⟩, _, rfl⟩, exact hx }, { rw [mk_image_eq subtype.val_injective, ←h1A2, ←hr'] }, { rintro _ ⟨⟨x, hx⟩, hx', rfl⟩, simpa only [set.mem_singleton_iff, set.mem_preimage, f, subtype.ext] using hx' }}, rcases this with ⟨r, t, hr, h1t, h2t, h3t⟩, refine ⟨t, h2t, r, _⟩, intros x y hxy, rw [←h3t x.2], apply set.ext, intro z, split, { intro hz, refine ⟨hz.1, h2A2 ⟨x, h1t x.2⟩ ⟨y, h1t y.2⟩ _ hz⟩, intro h, apply hxy, apply subtype.eq, apply congr_arg subtype.val h }, intro hz, refine ⟨hz.1, _⟩, rw [h3t x.2, ←h3t y.2] at hz, exact hz.1 end open_locale classical lemma delta_system_lemma_1 {κ : cardinal} (hκ : cardinal.omega ≤ κ) {θ : Type u} (θr : θ → θ → Prop) [θwo : is_well_order θ θr] (hκθ : κ < mk θ) (hθ : is_regular $ mk θ) (θtype_eq : ord (mk θ) = type θr) (hθ_le : ∀(β < mk θ), β ^< κ < mk θ) {ρ : Type u} (ρr : ρ → ρ → Prop) [ρwo : is_well_order ρ ρr] (hρ : mk ρ < κ) {ι : Type u} {A : ι → set θ} (h2A : ∀i, ρr ≃o subrel θr (A i)) (hι : mk θ = mk ι) : ∃(t : set ι), mk t = mk θ ∧ is_delta_system (restrict A t) := begin by_cases h3A : unbounded θr (⋃i, A i), { exact delta_system_lemma_2 hκ θr hκθ hθ θtype_eq hθ_le ρr hρ h2A h3A }, rw [not_unbounded_iff] at h3A, cases h3A with μ hμ, let γ := ⋃i, A i, let A' : @set.univ ι → {s : set θ // s ⊆ γ ∧ mk s ≤ mk ρ} := λx, ⟨restrict A set.univ x, subset_Union A x.1, _⟩, swap, { apply le_of_eq, apply quotient.sound, exact ⟨(h2A x.1).to_equiv.symm⟩ }, rcases infinite_pigeonhole_set A' (mk θ) _ hθ.1 _ with ⟨R, t, h₁t, h₂t, h₃t⟩, have : ∀(x : t), restrict A t x = R.1 := λ x, congr_arg subtype.val (h₃t x.2), refine ⟨t, _, R.1, _⟩, { apply le_antisymm _ h₂t, rw [hι], apply mk_set_le }, { intros i j hij, rw [this i, this j, inter_self] }, { rw [mk_univ, hι] }, refine lt_of_le_of_lt (mk_bounded_subset_le _ _) _, refine lt_of_le_of_lt (le_powerlt hρ) _, rw [hθ.2], apply hθ_le, apply max_lt _ (lt_of_le_of_lt hκ hκθ), rw [←ord_lt_ord, θtype_eq], apply lt_of_le_of_lt _ (typein_lt_type θr μ), rw [ord_le, typein, card_type], apply mk_le_mk_of_subset hμ end /-- The delta-system lemma. [Kunen 1980, Theorem 1.6, p49] -/ theorem delta_system_lemma {α ι : Type u} {κ θ : cardinal} (hκ : cardinal.omega ≤ κ) (hκθ : κ < θ) (hθ : is_regular θ) (hθ_le : ∀(c < θ), c ^< κ < θ) (A : ι → set α) (hA : θ ≤ mk ι) (h2A : ∀i, mk (A i) < κ) : ∃(t : set ι), mk t = θ ∧ is_delta_system (restrict A t) := begin revert hθ hθ_le hκ hκθ hA h2A, refine quotient.induction_on θ _, clear θ, intro θ, intros, rcases ord_eq θ with ⟨θr, θwo, θtype_eq⟩, rcases le_mk_iff_exists_set.mp hA with ⟨t₁, ht₁⟩, resetI, let β := ⋃(i ∈ t₁), A i, have hβ : mk β ≤ mk θ, { refine le_trans (mk_bUnion_le _ _) _, rw [ht₁], refine le_trans (mul_le_max_of_omega_le_left _) _, exact hθ.1, apply max_le, refl, rw [cardinal.sup_le], intro i, apply le_of_lt, apply lt_trans (h2A i.1) hκθ }, have h2β : A '' t₁ ⊆ powerset (range (subtype.val : β → α)), { rintro _ ⟨i, hi, rfl⟩ x hx, refine ⟨⟨x, mem_bUnion hi hx⟩, rfl⟩ }, have f : β ↪ θ, { exact (classical.choice hβ) }, let A₀ : ι → set θ := image f ∘ preimage subtype.val ∘ A, let κα := κ.ord.out.α, let κr := κ.ord.out.r, have h3A₀ : ∀(i : t₁), type (subrel θr (A₀ i)) < type κr, { rintro ⟨i, hi⟩, rw [type_out, lt_ord, card_type], rw [mk_image_eq], rw [mk_preimage_of_injective_of_subset_range], apply h2A i, apply subtype.val_injective, apply h2β (mem_image_of_mem _ hi), exact f.2 }, have hκθ' : mk κα < cof (ord (mk θ)), { rw [←card_type κr, type_out, card_ord], convert hκθ, exact hθ.2 }, let g : t₁ → κα := λ i : t₁, enum κr (type (subrel θr (A₀ i))) (h3A₀ i), rcases infinite_pigeonhole_set g (mk θ) (ge_of_eq ht₁) hθ.1 hκθ' with ⟨ρ', t₂, ht₂, h2t₂, h3t₂⟩, let ρ := { x : κα | κr x ρ' }, let ρr := subrel κr ρ, have hρ : mk ρ < κ, { exact card_typein_out_lt κ ρ', }, have h4A₁ : Π(i : t₂), ρr ≃o subrel θr (A₀ i), { rintro ⟨i, hi⟩, symmetry, have : type (subrel θr (A₀ i)) = typein κr ρ', { rw [← h3t₂ hi, typein_enum], refl }, exact classical.choice (quotient.exact this) }, have h4t₂ : mk θ = mk ↥t₂, { apply le_antisymm h2t₂, convert mk_le_mk_of_subset ht₂ using 1, rw ht₁, refl }, rcases delta_system_lemma_1 hκ θr hκθ hθ θtype_eq hθ_le ρr hρ h4A₁ h4t₂ with ⟨t, h1t, h2t⟩, refine ⟨subtype.val '' t, _, _⟩, { rw [mk_image_eq], exact h1t, exact subtype.val_injective }, have : is_delta_system (λ(i : t), A₀ i.1) := h2t, rw [is_delta_system_image, is_delta_system_preimage_iff] at this, rw [is_delta_system_precompose_iff (equiv.set.image subtype.val t subtype.val_injective)], convert this using 1, apply funext, rintro ⟨⟨i, hi⟩, h2i⟩, simp only [function.comp_app, subtype.coe_mk, equiv.set.image_apply, restrict], apply subtype.val_injective, intro i, refine h2β (mem_image_of_mem _ $ ht₂ i.1.2), exact f.2 end theorem delta_system_lemma_uncountable {α : Type u} {ι : Type v} (A : ι → set α) (h : cardinal.omega < mk ι) (h2A : ∀i, finite (A i)) : ∃(t : set ι), cardinal.omega < mk t ∧ is_delta_system (restrict A t) := begin have : ∀ (c : cardinal), c < succ omega → c ^< omega < succ omega, { intros c hc, refine lt_of_le_of_lt (powerlt_omega_le _) _, apply max_lt hc (lt_succ_self _) }, rcases delta_system_lemma (le_refl _) (lt_succ_self _) (succ_is_regular (le_refl _)) this (image ulift.up.{v u} ∘ A ∘ ulift.down.{u v}) _ _ with ⟨t, h1t, h2t⟩, refine ⟨ulift.down '' t, _, _⟩, { rw [←cardinal.lift_lt.{_ max u v}, mk_image_eq_lift, h1t, cardinal.lift_succ, cardinal.lift_omega, cardinal.lift_omega], apply cardinal.lt_succ_self, apply equiv.ulift.bijective.1 }, { rw [restrict, is_delta_system_image] at h2t, swap, exact equiv.ulift.symm.bijective.1, rw [is_delta_system_precompose_iff (equiv.set.image _ _ _)], swap, exact equiv.ulift.bijective.1, convert h2t, apply funext, rintro ⟨x, hx⟩, refl }, { rw [←cardinal.lift_lt, cardinal.lift_omega, ←cardinal.succ_le] at h, exact h }, rintro ⟨i⟩, rw [lt_omega_iff_finite], apply finite_image, exact h2A i end end delta_system namespace set variables {α β : Type u} open cardinal subtype /- currently the proof depends on cardinal numbers -/ lemma finite_of_finite_image_of_inj_on (f : α → β) (s : set α) (hf : inj_on f s) (h : finite (f '' s)) : finite s := by { rw [←lt_omega_iff_finite] at h ⊢, rwa [mk_image_eq_of_inj_on f s hf] at h } /- currently the proof depends on cardinal numbers -/ lemma countable_of_embedding {s : set α} {t : set β} (f : s ↪ t) (h : countable t) : countable s := begin rw [countable_iff], rw [countable_iff] at h, refine le_trans _ h, refine ⟨f⟩ end /- maybe generalize eq_on -/ def eq_on' {α} {β : α → Type*} (f g : ∀x, β x) (s : set α) : Prop := ∀{{x}}, x ∈ s → f x = g x lemma eq_on'_iff {α} {β : α → Type*} (f g : ∀x, β x) (s : set α) : eq_on' f g s ↔ restrict f s = restrict g s := begin split, intros h, apply funext, rintro ⟨x, hx⟩, exact h hx, intros h x hx, apply congr_fun h ⟨x, hx⟩ end end set -- namespace topological_space open set topological_space cardinal subtype (restrict) section CCC variables (α : Type u) [topological_space α] def countable_chain_condition : Prop := ∀(s : set (set α)), (∀{{o}}, o ∈ s → is_open o) → pairwise_disjoint s → s.countable variable {α} lemma countable_chain_condition_of_nonempty (h : ∀(s : set (set α)), (∀{{o}}, o ∈ s → o ≠ ∅) → (∀{{o}}, o ∈ s → is_open o) → pairwise_disjoint s → s.countable) : countable_chain_condition α := begin intros s open_s hs, let s' : set (set α) := s \ {∅}, have hs' : ∀{{o : set α}}, o ∈ s' → o ≠ ∅, { intros o ho h2o, apply ho.2, rw [mem_singleton_iff, h2o] }, have open_s' : ∀{{o : set α}}, o ∈ s' → is_open o, { intros o ho, exact open_s (diff_subset _ _ ho), }, have h2s' : pairwise_disjoint s', { refine pairwise_disjoint_subset _ hs, apply diff_subset }, have : countable (insert ∅ s'), { apply countable_insert (h s' hs' open_s' h2s') }, apply countable_subset _ this, rw [insert_diff_singleton], apply subset_insert end lemma countable_chain_condition_of_topological_basis (B : set (set α)) (hB : is_topological_basis B) (h : ∀(s : set (set α)), s ⊆ B → pairwise_disjoint s → s.countable) : countable_chain_condition α := begin apply countable_chain_condition_of_nonempty, intros s s_nonempty s_open hs, have f : ∀(x : s), { y : set α // y ∈ B ∧ y ≠ ∅ ∧ y ⊆ x.1 }, { rintro ⟨x, hx⟩, apply classical.subtype_of_exists, have : ∃y, y ∈ x, { exact ne_empty_iff_exists_mem.mp (s_nonempty hx) }, cases this with y hy, rcases mem_basis_subset_of_mem_open hB hy (s_open hx) with ⟨h₁, h₂, h₃, h₄⟩, refine ⟨h₁, h₂, _, h₄⟩, rw [ne_empty_iff_exists_mem], exact ⟨_, h₃⟩ }, let s' : set (set α) := range (λ(x : s), (f x).1), have hs' : ∀{{o : set α}}, o ∈ s' → o ∈ B, { rintro _ ⟨o, rfl⟩, exact (f o).2.1 }, have h2s' : pairwise_disjoint s', { apply pairwise_disjoint_range _ _ hs, intro x, exact (f x).2.2.2 }, have := h s' hs' h2s', rw [cardinal.countable_iff] at this ⊢, convert this using 1, rw [mk_range_eq], rintro x x' hxx', have : ∃y, y ∈ (f x).1, { exact ne_empty_iff_exists_mem.mp (f x).2.2.1 }, cases this with y hy, apply subtype.eq, apply pairwise_disjoint_elim hs x.2 x'.2 y ((f x).2.2.2 hy), dsimp only at hxx', rw hxx' at hy, exact (f x').2.2.2 hy end lemma countable_chain_condition_of_separable_space [h : separable_space α] : countable_chain_condition α := begin have := h, rcases this with ⟨⟨D, h1D, h2D⟩⟩, rw [dense_iff_inter_open] at h2D, apply countable_chain_condition_of_nonempty, intros C h1C h2C h3C, refine countable_of_embedding _ h1D, have f : ∀(c : C), Σ'(d : D), d.1 ∈ c.1, { rintro ⟨s, hs⟩, have := h2D s (h2C hs) (h1C hs), rw [ne_empty_iff_exists_mem] at this, rcases classical.subtype_of_exists this with ⟨x, ⟨hx, h2x⟩⟩, exact ⟨⟨x, h2x⟩, hx⟩ }, refine ⟨λ c, (f c).1, _⟩, rintros ⟨s, hs⟩ ⟨s', hs'⟩ hss', rw [subtype.ext], dsimp, apply pairwise_disjoint_elim h3C hs hs' (f ⟨s, hs⟩).1 (f ⟨s, hs⟩).2, dsimp only at hss', rw [hss'], exact (f ⟨s', hs'⟩).2 end lemma countable_chain_condition_of_countable (h : mk α ≤ omega) : countable_chain_condition α := begin haveI : separable_space α := ⟨⟨set.univ, by rwa [countable_iff, mk_univ], closure_univ⟩⟩, apply countable_chain_condition_of_separable_space, end end CCC section pi variables {α : Type u} {β : α → Type v} [∀x, topological_space (β x)] {γ : Type w} [topological_space γ] def standard_open {i : α} (o : opens (β i)) : set (Πx, β x) := {f : Πx, β x | f i ∈ o } variable (β) def pi_subbasis : set (set (Πx, β x)) := range (λ(x : Σ(i : α), opens (β i)), standard_open x.2) variable {β} variable (β) lemma is_subbasis_pi : Pi.topological_space = generate_from (pi_subbasis β) := begin symmetry, apply le_antisymm, { refine lattice.le_infi _, intro i, rintro _ ⟨s, hs, rfl⟩, apply generate_open.basic, exact ⟨⟨i, s, hs⟩, rfl⟩ }, { rw [le_generate_from_iff_subset_is_open], rintro _ ⟨⟨i, o, ho⟩, rfl⟩, apply generate_open.basic, apply mem_bUnion (mem_range_self i), exact ⟨o, ho, rfl⟩ } end def pi_basis : set (set (Πx, β x)) := (λf, ⋂₀ f) '' {f : set (set (Πx, β x)) | finite f ∧ f ⊆ pi_subbasis β ∧ ⋂₀ f ≠ ∅ } lemma pi_basis_eq : pi_basis β = {g | ∃(s:Πx, set (β x)) (i : finset α), (∀x∈i, is_open (s x)) ∧ g = pi ↑i s} \ {∅} := begin apply subset.antisymm, swap, { rintro _ ⟨⟨s, i, hs, rfl⟩, hs'⟩, let O : (↑i : set α) → set (Πx, β x) := (λ x, standard_open ⟨s x.1, hs x.1 x.2⟩), have : ⋂₀ range O = pi ↑i s, { apply subset.antisymm, { rintro f hf x hx, exact hf (O ⟨x, hx⟩) (mem_range_self _) }, { rintro f hf _ ⟨x, rfl⟩, exact hf x.1 x.2 } }, refine ⟨range O, ⟨finite_range _, _, _⟩, this⟩, { rintro _ ⟨x, rfl⟩, dsimp only, refine ⟨⟨x.1, ⟨s x.1, hs x.1 x.2⟩⟩, rfl⟩ }, { rw [this], exact mt mem_singleton_iff.mpr hs' } }, rintro _ ⟨o, ⟨h1o, h2o, h3o⟩, rfl⟩, dsimp only, refine ⟨_, mt mem_singleton_iff.mp h3o⟩, rw [pi_subbasis, subset_range_iff] at h2o, rcases h2o with ⟨o', rfl, _⟩, let γ := Σi, opens (β i), let O := (λ (x : γ), standard_open (x.snd)), have hβ : nonempty (Πx, β x), { rw [←coe_nonempty_iff_ne_empty] at h3o, rcases h3o with ⟨f, hf⟩, exact ⟨f⟩ }, have ho' : ∀(x ∈ o'), (x : γ).2.1 ≠ ∅, { rintros ⟨x, o⟩ ho, refine mt _ h3o, intro h, rw [eq_empty_iff_forall_not_mem], intros f hf, rw [eq_empty_iff_forall_not_mem] at h, apply h (f _), exact hf _ (mem_image_of_mem _ ho) }, let o'' := {x ∈ o' | (x : γ).2.1 ≠ univ }, let i := sigma.fst '' o'', have h2o' : finite o'', { refine finite_of_finite_image_of_inj_on O _ _ _, { haveI : decidable_eq α := λ _ _, classical.prop_decidable _, rintro ⟨x, o⟩ ⟨x', o'⟩ hx hx' hoo', cases hβ with f, have h₁ : nonempty o.1, { rw [coe_nonempty_iff_ne_empty], exact ho' _ hx.1 }, have h₂ : nonempty (-o.1 : set (β x)), { rw [nonempty_compl], exact hx.2 }, rcases h₁ with ⟨z₁, hz₁⟩, rcases h₂ with ⟨z₂, hz₂⟩, let f₁ : Πx', β x' := change f z₁, let f₂ : Πx', β x' := change f z₂, have hf₁ : f₁ ∈ O ⟨x, o⟩, { simp [O, standard_open, f₁, change], exact hz₁ }, have hf₂ : f₂ ∉ O ⟨x, o⟩, { simp [O, standard_open, f₂, change], exact hz₂ }, rw [hoo'] at hf₁ hf₂, have : x = x', { by_contradiction h', have : f₁ x' ≠ f₂ x', { refine mt _ hf₂, dsimp only [O, standard_open, mem_sep, mem_set_of_eq], intro h, rw ←h, exact hf₁ }, apply this, simp [f₁, f₂, change, h'] }, subst this, apply congr_arg (sigma.mk x), apply subtype.eq, apply set.ext, intro z, rw [set.ext_iff] at hoo', simpa [O, standard_open, change] using hoo' (change f z) }, apply finite_subset h1o, apply image_subset, apply sep_subset } , have hi : finite i, { refine finite_image _ h2o' }, haveI : ∀x, decidable (x ∈ i) := λ x, classical.prop_decidable _, let C : Πx, set (β x) := λ x, if h : x ∈ i then ⋂₀ { O | ∃h : _root_.is_open O, (⟨x, ⟨O, h⟩⟩ : γ) ∈ o'' } else univ, refine ⟨C, finite.to_finset hi, _, _⟩, { intros x hx, rw [finite.mem_to_finset] at hx, have := hx, rcases this with ⟨⟨x, u⟩, ⟨hu, hu'⟩, rfl⟩, simp only [C, dif_pos hx], apply is_open_sInter, { have : finite (subtype.val '' ((λ s : opens (β x), sigma.mk x s) ⁻¹' o'')), { apply finite_image, apply finite_preimage _ h2o', finish }, convert this, apply subset.antisymm, { rintro o ⟨h1o, h2o⟩, exact ⟨⟨o, h1o⟩, h2o, rfl⟩ }, rintro _ ⟨⟨o, ho⟩, h2o, rfl⟩, refine ⟨ho, h2o⟩ }, rintro s ⟨hs, hs'⟩, exact hs }, apply subset.antisymm, { rintro f hf x hx, rw [finite.coe_to_finset] at hx, have := hx, rcases this with ⟨⟨x, o⟩, ⟨ho, h2o⟩, rfl⟩, simp only [C, dif_pos hx], rintro s ⟨hs, h2s⟩, exact hf _ (mem_image_of_mem _ h2s.1) }, rintro f hf _ ⟨⟨x, o⟩, ho, rfl⟩, dsimp [standard_open], rw [finite.coe_to_finset] at hf, haveI : decidable (o.1 = univ) := classical.prop_decidable _, by_cases ho' : o.1 = univ, { rw [mem_opens, ho'], apply mem_univ }, have hx : x ∈ i, { refine ⟨⟨x, o⟩, ⟨ho, ho'⟩, rfl⟩ }, have : C x ⊆ o.1, { cases o with o h2o, simp only [C, dif_pos hx], rintro z hz, exact hz o ⟨h2o, ⟨ho, ho'⟩⟩ }, exact this (hf x hx), end variable {β} lemma nonempty_of_mem_pi_basis {o : set (Πx, β x)} (h : o ∈ pi_basis β) : nonempty o := by { rcases h with ⟨o, ho, rfl⟩, rw [coe_nonempty_iff_ne_empty], exact ho.2.2 } variable (β) lemma is_topological_basis_pi : is_topological_basis (pi_basis β) := is_topological_basis_of_subbasis (is_subbasis_pi β) variable {β} open_locale classical lemma is_open_map_apply (i : α) : is_open_map (λf : Πi, β i, f i) := begin apply is_open_map_of_is_topological_basis (is_topological_basis_pi β), intros o ho, rw [pi_basis_eq] at ho, rcases ho with ⟨⟨s, t, hs, rfl⟩, hs'⟩, have : nonempty (pi ↑t s), { rw [←nmem_singleton_empty], exact hs' }, by_cases i ∈ t, { rw [image_pi_pos ↑t s this i h], exact hs i h }, rw [image_pi_neg ↑t s this i h], exact is_open_univ end lemma restrict_image_pi (t : set α) (s : set α) (s' : Πi, set (β i)) (h : nonempty (pi t s')) : (λ(f : Πi, β i), restrict f s) '' pi t s' = pi (subtype.val ⁻¹' t) (λi, s' i.1) := begin apply subset.antisymm, { rintro _ ⟨f, hf, rfl⟩ i hi, exact hf _ hi }, rintro f hf, have := h, rcases this with ⟨f', hf'⟩, refine ⟨λ i, if h : i ∈ s then f ⟨i, h⟩ else f' i, _, _⟩, { intros i hi, dsimp only, by_cases h2i : i ∈ s, { rw [dif_pos h2i], exact hf ⟨i, h2i⟩ hi, }, { rw [dif_neg h2i], exact hf' i hi } }, apply funext, rintro ⟨i, hi⟩, dsimp only, rw [restrict], apply dif_pos hi end lemma is_open_map_restrict (s : set α) : is_open_map (λ(f : Πi, β i), restrict f s) := begin apply is_open_map_of_is_topological_basis (is_topological_basis_pi β), intros o ho, rw [pi_basis_eq] at ho, rcases ho with ⟨⟨s', t, hs, rfl⟩, hs'⟩, have : nonempty (pi ↑t s'), { rw [←nmem_singleton_empty], exact hs' }, rw [restrict_image_pi _ _ _ this], apply is_open_of_is_topological_basis (is_topological_basis_pi _), rw [pi_basis_eq], refine ⟨⟨λ x, s' x.1, _, _, _⟩, _⟩, { apply finite.to_finset, apply finite_preimage (inj_on_of_injective _ subtype.val_injective), exact t.finite_to_set }, { intros x hx, rw [finite.mem_to_finset] at hx, exact hs _ hx }, { congr' 1, ext, rw [finite.coe_to_finset] }, dsimp only, rw [nmem_singleton_empty], rcases this with ⟨f, hf⟩, exact ⟨⟨λ i, f i.1, λ i hi, hf i.1 hi⟩⟩ end /- The set of indices where a set o is constant, i.e. that coordinate doesn't matter for deciding whether a point is in o -/ def support (o : set (Πx, β x)) : set α := ⋂₀ { s : set α | ∀{{f g : Πi, β i}}, eq_on' f g s → f ∈ o → g ∈ o } lemma support_pi (i : set α) (s : ∀x, set (β x)) (h : nonempty $ pi i s) : support (pi i s) = {x ∈ i | s x ≠ set.univ } := begin apply subset.antisymm, { apply sInter_subset_of_mem, intros f g hfg hf x hx, by_cases h : s x = univ, rw [h], trivial, rw [←hfg ⟨hx, h⟩], exact hf x hx }, { apply subset_sInter, rintro j hj x ⟨hx, hs⟩, have := h, rcases this with ⟨f, hf⟩, have h₂ : nonempty (-(s x) : set _), { rw [nonempty_compl], exact hs }, rcases h₂ with ⟨z₂, hz₂⟩, let f₂ : Πx', β x' := change f z₂, have hf₂ : f₂ ∉ pi i s, { refine mt _ hz₂, intro hf₂, convert hf₂ x hx, simp only [f₂, change, dif_pos] }, by_contradiction h2x, apply hf₂, apply hj _ hf, intros x' hx', have hxx' : x ≠ x', { refine mt _ h2x, intro h, rw h, exact hx' }, simp only [f₂, change, dif_neg hxx'] } end lemma support_elim {o : set (Πx, β x)} {f g : Πx, β x} (ho : o ∈ pi_basis β) (h : eq_on' f g (support o)) (hf : f ∈ o) : g ∈ o := begin rw [pi_basis_eq] at ho, rcases ho with ⟨⟨s, i, hs, rfl⟩, hs'⟩, have : nonempty (pi ↑i s), { rw [←nmem_singleton_empty], exact hs' }, intros x hx, rw [support_pi _ _ this] at h, by_cases h' : s x = univ, rw [h'], trivial, rw [← h ⟨hx, h'⟩], exact hf x hx end lemma finite_support_of_pi_subbasis {o : set (Πx, β x)} (h : o ∈ pi_subbasis β) : finite (support o) := begin rcases h with ⟨⟨i, o⟩, rfl⟩, apply finite_subset (finite_singleton i), apply sInter_subset_of_mem, dsimp [standard_open], intros f g hfg hf, rwa [hfg (mem_singleton i)] at hf end lemma finite_support_of_pi_basis {o : set (Πx, β x)} (h : o ∈ pi_basis β) : finite (support o) := begin rcases h with ⟨s, ⟨hs, h2s, h3s⟩, rfl⟩, apply finite_subset (finite_bUnion' support hs $ λ i hi, finite_support_of_pi_subbasis $ h2s hi), apply sInter_subset_of_mem, dsimp only, intros f g hfg hf, intros t ht, apply support_elim _ _ (hf t ht), { apply subbasis_subset_basis (is_subbasis_pi β), split, exact h2s ht, refine mt _ h3s, rw [mem_singleton_iff], rintro rfl, refine subset.antisymm _ (empty_subset _), exact sInter_subset_of_mem ht }, intros x hx, apply hfg, exact mem_bUnion ht hx end def extend (g₁ g₂ : Πx, β x) (s : set α) (x : α) : β x := if h : x ∈ s then g₁ x else g₂ x open delta_system theorem countable_chain_condition_pi (h : ∀(s : set α), finite s → countable_chain_condition (Π(x : s), β x)) : countable_chain_condition (Πx, β x) := begin apply countable_chain_condition_of_topological_basis _ (is_topological_basis_pi β), intros C hC h2C, rw [countable_iff], apply le_of_not_gt, intro h3C, let A : C → set α := λ s, support s.1, have h2A : ∀ (s : C), finite (A s), { rintro ⟨s, hs⟩, apply finite_support_of_pi_basis, exact hC hs }, rcases delta_system_lemma_uncountable A h3C h2A with ⟨C', h1C', ⟨R, hR⟩⟩, have h3A' : 2 ≤ mk C', { apply le_of_lt, refine lt_trans _ h1C', convert cardinal.nat_lt_omega 2, rw [nat.cast_bit0, nat.cast_one] }, have h2R : finite R, { apply finite_root h3A' _ hR, intro s, exact h2A s.1 }, let C'' := subtype.val '' C', have hC'' : C'' ⊆ C, { rintro _ ⟨⟨x, hx⟩, h2x, rfl⟩, exact hx }, let π : (Πx, β x) → Π(x : R), β x := λ f, restrict f R, let D : set (set (Π(x : R), β x)) := image π '' C'', have hD : ∀ ⦃o : set (Π (x : R), β x)⦄, o ∈ D → is_open o, { rintro _ ⟨o, ho, rfl⟩, apply is_open_map_restrict, apply is_open_of_is_topological_basis (is_topological_basis_pi β) (hC $ hC'' ho) }, have h2'D : ∀(s t : set (Πx, β x)), s ∈ C'' → t ∈ C'' → s ≠ t → disjoint (π '' s) (π '' t), { rintro s t hs ht hst, rw [disjoint_iff_eq_empty, eq_empty_iff_forall_not_mem], rintro f ⟨hfs, hft⟩, have := h2C _ (hC'' hs) _ (hC'' ht) (λ h, hst $ by rw h), rcases hfs with ⟨g₁, hg₁, rfl⟩, rcases hft with ⟨g₂, hg₂, hg⟩, rw [disjoint_iff_eq_empty, eq_empty_iff_forall_not_mem] at this, apply this (extend g₁ g₂ (support s)), split, { apply support_elim (hC (hC'' hs)) _ hg₁, intros x hx, rw [extend, dif_pos hx] }, apply support_elim (hC (hC'' ht)) _ hg₂, intros x hx, by_cases x ∈ support s, { rw [extend, dif_pos h], rw [←eq_on'_iff] at hg, apply hg, rcases hs with ⟨s', hs', rfl⟩, rcases ht with ⟨t', ht', rfl⟩, rw [←@hR ⟨s', hs'⟩ ⟨t', ht'⟩], exact ⟨h, hx⟩, intro h, apply hst, apply congr_arg subtype.val (congr_arg subtype.val h) }, rw [extend, dif_neg h] }, have h2D : pairwise_disjoint D, { rintro _ ⟨s, hs, rfl⟩ _ ⟨t, ht, rfl⟩ hst, apply h2'D s t hs ht, intro h, apply hst, rw h }, have h3D : cardinal.omega < mk D, { rw [mk_image_eq_of_inj_on, mk_image_eq], exact h1C', apply subtype.val_injective, intros s t hs ht hst, by_contradiction hst', apply ne_of_disjoint _ (h2'D s t hs ht hst') hst, apply coe_nonempty_iff_ne_empty.mp, apply nonempty_image, apply nonempty_of_mem_pi_basis (hC $ hC'' hs) }, apply not_le_of_gt h3D, rw [←countable_iff], exact h R h2R D hD h2D end end pi
74e6e05ab4eca2adf70b72acc8d0953f7ed048d7
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/ring_theory/jacobson.lean
983779736aa2318484934dc482d019f701c0f721
[ "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
33,286
lean
/- Copyright (c) 2020 Devon Tuma. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Devon Tuma -/ import data.mv_polynomial import ring_theory.ideal.over import ring_theory.jacobson_ideal import ring_theory.localization /-! # Jacobson Rings The following conditions are equivalent for a ring `R`: 1. Every radical ideal `I` is equal to its Jacobson radical 2. Every radical ideal `I` can be written as an intersection of maximal ideals 3. Every prime ideal `I` is equal to its Jacobson radical Any ring satisfying any of these equivalent conditions is said to be Jacobson. Some particular examples of Jacobson rings are also proven. `is_jacobson_quotient` says that the quotient of a Jacobson ring is Jacobson. `is_jacobson_localization` says the localization of a Jacobson ring to a single element is Jacobson. `is_jacobson_polynomial_iff_is_jacobson` says polynomials over a Jacobson ring form a Jacobson ring. ## Main definitions Let `R` be a commutative ring. Jacobson Rings are defined using the first of the above conditions * `is_jacobson R` is the proposition that `R` is a Jacobson ring. It is a class, implemented as the predicate that for any ideal, `I.radical = I` implies `I.jacobson = I`. ## Main statements * `is_jacobson_iff_prime_eq` is the equivalence between conditions 1 and 3 above. * `is_jacobson_iff_Inf_maximal` is the equivalence between conditions 1 and 2 above. * `is_jacobson_of_surjective` says that if `R` is a Jacobson ring and `f : R →+* S` is surjective, then `S` is also a Jacobson ring * `is_jacobson_mv_polynomial` says that multi-variate polynomials over a Jacobson ring are Jacobson. ## Tags Jacobson, Jacobson Ring -/ namespace ideal open polynomial section is_jacobson variables {R S : Type*} [comm_ring R] [comm_ring S] {I : ideal R} /-- A ring is a Jacobson ring if for every radical ideal `I`, the Jacobson radical of `I` is equal to `I`. See `is_jacobson_iff_prime_eq` and `is_jacobson_iff_Inf_maximal` for equivalent definitions. -/ class is_jacobson (R : Type*) [comm_ring R] : Prop := (out' : ∀ (I : ideal R), I.radical = I → I.jacobson = I) theorem is_jacobson_iff {R} [comm_ring R] : is_jacobson R ↔ ∀ (I : ideal R), I.radical = I → I.jacobson = I := ⟨λ h, h.1, λ h, ⟨h⟩⟩ theorem is_jacobson.out {R} [comm_ring R] : is_jacobson R → ∀ {I : ideal R}, I.radical = I → I.jacobson = I := is_jacobson_iff.1 /-- A ring is a Jacobson ring if and only if for all prime ideals `P`, the Jacobson radical of `P` is equal to `P`. -/ lemma is_jacobson_iff_prime_eq : is_jacobson R ↔ ∀ P : ideal R, is_prime P → P.jacobson = P := begin refine is_jacobson_iff.trans ⟨λ h I hI, h I (is_prime.radical hI), _⟩, refine λ h I hI, le_antisymm (λ x hx, _) (λ x hx, mem_Inf.mpr (λ _ hJ, hJ.left hx)), rw [← hI, radical_eq_Inf I, mem_Inf], intros P hP, rw set.mem_set_of_eq at hP, erw mem_Inf at hx, erw [← h P hP.right, mem_Inf], exact λ J hJ, hx ⟨le_trans hP.left hJ.left, hJ.right⟩ end /-- A ring `R` is Jacobson if and only if for every prime ideal `I`, `I` can be written as the infimum of some collection of maximal ideals. Allowing ⊤ in the set `M` of maximal ideals is equivalent, but makes some proofs cleaner. -/ lemma is_jacobson_iff_Inf_maximal : is_jacobson R ↔ ∀ {I : ideal R}, I.is_prime → ∃ M : set (ideal R), (∀ J ∈ M, is_maximal J ∨ J = ⊤) ∧ I = Inf M := ⟨λ H I h, eq_jacobson_iff_Inf_maximal.1 (H.out (is_prime.radical h)), λ H, is_jacobson_iff_prime_eq.2 (λ P hP, eq_jacobson_iff_Inf_maximal.2 (H hP))⟩ lemma is_jacobson_iff_Inf_maximal' : is_jacobson R ↔ ∀ {I : ideal R}, I.is_prime → ∃ M : set (ideal R), (∀ (J ∈ M) (K : ideal R), J < K → K = ⊤) ∧ I = Inf M := ⟨λ H I h, eq_jacobson_iff_Inf_maximal'.1 (H.out (is_prime.radical h)), λ H, is_jacobson_iff_prime_eq.2 (λ P hP, eq_jacobson_iff_Inf_maximal'.2 (H hP))⟩ lemma radical_eq_jacobson [H : is_jacobson R] (I : ideal R) : I.radical = I.jacobson := le_antisymm (le_Inf (λ J ⟨hJ, hJ_max⟩, (is_prime.radical_le_iff hJ_max.is_prime).mpr hJ)) ((H.out (radical_idem I)) ▸ (jacobson_mono le_radical)) /-- Fields have only two ideals, and the condition holds for both of them. -/ @[priority 100] instance is_jacobson_field {K : Type*} [field K] : is_jacobson K := ⟨λ I hI, or.rec_on (eq_bot_or_top I) (λ h, le_antisymm (Inf_le ⟨le_of_eq rfl, (eq.symm h) ▸ bot_is_maximal⟩) ((eq.symm h) ▸ bot_le)) (λ h, by rw [h, jacobson_eq_top_iff])⟩ theorem is_jacobson_of_surjective [H : is_jacobson R] : (∃ (f : R →+* S), function.surjective f) → is_jacobson S := begin rintros ⟨f, hf⟩, rw is_jacobson_iff_Inf_maximal, intros p hp, use map f '' {J : ideal R | comap f p ≤ J ∧ J.is_maximal }, use λ j ⟨J, hJ, hmap⟩, hmap ▸ or.symm (map_eq_top_or_is_maximal_of_surjective f hf hJ.right), have : p = map f ((comap f p).jacobson), from (is_jacobson.out' (comap f p) (by rw [← comap_radical, is_prime.radical hp])).symm ▸ (map_comap_of_surjective f hf p).symm, exact eq.trans this (map_Inf hf (λ J ⟨hJ, _⟩, le_trans (ideal.ker_le_comap f) hJ)), end @[priority 100] instance is_jacobson_quotient [is_jacobson R] : is_jacobson (quotient I) := is_jacobson_of_surjective ⟨quotient.mk I, (by rintro ⟨x⟩; use x; refl)⟩ lemma is_jacobson_iso (e : R ≃+* S) : is_jacobson R ↔ is_jacobson S := ⟨λ h, @is_jacobson_of_surjective _ _ _ _ h ⟨(e : R →+* S), e.surjective⟩, λ h, @is_jacobson_of_surjective _ _ _ _ h ⟨(e.symm : S →+* R), e.symm.surjective⟩⟩ lemma is_jacobson_of_is_integral [algebra R S] (hRS : algebra.is_integral R S) (hR : is_jacobson R) : is_jacobson S := begin rw is_jacobson_iff_prime_eq, introsI P hP, by_cases hP_top : comap (algebra_map R S) P = ⊤, { simp [comap_eq_top_iff.1 hP_top] }, { haveI : nontrivial (comap (algebra_map R S) P).quotient := quotient.nontrivial hP_top, rw jacobson_eq_iff_jacobson_quotient_eq_bot, refine eq_bot_of_comap_eq_bot (is_integral_quotient_of_is_integral hRS) _, rw [eq_bot_iff, ← jacobson_eq_iff_jacobson_quotient_eq_bot.1 ((is_jacobson_iff_prime_eq.1 hR) (comap (algebra_map R S) P) (comap_is_prime _ _)), comap_jacobson], refine Inf_le_Inf (λ J hJ, _), simp only [true_and, set.mem_image, bot_le, set.mem_set_of_eq], haveI : J.is_maximal := by simpa using hJ, exact exists_ideal_over_maximal_of_is_integral (is_integral_quotient_of_is_integral hRS) J (comap_bot_le_of_injective _ algebra_map_quotient_injective) } end lemma is_jacobson_of_is_integral' (f : R →+* S) (hf : f.is_integral) (hR : is_jacobson R) : is_jacobson S := @is_jacobson_of_is_integral _ _ _ _ f.to_algebra hf hR end is_jacobson section localization open localization_map submonoid variables {R S : Type*} [comm_ring R] [comm_ring S] {I : ideal R} variables {y : R} (f : away_map y S) lemma disjoint_powers_iff_not_mem (hI : I.radical = I) : disjoint ((submonoid.powers y) : set R) ↑I ↔ y ∉ I.1 := begin refine ⟨λ h, set.disjoint_left.1 h (mem_powers _), λ h, (disjoint_iff).mpr (eq_bot_iff.mpr _)⟩, rintros x ⟨⟨n, rfl⟩, hx'⟩, rw [← hI] at hx', exact absurd (hI ▸ mem_radical_of_pow_mem hx' : y ∈ I.carrier) h end /-- If `R` is a Jacobson ring, then maximal ideals in the localization at `y` correspond to maximal ideals in the original ring `R` that don't contain `y`. This lemma gives the correspondence in the particular case of an ideal and its comap. See `le_rel_iso_of_maximal` for the more general relation isomorphism -/ lemma is_maximal_iff_is_maximal_disjoint [H : is_jacobson R] (J : ideal S) : J.is_maximal ↔ (comap f.to_map J).is_maximal ∧ y ∉ ideal.comap f.to_map J := begin split, { refine λ h, ⟨_, λ hy, h.ne_top (ideal.eq_top_of_is_unit_mem _ hy (map_units f ⟨y, submonoid.mem_powers _⟩))⟩, have hJ : J.is_prime := is_maximal.is_prime h, rw is_prime_iff_is_prime_disjoint f at hJ, have : y ∉ (comap f.to_map J).1 := set.disjoint_left.1 hJ.right (submonoid.mem_powers _), erw [← H.out (is_prime.radical hJ.left), mem_Inf] at this, push_neg at this, rcases this with ⟨I, hI, hI'⟩, convert hI.right, by_cases hJ : J = map f.to_map I, { rw [hJ, comap_map_of_is_prime_disjoint f I (is_maximal.is_prime hI.right)], rwa disjoint_powers_iff_not_mem (is_maximal.is_prime hI.right).radical}, { have hI_p : (map f.to_map I).is_prime, { refine is_prime_of_is_prime_disjoint f I hI.right.is_prime _, rwa disjoint_powers_iff_not_mem (is_maximal.is_prime hI.right).radical }, have : J ≤ map f.to_map I := (map_comap f J) ▸ (map_mono hI.left), exact absurd (h.1.2 _ (lt_of_le_of_ne this hJ)) hI_p.1 } }, { refine λ h, ⟨⟨λ hJ, h.1.ne_top (eq_top_iff.2 _), λ I hI, _⟩⟩, { rwa [eq_top_iff, ← f.order_embedding.le_iff_le] at hJ }, { have := congr_arg (map f.to_map) (h.1.1.2 _ ⟨comap_mono (le_of_lt hI), _⟩), rwa [map_comap f I, map_top f.to_map] at this, refine λ hI', hI.right _, rw [← map_comap f I, ← map_comap f J], exact map_mono hI' } } end /-- If `R` is a Jacobson ring, then maximal ideals in the localization at `y` correspond to maximal ideals in the original ring `R` that don't contain `y`. This lemma gives the correspondence in the particular case of an ideal and its map. See `le_rel_iso_of_maximal` for the more general statement, and the reverse of this implication -/ lemma is_maximal_of_is_maximal_disjoint [is_jacobson R] (I : ideal R) (hI : I.is_maximal) (hy : y ∉ I) : (map f.to_map I).is_maximal := begin rw [is_maximal_iff_is_maximal_disjoint f, comap_map_of_is_prime_disjoint f I (is_maximal.is_prime hI) ((disjoint_powers_iff_not_mem (is_maximal.is_prime hI).radical).2 hy)], exact ⟨hI, hy⟩ end /-- If `R` is a Jacobson ring, then maximal ideals in the localization at `y` correspond to maximal ideals in the original ring `R` that don't contain `y` -/ def order_iso_of_maximal [is_jacobson R] : {p : ideal S // p.is_maximal} ≃o {p : ideal R // p.is_maximal ∧ y ∉ p} := { to_fun := λ p, ⟨ideal.comap f.to_map p.1, (is_maximal_iff_is_maximal_disjoint f p.1).1 p.2⟩, inv_fun := λ p, ⟨ideal.map f.to_map p.1, is_maximal_of_is_maximal_disjoint f p.1 p.2.1 p.2.2⟩, left_inv := λ J, subtype.eq (map_comap f J), right_inv := λ I, subtype.eq (comap_map_of_is_prime_disjoint f I.1 (is_maximal.is_prime I.2.1) ((disjoint_powers_iff_not_mem I.2.1.is_prime.radical).2 I.2.2)), map_rel_iff' := λ I I', ⟨λ h, (show I.val ≤ I'.val, from (map_comap f I.val) ▸ (map_comap f I'.val) ▸ (ideal.map_mono h)), λ h x hx, h hx⟩ } /-- If `S` is the localization of the Jacobson ring `R` at the submonoid generated by `y : R`, then `S` is Jacobson. -/ lemma is_jacobson_localization [H : is_jacobson R] (f : away_map y S) : is_jacobson S := begin rw is_jacobson_iff_prime_eq, refine λ P' hP', le_antisymm _ le_jacobson, obtain ⟨hP', hPM⟩ := (localization_map.is_prime_iff_is_prime_disjoint f P').mp hP', have hP := H.out (is_prime.radical hP'), refine le_trans (le_trans (le_of_eq (localization_map.map_comap f P'.jacobson).symm) (map_mono _)) (le_of_eq (localization_map.map_comap f P')), have : Inf { I : ideal R | comap f.to_map P' ≤ I ∧ I.is_maximal ∧ y ∉ I } ≤ comap f.to_map P', { intros x hx, have hxy : x * y ∈ (comap f.to_map P').jacobson, { rw [ideal.jacobson, mem_Inf], intros J hJ, by_cases y ∈ J, { exact J.smul_mem x h }, { exact (mul_comm y x) ▸ J.smul_mem y ((mem_Inf.1 hx) ⟨hJ.left, ⟨hJ.right, h⟩⟩) } }, rw hP at hxy, cases hP'.mem_or_mem hxy with hxy hxy, { exact hxy }, { exact (hPM ⟨submonoid.mem_powers _, hxy⟩).elim } }, refine le_trans _ this, rw [ideal.jacobson, comap_Inf', Inf_eq_infi], refine infi_le_infi_of_subset (λ I hI, ⟨map f.to_map I, ⟨_, _⟩⟩), { exact ⟨le_trans (le_of_eq ((localization_map.map_comap f P').symm)) (map_mono hI.1), is_maximal_of_is_maximal_disjoint f _ hI.2.1 hI.2.2⟩ }, { exact localization_map.comap_map_of_is_prime_disjoint f I (is_maximal.is_prime hI.2.1) ((disjoint_powers_iff_not_mem hI.2.1.is_prime.radical).2 hI.2.2) } end end localization namespace polynomial open polynomial section comm_ring variables {R S : Type*} [comm_ring R] [integral_domain S] variables {Rₘ Sₘ : Type*} [comm_ring Rₘ] [comm_ring Sₘ] /-- If `I` is a prime ideal of `polynomial R` and `pX ∈ I` is a non-constant polynomial, then the map `R →+* R[x]/I` descends to an integral map when localizing at `pX.leading_coeff`. In particular `X` is integral because it satisfies `pX`, and constants are trivially integral, so integrality of the entire extension follows by closure under addition and multiplication. -/ lemma is_integral_localization_map_polynomial_quotient (P : ideal (polynomial R)) [P.is_prime] (pX : polynomial R) (hpX : pX ∈ P) (ϕ : localization_map (submonoid.powers (pX.map (quotient.mk (P.comap C))).leading_coeff) Rₘ) (ϕ' : localization_map ((submonoid.powers (pX.map (quotient.mk (P.comap C))).leading_coeff).map (quotient_map P C le_rfl) : submonoid P.quotient) Sₘ) : (ϕ.map ((submonoid.powers (pX.map (quotient.mk (P.comap C))).leading_coeff).apply_coe_mem_map (quotient_map P C le_rfl : (P.comap C : ideal R).quotient →* P.quotient)) ϕ').is_integral := begin let P' : ideal R := P.comap C, let M : submonoid P'.quotient := submonoid.powers (pX.map (quotient.mk (P.comap C))).leading_coeff, let φ : P'.quotient →+* P.quotient := quotient_map P C le_rfl, let φ' := (ϕ.map (M.apply_coe_mem_map (φ : P'.quotient →* P.quotient)) ϕ'), have hφ' : φ.comp (quotient.mk P') = (quotient.mk P).comp C := rfl, intro p, obtain ⟨⟨p', ⟨q, hq⟩⟩, hp⟩ := ϕ'.surj p, suffices : φ'.is_integral_elem (ϕ'.to_map p'), { obtain ⟨q', hq', rfl⟩ := hq, obtain ⟨q'', hq''⟩ := is_unit_iff_exists_inv'.1 (ϕ.map_units ⟨q', hq'⟩), refine φ'.is_integral_of_is_integral_mul_unit p (ϕ'.to_map (φ q')) q'' _ (hp.symm ▸ this), convert trans (trans (φ'.map_mul _ _).symm (congr_arg φ' hq'')) φ'.map_one using 2, rw [← φ'.comp_apply, localization_map.map_comp, ϕ'.to_map.comp_apply, subtype.coe_mk] }, refine is_integral_of_mem_closure'' ((ϕ'.to_map.comp (quotient.mk P)) '' (insert X {p | p.degree ≤ 0})) _ _ _, { rintros x ⟨p, hp, rfl⟩, refine hp.rec_on (λ hy, _) (λ hy, _), { refine hy.symm ▸ (φ.is_integral_elem_localization_at_leading_coeff ((quotient.mk P) X) (pX.map (quotient.mk P')) _ M ⟨1, pow_one _⟩ _ _), rwa [eval₂_map, hφ', ← hom_eval₂, quotient.eq_zero_iff_mem, eval₂_C_X] }, { rw [set.mem_set_of_eq, degree_le_zero_iff] at hy, refine hy.symm ▸ ⟨X - C (ϕ.to_map ((quotient.mk P') (p.coeff 0))), monic_X_sub_C _, _⟩, simp only [eval₂_sub, eval₂_C, eval₂_X], rw [sub_eq_zero, ← φ'.comp_apply, localization_map.map_comp, ring_hom.comp_apply], refl } }, { obtain ⟨p, rfl⟩ := quotient.mk_surjective p', refine polynomial.induction_on p (λ r, subring.subset_closure $ set.mem_image_of_mem _ (or.inr degree_C_le)) (λ _ _ h1 h2, _) (λ n _ hr, _), { convert subring.add_mem _ h1 h2, rw [ring_hom.map_add, ring_hom.map_add] }, { rw [pow_succ X n, mul_comm X, ← mul_assoc, ring_hom.map_mul, ϕ'.to_map.map_mul], exact subring.mul_mem _ hr (subring.subset_closure (set.mem_image_of_mem _ (or.inl rfl))) } }, end /-- If `f : R → S` descends to an integral map in the localization at `x`, and `R` is a Jacobson ring, then the intersection of all maximal ideals in `S` is trivial -/ lemma jacobson_bot_of_integral_localization {R : Type*} [integral_domain R] [is_jacobson R] (φ : R →+* S) (hφ : function.injective φ) (x : R) (hx : x ≠ 0) (ϕ : localization_map (submonoid.powers x) Rₘ) (ϕ' : localization_map ((submonoid.powers x).map φ : submonoid S) Sₘ) (hφ' : (ϕ.map ((submonoid.powers x).apply_coe_mem_map (φ : R →* S)) ϕ').is_integral) : (⊥ : ideal S).jacobson = ⊥ := begin have hM : ((submonoid.powers x).map φ : submonoid S) ≤ non_zero_divisors S := map_le_non_zero_divisors_of_injective hφ (powers_le_non_zero_divisors_of_domain hx), letI : integral_domain Sₘ := localization_map.integral_domain_of_le_non_zero_divisors ϕ' hM, let φ' : Rₘ →+* Sₘ := ϕ.map ((submonoid.powers x).apply_coe_mem_map (φ : R →* S)) ϕ', suffices : ∀ I : ideal Sₘ, I.is_maximal → (I.comap ϕ'.to_map).is_maximal, { have hϕ' : comap ϕ'.to_map ⊥ = ⊥, { simpa [ring_hom.injective_iff_ker_eq_bot, ring_hom.ker_eq_comap_bot] using ϕ'.injective hM }, refine eq_bot_iff.2 (le_trans _ (le_of_eq hϕ')), have hSₘ : is_jacobson Sₘ := is_jacobson_of_is_integral' φ' hφ' (is_jacobson_localization ϕ), rw [← hSₘ.out radical_bot_of_integral_domain, comap_jacobson], exact Inf_le_Inf (λ j hj, ⟨bot_le, let ⟨J, hJ⟩ := hj in hJ.2 ▸ this J hJ.1.2⟩) }, introsI I hI, -- Remainder of the proof is pulling and pushing ideals around the square and the quotient square haveI : (I.comap ϕ'.to_map).is_prime := comap_is_prime ϕ'.to_map I, haveI : (I.comap φ').is_prime := comap_is_prime φ' I, haveI : (⊥ : ideal (I.comap ϕ'.to_map).quotient).is_prime := bot_prime, have hcomm: φ'.comp ϕ.to_map = ϕ'.to_map.comp φ := ϕ.map_comp _, let f := quotient_map (I.comap ϕ'.to_map) φ le_rfl, let g := quotient_map I ϕ'.to_map le_rfl, have := is_maximal_comap_of_is_integral_of_is_maximal' φ' hφ' I (by convert hI; casesI _inst_4; refl), have := ((is_maximal_iff_is_maximal_disjoint ϕ _).1 this).left, have : ((I.comap ϕ'.to_map).comap φ).is_maximal, { rwa [comap_comap, hcomm, ← comap_comap] at this }, rw ← bot_quotient_is_maximal_iff at this ⊢, refine is_maximal_of_is_integral_of_is_maximal_comap' f _ ⊥ ((eq_bot_iff.2 (comap_bot_le_of_injective f quotient_map_injective)).symm ▸ this), exact f.is_integral_tower_bot_of_is_integral g quotient_map_injective ((comp_quotient_map_eq_of_comp_eq hcomm I).symm ▸ (ring_hom.is_integral_trans _ _ (ring_hom.is_integral_of_surjective _ (localization_map.surjective_quotient_map_of_maximal_of_localization (by rwa [comap_comap, hcomm, ← bot_quotient_is_maximal_iff]))) (ring_hom.is_integral_quotient_of_is_integral _ hφ'))), end /-- Used to bootstrap the proof of `is_jacobson_polynomial_iff_is_jacobson`. That theorem is more general and should be used instead of this one. -/ private lemma is_jacobson_polynomial_of_domain (R : Type*) [integral_domain R] [hR : is_jacobson R] (P : ideal (polynomial R)) [is_prime P] (hP : ∀ (x : R), C x ∈ P → x = 0) : P.jacobson = P := begin by_cases Pb : (P = ⊥), { exact Pb.symm ▸ jacobson_bot_polynomial_of_jacobson_bot (hR.out radical_bot_of_integral_domain) }, { refine jacobson_eq_iff_jacobson_quotient_eq_bot.mpr _, haveI : (P.comap (C : R →+* polynomial R)).is_prime := comap_is_prime C P, obtain ⟨p, pP, p0⟩ := exists_nonzero_mem_of_ne_bot Pb hP, refine jacobson_bot_of_integral_localization (quotient_map P C le_rfl) quotient_map_injective _ _ (localization.of (submonoid.powers (p.map (quotient.mk (P.comap C))).leading_coeff)) (localization.of _) (by apply (is_integral_localization_map_polynomial_quotient P _ pP _ _)), rwa [ne.def, leading_coeff_eq_zero] } end lemma is_jacobson_polynomial_of_is_jacobson (hR : is_jacobson R) : is_jacobson (polynomial R) := begin refine is_jacobson_iff_prime_eq.mpr (λ I, _), introI hI, let R' : subring I.quotient := ((quotient.mk I).comp C).range, let i : R →+* R' := ((quotient.mk I).comp C).range_restrict, have hi : function.surjective (i : R → R') := ((quotient.mk I).comp C).range_restrict_surjective, have hi' : (polynomial.map_ring_hom i : polynomial R →+* polynomial R').ker ≤ I, { refine λ f hf, polynomial_mem_ideal_of_coeff_mem_ideal I f (λ n, _), replace hf := congr_arg (λ (g : polynomial (((quotient.mk I).comp C).range)), g.coeff n) hf, change (polynomial.map ((quotient.mk I).comp C).range_restrict f).coeff n = 0 at hf, rw [coeff_map, subtype.ext_iff] at hf, rwa [mem_comap, ← quotient.eq_zero_iff_mem, ← ring_hom.comp_apply], }, haveI : (ideal.map (map_ring_hom i) I).is_prime := map_is_prime_of_surjective (map_surjective i hi) hi', suffices : (I.map (polynomial.map_ring_hom i)).jacobson = (I.map (polynomial.map_ring_hom i)), { replace this := congr_arg (comap (polynomial.map_ring_hom i)) this, rw [← map_jacobson_of_surjective _ hi', comap_map_of_surjective _ _, comap_map_of_surjective _ _] at this, refine le_antisymm (le_trans (le_sup_of_le_left le_rfl) (le_trans (le_of_eq this) (sup_le le_rfl hi'))) le_jacobson, all_goals {exact polynomial.map_surjective i hi} }, exact @is_jacobson_polynomial_of_domain R' _ (is_jacobson_of_surjective ⟨i, hi⟩) (map (map_ring_hom i) I) _ (eq_zero_of_polynomial_mem_map_range I), end theorem is_jacobson_polynomial_iff_is_jacobson : is_jacobson (polynomial R) ↔ is_jacobson R := begin refine ⟨_, is_jacobson_polynomial_of_is_jacobson⟩, introI H, exact is_jacobson_of_surjective ⟨eval₂_ring_hom (ring_hom.id _) 1, λ x, ⟨C x, by simp only [coe_eval₂_ring_hom, ring_hom.id_apply, eval₂_C]⟩⟩, end instance [is_jacobson R] : is_jacobson (polynomial R) := is_jacobson_polynomial_iff_is_jacobson.mpr ‹is_jacobson R› end comm_ring section integral_domain variables {R : Type*} [integral_domain R] [is_jacobson R] variables (P : ideal (polynomial R)) [hP : P.is_maximal] include P hP lemma is_maximal_comap_C_of_is_maximal (hP' : ∀ (x : R), C x ∈ P → x = 0) : is_maximal (comap C P : ideal R) := begin haveI hp'_prime : (P.comap C : ideal R).is_prime := comap_is_prime C P, obtain ⟨m, hm⟩ := submodule.nonzero_mem_of_bot_lt (bot_lt_of_maximal P polynomial_not_is_field), have : (m : polynomial R) ≠ 0, rwa [ne.def, submodule.coe_eq_zero], let φ : (P.comap C : ideal R).quotient →+* P.quotient := quotient_map P C le_rfl, let M : submonoid (P.comap C : ideal R).quotient := submonoid.powers ((m : polynomial R).map (quotient.mk (P.comap C : ideal R))).leading_coeff, rw ← bot_quotient_is_maximal_iff at hP ⊢, have hp0 : ((m : polynomial R).map (quotient.mk (P.comap C : ideal R))).leading_coeff ≠ 0 := λ hp0', this $ map_injective (quotient.mk (P.comap C : ideal R)) ((quotient.mk (P.comap C : ideal R)).injective_iff.2 (λ x hx, by rwa [quotient.eq_zero_iff_mem, (by rwa eq_bot_iff : (P.comap C : ideal R) = ⊥)] at hx)) (by simpa only [leading_coeff_eq_zero, map_zero] using hp0'), let ϕ : localization_map M (localization M) := localization.of M, have hM : (0 : ((P.comap C : ideal R)).quotient) ∉ M := λ ⟨n, hn⟩, hp0 (pow_eq_zero hn), suffices : (⊥ : ideal (localization M)).is_maximal, { rw ← ϕ.comap_map_of_is_prime_disjoint ⊥ bot_prime (λ x hx, hM (hx.2 ▸ hx.1)), refine ((is_maximal_iff_is_maximal_disjoint ϕ _).mp _).1, rwa map_bot }, let M' : submonoid P.quotient := M.map φ, have hM' : (0 : P.quotient) ∉ M' := λ ⟨z, hz⟩, hM (quotient_map_injective (trans hz.2 φ.map_zero.symm) ▸ hz.1), letI : integral_domain (localization M') := localization_map.integral_domain_localization (le_non_zero_divisors_of_domain hM'), let ϕ' : localization_map (M.map ↑φ) (localization (M.map ↑φ)) := localization.of (M.map ↑φ), suffices : (⊥ : ideal (localization M')).is_maximal, { rw le_antisymm bot_le (comap_bot_le_of_injective _ (map_injective_of_injective _ quotient_map_injective M ϕ ϕ' (le_non_zero_divisors_of_domain hM'))), refine is_maximal_comap_of_is_integral_of_is_maximal' _ _ ⊥ this, apply is_integral_localization_map_polynomial_quotient P _ (submodule.coe_mem m) ϕ (ϕ' : _) }, rw (map_bot.symm : (⊥ : ideal (localization M')) = map ϕ'.to_map ⊥), refine map.is_maximal ϕ'.to_map (localization_map_bijective_of_field hM' _ ϕ') hP, rwa [← quotient.maximal_ideal_iff_is_field_quotient, ← bot_quotient_is_maximal_iff], end /-- Used to bootstrap the more general `quotient_mk_comp_C_is_integral_of_jacobson` -/ private lemma quotient_mk_comp_C_is_integral_of_jacobson' (hR : is_jacobson R) (hP' : ∀ (x : R), C x ∈ P → x = 0) : ((quotient.mk P).comp C : R →+* P.quotient).is_integral := begin refine (is_integral_quotient_map_iff _).mp _, let P' : ideal R := P.comap C, obtain ⟨pX, hpX, hp0⟩ := exists_nonzero_mem_of_ne_bot (ne_of_lt (bot_lt_of_maximal P polynomial_not_is_field)).symm hP', let M : submonoid P'.quotient := submonoid.powers (pX.map (quotient.mk P')).leading_coeff, let φ : P'.quotient →+* P.quotient := quotient_map P C le_rfl, let ϕ' : localization_map (M.map ↑φ) (localization (M.map ↑φ)) := localization.of (M.map ↑φ), haveI hp'_prime : P'.is_prime := comap_is_prime C P, have hM : (0 : P'.quotient) ∉ M := λ ⟨n, hn⟩, hp0 $ leading_coeff_eq_zero.mp (pow_eq_zero hn), refine ((quotient_map P C le_rfl).is_integral_tower_bot_of_is_integral (localization.of (M.map ↑(quotient_map P C le_rfl))).to_map _ _), { refine ϕ'.injective (le_non_zero_divisors_of_domain (λ hM', hM _)), exact (let ⟨z, zM, z0⟩ := hM' in (quotient_map_injective (trans z0 φ.map_zero.symm)) ▸ zM) }, { let ϕ : localization_map M (localization M) := localization.of M, rw ← (ϕ.map_comp _), refine ring_hom.is_integral_trans ϕ.to_map (ϕ.map (M.apply_coe_mem_map (φ : P'.quotient →* P.quotient)) ϕ') _ _, { exact ϕ.to_map.is_integral_of_surjective (localization_map_bijective_of_field hM ((quotient.maximal_ideal_iff_is_field_quotient _).mp (is_maximal_comap_C_of_is_maximal P hP')) _).2 }, { -- `convert` here is faster than `exact`, and this proof is near the time limit. convert is_integral_localization_map_polynomial_quotient P pX hpX _ _ } } end /-- If `R` is a Jacobson ring, and `P` is a maximal ideal of `polynomial R`, then `R → (polynomial R)/P` is an integral map. -/ lemma quotient_mk_comp_C_is_integral_of_jacobson : ((quotient.mk P).comp C : R →+* P.quotient).is_integral := begin let P' : ideal R := P.comap C, haveI : P'.is_prime := comap_is_prime C P, let f : polynomial R →+* polynomial P'.quotient := polynomial.map_ring_hom (quotient.mk P'), have hf : function.surjective f := map_surjective (quotient.mk P') quotient.mk_surjective, have hPJ : P = (P.map f).comap f, { rw comap_map_of_surjective _ hf, refine le_antisymm (le_sup_of_le_left le_rfl) (sup_le le_rfl _), refine λ p hp, polynomial_mem_ideal_of_coeff_mem_ideal P p (λ n, quotient.eq_zero_iff_mem.mp _), simpa only [coeff_map, coe_map_ring_hom] using (polynomial.ext_iff.mp hp) n }, refine ring_hom.is_integral_tower_bot_of_is_integral _ _ (injective_quotient_le_comap_map P) _, rw ← quotient_mk_maps_eq, refine ring_hom.is_integral_trans _ _ ((quotient.mk P').is_integral_of_surjective quotient.mk_surjective) _, apply quotient_mk_comp_C_is_integral_of_jacobson' _ _ (λ x hx, _), any_goals { exact ideal.is_jacobson_quotient }, { exact or.rec_on (map_eq_top_or_is_maximal_of_surjective f hf hP) (λ h, absurd (trans (h ▸ hPJ : P = comap f ⊤) comap_top : P = ⊤) hP.ne_top) id }, { obtain ⟨z, rfl⟩ := quotient.mk_surjective x, rwa [quotient.eq_zero_iff_mem, mem_comap, hPJ, mem_comap, coe_map_ring_hom, map_C] } end lemma is_maximal_comap_C_of_is_jacobson : (P.comap (C : R →+* polynomial R)).is_maximal := begin rw [← @mk_ker _ _ P, ring_hom.ker_eq_comap_bot, comap_comap], exact is_maximal_comap_of_is_integral_of_is_maximal' _ (quotient_mk_comp_C_is_integral_of_jacobson P) ⊥ ((bot_quotient_is_maximal_iff _).mpr hP), end omit P hP lemma comp_C_integral_of_surjective_of_jacobson {S : Type*} [field S] (f : (polynomial R) →+* S) (hf : function.surjective f) : (f.comp C).is_integral := begin haveI : (f.ker).is_maximal := @comap_is_maximal_of_surjective _ _ _ _ f ⊥ hf bot_is_maximal, let g : f.ker.quotient →+* S := ideal.quotient.lift f.ker f (λ _ h, h), have hfg : (g.comp (quotient.mk f.ker)) = f := ring_hom_ext' rfl rfl, rw [← hfg, ring_hom.comp_assoc], refine ring_hom.is_integral_trans _ g (quotient_mk_comp_C_is_integral_of_jacobson f.ker) (g.is_integral_of_surjective _), --(quotient.lift_surjective f.ker f _ hf)), rw [← hfg] at hf, exact function.surjective.of_comp hf, end end integral_domain end polynomial namespace mv_polynomial open mv_polynomial ring_hom lemma is_jacobson_mv_polynomial_fin {R : Type*} [comm_ring R] [H : is_jacobson R] : ∀ (n : ℕ), is_jacobson (mv_polynomial (fin n) R) | 0 := ((is_jacobson_iso ((rename_equiv R (equiv.equiv_pempty (fin 0))).to_ring_equiv.trans (pempty_ring_equiv R))).mpr H) | (n+1) := (is_jacobson_iso (fin_succ_equiv R n).to_ring_equiv).2 (polynomial.is_jacobson_polynomial_iff_is_jacobson.2 (is_jacobson_mv_polynomial_fin n)) /-- General form of the nullstellensatz for Jacobson rings, since in a Jacobson ring we have `Inf {P maximal | P ≥ I} = Inf {P prime | P ≥ I} = I.radical`. Fields are always Jacobson, and in that special case this is (most of) the classical Nullstellensatz, since `I(V(I))` is the intersection of maximal ideals containing `I`, which is then `I.radical` -/ instance {R : Type*} [comm_ring R] {ι : Type*} [fintype ι] [is_jacobson R] : is_jacobson (mv_polynomial ι R) := begin haveI := classical.dec_eq ι, let e := fintype.equiv_fin ι, rw is_jacobson_iso (rename_equiv R e).to_ring_equiv, exact is_jacobson_mv_polynomial_fin _ end variables {n : ℕ} lemma quotient_mk_comp_C_is_integral_of_jacobson {R : Type*} [integral_domain R] [is_jacobson R] (P : ideal (mv_polynomial (fin n) R)) [P.is_maximal] : ((quotient.mk P).comp mv_polynomial.C : R →+* P.quotient).is_integral := begin unfreezingI {induction n with n IH}, { refine ring_hom.is_integral_of_surjective _ (function.surjective.comp quotient.mk_surjective _), exact C_surjective_fin_0 }, { rw [← fin_succ_equiv_comp_C_eq_C, ← ring_hom.comp_assoc, ← ring_hom.comp_assoc, ← quotient_map_comp_mk le_rfl, ring_hom.comp_assoc (polynomial.C), ← quotient_map_comp_mk le_rfl, ring_hom.comp_assoc, ring_hom.comp_assoc, ← quotient_map_comp_mk le_rfl, ← ring_hom.comp_assoc (quotient.mk _)], refine ring_hom.is_integral_trans _ _ _ _, { refine ring_hom.is_integral_trans _ _ (is_integral_of_surjective _ quotient.mk_surjective) _, refine ring_hom.is_integral_trans _ _ _ _, { apply (is_integral_quotient_map_iff _).mpr (IH _), apply polynomial.is_maximal_comap_C_of_is_jacobson _, { exact mv_polynomial.is_jacobson_mv_polynomial_fin n }, { apply comap_is_maximal_of_surjective, exact (fin_succ_equiv R n).symm.surjective } }, { refine (is_integral_quotient_map_iff _).mpr _, rw ← quotient_map_comp_mk le_rfl, refine ring_hom.is_integral_trans _ _ _ ((is_integral_quotient_map_iff _).mpr _), { exact ring_hom.is_integral_of_surjective _ quotient.mk_surjective }, { apply polynomial.quotient_mk_comp_C_is_integral_of_jacobson _, { exact mv_polynomial.is_jacobson_mv_polynomial_fin n }, { exact comap_is_maximal_of_surjective _ (fin_succ_equiv R n).symm.surjective } } } }, { refine (is_integral_quotient_map_iff _).mpr _, refine ring_hom.is_integral_trans _ _ _ (is_integral_of_surjective _ quotient.mk_surjective), exact ring_hom.is_integral_of_surjective _ (fin_succ_equiv R n).symm.surjective } } end lemma comp_C_integral_of_surjective_of_jacobson {R : Type*} [integral_domain R] [is_jacobson R] {σ : Type*} [fintype σ] {S : Type*} [field S] (f : mv_polynomial σ R →+* S) (hf : function.surjective f) : (f.comp C).is_integral := begin haveI := classical.dec_eq σ, obtain ⟨e⟩ := fintype.trunc_equiv_fin σ, let f' : mv_polynomial (fin _) R →+* S := f.comp (rename_equiv R e.symm).to_ring_equiv.to_ring_hom, have hf' : function.surjective f' := ((function.surjective.comp hf (rename_equiv R e.symm).surjective)), have : (f'.comp C).is_integral, { haveI : (f'.ker).is_maximal := @comap_is_maximal_of_surjective _ _ _ _ f' ⊥ hf' bot_is_maximal, let g : f'.ker.quotient →+* S := ideal.quotient.lift f'.ker f' (λ _ h, h), have hfg : (g.comp (quotient.mk f'.ker)) = f' := ring_hom_ext (λ r, rfl) (λ i, rfl), rw [← hfg, ring_hom.comp_assoc], refine ring_hom.is_integral_trans _ g (quotient_mk_comp_C_is_integral_of_jacobson f'.ker) (g.is_integral_of_surjective _), rw ← hfg at hf', exact function.surjective.of_comp hf' }, rw ring_hom.comp_assoc at this, convert this, refine ring_hom.ext (λ x, _), exact ((rename_equiv R e.symm).commutes' x).symm, end end mv_polynomial end ideal
da9dce33e1411f3646c2e94b589fe4271679c2f8
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Compiler/FFI.lean
f0480df81f70389062b60cf1bf1ba4cdf4fa6a27
[ "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
874
lean
/- Copyright (c) 2021 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ open System namespace Lean.Compiler.FFI @[extern "lean_get_leanc_extra_flags"] private opaque getLeancExtraFlags : Unit → String /-- Return C compiler flags for including Lean's headers. -/ def getCFlags (leanSysroot : FilePath) : Array String := #["-I", (leanSysroot / "include").toString] ++ (getLeancExtraFlags ()).trim.splitOn @[extern "lean_get_linker_flags"] private opaque getBuiltinLinkerFlags (linkStatic : Bool) : String /-- Return linker flags for linking against Lean's libraries. -/ def getLinkerFlags (leanSysroot : FilePath) (linkStatic := true) : Array String := #["-L", (leanSysroot / "lib" / "lean").toString] ++ (getBuiltinLinkerFlags linkStatic).trim.splitOn end Lean.Compiler.FFI
e8a10cf6bd677d7366ec50c55af0c8e7f9bd451c
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/category_theory/functor_category.lean
0a4c61e546824a4cbf56f39b875f21e5e0c502df
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,342
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn -/ import category_theory.natural_transformation /-! # The category of functors and natural transformations between two fixed categories. We provide the category instance on `C ⥤ D`, with morphisms the natural transformations. ## Universes If `C` and `D` are both small categories at the same universe level, this is another small category at that level. However if `C` and `D` are both large categories at the same universe level, this is a small category at the next higher level. -/ namespace category_theory -- declare the `v`'s first; see `category_theory.category` for an explanation universes v₁ v₂ v₃ u₁ u₂ u₃ open nat_trans category category_theory.functor variables (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D] local attribute [simp] vcomp_app /-- `functor.category C D` gives the category structure on functors and natural transformations between categories `C` and `D`. Notice that if `C` and `D` are both small categories at the same universe level, this is another small category at that level. However if `C` and `D` are both large categories at the same universe level, this is a small category at the next higher level. -/ instance functor.category : category.{(max u₁ v₂)} (C ⥤ D) := { hom := λ F G, nat_trans F G, id := λ F, nat_trans.id F, comp := λ _ _ _ α β, vcomp α β } variables {C D} {E : Type u₃} [category.{v₃} E] variables {F G H I : C ⥤ D} namespace nat_trans @[simp] lemma vcomp_eq_comp (α : F ⟶ G) (β : G ⟶ H) : vcomp α β = α ≫ β := rfl lemma vcomp_app' (α : F ⟶ G) (β : G ⟶ H) (X : C) : (α ≫ β).app X = (α.app X) ≫ (β.app X) := rfl lemma congr_app {α β : F ⟶ G} (h : α = β) (X : C) : α.app X = β.app X := by rw h @[simp] lemma id_app (F : C ⥤ D) (X : C) : (𝟙 F : F ⟶ F).app X = 𝟙 (F.obj X) := rfl @[simp] lemma comp_app {F G H : C ⥤ D} (α : F ⟶ G) (β : G ⟶ H) (X : C) : (α ≫ β).app X = α.app X ≫ β.app X := rfl lemma app_naturality {F G : C ⥤ (D ⥤ E)} (T : F ⟶ G) (X : C) {Y Z : D} (f : Y ⟶ Z) : ((F.obj X).map f) ≫ ((T.app X).app Z) = ((T.app X).app Y) ≫ ((G.obj X).map f) := (T.app X).naturality f lemma naturality_app {F G : C ⥤ (D ⥤ E)} (T : F ⟶ G) (Z : D) {X Y : C} (f : X ⟶ Y) : ((F.map f).app Z) ≫ ((T.app Y).app Z) = ((T.app X).app Z) ≫ ((G.map f).app Z) := congr_fun (congr_arg app (T.naturality f)) Z /-- A natural transformation is a monomorphism if each component is. -/ lemma mono_app_of_mono (α : F ⟶ G) [∀ (X : C), mono (α.app X)] : mono α := ⟨λ H g h eq, by { ext X, rw [←cancel_mono (α.app X), ←comp_app, eq, comp_app] }⟩ /-- A natural transformation is an epimorphism if each component is. -/ lemma epi_app_of_epi (α : F ⟶ G) [∀ (X : C), epi (α.app X)] : epi α := ⟨λ H g h eq, by { ext X, rw [←cancel_epi (α.app X), ←comp_app, eq, comp_app] }⟩ /-- `hcomp α β` is the horizontal composition of natural transformations. -/ def hcomp {H I : D ⥤ E} (α : F ⟶ G) (β : H ⟶ I) : (F ⋙ H) ⟶ (G ⋙ I) := { app := λ X : C, (β.app (F.obj X)) ≫ (I.map (α.app X)), naturality' := λ X Y f, begin rw [functor.comp_map, functor.comp_map, ←assoc, naturality, assoc, ←map_comp I, naturality, map_comp, assoc] end } infix ` ◫ `:80 := hcomp @[simp] lemma hcomp_app {H I : D ⥤ E} (α : F ⟶ G) (β : H ⟶ I) (X : C) : (α ◫ β).app X = (β.app (F.obj X)) ≫ (I.map (α.app X)) := rfl @[simp] lemma hcomp_id_app {H : D ⥤ E} (α : F ⟶ G) (X : C) : (α ◫ 𝟙 H).app X = H.map (α.app X) := by {dsimp, simp} -- See note [dsimp, simp]. lemma id_hcomp_app {H : E ⥤ C} (α : F ⟶ G) (X : E) : (𝟙 H ◫ α).app X = α.app _ := by simp -- Note that we don't yet prove a `hcomp_assoc` lemma here: even stating it is painful, because we -- need to use associativity of functor composition. (It's true without the explicit associator, -- because functor composition is definitionally associative, -- but relying on the definitional equality causes bad problems with elaboration later.) lemma exchange {I J K : D ⥤ E} (α : F ⟶ G) (β : G ⟶ H) (γ : I ⟶ J) (δ : J ⟶ K) : (α ≫ β) ◫ (γ ≫ δ) = (α ◫ γ) ≫ (β ◫ δ) := by ext; simp end nat_trans open nat_trans namespace functor /-- Flip the arguments of a bifunctor. See also `currying.lean`. -/ protected def flip (F : C ⥤ (D ⥤ E)) : D ⥤ (C ⥤ E) := { obj := λ k, { obj := λ j, (F.obj j).obj k, map := λ j j' f, (F.map f).app k, map_id' := λ X, begin rw category_theory.functor.map_id, refl end, map_comp' := λ X Y Z f g, by rw [map_comp, ←comp_app] }, map := λ c c' f, { app := λ j, (F.obj j).map f } }. @[simp] lemma flip_obj_obj (F : C ⥤ (D ⥤ E)) (c) (d) : (F.flip.obj d).obj c = (F.obj c).obj d := rfl @[simp] lemma flip_obj_map (F : C ⥤ (D ⥤ E)) {c c' : C} (f : c ⟶ c') (d : D) : (F.flip.obj d).map f = (F.map f).app d := rfl @[simp] lemma flip_map_app (F : C ⥤ (D ⥤ E)) {d d' : D} (f : d ⟶ d') (c : C) : (F.flip.map f).app c = (F.obj c).map f := rfl end functor end category_theory
037d9513a7017af701aa8891e08710dce5b83b02
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/reserve.lean
a051a3d4b81cb71928c1eaa313a083957af5cfb8
[ "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
191
lean
reserve infix `=?=`:50 reserve infixr `&&&`:25 notation (name := eq) a `=?=` b := eq a b notation a `&&&` b := and a b set_option pp.notation false #check λ a b : nat, a =?= b &&& b =?= a
2b5a84898aa6f2a635ad7dc215f205b4a8cc3f12
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/data/list/zip.lean
ab1f4aef56e2a17c0ad4aaa65141b5742558fbfc
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
14,205
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kenny Lau -/ import data.list.basic universes u v w z variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type z} open nat namespace list /- zip & unzip -/ @[simp] theorem zip_with_cons_cons (f : α → β → γ) (a : α) (b : β) (l₁ : list α) (l₂ : list β) : zip_with f (a :: l₁) (b :: l₂) = f a b :: zip_with f l₁ l₂ := rfl @[simp] theorem zip_cons_cons (a : α) (b : β) (l₁ : list α) (l₂ : list β) : zip (a :: l₁) (b :: l₂) = (a, b) :: zip l₁ l₂ := rfl @[simp] theorem zip_with_nil_left (f : α → β → γ) (l) : zip_with f [] l = [] := rfl @[simp] theorem zip_with_nil_right (f : α → β → γ) (l) : zip_with f l [] = [] := by cases l; refl @[simp] lemma zip_with_eq_nil_iff {f : α → β → γ} {l l'} : zip_with f l l' = [] ↔ l = [] ∨ l' = [] := by { cases l; cases l'; simp } @[simp] theorem zip_nil_left (l : list α) : zip ([] : list β) l = [] := rfl @[simp] theorem zip_nil_right (l : list α) : zip l ([] : list β) = [] := zip_with_nil_right _ l @[simp] theorem zip_swap : ∀ (l₁ : list α) (l₂ : list β), (zip l₁ l₂).map prod.swap = zip l₂ l₁ | [] l₂ := (zip_nil_right _).symm | l₁ [] := by rw zip_nil_right; refl | (a::l₁) (b::l₂) := by simp only [zip_cons_cons, map_cons, zip_swap l₁ l₂, prod.swap_prod_mk]; split; refl @[simp] theorem length_zip_with (f : α → β → γ) : ∀ (l₁ : list α) (l₂ : list β), length (zip_with f l₁ l₂) = min (length l₁) (length l₂) | [] l₂ := rfl | l₁ [] := by simp only [length, min_zero, zip_with_nil_right] | (a::l₁) (b::l₂) := by by simp [length, zip_cons_cons, length_zip_with l₁ l₂, min_add_add_right] @[simp] theorem length_zip : ∀ (l₁ : list α) (l₂ : list β), length (zip l₁ l₂) = min (length l₁) (length l₂) := length_zip_with _ lemma lt_length_left_of_zip_with {f : α → β → γ} {i : ℕ} {l : list α} {l' : list β} (h : i < (zip_with f l l').length) : i < l.length := by { rw [length_zip_with, lt_min_iff] at h, exact h.left } lemma lt_length_right_of_zip_with {f : α → β → γ} {i : ℕ} {l : list α} {l' : list β} (h : i < (zip_with f l l').length) : i < l'.length := by { rw [length_zip_with, lt_min_iff] at h, exact h.right } lemma lt_length_left_of_zip {i : ℕ} {l : list α} {l' : list β} (h : i < (zip l l').length) : i < l.length := lt_length_left_of_zip_with h lemma lt_length_right_of_zip {i : ℕ} {l : list α} {l' : list β} (h : i < (zip l l').length) : i < l'.length := lt_length_right_of_zip_with h theorem zip_append : ∀ {l₁ r₁ : list α} {l₂ r₂ : list β} (h : length l₁ = length l₂), zip (l₁ ++ r₁) (l₂ ++ r₂) = zip l₁ l₂ ++ zip r₁ r₂ | [] r₁ l₂ r₂ h := by simp only [eq_nil_of_length_eq_zero h.symm]; refl | l₁ r₁ [] r₂ h := by simp only [eq_nil_of_length_eq_zero h]; refl | (a::l₁) r₁ (b::l₂) r₂ h := by simp only [cons_append, zip_cons_cons, zip_append (succ.inj h)]; split; refl theorem zip_map (f : α → γ) (g : β → δ) : ∀ (l₁ : list α) (l₂ : list β), zip (l₁.map f) (l₂.map g) = (zip l₁ l₂).map (prod.map f g) | [] l₂ := rfl | l₁ [] := by simp only [map, zip_nil_right] | (a::l₁) (b::l₂) := by simp only [map, zip_cons_cons, zip_map l₁ l₂, prod.map]; split; refl theorem zip_map_left (f : α → γ) (l₁ : list α) (l₂ : list β) : zip (l₁.map f) l₂ = (zip l₁ l₂).map (prod.map f id) := by rw [← zip_map, map_id] theorem zip_map_right (f : β → γ) (l₁ : list α) (l₂ : list β) : zip l₁ (l₂.map f) = (zip l₁ l₂).map (prod.map id f) := by rw [← zip_map, map_id] @[simp] lemma zip_with_map {μ} (f : γ → δ → μ) (g : α → γ) (h : β → δ) (as : list α) (bs : list β) : zip_with f (as.map g) (bs.map h) = zip_with (λ a b, f (g a) (h b)) as bs := begin induction as generalizing bs, { simp }, { cases bs; simp * } end lemma zip_with_map_left (f : α → β → γ) (g : δ → α) (l : list δ) (l' : list β) : zip_with f (l.map g) l' = zip_with (f ∘ g) l l' := by { convert (zip_with_map f g id l l'), exact eq.symm (list.map_id _) } lemma zip_with_map_right (f : α → β → γ) (l : list α) (g : δ → β) (l' : list δ) : zip_with f l (l'.map g) = zip_with (λ x, f x ∘ g) l l' := by { convert (list.zip_with_map f id g l l'), exact eq.symm (list.map_id _) } theorem zip_map' (f : α → β) (g : α → γ) : ∀ (l : list α), zip (l.map f) (l.map g) = l.map (λ a, (f a, g a)) | [] := rfl | (a::l) := by simp only [map, zip_cons_cons, zip_map' l]; split; refl lemma map_zip_with {δ : Type*} (f : α → β) (g : γ → δ → α) (l : list γ) (l' : list δ) : map f (zip_with g l l') = zip_with (λ x y, f (g x y)) l l' := begin induction l with hd tl hl generalizing l', { simp }, { cases l', { simp }, { simp [hl] } } end theorem mem_zip {a b} : ∀ {l₁ : list α} {l₂ : list β}, (a, b) ∈ zip l₁ l₂ → a ∈ l₁ ∧ b ∈ l₂ | (_::l₁) (_::l₂) (or.inl rfl) := ⟨or.inl rfl, or.inl rfl⟩ | (a'::l₁) (b'::l₂) (or.inr h) := by split; simp only [mem_cons_iff, or_true, mem_zip h] theorem map_fst_zip : ∀ (l₁ : list α) (l₂ : list β), l₁.length ≤ l₂.length → map prod.fst (zip l₁ l₂) = l₁ | [] bs _ := rfl | (a :: as) (b :: bs) h := by { simp at h, simp! * } | (a :: as) [] h := by { simp at h, contradiction } theorem map_snd_zip : ∀ (l₁ : list α) (l₂ : list β), l₂.length ≤ l₁.length → map prod.snd (zip l₁ l₂) = l₂ | _ [] _ := by { rw zip_nil_right, refl } | [] (b :: bs) h := by { simp at h, contradiction } | (a :: as) (b :: bs) h := by { simp at h, simp! * } @[simp] theorem unzip_nil : unzip (@nil (α × β)) = ([], []) := rfl @[simp] theorem unzip_cons (a : α) (b : β) (l : list (α × β)) : unzip ((a, b) :: l) = (a :: (unzip l).1, b :: (unzip l).2) := by rw unzip; cases unzip l; refl theorem unzip_eq_map : ∀ (l : list (α × β)), unzip l = (l.map prod.fst, l.map prod.snd) | [] := rfl | ((a, b) :: l) := by simp only [unzip_cons, map_cons, unzip_eq_map l] theorem unzip_left (l : list (α × β)) : (unzip l).1 = l.map prod.fst := by simp only [unzip_eq_map] theorem unzip_right (l : list (α × β)) : (unzip l).2 = l.map prod.snd := by simp only [unzip_eq_map] theorem unzip_swap (l : list (α × β)) : unzip (l.map prod.swap) = (unzip l).swap := by simp only [unzip_eq_map, map_map]; split; refl theorem zip_unzip : ∀ (l : list (α × β)), zip (unzip l).1 (unzip l).2 = l | [] := rfl | ((a, b) :: l) := by simp only [unzip_cons, zip_cons_cons, zip_unzip l]; split; refl theorem unzip_zip_left : ∀ {l₁ : list α} {l₂ : list β}, length l₁ ≤ length l₂ → (unzip (zip l₁ l₂)).1 = l₁ | [] l₂ h := rfl | l₁ [] h := by rw eq_nil_of_length_eq_zero (eq_zero_of_le_zero h); refl | (a::l₁) (b::l₂) h := by simp only [zip_cons_cons, unzip_cons, unzip_zip_left (le_of_succ_le_succ h)]; split; refl theorem unzip_zip_right {l₁ : list α} {l₂ : list β} (h : length l₂ ≤ length l₁) : (unzip (zip l₁ l₂)).2 = l₂ := by rw [← zip_swap, unzip_swap]; exact unzip_zip_left h theorem unzip_zip {l₁ : list α} {l₂ : list β} (h : length l₁ = length l₂) : unzip (zip l₁ l₂) = (l₁, l₂) := by rw [← @prod.mk.eta _ _ (unzip (zip l₁ l₂)), unzip_zip_left (le_of_eq h), unzip_zip_right (ge_of_eq h)] lemma zip_of_prod {l : list α} {l' : list β} {lp : list (α × β)} (hl : lp.map prod.fst = l) (hr : lp.map prod.snd = l') : lp = l.zip l' := by rw [←hl, ←hr, ←zip_unzip lp, ←unzip_left, ←unzip_right, zip_unzip, zip_unzip] lemma map_prod_left_eq_zip {l : list α} (f : α → β) : l.map (λ x, (x, f x)) = l.zip (l.map f) := by { rw ←zip_map', congr, exact map_id _ } lemma map_prod_right_eq_zip {l : list α} (f : α → β) : l.map (λ x, (f x, x)) = (l.map f).zip l := by { rw ←zip_map', congr, exact map_id _ } lemma zip_with_comm (f : α → α → β) (comm : ∀ (x y : α), f x y = f y x) (l l' : list α) : zip_with f l l' = zip_with f l' l := begin induction l with hd tl hl generalizing l', { simp }, { cases l', { simp }, { simp [comm, hl] } } end instance (f : α → α → β) [is_symm_op α β f] : is_symm_op (list α) (list β) (zip_with f) := ⟨zip_with_comm f is_symm_op.symm_op⟩ @[simp] theorem length_revzip (l : list α) : length (revzip l) = length l := by simp only [revzip, length_zip, length_reverse, min_self] @[simp] theorem unzip_revzip (l : list α) : (revzip l).unzip = (l, l.reverse) := unzip_zip (length_reverse l).symm @[simp] theorem revzip_map_fst (l : list α) : (revzip l).map prod.fst = l := by rw [← unzip_left, unzip_revzip] @[simp] theorem revzip_map_snd (l : list α) : (revzip l).map prod.snd = l.reverse := by rw [← unzip_right, unzip_revzip] theorem reverse_revzip (l : list α) : reverse l.revzip = revzip l.reverse := by rw [← zip_unzip.{u u} (revzip l).reverse, unzip_eq_map]; simp; simp [revzip] theorem revzip_swap (l : list α) : (revzip l).map prod.swap = revzip l.reverse := by simp [revzip] lemma nth_zip_with (f : α → β → γ) (l₁ : list α) (l₂ : list β) (i : ℕ) : (zip_with f l₁ l₂).nth i = ((l₁.nth i).map f).bind (λ g, (l₂.nth i).map g) := begin induction l₁ generalizing l₂ i, { simp [zip_with, (<*>)] }, { cases l₂; simp only [zip_with, has_seq.seq, functor.map, nth, option.map_none'], { cases ((l₁_hd :: l₁_tl).nth i); refl }, { cases i; simp only [option.map_some', nth, option.some_bind', *] } } end lemma nth_zip_with_eq_some {α β γ} (f : α → β → γ) (l₁ : list α) (l₂ : list β) (z : γ) (i : ℕ) : (zip_with f l₁ l₂).nth i = some z ↔ ∃ x y, l₁.nth i = some x ∧ l₂.nth i = some y ∧ f x y = z := begin induction l₁ generalizing l₂ i, { simp [zip_with] }, { cases l₂; simp only [zip_with, nth, exists_false, and_false, false_and], cases i; simp *, }, end lemma nth_zip_eq_some (l₁ : list α) (l₂ : list β) (z : α × β) (i : ℕ) : (zip l₁ l₂).nth i = some z ↔ l₁.nth i = some z.1 ∧ l₂.nth i = some z.2 := begin cases z, rw [zip, nth_zip_with_eq_some], split, { rintro ⟨x, y, h₀, h₁, h₂⟩, cc }, { rintro ⟨h₀, h₁⟩, exact ⟨_,_,h₀,h₁,rfl⟩ } end @[simp] lemma nth_le_zip_with {f : α → β → γ} {l : list α} {l' : list β} {i : ℕ} {h : i < (zip_with f l l').length} : (zip_with f l l').nth_le i h = f (l.nth_le i (lt_length_left_of_zip_with h)) (l'.nth_le i (lt_length_right_of_zip_with h)) := begin rw [←option.some_inj, ←nth_le_nth, nth_zip_with_eq_some], refine ⟨l.nth_le i (lt_length_left_of_zip_with h), l'.nth_le i (lt_length_right_of_zip_with h), nth_le_nth _, _⟩, simp only [←nth_le_nth, eq_self_iff_true, and_self] end @[simp] lemma nth_le_zip {l : list α} {l' : list β} {i : ℕ} {h : i < (zip l l').length} : (zip l l').nth_le i h = (l.nth_le i (lt_length_left_of_zip h), l'.nth_le i (lt_length_right_of_zip h)) := nth_le_zip_with lemma mem_zip_inits_tails {l : list α} {init tail : list α} : (init, tail) ∈ zip l.inits l.tails ↔ init ++ tail = l := begin induction l generalizing init tail; simp_rw [tails, inits, zip_cons_cons], { simp }, { split; rw [mem_cons_iff, zip_map_left, mem_map, prod.exists], { rintros (⟨rfl, rfl⟩ | ⟨_, _, h, rfl, rfl⟩), { simp }, { simp [l_ih.mp h], }, }, { cases init, { simp }, { intro h, right, use [init_tl, tail], simp * at *, }, }, }, end lemma map_uncurry_zip_eq_zip_with (f : α → β → γ) (l : list α) (l' : list β) : map (function.uncurry f) (l.zip l') = zip_with f l l' := begin induction l with hd tl hl generalizing l', { simp }, { cases l' with hd' tl', { simp }, { simp [hl] } } end @[simp] lemma sum_zip_with_distrib_left {γ : Type*} [semiring γ] (f : α → β → γ) (n : γ) (l : list α) (l' : list β) : (l.zip_with (λ x y, n * f x y) l').sum = n * (l.zip_with f l').sum := begin induction l with hd tl hl generalizing f n l', { simp }, { cases l' with hd' tl', { simp, }, { simp [hl, mul_add] } } end section distrib /-! ### Operations that can be applied before or after a `zip_with` -/ variables (f : α → β → γ) (l : list α) (l' : list β) (n : ℕ) lemma zip_with_distrib_take : (zip_with f l l').take n = zip_with f (l.take n) (l'.take n) := begin induction l with hd tl hl generalizing l' n, { simp }, { cases l', { simp }, { cases n, { simp }, { simp [hl] } } } end lemma zip_with_distrib_drop : (zip_with f l l').drop n = zip_with f (l.drop n) (l'.drop n) := begin induction l with hd tl hl generalizing l' n, { simp }, { cases l', { simp }, { cases n, { simp }, { simp [hl] } } } end lemma zip_with_distrib_tail : (zip_with f l l').tail = zip_with f l.tail l'.tail := by simp_rw [←drop_one, zip_with_distrib_drop] lemma zip_with_append (f : α → β → γ) (l la : list α) (l' lb : list β) (h : l.length = l'.length) : zip_with f (l ++ la) (l' ++ lb) = zip_with f l l' ++ zip_with f la lb := begin induction l with hd tl hl generalizing l', { have : l' = [] := eq_nil_of_length_eq_zero (by simpa using h.symm), simp [this], }, { cases l', { simpa using h }, { simp only [add_left_inj, length] at h, simp [hl _ h] } } end lemma zip_with_distrib_reverse (h : l.length = l'.length) : (zip_with f l l').reverse = zip_with f l.reverse l'.reverse := begin induction l with hd tl hl generalizing l', { simp }, { cases l' with hd' tl', { simp }, { simp only [add_left_inj, length] at h, have : tl.reverse.length = tl'.reverse.length := by simp [h], simp [hl _ h, zip_with_append _ _ _ _ _ this] } } end end distrib end list
c82c10329ec6f98c5c92181278e04c7f511c2e9c
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/combinatorics/simple_graph/subgraph.lean
f3aca1f2e626cd674f4e4b41f72f627a4b79b913
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
27,142
lean
/- Copyright (c) 2021 Hunter Monroe. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Hunter Monroe, Kyle Miller, Alena Gusakov -/ import combinatorics.simple_graph.basic /-! # Subgraphs of a simple graph A subgraph of a simple graph consists of subsets of the graph's vertices and edges such that the endpoints of each edge are present in the vertex subset. The edge subset is formalized as a sub-relation of the adjacency relation of the simple graph. ## Main definitions * `subgraph G` is the type of subgraphs of a `G : simple_graph` * `subgraph.neighbor_set`, `subgraph.incidence_set`, and `subgraph.degree` are like their `simple_graph` counterparts, but they refer to vertices from `G` to avoid subtype coercions. * `subgraph.coe` is the coercion from a `G' : subgraph G` to a `simple_graph G'.verts`. (This cannot be a `has_coe` instance since the destination type depends on `G'`.) * `subgraph.is_spanning` for whether a subgraph is a spanning subgraph and `subgraph.is_induced` for whether a subgraph is an induced subgraph. * Instances for `lattice (subgraph G)` and `bounded_order (subgraph G)`. * `simple_graph.to_subgraph`: If a `simple_graph` is a subgraph of another, then you can turn it into a member of the larger graph's `simple_graph.subgraph` type. * Graph homomorphisms from a subgraph to a graph (`subgraph.map_top`) and between subgraphs (`subgraph.map`). ## Implementation notes * Recall that subgraphs are not determined by their vertex sets, so `set_like` does not apply to this kind of subobject. ## Todo * Images of graph homomorphisms as subgraphs. -/ universes u v namespace simple_graph /-- A subgraph of a `simple_graph` is a subset of vertices along with a restriction of the adjacency relation that is symmetric and is supported by the vertex subset. They also form a bounded lattice. Thinking of `V → V → Prop` as `set (V × V)`, a set of darts (i.e., half-edges), then `subgraph.adj_sub` is that the darts of a subgraph are a subset of the darts of `G`. -/ @[ext] structure subgraph {V : Type u} (G : simple_graph V) := (verts : set V) (adj : V → V → Prop) (adj_sub : ∀ {v w : V}, adj v w → G.adj v w) (edge_vert : ∀ {v w : V}, adj v w → v ∈ verts) (symm : symmetric adj . obviously) namespace subgraph variables {V : Type u} {W : Type v} {G : simple_graph V} protected lemma loopless (G' : subgraph G) : irreflexive G'.adj := λ v h, G.loopless v (G'.adj_sub h) lemma adj_comm (G' : subgraph G) (v w : V) : G'.adj v w ↔ G'.adj w v := ⟨λ x, G'.symm x, λ x, G'.symm x⟩ @[symm] lemma adj_symm (G' : subgraph G) {u v : V} (h : G'.adj u v) : G'.adj v u := G'.symm h protected lemma adj.symm {G' : subgraph G} {u v : V} (h : G'.adj u v) : G'.adj v u := G'.symm h /-- Coercion from `G' : subgraph G` to a `simple_graph ↥G'.verts`. -/ @[simps] protected def coe (G' : subgraph G) : simple_graph G'.verts := { adj := λ v w, G'.adj v w, symm := λ v w h, G'.symm h, loopless := λ v h, loopless G v (G'.adj_sub h) } @[simp] lemma coe_adj_sub (G' : subgraph G) (u v : G'.verts) (h : G'.coe.adj u v) : G.adj u v := G'.adj_sub h /-- A subgraph is called a *spanning subgraph* if it contains all the vertices of `G`. --/ def is_spanning (G' : subgraph G) : Prop := ∀ (v : V), v ∈ G'.verts lemma is_spanning_iff {G' : subgraph G} : G'.is_spanning ↔ G'.verts = set.univ := set.eq_univ_iff_forall.symm /-- Coercion from `subgraph G` to `simple_graph V`. If `G'` is a spanning subgraph, then `G'.spanning_coe` yields an isomorphic graph. In general, this adds in all vertices from `V` as isolated vertices. -/ @[simps] protected def spanning_coe (G' : subgraph G) : simple_graph V := { adj := G'.adj, symm := G'.symm, loopless := λ v hv, G.loopless v (G'.adj_sub hv) } @[simp] lemma adj.of_spanning_coe {G' : subgraph G} {u v : G'.verts} (h : G'.spanning_coe.adj u v) : G.adj u v := G'.adj_sub h /-- `spanning_coe` is equivalent to `coe` for a subgraph that `is_spanning`. -/ @[simps] def spanning_coe_equiv_coe_of_spanning (G' : subgraph G) (h : G'.is_spanning) : G'.spanning_coe ≃g G'.coe := { to_fun := λ v, ⟨v, h v⟩, inv_fun := λ v, v, left_inv := λ v, rfl, right_inv := λ ⟨v, hv⟩, rfl, map_rel_iff' := λ v w, iff.rfl } /-- A subgraph is called an *induced subgraph* if vertices of `G'` are adjacent if they are adjacent in `G`. -/ def is_induced (G' : subgraph G) : Prop := ∀ {v w : V}, v ∈ G'.verts → w ∈ G'.verts → G.adj v w → G'.adj v w /-- `H.support` is the set of vertices that form edges in the subgraph `H`. -/ def support (H : subgraph G) : set V := rel.dom H.adj lemma mem_support (H : subgraph G) {v : V} : v ∈ H.support ↔ ∃ w, H.adj v w := iff.rfl lemma support_subset_verts (H : subgraph G) : H.support ⊆ H.verts := λ v ⟨w, h⟩, H.edge_vert h /-- `G'.neighbor_set v` is the set of vertices adjacent to `v` in `G'`. -/ def neighbor_set (G' : subgraph G) (v : V) : set V := set_of (G'.adj v) lemma neighbor_set_subset (G' : subgraph G) (v : V) : G'.neighbor_set v ⊆ G.neighbor_set v := λ w h, G'.adj_sub h lemma neighbor_set_subset_verts (G' : subgraph G) (v : V) : G'.neighbor_set v ⊆ G'.verts := λ _ h, G'.edge_vert (adj_symm G' h) @[simp] lemma mem_neighbor_set (G' : subgraph G) (v w : V) : w ∈ G'.neighbor_set v ↔ G'.adj v w := iff.rfl /-- A subgraph as a graph has equivalent neighbor sets. -/ def coe_neighbor_set_equiv {G' : subgraph G} (v : G'.verts) : G'.coe.neighbor_set v ≃ G'.neighbor_set v := { to_fun := λ w, ⟨w, by { obtain ⟨w', hw'⟩ := w, simpa using hw' }⟩, inv_fun := λ w, ⟨⟨w, G'.edge_vert (G'.adj_symm w.2)⟩, by simpa using w.2⟩, left_inv := λ w, by simp, right_inv := λ w, by simp } /-- The edge set of `G'` consists of a subset of edges of `G`. -/ def edge_set (G' : subgraph G) : set (sym2 V) := sym2.from_rel G'.symm lemma edge_set_subset (G' : subgraph G) : G'.edge_set ⊆ G.edge_set := λ e, quotient.ind (λ e h, G'.adj_sub h) e @[simp] lemma mem_edge_set {G' : subgraph G} {v w : V} : ⟦(v, w)⟧ ∈ G'.edge_set ↔ G'.adj v w := iff.rfl lemma mem_verts_if_mem_edge {G' : subgraph G} {e : sym2 V} {v : V} (he : e ∈ G'.edge_set) (hv : v ∈ e) : v ∈ G'.verts := begin refine quotient.ind (λ e he hv, _) e he hv, cases e with v w, simp only [mem_edge_set] at he, cases sym2.mem_iff.mp hv with h h; subst h, { exact G'.edge_vert he, }, { exact G'.edge_vert (G'.symm he), }, end /-- The `incidence_set` is the set of edges incident to a given vertex. -/ def incidence_set (G' : subgraph G) (v : V) : set (sym2 V) := {e ∈ G'.edge_set | v ∈ e} lemma incidence_set_subset_incidence_set (G' : subgraph G) (v : V) : G'.incidence_set v ⊆ G.incidence_set v := λ e h, ⟨G'.edge_set_subset h.1, h.2⟩ lemma incidence_set_subset (G' : subgraph G) (v : V) : G'.incidence_set v ⊆ G'.edge_set := λ _ h, h.1 /-- Give a vertex as an element of the subgraph's vertex type. -/ @[reducible] def vert (G' : subgraph G) (v : V) (h : v ∈ G'.verts) : G'.verts := ⟨v, h⟩ /-- Create an equal copy of a subgraph (see `copy_eq`) with possibly different definitional equalities. See Note [range copy pattern]. -/ def copy (G' : subgraph G) (V'' : set V) (hV : V'' = G'.verts) (adj' : V → V → Prop) (hadj : adj' = G'.adj) : subgraph G := { verts := V'', adj := adj', adj_sub := hadj.symm ▸ G'.adj_sub, edge_vert := hV.symm ▸ hadj.symm ▸ G'.edge_vert, symm := hadj.symm ▸ G'.symm } lemma copy_eq (G' : subgraph G) (V'' : set V) (hV : V'' = G'.verts) (adj' : V → V → Prop) (hadj : adj' = G'.adj) : G'.copy V'' hV adj' hadj = G' := subgraph.ext _ _ hV hadj /-- The union of two subgraphs. -/ def union (x y : subgraph G) : subgraph G := { verts := x.verts ∪ y.verts, adj := x.adj ⊔ y.adj, adj_sub := λ v w h, or.cases_on h (λ h, x.adj_sub h) (λ h, y.adj_sub h), edge_vert := λ v w h, or.cases_on h (λ h, or.inl (x.edge_vert h)) (λ h, or.inr (y.edge_vert h)), symm := λ v w h, by rwa [pi.sup_apply, pi.sup_apply, x.adj_comm, y.adj_comm] } /-- The intersection of two subgraphs. -/ def inter (x y : subgraph G) : subgraph G := { verts := x.verts ∩ y.verts, adj := x.adj ⊓ y.adj, adj_sub := λ v w h, x.adj_sub h.1, edge_vert := λ v w h, ⟨x.edge_vert h.1, y.edge_vert h.2⟩, symm := λ v w h, by rwa [pi.inf_apply, pi.inf_apply, x.adj_comm, y.adj_comm] } /-- The `top` subgraph is `G` as a subgraph of itself. -/ def top : subgraph G := { verts := set.univ, adj := G.adj, adj_sub := λ v w h, h, edge_vert := λ v w h, set.mem_univ v, symm := G.symm } /-- The `bot` subgraph is the subgraph with no vertices or edges. -/ def bot : subgraph G := { verts := ∅, adj := ⊥, adj_sub := λ v w h, false.rec _ h, edge_vert := λ v w h, false.rec _ h, symm := λ u v h, h } /-- The relation that one subgraph is a subgraph of another. -/ def is_subgraph (x y : subgraph G) : Prop := x.verts ⊆ y.verts ∧ ∀ ⦃v w : V⦄, x.adj v w → y.adj v w instance : lattice (subgraph G) := { le := is_subgraph, sup := union, inf := inter, le_refl := λ x, ⟨rfl.subset, λ _ _ h, h⟩, le_trans := λ x y z hxy hyz, ⟨hxy.1.trans hyz.1, λ _ _ h, hyz.2 (hxy.2 h)⟩, le_antisymm := begin intros x y hxy hyx, ext1 v, exact set.subset.antisymm hxy.1 hyx.1, ext v w, exact iff.intro (λ h, hxy.2 h) (λ h, hyx.2 h), end, sup_le := λ x y z hxy hyz, ⟨set.union_subset hxy.1 hyz.1, (λ v w h, h.cases_on (λ h, hxy.2 h) (λ h, hyz.2 h))⟩, le_sup_left := λ x y, ⟨set.subset_union_left x.verts y.verts, (λ v w h, or.inl h)⟩, le_sup_right := λ x y, ⟨set.subset_union_right x.verts y.verts, (λ v w h, or.inr h)⟩, le_inf := λ x y z hxy hyz, ⟨set.subset_inter hxy.1 hyz.1, (λ v w h, ⟨hxy.2 h, hyz.2 h⟩)⟩, inf_le_left := λ x y, ⟨set.inter_subset_left x.verts y.verts, (λ v w h, h.1)⟩, inf_le_right := λ x y, ⟨set.inter_subset_right x.verts y.verts, (λ v w h, h.2)⟩ } instance : bounded_order (subgraph G) := { top := top, bot := bot, le_top := λ x, ⟨set.subset_univ _, (λ v w h, x.adj_sub h)⟩, bot_le := λ x, ⟨set.empty_subset _, (λ v w h, false.rec _ h)⟩ } @[simps] instance subgraph_inhabited : inhabited (subgraph G) := ⟨⊥⟩ -- TODO simp lemmas for the other lattice operations on subgraphs @[simp] lemma top_verts : (⊤ : subgraph G).verts = set.univ := rfl @[simp] lemma top_adj_iff {v w : V} : (⊤ : subgraph G).adj v w ↔ G.adj v w := iff.rfl @[simp] lemma bot_verts : (⊥ : subgraph G).verts = ∅ := rfl @[simp] lemma not_bot_adj {v w : V} : ¬(⊥ : subgraph G).adj v w := not_false @[simp] lemma inf_adj {H₁ H₂ : subgraph G} {v w : V} : (H₁ ⊓ H₂).adj v w ↔ H₁.adj v w ∧ H₂.adj v w := iff.rfl @[simp] lemma sup_adj {H₁ H₂ : subgraph G} {v w : V} : (H₁ ⊔ H₂).adj v w ↔ H₁.adj v w ∨ H₂.adj v w := iff.rfl @[simp] lemma edge_set_top : (⊤ : subgraph G).edge_set = G.edge_set := rfl @[simp] lemma edge_set_bot : (⊥ : subgraph G).edge_set = ∅ := set.ext $ sym2.ind (by simp) @[simp] lemma edge_set_inf {H₁ H₂ : subgraph G} : (H₁ ⊓ H₂).edge_set = H₁.edge_set ∩ H₂.edge_set := set.ext $ sym2.ind (by simp) @[simp] lemma edge_set_sup {H₁ H₂ : subgraph G} : (H₁ ⊔ H₂).edge_set = H₁.edge_set ∪ H₂.edge_set := set.ext $ sym2.ind (by simp) @[simp] lemma spanning_coe_top : (⊤ : subgraph G).spanning_coe = G := by { ext, refl } @[simp] lemma spanning_coe_bot : (⊥ : subgraph G).spanning_coe = ⊥ := rfl /-- Turn a subgraph of a `simple_graph` into a member of its subgraph type. -/ @[simps] def _root_.simple_graph.to_subgraph (H : simple_graph V) (h : H ≤ G) : G.subgraph := { verts := set.univ, adj := H.adj, adj_sub := h, edge_vert := λ v w h, set.mem_univ v, symm := H.symm } lemma support_mono {H H' : subgraph G} (h : H ≤ H') : H.support ⊆ H'.support := rel.dom_mono h.2 lemma _root_.simple_graph.to_subgraph.is_spanning (H : simple_graph V) (h : H ≤ G) : (H.to_subgraph h).is_spanning := set.mem_univ lemma spanning_coe_le_of_le {H H' : subgraph G} (h : H ≤ H') : H.spanning_coe ≤ H'.spanning_coe := h.2 /-- The top of the `subgraph G` lattice is equivalent to the graph itself. -/ def top_equiv : (⊤ : subgraph G).coe ≃g G := { to_fun := λ v, ↑v, inv_fun := λ v, ⟨v, trivial⟩, left_inv := λ ⟨v, _⟩, rfl, right_inv := λ v, rfl, map_rel_iff' := λ a b, iff.rfl } /-- The bottom of the `subgraph G` lattice is equivalent to the empty graph on the empty vertex type. -/ def bot_equiv : (⊥ : subgraph G).coe ≃g (⊥ : simple_graph empty) := { to_fun := λ v, v.property.elim, inv_fun := λ v, v.elim, left_inv := λ ⟨_, h⟩, h.elim, right_inv := λ v, v.elim, map_rel_iff' := λ a b, iff.rfl } lemma edge_set_mono {H₁ H₂ : subgraph G} (h : H₁ ≤ H₂) : H₁.edge_set ≤ H₂.edge_set := λ e, sym2.ind h.2 e lemma _root_.disjoint.edge_set {H₁ H₂ : subgraph G} (h : disjoint H₁ H₂) : disjoint H₁.edge_set H₂.edge_set := by simpa using edge_set_mono h /-- Graph homomorphisms induce a covariant function on subgraphs. -/ @[simps] protected def map {G' : simple_graph W} (f : G →g G') (H : G.subgraph) : G'.subgraph := { verts := f '' H.verts, adj := relation.map H.adj f f, adj_sub := by { rintro _ _ ⟨u, v, h, rfl, rfl⟩, exact f.map_rel (H.adj_sub h) }, edge_vert := by { rintro _ _ ⟨u, v, h, rfl, rfl⟩, exact set.mem_image_of_mem _ (H.edge_vert h) }, symm := by { rintro _ _ ⟨u, v, h, rfl, rfl⟩, exact ⟨v, u, H.symm h, rfl, rfl⟩ } } lemma map_monotone {G' : simple_graph W} (f : G →g G') : monotone (subgraph.map f) := begin intros H H' h, split, { intro, simp only [map_verts, set.mem_image, forall_exists_index, and_imp], rintro v hv rfl, exact ⟨_, h.1 hv, rfl⟩ }, { rintros _ _ ⟨u, v, ha, rfl, rfl⟩, exact ⟨_, _, h.2 ha, rfl, rfl⟩ } end /-- Graph homomorphisms induce a contravariant function on subgraphs. -/ @[simps] protected def comap {G' : simple_graph W} (f : G →g G') (H : G'.subgraph) : G.subgraph := { verts := f ⁻¹' H.verts, adj := λ u v, G.adj u v ∧ H.adj (f u) (f v), adj_sub := by { rintros v w ⟨ga, ha⟩, exact ga }, edge_vert := by { rintros v w ⟨ga, ha⟩, simp [H.edge_vert ha] } } lemma comap_monotone {G' : simple_graph W} (f : G →g G') : monotone (subgraph.comap f) := begin intros H H' h, split, { intro, simp only [comap_verts, set.mem_preimage], apply h.1, }, { intros v w, simp only [comap_adj, and_imp, true_and] { contextual := tt }, intro, apply h.2, } end lemma map_le_iff_le_comap {G' : simple_graph W} (f : G →g G') (H : G.subgraph) (H' : G'.subgraph) : H.map f ≤ H' ↔ H ≤ H'.comap f := begin refine ⟨λ h, ⟨λ v hv, _, λ v w hvw, _⟩, λ h, ⟨λ v, _, λ v w, _⟩⟩, { simp only [comap_verts, set.mem_preimage], exact h.1 ⟨v, hv, rfl⟩, }, { simp only [H.adj_sub hvw, comap_adj, true_and], exact h.2 ⟨v, w, hvw, rfl, rfl⟩, }, { simp only [map_verts, set.mem_image, forall_exists_index, and_imp], rintro w hw rfl, exact h.1 hw, }, { simp only [relation.map, map_adj, forall_exists_index, and_imp], rintros u u' hu rfl rfl, have := h.2 hu, simp only [comap_adj] at this, exact this.2, } end /-- Given two subgraphs, one a subgraph of the other, there is an induced injective homomorphism of the subgraphs as graphs. -/ @[simps] def inclusion {x y : subgraph G} (h : x ≤ y) : x.coe →g y.coe := { to_fun := λ v, ⟨↑v, and.left h v.property⟩, map_rel' := λ v w hvw, h.2 hvw } lemma inclusion.injective {x y : subgraph G} (h : x ≤ y) : function.injective (inclusion h) := λ v w h, by { simp only [inclusion, rel_hom.coe_fn_mk, subtype.mk_eq_mk] at h, exact subtype.ext h } /-- There is an induced injective homomorphism of a subgraph of `G` into `G`. -/ @[simps] protected def hom (x : subgraph G) : x.coe →g G := { to_fun := λ v, v, map_rel' := λ v w hvw, x.adj_sub hvw } lemma hom.injective {x : subgraph G} : function.injective x.hom := λ v w h, subtype.ext h /-- There is an induced injective homomorphism of a subgraph of `G` as a spanning subgraph into `G`. -/ @[simps] def spanning_hom (x : subgraph G) : x.spanning_coe →g G := { to_fun := id, map_rel' := λ v w hvw, x.adj_sub hvw } lemma spanning_hom.injective {x : subgraph G} : function.injective x.spanning_hom := λ v w h, h lemma neighbor_set_subset_of_subgraph {x y : subgraph G} (h : x ≤ y) (v : V) : x.neighbor_set v ⊆ y.neighbor_set v := λ w h', h.2 h' instance neighbor_set.decidable_pred (G' : subgraph G) [h : decidable_rel G'.adj] (v : V) : decidable_pred (∈ G'.neighbor_set v) := h v /-- If a graph is locally finite at a vertex, then so is a subgraph of that graph. -/ instance finite_at {G' : subgraph G} (v : G'.verts) [decidable_rel G'.adj] [fintype (G.neighbor_set v)] : fintype (G'.neighbor_set v) := set.fintype_subset (G.neighbor_set v) (G'.neighbor_set_subset v) /-- If a subgraph is locally finite at a vertex, then so are subgraphs of that subgraph. This is not an instance because `G''` cannot be inferred. -/ def finite_at_of_subgraph {G' G'' : subgraph G} [decidable_rel G'.adj] (h : G' ≤ G'') (v : G'.verts) [hf : fintype (G''.neighbor_set v)] : fintype (G'.neighbor_set v) := set.fintype_subset (G''.neighbor_set v) (neighbor_set_subset_of_subgraph h v) instance (G' : subgraph G) [fintype G'.verts] (v : V) [decidable_pred (∈ G'.neighbor_set v)] : fintype (G'.neighbor_set v) := set.fintype_subset G'.verts (neighbor_set_subset_verts G' v) instance coe_finite_at {G' : subgraph G} (v : G'.verts) [fintype (G'.neighbor_set v)] : fintype (G'.coe.neighbor_set v) := fintype.of_equiv _ (coe_neighbor_set_equiv v).symm lemma is_spanning.card_verts [fintype V] {G' : subgraph G} [fintype G'.verts] (h : G'.is_spanning) : G'.verts.to_finset.card = fintype.card V := by { rw is_spanning_iff at h, simpa [h] } /-- The degree of a vertex in a subgraph. It's zero for vertices outside the subgraph. -/ def degree (G' : subgraph G) (v : V) [fintype (G'.neighbor_set v)] : ℕ := fintype.card (G'.neighbor_set v) lemma finset_card_neighbor_set_eq_degree {G' : subgraph G} {v : V} [fintype (G'.neighbor_set v)] : (G'.neighbor_set v).to_finset.card = G'.degree v := by rw [degree, set.to_finset_card] lemma degree_le (G' : subgraph G) (v : V) [fintype (G'.neighbor_set v)] [fintype (G.neighbor_set v)] : G'.degree v ≤ G.degree v := begin rw ←card_neighbor_set_eq_degree, exact set.card_le_of_subset (G'.neighbor_set_subset v), end lemma degree_le' (G' G'' : subgraph G) (h : G' ≤ G'') (v : V) [fintype (G'.neighbor_set v)] [fintype (G''.neighbor_set v)] : G'.degree v ≤ G''.degree v := set.card_le_of_subset (neighbor_set_subset_of_subgraph h v) @[simp] lemma coe_degree (G' : subgraph G) (v : G'.verts) [fintype (G'.coe.neighbor_set v)] [fintype (G'.neighbor_set v)] : G'.coe.degree v = G'.degree v := begin rw ←card_neighbor_set_eq_degree, exact fintype.card_congr (coe_neighbor_set_equiv v), end @[simp] lemma degree_spanning_coe {G' : G.subgraph} (v : V) [fintype (G'.neighbor_set v)] [fintype (G'.spanning_coe.neighbor_set v)] : G'.spanning_coe.degree v = G'.degree v := by { rw [← card_neighbor_set_eq_degree, subgraph.degree], congr } lemma degree_eq_one_iff_unique_adj {G' : subgraph G} {v : V} [fintype (G'.neighbor_set v)] : G'.degree v = 1 ↔ ∃! (w : V), G'.adj v w := begin rw [← finset_card_neighbor_set_eq_degree, finset.card_eq_one, finset.singleton_iff_unique_mem], simp only [set.mem_to_finset, mem_neighbor_set], end /-! ## Subgraphs of subgraphs -/ /-- Given a subgraph of a subgraph of `G`, construct a subgraph of `G`. -/ @[reducible] protected def coe_subgraph {G' : G.subgraph} : G'.coe.subgraph → G.subgraph := subgraph.map G'.hom /-- Given a subgraph of `G`, restrict it to being a subgraph of another subgraph `G'` by taking the portion of `G` that intersects `G'`. -/ @[reducible] protected def restrict {G' : G.subgraph} : G.subgraph → G'.coe.subgraph := subgraph.comap G'.hom lemma restrict_coe_subgraph {G' : G.subgraph} (G'' : G'.coe.subgraph) : G''.coe_subgraph.restrict = G'' := begin ext, { simp }, { simp only [relation.map, comap_adj, coe_adj, subtype.coe_prop, hom_apply, map_adj, set_coe.exists, subtype.coe_mk, exists_and_distrib_right, exists_eq_right_right, subtype.coe_eta, exists_true_left, exists_eq_right, and_iff_right_iff_imp], apply G''.adj_sub, } end lemma coe_subgraph_injective (G' : G.subgraph) : function.injective (subgraph.coe_subgraph : G'.coe.subgraph → G.subgraph) := function.left_inverse.injective restrict_coe_subgraph /-! ## Edge deletion -/ /-- Given a subgraph `G'` and a set of vertex pairs, remove all of the corresponding edges from its edge set, if present. See also: `simple_graph.delete_edges`. -/ def delete_edges (G' : G.subgraph) (s : set (sym2 V)) : G.subgraph := { verts := G'.verts, adj := G'.adj \ sym2.to_rel s, adj_sub := λ a b h', G'.adj_sub h'.1, edge_vert := λ a b h', G'.edge_vert h'.1, symm := λ a b, by simp [G'.adj_comm, sym2.eq_swap] } section delete_edges variables {G' : G.subgraph} (s : set (sym2 V)) @[simp] lemma delete_edges_verts : (G'.delete_edges s).verts = G'.verts := rfl @[simp] lemma delete_edges_adj (v w : V) : (G'.delete_edges s).adj v w ↔ G'.adj v w ∧ ¬ ⟦(v, w)⟧ ∈ s := iff.rfl @[simp] lemma delete_edges_delete_edges (s s' : set (sym2 V)) : (G'.delete_edges s).delete_edges s' = G'.delete_edges (s ∪ s') := by ext; simp [and_assoc, not_or_distrib] @[simp] lemma delete_edges_empty_eq : G'.delete_edges ∅ = G' := by ext; simp @[simp] lemma delete_edges_spanning_coe_eq : G'.spanning_coe.delete_edges s = (G'.delete_edges s).spanning_coe := by { ext, simp } lemma delete_edges_coe_eq (s : set (sym2 G'.verts)) : G'.coe.delete_edges s = (G'.delete_edges (sym2.map coe '' s)).coe := begin ext ⟨v, hv⟩ ⟨w, hw⟩, simp only [simple_graph.delete_edges_adj, coe_adj, subtype.coe_mk, delete_edges_adj, set.mem_image, not_exists, not_and, and.congr_right_iff], intro h, split, { intros hs, refine sym2.ind _, rintro ⟨v', hv'⟩ ⟨w', hw'⟩, simp only [sym2.map_pair_eq, subtype.coe_mk, quotient.eq], contrapose!, rintro (_ | _); simpa [sym2.eq_swap], }, { intros h' hs, exact h' _ hs rfl, }, end lemma coe_delete_edges_eq (s : set (sym2 V)) : (G'.delete_edges s).coe = G'.coe.delete_edges (sym2.map coe ⁻¹' s) := by { ext ⟨v, hv⟩ ⟨w, hw⟩, simp } lemma delete_edges_le : G'.delete_edges s ≤ G' := by split; simp { contextual := tt } lemma delete_edges_le_of_le {s s' : set (sym2 V)} (h : s ⊆ s') : G'.delete_edges s' ≤ G'.delete_edges s := begin split; simp only [delete_edges_verts, delete_edges_adj, true_and, and_imp] {contextual := tt}, exact λ v w hvw hs' hs, hs' (h hs), end @[simp] lemma delete_edges_inter_edge_set_left_eq : G'.delete_edges (G'.edge_set ∩ s) = G'.delete_edges s := by ext; simp [imp_false] { contextual := tt } @[simp] lemma delete_edges_inter_edge_set_right_eq : G'.delete_edges (s ∩ G'.edge_set) = G'.delete_edges s := by ext; simp [imp_false] { contextual := tt } lemma coe_delete_edges_le : (G'.delete_edges s).coe ≤ (G'.coe : simple_graph G'.verts) := λ v w, by simp { contextual := tt } lemma spanning_coe_delete_edges_le (G' : G.subgraph) (s : set (sym2 V)) : (G'.delete_edges s).spanning_coe ≤ G'.spanning_coe := spanning_coe_le_of_le (delete_edges_le s) end delete_edges /-! ## Induced subgraphs -/ /- Given a subgraph, we can change its vertex set while removing any invalid edges, which gives induced subgraphs. See also `simple_graph.induce` for the `simple_graph` version, which, unlike for subgraphs, results in a graph with a different vertex type. -/ /-- The induced subgraph of a subgraph. The expectation is that `s ⊆ G'.verts` for the usual notion of an induced subgraph, but, in general, `s` is taken to be the new vertex set and edges are induced from the subgraph `G'`. -/ @[simps] def induce (G' : G.subgraph) (s : set V) : G.subgraph := { verts := s, adj := λ u v, u ∈ s ∧ v ∈ s ∧ G'.adj u v, adj_sub := λ u v, by { rintro ⟨-, -, ha⟩, exact G'.adj_sub ha }, edge_vert := λ u v, by { rintro ⟨h, -, -⟩, exact h } } lemma _root_.simple_graph.induce_eq_coe_induce_top (s : set V) : G.induce s = ((⊤ : G.subgraph).induce s).coe := by { ext v w, simp } section induce variables {G' G'' : G.subgraph} {s s' : set V} lemma induce_mono (hg : G' ≤ G'') (hs : s ⊆ s') : G'.induce s ≤ G''.induce s' := begin split, { simp [hs], }, { simp only [induce_adj, true_and, and_imp] { contextual := tt }, intros v w hv hw ha, exact ⟨hs hv, hs hw, hg.2 ha⟩, }, end @[mono] lemma induce_mono_left (hg : G' ≤ G'') : G'.induce s ≤ G''.induce s := induce_mono hg (by refl) @[mono] lemma induce_mono_right (hs : s ⊆ s') : G'.induce s ≤ G'.induce s' := induce_mono (by refl) hs @[simp] lemma induce_empty : G'.induce ∅ = ⊥ := by ext; simp @[simp] lemma induce_self_verts : G'.induce G'.verts = G' := begin ext, { simp }, { split; simp only [induce_adj, implies_true_iff, and_true] {contextual := tt}, exact λ ha, ⟨G'.edge_vert ha, G'.edge_vert ha.symm⟩ } end end induce /-- Given a subgraph and a set of vertices, delete all the vertices from the subgraph, if present. Any edges indicent to the deleted vertices are deleted as well. -/ @[reducible] def delete_verts (G' : G.subgraph) (s : set V) : G.subgraph := G'.induce (G'.verts \ s) section delete_verts variables {G' : G.subgraph} {s : set V} lemma delete_verts_verts : (G'.delete_verts s).verts = G'.verts \ s := rfl lemma delete_verts_adj {u v : V} : (G'.delete_verts s).adj u v ↔ u ∈ G'.verts ∧ ¬ u ∈ s ∧ v ∈ G'.verts ∧ ¬ v ∈ s ∧ G'.adj u v := by simp [and_assoc] @[simp] lemma delete_verts_delete_verts (s s' : set V) : (G'.delete_verts s).delete_verts s' = G'.delete_verts (s ∪ s') := by ext; simp [not_or_distrib, and_assoc] { contextual := tt } @[simp] lemma delete_verts_empty : G'.delete_verts ∅ = G' := by simp [delete_verts] lemma delete_verts_le : G'.delete_verts s ≤ G' := by split; simp [set.diff_subset] @[mono] lemma delete_verts_mono {G' G'' : G.subgraph} (h : G' ≤ G'') : G'.delete_verts s ≤ G''.delete_verts s := induce_mono h (set.diff_subset_diff_left h.1) @[mono] lemma delete_verts_anti {s s' : set V} (h : s ⊆ s') : G'.delete_verts s' ≤ G'.delete_verts s := induce_mono (le_refl _) (set.diff_subset_diff_right h) @[simp] lemma delete_verts_inter_verts_left_eq : G'.delete_verts (G'.verts ∩ s) = G'.delete_verts s := by ext; simp [imp_false] { contextual := tt } @[simp] lemma delete_verts_inter_verts_set_right_eq : G'.delete_verts (s ∩ G'.verts) = G'.delete_verts s := by ext; simp [imp_false] { contextual := tt } end delete_verts end subgraph end simple_graph
f454fd6d5627a9a78411502da66285c8c353ae95
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/topology/algebra/ring.lean
46d628aabc25e2daeb125021e57600e74167b15d
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
19,302
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl -/ import algebra.ring.prod import ring_theory.ideal.quotient import ring_theory.subring.basic import topology.algebra.group /-! # Topological (semi)rings A topological (semi)ring is a (semi)ring equipped with a topology such that all operations are continuous. Besides this definition, this file proves that the topological closure of a subring (resp. an ideal) is a subring (resp. an ideal) and defines products and quotients of topological (semi)rings. ## Main Results - `subring.topological_closure`/`subsemiring.topological_closure`: the topological closure of a `subring`/`subsemiring` is itself a `sub(semi)ring`. - `prod.topological_semiring`/`prod.topological_ring`: The product of two topological (semi)rings. - `pi.topological_semiring`/`pi.topological_ring`: The arbitrary product of topological (semi)rings. - `ideal.closure`: The closure of an ideal is an ideal. - `topological_ring_quotient`: The quotient of a topological semiring by an ideal is a topological ring. -/ open classical set filter topological_space function open_locale classical topological_space filter section topological_semiring variables (α : Type*) /-- a topological semiring is a semiring `R` where addition and multiplication are continuous. We allow for non-unital and non-associative semirings as well. The `topological_semiring` class should *only* be instantiated in the presence of a `non_unital_non_assoc_semiring` instance; if there is an instance of `non_unital_non_assoc_ring`, then `topological_ring` should be used. Note: in the presence of `non_assoc_ring`, these classes are mathematically equivalent (see `topological_semiring.has_continuous_neg_of_mul` or `topological_semiring.to_topological_ring`). -/ class topological_semiring [topological_space α] [non_unital_non_assoc_semiring α] extends has_continuous_add α, has_continuous_mul α : Prop /-- A topological ring is a ring `R` where addition, multiplication and negation are continuous. If `R` is a (unital) ring, then continuity of negation can be derived from continuity of multiplication as it is multiplication with `-1`. (See `topological_semiring.has_continuous_neg_of_mul` and `topological_semiring.to_topological_add_group`) -/ class topological_ring [topological_space α] [non_unital_non_assoc_ring α] extends topological_semiring α, has_continuous_neg α : Prop variables {α} /-- If `R` is a ring with a continuous multiplication, then negation is continuous as well since it is just multiplication with `-1`. -/ lemma topological_semiring.has_continuous_neg_of_mul [topological_space α] [non_assoc_ring α] [has_continuous_mul α] : has_continuous_neg α := { continuous_neg := by simpa using (continuous_const.mul continuous_id : continuous (λ x : α, (-1) * x)) } /-- If `R` is a ring which is a topological semiring, then it is automatically a topological ring. This exists so that one can place a topological ring structure on `R` without explicitly proving `continuous_neg`. -/ lemma topological_semiring.to_topological_ring [topological_space α] [non_assoc_ring α] (h : topological_semiring α) : topological_ring α := { ..h, ..(by { haveI := h.to_has_continuous_mul, exact topological_semiring.has_continuous_neg_of_mul } : has_continuous_neg α) } @[priority 100] -- See note [lower instance priority] instance topological_ring.to_topological_add_group [non_unital_non_assoc_ring α] [topological_space α] [topological_ring α] : topological_add_group α := { ..topological_ring.to_topological_semiring.to_has_continuous_add, ..topological_ring.to_has_continuous_neg } @[priority 50] instance discrete_topology.topological_semiring [topological_space α] [non_unital_non_assoc_semiring α] [discrete_topology α] : topological_semiring α := ⟨⟩ @[priority 50] instance discrete_topology.topological_ring [topological_space α] [non_unital_non_assoc_ring α] [discrete_topology α] : topological_ring α := ⟨⟩ section variables [topological_space α] [semiring α] [topological_semiring α] namespace subsemiring instance (S : subsemiring α) : topological_semiring S := { ..S.to_submonoid.has_continuous_mul, ..S.to_add_submonoid.has_continuous_add } end subsemiring /-- The (topological-space) closure of a subsemiring of a topological semiring is itself a subsemiring. -/ def subsemiring.topological_closure (s : subsemiring α) : subsemiring α := { carrier := closure (s : set α), ..(s.to_submonoid.topological_closure), ..(s.to_add_submonoid.topological_closure ) } @[simp] lemma subsemiring.topological_closure_coe (s : subsemiring α) : (s.topological_closure : set α) = closure (s : set α) := rfl lemma subsemiring.subring_topological_closure (s : subsemiring α) : s ≤ s.topological_closure := subset_closure lemma subsemiring.is_closed_topological_closure (s : subsemiring α) : is_closed (s.topological_closure : set α) := by convert is_closed_closure lemma subsemiring.topological_closure_minimal (s : subsemiring α) {t : subsemiring α} (h : s ≤ t) (ht : is_closed (t : set α)) : s.topological_closure ≤ t := closure_minimal h ht /-- If a subsemiring of a topological semiring is commutative, then so is its topological closure. -/ def subsemiring.comm_semiring_topological_closure [t2_space α] (s : subsemiring α) (hs : ∀ (x y : s), x * y = y * x) : comm_semiring s.topological_closure := { ..s.topological_closure.to_semiring, ..s.to_submonoid.comm_monoid_topological_closure hs } end section variables {β : Type*} [topological_space α] [topological_space β] /-- The product topology on the cartesian product of two topological semirings makes the product into a topological semiring. -/ instance [non_unital_non_assoc_semiring α] [non_unital_non_assoc_semiring β] [topological_semiring α] [topological_semiring β] : topological_semiring (α × β) := {} /-- The product topology on the cartesian product of two topological rings makes the product into a topological ring. -/ instance [non_unital_non_assoc_ring α] [non_unital_non_assoc_ring β] [topological_ring α] [topological_ring β] : topological_ring (α × β) := {} end instance {β : Type*} {C : β → Type*} [∀ b, topological_space (C b)] [Π b, non_unital_non_assoc_semiring (C b)] [Π b, topological_semiring (C b)] : topological_semiring (Π b, C b) := {} instance {β : Type*} {C : β → Type*} [∀ b, topological_space (C b)] [Π b, non_unital_non_assoc_ring (C b)] [Π b, topological_ring (C b)] : topological_ring (Π b, C b) := {} section mul_opposite open mul_opposite instance [non_unital_non_assoc_semiring α] [topological_space α] [has_continuous_add α] : has_continuous_add αᵐᵒᵖ := { continuous_add := continuous_induced_rng.2 $ (@continuous_add α _ _ _).comp (continuous_unop.prod_map continuous_unop) } instance [non_unital_non_assoc_semiring α] [topological_space α] [topological_semiring α] : topological_semiring αᵐᵒᵖ := {} instance [non_unital_non_assoc_ring α] [topological_space α] [has_continuous_neg α] : has_continuous_neg αᵐᵒᵖ := { continuous_neg := continuous_induced_rng.2 $ (@continuous_neg α _ _ _).comp continuous_unop } instance [non_unital_non_assoc_ring α] [topological_space α] [topological_ring α] : topological_ring αᵐᵒᵖ := {} end mul_opposite section add_opposite open add_opposite instance [non_unital_non_assoc_semiring α] [topological_space α] [has_continuous_mul α] : has_continuous_mul αᵃᵒᵖ := { continuous_mul := by convert (continuous_op.comp $ (@continuous_mul α _ _ _).comp $ continuous_unop.prod_map continuous_unop) } instance [non_unital_non_assoc_semiring α] [topological_space α] [topological_semiring α] : topological_semiring αᵃᵒᵖ := {} instance [non_unital_non_assoc_ring α] [topological_space α] [topological_ring α] : topological_ring αᵃᵒᵖ := {} end add_opposite section variables {R : Type*} [non_unital_non_assoc_ring R] [topological_space R] lemma topological_ring.of_add_group_of_nhds_zero [topological_add_group R] (hmul : tendsto (uncurry ((*) : R → R → R)) ((𝓝 0) ×ᶠ (𝓝 0)) $ 𝓝 0) (hmul_left : ∀ (x₀ : R), tendsto (λ x : R, x₀ * x) (𝓝 0) $ 𝓝 0) (hmul_right : ∀ (x₀ : R), tendsto (λ x : R, x * x₀) (𝓝 0) $ 𝓝 0) : topological_ring R := begin refine {..‹topological_add_group R›, ..}, have hleft : ∀ x₀ : R, 𝓝 x₀ = map (λ x, x₀ + x) (𝓝 0), by simp, have hadd : tendsto (uncurry ((+) : R → R → R)) ((𝓝 0) ×ᶠ (𝓝 0)) (𝓝 0), { rw ← nhds_prod_eq, convert continuous_add.tendsto ((0 : R), (0 : R)), rw zero_add }, rw continuous_iff_continuous_at, rintro ⟨x₀, y₀⟩, rw [continuous_at, nhds_prod_eq, hleft x₀, hleft y₀, hleft (x₀*y₀), filter.prod_map_map_eq, tendsto_map'_iff], suffices : tendsto ((λ (x : R), x + x₀ * y₀) ∘ (λ (p : R × R), p.1 + p.2) ∘ (λ (p : R × R), (p.1*y₀ + x₀*p.2, p.1*p.2))) ((𝓝 0) ×ᶠ (𝓝 0)) (map (λ (x : R), x + x₀ * y₀) $ 𝓝 0), { convert this using 1, { ext, simp only [comp_app, mul_add, add_mul], abel }, { simp only [add_comm] } }, refine tendsto_map.comp (hadd.comp (tendsto.prod_mk _ hmul)), exact hadd.comp (((hmul_right y₀).comp tendsto_fst).prod_mk ((hmul_left x₀).comp tendsto_snd)) end lemma topological_ring.of_nhds_zero (hadd : tendsto (uncurry ((+) : R → R → R)) ((𝓝 0) ×ᶠ (𝓝 0)) $ 𝓝 0) (hneg : tendsto (λ x, -x : R → R) (𝓝 0) (𝓝 0)) (hmul : tendsto (uncurry ((*) : R → R → R)) ((𝓝 0) ×ᶠ (𝓝 0)) $ 𝓝 0) (hmul_left : ∀ (x₀ : R), tendsto (λ x : R, x₀ * x) (𝓝 0) $ 𝓝 0) (hmul_right : ∀ (x₀ : R), tendsto (λ x : R, x * x₀) (𝓝 0) $ 𝓝 0) (hleft : ∀ x₀ : R, 𝓝 x₀ = map (λ x, x₀ + x) (𝓝 0)) : topological_ring R := begin haveI := topological_add_group.of_comm_of_nhds_zero hadd hneg hleft, exact topological_ring.of_add_group_of_nhds_zero hmul hmul_left hmul_right end end variables {α} [topological_space α] section variables [non_unital_non_assoc_ring α] [topological_ring α] /-- In a topological semiring, the left-multiplication `add_monoid_hom` is continuous. -/ lemma mul_left_continuous (x : α) : continuous (add_monoid_hom.mul_left x) := continuous_const.mul continuous_id /-- In a topological semiring, the right-multiplication `add_monoid_hom` is continuous. -/ lemma mul_right_continuous (x : α) : continuous (add_monoid_hom.mul_right x) := continuous_id.mul continuous_const end variables [ring α] [topological_ring α] namespace subring instance (S : subring α) : topological_ring S := topological_semiring.to_topological_ring S.to_subsemiring.topological_semiring end subring /-- The (topological-space) closure of a subring of a topological ring is itself a subring. -/ def subring.topological_closure (S : subring α) : subring α := { carrier := closure (S : set α), ..S.to_submonoid.topological_closure, ..S.to_add_subgroup.topological_closure } lemma subring.subring_topological_closure (s : subring α) : s ≤ s.topological_closure := subset_closure lemma subring.is_closed_topological_closure (s : subring α) : is_closed (s.topological_closure : set α) := by convert is_closed_closure lemma subring.topological_closure_minimal (s : subring α) {t : subring α} (h : s ≤ t) (ht : is_closed (t : set α)) : s.topological_closure ≤ t := closure_minimal h ht /-- If a subring of a topological ring is commutative, then so is its topological closure. -/ def subring.comm_ring_topological_closure [t2_space α] (s : subring α) (hs : ∀ (x y : s), x * y = y * x) : comm_ring s.topological_closure := { ..s.topological_closure.to_ring, ..s.to_submonoid.comm_monoid_topological_closure hs } end topological_semiring section topological_comm_ring variables {α : Type*} [topological_space α] [comm_ring α] [topological_ring α] /-- The closure of an ideal in a topological ring as an ideal. -/ def ideal.closure (S : ideal α) : ideal α := { carrier := closure S, smul_mem' := λ c x hx, map_mem_closure (mul_left_continuous _) hx $ λ a, S.mul_mem_left c, ..(add_submonoid.topological_closure S.to_add_submonoid) } @[simp] lemma ideal.coe_closure (S : ideal α) : (S.closure : set α) = closure S := rfl end topological_comm_ring section topological_ring variables {α : Type*} [topological_space α] [comm_ring α] (N : ideal α) open ideal.quotient instance topological_ring_quotient_topology : topological_space (α ⧸ N) := show topological_space (quotient _), by apply_instance -- note for the reader: in the following, `mk` is `ideal.quotient.mk`, the canonical map `R → R/I`. variable [topological_ring α] lemma quotient_ring.is_open_map_coe : is_open_map (mk N) := begin intros s s_op, change is_open (mk N ⁻¹' (mk N '' s)), rw quotient_ring_saturate, exact is_open_Union (λ ⟨n, _⟩, is_open_map_add_left n s s_op) end lemma quotient_ring.quotient_map_coe_coe : quotient_map (λ p : α × α, (mk N p.1, mk N p.2)) := is_open_map.to_quotient_map ((quotient_ring.is_open_map_coe N).prod (quotient_ring.is_open_map_coe N)) ((continuous_quot_mk.comp continuous_fst).prod_mk (continuous_quot_mk.comp continuous_snd)) (by rintro ⟨⟨x⟩, ⟨y⟩⟩; exact ⟨(x, y), rfl⟩) instance topological_ring_quotient : topological_ring (α ⧸ N) := topological_semiring.to_topological_ring { continuous_add := have cont : continuous (mk N ∘ (λ (p : α × α), p.fst + p.snd)) := continuous_quot_mk.comp continuous_add, (quotient_map.continuous_iff (quotient_ring.quotient_map_coe_coe N)).mpr cont, continuous_mul := have cont : continuous (mk N ∘ (λ (p : α × α), p.fst * p.snd)) := continuous_quot_mk.comp continuous_mul, (quotient_map.continuous_iff (quotient_ring.quotient_map_coe_coe N)).mpr cont } end topological_ring /-! ### Lattice of ring topologies We define a type class `ring_topology α` which endows a ring `α` with a topology such that all ring operations are continuous. Ring topologies on a fixed ring `α` are ordered, by reverse inclusion. They form a complete lattice, with `⊥` the discrete topology and `⊤` the indiscrete topology. Any function `f : α → β` induces `coinduced f : topological_space α → ring_topology β`. -/ universes u v /-- A ring topology on a ring `α` is a topology for which addition, negation and multiplication are continuous. -/ @[ext] structure ring_topology (α : Type u) [ring α] extends topological_space α, topological_ring α : Type u namespace ring_topology variables {α : Type*} [ring α] instance inhabited {α : Type u} [ring α] : inhabited (ring_topology α) := ⟨{to_topological_space := ⊤, continuous_add := continuous_top, continuous_mul := continuous_top, continuous_neg := continuous_top}⟩ @[ext] lemma ext' {f g : ring_topology α} (h : f.is_open = g.is_open) : f = g := by { ext, rw h } /-- The ordering on ring topologies on the ring `α`. `t ≤ s` if every set open in `s` is also open in `t` (`t` is finer than `s`). -/ instance : partial_order (ring_topology α) := partial_order.lift ring_topology.to_topological_space $ ext local notation `cont` := @continuous _ _ private def def_Inf (S : set (ring_topology α)) : ring_topology α := let Inf_S' := Inf (to_topological_space '' S) in { to_topological_space := Inf_S', continuous_add := begin apply continuous_Inf_rng.2, rintros _ ⟨⟨t, tr⟩, haS, rfl⟩, resetI, have h := continuous_Inf_dom (set.mem_image_of_mem to_topological_space haS) continuous_id, have h_continuous_id := @continuous.prod_map _ _ _ _ t t Inf_S' Inf_S' _ _ h h, exact @continuous.comp _ _ _ (id _) (id _) t _ _ continuous_add h_continuous_id, end, continuous_mul := begin apply continuous_Inf_rng.2, rintros _ ⟨⟨t, tr⟩, haS, rfl⟩, resetI, have h := continuous_Inf_dom (set.mem_image_of_mem to_topological_space haS) continuous_id, have h_continuous_id := @continuous.prod_map _ _ _ _ t t Inf_S' Inf_S' _ _ h h, exact @continuous.comp _ _ _ (id _) (id _) t _ _ continuous_mul h_continuous_id, end, continuous_neg := begin apply continuous_Inf_rng.2, rintros _ ⟨⟨t, tr⟩, haS, rfl⟩, resetI, have h := continuous_Inf_dom (set.mem_image_of_mem to_topological_space haS) continuous_id, exact @continuous.comp _ _ _ (id _) (id _) t _ _ continuous_neg h, end } /-- Ring topologies on `α` form a complete lattice, with `⊥` the discrete topology and `⊤` the indiscrete topology. The infimum of a collection of ring topologies is the topology generated by all their open sets (which is a ring topology). The supremum of two ring topologies `s` and `t` is the infimum of the family of all ring topologies contained in the intersection of `s` and `t`. -/ instance : complete_semilattice_Inf (ring_topology α) := { Inf := def_Inf, Inf_le := λ S a haS, by { apply topological_space.complete_lattice.Inf_le, use [a, ⟨ haS, rfl⟩] }, le_Inf := begin intros S a hab, apply topological_space.complete_lattice.le_Inf, rintros _ ⟨b, hbS, rfl⟩, exact hab b hbS, end, ..ring_topology.partial_order } instance : complete_lattice (ring_topology α) := complete_lattice_of_complete_semilattice_Inf _ /-- Given `f : α → β` and a topology on `α`, the coinduced ring topology on `β` is the finest topology such that `f` is continuous and `β` is a topological ring. -/ def coinduced {α β : Type*} [t : topological_space α] [ring β] (f : α → β) : ring_topology β := Inf {b : ring_topology β | (topological_space.coinduced f t) ≤ b.to_topological_space} lemma coinduced_continuous {α β : Type*} [t : topological_space α] [ring β] (f : α → β) : cont t (coinduced f).to_topological_space f := begin rw continuous_iff_coinduced_le, refine le_Inf _, rintros _ ⟨t', ht', rfl⟩, exact ht', end /-- The forgetful functor from ring topologies on `a` to additive group topologies on `a`. -/ def to_add_group_topology (t : ring_topology α) : add_group_topology α := { to_topological_space := t.to_topological_space, to_topological_add_group := @topological_ring.to_topological_add_group _ _ t.to_topological_space t.to_topological_ring } /-- The order embedding from ring topologies on `a` to additive group topologies on `a`. -/ def to_add_group_topology.order_embedding : order_embedding (ring_topology α) (add_group_topology α) := { to_fun := λ t, t.to_add_group_topology, inj' := begin intros t₁ t₂ h_eq, dsimp only at h_eq, ext, have h_t₁ : t₁.to_topological_space = t₁.to_add_group_topology.to_topological_space := rfl, rw [h_t₁, h_eq], refl, end, map_rel_iff' := begin intros t₁ t₂, rw [embedding.coe_fn_mk], have h_le : t₁ ≤ t₂ ↔ t₁.to_topological_space ≤ t₂.to_topological_space := by refl, rw h_le, refl, end } end ring_topology
fb8606da5641afd989e2f648fffe66baa5947fcd
a46270e2f76a375564f3b3e9c1bf7b635edc1f2c
/8.9.6.lean
6280fbd6a64700f9fb9fbc5b11914e02acd1476d
[ "CC0-1.0" ]
permissive
wudcscheme/lean-exercise
88ea2506714eac343de2a294d1132ee8ee6d3a20
5b23b9be3d361fff5e981d5be3a0a1175504b9f6
refs/heads/master
1,678,958,930,293
1,583,197,205,000
1,583,197,205,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,919
lean
inductive aexpr : Type | const : ℕ → aexpr | var : ℕ → aexpr | plus : aexpr → aexpr → aexpr | times : aexpr → aexpr → aexpr open aexpr def sample_aexpr : aexpr := plus (times (var 0) (const 7)) (times (const 2) (var 1)) -- BEGIN def aeval (v : ℕ → ℕ) : aexpr → ℕ | (const n) := n | (var n) := v n | (plus e₁ e₂) := (aeval e₁) + (aeval e₂) | (times e₁ e₂) := (aeval e₁) * (aeval e₂) def sample_val : ℕ → ℕ | 0 := 5 | 1 := 6 | _ := 0 -- Try it out. You should get 47 here. #eval aeval sample_val sample_aexpr -- END -- BEGIN def simp_const : aexpr → aexpr | (plus (const n₁) (const n₂)) := const (n₁ + n₂) | (times (const n₁) (const n₂)) := const (n₁ * n₂) | e := e def fuse : aexpr → aexpr | (plus e₁ e₂) := simp_const (plus (fuse e₁) (fuse e₂)) | (times e₁ e₂) := simp_const (times (fuse e₁) (fuse e₂)) | e := e #eval aeval sample_val (fuse sample_aexpr) theorem simp_const_eq (v : ℕ → ℕ) : ∀ e : aexpr, aeval v (simp_const e) = aeval v e | (const n) := rfl | (var n) := rfl | (plus e1 e2) := begin cases e1, { cases e2, reflexivity, repeat { dsimp [simp_const], reflexivity } }, repeat { dsimp [simp_const], reflexivity } end | (times e1 e2) := begin cases e1, { cases e2, reflexivity, repeat { dsimp [simp_const], reflexivity } }, repeat { dsimp [simp_const], reflexivity } end theorem fuse_eq (v : ℕ → ℕ) : ∀ e : aexpr, aeval v (fuse e) = aeval v e | (plus e1 e2) := begin dsimp [fuse], simp [simp_const_eq], dsimp [aeval], simp [fuse_eq e1, fuse_eq e2], end | (times e1 e2) := begin dsimp [fuse], simp [simp_const_eq], dsimp [aeval], simp [fuse_eq e1, fuse_eq e2], end | (const _) := rfl | (var _) := rfl -- END
67d1c3c8a796eaf270a9d5648ddabfeba9472ff4
618003631150032a5676f229d13a079ac875ff77
/src/tactic/noncomm_ring.lean
4c58d820db73473dc335297c1c7d74fd834097a9
[ "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,336
lean
/- Copyright (c) 2020 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Oliver Nash -/ import tactic.doc_commands import tactic.abel namespace tactic namespace interactive /-- A tactic for simplifying identities in not-necessarily-commutative rings. An example: ```lean example {R : Type*} [ring R] (a b c : R) : a * (b + c + c - b) = 2*a*c := by noncomm_ring ``` -/ meta def noncomm_ring := `[simp only [-- Expand everything out. add_mul, mul_add, sub_eq_add_neg, -- Right associate all products. mul_assoc, -- Expand powers to numerals. pow_bit0, pow_bit1, pow_one, -- Replace multiplication by numerals with `gsmul`. bit0_mul, mul_bit0, bit1_mul, mul_bit1, one_mul, mul_one, zero_mul, mul_zero, -- Pull `gsmul n` out the front so `abel` can see them. ←mul_gsmul_assoc, ←mul_gsmul_left, -- Pull out negations. neg_mul_eq_neg_mul_symm, mul_neg_eq_neg_mul_symm]; abel] add_tactic_doc { name := "noncomm_ring", category := doc_category.tactic, decl_names := [`tactic.interactive.noncomm_ring], tags := ["arithmetic", "simplification", "decision procedure"] } end interactive end tactic
7c2fb552f664fe03f08303ae34341c0062dabf2b
4727251e0cd73359b15b664c3170e5d754078599
/archive/oxford_invariants/2021summer/week3_p1.lean
e6c9599da8f9d063eb0771a98ec325783064b042
[ "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,766
lean
/- Copyright (c) 2021 Yaël Dillies, Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Bhavik Mehta -/ import algebra.big_operators.order import algebra.big_operators.ring import data.rat.cast /-! # The Oxford Invariants Puzzle Challenges - Summer 2021, Week 3, Problem 1 ## Original statement Let `n ≥ 3`, `a₁, ..., aₙ` be strictly positive integers such that `aᵢ ∣ aᵢ₋₁ + aᵢ₊₁` for `i = 2, ..., n - 1`. Show that $\sum_{i=1}^{n-1}\dfrac{a_0a_n}{a_ia_{i+1}} ∈ \mathbb N$. ## Comments Mathlib is based on type theory, so saying that a rational is a natural doesn't make sense. Instead, we ask that there exists `b : ℕ` whose cast to `α` is the sum we want. In mathlib, `ℕ` starts at `0`. To make the indexing cleaner, we use `a₀, ..., aₙ₋₁` instead of `a₁, ..., aₙ`. Similarly, it's nicer to not use substraction of naturals, so we replace `aᵢ ∣ aᵢ₋₁ + aᵢ₊₁` by `aᵢ₊₁ ∣ aᵢ + aᵢ₊₂`. We don't actually have to work in `ℚ` or `ℝ`. We can be even more general by stating the result for any linearly ordered field. Instead of having `n` naturals, we use a function `a : ℕ → ℕ`. In the proof itself, we replace `n : ℕ, 1 ≤ n` by `n + 1`. The statement is actually true for `n = 0, 1` (`n = 1, 2` before the reindexing) as the sum is simply `0` and `1` respectively. So the version we prove is slightly more general. Overall, the indexing is a bit of a mess to understand. But, trust Lean, it works. ## Formalised statement Let `n : ℕ`, `a : ℕ → ℕ`, `∀ i ≤ n, 0 < a i`, `∀ i, i + 2 ≤ n → aᵢ₊₁ ∣ aᵢ + aᵢ₊₂` (read `→` as "implies"). Then there exists `b : ℕ` such that `b` as an element of any linearly ordered field equals $\sum_{i=0}^{n-1} (a_0 a_n) / (a_i a_{i+1})$. ## Proof outline The case `n = 0` is trivial. For `n + 1`, we prove the result by induction but by adding `aₙ₊₁ ∣ aₙ * b - a₀` to the induction hypothesis, where `b` is the previous sum, $\sum_{i=0}^{n-1} (a_0 a_n) / (a_i a_{i+1})$, as a natural. * Base case: * $\sum_{i=0}^0 (a_0 a_{0+1}) / (a_0 a_{0+1})$ is a natural: $\sum_{i=0}^0 (a_0 a_{0+1}) / (a_0 a_{0+1}) = (a_0 a_1) / (a_0 a_1) = 1$. * Divisibility condition: `a₀ * 1 - a₀ = 0` is clearly divisible by `a₁`. * Induction step: * $\sum_{i=0}^n (a_0 a_{n+1}) / (a_i a_{i+1})$ is a natural: $$\sum_{i=0}^{n+1} (a_0 a_{n+2}) / (a_i a_{i+1}) = \sum_{i=0}^n\ (a_0 a_{n+2}) / (a_i a_{i+1}) + (a_0 a_{n+2}) / (a_{n+1} a_{n+2}) = a_{n+2} / a_{n+1} × \sum_{i=0}^n (a_0 a_{n+1}) / (a_i a_{i+1}) + a_0 / a_{n+1} = a_{n+2} / a_{n+1} × b + a_0 / a_{n+1} = (a_n + a_{n+2}) / a_{n+1} × b - (a_n b - a_0)(a_{n+1})$$ which is a natural because `(aₙ + aₙ₊₂)/aₙ₊₁`, `b` and `(aₙ * b - a₀)/aₙ₊₁` are (plus an annoying inequality, or the fact that the original sum is positive because its terms are). * Divisibility condition: `aₙ₊₁ * ((aₙ + aₙ₊₂)/aₙ₊₁ * b - (aₙ * b - a₀)/aₙ₊₁) - a₀ = aₙ₊₁aₙ₊₂b` is divisible by `aₙ₊₂`. -/ open_locale big_operators variables {α : Type*} [linear_ordered_field α] theorem week3_p1 (n : ℕ) (a : ℕ → ℕ) (a_pos : ∀ i ≤ n, 0 < a i) (ha : ∀ i, i + 2 ≤ n → a (i + 1) ∣ a i + a (i + 2)) : ∃ b : ℕ, (b : α) = ∑ i in finset.range n, (a 0 * a n)/(a i * a (i + 1)) := begin -- Treat separately `n = 0` and `n ≥ 1` cases n, /- Case `n = 0` The sum is trivially equal to `0` -/ { exact ⟨0, by rw [nat.cast_zero, finset.sum_range_zero]⟩ }, -- `⟨Claim it, Prove it⟩` /- Case `n ≥ 1`. We replace `n` by `n + 1` everywhere to make this inequality explicit Set up the stronger induction hypothesis -/ suffices h : ∃ b : ℕ, (b : α) = ∑ i in finset.range (n + 1), (a 0 * a (n + 1))/(a i * a (i + 1)) ∧ a (n + 1) ∣ a n * b - a 0, { obtain ⟨b, hb, -⟩ := h, exact ⟨b, hb⟩ }, simp_rw ←@nat.cast_pos α at a_pos, /- Declare the induction `ih` will be the induction hypothesis -/ induction n with n ih, /- Base case Claim that the sum equals `1`-/ { refine ⟨1, _, _⟩, -- Check that this indeed equals the sum { rw [nat.cast_one, finset.sum_range_one, div_self], exact (mul_pos (a_pos 0 (nat.zero_le _)) (a_pos 1 (nat.zero_lt_succ _))).ne' }, -- Check the divisibility condition { rw [mul_one, tsub_self], exact dvd_zero _ } }, /- Induction step `b` is the value of the previous sum as a natural, `hb` is the proof that it is indeed the value, and `han` is the divisibility condition -/ obtain ⟨b, hb, han⟩ := ih (λ i hi, ha i $ nat.le_succ_of_le hi) (λ i hi, a_pos i $ nat.le_succ_of_le hi), specialize ha n le_rfl, have ha₀ : a 0 ≤ a n * b, -- Needing this is an artifact of `ℕ`-substraction. { rw [←@nat.cast_le α, nat.cast_mul, hb, ←div_le_iff' (a_pos _ $ n.le_succ.trans $ nat.le_succ _), ←mul_div_mul_right _ _ (a_pos _ $ nat.le_succ _).ne'], suffices h : ∀ i, i ∈ finset.range (n + 1) → 0 ≤ (a 0 : α) * a (n + 1) / (a i * a (i + 1)), { exact finset.single_le_sum h (finset.self_mem_range_succ n) }, refine (λ i _, div_nonneg _ _); refine mul_nonneg _ _; exact nat.cast_nonneg _ }, -- Claim that the sum equals `(aₙ + aₙ₊₂)/aₙ₊₁ * b - (aₙ * b - a₀)/aₙ₊₁` refine ⟨(a n + a (n + 2))/ a (n + 1) * b - (a n * b - a 0) / a (n + 1), _, _⟩, -- Check that this indeed equals the sum { calc (((a n + a (n + 2)) / a (n + 1) * b - (a n * b - a 0) / a (n + 1) : ℕ) : α) = (a n + a (n + 2)) / a (n + 1) * b - (a n * b - a 0) / a (n + 1) : begin norm_cast, rw nat.cast_sub (nat.div_le_of_le_mul _), rw [←mul_assoc, nat.mul_div_cancel' ha, add_mul], exact tsub_le_self.trans (nat.le_add_right _ _), end ... = a (n + 2) / a (n + 1) * b + (a 0 * a (n + 2)) / (a (n + 1) * a (n + 2)) : by rw [add_div, add_mul, sub_div, mul_div_right_comm, add_sub_sub_cancel, mul_div_mul_right _ _ (a_pos _ le_rfl).ne'] ... = ∑ (i : ℕ) in finset.range (n + 2), a 0 * a (n + 2) / (a i * a (i + 1)) : begin rw [finset.sum_range_succ, hb, finset.mul_sum], congr, ext i, rw [←mul_div_assoc, ←mul_div_right_comm, mul_div_assoc, mul_div_cancel _ (a_pos _ $ nat.le_succ _).ne', mul_comm], end }, -- Check the divisibility condition { rw [mul_tsub, ← mul_assoc, nat.mul_div_cancel' ha, add_mul, nat.mul_div_cancel' han, add_tsub_tsub_cancel ha₀, add_tsub_cancel_right], exact dvd_mul_right _ _ } end
f9b7ec8b9b7fad5a2c1141aa0af734dc64d96c62
2ee768fce4d0783536570385ee340d97e2411436
/src/v_def.lean
abcedf425c89831f653b3335c7ad7df6477d199f
[]
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
4,833
lean
import topology.instances.real local prefix `𝒫`:100 := λ {α : Type} (s : finset α), {t // t ≤ s} /-- We consider times `t` up to a final (possibly infinite) time `T`. -/ def Tt (T : with_top ℝ) : set ℝ := {t : ℝ | 0 ≤ t ∧ (t : with_top ℝ) < T} /-- At `t` we will be interested in maturities `τ` between `t` and `T`. -/ def Tτ {T : with_top ℝ} (t : Tt T) : set ℝ := {τ : ℝ | t.1 < τ ∧ (τ : with_top ℝ) ≤ T} /-- Where `𝒩` is the set of banks, `X 𝒩` is the space of (log) external asset values associated. -/ def X (𝒩 : Type) := 𝒩 → ℝ /-- A *debt function* returns for a given point in time and space a valuation for each bank's debt for each possible maturity. -/ def debt_fn (𝒩 : Type) (T : with_top ℝ) := ∀ (t : Tt T), X 𝒩 → 𝒩 → Tτ t → ℝ variables {𝒩 : Type} {T : with_top ℝ} instance : has_coe ℝ (X 𝒩) := ⟨fun r i, r⟩ instance : has_subset (Tt T → set (X 𝒩)) := ⟨fun V₁ V₂, (∀ t, V₁ t ⊆ V₂ t)⟩ instance : add_comm_group (X 𝒩) := pi.add_comm_group noncomputable instance : topological_space (X 𝒩) := Pi.topological_space instance : topological_add_group (X 𝒩) := pi.topological_add_group instance : has_zero (debt_fn 𝒩 T) := pi.has_zero noncomputable instance : lattice (debt_fn 𝒩 T) := pi.lattice /-- A valuation function defined throughout time and space may be `continuous_wrt_assets`. -/ def continuous_wrt_assets {α : Tt T → Type*} [∀ t, topological_space (α t)] (v : ∀ (t : Tt T), X 𝒩 → α t) : Prop := ∀ t, continuous (v t) /-- A valuation function defined on a domain's complement may be `continuous_wrt_assets_on_compl` -/ def continuous_wrt_assets_on_compl {α : Tt T → Type*} [∀ t, topological_space (α t)] {V : Tt T → set (X 𝒩)} (v' : ∀ t y, y ∉ V t → α t) : Prop := ∀ t, continuous (fun y : (V t)ᶜ, v' t y y.prop) section variables (𝒩) (T) /-- Our model of credit risk is a structural one. Properties of `ℋ` are based on those of a solution procedure for the Black-Scholes-Merton partial differential equation. Given a valuation function defined outside some time-dependent domain `V` (implying initial/boundary conditions), `ℋ` will return a function defined on the whole space. -/ structure well_behaved_soln := (ℋ : ∀ {V : Tt T → set (X 𝒩)} (v' : ∀ t y, y ∉ V t → Tτ t → ℝ), (∀ (t : Tt T), X 𝒩 → Tτ t → ℝ)) (matching_on_complement {V : Tt T → set (X 𝒩)} (v' : ∀ t y, y ∉ V t → Tτ t → ℝ) : ∀ t y h, ℋ v' t y = v' t y h) (positivity_preserving {V : Tt T → set (X 𝒩)} (v' : ∀ t y, y ∉ V t → Tτ t → ℝ) : (∀ t y h, 0 ≤ v' t y h) → 0 ≤ ℋ v') (continuity_preserving {V : Tt T → set (X 𝒩)} (v' : ∀ t y, y ∉ V t → Tτ t → ℝ) : continuous_wrt_assets_on_compl v' → continuous_wrt_assets (ℋ v')) (translation_invariant {V : Tt T → set (X 𝒩)} (v' : ∀ t y, y ∉ V t → Tτ t → ℝ) : ∀ η t y, ℋ v' t (y + η) = ℋ (fun s x (h : x + η ∉ V s), v' s (x + η) h) t y) (compatible_on_subsets {V V' : Tt T → set (X 𝒩)} {v' : ∀ t y, y ∉ V t → Tτ t → ℝ} : V' ⊆ V → ℋ v' = ℋ (fun t y (h : y ∉ V' t), ℋ v' t y)) (mono_wrt_val_on_compl {V : Tt T → set (X 𝒩)} {v₁ v₂ : ∀ t y, y ∉ V t → Tτ t → ℝ} : (∀ t y h, v₁ t y h ≤ v₂ t y h) → ℋ v₁ ≤ ℋ v₂) end instance : has_coe_to_fun (well_behaved_soln 𝒩 T) := { F := fun _, ∀ {V : Tt T → set (X 𝒩)} (v' : ∀ t y, y ∉ V t → Tτ t → ℝ), (∀ t, X 𝒩 → Tτ t → ℝ), coe := well_behaved_soln.ℋ } /-- We define also a function named `V`, argument `ψ`, returning the set of points with `ψ A = A`. It is the domain passed to `ℋ`. Later we will ask that `V` correspond to the region with `A` viable. -/ def V (ψ : ∀ (B : finset 𝒩), Tt T → X 𝒩 → 𝒫 B) (A : finset 𝒩) : Tt T → set (X 𝒩) := fun t y, A ≤ ψ A t y variables [decidable_eq 𝒩] (ℋ : well_behaved_soln 𝒩 T) instance (ψ : ∀ B, Tt T → X 𝒩 → 𝒫 B) (A : finset 𝒩) (t : Tt T) : ∀ y, decidable (y ∈ V ψ A t) := by { delta V, apply_instance, } /-- Given a set of banks `B`, a survivors function for `B` and a debt function for each proper subset of `B`, we use `ℋ` to calculate a debt function for `B` itself. -/ def v_mk {B : finset 𝒩} (ψB : Tt T → X 𝒩 → 𝒫 B) (υ : ∀ C < B, debt_fn 𝒩 T) : debt_fn 𝒩 T := fun t y i, if i ∈ B then ℋ (fun s x h, υ (ψB s x) ⟨(ψB s x).prop, h⟩ s x i) t y else 0 /-- By induction we find debt functions for each set of banks up to the full set (likely what we want). -/ def v (ψ : ∀ (B : finset 𝒩), Tt T → X 𝒩 → 𝒫 B) : finset 𝒩 → debt_fn 𝒩 T := finset.strong_induction (fun B, v_mk ℋ (ψ B))
25399dd604224029f0e6041f188a5099bc77b8b3
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebraic_topology/dold_kan/functor_gamma.lean
bd138e8ba7d11a8a1f06a89cdfdeb868ece15d49
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
14,408
lean
/- Copyright (c) 2022 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou -/ import algebraic_topology.dold_kan.split_simplicial_object /-! # Construction of the inverse functor of the Dold-Kan equivalence > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file, we construct the functor `Γ₀ : chain_complex C ℕ ⥤ simplicial_object C` which shall be the inverse functor of the Dold-Kan equivalence in the case of abelian categories, and more generally pseudoabelian categories. By definition, when `K` is a chain_complex, `Γ₀.obj K` is a simplicial object which sends `Δ : simplex_categoryᵒᵖ` to a certain coproduct indexed by the set `splitting.index_set Δ` whose elements consists of epimorphisms `e : Δ.unop ⟶ Δ'.unop` (with `Δ' : simplex_categoryᵒᵖ`); the summand attached to such an `e` is `K.X Δ'.unop.len`. By construction, `Γ₀.obj K` is a split simplicial object whose splitting is `Γ₀.splitting K`. We also construct `Γ₂ : karoubi (chain_complex C ℕ) ⥤ karoubi (simplicial_object C)` which shall be an equivalence for any additive category `C`. (See `equivalence.lean` for the general strategy of proof of the Dold-Kan equivalence.) -/ noncomputable theory open category_theory category_theory.category category_theory.limits simplex_category simplicial_object opposite category_theory.idempotents open_locale simplicial dold_kan namespace algebraic_topology namespace dold_kan variables {C : Type*} [category C] [preadditive C] (K K' : chain_complex C ℕ) (f : K ⟶ K') {Δ'' Δ' Δ : simplex_category} (i' : Δ'' ⟶ Δ') [mono i'] (i : Δ' ⟶ Δ) [mono i] /-- `is_δ₀ i` is a simple condition used to check whether a monomorphism `i` in `simplex_category` identifies to the coface map `δ 0`. -/ @[nolint unused_arguments] def is_δ₀ {Δ Δ' : simplex_category} (i : Δ' ⟶ Δ) [mono i] : Prop := (Δ.len = Δ'.len+1) ∧ (i.to_order_hom 0 ≠ 0) namespace is_δ₀ lemma iff {j : ℕ} {i : fin (j+2)} : is_δ₀ (simplex_category.δ i) ↔ i = 0 := begin split, { rintro ⟨h₁, h₂⟩, by_contradiction, exact h₂ (fin.succ_above_ne_zero_zero h), }, { rintro rfl, exact ⟨rfl, fin.succ_ne_zero _⟩, }, end lemma eq_δ₀ {n : ℕ} {i : [n] ⟶ [n+1]} [mono i] (hi : is_δ₀ i) : i = simplex_category.δ 0 := begin unfreezingI { obtain ⟨j, rfl⟩ := simplex_category.eq_δ_of_mono i, }, rw iff at hi, rw hi, end end is_δ₀ namespace Γ₀ namespace obj /-- In the definition of `(Γ₀.obj K).obj Δ` as a direct sum indexed by `A : splitting.index_set Δ`, the summand `summand K Δ A` is `K.X A.1.len`. -/ def summand (Δ : simplex_categoryᵒᵖ) (A : splitting.index_set Δ) : C := K.X A.1.unop.len /-- The functor `Γ₀` sends a chain complex `K` to the simplicial object which sends `Δ` to the direct sum of the objects `summand K Δ A` for all `A : splitting.index_set Δ` -/ def obj₂ (K : chain_complex C ℕ) (Δ : simplex_categoryᵒᵖ) [has_finite_coproducts C] : C := ∐ (λ (A : splitting.index_set Δ), summand K Δ A) namespace termwise /-- A monomorphism `i : Δ' ⟶ Δ` induces a morphism `K.X Δ.len ⟶ K.X Δ'.len` which is the identity if `Δ = Δ'`, the differential on the complex `K` if `i = δ 0`, and zero otherwise. -/ def map_mono (K : chain_complex C ℕ) {Δ' Δ : simplex_category} (i : Δ' ⟶ Δ) [mono i] : K.X Δ.len ⟶ K.X Δ'.len := begin by_cases Δ = Δ', { exact eq_to_hom (by congr'), }, { by_cases is_δ₀ i, { exact K.d Δ.len Δ'.len, }, { exact 0, }, }, end variable (Δ) lemma map_mono_id : map_mono K (𝟙 Δ) = 𝟙 _ := by { unfold map_mono, simp only [eq_self_iff_true, eq_to_hom_refl, dite_eq_ite, if_true], } variable {Δ} lemma map_mono_δ₀' (hi : is_δ₀ i) : map_mono K i = K.d Δ.len Δ'.len := begin unfold map_mono, classical, rw [dif_neg, dif_pos hi], unfreezingI { rintro rfl, }, simpa only [self_eq_add_right, nat.one_ne_zero] using hi.1, end @[simp] lemma map_mono_δ₀ {n : ℕ} : map_mono K (δ (0 : fin (n+2))) = K.d (n+1) n := map_mono_δ₀' K _ (by rw is_δ₀.iff) lemma map_mono_eq_zero (h₁ : Δ ≠ Δ') (h₂ : ¬is_δ₀ i) : map_mono K i = 0 := by { unfold map_mono, rw ne.def at h₁, split_ifs, refl, } variables {K K'} @[simp, reassoc] lemma map_mono_naturality : map_mono K i ≫ f.f Δ'.len = f.f Δ.len ≫ map_mono K' i := begin unfold map_mono, split_ifs, { unfreezingI { subst h, }, simp only [id_comp, eq_to_hom_refl, comp_id], }, { rw homological_complex.hom.comm, }, { rw [zero_comp, comp_zero], } end variable (K) @[simp, reassoc] lemma map_mono_comp : map_mono K i ≫ map_mono K i' = map_mono K (i' ≫ i) := begin /- case where i : Δ' ⟶ Δ is the identity -/ by_cases h₁ : Δ = Δ', { unfreezingI { subst h₁, }, simp only [simplex_category.eq_id_of_mono i, comp_id, id_comp, map_mono_id K, eq_to_hom_refl], }, /- case where i' : Δ'' ⟶ Δ' is the identity -/ by_cases h₂ : Δ' = Δ'', { unfreezingI { subst h₂, }, simp only [simplex_category.eq_id_of_mono i', comp_id, id_comp, map_mono_id K, eq_to_hom_refl], }, /- then the RHS is always zero -/ obtain ⟨k, hk⟩ := nat.exists_eq_add_of_lt (len_lt_of_mono i h₁), obtain ⟨k', hk'⟩ := nat.exists_eq_add_of_lt (len_lt_of_mono i' h₂), have eq : Δ.len = Δ''.len + (k+k'+2) := by linarith, rw map_mono_eq_zero K (i' ≫ i) _ _, rotate, { by_contradiction, simpa only [self_eq_add_right, h] using eq, }, { by_contradiction, simp only [h.1, add_right_inj] at eq, linarith, }, /- in all cases, the LHS is also zero, either by definition, or because d ≫ d = 0 -/ by_cases h₃ : is_δ₀ i, { by_cases h₄ : is_δ₀ i', { rw [map_mono_δ₀' K i h₃, map_mono_δ₀' K i' h₄, homological_complex.d_comp_d], }, { simp only [map_mono_eq_zero K i' h₂ h₄, comp_zero], }, }, { simp only [map_mono_eq_zero K i h₁ h₃, zero_comp], }, end end termwise variable [has_finite_coproducts C] /-- The simplicial morphism on the simplicial object `Γ₀.obj K` induced by a morphism `Δ' → Δ` in `simplex_category` is defined on each summand associated to an `A : Γ_index_set Δ` in terms of the epi-mono factorisation of `θ ≫ A.e`. -/ def map (K : chain_complex C ℕ) {Δ' Δ : simplex_categoryᵒᵖ} (θ : Δ ⟶ Δ') : obj₂ K Δ ⟶ obj₂ K Δ' := sigma.desc (λ A, termwise.map_mono K (image.ι (θ.unop ≫ A.e)) ≫ (sigma.ι (summand K Δ') (A.pull θ))) @[reassoc] lemma map_on_summand₀ {Δ Δ' : simplex_categoryᵒᵖ} (A : splitting.index_set Δ) {θ : Δ ⟶ Δ'} {Δ'' : simplex_category} {e : Δ'.unop ⟶ Δ''} {i : Δ'' ⟶ A.1.unop} [epi e] [mono i] (fac : e ≫ i = θ.unop ≫ A.e) : (sigma.ι (summand K Δ) A) ≫ map K θ = termwise.map_mono K i ≫ sigma.ι (summand K Δ') (splitting.index_set.mk e) := begin simp only [map, colimit.ι_desc, cofan.mk_ι_app], have h := simplex_category.image_eq fac, unfreezingI { subst h, }, congr, { exact simplex_category.image_ι_eq fac, }, { dsimp only [simplicial_object.splitting.index_set.pull], congr, exact simplex_category.factor_thru_image_eq fac, }, end @[reassoc] lemma map_on_summand₀' {Δ Δ' : simplex_categoryᵒᵖ} (A : splitting.index_set Δ) (θ : Δ ⟶ Δ') : (sigma.ι (summand K Δ) A) ≫ map K θ = termwise.map_mono K (image.ι (θ.unop ≫ A.e)) ≫ sigma.ι (summand K _) (A.pull θ) := map_on_summand₀ K A (A.fac_pull θ) end obj variable [has_finite_coproducts C] /-- The functor `Γ₀ : chain_complex C ℕ ⥤ simplicial_object C`, on objects. -/ @[simps] def obj (K : chain_complex C ℕ) : simplicial_object C := { obj := λ Δ, obj.obj₂ K Δ, map := λ Δ Δ' θ, obj.map K θ, map_id' := λ Δ, begin ext A, cases A, have fac : A.e ≫ 𝟙 A.1.unop = (𝟙 Δ).unop ≫ A.e := by rw [unop_id, comp_id, id_comp], erw [obj.map_on_summand₀ K A fac, obj.termwise.map_mono_id, id_comp, comp_id], unfreezingI { rcases A with ⟨Δ', ⟨e, he⟩⟩, }, refl, end, map_comp' := λ Δ'' Δ' Δ θ' θ, begin ext A, cases A, have fac : θ.unop ≫ θ'.unop ≫ A.e = (θ' ≫ θ).unop ≫ A.e := by rw [unop_comp, assoc], rw [← image.fac (θ'.unop ≫ A.e), ← assoc, ← image.fac (θ.unop ≫ factor_thru_image (θ'.unop ≫ A.e)), assoc] at fac, simpa only [obj.map_on_summand₀'_assoc K A θ', obj.map_on_summand₀' K _ θ, obj.termwise.map_mono_comp_assoc, obj.map_on_summand₀ K A fac], end } lemma splitting_map_eq_id (Δ : simplex_categoryᵒᵖ) : (simplicial_object.splitting.map (Γ₀.obj K) (λ (n : ℕ), sigma.ι (Γ₀.obj.summand K (op [n])) (splitting.index_set.id (op [n]))) Δ) = 𝟙 _ := begin ext A, discrete_cases, induction Δ using opposite.rec, induction Δ with n, dsimp, simp only [colimit.ι_desc, cofan.mk_ι_app, comp_id, Γ₀.obj_map], rw [Γ₀.obj.map_on_summand₀ K (simplicial_object.splitting.index_set.id A.1) (show A.e ≫ 𝟙 _ = A.e.op.unop ≫ 𝟙 _, by refl), Γ₀.obj.termwise.map_mono_id, A.ext'], apply id_comp, end /-- By construction, the simplicial `Γ₀.obj K` is equipped with a splitting. -/ def splitting (K : chain_complex C ℕ) : simplicial_object.splitting (Γ₀.obj K) := { N := λ n, K.X n, ι := λ n, sigma.ι (Γ₀.obj.summand K (op [n])) (splitting.index_set.id (op [n])), map_is_iso' := λ Δ, begin rw Γ₀.splitting_map_eq_id, apply is_iso.id, end, } @[simp] lemma splitting_iso_hom_eq_id (Δ : simplex_categoryᵒᵖ) : ((splitting K).iso Δ).hom = 𝟙 _ := splitting_map_eq_id K Δ @[reassoc] lemma obj.map_on_summand {Δ Δ' : simplex_categoryᵒᵖ} (A : splitting.index_set Δ) (θ : Δ ⟶ Δ') {Δ'' : simplex_category} {e : Δ'.unop ⟶ Δ''} {i : Δ'' ⟶ A.1.unop} [epi e] [mono i] (fac : e ≫ i = θ.unop ≫ A.e) : (Γ₀.splitting K).ι_summand A ≫ (Γ₀.obj K).map θ = Γ₀.obj.termwise.map_mono K i ≫ (Γ₀.splitting K).ι_summand (splitting.index_set.mk e) := begin dsimp only [simplicial_object.splitting.ι_summand, simplicial_object.splitting.ι_coprod], simp only [assoc, Γ₀.splitting_iso_hom_eq_id, id_comp, comp_id], exact Γ₀.obj.map_on_summand₀ K A fac, end @[reassoc] lemma obj.map_on_summand' {Δ Δ' : simplex_categoryᵒᵖ} (A : splitting.index_set Δ) (θ : Δ ⟶ Δ') : (splitting K).ι_summand A ≫ (obj K).map θ = obj.termwise.map_mono K (image.ι (θ.unop ≫ A.e)) ≫ (splitting K).ι_summand (A.pull θ) := by { apply obj.map_on_summand, apply image.fac, } @[reassoc] lemma obj.map_mono_on_summand_id {Δ Δ' : simplex_category} (i : Δ' ⟶ Δ) [mono i] : (splitting K).ι_summand (splitting.index_set.id (op Δ)) ≫ (obj K).map i.op = obj.termwise.map_mono K i ≫ (splitting K).ι_summand (splitting.index_set.id (op Δ')) := obj.map_on_summand K (splitting.index_set.id (op Δ)) i.op (rfl : 𝟙 _ ≫ i = i ≫ 𝟙 _) @[reassoc] lemma obj.map_epi_on_summand_id {Δ Δ' : simplex_category } (e : Δ' ⟶ Δ) [epi e] : (Γ₀.splitting K).ι_summand (splitting.index_set.id (op Δ)) ≫ (Γ₀.obj K).map e.op = (Γ₀.splitting K).ι_summand (splitting.index_set.mk e) := by simpa only [Γ₀.obj.map_on_summand K (splitting.index_set.id (op Δ)) e.op (rfl : e ≫ 𝟙 Δ = e ≫ 𝟙 Δ), Γ₀.obj.termwise.map_mono_id] using id_comp _ /-- The functor `Γ₀ : chain_complex C ℕ ⥤ simplicial_object C`, on morphisms. -/ @[simps] def map {K K' : chain_complex C ℕ} (f : K ⟶ K') : obj K ⟶ obj K' := { app := λ Δ, (Γ₀.splitting K).desc Δ (λ A, f.f A.1.unop.len ≫ (Γ₀.splitting K').ι_summand A), naturality' := λ Δ' Δ θ, begin apply (Γ₀.splitting K).hom_ext', intro A, simp only [(splitting K).ι_desc_assoc, obj.map_on_summand'_assoc K _ θ, (splitting K).ι_desc, assoc, obj.map_on_summand' K' _ θ], apply obj.termwise.map_mono_naturality_assoc, end, } end Γ₀ variable [has_finite_coproducts C] /-- The functor `Γ₀' : chain_complex C ℕ ⥤ simplicial_object.split C` that induces `Γ₀ : chain_complex C ℕ ⥤ simplicial_object C`, which shall be the inverse functor of the Dold-Kan equivalence for abelian or pseudo-abelian categories. -/ @[simps] def Γ₀' : chain_complex C ℕ ⥤ simplicial_object.split C := { obj := λ K, simplicial_object.split.mk' (Γ₀.splitting K), map := λ K K' f, { F := Γ₀.map f, f := f.f, comm' := λ n, by { dsimp, simpa only [← splitting.ι_summand_id, (Γ₀.splitting K).ι_desc], }, }, } /-- The functor `Γ₀ : chain_complex C ℕ ⥤ simplicial_object C`, which is the inverse functor of the Dold-Kan equivalence when `C` is an abelian category, or more generally a pseudoabelian category. -/ @[simps] def Γ₀ : chain_complex C ℕ ⥤ simplicial_object C := Γ₀' ⋙ split.forget _ /-- The extension of `Γ₀ : chain_complex C ℕ ⥤ simplicial_object C` on the idempotent completions. It shall be an equivalence of categories for any additive category `C`. -/ @[simps] def Γ₂ : karoubi (chain_complex C ℕ) ⥤ karoubi (simplicial_object C) := (category_theory.idempotents.functor_extension₂ _ _).obj Γ₀ lemma higher_faces_vanish.on_Γ₀_summand_id (K : chain_complex C ℕ) (n : ℕ) : higher_faces_vanish (n+1) ((Γ₀.splitting K).ι_summand (splitting.index_set.id (op [n+1]))) := begin intros j hj, have eq := Γ₀.obj.map_mono_on_summand_id K (simplex_category.δ j.succ), rw [Γ₀.obj.termwise.map_mono_eq_zero K, zero_comp] at eq, rotate, { intro h, exact (nat.succ_ne_self n) (congr_arg simplex_category.len h), }, { exact λ h, fin.succ_ne_zero j (by simpa only [is_δ₀.iff] using h), }, exact eq, end @[simp, reassoc] lemma P_infty_on_Γ₀_splitting_summand_eq_self (K : chain_complex C ℕ) {n : ℕ} : (Γ₀.splitting K).ι_summand (splitting.index_set.id (op [n])) ≫ (P_infty : K[Γ₀.obj K] ⟶ _).f n = (Γ₀.splitting K).ι_summand (splitting.index_set.id (op [n])) := begin rw P_infty_f, cases n, { simpa only [P_f_0_eq] using comp_id _, }, { exact (higher_faces_vanish.on_Γ₀_summand_id K n).comp_P_eq_self, }, end end dold_kan end algebraic_topology
be2c31b40491c8c2a996e9fb59ce2ed96a36755c
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/algebra/gcd_monoid/basic.lean
d84cc4d8f8aa58b93de86ba45323bc9f6e0f7825
[ "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
34,645
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jens Wagemaker -/ import algebra.associated import algebra.group_power.lemmas import data.nat.gcd /-! # Monoids with normalization functions, `gcd`, and `lcm` This file defines extra structures on `comm_cancel_monoid_with_zero`s, including `integral_domain`s. ## Main Definitions * `normalization_monoid` * `gcd_monoid` * `gcd_monoid_of_exists_gcd` * `gcd_monoid_of_exists_lcm` For the `gcd_monoid` instances on `ℕ` and `ℤ`, see `ring_theory.int.basic`. ## Implementation Notes * `normalization_monoid` is defined by assigning to each element a `norm_unit` such that multiplying by that unit normalizes the monoid, and `normalize` is an idempotent monoid homomorphism. This definition as currently implemented does casework on `0`. * `gcd_monoid` extends `normalization_monoid`, so the `gcd` and `lcm` are always normalized. This makes `gcd`s of polynomials easier to work with, but excludes Euclidean domains, and monoids without zero. * `gcd_monoid_of_gcd` noncomputably constructs a `gcd_monoid` structure just from the `gcd` and its properties. * `gcd_monoid_of_exists_gcd` noncomputably constructs a `gcd_monoid` structure just from a proof that any two elements have a (not necessarily normalized) `gcd`. * `gcd_monoid_of_lcm` noncomputably constructs a `gcd_monoid` structure just from the `lcm` and its properties. * `gcd_monoid_of_exists_lcm` noncomputably constructs a `gcd_monoid` structure just from a proof that any two elements have a (not necessarily normalized) `lcm`. ## TODO * Port GCD facts about nats, definition of coprime * Generalize normalization monoids to commutative (cancellative) monoids with or without zero * Generalize GCD monoid to not require normalization in all cases ## Tags divisibility, gcd, lcm, normalize -/ variables {α : Type*} set_option old_structure_cmd true /-- Normalization monoid: multiplying with `norm_unit` gives a normal form for associated elements. -/ @[protect_proj] class normalization_monoid (α : Type*) [nontrivial α] [comm_cancel_monoid_with_zero α] := (norm_unit : α → units α) (norm_unit_zero : norm_unit 0 = 1) (norm_unit_mul : ∀{a b}, a ≠ 0 → b ≠ 0 → norm_unit (a * b) = norm_unit a * norm_unit b) (norm_unit_coe_units : ∀(u : units α), norm_unit u = u⁻¹) export normalization_monoid (norm_unit norm_unit_zero norm_unit_mul norm_unit_coe_units) attribute [simp] norm_unit_coe_units norm_unit_zero norm_unit_mul section normalization_monoid variables [comm_cancel_monoid_with_zero α] [nontrivial α] [normalization_monoid α] @[simp] theorem norm_unit_one : norm_unit (1:α) = 1 := norm_unit_coe_units 1 /-- Chooses an element of each associate class, by multiplying by `norm_unit` -/ def normalize : monoid_with_zero_hom α α := { to_fun := λ x, x * norm_unit x, map_zero' := by simp, map_one' := by rw [norm_unit_one, units.coe_one, mul_one], map_mul' := λ x y, classical.by_cases (λ hx : x = 0, by rw [hx, zero_mul, zero_mul, zero_mul]) $ λ hx, classical.by_cases (λ hy : y = 0, by rw [hy, mul_zero, zero_mul, mul_zero]) $ λ hy, by simp only [norm_unit_mul hx hy, units.coe_mul]; simp only [mul_assoc, mul_left_comm y], } theorem associated_normalize (x : α) : associated x (normalize x) := ⟨_, rfl⟩ theorem normalize_associated (x : α) : associated (normalize x) x := (associated_normalize _).symm lemma associates.mk_normalize (x : α) : associates.mk (normalize x) = associates.mk x := associates.mk_eq_mk_iff_associated.2 (normalize_associated _) @[simp] lemma normalize_apply (x : α) : normalize x = x * norm_unit x := rfl @[simp] lemma normalize_zero : normalize (0 : α) = 0 := normalize.map_zero @[simp] lemma normalize_one : normalize (1 : α) = 1 := normalize.map_one lemma normalize_coe_units (u : units α) : normalize (u : α) = 1 := by simp lemma normalize_eq_zero {x : α} : normalize x = 0 ↔ x = 0 := ⟨λ hx, (associated_zero_iff_eq_zero x).1 $ hx ▸ associated_normalize _, by rintro rfl; exact normalize_zero⟩ lemma normalize_eq_one {x : α} : normalize x = 1 ↔ is_unit x := ⟨λ hx, is_unit_iff_exists_inv.2 ⟨_, hx⟩, λ ⟨u, hu⟩, hu ▸ normalize_coe_units u⟩ @[simp] theorem norm_unit_mul_norm_unit (a : α) : norm_unit (a * norm_unit a) = 1 := classical.by_cases (assume : a = 0, by simp only [this, norm_unit_zero, zero_mul]) $ assume h, by rw [norm_unit_mul h (units.ne_zero _), norm_unit_coe_units, mul_inv_eq_one] theorem normalize_idem (x : α) : normalize (normalize x) = normalize x := by simp theorem normalize_eq_normalize {a b : α} (hab : a ∣ b) (hba : b ∣ a) : normalize a = normalize b := begin rcases associated_of_dvd_dvd hab hba with ⟨u, rfl⟩, refine classical.by_cases (by rintro rfl; simp only [zero_mul]) (assume ha : a ≠ 0, _), suffices : a * ↑(norm_unit a) = a * ↑u * ↑(norm_unit a) * ↑u⁻¹, by simpa only [normalize_apply, mul_assoc, norm_unit_mul ha u.ne_zero, norm_unit_coe_units], calc a * ↑(norm_unit a) = a * ↑(norm_unit a) * ↑u * ↑u⁻¹: (units.mul_inv_cancel_right _ _).symm ... = a * ↑u * ↑(norm_unit a) * ↑u⁻¹ : by rw mul_right_comm a end lemma normalize_eq_normalize_iff {x y : α} : normalize x = normalize y ↔ x ∣ y ∧ y ∣ x := ⟨λ h, ⟨units.dvd_mul_right.1 ⟨_, h.symm⟩, units.dvd_mul_right.1 ⟨_, h⟩⟩, λ ⟨hxy, hyx⟩, normalize_eq_normalize hxy hyx⟩ theorem dvd_antisymm_of_normalize_eq {a b : α} (ha : normalize a = a) (hb : normalize b = b) (hab : a ∣ b) (hba : b ∣ a) : a = b := ha ▸ hb ▸ normalize_eq_normalize hab hba --can be proven by simp lemma dvd_normalize_iff {a b : α} : a ∣ normalize b ↔ a ∣ b := units.dvd_mul_right --can be proven by simp lemma normalize_dvd_iff {a b : α} : normalize a ∣ b ↔ a ∣ b := units.mul_right_dvd end normalization_monoid namespace comm_group_with_zero variables [decidable_eq α] [comm_group_with_zero α] @[priority 100] -- see Note [lower instance priority] instance : normalization_monoid α := { norm_unit := λ x, if h : x = 0 then 1 else (units.mk0 x h)⁻¹, norm_unit_zero := dif_pos rfl, norm_unit_mul := λ x y x0 y0, units.eq_iff.1 (by simp [x0, y0, mul_comm]), norm_unit_coe_units := λ u, by { rw [dif_neg (units.ne_zero _), units.mk0_coe], apply_instance } } @[simp] lemma coe_norm_unit {a : α} (h0 : a ≠ 0) : (↑(norm_unit a) : α) = a⁻¹ := by simp [norm_unit, h0] end comm_group_with_zero namespace associates variables [comm_cancel_monoid_with_zero α] [nontrivial α] [normalization_monoid α] local attribute [instance] associated.setoid /-- Maps an element of `associates` back to the normalized element of its associate class -/ protected def out : associates α → α := quotient.lift (normalize : α → α) $ λ a b ⟨u, hu⟩, hu ▸ normalize_eq_normalize ⟨_, rfl⟩ (units.mul_right_dvd.2 $ dvd_refl a) lemma out_mk (a : α) : (associates.mk a).out = normalize a := rfl @[simp] lemma out_one : (1 : associates α).out = 1 := normalize_one lemma out_mul (a b : associates α) : (a * b).out = a.out * b.out := quotient.induction_on₂ a b $ assume a b, by simp only [associates.quotient_mk_eq_mk, out_mk, mk_mul_mk, normalize.map_mul] lemma dvd_out_iff (a : α) (b : associates α) : a ∣ b.out ↔ associates.mk a ≤ b := quotient.induction_on b $ by simp [associates.out_mk, associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd_iff] lemma out_dvd_iff (a : α) (b : associates α) : b.out ∣ a ↔ b ≤ associates.mk a := quotient.induction_on b $ by simp [associates.out_mk, associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd_iff] @[simp] lemma out_top : (⊤ : associates α).out = 0 := normalize_zero @[simp] lemma normalize_out (a : associates α) : normalize a.out = a.out := quotient.induction_on a normalize_idem end associates /-- GCD monoid: a `comm_cancel_monoid_with_zero` with normalization and `gcd` (greatest common divisor) and `lcm` (least common multiple) operations. In this setting `gcd` and `lcm` form a bounded lattice on the associated elements where `gcd` is the infimum, `lcm` is the supremum, `1` is bottom, and `0` is top. The type class focuses on `gcd` and we derive the corresponding `lcm` facts from `gcd`. -/ @[protect_proj] class gcd_monoid (α : Type*) [comm_cancel_monoid_with_zero α] [nontrivial α] extends normalization_monoid α := (gcd : α → α → α) (lcm : α → α → α) (gcd_dvd_left : ∀a b, gcd a b ∣ a) (gcd_dvd_right : ∀a b, gcd a b ∣ b) (dvd_gcd : ∀{a b c}, a ∣ c → a ∣ b → a ∣ gcd c b) (normalize_gcd : ∀a b, normalize (gcd a b) = gcd a b) (gcd_mul_lcm : ∀a b, gcd a b * lcm a b = normalize (a * b)) (lcm_zero_left : ∀a, lcm 0 a = 0) (lcm_zero_right : ∀a, lcm a 0 = 0) export gcd_monoid (gcd lcm gcd_dvd_left gcd_dvd_right dvd_gcd lcm_zero_left lcm_zero_right) attribute [simp] lcm_zero_left lcm_zero_right section gcd_monoid variables [comm_cancel_monoid_with_zero α] [nontrivial α] [gcd_monoid α] @[simp] theorem normalize_gcd : ∀a b:α, normalize (gcd a b) = gcd a b := gcd_monoid.normalize_gcd @[simp] theorem gcd_mul_lcm : ∀a b:α, gcd a b * lcm a b = normalize (a * b) := gcd_monoid.gcd_mul_lcm section gcd theorem dvd_gcd_iff (a b c : α) : a ∣ gcd b c ↔ (a ∣ b ∧ a ∣ c) := iff.intro (assume h, ⟨h.trans (gcd_dvd_left _ _), h.trans (gcd_dvd_right _ _)⟩) (assume ⟨hab, hac⟩, dvd_gcd hab hac) theorem gcd_comm (a b : α) : gcd a b = gcd b a := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _)) (dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _)) theorem gcd_assoc (m n k : α) : gcd (gcd m n) k = gcd m (gcd n k) := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_left m n)) (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_right m n)) (gcd_dvd_right (gcd m n) k))) (dvd_gcd (dvd_gcd (gcd_dvd_left m (gcd n k)) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_left n k))) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_right n k))) instance : is_commutative α gcd := ⟨gcd_comm⟩ instance : is_associative α gcd := ⟨gcd_assoc⟩ theorem gcd_eq_normalize {a b c : α} (habc : gcd a b ∣ c) (hcab : c ∣ gcd a b) : gcd a b = normalize c := normalize_gcd a b ▸ normalize_eq_normalize habc hcab @[simp] theorem gcd_zero_left (a : α) : gcd 0 a = normalize a := gcd_eq_normalize (gcd_dvd_right 0 a) (dvd_gcd (dvd_zero _) (dvd_refl a)) @[simp] theorem gcd_zero_right (a : α) : gcd a 0 = normalize a := gcd_eq_normalize (gcd_dvd_left a 0) (dvd_gcd (dvd_refl a) (dvd_zero _)) @[simp] theorem gcd_eq_zero_iff (a b : α) : gcd a b = 0 ↔ a = 0 ∧ b = 0 := iff.intro (assume h, let ⟨ca, ha⟩ := gcd_dvd_left a b, ⟨cb, hb⟩ := gcd_dvd_right a b in by rw [h, zero_mul] at ha hb; exact ⟨ha, hb⟩) (assume ⟨ha, hb⟩, by rw [ha, hb, gcd_zero_left, normalize_zero]) @[simp] theorem gcd_one_left (a : α) : gcd 1 a = 1 := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_left _ _) (one_dvd _) @[simp] theorem gcd_one_right (a : α) : gcd a 1 = 1 := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_right _ _) (one_dvd _) theorem gcd_dvd_gcd {a b c d: α} (hab : a ∣ b) (hcd : c ∣ d) : gcd a c ∣ gcd b d := dvd_gcd ((gcd_dvd_left _ _).trans hab) ((gcd_dvd_right _ _).trans hcd) @[simp] theorem gcd_same (a : α) : gcd a a = normalize a := gcd_eq_normalize (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) (dvd_refl a)) @[simp] theorem gcd_mul_left (a b c : α) : gcd (a * b) (a * c) = normalize a * gcd b c := classical.by_cases (by rintro rfl; simp only [zero_mul, gcd_zero_left, normalize_zero]) $ assume ha : a ≠ 0, suffices gcd (a * b) (a * c) = normalize (a * gcd b c), by simpa only [normalize.map_mul, normalize_gcd], let ⟨d, eq⟩ := dvd_gcd (dvd_mul_right a b) (dvd_mul_right a c) in gcd_eq_normalize (eq.symm ▸ mul_dvd_mul_left a $ show d ∣ gcd b c, from dvd_gcd ((mul_dvd_mul_iff_left ha).1 $ eq ▸ gcd_dvd_left _ _) ((mul_dvd_mul_iff_left ha).1 $ eq ▸ gcd_dvd_right _ _)) (dvd_gcd (mul_dvd_mul_left a $ gcd_dvd_left _ _) (mul_dvd_mul_left a $ gcd_dvd_right _ _)) @[simp] theorem gcd_mul_right (a b c : α) : gcd (b * a) (c * a) = gcd b c * normalize a := by simp only [mul_comm, gcd_mul_left] theorem gcd_eq_left_iff (a b : α) (h : normalize a = a) : gcd a b = a ↔ a ∣ b := iff.intro (assume eq, eq ▸ gcd_dvd_right _ _) $ assume hab, dvd_antisymm_of_normalize_eq (normalize_gcd _ _) h (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) hab) theorem gcd_eq_right_iff (a b : α) (h : normalize b = b) : gcd a b = b ↔ b ∣ a := by simpa only [gcd_comm a b] using gcd_eq_left_iff b a h theorem gcd_dvd_gcd_mul_left (m n k : α) : gcd m n ∣ gcd (k * m) n := gcd_dvd_gcd (dvd_mul_left _ _) dvd_rfl theorem gcd_dvd_gcd_mul_right (m n k : α) : gcd m n ∣ gcd (m * k) n := gcd_dvd_gcd (dvd_mul_right _ _) dvd_rfl theorem gcd_dvd_gcd_mul_left_right (m n k : α) : gcd m n ∣ gcd m (k * n) := gcd_dvd_gcd dvd_rfl (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right_right (m n k : α) : gcd m n ∣ gcd m (n * k) := gcd_dvd_gcd dvd_rfl (dvd_mul_right _ _) theorem associated.gcd_eq_left {m n : α} (h : associated m n) (k : α) : gcd m k = gcd n k := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (gcd_dvd_gcd h.dvd dvd_rfl) (gcd_dvd_gcd h.symm.dvd dvd_rfl) theorem associated.gcd_eq_right {m n : α} (h : associated m n) (k : α) : gcd k m = gcd k n := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (gcd_dvd_gcd dvd_rfl h.dvd) (gcd_dvd_gcd dvd_rfl h.symm.dvd) lemma dvd_gcd_mul_of_dvd_mul {m n k : α} (H : k ∣ m * n) : k ∣ (gcd k m) * n := begin transitivity gcd k m * normalize n, { rw ← gcd_mul_right, exact dvd_gcd (dvd_mul_right _ _) H }, { apply dvd.intro ↑(norm_unit n)⁻¹, rw [normalize_apply, mul_assoc, mul_assoc, ← units.coe_mul], simp } end lemma dvd_mul_gcd_of_dvd_mul {m n k : α} (H : k ∣ m * n) : k ∣ m * gcd k n := by { rw mul_comm at H ⊢, exact dvd_gcd_mul_of_dvd_mul H } /-- Represent a divisor of `m * n` as a product of a divisor of `m` and a divisor of `n`. Note: In general, this representation is highly non-unique. -/ lemma exists_dvd_and_dvd_of_dvd_mul {m n k : α} (H : k ∣ m * n) : ∃ d₁ (hd₁ : d₁ ∣ m) d₂ (hd₂ : d₂ ∣ n), k = d₁ * d₂ := begin by_cases h0 : gcd k m = 0, { rw gcd_eq_zero_iff at h0, rcases h0 with ⟨rfl, rfl⟩, refine ⟨0, dvd_refl 0, n, dvd_refl n, _⟩, simp }, { obtain ⟨a, ha⟩ := gcd_dvd_left k m, refine ⟨gcd k m, gcd_dvd_right _ _, a, _, ha⟩, suffices h : gcd k m * a ∣ gcd k m * n, { cases h with b hb, use b, rw mul_assoc at hb, apply mul_left_cancel' h0 hb }, rw ← ha, exact dvd_gcd_mul_of_dvd_mul H } end theorem gcd_mul_dvd_mul_gcd (k m n : α) : gcd k (m * n) ∣ gcd k m * gcd k n := begin obtain ⟨m', hm', n', hn', h⟩ := (exists_dvd_and_dvd_of_dvd_mul $ gcd_dvd_right k (m * n)), replace h : gcd k (m * n) = m' * n' := h, rw h, have hm'n' : m' * n' ∣ k := h ▸ gcd_dvd_left _ _, apply mul_dvd_mul, { have hm'k : m' ∣ k := (dvd_mul_right m' n').trans hm'n', exact dvd_gcd hm'k hm' }, { have hn'k : n' ∣ k := (dvd_mul_left n' m').trans hm'n', exact dvd_gcd hn'k hn' } end theorem gcd_pow_right_dvd_pow_gcd {a b : α} {k : ℕ} : gcd a (b ^ k) ∣ (gcd a b) ^ k := begin by_cases hg : gcd a b = 0, { rw gcd_eq_zero_iff at hg, rcases hg with ⟨rfl, rfl⟩, simp }, { induction k with k hk, simp, rw [pow_succ, pow_succ], transitivity gcd a b * gcd a (b ^ k), apply gcd_mul_dvd_mul_gcd a b (b ^ k), refine (mul_dvd_mul_iff_left hg).mpr hk } end theorem gcd_pow_left_dvd_pow_gcd {a b : α} {k : ℕ} : gcd (a ^ k) b ∣ (gcd a b) ^ k := by { rw [gcd_comm, gcd_comm a b], exact gcd_pow_right_dvd_pow_gcd } theorem pow_dvd_of_mul_eq_pow {a b c d₁ d₂ : α} (ha : a ≠ 0) (hab : gcd a b = 1) {k : ℕ} (h : a * b = c ^ k) (hc : c = d₁ * d₂) (hd₁ : d₁ ∣ a) : d₁ ^ k ≠ 0 ∧ d₁ ^ k ∣ a := begin have h1 : gcd (d₁ ^ k) b = 1, { rw ← normalize_gcd (d₁ ^ k) b, rw normalize_eq_one, apply is_unit_of_dvd_one, transitivity (gcd d₁ b) ^ k, { exact gcd_pow_left_dvd_pow_gcd }, { apply is_unit.dvd, apply is_unit.pow, apply is_unit_of_dvd_one, rw ← hab, apply gcd_dvd_gcd hd₁ (dvd_refl b) } }, have h2 : d₁ ^ k ∣ a * b, { use d₂ ^ k, rw [h, hc], exact mul_pow d₁ d₂ k }, rw mul_comm at h2, have h3 : d₁ ^ k ∣ a, { rw [← one_mul a, ← h1], apply dvd_gcd_mul_of_dvd_mul h2 }, have h4 : d₁ ^ k ≠ 0, { intro hdk, rw hdk at h3, apply absurd (zero_dvd_iff.mp h3) ha }, tauto end theorem exists_associated_pow_of_mul_eq_pow {a b c : α} (hab : gcd a b = 1) {k : ℕ} (h : a * b = c ^ k) : ∃ (d : α), associated (d ^ k) a := begin by_cases ha : a = 0, { use 0, rw ha, obtain (rfl | hk) := k.eq_zero_or_pos, { exfalso, revert h, rw [ha, zero_mul, pow_zero], apply zero_ne_one }, { rw zero_pow hk } }, by_cases hb : b = 0, { rw [hb, gcd_zero_right] at hab, use 1, rw one_pow, apply (associated_one_iff_is_unit.mpr (normalize_eq_one.mp hab)).symm }, obtain (rfl | hk) := k.eq_zero_or_pos, { use 1, rw pow_zero at h ⊢, use units.mk_of_mul_eq_one _ _ h, rw [units.coe_mk_of_mul_eq_one, one_mul] }, have hc : c ∣ a * b, { rw h, exact dvd_pow_self _ hk.ne' }, obtain ⟨d₁, hd₁, d₂, hd₂, hc⟩ := exists_dvd_and_dvd_of_dvd_mul hc, use d₁, obtain ⟨h0₁, ⟨a', ha'⟩⟩ := pow_dvd_of_mul_eq_pow ha hab h hc hd₁, rw [mul_comm] at h hc, rw [gcd_comm] at hab, obtain ⟨h0₂, ⟨b', hb'⟩⟩ := pow_dvd_of_mul_eq_pow hb hab h hc hd₂, rw [ha', hb', hc, mul_pow] at h, have h' : a' * b' = 1, { apply (mul_right_inj' h0₁).mp, rw mul_one, apply (mul_right_inj' h0₂).mp, rw ← h, rw [mul_assoc, mul_comm a', ← mul_assoc _ b', ← mul_assoc b', mul_comm b'] }, use units.mk_of_mul_eq_one _ _ h', rw [units.coe_mk_of_mul_eq_one, ha'] end end gcd section lcm lemma lcm_dvd_iff {a b c : α} : lcm a b ∣ c ↔ a ∣ c ∧ b ∣ c := classical.by_cases (assume : a = 0 ∨ b = 0, by rcases this with rfl | rfl; simp only [iff_def, lcm_zero_left, lcm_zero_right, zero_dvd_iff, dvd_zero, eq_self_iff_true, and_true, imp_true_iff] {contextual:=tt}) (assume this : ¬ (a = 0 ∨ b = 0), let ⟨h1, h2⟩ := not_or_distrib.1 this in have h : gcd a b ≠ 0, from λ H, h1 ((gcd_eq_zero_iff _ _).1 H).1, by rw [← mul_dvd_mul_iff_left h, gcd_mul_lcm, normalize_dvd_iff, ← dvd_normalize_iff, normalize.map_mul, normalize_gcd, ← gcd_mul_right, dvd_gcd_iff, mul_comm b c, mul_dvd_mul_iff_left h1, mul_dvd_mul_iff_right h2, and_comm]) lemma dvd_lcm_left (a b : α) : a ∣ lcm a b := (lcm_dvd_iff.1 dvd_rfl).1 lemma dvd_lcm_right (a b : α) : b ∣ lcm a b := (lcm_dvd_iff.1 dvd_rfl).2 lemma lcm_dvd {a b c : α} (hab : a ∣ b) (hcb : c ∣ b) : lcm a c ∣ b := lcm_dvd_iff.2 ⟨hab, hcb⟩ @[simp] theorem lcm_eq_zero_iff (a b : α) : lcm a b = 0 ↔ a = 0 ∨ b = 0 := iff.intro (assume h : lcm a b = 0, have normalize (a * b) = 0, by rw [← gcd_mul_lcm _ _, h, mul_zero], by simpa only [normalize_eq_zero, mul_eq_zero, units.ne_zero, or_false]) (by rintro (rfl | rfl); [apply lcm_zero_left, apply lcm_zero_right]) @[simp] lemma normalize_lcm (a b : α) : normalize (lcm a b) = lcm a b := classical.by_cases (assume : lcm a b = 0, by rw [this, normalize_zero]) $ assume h_lcm : lcm a b ≠ 0, have h1 : gcd a b ≠ 0, from mt (by rw [gcd_eq_zero_iff, lcm_eq_zero_iff]; rintros ⟨rfl, rfl⟩; left; refl) h_lcm, have h2 : normalize (gcd a b * lcm a b) = gcd a b * lcm a b, by rw [gcd_mul_lcm, normalize_idem], by simpa only [normalize.map_mul, normalize_gcd, one_mul, mul_right_inj' h1] using h2 theorem lcm_comm (a b : α) : lcm a b = lcm b a := dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _)) (lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _)) theorem lcm_assoc (m n k : α) : lcm (lcm m n) k = lcm m (lcm n k) := dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd (lcm_dvd (dvd_lcm_left _ _) ((dvd_lcm_left _ _).trans (dvd_lcm_right _ _))) ((dvd_lcm_right _ _).trans (dvd_lcm_right _ _))) (lcm_dvd ((dvd_lcm_left _ _).trans (dvd_lcm_left _ _)) (lcm_dvd ((dvd_lcm_right _ _).trans (dvd_lcm_left _ _)) (dvd_lcm_right _ _))) instance : is_commutative α lcm := ⟨lcm_comm⟩ instance : is_associative α lcm := ⟨lcm_assoc⟩ lemma lcm_eq_normalize {a b c : α} (habc : lcm a b ∣ c) (hcab : c ∣ lcm a b) : lcm a b = normalize c := normalize_lcm a b ▸ normalize_eq_normalize habc hcab theorem lcm_dvd_lcm {a b c d : α} (hab : a ∣ b) (hcd : c ∣ d) : lcm a c ∣ lcm b d := lcm_dvd (hab.trans (dvd_lcm_left _ _)) (hcd.trans (dvd_lcm_right _ _)) @[simp] theorem lcm_units_coe_left (u : units α) (a : α) : lcm ↑u a = normalize a := lcm_eq_normalize (lcm_dvd units.coe_dvd dvd_rfl) (dvd_lcm_right _ _) @[simp] theorem lcm_units_coe_right (a : α) (u : units α) : lcm a ↑u = normalize a := (lcm_comm a u).trans $ lcm_units_coe_left _ _ @[simp] theorem lcm_one_left (a : α) : lcm 1 a = normalize a := lcm_units_coe_left 1 a @[simp] theorem lcm_one_right (a : α) : lcm a 1 = normalize a := lcm_units_coe_right a 1 @[simp] theorem lcm_same (a : α) : lcm a a = normalize a := lcm_eq_normalize (lcm_dvd dvd_rfl dvd_rfl) (dvd_lcm_left _ _) @[simp] theorem lcm_eq_one_iff (a b : α) : lcm a b = 1 ↔ a ∣ 1 ∧ b ∣ 1 := iff.intro (assume eq, eq ▸ ⟨dvd_lcm_left _ _, dvd_lcm_right _ _⟩) (assume ⟨⟨c, hc⟩, ⟨d, hd⟩⟩, show lcm (units.mk_of_mul_eq_one a c hc.symm : α) (units.mk_of_mul_eq_one b d hd.symm) = 1, by rw [lcm_units_coe_left, normalize_coe_units]) @[simp] theorem lcm_mul_left (a b c : α) : lcm (a * b) (a * c) = normalize a * lcm b c := classical.by_cases (by rintro rfl; simp only [zero_mul, lcm_zero_left, normalize_zero]) $ assume ha : a ≠ 0, suffices lcm (a * b) (a * c) = normalize (a * lcm b c), by simpa only [normalize.map_mul, normalize_lcm], have a ∣ lcm (a * b) (a * c), from (dvd_mul_right _ _).trans (dvd_lcm_left _ _), let ⟨d, eq⟩ := this in lcm_eq_normalize (lcm_dvd (mul_dvd_mul_left a (dvd_lcm_left _ _)) (mul_dvd_mul_left a (dvd_lcm_right _ _))) (eq.symm ▸ (mul_dvd_mul_left a $ lcm_dvd ((mul_dvd_mul_iff_left ha).1 $ eq ▸ dvd_lcm_left _ _) ((mul_dvd_mul_iff_left ha).1 $ eq ▸ dvd_lcm_right _ _))) @[simp] theorem lcm_mul_right (a b c : α) : lcm (b * a) (c * a) = lcm b c * normalize a := by simp only [mul_comm, lcm_mul_left] theorem lcm_eq_left_iff (a b : α) (h : normalize a = a) : lcm a b = a ↔ b ∣ a := iff.intro (assume eq, eq ▸ dvd_lcm_right _ _) $ assume hab, dvd_antisymm_of_normalize_eq (normalize_lcm _ _) h (lcm_dvd (dvd_refl a) hab) (dvd_lcm_left _ _) theorem lcm_eq_right_iff (a b : α) (h : normalize b = b) : lcm a b = b ↔ a ∣ b := by simpa only [lcm_comm b a] using lcm_eq_left_iff b a h theorem lcm_dvd_lcm_mul_left (m n k : α) : lcm m n ∣ lcm (k * m) n := lcm_dvd_lcm (dvd_mul_left _ _) dvd_rfl theorem lcm_dvd_lcm_mul_right (m n k : α) : lcm m n ∣ lcm (m * k) n := lcm_dvd_lcm (dvd_mul_right _ _) dvd_rfl theorem lcm_dvd_lcm_mul_left_right (m n k : α) : lcm m n ∣ lcm m (k * n) := lcm_dvd_lcm dvd_rfl (dvd_mul_left _ _) theorem lcm_dvd_lcm_mul_right_right (m n k : α) : lcm m n ∣ lcm m (n * k) := lcm_dvd_lcm dvd_rfl (dvd_mul_right _ _) theorem lcm_eq_of_associated_left {m n : α} (h : associated m n) (k : α) : lcm m k = lcm n k := dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd_lcm h.dvd dvd_rfl) (lcm_dvd_lcm h.symm.dvd dvd_rfl) theorem lcm_eq_of_associated_right {m n : α} (h : associated m n) (k : α) : lcm k m = lcm k n := dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd_lcm dvd_rfl h.dvd) (lcm_dvd_lcm dvd_rfl h.symm.dvd) end lcm namespace gcd_monoid theorem prime_of_irreducible {x : α} (hi: irreducible x) : prime x := ⟨hi.ne_zero, ⟨hi.1, λ a b h, begin cases gcd_dvd_left x a with y hy, cases hi.is_unit_or_is_unit hy with hu hu; cases hu with u hu, { right, transitivity (gcd (x * b) (a * b)), apply dvd_gcd (dvd_mul_right x b) h, rw gcd_mul_right, rw ← hu, apply associated.dvd, transitivity (normalize b), symmetry, use u, apply mul_comm, apply normalize_associated, }, { left, rw [hy, ← hu], transitivity, { apply associated.dvd, symmetry, use u }, apply gcd_dvd_right, } end ⟩⟩ theorem irreducible_iff_prime {p : α} : irreducible p ↔ prime p := ⟨prime_of_irreducible, prime.irreducible⟩ end gcd_monoid end gcd_monoid section unique_unit variables [comm_cancel_monoid_with_zero α] [unique (units α)] lemma units_eq_one (u : units α) : u = 1 := subsingleton.elim u 1 variable [nontrivial α] @[priority 100] -- see Note [lower instance priority] instance normalization_monoid_of_unique_units : normalization_monoid α := { norm_unit := λ x, 1, norm_unit_zero := rfl, norm_unit_mul := λ x y hx hy, (mul_one 1).symm, norm_unit_coe_units := λ u, subsingleton.elim _ _ } @[simp] lemma norm_unit_eq_one (x : α) : norm_unit x = 1 := rfl @[simp] lemma normalize_eq (x : α) : normalize x = x := mul_one x end unique_unit section integral_domain variables [integral_domain α] [gcd_monoid α] lemma gcd_eq_of_dvd_sub_right {a b c : α} (h : a ∣ b - c) : gcd a b = gcd a c := begin apply dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _); rw dvd_gcd_iff; refine ⟨gcd_dvd_left _ _, _⟩, { rcases h with ⟨d, hd⟩, rcases gcd_dvd_right a b with ⟨e, he⟩, rcases gcd_dvd_left a b with ⟨f, hf⟩, use e - f * d, rw [mul_sub, ← he, ← mul_assoc, ← hf, ← hd, sub_sub_cancel] }, { rcases h with ⟨d, hd⟩, rcases gcd_dvd_right a c with ⟨e, he⟩, rcases gcd_dvd_left a c with ⟨f, hf⟩, use e + f * d, rw [mul_add, ← he, ← mul_assoc, ← hf, ← hd, ← add_sub_assoc, add_comm c b, add_sub_cancel] } end lemma gcd_eq_of_dvd_sub_left {a b c : α} (h : a ∣ b - c) : gcd b a = gcd c a := by rw [gcd_comm _ a, gcd_comm _ a, gcd_eq_of_dvd_sub_right h] end integral_domain section constructors noncomputable theory open associates variables [comm_cancel_monoid_with_zero α] [nontrivial α] private lemma map_mk_unit_aux [decidable_eq α] {f : associates α →* α} (hinv : function.right_inverse f associates.mk) (a : α) : a * ↑(classical.some (associated_map_mk hinv a)) = f (associates.mk a) := classical.some_spec (associated_map_mk hinv a) /-- Define `normalization_monoid` on a structure from a `monoid_hom` inverse to `associates.mk`. -/ def normalization_monoid_of_monoid_hom_right_inverse [decidable_eq α] (f : associates α →* α) (hinv : function.right_inverse f associates.mk) : normalization_monoid α := { norm_unit := λ a, if a = 0 then 1 else classical.some (associates.mk_eq_mk_iff_associated.1 (hinv (associates.mk a)).symm), norm_unit_zero := if_pos rfl, norm_unit_mul := λ a b ha hb, by { rw [if_neg (mul_ne_zero ha hb), if_neg ha, if_neg hb, units.ext_iff, units.coe_mul], suffices : (a * b) * ↑(classical.some (associated_map_mk hinv (a * b))) = (a * ↑(classical.some (associated_map_mk hinv a))) * (b * ↑(classical.some (associated_map_mk hinv b))), { apply mul_left_cancel' (mul_ne_zero ha hb) _, simpa only [mul_assoc, mul_comm, mul_left_comm] using this }, rw [map_mk_unit_aux hinv a, map_mk_unit_aux hinv (a * b), map_mk_unit_aux hinv b, ← monoid_hom.map_mul, associates.mk_mul_mk] }, norm_unit_coe_units := λ u, by { rw [if_neg (units.ne_zero u), units.ext_iff], apply mul_left_cancel' (units.ne_zero u), rw [units.mul_inv, map_mk_unit_aux hinv u, associates.mk_eq_mk_iff_associated.2 (associated_one_iff_is_unit.2 ⟨u, rfl⟩), associates.mk_one, monoid_hom.map_one] } } variable [normalization_monoid α] /-- Define `gcd_monoid` on a structure just from the `gcd` and its properties. -/ noncomputable def gcd_monoid_of_gcd [decidable_eq α] (gcd : α → α → α) (gcd_dvd_left : ∀a b, gcd a b ∣ a) (gcd_dvd_right : ∀a b, gcd a b ∣ b) (dvd_gcd : ∀{a b c}, a ∣ c → a ∣ b → a ∣ gcd c b) (normalize_gcd : ∀a b, normalize (gcd a b) = gcd a b) : gcd_monoid α := { gcd := gcd, gcd_dvd_left := gcd_dvd_left, gcd_dvd_right := gcd_dvd_right, dvd_gcd := λ a b c, dvd_gcd, normalize_gcd := normalize_gcd, lcm := λ a b, if a = 0 then 0 else classical.some (dvd_normalize_iff.2 ((gcd_dvd_left a b).trans (dvd.intro b rfl))), gcd_mul_lcm := λ a b, by { split_ifs with a0, { rw [mul_zero, a0, zero_mul, normalize_zero] }, { exact (classical.some_spec (dvd_normalize_iff.2 ((gcd_dvd_left a b).trans (dvd.intro b rfl)))).symm } }, lcm_zero_left := λ a, if_pos rfl, lcm_zero_right := λ a, by { split_ifs with a0, { refl }, rw ← normalize_eq_zero at a0, have h := (classical.some_spec (dvd_normalize_iff.2 ((gcd_dvd_left a 0).trans (dvd.intro 0 rfl)))).symm, have gcd0 : gcd a 0 = normalize a, { rw ← normalize_gcd, exact normalize_eq_normalize (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) (dvd_zero a)) }, rw ← gcd0 at a0, apply or.resolve_left (mul_eq_zero.1 _) a0, rw [h, mul_zero, normalize_zero] }, .. (infer_instance : normalization_monoid α) } /-- Define `gcd_monoid` on a structure just from the `lcm` and its properties. -/ noncomputable def gcd_monoid_of_lcm [decidable_eq α] (lcm : α → α → α) (dvd_lcm_left : ∀a b, a ∣ lcm a b) (dvd_lcm_right : ∀a b, b ∣ lcm a b) (lcm_dvd : ∀{a b c}, c ∣ a → b ∣ a → lcm c b ∣ a) (normalize_lcm : ∀a b, normalize (lcm a b) = lcm a b) : gcd_monoid α := let exists_gcd := λ a b, dvd_normalize_iff.2 (lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl)) in { lcm := lcm, gcd := λ a b, if a = 0 then normalize b else (if b = 0 then normalize a else classical.some (exists_gcd a b)), gcd_mul_lcm := λ a b, by { split_ifs, { rw [h, zero_dvd_iff.1 (dvd_lcm_left _ _), mul_zero, zero_mul, normalize_zero] }, { rw [h_1, zero_dvd_iff.1 (dvd_lcm_right _ _), mul_zero, mul_zero, normalize_zero] }, apply eq.trans (mul_comm _ _) (classical.some_spec (dvd_normalize_iff.2 (lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl)))).symm }, normalize_gcd := λ a b, by { split_ifs, { apply normalize_idem }, { apply normalize_idem }, have h0 : lcm a b ≠ 0, { intro con, have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl), rw [con, zero_dvd_iff, mul_eq_zero] at h, cases h; tauto }, apply mul_left_cancel' h0, refine trans _ (classical.some_spec (exists_gcd a b)), conv_lhs { congr, rw [← normalize_lcm a b] }, erw [← normalize.map_mul, ← classical.some_spec (exists_gcd a b), normalize_idem] }, lcm_zero_left := λ a, zero_dvd_iff.1 (dvd_lcm_left _ _), lcm_zero_right := λ a, zero_dvd_iff.1 (dvd_lcm_right _ _), gcd_dvd_left := λ a b, by { split_ifs, { rw h, apply dvd_zero }, { exact (normalize_associated _).dvd }, have h0 : lcm a b ≠ 0, { intro con, have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl), rw [con, zero_dvd_iff, mul_eq_zero] at h, cases h; tauto }, rw [← mul_dvd_mul_iff_left h0, ← classical.some_spec (exists_gcd a b), normalize_dvd_iff, mul_comm, mul_dvd_mul_iff_right h], apply dvd_lcm_right }, gcd_dvd_right := λ a b, by { split_ifs, { exact (normalize_associated _).dvd }, { rw h_1, apply dvd_zero }, have h0 : lcm a b ≠ 0, { intro con, have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl), rw [con, zero_dvd_iff, mul_eq_zero] at h, cases h; tauto }, rw [← mul_dvd_mul_iff_left h0, ← classical.some_spec (exists_gcd a b), normalize_dvd_iff, mul_dvd_mul_iff_right h_1], apply dvd_lcm_left }, dvd_gcd := λ a b c ac ab, by { split_ifs, { apply dvd_normalize_iff.2 ab }, { apply dvd_normalize_iff.2 ac }, have h0 : lcm c b ≠ 0, { intro con, have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left c rfl), rw [con, zero_dvd_iff, mul_eq_zero] at h, cases h; tauto }, rw [← mul_dvd_mul_iff_left h0, ← classical.some_spec (dvd_normalize_iff.2 (lcm_dvd (dvd.intro b rfl) (dvd.intro_left c rfl))), dvd_normalize_iff], rcases ab with ⟨d, rfl⟩, rw mul_eq_zero at h_1, push_neg at h_1, rw [mul_comm a, ← mul_assoc, mul_dvd_mul_iff_right h_1.1], apply lcm_dvd (dvd.intro d rfl), rw [mul_comm, mul_dvd_mul_iff_right h_1.2], apply ac }, .. (infer_instance : normalization_monoid α) } /-- Define a `gcd_monoid` structure on a monoid just from the existence of a `gcd`. -/ noncomputable def gcd_monoid_of_exists_gcd [decidable_eq α] (h : ∀ a b : α, ∃ c : α, ∀ d : α, d ∣ a ∧ d ∣ b ↔ d ∣ c) : gcd_monoid α := gcd_monoid_of_gcd (λ a b, normalize (classical.some (h a b))) (λ a b, normalize_dvd_iff.2 (((classical.some_spec (h a b) (classical.some (h a b))).2 dvd_rfl)).1) (λ a b, normalize_dvd_iff.2 (((classical.some_spec (h a b) (classical.some (h a b))).2 dvd_rfl)).2) (λ a b c ac ab, dvd_normalize_iff.2 ((classical.some_spec (h c b) a).1 ⟨ac, ab⟩)) (λ a b, normalize_idem _) /-- Define a `gcd_monoid` structure on a monoid just from the existence of an `lcm`. -/ noncomputable def gcd_monoid_of_exists_lcm [decidable_eq α] (h : ∀ a b : α, ∃ c : α, ∀ d : α, a ∣ d ∧ b ∣ d ↔ c ∣ d) : gcd_monoid α := gcd_monoid_of_lcm (λ a b, normalize (classical.some (h a b))) (λ a b, dvd_normalize_iff.2 (((classical.some_spec (h a b) (classical.some (h a b))).2 dvd_rfl)).1) (λ a b, dvd_normalize_iff.2 (((classical.some_spec (h a b) (classical.some (h a b))).2 dvd_rfl)).2) (λ a b c ac ab, normalize_dvd_iff.2 ((classical.some_spec (h c b) a).1 ⟨ac, ab⟩)) (λ a b, normalize_idem _) end constructors
b9c7c7b65aea9a1353a981390f6457528dd5efb3
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/star/self_adjoint.lean
146c294221606f2bc6ec92d08aa0b94afa6a783a
[ "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
16,508
lean
/- Copyright (c) 2021 Frédéric Dupuis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Frédéric Dupuis -/ import algebra.star.basic import group_theory.subgroup.basic /-! # Self-adjoint, skew-adjoint and normal elements of a star additive group > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines `self_adjoint R` (resp. `skew_adjoint R`), where `R` is a star additive group, as the additive subgroup containing the elements that satisfy `star x = x` (resp. `star x = -x`). This includes, for instance, (skew-)Hermitian operators on Hilbert spaces. We also define `is_star_normal R`, a `Prop` that states that an element `x` satisfies `star x * x = x * star x`. ## Implementation notes * When `R` is a `star_module R₂ R`, then `self_adjoint R` has a natural `module (self_adjoint R₂) (self_adjoint R)` structure. However, doing this literally would be undesirable since in the main case of interest (`R₂ = ℂ`) we want `module ℝ (self_adjoint R)` and not `module (self_adjoint ℂ) (self_adjoint R)`. We solve this issue by adding the typeclass `[has_trivial_star R₃]`, of which `ℝ` is an instance (registered in `data/real/basic`), and then add a `[module R₃ (self_adjoint R)]` instance whenever we have `[module R₃ R] [has_trivial_star R₃]`. (Another approach would have been to define `[star_invariant_scalars R₃ R]` to express the fact that `star (x • v) = x • star v`, but this typeclass would have the disadvantage of taking two type arguments.) ## TODO * Define `is_skew_adjoint` to match `is_self_adjoint`. * Define `λ z x, z * x * star z` (i.e. conjugation by `z`) as a monoid action of `R` on `R` (similar to the existing `conj_act` for groups), and then state the fact that `self_adjoint R` is invariant under it. -/ variables {R A : Type*} /-- An element is self-adjoint if it is equal to its star. -/ def is_self_adjoint [has_star R] (x : R) : Prop := star x = x /-- An element of a star monoid is normal if it commutes with its adjoint. -/ class is_star_normal [has_mul R] [has_star R] (x : R) : Prop := (star_comm_self : commute (star x) x) export is_star_normal (star_comm_self) lemma star_comm_self' [has_mul R] [has_star R] (x : R) [is_star_normal x] : (star x) * x = x * star x := is_star_normal.star_comm_self namespace is_self_adjoint -- named to match `commute.all` /-- All elements are self-adjoint when `star` is trivial. -/ lemma all [has_star R] [has_trivial_star R] (r : R) : is_self_adjoint r := star_trivial _ lemma star_eq [has_star R] {x : R} (hx : is_self_adjoint x) : star x = x := hx lemma _root_.is_self_adjoint_iff [has_star R] {x : R} : is_self_adjoint x ↔ star x = x := iff.rfl @[simp] lemma star_iff [has_involutive_star R] {x : R} : is_self_adjoint (star x) ↔ is_self_adjoint x := by simpa only [is_self_adjoint, star_star] using eq_comm @[simp] lemma star_mul_self [semigroup R] [star_semigroup R] (x : R) : is_self_adjoint (star x * x) := by simp only [is_self_adjoint, star_mul, star_star] @[simp] lemma mul_star_self [semigroup R] [star_semigroup R] (x : R) : is_self_adjoint (x * star x) := by simpa only [star_star] using star_mul_self (star x) /-- Functions in a `star_hom_class` preserve self-adjoint elements. -/ lemma star_hom_apply {F R S : Type*} [has_star R] [has_star S] [star_hom_class F R S] {x : R} (hx : is_self_adjoint x) (f : F) : is_self_adjoint (f x) := show star (f x) = f x, from map_star f x ▸ congr_arg f hx section add_monoid variables [add_monoid R] [star_add_monoid R] variables (R) lemma _root_.is_self_adjoint_zero : is_self_adjoint (0 : R) := star_zero R variables {R} lemma add {x y : R} (hx : is_self_adjoint x) (hy : is_self_adjoint y) : is_self_adjoint (x + y) := by simp only [is_self_adjoint_iff, star_add, hx.star_eq, hy.star_eq] lemma bit0 {x : R} (hx : is_self_adjoint x) : is_self_adjoint (bit0 x) := by simp only [is_self_adjoint_iff, star_bit0, hx.star_eq] end add_monoid section add_group variables [add_group R] [star_add_monoid R] lemma neg {x : R} (hx : is_self_adjoint x) : is_self_adjoint (-x) := by simp only [is_self_adjoint_iff, star_neg, hx.star_eq] lemma sub {x y : R} (hx : is_self_adjoint x) (hy : is_self_adjoint y) : is_self_adjoint (x - y) := by simp only [is_self_adjoint_iff, star_sub, hx.star_eq, hy.star_eq] end add_group section add_comm_monoid variables [add_comm_monoid R] [star_add_monoid R] lemma _root_.is_self_adjoint_add_star_self (x : R) : is_self_adjoint (x + star x) := by simp only [is_self_adjoint_iff, add_comm, star_add, star_star] lemma _root_.is_self_adjoint_star_add_self (x : R) : is_self_adjoint (star x + x) := by simp only [is_self_adjoint_iff, add_comm, star_add, star_star] end add_comm_monoid section semigroup variables [semigroup R] [star_semigroup R] lemma conjugate {x : R} (hx : is_self_adjoint x) (z : R) : is_self_adjoint (z * x * star z) := by simp only [is_self_adjoint_iff, star_mul, star_star, mul_assoc, hx.star_eq] lemma conjugate' {x : R} (hx : is_self_adjoint x) (z : R) : is_self_adjoint (star z * x * z) := by simp only [is_self_adjoint_iff, star_mul, star_star, mul_assoc, hx.star_eq] lemma is_star_normal {x : R} (hx : is_self_adjoint x) : is_star_normal x := ⟨by simp only [hx.star_eq]⟩ end semigroup section monoid variables [monoid R] [star_semigroup R] variables (R) lemma _root_.is_self_adjoint_one : is_self_adjoint (1 : R) := star_one R variables {R} lemma pow {x : R} (hx : is_self_adjoint x) (n : ℕ) : is_self_adjoint (x ^ n):= by simp only [is_self_adjoint_iff, star_pow, hx.star_eq] end monoid section semiring variables [semiring R] [star_ring R] lemma bit1 {x : R} (hx : is_self_adjoint x) : is_self_adjoint (bit1 x) := by simp only [is_self_adjoint_iff, star_bit1, hx.star_eq] @[simp] lemma _root_.is_self_adjoint_nat_cast (n : ℕ) : is_self_adjoint (n : R) := star_nat_cast _ end semiring section comm_semigroup variables [comm_semigroup R] [star_semigroup R] lemma mul {x y : R} (hx : is_self_adjoint x) (hy : is_self_adjoint y) : is_self_adjoint (x * y) := by simp only [is_self_adjoint_iff, star_mul', hx.star_eq, hy.star_eq] end comm_semigroup section ring variables [ring R] [star_ring R] @[simp] lemma _root_.is_self_adjoint_int_cast (z : ℤ) : is_self_adjoint (z : R) := star_int_cast _ end ring section division_semiring variables [division_semiring R] [star_ring R] lemma inv {x : R} (hx : is_self_adjoint x) : is_self_adjoint x⁻¹ := by simp only [is_self_adjoint_iff, star_inv', hx.star_eq] lemma zpow {x : R} (hx : is_self_adjoint x) (n : ℤ) : is_self_adjoint (x ^ n):= by simp only [is_self_adjoint_iff, star_zpow₀, hx.star_eq] end division_semiring section division_ring variables [division_ring R] [star_ring R] lemma _root_.is_self_adjoint_rat_cast (x : ℚ) : is_self_adjoint (x : R) := star_rat_cast _ end division_ring section semifield variables [semifield R] [star_ring R] lemma div {x y : R} (hx : is_self_adjoint x) (hy : is_self_adjoint y) : is_self_adjoint (x / y) := by simp only [is_self_adjoint_iff, star_div', hx.star_eq, hy.star_eq] end semifield section has_smul variables [has_star R] [add_monoid A] [star_add_monoid A] [has_smul R A] [star_module R A] lemma smul {r : R} (hr : is_self_adjoint r) {x : A} (hx : is_self_adjoint x) : is_self_adjoint (r • x) := by simp only [is_self_adjoint_iff, star_smul, hr.star_eq, hx.star_eq] end has_smul end is_self_adjoint variables (R) /-- The self-adjoint elements of a star additive group, as an additive subgroup. -/ def self_adjoint [add_group R] [star_add_monoid R] : add_subgroup R := { carrier := {x | is_self_adjoint x}, zero_mem' := star_zero R, add_mem' := λ _ _ hx, hx.add, neg_mem' := λ _ hx, hx.neg } /-- The skew-adjoint elements of a star additive group, as an additive subgroup. -/ def skew_adjoint [add_comm_group R] [star_add_monoid R] : add_subgroup R := { carrier := {x | star x = -x}, zero_mem' := show star (0 : R) = -0, by simp only [star_zero, neg_zero], add_mem' := λ x y (hx : star x = -x) (hy : star y = -y), show star (x + y) = -(x + y), by rw [star_add x y, hx, hy, neg_add], neg_mem' := λ x (hx : star x = -x), show star (-x) = (- -x), by simp only [hx, star_neg] } variables {R} namespace self_adjoint section add_group variables [add_group R] [star_add_monoid R] lemma mem_iff {x : R} : x ∈ self_adjoint R ↔ star x = x := by { rw [←add_subgroup.mem_carrier], exact iff.rfl } @[simp, norm_cast] lemma star_coe_eq {x : self_adjoint R} : star (x : R) = x := x.prop instance : inhabited (self_adjoint R) := ⟨0⟩ end add_group section ring variables [ring R] [star_ring R] instance : has_one (self_adjoint R) := ⟨⟨1, is_self_adjoint_one R⟩⟩ @[simp, norm_cast] lemma coe_one : ↑(1 : self_adjoint R) = (1 : R) := rfl instance [nontrivial R] : nontrivial (self_adjoint R) := ⟨⟨0, 1, subtype.ne_of_val_ne zero_ne_one⟩⟩ instance : has_nat_cast (self_adjoint R) := ⟨λ n, ⟨n, is_self_adjoint_nat_cast _⟩⟩ instance : has_int_cast (self_adjoint R) := ⟨λ n, ⟨n, is_self_adjoint_int_cast _⟩ ⟩ instance : has_pow (self_adjoint R) ℕ := ⟨λ x n, ⟨(x : R) ^ n, x.prop.pow n⟩⟩ @[simp, norm_cast] lemma coe_pow (x : self_adjoint R) (n : ℕ) : ↑(x ^ n) = (x : R) ^ n := rfl end ring section non_unital_comm_ring variables [non_unital_comm_ring R] [star_ring R] instance : has_mul (self_adjoint R) := ⟨λ x y, ⟨(x : R) * y, x.prop.mul y.prop⟩⟩ @[simp, norm_cast] lemma coe_mul (x y : self_adjoint R) : ↑(x * y) = (x : R) * y := rfl end non_unital_comm_ring section comm_ring variables [comm_ring R] [star_ring R] instance : comm_ring (self_adjoint R) := function.injective.comm_ring _ subtype.coe_injective (self_adjoint R).coe_zero coe_one (self_adjoint R).coe_add coe_mul (self_adjoint R).coe_neg (self_adjoint R).coe_sub (self_adjoint R).coe_nsmul (self_adjoint R).coe_zsmul coe_pow (λ _, rfl) (λ _, rfl) end comm_ring section field variables [field R] [star_ring R] instance : has_inv (self_adjoint R) := { inv := λ x, ⟨(x.val)⁻¹, x.prop.inv⟩ } @[simp, norm_cast] lemma coe_inv (x : self_adjoint R) : ↑(x⁻¹) = (x : R)⁻¹ := rfl instance : has_div (self_adjoint R) := { div := λ x y, ⟨x / y, x.prop.div y.prop⟩ } @[simp, norm_cast] lemma coe_div (x y : self_adjoint R) : ↑(x / y) = (x / y : R) := rfl instance : has_pow (self_adjoint R) ℤ := { pow := λ x z, ⟨x ^ z, x.prop.zpow z⟩ } @[simp, norm_cast] lemma coe_zpow (x : self_adjoint R) (z : ℤ) : ↑(x ^ z) = (x : R) ^ z := rfl instance : has_rat_cast (self_adjoint R) := ⟨λ n, ⟨n, is_self_adjoint_rat_cast n⟩⟩ @[simp, norm_cast] lemma coe_rat_cast (x : ℚ) : ↑(x : self_adjoint R) = (x : R) := rfl instance has_qsmul : has_smul ℚ (self_adjoint R) := ⟨λ a x, ⟨a • x, by rw rat.smul_def; exact is_self_adjoint.mul (is_self_adjoint_rat_cast a) x.prop⟩⟩ @[simp, norm_cast] lemma coe_rat_smul (x : self_adjoint R) (a : ℚ) : ↑(a • x) = a • (x : R) := rfl instance : field (self_adjoint R) := function.injective.field _ subtype.coe_injective (self_adjoint R).coe_zero coe_one (self_adjoint R).coe_add coe_mul (self_adjoint R).coe_neg (self_adjoint R).coe_sub coe_inv coe_div (self_adjoint R).coe_nsmul (self_adjoint R).coe_zsmul coe_rat_smul coe_pow coe_zpow (λ _, rfl) (λ _, rfl) coe_rat_cast end field section has_smul variables [has_star R] [has_trivial_star R] [add_group A] [star_add_monoid A] instance [has_smul R A] [star_module R A] : has_smul R (self_adjoint A) := ⟨λ r x, ⟨r • x, (is_self_adjoint.all _).smul x.prop⟩⟩ @[simp, norm_cast] lemma coe_smul [has_smul R A] [star_module R A] (r : R) (x : self_adjoint A) : ↑(r • x) = r • (x : A) := rfl instance [monoid R] [mul_action R A] [star_module R A] : mul_action R (self_adjoint A) := function.injective.mul_action coe subtype.coe_injective coe_smul instance [monoid R] [distrib_mul_action R A] [star_module R A] : distrib_mul_action R (self_adjoint A) := function.injective.distrib_mul_action (self_adjoint A).subtype subtype.coe_injective coe_smul end has_smul section module variables [has_star R] [has_trivial_star R] [add_comm_group A] [star_add_monoid A] instance [semiring R] [module R A] [star_module R A] : module R (self_adjoint A) := function.injective.module R (self_adjoint A).subtype subtype.coe_injective coe_smul end module end self_adjoint namespace skew_adjoint section add_group variables [add_comm_group R] [star_add_monoid R] lemma mem_iff {x : R} : x ∈ skew_adjoint R ↔ star x = -x := by { rw [←add_subgroup.mem_carrier], exact iff.rfl } @[simp, norm_cast] lemma star_coe_eq {x : skew_adjoint R} : star (x : R) = -x := x.prop instance : inhabited (skew_adjoint R) := ⟨0⟩ lemma bit0_mem {x : R} (hx : x ∈ skew_adjoint R) : bit0 x ∈ skew_adjoint R := by rw [mem_iff, star_bit0, mem_iff.mp hx, bit0, bit0, neg_add] end add_group section ring variables [ring R] [star_ring R] lemma conjugate {x : R} (hx : x ∈ skew_adjoint R) (z : R) : z * x * star z ∈ skew_adjoint R := by simp only [mem_iff, star_mul, star_star, mem_iff.mp hx, neg_mul, mul_neg, mul_assoc] lemma conjugate' {x : R} (hx : x ∈ skew_adjoint R) (z : R) : star z * x * z ∈ skew_adjoint R := by simp only [mem_iff, star_mul, star_star, mem_iff.mp hx, neg_mul, mul_neg, mul_assoc] lemma is_star_normal_of_mem {x : R} (hx : x ∈ skew_adjoint R) : is_star_normal x := ⟨by { simp only [mem_iff] at hx, simp only [hx, commute.neg_left] }⟩ instance (x : skew_adjoint R) : is_star_normal (x : R) := is_star_normal_of_mem (set_like.coe_mem _) end ring section has_smul variables [has_star R] [has_trivial_star R] [add_comm_group A] [star_add_monoid A] lemma smul_mem [monoid R] [distrib_mul_action R A] [star_module R A] (r : R) {x : A} (h : x ∈ skew_adjoint A) : r • x ∈ skew_adjoint A := by rw [mem_iff, star_smul, star_trivial, mem_iff.mp h, smul_neg r] instance [monoid R] [distrib_mul_action R A] [star_module R A] : has_smul R (skew_adjoint A) := ⟨λ r x, ⟨r • x, smul_mem r x.prop⟩⟩ @[simp, norm_cast] lemma coe_smul [monoid R] [distrib_mul_action R A] [star_module R A] (r : R) (x : skew_adjoint A) : ↑(r • x) = r • (x : A) := rfl instance [monoid R] [distrib_mul_action R A] [star_module R A] : distrib_mul_action R (skew_adjoint A) := function.injective.distrib_mul_action (skew_adjoint A).subtype subtype.coe_injective coe_smul instance [semiring R] [module R A] [star_module R A] : module R (skew_adjoint A) := function.injective.module R (skew_adjoint A).subtype subtype.coe_injective coe_smul end has_smul end skew_adjoint /-- Scalar multiplication of a self-adjoint element by a skew-adjoint element produces a skew-adjoint element. -/ lemma is_self_adjoint.smul_mem_skew_adjoint [ring R] [add_comm_group A] [module R A] [star_add_monoid R] [star_add_monoid A] [star_module R A] {r : R} (hr : r ∈ skew_adjoint R) {a : A} (ha : is_self_adjoint a) : r • a ∈ skew_adjoint A := (star_smul _ _).trans $ (congr_arg2 _ hr ha).trans $ neg_smul _ _ /-- Scalar multiplication of a skew-adjoint element by a skew-adjoint element produces a self-adjoint element. -/ lemma is_self_adjoint_smul_of_mem_skew_adjoint [ring R] [add_comm_group A] [module R A] [star_add_monoid R] [star_add_monoid A] [star_module R A] {r : R} (hr : r ∈ skew_adjoint R) {a : A} (ha : a ∈ skew_adjoint A) : is_self_adjoint (r • a) := (star_smul _ _).trans $ (congr_arg2 _ hr ha).trans $ neg_smul_neg _ _ instance is_star_normal_zero [semiring R] [star_ring R] : is_star_normal (0 : R) := ⟨by simp only [star_comm_self, star_zero]⟩ instance is_star_normal_one [monoid R] [star_semigroup R] : is_star_normal (1 : R) := ⟨by simp only [star_comm_self, star_one]⟩ instance is_star_normal_star_self [monoid R] [star_semigroup R] {x : R} [is_star_normal x] : is_star_normal (star x) := ⟨show star (star x) * (star x) = (star x) * star (star x), by rw [star_star, star_comm_self']⟩ @[priority 100] -- see Note [lower instance priority] instance has_trivial_star.is_star_normal [monoid R] [star_semigroup R] [has_trivial_star R] {x : R} : is_star_normal x := ⟨by rw [star_trivial]⟩ @[priority 100] -- see Note [lower instance priority] instance comm_monoid.is_star_normal [comm_monoid R] [star_semigroup R] {x : R} : is_star_normal x := ⟨mul_comm _ _⟩
9aad6a6a18bf2cbf986d8c72bb9f14e3de9de36e
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/order/order_iso.lean
a2fa9e5d974e9f412494235399d87d4dd35563d1
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
13,954
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import order.basic logic.embedding data.nat.basic open function universes u v w variables {α : Type*} {β : Type*} {γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} /-- An increasing function is injective -/ lemma injective_of_increasing (r : α → α → Prop) (s : β → β → Prop) [is_trichotomous α r] [is_irrefl β s] (f : α → β) (hf : ∀{x y}, r x y → s (f x) (f y)) : injective f := begin intros x y hxy, rcases trichotomous_of r x y with h | h | h, have := hf h, rw hxy at this, exfalso, exact irrefl_of s (f y) this, exact h, have := hf h, rw hxy at this, exfalso, exact irrefl_of s (f y) this end /-- An order embedding with respect to a given pair of orders `r` and `s` is an embedding `f : α ↪ β` such that `r a b ↔ s (f a) (f b)`. -/ structure order_embedding {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ↪ β := (ord : ∀ {a b}, r a b ↔ s (to_embedding a) (to_embedding b)) infix ` ≼o `:25 := order_embedding /-- the induced order on a subtype is an embedding under the natural inclusion. -/ definition subtype.order_embedding {X : Type*} (r : X → X → Prop) (p : X → Prop) : ((subtype.val : subtype p → X) ⁻¹'o r) ≼o r := ⟨⟨subtype.val,subtype.val_injective⟩,by intros;refl⟩ theorem preimage_equivalence {α β} (f : α → β) {s : β → β → Prop} (hs : equivalence s) : equivalence (f ⁻¹'o s) := ⟨λ a, hs.1 _, λ a b h, hs.2.1 h, λ a b c h₁ h₂, hs.2.2 h₁ h₂⟩ namespace order_embedding instance : has_coe_to_fun (r ≼o s) := ⟨λ _, α → β, λ o, o.to_embedding⟩ theorem ord' : ∀ (f : r ≼o s) {a b}, r a b ↔ s (f a) (f b) | ⟨f, o⟩ := @o @[simp] theorem coe_fn_mk (f : α ↪ β) (o) : (@order_embedding.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_embedding (f : r ≼o s) : (f.to_embedding : α → β) = f := rfl theorem eq_of_to_fun_eq : ∀ {e₁ e₂ : r ≼o s}, (e₁ : α → β) = e₂ → e₁ = e₂ | ⟨⟨f₁, h₁⟩, o₁⟩ ⟨⟨f₂, h₂⟩, o₂⟩ h := by congr; exact h @[refl] protected def refl (r : α → α → Prop) : r ≼o r := ⟨embedding.refl _, λ a b, iff.rfl⟩ @[trans] protected def trans (f : r ≼o s) (g : s ≼o t) : r ≼o t := ⟨f.1.trans g.1, λ a b, by rw [f.2, g.2]; simp⟩ @[simp] theorem refl_apply (x : α) : order_embedding.refl r x = x := rfl @[simp] theorem trans_apply (f : r ≼o s) (g : s ≼o t) (a : α) : (f.trans g) a = g (f a) := rfl /-- An order embedding is also an order embedding between dual orders. -/ def rsymm (f : r ≼o s) : swap r ≼o swap s := ⟨f.to_embedding, λ a b, f.ord'⟩ /-- If `f` is injective, then it is an order embedding from the preimage order of `s` to `s`. -/ def preimage (f : α ↪ β) (s : β → β → Prop) : f ⁻¹'o s ≼o s := ⟨f, λ a b, iff.rfl⟩ theorem eq_preimage (f : r ≼o s) : r = f ⁻¹'o s := by funext a b; exact propext f.ord' protected theorem is_irrefl : ∀ (f : r ≼o s) [is_irrefl β s], is_irrefl α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a h, H _ (o.1 h)⟩ protected theorem is_refl : ∀ (f : r ≼o s) [is_refl β s], is_refl α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a, o.2 (H _)⟩ protected theorem is_symm : ∀ (f : r ≼o s) [is_symm β s], is_symm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h, o.2 (H _ _ (o.1 h))⟩ protected theorem is_asymm : ∀ (f : r ≼o s) [is_asymm β s], is_asymm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, H _ _ (o.1 h₁) (o.1 h₂)⟩ protected theorem is_antisymm : ∀ (f : r ≼o s) [is_antisymm β s], is_antisymm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, f.inj' (H _ _ (o.1 h₁) (o.1 h₂))⟩ protected theorem is_trans : ∀ (f : r ≼o s) [is_trans β s], is_trans α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b c h₁ h₂, o.2 (H _ _ _ (o.1 h₁) (o.1 h₂))⟩ protected theorem is_total : ∀ (f : r ≼o s) [is_total β s], is_total α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o o).2 (H _ _)⟩ protected theorem is_preorder : ∀ (f : r ≼o s) [is_preorder β s], is_preorder α r | f H := by exactI {..f.is_refl, ..f.is_trans} protected theorem is_partial_order : ∀ (f : r ≼o s) [is_partial_order β s], is_partial_order α r | f H := by exactI {..f.is_preorder, ..f.is_antisymm} protected theorem is_linear_order : ∀ (f : r ≼o s) [is_linear_order β s], is_linear_order α r | f H := by exactI {..f.is_partial_order, ..f.is_total} protected theorem is_strict_order : ∀ (f : r ≼o s) [is_strict_order β s], is_strict_order α r | f H := by exactI {..f.is_irrefl, ..f.is_trans} protected theorem is_trichotomous : ∀ (f : r ≼o s) [is_trichotomous β s], is_trichotomous α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o (or_congr f.inj'.eq_iff.symm o)).2 (H _ _)⟩ protected theorem is_strict_total_order' : ∀ (f : r ≼o s) [is_strict_total_order' β s], is_strict_total_order' α r | f H := by exactI {..f.is_trichotomous, ..f.is_strict_order} protected theorem acc (f : r ≼o s) (a : α) : acc s (f a) → acc r a := begin generalize h : f a = b, intro ac, induction ac with _ H IH generalizing a, subst h, exact ⟨_, λ a' h, IH (f a') (f.ord'.1 h) _ rfl⟩ end protected theorem well_founded : ∀ (f : r ≼o s) (h : well_founded s), well_founded r | f ⟨H⟩ := ⟨λ a, f.acc _ (H _)⟩ protected theorem is_well_order : ∀ (f : r ≼o s) [is_well_order β s], is_well_order α r | f H := by exactI {wf := f.well_founded H.wf, ..f.is_strict_total_order'} /-- It suffices to prove `f` is monotone between strict orders to show it is an order embedding. -/ def of_monotone [is_trichotomous α r] [is_asymm β s] (f : α → β) (H : ∀ a b, r a b → s (f a) (f b)) : r ≼o s := begin haveI := @is_irrefl_of_is_asymm β s _, refine ⟨⟨f, λ a b e, _⟩, λ a b, ⟨H _ _, λ h, _⟩⟩, { refine ((@trichotomous _ r _ a b).resolve_left _).resolve_right _; exact λ h, @irrefl _ s _ _ (by simpa [e] using H _ _ h) }, { refine (@trichotomous _ r _ a b).resolve_right (or.rec (λ e, _) (λ h', _)), { subst e, exact irrefl _ h }, { exact asymm (H _ _ h') h } } end @[simp] theorem of_monotone_coe [is_trichotomous α r] [is_asymm β s] (f : α → β) (H) : (@of_monotone _ _ r s _ _ f H : α → β) = f := rfl -- If le is preserved by an order embedding of preorders, then lt is too def lt_embedding_of_le_embedding [preorder α] [preorder β] (f : (has_le.le : α → α → Prop) ≼o (has_le.le : β → β → Prop)) : (has_lt.lt : α → α → Prop) ≼o (has_lt.lt : β → β → Prop) := { to_fun := f, inj := f.inj, ord := by intros; simp [lt_iff_le_not_le,f.ord] } def nat_lt [is_strict_order α r] (f : ℕ → α) (H : ∀ n:ℕ, r (f n) (f (n+1))) : ((<) : ℕ → ℕ → Prop) ≼o r := of_monotone f $ λ a b h, begin induction b with b IH, {exact (nat.not_lt_zero _ h).elim}, cases nat.lt_succ_iff_lt_or_eq.1 h with h e, { exact trans (IH h) (H _) }, { subst b, apply H } end def nat_gt [is_strict_order α r] (f : ℕ → α) (H : ∀ n:ℕ, r (f (n+1)) (f n)) : ((>) : ℕ → ℕ → Prop) ≼o r := by haveI := is_strict_order.swap r; exact rsymm (nat_lt f H) theorem well_founded_iff_no_descending_seq [is_strict_order α r] : well_founded r ↔ ¬ nonempty (((>) : ℕ → ℕ → Prop) ≼o r) := ⟨λ ⟨h⟩ ⟨⟨f, o⟩⟩, suffices ∀ a, acc r a → ∀ n, a ≠ f n, from this (f 0) (h _) 0 rfl, λ a ac, begin induction ac with a _ IH, intros n h, subst a, exact IH (f (n+1)) (o.1 (nat.lt_succ_self _)) _ rfl end, λ N, ⟨λ a, classical.by_contradiction $ λ na, let ⟨f, h⟩ := classical.axiom_of_choice $ show ∀ x : {a // ¬ acc r a}, ∃ y : {a // ¬ acc r a}, r y.1 x.1, from λ ⟨x, h⟩, classical.by_contradiction $ λ hn, h $ ⟨_, λ y h, classical.by_contradiction $ λ na, hn ⟨⟨y, na⟩, h⟩⟩ in N ⟨nat_gt (λ n, (f^[n] ⟨a, na⟩).1) $ λ n, by rw nat.iterate_succ'; apply h⟩⟩⟩ end order_embedding /-- The inclusion map `fin n → ℕ` is an order embedding. -/ def fin.val.order_embedding (n) : @order_embedding (fin n) ℕ (<) (<) := ⟨⟨fin.val, @fin.eq_of_veq _⟩, λ a b, iff.rfl⟩ /-- The inclusion map `fin m → fin n` is an order embedding. -/ def fin_fin.order_embedding {m n} (h : m ≤ n) : @order_embedding (fin m) (fin n) (<) (<) := ⟨⟨λ ⟨x, h'⟩, ⟨x, lt_of_lt_of_le h' h⟩, λ ⟨a, _⟩ ⟨b, _⟩ h, by congr; injection h⟩, by intros; cases a; cases b; refl⟩ instance fin.lt.is_well_order (n) : is_well_order (fin n) (<) := (fin.val.order_embedding _).is_well_order /-- An order isomorphism is an equivalence that is also an order embedding. -/ structure order_iso {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ≃ β := (ord : ∀ {a b}, r a b ↔ s (to_equiv a) (to_equiv b)) infix ` ≃o `:25 := order_iso namespace order_iso def to_order_embedding (f : r ≃o s) : r ≼o s := ⟨f.to_equiv.to_embedding, f.ord⟩ instance : has_coe (r ≃o s) (r ≼o s) := ⟨to_order_embedding⟩ -- see Note [function coercion] instance : has_coe_to_fun (r ≃o s) := ⟨λ _, α → β, λ f, f⟩ @[simp] lemma coe_coe_fn (f : r ≃o s) : ((f : r ≼o s) : α → β) = f := rfl @[simp] lemma to_equiv_to_fun (f : r ≃o s) (x : α) : f.to_equiv.to_fun x = f x := rfl theorem ord' : ∀ (f : r ≃o s) {a b}, r a b ↔ s (f a) (f b) | ⟨f, o⟩ := @o lemma ord'' {r : α → α → Prop} {s : β → β → Prop} (f : r ≃o s) {x y : α} : r x y ↔ s ((↑f : r ≼o s) x) ((↑f : r ≼o s) y) := f.ord' @[simp] theorem coe_fn_mk (f : α ≃ β) (o) : (@order_iso.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_equiv (f : r ≃o s) : (f.to_equiv : α → β) = f := rfl theorem eq_of_to_fun_eq : ∀ {e₁ e₂ : r ≃o s}, (e₁ : α → β) = e₂ → e₁ = e₂ | ⟨e₁, o₁⟩ ⟨e₂, o₂⟩ h := by congr; exact equiv.eq_of_to_fun_eq h @[refl] protected def refl (r : α → α → Prop) : r ≃o r := ⟨equiv.refl _, λ a b, iff.rfl⟩ @[symm] protected def symm (f : r ≃o s) : s ≃o r := ⟨f.to_equiv.symm, λ a b, by cases f with f o; rw o; simp⟩ @[trans] protected def trans (f₁ : r ≃o s) (f₂ : s ≃o t) : r ≃o t := ⟨f₁.to_equiv.trans f₂.to_equiv, λ a b, by cases f₁ with f₁ o₁; cases f₂ with f₂ o₂; rw [o₁, o₂]; simp⟩ @[simp] theorem coe_fn_symm_mk (f o) : ((@order_iso.mk _ _ r s f o).symm : β → α) = f.symm := rfl @[simp] theorem refl_apply (x : α) : order_iso.refl r x = x := rfl @[simp] theorem trans_apply : ∀ (f : r ≃o s) (g : s ≃o t) (a : α), (f.trans g) a = g (f a) | ⟨f₁, o₁⟩ ⟨f₂, o₂⟩ a := equiv.trans_apply _ _ _ @[simp] theorem apply_symm_apply : ∀ (e : r ≃o s) (x : β), e (e.symm x) = x | ⟨f₁, o₁⟩ x := by simp @[simp] theorem symm_apply_apply : ∀ (e : r ≃o s) (x : α), e.symm (e x) = x | ⟨f₁, o₁⟩ x := by simp /-- Any equivalence lifts to an order isomorphism between `s` and its preimage. -/ def preimage (f : α ≃ β) (s : β → β → Prop) : f ⁻¹'o s ≃o s := ⟨f, λ a b, iff.rfl⟩ noncomputable def of_surjective (f : r ≼o s) (H : surjective f) : r ≃o s := ⟨equiv.of_bijective ⟨f.inj, H⟩, by simp [f.ord']⟩ @[simp] theorem of_surjective_coe (f : r ≼o s) (H) : (of_surjective f H : α → β) = f := by delta of_surjective; simp def sum_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂} (e₁ : @order_iso α₁ α₂ r₁ r₂) (e₂ : @order_iso β₁ β₂ s₁ s₂) : sum.lex r₁ s₁ ≃o sum.lex r₂ s₂ := ⟨equiv.sum_congr e₁.to_equiv e₂.to_equiv, λ a b, by cases e₁ with f hf; cases e₂ with g hg; cases a; cases b; simp [hf, hg]⟩ def prod_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂} (e₁ : @order_iso α₁ α₂ r₁ r₂) (e₂ : @order_iso β₁ β₂ s₁ s₂) : prod.lex r₁ s₁ ≃o prod.lex r₂ s₂ := ⟨equiv.prod_congr e₁.to_equiv e₂.to_equiv, λ a b, begin cases e₁ with f hf; cases e₂ with g hg, cases a with a₁ a₂; cases b with b₁ b₂, suffices : prod.lex r₁ s₁ (a₁, a₂) (b₁, b₂) ↔ prod.lex r₂ s₂ (f a₁, g a₂) (f b₁, g b₂), {simpa [hf, hg]}, split, { intro h, cases h with _ _ _ _ h _ _ _ h, { left, exact hf.1 h }, { right, exact hg.1 h } }, { generalize e : f b₁ = fb₁, intro h, cases h with _ _ _ _ h _ _ _ h, { subst e, left, exact hf.2 h }, { have := f.injective e, subst b₁, right, exact hg.2 h } } end⟩ end order_iso /-- A subset `p : set α` embeds into `α` -/ def set_coe_embedding {α : Type*} (p : set α) : p ↪ α := ⟨subtype.val, @subtype.eq _ _⟩ /-- `subrel r p` is the inherited relation on a subset. -/ def subrel (r : α → α → Prop) (p : set α) : p → p → Prop := @subtype.val _ p ⁻¹'o r @[simp] theorem subrel_val (r : α → α → Prop) (p : set α) {a b} : subrel r p a b ↔ r a.1 b.1 := iff.rfl namespace subrel protected def order_embedding (r : α → α → Prop) (p : set α) : subrel r p ≼o r := ⟨set_coe_embedding _, λ a b, iff.rfl⟩ @[simp] theorem order_embedding_apply (r : α → α → Prop) (p a) : subrel.order_embedding r p a = a.1 := rfl instance (r : α → α → Prop) [is_well_order α r] (p : set α) : is_well_order p (subrel r p) := order_embedding.is_well_order (subrel.order_embedding r p) end subrel /-- Restrict the codomain of an order embedding -/ def order_embedding.cod_restrict (p : set β) (f : r ≼o s) (H : ∀ a, f a ∈ p) : r ≼o subrel s p := ⟨f.to_embedding.cod_restrict p H, f.ord⟩ @[simp] theorem order_embedding.cod_restrict_apply (p) (f : r ≼o s) (H a) : order_embedding.cod_restrict p f H a = ⟨f a, H a⟩ := rfl
fbc30e03446222df2ee92c2314bf0d2d6d7f46df
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/letFun.lean
15aa19971042602e346381765a159c9da3292e82
[ "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
235
lean
#check let_fun f x := x * 2 let_fun x := 1 let_fun y := x + 1 f (y + x) example (a b : Nat) (h1 : a = 0) (h2 : b = 0) : (let_fun x := a + 1; x + x) > b := by simp (config := { beta := false }) [h1] trace_state simp [h2]
dede24d0525b397a4ce1c3d17b4c7dcc22726eee
f02b05b665ad2d2cd970e93758a98d57f310a822
/src/tactic/tcache/command.lean
50a5c03514e2251d470e6ee0edbd38a6ea49bf82
[]
no_license
khoek/leancache
b0ed5d2a9a2e87f58d4ad27343d957a9cd49ce5f
5c8329f7b647b8d82966ab180c4473b20d1f249c
refs/heads/master
1,585,344,140,717
1,549,094,327,000
1,549,094,327,000
147,138,187
0
0
null
null
null
null
UTF-8
Lean
false
false
294
lean
import .store open tcache open lean.parser interactive interaction_monad @[user_command] meta def clear_cmd (_ : parse $ tk "#clear") : lean.parser unit := do n ← ident, match n with | `tcache := clear_tcache >> tactic.trace "cache cleared" | _ := fail "unknown item to clear" end
cea60b263fe1b67b4086f638cef272793c55afc9
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/logic/embedding.lean
224eadda05d6eed8e9705d4a19ffa23b7ccd7835
[ "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
11,664
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.equiv.basic import data.sigma.basic /-! # Injective functions -/ universes u v w x namespace function /-- `α ↪ β` is a bundled injective function. -/ @[nolint has_inhabited_instance] -- depending on cardinalities, an injective function may not exist structure embedding (α : Sort*) (β : Sort*) := (to_fun : α → β) (inj' : injective to_fun) infixr ` ↪ `:25 := embedding instance {α : Sort u} {β : Sort v} : has_coe_to_fun (α ↪ β) := ⟨_, embedding.to_fun⟩ initialize_simps_projections embedding (to_fun → apply) end function /-- Convert an `α ≃ β` to `α ↪ β`. -/ @[simps] protected def equiv.to_embedding {α : Sort u} {β : Sort v} (f : α ≃ β) : α ↪ β := ⟨f, f.injective⟩ /-- Given an equivalence to a subtype, produce an embedding to the elements of the corresponding set. -/ @[simps] def equiv.as_embedding {α β : Sort*} {p : β → Prop} (e : α ≃ subtype p) : α ↪ β := ⟨coe ∘ e, subtype.coe_injective.comp e.injective⟩ @[simp] lemma equiv.as_embedding_range {α β : Sort*} {p : β → Prop} (e : α ≃ subtype p) : set.range e.as_embedding = set_of p := set.ext $ λ x, ⟨λ ⟨y, h⟩, h ▸ subtype.coe_prop (e y), λ hs, ⟨e.symm ⟨x, hs⟩, by simp⟩⟩ namespace function namespace embedding lemma coe_injective {α β} : @function.injective (α ↪ β) (α → β) coe_fn | ⟨x, _⟩ ⟨y, _⟩ rfl := rfl @[ext] lemma ext {α β} {f g : embedding α β} (h : ∀ x, f x = g x) : f = g := coe_injective (funext h) lemma ext_iff {α β} {f g : embedding α β} : (∀ x, f x = g x) ↔ f = g := ⟨ext, λ h _, by rw h⟩ @[simp] theorem to_fun_eq_coe {α β} (f : α ↪ β) : to_fun f = f := rfl @[simp] theorem coe_fn_mk {α β} (f : α → β) (i) : (@mk _ _ f i : α → β) = f := rfl @[simp] lemma mk_coe {α β : Type*} (f : α ↪ β) (inj) : (⟨f, inj⟩ : α ↪ β) = f := by { ext, simp } theorem injective {α β} (f : α ↪ β) : injective f := f.inj' @[refl, simps {simp_rhs := tt}] protected def refl (α : Sort*) : α ↪ α := ⟨id, injective_id⟩ @[trans, simps {simp_rhs := tt}] protected def trans {α β γ} (f : α ↪ β) (g : β ↪ γ) : α ↪ γ := ⟨g ∘ f, g.injective.comp f.injective⟩ @[simp] lemma equiv_to_embedding_trans_symm_to_embedding {α β : Sort*} (e : α ≃ β) : e.to_embedding.trans e.symm.to_embedding = embedding.refl _ := by { ext, simp, } @[simp] lemma equiv_symm_to_embedding_trans_to_embedding {α β : Sort*} (e : α ≃ β) : e.symm.to_embedding.trans e.to_embedding = embedding.refl _ := by { ext, simp, } protected def congr {α : Sort u} {β : Sort v} {γ : Sort w} {δ : Sort x} (e₁ : α ≃ β) (e₂ : γ ≃ δ) (f : α ↪ γ) : (β ↪ δ) := (equiv.to_embedding e₁.symm).trans (f.trans e₂.to_embedding) /-- A right inverse `surj_inv` of a surjective function as an `embedding`. -/ protected noncomputable def of_surjective {α β} (f : β → α) (hf : surjective f) : α ↪ β := ⟨surj_inv hf, injective_surj_inv _⟩ /-- Convert a surjective `embedding` to an `equiv` -/ protected noncomputable def equiv_of_surjective {α β} (f : α ↪ β) (hf : surjective f) : α ≃ β := equiv.of_bijective f ⟨f.injective, hf⟩ /-- There is always an embedding from an empty type. --/ protected def of_is_empty {α β} [is_empty α] : α ↪ β := ⟨is_empty_elim, is_empty_elim⟩ protected def of_not_nonempty {α β} (hα : ¬ nonempty α) : α ↪ β := ⟨λa, (hα ⟨a⟩).elim, assume a, (hα ⟨a⟩).elim⟩ /-- Change the value of an embedding `f` at one point. If the prescribed image is already occupied by some `f a'`, then swap the values at these two points. -/ def set_value {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : α ↪ β := ⟨λ a', if a' = a then b else if f a' = b then f a else f a', begin intros x y h, dsimp at h, split_ifs at h; try { substI b }; try { simp only [f.injective.eq_iff] at * }; cc end⟩ theorem set_value_eq {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : set_value f a b a = b := by simp [set_value] /-- Embedding into `option` -/ protected def some {α} : α ↪ option α := ⟨some, option.some_injective α⟩ /-- Embedding of a `subtype`. -/ def subtype {α} (p : α → Prop) : subtype p ↪ α := ⟨coe, λ _ _, subtype.ext_val⟩ @[simp] lemma coe_subtype {α} (p : α → Prop) : ⇑(subtype p) = coe := rfl /-- Choosing an element `b : β` gives an embedding of `punit` into `β`. -/ def punit {β : Sort*} (b : β) : punit ↪ β := ⟨λ _, b, by { rintros ⟨⟩ ⟨⟩ _, refl, }⟩ /-- Fixing an element `b : β` gives an embedding `α ↪ α × β`. -/ def sectl (α : Sort*) {β : Sort*} (b : β) : α ↪ α × β := ⟨λ a, (a, b), λ a a' h, congr_arg prod.fst h⟩ /-- Fixing an element `a : α` gives an embedding `β ↪ α × β`. -/ def sectr {α : Sort*} (a : α) (β : Sort*): β ↪ α × β := ⟨λ b, (a, b), λ b b' h, congr_arg prod.snd h⟩ /-- Restrict the codomain of an embedding. -/ def cod_restrict {α β} (p : set β) (f : α ↪ β) (H : ∀ a, f a ∈ p) : α ↪ p := ⟨λ a, ⟨f a, H a⟩, λ a b h, f.injective (@congr_arg _ _ _ _ subtype.val h)⟩ @[simp] theorem cod_restrict_apply {α β} (p) (f : α ↪ β) (H a) : cod_restrict p f H a = ⟨f a, H a⟩ := rfl /-- If `e₁` and `e₂` are embeddings, then so is `prod.map e₁ e₂ : (a, b) ↦ (e₁ a, e₂ b)`. -/ def prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α × γ ↪ β × δ := ⟨prod.map e₁ e₂, e₁.injective.prod_map e₂.injective⟩ @[simp] lemma coe_prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(e₁.prod_map e₂) = prod.map e₁ e₂ := rfl section sum open sum /-- If `e₁` and `e₂` are embeddings, then so is `sum.map e₁ e₂`. -/ def sum_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α ⊕ γ ↪ β ⊕ δ := ⟨sum.map e₁ e₂, assume s₁ s₂ h, match s₁, s₂, h with | inl a₁, inl a₂, h := congr_arg inl $ e₁.injective $ inl.inj h | inr b₁, inr b₂, h := congr_arg inr $ e₂.injective $ inr.inj h end⟩ @[simp] theorem coe_sum_map {α β γ δ} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(sum_map e₁ e₂) = sum.map e₁ e₂ := rfl /-- The embedding of `α` into the sum `α ⊕ β`. -/ @[simps] def inl {α β : Type*} : α ↪ α ⊕ β := ⟨sum.inl, λ a b, sum.inl.inj⟩ /-- The embedding of `β` into the sum `α ⊕ β`. -/ @[simps] def inr {α β : Type*} : β ↪ α ⊕ β := ⟨sum.inr, λ a b, sum.inr.inj⟩ end sum section sigma variables {α α' : Type*} {β : α → Type*} {β' : α' → Type*} /-- `sigma.mk` as an `function.embedding`. -/ @[simps apply] def sigma_mk (a : α) : β a ↪ Σ x, β x := ⟨sigma.mk a, sigma_mk_injective⟩ /-- If `f : α ↪ α'` is an embedding and `g : Π a, β α ↪ β' (f α)` is a family of embeddings, then `sigma.map f g` is an embedding. -/ @[simps apply] def sigma_map (f : α ↪ α') (g : Π a, β a ↪ β' (f a)) : (Σ a, β a) ↪ Σ a', β' a' := ⟨sigma.map f (λ a, g a), f.injective.sigma_map (λ a, (g a).injective)⟩ end sigma def Pi_congr_right {α : Sort*} {β γ : α → Sort*} (e : ∀ a, β a ↪ γ a) : (Π a, β a) ↪ (Π a, γ a) := ⟨λf a, e a (f a), λ f₁ f₂ h, funext $ λ a, (e a).injective (congr_fun h a)⟩ def arrow_congr_left {α : Sort u} {β : Sort v} {γ : Sort w} (e : α ↪ β) : (γ → α) ↪ (γ → β) := Pi_congr_right (λ _, e) noncomputable def arrow_congr_right {α : Sort u} {β : Sort v} {γ : Sort w} [inhabited γ] (e : α ↪ β) : (α → γ) ↪ (β → γ) := by haveI := classical.prop_decidable; exact let f' : (α → γ) → (β → γ) := λf b, if h : ∃c, e c = b then f (classical.some h) else default γ in ⟨f', assume f₁ f₂ h, funext $ assume c, have ∃c', e c' = e c, from ⟨c, rfl⟩, have eq' : f' f₁ (e c) = f' f₂ (e c), from congr_fun h _, have eq_b : classical.some this = c, from e.injective $ classical.some_spec this, by simp [f', this, if_pos, eq_b] at eq'; assumption⟩ protected def subtype_map {α β} {p : α → Prop} {q : β → Prop} (f : α ↪ β) (h : ∀{{x}}, p x → q (f x)) : {x : α // p x} ↪ {y : β // q y} := ⟨subtype.map f h, subtype.map_injective h f.2⟩ open set /-- `set.image` as an embedding `set α ↪ set β`. -/ @[simps apply] protected def image {α β} (f : α ↪ β) : set α ↪ set β := ⟨image f, f.2.image_injective⟩ lemma swap_apply {α β : Type*} [decidable_eq α] [decidable_eq β] (f : α ↪ β) (x y z : α) : equiv.swap (f x) (f y) (f z) = f (equiv.swap x y z) := f.injective.swap_apply x y z lemma swap_comp {α β : Type*} [decidable_eq α] [decidable_eq β] (f : α ↪ β) (x y : α) : equiv.swap (f x) (f y) ∘ f = f ∘ equiv.swap x y := f.injective.swap_comp x y end embedding end function namespace equiv open function.embedding /-- The type of embeddings `α ↪ β` is equivalent to the subtype of all injective functions `α → β`. -/ def subtype_injective_equiv_embedding (α β : Sort*) : {f : α → β // function.injective f} ≃ (α ↪ β) := { to_fun := λ f, ⟨f.val, f.property⟩, inv_fun := λ f, ⟨f, f.injective⟩, left_inv := λ f, by simp, right_inv := λ f, by {ext, refl} } /-- If `α₁ ≃ α₂` and `β₁ ≃ β₂`, then the type of embeddings `α₁ ↪ β₁` is equivalent to the type of embeddings `α₂ ↪ β₂`. -/ @[congr, simps apply] def embedding_congr {α β γ δ : Sort*} (h : α ≃ β) (h' : γ ≃ δ) : (α ↪ γ) ≃ (β ↪ δ) := { to_fun := λ f, h.symm.to_embedding.trans $ f.trans $ h'.to_embedding, inv_fun := λ f, h.to_embedding.trans $ f.trans $ h'.symm.to_embedding, left_inv := λ x, by {ext, simp}, right_inv := λ x, by {ext, simp} } @[simp] lemma embedding_congr_refl {α β : Sort*} : embedding_congr (equiv.refl α) (equiv.refl β) = equiv.refl (α ↪ β) := by {ext, refl} @[simp] lemma embedding_congr_trans {α₁ β₁ α₂ β₂ α₃ β₃ : Sort*} (e₁ : α₁ ≃ α₂) (e₁' : β₁ ≃ β₂) (e₂ : α₂ ≃ α₃) (e₂' : β₂ ≃ β₃) : embedding_congr (e₁.trans e₂) (e₁'.trans e₂') = (embedding_congr e₁ e₁').trans (embedding_congr e₂ e₂') := rfl @[simp] lemma embedding_congr_symm {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (embedding_congr e₁ e₂).symm = embedding_congr e₁.symm e₂.symm := rfl lemma embedding_congr_apply_trans {α₁ β₁ γ₁ α₂ β₂ γ₂ : Sort*} (ea : α₁ ≃ α₂) (eb : β₁ ≃ β₂) (ec : γ₁ ≃ γ₂) (f : α₁ ↪ β₁) (g : β₁ ↪ γ₁) : equiv.embedding_congr ea ec (f.trans g) = (equiv.embedding_congr ea eb f).trans (equiv.embedding_congr eb ec g) := by {ext, simp} @[simp] lemma refl_to_embedding {α : Type*} : (equiv.refl α).to_embedding = function.embedding.refl α := rfl @[simp] lemma trans_to_embedding {α β γ : Type*} (e : α ≃ β) (f : β ≃ γ) : (e.trans f).to_embedding = e.to_embedding.trans f.to_embedding := rfl end equiv namespace set /-- The injection map is an embedding between subsets. -/ @[simps apply] def embedding_of_subset {α} (s t : set α) (h : s ⊆ t) : s ↪ t := ⟨λ x, ⟨x.1, h x.2⟩, λ ⟨x, hx⟩ ⟨y, hy⟩ h, by { congr, injection h }⟩ end set
774136af60125dd008dd20d13834c3cfce3a5c0e
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/tactic_ref.lean
8772b6bacab722fd983341fb9556c63f5c83fa88
[ "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
705
lean
open tactic meta def tst1 (n : nat) : tactic nat := using_new_ref n $ λ r, do v ← read_ref r, trace v, write_ref r (v*2), v ← read_ref r, trace v, return v example : true := by do v ← tst1 10, guard (v = 20), constructor meta def tst2 : tactic nat := do r ← using_new_ref 0 $ λ r, do { return r }, read_ref r -- Should produce an exception, the reference is not valid anymore run_cmd fail_if_success $ tst2 meta def tst3 (n : nat) : tactic nat := using_new_ref n $ λ r, (write_ref r (n+10) >> failed) -- The state should be restored during backtracking <|> (read_ref r) example : true := by do v ← tst3 10, guard (v = 10), constructor
66dff07c8f01688a2bf5f39f18866ee92a646826
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/category_theory/monoidal/preadditive.lean
4ed0d57bc0269c91ec7d75ae5b5f990788135625
[ "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
1,838
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.preadditive.additive_functor import category_theory.monoidal.category /-! # Preadditive monoidal categories A monoidal category is `monoidal_preadditive` if it is preadditive and tensor product of morphisms is linear in both factors. -/ noncomputable theory namespace category_theory open category_theory.limits open category_theory.monoidal_category variables (C : Type*) [category C] [preadditive C] [monoidal_category C] /-- A category is `monoidal_preadditive` if tensoring is linear in both factors. Note we don't `extend preadditive C` here, as `abelian C` already extends it, and we'll need to have both typeclasses sometimes. -/ class monoidal_preadditive := (tensor_zero' : ∀ {W X Y Z : C} (f : W ⟶ X), f ⊗ (0 : Y ⟶ Z) = 0 . obviously) (zero_tensor' : ∀ {W X Y Z : C} (f : Y ⟶ Z), (0 : W ⟶ X) ⊗ f = 0 . obviously) (tensor_add' : ∀ {W X Y Z : C} (f : W ⟶ X) (g h : Y ⟶ Z), f ⊗ (g + h) = f ⊗ g + f ⊗ h . obviously) (add_tensor' : ∀ {W X Y Z : C} (f g : W ⟶ X) (h : Y ⟶ Z), (f + g) ⊗ h = f ⊗ h + g ⊗ h . obviously) restate_axiom monoidal_preadditive.tensor_zero' restate_axiom monoidal_preadditive.zero_tensor' restate_axiom monoidal_preadditive.tensor_add' restate_axiom monoidal_preadditive.add_tensor' attribute [simp] monoidal_preadditive.tensor_zero monoidal_preadditive.zero_tensor variables [monoidal_preadditive C] local attribute [simp] monoidal_preadditive.tensor_add monoidal_preadditive.add_tensor instance tensoring_left_additive (X : C) : ((tensoring_left C).obj X).additive := {} instance tensoring_right_additive (X : C) : ((tensoring_right C).obj X).additive := {} end category_theory
0f153c7a6216eca3b25a004e0eae8fb9daf510e9
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Elab/Tactic/Config.lean
9f8fa220a1be7cbe04d95384365adaf6b457d247
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,241
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Eval import Lean.Elab.Tactic.Basic import Lean.Elab.SyntheticMVars import Lean.Linter.MissingDocs namespace Lean.Elab.Tactic open Meta macro (name := configElab) doc?:(docComment)? "declare_config_elab" elabName:ident type:ident : command => `(unsafe def evalUnsafe (e : Expr) : TermElabM $type := Meta.evalExpr' (safety := .unsafe) $type ``$type e @[implemented_by evalUnsafe] opaque eval (e : Expr) : TermElabM $type $[$doc?:docComment]? def $elabName (optConfig : Syntax) : TermElabM $type := do if optConfig.isNone then return { : $type } else let c ← withoutModifyingStateWithInfoAndMessages <| withLCtx {} {} <| withSaveInfoContext <| Term.withSynthesize do let c ← Term.elabTermEnsuringType optConfig[0][3] (Lean.mkConst ``$type) Term.synthesizeSyntheticMVarsNoPostponing instantiateMVars c eval c ) open Linter.MissingDocs in @[builtin_missing_docs_handler Elab.Tactic.configElab] def checkConfigElab : SimpleHandler := mkSimpleHandler "config elab" end Lean.Elab.Tactic
31303fee7fee1a2a882aa0731395608c9e31fc5e
26ac254ecb57ffcb886ff709cf018390161a9225
/src/data/finset/basic.lean
8d8c89565c8ca3bda03a47eff26311abd491258c
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
86,105
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro -/ import data.multiset.finset_ops import tactic.monotonicity import tactic.apply /-! # Finite sets mathlib has several different models for finite sets, and it can be confusing when you're first getting used to them! This file builds the basic theory of `finset α`, modelled as a `multiset α` without duplicates. It's "constructive" in the since that there is an underlying list of elements, although this is wrapped in a quotient by permutations, so anytime you actually use this list you're obligated to show you didn't depend on the ordering. There's also the typeclass `fintype α` (which asserts that there is some `finset α` containing every term of type `α`) as well as the predicate `finite` on `s : set α` (which asserts `nonempty (fintype s)`). -/ open multiset subtype nat variables {α : Type*} {β : Type*} {γ : Type*} /-- `finset α` is the type of finite sets of elements of `α`. It is implemented as a multiset (a list up to permutation) which has no duplicate elements. -/ structure finset (α : Type*) := (val : multiset α) (nodup : nodup val) namespace finset theorem eq_of_veq : ∀ {s t : finset α}, s.1 = t.1 → s = t | ⟨s, _⟩ ⟨t, _⟩ rfl := rfl @[simp] theorem val_inj {s t : finset α} : s.1 = t.1 ↔ s = t := ⟨eq_of_veq, congr_arg _⟩ @[simp] theorem erase_dup_eq_self [decidable_eq α] (s : finset α) : erase_dup s.1 = s.1 := erase_dup_eq_self.2 s.2 instance has_decidable_eq [decidable_eq α] : decidable_eq (finset α) | s₁ s₂ := decidable_of_iff _ val_inj /- membership -/ instance : has_mem α (finset α) := ⟨λ a s, a ∈ s.1⟩ theorem mem_def {a : α} {s : finset α} : a ∈ s ↔ a ∈ s.1 := iff.rfl @[simp] theorem mem_mk {a : α} {s nd} : a ∈ @finset.mk α s nd ↔ a ∈ s := iff.rfl instance decidable_mem [h : decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ s) := multiset.decidable_mem _ _ /-! ### set coercion -/ /-- Convert a finset to a set in the natural way. -/ instance : has_lift (finset α) (set α) := ⟨λ s, {x | x ∈ s}⟩ @[simp, norm_cast] lemma mem_coe {a : α} {s : finset α} : a ∈ (↑s : set α) ↔ a ∈ s := iff.rfl @[simp] lemma set_of_mem {α} {s : finset α} : {a | a ∈ s} = ↑s := rfl @[simp] lemma coe_mem {s : finset α} (x : (↑s : set α)) : ↑x ∈ s := x.2 @[simp] lemma mk_coe {s : finset α} (x : (↑s : set α)) {h} : (⟨↑x, h⟩ : (↑s : set α)) = x := by { apply subtype.eq, refl, } instance decidable_mem' [decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ (↑s : set α)) := s.decidable_mem _ /-! ### extensionality -/ theorem ext_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ ∀ a, a ∈ s₁ ↔ a ∈ s₂ := val_inj.symm.trans $ nodup_ext s₁.2 s₂.2 @[ext] theorem ext {s₁ s₂ : finset α} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ := ext_iff.2 @[simp, norm_cast] theorem coe_inj {s₁ s₂ : finset α} : (↑s₁ : set α) = ↑s₂ ↔ s₁ = s₂ := set.ext_iff.trans ext_iff.symm lemma coe_injective {α} : function.injective (coe : finset α → set α) := λ s t, coe_inj.1 /-! ### subset -/ instance : has_subset (finset α) := ⟨λ s₁ s₂, ∀ ⦃a⦄, a ∈ s₁ → a ∈ s₂⟩ theorem subset_def {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ s₁.1 ⊆ s₂.1 := iff.rfl @[simp] theorem subset.refl (s : finset α) : s ⊆ s := subset.refl _ theorem subset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := subset.trans theorem superset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊇ s₂ → s₂ ⊇ s₃ → s₁ ⊇ s₃ := λ h' h, subset.trans h h' -- TODO: these should be global attributes, but this will require fixing other files local attribute [trans] subset.trans superset.trans theorem mem_of_subset {s₁ s₂ : finset α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := mem_of_subset theorem subset.antisymm {s₁ s₂ : finset α} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ := ext $ λ a, ⟨@H₁ a, @H₂ a⟩ theorem subset_iff {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ ∀ ⦃x⦄, x ∈ s₁ → x ∈ s₂ := iff.rfl @[simp, norm_cast] theorem coe_subset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊆ ↑s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem val_le_iff {s₁ s₂ : finset α} : s₁.1 ≤ s₂.1 ↔ s₁ ⊆ s₂ := le_iff_subset s₁.2 instance : has_ssubset (finset α) := ⟨λa b, a ⊆ b ∧ ¬ b ⊆ a⟩ instance : partial_order (finset α) := { le := (⊆), lt := (⊂), le_refl := subset.refl, le_trans := @subset.trans _, le_antisymm := @subset.antisymm _ } theorem subset.antisymm_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ s₁ ⊆ s₂ ∧ s₂ ⊆ s₁ := le_antisymm_iff @[simp] theorem le_iff_subset {s₁ s₂ : finset α} : s₁ ≤ s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem lt_iff_ssubset {s₁ s₂ : finset α} : s₁ < s₂ ↔ s₁ ⊂ s₂ := iff.rfl @[simp, norm_cast] lemma coe_ssubset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊂ s₂ := show (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊆ s₂ ∧ ¬s₂ ⊆ s₁, by simp only [set.ssubset_def, finset.coe_subset] @[simp] theorem val_lt_iff {s₁ s₂ : finset α} : s₁.1 < s₂.1 ↔ s₁ ⊂ s₂ := and_congr val_le_iff $ not_congr val_le_iff theorem ssubset_iff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ⊂ s₂ ↔ ∃ x ∈ s₂, x ∉ s₁ := set.ssubset_iff_of_subset h /-! ### Nonempty -/ /-- The property `s.nonempty` expresses the fact that the finset `s` is not empty. It should be used in theorem assumptions instead of `∃ x, x ∈ s` or `s ≠ ∅` as it gives access to a nice API thanks to the dot notation. -/ protected def nonempty (s : finset α) : Prop := ∃ x:α, x ∈ s @[simp, norm_cast] lemma coe_nonempty {s : finset α} : (↑s:set α).nonempty ↔ s.nonempty := iff.rfl lemma nonempty.bex {s : finset α} (h : s.nonempty) : ∃ x:α, x ∈ s := h lemma nonempty.mono {s t : finset α} (hst : s ⊆ t) (hs : s.nonempty) : t.nonempty := set.nonempty.mono hst hs /-! ### empty -/ /-- The empty finset -/ protected def empty : finset α := ⟨0, nodup_zero⟩ instance : has_emptyc (finset α) := ⟨finset.empty⟩ instance : inhabited (finset α) := ⟨∅⟩ @[simp] theorem empty_val : (∅ : finset α).1 = 0 := rfl @[simp] theorem not_mem_empty (a : α) : a ∉ (∅ : finset α) := id @[simp] theorem ne_empty_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ≠ ∅ := λ e, not_mem_empty a $ e ▸ h theorem nonempty.ne_empty {s : finset α} (h : s.nonempty) : s ≠ ∅ := exists.elim h $ λ a, ne_empty_of_mem @[simp] theorem empty_subset (s : finset α) : ∅ ⊆ s := zero_subset _ theorem eq_empty_of_forall_not_mem {s : finset α} (H : ∀x, x ∉ s) : s = ∅ := eq_of_veq (eq_zero_of_forall_not_mem H) lemma eq_empty_iff_forall_not_mem {s : finset α} : s = ∅ ↔ ∀ x, x ∉ s := ⟨by rintro rfl x; exact id, λ h, eq_empty_of_forall_not_mem h⟩ @[simp] theorem val_eq_zero {s : finset α} : s.1 = 0 ↔ s = ∅ := @val_inj _ s ∅ theorem subset_empty {s : finset α} : s ⊆ ∅ ↔ s = ∅ := subset_zero.trans val_eq_zero theorem nonempty_of_ne_empty {s : finset α} (h : s ≠ ∅) : s.nonempty := exists_mem_of_ne_zero (mt val_eq_zero.1 h) theorem nonempty_iff_ne_empty {s : finset α} : s.nonempty ↔ s ≠ ∅ := ⟨nonempty.ne_empty, nonempty_of_ne_empty⟩ theorem eq_empty_or_nonempty (s : finset α) : s = ∅ ∨ s.nonempty := classical.by_cases or.inl (λ h, or.inr (nonempty_of_ne_empty h)) @[simp] lemma coe_empty : ↑(∅ : finset α) = (∅ : set α) := rfl /-- A `finset` for an empty type is empty. -/ lemma eq_empty_of_not_nonempty (h : ¬ nonempty α) (s : finset α) : s = ∅ := finset.eq_empty_of_forall_not_mem $ λ x, false.elim $ not_nonempty_iff_imp_false.1 h x /-! ### singleton -/ /-- `{a} : finset a` is the set `{a}` containing `a` and nothing else. This differs from `insert a ∅` in that it does not require a `decidable_eq` instance for `α`. -/ instance : has_singleton α (finset α) := ⟨λ a, ⟨{a}, nodup_singleton a⟩⟩ @[simp] theorem singleton_val (a : α) : ({a} : finset α).1 = a :: 0 := rfl @[simp] theorem mem_singleton {a b : α} : b ∈ ({a} : finset α) ↔ b = a := mem_singleton theorem not_mem_singleton {a b : α} : a ∉ ({b} : finset α) ↔ a ≠ b := not_congr mem_singleton theorem mem_singleton_self (a : α) : a ∈ ({a} : finset α) := or.inl rfl theorem singleton_inj {a b : α} : ({a} : finset α) = {b} ↔ a = b := ⟨λ h, mem_singleton.1 (h ▸ mem_singleton_self _), congr_arg _⟩ theorem singleton_nonempty (a : α) : ({a} : finset α).nonempty := ⟨a, mem_singleton_self a⟩ @[simp] theorem singleton_ne_empty (a : α) : ({a} : finset α) ≠ ∅ := (singleton_nonempty a).ne_empty @[simp] lemma coe_singleton (a : α) : ↑({a} : finset α) = ({a} : set α) := by { ext, simp } lemma eq_singleton_iff_unique_mem {s : finset α} {a : α} : s = {a} ↔ a ∈ s ∧ ∀ x ∈ s, x = a := begin split; intro t, rw t, refine ⟨finset.mem_singleton_self _, λ _, finset.mem_singleton.1⟩, ext, rw finset.mem_singleton, refine ⟨t.right _, λ r, r.symm ▸ t.left⟩ end lemma singleton_iff_unique_mem (s : finset α) : (∃ a, s = {a}) ↔ ∃! a, a ∈ s := by simp only [eq_singleton_iff_unique_mem, exists_unique] lemma singleton_subset_set_iff {s : set α} {a : α} : ↑({a} : finset α) ⊆ s ↔ a ∈ s := by rw [coe_singleton, set.singleton_subset_iff] @[simp] lemma singleton_subset_iff {s : finset α} {a : α} : {a} ⊆ s ↔ a ∈ s := singleton_subset_set_iff /-! ### insert -/ section decidable_eq variables [decidable_eq α] /-- `insert a s` is the set `{a} ∪ s` containing `a` and the elements of `s`. -/ instance : has_insert α (finset α) := ⟨λ a s, ⟨_, nodup_ndinsert a s.2⟩⟩ theorem insert_def (a : α) (s : finset α) : insert a s = ⟨_, nodup_ndinsert a s.2⟩ := rfl @[simp] theorem insert_val (a : α) (s : finset α) : (insert a s).1 = ndinsert a s.1 := rfl theorem insert_val' (a : α) (s : finset α) : (insert a s).1 = erase_dup (a :: s.1) := by rw [erase_dup_cons, erase_dup_eq_self]; refl theorem insert_val_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : (insert a s).1 = a :: s.1 := by rw [insert_val, ndinsert_of_not_mem h] @[simp] theorem mem_insert {a b : α} {s : finset α} : a ∈ insert b s ↔ a = b ∨ a ∈ s := mem_ndinsert theorem mem_insert_self (a : α) (s : finset α) : a ∈ insert a s := mem_ndinsert_self a s.1 theorem mem_insert_of_mem {a b : α} {s : finset α} (h : a ∈ s) : a ∈ insert b s := mem_ndinsert_of_mem h theorem mem_of_mem_insert_of_ne {a b : α} {s : finset α} (h : b ∈ insert a s) : b ≠ a → b ∈ s := (mem_insert.1 h).resolve_left @[simp, norm_cast] lemma coe_insert (a : α) (s : finset α) : ↑(insert a s) = (insert a ↑s : set α) := set.ext $ λ x, by simp only [mem_coe, mem_insert, set.mem_insert_iff] instance : is_lawful_singleton α (finset α) := ⟨λ a, by { ext, simp }⟩ @[simp] theorem insert_eq_of_mem {a : α} {s : finset α} (h : a ∈ s) : insert a s = s := eq_of_veq $ ndinsert_of_mem h @[simp] theorem insert_singleton_self_eq (a : α) : ({a, a} : finset α) = {a} := insert_eq_of_mem $ mem_singleton_self _ theorem insert.comm (a b : α) (s : finset α) : insert a (insert b s) = insert b (insert a s) := ext $ λ x, by simp only [finset.mem_insert, or.left_comm] @[simp] theorem insert_idem (a : α) (s : finset α) : insert a (insert a s) = insert a s := ext $ λ x, by simp only [finset.mem_insert, or.assoc.symm, or_self] @[simp] theorem insert_ne_empty (a : α) (s : finset α) : insert a s ≠ ∅ := ne_empty_of_mem (mem_insert_self a s) lemma ne_insert_of_not_mem (s t : finset α) {a : α} (h : a ∉ s) : s ≠ insert a t := by { contrapose! h, simp [h] } theorem insert_subset {a : α} {s t : finset α} : insert a s ⊆ t ↔ a ∈ t ∧ s ⊆ t := by simp only [subset_iff, mem_insert, forall_eq, or_imp_distrib, forall_and_distrib] theorem subset_insert (a : α) (s : finset α) : s ⊆ insert a s := λ b, mem_insert_of_mem theorem insert_subset_insert (a : α) {s t : finset α} (h : s ⊆ t) : insert a s ⊆ insert a t := insert_subset.2 ⟨mem_insert_self _ _, subset.trans h (subset_insert _ _)⟩ lemma ssubset_iff {s t : finset α} : s ⊂ t ↔ (∃a ∉ s, insert a s ⊆ t) := by exact_mod_cast @set.ssubset_iff_insert α ↑s ↑t lemma ssubset_insert {s : finset α} {a : α} (h : a ∉ s) : s ⊂ insert a s := ssubset_iff.mpr ⟨a, h, subset.refl _⟩ @[elab_as_eliminator] protected theorem induction {α : Type*} {p : finset α → Prop} [decidable_eq α] (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : ∀ s, p s | ⟨s, nd⟩ := multiset.induction_on s (λ _, h₁) (λ a s IH nd, begin cases nodup_cons.1 nd with m nd', rw [← (eq_of_veq _ : insert a (finset.mk s _) = ⟨a::s, nd⟩)], { exact h₂ (by exact m) (IH nd') }, { rw [insert_val, ndinsert_of_not_mem m] } end) nd /-- To prove a proposition about an arbitrary `finset α`, it suffices to prove it for the empty `finset`, and to show that if it holds for some `finset α`, then it holds for the `finset` obtained by inserting a new element. -/ @[elab_as_eliminator] protected theorem induction_on {α : Type*} {p : finset α → Prop} [decidable_eq α] (s : finset α) (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : p s := finset.induction h₁ h₂ s /-- Inserting an element to a finite set is equivalent to the option type. -/ def subtype_insert_equiv_option {t : finset α} {x : α} (h : x ∉ t) : {i // i ∈ insert x t} ≃ option {i // i ∈ t} := begin refine { to_fun := λ y, if h : ↑y = x then none else some ⟨y, (finset.mem_insert.mp y.2).resolve_left h⟩, inv_fun := λ y, y.elim ⟨x, finset.mem_insert_self _ _⟩ $ λ z, ⟨z, finset.mem_insert_of_mem z.2⟩, .. }, { intro y, by_cases h : ↑y = x, simp only [subtype.ext_iff, h, option.elim, dif_pos, subtype.coe_mk], simp only [h, option.elim, dif_neg, not_false_iff, subtype.coe_eta, subtype.coe_mk] }, { rintro (_|y), simp only [option.elim, dif_pos, subtype.coe_mk], have : ↑y ≠ x, { rintro ⟨⟩, exact h y.2 }, simp only [this, option.elim, subtype.eta, dif_neg, not_false_iff, subtype.coe_eta, subtype.coe_mk] }, end /-! ### union -/ /-- `s ∪ t` is the set such that `a ∈ s ∪ t` iff `a ∈ s` or `a ∈ t`. -/ instance : has_union (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndunion s₁.1 s₂.2⟩⟩ theorem union_val_nd (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = ndunion s₁.1 s₂.1 := rfl @[simp] theorem union_val (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = s₁.1 ∪ s₂.1 := ndunion_eq_union s₁.2 @[simp] theorem mem_union {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := mem_ndunion theorem mem_union_left {a : α} {s₁ : finset α} (s₂ : finset α) (h : a ∈ s₁) : a ∈ s₁ ∪ s₂ := mem_union.2 $ or.inl h theorem mem_union_right {a : α} {s₂ : finset α} (s₁ : finset α) (h : a ∈ s₂) : a ∈ s₁ ∪ s₂ := mem_union.2 $ or.inr h theorem not_mem_union {a : α} {s₁ s₂ : finset α} : a ∉ s₁ ∪ s₂ ↔ a ∉ s₁ ∧ a ∉ s₂ := by rw [mem_union, not_or_distrib] @[simp, norm_cast] lemma coe_union (s₁ s₂ : finset α) : ↑(s₁ ∪ s₂) = (↑s₁ ∪ ↑s₂ : set α) := set.ext $ λ x, mem_union theorem union_subset {s₁ s₂ s₃ : finset α} (h₁ : s₁ ⊆ s₃) (h₂ : s₂ ⊆ s₃) : s₁ ∪ s₂ ⊆ s₃ := val_le_iff.1 (ndunion_le.2 ⟨h₁, val_le_iff.2 h₂⟩) theorem subset_union_left (s₁ s₂ : finset α) : s₁ ⊆ s₁ ∪ s₂ := λ x, mem_union_left _ theorem subset_union_right (s₁ s₂ : finset α) : s₂ ⊆ s₁ ∪ s₂ := λ x, mem_union_right _ theorem union_comm (s₁ s₂ : finset α) : s₁ ∪ s₂ = s₂ ∪ s₁ := ext $ λ x, by simp only [mem_union, or_comm] instance : is_commutative (finset α) (∪) := ⟨union_comm⟩ @[simp] theorem union_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) := ext $ λ x, by simp only [mem_union, or_assoc] instance : is_associative (finset α) (∪) := ⟨union_assoc⟩ @[simp] theorem union_idempotent (s : finset α) : s ∪ s = s := ext $ λ _, mem_union.trans $ or_self _ instance : is_idempotent (finset α) (∪) := ⟨union_idempotent⟩ theorem union_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := ext $ λ _, by simp only [mem_union, or.left_comm] theorem union_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := ext $ λ x, by simp only [mem_union, or_assoc, or_comm (x ∈ s₂)] theorem union_self (s : finset α) : s ∪ s = s := union_idempotent s @[simp] theorem union_empty (s : finset α) : s ∪ ∅ = s := ext $ λ x, mem_union.trans $ or_false _ @[simp] theorem empty_union (s : finset α) : ∅ ∪ s = s := ext $ λ x, mem_union.trans $ false_or _ theorem insert_eq (a : α) (s : finset α) : insert a s = {a} ∪ s := rfl @[simp] theorem insert_union (a : α) (s t : finset α) : insert a s ∪ t = insert a (s ∪ t) := by simp only [insert_eq, union_assoc] @[simp] theorem union_insert (a : α) (s t : finset α) : s ∪ insert a t = insert a (s ∪ t) := by simp only [insert_eq, union_left_comm] theorem insert_union_distrib (a : α) (s t : finset α) : insert a (s ∪ t) = insert a s ∪ insert a t := by simp only [insert_union, union_insert, insert_idem] @[simp] lemma union_eq_left_iff_subset {s t : finset α} : s ∪ t = s ↔ t ⊆ s := begin split, { assume h, have : t ⊆ s ∪ t := subset_union_right _ _, rwa h at this }, { assume h, exact subset.antisymm (union_subset (subset.refl _) h) (subset_union_left _ _) } end @[simp] lemma left_eq_union_iff_subset {s t : finset α} : s = s ∪ t ↔ t ⊆ s := by rw [← union_eq_left_iff_subset, eq_comm] @[simp] lemma union_eq_right_iff_subset {s t : finset α} : t ∪ s = s ↔ t ⊆ s := by rw [union_comm, union_eq_left_iff_subset] @[simp] lemma right_eq_union_iff_subset {s t : finset α} : s = t ∪ s ↔ t ⊆ s := by rw [← union_eq_right_iff_subset, eq_comm] /-! ### inter -/ /-- `s ∩ t` is the set such that `a ∈ s ∩ t` iff `a ∈ s` and `a ∈ t`. -/ instance : has_inter (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndinter s₂.1 s₁.2⟩⟩ theorem inter_val_nd (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = ndinter s₁.1 s₂.1 := rfl @[simp] theorem inter_val (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = s₁.1 ∩ s₂.1 := ndinter_eq_inter s₁.2 @[simp] theorem mem_inter {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ := mem_ndinter theorem mem_of_mem_inter_left {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₁ := (mem_inter.1 h).1 theorem mem_of_mem_inter_right {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₂ := (mem_inter.1 h).2 theorem mem_inter_of_mem {a : α} {s₁ s₂ : finset α} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ := and_imp.1 mem_inter.2 theorem inter_subset_left (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₁ := λ a, mem_of_mem_inter_left theorem inter_subset_right (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₂ := λ a, mem_of_mem_inter_right theorem subset_inter {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₁ ⊆ s₃ → s₁ ⊆ s₂ ∩ s₃ := by simp only [subset_iff, mem_inter] {contextual:=tt}; intros; split; trivial @[simp, norm_cast] lemma coe_inter (s₁ s₂ : finset α) : ↑(s₁ ∩ s₂) = (↑s₁ ∩ ↑s₂ : set α) := set.ext $ λ _, mem_inter @[simp] theorem union_inter_cancel_left {s t : finset α} : (s ∪ t) ∩ s = s := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_left] @[simp] theorem union_inter_cancel_right {s t : finset α} : (s ∪ t) ∩ t = t := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_right] theorem inter_comm (s₁ s₂ : finset α) : s₁ ∩ s₂ = s₂ ∩ s₁ := ext $ λ _, by simp only [mem_inter, and_comm] @[simp] theorem inter_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) := ext $ λ _, by simp only [mem_inter, and_assoc] theorem inter_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := ext $ λ _, by simp only [mem_inter, and.left_comm] theorem inter_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := ext $ λ _, by simp only [mem_inter, and.right_comm] @[simp] theorem inter_self (s : finset α) : s ∩ s = s := ext $ λ _, mem_inter.trans $ and_self _ @[simp] theorem inter_empty (s : finset α) : s ∩ ∅ = ∅ := ext $ λ _, mem_inter.trans $ and_false _ @[simp] theorem empty_inter (s : finset α) : ∅ ∩ s = ∅ := ext $ λ _, mem_inter.trans $ false_and _ @[simp] lemma inter_union_self (s t : finset α) : s ∩ (t ∪ s) = s := by rw [inter_comm, union_inter_cancel_right] @[simp] theorem insert_inter_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₂) : insert a s₁ ∩ s₂ = insert a (s₁ ∩ s₂) := ext $ λ x, have x = a ∨ x ∈ s₂ ↔ x ∈ s₂, from or_iff_right_of_imp $ by rintro rfl; exact h, by simp only [mem_inter, mem_insert, or_and_distrib_left, this] @[simp] theorem inter_insert_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₁) : s₁ ∩ insert a s₂ = insert a (s₁ ∩ s₂) := by rw [inter_comm, insert_inter_of_mem h, inter_comm] @[simp] theorem insert_inter_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₂) : insert a s₁ ∩ s₂ = s₁ ∩ s₂ := ext $ λ x, have ¬ (x = a ∧ x ∈ s₂), by rintro ⟨rfl, H⟩; exact h H, by simp only [mem_inter, mem_insert, or_and_distrib_right, this, false_or] @[simp] theorem inter_insert_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₁) : s₁ ∩ insert a s₂ = s₁ ∩ s₂ := by rw [inter_comm, insert_inter_of_not_mem h, inter_comm] @[simp] theorem singleton_inter_of_mem {a : α} {s : finset α} (H : a ∈ s) : {a} ∩ s = {a} := show insert a ∅ ∩ s = insert a ∅, by rw [insert_inter_of_mem H, empty_inter] @[simp] theorem singleton_inter_of_not_mem {a : α} {s : finset α} (H : a ∉ s) : {a} ∩ s = ∅ := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_singleton]; rintro x ⟨rfl, h⟩; exact H h @[simp] theorem inter_singleton_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ∩ {a} = {a} := by rw [inter_comm, singleton_inter_of_mem h] @[simp] theorem inter_singleton_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : s ∩ {a} = ∅ := by rw [inter_comm, singleton_inter_of_not_mem h] @[mono] lemma inter_subset_inter {x y s t : finset α} (h : x ⊆ y) (h' : s ⊆ t) : x ∩ s ⊆ y ∩ t := begin intros a a_in, rw finset.mem_inter at a_in ⊢, exact ⟨h a_in.1, h' a_in.2⟩ end lemma inter_subset_inter_right {x y s : finset α} (h : x ⊆ y) : x ∩ s ⊆ y ∩ s := finset.inter_subset_inter h (finset.subset.refl _) lemma inter_subset_inter_left {x y s : finset α} (h : x ⊆ y) : s ∩ x ⊆ s ∩ y := finset.inter_subset_inter (finset.subset.refl _) h /-! ### lattice laws -/ instance : lattice (finset α) := { sup := (∪), sup_le := assume a b c, union_subset, le_sup_left := subset_union_left, le_sup_right := subset_union_right, inf := (∩), le_inf := assume a b c, subset_inter, inf_le_left := inter_subset_left, inf_le_right := inter_subset_right, ..finset.partial_order } @[simp] theorem sup_eq_union (s t : finset α) : s ⊔ t = s ∪ t := rfl @[simp] theorem inf_eq_inter (s t : finset α) : s ⊓ t = s ∩ t := rfl instance : semilattice_inf_bot (finset α) := { bot := ∅, bot_le := empty_subset, ..finset.lattice } instance {α : Type*} [decidable_eq α] : semilattice_sup_bot (finset α) := { ..finset.semilattice_inf_bot, ..finset.lattice } instance : distrib_lattice (finset α) := { le_sup_inf := assume a b c, show (a ∪ b) ∩ (a ∪ c) ⊆ a ∪ b ∩ c, by simp only [subset_iff, mem_inter, mem_union, and_imp, or_imp_distrib] {contextual:=tt}; simp only [true_or, imp_true_iff, true_and, or_true], ..finset.lattice } theorem inter_distrib_left (s t u : finset α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := inf_sup_left theorem inter_distrib_right (s t u : finset α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := inf_sup_right theorem union_distrib_left (s t u : finset α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := sup_inf_left theorem union_distrib_right (s t u : finset α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := sup_inf_right lemma union_eq_empty_iff (A B : finset α) : A ∪ B = ∅ ↔ A = ∅ ∧ B = ∅ := sup_eq_bot_iff /-! ### erase -/ /-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are not equal to `a`. -/ def erase (s : finset α) (a : α) : finset α := ⟨_, nodup_erase_of_nodup a s.2⟩ @[simp] theorem erase_val (s : finset α) (a : α) : (erase s a).1 = s.1.erase a := rfl @[simp] theorem mem_erase {a b : α} {s : finset α} : a ∈ erase s b ↔ a ≠ b ∧ a ∈ s := mem_erase_iff_of_nodup s.2 theorem not_mem_erase (a : α) (s : finset α) : a ∉ erase s a := mem_erase_of_nodup s.2 @[simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl theorem ne_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ≠ a := by simp only [mem_erase]; exact and.left theorem mem_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ∈ s := mem_of_mem_erase theorem mem_erase_of_ne_of_mem {a b : α} {s : finset α} : a ≠ b → a ∈ s → a ∈ erase s b := by simp only [mem_erase]; exact and.intro /-- An element of `s` that is not an element of `erase s a` must be `a`. -/ lemma eq_of_mem_of_not_mem_erase {a b : α} {s : finset α} (hs : b ∈ s) (hsa : b ∉ s.erase a) : b = a := begin rw [mem_erase, not_and] at hsa, exact not_imp_not.mp hsa hs end theorem erase_insert {a : α} {s : finset α} (h : a ∉ s) : erase (insert a s) a = s := ext $ assume x, by simp only [mem_erase, mem_insert, and_or_distrib_left, not_and_self, false_or]; apply and_iff_right_of_imp; rintro H rfl; exact h H theorem insert_erase {a : α} {s : finset α} (h : a ∈ s) : insert a (erase s a) = s := ext $ assume x, by simp only [mem_insert, mem_erase, or_and_distrib_left, dec_em, true_and]; apply or_iff_right_of_imp; rintro rfl; exact h theorem erase_subset_erase (a : α) {s t : finset α} (h : s ⊆ t) : erase s a ⊆ erase t a := val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h theorem erase_subset (a : α) (s : finset α) : erase s a ⊆ s := erase_subset _ _ @[simp, norm_cast] lemma coe_erase (a : α) (s : finset α) : ↑(erase s a) = (↑s \ {a} : set α) := set.ext $ λ _, mem_erase.trans $ by rw [and_comm, set.mem_diff, set.mem_singleton_iff]; refl lemma erase_ssubset {a : α} {s : finset α} (h : a ∈ s) : s.erase a ⊂ s := calc s.erase a ⊂ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _ ... = _ : insert_erase h theorem erase_eq_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : erase s a = s := eq_of_veq $ erase_of_not_mem h theorem subset_insert_iff {a : α} {s t : finset α} : s ⊆ insert a t ↔ erase s a ⊆ t := by simp only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp]; exact forall_congr (λ x, forall_swap) theorem erase_insert_subset (a : α) (s : finset α) : erase (insert a s) a ⊆ s := subset_insert_iff.1 $ subset.refl _ theorem insert_erase_subset (a : α) (s : finset α) : s ⊆ insert a (erase s a) := subset_insert_iff.2 $ subset.refl _ /-! ### sdiff -/ /-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/ instance : has_sdiff (finset α) := ⟨λs₁ s₂, ⟨s₁.1 - s₂.1, nodup_of_le (sub_le_self _ _) s₁.2⟩⟩ @[simp] theorem mem_sdiff {a : α} {s₁ s₂ : finset α} : a ∈ s₁ \ s₂ ↔ a ∈ s₁ ∧ a ∉ s₂ := mem_sub_of_nodup s₁.2 lemma not_mem_sdiff_of_mem_right {a : α} {s t : finset α} (h : a ∈ t) : a ∉ s \ t := by simp only [mem_sdiff, h, not_true, not_false_iff, and_false] theorem sdiff_union_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : (s₂ \ s₁) ∪ s₁ = s₂ := ext $ λ a, by simpa only [mem_sdiff, mem_union, or_comm, or_and_distrib_left, dec_em, and_true] using or_iff_right_of_imp (@h a) theorem union_sdiff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ∪ (s₂ \ s₁) = s₂ := (union_comm _ _).trans (sdiff_union_of_subset h) theorem inter_sdiff (s t u : finset α) : s ∩ (t \ u) = s ∩ t \ u := by { ext x, simp [and_assoc] } @[simp] theorem inter_sdiff_self (s₁ s₂ : finset α) : s₁ ∩ (s₂ \ s₁) = ∅ := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_sdiff]; rintro x ⟨h, _, hn⟩; exact hn h @[simp] theorem sdiff_inter_self (s₁ s₂ : finset α) : (s₂ \ s₁) ∩ s₁ = ∅ := (inter_comm _ _).trans (inter_sdiff_self _ _) @[simp] theorem sdiff_self (s₁ : finset α) : s₁ \ s₁ = ∅ := by ext; simp theorem sdiff_inter_distrib_right (s₁ s₂ s₃ : finset α) : s₁ \ (s₂ ∩ s₃) = (s₁ \ s₂) ∪ (s₁ \ s₃) := by ext; simp only [and_or_distrib_left, mem_union, classical.not_and_distrib, mem_sdiff, mem_inter] @[simp] theorem sdiff_inter_self_left (s₁ s₂ : finset α) : s₁ \ (s₁ ∩ s₂) = s₁ \ s₂ := by simp only [sdiff_inter_distrib_right, sdiff_self, empty_union] @[simp] theorem sdiff_inter_self_right (s₁ s₂ : finset α) : s₁ \ (s₂ ∩ s₁) = s₁ \ s₂ := by simp only [sdiff_inter_distrib_right, sdiff_self, union_empty] @[simp] theorem sdiff_empty {s₁ : finset α} : s₁ \ ∅ = s₁ := ext (by simp) @[mono] theorem sdiff_subset_sdiff {s₁ s₂ t₁ t₂ : finset α} (h₁ : t₁ ⊆ t₂) (h₂ : s₂ ⊆ s₁) : t₁ \ s₁ ⊆ t₂ \ s₂ := by simpa only [subset_iff, mem_sdiff, and_imp] using λ a m₁ m₂, and.intro (h₁ m₁) (mt (@h₂ _) m₂) theorem sdiff_subset_self {s₁ s₂ : finset α} : s₁ \ s₂ ⊆ s₁ := suffices s₁ \ s₂ ⊆ s₁ \ ∅, by simpa [sdiff_empty] using this, sdiff_subset_sdiff (subset.refl _) (empty_subset _) @[simp, norm_cast] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (↑s₁ \ ↑s₂ : set α) := set.ext $ λ _, mem_sdiff @[simp] theorem union_sdiff_self_eq_union {s t : finset α} : s ∪ (t \ s) = s ∪ t := ext $ λ a, by simp only [mem_union, mem_sdiff, or_iff_not_imp_left, imp_and_distrib, and_iff_left id] @[simp] theorem sdiff_union_self_eq_union {s t : finset α} : (s \ t) ∪ t = s ∪ t := by rw [union_comm, union_sdiff_self_eq_union, union_comm] lemma union_sdiff_symm {s t : finset α} : s ∪ (t \ s) = t ∪ (s \ t) := by rw [union_sdiff_self_eq_union, union_sdiff_self_eq_union, union_comm] lemma sdiff_union_inter (s t : finset α) : (s \ t) ∪ (s ∩ t) = s := by { simp only [ext_iff, mem_union, mem_sdiff, mem_inter], tauto } @[simp] lemma sdiff_idem (s t : finset α) : s \ t \ t = s \ t := by { simp only [ext_iff, mem_sdiff], tauto } lemma sdiff_eq_empty_iff_subset {s t : finset α} : s \ t = ∅ ↔ s ⊆ t := by { rw [subset_iff, ext_iff], simp } @[simp] lemma empty_sdiff (s : finset α) : ∅ \ s = ∅ := by { rw sdiff_eq_empty_iff_subset, exact empty_subset _ } lemma insert_sdiff_of_not_mem (s : finset α) {t : finset α} {x : α} (h : x ∉ t) : (insert x s) \ t = insert x (s \ t) := begin rw [← coe_inj, coe_insert, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_not_mem ↑s h end lemma insert_sdiff_of_mem (s : finset α) {t : finset α} {x : α} (h : x ∈ t) : (insert x s) \ t = s \ t := begin rw [← coe_inj, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_mem ↑s h end @[simp] lemma sdiff_subset (s t : finset α) : s \ t ⊆ s := by simp [subset_iff, mem_sdiff] {contextual := tt} lemma union_sdiff_distrib (s₁ s₂ t : finset α) : (s₁ ∪ s₂) \ t = s₁ \ t ∪ s₂ \ t := by { simp only [ext_iff, mem_sdiff, mem_union], tauto } lemma sdiff_union_distrib (s t₁ t₂ : finset α) : s \ (t₁ ∪ t₂) = (s \ t₁) ∩ (s \ t₂) := by { simp only [ext_iff, mem_union, mem_sdiff, mem_inter], tauto } lemma union_sdiff_self (s t : finset α) : (s ∪ t) \ t = s \ t := by rw [union_sdiff_distrib, sdiff_self, union_empty] lemma sdiff_singleton_eq_erase (a : α) (s : finset α) : s \ singleton a = erase s a := by { ext, rw [mem_erase, mem_sdiff, mem_singleton], tauto } lemma sdiff_sdiff_self_left (s t : finset α) : s \ (s \ t) = s ∩ t := by { simp only [ext_iff, mem_sdiff, mem_inter], tauto } lemma inter_eq_inter_of_sdiff_eq_sdiff {s t₁ t₂ : finset α} : s \ t₁ = s \ t₂ → s ∩ t₁ = s ∩ t₂ := by { simp only [ext_iff, mem_sdiff, mem_inter], intros b c, replace b := b c, split; tauto } end decidable_eq /-! ### attach -/ /-- `attach s` takes the elements of `s` and forms a new set of elements of the subtype `{x // x ∈ s}`. -/ def attach (s : finset α) : finset {x // x ∈ s} := ⟨attach s.1, nodup_attach.2 s.2⟩ @[simp] theorem attach_val (s : finset α) : s.attach.1 = s.1.attach := rfl @[simp] theorem mem_attach (s : finset α) : ∀ x, x ∈ s.attach := mem_attach _ @[simp] theorem attach_empty : attach (∅ : finset α) = ∅ := rfl /-! ### piecewise -/ section piecewise /-- `s.piecewise f g` is the function equal to `f` on the finset `s`, and to `g` on its complement. -/ def piecewise {α : Type*} {δ : α → Sort*} (s : finset α) (f g : Πi, δ i) [∀j, decidable (j ∈ s)] : Πi, δ i := λi, if i ∈ s then f i else g i variables {δ : α → Sort*} (s : finset α) (f g : Πi, δ i) @[simp] lemma piecewise_insert_self [decidable_eq α] {j : α} [∀i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g j = f j := by simp [piecewise] @[simp] lemma piecewise_empty [∀i : α, decidable (i ∈ (∅ : finset α))] : piecewise ∅ f g = g := by { ext i, simp [piecewise] } variable [∀j, decidable (j ∈ s)] @[norm_cast] lemma piecewise_coe [∀j, decidable (j ∈ (↑s : set α))] : (↑s : set α).piecewise f g = s.piecewise f g := by { ext, congr } @[simp, priority 980] lemma piecewise_eq_of_mem {i : α} (hi : i ∈ s) : s.piecewise f g i = f i := by simp [piecewise, hi] @[simp, priority 980] lemma piecewise_eq_of_not_mem {i : α} (hi : i ∉ s) : s.piecewise f g i = g i := by simp [piecewise, hi] @[simp, priority 990] lemma piecewise_insert_of_ne [decidable_eq α] {i j : α} [∀i, decidable (i ∈ insert j s)] (h : i ≠ j) : (insert j s).piecewise f g i = s.piecewise f g i := by simp [piecewise, h] lemma piecewise_insert [decidable_eq α] (j : α) [∀i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g = function.update (s.piecewise f g) j (f j) := begin classical, rw [← piecewise_coe, ← piecewise_coe, ← set.piecewise_insert, ← coe_insert j s], congr end lemma update_eq_piecewise {β : Type*} [decidable_eq α] (f : α → β) (i : α) (v : β) : function.update f i v = piecewise (singleton i) (λj, v) f := begin ext j, by_cases h : j = i, { rw [h], simp }, { simp [h] } end end piecewise section decidable_pi_exists variables {s : finset α} instance decidable_dforall_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] : decidable (∀a (h : a ∈ s), p a h) := multiset.decidable_dforall_multiset /-- decidable equality for functions whose domain is bounded by finsets -/ instance decidable_eq_pi_finset {β : α → Type*} [h : ∀a, decidable_eq (β a)] : decidable_eq (Πa∈s, β a) := multiset.decidable_eq_pi_multiset instance decidable_dexists_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] : decidable (∃a (h : a ∈ s), p a h) := multiset.decidable_dexists_multiset end decidable_pi_exists /-! ### filter -/ section filter variables {p q : α → Prop} [decidable_pred p] [decidable_pred q] /-- `filter p s` is the set of elements of `s` that satisfy `p`. -/ def filter (p : α → Prop) [decidable_pred p] (s : finset α) : finset α := ⟨_, nodup_filter p s.2⟩ @[simp] theorem filter_val (s : finset α) : (filter p s).1 = s.1.filter p := rfl @[simp] theorem mem_filter {s : finset α} {a : α} : a ∈ s.filter p ↔ a ∈ s ∧ p a := mem_filter @[simp] theorem filter_subset (s : finset α) : s.filter p ⊆ s := filter_subset _ theorem filter_ssubset {s : finset α} : s.filter p ⊂ s ↔ ∃ x ∈ s, ¬ p x := ⟨λ h, let ⟨x, hs, hp⟩ := set.exists_of_ssubset h in ⟨x, hs, mt (λ hp, mem_filter.2 ⟨hs, hp⟩) hp⟩, λ ⟨x, hs, hp⟩, ⟨s.filter_subset, λ h, hp (mem_filter.1 (h hs)).2⟩⟩ theorem filter_filter (s : finset α) : (s.filter p).filter q = s.filter (λa, p a ∧ q a) := ext $ assume a, by simp only [mem_filter, and_comm, and.left_comm] lemma filter_true {s : finset α} [h : decidable_pred (λ _, true)] : @finset.filter α (λ _, true) h s = s := by ext; simp @[simp] theorem filter_false {h} (s : finset α) : @filter α (λa, false) h s = ∅ := ext $ assume a, by simp only [mem_filter, and_false]; refl /-- If all elements of a `finset` satisfy the predicate `p`, `s.filter p` is `s`. -/ @[simp] lemma filter_true_of_mem {s : finset α} (h : ∀ x ∈ s, p x) : s.filter p = s := ext $ λ x, ⟨λ h, (mem_filter.1 h).1, λ hx, mem_filter.2 ⟨hx, h x hx⟩⟩ lemma filter_congr {s : finset α} (H : ∀ x ∈ s, p x ↔ q x) : filter p s = filter q s := eq_of_veq $ filter_congr H lemma filter_empty : filter p ∅ = ∅ := subset_empty.1 $ filter_subset _ lemma filter_subset_filter {s t : finset α} (h : s ⊆ t) : s.filter p ⊆ t.filter p := assume a ha, mem_filter.2 ⟨h (mem_filter.1 ha).1, (mem_filter.1 ha).2⟩ @[simp, norm_cast] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) := set.ext $ λ _, mem_filter theorem filter_singleton (a : α) : filter p (singleton a) = if p a then singleton a else ∅ := by { classical, ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } variable [decidable_eq α] theorem filter_union (s₁ s₂ : finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p := ext $ λ _, by simp only [mem_filter, mem_union, or_and_distrib_right] theorem filter_union_right (p q : α → Prop) [decidable_pred p] [decidable_pred q] (s : finset α) : s.filter p ∪ s.filter q = s.filter (λx, p x ∨ q x) := ext $ λ x, by simp only [mem_filter, mem_union, and_or_distrib_left.symm] lemma filter_mem_eq_inter {s t : finset α} : s.filter (λ i, i ∈ t) = s ∩ t := ext $ λ i, by rw [mem_filter, mem_inter] theorem filter_inter {s t : finset α} : filter p s ∩ t = filter p (s ∩ t) := by { ext, simp only [mem_inter, mem_filter, and.right_comm] } theorem inter_filter {s t : finset α} : s ∩ filter p t = filter p (s ∩ t) := by rw [inter_comm, filter_inter, inter_comm] theorem filter_insert (a : α) (s : finset α) : filter p (insert a s) = if p a then insert a (filter p s) else (filter p s) := by { ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } theorem filter_or [decidable_pred (λ a, p a ∨ q a)] (s : finset α) : s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q := ext $ λ _, by simp only [mem_filter, mem_union, and_or_distrib_left] theorem filter_and [decidable_pred (λ a, p a ∧ q a)] (s : finset α) : s.filter (λ a, p a ∧ q a) = s.filter p ∩ s.filter q := ext $ λ _, by simp only [mem_filter, mem_inter, and_comm, and.left_comm, and_self] theorem filter_not [decidable_pred (λ a, ¬ p a)] (s : finset α) : s.filter (λ a, ¬ p a) = s \ s.filter p := ext $ by simpa only [mem_filter, mem_sdiff, and_comm, not_and] using λ a, and_congr_right $ λ h : a ∈ s, (imp_iff_right h).symm.trans imp_not_comm theorem sdiff_eq_filter (s₁ s₂ : finset α) : s₁ \ s₂ = filter (∉ s₂) s₁ := ext $ λ _, by simp only [mem_sdiff, mem_filter] theorem sdiff_eq_self (s₁ s₂ : finset α) : s₁ \ s₂ = s₁ ↔ s₁ ∩ s₂ ⊆ ∅ := by { simp [subset.antisymm_iff,sdiff_subset_self], split; intro h, { transitivity' ((s₁ \ s₂) ∩ s₂), mono, simp }, { calc s₁ \ s₂ ⊇ s₁ \ (s₁ ∩ s₂) : by simp [(⊇)] ... ⊇ s₁ \ ∅ : by mono using [(⊇)] ... ⊇ s₁ : by simp [(⊇)] } } theorem filter_union_filter_neg_eq [decidable_pred (λ a, ¬ p a)] (s : finset α) : s.filter p ∪ s.filter (λa, ¬ p a) = s := by simp only [filter_not, union_sdiff_of_subset (filter_subset s)] theorem filter_inter_filter_neg_eq (s : finset α) : s.filter p ∩ s.filter (λa, ¬ p a) = ∅ := by simp only [filter_not, inter_sdiff_self] lemma subset_union_elim {s : finset α} {t₁ t₂ : set α} (h : ↑s ⊆ t₁ ∪ t₂) : ∃s₁ s₂ : finset α, s₁ ∪ s₂ = s ∧ ↑s₁ ⊆ t₁ ∧ ↑s₂ ⊆ t₂ \ t₁ := begin classical, refine ⟨s.filter (∈ t₁), s.filter (∉ t₁), _, _ , _⟩, { simp [filter_union_right, classical.or_not] }, { intro x, simp }, { intro x, simp, intros hx hx₂, refine ⟨or.resolve_left (h hx) hx₂, hx₂⟩ } end /- We can simplify an application of filter where the decidability is inferred in "the wrong way" -/ @[simp] lemma filter_congr_decidable {α} (s : finset α) (p : α → Prop) (h : decidable_pred p) [decidable_pred p] : @filter α p h s = s.filter p := by congr section classical open_locale classical /-- The following instance allows us to write `{ x ∈ s | p x }` for `finset.filter s p`. Since the former notation requires us to define this for all propositions `p`, and `finset.filter` only works for decidable propositions, the notation `{ x ∈ s | p x }` is only compatible with classical logic because it uses `classical.prop_decidable`. We don't want to redo all lemmas of `finset.filter` for `has_sep.sep`, so we make sure that `simp` unfolds the notation `{ x ∈ s | p x }` to `finset.filter s p`. If `p` happens to be decidable, the simp-lemma `filter_congr_decidable` will make sure that `finset.filter` uses the right instance for decidability. -/ noncomputable instance {α : Type*} : has_sep α (finset α) := ⟨λ p x, x.filter p⟩ @[simp] lemma sep_def {α : Type*} (s : finset α) (p : α → Prop) : {x ∈ s | p x} = s.filter p := rfl end classical /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq'` with the equality the other way. -/ -- This is not a good simp lemma, as it would prevent `finset.mem_filter` from firing -- on, e.g. `x ∈ s.filter(eq b)`. lemma filter_eq [decidable_eq β] (s : finset β) (b : β) : s.filter(eq b) = ite (b ∈ s) {b} ∅ := begin split_ifs, { ext, simp only [mem_filter, mem_singleton], exact ⟨λ h, h.2.symm, by { rintro ⟨h⟩, exact ⟨h, rfl⟩, }⟩ }, { ext, simp only [mem_filter, not_and, iff_false, not_mem_empty], rintros m ⟨e⟩, exact h m, } end /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq` with the equality the other way. -/ lemma filter_eq' [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, a = b) = ite (b ∈ s) {b} ∅ := trans (filter_congr (λ _ _, ⟨eq.symm, eq.symm⟩)) (filter_eq s b) lemma filter_ne [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, b ≠ a) = s.erase b := by { ext, simp only [mem_filter, mem_erase, ne.def], cc, } lemma filter_ne' [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, a ≠ b) = s.erase b := trans (filter_congr (λ _ _, ⟨ne.symm, ne.symm⟩)) (filter_ne s b) end filter /-! ### range -/ section range variables {n m l : ℕ} /-- `range n` is the set of natural numbers less than `n`. -/ def range (n : ℕ) : finset ℕ := ⟨_, nodup_range n⟩ @[simp] theorem range_coe (n : ℕ) : (range n).1 = multiset.range n := rfl @[simp] theorem mem_range : m ∈ range n ↔ m < n := mem_range @[simp] theorem range_zero : range 0 = ∅ := rfl @[simp] theorem range_one : range 1 = {0} := rfl theorem range_succ : range (succ n) = insert n (range n) := eq_of_veq $ (range_succ n).trans $ (ndinsert_of_not_mem not_mem_range_self).symm theorem range_add_one : range (n + 1) = insert n (range n) := range_succ @[simp] theorem not_mem_range_self : n ∉ range n := not_mem_range_self @[simp] theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) := multiset.self_mem_range_succ n @[simp] theorem range_subset {n m} : range n ⊆ range m ↔ n ≤ m := range_subset theorem range_mono : monotone range := λ _ _, range_subset.2 end range /- useful rules for calculations with quantifiers -/ theorem exists_mem_empty_iff (p : α → Prop) : (∃ x, x ∈ (∅ : finset α) ∧ p x) ↔ false := by simp only [not_mem_empty, false_and, exists_false] theorem exists_mem_insert [d : decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∃ x, x ∈ insert a s ∧ p x) ↔ p a ∨ (∃ x, x ∈ s ∧ p x) := by simp only [mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] theorem forall_mem_empty_iff (p : α → Prop) : (∀ x, x ∈ (∅ : finset α) → p x) ↔ true := iff_true_intro $ λ _, false.elim theorem forall_mem_insert [d : decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∀ x, x ∈ insert a s → p x) ↔ p a ∧ (∀ x, x ∈ s → p x) := by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] end finset /-- Equivalence between the set of natural numbers which are `≥ k` and `ℕ`, given by `n → n - k`. -/ def not_mem_range_equiv (k : ℕ) : {n // n ∉ range k} ≃ ℕ := { to_fun := λ i, i.1 - k, inv_fun := λ j, ⟨j + k, by simp⟩, left_inv := begin assume j, rw subtype.ext_iff_val, apply nat.sub_add_cancel, simpa using j.2 end, right_inv := λ j, nat.add_sub_cancel _ _ } @[simp] lemma coe_not_mem_range_equiv (k : ℕ) : (not_mem_range_equiv k : {n // n ∉ range k} → ℕ) = (λ i, i - k) := rfl @[simp] lemma coe_not_mem_range_equiv_symm (k : ℕ) : ((not_mem_range_equiv k).symm : ℕ → {n // n ∉ range k}) = λ j, ⟨j + k, by simp⟩ := rfl namespace option /-- Construct an empty or singleton finset from an `option` -/ def to_finset (o : option α) : finset α := match o with | none := ∅ | some a := {a} end @[simp] theorem to_finset_none : none.to_finset = (∅ : finset α) := rfl @[simp] theorem to_finset_some {a : α} : (some a).to_finset = {a} := rfl @[simp] theorem mem_to_finset {a : α} {o : option α} : a ∈ o.to_finset ↔ a ∈ o := by cases o; simp only [to_finset, finset.mem_singleton, option.mem_def, eq_comm]; refl end option /-! ### erase_dup on list and multiset -/ namespace multiset variable [decidable_eq α] /-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/ def to_finset (s : multiset α) : finset α := ⟨_, nodup_erase_dup s⟩ @[simp] theorem to_finset_val (s : multiset α) : s.to_finset.1 = s.erase_dup := rfl theorem to_finset_eq {s : multiset α} (n : nodup s) : finset.mk s n = s.to_finset := finset.val_inj.1 (erase_dup_eq_self.2 n).symm @[simp] theorem mem_to_finset {a : α} {s : multiset α} : a ∈ s.to_finset ↔ a ∈ s := mem_erase_dup @[simp] lemma to_finset_zero : to_finset (0 : multiset α) = ∅ := rfl @[simp] lemma to_finset_cons (a : α) (s : multiset α) : to_finset (a :: s) = insert a (to_finset s) := finset.eq_of_veq erase_dup_cons @[simp] lemma to_finset_add (s t : multiset α) : to_finset (s + t) = to_finset s ∪ to_finset t := finset.ext $ by simp @[simp] lemma to_finset_nsmul (s : multiset α) : ∀(n : ℕ) (hn : n ≠ 0), (n •ℕ s).to_finset = s.to_finset | 0 h := by contradiction | (n+1) h := begin by_cases n = 0, { rw [h, zero_add, one_nsmul] }, { rw [add_nsmul, to_finset_add, one_nsmul, to_finset_nsmul n h, finset.union_idempotent] } end @[simp] lemma to_finset_inter (s t : multiset α) : to_finset (s ∩ t) = to_finset s ∩ to_finset t := finset.ext $ by simp theorem to_finset_eq_empty {m : multiset α} : m.to_finset = ∅ ↔ m = 0 := finset.val_inj.symm.trans multiset.erase_dup_eq_zero end multiset namespace list variable [decidable_eq α] /-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/ def to_finset (l : list α) : finset α := multiset.to_finset l @[simp] theorem to_finset_val (l : list α) : l.to_finset.1 = (l.erase_dup : multiset α) := rfl theorem to_finset_eq {l : list α} (n : nodup l) : @finset.mk α l n = l.to_finset := multiset.to_finset_eq n @[simp] theorem mem_to_finset {a : α} {l : list α} : a ∈ l.to_finset ↔ a ∈ l := mem_erase_dup @[simp] theorem to_finset_nil : to_finset (@nil α) = ∅ := rfl @[simp] theorem to_finset_cons {a : α} {l : list α} : to_finset (a :: l) = insert a (to_finset l) := finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.erase_dup_cons, h] end list namespace finset /-! ### map -/ section map open function /-- When `f` is an embedding of `α` in `β` and `s` is a finset in `α`, then `s.map f` is the image finset in `β`. The embedding condition guarantees that there are no duplicates in the image. -/ def map (f : α ↪ β) (s : finset α) : finset β := ⟨s.1.map f, nodup_map f.2 s.2⟩ @[simp] theorem map_val (f : α ↪ β) (s : finset α) : (map f s).1 = s.1.map f := rfl @[simp] theorem map_empty (f : α ↪ β) : (∅ : finset α).map f = ∅ := rfl variables {f : α ↪ β} {s : finset α} @[simp] theorem mem_map {b : β} : b ∈ s.map f ↔ ∃ a ∈ s, f a = b := mem_map.trans $ by simp only [exists_prop]; refl theorem mem_map' (f : α ↪ β) {a} {s : finset α} : f a ∈ s.map f ↔ a ∈ s := mem_map_of_injective f.2 theorem mem_map_of_mem (f : α ↪ β) {a} {s : finset α} : a ∈ s → f a ∈ s.map f := (mem_map' _).2 @[simp, norm_cast] theorem coe_map (f : α ↪ β) (s : finset α) : (↑(s.map f) : set β) = f '' ↑s := set.ext $ λ x, mem_map.trans set.mem_image_iff_bex.symm theorem coe_map_subset_range (f : α ↪ β) (s : finset α) : (↑(s.map f) : set β) ⊆ set.range f := calc ↑(s.map f) = f '' ↑s : coe_map f s ... ⊆ set.range f : set.image_subset_range f ↑s theorem map_to_finset [decidable_eq α] [decidable_eq β] {s : multiset α} : s.to_finset.map f = (s.map f).to_finset := ext $ λ _, by simp only [mem_map, multiset.mem_map, exists_prop, multiset.mem_to_finset] @[simp] theorem map_refl : s.map (embedding.refl _) = s := ext $ λ _, by simpa only [mem_map, exists_prop] using exists_eq_right theorem map_map {g : β ↪ γ} : (s.map f).map g = s.map (f.trans g) := eq_of_veq $ by simp only [map_val, multiset.map_map]; refl theorem map_subset_map {s₁ s₂ : finset α} : s₁.map f ⊆ s₂.map f ↔ s₁ ⊆ s₂ := ⟨λ h x xs, (mem_map' _).1 $ h $ (mem_map' f).2 xs, λ h, by simp [subset_def, map_subset_map h]⟩ theorem map_inj {s₁ s₂ : finset α} : s₁.map f = s₂.map f ↔ s₁ = s₂ := by simp only [subset.antisymm_iff, map_subset_map] /-- Associate to an embedding `f` from `α` to `β` the embedding that maps a finset to its image under `f`. -/ def map_embedding (f : α ↪ β) : finset α ↪ finset β := ⟨map f, λ s₁ s₂, map_inj.1⟩ @[simp] theorem map_embedding_apply : map_embedding f s = map f s := rfl theorem map_filter {p : β → Prop} [decidable_pred p] : (s.map f).filter p = (s.filter (p ∘ f)).map f := ext $ λ b, by simp only [mem_filter, mem_map, exists_prop, and_assoc]; exact ⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, h1, h2, rfl⟩, by rintro ⟨x, h1, h2, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩ theorem map_union [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).map f = s₁.map f ∪ s₂.map f := ext $ λ _, by simp only [mem_map, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib] theorem map_inter [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∩ s₂).map f = s₁.map f ∩ s₂.map f := ext $ λ b, by simp only [mem_map, mem_inter, exists_prop]; exact ⟨by rintro ⟨a, ⟨m₁, m₂⟩, rfl⟩; exact ⟨⟨a, m₁, rfl⟩, ⟨a, m₂, rfl⟩⟩, by rintro ⟨⟨a, m₁, e⟩, ⟨a', m₂, rfl⟩⟩; cases f.2 e; exact ⟨_, ⟨m₁, m₂⟩, rfl⟩⟩ @[simp] theorem map_singleton (f : α ↪ β) (a : α) : map f {a} = {f a} := ext $ λ _, by simp only [mem_map, mem_singleton, exists_prop, exists_eq_left]; exact eq_comm @[simp] theorem map_insert [decidable_eq α] [decidable_eq β] (f : α ↪ β) (a : α) (s : finset α) : (insert a s).map f = insert (f a) (s.map f) := by simp only [insert_eq, map_union, map_singleton] @[simp] theorem map_eq_empty : s.map f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_map_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ lemma attach_map_val {s : finset α} : s.attach.map (embedding.subtype _) = s := eq_of_veq $ by rw [map_val, attach_val]; exact attach_map_val _ end map lemma range_add_one' (n : ℕ) : range (n + 1) = insert 0 ((range n).map ⟨λi, i + 1, assume i j, nat.succ.inj⟩) := by ext (⟨⟩ | ⟨n⟩); simp [nat.succ_eq_add_one, nat.zero_lt_succ n] /-! ### image -/ section image variables [decidable_eq β] /-- `image f s` is the forward image of `s` under `f`. -/ def image (f : α → β) (s : finset α) : finset β := (s.1.map f).to_finset @[simp] theorem image_val (f : α → β) (s : finset α) : (image f s).1 = (s.1.map f).erase_dup := rfl @[simp] theorem image_empty (f : α → β) : (∅ : finset α).image f = ∅ := rfl variables {f : α → β} {s : finset α} @[simp] theorem mem_image {b : β} : b ∈ s.image f ↔ ∃ a ∈ s, f a = b := by simp only [mem_def, image_val, mem_erase_dup, multiset.mem_map, exists_prop] theorem mem_image_of_mem (f : α → β) {a} {s : finset α} (h : a ∈ s) : f a ∈ s.image f := mem_image.2 ⟨_, h, rfl⟩ @[simp, norm_cast] lemma coe_image {f : α → β} : ↑(s.image f) = f '' ↑s := set.ext $ λ _, mem_image.trans set.mem_image_iff_bex.symm lemma nonempty.image (h : s.nonempty) (f : α → β) : (s.image f).nonempty := let ⟨a, ha⟩ := h in ⟨f a, mem_image_of_mem f ha⟩ theorem image_to_finset [decidable_eq α] {s : multiset α} : s.to_finset.image f = (s.map f).to_finset := ext $ λ _, by simp only [mem_image, multiset.mem_to_finset, exists_prop, multiset.mem_map] theorem image_val_of_inj_on (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : (image f s).1 = s.1.map f := multiset.erase_dup_eq_self.2 (nodup_map_on H s.2) theorem image_id [decidable_eq α] : s.image id = s := ext $ λ _, by simp only [mem_image, exists_prop, id, exists_eq_right] theorem image_image [decidable_eq γ] {g : β → γ} : (s.image f).image g = s.image (g ∘ f) := eq_of_veq $ by simp only [image_val, erase_dup_map_erase_dup_eq, multiset.map_map] theorem image_subset_image {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.image f ⊆ s₂.image f := by simp only [subset_def, image_val, subset_erase_dup', erase_dup_subset', multiset.map_subset_map h] theorem image_subset_iff {s : finset α} {t : finset β} {f : α → β} : s.image f ⊆ t ↔ ∀ x ∈ s, f x ∈ t := calc s.image f ⊆ t ↔ f '' ↑s ⊆ ↑t : by norm_cast ... ↔ _ : set.image_subset_iff theorem image_mono (f : α → β) : monotone (finset.image f) := λ _ _, image_subset_image theorem coe_image_subset_range : ↑(s.image f) ⊆ set.range f := calc ↑(s.image f) = f '' ↑s : coe_image ... ⊆ set.range f : set.image_subset_range f ↑s theorem image_filter {p : β → Prop} [decidable_pred p] : (s.image f).filter p = (s.filter (p ∘ f)).image f := ext $ λ b, by simp only [mem_filter, mem_image, exists_prop]; exact ⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, ⟨h1, h2⟩, rfl⟩, by rintro ⟨x, ⟨h1, h2⟩, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩ theorem image_union [decidable_eq α] {f : α → β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).image f = s₁.image f ∪ s₂.image f := ext $ λ _, by simp only [mem_image, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib] theorem image_inter [decidable_eq α] (s₁ s₂ : finset α) (hf : ∀x y, f x = f y → x = y) : (s₁ ∩ s₂).image f = s₁.image f ∩ s₂.image f := ext $ by simp only [mem_image, exists_prop, mem_inter]; exact λ b, ⟨λ ⟨a, ⟨m₁, m₂⟩, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, m₂, e⟩⟩, λ ⟨⟨a, m₁, e₁⟩, ⟨a', m₂, e₂⟩⟩, ⟨a, ⟨m₁, hf _ _ (e₂.trans e₁.symm) ▸ m₂⟩, e₁⟩⟩. @[simp] theorem image_singleton (f : α → β) (a : α) : image f {a} = {f a} := ext $ λ x, by simpa only [mem_image, exists_prop, mem_singleton, exists_eq_left] using eq_comm @[simp] theorem image_insert [decidable_eq α] (f : α → β) (a : α) (s : finset α) : (insert a s).image f = insert (f a) (s.image f) := by simp only [insert_eq, image_singleton, image_union] @[simp] theorem image_eq_empty : s.image f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_image_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ lemma attach_image_val [decidable_eq α] {s : finset α} : s.attach.image subtype.val = s := eq_of_veq $ by rw [image_val, attach_val, multiset.attach_map_val, erase_dup_eq_self] @[simp] lemma attach_insert [decidable_eq α] {a : α} {s : finset α} : attach (insert a s) = insert (⟨a, mem_insert_self a s⟩ : {x // x ∈ insert a s}) ((attach s).image (λx, ⟨x.1, mem_insert_of_mem x.2⟩)) := ext $ λ ⟨x, hx⟩, ⟨or.cases_on (mem_insert.1 hx) (λ h : x = a, λ _, mem_insert.2 $ or.inl $ subtype.eq h) (λ h : x ∈ s, λ _, mem_insert_of_mem $ mem_image.2 $ ⟨⟨x, h⟩, mem_attach _ _, subtype.eq rfl⟩), λ _, finset.mem_attach _ _⟩ theorem map_eq_image (f : α ↪ β) (s : finset α) : s.map f = s.image f := eq_of_veq $ (multiset.erase_dup_eq_self.2 (s.map f).2).symm lemma image_const {s : finset α} (h : s.nonempty) (b : β) : s.image (λa, b) = singleton b := ext $ assume b', by simp only [mem_image, exists_prop, exists_and_distrib_right, h.bex, true_and, mem_singleton, eq_comm] /-- Because `finset.image` requires a `decidable_eq` instances for the target type, we can only construct a `functor finset` when working classically. -/ instance [Π P, decidable P] : functor finset := { map := λ α β f s, s.image f, } instance [Π P, decidable P] : is_lawful_functor finset := { id_map := λ α x, image_id, comp_map := λ α β γ f g s, image_image.symm, } /-- Given a finset `s` and a predicate `p`, `s.subtype p` is the finset of `subtype p` whose elements belong to `s`. -/ protected def subtype {α} (p : α → Prop) [decidable_pred p] (s : finset α) : finset (subtype p) := (s.filter p).attach.map ⟨λ x, ⟨x.1, (finset.mem_filter.1 x.2).2⟩, λ x y H, subtype.eq $ subtype.mk.inj H⟩ @[simp] lemma mem_subtype {p : α → Prop} [decidable_pred p] {s : finset α} : ∀{a : subtype p}, a ∈ s.subtype p ↔ a.val ∈ s | ⟨a, ha⟩ := by simp [finset.subtype, ha] /-- `s.subtype p` converts back to `s.filter p` with `embedding.subtype`. -/ @[simp] lemma subtype_map (p : α → Prop) [decidable_pred p] : (s.subtype p).map (function.embedding.subtype _) = s.filter p := begin ext x, rw mem_map, change (∃ a : {x // p x}, ∃ H, a.val = x) ↔ _, split, { rintros ⟨y, hy, hyval⟩, rw [mem_subtype, hyval] at hy, rw mem_filter, use hy, rw ← hyval, use y.property }, { intro hx, rw mem_filter at hx, use ⟨⟨x, hx.2⟩, mem_subtype.2 hx.1, rfl⟩ } end /-- If all elements of a `finset` satisfy the predicate `p`, `s.subtype p` converts back to `s` with `embedding.subtype`. -/ lemma subtype_map_of_mem {p : α → Prop} [decidable_pred p] (h : ∀ x ∈ s, p x) : (s.subtype p).map (function.embedding.subtype _) = s := by rw [subtype_map, filter_true_of_mem h] /-- If a `finset` of a subtype is converted to the main type with `embedding.subtype`, the result does not contain any value that does not satisfy the property of the subtype. -/ lemma not_mem_map_subtype_of_not_property {p : α → Prop} (s : finset {x // p x}) {a : α} (h : ¬ p a) : a ∉ (s.map (function.embedding.subtype _)) := begin rw mem_map, push_neg, exact λ x, λ hxs hx, h (hx ▸ x.property) end lemma subset_image_iff {f : α → β} {s : finset β} {t : set α} : ↑s ⊆ f '' t ↔ ∃s' : finset α, ↑s' ⊆ t ∧ s'.image f = s := begin classical, split, swap, { rintro ⟨s, hs, rfl⟩, rw [coe_image], exact set.image_subset f hs }, intro h, induction s using finset.induction with a s has ih h, { refine ⟨∅, set.empty_subset _, _⟩, convert finset.image_empty _ }, rw [finset.coe_insert, set.insert_subset] at h, rcases ih h.2 with ⟨s', hst, hsi⟩, rcases h.1 with ⟨x, hxt, rfl⟩, refine ⟨insert x s', _, _⟩, { rw [finset.coe_insert, set.insert_subset], exact ⟨hxt, hst⟩ }, rw [finset.image_insert, hsi], congr end end image /-! ### card -/ section card /-- `card s` is the cardinality (number of elements) of `s`. -/ def card (s : finset α) : nat := s.1.card theorem card_def (s : finset α) : s.card = s.1.card := rfl @[simp] lemma card_mk {m nodup} : (⟨m, nodup⟩ : finset α).card = m.card := rfl @[simp] theorem card_empty : card (∅ : finset α) = 0 := rfl @[simp] theorem card_eq_zero {s : finset α} : card s = 0 ↔ s = ∅ := card_eq_zero.trans val_eq_zero theorem card_pos {s : finset α} : 0 < card s ↔ s.nonempty := pos_iff_ne_zero.trans $ (not_congr card_eq_zero).trans nonempty_iff_ne_empty.symm theorem card_ne_zero_of_mem {s : finset α} {a : α} (h : a ∈ s) : card s ≠ 0 := (not_congr card_eq_zero).2 (ne_empty_of_mem h) theorem card_eq_one {s : finset α} : s.card = 1 ↔ ∃ a, s = {a} := by cases s; simp only [multiset.card_eq_one, finset.card, ← val_inj, singleton_val] @[simp] theorem card_insert_of_not_mem [decidable_eq α] {a : α} {s : finset α} (h : a ∉ s) : card (insert a s) = card s + 1 := by simpa only [card_cons, card, insert_val] using congr_arg multiset.card (ndinsert_of_not_mem h) theorem card_insert_of_mem [decidable_eq α] {a : α} {s : finset α} (h : a ∈ s) : card (insert a s) = card s := by rw insert_eq_of_mem h theorem card_insert_le [decidable_eq α] (a : α) (s : finset α) : card (insert a s) ≤ card s + 1 := by by_cases a ∈ s; [{rw [insert_eq_of_mem h], apply nat.le_add_right}, rw [card_insert_of_not_mem h]] @[simp] theorem card_singleton (a : α) : card ({a} : finset α) = 1 := card_singleton _ theorem card_erase_of_mem [decidable_eq α] {a : α} {s : finset α} : a ∈ s → card (erase s a) = pred (card s) := card_erase_of_mem theorem card_erase_lt_of_mem [decidable_eq α] {a : α} {s : finset α} : a ∈ s → card (erase s a) < card s := card_erase_lt_of_mem theorem card_erase_le [decidable_eq α] {a : α} {s : finset α} : card (erase s a) ≤ card s := card_erase_le @[simp] theorem card_range (n : ℕ) : card (range n) = n := card_range n @[simp] theorem card_attach {s : finset α} : card (attach s) = card s := multiset.card_attach end card end finset theorem multiset.to_finset_card_le [decidable_eq α] (m : multiset α) : m.to_finset.card ≤ m.card := card_le_of_le (erase_dup_le _) theorem list.to_finset_card_le [decidable_eq α] (l : list α) : l.to_finset.card ≤ l.length := multiset.to_finset_card_le ⟦l⟧ namespace finset section card theorem card_image_le [decidable_eq β] {f : α → β} {s : finset α} : card (image f s) ≤ card s := by simpa only [card_map] using (s.1.map f).to_finset_card_le theorem card_image_of_inj_on [decidable_eq β] {f : α → β} {s : finset α} (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : card (image f s) = card s := by simp only [card, image_val_of_inj_on H, card_map] theorem card_image_of_injective [decidable_eq β] {f : α → β} (s : finset α) (H : function.injective f) : card (image f s) = card s := card_image_of_inj_on $ λ x _ y _ h, H h @[simp] lemma card_map {α β} (f : α ↪ β) {s : finset α} : (s.map f).card = s.card := multiset.card_map _ _ lemma card_eq_of_bijective {s : finset α} {n : ℕ} (f : ∀i, i < n → α) (hf : ∀a∈s, ∃i, ∃h:i<n, f i h = a) (hf' : ∀i (h : i < n), f i h ∈ s) (f_inj : ∀i j (hi : i < n) (hj : j < n), f i hi = f j hj → i = j) : card s = n := begin classical, have : ∀ (a : α), a ∈ s ↔ ∃i (hi : i ∈ range n), f i (mem_range.1 hi) = a, from assume a, ⟨assume ha, let ⟨i, hi, eq⟩ := hf a ha in ⟨i, mem_range.2 hi, eq⟩, assume ⟨i, hi, eq⟩, eq ▸ hf' i (mem_range.1 hi)⟩, have : s = ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)), by simpa only [ext_iff, mem_image, exists_prop, subtype.exists, mem_attach, true_and], calc card s = card ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)) : by rw [this] ... = card ((range n).attach) : card_image_of_injective _ $ assume ⟨i, hi⟩ ⟨j, hj⟩ eq, subtype.eq $ f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq ... = card (range n) : card_attach ... = n : card_range n end lemma card_eq_succ [decidable_eq α] {s : finset α} {n : ℕ} : s.card = n + 1 ↔ (∃a t, a ∉ t ∧ insert a t = s ∧ card t = n) := iff.intro (assume eq, have 0 < card s, from eq.symm ▸ nat.zero_lt_succ _, let ⟨a, has⟩ := card_pos.mp this in ⟨a, s.erase a, s.not_mem_erase a, insert_erase has, by simp only [eq, card_erase_of_mem has, pred_succ]⟩) (assume ⟨a, t, hat, s_eq, n_eq⟩, s_eq ▸ n_eq ▸ card_insert_of_not_mem hat) theorem card_le_of_subset {s t : finset α} : s ⊆ t → card s ≤ card t := multiset.card_le_of_le ∘ val_le_iff.mpr theorem eq_of_subset_of_card_le {s t : finset α} (h : s ⊆ t) (h₂ : card t ≤ card s) : s = t := eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) h₂ lemma card_lt_card {s t : finset α} (h : s ⊂ t) : s.card < t.card := card_lt_of_lt (val_lt_iff.2 h) lemma card_le_card_of_inj_on {s : finset α} {t : finset β} (f : α → β) (hf : ∀a∈s, f a ∈ t) (f_inj : ∀a₁∈s, ∀a₂∈s, f a₁ = f a₂ → a₁ = a₂) : card s ≤ card t := begin classical, calc card s = card (s.image f) : by rw [card_image_of_inj_on f_inj] ... ≤ card t : card_le_of_subset $ assume x hx, match x, finset.mem_image.1 hx with _, ⟨a, ha, rfl⟩ := hf a ha end end lemma card_le_of_inj_on {n} {s : finset α} (f : ℕ → α) (hf : ∀i<n, f i ∈ s) (f_inj : ∀i j, i<n → j<n → f i = f j → i = j) : n ≤ card s := calc n = card (range n) : (card_range n).symm ... ≤ card s : card_le_card_of_inj_on f (by simpa only [mem_range]) (by simp only [mem_range]; exact assume a₁ h₁ a₂ h₂, f_inj a₁ a₂ h₁ h₂) /-- Suppose that, given objects defined on all strict subsets of any finset `s`, one knows how to define an object on `s`. Then one can inductively define an object on all finsets, starting from the empty set and iterating. This can be used either to define data, or to prove properties. -/ @[elab_as_eliminator] def strong_induction_on {p : finset α → Sort*} : ∀ (s : finset α), (∀s, (∀t ⊂ s, p t) → p s) → p s | ⟨s, nd⟩ ih := multiset.strong_induction_on s (λ s IH nd, ih ⟨s, nd⟩ (λ ⟨t, nd'⟩ ss, IH t (val_lt_iff.2 ss) nd')) nd @[elab_as_eliminator] lemma case_strong_induction_on [decidable_eq α] {p : finset α → Prop} (s : finset α) (h₀ : p ∅) (h₁ : ∀ a s, a ∉ s → (∀t ⊆ s, p t) → p (insert a s)) : p s := finset.strong_induction_on s $ λ s, finset.induction_on s (λ _, h₀) $ λ a s n _ ih, h₁ a s n $ λ t ss, ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _) lemma card_congr {s : finset α} {t : finset β} (f : Π a ∈ s, β) (h₁ : ∀ a ha, f a ha ∈ t) (h₂ : ∀ a b ha hb, f a ha = f b hb → a = b) (h₃ : ∀ b ∈ t, ∃ a ha, f a ha = b) : s.card = t.card := by haveI := classical.prop_decidable; exact calc s.card = s.attach.card : card_attach.symm ... = (s.attach.image (λ (a : {a // a ∈ s}), f a.1 a.2)).card : eq.symm (card_image_of_injective _ (λ a b h, subtype.eq (h₂ _ _ _ _ h))) ... = t.card : congr_arg card (finset.ext $ λ b, ⟨λ h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ h₁ _ _, λ h, let ⟨a, ha₁, ha₂⟩ := h₃ b h in mem_image.2 ⟨⟨a, ha₁⟩, by simp [ha₂]⟩⟩) lemma card_union_add_card_inter [decidable_eq α] (s t : finset α) : (s ∪ t).card + (s ∩ t).card = s.card + t.card := finset.induction_on t (by simp) $ λ a r har, by by_cases a ∈ s; simp *; cc lemma card_union_le [decidable_eq α] (s t : finset α) : (s ∪ t).card ≤ s.card + t.card := card_union_add_card_inter s t ▸ le_add_right _ _ lemma card_union_eq [decidable_eq α] {s t : finset α} (h : disjoint s t) : (s ∪ t).card = s.card + t.card := begin rw [← card_union_add_card_inter], convert (add_zero _).symm, rw [card_eq_zero], rwa [disjoint_iff] at h end lemma surj_on_of_inj_on_of_card_le {s : finset α} {t : finset β} (f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t) (hinj : ∀ a₁ a₂ ha₁ ha₂, f a₁ ha₁ = f a₂ ha₂ → a₁ = a₂) (hst : card t ≤ card s) : (∀ b ∈ t, ∃ a ha, b = f a ha) := by haveI := classical.dec_eq β; exact λ b hb, have h : card (image (λ (a : {a // a ∈ s}), f a a.prop) (attach s)) = card s, from @card_attach _ s ▸ card_image_of_injective _ (λ ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ h, subtype.eq $ hinj _ _ _ _ h), have h₁ : image (λ a : {a // a ∈ s}, f a a.prop) s.attach = t := eq_of_subset_of_card_le (λ b h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ hf _ _) (by simp [hst, h]), begin rw ← h₁ at hb, rcases mem_image.1 hb with ⟨a, ha₁, ha₂⟩, exact ⟨a, a.2, ha₂.symm⟩, end open function lemma inj_on_of_surj_on_of_card_le {s : finset α} {t : finset β} (f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t) (hsurj : ∀ b ∈ t, ∃ a ha, b = f a ha) (hst : card s ≤ card t) ⦃a₁ a₂⦄ (ha₁ : a₁ ∈ s) (ha₂ : a₂ ∈ s) (ha₁a₂: f a₁ ha₁ = f a₂ ha₂) : a₁ = a₂ := by haveI : inhabited {x // x ∈ s} := ⟨⟨a₁, ha₁⟩⟩; exact let f' : {x // x ∈ s} → {x // x ∈ t} := λ x, ⟨f x.1 x.2, hf x.1 x.2⟩ in let g : {x // x ∈ t} → {x // x ∈ s} := @surj_inv _ _ f' (λ x, let ⟨y, hy₁, hy₂⟩ := hsurj x.1 x.2 in ⟨⟨y, hy₁⟩, subtype.eq hy₂.symm⟩) in have hg : injective g, from function.injective_surj_inv _, have hsg : surjective g, from λ x, let ⟨y, hy⟩ := surj_on_of_inj_on_of_card_le (λ (x : {x // x ∈ t}) (hx : x ∈ t.attach), g x) (λ x _, show (g x) ∈ s.attach, from mem_attach _ _) (λ x y _ _ hxy, hg hxy) (by simpa) x (mem_attach _ _) in ⟨y, hy.snd.symm⟩, have hif : injective f', from (left_inverse_of_surjective_of_right_inverse hsg (right_inverse_surj_inv _)).injective, subtype.ext_iff_val.1 (@hif ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ (subtype.eq ha₁a₂)) end card /-! ### bind -/ section bind variables [decidable_eq β] {s : finset α} {t : α → finset β} /-- `bind s t` is the union of `t x` over `x ∈ s` -/ protected def bind (s : finset α) (t : α → finset β) : finset β := (s.1.bind (λ a, (t a).1)).to_finset @[simp] theorem bind_val (s : finset α) (t : α → finset β) : (s.bind t).1 = (s.1.bind (λ a, (t a).1)).erase_dup := rfl @[simp] theorem bind_empty : finset.bind ∅ t = ∅ := rfl @[simp] theorem mem_bind {b : β} : b ∈ s.bind t ↔ ∃a∈s, b ∈ t a := by simp only [mem_def, bind_val, mem_erase_dup, mem_bind, exists_prop] @[simp] theorem bind_insert [decidable_eq α] {a : α} : (insert a s).bind t = t a ∪ s.bind t := ext $ λ x, by simp only [mem_bind, exists_prop, mem_union, mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] -- ext $ λ x, by simp [or_and_distrib_right, exists_or_distrib] @[simp] lemma singleton_bind {a : α} : finset.bind {a} t = t a := begin classical, rw [← insert_emptyc_eq, bind_insert, bind_empty, union_empty] end theorem bind_inter (s : finset α) (f : α → finset β) (t : finset β) : s.bind f ∩ t = s.bind (λ x, f x ∩ t) := begin ext x, simp only [mem_bind, mem_inter], tauto end theorem inter_bind (t : finset β) (s : finset α) (f : α → finset β) : t ∩ s.bind f = s.bind (λ x, t ∩ f x) := by rw [inter_comm, bind_inter]; simp [inter_comm] theorem image_bind [decidable_eq γ] {f : α → β} {s : finset α} {t : β → finset γ} : (s.image f).bind t = s.bind (λa, t (f a)) := by haveI := classical.dec_eq α; exact finset.induction_on s rfl (λ a s has ih, by simp only [image_insert, bind_insert, ih]) theorem bind_image [decidable_eq γ] {s : finset α} {t : α → finset β} {f : β → γ} : (s.bind t).image f = s.bind (λa, (t a).image f) := by haveI := classical.dec_eq α; exact finset.induction_on s rfl (λ a s has ih, by simp only [bind_insert, image_union, ih]) theorem bind_to_finset [decidable_eq α] (s : multiset α) (t : α → multiset β) : (s.bind t).to_finset = s.to_finset.bind (λa, (t a).to_finset) := ext $ λ x, by simp only [multiset.mem_to_finset, mem_bind, multiset.mem_bind, exists_prop] lemma bind_mono {t₁ t₂ : α → finset β} (h : ∀a∈s, t₁ a ⊆ t₂ a) : s.bind t₁ ⊆ s.bind t₂ := have ∀b a, a ∈ s → b ∈ t₁ a → (∃ (a : α), a ∈ s ∧ b ∈ t₂ a), from assume b a ha hb, ⟨a, ha, finset.mem_of_subset (h a ha) hb⟩, by simpa only [subset_iff, mem_bind, exists_imp_distrib, and_imp, exists_prop] lemma bind_subset_bind_of_subset_left {α : Type*} {s₁ s₂ : finset α} (t : α → finset β) (h : s₁ ⊆ s₂) : s₁.bind t ⊆ s₂.bind t := begin intro x, simp only [and_imp, mem_bind, exists_prop], exact Exists.imp (λ a ha, ⟨h ha.1, ha.2⟩) end lemma bind_singleton {f : α → β} : s.bind (λa, {f a}) = s.image f := ext $ λ x, by simp only [mem_bind, mem_image, mem_singleton, eq_comm] lemma image_bind_filter_eq [decidable_eq α] (s : finset β) (g : β → α) : (s.image g).bind (λa, s.filter $ (λc, g c = a)) = s := begin ext b, suffices : (∃ a, a ∈ s ∧ b ∈ s ∧ g b = g a) ↔ b ∈ s, by simpa, exact ⟨λ ⟨a, ha, hb, hab⟩, hb, λ hb, ⟨b, hb, hb, rfl⟩⟩ end end bind /-! ### prod-/ section prod variables {s : finset α} {t : finset β} /-- `product s t` is the set of pairs `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def product (s : finset α) (t : finset β) : finset (α × β) := ⟨_, nodup_product s.2 t.2⟩ @[simp] theorem product_val : (s.product t).1 = s.1.product t.1 := rfl @[simp] theorem mem_product {p : α × β} : p ∈ s.product t ↔ p.1 ∈ s ∧ p.2 ∈ t := mem_product theorem subset_product [decidable_eq α] [decidable_eq β] {s : finset (α × β)} : s ⊆ (s.image prod.fst).product (s.image prod.snd) := λ p hp, mem_product.2 ⟨mem_image_of_mem _ hp, mem_image_of_mem _ hp⟩ theorem product_eq_bind [decidable_eq α] [decidable_eq β] (s : finset α) (t : finset β) : s.product t = s.bind (λa, t.image $ λb, (a, b)) := ext $ λ ⟨x, y⟩, by simp only [mem_product, mem_bind, mem_image, exists_prop, prod.mk.inj_iff, and.left_comm, exists_and_distrib_left, exists_eq_right, exists_eq_left] @[simp] theorem card_product (s : finset α) (t : finset β) : card (s.product t) = card s * card t := multiset.card_product _ _ end prod /-! ### sigma -/ section sigma variables {σ : α → Type*} {s : finset α} {t : Πa, finset (σ a)} /-- `sigma s t` is the set of dependent pairs `⟨a, b⟩` such that `a ∈ s` and `b ∈ t a`. -/ protected def sigma (s : finset α) (t : Πa, finset (σ a)) : finset (Σa, σ a) := ⟨_, nodup_sigma s.2 (λ a, (t a).2)⟩ @[simp] theorem mem_sigma {p : sigma σ} : p ∈ s.sigma t ↔ p.1 ∈ s ∧ p.2 ∈ t (p.1) := mem_sigma theorem sigma_mono {s₁ s₂ : finset α} {t₁ t₂ : Πa, finset (σ a)} (H1 : s₁ ⊆ s₂) (H2 : ∀a, t₁ a ⊆ t₂ a) : s₁.sigma t₁ ⊆ s₂.sigma t₂ := λ ⟨x, sx⟩ H, let ⟨H3, H4⟩ := mem_sigma.1 H in mem_sigma.2 ⟨H1 H3, H2 x H4⟩ theorem sigma_eq_bind [decidable_eq α] [∀a, decidable_eq (σ a)] (s : finset α) (t : Πa, finset (σ a)) : s.sigma t = s.bind (λa, (t a).image $ λb, ⟨a, b⟩) := ext $ λ ⟨x, y⟩, by simp only [mem_sigma, mem_bind, mem_image, exists_prop, and.left_comm, exists_and_distrib_left, exists_eq_left, heq_iff_eq, exists_eq_right] end sigma /-! ### disjoint -/ section disjoint variable [decidable_eq α] theorem disjoint_left {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := by simp only [_root_.disjoint, inf_eq_inter, le_iff_subset, subset_iff, mem_inter, not_and, and_imp]; refl theorem disjoint_val {s t : finset α} : disjoint s t ↔ s.1.disjoint t.1 := disjoint_left theorem disjoint_iff_inter_eq_empty {s t : finset α} : disjoint s t ↔ s ∩ t = ∅ := disjoint_iff instance decidable_disjoint (U V : finset α) : decidable (disjoint U V) := decidable_of_decidable_of_iff (by apply_instance) eq_bot_iff theorem disjoint_right {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s := by rw [disjoint.comm, disjoint_left] theorem disjoint_iff_ne {s t : finset α} : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b := by simp only [disjoint_left, imp_not_comm, forall_eq'] theorem disjoint_of_subset_left {s t u : finset α} (h : s ⊆ u) (d : disjoint u t) : disjoint s t := disjoint_left.2 (λ x m₁, (disjoint_left.1 d) (h m₁)) theorem disjoint_of_subset_right {s t u : finset α} (h : t ⊆ u) (d : disjoint s u) : disjoint s t := disjoint_right.2 (λ x m₁, (disjoint_right.1 d) (h m₁)) @[simp] theorem disjoint_empty_left (s : finset α) : disjoint ∅ s := disjoint_bot_left @[simp] theorem disjoint_empty_right (s : finset α) : disjoint s ∅ := disjoint_bot_right @[simp] theorem singleton_disjoint {s : finset α} {a : α} : disjoint (singleton a) s ↔ a ∉ s := by simp only [disjoint_left, mem_singleton, forall_eq] @[simp] theorem disjoint_singleton {s : finset α} {a : α} : disjoint s (singleton a) ↔ a ∉ s := disjoint.comm.trans singleton_disjoint @[simp] theorem disjoint_insert_left {a : α} {s t : finset α} : disjoint (insert a s) t ↔ a ∉ t ∧ disjoint s t := by simp only [disjoint_left, mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] @[simp] theorem disjoint_insert_right {a : α} {s t : finset α} : disjoint s (insert a t) ↔ a ∉ s ∧ disjoint s t := disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm] @[simp] theorem disjoint_union_left {s t u : finset α} : disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u := by simp only [disjoint_left, mem_union, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_union_right {s t u : finset α} : disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u := by simp only [disjoint_right, mem_union, or_imp_distrib, forall_and_distrib] lemma sdiff_disjoint {s t : finset α} : disjoint (t \ s) s := disjoint_left.2 $ assume a ha, (mem_sdiff.1 ha).2 lemma disjoint_sdiff {s t : finset α} : disjoint s (t \ s) := sdiff_disjoint.symm lemma disjoint_sdiff_inter (s t : finset α) : disjoint (s \ t) (s ∩ t) := disjoint_of_subset_right (inter_subset_right _ _) sdiff_disjoint lemma sdiff_eq_self_iff_disjoint {s t : finset α} : s \ t = s ↔ disjoint s t := by rw [sdiff_eq_self, subset_empty, disjoint_iff_inter_eq_empty] lemma sdiff_eq_self_of_disjoint {s t : finset α} (h : disjoint s t) : s \ t = s := sdiff_eq_self_iff_disjoint.2 h lemma disjoint_self_iff_empty (s : finset α) : disjoint s s ↔ s = ∅ := disjoint_self lemma disjoint_bind_left {ι : Type*} (s : finset ι) (f : ι → finset α) (t : finset α) : disjoint (s.bind f) t ↔ (∀i∈s, disjoint (f i) t) := begin classical, refine s.induction _ _, { simp only [forall_mem_empty_iff, bind_empty, disjoint_empty_left] }, { assume i s his ih, simp only [disjoint_union_left, bind_insert, his, forall_mem_insert, ih] } end lemma disjoint_bind_right {ι : Type*} (s : finset α) (t : finset ι) (f : ι → finset α) : disjoint s (t.bind f) ↔ (∀i∈t, disjoint s (f i)) := by simpa only [disjoint.comm] using disjoint_bind_left t f s @[simp] theorem card_disjoint_union {s t : finset α} (h : disjoint s t) : card (s ∪ t) = card s + card t := by rw [← card_union_add_card_inter, disjoint_iff_inter_eq_empty.1 h, card_empty, add_zero] theorem card_sdiff {s t : finset α} (h : s ⊆ t) : card (t \ s) = card t - card s := suffices card (t \ s) = card ((t \ s) ∪ s) - card s, by rwa sdiff_union_of_subset h at this, by rw [card_disjoint_union sdiff_disjoint, nat.add_sub_cancel] lemma disjoint_filter {s : finset α} {p q : α → Prop} [decidable_pred p] [decidable_pred q] : disjoint (s.filter p) (s.filter q) ↔ (∀ x ∈ s, p x → ¬ q x) := by split; simp [disjoint_left] {contextual := tt} lemma disjoint_filter_filter {s t : finset α} {p q : α → Prop} [decidable_pred p] [decidable_pred q] : (disjoint s t) → disjoint (s.filter p) (t.filter q) := disjoint.mono (filter_subset _) (filter_subset _) lemma disjoint_iff_disjoint_coe {α : Type*} {a b : finset α} [decidable_eq α] : disjoint a b ↔ disjoint (↑a : set α) (↑b : set α) := by { rw [finset.disjoint_left, set.disjoint_left], refl } end disjoint /-- Given a set A and a set B inside it, we can shrink A to any appropriate size, and keep B inside it. -/ lemma exists_intermediate_set {A B : finset α} (i : ℕ) (h₁ : i + card B ≤ card A) (h₂ : B ⊆ A) : ∃ (C : finset α), B ⊆ C ∧ C ⊆ A ∧ card C = i + card B := begin classical, rcases nat.le.dest h₁ with ⟨k, _⟩, clear h₁, induction k with k ih generalizing A, { exact ⟨A, h₂, subset.refl _, h.symm⟩ }, { have : (A \ B).nonempty, { rw [← card_pos, card_sdiff h₂, ← h, nat.add_right_comm, nat.add_sub_cancel, nat.add_succ], apply nat.succ_pos }, rcases this with ⟨a, ha⟩, have z : i + card B + k = card (erase A a), { rw [card_erase_of_mem, ← h, nat.add_succ, nat.pred_succ], rw mem_sdiff at ha, exact ha.1 }, rcases ih _ z with ⟨B', hB', B'subA', cards⟩, { exact ⟨B', hB', trans B'subA' (erase_subset _ _), cards⟩ }, { rintros t th, apply mem_erase_of_ne_of_mem _ (h₂ th), rintro rfl, exact not_mem_sdiff_of_mem_right th ha } } end /-- We can shrink A to any smaller size. -/ lemma exists_smaller_set (A : finset α) (i : ℕ) (h₁ : i ≤ card A) : ∃ (B : finset α), B ⊆ A ∧ card B = i := let ⟨B, _, x₁, x₂⟩ := exists_intermediate_set i (by simpa) (empty_subset A) in ⟨B, x₁, x₂⟩ /-- `finset.fin_range k` is the finset `{0, 1, ..., k-1}`, as a `finset (fin k)`. -/ def fin_range (k : ℕ) : finset (fin k) := ⟨list.fin_range k, list.nodup_fin_range k⟩ @[simp] lemma fin_range_card {k : ℕ} : (fin_range k).card = k := by simp [fin_range] @[simp] lemma mem_fin_range {k : ℕ} (m : fin k) : m ∈ fin_range k := list.mem_fin_range m /-- Given a finset `s` of `ℕ` contained in `{0,..., n-1}`, the corresponding finset in `fin n` is `s.attach_fin h` where `h` is a proof that all elements of `s` are less than `n`. -/ def attach_fin (s : finset ℕ) {n : ℕ} (h : ∀ m ∈ s, m < n) : finset (fin n) := ⟨s.1.pmap (λ a ha, ⟨a, ha⟩) h, multiset.nodup_pmap (λ _ _ _ _, fin.mk.inj) s.2⟩ @[simp] lemma mem_attach_fin {n : ℕ} {s : finset ℕ} (h : ∀ m ∈ s, m < n) {a : fin n} : a ∈ s.attach_fin h ↔ a.1 ∈ s := ⟨λ h, let ⟨b, hb₁, hb₂⟩ := multiset.mem_pmap.1 h in hb₂ ▸ hb₁, λ h, multiset.mem_pmap.2 ⟨a.1, h, fin.eta _ _⟩⟩ @[simp] lemma card_attach_fin {n : ℕ} (s : finset ℕ) (h : ∀ m ∈ s, m < n) : (s.attach_fin h).card = s.card := multiset.card_pmap _ _ _ /-! ### choose -/ section choose variables (p : α → Prop) [decidable_pred p] (l : finset α) /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the corresponding subtype. -/ def choose_x (hp : (∃! a, a ∈ l ∧ p a)) : { a // a ∈ l ∧ p a } := multiset.choose_x p l.val hp /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the ambient type. -/ def choose (hp : ∃! a, a ∈ l ∧ p a) : α := choose_x p l hp lemma choose_spec (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (choose_x p l hp).property lemma choose_mem (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 lemma choose_property (hp : ∃! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end choose theorem lt_wf {α} : well_founded (@has_lt.lt (finset α) _) := have H : subrelation (@has_lt.lt (finset α) _) (inv_image (<) card), from λ x y hxy, card_lt_card hxy, subrelation.wf H $ inv_image.wf _ $ nat.lt_wf end finset namespace list variable [decidable_eq α] theorem to_finset_card_of_nodup {l : list α} (h : l.nodup) : l.to_finset.card = l.length := congr_arg card $ (@multiset.erase_dup_eq_self α _ l).2 h end list
e64a32006becb31513ed16d1e33c7c1c931fcb2d
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/probability/moments.lean
392a55356ec10c5e15365441920904fc18524f62
[ "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
15,653
lean
/- Copyright (c) 2022 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import probability.variance /-! # Moments and moment generating function ## Main definitions * `probability_theory.moment X p μ`: `p`th moment of a real random variable `X` with respect to measure `μ`, `μ[X^p]` * `probability_theory.central_moment X p μ`:`p`th central moment of `X` with respect to measure `μ`, `μ[(X - μ[X])^p]` * `probability_theory.mgf X μ t`: moment generating function of `X` with respect to measure `μ`, `μ[exp(t*X)]` * `probability_theory.cgf X μ t`: cumulant generating function, logarithm of the moment generating function ## Main results * `probability_theory.indep_fun.mgf_add`: if two real random variables `X` and `Y` are independent and their mgf are defined at `t`, then `mgf (X + Y) μ t = mgf X μ t * mgf Y μ t` * `probability_theory.indep_fun.cgf_add`: if two real random variables `X` and `Y` are independent and their mgf are defined at `t`, then `cgf (X + Y) μ t = cgf X μ t + cgf Y μ t` * `probability_theory.measure_ge_le_exp_cgf` and `probability_theory.measure_le_le_exp_cgf`: Chernoff bound on the upper (resp. lower) tail of a random variable. For `t` nonnegative such that the cgf exists, `ℙ(ε ≤ X) ≤ exp(- t*ε + cgf X ℙ t)`. See also `probability_theory.measure_ge_le_exp_mul_mgf` and `probability_theory.measure_le_le_exp_mul_mgf` for versions of these results using `mgf` instead of `cgf`. -/ open measure_theory filter finset real noncomputable theory open_locale big_operators measure_theory probability_theory ennreal nnreal namespace probability_theory variables {Ω ι : Type*} {m : measurable_space Ω} {X : Ω → ℝ} {p : ℕ} {μ : measure Ω} include m /-- Moment of a real random variable, `μ[X ^ p]`. -/ def moment (X : Ω → ℝ) (p : ℕ) (μ : measure Ω) : ℝ := μ[X ^ p] /-- Central moment of a real random variable, `μ[(X - μ[X]) ^ p]`. -/ def central_moment (X : Ω → ℝ) (p : ℕ) (μ : measure Ω) : ℝ := μ[(X - (λ x, μ[X])) ^ p] @[simp] lemma moment_zero (hp : p ≠ 0) : moment 0 p μ = 0 := by simp only [moment, hp, zero_pow', ne.def, not_false_iff, pi.zero_apply, integral_const, algebra.id.smul_eq_mul, mul_zero] @[simp] lemma central_moment_zero (hp : p ≠ 0) : central_moment 0 p μ = 0 := by simp only [central_moment, hp, pi.zero_apply, integral_const, algebra.id.smul_eq_mul, mul_zero, zero_sub, pi.pow_apply, pi.neg_apply, neg_zero, zero_pow', ne.def, not_false_iff] lemma central_moment_one' [is_finite_measure μ] (h_int : integrable X μ) : central_moment X 1 μ = (1 - (μ set.univ).to_real) * μ[X] := begin simp only [central_moment, pi.sub_apply, pow_one], rw integral_sub h_int (integrable_const _), simp only [sub_mul, integral_const, algebra.id.smul_eq_mul, one_mul], end @[simp] lemma central_moment_one [is_probability_measure μ] : central_moment X 1 μ = 0 := begin by_cases h_int : integrable X μ, { rw central_moment_one' h_int, simp only [measure_univ, ennreal.one_to_real, sub_self, zero_mul], }, { simp only [central_moment, pi.sub_apply, pow_one], have : ¬ integrable (λ x, X x - integral μ X) μ, { refine λ h_sub, h_int _, have h_add : X = (λ x, X x - integral μ X) + (λ x, integral μ X), { ext1 x, simp, }, rw h_add, exact h_sub.add (integrable_const _), }, rw integral_undef this, }, end lemma central_moment_two_eq_variance [is_finite_measure μ] (hX : mem_ℒp X 2 μ) : central_moment X 2 μ = variance X μ := by { rw hX.variance_eq, refl, } section moment_generating_function variables {t : ℝ} /-- Moment generating function of a real random variable `X`: `λ t, μ[exp(t*X)]`. -/ def mgf (X : Ω → ℝ) (μ : measure Ω) (t : ℝ) : ℝ := μ[λ ω, exp (t * X ω)] /-- Cumulant generating function of a real random variable `X`: `λ t, log μ[exp(t*X)]`. -/ def cgf (X : Ω → ℝ) (μ : measure Ω) (t : ℝ) : ℝ := log (mgf X μ t) @[simp] lemma mgf_zero_fun : mgf 0 μ t = (μ set.univ).to_real := by simp only [mgf, pi.zero_apply, mul_zero, exp_zero, integral_const, algebra.id.smul_eq_mul, mul_one] @[simp] lemma cgf_zero_fun : cgf 0 μ t = log (μ set.univ).to_real := by simp only [cgf, mgf_zero_fun] @[simp] lemma mgf_zero_measure : mgf X (0 : measure Ω) t = 0 := by simp only [mgf, integral_zero_measure] @[simp] lemma cgf_zero_measure : cgf X (0 : measure Ω) t = 0 := by simp only [cgf, log_zero, mgf_zero_measure] @[simp] lemma mgf_const' (c : ℝ) : mgf (λ _, c) μ t = (μ set.univ).to_real * exp (t * c) := by simp only [mgf, integral_const, algebra.id.smul_eq_mul] @[simp] lemma mgf_const (c : ℝ) [is_probability_measure μ] : mgf (λ _, c) μ t = exp (t * c) := by simp only [mgf_const', measure_univ, ennreal.one_to_real, one_mul] @[simp] lemma cgf_const' [is_finite_measure μ] (hμ : μ ≠ 0) (c : ℝ) : cgf (λ _, c) μ t = log (μ set.univ).to_real + t * c := begin simp only [cgf, mgf_const'], rw log_mul _ (exp_pos _).ne', { rw log_exp _, }, { rw [ne.def, ennreal.to_real_eq_zero_iff, measure.measure_univ_eq_zero], simp only [hμ, measure_ne_top μ set.univ, or_self, not_false_iff], }, end @[simp] lemma cgf_const [is_probability_measure μ] (c : ℝ) : cgf (λ _, c) μ t = t * c := by simp only [cgf, mgf_const, log_exp] @[simp] lemma mgf_zero' : mgf X μ 0 = (μ set.univ).to_real := by simp only [mgf, zero_mul, exp_zero, integral_const, algebra.id.smul_eq_mul, mul_one] @[simp] lemma mgf_zero [is_probability_measure μ] : mgf X μ 0 = 1 := by simp only [mgf_zero', measure_univ, ennreal.one_to_real] @[simp] lemma cgf_zero' : cgf X μ 0 = log (μ set.univ).to_real := by simp only [cgf, mgf_zero'] @[simp] lemma cgf_zero [is_probability_measure μ] : cgf X μ 0 = 0 := by simp only [cgf_zero', measure_univ, ennreal.one_to_real, log_one] lemma mgf_undef (hX : ¬ integrable (λ ω, exp (t * X ω)) μ) : mgf X μ t = 0 := by simp only [mgf, integral_undef hX] lemma cgf_undef (hX : ¬ integrable (λ ω, exp (t * X ω)) μ) : cgf X μ t = 0 := by simp only [cgf, mgf_undef hX, log_zero] lemma mgf_nonneg : 0 ≤ mgf X μ t := begin refine integral_nonneg _, intro ω, simp only [pi.zero_apply], exact (exp_pos _).le, end lemma mgf_pos' (hμ : μ ≠ 0) (h_int_X : integrable (λ ω, exp (t * X ω)) μ) : 0 < mgf X μ t := begin simp_rw mgf, have : ∫ (x : Ω), exp (t * X x) ∂μ = ∫ (x : Ω) in set.univ, exp (t * X x) ∂μ, { simp only [measure.restrict_univ], }, rw [this, set_integral_pos_iff_support_of_nonneg_ae _ _], { have h_eq_univ : function.support (λ (x : Ω), exp (t * X x)) = set.univ, { ext1 x, simp only [function.mem_support, set.mem_univ, iff_true], exact (exp_pos _).ne', }, rw [h_eq_univ, set.inter_univ _], refine ne.bot_lt _, simp only [hμ, ennreal.bot_eq_zero, ne.def, measure.measure_univ_eq_zero, not_false_iff], }, { refine eventually_of_forall (λ x, _), rw pi.zero_apply, exact (exp_pos _).le, }, { rwa integrable_on_univ, }, end lemma mgf_pos [is_probability_measure μ] (h_int_X : integrable (λ ω, exp (t * X ω)) μ) : 0 < mgf X μ t := mgf_pos' (is_probability_measure.ne_zero μ) h_int_X lemma mgf_neg : mgf (-X) μ t = mgf X μ (-t) := by simp_rw [mgf, pi.neg_apply, mul_neg, neg_mul] lemma cgf_neg : cgf (-X) μ t = cgf X μ (-t) := by simp_rw [cgf, mgf_neg] /-- This is a trivial application of `indep_fun.comp` but it will come up frequently. -/ lemma indep_fun.exp_mul {X Y : Ω → ℝ} (h_indep : indep_fun X Y μ) (s t : ℝ) : indep_fun (λ ω, exp (s * X ω)) (λ ω, exp (t * Y ω)) μ := begin have h_meas : ∀ t, measurable (λ x, exp (t * x)) := λ t, (measurable_id'.const_mul t).exp, change indep_fun ((λ x, exp (s * x)) ∘ X) ((λ x, exp (t * x)) ∘ Y) μ, exact indep_fun.comp h_indep (h_meas s) (h_meas t), end lemma indep_fun.mgf_add {X Y : Ω → ℝ} (h_indep : indep_fun X Y μ) (hX : ae_strongly_measurable (λ ω, exp (t * X ω)) μ) (hY : ae_strongly_measurable (λ ω, exp (t * Y ω)) μ) : mgf (X + Y) μ t = mgf X μ t * mgf Y μ t := begin simp_rw [mgf, pi.add_apply, mul_add, exp_add], exact (h_indep.exp_mul t t).integral_mul hX hY, end lemma indep_fun.mgf_add' {X Y : Ω → ℝ} (h_indep : indep_fun X Y μ) (hX : ae_strongly_measurable X μ) (hY : ae_strongly_measurable Y μ) : mgf (X + Y) μ t = mgf X μ t * mgf Y μ t := begin have A : continuous (λ (x : ℝ), exp (t * x)), by continuity, have h'X : ae_strongly_measurable (λ ω, exp (t * X ω)) μ := A.ae_strongly_measurable.comp_ae_measurable hX.ae_measurable, have h'Y : ae_strongly_measurable (λ ω, exp (t * Y ω)) μ := A.ae_strongly_measurable.comp_ae_measurable hY.ae_measurable, exact h_indep.mgf_add h'X h'Y end lemma indep_fun.cgf_add {X Y : Ω → ℝ} (h_indep : indep_fun X Y μ) (h_int_X : integrable (λ ω, exp (t * X ω)) μ) (h_int_Y : integrable (λ ω, exp (t * Y ω)) μ) : cgf (X + Y) μ t = cgf X μ t + cgf Y μ t := begin by_cases hμ : μ = 0, { simp [hμ], }, simp only [cgf, h_indep.mgf_add h_int_X.ae_strongly_measurable h_int_Y.ae_strongly_measurable], exact log_mul (mgf_pos' hμ h_int_X).ne' (mgf_pos' hμ h_int_Y).ne', end lemma ae_strongly_measurable_exp_mul_add {X Y : Ω → ℝ} (h_int_X : ae_strongly_measurable (λ ω, exp (t * X ω)) μ) (h_int_Y : ae_strongly_measurable (λ ω, exp (t * Y ω)) μ) : ae_strongly_measurable (λ ω, exp (t * (X + Y) ω)) μ := begin simp_rw [pi.add_apply, mul_add, exp_add], exact ae_strongly_measurable.mul h_int_X h_int_Y, end lemma ae_strongly_measurable_exp_mul_sum {X : ι → Ω → ℝ} {s : finset ι} (h_int : ∀ i ∈ s, ae_strongly_measurable (λ ω, exp (t * X i ω)) μ) : ae_strongly_measurable (λ ω, exp (t * (∑ i in s, X i) ω)) μ := begin classical, induction s using finset.induction_on with i s hi_notin_s h_rec h_int, { simp only [pi.zero_apply, sum_apply, sum_empty, mul_zero, exp_zero], exact ae_strongly_measurable_const, }, { have : ∀ (i : ι), i ∈ s → ae_strongly_measurable (λ (ω : Ω), exp (t * X i ω)) μ, from λ i hi, h_int i (mem_insert_of_mem hi), specialize h_rec this, rw sum_insert hi_notin_s, apply ae_strongly_measurable_exp_mul_add (h_int i (mem_insert_self _ _)) h_rec } end lemma indep_fun.integrable_exp_mul_add {X Y : Ω → ℝ} (h_indep : indep_fun X Y μ) (h_int_X : integrable (λ ω, exp (t * X ω)) μ) (h_int_Y : integrable (λ ω, exp (t * Y ω)) μ) : integrable (λ ω, exp (t * (X + Y) ω)) μ := begin simp_rw [pi.add_apply, mul_add, exp_add], exact (h_indep.exp_mul t t).integrable_mul h_int_X h_int_Y, end lemma Indep_fun.integrable_exp_mul_sum [is_probability_measure μ] {X : ι → Ω → ℝ} (h_indep : Indep_fun (λ i, infer_instance) X μ) (h_meas : ∀ i, measurable (X i)) {s : finset ι} (h_int : ∀ i ∈ s, integrable (λ ω, exp (t * X i ω)) μ) : integrable (λ ω, exp (t * (∑ i in s, X i) ω)) μ := begin classical, induction s using finset.induction_on with i s hi_notin_s h_rec h_int, { simp only [pi.zero_apply, sum_apply, sum_empty, mul_zero, exp_zero], exact integrable_const _, }, { have : ∀ (i : ι), i ∈ s → integrable (λ (ω : Ω), exp (t * X i ω)) μ, from λ i hi, h_int i (mem_insert_of_mem hi), specialize h_rec this, rw sum_insert hi_notin_s, refine indep_fun.integrable_exp_mul_add _ (h_int i (mem_insert_self _ _)) h_rec, exact (h_indep.indep_fun_finset_sum_of_not_mem h_meas hi_notin_s).symm, }, end lemma Indep_fun.mgf_sum [is_probability_measure μ] {X : ι → Ω → ℝ} (h_indep : Indep_fun (λ i, infer_instance) X μ) (h_meas : ∀ i, measurable (X i)) (s : finset ι) : mgf (∑ i in s, X i) μ t = ∏ i in s, mgf (X i) μ t := begin classical, induction s using finset.induction_on with i s hi_notin_s h_rec h_int, { simp only [sum_empty, mgf_zero_fun, measure_univ, ennreal.one_to_real, prod_empty], }, { have h_int' : ∀ (i : ι), ae_strongly_measurable (λ (ω : Ω), exp (t * X i ω)) μ, from λ i, ((h_meas i).const_mul t).exp.ae_strongly_measurable, rw [sum_insert hi_notin_s, indep_fun.mgf_add (h_indep.indep_fun_finset_sum_of_not_mem h_meas hi_notin_s).symm (h_int' i) (ae_strongly_measurable_exp_mul_sum (λ i hi, h_int' i)), h_rec, prod_insert hi_notin_s] } end lemma Indep_fun.cgf_sum [is_probability_measure μ] {X : ι → Ω → ℝ} (h_indep : Indep_fun (λ i, infer_instance) X μ) (h_meas : ∀ i, measurable (X i)) {s : finset ι} (h_int : ∀ i ∈ s, integrable (λ ω, exp (t * X i ω)) μ) : cgf (∑ i in s, X i) μ t = ∑ i in s, cgf (X i) μ t := begin simp_rw cgf, rw ← log_prod _ _ (λ j hj, _), { rw h_indep.mgf_sum h_meas }, { exact (mgf_pos (h_int j hj)).ne', }, end /-- **Chernoff bound** on the upper tail of a real random variable. -/ lemma measure_ge_le_exp_mul_mgf [is_finite_measure μ] (ε : ℝ) (ht : 0 ≤ t) (h_int : integrable (λ ω, exp (t * X ω)) μ) : (μ {ω | ε ≤ X ω}).to_real ≤ exp (- t * ε) * mgf X μ t := begin cases ht.eq_or_lt with ht_zero_eq ht_pos, { rw ht_zero_eq.symm, simp only [neg_zero, zero_mul, exp_zero, mgf_zero', one_mul], rw ennreal.to_real_le_to_real (measure_ne_top μ _) (measure_ne_top μ _), exact measure_mono (set.subset_univ _), }, calc (μ {ω | ε ≤ X ω}).to_real = (μ {ω | exp (t * ε) ≤ exp (t * X ω)}).to_real : begin congr' with ω, simp only [exp_le_exp, eq_iff_iff], exact ⟨λ h, mul_le_mul_of_nonneg_left h ht_pos.le, λ h, le_of_mul_le_mul_left h ht_pos⟩, end ... ≤ (exp (t * ε))⁻¹ * μ[λ ω, exp (t * X ω)] : begin have : exp (t * ε) * (μ {ω | exp (t * ε) ≤ exp (t * X ω)}).to_real ≤ μ[λ ω, exp (t * X ω)], from mul_meas_ge_le_integral_of_nonneg (λ x, (exp_pos _).le) h_int _, rwa [mul_comm (exp (t * ε))⁻¹, ← div_eq_mul_inv, le_div_iff' (exp_pos _)], end ... = exp (- t * ε) * mgf X μ t : by { rw [neg_mul, exp_neg], refl, }, end /-- **Chernoff bound** on the lower tail of a real random variable. -/ lemma measure_le_le_exp_mul_mgf [is_finite_measure μ] (ε : ℝ) (ht : t ≤ 0) (h_int : integrable (λ ω, exp (t * X ω)) μ) : (μ {ω | X ω ≤ ε}).to_real ≤ exp (- t * ε) * mgf X μ t := begin rw [← neg_neg t, ← mgf_neg, neg_neg, ← neg_mul_neg (-t)], refine eq.trans_le _ (measure_ge_le_exp_mul_mgf (-ε) (neg_nonneg.mpr ht) _), { congr' with ω, simp only [pi.neg_apply, neg_le_neg_iff], }, { simp_rw [pi.neg_apply, neg_mul_neg], exact h_int, }, end /-- **Chernoff bound** on the upper tail of a real random variable. -/ lemma measure_ge_le_exp_cgf [is_finite_measure μ] (ε : ℝ) (ht : 0 ≤ t) (h_int : integrable (λ ω, exp (t * X ω)) μ) : (μ {ω | ε ≤ X ω}).to_real ≤ exp (- t * ε + cgf X μ t) := begin refine (measure_ge_le_exp_mul_mgf ε ht h_int).trans _, rw exp_add, exact mul_le_mul le_rfl (le_exp_log _) mgf_nonneg (exp_pos _).le, end /-- **Chernoff bound** on the lower tail of a real random variable. -/ lemma measure_le_le_exp_cgf [is_finite_measure μ] (ε : ℝ) (ht : t ≤ 0) (h_int : integrable (λ ω, exp (t * X ω)) μ) : (μ {ω | X ω ≤ ε}).to_real ≤ exp (- t * ε + cgf X μ t) := begin refine (measure_le_le_exp_mul_mgf ε ht h_int).trans _, rw exp_add, exact mul_le_mul le_rfl (le_exp_log _) mgf_nonneg (exp_pos _).le, end end moment_generating_function end probability_theory
5e1b7c847e4c897ecefdf63d14a00ab4fdf03bf8
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/category_theory/closed/cartesian.lean
0de88df22c366f6dd5f0202e65bfa9d6998f24b5
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,740
lean
/- Copyright (c) 2020 Bhavik Mehta, Edward Ayers, Thomas Read. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Edward Ayers, Thomas Read -/ import category_theory.limits.shapes.finite_products import category_theory.limits.preserves.shapes.binary_products import category_theory.closed.monoidal import category_theory.monoidal.of_has_finite_products import category_theory.adjunction import category_theory.adjunction.mates import category_theory.epi_mono /-! # Cartesian closed categories Given a category with finite products, the cartesian monoidal structure is provided by the local instance `monoidal_of_has_finite_products`. We define exponentiable objects to be closed objects with respect to this monoidal structure, i.e. `(X × -)` is a left adjoint. We say a category is cartesian closed if every object is exponentiable (equivalently, that the category equipped with the cartesian monoidal structure is closed monoidal). Show that exponential forms a difunctor and define the exponential comparison morphisms. ## TODO Some of the results here are true more generally for closed objects and for closed monoidal categories, and these could be generalised. -/ universes v u u₂ noncomputable theory namespace category_theory open category_theory category_theory.category category_theory.limits local attribute [instance] monoidal_of_has_finite_products /-- An object `X` is *exponentiable* if `(X × -)` is a left adjoint. We define this as being `closed` in the cartesian monoidal structure. -/ abbreviation exponentiable {C : Type u} [category.{v} C] [has_finite_products C] (X : C) := closed X /-- If `X` and `Y` are exponentiable then `X ⨯ Y` is. This isn't an instance because it's not usually how we want to construct exponentials, we'll usually prove all objects are exponential uniformly. -/ def binary_product_exponentiable {C : Type u} [category.{v} C] [has_finite_products C] {X Y : C} (hX : exponentiable X) (hY : exponentiable Y) : exponentiable (X ⨯ Y) := { is_adj := begin haveI := hX.is_adj, haveI := hY.is_adj, exact adjunction.left_adjoint_of_nat_iso (monoidal_category.tensor_left_tensor _ _).symm end } /-- The terminal object is always exponentiable. This isn't an instance because most of the time we'll prove cartesian closed for all objects at once, rather than just for this one. -/ def terminal_exponentiable {C : Type u} [category.{v} C] [has_finite_products C] : exponentiable ⊤_ C := unit_closed /-- A category `C` is cartesian closed if it has finite products and every object is exponentiable. We define this as `monoidal_closed` with respect to the cartesian monoidal structure. -/ abbreviation cartesian_closed (C : Type u) [category.{v} C] [has_finite_products C] := monoidal_closed C variables {C : Type u} [category.{v} C] (A B : C) {X X' Y Y' Z : C} section exp variables [has_finite_products C] [exponentiable A] /-- This is (-)^A. -/ def exp : C ⥤ C := (@closed.is_adj _ _ _ A _).right /-- The adjunction between A ⨯ - and (-)^A. -/ def exp.adjunction : prod.functor.obj A ⊣ exp A := closed.is_adj.adj /-- The evaluation natural transformation. -/ def ev : exp A ⋙ prod.functor.obj A ⟶ 𝟭 C := (exp.adjunction A).counit /-- The coevaluation natural transformation. -/ def coev : 𝟭 C ⟶ prod.functor.obj A ⋙ exp A := (exp.adjunction A).unit @[simp] lemma exp_adjunction_counit : (exp.adjunction A).counit = ev A := rfl @[simp] lemma exp_adjunction_unit : (exp.adjunction A).unit = coev A := rfl @[simp, reassoc] lemma ev_naturality {X Y : C} (f : X ⟶ Y) : limits.prod.map (𝟙 A) ((exp A).map f) ≫ (ev A).app Y = (ev A).app X ≫ f := (ev A).naturality f @[simp, reassoc] lemma coev_naturality {X Y : C} (f : X ⟶ Y) : f ≫ (coev A).app Y = (coev A).app X ≫ (exp A).map (limits.prod.map (𝟙 A) f) := (coev A).naturality f notation A ` ⟹ `:20 B:20 := (exp A).obj B notation B ` ^^ `:30 A:30 := (exp A).obj B @[simp, reassoc] lemma ev_coev : limits.prod.map (𝟙 A) ((coev A).app B) ≫ (ev A).app (A ⨯ B) = 𝟙 (A ⨯ B) := adjunction.left_triangle_components (exp.adjunction A) @[simp, reassoc] lemma coev_ev : (coev A).app (A⟹B) ≫ (exp A).map ((ev A).app B) = 𝟙 (A⟹B) := adjunction.right_triangle_components (exp.adjunction A) instance : preserves_colimits (prod.functor.obj A) := (exp.adjunction A).left_adjoint_preserves_colimits end exp variables {A} -- Wrap these in a namespace so we don't clash with the core versions. namespace cartesian_closed variables [has_finite_products C] [exponentiable A] /-- Currying in a cartesian closed category. -/ def curry : (A ⨯ Y ⟶ X) → (Y ⟶ A ⟹ X) := (exp.adjunction A).hom_equiv _ _ /-- Uncurrying in a cartesian closed category. -/ def uncurry : (Y ⟶ A ⟹ X) → (A ⨯ Y ⟶ X) := ((exp.adjunction A).hom_equiv _ _).symm @[simp] lemma hom_equiv_apply_eq (f : A ⨯ Y ⟶ X) : (exp.adjunction A).hom_equiv _ _ f = curry f := rfl @[simp] lemma hom_equiv_symm_apply_eq (f : Y ⟶ A ⟹ X) : ((exp.adjunction A).hom_equiv _ _).symm f = uncurry f := rfl end cartesian_closed open cartesian_closed variables [has_finite_products C] [exponentiable A] @[reassoc] lemma curry_natural_left (f : X ⟶ X') (g : A ⨯ X' ⟶ Y) : curry (limits.prod.map (𝟙 _) f ≫ g) = f ≫ curry g := adjunction.hom_equiv_naturality_left _ _ _ @[reassoc] lemma curry_natural_right (f : A ⨯ X ⟶ Y) (g : Y ⟶ Y') : curry (f ≫ g) = curry f ≫ (exp _).map g := adjunction.hom_equiv_naturality_right _ _ _ @[reassoc] lemma uncurry_natural_right (f : X ⟶ A⟹Y) (g : Y ⟶ Y') : uncurry (f ≫ (exp _).map g) = uncurry f ≫ g := adjunction.hom_equiv_naturality_right_symm _ _ _ @[reassoc] lemma uncurry_natural_left (f : X ⟶ X') (g : X' ⟶ A⟹Y) : uncurry (f ≫ g) = limits.prod.map (𝟙 _) f ≫ uncurry g := adjunction.hom_equiv_naturality_left_symm _ _ _ @[simp] lemma uncurry_curry (f : A ⨯ X ⟶ Y) : uncurry (curry f) = f := (closed.is_adj.adj.hom_equiv _ _).left_inv f @[simp] lemma curry_uncurry (f : X ⟶ A⟹Y) : curry (uncurry f) = f := (closed.is_adj.adj.hom_equiv _ _).right_inv f lemma curry_eq_iff (f : A ⨯ Y ⟶ X) (g : Y ⟶ A ⟹ X) : curry f = g ↔ f = uncurry g := adjunction.hom_equiv_apply_eq _ f g lemma eq_curry_iff (f : A ⨯ Y ⟶ X) (g : Y ⟶ A ⟹ X) : g = curry f ↔ uncurry g = f := adjunction.eq_hom_equiv_apply _ f g -- I don't think these two should be simp. lemma uncurry_eq (g : Y ⟶ A ⟹ X) : uncurry g = limits.prod.map (𝟙 A) g ≫ (ev A).app X := adjunction.hom_equiv_counit _ lemma curry_eq (g : A ⨯ Y ⟶ X) : curry g = (coev A).app Y ≫ (exp A).map g := adjunction.hom_equiv_unit _ lemma uncurry_id_eq_ev (A X : C) [exponentiable A] : uncurry (𝟙 (A ⟹ X)) = (ev A).app X := by rw [uncurry_eq, prod.map_id_id, id_comp] lemma curry_id_eq_coev (A X : C) [exponentiable A] : curry (𝟙 _) = (coev A).app X := by { rw [curry_eq, (exp A).map_id (A ⨯ _)], apply comp_id } lemma curry_injective : function.injective (curry : (A ⨯ Y ⟶ X) → (Y ⟶ A ⟹ X)) := (closed.is_adj.adj.hom_equiv _ _).injective lemma uncurry_injective : function.injective (uncurry : (Y ⟶ A ⟹ X) → (A ⨯ Y ⟶ X)) := (closed.is_adj.adj.hom_equiv _ _).symm.injective /-- Show that the exponential of the terminal object is isomorphic to itself, i.e. `X^1 ≅ X`. The typeclass argument is explicit: any instance can be used. -/ def exp_terminal_iso_self [exponentiable ⊤_ C] : (⊤_ C ⟹ X) ≅ X := yoneda.ext (⊤_ C ⟹ X) X (λ Y f, (prod.left_unitor Y).inv ≫ uncurry f) (λ Y f, curry ((prod.left_unitor Y).hom ≫ f)) (λ Z g, by rw [curry_eq_iff, iso.hom_inv_id_assoc] ) (λ Z g, by simp) (λ Z W f g, by rw [uncurry_natural_left, prod.left_unitor_inv_naturality_assoc f] ) /-- The internal element which points at the given morphism. -/ def internalize_hom (f : A ⟶ Y) : ⊤_ C ⟶ (A ⟹ Y) := curry (limits.prod.fst ≫ f) section pre variables {B} /-- Pre-compose an internal hom with an external hom. -/ def pre (f : B ⟶ A) [exponentiable B] : exp A ⟶ exp B := transfer_nat_trans_self (exp.adjunction _) (exp.adjunction _) (prod.functor.map f) lemma prod_map_pre_app_comp_ev (f : B ⟶ A) [exponentiable B] (X : C) : limits.prod.map (𝟙 B) ((pre f).app X) ≫ (ev B).app X = limits.prod.map f (𝟙 (A ⟹ X)) ≫ (ev A).app X := transfer_nat_trans_self_counit _ _ (prod.functor.map f) X lemma uncurry_pre (f : B ⟶ A) [exponentiable B] (X : C) : uncurry ((pre f).app X) = limits.prod.map f (𝟙 _) ≫ (ev A).app X := begin rw [uncurry_eq, prod_map_pre_app_comp_ev] end lemma coev_app_comp_pre_app (f : B ⟶ A) [exponentiable B] : (coev A).app X ≫ (pre f).app (A ⨯ X) = (coev B).app X ≫ (exp B).map (limits.prod.map f (𝟙 _)) := unit_transfer_nat_trans_self _ _ (prod.functor.map f) X @[simp] lemma pre_id (A : C) [exponentiable A] : pre (𝟙 A) = 𝟙 _ := by simp [pre] @[simp] lemma pre_map {A₁ A₂ A₃ : C} [exponentiable A₁] [exponentiable A₂] [exponentiable A₃] (f : A₁ ⟶ A₂) (g : A₂ ⟶ A₃) : pre (f ≫ g) = pre g ≫ pre f := by rw [pre, pre, pre, transfer_nat_trans_self_comp, prod.functor.map_comp] end pre /-- The internal hom functor given by the cartesian closed structure. -/ def internal_hom [cartesian_closed C] : Cᵒᵖ ⥤ C ⥤ C := { obj := λ X, exp X.unop, map := λ X Y f, pre f.unop } /-- If an initial object `I` exists in a CCC, then `A ⨯ I ≅ I`. -/ @[simps] def zero_mul {I : C} (t : is_initial I) : A ⨯ I ≅ I := { hom := limits.prod.snd, inv := t.to _, hom_inv_id' := begin have: (limits.prod.snd : A ⨯ I ⟶ I) = uncurry (t.to _), rw ← curry_eq_iff, apply t.hom_ext, rw [this, ← uncurry_natural_right, ← eq_curry_iff], apply t.hom_ext, end, inv_hom_id' := t.hom_ext _ _ } /-- If an initial object `0` exists in a CCC, then `0 ⨯ A ≅ 0`. -/ def mul_zero {I : C} (t : is_initial I) : I ⨯ A ≅ I := limits.prod.braiding _ _ ≪≫ zero_mul t /-- If an initial object `0` exists in a CCC then `0^B ≅ 1` for any `B`. -/ def pow_zero {I : C} (t : is_initial I) [cartesian_closed C] : I ⟹ B ≅ ⊤_ C := { hom := default _, inv := curry ((mul_zero t).hom ≫ t.to _), hom_inv_id' := begin rw [← curry_natural_left, curry_eq_iff, ← cancel_epi (mul_zero t).inv], { apply t.hom_ext }, { apply_instance }, { apply_instance } end } -- TODO: Generalise the below to its commutated variants. -- TODO: Define a distributive category, so that zero_mul and friends can be derived from this. /-- In a CCC with binary coproducts, the distribution morphism is an isomorphism. -/ def prod_coprod_distrib [has_binary_coproducts C] [cartesian_closed C] (X Y Z : C) : (Z ⨯ X) ⨿ (Z ⨯ Y) ≅ Z ⨯ (X ⨿ Y) := { hom := coprod.desc (limits.prod.map (𝟙 _) coprod.inl) (limits.prod.map (𝟙 _) coprod.inr), inv := uncurry (coprod.desc (curry coprod.inl) (curry coprod.inr)), hom_inv_id' := begin apply coprod.hom_ext, rw [coprod.inl_desc_assoc, comp_id, ←uncurry_natural_left, coprod.inl_desc, uncurry_curry], rw [coprod.inr_desc_assoc, comp_id, ←uncurry_natural_left, coprod.inr_desc, uncurry_curry], end, inv_hom_id' := begin rw [← uncurry_natural_right, ←eq_curry_iff], apply coprod.hom_ext, rw [coprod.inl_desc_assoc, ←curry_natural_right, coprod.inl_desc, ←curry_natural_left, comp_id], rw [coprod.inr_desc_assoc, ←curry_natural_right, coprod.inr_desc, ←curry_natural_left, comp_id], end } /-- If an initial object `I` exists in a CCC then it is a strict initial object, i.e. any morphism to `I` is an iso. This actually shows a slightly stronger version: any morphism to an initial object from an exponentiable object is an isomorphism. -/ lemma strict_initial {I : C} (t : is_initial I) (f : A ⟶ I) : is_iso f := begin haveI : mono (limits.prod.lift (𝟙 A) f ≫ (zero_mul t).hom) := mono_comp _ _, rw [zero_mul_hom, prod.lift_snd] at _inst, haveI: split_epi f := ⟨t.to _, t.hom_ext _ _⟩, apply is_iso_of_mono_of_split_epi end instance to_initial_is_iso [has_initial C] (f : A ⟶ ⊥_ C) : is_iso f := strict_initial initial_is_initial _ /-- If an initial object `0` exists in a CCC then every morphism from it is monic. -/ lemma initial_mono {I : C} (B : C) (t : is_initial I) [cartesian_closed C] : mono (t.to B) := ⟨λ B g h _, begin haveI := strict_initial t g, haveI := strict_initial t h, exact eq_of_inv_eq_inv (t.hom_ext _ _) end⟩ instance initial.mono_to [has_initial C] (B : C) [cartesian_closed C] : mono (initial.to B) := initial_mono B initial_is_initial variables {D : Type u₂} [category.{v} D] section functor variables [has_finite_products D] /-- Transport the property of being cartesian closed across an equivalence of categories. Note we didn't require any coherence between the choice of finite products here, since we transport along the `prod_comparison` isomorphism. -/ def cartesian_closed_of_equiv (e : C ≌ D) [h : cartesian_closed C] : cartesian_closed D := { closed := λ X, { is_adj := begin haveI q : exponentiable (e.inverse.obj X) := infer_instance, have : is_left_adjoint (prod.functor.obj (e.inverse.obj X)) := q.is_adj, have : e.functor ⋙ prod.functor.obj X ⋙ e.inverse ≅ prod.functor.obj (e.inverse.obj X), apply nat_iso.of_components _ _, intro Y, { apply as_iso (prod_comparison e.inverse X (e.functor.obj Y)) ≪≫ _, apply prod.map_iso (iso.refl _) (e.unit_iso.app Y).symm }, { intros Y Z g, dsimp [prod_comparison], simp [prod.comp_lift, ← e.inverse.map_comp, ← e.inverse.map_comp_assoc], -- I wonder if it would be a good idea to make `map_comp` a simp lemma the other way round dsimp, simp }, -- See note [dsimp, simp] { have : is_left_adjoint (e.functor ⋙ prod.functor.obj X ⋙ e.inverse) := by exactI adjunction.left_adjoint_of_nat_iso this.symm, have : is_left_adjoint (e.inverse ⋙ e.functor ⋙ prod.functor.obj X ⋙ e.inverse) := by exactI adjunction.left_adjoint_of_comp e.inverse _, have : (e.inverse ⋙ e.functor ⋙ prod.functor.obj X ⋙ e.inverse) ⋙ e.functor ≅ prod.functor.obj X, { apply iso_whisker_right e.counit_iso (prod.functor.obj X ⋙ e.inverse ⋙ e.functor) ≪≫ _, change prod.functor.obj X ⋙ e.inverse ⋙ e.functor ≅ prod.functor.obj X, apply iso_whisker_left (prod.functor.obj X) e.counit_iso, }, resetI, apply adjunction.left_adjoint_of_nat_iso this }, end } } end functor end category_theory
6d141ff7ee08a38dd516beecd4853c9bf91d9d99
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/category/ulift.lean
f1ee02a5c9c96358cf4110c75f4014ab2b52d2af
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
6,010
lean
/- Copyright (c) 2021 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz -/ import category_theory.category.basic import category_theory.equivalence import category_theory.eq_to_hom /-! # Basic API for ulift > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file contains a very basic API for working with the categorical instance on `ulift C` where `C` is a type with a category instance. 1. `category_theory.ulift.up` is the functorial version of the usual `ulift.up`. 2. `category_theory.ulift.down` is the functorial version of the usual `ulift.down`. 3. `category_theory.ulift.equivalence` is the categorical equivalence between `C` and `ulift C`. # ulift_hom Given a type `C : Type u`, `ulift_hom.{w} C` is just an alias for `C`. If we have `category.{v} C`, then `ulift_hom.{w} C` is endowed with a category instance whose morphisms are obtained by applying `ulift.{w}` to the morphisms from `C`. This is a category equivalent to `C`. The forward direction of the equivalence is `ulift_hom.up`, the backward direction is `ulift_hom.donw` and the equivalence is `ulift_hom.equiv`. # as_small This file also contains a construction which takes a type `C : Type u` with a category instance `category.{v} C` and makes a small category `as_small.{w} C : Type (max w v u)` equivalent to `C`. The forward direction of the equivalence, `C ⥤ as_small C`, is denoted `as_small.up` and the backward direction is `as_small.down`. The equivalence itself is `as_small.equiv`. -/ universes w₁ v₁ v₂ u₁ u₂ namespace category_theory variables {C : Type u₁} [category.{v₁} C] /-- The functorial version of `ulift.up`. -/ @[simps] def ulift.up_functor : C ⥤ (ulift.{u₂} C) := { obj := ulift.up, map := λ X Y f, f } /-- The functorial version of `ulift.down`. -/ @[simps] def ulift.down_functor : (ulift.{u₂} C) ⥤ C := { obj := ulift.down, map := λ X Y f, f } /-- The categorical equivalence between `C` and `ulift C`. -/ @[simps] def ulift.equivalence : C ≌ (ulift.{u₂} C) := { functor := ulift.up_functor, inverse := ulift.down_functor, unit_iso := { hom := 𝟙 _, inv := 𝟙 _ }, counit_iso := { hom := { app := λ X, 𝟙 _, naturality' := λ X Y f, by {change f ≫ 𝟙 _ = 𝟙 _ ≫ f, simp} }, inv := { app := λ X, 𝟙 _, naturality' := λ X Y f, by {change f ≫ 𝟙 _ = 𝟙 _ ≫ f, simp} }, hom_inv_id' := by {ext, change (𝟙 _) ≫ (𝟙 _) = 𝟙 _, simp}, inv_hom_id' := by {ext, change (𝟙 _) ≫ (𝟙 _) = 𝟙 _, simp} }, functor_unit_iso_comp' := λ X, by {change (𝟙 X) ≫ (𝟙 X) = 𝟙 X, simp} } section ulift_hom /-- `ulift_hom.{w} C` is an alias for `C`, which is endowed with a category instance whose morphisms are obtained by applying `ulift.{w}` to the morphisms from `C`. -/ def {w u} ulift_hom (C : Type u) := C instance {C} [inhabited C] : inhabited (ulift_hom C) := ⟨(arbitrary C : C)⟩ /-- The obvious function `ulift_hom C → C`. -/ def ulift_hom.obj_down {C} (A : ulift_hom C) : C := A /-- The obvious function `C → ulift_hom C`. -/ def ulift_hom.obj_up {C} (A : C) : ulift_hom C := A @[simp] lemma obj_down_obj_up {C} (A : C) : (ulift_hom.obj_up A).obj_down = A := rfl @[simp] lemma obj_up_obj_down {C} (A : ulift_hom C) : ulift_hom.obj_up A.obj_down = A := rfl instance : category.{max v₂ v₁} (ulift_hom.{v₂} C) := { hom := λ A B, ulift.{v₂} $ A.obj_down ⟶ B.obj_down, id := λ A, ⟨𝟙 _⟩, comp := λ A B C f g, ⟨f.down ≫ g.down⟩} /-- One half of the quivalence between `C` and `ulift_hom C`. -/ @[simps] def ulift_hom.up : C ⥤ ulift_hom C := { obj := ulift_hom.obj_up, map := λ X Y f, ⟨f⟩ } /-- One half of the quivalence between `C` and `ulift_hom C`. -/ @[simps] def ulift_hom.down : ulift_hom C ⥤ C := { obj := ulift_hom.obj_down, map := λ X Y f, f.down } /-- The equivalence between `C` and `ulift_hom C`. -/ def ulift_hom.equiv : C ≌ ulift_hom C := { functor := ulift_hom.up, inverse := ulift_hom.down, unit_iso := nat_iso.of_components (λ A, eq_to_iso rfl) (by tidy), counit_iso := nat_iso.of_components (λ A, eq_to_iso rfl) (by tidy) } end ulift_hom /-- `as_small C` is a small category equivalent to `C`. More specifically, if `C : Type u` is endowed with `category.{v} C`, then `as_small.{w} C : Type (max w v u)` is endowed with an instance of a small category. The objects and morphisms of `as_small C` are defined by applying `ulift` to the objects and morphisms of `C`. Note: We require a category instance for this definition in order to have direct access to the universe level `v`. -/ @[nolint unused_arguments] def {w v u} as_small (C : Type u) [category.{v} C] := ulift.{max w v} C instance : small_category (as_small.{w₁} C) := { hom := λ X Y, ulift.{max w₁ u₁} $ X.down ⟶ Y.down, id := λ X, ⟨𝟙 _⟩, comp := λ X Y Z f g, ⟨f.down ≫ g.down⟩ } /-- One half of the equivalence between `C` and `as_small C`. -/ @[simps] def as_small.up : C ⥤ as_small C := { obj := λ X, ⟨X⟩, map := λ X Y f, ⟨f⟩ } /-- One half of the equivalence between `C` and `as_small C`. -/ @[simps] def as_small.down : as_small C ⥤ C := { obj := λ X, X.down, map := λ X Y f, f.down } /-- The equivalence between `C` and `as_small C`. -/ @[simps] def as_small.equiv : C ≌ as_small C := { functor := as_small.up, inverse := as_small.down, unit_iso := nat_iso.of_components (λ X, eq_to_iso rfl) (by tidy), counit_iso := nat_iso.of_components (λ X, eq_to_iso $ by { ext, refl }) (by tidy) } instance [inhabited C] : inhabited (as_small C) := ⟨⟨arbitrary _⟩⟩ /-- The equivalence between `C` and `ulift_hom (ulift C)`. -/ def {v' u' v u} ulift_hom_ulift_category.equiv (C : Type u) [category.{v} C] : C ≌ ulift_hom.{v'} (ulift.{u'} C) := ulift.equivalence.trans ulift_hom.equiv end category_theory