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
36f47cc2798bf9d8d2cfea604f1d52b7d469cb20
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/real/basic.lean
a2202fbf3dad6688510b3b84b34448614a43a849
[]
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
15,354
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Floris van Doorn The (classical) real numbers ℝ. This is a direct construction from Cauchy sequences. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.order.conditionally_complete_lattice import Mathlib.data.real.cau_seq_completion import Mathlib.algebra.archimedean import Mathlib.algebra.star.basic import Mathlib.PostPort namespace Mathlib /-- The type `ℝ` of real numbers constructed as equivalence classes of Cauchy sequences of rational numbers. -/ def real := cau_seq.completion.Cauchy notation:1024 "ℝ" => Mathlib.real namespace real def comm_ring_aux : comm_ring ℝ := cau_seq.completion.Cauchy.comm_ring protected instance comm_ring : comm_ring ℝ := comm_ring.mk comm_ring.add comm_ring.add_assoc comm_ring.zero comm_ring.zero_add comm_ring.add_zero comm_ring.neg comm_ring.sub comm_ring.add_left_neg comm_ring.add_comm comm_ring.mul comm_ring.mul_assoc comm_ring.one comm_ring.one_mul comm_ring.mul_one comm_ring.left_distrib comm_ring.right_distrib comm_ring.mul_comm /- Extra instances to short-circuit type class resolution -/ protected instance ring : ring ℝ := comm_ring.to_ring ℝ protected instance comm_semiring : comm_semiring ℝ := comm_ring.to_comm_semiring protected instance semiring : semiring ℝ := ring.to_semiring protected instance add_comm_group : add_comm_group ℝ := ring.to_add_comm_group ℝ protected instance add_group : add_group ℝ := add_comm_group.to_add_group ℝ protected instance add_comm_monoid : add_comm_monoid ℝ := add_comm_group.to_add_comm_monoid ℝ protected instance add_monoid : add_monoid ℝ := sub_neg_monoid.to_add_monoid ℝ protected instance add_left_cancel_semigroup : add_left_cancel_semigroup ℝ := add_left_cancel_monoid.to_add_left_cancel_semigroup ℝ protected instance add_right_cancel_semigroup : add_right_cancel_semigroup ℝ := add_right_cancel_monoid.to_add_right_cancel_semigroup ℝ protected instance add_comm_semigroup : add_comm_semigroup ℝ := add_comm_monoid.to_add_comm_semigroup ℝ protected instance add_semigroup : add_semigroup ℝ := add_monoid.to_add_semigroup ℝ protected instance comm_monoid : comm_monoid ℝ := comm_semiring.to_comm_monoid ℝ protected instance monoid : monoid ℝ := ring.to_monoid ℝ protected instance comm_semigroup : comm_semigroup ℝ := comm_ring.to_comm_semigroup ℝ protected instance semigroup : semigroup ℝ := monoid.to_semigroup ℝ protected instance inhabited : Inhabited ℝ := { default := 0 } /-- The real numbers are a *-ring, with the trivial *-structure. -/ protected instance star_ring : star_ring ℝ := star_ring_of_comm /-- Coercion `β„š` β†’ `ℝ` as a `ring_hom`. Note that this is `cau_seq.completion.of_rat`, not `rat.cast`. -/ def of_rat : β„š β†’+* ℝ := ring_hom.mk cau_seq.completion.of_rat sorry sorry sorry sorry /-- Make a real number from a Cauchy sequence of rationals (by taking the equivalence class). -/ def mk (x : cau_seq β„š abs) : ℝ := cau_seq.completion.mk x theorem of_rat_sub (x : β„š) (y : β„š) : coe_fn of_rat (x - y) = coe_fn of_rat x - coe_fn of_rat y := congr_arg mk (cau_seq.const_sub x y) protected instance has_lt : HasLess ℝ := { Less := fun (x y : ℝ) => quotient.lift_onβ‚‚ x y Less sorry } @[simp] theorem mk_lt {f : cau_seq β„š abs} {g : cau_seq β„š abs} : mk f < mk g ↔ f < g := iff.rfl theorem mk_eq {f : cau_seq β„š abs} {g : cau_seq β„š abs} : mk f = mk g ↔ f β‰ˆ g := cau_seq.completion.mk_eq theorem quotient_mk_eq_mk (f : cau_seq β„š abs) : quotient.mk f = mk f := rfl theorem mk_eq_mk {f : cau_seq β„š abs} : cau_seq.completion.mk f = mk f := rfl @[simp] theorem mk_pos {f : cau_seq β„š abs} : 0 < mk f ↔ cau_seq.pos f := iff_of_eq (congr_arg cau_seq.pos (sub_zero f)) protected def le (x : ℝ) (y : ℝ) := x < y ∨ x = y protected instance has_le : HasLessEq ℝ := { LessEq := real.le } @[simp] theorem mk_le {f : cau_seq β„š abs} {g : cau_seq β„š abs} : mk f ≀ mk g ↔ f ≀ g := or_congr iff.rfl quotient.eq theorem add_lt_add_iff_left {a : ℝ} {b : ℝ} (c : ℝ) : c + a < c + b ↔ a < b := sorry protected instance partial_order : partial_order ℝ := partial_order.mk LessEq Less sorry sorry sorry protected instance preorder : preorder ℝ := partial_order.to_preorder ℝ theorem of_rat_lt {x : β„š} {y : β„š} : coe_fn of_rat x < coe_fn of_rat y ↔ x < y := cau_seq.const_lt protected theorem zero_lt_one : 0 < 1 := iff.mpr of_rat_lt zero_lt_one protected theorem mul_pos {a : ℝ} {b : ℝ} : 0 < a β†’ 0 < b β†’ 0 < a * b := sorry protected instance ordered_ring : ordered_ring ℝ := ordered_ring.mk comm_ring.add comm_ring.add_assoc comm_ring.zero comm_ring.zero_add comm_ring.add_zero comm_ring.neg comm_ring.sub comm_ring.add_left_neg comm_ring.add_comm comm_ring.mul comm_ring.mul_assoc comm_ring.one comm_ring.one_mul comm_ring.mul_one comm_ring.left_distrib comm_ring.right_distrib partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry sorry real.mul_pos protected instance ordered_semiring : ordered_semiring ℝ := ordered_ring.to_ordered_semiring protected instance ordered_add_comm_group : ordered_add_comm_group ℝ := ordered_ring.to_ordered_add_comm_group ℝ protected instance ordered_cancel_add_comm_monoid : ordered_cancel_add_comm_monoid ℝ := ordered_semiring.to_ordered_cancel_add_comm_monoid ℝ protected instance ordered_add_comm_monoid : ordered_add_comm_monoid ℝ := ordered_cancel_add_comm_monoid.to_ordered_add_comm_monoid protected instance has_one : HasOne ℝ := monoid.to_has_one ℝ protected instance has_zero : HasZero ℝ := mul_zero_class.to_has_zero ℝ protected instance has_mul : Mul ℝ := distrib.to_has_mul ℝ protected instance has_add : Add ℝ := distrib.to_has_add ℝ protected instance has_sub : Sub ℝ := sub_neg_monoid.to_has_sub ℝ protected instance nontrivial : nontrivial ℝ := nontrivial.mk (Exists.intro 0 (Exists.intro 1 (ne_of_lt real.zero_lt_one))) protected instance linear_order : linear_order ℝ := linear_order.mk partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry (fun (a b : ℝ) => classical.prop_decidable (a ≀ b)) Mathlib.decidable_eq_of_decidable_le Mathlib.decidable_lt_of_decidable_le protected instance linear_ordered_comm_ring : linear_ordered_comm_ring ℝ := linear_ordered_comm_ring.mk ordered_ring.add ordered_ring.add_assoc ordered_ring.zero ordered_ring.zero_add ordered_ring.add_zero ordered_ring.neg ordered_ring.sub ordered_ring.add_left_neg ordered_ring.add_comm ordered_ring.mul ordered_ring.mul_assoc ordered_ring.one ordered_ring.one_mul ordered_ring.mul_one ordered_ring.left_distrib ordered_ring.right_distrib ordered_ring.le ordered_ring.lt ordered_ring.le_refl ordered_ring.le_trans ordered_ring.le_antisymm ordered_ring.add_le_add_left ordered_ring.zero_le_one ordered_ring.mul_pos linear_order.le_total linear_order.decidable_le linear_order.decidable_eq linear_order.decidable_lt nontrivial.exists_pair_ne comm_ring.mul_comm /- Extra instances to short-circuit type class resolution -/ protected instance linear_ordered_ring : linear_ordered_ring ℝ := linear_ordered_comm_ring.to_linear_ordered_ring ℝ protected instance linear_ordered_semiring : linear_ordered_semiring ℝ := linear_ordered_comm_ring.to_linear_ordered_semiring protected instance domain : domain ℝ := domain.mk comm_ring.add comm_ring.add_assoc comm_ring.zero comm_ring.zero_add comm_ring.add_zero comm_ring.neg comm_ring.sub comm_ring.add_left_neg comm_ring.add_comm comm_ring.mul comm_ring.mul_assoc comm_ring.one comm_ring.one_mul comm_ring.mul_one comm_ring.left_distrib comm_ring.right_distrib nontrivial.exists_pair_ne sorry protected instance linear_ordered_field : linear_ordered_field ℝ := sorry /- Extra instances to short-circuit type class resolution -/ protected instance linear_ordered_add_comm_group : linear_ordered_add_comm_group ℝ := linear_ordered_ring.to_linear_ordered_add_comm_group protected instance field : field ℝ := linear_ordered_field.to_field ℝ protected instance division_ring : division_ring ℝ := field.to_division_ring protected instance integral_domain : integral_domain ℝ := field.to_integral_domain protected instance distrib_lattice : distrib_lattice ℝ := Mathlib.distrib_lattice_of_linear_order protected instance lattice : lattice ℝ := Mathlib.lattice_of_linear_order protected instance semilattice_inf : semilattice_inf ℝ := lattice.to_semilattice_inf ℝ protected instance semilattice_sup : semilattice_sup ℝ := lattice.to_semilattice_sup ℝ protected instance has_inf : has_inf ℝ := semilattice_inf.to_has_inf ℝ protected instance has_sup : has_sup ℝ := semilattice_sup.to_has_sup ℝ protected instance decidable_lt (a : ℝ) (b : ℝ) : Decidable (a < b) := has_lt.lt.decidable a b protected instance decidable_le (a : ℝ) (b : ℝ) : Decidable (a ≀ b) := has_le.le.decidable a b protected instance decidable_eq (a : ℝ) (b : ℝ) : Decidable (a = b) := quotient.decidable_eq a b @[simp] theorem of_rat_eq_cast (x : β„š) : coe_fn of_rat x = ↑x := ring_hom.eq_rat_cast of_rat theorem le_mk_of_forall_le {x : ℝ} {f : cau_seq β„š abs} : (βˆƒ (i : β„•), βˆ€ (j : β„•), j β‰₯ i β†’ x ≀ ↑(coe_fn f j)) β†’ x ≀ mk f := sorry theorem mk_le_of_forall_le {f : cau_seq β„š abs} {x : ℝ} : (βˆƒ (i : β„•), βˆ€ (j : β„•), j β‰₯ i β†’ ↑(coe_fn f j) ≀ x) β†’ mk f ≀ x := sorry theorem mk_near_of_forall_near {f : cau_seq β„š abs} {x : ℝ} {Ξ΅ : ℝ} (H : βˆƒ (i : β„•), βˆ€ (j : β„•), j β‰₯ i β†’ abs (↑(coe_fn f j) - x) ≀ Ξ΅) : abs (mk f - x) ≀ Ξ΅ := sorry protected instance archimedean : archimedean ℝ := iff.mpr archimedean_iff_rat_le fun (x : ℝ) => quotient.induction_on x fun (f : cau_seq β„š abs) => sorry /- mark `real` irreducible in order to prevent `auto_cases` unfolding reals, since users rarely want to consider real numbers as Cauchy sequences. Marking `comm_ring_aux` `irreducible` is done to ensure that there are no problems with non definitionally equal instances, caused by making `real` irreducible-/ protected instance floor_ring : floor_ring ℝ := archimedean.floor_ring ℝ theorem is_cau_seq_iff_lift {f : β„• β†’ β„š} : is_cau_seq abs f ↔ is_cau_seq abs fun (i : β„•) => ↑(f i) := sorry theorem of_near (f : β„• β†’ β„š) (x : ℝ) (h : βˆ€ (Ξ΅ : ℝ), Ξ΅ > 0 β†’ βˆƒ (i : β„•), βˆ€ (j : β„•), j β‰₯ i β†’ abs (↑(f j) - x) < Ξ΅) : βˆƒ (h' : is_cau_seq abs f), mk { val := f, property := h' } = x := sorry theorem exists_floor (x : ℝ) : βˆƒ (ub : β„€), ↑ub ≀ x ∧ βˆ€ (z : β„€), ↑z ≀ x β†’ z ≀ ub := sorry theorem exists_sup (S : set ℝ) : (βˆƒ (x : ℝ), x ∈ S) β†’ (βˆƒ (x : ℝ), βˆ€ (y : ℝ), y ∈ S β†’ y ≀ x) β†’ βˆƒ (x : ℝ), βˆ€ (y : ℝ), x ≀ y ↔ βˆ€ (z : ℝ), z ∈ S β†’ z ≀ y := sorry protected instance has_Sup : has_Sup ℝ := has_Sup.mk fun (S : set ℝ) => dite ((βˆƒ (x : ℝ), x ∈ S) ∧ βˆƒ (x : ℝ), βˆ€ (y : ℝ), y ∈ S β†’ y ≀ x) (fun (h : (βˆƒ (x : ℝ), x ∈ S) ∧ βˆƒ (x : ℝ), βˆ€ (y : ℝ), y ∈ S β†’ y ≀ x) => classical.some sorry) fun (h : Β¬((βˆƒ (x : ℝ), x ∈ S) ∧ βˆƒ (x : ℝ), βˆ€ (y : ℝ), y ∈ S β†’ y ≀ x)) => 0 theorem Sup_def (S : set ℝ) : Sup S = dite ((βˆƒ (x : ℝ), x ∈ S) ∧ βˆƒ (x : ℝ), βˆ€ (y : ℝ), y ∈ S β†’ y ≀ x) (fun (h : (βˆƒ (x : ℝ), x ∈ S) ∧ βˆƒ (x : ℝ), βˆ€ (y : ℝ), y ∈ S β†’ y ≀ x) => classical.some (exists_sup S (and.left h) (and.right h))) fun (h : Β¬((βˆƒ (x : ℝ), x ∈ S) ∧ βˆƒ (x : ℝ), βˆ€ (y : ℝ), y ∈ S β†’ y ≀ x)) => 0 := rfl theorem Sup_le (S : set ℝ) (h₁ : βˆƒ (x : ℝ), x ∈ S) (hβ‚‚ : βˆƒ (x : ℝ), βˆ€ (y : ℝ), y ∈ S β†’ y ≀ x) {y : ℝ} : Sup S ≀ y ↔ βˆ€ (z : ℝ), z ∈ S β†’ z ≀ y := sorry -- this proof times out without this theorem lt_Sup (S : set ℝ) (h₁ : βˆƒ (x : ℝ), x ∈ S) (hβ‚‚ : βˆƒ (x : ℝ), βˆ€ (y : ℝ), y ∈ S β†’ y ≀ x) {y : ℝ} : y < Sup S ↔ βˆƒ (z : ℝ), βˆƒ (H : z ∈ S), y < z := sorry theorem le_Sup (S : set ℝ) (hβ‚‚ : βˆƒ (x : ℝ), βˆ€ (y : ℝ), y ∈ S β†’ y ≀ x) {x : ℝ} (xS : x ∈ S) : x ≀ Sup S := iff.mp (Sup_le S (Exists.intro x xS) hβ‚‚) (le_refl (Sup S)) x xS theorem Sup_le_ub (S : set ℝ) (h₁ : βˆƒ (x : ℝ), x ∈ S) {ub : ℝ} (hβ‚‚ : βˆ€ (y : ℝ), y ∈ S β†’ y ≀ ub) : Sup S ≀ ub := iff.mpr (Sup_le S h₁ (Exists.intro ub hβ‚‚)) hβ‚‚ protected theorem is_lub_Sup {s : set ℝ} {a : ℝ} {b : ℝ} (ha : a ∈ s) (hb : b ∈ upper_bounds s) : is_lub s (Sup s) := { left := fun (x : ℝ) (xs : x ∈ s) => le_Sup s (Exists.intro b hb) xs, right := fun (u : ℝ) (h : u ∈ upper_bounds s) => Sup_le_ub s (Exists.intro a ha) h } protected instance has_Inf : has_Inf ℝ := has_Inf.mk fun (S : set ℝ) => -Sup (set_of fun (x : ℝ) => -x ∈ S) theorem Inf_def (S : set ℝ) : Inf S = -Sup (set_of fun (x : ℝ) => -x ∈ S) := rfl theorem le_Inf (S : set ℝ) (h₁ : βˆƒ (x : ℝ), x ∈ S) (hβ‚‚ : βˆƒ (x : ℝ), βˆ€ (y : ℝ), y ∈ S β†’ x ≀ y) {y : ℝ} : y ≀ Inf S ↔ βˆ€ (z : ℝ), z ∈ S β†’ y ≀ z := sorry -- this proof times out without this theorem Inf_lt (S : set ℝ) (h₁ : βˆƒ (x : ℝ), x ∈ S) (hβ‚‚ : βˆƒ (x : ℝ), βˆ€ (y : ℝ), y ∈ S β†’ x ≀ y) {y : ℝ} : Inf S < y ↔ βˆƒ (z : ℝ), βˆƒ (H : z ∈ S), z < y := sorry theorem Inf_le (S : set ℝ) (hβ‚‚ : βˆƒ (x : ℝ), βˆ€ (y : ℝ), y ∈ S β†’ x ≀ y) {x : ℝ} (xS : x ∈ S) : Inf S ≀ x := iff.mp (le_Inf S (Exists.intro x xS) hβ‚‚) (le_refl (Inf S)) x xS theorem lb_le_Inf (S : set ℝ) (h₁ : βˆƒ (x : ℝ), x ∈ S) {lb : ℝ} (hβ‚‚ : βˆ€ (y : ℝ), y ∈ S β†’ lb ≀ y) : lb ≀ Inf S := iff.mpr (le_Inf S h₁ (Exists.intro lb hβ‚‚)) hβ‚‚ protected instance conditionally_complete_linear_order : conditionally_complete_linear_order ℝ := conditionally_complete_linear_order.mk lattice.sup linear_order.le linear_order.lt linear_order.le_refl linear_order.le_trans linear_order.le_antisymm lattice.le_sup_left lattice.le_sup_right lattice.sup_le lattice.inf lattice.inf_le_left lattice.inf_le_right lattice.le_inf Sup Inf sorry sorry sorry sorry linear_order.le_total (classical.dec_rel LessEq) linear_order.decidable_eq linear_order.decidable_lt theorem Sup_empty : Sup βˆ… = 0 := sorry theorem Sup_of_not_bdd_above {s : set ℝ} (hs : Β¬bdd_above s) : Sup s = 0 := dif_neg fun (h : (βˆƒ (x : ℝ), x ∈ s) ∧ βˆƒ (x : ℝ), βˆ€ (y : ℝ), y ∈ s β†’ y ≀ x) => hs (and.right h) theorem Sup_univ : Sup set.univ = 0 := sorry theorem Inf_empty : Inf βˆ… = 0 := sorry theorem Inf_of_not_bdd_below {s : set ℝ} (hs : Β¬bdd_below s) : Inf s = 0 := sorry theorem cau_seq_converges (f : cau_seq ℝ abs) : βˆƒ (x : ℝ), f β‰ˆ cau_seq.const abs x := sorry protected instance abs.cau_seq.is_complete : cau_seq.is_complete ℝ abs := cau_seq.is_complete.mk cau_seq_converges
aae6b4ffa71669c193159c36f979a49ee035c4b4
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/real/cardinality.lean
58db59e8ab65a26c595a86442a0a8ec3c56d415e
[ "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
9,603
lean
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import analysis.specific_limits.basic import data.rat.denumerable import data.set.pointwise.interval import set_theory.cardinal.continuum /-! # The cardinality of the reals This file shows that the real numbers have cardinality continuum, i.e. `#ℝ = 𝔠`. We show that `#ℝ ≀ 𝔠` by noting that every real number is determined by a Cauchy-sequence of the form `β„• β†’ β„š`, which has cardinality `𝔠`. To show that `#ℝ β‰₯ 𝔠` we define an injection from `{0, 1} ^ β„•` to `ℝ` with `f ↦ Ξ£ n, f n * (1 / 3) ^ n`. We conclude that all intervals with distinct endpoints have cardinality continuum. ## Main definitions * `cardinal.cantor_function` is the function that sends `f` in `{0, 1} ^ β„•` to `ℝ` by `f ↦ Ξ£' n, f n * (1 / 3) ^ n` ## Main statements * `cardinal.mk_real : #ℝ = 𝔠`: the reals have cardinality continuum. * `cardinal.not_countable_real`: the universal set of real numbers is not countable. We can use this same proof to show that all the other sets in this file are not countable. * 8 lemmas of the form `mk_Ixy_real` for `x,y ∈ {i,o,c}` state that intervals on the reals have cardinality continuum. ## Notation * `𝔠` : notation for `cardinal.continuum` in locale `cardinal`, defined in `set_theory.continuum`. ## Tags continuum, cardinality, reals, cardinality of the reals -/ open nat set open_locale cardinal noncomputable theory namespace cardinal variables {c : ℝ} {f g : β„• β†’ bool} {n : β„•} /-- The body of the sum in `cantor_function`. `cantor_function_aux c f n = c ^ n` if `f n = tt`; `cantor_function_aux c f n = 0` if `f n = ff`. -/ def cantor_function_aux (c : ℝ) (f : β„• β†’ bool) (n : β„•) : ℝ := cond (f n) (c ^ n) 0 @[simp] lemma cantor_function_aux_tt (h : f n = tt) : cantor_function_aux c f n = c ^ n := by simp [cantor_function_aux, h] @[simp] lemma cantor_function_aux_ff (h : f n = ff) : cantor_function_aux c f n = 0 := by simp [cantor_function_aux, h] lemma cantor_function_aux_nonneg (h : 0 ≀ c) : 0 ≀ cantor_function_aux c f n := by { cases h' : f n; simp [h'], apply pow_nonneg h } lemma cantor_function_aux_eq (h : f n = g n) : cantor_function_aux c f n = cantor_function_aux c g n := by simp [cantor_function_aux, h] lemma cantor_function_aux_succ (f : β„• β†’ bool) : (Ξ» n, cantor_function_aux c f (n + 1)) = Ξ» n, c * cantor_function_aux c (Ξ» n, f (n + 1)) n := by { ext n, cases h : f (n + 1); simp [h, pow_succ] } lemma summable_cantor_function (f : β„• β†’ bool) (h1 : 0 ≀ c) (h2 : c < 1) : summable (cantor_function_aux c f) := begin apply (summable_geometric_of_lt_1 h1 h2).summable_of_eq_zero_or_self, intro n, cases h : f n; simp [h] end /-- `cantor_function c (f : β„• β†’ bool)` is `Ξ£ n, f n * c ^ n`, where `tt` is interpreted as `1` and `ff` is interpreted as `0`. It is implemented using `cantor_function_aux`. -/ def cantor_function (c : ℝ) (f : β„• β†’ bool) : ℝ := βˆ‘' n, cantor_function_aux c f n lemma cantor_function_le (h1 : 0 ≀ c) (h2 : c < 1) (h3 : βˆ€ n, f n β†’ g n) : cantor_function c f ≀ cantor_function c g := begin apply tsum_le_tsum _ (summable_cantor_function f h1 h2) (summable_cantor_function g h1 h2), intro n, cases h : f n, simp [h, cantor_function_aux_nonneg h1], replace h3 : g n = tt := h3 n h, simp [h, h3] end lemma cantor_function_succ (f : β„• β†’ bool) (h1 : 0 ≀ c) (h2 : c < 1) : cantor_function c f = cond (f 0) 1 0 + c * cantor_function c (Ξ» n, f (n+1)) := begin rw [cantor_function, tsum_eq_zero_add (summable_cantor_function f h1 h2)], rw [cantor_function_aux_succ, tsum_mul_left, cantor_function_aux, pow_zero], refl end /-- `cantor_function c` is strictly increasing with if `0 < c < 1/2`, if we endow `β„• β†’ bool` with a lexicographic order. The lexicographic order doesn't exist for these infinitary products, so we explicitly write out what it means. -/ lemma increasing_cantor_function (h1 : 0 < c) (h2 : c < 1 / 2) {n : β„•} {f g : β„• β†’ bool} (hn : βˆ€(k < n), f k = g k) (fn : f n = ff) (gn : g n = tt) : cantor_function c f < cantor_function c g := begin have h3 : c < 1, { apply h2.trans, norm_num }, induction n with n ih generalizing f g, { let f_max : β„• β†’ bool := Ξ» n, nat.rec ff (Ξ» _ _, tt) n, have hf_max : βˆ€n, f n β†’ f_max n, { intros n hn, cases n, rw [fn] at hn, contradiction, apply rfl }, let g_min : β„• β†’ bool := Ξ» n, nat.rec tt (Ξ» _ _, ff) n, have hg_min : βˆ€n, g_min n β†’ g n, { intros n hn, cases n, rw [gn], apply rfl, contradiction }, apply (cantor_function_le (le_of_lt h1) h3 hf_max).trans_lt, refine lt_of_lt_of_le _ (cantor_function_le (le_of_lt h1) h3 hg_min), have : c / (1 - c) < 1, { rw [div_lt_one, lt_sub_iff_add_lt], { convert add_lt_add h2 h2, norm_num }, rwa sub_pos }, convert this, { rw [cantor_function_succ _ (le_of_lt h1) h3, div_eq_mul_inv, ←tsum_geometric_of_lt_1 (le_of_lt h1) h3], apply zero_add }, { convert tsum_eq_single 0 _, { apply_instance }, { intros n hn, cases n, contradiction, refl } } }, rw [cantor_function_succ f (le_of_lt h1) h3, cantor_function_succ g (le_of_lt h1) h3], rw [hn 0 $ zero_lt_succ n], apply add_lt_add_left, rw mul_lt_mul_left h1, exact ih (Ξ» k hk, hn _ $ nat.succ_lt_succ hk) fn gn end /-- `cantor_function c` is injective if `0 < c < 1/2`. -/ lemma cantor_function_injective (h1 : 0 < c) (h2 : c < 1 / 2) : function.injective (cantor_function c) := begin intros f g hfg, classical, by_contra h, revert hfg, have : βˆƒn, f n β‰  g n, { rw [←not_forall], intro h', apply h, ext, apply h' }, let n := nat.find this, have hn : βˆ€ (k : β„•), k < n β†’ f k = g k, { intros k hk, apply of_not_not, exact nat.find_min this hk }, cases fn : f n, { apply ne_of_lt, refine increasing_cantor_function h1 h2 hn fn _, apply eq_tt_of_not_eq_ff, rw [←fn], apply ne.symm, exact nat.find_spec this }, { apply ne_of_gt, refine increasing_cantor_function h1 h2 (Ξ» k hk, (hn k hk).symm) _ fn, apply eq_ff_of_not_eq_tt, rw [←fn], apply ne.symm, exact nat.find_spec this } end /-- The cardinality of the reals, as a type. -/ lemma mk_real : #ℝ = 𝔠 := begin apply le_antisymm, { rw real.equiv_Cauchy.cardinal_eq, apply mk_quotient_le.trans, apply (mk_subtype_le _).trans_eq, rw [← power_def, mk_nat, mk_rat, aleph_0_power_aleph_0] }, { convert mk_le_of_injective (cantor_function_injective _ _), rw [←power_def, mk_bool, mk_nat, two_power_aleph_0], exact 1 / 3, norm_num, norm_num } end /-- The cardinality of the reals, as a set. -/ lemma mk_univ_real : #(set.univ : set ℝ) = 𝔠 := by rw [mk_univ, mk_real] /-- **Non-Denumerability of the Continuum**: The reals are not countable. -/ lemma not_countable_real : Β¬ (set.univ : set ℝ).countable := by { rw [← le_aleph_0_iff_set_countable, not_le, mk_univ_real], apply cantor } /-- The cardinality of the interval (a, ∞). -/ lemma mk_Ioi_real (a : ℝ) : #(Ioi a) = 𝔠 := begin refine le_antisymm (mk_real β–Έ mk_set_le _) _, rw [← not_lt], intro h, refine ne_of_lt _ mk_univ_real, have hu : Iio a βˆͺ {a} βˆͺ Ioi a = set.univ, { convert Iic_union_Ioi, exact Iio_union_right }, rw ← hu, refine lt_of_le_of_lt (mk_union_le _ _) _, refine lt_of_le_of_lt (add_le_add_right (mk_union_le _ _) _) _, have h2 : (Ξ» x, a + a - x) '' Ioi a = Iio a, { convert image_const_sub_Ioi _ _, simp }, rw ← h2, refine add_lt_of_lt (cantor _).le _ h, refine add_lt_of_lt (cantor _).le (mk_image_le.trans_lt h) _, rw mk_singleton, exact one_lt_aleph_0.trans (cantor _) end /-- The cardinality of the interval [a, ∞). -/ lemma mk_Ici_real (a : ℝ) : #(Ici a) = 𝔠 := le_antisymm (mk_real β–Έ mk_set_le _) (mk_Ioi_real a β–Έ mk_le_mk_of_subset Ioi_subset_Ici_self) /-- The cardinality of the interval (-∞, a). -/ lemma mk_Iio_real (a : ℝ) : #(Iio a) = 𝔠 := begin refine le_antisymm (mk_real β–Έ mk_set_le _) _, have h2 : (Ξ» x, a + a - x) '' Iio a = Ioi a, { convert image_const_sub_Iio _ _, simp }, exact mk_Ioi_real a β–Έ h2 β–Έ mk_image_le end /-- The cardinality of the interval (-∞, a]. -/ lemma mk_Iic_real (a : ℝ) : #(Iic a) = 𝔠 := le_antisymm (mk_real β–Έ mk_set_le _) (mk_Iio_real a β–Έ mk_le_mk_of_subset Iio_subset_Iic_self) /-- The cardinality of the interval (a, b). -/ lemma mk_Ioo_real {a b : ℝ} (h : a < b) : #(Ioo a b) = 𝔠 := begin refine le_antisymm (mk_real β–Έ mk_set_le _) _, have h1 : #((Ξ» x, x - a) '' Ioo a b) ≀ #(Ioo a b) := mk_image_le, refine le_trans _ h1, rw [image_sub_const_Ioo, sub_self], replace h := sub_pos_of_lt h, have h2 : #(has_inv.inv '' Ioo 0 (b - a)) ≀ #(Ioo 0 (b - a)) := mk_image_le, refine le_trans _ h2, rw [image_inv, inv_Ioo_0_left h, mk_Ioi_real] end /-- The cardinality of the interval [a, b). -/ lemma mk_Ico_real {a b : ℝ} (h : a < b) : #(Ico a b) = 𝔠 := le_antisymm (mk_real β–Έ mk_set_le _) (mk_Ioo_real h β–Έ mk_le_mk_of_subset Ioo_subset_Ico_self) /-- The cardinality of the interval [a, b]. -/ lemma mk_Icc_real {a b : ℝ} (h : a < b) : #(Icc a b) = 𝔠 := le_antisymm (mk_real β–Έ mk_set_le _) (mk_Ioo_real h β–Έ mk_le_mk_of_subset Ioo_subset_Icc_self) /-- The cardinality of the interval (a, b]. -/ lemma mk_Ioc_real {a b : ℝ} (h : a < b) : #(Ioc a b) = 𝔠 := le_antisymm (mk_real β–Έ mk_set_le _) (mk_Ioo_real h β–Έ mk_le_mk_of_subset Ioo_subset_Ioc_self) end cardinal
529d4f30e6b47e4089eb01725f0cfe71dd1fd13b
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/category_theory/bifunctor.lean
d27e04b8023c4271ac0fc6c58234c4671f71618c
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
1,899
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.products open category_theory namespace category_theory.bifunctor universes v₁ vβ‚‚ v₃ u₁ uβ‚‚ u₃ variable {C : Type u₁} variable [π’ž : category.{v₁+1} C] variable {D : Type uβ‚‚} variable [π’Ÿ : category.{vβ‚‚+1} D] variable {E : Type u₃} variable [β„° : category.{v₃+1} E] include π’ž π’Ÿ β„° @[simp] lemma map_id (F : (C Γ— D) β₯€ E) (X : C) (Y : D) : F.map ((πŸ™ X, πŸ™ Y) : (X, Y) ⟢ (X, Y)) = πŸ™ (F.obj (X, Y)) := F.map_id (X, Y) @[simp] lemma map_id_comp (F : (C Γ— D) β₯€ E) (W : C) {X Y Z : D} (f : X ⟢ Y) (g : Y ⟢ Z) : F.map ((πŸ™ W, f ≫ g) : (W, X) ⟢ (W, Z)) = F.map ((πŸ™ W, f) : (W, X) ⟢ (W, Y)) ≫ F.map ((πŸ™ W, g) : (W, Y) ⟢ (W, Z)) := by rw [←functor.map_comp,prod_comp,category.comp_id] @[simp] lemma map_comp_id (F : (C Γ— D) β₯€ E) (X Y Z : C) (W : D) (f : X ⟢ Y) (g : Y ⟢ Z) : F.map ((f ≫ g, πŸ™ W) : (X, W) ⟢ (Z, W)) = F.map ((f, πŸ™ W) : (X, W) ⟢ (Y, W)) ≫ F.map ((g, πŸ™ W) : (Y, W) ⟢ (Z, W)) := by rw [←functor.map_comp,prod_comp,category.comp_id] @[simp] lemma diagonal (F : (C Γ— D) β₯€ E) (X X' : C) (f : X ⟢ X') (Y Y' : D) (g : Y ⟢ Y') : F.map ((πŸ™ X, g) : (X, Y) ⟢ (X, Y')) ≫ F.map ((f, πŸ™ Y') : (X, Y') ⟢ (X', Y')) = F.map ((f, g) : (X, Y) ⟢ (X', Y')) := by rw [←functor.map_comp, prod_comp, category.id_comp, category.comp_id] @[simp] lemma diagonal' (F : (C Γ— D) β₯€ E) (X X' : C) (f : X ⟢ X') (Y Y' : D) (g : Y ⟢ Y') : F.map ((f, πŸ™ Y) : (X, Y) ⟢ (X', Y)) ≫ F.map ((πŸ™ X', g) : (X', Y) ⟢ (X', Y')) = F.map ((f, g) : (X, Y) ⟢ (X', Y')) := by rw [←functor.map_comp, prod_comp, category.id_comp, category.comp_id] end category_theory.bifunctor
d52ba6694fe0952820558c29c5034571f0c92821
c780ac8c1c0dd4de0fb63981226f2b53b62fe298
/src/finite_field.lean
92b8af8ed21e233f0a975dc16ff516adf3d661e2
[]
no_license
jmvlangen/finite_fields
0672670245ccf62b2967d9c5edc2f60c53fc141f
757bd91636e0b3508ee21c92be775eb9d908391e
refs/heads/master
1,586,987,952,433
1,549,629,122,000
1,549,629,122,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,168
lean
import algebra.char_p import data.zmod.basic import data.equiv.algebra import vector_space universes u v namespace char_p variable {Ξ± : Type u} variable [ring Ξ±] open nat --char_p lemma char_p_eq_mod (n : β„•) [char_p Ξ± n] (k : β„•) : (k : Ξ±) = (k % n : β„•) := calc (k : Ξ±) = (k % n + n * (k / n) : β„•) : by rw [mod_add_div] ... = ↑(k % n) + ↑n * ↑(k / n) : by rw [cast_add, cast_mul] ... = ↑(k % n) + 0 : by rw [char_p.cast_eq_zero Ξ± n, zero_mul] ... = ↑(k % n) : by rw [add_zero] end char_p namespace zmod variable {n : β„•+} variable (Ξ± : Type u) variables [ring Ξ±] open char_p nat --zmod n or char_p def cast : zmod n β†’ Ξ± := nat.cast ∘ fin.val --zmod n or char_p instance cast_is_ring_hom [char_p Ξ± n] : is_ring_hom (cast Ξ±) := { map_one := by rw ←cast_one; exact eq.symm (char_p_eq_mod n 1), map_mul := assume x y : zmod n, show ↑((x * y).val) = ↑(x.val) * ↑(y.val), from by rw [zmod.mul_val, ←char_p_eq_mod, cast_mul], map_add := assume x y : zmod n, show ↑((x + y).val) = ↑(x.val) + ↑(y.val), from by rw [zmod.add_val, ←char_p_eq_mod, cast_add] } open is_ring_hom --zmod n or char_p instance to_module [char_p Ξ± n] : module (zmod n) Ξ± := module.of_core { smul := Ξ» r x, (cast Ξ±) r * x, smul_add := Ξ» r x y, by unfold has_scalar.smul; rw[mul_add]; refl, add_smul := Ξ» r s x, by unfold has_scalar.smul; rw[map_add (cast Ξ±), add_mul]; apply_instance, mul_smul := Ξ» r s x, by unfold has_scalar.smul; rw[map_mul (cast Ξ±), mul_assoc]; apply_instance, one_smul := Ξ» x, show (cast Ξ±) 1 * x = _, by rw[map_one (cast Ξ±), one_mul]; apply_instance } instance to_module' {m : β„•} {hm : m > 0} [hc : char_p Ξ± m] : module (zmod ⟨m, hm⟩) Ξ± := @zmod.to_module ⟨m, hm⟩ Ξ± _ hc end zmod namespace char_p variables (Ξ± : Type u) [ring Ξ±] open function nat --char_p lemma ne_zero [fintype Ξ±] [decidable_eq Ξ±] (p : β„•) [char_p Ξ± p] : p β‰  0 := (assume h : p = 0, have βˆ€ n : β„•, (n : Ξ±) = 0 β†’ n = 0, from assume (n : β„•) (hβ‚€ : (n : Ξ±) = 0), have 0 ∣ n, from h β–Έ (cast_eq_zero_iff Ξ± p n).mp hβ‚€, show n = 0, from zero_dvd_iff.mp this, have char_zero Ξ±, from add_group.char_zero_of_inj_zero this, have ht : injective nat.cast, from @cast_injective Ξ± _ _ this, have hf : Β¬injective nat.cast, from @set.not_injective_nat_fintype Ξ± _ _ _, absurd ht hf) end char_p section integral_domain variables (Ξ± : Type u) [integral_domain Ξ±] open nat function char_p --integral_domain lemma char_p_ne_one (p : β„•) [hc : char_p Ξ± p] : p β‰  1 := assume hp : p = 1, have (↑1 : Ξ±) = 0, from (cast_eq_zero_iff Ξ± p 1).mpr (hp β–Έ (dvd_refl p)), have ( 1 : Ξ±) = 0, from @cast_one Ξ± _ _ β–Έ this, absurd this one_ne_zero --integral_domain lemma char_p_prime_of_ge_two (p : β„•) [hc : char_p Ξ± p] (hp : p β‰₯ 2) : nat.prime p := suffices βˆ€d ∣ p, d = 1 ∨ d = p, from ⟨hp, this⟩, assume (d : β„•) (hdvd : βˆƒ e, p = d * e), let ⟨e, hmul⟩ := hdvd in have (p : Ξ±) = 0, from (cast_eq_zero_iff Ξ± p p).mpr (dvd_refl p), have (d : Ξ±) * e = 0, from (@cast_mul Ξ± _ d e) β–Έ (hmul β–Έ this), or.elim (no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero (d : Ξ±) e this) (assume hd : (d : Ξ±) = 0, have p ∣ d, from (cast_eq_zero_iff Ξ± p d).mp hd, show d = 1 ∨ d = p, from or.inr (dvd_antisymm ⟨e, hmul⟩ this)) (assume he : (e : Ξ±) = 0, have p ∣ e, from (cast_eq_zero_iff Ξ± p e).mp he, have e ∣ p, from dvd_of_mul_left_eq d (eq.symm hmul), have e = p, from dvd_antisymm β€Ήe ∣ pβ€Ί β€Ήp ∣ eβ€Ί, have hβ‚€ : p > 0, from gt_of_ge_of_gt hp (nat.zero_lt_succ 1), have d * p = 1 * p, by rw β€Ήe = pβ€Ί at hmul; rw [one_mul]; exact eq.symm hmul, show d = 1 ∨ d = p, from or.inl (eq_of_mul_eq_mul_right hβ‚€ this)) --integral_domain lemma char_p_prime_or_zero (p : β„•) [hc : char_p Ξ± p] : nat.prime p ∨ p = 0 := match p, hc with | 0, _ := or.inr rfl | 1, hc := absurd (eq.refl (1 : β„•)) (@char_p_ne_one Ξ± _ (1 : β„•) hc) | (m+2), hc := or.inl (@char_p_prime_of_ge_two Ξ± _ (m+2) hc (nat.le_add_left 2 m)) end --integral_domain lemma char_p_prime [fintype Ξ±] [decidable_eq Ξ±] (p : β„•) [char_p Ξ± p] : nat.prime p := or.resolve_right (char_p_prime_or_zero Ξ± p) (char_p.ne_zero Ξ± p) lemma ring_char_prime [fintype Ξ±] [decidable_eq Ξ±] : nat.prime (ring_char Ξ±) := @char_p_prime Ξ± _ _ _ (ring_char Ξ±) ⟨ring_char.spec α⟩ end integral_domain namespace finite_field open fintype variables (Ξ± : Type u) {Ξ² : Type v} variables [discrete_field Ξ±] [fintype Ξ±] variables [discrete_field Ξ²] [fintype Ξ²] --finite field theorem fin_field_card (p : β„•) [char_p Ξ± p] : βˆƒ (n : β„•+), nat.prime p ∧ card Ξ± = p^(n : β„•) := have hp : nat.prime p, from char_p_prime Ξ± p, have V : vector_space (zmodp p hp) Ξ±, from {..zmod.to_module' Ξ±}, let ⟨n, h⟩ := @vector_space.card_fin _ _ _ _ _ _ V _ in have hn : n > 0, from or.resolve_left (nat.eq_zero_or_pos n) (assume h0 : n = 0, have card Ξ± = 1, by rw[←nat.pow_zero (card _), ←h0]; exact h, have (1 : Ξ±) = 0, from (fintype.card_le_one_iff.mp (le_of_eq this)) 1 0, absurd this one_ne_zero), ⟨⟨n, hn⟩, hp, fintype.card_fin p β–Έ h⟩ theorem fin_field_card_exists : βˆƒ (p : β„•) (n : β„•+), nat.prime p ∧ card Ξ± = p^(n : β„•) := let ⟨p, hc⟩ := char_p.exists Ξ± in ⟨p, @fin_field_card Ξ± _ _ p hc⟩ theorem finite_field.exists : βˆ€ (p : β„•) (n : β„•+), nat.prime p β†’ βˆƒ (Ξ± : Type*) [hf : field Ξ±] [hfin : fintype Ξ±], @card Ξ± hfin = p^(n : β„•) := sorry theorem finite_field.unique : card Ξ± = card Ξ² β†’ (Ξ± ≃r Ξ²) := sorry def fin_field (p : β„•) (n : β„•+) {hp : nat.prime p} := classical.some (finite_field.exists p n hp) notation `𝔽_[` p `;` n `]` := fin_field p n --find better notation? variables {p : β„•} {n : β„•+} {hp : nat.prime p} noncomputable instance : field 𝔽_[p;n] := classical.some (classical.some_spec (finite_field.exists p n hp)) noncomputable instance : fintype 𝔽_[p;n] := classical.some (classical.some_spec $ classical.some_spec (finite_field.exists p n hp)) end finite_field
7e907d5c72a533a3cacc75773441a4ef2eb397c5
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/homology/homology.lean
50511df3d3e29017cefbed281ff017e83523bb29
[ "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
7,755
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.homology.image_to_kernel import algebra.homology.homological_complex import category_theory.graded_object /-! # The homology of a complex Given `C : homological_complex V c`, we have `C.cycles i` and `C.boundaries i`, both defined as subobjects of `C.X i`. We show these are functorial with respect to chain maps, as `C.cycles_map f i` and `C.boundaries_map f i`. As a consequence we construct `homology_functor i : homological_complex V c β₯€ V`, computing the `i`-th homology. -/ universes v u open category_theory category_theory.limits variables {ΞΉ : Type*} variables {V : Type u} [category.{v} V] [has_zero_morphisms V] variables {c : complex_shape ΞΉ} (C : homological_complex V c) open_locale classical noncomputable theory namespace homological_complex variables [has_zero_object V] section cycles variables [has_kernels V] /-- The cycles at index `i`, as a subobject. -/ def cycles (i : ΞΉ) : subobject (C.X i) := kernel_subobject (C.d_from i) @[simp, reassoc] lemma cycles_arrow_d_from (i : ΞΉ) : (C.cycles i).arrow ≫ C.d_from i = 0 := by { dsimp [cycles], simp, } lemma cycles_eq_kernel_subobject {i j : ΞΉ} (r : c.rel i j) : C.cycles i = kernel_subobject (C.d i j) := C.kernel_from_eq_kernel r /-- The underlying object of `C.cycles i` is isomorphic to `kernel (C.d i j)`, for any `j` such that `rel i j`. -/ def cycles_iso_kernel {i j : ΞΉ} (r : c.rel i j) : (C.cycles i : V) β‰… kernel (C.d i j) := subobject.iso_of_eq _ _ (C.cycles_eq_kernel_subobject r) β‰ͺ≫ kernel_subobject_iso (C.d i j) lemma cycles_eq_top {i} (h : c.next i = none) : C.cycles i = ⊀ := begin rw eq_top_iff, apply le_kernel_subobject, rw [C.d_from_eq_zero h, comp_zero], end end cycles section boundaries variables [has_images V] /-- The boundaries at index `i`, as a subobject. -/ abbreviation boundaries (C : homological_complex V c) (j : ΞΉ) : subobject (C.X j) := image_subobject (C.d_to j) lemma boundaries_eq_image_subobject [has_equalizers V] {i j : ΞΉ} (r : c.rel i j) : C.boundaries j = image_subobject (C.d i j) := C.image_to_eq_image r /-- The underlying object of `C.boundaries j` is isomorphic to `image (C.d i j)`, for any `i` such that `rel i j`. -/ def boundaries_iso_image [has_equalizers V] {i j : ΞΉ} (r : c.rel i j) : (C.boundaries j : V) β‰… image (C.d i j) := subobject.iso_of_eq _ _ (C.boundaries_eq_image_subobject r) β‰ͺ≫ image_subobject_iso (C.d i j) lemma boundaries_eq_bot {j} (h : c.prev j = none) : C.boundaries j = βŠ₯ := begin rw eq_bot_iff, refine image_subobject_le _ 0 _, rw [C.d_to_eq_zero h, zero_comp], end end boundaries section variables [has_kernels V] [has_images V] lemma boundaries_le_cycles (C : homological_complex V c) (i : ΞΉ) : C.boundaries i ≀ C.cycles i := image_le_kernel _ _ (C.d_to_comp_d_from i) /-- The canonical map from `boundaries i` to `cycles i`. -/ abbreviation boundaries_to_cycles (C : homological_complex V c) (i : ΞΉ) : (C.boundaries i : V) ⟢ (C.cycles i : V) := image_to_kernel _ _ (C.d_to_comp_d_from i) /-- Prefer `boundaries_to_cycles`. -/ @[simp] lemma image_to_kernel_as_boundaries_to_cycles (C : homological_complex V c) (i : ΞΉ) (h) : (C.boundaries i).of_le (C.cycles i) h = C.boundaries_to_cycles i := rfl @[simp, reassoc] lemma boundaries_to_cycles_arrow (C : homological_complex V c) (i : ΞΉ) : C.boundaries_to_cycles i ≫ (C.cycles i).arrow = (C.boundaries i).arrow := by { dsimp [cycles], simp, } variables [has_cokernels V] /-- The homology of a complex at index `i`. -/ abbreviation homology (C : homological_complex V c) (i : ΞΉ) : V := homology (C.d_to i) (C.d_from i) (C.d_to_comp_d_from i) end end homological_complex open homological_complex /-! Computing the cycles is functorial. -/ section variables [has_zero_object V] [has_kernels V] variables {C₁ Cβ‚‚ C₃ : homological_complex V c} (f : C₁ ⟢ Cβ‚‚) /-- The morphism between cycles induced by a chain map. -/ abbreviation cycles_map (f : C₁ ⟢ Cβ‚‚) (i : ΞΉ) : (C₁.cycles i : V) ⟢ (Cβ‚‚.cycles i : V) := subobject.factor_thru _ ((C₁.cycles i).arrow ≫ f.f i) (kernel_subobject_factors _ _ (by simp)) @[simp] lemma cycles_map_arrow (f : C₁ ⟢ Cβ‚‚) (i : ΞΉ) : (cycles_map f i) ≫ (Cβ‚‚.cycles i).arrow = (C₁.cycles i).arrow ≫ f.f i := by { simp, } @[simp] lemma cycles_map_id (i : ΞΉ) : cycles_map (πŸ™ C₁) i = πŸ™ _ := by { dunfold cycles_map, simp, } @[simp] lemma cycles_map_comp (f : C₁ ⟢ Cβ‚‚) (g : Cβ‚‚ ⟢ C₃) (i : ΞΉ) : cycles_map (f ≫ g) i = cycles_map f i ≫ cycles_map g i := by { dunfold cycles_map, simp [subobject.factor_thru_right], } variables (V c) /-- Cycles as a functor. -/ @[simps] def cycles_functor (i : ΞΉ) : homological_complex V c β₯€ V := { obj := Ξ» C, C.cycles i, map := Ξ» C₁ Cβ‚‚ f, cycles_map f i, } end /-! Computing the boundaries is functorial. -/ section variables [has_zero_object V] [has_images V] [has_image_maps V] variables {C₁ Cβ‚‚ C₃ : homological_complex V c} (f : C₁ ⟢ Cβ‚‚) /-- The morphism between boundaries induced by a chain map. -/ abbreviation boundaries_map (f : C₁ ⟢ Cβ‚‚) (i : ΞΉ) : (C₁.boundaries i : V) ⟢ (Cβ‚‚.boundaries i : V) := image_subobject_map (f.sq_to i) variables (V c) /-- Boundaries as a functor. -/ @[simps] def boundaries_functor (i : ΞΉ) : homological_complex V c β₯€ V := { obj := Ξ» C, C.boundaries i, map := Ξ» C₁ Cβ‚‚ f, image_subobject_map (f.sq_to i), } end section /-! The `boundaries_to_cycles` morphisms are natural. -/ variables [has_zero_object V] [has_equalizers V] [has_images V] [has_image_maps V] variables {C₁ Cβ‚‚ : homological_complex V c} (f : C₁ ⟢ Cβ‚‚) @[simp, reassoc] lemma boundaries_to_cycles_naturality (i : ΞΉ) : boundaries_map f i ≫ Cβ‚‚.boundaries_to_cycles i = C₁.boundaries_to_cycles i ≫ cycles_map f i := by { ext, simp, } variables (V c) /-- The natural transformation from the boundaries functor to the cycles functor. -/ @[simps] def boundaries_to_cycles_nat_trans (i : ΞΉ) : boundaries_functor V c i ⟢ cycles_functor V c i := { app := Ξ» C, C.boundaries_to_cycles i, naturality' := Ξ» C₁ Cβ‚‚ f, boundaries_to_cycles_naturality f i, } /-- The `i`-th homology, as a functor to `V`. -/ @[simps] def homology_functor [has_cokernels V] (i : ΞΉ) : homological_complex V c β₯€ V := -- It would be nice if we could just write -- `cokernel (boundaries_to_cycles_nat_trans V c i)` -- here, but universe implementation details get in the way... { obj := Ξ» C, C.homology i, map := Ξ» C₁ Cβ‚‚ f, _root_.homology.map _ _ (f.sq_to i) (f.sq_from i) rfl, map_id' := begin intros, ext1, simp only [homology.Ο€_map, kernel_subobject_map_id, hom.sq_from_id, category.id_comp, category.comp_id] end, map_comp' := begin intros, ext1, simp only [hom.sq_from_comp, kernel_subobject_map_comp, homology.Ο€_map_assoc, homology.Ο€_map, category.assoc] end } /-- The homology functor from `ΞΉ`-indexed complexes to `ΞΉ`-graded objects in `V`. -/ @[simps] def graded_homology_functor [has_cokernels V] : homological_complex V c β₯€ graded_object ΞΉ V := { obj := Ξ» C i, C.homology i, map := Ξ» C C' f i, (homology_functor V c i).map f, map_id' := begin intros, ext, simp only [pi.id_apply, homology.Ο€_map, homology_functor_map, kernel_subobject_map_id, hom.sq_from_id, category.id_comp, category.comp_id] end, map_comp' := begin intros, ext, simp only [hom.sq_from_comp, kernel_subobject_map_comp, homology.Ο€_map_assoc, pi.comp_apply, homology.Ο€_map, homology_functor_map, category.assoc] end } end
aa5c2048a69aa91ff3dcdcf0db2f418900b127fd
0d22ec5e0205ec4203aae2ddf07e1f695ff21a61
/src/polynomial_derivations.lean
23f1958bebbd2c0d2de2dc7ec17bc835222daf00
[ "Apache-2.0" ]
permissive
shingtaklam1324/step3-06-q8-lean
4611fda6b002797d38d3f625253960dcfeb2a987
20e5161fab8b5c3c2dd051bd707a26f1dc50dac2
refs/heads/master
1,649,936,589,910
1,585,242,273,000
1,585,242,273,000
250,318,324
2
0
Apache-2.0
1,585,389,271,000
1,585,241,690,000
Lean
UTF-8
Lean
false
false
5,021
lean
/- Copyright (c) 2020 Shing Tak Lam. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Shing Tak Lam -/ import tactic data.polynomial data.real.basic -- set_option profiler true /-! # Polynomial derivations We prove the structure theorem for polynomial derivations. # Main definitions * `polynomial_derivation (R : Type) [comm_ring R]` : the type of `R`-derivations on `R[X]`. # Main statements We prove the structure theorem, that every polynomial derivation is equal to an R[X]-linear multiple of polynomial differentiation. Proof that there is an equivalence between polynomials and polynomial derivations, sending a polynomial h to the derivation mapping f to h*derivative(f). proof that differentiation is a derivation. # Further work -/ -- start of file /-- A polynomial derivation on a ring R is a function d : R[X] β†’ R[X] satisfying three axioms: map_add : βˆ€ {f g : polynomial R} , d (f + g) = d f + d g map_C_mul : βˆ€ (k : R) (f : polynomial R), d (C k * f) = C k * d f map_mul : βˆ€ (f g : polynomial R) , d (f * g) = f * d g + g * d f -/ structure polynomial_derivation (R : Type) [comm_ring R] := (to_fun : polynomial R β†’ polynomial R) -- Delta (map_add' : βˆ€ (f g : polynomial R), to_fun (f + g) = to_fun f + to_fun g) (map_C_mul' : βˆ€ (k : R) (f : polynomial R), to_fun (polynomial.C k * f) = polynomial.C k * to_fun f) (map_mul' : βˆ€ (f g : polynomial R), to_fun (f * g) = f * to_fun g + g * to_fun f) open polynomial namespace polynomial_derivation variables {R : Type} [comm_ring R] /-- think of a polynomial derivation as a function from R[X] to R[X] -/ instance : has_coe_to_fun (polynomial_derivation R) := { F := Ξ» _, polynomial R β†’ polynomial R, coe := to_fun } @[simp] theorem map_add (d : polynomial_derivation R) : βˆ€ {f g : polynomial R}, d (f + g) = d f + d g := d.map_add' @[simp] theorem map_mul (d : polynomial_derivation R): βˆ€ (f g : polynomial R), d (f * g) = f * d g + g * d f := d.map_mul' @[simp] theorem map_C_mul (d : polynomial_derivation R): βˆ€ (k : R) (f : polynomial R), d (C k * f) = C k * d f := d.map_C_mul' @[simp] lemma map_one (d : polynomial_derivation R) : d 1 = 0 := begin have hd : d 1 + d 1 = d 1 := begin conv begin to_rhs, rw (show (1 : polynomial R) = 1 * 1, by simp), end, rw map_mul, ring, end, rwa add_left_eq_self at hd, end @[simp] lemma map_C (d : polynomial_derivation R) (a : R) : d (C a) = 0 := by rw [(show C a = C a * (1 : polynomial R), from (mul_one _).symm), map_C_mul, map_one, mul_zero] lemma map_pow_succ_aux (n : β„•) (d : polynomial_derivation R) : X * ((↑n + 1) * X ^ n * d X) + X * X ^ n * d X = (↑n + 1 + 1) * (X * X ^ n) * d X := by ring -- Leibniz rule lemma map_pow_succ (d : polynomial_derivation R) (n : β„•) : d (X ^ (n + 1)) = (n + 1)*X^n * d(X) := begin induction n with n IH, {simp}, rw pow_succ, rw map_mul, rw IH, simp only [pow_succ, nat.succ_eq_add_one], push_cast, exact map_pow_succ_aux n d, end lemma structure_theorem_aux' (a : R) (n : β„•) (d : polynomial_derivation R) : C a * X ^ n * d X + X * (d X * derivative (C a * X ^ n)) = d X * (derivative (C a * X ^ n) * X + C a * X ^ n * 1) := by ring /-- structure theorem for polynomial derivations -/ theorem structure_theorem (d : polynomial_derivation R) (f : polynomial R): d f = d X * polynomial.derivative f := begin apply f.induction_on, {intro a, simp [map_C]}, { intros _ _ hp hq, rw [map_add, hp, hq, derivative_add, mul_add],}, intros n a h, rw [pow_succ, mul_comm X, ←mul_assoc, map_mul, derivative_mul, h, derivative_X], exact structure_theorem_aux' a n d, end @[ext] theorem ext : βˆ€ {d₁ dβ‚‚ : polynomial_derivation R} (H : βˆ€ f, d₁ f = dβ‚‚ f), d₁ = dβ‚‚ | ⟨_, _, _, _⟩ ⟨_, _, _, _⟩ H := by congr; exact funext H lemma structure_classification_aux1 (j : polynomial R) (k : R) (f : polynomial R) : j * (0 * f + C k * derivative f) = C k * (j * derivative f) := by ring lemma structure_classification_aux2 (j f g : polynomial R) : j * (derivative f * g + f * derivative g) = f * (j * derivative g) + g * (j * derivative f) := by ring noncomputable definition structure_classification (R : Type) [comm_ring R] : polynomial_derivation R ≃ polynomial R := { to_fun := Ξ» d, d X, inv_fun := Ξ» j, { to_fun := Ξ» f, j * f.derivative, map_add' := Ξ» _ _, by rw [derivative_add, mul_add], map_C_mul' := begin intros, rw [derivative_mul, derivative_C], exact structure_classification_aux1 j k f, end, map_mul' := begin intros, rw derivative_mul, exact structure_classification_aux2 j f g, end }, left_inv := begin intro d, ext1 f, rw structure_theorem d f, refl, end, right_inv := begin intro p, change p * derivative X = p, simp only [mul_one, polynomial.derivative_X], end } end polynomial_derivation
526bd9c2897d01bf520b3b1964c85b1cbbad5668
87a08a8e9b222ec02f3327dca4ae24590c1b3de9
/src/measure_theory/measure_space.lean
5565e4f4e45c64bcc9c57dd9d960961780c63fe5
[ "Apache-2.0" ]
permissive
naussicaa/mathlib
86d05223517a39e80920549a8052f9cf0e0b77b8
1ef2c2df20cf45c21675d855436228c7ae02d47a
refs/heads/master
1,592,104,950,080
1,562,073,069,000
1,562,073,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
35,098
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 Measure spaces -- measures Measures are restricted to a measurable space (associated by the type class `measurable_space`). This allows us to prove equalities between measures by restricting to a generating set of the measurable space. On the other hand, the `ΞΌ.measure s` projection (i.e. the measure of `s` on the measure space `ΞΌ`) is the _outer_ measure generated by `ΞΌ`. This gives us a unrestricted monotonicity rule and it is somehow well-behaved on non-measurable sets. This allows us for the `lebesgue` measure space to have the `borel` measurable space, but still be a complete measure. -/ import data.set.lattice data.set.finite import topology.instances.ennreal measure_theory.outer_measure noncomputable theory open classical set lattice filter finset function local attribute [instance] prop_decidable universes u v w x namespace measure_theory section of_measurable parameters {Ξ± : Type*} [measurable_space Ξ±] parameters (m : Ξ  (s : set Ξ±), is_measurable s β†’ ennreal) parameters (m0 : m βˆ… is_measurable.empty = 0) include m0 /-- Measure projection which is ∞ for non-measurable sets. `measure'` is mainly used to derive the outer measure, for the main `measure` projection. -/ def measure' (s : set Ξ±) : ennreal := β¨… h : is_measurable s, m s h lemma measure'_eq {s} (h : is_measurable s) : measure' s = m s h := by simp [measure', h] lemma measure'_empty : measure' βˆ… = 0 := (measure'_eq is_measurable.empty).trans m0 lemma measure'_Union_nat {f : β„• β†’ set Ξ±} (hm : βˆ€i, is_measurable (f i)) (mU : m (⋃i, f i) (is_measurable.Union hm) = (βˆ‘i, m (f i) (hm i))) : measure' (⋃i, f i) = (βˆ‘i, measure' (f i)) := (measure'_eq _).trans $ mU.trans $ by congr; funext i; rw measure'_eq /-- outer measure of a measure -/ def outer_measure' : outer_measure Ξ± := outer_measure.of_function measure' measure'_empty lemma measure'_Union_le_tsum_nat' (mU : βˆ€ {f : β„• β†’ set Ξ±} (hm : βˆ€i, is_measurable (f i)), m (⋃i, f i) (is_measurable.Union hm) ≀ (βˆ‘i, m (f i) (hm i))) (s : β„• β†’ set Ξ±) : measure' (⋃i, s i) ≀ (βˆ‘i, measure' (s i)) := begin by_cases h : βˆ€i, is_measurable (s i), { rw [measure'_eq _ _ (is_measurable.Union h), congr_arg tsum _], {apply mU h}, funext i, apply measure'_eq _ _ (h i) }, { cases not_forall.1 h with i hi, exact le_trans (le_infi $ Ξ» h, hi.elim h) (ennreal.le_tsum i) } end parameter (mU : βˆ€ {f : β„• β†’ set Ξ±} (hm : βˆ€i, is_measurable (f i)), pairwise (disjoint on f) β†’ m (⋃i, f i) (is_measurable.Union hm) = (βˆ‘i, m (f i) (hm i))) include mU lemma measure'_Union {Ξ²} [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (hd : pairwise (disjoint on f)) (hm : βˆ€i, is_measurable (f i)) : measure' (⋃i, f i) = (βˆ‘i, measure' (f i)) := begin rw [encodable.Union_decode2, outer_measure.Union_aux], { exact measure'_Union_nat _ _ (Ξ» n, encodable.Union_decode2_cases is_measurable.empty hm) (mU _ (measurable_space.Union_decode2_disjoint_on hd)) }, { apply measure'_empty }, end lemma measure'_union {s₁ sβ‚‚ : set Ξ±} (hd : disjoint s₁ sβ‚‚) (h₁ : is_measurable s₁) (hβ‚‚ : is_measurable sβ‚‚) : measure' (s₁ βˆͺ sβ‚‚) = measure' s₁ + measure' sβ‚‚ := begin rw [union_eq_Union, measure'_Union _ _ @mU (pairwise_disjoint_on_bool.2 hd) (bool.forall_bool.2 ⟨hβ‚‚, hβ‚βŸ©), tsum_fintype], change _+_ = _, simp end lemma measure'_mono {s₁ sβ‚‚ : set Ξ±} (h₁ : is_measurable s₁) (hs : s₁ βŠ† sβ‚‚) : measure' s₁ ≀ measure' sβ‚‚ := le_infi $ Ξ» hβ‚‚, begin have := measure'_union _ _ @mU disjoint_diff h₁ (hβ‚‚.diff h₁), rw union_diff_cancel hs at this, rw ← measure'_eq m m0 _, exact le_iff_exists_add.2 ⟨_, this⟩ end lemma measure'_Union_le_tsum_nat : βˆ€ (s : β„• β†’ set Ξ±), measure' (⋃i, s i) ≀ (βˆ‘i, measure' (s i)) := measure'_Union_le_tsum_nat' $ Ξ» f h, begin simp [Union_disjointed.symm] {single_pass := tt}, rw [mU (is_measurable.disjointed h) disjoint_disjointed], refine ennreal.tsum_le_tsum (Ξ» i, _), rw [← measure'_eq m m0, ← measure'_eq m m0], exact measure'_mono _ _ @mU (is_measurable.disjointed h _) (inter_subset_left _ _) end lemma outer_measure'_eq {s : set Ξ±} (hs : is_measurable s) : outer_measure' s = m s hs := by rw ← measure'_eq m m0 hs; exact (le_antisymm (outer_measure.of_function_le _ _ _) $ le_infi $ Ξ» f, le_infi $ Ξ» hf, le_trans (measure'_mono _ _ @mU hs hf) $ measure'_Union_le_tsum_nat _ _ @mU _) lemma outer_measure'_eq_measure' {s : set Ξ±} (hs : is_measurable s) : outer_measure' s = measure' s := by rw [measure'_eq m m0 hs, outer_measure'_eq m m0 @mU hs] end of_measurable namespace outer_measure variables {Ξ± : Type*} [measurable_space Ξ±] (m : outer_measure Ξ±) def trim : outer_measure Ξ± := outer_measure' (Ξ» s _, m s) m.empty theorem trim_ge : m ≀ m.trim := Ξ» s, le_infi $ Ξ» f, le_infi $ Ξ» hs, le_trans (m.mono hs) $ le_trans (m.Union_nat f) $ ennreal.tsum_le_tsum $ Ξ» i, le_infi $ Ξ» hf, le_refl _ theorem trim_eq {s : set Ξ±} (hs : is_measurable s) : m.trim s = m s := le_antisymm (le_trans (of_function_le _ _ _) (infi_le _ hs)) (trim_ge _ _) theorem trim_congr {m₁ mβ‚‚ : outer_measure Ξ±} (H : βˆ€ {s : set Ξ±}, is_measurable s β†’ m₁ s = mβ‚‚ s) : m₁.trim = mβ‚‚.trim := by unfold trim; congr; funext s hs; exact H hs theorem trim_le_trim {m₁ mβ‚‚ : outer_measure Ξ±} (H : m₁ ≀ mβ‚‚) : m₁.trim ≀ mβ‚‚.trim := Ξ» s, infi_le_infi $ Ξ» f, infi_le_infi $ Ξ» hs, ennreal.tsum_le_tsum $ Ξ» b, infi_le_infi $ Ξ» hf, H _ theorem le_trim_iff {m₁ mβ‚‚ : outer_measure Ξ±} : m₁ ≀ mβ‚‚.trim ↔ βˆ€ s, is_measurable s β†’ m₁ s ≀ mβ‚‚ s := le_of_function.trans $ forall_congr $ Ξ» s, le_infi_iff theorem trim_eq_infi (s : set Ξ±) : m.trim s = β¨… t (st : s βŠ† t) (ht : is_measurable t), m t := begin refine le_antisymm (le_infi $ Ξ» t, le_infi $ Ξ» st, le_infi $ Ξ» ht, _) (le_infi $ Ξ» f, le_infi $ Ξ» hf, _), { rw ← trim_eq m ht, exact (trim m).mono st }, { by_cases h : βˆ€i, is_measurable (f i), { refine infi_le_of_le _ (infi_le_of_le hf $ infi_le_of_le (is_measurable.Union h) _), rw congr_arg tsum _, {exact m.Union_nat _}, funext i, exact measure'_eq _ _ (h i) }, { cases not_forall.1 h with i hi, exact le_trans (le_infi $ Ξ» h, hi.elim h) (ennreal.le_tsum i) } } end theorem trim_eq_infi' (s : set Ξ±) : m.trim s = β¨… t : {t // s βŠ† t ∧ is_measurable t}, m t.1 := by simp [infi_subtype, infi_and, trim_eq_infi] theorem trim_trim (m : outer_measure Ξ±) : m.trim.trim = m.trim := le_antisymm (le_trim_iff.2 $ Ξ» s hs, by simp [trim_eq _ hs, le_refl]) (trim_ge _) theorem trim_zero : (0 : outer_measure Ξ±).trim = 0 := ext $ Ξ» s, le_antisymm (le_trans ((trim 0).mono (subset_univ s)) $ le_of_eq $ trim_eq _ is_measurable.univ) (zero_le _) theorem trim_add (m₁ mβ‚‚ : outer_measure Ξ±) : (m₁ + mβ‚‚).trim = m₁.trim + mβ‚‚.trim := ext $ Ξ» s, begin simp [trim_eq_infi'], rw ennreal.infi_add_infi, rintro ⟨t₁, st₁, htβ‚βŸ© ⟨tβ‚‚, stβ‚‚, htβ‚‚βŸ©, exact ⟨⟨_, subset_inter_iff.2 ⟨st₁, stβ‚‚βŸ©, ht₁.inter htβ‚‚βŸ©, add_le_add' (m₁.mono' (inter_subset_left _ _)) (mβ‚‚.mono' (inter_subset_right _ _))⟩, end theorem trim_sum_ge {ΞΉ} (m : ΞΉ β†’ outer_measure Ξ±) : sum (Ξ» i, (m i).trim) ≀ (sum m).trim := Ξ» s, by simp [trim_eq_infi]; exact Ξ» t st ht, ennreal.tsum_le_tsum (Ξ» i, infi_le_of_le t $ infi_le_of_le st $ infi_le _ ht) end outer_measure structure measure (Ξ± : Type*) [measurable_space Ξ±] extends outer_measure Ξ± := (m_Union {f : β„• β†’ set Ξ±} : (βˆ€i, is_measurable (f i)) β†’ pairwise (disjoint on f) β†’ measure_of (⋃i, f i) = (βˆ‘i, measure_of (f i))) (trimmed : to_outer_measure.trim = to_outer_measure) /-- Measure projections for a measure space. For measurable sets this returns the measure assigned by the `measure_of` field in `measure`. But we can extend this to _all_ sets, but using the outer measure. This gives us monotonicity and subadditivity for all sets. -/ instance measure.has_coe_to_fun {Ξ±} [measurable_space Ξ±] : has_coe_to_fun (measure Ξ±) := ⟨λ _, set Ξ± β†’ ennreal, Ξ» m, m.to_outer_measure⟩ namespace measure def of_measurable {Ξ±} [measurable_space Ξ±] (m : Ξ  (s : set Ξ±), is_measurable s β†’ ennreal) (m0 : m βˆ… is_measurable.empty = 0) (mU : βˆ€ {f : β„• β†’ set Ξ±} (h : βˆ€i, is_measurable (f i)), pairwise (disjoint on f) β†’ m (⋃i, f i) (is_measurable.Union h) = (βˆ‘i, m (f i) (h i))) : measure Ξ± := { m_Union := Ξ» f hf hd, show outer_measure' m m0 (Union f) = βˆ‘ i, outer_measure' m m0 (f i), begin rw [outer_measure'_eq m m0 @mU, mU hf hd], congr, funext n, rw outer_measure'_eq m m0 @mU end, trimmed := show (outer_measure' m m0).trim = outer_measure' m m0, begin unfold outer_measure.trim, congr, funext s hs, exact outer_measure'_eq m m0 @mU hs end, ..outer_measure' m m0 } lemma of_measurable_apply {Ξ±} [measurable_space Ξ±] {m : Ξ  (s : set Ξ±), is_measurable s β†’ ennreal} {m0 : m βˆ… is_measurable.empty = 0} {mU : βˆ€ {f : β„• β†’ set Ξ±} (h : βˆ€i, is_measurable (f i)), pairwise (disjoint on f) β†’ m (⋃i, f i) (is_measurable.Union h) = (βˆ‘i, m (f i) (h i))} (s : set Ξ±) (hs : is_measurable s) : of_measurable m m0 @mU s = m s hs := outer_measure'_eq m m0 @mU hs @[extensionality] lemma ext {Ξ±} [measurable_space Ξ±] : βˆ€ {μ₁ ΞΌβ‚‚ : measure Ξ±}, (βˆ€s, is_measurable s β†’ μ₁ s = ΞΌβ‚‚ s) β†’ μ₁ = ΞΌβ‚‚ | ⟨m₁, u₁, hβ‚βŸ© ⟨mβ‚‚, uβ‚‚, hβ‚‚βŸ© h := by congr; rw [← h₁, ← hβ‚‚]; exact outer_measure.trim_congr h end measure section variables {Ξ± : Type*} {Ξ² : Type*} [measurable_space Ξ±] {ΞΌ μ₁ ΞΌβ‚‚ : measure Ξ±} {s s₁ sβ‚‚ : set Ξ±} @[simp] lemma to_outer_measure_apply (s) : ΞΌ.to_outer_measure s = ΞΌ s := rfl lemma measure_eq_trim (s) : ΞΌ s = ΞΌ.to_outer_measure.trim s := by rw ΞΌ.trimmed; refl lemma measure_eq_infi (s) : ΞΌ s = β¨… t (st : s βŠ† t) (ht : is_measurable t), ΞΌ t := by rw [measure_eq_trim, outer_measure.trim_eq_infi]; refl lemma measure_eq_outer_measure' : ΞΌ s = outer_measure' (Ξ» s _, ΞΌ s) ΞΌ.empty s := measure_eq_trim _ lemma to_outer_measure_eq_outer_measure' : ΞΌ.to_outer_measure = outer_measure' (Ξ» s _, ΞΌ s) ΞΌ.empty := ΞΌ.trimmed.symm lemma measure_eq_measure' (hs : is_measurable s) : ΞΌ s = measure' (Ξ» s _, ΞΌ s) ΞΌ.empty s := by rw [measure_eq_outer_measure', outer_measure'_eq_measure' (Ξ» s _, ΞΌ s) _ ΞΌ.m_Union hs] @[simp] lemma measure_empty : ΞΌ βˆ… = 0 := ΞΌ.empty lemma measure_mono (h : s₁ βŠ† sβ‚‚) : ΞΌ s₁ ≀ ΞΌ sβ‚‚ := ΞΌ.mono h lemma measure_mono_null (h : s₁ βŠ† sβ‚‚) (hβ‚‚ : ΞΌ sβ‚‚ = 0) : ΞΌ s₁ = 0 := by rw [← le_zero_iff_eq, ← hβ‚‚]; exact measure_mono h lemma exists_is_measurable_superset_of_measure_eq_zero {s : set Ξ±} (h : ΞΌ s = 0) : βˆƒt, s βŠ† t ∧ is_measurable t ∧ ΞΌ t = 0 := begin rw [measure_eq_infi] at h, have h := (infi_eq_bot _).1 h, choose t ht using show βˆ€n:β„•, βˆƒt, s βŠ† t ∧ is_measurable t ∧ ΞΌ t < n⁻¹, { assume n, have : (0 : ennreal) < n⁻¹ := (zero_lt_iff_ne_zero.2 $ ennreal.inv_ne_zero.2 $ ennreal.nat_ne_top _), rcases h _ this with ⟨t, ht⟩, use [t], simpa [(>), infi_lt_iff, -add_comm] using ht }, refine βŸ¨β‹‚n, t n, subset_Inter (Ξ»n, (ht n).1), is_measurable.Inter (Ξ»n, (ht n).2.1), _⟩, refine eq_of_le_of_forall_le_of_dense bot_le (assume r hr, _), rcases ennreal.exists_inv_nat_lt (ne_of_gt hr) with ⟨n, hn⟩, calc ΞΌ (β‹‚n, t n) ≀ ΞΌ (t n) : measure_mono (Inter_subset _ _) ... ≀ n⁻¹ : le_of_lt (ht n).2.2 ... ≀ r : le_of_lt hn end theorem measure_Union_le {Ξ²} [encodable Ξ²] (s : Ξ² β†’ set Ξ±) : ΞΌ (⋃i, s i) ≀ (βˆ‘i, ΞΌ (s i)) := ΞΌ.to_outer_measure.Union _ lemma measure_Union_null {Ξ²} [encodable Ξ²] {s : Ξ² β†’ set Ξ±} : (βˆ€ i, ΞΌ (s i) = 0) β†’ ΞΌ (⋃i, s i) = 0 := ΞΌ.to_outer_measure.Union_null theorem measure_union_le (s₁ sβ‚‚ : set Ξ±) : ΞΌ (s₁ βˆͺ sβ‚‚) ≀ ΞΌ s₁ + ΞΌ sβ‚‚ := ΞΌ.to_outer_measure.union _ _ lemma measure_union_null {s₁ sβ‚‚ : set Ξ±} : ΞΌ s₁ = 0 β†’ ΞΌ sβ‚‚ = 0 β†’ ΞΌ (s₁ βˆͺ sβ‚‚) = 0 := ΞΌ.to_outer_measure.union_null lemma measure_Union {Ξ²} [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (hn : pairwise (disjoint on f)) (h : βˆ€i, is_measurable (f i)) : ΞΌ (⋃i, f i) = (βˆ‘i, ΞΌ (f i)) := by rw [measure_eq_measure' (is_measurable.Union h), measure'_Union (Ξ» s _, ΞΌ s) _ ΞΌ.m_Union hn h]; simp [measure_eq_measure', h] lemma measure_union (hd : disjoint s₁ sβ‚‚) (h₁ : is_measurable s₁) (hβ‚‚ : is_measurable sβ‚‚) : ΞΌ (s₁ βˆͺ sβ‚‚) = ΞΌ s₁ + ΞΌ sβ‚‚ := by rw [measure_eq_measure' (h₁.union hβ‚‚), measure'_union (Ξ» s _, ΞΌ s) _ ΞΌ.m_Union hd h₁ hβ‚‚]; simp [measure_eq_measure', h₁, hβ‚‚] lemma measure_bUnion {s : set Ξ²} {f : Ξ² β†’ set Ξ±} (hs : countable s) (hd : pairwise_on s (disjoint on f)) (h : βˆ€b∈s, is_measurable (f b)) : ΞΌ (⋃b∈s, f b) = βˆ‘p:s, ΞΌ (f p.1) := begin haveI := hs.to_encodable, rw [← measure_Union, bUnion_eq_Union], { rintro ⟨i, hi⟩ ⟨j, hj⟩ ij x ⟨h₁, hβ‚‚βŸ©, exact hd i hi j hj (mt subtype.eq' ij:_) ⟨h₁, hβ‚‚βŸ© }, { simpa } end lemma measure_sUnion {S : set (set Ξ±)} (hs : countable S) (hd : pairwise_on S disjoint) (h : βˆ€s∈S, is_measurable s) : ΞΌ (⋃₀ S) = βˆ‘s:S, ΞΌ s.1 := by rw [sUnion_eq_bUnion, measure_bUnion hs hd h] lemma measure_diff {s₁ sβ‚‚ : set Ξ±} (h : sβ‚‚ βŠ† s₁) (h₁ : is_measurable s₁) (hβ‚‚ : is_measurable sβ‚‚) (h_fin : ΞΌ sβ‚‚ < ⊀) : ΞΌ (s₁ \ sβ‚‚) = ΞΌ s₁ - ΞΌ sβ‚‚ := begin refine (ennreal.add_sub_self' h_fin).symm.trans _, rw [← measure_union disjoint_diff hβ‚‚ (h₁.diff hβ‚‚), union_diff_cancel h] end lemma measure_Union_eq_supr_nat {s : β„• β†’ set Ξ±} (h : βˆ€i, is_measurable (s i)) (hs : monotone s) : ΞΌ (⋃i, s i) = (⨆i, ΞΌ (s i)) := begin refine le_antisymm _ (supr_le $ Ξ» i, measure_mono $ subset_Union _ _), rw [← Union_disjointed, measure_Union disjoint_disjointed (is_measurable.disjointed h), ennreal.tsum_eq_supr_nat], refine supr_le (Ξ» n, _), cases n, {apply zero_le _}, suffices : sum (finset.range n.succ) (Ξ» i, ΞΌ (disjointed s i)) = ΞΌ (s n), { rw this, exact le_supr _ n }, rw [← Union_disjointed_of_mono hs, measure_Union, tsum_eq_sum], { apply sum_congr rfl, intros i hi, simp [finset.mem_range.1 hi] }, { intros i hi, simp [mt finset.mem_range.2 hi] }, { rintro i j ij x ⟨⟨_, ⟨_, rfl⟩, hβ‚βŸ©, ⟨_, ⟨_, rfl⟩, hβ‚‚βŸ©βŸ©, exact disjoint_disjointed i j ij ⟨h₁, hβ‚‚βŸ© }, { intro i, by_cases h' : i < n.succ; simp [h', is_measurable.empty], apply is_measurable.disjointed h } end lemma measure_Inter_eq_infi_nat {s : β„• β†’ set Ξ±} (h : βˆ€i, is_measurable (s i)) (hs : βˆ€i j, i ≀ j β†’ s j βŠ† s i) (hfin : βˆƒi, ΞΌ (s i) < ⊀) : ΞΌ (β‹‚i, s i) = (β¨…i, ΞΌ (s i)) := begin rcases hfin with ⟨k, hk⟩, rw [← ennreal.sub_sub_cancel (by exact hk) (infi_le _ k), ennreal.sub_infi, ← ennreal.sub_sub_cancel (by exact hk) (measure_mono (Inter_subset _ k)), ← measure_diff (Inter_subset _ k) (h k) (is_measurable.Inter h) (lt_of_le_of_lt (measure_mono (Inter_subset _ k)) hk), diff_Inter, measure_Union_eq_supr_nat], { congr, funext i, cases le_total k i with ik ik, { exact measure_diff (hs _ _ ik) (h k) (h i) (lt_of_le_of_lt (measure_mono (hs _ _ ik)) hk) }, { rw [diff_eq_empty.2 (hs _ _ ik), measure_empty, ennreal.sub_eq_zero_of_le (measure_mono (hs _ _ ik))] } }, { exact Ξ» i, (h k).diff (h i) }, { exact Ξ» i j ij, diff_subset_diff_right (hs _ _ ij) } end lemma measure_eq_inter_diff {ΞΌ : measure Ξ±} {s t : set Ξ±} (hs : is_measurable s) (ht : is_measurable t) : ΞΌ s = ΞΌ (s ∩ t) + ΞΌ (s \ t) := have hd : disjoint (s ∩ t) (s \ t) := assume a ⟨⟨_, hs⟩, _, hns⟩, hns hs , by rw [← measure_union hd (hs.inter ht) (hs.diff ht), inter_union_diff s t] lemma tendsto_measure_Union {ΞΌ : measure Ξ±} {s : β„• β†’ set Ξ±} (hs : βˆ€n, is_measurable (s n)) (hm : monotone s) : tendsto (ΞΌ ∘ s) at_top (nhds (ΞΌ (⋃n, s n))) := begin rw measure_Union_eq_supr_nat hs hm, exact tendsto_at_top_supr_nat (ΞΌ ∘ s) (assume n m hnm, measure_mono $ hm $ hnm) end lemma tendsto_measure_Inter {ΞΌ : measure Ξ±} {s : β„• β†’ set Ξ±} (hs : βˆ€n, is_measurable (s n)) (hm : βˆ€n m, n ≀ m β†’ s m βŠ† s n) (hf : βˆƒi, ΞΌ (s i) < ⊀): tendsto (ΞΌ ∘ s) at_top (nhds (ΞΌ (β‹‚n, s n))) := begin rw measure_Inter_eq_infi_nat hs hm hf, exact tendsto_at_top_infi_nat (ΞΌ ∘ s) (assume n m hnm, measure_mono $ hm _ _ $ hnm), end end def outer_measure.to_measure {Ξ±} (m : outer_measure Ξ±) [ms : measurable_space Ξ±] (h : ms ≀ m.caratheodory) : measure Ξ± := measure.of_measurable (Ξ» s _, m s) m.empty (Ξ» f hf hd, m.Union_eq_of_caratheodory (Ξ» i, h _ (hf i)) hd) lemma le_to_outer_measure_caratheodory {Ξ±} [ms : measurable_space Ξ±] (ΞΌ : measure Ξ±) : ms ≀ ΞΌ.to_outer_measure.caratheodory := begin assume s hs, rw to_outer_measure_eq_outer_measure', refine outer_measure.caratheodory_is_measurable (Ξ» t, le_infi $ Ξ» ht, _), rw [← measure_eq_measure' (ht.inter hs), ← measure_eq_measure' (ht.diff hs), ← measure_union _ (ht.inter hs) (ht.diff hs), inter_union_diff], exact le_refl _, exact Ξ» x ⟨⟨_, hβ‚βŸ©, _, hβ‚‚βŸ©, hβ‚‚ h₁ end lemma to_measure_to_outer_measure {Ξ±} (m : outer_measure Ξ±) [ms : measurable_space Ξ±] (h : ms ≀ m.caratheodory) : (m.to_measure h).to_outer_measure = m.trim := rfl @[simp] lemma to_measure_apply {Ξ±} (m : outer_measure Ξ±) [ms : measurable_space Ξ±] (h : ms ≀ m.caratheodory) {s : set Ξ±} (hs : is_measurable s) : m.to_measure h s = m s := m.trim_eq hs lemma to_outer_measure_to_measure {Ξ± : Type*} [ms : measurable_space Ξ±] {ΞΌ : measure Ξ±} : ΞΌ.to_outer_measure.to_measure (le_to_outer_measure_caratheodory _) = ΞΌ := measure.ext $ Ξ» s, ΞΌ.to_outer_measure.trim_eq namespace measure variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] instance : has_zero (measure Ξ±) := ⟨{ to_outer_measure := 0, m_Union := Ξ» f hf hd, tsum_zero.symm, trimmed := outer_measure.trim_zero }⟩ @[simp] theorem zero_to_outer_measure : (0 : measure Ξ±).to_outer_measure = 0 := rfl @[simp] theorem zero_apply (s : set Ξ±) : (0 : measure Ξ±) s = 0 := rfl instance : inhabited (measure Ξ±) := ⟨0⟩ instance : has_add (measure Ξ±) := βŸ¨Ξ»ΞΌβ‚ ΞΌβ‚‚, { to_outer_measure := μ₁.to_outer_measure + ΞΌβ‚‚.to_outer_measure, m_Union := Ξ»s hs hd, show μ₁ (⋃ i, s i) + ΞΌβ‚‚ (⋃ i, s i) = βˆ‘ i, μ₁ (s i) + ΞΌβ‚‚ (s i), by rw [ennreal.tsum_add, measure_Union hd hs, measure_Union hd hs], trimmed := by rw [outer_measure.trim_add, μ₁.trimmed, ΞΌβ‚‚.trimmed] }⟩ @[simp] theorem add_to_outer_measure (μ₁ ΞΌβ‚‚ : measure Ξ±) : (μ₁ + ΞΌβ‚‚).to_outer_measure = μ₁.to_outer_measure + ΞΌβ‚‚.to_outer_measure := rfl @[simp] theorem add_apply (μ₁ ΞΌβ‚‚ : measure Ξ±) (s : set Ξ±) : (μ₁ + ΞΌβ‚‚) s = μ₁ s + ΞΌβ‚‚ s := rfl instance : add_comm_monoid (measure Ξ±) := { zero := 0, add := (+), add_assoc := assume a b c, ext $ assume s hs, add_assoc _ _ _, add_comm := assume a b, ext $ assume s hs, add_comm _ _, zero_add := assume a, ext $ assume s hs, zero_add _, add_zero := assume a, ext $ assume s hs, add_zero _ } instance : partial_order (measure Ξ±) := { le := Ξ»m₁ mβ‚‚, βˆ€ s, is_measurable s β†’ m₁ s ≀ mβ‚‚ s, le_refl := assume m s hs, le_refl _, le_trans := assume m₁ mβ‚‚ m₃ h₁ hβ‚‚ s hs, le_trans (h₁ s hs) (hβ‚‚ s hs), le_antisymm := assume m₁ mβ‚‚ h₁ hβ‚‚, ext $ assume s hs, le_antisymm (h₁ s hs) (hβ‚‚ s hs) } theorem le_iff {μ₁ ΞΌβ‚‚ : measure Ξ±} : μ₁ ≀ ΞΌβ‚‚ ↔ βˆ€ s, is_measurable s β†’ μ₁ s ≀ ΞΌβ‚‚ s := iff.rfl theorem to_outer_measure_le {μ₁ ΞΌβ‚‚ : measure Ξ±} : μ₁.to_outer_measure ≀ ΞΌβ‚‚.to_outer_measure ↔ μ₁ ≀ ΞΌβ‚‚ := by rw [← ΞΌβ‚‚.trimmed, outer_measure.le_trim_iff]; refl theorem le_iff' {μ₁ ΞΌβ‚‚ : measure Ξ±} : μ₁ ≀ ΞΌβ‚‚ ↔ βˆ€ s, μ₁ s ≀ ΞΌβ‚‚ s := to_outer_measure_le.symm section variables {m : set (measure Ξ±)} {ΞΌ : measure Ξ±} lemma Inf_caratheodory (s : set Ξ±) (hs : is_measurable s) : (Inf (measure.to_outer_measure '' m)).caratheodory.is_measurable s := begin rw [outer_measure.Inf_eq_of_function_Inf_gen], refine outer_measure.caratheodory_is_measurable (assume t, _), by_cases ht : t = βˆ…, { simp [ht] }, simp only [outer_measure.Inf_gen_nonempty1 _ _ ht, le_infi_iff, ball_image_iff, to_outer_measure_apply, measure_eq_infi t], assume ΞΌ hΞΌ u htu hu, have hm : βˆ€{s t}, s βŠ† t β†’ outer_measure.Inf_gen (to_outer_measure '' m) s ≀ ΞΌ t, { assume s t hst, rw [outer_measure.Inf_gen_nonempty2 _ _ (mem_image_of_mem _ hΞΌ)], refine infi_le_of_le (ΞΌ.to_outer_measure) (infi_le_of_le (mem_image_of_mem _ hΞΌ) _), rw [to_outer_measure_apply], refine measure_mono hst }, rw [measure_eq_inter_diff hu hs], refine add_le_add' (hm $ inter_subset_inter_left _ htu) (hm $ diff_subset_diff_left htu) end instance : has_Inf (measure Ξ±) := ⟨λm, (Inf (to_outer_measure '' m)).to_measure $ Inf_caratheodory⟩ lemma Inf_apply {m : set (measure Ξ±)} {s : set Ξ±} (hs : is_measurable s) : Inf m s = Inf (to_outer_measure '' m) s := to_measure_apply _ _ hs private lemma Inf_le (h : ΞΌ ∈ m) : Inf m ≀ ΞΌ := have Inf (to_outer_measure '' m) ≀ ΞΌ.to_outer_measure := Inf_le (mem_image_of_mem _ h), assume s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s private lemma le_Inf (h : βˆ€ΞΌ' ∈ m, ΞΌ ≀ ΞΌ') : ΞΌ ≀ Inf m := have ΞΌ.to_outer_measure ≀ Inf (to_outer_measure '' m) := le_Inf $ ball_image_of_ball $ assume ΞΌ hΞΌ, to_outer_measure_le.2 $ h _ hΞΌ, assume s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s instance : has_Sup (measure Ξ±) := ⟨λs, Inf {ΞΌ' | βˆ€ΞΌβˆˆs, ΞΌ ≀ ΞΌ' }⟩ private lemma le_Sup (h : ΞΌ ∈ m) : ΞΌ ≀ Sup m := le_Inf $ assume ΞΌ' h', h' _ h private lemma Sup_le (h : βˆ€ΞΌ' ∈ m, ΞΌ' ≀ ΞΌ) : Sup m ≀ ΞΌ := Inf_le h instance : order_bot (measure Ξ±) := { bot := 0, bot_le := assume a s hs, bot_le, .. measure.partial_order } instance : order_top (measure Ξ±) := { top := (⊀ : outer_measure Ξ±).to_measure (by rw [outer_measure.top_caratheodory]; exact le_top), le_top := assume a s hs, by by_cases s = βˆ…; simp [h, to_measure_apply ⊀ _ hs, outer_measure.top_apply], .. measure.partial_order } instance : complete_lattice (measure Ξ±) := { Inf := Inf, Sup := Sup, inf := Ξ»a b, Inf {a, b}, sup := Ξ»a b, Sup {a, b}, le_Sup := assume s ΞΌ h, le_Sup h, Sup_le := assume s ΞΌ h, Sup_le h, Inf_le := assume s ΞΌ h, Inf_le h, le_Inf := assume s ΞΌ h, le_Inf h, le_sup_left := assume a b, le_Sup $ by simp, le_sup_right := assume a b, le_Sup $ by simp, sup_le := assume a b c hac hbc, Sup_le $ by simp [*, or_imp_distrib] {contextual := tt}, inf_le_left := assume a b, Inf_le $ by simp, inf_le_right := assume a b, Inf_le $ by simp, le_inf := assume a b c hac hbc, le_Inf $ by simp [*, or_imp_distrib] {contextual := tt}, .. measure.partial_order, .. measure.lattice.order_top, .. measure.lattice.order_bot } end def map (f : Ξ± β†’ Ξ²) (ΞΌ : measure Ξ±) : measure Ξ² := if hf : measurable f then (ΞΌ.to_outer_measure.map f).to_measure $ Ξ» s hs t, le_to_outer_measure_caratheodory ΞΌ _ (hf _ hs) (f ⁻¹' t) else 0 variables {ΞΌ Ξ½ : measure Ξ±} @[simp] theorem map_apply {f : Ξ± β†’ Ξ²} (hf : measurable f) {s : set Ξ²} (hs : is_measurable s) : (map f ΞΌ : measure Ξ²) s = ΞΌ (f ⁻¹' s) := by rw [map, dif_pos hf, to_measure_apply _ _ hs]; refl @[simp] lemma map_id : map id ΞΌ = ΞΌ := ext $ Ξ» s, map_apply measurable_id lemma map_map {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} (hg : measurable g) (hf : measurable f) : map g (map f ΞΌ) = map (g ∘ f) ΞΌ := ext $ Ξ» s hs, by simp [hf, hg, hs, hg.preimage hs, hg.comp hf]; rw ← preimage_comp /-- The dirac measure. -/ def dirac (a : Ξ±) : measure Ξ± := (outer_measure.dirac a).to_measure (by simp) @[simp] lemma dirac_apply (a : Ξ±) {s : set Ξ±} (hs : is_measurable s) : (dirac a : measure Ξ±) s = ⨆ h : a ∈ s, 1 := to_measure_apply _ _ hs /-- Sum of an indexed family of measures. -/ def sum {ΞΉ : Type*} (f : ΞΉ β†’ measure Ξ±) : measure Ξ± := (outer_measure.sum (Ξ» i, (f i).to_outer_measure)).to_measure $ le_trans (by exact le_infi (Ξ» i, le_to_outer_measure_caratheodory _)) (outer_measure.le_sum_caratheodory _) /-- Counting measure on any measurable space. -/ def count : measure Ξ± := sum dirac @[class] def is_complete {Ξ±} {_:measurable_space Ξ±} (ΞΌ : measure Ξ±) : Prop := βˆ€ s, ΞΌ s = 0 β†’ is_measurable s /-- The "almost everywhere" filter of co-null sets. -/ def a_e (ΞΌ : measure Ξ±) : filter Ξ± := { sets := {s | ΞΌ (-s) = 0}, univ_sets := by simp [measure_empty], inter_sets := Ξ» s t hs ht, by simp [compl_inter]; exact measure_union_null hs ht, sets_of_superset := Ξ» s t hs hst, measure_mono_null (set.compl_subset_compl.2 hst) hs } lemma mem_a_e_iff (s : set Ξ±) : s ∈ ΞΌ.a_e.sets ↔ ΞΌ (- s) = 0 := iff.refl _ end measure end measure_theory section is_complete open measure_theory variables {Ξ± : Type*} [measurable_space Ξ±] (ΞΌ : measure Ξ±) def is_null_measurable (s : set Ξ±) : Prop := βˆƒ t z, s = t βˆͺ z ∧ is_measurable t ∧ ΞΌ z = 0 theorem is_null_measurable_iff {ΞΌ : measure Ξ±} {s : set Ξ±} : is_null_measurable ΞΌ s ↔ βˆƒ t, t βŠ† s ∧ is_measurable t ∧ ΞΌ (s \ t) = 0 := begin split, { rintro ⟨t, z, rfl, ht, hz⟩, refine ⟨t, set.subset_union_left _ _, ht, measure_mono_null _ hz⟩, simp [union_diff_left, diff_subset] }, { rintro ⟨t, st, ht, hz⟩, exact ⟨t, _, (union_diff_cancel st).symm, ht, hz⟩ } end theorem is_null_measurable_measure_eq {ΞΌ : measure Ξ±} {s t : set Ξ±} (st : t βŠ† s) (hz : ΞΌ (s \ t) = 0) : ΞΌ s = ΞΌ t := begin refine le_antisymm _ (measure_mono st), have := measure_union_le t (s \ t), rw [union_diff_cancel st, hz] at this, simpa end theorem is_measurable.is_null_measurable {s : set Ξ±} (hs : is_measurable s) : is_null_measurable ΞΌ s := ⟨s, βˆ…, by simp, hs, ΞΌ.empty⟩ theorem is_null_measurable_of_complete [c : ΞΌ.is_complete] {s : set Ξ±} : is_null_measurable ΞΌ s ↔ is_measurable s := ⟨by rintro ⟨t, z, rfl, ht, hz⟩; exact is_measurable.union ht (c _ hz), Ξ» h, h.is_null_measurable _⟩ variables {ΞΌ} theorem is_null_measurable.union_null {s z : set Ξ±} (hs : is_null_measurable ΞΌ s) (hz : ΞΌ z = 0) : is_null_measurable ΞΌ (s βˆͺ z) := begin rcases hs with ⟨t, z', rfl, ht, hz'⟩, exact ⟨t, z' βˆͺ z, set.union_assoc _ _ _, ht, le_zero_iff_eq.1 (le_trans (measure_union_le _ _) $ by simp [hz, hz'])⟩ end theorem null_is_null_measurable {z : set Ξ±} (hz : ΞΌ z = 0) : is_null_measurable ΞΌ z := by simpa using (is_measurable.empty.is_null_measurable _).union_null hz theorem is_null_measurable.Union_nat {s : β„• β†’ set Ξ±} (hs : βˆ€ i, is_null_measurable ΞΌ (s i)) : is_null_measurable ΞΌ (Union s) := begin choose t ht using assume i, is_null_measurable_iff.1 (hs i), simp [forall_and_distrib] at ht, rcases ht with ⟨st, ht, hz⟩, refine is_null_measurable_iff.2 ⟨Union t, Union_subset_Union st, is_measurable.Union ht, measure_mono_null _ (measure_Union_null hz)⟩, rw [diff_subset_iff, ← Union_union_distrib], exact Union_subset_Union (Ξ» i, by rw ← diff_subset_iff) end theorem is_measurable.diff_null {s z : set Ξ±} (hs : is_measurable s) (hz : ΞΌ z = 0) : is_null_measurable ΞΌ (s \ z) := begin rw measure_eq_infi at hz, choose f hf using show βˆ€ q : {q:β„š//q>0}, βˆƒ t:set Ξ±, z βŠ† t ∧ is_measurable t ∧ ΞΌ t < (nnreal.of_real q.1 : ennreal), { rintro ⟨Ρ, Ξ΅0⟩, have : 0 < (nnreal.of_real Ξ΅ : ennreal), { simpa using Ξ΅0 }, rw ← hz at this, simpa [infi_lt_iff] }, refine is_null_measurable_iff.2 ⟨s \ Inter f, diff_subset_diff_right (subset_Inter (Ξ» i, (hf i).1)), hs.diff (is_measurable.Inter (Ξ» i, (hf i).2.1)), measure_mono_null _ (le_zero_iff_eq.1 $ le_of_not_lt $ Ξ» h, _)⟩, { exact Inter f }, { rw [diff_subset_iff, diff_union_self], exact subset.trans (diff_subset _ _) (subset_union_left _ _) }, rcases ennreal.lt_iff_exists_rat_btwn.1 h with ⟨Ρ, Ξ΅0', Ξ΅0, h⟩, simp at Ξ΅0, apply not_le_of_lt (lt_trans (hf ⟨Ρ, Ξ΅0⟩).2.2 h), exact measure_mono (Inter_subset _ _) end theorem is_null_measurable.diff_null {s z : set Ξ±} (hs : is_null_measurable ΞΌ s) (hz : ΞΌ z = 0) : is_null_measurable ΞΌ (s \ z) := begin rcases hs with ⟨t, z', rfl, ht, hz'⟩, rw [set.union_diff_distrib], exact (ht.diff_null hz).union_null (measure_mono_null (diff_subset _ _) hz') end theorem is_null_measurable.compl {s : set Ξ±} (hs : is_null_measurable ΞΌ s) : is_null_measurable ΞΌ (-s) := begin rcases hs with ⟨t, z, rfl, ht, hz⟩, rw compl_union, exact ht.compl.diff_null hz end def null_measurable {Ξ± : Type u} [measurable_space Ξ±] (ΞΌ : measure Ξ±) : measurable_space Ξ± := { is_measurable := is_null_measurable ΞΌ, is_measurable_empty := is_measurable.empty.is_null_measurable _, is_measurable_compl := Ξ» s hs, hs.compl, is_measurable_Union := Ξ» f, is_null_measurable.Union_nat } def completion {Ξ± : Type u} [measurable_space Ξ±] (ΞΌ : measure Ξ±) : @measure_theory.measure Ξ± (null_measurable ΞΌ) := { to_outer_measure := ΞΌ.to_outer_measure, m_Union := Ξ» s hs hd, show ΞΌ (Union s) = βˆ‘ i, ΞΌ (s i), begin choose t ht using assume i, is_null_measurable_iff.1 (hs i), simp [forall_and_distrib] at ht, rcases ht with ⟨st, ht, hz⟩, rw is_null_measurable_measure_eq (Union_subset_Union st), { rw measure_Union _ ht, { congr, funext i, exact (is_null_measurable_measure_eq (st i) (hz i)).symm }, { rintro i j ij x ⟨h₁, hβ‚‚βŸ©, exact hd i j ij ⟨st i h₁, st j hβ‚‚βŸ© } }, { refine measure_mono_null _ (measure_Union_null hz), rw [diff_subset_iff, ← Union_union_distrib], exact Union_subset_Union (Ξ» i, by rw ← diff_subset_iff) } end, trimmed := begin letI := null_measurable ΞΌ, refine le_antisymm (Ξ» s, _) (outer_measure.trim_ge _), rw outer_measure.trim_eq_infi, dsimp, clear _inst, rw measure_eq_infi s, exact infi_le_infi (Ξ» t, infi_le_infi $ Ξ» st, infi_le_infi2 $ Ξ» ht, ⟨ht.is_null_measurable _, le_refl _⟩) end } instance completion.is_complete {Ξ± : Type u} [measurable_space Ξ±] (ΞΌ : measure Ξ±) : (completion ΞΌ).is_complete := Ξ» z hz, null_is_null_measurable hz end is_complete namespace measure_theory /-- A measure space is a measurable space equipped with a measure, referred to as `volume`. -/ class measure_space (Ξ± : Type*) extends measurable_space Ξ± := (ΞΌ {} : measure Ξ±) section measure_space variables {Ξ± : Type*} [measure_space Ξ±] {s₁ sβ‚‚ : set Ξ±} open measure_space def volume : set Ξ± β†’ ennreal := @ΞΌ Ξ± _ @[simp] lemma volume_empty : volume (βˆ… : set Ξ±) = 0 := ΞΌ.empty lemma volume_mono : s₁ βŠ† sβ‚‚ β†’ volume s₁ ≀ volume sβ‚‚ := measure_mono lemma volume_mono_null : s₁ βŠ† sβ‚‚ β†’ volume sβ‚‚ = 0 β†’ volume s₁ = 0 := measure_mono_null theorem volume_Union_le {Ξ²} [encodable Ξ²] : βˆ€ (s : Ξ² β†’ set Ξ±), volume (⋃i, s i) ≀ (βˆ‘i, volume (s i)) := measure_Union_le lemma volume_Union_null {Ξ²} [encodable Ξ²] {s : Ξ² β†’ set Ξ±} : (βˆ€ i, volume (s i) = 0) β†’ volume (⋃i, s i) = 0 := measure_Union_null theorem volume_union_le : βˆ€ (s₁ sβ‚‚ : set Ξ±), volume (s₁ βˆͺ sβ‚‚) ≀ volume s₁ + volume sβ‚‚ := measure_union_le lemma volume_union_null : volume s₁ = 0 β†’ volume sβ‚‚ = 0 β†’ volume (s₁ βˆͺ sβ‚‚) = 0 := measure_union_null lemma volume_Union {Ξ²} [encodable Ξ²] {f : Ξ² β†’ set Ξ±} : pairwise (disjoint on f) β†’ (βˆ€i, is_measurable (f i)) β†’ volume (⋃i, f i) = (βˆ‘i, volume (f i)) := measure_Union lemma volume_union : disjoint s₁ sβ‚‚ β†’ is_measurable s₁ β†’ is_measurable sβ‚‚ β†’ volume (s₁ βˆͺ sβ‚‚) = volume s₁ + volume sβ‚‚ := measure_union lemma volume_bUnion {Ξ²} {s : set Ξ²} {f : Ξ² β†’ set Ξ±} : countable s β†’ pairwise_on s (disjoint on f) β†’ (βˆ€b∈s, is_measurable (f b)) β†’ volume (⋃b∈s, f b) = βˆ‘p:s, volume (f p.1) := measure_bUnion lemma volume_sUnion {S : set (set Ξ±)} : countable S β†’ pairwise_on S disjoint β†’ (βˆ€s∈S, is_measurable s) β†’ volume (⋃₀ S) = βˆ‘s:S, volume s.1 := measure_sUnion lemma volume_bUnion_finset {Ξ²} {s : finset Ξ²} {f : Ξ² β†’ set Ξ±} (hd : pairwise_on ↑s (disjoint on f)) (hm : βˆ€b∈s, is_measurable (f b)) : volume (⋃b∈s, f b) = s.sum (Ξ»p, volume (f p)) := show volume (⋃b∈(↑s : set Ξ²), f b) = s.sum (Ξ»p, volume (f p)), begin rw [volume_bUnion (countable_finite (finset.finite_to_set s)) hd hm, tsum_eq_sum], { show s.attach.sum (Ξ»b:(↑s : set Ξ²), volume (f b)) = s.sum (Ξ»b, volume (f b)), exact @finset.sum_attach _ _ s _ (Ξ»b, volume (f b)) }, simp end lemma volume_diff : sβ‚‚ βŠ† s₁ β†’ is_measurable s₁ β†’ is_measurable sβ‚‚ β†’ volume sβ‚‚ < ⊀ β†’ volume (s₁ \ sβ‚‚) = volume s₁ - volume sβ‚‚ := measure_diff /-- `βˆ€β‚˜ a:Ξ±, p a` states that the property `p` is almost everywhere true in the measure space associated with `Ξ±`. This means that the measure of the complementary of `p` is `0`. In a probability measure, the measure of `p` is `1`, when `p` is measurable. -/ def all_ae (p : Ξ± β†’ Prop) : Prop := { a | p a } ∈ (@measure_space.ΞΌ Ξ± _).a_e notation `βˆ€β‚˜` binders `, ` r:(scoped P, all_ae P) := r lemma all_ae_congr {p q : Ξ± β†’ Prop} (h : βˆ€β‚˜ a, p a ↔ q a) : (βˆ€β‚˜ a, p a) ↔ (βˆ€β‚˜ a, q a) := iff.intro (assume h', by filter_upwards [h, h'] assume a hpq hp, hpq.1 hp) (assume h', by filter_upwards [h, h'] assume a hpq hq, hpq.2 hq) lemma all_ae_iff {p : Ξ± β†’ Prop} : (βˆ€β‚˜ a, p a) ↔ volume { a | Β¬ p a } = 0 := iff.refl _ lemma all_ae_of_all {p : Ξ± β†’ Prop} : (βˆ€a, p a) β†’ βˆ€β‚˜ a, p a := assume h, by {rw all_ae_iff, convert volume_empty, simp only [h, not_true], reflexivity} lemma all_ae_all_iff {ΞΉ : Type*} [encodable ΞΉ] {p : Ξ± β†’ ΞΉ β†’ Prop} : (βˆ€β‚˜ a, βˆ€i, p a i) ↔ (βˆ€i, βˆ€β‚˜ a, p a i):= begin refine iff.intro (assume h i, _) (assume h, _), { filter_upwards [h] assume a ha, ha i }, { have h := measure_Union_null h, rw [← compl_Inter] at h, filter_upwards [h] assume a, mem_Inter.1 } end end measure_space end measure_theory
7f7fdeb026f7a0203f9583929f04944218d91783
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/tests/lean/run/using_bug2.lean
cf457f683ba74d61614a2e19e19ccccbece9938b
[ "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
412
lean
variable {A : Type} variable {f : A β†’ A β†’ A} variable {finv : A β†’ A} premise (h : βˆ€ x y : A, finv (f x y) = y) theorem foo₁ : βˆ€ x y z : A, f x y = f x z β†’ y = z := Ξ» x y z, assume e, using h, from sorry theorem fooβ‚‚ : βˆ€ x y z : A, f x y = f x z β†’ y = z := Ξ» x y z, assume e, assert s₁ : finv (f x y) = finv (f x z), by rewrite e, show y = z, using h, by rewrite *h at s₁; exact s₁
d67e663964a5a0c4ae0af8fe84f6763785814cba
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/induction1.lean
d5d2c458ca9bd6599f11524ede7704bef472eec0
[ "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
2,424
lean
new_frontend @[recursor 4] def Or.elim2 {p q r : Prop} (major : p ∨ q) (left : p β†’ r) (right : q β†’ r) : r := Or.elim major left right theorem tst0 {p q : Prop } (h : p ∨ q) : q ∨ p := by { induction h; { apply Or.inr; assumption }; { apply Or.inl; assumption } } theorem tst0' {p q : Prop } (h : p ∨ q) : q ∨ p := by induction h focus apply Or.inr assumption focus apply Or.inl assumption theorem tst1 {p q : Prop } (h : p ∨ q) : q ∨ p := by induction h with | inr h2 => exact Or.inl h2 | inl h1 => exact Or.inr h1 theorem tst2 {p q : Prop } (h : p ∨ q) : q ∨ p := by induction h using elim2 with | left _ => apply Or.inr; assumption | right _ => apply Or.inl; assumption theorem tst3 {p q : Prop } (h : p ∨ q) : q ∨ p := by induction h using elim2 with | right h => exact Or.inl h | left h => exact Or.inr h theorem tst4 {p q : Prop } (h : p ∨ q) : q ∨ p := by induction h using elim2 with | right h => ?myright | left h => ?myleft case myleft => exact Or.inr h case myright => exact Or.inl h theorem tst5 {p q : Prop } (h : p ∨ q) : q ∨ p := by induction h using elim2 with | right h => _ | left h => refine Or.inr ?_ exact h case right => exact Or.inl h theorem tst6 {p q : Prop } (h : p ∨ q) : q ∨ p := by { cases h with | inr h2 => exact Or.inl h2 | inl h1 => exact Or.inr h1 } theorem tst7 {Ξ± : Type} (xs : List Ξ±) (h : (a : Ξ±) β†’ (as : List Ξ±) β†’ xs β‰  a :: as) : xs = [] := by { induction xs with | nil => exact rfl | cons z zs ih => exact absurd rfl (h z zs) } theorem tst8 {Ξ± : Type} (xs : List Ξ±) (h : (a : Ξ±) β†’ (as : List Ξ±) β†’ xs β‰  a :: as) : xs = [] := by { induction xs; exact rfl; exact absurd rfl $ h _ _ } theorem tst9 {Ξ± : Type} (xs : List Ξ±) (h : (a : Ξ±) β†’ (as : List Ξ±) β†’ xs β‰  a :: as) : xs = [] := by cases xs with | nil => exact rfl | cons z zs => exact absurd rfl (h z zs) theorem tst10 {p q : Prop } (h₁ : p ↔ q) (hβ‚‚ : p) : q := by induction h₁ using Iff.elim with | _ h _ => exact h hβ‚‚ def Iff2 (m p q : Prop) := p ↔ q theorem tst11 {p q r : Prop } (h₁ : Iff2 r p q) (hβ‚‚ : p) : q := by induction h₁ using Iff.elim with | _ h _ => exact h hβ‚‚ theorem tst12 {p q : Prop } (h₁ : p ∨ q) (hβ‚‚ : p ↔ q) (h₃ : p) : q := by failIfSuccess induction h₁ using Iff.elim induction hβ‚‚ using Iff.elim with | _ h _ => exact h h₃
a81b8f8222a91dc5b43f7f308e9501f7597d35cf
1b8c143de090d160f4629428a34dd6a2addf5264
/Chap2.hlean
7ba523fa907aab1f4ef3e08368891f0b869d4b36
[]
no_license
BinderDavid/HoTT-StudyGroup
23a86e220b6ce70eb49c3a9bf990443e292416d6
8baea70970f2aa59cce5396cf69c3dd1d2b461b1
refs/heads/master
1,620,090,578,419
1,494,946,912,000
1,494,946,912,000
71,147,801
3
2
null
null
null
null
UTF-8
Lean
false
false
1,854
hlean
-- Homotopy Type Theory Book, formalization of the theorems in the book order -- (David Binder) --Nicer pretty printer, print types of bound variables set_option pp.binder_types true --Show the type of the identity eliminator check @eq.rec -- Ξ  {A : Type} {a : A} -- {C : Ξ  {a_1 : A}, a = a_1 β†’ Type}, Any type family over a_1 and proofs a = a_1 -- C a (eq.refl a) β†’ Induction step -- (Ξ  {a_1 : A} (n : a = a_1), C a_1 n) Result of the Eliminator -- The type shows that Lean uses Based Path Induction instead of J -- Chapter 2.1: Types are higher groupoids section chap2 --Lemma 2.1.1: Symmetry --Define a type family P_symm definition P_symm (A : Type) (a : A) := Ξ» (x : A)(n : a = x), x = a check P_symm --Proof: theorem symm (A : Type) (a b : A) : a = b β†’ b = a := have IndHyp : P_symm A a a (eq.refl a), from eq.refl a, --The induction case is proved with refl have Elim : Ξ  {a_1 : A} (n : a = a_1), P_symm A a a_1 n , --The eq.rec applied to the induction case gives the eliminator from @eq.rec A a (P_symm A a) IndHyp, assume Hab : a = b, --We assume a = b show b = a, from @Elim b Hab --And therefore get b = a --Lemma 2.1.2: Transitivity --Type family: definition P_trans (A : Type) (a : A) := Ξ» (x : A)(n : a = x), Ξ  c : A, x = c β†’ a = c check P_trans --Proof: theorem trans' (A : Type) (a b c : A) : a = b β†’ (b = c β†’ a = c) := have IndHyp : P_trans A a a (eq.refl a), from Ξ» c : A, id, have Elim : Ξ  {a_1 : A} (n : a = a_1), P_trans A a a_1 n, from @eq.rec A a (P_trans A a) IndHyp, assume Hab : a = b, (@Elim b Hab) c --Lemma 2.1.4 end chap2
47fd76ce18bd31e862b4785d9696f02cad374ce6
6094e25ea0b7699e642463b48e51b2ead6ddc23f
/tests/lean/run/vector.lean
6c270907cd481ca7654414ea12692c01c2ab1303
[ "Apache-2.0" ]
permissive
gbaz/lean
a7835c4e3006fbbb079e8f8ffe18aacc45adebfb
a501c308be3acaa50a2c0610ce2e0d71becf8032
refs/heads/master
1,611,198,791,433
1,451,339,111,000
1,451,339,111,000
48,713,797
0
0
null
1,451,338,939,000
1,451,338,939,000
null
UTF-8
Lean
false
false
4,892
lean
import logic data.nat.basic data.prod data.unit open nat prod inductive vector (A : Type) : nat β†’ Type := | vnil {} : vector A zero | vcons : Ξ  {n : nat}, A β†’ vector A n β†’ vector A (succ n) namespace vector -- print definition no_confusion infixr `::` := vcons local abbreviation no_confusion := @vector.no_confusion local abbreviation below := @vector.below theorem vcons.inj₁ {A : Type} {n : nat} (a₁ aβ‚‚ : A) (v₁ vβ‚‚ : vector A n) : vcons a₁ v₁ = vcons aβ‚‚ vβ‚‚ β†’ a₁ = aβ‚‚ := begin intro h, apply (no_confusion h), intros, assumption end theorem vcons.injβ‚‚ {A : Type} {n : nat} (a₁ aβ‚‚ : A) (v₁ vβ‚‚ : vector A n) : vcons a₁ v₁ = vcons aβ‚‚ vβ‚‚ β†’ v₁ = vβ‚‚ := begin intro h, apply heq.to_eq, apply (no_confusion h), intros, eassumption, end set_option pp.universes true check @below namespace manual section universe variables l₁ lβ‚‚ variable {A : Type.{l₁}} variable {C : Ξ  (n : nat), vector A n β†’ Type.{lβ‚‚}} definition brec_on {n : nat} (v : vector A n) (H : Ξ  (n : nat) (v : vector A n), @vector.below A C n v β†’ C n v) : C n v := have general : C n v Γ— @vector.below A C n v, from vector.rec_on v (pair (H zero vnil poly_unit.star) poly_unit.star) (Ξ» (n₁ : nat) (a₁ : A) (v₁ : vector A n₁) (r₁ : C n₁ v₁ Γ— @vector.below A C n₁ v₁), have b : @vector.below A C _ (vcons a₁ v₁), from r₁, have c : C (succ n₁) (vcons a₁ v₁), from H (succ n₁) (vcons a₁ v₁) b, pair c b), pr₁ general end end manual -- check vector.brec_on definition bw := @below definition sum {n : nat} (v : vector nat n) : nat := vector.brec_on v (Ξ» (n : nat) (v : vector nat n), vector.cases_on v (Ξ» (B : bw vnil), zero) (Ξ» (n₁ : nat) (a : nat) (v₁ : vector nat n₁) (B : bw (vcons a v₁)), a + pr₁ B)) example : sum (10 :: 20 :: vnil) = 30 := rfl definition addk {n : nat} (v : vector nat n) (k : nat) : vector nat n := vector.brec_on v (Ξ» (n : nat) (v : vector nat n), vector.cases_on v (Ξ» (B : bw vnil), vnil) (Ξ» (n₁ : nat) (a₁ : nat) (v₁ : vector nat n₁) (B : bw (vcons a₁ v₁)), vcons (a₁+k) (pr₁ B))) example : addk (1 :: 2 :: vnil) 3 = 4 :: 5 :: vnil := rfl definition append.{l} {A : Type.{l+1}} {n m : nat} (w : vector A m) (v : vector A n) : vector A (n + m) := vector.brec_on w (Ξ» (n : nat) (w : vector A n), vector.cases_on w (Ξ» (B : bw vnil), v) (Ξ» (n₁ : nat) (a₁ : A) (v₁ : vector A n₁) (B : bw (vcons a₁ v₁)), vcons a₁ (pr₁ B))) example : append ((1:nat) :: 2 :: vnil) (3 :: vnil) = 1 :: 2 :: 3 :: vnil := rfl definition head {A : Type} {n : nat} (v : vector A (succ n)) : A := vector.cases_on v (Ξ» H : succ n = 0, nat.no_confusion H) (Ξ»n' h t (H : succ n = succ n'), h) rfl definition tail {A : Type} {n : nat} (v : vector A (succ n)) : vector A n := @vector.cases_on A (Ξ»n' v, succ n = n' β†’ vector A (pred n')) (succ n) v (Ξ» H : succ n = 0, nat.no_confusion H) (Ξ» (n' : nat) (h : A) (t : vector A n') (H : succ n = succ n'), t) rfl definition add {n : nat} (w v : vector nat n) : vector nat n := @vector.brec_on nat (Ξ» (n : nat) (v : vector nat n), vector nat n β†’ vector nat n) n w (Ξ» (n : nat) (w : vector nat n), vector.cases_on w (Ξ» (B : bw vnil) (w : vector nat zero), vnil) (Ξ» (n₁ : nat) (a₁ : nat) (v₁ : vector nat n₁) (B : bw (vcons a₁ v₁)) (v : vector nat (succ n₁)), vcons (a₁ + head v) (pr₁ B (tail v)))) v example : add (1 :: 2 :: vnil) (3 :: 5 :: vnil) = 4 :: 7 :: vnil := rfl definition map {A B C : Type} {n : nat} (f : A β†’ B β†’ C) (w : vector A n) (v : vector B n) : vector C n := let P := Ξ» (n : nat) (v : vector A n), vector B n β†’ vector C n in @vector.brec_on A P n w (Ξ» (n : nat) (w : vector A n), begin cases w with [n', h₁, t₁], show @below A P zero vnil β†’ vector B zero β†’ vector C zero, from Ξ» b v, vnil, show @below A P (succ n') (h₁ :: t₁) β†’ vector B (succ n') β†’ vector C (succ n'), from Ξ» b v, begin cases v with [n', hβ‚‚, tβ‚‚], have r : vector B n' β†’ vector C n', from pr₁ b, exact ((f h₁ hβ‚‚) :: r tβ‚‚), end end) v theorem map_nil_nil {A B C : Type} (f : A β†’ B β†’ C) : map f vnil vnil = vnil := rfl theorem map_cons_cons {A B C : Type} (f : A β†’ B β†’ C) (a : A) (b : B) {n : nat} (va : vector A n) (vb : vector B n) : map f (a :: va) (b :: vb) = f a b :: map f va vb := rfl example : map nat.add (1 :: 2 :: vnil) (3 :: 5 :: vnil) = 4 :: 7 :: vnil := rfl print definition map end vector
c42f73d3acb8f92941d8f06277d2c3a70d7a063e
bb31430994044506fa42fd667e2d556327e18dfe
/src/measure_theory/measure/doubling.lean
c293a057d4dd6eed9dd125cf5ecb6fe158cdff29
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
6,961
lean
/- Copyright (c) 2022 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import analysis.special_functions.log.base import measure_theory.measure.measure_space_def /-! # Doubling measures A doubling measure `ΞΌ` on a metric space is a measure for which there exists a constant `C` such that for all sufficiently small radii `Ξ΅`, and for any centre, the measure of a ball of radius `2 * Ξ΅` is bounded by `C` times the measure of the concentric ball of radius `Ξ΅`. This file records basic files on doubling measures. ## Main definitions * `is_doubling_measure`: the definition of a doubling measure (as a typeclass). * `is_doubling_measure.doubling_constant`: a function yielding the doubling constant `C` appearing in the definition of a doubling measure. -/ noncomputable theory open set filter metric measure_theory topological_space open_locale ennreal nnreal topological_space /-- A measure `ΞΌ` is said to be a doubling measure if there exists a constant `C` such that for all sufficiently small radii `Ξ΅`, and for any centre, the measure of a ball of radius `2 * Ξ΅` is bounded by `C` times the measure of the concentric ball of radius `Ξ΅`. Note: it is important that this definition makes a demand only for sufficiently small `Ξ΅`. For example we want hyperbolic space to carry the instance `is_doubling_measure volume` but volumes grow exponentially in hyperbolic space. To be really explicit, consider the hyperbolic plane of curvature -1, the area of a disc of radius `Ξ΅` is `A(Ξ΅) = 2Ο€(cosh(Ξ΅) - 1)` so `A(2Ξ΅)/A(Ξ΅) ~ exp(Ξ΅)`. -/ class is_doubling_measure {Ξ± : Type*} [metric_space Ξ±] [measurable_space Ξ±] (ΞΌ : measure Ξ±) := (exists_measure_closed_ball_le_mul [] : βˆƒ (C : ℝβ‰₯0), βˆ€αΆ  Ξ΅ in 𝓝[>] 0, βˆ€ x, ΞΌ (closed_ball x (2 * Ξ΅)) ≀ C * ΞΌ (closed_ball x Ξ΅)) namespace is_doubling_measure variables {Ξ± : Type*} [metric_space Ξ±] [measurable_space Ξ±] (ΞΌ : measure Ξ±) [is_doubling_measure ΞΌ] /-- A doubling constant for a doubling measure. See also `is_doubling_measure.scaling_constant_of`. -/ def doubling_constant : ℝβ‰₯0 := classical.some $ exists_measure_closed_ball_le_mul ΞΌ lemma exists_measure_closed_ball_le_mul' : βˆ€αΆ  Ξ΅ in 𝓝[>] 0, βˆ€ x, ΞΌ (closed_ball x (2 * Ξ΅)) ≀ doubling_constant ΞΌ * ΞΌ (closed_ball x Ξ΅) := classical.some_spec $ exists_measure_closed_ball_le_mul ΞΌ lemma exists_eventually_forall_measure_closed_ball_le_mul (K : ℝ) : βˆƒ (C : ℝβ‰₯0), βˆ€αΆ  Ξ΅ in 𝓝[>] 0, βˆ€ x t (ht : t ≀ K), ΞΌ (closed_ball x (t * Ξ΅)) ≀ C * ΞΌ (closed_ball x Ξ΅) := begin let C := doubling_constant ΞΌ, have hΞΌ : βˆ€ (n : β„•), βˆ€αΆ  Ξ΅ in 𝓝[>] 0, βˆ€ x, ΞΌ (closed_ball x (2^n * Ξ΅)) ≀ ↑(C^n) * ΞΌ (closed_ball x Ξ΅), { intros n, induction n with n ih, { simp, }, replace ih := eventually_nhds_within_pos_mul_left (two_pos : 0 < (2 : ℝ)) ih, refine (ih.and (exists_measure_closed_ball_le_mul' ΞΌ)).mono (Ξ» Ξ΅ hΞ΅ x, _), calc ΞΌ (closed_ball x (2^(n + 1) * Ξ΅)) = ΞΌ (closed_ball x (2^n * (2 * Ξ΅))) : by rw [pow_succ', mul_assoc] ... ≀ ↑(C^n) * ΞΌ (closed_ball x (2 * Ξ΅)) : hΞ΅.1 x ... ≀ ↑(C^n) * (C * ΞΌ (closed_ball x Ξ΅)) : ennreal.mul_left_mono (hΞ΅.2 x) ... = ↑(C^(n + 1)) * ΞΌ (closed_ball x Ξ΅) : by rw [← mul_assoc, pow_succ', ennreal.coe_mul], }, rcases lt_or_le K 1 with hK | hK, { refine ⟨1, _⟩, simp only [ennreal.coe_one, one_mul], exact eventually_mem_nhds_within.mono (Ξ» Ξ΅ hΞ΅ x t ht, measure_mono $ closed_ball_subset_closed_ball (by nlinarith [mem_Ioi.mp hΞ΅])), }, { refine ⟨C^⌈real.logb 2 KβŒ‰β‚Š, ((hΞΌ ⌈real.logb 2 KβŒ‰β‚Š).and eventually_mem_nhds_within).mono (Ξ» Ξ΅ hΞ΅ x t ht, le_trans (measure_mono $ closed_ball_subset_closed_ball _) (hΞ΅.1 x))⟩, refine mul_le_mul_of_nonneg_right (ht.trans _) (mem_Ioi.mp hΞ΅.2).le, conv_lhs { rw ← real.rpow_logb two_pos (by norm_num) (by linarith : 0 < K), }, rw ← real.rpow_nat_cast, exact real.rpow_le_rpow_of_exponent_le one_le_two (nat.le_ceil (real.logb 2 K)), }, end /-- A variant of `is_doubling_measure.doubling_constant` which allows for scaling the radius by values other than `2`. -/ def scaling_constant_of (K : ℝ) : ℝβ‰₯0 := max (classical.some $ exists_eventually_forall_measure_closed_ball_le_mul ΞΌ K) 1 @[simp] lemma one_le_scaling_constant_of (K : ℝ) : 1 ≀ scaling_constant_of ΞΌ K := le_max_of_le_right $ le_refl 1 lemma eventually_measure_mul_le_scaling_constant_of_mul (K : ℝ) : βˆƒ (R : ℝ), 0 < R ∧ βˆ€ x t r (ht : t ∈ Ioc 0 K) (hr : r ≀ R), ΞΌ (closed_ball x (t * r)) ≀ scaling_constant_of ΞΌ K * ΞΌ (closed_ball x r) := begin have h := classical.some_spec (exists_eventually_forall_measure_closed_ball_le_mul ΞΌ K), rcases mem_nhds_within_Ioi_iff_exists_Ioc_subset.1 h with ⟨R, Rpos, hR⟩, refine ⟨R, Rpos, Ξ» x t r ht hr, _⟩, rcases lt_trichotomy r 0 with rneg|rfl|rpos, { have : t * r < 0, from mul_neg_of_pos_of_neg ht.1 rneg, simp only [closed_ball_eq_empty.2 this, measure_empty, zero_le'] }, { simp only [mul_zero, closed_ball_zero], refine le_mul_of_one_le_of_le _ le_rfl, apply ennreal.one_le_coe_iff.2 (le_max_right _ _) }, { apply (hR ⟨rpos, hr⟩ x t ht.2).trans _, exact ennreal.mul_le_mul (ennreal.coe_le_coe.2 (le_max_left _ _)) le_rfl } end lemma eventually_measure_le_scaling_constant_mul (K : ℝ) : βˆ€αΆ  r in 𝓝[>] 0, βˆ€ x, ΞΌ (closed_ball x (K * r)) ≀ scaling_constant_of ΞΌ K * ΞΌ (closed_ball x r) := begin filter_upwards [classical.some_spec (exists_eventually_forall_measure_closed_ball_le_mul ΞΌ K)] with r hr x, exact (hr x K le_rfl).trans (ennreal.mul_le_mul (ennreal.coe_le_coe.2 (le_max_left _ _)) le_rfl) end lemma eventually_measure_le_scaling_constant_mul' (K : ℝ) (hK : 0 < K) : βˆ€αΆ  r in 𝓝[>] 0, βˆ€ x, ΞΌ (closed_ball x r) ≀ scaling_constant_of ΞΌ K⁻¹ * ΞΌ (closed_ball x (K * r)) := begin convert eventually_nhds_within_pos_mul_left hK (eventually_measure_le_scaling_constant_mul ΞΌ K⁻¹), ext, simp [inv_mul_cancel_leftβ‚€ hK.ne'], end /-- A scale below which the doubling measure `ΞΌ` satisfies good rescaling properties when one multiplies the radius of balls by at most `K`, as stated in `measure_mul_le_scaling_constant_of_mul`. -/ def scaling_scale_of (K : ℝ) : ℝ := (eventually_measure_mul_le_scaling_constant_of_mul ΞΌ K).some lemma scaling_scale_of_pos (K : ℝ) : 0 < scaling_scale_of ΞΌ K := (eventually_measure_mul_le_scaling_constant_of_mul ΞΌ K).some_spec.1 lemma measure_mul_le_scaling_constant_of_mul {K : ℝ} {x : Ξ±} {t r : ℝ} (ht : t ∈ Ioc 0 K) (hr : r ≀ scaling_scale_of ΞΌ K) : ΞΌ (closed_ball x (t * r)) ≀ scaling_constant_of ΞΌ K * ΞΌ (closed_ball x r) := (eventually_measure_mul_le_scaling_constant_of_mul ΞΌ K).some_spec.2 x t r ht hr end is_doubling_measure
fd56a14cd14adb57c7964d6039a129ff192214c6
fe25de614feb5587799621c41487aaee0d083b08
/src/Std/Data/HashSet.lean
6369abd1c19a2007754ee2a8b70ff1975e07f7f9
[ "Apache-2.0" ]
permissive
pollend/lean4
e8469c2f5fb8779b773618c3267883cf21fb9fac
c913886938c4b3b83238a3f99673c6c5a9cec270
refs/heads/master
1,687,973,251,481
1,628,039,739,000
1,628,039,739,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,195
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ namespace Std universe u v w def HashSetBucket (Ξ± : Type u) := { b : Array (List Ξ±) // b.size > 0 } def HashSetBucket.update {Ξ± : Type u} (data : HashSetBucket Ξ±) (i : USize) (d : List Ξ±) (h : i.toNat < data.val.size) : HashSetBucket Ξ± := ⟨ data.val.uset i d h, by erw [Array.size_set]; exact data.property ⟩ structure HashSetImp (Ξ± : Type u) where size : Nat buckets : HashSetBucket Ξ± def mkHashSetImp {Ξ± : Type u} (nbuckets := 8) : HashSetImp Ξ± := let n := if nbuckets = 0 then 8 else nbuckets { size := 0, buckets := ⟨ mkArray n [], by rw [Array.size_mkArray]; cases nbuckets; decide; apply Nat.zeroLtSucc ⟩ } namespace HashSetImp variable {Ξ± : Type u} def mkIdx {n : Nat} (h : n > 0) (u : USize) : { u : USize // u.toNat < n } := ⟨u % n, USize.modn_lt _ h⟩ @[inline] def reinsertAux (hashFn : Ξ± β†’ UInt64) (data : HashSetBucket Ξ±) (a : Ξ±) : HashSetBucket Ξ± := let ⟨i, h⟩ := mkIdx data.property (hashFn a |>.toUSize) data.update i (a :: data.val.uget i h) h @[inline] def foldBucketsM {Ξ΄ : Type w} {m : Type w β†’ Type w} [Monad m] (data : HashSetBucket Ξ±) (d : Ξ΄) (f : Ξ΄ β†’ Ξ± β†’ m Ξ΄) : m Ξ΄ := data.val.foldlM (init := d) fun d as => as.foldlM f d @[inline] def foldBuckets {Ξ΄ : Type w} (data : HashSetBucket Ξ±) (d : Ξ΄) (f : Ξ΄ β†’ Ξ± β†’ Ξ΄) : Ξ΄ := Id.run $ foldBucketsM data d f @[inline] def foldM {Ξ΄ : Type w} {m : Type w β†’ Type w} [Monad m] (f : Ξ΄ β†’ Ξ± β†’ m Ξ΄) (d : Ξ΄) (h : HashSetImp Ξ±) : m Ξ΄ := foldBucketsM h.buckets d f @[inline] def fold {Ξ΄ : Type w} (f : Ξ΄ β†’ Ξ± β†’ Ξ΄) (d : Ξ΄) (m : HashSetImp Ξ±) : Ξ΄ := foldBuckets m.buckets d f def find? [BEq Ξ±] [Hashable Ξ±] (m : HashSetImp Ξ±) (a : Ξ±) : Option Ξ± := match m with | ⟨_, buckets⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a |>.toUSize) (buckets.val.uget i h).find? (fun a' => a == a') def contains [BEq Ξ±] [Hashable Ξ±] (m : HashSetImp Ξ±) (a : Ξ±) : Bool := match m with | ⟨_, buckets⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a |>.toUSize) (buckets.val.uget i h).contains a -- TODO: remove `partial` by using well-founded recursion partial def moveEntries [Hashable Ξ±] (i : Nat) (source : Array (List Ξ±)) (target : HashSetBucket Ξ±) : HashSetBucket Ξ± := if h : i < source.size then let idx : Fin source.size := ⟨i, h⟩ let es : List Ξ± := source.get idx -- We remove `es` from `source` to make sure we can reuse its memory cells when performing es.foldl let source := source.set idx [] let target := es.foldl (reinsertAux hash) target moveEntries (i+1) source target else target def expand [Hashable Ξ±] (size : Nat) (buckets : HashSetBucket Ξ±) : HashSetImp Ξ± := let nbuckets := buckets.val.size * 2 have : nbuckets > 0 := Nat.mulPos buckets.property (by decide) let new_buckets : HashSetBucket Ξ± := ⟨mkArray nbuckets [], by rw [Array.size_mkArray]; assumption⟩ { size := size, buckets := moveEntries 0 buckets.val new_buckets } def insert [BEq Ξ±] [Hashable Ξ±] (m : HashSetImp Ξ±) (a : Ξ±) : HashSetImp Ξ± := match m with | ⟨size, buckets⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a |>.toUSize) let bkt := buckets.val.uget i h if bkt.contains a then ⟨size, buckets.update i (bkt.replace a a) h⟩ else let size' := size + 1 let buckets' := buckets.update i (a :: bkt) h if size' ≀ buckets.val.size then { size := size', buckets := buckets' } else expand size' buckets' def erase [BEq Ξ±] [Hashable Ξ±] (m : HashSetImp Ξ±) (a : Ξ±) : HashSetImp Ξ± := match m with | ⟨ size, buckets ⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a |>.toUSize) let bkt := buckets.val.uget i h if bkt.contains a then ⟨size - 1, buckets.update i (bkt.erase a) h⟩ else m inductive WellFormed [BEq Ξ±] [Hashable Ξ±] : HashSetImp Ξ± β†’ Prop where | mkWff : βˆ€ n, WellFormed (mkHashSetImp n) | insertWff : βˆ€ m a, WellFormed m β†’ WellFormed (insert m a) | eraseWff : βˆ€ m a, WellFormed m β†’ WellFormed (erase m a) end HashSetImp def HashSet (Ξ± : Type u) [BEq Ξ±] [Hashable Ξ±] := { m : HashSetImp Ξ± // m.WellFormed } open HashSetImp def mkHashSet {Ξ± : Type u} [BEq Ξ±] [Hashable Ξ±] (nbuckets := 8) : HashSet Ξ± := ⟨ mkHashSetImp nbuckets, WellFormed.mkWff nbuckets ⟩ namespace HashSet @[inline] def empty [BEq Ξ±] [Hashable Ξ±] : HashSet Ξ± := mkHashSet instance [BEq Ξ±] [Hashable Ξ±] : Inhabited (HashSet Ξ±) where default := mkHashSet instance [BEq Ξ±] [Hashable Ξ±] : EmptyCollection (HashSet Ξ±) := ⟨mkHashSet⟩ variable {Ξ± : Type u} {_ : BEq Ξ±} {_ : Hashable Ξ±} @[inline] def insert (m : HashSet Ξ±) (a : Ξ±) : HashSet Ξ± := match m with | ⟨ m, hw ⟩ => ⟨ m.insert a, WellFormed.insertWff m a hw ⟩ @[inline] def erase (m : HashSet Ξ±) (a : Ξ±) : HashSet Ξ± := match m with | ⟨ m, hw ⟩ => ⟨ m.erase a, WellFormed.eraseWff m a hw ⟩ @[inline] def find? (m : HashSet Ξ±) (a : Ξ±) : Option Ξ± := match m with | ⟨ m, _ ⟩ => m.find? a @[inline] def contains (m : HashSet Ξ±) (a : Ξ±) : Bool := match m with | ⟨ m, _ ⟩ => m.contains a @[inline] def foldM {Ξ΄ : Type w} {m : Type w β†’ Type w} [Monad m] (f : Ξ΄ β†’ Ξ± β†’ m Ξ΄) (init : Ξ΄) (h : HashSet Ξ±) : m Ξ΄ := match h with | ⟨ h, _ ⟩ => h.foldM f init @[inline] def fold {Ξ΄ : Type w} (f : Ξ΄ β†’ Ξ± β†’ Ξ΄) (init : Ξ΄) (m : HashSet Ξ±) : Ξ΄ := match m with | ⟨ m, _ ⟩ => m.fold f init @[inline] def size (m : HashSet Ξ±) : Nat := match m with | ⟨ {size := sz, ..}, _ ⟩ => sz @[inline] def isEmpty (m : HashSet Ξ±) : Bool := m.size = 0 def toList (m : HashSet Ξ±) : List Ξ± := m.fold (init := []) fun r a => a::r def toArray (m : HashSet Ξ±) : Array Ξ± := m.fold (init := #[]) fun r a => r.push a def numBuckets (m : HashSet Ξ±) : Nat := m.val.buckets.val.size end HashSet end Std
0e13b6edfa09138455a769f3e25a33699613e61a
af702bb100e4fe8a128e0e9a7e9475e590a0e051
/8-kyu/multiply/solution-lean.lean
fe93fdd2ff5de6c10b3e81f8d2a51bfd89eab25c
[]
no_license
thaliaarchi/codewars
e6762c141a0f3375323cb377fc30357a46069ca9
66a3687f90de974acc5d770d7330e11f239a0e91
refs/heads/master
1,674,385,357,441
1,604,907,437,000
1,604,907,437,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
39
lean
def multiply (a b : β„•) : β„• := a * b
fde8e275f58164f40e9ff7aa8fc7be0460522020
9028d228ac200bbefe3a711342514dd4e4458bff
/src/category_theory/monoidal/category.lean
f1b33dab678c2f27d2926dbb8373cc0f9dea9b71
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
21,010
lean
/- Copyright (c) 2018 Michael Jendrusch. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Michael Jendrusch, Scott Morrison -/ import category_theory.products.basic /-! # Monoidal categories A monoidal category is a category equipped with a tensor product, unitors, and an associator. In the definition, we provide the tensor product as a pair of functions * `tensor_obj : C β†’ C β†’ C` * `tensor_hom : (X₁ ⟢ Y₁) β†’ (Xβ‚‚ ⟢ Yβ‚‚) β†’ ((X₁ βŠ— Xβ‚‚) ⟢ (Y₁ βŠ— Yβ‚‚))` and allow use of the overloaded notation `βŠ—` for both. The unitors and associator are provided componentwise. The tensor product can be expressed as a functor via `tensor : C Γ— C β₯€ C`. The unitors and associator are gathered together as natural isomorphisms in `left_unitor_nat_iso`, `right_unitor_nat_iso` and `associator_nat_iso`. Some consequences of the definition are proved in other files, e.g. `(Ξ»_ (πŸ™_ C)).hom = (ρ_ (πŸ™_ C)).hom` in `category_theory.monoidal.unitors_equal`. ## Implementation Dealing with unitors and associators is painful, and at this stage we do not have a useful implementation of coherence for monoidal categories. In an effort to lessen the pain, we put some effort into choosing the right `simp` lemmas. Generally, the rule is that the component index of a natural transformation "weighs more" in considering the complexity of an expression than does a structural isomorphism (associator, etc). As an example when we prove Proposition 2.2.4 of <http://www-math.mit.edu/~etingof/egnobookfinal.pdf> we state it as a `@[simp]` lemma as ``` (Ξ»_ (X βŠ— Y)).hom = (Ξ±_ (πŸ™_ C) X Y).inv ≫ (Ξ»_ X).hom βŠ— (πŸ™ Y) ``` This is far from completely effective, but seems to prove a useful principle. ## References * Tensor categories, Etingof, Gelaki, Nikshych, Ostrik, http://www-math.mit.edu/~etingof/egnobookfinal.pdf * https://stacks.math.columbia.edu/tag/0FFK. -/ open category_theory universes v u open category_theory open category_theory.category open category_theory.iso namespace category_theory /-- In a monoidal category, we can take the tensor product of objects, `X βŠ— Y` and of morphisms `f βŠ— g`. Tensor product does not need to be strictly associative on objects, but there is a specified associator, `Ξ±_ X Y Z : (X βŠ— Y) βŠ— Z β‰… X βŠ— (Y βŠ— Z)`. There is a tensor unit `πŸ™_ C`, with specified left and right unitor isomorphisms `Ξ»_ X : πŸ™_ C βŠ— X β‰… X` and `ρ_ X : X βŠ— πŸ™_ C β‰… X`. These associators and unitors satisfy the pentagon and triangle equations. See https://stacks.math.columbia.edu/tag/0FFK. -/ class monoidal_category (C : Type u) [π’ž : category.{v} C] := -- curried tensor product of objects: (tensor_obj : C β†’ C β†’ C) (infixr ` βŠ— `:70 := tensor_obj) -- This notation is only temporary -- curried tensor product of morphisms: (tensor_hom : Ξ  {X₁ Y₁ Xβ‚‚ Yβ‚‚ : C}, (X₁ ⟢ Y₁) β†’ (Xβ‚‚ ⟢ Yβ‚‚) β†’ ((X₁ βŠ— Xβ‚‚) ⟢ (Y₁ βŠ— Yβ‚‚))) (infixr ` βŠ—' `:69 := tensor_hom) -- This notation is only temporary -- tensor product laws: (tensor_id' : βˆ€ (X₁ Xβ‚‚ : C), (πŸ™ X₁) βŠ—' (πŸ™ Xβ‚‚) = πŸ™ (X₁ βŠ— Xβ‚‚) . obviously) (tensor_comp' : βˆ€ {X₁ Y₁ Z₁ Xβ‚‚ Yβ‚‚ Zβ‚‚ : C} (f₁ : X₁ ⟢ Y₁) (fβ‚‚ : Xβ‚‚ ⟢ Yβ‚‚) (g₁ : Y₁ ⟢ Z₁) (gβ‚‚ : Yβ‚‚ ⟢ Zβ‚‚), (f₁ ≫ g₁) βŠ—' (fβ‚‚ ≫ gβ‚‚) = (f₁ βŠ—' fβ‚‚) ≫ (g₁ βŠ—' gβ‚‚) . obviously) -- tensor unit: (tensor_unit [] : C) (notation `πŸ™_` := tensor_unit) -- associator: (associator : Ξ  X Y Z : C, (X βŠ— Y) βŠ— Z β‰… X βŠ— (Y βŠ— Z)) (notation `Ξ±_` := associator) (associator_naturality' : βˆ€ {X₁ Xβ‚‚ X₃ Y₁ Yβ‚‚ Y₃ : C} (f₁ : X₁ ⟢ Y₁) (fβ‚‚ : Xβ‚‚ ⟢ Yβ‚‚) (f₃ : X₃ ⟢ Y₃), ((f₁ βŠ—' fβ‚‚) βŠ—' f₃) ≫ (Ξ±_ Y₁ Yβ‚‚ Y₃).hom = (Ξ±_ X₁ Xβ‚‚ X₃).hom ≫ (f₁ βŠ—' (fβ‚‚ βŠ—' f₃)) . obviously) -- left unitor: (left_unitor : Ξ  X : C, πŸ™_ βŠ— X β‰… X) (notation `Ξ»_` := left_unitor) (left_unitor_naturality' : βˆ€ {X Y : C} (f : X ⟢ Y), ((πŸ™ πŸ™_) βŠ—' f) ≫ (Ξ»_ Y).hom = (Ξ»_ X).hom ≫ f . obviously) -- right unitor: (right_unitor : Ξ  X : C, X βŠ— πŸ™_ β‰… X) (notation `ρ_` := right_unitor) (right_unitor_naturality' : βˆ€ {X Y : C} (f : X ⟢ Y), (f βŠ—' (πŸ™ πŸ™_)) ≫ (ρ_ Y).hom = (ρ_ X).hom ≫ f . obviously) -- pentagon identity: (pentagon' : βˆ€ W X Y Z : C, ((Ξ±_ W X Y).hom βŠ—' (πŸ™ Z)) ≫ (Ξ±_ W (X βŠ— Y) Z).hom ≫ ((πŸ™ W) βŠ—' (Ξ±_ X Y Z).hom) = (Ξ±_ (W βŠ— X) Y Z).hom ≫ (Ξ±_ W X (Y βŠ— Z)).hom . obviously) -- triangle identity: (triangle' : βˆ€ X Y : C, (Ξ±_ X πŸ™_ Y).hom ≫ ((πŸ™ X) βŠ—' (Ξ»_ Y).hom) = (ρ_ X).hom βŠ—' (πŸ™ Y) . obviously) restate_axiom monoidal_category.tensor_id' attribute [simp] monoidal_category.tensor_id restate_axiom monoidal_category.tensor_comp' attribute [reassoc] monoidal_category.tensor_comp -- This would be redundant in the simp set. attribute [simp] monoidal_category.tensor_comp restate_axiom monoidal_category.associator_naturality' attribute [reassoc] monoidal_category.associator_naturality restate_axiom monoidal_category.left_unitor_naturality' attribute [reassoc] monoidal_category.left_unitor_naturality restate_axiom monoidal_category.right_unitor_naturality' attribute [reassoc] monoidal_category.right_unitor_naturality restate_axiom monoidal_category.pentagon' restate_axiom monoidal_category.triangle' attribute [simp, reassoc] monoidal_category.triangle open monoidal_category infixr ` βŠ— `:70 := tensor_obj infixr ` βŠ— `:70 := tensor_hom notation `πŸ™_` := tensor_unit notation `Ξ±_` := associator notation `Ξ»_` := left_unitor notation `ρ_` := right_unitor /-- The tensor product of two isomorphisms is an isomorphism. -/ @[simps] def tensor_iso {C : Type u} {X Y X' Y' : C} [category.{v} C] [monoidal_category.{v} C] (f : X β‰… Y) (g : X' β‰… Y') : X βŠ— X' β‰… Y βŠ— Y' := { hom := f.hom βŠ— g.hom, inv := f.inv βŠ— g.inv, hom_inv_id' := by rw [←tensor_comp, iso.hom_inv_id, iso.hom_inv_id, ←tensor_id], inv_hom_id' := by rw [←tensor_comp, iso.inv_hom_id, iso.inv_hom_id, ←tensor_id] } infixr ` βŠ— `:70 := tensor_iso namespace monoidal_category section variables {C : Type u} [category.{v} C] [monoidal_category.{v} C] instance tensor_is_iso {W X Y Z : C} (f : W ⟢ X) [is_iso f] (g : Y ⟢ Z) [is_iso g] : is_iso (f βŠ— g) := { ..(as_iso f βŠ— as_iso g) } @[simp] lemma inv_tensor {W X Y Z : C} (f : W ⟢ X) [is_iso f] (g : Y ⟢ Z) [is_iso g] : inv (f βŠ— g) = inv f βŠ— inv g := rfl variables {U V W X Y Z : C} -- When `rewrite_search` lands, add @[search] attributes to -- monoidal_category.tensor_id monoidal_category.tensor_comp monoidal_category.associator_naturality -- monoidal_category.left_unitor_naturality monoidal_category.right_unitor_naturality -- monoidal_category.pentagon monoidal_category.triangle -- tensor_comp_id tensor_id_comp comp_id_tensor_tensor_id -- triangle_assoc_comp_left triangle_assoc_comp_right triangle_assoc_comp_left_inv triangle_assoc_comp_right_inv -- left_unitor_tensor left_unitor_tensor_inv -- right_unitor_tensor right_unitor_tensor_inv -- pentagon_inv -- associator_inv_naturality -- left_unitor_inv_naturality -- right_unitor_inv_naturality @[simp] lemma comp_tensor_id (f : W ⟢ X) (g : X ⟢ Y) : (f ≫ g) βŠ— (πŸ™ Z) = (f βŠ— (πŸ™ Z)) ≫ (g βŠ— (πŸ™ Z)) := by { rw ←tensor_comp, simp } @[simp] lemma id_tensor_comp (f : W ⟢ X) (g : X ⟢ Y) : (πŸ™ Z) βŠ— (f ≫ g) = (πŸ™ Z βŠ— f) ≫ (πŸ™ Z βŠ— g) := by { rw ←tensor_comp, simp } @[simp, reassoc] lemma id_tensor_comp_tensor_id (f : W ⟢ X) (g : Y ⟢ Z) : ((πŸ™ Y) βŠ— f) ≫ (g βŠ— (πŸ™ X)) = g βŠ— f := by { rw [←tensor_comp], simp } @[simp, reassoc] lemma tensor_id_comp_id_tensor (f : W ⟢ X) (g : Y ⟢ Z) : (g βŠ— (πŸ™ W)) ≫ ((πŸ™ Z) βŠ— f) = g βŠ— f := by { rw [←tensor_comp], simp } lemma left_unitor_inv_naturality {X X' : C} (f : X ⟢ X') : f ≫ (Ξ»_ X').inv = (Ξ»_ X).inv ≫ (πŸ™ _ βŠ— f) := begin apply (cancel_mono (Ξ»_ X').hom).1, simp only [assoc, comp_id, iso.inv_hom_id], rw [left_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] end lemma right_unitor_inv_naturality {X X' : C} (f : X ⟢ X') : f ≫ (ρ_ X').inv = (ρ_ X).inv ≫ (f βŠ— πŸ™ _) := begin apply (cancel_mono (ρ_ X').hom).1, simp only [assoc, comp_id, iso.inv_hom_id], rw [right_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] end @[simp] lemma right_unitor_conjugation {X Y : C} (f : X ⟢ Y) : (ρ_ X).inv ≫ (f βŠ— (πŸ™ (πŸ™_ C))) ≫ (ρ_ Y).hom = f := by rw [right_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] @[simp] lemma left_unitor_conjugation {X Y : C} (f : X ⟢ Y) : (Ξ»_ X).inv ≫ ((πŸ™ (πŸ™_ C)) βŠ— f) ≫ (Ξ»_ Y).hom = f := by rw [left_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] @[simp] lemma tensor_left_iff {X Y : C} (f g : X ⟢ Y) : ((πŸ™ (πŸ™_ C)) βŠ— f = (πŸ™ (πŸ™_ C)) βŠ— g) ↔ (f = g) := begin split, { intro h, have h' := congr_arg (Ξ» k, (Ξ»_ _).inv ≫ k) h, dsimp at h', rw [←left_unitor_inv_naturality, ←left_unitor_inv_naturality] at h', exact (cancel_mono _).1 h', }, { intro h, subst h, } end @[simp] lemma tensor_right_iff {X Y : C} (f g : X ⟢ Y) : (f βŠ— (πŸ™ (πŸ™_ C)) = g βŠ— (πŸ™ (πŸ™_ C))) ↔ (f = g) := begin split, { intro h, have h' := congr_arg (Ξ» k, (ρ_ _).inv ≫ k) h, dsimp at h', rw [←right_unitor_inv_naturality, ←right_unitor_inv_naturality] at h', exact (cancel_mono _).1 h' }, { intro h, subst h, } end -- We now prove: -- ((Ξ±_ (πŸ™_ C) X Y).hom) ≫ -- ((Ξ»_ (X βŠ— Y)).hom) -- = ((Ξ»_ X).hom βŠ— (πŸ™ Y)) -- (and the corresponding fact for right unitors) -- following the proof on nLab: -- Lemma 2.2 at <https://ncatlab.org/nlab/revision/monoidal+category/115> lemma left_unitor_product_aux_perimeter (X Y : C) : ((Ξ±_ (πŸ™_ C) (πŸ™_ C) X).hom βŠ— (πŸ™ Y)) ≫ (Ξ±_ (πŸ™_ C) ((πŸ™_ C) βŠ— X) Y).hom ≫ ((πŸ™ (πŸ™_ C)) βŠ— (Ξ±_ (πŸ™_ C) X Y).hom) ≫ ((πŸ™ (πŸ™_ C)) βŠ— (Ξ»_ (X βŠ— Y)).hom) = (((ρ_ (πŸ™_ C)).hom βŠ— (πŸ™ X)) βŠ— (πŸ™ Y)) ≫ (Ξ±_ (πŸ™_ C) X Y).hom := begin conv_lhs { congr, skip, rw [←category.assoc] }, rw [←category.assoc, monoidal_category.pentagon, associator_naturality, tensor_id, ←monoidal_category.triangle, ←category.assoc] end lemma left_unitor_product_aux_triangle (X Y : C) : ((Ξ±_ (πŸ™_ C) (πŸ™_ C) X).hom βŠ— (πŸ™ Y)) ≫ (((πŸ™ (πŸ™_ C)) βŠ— (Ξ»_ X).hom) βŠ— (πŸ™ Y)) = ((ρ_ (πŸ™_ C)).hom βŠ— (πŸ™ X)) βŠ— (πŸ™ Y) := by rw [←comp_tensor_id, ←monoidal_category.triangle] lemma left_unitor_product_aux_square (X Y : C) : (Ξ±_ (πŸ™_ C) ((πŸ™_ C) βŠ— X) Y).hom ≫ ((πŸ™ (πŸ™_ C)) βŠ— (Ξ»_ X).hom βŠ— (πŸ™ Y)) = (((πŸ™ (πŸ™_ C)) βŠ— (Ξ»_ X).hom) βŠ— (πŸ™ Y)) ≫ (Ξ±_ (πŸ™_ C) X Y).hom := by rw associator_naturality lemma left_unitor_product_aux (X Y : C) : ((πŸ™ (πŸ™_ C)) βŠ— (Ξ±_ (πŸ™_ C) X Y).hom) ≫ ((πŸ™ (πŸ™_ C)) βŠ— (Ξ»_ (X βŠ— Y)).hom) = (πŸ™ (πŸ™_ C)) βŠ— ((Ξ»_ X).hom βŠ— (πŸ™ Y)) := begin rw ←(cancel_epi (Ξ±_ (πŸ™_ C) ((πŸ™_ C) βŠ— X) Y).hom), rw left_unitor_product_aux_square, rw ←(cancel_epi ((Ξ±_ (πŸ™_ C) (πŸ™_ C) X).hom βŠ— (πŸ™ Y))), slice_rhs 1 2 { rw left_unitor_product_aux_triangle }, conv_lhs { rw [left_unitor_product_aux_perimeter] } end lemma right_unitor_product_aux_perimeter (X Y : C) : ((Ξ±_ X Y (πŸ™_ C)).hom βŠ— (πŸ™ (πŸ™_ C))) ≫ (Ξ±_ X (Y βŠ— (πŸ™_ C)) (πŸ™_ C)).hom ≫ ((πŸ™ X) βŠ— (Ξ±_ Y (πŸ™_ C) (πŸ™_ C)).hom) ≫ ((πŸ™ X) βŠ— (πŸ™ Y) βŠ— (Ξ»_ (πŸ™_ C)).hom) = ((ρ_ (X βŠ— Y)).hom βŠ— (πŸ™ (πŸ™_ C))) ≫ (Ξ±_ X Y (πŸ™_ C)).hom := begin transitivity (((Ξ±_ X Y _).hom βŠ— πŸ™ _) ≫ (Ξ±_ X _ _).hom ≫ (πŸ™ X βŠ— (Ξ±_ Y _ _).hom)) ≫ (πŸ™ X βŠ— πŸ™ Y βŠ— (Ξ»_ _).hom), { conv_lhs { congr, skip, rw [←category.assoc] }, conv_rhs { rw [category.assoc] } }, { conv_lhs { congr, rw [monoidal_category.pentagon] }, conv_rhs { congr, rw [←monoidal_category.triangle] }, conv_rhs { rw [category.assoc] }, conv_rhs { congr, skip, congr, congr, rw [←tensor_id] }, conv_rhs { congr, skip, rw [associator_naturality] }, conv_rhs { rw [←category.assoc] } } end lemma right_unitor_product_aux_triangle (X Y : C) : ((πŸ™ X) βŠ— (Ξ±_ Y (πŸ™_ C) (πŸ™_ C)).hom) ≫ ((πŸ™ X) βŠ— (πŸ™ Y) βŠ— (Ξ»_ (πŸ™_ C)).hom) = (πŸ™ X) βŠ— (ρ_ Y).hom βŠ— (πŸ™ (πŸ™_ C)) := by rw [←id_tensor_comp, ←monoidal_category.triangle] lemma right_unitor_product_aux_square (X Y : C) : (Ξ±_ X (Y βŠ— (πŸ™_ C)) (πŸ™_ C)).hom ≫ ((πŸ™ X) βŠ— (ρ_ Y).hom βŠ— (πŸ™ (πŸ™_ C))) = (((πŸ™ X) βŠ— (ρ_ Y).hom) βŠ— (πŸ™ (πŸ™_ C))) ≫ (Ξ±_ X Y (πŸ™_ C)).hom := by rw [associator_naturality] lemma right_unitor_product_aux (X Y : C) : ((Ξ±_ X Y (πŸ™_ C)).hom βŠ— (πŸ™ (πŸ™_ C))) ≫ (((πŸ™ X) βŠ— (ρ_ Y).hom) βŠ— (πŸ™ (πŸ™_ C))) = ((ρ_ (X βŠ— Y)).hom βŠ— (πŸ™ (πŸ™_ C))) := begin rw ←(cancel_mono (Ξ±_ X Y (πŸ™_ C)).hom), slice_lhs 2 3 { rw ←right_unitor_product_aux_square }, rw [←right_unitor_product_aux_triangle, ←right_unitor_product_aux_perimeter], end -- See Proposition 2.2.4 of <http://www-math.mit.edu/~etingof/egnobookfinal.pdf> lemma left_unitor_tensor' (X Y : C) : ((Ξ±_ (πŸ™_ C) X Y).hom) ≫ ((Ξ»_ (X βŠ— Y)).hom) = ((Ξ»_ X).hom βŠ— (πŸ™ Y)) := by rw [←tensor_left_iff, id_tensor_comp, left_unitor_product_aux] @[simp] lemma left_unitor_tensor (X Y : C) : ((Ξ»_ (X βŠ— Y)).hom) = ((Ξ±_ (πŸ™_ C) X Y).inv) ≫ ((Ξ»_ X).hom βŠ— (πŸ™ Y)) := by { rw [←left_unitor_tensor'], simp } lemma left_unitor_tensor_inv' (X Y : C) : ((Ξ»_ (X βŠ— Y)).inv) ≫ ((Ξ±_ (πŸ™_ C) X Y).inv) = ((Ξ»_ X).inv βŠ— (πŸ™ Y)) := eq_of_inv_eq_inv (by simp) @[simp] lemma left_unitor_tensor_inv (X Y : C) : ((Ξ»_ (X βŠ— Y)).inv) = ((Ξ»_ X).inv βŠ— (πŸ™ Y)) ≫ ((Ξ±_ (πŸ™_ C) X Y).hom) := by { rw [←left_unitor_tensor_inv'], simp } @[simp] lemma right_unitor_tensor (X Y : C) : ((ρ_ (X βŠ— Y)).hom) = ((Ξ±_ X Y (πŸ™_ C)).hom) ≫ ((πŸ™ X) βŠ— (ρ_ Y).hom) := by rw [←tensor_right_iff, comp_tensor_id, right_unitor_product_aux] @[simp] lemma right_unitor_tensor_inv (X Y : C) : ((ρ_ (X βŠ— Y)).inv) = ((πŸ™ X) βŠ— (ρ_ Y).inv) ≫ ((Ξ±_ X Y (πŸ™_ C)).inv) := eq_of_inv_eq_inv (by simp) lemma associator_inv_naturality {X Y Z X' Y' Z' : C} (f : X ⟢ X') (g : Y ⟢ Y') (h : Z ⟢ Z') : (f βŠ— (g βŠ— h)) ≫ (Ξ±_ X' Y' Z').inv = (Ξ±_ X Y Z).inv ≫ ((f βŠ— g) βŠ— h) := begin apply (cancel_mono (Ξ±_ X' Y' Z').hom).1, simp only [assoc, comp_id, iso.inv_hom_id], rw [associator_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] end lemma pentagon_inv (W X Y Z : C) : ((πŸ™ W) βŠ— (Ξ±_ X Y Z).inv) ≫ (Ξ±_ W (X βŠ— Y) Z).inv ≫ ((Ξ±_ W X Y).inv βŠ— (πŸ™ Z)) = (Ξ±_ W X (Y βŠ— Z)).inv ≫ (Ξ±_ (W βŠ— X) Y Z).inv := begin apply category_theory.eq_of_inv_eq_inv, dsimp, rw [category.assoc, monoidal_category.pentagon] end lemma triangle_assoc_comp_left (X Y : C) : (Ξ±_ X (πŸ™_ C) Y).hom ≫ ((πŸ™ X) βŠ— (Ξ»_ Y).hom) = (ρ_ X).hom βŠ— πŸ™ Y := monoidal_category.triangle X Y @[simp] lemma triangle_assoc_comp_right (X Y : C) : (Ξ±_ X (πŸ™_ C) Y).inv ≫ ((ρ_ X).hom βŠ— πŸ™ Y) = ((πŸ™ X) βŠ— (Ξ»_ Y).hom) := by rw [←triangle_assoc_comp_left, ←category.assoc, iso.inv_hom_id, category.id_comp] @[simp] lemma triangle_assoc_comp_right_inv (X Y : C) : ((ρ_ X).inv βŠ— πŸ™ Y) ≫ (Ξ±_ X (πŸ™_ C) Y).hom = ((πŸ™ X) βŠ— (Ξ»_ Y).inv) := begin apply (cancel_mono (πŸ™ X βŠ— (Ξ»_ Y).hom)).1, simp only [assoc, triangle_assoc_comp_left], rw [←comp_tensor_id, iso.inv_hom_id, ←id_tensor_comp, iso.inv_hom_id] end @[simp] lemma triangle_assoc_comp_left_inv (X Y : C) : ((πŸ™ X) βŠ— (Ξ»_ Y).inv) ≫ (Ξ±_ X (πŸ™_ C) Y).inv = ((ρ_ X).inv βŠ— πŸ™ Y) := begin apply (cancel_mono ((ρ_ X).hom βŠ— πŸ™ Y)).1, simp only [triangle_assoc_comp_right, assoc], rw [←id_tensor_comp, iso.inv_hom_id, ←comp_tensor_id, iso.inv_hom_id] end end section variables (C : Type u) [category.{v} C] [monoidal_category.{v} C] /-- The tensor product expressed as a functor. -/ def tensor : (C Γ— C) β₯€ C := { obj := Ξ» X, X.1 βŠ— X.2, map := Ξ» {X Y : C Γ— C} (f : X ⟢ Y), f.1 βŠ— f.2 } /-- The left-associated triple tensor product as a functor. -/ def left_assoc_tensor : (C Γ— C Γ— C) β₯€ C := { obj := Ξ» X, (X.1 βŠ— X.2.1) βŠ— X.2.2, map := Ξ» {X Y : C Γ— C Γ— C} (f : X ⟢ Y), (f.1 βŠ— f.2.1) βŠ— f.2.2 } @[simp] lemma left_assoc_tensor_obj (X) : (left_assoc_tensor C).obj X = (X.1 βŠ— X.2.1) βŠ— X.2.2 := rfl @[simp] lemma left_assoc_tensor_map {X Y} (f : X ⟢ Y) : (left_assoc_tensor C).map f = (f.1 βŠ— f.2.1) βŠ— f.2.2 := rfl /-- The right-associated triple tensor product as a functor. -/ def right_assoc_tensor : (C Γ— C Γ— C) β₯€ C := { obj := Ξ» X, X.1 βŠ— (X.2.1 βŠ— X.2.2), map := Ξ» {X Y : C Γ— C Γ— C} (f : X ⟢ Y), f.1 βŠ— (f.2.1 βŠ— f.2.2) } @[simp] lemma right_assoc_tensor_obj (X) : (right_assoc_tensor C).obj X = X.1 βŠ— (X.2.1 βŠ— X.2.2) := rfl @[simp] lemma right_assoc_tensor_map {X Y} (f : X ⟢ Y) : (right_assoc_tensor C).map f = f.1 βŠ— (f.2.1 βŠ— f.2.2) := rfl /-- The functor `Ξ» X, πŸ™_ C βŠ— X`. -/ def tensor_unit_left : C β₯€ C := { obj := Ξ» X, πŸ™_ C βŠ— X, map := Ξ» {X Y : C} (f : X ⟢ Y), (πŸ™ (πŸ™_ C)) βŠ— f } /-- The functor `Ξ» X, X βŠ— πŸ™_ C`. -/ def tensor_unit_right : C β₯€ C := { obj := Ξ» X, X βŠ— πŸ™_ C, map := Ξ» {X Y : C} (f : X ⟢ Y), f βŠ— (πŸ™ (πŸ™_ C)) } -- We can express the associator and the unitors, given componentwise above, -- as natural isomorphisms. /-- The associator as a natural isomorphism. -/ @[simps {rhs_md := semireducible}] def associator_nat_iso : left_assoc_tensor C β‰… right_assoc_tensor C := nat_iso.of_components (by { intros, apply monoidal_category.associator }) (by { intros, apply monoidal_category.associator_naturality }) /-- The left unitor as a natural isomorphism. -/ @[simps {rhs_md := semireducible}] def left_unitor_nat_iso : tensor_unit_left C β‰… 𝟭 C := nat_iso.of_components (by { intros, apply monoidal_category.left_unitor }) (by { intros, apply monoidal_category.left_unitor_naturality }) /-- The right unitor as a natural isomorphism. -/ @[simps {rhs_md := semireducible}] def right_unitor_nat_iso : tensor_unit_right C β‰… 𝟭 C := nat_iso.of_components (by { intros, apply monoidal_category.right_unitor }) (by { intros, apply monoidal_category.right_unitor_naturality }) section variables {C} /-- Tensoring on the left with as fixed object, as a functor. -/ @[simps] def tensor_left (X : C) : C β₯€ C := { obj := Ξ» Y, X βŠ— Y, map := Ξ» Y Y' f, (πŸ™ X) βŠ— f, } /-- Tensoring on the left with `X βŠ— Y` is naturally isomorphic to tensoring on the left with `Y`, and then again with `X`. -/ def tensor_left_tensor (X Y : C) : tensor_left (X βŠ— Y) β‰… tensor_left Y β‹™ tensor_left X := nat_iso.of_components (associator _ _) (Ξ» Z Z' f, by { dsimp, rw[←tensor_id], apply associator_naturality }) @[simp] lemma tensor_left_tensor_hom_app (X Y Z : C) : (tensor_left_tensor X Y).hom.app Z = (associator X Y Z).hom := rfl @[simp] lemma tensor_left_tensor_inv_app (X Y Z : C) : (tensor_left_tensor X Y).inv.app Z = (associator X Y Z).inv := rfl /-- Tensoring on the right with as fixed object, as a functor. -/ @[simps] def tensor_right (X : C) : C β₯€ C := { obj := Ξ» Y, Y βŠ— X, map := Ξ» Y Y' f, f βŠ— (πŸ™ X), } variables (C) /-- Tensoring on the right, as a functor from `C` into endofunctors of `C`. We later show this is a monoidal functor. -/ @[simps] def tensoring_right : C β₯€ (C β₯€ C) := { obj := tensor_right, map := Ξ» X Y f, { app := Ξ» Z, (πŸ™ Z) βŠ— f } } instance : faithful (tensoring_right C) := { map_injective' := Ξ» X Y f g h, begin injections with h, replace h := congr_fun h (πŸ™_ C), simpa using h, end } variables {C} /-- Tensoring on the right with `X βŠ— Y` is naturally isomorphic to tensoring on the right with `X`, and then again with `Y`. -/ def tensor_right_tensor (X Y : C) : tensor_right (X βŠ— Y) β‰… tensor_right X β‹™ tensor_right Y := nat_iso.of_components (Ξ» Z, (associator Z X Y).symm) (Ξ» Z Z' f, by { dsimp, rw[←tensor_id], apply associator_inv_naturality }) @[simp] lemma tensor_right_tensor_hom_app (X Y Z : C) : (tensor_right_tensor X Y).hom.app Z = (associator Z X Y).inv := rfl @[simp] lemma tensor_right_tensor_inv_app (X Y Z : C) : (tensor_right_tensor X Y).inv.app Z = (associator Z X Y).hom := rfl end end end monoidal_category end category_theory
766f445c1fd067f52b920af666b93710e300e67b
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/print_reducible.lean
06f1defc3c2d4512733b2bab27c57e86ccf7fd8e
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
445
lean
prelude attribute [reducible] definition id₁ {A : Type} (a : A) := a attribute [reducible] definition idβ‚‚ {A : Type} (a : A) := a attribute [irreducible] definition idβ‚… {A : Type} (a : A) := a attribute [irreducible] definition id₆ {A : Type} (a : A) := a attribute [reducible] definition pr {A B : Type} (a : A) (b : B) := a definition pr2 {A B : Type} (a : A) (b : B) := a print [reducible] print "-----------" print [irreducible]
e573569e5fa90549d8b55f026c74586a3db7689b
e953c38599905267210b87fb5d82dcc3e52a4214
/library/init/nat.lean
18e4f1dbc000f746d2b947828643bcaebdec1412
[ "Apache-2.0" ]
permissive
c-cube/lean
563c1020bff98441c4f8ba60111fef6f6b46e31b
0fb52a9a139f720be418dafac35104468e293b66
refs/heads/master
1,610,753,294,113
1,440,451,356,000
1,440,499,588,000
41,748,334
0
0
null
1,441,122,656,000
1,441,122,656,000
null
UTF-8
Lean
false
false
8,773
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura -/ prelude import init.wf init.tactic init.num open eq.ops decidable or namespace nat notation `β„•` := nat /- basic definitions on natural numbers -/ inductive le (a : β„•) : β„• β†’ Prop := | refl : le a a | step : Ξ  {b}, le a b β†’ le a (succ b) infix `≀` := le attribute le.refl [refl] definition lt [reducible] (n m : β„•) := succ n ≀ m definition ge [reducible] (n m : β„•) := m ≀ n definition gt [reducible] (n m : β„•) := succ m ≀ n infix `<` := lt infix `β‰₯` := ge infix `>` := gt definition pred [unfold 1] (a : nat) : nat := nat.cases_on a zero (Ξ» a₁, a₁) -- add is defined in init.num definition sub (a b : nat) : nat := nat.rec_on b a (Ξ» b₁, pred) definition mul (a b : nat) : nat := nat.rec_on b zero (Ξ» b₁ r, r + a) notation a - b := sub a b notation a * b := mul a b /- properties of β„• -/ protected definition is_inhabited [instance] : inhabited nat := inhabited.mk zero protected definition has_decidable_eq [instance] : βˆ€ x y : nat, decidable (x = y) | has_decidable_eq zero zero := inl rfl | has_decidable_eq (succ x) zero := inr (by contradiction) | has_decidable_eq zero (succ y) := inr (by contradiction) | has_decidable_eq (succ x) (succ y) := match has_decidable_eq x y with | inl xeqy := inl (by rewrite xeqy) | inr xney := inr (Ξ» h : succ x = succ y, by injection h with xeqy; exact absurd xeqy xney) end /- properties of inequality -/ theorem le_of_eq {n m : β„•} (p : n = m) : n ≀ m := p β–Έ !le.refl theorem le_succ (n : β„•) : n ≀ succ n := le.step !le.refl theorem pred_le (n : β„•) : pred n ≀ n := by cases n;repeat constructor theorem le_succ_iff_true [simp] (n : β„•) : n ≀ succ n ↔ true := iff_true_intro (le_succ n) theorem pred_le_iff_true [simp] (n : β„•) : pred n ≀ n ↔ true := iff_true_intro (pred_le n) theorem le.trans [trans] {n m k : β„•} (H1 : n ≀ m) : m ≀ k β†’ n ≀ k := le.rec H1 (Ξ»p H2, le.step) theorem le_succ_of_le {n m : β„•} (H : n ≀ m) : n ≀ succ m := le.trans H !le_succ theorem le_of_succ_le {n m : β„•} (H : succ n ≀ m) : n ≀ m := le.trans !le_succ H theorem le_of_lt {n m : β„•} (H : n < m) : n ≀ m := le_of_succ_le H theorem succ_le_succ {n m : β„•} : n ≀ m β†’ succ n ≀ succ m := le.rec !le.refl (Ξ»a b, le.step) theorem pred_le_pred {n m : β„•} : n ≀ m β†’ pred n ≀ pred m := le.rec !le.refl (nat.rec (Ξ»a b, b) (Ξ»a b c, le.step)) theorem le_of_succ_le_succ {n m : β„•} : succ n ≀ succ m β†’ n ≀ m := pred_le_pred theorem le_succ_of_pred_le {n m : β„•} : pred n ≀ m β†’ n ≀ succ m := nat.cases_on n le.step (Ξ»a, succ_le_succ) theorem not_succ_le_zero (n : β„•) : Β¬succ n ≀ zero := by intro H; cases H theorem succ_le_zero_iff_false (n : β„•) : succ n ≀ zero ↔ false := iff_false_intro !not_succ_le_zero theorem not_succ_le_self : Ξ  {n : β„•}, Β¬succ n ≀ n := nat.rec !not_succ_le_zero (Ξ»a b c, b (le_of_succ_le_succ c)) theorem succ_le_self_iff_false [simp] (n : β„•) : succ n ≀ n ↔ false := iff_false_intro not_succ_le_self theorem zero_le : βˆ€ (n : β„•), 0 ≀ n := nat.rec !le.refl (Ξ»a, le.step) theorem zero_le_iff_true [simp] (n : β„•) : 0 ≀ n ↔ true := iff_true_intro !zero_le theorem lt.step {n m : β„•} : n < m β†’ n < succ m := le.step theorem zero_lt_succ (n : β„•) : 0 < succ n := succ_le_succ !zero_le theorem zero_lt_succ_iff_true [simp] (n : β„•) : 0 < succ n ↔ true := iff_true_intro (zero_lt_succ n) theorem lt.trans [trans] {n m k : β„•} (H1 : n < m) : m < k β†’ n < k := le.trans (le.step H1) theorem lt_of_le_of_lt [trans] {n m k : β„•} (H1 : n ≀ m) : m < k β†’ n < k := le.trans (succ_le_succ H1) theorem lt_of_lt_of_le [trans] {n m k : β„•} : n < m β†’ m ≀ k β†’ n < k := le.trans theorem lt.irrefl (n : β„•) : Β¬n < n := not_succ_le_self theorem lt_self_iff_false [simp] (n : β„•) : n < n ↔ false := iff_false_intro (lt.irrefl n) theorem self_lt_succ (n : β„•) : n < succ n := !le.refl theorem self_lt_succ_iff_true [simp] (n : β„•) : n < succ n ↔ true := iff_true_intro (self_lt_succ n) theorem lt.base (n : β„•) : n < succ n := !le.refl theorem le_lt_antisymm {n m : β„•} (H1 : n ≀ m) (H2 : m < n) : false := !lt.irrefl (lt_of_le_of_lt H1 H2) theorem le.antisymm {n m : β„•} (H1 : n ≀ m) : m ≀ n β†’ n = m := le.cases_on H1 (Ξ»a, rfl) (Ξ»a b c, absurd (lt_of_le_of_lt b c) !lt.irrefl) theorem lt_le_antisymm {n m : β„•} (H1 : n < m) (H2 : m ≀ n) : false := le_lt_antisymm H2 H1 theorem lt.asymm {n m : β„•} (H1 : n < m) : Β¬ m < n := le_lt_antisymm (le_of_lt H1) theorem not_lt_zero (a : β„•) : Β¬ a < zero := !not_succ_le_zero theorem lt_zero_iff_false [simp] (a : β„•) : a < zero ↔ false := iff_false_intro (not_lt_zero a) theorem eq_or_lt_of_le {a b : β„•} (H : a ≀ b) : a = b ∨ a < b := le.cases_on H (inl rfl) (Ξ»n h, inr (succ_le_succ h)) theorem le_of_eq_or_lt {a b : β„•} (H : a = b ∨ a < b) : a ≀ b := or.elim H !le_of_eq !le_of_lt -- less-than is well-founded definition lt.wf [instance] : well_founded lt := well_founded.intro (nat.rec (!acc.intro (Ξ»n H, absurd H (not_lt_zero n))) (Ξ»n IH, !acc.intro (Ξ»m H, elim (eq_or_lt_of_le (le_of_succ_le_succ H)) (Ξ»e, eq.substr e IH) (acc.inv IH)))) definition measure {A : Type} : (A β†’ β„•) β†’ A β†’ A β†’ Prop := inv_image lt definition measure.wf {A : Type} (f : A β†’ β„•) : well_founded (measure f) := inv_image.wf f lt.wf theorem succ_lt_succ {a b : β„•} : a < b β†’ succ a < succ b := succ_le_succ theorem lt_of_succ_lt {a b : β„•} : succ a < b β†’ a < b := le_of_succ_le theorem lt_of_succ_lt_succ {a b : β„•} : succ a < succ b β†’ a < b := le_of_succ_le_succ definition decidable_le [instance] : decidable_rel le := nat.rec (Ξ»m, (decidable.inl !zero_le)) (Ξ»n IH m, !nat.cases_on (decidable.inr (not_succ_le_zero n)) (Ξ»m, decidable.rec (Ξ»H, inl (succ_le_succ H)) (Ξ»H, inr (Ξ»a, H (le_of_succ_le_succ a))) (IH m))) definition decidable_lt [instance] : decidable_rel lt := _ definition decidable_gt [instance] : decidable_rel gt := _ definition decidable_ge [instance] : decidable_rel ge := _ theorem lt_or_ge (a b : β„•) : a < b ∨ a β‰₯ b := nat.rec (inr !zero_le) (Ξ»n, or.rec (Ξ»h, inl (le_succ_of_le h)) (Ξ»h, elim (eq_or_lt_of_le h) (Ξ»e, inl (eq.subst e !le.refl)) inr)) b definition lt_ge_by_cases {a b : β„•} {P : Type} (H1 : a < b β†’ P) (H2 : a β‰₯ b β†’ P) : P := by_cases H1 (Ξ»h, H2 (elim !lt_or_ge (Ξ»a, absurd a h) (Ξ»a, a))) definition lt.by_cases {a b : β„•} {P : Type} (H1 : a < b β†’ P) (H2 : a = b β†’ P) (H3 : b < a β†’ P) : P := lt_ge_by_cases H1 (Ξ»h₁, lt_ge_by_cases H3 (Ξ»hβ‚‚, H2 (le.antisymm hβ‚‚ h₁))) theorem lt.trichotomy (a b : β„•) : a < b ∨ a = b ∨ b < a := lt.by_cases (Ξ»H, inl H) (Ξ»H, inr (inl H)) (Ξ»H, inr (inr H)) theorem eq_or_lt_of_not_lt {a b : β„•} (hnlt : Β¬ a < b) : a = b ∨ b < a := or.rec_on (lt.trichotomy a b) (Ξ» hlt, absurd hlt hnlt) (Ξ» h, h) theorem lt_succ_of_le {a b : β„•} : a ≀ b β†’ a < succ b := succ_le_succ theorem lt_of_succ_le {a b : β„•} (h : succ a ≀ b) : a < b := h theorem succ_le_of_lt {a b : β„•} (h : a < b) : succ a ≀ b := h theorem succ_sub_succ_eq_sub [simp] (a b : β„•) : succ a - succ b = a - b := nat.rec rfl (Ξ» b, congr_arg pred) b theorem sub_eq_succ_sub_succ (a b : β„•) : a - b = succ a - succ b := eq.symm !succ_sub_succ_eq_sub theorem zero_sub_eq_zero [simp] (a : β„•) : zero - a = zero := nat.rec rfl (Ξ» a, congr_arg pred) a theorem zero_eq_zero_sub (a : β„•) : zero = zero - a := eq.symm !zero_sub_eq_zero theorem sub_le (a b : β„•) : a - b ≀ a := nat.rec_on b !le.refl (Ξ» b₁, le.trans !pred_le) theorem sub_le_iff_true [simp] (a b : β„•) : a - b ≀ a ↔ true := iff_true_intro (sub_le a b) theorem sub_lt {a b : β„•} (H1 : zero < a) (H2 : zero < b) : a - b < a := !nat.cases_on (Ξ»h, absurd h !lt.irrefl) (Ξ»a h, succ_le_succ (!nat.cases_on (Ξ»h, absurd h !lt.irrefl) (Ξ»b c, eq.substr !succ_sub_succ_eq_sub !sub_le) H2)) H1 theorem sub_lt_succ (a b : β„•) : a - b < succ a := lt_succ_of_le !sub_le theorem sub_lt_succ_iff_true [simp] (a b : β„•) : a - b < succ a ↔ true := iff_true_intro !sub_lt_succ end nat namespace nat_esimp open nat attribute add mul sub [unfold 2] attribute of_num [unfold 1] end nat_esimp
6462b0fa64ca26281adcef62b094210adaeb63d9
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/rat/floor.lean
f924c35b7c09fa4edb137801fd5a6e25ff137ee2
[ "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
6,441
lean
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro, Kevin Kappelmann -/ import algebra.order.floor import tactic.field_simp /-! # Floor Function for Rational Numbers ## Summary We define the `floor` function and the `floor_ring` instance on `β„š`. Some technical lemmas relating `floor` to integer division and modulo arithmetic are derived as well as some simple inequalities. ## Tags rat, rationals, β„š, floor -/ open int namespace rat /-- `floor q` is the largest integer `z` such that `z ≀ q` -/ protected def floor : β„š β†’ β„€ | ⟨n, d, h, c⟩ := n / d protected theorem le_floor {z : β„€} : βˆ€ {r : β„š}, z ≀ rat.floor r ↔ (z : β„š) ≀ r | ⟨n, d, h, c⟩ := begin simp [rat.floor], rw [num_denom'], have h' := int.coe_nat_lt.2 h, conv { to_rhs, rw [coe_int_eq_mk, rat.le_def zero_lt_one h', mul_one] }, exact int.le_div_iff_mul_le h' end instance : floor_ring β„š := floor_ring.of_floor β„š rat.floor $ Ξ» a z, rat.le_floor.symm protected lemma floor_def {q : β„š} : ⌊qβŒ‹ = q.num / q.denom := by { cases q, refl } lemma floor_int_div_nat_eq_div {n : β„€} {d : β„•} : ⌊(↑n : β„š) / (↑d : β„š)βŒ‹ = n / (↑d : β„€) := begin rw [rat.floor_def], cases decidable.em (d = 0) with d_eq_zero d_ne_zero, { simp [d_eq_zero] }, { cases decidable.em (n = 0) with n_eq_zero n_ne_zero, { simp [n_eq_zero] }, { set q := (n : β„š) / d with q_eq, obtain ⟨c, n_eq_c_mul_num, d_eq_c_mul_denom⟩ : βˆƒ c, n = c * q.num ∧ (d : β„€) = c * q.denom, by { rw q_eq, exact_mod_cast (@rat.exists_eq_mul_div_num_and_eq_mul_div_denom n d n_ne_zero (by exact_mod_cast d_ne_zero)) }, suffices : q.num / q.denom = c * q.num / (c * q.denom), by rwa [n_eq_c_mul_num, d_eq_c_mul_denom], suffices : c > 0, by solve_by_elim [int.mul_div_mul_of_pos], have q_denom_mul_c_pos : (0 : β„€) < q.denom * c, by { have : (d : β„€) > 0, by exact_mod_cast (pos_iff_ne_zero.elim_right d_ne_zero), rwa [d_eq_c_mul_denom, mul_comm] at this }, suffices : (0 : β„€) ≀ q.denom, from pos_of_mul_pos_left q_denom_mul_c_pos this, exact_mod_cast (le_of_lt q.pos) } } end end rat lemma int.mod_nat_eq_sub_mul_floor_rat_div {n : β„€} {d : β„•} : n % d = n - d * ⌊(n : β„š) / dβŒ‹ := by rw [(eq_sub_of_add_eq $ int.mod_add_div n d), rat.floor_int_div_nat_eq_div] lemma nat.coprime_sub_mul_floor_rat_div_of_coprime {n d : β„•} (n_coprime_d : n.coprime d) : ((n : β„€) - d * ⌊(n : β„š)/ dβŒ‹).nat_abs.coprime d := begin have : (n : β„€) % d = n - d * ⌊(n : β„š)/ dβŒ‹, from int.mod_nat_eq_sub_mul_floor_rat_div, rw ←this, have : d.coprime n, from n_coprime_d.symm, rwa [nat.coprime, nat.gcd_rec] at this end namespace rat lemma num_lt_succ_floor_mul_denom (q : β„š) : q.num < (⌊qβŒ‹ + 1) * q.denom := begin suffices : (q.num : β„š) < (⌊qβŒ‹ + 1) * q.denom, by exact_mod_cast this, suffices : (q.num : β„š) < (q - fract q + 1) * q.denom, by { have : (⌊qβŒ‹ : β„š) = q - fract q, from (eq_sub_of_add_eq $ floor_add_fract q), rwa this }, suffices : (q.num : β„š) < q.num + (1 - fract q) * q.denom, by { have : (q - fract q + 1) * q.denom = q.num + (1 - fract q) * q.denom, calc (q - fract q + 1) * q.denom = (q + (1 - fract q)) * q.denom : by ring ... = q * q.denom + (1 - fract q) * q.denom : by rw add_mul ... = q.num + (1 - fract q) * q.denom : by simp, rwa this }, suffices : 0 < (1 - fract q) * q.denom, by { rw ←sub_lt_iff_lt_add', simpa }, have : 0 < 1 - fract q, by { have : fract q < 1, from fract_lt_one q, have : 0 + fract q < 1, by simp [this], rwa lt_sub_iff_add_lt }, exact mul_pos this (by exact_mod_cast q.pos) end lemma fract_inv_num_lt_num_of_pos {q : β„š} (q_pos : 0 < q): (fract q⁻¹).num < q.num := begin -- we know that the numerator must be positive have q_num_pos : 0 < q.num, from rat.num_pos_iff_pos.elim_right q_pos, -- we will work with the absolute value of the numerator, which is equal to the numerator have q_num_abs_eq_q_num : (q.num.nat_abs : β„€) = q.num, from (int.nat_abs_of_nonneg q_num_pos.le), set q_inv := (q.denom : β„š) / q.num with q_inv_def, have q_inv_eq : q⁻¹ = q_inv, from rat.inv_def', suffices : (q_inv - ⌊q_invβŒ‹).num < q.num, by rwa q_inv_eq, suffices : ((q.denom - q.num * ⌊q_invβŒ‹ : β„š) / q.num).num < q.num, by field_simp [this, (ne_of_gt q_num_pos)], suffices : (q.denom : β„€) - q.num * ⌊q_invβŒ‹ < q.num, by { -- use that `q.num` and `q.denom` are coprime to show that the numerator stays unreduced have : ((q.denom - q.num * ⌊q_invβŒ‹ : β„š) / q.num).num = q.denom - q.num * ⌊q_invβŒ‹, by { suffices : ((q.denom : β„€) - q.num * ⌊q_invβŒ‹).nat_abs.coprime q.num.nat_abs, by exact_mod_cast (rat.num_div_eq_of_coprime q_num_pos this), have : (q.num.nat_abs : β„š) = (q.num : β„š), by exact_mod_cast q_num_abs_eq_q_num, have tmp := nat.coprime_sub_mul_floor_rat_div_of_coprime q.cop.symm, simpa only [this, q_num_abs_eq_q_num] using tmp }, rwa this }, -- to show the claim, start with the following inequality have q_inv_num_denom_ineq : q⁻¹.num - ⌊qβ»ΒΉβŒ‹ * q⁻¹.denom < q⁻¹.denom, by { have : q⁻¹.num < (⌊qβ»ΒΉβŒ‹ + 1) * q⁻¹.denom, from rat.num_lt_succ_floor_mul_denom q⁻¹, have : q⁻¹.num < ⌊qβ»ΒΉβŒ‹ * q⁻¹.denom + q⁻¹.denom, by rwa [right_distrib, one_mul] at this, rwa [←sub_lt_iff_lt_add'] at this }, -- use that `q.num` and `q.denom` are coprime to show that q_inv is the unreduced reciprocal -- of `q` have : q_inv.num = q.denom ∧ q_inv.denom = q.num.nat_abs, by { have coprime_q_denom_q_num : q.denom.coprime q.num.nat_abs, from q.cop.symm, have : int.nat_abs q.denom = q.denom, by simp, rw ←this at coprime_q_denom_q_num, rw q_inv_def, split, { exact_mod_cast (rat.num_div_eq_of_coprime q_num_pos coprime_q_denom_q_num) }, { suffices : (((q.denom : β„š) / q.num).denom : β„€) = q.num.nat_abs, by exact_mod_cast this, rw q_num_abs_eq_q_num, exact_mod_cast (rat.denom_div_eq_of_coprime q_num_pos coprime_q_denom_q_num) } }, rwa [q_inv_eq, this.left, this.right, q_num_abs_eq_q_num, mul_comm] at q_inv_num_denom_ineq end end rat
dd2a02003fbba331ac8c3ad9b7434518f80a7e42
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/topology/metric_space/emetric_space.lean
4572a62ecd7f93752e5fcb1064cf962ff31440e6
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
37,665
lean
/- Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis, Johannes HΓΆlzl, Mario Carneiro, SΓ©bastien GouΓ«zel -/ import data.real.ennreal import data.finset.intervals import topology.uniform_space.uniform_embedding import topology.uniform_space.pi import topology.uniform_space.uniform_convergence /-! # Extended metric spaces This file is devoted to the definition and study of `emetric_spaces`, i.e., metric spaces in which the distance is allowed to take the value ∞. This extended distance is called `edist`, and takes values in `ennreal`. Many definitions and theorems expected on emetric spaces are already introduced on uniform spaces and topological spaces. For example: open and closed sets, compactness, completeness, continuity and uniform continuity The class `emetric_space` therefore extends `uniform_space` (and `topological_space`). -/ open set filter classical noncomputable theory open_locale uniformity topological_space big_operators filter nnreal universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} /-- Characterizing uniformities associated to a (generalized) distance function `D` in terms of the elements of the uniformity. -/ theorem uniformity_dist_of_mem_uniformity [linear_order Ξ²] {U : filter (Ξ± Γ— Ξ±)} (z : Ξ²) (D : Ξ± β†’ Ξ± β†’ Ξ²) (H : βˆ€ s, s ∈ U ↔ βˆƒΞ΅>z, βˆ€{a b:Ξ±}, D a b < Ξ΅ β†’ (a, b) ∈ s) : U = β¨… Ξ΅>z, π“Ÿ {p:Ξ±Γ—Ξ± | D p.1 p.2 < Ξ΅} := le_antisymm (le_infi $ Ξ» Ξ΅, le_infi $ Ξ» Ξ΅0, le_principal_iff.2 $ (H _).2 ⟨Ρ, Ξ΅0, Ξ» a b, id⟩) (Ξ» r ur, let ⟨Ρ, Ξ΅0, h⟩ := (H _).1 ur in mem_infi_sets Ξ΅ $ mem_infi_sets Ξ΅0 $ mem_principal_sets.2 $ Ξ» ⟨a, b⟩, h) class has_edist (Ξ± : Type*) := (edist : Ξ± β†’ Ξ± β†’ ennreal) export has_edist (edist) /-- Creating a uniform space from an extended distance. -/ def uniform_space_of_edist (edist : Ξ± β†’ Ξ± β†’ ennreal) (edist_self : βˆ€ x : Ξ±, edist x x = 0) (edist_comm : βˆ€ x y : Ξ±, edist x y = edist y x) (edist_triangle : βˆ€ x y z : Ξ±, edist x z ≀ edist x y + edist y z) : uniform_space Ξ± := uniform_space.of_core { uniformity := (β¨… Ξ΅>0, π“Ÿ {p:Ξ±Γ—Ξ± | edist p.1 p.2 < Ξ΅}), refl := le_infi $ assume Ξ΅, le_infi $ by simp [set.subset_def, id_rel, edist_self, (>)] {contextual := tt}, comp := le_infi $ assume Ξ΅, le_infi $ assume h, have (2 : ennreal) = (2 : β„•) := by simp, have A : 0 < Ξ΅ / 2 := ennreal.div_pos_iff.2 ⟨ne_of_gt h, by { convert ennreal.nat_ne_top 2 }⟩, lift'_le (mem_infi_sets (Ξ΅ / 2) $ mem_infi_sets A (subset.refl _)) $ have βˆ€ (a b c : Ξ±), edist a c < Ξ΅ / 2 β†’ edist c b < Ξ΅ / 2 β†’ edist a b < Ξ΅, from assume a b c hac hcb, calc edist a b ≀ edist a c + edist c b : edist_triangle _ _ _ ... < Ξ΅ / 2 + Ξ΅ / 2 : ennreal.add_lt_add hac hcb ... = Ξ΅ : by rw [ennreal.add_halves], by simpa [comp_rel], symm := tendsto_infi.2 $ assume Ξ΅, tendsto_infi.2 $ assume h, tendsto_infi' Ξ΅ $ tendsto_infi' h $ tendsto_principal_principal.2 $ by simp [edist_comm] } -- the uniform structure is embedded in the emetric space structure -- to avoid instance diamond issues. See Note [forgetful inheritance]. /-- Extended metric spaces, with an extended distance `edist` possibly taking the value ∞ Each emetric space induces a canonical `uniform_space` and hence a canonical `topological_space`. This is enforced in the type class definition, by extending the `uniform_space` structure. When instantiating an `emetric_space` structure, the uniformity fields are not necessary, they will be filled in by default. There is a default value for the uniformity, that can be substituted in cases of interest, for instance when instantiating an `emetric_space` structure on a product. Continuity of `edist` is proved in `topology.instances.ennreal` -/ class emetric_space (Ξ± : Type u) extends has_edist Ξ± : Type u := (edist_self : βˆ€ x : Ξ±, edist x x = 0) (eq_of_edist_eq_zero : βˆ€ {x y : Ξ±}, edist x y = 0 β†’ x = y) (edist_comm : βˆ€ x y : Ξ±, edist x y = edist y x) (edist_triangle : βˆ€ x y z : Ξ±, edist x z ≀ edist x y + edist y z) (to_uniform_space : uniform_space Ξ± := uniform_space_of_edist edist edist_self edist_comm edist_triangle) (uniformity_edist : 𝓀 Ξ± = β¨… Ξ΅>0, π“Ÿ {p:Ξ±Γ—Ξ± | edist p.1 p.2 < Ξ΅} . control_laws_tac) /- emetric spaces are less common than metric spaces. Therefore, we work in a dedicated namespace, while notions associated to metric spaces are mostly in the root namespace. -/ variables [emetric_space Ξ±] @[priority 100] -- see Note [lower instance priority] instance emetric_space.to_uniform_space' : uniform_space Ξ± := emetric_space.to_uniform_space export emetric_space (edist_self eq_of_edist_eq_zero edist_comm edist_triangle) attribute [simp] edist_self /-- Characterize the equality of points by the vanishing of their extended distance -/ @[simp] theorem edist_eq_zero {x y : Ξ±} : edist x y = 0 ↔ x = y := iff.intro eq_of_edist_eq_zero (assume : x = y, this β–Έ edist_self _) @[simp] theorem zero_eq_edist {x y : Ξ±} : 0 = edist x y ↔ x = y := iff.intro (assume h, eq_of_edist_eq_zero (h.symm)) (assume : x = y, this β–Έ (edist_self _).symm) theorem edist_le_zero {x y : Ξ±} : (edist x y ≀ 0) ↔ x = y := nonpos_iff_eq_zero.trans edist_eq_zero /-- Triangle inequality for the extended distance -/ theorem edist_triangle_left (x y z : Ξ±) : edist x y ≀ edist z x + edist z y := by rw edist_comm z; apply edist_triangle theorem edist_triangle_right (x y z : Ξ±) : edist x y ≀ edist x z + edist y z := by rw edist_comm y; apply edist_triangle lemma edist_triangle4 (x y z t : Ξ±) : edist x t ≀ edist x y + edist y z + edist z t := calc edist x t ≀ edist x z + edist z t : edist_triangle x z t ... ≀ (edist x y + edist y z) + edist z t : add_le_add_right (edist_triangle x y z) _ /-- The triangle (polygon) inequality for sequences of points; `finset.Ico` version. -/ lemma edist_le_Ico_sum_edist (f : β„• β†’ Ξ±) {m n} (h : m ≀ n) : edist (f m) (f n) ≀ βˆ‘ i in finset.Ico m n, edist (f i) (f (i + 1)) := begin revert n, refine nat.le_induction _ _, { simp only [finset.sum_empty, finset.Ico.self_eq_empty, edist_self], -- TODO: Why doesn't Lean close this goal automatically? `apply le_refl` fails too. exact le_refl (0:ennreal) }, { assume n hn hrec, calc edist (f m) (f (n+1)) ≀ edist (f m) (f n) + edist (f n) (f (n+1)) : edist_triangle _ _ _ ... ≀ βˆ‘ i in finset.Ico m n, _ + _ : add_le_add hrec (le_refl _) ... = βˆ‘ i in finset.Ico m (n+1), _ : by rw [finset.Ico.succ_top hn, finset.sum_insert, add_comm]; simp } end /-- The triangle (polygon) inequality for sequences of points; `finset.range` version. -/ lemma edist_le_range_sum_edist (f : β„• β†’ Ξ±) (n : β„•) : edist (f 0) (f n) ≀ βˆ‘ i in finset.range n, edist (f i) (f (i + 1)) := finset.Ico.zero_bot n β–Έ edist_le_Ico_sum_edist f (nat.zero_le n) /-- A version of `edist_le_Ico_sum_edist` with each intermediate distance replaced with an upper estimate. -/ lemma edist_le_Ico_sum_of_edist_le {f : β„• β†’ Ξ±} {m n} (hmn : m ≀ n) {d : β„• β†’ ennreal} (hd : βˆ€ {k}, m ≀ k β†’ k < n β†’ edist (f k) (f (k + 1)) ≀ d k) : edist (f m) (f n) ≀ βˆ‘ i in finset.Ico m n, d i := le_trans (edist_le_Ico_sum_edist f hmn) $ finset.sum_le_sum $ Ξ» k hk, hd (finset.Ico.mem.1 hk).1 (finset.Ico.mem.1 hk).2 /-- A version of `edist_le_range_sum_edist` with each intermediate distance replaced with an upper estimate. -/ lemma edist_le_range_sum_of_edist_le {f : β„• β†’ Ξ±} (n : β„•) {d : β„• β†’ ennreal} (hd : βˆ€ {k}, k < n β†’ edist (f k) (f (k + 1)) ≀ d k) : edist (f 0) (f n) ≀ βˆ‘ i in finset.range n, d i := finset.Ico.zero_bot n β–Έ edist_le_Ico_sum_of_edist_le (zero_le n) (Ξ» _ _, hd) /-- Two points coincide if their distance is `< Ξ΅` for all positive Ξ΅ -/ theorem eq_of_forall_edist_le {x y : Ξ±} (h : βˆ€Ξ΅ > 0, edist x y ≀ Ξ΅) : x = y := eq_of_edist_eq_zero (eq_of_le_of_forall_le_of_dense bot_le h) /-- Reformulation of the uniform structure in terms of the extended distance -/ theorem uniformity_edist : 𝓀 Ξ± = β¨… Ξ΅>0, π“Ÿ {p:Ξ±Γ—Ξ± | edist p.1 p.2 < Ξ΅} := emetric_space.uniformity_edist theorem uniformity_basis_edist : (𝓀 Ξ±).has_basis (Ξ» Ξ΅ : ennreal, 0 < Ξ΅) (Ξ» Ξ΅, {p:Ξ±Γ—Ξ± | edist p.1 p.2 < Ξ΅}) := (@uniformity_edist Ξ± _).symm β–Έ has_basis_binfi_principal (Ξ» r hr p hp, ⟨min r p, lt_min hr hp, Ξ» x hx, lt_of_lt_of_le hx (min_le_left _ _), Ξ» x hx, lt_of_lt_of_le hx (min_le_right _ _)⟩) ⟨1, ennreal.zero_lt_one⟩ /-- Characterization of the elements of the uniformity in terms of the extended distance -/ theorem mem_uniformity_edist {s : set (Ξ±Γ—Ξ±)} : s ∈ 𝓀 Ξ± ↔ (βˆƒΞ΅>0, βˆ€{a b:Ξ±}, edist a b < Ξ΅ β†’ (a, b) ∈ s) := uniformity_basis_edist.mem_uniformity_iff /-- Given `f : Ξ² β†’ ennreal`, if `f` sends `{i | p i}` to a set of positive numbers accumulating to zero, then `f i`-neighborhoods of the diagonal form a basis of `𝓀 Ξ±`. For specific bases see `uniformity_basis_edist`, `uniformity_basis_edist'`, `uniformity_basis_edist_nnreal`, and `uniformity_basis_edist_inv_nat`. -/ protected theorem emetric.mk_uniformity_basis {Ξ² : Type*} {p : Ξ² β†’ Prop} {f : Ξ² β†’ ennreal} (hfβ‚€ : βˆ€ x, p x β†’ 0 < f x) (hf : βˆ€ Ξ΅, 0 < Ξ΅ β†’ βˆƒ x (hx : p x), f x ≀ Ξ΅) : (𝓀 Ξ±).has_basis p (Ξ» x, {p:Ξ±Γ—Ξ± | edist p.1 p.2 < f x}) := begin refine ⟨λ s, uniformity_basis_edist.mem_iff.trans _⟩, split, { rintros ⟨Ρ, Ξ΅β‚€, hΡ⟩, rcases hf Ξ΅ Ξ΅β‚€ with ⟨i, hi, H⟩, exact ⟨i, hi, Ξ» x hx, hΞ΅ $ lt_of_lt_of_le hx H⟩ }, { exact Ξ» ⟨i, hi, H⟩, ⟨f i, hfβ‚€ i hi, H⟩ } end /-- Given `f : Ξ² β†’ ennreal`, if `f` sends `{i | p i}` to a set of positive numbers accumulating to zero, then closed `f i`-neighborhoods of the diagonal form a basis of `𝓀 Ξ±`. For specific bases see `uniformity_basis_edist_le` and `uniformity_basis_edist_le'`. -/ protected theorem emetric.mk_uniformity_basis_le {Ξ² : Type*} {p : Ξ² β†’ Prop} {f : Ξ² β†’ ennreal} (hfβ‚€ : βˆ€ x, p x β†’ 0 < f x) (hf : βˆ€ Ξ΅, 0 < Ξ΅ β†’ βˆƒ x (hx : p x), f x ≀ Ξ΅) : (𝓀 Ξ±).has_basis p (Ξ» x, {p:Ξ±Γ—Ξ± | edist p.1 p.2 ≀ f x}) := begin refine ⟨λ s, uniformity_basis_edist.mem_iff.trans _⟩, split, { rintros ⟨Ρ, Ξ΅β‚€, hΡ⟩, rcases exists_between Ξ΅β‚€ with ⟨Ρ', hΞ΅'⟩, rcases hf Ξ΅' hΞ΅'.1 with ⟨i, hi, H⟩, exact ⟨i, hi, Ξ» x hx, hΞ΅ $ lt_of_le_of_lt (le_trans hx H) hΞ΅'.2⟩ }, { exact Ξ» ⟨i, hi, H⟩, ⟨f i, hfβ‚€ i hi, Ξ» x hx, H (le_of_lt hx)⟩ } end theorem uniformity_basis_edist_le : (𝓀 Ξ±).has_basis (Ξ» Ξ΅ : ennreal, 0 < Ξ΅) (Ξ» Ξ΅, {p:Ξ±Γ—Ξ± | edist p.1 p.2 ≀ Ξ΅}) := emetric.mk_uniformity_basis_le (Ξ» _, id) (Ξ» Ξ΅ Ξ΅β‚€, ⟨Ρ, Ξ΅β‚€, le_refl Ρ⟩) theorem uniformity_basis_edist' (Ξ΅' : ennreal) (hΞ΅' : 0 < Ξ΅') : (𝓀 Ξ±).has_basis (Ξ» Ξ΅ : ennreal, Ξ΅ ∈ Ioo 0 Ξ΅') (Ξ» Ξ΅, {p:Ξ±Γ—Ξ± | edist p.1 p.2 < Ξ΅}) := emetric.mk_uniformity_basis (Ξ» _, and.left) (Ξ» Ξ΅ Ξ΅β‚€, let ⟨δ, hδ⟩ := exists_between hΞ΅' in ⟨min Ξ΅ Ξ΄, ⟨lt_min Ξ΅β‚€ hΞ΄.1, lt_of_le_of_lt (min_le_right _ _) hΞ΄.2⟩, min_le_left _ _⟩) theorem uniformity_basis_edist_le' (Ξ΅' : ennreal) (hΞ΅' : 0 < Ξ΅') : (𝓀 Ξ±).has_basis (Ξ» Ξ΅ : ennreal, Ξ΅ ∈ Ioo 0 Ξ΅') (Ξ» Ξ΅, {p:Ξ±Γ—Ξ± | edist p.1 p.2 ≀ Ξ΅}) := emetric.mk_uniformity_basis_le (Ξ» _, and.left) (Ξ» Ξ΅ Ξ΅β‚€, let ⟨δ, hδ⟩ := exists_between hΞ΅' in ⟨min Ξ΅ Ξ΄, ⟨lt_min Ξ΅β‚€ hΞ΄.1, lt_of_le_of_lt (min_le_right _ _) hΞ΄.2⟩, min_le_left _ _⟩) theorem uniformity_basis_edist_nnreal : (𝓀 Ξ±).has_basis (Ξ» Ξ΅ : ℝβ‰₯0, 0 < Ξ΅) (Ξ» Ξ΅, {p:Ξ±Γ—Ξ± | edist p.1 p.2 < Ξ΅}) := emetric.mk_uniformity_basis (Ξ» _, ennreal.coe_pos.2) (Ξ» Ξ΅ Ξ΅β‚€, let ⟨δ, hδ⟩ := ennreal.lt_iff_exists_nnreal_btwn.1 Ξ΅β‚€ in ⟨δ, ennreal.coe_pos.1 hΞ΄.1, le_of_lt hΞ΄.2⟩) theorem uniformity_basis_edist_inv_nat : (𝓀 Ξ±).has_basis (Ξ» _, true) (Ξ» n:β„•, {p:Ξ±Γ—Ξ± | edist p.1 p.2 < (↑n)⁻¹}) := emetric.mk_uniformity_basis (Ξ» n _, ennreal.inv_pos.2 $ ennreal.nat_ne_top n) (Ξ» Ξ΅ Ξ΅β‚€, let ⟨n, hn⟩ := ennreal.exists_inv_nat_lt (ne_of_gt Ξ΅β‚€) in ⟨n, trivial, le_of_lt hn⟩) /-- Fixed size neighborhoods of the diagonal belong to the uniform structure -/ theorem edist_mem_uniformity {Ξ΅:ennreal} (Ξ΅0 : 0 < Ξ΅) : {p:Ξ±Γ—Ξ± | edist p.1 p.2 < Ξ΅} ∈ 𝓀 Ξ± := mem_uniformity_edist.2 ⟨Ρ, Ξ΅0, Ξ» a b, id⟩ namespace emetric theorem uniformity_has_countable_basis : is_countably_generated (𝓀 Ξ±) := is_countably_generated_of_seq ⟨_, uniformity_basis_edist_inv_nat.eq_infi⟩ /-- Ξ΅-Ξ΄ characterization of uniform continuity on a set for emetric spaces -/ theorem uniform_continuous_on_iff [emetric_space Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} : uniform_continuous_on f s ↔ βˆ€ Ξ΅ > 0, βˆƒ Ξ΄ > 0, βˆ€{a b}, a ∈ s β†’ b ∈ s β†’ edist a b < Ξ΄ β†’ edist (f a) (f b) < Ξ΅ := uniformity_basis_edist.uniform_continuous_on_iff uniformity_basis_edist /-- Ξ΅-Ξ΄ characterization of uniform continuity on emetric spaces -/ theorem uniform_continuous_iff [emetric_space Ξ²] {f : Ξ± β†’ Ξ²} : uniform_continuous f ↔ βˆ€ Ξ΅ > 0, βˆƒ Ξ΄ > 0, βˆ€{a b:Ξ±}, edist a b < Ξ΄ β†’ edist (f a) (f b) < Ξ΅ := uniformity_basis_edist.uniform_continuous_iff uniformity_basis_edist /-- Ξ΅-Ξ΄ characterization of uniform embeddings on emetric spaces -/ theorem uniform_embedding_iff [emetric_space Ξ²] {f : Ξ± β†’ Ξ²} : uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧ βˆ€ Ξ΄ > 0, βˆƒ Ξ΅ > 0, βˆ€ {a b : Ξ±}, edist (f a) (f b) < Ξ΅ β†’ edist a b < Ξ΄ := uniform_embedding_def'.trans $ and_congr iff.rfl $ and_congr iff.rfl ⟨λ H Ξ΄ Ξ΄0, let ⟨t, tu, ht⟩ := H _ (edist_mem_uniformity Ξ΄0), ⟨Ρ, Ξ΅0, hΡ⟩ := mem_uniformity_edist.1 tu in ⟨Ρ, Ξ΅0, Ξ» a b h, ht _ _ (hΞ΅ h)⟩, Ξ» H s su, let ⟨δ, Ξ΄0, hδ⟩ := mem_uniformity_edist.1 su, ⟨Ρ, Ξ΅0, hΡ⟩ := H _ Ξ΄0 in ⟨_, edist_mem_uniformity Ξ΅0, Ξ» a b h, hΞ΄ (hΞ΅ h)⟩⟩ /-- A map between emetric spaces is a uniform embedding if and only if the edistance between `f x` and `f y` is controlled in terms of the distance between `x` and `y` and conversely. -/ theorem uniform_embedding_iff' [emetric_space Ξ²] {f : Ξ± β†’ Ξ²} : uniform_embedding f ↔ (βˆ€ Ξ΅ > 0, βˆƒ Ξ΄ > 0, βˆ€ {a b : Ξ±}, edist a b < Ξ΄ β†’ edist (f a) (f b) < Ξ΅) ∧ (βˆ€ Ξ΄ > 0, βˆƒ Ξ΅ > 0, βˆ€ {a b : Ξ±}, edist (f a) (f b) < Ξ΅ β†’ edist a b < Ξ΄) := begin split, { assume h, exact ⟨uniform_continuous_iff.1 (uniform_embedding_iff.1 h).2.1, (uniform_embedding_iff.1 h).2.2⟩ }, { rintros ⟨h₁, hβ‚‚βŸ©, refine uniform_embedding_iff.2 ⟨_, uniform_continuous_iff.2 h₁, hβ‚‚βŸ©, assume x y hxy, have : edist x y ≀ 0, { refine le_of_forall_lt' (λδ Ξ΄pos, _), rcases hβ‚‚ Ξ΄ Ξ΄pos with ⟨Ρ, Ξ΅pos, hΡ⟩, have : edist (f x) (f y) < Ξ΅, by simpa [hxy], exact hΞ΅ this }, simpa using this } end /-- Ξ΅-Ξ΄ characterization of Cauchy sequences on emetric spaces -/ protected lemma cauchy_iff {f : filter Ξ±} : cauchy f ↔ f β‰  βŠ₯ ∧ βˆ€ Ξ΅ > 0, βˆƒ t ∈ f, βˆ€ x y ∈ t, edist x y < Ξ΅ := uniformity_basis_edist.cauchy_iff /-- A very useful criterion to show that a space is complete is to show that all sequences which satisfy a bound of the form `edist (u n) (u m) < B N` for all `n m β‰₯ N` are converging. This is often applied for `B N = 2^{-N}`, i.e., with a very fast convergence to `0`, which makes it possible to use arguments of converging series, while this is impossible to do in general for arbitrary Cauchy sequences. -/ theorem complete_of_convergent_controlled_sequences (B : β„• β†’ ennreal) (hB : βˆ€n, 0 < B n) (H : βˆ€u : β„• β†’ Ξ±, (βˆ€N n m : β„•, N ≀ n β†’ N ≀ m β†’ edist (u n) (u m) < B N) β†’ βˆƒx, tendsto u at_top (𝓝 x)) : complete_space Ξ± := uniform_space.complete_of_convergent_controlled_sequences uniformity_has_countable_basis (Ξ» n, {p:Ξ±Γ—Ξ± | edist p.1 p.2 < B n}) (Ξ» n, edist_mem_uniformity $ hB n) H /-- A sequentially complete emetric space is complete. -/ theorem complete_of_cauchy_seq_tendsto : (βˆ€ u : β„• β†’ Ξ±, cauchy_seq u β†’ βˆƒa, tendsto u at_top (𝓝 a)) β†’ complete_space Ξ± := uniform_space.complete_of_cauchy_seq_tendsto uniformity_has_countable_basis /-- Expressing locally uniform convergence on a set using `edist`. -/ lemma tendsto_locally_uniformly_on_iff {ΞΉ : Type*} [topological_space Ξ²] {F : ΞΉ β†’ Ξ² β†’ Ξ±} {f : Ξ² β†’ Ξ±} {p : filter ΞΉ} {s : set Ξ²} : tendsto_locally_uniformly_on F f p s ↔ βˆ€ Ξ΅ > 0, βˆ€ x ∈ s, βˆƒ t ∈ 𝓝[s] x, βˆ€αΆ  n in p, βˆ€ y ∈ t, edist (f y) (F n y) < Ξ΅ := begin refine ⟨λ H Ξ΅ hΞ΅, H _ (edist_mem_uniformity hΞ΅), Ξ» H u hu x hx, _⟩, rcases mem_uniformity_edist.1 hu with ⟨Ρ, Ξ΅pos, hΡ⟩, rcases H Ξ΅ Ξ΅pos x hx with ⟨t, ht, Ht⟩, exact ⟨t, ht, Ht.mono (Ξ» n hs x hx, hΞ΅ (hs x hx))⟩ end /-- Expressing uniform convergence on a set using `edist`. -/ lemma tendsto_uniformly_on_iff {ΞΉ : Type*} {F : ΞΉ β†’ Ξ² β†’ Ξ±} {f : Ξ² β†’ Ξ±} {p : filter ΞΉ} {s : set Ξ²} : tendsto_uniformly_on F f p s ↔ βˆ€ Ξ΅ > 0, βˆ€αΆ  n in p, βˆ€ x ∈ s, edist (f x) (F n x) < Ξ΅ := begin refine ⟨λ H Ξ΅ hΞ΅, H _ (edist_mem_uniformity hΞ΅), Ξ» H u hu, _⟩, rcases mem_uniformity_edist.1 hu with ⟨Ρ, Ξ΅pos, hΡ⟩, exact (H Ξ΅ Ξ΅pos).mono (Ξ» n hs x hx, hΞ΅ (hs x hx)) end /-- Expressing locally uniform convergence using `edist`. -/ lemma tendsto_locally_uniformly_iff {ΞΉ : Type*} [topological_space Ξ²] {F : ΞΉ β†’ Ξ² β†’ Ξ±} {f : Ξ² β†’ Ξ±} {p : filter ΞΉ} : tendsto_locally_uniformly F f p ↔ βˆ€ Ξ΅ > 0, βˆ€ (x : Ξ²), βˆƒ t ∈ 𝓝 x, βˆ€αΆ  n in p, βˆ€ y ∈ t, edist (f y) (F n y) < Ξ΅ := by simp only [← tendsto_locally_uniformly_on_univ, tendsto_locally_uniformly_on_iff, mem_univ, forall_const, exists_prop, nhds_within_univ] /-- Expressing uniform convergence using `edist`. -/ lemma tendsto_uniformly_iff {ΞΉ : Type*} {F : ΞΉ β†’ Ξ² β†’ Ξ±} {f : Ξ² β†’ Ξ±} {p : filter ΞΉ} : tendsto_uniformly F f p ↔ βˆ€ Ξ΅ > 0, βˆ€αΆ  n in p, βˆ€ x, edist (f x) (F n x) < Ξ΅ := by simp only [← tendsto_uniformly_on_univ, tendsto_uniformly_on_iff, mem_univ, forall_const] end emetric open emetric /-- An emetric space is separated -/ @[priority 100] -- see Note [lower instance priority] instance to_separated : separated_space Ξ± := separated_def.2 $ Ξ» x y h, eq_of_forall_edist_le $ Ξ» Ξ΅ Ξ΅0, le_of_lt (h _ (edist_mem_uniformity Ξ΅0)) /-- Auxiliary function to replace the uniformity on an emetric space with a uniformity which is equal to the original one, but maybe not defeq. This is useful if one wants to construct an emetric space with a specified uniformity. See Note [forgetful inheritance] explaining why having definitionally the right uniformity is often important. -/ def emetric_space.replace_uniformity {Ξ±} [U : uniform_space Ξ±] (m : emetric_space Ξ±) (H : @uniformity _ U = @uniformity _ emetric_space.to_uniform_space) : emetric_space Ξ± := { edist := @edist _ m.to_has_edist, edist_self := edist_self, eq_of_edist_eq_zero := @eq_of_edist_eq_zero _ _, edist_comm := edist_comm, edist_triangle := edist_triangle, to_uniform_space := U, uniformity_edist := H.trans (@emetric_space.uniformity_edist Ξ± _) } /-- The extended metric induced by an injective function taking values in an emetric space. -/ def emetric_space.induced {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (hf : function.injective f) (m : emetric_space Ξ²) : emetric_space Ξ± := { edist := Ξ» x y, edist (f x) (f y), edist_self := Ξ» x, edist_self _, eq_of_edist_eq_zero := Ξ» x y h, hf (edist_eq_zero.1 h), edist_comm := Ξ» x y, edist_comm _ _, edist_triangle := Ξ» x y z, edist_triangle _ _ _, to_uniform_space := uniform_space.comap f m.to_uniform_space, uniformity_edist := begin apply @uniformity_dist_of_mem_uniformity _ _ _ _ _ (Ξ» x y, edist (f x) (f y)), refine Ξ» s, mem_comap_sets.trans _, split; intro H, { rcases H with ⟨r, ru, rs⟩, rcases mem_uniformity_edist.1 ru with ⟨Ρ, Ξ΅0, hΡ⟩, refine ⟨Ρ, Ξ΅0, Ξ» a b h, rs (hΞ΅ _)⟩, exact h }, { rcases H with ⟨Ρ, Ξ΅0, hΡ⟩, exact ⟨_, edist_mem_uniformity Ξ΅0, Ξ» ⟨a, b⟩, hΡ⟩ } end } /-- Emetric space instance on subsets of emetric spaces -/ instance {Ξ± : Type*} {p : Ξ± β†’ Prop} [t : emetric_space Ξ±] : emetric_space (subtype p) := t.induced coe (Ξ» x y, subtype.ext_iff_val.2) /-- The extended distance on a subset of an emetric space is the restriction of the original distance, by definition -/ theorem subtype.edist_eq {p : Ξ± β†’ Prop} (x y : subtype p) : edist x y = edist (x : Ξ±) y := rfl /-- The product of two emetric spaces, with the max distance, is an extended metric spaces. We make sure that the uniform structure thus constructed is the one corresponding to the product of uniform spaces, to avoid diamond problems. -/ instance prod.emetric_space_max [emetric_space Ξ²] : emetric_space (Ξ± Γ— Ξ²) := { edist := Ξ» x y, max (edist x.1 y.1) (edist x.2 y.2), edist_self := Ξ» x, by simp, eq_of_edist_eq_zero := Ξ» x y h, begin cases max_le_iff.1 (le_of_eq h) with h₁ hβ‚‚, have A : x.fst = y.fst := edist_le_zero.1 h₁, have B : x.snd = y.snd := edist_le_zero.1 hβ‚‚, exact prod.ext_iff.2 ⟨A, B⟩ end, edist_comm := Ξ» x y, by simp [edist_comm], edist_triangle := Ξ» x y z, max_le (le_trans (edist_triangle _ _ _) (add_le_add (le_max_left _ _) (le_max_left _ _))) (le_trans (edist_triangle _ _ _) (add_le_add (le_max_right _ _) (le_max_right _ _))), uniformity_edist := begin refine uniformity_prod.trans _, simp [emetric_space.uniformity_edist, comap_infi], rw ← infi_inf_eq, congr, funext, rw ← infi_inf_eq, congr, funext, simp [inf_principal, ext_iff, max_lt_iff] end, to_uniform_space := prod.uniform_space } lemma prod.edist_eq [emetric_space Ξ²] (x y : Ξ± Γ— Ξ²) : edist x y = max (edist x.1 y.1) (edist x.2 y.2) := rfl section pi open finset variables {Ο€ : Ξ² β†’ Type*} [fintype Ξ²] /-- The product of a finite number of emetric spaces, with the max distance, is still an emetric space. This construction would also work for infinite products, but it would not give rise to the product topology. Hence, we only formalize it in the good situation of finitely many spaces. -/ instance emetric_space_pi [βˆ€b, emetric_space (Ο€ b)] : emetric_space (Ξ b, Ο€ b) := { edist := Ξ» f g, finset.sup univ (Ξ»b, edist (f b) (g b)), edist_self := assume f, bot_unique $ finset.sup_le $ by simp, edist_comm := assume f g, by unfold edist; congr; funext a; exact edist_comm _ _, edist_triangle := assume f g h, begin simp only [finset.sup_le_iff], assume b hb, exact le_trans (edist_triangle _ (g b) _) (add_le_add (le_sup hb) (le_sup hb)) end, eq_of_edist_eq_zero := assume f g eq0, begin have eq1 : sup univ (Ξ» (b : Ξ²), edist (f b) (g b)) ≀ 0 := le_of_eq eq0, simp only [finset.sup_le_iff] at eq1, exact (funext $ assume b, edist_le_zero.1 $ eq1 b $ mem_univ b), end, to_uniform_space := Pi.uniform_space _, uniformity_edist := begin simp only [Pi.uniformity, emetric_space.uniformity_edist, comap_infi, gt_iff_lt, preimage_set_of_eq, comap_principal], rw infi_comm, congr, funext Ξ΅, rw infi_comm, congr, funext Ξ΅pos, change 0 < Ξ΅ at Ξ΅pos, simp [set.ext_iff, Ξ΅pos] end } lemma edist_pi_def [Ξ  b, emetric_space (Ο€ b)] (f g : Ξ  b, Ο€ b) : edist f g = finset.sup univ (Ξ»b, edist (f b) (g b)) := rfl end pi namespace emetric variables {x y z : Ξ±} {Ξ΅ Ρ₁ Ξ΅β‚‚ : ennreal} {s : set Ξ±} /-- `emetric.ball x Ξ΅` is the set of all points `y` with `edist y x < Ξ΅` -/ def ball (x : Ξ±) (Ξ΅ : ennreal) : set Ξ± := {y | edist y x < Ξ΅} @[simp] theorem mem_ball : y ∈ ball x Ξ΅ ↔ edist y x < Ξ΅ := iff.rfl theorem mem_ball' : y ∈ ball x Ξ΅ ↔ edist x y < Ξ΅ := by rw edist_comm; refl /-- `emetric.closed_ball x Ξ΅` is the set of all points `y` with `edist y x ≀ Ξ΅` -/ def closed_ball (x : Ξ±) (Ξ΅ : ennreal) := {y | edist y x ≀ Ξ΅} @[simp] theorem mem_closed_ball : y ∈ closed_ball x Ξ΅ ↔ edist y x ≀ Ξ΅ := iff.rfl theorem ball_subset_closed_ball : ball x Ξ΅ βŠ† closed_ball x Ξ΅ := assume y, by simp; intros h; apply le_of_lt h theorem pos_of_mem_ball (hy : y ∈ ball x Ξ΅) : 0 < Ξ΅ := lt_of_le_of_lt (zero_le _) hy theorem mem_ball_self (h : 0 < Ξ΅) : x ∈ ball x Ξ΅ := show edist x x < Ξ΅, by rw edist_self; assumption theorem mem_closed_ball_self : x ∈ closed_ball x Ξ΅ := show edist x x ≀ Ξ΅, by rw edist_self; exact bot_le theorem mem_ball_comm : x ∈ ball y Ξ΅ ↔ y ∈ ball x Ξ΅ := by simp [edist_comm] theorem ball_subset_ball (h : Ρ₁ ≀ Ξ΅β‚‚) : ball x Ρ₁ βŠ† ball x Ξ΅β‚‚ := Ξ» y (yx : _ < Ρ₁), lt_of_lt_of_le yx h theorem closed_ball_subset_closed_ball (h : Ρ₁ ≀ Ξ΅β‚‚) : closed_ball x Ρ₁ βŠ† closed_ball x Ξ΅β‚‚ := Ξ» y (yx : _ ≀ Ρ₁), le_trans yx h theorem ball_disjoint (h : Ρ₁ + Ξ΅β‚‚ ≀ edist x y) : ball x Ρ₁ ∩ ball y Ξ΅β‚‚ = βˆ… := eq_empty_iff_forall_not_mem.2 $ Ξ» z ⟨h₁, hβ‚‚βŸ©, not_lt_of_le (edist_triangle_left x y z) (lt_of_lt_of_le (ennreal.add_lt_add h₁ hβ‚‚) h) theorem ball_subset (h : edist x y + Ρ₁ ≀ Ξ΅β‚‚) (h' : edist x y < ⊀) : ball x Ρ₁ βŠ† ball y Ξ΅β‚‚ := Ξ» z zx, calc edist z y ≀ edist z x + edist x y : edist_triangle _ _ _ ... = edist x y + edist z x : add_comm _ _ ... < edist x y + Ρ₁ : (ennreal.add_lt_add_iff_left h').2 zx ... ≀ Ξ΅β‚‚ : h theorem exists_ball_subset_ball (h : y ∈ ball x Ξ΅) : βˆƒ Ξ΅' > 0, ball y Ξ΅' βŠ† ball x Ξ΅ := begin have : 0 < Ξ΅ - edist y x := by simpa using h, refine ⟨Ρ - edist y x, this, ball_subset _ _⟩, { rw ennreal.add_sub_cancel_of_le (le_of_lt h), apply le_refl _}, { have : edist y x β‰  ⊀ := ne_top_of_lt h, apply lt_top_iff_ne_top.2 this } end theorem ball_eq_empty_iff : ball x Ξ΅ = βˆ… ↔ Ξ΅ = 0 := eq_empty_iff_forall_not_mem.trans ⟨λh, le_bot_iff.1 (le_of_not_gt (Ξ» Ξ΅0, h _ (mem_ball_self Ξ΅0))), λΡ0 y h, not_lt_of_le (le_of_eq Ξ΅0) (pos_of_mem_ball h)⟩ /-- Relation β€œtwo points are at a finite edistance” is an equivalence relation. -/ def edist_lt_top_setoid : setoid Ξ± := { r := Ξ» x y, edist x y < ⊀, iseqv := ⟨λ x, by { rw edist_self, exact ennreal.coe_lt_top }, Ξ» x y h, by rwa edist_comm, Ξ» x y z hxy hyz, lt_of_le_of_lt (edist_triangle x y z) (ennreal.add_lt_top.2 ⟨hxy, hyz⟩)⟩ } @[simp] lemma ball_zero : ball x 0 = βˆ… := by rw [emetric.ball_eq_empty_iff] theorem nhds_basis_eball : (𝓝 x).has_basis (Ξ» Ξ΅:ennreal, 0 < Ξ΅) (ball x) := nhds_basis_uniformity uniformity_basis_edist theorem nhds_basis_closed_eball : (𝓝 x).has_basis (Ξ» Ξ΅:ennreal, 0 < Ξ΅) (closed_ball x) := nhds_basis_uniformity uniformity_basis_edist_le theorem nhds_eq : 𝓝 x = (β¨…Ξ΅>0, π“Ÿ (ball x Ξ΅)) := nhds_basis_eball.eq_binfi theorem mem_nhds_iff : s ∈ 𝓝 x ↔ βˆƒΞ΅>0, ball x Ξ΅ βŠ† s := nhds_basis_eball.mem_iff theorem is_open_iff : is_open s ↔ βˆ€x∈s, βˆƒΞ΅>0, ball x Ξ΅ βŠ† s := by simp [is_open_iff_nhds, mem_nhds_iff] theorem is_open_ball : is_open (ball x Ξ΅) := is_open_iff.2 $ Ξ» y, exists_ball_subset_ball theorem is_closed_ball_top : is_closed (ball x ⊀) := is_open_iff.2 $ Ξ» y hy, ⟨⊀, ennreal.coe_lt_top, subset_compl_iff_disjoint.2 $ ball_disjoint $ by { rw ennreal.top_add, exact le_of_not_lt hy }⟩ theorem ball_mem_nhds (x : Ξ±) {Ξ΅ : ennreal} (Ξ΅0 : 0 < Ξ΅) : ball x Ξ΅ ∈ 𝓝 x := mem_nhds_sets is_open_ball (mem_ball_self Ξ΅0) /-- Ξ΅-characterization of the closure in emetric spaces -/ theorem mem_closure_iff : x ∈ closure s ↔ βˆ€Ξ΅>0, βˆƒy ∈ s, edist x y < Ξ΅ := (mem_closure_iff_nhds_basis nhds_basis_eball).trans $ by simp only [mem_ball, edist_comm x] theorem tendsto_nhds {f : filter Ξ²} {u : Ξ² β†’ Ξ±} {a : Ξ±} : tendsto u f (𝓝 a) ↔ βˆ€ Ξ΅ > 0, βˆ€αΆ  x in f, edist (u x) a < Ξ΅ := nhds_basis_eball.tendsto_right_iff theorem tendsto_at_top [nonempty Ξ²] [semilattice_sup Ξ²] {u : Ξ² β†’ Ξ±} {a : Ξ±} : tendsto u at_top (𝓝 a) ↔ βˆ€Ξ΅>0, βˆƒN, βˆ€nβ‰₯N, edist (u n) a < Ξ΅ := (at_top_basis.tendsto_iff nhds_basis_eball).trans $ by simp only [exists_prop, true_and, mem_Ici, mem_ball] /-- In an emetric space, Cauchy sequences are characterized by the fact that, eventually, the edistance between its elements is arbitrarily small -/ @[nolint ge_or_gt] -- see Note [nolint_ge] theorem cauchy_seq_iff [nonempty Ξ²] [semilattice_sup Ξ²] {u : Ξ² β†’ Ξ±} : cauchy_seq u ↔ βˆ€Ξ΅>0, βˆƒN, βˆ€m nβ‰₯N, edist (u m) (u n) < Ξ΅ := uniformity_basis_edist.cauchy_seq_iff /-- A variation around the emetric characterization of Cauchy sequences -/ theorem cauchy_seq_iff' [nonempty Ξ²] [semilattice_sup Ξ²] {u : Ξ² β†’ Ξ±} : cauchy_seq u ↔ βˆ€Ξ΅>(0 : ennreal), βˆƒN, βˆ€nβ‰₯N, edist (u n) (u N) < Ξ΅ := uniformity_basis_edist.cauchy_seq_iff' /-- A variation of the emetric characterization of Cauchy sequences that deals with `ℝβ‰₯0` upper bounds. -/ theorem cauchy_seq_iff_nnreal [nonempty Ξ²] [semilattice_sup Ξ²] {u : Ξ² β†’ Ξ±} : cauchy_seq u ↔ βˆ€ Ξ΅ : ℝβ‰₯0, 0 < Ξ΅ β†’ βˆƒ N, βˆ€ n, N ≀ n β†’ edist (u n) (u N) < Ξ΅ := uniformity_basis_edist_nnreal.cauchy_seq_iff' theorem totally_bounded_iff {s : set Ξ±} : totally_bounded s ↔ βˆ€ Ξ΅ > 0, βˆƒt : set Ξ±, finite t ∧ s βŠ† ⋃y∈t, ball y Ξ΅ := ⟨λ H Ξ΅ Ξ΅0, H _ (edist_mem_uniformity Ξ΅0), Ξ» H r ru, let ⟨Ρ, Ξ΅0, hΡ⟩ := mem_uniformity_edist.1 ru, ⟨t, ft, h⟩ := H Ξ΅ Ξ΅0 in ⟨t, ft, subset.trans h $ Union_subset_Union $ Ξ» y, Union_subset_Union $ Ξ» yt z, hΡ⟩⟩ theorem totally_bounded_iff' {s : set Ξ±} : totally_bounded s ↔ βˆ€ Ξ΅ > 0, βˆƒtβŠ†s, finite t ∧ s βŠ† ⋃y∈t, ball y Ξ΅ := ⟨λ H Ξ΅ Ξ΅0, (totally_bounded_iff_subset.1 H) _ (edist_mem_uniformity Ξ΅0), Ξ» H r ru, let ⟨Ρ, Ξ΅0, hΡ⟩ := mem_uniformity_edist.1 ru, ⟨t, _, ft, h⟩ := H Ξ΅ Ξ΅0 in ⟨t, ft, subset.trans h $ Union_subset_Union $ Ξ» y, Union_subset_Union $ Ξ» yt z, hΡ⟩⟩ section compact /-- A compact set in an emetric space is separable, i.e., it is the closure of a countable set -/ lemma countable_closure_of_compact {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} (hs : is_compact s) : βˆƒ t βŠ† s, (countable t ∧ s = closure t) := begin have A : βˆ€ (e:ennreal), e > 0 β†’ βˆƒ t βŠ† s, (finite t ∧ s βŠ† (⋃x∈t, ball x e)) := totally_bounded_iff'.1 (compact_iff_totally_bounded_complete.1 hs).1, -- assume e, finite_cover_balls_of_compact hs, have B : βˆ€ (e:ennreal), βˆƒ t βŠ† s, finite t ∧ (e > 0 β†’ s βŠ† (⋃x∈t, ball x e)), { intro e, cases le_or_gt e 0 with h, { exact βŸ¨βˆ…, by finish⟩ }, { rcases A e h with ⟨s, ⟨finite_s, closure_s⟩⟩, existsi s, finish }}, /-The desired countable set is obtained by taking for each `n` the centers of a finite cover by balls of radius `1/n`, and then the union over `n`. -/ choose T T_in_s finite_T using B, let t := ⋃n:β„•, T n⁻¹, have T₁ : t βŠ† s := begin apply Union_subset, assume n, apply T_in_s end, have Tβ‚‚ : countable t := by finish [countable_Union, finite.countable], have T₃ : s βŠ† closure t, { intros x x_in_s, apply mem_closure_iff.2, intros Ξ΅ Ξ΅pos, rcases ennreal.exists_inv_nat_lt (bot_lt_iff_ne_bot.1 Ξ΅pos) with ⟨n, hn⟩, have inv_n_pos : (0 : ennreal) < (n : β„•)⁻¹ := by simp [ennreal.bot_lt_iff_ne_bot], have C : x ∈ (⋃y∈ T (n : β„•)⁻¹, ball y (n : β„•)⁻¹) := mem_of_mem_of_subset x_in_s ((finite_T (n : β„•)⁻¹).2 inv_n_pos), rcases mem_Union.1 C with ⟨y, _, ⟨y_in_T, rfl⟩, Dxy⟩, simp at Dxy, -- Dxy : edist x y < 1 / ↑n have : y ∈ t := mem_of_mem_of_subset y_in_T (by apply subset_Union (Ξ» (n:β„•), T (n : β„•)⁻¹)), have : edist x y < Ξ΅ := lt_trans Dxy hn, exact ⟨y, β€Ήy ∈ tβ€Ί, β€Ήedist x y < Ξ΅β€ΊβŸ© }, have Tβ‚„ : closure t βŠ† s := calc closure t βŠ† closure s : closure_mono T₁ ... = s : hs.is_closed.closure_eq, exact ⟨t, ⟨T₁, Tβ‚‚, subset.antisymm T₃ Tβ‚„βŸ©βŸ© end end compact section first_countable @[priority 100] -- see Note [lower instance priority] instance (Ξ± : Type u) [emetric_space Ξ±] : topological_space.first_countable_topology Ξ± := uniform_space.first_countable_topology uniformity_has_countable_basis end first_countable section second_countable open topological_space /-- A separable emetric space is second countable: one obtains a countable basis by taking the balls centered at points in a dense subset, and with rational radii. We do not register this as an instance, as there is already an instance going in the other direction from second countable spaces to separable spaces, and we want to avoid loops. -/ lemma second_countable_of_separable (Ξ± : Type u) [emetric_space Ξ±] [separable_space Ξ±] : second_countable_topology Ξ± := uniform_space.second_countable_of_separable uniformity_has_countable_basis end second_countable section diam /-- The diameter of a set in an emetric space, named `emetric.diam` -/ def diam (s : set Ξ±) := ⨆ (x ∈ s) (y ∈ s), edist x y lemma diam_le_iff_forall_edist_le {d : ennreal} : diam s ≀ d ↔ βˆ€ (x ∈ s) (y ∈ s), edist x y ≀ d := by simp only [diam, supr_le_iff] /-- If two points belong to some set, their edistance is bounded by the diameter of the set -/ lemma edist_le_diam_of_mem (hx : x ∈ s) (hy : y ∈ s) : edist x y ≀ diam s := diam_le_iff_forall_edist_le.1 (le_refl _) x hx y hy /-- If the distance between any two points in a set is bounded by some constant, this constant bounds the diameter. -/ lemma diam_le_of_forall_edist_le {d : ennreal} (h : βˆ€ (x ∈ s) (y ∈ s), edist x y ≀ d) : diam s ≀ d := diam_le_iff_forall_edist_le.2 h /-- The diameter of a subsingleton vanishes. -/ lemma diam_subsingleton (hs : s.subsingleton) : diam s = 0 := nonpos_iff_eq_zero.1 $ diam_le_of_forall_edist_le $ Ξ» x hx y hy, (hs hx hy).symm β–Έ edist_self y β–Έ le_refl _ /-- The diameter of the empty set vanishes -/ @[simp] lemma diam_empty : diam (βˆ… : set Ξ±) = 0 := diam_subsingleton subsingleton_empty /-- The diameter of a singleton vanishes -/ @[simp] lemma diam_singleton : diam ({x} : set Ξ±) = 0 := diam_subsingleton subsingleton_singleton lemma diam_eq_zero_iff : diam s = 0 ↔ s.subsingleton := ⟨λ h x hx y hy, edist_le_zero.1 $ h β–Έ edist_le_diam_of_mem hx hy, diam_subsingleton⟩ lemma diam_pos_iff : 0 < diam s ↔ βˆƒ (x ∈ s) (y ∈ s), x β‰  y := begin have := not_congr (@diam_eq_zero_iff _ _ s), dunfold set.subsingleton at this, push_neg at this, simpa only [pos_iff_ne_zero, exists_prop] using this end lemma diam_insert : diam (insert x s) = max (⨆ y ∈ s, edist x y) (diam s) := eq_of_forall_ge_iff $ Ξ» d, by simp only [diam_le_iff_forall_edist_le, ball_insert_iff, edist_self, edist_comm x, max_le_iff, supr_le_iff, zero_le, true_and, forall_and_distrib, and_self, ← and_assoc] lemma diam_pair : diam ({x, y} : set Ξ±) = edist x y := by simp only [supr_singleton, diam_insert, diam_singleton, ennreal.max_zero_right] lemma diam_triple : diam ({x, y, z} : set Ξ±) = max (max (edist x y) (edist x z)) (edist y z) := by simp only [diam_insert, supr_insert, supr_singleton, diam_singleton, ennreal.max_zero_right, ennreal.sup_eq_max] /-- The diameter is monotonous with respect to inclusion -/ lemma diam_mono {s t : set Ξ±} (h : s βŠ† t) : diam s ≀ diam t := diam_le_of_forall_edist_le $ Ξ» x hx y hy, edist_le_diam_of_mem (h hx) (h hy) /-- The diameter of a union is controlled by the diameter of the sets, and the edistance between two points in the sets. -/ lemma diam_union {t : set Ξ±} (xs : x ∈ s) (yt : y ∈ t) : diam (s βˆͺ t) ≀ diam s + edist x y + diam t := begin have A : βˆ€a ∈ s, βˆ€b ∈ t, edist a b ≀ diam s + edist x y + diam t := Ξ»a ha b hb, calc edist a b ≀ edist a x + edist x y + edist y b : edist_triangle4 _ _ _ _ ... ≀ diam s + edist x y + diam t : add_le_add (add_le_add (edist_le_diam_of_mem ha xs) (le_refl _)) (edist_le_diam_of_mem yt hb), refine diam_le_of_forall_edist_le (Ξ»a ha b hb, _), cases (mem_union _ _ _).1 ha with h'a h'a; cases (mem_union _ _ _).1 hb with h'b h'b, { calc edist a b ≀ diam s : edist_le_diam_of_mem h'a h'b ... ≀ diam s + (edist x y + diam t) : le_add_right (le_refl _) ... = diam s + edist x y + diam t : by simp only [add_comm, eq_self_iff_true, add_left_comm] }, { exact A a h'a b h'b }, { have Z := A b h'b a h'a, rwa [edist_comm] at Z }, { calc edist a b ≀ diam t : edist_le_diam_of_mem h'a h'b ... ≀ (diam s + edist x y) + diam t : le_add_left (le_refl _) } end lemma diam_union' {t : set Ξ±} (h : (s ∩ t).nonempty) : diam (s βˆͺ t) ≀ diam s + diam t := let ⟨x, ⟨xs, xt⟩⟩ := h in by simpa using diam_union xs xt lemma diam_closed_ball {r : ennreal} : diam (closed_ball x r) ≀ 2 * r := diam_le_of_forall_edist_le $ Ξ»a ha b hb, calc edist a b ≀ edist a x + edist b x : edist_triangle_right _ _ _ ... ≀ r + r : add_le_add ha hb ... = 2 * r : by simp [mul_two, mul_comm] lemma diam_ball {r : ennreal} : diam (ball x r) ≀ 2 * r := le_trans (diam_mono ball_subset_closed_ball) diam_closed_ball end diam end emetric --namespace
0b47fc2bc4615a396eaf3b591ab08074c1cd1130
a7602958ab456501ff85db8cf5553f7bcab201d7
/Notes/Logic_and_Proof/Chapter9/9.20.lean
bd77164f289c931729c7e609e9c314ecf13362fc
[]
no_license
enlauren/math-logic
081e2e737c8afb28dbb337968df95ead47321ba0
086b6935543d1841f1db92d0e49add1124054c37
refs/heads/master
1,594,506,621,950
1,558,634,976,000
1,558,634,976,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,558
lean
-- 9.2 Using the universal quantifier import init.data.nat open nat constant A : β„• β†’ Prop constant B : β„• β†’ Prop #check βˆ€ x, (A x ∨ B x) ∧ Β¬ (A x ∧ B x) -- Remember, after βˆ€ x, ___, the universal quantifier has the widest scope -- possible. This means that: #check βˆ€ x, A x -> B x -- Is the same as: #check βˆ€ x, (A x -> B x) -- But we can tighten the bounds if we want: #check βˆ€ x, (A x) -> B x -- βˆ€-Introduction. -- In this case, our goal is to _prove_ some βˆ€ statement. We do this with the -- βˆ€-introduction. example: βˆ€ x, A x := show βˆ€ x, A x, from assume x, -- This just says "fix an arbitrary x of type β„•" (in this case). -- We can also change the variable name here, since it is bound (I guess). show A x, from sorry -- sorry should be some proof of |A x|. -- βˆ€-Elimination. -- In this case, our goal is to prove some |A x|, from an |βˆ€ x, A x| assumption. -- Setup a variable of type |β„•|, and |Type| variable n: β„• variable z: Type -- Setup a |C|, analogous to |A|, but which accepts Type, instead of |β„•|. -- constant A : β„• β†’ Prop variable C: Type -> Prop -- Setup hypotheses that βˆ€ x, we assume |A x| and |C x| are true. We are allowed -- to "apply" this hypotheses to any variable whose type matches the formula -- associated with the hypothesis. variable h1: βˆ€ x, A x -- Can apply this to arbitrary values of |β„•|. variable h2: βˆ€ x, C x -- Can | | | | | | |Type|. example: A n := show A n, from h1 n example: C z := show C z, from h2 z
477511ac91de58d6ddcf55ad4dbb6122772fd373
e21db629d2e37a833531fdcb0b37ce4d71825408
/src/use_cases/parlang/if.lean
2ddd3e6f1bc89c4b4e885f02f781ee209de775fe
[]
no_license
fischerman/GPU-transformation-verifier
614a28cb4606a05a0eb27e8d4eab999f4f5ea60c
75a5016f05382738ff93ce5859c4cfa47ccb63c1
refs/heads/master
1,586,985,789,300
1,579,290,514,000
1,579,290,514,000
165,031,073
1
0
null
null
null
null
UTF-8
Lean
false
false
5,175
lean
import parlang /- IDEA: try to remove the active map from the assertions -/ namespace parlang open kernel -- notation ac ` ⇃ ` c ` β—‚ ` x:(foldr ` β—‚ ` (h t, deactivate_threads c ac h) ac) := x notation ac ` ⇃ ` c ` β—‚ ` s := deactivate_threads c ac s example {σ₁ ι₁ ΞΉβ‚‚ : Type} {τ₁ : ι₁ β†’ Type} {Ο„β‚‚ : ΞΉβ‚‚ β†’ Type} [decidable_eq ι₁] [decidable_eq ΞΉβ‚‚] : {* Ξ» n₁ s₁ ac₁ nβ‚‚ (sβ‚‚ : state nβ‚‚ (memory (Ξ» (n: string), β„•)) Ο„β‚‚) acβ‚‚, 0 < nβ‚‚ ∧ all_threads_active acβ‚‚ *} @kernel.compute ι₁ σ₁ τ₁ id ~> kernel.ite (Ξ»m, m.get "tid" = 1) (kernel.compute (Ξ» m, m.update "x" 1)) (kernel.compute (Ξ»m, m.update "x" 0)) {* Ξ» n₁ s₁ ac₁ nβ‚‚ sβ‚‚ acβ‚‚, βˆ€ (h : 0 < nβ‚‚), (sβ‚‚.threads.nth ⟨0, h⟩).tlocal.get "x" = 1 *} := begin suffices : {* Ξ» n₁ s₁ ac₁ nβ‚‚ (sβ‚‚ : state nβ‚‚ (memory (Ξ» (n: string), β„•)) Ο„β‚‚) acβ‚‚, 0 < nβ‚‚ ∧ (Ξ»acβ‚‚, all_threads_active acβ‚‚) acβ‚‚ *} @kernel.compute ι₁ σ₁ τ₁ id ~> kernel.ite (Ξ»m, m.get "tid" = 1) (kernel.compute (Ξ» m, m.update "x" 1)) (kernel.compute (Ξ»m, m.update "x" 0)) {* Ξ» n₁ s₁ ac₁ nβ‚‚ sβ‚‚ acβ‚‚, (βˆ€ (h : 0 < nβ‚‚), (sβ‚‚.threads.nth ⟨0, h⟩).tlocal.get "x" = 1) ∧ (Ξ»acβ‚‚, all_threads_active acβ‚‚) acβ‚‚ *}, { apply consequence, exact this, simp, intros, exact ⟨a, a_1⟩, intros, exact (a.left) h, }, apply ite_right, swap 7, exact (Ξ»n₁ s₁ ac₁ nβ‚‚ sβ‚‚ acβ‚‚, βˆ€ (h : 0 < nβ‚‚), (sβ‚‚.threads.nth ⟨0, h⟩).tlocal.get "x" = 1), { intros, simp *, }, { intros, exact a, }, { intros, exact a, }, { intros, exact a h, }, { apply consequence_pre, apply compute_right, intros _ _ _ _ _ _ hp _, rw state.map_active_threads_nth_ac, refl, sorry, }, { apply consequence_pre, apply compute_right, intros _ _ _ _ _ _ hp _, rw ← state.map_active_threads_nth_inac, exact hp.left h, sorry, } end section parameters (k : kernel bool (Ξ» (s : string), β„•)) def p₁ : program bool (Ξ» (s : string), β„•) := program.intro (Ξ»m, m.get "x") ( compute (Ξ»_, tt) ;; ite id ( k ) ( store (Ξ»_, ⟨"a", 5⟩) ) ) def pβ‚‚ : program bool (Ξ» (s : string), β„•) := program.intro (Ξ»m, m.get "x") ( compute (Ξ»_, tt) ;; k ) example : rel_hoare_program (Ξ»_, ff) (Ξ»_, ff) (Ξ» m₁ mβ‚‚, eq m₁ mβ‚‚ ∧ 0 < m₁.get "x") p₁ pβ‚‚ eq := begin apply rel_kernel_to_program, apply single_step_left, swap, apply single_step_right, swap, { apply known_branch_left, swap, { apply consequence, apply rhl_eq, swap, {intros, cases a_1 with m₁, use m₁, use m₁, split, assumption, cases a, subst a_left, specialize a_right rfl, cases a_right, subst a_right_left, split, exact a_1_h, refl,}, intros, have : (βˆ€ (tid : fin n₁), id ((vector.nth (s₁.threads) tid).tlocal) = tt) ∧ n₁ = nβ‚‚ ∧ βˆ€ h : n₁ = nβ‚‚, s₁ = (by rw h; exact sβ‚‚) ∧ ac₁ = (by rw h; exact acβ‚‚) := a, exact this.right, }, intros, exact a.left tid, }, apply compute_right, { apply consequence_pre, apply swap (compute_right _), { intros, use s₁, apply exec_skip, }, { intros _ _ _ _ _ _ h, simp[assertion_swap_side], cases h with m₁ h, cases h with mβ‚‚ h, cases h with h₁ h, cases h with hβ‚‚ h, cases h with h₃ h, cases h with hβ‚„ h, cases h with hβ‚… h, cases h with h₆ h, cases h with h₇ h, cases h with hβ‚ˆ h₉, split, { intro tid, rw state.map_active_threads_nth_ac, refl, apply all_threads_active_nth hβ‚ˆ, }, split, { cases h₇, subst h₇_left, rw [h₃, ← hβ‚„], }, intro h', subst h', split, { rw eq.mpr.intro, unfold state.map_active_threads, simp, apply vector.eq_element_wise, intro i, simp, have : ac₁ = acβ‚‚ := all_threads_active_eq hβ‚ˆ h₉, subst this, rw h₆, rw hβ‚…, cases h₇, subst h₇_left, }, { have : ac₁ = acβ‚‚ := all_threads_active_eq hβ‚ˆ h₉, exact this, } } }, { intros, exact a.right, } end end end parlang
d43ab2cc066dd4aa83ac2ff559980c2f2ec2b939
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/continuous_function/basic.lean
3d681aeaacc74277c57b06bece6aa4c229bc1fea
[ "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
7,855
lean
/- Copyright Β© 2020 NicolΓ² Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: NicolΓ² Cavalleri -/ import data.set.Union_lift import topology.homeomorph /-! # Continuous bundled map In this file we define the type `continuous_map` of continuous bundled maps. -/ /-- Bundled continuous maps. -/ @[protect_proj] structure continuous_map (Ξ± : Type*) (Ξ² : Type*) [topological_space Ξ±] [topological_space Ξ²] := (to_fun : Ξ± β†’ Ξ²) (continuous_to_fun : continuous to_fun . tactic.interactive.continuity') notation `C(` Ξ± `, ` Ξ² `)` := continuous_map Ξ± Ξ² namespace continuous_map attribute [continuity] continuous_map.continuous_to_fun variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} variables [topological_space Ξ±] [topological_space Ξ²] [topological_space Ξ³] instance : has_coe_to_fun (C(Ξ±, Ξ²)) (Ξ» _, Ξ± β†’ Ξ²) := ⟨continuous_map.to_fun⟩ @[simp] lemma to_fun_eq_coe {f : C(Ξ±, Ξ²)} : f.to_fun = (f : Ξ± β†’ Ξ²) := rfl variables {Ξ± Ξ²} {f g : continuous_map Ξ± Ξ²} @[continuity] protected lemma continuous (f : C(Ξ±, Ξ²)) : continuous f := f.continuous_to_fun @[continuity] lemma continuous_set_coe (s : set C(Ξ±, Ξ²)) (f : s) : continuous f := by { cases f, rw @coe_fn_coe_base', continuity } protected lemma continuous_at (f : C(Ξ±, Ξ²)) (x : Ξ±) : continuous_at f x := f.continuous.continuous_at protected lemma continuous_within_at (f : C(Ξ±, Ξ²)) (s : set Ξ±) (x : Ξ±) : continuous_within_at f s x := f.continuous.continuous_within_at protected lemma congr_fun {f g : C(Ξ±, Ξ²)} (H : f = g) (x : Ξ±) : f x = g x := H β–Έ rfl protected lemma congr_arg (f : C(Ξ±, Ξ²)) {x y : Ξ±} (h : x = y) : f x = f y := h β–Έ rfl @[ext] theorem ext (H : βˆ€ x, f x = g x) : f = g := by cases f; cases g; congr'; exact funext H lemma ext_iff : f = g ↔ βˆ€ x, f x = g x := ⟨continuous_map.congr_fun, ext⟩ instance [inhabited Ξ²] : inhabited C(Ξ±, Ξ²) := ⟨{ to_fun := Ξ» _, default, }⟩ lemma coe_inj ⦃f g : C(Ξ±, Ξ²)⦄ (h : (f : Ξ± β†’ Ξ²) = g) : f = g := by cases f; cases g; cases h; refl @[simp] lemma coe_mk (f : Ξ± β†’ Ξ²) (h : continuous f) : ⇑(⟨f, h⟩ : continuous_map Ξ± Ξ²) = f := rfl section variables (Ξ± Ξ²) /-- The continuous functions from `Ξ±` to `Ξ²` are the same as the plain functions when `Ξ±` is discrete. -/ @[simps] def equiv_fn_of_discrete [discrete_topology Ξ±] : C(Ξ±, Ξ²) ≃ (Ξ± β†’ Ξ²) := ⟨(Ξ» f, f), (Ξ» f, ⟨f, continuous_of_discrete_topology⟩), Ξ» f, by { ext, refl, }, Ξ» f, by { ext, refl, }⟩ end /-- The identity as a continuous map. -/ def id : C(Ξ±, Ξ±) := ⟨id⟩ @[simp] lemma id_coe : (id : Ξ± β†’ Ξ±) = _root_.id := rfl lemma id_apply (a : Ξ±) : id a = a := rfl /-- The composition of continuous maps, as a continuous map. -/ def comp (f : C(Ξ², Ξ³)) (g : C(Ξ±, Ξ²)) : C(Ξ±, Ξ³) := ⟨f ∘ g⟩ @[simp] lemma comp_coe (f : C(Ξ², Ξ³)) (g : C(Ξ±, Ξ²)) : (comp f g : Ξ± β†’ Ξ³) = f ∘ g := rfl lemma comp_apply (f : C(Ξ², Ξ³)) (g : C(Ξ±, Ξ²)) (a : Ξ±) : comp f g a = f (g a) := rfl @[simp] lemma id_comp (f : C(Ξ², Ξ³)) : id.comp f = f := by { ext, refl } @[simp] lemma comp_id (f : C(Ξ±, Ξ²)) : f.comp id = f := by { ext, refl } /-- Constant map as a continuous map -/ def const (b : Ξ²) : C(Ξ±, Ξ²) := ⟨λ x, b⟩ @[simp] lemma const_coe (b : Ξ²) : (const b : Ξ± β†’ Ξ²) = (Ξ» x, b) := rfl lemma const_apply (b : Ξ²) (a : Ξ±) : const b a = b := rfl instance [h : nonempty Ξ±] [nontrivial Ξ²] : nontrivial C(Ξ±, Ξ²) := { exists_pair_ne := begin obtain ⟨b₁, bβ‚‚, hb⟩ := exists_pair_ne Ξ², refine ⟨const b₁, const bβ‚‚, _⟩, contrapose! hb, change const b₁ h.some = const bβ‚‚ h.some, simp [hb] end } section prod variables {α₁ Ξ±β‚‚ β₁ Ξ²β‚‚ : Type*} [topological_space α₁] [topological_space Ξ±β‚‚] [topological_space β₁] [topological_space Ξ²β‚‚] /-- Given two continuous maps `f` and `g`, this is the continuous map `x ↦ (f x, g x)`. -/ def prod_mk (f : C(Ξ±, β₁)) (g : C(Ξ±, Ξ²β‚‚)) : C(Ξ±, β₁ Γ— Ξ²β‚‚) := { to_fun := (Ξ» x, (f x, g x)), continuous_to_fun := continuous.prod_mk f.continuous g.continuous } /-- Given two continuous maps `f` and `g`, this is the continuous map `(x, y) ↦ (f x, g y)`. -/ def prod_map (f : C(α₁, Ξ±β‚‚)) (g : C(β₁, Ξ²β‚‚)) : C(α₁ Γ— β₁, Ξ±β‚‚ Γ— Ξ²β‚‚) := { to_fun := prod.map f g, continuous_to_fun := continuous.prod_map f.continuous g.continuous } @[simp] lemma prod_eval (f : C(Ξ±, β₁)) (g : C(Ξ±, Ξ²β‚‚)) (a : Ξ±) : (prod_mk f g) a = (f a, g a) := rfl end prod section pi variables {I A : Type*} {X : I β†’ Type*} [topological_space A] [βˆ€ i, topological_space (X i)] /-- Abbreviation for product of continuous maps, which is continuous -/ def pi (f : Ξ  i, C(A, X i)) : C(A, Ξ  i, X i) := { to_fun := Ξ» (a : A) (i : I), f i a, } @[simp] lemma pi_eval (f : Ξ  i, C(A, X i)) (a : A) : (pi f) a = Ξ» i : I, (f i) a := rfl end pi section restrict variables (s : set Ξ±) /-- The restriction of a continuous function `Ξ± β†’ Ξ²` to a subset `s` of `Ξ±`. -/ def restrict (f : C(Ξ±, Ξ²)) : C(s, Ξ²) := ⟨f ∘ coe⟩ @[simp] lemma coe_restrict (f : C(Ξ±, Ξ²)) : ⇑(f.restrict s) = f ∘ coe := rfl end restrict section gluing variables {ΞΉ : Type*} (S : ΞΉ β†’ set Ξ±) (Ο† : Ξ  i : ΞΉ, C(S i, Ξ²)) (hΟ† : βˆ€ i j (x : Ξ±) (hxi : x ∈ S i) (hxj : x ∈ S j), Ο† i ⟨x, hxi⟩ = Ο† j ⟨x, hxj⟩) (hS : βˆ€ x : Ξ±, βˆƒ i, S i ∈ nhds x) include hΟ† hS /-- A family `Ο† i` of continuous maps `C(S i, Ξ²)`, where the domains `S i` contain a neighbourhood of each point in `Ξ±` and the functions `Ο† i` agree pairwise on intersections, can be glued to construct a continuous map in `C(Ξ±, Ξ²)`. -/ noncomputable def lift_cover : C(Ξ±, Ξ²) := begin have H : (⋃ i, S i) = set.univ, { rw set.eq_univ_iff_forall, intros x, rw set.mem_Union, obtain ⟨i, hi⟩ := hS x, exact ⟨i, mem_of_mem_nhds hi⟩ }, refine ⟨set.lift_cover S (Ξ» i, Ο† i) hΟ† H, continuous_subtype_nhds_cover hS _⟩, intros i, convert (Ο† i).continuous, ext x, exact set.lift_cover_coe x, end variables {S Ο† hΟ† hS} @[simp] lemma lift_cover_coe {i : ΞΉ} (x : S i) : lift_cover S Ο† hΟ† hS x = Ο† i x := set.lift_cover_coe _ @[simp] lemma lift_cover_restrict {i : ΞΉ} : (lift_cover S Ο† hΟ† hS).restrict (S i) = Ο† i := ext $ lift_cover_coe omit hΟ† hS variables (A : set (set Ξ±)) (F : Ξ  (s : set Ξ±) (hi : s ∈ A), C(s, Ξ²)) (hF : βˆ€ s (hs : s ∈ A) t (ht : t ∈ A) (x : Ξ±) (hxi : x ∈ s) (hxj : x ∈ t), F s hs ⟨x, hxi⟩ = F t ht ⟨x, hxj⟩) (hA : βˆ€ x : Ξ±, βˆƒ i ∈ A, i ∈ nhds x) include hF hA /-- A family `F s` of continuous maps `C(s, Ξ²)`, where (1) the domains `s` are taken from a set `A` of sets in `Ξ±` which contain a neighbourhood of each point in `Ξ±` and (2) the functions `F s` agree pairwise on intersections, can be glued to construct a continuous map in `C(Ξ±, Ξ²)`. -/ noncomputable def lift_cover' : C(Ξ±, Ξ²) := begin let S : A β†’ set Ξ± := coe, let F : Ξ  i : A, C(i, Ξ²) := Ξ» i, F i i.prop, refine lift_cover S F (Ξ» i j, hF i i.prop j j.prop) _, intros x, obtain ⟨s, hs, hsx⟩ := hA x, exact ⟨⟨s, hs⟩, hsx⟩ end variables {A F hF hA} @[simp] lemma lift_cover_coe' {s : set Ξ±} {hs : s ∈ A} (x : s) : lift_cover' A F hF hA x = F s hs x := let x' : (coe : A β†’ set Ξ±) ⟨s, hs⟩ := x in lift_cover_coe x' @[simp] lemma lift_cover_restrict' {s : set Ξ±} {hs : s ∈ A} : (lift_cover' A F hF hA).restrict s = F s hs := ext $ lift_cover_coe' end gluing end continuous_map /-- The forward direction of a homeomorphism, as a bundled continuous map. -/ @[simps] def homeomorph.to_continuous_map {Ξ± Ξ² : Type*} [topological_space Ξ±] [topological_space Ξ²] (e : Ξ± β‰ƒβ‚œ Ξ²) : C(Ξ±, Ξ²) := ⟨e⟩
1bc575b62f5d194e8ae8869059a385b8914e1fcf
6df8d5ae3acf20ad0d7f0247d2cee1957ef96df1
/notes/10.29.19.lean
eab58d59e8ac88c7ff9758c45a32765696e6585a
[]
no_license
derekjohnsonva/CS2102
8ed45daa6658e6121bac0f6691eac6147d08246d
b3f507d4be824a2511838a1054d04fc9aef3304c
refs/heads/master
1,648,529,162,527
1,578,851,859,000
1,578,851,859,000
233,433,207
0
0
null
null
null
null
UTF-8
Lean
false
false
316
lean
--Derek Johnson /- Propositional logic vs Predicate logic *in prop logic, a variable can only be true or false in Predicate, variables can have any range *Propositions can have qualifiers * Predicate can have quantifiers Propositions and Proofs Proofs of (not true) do not exist -/
fc49f62fafca42905b25b79724b1ae4a11adca57
49ffcd4736fa3bdcc1cdbb546d4c855d67c0f28a
/library/tools/debugger/cli.lean
ece6579a75aa7bfb25bcedf3cfdca268c741d2d1
[ "Apache-2.0" ]
permissive
black13/lean
979e24d09e17b2fdf8ec74aac160583000086bc8
1a80ea9c8e28902cadbfb612896bcd45ba4ce697
refs/heads/master
1,626,839,620,164
1,509,113,016,000
1,509,122,889,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,237
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 Simple command line interface for debugging Lean programs and tactics. -/ import tools.debugger.util namespace debugger @[derive decidable_eq] inductive mode | init | step | run | done structure state := (md : mode) (csz : nat) (fn_bps : list name) (active_bps : list (nat Γ— name)) def init_state : state := { md := mode.init, csz := 0, fn_bps := [], active_bps := [] } meta def show_help : vm unit := do vm.put_str "exit - stop debugger\n", vm.put_str "help - display this message\n", vm.put_str "run - continue execution\n", vm.put_str "step - execute until another function in on the top of the stack\n", vm.put_str "stack trace\n", vm.put_str " up - move up in the stack trace\n", vm.put_str " down - move down in the stack trace\n", vm.put_str " vars - display variables in the current stack frame\n", vm.put_str " stack - display all functions on the call stack\n", vm.put_str " print var - display the value of variable named 'var' in the current stack frame\n", vm.put_str " pidx idx - display the value of variable at position #idx in the current stack frame\n", vm.put_str "breakpoints\n", vm.put_str " break fn - add breakpoint for fn\n", vm.put_str " rbreak fn - remove breakpoint\n", vm.put_str " bs - show breakpoints\n" meta def add_breakpoint (s : state) (args : list string) : vm state := match args with | [arg] := do fn ← return $ to_qualified_name arg, ok ← is_valid_fn_prefix fn, if ok then return {s with fn_bps := fn :: list.filter (Ξ» fn', fn β‰  fn') s.fn_bps} else vm.put_str "invalid 'break' command, given name is not the prefix for any function\n" >> return s | _ := vm.put_str "invalid 'break <fn>' command, incorrect number of arguments\n" >> return s end meta def remove_breakpoint (s : state) (args : list string) : vm state := match args with | [arg] := do fn ← return $ to_qualified_name arg, return {s with fn_bps := list.filter (Ξ» fn', fn β‰  fn') s.fn_bps} | _ := vm.put_str "invalid 'rbreak <fn>' command, incorrect number of arguments\n" >> return s end meta def show_breakpoints : list name β†’ vm unit | [] := return () | (fn::fns) := do vm.put_str " ", vm.put_str fn.to_string, vm.put_str "\n", show_breakpoints fns meta def up_cmd (frame : nat) : vm nat := if frame = 0 then return 0 else show_frame (frame - 1) >> return (frame - 1) meta def down_cmd (frame : nat) : vm nat := do sz ← vm.call_stack_size, if frame >= sz - 1 then return frame else show_frame (frame + 1) >> return (frame + 1) meta def pidx_cmd : nat β†’ list string β†’ vm unit | frame [arg] := do idx ← return $ arg.to_nat, sz ← vm.stack_size, (bp, ep) ← vm.call_stack_var_range frame, if bp + idx >= ep then vm.put_str "invalid 'pidx <idx>' command, index out of bounds\n" else do v ← vm.pp_stack_obj (bp+idx), (n, t) ← vm.stack_obj_info (bp+idx), opts ← vm.get_options, vm.put_str n.to_string, vm.put_str " := ", vm.put_str $ v.to_string opts, vm.put_str "\n" | _ _ := vm.put_str "invalid 'pidx <idx>' command, incorrect number of arguments\n" meta def print_var : nat β†’ nat β†’ name β†’ vm unit | i ep v := do if i = ep then vm.put_str "invalid 'print <var>', unknown variable\n" else do (n, t) ← vm.stack_obj_info i, if n = v then do v ← vm.pp_stack_obj i, opts ← vm.get_options, vm.put_str n.to_string, vm.put_str " := ", vm.put_str $ v.to_string opts, vm.put_str "\n" else print_var (i+1) ep v meta def print_cmd : nat β†’ list string β†’ vm unit | frame [arg] := do (bp, ep) ← vm.call_stack_var_range frame, print_var bp ep (to_qualified_name arg) | _ _ := vm.put_str "invalid 'print <var>' command, incorrect number of arguments\n" meta def cmd_loop_core : state β†’ nat β†’ list string β†’ vm state | s frame default_cmd := do is_eof ← vm.eof, if is_eof then do vm.put_str "stopping debugger... 'end of file' has been found\n", return {s with md := mode.done } else do vm.put_str "% ", l ← vm.get_line, tks ← return $ split l, tks ← return $ if tks = [] then default_cmd else tks, match tks with | [] := cmd_loop_core s frame default_cmd | (cmd::args) := if cmd = "help" ∨ cmd = "h" then show_help >> cmd_loop_core s frame [] else if cmd = "exit" then return {s with md := mode.done } else if cmd = "run" ∨ cmd = "r" then return {s with md := mode.run } else if cmd = "step" ∨ cmd = "s" then return {s with md := mode.step } else if cmd = "break" ∨ cmd = "b" then do new_s ← add_breakpoint s args, cmd_loop_core new_s frame [] else if cmd = "rbreak" then do new_s ← remove_breakpoint s args, cmd_loop_core new_s frame [] else if cmd = "bs" then do vm.put_str "breakpoints\n", show_breakpoints s.fn_bps, cmd_loop_core s frame default_cmd else if cmd = "up" ∨ cmd = "u" then do frame ← up_cmd frame, cmd_loop_core s frame ["u"] else if cmd = "down" ∨ cmd = "d" then do frame ← down_cmd frame, cmd_loop_core s frame ["d"] else if cmd = "vars" ∨ cmd = "v" then do show_vars frame, cmd_loop_core s frame [] else if cmd = "stack" then do show_stack, cmd_loop_core s frame [] else if cmd = "pidx" then do pidx_cmd frame args, cmd_loop_core s frame [] else if cmd = "print" ∨ cmd = "p" then do print_cmd frame args, cmd_loop_core s frame [] else do vm.put_str "unknown command, type 'help' for help\n", cmd_loop_core s frame default_cmd end meta def cmd_loop (s : state) (default_cmd : list string) : vm state := do csz ← vm.call_stack_size, cmd_loop_core s (csz - 1) default_cmd def prune_active_bps_core (csz : nat) : list (nat Γ— name) β†’ list (nat Γ— name) | [] := [] | ((csz', n)::ls) := if csz < csz' then prune_active_bps_core ls else ((csz',n)::ls) meta def prune_active_bps (s : state) : vm state := do sz ← vm.call_stack_size, return {s with active_bps := prune_active_bps_core sz s.active_bps} meta def updt_csz (s : state) : vm state := do sz ← vm.call_stack_size, return {s with csz := sz} meta def init_transition (s : state) : vm state := do opts ← vm.get_options, if opts.get_bool `server ff then return {s with md := mode.done} else do bps ← vm.get_attribute `breakpoint, new_s ← return {s with fn_bps := bps}, if opts.get_bool `debugger.autorun ff then return {new_s with md := mode.run} else do vm.put_str "Lean debugger\n", show_curr_fn "debugging", vm.put_str "type 'help' for help\n", cmd_loop new_s [] meta def step_transition (s : state) : vm state := do sz ← vm.call_stack_size, if sz = s.csz then return s else do show_curr_fn "step", cmd_loop s ["s"] meta def bp_reached (s : state) : vm bool := (do fn ← vm.curr_fn, return $ s.fn_bps.any (Ξ» p, p.is_prefix_of fn)) <|> return ff meta def in_active_bps (s : state) : vm bool := do sz ← vm.call_stack_size, match s.active_bps with | [] := return ff | (csz, _)::_ := return (sz = csz) end meta def run_transition (s : state) : vm state := do b1 ← in_active_bps s, b2 ← bp_reached s, if b1 ∨ not b2 then return s else do show_curr_fn "breakpoint", fn ← vm.curr_fn, sz ← vm.call_stack_size, new_s ← return $ {s with active_bps := (sz, fn) :: s.active_bps}, cmd_loop new_s ["r"] meta def step_fn (s : state) : vm state := do s ← prune_active_bps s, if s.md = mode.init then do new_s ← init_transition s, updt_csz new_s else if s.md = mode.done then return s else if s.md = mode.step then do new_s ← step_transition s, updt_csz new_s else if s.md = mode.run then do new_s ← run_transition s, updt_csz new_s else return s @[vm_monitor] meta def monitor : vm_monitor state := { init := init_state, step := step_fn } end debugger
6d67f82e2a59dd85044cd5d4221552da584b1177
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/finset/intervals.lean
7bf4f99e59324170391f997e82c21ca4bbe93937
[ "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
7,439
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.finset.basic import data.multiset.intervals /-! # Intervals in β„• as finsets For now this only covers `Ico n m`, the "closed-open" interval containing `[n, ..., m-1]`. -/ namespace finset open multiset nat /-! ### intervals -/ /- Ico (a closed open interval) -/ variables {n m l : β„•} /-- `Ico n m` is the set of natural numbers `n ≀ k < m`. -/ def Ico (n m : β„•) : finset β„• := ⟨_, Ico.nodup n m⟩ namespace Ico @[simp] theorem val (n m : β„•) : (Ico n m).1 = multiset.Ico n m := rfl @[simp] theorem to_finset (n m : β„•) : (multiset.Ico n m).to_finset = Ico n m := (multiset.to_finset_eq _).symm theorem image_add (n m k : β„•) : (Ico n m).image ((+) k) = Ico (n + k) (m + k) := by simp [image, multiset.Ico.map_add] theorem image_sub (n m k : β„•) (h : k ≀ n) : (Ico n m).image (Ξ» x, x - k) = Ico (n - k) (m - k) := begin dsimp [image], rw [multiset.Ico.map_sub _ _ _ h, ←multiset.to_finset_eq], refl, end theorem zero_bot (n : β„•) : Ico 0 n = range n := eq_of_veq $ multiset.Ico.zero_bot _ @[simp] theorem card (n m : β„•) : (Ico n m).card = m - n := multiset.Ico.card _ _ @[simp] theorem mem {n m l : β„•} : l ∈ Ico n m ↔ n ≀ l ∧ l < m := multiset.Ico.mem theorem eq_empty_of_le {n m : β„•} (h : m ≀ n) : Ico n m = βˆ… := eq_of_veq $ multiset.Ico.eq_zero_of_le h @[simp] theorem self_eq_empty (n : β„•) : Ico n n = βˆ… := eq_empty_of_le $ le_refl n @[simp] theorem eq_empty_iff {n m : β„•} : Ico n m = βˆ… ↔ m ≀ n := iff.trans val_eq_zero.symm multiset.Ico.eq_zero_iff theorem subset_iff {m₁ n₁ mβ‚‚ nβ‚‚ : β„•} (hmn : m₁ < n₁) : Ico m₁ n₁ βŠ† Ico mβ‚‚ nβ‚‚ ↔ (mβ‚‚ ≀ m₁ ∧ n₁ ≀ nβ‚‚) := begin simp only [subset_iff, mem], refine ⟨λ h, ⟨_, _⟩, _⟩, { exact (h ⟨le_refl _, hmn⟩).1 }, { refine le_of_pred_lt (@h (pred n₁) ⟨le_pred_of_lt hmn, pred_lt _⟩).2, exact ne_of_gt (lt_of_le_of_lt (nat.zero_le m₁) hmn) }, { rintros ⟨hm, hn⟩ k ⟨hmk, hkn⟩, exact ⟨le_trans hm hmk, lt_of_lt_of_le hkn hn⟩ } end protected theorem subset {m₁ n₁ mβ‚‚ nβ‚‚ : β„•} (hmm : mβ‚‚ ≀ m₁) (hnn : n₁ ≀ nβ‚‚) : Ico m₁ n₁ βŠ† Ico mβ‚‚ nβ‚‚ := begin simp only [finset.subset_iff, Ico.mem], assume x hx, exact ⟨le_trans hmm hx.1, lt_of_lt_of_le hx.2 hnn⟩ end lemma union_consecutive {n m l : β„•} (hnm : n ≀ m) (hml : m ≀ l) : Ico n m βˆͺ Ico m l = Ico n l := by rw [← to_finset, ← to_finset, ← multiset.to_finset_add, multiset.Ico.add_consecutive hnm hml, to_finset] @[simp] lemma inter_consecutive (n m l : β„•) : Ico n m ∩ Ico m l = βˆ… := begin rw [← to_finset, ← to_finset, ← multiset.to_finset_inter, multiset.Ico.inter_consecutive], simp, end lemma disjoint_consecutive (n m l : β„•) : disjoint (Ico n m) (Ico m l) := le_of_eq $ inter_consecutive n m l @[simp] theorem succ_singleton (n : β„•) : Ico n (n+1) = {n} := eq_of_veq $ multiset.Ico.succ_singleton theorem succ_top {n m : β„•} (h : n ≀ m) : Ico n (m + 1) = insert m (Ico n m) := by rw [← to_finset, multiset.Ico.succ_top h, multiset.to_finset_cons, to_finset] theorem succ_top' {n m : β„•} (h : n < m) : Ico n m = insert (m - 1) (Ico n (m - 1)) := begin have w : m = m - 1 + 1 := (nat.sub_add_cancel (nat.one_le_of_lt h)).symm, conv { to_lhs, rw w }, rw succ_top, exact nat.le_pred_of_lt h end theorem insert_succ_bot {n m : β„•} (h : n < m) : insert n (Ico (n + 1) m) = Ico n m := by rw [eq_comm, ← to_finset, multiset.Ico.eq_cons h, multiset.to_finset_cons, to_finset] @[simp] theorem pred_singleton {m : β„•} (h : 0 < m) : Ico (m - 1) m = {m - 1} := eq_of_veq $ multiset.Ico.pred_singleton h @[simp] theorem not_mem_top {n m : β„•} : m βˆ‰ Ico n m := multiset.Ico.not_mem_top lemma filter_lt_of_top_le {n m l : β„•} (hml : m ≀ l) : (Ico n m).filter (Ξ» x, x < l) = Ico n m := eq_of_veq $ multiset.Ico.filter_lt_of_top_le hml lemma filter_lt_of_le_bot {n m l : β„•} (hln : l ≀ n) : (Ico n m).filter (Ξ» x, x < l) = βˆ… := eq_of_veq $ multiset.Ico.filter_lt_of_le_bot hln lemma filter_Ico_bot {n m : β„•} (hnm : n < m) : (Ico n m).filter (Ξ» x, x ≀ n) = {n} := eq_of_veq $ multiset.Ico.filter_le_of_bot hnm lemma filter_lt_of_ge {n m l : β„•} (hlm : l ≀ m) : (Ico n m).filter (Ξ» x, x < l) = Ico n l := eq_of_veq $ multiset.Ico.filter_lt_of_ge hlm @[simp] lemma filter_lt (n m l : β„•) : (Ico n m).filter (Ξ» x, x < l) = Ico n (min m l) := eq_of_veq $ multiset.Ico.filter_lt n m l lemma filter_le_of_le_bot {n m l : β„•} (hln : l ≀ n) : (Ico n m).filter (Ξ» x, l ≀ x) = Ico n m := eq_of_veq $ multiset.Ico.filter_le_of_le_bot hln lemma filter_le_of_top_le {n m l : β„•} (hml : m ≀ l) : (Ico n m).filter (Ξ» x, l ≀ x) = βˆ… := eq_of_veq $ multiset.Ico.filter_le_of_top_le hml lemma filter_le_of_le {n m l : β„•} (hnl : n ≀ l) : (Ico n m).filter (Ξ» x, l ≀ x) = Ico l m := eq_of_veq $ multiset.Ico.filter_le_of_le hnl @[simp] lemma filter_le (n m l : β„•) : (Ico n m).filter (Ξ» x, l ≀ x) = Ico (max n l) m := eq_of_veq $ multiset.Ico.filter_le n m l @[simp] lemma diff_left (l n m : β„•) : (Ico n m) \ (Ico n l) = Ico (max n l) m := by ext k; by_cases n ≀ k; simp [h, and_comm] @[simp] lemma diff_right (l n m : β„•) : (Ico n m) \ (Ico l m) = Ico n (min m l) := have βˆ€k, (k < m ∧ (l ≀ k β†’ m ≀ k)) ↔ (k < m ∧ k < l) := assume k, and_congr_right $ assume hk, by rw [← not_imp_not]; simp [hk], by ext k; by_cases n ≀ k; simp [h, this] lemma image_const_sub {k m n : β„•} (hkn : k ≀ n) : (Ico k m).image (Ξ» j, n - j) = Ico (n + 1 - m) (n + 1 - k) := begin rw [nat.sub_add_comm hkn], ext j, simp only [mem, mem_image, exists_prop, nat.lt_iff_add_one_le, add_le_add_iff_right], split, { rintros ⟨j, ⟨hjk, hjm⟩, rfl⟩, split, { simp only [← nat.add_sub_add_right n 1 j, nat.sub_le_sub_left, hjm] }, { exact nat.sub_le_sub_left _ hjk } }, { rintros ⟨hm, hk⟩, have hj : j ≀ n := le_trans hk (nat.sub_le_self _ _), refine ⟨n - j, ⟨_, _⟩, _⟩, { apply nat.le_sub_right_of_add_le, rwa nat.le_sub_left_iff_add_le hkn at hk }, { rwa [← nat.sub_add_comm hj, nat.sub_le_iff] }, { exact nat.sub_sub_self hj } } end end Ico lemma range_eq_Ico (n : β„•) : finset.range n = finset.Ico 0 n := by { ext i, simp } lemma range_image_pred_top_sub (n : β„•) : (finset.range n).image (Ξ» j, n - 1 - j) = finset.range n := begin cases n, { simp }, { simp [range_eq_Ico, Ico.image_const_sub] } end -- TODO We don't yet attempt to reproduce the entire interface for `Ico` for `Ico_β„€`. /-- `Ico_β„€ l u` is the set of integers `l ≀ k < u`. -/ def Ico_β„€ (l u : β„€) : finset β„€ := (finset.range (u - l).to_nat).map { to_fun := Ξ» n, n + l, inj' := Ξ» n m h, by simpa using h } @[simp] lemma Ico_β„€.mem {n m l : β„€} : l ∈ Ico_β„€ n m ↔ n ≀ l ∧ l < m := begin dsimp [Ico_β„€], simp only [int.lt_to_nat, exists_prop, mem_range, add_comm, function.embedding.coe_fn_mk, mem_map], split, { rintro ⟨a, ⟨h, rfl⟩⟩, exact ⟨int.le.intro rfl, lt_sub_iff_add_lt'.mp h⟩ }, { rintro ⟨h₁, hβ‚‚βŸ©, use (l - n).to_nat, split; simp [h₁, hβ‚‚], } end @[simp] lemma Ico_β„€.card (l u : β„€) : (Ico_β„€ l u).card = (u - l).to_nat := by simp [Ico_β„€] end finset
2451a309d2262990edd905d5ab8d18f315e8a7a6
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/analysis/special_functions/complex/log.lean
34f328cd867f0b42e4828195378596eeb52dfbe8
[ "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
13,633
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle SΓΆnne, Benjamin Davidson -/ import analysis.special_functions.complex.arg import analysis.special_functions.log import analysis.special_functions.trigonometric.inverse_deriv /-! # The complex `log` function Basic properties, relationship with `exp`, and differentiability. -/ noncomputable theory namespace complex open set filter open_locale real topological_space /-- Inverse of the `exp` function. Returns values such that `(log x).im > - Ο€` and `(log x).im ≀ Ο€`. `log 0 = 0`-/ @[pp_nodot] noncomputable def log (x : β„‚) : β„‚ := x.abs.log + arg x * I lemma log_re (x : β„‚) : x.log.re = x.abs.log := by simp [log] lemma log_im (x : β„‚) : x.log.im = x.arg := by simp [log] lemma neg_pi_lt_log_im (x : β„‚) : -Ο€ < (log x).im := by simp only [log_im, neg_pi_lt_arg] lemma log_im_le_pi (x : β„‚) : (log x).im ≀ Ο€ := by simp only [log_im, arg_le_pi] lemma exp_log {x : β„‚} (hx : x β‰  0) : exp (log x) = x := by rw [log, exp_add_mul_I, ← of_real_sin, sin_arg, ← of_real_cos, cos_arg hx, ← of_real_exp, real.exp_log (abs_pos.2 hx), mul_add, of_real_div, of_real_div, mul_div_cancel' _ (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), ← mul_assoc, mul_div_cancel' _ (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), re_add_im] @[simp] lemma range_exp : range exp = {0}ᢜ := set.ext $ Ξ» x, ⟨by { rintro ⟨x, rfl⟩, exact exp_ne_zero x }, Ξ» hx, ⟨log x, exp_log hx⟩⟩ lemma exp_inj_of_neg_pi_lt_of_le_pi {x y : β„‚} (hx₁ : -Ο€ < x.im) (hxβ‚‚ : x.im ≀ Ο€) (hy₁ : - Ο€ < y.im) (hyβ‚‚ : y.im ≀ Ο€) (hxy : exp x = exp y) : x = y := by rw [exp_eq_exp_re_mul_sin_add_cos, exp_eq_exp_re_mul_sin_add_cos y] at hxy; exact complex.ext (real.exp_injective $ by simpa [abs_mul, abs_cos_add_sin_mul_I] using congr_arg complex.abs hxy) (by simpa [(of_real_exp _).symm, - of_real_exp, arg_real_mul _ (real.exp_pos _), arg_cos_add_sin_mul_I hx₁ hxβ‚‚, arg_cos_add_sin_mul_I hy₁ hyβ‚‚] using congr_arg arg hxy) lemma log_exp {x : β„‚} (hx₁ : -Ο€ < x.im) (hxβ‚‚: x.im ≀ Ο€) : log (exp x) = x := exp_inj_of_neg_pi_lt_of_le_pi (by rw log_im; exact neg_pi_lt_arg _) (by rw log_im; exact arg_le_pi _) hx₁ hxβ‚‚ (by rw [exp_log (exp_ne_zero _)]) lemma of_real_log {x : ℝ} (hx : 0 ≀ x) : (x.log : β„‚) = log x := complex.ext (by rw [log_re, of_real_re, abs_of_nonneg hx]) (by rw [of_real_im, log_im, arg_of_real_of_nonneg hx]) lemma log_of_real_re (x : ℝ) : (log (x : β„‚)).re = real.log x := by simp [log_re] @[simp] lemma log_zero : log 0 = 0 := by simp [log] @[simp] lemma log_one : log 1 = 0 := by simp [log] lemma log_neg_one : log (-1) = Ο€ * I := by simp [log] lemma log_I : log I = Ο€ / 2 * I := by simp [log] lemma log_neg_I : log (-I) = -(Ο€ / 2) * I := by simp [log] lemma two_pi_I_ne_zero : (2 * Ο€ * I : β„‚) β‰  0 := by norm_num [real.pi_ne_zero, I_ne_zero] lemma exp_eq_one_iff {x : β„‚} : exp x = 1 ↔ βˆƒ n : β„€, x = n * ((2 * Ο€) * I) := have real.exp (x.re) * real.cos (x.im) = 1 β†’ real.cos x.im β‰  -1, from Ξ» h₁ hβ‚‚, begin rw [hβ‚‚, mul_neg_eq_neg_mul_symm, mul_one, neg_eq_iff_neg_eq] at h₁, have := real.exp_pos x.re, rw ← h₁ at this, exact absurd this (by norm_num) end, calc exp x = 1 ↔ (exp x).re = 1 ∧ (exp x).im = 0 : by simp [complex.ext_iff] ... ↔ real.cos x.im = 1 ∧ real.sin x.im = 0 ∧ x.re = 0 : begin rw exp_eq_exp_re_mul_sin_add_cos, simp [complex.ext_iff, cos_of_real_re, sin_of_real_re, exp_of_real_re, real.exp_ne_zero], split; finish [real.sin_eq_zero_iff_cos_eq] end ... ↔ (βˆƒ n : β„€, ↑n * (2 * Ο€) = x.im) ∧ (βˆƒ n : β„€, ↑n * Ο€ = x.im) ∧ x.re = 0 : by rw [real.sin_eq_zero_iff, real.cos_eq_one_iff] ... ↔ βˆƒ n : β„€, x = n * ((2 * Ο€) * I) : ⟨λ ⟨⟨n, hn⟩, ⟨m, hm⟩, h⟩, ⟨n, by simp [complex.ext_iff, hn.symm, h]⟩, Ξ» ⟨n, hn⟩, ⟨⟨n, by simp [hn]⟩, ⟨2 * n, by simp [hn, mul_comm, mul_assoc, mul_left_comm]⟩, by simp [hn]⟩⟩ lemma exp_eq_exp_iff_exp_sub_eq_one {x y : β„‚} : exp x = exp y ↔ exp (x - y) = 1 := by rw [exp_sub, div_eq_one_iff_eq (exp_ne_zero _)] lemma exp_eq_exp_iff_exists_int {x y : β„‚} : exp x = exp y ↔ βˆƒ n : β„€, x = y + n * ((2 * Ο€) * I) := by simp only [exp_eq_exp_iff_exp_sub_eq_one, exp_eq_one_iff, sub_eq_iff_eq_add'] @[simp] lemma countable_preimage_exp {s : set β„‚} : countable (exp ⁻¹' s) ↔ countable s := begin refine ⟨λ hs, _, Ξ» hs, _⟩, { refine ((hs.image exp).insert 0).mono _, rw [image_preimage_eq_inter_range, range_exp, ← diff_eq, ← union_singleton, diff_union_self], exact subset_union_left _ _ }, { rw ← bUnion_preimage_singleton, refine hs.bUnion (Ξ» z hz, _), rcases em (βˆƒ w, exp w = z) with ⟨w, rfl⟩|hne, { simp only [preimage, mem_singleton_iff, exp_eq_exp_iff_exists_int, set_of_exists], exact countable_Union (Ξ» m, countable_singleton _) }, { push_neg at hne, simp [preimage, hne] } } end alias countable_preimage_exp ↔ _ set.countable.preimage_cexp /-- `complex.exp` as a `local_homeomorph` with `source = {z | -Ο€ < im z < Ο€}` and `target = {z | 0 < re z} βˆͺ {z | im z β‰  0}`. This definition is used to prove that `complex.log` is complex differentiable at all points but the negative real semi-axis. -/ def exp_local_homeomorph : local_homeomorph β„‚ β„‚ := local_homeomorph.of_continuous_open { to_fun := exp, inv_fun := log, source := {z : β„‚ | z.im ∈ Ioo (- Ο€) Ο€}, target := {z : β„‚ | 0 < z.re} βˆͺ {z : β„‚ | z.im β‰  0}, map_source' := begin rintro ⟨x, y⟩ ⟨h₁ : -Ο€ < y, hβ‚‚ : y < Ο€βŸ©, refine (not_or_of_imp $ Ξ» hz, _).symm, obtain rfl : y = 0, { rw exp_im at hz, simpa [(real.exp_pos _).ne', real.sin_eq_zero_iff_of_lt_of_lt h₁ hβ‚‚] using hz }, rw [mem_set_of_eq, ← of_real_def, exp_of_real_re], exact real.exp_pos x end, map_target' := Ξ» z h, suffices 0 ≀ z.re ∨ z.im β‰  0, by simpa [log_im, neg_pi_lt_arg, (arg_le_pi _).lt_iff_ne, arg_eq_pi_iff, not_and_distrib], h.imp (Ξ» h, le_of_lt h) id, left_inv' := Ξ» x hx, log_exp hx.1 (le_of_lt hx.2), right_inv' := Ξ» x hx, exp_log $ by { rintro rfl, simpa [lt_irrefl] using hx } } continuous_exp.continuous_on is_open_map_exp (is_open_Ioo.preimage continuous_im) lemma has_strict_deriv_at_log {x : β„‚} (h : 0 < x.re ∨ x.im β‰  0) : has_strict_deriv_at log x⁻¹ x := have h0 : x β‰  0, by { rintro rfl, simpa [lt_irrefl] using h }, exp_local_homeomorph.has_strict_deriv_at_symm h h0 $ by simpa [exp_log h0] using has_strict_deriv_at_exp (log x) lemma has_strict_fderiv_at_log_real {x : β„‚} (h : 0 < x.re ∨ x.im β‰  0) : has_strict_fderiv_at log (x⁻¹ β€’ (1 : β„‚ β†’L[ℝ] β„‚)) x := (has_strict_deriv_at_log h).complex_to_real_fderiv lemma times_cont_diff_at_log {x : β„‚} (h : 0 < x.re ∨ x.im β‰  0) {n : with_top β„•} : times_cont_diff_at β„‚ n log x := exp_local_homeomorph.times_cont_diff_at_symm_deriv (exp_ne_zero $ log x) h (has_deriv_at_exp _) times_cont_diff_exp.times_cont_diff_at lemma tendsto_log_nhds_within_im_neg_of_re_neg_of_im_zero {z : β„‚} (hre : z.re < 0) (him : z.im = 0) : tendsto log (𝓝[{z : β„‚ | z.im < 0}] z) (𝓝 $ real.log (abs z) - Ο€ * I) := begin have := (continuous_of_real.continuous_at.comp_continuous_within_at (continuous_abs.continuous_within_at.log _)).tendsto.add (((continuous_of_real.tendsto _).comp $ tendsto_arg_nhds_within_im_neg_of_re_neg_of_im_zero hre him).mul tendsto_const_nhds), convert this, { simp [sub_eq_add_neg] }, { lift z to ℝ using him, simpa using hre.ne } end lemma continuous_within_at_log_of_re_neg_of_im_zero {z : β„‚} (hre : z.re < 0) (him : z.im = 0) : continuous_within_at log {z : β„‚ | 0 ≀ z.im} z := begin have := (continuous_of_real.continuous_at.comp_continuous_within_at (continuous_abs.continuous_within_at.log _)).tendsto.add ((continuous_of_real.continuous_at.comp_continuous_within_at $ continuous_within_at_arg_of_re_neg_of_im_zero hre him).mul tendsto_const_nhds), convert this, { lift z to ℝ using him, simpa using hre.ne } end lemma tendsto_log_nhds_within_im_nonneg_of_re_neg_of_im_zero {z : β„‚} (hre : z.re < 0) (him : z.im = 0) : tendsto log (𝓝[{z : β„‚ | 0 ≀ z.im}] z) (𝓝 $ real.log (abs z) + Ο€ * I) := by simpa only [log, arg_eq_pi_iff.2 ⟨hre, him⟩] using (continuous_within_at_log_of_re_neg_of_im_zero hre him).tendsto end complex section log_deriv open complex filter open_locale topological_space variables {Ξ± : Type*} lemma filter.tendsto.clog {l : filter Ξ±} {f : Ξ± β†’ β„‚} {x : β„‚} (h : tendsto f l (𝓝 x)) (hx : 0 < x.re ∨ x.im β‰  0) : tendsto (Ξ» t, log (f t)) l (𝓝 $ log x) := (has_strict_deriv_at_log hx).continuous_at.tendsto.comp h variables [topological_space Ξ±] lemma continuous_at.clog {f : Ξ± β†’ β„‚} {x : Ξ±} (h₁ : continuous_at f x) (hβ‚‚ : 0 < (f x).re ∨ (f x).im β‰  0) : continuous_at (Ξ» t, log (f t)) x := h₁.clog hβ‚‚ lemma continuous_within_at.clog {f : Ξ± β†’ β„‚} {s : set Ξ±} {x : Ξ±} (h₁ : continuous_within_at f s x) (hβ‚‚ : 0 < (f x).re ∨ (f x).im β‰  0) : continuous_within_at (Ξ» t, log (f t)) s x := h₁.clog hβ‚‚ lemma continuous_on.clog {f : Ξ± β†’ β„‚} {s : set Ξ±} (h₁ : continuous_on f s) (hβ‚‚ : βˆ€ x ∈ s, 0 < (f x).re ∨ (f x).im β‰  0) : continuous_on (Ξ» t, log (f t)) s := Ξ» x hx, (h₁ x hx).clog (hβ‚‚ x hx) lemma continuous.clog {f : Ξ± β†’ β„‚} (h₁ : continuous f) (hβ‚‚ : βˆ€ x, 0 < (f x).re ∨ (f x).im β‰  0) : continuous (Ξ» t, log (f t)) := continuous_iff_continuous_at.2 $ Ξ» x, h₁.continuous_at.clog (hβ‚‚ x) variables {E : Type*} [normed_group E] [normed_space β„‚ E] lemma has_strict_fderiv_at.clog {f : E β†’ β„‚} {f' : E β†’L[β„‚] β„‚} {x : E} (h₁ : has_strict_fderiv_at f f' x) (hβ‚‚ : 0 < (f x).re ∨ (f x).im β‰  0) : has_strict_fderiv_at (Ξ» t, log (f t)) ((f x)⁻¹ β€’ f') x := (has_strict_deriv_at_log hβ‚‚).comp_has_strict_fderiv_at x h₁ lemma has_strict_deriv_at.clog {f : β„‚ β†’ β„‚} {f' x : β„‚} (h₁ : has_strict_deriv_at f f' x) (hβ‚‚ : 0 < (f x).re ∨ (f x).im β‰  0) : has_strict_deriv_at (Ξ» t, log (f t)) (f' / f x) x := by { rw div_eq_inv_mul, exact (has_strict_deriv_at_log hβ‚‚).comp x h₁ } lemma has_strict_deriv_at.clog_real {f : ℝ β†’ β„‚} {x : ℝ} {f' : β„‚} (h₁ : has_strict_deriv_at f f' x) (hβ‚‚ : 0 < (f x).re ∨ (f x).im β‰  0) : has_strict_deriv_at (Ξ» t, log (f t)) (f' / f x) x := by simpa only [div_eq_inv_mul] using (has_strict_fderiv_at_log_real hβ‚‚).comp_has_strict_deriv_at x h₁ lemma has_fderiv_at.clog {f : E β†’ β„‚} {f' : E β†’L[β„‚] β„‚} {x : E} (h₁ : has_fderiv_at f f' x) (hβ‚‚ : 0 < (f x).re ∨ (f x).im β‰  0) : has_fderiv_at (Ξ» t, log (f t)) ((f x)⁻¹ β€’ f') x := (has_strict_deriv_at_log hβ‚‚).has_deriv_at.comp_has_fderiv_at x h₁ lemma has_deriv_at.clog {f : β„‚ β†’ β„‚} {f' x : β„‚} (h₁ : has_deriv_at f f' x) (hβ‚‚ : 0 < (f x).re ∨ (f x).im β‰  0) : has_deriv_at (Ξ» t, log (f t)) (f' / f x) x := by { rw div_eq_inv_mul, exact (has_strict_deriv_at_log hβ‚‚).has_deriv_at.comp x h₁ } lemma has_deriv_at.clog_real {f : ℝ β†’ β„‚} {x : ℝ} {f' : β„‚} (h₁ : has_deriv_at f f' x) (hβ‚‚ : 0 < (f x).re ∨ (f x).im β‰  0) : has_deriv_at (Ξ» t, log (f t)) (f' / f x) x := by simpa only [div_eq_inv_mul] using (has_strict_fderiv_at_log_real hβ‚‚).has_fderiv_at.comp_has_deriv_at x h₁ lemma differentiable_at.clog {f : E β†’ β„‚} {x : E} (h₁ : differentiable_at β„‚ f x) (hβ‚‚ : 0 < (f x).re ∨ (f x).im β‰  0) : differentiable_at β„‚ (Ξ» t, log (f t)) x := (h₁.has_fderiv_at.clog hβ‚‚).differentiable_at lemma has_fderiv_within_at.clog {f : E β†’ β„‚} {f' : E β†’L[β„‚] β„‚} {s : set E} {x : E} (h₁ : has_fderiv_within_at f f' s x) (hβ‚‚ : 0 < (f x).re ∨ (f x).im β‰  0) : has_fderiv_within_at (Ξ» t, log (f t)) ((f x)⁻¹ β€’ f') s x := (has_strict_deriv_at_log hβ‚‚).has_deriv_at.comp_has_fderiv_within_at x h₁ lemma has_deriv_within_at.clog {f : β„‚ β†’ β„‚} {f' x : β„‚} {s : set β„‚} (h₁ : has_deriv_within_at f f' s x) (hβ‚‚ : 0 < (f x).re ∨ (f x).im β‰  0) : has_deriv_within_at (Ξ» t, log (f t)) (f' / f x) s x := by { rw div_eq_inv_mul, exact (has_strict_deriv_at_log hβ‚‚).has_deriv_at.comp_has_deriv_within_at x h₁ } lemma has_deriv_within_at.clog_real {f : ℝ β†’ β„‚} {s : set ℝ} {x : ℝ} {f' : β„‚} (h₁ : has_deriv_within_at f f' s x) (hβ‚‚ : 0 < (f x).re ∨ (f x).im β‰  0) : has_deriv_within_at (Ξ» t, log (f t)) (f' / f x) s x := by simpa only [div_eq_inv_mul] using (has_strict_fderiv_at_log_real hβ‚‚).has_fderiv_at.comp_has_deriv_within_at x h₁ lemma differentiable_within_at.clog {f : E β†’ β„‚} {s : set E} {x : E} (h₁ : differentiable_within_at β„‚ f s x) (hβ‚‚ : 0 < (f x).re ∨ (f x).im β‰  0) : differentiable_within_at β„‚ (Ξ» t, log (f t)) s x := (h₁.has_fderiv_within_at.clog hβ‚‚).differentiable_within_at lemma differentiable_on.clog {f : E β†’ β„‚} {s : set E} (h₁ : differentiable_on β„‚ f s) (hβ‚‚ : βˆ€ x ∈ s, 0 < (f x).re ∨ (f x).im β‰  0) : differentiable_on β„‚ (Ξ» t, log (f t)) s := Ξ» x hx, (h₁ x hx).clog (hβ‚‚ x hx) lemma differentiable.clog {f : E β†’ β„‚} (h₁ : differentiable β„‚ f) (hβ‚‚ : βˆ€ x, 0 < (f x).re ∨ (f x).im β‰  0) : differentiable β„‚ (Ξ» t, log (f t)) := Ξ» x, (h₁ x).clog (hβ‚‚ x) end log_deriv
910f063333650023649df4f30acf76093f0ecc8c
88fb7558b0636ec6b181f2a548ac11ad3919f8a5
/tests/lean/1207.lean
c7836765ec63a79c309b7fae2e693ee75d8bcdbb
[ "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
480
lean
example : true := begin assertv H : true := (by trivial), exact H end example : true := begin assertv H : true := (by tactic.triv), exact H end meta example (h : tactic unit) : true := begin h, -- ERROR h should not be visible here trivial end example : false := begin assertv H : true := (by foo), -- ERROR exact sorry end constant P : Prop example (p : P) : true := begin assertv H : P := by do { p ← tactic.get_local `p, tactic.exact p }, trivial end
7f3eae5ab8ad92a0fb6f00f16e194692ef852de6
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/quaternion_auto.lean
a9d8d4699863ea3e084afff0d2d44f83c1c97164
[]
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
32,551
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.ring_exp import Mathlib.algebra.algebra.basic import Mathlib.algebra.opposites import Mathlib.data.equiv.ring import Mathlib.PostPort universes u_1 l namespace Mathlib /-! # Quaternions In this file we define quaternions `ℍ[R]` over a commutative ring `R`, and define some algebraic structures on `ℍ[R]`. ## Main definitions * `quaternion_algebra R a b`, `ℍ[R, a, b]` : [quaternion algebra](https://en.wikipedia.org/wiki/Quaternion_algebra) with coefficients `a`, `b` * `quaternion R`, `ℍ[R]` : the space of quaternions, a.k.a. `quaternion_algebra R (-1) (-1)`; * `quaternion.norm_sq` : square of the norm of a quaternion; * `quaternion.conj` : conjugate of a quaternion; We also define the following algebraic structures on `ℍ[R]`: * `ring ℍ[R, a, b]` and `algebra R ℍ[R, a, b]` : for any commutative ring `R`; * `ring ℍ[R]` and `algebra R ℍ[R]` : for any commutative ring `R`; * `domain ℍ[R]` : for a linear ordered commutative ring `R`; * `division_algebra ℍ[R]` : for a linear ordered field `R`. ## Notation The following notation is available with `open_locale quaternion`. * `ℍ[R, c₁, cβ‚‚]` : `quaternion_algebra R c₁ cβ‚‚` * `ℍ[R]` : quaternions over `R`. ## Implementation notes We define quaternions over any ring `R`, not just `ℝ` to be able to deal with, e.g., integer or rational quaternions without using real numbers. In particular, all definitions in this file are computable. ## Tags quaternion -/ /-- Quaternion algebra over a type with fixed coefficients $a=i^2$ and $b=j^2$. Implemented as a structure with four fields: `re`, `im_i`, `im_j`, and `im_k`. -/ structure quaternion_algebra (R : Type u_1) (a : R) (b : R) where re : R im_i : R im_j : R im_k : R namespace quaternion_algebra @[simp] theorem mk.eta {R : Type u_1} {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : mk (re a) (im_i a) (im_j a) (im_k a) = a := sorry protected instance has_coe_t {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : has_coe_t R (quaternion_algebra R c₁ cβ‚‚) := has_coe_t.mk fun (x : R) => mk x 0 0 0 @[simp] theorem coe_re {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (x : R) : re ↑x = x := rfl @[simp] theorem coe_im_i {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (x : R) : im_i ↑x = 0 := rfl @[simp] theorem coe_im_j {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (x : R) : im_j ↑x = 0 := rfl @[simp] theorem coe_im_k {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (x : R) : im_k ↑x = 0 := rfl theorem coe_injective {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : function.injective coe := fun (x y : R) (h : ↑x = ↑y) => congr_arg re h @[simp] theorem coe_inj {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} {x : R} {y : R} : ↑x = ↑y ↔ x = y := function.injective.eq_iff coe_injective @[simp] theorem has_zero_zero_im_j {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : im_j 0 = 0 := Eq.refl (im_j 0) @[simp] theorem coe_zero {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : ↑0 = 0 := rfl protected instance inhabited {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : Inhabited (quaternion_algebra R c₁ cβ‚‚) := { default := 0 } @[simp] theorem has_one_one_re {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : re 1 = 1 := Eq.refl (re 1) @[simp] theorem coe_one {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : ↑1 = 1 := rfl @[simp] theorem has_add_add_im_i {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) (b : quaternion_algebra R c₁ cβ‚‚) : im_i (a + b) = im_i a + im_i b := Eq.refl (im_i (a + b)) @[simp] theorem mk_add_mk {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a₁ : R) (aβ‚‚ : R) (a₃ : R) (aβ‚„ : R) (b₁ : R) (bβ‚‚ : R) (b₃ : R) (bβ‚„ : R) : mk a₁ aβ‚‚ a₃ aβ‚„ + mk b₁ bβ‚‚ b₃ bβ‚„ = mk (a₁ + b₁) (aβ‚‚ + bβ‚‚) (a₃ + b₃) (aβ‚„ + bβ‚„) := rfl protected instance has_neg {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : Neg (quaternion_algebra R c₁ cβ‚‚) := { neg := fun (a : quaternion_algebra R c₁ cβ‚‚) => mk (-re a) (-im_i a) (-im_j a) (-im_k a) } @[simp] theorem neg_mk {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a₁ : R) (aβ‚‚ : R) (a₃ : R) (aβ‚„ : R) : -mk a₁ aβ‚‚ a₃ aβ‚„ = mk (-a₁) (-aβ‚‚) (-a₃) (-aβ‚„) := rfl @[simp] theorem has_sub_sub_im_k {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) (b : quaternion_algebra R c₁ cβ‚‚) : im_k (a - b) = im_k a - im_k b := Eq.refl (im_k (a - b)) @[simp] theorem mk_sub_mk {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a₁ : R) (aβ‚‚ : R) (a₃ : R) (aβ‚„ : R) (b₁ : R) (bβ‚‚ : R) (b₃ : R) (bβ‚„ : R) : mk a₁ aβ‚‚ a₃ aβ‚„ - mk b₁ bβ‚‚ b₃ bβ‚„ = mk (a₁ - b₁) (aβ‚‚ - bβ‚‚) (a₃ - b₃) (aβ‚„ - bβ‚„) := rfl /-- Multiplication is given by * `1 * x = x * 1 = x`; * `i * i = c₁`; * `j * j = cβ‚‚`; * `i * j = k`, `j * i = -k`; * `k * k = -c₁ * cβ‚‚`; * `i * k = c₁ * j`, `k * i = `-c₁ * j`; * `j * k = -cβ‚‚ * i`, `k * j = cβ‚‚ * i`. -/ @[simp] theorem has_mul_mul_im_i {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) (b : quaternion_algebra R c₁ cβ‚‚) : im_i (a * b) = re a * im_i b + im_i a * re b - cβ‚‚ * im_j a * im_k b + cβ‚‚ * im_k a * im_j b := Eq.refl (im_i (a * b)) @[simp] theorem mk_mul_mk {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a₁ : R) (aβ‚‚ : R) (a₃ : R) (aβ‚„ : R) (b₁ : R) (bβ‚‚ : R) (b₃ : R) (bβ‚„ : R) : mk a₁ aβ‚‚ a₃ aβ‚„ * mk b₁ bβ‚‚ b₃ bβ‚„ = mk (a₁ * b₁ + c₁ * aβ‚‚ * bβ‚‚ + cβ‚‚ * a₃ * b₃ - c₁ * cβ‚‚ * aβ‚„ * bβ‚„) (a₁ * bβ‚‚ + aβ‚‚ * b₁ - cβ‚‚ * a₃ * bβ‚„ + cβ‚‚ * aβ‚„ * b₃) (a₁ * b₃ + c₁ * aβ‚‚ * bβ‚„ + a₃ * b₁ - c₁ * aβ‚„ * bβ‚‚) (a₁ * bβ‚„ + aβ‚‚ * b₃ - a₃ * bβ‚‚ + aβ‚„ * b₁) := rfl protected instance ring {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : ring (quaternion_algebra R c₁ cβ‚‚) := ring.mk Add.add sorry 0 sorry sorry Neg.neg Sub.sub sorry sorry Mul.mul sorry 1 sorry sorry sorry sorry protected instance algebra {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : algebra R (quaternion_algebra R c₁ cβ‚‚) := algebra.mk (ring_hom.mk coe sorry sorry sorry sorry) sorry sorry @[simp] theorem smul_re {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (r : R) (a : quaternion_algebra R c₁ cβ‚‚) : re (r β€’ a) = r β€’ re a := rfl @[simp] theorem smul_im_i {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (r : R) (a : quaternion_algebra R c₁ cβ‚‚) : im_i (r β€’ a) = r β€’ im_i a := rfl @[simp] theorem smul_im_j {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (r : R) (a : quaternion_algebra R c₁ cβ‚‚) : im_j (r β€’ a) = r β€’ im_j a := rfl @[simp] theorem smul_im_k {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (r : R) (a : quaternion_algebra R c₁ cβ‚‚) : im_k (r β€’ a) = r β€’ im_k a := rfl @[simp] theorem coe_add {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (x : R) (y : R) : ↑(x + y) = ↑x + ↑y := ring_hom.map_add (algebra_map R (quaternion_algebra R c₁ cβ‚‚)) x y @[simp] theorem coe_sub {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (x : R) (y : R) : ↑(x - y) = ↑x - ↑y := ring_hom.map_sub (algebra_map R (quaternion_algebra R c₁ cβ‚‚)) x y @[simp] theorem coe_neg {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (x : R) : ↑(-x) = -↑x := ring_hom.map_neg (algebra_map R (quaternion_algebra R c₁ cβ‚‚)) x @[simp] theorem coe_mul {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (x : R) (y : R) : ↑(x * y) = ↑x * ↑y := ring_hom.map_mul (algebra_map R (quaternion_algebra R c₁ cβ‚‚)) x y theorem coe_commutes {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (r : R) (a : quaternion_algebra R c₁ cβ‚‚) : ↑r * a = a * ↑r := algebra.commutes r a theorem coe_commute {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (r : R) (a : quaternion_algebra R c₁ cβ‚‚) : commute (↑r) a := coe_commutes r a theorem coe_mul_eq_smul {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (r : R) (a : quaternion_algebra R c₁ cβ‚‚) : ↑r * a = r β€’ a := Eq.symm (algebra.smul_def r a) theorem mul_coe_eq_smul {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (r : R) (a : quaternion_algebra R c₁ cβ‚‚) : a * ↑r = r β€’ a := eq.mpr (id (Eq._oldrec (Eq.refl (a * ↑r = r β€’ a)) (Eq.symm (coe_commutes r a)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑r * a = r β€’ a)) (coe_mul_eq_smul r a))) (Eq.refl (r β€’ a))) @[simp] theorem coe_algebra_map {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : ⇑(algebra_map R (quaternion_algebra R c₁ cβ‚‚)) = coe := rfl theorem smul_coe {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (x : R) (y : R) : x β€’ ↑y = ↑(x * y) := eq.mpr (id (Eq._oldrec (Eq.refl (x β€’ ↑y = ↑(x * y))) (coe_mul x y))) (eq.mpr (id (Eq._oldrec (Eq.refl (x β€’ ↑y = ↑x * ↑y)) (coe_mul_eq_smul x ↑y))) (Eq.refl (x β€’ ↑y))) /-- Quaternion conjugate. -/ def conj {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : linear_equiv R (quaternion_algebra R c₁ cβ‚‚) (quaternion_algebra R c₁ cβ‚‚) := linear_equiv.of_involutive (linear_map.mk (fun (a : quaternion_algebra R c₁ cβ‚‚) => mk (re a) (-im_i a) (-im_j a) (-im_k a)) sorry sorry) sorry @[simp] theorem re_conj {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : re (coe_fn conj a) = re a := rfl @[simp] theorem im_i_conj {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : im_i (coe_fn conj a) = -im_i a := rfl @[simp] theorem im_j_conj {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : im_j (coe_fn conj a) = -im_j a := rfl @[simp] theorem im_k_conj {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : im_k (coe_fn conj a) = -im_k a := rfl @[simp] theorem conj_conj {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : coe_fn conj (coe_fn conj a) = a := ext (coe_fn conj (coe_fn conj a)) a rfl (neg_neg (im_i a)) (neg_neg (im_j a)) (neg_neg (im_k a)) theorem conj_add {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) (b : quaternion_algebra R c₁ cβ‚‚) : coe_fn conj (a + b) = coe_fn conj a + coe_fn conj b := linear_equiv.map_add conj a b @[simp] theorem conj_mul {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) (b : quaternion_algebra R c₁ cβ‚‚) : coe_fn conj (a * b) = coe_fn conj b * coe_fn conj a := sorry theorem conj_conj_mul {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) (b : quaternion_algebra R c₁ cβ‚‚) : coe_fn conj (coe_fn conj a * b) = coe_fn conj b * a := eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn conj (coe_fn conj a * b) = coe_fn conj b * a)) (conj_mul (coe_fn conj a) b))) (eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn conj b * coe_fn conj (coe_fn conj a) = coe_fn conj b * a)) (conj_conj a))) (Eq.refl (coe_fn conj b * a))) theorem conj_mul_conj {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) (b : quaternion_algebra R c₁ cβ‚‚) : coe_fn conj (a * coe_fn conj b) = b * coe_fn conj a := eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn conj (a * coe_fn conj b) = b * coe_fn conj a)) (conj_mul a (coe_fn conj b)))) (eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn conj (coe_fn conj b) * coe_fn conj a = b * coe_fn conj a)) (conj_conj b))) (Eq.refl (b * coe_fn conj a))) theorem self_add_conj' {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : a + coe_fn conj a = ↑(bit0 1 * re a) := sorry theorem self_add_conj {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : a + coe_fn conj a = bit0 1 * ↑(re a) := sorry theorem conj_add_self' {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : coe_fn conj a + a = ↑(bit0 1 * re a) := eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn conj a + a = ↑(bit0 1 * re a))) (add_comm (coe_fn conj a) a))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + coe_fn conj a = ↑(bit0 1 * re a))) (self_add_conj' a))) (Eq.refl ↑(bit0 1 * re a))) theorem conj_add_self {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : coe_fn conj a + a = bit0 1 * ↑(re a) := eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn conj a + a = bit0 1 * ↑(re a))) (add_comm (coe_fn conj a) a))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + coe_fn conj a = bit0 1 * ↑(re a))) (self_add_conj a))) (Eq.refl (bit0 1 * ↑(re a)))) theorem conj_eq_two_re_sub {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : coe_fn conj a = ↑(bit0 1 * re a) - a := iff.mpr eq_sub_iff_add_eq (conj_add_self' a) theorem commute_conj_self {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : commute (coe_fn conj a) a := eq.mpr (id (Eq._oldrec (Eq.refl (commute (coe_fn conj a) a)) (conj_eq_two_re_sub a))) (commute.sub_left (coe_commute (bit0 1 * re a) a) (commute.refl a)) theorem commute_self_conj {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : commute a (coe_fn conj a) := commute.symm (commute_conj_self a) theorem commute_conj_conj {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} {a : quaternion_algebra R c₁ cβ‚‚} {b : quaternion_algebra R c₁ cβ‚‚} (h : commute a b) : commute (coe_fn conj a) (coe_fn conj b) := sorry @[simp] theorem conj_coe {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (x : R) : coe_fn conj ↑x = ↑x := sorry theorem conj_smul {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (r : R) (a : quaternion_algebra R c₁ cβ‚‚) : coe_fn conj (r β€’ a) = r β€’ coe_fn conj a := linear_equiv.map_smul conj r a @[simp] theorem conj_one {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : coe_fn conj 1 = 1 := conj_coe 1 theorem eq_re_of_eq_coe {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} {a : quaternion_algebra R c₁ cβ‚‚} {x : R} (h : a = ↑x) : a = ↑(re a) := eq.mpr (id (Eq._oldrec (Eq.refl (a = ↑(re a))) h)) (eq.mpr (id (Eq._oldrec (Eq.refl (↑x = ↑(re ↑x))) (coe_re x))) (Eq.refl ↑x)) theorem eq_re_iff_mem_range_coe {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} {a : quaternion_algebra R c₁ cβ‚‚} : a = ↑(re a) ↔ a ∈ set.range coe := sorry @[simp] theorem conj_fixed {R : Type u_1} [comm_ring R] [no_zero_divisors R] [char_zero R] {c₁ : R} {cβ‚‚ : R} {a : quaternion_algebra R c₁ cβ‚‚} : coe_fn conj a = a ↔ a = ↑(re a) := sorry -- Can't use `rw ← conj_fixed` in the proof without additional assumptions theorem conj_mul_eq_coe {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : coe_fn conj a * a = ↑(re (coe_fn conj a * a)) := sorry theorem mul_conj_eq_coe {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : a * coe_fn conj a = ↑(re (a * coe_fn conj a)) := eq.mpr (id (Eq._oldrec (Eq.refl (a * coe_fn conj a = ↑(re (a * coe_fn conj a)))) (commute.eq (commute_self_conj a)))) (conj_mul_eq_coe a) theorem conj_zero {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : coe_fn conj 0 = 0 := linear_equiv.map_zero conj theorem conj_neg {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : coe_fn conj (-a) = -coe_fn conj a := linear_equiv.map_neg conj a theorem conj_sub {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) (b : quaternion_algebra R c₁ cβ‚‚) : coe_fn conj (a - b) = coe_fn conj a - coe_fn conj b := linear_equiv.map_sub conj a b protected instance star_ring {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : star_ring (quaternion_algebra R c₁ cβ‚‚) := star_ring.mk conj_add @[simp] theorem star_def {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} (a : quaternion_algebra R c₁ cβ‚‚) : star a = coe_fn conj a := rfl /-- Quaternion conjugate as an `alg_equiv` to the opposite ring. -/ def conj_alg_equiv {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : alg_equiv R (quaternion_algebra R c₁ cβ‚‚) (quaternion_algebra R c₁ cβ‚‚α΅’α΅–) := alg_equiv.mk (opposite.op ∘ ⇑conj) (⇑conj ∘ opposite.unop) sorry sorry sorry sorry sorry @[simp] theorem coe_conj_alg_equiv {R : Type u_1} [comm_ring R] {c₁ : R} {cβ‚‚ : R} : ⇑conj_alg_equiv = opposite.op ∘ ⇑conj := rfl end quaternion_algebra /-- Space of quaternions over a type. Implemented as a structure with four fields: `re`, `im_i`, `im_j`, and `im_k`. -/ def quaternion (R : Type u_1) [HasOne R] [Neg R] := quaternion_algebra R (-1) (-1) namespace quaternion protected instance has_coe_t {R : Type u_1} [comm_ring R] : has_coe_t R (quaternion R) := quaternion_algebra.has_coe_t protected instance ring {R : Type u_1} [comm_ring R] : ring (quaternion R) := quaternion_algebra.ring protected instance inhabited {R : Type u_1} [comm_ring R] : Inhabited (quaternion R) := quaternion_algebra.inhabited protected instance algebra {R : Type u_1} [comm_ring R] : algebra R (quaternion R) := quaternion_algebra.algebra protected instance star_ring {R : Type u_1} [comm_ring R] : star_ring (quaternion R) := quaternion_algebra.star_ring theorem ext {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : re a = re b β†’ im_i a = im_i b β†’ im_j a = im_j b β†’ im_k a = im_k b β†’ a = b := quaternion_algebra.ext a b theorem ext_iff {R : Type u_1} [comm_ring R] {a : quaternion R} {b : quaternion R} : a = b ↔ re a = re b ∧ im_i a = im_i b ∧ im_j a = im_j b ∧ im_k a = im_k b := quaternion_algebra.ext_iff a b @[simp] theorem coe_re {R : Type u_1} [comm_ring R] (x : R) : re ↑x = x := rfl @[simp] theorem coe_im_i {R : Type u_1} [comm_ring R] (x : R) : im_i ↑x = 0 := rfl @[simp] theorem coe_im_j {R : Type u_1} [comm_ring R] (x : R) : im_j ↑x = 0 := rfl @[simp] theorem coe_im_k {R : Type u_1} [comm_ring R] (x : R) : im_k ↑x = 0 := rfl @[simp] theorem zero_re {R : Type u_1} [comm_ring R] : re 0 = 0 := rfl @[simp] theorem zero_im_i {R : Type u_1} [comm_ring R] : im_i 0 = 0 := rfl @[simp] theorem zero_im_j {R : Type u_1} [comm_ring R] : im_j 0 = 0 := rfl @[simp] theorem zero_im_k {R : Type u_1} [comm_ring R] : im_k 0 = 0 := rfl @[simp] theorem coe_zero {R : Type u_1} [comm_ring R] : ↑0 = 0 := rfl @[simp] theorem one_re {R : Type u_1} [comm_ring R] : re 1 = 1 := rfl @[simp] theorem one_im_i {R : Type u_1} [comm_ring R] : im_i 1 = 0 := rfl @[simp] theorem one_im_j {R : Type u_1} [comm_ring R] : im_j 1 = 0 := rfl @[simp] theorem one_im_k {R : Type u_1} [comm_ring R] : im_k 1 = 0 := rfl @[simp] theorem coe_one {R : Type u_1} [comm_ring R] : ↑1 = 1 := rfl @[simp] theorem add_re {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : re (a + b) = re a + re b := rfl @[simp] theorem add_im_i {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : im_i (a + b) = im_i a + im_i b := rfl @[simp] theorem add_im_j {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : im_j (a + b) = im_j a + im_j b := rfl @[simp] theorem add_im_k {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : im_k (a + b) = im_k a + im_k b := rfl @[simp] theorem coe_add {R : Type u_1} [comm_ring R] (x : R) (y : R) : ↑(x + y) = ↑x + ↑y := quaternion_algebra.coe_add x y @[simp] theorem neg_re {R : Type u_1} [comm_ring R] (a : quaternion R) : re (-a) = -re a := rfl @[simp] theorem neg_im_i {R : Type u_1} [comm_ring R] (a : quaternion R) : im_i (-a) = -im_i a := rfl @[simp] theorem neg_im_j {R : Type u_1} [comm_ring R] (a : quaternion R) : im_j (-a) = -im_j a := rfl @[simp] theorem neg_im_k {R : Type u_1} [comm_ring R] (a : quaternion R) : im_k (-a) = -im_k a := rfl @[simp] theorem coe_neg {R : Type u_1} [comm_ring R] (x : R) : ↑(-x) = -↑x := quaternion_algebra.coe_neg x @[simp] theorem sub_re {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : re (a - b) = re a - re b := rfl @[simp] theorem sub_im_i {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : im_i (a - b) = im_i a - im_i b := rfl @[simp] theorem sub_im_j {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : im_j (a - b) = im_j a - im_j b := rfl @[simp] theorem sub_im_k {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : im_k (a - b) = im_k a - im_k b := rfl @[simp] theorem coe_sub {R : Type u_1} [comm_ring R] (x : R) (y : R) : ↑(x - y) = ↑x - ↑y := quaternion_algebra.coe_sub x y @[simp] theorem mul_re {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : re (a * b) = re a * re b - im_i a * im_i b - im_j a * im_j b - im_k a * im_k b := sorry @[simp] theorem mul_im_i {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : im_i (a * b) = re a * im_i b + im_i a * re b + im_j a * im_k b - im_k a * im_j b := sorry @[simp] theorem mul_im_j {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : im_j (a * b) = re a * im_j b - im_i a * im_k b + im_j a * re b + im_k a * im_i b := sorry @[simp] theorem mul_im_k {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : im_k (a * b) = re a * im_k b + im_i a * im_j b - im_j a * im_i b + im_k a * re b := sorry @[simp] theorem coe_mul {R : Type u_1} [comm_ring R] (x : R) (y : R) : ↑(x * y) = ↑x * ↑y := quaternion_algebra.coe_mul x y theorem coe_injective {R : Type u_1} [comm_ring R] : function.injective coe := quaternion_algebra.coe_injective @[simp] theorem coe_inj {R : Type u_1} [comm_ring R] {x : R} {y : R} : ↑x = ↑y ↔ x = y := function.injective.eq_iff coe_injective @[simp] theorem smul_re {R : Type u_1} [comm_ring R] (r : R) (a : quaternion R) : re (r β€’ a) = r β€’ re a := rfl @[simp] theorem smul_im_i {R : Type u_1} [comm_ring R] (r : R) (a : quaternion R) : im_i (r β€’ a) = r β€’ im_i a := rfl @[simp] theorem smul_im_j {R : Type u_1} [comm_ring R] (r : R) (a : quaternion R) : im_j (r β€’ a) = r β€’ im_j a := rfl @[simp] theorem smul_im_k {R : Type u_1} [comm_ring R] (r : R) (a : quaternion R) : im_k (r β€’ a) = r β€’ im_k a := rfl theorem coe_commutes {R : Type u_1} [comm_ring R] (r : R) (a : quaternion R) : ↑r * a = a * ↑r := quaternion_algebra.coe_commutes r a theorem coe_commute {R : Type u_1} [comm_ring R] (r : R) (a : quaternion R) : commute (↑r) a := quaternion_algebra.coe_commute r a theorem coe_mul_eq_smul {R : Type u_1} [comm_ring R] (r : R) (a : quaternion R) : ↑r * a = r β€’ a := quaternion_algebra.coe_mul_eq_smul r a theorem mul_coe_eq_smul {R : Type u_1} [comm_ring R] (r : R) (a : quaternion R) : a * ↑r = r β€’ a := quaternion_algebra.mul_coe_eq_smul r a @[simp] theorem algebra_map_def {R : Type u_1} [comm_ring R] : ⇑(algebra_map R (quaternion R)) = coe := rfl theorem smul_coe {R : Type u_1} [comm_ring R] (x : R) (y : R) : x β€’ ↑y = ↑(x * y) := quaternion_algebra.smul_coe x y /-- Quaternion conjugate. -/ def conj {R : Type u_1} [comm_ring R] : linear_equiv R (quaternion R) (quaternion R) := quaternion_algebra.conj @[simp] theorem conj_re {R : Type u_1} [comm_ring R] (a : quaternion R) : re (coe_fn conj a) = re a := rfl @[simp] theorem conj_im_i {R : Type u_1} [comm_ring R] (a : quaternion R) : im_i (coe_fn conj a) = -im_i a := rfl @[simp] theorem conj_im_j {R : Type u_1} [comm_ring R] (a : quaternion R) : im_j (coe_fn conj a) = -im_j a := rfl @[simp] theorem conj_im_k {R : Type u_1} [comm_ring R] (a : quaternion R) : im_k (coe_fn conj a) = -im_k a := rfl @[simp] theorem conj_conj {R : Type u_1} [comm_ring R] (a : quaternion R) : coe_fn conj (coe_fn conj a) = a := quaternion_algebra.conj_conj a @[simp] theorem conj_add {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : coe_fn conj (a + b) = coe_fn conj a + coe_fn conj b := quaternion_algebra.conj_add a b @[simp] theorem conj_mul {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : coe_fn conj (a * b) = coe_fn conj b * coe_fn conj a := quaternion_algebra.conj_mul a b theorem conj_conj_mul {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : coe_fn conj (coe_fn conj a * b) = coe_fn conj b * a := quaternion_algebra.conj_conj_mul a b theorem conj_mul_conj {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : coe_fn conj (a * coe_fn conj b) = b * coe_fn conj a := quaternion_algebra.conj_mul_conj a b theorem self_add_conj' {R : Type u_1} [comm_ring R] (a : quaternion R) : a + coe_fn conj a = ↑(bit0 1 * re a) := quaternion_algebra.self_add_conj' a theorem self_add_conj {R : Type u_1} [comm_ring R] (a : quaternion R) : a + coe_fn conj a = bit0 1 * ↑(re a) := quaternion_algebra.self_add_conj a theorem conj_add_self' {R : Type u_1} [comm_ring R] (a : quaternion R) : coe_fn conj a + a = ↑(bit0 1 * re a) := quaternion_algebra.conj_add_self' a theorem conj_add_self {R : Type u_1} [comm_ring R] (a : quaternion R) : coe_fn conj a + a = bit0 1 * ↑(re a) := quaternion_algebra.conj_add_self a theorem conj_eq_two_re_sub {R : Type u_1} [comm_ring R] (a : quaternion R) : coe_fn conj a = ↑(bit0 1 * re a) - a := quaternion_algebra.conj_eq_two_re_sub a theorem commute_conj_self {R : Type u_1} [comm_ring R] (a : quaternion R) : commute (coe_fn conj a) a := quaternion_algebra.commute_conj_self a theorem commute_self_conj {R : Type u_1} [comm_ring R] (a : quaternion R) : commute a (coe_fn conj a) := quaternion_algebra.commute_self_conj a theorem commute_conj_conj {R : Type u_1} [comm_ring R] {a : quaternion R} {b : quaternion R} (h : commute a b) : commute (coe_fn conj a) (coe_fn conj b) := quaternion_algebra.commute_conj_conj h theorem Mathlib.commute.quaternion_conj {R : Type u_1} [comm_ring R] {a : quaternion R} {b : quaternion R} (h : commute a b) : commute (coe_fn conj a) (coe_fn conj b) := commute_conj_conj @[simp] theorem conj_coe {R : Type u_1} [comm_ring R] (x : R) : coe_fn conj ↑x = ↑x := quaternion_algebra.conj_coe x @[simp] theorem conj_smul {R : Type u_1} [comm_ring R] (r : R) (a : quaternion R) : coe_fn conj (r β€’ a) = r β€’ coe_fn conj a := quaternion_algebra.conj_smul r a @[simp] theorem conj_one {R : Type u_1} [comm_ring R] : coe_fn conj 1 = 1 := conj_coe 1 theorem eq_re_of_eq_coe {R : Type u_1} [comm_ring R] {a : quaternion R} {x : R} (h : a = ↑x) : a = ↑(re a) := quaternion_algebra.eq_re_of_eq_coe h theorem eq_re_iff_mem_range_coe {R : Type u_1} [comm_ring R] {a : quaternion R} : a = ↑(re a) ↔ a ∈ set.range coe := quaternion_algebra.eq_re_iff_mem_range_coe @[simp] theorem conj_fixed {R : Type u_1} [comm_ring R] [no_zero_divisors R] [char_zero R] {a : quaternion R} : coe_fn conj a = a ↔ a = ↑(re a) := quaternion_algebra.conj_fixed theorem conj_mul_eq_coe {R : Type u_1} [comm_ring R] (a : quaternion R) : coe_fn conj a * a = ↑(re (coe_fn conj a * a)) := quaternion_algebra.conj_mul_eq_coe a theorem mul_conj_eq_coe {R : Type u_1} [comm_ring R] (a : quaternion R) : a * coe_fn conj a = ↑(re (a * coe_fn conj a)) := quaternion_algebra.mul_conj_eq_coe a @[simp] theorem conj_zero {R : Type u_1} [comm_ring R] : coe_fn conj 0 = 0 := quaternion_algebra.conj_zero @[simp] theorem conj_neg {R : Type u_1} [comm_ring R] (a : quaternion R) : coe_fn conj (-a) = -coe_fn conj a := quaternion_algebra.conj_neg a @[simp] theorem conj_sub {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : coe_fn conj (a - b) = coe_fn conj a - coe_fn conj b := quaternion_algebra.conj_sub a b /-- Quaternion conjugate as an `alg_equiv` to the opposite ring. -/ def conj_alg_equiv {R : Type u_1} [comm_ring R] : alg_equiv R (quaternion R) (quaternion Rα΅’α΅–) := quaternion_algebra.conj_alg_equiv @[simp] theorem coe_conj_alg_equiv {R : Type u_1} [comm_ring R] : ⇑conj_alg_equiv = opposite.op ∘ ⇑conj := rfl /-- Square of the norm. -/ def norm_sq {R : Type u_1} [comm_ring R] : monoid_with_zero_hom (quaternion R) R := monoid_with_zero_hom.mk (fun (a : quaternion R) => re (a * coe_fn conj a)) sorry sorry sorry theorem norm_sq_def {R : Type u_1} [comm_ring R] (a : quaternion R) : coe_fn norm_sq a = re (a * coe_fn conj a) := rfl theorem norm_sq_def' {R : Type u_1} [comm_ring R] (a : quaternion R) : coe_fn norm_sq a = re a ^ bit0 1 + im_i a ^ bit0 1 + im_j a ^ bit0 1 + im_k a ^ bit0 1 := sorry theorem norm_sq_coe {R : Type u_1} [comm_ring R] (x : R) : coe_fn norm_sq ↑x = x ^ bit0 1 := sorry @[simp] theorem norm_sq_neg {R : Type u_1} [comm_ring R] (a : quaternion R) : coe_fn norm_sq (-a) = coe_fn norm_sq a := sorry theorem self_mul_conj {R : Type u_1} [comm_ring R] (a : quaternion R) : a * coe_fn conj a = ↑(coe_fn norm_sq a) := eq.mpr (id (Eq._oldrec (Eq.refl (a * coe_fn conj a = ↑(coe_fn norm_sq a))) (mul_conj_eq_coe a))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑(re (a * coe_fn conj a)) = ↑(coe_fn norm_sq a))) (norm_sq_def a))) (Eq.refl ↑(re (a * coe_fn conj a)))) theorem conj_mul_self {R : Type u_1} [comm_ring R] (a : quaternion R) : coe_fn conj a * a = ↑(coe_fn norm_sq a) := sorry theorem coe_norm_sq_add {R : Type u_1} [comm_ring R] (a : quaternion R) (b : quaternion R) : ↑(coe_fn norm_sq (a + b)) = ↑(coe_fn norm_sq a) + a * coe_fn conj b + b * coe_fn conj a + ↑(coe_fn norm_sq b) := sorry end quaternion namespace quaternion @[simp] theorem norm_sq_eq_zero {R : Type u_1} [linear_ordered_comm_ring R] {a : quaternion R} : coe_fn norm_sq a = 0 ↔ a = 0 := sorry theorem norm_sq_ne_zero {R : Type u_1} [linear_ordered_comm_ring R] {a : quaternion R} : coe_fn norm_sq a β‰  0 ↔ a β‰  0 := not_congr norm_sq_eq_zero @[simp] theorem norm_sq_nonneg {R : Type u_1} [linear_ordered_comm_ring R] {a : quaternion R} : 0 ≀ coe_fn norm_sq a := eq.mpr (id (Eq._oldrec (Eq.refl (0 ≀ coe_fn norm_sq a)) (norm_sq_def' a))) (add_nonneg (add_nonneg (add_nonneg (pow_two_nonneg (re a)) (pow_two_nonneg (im_i a))) (pow_two_nonneg (im_j a))) (pow_two_nonneg (im_k a))) @[simp] theorem norm_sq_le_zero {R : Type u_1} [linear_ordered_comm_ring R] {a : quaternion R} : coe_fn norm_sq a ≀ 0 ↔ a = 0 := sorry protected instance domain {R : Type u_1} [linear_ordered_comm_ring R] : domain (quaternion R) := domain.mk ring.add sorry ring.zero sorry sorry ring.neg ring.sub sorry sorry ring.mul sorry ring.one sorry sorry sorry sorry sorry sorry theorem has_inv_inv {R : Type u_1} [linear_ordered_field R] (a : quaternion R) : a⁻¹ = coe_fn norm_sq a⁻¹ β€’ coe_fn conj a := Eq.refl (a⁻¹) protected instance division_ring {R : Type u_1} [linear_ordered_field R] : division_ring (quaternion R) := division_ring.mk domain.add sorry domain.zero sorry sorry domain.neg domain.sub sorry sorry domain.mul sorry domain.one sorry sorry sorry sorry has_inv.inv (div_inv_monoid.div._default domain.mul sorry domain.one sorry sorry has_inv.inv) sorry sorry sorry @[simp] theorem norm_sq_inv {R : Type u_1} [linear_ordered_field R] (a : quaternion R) : coe_fn norm_sq (a⁻¹) = (coe_fn norm_sq a⁻¹) := monoid_with_zero_hom.map_inv' norm_sq a @[simp] theorem norm_sq_div {R : Type u_1} [linear_ordered_field R] (a : quaternion R) (b : quaternion R) : coe_fn norm_sq (a / b) = coe_fn norm_sq a / coe_fn norm_sq b := monoid_with_zero_hom.map_div norm_sq a b end Mathlib
5957b6d314dc70a1a2564894fc0495f20f06698a
a25cc44b447f2fbde61e4b848318762eb892f2bf
/src/sort_rank.lean
6ea45abfd6afbddf1bc0e202029d64d8b7325d2e
[]
no_license
NeilStrickland/itloc
20f9b9bcaa4d5840f7f32e84108174b294d84b1a
5b13b5b418766d10926b983eb3dd2ac42abf63d8
refs/heads/master
1,592,747,030,159
1,578,138,004,000
1,578,138,004,000
197,470,135
2
0
null
null
null
null
UTF-8
Lean
false
false
21,374
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland This file formalises the fact that any finite, linearly ordered set of size n has a unique order-isomorphism with the set fin n = {0,...,n-1}. -/ import data.finset data.fintype import fin_extra open list variables {Ξ± : Type*} namespace list /- If we have a list l (of length n) and a natural number i, we might want to refer to the i'th entry in the list, but then we have to decide what to do about the possibility that i is too large so that there is no i'th entry. We always use 0-based indexing so the i'th entry is defined for i < n but not for i β‰₯ n. The standard library defines (l.nth i) to be of type (option Ξ±), and to have value (some x) if i < n and x is the i'th entry, but to have value (none) if i β‰₯ n. The standard library also defines (l.nth_le i h) to be x if h is a proof of i < n. Here we just repackage this slightly: the pair ⟨i,h⟩ gives a term of type (fin n), and we define l.fin_nth ⟨i,h⟩ to be x again. -/ def fin_nth (l : list Ξ±) (i : fin l.length) : Ξ± := l.nth_le i.val i.is_lt /- An obvious lemma about the behaviour of fin_nth. -/ lemma fin_nth_cons (a : Ξ±) (l : list Ξ±) (i : fin l.length) : (list.cons a l).fin_nth i.succ = l.fin_nth i := begin cases i,dsimp[fin.succ,fin_nth],refl, end /- If R is a relation on Ξ± and l is a list, we define (pairwise_nth R l) to be true if all entries in l are R-related to all strictly later entries. For example, if R is an order relation then this just means that the list is sorted, and if R is the relation x β‰  y then this just means that l has no duplicates. Here we define pairwise_nth in the obvious way using indices. We then prove that our pairwise_nth is equivalent to the definition of pairwise in the standard library, which is formulated by structural induction rather than using indices. -/ def pairwise_nthβ‚€ (R : Ξ± β†’ Ξ± β†’ Prop) (l : list Ξ±) := βˆ€ (i j : fin l.length), i < j β†’ R (l.fin_nth i) (l.fin_nth j) def pairwise_nth (R : Ξ± β†’ Ξ± β†’ Prop) (l : list Ξ±) := βˆ€ {i j : β„•} (hi : i < j) (hj : j < l.length), R (l.nth_le i (lt_trans hi hj)) (l.nth_le j hj) lemma pairwise_nth_mp {R : Ξ± β†’ Ξ± β†’ Prop} {l : list Ξ±} (p : pairwise R l) : pairwise_nth R l := begin induction p with a l0 R_a_l0 p0 ih, { intros i j hi hj, cases hj }, { intros i j hi hj, rcases j with j, { cases hi }, have hj0 : j < l0.length := nat.lt_of_succ_lt_succ hj, cases i with i, { exact R_a_l0 _ (l0.nth_le_mem j hj0) }, { exact ih (nat.lt_of_succ_lt_succ hi) hj0 } } end lemma pairwise_nth_mpr {R : Ξ± β†’ Ξ± β†’ Prop} {l : list Ξ±} (hl : pairwise_nth R l) : (pairwise R l) := begin induction l with a l ih, { exact @pairwise.nil _ R }, { have R_a_l : βˆ€ (b ∈ l), (R a b) := begin intros b b_in_l, rcases nth_le_of_mem b_in_l with ⟨j,⟨hj,eq_b⟩⟩, rw [← eq_b], exact hl (nat.zero_lt_succ j) (nat.succ_lt_succ hj) end, have : l.pairwise R := begin apply ih, intros i j hi hj, exact hl (nat.succ_lt_succ hi) (nat.succ_lt_succ hj), end, exact pairwise.cons R_a_l this } end lemma pairwise_nth_iff {R : Ξ± β†’ Ξ± β†’ Prop} {l : list Ξ±} : (pairwise R l) ↔ (pairwise_nth R l) := iff.intro (@pairwise_nth_mp _ R l) (@pairwise_nth_mpr _ R l) /- The standard library defines a list to be sorted if all entries are less than or equal to all strictly later entries. Here we define a list to be strongly sorted if the relevant inequalities hold strictly. This is clearly equivalent to saying that the list is sorted and has distinct entries; we also formalise this equivalence. -/ variables [linear_order Ξ±] [decidable_rel (@has_le.le Ξ± _)] def strongly_sorted (l : list Ξ±) : Prop := list.pairwise has_lt.lt l lemma strongly_sorted_of_sorted_nodup (l : list Ξ±) : l.sorted has_le.le β†’ l.nodup β†’ l.strongly_sorted := begin intro l_sorted, induction l_sorted with a l0 a_le_l0 l0_sorted ih, {intro,exact pairwise.nil}, {intro l_nodup, cases l_nodup with _ _ a_ne_l0 l0_nodup, have l0_strongly_sorted := ih l0_nodup, have a_lt_l0 : βˆ€ (x : Ξ±) (x_in_l0 : x ∈ l0), (a < x) := begin intros x x_in_l0, exact lt_of_le_of_ne (a_le_l0 x x_in_l0) (a_ne_l0 x x_in_l0), end, exact pairwise.cons a_lt_l0 l0_strongly_sorted, } end lemma sorted_of_strongly_sorted (l : list Ξ±) : l.strongly_sorted β†’ l.sorted has_le.le := begin intro l_strongly_sorted, induction l_strongly_sorted with a l0 a_lt_l0 l0_strongly_sorted ih, {exact pairwise.nil}, {have a_le_l0 : βˆ€ x ∈ l0, a ≀ x := Ξ» x x_in_l0,le_of_lt (a_lt_l0 x x_in_l0), have l0_sorted := ih, exact pairwise.cons a_le_l0 l0_sorted } end lemma nodup_of_strongly_sorted (l : list Ξ±) : l.strongly_sorted β†’ l.nodup := begin intro l_strongly_sorted, induction l_strongly_sorted with a l0 a_ne_l0 l0_strongly_sorted ih, {exact pairwise.nil}, {have a_ne_l0 : βˆ€ x ∈ l0, a β‰  x := Ξ» x x_in_l0,ne_of_lt (a_ne_l0 x x_in_l0), have l0_nodup := ih, exact pairwise.cons a_ne_l0 l0_nodup } end /- Sortedness is defined in the standard library by structural induction. Here we reformulate it in terms of indices. -/ lemma sorted_nth_lt_mp {l : list Ξ±} (l_nodup : list.nodup l) (l_sorted : list.sorted has_le.le l) {i j : β„•} (hi : i < j) (hj : j < l.length) : (l.nth_le i (lt_trans hi hj) < l.nth_le j hj) := begin let x := l.nth_le i (lt_trans hi hj), let y := l.nth_le j hj, have x_ne_y : x β‰  y := pairwise_nth_mp l_nodup hi hj, have x_le_y : x ≀ y := pairwise_nth_mp l_sorted hi hj, exact lt_of_le_of_ne x_le_y x_ne_y, end lemma sorted_nth_le_mp {l : list Ξ±} (l_nodup : list.nodup l) (l_sorted : list.sorted has_le.le l) {i j : β„•} (hi : i ≀ j) (hj : j < l.length) : (l.nth_le i (lt_of_le_of_lt hi hj) ≀ l.nth_le j hj) := begin by_cases h : i = j, { cases h, refl }, { have hi' : i < j := lt_of_le_of_ne hi h, exact (le_of_lt $ sorted_nth_lt_mp l_nodup l_sorted hi' hj) } end lemma sorted_nth_lt {l : list Ξ±} (l_nodup : list.nodup l) (l_sorted : list.sorted has_le.le l) { i j : β„• } (hi : i < l.length) (hj : j < l.length) : (i < j) ↔ (l.nth_le i hi < l.nth_le j hj) := begin split, { intro hi', exact sorted_nth_lt_mp l_nodup l_sorted hi' hj }, { intro x_lt_y, rcases lt_or_ge i j with i_lt_j | j_le_i, { assumption }, { exfalso, let y_le_x := sorted_nth_le_mp l_nodup l_sorted j_le_i hi, exact not_lt_of_ge y_le_x x_lt_y } } end lemma sorted_nth_le {l : list Ξ±} (l_nodup : list.nodup l) (l_sorted : list.sorted has_le.le l) { i j : β„• } (hi : i < l.length) (hj : j < l.length) : (i ≀ j) ↔ (l.nth_le i hi ≀ l.nth_le j hj) := begin split, { intro hi', exact sorted_nth_le_mp l_nodup l_sorted hi' hj }, { intro x_le_y, rcases le_or_gt i j with i_le_j | j_lt_i, { assumption }, { exfalso, let y_lt_x := sorted_nth_lt_mp l_nodup l_sorted j_lt_i hi, exact not_le_of_gt y_lt_x x_le_y } } end end list namespace finset variables [linear_order Ξ±] [decidable_rel (@has_le.le Ξ± _)] /- The standard library defines a function that accepts a finite set s and returns the list of elements in sorted order. The next lemma tells us how we can recognise that list if we have obtained it by other means. -/ theorem list.perm.eqv' (Ξ± : Type*) : equivalence (@perm Ξ±) := mk_equivalence (@perm Ξ±) (@perm.refl Ξ±) (@perm.symm Ξ±) (@perm.trans Ξ±) instance list.is_setoid' (Ξ± : Type*) : setoid (list Ξ±) := setoid.mk (@perm Ξ±) (list.perm.eqv' Ξ±) lemma sort_spec (s : finset Ξ±) (l : list Ξ±) (l_nodup : l.nodup) (l_sorted : l.sorted has_le.le) (s_eq_l : s = l.to_finset) : s.sort has_le.le = l := begin let h0 := (congr_arg val (eq.trans (eq.trans (to_finset_eq (sort_nodup has_le.le s)) (eq.trans (sort_to_finset has_le.le s) s_eq_l)) (eq.symm (to_finset_eq l_nodup)))), let h1 := list.eq_of_sorted_of_perm (@quotient.exact (list Ξ±) (list.is_setoid' Ξ±) _ _ h0) (sort_sorted has_le.le s) l_sorted, let ll : multiset Ξ± := quot.mk _ l, have ll_nodup : ll.nodup := multiset.coe_nodup.mpr l_nodup, have ll_eq : (⟨ll,ll_nodup⟩ : finset Ξ±) = l.to_finset := list.to_finset_eq l_nodup, let m := s.sort has_le.le, have m_nodup : m.nodup := s.sort_nodup has_le.le, have m_sorted : m.sorted has_le.le := s.sort_sorted has_le.le, let mm : multiset Ξ± := quot.mk _ m, have mm_nodup : mm.nodup := multiset.coe_nodup.mpr m_nodup, have mm_eq : (⟨mm,mm_nodup⟩ : finset Ξ±) = m.to_finset := list.to_finset_eq m_nodup, have m_to_finset : m.to_finset = s := s.sort_to_finset has_le.le, let mm_eq_ll := congr_arg finset.val ((mm_eq.trans (m_to_finset.trans s_eq_l)).trans ll_eq.symm), dsimp[finset.val] at mm_eq_ll, have perm_m_l : perm m l := quotient.exact mm_eq_ll, exact list.eq_of_sorted_of_perm perm_m_l m_sorted l_sorted, end /- The next lemma is equivalent to the previous one. However, as an experiment we have written it as a single term rather than using tactics. We used the command "#print sort_spec" to print the proof term generated automatically be Lean from the tactic proof, and then reorganised it a bit to eliminate some inefficiencies. -/ lemma sort_spec_alt (s : finset Ξ±) (l : list Ξ±) (l_nodup : l.nodup) (l_sorted : l.sorted has_le.le) (s_eq_l : s = l.to_finset) : s.sort has_le.le = l := list.eq_of_sorted_of_perm (@quotient.exact (list Ξ±) (list.is_setoid' Ξ±) _ _ (@congr_arg (finset Ξ±) (multiset Ξ±) _ _ finset.val (eq.trans (eq.trans (to_finset_eq (sort_nodup has_le.le s)) (eq.trans (sort_to_finset has_le.le s) s_eq_l)) (eq.symm (to_finset_eq l_nodup)) ) ) ) (sort_sorted has_le.le s) l_sorted section rank_equiv /- We now give our first version of the fact that any finite, linearly ordered set of size n is order-isomorphic to (fin n). In this version, the type Ξ± has a decidable linear order and s is a finite subset of Ξ± that need not contain all elements of Ξ±. In order to talk about an order-equivalence we need to convert s into a separate type by considering {a // a ∈ s}. The definition (rank_equiv s) gives an equivalence from this type to (fin n), but it is not initially packaged with any information about order-preserving properties. Instead, we prove those properties as four separate lemmas after the definition of rank_equiv. Note that we have arguments n and s_card : s.card = n, and we produce an equivalence with (fin n). One might think that it would be more natural to just produce an equivalence with (fin s.card), and then use eq.mp to convert this to (fin n) if necessary. Unfortunately, this leads to a lot of tedious troubles with heterogenous equality. As far as I can tell, the present approach is the simplest way to avoid that. -/ variables {s : finset Ξ±} {n : β„•} (s_card : s.card = n) include s_card def rank_equiv : { a // a ∈ s } ≃ fin n := begin let l := s.sort has_le.le, have l_nodup : list.nodup l := finset.sort_nodup has_le.le s, have l_eq_s : l.to_finset = s := by simp[finset.sort_eq], have l_len : l.length = n := ((@eq.subst (finset Ξ±) (Ξ» t, t.card = l.length) _ _ l_eq_s (list.to_finset_card_of_nodup l_nodup)).symm).trans s_card, let mem_equiv : βˆ€ a : Ξ±, a ∈ s ↔ a ∈ l := Ξ» a, @eq.subst (finset Ξ±) (Ξ» t, a ∈ t ↔ a ∈ l) _ _ l_eq_s (@list.mem_to_finset Ξ± _ a l), let to_fun : { a // a ∈ s } β†’ fin n := begin intro a, have a_in_l : a.val ∈ l := (mem_equiv a.val).mp a.property, let i : β„• := list.index_of a.val l, have i_lt_n : i < n := l_len.subst (list.index_of_lt_length.mpr a_in_l), exact ⟨i,i_lt_n⟩ end, let inv_fun : fin n β†’ { a // a ∈ s } := begin intro i, have i_is_lt : i.val < l.length := @eq.subst β„• (Ξ» m, i.val < m) _ _ l_len.symm i.is_lt, let a := l.nth_le i.val i_is_lt, have a_in_s : a ∈ s := (mem_equiv a).mpr (l.nth_le_mem i.val i_is_lt), exact ⟨a,a_in_s⟩ end, have left_inv : βˆ€ a, inv_fun (to_fun a) = a := begin intro a, rcases a with ⟨a_val,a_in_s⟩, have a_in_l : a_val ∈ l := (mem_equiv a_val).mp a_in_s, let i := list.index_of a_val l, have i_lt_len : i < l.length := list.index_of_lt_length.mpr a_in_l, have i_lt_n : i < n := l_len.subst i_lt_len, dsimp[inv_fun,to_fun], apply subtype.eq,dsimp[subtype.val], exact list.index_of_nth_le i_lt_len, end, have right_inv : βˆ€ i, to_fun (inv_fun i) = i := begin intro i, rcases i with ⟨i_val,i_lt_n⟩, have i_lt_len : i_val < l.length := l_len.symm.subst i_lt_n, dsimp[inv_fun,to_fun], apply fin.eq_of_veq,dsimp[fin.val], exact list.nth_le_index_of l_nodup i_val i_lt_len, end, exact ⟨to_fun,inv_fun,left_inv,right_inv⟩, end lemma seq_le (i1 i2 : fin n) : i1 ≀ i2 ↔ (((rank_equiv s_card).inv_fun i1).val ≀ ((rank_equiv s_card).inv_fun i2).val) := begin dsimp [rank_equiv, nth_le, has_le.le, fin.le], let l := s.sort has_le.le, have l_nodup : list.nodup l := finset.sort_nodup has_le.le s, have l_sorted : l.sorted has_le.le := sort_sorted has_le.le s, have l_eq_s : l.to_finset = s := by simp[finset.sort_eq], have l_len : l.length = n := ((@eq.subst (finset Ξ±) (Ξ» t, t.card = l.length) _ _ l_eq_s (list.to_finset_card_of_nodup l_nodup)).symm).trans s_card, have i1p : i1.val < l.length := by { rw[l_len], exact i1.is_lt }, have i2p : i2.val < l.length := by { rw[l_len], exact i2.is_lt }, exact list.sorted_nth_le l_nodup l_sorted i1p i2p end lemma seq_lt (i1 i2 : fin n) : i1 < i2 ↔ (((rank_equiv s_card).inv_fun i1).val < ((rank_equiv s_card).inv_fun i2).val) := begin dsimp [rank_equiv, nth_le, has_le.le, fin.le], let l := s.sort has_le.le, have l_nodup : list.nodup l := finset.sort_nodup has_le.le s, have l_sorted : l.sorted has_le.le := sort_sorted has_le.le s, have l_eq_s : l.to_finset = s := by simp[finset.sort_eq], have l_len : l.length = n := ((@eq.subst (finset Ξ±) (Ξ» t, t.card = l.length) _ _ l_eq_s (list.to_finset_card_of_nodup l_nodup)).symm).trans s_card, have i1p : i1.val < l.length := by { rw[l_len], exact i1.is_lt }, have i2p : i2.val < l.length := by { rw[l_len], exact i2.is_lt }, exact list.sorted_nth_lt l_nodup l_sorted i1p i2p end lemma rank_le (a1 a2 : {a // a ∈ s }) : a1.val ≀ a2.val ↔ (rank_equiv s_card).to_fun a1 ≀ (rank_equiv s_card).to_fun a2 := begin let f := rank_equiv s_card, let i1 := f.to_fun a1, let i2 := f.to_fun a2, let a1a := f.inv_fun i1, let a2a := f.inv_fun i2, let h := ((seq_le s_card) (f.to_fun a1) (f.to_fun a2)).symm, let e1 : (f.inv_fun i1).val = a1.val := congr_arg subtype.val (f.left_inv a1), let e2 : (f.inv_fun i2).val = a2.val := congr_arg subtype.val (f.left_inv a2), split, {intro h1,rw[← e1,← e2] at h1,exact h.mp h1,}, {intro h2,let h3 := h.mpr h2,rw[e1,e2] at h3,exact h3} end lemma rank_lt (a1 a2 : {a // a ∈ s }) : a1.val < a2.val ↔ ((rank_equiv s_card).to_fun a1) < ((rank_equiv s_card).to_fun a2) := begin let f := rank_equiv s_card, let i1 := f.to_fun a1, let i2 := f.to_fun a2, let a1a := f.inv_fun i1, let a2a := f.inv_fun i2, let h := ((seq_lt s_card) (f.to_fun a1) (f.to_fun a2)).symm, let e1 : (f.inv_fun i1).val = a1.val := congr_arg subtype.val (f.left_inv a1), let e2 : (f.inv_fun i2).val = a2.val := congr_arg subtype.val (f.left_inv a2), split, {intro h1,rw[← e1,← e2] at h1,exact h.mp h1,}, {intro h2,let h3 := h.mpr h2,rw[e1,e2] at h3,exact h3} end lemma rank_as_card (a : { x // x ∈ s}) : ((rank_equiv s_card).to_fun a).val = card (s.filter (Ξ» x, x < a.val)) := begin let f := rank_equiv s_card, let u := s.filter (Ξ» x, x < a.val), let ut := {x // x ∈ u}, let k := f.to_fun a, have fi_k : f.inv_fun k = a := f.left_inv a, let g_to_fun : ut β†’ fin k.val := begin intro x, let x0 : { x // x ∈ s } := ⟨x.val,(mem_filter.mp x.property).left⟩, let x_lt_a : x0.val < a.val := (mem_filter.mp x.property).right, let i := f.to_fun x0, let i_lt_k : i < k := ((rank_lt s_card) x0 a).mp x_lt_a, let i_lt_k_val : i.val < k.val := begin let h := i_lt_k, dsimp[has_lt.lt,fin.lt] at h, exact h, end, exact ⟨i.val,i_lt_k_val⟩, end, let g_inv_fun : fin k.val β†’ ut := begin intro i, let i0 : fin n := ⟨i.val,lt_trans i.is_lt k.is_lt⟩, have i0_lt_k : i0 < k := by { simp[has_lt.lt,fin.lt], exact i.is_lt,}, let x0 := f.inv_fun i0, let x_lt_a := ((seq_lt s_card) i0 k).mp i0_lt_k, rw[fi_k] at x_lt_a, have x_in_u : x0.val ∈ u := begin apply mem_filter.mpr, exact ⟨x0.property,x_lt_a⟩, end, exact ⟨x0.val,x_in_u⟩, end, have g_left_inv : βˆ€ x, g_inv_fun (g_to_fun x) = x := begin intro x, dsimp[g_to_fun,g_inv_fun], apply subtype.eq; simp[subtype.val], exact congr_arg subtype.val (f.left_inv _), end, have g_right_inv : βˆ€ i, g_to_fun (g_inv_fun i) = i := begin intro i, dsimp[g_to_fun,g_inv_fun], apply fin.eq_of_veq; simp[fin.val], exact congr_arg fin.val (f.right_inv _), end, let g : ut ≃ fin k.val := ⟨g_to_fun,g_inv_fun,g_left_inv,g_right_inv⟩, let h0 := fintype.card_congr g, let h1 := @finset.card_attach Ξ± u, let h2 := fintype.card_fin k.val, exact (h1.symm.trans (h0.trans h2)).symm, end end rank_equiv end finset namespace fintype open finset fintype variables [linear_order Ξ±] [decidable_rel (@has_le.le Ξ± _)] variables [fintype Ξ±] {n : β„•} (Ξ±_card : card Ξ± = n) include Ξ±_card /- We now give our second version of the fact that any finite, linearly ordered set of size n is order-isomorphic to (fin n). In this version we assume given an instance of (fintype Ξ±), which is essentially a proof that the whole type Ξ± is finite. From this we build an equivalence from Ξ± to (fin n), and again we prove the order properties as separate lemmas. -/ def rank_equiv : Ξ± ≃ fin n := begin have h0 : finset.card (@elems Ξ± _) = n := Ξ±_card, let f := finset.rank_equiv h0, let to_fun : Ξ± β†’ fin n := Ξ» a, f.to_fun ⟨a,mem_univ a⟩, let inv_fun : fin n β†’ Ξ± := Ξ» i, (f.inv_fun i).val, have left_inv : βˆ€ a : Ξ±, inv_fun (to_fun a) = a := Ξ» a, congr_arg subtype.val (f.left_inv ⟨a,mem_univ a⟩), have right_inv : βˆ€ i : fin n, to_fun (inv_fun i) = i := begin intro i, have e : f.inv_fun i = ⟨inv_fun i,mem_univ _⟩ := by {apply subtype.eq,simp[inv_fun]}, dsimp[inv_fun,to_fun],rw[← e],exact f.right_inv i, end, exact ⟨to_fun,inv_fun,left_inv,right_inv⟩, end lemma seq_le (i1 i2 : fin n) : i1 ≀ i2 ↔ (rank_equiv Ξ±_card).inv_fun i1 ≀ (rank_equiv Ξ±_card).inv_fun i2 := (seq_le Ξ±_card) i1 i2 lemma seq_lt (i1 i2 : fin n) : i1 < i2 ↔ (rank_equiv Ξ±_card).inv_fun i1 < (rank_equiv Ξ±_card).inv_fun i2 := (seq_lt Ξ±_card) i1 i2 lemma rank_le (a1 a2 : Ξ±) : a1 ≀ a2 ↔ (rank_equiv Ξ±_card).to_fun a1 ≀ (rank_equiv Ξ±_card).to_fun a2 := (rank_le Ξ±_card) ⟨a1,mem_univ a1⟩ ⟨a2,mem_univ a2⟩ lemma rank_lt (a1 a2 : Ξ±) : a1 < a2 ↔ (rank_equiv Ξ±_card).to_fun a1 < (rank_equiv Ξ±_card).to_fun a2 := (rank_lt Ξ±_card) ⟨a1,mem_univ a1⟩ ⟨a2,mem_univ a2⟩ end fintype namespace fin /- The only strictly increasing self-map of (fin n) is the identity -/ lemma rigid {n : β„•} {f : fin n β†’ fin n} (f_mono : βˆ€ {i1 i2 : fin n}, i1 < i2 β†’ (f i1) < (f i2)) : βˆ€ (i : fin n), f i = i := begin induction n with n0 ih, {intro i,exact fin.elim0 i}, { let f0 : fin n0 β†’ fin n0 := begin intro i0, let h0 := ne_of_lt (lt_of_le_of_lt (fin.zero_le (f 0)) (f_mono (@fin.zero_lt_succ n0 i0))), exact fin.pred (f i0.succ) h0.symm, end, have succ_f0 : βˆ€ (i0 : fin n0), (f0 i0).succ = f i0.succ := begin intro i0, let h0 := ne_of_lt (lt_of_le_of_lt (fin.zero_le (f 0)) (f_mono (@fin.zero_lt_succ n0 i0))), exact fin.succ_pred (f i0.succ) h0.symm, end, have f0_mono : βˆ€ {i1 i2 : fin n0}, i1 < i2 β†’ (f0 i1) < (f0 i2) := begin intros i1 i2 i1_lt_i2, let h0 := f_mono (fin.succ_lt_succ i1_lt_i2), rw[← (succ_f0 i1)] at h0, rw[← (succ_f0 i2)] at h0, exact fin.lt_of_succ_lt_succ h0, end, let f0_id := ih @f0_mono, have f_zero_a : βˆ€ (j : fin n0.succ), f 0 = j β†’ j = 0 := begin intros j e, cases j, cases j_val with j0_val, {refl}, { exfalso, have j0_is_lt : j0_val < n0 := nat.lt_of_succ_lt_succ j_is_lt, let j0 : fin n0 := ⟨j0_val,j0_is_lt⟩, let j := j0.succ, have h0 : f j = j := (succ_f0 j0).symm.trans (congr_arg fin.succ (f0_id j0)), have h1 : f 0 < f j := f_mono (@fin.zero_lt_succ n0 j0), have h2 : j < f j := @eq.subst (fin n0.succ) (Ξ» k, k < f j) _ _ e h1, exact (ne_of_lt h2) h0.symm, } end, have f_zero : f 0 = 0 := f_zero_a (f 0) rfl, intro i, cases i, cases i_val with i0_val, {exact f_zero,}, { have i0_is_lt : i0_val < n0 := nat.lt_of_succ_lt_succ i_is_lt, let h0 := congr_arg fin.succ (f0_id ⟨i0_val,i0_is_lt⟩), let h1 := succ_f0 ⟨i0_val,i0_is_lt⟩, let h2 := h1.symm.trans h0, exact h2, } } end end fin
ec5d7de5caa90964dff441f9a5c6478e1fa6c209
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/data/padics/padic_norm.lean
f64eb8224af26cf30d5987fec2f8e141e9b5881d
[ "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
26,586
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import ring_theory.int.basic import algebra.field_power import ring_theory.multiplicity import data.real.cau_seq import tactic.ring_exp import tactic.basic /-! # p-adic norm This file defines the p-adic valuation and the p-adic norm on β„š. The p-adic valuation on β„š is the difference of the multiplicities of `p` in the numerator and denominator of `q`. This function obeys the standard properties of a valuation, with the appropriate assumptions on p. The valuation induces a norm on β„š. This norm is a nonarchimedean absolute value. It takes values in {0} βˆͺ {1/p^k | k ∈ β„€}. ## Notations This file uses the local notation `/.` for `rat.mk`. ## Implementation notes Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically by taking `[fact (prime p)]` as a type class argument. ## References * [F. Q. GouΓͺva, *p-adic numbers*][gouvea1997] * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * <https://en.wikipedia.org/wiki/P-adic_number> ## Tags p-adic, p adic, padic, norm, valuation -/ universe u open nat open_locale rat open multiplicity /-- For `p β‰  1`, the p-adic valuation of an integer `z β‰  0` is the largest natural number `n` such that p^n divides z. `padic_val_rat` defines the valuation of a rational `q` to be the valuation of `q.num` minus the valuation of `q.denom`. If `q = 0` or `p = 1`, then `padic_val_rat p q` defaults to 0. -/ def padic_val_rat (p : β„•) (q : β„š) : β„€ := if h : q β‰  0 ∧ p β‰  1 then (multiplicity (p : β„€) q.num).get (multiplicity.finite_int_iff.2 ⟨h.2, rat.num_ne_zero_of_ne_zero h.1⟩) - (multiplicity (p : β„€) q.denom).get (multiplicity.finite_int_iff.2 ⟨h.2, by exact_mod_cast rat.denom_ne_zero _⟩) else 0 /-- A simplification of the definition of `padic_val_rat p q` when `q β‰  0` and `p` is prime. -/ lemma padic_val_rat_def (p : β„•) [hp : fact p.prime] {q : β„š} (hq : q β‰  0) : padic_val_rat p q = (multiplicity (p : β„€) q.num).get (finite_int_iff.2 ⟨hp.1.ne_one, rat.num_ne_zero_of_ne_zero hq⟩) - (multiplicity (p : β„€) q.denom).get (finite_int_iff.2 ⟨hp.1.ne_one, by exact_mod_cast rat.denom_ne_zero _⟩) := dif_pos ⟨hq, hp.1.ne_one⟩ namespace padic_val_rat open multiplicity variables {p : β„•} /-- `padic_val_rat p q` is symmetric in `q`. -/ @[simp] protected lemma neg (q : β„š) : padic_val_rat p (-q) = padic_val_rat p q := begin unfold padic_val_rat, split_ifs, { simp [-add_comm]; refl }, { exfalso, simp * at * }, { exfalso, simp * at * }, { refl } end /-- `padic_val_rat p 1` is 0 for any `p`. -/ @[simp] protected lemma one : padic_val_rat p 1 = 0 := by unfold padic_val_rat; split_ifs; simp * /-- For `p β‰  0, p β‰  1, `padic_val_rat p p` is 1. -/ @[simp] lemma padic_val_rat_self (hp : 1 < p) : padic_val_rat p p = 1 := by unfold padic_val_rat; split_ifs; simp [*, nat.one_lt_iff_ne_zero_and_ne_one] at * /-- The p-adic value of an integer `z β‰  0` is the multiplicity of `p` in `z`. -/ lemma padic_val_rat_of_int (z : β„€) (hp : p β‰  1) (hz : z β‰  0) : padic_val_rat p (z : β„š) = (multiplicity (p : β„€) z).get (finite_int_iff.2 ⟨hp, hz⟩) := by rw [padic_val_rat, dif_pos]; simp *; refl end padic_val_rat /-- A convenience function for the case of `padic_val_rat` when both inputs are natural numbers. -/ def padic_val_nat (p : β„•) (n : β„•) : β„• := int.to_nat (padic_val_rat p n) section padic_val_nat /-- `padic_val_nat` is defined as an `int.to_nat` cast; this lemma ensures that the cast is well-behaved. -/ lemma zero_le_padic_val_rat_of_nat (p n : β„•) : 0 ≀ padic_val_rat p n := begin unfold padic_val_rat, split_ifs, { simp, }, { trivial, }, end /-- `padic_val_rat` coincides with `padic_val_nat`. -/ @[simp, norm_cast] lemma padic_val_rat_of_nat (p n : β„•) : ↑(padic_val_nat p n) = padic_val_rat p n := begin unfold padic_val_nat, rw int.to_nat_of_nonneg (zero_le_padic_val_rat_of_nat p n), end /-- A simplification of `padic_val_nat` when one input is prime, by analogy with `padic_val_rat_def`. -/ lemma padic_val_nat_def {p : β„•} [hp : fact p.prime] {n : β„•} (hn : n β‰  0) : padic_val_nat p n = (multiplicity p n).get (multiplicity.finite_nat_iff.2 ⟨nat.prime.ne_one hp.1, bot_lt_iff_ne_bot.mpr hn⟩) := begin have n_nonzero : (n : β„š) β‰  0, by simpa only [cast_eq_zero, ne.def], -- Infinite loop with @simp padic_val_rat_of_nat unless we restrict the available lemmas here, -- hence the very long list simpa only [ int.coe_nat_multiplicity p n, rat.coe_nat_denom n, (padic_val_rat_of_nat p n).symm, int.coe_nat_zero, int.coe_nat_inj', sub_zero, get_one_right, int.coe_nat_succ, zero_add, rat.coe_nat_num ] using padic_val_rat_def p n_nonzero, end lemma one_le_padic_val_nat_of_dvd {n p : nat} [prime : fact p.prime] (nonzero : n β‰  0) (div : p ∣ n) : 1 ≀ padic_val_nat p n := begin rw @padic_val_nat_def _ prime _ nonzero, let one_le_mul : _ ≀ multiplicity p n := @multiplicity.le_multiplicity_of_pow_dvd _ _ _ p n 1 (begin norm_num, exact div end), simp only [enat.coe_one] at one_le_mul, rcases one_le_mul with ⟨_, q⟩, dsimp at q, solve_by_elim, end @[simp] lemma padic_val_nat_zero (m : nat) : padic_val_nat m 0 = 0 := by simpa @[simp] lemma padic_val_nat_one (m : nat) : padic_val_nat m 1 = 0 := by simp [padic_val_nat] end padic_val_nat namespace padic_val_rat open multiplicity variables (p : β„•) [p_prime : fact p.prime] include p_prime /-- The multiplicity of `p : β„•` in `a : β„€` is finite exactly when `a β‰  0`. -/ lemma finite_int_prime_iff {p : β„•} [p_prime : fact p.prime] {a : β„€} : finite (p : β„€) a ↔ a β‰  0 := by simp [finite_int_iff, ne.symm (ne_of_lt (p_prime.1.one_lt))] /-- A rewrite lemma for `padic_val_rat p q` when `q` is expressed in terms of `rat.mk`. -/ protected lemma defn {q : β„š} {n d : β„€} (hqz : q β‰  0) (qdf : q = n /. d) : padic_val_rat p q = (multiplicity (p : β„€) n).get (finite_int_iff.2 ⟨ne.symm $ ne_of_lt p_prime.1.one_lt, Ξ» hn, by simp * at *⟩) - (multiplicity (p : β„€) d).get (finite_int_iff.2 ⟨ne.symm $ ne_of_lt p_prime.1.one_lt, Ξ» hd, by simp * at *⟩) := have hn : n β‰  0, from rat.mk_num_ne_zero_of_ne_zero hqz qdf, have hd : d β‰  0, from rat.mk_denom_ne_zero_of_ne_zero hqz qdf, let ⟨c, hc1, hc2⟩ := rat.num_denom_mk hn hd qdf in by rw [padic_val_rat, dif_pos]; simp [hc1, hc2, multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime.1), (ne.symm (ne_of_lt p_prime.1.one_lt)), hqz] /-- A rewrite lemma for `padic_val_rat p (q * r)` with conditions `q β‰  0`, `r β‰  0`. -/ protected lemma mul {q r : β„š} (hq : q β‰  0) (hr : r β‰  0) : padic_val_rat p (q * r) = padic_val_rat p q + padic_val_rat p r := have q*r = (q.num * r.num) /. (↑q.denom * ↑r.denom), by rw_mod_cast rat.mul_num_denom, have hq' : q.num /. q.denom β‰  0, by rw rat.num_denom; exact hq, have hr' : r.num /. r.denom β‰  0, by rw rat.num_denom; exact hr, have hp' : _root_.prime (p : β„€), from nat.prime_iff_prime_int.1 p_prime.1, begin rw [padic_val_rat.defn p (mul_ne_zero hq hr) this], conv_rhs { rw [←(@rat.num_denom q), padic_val_rat.defn p hq', ←(@rat.num_denom r), padic_val_rat.defn p hr'] }, rw [multiplicity.mul' hp', multiplicity.mul' hp']; simp [add_comm, add_left_comm, sub_eq_add_neg] end /-- A rewrite lemma for `padic_val_rat p (q^k) with condition `q β‰  0`. -/ protected lemma pow {q : β„š} (hq : q β‰  0) {k : β„•} : padic_val_rat p (q ^ k) = k * padic_val_rat p q := by induction k; simp [*, padic_val_rat.mul _ hq (pow_ne_zero _ hq), pow_succ, add_mul, add_comm] /-- A rewrite lemma for `padic_val_rat p (q⁻¹)` with condition `q β‰  0`. -/ protected lemma inv {q : β„š} (hq : q β‰  0) : padic_val_rat p (q⁻¹) = -padic_val_rat p q := by rw [eq_neg_iff_add_eq_zero, ← padic_val_rat.mul p (inv_ne_zero hq) hq, inv_mul_cancel hq, padic_val_rat.one] /-- A rewrite lemma for `padic_val_rat p (q / r)` with conditions `q β‰  0`, `r β‰  0`. -/ protected lemma div {q r : β„š} (hq : q β‰  0) (hr : r β‰  0) : padic_val_rat p (q / r) = padic_val_rat p q - padic_val_rat p r := by rw [div_eq_mul_inv, padic_val_rat.mul p hq (inv_ne_zero hr), padic_val_rat.inv p hr, sub_eq_add_neg] /-- A condition for `padic_val_rat p (n₁ / d₁) ≀ padic_val_rat p (nβ‚‚ / dβ‚‚), in terms of divisibility by `p^n`. -/ lemma padic_val_rat_le_padic_val_rat_iff {n₁ nβ‚‚ d₁ dβ‚‚ : β„€} (hn₁ : n₁ β‰  0) (hnβ‚‚ : nβ‚‚ β‰  0) (hd₁ : d₁ β‰  0) (hdβ‚‚ : dβ‚‚ β‰  0) : padic_val_rat p (n₁ /. d₁) ≀ padic_val_rat p (nβ‚‚ /. dβ‚‚) ↔ βˆ€ (n : β„•), ↑p ^ n ∣ n₁ * dβ‚‚ β†’ ↑p ^ n ∣ nβ‚‚ * d₁ := have hf1 : finite (p : β„€) (n₁ * dβ‚‚), from finite_int_prime_iff.2 (mul_ne_zero hn₁ hdβ‚‚), have hf2 : finite (p : β„€) (nβ‚‚ * d₁), from finite_int_prime_iff.2 (mul_ne_zero hnβ‚‚ hd₁), by conv { to_lhs, rw [padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hn₁ hd₁) rfl, padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hnβ‚‚ hdβ‚‚) rfl, sub_le_iff_le_add', ← add_sub_assoc, le_sub_iff_add_le], norm_cast, rw [← multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime.1) hf1, add_comm, ← multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime.1) hf2, enat.get_le_get, multiplicity_le_multiplicity_iff] } /-- Sufficient conditions to show that the p-adic valuation of `q` is less than or equal to the p-adic vlauation of `q + r`. -/ theorem le_padic_val_rat_add_of_le {q r : β„š} (hq : q β‰  0) (hr : r β‰  0) (hqr : q + r β‰  0) (h : padic_val_rat p q ≀ padic_val_rat p r) : padic_val_rat p q ≀ padic_val_rat p (q + r) := have hqn : q.num β‰  0, from rat.num_ne_zero_of_ne_zero hq, have hqd : (q.denom : β„€) β‰  0, by exact_mod_cast rat.denom_ne_zero _, have hrn : r.num β‰  0, from rat.num_ne_zero_of_ne_zero hr, have hrd : (r.denom : β„€) β‰  0, by exact_mod_cast rat.denom_ne_zero _, have hqreq : q + r = (((q.num * r.denom + q.denom * r.num : β„€)) /. (↑q.denom * ↑r.denom : β„€)), from rat.add_num_denom _ _, have hqrd : q.num * ↑(r.denom) + ↑(q.denom) * r.num β‰  0, from rat.mk_num_ne_zero_of_ne_zero hqr hqreq, begin conv_lhs { rw ←(@rat.num_denom q) }, rw [hqreq, padic_val_rat_le_padic_val_rat_iff p hqn hqrd hqd (mul_ne_zero hqd hrd), ← multiplicity_le_multiplicity_iff, mul_left_comm, multiplicity.mul (nat.prime_iff_prime_int.1 p_prime.1), add_mul], rw [←(@rat.num_denom q), ←(@rat.num_denom r), padic_val_rat_le_padic_val_rat_iff p hqn hrn hqd hrd, ← multiplicity_le_multiplicity_iff] at h, calc _ ≀ min (multiplicity ↑p (q.num * ↑(r.denom) * ↑(q.denom))) (multiplicity ↑p (↑(q.denom) * r.num * ↑(q.denom))) : (le_min (by rw [@multiplicity.mul _ _ _ _ (_ * _) _ (nat.prime_iff_prime_int.1 p_prime.1), add_comm]) (by rw [mul_assoc, @multiplicity.mul _ _ _ _ (q.denom : β„€) (_ * _) (nat.prime_iff_prime_int.1 p_prime.1)]; exact add_le_add_left h _)) ... ≀ _ : min_le_multiplicity_add end /-- The minimum of the valuations of `q` and `r` is less than or equal to the valuation of `q + r`. -/ theorem min_le_padic_val_rat_add {q r : β„š} (hq : q β‰  0) (hr : r β‰  0) (hqr : q + r β‰  0) : min (padic_val_rat p q) (padic_val_rat p r) ≀ padic_val_rat p (q + r) := (le_total (padic_val_rat p q) (padic_val_rat p r)).elim (Ξ» h, by rw [min_eq_left h]; exact le_padic_val_rat_add_of_le _ hq hr hqr h) (Ξ» h, by rw [min_eq_right h, add_comm]; exact le_padic_val_rat_add_of_le _ hr hq (by rwa add_comm) h) open_locale big_operators /-- A finite sum of rationals with positive p-adic valuation has positive p-adic valuation (if the sum is non-zero). -/ theorem sum_pos_of_pos {n : β„•} {F : β„• β†’ β„š} (hF : βˆ€ i, i < n β†’ 0 < padic_val_rat p (F i)) (hn0 : βˆ‘ i in finset.range n, F i β‰  0) : 0 < padic_val_rat p (βˆ‘ i in finset.range n, F i) := begin induction n with d hd, { exact false.elim (hn0 rfl) }, { rw finset.sum_range_succ at hn0 ⊒, by_cases h : βˆ‘ (x : β„•) in finset.range d, F x = 0, { rw [h, add_zero], exact hF d (lt_add_one _) }, { refine lt_of_lt_of_le _ (min_le_padic_val_rat_add p (Ξ» h1, _) h hn0), { refine lt_min (hF d (lt_add_one _)) (hd (Ξ» i hi, _) h), exact hF _ (lt_trans hi (lt_add_one _)) }, { have h2 := hF d (lt_add_one _), rw h1 at h2, exact lt_irrefl _ h2 } } } end end padic_val_rat namespace padic_val_nat /-- A rewrite lemma for `padic_val_nat p (q * r)` with conditions `q β‰  0`, `r β‰  0`. -/ protected lemma mul (p : β„•) [p_prime : fact p.prime] {q r : β„•} (hq : q β‰  0) (hr : r β‰  0) : padic_val_nat p (q * r) = padic_val_nat p q + padic_val_nat p r := begin apply int.coe_nat_inj, simp only [padic_val_rat_of_nat, nat.cast_mul], rw padic_val_rat.mul, norm_cast, exact cast_ne_zero.mpr hq, exact cast_ne_zero.mpr hr, end /-- Dividing out by a prime factor reduces the padic_val_nat by 1. -/ protected lemma div {p : β„•} [p_prime : fact p.prime] {b : β„•} (dvd : p ∣ b) : (padic_val_nat p (b / p)) = (padic_val_nat p b) - 1 := begin by_cases b_split : (b = 0), { simp [b_split], }, { have split_frac : padic_val_rat p (b / p) = padic_val_rat p b - padic_val_rat p p := padic_val_rat.div p (nat.cast_ne_zero.mpr b_split) (nat.cast_ne_zero.mpr (nat.prime.ne_zero p_prime.1)), rw padic_val_rat.padic_val_rat_self (nat.prime.one_lt p_prime.1) at split_frac, have r : 1 ≀ padic_val_nat p b := one_le_padic_val_nat_of_dvd b_split dvd, exact_mod_cast split_frac, } end end padic_val_nat section padic_val_nat /-- If a prime doesn't appear in `n`, `padic_val_nat p n` is `0`. -/ lemma padic_val_nat_of_not_dvd {p : β„•} [fact p.prime] {n : β„•} (not_dvd : Β¬(p ∣ n)) : padic_val_nat p n = 0 := begin by_cases hn : n = 0, { subst hn, simp at not_dvd, trivial, }, { rw padic_val_nat_def hn, exact (@multiplicity.unique' _ _ _ p n 0 (by simp) (by simpa using not_dvd)).symm, assumption, }, end lemma dvd_of_one_le_padic_val_nat {n p : nat} [prime : fact p.prime] (hp : 1 ≀ padic_val_nat p n) : p ∣ n := begin by_contra h, rw padic_val_nat_of_not_dvd h at hp, exact lt_irrefl 0 (lt_of_lt_of_le zero_lt_one hp), end lemma padic_val_nat_primes {p q : β„•} [p_prime : fact p.prime] [q_prime : fact q.prime] (neq : p β‰  q) : padic_val_nat p q = 0 := @padic_val_nat_of_not_dvd p p_prime q $ (not_congr (iff.symm (prime_dvd_prime_iff_eq p_prime.1 q_prime.1))).mp neq protected lemma padic_val_nat.div' {p : β„•} [p_prime : fact p.prime] : βˆ€ {m : β„•} (cpm : coprime p m) {b : β„•} (dvd : m ∣ b), padic_val_nat p (b / m) = padic_val_nat p b | 0 := Ξ» cpm b dvd, by { rw zero_dvd_iff at dvd, rw [dvd, nat.zero_div], } | (n + 1) := Ξ» cpm b dvd, begin rcases dvd with ⟨c, rfl⟩, rw [mul_div_right c (nat.succ_pos _)],by_cases hc : c = 0, { rw [hc, mul_zero] }, { rw padic_val_nat.mul, { suffices : Β¬ p ∣ (n+1), { rw [padic_val_nat_of_not_dvd this, zero_add] }, contrapose! cpm, exact p_prime.1.dvd_iff_not_coprime.mp cpm }, { exact nat.succ_ne_zero _ }, { exact hc } }, end lemma padic_val_nat_eq_factors_count (p : β„•) [hp : fact p.prime] : βˆ€ (n : β„•), padic_val_nat p n = (factors n).count p | 0 := rfl | 1 := by { rw padic_val_nat_one, refl } | (m + 2) := let n := m + 2 in let q := min_fac n in have hq : fact q.prime := ⟨min_fac_prime (show m + 2 β‰  1, by linarith)⟩, have wf : n / q < n := nat.div_lt_self (nat.succ_pos _) hq.1.one_lt, begin rw factors_add_two, show padic_val_nat p n = list.count p (q :: (factors (n / q))), rw [list.count_cons', ← padic_val_nat_eq_factors_count], split_ifs with h, have p_dvd_n : p ∣ n, { have: q ∣ n := nat.min_fac_dvd n, cc }, { rw [←h, padic_val_nat.div], { have: 1 ≀ padic_val_nat p n := one_le_padic_val_nat_of_dvd (by linarith) p_dvd_n, exact (nat.sub_eq_iff_eq_add this).mp rfl, }, { exact p_dvd_n, }, }, { suffices : p.coprime q, { rw [padic_val_nat.div' this (min_fac_dvd n), add_zero], }, rwa nat.coprime_primes hp.1 hq.1, }, end open_locale big_operators lemma prod_pow_prime_padic_val_nat (n : nat) (hn : n β‰  0) (m : nat) (pr : n < m) : ∏ p in finset.filter nat.prime (finset.range m), p ^ (padic_val_nat p n) = n := begin rw ← pos_iff_ne_zero at hn, have H : (factors n : multiset β„•).prod = n, { rw [multiset.coe_prod, prod_factors hn], }, rw finset.prod_multiset_count at H, conv_rhs { rw ← H, }, refine finset.prod_bij_ne_one (Ξ» p hp hp', p) _ _ _ _, { rintro p hp hpn, rw [finset.mem_filter, finset.mem_range] at hp, rw [multiset.mem_to_finset, multiset.mem_coe, mem_factors_iff_dvd hn hp.2], contrapose! hpn, haveI Hp : fact p.prime := ⟨hp.2⟩, rw [padic_val_nat_of_not_dvd hpn, pow_zero], }, { intros, assumption }, { intros p hp hpn, rw [multiset.mem_to_finset, multiset.mem_coe] at hp, haveI Hp : fact p.prime := ⟨mem_factors hp⟩, simp only [exists_prop, ne.def, finset.mem_filter, finset.mem_range], refine ⟨p, ⟨_, Hp.1⟩, ⟨_, rfl⟩⟩, { rw mem_factors_iff_dvd hn Hp.1 at hp, exact lt_of_le_of_lt (le_of_dvd hn hp) pr }, { rw padic_val_nat_eq_factors_count, simpa [ne.def, multiset.coe_count] using hpn } }, { intros p hp hpn, rw [finset.mem_filter, finset.mem_range] at hp, haveI Hp : fact p.prime := ⟨hp.2⟩, rw [padic_val_nat_eq_factors_count, multiset.coe_count] } end end padic_val_nat /-- If `q β‰  0`, the p-adic norm of a rational `q` is `p ^ (-(padic_val_rat p q))`. If `q = 0`, the p-adic norm of `q` is 0. -/ def padic_norm (p : β„•) (q : β„š) : β„š := if q = 0 then 0 else (↑p : β„š) ^ (-(padic_val_rat p q)) namespace padic_norm section padic_norm open padic_val_rat variables (p : β„•) /-- Unfolds the definition of the p-adic norm of `q` when `q β‰  0`. -/ @[simp] protected lemma eq_fpow_of_nonzero {q : β„š} (hq : q β‰  0) : padic_norm p q = p ^ (-(padic_val_rat p q)) := by simp [hq, padic_norm] /-- The p-adic norm is nonnegative. -/ protected lemma nonneg (q : β„š) : 0 ≀ padic_norm p q := if hq : q = 0 then by simp [hq, padic_norm] else begin unfold padic_norm; split_ifs, apply fpow_nonneg, exact_mod_cast nat.zero_le _ end /-- The p-adic norm of 0 is 0. -/ @[simp] protected lemma zero : padic_norm p 0 = 0 := by simp [padic_norm] /-- The p-adic norm of 1 is 1. -/ @[simp] protected lemma one : padic_norm p 1 = 1 := by simp [padic_norm] /-- The p-adic norm of `p` is `1/p` if `p > 1`. See also `padic_norm.padic_norm_p_of_prime` for a version that assumes `p` is prime. -/ lemma padic_norm_p {p : β„•} (hp : 1 < p) : padic_norm p p = 1 / p := by simp [padic_norm, (show p β‰  0, by linarith), padic_val_rat.padic_val_rat_self hp] /-- The p-adic norm of `p` is `1/p` if `p` is prime. See also `padic_norm.padic_norm_p` for a version that assumes `1 < p`. -/ @[simp] lemma padic_norm_p_of_prime (p : β„•) [fact p.prime] : padic_norm p p = 1 / p := padic_norm_p $ nat.prime.one_lt (fact.out _) /-- The p-adic norm of `q` is `1` if `q` is prime and not equal to `p`. -/ lemma padic_norm_of_prime_of_ne {p q : β„•} [p_prime : fact p.prime] [q_prime : fact q.prime] (neq : p β‰  q) : padic_norm p q = 1 := begin have p : padic_val_rat p q = 0, { exact_mod_cast @padic_val_nat_primes p q p_prime q_prime neq }, simp [padic_norm, p, q_prime.1.1, q_prime.1.ne_zero], end /-- The p-adic norm of `p` is less than 1 if `1 < p`. See also `padic_norm.padic_norm_p_lt_one_of_prime` for a version assuming `prime p`. -/ lemma padic_norm_p_lt_one {p : β„•} (hp : 1 < p) : padic_norm p p < 1 := begin rw [padic_norm_p hp, div_lt_iff, one_mul], { exact_mod_cast hp }, { exact_mod_cast zero_lt_one.trans hp }, end /-- The p-adic norm of `p` is less than 1 if `p` is prime. See also `padic_norm.padic_norm_p_lt_one` for a version assuming `1 < p`. -/ lemma padic_norm_p_lt_one_of_prime (p : β„•) [fact p.prime] : padic_norm p p < 1 := padic_norm_p_lt_one $ nat.prime.one_lt (fact.out _) /-- `padic_norm p q` takes discrete values `p ^ -z` for `z : β„€`. -/ protected theorem values_discrete {q : β„š} (hq : q β‰  0) : βˆƒ z : β„€, padic_norm p q = p ^ (-z) := ⟨ (padic_val_rat p q), by simp [padic_norm, hq] ⟩ /-- `padic_norm p` is symmetric. -/ @[simp] protected lemma neg (q : β„š) : padic_norm p (-q) = padic_norm p q := if hq : q = 0 then by simp [hq] else by simp [padic_norm, hq] variable [hp : fact p.prime] include hp /-- If `q β‰  0`, then `padic_norm p q β‰  0`. -/ protected lemma nonzero {q : β„š} (hq : q β‰  0) : padic_norm p q β‰  0 := begin rw padic_norm.eq_fpow_of_nonzero p hq, apply fpow_ne_zero_of_ne_zero, exact_mod_cast ne_of_gt hp.1.pos end /-- If the p-adic norm of `q` is 0, then `q` is 0. -/ lemma zero_of_padic_norm_eq_zero {q : β„š} (h : padic_norm p q = 0) : q = 0 := begin apply by_contradiction, intro hq, unfold padic_norm at h, rw if_neg hq at h, apply absurd h, apply fpow_ne_zero_of_ne_zero, exact_mod_cast hp.1.ne_zero end /-- The p-adic norm is multiplicative. -/ @[simp] protected theorem mul (q r : β„š) : padic_norm p (q*r) = padic_norm p q * padic_norm p r := if hq : q = 0 then by simp [hq] else if hr : r = 0 then by simp [hr] else have q*r β‰  0, from mul_ne_zero hq hr, have (↑p : β„š) β‰  0, by simp [hp.1.ne_zero], by simp [padic_norm, *, padic_val_rat.mul, fpow_add this, mul_comm] /-- The p-adic norm respects division. -/ @[simp] protected theorem div (q r : β„š) : padic_norm p (q / r) = padic_norm p q / padic_norm p r := if hr : r = 0 then by simp [hr] else eq_div_of_mul_eq (padic_norm.nonzero _ hr) (by rw [←padic_norm.mul, div_mul_cancel _ hr]) /-- The p-adic norm of an integer is at most 1. -/ protected theorem of_int (z : β„€) : padic_norm p ↑z ≀ 1 := if hz : z = 0 then by simp [hz, zero_le_one] else begin unfold padic_norm, rw [if_neg _], { refine fpow_le_one_of_nonpos _ _, { exact_mod_cast le_of_lt hp.1.one_lt, }, { rw [padic_val_rat_of_int _ hp.1.ne_one hz, neg_nonpos], norm_cast, simp }}, exact_mod_cast hz end private lemma nonarchimedean_aux {q r : β„š} (h : padic_val_rat p q ≀ padic_val_rat p r) : padic_norm p (q + r) ≀ max (padic_norm p q) (padic_norm p r) := have hnqp : padic_norm p q β‰₯ 0, from padic_norm.nonneg _ _, have hnrp : padic_norm p r β‰₯ 0, from padic_norm.nonneg _ _, if hq : q = 0 then by simp [hq, max_eq_right hnrp, le_max_right] else if hr : r = 0 then by simp [hr, max_eq_left hnqp, le_max_left] else if hqr : q + r = 0 then le_trans (by simpa [hqr] using hnqp) (le_max_left _ _) else begin unfold padic_norm, split_ifs, apply le_max_iff.2, left, apply fpow_le_of_le, { exact_mod_cast le_of_lt hp.1.one_lt }, { apply neg_le_neg, have : padic_val_rat p q = min (padic_val_rat p q) (padic_val_rat p r), from (min_eq_left h).symm, rw this, apply min_le_padic_val_rat_add; assumption } end /-- The p-adic norm is nonarchimedean: the norm of `p + q` is at most the max of the norm of `p` and the norm of `q`. -/ protected theorem nonarchimedean {q r : β„š} : padic_norm p (q + r) ≀ max (padic_norm p q) (padic_norm p r) := begin wlog hle := le_total (padic_val_rat p q) (padic_val_rat p r) using [q r], exact nonarchimedean_aux p hle end /-- The p-adic norm respects the triangle inequality: the norm of `p + q` is at most the norm of `p` plus the norm of `q`. -/ theorem triangle_ineq (q r : β„š) : padic_norm p (q + r) ≀ padic_norm p q + padic_norm p r := calc padic_norm p (q + r) ≀ max (padic_norm p q) (padic_norm p r) : padic_norm.nonarchimedean p ... ≀ padic_norm p q + padic_norm p r : max_le_add_of_nonneg (padic_norm.nonneg p _) (padic_norm.nonneg p _) /-- The p-adic norm of a difference is at most the max of each component. Restates the archimedean property of the p-adic norm. -/ protected theorem sub {q r : β„š} : padic_norm p (q - r) ≀ max (padic_norm p q) (padic_norm p r) := by rw [sub_eq_add_neg, ←padic_norm.neg p r]; apply padic_norm.nonarchimedean /-- If the p-adic norms of `q` and `r` are different, then the norm of `q + r` is equal to the max of the norms of `q` and `r`. -/ lemma add_eq_max_of_ne {q r : β„š} (hne : padic_norm p q β‰  padic_norm p r) : padic_norm p (q + r) = max (padic_norm p q) (padic_norm p r) := begin wlog hle := le_total (padic_norm p r) (padic_norm p q) using [q r], have hlt : padic_norm p r < padic_norm p q, from lt_of_le_of_ne hle hne.symm, have : padic_norm p q ≀ max (padic_norm p (q + r)) (padic_norm p r), from calc padic_norm p q = padic_norm p (q + r - r) : by congr; ring ... ≀ max (padic_norm p (q + r)) (padic_norm p (-r)) : padic_norm.nonarchimedean p ... = max (padic_norm p (q + r)) (padic_norm p r) : by simp, have hnge : padic_norm p r ≀ padic_norm p (q + r), { apply le_of_not_gt, intro hgt, rw max_eq_right_of_lt hgt at this, apply not_lt_of_ge this, assumption }, have : padic_norm p q ≀ padic_norm p (q + r), by rwa [max_eq_left hnge] at this, apply _root_.le_antisymm, { apply padic_norm.nonarchimedean p }, { rw max_eq_left_of_lt hlt, assumption } end /-- The p-adic norm is an absolute value: positive-definite and multiplicative, satisfying the triangle inequality. -/ instance : is_absolute_value (padic_norm p) := { abv_nonneg := padic_norm.nonneg p, abv_eq_zero := begin intros, constructor; intro, { apply zero_of_padic_norm_eq_zero p, assumption }, { simp [*] } end, abv_add := padic_norm.triangle_ineq p, abv_mul := padic_norm.mul p } variable {p} lemma dvd_iff_norm_le {n : β„•} {z : β„€} : ↑(p^n) ∣ z ↔ padic_norm p z ≀ ↑p ^ (-n : β„€) := begin unfold padic_norm, split_ifs with hz, { norm_cast at hz, have : 0 ≀ (p^n : β„š), {apply pow_nonneg, exact_mod_cast le_of_lt hp.1.pos }, simp [hz, this] }, { rw [fpow_le_iff_le, neg_le_neg_iff, padic_val_rat_of_int _ hp.1.ne_one _], { norm_cast, rw [← enat.coe_le_coe, enat.coe_get, ← multiplicity.pow_dvd_iff_le_multiplicity], simp }, { exact_mod_cast hz }, { exact_mod_cast hp.1.one_lt } } end end padic_norm end padic_norm
0fa3bd22fa536471923368848e350fc400debc5c
7cef822f3b952965621309e88eadf618da0c8ae9
/src/analysis/asymptotics.lean
1f4e54df699bd1c59f8abe87bf97f37f141315d8
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
30,725
lean
/- Copyright (c) 2019 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad -/ import analysis.normed_space.basic /-! # Asymptotics We introduce these relations: `is_O f g l` : "f is big O of g along l" `is_o f g l` : "f is little o of g along l" Here `l` is any filter on the domain of `f` and `g`, which are assumed to be the same. The codomains of `f` and `g` do not need to be the same; all that is needed that there is a norm associated with these types, and it is the norm that is compared asymptotically. Often the ranges of `f` and `g` will be the real numbers, in which case the norm is the absolute value. In general, we have `is_O f g l ↔ is_O (Ξ» x, βˆ₯f xβˆ₯) (Ξ» x, βˆ₯g xβˆ₯) l`, and similarly for `is_o`. But our setup allows us to use the notions e.g. with functions to the integers, rationals, complex numbers, or any normed vector space without mentioning the norm explicitly. If `f` and `g` are functions to a normed field like the reals or complex numbers and `g` is always nonzero, we have `is_o f g l ↔ tendsto (Ξ» x, f x / (g x)) (𝓝 0) l`. In fact, the right-to-left direction holds without the hypothesis on `g`, and in the other direction it suffices to assume that `f` is zero wherever `g` is. (This generalization is useful in defining the FrΓ©chet derivative.) -/ open filter open_locale topological_space namespace asymptotics variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} {Ξ΄ : Type*} {π•œ : Type*} {π•œ' : Type*} section variables [has_norm Ξ²] [has_norm Ξ³] [has_norm Ξ΄] /-- The Landau notation `is_O f g l` where `f` and `g` are two functions on a type `Ξ±` and `l` is a filter on `Ξ±`, means that eventually for `l`, `βˆ₯fβˆ₯` is bounded by a constant multiple of `βˆ₯gβˆ₯`. In other words, `βˆ₯fβˆ₯ / βˆ₯gβˆ₯` is eventually bounded, modulo division by zero issues that are avoided by this definition. -/ def is_O (f : Ξ± β†’ Ξ²) (g : Ξ± β†’ Ξ³) (l : filter Ξ±) : Prop := βˆƒ c > 0, { x | βˆ₯ f x βˆ₯ ≀ c * βˆ₯ g x βˆ₯ } ∈ l /-- The Landau notation `is_o f g l` where `f` and `g` are two functions on a type `Ξ±` and `l` is a filter on `Ξ±`, means that eventually for `l`, `βˆ₯fβˆ₯` is bounded by an arbitrarily small constant multiple of `βˆ₯gβˆ₯`. In other words, `βˆ₯fβˆ₯ / βˆ₯gβˆ₯` tends to `0` along `l`, modulo division by zero issues that are avoided by this definition. -/ def is_o (f : Ξ± β†’ Ξ²) (g : Ξ± β†’ Ξ³) (l : filter Ξ±) : Prop := βˆ€ c > 0, { x | βˆ₯ f x βˆ₯ ≀ c * βˆ₯ g x βˆ₯ } ∈ l theorem is_O_refl (f : Ξ± β†’ Ξ²) (l : filter Ξ±) : is_O f f l := ⟨1, zero_lt_one, by { filter_upwards [univ_mem_sets], intros x _, simp }⟩ theorem is_O.comp {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (hfg : is_O f g l) {Ξ΄ : Type*} (k : Ξ΄ β†’ Ξ±) : is_O (f ∘ k) (g ∘ k) (l.comap k) := let ⟨c, cpos, hfgc⟩ := hfg in ⟨c, cpos, mem_comap_sets.mpr ⟨_, hfgc, set.subset.refl _⟩⟩ theorem is_o.comp {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (hfg : is_o f g l) {Ξ΄ : Type*} (k : Ξ΄ β†’ Ξ±) : is_o (f ∘ k) (g ∘ k) (l.comap k) := Ξ» c cpos, mem_comap_sets.mpr ⟨_, hfg c cpos, set.subset.refl _⟩ theorem is_O.mono {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l₁ lβ‚‚ : filter Ξ±} (h : l₁ ≀ lβ‚‚) (h' : is_O f g lβ‚‚) : is_O f g l₁ := let ⟨c, cpos, h'c⟩ := h' in ⟨c, cpos, h (h'c)⟩ theorem is_o.mono {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l₁ lβ‚‚ : filter Ξ±} (h : l₁ ≀ lβ‚‚) (h' : is_o f g lβ‚‚) : is_o f g l₁ := Ξ» c cpos, h (h' c cpos) theorem is_o.to_is_O {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (hgf : is_o f g l) : is_O f g l := ⟨1, zero_lt_one, hgf 1 zero_lt_one⟩ theorem is_O.trans {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {k : Ξ± β†’ Ξ΄} {l : filter Ξ±} (h₁ : is_O f g l) (hβ‚‚ : is_O g k l) : is_O f k l := let ⟨c, cpos, hc⟩ := h₁, ⟨c', c'pos, hc'⟩ := hβ‚‚ in begin use [c * c', mul_pos cpos c'pos], filter_upwards [hc, hc'], dsimp, intros x h₁x hβ‚‚x, rw mul_assoc, exact le_trans h₁x (mul_le_mul_of_nonneg_left hβ‚‚x (le_of_lt cpos)) end theorem is_o.trans_is_O {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {k : Ξ± β†’ Ξ΄} {l : filter Ξ±} (h₁ : is_o f g l) (hβ‚‚ : is_O g k l) : is_o f k l := begin intros c cpos, rcases hβ‚‚ with ⟨c', c'pos, hc'⟩, have cc'pos := div_pos cpos c'pos, filter_upwards [h₁ (c / c') cc'pos, hc'], dsimp, intros x h₁x hβ‚‚x, refine le_trans h₁x (le_trans (mul_le_mul_of_nonneg_left hβ‚‚x (le_of_lt cc'pos)) _), rw [←mul_assoc, div_mul_cancel _ (ne_of_gt c'pos)] end theorem is_O.trans_is_o {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {k : Ξ± β†’ Ξ΄} {l : filter Ξ±} (h₁ : is_O f g l) (hβ‚‚ : is_o g k l) : is_o f k l := begin intros c cpos, rcases h₁ with ⟨c', c'pos, hc'⟩, have cc'pos := div_pos cpos c'pos, filter_upwards [hc', hβ‚‚ (c / c') cc'pos], dsimp, intros x h₁x hβ‚‚x, refine le_trans h₁x (le_trans (mul_le_mul_of_nonneg_left hβ‚‚x (le_of_lt c'pos)) _), rw [←mul_assoc, mul_div_cancel' _ (ne_of_gt c'pos)] end theorem is_o.trans {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {k : Ξ± β†’ Ξ΄} {l : filter Ξ±} (h₁ : is_o f g l) (hβ‚‚ : is_o g k l) : is_o f k l := h₁.to_is_O.trans_is_o hβ‚‚ theorem is_o_join {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l₁ lβ‚‚ : filter Ξ±} (h₁ : is_o f g l₁) (hβ‚‚ : is_o f g lβ‚‚) : is_o f g (l₁ βŠ” lβ‚‚) := begin intros c cpos, rw mem_sup_sets, exact ⟨h₁ c cpos, hβ‚‚ c cpos⟩ end theorem is_O_congr {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g₁ gβ‚‚ : Ξ± β†’ Ξ³} {l : filter Ξ±} (hf : {x | f₁ x = fβ‚‚ x} ∈ l) (hg : {x | g₁ x = gβ‚‚ x} ∈ l) : is_O f₁ g₁ l ↔ is_O fβ‚‚ gβ‚‚ l := bex_congr $ Ξ» c _, filter.congr_sets $ by filter_upwards [hf, hg] Ξ» x e₁ eβ‚‚, by dsimp at e₁ eβ‚‚ ⊒; rw [e₁, eβ‚‚] theorem is_o_congr {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g₁ gβ‚‚ : Ξ± β†’ Ξ³} {l : filter Ξ±} (hf : {x | f₁ x = fβ‚‚ x} ∈ l) (hg : {x | g₁ x = gβ‚‚ x} ∈ l) : is_o f₁ g₁ l ↔ is_o fβ‚‚ gβ‚‚ l := ball_congr $ Ξ» c _, filter.congr_sets $ by filter_upwards [hf, hg] Ξ» x e₁ eβ‚‚, by dsimp at e₁ eβ‚‚ ⊒; rw [e₁, eβ‚‚] theorem is_O.congr {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g₁ gβ‚‚ : Ξ± β†’ Ξ³} {l : filter Ξ±} (hf : βˆ€ x, f₁ x = fβ‚‚ x) (hg : βˆ€ x, g₁ x = gβ‚‚ x) : is_O f₁ g₁ l β†’ is_O fβ‚‚ gβ‚‚ l := (is_O_congr (univ_mem_sets' hf) (univ_mem_sets' hg)).1 theorem is_o.congr {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g₁ gβ‚‚ : Ξ± β†’ Ξ³} {l : filter Ξ±} (hf : βˆ€ x, f₁ x = fβ‚‚ x) (hg : βˆ€ x, g₁ x = gβ‚‚ x) : is_o f₁ g₁ l β†’ is_o fβ‚‚ gβ‚‚ l := (is_o_congr (univ_mem_sets' hf) (univ_mem_sets' hg)).1 theorem is_O_congr_left {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h : {x | f₁ x = fβ‚‚ x} ∈ l) : is_O f₁ g l ↔ is_O fβ‚‚ g l := is_O_congr h (univ_mem_sets' $ Ξ» _, rfl) theorem is_o_congr_left {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h : {x | f₁ x = fβ‚‚ x} ∈ l) : is_o f₁ g l ↔ is_o fβ‚‚ g l := is_o_congr h (univ_mem_sets' $ Ξ» _, rfl) theorem is_O.congr_left {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (hf : βˆ€ x, f₁ x = fβ‚‚ x) : is_O f₁ g l β†’ is_O fβ‚‚ g l := is_O.congr hf (Ξ» _, rfl) theorem is_o.congr_left {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (hf : βˆ€ x, f₁ x = fβ‚‚ x) : is_o f₁ g l β†’ is_o fβ‚‚ g l := is_o.congr hf (Ξ» _, rfl) theorem is_O_congr_right {f : Ξ± β†’ Ξ²} {g₁ gβ‚‚ : Ξ± β†’ Ξ³} {l : filter Ξ±} (h : {x | g₁ x = gβ‚‚ x} ∈ l) : is_O f g₁ l ↔ is_O f gβ‚‚ l := is_O_congr (univ_mem_sets' $ Ξ» _, rfl) h theorem is_o_congr_right {f : Ξ± β†’ Ξ²} {g₁ gβ‚‚ : Ξ± β†’ Ξ³} {l : filter Ξ±} (h : {x | g₁ x = gβ‚‚ x} ∈ l) : is_o f g₁ l ↔ is_o f gβ‚‚ l := is_o_congr (univ_mem_sets' $ Ξ» _, rfl) h theorem is_O.congr_right {f : Ξ± β†’ Ξ²} {g₁ gβ‚‚ : Ξ± β†’ Ξ³} {l : filter Ξ±} (hg : βˆ€ x, g₁ x = gβ‚‚ x) : is_O f g₁ l β†’ is_O f gβ‚‚ l := is_O.congr (Ξ» _, rfl) hg theorem is_o.congr_right {f : Ξ± β†’ Ξ²} {g₁ gβ‚‚ : Ξ± β†’ Ξ³} {l : filter Ξ±} (hg : βˆ€ x, g₁ x = gβ‚‚ x) : is_o f g₁ l β†’ is_o f gβ‚‚ l := is_o.congr (Ξ» _, rfl) hg end section variables [has_norm Ξ²] [normed_group Ξ³] [normed_group Ξ΄] @[simp] theorem is_O_norm_right {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_O f (Ξ» x, βˆ₯g xβˆ₯) l ↔ is_O f g l := by simp only [is_O, norm_norm] @[simp] theorem is_o_norm_right {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_o f (Ξ» x, βˆ₯g xβˆ₯) l ↔ is_o f g l := by simp only [is_o, norm_norm] @[simp] theorem is_O_neg_right {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_O f (Ξ» x, -(g x)) l ↔ is_O f g l := by { rw ←is_O_norm_right, simp only [norm_neg], rw is_O_norm_right } @[simp] theorem is_o_neg_right {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_o f (Ξ» x, -(g x)) l ↔ is_o f g l := by { rw ←is_o_norm_right, simp only [norm_neg], rw is_o_norm_right } theorem is_O_iff {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_O f g l ↔ βˆƒ c, { x | βˆ₯f xβˆ₯ ≀ c * βˆ₯g xβˆ₯ } ∈ l := suffices (βˆƒ c, { x | βˆ₯f xβˆ₯ ≀ c * βˆ₯g xβˆ₯ } ∈ l) β†’ is_O f g l, from ⟨λ ⟨c, cpos, hc⟩, ⟨c, hc⟩, this⟩, assume ⟨c, hc⟩, or.elim (lt_or_ge 0 c) (assume : c > 0, ⟨c, this, hc⟩) (assume h'c : c ≀ 0, have {x : Ξ± | βˆ₯f xβˆ₯ ≀ 1 * βˆ₯g xβˆ₯} ∈ l, begin filter_upwards [hc], intros x, show βˆ₯f xβˆ₯ ≀ c * βˆ₯g xβˆ₯ β†’ βˆ₯f xβˆ₯ ≀ 1 * βˆ₯g xβˆ₯, { intro hx, apply le_trans hx, apply mul_le_mul_of_nonneg_right _ (norm_nonneg _), show c ≀ 1, from le_trans h'c zero_le_one } end, ⟨1, zero_lt_one, this⟩) theorem is_O_join {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l₁ lβ‚‚ : filter Ξ±} (h₁ : is_O f g l₁) (hβ‚‚ : is_O f g lβ‚‚) : is_O f g (l₁ βŠ” lβ‚‚) := begin rcases h₁ with ⟨c₁, c₁pos, hcβ‚βŸ©, rcases hβ‚‚ with ⟨cβ‚‚, cβ‚‚pos, hcβ‚‚βŸ©, have : 0 < max c₁ cβ‚‚, by { rw lt_max_iff, left, exact c₁pos }, use [max c₁ cβ‚‚, this], rw mem_sup_sets, split, { filter_upwards [hc₁], dsimp, intros x hx, exact le_trans hx (mul_le_mul_of_nonneg_right (le_max_left _ _) (norm_nonneg _)) }, filter_upwards [hcβ‚‚], dsimp, intros x hx, exact le_trans hx (mul_le_mul_of_nonneg_right (le_max_right _ _) (norm_nonneg _)) end lemma is_O.prod_rightl {f : Ξ± β†’ Ξ²} {g₁ : Ξ± β†’ Ξ³} {gβ‚‚ : Ξ± β†’ Ξ΄} {l : filter Ξ±} (h : is_O f g₁ l) : is_O f (Ξ»x, (g₁ x, gβ‚‚ x)) l := begin have : is_O g₁ (Ξ»x, (g₁ x, gβ‚‚ x)) l := ⟨1, zero_lt_one, filter.univ_mem_sets' (Ξ»x, by simp [norm, le_refl])⟩, exact is_O.trans h this end lemma is_O.prod_rightr {f : Ξ± β†’ Ξ²} {g₁ : Ξ± β†’ Ξ³} {gβ‚‚ : Ξ± β†’ Ξ΄} {l : filter Ξ±} (h : is_O f gβ‚‚ l) : is_O f (Ξ»x, (g₁ x, gβ‚‚ x)) l := begin have : is_O gβ‚‚ (Ξ»x, (g₁ x, gβ‚‚ x)) l := ⟨1, zero_lt_one, filter.univ_mem_sets' (Ξ»x, by simp [norm, le_refl])⟩, exact is_O.trans h this end lemma is_o.prod_rightl {f : Ξ± β†’ Ξ²} {g₁ : Ξ± β†’ Ξ³} {gβ‚‚ : Ξ± β†’ Ξ΄} {l : filter Ξ±} (h : is_o f g₁ l) : is_o f (Ξ»x, (g₁ x, gβ‚‚ x)) l := is_o.trans_is_O h (is_O.prod_rightl (is_O_refl g₁ l)) lemma is_o.prod_rightr {f : Ξ± β†’ Ξ²} {g₁ : Ξ± β†’ Ξ³} {gβ‚‚ : Ξ± β†’ Ξ΄} {l : filter Ξ±} (h : is_o f gβ‚‚ l) : is_o f (Ξ»x, (g₁ x, gβ‚‚ x)) l := is_o.trans_is_O h (is_O.prod_rightr (is_O_refl gβ‚‚ l)) end section variables [normed_group Ξ²] [normed_group Ξ΄] [has_norm Ξ³] @[simp] theorem is_O_norm_left {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_O (Ξ» x, βˆ₯f xβˆ₯) g l ↔ is_O f g l := by simp only [is_O, norm_norm] @[simp] theorem is_o_norm_left {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_o (Ξ» x, βˆ₯f xβˆ₯) g l ↔ is_o f g l := by simp only [is_o, norm_norm] @[simp] theorem is_O_neg_left {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_O (Ξ» x, -f x) g l ↔ is_O f g l := by { rw ←is_O_norm_left, simp only [norm_neg], rw is_O_norm_left } @[simp] theorem is_o_neg_left {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_o (Ξ» x, -f x) g l ↔ is_o f g l := by { rw ←is_o_norm_left, simp only [norm_neg], rw is_o_norm_left } theorem is_O.add {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h₁ : is_O f₁ g l) (hβ‚‚ : is_O fβ‚‚ g l) : is_O (Ξ» x, f₁ x + fβ‚‚ x) g l := let ⟨c₁, c₁pos, hcβ‚βŸ© := h₁, ⟨cβ‚‚, cβ‚‚pos, hcβ‚‚βŸ© := hβ‚‚ in begin use [c₁ + cβ‚‚, add_pos c₁pos cβ‚‚pos], filter_upwards [hc₁, hcβ‚‚], intros x hx₁ hxβ‚‚, show βˆ₯f₁ x + fβ‚‚ xβˆ₯ ≀ (c₁ + cβ‚‚) * βˆ₯g xβˆ₯, rw add_mul, exact norm_add_le_of_le hx₁ hxβ‚‚ end theorem is_o.add {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h₁ : is_o f₁ g l) (hβ‚‚ : is_o fβ‚‚ g l) : is_o (Ξ» x, f₁ x + fβ‚‚ x) g l := begin intros c cpos, filter_upwards [h₁ (c / 2) (half_pos cpos), hβ‚‚ (c / 2) (half_pos cpos)], intros x hx₁ hxβ‚‚, dsimp at hx₁ hxβ‚‚, apply le_trans (norm_add_le_of_le hx₁ hxβ‚‚), rw [←mul_add, ←two_mul, ←mul_assoc, div_mul_cancel _ two_ne_zero] end theorem is_O.sub {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h₁ : is_O f₁ g l) (hβ‚‚ : is_O fβ‚‚ g l) : is_O (Ξ» x, f₁ x - fβ‚‚ x) g l := h₁.add (is_O_neg_left.mpr hβ‚‚) theorem is_o.sub {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h₁ : is_o f₁ g l) (hβ‚‚ : is_o fβ‚‚ g l) : is_o (Ξ» x, f₁ x - fβ‚‚ x) g l := h₁.add (is_o_neg_left.mpr hβ‚‚) theorem is_O_comm {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_O (Ξ» x, f₁ x - fβ‚‚ x) g l ↔ is_O (Ξ» x, fβ‚‚ x - f₁ x) g l := by simpa using @is_O_neg_left _ _ _ _ _ (Ξ» x, fβ‚‚ x - f₁ x) g l theorem is_O.symm {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_O (Ξ» x, f₁ x - fβ‚‚ x) g l β†’ is_O (Ξ» x, fβ‚‚ x - f₁ x) g l := is_O_comm.1 theorem is_O.tri {f₁ fβ‚‚ f₃ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h₁ : is_O (Ξ» x, f₁ x - fβ‚‚ x) g l) (hβ‚‚ : is_O (Ξ» x, fβ‚‚ x - f₃ x) g l) : is_O (Ξ» x, f₁ x - f₃ x) g l := (h₁.add hβ‚‚).congr_left (by simp) theorem is_O.congr_of_sub {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h : is_O (Ξ» x, f₁ x - fβ‚‚ x) g l) : is_O f₁ g l ↔ is_O fβ‚‚ g l := ⟨λ h', (h'.sub h).congr_left (Ξ» x, sub_sub_cancel _ _), Ξ» h', (h.add h').congr_left (Ξ» x, sub_add_cancel _ _)⟩ theorem is_o_comm {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_o (Ξ» x, f₁ x - fβ‚‚ x) g l ↔ is_o (Ξ» x, fβ‚‚ x - f₁ x) g l := by simpa using @is_o_neg_left _ _ _ _ _ (Ξ» x, fβ‚‚ x - f₁ x) g l theorem is_o.symm {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_o (Ξ» x, f₁ x - fβ‚‚ x) g l β†’ is_o (Ξ» x, fβ‚‚ x - f₁ x) g l := is_o_comm.1 theorem is_o.tri {f₁ fβ‚‚ f₃ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h₁ : is_o (Ξ» x, f₁ x - fβ‚‚ x) g l) (hβ‚‚ : is_o (Ξ» x, fβ‚‚ x - f₃ x) g l) : is_o (Ξ» x, f₁ x - f₃ x) g l := (h₁.add hβ‚‚).congr_left (by simp) theorem is_o.congr_of_sub {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h : is_o (Ξ» x, f₁ x - fβ‚‚ x) g l) : is_o f₁ g l ↔ is_o fβ‚‚ g l := ⟨λ h', (h'.sub h).congr_left (Ξ» x, sub_sub_cancel _ _), Ξ» h', (h.add h').congr_left (Ξ» x, sub_add_cancel _ _)⟩ @[simp] theorem is_O_prod_left {f₁ : Ξ± β†’ Ξ²} {fβ‚‚ : Ξ± β†’ Ξ΄} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_O (Ξ»x, (f₁ x, fβ‚‚ x)) g l ↔ is_O f₁ g l ∧ is_O fβ‚‚ g l := begin split, { assume h, split, { exact is_O.trans (is_O.prod_rightl (is_O_refl f₁ l)) h }, { exact is_O.trans (is_O.prod_rightr (is_O_refl fβ‚‚ l)) h } }, { rintros ⟨h₁, hβ‚‚βŸ©, have : is_O (Ξ»x, βˆ₯f₁ xβˆ₯ + βˆ₯fβ‚‚ xβˆ₯) g l := is_O.add (is_O_norm_left.2 h₁) (is_O_norm_left.2 hβ‚‚), apply is_O.trans _ this, refine ⟨1, zero_lt_one, filter.univ_mem_sets' (Ξ»x, _)⟩, simp only [norm, max_le_iff, one_mul, set.mem_set_of_eq], split; exact le_trans (by simp) (le_abs_self _) } end @[simp] theorem is_o_prod_left {f₁ : Ξ± β†’ Ξ²} {fβ‚‚ : Ξ± β†’ Ξ΄} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_o (Ξ»x, (f₁ x, fβ‚‚ x)) g l ↔ is_o f₁ g l ∧ is_o fβ‚‚ g l := begin split, { assume h, split, { exact is_O.trans_is_o (is_O.prod_rightl (is_O_refl f₁ l)) h }, { exact is_O.trans_is_o (is_O.prod_rightr (is_O_refl fβ‚‚ l)) h } }, { rintros ⟨h₁, hβ‚‚βŸ©, have : is_o (Ξ»x, βˆ₯f₁ xβˆ₯ + βˆ₯fβ‚‚ xβˆ₯) g l := is_o.add (is_o_norm_left.2 h₁) (is_o_norm_left.2 hβ‚‚), apply is_O.trans_is_o _ this, refine ⟨1, zero_lt_one, filter.univ_mem_sets' (Ξ»x, _)⟩, simp only [norm, max_le_iff, one_mul, set.mem_set_of_eq], split; exact le_trans (by simp) (le_abs_self _) } end end section variables [normed_group Ξ²] [normed_group Ξ³] theorem is_O_zero (g : Ξ± β†’ Ξ³) (l : filter Ξ±) : is_O (Ξ» x, (0 : Ξ²)) g l := ⟨1, zero_lt_one, by { filter_upwards [univ_mem_sets], intros x _, simp }⟩ theorem is_O_refl_left {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_O (Ξ» x, f x - f x) g l := by simpa using is_O_zero g l theorem is_O_zero_right_iff {f : Ξ± β†’ Ξ²} {l : filter Ξ±} : is_O f (Ξ» x, (0 : Ξ³)) l ↔ {x | f x = 0} ∈ l := begin rw [is_O_iff], split, { rintros ⟨c, hc⟩, filter_upwards [hc], dsimp, intro x, rw [norm_zero, mul_zero], intro hx, rw ←norm_eq_zero, exact le_antisymm hx (norm_nonneg _) }, intro h, use 0, filter_upwards [h], dsimp, intros x hx, rw [hx, norm_zero, zero_mul] end theorem is_o_zero (g : Ξ± β†’ Ξ³) (l : filter Ξ±) : is_o (Ξ» x, (0 : Ξ²)) g l := Ξ» c cpos, by { filter_upwards [univ_mem_sets], intros x _, simp, exact mul_nonneg (le_of_lt cpos) (norm_nonneg _)} theorem is_o_refl_left {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} : is_o (Ξ» x, f x - f x) g l := by simpa using is_o_zero g l theorem is_o_zero_right_iff {f : Ξ± β†’ Ξ²} (l : filter Ξ±) : is_o f (Ξ» x, (0 : Ξ³)) l ↔ {x | f x = 0} ∈ l := begin split, { intro h, exact is_O_zero_right_iff.mp h.to_is_O }, intros h c cpos, filter_upwards [h], dsimp, intros x hx, rw [hx, norm_zero, norm_zero, mul_zero] end end section variables [has_norm Ξ²] [normed_field π•œ] open normed_field theorem is_O_const_one (c : Ξ²) (l : filter Ξ±) : is_O (Ξ» x : Ξ±, c) (Ξ» x, (1 : π•œ)) l := begin rw is_O_iff, refine ⟨βˆ₯cβˆ₯, _⟩, simp only [norm_one, mul_one], apply univ_mem_sets', simp [le_refl], end end section variables [normed_field π•œ] [normed_group Ξ³] theorem is_O_const_mul_left {f : Ξ± β†’ π•œ} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h : is_O f g l) (c : π•œ) : is_O (Ξ» x, c * f x) g l := begin cases classical.em (c = 0) with h'' h'', { simp [h''], apply is_O_zero }, rcases h with ⟨c', c'pos, h'⟩, have cne0 : βˆ₯cβˆ₯ β‰  0, from mt (norm_eq_zero _).mp h'', have cpos : βˆ₯cβˆ₯ > 0, from lt_of_le_of_ne (norm_nonneg _) (ne.symm cne0), refine ⟨βˆ₯cβˆ₯ * c', mul_pos cpos c'pos, _⟩, filter_upwards [h'], dsimp, intros x hβ‚€, rw [normed_field.norm_mul, mul_assoc], exact mul_le_mul_of_nonneg_left hβ‚€ (norm_nonneg _) end theorem is_O_const_mul_left_iff {f : Ξ± β†’ π•œ} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} {c : π•œ} (hc : c β‰  0) : is_O (Ξ» x, c * f x) g l ↔ is_O f g l := begin split, { intro h, convert is_O_const_mul_left h c⁻¹, ext, rw [←mul_assoc, inv_mul_cancel hc, one_mul] }, intro h, apply is_O_const_mul_left h end theorem is_o_const_mul_left {f : Ξ± β†’ π•œ} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h : is_o f g l) (c : π•œ) : is_o (Ξ» x, c * f x) g l := begin cases classical.em (c = 0) with h'' h'', { simp [h''], apply is_o_zero }, have cne0 : βˆ₯cβˆ₯ β‰  0, from mt (norm_eq_zero _).mp h'', have cpos : βˆ₯cβˆ₯ > 0, from lt_of_le_of_ne (norm_nonneg _) (ne.symm cne0), intros c' c'pos, dsimp, filter_upwards [h (c' / βˆ₯cβˆ₯) (div_pos c'pos cpos)], dsimp, intros x hx, rw [normed_field.norm_mul], apply le_trans (mul_le_mul_of_nonneg_left hx (le_of_lt cpos)), rw [←mul_assoc, mul_div_cancel' _ cne0] end theorem is_o_const_mul_left_iff {f : Ξ± β†’ π•œ} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} {c : π•œ} (hc : c β‰  0) : is_o (Ξ» x, c * f x) g l ↔ is_o f g l := begin split, { intro h, convert is_o_const_mul_left h c⁻¹, ext, rw [←mul_assoc, inv_mul_cancel hc, one_mul] }, intro h', apply is_o_const_mul_left h' end end section variables [normed_group Ξ²] [normed_field π•œ] theorem is_O_of_is_O_const_mul_right {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ π•œ} {l : filter Ξ±} {c : π•œ} (h : is_O f (Ξ» x, c * g x) l) : is_O f g l := begin cases classical.em (c = 0) with h' h', { simp [h', is_O_zero_right_iff] at h, rw is_O_congr_left h, apply is_O_zero }, have cne0 : βˆ₯cβˆ₯ β‰  0, from mt (norm_eq_zero _).mp h', have cpos : βˆ₯cβˆ₯ > 0, from lt_of_le_of_ne (norm_nonneg _) (ne.symm cne0), rcases h with ⟨c', c'pos, h''⟩, refine ⟨c' * βˆ₯cβˆ₯, mul_pos c'pos cpos, _⟩, convert h'', ext x, dsimp, rw [normed_field.norm_mul, mul_assoc] end theorem is_O_const_mul_right_iff {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ π•œ} {l : filter Ξ±} {c : π•œ} (hc : c β‰  0) : is_O f (Ξ» x, c * g x) l ↔ is_O f g l := begin split, { intro h, exact is_O_of_is_O_const_mul_right h }, intro h, apply is_O_of_is_O_const_mul_right, show is_O f (Ξ» (x : Ξ±), c⁻¹ * (c * g x)) l, convert h, ext, rw [←mul_assoc, inv_mul_cancel hc, one_mul] end theorem is_o_of_is_o_const_mul_right {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ π•œ} {l : filter Ξ±} {c : π•œ} (h : is_o f (Ξ» x, c * g x) l) : is_o f g l := begin cases classical.em (c = 0) with h' h', { simp [h', is_o_zero_right_iff] at h, rw is_o_congr_left h, apply is_o_zero }, have cne0 : βˆ₯cβˆ₯ β‰  0, from mt (norm_eq_zero _).mp h', have cpos : βˆ₯cβˆ₯ > 0, from lt_of_le_of_ne (norm_nonneg _) (ne.symm cne0), intros c' c'pos, convert h (c' / βˆ₯cβˆ₯) (div_pos c'pos cpos), dsimp, ext x, rw [normed_field.norm_mul, ←mul_assoc, div_mul_cancel _ cne0] end theorem is_o_const_mul_right {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ π•œ} {l : filter Ξ±} {c : π•œ} (hc : c β‰  0) : is_o f (Ξ» x, c * g x) l ↔ is_o f g l := begin split, { intro h, exact is_o_of_is_o_const_mul_right h }, intro h, apply is_o_of_is_o_const_mul_right, show is_o f (Ξ» (x : Ξ±), c⁻¹ * (c * g x)) l, convert h, ext, rw [←mul_assoc, inv_mul_cancel hc, one_mul] end theorem is_o_one_iff {f : Ξ± β†’ Ξ²} {l : filter Ξ±} : is_o f (Ξ» x, (1 : π•œ)) l ↔ tendsto f l (𝓝 0) := begin rw [normed_group.tendsto_nhds_zero, is_o], split, { intros h e epos, filter_upwards [h (e / 2) (half_pos epos)], simp, intros x hx, exact lt_of_le_of_lt hx (half_lt_self epos) }, intros h e epos, filter_upwards [h e epos], simp, intros x hx, exact le_of_lt hx end theorem is_O_one_of_tendsto {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {y : Ξ²} (h : tendsto f l (𝓝 y)) : is_O f (Ξ» x, (1 : π•œ)) l := begin have Iy : βˆ₯yβˆ₯ < βˆ₯yβˆ₯ + 1 := lt_add_one _, refine ⟨βˆ₯yβˆ₯ + 1, lt_of_le_of_lt (norm_nonneg _) Iy, _⟩, simp only [mul_one, normed_field.norm_one], have : tendsto (Ξ»x, βˆ₯f xβˆ₯) l (𝓝 βˆ₯yβˆ₯) := (continuous_norm.tendsto _).comp h, exact this (ge_mem_nhds Iy) end end section variables [normed_group Ξ²] [normed_group Ξ³] theorem is_O.trans_tendsto {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h₁ : is_O f g l) (hβ‚‚ : tendsto g l (𝓝 0)) : tendsto f l (𝓝 0) := (@is_o_one_iff _ _ ℝ _ _ _ _).1 $ h₁.trans_is_o $ is_o_one_iff.2 hβ‚‚ theorem is_o.trans_tendsto {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h₁ : is_o f g l) : tendsto g l (𝓝 0) β†’ tendsto f l (𝓝 0) := h₁.to_is_O.trans_tendsto end section variables [normed_field π•œ] [normed_field π•œ'] theorem is_O_mul {f₁ fβ‚‚ : Ξ± β†’ π•œ} {g₁ gβ‚‚ : Ξ± β†’ π•œ'} {l : filter Ξ±} (h₁ : is_O f₁ g₁ l) (hβ‚‚ : is_O fβ‚‚ gβ‚‚ l) : is_O (Ξ» x, f₁ x * fβ‚‚ x) (Ξ» x, g₁ x * gβ‚‚ x) l := begin rcases h₁ with ⟨c₁, c₁pos, hcβ‚βŸ©, rcases hβ‚‚ with ⟨cβ‚‚, cβ‚‚pos, hcβ‚‚βŸ©, refine ⟨c₁ * cβ‚‚, mul_pos c₁pos cβ‚‚pos, _⟩, filter_upwards [hc₁, hcβ‚‚], dsimp, intros x hx₁ hxβ‚‚, rw [normed_field.norm_mul, normed_field.norm_mul, mul_assoc, mul_left_comm cβ‚‚, ←mul_assoc], exact mul_le_mul hx₁ hxβ‚‚ (norm_nonneg _) (mul_nonneg (le_of_lt c₁pos) (norm_nonneg _)) end theorem is_o_mul_left {f₁ fβ‚‚ : Ξ± β†’ π•œ} {g₁ gβ‚‚ : Ξ± β†’ π•œ'} {l : filter Ξ±} (h₁ : is_O f₁ g₁ l) (hβ‚‚ : is_o fβ‚‚ gβ‚‚ l) : is_o (Ξ» x, f₁ x * fβ‚‚ x) (Ξ» x, g₁ x * gβ‚‚ x) l := begin intros c cpos, rcases h₁ with ⟨c₁, c₁pos, hcβ‚βŸ©, filter_upwards [hc₁, hβ‚‚ (c / c₁) (div_pos cpos c₁pos)], dsimp, intros x hx₁ hxβ‚‚, rw [normed_field.norm_mul, normed_field.norm_mul], apply le_trans (mul_le_mul hx₁ hxβ‚‚ (norm_nonneg _) (mul_nonneg (le_of_lt c₁pos) (norm_nonneg _))), rw [mul_comm c₁, mul_assoc, mul_left_comm c₁, ←mul_assoc _ c₁, div_mul_cancel _ (ne_of_gt c₁pos)], rw [mul_left_comm] end theorem is_o_mul_right {f₁ fβ‚‚ : Ξ± β†’ π•œ} {g₁ gβ‚‚ : Ξ± β†’ π•œ'} {l : filter Ξ±} (h₁ : is_o f₁ g₁ l) (hβ‚‚ : is_O fβ‚‚ gβ‚‚ l) : is_o (Ξ» x, f₁ x * fβ‚‚ x) (Ξ» x, g₁ x * gβ‚‚ x) l := by convert is_o_mul_left hβ‚‚ h₁; simp only [mul_comm] theorem is_o_mul {f₁ fβ‚‚ : Ξ± β†’ π•œ} {g₁ gβ‚‚ : Ξ± β†’ π•œ'} {l : filter Ξ±} (h₁ : is_o f₁ g₁ l) (hβ‚‚ : is_o fβ‚‚ gβ‚‚ l) : is_o (Ξ» x, f₁ x * fβ‚‚ x) (Ξ» x, g₁ x * gβ‚‚ x) l := is_o_mul_left h₁.to_is_O hβ‚‚ end /- Note: the theorems in the next two sections can also be used for the integers, since scalar multiplication is multiplication. -/ section variables [normed_field π•œ] [normed_group Ξ²] [normed_space π•œ Ξ²] [normed_group Ξ³] set_option class.instance_max_depth 43 theorem is_O_const_smul_left {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h : is_O f g l) (c : π•œ) : is_O (Ξ» x, c β€’ f x) g l := begin rw [←is_O_norm_left], simp only [norm_smul], apply is_O_const_mul_left, rw [is_O_norm_left], apply h end theorem is_O_const_smul_left_iff {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} {c : π•œ} (hc : c β‰  0) : is_O (Ξ» x, c β€’ f x) g l ↔ is_O f g l := begin have cne0 : βˆ₯cβˆ₯ β‰  0, from mt (norm_eq_zero _).mp hc, rw [←is_O_norm_left], simp only [norm_smul], rw [is_O_const_mul_left_iff cne0, is_O_norm_left] end theorem is_o_const_smul_left {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h : is_o f g l) (c : π•œ) : is_o (Ξ» x, c β€’ f x) g l := begin rw [←is_o_norm_left], simp only [norm_smul], apply is_o_const_mul_left, rw [is_o_norm_left], apply h end theorem is_o_const_smul_left_iff {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} {c : π•œ} (hc : c β‰  0) : is_o (Ξ» x, c β€’ f x) g l ↔ is_o f g l := begin have cne0 : βˆ₯cβˆ₯ β‰  0, from mt (norm_eq_zero _).mp hc, rw [←is_o_norm_left], simp only [norm_smul], rw [is_o_const_mul_left_iff cne0, is_o_norm_left] end end section variables [normed_group Ξ²] [normed_field π•œ] [normed_group Ξ³] [normed_space π•œ Ξ³] set_option class.instance_max_depth 43 theorem is_O_const_smul_right {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} {c : π•œ} (hc : c β‰  0) : is_O f (Ξ» x, c β€’ g x) l ↔ is_O f g l := begin have cne0 : βˆ₯cβˆ₯ β‰  0, from mt (norm_eq_zero _).mp hc, rw [←is_O_norm_right], simp only [norm_smul], rw [is_O_const_mul_right_iff cne0, is_O_norm_right] end theorem is_o_const_smul_right {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} {c : π•œ} (hc : c β‰  0) : is_o f (Ξ» x, c β€’ g x) l ↔ is_o f g l := begin have cne0 : βˆ₯cβˆ₯ β‰  0, from mt (norm_eq_zero _).mp hc, rw [←is_o_norm_right], simp only [norm_smul], rw [is_o_const_mul_right cne0, is_o_norm_right] end end section variables [normed_field π•œ] [normed_group Ξ²] [normed_space π•œ Ξ²] [normed_group Ξ³] [normed_space π•œ Ξ³] set_option class.instance_max_depth 43 theorem is_O_smul {k : Ξ± β†’ π•œ} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h : is_O f g l) : is_O (Ξ» x, k x β€’ f x) (Ξ» x, k x β€’ g x) l := begin rw [←is_O_norm_left, ←is_O_norm_right], simp only [norm_smul], apply is_O_mul (is_O_refl _ _), rw [is_O_norm_left, is_O_norm_right], exact h end theorem is_o_smul {k : Ξ± β†’ π•œ} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {l : filter Ξ±} (h : is_o f g l) : is_o (Ξ» x, k x β€’ f x) (Ξ» x, k x β€’ g x) l := begin rw [←is_o_norm_left, ←is_o_norm_right], simp only [norm_smul], apply is_o_mul_left (is_O_refl _ _), rw [is_o_norm_left, is_o_norm_right], exact h end end section variables [normed_field π•œ] theorem tendsto_nhds_zero_of_is_o {f g : Ξ± β†’ π•œ} {l : filter Ξ±} (h : is_o f g l) : tendsto (Ξ» x, f x / (g x)) l (𝓝 0) := have eq₁ : is_o (Ξ» x, f x / g x) (Ξ» x, g x / g x) l, from is_o_mul_right h (is_O_refl _ _), have eqβ‚‚ : is_O (Ξ» x, g x / g x) (Ξ» x, (1 : π•œ)) l, begin use [1, zero_lt_one], filter_upwards [univ_mem_sets], simp, intro x, cases classical.em (βˆ₯g xβˆ₯ = 0) with h' h'; simp [h'], exact zero_le_one end, is_o_one_iff.mp (eq₁.trans_is_O eqβ‚‚) private theorem is_o_of_tendsto {f g : Ξ± β†’ π•œ} {l : filter Ξ±} (hgf : βˆ€ x, g x = 0 β†’ f x = 0) (h : tendsto (Ξ» x, f x / (g x)) l (𝓝 0)) : is_o f g l := have eq₁ : is_o (Ξ» x, f x / (g x)) (Ξ» x, (1 : π•œ)) l, from is_o_one_iff.mpr h, have eqβ‚‚ : is_o (Ξ» x, f x / g x * g x) g l, by convert is_o_mul_right eq₁ (is_O_refl _ _); simp, have eq₃ : is_O f (Ξ» x, f x / g x * g x) l, begin use [1, zero_lt_one], refine filter.univ_mem_sets' (assume x, _), suffices : βˆ₯f xβˆ₯ ≀ βˆ₯f xβˆ₯ / βˆ₯g xβˆ₯ * βˆ₯g xβˆ₯, { simpa }, by_cases g x = 0, { simp only [h, hgf x h, norm_zero, mul_zero] }, { rw [div_mul_cancel], exact mt (norm_eq_zero _).1 h } end, eq₃.trans_is_o eqβ‚‚ theorem is_o_iff_tendsto {f g : Ξ± β†’ π•œ} {l : filter Ξ±} (hgf : βˆ€ x, g x = 0 β†’ f x = 0) : is_o f g l ↔ tendsto (Ξ» x, f x / (g x)) l (𝓝 0) := iff.intro tendsto_nhds_zero_of_is_o (is_o_of_tendsto hgf) theorem is_o_pow_pow {m n : β„•} (h : m < n) : is_o (Ξ»(x : π•œ), x^n) (Ξ»x, x^m) (𝓝 0) := begin let p := n - m, have nmp : n = m + p := (nat.add_sub_cancel' (le_of_lt h)).symm, have : (Ξ»(x : π•œ), x^m) = (Ξ»x, x^m * 1), by simp, simp only [this, pow_add, nmp], refine is_o_mul_left (is_O_refl _ _) (is_o_one_iff.2 _), convert (continuous_pow p).tendsto (0 : π•œ), exact (zero_pow (nat.sub_pos_of_lt h)).symm end theorem is_o_pow_id {n : β„•} (h : 1 < n) : is_o (Ξ»(x : π•œ), x^n) (Ξ»x, x) (𝓝 0) := by { convert is_o_pow_pow h, simp } end end asymptotics
ef813cd47ba2e305c68a6d5ed44b1849c97d32e6
d406927ab5617694ec9ea7001f101b7c9e3d9702
/archive/imo/imo2005_q3.lean
2c4acffbb7a1159802a96a67ebf6438efd7cfb2b
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
2,806
lean
/- Copyright (c) 2021 Manuel Candales. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Manuel Candales -/ import data.real.basic import tactic.positivity /-! # IMO 2005 Q3 Let `x`, `y` and `z` be positive real numbers such that `xyz β‰₯ 1`. Prove that: `(x^5 - x^2)/(x^5 + y^2 + z^2) + (y^5 - y^2)/(y^5 + z^2 + x^2) + (z^5 - z^2)/(z^5 + x^2 + y^2) β‰₯ 0` # Solution The solution by Iurie Boreico from Moldova is presented, which won a special prize during the exam. The key insight is that `(x^5-x^2)/(x^5+y^2+z^2) β‰₯ (x^2-y*z)/(x^2+y^2+z^2)`, which is proven by factoring `(x^5-x^2)/(x^5+y^2+z^2) - (x^5-x^2)/(x^3*(x^2+y^2+z^2))` into a non-negative expression and then making use of `xyz β‰₯ 1` to show `(x^5-x^2)/(x^3*(x^2+y^2+z^2)) β‰₯ (x^2-y*z)/(x^2+y^2+z^2)`. -/ lemma key_insight (x y z : ℝ) (hx : x > 0) (hy : y > 0) (hz : z > 0) (h : x*y*z β‰₯ 1) : (x^5-x^2)/(x^5+y^2+z^2) β‰₯ (x^2-y*z)/(x^2+y^2+z^2) := begin have h₁ : 0 < x^5+y^2+z^2 := by positivity, have hβ‚‚ : 0 < x^3 := by positivity, have h₃ : 0 < x^2+y^2+z^2 := by positivity, have hβ‚„ : 0 < x^3*(x^2+y^2+z^2) := by positivity, have key : (x^5-x^2)/(x^5+y^2+z^2) - (x^5-x^2)/(x^3*(x^2+y^2+z^2)) = ((x^3 - 1)^2*x^2*(y^2 + z^2))/((x^5+y^2+z^2)*(x^3*(x^2+y^2+z^2))), { field_simp [h₁.ne', hβ‚„.ne'], ring }, have hβ‚… : ((x^3 - 1)^2*x^2*(y^2 + z^2))/((x^5+y^2+z^2)*(x^3*(x^2+y^2+z^2))) β‰₯ 0 := by positivity, calc (x^5-x^2)/(x^5+y^2+z^2) β‰₯ (x^5-x^2)/(x^3*(x^2+y^2+z^2)) : by linarith [key, hβ‚…] ... β‰₯ (x^5-x^2*(x*y*z))/(x^3*(x^2+y^2+z^2)) : by { refine (div_le_div_right hβ‚„).mpr _, simp, exact (le_mul_iff_one_le_right (pow_pos hx 2)).mpr h } ... = (x^2-y*z)/(x^2+y^2+z^2) : by { field_simp [hβ‚‚.ne', h₃.ne'], ring }, end theorem imo2005_q3 (x y z : ℝ) (hx : x > 0) (hy : y > 0) (hz : z > 0) (h : x*y*z β‰₯ 1) : (x^5-x^2)/(x^5+y^2+z^2) + (y^5-y^2)/(y^5+z^2+x^2) + (z^5-z^2)/(z^5+x^2+y^2) β‰₯ 0 := begin calc (x^5-x^2)/(x^5+y^2+z^2) + (y^5-y^2)/(y^5+z^2+x^2) + (z^5-z^2)/(z^5+x^2+y^2) β‰₯ (x^2-y*z)/(x^2+y^2+z^2) + (y^2-z*x)/(y^2+z^2+x^2) + (z^2-x*y)/(z^2+x^2+y^2) : by { linarith [key_insight x y z hx hy hz h, key_insight y z x hy hz hx (by linarith [h]), key_insight z x y hz hx hy (by linarith [h])] } ... = 1/2*( (x-y)^2 + (y-z)^2 + (z-x)^2 ) / (x^2+y^2+z^2) : by { have h₁ : y^2+z^2+x^2 = x^2+y^2+z^2, ring, have hβ‚‚ : z^2+x^2+y^2 = x^2+y^2+z^2, ring, rw [h₁, hβ‚‚], ring } ... β‰₯ 0 : by { exact div_nonneg (by linarith [sq_nonneg (x-y), sq_nonneg (y-z), sq_nonneg (z-x)]) (by linarith [sq_nonneg x, sq_nonneg y, sq_nonneg z]) }, end
e62e6139f0a1fcb1edc2e8bc964759c2e7369aba
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/hott/types/int/hott.hlean
073439408ef25e6ce7189e43aa99d4e8c376e34e
[ "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
5,816
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 Theorems about the integers specific to HoTT -/ import .basic types.eq arity algebra.bundled open core eq is_equiv equiv equiv.ops algebra is_trunc open nat (hiding pred) namespace int section open algebra definition group_integers : Group := Group.mk β„€ (group_of_add_group _) end definition is_equiv_succ [instance] : is_equiv succ := adjointify succ pred (Ξ»a, !add_sub_cancel) (Ξ»a, !sub_add_cancel) definition equiv_succ : β„€ ≃ β„€ := equiv.mk succ _ definition is_equiv_neg [instance] : is_equiv (neg : β„€ β†’ β„€) := adjointify neg neg (Ξ»x, !neg_neg) (Ξ»a, !neg_neg) definition equiv_neg : β„€ ≃ β„€ := equiv.mk neg _ definition iterate {A : Type} (f : A ≃ A) (a : β„€) : A ≃ A := rec_nat_on a erfl (Ξ»b g, f ⬝e g) (Ξ»b g, g ⬝e f⁻¹ᡉ) -- definition iterate_trans {A : Type} (f : A ≃ A) (a : β„€) -- : iterate f a ⬝e f = iterate f (a + 1) := -- sorry -- definition trans_iterate {A : Type} (f : A ≃ A) (a : β„€) -- : f ⬝e iterate f a = iterate f (a + 1) := -- sorry -- definition iterate_trans_symm {A : Type} (f : A ≃ A) (a : β„€) -- : iterate f a ⬝e f⁻¹e = iterate f (a - 1) := -- sorry -- definition symm_trans_iterate {A : Type} (f : A ≃ A) (a : β„€) -- : f⁻¹e ⬝e iterate f a = iterate f (a - 1) := -- sorry -- definition iterate_neg {A : Type} (f : A ≃ A) (a : β„€) -- : iterate f (-a) = (iterate f a)⁻¹e := -- rec_nat_on a idp -- (Ξ»n p, calc -- iterate f (-succ n) = iterate f (-n) ⬝e f⁻¹e : rec_nat_on_neg -- ... = (iterate f n)⁻¹e ⬝e f⁻¹e : by rewrite p -- ... = (f ⬝e iterate f n)⁻¹e : sorry -- ... = (iterate f (succ n))⁻¹e : idp) -- sorry -- definition iterate_add {A : Type} (f : A ≃ A) (a b : β„€) -- : iterate f (a + b) = equiv.trans (iterate f a) (iterate f b) := -- sorry -- definition iterate_sub {A : Type} (f : A ≃ A) (a b : β„€) -- : iterate f (a - b) = equiv.trans (iterate f a) (equiv.symm (iterate f b)) := -- sorry -- definition iterate_mul {A : Type} (f : A ≃ A) (a b : β„€) -- : iterate f (a * b) = iterate (iterate f a) b := -- sorry end int open int namespace eq variables {A : Type} {a : A} (p : a = a) (b c : β„€) (n : β„•) definition power : a = a := rec_nat_on b idp (Ξ»c q, q ⬝ p) (Ξ»c q, q ⬝ p⁻¹) --iterate (equiv_eq_closed_right p a) b idp -- definition power_neg_succ (n : β„•) : power p (-succ n) = power p (-n) ⬝ p⁻¹ := -- !rec_nat_on_neg -- local attribute nat.add int.add int.of_num nat.of_num int.succ [constructor] definition power_con : power p b ⬝ p = power p (succ b) := rec_nat_on b idp (Ξ»n IH, idp) (Ξ»n IH, calc power p (-succ n) ⬝ p = (power p (-int.of_nat n) ⬝ p⁻¹) ⬝ p : by rewrite [↑power,-rec_nat_on_neg] ... = power p (-int.of_nat n) : inv_con_cancel_right ... = power p (succ (-succ n)) : by rewrite -succ_neg_succ) definition power_con_inv : power p b ⬝ p⁻¹ = power p (pred b) := rec_nat_on b idp (Ξ»n IH, calc power p (succ n) ⬝ p⁻¹ = power p n : by apply con_inv_cancel_right ... = power p (pred (succ n)) : by rewrite pred_nat_succ) (Ξ»n IH, calc power p (-int.of_nat (succ n)) ⬝ p⁻¹ = power p (-int.of_nat (succ (succ n))) : by rewrite [↑power,-rec_nat_on_neg] ... = power p (pred (-succ n)) : by rewrite -neg_succ) definition con_power : p ⬝ power p b = power p (succ b) := rec_nat_on b ( by rewrite ↑[power];exact !idp_con⁻¹) ( Ξ»n IH, proof calc p ⬝ power p (succ n) = (p ⬝ power p n) ⬝ p : con.assoc p _ p ... = power p (succ (succ n)) : by rewrite IH qed) ( Ξ»n IH, calc p ⬝ power p (-int.of_nat (succ n)) = p ⬝ (power p (-int.of_nat n) ⬝ p⁻¹) : by rewrite [↑power, rec_nat_on_neg] ... = (p ⬝ power p (-int.of_nat n)) ⬝ p⁻¹ : con.assoc ... = power p (succ (-int.of_nat n)) ⬝ p⁻¹ : by rewrite IH ... = power p (pred (succ (-int.of_nat n))) : power_con_inv ... = power p (succ (-int.of_nat (succ n))) : by rewrite [succ_neg_nat_succ,int.pred_succ]) definition inv_con_power : p⁻¹ ⬝ power p b = power p (pred b) := rec_nat_on b ( by rewrite ↑[power];exact !idp_con⁻¹) (Ξ»n IH, calc p⁻¹ ⬝ power p (succ n) = p⁻¹ ⬝ power p n ⬝ p : con.assoc ... = power p (pred n) ⬝ p : by rewrite IH ... = power p (succ (pred n)) : power_con ... = power p (pred (succ n)) : by rewrite [succ_pred,-int.pred_succ n]) ( Ξ»n IH, calc p⁻¹ ⬝ power p (-int.of_nat (succ n)) = p⁻¹ ⬝ (power p (-int.of_nat n) ⬝ p⁻¹) : by rewrite [↑power,rec_nat_on_neg] ... = (p⁻¹ ⬝ power p (-int.of_nat n)) ⬝ p⁻¹ : con.assoc ... = power p (pred (-int.of_nat n)) ⬝ p⁻¹ : by rewrite IH ... = power p (-int.of_nat (succ n)) ⬝ p⁻¹ : by rewrite -neg_succ ... = power p (-succ (succ n)) : by rewrite [↑power,-rec_nat_on_neg] ... = power p (pred (-succ n)) : by rewrite -neg_succ) definition power_con_power : Ξ (b : β„€), power p b ⬝ power p c = power p (b + c) := rec_nat_on c (Ξ»b, by rewrite int.add_zero) (Ξ»n IH b, by rewrite [-con_power,-con.assoc,power_con,IH,↑succ,add.assoc, add.comm (int.of_nat n)]) (Ξ»n IH b, by rewrite [neg_nat_succ,-inv_con_power,-con.assoc,power_con_inv,IH,↑pred, +sub_eq_add_neg,add.assoc,add.comm (-n)]) end eq
1e7c5f86eeb2e64fb0b2837b414e0528dcc58c97
737dc4b96c97368cb66b925eeea3ab633ec3d702
/src/Lean/Meta/Tactic/Simp/Rewrite.lean
9e49b85c5e95f99dd85b07babc3c0f9a03fce0b9
[ "Apache-2.0" ]
permissive
Bioye97/lean4
1ace34638efd9913dc5991443777b01a08983289
bc3900cbb9adda83eed7e6affeaade7cfd07716d
refs/heads/master
1,690,589,820,211
1,631,051,000,000
1,631,067,598,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,275
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.SynthInstance import Lean.Meta.Tactic.Simp.Types namespace Lean.Meta.Simp def synthesizeArgs (lemmaName : Name) (xs : Array Expr) (bis : Array BinderInfo) (discharge? : Expr β†’ SimpM (Option Expr)) : SimpM Bool := do for x in xs, bi in bis do let type ← inferType x if bi.isInstImplicit then unless (← synthesizeInstance x type) do return false else if (← instantiateMVars x).isMVar then if (← isProp type) then match (← discharge? type) with | some proof => unless (← isDefEq x proof) do trace[Meta.Tactic.simp.discharge] "{lemmaName}, failed to assign proof{indentExpr type}" return false | none => trace[Meta.Tactic.simp.discharge] "{lemmaName}, failed to discharge hypotheses{indentExpr type}" return false else if (← isClass? type).isSome then unless (← synthesizeInstance x type) do return false return true where synthesizeInstance (x type : Expr) : SimpM Bool := do match (← trySynthInstance type) with | LOption.some val => if (← isDefEq x val) then return true else trace[Meta.Tactic.simp.discharge] "{lemmaName}, failed to assign instance{indentExpr type}" return false | _ => trace[Meta.Tactic.simp.discharge] "{lemmaName}, failed to synthesize instance{indentExpr type}" return false def tryLemma? (e : Expr) (lemma : SimpLemma) (discharge? : Expr β†’ SimpM (Option Expr)) : SimpM (Option Result) := withNewMCtxDepth do let val ← lemma.getValue let type ← inferType val let (xs, bis, type) ← forallMetaTelescopeReducing type let type ← whnf (← instantiateMVars type) let lhs := type.appFn!.appArg! if (← isDefEq lhs e) then unless (← synthesizeArgs lemma.getName xs bis discharge?) do return none let proof ← instantiateMVars (mkAppN val xs) if ← hasAssignableMVar proof then trace[Meta.Tactic.simp.rewrite] "{lemma}, has unassigned metavariables after unification" return none let rhs ← instantiateMVars type.appArg! if e == rhs then return none if lemma.perm && !Expr.lt rhs e then trace[Meta.Tactic.simp.rewrite] "{lemma}, perm rejected {e} ==> {rhs}" return none trace[Meta.Tactic.simp.rewrite] "{lemma}, {e} ==> {rhs}" return some { expr := rhs, proof? := proof } else unless lhs.isMVar do -- We do not report unification failures when `lhs` is a metavariable -- Example: `x = ()` -- TODO: reconsider if we want lemmas such as `(x : Unit) β†’ x = ()` trace[Meta.Tactic.simp.unify] "{lemma}, failed to unify {lhs} with {e}" return none /- Remark: the parameter tag is used for creating trace messages. It is irrelevant otherwise. -/ def rewrite (e : Expr) (s : DiscrTree SimpLemma) (erased : Std.PHashSet Name) (discharge? : Expr β†’ SimpM (Option Expr)) (tag : String) : SimpM Result := do let lemmas ← s.getMatch e if lemmas.isEmpty then trace[Debug.Meta.Tactic.simp] "no theorems found for {tag}-rewriting {e}" return { expr := e } else let lemmas := lemmas.insertionSort fun e₁ eβ‚‚ => e₁.priority < eβ‚‚.priority for lemma in lemmas do unless inErasedSet lemma do if let some result ← tryLemma? e lemma discharge? then return result return { expr := e } where inErasedSet (lemma : SimpLemma) : Bool := match lemma.name? with | none => false | some name => erased.contains name def rewriteCtorEq? (e : Expr) : MetaM (Option Result) := withReducibleAndInstances do match e.eq? with | none => return none | some (_, lhs, rhs) => let lhs ← whnf lhs let rhs ← whnf rhs let env ← getEnv match lhs.constructorApp? env, rhs.constructorApp? env with | some (c₁, _), some (cβ‚‚, _) => if c₁.name != cβ‚‚.name then withLocalDeclD `h e fun h => return some { expr := mkConst ``False, proof? := (← mkEqFalse' (← mkLambdaFVars #[h] (← mkNoConfusion (mkConst ``False) h))) } else return none | _, _ => return none @[inline] def tryRewriteCtorEq (e : Expr) (x : SimpM Step) : SimpM Step := do match (← rewriteCtorEq? e) with | some r => return Step.done r | none => x def rewriteUsingDecide? (e : Expr) : MetaM (Option Result) := withReducibleAndInstances do if e.hasFVar || e.hasMVar || e.isConstOf ``True || e.isConstOf ``False then return none else try let d ← mkDecide e let r ← withDefault <| whnf d if r.isConstOf ``true then return some { expr := mkConst ``True, proof? := mkAppN (mkConst ``eq_true_of_decide) #[e, d.appArg!, (← mkEqRefl (mkConst ``true))] } else if r.isConstOf ``false then let h ← mkEqRefl d return some { expr := mkConst ``False, proof? := mkAppN (mkConst ``eq_false_of_decide) #[e, d.appArg!, (← mkEqRefl (mkConst ``false))] } else return none catch _ => return none @[inline] def tryRewriteUsingDecide (e : Expr) (x : SimpM Step) : SimpM Step := do if (← read).config.decide then match (← rewriteUsingDecide? e) with | some r => return Step.done r | none => x else x def rewritePre (e : Expr) (discharge? : Expr β†’ SimpM (Option Expr)) : SimpM Step := do let lemmas ← (← read).simpLemmas return Step.visit (← rewrite e lemmas.pre lemmas.erased discharge? (tag := "pre")) def rewritePost (e : Expr) (discharge? : Expr β†’ SimpM (Option Expr)) : SimpM Step := do let lemmas ← (← read).simpLemmas return Step.visit (← rewrite e lemmas.post lemmas.erased discharge? (tag := "post")) def preDefault (e : Expr) (discharge? : Expr β†’ SimpM (Option Expr)) : SimpM Step := tryRewriteCtorEq e <| rewritePre e discharge? def postDefault (e : Expr) (discharge? : Expr β†’ SimpM (Option Expr)) : SimpM Step := do -- TODO: try equation lemmas tryRewriteCtorEq e <| tryRewriteUsingDecide e <| rewritePost e discharge? end Lean.Meta.Simp
5af573c9725d5b11bfc1e615725a4a833c65608a
ad3e8f15221a986da27c99f371922c0b3f5792b6
/src/week04/e01_groups.lean
1bf6cf11a9a62ca4ad5fbefff9a8851585d243ac
[]
no_license
VArtem/lean-itmo
a0e1424c8cc4c2de2ac85ab6fd4a12d80e9b85f1
dc44cd06f9f5b984d051831b3aaa7364e64c2dc4
refs/heads/main
1,683,761,214,467
1,622,821,295,000
1,622,821,295,000
357,236,048
12
0
null
null
null
null
UTF-8
Lean
false
false
8,184
lean
import tactic -- Π­Ρ‚ΠΎΡ‚ Ρ„Π°ΠΉΠ» ΠΏΠ΅Ρ€Π΅Π²Π΅Π΄Π΅Π½ ΠΈ Π°Π΄Π°ΠΏΡ‚ΠΈΡ€ΠΎΠ²Π°Π½ ΠΈΠ· Π²Ρ‚ΠΎΡ€ΠΎΠΉ Π½Π΅Π΄Π΅Π»ΠΈ курса Formalising Mathematics: https://github.com/ImperialCollegeLondon/formalising-mathematics/tree/master/src/week_2 -- Π’Π°ΠΌ ΠΆΠ΅ Π΅ΡΡ‚ΡŒ ΠΏΡ€ΠΎΠ΄ΠΎΠ»ΠΆΠ΅Π½ΠΈΠ΅ ΠΏΡ€ΠΎ ΠΏΠΎΠ΄Π³Ρ€ΡƒΠΏΠΏΡ‹, скачайтС Ρ€Π΅ΠΏΠΎΠ·ΠΈΡ‚ΠΎΡ€ΠΈΠΉ с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ -- `leanproject get ImperialCollegeLondon/formalising-mathematics` -- И ΡΠ°ΠΌΠΎΡΡ‚ΠΎΡΡ‚Π΅Π»ΡŒΠ½ΠΎ ΠΏΡ€ΠΎΠΉΠ΄ΠΈΡ‚Π΅ Ρ‚ΠΎ, Ρ‡Ρ‚ΠΎ Ρ‚Π°ΠΌ Π΅ΡΡ‚ΡŒ namespace itmo.lean /- ΠžΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠΌ Π³Ρ€ΡƒΠΏΠΏΡƒ ΠΊΠ°ΠΊ тайпкласс (ΠΏΠΎΠ΄Ρ€ΠΎΠ±Π½Π΅Π΅: https://leanprover.github.io/theorem_proving_in_lean/type_classes.html), Ρ€Π°ΡΡˆΠΈΡ€ΡΡŽΡ‰ΠΈΠΉ `has_mul`, `has_one` ΠΈ `has_inv`. `has_mul G` ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ Π½Π° `G` ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΎ ΡƒΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅ `* : G β†’ G β†’ G` `has_one G` ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ Π² `G` Π΅ΡΡ‚ΡŒ Π΅Π΄ΠΈΠ½ΠΈΡ†Π° `1 : G` `has_inv G` ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ Π΅ΡΡ‚ΡŒ функция, Π²Ρ‹Π΄Π°ΡŽΡ‰Π°Ρ ΠΎΠ±Ρ€Π°Ρ‚Π½Ρ‹ΠΉ для элСмСнта `G`: `⁻¹ : G β†’ G` ВсС эти опрСдСлСния - просто нотация, свойства ΠΈ взаимодСйствиС этих Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ Π½Π°Π΄ΠΎ Π±ΡƒΠ΄Π΅Ρ‚ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ. ΠžΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠΌ класс `group` с аксиомами Π³Ρ€ΡƒΠΏΠΏ: -/ class group (G : Type) extends has_mul G, has_one G, has_inv G := (mul_assoc : βˆ€ (a b c : G), a * b * c = a * (b * c)) (one_mul : βˆ€ (a : G), 1 * a = a) (mul_left_inv : βˆ€ (a : G), a⁻¹ * a = 1) /- Π€ΠΎΡ€ΠΌΠ°Π»ΡŒΠ½ΠΎ, `group G` это структура, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Π΅ΡΡ‚ΡŒ ΡƒΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅, 1, ΠΎΠ±Ρ€Π°Ρ‚Π½Ρ‹ΠΉ элСмСнт, ΠΈ Π΄ΠΎΠΊΠ°Π·Π°Ρ‚Π΅Π»ΡŒΡΡ‚Π²Π° аксиом Π³Ρ€ΡƒΠΏΠΏ. ΠŸΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ `group G` это тайпкласс, Ρ‚ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ Π² сигнатуру Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ "ΠΏΡƒΡΡ‚ΡŒ `G` - Π³Ρ€ΡƒΠΏΠΏΠ°" Π½ΡƒΠΆΠ½ΠΎ, ΠΊΠ°ΠΊ `(G : Type) [group G]`. ΠšΠ²Π°Π΄Ρ€Π°Ρ‚Π½Ρ‹Π΅ скобки ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡŽΡ‚ΡΡ для тайпклассов ΠΈ автоматичСски (насколько получится) находятся ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΡ‚Π²ΡƒΡŽΡ‰ΠΈΠ΅ инстансы для `G`. ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, Ρ‡Ρ‚ΠΎ Π½Π°Π±ΠΎΡ€ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Π½Π½Ρ‹Ρ… аксиом ΠΌΠΈΠ½ΠΈΠΌΠ°Π»Π΅Π½, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, Π½Π΅Ρ‚ аксиом `mul_one : βˆ€ (a : G), a * 1 = a` ΠΈ `mul_right_inv : βˆ€ (a : G), a * a⁻¹ = 1`. На самом Π΄Π΅Π»Π΅, эти аксиомы ΡΠ»Π΅Π΄ΡƒΡŽΡ‚ ΠΈΠ· основных, ΠΈ ΠΌΡ‹ это Π΄ΠΎΠΊΠ°ΠΆΠ΅ΠΌ. -/ namespace group variables {G : Type} [group G] lemma mul_left_cancel (a b c : G) (Habac : a * b = a * c) : b = c := begin sorry, end -- Π”ΠΎΠΊΠ°Π·Π°Ρ‚Π΅Π»ΡŒΡΡ‚Π²Π° ΠΈΠ· Π΄Π»ΠΈΠ½Π½Ρ‹Ρ… Ρ†Π΅ΠΏΠΎΡ‡Π΅ΠΊ равСнств ΠΈΠ»ΠΈ нСравСнств часто ΠΌΠΎΠΆΠ½ΠΎ Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π½Π° `calc` (справка ΠΏΠΎ `calc`: https://leanprover-community.github.io/extras/calc.html) -- ΠŸΡ€ΠΈΠΌΠ΅Ρ€ Ρ€Π΅ΡˆΠ΅Π½ΠΈΡ Π·Π°Π΄Π°Ρ‡ΠΈ с IMO 2020 с использованиСм `calc`: https://github.com/leanprover-community/mathlib/blob/master/archive/imo/imo2020_q2.lean example (a b c : G) (Habac : a * b = a * c) : b = c := begin calc b = 1 * b : by rw one_mul ... = (a⁻¹ * a) * b : by rw ← mul_left_inv a ... = a⁻¹ * (a * b) : by rw ← mul_assoc ... = a⁻¹ * (a * c) : by rw Habac ... = (a⁻¹ * a) * c : by rw ← mul_assoc ... = 1 * c : by rw mul_left_inv a ... = c : by rw one_mul, end lemma mul_eq_of_eq_inv_mul {a x y : G} (h : x = a⁻¹ * y) : a * x = y := begin sorry, end variables (a b c x y : G) /- ΠŸΠΎΠΏΡ€ΠΎΠ±ΡƒΠΉΡ‚Π΅ Π½Π°Ρ‡Π°Ρ‚ΡŒ Π΄ΠΎΠΊΠ°Π·Π°Ρ‚Π΅Π»ΡŒΡΡ‚Π²ΠΎ с `apply mul_eq_of_eq_inv_mul`. -/ @[simp] theorem mul_one : a * 1 = a := begin sorry, end @[simp] theorem mul_right_inv : a * a⁻¹ = 1 := begin sorry, end /- Π₯ΠΎΡ€ΠΎΡˆΠ΅ΠΉ ΠΈΠ΄Π΅Π΅ΠΉ Π±Ρ‹Π»ΠΎ Π±Ρ‹ Π½Π°ΡƒΡ‡ΠΈΡ‚ΡŒ `simp` Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ с Π»Π΅ΠΌΠΌΠ°ΠΌΠΈ Π²ΠΈΠ΄Π° `A = B` ΠΈΠ»ΠΈ `A ↔ B`, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΌΡ‹ описали Π²Ρ‹ΡˆΠ΅. Для этого ΠΌΡ‹ написали Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚ `@[simp]` ΠΏΠ΅Ρ€Π΅Π΄ Π»Π΅ΠΌΠΌΠΎΠΉ. Π’ ΠΈΠ΄Π΅Π°Π»Π΅, хочСтся, Ρ‡Ρ‚ΠΎΠ±Ρ‹ равСнства Π² Π³Ρ€ΡƒΠΏΠΏΠ°Ρ… Ρ€Π΅ΡˆΠ°Π»ΠΈΡΡŒ автоматичСски: `example : (a * b) * 1⁻¹⁻¹ * b⁻¹ * (a⁻¹ * a⁻¹⁻¹⁻¹) * a = 1` Π’Π°ΠΊΡ‚ΠΈΠΊΠ° `simp` Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΡ‹Ρ‚Π°Ρ‚ΡŒΡΡ "ΡƒΠΏΡ€ΠΎΡΡ‚ΠΈΡ‚ΡŒ" Ρ†Π΅Π»ΡŒ ΠΊΠ°ΠΊ ΠΌΠΎΠΆΠ½ΠΎ большС, Π² ΠΈΠ΄Π΅Π°Π»Π΅ Π·Π°ΠΊΡ€Ρ‹Ρ‚ΡŒ Ρ†Π΅Π»ΡŒ с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ извСстных Π΅ΠΉ Π»Π΅ΠΌΠΌ. `simp` Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ пСрСписываниями, Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ пСрСписывания слСва Π½Π°ΠΏΡ€Π°Π²ΠΎ (Ссли хочСтся ΠΈΠ·ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π½Π°ΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅, ΠΌΠΎΠΆΠ½ΠΎ Π΄Π°Ρ‚ΡŒ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ `simp [← h] at ...`). ΠŸΠΎΡΡ‚ΠΎΠΌΡƒ, Π² тоТдСствах, ΠΏΠΎΠΌΠ΅Ρ‡Π΅Π½Π½Ρ‹Ρ… `simp`, правая Ρ‡Π°ΡΡ‚ΡŒ Π΄ΠΎΠ»ΠΆΠ½Π° Π±Ρ‹Ρ‚ΡŒ "ΠΏΡ€ΠΎΡ‰Π΅", Ρ‡Π΅ΠΌ лСвая. Π’ частности, `add_comm (a b : β„•) : a + b = b + a` - плохая Π»Π΅ΠΌΠΌΠ° для `simp`. ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, Ρ‡Ρ‚ΠΎ Π²ΠΎ всСх Π»Π΅ΠΌΠΌΠ°Ρ… Π²Ρ‹ΡˆΠ΅, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, `@[simp] theorem mul_one (a : G) : a * 1 = a` `@[simp] theorem mul_right_inv (a : G) : a * a⁻¹ = 1` правая Ρ‡Π°ΡΡ‚ΡŒ ΠΏΡ€ΠΎΡ‰Π΅ Π»Π΅Π²ΠΎΠΉ. ΠŸΠΎΠΌΠ΅Ρ‡Π°Ρ‚ΡŒ равСнство `a = a * 1` Ρ‚Π΅Π³ΠΎΠΌ `@[simp]` - плохая идСя. Π£ΠΆΠ΅ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½Π½Ρ‹ΠΌ функциям (ΠΈΠ»ΠΈ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½Π½Ρ‹ΠΌ Π² Π΄Ρ€ΡƒΠ³ΠΎΠΌ Ρ„Π°ΠΉΠ»Π΅) ΠΌΠΎΠΆΠ½ΠΎ рСтроспСктивно ΠΏΡ€ΠΎΡΡ‚Π°Π²ΠΈΡ‚ΡŒ Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚Ρ‹ Π²Ρ€ΡƒΡ‡Π½ΡƒΡŽ. -/ attribute [simp] one_mul mul_left_inv mul_assoc /- Научим `simp` ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠ΅ ΠΏΡΡ‚ΡŒ Π»Π΅ΠΌΠΌ: `inv_mul_cancel_left : a⁻¹ * (a * b) = b` `mul_inv_cancel_left : a * (a⁻¹ * b) = b` `inv_mul : (a * b)⁻¹ = b⁻¹ * a⁻¹` `one_inv : (1 : G)⁻¹ = 1` `inv_inv : (a⁻¹)⁻¹ = a` ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, Ρ‡Ρ‚ΠΎ Π²Π΅Π·Π΄Π΅ правая Ρ‡Π°ΡΡ‚ΡŒ "ΠΏΡ€ΠΎΡ‰Π΅" Π»Π΅Π²ΠΎΠΉ. -/ @[simp] lemma inv_mul_cancel_left : a⁻¹ * (a * b) = b := begin rw ← mul_assoc, -- Π—Π΄Π΅ΡΡŒ ΡƒΠΆΠ΅ `simp` смоТСт ΠΏΠ΅Ρ€Π΅ΠΏΠΈΡΠ°Ρ‚ΡŒ `a⁻¹ * a = 1` ΠΈ `1 * b = b` simp, end @[simp] lemma mul_inv_cancel_left : a * (a⁻¹ * b) = b := begin sorry, end @[simp] lemma inv_mul : (a * b)⁻¹ = b⁻¹ * a⁻¹ := begin sorry, end @[simp] lemma one_inv : (1 : G)⁻¹ = 1 := begin sorry, end @[simp] lemma inv_inv : a ⁻¹ ⁻¹ = a := begin sorry, end /- Π­Ρ‚ΠΈ 5 Π»Π΅ΠΌΠΌ Π²Ρ‹Π±Ρ€Π°Π½Ρ‹ нСспроста: это Π²Ρ‹Π²ΠΎΠ΄ Π°Π»Π³ΠΎΡ€ΠΈΡ‚ΠΌΠ° ΠšΠ½ΡƒΡ‚Π°-БСндикса для пСрСписываний Π² Π³Ρ€ΡƒΠΏΠΏΠ΅: https://en.wikipedia.org/wiki/Word_problem_(mathematics)#Example:_A_term_rewriting_system_to_decide_the_word_problem_in_the_free_group -/ -- Π‘Π»ΠΎΠΆΠ½Ρ‹ΠΉ ΠΏΡ€ΠΈΠΌΠ΅Ρ€ Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Ρ€Π΅ΡˆΠ°Π΅Ρ‚ΡΡ автоматичСски example : (a * b) * 1⁻¹⁻¹ * b⁻¹ * (a⁻¹ * a⁻¹⁻¹⁻¹) * a = 1 := by simp lemma eq_mul_inv_of_mul_eq {a b c : G} (h : a * c = b) : a = b * c⁻¹ := begin sorry, end lemma eq_inv_mul_of_mul_eq {a b c : G} (h : b * a = c) : a = b⁻¹ * c := begin sorry, end lemma mul_left_eq_self {a b : G} : a * b = b ↔ a = 1 := begin sorry, end lemma mul_right_eq_self {a b : G} : a * b = a ↔ b = 1 := begin sorry, end lemma eq_inv_of_mul_eq_one {a b : G} (h : a * b = 1) : a = b⁻¹ := calc a = a * 1 : by rw mul_one ... = b⁻¹ : by sorry lemma inv_eq_of_mul_eq_one {a b : G} (h : a * b = 1) : a⁻¹ = b := begin sorry, end lemma unique_left_id {e : G} (h : βˆ€ x : G, e * x = x) : e = 1 := begin sorry, end lemma unique_right_inv {a b : G} (h : a * b = 1) : b = a⁻¹ := begin sorry, end lemma mul_left_cancel_iff (a x y : G) : a * x = a * y ↔ x = y := begin sorry, end lemma mul_right_cancel (a x y : G) (Habac : x * a = y * a) : x = y := calc x = x * 1 : by rw mul_one ... = y : by sorry @[simp] theorem inv_inj_iff {a b : G}: a⁻¹ = b⁻¹ ↔ a = b := begin sorry, end theorem inv_eq {a b : G}: a⁻¹ = b ↔ b⁻¹ = a := begin sorry, end end group end itmo.lean
a38bf96861259926ab30bf0f9abb44baeafa4dfe
aa5a655c05e5359a70646b7154e7cac59f0b4132
/stage0/src/Init/Control/Except.lean
a5ca4ae4fd95b6497eba0a7aa6991df43a53a7c6
[ "Apache-2.0" ]
permissive
lambdaxymox/lean4
ae943c960a42247e06eff25c35338268d07454cb
278d47c77270664ef29715faab467feac8a0f446
refs/heads/master
1,677,891,867,340
1,612,500,005,000
1,612,500,005,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,137
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jared Roesch, Sebastian Ullrich The Except monad transformer. -/ prelude import Init.Control.Basic import Init.Control.Id universes u v w u' namespace Except variable {Ξ΅ : Type u} @[inline] protected def pure {Ξ± : Type v} (a : Ξ±) : Except Ξ΅ Ξ± := Except.ok a @[inline] protected def map {Ξ± Ξ² : Type v} (f : Ξ± β†’ Ξ²) : Except Ξ΅ Ξ± β†’ Except Ξ΅ Ξ² | Except.error err => Except.error err | Except.ok v => Except.ok <| f v @[inline] protected def mapError {Ξ΅' : Type u} {Ξ± : Type v} (f : Ξ΅ β†’ Ξ΅') : Except Ξ΅ Ξ± β†’ Except Ξ΅' Ξ± | Except.error err => Except.error <| f err | Except.ok v => Except.ok v @[inline] protected def bind {Ξ± Ξ² : Type v} (ma : Except Ξ΅ Ξ±) (f : Ξ± β†’ Except Ξ΅ Ξ²) : Except Ξ΅ Ξ² := match ma with | Except.error err => Except.error err | Except.ok v => f v @[inline] protected def toBool {Ξ± : Type v} : Except Ξ΅ Ξ± β†’ Bool | Except.ok _ => true | Except.error _ => false @[inline] protected def toOption {Ξ± : Type v} : Except Ξ΅ Ξ± β†’ Option Ξ± | Except.ok a => some a | Except.error _ => none @[inline] protected def tryCatch {Ξ± : Type u} (ma : Except Ξ΅ Ξ±) (handle : Ξ΅ β†’ Except Ξ΅ Ξ±) : Except Ξ΅ Ξ± := match ma with | Except.ok a => Except.ok a | Except.error e => handle e instance : Monad (Except Ξ΅) where pure := Except.pure bind := Except.bind map := Except.map end Except def ExceptT (Ξ΅ : Type u) (m : Type u β†’ Type v) (Ξ± : Type u) : Type v := m (Except Ξ΅ Ξ±) @[inline] def ExceptT.mk {Ξ΅ : Type u} {m : Type u β†’ Type v} {Ξ± : Type u} (x : m (Except Ξ΅ Ξ±)) : ExceptT Ξ΅ m Ξ± := x @[inline] def ExceptT.run {Ξ΅ : Type u} {m : Type u β†’ Type v} {Ξ± : Type u} (x : ExceptT Ξ΅ m Ξ±) : m (Except Ξ΅ Ξ±) := x namespace ExceptT variable {Ξ΅ : Type u} {m : Type u β†’ Type v} [Monad m] @[inline] protected def pure {Ξ± : Type u} (a : Ξ±) : ExceptT Ξ΅ m Ξ± := ExceptT.mk <| pure (Except.ok a) @[inline] protected def bindCont {Ξ± Ξ² : Type u} (f : Ξ± β†’ ExceptT Ξ΅ m Ξ²) : Except Ξ΅ Ξ± β†’ m (Except Ξ΅ Ξ²) | Except.ok a => f a | Except.error e => pure (Except.error e) @[inline] protected def bind {Ξ± Ξ² : Type u} (ma : ExceptT Ξ΅ m Ξ±) (f : Ξ± β†’ ExceptT Ξ΅ m Ξ²) : ExceptT Ξ΅ m Ξ² := ExceptT.mk <| ma >>= ExceptT.bindCont f @[inline] protected def map {Ξ± Ξ² : Type u} (f : Ξ± β†’ Ξ²) (x : ExceptT Ξ΅ m Ξ±) : ExceptT Ξ΅ m Ξ² := ExceptT.mk <| x >>= fun a => match a with | (Except.ok a) => pure <| Except.ok (f a) | (Except.error e) => pure <| Except.error e @[inline] protected def lift {Ξ± : Type u} (t : m Ξ±) : ExceptT Ξ΅ m Ξ± := ExceptT.mk <| Except.ok <$> t instance : MonadLift (Except Ξ΅) (ExceptT Ξ΅ m) := ⟨fun e => ExceptT.mk <| pure e⟩ instance : MonadLift m (ExceptT Ξ΅ m) := ⟨ExceptT.lift⟩ @[inline] protected def tryCatch {Ξ± : Type u} (ma : ExceptT Ξ΅ m Ξ±) (handle : Ξ΅ β†’ ExceptT Ξ΅ m Ξ±) : ExceptT Ξ΅ m Ξ± := ExceptT.mk <| ma >>= fun res => match res with | Except.ok a => pure (Except.ok a) | Except.error e => (handle e) instance : MonadFunctor m (ExceptT Ξ΅ m) := ⟨fun f x => f x⟩ instance : Monad (ExceptT Ξ΅ m) where pure := ExceptT.pure bind := ExceptT.bind map := ExceptT.map @[inline] protected def adapt {Ξ΅' Ξ± : Type u} (f : Ξ΅ β†’ Ξ΅') : ExceptT Ξ΅ m Ξ± β†’ ExceptT Ξ΅' m Ξ± := fun x => ExceptT.mk <| Except.mapError f <$> x end ExceptT instance (m : Type u β†’ Type v) (Ρ₁ : Type u) (Ξ΅β‚‚ : Type u) [Monad m] [MonadExceptOf Ρ₁ m] : MonadExceptOf Ρ₁ (ExceptT Ξ΅β‚‚ m) where throw e := ExceptT.mk <| throwThe Ρ₁ e tryCatch x handle := ExceptT.mk <| tryCatchThe Ρ₁ x handle instance (m : Type u β†’ Type v) (Ξ΅ : Type u) [Monad m] : MonadExceptOf Ξ΅ (ExceptT Ξ΅ m) where throw e := ExceptT.mk <| pure (Except.error e) tryCatch := ExceptT.tryCatch instance [Monad m] [Inhabited Ξ΅] : Inhabited (ExceptT Ξ΅ m Ξ±) where default := throw arbitrary instance (Ξ΅) : MonadExceptOf Ξ΅ (Except Ξ΅) where throw := Except.error tryCatch := Except.tryCatch namespace MonadExcept variable {Ξ΅ : Type u} {m : Type v β†’ Type w} /-- Alternative orelse operator that allows to select which exception should be used. The default is to use the first exception since the standard `orelse` uses the second. -/ @[inline] def orelse' [MonadExcept Ξ΅ m] {Ξ± : Type v} (t₁ tβ‚‚ : m Ξ±) (useFirstEx := true) : m Ξ± := tryCatch t₁ fun e₁ => tryCatch tβ‚‚ fun eβ‚‚ => throw (if useFirstEx = true then e₁ else eβ‚‚) end MonadExcept @[inline] def observing {Ξ΅ Ξ± : Type u} {m : Type u β†’ Type v} [Monad m] [MonadExcept Ξ΅ m] (x : m Ξ±) : m (Except Ξ΅ Ξ±) := tryCatch (do let a ← x; pure (Except.ok a)) (fun ex => pure (Except.error ex)) instance (Ξ΅ : Type u) (m : Type u β†’ Type v) [Monad m] : MonadControl m (ExceptT Ξ΅ m) where stM := Except Ξ΅ liftWith f := liftM <| f fun x => x.run restoreM x := x class MonadFinally (m : Type u β†’ Type v) where tryFinally' {Ξ± Ξ²} : m Ξ± β†’ (Option Ξ± β†’ m Ξ²) β†’ m (Ξ± Γ— Ξ²) export MonadFinally (tryFinally') /-- Execute `x` and then execute `finalizer` even if `x` threw an exception -/ @[inline] abbrev tryFinally {m : Type u β†’ Type v} {Ξ± Ξ² : Type u} [MonadFinally m] [Functor m] (x : m Ξ±) (finalizer : m Ξ²) : m Ξ± := let y := tryFinally' x (fun _ => finalizer) (Β·.1) <$> y instance Id.finally : MonadFinally Id where tryFinally' := fun x h => let a := x let b := h (some x) pure (a, b) instance ExceptT.finally {m : Type u β†’ Type v} {Ξ΅ : Type u} [MonadFinally m] [Monad m] : MonadFinally (ExceptT Ξ΅ m) where tryFinally' := fun x h => ExceptT.mk do let r ← tryFinally' x fun e? => match e? with | some (Except.ok a) => h (some a) | _ => h none match r with | (Except.ok a, Except.ok b) => pure (Except.ok (a, b)) | (_, Except.error e) => pure (Except.error e) -- second error has precedence | (Except.error e, _) => pure (Except.error e)
60f7f3dbe97fec7d133048e073fec14a47532b3e
3618c6e11aa822fd542440674dfb9a7b9921dba0
/src/exp_sum_eq_zero.lean
9de7090921b99993d1a22e2301a0067dc3b6c914
[]
no_license
ChrisHughes24/single_relation
99ceedcc02d236ce46d6c65d72caa669857533c5
057e157a59de6d0e43b50fcb537d66792ec20450
refs/heads/master
1,683,652,062,698
1,683,360,089,000
1,683,360,089,000
279,346,432
0
0
null
null
null
null
UTF-8
Lean
false
false
1,693
lean
import HNN_normalize import cyclically_reduce /-! # Exp sum equal to zero The case when there is a `t` in the relation with exponent sum equal to zero ## Main definitions The main definition is `exp_sum_eq_zero`, which solves the word problem in the case when there is a `t` with exponent_sum 0 in `r`. -/ open free_group multiplicative P semidirect_product variables {ΞΉ : Type} [decidable_eq ΞΉ] (T : set ΞΉ) [decidable_pred T] /-- `exp_sum_eq_zero T t x hs cyc_r` solves the word problem for `cyc_r` and `T` when `t` has exponent sum zero in `cyc_r`, `cyc_r` is cyclically reduced and `t ∈ T β†’ x βˆ‰ T` and `x β‰  t` -/ @[inline] meta def exp_sum_eq_zero (t x : ΞΉ) (hs : Ξ  (r : free_group (ΞΉ Γ— C∞)) (T : set (ΞΉ Γ— C∞)) [decidable_pred T], solver r T) (cyc_r : free_group ΞΉ) : solver cyc_r T := Ξ» w, let (cβ‚‚, conj_r) := cyclically_conjugate x cyc_r in let r' := (add_subscript t conj_r).left in let (a, b) := min_max_subscript x r' in do (n, p) ← HNN_normalize' t x r' a b hs w, if t ∈ T then let T' : set (ΞΉ Γ— C∞) := { i : ΞΉ Γ— C∞ | i.1 ∈ T } in do np ← hs r' T' p.right, return (change_r cβ‚‚ --(inr (of' t n) * P.map (remove_subscript t) undefined (P.trans p np))) --The line for HNN_normalize (P.map (remove_subscript t) undefined (P.trans p np)) * inr (of' t n)) --The line for HNN_normalize' -- case t βˆ‰ T else -- Kind of silly. Could check if n = 1 before normalization. guard (n = 1) >> let T' : set (ΞΉ Γ— C∞) := { i : ΞΉ Γ— C∞ | i.1 ∈ T ∧ i.2 = 1 } in do np ← hs r' T' p.right, return (change_r cβ‚‚ (P.map (remove_subscript t) undefined (P.trans p np)))
16127dab440e1d7e9021068e24880202ab25c1c3
9d2e3d5a2e2342a283affd97eead310c3b528a24
/src/solutions/thursday/afternoon/category_theory/exercise7.lean
8b01590b8a498a4ed38e1fce7e915949cee66af7
[]
permissive
Vtec234/lftcm2020
ad2610ab614beefe44acc5622bb4a7fff9a5ea46
bbbd4c8162f8c2ef602300ab8fdeca231886375d
refs/heads/master
1,668,808,098,623
1,594,989,081,000
1,594,990,079,000
280,423,039
0
0
MIT
1,594,990,209,000
1,594,990,209,000
null
UTF-8
Lean
false
false
2,191
lean
import category_theory.monoidal.category import algebra.category.CommRing.basic /-! Let's define the category of monoid objects in a monoidal category. -/ open category_theory variables (C : Type*) [category C] [monoidal_category C] structure Mon_in := (X : C) (ΞΉ : πŸ™_ C ⟢ X) (ΞΌ : X βŠ— X ⟢ X) -- There are three missing axioms here! -- Use `Ξ»_ X`, `ρ_ X` and `Ξ±_ X Y Z` for unitors and associators. -- sorry (ΞΌ_ΞΉ : (Ξ»_ X).inv ≫ (ΞΉ βŠ— πŸ™ X) ≫ ΞΌ = πŸ™ X) (ΞΉ_ΞΌ : (ρ_ X).inv ≫ (πŸ™ X βŠ— ΞΉ) ≫ ΞΌ = πŸ™ X) (ΞΌ_assoc : (Ξ±_ X X X).hom ≫ (πŸ™ X βŠ— ΞΌ) ≫ ΞΌ = (ΞΌ βŠ— πŸ™ X) ≫ ΞΌ) -- sorry namespace Mon_in variables {C} @[ext] structure hom (M N : Mon_in C) := -- sorry (hom : M.X ⟢ N.X) (ΞΉ_hom' : M.ΞΉ ≫ hom = N.ΞΉ . obviously) (ΞΌ_hom' : M.ΞΌ ≫ hom = (hom βŠ— hom) ≫ N.ΞΌ . obviously) -- sorry -- omit restate_axiom hom.ΞΉ_hom' restate_axiom hom.ΞΌ_hom' attribute [simp, reassoc] hom.ΞΉ_hom hom.ΞΌ_hom @[simps] def id (M : Mon_in C) : hom M M := { hom := πŸ™ M.X, } @[simps] def comp {M N O : Mon_in C} (f : hom M N) (g : hom N O) : hom M O := { hom := f.hom ≫ g.hom, } -- omit instance : category (Mon_in C) := -- sorry { hom := Ξ» M N, hom M N, id := id, comp := Ξ» M N O f g, comp f g, } -- sorry end Mon_in /-! Bonus projects (all but the first will be non-trivial with today's mathlib): * Construct the category of module objects for a fixed monoid object. * Check that `Mon_in Type β‰Œ Mon`. * Check that `Mon_in Mon β‰Œ CommMon`, via the Eckmann-Hilton argument. (You'll have to hook up the cartesian monoidal structure on `Mon` first.) * Check that `Mon_in AddCommGroup β‰Œ Ring`. (You'll have to hook up the monoidal structure on `AddCommGroup`. Currently we have the monoidal structure on `Module R`; perhaps one could specialize to `R = β„€` and transport the monoidal structure across an equivalence? This sounds like some work!) * Check that `Mon_in (Module R) β‰Œ Algebra R`. * Show that if `C` is braided (you'll have to define that first!) then `Mon_in C` is naturally monoidal. * Can you transport this monoidal structure to `Ring` or `Algebra R`? How does it compare to the "native" one? -/
3aa79133f3af17dffd41cbabf6e7e5bdcba67ea8
05b503addd423dd68145d68b8cde5cd595d74365
/src/data/real/nnreal.lean
1ee328fdeae0e02b84720b9ee7b1a5b41cf67d81
[ "Apache-2.0" ]
permissive
aestriplex/mathlib
77513ff2b176d74a3bec114f33b519069788811d
e2fa8b2b1b732d7c25119229e3cdfba8370cb00f
refs/heads/master
1,621,969,960,692
1,586,279,279,000
1,586,279,279,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
25,011
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin Nonnegative real numbers. -/ import data.real.basic order.lattice algebra.field noncomputable theory open_locale classical /-- Nonnegative real numbers. -/ 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 : nnreal) : n.val = n := rfl instance : can_lift ℝ nnreal := { coe := coe, cond := Ξ» r, r β‰₯ 0, prf := Ξ» x hx, ⟨⟨x, hx⟩, rfl⟩ } 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 def of_real (r : ℝ) : ℝβ‰₯0 := ⟨max r 0, le_max_right _ _⟩ lemma coe_of_real (r : ℝ) (hr : 0 ≀ r) : (nnreal.of_real r : ℝ) = r := max_eq_left hr lemma le_coe_of_real (r : ℝ) : r ≀ nnreal.of_real r := le_max_left r 0 lemma coe_nonneg (r : nnreal) : (0 : ℝ) ≀ r := r.2 @[elim_cast, simp, nolint simp_nf] -- takes a crazy amount of time simplify lhs theorem coe_mk (a : ℝ) (ha) : ((⟨a, ha⟩ : ℝβ‰₯0) : ℝ) = a := rfl instance : has_zero ℝβ‰₯0 := ⟨⟨0, le_refl 0⟩⟩ instance : has_one ℝβ‰₯0 := ⟨⟨1, zero_le_one⟩⟩ instance : has_add ℝβ‰₯0 := ⟨λa b, ⟨a + b, add_nonneg a.2 b.2⟩⟩ instance : has_sub ℝβ‰₯0 := ⟨λa b, nnreal.of_real (a - b)⟩ instance : has_mul ℝβ‰₯0 := ⟨λa b, ⟨a * b, mul_nonneg a.2 b.2⟩⟩ instance : has_inv ℝβ‰₯0 := ⟨λa, ⟨(a.1)⁻¹, inv_nonneg.2 a.2⟩⟩ instance : has_div ℝβ‰₯0 := ⟨λa b, ⟨a.1 / b.1, div_nonneg' a.2 b.2⟩⟩ instance : has_le ℝβ‰₯0 := ⟨λ r s, (r:ℝ) ≀ s⟩ instance : has_bot ℝβ‰₯0 := ⟨0⟩ instance : inhabited ℝβ‰₯0 := ⟨0⟩ @[simp] protected lemma coe_zero : ((0 : ℝβ‰₯0) : ℝ) = 0 := rfl @[simp] protected lemma coe_one : ((1 : ℝβ‰₯0) : ℝ) = 1 := rfl @[simp, move_cast] protected lemma coe_add (r₁ rβ‚‚ : ℝβ‰₯0) : ((r₁ + rβ‚‚ : ℝβ‰₯0) : ℝ) = r₁ + rβ‚‚ := rfl @[simp, move_cast] protected lemma coe_mul (r₁ rβ‚‚ : ℝβ‰₯0) : ((r₁ * rβ‚‚ : ℝβ‰₯0) : ℝ) = r₁ * rβ‚‚ := rfl @[simp, move_cast] protected lemma coe_div (r₁ rβ‚‚ : ℝβ‰₯0) : ((r₁ / rβ‚‚ : ℝβ‰₯0) : ℝ) = r₁ / rβ‚‚ := rfl @[simp, move_cast] protected lemma coe_inv (r : ℝβ‰₯0) : ((r⁻¹ : ℝβ‰₯0) : ℝ) = r⁻¹ := rfl @[simp] 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] protected lemma zero_div (r : ℝβ‰₯0) : 0 / r = 0 := nnreal.eq (zero_div _) @[simp, elim_cast] protected lemma coe_eq_zero (r : ℝβ‰₯0) : ↑r = (0 : ℝ) ↔ r = 0 := @nnreal.eq_iff r 0 @[elim_cast] lemma coe_ne_zero {r : ℝβ‰₯0} : (r : ℝ) β‰  0 ↔ r β‰  0 := by simp instance : comm_semiring ℝβ‰₯0 := begin refine { zero := 0, add := (+), one := 1, mul := (*), ..}; { intros; apply nnreal.eq; simp [mul_comm, mul_assoc, add_comm_monoid.add, left_distrib, right_distrib, add_comm_monoid.zero, add_comm, add_left_comm] } end instance : is_semiring_hom (coe : ℝβ‰₯0 β†’ ℝ) := by refine_struct {..}; intros; refl instance : comm_group_with_zero ℝβ‰₯0 := { zero_ne_one := assume h, zero_ne_one $ nnreal.eq_iff.2 h, inv_zero := nnreal.eq $ inv_zero, mul_inv_cancel := assume x h, nnreal.eq $ mul_inv_cancel $ ne_iff.2 h, .. (by apply_instance : has_inv ℝβ‰₯0), .. (_ : comm_semiring ℝβ‰₯0) } @[move_cast] lemma coe_pow (r : ℝβ‰₯0) (n : β„•) : ((r^n : ℝβ‰₯0) : ℝ) = r^n := is_monoid_hom.map_pow coe r n @[move_cast] lemma coe_list_sum (l : list ℝβ‰₯0) : ((l.sum : ℝβ‰₯0) : ℝ) = (l.map coe).sum := eq.symm $ l.sum_hom coe @[move_cast] lemma coe_list_prod (l : list ℝβ‰₯0) : ((l.prod : ℝβ‰₯0) : ℝ) = (l.map coe).prod := eq.symm $ l.prod_hom coe @[move_cast] lemma coe_multiset_sum (s : multiset ℝβ‰₯0) : ((s.sum : ℝβ‰₯0) : ℝ) = (s.map coe).sum := eq.symm $ s.sum_hom coe @[move_cast] lemma coe_multiset_prod (s : multiset ℝβ‰₯0) : ((s.prod : ℝβ‰₯0) : ℝ) = (s.map coe).prod := eq.symm $ s.prod_hom coe @[move_cast] lemma coe_sum {Ξ±} {s : finset Ξ±} {f : Ξ± β†’ ℝβ‰₯0} : ↑(s.sum f) = s.sum (Ξ»a, (f a : ℝ)) := eq.symm $ s.sum_hom coe @[move_cast] lemma coe_prod {Ξ±} {s : finset Ξ±} {f : Ξ± β†’ ℝβ‰₯0} : ↑(s.prod f) = s.prod (Ξ»a, (f a : ℝ)) := eq.symm $ s.prod_hom coe @[move_cast] lemma smul_coe (r : ℝβ‰₯0) (n : β„•) : ↑(add_monoid.smul n r) = add_monoid.smul n (r:ℝ) := is_add_monoid_hom.map_smul coe r n @[simp, squash_cast] protected lemma coe_nat_cast (n : β„•) : (↑(↑n : ℝβ‰₯0) : ℝ) = n := (ring_hom.of (coe : ℝβ‰₯0 β†’ ℝ)).map_nat_cast n instance : decidable_linear_order ℝβ‰₯0 := decidable_linear_order.lift (coe : ℝβ‰₯0 β†’ ℝ) subtype.val_injective (by apply_instance) @[elim_cast] protected lemma coe_le_coe {r₁ rβ‚‚ : ℝβ‰₯0} : (r₁ : ℝ) ≀ rβ‚‚ ↔ r₁ ≀ rβ‚‚ := iff.rfl @[elim_cast] protected lemma coe_lt_coe {r₁ rβ‚‚ : ℝβ‰₯0} : (r₁ : ℝ) < rβ‚‚ ↔ r₁ < rβ‚‚ := iff.rfl @[elim_cast] protected lemma coe_pos {r : ℝβ‰₯0} : (0 : ℝ) < r ↔ 0 < r := iff.rfl @[elim_cast] protected lemma coe_eq {r₁ rβ‚‚ : ℝβ‰₯0} : (r₁ : ℝ) = rβ‚‚ ↔ r₁ = rβ‚‚ := subtype.ext.symm protected lemma coe_mono : monotone (coe : ℝβ‰₯0 β†’ ℝ) := Ξ» _ _, nnreal.coe_le_coe.2 protected lemma of_real_mono : monotone nnreal.of_real := Ξ» x y h, max_le_max h (le_refl 0) @[simp] lemma of_real_coe {r : ℝβ‰₯0} : nnreal.of_real r = r := nnreal.eq $ max_eq_left r.2 /-- `nnreal.of_real` and `coe : ℝβ‰₯0 β†’ ℝ` form a Galois insertion. -/ protected def gi : galois_insertion nnreal.of_real coe := galois_insertion.monotone_intro nnreal.coe_mono nnreal.of_real_mono le_coe_of_real (Ξ» _, of_real_coe) instance : order_bot ℝβ‰₯0 := { bot := βŠ₯, bot_le := assume ⟨a, h⟩, h, .. nnreal.decidable_linear_order } instance : canonically_ordered_monoid ℝβ‰₯0 := { add_le_add_left := assume a b h c, @add_le_add_left ℝ _ a b h c, lt_of_add_lt_add_left := assume a b c, @lt_of_add_lt_add_left ℝ _ a b c, le_iff_exists_add := assume ⟨a, ha⟩ ⟨b, hb⟩, iff.intro (assume h : a ≀ b, ⟨⟨b - a, le_sub_iff_add_le.2 $ by simp [h]⟩, nnreal.eq $ show b = a + (b - a), by rw [add_sub_cancel'_right]⟩) (assume ⟨⟨c, hc⟩, eq⟩, eq.symm β–Έ show a ≀ a + c, from (le_add_iff_nonneg_right a).2 hc), ..nnreal.comm_semiring, ..nnreal.order_bot, ..nnreal.decidable_linear_order } instance : distrib_lattice ℝβ‰₯0 := by apply_instance instance : semilattice_inf_bot ℝβ‰₯0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : semilattice_sup_bot ℝβ‰₯0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : linear_ordered_semiring ℝβ‰₯0 := { add_left_cancel := assume a b c h, nnreal.eq $ @add_left_cancel ℝ _ a b c (nnreal.eq_iff.2 h), add_right_cancel := assume a b c h, nnreal.eq $ @add_right_cancel ℝ _ a b c (nnreal.eq_iff.2 h), le_of_add_le_add_left := assume a b c, @le_of_add_le_add_left ℝ _ a b c, mul_le_mul_of_nonneg_left := assume a b c, @mul_le_mul_of_nonneg_left ℝ _ a b c, mul_le_mul_of_nonneg_right := assume a b c, @mul_le_mul_of_nonneg_right ℝ _ a b c, mul_lt_mul_of_pos_left := assume a b c, @mul_lt_mul_of_pos_left ℝ _ a b c, mul_lt_mul_of_pos_right := assume a b c, @mul_lt_mul_of_pos_right ℝ _ a b c, zero_lt_one := @zero_lt_one ℝ _, .. nnreal.decidable_linear_order, .. nnreal.canonically_ordered_monoid, .. nnreal.comm_semiring } instance : canonically_ordered_comm_semiring ℝβ‰₯0 := { zero_ne_one := assume h, @zero_ne_one ℝ _ $ congr_arg subtype.val $ h, mul_eq_zero_iff := assume a b, nnreal.eq_iff.symm.trans $ mul_eq_zero.trans $ by simp, .. nnreal.linear_ordered_semiring, .. nnreal.canonically_ordered_monoid, .. nnreal.comm_semiring } instance : densely_ordered ℝβ‰₯0 := ⟨assume a b (h : (a : ℝ) < b), let ⟨c, hac, hcb⟩ := dense h in ⟨⟨c, le_trans a.property $ le_of_lt $ hac⟩, hac, hcb⟩⟩ instance : no_top_order ℝβ‰₯0 := ⟨assume a, let ⟨b, hb⟩ := no_top (a:ℝ) in ⟨⟨b, le_trans a.property $ le_of_lt $ hb⟩, hb⟩⟩ lemma bdd_above_coe {s : set ℝβ‰₯0} : bdd_above ((coe : nnreal β†’ ℝ) '' s) ↔ bdd_above s := iff.intro (assume ⟨b, hb⟩, ⟨nnreal.of_real b, assume ⟨y, hy⟩ hys, show y ≀ max b 0, from le_max_left_of_le $ 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 : nnreal β†’ ℝ) '' s) := ⟨0, assume r ⟨q, _, eq⟩, eq β–Έ q.2⟩ instance : has_Sup ℝβ‰₯0 := ⟨λs, ⟨Sup ((coe : nnreal β†’ ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Sup_empty] }, rcases h with ⟨⟨b, hb⟩, hbs⟩, by_cases h' : bdd_above s, { exact le_cSup_of_le (bdd_above_coe.2 h') (set.mem_image_of_mem _ hbs) hb }, { rw [real.Sup_of_not_bdd_above], rwa [bdd_above_coe] } end⟩⟩ instance : has_Inf ℝβ‰₯0 := ⟨λs, ⟨Inf ((coe : nnreal β†’ ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Inf_empty] }, exact le_cInf (h.image _) (assume r ⟨q, _, eq⟩, eq β–Έ q.2) end⟩⟩ lemma coe_Sup (s : set nnreal) : (↑(Sup s) : ℝ) = Sup ((coe : nnreal β†’ ℝ) '' s) := rfl lemma coe_Inf (s : set nnreal) : (↑(Inf s) : ℝ) = Inf ((coe : nnreal β†’ ℝ) '' s) := rfl instance : conditionally_complete_linear_order_bot ℝβ‰₯0 := { Sup := Sup, Inf := Inf, le_cSup := assume s a hs ha, le_cSup (bdd_above_coe.2 hs) (set.mem_image_of_mem _ ha), cSup_le := assume s a hs h,show Sup ((coe : nnreal β†’ ℝ) '' s) ≀ a, from cSup_le (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq β–Έ h hb, cInf_le := assume s a _ has, cInf_le (bdd_below_coe s) (set.mem_image_of_mem _ has), le_cInf := assume s a hs h, show (↑a : ℝ) ≀ Inf ((coe : nnreal β†’ ℝ) '' s), from le_cInf (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq β–Έ h hb, cSup_empty := nnreal.eq $ by simp [coe_Sup, real.Sup_empty]; refl, decidable_le := begin assume x y, apply classical.dec end, .. nnreal.linear_ordered_semiring, .. lattice_of_decidable_linear_order, .. nnreal.order_bot } instance : archimedean nnreal := ⟨ assume x y pos_y, let ⟨n, hr⟩ := archimedean.arch (x:ℝ) (pos_y : (0 : ℝ) < y) in ⟨n, show (x:ℝ) ≀ (add_monoid.smul n y : nnreal), by simp [*, smul_coe]⟩ ⟩ lemma le_of_forall_epsilon_le {a b : nnreal} (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 : nnreal) : a < b ↔ (βˆƒq:β„š, 0 ≀ q ∧ a < nnreal.of_real q ∧ nnreal.of_real 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 [coe_of_real _ this, nnreal.coe_lt_coe.symm, haq, hqb]⟩) (assume ⟨q, _, haq, hqb⟩, lt_trans haq hqb) lemma bot_eq_zero : (βŠ₯ : nnreal) = 0 := rfl lemma mul_sup (a b c : ℝβ‰₯0) : a * (b βŠ” c) = (a * b) βŠ” (a * c) := begin cases le_total b c with h h, { simp [sup_eq_max, max_eq_right h, max_eq_right (mul_le_mul_of_nonneg_left h (zero_le a))] }, { simp [sup_eq_max, max_eq_left h, max_eq_left (mul_le_mul_of_nonneg_left h (zero_le a))] }, end lemma mul_finset_sup {Ξ±} {f : Ξ± β†’ ℝβ‰₯0} {s : finset Ξ±} (r : ℝβ‰₯0) : r * s.sup f = s.sup (Ξ»a, r * f a) := begin refine s.induction_on _ _, { simp [bot_eq_zero] }, { assume a s has ih, simp [has, ih, mul_sup], } end @[simp, move_cast] lemma coe_max (x y : nnreal) : ((max x y : nnreal) : ℝ) = max (x : ℝ) (y : ℝ) := by { delta max, split_ifs; refl } @[simp, move_cast] lemma coe_min (x y : nnreal) : ((min x y : nnreal) : ℝ) = min (x : ℝ) (y : ℝ) := by { delta min, split_ifs; refl } section of_real @[simp] lemma zero_le_coe {q : nnreal} : 0 ≀ (q : ℝ) := q.2 @[simp] lemma of_real_zero : nnreal.of_real 0 = 0 := by simp [nnreal.of_real]; refl @[simp] lemma of_real_one : nnreal.of_real 1 = 1 := by simp [nnreal.of_real, max_eq_left (zero_le_one : (0 :ℝ) ≀ 1)]; refl @[simp] lemma of_real_pos {r : ℝ} : 0 < nnreal.of_real r ↔ 0 < r := by simp [nnreal.of_real, nnreal.coe_lt_coe.symm, lt_irrefl] @[simp] lemma of_real_eq_zero {r : ℝ} : nnreal.of_real r = 0 ↔ r ≀ 0 := by simpa [-of_real_pos] using (not_iff_not.2 (@of_real_pos r)) lemma of_real_of_nonpos {r : ℝ} : r ≀ 0 β†’ nnreal.of_real r = 0 := of_real_eq_zero.2 @[simp] lemma of_real_le_of_real_iff {r p : ℝ} (hp : 0 ≀ p) : nnreal.of_real r ≀ nnreal.of_real p ↔ r ≀ p := by simp [nnreal.coe_le_coe.symm, nnreal.of_real, hp] @[simp] lemma of_real_lt_of_real_iff' {r p : ℝ} : nnreal.of_real r < nnreal.of_real p ↔ r < p ∧ 0 < p := by simp [nnreal.coe_lt_coe.symm, nnreal.of_real, lt_irrefl] lemma of_real_lt_of_real_iff {r p : ℝ} (h : 0 < p) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans (and_iff_left h) lemma of_real_lt_of_real_iff_of_nonneg {r p : ℝ} (hr : 0 ≀ r) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans ⟨and.left, Ξ» h, ⟨h, lt_of_le_of_lt hr h⟩⟩ @[simp] lemma of_real_add {r p : ℝ} (hr : 0 ≀ r) (hp : 0 ≀ p) : nnreal.of_real (r + p) = nnreal.of_real r + nnreal.of_real p := nnreal.eq $ by simp [nnreal.of_real, hr, hp, add_nonneg] lemma of_real_add_of_real {r p : ℝ} (hr : 0 ≀ r) (hp : 0 ≀ p) : nnreal.of_real r + nnreal.of_real p = nnreal.of_real (r + p) := (of_real_add hr hp).symm lemma of_real_le_of_real {r p : ℝ} (h : r ≀ p) : nnreal.of_real r ≀ nnreal.of_real p := nnreal.of_real_mono h lemma of_real_add_le {r p : ℝ} : nnreal.of_real (r + p) ≀ nnreal.of_real r + nnreal.of_real p := nnreal.coe_le_coe.1 $ max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) nnreal.zero_le_coe lemma of_real_le_iff_le_coe {r : ℝ} {p : nnreal} : nnreal.of_real r ≀ p ↔ r ≀ ↑p := nnreal.gi.gc r p lemma le_of_real_iff_coe_le {r : nnreal} {p : ℝ} (hp : p β‰₯ 0) : r ≀ nnreal.of_real p ↔ ↑r ≀ p := by rw [← nnreal.coe_le_coe, nnreal.coe_of_real p hp] lemma of_real_lt_iff_lt_coe {r : ℝ} {p : nnreal} (ha : r β‰₯ 0) : nnreal.of_real r < p ↔ r < ↑p := by rw [← nnreal.coe_lt_coe, nnreal.coe_of_real r ha] lemma lt_of_real_iff_coe_lt {r : nnreal} {p : ℝ} : r < nnreal.of_real p ↔ ↑r < p := begin cases le_total 0 p, { rw [← nnreal.coe_lt_coe, nnreal.coe_of_real p h] }, { rw [of_real_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 (coe_nonneg _) rp), contradiction } end end of_real section mul lemma mul_eq_mul_left {a b c : nnreal} (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 eq_of_mul_eq_mul_left (mt (@nnreal.eq_iff a 0).1 h) }, { assume h, rw [h] } end lemma of_real_mul {p q : ℝ} (hp : 0 ≀ p) : nnreal.of_real (p * q) = nnreal.of_real p * nnreal.of_real q := begin cases le_total 0 q with hq hq, { apply nnreal.eq, have := max_eq_left (mul_nonneg hp hq), simpa [nnreal.of_real, hp, hq, max_eq_left] }, { have hpq := mul_nonpos_of_nonneg_of_nonpos hp hq, rw [of_real_eq_zero.2 hq, of_real_eq_zero.2 hpq, mul_zero] } end @[field_simps] theorem mul_ne_zero' {a b : nnreal} (h₁ : a β‰  0) (hβ‚‚ : b β‰  0) : a * b β‰  0 := mul_ne_zero'' h₁ hβ‚‚ end mul section sub lemma sub_def {r p : ℝβ‰₯0} : r - p = nnreal.of_real (r - p) := rfl lemma sub_eq_zero {r p : ℝβ‰₯0} (h : r ≀ p) : r - p = 0 := nnreal.eq $ max_eq_right $ sub_le_iff_le_add.2 $ by simpa [nnreal.coe_le_coe] using h @[simp] lemma sub_self {r : ℝβ‰₯0} : r - r = 0 := sub_eq_zero $ le_refl r @[simp] lemma sub_zero {r : ℝβ‰₯0} : r - 0 = r := by rw [sub_def, nnreal.coe_zero, sub_zero, nnreal.of_real_coe] lemma sub_pos {r p : ℝβ‰₯0} : 0 < r - p ↔ p < r := of_real_pos.trans $ sub_pos.trans $ nnreal.coe_lt_coe protected lemma sub_lt_self {r p : nnreal} : 0 < r β†’ 0 < p β†’ r - p < r := assume hr hp, begin cases le_total r p, { rwa [sub_eq_zero h] }, { rw [← nnreal.coe_lt_coe, nnreal.coe_sub h], exact sub_lt_self _ hp } end @[simp] lemma sub_le_iff_le_add {r p q : nnreal} : r - p ≀ q ↔ r ≀ q + p := match le_total p r with | or.inl h := by rw [← nnreal.coe_le_coe, ← nnreal.coe_le_coe, nnreal.coe_sub h, nnreal.coe_add, sub_le_iff_le_add] | or.inr h := have r ≀ p + q, from le_add_right h, by simpa [nnreal.coe_le_coe, nnreal.coe_le_coe, sub_eq_zero h, add_comm] end @[simp] lemma sub_le_self {r p : ℝβ‰₯0} : r - p ≀ r := sub_le_iff_le_add.2 $ le_add_right $ le_refl r lemma add_sub_cancel {r p : nnreal} : (p + r) - r = p := nnreal.eq $ by rw [nnreal.coe_sub, nnreal.coe_add, add_sub_cancel]; exact le_add_left (le_refl _) lemma add_sub_cancel' {r p : nnreal} : (r + p) - r = p := by rw [add_comm, add_sub_cancel] @[simp] lemma sub_add_cancel_of_le {a b : nnreal} (h : b ≀ a) : (a - b) + b = a := nnreal.eq $ by rw [nnreal.coe_add, nnreal.coe_sub h, sub_add_cancel] lemma sub_sub_cancel_of_le {r p : ℝβ‰₯0} (h : r ≀ p) : p - (p - r) = r := by rw [nnreal.sub_def, nnreal.sub_def, nnreal.coe_of_real _ $ sub_nonneg.2 h, sub_sub_cancel, nnreal.of_real_coe] lemma lt_sub_iff_add_lt {p q r : nnreal} : p < q - r ↔ p + r < q := begin split, { assume H, have : (((q - r) : nnreal) : ℝ) = (q : ℝ) - (r : ℝ) := nnreal.coe_sub (le_of_lt (sub_pos.1 (lt_of_le_of_lt (zero_le _) H))), rwa [← nnreal.coe_lt_coe, this, lt_sub_iff_add_lt, ← nnreal.coe_add] at H }, { assume H, have : r ≀ q := le_trans (le_add_left (le_refl _)) (le_of_lt H), rwa [← nnreal.coe_lt_coe, nnreal.coe_sub this, lt_sub_iff_add_lt, ← nnreal.coe_add] } end end sub section inv lemma div_def {r p : nnreal} : r / p = r * p⁻¹ := rfl @[simp] lemma inv_zero : (0 : nnreal)⁻¹ = 0 := nnreal.eq inv_zero @[simp] lemma inv_eq_zero {r : nnreal} : (r : nnreal)⁻¹ = 0 ↔ r = 0 := inv_eq_zero @[simp] lemma inv_pos {r : nnreal} : 0 < r⁻¹ ↔ 0 < r := by simp [zero_lt_iff_ne_zero] lemma div_pos {r p : ℝβ‰₯0} (hr : 0 < r) (hp : 0 < p) : 0 < r / p := mul_pos hr (inv_pos.2 hp) @[simp] lemma inv_one : (1:ℝβ‰₯0)⁻¹ = 1 := nnreal.eq $ inv_one @[simp] lemma div_one {r : ℝβ‰₯0} : r / 1 = r := by rw [div_def, inv_one, mul_one] protected lemma mul_inv {r p : ℝβ‰₯0} : (r * p)⁻¹ = p⁻¹ * r⁻¹ := nnreal.eq $ mul_inv' _ _ protected lemma inv_pow {r : ℝβ‰₯0} {n : β„•} : (r^n)⁻¹ = (r⁻¹)^n := nnreal.eq $ by { push_cast, exact (inv_pow' _ _).symm } @[simp] lemma inv_mul_cancel {r : ℝβ‰₯0} (h : r β‰  0) : r⁻¹ * r = 1 := nnreal.eq $ inv_mul_cancel $ mt (@nnreal.eq_iff r 0).1 h @[simp] lemma mul_inv_cancel {r : ℝβ‰₯0} (h : r β‰  0) : r * r⁻¹ = 1 := by rw [mul_comm, inv_mul_cancel h] @[simp] lemma div_self {r : ℝβ‰₯0} (h : r β‰  0) : r / r = 1 := mul_inv_cancel h @[simp] lemma div_mul_cancel {r p : ℝβ‰₯0} (h : p β‰  0) : r / p * p = r := by rw [div_def, mul_assoc, inv_mul_cancel h, mul_one] @[simp] lemma mul_div_cancel {r p : ℝβ‰₯0} (h : p β‰  0) : r * p / p = r := by rw [div_def, mul_assoc, mul_inv_cancel h, mul_one] @[simp] lemma mul_div_cancel' {r p : ℝβ‰₯0} (h : r β‰  0) : r * (p / r) = p := by rw [mul_comm, div_mul_cancel h] @[simp] lemma inv_inv {r : ℝβ‰₯0} : r⁻¹⁻¹ = r := nnreal.eq inv_inv' @[simp] lemma inv_le {r p : ℝβ‰₯0} (h : r β‰  0) : r⁻¹ ≀ p ↔ 1 ≀ r * p := by rw [← mul_le_mul_left (zero_lt_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 (zero_lt_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 (zero_lt_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_def, mul_comm, ← mul_le_iff_le_inv hr, mul_comm] lemma le_of_forall_lt_one_mul_lt {x y : ℝβ‰₯0} (h : βˆ€a<1, a * x ≀ y) : x ≀ y := le_of_forall_ge_of_dense $ assume a ha, have hx : x β‰  0 := zero_lt_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_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 [div_def] using half_lt_self zero_ne_one.symm lemma div_lt_iff {a b c : ℝβ‰₯0} (hc : c β‰  0) : b / c < a ↔ b < a * c := begin rw [← nnreal.coe_lt_coe, ← nnreal.coe_lt_coe, nnreal.coe_div, nnreal.coe_mul], exact div_lt_iff (zero_lt_iff_ne_zero.mpr hc) end 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] theorem div_pow {a b : ℝβ‰₯0} (n : β„•) : (a / b) ^ n = a ^ n / b ^ n := div_pow _ _ _ @[field_simps] lemma mul_div_assoc' (a b c : ℝβ‰₯0) : a * (b / c) = (a * b) / c := by rw [div_def, div_def, mul_assoc] @[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 inv_eq_one_div (a : ℝβ‰₯0) : a⁻¹ = 1/a := by rw [div_def, one_mul] @[field_simps] lemma div_mul_eq_mul_div (a b c : ℝβ‰₯0) : (a / b) * c = (a * c) / b := by { rw [div_def, div_def], ac_refl } @[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 one_div_eq_inv (a : ℝβ‰₯0) : 1 / a = a⁻¹ := one_mul a⁻¹ lemma one_div_div (a b : ℝβ‰₯0) : 1 / (a / b) = b / a := by { rw ← nnreal.eq_iff, simp [one_div_div] } lemma div_eq_mul_one_div (a b : ℝβ‰₯0) : a / b = a * (1 / b) := by rw [div_def, div_def, one_mul] @[field_simps] lemma div_div_eq_mul_div (a b c : ℝβ‰₯0) : a / (b / c) = (a * c) / b := by { rw ← nnreal.eq_iff, simp [div_div_eq_mul_div] } @[field_simps] lemma div_div_eq_div_mul (a b c : ℝβ‰₯0) : (a / b) / c = a / (b * c) := by { rw ← nnreal.eq_iff, simp [div_div_eq_div_mul] } @[field_simps] lemma div_eq_div_iff {a b c d : ℝβ‰₯0} (hb : b β‰  0) (hd : d β‰  0) : a / b = c / d ↔ a * d = c * b := div_eq_div_iff hb hd @[field_simps] lemma div_eq_iff {a b c : ℝβ‰₯0} (hb : b β‰  0) : a / b = c ↔ a = c * b := by simpa using @div_eq_div_iff a b c 1 hb one_ne_zero @[field_simps] lemma eq_div_iff {a b c : ℝβ‰₯0} (hb : b β‰  0) : c = a / b ↔ c * b = a := by simpa using @div_eq_div_iff c 1 a b one_ne_zero hb end inv section pow theorem pow_eq_zero {a : ℝβ‰₯0} {n : β„•} (h : a^n = 0) : a = 0 := begin rw ← nnreal.eq_iff, rw [← nnreal.eq_iff, coe_pow] at h, exact pow_eq_zero h end @[field_simps] theorem pow_ne_zero {a : ℝβ‰₯0} (n : β„•) (h : a β‰  0) : a ^ n β‰  0 := mt pow_eq_zero h end pow end nnreal
7cdb15cf060cd2cc282b92947826d54a6567f32a
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/pfunctor/univariate/M.lean
0d7980a33d14c57ef3779fde2b0c3e55b5c1ad7e
[ "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
21,952
lean
/- Copyright (c) 2017 Simon Hudon All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import data.pfunctor.univariate.basic /-! # M-types M types are potentially infinite tree-like structures. They are defined as the greatest fixpoint of a polynomial functor. -/ universes u v w open nat function list (hiding head') variables (F : pfunctor.{u}) local prefix `β™―`:0 := cast (by simp [*] <|> cc <|> solve_by_elim) namespace pfunctor namespace approx /-- `cofix_a F n` is an `n` level approximation of a M-type -/ inductive cofix_a : β„• β†’ Type u | continue : cofix_a 0 | intro {n} : βˆ€ a, (F.B a β†’ cofix_a n) β†’ cofix_a (succ n) /-- default inhabitant of `cofix_a` -/ protected def cofix_a.default [inhabited F.A] : Ξ  n, cofix_a F n | 0 := cofix_a.continue | (succ n) := cofix_a.intro default $ Ξ» _, cofix_a.default n instance [inhabited F.A] {n} : inhabited (cofix_a F n) := ⟨ cofix_a.default F n ⟩ lemma cofix_a_eq_zero : βˆ€ x y : cofix_a F 0, x = y | cofix_a.continue cofix_a.continue := rfl variables {F} /-- The label of the root of the tree for a non-trivial approximation of the cofix of a pfunctor. -/ def head' : Ξ  {n}, cofix_a F (succ n) β†’ F.A | n (cofix_a.intro i _) := i /-- for a non-trivial approximation, return all the subtrees of the root -/ def children' : Ξ  {n} (x : cofix_a F (succ n)), F.B (head' x) β†’ cofix_a F n | n (cofix_a.intro a f) := f lemma approx_eta {n : β„•} (x : cofix_a F (n+1)) : x = cofix_a.intro (head' x) (children' x) := by cases x; refl /-- Relation between two approximations of the cofix of a pfunctor that state they both contain the same data until one of them is truncated -/ inductive agree : βˆ€ {n : β„•}, cofix_a F n β†’ cofix_a F (n+1) β†’ Prop | continue (x : cofix_a F 0) (y : cofix_a F 1) : agree x y | intro {n} {a} (x : F.B a β†’ cofix_a F n) (x' : F.B a β†’ cofix_a F (n+1)) : (βˆ€ i : F.B a, agree (x i) (x' i)) β†’ agree (cofix_a.intro a x) (cofix_a.intro a x') /-- Given an infinite series of approximations `approx`, `all_agree approx` states that they are all consistent with each other. -/ def all_agree (x : Ξ  n, cofix_a F n) := βˆ€ n, agree (x n) (x (succ n)) @[simp] lemma agree_trival {x : cofix_a F 0} {y : cofix_a F 1} : agree x y := by { constructor } lemma agree_children {n : β„•} (x : cofix_a F (succ n)) (y : cofix_a F (succ n+1)) {i j} (hβ‚€ : i == j) (h₁ : agree x y) : agree (children' x i) (children' y j) := begin cases h₁ with _ _ _ _ _ _ hagree, cases hβ‚€, apply hagree, end /-- `truncate a` turns `a` into a more limited approximation -/ def truncate : βˆ€ {n : β„•}, cofix_a F (n+1) β†’ cofix_a F n | 0 (cofix_a.intro _ _) := cofix_a.continue | (succ n) (cofix_a.intro i f) := cofix_a.intro i $ truncate ∘ f lemma truncate_eq_of_agree {n : β„•} (x : cofix_a F n) (y : cofix_a F (succ n)) (h : agree x y) : truncate y = x := begin induction n generalizing x y; cases x; cases y, { refl }, { cases h with _ _ _ _ _ hβ‚€ h₁, cases h, simp only [truncate, function.comp, true_and, eq_self_iff_true, heq_iff_eq], ext y, apply n_ih, apply h₁ } end variables {X : Type w} variables (f : X β†’ F.obj X) /-- `s_corec f i n` creates an approximation of height `n` of the final coalgebra of `f` -/ def s_corec : Ξ  (i : X) n, cofix_a F n | _ 0 := cofix_a.continue | j (succ n) := cofix_a.intro (f j).1 (Ξ» i, s_corec ((f j).2 i) _) lemma P_corec (i : X) (n : β„•) : agree (s_corec f i n) (s_corec f i (succ n)) := begin induction n with n generalizing i, constructor, cases h : f i with y g, constructor, introv, apply n_ih, end /-- `path F` provides indices to access internal nodes in `corec F` -/ def path (F : pfunctor.{u}) := list F.Idx instance path.inhabited : inhabited (path F) := ⟨ [] ⟩ open list nat instance : subsingleton (cofix_a F 0) := ⟨ by { intros, casesm* cofix_a F 0, refl } ⟩ lemma head_succ' (n m : β„•) (x : Ξ  n, cofix_a F n) (Hconsistent : all_agree x) : head' (x (succ n)) = head' (x (succ m)) := begin suffices : βˆ€ n, head' (x (succ n)) = head' (x 1), { simp [this] }, clear m n, intro, cases hβ‚€ : x (succ n) with _ iβ‚€ fβ‚€, cases h₁ : x 1 with _ i₁ f₁, dsimp only [head'], induction n with n, { rw h₁ at hβ‚€, cases hβ‚€, trivial }, { have H := Hconsistent (succ n), cases hβ‚‚ : x (succ n) with _ iβ‚‚ fβ‚‚, rw [hβ‚€,hβ‚‚] at H, apply n_ih (truncate ∘ fβ‚€), rw hβ‚‚, cases H with _ _ _ _ _ _ hagree, congr, funext j, dsimp only [comp_app], rw truncate_eq_of_agree, apply hagree } end end approx open approx /-- Internal definition for `M`. It is needed to avoid name clashes between `M.mk` and `M.cases_on` and the declarations generated for the structure -/ structure M_intl := (approx : βˆ€ n, cofix_a F n) (consistent : all_agree approx) /-- For polynomial functor `F`, `M F` is its final coalgebra -/ def M := M_intl F lemma M.default_consistent [inhabited F.A] : Ξ  n, agree (default : cofix_a F n) default | 0 := agree.continue _ _ | (succ n) := agree.intro _ _ $ Ξ» _, M.default_consistent n instance M.inhabited [inhabited F.A] : inhabited (M F) := ⟨ { approx := default, consistent := M.default_consistent _ } ⟩ instance M_intl.inhabited [inhabited F.A] : inhabited (M_intl F) := show inhabited (M F), by apply_instance namespace M lemma ext' (x y : M F) (H : βˆ€ i : β„•, x.approx i = y.approx i) : x = y := by { cases x, cases y, congr' with n, apply H } variables {X : Type*} variables (f : X β†’ F.obj X) variables {F} /-- Corecursor for the M-type defined by `F`. -/ protected def corec (i : X) : M F := { approx := s_corec f i, consistent := P_corec _ _ } variables {F} /-- given a tree generated by `F`, `head` gives us the first piece of data it contains -/ def head (x : M F) := head' (x.1 1) /-- return all the subtrees of the root of a tree `x : M F` -/ def children (x : M F) (i : F.B (head x)) : M F := let H := Ξ» n : β„•, @head_succ' _ n 0 x.1 x.2 in { approx := Ξ» n, children' (x.1 _) (cast (congr_arg _ $ by simp only [head,H]; refl) i), consistent := begin intro, have P' := x.2 (succ n), apply agree_children _ _ _ P', transitivity i, apply cast_heq, symmetry, apply cast_heq, end } /-- select a subtree using a `i : F.Idx` or return an arbitrary tree if `i` designates no subtree of `x` -/ def ichildren [inhabited (M F)] [decidable_eq F.A] (i : F.Idx) (x : M F) : M F := if H' : i.1 = head x then children x (cast (congr_arg _ $ by simp only [head,H']; refl) i.2) else default lemma head_succ (n m : β„•) (x : M F) : head' (x.approx (succ n)) = head' (x.approx (succ m)) := head_succ' n m _ x.consistent lemma head_eq_head' : Ξ  (x : M F) (n : β„•), head x = head' (x.approx $ n+1) | ⟨x,h⟩ n := head_succ' _ _ _ h lemma head'_eq_head : Ξ  (x : M F) (n : β„•), head' (x.approx $ n+1) = head x | ⟨x,h⟩ n := head_succ' _ _ _ h lemma truncate_approx (x : M F) (n : β„•) : truncate (x.approx $ n+1) = x.approx n := truncate_eq_of_agree _ _ (x.consistent _) /-- unfold an M-type -/ def dest : M F β†’ F.obj (M F) | x := ⟨head x,Ξ» i, children x i ⟩ namespace approx /-- generates the approximations needed for `M.mk` -/ protected def s_mk (x : F.obj $ M F) : Ξ  n, cofix_a F n | 0 := cofix_a.continue | (succ n) := cofix_a.intro x.1 (Ξ» i, (x.2 i).approx n) protected lemma P_mk (x : F.obj $ M F) : all_agree (approx.s_mk x) | 0 := by { constructor } | (succ n) := by { constructor, introv, apply (x.2 i).consistent } end approx /-- constructor for M-types -/ protected def mk (x : F.obj $ M F) : M F := { approx := approx.s_mk x, consistent := approx.P_mk x } /-- `agree' n` relates two trees of type `M F` that are the same up to dept `n` -/ inductive agree' : β„• β†’ M F β†’ M F β†’ Prop | trivial (x y : M F) : agree' 0 x y | step {n : β„•} {a} (x y : F.B a β†’ M F) {x' y'} : x' = M.mk ⟨a,x⟩ β†’ y' = M.mk ⟨a,y⟩ β†’ (βˆ€ i, agree' n (x i) (y i)) β†’ agree' (succ n) x' y' @[simp] lemma dest_mk (x : F.obj $ M F) : dest (M.mk x) = x := begin funext i, dsimp only [M.mk,dest], cases x with x ch, congr' with i, cases h : ch i, simp only [children,M.approx.s_mk,children',cast_eq], dsimp only [M.approx.s_mk,children'], congr, rw h, end @[simp] lemma mk_dest (x : M F) : M.mk (dest x) = x := begin apply ext', intro n, dsimp only [M.mk], induction n with n, { apply subsingleton.elim }, dsimp only [approx.s_mk,dest,head], cases h : x.approx (succ n) with _ hd ch, have h' : hd = head' (x.approx 1), { rw [← head_succ' n,h,head'], apply x.consistent }, revert ch, rw h', intros, congr, { ext a, dsimp only [children], h_generalize! hh : a == a'', rw h, intros, cases hh, refl }, end lemma mk_inj {x y : F.obj $ M F} (h : M.mk x = M.mk y) : x = y := by rw [← dest_mk x,h,dest_mk] /-- destructor for M-types -/ protected def cases {r : M F β†’ Sort w} (f : βˆ€ (x : F.obj $ M F), r (M.mk x)) (x : M F) : r x := suffices r (M.mk (dest x)), by { haveI := classical.prop_decidable, haveI := inhabited.mk x, rw [← mk_dest x], exact this }, f _ /-- destructor for M-types -/ protected def cases_on {r : M F β†’ Sort w} (x : M F) (f : βˆ€ (x : F.obj $ M F), r (M.mk x)) : r x := M.cases f x /-- destructor for M-types, similar to `cases_on` but also gives access directly to the root and subtrees on an M-type -/ protected def cases_on' {r : M F β†’ Sort w} (x : M F) (f : βˆ€ a f, r (M.mk ⟨a,f⟩)) : r x := M.cases_on x (Ξ» ⟨a,g⟩, f a _) lemma approx_mk (a : F.A) (f : F.B a β†’ M F) (i : β„•) : (M.mk ⟨a, f⟩).approx (succ i) = cofix_a.intro a (Ξ» j, (f j).approx i) := rfl @[simp] lemma agree'_refl {n : β„•} (x : M F) : agree' n x x := by { induction n generalizing x; induction x using pfunctor.M.cases_on'; constructor; try { refl }, intros, apply n_ih } lemma agree_iff_agree' {n : β„•} (x y : M F) : agree (x.approx n) (y.approx $ n+1) ↔ agree' n x y := begin split; intros h, { induction n generalizing x y, constructor, { induction x using pfunctor.M.cases_on', induction y using pfunctor.M.cases_on', simp only [approx_mk] at h, cases h with _ _ _ _ _ _ hagree, constructor; try { refl }, intro i, apply n_ih, apply hagree } }, { induction n generalizing x y, constructor, { cases h, induction x using pfunctor.M.cases_on', induction y using pfunctor.M.cases_on', simp only [approx_mk], have h_a_1 := mk_inj β€ΉM.mk ⟨x_a, x_f⟩ = M.mk ⟨h_a, h_xβŸ©β€Ί, cases h_a_1, replace h_a_2 := mk_inj β€ΉM.mk ⟨y_a, y_f⟩ = M.mk ⟨h_a, h_yβŸ©β€Ί, cases h_a_2, constructor, intro i, apply n_ih, simp * } }, end @[simp] lemma cases_mk {r : M F β†’ Sort*} (x : F.obj $ M F) (f : Ξ  (x : F.obj $ M F), r (M.mk x)) : pfunctor.M.cases f (M.mk x) = f x := begin dsimp only [M.mk,pfunctor.M.cases,dest,head,approx.s_mk,head'], cases x, dsimp only [approx.s_mk], apply eq_of_heq, apply rec_heq_of_heq, congr' with x, dsimp only [children,approx.s_mk,children'], cases h : x_snd x, dsimp only [head], congr' with n, change (x_snd (x)).approx n = _, rw h end @[simp] lemma cases_on_mk {r : M F β†’ Sort*} (x : F.obj $ M F) (f : Ξ  x : F.obj $ M F, r (M.mk x)) : pfunctor.M.cases_on (M.mk x) f = f x := cases_mk x f @[simp] lemma cases_on_mk' {r : M F β†’ Sort*} {a} (x : F.B a β†’ M F) (f : Ξ  a (f : F.B a β†’ M F), r (M.mk ⟨a,f⟩)) : pfunctor.M.cases_on' (M.mk ⟨a,x⟩) f = f a x := cases_mk ⟨_,x⟩ _ /-- `is_path p x` tells us if `p` is a valid path through `x` -/ inductive is_path : path F β†’ M F β†’ Prop | nil (x : M F) : is_path [] x | cons (xs : path F) {a} (x : M F) (f : F.B a β†’ M F) (i : F.B a) : x = M.mk ⟨a,f⟩ β†’ is_path xs (f i) β†’ is_path (⟨a,i⟩ :: xs) x lemma is_path_cons {xs : path F} {a a'} {f : F.B a β†’ M F} {i : F.B a'} : is_path (⟨a',i⟩ :: xs) (M.mk ⟨a,f⟩) β†’ a = a' := begin generalize h : (M.mk ⟨a,f⟩) = x, rintro (_ | ⟨_, _, _, _, _, rfl, _⟩), cases mk_inj h, refl end lemma is_path_cons' {xs : path F} {a} {f : F.B a β†’ M F} {i : F.B a} : is_path (⟨a,i⟩ :: xs) (M.mk ⟨a,f⟩) β†’ is_path xs (f i) := begin generalize h : (M.mk ⟨a,f⟩) = x, rintro (_ | ⟨_, _, _, _, _, rfl, hp⟩), cases mk_inj h, exact hp end /-- follow a path through a value of `M F` and return the subtree found at the end of the path if it is a valid path for that value and return a default tree -/ def isubtree [decidable_eq F.A] [inhabited (M F)] : path F β†’ M F β†’ M F | [] x := x | (⟨a, i⟩ :: ps) x := pfunctor.M.cases_on' x (Ξ» a' f, (if h : a = a' then isubtree ps (f $ cast (by rw h) i) else default : (Ξ» x, M F) (M.mk ⟨a',f⟩))) /-- similar to `isubtree` but returns the data at the end of the path instead of the whole subtree -/ def iselect [decidable_eq F.A] [inhabited (M F)] (ps : path F) : M F β†’ F.A := Ξ» (x : M F), head $ isubtree ps x lemma iselect_eq_default [decidable_eq F.A] [inhabited (M F)] (ps : path F) (x : M F) (h : Β¬ is_path ps x) : iselect ps x = head default := begin induction ps generalizing x, { exfalso, apply h, constructor }, { cases ps_hd with a i, induction x using pfunctor.M.cases_on', simp only [iselect,isubtree] at ps_ih ⊒, by_cases h'' : a = x_a, subst x_a, { simp only [dif_pos, eq_self_iff_true, cases_on_mk'], rw ps_ih, intro h', apply h, constructor; try { refl }, apply h' }, { simp * } } end @[simp] lemma head_mk (x : F.obj (M F)) : head (M.mk x) = x.1 := eq.symm $ calc x.1 = (dest (M.mk x)).1 : by rw dest_mk ... = head (M.mk x) : by refl lemma children_mk {a} (x : F.B a β†’ (M F)) (i : F.B (head (M.mk ⟨a,x⟩))) : children (M.mk ⟨a,x⟩) i = x (cast (by rw head_mk) i) := by apply ext'; intro n; refl @[simp] lemma ichildren_mk [decidable_eq F.A] [inhabited (M F)] (x : F.obj (M F)) (i : F.Idx) : ichildren i (M.mk x) = x.iget i := by { dsimp only [ichildren,pfunctor.obj.iget], congr' with h, apply ext', dsimp only [children',M.mk,approx.s_mk], intros, refl } @[simp] lemma isubtree_cons [decidable_eq F.A] [inhabited (M F)] (ps : path F) {a} (f : F.B a β†’ M F) {i : F.B a} : isubtree (⟨_,i⟩ :: ps) (M.mk ⟨a,f⟩) = isubtree ps (f i) := by simp only [isubtree,ichildren_mk,pfunctor.obj.iget,dif_pos,isubtree,M.cases_on_mk']; refl @[simp] lemma iselect_nil [decidable_eq F.A] [inhabited (M F)] {a} (f : F.B a β†’ M F) : iselect nil (M.mk ⟨a,f⟩) = a := by refl @[simp] lemma iselect_cons [decidable_eq F.A] [inhabited (M F)] (ps : path F) {a} (f : F.B a β†’ M F) {i} : iselect (⟨a,i⟩ :: ps) (M.mk ⟨a,f⟩) = iselect ps (f i) := by simp only [iselect,isubtree_cons] lemma corec_def {X} (f : X β†’ F.obj X) (xβ‚€ : X) : M.corec f xβ‚€ = M.mk (M.corec f <$> f xβ‚€) := begin dsimp only [M.corec,M.mk], congr' with n, cases n with n, { dsimp only [s_corec,approx.s_mk], refl, }, { dsimp only [s_corec,approx.s_mk], cases h : (f xβ‚€), dsimp only [(<$>),pfunctor.map], congr, } end lemma ext_aux [inhabited (M F)] [decidable_eq F.A] {n : β„•} (x y z : M F) (hx : agree' n z x) (hy : agree' n z y) (hrec : βˆ€ (ps : path F), n = ps.length β†’ iselect ps x = iselect ps y) : x.approx (n+1) = y.approx (n+1) := begin induction n with n generalizing x y z, { specialize hrec [] rfl, induction x using pfunctor.M.cases_on', induction y using pfunctor.M.cases_on', simp only [iselect_nil] at hrec, subst hrec, simp only [approx_mk, true_and, eq_self_iff_true, heq_iff_eq], apply subsingleton.elim }, { cases hx, cases hy, induction x using pfunctor.M.cases_on', induction y using pfunctor.M.cases_on', subst z, iterate 3 { have := mk_inj β€Ή_β€Ί, repeat { cases this } }, simp only [approx_mk, true_and, eq_self_iff_true, heq_iff_eq], ext i, apply n_ih, { solve_by_elim }, { solve_by_elim }, introv h, specialize hrec (⟨_,i⟩ :: ps) (congr_arg _ h), simp only [iselect_cons] at hrec, exact hrec } end open pfunctor.approx variables {F} local attribute [instance, priority 0] classical.prop_decidable lemma ext [inhabited (M F)] (x y : M F) (H : βˆ€ (ps : path F), iselect ps x = iselect ps y) : x = y := begin apply ext', intro i, induction i with i, { cases x.approx 0, cases y.approx 0, constructor }, { apply ext_aux x y x, { rw ← agree_iff_agree', apply x.consistent }, { rw [← agree_iff_agree',i_ih], apply y.consistent }, introv H', dsimp only [iselect] at H, cases H', apply H ps } end section bisim variable (R : M F β†’ M F β†’ Prop) local infix ` ~ `:50 := R /-- Bisimulation is the standard proof technique for equality between infinite tree-like structures -/ structure is_bisimulation : Prop := (head : βˆ€ {a a'} {f f'}, M.mk ⟨a,f⟩ ~ M.mk ⟨a',f'⟩ β†’ a = a') (tail : βˆ€ {a} {f f' : F.B a β†’ M F}, M.mk ⟨a,f⟩ ~ M.mk ⟨a,f'⟩ β†’ (βˆ€ (i : F.B a), f i ~ f' i) ) theorem nth_of_bisim [inhabited (M F)] (bisim : is_bisimulation R) (s₁ sβ‚‚) (ps : path F) : s₁ ~ sβ‚‚ β†’ is_path ps s₁ ∨ is_path ps sβ‚‚ β†’ iselect ps s₁ = iselect ps sβ‚‚ ∧ βˆƒ a (f f' : F.B a β†’ M F), isubtree ps s₁ = M.mk ⟨a,f⟩ ∧ isubtree ps sβ‚‚ = M.mk ⟨a,f'⟩ ∧ βˆ€ (i : F.B a), f i ~ f' i := begin intros hβ‚€ hh, induction s₁ using pfunctor.M.cases_on' with a f, induction sβ‚‚ using pfunctor.M.cases_on' with a' f', obtain rfl : a = a' := bisim.head hβ‚€, induction ps with i ps generalizing a f f', { existsi [rfl,a,f,f',rfl,rfl], apply bisim.tail hβ‚€ }, cases i with a' i, obtain rfl : a = a', { cases hh; cases is_path_cons hh; refl }, dsimp only [iselect] at ps_ih ⊒, have h₁ := bisim.tail hβ‚€ i, induction h : (f i) using pfunctor.M.cases_on' with aβ‚€ fβ‚€, induction h' : (f' i) using pfunctor.M.cases_on' with a₁ f₁, simp only [h,h',isubtree_cons] at ps_ih ⊒, rw [h,h'] at h₁, obtain rfl : aβ‚€ = a₁ := bisim.head h₁, apply (ps_ih _ _ _ h₁), rw [← h,← h'], apply or_of_or_of_imp_of_imp hh is_path_cons' is_path_cons' end theorem eq_of_bisim [nonempty (M F)] (bisim : is_bisimulation R) : βˆ€ s₁ sβ‚‚, s₁ ~ sβ‚‚ β†’ s₁ = sβ‚‚ := begin inhabit (M F), introv Hr, apply ext, introv, by_cases h : is_path ps s₁ ∨ is_path ps sβ‚‚, { have H := nth_of_bisim R bisim _ _ ps Hr h, exact H.left }, { rw not_or_distrib at h, cases h with hβ‚€ h₁, simp only [iselect_eq_default,*,not_false_iff] } end end bisim universes u' v' /-- corecursor for `M F` with swapped arguments -/ def corec_on {X : Type*} (xβ‚€ : X) (f : X β†’ F.obj X) : M F := M.corec f xβ‚€ variables {P : pfunctor.{u}} {Ξ± : Type u} lemma dest_corec (g : Ξ± β†’ P.obj Ξ±) (x : Ξ±) : M.dest (M.corec g x) = M.corec g <$> g x := by rw [corec_def,dest_mk] lemma bisim (R : M P β†’ M P β†’ Prop) (h : βˆ€ x y, R x y β†’ βˆƒ a f f', M.dest x = ⟨a, f⟩ ∧ M.dest y = ⟨a, f'⟩ ∧ βˆ€ i, R (f i) (f' i)) : βˆ€ x y, R x y β†’ x = y := begin introv h', haveI := inhabited.mk x.head, apply eq_of_bisim R _ _ _ h', clear h' x y, split; introv ih; rcases h _ _ ih with ⟨ a'', g, g', hβ‚€, h₁, hβ‚‚ ⟩; clear h, { replace hβ‚€ := congr_arg sigma.fst hβ‚€, replace h₁ := congr_arg sigma.fst h₁, simp only [dest_mk] at hβ‚€ h₁, rw [hβ‚€,h₁], }, { simp only [dest_mk] at hβ‚€ h₁, cases hβ‚€, cases h₁, apply hβ‚‚, }, end theorem bisim' {Ξ± : Type*} (Q : Ξ± β†’ Prop) (u v : Ξ± β†’ M P) (h : βˆ€ x, Q x β†’ βˆƒ a f f', M.dest (u x) = ⟨a, f⟩ ∧ M.dest (v x) = ⟨a, f'⟩ ∧ βˆ€ i, βˆƒ x', Q x' ∧ f i = u x' ∧ f' i = v x') : βˆ€ x, Q x β†’ u x = v x := Ξ» x Qx, let R := Ξ» w z : M P, βˆƒ x', Q x' ∧ w = u x' ∧ z = v x' in @M.bisim P R (Ξ» x y ⟨x', Qx', xeq, yeq⟩, let ⟨a, f, f', ux'eq, vx'eq, h'⟩ := h x' Qx' in ⟨a, f, f', xeq.symm β–Έ ux'eq, yeq.symm β–Έ vx'eq, h'⟩) _ _ ⟨x, Qx, rfl, rfl⟩ -- for the record, show M_bisim follows from _bisim' theorem bisim_equiv (R : M P β†’ M P β†’ Prop) (h : βˆ€ x y, R x y β†’ βˆƒ a f f', M.dest x = ⟨a, f⟩ ∧ M.dest y = ⟨a, f'⟩ ∧ βˆ€ i, R (f i) (f' i)) : βˆ€ x y, R x y β†’ x = y := Ξ» x y Rxy, let Q : M P Γ— M P β†’ Prop := Ξ» p, R p.fst p.snd in bisim' Q prod.fst prod.snd (Ξ» p Qp, let ⟨a, f, f', hx, hy, h'⟩ := h p.fst p.snd Qp in ⟨a, f, f', hx, hy, Ξ» i, ⟨⟨f i, f' i⟩, h' i, rfl, rfl⟩⟩) ⟨x, y⟩ Rxy theorem corec_unique (g : Ξ± β†’ P.obj Ξ±) (f : Ξ± β†’ M P) (hyp : βˆ€ x, M.dest (f x) = f <$> (g x)) : f = M.corec g := begin ext x, apply bisim' (Ξ» x, true) _ _ _ _ trivial, clear x, intros x _, cases gxeq : g x with a f', have hβ‚€ : M.dest (f x) = ⟨a, f ∘ f'⟩, { rw [hyp, gxeq, pfunctor.map_eq] }, have h₁ : M.dest (M.corec g x) = ⟨a, M.corec g ∘ f'⟩, { rw [dest_corec, gxeq, pfunctor.map_eq], }, refine ⟨_, _, _, hβ‚€, h₁, _⟩, intro i, exact ⟨f' i, trivial, rfl, rfl⟩ end /-- corecursor where the state of the computation can be sent downstream in the form of a recursive call -/ def corec₁ {Ξ± : Type u} (F : Ξ  X, (Ξ± β†’ X) β†’ Ξ± β†’ P.obj X) : Ξ± β†’ M P := M.corec (F _ id) /-- corecursor where it is possible to return a fully formed value at any point of the computation -/ def corec' {Ξ± : Type u} (F : Ξ  {X : Type u}, (Ξ± β†’ X) β†’ Ξ± β†’ M P βŠ• P.obj X) (x : Ξ±) : M P := corec₁ (Ξ» X rec (a : M P βŠ• Ξ±), let y := a >>= F (rec ∘ sum.inr) in match y with | sum.inr y := y | sum.inl y := (rec ∘ sum.inl) <$> M.dest y end ) (@sum.inr (M P) _ x) end M end pfunctor
d54ea38ba62efcfefa8299f164379529ac35643c
5d166a16ae129621cb54ca9dde86c275d7d2b483
/tests/lean/defeq_simp2.lean
bb1e03866a86936eb737080f20f54c1c80825e01
[ "Apache-2.0" ]
permissive
jcarlson23/lean
b00098763291397e0ac76b37a2dd96bc013bd247
8de88701247f54d325edd46c0eed57aeacb64baf
refs/heads/master
1,611,571,813,719
1,497,020,963,000
1,497,021,515,000
93,882,536
1
0
null
1,497,029,896,000
1,497,029,896,000
null
UTF-8
Lean
false
false
1,136
lean
open tactic constant f (n : nat) : n β‰₯ 0 β†’ nat axiom foo1 (n : nat) : n β‰₯ 0 axiom foo2 (n : nat) : n β‰₯ 0 axiom foo3 (n : nat) : n β‰₯ 0 -- by default we dont canonize proofs example (a b : nat) (H : f a (foo1 a) = f a (foo2 a)) : true := by do s ← simp_lemmas.mk_default, get_local `H >>= infer_type >>= s^.dsimplify >>= trace, constructor constant x1 : nat -- update the environment to force defeq_canonize cache to be reset example (a b : nat) (H : f a (foo1 a) = f a (foo2 a)) : true := by do s ← simp_lemmas.mk_default, get_local `H >>= infer_type >>= s^.dsimplify >>= trace, constructor constant x2 : nat -- update the environment to force defeq_canonize cache to be reset example (a b : nat) (H : f a (id (id (id (foo1 a)))) = f a (foo2 a)) : true := by do s ← simp_lemmas.mk_default, get_local `H >>= infer_type >>= s^.dsimplify >>= trace, constructor -- Example that does not work example (a b : nat) (H : (Ξ» x, f x (id (id (id (foo1 x))))) = (Ξ» x, f x (foo2 x))) : true := by do s ← simp_lemmas.mk_default, get_local `H >>= infer_type >>= s^.dsimplify >>= trace, constructor
cdd962190b369ae9c5a5c979a308d0e6f547577b
a4673261e60b025e2c8c825dfa4ab9108246c32e
/tests/lean/run/108.lean
f07d647b03ea2a869ff09eefae9d5a5dba97ca19
[ "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
65
lean
macro m n:ident : command => `(def $n := 1) m foo #check foo
fb0a700952c00507464c8057e9d53b66dd543a4e
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/test/linarith.lean
88523de91acfb1b4b9f7f94bc7e74d6b885f9810
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
9,844
lean
import tactic.linarith example {Ξ± : Type} (_inst : Ξ  (a : Prop), decidable a) [linear_ordered_field Ξ±] {a b c : Ξ±} (ha : a < 0) (hb : Β¬b = 0) (hc' : c = 0) (h : (1 - a) * (b * b) ≀ 0) (hc : 0 ≀ 0) (this : -(a * -b * -b + b * -b + 0) = (1 - a) * (b * b)) (h : (1 - a) * (b * b) ≀ 0) : 0 < 1 - a := begin linarith end example (e b c a v0 v1 : β„š) (h1 : v0 = 5*a) (h2 : v1 = 3*b) (h3 : v0 + v1 + c = 10) : v0 + 5 + (v1 - 3) + (c - 2) = 10 := by linarith example (u v r s t : β„š) (h : 0 < u*(t*v + t*r + s)) : 0 < (t*(r + v) + s)*3*u := by linarith example (A B : β„š) (h : 0 < A * B) : 0 < 8*A*B := begin linarith end example (A B : β„š) (h : 0 < A * B) : 0 < A*8*B := begin linarith end example (A B : β„š) (h : 0 < A * B) : 0 < A*B/8 := begin linarith end example (A B : β„š) (h : 0 < A * B) : 0 < A/8*B := begin linarith end example (Ξ΅ : β„š) (h1 : Ξ΅ > 0) : Ξ΅ / 2 + Ξ΅ / 3 + Ξ΅ / 7 < Ξ΅ := by linarith example (x y z : β„š) (h1 : 2*x < 3*y) (h2 : -4*x + z/2 < 0) (h3 : 12*y - z < 0) : false := by linarith example (Ξ΅ : β„š) (h1 : Ξ΅ > 0) : Ξ΅ / 2 < Ξ΅ := by linarith example (Ξ΅ : β„š) (h1 : Ξ΅ > 0) : Ξ΅ / 3 + Ξ΅ / 3 + Ξ΅ / 3 = Ξ΅ := by linarith example (a b c : β„š) (h2 : b + 2 > 3 + b) : false := by linarith {discharger := `[ring SOP]} example (a b c : β„š) (h2 : b + 2 > 3 + b) : false := by linarith example (a b c : β„š) (x y : β„€) (h1 : x ≀ 3*y) (h2 : b + 2 > 3 + b) : false := by linarith {restrict_type := β„š} example (g v V c h : β„š) (h1 : h = 0) (h2 : v = V) (h3 : V > 0) (h4 : g > 0) (h5 : 0 ≀ c) (h6 : c < 1) : v ≀ V := by linarith example (x y z : β„š) (h1 : 2*x + ((-3)*y) < 0) (h2 : (-4)*x + 2*z < 0) (h3 : 12*y + (-4)* z < 0) (h4 : nat.prime 7) : false := by linarith example (x y z : β„š) (h1 : 2*1*x + (3)*(y*(-1)) < 0) (h2 : (-2)*x*2 < -(z + z)) (h3 : 12*y + (-4)* z < 0) (h4 : nat.prime 7) : false := by linarith example (x y z : β„€) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : 12*y - 4* z < 0) : false := by linarith example (x y z : β„€) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : x*y < 5) (h3 : 12*y - 4* z < 0) : false := by linarith example (x y z : β„€) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : x*y < 5) : Β¬ 12*y - 4* z < 0 := by linarith example (w x y z : β„€) (h1 : 4*x + (-3)*y + 6*w ≀ 0) (h2 : (-1)*x < 0) (h3 : y < 0) (h4 : w β‰₯ 0) (h5 : nat.prime x.nat_abs) : false := by linarith example (a b c : β„š) (h1 : a > 0) (h2 : b > 5) (h3 : c < -10) (h4 : a + b - c < 3) : false := by linarith example (a b c : β„š) (h2 : b > 0) (h3 : Β¬ b β‰₯ 0) : false := by linarith example (a b c : β„š) (h2 : (2 : β„š) > 3) : a + b - c β‰₯ 3 := by linarith {exfalso := ff} example (x : β„š) (hx : x > 0) (h : x.num < 0) : false := by linarith [rat.num_pos_iff_pos.mpr hx, h] example (x : β„š) (hx : x > 0) (h : x.num < 0) : false := by linarith only [rat.num_pos_iff_pos.mpr hx, h] example (x y z : β„š) (hx : x ≀ 3*y) (h2 : y ≀ 2*z) (h3 : x β‰₯ 6*z) : x = 3*y := by linarith example (x y z : β„•) (hx : x ≀ 3*y) (h2 : y ≀ 2*z) (h3 : x β‰₯ 6*z) : x = 3*y := by linarith example (x y z : β„š) (hx : Β¬ x > 3*y) (h2 : Β¬ y > 2*z) (h3 : x β‰₯ 6*z) : x = 3*y := by linarith example (h1 : (1 : β„•) < 1) : false := by linarith example (a b c : β„š) (h2 : b > 0) (h3 : b < 0) : nat.prime 10 := by linarith example (a b c : β„•) : a + b β‰₯ a := by linarith example (a b c : β„•) : Β¬ a + b < a := by linarith example (x y : β„š) (h : 6 + ((x + 4) * x + (6 + 3 * y) * y) = 3) (h' : (x + 4) * x β‰₯ 0) (h'' : (6 + 3 * y) * y β‰₯ 0) : false := by linarith example (x y : β„š) (h : 6 + ((x + 4) * x + (6 + 3 * y) * y) = 3 ∧ (x + 4) * x β‰₯ 0 ∧ (6 + 3 * y) * y β‰₯ 0) : false := by linarith example (a b i : β„•) (h1 : Β¬ a < i) (h2 : b < i) (h3 : a ≀ b) : false := by linarith example (n : β„•) (h1 : n ≀ 3) (h2 : n > 2) : n = 3 := by linarith example (z : β„•) (hz : Β¬ z β‰₯ 2) (h2 : Β¬ z + 1 ≀ 2) : false := by linarith example (z : β„•) (hz : Β¬ z β‰₯ 2) : z + 1 ≀ 2 := by linarith example (a b c : β„š) (h1 : 1 / a < b) (h2 : b < c) : 1 / a < c := by linarith example (N : β„•) (n : β„•) (Hirrelevant : n > N) (A : β„š) (l : β„š) (h : A - l ≀ -(A - l)) (h_1 : Β¬A ≀ -A) (h_2 : Β¬l ≀ -l) (h_3 : -(A - l) < 1) : A < l + 1 := by linarith example (d : β„š) (q n : β„•) (h1 : ((q : β„š) - 1)*n β‰₯ 0) (h2 : d = 2/3*(((q : β„š) - 1)*n)) : d ≀ ((q : β„š) - 1)*n := by linarith example (d : β„š) (q n : β„•) (h1 : ((q : β„š) - 1)*n β‰₯ 0) (h2 : d = 2/3*(((q : β„š) - 1)*n)) : ((q : β„š) - 1)*n - d = 1/3 * (((q : β„š) - 1)*n) := by linarith example (a : β„š) (ha : 0 ≀ a) : 0 * 0 ≀ 2 * a := by linarith example (x : β„š) : id x β‰₯ x := by success_if_fail {linarith}; linarith! example (x y z : β„š) (hx : x < 5) (hx2 : x > 5) (hy : y < 5000000000) (hz : z > 34*y) : false := by linarith only [hx, hx2] example (x y z : β„š) (hx : x < 5) (hy : y < 5000000000) (hz : z > 34*y) : x ≀ 5 := by linarith only [hx] example (x y : β„š) (h : x < y) : x β‰  y := by linarith example (x y : β„š) (h : x < y) : Β¬ x = y := by linarith example (u v x y A B : β„š) (a : 0 < A) (a_1 : 0 <= 1 - A) (a_2 : 0 <= B - 1) (a_3 : 0 <= B - x) (a_4 : 0 <= B - y) (a_5 : 0 <= u) (a_6 : 0 <= v) (a_7 : 0 < A - u) (a_8 : 0 < A - v) : u * y + v * x + u * v < 3 * A * B := by nlinarith example (u v x y A B : β„š) : (0 < A) β†’ (A ≀ 1) β†’ (1 ≀ B) β†’ (x ≀ B) β†’ ( y ≀ B) β†’ (0 ≀ u ) β†’ (0 ≀ v ) β†’ (u < A) β†’ ( v < A) β†’ (u * y + v * x + u * v < 3 * A * B) := begin intros, nlinarith end example (u v x y A B : β„š) (a : 0 < A) (a_1 : 0 <= 1 - A) (a_2 : 0 <= B - 1) (a_3 : 0 <= B - x) (a_4 : 0 <= B - y) (a_5 : 0 <= u) (a_6 : 0 <= v) (a_7 : 0 < A - u) (a_8 : 0 < A - v) : (0 < A * A) -> (0 <= A * (1 - A)) -> (0 <= A * (B - 1)) -> (0 <= A * (B - x)) -> (0 <= A * (B - y)) -> (0 <= A * u) -> (0 <= A * v) -> (0 < A * (A - u)) -> (0 < A * (A - v)) -> (0 <= (1 - A) * A) -> (0 <= (1 - A) * (1 - A)) -> (0 <= (1 - A) * (B - 1)) -> (0 <= (1 - A) * (B - x)) -> (0 <= (1 - A) * (B - y)) -> (0 <= (1 - A) * u) -> (0 <= (1 - A) * v) -> (0 <= (1 - A) * (A - u)) -> (0 <= (1 - A) * (A - v)) -> (0 <= (B - 1) * A) -> (0 <= (B - 1) * (1 - A)) -> (0 <= (B - 1) * (B - 1)) -> (0 <= (B - 1) * (B - x)) -> (0 <= (B - 1) * (B - y)) -> (0 <= (B - 1) * u) -> (0 <= (B - 1) * v) -> (0 <= (B - 1) * (A - u)) -> (0 <= (B - 1) * (A - v)) -> (0 <= (B - x) * A) -> (0 <= (B - x) * (1 - A)) -> (0 <= (B - x) * (B - 1)) -> (0 <= (B - x) * (B - x)) -> (0 <= (B - x) * (B - y)) -> (0 <= (B - x) * u) -> (0 <= (B - x) * v) -> (0 <= (B - x) * (A - u)) -> (0 <= (B - x) * (A - v)) -> (0 <= (B - y) * A) -> (0 <= (B - y) * (1 - A)) -> (0 <= (B - y) * (B - 1)) -> (0 <= (B - y) * (B - x)) -> (0 <= (B - y) * (B - y)) -> (0 <= (B - y) * u) -> (0 <= (B - y) * v) -> (0 <= (B - y) * (A - u)) -> (0 <= (B - y) * (A - v)) -> (0 <= u * A) -> (0 <= u * (1 - A)) -> (0 <= u * (B - 1)) -> (0 <= u * (B - x)) -> (0 <= u * (B - y)) -> (0 <= u * u) -> (0 <= u * v) -> (0 <= u * (A - u)) -> (0 <= u * (A - v)) -> (0 <= v * A) -> (0 <= v * (1 - A)) -> (0 <= v * (B - 1)) -> (0 <= v * (B - x)) -> (0 <= v * (B - y)) -> (0 <= v * u) -> (0 <= v * v) -> (0 <= v * (A - u)) -> (0 <= v * (A - v)) -> (0 < (A - u) * A) -> (0 <= (A - u) * (1 - A)) -> (0 <= (A - u) * (B - 1)) -> (0 <= (A - u) * (B - x)) -> (0 <= (A - u) * (B - y)) -> (0 <= (A - u) * u) -> (0 <= (A - u) * v) -> (0 < (A - u) * (A - u)) -> (0 < (A - u) * (A - v)) -> (0 < (A - v) * A) -> (0 <= (A - v) * (1 - A)) -> (0 <= (A - v) * (B - 1)) -> (0 <= (A - v) * (B - x)) -> (0 <= (A - v) * (B - y)) -> (0 <= (A - v) * u) -> (0 <= (A - v) * v) -> (0 < (A - v) * (A - u)) -> (0 < (A - v) * (A - v)) -> u * y + v * x + u * v < 3 * A * B := begin intros, linarith end example (A B : β„š) : (0 < A) β†’ (1 ≀ B) β†’ (0 < A / 8 * B) := begin intros, nlinarith end example (x y : β„š) : 0 ≀ x ^2 + y ^2 := by nlinarith example (x y : β„š) : 0 ≀ x*x + y*y := by nlinarith example (x y : β„š) : x = 0 β†’ y = 0 β†’ x*x + y*y = 0 := by intros; nlinarith lemma norm_eq_zero_iff {x y : β„š} : x * x + y * y = 0 ↔ x = 0 ∧ y = 0 := begin split, { intro, split; nlinarith }, { intro, nlinarith } end lemma norm_nonpos_right {x y : β„š} (h1 : x * x + y * y ≀ 0) : y = 0 := by nlinarith lemma norm_nonpos_left (x y : β„š) (h1 : x * x + y * y ≀ 0) : x = 0 := by nlinarith variables {E : Type*} [add_group E] example (f : β„€ β†’ E) (h : 0 = f 0) : 1 ≀ 2 := by nlinarith example (a : E) (h : a = a) : 1 ≀ 2 := by nlinarith -- test that the apply bug doesn't affect linarith preprocessing constant Ξ± : Type def leΞ± : Ξ± β†’ Ξ± β†’ Prop := Ξ» a b, βˆ€ c : Ξ±, true noncomputable instance : discrete_linear_ordered_field Ξ± := by refine_struct { le := leΞ± }; admit example (a : Ξ±) (ha : a < 2) : a ≀ a := by linarith example (p q r s t u v w : β„•) (h1 : p + u = q + t) (h2 : r + w = s + v) : p * r + q * s + (t * w + u * v) = p * s + q * r + (t * v + u * w) := by nlinarith -- Tests involving a norm, including that squares in a type where `pow_two_nonneg` does not apply -- do not cause an exception variables {R : Type*} [ring R] (abs : R β†’ β„š) lemma abs_nonneg' : βˆ€ r, 0 ≀ abs r := sorry example (t : R) (a b : β„š) (h : a ≀ b) : abs (t^2) * a ≀ abs (t^2) * b := by nlinarith [abs_nonneg' abs (t^2)] example (t : R) (a b : β„š) (h : a ≀ b) : a ≀ abs (t^2) + b := by linarith [abs_nonneg' abs (t^2)] example (t : R) (a b : β„š) (h : a ≀ b) : abs t * a ≀ abs t * b := by nlinarith [abs_nonneg' abs t] constant T : Type attribute [instance] constant T_zero : ordered_ring T namespace T lemma zero_lt_one : (0 : T) < 1 := sorry lemma works {a b : β„•} (hab : a ≀ b) (h : b < a) : false := begin linarith, end end T
a3810df4a3b0a04ac9b45010f5721c41d4872713
0c1546a496eccfb56620165cad015f88d56190c5
/library/init/data/quot.lean
3c69076391e5f3060ca6d1e680d3f0d01bac2c4b
[ "Apache-2.0" ]
permissive
Solertis/lean
491e0939957486f664498fbfb02546e042699958
84188c5aa1673fdf37a082b2de8562dddf53df3f
refs/heads/master
1,610,174,257,606
1,486,263,620,000
1,486,263,620,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,401
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura Quotient types. -/ prelude /- We import propext here, otherwise we would need a quot.lift for propositions. -/ import init.data.sigma.basic init.logic init.propext init.data.setoid universe variables u v -- iff can now be used to do substitutions in a calculation attribute [subst] lemma iff_subst {a b : Prop} {p : Prop β†’ Prop} (h₁ : a ↔ b) (hβ‚‚ : p a) : p b := eq.subst (propext h₁) hβ‚‚ namespace quot constant sound : Ξ  {Ξ± : Type u} {r : Ξ± β†’ Ξ± β†’ Prop} {a b : Ξ±}, r a b β†’ quot.mk r a = quot.mk r b attribute [elab_as_eliminator] lift ind protected lemma lift_beta {Ξ± : Type u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Type v} (f : Ξ± β†’ Ξ²) (c : βˆ€ a b, r a b β†’ f a = f b) (a : Ξ±) : lift f c (quot.mk r a) = f a := rfl protected lemma ind_beta {Ξ± : Type u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : quot r β†’ Prop} (p : βˆ€ a, Ξ² (quot.mk r a)) (a : Ξ±) : (ind p (quot.mk r a) : Ξ² (quot.mk r a)) = p a := rfl attribute [reducible, elab_as_eliminator] protected def lift_on {Ξ± : Type u} {Ξ² : Type v} {r : Ξ± β†’ Ξ± β†’ Prop} (q : quot r) (f : Ξ± β†’ Ξ²) (c : βˆ€ a b, r a b β†’ f a = f b) : Ξ² := lift f c q attribute [elab_as_eliminator] protected lemma induction_on {Ξ± : Type u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : quot r β†’ Prop} (q : quot r) (h : βˆ€ a, Ξ² (quot.mk r a)) : Ξ² q := ind h q lemma exists_rep {Ξ± : Type u} {r : Ξ± β†’ Ξ± β†’ Prop} (q : quot r) : βˆƒ a : Ξ±, (quot.mk r a) = q := quot.induction_on q (Ξ» a, ⟨a, rfl⟩) section variable {Ξ± : Type u} variable {r : Ξ± β†’ Ξ± β†’ Prop} variable {Ξ² : quot r β†’ Type v} local notation `⟦`:max a `⟧` := quot.mk r a attribute [reducible] protected def indep (f : Ξ  a, Ξ² ⟦a⟧) (a : Ξ±) : Ξ£ q, Ξ² q := ⟨⟦a⟧, f a⟩ protected lemma indep_coherent (f : Ξ  a, Ξ² ⟦a⟧) (h : βˆ€ (a b : Ξ±) (p : r a b), (eq.rec (f a) (sound p) : Ξ² ⟦b⟧) = f b) : βˆ€ a b, r a b β†’ quot.indep f a = quot.indep f b := Ξ» a b e, sigma.eq (sound e) (h a b e) protected lemma lift_indep_pr1 (f : Ξ  a, Ξ² ⟦a⟧) (h : βˆ€ (a b : Ξ±) (p : r a b), (eq.rec (f a) (sound p) : Ξ² ⟦b⟧) = f b) (q : quot r) : (lift (quot.indep f) (quot.indep_coherent f h) q).1 = q := quot.ind (Ξ» (a : Ξ±), eq.refl (quot.indep f a).1) q attribute [reducible, elab_as_eliminator] protected def rec (f : Ξ  a, Ξ² ⟦a⟧) (h : βˆ€ (a b : Ξ±) (p : r a b), (eq.rec (f a) (sound p) : Ξ² ⟦b⟧) = f b) (q : quot r) : Ξ² q := eq.rec_on (quot.lift_indep_pr1 f h q) ((lift (quot.indep f) (quot.indep_coherent f h) q).2) attribute [reducible, elab_as_eliminator] protected def rec_on (q : quot r) (f : Ξ  a, Ξ² ⟦a⟧) (h : βˆ€ (a b : Ξ±) (p : r a b), (eq.rec (f a) (sound p) : Ξ² ⟦b⟧) = f b) : Ξ² q := quot.rec f h q attribute [reducible, elab_as_eliminator] protected def rec_on_subsingleton [h : βˆ€ a, subsingleton (Ξ² ⟦a⟧)] (q : quot r) (f : Ξ  a, Ξ² ⟦a⟧) : Ξ² q := quot.rec f (Ξ» a b h, subsingleton.elim _ (f b)) q attribute [reducible, elab_as_eliminator] protected def hrec_on (q : quot r) (f : Ξ  a, Ξ² ⟦a⟧) (c : βˆ€ (a b : Ξ±) (p : r a b), f a == f b) : Ξ² q := quot.rec_on q f (Ξ» a b p, eq_of_heq (calc (eq.rec (f a) (sound p) : Ξ² ⟦b⟧) == f a : eq_rec_heq (sound p) (f a) ... == f b : c a b p)) end end quot def quotient {Ξ± : Type u} (s : setoid Ξ±) := @quot Ξ± setoid.r namespace quotient protected def mk {Ξ± : Type u} [s : setoid Ξ±] (a : Ξ±) : quotient s := quot.mk setoid.r a notation `⟦`:max a `⟧`:0 := quotient.mk a def sound {Ξ± : Type u} [s : setoid Ξ±] {a b : Ξ±} : a β‰ˆ b β†’ ⟦a⟧ = ⟦b⟧ := quot.sound attribute [reducible, elab_as_eliminator] protected def lift {Ξ± : Type u} {Ξ² : Type v} [s : setoid Ξ±] (f : Ξ± β†’ Ξ²) : (βˆ€ a b, a β‰ˆ b β†’ f a = f b) β†’ quotient s β†’ Ξ² := quot.lift f attribute [elab_as_eliminator] protected lemma ind {Ξ± : Type u} [s : setoid Ξ±] {Ξ² : quotient s β†’ Prop} : (βˆ€ a, Ξ² ⟦a⟧) β†’ βˆ€ q, Ξ² q := quot.ind attribute [reducible, elab_as_eliminator] protected def lift_on {Ξ± : Type u} {Ξ² : Type v} [s : setoid Ξ±] (q : quotient s) (f : Ξ± β†’ Ξ²) (c : βˆ€ a b, a β‰ˆ b β†’ f a = f b) : Ξ² := quot.lift_on q f c attribute [elab_as_eliminator] protected lemma induction_on {Ξ± : Type u} [s : setoid Ξ±] {Ξ² : quotient s β†’ Prop} (q : quotient s) (h : βˆ€ a, Ξ² ⟦a⟧) : Ξ² q := quot.induction_on q h lemma exists_rep {Ξ± : Type u} [s : setoid Ξ±] (q : quotient s) : βˆƒ a : Ξ±, ⟦a⟧ = q := quot.exists_rep q section variable {Ξ± : Type u} variable [s : setoid Ξ±] variable {Ξ² : quotient s β†’ Type v} protected def rec (f : Ξ  a, Ξ² ⟦a⟧) (h : βˆ€ (a b : Ξ±) (p : a β‰ˆ b), (eq.rec (f a) (quotient.sound p) : Ξ² ⟦b⟧) = f b) (q : quotient s) : Ξ² q := quot.rec f h q attribute [reducible, elab_as_eliminator] protected def rec_on (q : quotient s) (f : Ξ  a, Ξ² ⟦a⟧) (h : βˆ€ (a b : Ξ±) (p : a β‰ˆ b), (eq.rec (f a) (quotient.sound p) : Ξ² ⟦b⟧) = f b) : Ξ² q := quot.rec_on q f h attribute [reducible, elab_as_eliminator] protected def rec_on_subsingleton [h : βˆ€ a, subsingleton (Ξ² ⟦a⟧)] (q : quotient s) (f : Ξ  a, Ξ² ⟦a⟧) : Ξ² q := @quot.rec_on_subsingleton _ _ _ h q f attribute [reducible, elab_as_eliminator] protected def hrec_on (q : quotient s) (f : Ξ  a, Ξ² ⟦a⟧) (c : βˆ€ (a b : Ξ±) (p : a β‰ˆ b), f a == f b) : Ξ² q := quot.hrec_on q f c end section universe variables u_a u_b u_c variables {Ξ± : Type u_a} {Ξ² : Type u_b} {Ο† : Type u_c} variables [s₁ : setoid Ξ±] [sβ‚‚ : setoid Ξ²] include s₁ sβ‚‚ attribute [reducible, elab_as_eliminator] protected def liftβ‚‚ (f : Ξ± β†’ Ξ² β†’ Ο†)(c : βˆ€ a₁ aβ‚‚ b₁ bβ‚‚, a₁ β‰ˆ b₁ β†’ aβ‚‚ β‰ˆ bβ‚‚ β†’ f a₁ aβ‚‚ = f b₁ bβ‚‚) (q₁ : quotient s₁) (qβ‚‚ : quotient sβ‚‚) : Ο† := quotient.lift (Ξ» (a₁ : Ξ±), quot.lift (f a₁) (Ξ» (a b : Ξ²), c a₁ a a₁ b (setoid.refl a₁)) qβ‚‚) (Ξ» (a b : Ξ±) (h : a β‰ˆ b), @quotient.ind Ξ² sβ‚‚ (Ξ» (a_1 : quotient sβ‚‚), (quotient.lift (f a) (Ξ» (a_1 b : Ξ²), c a a_1 a b (setoid.refl a)) a_1) = (quotient.lift (f b) (Ξ» (a b_1 : Ξ²), c b a b b_1 (setoid.refl b)) a_1)) (Ξ» (a' : Ξ²), c a a' b a' h (setoid.refl a')) qβ‚‚) q₁ attribute [reducible, elab_as_eliminator] protected def lift_onβ‚‚ (q₁ : quotient s₁) (qβ‚‚ : quotient sβ‚‚) (f : Ξ± β†’ Ξ² β†’ Ο†) (c : βˆ€ a₁ aβ‚‚ b₁ bβ‚‚, a₁ β‰ˆ b₁ β†’ aβ‚‚ β‰ˆ bβ‚‚ β†’ f a₁ aβ‚‚ = f b₁ bβ‚‚) : Ο† := quotient.liftβ‚‚ f c q₁ qβ‚‚ attribute [elab_as_eliminator] protected lemma indβ‚‚ {Ο† : quotient s₁ β†’ quotient sβ‚‚ β†’ Prop} (h : βˆ€ a b, Ο† ⟦a⟧ ⟦b⟧) (q₁ : quotient s₁) (qβ‚‚ : quotient sβ‚‚) : Ο† q₁ qβ‚‚ := quotient.ind (Ξ» a₁, quotient.ind (Ξ» aβ‚‚, h a₁ aβ‚‚) qβ‚‚) q₁ attribute [elab_as_eliminator] protected lemma induction_onβ‚‚ {Ο† : quotient s₁ β†’ quotient sβ‚‚ β†’ Prop} (q₁ : quotient s₁) (qβ‚‚ : quotient sβ‚‚) (h : βˆ€ a b, Ο† ⟦a⟧ ⟦b⟧) : Ο† q₁ qβ‚‚ := quotient.ind (Ξ» a₁, quotient.ind (Ξ» aβ‚‚, h a₁ aβ‚‚) qβ‚‚) q₁ attribute [elab_as_eliminator] protected lemma induction_on₃ [s₃ : setoid Ο†] {Ξ΄ : quotient s₁ β†’ quotient sβ‚‚ β†’ quotient s₃ β†’ Prop} (q₁ : quotient s₁) (qβ‚‚ : quotient sβ‚‚) (q₃ : quotient s₃) (h : βˆ€ a b c, Ξ΄ ⟦a⟧ ⟦b⟧ ⟦c⟧) : Ξ΄ q₁ qβ‚‚ q₃ := quot.ind (Ξ» a₁, quot.ind (Ξ» aβ‚‚, quot.ind (Ξ» a₃, h a₁ aβ‚‚ a₃) q₃) qβ‚‚) q₁ end section exact variable {Ξ± : Type u} variable [s : setoid Ξ±] include s private def rel (q₁ qβ‚‚ : quotient s) : Prop := quotient.lift_onβ‚‚ q₁ qβ‚‚ (Ξ» a₁ aβ‚‚, a₁ β‰ˆ aβ‚‚) (Ξ» a₁ aβ‚‚ b₁ bβ‚‚ a₁b₁ aβ‚‚bβ‚‚, propext (iff.intro (Ξ» a₁aβ‚‚, setoid.trans (setoid.symm a₁b₁) (setoid.trans a₁aβ‚‚ aβ‚‚bβ‚‚)) (Ξ» b₁bβ‚‚, setoid.trans a₁b₁ (setoid.trans b₁bβ‚‚ (setoid.symm aβ‚‚bβ‚‚))))) local infix `~` := rel private lemma rel.refl : βˆ€ q : quotient s, q ~ q := Ξ» q, quot.induction_on q (Ξ» a, setoid.refl a) private lemma eq_imp_rel {q₁ qβ‚‚ : quotient s} : q₁ = qβ‚‚ β†’ q₁ ~ qβ‚‚ := assume h, eq.rec_on h (rel.refl q₁) lemma exact {a b : Ξ±} : ⟦a⟧ = ⟦b⟧ β†’ a β‰ˆ b := assume h, eq_imp_rel h end exact section universe variables u_a u_b u_c variables {Ξ± : Type u_a} {Ξ² : Type u_b} variables [s₁ : setoid Ξ±] [sβ‚‚ : setoid Ξ²] include s₁ sβ‚‚ attribute [reducible, elab_as_eliminator] protected def rec_on_subsingletonβ‚‚ {Ο† : quotient s₁ β†’ quotient sβ‚‚ β†’ Type u_c} [h : βˆ€ a b, subsingleton (Ο† ⟦a⟧ ⟦b⟧)] (q₁ : quotient s₁) (qβ‚‚ : quotient sβ‚‚) (f : Ξ  a b, Ο† ⟦a⟧ ⟦b⟧) : Ο† q₁ qβ‚‚:= @quotient.rec_on_subsingleton _ s₁ (Ξ» q, Ο† q qβ‚‚) (Ξ» a, quotient.ind (Ξ» b, h a b) qβ‚‚) q₁ (Ξ» a, quotient.rec_on_subsingleton qβ‚‚ (Ξ» b, f a b)) end end quotient open decidable instance {Ξ± : Type u} {s : setoid Ξ±} [d : βˆ€ a b : Ξ±, decidable (a β‰ˆ b)] : decidable_eq (quotient s) := Ξ» q₁ qβ‚‚ : quotient s, quotient.rec_on_subsingletonβ‚‚ q₁ qβ‚‚ (Ξ» a₁ aβ‚‚, match (d a₁ aβ‚‚) with | (is_true h₁) := is_true (quotient.sound h₁) | (is_false hβ‚‚) := is_false (Ξ» h, absurd (quotient.exact h) hβ‚‚) end)
0b635ead2eba1e5ee3557a7ef31d9f2cf3e0b314
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/list/big_operators/basic.lean
4e6925515baf0f182acdc81edd0d8b0ffda8e9e4
[ "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
23,316
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, Floris van Doorn, SΓ©bastien GouΓ«zel, Alex J. Best -/ import data.int.order.basic import data.list.forall2 /-! # Sums and products from lists > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file provides basic results about `list.prod`, `list.sum`, which calculate the product and sum of elements of a list and `list.alternating_prod`, `list.alternating_sum`, their alternating counterparts. These are defined in [`data.list.defs`](./defs). -/ variables {ΞΉ Ξ± M N P Mβ‚€ G R : Type*} namespace list section monoid variables [monoid M] [monoid N] [monoid P] {l l₁ lβ‚‚ : list M} {a : M} @[simp, to_additive] lemma prod_nil : ([] : list M).prod = 1 := rfl @[to_additive] lemma prod_singleton : [a].prod = a := one_mul a @[simp, to_additive] lemma prod_cons : (a :: l).prod = a * l.prod := calc (a :: l).prod = foldl (*) (a * 1) l : by simp only [list.prod, foldl_cons, one_mul, mul_one] ... = _ : foldl_assoc @[simp, to_additive] lemma prod_append : (l₁ ++ lβ‚‚).prod = l₁.prod * lβ‚‚.prod := calc (l₁ ++ lβ‚‚).prod = foldl (*) (foldl (*) 1 l₁ * 1) lβ‚‚ : by simp [list.prod] ... = l₁.prod * lβ‚‚.prod : foldl_assoc @[to_additive] lemma prod_concat : (l.concat a).prod = l.prod * a := by rw [concat_eq_append, prod_append, prod_singleton] @[simp, to_additive] lemma prod_join {l : list (list M)} : l.join.prod = (l.map list.prod).prod := by induction l; [refl, simp only [*, list.join, map, prod_append, prod_cons]] @[to_additive] lemma prod_eq_foldr : l.prod = foldr (*) 1 l := list.rec_on l rfl $ Ξ» a l ihl, by rw [prod_cons, foldr_cons, ihl] @[simp, priority 500, to_additive] theorem prod_replicate (n : β„•) (a : M) : (replicate n a).prod = a ^ n := begin induction n with n ih, { rw pow_zero, refl }, { rw [list.replicate_succ, list.prod_cons, ih, pow_succ] } end @[to_additive sum_eq_card_nsmul] lemma prod_eq_pow_card (l : list M) (m : M) (h : βˆ€ (x ∈ l), x = m) : l.prod = m ^ l.length := by rw [← prod_replicate, ← eq_replicate_length.2 h] @[to_additive] lemma prod_hom_rel (l : list ΞΉ) {r : M β†’ N β†’ Prop} {f : ΞΉ β†’ M} {g : ΞΉ β†’ N} (h₁ : r 1 1) (hβ‚‚ : βˆ€ ⦃i a b⦄, r a b β†’ r (f i * a) (g i * b)) : r (l.map f).prod (l.map g).prod := list.rec_on l h₁ (Ξ» a l hl, by simp only [map_cons, prod_cons, hβ‚‚ hl]) @[to_additive] lemma prod_hom (l : list M) {F : Type*} [monoid_hom_class F M N] (f : F) : (l.map f).prod = f l.prod := by { simp only [prod, foldl_map, ← map_one f], exact l.foldl_hom _ _ _ 1 (map_mul f) } @[to_additive] lemma prod_homβ‚‚ (l : list ΞΉ) (f : M β†’ N β†’ P) (hf : βˆ€ a b c d, f (a * b) (c * d) = f a c * f b d) (hf' : f 1 1 = 1) (f₁ : ΞΉ β†’ M) (fβ‚‚ : ΞΉ β†’ N) : (l.map $ Ξ» i, f (f₁ i) (fβ‚‚ i)).prod = f (l.map f₁).prod (l.map fβ‚‚).prod := begin simp only [prod, foldl_map], convert l.foldl_homβ‚‚ (Ξ» a b, f a b) _ _ _ _ _ (Ξ» a b i, _), { exact hf'.symm }, { exact hf _ _ _ _ } end @[simp, to_additive] lemma prod_map_mul {Ξ± : Type*} [comm_monoid Ξ±] {l : list ΞΉ} {f g : ΞΉ β†’ Ξ±} : (l.map $ Ξ» i, f i * g i).prod = (l.map f).prod * (l.map g).prod := l.prod_homβ‚‚ (*) mul_mul_mul_comm (mul_one _) _ _ @[simp] lemma prod_map_neg {Ξ±} [comm_monoid Ξ±] [has_distrib_neg Ξ±] (l : list Ξ±) : (l.map has_neg.neg).prod = (-1) ^ l.length * l.prod := by simpa only [id, neg_mul, one_mul, map_const', prod_replicate, map_id] using @prod_map_mul Ξ± Ξ± _ l (Ξ» _, -1) id @[to_additive] lemma prod_map_hom (L : list ΞΉ) (f : ΞΉ β†’ M) {G : Type*} [monoid_hom_class G M N] (g : G) : (L.map (g ∘ f)).prod = g ((L.map f).prod) := by rw [← prod_hom, map_map] @[to_additive] lemma prod_is_unit : Ξ  {L : list M} (u : βˆ€ m ∈ L, is_unit m), is_unit L.prod | [] _ := by simp | (h :: t) u := begin simp only [list.prod_cons], exact is_unit.mul (u h (mem_cons_self h t)) (prod_is_unit (Ξ» m mt, u m (mem_cons_of_mem h mt))) end @[to_additive] lemma prod_is_unit_iff {Ξ± : Type*} [comm_monoid Ξ±] {L : list Ξ±} : is_unit L.prod ↔ βˆ€ m ∈ L, is_unit m := begin refine ⟨λ h, _, prod_is_unit⟩, induction L with m L ih, { exact Ξ» m' h', false.elim (not_mem_nil m' h'), }, rw [prod_cons, is_unit.mul_iff] at h, exact Ξ» m' h', or.elim (eq_or_mem_of_mem_cons h') (Ξ» H, H.substr h.1) (Ξ» H, ih h.2 _ H), end @[simp, to_additive] lemma prod_take_mul_prod_drop : βˆ€ (L : list M) (i : β„•), (L.take i).prod * (L.drop i).prod = L.prod | [] i := by simp [nat.zero_le] | L 0 := by simp | (h :: t) (n+1) := by { dsimp, rw [prod_cons, prod_cons, mul_assoc, prod_take_mul_prod_drop] } @[simp, to_additive] lemma prod_take_succ : βˆ€ (L : list M) (i : β„•) (p), (L.take (i + 1)).prod = (L.take i).prod * L.nth_le i p | [] i p := by cases p | (h :: t) 0 _ := by simp | (h :: t) (n+1) _ := by { dsimp, rw [prod_cons, prod_cons, prod_take_succ, mul_assoc] } /-- A list with product not one must have positive length. -/ @[to_additive "A list with sum not zero must have positive length."] lemma length_pos_of_prod_ne_one (L : list M) (h : L.prod β‰  1) : 0 < L.length := by { cases L, { contrapose h, simp }, { simp } } /-- A list with product greater than one must have positive length. -/ @[to_additive length_pos_of_sum_pos "A list with positive sum must have positive length."] lemma length_pos_of_one_lt_prod [preorder M] (L : list M) (h : 1 < L.prod) : 0 < L.length := length_pos_of_prod_ne_one L h.ne' /-- A list with product less than one must have positive length. -/ @[to_additive "A list with negative sum must have positive length."] lemma length_pos_of_prod_lt_one [preorder M] (L : list M) (h : L.prod < 1) : 0 < L.length := length_pos_of_prod_ne_one L h.ne @[to_additive] lemma prod_update_nth : βˆ€ (L : list M) (n : β„•) (a : M), (L.update_nth n a).prod = (L.take n).prod * (if n < L.length then a else 1) * (L.drop (n + 1)).prod | (x :: xs) 0 a := by simp [update_nth] | (x :: xs) (i+1) a := by simp [update_nth, prod_update_nth xs i a, mul_assoc] | [] _ _ := by simp [update_nth, (nat.zero_le _).not_lt, nat.zero_le] open mul_opposite /-- We'd like to state this as `L.head * L.tail.prod = L.prod`, but because `L.head` relies on an inhabited instance to return a garbage value on the empty list, this is not possible. Instead, we write the statement in terms of `(L.nth 0).get_or_else 1`. -/ @[to_additive "We'd like to state this as `L.head + L.tail.sum = L.sum`, but because `L.head` relies on an inhabited instance to return a garbage value on the empty list, this is not possible. Instead, we write the statement in terms of `(L.nth 0).get_or_else 0`."] lemma nth_zero_mul_tail_prod (l : list M) : (l.nth 0).get_or_else 1 * l.tail.prod = l.prod := by cases l; simp /-- Same as `nth_zero_mul_tail_prod`, but avoiding the `list.head` garbage complication by requiring the list to be nonempty. -/ @[to_additive "Same as `nth_zero_add_tail_sum`, but avoiding the `list.head` garbage complication by requiring the list to be nonempty."] lemma head_mul_tail_prod_of_ne_nil [inhabited M] (l : list M) (h : l β‰  []) : l.head * l.tail.prod = l.prod := by cases l; [contradiction, simp] @[to_additive] lemma _root_.commute.list_prod_right (l : list M) (y : M) (h : βˆ€ (x ∈ l), commute y x) : commute y l.prod := begin induction l with z l IH, { simp }, { rw list.ball_cons at h, rw list.prod_cons, exact commute.mul_right h.1 (IH h.2), } end @[to_additive] lemma _root_.commute.list_prod_left (l : list M) (y : M) (h : βˆ€ (x ∈ l), commute x y) : commute l.prod y := (commute.list_prod_right _ _ $ Ξ» x hx, (h _ hx).symm).symm @[to_additive sum_le_sum] lemma forallβ‚‚.prod_le_prod' [preorder M] [covariant_class M M (function.swap (*)) (≀)] [covariant_class M M (*) (≀)] {l₁ lβ‚‚ : list M} (h : forallβ‚‚ (≀) l₁ lβ‚‚) : l₁.prod ≀ lβ‚‚.prod := begin induction h with a b la lb hab ih ih', { refl }, { simpa only [prod_cons] using mul_le_mul' hab ih' } end /-- If `l₁` is a sublist of `lβ‚‚` and all elements of `lβ‚‚` are greater than or equal to one, then `l₁.prod ≀ lβ‚‚.prod`. One can prove a stronger version assuming `βˆ€ a ∈ lβ‚‚.diff l₁, 1 ≀ a` instead of `βˆ€ a ∈ lβ‚‚, 1 ≀ a` but this lemma is not yet in `mathlib`. -/ @[to_additive sum_le_sum "If `l₁` is a sublist of `lβ‚‚` and all elements of `lβ‚‚` are nonnegative, then `l₁.sum ≀ lβ‚‚.sum`. One can prove a stronger version assuming `βˆ€ a ∈ lβ‚‚.diff l₁, 0 ≀ a` instead of `βˆ€ a ∈ lβ‚‚, 0 ≀ a` but this lemma is not yet in `mathlib`."] lemma sublist.prod_le_prod' [preorder M] [covariant_class M M (function.swap (*)) (≀)] [covariant_class M M (*) (≀)] {l₁ lβ‚‚ : list M} (h : l₁ <+ lβ‚‚) (h₁ : βˆ€ a ∈ lβ‚‚, (1 : M) ≀ a) : l₁.prod ≀ lβ‚‚.prod := begin induction h, { refl }, case cons : l₁ lβ‚‚ a ih ih' { simp only [prod_cons, forall_mem_cons] at h₁ ⊒, exact (ih' h₁.2).trans (le_mul_of_one_le_left' h₁.1) }, case cons2 : l₁ lβ‚‚ a ih ih' { simp only [prod_cons, forall_mem_cons] at h₁ ⊒, exact mul_le_mul_left' (ih' h₁.2) _ } end @[to_additive sum_le_sum] lemma sublist_forallβ‚‚.prod_le_prod' [preorder M] [covariant_class M M (function.swap (*)) (≀)] [covariant_class M M (*) (≀)] {l₁ lβ‚‚ : list M} (h : sublist_forallβ‚‚ (≀) l₁ lβ‚‚) (h₁ : βˆ€ a ∈ lβ‚‚, (1 : M) ≀ a) : l₁.prod ≀ lβ‚‚.prod := let ⟨l, hall, hsub⟩ := sublist_forallβ‚‚_iff.1 h in hall.prod_le_prod'.trans $ hsub.prod_le_prod' h₁ @[to_additive sum_le_sum] lemma prod_le_prod' [preorder M] [covariant_class M M (function.swap (*)) (≀)] [covariant_class M M (*) (≀)] {l : list ΞΉ} {f g : ΞΉ β†’ M} (h : βˆ€ i ∈ l, f i ≀ g i) : (l.map f).prod ≀ (l.map g).prod := forallβ‚‚.prod_le_prod' $ by simpa @[to_additive sum_lt_sum] lemma prod_lt_prod' [preorder M] [covariant_class M M (*) (<)] [covariant_class M M (*) (≀)] [covariant_class M M (function.swap (*)) (<)] [covariant_class M M (function.swap (*)) (≀)] {l : list ΞΉ} (f g : ΞΉ β†’ M) (h₁ : βˆ€ i ∈ l, f i ≀ g i) (hβ‚‚ : βˆƒ i ∈ l, f i < g i) : (l.map f).prod < (l.map g).prod := begin induction l with i l ihl, { rcases hβ‚‚ with ⟨_, ⟨⟩, _⟩ }, simp only [ball_cons, bex_cons, map_cons, prod_cons] at h₁ hβ‚‚ ⊒, cases hβ‚‚, exacts [mul_lt_mul_of_lt_of_le hβ‚‚ (prod_le_prod' h₁.2), mul_lt_mul_of_le_of_lt h₁.1 $ ihl h₁.2 hβ‚‚] end @[to_additive] lemma prod_lt_prod_of_ne_nil [preorder M] [covariant_class M M (*) (<)] [covariant_class M M (*) (≀)] [covariant_class M M (function.swap (*)) (<)] [covariant_class M M (function.swap (*)) (≀)] {l : list ΞΉ} (hl : l β‰  []) (f g : ΞΉ β†’ M) (hlt : βˆ€ i ∈ l, f i < g i) : (l.map f).prod < (l.map g).prod := prod_lt_prod' f g (Ξ» i hi, (hlt i hi).le) $ (exists_mem_of_ne_nil l hl).imp $ Ξ» i hi, ⟨hi, hlt i hi⟩ @[to_additive sum_le_card_nsmul] lemma prod_le_pow_card [preorder M] [covariant_class M M (function.swap (*)) (≀)] [covariant_class M M (*) (≀)] (l : list M) (n : M) (h : βˆ€ (x ∈ l), x ≀ n) : l.prod ≀ n ^ l.length := by simpa only [map_id'', map_const, prod_replicate] using prod_le_prod' h @[to_additive exists_lt_of_sum_lt] lemma exists_lt_of_prod_lt' [linear_order M] [covariant_class M M (function.swap (*)) (≀)] [covariant_class M M (*) (≀)] {l : list ΞΉ} (f g : ΞΉ β†’ M) (h : (l.map f).prod < (l.map g).prod) : βˆƒ i ∈ l, f i < g i := by { contrapose! h, exact prod_le_prod' h } @[to_additive exists_le_of_sum_le] lemma exists_le_of_prod_le' [linear_order M] [covariant_class M M (*) (<)] [covariant_class M M (*) (≀)] [covariant_class M M (function.swap (*)) (<)] [covariant_class M M (function.swap (*)) (≀)] {l : list ΞΉ} (hl : l β‰  []) (f g : ΞΉ β†’ M) (h : (l.map f).prod ≀ (l.map g).prod) : βˆƒ x ∈ l, f x ≀ g x := by { contrapose! h, exact prod_lt_prod_of_ne_nil hl _ _ h } @[to_additive sum_nonneg] lemma one_le_prod_of_one_le [preorder M] [covariant_class M M (*) (≀)] {l : list M} (hl₁ : βˆ€ x ∈ l, (1 : M) ≀ x) : 1 ≀ l.prod := begin -- We don't use `pow_card_le_prod` to avoid assumption -- [covariant_class M M (function.swap (*)) (≀)] induction l with hd tl ih, { refl }, rw prod_cons, exact one_le_mul (hl₁ hd (mem_cons_self hd tl)) (ih (Ξ» x h, hl₁ x (mem_cons_of_mem hd h))) end end monoid section monoid_with_zero variables [monoid_with_zero Mβ‚€] /-- If zero is an element of a list `L`, then `list.prod L = 0`. If the domain is a nontrivial monoid with zero with no divisors, then this implication becomes an `iff`, see `list.prod_eq_zero_iff`. -/ lemma prod_eq_zero {L : list Mβ‚€} (h : (0 : Mβ‚€) ∈ L) : L.prod = 0 := begin induction L with a L ihL, { exact absurd h (not_mem_nil _) }, { rw prod_cons, cases (mem_cons_iff _ _ _).1 h with ha hL, exacts [mul_eq_zero_of_left ha.symm _, mul_eq_zero_of_right _ (ihL hL)] } end /-- Product of elements of a list `L` equals zero if and only if `0 ∈ L`. See also `list.prod_eq_zero` for an implication that needs weaker typeclass assumptions. -/ @[simp] lemma prod_eq_zero_iff [nontrivial Mβ‚€] [no_zero_divisors Mβ‚€] {L : list Mβ‚€} : L.prod = 0 ↔ (0 : Mβ‚€) ∈ L := begin induction L with a L ihL, { simp }, { rw [prod_cons, mul_eq_zero, ihL, mem_cons_iff, eq_comm] } end lemma prod_ne_zero [nontrivial Mβ‚€] [no_zero_divisors Mβ‚€] {L : list Mβ‚€} (hL : (0 : Mβ‚€) βˆ‰ L) : L.prod β‰  0 := mt prod_eq_zero_iff.1 hL end monoid_with_zero section group variables [group G] /-- This is the `list.prod` version of `mul_inv_rev` -/ @[to_additive "This is the `list.sum` version of `add_neg_rev`"] lemma prod_inv_reverse : βˆ€ (L : list G), L.prod⁻¹ = (L.map (Ξ» x, x⁻¹)).reverse.prod | [] := by simp | (x :: xs) := by simp [prod_inv_reverse xs] /-- A non-commutative variant of `list.prod_reverse` -/ @[to_additive "A non-commutative variant of `list.sum_reverse`"] lemma prod_reverse_noncomm : βˆ€ (L : list G), L.reverse.prod = (L.map (Ξ» x, x⁻¹)).prod⁻¹ := by simp [prod_inv_reverse] /-- Counterpart to `list.prod_take_succ` when we have an inverse operation -/ @[simp, to_additive /-"Counterpart to `list.sum_take_succ` when we have an negation operation"-/] lemma prod_drop_succ : βˆ€ (L : list G) (i : β„•) (p), (L.drop (i + 1)).prod = (L.nth_le i p)⁻¹ * (L.drop i).prod | [] i p := false.elim (nat.not_lt_zero _ p) | (x :: xs) 0 p := by simp | (x :: xs) (i + 1) p := prod_drop_succ xs i _ end group section comm_group variables [comm_group G] /-- This is the `list.prod` version of `mul_inv` -/ @[to_additive "This is the `list.sum` version of `add_neg`"] lemma prod_inv : βˆ€ (L : list G), L.prod⁻¹ = (L.map (Ξ» x, x⁻¹)).prod | [] := by simp | (x :: xs) := by simp [mul_comm, prod_inv xs] /-- Alternative version of `list.prod_update_nth` when the list is over a group -/ @[to_additive /-"Alternative version of `list.sum_update_nth` when the list is over a group"-/] lemma prod_update_nth' (L : list G) (n : β„•) (a : G) : (L.update_nth n a).prod = L.prod * (if hn : n < L.length then (L.nth_le n hn)⁻¹ * a else 1) := begin refine (prod_update_nth L n a).trans _, split_ifs with hn hn, { rw [mul_comm _ a, mul_assoc a, prod_drop_succ L n hn, mul_comm _ (drop n L).prod, ← mul_assoc (take n L).prod, prod_take_mul_prod_drop, mul_comm a, mul_assoc] }, { simp only [take_all_of_le (le_of_not_lt hn), prod_nil, mul_one, drop_eq_nil_of_le ((le_of_not_lt hn).trans n.le_succ)] } end end comm_group @[to_additive] lemma eq_of_prod_take_eq [left_cancel_monoid M] {L L' : list M} (h : L.length = L'.length) (h' : βˆ€ i ≀ L.length, (L.take i).prod = (L'.take i).prod) : L = L' := begin apply ext_le h (Ξ» i h₁ hβ‚‚, _), have : (L.take (i + 1)).prod = (L'.take (i + 1)).prod := h' _ (nat.succ_le_of_lt h₁), rw [prod_take_succ L i h₁, prod_take_succ L' i hβ‚‚, h' i (le_of_lt h₁)] at this, convert mul_left_cancel this end @[to_additive] lemma monotone_prod_take [canonically_ordered_monoid M] (L : list M) : monotone (Ξ» i, (L.take i).prod) := begin apply monotone_nat_of_le_succ (Ξ» n, _), cases lt_or_le n L.length with h h, { rw prod_take_succ _ _ h, exact le_self_mul }, { simp [take_all_of_le h, take_all_of_le (le_trans h (nat.le_succ _))] } end @[to_additive sum_pos] lemma one_lt_prod_of_one_lt [ordered_comm_monoid M] : βˆ€ (l : list M) (hl : βˆ€ x ∈ l, (1 : M) < x) (hlβ‚‚ : l β‰  []), 1 < l.prod | [] _ h := (h rfl).elim | [b] h _ := by simpa using h | (a :: b :: l) hl₁ hlβ‚‚ := begin simp only [forall_eq_or_imp, list.mem_cons_iff _ a] at hl₁, rw list.prod_cons, apply one_lt_mul_of_lt_of_le' hl₁.1, apply le_of_lt ((b :: l).one_lt_prod_of_one_lt hl₁.2 (l.cons_ne_nil b)), end @[to_additive] lemma single_le_prod [ordered_comm_monoid M] {l : list M} (hl₁ : βˆ€ x ∈ l, (1 : M) ≀ x) : βˆ€ x ∈ l, x ≀ l.prod := begin induction l, { simp }, simp_rw [prod_cons, forall_mem_cons] at ⊒ hl₁, split, { exact le_mul_of_one_le_right' (one_le_prod_of_one_le hl₁.2) }, { exact Ξ» x H, le_mul_of_one_le_of_le hl₁.1 (l_ih hl₁.right x H) }, end @[to_additive all_zero_of_le_zero_le_of_sum_eq_zero] lemma all_one_of_le_one_le_of_prod_eq_one [ordered_comm_monoid M] {l : list M} (hl₁ : βˆ€ x ∈ l, (1 : M) ≀ x) (hlβ‚‚ : l.prod = 1) {x : M} (hx : x ∈ l) : x = 1 := le_antisymm (hlβ‚‚ β–Έ single_le_prod hl₁ _ hx) (hl₁ x hx) /-- Slightly more general version of `list.prod_eq_one_iff` for a non-ordered `monoid` -/ @[to_additive "Slightly more general version of `list.sum_eq_zero_iff` for a non-ordered `add_monoid`"] lemma prod_eq_one [monoid M] {l : list M} (hl : βˆ€ (x ∈ l), x = (1 : M)) : l.prod = 1 := begin induction l with i l hil, { refl }, rw [list.prod_cons, hil (Ξ» x hx, hl _ (mem_cons_of_mem i hx)), hl _ (mem_cons_self i l), one_mul] end @[to_additive] lemma exists_mem_ne_one_of_prod_ne_one [monoid M] {l : list M} (h : l.prod β‰  1) : βˆƒ (x ∈ l), x β‰  (1 : M) := by simpa only [not_forall] using mt prod_eq_one h -- TODO: develop theory of tropical rings lemma sum_le_foldr_max [add_monoid M] [add_monoid N] [linear_order N] (f : M β†’ N) (h0 : f 0 ≀ 0) (hadd : βˆ€ x y, f (x + y) ≀ max (f x) (f y)) (l : list M) : f l.sum ≀ (l.map f).foldr max 0 := begin induction l with hd tl IH, { simpa using h0 }, simp only [list.sum_cons, list.foldr_map, list.foldr] at IH ⊒, exact (hadd _ _).trans (max_le_max le_rfl IH) end @[simp, to_additive] lemma prod_erase [decidable_eq M] [comm_monoid M] {a} : βˆ€ {l : list M}, a ∈ l β†’ a * (l.erase a).prod = l.prod | (b :: l) h := begin obtain rfl | ⟨ne, h⟩ := decidable.list.eq_or_ne_mem_of_mem h, { simp only [list.erase, if_pos, prod_cons] }, { simp only [list.erase, if_neg (mt eq.symm ne), prod_cons, prod_erase h, mul_left_comm a b] } end @[simp, to_additive] lemma prod_map_erase [decidable_eq ΞΉ] [comm_monoid M] (f : ΞΉ β†’ M) {a} : βˆ€ {l : list ΞΉ}, a ∈ l β†’ f a * ((l.erase a).map f).prod = (l.map f).prod | (b :: l) h := begin obtain rfl | ⟨ne, h⟩ := decidable.list.eq_or_ne_mem_of_mem h, { simp only [map, erase_cons_head, prod_cons] }, { simp only [map, erase_cons_tail _ ne.symm, prod_cons, prod_map_erase h, mul_left_comm (f a) (f b)], } end lemma sum_const_nat (m n : β„•) : sum (replicate m n) = m * n := by rw [sum_replicate, smul_eq_mul] /-- The product of a list of positive natural numbers is positive, and likewise for any nontrivial ordered semiring. -/ lemma prod_pos [strict_ordered_semiring R] (l : list R) (h : βˆ€ a ∈ l, (0 : R) < a) : 0 < l.prod := begin induction l with a l ih, { simp }, { rw prod_cons, exact mul_pos (h _ $ mem_cons_self _ _) (ih $ Ξ» a ha, h a $ mem_cons_of_mem _ ha) } end /-- A variant of `list.prod_pos` for `canonically_ordered_comm_semiring`. -/ @[simp] lemma _root_.canonically_ordered_comm_semiring.list_prod_pos {Ξ± : Type*} [canonically_ordered_comm_semiring Ξ±] [nontrivial Ξ±] : Ξ  {l : list Ξ±}, 0 < l.prod ↔ (βˆ€ x ∈ l, (0 : Ξ±) < x) | [] := ⟨λ h x hx, hx.elim, Ξ» _, zero_lt_one⟩ | (x :: xs) := by simp_rw [prod_cons, mem_cons_iff, forall_eq_or_imp, canonically_ordered_comm_semiring.mul_pos, _root_.canonically_ordered_comm_semiring.list_prod_pos] /-! Several lemmas about sum/head/tail for `list β„•`. These are hard to generalize well, as they rely on the fact that `default β„• = 0`. If desired, we could add a class stating that `default = 0`. -/ /-- This relies on `default β„• = 0`. -/ lemma head_add_tail_sum (L : list β„•) : L.head + L.tail.sum = L.sum := by { cases L, { simp, refl }, { simp } } /-- This relies on `default β„• = 0`. -/ lemma head_le_sum (L : list β„•) : L.head ≀ L.sum := nat.le.intro (head_add_tail_sum L) /-- This relies on `default β„• = 0`. -/ lemma tail_sum (L : list β„•) : L.tail.sum = L.sum - L.head := by rw [← head_add_tail_sum L, add_comm, add_tsub_cancel_right] section alternating section variables [has_one Ξ±] [has_mul Ξ±] [has_inv Ξ±] @[simp, to_additive] lemma alternating_prod_nil : alternating_prod ([] : list Ξ±) = 1 := rfl @[simp, to_additive] lemma alternating_prod_singleton (a : Ξ±) : alternating_prod [a] = a := rfl @[to_additive] lemma alternating_prod_cons_cons' (a b : Ξ±) (l : list Ξ±) : alternating_prod (a :: b :: l) = a * b⁻¹ * alternating_prod l := rfl end @[to_additive] lemma alternating_prod_cons_cons [div_inv_monoid Ξ±] (a b : Ξ±) (l : list Ξ±) : alternating_prod (a :: b :: l) = a / b * alternating_prod l := by rw [div_eq_mul_inv, alternating_prod_cons_cons'] variables [comm_group Ξ±] @[to_additive] lemma alternating_prod_cons' : βˆ€ (a : Ξ±) (l : list Ξ±), alternating_prod (a :: l) = a * (alternating_prod l)⁻¹ | a [] := by rw [alternating_prod_nil, inv_one, mul_one, alternating_prod_singleton] | a (b :: l) := by rw [alternating_prod_cons_cons', alternating_prod_cons' b l, mul_inv, inv_inv, mul_assoc] @[simp, to_additive] lemma alternating_prod_cons (a : Ξ±) (l : list Ξ±) : alternating_prod (a :: l) = a / alternating_prod l := by rw [div_eq_mul_inv, alternating_prod_cons'] end alternating lemma sum_nat_mod (l : list β„•) (n : β„•) : l.sum % n = (l.map (% n)).sum % n := by induction l; simp [nat.add_mod, *] lemma prod_nat_mod (l : list β„•) (n : β„•) : l.prod % n = (l.map (% n)).prod % n := by induction l; simp [nat.mul_mod, *] lemma sum_int_mod (l : list β„€) (n : β„€) : l.sum % n = (l.map (% n)).sum % n := by induction l; simp [int.add_mod, *] lemma prod_int_mod (l : list β„€) (n : β„€) : l.prod % n = (l.map (% n)).prod % n := by induction l; simp [int.mul_mod, *] end list section monoid_hom variables [monoid M] [monoid N] @[to_additive] lemma map_list_prod {F : Type*} [monoid_hom_class F M N] (f : F) (l : list M) : f l.prod = (l.map f).prod := (l.prod_hom f).symm namespace monoid_hom /-- Deprecated, use `_root_.map_list_prod` instead. -/ @[to_additive "Deprecated, use `_root_.map_list_sum` instead."] protected lemma map_list_prod (f : M β†’* N) (l : list M) : f l.prod = (l.map f).prod := map_list_prod f l end monoid_hom end monoid_hom
18069cfcd13cc9588c38d825c335acad97008be8
aa101d73b1a3173c7ec56de02b96baa8ca64c42e
/src/my_exercises/00_first_proofs.lean
cbadbcc71d9aca4908a9a44df4ba2929b225eb1b
[ "Apache-2.0" ]
permissive
gihanmarasingha/tutorials
b554d4d53866c493c4341dc13e914b01444e95a6
56617114ef0f9f7b808476faffd11e22e4380918
refs/heads/master
1,671,141,758,153
1,599,173,318,000
1,599,173,318,000
282,405,870
0
0
Apache-2.0
1,595,666,751,000
1,595,666,750,000
null
UTF-8
Lean
false
false
18,087
lean
/- This file is intended for Lean beginners. The goal is to demonstrate what it feels like to prove things using Lean and mathlib. Complicated definitions and theory building are not covered. Everything is covered again more slowly and with exercises in the next files. -/ -- We want real numbers and their basic properties import data.real.basic -- We want to be able to use Lean's built-in "help" functionality import tactic.suggest -- We want to be able to define functions using the law of excluded middle noncomputable theory open_locale classical /- Our first goal is to define the set of upper bounds of a set of real numbers. This is already defined in mathlib (in a more general context), but we repeat it for the sake of exposition. Right-click "upper_bounds" below to get offered to jump to mathlib's version -/ #check upper_bounds /-- The set of upper bounds of a set of real numbers ℝ -/ def up_bounds (A : set ℝ) := { x : ℝ | βˆ€ a ∈ A, a ≀ x} /-- Predicate `is_max a A` means `a` is a maximum of `A` -/ def is_max (a : ℝ) (A : set ℝ) := a ∈ A ∧ a ∈ up_bounds A /- In the above definition, the symbol `∧` means "and". We also see the most visible difference between set theoretic foundations and type theoretic ones (used by almost all proof assistants). In set theory, everything is a set, and the only relation you get from foundations are `=` and `∈`. In type theory, there is a meta-theoretic relation of "typing": `a : ℝ` reads "`a` is a real number" or, more precisely, "the type of `a` is `ℝ`". Here "meta-theoretic" means this is not a statement you can prove or disprove inside the theory, it's a fact that is true or not. Here we impose this fact, in other circumstances, it would be checked by the Lean kernel. By contrast, `a ∈ A` is a statement inside the theory. Here it's part of the definition, in other circumstances it could be something proven inside Lean. -/ /- For illustrative purposes, we now define an infix version of the above predicate. It will allow us to write `a is_a_max_of A`, which is closer to a sentence. -/ infix ` is_a_max_of `:55 := is_max /- Let's prove something now! A set of real numbers has at most one maximum. Here everything left of the final `:` is introducing the objects and assumption. The equality `x = y` right of the colon is the conclusion. -/ lemma unique_max (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin -- We first break our assumptions in their two constituent pieces. -- We are free to choose the name following `with` cases hx with x_in x_up, cases hy with y_in y_up, -- Assumption `x_up` means x isn't less than elements of A, let's apply this to y specialize x_up y, -- Assumption `x_up` now needs the information that `y` is indeed in `A`. specialize x_up y_in, -- Let's do this quicker with roles swapped specialize y_up x x_in, -- We explained to Lean the idea of this proof. -- Now we know `x ≀ y` and `y ≀ x`, and Lean shouldn't need more help. -- `linarith` proves equalities and inequalities that follow linearly from -- the assumption we have. linarith, end /- The above proof is too long, even if you remove comments. We don't really need the unpacking steps at the beginning; we can access both parts of the assumption `hx : x is_a_max_of A` using shortcuts `h.1` and `h.2`. We can also improve readability without assistance from the tactic state display, clearly announcing intermediate goals using `have`. This way we get to the following version of the same proof. -/ example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin have : x ≀ y, from hy.2 x hx.1, have : y ≀ x, from hx.2 y hy.1, linarith, end /- Notice how mathematics based on type theory treats the assumption `βˆ€ a ∈ A, a ≀ y` as a function turning an element `a` of `A` into the statement `a ≀ y`. More precisely, this assumption is the abbreviation of `βˆ€ a : ℝ, a ∈ A β†’ a ≀ y`. The expression `hy.2 x` appearing in the above proof is then the statement `x ∈ A β†’ x ≀ y`, which itself is a function turning a statement `x ∈ A` into `x ≀ y` so that the full expression `hy.2 x hx.1` is indeed a proof of `x ≀ y`. One could argue a three-line-long proof of this lemma is still two lines too long. This is debatable, but mathlib's style is to write very short proofs for trivial lemmas. Those proofs are not easy to read but they are meant to indicate that the proof is probably not worth reading. In order to reach this stage, we need to know what `linarith` did for us. It invoked the lemma `le_antisymm` which says: `x ≀ y β†’ y ≀ x β†’ x = y`. This arrow, which is used both for function and implication, is right associative. So the statement is `x ≀ y β†’ (y ≀ x β†’ x = y)` which reads: I will send a proof `p` of `x ≀ y` to a function sending a proof `q'` of `y ≀ x` to a proof of `x = y`. Hence `le_antisymm p q'` is a proof of `x = y`. Using this we can get our one-line proof: -/ example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := le_antisymm (hy.2 x hx.1) (hx.2 y hy.1) /- Such a proof is called a proof term (or a "term mode" proof). Notice it has no `begin` and `end`. It is directly the kind of low level proof that the Lean kernel is consuming. Commands like `cases`, `specialize` or `linarith` are called tactics, they help users constructing proof terms that could be very tedious to write directly. The most efficient proof style combines tactics with proof terms like our previous `have : x ≀ y, from hy.2 x hx.1` where `hy.2 x hx.1` is a proof term embeded inside a tactic mode proof. In the remaining of this file, we'll be characterizing infima of sets of real numbers in term of sequences. -/ /-- The set of lower bounds of a set of real numbers ℝ -/ def low_bounds (A : set ℝ) := { x : ℝ | βˆ€ a ∈ A, x ≀ a} /- We now define `a` is an infimum of `A`. Again there is already a more general version in mathlib. -/ def is_inf (x : ℝ) (A : set ℝ) := x is_a_max_of (low_bounds A) infix ` is_an_inf_of `:55 := is_inf /- We need to prove that any number which is greater than the infimum of A is greater than some element of A. -/ lemma inf_lt {A : set ℝ} {x : ℝ} (hx : x is_an_inf_of A) : βˆ€ y, x < y β†’ βˆƒ a ∈ A, a < y := begin -- Let `y` be any real number. intro y, -- Let's prove the contrapositive contrapose, -- The symbol `Β¬` means negation. Let's ask Lean to rewrite the goal without negation, -- pushing negation through quantifiers and inequalities push_neg, -- Let's assume the premise, calling the assumption `h` intro h, -- `h` is exactly saying `y` is a lower bound of `A` so the second part of -- the infimum assumption `hx` applied to `y` and `h` is exactly what we want. exact hx.2 y h end /- In the above proof, the sequence `contrapose, push_neg` is so common that it can be abbreviated to `contrapose!`. With these commands, we enter the gray zone between proof checking and proof finding. Practical computer proof checking crucially needs the computer to handle tedious proof steps. In the next proof, we'll start using `linarith` a bit more seriously, going one step further into automation. Our next real goal is to prove inequalities for limits of sequences. We extract the following lemma: if `y ≀ x + Ξ΅` for all positive `Ξ΅` then `y ≀ x`. -/ lemma le_of_le_add_eps {x y : ℝ} : (βˆ€ Ξ΅ > 0, y ≀ x + Ξ΅) β†’ y ≀ x := begin -- Let's prove the contrapositive, asking Lean to push negations right away. contrapose!, -- Assume `h : x < y`. intro h, -- We need to find `Ξ΅` such that `Ξ΅` is positive and `x + Ξ΅ < y`. -- Let's use `(y-x)/2` use ((y-x)/2), -- we now have two properties to prove. Let's do both in turn, using `linarith` split, linarith, linarith, end /- Note how `linarith` was used for both sub-goals at the end of the above proof. We could have shortened that using the semi-colon combinator instead of comma, writing `split ; linarith`. Next we will study a compressed version of that proof: -/ example {x y : ℝ} : (βˆ€ Ξ΅ > 0, y ≀ x + Ξ΅) β†’ y ≀ x := begin contrapose!, exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩, end /- The angle brackets `⟨` and `⟩` introduce compound data or proofs. A proof of a `βˆƒ z, P z` statemement is composed of a witness `zβ‚€` and a proof `h` of `P zβ‚€`. The compound is denoted by `⟨zβ‚€, h⟩`. In the example above, the predicate is itself compound, it is a conjunction `P z ∧ Q z`. So the proof term should read `⟨zβ‚€, ⟨h₁, hβ‚‚βŸ©βŸ©` where `h₁` (resp. `hβ‚‚`) is a proof of `P zβ‚€` (resp. `Q zβ‚€`). But these so-called "anonymous constructor" brackets are right-associative, so we can get rid of the nested brackets. The keyword `by` introduces tactic mode inside term mode, it is a shorter version of the `begin`/`end` pair, which is more convenient for single tactic blocks. In this example, `begin` enters tactic mode, `exact` leaves it, `by` re-enters it. Going all the way to a proof term would make the proof much longer, because we crucially use automation with `contrapose!` and `linarith`. We can still get a one-line proof using curly braces to gather several tactic invocations, and the `by` abbreviation instead of `begin`/`end`: -/ example {x y : ℝ} : (βˆ€ Ξ΅ > 0, y ≀ x + Ξ΅) β†’ y ≀ x := by { contrapose!, exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩ } /- One could argue that the above proof is a bit too terse, and we are relying too much on linarith. Let's have more `linarith` calls for smaller steps. For the sake of (tiny) variation, we will also assume the premise and argue by contradiction instead of contraposing. -/ example {x y : ℝ} : (βˆ€ Ξ΅ > 0, y ≀ x + Ξ΅) β†’ y ≀ x := begin intro h, -- Assume the conclusion is false, and call this assumption H. by_contradiction H, push_neg at H, -- Now let's compute. have key := calc -- Each line must end with a colon followed by a proof term -- We want to specialize our assumption `h` to `Ξ΅ = (y-x)/2` but this is long to -- type, so let's put a hole `_` that Lean will fill in by comparing the -- statement we want to prove and our proof term with a hole. As usual, -- positivity of `(y-x)/2` is proved by `linarith` y ≀ x + (y-x)/2 : h _ (by linarith) ... = x/2 + y/2 : by ring ... < y : by linarith, -- our key now says `y < y` (notice how the sequence `≀`, `=`, `<` was correctly -- merged into a `<`). Let `linarith` find the desired contradiction now. linarith, -- alternatively, we could have provided the proof term -- `exact lt_irrefl y key` end /- Now we are ready for some analysis. Let's set up notation for absolute value -/ local notation `|`x`|` := abs x /- And let's define convergence of sequences of real numbers (of course there is a much more general definition in mathlib). -/ /-- The sequence `u` tends to `l` -/ def limit (u : β„• β†’ ℝ) (l : ℝ) := βˆ€ Ξ΅ > 0, βˆƒ N, βˆ€ n β‰₯ N, |u n - l| ≀ Ξ΅ /- In the above definition, `u n` denotes the n-th term of the sequence. We can add parentheses to get `u(n)` but we try to avoid parentheses because they pile up very quickly -/ -- If y ≀ u n for all n and u n goes to x then y ≀ x lemma le_lim {x y : ℝ} {u : β„• β†’ ℝ} (hu : limit u x) (ineq : βˆ€ n, y ≀ u n) : y ≀ x := begin -- Let's apply our previous lemma apply le_of_le_add_eps, -- We need to prove y ≀ x + Ξ΅ for all positive Ξ΅. -- Let Ξ΅ be any positive real intros Ξ΅ Ξ΅_pos, -- we now specialize our limit assumption to this `Ξ΅`, and immediately -- fix a `N` as promised by the definition. cases hu Ξ΅ Ξ΅_pos with N HN, -- Now we only need to compute until reaching the conclusion calc y ≀ u N : ineq N ... = x + (u N - x) : by linarith -- We'll need `add_le_add` which says `a ≀ b` and `c ≀ d` implies `a + c ≀ b + d` -- We need a lemma saying `z ≀ |z|`. Because we don't know the name of this lemma, -- let's use `library_search`. Because searching thourgh the library is slow, -- Lean will write what it found in the Lean message window when cursor is on -- that line, so that we can replace it by the lemma. We see `le_max_left` which -- says `a ≀ max a b`. Actually there is a more specific lemma `le_abs_self` ... ≀ x + |u N - x| : add_le_add (by linarith) (by library_search) ... ≀ x + Ξ΅ : add_le_add (by linarith) (HN N (by linarith)), end /- The next lemma has been extracted from the main proof in order to discuss numbers. In ordinary maths, we know that β„• is *not* contained in `ℝ`, whatever the construction of real numbers that we use. For instance a natural number is not an equivalence class of Cauchy sequences. But it's very easy to pretend otherwise. Formal maths requires slightly more care. In the statement below, the "type ascription" `(n + 1 : ℝ)` forces Lean to convert the natural number `n+1` into a real number. The "inclusion" map will be displayed in tactic state as `↑`. There are various lemmas asserting this map is compatible with addition and monotone, but we don't want to bother writing their names. The `norm_cast` tactic is designed to wisely apply those lemmas for us. -/ lemma inv_succ_pos : βˆ€ n : β„•, 1/(n+1 : ℝ) > 0 := begin -- Let `n` be any integer intro n, -- Since we don't know the name of the relevant lemma, asserting that the inverse of -- a positive number is positive, let's state that is suffices -- to prove that `n+1`, seen as a real number, is positive, and ask `library_search` suffices : (n + 1 : ℝ) > 0, { library_search }, -- Now we want to reduce to a statement about natural numbers, not real numbers -- coming from natural numbers. norm_cast, -- and then get the usual help from `linarith` linarith, end /- That was a pretty long proof for an obvious fact. And stating it as a lemma feels stupid, so let's find a way to write it on one line in case we want to include it in some other proof without stating a lemma. First the `library_search` call above displays the name of the relevant lemma: `one_div_pos_of_pos`. We can also replace the `linarith` call on the last line by `library_search` to learn the name of the lemma `nat.succ_pos` asserting that the successor of a natural number is positive. There is also a variant on `norm_cast` that combines it with `exact`. The term mode analogue of `intro` is `Ξ»`. We get down to: -/ example : βˆ€ n : β„•, 1/(n+1 : ℝ) > 0 := Ξ» n, one_div_pos_of_pos (by exact_mod_cast nat.succ_pos n) /- The next proof uses mostly known things, so we will commment only new aspects. -/ lemma limit_inv_succ : βˆ€ Ξ΅ > 0, βˆƒ N : β„•, βˆ€ n β‰₯ N, 1/(n + 1 : ℝ) ≀ Ξ΅ := begin intros Ξ΅ Ξ΅_pos, suffices : βˆƒ N : β„•, 1/Ξ΅ ≀ N, { -- Because we didn't provide a name for the above statement, Lean called it `this`. -- Let's fix an `N` that works. cases this with N HN, use N, intros n Hn, -- Now we want to rewrite the goal using lemmas -- `div_le_iff' : 0 < b β†’ (a / b ≀ c ↔ a ≀ b * c)` -- `div_le_iff : 0 < b β†’ (a / b ≀ c ↔ a ≀ c * b)` -- the second one will be rewritten from right to left, as indicated by `←`. -- Lean will create a side goal for the required positivity assumption that -- we don't provide for `div_le_iff'`. rw [div_le_iff', ← div_le_iff Ξ΅_pos], -- We want to replace assumption `Hn` by its real counter-part so that -- linarith can find what it needs. replace Hn : (N : ℝ) ≀ n, exact_mod_cast Hn, linarith, -- we are still left with the positivity assumption, but already discussed -- how to prove it in the preceding lemma exact_mod_cast nat.succ_pos n }, -- Now we need to prove that sufficient statement. -- We want to use that `ℝ` is archimedean. So we start typing -- `exact archimedean_` and hit Ctrl-space to see what completion Lean proposes -- the lemma `archimedean_iff_nat_le` sounds promising. We select the left to -- right implication using `.1`. This a generic lemma for fields equiped with -- a linear (ie total) order. We need to provide a proof that `ℝ` is indeed -- archimedean. This is done using the `apply_instance` tactic that will be -- covered elsewhere. exact archimedean_iff_nat_le.1 (by apply_instance) (1/Ξ΅), end /- We can now put all pieces together, with almost no new things to explain. -/ lemma inf_seq (A : set ℝ) (x : ℝ) : (x is_an_inf_of A) ↔ (x ∈ low_bounds A ∧ βˆƒ u : β„• β†’ ℝ, limit u x ∧ βˆ€ n, u n ∈ A ) := begin split, { intro h, split, { exact h.1 }, -- On the next line, we don't need to tell Lean to treat `n+1` as a real number because -- we add `x` to it, so Lean knows there is only one way to make sense of this expression. have key : βˆ€ n : β„•, βˆƒ a ∈ A, a < x + 1/(n+1), { intro n, -- we can use the lemma we proved above apply inf_lt h, -- and another one we proved! have : 0 < 1/(n+1 : ℝ), from inv_succ_pos n, linarith }, -- Now we need to use axiom of (countable) choice choose u hu using key, use u, split, { intros Ξ΅ Ξ΅_pos, -- again we use a lemma we proved, specializing it to our fixed `Ξ΅`, and fixing a `N` cases limit_inv_succ Ξ΅ Ξ΅_pos with N H, use N, intros n hn, have : x ≀ u n, from h.1 _ (hu n).1, have := calc u n < x + 1/(n + 1) : (hu n).2 ... ≀ x + Ξ΅ : add_le_add (le_refl x) (H n hn), rw abs_of_nonneg ; linarith }, { intro n, exact (hu n).1 } }, { intro h, -- Assumption `h` is made of nested compound statements. We can use the -- recursive version of `cases` to unpack it in one go. rcases h with ⟨x_min, u, lim, huA⟩, split, exact x_min, intros y y_mino, apply le_lim lim, intro n, exact y_mino (u n) (huA n) }, end
101bb9c5f19d2f5b030d5bb8e083275663699e84
0b3933727d99a2f351f5dbe6e24716bb786a6649
/src/qtt/matrix.lean
be38bb091d4fc569f7e9f3b72085e216f2ecba6f
[]
no_license
Vtec234/lean-sesh
1e770858215279f65aba92b4782b483ab4f09353
d11d7bb0599406e27d3a4d26242aec13d639ecf7
refs/heads/master
1,587,497,515,696
1,558,362,223,000
1,558,362,223,000
169,809,439
2
0
null
null
null
null
UTF-8
Lean
false
false
3,148
lean
/- Introduces matrices and horrifying linear algebra. -/ import qtt.context open context open debrujin_idx /- This matrix defines the context in which every variable (given by a debrujin idx) exists. In other words, it defines a substitution. Variables all exist in the same precontext, but the type multiplicities might differ. -/ def matrix (Ο„ mult: Type) (Ξ³ Ξ΄: precontext Ο„) : Type := Ξ  (T: Ο„), Ξ³ βˆ‹ T β†’ @context Ο„ mult Ξ΄ namespace matrix variable {Ο„: Type} variables {mult: Type} [semiring mult] def identity: Ξ  Ξ³, @matrix Ο„ mult Ξ³ Ξ³ | (T::Ξ΄) _ ZVar := ⟦1⬝T⟧::0 | (T::Ξ΄) U (SVar x) := ⟦0⬝T⟧::(identity Ξ΄ U x) @[unfold_] lemma identity_zvar {T: Ο„} {Ξ³: precontext Ο„} : identity (T::Ξ³) T ZVar = ⟦(1: mult)⬝T⟧::(0: context Ξ³) := by refl @[unfold_] lemma identity_svar {T U: Ο„} {Ξ΄: precontext Ο„} {x} : identity (T::Ξ΄) U (SVar x) = ⟦(0: mult)⬝T⟧::(identity Ξ΄ U x) := by refl def vmul: βˆ€ {Ξ³ Ξ΄}, @context Ο„ mult Ξ³ β†’ @matrix Ο„ mult Ξ³ Ξ΄ β†’ @context Ο„ mult Ξ΄ | _ Ξ΄ context.nil _ := 0 | _ _ (βŸ¦Ο€β¬T⟧::Ξ“) Ξ := (Ο€ β€’ Ξ T ZVar) + (vmul Ξ“ (Ξ» U x, Ξ U (SVar x))) infix ` βŠ› `:70 := vmul namespace vmul @[simp] lemma vmul_nil {Ξ΄} {Ξ: @matrix Ο„ mult [] Ξ΄} : context.nil βŠ› Ξ = 0 := by refl @[unfold_] lemma vmul_cons {Ξ³ Ξ΄} {T: Ο„} {Ξ“: context Ξ³} {Ο€: mult} {Ξ: @matrix Ο„ mult (T::Ξ³) Ξ΄} : βŸ¦Ο€β¬T⟧::Ξ“ βŠ› Ξ = (Ο€ β€’ Ξ T ZVar) + (Ξ“ βŠ› (Ξ» U x, Ξ U (SVar x))) := by refl @[simp] lemma zero_vmul : βˆ€ {Ξ³ Ξ΄: precontext Ο„} (M: @matrix Ο„ mult Ξ³ Ξ΄), 0 βŠ› M = 0 := begin intros, induction Ξ³ with T Ξ³ ih, { refl }, { unfold has_zero.zero zeros, simp * with unfold_, show (0: mult) β€’ M T ZVar + 0 βŠ› (Ξ» (U: Ο„) (x: Ξ³ βˆ‹ U), M U (SVar x)) = 0, simp * }, end @[simp] lemma vmul_ext_zero : βˆ€ {Ξ³ Ξ΄} {Ξ“: context Ξ³} {Ξ: @matrix Ο„ mult Ξ³ Ξ΄} {T: Ο„}, Ξ“ βŠ› (Ξ» U x, ⟦0⬝T⟧::(Ξ U x)) = ⟦0⬝T⟧::(Ξ“ βŠ› Ξ) := begin intros, induction Ξ“ with Ξ³' Ο€ T' Ξ“ ih, { refl }, { simp * with unfold_ }, end @[simp] lemma one_vmul : βˆ€ {Ξ³ Ξ΄} {Ξ: @matrix Ο„ mult Ξ³ Ξ΄} {T: Ο„} (x: Ξ³ βˆ‹ T), ------------------------------- (identity Ξ³ T x) βŠ› Ξ = Ξ T x := begin intros, induction x with Ξ³' T' Ξ΄' T'' U x' ih; { simp * with unfold_ }, end @[simp] lemma vmul_one : βˆ€ {Ξ³} (Ξ“: @context Ο„ mult Ξ³), Ξ“ βŠ› (identity Ξ³) = Ξ“ := begin intros, induction Ξ“ with Ξ³ Ο€ T Ξ“ ih, { refl }, { simp [*, vmul_ext_zero] with unfold_ }, end lemma smul_vmul : βˆ€ {Ξ³ Ξ΄} {Ξ“: @context Ο„ mult Ξ³} {Ξ: @matrix Ο„ mult Ξ³ Ξ΄} {Ο€: mult}, (Ο€ β€’ Ξ“) βŠ› Ξ = Ο€ β€’ (Ξ“ βŠ› Ξ) := begin intros, induction Ξ“ with Ξ³' Ο€' T Ξ“ ih; { simp [*, context.mul_smul] with unfold_ sop_form }, end @[sop_form] lemma vmul_right_distrib : βˆ€ {Ξ³ Ξ΄} {Γ₁ Ξ“β‚‚: @context Ο„ mult Ξ³} {Ξ: @matrix Ο„ mult Ξ³ Ξ΄}, (Γ₁ + Ξ“β‚‚) βŠ› Ξ = (Γ₁ βŠ› Ξ) + (Ξ“β‚‚ βŠ› Ξ) := begin intros, induction Γ₁ with γ₁ π₁ T₁ Γ₁ ih₁; { cases Ξ“β‚‚, simp * with unfold_ sop_form }, end end vmul end matrix
7529dce934f4ef2ce705d75d77760b3087a93ff8
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/data/nat/prime.lean
a4fafc88cc00af4ec8058cc2cfb2df132abe94b4
[ "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
38,061
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, Mario Carneiro -/ import algebra.group_power import data.list.sort import data.nat.gcd import data.nat.sqrt import tactic.norm_num import tactic.wlog /-! # Prime numbers This file deals with prime numbers: natural numbers `p β‰₯ 2` whose only divisors are `p` and `1`. ## Important declarations All the following declarations exist in the namespace `nat`. - `prime`: the predicate that expresses that a natural number `p` is prime - `primes`: the subtype of natural numbers that are prime - `min_fac n`: the minimal prime factor of a natural number `n β‰  1` - `exists_infinite_primes`: Euclid's theorem that there exist infinitely many prime numbers - `factors n`: the prime factorization of `n` - `factors_unique`: uniqueness of the prime factorisation -/ open bool subtype open_locale nat namespace nat /-- `prime p` means that `p` is a prime number, that is, a natural number at least 2 whose only divisors are `p` and `1`. -/ @[pp_nodot] def prime (p : β„•) := 2 ≀ p ∧ βˆ€ m ∣ p, m = 1 ∨ m = p theorem prime.two_le {p : β„•} : prime p β†’ 2 ≀ p := and.left theorem prime.one_lt {p : β„•} : prime p β†’ 1 < p := prime.two_le instance prime.one_lt' (p : β„•) [hp : _root_.fact p.prime] : _root_.fact (1 < p) := ⟨hp.1.one_lt⟩ lemma prime.ne_one {p : β„•} (hp : p.prime) : p β‰  1 := ne.symm $ ne_of_lt hp.one_lt theorem prime_def_lt {p : β„•} : prime p ↔ 2 ≀ p ∧ βˆ€ m < p, m ∣ p β†’ m = 1 := and_congr_right $ Ξ» p2, forall_congr $ Ξ» m, ⟨λ h l d, (h d).resolve_right (ne_of_lt l), Ξ» h d, (le_of_dvd (le_of_succ_le p2) d).lt_or_eq_dec.imp_left (Ξ» l, h l d)⟩ theorem prime_def_lt' {p : β„•} : prime p ↔ 2 ≀ p ∧ βˆ€ m, 2 ≀ m β†’ m < p β†’ Β¬ m ∣ p := prime_def_lt.trans $ and_congr_right $ Ξ» p2, forall_congr $ Ξ» m, ⟨λ h m2 l d, not_lt_of_ge m2 ((h l d).symm β–Έ dec_trivial), Ξ» h l d, begin rcases m with _|_|m, { rw eq_zero_of_zero_dvd d at p2, revert p2, exact dec_trivial }, { refl }, { exact (h dec_trivial l).elim d } end⟩ theorem prime_def_le_sqrt {p : β„•} : prime p ↔ 2 ≀ p ∧ βˆ€ m, 2 ≀ m β†’ m ≀ sqrt p β†’ Β¬ m ∣ p := prime_def_lt'.trans $ and_congr_right $ Ξ» p2, ⟨λ a m m2 l, a m m2 $ lt_of_le_of_lt l $ sqrt_lt_self p2, Ξ» a, have βˆ€ {m k}, m ≀ k β†’ 1 < m β†’ p β‰  m * k, from Ξ» m k mk m1 e, a m m1 (le_sqrt.2 (e.symm β–Έ nat.mul_le_mul_left m mk)) ⟨k, e⟩, Ξ» m m2 l ⟨k, e⟩, begin cases (le_total m k) with mk km, { exact this mk m2 e }, { rw [mul_comm] at e, refine this km (lt_of_mul_lt_mul_right _ (zero_le m)) e, rwa [one_mul, ← e] } end⟩ theorem prime_of_coprime (n : β„•) (h1 : 1 < n) (h : βˆ€ m < n, m β‰  0 β†’ n.coprime m) : prime n := begin refine prime_def_lt.mpr ⟨h1, Ξ» m mlt mdvd, _⟩, have hm : m β‰  0, { rintro rfl, rw zero_dvd_iff at mdvd, exact mlt.ne' mdvd }, exact (h m mlt hm).symm.eq_one_of_dvd mdvd, end section /-- This instance is slower than the instance `decidable_prime` defined below, but has the advantage that it works in the kernel for small values. If you need to prove that a particular number is prime, in any case you should not use `dec_trivial`, but rather `by norm_num`, which is much faster. -/ local attribute [instance] def decidable_prime_1 (p : β„•) : decidable (prime p) := decidable_of_iff' _ prime_def_lt' lemma prime.ne_zero {n : β„•} (h : prime n) : n β‰  0 := by { rintro rfl, revert h, dec_trivial } theorem prime.pos {p : β„•} (pp : prime p) : 0 < p := lt_of_succ_lt pp.one_lt theorem not_prime_zero : Β¬ prime 0 := by simp [prime] theorem not_prime_one : Β¬ prime 1 := by simp [prime] theorem prime_two : prime 2 := dec_trivial end theorem prime.pred_pos {p : β„•} (pp : prime p) : 0 < pred p := lt_pred_iff.2 pp.one_lt theorem succ_pred_prime {p : β„•} (pp : prime p) : succ (pred p) = p := succ_pred_eq_of_pos pp.pos theorem dvd_prime {p m : β„•} (pp : prime p) : m ∣ p ↔ m = 1 ∨ m = p := ⟨λ d, pp.2 m d, Ξ» h, h.elim (Ξ» e, e.symm β–Έ one_dvd _) (Ξ» e, e.symm β–Έ dvd_rfl)⟩ theorem dvd_prime_two_le {p m : β„•} (pp : prime p) (H : 2 ≀ m) : m ∣ p ↔ m = p := (dvd_prime pp).trans $ or_iff_right_of_imp $ not.elim $ ne_of_gt H theorem prime_dvd_prime_iff_eq {p q : β„•} (pp : p.prime) (qp : q.prime) : p ∣ q ↔ p = q := dvd_prime_two_le qp (prime.two_le pp) theorem prime.not_dvd_one {p : β„•} (pp : prime p) : Β¬ p ∣ 1 | d := (not_le_of_gt pp.one_lt) $ le_of_dvd dec_trivial d theorem not_prime_mul {a b : β„•} (a1 : 1 < a) (b1 : 1 < b) : Β¬ prime (a * b) := Ξ» h, ne_of_lt (nat.mul_lt_mul_of_pos_left b1 (lt_of_succ_lt a1)) $ by simpa using (dvd_prime_two_le h a1).1 (dvd_mul_right _ _) lemma not_prime_mul' {a b n : β„•} (h : a * b = n) (h₁ : 1 < a) (hβ‚‚ : 1 < b) : Β¬ prime n := by { rw ← h, exact not_prime_mul h₁ hβ‚‚ } section min_fac private lemma min_fac_lemma (n k : β„•) (h : Β¬ n < k * k) : sqrt n - k < sqrt n + 2 - k := (nat.sub_lt_sub_right_iff $ le_sqrt.2 $ le_of_not_gt h).2 $ nat.lt_add_of_pos_right dec_trivial /-- If `n < k * k`, then `min_fac_aux n k = n`, if `k | n`, then `min_fac_aux n k = k`. Otherwise, `min_fac_aux n k = min_fac_aux n (k+2)` using well-founded recursion. If `n` is odd and `1 < n`, then then `min_fac_aux n 3` is the smallest prime factor of `n`. -/ def min_fac_aux (n : β„•) : β„• β†’ β„• | k := if h : n < k * k then n else if k ∣ n then k else have _, from min_fac_lemma n k h, min_fac_aux (k + 2) using_well_founded {rel_tac := Ξ» _ _, `[exact ⟨_, measure_wf (Ξ» k, sqrt n + 2 - k)⟩]} /-- Returns the smallest prime factor of `n β‰  1`. -/ def min_fac : β„• β†’ β„• | 0 := 2 | 1 := 1 | (n+2) := if 2 ∣ n then 2 else min_fac_aux (n + 2) 3 @[simp] theorem min_fac_zero : min_fac 0 = 2 := rfl @[simp] theorem min_fac_one : min_fac 1 = 1 := rfl theorem min_fac_eq : βˆ€ n, min_fac n = if 2 ∣ n then 2 else min_fac_aux n 3 | 0 := by simp | 1 := by simp [show 2β‰ 1, from dec_trivial]; rw min_fac_aux; refl | (n+2) := have 2 ∣ n + 2 ↔ 2 ∣ n, from (nat.dvd_add_iff_left (by refl)).symm, by simp [min_fac, this]; congr private def min_fac_prop (n k : β„•) := 2 ≀ k ∧ k ∣ n ∧ βˆ€ m, 2 ≀ m β†’ m ∣ n β†’ k ≀ m theorem min_fac_aux_has_prop {n : β„•} (n2 : 2 ≀ n) (nd2 : Β¬ 2 ∣ n) : βˆ€ k i, k = 2*i+3 β†’ (βˆ€ m, 2 ≀ m β†’ m ∣ n β†’ k ≀ m) β†’ min_fac_prop n (min_fac_aux n k) | k := Ξ» i e a, begin rw min_fac_aux, by_cases h : n < k*k; simp [h], { have pp : prime n := prime_def_le_sqrt.2 ⟨n2, Ξ» m m2 l d, not_lt_of_ge l $ lt_of_lt_of_le (sqrt_lt.2 h) (a m m2 d)⟩, from ⟨n2, dvd_rfl, Ξ» m m2 d, le_of_eq ((dvd_prime_two_le pp m2).1 d).symm⟩ }, have k2 : 2 ≀ k, { subst e, exact dec_trivial }, by_cases dk : k ∣ n; simp [dk], { exact ⟨k2, dk, a⟩ }, { refine have _, from min_fac_lemma n k h, min_fac_aux_has_prop (k+2) (i+1) (by simp [e, left_distrib]) (Ξ» m m2 d, _), cases nat.eq_or_lt_of_le (a m m2 d) with me ml, { subst me, contradiction }, apply (nat.eq_or_lt_of_le ml).resolve_left, intro me, rw [← me, e] at d, change 2 * (i + 2) ∣ n at d, have := dvd_of_mul_right_dvd d, contradiction } end using_well_founded {rel_tac := Ξ» _ _, `[exact ⟨_, measure_wf (Ξ» k, sqrt n + 2 - k)⟩]} theorem min_fac_has_prop {n : β„•} (n1 : n β‰  1) : min_fac_prop n (min_fac n) := begin by_cases n0 : n = 0, {simp [n0, min_fac_prop, ge]}, have n2 : 2 ≀ n, { revert n0 n1, rcases n with _|_|_; exact dec_trivial }, simp [min_fac_eq], by_cases d2 : 2 ∣ n; simp [d2], { exact ⟨le_refl _, d2, Ξ» k k2 d, k2⟩ }, { refine min_fac_aux_has_prop n2 d2 3 0 rfl (Ξ» m m2 d, (nat.eq_or_lt_of_le m2).resolve_left (mt _ d2)), exact Ξ» e, e.symm β–Έ d } end theorem min_fac_dvd (n : β„•) : min_fac n ∣ n := if n1 : n = 1 then by simp [n1] else (min_fac_has_prop n1).2.1 theorem min_fac_prime {n : β„•} (n1 : n β‰  1) : prime (min_fac n) := let ⟨f2, fd, a⟩ := min_fac_has_prop n1 in prime_def_lt'.2 ⟨f2, Ξ» m m2 l d, not_le_of_gt l (a m m2 (d.trans fd))⟩ theorem min_fac_le_of_dvd {n : β„•} : βˆ€ {m : β„•}, 2 ≀ m β†’ m ∣ n β†’ min_fac n ≀ m := by by_cases n1 : n = 1; [exact Ξ» m m2 d, n1.symm β–Έ le_trans dec_trivial m2, exact (min_fac_has_prop n1).2.2] theorem min_fac_pos (n : β„•) : 0 < min_fac n := by by_cases n1 : n = 1; [exact n1.symm β–Έ dec_trivial, exact (min_fac_prime n1).pos] theorem min_fac_le {n : β„•} (H : 0 < n) : min_fac n ≀ n := le_of_dvd H (min_fac_dvd n) theorem le_min_fac {m n : β„•} : n = 1 ∨ m ≀ min_fac n ↔ βˆ€ p, prime p β†’ p ∣ n β†’ m ≀ p := ⟨λ h p pp d, h.elim (by rintro rfl; cases pp.not_dvd_one d) (Ξ» h, le_trans h $ min_fac_le_of_dvd pp.two_le d), Ξ» H, or_iff_not_imp_left.2 $ Ξ» n1, H _ (min_fac_prime n1) (min_fac_dvd _)⟩ theorem le_min_fac' {m n : β„•} : n = 1 ∨ m ≀ min_fac n ↔ βˆ€ p, 2 ≀ p β†’ p ∣ n β†’ m ≀ p := ⟨λ h p (pp:1<p) d, h.elim (by rintro rfl; cases not_le_of_lt pp (le_of_dvd dec_trivial d)) (Ξ» h, le_trans h $ min_fac_le_of_dvd pp d), Ξ» H, le_min_fac.2 (Ξ» p pp d, H p pp.two_le d)⟩ theorem prime_def_min_fac {p : β„•} : prime p ↔ 2 ≀ p ∧ min_fac p = p := ⟨λ pp, ⟨pp.two_le, let ⟨f2, fd, a⟩ := min_fac_has_prop $ ne_of_gt pp.one_lt in ((dvd_prime pp).1 fd).resolve_left (ne_of_gt f2)⟩, Ξ» ⟨p2, e⟩, e β–Έ min_fac_prime (ne_of_gt p2)⟩ /-- This instance is faster in the virtual machine than `decidable_prime_1`, but slower in the kernel. If you need to prove that a particular number is prime, in any case you should not use `dec_trivial`, but rather `by norm_num`, which is much faster. -/ instance decidable_prime (p : β„•) : decidable (prime p) := decidable_of_iff' _ prime_def_min_fac theorem not_prime_iff_min_fac_lt {n : β„•} (n2 : 2 ≀ n) : Β¬ prime n ↔ min_fac n < n := (not_congr $ prime_def_min_fac.trans $ and_iff_right n2).trans $ (lt_iff_le_and_ne.trans $ and_iff_right $ min_fac_le $ le_of_succ_le n2).symm lemma min_fac_le_div {n : β„•} (pos : 0 < n) (np : Β¬ prime n) : min_fac n ≀ n / min_fac n := match min_fac_dvd n with | ⟨0, h0⟩ := absurd pos $ by rw [h0, mul_zero]; exact dec_trivial | ⟨1, h1⟩ := begin rw mul_one at h1, rw [prime_def_min_fac, not_and_distrib, ← h1, eq_self_iff_true, not_true, or_false, not_le] at np, rw [le_antisymm (le_of_lt_succ np) (succ_le_of_lt pos), min_fac_one, nat.div_one] end | ⟨(x+2), hx⟩ := begin conv_rhs { congr, rw hx }, rw [nat.mul_div_cancel_left _ (min_fac_pos _)], exact min_fac_le_of_dvd dec_trivial ⟨min_fac n, by rwa mul_comm⟩ end end /-- The square of the smallest prime factor of a composite number `n` is at most `n`. -/ lemma min_fac_sq_le_self {n : β„•} (w : 0 < n) (h : Β¬ prime n) : (min_fac n)^2 ≀ n := have t : (min_fac n) ≀ (n/min_fac n) := min_fac_le_div w h, calc (min_fac n)^2 = (min_fac n) * (min_fac n) : sq (min_fac n) ... ≀ (n/min_fac n) * (min_fac n) : nat.mul_le_mul_right (min_fac n) t ... ≀ n : div_mul_le_self n (min_fac n) @[simp] lemma min_fac_eq_one_iff {n : β„•} : min_fac n = 1 ↔ n = 1 := begin split, { intro h, by_contradiction hn, have := min_fac_prime hn, rw h at this, exact not_prime_one this, }, { rintro rfl, refl, } end @[simp] lemma min_fac_eq_two_iff (n : β„•) : min_fac n = 2 ↔ 2 ∣ n := begin split, { intro h, convert min_fac_dvd _, rw h, }, { intro h, have ub := min_fac_le_of_dvd (le_refl 2) h, have lb := min_fac_pos n, -- If `interval_cases` and `norm_num` were already available here, -- this would be easy and pleasant. -- But they aren't, so it isn't. cases h : n.min_fac with m, { rw h at lb, cases lb, }, { cases m with m, { simp at h, subst h, cases h with n h, cases n; cases h, }, { cases m with m, { refl, }, { rw h at ub, cases ub with _ ub, cases ub with _ ub, cases ub, } } } } end end min_fac theorem exists_dvd_of_not_prime {n : β„•} (n2 : 2 ≀ n) (np : Β¬ prime n) : βˆƒ m, m ∣ n ∧ m β‰  1 ∧ m β‰  n := ⟨min_fac n, min_fac_dvd _, ne_of_gt (min_fac_prime (ne_of_gt n2)).one_lt, ne_of_lt $ (not_prime_iff_min_fac_lt n2).1 np⟩ theorem exists_dvd_of_not_prime2 {n : β„•} (n2 : 2 ≀ n) (np : Β¬ prime n) : βˆƒ m, m ∣ n ∧ 2 ≀ m ∧ m < n := ⟨min_fac n, min_fac_dvd _, (min_fac_prime (ne_of_gt n2)).two_le, (not_prime_iff_min_fac_lt n2).1 np⟩ theorem exists_prime_and_dvd {n : β„•} (n2 : 2 ≀ n) : βˆƒ p, prime p ∧ p ∣ n := ⟨min_fac n, min_fac_prime (ne_of_gt n2), min_fac_dvd _⟩ /-- Euclid's theorem on the **infinitude of primes**. Here given in the form: for every `n`, there exists a prime number `p β‰₯ n`. -/ theorem exists_infinite_primes (n : β„•) : βˆƒ p, n ≀ p ∧ prime p := let p := min_fac (n! + 1) in have f1 : n! + 1 β‰  1, from ne_of_gt $ succ_lt_succ $ factorial_pos _, have pp : prime p, from min_fac_prime f1, have np : n ≀ p, from le_of_not_ge $ Ξ» h, have h₁ : p ∣ n!, from dvd_factorial (min_fac_pos _) h, have hβ‚‚ : p ∣ 1, from (nat.dvd_add_iff_right h₁).2 (min_fac_dvd _), pp.not_dvd_one hβ‚‚, ⟨p, np, pp⟩ lemma prime.eq_two_or_odd {p : β„•} (hp : prime p) : p = 2 ∨ p % 2 = 1 := (nat.mod_two_eq_zero_or_one p).elim (Ξ» h, or.inl ((hp.2 2 (dvd_of_mod_eq_zero h)).resolve_left dec_trivial).symm) or.inr theorem coprime_of_dvd {m n : β„•} (H : βˆ€ k, prime k β†’ k ∣ m β†’ Β¬ k ∣ n) : coprime m n := begin cases nat.eq_zero_or_pos (gcd m n) with g0 g1, { rw [eq_zero_of_gcd_eq_zero_left g0, eq_zero_of_gcd_eq_zero_right g0] at H, exfalso, exact H 2 prime_two (dvd_zero _) (dvd_zero _) }, apply eq.symm, change 1 ≀ _ at g1, apply (lt_or_eq_of_le g1).resolve_left, intro g2, obtain ⟨p, hp, hpdvd⟩ := exists_prime_and_dvd g2, apply H p hp; apply dvd_trans hpdvd, { exact gcd_dvd_left _ _ }, { exact gcd_dvd_right _ _ } end theorem coprime_of_dvd' {m n : β„•} (H : βˆ€ k, prime k β†’ k ∣ m β†’ k ∣ n β†’ k ∣ 1) : coprime m n := coprime_of_dvd $ Ξ»k kp km kn, not_le_of_gt kp.one_lt $ le_of_dvd zero_lt_one $ H k kp km kn theorem factors_lemma {k} : (k+2) / min_fac (k+2) < k+2 := div_lt_self dec_trivial (min_fac_prime dec_trivial).one_lt /-- `factors n` is the prime factorization of `n`, listed in increasing order. -/ def factors : β„• β†’ list β„• | 0 := [] | 1 := [] | n@(k+2) := let m := min_fac n in have n / m < n := factors_lemma, m :: factors (n / m) @[simp] lemma factors_zero : factors 0 = [] := by rw factors @[simp] lemma factors_one : factors 1 = [] := by rw factors lemma prime_of_mem_factors : βˆ€ {n p}, p ∈ factors n β†’ prime p | 0 := by simp | 1 := by simp | n@(k+2) := Ξ» p h, let m := min_fac n in have n / m < n := factors_lemma, have h₁ : p = m ∨ p ∈ (factors (n / m)) := (list.mem_cons_iff _ _ _).1 (by rwa [factors] at h), or.cases_on h₁ (Ξ» hβ‚‚, hβ‚‚.symm β–Έ min_fac_prime dec_trivial) prime_of_mem_factors lemma prod_factors : βˆ€ {n}, 0 < n β†’ list.prod (factors n) = n | 0 := by simp | 1 := by simp | n@(k+2) := Ξ» h, let m := min_fac n in have n / m < n := factors_lemma, show (factors n).prod = n, from have h₁ : 0 < n / m := nat.pos_of_ne_zero $ Ξ» h, have n = 0 * m := (nat.div_eq_iff_eq_mul_left (min_fac_pos _) (min_fac_dvd _)).1 h, by rw zero_mul at this; exact (show k + 2 β‰  0, from dec_trivial) this, by rw [factors, list.prod_cons, prod_factors h₁, nat.mul_div_cancel' (min_fac_dvd _)] lemma factors_prime {p : β„•} (hp : nat.prime p) : p.factors = [p] := begin have : p = (p - 2) + 2 := (nat.sub_eq_iff_eq_add hp.1).mp rfl, rw [this, nat.factors], simp only [eq.symm this], have : nat.min_fac p = p := (nat.prime_def_min_fac.mp hp).2, split, { exact this, }, { simp only [this, nat.factors, nat.div_self (nat.prime.pos hp)], }, end lemma factors_chain : βˆ€ {n a}, (βˆ€ p, prime p β†’ p ∣ n β†’ a ≀ p) β†’ list.chain (≀) a (factors n) | 0 := Ξ» a h, by simp | 1 := Ξ» a h, by simp | n@(k+2) := Ξ» a h, let m := min_fac n in have n / m < n := factors_lemma, begin rw factors, refine list.chain.cons ((le_min_fac.2 h).resolve_left dec_trivial) (factors_chain _), exact Ξ» p pp d, min_fac_le_of_dvd pp.two_le (d.trans $ div_dvd_of_dvd $ min_fac_dvd _), end lemma factors_chain_2 (n) : list.chain (≀) 2 (factors n) := factors_chain $ Ξ» p pp _, pp.two_le lemma factors_chain' (n) : list.chain' (≀) (factors n) := @list.chain'.tail _ _ (_::_) (factors_chain_2 _) lemma factors_sorted (n : β„•) : list.sorted (≀) (factors n) := (list.chain'_iff_pairwise (@le_trans _ _)).1 (factors_chain' _) /-- `factors` can be constructed inductively by extracting `min_fac`, for sufficiently large `n`. -/ lemma factors_add_two (n : β„•) : factors (n+2) = min_fac (n+2) :: factors ((n+2) / min_fac (n+2)) := by rw factors @[simp] lemma factors_eq_nil (n : β„•) : n.factors = [] ↔ n = 0 ∨ n = 1 := begin split; intro h, { rcases n with (_ | _ | n), { exact or.inl rfl }, { exact or.inr rfl }, { rw factors at h, injection h }, }, { rcases h with (rfl | rfl), { exact factors_zero }, { exact factors_one }, } end theorem prime.coprime_iff_not_dvd {p n : β„•} (pp : prime p) : coprime p n ↔ Β¬ p ∣ n := ⟨λ co d, pp.not_dvd_one $ co.dvd_of_dvd_mul_left (by simp [d]), Ξ» nd, coprime_of_dvd $ Ξ» m m2 mp, ((prime_dvd_prime_iff_eq m2 pp).1 mp).symm β–Έ nd⟩ theorem prime.dvd_iff_not_coprime {p n : β„•} (pp : prime p) : p ∣ n ↔ Β¬ coprime p n := iff_not_comm.2 pp.coprime_iff_not_dvd theorem prime.not_coprime_iff_dvd {m n : β„•} : Β¬ coprime m n ↔ βˆƒp, prime p ∧ p ∣ m ∧ p ∣ n := begin apply iff.intro, { intro h, exact ⟨min_fac (gcd m n), min_fac_prime h, ((min_fac_dvd (gcd m n)).trans (gcd_dvd_left m n)), ((min_fac_dvd (gcd m n)).trans (gcd_dvd_right m n))⟩ }, { intro h, cases h with p hp, apply nat.not_coprime_of_dvd_of_dvd (prime.one_lt hp.1) hp.2.1 hp.2.2 } end theorem prime.dvd_mul {p m n : β„•} (pp : prime p) : p ∣ m * n ↔ p ∣ m ∨ p ∣ n := ⟨λ H, or_iff_not_imp_left.2 $ Ξ» h, (pp.coprime_iff_not_dvd.2 h).dvd_of_dvd_mul_left H, or.rec (Ξ» h : p ∣ m, h.mul_right _) (Ξ» h : p ∣ n, h.mul_left _)⟩ theorem prime.not_dvd_mul {p m n : β„•} (pp : prime p) (Hm : Β¬ p ∣ m) (Hn : Β¬ p ∣ n) : Β¬ p ∣ m * n := mt pp.dvd_mul.1 $ by simp [Hm, Hn] theorem prime.dvd_of_dvd_pow {p m n : β„•} (pp : prime p) (h : p ∣ m^n) : p ∣ m := by induction n with n IH; [exact pp.not_dvd_one.elim h, by { rw pow_succ at h, exact (pp.dvd_mul.1 h).elim id IH } ] lemma prime.pow_dvd_of_dvd_mul_right {p n a b : β„•} (hp : p.prime) (h : p ^ n ∣ a * b) (hpb : Β¬ p ∣ b) : p ^ n ∣ a := begin induction n with n ih, { simp }, { rw [pow_succ'] at *, rcases ih ((dvd_mul_right _ _).trans h) with ⟨c, rfl⟩, rw [mul_assoc] at h, rcases hp.dvd_mul.1 (nat.dvd_of_mul_dvd_mul_left (pow_pos hp.pos _) h) with ⟨d, rfl⟩|⟨d, rfl⟩, { rw [← mul_assoc], exact dvd_mul_right _ _ }, { exact (hpb (dvd_mul_right _ _)).elim } } end lemma prime.pow_dvd_of_dvd_mul_left {p n a b : β„•} (hp : p.prime) (h : p ^ n ∣ a * b) (hpb : Β¬ p ∣ a) : p ^ n ∣ b := by rw [mul_comm] at h; exact hp.pow_dvd_of_dvd_mul_right h hpb lemma prime.pow_not_prime {x n : β„•} (hn : 2 ≀ n) : Β¬ (x ^ n).prime := Ξ» hp, (hp.2 x $ dvd_trans ⟨x, sq _⟩ (pow_dvd_pow _ hn)).elim (Ξ» hx1, hp.ne_one $ hx1.symm β–Έ one_pow _) (Ξ» hxn, lt_irrefl x $ calc x = x ^ 1 : (pow_one _).symm ... < x ^ n : nat.pow_right_strict_mono (hxn.symm β–Έ hp.two_le) hn ... = x : hxn.symm) lemma prime.pow_not_prime' {x : β„•} : βˆ€ {n : β„•}, n β‰  1 β†’ Β¬ (x ^ n).prime | 0 := Ξ» _, not_prime_one | 1 := Ξ» h, (h rfl).elim | (n+2) := Ξ» _, prime.pow_not_prime le_add_self lemma prime.eq_one_of_pow {x n : β„•} (h : (x ^ n).prime) : n = 1 := not_imp_not.mp prime.pow_not_prime' h lemma prime.pow_eq_iff {p a k : β„•} (hp : p.prime) : a ^ k = p ↔ a = p ∧ k = 1 := begin refine ⟨_, Ξ» h, by rw [h.1, h.2, pow_one]⟩, rintro rfl, rw [hp.eq_one_of_pow, eq_self_iff_true, and_true, pow_one], end lemma prime.mul_eq_prime_sq_iff {x y p : β„•} (hp : p.prime) (hx : x β‰  1) (hy : y β‰  1) : x * y = p ^ 2 ↔ x = p ∧ y = p := ⟨λ h, have pdvdxy : p ∣ x * y, by rw h; simp [sq], begin wlog := hp.dvd_mul.1 pdvdxy using x y, cases case with a ha, have hap : a ∣ p, from ⟨y, by rwa [ha, sq, mul_assoc, nat.mul_right_inj hp.pos, eq_comm] at h⟩, exact ((nat.dvd_prime hp).1 hap).elim (Ξ» _, by clear_aux_decl; simp [*, sq, nat.mul_right_inj hp.pos] at * {contextual := tt}) (Ξ» _, by clear_aux_decl; simp [*, sq, mul_comm, mul_assoc, nat.mul_right_inj hp.pos, nat.mul_right_eq_self_iff hp.pos] at * {contextual := tt}) end, Ξ» ⟨h₁, hβ‚‚βŸ©, h₁.symm β–Έ hβ‚‚.symm β–Έ (sq _).symm⟩ lemma prime.dvd_factorial : βˆ€ {n p : β„•} (hp : prime p), p ∣ n! ↔ p ≀ n | 0 p hp := iff_of_false hp.not_dvd_one (not_le_of_lt hp.pos) | (n+1) p hp := begin rw [factorial_succ, hp.dvd_mul, prime.dvd_factorial hp], exact ⟨λ h, h.elim (le_of_dvd (succ_pos _)) le_succ_of_le, Ξ» h, (_root_.lt_or_eq_of_le h).elim (or.inr ∘ le_of_lt_succ) (Ξ» h, or.inl $ by rw h)⟩ end theorem prime.coprime_pow_of_not_dvd {p m a : β„•} (pp : prime p) (h : Β¬ p ∣ a) : coprime a (p^m) := (pp.coprime_iff_not_dvd.2 h).symm.pow_right _ theorem coprime_primes {p q : β„•} (pp : prime p) (pq : prime q) : coprime p q ↔ p β‰  q := pp.coprime_iff_not_dvd.trans $ not_congr $ dvd_prime_two_le pq pp.two_le theorem coprime_pow_primes {p q : β„•} (n m : β„•) (pp : prime p) (pq : prime q) (h : p β‰  q) : coprime (p^n) (q^m) := ((coprime_primes pp pq).2 h).pow _ _ theorem coprime_or_dvd_of_prime {p} (pp : prime p) (i : β„•) : coprime p i ∨ p ∣ i := by rw [pp.dvd_iff_not_coprime]; apply em theorem dvd_prime_pow {p : β„•} (pp : prime p) {m i : β„•} : i ∣ (p^m) ↔ βˆƒ k ≀ m, i = p^k := begin induction m with m IH generalizing i, {simp [pow_succ, le_zero_iff] at *}, by_cases p ∣ i, { cases h with a e, subst e, rw [pow_succ, nat.mul_dvd_mul_iff_left pp.pos, IH], split; intro h; rcases h with ⟨k, h, e⟩, { exact ⟨succ k, succ_le_succ h, by rw [e, pow_succ]; refl⟩ }, cases k with k, { apply pp.not_dvd_one.elim, simp at e, rw ← e, apply dvd_mul_right }, { refine ⟨k, le_of_succ_le_succ h, _⟩, rwa [mul_comm, pow_succ', nat.mul_left_inj pp.pos] at e } }, { split; intro d, { rw (pp.coprime_pow_of_not_dvd h).eq_one_of_dvd d, exact ⟨0, zero_le _, rfl⟩ }, { rcases d with ⟨k, l, e⟩, rw e, exact pow_dvd_pow _ l } } end /-- If `p` is prime, and `a` doesn't divide `p^k`, but `a` does divide `p^(k+1)` then `a = p^(k+1)`. -/ lemma eq_prime_pow_of_dvd_least_prime_pow {a p k : β„•} (pp : prime p) (h₁ : Β¬(a ∣ p^k)) (hβ‚‚ : a ∣ p^(k+1)) : a = p^(k+1) := begin obtain ⟨l, ⟨h, rfl⟩⟩ := (dvd_prime_pow pp).1 hβ‚‚, congr, exact le_antisymm h (not_le.1 ((not_congr (pow_dvd_pow_iff_le_right (prime.one_lt pp))).1 h₁)), end section open list lemma mem_list_primes_of_dvd_prod {p : β„•} (hp : prime p) : βˆ€ {l : list β„•}, (βˆ€ p ∈ l, prime p) β†’ p ∣ prod l β†’ p ∈ l | [] := Ξ» h₁ hβ‚‚, absurd hβ‚‚ (prime.not_dvd_one hp) | (q :: l) := Ξ» h₁ hβ‚‚, have h₃ : p ∣ q * prod l := @prod_cons _ _ l q β–Έ hβ‚‚, have hq : prime q := h₁ q (mem_cons_self _ _), or.cases_on ((prime.dvd_mul hp).1 h₃) (Ξ» h, by rw [prime.dvd_iff_not_coprime hp, coprime_primes hp hq, ne.def, not_not] at h; exact h β–Έ mem_cons_self _ _) (Ξ» h, have hl : βˆ€ p ∈ l, prime p := Ξ» p hlp, h₁ p ((mem_cons_iff _ _ _).2 (or.inr hlp)), (mem_cons_iff _ _ _).2 (or.inr (mem_list_primes_of_dvd_prod hl h))) lemma mem_factors_iff_dvd {n p : β„•} (hn : 0 < n) (hp : prime p) : p ∈ factors n ↔ p ∣ n := ⟨λ h, prod_factors hn β–Έ list.dvd_prod h, Ξ» h, mem_list_primes_of_dvd_prod hp (@prime_of_mem_factors n) ((prod_factors hn).symm β–Έ h)⟩ lemma mem_factors {n p} (hn : 0 < n) : p ∈ factors n ↔ prime p ∧ p ∣ n := ⟨λ h, ⟨prime_of_mem_factors h, (mem_factors_iff_dvd hn $ prime_of_mem_factors h).mp h⟩, Ξ» ⟨hprime, hdvd⟩, (mem_factors_iff_dvd hn hprime).mpr hdvd⟩ lemma factors_subset_right {n k : β„•} (h : k β‰  0) : n.factors βŠ† (n * k).factors := begin cases n, { rw zero_mul, refl }, cases n, { rw factors_one, apply list.nil_subset }, intros p hp, rw mem_factors succ_pos' at hp, rw mem_factors (nat.mul_pos succ_pos' (nat.pos_of_ne_zero h)), exact ⟨hp.1, hp.2.mul_right k⟩, end lemma factors_subset_of_dvd {n k : β„•} (h : n ∣ k) (h' : k β‰  0) : n.factors βŠ† k.factors := begin obtain ⟨a, rfl⟩ := h, exact factors_subset_right (right_ne_zero_of_mul h'), end lemma perm_of_prod_eq_prod : βˆ€ {l₁ lβ‚‚ : list β„•}, prod l₁ = prod lβ‚‚ β†’ (βˆ€ p ∈ l₁, prime p) β†’ (βˆ€ p ∈ lβ‚‚, prime p) β†’ l₁ ~ lβ‚‚ | [] [] _ _ _ := perm.nil | [] (a :: l) h₁ hβ‚‚ h₃ := have ha : a ∣ 1 := @prod_nil β„• _ β–Έ h₁.symm β–Έ (@prod_cons _ _ l a).symm β–Έ dvd_mul_right _ _, absurd ha (prime.not_dvd_one (h₃ a (mem_cons_self _ _))) | (a :: l) [] h₁ hβ‚‚ h₃ := have ha : a ∣ 1 := @prod_nil β„• _ β–Έ h₁ β–Έ (@prod_cons _ _ l a).symm β–Έ dvd_mul_right _ _, absurd ha (prime.not_dvd_one (hβ‚‚ a (mem_cons_self _ _))) | (a :: l₁) (b :: lβ‚‚) h hl₁ hlβ‚‚ := have hl₁' : βˆ€ p ∈ l₁, prime p := Ξ» p hp, hl₁ p (mem_cons_of_mem _ hp), have hlβ‚‚' : βˆ€ p ∈ (b :: lβ‚‚).erase a, prime p := Ξ» p hp, hlβ‚‚ p (mem_of_mem_erase hp), have ha : a ∈ (b :: lβ‚‚) := mem_list_primes_of_dvd_prod (hl₁ a (mem_cons_self _ _)) hlβ‚‚ (h β–Έ by rw prod_cons; exact dvd_mul_right _ _), have hb : b :: lβ‚‚ ~ a :: (b :: lβ‚‚).erase a := perm_cons_erase ha, have hl : prod l₁ = prod ((b :: lβ‚‚).erase a) := (nat.mul_right_inj (prime.pos (hl₁ a (mem_cons_self _ _)))).1 $ by rwa [← prod_cons, ← prod_cons, ← hb.prod_eq], perm.trans ((perm_of_prod_eq_prod hl hl₁' hlβ‚‚').cons _) hb.symm /-- **Fundamental theorem of arithmetic**-/ lemma factors_unique {n : β„•} {l : list β„•} (h₁ : prod l = n) (hβ‚‚ : βˆ€ p ∈ l, prime p) : l ~ factors n := have hn : 0 < n := nat.pos_of_ne_zero $ Ξ» h, begin rw h at *, clear h, induction l with a l hi, { exact absurd h₁ dec_trivial }, { rw prod_cons at h₁, exact nat.mul_ne_zero (ne_of_lt (prime.pos (hβ‚‚ a (mem_cons_self _ _)))).symm (hi (Ξ» p hp, hβ‚‚ p (mem_cons_of_mem _ hp))) h₁ } end, perm_of_prod_eq_prod (by rwa prod_factors hn) hβ‚‚ (@prime_of_mem_factors _) lemma prime.factors_pow {p : β„•} (hp : p.prime) (n : β„•) : (p ^ n).factors = list.repeat p n := begin symmetry, rw ← list.repeat_perm, apply nat.factors_unique (list.prod_repeat p n), { intros q hq, rwa eq_of_mem_repeat hq }, end end lemma succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul {p : β„•} (p_prime : prime p) {m n k l : β„•} (hpm : p ^ k ∣ m) (hpn : p ^ l ∣ n) (hpmn : p ^ (k+l+1) ∣ m*n) : p ^ (k+1) ∣ m ∨ p ^ (l+1) ∣ n := have hpd : p^(k+l)*p ∣ m*n, by rwa pow_succ' at hpmn, have hpd2 : p ∣ (m*n) / p ^ (k+l), from dvd_div_of_mul_dvd hpd, have hpd3 : p ∣ (m*n) / (p^k * p^l), by simpa [pow_add] using hpd2, have hpd4 : p ∣ (m / p^k) * (n / p^l), by simpa [nat.div_mul_div hpm hpn] using hpd3, have hpd5 : p ∣ (m / p^k) ∨ p ∣ (n / p^l), from (prime.dvd_mul p_prime).1 hpd4, suffices p^k*p ∣ m ∨ p^l*p ∣ n, by rwa [pow_succ', pow_succ'], hpd5.elim (assume : p ∣ m / p ^ k, or.inl $ mul_dvd_of_dvd_div hpm this) (assume : p ∣ n / p ^ l, or.inr $ mul_dvd_of_dvd_div hpn this) /-- The type of prime numbers -/ def primes := {p : β„• // p.prime} namespace primes instance : has_repr nat.primes := ⟨λ p, repr p.val⟩ instance inhabited_primes : inhabited primes := ⟨⟨2, prime_two⟩⟩ instance coe_nat : has_coe nat.primes β„• := ⟨subtype.val⟩ theorem coe_nat_inj (p q : nat.primes) : (p : β„•) = (q : β„•) β†’ p = q := Ξ» h, subtype.eq h end primes instance monoid.prime_pow {Ξ± : Type*} [monoid Ξ±] : has_pow Ξ± primes := ⟨λ x p, x^p.val⟩ end nat /-! ### Primality prover -/ namespace tactic namespace norm_num open norm_num lemma is_prime_helper (n : β„•) (h₁ : 1 < n) (hβ‚‚ : nat.min_fac n = n) : nat.prime n := nat.prime_def_min_fac.2 ⟨h₁, hβ‚‚βŸ© lemma min_fac_bit0 (n : β„•) : nat.min_fac (bit0 n) = 2 := by simp [nat.min_fac_eq, show 2 ∣ bit0 n, by simp [bit0_eq_two_mul n]] /-- A predicate representing partial progress in a proof of `min_fac`. -/ def min_fac_helper (n k : β„•) : Prop := 0 < k ∧ bit1 k ≀ nat.min_fac (bit1 n) theorem min_fac_helper.n_pos {n k : β„•} (h : min_fac_helper n k) : 0 < n := pos_iff_ne_zero.2 $ Ξ» e, by rw e at h; exact not_le_of_lt (nat.bit1_lt h.1) h.2 lemma min_fac_ne_bit0 {n k : β„•} : nat.min_fac (bit1 n) β‰  bit0 k := by rw bit0_eq_two_mul; exact Ξ» e, absurd ((nat.dvd_add_iff_right (by simp [bit0_eq_two_mul n])).2 (dvd_trans ⟨_, e⟩ (nat.min_fac_dvd _))) (by norm_num) lemma min_fac_helper_0 (n : β„•) (h : 0 < n) : min_fac_helper n 1 := begin refine ⟨zero_lt_one, lt_of_le_of_ne _ min_fac_ne_bit0.symm⟩, refine @lt_of_le_of_ne β„• _ _ _ (nat.min_fac_pos _) _, intro e, have := nat.min_fac_prime _, { rw ← e at this, exact nat.not_prime_one this }, { exact ne_of_gt (nat.bit1_lt h) } end lemma min_fac_helper_1 {n k k' : β„•} (e : k + 1 = k') (np : nat.min_fac (bit1 n) β‰  bit1 k) (h : min_fac_helper n k) : min_fac_helper n k' := begin rw ← e, refine ⟨nat.succ_pos _, (lt_of_le_of_ne (lt_of_le_of_ne _ _ : k+1+k < _) min_fac_ne_bit0.symm : bit0 (k+1) < _)⟩, { rw add_right_comm, exact h.2 }, { rw add_right_comm, exact np.symm } end lemma min_fac_helper_2 (n k k' : β„•) (e : k + 1 = k') (np : Β¬ nat.prime (bit1 k)) (h : min_fac_helper n k) : min_fac_helper n k' := begin refine min_fac_helper_1 e _ h, intro e₁, rw ← e₁ at np, exact np (nat.min_fac_prime $ ne_of_gt $ nat.bit1_lt h.n_pos) end lemma min_fac_helper_3 (n k k' c : β„•) (e : k + 1 = k') (nc : bit1 n % bit1 k = c) (c0 : 0 < c) (h : min_fac_helper n k) : min_fac_helper n k' := begin refine min_fac_helper_1 e _ h, refine mt _ (ne_of_gt c0), intro e₁, rw [← nc, ← nat.dvd_iff_mod_eq_zero, ← e₁], apply nat.min_fac_dvd end lemma min_fac_helper_4 (n k : β„•) (hd : bit1 n % bit1 k = 0) (h : min_fac_helper n k) : nat.min_fac (bit1 n) = bit1 k := by rw ← nat.dvd_iff_mod_eq_zero at hd; exact le_antisymm (nat.min_fac_le_of_dvd (nat.bit1_lt h.1) hd) h.2 lemma min_fac_helper_5 (n k k' : β„•) (e : bit1 k * bit1 k = k') (hd : bit1 n < k') (h : min_fac_helper n k) : nat.min_fac (bit1 n) = bit1 n := begin refine (nat.prime_def_min_fac.1 (nat.prime_def_le_sqrt.2 ⟨nat.bit1_lt h.n_pos, _⟩)).2, rw ← e at hd, intros m m2 hm md, have := le_trans h.2 (le_trans (nat.min_fac_le_of_dvd m2 md) hm), rw nat.le_sqrt at this, exact not_le_of_lt hd this end /-- Given `e` a natural numeral and `d : nat` a factor of it, return `⊒ Β¬ prime e`. -/ meta def prove_non_prime (e : expr) (n d₁ : β„•) : tactic expr := do let e₁ := reflect d₁, c ← mk_instance_cache `(nat), (c, p₁) ← prove_lt_nat c `(1) e₁, let dβ‚‚ := n / d₁, let eβ‚‚ := reflect dβ‚‚, (c, e', p) ← prove_mul_nat c e₁ eβ‚‚, guard (e' =ₐ e), (c, pβ‚‚) ← prove_lt_nat c `(1) eβ‚‚, return $ `(@nat.not_prime_mul').mk_app [e₁, eβ‚‚, e, p, p₁, pβ‚‚] /-- Given `a`,`a1 := bit1 a`, `n1` the value of `a1`, `b` and `p : min_fac_helper a b`, returns `(c, ⊒ min_fac a1 = c)`. -/ meta def prove_min_fac_aux (a a1 : expr) (n1 : β„•) : instance_cache β†’ expr β†’ expr β†’ tactic (instance_cache Γ— expr Γ— expr) | ic b p := do k ← b.to_nat, let k1 := bit1 k, let b1 := `(bit1:β„•β†’β„•).mk_app [b], if n1 < k1*k1 then do (ic, e', p₁) ← prove_mul_nat ic b1 b1, (ic, pβ‚‚) ← prove_lt_nat ic a1 e', return (ic, a1, `(min_fac_helper_5).mk_app [a, b, e', p₁, pβ‚‚, p]) else let d := k1.min_fac in if to_bool (d < k1) then do let k' := k+1, let e' := reflect k', (ic, p₁) ← prove_succ ic b e', pβ‚‚ ← prove_non_prime b1 k1 d, prove_min_fac_aux ic e' $ `(min_fac_helper_2).mk_app [a, b, e', p₁, pβ‚‚, p] else do let nc := n1 % k1, (ic, c, pc) ← prove_div_mod ic a1 b1 tt, if nc = 0 then return (ic, b1, `(min_fac_helper_4).mk_app [a, b, pc, p]) else do (ic, pβ‚€) ← prove_pos ic c, let k' := k+1, let e' := reflect k', (ic, p₁) ← prove_succ ic b e', prove_min_fac_aux ic e' $ `(min_fac_helper_3).mk_app [a, b, e', c, p₁, pc, pβ‚€, p] /-- Given `a` a natural numeral, returns `(b, ⊒ min_fac a = b)`. -/ meta def prove_min_fac (ic : instance_cache) (e : expr) : tactic (instance_cache Γ— expr Γ— expr) := match match_numeral e with | match_numeral_result.zero := return (ic, `(2:β„•), `(nat.min_fac_zero)) | match_numeral_result.one := return (ic, `(1:β„•), `(nat.min_fac_one)) | match_numeral_result.bit0 e := return (ic, `(2), `(min_fac_bit0).mk_app [e]) | match_numeral_result.bit1 e := do n ← e.to_nat, c ← mk_instance_cache `(nat), (c, p) ← prove_pos c e, let a1 := `(bit1:β„•β†’β„•).mk_app [e], prove_min_fac_aux e a1 (bit1 n) c `(1) (`(min_fac_helper_0).mk_app [e, p]) | _ := failed end /-- A partial proof of `factors`. Asserts that `l` is a sorted list of primes, lower bounded by a prime `p`, which multiplies to `n`. -/ def factors_helper (n p : β„•) (l : list β„•) : Prop := p.prime β†’ list.chain (≀) p l ∧ (βˆ€ a ∈ l, nat.prime a) ∧ list.prod l = n lemma factors_helper_nil (a : β„•) : factors_helper 1 a [] := Ξ» pa, ⟨list.chain.nil, by rintro _ ⟨⟩, list.prod_nil⟩ lemma factors_helper_cons' (n m a b : β„•) (l : list β„•) (h₁ : b * m = n) (hβ‚‚ : a ≀ b) (h₃ : nat.min_fac b = b) (H : factors_helper m b l) : factors_helper n a (b :: l) := Ξ» pa, have pb : b.prime, from nat.prime_def_min_fac.2 ⟨le_trans pa.two_le hβ‚‚, hβ‚ƒβŸ©, let ⟨f₁, fβ‚‚, fβ‚ƒβŸ© := H pb in ⟨list.chain.cons hβ‚‚ f₁, Ξ» c h, h.elim (Ξ» e, e.symm β–Έ pb) (fβ‚‚ _), by rw [list.prod_cons, f₃, h₁]⟩ lemma factors_helper_cons (n m a b : β„•) (l : list β„•) (h₁ : b * m = n) (hβ‚‚ : a < b) (h₃ : nat.min_fac b = b) (H : factors_helper m b l) : factors_helper n a (b :: l) := factors_helper_cons' _ _ _ _ _ h₁ hβ‚‚.le h₃ H lemma factors_helper_sn (n a : β„•) (h₁ : a < n) (hβ‚‚ : nat.min_fac n = n) : factors_helper n a [n] := factors_helper_cons _ _ _ _ _ (mul_one _) h₁ hβ‚‚ (factors_helper_nil _) lemma factors_helper_same (n m a : β„•) (l : list β„•) (h : a * m = n) (H : factors_helper m a l) : factors_helper n a (a :: l) := Ξ» pa, factors_helper_cons' _ _ _ _ _ h (le_refl _) (nat.prime_def_min_fac.1 pa).2 H pa lemma factors_helper_same_sn (a : β„•) : factors_helper a a [a] := factors_helper_same _ _ _ _ (mul_one _) (factors_helper_nil _) lemma factors_helper_end (n : β„•) (l : list β„•) (H : factors_helper n 2 l) : nat.factors n = l := let ⟨h₁, hβ‚‚, hβ‚ƒβŸ© := H nat.prime_two in have _, from (list.chain'_iff_pairwise (@le_trans _ _)).1 (@list.chain'.tail _ _ (_::_) h₁), (list.eq_of_perm_of_sorted (nat.factors_unique h₃ hβ‚‚) this (nat.factors_sorted _)).symm /-- Given `n` and `a` natural numerals, returns `(l, ⊒ factors_helper n a l)`. -/ meta def prove_factors_aux : instance_cache β†’ expr β†’ expr β†’ β„• β†’ β„• β†’ tactic (instance_cache Γ— expr Γ— expr) | c en ea n a := let b := n.min_fac in if b < n then do let m := n / b, (c, em) ← c.of_nat m, if b = a then do (c, _, p₁) ← prove_mul_nat c ea em, (c, l, pβ‚‚) ← prove_factors_aux c em ea m a, pure (c, `(%%ea::%%l:list β„•), `(factors_helper_same).mk_app [en, em, ea, l, p₁, pβ‚‚]) else do (c, eb) ← c.of_nat b, (c, _, p₁) ← prove_mul_nat c eb em, (c, pβ‚‚) ← prove_lt_nat c ea eb, (c, _, p₃) ← prove_min_fac c eb, (c, l, pβ‚„) ← prove_factors_aux c em eb m b, pure (c, `(%%eb::%%l : list β„•), `(factors_helper_cons).mk_app [en, em, ea, eb, l, p₁, pβ‚‚, p₃, pβ‚„]) else if b = a then pure (c, `([%%ea] : list β„•), `(factors_helper_same_sn).mk_app [ea]) else do (c, p₁) ← prove_lt_nat c ea en, (c, _, pβ‚‚) ← prove_min_fac c en, pure (c, `([%%en] : list β„•), `(factors_helper_sn).mk_app [en, ea, p₁, pβ‚‚]) /-- Evaluates the `prime` and `min_fac` functions. -/ @[norm_num] meta def eval_prime : expr β†’ tactic (expr Γ— expr) | `(nat.prime %%e) := do n ← e.to_nat, match n with | 0 := false_intro `(nat.not_prime_zero) | 1 := false_intro `(nat.not_prime_one) | _ := let d₁ := n.min_fac in if d₁ < n then prove_non_prime e n d₁ >>= false_intro else do let e₁ := reflect d₁, c ← mk_instance_cache `(β„•), (c, p₁) ← prove_lt_nat c `(1) e₁, (c, e₁, p) ← prove_min_fac c e, true_intro $ `(is_prime_helper).mk_app [e, p₁, p] end | `(nat.min_fac %%e) := do ic ← mk_instance_cache `(β„•), prod.snd <$> prove_min_fac ic e | `(nat.factors %%e) := do n ← e.to_nat, match n with | 0 := pure (`(@list.nil β„•), `(nat.factors_zero)) | 1 := pure (`(@list.nil β„•), `(nat.factors_one)) | _ := do c ← mk_instance_cache `(β„•), (c, l, p) ← prove_factors_aux c e `(2) n 2, pure (l, `(factors_helper_end).mk_app [e, l, p]) end | _ := failed end norm_num end tactic namespace nat theorem prime_three : prime 3 := by norm_num end nat
5c31fe4b0a57c23df9f153f35622b986ec760c19
42c01158c2730cc6ac3e058c1339c18cb90366e2
/xenalib/square_root.lean
cfc7128e14594872f43184322438067e6ee1a4b7
[]
no_license
ChrisHughes24/xena
c80d94355d0c2ae8deddda9d01e6d31bc21c30ae
337a0d7c9f0e255e08d6d0a383e303c080c6ec0c
refs/heads/master
1,631,059,898,392
1,511,200,551,000
1,511,200,551,000
111,468,589
1
0
null
null
null
null
UTF-8
Lean
false
false
13,700
lean
/- Copyright (c) 2017 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Kevin Buzzard A square root function. 1) The non-computable function square_root : Ξ  (x : ℝ), x β‰₯ 0 β†’ ℝ returns the non-negative square root of x, defined as the sup of all the reals whose square is at most x. 2) The non-computable function sqrt_abs : ℝ β†’ ℝ sends a real number x to the non-negative square root of abs x (a.k.a. |x|). -/ import analysis.real tactic.norm_num -- analysis.real -- for reals -- tactic.norm_num -- because I want to do proofs of things like 1/4 < 1 in ℝ quickly -- init.classical -- don't know why yet. -- Can I just dump the below things into Lean within no namespace? theorem pow_two_eq_mul_self {Ξ± : Type} [monoid Ξ±] {x : Ξ±} : x ^ 2 = x * x := by simp [pow_nat, has_pow_nat.pow_nat, monoid.pow] theorem mul_self_eq_pow_two {Ξ± : Type} [monoid Ξ±] {x : Ξ±} : x * x = x ^ 2 := eq.symm pow_two_eq_mul_self theorem imp_of_not_or {A B : Prop} : (A ∨ B) β†’ (Β¬ A) β†’ B := begin intros Hor HnBofA, cases Hor, contradiction, exact a end -- #check @abs_sub_square -- why is that even there? /- Possibly more useful: #check @nonneg_le_nonneg_of_squares_le nonneg_le_nonneg_of_squares_le : βˆ€ {Ξ± : Type u_1} [_inst_1 : linear_ordered_ring Ξ±] {a b : Ξ±}, b β‰₯ 0 β†’ a * a ≀ b * b β†’ a ≀ b -/ namespace square_root -- I have no idea whether this is the right thing to do open square_root theorem square_inj_on_nonneg {x y : ℝ} : (x β‰₯ 0) β†’ (y β‰₯ 0) β†’ (x^2 = y^2) β†’ (x=y) := begin assume H_x_ge_zero : x β‰₯ 0, assume H_y_ge_zero : y β‰₯ 0, assume H : x ^ 2 = y ^ 2, rw [pow_two_eq_mul_self,pow_two_eq_mul_self,mul_self_eq_mul_self_iff] at H, cases H with Heq Hneg, exact Heq, have H2 : x = 0 ∧ y = 0, exact (add_eq_zero_iff_eq_zero_of_nonneg H_x_ge_zero H_y_ge_zero).1 (add_eq_zero_iff_eq_neg.2 Hneg), rw [H2.left,H2.right], end theorem square_cont_at_zero : βˆ€ (r : ℝ), r > 0 β†’ βˆƒ (eps : ℝ), (eps > 0) ∧ eps ^ 2 < r := begin intros r Hr_gt_0, cases lt_or_ge r 1 with Hrl1 Hrge1, have H : r^2<r, unfold pow_nat has_pow_nat.pow_nat monoid.pow, exact calc r*(r*1) = r*r : by simp ... < r*1 : mul_lt_mul_of_pos_left Hrl1 Hr_gt_0 ... = r : mul_one r, existsi r, exact ⟨Hr_gt_0,H⟩, cases le_iff_eq_or_lt.mp Hrge1 with r1 rg1, rw [←r1], exact ⟨((1/2):ℝ),by norm_num⟩, existsi (1:ℝ), split, exact zero_lt_one, suffices : 1<r, by simpa, exact rg1, end -- #check @nonneg_le_nonneg_of_squares_le -- βˆ€ {Ξ± : Type u_1} [_inst_1 : linear_ordered_ring Ξ±] {a b : Ξ±}, -- b β‰₯ 0 β†’ a * a ≀ b * b β†’ a ≀ b theorem exists_square_root (r:ℝ) (rnneg : r β‰₯ 0) : βˆƒ (q : ℝ), (q β‰₯ 0) ∧ q^2=r := begin cases le_iff_eq_or_lt.mp rnneg with r0 rpos, rw [←r0], exact ⟨(0:ℝ),by norm_num⟩, clear rnneg, let S := { x:ℝ | x^2 ≀ r}, -- S non-empty have H0 : (0:ℝ) ∈ S, suffices : 0 ≀ r, by simpa [pow_two_eq_mul_self], exact le_of_lt rpos, -- S has upper bound have H1 : max r 1 ∈ upper_bounds S, cases classical.em (r ≀ 1) with rle1 rgt1, intros t Ht, suffices H : t ≀ 1, exact le_trans H (le_max_right r 1), exact nonneg_le_nonneg_of_squares_le (zero_le_one) (by rw [mul_one,←pow_two_eq_mul_self];exact (le_trans Ht rle1)), have H : 1<r, exact lt_of_not_ge rgt1, clear rgt1, intros t Ht, suffices H : t ≀ r, exact le_trans H (le_max_left r 1), -- need to prove t^2<=r implies t<=r apply imp_of_not_or (lt_or_ge r t), assume H1 : r<t, apply not_le_of_gt H1, apply nonneg_le_nonneg_of_squares_le (le_of_lt rpos), exact le_of_lt (calc t*t=t^2 : mul_self_eq_pow_two ... ≀ r : Ht ... = r*1 : eq.symm (mul_one r) ... < r*r : mul_lt_mul_of_pos_left H rpos), -- get LUB have H : βˆƒ (x : ℝ), is_lub S x, exact exists_supremum_real H0 H1, cases H with q Hq, existsi q, have Hqge0 : 0 ≀ q, exact Hq.left 0 H0, split, exact Hqge0, -- I tidied the code up, up to here; the rest is my original effort. -- idea is to prove q^2=r by showing not < or > -- first not < have H2 : Β¬ (q^2<r), intro Hq2r, have H2 : q ∈ upper_bounds S, exact Hq.left, clear Hq H0 H1, unfold upper_bounds at H2, have H3 : βˆ€ qe, q<qe β†’ Β¬(qe^2≀r), intro qe, intro qlqe, intro H4, have H5 : qe ≀ q, exact H2 qe H4, exact not_lt_of_ge H5 qlqe, have H4 : βˆ€ eps > 0,(q+eps)^2>r, intros eps Heps, exact lt_of_not_ge (H3 (q+eps) ((lt_add_iff_pos_right q).mpr Heps)), clear H3 H2 S, cases le_iff_eq_or_lt.mp Hqge0 with Hq0 Hqg0, cases (square_cont_at_zero r rpos) with eps Heps, specialize H4 eps, rw [←Hq0] at H4, simp at H4, have H3 : eps^2>r, exact H4 Heps.left, exact (lt_iff_not_ge r (eps^2)).mp H3 (le_of_lt Heps.right), clear Hqge0, -- want eps such that 2*q*eps+eps^2 <= r-q^2 -- so eps=min((r-q^2)/4q,thing-produced-by-square-cts-function) have H0 : (0:ℝ)<2, norm_num, have H : 0<(r-q^2), exact sub_pos_of_lt Hq2r, have H2 : 0 < (r-q^2)/2, exact div_pos_of_pos_of_pos H H0, have H3 : 0 < (r-q^2)/2/(2*q), exact div_pos_of_pos_of_pos H2 (mul_pos H0 Hqg0), cases (square_cont_at_zero ((r-q^2)/2) H2) with e0 He0, let e1 := min ((r-q^2)/2/(2*q)) e0, have He1 : e1>0, exact lt_min H3 He0.left, specialize H4 e1, -- should be a contradiction have H1 : (q+e1)^2 > r, exact H4 He1, have H5 : e1 ≀ ((r-q^2)/2/(2*q)), exact (min_le_left ((r-q^2)/2/(2*q)) e0), have H6 : e1*e1<(r - q ^ 2) / 2, exact calc e1*e1 ≀ e0*e1 : mul_le_mul_of_nonneg_right (min_le_right ((r - q ^ 2) / 2 / (2 * q)) e0) (le_of_lt He1) ... ≀ e0*e0 : mul_le_mul_of_nonneg_left (min_le_right ((r - q ^ 2) / 2 / (2 * q)) e0) (le_of_lt He0.left ) ... = e0^2 : by {unfold pow_nat has_pow_nat.pow_nat monoid.pow,simp} ... < (r-q^2)/2 : He0.right, have Hn1 : (q+e1)^2 < r, exact calc (q+e1)^2 = (q+e1)*(q+e1) : by {unfold pow_nat has_pow_nat.pow_nat monoid.pow,simp} ... = q*q+2*q*e1+e1*e1 : by rw [mul_add,add_mul,add_mul,mul_comm e1 q,two_mul,add_mul,add_assoc,add_assoc,add_assoc] ... = q^2 + (2*q)*e1 + e1*e1 : by {unfold pow_nat has_pow_nat.pow_nat monoid.pow,simp} ... ≀ q^2 + (2*q)*((r - q ^ 2) / 2 / (2 * q)) + e1*e1 : add_le_add_right (add_le_add_left ((mul_le_mul_left (mul_pos H0 Hqg0)).mpr H5) (q^2)) (e1*e1) ... < q^2 + (2*q)*((r - q ^ 2) / 2 / (2 * q)) + (r-q^2)/2 : add_lt_add_left H6 _ ... = r : by rw [mul_comm,div_mul_eq_mul_div,mul_div_assoc,div_self (ne_of_gt (mul_pos H0 Hqg0)),mul_one,add_assoc,div_add_div_same,←two_mul,mul_comm,mul_div_assoc,div_self (ne_of_gt H0),mul_one,add_sub,add_comm,←add_sub,sub_self,add_zero], -- rw [mul_div_cancel'], -- nearly there exact not_lt_of_ge (le_of_lt H1) Hn1, -- now not > have H3 : Β¬ (q^2>r), intro Hq2r, have H3 : q ∈ lower_bounds (upper_bounds S), exact Hq.right, clear Hq H0 H1 H2, have Hqg0 : 0 < q, cases le_iff_eq_or_lt.mp Hqge0 with Hq0 H, tactic.swap, exact H, unfold pow_nat has_pow_nat.pow_nat monoid.pow at Hq2r, rw [←Hq0] at Hq2r, simp at Hq2r, exfalso, exact not_lt_of_ge (le_of_lt rpos) Hq2r, clear Hqge0, have H : βˆ€ (eps:ℝ), (eps > 0 ∧ eps < q) β†’ (q-eps)^2 < r, unfold lower_bounds at H3, unfold set_of at H3, unfold has_mem.mem set.mem has_mem.mem at H3, intros eps Heps, have H : Β¬ ((q-eps) ∈ (upper_bounds S)), intro H, have H2 : q ≀ q-eps, exact H3 (q-eps) H, rw [le_sub_iff_add_le] at H2, have Hf : q<q, exact calc q < eps+q : lt_add_of_pos_left q Heps.left ... = q+eps : add_comm eps q ... ≀ q : H2, have Hf2 : Β¬ (q=q), exact ne_of_lt Hf, exact Hf2 (by simp), unfold upper_bounds at H, unfold has_mem.mem set.mem has_mem.mem set_of at H, have H2 : βˆƒ (b:ℝ), Β¬ (S b β†’ b ≀ q-eps), exact classical.not_forall.mp H, cases H2 with b Hb, clear H, cases classical.em (S b) with Hsb Hsnb, tactic.swap, have Hnb : S b β†’ b ≀ q - eps, intro Hsb, exfalso, exact Hsnb Hsb, exfalso, exact Hb Hnb, cases classical.em (b ≀ q - eps) with Hlt Hg, exfalso, exact Hb (Ξ» _,Hlt), have Hh : q-eps < b, exact lt_of_not_ge Hg, clear Hg Hb, -- todo: (q-eps)>0, (q-eps)^2<b^2<=r, have H0 : 0<q-eps, rw [lt_sub_iff,zero_add],exact Heps.right, unfold pow_nat has_pow_nat.pow_nat monoid.pow, exact calc (q-eps)*((q-eps)*1) = (q-eps)*(q-eps) : congr_arg (Ξ» t, (q-eps)*t) (mul_one (q-eps)) ... < (q-eps) * b : mul_lt_mul_of_pos_left Hh H0 ... < b * b : mul_lt_mul_of_pos_right Hh (lt_trans H0 Hh) ... = b^2 : by { unfold pow_nat has_pow_nat.pow_nat monoid.pow, simp} ... ≀ r : Hsb, -- We now know (q-eps)^2<r for all eps>0, and q^2>r. Need a contradiction. -- Idea: (q^2-2*q*eps+eps^2)<r so 2q.eps-eps^2>q^2-r>0, -- so we need to find eps such that 2q.eps-eps^2<(q^2-r) -- so set eps=min((q^2-r)/2q,q) have H0 : (0:ℝ)<2, norm_num, have H1 : 0<(q^2-r), exact sub_pos_of_lt Hq2r, have H2 : 0 < (q/2), exact div_pos_of_pos_of_pos Hqg0 H0, have J1 : 0 < (q^2-r)/(2*q), exact div_pos_of_pos_of_pos H1 (mul_pos H0 Hqg0), let e1 := min ((q^2-r)/(2*q)) (q/2), have He1 : e1>0, exact lt_min J1 H2, specialize H e1, -- should be a contradiction have J0 : e1<q, exact calc e1 ≀ (q/2) : min_le_right ((q^2-r)/(2*q)) (q/2) ... = q*(1/2) : by rw [←mul_div_assoc,mul_one] ... < q*1 : mul_lt_mul_of_pos_left (by norm_num) Hqg0 ... = q : by rw [mul_one], have H4 : (q-e1)^2 < r, exact H ⟨He1,J0⟩, have H5 : e1 ≀ ((q^2-r)/(2*q)), exact (min_le_left ((q^2-r)/(2*q)) (q/2)), have H6 : e1*e1>0, exact mul_pos He1 He1, have Hn1 : (q-e1)^2 > r, exact calc (q-e1)^2 = (q-e1)*(q-e1) : by {unfold pow_nat has_pow_nat.pow_nat monoid.pow,simp} ... = q*q-2*q*e1+e1*e1 : by rw [mul_sub,sub_mul,sub_mul,mul_comm e1 q,two_mul,add_mul];simp ... = q^2 - (2*q)*e1 + e1*e1 : by {unfold pow_nat has_pow_nat.pow_nat monoid.pow,simp} ... > q^2 - (2*q)*e1 : lt_add_of_pos_right (q^2 -(2*q)*e1) H6 ... β‰₯ q^2 - (2*q)*((q ^ 2 - r) / (2 * q)) : sub_le_sub (le_of_eq (eq.refl (q^2))) (mul_le_mul_of_nonneg_left H5 (le_of_lt (mul_pos H0 Hqg0))) -- lt_add_iff_pos_right -- (add_le_add_left ((mul_le_mul_left (mul_pos H0 Hqg0)).mpr H5) (q^2)) (e1*e1) ... = r : by rw [←div_mul_eq_mul_div_comm,div_self (ne_of_gt (mul_pos H0 Hqg0)),one_mul];simp, -- ... = r : by rw [mul_comm,div_mul_eq_mul_div,mul_div_assoc,div_self (ne_of_gt (mul_pos H0 Hqg0)),mul_one,add_assoc,div_add_div_same,←two_mul,mul_comm,mul_div_assoc,div_self (ne_of_gt H0),mul_one,add_sub,add_comm,←add_sub,sub_self,add_zero], -- rw [mul_div_cancel'], -- nearly there exact not_lt_of_ge (le_of_lt (H ⟨He1,J0⟩)) Hn1, have H : q^2 ≀ r, exact le_of_not_lt H3, cases lt_or_eq_of_le H with Hlt Heq, exfalso, exact H2 Hlt, exact Heq end -- #check exists_square_root -- exists_square_root : βˆ€ (r : ℝ), r β‰₯ 0 β†’ (βˆƒ (q : ℝ), q β‰₯ 0 ∧ q ^ 2 = r) theorem exists_unique_square_root : βˆ€ (r:ℝ), (r β‰₯ 0) β†’ βˆƒ (q:ℝ), (q β‰₯ 0 ∧ q^2 = r ∧ βˆ€ (s:ℝ), s β‰₯ 0 ∧ s^2 = r β†’ s=q) := begin intro r, assume H_r_ge_zero : r β‰₯ 0, cases (exists_square_root r H_r_ge_zero) with q H_q_squared_is_r, suffices H_unique : βˆ€ (s:ℝ), s β‰₯ 0 ∧ s ^ 2 = r β†’ s = q, exact ⟨q,⟨H_q_squared_is_r.left,⟨H_q_squared_is_r.right,H_unique⟩⟩⟩, intro s, assume H_s_ge_zero_and_square_is_r, exact square_inj_on_nonneg H_s_ge_zero_and_square_is_r.left H_q_squared_is_r.left (eq.trans H_s_ge_zero_and_square_is_r.right (eq.symm H_q_squared_is_r.right)) end noncomputable def square_root (x:ℝ) (H_x_nonneg : x β‰₯ 0) : ℝ := classical.some (exists_unique_square_root x H_x_nonneg) -- #reduce (square_root 2 (by norm_num)) -- oops -- Next is what Mario says I should do (at least in terms of where the proof that x>=0 goes) noncomputable def sqrt_abs (x : ℝ) : ℝ := square_root (abs x) (abs_nonneg x) def square_root_proof (x:ℝ) (h : x β‰₯ 0) : (square_root x h) ^ 2 = x := (classical.some_spec (exists_unique_square_root x h)).right.left def square_root_allinfo (x:ℝ) (h : x β‰₯ 0) := classical.some_spec (exists_unique_square_root x h) theorem sqrt_abs_ge_zero (x : ℝ) : sqrt_abs x β‰₯ 0 := (classical.some_spec (exists_unique_square_root (abs x) (abs_nonneg x))).left theorem sqrt_abs_unique (x : ℝ) (Hx_nonneg : 0 ≀ x) : βˆ€ (s : ℝ), s β‰₯ 0 ∧ s ^ 2 = x β†’ s = sqrt_abs x := begin have H : βˆ€ (s : ℝ), s β‰₯ 0 ∧ s ^ 2 = abs x β†’ s = square_root (abs x) (abs_nonneg x), exact (classical.some_spec (exists_unique_square_root (abs x) (abs_nonneg x))).right.right, intro s, intro Hs, rw [eq.symm (abs_of_nonneg Hx_nonneg)] at Hs, exact H s Hs, end theorem sqrt_abs_squared (x : ℝ) (Hx_nonneg : 0 ≀ x) : (sqrt_abs x) ^ 2 = x := begin have H0 : sqrt_abs x ^ 2 = abs x, exact square_root_proof (abs x) (abs_nonneg x), rw [H0], exact abs_of_nonneg Hx_nonneg, end theorem sqrt_abs_mul_self (x : ℝ) (Hx_nonneg : 0 ≀ x) : (sqrt_abs x) * (sqrt_abs x) = x := begin rw [mul_self_eq_pow_two], exact sqrt_abs_squared x Hx_nonneg, end meta def sqrt_tac : tactic unit := `[assumption <|> norm_num] noncomputable def sqrt (r : ℝ) (h : r β‰₯ 0 . sqrt_tac) : ℝ := classical.some (exists_unique_square_root r h) /- example of usage: noncomputable def s2 : ℝ := sqrt 2 example : s2^2=2 := sqrt_proof 2 -/ end square_root
9950745f0b34cb5a08138ea0e4cbe0649994b242
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/discrRefinement.lean
0b0af3b20cbf779e01bdfac24434544554d15614
[ "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
521
lean
inductive Vec (Ξ± : Type u) : Nat β†’ Type u | nil : Vec Ξ± 0 | cons : Ξ± β†’ Vec Ξ± n β†’ Vec Ξ± (n+1) def Vec.map (xs : Vec Ξ± n) (f : Ξ± β†’ Ξ²) : Vec Ξ² n := match xs with | nil => nil | cons a as => cons (f a) (map as f) def Vec.map' (f : Ξ± β†’ Ξ²) : Vec Ξ± n β†’ Vec Ξ² n | nil => nil | cons a as => cons (f a) (map' f as) def Vec.map2 (f : Ξ± β†’ Ξ± β†’ Ξ²) : Vec Ξ± n β†’ Vec Ξ± n β†’ Vec Ξ² n | nil, nil => nil | cons a as, cons b bs => cons (f a b) (map2 f as bs)
90467f9fbb61700c2478008936be57d6fe5c1801
a537b538f2bea3181e24409d8a52590603d1ddd9
/src/tidy/lib/list.lean
3b190fdd2d172940241d18df5b849a3ce886f00e
[]
no_license
rwbarton/lean-tidy
6134813ded72b275d19d4d32514dba80c21708e3
fe1125d32adb60decda7a77d0f679614ba9f6fbb
refs/heads/master
1,585,549,718,705
1,538,120,619,000
1,538,120,624,000
150,864,330
0
0
null
1,538,225,790,000
1,538,225,790,000
null
UTF-8
Lean
false
false
2,353
lean
import data.option import data.buffer universe u namespace list private def min_rel_aux {Ξ± : Type u} (r : Ξ± β†’ Ξ± β†’ Prop) [decidable_rel r] (curr : Ξ±) : list Ξ± β†’ Ξ± | [] := curr | (a :: rest) := if r a curr then a else curr def min_rel {Ξ± : Type u} (l : list Ξ±) (r : Ξ± β†’ Ξ± β†’ Prop) [decidable_rel r] : option Ξ± := match l with | [] := none | (a :: rest) := some $ min_rel_aux r a rest end def min {Ξ± : Type u} (l : list Ξ±) [has_lt Ξ±] [@decidable_rel Ξ± has_lt.lt] : option Ξ± := min_rel l has_lt.lt def contains {Ξ± : Type u} [decidable_eq Ξ±] (a : Ξ±) : list Ξ± β†’ bool | [] := ff | (h :: rest) := if a = h then tt else rest.contains meta def erase_duplicates {Ξ± : Type u} [decidable_eq Ξ±] : list Ξ± β†’ list Ξ± | [] := [] | (x :: t) := (x :: erase_duplicates (t.filter $ Ξ» a, a β‰  x)) def multiplex {Ξ± : Type u} : list Ξ± β†’ list Ξ± β†’ list Ξ± | [] l := l | l [] := l | (a₁ :: l₁) (aβ‚‚ :: lβ‚‚) := [a₁, aβ‚‚].append $ l₁.multiplex lβ‚‚ private def set_at_aux {Ξ± : Type u} (val : Ξ±) : list Ξ± β†’ list Ξ± β†’ β„• β†’ list Ξ± | _ [] _ := [] | l (a :: rest) 0 := l.append (val :: rest) | l (a :: rest) k := set_at_aux (l.append [a]) rest (k - 1) def set_at {Ξ± : Type u} (l : list Ξ±) (idx : β„•) (val : Ξ±) : list Ξ± := set_at_aux val [] l idx def split_on_aux {Ξ± : Type u} [decidable_eq Ξ±] (a : Ξ±) : list Ξ± β†’ list Ξ± β†’ list (list Ξ±) | [] l := [l.reverse] | (h :: t) l := if h = a then l.reverse :: (split_on_aux t []) else split_on_aux t (h :: l) def split_on {Ξ± : Type u} [decidable_eq Ξ±] (a : Ξ±) : list Ξ± β†’ list (list Ξ±) | l := split_on_aux a l [] def erase_first_such_that {Ξ± : Type u} (f : Ξ± β†’ Prop) [decidable_pred f] : list Ξ± β†’ list Ξ± | [] := [] | (h :: t) := if f h then t else (h :: t.erase_first_such_that) def erase_such_that {Ξ± : Type u} (f : Ξ± β†’ Prop) [decidable_pred f] : list Ξ± β†’ list Ξ± := filter (Ξ» x, Β¬ f x) def strip {Ξ± : Type u} [decidable_eq Ξ±] (l : list Ξ±) (v : Ξ±) : list Ξ± := l.erase_such_that (Ξ» c, c = v) def stripl {Ξ± : Type u} [decidable_eq Ξ±] (l : list Ξ±) (vs : list Ξ±) : list Ξ± := l.erase_such_that (Ξ» c, c ∈ vs) end list def list.at {Ξ± : Type u} [inhabited Ξ±] (l : list Ξ±) (n : β„•) : Ξ± := list.head $ option.to_list $ list.nth l n
71c0bff6323aaa0a30bad1f4c478a162166fbe1f
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/1603.lean
cdf3759f918496f8b0eda12592483392968a7801
[ "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
223
lean
def some_lets : β„• β†’ β„• β†’ β„• | 0 v := v | (nat.succ n) v := let k := some_lets n v + v in k def some_unfolded_lets (n : β„•) : βˆƒ v : β„• , v = some_lets 5 n := begin dunfold some_lets, -- admit end
5da7f1d99779b779689ae44534f0aa0b60f24838
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/tactic30.lean
e2f76ad7483cd84d91725f6e90464cf042f7be67
[ "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
276
lean
import logic open tactic section set_option pp.universes true set_option pp.implicit true variable {A : Type} variables {a b : A} variable H : a = b include H theorem test : a = b ∧ a = a := by apply and.intro; assumption; apply eq.refl end check @test
763ac7c7506e5728e1b028da6ec6fbbf81c73f2a
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/multiset/finset_ops.lean
a8cfe66a1bad0b9364c323895451418469a2b62d
[]
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
10,177
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.multiset.erase_dup import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Preparations for defining operations on `finset`. The operations here ignore multiplicities, and preparatory for defining the corresponding operations on `finset`. -/ namespace multiset /-! ### finset insert -/ /-- `ndinsert a s` is the lift of the list `insert` operation. This operation does not respect multiplicities, unlike `cons`, but it is suitable as an insert operation on `finset`. -/ def ndinsert {Ξ± : Type u_1} [DecidableEq Ξ±] (a : Ξ±) (s : multiset Ξ±) : multiset Ξ± := quot.lift_on s (fun (l : List Ξ±) => ↑(list.insert a l)) sorry @[simp] theorem coe_ndinsert {Ξ± : Type u_1} [DecidableEq Ξ±] (a : Ξ±) (l : List Ξ±) : ndinsert a ↑l = ↑(insert a l) := rfl @[simp] theorem ndinsert_zero {Ξ± : Type u_1} [DecidableEq Ξ±] (a : Ξ±) : ndinsert a 0 = a ::β‚˜ 0 := rfl @[simp] theorem ndinsert_of_mem {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} {s : multiset Ξ±} : a ∈ s β†’ ndinsert a s = s := quot.induction_on s fun (l : List Ξ±) (h : a ∈ Quot.mk setoid.r l) => congr_arg coe (list.insert_of_mem h) @[simp] theorem ndinsert_of_not_mem {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} {s : multiset Ξ±} : Β¬a ∈ s β†’ ndinsert a s = a ::β‚˜ s := quot.induction_on s fun (l : List Ξ±) (h : Β¬a ∈ Quot.mk setoid.r l) => congr_arg coe (list.insert_of_not_mem h) @[simp] theorem mem_ndinsert {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} {b : Ξ±} {s : multiset Ξ±} : a ∈ ndinsert b s ↔ a = b ∨ a ∈ s := quot.induction_on s fun (l : List Ξ±) => list.mem_insert_iff @[simp] theorem le_ndinsert_self {Ξ± : Type u_1} [DecidableEq Ξ±] (a : Ξ±) (s : multiset Ξ±) : s ≀ ndinsert a s := quot.induction_on s fun (l : List Ξ±) => list.sublist.subperm (list.sublist_of_suffix (list.suffix_insert a l)) @[simp] theorem mem_ndinsert_self {Ξ± : Type u_1} [DecidableEq Ξ±] (a : Ξ±) (s : multiset Ξ±) : a ∈ ndinsert a s := iff.mpr mem_ndinsert (Or.inl rfl) theorem mem_ndinsert_of_mem {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} {b : Ξ±} {s : multiset Ξ±} (h : a ∈ s) : a ∈ ndinsert b s := iff.mpr mem_ndinsert (Or.inr h) @[simp] theorem length_ndinsert_of_mem {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} {s : multiset Ξ±} (h : a ∈ s) : coe_fn card (ndinsert a s) = coe_fn card s := sorry @[simp] theorem length_ndinsert_of_not_mem {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} {s : multiset Ξ±} (h : Β¬a ∈ s) : coe_fn card (ndinsert a s) = coe_fn card s + 1 := sorry theorem erase_dup_cons {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} {s : multiset Ξ±} : erase_dup (a ::β‚˜ s) = ndinsert a (erase_dup s) := sorry theorem nodup_ndinsert {Ξ± : Type u_1} [DecidableEq Ξ±] (a : Ξ±) {s : multiset Ξ±} : nodup s β†’ nodup (ndinsert a s) := quot.induction_on s fun (l : List Ξ±) => list.nodup_insert theorem ndinsert_le {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} {s : multiset Ξ±} {t : multiset Ξ±} : ndinsert a s ≀ t ↔ s ≀ t ∧ a ∈ t := sorry theorem attach_ndinsert {Ξ± : Type u_1} [DecidableEq Ξ±] (a : Ξ±) (s : multiset Ξ±) : attach (ndinsert a s) = ndinsert { val := a, property := mem_ndinsert_self a s } (map (fun (p : Subtype fun (x : Ξ±) => x ∈ s) => { val := subtype.val p, property := mem_ndinsert_of_mem (subtype.property p) }) (attach s)) := sorry @[simp] theorem disjoint_ndinsert_left {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} {s : multiset Ξ±} {t : multiset Ξ±} : disjoint (ndinsert a s) t ↔ Β¬a ∈ t ∧ disjoint s t := sorry @[simp] theorem disjoint_ndinsert_right {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} {s : multiset Ξ±} {t : multiset Ξ±} : disjoint s (ndinsert a t) ↔ Β¬a ∈ s ∧ disjoint s t := sorry /-! ### finset union -/ /-- `ndunion s t` is the lift of the list `union` operation. This operation does not respect multiplicities, unlike `s βˆͺ t`, but it is suitable as a union operation on `finset`. (`s βˆͺ t` would also work as a union operation on finset, but this is more efficient.) -/ def ndunion {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) (t : multiset Ξ±) : multiset Ξ± := quotient.lift_onβ‚‚ s t (fun (l₁ lβ‚‚ : List Ξ±) => ↑(list.union l₁ lβ‚‚)) sorry @[simp] theorem coe_ndunion {Ξ± : Type u_1} [DecidableEq Ξ±] (l₁ : List Ξ±) (lβ‚‚ : List Ξ±) : ndunion ↑l₁ ↑lβ‚‚ = ↑(l₁ βˆͺ lβ‚‚) := rfl @[simp] theorem zero_ndunion {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) : ndunion 0 s = s := quot.induction_on s fun (l : List Ξ±) => rfl @[simp] theorem cons_ndunion {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) (t : multiset Ξ±) (a : Ξ±) : ndunion (a ::β‚˜ s) t = ndinsert a (ndunion s t) := quotient.induction_onβ‚‚ s t fun (l₁ lβ‚‚ : List Ξ±) => rfl @[simp] theorem mem_ndunion {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} {t : multiset Ξ±} {a : Ξ±} : a ∈ ndunion s t ↔ a ∈ s ∨ a ∈ t := quotient.induction_onβ‚‚ s t fun (l₁ lβ‚‚ : List Ξ±) => list.mem_union theorem le_ndunion_right {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) (t : multiset Ξ±) : t ≀ ndunion s t := quotient.induction_onβ‚‚ s t fun (l₁ lβ‚‚ : List Ξ±) => list.sublist.subperm (list.sublist_of_suffix (list.suffix_union_right l₁ lβ‚‚)) theorem subset_ndunion_right {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) (t : multiset Ξ±) : t βŠ† ndunion s t := subset_of_le (le_ndunion_right s t) theorem ndunion_le_add {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) (t : multiset Ξ±) : ndunion s t ≀ s + t := quotient.induction_onβ‚‚ s t fun (l₁ lβ‚‚ : List Ξ±) => list.sublist.subperm (list.union_sublist_append l₁ lβ‚‚) theorem ndunion_le {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} {t : multiset Ξ±} {u : multiset Ξ±} : ndunion s t ≀ u ↔ s βŠ† u ∧ t ≀ u := sorry theorem subset_ndunion_left {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) (t : multiset Ξ±) : s βŠ† ndunion s t := fun (a : Ξ±) (h : a ∈ s) => iff.mpr mem_ndunion (Or.inl h) theorem le_ndunion_left {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} (t : multiset Ξ±) (d : nodup s) : s ≀ ndunion s t := iff.mpr (le_iff_subset d) (subset_ndunion_left s t) theorem ndunion_le_union {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) (t : multiset Ξ±) : ndunion s t ≀ s βˆͺ t := iff.mpr ndunion_le { left := subset_of_le (le_union_left s t), right := le_union_right s t } theorem nodup_ndunion {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) {t : multiset Ξ±} : nodup t β†’ nodup (ndunion s t) := quotient.induction_onβ‚‚ s t fun (l₁ lβ‚‚ : List Ξ±) => list.nodup_union l₁ @[simp] theorem ndunion_eq_union {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} {t : multiset Ξ±} (d : nodup s) : ndunion s t = s βˆͺ t := le_antisymm (ndunion_le_union s t) (union_le (le_ndunion_left t d) (le_ndunion_right s t)) theorem erase_dup_add {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) (t : multiset Ξ±) : erase_dup (s + t) = ndunion s (erase_dup t) := quotient.induction_onβ‚‚ s t fun (l₁ lβ‚‚ : List Ξ±) => congr_arg coe (list.erase_dup_append l₁ lβ‚‚) /-! ### finset inter -/ /-- `ndinter s t` is the lift of the list `∩` operation. This operation does not respect multiplicities, unlike `s ∩ t`, but it is suitable as an intersection operation on `finset`. (`s ∩ t` would also work as a union operation on finset, but this is more efficient.) -/ def ndinter {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) (t : multiset Ξ±) : multiset Ξ± := filter (fun (_x : Ξ±) => _x ∈ t) s @[simp] theorem coe_ndinter {Ξ± : Type u_1} [DecidableEq Ξ±] (l₁ : List Ξ±) (lβ‚‚ : List Ξ±) : ndinter ↑l₁ ↑lβ‚‚ = ↑(l₁ ∩ lβ‚‚) := rfl @[simp] theorem zero_ndinter {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) : ndinter 0 s = 0 := rfl @[simp] theorem cons_ndinter_of_mem {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} (s : multiset Ξ±) {t : multiset Ξ±} (h : a ∈ t) : ndinter (a ::β‚˜ s) t = a ::β‚˜ ndinter s t := sorry @[simp] theorem ndinter_cons_of_not_mem {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} (s : multiset Ξ±) {t : multiset Ξ±} (h : Β¬a ∈ t) : ndinter (a ::β‚˜ s) t = ndinter s t := sorry @[simp] theorem mem_ndinter {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} {t : multiset Ξ±} {a : Ξ±} : a ∈ ndinter s t ↔ a ∈ s ∧ a ∈ t := mem_filter @[simp] theorem nodup_ndinter {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} (t : multiset Ξ±) : nodup s β†’ nodup (ndinter s t) := nodup_filter fun (_x : Ξ±) => _x ∈ t theorem le_ndinter {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} {t : multiset Ξ±} {u : multiset Ξ±} : s ≀ ndinter t u ↔ s ≀ t ∧ s βŠ† u := sorry theorem ndinter_le_left {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) (t : multiset Ξ±) : ndinter s t ≀ s := and.left (iff.mp le_ndinter (le_refl (ndinter s t))) theorem ndinter_subset_left {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) (t : multiset Ξ±) : ndinter s t βŠ† s := subset_of_le (ndinter_le_left s t) theorem ndinter_subset_right {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) (t : multiset Ξ±) : ndinter s t βŠ† t := and.right (iff.mp le_ndinter (le_refl (ndinter s t))) theorem ndinter_le_right {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} (t : multiset Ξ±) (d : nodup s) : ndinter s t ≀ t := iff.mpr (le_iff_subset (nodup_ndinter t d)) (ndinter_subset_right s t) theorem inter_le_ndinter {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) (t : multiset Ξ±) : s ∩ t ≀ ndinter s t := iff.mpr le_ndinter { left := inter_le_left s t, right := subset_of_le (inter_le_right s t) } @[simp] theorem ndinter_eq_inter {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} {t : multiset Ξ±} (d : nodup s) : ndinter s t = s ∩ t := le_antisymm (le_inter (ndinter_le_left s t) (ndinter_le_right t d)) (inter_le_ndinter s t) theorem ndinter_eq_zero_iff_disjoint {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} {t : multiset Ξ±} : ndinter s t = 0 ↔ disjoint s t := sorry
a7ecca6622094192e83f909e2c00e84b8972c384
94e33a31faa76775069b071adea97e86e218a8ee
/src/topology/continuous_on.lean
33faa2bdbcd2ec601fa65675e3c121ee44d6e65d
[ "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
54,388
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: SΓ©bastien GouΓ«zel -/ import topology.constructions /-! # Neighborhoods and continuity relative to a subset This file defines relative versions * `nhds_within` of `nhds` * `continuous_on` of `continuous` * `continuous_within_at` of `continuous_at` and proves their basic properties, including the relationships between these restricted notions and the corresponding notions for the subtype equipped with the subspace topology. ## Notation * `𝓝 x`: the filter of neighborhoods of a point `x`; * `π“Ÿ s`: the principal filter of a set `s`; * `𝓝[s] x`: the filter `nhds_within x s` of neighborhoods of a point `x` within a set `s`. -/ open set filter function open_locale topological_space filter variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} {Ξ΄ : Type*} variables [topological_space Ξ±] @[simp] lemma nhds_bind_nhds_within {a : Ξ±} {s : set Ξ±} : (𝓝 a).bind (Ξ» x, 𝓝[s] x) = 𝓝[s] a := bind_inf_principal.trans $ congr_arg2 _ nhds_bind_nhds rfl @[simp] lemma eventually_nhds_nhds_within {a : Ξ±} {s : set Ξ±} {p : Ξ± β†’ Prop} : (βˆ€αΆ  y in 𝓝 a, βˆ€αΆ  x in 𝓝[s] y, p x) ↔ βˆ€αΆ  x in 𝓝[s] a, p x := filter.ext_iff.1 nhds_bind_nhds_within {x | p x} lemma eventually_nhds_within_iff {a : Ξ±} {s : set Ξ±} {p : Ξ± β†’ Prop} : (βˆ€αΆ  x in 𝓝[s] a, p x) ↔ βˆ€αΆ  x in 𝓝 a, x ∈ s β†’ p x := eventually_inf_principal @[simp] lemma eventually_nhds_within_nhds_within {a : Ξ±} {s : set Ξ±} {p : Ξ± β†’ Prop} : (βˆ€αΆ  y in 𝓝[s] a, βˆ€αΆ  x in 𝓝[s] y, p x) ↔ βˆ€αΆ  x in 𝓝[s] a, p x := begin refine ⟨λ h, _, Ξ» h, (eventually_nhds_nhds_within.2 h).filter_mono inf_le_left⟩, simp only [eventually_nhds_within_iff] at h ⊒, exact h.mono (Ξ» x hx hxs, (hx hxs).self_of_nhds hxs) end theorem nhds_within_eq (a : Ξ±) (s : set Ξ±) : 𝓝[s] a = β¨… t ∈ {t : set Ξ± | a ∈ t ∧ is_open t}, π“Ÿ (t ∩ s) := ((nhds_basis_opens a).inf_principal s).eq_binfi theorem nhds_within_univ (a : Ξ±) : 𝓝[set.univ] a = 𝓝 a := by rw [nhds_within, principal_univ, inf_top_eq] lemma nhds_within_has_basis {p : Ξ² β†’ Prop} {s : Ξ² β†’ set Ξ±} {a : Ξ±} (h : (𝓝 a).has_basis p s) (t : set Ξ±) : (𝓝[t] a).has_basis p (Ξ» i, s i ∩ t) := h.inf_principal t lemma nhds_within_basis_open (a : Ξ±) (t : set Ξ±) : (𝓝[t] a).has_basis (Ξ» u, a ∈ u ∧ is_open u) (Ξ» u, u ∩ t) := nhds_within_has_basis (nhds_basis_opens a) t theorem mem_nhds_within {t : set Ξ±} {a : Ξ±} {s : set Ξ±} : t ∈ 𝓝[s] a ↔ βˆƒ u, is_open u ∧ a ∈ u ∧ u ∩ s βŠ† t := by simpa only [exists_prop, and_assoc, and_comm] using (nhds_within_basis_open a s).mem_iff lemma mem_nhds_within_iff_exists_mem_nhds_inter {t : set Ξ±} {a : Ξ±} {s : set Ξ±} : t ∈ 𝓝[s] a ↔ βˆƒ u ∈ 𝓝 a, u ∩ s βŠ† t := (nhds_within_has_basis (𝓝 a).basis_sets s).mem_iff lemma diff_mem_nhds_within_compl {x : Ξ±} {s : set Ξ±} (hs : s ∈ 𝓝 x) (t : set Ξ±) : s \ t ∈ 𝓝[tᢜ] x := diff_mem_inf_principal_compl hs t lemma diff_mem_nhds_within_diff {x : Ξ±} {s t : set Ξ±} (hs : s ∈ 𝓝[t] x) (t' : set Ξ±) : s \ t' ∈ 𝓝[t \ t'] x := begin rw [nhds_within, diff_eq, diff_eq, ← inf_principal, ← inf_assoc], exact inter_mem_inf hs (mem_principal_self _) end lemma nhds_of_nhds_within_of_nhds {s t : set Ξ±} {a : Ξ±} (h1 : s ∈ 𝓝 a) (h2 : t ∈ 𝓝[s] a) : (t ∈ 𝓝 a) := begin rcases mem_nhds_within_iff_exists_mem_nhds_inter.mp h2 with ⟨_, Hw, hw⟩, exact (nhds a).sets_of_superset ((nhds a).inter_sets Hw h1) hw, end lemma mem_nhds_within_iff_eventually {s t : set Ξ±} {x : Ξ±} : t ∈ 𝓝[s] x ↔ βˆ€αΆ  y in 𝓝 x, y ∈ s β†’ y ∈ t := begin rw [mem_nhds_within_iff_exists_mem_nhds_inter], split, { rintro ⟨u, hu, hut⟩, exact eventually_of_mem hu (Ξ» x hxu hxs, hut ⟨hxu, hxs⟩) }, { refine Ξ» h, ⟨_, h, Ξ» y hy, hy.1 hy.2⟩ } end lemma mem_nhds_within_iff_eventually_eq {s t : set Ξ±} {x : Ξ±} : t ∈ 𝓝[s] x ↔ s =αΆ [𝓝 x] (s ∩ t : set Ξ±) := by simp_rw [mem_nhds_within_iff_eventually, eventually_eq_set, mem_inter_iff, iff_self_and] lemma nhds_within_eq_iff_eventually_eq {s t : set Ξ±} {x : Ξ±} : 𝓝[s] x = 𝓝[t] x ↔ s =αΆ [𝓝 x] t := begin simp_rw [filter.ext_iff, mem_nhds_within_iff_eventually, eventually_eq_set], split, { intro h, filter_upwards [(h t).mpr (eventually_of_forall $ Ξ» x, id), (h s).mp (eventually_of_forall $ Ξ» x, id)], exact Ξ» x, iff.intro, }, { refine Ξ» h u, eventually_congr (h.mono $ Ξ» x h, _), rw [h] } end lemma nhds_within_le_iff {s t : set Ξ±} {x : Ξ±} : 𝓝[s] x ≀ 𝓝[t] x ↔ t ∈ 𝓝[s] x := begin simp_rw [filter.le_def, mem_nhds_within_iff_eventually], split, { exact Ξ» h, (h t $ eventually_of_forall (Ξ» x, id)).mono (Ξ» x, id) }, { exact Ξ» h u hu, (h.and hu).mono (Ξ» x hx h, hx.2 $ hx.1 h) } end lemma preimage_nhds_within_coinduced' {Ο€ : Ξ± β†’ Ξ²} {s : set Ξ²} {t : set Ξ±} {a : Ξ±} (h : a ∈ t) (ht : is_open t) (hs : s ∈ @nhds Ξ² (topological_space.coinduced (Ξ» x : t, Ο€ x) subtype.topological_space) (Ο€ a)) : Ο€ ⁻¹' s ∈ 𝓝[t] a := begin letI := topological_space.coinduced (Ξ» x : t, Ο€ x) subtype.topological_space, rcases mem_nhds_iff.mp hs with ⟨V, hVs, V_op, mem_V⟩, refine mem_nhds_within_iff_exists_mem_nhds_inter.mpr βŸ¨Ο€ ⁻¹' V, mem_nhds_iff.mpr ⟨t ∩ Ο€ ⁻¹' V, inter_subset_right t (Ο€ ⁻¹' V), _, mem_sep h mem_V⟩, subset.trans (inter_subset_left _ _) (preimage_mono hVs)⟩, obtain ⟨u, hu1, hu2⟩ := is_open_induced_iff.mp (is_open_coinduced.1 V_op), rw [preimage_comp] at hu2, rw [set.inter_comm, ←(subtype.preimage_coe_eq_preimage_coe_iff.mp hu2)], exact hu1.inter ht, end lemma mem_nhds_within_of_mem_nhds {s t : set Ξ±} {a : Ξ±} (h : s ∈ 𝓝 a) : s ∈ 𝓝[t] a := mem_inf_of_left h theorem self_mem_nhds_within {a : Ξ±} {s : set Ξ±} : s ∈ 𝓝[s] a := mem_inf_of_right (mem_principal_self s) theorem eventually_mem_nhds_within {a : Ξ±} {s : set Ξ±} : βˆ€αΆ  x in 𝓝[s] a, x ∈ s := self_mem_nhds_within theorem inter_mem_nhds_within (s : set Ξ±) {t : set Ξ±} {a : Ξ±} (h : t ∈ 𝓝 a) : s ∩ t ∈ 𝓝[s] a := inter_mem self_mem_nhds_within (mem_inf_of_left h) theorem nhds_within_mono (a : Ξ±) {s t : set Ξ±} (h : s βŠ† t) : 𝓝[s] a ≀ 𝓝[t] a := inf_le_inf_left _ (principal_mono.mpr h) lemma pure_le_nhds_within {a : Ξ±} {s : set Ξ±} (ha : a ∈ s) : pure a ≀ 𝓝[s] a := le_inf (pure_le_nhds a) (le_principal_iff.2 ha) lemma mem_of_mem_nhds_within {a : Ξ±} {s t : set Ξ±} (ha : a ∈ s) (ht : t ∈ 𝓝[s] a) : a ∈ t := pure_le_nhds_within ha ht lemma filter.eventually.self_of_nhds_within {p : Ξ± β†’ Prop} {s : set Ξ±} {x : Ξ±} (h : βˆ€αΆ  y in 𝓝[s] x, p y) (hx : x ∈ s) : p x := mem_of_mem_nhds_within hx h lemma tendsto_const_nhds_within {l : filter Ξ²} {s : set Ξ±} {a : Ξ±} (ha : a ∈ s) : tendsto (Ξ» x : Ξ², a) l (𝓝[s] a) := tendsto_const_pure.mono_right $ pure_le_nhds_within ha theorem nhds_within_restrict'' {a : Ξ±} (s : set Ξ±) {t : set Ξ±} (h : t ∈ 𝓝[s] a) : 𝓝[s] a = 𝓝[s ∩ t] a := le_antisymm (le_inf inf_le_left (le_principal_iff.mpr (inter_mem self_mem_nhds_within h))) (inf_le_inf_left _ (principal_mono.mpr (set.inter_subset_left _ _))) theorem nhds_within_restrict' {a : Ξ±} (s : set Ξ±) {t : set Ξ±} (h : t ∈ 𝓝 a) : 𝓝[s] a = 𝓝[s ∩ t] a := nhds_within_restrict'' s $ mem_inf_of_left h theorem nhds_within_restrict {a : Ξ±} (s : set Ξ±) {t : set Ξ±} (hβ‚€ : a ∈ t) (h₁ : is_open t) : 𝓝[s] a = 𝓝[s ∩ t] a := nhds_within_restrict' s (is_open.mem_nhds h₁ hβ‚€) theorem nhds_within_le_of_mem {a : Ξ±} {s t : set Ξ±} (h : s ∈ 𝓝[t] a) : 𝓝[t] a ≀ 𝓝[s] a := nhds_within_le_iff.mpr h theorem nhds_within_le_nhds {a : Ξ±} {s : set Ξ±} : 𝓝[s] a ≀ 𝓝 a := by { rw ← nhds_within_univ, apply nhds_within_le_of_mem, exact univ_mem } lemma nhds_within_eq_nhds_within' {a : Ξ±} {s t u : set Ξ±} (hs : s ∈ 𝓝 a) (hβ‚‚ : t ∩ s = u ∩ s) : 𝓝[t] a = 𝓝[u] a := by rw [nhds_within_restrict' t hs, nhds_within_restrict' u hs, hβ‚‚] theorem nhds_within_eq_nhds_within {a : Ξ±} {s t u : set Ξ±} (hβ‚€ : a ∈ s) (h₁ : is_open s) (hβ‚‚ : t ∩ s = u ∩ s) : 𝓝[t] a = 𝓝[u] a := by rw [nhds_within_restrict t hβ‚€ h₁, nhds_within_restrict u hβ‚€ h₁, hβ‚‚] theorem is_open.nhds_within_eq {a : Ξ±} {s : set Ξ±} (h : is_open s) (ha : a ∈ s) : 𝓝[s] a = 𝓝 a := inf_eq_left.2 $ le_principal_iff.2 $ is_open.mem_nhds h ha lemma preimage_nhds_within_coinduced {Ο€ : Ξ± β†’ Ξ²} {s : set Ξ²} {t : set Ξ±} {a : Ξ±} (h : a ∈ t) (ht : is_open t) (hs : s ∈ @nhds Ξ² (topological_space.coinduced (Ξ» x : t, Ο€ x) subtype.topological_space) (Ο€ a)) : Ο€ ⁻¹' s ∈ 𝓝 a := by { rw ← ht.nhds_within_eq h, exact preimage_nhds_within_coinduced' h ht hs } @[simp] theorem nhds_within_empty (a : Ξ±) : 𝓝[βˆ…] a = βŠ₯ := by rw [nhds_within, principal_empty, inf_bot_eq] theorem nhds_within_union (a : Ξ±) (s t : set Ξ±) : 𝓝[s βˆͺ t] a = 𝓝[s] a βŠ” 𝓝[t] a := by { delta nhds_within, rw [←inf_sup_left, sup_principal] } theorem nhds_within_inter (a : Ξ±) (s t : set Ξ±) : 𝓝[s ∩ t] a = 𝓝[s] a βŠ“ 𝓝[t] a := by { delta nhds_within, rw [inf_left_comm, inf_assoc, inf_principal, ←inf_assoc, inf_idem] } theorem nhds_within_inter' (a : Ξ±) (s t : set Ξ±) : 𝓝[s ∩ t] a = (𝓝[s] a) βŠ“ π“Ÿ t := by { delta nhds_within, rw [←inf_principal, inf_assoc] } theorem nhds_within_inter_of_mem {a : Ξ±} {s t : set Ξ±} (h : s ∈ 𝓝[t] a) : 𝓝[s ∩ t] a = 𝓝[t] a := by { rw [nhds_within_inter, inf_eq_right], exact nhds_within_le_of_mem h } @[simp] theorem nhds_within_singleton (a : Ξ±) : 𝓝[{a}] a = pure a := by rw [nhds_within, principal_singleton, inf_eq_right.2 (pure_le_nhds a)] @[simp] theorem nhds_within_insert (a : Ξ±) (s : set Ξ±) : 𝓝[insert a s] a = pure a βŠ” 𝓝[s] a := by rw [← singleton_union, nhds_within_union, nhds_within_singleton] lemma mem_nhds_within_insert {a : Ξ±} {s t : set Ξ±} : t ∈ 𝓝[insert a s] a ↔ a ∈ t ∧ t ∈ 𝓝[s] a := by simp lemma insert_mem_nhds_within_insert {a : Ξ±} {s t : set Ξ±} (h : t ∈ 𝓝[s] a) : insert a t ∈ 𝓝[insert a s] a := by simp [mem_of_superset h] lemma insert_mem_nhds_iff {a : Ξ±} {s : set Ξ±} : insert a s ∈ 𝓝 a ↔ s ∈ 𝓝[β‰ ] a := by simp only [nhds_within, mem_inf_principal, mem_compl_iff, mem_singleton_iff, or_iff_not_imp_left, insert_def] @[simp] theorem nhds_within_compl_singleton_sup_pure (a : Ξ±) : 𝓝[β‰ ] a βŠ” pure a = 𝓝 a := by rw [← nhds_within_singleton, ← nhds_within_union, compl_union_self, nhds_within_univ] lemma nhds_within_prod_eq {Ξ± : Type*} [topological_space Ξ±] {Ξ² : Type*} [topological_space Ξ²] (a : Ξ±) (b : Ξ²) (s : set Ξ±) (t : set Ξ²) : 𝓝[s Γ—Λ’ t] (a, b) = 𝓝[s] a Γ—αΆ  𝓝[t] b := by { delta nhds_within, rw [nhds_prod_eq, ←filter.prod_inf_prod, filter.prod_principal_principal] } lemma nhds_within_prod {Ξ± : Type*} [topological_space Ξ±] {Ξ² : Type*} [topological_space Ξ²] {s u : set Ξ±} {t v : set Ξ²} {a : Ξ±} {b : Ξ²} (hu : u ∈ 𝓝[s] a) (hv : v ∈ 𝓝[t] b) : (u Γ—Λ’ v) ∈ 𝓝[s Γ—Λ’ t] (a, b) := by { rw nhds_within_prod_eq, exact prod_mem_prod hu hv, } lemma nhds_within_pi_eq' {ΞΉ : Type*} {Ξ± : ΞΉ β†’ Type*} [Ξ  i, topological_space (Ξ± i)] {I : set ΞΉ} (hI : I.finite) (s : Ξ  i, set (Ξ± i)) (x : Ξ  i, Ξ± i) : 𝓝[pi I s] x = β¨… i, comap (Ξ» x, x i) (𝓝 (x i) βŠ“ β¨… (hi : i ∈ I), π“Ÿ (s i)) := by simp only [nhds_within, nhds_pi, filter.pi, comap_inf, comap_infi, pi_def, comap_principal, ← infi_principal_finite hI, ← infi_inf_eq] lemma nhds_within_pi_eq {ΞΉ : Type*} {Ξ± : ΞΉ β†’ Type*} [Ξ  i, topological_space (Ξ± i)] {I : set ΞΉ} (hI : I.finite) (s : Ξ  i, set (Ξ± i)) (x : Ξ  i, Ξ± i) : 𝓝[pi I s] x = (β¨… i ∈ I, comap (Ξ» x, x i) (𝓝[s i] (x i))) βŠ“ β¨… (i βˆ‰ I), comap (Ξ» x, x i) (𝓝 (x i)) := begin simp only [nhds_within, nhds_pi, filter.pi, pi_def, ← infi_principal_finite hI, comap_inf, comap_principal, eval], rw [infi_split _ (Ξ» i, i ∈ I), inf_right_comm], simp only [infi_inf_eq] end lemma nhds_within_pi_univ_eq {ΞΉ : Type*} {Ξ± : ΞΉ β†’ Type*} [fintype ΞΉ] [Ξ  i, topological_space (Ξ± i)] (s : Ξ  i, set (Ξ± i)) (x : Ξ  i, Ξ± i) : 𝓝[pi univ s] x = β¨… i, comap (Ξ» x, x i) 𝓝[s i] (x i) := by simpa [nhds_within] using nhds_within_pi_eq finite_univ s x lemma nhds_within_pi_eq_bot {ΞΉ : Type*} {Ξ± : ΞΉ β†’ Type*} [Ξ  i, topological_space (Ξ± i)] {I : set ΞΉ} {s : Ξ  i, set (Ξ± i)} {x : Ξ  i, Ξ± i} : 𝓝[pi I s] x = βŠ₯ ↔ βˆƒ i ∈ I, 𝓝[s i] (x i) = βŠ₯ := by simp only [nhds_within, nhds_pi, pi_inf_principal_pi_eq_bot] lemma nhds_within_pi_ne_bot {ΞΉ : Type*} {Ξ± : ΞΉ β†’ Type*} [Ξ  i, topological_space (Ξ± i)] {I : set ΞΉ} {s : Ξ  i, set (Ξ± i)} {x : Ξ  i, Ξ± i} : (𝓝[pi I s] x).ne_bot ↔ βˆ€ i ∈ I, (𝓝[s i] (x i)).ne_bot := by simp [ne_bot_iff, nhds_within_pi_eq_bot] theorem filter.tendsto.piecewise_nhds_within {f g : Ξ± β†’ Ξ²} {t : set Ξ±} [βˆ€ x, decidable (x ∈ t)] {a : Ξ±} {s : set Ξ±} {l : filter Ξ²} (hβ‚€ : tendsto f (𝓝[s ∩ t] a) l) (h₁ : tendsto g (𝓝[s ∩ tᢜ] a) l) : tendsto (piecewise t f g) (𝓝[s] a) l := by apply tendsto.piecewise; rwa ← nhds_within_inter' theorem filter.tendsto.if_nhds_within {f g : Ξ± β†’ Ξ²} {p : Ξ± β†’ Prop} [decidable_pred p] {a : Ξ±} {s : set Ξ±} {l : filter Ξ²} (hβ‚€ : tendsto f (𝓝[s ∩ {x | p x}] a) l) (h₁ : tendsto g (𝓝[s ∩ {x | Β¬ p x}] a) l) : tendsto (Ξ» x, if p x then f x else g x) (𝓝[s] a) l := hβ‚€.piecewise_nhds_within h₁ lemma map_nhds_within (f : Ξ± β†’ Ξ²) (a : Ξ±) (s : set Ξ±) : map f (𝓝[s] a) = β¨… t ∈ {t : set Ξ± | a ∈ t ∧ is_open t}, π“Ÿ (f '' (t ∩ s)) := ((nhds_within_basis_open a s).map f).eq_binfi theorem tendsto_nhds_within_mono_left {f : Ξ± β†’ Ξ²} {a : Ξ±} {s t : set Ξ±} {l : filter Ξ²} (hst : s βŠ† t) (h : tendsto f (𝓝[t] a) l) : tendsto f (𝓝[s] a) l := h.mono_left $ nhds_within_mono a hst theorem tendsto_nhds_within_mono_right {f : Ξ² β†’ Ξ±} {l : filter Ξ²} {a : Ξ±} {s t : set Ξ±} (hst : s βŠ† t) (h : tendsto f l (𝓝[s] a)) : tendsto f l (𝓝[t] a) := h.mono_right (nhds_within_mono a hst) theorem tendsto_nhds_within_of_tendsto_nhds {f : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} {l : filter Ξ²} (h : tendsto f (𝓝 a) l) : tendsto f (𝓝[s] a) l := h.mono_left inf_le_left theorem principal_subtype {Ξ± : Type*} (s : set Ξ±) (t : set {x // x ∈ s}) : π“Ÿ t = comap coe (π“Ÿ ((coe : s β†’ Ξ±) '' t)) := by rw [comap_principal, set.preimage_image_eq _ subtype.coe_injective] lemma nhds_within_ne_bot_of_mem {s : set Ξ±} {x : Ξ±} (hx : x ∈ s) : ne_bot (𝓝[s] x) := mem_closure_iff_nhds_within_ne_bot.1 $ subset_closure hx lemma is_closed.mem_of_nhds_within_ne_bot {s : set Ξ±} (hs : is_closed s) {x : Ξ±} (hx : ne_bot $ 𝓝[s] x) : x ∈ s := by simpa only [hs.closure_eq] using mem_closure_iff_nhds_within_ne_bot.2 hx lemma dense_range.nhds_within_ne_bot {ΞΉ : Type*} {f : ΞΉ β†’ Ξ±} (h : dense_range f) (x : Ξ±) : ne_bot (𝓝[range f] x) := mem_closure_iff_cluster_pt.1 (h x) lemma mem_closure_pi {ΞΉ : Type*} {Ξ± : ΞΉ β†’ Type*} [Ξ  i, topological_space (Ξ± i)] {I : set ΞΉ} {s : Ξ  i, set (Ξ± i)} {x : Ξ  i, Ξ± i} : x ∈ closure (pi I s) ↔ βˆ€ i ∈ I, x i ∈ closure (s i) := by simp only [mem_closure_iff_nhds_within_ne_bot, nhds_within_pi_ne_bot] lemma closure_pi_set {ΞΉ : Type*} {Ξ± : ΞΉ β†’ Type*} [Ξ  i, topological_space (Ξ± i)] (I : set ΞΉ) (s : Ξ  i, set (Ξ± i)) : closure (pi I s) = pi I (Ξ» i, closure (s i)) := set.ext $ Ξ» x, mem_closure_pi lemma dense_pi {ΞΉ : Type*} {Ξ± : ΞΉ β†’ Type*} [Ξ  i, topological_space (Ξ± i)] {s : Ξ  i, set (Ξ± i)} (I : set ΞΉ) (hs : βˆ€ i ∈ I, dense (s i)) : dense (pi I s) := by simp only [dense_iff_closure_eq, closure_pi_set, pi_congr rfl (Ξ» i hi, (hs i hi).closure_eq), pi_univ] lemma eventually_eq_nhds_within_iff {f g : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} : (f =αΆ [𝓝[s] a] g) ↔ βˆ€αΆ  x in 𝓝 a, x ∈ s β†’ f x = g x := mem_inf_principal lemma eventually_eq_nhds_within_of_eq_on {f g : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h : eq_on f g s) : f =αΆ [𝓝[s] a] g := mem_inf_of_right h lemma set.eq_on.eventually_eq_nhds_within {f g : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h : eq_on f g s) : f =αΆ [𝓝[s] a] g := eventually_eq_nhds_within_of_eq_on h lemma tendsto_nhds_within_congr {f g : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} {l : filter Ξ²} (hfg : βˆ€ x ∈ s, f x = g x) (hf : tendsto f (𝓝[s] a) l) : tendsto g (𝓝[s] a) l := (tendsto_congr' $ eventually_eq_nhds_within_of_eq_on hfg).1 hf lemma eventually_nhds_within_of_forall {s : set Ξ±} {a : Ξ±} {p : Ξ± β†’ Prop} (h : βˆ€ x ∈ s, p x) : βˆ€αΆ  x in 𝓝[s] a, p x := mem_inf_of_right h lemma tendsto_nhds_within_of_tendsto_nhds_of_eventually_within {a : Ξ±} {l : filter Ξ²} {s : set Ξ±} (f : Ξ² β†’ Ξ±) (h1 : tendsto f l (𝓝 a)) (h2 : βˆ€αΆ  x in l, f x ∈ s) : tendsto f l (𝓝[s] a) := tendsto_inf.2 ⟨h1, tendsto_principal.2 h2⟩ @[simp] lemma tendsto_nhds_within_range {a : Ξ±} {l : filter Ξ²} {f : Ξ² β†’ Ξ±} : tendsto f l (𝓝[range f] a) ↔ tendsto f l (𝓝 a) := ⟨λ h, h.mono_right inf_le_left, Ξ» h, tendsto_inf.2 ⟨h, tendsto_principal.2 $ eventually_of_forall mem_range_self⟩⟩ lemma filter.eventually_eq.eq_of_nhds_within {s : set Ξ±} {f g : Ξ± β†’ Ξ²} {a : Ξ±} (h : f =αΆ [𝓝[s] a] g) (hmem : a ∈ s) : f a = g a := h.self_of_nhds_within hmem lemma eventually_nhds_within_of_eventually_nhds {Ξ± : Type*} [topological_space Ξ±] {s : set Ξ±} {a : Ξ±} {p : Ξ± β†’ Prop} (h : βˆ€αΆ  x in 𝓝 a, p x) : βˆ€αΆ  x in 𝓝[s] a, p x := mem_nhds_within_of_mem_nhds h /-! ### `nhds_within` and subtypes -/ theorem mem_nhds_within_subtype {s : set Ξ±} {a : {x // x ∈ s}} {t u : set {x // x ∈ s}} : t ∈ 𝓝[u] a ↔ t ∈ comap (coe : s β†’ Ξ±) (𝓝[coe '' u] a) := by rw [nhds_within, nhds_subtype, principal_subtype, ←comap_inf, ←nhds_within] theorem nhds_within_subtype (s : set Ξ±) (a : {x // x ∈ s}) (t : set {x // x ∈ s}) : 𝓝[t] a = comap (coe : s β†’ Ξ±) (𝓝[coe '' t] a) := filter.ext $ Ξ» u, mem_nhds_within_subtype theorem nhds_within_eq_map_subtype_coe {s : set Ξ±} {a : Ξ±} (h : a ∈ s) : 𝓝[s] a = map (coe : s β†’ Ξ±) (𝓝 ⟨a, h⟩) := by simpa only [subtype.range_coe] using (embedding_subtype_coe.map_nhds_eq ⟨a, h⟩).symm theorem mem_nhds_subtype_iff_nhds_within {s : set Ξ±} {a : s} {t : set s} : t ∈ 𝓝 a ↔ coe '' t ∈ 𝓝[s] (a : Ξ±) := by rw [nhds_within_eq_map_subtype_coe a.coe_prop, mem_map, preimage_image_eq _ subtype.coe_injective, subtype.coe_eta] theorem preimage_coe_mem_nhds_subtype {s t : set Ξ±} {a : s} : coe ⁻¹' t ∈ 𝓝 a ↔ t ∈ 𝓝[s] ↑a := by simp only [mem_nhds_subtype_iff_nhds_within, subtype.image_preimage_coe, inter_mem_iff, self_mem_nhds_within, and_true] theorem tendsto_nhds_within_iff_subtype {s : set Ξ±} {a : Ξ±} (h : a ∈ s) (f : Ξ± β†’ Ξ²) (l : filter Ξ²) : tendsto f (𝓝[s] a) l ↔ tendsto (s.restrict f) (𝓝 ⟨a, h⟩) l := by simp only [tendsto, nhds_within_eq_map_subtype_coe h, filter.map_map, restrict] variables [topological_space Ξ²] [topological_space Ξ³] [topological_space Ξ΄] /-- A function between topological spaces is continuous at a point `xβ‚€` within a subset `s` if `f x` tends to `f xβ‚€` when `x` tends to `xβ‚€` while staying within `s`. -/ def continuous_within_at (f : Ξ± β†’ Ξ²) (s : set Ξ±) (x : Ξ±) : Prop := tendsto f (𝓝[s] x) (𝓝 (f x)) /-- If a function is continuous within `s` at `x`, then it tends to `f x` within `s` by definition. We register this fact for use with the dot notation, especially to use `tendsto.comp` as `continuous_within_at.comp` will have a different meaning. -/ lemma continuous_within_at.tendsto {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : continuous_within_at f s x) : tendsto f (𝓝[s] x) (𝓝 (f x)) := h /-- A function between topological spaces is continuous on a subset `s` when it's continuous at every point of `s` within `s`. -/ def continuous_on (f : Ξ± β†’ Ξ²) (s : set Ξ±) : Prop := βˆ€ x ∈ s, continuous_within_at f s x lemma continuous_on.continuous_within_at {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (hf : continuous_on f s) (hx : x ∈ s) : continuous_within_at f s x := hf x hx theorem continuous_within_at_univ (f : Ξ± β†’ Ξ²) (x : Ξ±) : continuous_within_at f set.univ x ↔ continuous_at f x := by rw [continuous_at, continuous_within_at, nhds_within_univ] theorem continuous_within_at_iff_continuous_at_restrict (f : Ξ± β†’ Ξ²) {x : Ξ±} {s : set Ξ±} (h : x ∈ s) : continuous_within_at f s x ↔ continuous_at (s.restrict f) ⟨x, h⟩ := tendsto_nhds_within_iff_subtype h f _ theorem continuous_within_at.tendsto_nhds_within {f : Ξ± β†’ Ξ²} {x : Ξ±} {s : set Ξ±} {t : set Ξ²} (h : continuous_within_at f s x) (ht : maps_to f s t) : tendsto f (𝓝[s] x) (𝓝[t] (f x)) := tendsto_inf.2 ⟨h, tendsto_principal.2 $ mem_inf_of_right $ mem_principal.2 $ ht⟩ theorem continuous_within_at.tendsto_nhds_within_image {f : Ξ± β†’ Ξ²} {x : Ξ±} {s : set Ξ±} (h : continuous_within_at f s x) : tendsto f (𝓝[s] x) (𝓝[f '' s] (f x)) := h.tendsto_nhds_within (maps_to_image _ _) lemma continuous_within_at.prod_map {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ΄} {s : set Ξ±} {t : set Ξ²} {x : Ξ±} {y : Ξ²} (hf : continuous_within_at f s x) (hg : continuous_within_at g t y) : continuous_within_at (prod.map f g) (s Γ—Λ’ t) (x, y) := begin unfold continuous_within_at at *, rw [nhds_within_prod_eq, prod.map, nhds_prod_eq], exact hf.prod_map hg, end lemma continuous_within_at_pi {ΞΉ : Type*} {Ο€ : ΞΉ β†’ Type*} [βˆ€ i, topological_space (Ο€ i)] {f : Ξ± β†’ Ξ  i, Ο€ i} {s : set Ξ±} {x : Ξ±} : continuous_within_at f s x ↔ βˆ€ i, continuous_within_at (Ξ» y, f y i) s x := tendsto_pi_nhds lemma continuous_on_pi {ΞΉ : Type*} {Ο€ : ΞΉ β†’ Type*} [βˆ€ i, topological_space (Ο€ i)] {f : Ξ± β†’ Ξ  i, Ο€ i} {s : set Ξ±} : continuous_on f s ↔ βˆ€ i, continuous_on (Ξ» y, f y i) s := ⟨λ h i x hx, tendsto_pi_nhds.1 (h x hx) i, Ξ» h x hx, tendsto_pi_nhds.2 (Ξ» i, h i x hx)⟩ lemma continuous_within_at.fin_insert_nth {n} {Ο€ : fin (n + 1) β†’ Type*} [Ξ  i, topological_space (Ο€ i)] (i : fin (n + 1)) {f : Ξ± β†’ Ο€ i} {a : Ξ±} {s : set Ξ±} (hf : continuous_within_at f s a) {g : Ξ± β†’ Ξ  j : fin n, Ο€ (i.succ_above j)} (hg : continuous_within_at g s a) : continuous_within_at (Ξ» a, i.insert_nth (f a) (g a)) s a := hf.fin_insert_nth i hg lemma continuous_on.fin_insert_nth {n} {Ο€ : fin (n + 1) β†’ Type*} [Ξ  i, topological_space (Ο€ i)] (i : fin (n + 1)) {f : Ξ± β†’ Ο€ i} {s : set Ξ±} (hf : continuous_on f s) {g : Ξ± β†’ Ξ  j : fin n, Ο€ (i.succ_above j)} (hg : continuous_on g s) : continuous_on (Ξ» a, i.insert_nth (f a) (g a)) s := Ξ» a ha, (hf a ha).fin_insert_nth i (hg a ha) theorem continuous_on_iff {f : Ξ± β†’ Ξ²} {s : set Ξ±} : continuous_on f s ↔ βˆ€ x ∈ s, βˆ€ t : set Ξ², is_open t β†’ f x ∈ t β†’ βˆƒ u, is_open u ∧ x ∈ u ∧ u ∩ s βŠ† f ⁻¹' t := by simp only [continuous_on, continuous_within_at, tendsto_nhds, mem_nhds_within] theorem continuous_on_iff_continuous_restrict {f : Ξ± β†’ Ξ²} {s : set Ξ±} : continuous_on f s ↔ continuous (s.restrict f) := begin rw [continuous_on, continuous_iff_continuous_at], split, { rintros h ⟨x, xs⟩, exact (continuous_within_at_iff_continuous_at_restrict f xs).mp (h x xs) }, intros h x xs, exact (continuous_within_at_iff_continuous_at_restrict f xs).mpr (h ⟨x, xs⟩) end theorem continuous_on_iff' {f : Ξ± β†’ Ξ²} {s : set Ξ±} : continuous_on f s ↔ βˆ€ t : set Ξ², is_open t β†’ βˆƒ u, is_open u ∧ f ⁻¹' t ∩ s = u ∩ s := have βˆ€ t, is_open (s.restrict f ⁻¹' t) ↔ βˆƒ (u : set Ξ±), is_open u ∧ f ⁻¹' t ∩ s = u ∩ s, begin intro t, rw [is_open_induced_iff, set.restrict_eq, set.preimage_comp], simp only [subtype.preimage_coe_eq_preimage_coe_iff], split; { rintros ⟨u, ou, useq⟩, exact ⟨u, ou, useq.symm⟩ } end, by rw [continuous_on_iff_continuous_restrict, continuous_def]; simp only [this] /-- If a function is continuous on a set for some topologies, then it is continuous on the same set with respect to any finer topology on the source space. -/ lemma continuous_on.mono_dom {Ξ± Ξ² : Type*} {t₁ tβ‚‚ : topological_space Ξ±} {t₃ : topological_space Ξ²} (h₁ : tβ‚‚ ≀ t₁) {s : set Ξ±} {f : Ξ± β†’ Ξ²} (hβ‚‚ : @continuous_on Ξ± Ξ² t₁ t₃ f s) : @continuous_on Ξ± Ξ² tβ‚‚ t₃ f s := begin rw continuous_on_iff' at hβ‚‚ ⊒, assume t ht, rcases hβ‚‚ t ht with ⟨u, hu, h'u⟩, exact ⟨u, h₁ u hu, h'u⟩ end /-- If a function is continuous on a set for some topologies, then it is continuous on the same set with respect to any coarser topology on the target space. -/ lemma continuous_on.mono_rng {Ξ± Ξ² : Type*} {t₁ : topological_space Ξ±} {tβ‚‚ t₃ : topological_space Ξ²} (h₁ : tβ‚‚ ≀ t₃) {s : set Ξ±} {f : Ξ± β†’ Ξ²} (hβ‚‚ : @continuous_on Ξ± Ξ² t₁ tβ‚‚ f s) : @continuous_on Ξ± Ξ² t₁ t₃ f s := begin rw continuous_on_iff' at hβ‚‚ ⊒, assume t ht, exact hβ‚‚ t (h₁ t ht) end theorem continuous_on_iff_is_closed {f : Ξ± β†’ Ξ²} {s : set Ξ±} : continuous_on f s ↔ βˆ€ t : set Ξ², is_closed t β†’ βˆƒ u, is_closed u ∧ f ⁻¹' t ∩ s = u ∩ s := have βˆ€ t, is_closed (s.restrict f ⁻¹' t) ↔ βˆƒ (u : set Ξ±), is_closed u ∧ f ⁻¹' t ∩ s = u ∩ s, begin intro t, rw [is_closed_induced_iff, set.restrict_eq, set.preimage_comp], simp only [subtype.preimage_coe_eq_preimage_coe_iff, eq_comm] end, by rw [continuous_on_iff_continuous_restrict, continuous_iff_is_closed]; simp only [this] lemma continuous_on.prod_map {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ΄} {s : set Ξ±} {t : set Ξ²} (hf : continuous_on f s) (hg : continuous_on g t) : continuous_on (prod.map f g) (s Γ—Λ’ t) := Ξ» ⟨x, y⟩ ⟨hx, hy⟩, continuous_within_at.prod_map (hf x hx) (hg y hy) lemma continuous_on_empty (f : Ξ± β†’ Ξ²) : continuous_on f βˆ… := Ξ» x, false.elim lemma continuous_on_singleton (f : Ξ± β†’ Ξ²) (a : Ξ±) : continuous_on f {a} := forall_eq.2 $ by simpa only [continuous_within_at, nhds_within_singleton, tendsto_pure_left] using Ξ» s, mem_of_mem_nhds lemma set.subsingleton.continuous_on {s : set Ξ±} (hs : s.subsingleton) (f : Ξ± β†’ Ξ²) : continuous_on f s := hs.induction_on (continuous_on_empty f) (continuous_on_singleton f) theorem nhds_within_le_comap {x : Ξ±} {s : set Ξ±} {f : Ξ± β†’ Ξ²} (ctsf : continuous_within_at f s x) : 𝓝[s] x ≀ comap f (𝓝[f '' s] (f x)) := ctsf.tendsto_nhds_within_image.le_comap @[simp] lemma comap_nhds_within_range {Ξ±} (f : Ξ± β†’ Ξ²) (y : Ξ²) : comap f (𝓝[range f] y) = comap f (𝓝 y) := comap_inf_principal_range theorem continuous_within_at_iff_ptendsto_res (f : Ξ± β†’ Ξ²) {x : Ξ±} {s : set Ξ±} : continuous_within_at f s x ↔ ptendsto (pfun.res f s) (𝓝 x) (𝓝 (f x)) := tendsto_iff_ptendsto _ _ _ _ lemma continuous_iff_continuous_on_univ {f : Ξ± β†’ Ξ²} : continuous f ↔ continuous_on f univ := by simp [continuous_iff_continuous_at, continuous_on, continuous_at, continuous_within_at, nhds_within_univ] lemma continuous_within_at.mono {f : Ξ± β†’ Ξ²} {s t : set Ξ±} {x : Ξ±} (h : continuous_within_at f t x) (hs : s βŠ† t) : continuous_within_at f s x := h.mono_left (nhds_within_mono x hs) lemma continuous_within_at.mono_of_mem {f : Ξ± β†’ Ξ²} {s t : set Ξ±} {x : Ξ±} (h : continuous_within_at f t x) (hs : t ∈ 𝓝[s] x) : continuous_within_at f s x := h.mono_left (nhds_within_le_of_mem hs) lemma continuous_within_at_inter' {f : Ξ± β†’ Ξ²} {s t : set Ξ±} {x : Ξ±} (h : t ∈ 𝓝[s] x) : continuous_within_at f (s ∩ t) x ↔ continuous_within_at f s x := by simp [continuous_within_at, nhds_within_restrict'' s h] lemma continuous_within_at_inter {f : Ξ± β†’ Ξ²} {s t : set Ξ±} {x : Ξ±} (h : t ∈ 𝓝 x) : continuous_within_at f (s ∩ t) x ↔ continuous_within_at f s x := by simp [continuous_within_at, nhds_within_restrict' s h] lemma continuous_within_at_union {f : Ξ± β†’ Ξ²} {s t : set Ξ±} {x : Ξ±} : continuous_within_at f (s βˆͺ t) x ↔ continuous_within_at f s x ∧ continuous_within_at f t x := by simp only [continuous_within_at, nhds_within_union, tendsto_sup] lemma continuous_within_at.union {f : Ξ± β†’ Ξ²} {s t : set Ξ±} {x : Ξ±} (hs : continuous_within_at f s x) (ht : continuous_within_at f t x) : continuous_within_at f (s βˆͺ t) x := continuous_within_at_union.2 ⟨hs, ht⟩ lemma continuous_within_at.mem_closure_image {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : continuous_within_at f s x) (hx : x ∈ closure s) : f x ∈ closure (f '' s) := by haveI := (mem_closure_iff_nhds_within_ne_bot.1 hx); exact (mem_closure_of_tendsto h $ mem_of_superset self_mem_nhds_within (subset_preimage_image f s)) lemma continuous_within_at.mem_closure {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} {A : set Ξ²} (h : continuous_within_at f s x) (hx : x ∈ closure s) (hA : maps_to f s A) : f x ∈ closure A := closure_mono (image_subset_iff.2 hA) (h.mem_closure_image hx) lemma set.maps_to.closure_of_continuous_within_at {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} (h : maps_to f s t) (hc : βˆ€ x ∈ closure s, continuous_within_at f s x) : maps_to f (closure s) (closure t) := Ξ» x hx, (hc x hx).mem_closure hx h lemma set.maps_to.closure_of_continuous_on {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} (h : maps_to f s t) (hc : continuous_on f (closure s)) : maps_to f (closure s) (closure t) := h.closure_of_continuous_within_at $ Ξ» x hx, (hc x hx).mono subset_closure lemma continuous_within_at.image_closure {f : Ξ± β†’ Ξ²} {s : set Ξ±} (hf : βˆ€ x ∈ closure s, continuous_within_at f s x) : f '' (closure s) βŠ† closure (f '' s) := maps_to'.1 $ (maps_to_image f s).closure_of_continuous_within_at hf lemma continuous_on.image_closure {f : Ξ± β†’ Ξ²} {s : set Ξ±} (hf : continuous_on f (closure s)) : f '' (closure s) βŠ† closure (f '' s) := continuous_within_at.image_closure $ Ξ» x hx, (hf x hx).mono subset_closure @[simp] lemma continuous_within_at_singleton {f : Ξ± β†’ Ξ²} {x : Ξ±} : continuous_within_at f {x} x := by simp only [continuous_within_at, nhds_within_singleton, tendsto_pure_nhds] @[simp] lemma continuous_within_at_insert_self {f : Ξ± β†’ Ξ²} {x : Ξ±} {s : set Ξ±} : continuous_within_at f (insert x s) x ↔ continuous_within_at f s x := by simp only [← singleton_union, continuous_within_at_union, continuous_within_at_singleton, true_and] alias continuous_within_at_insert_self ↔ _ continuous_within_at.insert_self lemma continuous_within_at.diff_iff {f : Ξ± β†’ Ξ²} {s t : set Ξ±} {x : Ξ±} (ht : continuous_within_at f t x) : continuous_within_at f (s \ t) x ↔ continuous_within_at f s x := ⟨λ h, (h.union ht).mono $ by simp only [diff_union_self, subset_union_left], Ξ» h, h.mono (diff_subset _ _)⟩ @[simp] lemma continuous_within_at_diff_self {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} : continuous_within_at f (s \ {x}) x ↔ continuous_within_at f s x := continuous_within_at_singleton.diff_iff @[simp] lemma continuous_within_at_compl_self {f : Ξ± β†’ Ξ²} {a : Ξ±} : continuous_within_at f {a}ᢜ a ↔ continuous_at f a := by rw [compl_eq_univ_diff, continuous_within_at_diff_self, continuous_within_at_univ] @[simp] lemma continuous_within_at_update_same [decidable_eq Ξ±] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} {y : Ξ²} : continuous_within_at (update f x y) s x ↔ tendsto f (𝓝[s \ {x}] x) (𝓝 y) := calc continuous_within_at (update f x y) s x ↔ tendsto (update f x y) (𝓝[s \ {x}] x) (𝓝 y) : by rw [← continuous_within_at_diff_self, continuous_within_at, function.update_same] ... ↔ tendsto f (𝓝[s \ {x}] x) (𝓝 y) : tendsto_congr' $ eventually_nhds_within_iff.2 $ eventually_of_forall $ Ξ» z hz, update_noteq hz.2 _ _ @[simp] lemma continuous_at_update_same [decidable_eq Ξ±] {f : Ξ± β†’ Ξ²} {x : Ξ±} {y : Ξ²} : continuous_at (function.update f x y) x ↔ tendsto f (𝓝[β‰ ] x) (𝓝 y) := by rw [← continuous_within_at_univ, continuous_within_at_update_same, compl_eq_univ_diff] theorem is_open_map.continuous_on_image_of_left_inv_on {f : Ξ± β†’ Ξ²} {s : set Ξ±} (h : is_open_map (s.restrict f)) {finv : Ξ² β†’ Ξ±} (hleft : left_inv_on finv f s) : continuous_on finv (f '' s) := begin refine continuous_on_iff'.2 (Ξ» t ht, ⟨f '' (t ∩ s), _, _⟩), { rw ← image_restrict, exact h _ (ht.preimage continuous_subtype_coe) }, { rw [inter_eq_self_of_subset_left (image_subset f (inter_subset_right t s)), hleft.image_inter'] }, end theorem is_open_map.continuous_on_range_of_left_inverse {f : Ξ± β†’ Ξ²} (hf : is_open_map f) {finv : Ξ² β†’ Ξ±} (hleft : function.left_inverse finv f) : continuous_on finv (range f) := begin rw [← image_univ], exact (hf.restrict is_open_univ).continuous_on_image_of_left_inv_on (Ξ» x _, hleft x) end lemma continuous_on.congr_mono {f g : Ξ± β†’ Ξ²} {s s₁ : set Ξ±} (h : continuous_on f s) (h' : eq_on g f s₁) (h₁ : s₁ βŠ† s) : continuous_on g s₁ := begin assume x hx, unfold continuous_within_at, have A := (h x (h₁ hx)).mono h₁, unfold continuous_within_at at A, rw ← h' hx at A, exact A.congr' h'.eventually_eq_nhds_within.symm end lemma continuous_on.congr {f g : Ξ± β†’ Ξ²} {s : set Ξ±} (h : continuous_on f s) (h' : eq_on g f s) : continuous_on g s := h.congr_mono h' (subset.refl _) lemma continuous_on_congr {f g : Ξ± β†’ Ξ²} {s : set Ξ±} (h' : eq_on g f s) : continuous_on g s ↔ continuous_on f s := ⟨λ h, continuous_on.congr h h'.symm, Ξ» h, h.congr h'⟩ lemma continuous_at.continuous_within_at {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : continuous_at f x) : continuous_within_at f s x := continuous_within_at.mono ((continuous_within_at_univ f x).2 h) (subset_univ _) lemma continuous_within_at_iff_continuous_at {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : s ∈ 𝓝 x) : continuous_within_at f s x ↔ continuous_at f x := by rw [← univ_inter s, continuous_within_at_inter h, continuous_within_at_univ] lemma continuous_within_at.continuous_at {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : continuous_within_at f s x) (hs : s ∈ 𝓝 x) : continuous_at f x := (continuous_within_at_iff_continuous_at hs).mp h lemma continuous_on.continuous_at {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : continuous_on f s) (hx : s ∈ 𝓝 x) : continuous_at f x := (h x (mem_of_mem_nhds hx)).continuous_at hx lemma continuous_at.continuous_on {f : Ξ± β†’ Ξ²} {s : set Ξ±} (hcont : βˆ€ x ∈ s, continuous_at f x) : continuous_on f s := Ξ» x hx, (hcont x hx).continuous_within_at lemma continuous_within_at.comp {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} {x : Ξ±} (hg : continuous_within_at g t (f x)) (hf : continuous_within_at f s x) (h : maps_to f s t) : continuous_within_at (g ∘ f) s x := hg.tendsto.comp (hf.tendsto_nhds_within h) lemma continuous_within_at.comp' {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} {x : Ξ±} (hg : continuous_within_at g t (f x)) (hf : continuous_within_at f s x) : continuous_within_at (g ∘ f) (s ∩ f⁻¹' t) x := hg.comp (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _) lemma continuous_at.comp_continuous_within_at {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (hg : continuous_at g (f x)) (hf : continuous_within_at f s x) : continuous_within_at (g ∘ f) s x := hg.continuous_within_at.comp hf (maps_to_univ _ _) lemma continuous_on.comp {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} (hg : continuous_on g t) (hf : continuous_on f s) (h : maps_to f s t) : continuous_on (g ∘ f) s := Ξ»x hx, continuous_within_at.comp (hg _ (h hx)) (hf x hx) h lemma continuous_on.mono {f : Ξ± β†’ Ξ²} {s t : set Ξ±} (hf : continuous_on f s) (h : t βŠ† s) : continuous_on f t := Ξ»x hx, (hf x (h hx)).mono_left (nhds_within_mono _ h) lemma antitone_continuous_on {f : Ξ± β†’ Ξ²} : antitone (continuous_on f) := Ξ» s t hst hf, hf.mono hst lemma continuous_on.comp' {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} (hg : continuous_on g t) (hf : continuous_on f s) : continuous_on (g ∘ f) (s ∩ f⁻¹' t) := hg.comp (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _) lemma continuous.continuous_on {f : Ξ± β†’ Ξ²} {s : set Ξ±} (h : continuous f) : continuous_on f s := begin rw continuous_iff_continuous_on_univ at h, exact h.mono (subset_univ _) end lemma continuous.continuous_within_at {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : continuous f) : continuous_within_at f s x := h.continuous_at.continuous_within_at lemma continuous.comp_continuous_on {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} {s : set Ξ±} (hg : continuous g) (hf : continuous_on f s) : continuous_on (g ∘ f) s := hg.continuous_on.comp hf (maps_to_univ _ _) lemma continuous_on.comp_continuous {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} {s : set Ξ²} (hg : continuous_on g s) (hf : continuous f) (hs : βˆ€ x, f x ∈ s) : continuous (g ∘ f) := begin rw continuous_iff_continuous_on_univ at *, exact hg.comp hf (Ξ» x _, hs x), end lemma continuous_within_at.preimage_mem_nhds_within {f : Ξ± β†’ Ξ²} {x : Ξ±} {s : set Ξ±} {t : set Ξ²} (h : continuous_within_at f s x) (ht : t ∈ 𝓝 (f x)) : f ⁻¹' t ∈ 𝓝[s] x := h ht lemma set.left_inv_on.map_nhds_within_eq {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ±} {x : Ξ²} {s : set Ξ²} (h : left_inv_on f g s) (hx : f (g x) = x) (hf : continuous_within_at f (g '' s) (g x)) (hg : continuous_within_at g s x) : map g (𝓝[s] x) = 𝓝[g '' s] (g x) := begin apply le_antisymm, { exact hg.tendsto_nhds_within (maps_to_image _ _) }, { have A : g ∘ f =αΆ [𝓝[g '' s] (g x)] id, from h.right_inv_on_image.eq_on.eventually_eq_of_mem self_mem_nhds_within, refine le_map_of_right_inverse A _, simpa only [hx] using hf.tendsto_nhds_within (h.maps_to (surj_on_image _ _)) } end lemma function.left_inverse.map_nhds_eq {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ±} {x : Ξ²} (h : function.left_inverse f g) (hf : continuous_within_at f (range g) (g x)) (hg : continuous_at g x) : map g (𝓝 x) = 𝓝[range g] (g x) := by simpa only [nhds_within_univ, image_univ] using (h.left_inv_on univ).map_nhds_within_eq (h x) (by rwa image_univ) hg.continuous_within_at lemma continuous_within_at.preimage_mem_nhds_within' {f : Ξ± β†’ Ξ²} {x : Ξ±} {s : set Ξ±} {t : set Ξ²} (h : continuous_within_at f s x) (ht : t ∈ 𝓝[f '' s] (f x)) : f ⁻¹' t ∈ 𝓝[s] x := h.tendsto_nhds_within (maps_to_image _ _) ht lemma filter.eventually_eq.congr_continuous_within_at {f g : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : f =αΆ [𝓝[s] x] g) (hx : f x = g x) : continuous_within_at f s x ↔ continuous_within_at g s x := by rw [continuous_within_at, hx, tendsto_congr' h, continuous_within_at] lemma continuous_within_at.congr_of_eventually_eq {f f₁ : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : continuous_within_at f s x) (h₁ : f₁ =αΆ [𝓝[s] x] f) (hx : f₁ x = f x) : continuous_within_at f₁ s x := (h₁.congr_continuous_within_at hx).2 h lemma continuous_within_at.congr {f f₁ : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : continuous_within_at f s x) (h₁ : βˆ€y∈s, f₁ y = f y) (hx : f₁ x = f x) : continuous_within_at f₁ s x := h.congr_of_eventually_eq (mem_of_superset self_mem_nhds_within h₁) hx lemma continuous_within_at.congr_mono {f g : Ξ± β†’ Ξ²} {s s₁ : set Ξ±} {x : Ξ±} (h : continuous_within_at f s x) (h' : eq_on g f s₁) (h₁ : s₁ βŠ† s) (hx : g x = f x): continuous_within_at g s₁ x := (h.mono h₁).congr h' hx lemma continuous_on_const {s : set Ξ±} {c : Ξ²} : continuous_on (Ξ»x, c) s := continuous_const.continuous_on lemma continuous_within_at_const {b : Ξ²} {s : set Ξ±} {x : Ξ±} : continuous_within_at (Ξ» _:Ξ±, b) s x := continuous_const.continuous_within_at lemma continuous_on_id {s : set Ξ±} : continuous_on id s := continuous_id.continuous_on lemma continuous_within_at_id {s : set Ξ±} {x : Ξ±} : continuous_within_at id s x := continuous_id.continuous_within_at lemma continuous_on_open_iff {f : Ξ± β†’ Ξ²} {s : set Ξ±} (hs : is_open s) : continuous_on f s ↔ (βˆ€t, is_open t β†’ is_open (s ∩ f⁻¹' t)) := begin rw continuous_on_iff', split, { assume h t ht, rcases h t ht with ⟨u, u_open, hu⟩, rw [inter_comm, hu], apply is_open.inter u_open hs }, { assume h t ht, refine ⟨s ∩ f ⁻¹' t, h t ht, _⟩, rw [@inter_comm _ s (f ⁻¹' t), inter_assoc, inter_self] } end lemma continuous_on.preimage_open_of_open {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} (hf : continuous_on f s) (hs : is_open s) (ht : is_open t) : is_open (s ∩ f⁻¹' t) := (continuous_on_open_iff hs).1 hf t ht lemma continuous_on.is_open_preimage {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} (h : continuous_on f s) (hs : is_open s) (hp : f ⁻¹' t βŠ† s) (ht : is_open t) : is_open (f ⁻¹' t) := begin convert (continuous_on_open_iff hs).mp h t ht, rw [inter_comm, inter_eq_self_of_subset_left hp], end lemma continuous_on.preimage_closed_of_closed {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} (hf : continuous_on f s) (hs : is_closed s) (ht : is_closed t) : is_closed (s ∩ f⁻¹' t) := begin rcases continuous_on_iff_is_closed.1 hf t ht with ⟨u, hu⟩, rw [inter_comm, hu.2], apply is_closed.inter hu.1 hs end lemma continuous_on.preimage_interior_subset_interior_preimage {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} (hf : continuous_on f s) (hs : is_open s) : s ∩ f⁻¹' (interior t) βŠ† s ∩ interior (f⁻¹' t) := calc s ∩ f ⁻¹' (interior t) βŠ† interior (s ∩ f ⁻¹' t) : interior_maximal (inter_subset_inter (subset.refl _) (preimage_mono interior_subset)) (hf.preimage_open_of_open hs is_open_interior) ... = s ∩ interior (f ⁻¹' t) : by rw [interior_inter, hs.interior_eq] lemma continuous_on_of_locally_continuous_on {f : Ξ± β†’ Ξ²} {s : set Ξ±} (h : βˆ€x∈s, βˆƒt, is_open t ∧ x ∈ t ∧ continuous_on f (s ∩ t)) : continuous_on f s := begin assume x xs, rcases h x xs with ⟨t, open_t, xt, ct⟩, have := ct x ⟨xs, xt⟩, rwa [continuous_within_at, ← nhds_within_restrict _ xt open_t] at this end lemma continuous_on_open_of_generate_from {Ξ² : Type*} {s : set Ξ±} {T : set (set Ξ²)} {f : Ξ± β†’ Ξ²} (hs : is_open s) (h : βˆ€t ∈ T, is_open (s ∩ f⁻¹' t)) : @continuous_on Ξ± Ξ² _ (topological_space.generate_from T) f s := begin rw continuous_on_open_iff, assume t ht, induction ht with u hu u v Tu Tv hu hv U hU hU', { exact h u hu }, { simp only [preimage_univ, inter_univ], exact hs }, { have : s ∩ f ⁻¹' (u ∩ v) = (s ∩ f ⁻¹' u) ∩ (s ∩ f ⁻¹' v), by rw [preimage_inter, inter_assoc, inter_left_comm _ s, ← inter_assoc s s, inter_self], rw this, exact hu.inter hv }, { rw [preimage_sUnion, inter_Unionβ‚‚], exact is_open_bUnion hU' }, { exact hs } end lemma continuous_within_at.prod {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {s : set Ξ±} {x : Ξ±} (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) : continuous_within_at (Ξ»x, (f x, g x)) s x := hf.prod_mk_nhds hg lemma continuous_on.prod {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {s : set Ξ±} (hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (Ξ»x, (f x, g x)) s := Ξ»x hx, continuous_within_at.prod (hf x hx) (hg x hx) lemma inducing.continuous_within_at_iff {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ³} (hg : inducing g) {s : set Ξ±} {x : Ξ±} : continuous_within_at f s x ↔ continuous_within_at (g ∘ f) s x := by simp_rw [continuous_within_at, inducing.tendsto_nhds_iff hg] lemma inducing.continuous_on_iff {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ³} (hg : inducing g) {s : set Ξ±} : continuous_on f s ↔ continuous_on (g ∘ f) s := by simp_rw [continuous_on, hg.continuous_within_at_iff] lemma embedding.continuous_on_iff {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ³} (hg : embedding g) {s : set Ξ±} : continuous_on f s ↔ continuous_on (g ∘ f) s := inducing.continuous_on_iff hg.1 lemma embedding.map_nhds_within_eq {f : Ξ± β†’ Ξ²} (hf : embedding f) (s : set Ξ±) (x : Ξ±) : map f (𝓝[s] x) = 𝓝[f '' s] (f x) := by rw [nhds_within, map_inf hf.inj, hf.map_nhds_eq, map_principal, ← nhds_within_inter', inter_eq_self_of_subset_right (image_subset_range _ _)] lemma open_embedding.map_nhds_within_preimage_eq {f : Ξ± β†’ Ξ²} (hf : open_embedding f) (s : set Ξ²) (x : Ξ±) : map f (𝓝[f ⁻¹' s] x) = 𝓝[s] (f x) := begin rw [hf.to_embedding.map_nhds_within_eq, image_preimage_eq_inter_range], apply nhds_within_eq_nhds_within (mem_range_self _) hf.open_range, rw [inter_assoc, inter_self] end lemma continuous_within_at_of_not_mem_closure {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} : x βˆ‰ closure s β†’ continuous_within_at f s x := begin intros hx, rw [mem_closure_iff_nhds_within_ne_bot, ne_bot_iff, not_not] at hx, rw [continuous_within_at, hx], exact tendsto_bot, end lemma continuous_on.piecewise' {s t : set Ξ±} {f g : Ξ± β†’ Ξ²} [βˆ€ a, decidable (a ∈ t)] (hpf : βˆ€ a ∈ s ∩ frontier t, tendsto f (𝓝[s ∩ t] a) (𝓝 (piecewise t f g a))) (hpg : βˆ€ a ∈ s ∩ frontier t, tendsto g (𝓝[s ∩ tᢜ] a) (𝓝 (piecewise t f g a))) (hf : continuous_on f $ s ∩ t) (hg : continuous_on g $ s ∩ tᢜ) : continuous_on (piecewise t f g) s := begin intros x hx, by_cases hx' : x ∈ frontier t, { exact (hpf x ⟨hx, hx'⟩).piecewise_nhds_within (hpg x ⟨hx, hx'⟩) }, { rw [← inter_univ s, ← union_compl_self t, inter_union_distrib_left] at hx ⊒, cases hx, { apply continuous_within_at.union, { exact (hf x hx).congr (Ξ» y hy, piecewise_eq_of_mem _ _ _ hy.2) (piecewise_eq_of_mem _ _ _ hx.2) }, { have : x βˆ‰ closure tᢜ, from Ξ» h, hx' ⟨subset_closure hx.2, by rwa closure_compl at h⟩, exact continuous_within_at_of_not_mem_closure (Ξ» h, this (closure_inter_subset_inter_closure _ _ h).2) } }, { apply continuous_within_at.union, { have : x βˆ‰ closure t, from (Ξ» h, hx' ⟨h, (Ξ» (h' : x ∈ interior t), hx.2 (interior_subset h'))⟩), exact continuous_within_at_of_not_mem_closure (Ξ» h, this (closure_inter_subset_inter_closure _ _ h).2) }, { exact (hg x hx).congr (Ξ» y hy, piecewise_eq_of_not_mem _ _ _ hy.2) (piecewise_eq_of_not_mem _ _ _ hx.2) } } } end lemma continuous_on.if' {s : set Ξ±} {p : Ξ± β†’ Prop} {f g : Ξ± β†’ Ξ²} [βˆ€ a, decidable (p a)] (hpf : βˆ€ a ∈ s ∩ frontier {a | p a}, tendsto f (𝓝[s ∩ {a | p a}] a) (𝓝 $ if p a then f a else g a)) (hpg : βˆ€ a ∈ s ∩ frontier {a | p a}, tendsto g (𝓝[s ∩ {a | Β¬p a}] a) (𝓝 $ if p a then f a else g a)) (hf : continuous_on f $ s ∩ {a | p a}) (hg : continuous_on g $ s ∩ {a | Β¬p a}) : continuous_on (Ξ» a, if p a then f a else g a) s := hf.piecewise' hpf hpg hg lemma continuous_on.if {Ξ± Ξ² : Type*} [topological_space Ξ±] [topological_space Ξ²] {p : Ξ± β†’ Prop} [βˆ€ a, decidable (p a)] {s : set Ξ±} {f g : Ξ± β†’ Ξ²} (hp : βˆ€ a ∈ s ∩ frontier {a | p a}, f a = g a) (hf : continuous_on f $ s ∩ closure {a | p a}) (hg : continuous_on g $ s ∩ closure {a | Β¬ p a}) : continuous_on (Ξ»a, if p a then f a else g a) s := begin apply continuous_on.if', { rintros a ha, simp only [← hp a ha, if_t_t], apply tendsto_nhds_within_mono_left (inter_subset_inter_right s subset_closure), exact hf a ⟨ha.1, ha.2.1⟩ }, { rintros a ha, simp only [hp a ha, if_t_t], apply tendsto_nhds_within_mono_left (inter_subset_inter_right s subset_closure), rcases ha with ⟨has, ⟨_, ha⟩⟩, rw [← mem_compl_iff, ← closure_compl] at ha, apply hg a ⟨has, ha⟩ }, { exact hf.mono (inter_subset_inter_right s subset_closure) }, { exact hg.mono (inter_subset_inter_right s subset_closure) } end lemma continuous_on.piecewise {s t : set Ξ±} {f g : Ξ± β†’ Ξ²} [βˆ€ a, decidable (a ∈ t)] (ht : βˆ€ a ∈ s ∩ frontier t, f a = g a) (hf : continuous_on f $ s ∩ closure t) (hg : continuous_on g $ s ∩ closure tᢜ) : continuous_on (piecewise t f g) s := hf.if ht hg lemma continuous_if' {p : Ξ± β†’ Prop} {f g : Ξ± β†’ Ξ²} [βˆ€ a, decidable (p a)] (hpf : βˆ€ a ∈ frontier {x | p x}, tendsto f (𝓝[{x | p x}] a) (𝓝 $ ite (p a) (f a) (g a))) (hpg : βˆ€ a ∈ frontier {x | p x}, tendsto g (𝓝[{x | Β¬p x}] a) (𝓝 $ ite (p a) (f a) (g a))) (hf : continuous_on f {x | p x}) (hg : continuous_on g {x | Β¬p x}) : continuous (Ξ» a, ite (p a) (f a) (g a)) := begin rw continuous_iff_continuous_on_univ, apply continuous_on.if'; simp *; assumption end lemma continuous_if {p : Ξ± β†’ Prop} {f g : Ξ± β†’ Ξ²} [βˆ€ a, decidable (p a)] (hp : βˆ€ a ∈ frontier {x | p x}, f a = g a) (hf : continuous_on f (closure {x | p x})) (hg : continuous_on g (closure {x | Β¬p x})) : continuous (Ξ» a, if p a then f a else g a) := begin rw continuous_iff_continuous_on_univ, apply continuous_on.if; simp; assumption end lemma continuous.if {p : Ξ± β†’ Prop} {f g : Ξ± β†’ Ξ²} [βˆ€ a, decidable (p a)] (hp : βˆ€ a ∈ frontier {x | p x}, f a = g a) (hf : continuous f) (hg : continuous g) : continuous (Ξ» a, if p a then f a else g a) := continuous_if hp hf.continuous_on hg.continuous_on lemma continuous_if_const (p : Prop) {f g : Ξ± β†’ Ξ²} [decidable p] (hf : p β†’ continuous f) (hg : Β¬ p β†’ continuous g) : continuous (Ξ» a, if p then f a else g a) := by { split_ifs, exact hf h, exact hg h } lemma continuous.if_const (p : Prop) {f g : Ξ± β†’ Ξ²} [decidable p] (hf : continuous f) (hg : continuous g) : continuous (Ξ» a, if p then f a else g a) := continuous_if_const p (Ξ» _, hf) (Ξ» _, hg) lemma continuous_piecewise {s : set Ξ±} {f g : Ξ± β†’ Ξ²} [βˆ€ a, decidable (a ∈ s)] (hs : βˆ€ a ∈ frontier s, f a = g a) (hf : continuous_on f (closure s)) (hg : continuous_on g (closure sᢜ)) : continuous (piecewise s f g) := continuous_if hs hf hg lemma continuous.piecewise {s : set Ξ±} {f g : Ξ± β†’ Ξ²} [βˆ€ a, decidable (a ∈ s)] (hs : βˆ€ a ∈ frontier s, f a = g a) (hf : continuous f) (hg : continuous g) : continuous (piecewise s f g) := hf.if hs hg lemma is_open.ite' {s s' t : set Ξ±} (hs : is_open s) (hs' : is_open s') (ht : βˆ€ x ∈ frontier t, x ∈ s ↔ x ∈ s') : is_open (t.ite s s') := begin classical, simp only [is_open_iff_continuous_mem, set.ite] at *, convert continuous_piecewise (Ξ» x hx, propext (ht x hx)) hs.continuous_on hs'.continuous_on, ext x, by_cases hx : x ∈ t; simp [hx] end lemma is_open.ite {s s' t : set Ξ±} (hs : is_open s) (hs' : is_open s') (ht : s ∩ frontier t = s' ∩ frontier t) : is_open (t.ite s s') := hs.ite' hs' $ Ξ» x hx, by simpa [hx] using ext_iff.1 ht x lemma ite_inter_closure_eq_of_inter_frontier_eq {s s' t : set Ξ±} (ht : s ∩ frontier t = s' ∩ frontier t) : t.ite s s' ∩ closure t = s ∩ closure t := by rw [closure_eq_self_union_frontier, inter_union_distrib_left, inter_union_distrib_left, ite_inter_self, ite_inter_of_inter_eq _ ht] lemma ite_inter_closure_compl_eq_of_inter_frontier_eq {s s' t : set Ξ±} (ht : s ∩ frontier t = s' ∩ frontier t) : t.ite s s' ∩ closure tᢜ = s' ∩ closure tᢜ := by { rw [← ite_compl, ite_inter_closure_eq_of_inter_frontier_eq], rwa [frontier_compl, eq_comm] } lemma continuous_on_piecewise_ite' {s s' t : set Ξ±} {f f' : Ξ± β†’ Ξ²} [βˆ€ x, decidable (x ∈ t)] (h : continuous_on f (s ∩ closure t)) (h' : continuous_on f' (s' ∩ closure tᢜ)) (H : s ∩ frontier t = s' ∩ frontier t) (Heq : eq_on f f' (s ∩ frontier t)) : continuous_on (t.piecewise f f') (t.ite s s') := begin apply continuous_on.piecewise, { rwa ite_inter_of_inter_eq _ H }, { rwa ite_inter_closure_eq_of_inter_frontier_eq H }, { rwa ite_inter_closure_compl_eq_of_inter_frontier_eq H } end lemma continuous_on_piecewise_ite {s s' t : set Ξ±} {f f' : Ξ± β†’ Ξ²} [βˆ€ x, decidable (x ∈ t)] (h : continuous_on f s) (h' : continuous_on f' s') (H : s ∩ frontier t = s' ∩ frontier t) (Heq : eq_on f f' (s ∩ frontier t)) : continuous_on (t.piecewise f f') (t.ite s s') := continuous_on_piecewise_ite' (h.mono (inter_subset_left _ _)) (h'.mono (inter_subset_left _ _)) H Heq lemma frontier_inter_open_inter {s t : set Ξ±} (ht : is_open t) : frontier (s ∩ t) ∩ t = frontier s ∩ t := by simp only [← subtype.preimage_coe_eq_preimage_coe_iff, ht.is_open_map_subtype_coe.preimage_frontier_eq_frontier_preimage continuous_subtype_coe, subtype.preimage_coe_inter_self] lemma continuous_on_fst {s : set (Ξ± Γ— Ξ²)} : continuous_on prod.fst s := continuous_fst.continuous_on lemma continuous_within_at_fst {s : set (Ξ± Γ— Ξ²)} {p : Ξ± Γ— Ξ²} : continuous_within_at prod.fst s p := continuous_fst.continuous_within_at lemma continuous_on.fst {f : Ξ± β†’ Ξ² Γ— Ξ³} {s : set Ξ±} (hf : continuous_on f s) : continuous_on (Ξ» x, (f x).1) s := continuous_fst.comp_continuous_on hf lemma continuous_within_at.fst {f : Ξ± β†’ Ξ² Γ— Ξ³} {s : set Ξ±} {a : Ξ±} (h : continuous_within_at f s a) : continuous_within_at (Ξ» x, (f x).fst) s a := continuous_at_fst.comp_continuous_within_at h lemma continuous_on_snd {s : set (Ξ± Γ— Ξ²)} : continuous_on prod.snd s := continuous_snd.continuous_on lemma continuous_within_at_snd {s : set (Ξ± Γ— Ξ²)} {p : Ξ± Γ— Ξ²} : continuous_within_at prod.snd s p := continuous_snd.continuous_within_at lemma continuous_on.snd {f : Ξ± β†’ Ξ² Γ— Ξ³} {s : set Ξ±} (hf : continuous_on f s) : continuous_on (Ξ» x, (f x).2) s := continuous_snd.comp_continuous_on hf lemma continuous_within_at.snd {f : Ξ± β†’ Ξ² Γ— Ξ³} {s : set Ξ±} {a : Ξ±} (h : continuous_within_at f s a) : continuous_within_at (Ξ» x, (f x).snd) s a := continuous_at_snd.comp_continuous_within_at h lemma continuous_within_at_prod_iff {f : Ξ± β†’ Ξ² Γ— Ξ³} {s : set Ξ±} {x : Ξ±} : continuous_within_at f s x ↔ continuous_within_at (prod.fst ∘ f) s x ∧ continuous_within_at (prod.snd ∘ f) s x := ⟨λ h, ⟨h.fst, h.snd⟩, by { rintro ⟨h1, h2⟩, convert h1.prod h2, ext, refl, refl }⟩
34b640ed6d8753dbf0e9402fd473990bbde02cbc
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/specific_limits/basic.lean
0a964e07c6ab9e08586d27f2dd3e9481a32e9cc5
[ "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
24,593
lean
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: SΓ©bastien GouΓ«zel, Johannes HΓΆlzl, Yury G. Kudryashov, Patrick Massot -/ import algebra.geom_sum import order.filter.archimedean import order.iterate import topology.instances.ennreal /-! # A collection of specific limit computations This file, by design, is independent of `normed_space` in the import hierarchy. It contains important specific limit computations in metric spaces, in ordered rings/fields, and in specific instances of these such as `ℝ`, `ℝβ‰₯0` and `ℝβ‰₯0∞`. -/ noncomputable theory open classical set function filter finset metric open_locale classical topological_space nat big_operators uniformity nnreal ennreal variables {Ξ± : Type*} {Ξ² : Type*} {ΞΉ : Type*} lemma tendsto_inverse_at_top_nhds_0_nat : tendsto (Ξ» n : β„•, (n : ℝ)⁻¹) at_top (𝓝 0) := tendsto_inv_at_top_zero.comp tendsto_coe_nat_at_top_at_top lemma tendsto_const_div_at_top_nhds_0_nat (C : ℝ) : tendsto (Ξ» n : β„•, C / n) at_top (𝓝 0) := by simpa only [mul_zero] using tendsto_const_nhds.mul tendsto_inverse_at_top_nhds_0_nat lemma nnreal.tendsto_inverse_at_top_nhds_0_nat : tendsto (Ξ» n : β„•, (n : ℝβ‰₯0)⁻¹) at_top (𝓝 0) := by { rw ← nnreal.tendsto_coe, exact tendsto_inverse_at_top_nhds_0_nat } lemma nnreal.tendsto_const_div_at_top_nhds_0_nat (C : ℝβ‰₯0) : tendsto (Ξ» n : β„•, C / n) at_top (𝓝 0) := by simpa using tendsto_const_nhds.mul nnreal.tendsto_inverse_at_top_nhds_0_nat lemma tendsto_one_div_add_at_top_nhds_0_nat : tendsto (Ξ» n : β„•, 1 / ((n : ℝ) + 1)) at_top (𝓝 0) := suffices tendsto (Ξ» n : β„•, 1 / (↑(n + 1) : ℝ)) at_top (𝓝 0), by simpa, (tendsto_add_at_top_iff_nat 1).2 (tendsto_const_div_at_top_nhds_0_nat 1) /-! ### Powers -/ lemma tendsto_add_one_pow_at_top_at_top_of_pos [linear_ordered_semiring Ξ±] [archimedean Ξ±] {r : Ξ±} (h : 0 < r) : tendsto (Ξ» n:β„•, (r + 1)^n) at_top at_top := tendsto_at_top_at_top_of_monotone' (Ξ» n m, pow_le_pow (le_add_of_nonneg_left (le_of_lt h))) $ not_bdd_above_iff.2 $ Ξ» x, set.exists_range_iff.2 $ add_one_pow_unbounded_of_pos _ h lemma tendsto_pow_at_top_at_top_of_one_lt [linear_ordered_ring Ξ±] [archimedean Ξ±] {r : Ξ±} (h : 1 < r) : tendsto (Ξ»n:β„•, r ^ n) at_top at_top := sub_add_cancel r 1 β–Έ tendsto_add_one_pow_at_top_at_top_of_pos (sub_pos.2 h) lemma nat.tendsto_pow_at_top_at_top_of_one_lt {m : β„•} (h : 1 < m) : tendsto (Ξ»n:β„•, m ^ n) at_top at_top := tsub_add_cancel_of_le (le_of_lt h) β–Έ tendsto_add_one_pow_at_top_at_top_of_pos (tsub_pos_of_lt h) lemma tendsto_pow_at_top_nhds_0_of_lt_1 {π•œ : Type*} [linear_ordered_field π•œ] [archimedean π•œ] [topological_space π•œ] [order_topology π•œ] {r : π•œ} (h₁ : 0 ≀ r) (hβ‚‚ : r < 1) : tendsto (Ξ»n:β„•, r^n) at_top (𝓝 0) := h₁.eq_or_lt.elim (assume : 0 = r, (tendsto_add_at_top_iff_nat 1).mp $ by simp [pow_succ, ← this, tendsto_const_nhds]) (assume : 0 < r, have tendsto (Ξ»n, (r⁻¹ ^ n)⁻¹) at_top (𝓝 0), from tendsto_inv_at_top_zero.comp (tendsto_pow_at_top_at_top_of_one_lt $ one_lt_inv this hβ‚‚), this.congr (Ξ» n, by simp)) lemma tendsto_pow_at_top_nhds_within_0_of_lt_1 {π•œ : Type*} [linear_ordered_field π•œ] [archimedean π•œ] [topological_space π•œ] [order_topology π•œ] {r : π•œ} (h₁ : 0 < r) (hβ‚‚ : r < 1) : tendsto (Ξ»n:β„•, r^n) at_top (𝓝[>] 0) := tendsto_inf.2 ⟨tendsto_pow_at_top_nhds_0_of_lt_1 h₁.le hβ‚‚, tendsto_principal.2 $ eventually_of_forall $ Ξ» n, pow_pos h₁ _⟩ lemma uniformity_basis_dist_pow_of_lt_1 {Ξ± : Type*} [pseudo_metric_space Ξ±] {r : ℝ} (hβ‚€ : 0 < r) (h₁ : r < 1) : (𝓀 Ξ±).has_basis (Ξ» k : β„•, true) (Ξ» k, {p : Ξ± Γ— Ξ± | dist p.1 p.2 < r ^ k}) := metric.mk_uniformity_basis (Ξ» i _, pow_pos hβ‚€ _) $ Ξ» Ξ΅ Ξ΅0, (exists_pow_lt_of_lt_one Ξ΅0 h₁).imp $ Ξ» k hk, ⟨trivial, hk.le⟩ lemma geom_lt {u : β„• β†’ ℝ} {c : ℝ} (hc : 0 ≀ c) {n : β„•} (hn : 0 < n) (h : βˆ€ k < n, c * u k < u (k + 1)) : c ^ n * u 0 < u n := begin refine (monotone_mul_left_of_nonneg hc).seq_pos_lt_seq_of_le_of_lt hn _ _ h, { simp }, { simp [pow_succ, mul_assoc, le_refl] } end lemma geom_le {u : β„• β†’ ℝ} {c : ℝ} (hc : 0 ≀ c) (n : β„•) (h : βˆ€ k < n, c * u k ≀ u (k + 1)) : c ^ n * u 0 ≀ u n := by refine (monotone_mul_left_of_nonneg hc).seq_le_seq n _ _ h; simp [pow_succ, mul_assoc, le_refl] lemma lt_geom {u : β„• β†’ ℝ} {c : ℝ} (hc : 0 ≀ c) {n : β„•} (hn : 0 < n) (h : βˆ€ k < n, u (k + 1) < c * u k) : u n < c ^ n * u 0 := begin refine (monotone_mul_left_of_nonneg hc).seq_pos_lt_seq_of_lt_of_le hn _ h _, { simp }, { simp [pow_succ, mul_assoc, le_refl] } end lemma le_geom {u : β„• β†’ ℝ} {c : ℝ} (hc : 0 ≀ c) (n : β„•) (h : βˆ€ k < n, u (k + 1) ≀ c * u k) : u n ≀ (c ^ n) * u 0 := by refine (monotone_mul_left_of_nonneg hc).seq_le_seq n _ h _; simp [pow_succ, mul_assoc, le_refl] /-- If a sequence `v` of real numbers satisfies `k * v n ≀ v (n+1)` with `1 < k`, then it goes to +∞. -/ lemma tendsto_at_top_of_geom_le {v : β„• β†’ ℝ} {c : ℝ} (hβ‚€ : 0 < v 0) (hc : 1 < c) (hu : βˆ€ n, c * v n ≀ v (n + 1)) : tendsto v at_top at_top := tendsto_at_top_mono (Ξ» n, geom_le (zero_le_one.trans hc.le) n (Ξ» k hk, hu k)) $ (tendsto_pow_at_top_at_top_of_one_lt hc).at_top_mul_const hβ‚€ lemma nnreal.tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝβ‰₯0} (hr : r < 1) : tendsto (Ξ» n:β„•, r^n) at_top (𝓝 0) := nnreal.tendsto_coe.1 $ by simp only [nnreal.coe_pow, nnreal.coe_zero, tendsto_pow_at_top_nhds_0_of_lt_1 r.coe_nonneg hr] lemma ennreal.tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝβ‰₯0∞} (hr : r < 1) : tendsto (Ξ» n:β„•, r^n) at_top (𝓝 0) := begin rcases ennreal.lt_iff_exists_coe.1 hr with ⟨r, rfl, hr'⟩, rw [← ennreal.coe_zero], norm_cast at *, apply nnreal.tendsto_pow_at_top_nhds_0_of_lt_1 hr end /-! ### Geometric series-/ section geometric lemma has_sum_geometric_of_lt_1 {r : ℝ} (h₁ : 0 ≀ r) (hβ‚‚ : r < 1) : has_sum (Ξ»n:β„•, r ^ n) (1 - r)⁻¹ := have r β‰  1, from ne_of_lt hβ‚‚, have tendsto (Ξ»n, (r ^ n - 1) * (r - 1)⁻¹) at_top (𝓝 ((0 - 1) * (r - 1)⁻¹)), from ((tendsto_pow_at_top_nhds_0_of_lt_1 h₁ hβ‚‚).sub tendsto_const_nhds).mul tendsto_const_nhds, (has_sum_iff_tendsto_nat_of_nonneg (pow_nonneg h₁) _).mpr $ by simp [neg_inv, geom_sum_eq, div_eq_mul_inv, *] at * lemma summable_geometric_of_lt_1 {r : ℝ} (h₁ : 0 ≀ r) (hβ‚‚ : r < 1) : summable (Ξ»n:β„•, r ^ n) := ⟨_, has_sum_geometric_of_lt_1 h₁ hβ‚‚βŸ© lemma tsum_geometric_of_lt_1 {r : ℝ} (h₁ : 0 ≀ r) (hβ‚‚ : r < 1) : βˆ‘'n:β„•, r ^ n = (1 - r)⁻¹ := (has_sum_geometric_of_lt_1 h₁ hβ‚‚).tsum_eq lemma has_sum_geometric_two : has_sum (Ξ»n:β„•, ((1:ℝ)/2) ^ n) 2 := by convert has_sum_geometric_of_lt_1 _ _; norm_num lemma summable_geometric_two : summable (Ξ»n:β„•, ((1:ℝ)/2) ^ n) := ⟨_, has_sum_geometric_two⟩ lemma summable_geometric_two_encode {ΞΉ : Type*} [encodable ΞΉ] : summable (Ξ» (i : ΞΉ), (1/2 : ℝ)^(encodable.encode i)) := summable_geometric_two.comp_injective encodable.encode_injective lemma tsum_geometric_two : βˆ‘'n:β„•, ((1:ℝ)/2) ^ n = 2 := has_sum_geometric_two.tsum_eq lemma sum_geometric_two_le (n : β„•) : βˆ‘ (i : β„•) in range n, (1 / (2 : ℝ)) ^ i ≀ 2 := begin have : βˆ€ i, 0 ≀ (1 / (2 : ℝ)) ^ i, { intro i, apply pow_nonneg, norm_num }, convert sum_le_tsum (range n) (Ξ» i _, this i) summable_geometric_two, exact tsum_geometric_two.symm end lemma tsum_geometric_inv_two : βˆ‘' n : β„•, (2 : ℝ)⁻¹ ^ n = 2 := (inv_eq_one_div (2 : ℝ)).symm β–Έ tsum_geometric_two /-- The sum of `2⁻¹ ^ i` for `n ≀ i` equals `2 * 2⁻¹ ^ n`. -/ lemma tsum_geometric_inv_two_ge (n : β„•) : βˆ‘' i, ite (n ≀ i) ((2 : ℝ)⁻¹ ^ i) 0 = 2 * 2⁻¹ ^ n := begin have A : summable (Ξ» (i : β„•), ite (n ≀ i) ((2⁻¹ : ℝ) ^ i) 0), { apply summable_of_nonneg_of_le _ _ summable_geometric_two; { intro i, by_cases hi : n ≀ i; simp [hi] } }, have B : (finset.range n).sum (Ξ» (i : β„•), ite (n ≀ i) ((2⁻¹ : ℝ)^i) 0) = 0 := finset.sum_eq_zero (Ξ» i hi, ite_eq_right_iff.2 $ Ξ» h, (lt_irrefl _ ((finset.mem_range.1 hi).trans_le h)).elim), simp only [← sum_add_tsum_nat_add n A, B, if_true, zero_add, zero_le', le_add_iff_nonneg_left, pow_add, tsum_mul_right, tsum_geometric_inv_two], end lemma has_sum_geometric_two' (a : ℝ) : has_sum (Ξ»n:β„•, (a / 2) / 2 ^ n) a := begin convert has_sum.mul_left (a / 2) (has_sum_geometric_of_lt_1 (le_of_lt one_half_pos) one_half_lt_one), { funext n, simp, refl, }, { norm_num } end lemma summable_geometric_two' (a : ℝ) : summable (Ξ» n:β„•, (a / 2) / 2 ^ n) := ⟨a, has_sum_geometric_two' a⟩ lemma tsum_geometric_two' (a : ℝ) : βˆ‘' n:β„•, (a / 2) / 2^n = a := (has_sum_geometric_two' a).tsum_eq /-- **Sum of a Geometric Series** -/ lemma nnreal.has_sum_geometric {r : ℝβ‰₯0} (hr : r < 1) : has_sum (Ξ» n : β„•, r ^ n) (1 - r)⁻¹ := begin apply nnreal.has_sum_coe.1, push_cast, rw [nnreal.coe_sub (le_of_lt hr)], exact has_sum_geometric_of_lt_1 r.coe_nonneg hr end lemma nnreal.summable_geometric {r : ℝβ‰₯0} (hr : r < 1) : summable (Ξ»n:β„•, r ^ n) := ⟨_, nnreal.has_sum_geometric hr⟩ lemma tsum_geometric_nnreal {r : ℝβ‰₯0} (hr : r < 1) : βˆ‘'n:β„•, r ^ n = (1 - r)⁻¹ := (nnreal.has_sum_geometric hr).tsum_eq /-- The series `pow r` converges to `(1-r)⁻¹`. For `r < 1` the RHS is a finite number, and for `1 ≀ r` the RHS equals `∞`. -/ @[simp] lemma ennreal.tsum_geometric (r : ℝβ‰₯0∞) : βˆ‘'n:β„•, r ^ n = (1 - r)⁻¹ := begin cases lt_or_le r 1 with hr hr, { rcases ennreal.lt_iff_exists_coe.1 hr with ⟨r, rfl, hr'⟩, norm_cast at *, convert ennreal.tsum_coe_eq (nnreal.has_sum_geometric hr), rw [ennreal.coe_inv $ ne_of_gt $ tsub_pos_iff_lt.2 hr] }, { rw [tsub_eq_zero_iff_le.mpr hr, ennreal.inv_zero, ennreal.tsum_eq_supr_nat, supr_eq_top], refine Ξ» a ha, (ennreal.exists_nat_gt (lt_top_iff_ne_top.1 ha)).imp (Ξ» n hn, lt_of_lt_of_le hn _), calc (n:ℝβ‰₯0∞) = βˆ‘ i in range n, 1 : by rw [sum_const, nsmul_one, card_range] ... ≀ βˆ‘ i in range n, r ^ i : sum_le_sum (Ξ» k _, one_le_pow_of_one_le' hr k) } end end geometric /-! ### Sequences with geometrically decaying distance in metric spaces In this paragraph, we discuss sequences in metric spaces or emetric spaces for which the distance between two consecutive terms decays geometrically. We show that such sequences are Cauchy sequences, and bound their distances to the limit. We also discuss series with geometrically decaying terms. -/ section edist_le_geometric variables [pseudo_emetric_space Ξ±] (r C : ℝβ‰₯0∞) (hr : r < 1) (hC : C β‰  ⊀) {f : β„• β†’ Ξ±} (hu : βˆ€n, edist (f n) (f (n+1)) ≀ C * r^n) include hr hC hu /-- If `edist (f n) (f (n+1))` is bounded by `C * r^n`, `C β‰  ∞`, `r < 1`, then `f` is a Cauchy sequence.-/ lemma cauchy_seq_of_edist_le_geometric : cauchy_seq f := begin refine cauchy_seq_of_edist_le_of_tsum_ne_top _ hu _, rw [ennreal.tsum_mul_left, ennreal.tsum_geometric], refine ennreal.mul_ne_top hC (ennreal.inv_ne_top.2 _), exact (tsub_pos_iff_lt.2 hr).ne' end omit hr hC /-- If `edist (f n) (f (n+1))` is bounded by `C * r^n`, then the distance from `f n` to the limit of `f` is bounded above by `C * r^n / (1 - r)`. -/ lemma edist_le_of_edist_le_geometric_of_tendsto {a : Ξ±} (ha : tendsto f at_top (𝓝 a)) (n : β„•) : edist (f n) a ≀ (C * r^n) / (1 - r) := begin convert edist_le_tsum_of_edist_le_of_tendsto _ hu ha _, simp only [pow_add, ennreal.tsum_mul_left, ennreal.tsum_geometric, div_eq_mul_inv, mul_assoc] end /-- If `edist (f n) (f (n+1))` is bounded by `C * r^n`, then the distance from `f 0` to the limit of `f` is bounded above by `C / (1 - r)`. -/ lemma edist_le_of_edist_le_geometric_of_tendstoβ‚€ {a : Ξ±} (ha : tendsto f at_top (𝓝 a)) : edist (f 0) a ≀ C / (1 - r) := by simpa only [pow_zero, mul_one] using edist_le_of_edist_le_geometric_of_tendsto r C hu ha 0 end edist_le_geometric section edist_le_geometric_two variables [pseudo_emetric_space Ξ±] (C : ℝβ‰₯0∞) (hC : C β‰  ⊀) {f : β„• β†’ Ξ±} (hu : βˆ€n, edist (f n) (f (n+1)) ≀ C / 2^n) {a : Ξ±} (ha : tendsto f at_top (𝓝 a)) include hC hu /-- If `edist (f n) (f (n+1))` is bounded by `C * 2^-n`, then `f` is a Cauchy sequence.-/ lemma cauchy_seq_of_edist_le_geometric_two : cauchy_seq f := begin simp only [div_eq_mul_inv, ennreal.inv_pow] at hu, refine cauchy_seq_of_edist_le_geometric 2⁻¹ C _ hC hu, simp [ennreal.one_lt_two] end omit hC include ha /-- If `edist (f n) (f (n+1))` is bounded by `C * 2^-n`, then the distance from `f n` to the limit of `f` is bounded above by `2 * C * 2^-n`. -/ lemma edist_le_of_edist_le_geometric_two_of_tendsto (n : β„•) : edist (f n) a ≀ 2 * C / 2^n := begin simp only [div_eq_mul_inv, ennreal.inv_pow] at *, rw [mul_assoc, mul_comm], convert edist_le_of_edist_le_geometric_of_tendsto 2⁻¹ C hu ha n, rw [ennreal.one_sub_inv_two, inv_inv] end /-- If `edist (f n) (f (n+1))` is bounded by `C * 2^-n`, then the distance from `f 0` to the limit of `f` is bounded above by `2 * C`. -/ lemma edist_le_of_edist_le_geometric_two_of_tendstoβ‚€: edist (f 0) a ≀ 2 * C := by simpa only [pow_zero, div_eq_mul_inv, ennreal.inv_one, mul_one] using edist_le_of_edist_le_geometric_two_of_tendsto C hu ha 0 end edist_le_geometric_two section le_geometric variables [pseudo_metric_space Ξ±] {r C : ℝ} (hr : r < 1) {f : β„• β†’ Ξ±} (hu : βˆ€n, dist (f n) (f (n+1)) ≀ C * r^n) include hr hu lemma aux_has_sum_of_le_geometric : has_sum (Ξ» n : β„•, C * r^n) (C / (1 - r)) := begin rcases sign_cases_of_C_mul_pow_nonneg (Ξ» n, dist_nonneg.trans (hu n)) with rfl | ⟨Cβ‚€, rβ‚€βŸ©, { simp [has_sum_zero] }, { refine has_sum.mul_left C _, simpa using has_sum_geometric_of_lt_1 rβ‚€ hr } end variables (r C) /-- If `dist (f n) (f (n+1))` is bounded by `C * r^n`, `r < 1`, then `f` is a Cauchy sequence. Note that this lemma does not assume `0 ≀ C` or `0 ≀ r`. -/ lemma cauchy_seq_of_le_geometric : cauchy_seq f := cauchy_seq_of_dist_le_of_summable _ hu ⟨_, aux_has_sum_of_le_geometric hr hu⟩ /-- If `dist (f n) (f (n+1))` is bounded by `C * r^n`, `r < 1`, then the distance from `f n` to the limit of `f` is bounded above by `C * r^n / (1 - r)`. -/ lemma dist_le_of_le_geometric_of_tendstoβ‚€ {a : Ξ±} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≀ C / (1 - r) := (aux_has_sum_of_le_geometric hr hu).tsum_eq β–Έ dist_le_tsum_of_dist_le_of_tendstoβ‚€ _ hu ⟨_, aux_has_sum_of_le_geometric hr hu⟩ ha /-- If `dist (f n) (f (n+1))` is bounded by `C * r^n`, `r < 1`, then the distance from `f 0` to the limit of `f` is bounded above by `C / (1 - r)`. -/ lemma dist_le_of_le_geometric_of_tendsto {a : Ξ±} (ha : tendsto f at_top (𝓝 a)) (n : β„•) : dist (f n) a ≀ (C * r^n) / (1 - r) := begin have := aux_has_sum_of_le_geometric hr hu, convert dist_le_tsum_of_dist_le_of_tendsto _ hu ⟨_, this⟩ ha n, simp only [pow_add, mul_left_comm C, mul_div_right_comm], rw [mul_comm], exact (this.mul_left _).tsum_eq.symm end omit hr hu variable (huβ‚‚ : βˆ€ n, dist (f n) (f (n+1)) ≀ (C / 2) / 2^n) /-- If `dist (f n) (f (n+1))` is bounded by `(C / 2) / 2^n`, then `f` is a Cauchy sequence. -/ lemma cauchy_seq_of_le_geometric_two : cauchy_seq f := cauchy_seq_of_dist_le_of_summable _ huβ‚‚ $ ⟨_, has_sum_geometric_two' C⟩ /-- If `dist (f n) (f (n+1))` is bounded by `(C / 2) / 2^n`, then the distance from `f 0` to the limit of `f` is bounded above by `C`. -/ lemma dist_le_of_le_geometric_two_of_tendstoβ‚€ {a : Ξ±} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≀ C := (tsum_geometric_two' C) β–Έ dist_le_tsum_of_dist_le_of_tendstoβ‚€ _ huβ‚‚ (summable_geometric_two' C) ha include huβ‚‚ /-- If `dist (f n) (f (n+1))` is bounded by `(C / 2) / 2^n`, then the distance from `f n` to the limit of `f` is bounded above by `C / 2^n`. -/ lemma dist_le_of_le_geometric_two_of_tendsto {a : Ξ±} (ha : tendsto f at_top (𝓝 a)) (n : β„•) : dist (f n) a ≀ C / 2^n := begin convert dist_le_tsum_of_dist_le_of_tendsto _ huβ‚‚ (summable_geometric_two' C) ha n, simp only [add_comm n, pow_add, ← div_div], symmetry, exact ((has_sum_geometric_two' C).div_const _).tsum_eq end end le_geometric /-! ### Summability tests based on comparison with geometric series -/ /-- A series whose terms are bounded by the terms of a converging geometric series converges. -/ lemma summable_one_div_pow_of_le {m : ℝ} {f : β„• β†’ β„•} (hm : 1 < m) (fi : βˆ€ i, i ≀ f i) : summable (Ξ» i, 1 / m ^ f i) := begin refine summable_of_nonneg_of_le (Ξ» a, one_div_nonneg.mpr (pow_nonneg (zero_le_one.trans hm.le) _)) (Ξ» a, _) (summable_geometric_of_lt_1 (one_div_nonneg.mpr (zero_le_one.trans hm.le)) ((one_div_lt (zero_lt_one.trans hm) zero_lt_one).mpr (one_div_one.le.trans_lt hm))), rw [div_pow, one_pow], refine (one_div_le_one_div _ _).mpr (pow_le_pow hm.le (fi a)); exact pow_pos (zero_lt_one.trans hm) _ end /-! ### Positive sequences with small sums on countable types -/ /-- For any positive `Ξ΅`, define on an encodable type a positive sequence with sum less than `Ξ΅` -/ def pos_sum_of_encodable {Ξ΅ : ℝ} (hΞ΅ : 0 < Ξ΅) (ΞΉ) [encodable ΞΉ] : {Ξ΅' : ΞΉ β†’ ℝ // (βˆ€ i, 0 < Ξ΅' i) ∧ βˆƒ c, has_sum Ξ΅' c ∧ c ≀ Ξ΅} := begin let f := Ξ» n, (Ξ΅ / 2) / 2 ^ n, have hf : has_sum f Ξ΅ := has_sum_geometric_two' _, have f0 : βˆ€ n, 0 < f n := Ξ» n, div_pos (half_pos hΞ΅) (pow_pos zero_lt_two _), refine ⟨f ∘ encodable.encode, Ξ» i, f0 _, _⟩, rcases hf.summable.comp_injective (@encodable.encode_injective ΞΉ _) with ⟨c, hg⟩, refine ⟨c, hg, has_sum_le_inj _ (@encodable.encode_injective ΞΉ _) _ _ hg hf⟩, { assume i _, exact le_of_lt (f0 _) }, { assume n, exact le_rfl } end lemma set.countable.exists_pos_has_sum_le {ΞΉ : Type*} {s : set ΞΉ} (hs : s.countable) {Ξ΅ : ℝ} (hΞ΅ : 0 < Ξ΅) : βˆƒ Ξ΅' : ΞΉ β†’ ℝ, (βˆ€ i, 0 < Ξ΅' i) ∧ βˆƒ c, has_sum (Ξ» i : s, Ξ΅' i) c ∧ c ≀ Ξ΅ := begin haveI := hs.to_encodable, rcases pos_sum_of_encodable hΞ΅ s with ⟨f, hf0, ⟨c, hfc, hcΡ⟩⟩, refine ⟨λ i, if h : i ∈ s then f ⟨i, h⟩ else 1, Ξ» i, _, ⟨c, _, hcΡ⟩⟩, { split_ifs, exacts [hf0 _, zero_lt_one] }, { simpa only [subtype.coe_prop, dif_pos, subtype.coe_eta] } end lemma set.countable.exists_pos_forall_sum_le {ΞΉ : Type*} {s : set ΞΉ} (hs : s.countable) {Ξ΅ : ℝ} (hΞ΅ : 0 < Ξ΅) : βˆƒ Ξ΅' : ΞΉ β†’ ℝ, (βˆ€ i, 0 < Ξ΅' i) ∧ βˆ€ t : finset ΞΉ, ↑t βŠ† s β†’ βˆ‘ i in t, Ξ΅' i ≀ Ξ΅ := begin rcases hs.exists_pos_has_sum_le hΞ΅ with ⟨Ρ', hpos, c, hΞ΅'c, hcΡ⟩, refine ⟨Ρ', hpos, Ξ» t ht, _⟩, rw [← sum_subtype_of_mem _ ht], refine (sum_le_has_sum _ _ hΞ΅'c).trans hcΞ΅, exact Ξ» _ _, (hpos _).le end namespace nnreal theorem exists_pos_sum_of_countable {Ξ΅ : ℝβ‰₯0} (hΞ΅ : Ξ΅ β‰  0) (ΞΉ) [countable ΞΉ] : βˆƒ Ξ΅' : ΞΉ β†’ ℝβ‰₯0, (βˆ€ i, 0 < Ξ΅' i) ∧ βˆƒc, has_sum Ξ΅' c ∧ c < Ξ΅ := begin casesI nonempty_encodable ΞΉ, obtain ⟨a, a0, aΡ⟩ := exists_between (pos_iff_ne_zero.2 hΞ΅), obtain ⟨Ρ', hΞ΅', c, hc, hcΡ⟩ := pos_sum_of_encodable a0 ΞΉ, exact ⟨λ i, ⟨Ρ' i, (hΞ΅' i).le⟩, Ξ» i, nnreal.coe_lt_coe.1 $ hΞ΅' i, ⟨c, has_sum_le (Ξ» i, (hΞ΅' i).le) has_sum_zero hc⟩, nnreal.has_sum_coe.1 hc, aΞ΅.trans_le' $ nnreal.coe_le_coe.1 hcΡ⟩, end end nnreal namespace ennreal theorem exists_pos_sum_of_countable {Ξ΅ : ℝβ‰₯0∞} (hΞ΅ : Ξ΅ β‰  0) (ΞΉ) [countable ΞΉ] : βˆƒ Ξ΅' : ΞΉ β†’ ℝβ‰₯0, (βˆ€ i, 0 < Ξ΅' i) ∧ βˆ‘' i, (Ξ΅' i : ℝβ‰₯0∞) < Ξ΅ := begin rcases exists_between (pos_iff_ne_zero.2 hΞ΅) with ⟨r, h0r, hrΡ⟩, rcases lt_iff_exists_coe.1 hrΞ΅ with ⟨x, rfl, hx⟩, rcases nnreal.exists_pos_sum_of_countable (coe_pos.1 h0r).ne' ΞΉ with ⟨Ρ', hp, c, hc, hcr⟩, exact ⟨Ρ', hp, (ennreal.tsum_coe_eq hc).symm β–Έ lt_trans (coe_lt_coe.2 hcr) hrΡ⟩ end theorem exists_pos_sum_of_countable' {Ξ΅ : ℝβ‰₯0∞} (hΞ΅ : Ξ΅ β‰  0) (ΞΉ) [countable ΞΉ] : βˆƒ Ξ΅' : ΞΉ β†’ ℝβ‰₯0∞, (βˆ€ i, 0 < Ξ΅' i) ∧ (βˆ‘' i, Ξ΅' i) < Ξ΅ := let ⟨δ, Ξ΄pos, hδ⟩ := exists_pos_sum_of_countable hΞ΅ ΞΉ in ⟨λ i, Ξ΄ i, Ξ» i, ennreal.coe_pos.2 (Ξ΄pos i), hδ⟩ theorem exists_pos_tsum_mul_lt_of_countable {Ξ΅ : ℝβ‰₯0∞} (hΞ΅ : Ξ΅ β‰  0) {ΞΉ} [countable ΞΉ] (w : ΞΉ β†’ ℝβ‰₯0∞) (hw : βˆ€ i, w i β‰  ∞) : βˆƒ Ξ΄ : ΞΉ β†’ ℝβ‰₯0, (βˆ€ i, 0 < Ξ΄ i) ∧ βˆ‘' i, (w i * Ξ΄ i : ℝβ‰₯0∞) < Ξ΅ := begin lift w to ΞΉ β†’ ℝβ‰₯0 using hw, rcases exists_pos_sum_of_countable hΞ΅ ΞΉ with ⟨δ', Hpos, Hsum⟩, have : βˆ€ i, 0 < max 1 (w i), from Ξ» i, zero_lt_one.trans_le (le_max_left _ _), refine ⟨λ i, Ξ΄' i / max 1 (w i), Ξ» i, nnreal.div_pos (Hpos _) (this i), _⟩, refine lt_of_le_of_lt (ennreal.tsum_le_tsum $ Ξ» i, _) Hsum, rw [coe_div (this i).ne'], refine mul_le_of_le_div' (ennreal.mul_le_mul le_rfl $ ennreal.inv_le_inv.2 _), exact coe_le_coe.2 (le_max_right _ _) end end ennreal /-! ### Factorial -/ lemma factorial_tendsto_at_top : tendsto nat.factorial at_top at_top := tendsto_at_top_at_top_of_monotone nat.monotone_factorial (Ξ» n, ⟨n, n.self_le_factorial⟩) lemma tendsto_factorial_div_pow_self_at_top : tendsto (Ξ» n, n! / n^n : β„• β†’ ℝ) at_top (𝓝 0) := tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds (tendsto_const_div_at_top_nhds_0_nat 1) (eventually_of_forall $ Ξ» n, div_nonneg (by exact_mod_cast n.factorial_pos.le) (pow_nonneg (by exact_mod_cast n.zero_le) _)) begin refine (eventually_gt_at_top 0).mono (Ξ» n hn, _), rcases nat.exists_eq_succ_of_ne_zero hn.ne.symm with ⟨k, rfl⟩, rw [← prod_range_add_one_eq_factorial, pow_eq_prod_const, div_eq_mul_inv, ← inv_eq_one_div, prod_nat_cast, nat.cast_succ, ← prod_inv_distrib, ← prod_mul_distrib, finset.prod_range_succ'], simp only [prod_range_succ', one_mul, nat.cast_add, zero_add, nat.cast_one], refine mul_le_of_le_one_left (inv_nonneg.mpr $ by exact_mod_cast hn.le) (prod_le_one _ _); intros x hx; rw finset.mem_range at hx, { refine mul_nonneg _ (inv_nonneg.mpr _); norm_cast; linarith }, { refine (div_le_one $ by exact_mod_cast hn).mpr _, norm_cast, linarith } end /-! ### Ceil and floor -/ section lemma tendsto_nat_floor_at_top {Ξ± : Type*} [linear_ordered_semiring Ξ±] [floor_semiring Ξ±] : tendsto (Ξ» (x : Ξ±), ⌊xβŒ‹β‚Š) at_top at_top := nat.floor_mono.tendsto_at_top_at_top (Ξ» x, ⟨max 0 (x + 1), by simp [nat.le_floor_iff]⟩) variables {R : Type*} [topological_space R] [linear_ordered_field R] [order_topology R] [floor_ring R] lemma tendsto_nat_floor_mul_div_at_top {a : R} (ha : 0 ≀ a) : tendsto (Ξ» x, (⌊a * xβŒ‹β‚Š : R) / x) at_top (𝓝 a) := begin have A : tendsto (Ξ» (x : R), a - x⁻¹) at_top (𝓝 (a - 0)) := tendsto_const_nhds.sub tendsto_inv_at_top_zero, rw sub_zero at A, apply tendsto_of_tendsto_of_tendsto_of_le_of_le' A tendsto_const_nhds, { refine eventually_at_top.2 ⟨1, Ξ» x hx, _⟩, simp only [le_div_iff (zero_lt_one.trans_le hx), sub_mul, inv_mul_cancel (zero_lt_one.trans_le hx).ne'], have := nat.lt_floor_add_one (a * x), linarith }, { refine eventually_at_top.2 ⟨1, Ξ» x hx, _⟩, rw div_le_iff (zero_lt_one.trans_le hx), simp [nat.floor_le (mul_nonneg ha (zero_le_one.trans hx))] } end lemma tendsto_nat_floor_div_at_top : tendsto (Ξ» x, (⌊xβŒ‹β‚Š : R) / x) at_top (𝓝 1) := by simpa using tendsto_nat_floor_mul_div_at_top (zero_le_one' R) lemma tendsto_nat_ceil_mul_div_at_top {a : R} (ha : 0 ≀ a) : tendsto (Ξ» x, (⌈a * xβŒ‰β‚Š : R) / x) at_top (𝓝 a) := begin have A : tendsto (Ξ» (x : R), a + x⁻¹) at_top (𝓝 (a + 0)) := tendsto_const_nhds.add tendsto_inv_at_top_zero, rw add_zero at A, apply tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds A, { refine eventually_at_top.2 ⟨1, Ξ» x hx, _⟩, rw le_div_iff (zero_lt_one.trans_le hx), exact nat.le_ceil _ }, { refine eventually_at_top.2 ⟨1, Ξ» x hx, _⟩, simp [div_le_iff (zero_lt_one.trans_le hx), inv_mul_cancel (zero_lt_one.trans_le hx).ne', (nat.ceil_lt_add_one ((mul_nonneg ha (zero_le_one.trans hx)))).le, add_mul] } end lemma tendsto_nat_ceil_div_at_top : tendsto (Ξ» x, (⌈xβŒ‰β‚Š : R) / x) at_top (𝓝 1) := by simpa using tendsto_nat_ceil_mul_div_at_top (zero_le_one' R) end
d88d33da5144d80ff5a6ac88655f22b06cb86caa
e1da55f4222dac91b940ca052928eaace09762da
/src/finpartition.lean
a3ccf0bc7fd63d376b60472eb828f6bba98ca830
[]
no_license
b-mehta/regularity-lemma
c5826e22c280d0b073a4e62dba731f4dd3d1b69f
cf26082b0c88fa54276e6fdc3338c15e607c52c6
refs/heads/master
1,658,209,524,267
1,644,406,456,000
1,644,406,456,000
457,327,371
1
0
null
null
null
null
UTF-8
Lean
false
false
18,248
lean
/- Copyright (c) 2022 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.basic import order.locally_finite import order.atoms import order.sup_indep /-! # Finite partitions In this file, we define finite partitions. A finpartition of `a : Ξ±` is a finite set of pairwise disjoint parts `parts : finset Ξ±` which does not contain `βŠ₯` and whose supremum is `a`. ## Constructions We provide many ways to build finpartitions: * `finpartition.of_erase`: Builds a finpartition by erasing `βŠ₯` for you. * `finpartition.of_subset`: Builds a finpartition from a subset of the parts of a previous finpartition. * `finpartition.empty`: The empty finpartition of `βŠ₯`. * `finpartition.indiscrete`: The indiscrete, aka trivial, aka pure, finpartition made of a single part. * `finpartition.discrete`: The discrete finpartition of `s : finset Ξ±` made of singletons. * `finpartition.bind`: Puts together the finpartitions of the parts of a finpartition into a new finpartition. * `finpartition.atomise`: Makes a finpartition of `s : finset Ξ±` by breaking `s` along all finsets in `F : finset (finset Ξ±)`. Two elements of `s` belong to the same part iff they belong to the same elements of `F`. `finpartition.indiscrete` and `finpartition.bind` together form the monadic structure of `finpartition`. ## Implementation notes Forbidding `βŠ₯` as a part follows mathematical tradition and is a pragmatic choice concerning operations on `finpartition`. Not caring about `βŠ₯` being a part or not breaks extensionality (it's not because the parts of `P` and the parts of `Q` have the same elements that `P = Q`). Enforcing `βŠ₯` to be a part makes `finpartition.bind` uglier and doesn't rid us of the need of `finpartition.of_erase`. ## TODO Link `finpartition` and `setoid.is_partition`. The order is the wrong way around to make `finpartition a` a graded order. Is it bad to depart from the literature and turn the order around? -/ lemma set.pairwise_disjoint.eq_of_le {Ξ± ΞΉ : Type*} [semilattice_inf Ξ±] [order_bot Ξ±] {s : set ΞΉ} {f : ΞΉ β†’ Ξ±} (hs : s.pairwise_disjoint f) {i j : ΞΉ} (hi : i ∈ s) (hj : j ∈ s) (h : f i β‰  βŠ₯) (hf : f i ≀ f j) : i = j := begin classical, by_contra hij, exact h ((hs hi hj hij).eq_bot_of_le hf), end instance {Ξ± ΞΉ : Type*} [lattice Ξ±] [order_bot Ξ±] {s t : finset ΞΉ} {f : ΞΉ β†’ Ξ±} {i : ΞΉ} [decidable_eq ΞΉ] [decidable_eq Ξ±] : decidable (s.sup_indep f) := begin apply @finset.decidable_forall_of_decidable_subsets _ _ _ _, intros t ht, apply @finset.decidable_dforall_finset _ _ _ _, exact Ξ» i hi, @implies.decidable _ _ _ (decidable_of_iff' (_ = βŠ₯) disjoint_iff), end open finset function open_locale big_operators variables {Ξ± : Type*} /-- A finite partition of `a : Ξ±` is a pairwise disjoint finite set of elements whose supremum is `a`. We forbid `βŠ₯` as a part. -/ @[ext, derive decidable_eq] structure finpartition [lattice Ξ±] [order_bot Ξ±] (a : Ξ±) := (parts : finset Ξ±) (sup_indep : parts.sup_indep id) (sup_parts : parts.sup id = a) (not_bot_mem : βŠ₯ βˆ‰ parts) attribute [protected] finpartition.sup_indep namespace finpartition section lattice variables [lattice Ξ±] [order_bot Ξ±] /-- A `finpartition` constructor which does not insist on `βŠ₯` not being a part. -/ @[simps] def of_erase [decidable_eq Ξ±] {a : Ξ±} (parts : finset Ξ±) (sup_indep : parts.sup_indep id) (sup_parts : parts.sup id = a) : finpartition a := { parts := parts.erase βŠ₯, sup_indep := sup_indep.subset (erase_subset _ _), sup_parts := (sup_erase_bot _).trans sup_parts, not_bot_mem := not_mem_erase _ _ } /-- A `finpartition` constructor from a bigger existing finpartition. -/ @[simps] def of_subset {a b : Ξ±} (P : finpartition a) {parts : finset Ξ±} (subset : parts βŠ† P.parts) (sup_parts : parts.sup id = b) : finpartition b := { parts := parts, sup_indep := P.sup_indep.subset subset, sup_parts := sup_parts, not_bot_mem := Ξ» h, P.not_bot_mem (subset h) } /-- Changes the type of a finpartition to an equal one. -/ @[simps] def copy {a b : Ξ±} (P : finpartition a) (h : a = b) : finpartition b := { parts := P.parts, sup_indep := P.sup_indep, sup_parts := h β–Έ P.sup_parts, not_bot_mem := P.not_bot_mem } variables (Ξ±) /-- The empty finpartition. -/ @[simps] protected def empty : finpartition (βŠ₯ : Ξ±) := { parts := βˆ…, sup_indep := sup_indep_empty _, sup_parts := finset.sup_empty, not_bot_mem := not_mem_empty βŠ₯ } instance : inhabited (finpartition (βŠ₯ : Ξ±)) := ⟨finpartition.empty α⟩ @[simp] lemma default_eq_empty : (default : finpartition (βŠ₯ : Ξ±)) = finpartition.empty Ξ± := rfl variables {Ξ±} {a : Ξ±} /-- The finpartition in one part, aka indiscrete finpartition. -/ @[simps] def indiscrete (ha : a β‰  βŠ₯) : finpartition a := { parts := {a}, sup_indep := sup_indep_singleton _ _, sup_parts := finset.sup_singleton, not_bot_mem := Ξ» h, ha (mem_singleton.1 h).symm } variables (P : finpartition a) protected lemma le {b : Ξ±} (hb : b ∈ P.parts) : b ≀ a := (le_sup hb).trans P.sup_parts.le lemma ne_bot {b : Ξ±} (hb : b ∈ P.parts) : b β‰  βŠ₯ := Ξ» h, P.not_bot_mem $ h.subst hb protected lemma disjoint : (P.parts : set Ξ±).pairwise_disjoint id := P.sup_indep.pairwise_disjoint variables {P} lemma parts_eq_empty_iff : P.parts = βˆ… ↔ a = βŠ₯ := begin simp_rw ←P.sup_parts, refine ⟨λ h, _, Ξ» h, eq_empty_iff_forall_not_mem.2 (Ξ» b hb, P.not_bot_mem _)⟩, { rw h, exact finset.sup_empty }, { rwa ←le_bot_iff.1 ((le_sup hb).trans h.le) } end lemma parts_nonempty_iff : P.parts.nonempty ↔ a β‰  βŠ₯ := by rw [nonempty_iff_ne_empty, not_iff_not, parts_eq_empty_iff] lemma parts_nonempty (P : finpartition a) (ha : a β‰  βŠ₯) : P.parts.nonempty := parts_nonempty_iff.2 ha instance : unique (finpartition (βŠ₯ : Ξ±)) := { uniq := Ξ» P, by { ext a, exact iff_of_false (Ξ» h, P.ne_bot h $ le_bot_iff.1 $ P.le h) (not_mem_empty a) }, ..finpartition.inhabited Ξ± } /-- There's a unique partition of an atom. -/ @[reducible] -- See note [reducible non instances] def _root_.is_atom.unique_finpartition (ha : is_atom a) : unique (finpartition a) := { default := indiscrete ha.1, uniq := Ξ» P, begin have h : βˆ€ b ∈ P.parts, b = a, { exact Ξ» b hb, (eq_bot_or_eq_of_le_atom ha $ P.le hb).resolve_left (P.ne_bot hb) }, ext b, refine iff.trans ⟨h b, _⟩ mem_singleton.symm, rintro rfl, obtain ⟨c, hc⟩ := P.parts_nonempty ha.1, simp_rw ←h c hc, exact hc, end } /-! ### Refinement order -/ section order /-- We say that `P ≀ Q` if `P` refines `Q`: each part of `P` is less than some part of `Q`. -/ instance : has_le (finpartition a) := ⟨λ P Q, βˆ€ ⦃b⦄, b ∈ P.parts β†’ βˆƒ c ∈ Q.parts, b ≀ c⟩ instance : partial_order (finpartition a) := { le_refl := Ξ» P b hb, ⟨b, hb, le_rfl⟩, le_trans := Ξ» P Q R hPQ hQR b hb, begin obtain ⟨c, hc, hbc⟩ := hPQ hb, obtain ⟨d, hd, hcd⟩ := hQR hc, exact ⟨d, hd, hbc.trans hcd⟩, end, le_antisymm := Ξ» P Q hPQ hQP, begin ext b, refine ⟨λ hb, _, Ξ» hb, _⟩, { obtain ⟨c, hc, hbc⟩ := hPQ hb, obtain ⟨d, hd, hcd⟩ := hQP hc, rwa hbc.antisymm, rwa P.disjoint.eq_of_le hb hd (P.ne_bot hb) (hbc.trans hcd) }, { obtain ⟨c, hc, hbc⟩ := hQP hb, obtain ⟨d, hd, hcd⟩ := hPQ hc, rwa hbc.antisymm, rwa Q.disjoint.eq_of_le hb hd (Q.ne_bot hb) (hbc.trans hcd) } end, ..finpartition.has_le } instance [decidable (a = βŠ₯)] : order_top (finpartition a) := { top := if ha : a = βŠ₯ then (finpartition.empty Ξ±).copy ha.symm else indiscrete ha, le_top := Ξ» P, begin split_ifs, { intros x hx, simpa [h, P.ne_bot hx] using P.le hx }, { exact Ξ» b hb, ⟨a, mem_singleton_self _, P.le hb⟩ } end } end order end lattice section distrib_lattice variables [distrib_lattice Ξ±] [order_bot Ξ±] [decidable_eq Ξ±] {a b c : Ξ±} instance : has_inf (finpartition a) := ⟨λ P Q, of_erase ((P.parts.product Q.parts).image $ Ξ» bc, bc.1 βŠ“ bc.2) begin rw sup_indep_iff_disjoint_erase, simp only [mem_image, and_imp, exists_prop, forall_exists_index, id.def, prod.exists, mem_product, finset.disjoint_sup_right, mem_erase, ne.def], rintro _ x₁ y₁ hx₁ hy₁ rfl _ h xβ‚‚ yβ‚‚ hxβ‚‚ hyβ‚‚ rfl, rcases eq_or_ne x₁ xβ‚‚ with rfl | xdiff, { refine disjoint.mono inf_le_right inf_le_right (Q.disjoint hy₁ hyβ‚‚ _), intro t, simpa [t] using h }, exact disjoint.mono inf_le_left inf_le_left (P.disjoint hx₁ hxβ‚‚ xdiff), end begin rw [sup_image, comp.left_id, sup_product_left], transitivity P.parts.sup id βŠ“ Q.parts.sup id, { simp_rw [finset.sup_inf_distrib_right, finset.sup_inf_distrib_left], refl }, { rw [P.sup_parts, Q.sup_parts, inf_idem] } end⟩ @[simp] lemma parts_inf (P Q : finpartition a) : (P βŠ“ Q).parts = ((P.parts.product Q.parts).image $ Ξ» bc : Ξ± Γ— Ξ±, bc.1 βŠ“ bc.2).erase βŠ₯ := rfl instance : semilattice_inf (finpartition a) := { inf_le_left := Ξ» P Q b hb, begin obtain ⟨c, hc, rfl⟩ := mem_image.1 (mem_of_mem_erase hb), rw mem_product at hc, exact ⟨c.1, hc.1, inf_le_left⟩, end, inf_le_right := Ξ» P Q b hb, begin obtain ⟨c, hc, rfl⟩ := mem_image.1 (mem_of_mem_erase hb), rw mem_product at hc, exact ⟨c.2, hc.2, inf_le_right⟩, end, le_inf := Ξ» P Q R hPQ hPR b hb, begin obtain ⟨c, hc, hbc⟩ := hPQ hb, obtain ⟨d, hd, hbd⟩ := hPR hb, have h := _root_.le_inf hbc hbd, refine ⟨c βŠ“ d, mem_erase_of_ne_of_mem (ne_bot_of_le_ne_bot (P.ne_bot hb) h) (mem_image.2 ⟨(c, d), mem_product.2 ⟨hc, hd⟩, rfl⟩), h⟩, end, ..finpartition.partial_order, ..finpartition.has_inf } lemma exists_le_of_le {P Q : finpartition a} (h : P ≀ Q) (hb : b ∈ Q.parts) : βˆƒ c ∈ P.parts, c ≀ b := begin by_contra' H, refine Q.ne_bot hb (disjoint_self.1 $ disjoint.mono_right (Q.le hb) _), rw [←P.sup_parts, finset.disjoint_sup_right], rintro c hc, obtain ⟨d, hd, hcd⟩ := h hc, refine (Q.disjoint hb hd _).mono_right hcd, rintro rfl, exact H _ hc hcd, end lemma card_mono {P Q : finpartition a} (h : P ≀ Q) : Q.parts.card ≀ P.parts.card := begin classical, have : βˆ€ b ∈ Q.parts, βˆƒ c ∈ P.parts, c ≀ b := Ξ» b, exists_le_of_le h, choose f hP hf using this, rw ←card_attach, refine card_le_card_of_inj_on (Ξ» b, f _ b.2) (Ξ» b _, hP _ b.2) (Ξ» b hb c hc h, _), exact subtype.coe_injective (Q.disjoint.elim b.2 c.2 $ Ξ» H, P.ne_bot (hP _ b.2) $ disjoint_self.1 $ H.mono (hf _ b.2) $ h.le.trans $ hf _ c.2), end section bind variables {a} {P : finpartition a} {Q : Ξ  i ∈ P.parts, finpartition i} /-- Given a finpartition `P` of `a` and finpartitions of each part of `P`, this yields the finpartition of `a` obtained by juxtaposing all the subpartitions. -/ @[simps] def bind (P : finpartition a) (Q : Ξ  i ∈ P.parts, finpartition i) : finpartition a := { parts := P.parts.attach.bUnion (Ξ» i, (Q i.1 i.2).parts), sup_indep := begin rw sup_indep_iff_pairwise_disjoint, rintro a ha b hb h, rw [finset.mem_coe, finset.mem_bUnion] at ha hb, obtain ⟨⟨A, hA⟩, -, ha⟩ := ha, obtain ⟨⟨B, hB⟩, -, hb⟩ := hb, obtain rfl | hAB := eq_or_ne A B, { exact (Q A hA).disjoint ha hb h }, { exact (P.disjoint hA hB hAB).mono ((Q A hA).le ha) ((Q B hB).le hb) } end, sup_parts := begin simp_rw [sup_bUnion, ←P.sup_parts], rw [eq_comm, ←finset.sup_attach], exact sup_congr rfl (Ξ» b hb, (Q b.1 b.2).sup_parts.symm), end, not_bot_mem := Ξ» h, begin rw finset.mem_bUnion at h, obtain ⟨⟨A, hA⟩, -, h⟩ := h, exact (Q A hA).not_bot_mem h, end } lemma mem_bind : b ∈ (P.bind Q).parts ↔ βˆƒ A hA, b ∈ (Q A hA).parts := begin rw [bind, mem_bUnion], split, { rintro ⟨⟨A, hA⟩, -, h⟩, exact ⟨A, hA, h⟩ }, { rintro ⟨A, hA, h⟩, exact ⟨⟨A, hA⟩, mem_attach _ ⟨A, hA⟩, h⟩ } end lemma card_bind (Q : Ξ  i ∈ P.parts, finpartition i) : (P.bind Q).parts.card = βˆ‘ A in P.parts.attach, (Q _ A.2).parts.card := begin apply card_bUnion, rintro ⟨b, hb⟩ - ⟨c, hc⟩ - hbc d, rw [inf_eq_inter, mem_inter], rintro ⟨hdb, hdc⟩, rw [ne.def, subtype.mk_eq_mk] at hbc, exact (Q b hb).ne_bot hdb (eq_bot_iff.2 $ (le_inf ((Q b hb).le hdb) $ (Q c hc).le hdc).trans $ P.disjoint hb hc hbc), end end bind /-- Adds `b` to a finpartition of `a` to make a finpartition of `a βŠ” b`. -/ @[simps] def extend (P : finpartition a) (hb : b β‰  βŠ₯) (hab : disjoint a b) (hc : a βŠ” b = c) : finpartition c := { parts := insert b P.parts, sup_indep := begin rw [sup_indep_iff_pairwise_disjoint, coe_insert], exact P.disjoint.insert (Ξ» d hd hbd, hab.symm.mono_right $ P.le hd), end, sup_parts := by rwa [sup_insert, P.sup_parts, id, _root_.sup_comm], not_bot_mem := Ξ» h, (mem_insert.1 h).elim hb.symm P.not_bot_mem } lemma card_extend (P : finpartition a) (b c : Ξ±) {hb : b β‰  βŠ₯} {hab : disjoint a b} {hc : a βŠ” b = c} : (P.extend hb hab hc).parts.card = P.parts.card + 1 := card_insert_of_not_mem $ Ξ» h, hb $ hab.symm.eq_bot_of_le $ P.le h end distrib_lattice section generalized_boolean_algebra variables [generalized_boolean_algebra Ξ±] [decidable_eq Ξ±] {a : Ξ±} (P : finpartition a) /-- Restricts a finpartition to avoid a given element. -/ @[simps] def avoid (b : Ξ±) : finpartition (a \ b) := of_erase (P.parts.image (\ b)) (P.disjoint.image_finset_of_le $ Ξ» a, sdiff_le).sup_indep (begin rw [sup_image, comp.left_id, finset.sup_sdiff_right], congr, exact P.sup_parts, end) end generalized_boolean_algebra end finpartition /-! ### Finite partitions of finsets -/ namespace finpartition variables [decidable_eq Ξ±] {s t : finset Ξ±} (P : finpartition s) lemma nonempty_of_mem_parts {a : finset Ξ±} (ha : a ∈ P.parts) : a.nonempty := nonempty_iff_ne_empty.2 $ P.ne_bot ha lemma exists_mem {a : Ξ±} (ha : a ∈ s) : βˆƒ t ∈ P.parts, a ∈ t := by { simp_rw ←P.sup_parts at ha, exact mem_sup.1 ha } lemma bUnion_parts : P.parts.bUnion id = s := (sup_eq_bUnion _ _).symm.trans P.sup_parts lemma sum_card_parts : βˆ‘ i in P.parts, i.card = s.card := begin convert congr_arg finset.card P.bUnion_parts, rw card_bUnion P.sup_indep.pairwise_disjoint, refl, end /-- `βŠ₯` is the partition in singletons, aka discrete partition. -/ instance (s : finset Ξ±) : has_bot (finpartition s) := ⟨{ parts := s.map ⟨singleton, singleton_injective⟩, sup_indep := set.pairwise_disjoint.sup_indep begin rw finset.coe_map, exact finset.pairwise_disjoint_range_singleton.subset (set.image_subset_range _ _), end, sup_parts := by rw [sup_map, comp.left_id, embedding.coe_fn_mk, finset.sup_singleton'], not_bot_mem := by simp }⟩ @[simp] lemma parts_bot (s : finset Ξ±) : (βŠ₯ : finpartition s).parts = s.map ⟨singleton, singleton_injective⟩ := rfl lemma card_bot (s : finset Ξ±) : (βŠ₯ : finpartition s).parts.card = s.card := finset.card_map _ lemma mem_bot_iff : t ∈ (βŠ₯ : finpartition s).parts ↔ βˆƒ a ∈ s, {a} = t := mem_map instance (s : finset Ξ±) : order_bot (finpartition s) := { bot_le := Ξ» P t ht, begin rw mem_bot_iff at ht, obtain ⟨a, ha, rfl⟩ := ht, obtain ⟨t, ht, hat⟩ := P.exists_mem ha, exact ⟨t, ht, singleton_subset_iff.2 hat⟩, end, ..finpartition.has_bot s } lemma card_parts_le_card (P : finpartition s) : P.parts.card ≀ s.card := by { rw ←card_bot s, exact card_mono bot_le } section atomise /-- Cuts `s` along the finsets in `F`: Two elements of `s` will be in the same part if they are in the same finsets of `F`. -/ def atomise (s : finset Ξ±) (F : finset (finset Ξ±)) : finpartition s := of_erase (F.powerset.image $ Ξ» Q, s.filter (Ξ» i, βˆ€ t ∈ F, t ∈ Q ↔ i ∈ t)) (set.pairwise_disjoint.sup_indep $ Ξ» x hx y hy h z hz, h begin rw [mem_coe, mem_image] at hx hy, obtain ⟨Q, hQ, rfl⟩ := hx, obtain ⟨R, hR, rfl⟩ := hy, suffices h : Q = R, { subst h }, rw [id, id, inf_eq_inter, mem_inter, mem_filter, mem_filter] at hz, rw mem_powerset at hQ hR, ext i, refine ⟨λ hi, _, Ξ» hi, _⟩, { rwa [hz.2.2 _ (hQ hi), ←hz.1.2 _ (hQ hi)] }, { rwa [hz.1.2 _ (hR hi), ←hz.2.2 _ (hR hi)] } end) (begin refine (finset.sup_le $ Ξ» t ht, _).antisymm (Ξ» a ha, _), { rw mem_image at ht, obtain ⟨A, hA, rfl⟩ := ht, exact s.filter_subset _ }, { rw [mem_sup], refine ⟨s.filter (Ξ» i, βˆ€ t, t ∈ F β†’ (t ∈ F.filter (Ξ» u, a ∈ u) ↔ i ∈ t)), mem_image_of_mem _ (mem_powerset.2 $ filter_subset _ _), mem_filter.2 ⟨ha, Ξ» t ht, _⟩⟩, rw mem_filter, exact and_iff_right ht } end) variables {F : finset (finset Ξ±)} lemma mem_atomise {t : finset Ξ±} : t ∈ (atomise s F).parts ↔ t.nonempty ∧ βˆƒ (Q βŠ† F), s.filter (Ξ» i, βˆ€ u ∈ F, u ∈ Q ↔ i ∈ u) = t := by simp only [atomise, of_erase, bot_eq_empty, mem_erase, mem_image, nonempty_iff_ne_empty, mem_singleton, and_comm, mem_powerset, exists_prop] lemma atomise_empty (hs : s.nonempty) : (atomise s βˆ…).parts = {s} := begin simp only [atomise, powerset_empty, image_singleton, not_mem_empty, forall_false_left, implies_true_iff, filter_true], exact erase_eq_of_not_mem (not_mem_singleton.2 hs.ne_empty.symm), end lemma card_atomise_le : (atomise s F).parts.card ≀ 2^F.card := (card_le_of_subset $ erase_subset _ _).trans $ finset.card_image_le.trans (card_powerset _).le lemma bUnion_filter_atomise (t : finset Ξ±) (ht : t ∈ F) (hts : t βŠ† s) : ((atomise s F).parts.filter $ Ξ» u, u βŠ† t).bUnion id = t := begin ext a, rw mem_bUnion, refine ⟨λ ⟨u, hu, ha⟩, (mem_filter.1 hu).2 ha, Ξ» ha, _⟩, obtain ⟨u, hu, hau⟩ := (atomise s F).exists_mem (hts ha), refine ⟨u, mem_filter.2 ⟨hu, Ξ» b hb, _⟩, hau⟩, obtain ⟨Q, hQ, rfl⟩ := (mem_atomise.1 hu).2, rw mem_filter at hau hb, rwa [←hb.2 _ ht, hau.2 _ ht] end end atomise end finpartition
1545bf56211bc8c309392317ba8b23378c3df050
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/algebra/category/Group/colimits.lean
c51ab395c1a886b1c9075631fe5169ce4f31ab2f
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
8,820
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.basic import category_theory.limits.limits /-! # 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 { rw cocone.naturality_concrete, }, -- zero { erw ((s.ΞΉ).app r).map_zero }, -- neg { rw ((s.ΞΉ).app r_j).map_neg }, -- add { rw ((s.ΞΉ).app r_j).map_add }, -- 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. -/ @[simps] 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_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 only [desc_morphism, quot_zero], erw m.map_zero, refl, }, { simp only [desc_morphism, quot_neg], erw m.map_neg, rw [x_ih], refl, }, { simp only [desc_morphism, quot_add], erw m.map_add, rw [x_ih_a, x_ih_a_1], refl, }, refl end }. instance has_colimits_AddCommGroup : has_colimits.{v} AddCommGroup.{v} := { has_colimits_of_shape := Ξ» J π’₯, { has_colimit := Ξ» F, by exactI { cocone := colimit_cocone F, is_colimit := colimit_is_colimit F } } } end AddCommGroup.colimits
ba1c0ac4f2df705b750e9d43c770cb073473e7a7
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/cc_ac1.lean
fcbef5aef0cb64184da2f06e79502905a9f44da7
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
186
lean
open tactic example (a b c : nat) (f : nat β†’ nat) : f (a + b + c) = f (c + b + a) := by cc example (a b c : nat) (f : nat β†’ nat) : a + b = c β†’ f (c + c) = f (a + b + c) := by cc
1769e7f03014e4bbf593f0096180d00e94d9fb40
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast_ematch_uni_issue.lean
c70b42686ba88772a6fdffc80ae68ebe5c9f752b
[ "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
464
lean
set_option blast.ematch true variable {A : Type} definition R : A β†’ A β†’ Prop := sorry definition R_trans [forward] {a b c : A} : R a b β†’ R b c β†’ R a c := sorry example (a b c : A) : R a b β†’ R b c β†’ R a c := by blast example {A : Type} (a b c : A) : R a b β†’ R b c β†’ R a c := by blast example {A : Type.{1}} (a b c : A) : R a b β†’ R b c β†’ R a c := by blast universe u example {A : Type.{u}} (a b c : A) : R a b β†’ R b c β†’ R a c := by blast
3d5cb8ee7c71300866a10248d87349fc2e612d8e
7cef822f3b952965621309e88eadf618da0c8ae9
/src/data/option/basic.lean
4b38285e0e952564a60a1e1a4caec5077edfc44d
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
6,623
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 logic.basic data.bool data.option.defs tactic.basic namespace option variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} lemma some_ne_none (x : Ξ±) : some x β‰  none := Ξ» h, option.no_confusion h @[simp] theorem get_mem : βˆ€ {o : option Ξ±} (h : is_some o), option.get h ∈ o | (some a) _ := rfl theorem get_of_mem {a : Ξ±} : βˆ€ {o : option Ξ±} (h : is_some o), a ∈ o β†’ option.get h = a | _ _ rfl := rfl @[simp] lemma not_mem_none (a : Ξ±) : a βˆ‰ (none : option Ξ±) := Ξ» h, option.no_confusion h @[simp] lemma some_get : βˆ€ {x : option Ξ±} (h : is_some x), some (option.get h) = x | (some x) hx := rfl @[simp] lemma get_some (x : Ξ±) (h : is_some (some x)) : option.get h = x := rfl theorem mem_unique {o : option Ξ±} {a b : Ξ±} (ha : a ∈ o) (hb : b ∈ o) : a = b := option.some.inj $ ha.symm.trans hb theorem injective_some (Ξ± : Type*) : function.injective (@some Ξ±) := Ξ» _ _, some_inj.mp /-- `option.map f` is injective if `f` is injective. -/ theorem injective_map {f : Ξ± β†’ Ξ²} (Hf : function.injective f) : function.injective (option.map f) | none none H := rfl | (some a₁) (some aβ‚‚) H := by rw Hf (option.some.inj H) @[ext] theorem ext : βˆ€ {o₁ oβ‚‚ : option Ξ±}, (βˆ€ a, a ∈ o₁ ↔ a ∈ oβ‚‚) β†’ o₁ = oβ‚‚ | none none H := rfl | (some a) o H := ((H _).1 rfl).symm | o (some b) H := (H _).2 rfl theorem eq_none_iff_forall_not_mem {o : option Ξ±} : o = none ↔ (βˆ€ a, a βˆ‰ o) := ⟨λ e a h, by rw e at h; cases h, Ξ» h, ext $ by simpa⟩ @[simp] theorem none_bind {Ξ± Ξ²} (f : Ξ± β†’ option Ξ²) : none >>= f = none := rfl @[simp] theorem some_bind {Ξ± Ξ²} (a : Ξ±) (f : Ξ± β†’ option Ξ²) : some a >>= f = f a := rfl @[simp] theorem none_bind' (f : Ξ± β†’ option Ξ²) : none.bind f = none := rfl @[simp] theorem some_bind' (a : Ξ±) (f : Ξ± β†’ option Ξ²) : (some a).bind f = f a := rfl @[simp] theorem bind_some : βˆ€ x : option Ξ±, x >>= some = x := @bind_pure Ξ± option _ _ @[simp] theorem bind_eq_some {Ξ± Ξ²} {x : option Ξ±} {f : Ξ± β†’ option Ξ²} {b : Ξ²} : x >>= f = some b ↔ βˆƒ a, x = some a ∧ f a = some b := by cases x; simp @[simp] theorem bind_eq_some' {x : option Ξ±} {f : Ξ± β†’ option Ξ²} {b : Ξ²} : x.bind f = some b ↔ βˆƒ a, x = some a ∧ f a = some b := by cases x; simp @[simp] theorem bind_eq_none' {o : option Ξ±} {f : Ξ± β†’ option Ξ²} : o.bind f = none ↔ (βˆ€ b a, a ∈ o β†’ b βˆ‰ f a) := by simp only [eq_none_iff_forall_not_mem, not_exists, not_and, mem_def, bind_eq_some'] @[simp] theorem bind_eq_none {Ξ± Ξ²} {o : option Ξ±} {f : Ξ± β†’ option Ξ²} : o >>= f = none ↔ (βˆ€ b a, a ∈ o β†’ b βˆ‰ f a) := bind_eq_none' lemma bind_comm {Ξ± Ξ² Ξ³} {f : Ξ± β†’ Ξ² β†’ option Ξ³} (a : option Ξ±) (b : option Ξ²) : a.bind (Ξ»x, b.bind (f x)) = b.bind (Ξ»y, a.bind (Ξ»x, f x y)) := by cases a; cases b; refl lemma bind_assoc (x : option Ξ±) (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ option Ξ³) : (x.bind f).bind g = x.bind (Ξ» y, (f y).bind g) := by cases x; refl @[simp] theorem map_none {Ξ± Ξ²} {f : Ξ± β†’ Ξ²} : f <$> none = none := rfl @[simp] theorem map_some {Ξ± Ξ²} {a : Ξ±} {f : Ξ± β†’ Ξ²} : f <$> some a = some (f a) := rfl @[simp] theorem map_none' {f : Ξ± β†’ Ξ²} : option.map f none = none := rfl @[simp] theorem map_some' {a : Ξ±} {f : Ξ± β†’ Ξ²} : option.map f (some a) = some (f a) := rfl @[simp] theorem map_eq_some {Ξ± Ξ²} {x : option Ξ±} {f : Ξ± β†’ Ξ²} {b : Ξ²} : f <$> x = some b ↔ βˆƒ a, x = some a ∧ f a = b := by cases x; simp @[simp] theorem map_eq_some' {x : option Ξ±} {f : Ξ± β†’ Ξ²} {b : Ξ²} : x.map f = some b ↔ βˆƒ a, x = some a ∧ f a = b := by cases x; simp @[simp] theorem map_id' : option.map (@id Ξ±) = id := map_id @[simp] theorem seq_some {Ξ± Ξ²} {a : Ξ±} {f : Ξ± β†’ Ξ²} : some f <*> some a = some (f a) := rfl @[simp] theorem some_orelse' (a : Ξ±) (x : option Ξ±) : (some a).orelse x = some a := rfl @[simp] theorem some_orelse (a : Ξ±) (x : option Ξ±) : (some a <|> x) = some a := rfl @[simp] theorem none_orelse' (x : option Ξ±) : none.orelse x = x := by cases x; refl @[simp] theorem none_orelse (x : option Ξ±) : (none <|> x) = x := none_orelse' x @[simp] theorem orelse_none' (x : option Ξ±) : x.orelse none = x := by cases x; refl @[simp] theorem orelse_none (x : option Ξ±) : (x <|> none) = x := orelse_none' x @[simp] theorem is_some_none : @is_some Ξ± none = ff := rfl @[simp] theorem is_some_some {a : Ξ±} : is_some (some a) = tt := rfl theorem is_some_iff_exists {x : option Ξ±} : is_some x ↔ βˆƒ a, x = some a := by cases x; simp [is_some]; exact ⟨_, rfl⟩ @[simp] theorem is_none_none : @is_none Ξ± none = tt := rfl @[simp] theorem is_none_some {a : Ξ±} : is_none (some a) = ff := rfl @[simp] theorem not_is_some {a : option Ξ±} : is_some a = ff ↔ a.is_none = tt := by cases a; simp lemma eq_some_iff_get_eq {o : option Ξ±} {a : Ξ±} : o = some a ↔ βˆƒ h : o.is_some, option.get h = a := by cases o; simp lemma not_is_some_iff_eq_none {o : option Ξ±} : Β¬o.is_some ↔ o = none := by cases o; simp lemma ne_none_iff_is_some {o : option Ξ±} : o β‰  none ↔ o.is_some := by cases o; simp lemma bex_ne_none {p : option Ξ± β†’ Prop} : (βˆƒ x β‰  none, p x) ↔ βˆƒ x, p (some x) := ⟨λ ⟨x, hx, hp⟩, ⟨get $ ne_none_iff_is_some.1 hx, by rwa [some_get]⟩, Ξ» ⟨x, hx⟩, ⟨some x, some_ne_none x, hx⟩⟩ lemma ball_ne_none {p : option Ξ± β†’ Prop} : (βˆ€ x β‰  none, p x) ↔ βˆ€ x, p (some x) := ⟨λ h x, h (some x) (some_ne_none x), Ξ» h x hx, by simpa only [some_get] using h (get $ ne_none_iff_is_some.1 hx)⟩ theorem iget_mem [inhabited Ξ±] : βˆ€ {o : option Ξ±}, is_some o β†’ o.iget ∈ o | (some a) _ := rfl theorem iget_of_mem [inhabited Ξ±] {a : Ξ±} : βˆ€ {o : option Ξ±}, a ∈ o β†’ o.iget = a | _ rfl := rfl @[simp] theorem guard_eq_some {p : Ξ± β†’ Prop} [decidable_pred p] {a b : Ξ±} : guard p a = some b ↔ a = b ∧ p a := by by_cases p a; simp [option.guard, h]; intro; contradiction @[simp] theorem guard_eq_some' {p : Prop} [decidable p] : βˆ€ u, _root_.guard p = some u ↔ p | () := by by_cases p; simp [guard, h, pure]; intro; contradiction theorem lift_or_get_choice {f : Ξ± β†’ Ξ± β†’ Ξ±} (h : βˆ€ a b, f a b = a ∨ f a b = b) : βˆ€ o₁ oβ‚‚, lift_or_get f o₁ oβ‚‚ = o₁ ∨ lift_or_get f o₁ oβ‚‚ = oβ‚‚ | none none := or.inl rfl | (some a) none := or.inl rfl | none (some b) := or.inr rfl | (some a) (some b) := by simpa [lift_or_get] using h a b end option
ebd6b4afd08b405815dcf60e04c523e439dbf01c
65b579fba1b0b66add04cccd4529add645eff597
/expression0.lean
5689db00e161a2a8eb7356f22807008916a5f8d9
[]
no_license
teodorov/sf_lean
ba637ca8ecc538aece4d02c8442d03ef713485db
cd4832d6bee9c606014c977951f6aebc4c8d611b
refs/heads/master
1,632,890,232,054
1,543,005,745,000
1,543,005,745,000
108,566,115
1
0
null
null
null
null
UTF-8
Lean
false
false
1,827
lean
-- inspired from https://github.com/UlfNorell/agda-summer-school/blob/OPLSS/lectures/Day1.agda inductive Expr : Type | elit (n : β„•) : Expr | ett : Expr | eff : Expr | eless (a b : Expr) : Expr | eplus (a b : Expr) : Expr | eif (a b c : Expr) : Expr . inductive Value : Type | nat : β„• β†’ Value | bool : bool β†’ Value . def getNat : Value β†’ option β„• | (Value.nat n) := some n | (Value.bool b) := none def getBool : Value β†’ option bool | (Value.nat n) := none | (Value.bool b) := some b def lookup : list β„• β†’ β„• β†’ option β„• | [] _ := none | (x::xs) i := if (i = 0) then some x else lookup xs (i-1). #eval lookup [10, 20, 30] 2 #eval lookup [10, 20, 30] 4 namespace Expr def lless : option β„• β†’ option β„• β†’ option Value := Ξ» mx my, mx >>= (Ξ» x, my >>= Ξ» y, return (Value.bool (x < y))) def pplus : option β„• β†’ option β„• β†’ option Value := Ξ» mx my, mx >>= (Ξ» x, my >>= Ξ» y, return (Value.nat (x + y))) def eval : Expr β†’ option Value | (elit n) := some (Value.nat n) | ett := some (Value.bool true) | eff := some (Value.bool false) | (eless e₁ eβ‚‚) := lless ((eval e₁) >>= getNat) ((eval eβ‚‚) >>=getNat) -- (Ξ» n m, Value.bool (n < m)) -- <$> (getNat =<< eval e₁) -- <*> (getNat =<< eval eβ‚‚) | (eplus e₁ eβ‚‚) := pplus ((eval e₁) >>= getNat) ((eval eβ‚‚) >>=getNat) -- (Ξ» n m, Value.nat (n + m)) -- <$> (getNat =<< eval e₁) -- <*> (getNat =<< eval eβ‚‚) | (eif e₁ eβ‚‚ e₃) := eval e₁ >>= getBool >>= (Ξ» b, if (b) then (eval eβ‚‚) else (eval e₃)) -- (Ξ» b, if (b) then (eval eβ‚‚) else (eval e₃)) -- =<< (getBool =<< (eval e₁)) #reduce eval (eif (eff) (elit 3) (elit 4)) end Expr
f347877f39eb456e152468ee5914a8a5f9ffdbd6
618003631150032a5676f229d13a079ac875ff77
/src/category_theory/currying.lean
2b0d3c0ef63d93f177c91a7bb19a507c4de69c0a
[ "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
3,228
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.products.bifunctor namespace category_theory universes v₁ vβ‚‚ v₃ u₁ uβ‚‚ u₃ variables {C : Type u₁} [category.{v₁} C] {D : Type uβ‚‚} [category.{vβ‚‚} D] {E : Type u₃} [category.{v₃} E] def uncurry : (C β₯€ (D β₯€ E)) β₯€ ((C Γ— D) β₯€ E) := { obj := Ξ» F, { obj := Ξ» X, (F.obj X.1).obj X.2, map := Ξ» X Y f, (F.map f.1).app X.2 ≫ (F.obj Y.1).map f.2, map_comp' := Ξ» X Y Z f g, begin simp only [prod_comp_fst, prod_comp_snd, functor.map_comp, nat_trans.comp_app, category.assoc], slice_lhs 2 3 { rw ← nat_trans.naturality }, rw category.assoc, end }, map := Ξ» F G T, { app := Ξ» X, (T.app X.1).app X.2, naturality' := Ξ» X Y f, begin simp only [prod_comp_fst, prod_comp_snd, category.comp_id, category.assoc, functor.map_id, functor.map_comp, nat_trans.id_app, nat_trans.comp_app], slice_lhs 2 3 { rw nat_trans.naturality }, slice_lhs 1 2 { rw [←nat_trans.comp_app, nat_trans.naturality, nat_trans.comp_app], }, rw category.assoc, end } }. def curry_obj (F : (C Γ— D) β₯€ E) : C β₯€ (D β₯€ E) := { obj := Ξ» X, { obj := Ξ» Y, F.obj (X, Y), map := Ξ» Y Y' g, F.map (πŸ™ X, g) }, map := Ξ» X X' f, { app := Ξ» Y, F.map (f, πŸ™ Y) } } def curry : ((C Γ— D) β₯€ E) β₯€ (C β₯€ (D β₯€ E)) := { obj := Ξ» F, curry_obj F, map := Ξ» F G T, { app := Ξ» X, { app := Ξ» Y, T.app (X, Y), naturality' := Ξ» Y Y' g, begin dsimp [curry_obj], rw nat_trans.naturality, end }, naturality' := Ξ» X X' f, begin ext, dsimp [curry_obj], rw nat_trans.naturality, end } }. @[simp] lemma uncurry.obj_obj {F : C β₯€ (D β₯€ E)} {X : C Γ— D} : (uncurry.obj F).obj X = (F.obj X.1).obj X.2 := rfl @[simp] lemma uncurry.obj_map {F : C β₯€ (D β₯€ E)} {X Y : C Γ— D} {f : X ⟢ Y} : (uncurry.obj F).map f = ((F.map f.1).app X.2) ≫ ((F.obj Y.1).map f.2) := rfl @[simp] lemma uncurry.map_app {F G : C β₯€ (D β₯€ E)} {Ξ± : F ⟢ G} {X : C Γ— D} : (uncurry.map Ξ±).app X = (Ξ±.app X.1).app X.2 := rfl @[simp] lemma curry.obj_obj_obj {F : (C Γ— D) β₯€ E} {X : C} {Y : D} : ((curry.obj F).obj X).obj Y = F.obj (X, Y) := rfl @[simp] lemma curry.obj_obj_map {F : (C Γ— D) β₯€ E} {X : C} {Y Y' : D} {g : Y ⟢ Y'} : ((curry.obj F).obj X).map g = F.map (πŸ™ X, g) := rfl @[simp] lemma curry.obj_map_app {F : (C Γ— D) β₯€ E} {X X' : C} {f : X ⟢ X'} {Y} : ((curry.obj F).map f).app Y = F.map (f, πŸ™ Y) := rfl @[simp] lemma curry.map_app_app {F G : (C Γ— D) β₯€ E} {Ξ± : F ⟢ G} {X} {Y} : ((curry.map Ξ±).app X).app Y = Ξ±.app (X, Y) := rfl def currying : (C β₯€ (D β₯€ E)) β‰Œ ((C Γ— D) β₯€ E) := equivalence.mk uncurry curry (nat_iso.of_components (Ξ» F, nat_iso.of_components (Ξ» X, nat_iso.of_components (Ξ» Y, as_iso (πŸ™ _)) (by tidy)) (by tidy)) (by tidy)) (nat_iso.of_components (Ξ» F, nat_iso.of_components (Ξ» X, eq_to_iso (by {dsimp, simp})) (by tidy)) (by tidy)) end category_theory
853cfaa9482bb5c21ddf836f0a313e23b464861b
bab2ace2b909818f20ac125499a8dd88ce812721
/src/2020/relations/partition_challenge_solution2.lean
16e3d9d362124cce46e55d4d39c346ca6f129e5d
[]
no_license
LaplaceKorea/M40001_lean
8a3cd411fb821a7665132c09e436f02f674cc666
116e9ed1fadf59dc2e78376fca92026859a03bf2
refs/heads/master
1,693,347,195,820
1,635,517,507,000
1,635,517,507,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,578
lean
import data.equiv.basic import tactic /-! # Definition and basic API for partitions -/ /-- The structure of a partition on a Type Ξ±. -/ -- Let Ξ± be a type. A *partition* on Ξ± is defined to -- be the following data. @[ext] structure partition (Ξ± : Type) := -- A set C of subsets of Ξ±, called "blocks". (C : set (set Ξ±)) -- A hypothesis (a.k.a. a proof) that all the blocks are non-empty. (Hnonempty : βˆ€ X ∈ C, (X : set Ξ±).nonempty) -- A hypothesis that every term of type Ξ± is in one of the blocks. (Hcover : βˆ€ a, βˆƒ X ∈ C, a ∈ X) -- A hypothesis that two blocks with non-empty intersection are equal. (Hdisjoint : βˆ€ X Y ∈ C, (X ∩ Y : set Ξ±).nonempty β†’ X = Y) namespace partition -- let Ξ± be a type, and fix a partition P on Ξ±. Let X and Y be subsets of Ξ±. variables {Ξ± : Type} {P : partition Ξ±} {X Y : set Ξ±} -- a more convenient way of putting it. theorem Hdisjoint' (hX : X ∈ P.C) (hY : Y ∈ P.C) : (X ∩ Y).nonempty β†’ X = Y := P.Hdisjoint X Y hX hY -- another way theorem Hdisjoint'' (hX : X ∈ P.C) (hY : Y ∈ P.C) {a : Ξ±} (haX : a ∈ X) (haY : a ∈ Y) : X = Y := P.Hdisjoint _ _ hX hY ⟨a, haX, haY⟩ end partition section equivalence_classes /-! # Definition and basic API for equivalence classes We define equivalence classes and prove a few basic results about them. -/ -- Notation and variables for this section: -- let Ξ± be a type, and let R be an equivalence relation on R. variables {Ξ± : Type} {R : Ξ± β†’ Ξ± β†’ Prop} (hR : equivalence R) -- Always assume R is an equivalence relation, even when we don't need it. include hR /-- The equivalence class of `x` is all the `y` such that `y` is related to `x`. -/ def cl (x : Ξ±) := {y : Ξ± | R y x} /-- Useful for rewriting -- `y` is in the equivalence class of `x` iff `y` is related to `x`. True by definition. -/ theorem mem_cl_iff {x y : Ξ±} : x ∈ cl hR y ↔ R x y := iff.rfl /-- x is in cl(x) -/ lemma mem_class_self (x : Ξ±) : x ∈ cl hR x := begin rcases hR with ⟨hrefl, hsymm, htrans⟩, exact hrefl x, end lemma class_sub {x y : Ξ±} : x ∈ cl hR y β†’ cl hR x βŠ† cl hR y := begin rcases hR with ⟨hrefl, hsymm, htrans⟩, intro hxy, intro z, intro hzx, exact htrans hzx hxy, end lemma class_eq {x y : Ξ±} : x ∈ cl hR y β†’ cl hR x = cl hR y := begin intro hxy, apply set.subset.antisymm, apply class_sub hR hxy, apply class_sub hR, rcases hR with ⟨hrefl, hsymm, htrans⟩, exact hsymm hxy, end end equivalence_classes -- section /-! # Statement of the theorem -/ open partition -- There is a bijection between equivalence relations and partitions example (Ξ± : Type) : {R : Ξ± β†’ Ξ± β†’ Prop // equivalence R} ≃ partition Ξ± := { -- Let R be an equivalence relation. to_fun := Ξ» R, { -- Let C be the set of equivalence classes for R. C := { B : set Ξ± | βˆƒ x : Ξ±, B = cl R.2 x}, -- I claim that C is a partition. We need to check three things. Hnonempty := begin -- If c is a block then c is nonempty. rintros c ⟨x, rfl⟩, use x, apply mem_class_self R.2, end, Hcover := begin intro x, use (cl R.2 x), split, { use x, }, { apply mem_class_self, } end, Hdisjoint := begin rintros c d ⟨x, rfl⟩ ⟨y, rfl⟩ ⟨z, hzx, hzy⟩, cases R with R hR, erw ← class_eq hR hzx, erw ← class_eq hR hzy, end }, -- Conversely, say P is an partition. inv_fun := Ξ» P, -- Let's define a binary relation by x ~ y iff there's a block they're both in ⟨λ a b, βˆ€ X ∈ P.C, a ∈ X β†’ b ∈ X, begin -- I claim this is an equivalence relation. split, { -- It's reflexive intros a C hC haC, exact haC, }, split, { -- it's symmetric intros x y h C hC hyC, rcases P.Hcover x with ⟨D, hD, hxD⟩, convert hxD, apply P.Hdisjoint _ _ hC hD, use [y, hyC], exact h D hD hxD, }, { -- it's transitive intros x y z hxy hyx C hC hxC, apply hyx C hC, apply hxy C hC, exact hxC, } end⟩, -- If you start with the equivalence relation, and then make the partition -- and a new equivalence relation, you get back to where you started. left_inv := begin rintro ⟨R, hR⟩, simp, ext a b, rcases hR with ⟨hRr,hRs,hRt⟩, split, { intro f, specialize f a (hRr a), exact hRs f, }, { intros hab t hat, refine hRt _ hat, exact hRs hab, }, end, -- Similarly, if you start with the partition, and then make the -- equivalence relation, and then construct the corresponding partition -- into equivalence classes, you have the same partition you started with. right_inv := begin intro P, ext W, simp, split, { rintro ⟨a, rfl⟩, rcases P.Hcover a with ⟨X, hX, haX⟩, convert hX, ext b, rw mem_cl_iff, split, { intro h, rcases P.Hcover b with ⟨Y, hY, hbY⟩, specialize h Y hY hbY, rwa Hdisjoint'' hX hY haX h, }, { intros hbX Y hY hbY, rwa Hdisjoint'' hY hX hbY hbX, } }, { intro hW, cases P.Hnonempty W hW with a haW, use a, ext b, rw mem_cl_iff, split, { intro hbW, intros X hX hbX, rwa Hdisjoint'' hX hW hbX hbW }, { intro haX, rcases P.Hcover b with ⟨X, hX, hbX⟩, specialize haX X hX hbX, rwa Hdisjoint'' hW hX haW haX } } end }
ae4cda0f9fa7a34e8e279dae1991685f1c2a0878
8b9f17008684d796c8022dab552e42f0cb6fb347
/library/logic/axioms/prop_decidable.lean
14daeeb835049d5cf0ae51d2f20c2d1a88f63e73
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
648
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: logic.axioms.prop_decidable Author: Leonardo de Moura Excluded middle + Hilbert implies every proposition is decidable. -/ import logic.axioms.prop_complete logic.axioms.hilbert open decidable inhabited nonempty theorem decidable_inhabited [instance] (a : Prop) : inhabited (decidable a) := inhabited_of_nonempty (or.elim (em a) (assume Ha, nonempty.intro (inl Ha)) (assume Hna, nonempty.intro (inr Hna))) theorem prop_decidable [instance] (a : Prop) : decidable a := arbitrary (decidable a)
cde83449626bb05761e400ca4af81241de2ff629
64874bd1010548c7f5a6e3e8902efa63baaff785
/library/logic/axioms/prop_decidable.lean
d3931aee0b29bbdc53a2773ef681ade1d2639c86
[ "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
883
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: logic.axioms.prop_decidable Author: Leonardo de Moura -/ import logic.axioms.classical logic.axioms.hilbert open decidable inhabited nonempty -- Excluded middle + Hilbert implies every proposition is decidable -- First, we show that (decidable a) is inhabited for any 'a' using the excluded middle theorem decidable_inhabited [instance] (a : Prop) : inhabited (decidable a) := inhabited_of_nonempty (or.elim (em a) (assume Ha, nonempty.intro (inl Ha)) (assume Hna, nonempty.intro (inr Hna))) -- Note that decidable_inhabited is marked as an instance, and it is silently used -- for synthesizing the implicit argument in the following 'epsilon' theorem prop_decidable [instance] (a : Prop) : decidable a := epsilon (Ξ»d, true)
dd02e16905e340cba494f189b11eeb5f54148624
1a61aba1b67cddccce19532a9596efe44be4285f
/library/algebra/lattice.lean
b2ec2fb0ed13c7067a8ea711e1085a4311e65cf7
[ "Apache-2.0" ]
permissive
eigengrau/lean
07986a0f2548688c13ba36231f6cdbee82abf4c6
f8a773be1112015e2d232661ce616d23f12874d0
refs/heads/master
1,610,939,198,566
1,441,352,386,000
1,441,352,494,000
41,903,576
0
0
null
1,441,352,210,000
1,441,352,210,000
null
UTF-8
Lean
false
false
4,095
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad -/ import .order namespace algebra variable {A : Type} /- lattices (we could split this to upper- and lower-semilattices, if needed) -/ structure lattice [class] (A : Type) extends weak_order A := (inf : A β†’ A β†’ A) (sup : A β†’ A β†’ A) (inf_le_left : βˆ€ a b, le (inf a b) a) (inf_le_right : βˆ€ a b, le (inf a b) b) (le_inf : βˆ€a b c, le c a β†’ le c b β†’ le c (inf a b)) (le_sup_left : βˆ€ a b, le a (sup a b)) (le_sup_right : βˆ€ a b, le b (sup a b)) (sup_le : βˆ€ a b c, le a c β†’ le b c β†’ le (sup a b) c) definition inf := @lattice.inf definition sup := @lattice.sup infix `βŠ“`:70 := inf infix `βŠ”`:65 := sup section variable [s : lattice A] include s theorem inf_le_left (a b : A) : a βŠ“ b ≀ a := !lattice.inf_le_left theorem inf_le_right (a b : A) : a βŠ“ b ≀ b := !lattice.inf_le_right theorem le_inf {a b c : A} (H₁ : c ≀ a) (Hβ‚‚ : c ≀ b) : c ≀ a βŠ“ b := !lattice.le_inf H₁ Hβ‚‚ theorem le_sup_left (a b : A) : a ≀ a βŠ” b := !lattice.le_sup_left theorem le_sup_right (a b : A) : b ≀ a βŠ” b := !lattice.le_sup_right theorem sup_le {a b c : A} (H₁ : a ≀ c) (Hβ‚‚ : b ≀ c) : a βŠ” b ≀ c := !lattice.sup_le H₁ Hβ‚‚ /- inf -/ theorem eq_inf {a b c : A} (H₁ : c ≀ a) (Hβ‚‚ : c ≀ b) (H₃ : βˆ€{d}, d ≀ a β†’ d ≀ b β†’ d ≀ c) : c = a βŠ“ b := le.antisymm (le_inf H₁ Hβ‚‚) (H₃ !inf_le_left !inf_le_right) theorem inf.comm (a b : A) : a βŠ“ b = b βŠ“ a := eq_inf !inf_le_right !inf_le_left (Ξ» c H₁ Hβ‚‚, le_inf Hβ‚‚ H₁) theorem inf.assoc (a b c : A) : (a βŠ“ b) βŠ“ c = a βŠ“ (b βŠ“ c) := begin apply eq_inf, { apply le.trans, apply inf_le_left, apply inf_le_left }, { apply le_inf, apply le.trans, apply inf_le_left, apply inf_le_right, apply inf_le_right }, { intros [d, H₁, Hβ‚‚], apply le_inf, apply le_inf H₁, apply le.trans Hβ‚‚, apply inf_le_left, apply le.trans Hβ‚‚, apply inf_le_right } end theorem inf.left_comm (a b c : A) : a βŠ“ (b βŠ“ c) = b βŠ“ (a βŠ“ c) := binary.left_comm (@inf.comm A s) (@inf.assoc A s) a b c theorem inf.right_comm (a b c : A) : (a βŠ“ b) βŠ“ c = (a βŠ“ c) βŠ“ b := binary.right_comm (@inf.comm A s) (@inf.assoc A s) a b c theorem inf_self (a : A) : a βŠ“ a = a := by apply eq.symm; apply eq_inf (le.refl a) !le.refl; intros; assumption theorem inf_eq_left {a b : A} (H : a ≀ b) : a βŠ“ b = a := by apply eq.symm; apply eq_inf !le.refl H; intros; assumption theorem inf_eq_right {a b : A} (H : b ≀ a) : a βŠ“ b = b := eq.subst !inf.comm (inf_eq_left H) /- sup -/ theorem eq_sup {a b c : A} (H₁ : a ≀ c) (Hβ‚‚ : b ≀ c) (H₃ : βˆ€{d}, a ≀ d β†’ b ≀ d β†’ c ≀ d) : c = a βŠ” b := le.antisymm (H₃ !le_sup_left !le_sup_right) (sup_le H₁ Hβ‚‚) theorem sup.comm (a b : A) : a βŠ” b = b βŠ” a := eq_sup !le_sup_right !le_sup_left (Ξ» c H₁ Hβ‚‚, sup_le Hβ‚‚ H₁) theorem sup.assoc (a b c : A) : (a βŠ” b) βŠ” c = a βŠ” (b βŠ” c) := begin apply eq_sup, { apply le.trans, apply le_sup_left a b, apply le_sup_left }, { apply sup_le, apply le.trans, apply le_sup_right a b, apply le_sup_left, apply le_sup_right }, { intros [d, H₁, Hβ‚‚], apply sup_le, apply sup_le H₁, apply le.trans !le_sup_left Hβ‚‚, apply le.trans !le_sup_right Hβ‚‚} end theorem sup.left_comm (a b c : A) : a βŠ” (b βŠ” c) = b βŠ” (a βŠ” c) := binary.left_comm (@sup.comm A s) (@sup.assoc A s) a b c theorem sup.right_comm (a b c : A) : (a βŠ” b) βŠ” c = (a βŠ” c) βŠ” b := binary.right_comm (@sup.comm A s) (@sup.assoc A s) a b c theorem sup_self (a : A) : a βŠ” a = a := by apply eq.symm; apply eq_sup (le.refl a) !le.refl; intros; assumption theorem sup_eq_left {a b : A} (H : b ≀ a) : a βŠ” b = a := by apply eq.symm; apply eq_sup !le.refl H; intros; assumption theorem sup_eq_right {a b : A} (H : a ≀ b) : a βŠ” b = b := eq.subst !sup.comm (sup_eq_left H) end end algebra
92c034fe1da8927101278cc6aae248dc313239ac
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Compiler/Util.lean
c522e95f69274562dd97682686cf108f556bcec8
[ "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
3,804
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Environment namespace Lean.Compiler def neutralExpr : Expr := mkConst `_neutral def unreachableExpr : Expr := mkConst `_unreachable def objectType : Expr := mkConst `_obj def voidType : Expr := mkConst `_void def mkLcProof (pred : Expr) := mkApp (mkConst `lcProof []) pred namespace atMostOnce structure AtMostOnceData where found : Bool result : Bool def Visitor := AtMostOnceData β†’ AtMostOnceData @[inline] def seq (f g : Visitor) : Visitor := fun d => match f d with | ⟨found, false⟩ => ⟨found, false⟩ | other => g other instance : AndThen Visitor where andThen a b := seq a (b ()) @[inline] def skip : Visitor := id @[inline] def visitFVar (x y : FVarId) : Visitor | d@{result := false, ..} => d | {found := false, result := true} => {found := x == y, result := true} | {found := true, result := true} => {found := true, result := x != y} def visit (x : FVarId) : Expr β†’ Visitor | Expr.fvar y _ => visitFVar y x | Expr.app f a _ => visit x a >> visit x f | Expr.lam _ d b _ => visit x d >> visit x b | Expr.forallE _ d b _ => visit x d >> visit x b | Expr.letE _ t v b _ => visit x t >> visit x v >> visit x b | Expr.mdata _ e _ => visit x e | Expr.proj _ _ e _ => visit x e | _ => skip end atMostOnce open atMostOnce (visit) in /-- Return true iff the free variable with id `x` occurs at most once in `e` -/ @[export lean_at_most_once] def atMostOnce (e : Expr) (x : FVarId) : Bool := let {result := result, ..} := visit x e {found := false, result := true} result /- Helper functions for creating auxiliary names used in compiler passes. -/ @[export lean_mk_eager_lambda_lifting_name] def mkEagerLambdaLiftingName (n : Name) (idx : Nat) : Name := Name.mkStr n ("_elambda_" ++ toString idx) @[export lean_is_eager_lambda_lifting_name] def isEagerLambdaLiftingName : Name β†’ Bool | Name.str p s _ => "_elambda".isPrefixOf s || isEagerLambdaLiftingName p | Name.num p _ _ => isEagerLambdaLiftingName p | _ => false /-- Return the name of new definitions in the a given declaration. Here we consider only declarations we generate code for. We use this definition to implement `add_and_compile`. -/ @[export lean_get_decl_names_for_code_gen] private def getDeclNamesForCodeGen : Declaration β†’ List Name | Declaration.defnDecl { name := n, .. } => [n] | Declaration.mutualDefnDecl defs => defs.map fun d => d.name | Declaration.opaqueDecl { name := n, .. } => [n] | Declaration.axiomDecl { name := n, .. } => [n] -- axiom may be tagged with `@[extern ...]` | _ => [] def checkIsDefinition (env : Environment) (n : Name) : Except String Unit := match env.find? n with | (some (ConstantInfo.defnInfo _)) => Except.ok () | (some (ConstantInfo.opaqueInfo _)) => Except.ok () | none => Except.error s!"unknow declaration '{n}'" | _ => Except.error s!"declaration is not a definition '{n}'" /-- We generate auxiliary unsafe definitions for regular recursive definitions. The auxiliary unsafe definition has a clear runtime cost execution model. This function returns the auxiliary unsafe definition name for the given name. -/ @[export lean_mk_unsafe_rec_name] def mkUnsafeRecName (declName : Name) : Name := Name.mkStr declName "_unsafe_rec" /-- Return `some _` if the given name was created using `mkUnsafeRecName` -/ @[export lean_is_unsafe_rec_name] def isUnsafeRecName? : Name β†’ Option Name | Name.str n "_unsafe_rec" _ => some n | _ => none end Lean.Compiler
9d8253852ddb8e91ce09a55a1b1ac823153ca079
f618aea02cb4104ad34ecf3b9713065cc0d06103
/src/set_theory/ordinal_notation.lean
0dc3172878ea8790d7a3681259998a5190eeacd8
[ "Apache-2.0" ]
permissive
joehendrix/mathlib
84b6603f6be88a7e4d62f5b1b0cbb523bb82b9a5
c15eab34ad754f9ecd738525cb8b5a870e834ddc
refs/heads/master
1,589,606,591,630
1,555,946,393,000
1,555,946,393,000
182,813,854
0
0
null
1,555,946,309,000
1,555,946,308,000
null
UTF-8
Lean
false
false
35,791
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Ordinal notations (constructive ordinal arithmetic for ordinals < Ξ΅β‚€). -/ import set_theory.ordinal data.pnat open ordinal local attribute [instance, priority 0] nat.cast_coe local notation `Ο‰` := omega.{0} /-- Recursive definition of an ordinal notation. `zero` denotes the ordinal 0, and `oadd e n a` is intended to refer to `Ο‰^e * n + a`. For this to be valid Cantor normal form, we must have the exponents decrease to the right, but we can't state this condition until we've defined `repr`, so it is a separate definition `NF`. -/ @[derive decidable_eq] inductive onote : Type | zero : onote | oadd : onote β†’ β„•+ β†’ onote β†’ onote namespace onote /-- Notation for 0 -/ instance : has_zero onote := ⟨zero⟩ @[simp] theorem zero_def : zero = 0 := rfl /-- Notation for 1 -/ instance : has_one onote := ⟨oadd 0 1 0⟩ /-- Notation for Ο‰ -/ def omega : onote := oadd 1 1 0 /-- The ordinal denoted by a notation -/ @[simp] noncomputable def repr : onote β†’ ordinal.{0} | 0 := 0 | (oadd e n a) := Ο‰ ^ repr e * n + repr a def to_string_aux1 (e : onote) (n : β„•) (s : string) : string := if e = 0 then _root_.to_string n else (if e = 1 then "Ο‰" else "Ο‰^(" ++ s ++ ")") ++ if n = 1 then "" else "*" ++ _root_.to_string n /-- Print an ordinal notation -/ def to_string : onote β†’ string | zero := "0" | (oadd e n 0) := to_string_aux1 e n (to_string e) | (oadd e n a) := to_string_aux1 e n (to_string e) ++ " + " ++ to_string a /-- Print an ordinal notation -/ def repr' : onote β†’ string | zero := "0" | (oadd e n a) := "(oadd " ++ repr' e ++ " " ++ _root_.to_string (n:β„•) ++ " " ++ repr' a ++ ")" instance : has_to_string onote := ⟨to_string⟩ instance : has_repr onote := ⟨repr'⟩ instance : preorder onote := { le := Ξ» x y, repr x ≀ repr y, lt := Ξ» x y, repr x < repr y, le_refl := Ξ» a, @le_refl ordinal _ _, le_trans := Ξ» a b c, @le_trans ordinal _ _ _ _, lt_iff_le_not_le := Ξ» a b, @lt_iff_le_not_le ordinal _ _ _ } theorem lt_def {x y : onote} : x < y ↔ repr x < repr y := iff.rfl theorem le_def {x y : onote} : x ≀ y ↔ repr x ≀ repr y := iff.rfl /-- Convert a `nat` into an ordinal -/ @[simp] def of_nat : β„• β†’ onote | 0 := 0 | (nat.succ n) := oadd 0 n.succ_pnat 0 @[simp] theorem of_nat_one : of_nat 1 = 1 := rfl @[simp] theorem repr_of_nat (n : β„•) : repr (of_nat n) = n := by cases n; simp @[simp] theorem repr_one : repr 1 = 1 := by simpa using repr_of_nat 1 theorem omega_le_oadd (e n a) : Ο‰ ^ repr e ≀ repr (oadd e n a) := begin unfold repr, refine le_trans _ (le_add_right _ _), simpa using (mul_le_mul_iff_left $ power_pos (repr e) omega_pos).2 (nat_cast_le.2 n.2) end theorem oadd_pos (e n a) : 0 < oadd e n a := @lt_of_lt_of_le _ _ _ _ _ (power_pos _ omega_pos) (omega_le_oadd _ _ _) /-- Compare ordinal notations -/ def cmp : onote β†’ onote β†’ ordering | 0 0 := ordering.eq | _ 0 := ordering.gt | 0 _ := ordering.lt | o₁@(oadd e₁ n₁ a₁) oβ‚‚@(oadd eβ‚‚ nβ‚‚ aβ‚‚) := (cmp e₁ eβ‚‚).or_else $ (_root_.cmp (n₁:β„•) nβ‚‚).or_else (cmp a₁ aβ‚‚) theorem eq_of_cmp_eq : βˆ€ {o₁ oβ‚‚}, cmp o₁ oβ‚‚ = ordering.eq β†’ o₁ = oβ‚‚ | 0 0 h := rfl | (oadd e n a) 0 h := by injection h | 0 (oadd e n a) h := by injection h | o₁@(oadd e₁ n₁ a₁) oβ‚‚@(oadd eβ‚‚ nβ‚‚ aβ‚‚) h := begin revert h, simp [cmp], cases h₁ : cmp e₁ eβ‚‚; intro h; try {cases h}, have := eq_of_cmp_eq h₁, subst eβ‚‚, revert h, cases hβ‚‚ : _root_.cmp (n₁:β„•) nβ‚‚; intro h; try {cases h}, have := eq_of_cmp_eq h, subst aβ‚‚, rw [_root_.cmp, cmp_using_eq_eq] at hβ‚‚, have := subtype.eq (eq_of_incomp hβ‚‚), subst nβ‚‚, simp end theorem zero_lt_one : (0 : onote) < 1 := by rw [lt_def, repr, repr_one]; exact zero_lt_one /-- `NF_below o b` says that `o` is a normal form ordinal notation satisfying `repr o < Ο‰ ^ b`. -/ inductive NF_below : onote β†’ ordinal.{0} β†’ Prop | zero {b} : NF_below 0 b | oadd' {e n a eb b} : NF_below e eb β†’ NF_below a (repr e) β†’ repr e < b β†’ NF_below (oadd e n a) b /-- A normal form ordinal notation has the form Ο‰ ^ a₁ * n₁ + Ο‰ ^ aβ‚‚ * nβ‚‚ + ... Ο‰ ^ aβ‚– * nβ‚– where `a₁ > aβ‚‚ > ... > aβ‚–` and all the `aα΅’` are also in normal form. We will essentially only be interested in normal form ordinal notations, but to avoid complicating the algorithms we define everything over general ordinal notations and only prove correctness with normal form as an invariant. -/ @[class] def NF (o : onote) := Exists (NF_below o) instance NF.zero : NF 0 := ⟨0, NF_below.zero⟩ theorem NF_below.oadd {e n a b} : NF e β†’ NF_below a (repr e) β†’ repr e < b β†’ NF_below (oadd e n a) b | ⟨eb, h⟩ := NF_below.oadd' h theorem NF_below.fst {e n a b} (h : NF_below (oadd e n a) b) : NF e := by cases h with _ _ _ _ eb _ h₁ hβ‚‚ h₃; exact ⟨_, hβ‚βŸ© theorem NF.fst {e n a} : NF (oadd e n a) β†’ NF e | ⟨b, h⟩ := h.fst theorem NF_below.snd {e n a b} (h : NF_below (oadd e n a) b) : NF_below a (repr e) := by cases h with _ _ _ _ eb _ h₁ hβ‚‚ h₃; exact hβ‚‚ theorem NF.snd' {e n a} : NF (oadd e n a) β†’ NF_below a (repr e) | ⟨b, h⟩ := h.snd theorem NF.snd {e n a} (h : NF (oadd e n a)) : NF a := ⟨_, h.snd'⟩ theorem NF.oadd {e a} (h₁ : NF e) (n) (hβ‚‚ : NF_below a (repr e)) : NF (oadd e n a) := ⟨_, NF_below.oadd h₁ hβ‚‚ (ordinal.lt_succ_self _)⟩ instance NF.oadd_zero (e n) [h : NF e] : NF (oadd e n 0) := h.oadd _ NF_below.zero theorem NF_below.lt {e n a b} (h : NF_below (oadd e n a) b) : repr e < b := by cases h with _ _ _ _ eb _ h₁ hβ‚‚ h₃; exact h₃ theorem NF_below_zero : βˆ€ {o}, NF_below o 0 ↔ o = 0 | 0 := ⟨λ _, rfl, Ξ» _, NF_below.zero⟩ | (oadd e n a) := ⟨λ h, (not_le_of_lt h.lt).elim (zero_le _), Ξ» e, e.symm β–Έ NF_below.zero⟩ theorem NF.zero_of_zero {e n a} (h : NF (oadd e n a)) (e0 : e = 0) : a = 0 := by simpa [e0, NF_below_zero] using h.snd' theorem NF_below.repr_lt {o b} (h : NF_below o b) : repr o < Ο‰ ^ b := begin induction h with _ e n a eb b h₁ hβ‚‚ h₃ _ IH, { exact power_pos _ omega_pos }, { rw repr, refine lt_of_lt_of_le ((ordinal.add_lt_add_iff_left _).2 IH) _, rw ← mul_succ, refine le_trans (mul_le_mul_left _ $ ordinal.succ_le.2 $ nat_lt_omega _) _, rw ← power_succ, exact power_le_power_right omega_pos (ordinal.succ_le.2 h₃) } end theorem NF_below.mono {o b₁ bβ‚‚} (bb : b₁ ≀ bβ‚‚) (h : NF_below o b₁) : NF_below o bβ‚‚ := begin induction h with _ e n a eb b h₁ hβ‚‚ h₃ _ IH; constructor, exacts [h₁, hβ‚‚, lt_of_lt_of_le h₃ bb] end theorem NF.below_of_lt {e n a b} (H : repr e < b) : NF (oadd e n a) β†’ NF_below (oadd e n a) b | ⟨b', h⟩ := by cases h with _ _ _ _ eb _ h₁ hβ‚‚ h₃; exact NF_below.oadd' h₁ hβ‚‚ H theorem NF.below_of_lt' : βˆ€ {o b}, repr o < Ο‰ ^ b β†’ NF o β†’ NF_below o b | 0 b H _ := NF_below.zero | (oadd e n a) b H h := h.below_of_lt $ (power_lt_power_iff_right one_lt_omega).1 $ (lt_of_le_of_lt (omega_le_oadd _ _ _) H) theorem NF_below_of_nat : βˆ€ n, NF_below (of_nat n) 1 | 0 := NF_below.zero | (nat.succ n) := NF_below.oadd NF.zero NF_below.zero ordinal.zero_lt_one instance NF_of_nat (n) : NF (of_nat n) := ⟨_, NF_below_of_nat n⟩ instance NF_one : NF 1 := by rw ← of_nat_one; apply_instance theorem oadd_lt_oadd_1 {e₁ n₁ o₁ eβ‚‚ nβ‚‚ oβ‚‚} (h₁ : NF (oadd e₁ n₁ o₁)) (hβ‚‚ : NF (oadd eβ‚‚ nβ‚‚ oβ‚‚)) (h : e₁ < eβ‚‚) : oadd e₁ n₁ o₁ < oadd eβ‚‚ nβ‚‚ oβ‚‚ := @lt_of_lt_of_le _ _ _ _ _ ((h₁.below_of_lt h).repr_lt) (omega_le_oadd _ _ _) theorem oadd_lt_oadd_2 {e n₁ o₁ nβ‚‚ oβ‚‚} (h₁ : NF (oadd e n₁ o₁)) (hβ‚‚ : NF (oadd e nβ‚‚ oβ‚‚)) (h : (n₁:β„•) < nβ‚‚) : oadd e n₁ o₁ < oadd e nβ‚‚ oβ‚‚ := begin simp [lt_def], refine lt_of_lt_of_le ((ordinal.add_lt_add_iff_left _).2 h₁.snd'.repr_lt) (le_trans _ (le_add_right _ _)), rwa [← mul_succ, mul_le_mul_iff_left (power_pos _ omega_pos), ordinal.succ_le, nat_cast_lt] end theorem oadd_lt_oadd_3 {e n a₁ aβ‚‚} (h₁ : NF (oadd e n a₁)) (hβ‚‚ : NF (oadd e n aβ‚‚)) (h : a₁ < aβ‚‚) : oadd e n a₁ < oadd e n aβ‚‚ := begin rw lt_def, unfold repr, exact (ordinal.add_lt_add_iff_left _).2 h end theorem cmp_compares : βˆ€ (a b : onote) [NF a] [NF b], (cmp a b).compares a b | 0 0 h₁ hβ‚‚ := rfl | (oadd e n a) 0 h₁ hβ‚‚ := oadd_pos _ _ _ | 0 (oadd e n a) h₁ hβ‚‚ := oadd_pos _ _ _ | o₁@(oadd e₁ n₁ a₁) oβ‚‚@(oadd eβ‚‚ nβ‚‚ aβ‚‚) h₁ hβ‚‚ := begin rw cmp, have IHe := @cmp_compares _ _ h₁.fst hβ‚‚.fst, cases cmp e₁ eβ‚‚, case ordering.lt { exact oadd_lt_oadd_1 h₁ hβ‚‚ IHe }, case ordering.gt { exact oadd_lt_oadd_1 hβ‚‚ h₁ IHe }, change e₁ = eβ‚‚ at IHe, subst IHe, unfold _root_.cmp, cases nh : cmp_using (<) (n₁:β„•) nβ‚‚, case ordering.lt { rw cmp_using_eq_lt at nh, exact oadd_lt_oadd_2 h₁ hβ‚‚ nh }, case ordering.gt { rw cmp_using_eq_gt at nh, exact oadd_lt_oadd_2 hβ‚‚ h₁ nh }, rw cmp_using_eq_eq at nh, have := subtype.eq (eq_of_incomp nh), subst nβ‚‚, have IHa := @cmp_compares _ _ h₁.snd hβ‚‚.snd, cases cmp a₁ aβ‚‚, case ordering.lt { exact oadd_lt_oadd_3 h₁ hβ‚‚ IHa }, case ordering.gt { exact oadd_lt_oadd_3 hβ‚‚ h₁ IHa }, change a₁ = aβ‚‚ at IHa, subst IHa, exact rfl end theorem repr_inj {a b} [NF a] [NF b] : repr a = repr b ↔ a = b := ⟨match cmp a b, cmp_compares a b with | ordering.lt, (h : repr a < repr b), e := (ne_of_lt h e).elim | ordering.gt, (h : repr a > repr b), e := (ne_of_gt h e).elim | ordering.eq, h, e := h end, congr_arg _⟩ theorem NF.of_dvd_omega_power {b e n a} (h : NF (oadd e n a)) (d : Ο‰ ^ b ∣ repr (oadd e n a)) : b ≀ repr e ∧ Ο‰ ^ b ∣ repr a := begin have := mt repr_inj.1 (Ξ» h, by injection h : oadd e n a β‰  0), have L := le_of_not_lt (Ξ» l, not_le_of_lt (h.below_of_lt l).repr_lt (le_of_dvd this d)), simp at d, exact ⟨L, (dvd_add_iff $ dvd_mul_of_dvd _ $ power_dvd_power _ L).1 d⟩ end theorem NF.of_dvd_omega {e n a} (h : NF (oadd e n a)) : Ο‰ ∣ repr (oadd e n a) β†’ repr e β‰  0 ∧ Ο‰ ∣ repr a := by rw [← power_one Ο‰, ← one_le_iff_ne_zero]; exact h.of_dvd_omega_power /-- `top_below b o` asserts that the largest exponent in `o`, if it exists, is less than `b`. This is an auxiliary definition for decidability of `NF`. -/ def top_below (b) : onote β†’ Prop | 0 := true | (oadd e n a) := cmp e b = ordering.lt instance decidable_top_below : decidable_rel top_below := by intros b o; cases o; delta top_below; apply_instance theorem NF_below_iff_top_below {b} [NF b] : βˆ€ {o}, NF_below o (repr b) ↔ NF o ∧ top_below b o | 0 := ⟨λ h, ⟨⟨_, h⟩, trivial⟩, Ξ» _, NF_below.zero⟩ | (oadd e n a) := ⟨λ h, ⟨⟨_, h⟩, (@cmp_compares _ b h.fst _).eq_lt.2 h.lt⟩, Ξ» ⟨h₁, hβ‚‚βŸ©, h₁.below_of_lt $ (@cmp_compares _ b h₁.fst _).eq_lt.1 hβ‚‚βŸ© instance decidable_NF : decidable_pred NF | 0 := is_true NF.zero | (oadd e n a) := begin have := decidable_NF e, have := decidable_NF a, resetI, apply decidable_of_iff (NF e ∧ NF a ∧ top_below e a), abstract { rw ← and_congr_right (Ξ» h, @NF_below_iff_top_below _ h _), exact ⟨λ ⟨h₁, hβ‚‚βŸ©, NF.oadd h₁ n hβ‚‚, Ξ» h, ⟨h.fst, h.snd'⟩⟩ }, end /-- Addition of ordinal notations (correct only for normal input) -/ def add : onote β†’ onote β†’ onote | 0 o := o | (oadd e n a) o := match add a o with | 0 := oadd e n 0 | o'@(oadd e' n' a') := match cmp e e' with | ordering.lt := o' | ordering.eq := oadd e (n + n') a' | ordering.gt := oadd e n o' end end instance : has_add onote := ⟨add⟩ @[simp] theorem zero_add (o : onote) : 0 + o = o := rfl theorem oadd_add (e n a o) : oadd e n a + o = add._match_1 e n (a + o) := rfl /-- Subtraction of ordinal notations (correct only for normal input) -/ def sub : onote β†’ onote β†’ onote | 0 o := 0 | o 0 := o | o₁@(oadd e₁ n₁ a₁) (oadd eβ‚‚ nβ‚‚ aβ‚‚) := match cmp e₁ eβ‚‚ with | ordering.lt := 0 | ordering.gt := o₁ | ordering.eq := match (n₁:β„•) - nβ‚‚ with | 0 := if n₁ = nβ‚‚ then sub a₁ aβ‚‚ else 0 | (nat.succ k) := oadd e₁ k.succ_pnat a₁ end end instance : has_sub onote := ⟨sub⟩ theorem add_NF_below {b} : βˆ€ {o₁ oβ‚‚}, NF_below o₁ b β†’ NF_below oβ‚‚ b β†’ NF_below (o₁ + oβ‚‚) b | 0 o h₁ hβ‚‚ := hβ‚‚ | (oadd e n a) o h₁ hβ‚‚ := begin have h' := add_NF_below (h₁.snd.mono $ le_of_lt h₁.lt) hβ‚‚, simp [oadd_add], cases a + o with e' n' a', { exact NF_below.oadd h₁.fst NF_below.zero h₁.lt }, simp [add], have := @cmp_compares _ _ h₁.fst h'.fst, cases cmp e e'; simp [add], { exact h' }, { simp at this, subst e', exact NF_below.oadd h'.fst h'.snd h'.lt }, { exact NF_below.oadd h₁.fst (NF.below_of_lt this ⟨_, h'⟩) h₁.lt } end instance add_NF (o₁ oβ‚‚) : βˆ€ [NF o₁] [NF oβ‚‚], NF (o₁ + oβ‚‚) | ⟨b₁, hβ‚βŸ© ⟨bβ‚‚, hβ‚‚βŸ© := (b₁.le_total bβ‚‚).elim (Ξ» h, ⟨bβ‚‚, add_NF_below (h₁.mono h) hβ‚‚βŸ©) (Ξ» h, ⟨b₁, add_NF_below h₁ (hβ‚‚.mono h)⟩) @[simp] theorem repr_add : βˆ€ o₁ oβ‚‚ [NF o₁] [NF oβ‚‚], repr (o₁ + oβ‚‚) = repr o₁ + repr oβ‚‚ | 0 o h₁ hβ‚‚ := by simp | (oadd e n a) o h₁ hβ‚‚ := begin haveI := h₁.snd, have h' := repr_add a o, conv at h' in (_+o) {simp [(+)]}, have nf := onote.add_NF a o, conv at nf in (_+o) {simp [(+)]}, conv in (_+o) {simp [(+), add]}, cases add a o with e' n' a'; simp [add, h'.symm], have := h₁.fst, have := nf.fst, have ee := cmp_compares e e', cases cmp e e'; simp [add], { rw [← add_assoc, @add_absorp _ (repr e') (Ο‰ ^ repr e' * (n':β„•))], { have := (h₁.below_of_lt ee).repr_lt, unfold repr at this, exact lt_of_le_of_lt (le_add_right _ _) this }, { simpa using (mul_le_mul_iff_left $ power_pos (repr e') omega_pos).2 (nat_cast_le.2 n'.pos) } }, { change e = e' at ee, subst e', rw [← add_assoc, ← ordinal.mul_add, ← nat.cast_add] } end theorem sub_NF_below : βˆ€ {o₁ oβ‚‚ b}, NF_below o₁ b β†’ NF oβ‚‚ β†’ NF_below (o₁ - oβ‚‚) b | 0 o b h₁ hβ‚‚ := by cases o; exact NF_below.zero | (oadd e n a) 0 b h₁ hβ‚‚ := h₁ | (oadd e₁ n₁ a₁) (oadd eβ‚‚ nβ‚‚ aβ‚‚) b h₁ hβ‚‚ := begin have h' := sub_NF_below h₁.snd hβ‚‚.snd, simp [has_sub.sub, sub] at h' ⊒, have := @cmp_compares _ _ h₁.fst hβ‚‚.fst, cases cmp e₁ eβ‚‚; simp [sub], { apply NF_below.zero }, { simp at this, subst eβ‚‚, cases mn : (n₁:β„•) - nβ‚‚; simp [sub], { by_cases en : n₁ = nβ‚‚; simp [en], { exact h'.mono (le_of_lt h₁.lt) }, { exact NF_below.zero } }, { exact NF_below.oadd h₁.fst h₁.snd h₁.lt } }, { exact h₁ } end instance sub_NF (o₁ oβ‚‚) : βˆ€ [NF o₁] [NF oβ‚‚], NF (o₁ - oβ‚‚) | ⟨b₁, hβ‚βŸ© hβ‚‚ := ⟨b₁, sub_NF_below h₁ hβ‚‚βŸ© @[simp] theorem repr_sub : βˆ€ o₁ oβ‚‚ [NF o₁] [NF oβ‚‚], repr (o₁ - oβ‚‚) = repr o₁ - repr oβ‚‚ | 0 o h₁ hβ‚‚ := by cases o; exact (ordinal.zero_sub _).symm | (oadd e n a) 0 h₁ hβ‚‚ := (ordinal.sub_zero _).symm | (oadd e₁ n₁ a₁) (oadd eβ‚‚ nβ‚‚ aβ‚‚) h₁ hβ‚‚ := begin haveI := h₁.snd, haveI := hβ‚‚.snd, have h' := repr_sub a₁ aβ‚‚, conv at h' in (a₁-aβ‚‚) {simp [has_sub.sub]}, have nf := onote.sub_NF a₁ aβ‚‚, conv at nf in (a₁-aβ‚‚) {simp [has_sub.sub]}, conv in (_-oadd _ _ _) {simp [has_sub.sub, sub]}, have ee := @cmp_compares _ _ h₁.fst hβ‚‚.fst, cases cmp e₁ eβ‚‚, { rw [sub_eq_zero_iff_le.2], {refl}, exact le_of_lt (oadd_lt_oadd_1 h₁ hβ‚‚ ee) }, { change e₁ = eβ‚‚ at ee, subst eβ‚‚, unfold sub._match_1, cases mn : (n₁:β„•) - nβ‚‚; dsimp only [sub._match_2], { by_cases en : n₁ = nβ‚‚, { simp [en], rwa [add_sub_add_cancel] }, { simp [en, -repr], exact (sub_eq_zero_iff_le.2 $ le_of_lt $ oadd_lt_oadd_2 h₁ hβ‚‚ $ lt_of_le_of_ne (nat.sub_eq_zero_iff_le.1 mn) (mt pnat.eq en)).symm } }, { simp [nat.succ_pnat, -nat.cast_succ], rw [(nat.sub_eq_iff_eq_add $ le_of_lt $ nat.lt_of_sub_eq_succ mn).1 mn, add_comm, nat.cast_add, ordinal.mul_add, add_assoc, add_sub_add_cancel], refine (ordinal.sub_eq_of_add_eq $ add_absorp hβ‚‚.snd'.repr_lt $ le_trans _ (le_add_right _ _)).symm, simpa using mul_le_mul_left _ (nat_cast_le.2 $ nat.succ_pos _) } }, { exact (ordinal.sub_eq_of_add_eq $ add_absorp (hβ‚‚.below_of_lt ee).repr_lt $ omega_le_oadd _ _ _).symm } end /-- Multiplication of ordinal notations (correct only for normal input) -/ def mul : onote β†’ onote β†’ onote | 0 _ := 0 | _ 0 := 0 | o₁@(oadd e₁ n₁ a₁) (oadd eβ‚‚ nβ‚‚ aβ‚‚) := if eβ‚‚ = 0 then oadd e₁ (n₁ * nβ‚‚) a₁ else oadd (e₁ + eβ‚‚) nβ‚‚ (mul o₁ aβ‚‚) instance : has_mul onote := ⟨mul⟩ @[simp] theorem zero_mul (o : onote) : 0 * o = 0 := by cases o; refl @[simp] theorem mul_zero (o : onote) : o * 0 = 0 := by cases o; refl theorem oadd_mul (e₁ n₁ a₁ eβ‚‚ nβ‚‚ aβ‚‚) : oadd e₁ n₁ a₁ * oadd eβ‚‚ nβ‚‚ aβ‚‚ = if eβ‚‚ = 0 then oadd e₁ (n₁ * nβ‚‚) a₁ else oadd (e₁ + eβ‚‚) nβ‚‚ (oadd e₁ n₁ a₁ * aβ‚‚) := rfl theorem oadd_mul_NF_below {e₁ n₁ a₁ b₁} (h₁ : NF_below (oadd e₁ n₁ a₁) b₁) : βˆ€ {oβ‚‚ bβ‚‚}, NF_below oβ‚‚ bβ‚‚ β†’ NF_below (oadd e₁ n₁ a₁ * oβ‚‚) (repr e₁ + bβ‚‚) | 0 bβ‚‚ hβ‚‚ := NF_below.zero | (oadd eβ‚‚ nβ‚‚ aβ‚‚) bβ‚‚ hβ‚‚ := begin have IH := oadd_mul_NF_below hβ‚‚.snd, by_cases e0 : eβ‚‚ = 0; simp [e0, oadd_mul], { apply NF_below.oadd h₁.fst h₁.snd, simpa using (add_lt_add_iff_left (repr e₁)).2 (lt_of_le_of_lt (ordinal.zero_le _) hβ‚‚.lt) }, { haveI := h₁.fst, haveI := hβ‚‚.fst, apply NF_below.oadd, apply_instance, { rwa repr_add }, { rw [repr_add, ordinal.add_lt_add_iff_left], exact hβ‚‚.lt } } end instance mul_NF : βˆ€ o₁ oβ‚‚ [NF o₁] [NF oβ‚‚], NF (o₁ * oβ‚‚) | 0 o h₁ hβ‚‚ := by cases o; exact NF.zero | (oadd e n a) o ⟨b₁, hbβ‚βŸ© ⟨bβ‚‚, hbβ‚‚βŸ© := ⟨_, oadd_mul_NF_below hb₁ hbβ‚‚βŸ© @[simp] theorem repr_mul : βˆ€ o₁ oβ‚‚ [NF o₁] [NF oβ‚‚], repr (o₁ * oβ‚‚) = repr o₁ * repr oβ‚‚ | 0 o h₁ hβ‚‚ := by cases o; exact (ordinal.zero_mul _).symm | (oadd e₁ n₁ a₁) 0 h₁ hβ‚‚ := (ordinal.mul_zero _).symm | (oadd e₁ n₁ a₁) (oadd eβ‚‚ nβ‚‚ aβ‚‚) h₁ hβ‚‚ := begin have IH : repr (mul _ _) = _ := @repr_mul _ _ h₁ hβ‚‚.snd, conv {to_lhs, simp [(*)]}, have ao : repr a₁ + Ο‰ ^ repr e₁ * (n₁:β„•) = Ο‰ ^ repr e₁ * (n₁:β„•), { apply add_absorp h₁.snd'.repr_lt, simpa using (mul_le_mul_iff_left $ power_pos _ omega_pos).2 (nat_cast_le.2 n₁.2) }, by_cases e0 : eβ‚‚ = 0; simp [e0, mul], { cases nat.exists_eq_succ_of_ne_zero nβ‚‚.ne_zero with x xe, simp [hβ‚‚.zero_of_zero e0, xe, -nat.cast_succ], rw [← nat_cast_succ x, add_mul_succ _ ao, mul_assoc] }, { haveI := h₁.fst, haveI := hβ‚‚.fst, simp [IH, repr_add, power_add, ordinal.mul_add], rw ← mul_assoc, congr' 2, have := mt repr_inj.1 e0, rw [add_mul_limit ao (power_is_limit_left omega_is_limit this), mul_assoc, mul_omega_dvd (nat_cast_pos.2 n₁.pos) (nat_lt_omega _)], simpa using power_dvd_power Ο‰ (one_le_iff_ne_zero.2 this) }, end /-- Calculate division and remainder of `o` mod Ο‰. `split' o = (a, n)` means `o = Ο‰ * a + n`. -/ def split' : onote β†’ onote Γ— β„• | 0 := (0, 0) | (oadd e n a) := if e = 0 then (0, n) else let (a', m) := split' a in (oadd (e - 1) n a', m) /-- Calculate division and remainder of `o` mod Ο‰. `split o = (a, n)` means `o = a + n`, where `Ο‰ ∣ a`. -/ def split : onote β†’ onote Γ— β„• | 0 := (0, 0) | (oadd e n a) := if e = 0 then (0, n) else let (a', m) := split a in (oadd e n a', m) /-- `scale x o` is the ordinal notation for `Ο‰ ^ x * o`. -/ def scale (x : onote) : onote β†’ onote | 0 := 0 | (oadd e n a) := oadd (x + e) n (scale a) /-- `mul_nat o n` is the ordinal notation for `o * n`. -/ def mul_nat : onote β†’ β„• β†’ onote | 0 m := 0 | _ 0 := 0 | (oadd e n a) (m+1) := oadd e (n * m.succ_pnat) a def power_aux (e a0 a : onote) : β„• β†’ β„• β†’ onote | _ 0 := 0 | 0 (m+1) := oadd e m.succ_pnat 0 | (k+1) m := scale (e + mul_nat a0 k) a + power_aux k m /-- `power o₁ oβ‚‚` calculates the ordinal notation for the ordinal exponential `o₁ ^ oβ‚‚`. -/ def power (o₁ oβ‚‚ : onote) : onote := match split o₁ with | (0, 0) := if oβ‚‚ = 0 then 1 else 0 | (0, 1) := 1 | (0, m+1) := let (b', k) := split' oβ‚‚ in oadd b' (@has_pow.pow β„•+ _ _ m.succ_pnat k) 0 | (a@(oadd a0 _ _), m) := match split oβ‚‚ with | (b, 0) := oadd (a0 * b) 1 0 | (b, k+1) := let eb := a0*b in scale (eb + mul_nat a0 k) a + power_aux eb a0 (mul_nat a m) k m end end instance : has_pow onote onote := ⟨power⟩ theorem power_def (o₁ oβ‚‚ : onote) : o₁ ^ oβ‚‚ = power._match_1 oβ‚‚ (split o₁) := rfl theorem split_eq_scale_split' : βˆ€ {o o' m} [NF o], split' o = (o', m) β†’ split o = (scale 1 o', m) | 0 o' m h p := by injection p; substs o' m; refl | (oadd e n a) o' m h p := begin by_cases e0 : e = 0; simp [e0, split, split'] at p ⊒, { rcases p with ⟨rfl, rfl⟩, exact ⟨rfl, rfl⟩ }, { revert p, cases h' : split' a with a' m', haveI := h.fst, haveI := h.snd, simp [split_eq_scale_split' h', split, split'], have : 1 + (e - 1) = e, { refine repr_inj.1 _, simp, have := mt repr_inj.1 e0, exact add_sub_cancel_of_le (one_le_iff_ne_zero.2 this) }, intros, substs o' m, simp [scale, this] } end theorem NF_repr_split' : βˆ€ {o o' m} [NF o], split' o = (o', m) β†’ NF o' ∧ repr o = Ο‰ * repr o' + m | 0 o' m h p := by injection p; substs o' m; simp [NF.zero] | (oadd e n a) o' m h p := begin by_cases e0 : e = 0; simp [e0, split, split'] at p ⊒, { rcases p with ⟨rfl, rfl⟩, simp [h.zero_of_zero e0, NF.zero] }, { revert p, cases h' : split' a with a' m', haveI := h.fst, haveI := h.snd, cases NF_repr_split' h' with IH₁ IHβ‚‚, simp [IHβ‚‚, split'], intros, substs o' m, have : Ο‰ ^ repr e = Ο‰ ^ (1 : ordinal.{0}) * Ο‰ ^ (repr e - 1), { have := mt repr_inj.1 e0, rw [← power_add, add_sub_cancel_of_le (one_le_iff_ne_zero.2 this)] }, refine ⟨NF.oadd (by apply_instance) _ _, _⟩, { simp at this ⊒, refine IH₁.below_of_lt' ((mul_lt_mul_iff_left omega_pos).1 $ lt_of_le_of_lt (le_add_right _ m') _), rw [← this, ← IHβ‚‚], exact h.snd'.repr_lt }, { rw this, simp [ordinal.mul_add, mul_assoc] } } end theorem scale_eq_mul (x) [NF x] : βˆ€ o [NF o], scale x o = oadd x 1 0 * o | 0 h := rfl | (oadd e n a) h := begin simp [(*)], simp [mul, scale], haveI := h.snd, by_cases e0 : e = 0, { rw scale_eq_mul, simp [e0, h.zero_of_zero, show x + 0 = x, from repr_inj.1 (by simp)] }, { simp [e0, scale_eq_mul, (*)] } end instance NF_scale (x) [NF x] (o) [NF o] : NF (scale x o) := by rw scale_eq_mul; apply_instance @[simp] theorem repr_scale (x) [NF x] (o) [NF o] : repr (scale x o) = Ο‰ ^ repr x * repr o := by simp [scale_eq_mul] theorem NF_repr_split {o o' m} [NF o] (h : split o = (o', m)) : NF o' ∧ repr o = repr o' + m := begin cases e : split' o with a n, cases NF_repr_split' e with s₁ sβ‚‚, resetI, rw split_eq_scale_split' e at h, injection h, substs o' n, simp [repr_scale, sβ‚‚.symm], apply_instance end theorem split_dvd {o o' m} [NF o] (h : split o = (o', m)) : Ο‰ ∣ repr o' := begin cases e : split' o with a n, rw split_eq_scale_split' e at h, injection h, subst o', cases NF_repr_split' e, resetI, simp [dvd_mul] end theorem split_add_lt {o e n a m} [NF o] (h : split o = (oadd e n a, m)) : repr a + m < Ο‰ ^ repr e := begin cases NF_repr_split h with h₁ hβ‚‚, cases h₁.of_dvd_omega (split_dvd h) with e0 d, have := h₁.fst, have := h₁.snd, refine add_lt_omega_power h₁.snd'.repr_lt (lt_of_lt_of_le (nat_lt_omega _) _), simpa using power_le_power_right omega_pos (one_le_iff_ne_zero.2 e0), end @[simp] theorem mul_nat_eq_mul (n o) : mul_nat o n = o * of_nat n := by cases o; cases n; refl instance NF_mul_nat (o) [NF o] (n) : NF (mul_nat o n) := by simp; apply_instance instance NF_power_aux (e a0 a) [NF e] [NF a0] [NF a] : βˆ€ k m, NF (power_aux e a0 a k m) | k 0 := by cases k; exact NF.zero | 0 (m+1) := NF.oadd_zero _ _ | (k+1) (m+1) := by haveI := NF_power_aux k; simp [power_aux, nat.succ_ne_zero]; apply_instance instance NF_power (o₁ oβ‚‚) [NF o₁] [NF oβ‚‚] : NF (o₁ ^ oβ‚‚) := begin cases e₁ : split o₁ with a m, have na := (NF_repr_split e₁).1, cases eβ‚‚ : split' oβ‚‚ with b' k, haveI := (NF_repr_split' eβ‚‚).1, cases a with a0 n a', { cases m with m, { by_cases oβ‚‚ = 0; simp [pow, power, e₁, h]; apply_instance }, { by_cases m = 0; simp [pow, power, e₁, eβ‚‚, h]; apply_instance } }, { simp [pow, power, e₁, eβ‚‚, split_eq_scale_split' eβ‚‚], have := na.fst, cases k with k; simp [succ_eq_add_one, power]; apply_instance } end theorem scale_power_aux (e a0 a : onote) [NF e] [NF a0] [NF a] : βˆ€ k m, repr (power_aux e a0 a k m) = Ο‰ ^ repr e * repr (power_aux 0 a0 a k m) | 0 m := by cases m; simp [power_aux] | (k+1) m := by by_cases m = 0; simp [h, power_aux, ordinal.mul_add, power_add, mul_assoc, scale_power_aux] theorem repr_power_aux₁ {e a} [Ne : NF e] [Na : NF a] {a' : ordinal} (e0 : repr e β‰  0) (h : a' < Ο‰ ^ repr e) (aa : repr a = a') (n : β„•+) : (Ο‰ ^ repr e * (n:β„•) + a') ^ Ο‰ = (Ο‰ ^ repr e) ^ Ο‰ := begin subst aa, have No := Ne.oadd n (Na.below_of_lt' h), have := omega_le_oadd e n a, unfold repr at this, refine le_antisymm _ (power_le_power_left _ this), apply (power_le_of_limit (ne_of_gt $ lt_of_lt_of_le (power_pos _ omega_pos) this) omega_is_limit).2, intros b l, have := (No.below_of_lt (lt_succ_self _)).repr_lt, unfold repr at this, apply le_trans (power_le_power_left b $ le_of_lt this), rw [← power_mul, ← power_mul], apply power_le_power_right omega_pos, cases le_or_lt Ο‰ (repr e) with h h, { apply le_trans (mul_le_mul_left _ $ le_of_lt $ lt_succ_self _), rw [succ, add_mul_succ _ (one_add_of_omega_le h), ← succ, succ_le, mul_lt_mul_iff_left (pos_iff_ne_zero.2 e0)], exact omega_is_limit.2 _ l }, { refine le_trans (le_of_lt $ mul_lt_omega (omega_is_limit.2 _ h) l) _, simpa using mul_le_mul_right Ο‰ (one_le_iff_ne_zero.2 e0) } end section local infixr ^ := @pow ordinal.{0} ordinal ordinal.has_pow theorem repr_power_auxβ‚‚ {a0 a'} [N0 : NF a0] [Na' : NF a'] (m : β„•) (d : Ο‰ ∣ repr a') (e0 : repr a0 β‰  0) (h : repr a' + m < Ο‰ ^ repr a0) (n : β„•+) (k : β„•) : let R := repr (power_aux 0 a0 (oadd a0 n a' * of_nat m) k m) in (k β‰  0 β†’ R < (Ο‰ ^ repr a0) ^ succ k) ∧ (Ο‰ ^ repr a0) ^ k * (Ο‰ ^ repr a0 * (n:β„•) + repr a') + R = (Ο‰ ^ repr a0 * (n:β„•) + repr a' + m) ^ succ k := begin intro, haveI No : NF (oadd a0 n a') := N0.oadd n (Na'.below_of_lt' $ lt_of_le_of_lt (le_add_right _ _) h), induction k with k IH, {cases m; simp [power_aux, R]}, rename R R', let R := repr (power_aux 0 a0 (oadd a0 n a' * of_nat m) k m), let Ο‰0 := Ο‰ ^ repr a0, let Ξ±' := Ο‰0 * n + repr a', change (k β‰  0 β†’ R < Ο‰0 ^ succ k) ∧ Ο‰0 ^ k * Ξ±' + R = (Ξ±' + m) ^ succ k at IH, have RR : R' = Ο‰0 ^ k * (Ξ±' * m) + R, { by_cases m = 0; simp [h, R', power_aux, R, power_mul], { cases k; simp [power_aux] }, { refl } }, have Ξ±0 : 0 < Ξ±', {simpa [Ξ±', lt_def, repr] using oadd_pos a0 n a'}, have Ο‰00 : 0 < Ο‰0 ^ k := power_pos _ (power_pos _ omega_pos), have Rl : R < Ο‰ ^ (repr a0 * succ ↑k), { by_cases k0 : k = 0, { simp [k0], refine lt_of_lt_of_le _ (power_le_power_right omega_pos (one_le_iff_ne_zero.2 e0)), cases m with m; simp [k0, R, power_aux, omega_pos], rw [← nat.cast_succ], apply nat_lt_omega }, { rw power_mul, exact IH.1 k0 } }, refine ⟨λ_, _, _⟩, { rw [RR, ← power_mul _ _ (succ k.succ)], have e0 := pos_iff_ne_zero.2 e0, have rr0 := lt_of_lt_of_le e0 (le_add_left _ _), apply add_lt_omega_power, { simp [power_mul, Ο‰0, power_add], rw [mul_lt_mul_iff_left Ο‰00, ← ordinal.power_add], have := (No.below_of_lt _).repr_lt, unfold repr at this, refine mul_lt_omega_power rr0 this (nat_lt_omega _), simpa using (add_lt_add_iff_left (repr a0)).2 e0 }, { refine lt_of_lt_of_le Rl (power_le_power_right omega_pos $ mul_le_mul_left _ $ succ_le_succ.2 $ nat_cast_le.2 $ le_of_lt k.lt_succ_self) } }, refine calc Ο‰0 ^ k.succ * Ξ±' + R' = Ο‰0 ^ succ k * Ξ±' + (Ο‰0 ^ k * Ξ±' * m + R) : by rw [nat_cast_succ, RR, ← mul_assoc] ... = (Ο‰0 ^ k * Ξ±' + R) * Ξ±' + (Ο‰0 ^ k * Ξ±' + R) * m : _ ... = (Ξ±' + m) ^ succ k.succ : by rw [← ordinal.mul_add, ← nat_cast_succ, power_succ, IH.2], congr' 1, { have Ξ±d : Ο‰ ∣ Ξ±' := dvd_add (dvd_mul_of_dvd _ (by simpa using power_dvd_power Ο‰ (one_le_iff_ne_zero.2 e0))) d, rw [ordinal.mul_add (Ο‰0 ^ k), add_assoc, ← mul_assoc, ← power_succ, add_mul_limit _ (is_limit_iff_omega_dvd.2 ⟨ne_of_gt Ξ±0, Ξ±d⟩), mul_assoc, @mul_omega_dvd n (nat_cast_pos.2 n.pos) (nat_lt_omega _) _ Ξ±d], apply @add_absorp _ (repr a0 * succ k), { refine add_lt_omega_power _ Rl, rw [power_mul, power_succ, mul_lt_mul_iff_left Ο‰00], exact No.snd'.repr_lt }, { have := mul_le_mul_left (Ο‰0 ^ succ k) (one_le_iff_pos.2 $ nat_cast_pos.2 n.pos), rw power_mul, simpa [-power_succ] } }, { cases m, { have : R = 0, {cases k; simp [R, power_aux]}, simp [this] }, { rw [← nat_cast_succ, add_mul_succ], apply add_absorp Rl, rw [power_mul, power_succ], apply ordinal.mul_le_mul_left, simpa [Ξ±', repr] using omega_le_oadd a0 n a' } } end end theorem repr_power (o₁ oβ‚‚) [NF o₁] [NF oβ‚‚] : repr (o₁ ^ oβ‚‚) = repr o₁ ^ repr oβ‚‚ := begin cases e₁ : split o₁ with a m, cases NF_repr_split e₁ with N₁ r₁, cases a with a0 n a', { cases m with m, { by_cases oβ‚‚ = 0; simp [power_def, power, e₁, h, r₁], have := mt repr_inj.1 h, rw zero_power this }, { cases eβ‚‚ : split' oβ‚‚ with b' k, cases NF_repr_split' eβ‚‚ with _ rβ‚‚, by_cases m = 0; simp [power_def, power, e₁, h, r₁, eβ‚‚, rβ‚‚, -nat.cast_succ], rw [power_add, power_mul, power_omega _ (nat_lt_omega _)], simpa using nat_cast_lt.2 (nat.succ_lt_succ $ nat.pos_iff_ne_zero.2 h) } }, { haveI := N₁.fst, haveI := N₁.snd, cases N₁.of_dvd_omega (split_dvd e₁) with a00 ad, have al := split_add_lt e₁, have aa : repr (a' + of_nat m) = repr a' + m, {simp}, cases eβ‚‚ : split' oβ‚‚ with b' k, cases NF_repr_split' eβ‚‚ with _ rβ‚‚, simp [power_def, power, e₁, r₁, split_eq_scale_split' eβ‚‚], cases k with k, { simp [power, rβ‚‚, power_mul, repr_power_aux₁ a00 al aa] }, { simp [succ_eq_add_one, power, rβ‚‚, power_add, power_mul, mul_assoc], rw [repr_power_aux₁ a00 al aa, scale_power_aux], simp [power_mul], rw [← ordinal.mul_add, ← add_assoc (Ο‰ ^ repr a0 * (n:β„•))], congr' 1, rw [← power_succ], exact (repr_power_auxβ‚‚ _ ad a00 al _ _).2 } } end end onote /-- The type of normal ordinal notations. (It would have been nicer to define this right in the inductive type, but `NF o` requires `repr` which requires `onote`, so all these things would have to be defined at once, which messes up the VM representation.) -/ def nonote := {o : onote // o.NF} instance : decidable_eq nonote := by unfold nonote; apply_instance namespace nonote open onote instance NF (o : nonote) : NF o.1 := o.2 /-- Construct a `nonote` from an ordinal notation (and infer normality) -/ def mk (o : onote) [h : NF o] : nonote := ⟨o, h⟩ /-- The ordinal represented by an ordinal notation. (This function is noncomputable because ordinal arithmetic is noncomputable. In computational applications `nonote` can be used exclusively without reference to `ordinal`, but this function allows for correctness results to be stated.) -/ noncomputable def repr (o : nonote) : ordinal := o.1.repr instance : has_to_string nonote := ⟨λ x, x.1.to_string⟩ instance : has_repr nonote := ⟨λ x, x.1.repr'⟩ instance : preorder nonote := { le := Ξ» x y, repr x ≀ repr y, lt := Ξ» x y, repr x < repr y, le_refl := Ξ» a, @le_refl ordinal _ _, le_trans := Ξ» a b c, @le_trans ordinal _ _ _ _, lt_iff_le_not_le := Ξ» a b, @lt_iff_le_not_le ordinal _ _ _ } instance : has_zero nonote := ⟨⟨0, NF.zero⟩⟩ /-- Convert a natural number to an ordinal notation -/ def of_nat (n : β„•) : nonote := ⟨of_nat n, _, NF_below_of_nat _⟩ /-- Compare ordinal notations -/ def cmp (a b : nonote) : ordering := cmp a.1 b.1 theorem cmp_compares : βˆ€ a b : nonote, (cmp a b).compares a b | ⟨a, ha⟩ ⟨b, hb⟩ := begin resetI, dsimp [cmp], have := onote.cmp_compares a b, cases onote.cmp a b; try {exact this}, exact subtype.mk_eq_mk.2 this end instance : linear_order nonote := { le_antisymm := Ξ» a b, match cmp a b, cmp_compares a b with | ordering.lt, h, h₁, hβ‚‚ := (not_lt_of_le hβ‚‚).elim h | ordering.eq, h, h₁, hβ‚‚ := h | ordering.gt, h, h₁, hβ‚‚ := (not_lt_of_le h₁).elim h end, le_total := Ξ» a b, match cmp a b, cmp_compares a b with | ordering.lt, h := or.inl (le_of_lt h) | ordering.eq, h := or.inl (le_of_eq h) | ordering.gt, h := or.inr (le_of_lt h) end, ..nonote.preorder } instance decidable_lt : @decidable_rel nonote (<) | a b := decidable_of_iff _ (cmp_compares a b).eq_lt instance : decidable_linear_order nonote := { decidable_le := Ξ» a b, decidable_of_iff _ not_lt, decidable_lt := nonote.decidable_lt, ..nonote.linear_order } /-- Asserts that `repr a < Ο‰ ^ repr b`. Used in `nonote.rec_on` -/ def below (a b : nonote) : Prop := NF_below a.1 (repr b) /-- The `oadd` pseudo-constructor for `nonote` -/ def oadd (e : nonote) (n : β„•+) (a : nonote) (h : below a e) : nonote := ⟨_, NF.oadd e.2 n h⟩ /-- This is a recursor-like theorem for `nonote` suggesting an inductive definition, which can't actually be defined this way due to conflicting dependencies. -/ @[elab_as_eliminator] def rec_on {C : nonote β†’ Sort*} (o : nonote) (H0 : C 0) (H1 : βˆ€ e n a h, C e β†’ C a β†’ C (oadd e n a h)) : C o := begin cases o with o h, induction o with e n a IHe IHa, { exact H0 }, { exact H1 ⟨e, h.fst⟩ n ⟨a, h.snd⟩ h.snd' (IHe _) (IHa _) } end /-- Addition of ordinal notations -/ instance : has_add nonote := ⟨λ x y, mk (x.1 + y.1)⟩ theorem repr_add (a b) : repr (a + b) = repr a + repr b := onote.repr_add a.1 b.1 /-- Subtraction of ordinal notations -/ instance : has_sub nonote := ⟨λ x y, mk (x.1 - y.1)⟩ theorem repr_sub (a b) : repr (a - b) = repr a - repr b := onote.repr_sub a.1 b.1 /-- Multiplication of ordinal notations -/ instance : has_mul nonote := ⟨λ x y, mk (x.1 * y.1)⟩ theorem repr_mul (a b) : repr (a * b) = repr a * repr b := onote.repr_mul a.1 b.1 /-- Exponentiation of ordinal notations -/ def power (x y : nonote) := mk (x.1.power y.1) theorem repr_power (a b) : repr (power a b) = (repr a).power (repr b) := onote.repr_power a.1 b.1 end nonote
d74cc00d12a2acfcb8981a37cee9c0e47adbe8ef
f3849be5d845a1cb97680f0bbbe03b85518312f0
/library/init/data/string/basic.lean
23717a37c6fcdb5239ef9dd70ba8efc94226cf3d
[ "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
1,497
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import init.data.list.basic import init.data.char.basic def string := list char namespace string @[pattern] def empty : string := list.nil instance : inhabited string := ⟨empty⟩ instance : has_sizeof string := ⟨list.sizeof⟩ @[pattern] def str : char β†’ string β†’ string := list.cons def concat (a b : string) : string := list.append b a instance : has_append string := ⟨string.concat⟩ end string open list private def utf8_length_aux : nat β†’ nat β†’ string β†’ nat | 0 r (c::s) := let n := char.to_nat c in if n < 0x80 then utf8_length_aux 0 (r+1) s else if 0xC0 ≀ n ∧ n < 0xE0 then utf8_length_aux 1 (r+1) s else if 0xE0 ≀ n ∧ n < 0xF0 then utf8_length_aux 2 (r+1) s else if 0xF0 ≀ n ∧ n < 0xF8 then utf8_length_aux 3 (r+1) s else if 0xF8 ≀ n ∧ n < 0xFC then utf8_length_aux 4 (r+1) s else if 0xFC ≀ n ∧ n < 0xFE then utf8_length_aux 5 (r+1) s else utf8_length_aux 0 (r+1) s | (n+1) r (c::s) := utf8_length_aux n r s | n r [] := r def utf8_length : string β†’ nat | s := utf8_length_aux 0 0 (reverse s) private def to_nat_core : list char β†’ nat β†’ nat | [] r := r | (c::cs) r := to_nat_core cs (char.to_nat c - char.to_nat '0' + r*10) def string.to_nat (s : string) : nat := to_nat_core s.reverse 0
1453721dd2002b087faa5f9af7535399b03e4935
b561a44b48979a98df50ade0789a21c79ee31288
/stage0/src/Lean/Elab/PreDefinition/WF/PackDomain.lean
d609761d73cddf4ad35b065359e6fb1ac18dc9c7
[ "Apache-2.0" ]
permissive
3401ijk/lean4
97659c475ebd33a034fed515cb83a85f75ccfb06
a5b1b8de4f4b038ff752b9e607b721f15a9a4351
refs/heads/master
1,693,933,007,651
1,636,424,845,000
1,636,424,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,051
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Elab.PreDefinition.Basic namespace Lean.Elab.WF open Meta /-- Given a (dependent) tuple `t` (using `PSigma`) of the given arity. Return an array containing its "elements". Example: `mkTupleElems a 4` returns `#[a.1, a.2.1, a.2.2.1, a.2.2.2]`. -/ private def mkTupleElems (t : Expr) (arity : Nat) : Array Expr := do let mut result := #[] let mut t := t for i in [:arity - 1] do result := result.push (mkProj ``PSigma 0 t) t := mkProj ``PSigma 1 t result.push t /-- Create a unary application by packing the given arguments using `PSigma.mk` -/ partial def mkUnaryArg (type : Expr) (args : Array Expr) : MetaM Expr := do go 0 type where go (i : Nat) (type : Expr) : MetaM Expr := do if i < args.size - 1 then let arg := args[i] assert! type.isAppOfArity ``PSigma 2 let us := type.getAppFn.constLevels! let Ξ± := type.appFn!.appArg! let Ξ² := type.appArg! assert! Ξ².isLambda let type := Ξ².bindingBody!.instantiate1 arg let rest ← go (i+1) type return mkApp4 (mkConst ``PSigma.mk us) Ξ± Ξ² arg rest else return args[i] private partial def mkPSigmaCasesOn (y : Expr) (codomain : Expr) (xs : Array Expr) (value : Expr) : MetaM Expr := do let mvar ← mkFreshExprSyntheticOpaqueMVar codomain let rec go (mvarId : MVarId) (y : FVarId) (ys : Array Expr) : MetaM Unit := do if ys.size < xs.size - 1 then let #[s] ← cases mvarId y | unreachable! go s.mvarId s.fields[1].fvarId! (ys.push s.fields[0]) else let ys := ys.push (mkFVar y) assignExprMVar mvarId (value.replaceFVars xs ys) go mvar.mvarId! y.fvarId! #[] instantiateMVars mvar /-- Convert the given pre-definitions into unary functions. We "pack" the arguments using `PSigma`. -/ def packDomain (preDefs : Array PreDefinition) : MetaM (Array PreDefinition) := do let mut preDefsNew := #[] let mut arities := #[] let mut modified := false for preDef in preDefs do let (preDefNew, arity, modifiedCurr) ← lambdaTelescope preDef.value fun xs body => do if xs.size == 0 then throwError "well-founded recursion cannot be used, '{preDef.declName}' does not take any arguments" if xs.size > 1 then let bodyType ← instantiateForall preDef.type xs let mut d ← inferType xs.back for x in xs.pop.reverse do d ← mkAppOptM ``PSigma #[some (← inferType x), some (← mkLambdaFVars #[x] d)] withLocalDeclD (← mkFreshUserName `_x) d fun tuple => do let elems := mkTupleElems tuple xs.size let codomain := bodyType.replaceFVars xs elems let preDefNew:= { preDef with declName := preDef.declName ++ `_unary type := (← mkForallFVars #[tuple] codomain) } addAsAxiom preDefNew return (preDefNew, xs.size, true) else return (preDef, 1, false) modified := modified || modifiedCurr arities := arities.push arity preDefsNew := preDefsNew.push preDefNew if !modified then return preDefs /- Return `some i` if `e` is a `preDefs[i]` application -/ let isAppOfPreDef? (e : Expr) : OptionM Nat := do let f := e.getAppFn guard f.isConst preDefs.findIdx? (Β·.declName == f.constName!) /- Return `some i` if `e` is a `preDefs[i]` application with `arities[i]` arguments. -/ let isTargetApp? (e : Expr) : OptionM Nat := do let i ← isAppOfPreDef? e guard (e.getAppNumArgs == arities[i]) return i -- Update values for i in [:preDefs.size] do let preDef := preDefs[i] let preDefNew := preDefsNew[i] let arity := arities[i] let valueNew ← lambdaTelescope preDef.value fun xs body => do forallBoundedTelescope preDefNew.type (some 1) fun y codomain => do let y := y[0] let newBody ← mkPSigmaCasesOn y codomain xs body let visit (e : Expr) : MetaM TransformStep := do if let some idx := isTargetApp? e |>.run then let f := e.getAppFn let fNew := mkConst preDefsNew[idx].declName f.constLevels! let Expr.forallE _ d .. ← inferType fNew | unreachable! let argNew ← mkUnaryArg d e.getAppArgs return TransformStep.done <| mkApp fNew argNew else return TransformStep.done e mkLambdaFVars #[y] (← transform newBody (post := visit)) if let some bad := valueNew.find? fun e => (isAppOfPreDef? e).isSome && e.getAppNumArgs > 1 then if let some i := isAppOfPreDef? bad then throwErrorAt preDef.ref "well-founded recursion cannot be used, function '{preDef.declName}' contains application of function '{preDefs[i].declName}' with #{bad.getAppNumArgs} argument(s), but function has arity {arities[i]}" preDefsNew := preDefsNew.set! i { preDefNew with value := valueNew } return preDefsNew end Lean.Elab.WF
aa827736e1a69978d60dd404cc9ad3312eb78f36
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Data/Position.lean
bf46839a8bdc05b701c0a9d2f09f983ead8485c4
[ "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
2,884
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Data.Format namespace Lean structure Position := (line : Nat) (column : Nat) namespace Position instance : DecidableEq Position := fun ⟨l₁, cβ‚βŸ© ⟨lβ‚‚, cβ‚‚βŸ© => if h₁ : l₁ = lβ‚‚ then if hβ‚‚ : c₁ = cβ‚‚ then isTrue (Eq.recOn h₁ (Eq.recOn hβ‚‚ rfl)) else isFalse (fun contra => Position.noConfusion contra (fun e₁ eβ‚‚ => absurd eβ‚‚ hβ‚‚)) else isFalse (fun contra => Position.noConfusion contra (fun e₁ eβ‚‚ => absurd e₁ h₁)) protected def lt : Position β†’ Position β†’ Bool | ⟨l₁, cβ‚βŸ©, ⟨lβ‚‚, cβ‚‚βŸ© => (l₁, c₁) < (lβ‚‚, cβ‚‚) instance : HasFormat Position := ⟨fun ⟨l, c⟩ => "⟨" ++ fmt l ++ ", " ++ fmt c ++ "⟩"⟩ instance : HasToString Position := ⟨fun ⟨l, c⟩ => "⟨" ++ toString l ++ ", " ++ toString c ++ "⟩"⟩ instance : Inhabited Position := ⟨⟨1, 0⟩⟩ end Position structure FileMap := (source : String) (positions : Array String.Pos) (lines : Array Nat) namespace FileMap instance : Inhabited FileMap := ⟨{ source := "", positions := #[], lines := #[] }⟩ private partial def ofStringAux (s : String) : String.Pos β†’ Nat β†’ Array String.Pos β†’ Array Nat β†’ FileMap | i, line, ps, lines => if s.atEnd i then { source := s, positions := ps.push i, lines := lines.push line } else let c := s.get i; let i := s.next i; if c == '\n' then ofStringAux i (line+1) (ps.push i) (lines.push (line+1)) else ofStringAux i line ps lines def ofString (s : String) : FileMap := ofStringAux s 0 1 (#[0]) (#[1]) private partial def toColumnAux (str : String) (pos : String.Pos) : String.Pos β†’ Nat β†’ Nat | i, c => if i == pos || str.atEnd i then c else toColumnAux (str.next i) (c+1) /- Remark: `pos` is in `[ps.get b, ps.get e]` and `b < e` -/ private partial def toPositionAux (str : String) (ps : Array Nat) (lines : Array Nat) (pos : String.Pos) : Nat β†’ Nat β†’ Position | b, e => let posB := ps.get! b; if e == b + 1 then { line := lines.get! b, column := toColumnAux str pos posB 0 } else let m := (b + e) / 2; let posM := ps.get! m; if pos == posM then { line := lines.get! m, column := 0 } else if pos > posM then toPositionAux m e else toPositionAux b m def toPosition : FileMap β†’ String.Pos β†’ Position | { source := str, positions := ps, lines := lines }, pos => if ps.size >= 2 && pos <= ps.back then toPositionAux str ps lines pos 0 (ps.size-1) else -- Some systems like the delaborator use synthetic positions without an input file, -- which would violate `toPositionAux`'s invariant ⟨1, pos⟩ end FileMap end Lean def String.toFileMap (s : String) : Lean.FileMap := Lean.FileMap.ofString s
9af1998549733d172619a9582cbaac7fe2b4ce63
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/data/equiv/basic.lean
cdff2f98b48da2eb069bc953f341338e944687b3
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
47,114
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro In the standard library we cannot assume the univalence axiom. We say two types are equivalent if they are isomorphic. Two equivalent types have the same cardinality. -/ import tactic.split_ifs logic.function logic.unique data.set.function data.bool data.quot open function universes u v w z variables {Ξ± : Sort u} {Ξ² : Sort v} {Ξ³ : Sort w} /-- `Ξ± ≃ Ξ²` is the type of functions from `Ξ± β†’ Ξ²` with a two-sided inverse. -/ structure equiv (Ξ± : Sort*) (Ξ² : Sort*) := (to_fun : Ξ± β†’ Ξ²) (inv_fun : Ξ² β†’ Ξ±) (left_inv : left_inverse inv_fun to_fun) (right_inv : right_inverse inv_fun to_fun) infix ` ≃ `:25 := equiv def function.involutive.to_equiv {f : Ξ± β†’ Ξ±} (h : involutive f) : Ξ± ≃ Ξ± := ⟨f, f, h.left_inverse, h.right_inverse⟩ namespace equiv /-- `perm Ξ±` is the type of bijections from `Ξ±` to itself. -/ @[reducible] def perm (Ξ± : Sort*) := equiv Ξ± Ξ± instance : has_coe_to_fun (Ξ± ≃ Ξ²) := ⟨_, to_fun⟩ @[simp] theorem coe_fn_mk (f : Ξ± β†’ Ξ²) (g l r) : (equiv.mk f g l r : Ξ± β†’ Ξ²) = f := rfl theorem eq_of_to_fun_eq : βˆ€ {e₁ eβ‚‚ : equiv Ξ± Ξ²}, (e₁ : Ξ± β†’ Ξ²) = eβ‚‚ β†’ e₁ = eβ‚‚ | ⟨f₁, g₁, l₁, rβ‚βŸ© ⟨fβ‚‚, gβ‚‚, lβ‚‚, rβ‚‚βŸ© h := have f₁ = fβ‚‚, from h, have g₁ = gβ‚‚, from funext $ assume x, have f₁ (g₁ x) = fβ‚‚ (gβ‚‚ x), from (r₁ x).trans (rβ‚‚ x).symm, have f₁ (g₁ x) = f₁ (gβ‚‚ x), by { subst fβ‚‚, exact this }, show g₁ x = gβ‚‚ x, from injective_of_left_inverse l₁ this, by simp * @[ext] lemma ext (f g : equiv Ξ± Ξ²) (H : βˆ€ x, f x = g x) : f = g := eq_of_to_fun_eq (funext H) @[ext] lemma perm.ext (Οƒ Ο„ : equiv.perm Ξ±) (H : βˆ€ x, Οƒ x = Ο„ x) : Οƒ = Ο„ := equiv.ext _ _ H @[refl] protected def refl (Ξ± : Sort*) : Ξ± ≃ Ξ± := ⟨id, id, Ξ» x, rfl, Ξ» x, rfl⟩ @[symm] protected def symm (e : Ξ± ≃ Ξ²) : Ξ² ≃ Ξ± := ⟨e.inv_fun, e.to_fun, e.right_inv, e.left_inv⟩ @[trans] protected def trans (e₁ : Ξ± ≃ Ξ²) (eβ‚‚ : Ξ² ≃ Ξ³) : Ξ± ≃ Ξ³ := ⟨eβ‚‚.to_fun ∘ e₁.to_fun, e₁.inv_fun ∘ eβ‚‚.inv_fun, eβ‚‚.left_inv.comp e₁.left_inv, eβ‚‚.right_inv.comp e₁.right_inv⟩ @[simp] lemma to_fun_as_coe (e : Ξ± ≃ Ξ²) (a : Ξ±) : e.to_fun a = e a := rfl @[simp] lemma inv_fun_as_coe (e : Ξ± ≃ Ξ²) (b : Ξ²) : e.inv_fun b = e.symm b := rfl protected theorem injective : βˆ€ f : Ξ± ≃ Ξ², injective f | ⟨f, g, h₁, hβ‚‚βŸ© := injective_of_left_inverse h₁ protected theorem surjective : βˆ€ f : Ξ± ≃ Ξ², surjective f | ⟨f, g, h₁, hβ‚‚βŸ© := surjective_of_has_right_inverse ⟨_, hβ‚‚βŸ© protected theorem bijective (f : Ξ± ≃ Ξ²) : bijective f := ⟨f.injective, f.surjective⟩ @[simp] lemma range_eq_univ {Ξ± : Type*} {Ξ² : Type*} (e : Ξ± ≃ Ξ²) : set.range e = set.univ := set.eq_univ_of_forall e.surjective protected theorem subsingleton (e : Ξ± ≃ Ξ²) [subsingleton Ξ²] : subsingleton Ξ± := e.injective.comap_subsingleton protected def decidable_eq (e : Ξ± ≃ Ξ²) [H : decidable_eq Ξ²] : decidable_eq Ξ± | a b := decidable_of_iff _ e.injective.eq_iff lemma nonempty_iff_nonempty : Ξ± ≃ Ξ² β†’ (nonempty Ξ± ↔ nonempty Ξ²) | ⟨f, g, _, _⟩ := nonempty.congr f g protected def cast {Ξ± Ξ² : Sort*} (h : Ξ± = Ξ²) : Ξ± ≃ Ξ² := ⟨cast h, cast h.symm, Ξ» x, by { cases h, refl }, Ξ» x, by { cases h, refl }⟩ @[simp] theorem coe_fn_symm_mk (f : Ξ± β†’ Ξ²) (g l r) : ((equiv.mk f g l r).symm : Ξ² β†’ Ξ±) = g := rfl @[simp] theorem refl_apply (x : Ξ±) : equiv.refl Ξ± x = x := rfl @[simp] theorem trans_apply (f : Ξ± ≃ Ξ²) (g : Ξ² ≃ Ξ³) (a : Ξ±) : (f.trans g) a = g (f a) := rfl @[simp] theorem apply_symm_apply : βˆ€ (e : Ξ± ≃ Ξ²) (x : Ξ²), e (e.symm x) = x | ⟨f₁, g₁, l₁, rβ‚βŸ© x := by { simp [equiv.symm], rw r₁ } @[simp] theorem symm_apply_apply : βˆ€ (e : Ξ± ≃ Ξ²) (x : Ξ±), e.symm (e x) = x | ⟨f₁, g₁, l₁, rβ‚βŸ© x := by { simp [equiv.symm], rw l₁ } @[simp] lemma symm_trans_apply (f : Ξ± ≃ Ξ²) (g : Ξ² ≃ Ξ³) (a : Ξ³) : (f.trans g).symm a = f.symm (g.symm a) := rfl @[simp] theorem apply_eq_iff_eq : βˆ€ (f : Ξ± ≃ Ξ²) (x y : Ξ±), f x = f y ↔ x = y | ⟨f₁, g₁, l₁, rβ‚βŸ© x y := (injective_of_left_inverse l₁).eq_iff @[simp] theorem cast_apply {Ξ± Ξ²} (h : Ξ± = Ξ²) (x : Ξ±) : equiv.cast h x = cast h x := rfl lemma symm_apply_eq {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) {x y} : e.symm x = y ↔ x = e y := ⟨λ H, by simp [H.symm], Ξ» H, by simp [H]⟩ lemma eq_symm_apply {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) {x y} : y = e.symm x ↔ e y = x := (eq_comm.trans e.symm_apply_eq).trans eq_comm @[simp] theorem symm_symm (e : Ξ± ≃ Ξ²) : e.symm.symm = e := by { cases e, refl } @[simp] theorem symm_symm_apply (e : Ξ± ≃ Ξ²) (a : Ξ±) : e.symm.symm a = e a := by { cases e, refl } @[simp] theorem trans_refl (e : Ξ± ≃ Ξ²) : e.trans (equiv.refl Ξ²) = e := by { cases e, refl } @[simp] theorem refl_symm : (equiv.refl Ξ±).symm = equiv.refl Ξ± := rfl @[simp] theorem refl_trans (e : Ξ± ≃ Ξ²) : (equiv.refl Ξ±).trans e = e := by { cases e, refl } @[simp] theorem symm_trans (e : Ξ± ≃ Ξ²) : e.symm.trans e = equiv.refl Ξ² := ext _ _ (by simp) @[simp] theorem trans_symm (e : Ξ± ≃ Ξ²) : e.trans e.symm = equiv.refl Ξ± := ext _ _ (by simp) lemma trans_assoc {Ξ΄} (ab : Ξ± ≃ Ξ²) (bc : Ξ² ≃ Ξ³) (cd : Ξ³ ≃ Ξ΄) : (ab.trans bc).trans cd = ab.trans (bc.trans cd) := equiv.ext _ _ $ assume a, rfl theorem left_inverse_symm (f : equiv Ξ± Ξ²) : left_inverse f.symm f := f.left_inv theorem right_inverse_symm (f : equiv Ξ± Ξ²) : function.right_inverse f.symm f := f.right_inv def equiv_congr {Ξ΄} (ab : Ξ± ≃ Ξ²) (cd : Ξ³ ≃ Ξ΄) : (Ξ± ≃ Ξ³) ≃ (Ξ² ≃ Ξ΄) := ⟨ Ξ»ac, (ab.symm.trans ac).trans cd, Ξ»bd, ab.trans $ bd.trans $ cd.symm, assume ac, by { ext x, simp }, assume ac, by { ext x, simp } ⟩ def perm_congr {Ξ± : Type*} {Ξ² : Type*} (e : Ξ± ≃ Ξ²) : perm Ξ± ≃ perm Ξ² := equiv_congr e e protected lemma image_eq_preimage {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s : set Ξ±) : e '' s = e.symm ⁻¹' s := set.ext $ assume x, set.mem_image_iff_of_inverse e.left_inv e.right_inv protected lemma subset_image {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s : set Ξ±) (t : set Ξ²) : t βŠ† e '' s ↔ e.symm '' t βŠ† s := by rw [set.image_subset_iff, e.image_eq_preimage] lemma symm_image_image {Ξ± Ξ²} (f : equiv Ξ± Ξ²) (s : set Ξ±) : f.symm '' (f '' s) = s := by { rw [← set.image_comp], simp } protected lemma image_compl {Ξ± Ξ²} (f : equiv Ξ± Ξ²) (s : set Ξ±) : f '' -s = -(f '' s) := set.image_compl_eq f.bijective /- The group of permutations (self-equivalences) of a type `Ξ±` -/ namespace perm instance perm_group {Ξ± : Type u} : group (perm Ξ±) := begin refine { mul := Ξ» f g, equiv.trans g f, one := equiv.refl Ξ±, inv:= equiv.symm, ..}; intros; apply equiv.ext; try { apply trans_apply }, apply symm_apply_apply end @[simp] theorem mul_apply {Ξ± : Type u} (f g : perm Ξ±) (x) : (f * g) x = f (g x) := equiv.trans_apply _ _ _ @[simp] theorem one_apply {Ξ± : Type u} (x) : (1 : perm Ξ±) x = x := rfl @[simp] lemma inv_apply_self {Ξ± : Type u} (f : perm Ξ±) (x) : f⁻¹ (f x) = x := equiv.symm_apply_apply _ _ @[simp] lemma apply_inv_self {Ξ± : Type u} (f : perm Ξ±) (x) : f (f⁻¹ x) = x := equiv.apply_symm_apply _ _ lemma one_def {Ξ± : Type u} : (1 : perm Ξ±) = equiv.refl Ξ± := rfl lemma mul_def {Ξ± : Type u} (f g : perm Ξ±) : f * g = g.trans f := rfl lemma inv_def {Ξ± : Type u} (f : perm Ξ±) : f⁻¹ = f.symm := rfl end perm def equiv_empty (h : Ξ± β†’ false) : Ξ± ≃ empty := ⟨λ x, (h x).elim, Ξ» e, e.rec _, Ξ» x, (h x).elim, Ξ» e, e.rec _⟩ def false_equiv_empty : false ≃ empty := equiv_empty _root_.id def equiv_pempty (h : Ξ± β†’ false) : Ξ± ≃ pempty := ⟨λ x, (h x).elim, Ξ» e, e.rec _, Ξ» x, (h x).elim, Ξ» e, e.rec _⟩ def false_equiv_pempty : false ≃ pempty := equiv_pempty _root_.id def empty_equiv_pempty : empty ≃ pempty := equiv_pempty $ empty.rec _ def pempty_equiv_pempty : pempty.{v} ≃ pempty.{w} := equiv_pempty pempty.elim def empty_of_not_nonempty {Ξ± : Sort*} (h : Β¬ nonempty Ξ±) : Ξ± ≃ empty := equiv_empty $ assume a, h ⟨a⟩ def pempty_of_not_nonempty {Ξ± : Sort*} (h : Β¬ nonempty Ξ±) : Ξ± ≃ pempty := equiv_pempty $ assume a, h ⟨a⟩ def prop_equiv_punit {p : Prop} (h : p) : p ≃ punit := ⟨λ x, (), Ξ» x, h, Ξ» _, rfl, Ξ» ⟨⟩, rfl⟩ def true_equiv_punit : true ≃ punit := prop_equiv_punit trivial protected def ulift {Ξ± : Type u} : ulift Ξ± ≃ Ξ± := ⟨ulift.down, ulift.up, ulift.up_down, Ξ» a, rfl⟩ protected def plift : plift Ξ± ≃ Ξ± := ⟨plift.down, plift.up, plift.up_down, plift.down_up⟩ @[congr] def arrow_congr {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Sort*} (e₁ : α₁ ≃ Ξ±β‚‚) (eβ‚‚ : β₁ ≃ Ξ²β‚‚) : (α₁ β†’ β₁) ≃ (Ξ±β‚‚ β†’ Ξ²β‚‚) := { to_fun := Ξ» f, eβ‚‚.to_fun ∘ f ∘ e₁.inv_fun, inv_fun := Ξ» f, eβ‚‚.inv_fun ∘ f ∘ e₁.to_fun, left_inv := Ξ» f, funext $ Ξ» x, by { dsimp, simp }, right_inv := Ξ» f, funext $ Ξ» x, by { dsimp, simp } } @[simp] lemma arrow_congr_apply {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Sort*} (e₁ : α₁ ≃ Ξ±β‚‚) (eβ‚‚ : β₁ ≃ Ξ²β‚‚) (f : α₁ β†’ β₁) (x : Ξ±β‚‚) : arrow_congr e₁ eβ‚‚ f x = (eβ‚‚ $ f $ e₁.symm x) := rfl lemma arrow_congr_comp {α₁ β₁ γ₁ Ξ±β‚‚ Ξ²β‚‚ Ξ³β‚‚ : Sort*} (ea : α₁ ≃ Ξ±β‚‚) (eb : β₁ ≃ Ξ²β‚‚) (ec : γ₁ ≃ Ξ³β‚‚) (f : α₁ β†’ β₁) (g : β₁ β†’ γ₁) : arrow_congr ea ec (g ∘ f) = (arrow_congr eb ec g) ∘ (arrow_congr ea eb f) := by { ext, simp only [comp, arrow_congr_apply, eb.symm_apply_apply] } @[simp] lemma arrow_congr_refl {Ξ± Ξ² : Sort*} : arrow_congr (equiv.refl Ξ±) (equiv.refl Ξ²) = equiv.refl (Ξ± β†’ Ξ²) := rfl @[simp] lemma arrow_congr_trans {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ α₃ β₃ : Sort*} (e₁ : α₁ ≃ Ξ±β‚‚) (e₁' : β₁ ≃ Ξ²β‚‚) (eβ‚‚ : Ξ±β‚‚ ≃ α₃) (eβ‚‚' : Ξ²β‚‚ ≃ β₃) : arrow_congr (e₁.trans eβ‚‚) (e₁'.trans eβ‚‚') = (arrow_congr e₁ e₁').trans (arrow_congr eβ‚‚ eβ‚‚') := rfl @[simp] lemma arrow_congr_symm {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Sort*} (e₁ : α₁ ≃ Ξ±β‚‚) (eβ‚‚ : β₁ ≃ Ξ²β‚‚) : (arrow_congr e₁ eβ‚‚).symm = arrow_congr e₁.symm eβ‚‚.symm := rfl def conj (e : Ξ± ≃ Ξ²) : (Ξ± β†’ Ξ±) ≃ (Ξ² β†’ Ξ²) := arrow_congr e e @[simp] lemma conj_apply (e : Ξ± ≃ Ξ²) (f : Ξ± β†’ Ξ±) (x : Ξ²) : e.conj f x = (e $ f $ e.symm x) := rfl @[simp] lemma conj_refl : conj (equiv.refl Ξ±) = equiv.refl (Ξ± β†’ Ξ±) := rfl @[simp] lemma conj_symm (e : Ξ± ≃ Ξ²) : e.conj.symm = e.symm.conj := rfl @[simp] lemma conj_trans (e₁ : Ξ± ≃ Ξ²) (eβ‚‚ : Ξ² ≃ Ξ³) : (e₁.trans eβ‚‚).conj = e₁.conj.trans eβ‚‚.conj := rfl -- This should not be a simp lemma as long as `(∘)` is reducible: -- when `(∘)` is reducible, Lean can unify `f₁ ∘ fβ‚‚` with any `g` using -- `f₁ := g` and `fβ‚‚ := Ξ» x, x`. This causes nontermination. lemma conj_comp (e : Ξ± ≃ Ξ²) (f₁ fβ‚‚ : Ξ± β†’ Ξ±) : e.conj (f₁ ∘ fβ‚‚) = (e.conj f₁) ∘ (e.conj fβ‚‚) := by apply arrow_congr_comp def punit_equiv_punit : punit.{v} ≃ punit.{w} := ⟨λ _, punit.star, Ξ» _, punit.star, Ξ» u, by { cases u, refl }, Ξ» u, by { cases u, reflexivity }⟩ section @[simp] def arrow_punit_equiv_punit (Ξ± : Sort*) : (Ξ± β†’ punit.{v}) ≃ punit.{w} := ⟨λ f, punit.star, Ξ» u f, punit.star, Ξ» f, by { funext x, cases f x, refl }, Ξ» u, by { cases u, reflexivity }⟩ @[simp] def punit_arrow_equiv (Ξ± : Sort*) : (punit.{u} β†’ Ξ±) ≃ Ξ± := ⟨λ f, f punit.star, Ξ» a u, a, Ξ» f, by { funext x, cases x, refl }, Ξ» u, rfl⟩ @[simp] def empty_arrow_equiv_punit (Ξ± : Sort*) : (empty β†’ Ξ±) ≃ punit.{u} := ⟨λ f, punit.star, Ξ» u e, e.rec _, Ξ» f, funext $ Ξ» x, x.rec _, Ξ» u, by { cases u, refl }⟩ @[simp] def pempty_arrow_equiv_punit (Ξ± : Sort*) : (pempty β†’ Ξ±) ≃ punit.{u} := ⟨λ f, punit.star, Ξ» u e, e.rec _, Ξ» f, funext $ Ξ» x, x.rec _, Ξ» u, by { cases u, refl }⟩ @[simp] def false_arrow_equiv_punit (Ξ± : Sort*) : (false β†’ Ξ±) ≃ punit.{u} := calc (false β†’ Ξ±) ≃ (empty β†’ Ξ±) : arrow_congr false_equiv_empty (equiv.refl _) ... ≃ punit : empty_arrow_equiv_punit _ end @[congr] def prod_congr {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Sort*} (e₁ : α₁ ≃ Ξ±β‚‚) (eβ‚‚ :β₁ ≃ Ξ²β‚‚) : α₁ Γ— β₁ ≃ Ξ±β‚‚ Γ— Ξ²β‚‚ := ⟨λp, (e₁ p.1, eβ‚‚ p.2), Ξ»p, (e₁.symm p.1, eβ‚‚.symm p.2), Ξ» ⟨a, b⟩, show (e₁.symm (e₁ a), eβ‚‚.symm (eβ‚‚ b)) = (a, b), by rw [symm_apply_apply, symm_apply_apply], Ξ» ⟨a, b⟩, show (e₁ (e₁.symm a), eβ‚‚ (eβ‚‚.symm b)) = (a, b), by rw [apply_symm_apply, apply_symm_apply]⟩ @[simp] theorem prod_congr_apply {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Sort*} (e₁ : α₁ ≃ Ξ±β‚‚) (eβ‚‚ : β₁ ≃ Ξ²β‚‚) (a : α₁) (b : β₁) : prod_congr e₁ eβ‚‚ (a, b) = (e₁ a, eβ‚‚ b) := rfl @[simp] def prod_comm (Ξ± Ξ² : Sort*) : Ξ± Γ— Ξ² ≃ Ξ² Γ— Ξ± := ⟨λ p, (p.2, p.1), Ξ» p, (p.2, p.1), λ⟨a, b⟩, rfl, λ⟨a, b⟩, rfl⟩ @[simp] def prod_assoc (Ξ± Ξ² Ξ³ : Sort*) : (Ξ± Γ— Ξ²) Γ— Ξ³ ≃ Ξ± Γ— (Ξ² Γ— Ξ³) := ⟨λ p, ⟨p.1.1, ⟨p.1.2, p.2⟩⟩, Ξ»p, ⟨⟨p.1, p.2.1⟩, p.2.2⟩, Ξ» ⟨⟨a, b⟩, c⟩, rfl, Ξ» ⟨a, ⟨b, c⟩⟩, rfl⟩ @[simp] theorem prod_assoc_apply {Ξ± Ξ² Ξ³ : Sort*} (p : (Ξ± Γ— Ξ²) Γ— Ξ³) : prod_assoc Ξ± Ξ² Ξ³ p = ⟨p.1.1, ⟨p.1.2, p.2⟩⟩ := rfl section @[simp] def prod_punit (Ξ± : Sort*) : Ξ± Γ— punit.{u+1} ≃ Ξ± := ⟨λ p, p.1, Ξ» a, (a, punit.star), Ξ» ⟨_, punit.star⟩, rfl, Ξ» a, rfl⟩ @[simp] theorem prod_punit_apply {Ξ± : Sort*} (a : Ξ± Γ— punit.{u+1}) : prod_punit Ξ± a = a.1 := rfl @[simp] def punit_prod (Ξ± : Sort*) : punit.{u+1} Γ— Ξ± ≃ Ξ± := calc punit Γ— Ξ± ≃ Ξ± Γ— punit : prod_comm _ _ ... ≃ Ξ± : prod_punit _ @[simp] theorem punit_prod_apply {Ξ± : Sort*} (a : punit.{u+1} Γ— Ξ±) : punit_prod Ξ± a = a.2 := rfl @[simp] def prod_empty (Ξ± : Sort*) : Ξ± Γ— empty ≃ empty := equiv_empty (Ξ» ⟨_, e⟩, e.rec _) @[simp] def empty_prod (Ξ± : Sort*) : empty Γ— Ξ± ≃ empty := equiv_empty (Ξ» ⟨e, _⟩, e.rec _) @[simp] def prod_pempty (Ξ± : Sort*) : Ξ± Γ— pempty ≃ pempty := equiv_pempty (Ξ» ⟨_, e⟩, e.rec _) @[simp] def pempty_prod (Ξ± : Sort*) : pempty Γ— Ξ± ≃ pempty := equiv_pempty (Ξ» ⟨e, _⟩, e.rec _) end section open sum def psum_equiv_sum (Ξ± Ξ² : Sort*) : psum Ξ± Ξ² ≃ Ξ± βŠ• Ξ² := ⟨λ s, psum.cases_on s inl inr, Ξ» s, sum.cases_on s psum.inl psum.inr, Ξ» s, by cases s; refl, Ξ» s, by cases s; refl⟩ def sum_congr {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Sort*} : α₁ ≃ Ξ±β‚‚ β†’ β₁ ≃ Ξ²β‚‚ β†’ α₁ βŠ• β₁ ≃ Ξ±β‚‚ βŠ• Ξ²β‚‚ | ⟨f₁, g₁, l₁, rβ‚βŸ© ⟨fβ‚‚, gβ‚‚, lβ‚‚, rβ‚‚βŸ© := ⟨λ s, match s with inl a₁ := inl (f₁ a₁) | inr b₁ := inr (fβ‚‚ b₁) end, Ξ» s, match s with inl aβ‚‚ := inl (g₁ aβ‚‚) | inr bβ‚‚ := inr (gβ‚‚ bβ‚‚) end, Ξ» s, match s with inl a := congr_arg inl (l₁ a) | inr a := congr_arg inr (lβ‚‚ a) end, Ξ» s, match s with inl a := congr_arg inl (r₁ a) | inr a := congr_arg inr (rβ‚‚ a) end⟩ @[simp] theorem sum_congr_apply_inl {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Sort*} (e₁ : α₁ ≃ Ξ±β‚‚) (eβ‚‚ : β₁ ≃ Ξ²β‚‚) (a : α₁) : sum_congr e₁ eβ‚‚ (inl a) = inl (e₁ a) := by { cases e₁, cases eβ‚‚, refl } @[simp] theorem sum_congr_apply_inr {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Sort*} (e₁ : α₁ ≃ Ξ±β‚‚) (eβ‚‚ : β₁ ≃ Ξ²β‚‚) (b : β₁) : sum_congr e₁ eβ‚‚ (inr b) = inr (eβ‚‚ b) := by { cases e₁, cases eβ‚‚, refl } @[simp] lemma sum_congr_symm {Ξ± Ξ² Ξ³ Ξ΄ : Type u} (e : Ξ± ≃ Ξ²) (f : Ξ³ ≃ Ξ΄) (x) : (equiv.sum_congr e f).symm x = (equiv.sum_congr (e.symm) (f.symm)) x := by { cases e, cases f, cases x; refl } def bool_equiv_punit_sum_punit : bool ≃ punit.{u+1} βŠ• punit.{v+1} := ⟨λ b, cond b (inr punit.star) (inl punit.star), Ξ» s, sum.rec_on s (Ξ»_, ff) (Ξ»_, tt), Ξ» b, by cases b; refl, Ξ» s, by rcases s with ⟨⟨⟩⟩ | ⟨⟨⟩⟩; refl⟩ noncomputable def Prop_equiv_bool : Prop ≃ bool := ⟨λ p, @to_bool p (classical.prop_decidable _), Ξ» b, b, Ξ» p, by simp, Ξ» b, by simp⟩ @[simp] def sum_comm (Ξ± Ξ² : Sort*) : Ξ± βŠ• Ξ² ≃ Ξ² βŠ• Ξ± := ⟨λ s, match s with inl a := inr a | inr b := inl b end, Ξ» s, match s with inl b := inr b | inr a := inl a end, Ξ» s, by cases s; refl, Ξ» s, by cases s; refl⟩ @[simp] def sum_assoc (Ξ± Ξ² Ξ³ : Sort*) : (Ξ± βŠ• Ξ²) βŠ• Ξ³ ≃ Ξ± βŠ• (Ξ² βŠ• Ξ³) := ⟨λ s, match s with inl (inl a) := inl a | inl (inr b) := inr (inl b) | inr c := inr (inr c) end, Ξ» s, match s with inl a := inl (inl a) | inr (inl b) := inl (inr b) | inr (inr c) := inr c end, Ξ» s, by rcases s with ⟨_ | _⟩ | _; refl, Ξ» s, by rcases s with _ | _ | _; refl⟩ @[simp] theorem sum_assoc_apply_in1 {Ξ± Ξ² Ξ³} (a) : sum_assoc Ξ± Ξ² Ξ³ (inl (inl a)) = inl a := rfl @[simp] theorem sum_assoc_apply_in2 {Ξ± Ξ² Ξ³} (b) : sum_assoc Ξ± Ξ² Ξ³ (inl (inr b)) = inr (inl b) := rfl @[simp] theorem sum_assoc_apply_in3 {Ξ± Ξ² Ξ³} (c) : sum_assoc Ξ± Ξ² Ξ³ (inr c) = inr (inr c) := rfl @[simp] def sum_empty (Ξ± : Sort*) : Ξ± βŠ• empty ≃ Ξ± := ⟨λ s, match s with inl a := a | inr e := empty.rec _ e end, inl, Ξ» s, by { rcases s with _ | ⟨⟨⟩⟩, refl }, Ξ» a, rfl⟩ @[simp] def empty_sum (Ξ± : Sort*) : empty βŠ• Ξ± ≃ Ξ± := (sum_comm _ _).trans $ sum_empty _ @[simp] def sum_pempty (Ξ± : Sort*) : Ξ± βŠ• pempty ≃ Ξ± := ⟨λ s, match s with inl a := a | inr e := pempty.rec _ e end, inl, Ξ» s, by { rcases s with _ | ⟨⟨⟩⟩, refl }, Ξ» a, rfl⟩ @[simp] def pempty_sum (Ξ± : Sort*) : pempty βŠ• Ξ± ≃ Ξ± := (sum_comm _ _).trans $ sum_pempty _ @[simp] def option_equiv_sum_punit (Ξ± : Sort*) : option Ξ± ≃ Ξ± βŠ• punit.{u+1} := ⟨λ o, match o with none := inr punit.star | some a := inl a end, Ξ» s, match s with inr _ := none | inl a := some a end, Ξ» o, by cases o; refl, Ξ» s, by rcases s with _ | ⟨⟨⟩⟩; refl⟩ def sum_equiv_sigma_bool (Ξ± Ξ² : Sort*) : Ξ± βŠ• Ξ² ≃ (Ξ£ b: bool, cond b Ξ± Ξ²) := ⟨λ s, match s with inl a := ⟨tt, a⟩ | inr b := ⟨ff, b⟩ end, Ξ» s, match s with ⟨tt, a⟩ := inl a | ⟨ff, b⟩ := inr b end, Ξ» s, by cases s; refl, Ξ» s, by rcases s with ⟨_|_, _⟩; refl⟩ def sigma_preimage_equiv {Ξ± Ξ² : Type*} (f : Ξ± β†’ Ξ²) : (Ξ£ y : Ξ², {x // f x = y}) ≃ Ξ± := ⟨λ x, x.2.1, Ξ» x, ⟨f x, x, rfl⟩, Ξ» ⟨y, x, rfl⟩, rfl, Ξ» x, rfl⟩ end section def Pi_congr_right {Ξ±} {β₁ Ξ²β‚‚ : Ξ± β†’ Sort*} (F : βˆ€ a, β₁ a ≃ Ξ²β‚‚ a) : (Ξ  a, β₁ a) ≃ (Ξ  a, Ξ²β‚‚ a) := ⟨λ H a, F a (H a), Ξ» H a, (F a).symm (H a), Ξ» H, funext $ by simp, Ξ» H, funext $ by simp⟩ def Pi_curry {Ξ±} {Ξ² : Ξ± β†’ Sort*} (Ξ³ : Ξ  a, Ξ² a β†’ Sort*) : (Ξ  x : sigma Ξ², Ξ³ x.1 x.2) ≃ (Ξ  a b, Ξ³ a b) := { to_fun := Ξ» f x y, f ⟨x,y⟩, inv_fun := Ξ» f x, f x.1 x.2, left_inv := Ξ» f, funext $ Ξ» ⟨x,y⟩, rfl, right_inv := Ξ» f, funext $ Ξ» x, funext $ Ξ» y, rfl } end section def psigma_equiv_sigma {Ξ±} (Ξ² : Ξ± β†’ Sort*) : psigma Ξ² ≃ sigma Ξ² := ⟨λ ⟨a, b⟩, ⟨a, b⟩, Ξ» ⟨a, b⟩, ⟨a, b⟩, Ξ» ⟨a, b⟩, rfl, Ξ» ⟨a, b⟩, rfl⟩ def sigma_congr_right {Ξ±} {β₁ Ξ²β‚‚ : Ξ± β†’ Sort*} (F : βˆ€ a, β₁ a ≃ Ξ²β‚‚ a) : sigma β₁ ≃ sigma Ξ²β‚‚ := ⟨λ ⟨a, b⟩, ⟨a, F a b⟩, Ξ» ⟨a, b⟩, ⟨a, (F a).symm b⟩, Ξ» ⟨a, b⟩, congr_arg (sigma.mk a) $ symm_apply_apply (F a) b, Ξ» ⟨a, b⟩, congr_arg (sigma.mk a) $ apply_symm_apply (F a) b⟩ def sigma_congr_left {α₁ Ξ±β‚‚} {Ξ² : Ξ±β‚‚ β†’ Sort*} : βˆ€ f : α₁ ≃ Ξ±β‚‚, (Ξ£ a:α₁, Ξ² (f a)) ≃ (Ξ£ a:Ξ±β‚‚, Ξ² a) | ⟨f, g, l, r⟩ := ⟨λ ⟨a, b⟩, ⟨f a, b⟩, Ξ» ⟨a, b⟩, ⟨g a, @@eq.rec Ξ² b (r a).symm⟩, Ξ» ⟨a, b⟩, match g (f a), l a : βˆ€ a' (h : a' = a), @sigma.mk _ (Ξ² ∘ f) _ (@@eq.rec Ξ² b (congr_arg f h.symm)) = ⟨a, b⟩ with | _, rfl := rfl end, Ξ» ⟨a, b⟩, match f (g a), _ : βˆ€ a' (h : a' = a), sigma.mk a' (@@eq.rec Ξ² b h.symm) = ⟨a, b⟩ with | _, rfl := rfl end⟩ /-- transporting a sigma type through an equivalence of the base -/ def sigma_congr_left' {α₁ Ξ±β‚‚} {Ξ² : α₁ β†’ Sort*} : βˆ€ f : α₁ ≃ Ξ±β‚‚, (Ξ£ a:α₁, Ξ² a) ≃ (Ξ£ a:Ξ±β‚‚, Ξ² (f.symm a)) := Ξ» f, (sigma_congr_left f.symm).symm /-- transporting a sigma type through an equivalence of the base and a family of equivalences of matching fibres -/ def sigma_congr {α₁ Ξ±β‚‚} {β₁ : α₁ β†’ Sort*} {Ξ²β‚‚ : Ξ±β‚‚ β†’ Sort*} (f : α₁ ≃ Ξ±β‚‚) (F : βˆ€ a, β₁ a ≃ Ξ²β‚‚ (f a)) : sigma β₁ ≃ sigma Ξ²β‚‚ := (sigma_congr_right F).trans (sigma_congr_left f) def sigma_equiv_prod (Ξ± Ξ² : Sort*) : (Ξ£_:Ξ±, Ξ²) ≃ Ξ± Γ— Ξ² := ⟨λ ⟨a, b⟩, ⟨a, b⟩, Ξ» ⟨a, b⟩, ⟨a, b⟩, Ξ» ⟨a, b⟩, rfl, Ξ» ⟨a, b⟩, rfl⟩ def sigma_equiv_prod_of_equiv {Ξ± Ξ²} {β₁ : Ξ± β†’ Sort*} (F : βˆ€ a, β₁ a ≃ Ξ²) : sigma β₁ ≃ Ξ± Γ— Ξ² := (sigma_congr_right F).trans (sigma_equiv_prod Ξ± Ξ²) end section def arrow_prod_equiv_prod_arrow (Ξ± Ξ² Ξ³ : Type*) : (Ξ³ β†’ Ξ± Γ— Ξ²) ≃ (Ξ³ β†’ Ξ±) Γ— (Ξ³ β†’ Ξ²) := ⟨λ f, (Ξ» c, (f c).1, Ξ» c, (f c).2), Ξ» p c, (p.1 c, p.2 c), Ξ» f, funext $ Ξ» c, prod.mk.eta, Ξ» p, by { cases p, refl }⟩ def arrow_arrow_equiv_prod_arrow (Ξ± Ξ² Ξ³ : Sort*) : (Ξ± β†’ Ξ² β†’ Ξ³) ≃ (Ξ± Γ— Ξ² β†’ Ξ³) := ⟨λ f, Ξ» p, f p.1 p.2, Ξ» f, Ξ» a b, f (a, b), Ξ» f, rfl, Ξ» f, by { funext p, cases p, refl }⟩ open sum def sum_arrow_equiv_prod_arrow (Ξ± Ξ² Ξ³ : Type*) : ((Ξ± βŠ• Ξ²) β†’ Ξ³) ≃ (Ξ± β†’ Ξ³) Γ— (Ξ² β†’ Ξ³) := ⟨λ f, (f ∘ inl, f ∘ inr), Ξ» p s, sum.rec_on s p.1 p.2, Ξ» f, by { funext s, cases s; refl }, Ξ» p, by { cases p, refl }⟩ def sum_prod_distrib (Ξ± Ξ² Ξ³ : Sort*) : (Ξ± βŠ• Ξ²) Γ— Ξ³ ≃ (Ξ± Γ— Ξ³) βŠ• (Ξ² Γ— Ξ³) := ⟨λ p, match p with (inl a, c) := inl (a, c) | (inr b, c) := inr (b, c) end, Ξ» s, match s with inl (a, c) := (inl a, c) | inr (b, c) := (inr b, c) end, Ξ» p, by rcases p with ⟨_ | _, _⟩; refl, Ξ» s, by rcases s with ⟨_, _⟩ | ⟨_, _⟩; refl⟩ @[simp] theorem sum_prod_distrib_apply_left {Ξ± Ξ² Ξ³} (a : Ξ±) (c : Ξ³) : sum_prod_distrib Ξ± Ξ² Ξ³ (sum.inl a, c) = sum.inl (a, c) := rfl @[simp] theorem sum_prod_distrib_apply_right {Ξ± Ξ² Ξ³} (b : Ξ²) (c : Ξ³) : sum_prod_distrib Ξ± Ξ² Ξ³ (sum.inr b, c) = sum.inr (b, c) := rfl def prod_sum_distrib (Ξ± Ξ² Ξ³ : Sort*) : Ξ± Γ— (Ξ² βŠ• Ξ³) ≃ (Ξ± Γ— Ξ²) βŠ• (Ξ± Γ— Ξ³) := calc Ξ± Γ— (Ξ² βŠ• Ξ³) ≃ (Ξ² βŠ• Ξ³) Γ— Ξ± : prod_comm _ _ ... ≃ (Ξ² Γ— Ξ±) βŠ• (Ξ³ Γ— Ξ±) : sum_prod_distrib _ _ _ ... ≃ (Ξ± Γ— Ξ²) βŠ• (Ξ± Γ— Ξ³) : sum_congr (prod_comm _ _) (prod_comm _ _) @[simp] theorem prod_sum_distrib_apply_left {Ξ± Ξ² Ξ³} (a : Ξ±) (b : Ξ²) : prod_sum_distrib Ξ± Ξ² Ξ³ (a, sum.inl b) = sum.inl (a, b) := rfl @[simp] theorem prod_sum_distrib_apply_right {Ξ± Ξ² Ξ³} (a : Ξ±) (c : Ξ³) : prod_sum_distrib Ξ± Ξ² Ξ³ (a, sum.inr c) = sum.inr (a, c) := rfl def sigma_prod_distrib {ΞΉ : Type*} (Ξ± : ΞΉ β†’ Type*) (Ξ² : Type*) : ((Ξ£ i, Ξ± i) Γ— Ξ²) ≃ (Ξ£ i, (Ξ± i Γ— Ξ²)) := ⟨λ p, ⟨p.1.1, (p.1.2, p.2)⟩, Ξ» p, (⟨p.1, p.2.1⟩, p.2.2), Ξ» p, by { rcases p with ⟨⟨_, _⟩, _⟩, refl }, Ξ» p, by { rcases p with ⟨_, ⟨_, _⟩⟩, refl }⟩ def bool_prod_equiv_sum (Ξ± : Type u) : bool Γ— Ξ± ≃ Ξ± βŠ• Ξ± := calc bool Γ— Ξ± ≃ (unit βŠ• unit) Γ— Ξ± : prod_congr bool_equiv_punit_sum_punit (equiv.refl _) ... ≃ Ξ± Γ— (unit βŠ• unit) : prod_comm _ _ ... ≃ (Ξ± Γ— unit) βŠ• (Ξ± Γ— unit) : prod_sum_distrib _ _ _ ... ≃ Ξ± βŠ• Ξ± : sum_congr (prod_punit _) (prod_punit _) end section open sum nat def nat_equiv_nat_sum_punit : β„• ≃ β„• βŠ• punit.{u+1} := ⟨λ n, match n with zero := inr punit.star | succ a := inl a end, Ξ» s, match s with inl n := succ n | inr punit.star := zero end, Ξ» n, begin cases n, repeat { refl } end, Ξ» s, begin cases s with a u, { refl }, {cases u, { refl }} end⟩ @[simp] def nat_sum_punit_equiv_nat : β„• βŠ• punit.{u+1} ≃ β„• := nat_equiv_nat_sum_punit.symm def int_equiv_nat_sum_nat : β„€ ≃ β„• βŠ• β„• := by refine ⟨_, _, _, _⟩; intro z; {cases z; [left, right]; assumption} <|> {cases z; refl} end def list_equiv_of_equiv {Ξ± Ξ² : Type*} : Ξ± ≃ Ξ² β†’ list Ξ± ≃ list Ξ² | ⟨f, g, l, r⟩ := by refine ⟨list.map f, list.map g, Ξ» x, _, Ξ» x, _⟩; simp [id_of_left_inverse l, id_of_right_inverse r] def fin_equiv_subtype (n : β„•) : fin n ≃ {m // m < n} := ⟨λ x, ⟨x.1, x.2⟩, Ξ» x, ⟨x.1, x.2⟩, Ξ» ⟨a, b⟩, rfl,Ξ» ⟨a, b⟩, rfl⟩ def decidable_eq_of_equiv [decidable_eq Ξ²] (e : Ξ± ≃ Ξ²) : decidable_eq Ξ± | a₁ aβ‚‚ := decidable_of_iff (e a₁ = e aβ‚‚) e.injective.eq_iff def inhabited_of_equiv [inhabited Ξ²] (e : Ξ± ≃ Ξ²) : inhabited Ξ± := ⟨e.symm (default _)⟩ def unique_of_equiv (e : Ξ± ≃ Ξ²) (h : unique Ξ²) : unique Ξ± := e.symm.surjective.unique def unique_congr (e : Ξ± ≃ Ξ²) : unique Ξ± ≃ unique Ξ² := { to_fun := e.symm.unique_of_equiv, inv_fun := e.unique_of_equiv, left_inv := Ξ» _, subsingleton.elim _ _, right_inv := Ξ» _, subsingleton.elim _ _ } section open subtype def subtype_congr {p : Ξ± β†’ Prop} {q : Ξ² β†’ Prop} (e : Ξ± ≃ Ξ²) (h : βˆ€ a, p a ↔ q (e a)) : {a : Ξ± // p a} ≃ {b : Ξ² // q b} := ⟨λ x, ⟨e x.1, (h _).1 x.2⟩, Ξ» y, ⟨e.symm y.1, (h _).2 (by { simp, exact y.2 })⟩, Ξ» ⟨x, h⟩, subtype.eq' $ by simp, Ξ» ⟨y, h⟩, subtype.eq' $ by simp⟩ def subtype_congr_right {p q : Ξ± β†’ Prop} (e : βˆ€x, p x ↔ q x) : subtype p ≃ subtype q := subtype_congr (equiv.refl _) e @[simp] lemma subtype_congr_right_mk {p q : Ξ± β†’ Prop} (e : βˆ€x, p x ↔ q x) {x : Ξ±} (h : p x) : subtype_congr_right e ⟨x, h⟩ = ⟨x, (e x).1 h⟩ := rfl def subtype_equiv_of_subtype' {p : Ξ± β†’ Prop} (e : Ξ± ≃ Ξ²) : {a : Ξ± // p a} ≃ {b : Ξ² // p (e.symm b)} := subtype_congr e $ by simp def subtype_congr_prop {Ξ± : Type*} {p q : Ξ± β†’ Prop} (h : p = q) : subtype p ≃ subtype q := subtype_congr (equiv.refl Ξ±) (assume a, h β–Έ iff.rfl) def set_congr {Ξ± : Type*} {s t : set Ξ±} (h : s = t) : s ≃ t := subtype_congr_prop h def subtype_subtype_equiv_subtype_exists {Ξ± : Type u} (p : Ξ± β†’ Prop) (q : subtype p β†’ Prop) : subtype q ≃ {a : Ξ± // βˆƒh:p a, q ⟨a, h⟩ } := ⟨λ⟨⟨a, ha⟩, ha'⟩, ⟨a, ha, ha'⟩, λ⟨a, ha⟩, ⟨⟨a, ha.cases_on $ assume h _, h⟩, by { cases ha, exact ha_h }⟩, assume ⟨⟨a, ha⟩, h⟩, rfl, assume ⟨a, h₁, hβ‚‚βŸ©, rfl⟩ def subtype_subtype_equiv_subtype_inter {Ξ± : Type u} (p q : Ξ± β†’ Prop) : {x : subtype p // q x.1} ≃ subtype (Ξ» x, p x ∧ q x) := (subtype_subtype_equiv_subtype_exists p _).trans $ subtype_congr_right $ Ξ» x, exists_prop /-- If the outer subtype has more restrictive predicate than the inner one, then we can drop the latter. -/ def subtype_subtype_equiv_subtype {Ξ± : Type u} {p q : Ξ± β†’ Prop} (h : βˆ€ {x}, q x β†’ p x) : {x : subtype p // q x.1} ≃ subtype q := (subtype_subtype_equiv_subtype_inter p _).trans $ subtype_congr_right $ assume x, ⟨and.right, Ξ» h₁, ⟨h h₁, hβ‚βŸ©βŸ© /-- If a proposition holds for all elements, then the subtype is equivalent to the original type. -/ def subtype_univ_equiv {Ξ± : Type u} {p : Ξ± β†’ Prop} (h : βˆ€ x, p x) : subtype p ≃ Ξ± := ⟨λ x, x, Ξ» x, ⟨x, h x⟩, Ξ» x, subtype.eq rfl, Ξ» x, rfl⟩ /-- A subtype of a sigma-type is a sigma-type over a subtype. -/ def subtype_sigma_equiv {Ξ± : Type u} (p : Ξ± β†’ Type v) (q : Ξ± β†’ Prop) : { y : sigma p // q y.1 } ≃ Ξ£(x : subtype q), p x.1 := ⟨λ x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩, Ξ» x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩, Ξ» ⟨⟨x, h⟩, y⟩, rfl, Ξ» ⟨⟨x, y⟩, h⟩, rfl⟩ /-- A sigma type over a subtype is equivalent to the sigma set over the original type, if the fiber is empty outside of the subset -/ def sigma_subtype_equiv_of_subset {Ξ± : Type u} (p : Ξ± β†’ Type v) (q : Ξ± β†’ Prop) (h : βˆ€ x, p x β†’ q x) : (Ξ£ x : subtype q, p x) ≃ Ξ£ x : Ξ±, p x := (subtype_sigma_equiv p q).symm.trans $ subtype_univ_equiv $ Ξ» x, h x.1 x.2 def sigma_subtype_preimage_equiv {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β†’ Ξ²) (p : Ξ² β†’ Prop) (h : βˆ€ x, p (f x)) : (Ξ£ y : subtype p, {x : Ξ± // f x = y}) ≃ Ξ± := calc _ ≃ Ξ£ y : Ξ², {x : Ξ± // f x = y} : sigma_subtype_equiv_of_subset _ p (Ξ» y ⟨x, h'⟩, h' β–Έ h x) ... ≃ Ξ± : sigma_preimage_equiv f def sigma_subtype_preimage_equiv_subtype {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β†’ Ξ²) {p : Ξ± β†’ Prop} {q : Ξ² β†’ Prop} (h : βˆ€ x, p x ↔ q (f x)) : (Ξ£ y : subtype q, {x : Ξ± // f x = y}) ≃ subtype p := calc (Ξ£ y : subtype q, {x : Ξ± // f x = y}) ≃ Ξ£ y : subtype q, {x : subtype p // subtype.mk (f x) ((h x).1 x.2) = y} : begin apply sigma_congr_right, assume y, symmetry, refine (subtype_subtype_equiv_subtype_exists _ _).trans (subtype_congr_right _), assume x, exact ⟨λ ⟨hp, h'⟩, congr_arg subtype.val h', Ξ» h', ⟨(h x).2 (h'.symm β–Έ y.2), subtype.eq h'⟩⟩ end ... ≃ subtype p : sigma_preimage_equiv (Ξ» x : subtype p, (⟨f x, (h x).1 x.property⟩ : subtype q)) def pi_equiv_subtype_sigma (ΞΉ : Type*) (Ο€ : ΞΉ β†’ Type*) : (Ξ i, Ο€ i) ≃ {f : ΞΉ β†’ Ξ£i, Ο€ i | βˆ€i, (f i).1 = i } := ⟨ Ξ»f, ⟨λi, ⟨i, f i⟩, assume i, rfl⟩, Ξ»f i, begin rw ← f.2 i, exact (f.1 i).2 end, assume f, funext $ assume i, rfl, assume ⟨f, hf⟩, subtype.eq $ funext $ assume i, sigma.eq (hf i).symm $ eq_of_heq $ rec_heq_of_heq _ $ rec_heq_of_heq _ $ heq.refl _⟩ def subtype_pi_equiv_pi {Ξ± : Sort u} {Ξ² : Ξ± β†’ Sort v} {p : Ξ a, Ξ² a β†’ Prop} : {f : Ξ a, Ξ² a // βˆ€a, p a (f a) } ≃ Ξ a, { b : Ξ² a // p a b } := ⟨λf a, ⟨f.1 a, f.2 a⟩, Ξ»f, ⟨λa, (f a).1, Ξ»a, (f a).2⟩, by { rintro ⟨f, h⟩, refl }, by { rintro f, funext a, exact subtype.eq' rfl }⟩ end namespace set open set protected def univ (Ξ±) : @univ Ξ± ≃ Ξ± := ⟨subtype.val, Ξ» a, ⟨a, trivial⟩, Ξ» ⟨a, _⟩, rfl, Ξ» a, rfl⟩ @[simp] lemma univ_apply {Ξ± : Type u} (x : @univ Ξ±) : equiv.set.univ Ξ± x = x := rfl @[simp] lemma univ_symm_apply {Ξ± : Type u} (x : Ξ±) : (equiv.set.univ Ξ±).symm x = ⟨x, trivial⟩ := rfl protected def empty (Ξ±) : (βˆ… : set Ξ±) ≃ empty := equiv_empty $ Ξ» ⟨x, h⟩, not_mem_empty x h protected def pempty (Ξ±) : (βˆ… : set Ξ±) ≃ pempty := equiv_pempty $ Ξ» ⟨x, h⟩, not_mem_empty x h protected def union' {Ξ±} {s t : set Ξ±} (p : Ξ± β†’ Prop) [decidable_pred p] (hs : βˆ€ x ∈ s, p x) (ht : βˆ€ x ∈ t, Β¬ p x) : (s βˆͺ t : set Ξ±) ≃ s βŠ• t := { to_fun := Ξ» x, if hp : p x.1 then sum.inl ⟨_, x.2.resolve_right (Ξ» xt, ht _ xt hp)⟩ else sum.inr ⟨_, x.2.resolve_left (Ξ» xs, hp (hs _ xs))⟩, inv_fun := Ξ» o, match o with | (sum.inl x) := ⟨x.1, or.inl x.2⟩ | (sum.inr x) := ⟨x.1, or.inr x.2⟩ end, left_inv := Ξ» ⟨x, h'⟩, by by_cases p x; simp [union'._match_1, h]; congr, right_inv := Ξ» o, begin rcases o with ⟨x, h⟩ | ⟨x, h⟩; dsimp [union'._match_1]; [simp [hs _ h], simp [ht _ h]] end } protected def union {Ξ±} {s t : set Ξ±} [decidable_pred s] (H : s ∩ t = βˆ…) : (s βˆͺ t : set Ξ±) ≃ s βŠ• t := set.union' s (Ξ» _, id) (Ξ» x xt xs, subset_empty_iff.2 H ⟨xs, xt⟩) lemma union_apply_left {Ξ±} {s t : set Ξ±} [decidable_pred s] (H : s ∩ t = βˆ…) {a : (s βˆͺ t : set Ξ±)} (ha : ↑a ∈ s) : equiv.set.union H a = sum.inl ⟨a, ha⟩ := dif_pos ha lemma union_apply_right {Ξ±} {s t : set Ξ±} [decidable_pred s] (H : s ∩ t = βˆ…) {a : (s βˆͺ t : set Ξ±)} (ha : ↑a ∈ t) : equiv.set.union H a = sum.inr ⟨a, ha⟩ := dif_neg (show ↑a βˆ‰ s, by finish [set.ext_iff]) protected def singleton {Ξ±} (a : Ξ±) : ({a} : set Ξ±) ≃ punit.{u} := ⟨λ _, punit.star, Ξ» _, ⟨a, mem_singleton _⟩, Ξ» ⟨x, h⟩, by { simp at h, subst x }, Ξ» ⟨⟩, rfl⟩ protected def of_eq {Ξ± : Type u} {s t : set Ξ±} (h : s = t) : s ≃ t := { to_fun := Ξ» x, ⟨x.1, h β–Έ x.2⟩, inv_fun := Ξ» x, ⟨x.1, h.symm β–Έ x.2⟩, left_inv := Ξ» _, subtype.eq rfl, right_inv := Ξ» _, subtype.eq rfl } @[simp] lemma of_eq_apply {Ξ± : Type u} {s t : set Ξ±} (h : s = t) (a : s) : equiv.set.of_eq h a = ⟨a, h β–Έ a.2⟩ := rfl @[simp] lemma of_eq_symm_apply {Ξ± : Type u} {s t : set Ξ±} (h : s = t) (a : t) : (equiv.set.of_eq h).symm a = ⟨a, h.symm β–Έ a.2⟩ := rfl protected def insert {Ξ±} {s : set.{u} Ξ±} [decidable_pred s] {a : Ξ±} (H : a βˆ‰ s) : (insert a s : set Ξ±) ≃ s βŠ• punit.{u+1} := calc (insert a s : set Ξ±) ≃ β†₯(s βˆͺ {a}) : equiv.set.of_eq (by simp) ... ≃ s βŠ• ({a} : set Ξ±) : equiv.set.union (by finish [set.ext_iff]) ... ≃ s βŠ• punit.{u+1} : sum_congr (equiv.refl _) (equiv.set.singleton _) protected def sum_compl {Ξ±} (s : set Ξ±) [decidable_pred s] : s βŠ• (-s : set Ξ±) ≃ Ξ± := calc s βŠ• (-s : set Ξ±) ≃ β†₯(s βˆͺ -s) : (equiv.set.union (by simp [set.ext_iff])).symm ... ≃ @univ Ξ± : equiv.set.of_eq (by simp) ... ≃ Ξ± : equiv.set.univ _ @[simp] lemma sum_compl_apply_inl {Ξ± : Type u} (s : set Ξ±) [decidable_pred s] (x : s) : equiv.set.sum_compl s (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {Ξ± : Type u} (s : set Ξ±) [decidable_pred s] (x : -s) : equiv.set.sum_compl s (sum.inr x) = x := rfl lemma sum_compl_symm_apply_of_mem {Ξ± : Type u} {s : set Ξ±} [decidable_pred s] {x : Ξ±} (hx : x ∈ s) : (equiv.set.sum_compl s).symm x = sum.inl ⟨x, hx⟩ := have ↑(⟨x, or.inl hx⟩ : (s βˆͺ -s : set Ξ±)) ∈ s, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_left _ this } lemma sum_compl_symm_apply_of_not_mem {Ξ± : Type u} {s : set Ξ±} [decidable_pred s] {x : Ξ±} (hx : x βˆ‰ s) : (equiv.set.sum_compl s).symm x = sum.inr ⟨x, hx⟩ := have ↑(⟨x, or.inr hx⟩ : (s βˆͺ -s : set Ξ±)) ∈ -s, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_right _ this } protected def union_sum_inter {Ξ± : Type u} (s t : set Ξ±) [decidable_pred s] : (s βˆͺ t : set Ξ±) βŠ• (s ∩ t : set Ξ±) ≃ s βŠ• t := calc (s βˆͺ t : set Ξ±) βŠ• (s ∩ t : set Ξ±) ≃ (s βˆͺ t \ s : set Ξ±) βŠ• (s ∩ t : set Ξ±) : by rw [union_diff_self] ... ≃ (s βŠ• (t \ s : set Ξ±)) βŠ• (s ∩ t : set Ξ±) : sum_congr (set.union (inter_diff_self _ _)) (equiv.refl _) ... ≃ s βŠ• (t \ s : set Ξ±) βŠ• (s ∩ t : set Ξ±) : sum_assoc _ _ _ ... ≃ s βŠ• (t \ s βˆͺ s ∩ t : set Ξ±) : sum_congr (equiv.refl _) begin refine (set.union' (βˆ‰ s) _ _).symm, exacts [Ξ» x hx, hx.2, Ξ» x hx, not_not_intro hx.1] end ... ≃ s βŠ• t : by { rw (_ : t \ s βˆͺ s ∩ t = t), rw [union_comm, inter_comm, inter_union_diff] } protected def prod {Ξ± Ξ²} (s : set Ξ±) (t : set Ξ²) : s.prod t ≃ s Γ— t := ⟨λp, ⟨⟨p.1.1, p.2.1⟩, ⟨p.1.2, p.2.2⟩⟩, Ξ»p, ⟨⟨p.1.1, p.2.1⟩, ⟨p.1.2, p.2.2⟩⟩, Ξ» ⟨⟨x, y⟩, ⟨h₁, hβ‚‚βŸ©βŸ©, rfl, Ξ» ⟨⟨x, hβ‚βŸ©, ⟨y, hβ‚‚βŸ©βŸ©, rfl⟩ protected noncomputable def image_of_inj_on {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (s : set Ξ±) (H : inj_on f s) : s ≃ (f '' s) := ⟨λ ⟨x, h⟩, ⟨f x, mem_image_of_mem f h⟩, Ξ» ⟨y, h⟩, ⟨classical.some h, (classical.some_spec h).1⟩, Ξ» ⟨x, h⟩, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).1 h (classical.some_spec (mem_image_of_mem f h)).2), Ξ» ⟨y, h⟩, subtype.eq (classical.some_spec h).2⟩ protected noncomputable def image {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (s : set Ξ±) (H : injective f) : s ≃ (f '' s) := equiv.set.image_of_inj_on f s (Ξ» x y hx hy hxy, H hxy) @[simp] theorem image_apply {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (s : set Ξ±) (H : injective f) (a h) : set.image f s H ⟨a, h⟩ = ⟨f a, mem_image_of_mem _ h⟩ := rfl protected noncomputable def range {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (H : injective f) : Ξ± ≃ range f := { to_fun := Ξ» x, ⟨f x, mem_range_self _⟩, inv_fun := Ξ» x, classical.some x.2, left_inv := Ξ» x, H (classical.some_spec (show f x ∈ range f, from mem_range_self _)), right_inv := Ξ» x, subtype.eq $ classical.some_spec x.2 } @[simp] theorem range_apply {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (H : injective f) (a) : set.range f H a = ⟨f a, set.mem_range_self _⟩ := rfl protected def congr {Ξ± Ξ² : Type*} (e : Ξ± ≃ Ξ²) : set Ξ± ≃ set Ξ² := ⟨λ s, e '' s, Ξ» t, e.symm '' t, symm_image_image e, symm_image_image e.symm⟩ protected def sep {Ξ± : Type u} (s : set Ξ±) (t : Ξ± β†’ Prop) : ({ x ∈ s | t x } : set Ξ±) ≃ { x : s | t x.1 } := (equiv.subtype_subtype_equiv_subtype_inter s t).symm end set noncomputable def of_bijective {Ξ± Ξ²} {f : Ξ± β†’ Ξ²} (hf : bijective f) : Ξ± ≃ Ξ² := ⟨f, Ξ» x, classical.some (hf.2 x), Ξ» x, hf.1 (classical.some_spec (hf.2 (f x))), Ξ» x, classical.some_spec (hf.2 x)⟩ @[simp] theorem of_bijective_to_fun {Ξ± Ξ²} {f : Ξ± β†’ Ξ²} (hf : bijective f) : (of_bijective hf : Ξ± β†’ Ξ²) = f := rfl def subtype_quotient_equiv_quotient_subtype (p₁ : Ξ± β†’ Prop) [s₁ : setoid Ξ±] [sβ‚‚ : setoid (subtype p₁)] (pβ‚‚ : quotient s₁ β†’ Prop) (hpβ‚‚ : βˆ€ a, p₁ a ↔ pβ‚‚ ⟦a⟧) (h : βˆ€ x y : subtype p₁, @setoid.r _ sβ‚‚ x y ↔ (x : Ξ±) β‰ˆ y) : {x // pβ‚‚ x} ≃ quotient sβ‚‚ := { to_fun := Ξ» a, quotient.hrec_on a.1 (Ξ» a h, ⟦⟨a, (hpβ‚‚ _).2 h⟩⟧) (Ξ» a b hab, hfunext (by rw quotient.sound hab) (Ξ» h₁ hβ‚‚ _, heq_of_eq (quotient.sound ((h _ _).2 hab)))) a.2, inv_fun := Ξ» a, quotient.lift_on a (Ξ» a, (⟨⟦a.1⟧, (hpβ‚‚ _).1 a.2⟩ : {x // pβ‚‚ x})) (Ξ» a b hab, subtype.eq' (quotient.sound ((h _ _).1 hab))), left_inv := Ξ» ⟨a, ha⟩, quotient.induction_on a (Ξ» a ha, rfl) ha, right_inv := Ξ» a, quotient.induction_on a (Ξ» ⟨a, ha⟩, rfl) } section swap variable [decidable_eq Ξ±] open decidable def swap_core (a b r : Ξ±) : Ξ± := if r = a then b else if r = b then a else r theorem swap_core_self (r a : Ξ±) : swap_core a a r = r := by { unfold swap_core, split_ifs; cc } theorem swap_core_swap_core (r a b : Ξ±) : swap_core a b (swap_core a b r) = r := by { unfold swap_core, split_ifs; cc } theorem swap_core_comm (r a b : Ξ±) : swap_core a b r = swap_core b a r := by { unfold swap_core, split_ifs; cc } /-- `swap a b` is the permutation that swaps `a` and `b` and leaves other values as is. -/ def swap (a b : Ξ±) : perm Ξ± := ⟨swap_core a b, swap_core a b, Ξ»r, swap_core_swap_core r a b, Ξ»r, swap_core_swap_core r a b⟩ theorem swap_self (a : Ξ±) : swap a a = equiv.refl _ := eq_of_to_fun_eq $ funext $ Ξ» r, swap_core_self r a theorem swap_comm (a b : Ξ±) : swap a b = swap b a := eq_of_to_fun_eq $ funext $ Ξ» r, swap_core_comm r _ _ theorem swap_apply_def (a b x : Ξ±) : swap a b x = if x = a then b else if x = b then a else x := rfl @[simp] theorem swap_apply_left (a b : Ξ±) : swap a b a = b := if_pos rfl @[simp] theorem swap_apply_right (a b : Ξ±) : swap a b b = a := by { by_cases h : b = a; simp [swap_apply_def, h], } theorem swap_apply_of_ne_of_ne {a b x : Ξ±} : x β‰  a β†’ x β‰  b β†’ swap a b x = x := by simp [swap_apply_def] {contextual := tt} @[simp] theorem swap_swap (a b : Ξ±) : (swap a b).trans (swap a b) = equiv.refl _ := eq_of_to_fun_eq $ funext $ Ξ» x, swap_core_swap_core _ _ _ theorem swap_comp_apply {a b x : Ξ±} (Ο€ : perm Ξ±) : Ο€.trans (swap a b) x = if Ο€ x = a then b else if Ο€ x = b then a else Ο€ x := by { cases Ο€, refl } @[simp] lemma swap_inv {Ξ± : Type*} [decidable_eq Ξ±] (x y : Ξ±) : (swap x y)⁻¹ = swap x y := rfl @[simp] lemma symm_trans_swap_trans [decidable_eq Ξ²] (a b : Ξ±) (e : Ξ± ≃ Ξ²) : (e.symm.trans (swap a b)).trans e = swap (e a) (e b) := equiv.ext _ _ (Ξ» x, begin have : βˆ€ a, e.symm x = a ↔ x = e a := Ξ» a, by { rw @eq_comm _ (e.symm x), split; intros; simp * at * }, simp [swap_apply_def, this], split_ifs; simp end) @[simp] lemma swap_mul_self {Ξ± : Type*} [decidable_eq Ξ±] (i j : Ξ±) : swap i j * swap i j = 1 := equiv.swap_swap i j @[simp] lemma swap_apply_self {Ξ± : Type*} [decidable_eq Ξ±] (i j a : Ξ±) : swap i j (swap i j a) = a := by rw [← perm.mul_apply, swap_mul_self, perm.one_apply] /-- Augment an equivalence with a prescribed mapping `f a = b` -/ def set_value (f : Ξ± ≃ Ξ²) (a : Ξ±) (b : Ξ²) : Ξ± ≃ Ξ² := (swap a (f.symm b)).trans f @[simp] theorem set_value_eq (f : Ξ± ≃ Ξ²) (a : Ξ±) (b : Ξ²) : set_value f a b a = b := by { dsimp [set_value], simp [swap_apply_left] } end swap protected lemma forall_congr {p : Ξ± β†’ Prop} {q : Ξ² β†’ Prop} (f : Ξ± ≃ Ξ²) (h : βˆ€{x}, p x ↔ q (f x)) : (βˆ€x, p x) ↔ (βˆ€y, q y) := begin split; intros hβ‚‚ x, { rw [←f.right_inv x], apply h.mp, apply hβ‚‚ }, apply h.mpr, apply hβ‚‚ end protected lemma forall_congr_left' {p : Ξ± β†’ Prop} (f : Ξ± ≃ Ξ²) : (βˆ€x, p x) ↔ (βˆ€y, p (f.symm y)) := equiv.forall_congr f (Ξ»x, by simp) protected lemma forall_congr_left {p : Ξ² β†’ Prop} (f : Ξ± ≃ Ξ²) : (βˆ€x, p (f x)) ↔ (βˆ€y, p y) := (equiv.forall_congr_left' f.symm).symm section variables (P : Ξ± β†’ Sort w) (e : Ξ± ≃ Ξ²) /-- Transport dependent functions through an equivalence of the base space. -/ def Pi_congr_left' : (Ξ  a, P a) ≃ (Ξ  b, P (e.symm b)) := { to_fun := Ξ» f x, f (e.symm x), inv_fun := Ξ» f x, begin rw [← e.symm_apply_apply x], exact f (e x) end, left_inv := Ξ» f, funext $ Ξ» x, eq_of_heq ((eq_rec_heq _ _).trans (by { dsimp, rw e.symm_apply_apply })), right_inv := Ξ» f, funext $ Ξ» x, eq_of_heq ((eq_rec_heq _ _).trans (by { rw e.apply_symm_apply })) } @[simp] lemma Pi_congr_left'_apply (f : Ξ  a, P a) (b : Ξ²) : ((Pi_congr_left' P e) f) b = f (e.symm b) := rfl @[simp] lemma Pi_congr_left'_symm_apply (g : Ξ  b, P (e.symm b)) (a : Ξ±) : ((Pi_congr_left' P e).symm g) a = (by { convert g (e a), simp }) := rfl end section variables (P : Ξ² β†’ Sort w) (e : Ξ± ≃ Ξ²) /-- Transporting dependent functions through an equivalence of the base, expressed as a "simplification". -/ def Pi_congr_left : (Ξ  a, P (e a)) ≃ (Ξ  b, P b) := (Pi_congr_left' P e.symm).symm end section variables {W : Ξ± β†’ Sort w} {Z : Ξ² β†’ Sort z} (h₁ : Ξ± ≃ Ξ²) (hβ‚‚ : Ξ  a : Ξ±, (W a ≃ Z (h₁ a))) /-- Transport dependent functions through an equivalence of the base spaces and a family of equivalences of the matching fibres. -/ def Pi_congr : (Ξ  a, W a) ≃ (Ξ  b, Z b) := (equiv.Pi_congr_right hβ‚‚).trans (equiv.Pi_congr_left _ h₁) end end equiv instance {Ξ±} [subsingleton Ξ±] : subsingleton (ulift Ξ±) := equiv.ulift.subsingleton instance {Ξ±} [subsingleton Ξ±] : subsingleton (plift Ξ±) := equiv.plift.subsingleton instance {Ξ±} [decidable_eq Ξ±] : decidable_eq (ulift Ξ±) := equiv.ulift.decidable_eq instance {Ξ±} [decidable_eq Ξ±] : decidable_eq (plift Ξ±) := equiv.plift.decidable_eq def unique_unique_equiv : unique (unique Ξ±) ≃ unique Ξ± := { to_fun := Ξ» h, h.default, inv_fun := Ξ» h, { default := h, uniq := Ξ» _, subsingleton.elim _ _ }, left_inv := Ξ» _, subsingleton.elim _ _, right_inv := Ξ» _, subsingleton.elim _ _ } def equiv_of_unique_of_unique [unique Ξ±] [unique Ξ²] : Ξ± ≃ Ξ² := { to_fun := Ξ» _, default Ξ², inv_fun := Ξ» _, default Ξ±, left_inv := Ξ» _, subsingleton.elim _ _, right_inv := Ξ» _, subsingleton.elim _ _ } def equiv_punit_of_unique [unique Ξ±] : Ξ± ≃ punit.{v} := equiv_of_unique_of_unique namespace quot /-- An equivalence `e : Ξ± ≃ Ξ²` generates an equivalence between quotient spaces, if `ra a₁ aβ‚‚ ↔ rb (e a₁) (e aβ‚‚). -/ protected def congr {ra : Ξ± β†’ Ξ± β†’ Prop} {rb : Ξ² β†’ Ξ² β†’ Prop} (e : Ξ± ≃ Ξ²) (eq : βˆ€a₁ aβ‚‚, ra a₁ aβ‚‚ ↔ rb (e a₁) (e aβ‚‚)) : quot ra ≃ quot rb := { to_fun := quot.map e (assume a₁ aβ‚‚, (eq a₁ aβ‚‚).1), inv_fun := quot.map e.symm (assume b₁ bβ‚‚ h, (eq (e.symm b₁) (e.symm bβ‚‚)).2 ((e.apply_symm_apply b₁).symm β–Έ (e.apply_symm_apply bβ‚‚).symm β–Έ h)), left_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.symm_apply_apply] }, right_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.apply_symm_apply] } } /-- Quotients are congruent on equivalences under equality of their relation. An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/ protected def congr_right {r r' : Ξ± β†’ Ξ± β†’ Prop} (eq : βˆ€a₁ aβ‚‚, r a₁ aβ‚‚ ↔ r' a₁ aβ‚‚) : quot r ≃ quot r' := quot.congr (equiv.refl Ξ±) eq /-- An equivalence `e : Ξ± ≃ Ξ²` generates an equivalence between the quotient space of `Ξ±` by a relation `ra` and the quotient space of `Ξ²` by the image of this relation under `e`. -/ protected def congr_left {r : Ξ± β†’ Ξ± β†’ Prop} (e : Ξ± ≃ Ξ²) : quot r ≃ quot (Ξ» b b', r (e.symm b) (e.symm b')) := @quot.congr Ξ± Ξ² r (Ξ» b b', r (e.symm b) (e.symm b')) e (Ξ» a₁ aβ‚‚, by simp only [e.symm_apply_apply]) end quot namespace quotient /-- An equivalence `e : Ξ± ≃ Ξ²` generates an equivalence between quotient spaces, if `ra a₁ aβ‚‚ ↔ rb (e a₁) (e aβ‚‚). -/ protected def congr {ra : setoid Ξ±} {rb : setoid Ξ²} (e : Ξ± ≃ Ξ²) (eq : βˆ€a₁ aβ‚‚, @setoid.r Ξ± ra a₁ aβ‚‚ ↔ @setoid.r Ξ² rb (e a₁) (e aβ‚‚)) : quotient ra ≃ quotient rb := quot.congr e eq /-- Quotients are congruent on equivalences under equality of their relation. An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/ protected def congr_right {r r' : setoid Ξ±} (eq : βˆ€a₁ aβ‚‚, @setoid.r Ξ± r a₁ aβ‚‚ ↔ @setoid.r Ξ± r' a₁ aβ‚‚) : quotient r ≃ quotient r' := quot.congr_right eq end quotient /-- If a function is a bijection between `univ` and a set `s` in the target type, it induces an equivalence between the original type and the type `↑s`. -/ noncomputable def set.bij_on.equiv {Ξ± : Type*} {Ξ² : Type*} {s : set Ξ²} (f : Ξ± β†’ Ξ²) (h : set.bij_on f set.univ s) : Ξ± ≃ s := begin have : function.bijective (Ξ» (x : Ξ±), (⟨f x, begin exact h.maps_to (set.mem_univ x) end⟩ : s)), { split, { assume x y hxy, apply h.inj_on (set.mem_univ x) (set.mem_univ y) (subtype.mk.inj hxy) }, { assume x, rcases h.surj_on x.2 with ⟨y, hy⟩, exact ⟨y, subtype.eq hy.2⟩ } }, exact equiv.of_bijective this end /-- The composition of an updated function with an equiv on a subset can be expressed as an updated function. -/ lemma dite_comp_equiv_update {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} {s : set Ξ±} (e : Ξ² ≃ s) (v : Ξ² β†’ Ξ³) (w : Ξ± β†’ Ξ³) (j : Ξ²) (x : Ξ³) [decidable_eq Ξ²] [decidable_eq Ξ±] [βˆ€ j, decidable (j ∈ s)] : (Ξ» (i : Ξ±), if h : i ∈ s then (function.update v j x) (e.symm ⟨i, h⟩) else w i) = function.update (Ξ» (i : Ξ±), if h : i ∈ s then v (e.symm ⟨i, h⟩) else w i) (e j) x := begin ext i, by_cases h : i ∈ s, { simp only [h, dif_pos], have A : e.symm ⟨i, h⟩ = j ↔ i = e j, by { rw equiv.symm_apply_eq, exact subtype.ext }, by_cases h' : i = e j, { rw [A.2 h', h'], simp }, { have : Β¬ e.symm ⟨i, h⟩ = j, by simpa [← A] using h', simp [h, h', this] } }, { have : i β‰  e j, by { contrapose! h, have : (e j : Ξ±) ∈ s := (e j).2, rwa ← h at this }, simp [h, this] } end
10921155943c10ef80ccab5667f979056f856f4c
9dc8cecdf3c4634764a18254e94d43da07142918
/src/linear_algebra/eigenspace.lean
3d2bdd6d8c239ca7e7e0d75ed341447545df7ab3
[ "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
29,281
lean
/- Copyright (c) 2020 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp -/ import linear_algebra.charpoly.basic import linear_algebra.finsupp import linear_algebra.matrix.to_lin import algebra.algebra.spectrum import order.hom.basic /-! # Eigenvectors and eigenvalues This file defines eigenspaces, eigenvalues, and eigenvalues, as well as their generalized counterparts. We follow Axler's approach [axler2015] because it allows us to derive many properties without choosing a basis and without using matrices. An eigenspace of a linear map `f` for a scalar `ΞΌ` is the kernel of the map `(f - ΞΌ β€’ id)`. The nonzero elements of an eigenspace are eigenvectors `x`. They have the property `f x = ΞΌ β€’ x`. If there are eigenvectors for a scalar `ΞΌ`, the scalar `ΞΌ` is called an eigenvalue. There is no consensus in the literature whether `0` is an eigenvector. Our definition of `has_eigenvector` permits only nonzero vectors. For an eigenvector `x` that may also be `0`, we write `x ∈ f.eigenspace ΞΌ`. A generalized eigenspace of a linear map `f` for a natural number `k` and a scalar `ΞΌ` is the kernel of the map `(f - ΞΌ β€’ id) ^ k`. The nonzero elements of a generalized eigenspace are generalized eigenvectors `x`. If there are generalized eigenvectors for a natural number `k` and a scalar `ΞΌ`, the scalar `ΞΌ` is called a generalized eigenvalue. ## References * [Sheldon Axler, *Linear Algebra Done Right*][axler2015] * https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors ## Tags eigenspace, eigenvector, eigenvalue, eigen -/ universes u v w namespace module namespace End open module principal_ideal_ring polynomial finite_dimensional open_locale polynomial variables {K R : Type v} {V M : Type w} [comm_ring R] [add_comm_group M] [module R M] [field K] [add_comm_group V] [module K V] /-- The submodule `eigenspace f ΞΌ` for a linear map `f` and a scalar `ΞΌ` consists of all vectors `x` such that `f x = ΞΌ β€’ x`. (Def 5.36 of [axler2015])-/ def eigenspace (f : End R M) (ΞΌ : R) : submodule R M := (f - algebra_map R (End R M) ΞΌ).ker @[simp] lemma eigenspace_zero (f : End R M) : f.eigenspace 0 = f.ker := by simp [eigenspace] /-- A nonzero element of an eigenspace is an eigenvector. (Def 5.7 of [axler2015]) -/ def has_eigenvector (f : End R M) (ΞΌ : R) (x : M) : Prop := x ∈ eigenspace f ΞΌ ∧ x β‰  0 /-- A scalar `ΞΌ` is an eigenvalue for a linear map `f` if there are nonzero vectors `x` such that `f x = ΞΌ β€’ x`. (Def 5.5 of [axler2015]) -/ def has_eigenvalue (f : End R M) (a : R) : Prop := eigenspace f a β‰  βŠ₯ /-- The eigenvalues of the endomorphism `f`, as a subtype of `R`. -/ def eigenvalues (f : End R M) : Type* := {ΞΌ : R // f.has_eigenvalue ΞΌ} instance (f : End R M) : has_coe f.eigenvalues R := coe_subtype lemma has_eigenvalue_of_has_eigenvector {f : End R M} {ΞΌ : R} {x : M} (h : has_eigenvector f ΞΌ x) : has_eigenvalue f ΞΌ := begin rw [has_eigenvalue, submodule.ne_bot_iff], use x, exact h, end lemma mem_eigenspace_iff {f : End R M} {ΞΌ : R} {x : M} : x ∈ eigenspace f ΞΌ ↔ f x = ΞΌ β€’ x := by rw [eigenspace, linear_map.mem_ker, linear_map.sub_apply, algebra_map_End_apply, sub_eq_zero] lemma has_eigenvector.apply_eq_smul {f : End R M} {ΞΌ : R} {x : M} (hx : f.has_eigenvector ΞΌ x) : f x = ΞΌ β€’ x := mem_eigenspace_iff.mp hx.1 lemma has_eigenvalue.exists_has_eigenvector {f : End R M} {ΞΌ : R} (hΞΌ : f.has_eigenvalue ΞΌ) : βˆƒ v, f.has_eigenvector ΞΌ v := submodule.exists_mem_ne_zero_of_ne_bot hΞΌ lemma mem_spectrum_of_has_eigenvalue {f : End R M} {ΞΌ : R} (hΞΌ : has_eigenvalue f ΞΌ) : ΞΌ ∈ spectrum R f := begin refine spectrum.mem_iff.mpr (Ξ» h_unit, _), set f' := linear_map.general_linear_group.to_linear_equiv h_unit.unit, rcases hΞΌ.exists_has_eigenvector with ⟨v, hv⟩, refine hv.2 ((linear_map.ker_eq_bot'.mp f'.ker) v (_ : ΞΌ β€’ v - f v = 0)), rw [hv.apply_eq_smul, sub_self] end lemma has_eigenvalue_iff_mem_spectrum [finite_dimensional K V] {f : End K V} {ΞΌ : K} : f.has_eigenvalue ΞΌ ↔ ΞΌ ∈ spectrum K f := iff.intro mem_spectrum_of_has_eigenvalue (Ξ» h, by rwa [spectrum.mem_iff, is_unit.sub_iff, linear_map.is_unit_iff_ker_eq_bot] at h) lemma eigenspace_div (f : End K V) (a b : K) (hb : b β‰  0) : eigenspace f (a / b) = (b β€’ f - algebra_map K (End K V) a).ker := calc eigenspace f (a / b) = eigenspace f (b⁻¹ * a) : by { rw [div_eq_mul_inv, mul_comm] } ... = (f - (b⁻¹ * a) β€’ linear_map.id).ker : rfl ... = (f - b⁻¹ β€’ a β€’ linear_map.id).ker : by rw smul_smul ... = (f - b⁻¹ β€’ algebra_map K (End K V) a).ker : rfl ... = (b β€’ (f - b⁻¹ β€’ algebra_map K (End K V) a)).ker : by rw linear_map.ker_smul _ b hb ... = (b β€’ f - algebra_map K (End K V) a).ker : by rw [smul_sub, smul_inv_smulβ‚€ hb] lemma eigenspace_aeval_polynomial_degree_1 (f : End K V) (q : K[X]) (hq : degree q = 1) : eigenspace f (- q.coeff 0 / q.leading_coeff) = (aeval f q).ker := calc eigenspace f (- q.coeff 0 / q.leading_coeff) = (q.leading_coeff β€’ f - algebra_map K (End K V) (- q.coeff 0)).ker : by { rw eigenspace_div, intro h, rw leading_coeff_eq_zero_iff_deg_eq_bot.1 h at hq, cases hq } ... = (aeval f (C q.leading_coeff * X + C (q.coeff 0))).ker : by { rw [C_mul', aeval_def], simp [algebra_map, algebra.to_ring_hom], } ... = (aeval f q).ker : by rwa ← eq_X_add_C_of_degree_eq_one lemma ker_aeval_ring_hom'_unit_polynomial (f : End K V) (c : (K[X])Λ£) : (aeval f (c : K[X])).ker = βŠ₯ := begin rw polynomial.eq_C_of_degree_eq_zero (degree_coe_units c), simp only [aeval_def, evalβ‚‚_C], apply ker_algebra_map_End, apply coeff_coe_units_zero_ne_zero c end theorem aeval_apply_of_has_eigenvector {f : End K V} {p : K[X]} {ΞΌ : K} {x : V} (h : f.has_eigenvector ΞΌ x) : aeval f p x = (p.eval ΞΌ) β€’ x := begin apply p.induction_on, { intro a, simp [module.algebra_map_End_apply] }, { intros p q hp hq, simp [hp, hq, add_smul] }, { intros n a hna, rw [mul_comm, pow_succ, mul_assoc, alg_hom.map_mul, linear_map.mul_apply, mul_comm, hna], simp only [mem_eigenspace_iff.1 h.1, smul_smul, aeval_X, eval_mul, eval_C, eval_pow, eval_X, linear_map.map_smulβ‚›β‚—, ring_hom.id_apply, mul_comm] } end section minpoly theorem is_root_of_has_eigenvalue {f : End K V} {ΞΌ : K} (h : f.has_eigenvalue ΞΌ) : (minpoly K f).is_root ΞΌ := begin rcases (submodule.ne_bot_iff _).1 h with ⟨w, ⟨H, ne0⟩⟩, refine or.resolve_right (smul_eq_zero.1 _) ne0, simp [← aeval_apply_of_has_eigenvector ⟨H, ne0⟩, minpoly.aeval K f], end variables [finite_dimensional K V] (f : End K V) variables {f} {ΞΌ : K} theorem has_eigenvalue_of_is_root (h : (minpoly K f).is_root ΞΌ) : f.has_eigenvalue ΞΌ := begin cases dvd_iff_is_root.2 h with p hp, rw [has_eigenvalue, eigenspace], intro con, cases (linear_map.is_unit_iff_ker_eq_bot _).2 con with u hu, have p_ne_0 : p β‰  0, { intro con, apply minpoly.ne_zero f.is_integral, rw [hp, con, mul_zero] }, have h_deg := minpoly.degree_le_of_ne_zero K f p_ne_0 _, { rw [hp, degree_mul, degree_X_sub_C, polynomial.degree_eq_nat_degree p_ne_0] at h_deg, norm_cast at h_deg, linarith, }, { have h_aeval := minpoly.aeval K f, revert h_aeval, simp [hp, ← hu] }, end theorem has_eigenvalue_iff_is_root : f.has_eigenvalue ΞΌ ↔ (minpoly K f).is_root ΞΌ := ⟨is_root_of_has_eigenvalue, has_eigenvalue_of_is_root⟩ /-- An endomorphism of a finite-dimensional vector space has finitely many eigenvalues. -/ noncomputable instance (f : End K V) : fintype f.eigenvalues := set.finite.fintype begin have h : minpoly K f β‰  0 := minpoly.ne_zero f.is_integral, convert (minpoly K f).root_set_finite K, ext ΞΌ, have : (ΞΌ ∈ {ΞΌ : K | f.eigenspace ΞΌ = βŠ₯ β†’ false}) ↔ Β¬f.eigenspace ΞΌ = βŠ₯ := by tauto, convert rfl.mpr this, simp [polynomial.root_set_def, polynomial.mem_roots h, ← has_eigenvalue_iff_is_root, has_eigenvalue] end end minpoly /-- Every linear operator on a vector space over an algebraically closed field has an eigenvalue. -/ -- This is Lemma 5.21 of [axler2015], although we are no longer following that proof. lemma exists_eigenvalue [is_alg_closed K] [finite_dimensional K V] [nontrivial V] (f : End K V) : βˆƒ (c : K), f.has_eigenvalue c := by { simp_rw has_eigenvalue_iff_mem_spectrum, exact spectrum.nonempty_of_is_alg_closed_of_finite_dimensional K f } noncomputable instance [is_alg_closed K] [finite_dimensional K V] [nontrivial V] (f : End K V) : inhabited f.eigenvalues := ⟨⟨f.exists_eigenvalue.some, f.exists_eigenvalue.some_spec⟩⟩ /-- The eigenspaces of a linear operator form an independent family of subspaces of `V`. That is, any eigenspace has trivial intersection with the span of all the other eigenspaces. -/ lemma eigenspaces_independent (f : End K V) : complete_lattice.independent f.eigenspace := begin classical, -- Define an operation from `Ξ β‚€ ΞΌ : K, f.eigenspace ΞΌ`, the vector space of of finitely-supported -- choices of an eigenvector from each eigenspace, to `V`, by sending a collection to its sum. let S : @linear_map K K _ _ (ring_hom.id K) (Ξ β‚€ ΞΌ : K, f.eigenspace ΞΌ) V (@dfinsupp.add_comm_monoid K (Ξ» ΞΌ, f.eigenspace ΞΌ) _) _ (@dfinsupp.module K _ (Ξ» ΞΌ, f.eigenspace ΞΌ) _ _ _) _ := @dfinsupp.lsum K K β„• _ V _ _ _ _ _ _ _ _ _ (Ξ» ΞΌ, (f.eigenspace ΞΌ).subtype), -- We need to show that if a finitely-supported collection `l` of representatives of the -- eigenspaces has sum `0`, then it itself is zero. suffices : βˆ€ l : Ξ β‚€ ΞΌ, f.eigenspace ΞΌ, S l = 0 β†’ l = 0, { rw complete_lattice.independent_iff_dfinsupp_lsum_injective, change function.injective S, rw ← @linear_map.ker_eq_bot K K (Ξ β‚€ ΞΌ, (f.eigenspace ΞΌ)) V _ _ (@dfinsupp.add_comm_group K (Ξ» ΞΌ, f.eigenspace ΞΌ) _), rw eq_bot_iff, exact this }, intros l hl, -- We apply induction on the finite set of eigenvalues from which `l` selects a nonzero -- eigenvector, i.e. on the support of `l`. induction h_l_support : l.support using finset.induction with ΞΌβ‚€ l_support' hΞΌβ‚€ ih generalizing l, -- If the support is empty, all coefficients are zero and we are done. { exact dfinsupp.support_eq_empty.1 h_l_support }, -- Now assume that the support of `l` contains at least one eigenvalue `ΞΌβ‚€`. We define a new -- collection of representatives `l'` to apply the induction hypothesis on later. The collection -- of representatives `l'` is derived from `l` by multiplying the coefficient of the eigenvector -- with eigenvalue `ΞΌ` by `ΞΌ - ΞΌβ‚€`. { let l' := dfinsupp.map_range.linear_map (Ξ» ΞΌ, (ΞΌ - ΞΌβ‚€) β€’ @linear_map.id K (f.eigenspace ΞΌ) _ _ _) l, -- The support of `l'` is the support of `l` without `ΞΌβ‚€`. have h_l_support' : l'.support = l_support', { rw [← finset.erase_insert hΞΌβ‚€, ← h_l_support], ext a, have : Β¬(a = ΞΌβ‚€ ∨ l a = 0) ↔ Β¬a = ΞΌβ‚€ ∧ Β¬l a = 0 := not_or_distrib, simp only [l', dfinsupp.map_range.linear_map_apply, dfinsupp.map_range_apply, dfinsupp.mem_support_iff, finset.mem_erase, id.def, linear_map.id_coe, linear_map.smul_apply, ne.def, smul_eq_zero, sub_eq_zero, this] }, -- The entries of `l'` add up to `0`. have total_l' : S l' = 0, { let g := f - algebra_map K (End K V) ΞΌβ‚€, let a : Ξ β‚€ ΞΌ : K, V := dfinsupp.map_range.linear_map (Ξ» ΞΌ, (f.eigenspace ΞΌ).subtype) l, calc S l' = dfinsupp.lsum β„• (Ξ» ΞΌ, (f.eigenspace ΞΌ).subtype.comp ((ΞΌ - ΞΌβ‚€) β€’ linear_map.id)) l : _ ... = dfinsupp.lsum β„• (Ξ» ΞΌ, g.comp (f.eigenspace ΞΌ).subtype) l : _ ... = dfinsupp.lsum β„• (Ξ» ΞΌ, g) a : _ ... = g (dfinsupp.lsum β„• (Ξ» ΞΌ, (linear_map.id : V β†’β‚—[K] V)) a) : _ ... = g (S l) : _ ... = 0 : by rw [hl, g.map_zero], { exact dfinsupp.sum_map_range_index.linear_map }, { congr, ext ΞΌ v, simp only [g, eq_self_iff_true, function.comp_app, id.def, linear_map.coe_comp, linear_map.id_coe, linear_map.smul_apply, linear_map.sub_apply, module.algebra_map_End_apply, sub_left_inj, sub_smul, submodule.coe_smul_of_tower, submodule.coe_sub, submodule.subtype_apply, mem_eigenspace_iff.1 v.prop], }, { rw dfinsupp.sum_map_range_index.linear_map }, { simp only [dfinsupp.sum_add_hom_apply, linear_map.id_coe, linear_map.map_dfinsupp_sum, id.def, linear_map.to_add_monoid_hom_coe, dfinsupp.lsum_apply_apply], }, { congr, simp only [S, a, dfinsupp.sum_map_range_index.linear_map, linear_map.id_comp] } }, -- Therefore, by the induction hypothesis, all entries of `l'` are zero. have l'_eq_0 := ih l' total_l' h_l_support', -- By the definition of `l'`, this means that `(ΞΌ - ΞΌβ‚€) β€’ l ΞΌ = 0` for all `ΞΌ`. have h_smul_eq_0 : βˆ€ ΞΌ, (ΞΌ - ΞΌβ‚€) β€’ l ΞΌ = 0, { intro ΞΌ, calc (ΞΌ - ΞΌβ‚€) β€’ l ΞΌ = l' ΞΌ : by simp only [l', linear_map.id_coe, id.def, linear_map.smul_apply, dfinsupp.map_range_apply, dfinsupp.map_range.linear_map_apply] ... = 0 : by { rw [l'_eq_0], refl } }, -- Thus, the eigenspace-representatives in `l` for all `ΞΌ β‰  ΞΌβ‚€` are `0`. have h_lΞΌ_eq_0 : βˆ€ ΞΌ : K, ΞΌ β‰  ΞΌβ‚€ β†’ l ΞΌ = 0, { intros ΞΌ hΞΌ, apply or_iff_not_imp_left.1 (smul_eq_zero.1 (h_smul_eq_0 ΞΌ)), rwa [sub_eq_zero] }, -- So if we sum over all these representatives, we obtain `0`. have h_sum_l_support'_eq_0 : finset.sum l_support' (Ξ» ΞΌ, (l ΞΌ : V)) = 0, { rw ←finset.sum_const_zero, apply finset.sum_congr rfl, intros ΞΌ hΞΌ, rw [submodule.coe_eq_zero, h_lΞΌ_eq_0], rintro rfl, exact hΞΌβ‚€ hΞΌ }, -- The only potentially nonzero eigenspace-representative in `l` is the one corresponding to -- `ΞΌβ‚€`. But since the overall sum is `0` by assumption, this representative must also be `0`. have : l ΞΌβ‚€ = 0, { simp only [S, dfinsupp.lsum_apply_apply, dfinsupp.sum_add_hom_apply, linear_map.to_add_monoid_hom_coe, dfinsupp.sum, h_l_support, submodule.subtype_apply, submodule.coe_eq_zero, finset.sum_insert hΞΌβ‚€, h_sum_l_support'_eq_0, add_zero] at hl, exact hl }, -- Thus, all coefficients in `l` are `0`. show l = 0, { ext ΞΌ, by_cases h_cases : ΞΌ = ΞΌβ‚€, { rwa [h_cases, set_like.coe_eq_coe, dfinsupp.coe_zero, pi.zero_apply] }, exact congr_arg (coe : _ β†’ V) (h_lΞΌ_eq_0 ΞΌ h_cases) }} end /-- Eigenvectors corresponding to distinct eigenvalues of a linear operator are linearly independent. (Lemma 5.10 of [axler2015]) We use the eigenvalues as indexing set to ensure that there is only one eigenvector for each eigenvalue in the image of `xs`. -/ lemma eigenvectors_linear_independent (f : End K V) (ΞΌs : set K) (xs : ΞΌs β†’ V) (h_eigenvec : βˆ€ ΞΌ : ΞΌs, f.has_eigenvector ΞΌ (xs ΞΌ)) : linear_independent K xs := complete_lattice.independent.linear_independent _ (f.eigenspaces_independent.comp subtype.coe_injective) (Ξ» ΞΌ, (h_eigenvec ΞΌ).1) (Ξ» ΞΌ, (h_eigenvec ΞΌ).2) /-- The generalized eigenspace for a linear map `f`, a scalar `ΞΌ`, and an exponent `k ∈ β„•` is the kernel of `(f - ΞΌ β€’ id) ^ k`. (Def 8.10 of [axler2015]). Furthermore, a generalized eigenspace for some exponent `k` is contained in the generalized eigenspace for exponents larger than `k`. -/ def generalized_eigenspace (f : End R M) (ΞΌ : R) : β„• β†’o submodule R M := { to_fun := Ξ» k, ((f - algebra_map R (End R M) ΞΌ) ^ k).ker, monotone' := Ξ» k m hm, begin simp only [← pow_sub_mul_pow _ hm], exact linear_map.ker_le_ker_comp ((f - algebra_map R (End R M) ΞΌ) ^ k) ((f - algebra_map R (End R M) ΞΌ) ^ (m - k)), end } @[simp] lemma mem_generalized_eigenspace (f : End R M) (ΞΌ : R) (k : β„•) (m : M) : m ∈ f.generalized_eigenspace ΞΌ k ↔ ((f - ΞΌ β€’ 1)^k) m = 0 := iff.rfl @[simp] lemma generalized_eigenspace_zero (f : End R M) (k : β„•) : f.generalized_eigenspace 0 k = (f^k).ker := by simp [module.End.generalized_eigenspace] /-- A nonzero element of a generalized eigenspace is a generalized eigenvector. (Def 8.9 of [axler2015])-/ def has_generalized_eigenvector (f : End R M) (ΞΌ : R) (k : β„•) (x : M) : Prop := x β‰  0 ∧ x ∈ generalized_eigenspace f ΞΌ k /-- A scalar `ΞΌ` is a generalized eigenvalue for a linear map `f` and an exponent `k ∈ β„•` if there are generalized eigenvectors for `f`, `k`, and `ΞΌ`. -/ def has_generalized_eigenvalue (f : End R M) (ΞΌ : R) (k : β„•) : Prop := generalized_eigenspace f ΞΌ k β‰  βŠ₯ /-- The generalized eigenrange for a linear map `f`, a scalar `ΞΌ`, and an exponent `k ∈ β„•` is the range of `(f - ΞΌ β€’ id) ^ k`. -/ def generalized_eigenrange (f : End R M) (ΞΌ : R) (k : β„•) : submodule R M := ((f - algebra_map R (End R M) ΞΌ) ^ k).range /-- The exponent of a generalized eigenvalue is never 0. -/ lemma exp_ne_zero_of_has_generalized_eigenvalue {f : End R M} {ΞΌ : R} {k : β„•} (h : f.has_generalized_eigenvalue ΞΌ k) : k β‰  0 := begin rintro rfl, exact h linear_map.ker_id end /-- The union of the kernels of `(f - ΞΌ β€’ id) ^ k` over all `k`. -/ def maximal_generalized_eigenspace (f : End R M) (ΞΌ : R) : submodule R M := ⨆ k, f.generalized_eigenspace ΞΌ k lemma generalized_eigenspace_le_maximal (f : End R M) (ΞΌ : R) (k : β„•) : f.generalized_eigenspace ΞΌ k ≀ f.maximal_generalized_eigenspace ΞΌ := le_supr _ _ @[simp] lemma mem_maximal_generalized_eigenspace (f : End R M) (ΞΌ : R) (m : M) : m ∈ f.maximal_generalized_eigenspace ΞΌ ↔ βˆƒ (k : β„•), ((f - ΞΌ β€’ 1)^k) m = 0 := by simp only [maximal_generalized_eigenspace, ← mem_generalized_eigenspace, submodule.mem_supr_of_chain] /-- If there exists a natural number `k` such that the kernel of `(f - ΞΌ β€’ id) ^ k` is the maximal generalized eigenspace, then this value is the least such `k`. If not, this value is not meaningful. -/ noncomputable def maximal_generalized_eigenspace_index (f : End R M) (ΞΌ : R) := monotonic_sequence_limit_index (f.generalized_eigenspace ΞΌ) /-- For an endomorphism of a Noetherian module, the maximal eigenspace is always of the form kernel `(f - ΞΌ β€’ id) ^ k` for some `k`. -/ lemma maximal_generalized_eigenspace_eq [h : is_noetherian R M] (f : End R M) (ΞΌ : R) : maximal_generalized_eigenspace f ΞΌ = f.generalized_eigenspace ΞΌ (maximal_generalized_eigenspace_index f ΞΌ) := begin rw is_noetherian_iff_well_founded at h, exact (well_founded.supr_eq_monotonic_sequence_limit h (f.generalized_eigenspace ΞΌ) : _), end /-- A generalized eigenvalue for some exponent `k` is also a generalized eigenvalue for exponents larger than `k`. -/ lemma has_generalized_eigenvalue_of_has_generalized_eigenvalue_of_le {f : End R M} {ΞΌ : R} {k : β„•} {m : β„•} (hm : k ≀ m) (hk : f.has_generalized_eigenvalue ΞΌ k) : f.has_generalized_eigenvalue ΞΌ m := begin unfold has_generalized_eigenvalue at *, contrapose! hk, rw [←le_bot_iff, ←hk], exact (f.generalized_eigenspace ΞΌ).monotone hm, end /-- The eigenspace is a subspace of the generalized eigenspace. -/ lemma eigenspace_le_generalized_eigenspace {f : End R M} {ΞΌ : R} {k : β„•} (hk : 0 < k) : f.eigenspace ΞΌ ≀ f.generalized_eigenspace ΞΌ k := (f.generalized_eigenspace ΞΌ).monotone (nat.succ_le_of_lt hk) /-- All eigenvalues are generalized eigenvalues. -/ lemma has_generalized_eigenvalue_of_has_eigenvalue {f : End R M} {ΞΌ : R} {k : β„•} (hk : 0 < k) (hΞΌ : f.has_eigenvalue ΞΌ) : f.has_generalized_eigenvalue ΞΌ k := begin apply has_generalized_eigenvalue_of_has_generalized_eigenvalue_of_le hk, rw [has_generalized_eigenvalue, generalized_eigenspace, order_hom.coe_fun_mk, pow_one], exact hΞΌ, end /-- All generalized eigenvalues are eigenvalues. -/ lemma has_eigenvalue_of_has_generalized_eigenvalue {f : End R M} {ΞΌ : R} {k : β„•} (hΞΌ : f.has_generalized_eigenvalue ΞΌ k) : f.has_eigenvalue ΞΌ := begin intros contra, apply hΞΌ, erw linear_map.ker_eq_bot at ⊒ contra, rw linear_map.coe_pow, exact function.injective.iterate contra k, end /-- Generalized eigenvalues are actually just eigenvalues. -/ @[simp] lemma has_generalized_eigenvalue_iff_has_eigenvalue {f : End R M} {ΞΌ : R} {k : β„•} (hk : 0 < k) : f.has_generalized_eigenvalue ΞΌ k ↔ f.has_eigenvalue ΞΌ := ⟨has_eigenvalue_of_has_generalized_eigenvalue, has_generalized_eigenvalue_of_has_eigenvalue hk⟩ /-- Every generalized eigenvector is a generalized eigenvector for exponent `finrank K V`. (Lemma 8.11 of [axler2015]) -/ lemma generalized_eigenspace_le_generalized_eigenspace_finrank [finite_dimensional K V] (f : End K V) (ΞΌ : K) (k : β„•) : f.generalized_eigenspace ΞΌ k ≀ f.generalized_eigenspace ΞΌ (finrank K V) := ker_pow_le_ker_pow_finrank _ _ /-- Generalized eigenspaces for exponents at least `finrank K V` are equal to each other. -/ lemma generalized_eigenspace_eq_generalized_eigenspace_finrank_of_le [finite_dimensional K V] (f : End K V) (ΞΌ : K) {k : β„•} (hk : finrank K V ≀ k) : f.generalized_eigenspace ΞΌ k = f.generalized_eigenspace ΞΌ (finrank K V) := ker_pow_eq_ker_pow_finrank_of_le hk /-- If `f` maps a subspace `p` into itself, then the generalized eigenspace of the restriction of `f` to `p` is the part of the generalized eigenspace of `f` that lies in `p`. -/ lemma generalized_eigenspace_restrict (f : End R M) (p : submodule R M) (k : β„•) (ΞΌ : R) (hfp : βˆ€ (x : M), x ∈ p β†’ f x ∈ p) : generalized_eigenspace (linear_map.restrict f hfp) ΞΌ k = submodule.comap p.subtype (f.generalized_eigenspace ΞΌ k) := begin simp only [generalized_eigenspace, order_hom.coe_fun_mk, ← linear_map.ker_comp], induction k with k ih, { rw [pow_zero, pow_zero, linear_map.one_eq_id], apply (submodule.ker_subtype _).symm }, { erw [pow_succ', pow_succ', linear_map.ker_comp, linear_map.ker_comp, ih, ← linear_map.ker_comp, linear_map.comp_assoc] }, end /-- If `p` is an invariant submodule of an endomorphism `f`, then the `ΞΌ`-eigenspace of the restriction of `f` to `p` is a submodule of the `ΞΌ`-eigenspace of `f`. -/ lemma eigenspace_restrict_le_eigenspace (f : End R M) {p : submodule R M} (hfp : βˆ€ x ∈ p, f x ∈ p) (ΞΌ : R) : (eigenspace (f.restrict hfp) ΞΌ).map p.subtype ≀ f.eigenspace ΞΌ := begin rintros a ⟨x, hx, rfl⟩, simp only [set_like.mem_coe, mem_eigenspace_iff, linear_map.restrict_apply] at hx ⊒, exact congr_arg coe hx end /-- Generalized eigenrange and generalized eigenspace for exponent `finrank K V` are disjoint. -/ lemma generalized_eigenvec_disjoint_range_ker [finite_dimensional K V] (f : End K V) (ΞΌ : K) : disjoint (f.generalized_eigenrange ΞΌ (finrank K V)) (f.generalized_eigenspace ΞΌ (finrank K V)) := begin have h := calc submodule.comap ((f - algebra_map _ _ ΞΌ) ^ finrank K V) (f.generalized_eigenspace ΞΌ (finrank K V)) = ((f - algebra_map _ _ ΞΌ) ^ finrank K V * (f - algebra_map K (End K V) ΞΌ) ^ finrank K V).ker : by { simpa only [generalized_eigenspace, order_hom.coe_fun_mk, ← linear_map.ker_comp] } ... = f.generalized_eigenspace ΞΌ (finrank K V + finrank K V) : by { rw ←pow_add, refl } ... = f.generalized_eigenspace ΞΌ (finrank K V) : by { rw generalized_eigenspace_eq_generalized_eigenspace_finrank_of_le, linarith }, rw [disjoint, generalized_eigenrange, linear_map.range_eq_map, submodule.map_inf_eq_map_inf_comap, top_inf_eq, h], apply submodule.map_comap_le end /-- If an invariant subspace `p` of an endomorphism `f` is disjoint from the `ΞΌ`-eigenspace of `f`, then the restriction of `f` to `p` has trivial `ΞΌ`-eigenspace. -/ lemma eigenspace_restrict_eq_bot {f : End R M} {p : submodule R M} (hfp : βˆ€ x ∈ p, f x ∈ p) {ΞΌ : R} (hΞΌp : disjoint (f.eigenspace ΞΌ) p) : eigenspace (f.restrict hfp) ΞΌ = βŠ₯ := begin rw eq_bot_iff, intros x hx, simpa using hΞΌp ⟨eigenspace_restrict_le_eigenspace f hfp ΞΌ ⟨x, hx, rfl⟩, x.prop⟩, end /-- The generalized eigenspace of an eigenvalue has positive dimension for positive exponents. -/ lemma pos_finrank_generalized_eigenspace_of_has_eigenvalue [finite_dimensional K V] {f : End K V} {k : β„•} {ΞΌ : K} (hx : f.has_eigenvalue ΞΌ) (hk : 0 < k): 0 < finrank K (f.generalized_eigenspace ΞΌ k) := calc 0 = finrank K (βŠ₯ : submodule K V) : by rw finrank_bot ... < finrank K (f.eigenspace ΞΌ) : submodule.finrank_lt_finrank_of_lt (bot_lt_iff_ne_bot.2 hx) ... ≀ finrank K (f.generalized_eigenspace ΞΌ k) : submodule.finrank_mono ((f.generalized_eigenspace ΞΌ).monotone (nat.succ_le_of_lt hk)) /-- A linear map maps a generalized eigenrange into itself. -/ lemma map_generalized_eigenrange_le {f : End K V} {ΞΌ : K} {n : β„•} : submodule.map f (f.generalized_eigenrange ΞΌ n) ≀ f.generalized_eigenrange ΞΌ n := calc submodule.map f (f.generalized_eigenrange ΞΌ n) = (f * ((f - algebra_map _ _ ΞΌ) ^ n)).range : (linear_map.range_comp _ _).symm ... = (((f - algebra_map _ _ ΞΌ) ^ n) * f).range : by rw algebra.mul_sub_algebra_map_pow_commutes ... = submodule.map ((f - algebra_map _ _ ΞΌ) ^ n) f.range : linear_map.range_comp _ _ ... ≀ f.generalized_eigenrange ΞΌ n : linear_map.map_le_range /-- The generalized eigenvectors span the entire vector space (Lemma 8.21 of [axler2015]). -/ lemma supr_generalized_eigenspace_eq_top [is_alg_closed K] [finite_dimensional K V] (f : End K V) : (⨆ (ΞΌ : K) (k : β„•), f.generalized_eigenspace ΞΌ k) = ⊀ := begin -- We prove the claim by strong induction on the dimension of the vector space. unfreezingI { induction h_dim : finrank K V using nat.strong_induction_on with n ih generalizing V }, cases n, -- If the vector space is 0-dimensional, the result is trivial. { rw ←top_le_iff, simp only [finrank_eq_zero.1 (eq.trans finrank_top h_dim), bot_le] }, -- Otherwise the vector space is nontrivial. { haveI : nontrivial V := finrank_pos_iff.1 (by { rw h_dim, apply nat.zero_lt_succ }), -- Hence, `f` has an eigenvalue `ΞΌβ‚€`. obtain βŸ¨ΞΌβ‚€, hΞΌβ‚€βŸ© : βˆƒ ΞΌβ‚€, f.has_eigenvalue ΞΌβ‚€ := exists_eigenvalue f, -- We define `ES` to be the generalized eigenspace let ES := f.generalized_eigenspace ΞΌβ‚€ (finrank K V), -- and `ER` to be the generalized eigenrange. let ER := f.generalized_eigenrange ΞΌβ‚€ (finrank K V), -- `f` maps `ER` into itself. have h_f_ER : βˆ€ (x : V), x ∈ ER β†’ f x ∈ ER, from Ξ» x hx, map_generalized_eigenrange_le (submodule.mem_map_of_mem hx), -- Therefore, we can define the restriction `f'` of `f` to `ER`. let f' : End K ER := f.restrict h_f_ER, -- The dimension of `ES` is positive have h_dim_ES_pos : 0 < finrank K ES, { dsimp only [ES], rw h_dim, apply pos_finrank_generalized_eigenspace_of_has_eigenvalue hΞΌβ‚€ (nat.zero_lt_succ n) }, -- and the dimensions of `ES` and `ER` add up to `finrank K V`. have h_dim_add : finrank K ER + finrank K ES = finrank K V, { apply linear_map.finrank_range_add_finrank_ker }, -- Therefore the dimension `ER` mus be smaller than `finrank K V`. have h_dim_ER : finrank K ER < n.succ, by linarith, -- This allows us to apply the induction hypothesis on `ER`: have ih_ER : (⨆ (ΞΌ : K) (k : β„•), f'.generalized_eigenspace ΞΌ k) = ⊀, from ih (finrank K ER) h_dim_ER f' rfl, -- The induction hypothesis gives us a statement about subspaces of `ER`. We can transfer this -- to a statement about subspaces of `V` via `submodule.subtype`: have ih_ER' : (⨆ (ΞΌ : K) (k : β„•), (f'.generalized_eigenspace ΞΌ k).map ER.subtype) = ER, by simp only [(submodule.map_supr _ _).symm, ih_ER, submodule.map_subtype_top ER], -- Moreover, every generalized eigenspace of `f'` is contained in the corresponding generalized -- eigenspace of `f`. have hff' : βˆ€ ΞΌ k, (f'.generalized_eigenspace ΞΌ k).map ER.subtype ≀ f.generalized_eigenspace ΞΌ k, { intros, rw generalized_eigenspace_restrict, apply submodule.map_comap_le }, -- It follows that `ER` is contained in the span of all generalized eigenvectors. have hER : ER ≀ ⨆ (ΞΌ : K) (k : β„•), f.generalized_eigenspace ΞΌ k, { rw ← ih_ER', exact suprβ‚‚_mono hff' }, -- `ES` is contained in this span by definition. have hES : ES ≀ ⨆ (ΞΌ : K) (k : β„•), f.generalized_eigenspace ΞΌ k, from le_trans (le_supr (Ξ» k, f.generalized_eigenspace ΞΌβ‚€ k) (finrank K V)) (le_supr (Ξ» (ΞΌ : K), ⨆ (k : β„•), f.generalized_eigenspace ΞΌ k) ΞΌβ‚€), -- Moreover, we know that `ER` and `ES` are disjoint. have h_disjoint : disjoint ER ES, from generalized_eigenvec_disjoint_range_ker f ΞΌβ‚€, -- Since the dimensions of `ER` and `ES` add up to the dimension of `V`, it follows that the -- span of all generalized eigenvectors is all of `V`. show (⨆ (ΞΌ : K) (k : β„•), f.generalized_eigenspace ΞΌ k) = ⊀, { rw [←top_le_iff, ←submodule.eq_top_of_disjoint ER ES h_dim_add h_disjoint], apply sup_le hER hES } } end end End end module
e768aea7842dc493cb5a0b599a9af8eca914e6fb
26ac254ecb57ffcb886ff709cf018390161a9225
/src/topology/algebra/polynomial.lean
3ab115ad55ec5bcb9273939c004285910656dbb3
[ "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
2,103
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Robert Y. Lewis Analytic facts about polynomials. -/ import topology.algebra.ring import data.polynomial.div import data.real.cau_seq open polynomial is_absolute_value @[nolint ge_or_gt] -- see Note [nolint_ge] lemma polynomial.tendsto_infinity {Ξ± Ξ² : Type*} [comm_ring Ξ±] [discrete_linear_ordered_field Ξ²] (abv : Ξ± β†’ Ξ²) [is_absolute_value abv] {p : polynomial Ξ±} (h : 0 < degree p) : βˆ€ x : Ξ², βˆƒ r > 0, βˆ€ z : Ξ±, r < abv z β†’ x < abv (p.eval z) := degree_pos_induction_on p h (Ξ» a ha x, ⟨max (x / abv a) 1, (lt_max_iff.2 (or.inr zero_lt_one)), Ξ» z hz, by simp [max_lt_iff, div_lt_iff' ((abv_pos abv).2 ha), abv_mul abv] at *; tauto⟩) (Ξ» p hp ih x, let ⟨r, hr0, hr⟩ := ih x in ⟨max r 1, lt_max_iff.2 (or.inr zero_lt_one), Ξ» z hz, by rw [eval_mul, eval_X, abv_mul abv]; calc x < abv (p.eval z) : hr _ (max_lt_iff.1 hz).1 ... ≀ abv (eval z p) * abv z : le_mul_of_ge_one_right (abv_nonneg _ _) (max_le_iff.1 (le_of_lt hz)).2⟩) (Ξ» p a hp ih x, let ⟨r, hr0, hr⟩ := ih (x + abv a) in ⟨r, hr0, Ξ» z hz, by rw [eval_add, eval_C, ← sub_neg_eq_add]; exact lt_of_lt_of_le (lt_sub_iff_add_lt.2 (by rw abv_neg abv; exact (hr z hz))) (le_trans (le_abs_self _) (abs_abv_sub_le_abv_sub _ _ _))⟩) lemma polynomial.continuous_eval {Ξ±} [comm_semiring Ξ±] [topological_space Ξ±] [topological_semiring Ξ±] (p : polynomial Ξ±) : continuous (Ξ» x, p.eval x) := begin apply p.induction, { convert continuous_const, ext, show polynomial.eval x 0 = 0, from rfl }, { intros a b f haf hb hcts, simp only [polynomial.eval_add], refine continuous.add _ hcts, have : βˆ€ x, finsupp.sum (finsupp.single a b) (Ξ» (e : β„•) (a : Ξ±), a * x ^ e) = b * x ^a, from Ξ» x, finsupp.sum_single_index (by simp), convert continuous.mul _ _, { ext, apply this }, { apply_instance }, { apply continuous_const }, { apply continuous_pow }} end
91e82005781b2d19ce68476163cbce838b71aa4d
d9ed0fce1c218297bcba93e046cb4e79c83c3af8
/library/tools/super/defs.lean
0ef5caa3ee9013f1e82dc64e7be6721b2f0476c6
[ "Apache-2.0" ]
permissive
leodemoura/lean_clone
005c63aa892a6492f2d4741ee3c2cb07a6be9d7f
cc077554b584d39bab55c360bc12a6fe7957afe6
refs/heads/master
1,610,506,475,484
1,482,348,354,000
1,482,348,543,000
77,091,586
0
0
null
null
null
null
UTF-8
Lean
false
false
1,205
lean
/- Copyright (c) 2016 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import .clause_ops .prover_state open tactic expr monad namespace super -- FIXME: how to cleanly unfold one level of definitions? -- what should one level even be? consider dvd->has_dvd->nat_has_dvd->... meta def try_unfold_one_def (type : expr) : tactic expr := do whnf type meta def try_unfold_def_left (c : clause) (i : β„•) : tactic (list clause) := on_left_at c i $ Ξ»t, do t' ← try_unfold_one_def t, ht' ← mk_local_def `h t', return [([ht'], ht')] meta def try_unfold_def_right (c : clause) (i : β„•) : tactic (list clause) := on_right_at c i $ Ξ»h, do t' ← try_unfold_one_def h^.local_type, hnt' ← mk_local_def `h (imp t' c^.local_false), return [([hnt'], app hnt' h)] @[super.inf] meta def unfold_def_inf : inf_decl := inf_decl.mk 40 $ take given, sequence' $ do r ← [try_unfold_def_right, try_unfold_def_left], -- NOTE: we cannot restrict to selected literals here -- as this might prevent factoring, e.g. _n>0_ ∨ is_pos(0) i ← list.range given^.c^.num_lits, [inf_if_successful 3 given (r given^.c i)] end super
2271421233234e80ee1626e5932fd5f69995ed8a
510e96af568b060ed5858226ad954c258549f143
/data/set/finite.lean
a8349438ab2067fc7a5203b4cafcbb65f795a7e8
[]
no_license
Shamrock-Frost/library_dev
cb6d1739237d81e17720118f72ba0a6db8a5906b
0245c71e4931d3aceeacf0aea776454f6ee03c9c
refs/heads/master
1,609,481,034,595
1,500,165,215,000
1,500,165,347,000
97,350,162
0
0
null
1,500,164,969,000
1,500,164,969,000
null
UTF-8
Lean
false
false
2,214
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 Finite sets -- assuming a classical logic. -/ import .basic noncomputable theory universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {ΞΉ : Sort w} open set lattice namespace set /- local attribute [instance] classical.decidable_inhabited local attribute [instance] classical.prop_decidable -/ inductive finite : set Ξ± β†’ Prop | empty : finite βˆ… | insert : βˆ€a s, a βˆ‰ s β†’ finite s β†’ finite (insert a s) attribute [simp] finite.empty @[simp] lemma finite_insert {a : Ξ±} {s : set Ξ±} (h : finite s) : finite (insert a s) := classical.by_cases (suppose a ∈ s, by simp [*]) (suppose a βˆ‰ s, finite.insert a s this h) @[simp] lemma finite_singleton {a : Ξ±} : finite ({a} : set Ξ±) := finite_insert finite.empty lemma finite_union {s t : set Ξ±} (hs : finite s) (ht : finite t) : finite (s βˆͺ t) := finite.drec_on ht (by simp [hs]) $ assume a t _ _, by simp; exact finite_insert lemma finite_subset {s : set Ξ±} (hs : finite s) : βˆ€{t}, t βŠ† s β†’ finite t := begin induction hs with a t' ha ht' ih, { intros t ht, simp [(subset_empty_iff t)^.mp ht, finite.empty] }, { intros t ht, have tm : finite (t \ {a}) := (ih $ show t \ {a} βŠ† t', from assume x ⟨hxt, hna⟩, or.resolve_left (ht hxt) (by simp at hna; assumption)), cases (classical.em $ a ∈ t) with ha hna, { exact have finite (insert a (t \ {a})), from finite_insert tm, show finite t, by simp [ha] at this; assumption }, { simp [sdiff_singleton_eq_same, hna] at tm, exact tm } } end lemma finite_image {s : set Ξ±} {f : Ξ± β†’ Ξ²} (h : finite s) : finite (f '' s) := begin induction h with a s' hns' hs' hi, simp [image_empty, finite.empty], simp [image_insert_eq, finite_insert, hi] end lemma finite_sUnion {s : set (set Ξ±)} (h : finite s) : (βˆ€t∈s, finite t) β†’ finite (⋃₀ s) := begin induction h with a s' hns' hs' hi, { simp [finite.empty] }, { intro h, simp, apply finite_union, { apply h, simp }, { exact (hi $ assume t ht, h _ $ mem_insert_of_mem _ ht) } } end end set
ca45869d6bac7ac0e77ea2c1f1838d5d9a6ad998
26ac254ecb57ffcb886ff709cf018390161a9225
/src/ring_theory/adjoin.lean
342ff688de680f39f7bde59e531fec70378bee1b
[ "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
8,707
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Adjoining elements to form subalgebras. -/ import ring_theory.polynomial import ring_theory.principal_ideal_domain universes u v w open submodule namespace algebra variables {R : Type u} {A : Type v} section semiring variables [comm_semiring R] [semiring A] variables [algebra R A] {s t : set A} open subsemiring theorem subset_adjoin : s βŠ† adjoin R s := set.subset.trans (set.subset_union_right _ _) subset_closure theorem adjoin_le {S : subalgebra R A} (H : s βŠ† S) : adjoin R s ≀ S := closure_le.2 $ set.union_subset S.range_le H theorem adjoin_le_iff {S : subalgebra R A} : adjoin R s ≀ S ↔ s βŠ† S:= ⟨set.subset.trans subset_adjoin, adjoin_le⟩ theorem adjoin_mono (H : s βŠ† t) : adjoin R s ≀ adjoin R t := closure_le.2 (set.subset.trans (set.union_subset_union_right _ H) subset_closure) variables (R A) @[simp] theorem adjoin_empty : adjoin R (βˆ… : set A) = βŠ₯ := eq_bot_iff.2 $ adjoin_le $ set.empty_subset _ variables (R) {A} (s) theorem adjoin_eq_span : (adjoin R s : submodule R A) = span R (monoid.closure s) := begin apply le_antisymm, { intros r hr, rcases mem_closure_iff_exists_list.1 hr with ⟨L, HL, rfl⟩, clear hr, induction L with hd tl ih, { exact zero_mem _ }, rw list.forall_mem_cons at HL, rw [list.map_cons, list.sum_cons], refine submodule.add_mem _ _ (ih HL.2), replace HL := HL.1, clear ih tl, suffices : βˆƒ z r (hr : r ∈ monoid.closure s), has_scalar.smul.{u v} z r = list.prod hd, { rcases this with ⟨z, r, hr, hzr⟩, rw ← hzr, exact smul_mem _ _ (subset_span hr) }, induction hd with hd tl ih, { exact ⟨1, 1, is_submonoid.one_mem, one_smul _ _⟩ }, rw list.forall_mem_cons at HL, rcases (ih HL.2) with ⟨z, r, hr, hzr⟩, rw [list.prod_cons, ← hzr], rcases HL.1 with ⟨hd, rfl⟩ | hs, { refine ⟨hd * z, r, hr, _⟩, rw [smul_def, smul_def, (algebra_map _ _).map_mul, _root_.mul_assoc] }, { exact ⟨z, hd * r, is_submonoid.mul_mem (monoid.subset_closure hs) hr, (mul_smul_comm _ _ _).symm⟩ } }, exact span_le.2 (show monoid.closure s βŠ† adjoin R s, from monoid.closure_subset subset_adjoin) end end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A] variables [algebra R A] {s t : set A} open subsemiring variables (R s t) theorem adjoin_union : adjoin R (s βˆͺ t) = (adjoin R s).under (adjoin (adjoin R s) t) := le_antisymm (closure_mono $ set.union_subset (set.range_subset_iff.2 $ Ξ» r, or.inl ⟨algebra_map R (adjoin R s) r, rfl⟩) (set.union_subset_union_left _ $ Ξ» x hxs, ⟨⟨_, subset_adjoin hxs⟩, rfl⟩)) (closure_le.2 $ set.union_subset (set.range_subset_iff.2 $ Ξ» x, adjoin_mono (set.subset_union_left _ _) x.2) (set.subset.trans (set.subset_union_right _ _) subset_adjoin)) theorem adjoin_eq_range : adjoin R s = (mv_polynomial.aeval R A (coe : s β†’ A)).range := le_antisymm (adjoin_le $ Ξ» x hx, ⟨mv_polynomial.X ⟨x, hx⟩, mv_polynomial.evalβ‚‚_X _ _ _⟩) (Ξ» x ⟨p, hp⟩, hp β–Έ mv_polynomial.induction_on p (Ξ» r, by rw [mv_polynomial.aeval_def, mv_polynomial.evalβ‚‚_C]; exact (adjoin R s).2 r) (Ξ» p q hp hq, by rw alg_hom.map_add; exact is_add_submonoid.add_mem hp hq) (Ξ» p ⟨n, hn⟩ hp, by rw [alg_hom.map_mul, mv_polynomial.aeval_def _ _ _ (mv_polynomial.X _), mv_polynomial.evalβ‚‚_X]; exact is_submonoid.mul_mem hp (subset_adjoin hn))) theorem adjoin_singleton_eq_range (x : A) : adjoin R {x} = (polynomial.aeval R A x).range := le_antisymm (adjoin_le $ set.singleton_subset_iff.2 ⟨polynomial.X, polynomial.evalβ‚‚_X _ _⟩) (Ξ» y ⟨p, hp⟩, hp β–Έ polynomial.induction_on p (Ξ» r, by rw [polynomial.aeval_def, polynomial.evalβ‚‚_C]; exact (adjoin R _).2 r) (Ξ» p q hp hq, by rw alg_hom.map_add; exact is_add_submonoid.add_mem hp hq) (Ξ» n r ih, by { rw [pow_succ', ← mul_assoc, alg_hom.map_mul, polynomial.aeval_def _ polynomial.X, polynomial.evalβ‚‚_X], exact is_submonoid.mul_mem ih (subset_adjoin rfl) })) theorem adjoin_union_coe_submodule : (adjoin R (s βˆͺ t) : submodule R A) = (adjoin R s) * (adjoin R t) := begin rw [adjoin_eq_span, adjoin_eq_span, adjoin_eq_span, span_mul_span], congr' 1, ext z, simp [monoid.mem_closure_union_iff, set.mem_mul], end end comm_semiring section ring variables [comm_ring R] [ring A] variables [algebra R A] {s t : set A} variables {R s t} open ring theorem adjoin_int (s : set R) : adjoin β„€ s = subalgebra_of_subring (closure s) := le_antisymm (adjoin_le subset_closure) (closure_subset subset_adjoin) theorem mem_adjoin_iff {s : set A} {x : A} : x ∈ adjoin R s ↔ x ∈ closure (set.range (algebra_map R A) βˆͺ s) := ⟨λ hx, subsemiring.closure_induction hx subset_closure is_add_submonoid.zero_mem is_submonoid.one_mem (Ξ» _ _, is_add_submonoid.add_mem) (Ξ» _ _, is_submonoid.mul_mem), suffices closure (set.range ⇑(algebra_map R A) βˆͺ s) βŠ† adjoin R s, from @this x, closure_subset subsemiring.subset_closure⟩ theorem adjoin_eq_ring_closure (s : set A) : (adjoin R s : set A) = closure (set.range (algebra_map R A) βˆͺ s) := set.ext $ Ξ» x, mem_adjoin_iff end ring section comm_ring variables [comm_ring R] [comm_ring A] variables [algebra R A] {s t : set A} variables {R s t} open ring theorem fg_trans (h1 : (adjoin R s : submodule R A).fg) (h2 : (adjoin (adjoin R s) t : submodule (adjoin R s) A).fg) : (adjoin R (s βˆͺ t) : submodule R A).fg := begin rcases fg_def.1 h1 with ⟨p, hp, hp'⟩, rcases fg_def.1 h2 with ⟨q, hq, hq'⟩, refine fg_def.2 ⟨p * q, hp.mul hq, le_antisymm _ _⟩, { rw [span_le], rintros _ ⟨x, y, hx, hy, rfl⟩, change x * y ∈ _, refine is_submonoid.mul_mem _ _, { have : x ∈ (adjoin R s : submodule R A), { rw ← hp', exact subset_span hx }, exact adjoin_mono (set.subset_union_left _ _) this }, have : y ∈ (adjoin (adjoin R s) t : submodule (adjoin R s) A), { rw ← hq', exact subset_span hy }, change y ∈ adjoin R (s βˆͺ t), rwa adjoin_union }, { intros r hr, change r ∈ adjoin R (s βˆͺ t) at hr, rw adjoin_union at hr, change r ∈ (adjoin (adjoin R s) t : submodule (adjoin R s) A) at hr, haveI := classical.dec_eq A, haveI := classical.dec_eq R, rw [← hq', ← set.image_id q, finsupp.mem_span_iff_total (adjoin R s)] at hr, rcases hr with ⟨l, hlq, rfl⟩, have := @finsupp.total_apply A A (adjoin R s), rw [this, finsupp.sum], refine sum_mem _ _, intros z hz, change (l z).1 * _ ∈ _, have : (l z).1 ∈ (adjoin R s : submodule R A) := (l z).2, rw [← hp', ← set.image_id p, finsupp.mem_span_iff_total R] at this, rcases this with ⟨l2, hlp, hl⟩, have := @finsupp.total_apply A A R, rw this at hl, rw [←hl, finsupp.sum_mul], refine sum_mem _ _, intros t ht, change _ * _ ∈ _, rw smul_mul_assoc, refine smul_mem _ _ _, exact subset_span ⟨t, z, hlp ht, hlq hz, rfl⟩ } end end comm_ring end algebra namespace subalgebra variables {R : Type u} {A : Type v} variables [comm_ring R] [comm_ring A] [algebra R A] /-- A subalgebra `S` is finitely generated if there exists `t : finset A` such that `algebra.adjoin R t = S`. -/ def fg (S : subalgebra R A) : Prop := βˆƒ t : finset A, algebra.adjoin R ↑t = S theorem fg_def {S : subalgebra R A} : S.fg ↔ βˆƒ t : set A, set.finite t ∧ algebra.adjoin R t = S := ⟨λ ⟨t, ht⟩, βŸ¨β†‘t, set.finite_mem_finset t, ht⟩, Ξ» ⟨t, ht1, ht2⟩, ⟨ht1.to_finset, by rwa set.finite.coe_to_finset⟩⟩ theorem fg_bot : (βŠ₯ : subalgebra R A).fg := βŸ¨βˆ…, algebra.adjoin_empty R A⟩ end subalgebra variables {R : Type u} {A : Type v} {B : Type w} variables [comm_ring R] [comm_ring A] [comm_ring B] [algebra R A] [algebra R B] instance alg_hom.is_noetherian_ring_range (f : A →ₐ[R] B) [is_noetherian_ring A] : is_noetherian_ring f.range := is_noetherian_ring_range f.to_ring_hom theorem is_noetherian_ring_of_fg {S : subalgebra R A} (HS : S.fg) [is_noetherian_ring R] : is_noetherian_ring S := let ⟨t, ht⟩ := HS in ht β–Έ (algebra.adjoin_eq_range R (↑t : set A)).symm β–Έ by haveI : is_noetherian_ring (mv_polynomial (↑t : set A) R) := mv_polynomial.is_noetherian_ring; convert alg_hom.is_noetherian_ring_range _; apply_instance theorem is_noetherian_ring_closure (s : set R) (hs : s.finite) : is_noetherian_ring (ring.closure s) := show is_noetherian_ring (subalgebra_of_subring (ring.closure s)), from algebra.adjoin_int s β–Έ is_noetherian_ring_of_fg (subalgebra.fg_def.2 ⟨s, hs, rfl⟩)
93d49feb3893315257cfb86ab5b60309bc139e25
4727251e0cd73359b15b664c3170e5d754078599
/src/data/rbmap/default.lean
c565e084d5c7497b4757ffed159211b573b15b70
[ "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
15,086
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 -/ import data.rbtree import data.rbmap.basic universes u v namespace rbmap variables {Ξ± : Type u} {Ξ² : Type v} {lt : Ξ± β†’ Ξ± β†’ Prop} /- Auxiliary instances -/ private def rbmap_lt_is_swo {Ξ± : Type u} {Ξ² : Type v} {lt : Ξ± β†’ Ξ± β†’ Prop} [is_strict_weak_order Ξ± lt] : is_strict_weak_order (Ξ± Γ— Ξ²) (rbmap_lt lt) := { irrefl := Ξ» _, irrefl_of lt _, trans := Ξ» _ _ _ h₁ hβ‚‚, trans_of lt h₁ hβ‚‚, incomp_trans := Ξ» _ _ _ h₁ hβ‚‚, incomp_trans_of lt h₁ hβ‚‚ } private def rbmap_lt_dec {Ξ± : Type u} {Ξ² : Type v} {lt : Ξ± β†’ Ξ± β†’ Prop} [h : decidable_rel lt] : decidable_rel (@rbmap_lt Ξ± Ξ² lt) := Ξ» a b, h a.1 b.1 local attribute [instance] rbmap_lt_is_swo rbmap_lt_dec /- Helper lemmas for reusing rbtree results. -/ private lemma to_rbtree_mem {k : Ξ±} {m : rbmap Ξ± Ξ² lt} : k ∈ m β†’ βˆƒ v : Ξ², rbtree.mem (k, v) m := begin cases m with n p; cases n; intros h, { exact false.elim h }, all_goals { existsi n_val.2, exact h } end private lemma eqv_entries_of_eqv_keys {k₁ kβ‚‚ : Ξ±} (v₁ vβ‚‚ : Ξ²) : k₁ β‰ˆ[lt] kβ‚‚ β†’ (k₁, v₁) β‰ˆ[rbmap_lt lt] (kβ‚‚, vβ‚‚) := id private lemma eqv_keys_of_eqv_entries {k₁ kβ‚‚ : Ξ±} {v₁ vβ‚‚ : Ξ²} : (k₁, v₁) β‰ˆ[rbmap_lt lt] (kβ‚‚, vβ‚‚) β†’ k₁ β‰ˆ[lt] kβ‚‚ := id private lemma eqv_entries [is_irrefl Ξ± lt] (k : Ξ±) (v₁ vβ‚‚ : Ξ²) : (k, v₁) β‰ˆ[rbmap_lt lt] (k, vβ‚‚) := and.intro (irrefl_of lt k) (irrefl_of lt k) private lemma to_rbmap_mem [is_strict_weak_order Ξ± lt] {k : Ξ±} {v : Ξ²} {m : rbmap Ξ± Ξ² lt} : rbtree.mem (k, v) m β†’ k ∈ m := begin cases m with n p; cases n; intros h, { exact false.elim h }, { simp [has_mem.mem, rbmap.mem], exact @rbtree.mem_of_mem_of_eqv _ _ _ ⟨rbnode.red_node n_lchild n_val n_rchild, p⟩ _ _ h (eqv_entries _ _ _) }, { simp [has_mem.mem, rbmap.mem], exact @rbtree.mem_of_mem_of_eqv _ _ _ ⟨rbnode.black_node n_lchild n_val n_rchild, p⟩ _ _ h (eqv_entries _ _ _) } end private lemma to_rbtree_mem' [is_strict_weak_order Ξ± lt] {k : Ξ±} {m : rbmap Ξ± Ξ² lt} (v : Ξ²) : k ∈ m β†’ rbtree.mem (k, v) m := begin intro h, cases to_rbtree_mem h with v' hm, apply rbtree.mem_of_mem_of_eqv hm, apply eqv_entries end lemma eq_some_of_to_value_eq_some {e : option (Ξ± Γ— Ξ²)} {v : Ξ²} : to_value e = some v β†’ βˆƒ k, e = some (k, v) := begin cases e with val; simp [to_value, false_implies_iff], { cases val, simp, intro h, subst v, constructor, refl } end lemma eq_none_of_to_value_eq_none {e : option (Ξ± Γ— Ξ²)} : to_value e = none β†’ e = none := by cases e; simp [to_value, false_implies_iff] /- Lemmas -/ lemma not_mem_mk_rbmap : βˆ€ (k : Ξ±), k βˆ‰ mk_rbmap Ξ± Ξ² lt := by simp [has_mem.mem, mk_rbmap, mk_rbtree, rbmap.mem] lemma not_mem_of_empty {m : rbmap Ξ± Ξ² lt} (k : Ξ±) : m.empty = tt β†’ k βˆ‰ m := by cases m with n p; cases n; simp [has_mem.mem, mk_rbmap, mk_rbtree, rbmap.mem, rbmap.empty, rbtree.empty, false_implies_iff] lemma mem_of_mem_of_eqv [is_strict_weak_order Ξ± lt] {m : rbmap Ξ± Ξ² lt} {k₁ kβ‚‚ : Ξ±} : k₁ ∈ m β†’ k₁ β‰ˆ[lt] kβ‚‚ β†’ kβ‚‚ ∈ m := begin intros h₁ hβ‚‚, have h₁ := to_rbtree_mem h₁, cases h₁ with v h₁, exact to_rbmap_mem (rbtree.mem_of_mem_of_eqv h₁ (eqv_entries_of_eqv_keys v v hβ‚‚)) end section decidable variables [decidable_rel lt] lemma not_mem_of_find_entry_none [is_strict_weak_order Ξ± lt] {k : Ξ±} {m : rbmap Ξ± Ξ² lt} : m.find_entry k = none β†’ k βˆ‰ m := begin cases m with t p, cases t; simp [find_entry], { intros, simp [has_mem.mem, rbmap.mem] }, all_goals { intro h, exact rbtree.not_mem_of_find_none h, } end lemma not_mem_of_find_none [is_strict_weak_order Ξ± lt] {k : Ξ±} {m : rbmap Ξ± Ξ² lt} : m.find k = none β†’ k βˆ‰ m := begin simp [find], intro h, have := eq_none_of_to_value_eq_none h, exact not_mem_of_find_entry_none this end lemma mem_of_find_entry_some [is_strict_weak_order Ξ± lt] {k₁ : Ξ±} {e : Ξ± Γ— Ξ²} {m : rbmap Ξ± Ξ² lt} : m.find_entry k₁ = some e β†’ k₁ ∈ m := begin cases m with t p, cases t; simp [find_entry, false_implies_iff], all_goals { intro h, exact rbtree.mem_of_find_some h } end lemma mem_of_find_some [is_strict_weak_order Ξ± lt] {k : Ξ±} {v : Ξ²} {m : rbmap Ξ± Ξ² lt} : m.find k = some v β†’ k ∈ m := begin simp [find], intro h, have := eq_some_of_to_value_eq_some h, cases this with _ he, exact mem_of_find_entry_some he end lemma find_entry_eq_find_entry_of_eqv [is_strict_weak_order Ξ± lt] {m : rbmap Ξ± Ξ² lt} {k₁ kβ‚‚ : Ξ±} : k₁ β‰ˆ[lt] kβ‚‚ β†’ m.find_entry k₁ = m.find_entry kβ‚‚ := begin intro h, cases m with t p, cases t; simp [find_entry], all_goals { apply rbtree.find_eq_find_of_eqv, apply eqv_entries_of_eqv_keys, assumption } end lemma find_eq_find_of_eqv [is_strict_weak_order Ξ± lt] {k₁ kβ‚‚ : Ξ±} (m : rbmap Ξ± Ξ² lt) : k₁ β‰ˆ[lt] kβ‚‚ β†’ m.find k₁ = m.find kβ‚‚ := begin intro h, simp [find], apply congr_arg, apply find_entry_eq_find_entry_of_eqv, assumption end lemma find_entry_correct [is_strict_weak_order Ξ± lt] (k : Ξ±) (m : rbmap Ξ± Ξ² lt) : k ∈ m ↔ (βˆƒ e, m.find_entry k = some e ∧ k β‰ˆ[lt] e.1) := begin apply iff.intro; cases m with t p, { intro h, have h := to_rbtree_mem h, cases h with v h₁, have hex := iff.mp (rbtree.find_correct _ _) h₁, cases hex with e hβ‚‚, existsi e, cases t; simp [find_entry] at ⊒ hβ‚‚, { simp [rbtree.find, rbnode.find] at hβ‚‚, cases hβ‚‚ }, { cases hβ‚‚ with h₂₁ hβ‚‚β‚‚, split, { have := rbtree.find_eq_find_of_eqv ⟨rbnode.red_node t_lchild t_val t_rchild, p⟩ (eqv_entries k v t_val.2), rw [←this], exact h₂₁ }, { cases e, apply eqv_keys_of_eqv_entries hβ‚‚β‚‚ } }, { cases hβ‚‚ with h₂₁ hβ‚‚β‚‚, split, { have := rbtree.find_eq_find_of_eqv ⟨rbnode.black_node t_lchild t_val t_rchild, p⟩ (eqv_entries k v t_val.2), rw [←this], exact h₂₁ }, { cases e, apply eqv_keys_of_eqv_entries hβ‚‚β‚‚ } } }, { intro h, cases h with e h, cases h with h₁ hβ‚‚, cases t; simp [find_entry] at h₁, { contradiction }, all_goals { exact to_rbmap_mem (rbtree.mem_of_find_some h₁) } } end lemma eqv_of_find_entry_some [is_strict_weak_order Ξ± lt] {k₁ kβ‚‚ : Ξ±} {v : Ξ²} {m : rbmap Ξ± Ξ² lt} : m.find_entry k₁ = some (kβ‚‚, v) β†’ k₁ β‰ˆ[lt] kβ‚‚ := begin cases m with t p, cases t; simp [find_entry, false_implies_iff], all_goals { intro h, exact eqv_keys_of_eqv_entries (rbtree.eqv_of_find_some h) } end lemma eq_of_find_entry_some [is_strict_total_order Ξ± lt] {k₁ kβ‚‚ : Ξ±} {v : Ξ²} {m : rbmap Ξ± Ξ² lt} : m.find_entry k₁ = some (kβ‚‚, v) β†’ k₁ = kβ‚‚ := Ξ» h, suffices k₁ β‰ˆ[lt] kβ‚‚, from eq_of_eqv_lt this, eqv_of_find_entry_some h lemma find_correct [is_strict_weak_order Ξ± lt] (k : Ξ±) (m : rbmap Ξ± Ξ² lt) : k ∈ m ↔ βˆƒ v, m.find k = some v := begin apply iff.intro, { intro h, have := iff.mp (find_entry_correct k m) h, cases this with e h, cases h with h₁ hβ‚‚, existsi e.2, simp [find, h₁, to_value] }, { intro h, cases h with v h, simp [find] at h, have h := eq_some_of_to_value_eq_some h, cases h with k' h, have heqv := eqv_of_find_entry_some h, exact iff.mpr (find_entry_correct k m) ⟨(k', v), ⟨h, heqv⟩⟩ } end lemma constains_correct [is_strict_weak_order Ξ± lt] (k : Ξ±) (m : rbmap Ξ± Ξ² lt) : k ∈ m ↔ m.contains k = tt := begin apply iff.intro, { intro h, have h := iff.mp (find_entry_correct k m) h, cases h with e h, cases h with h₁ hβ‚‚, simp [contains, h₁, option.is_some] }, { simp [contains], intro h, generalize he : find_entry m k = e, cases e, { simp [he, option.is_some] at h, contradiction }, { exact mem_of_find_entry_some he } } end lemma mem_insert_of_incomp [is_strict_weak_order Ξ± lt] {k₁ kβ‚‚ : Ξ±} (m : rbmap Ξ± Ξ² lt) (v : Ξ²) : (Β¬ lt k₁ kβ‚‚ ∧ Β¬ lt kβ‚‚ k₁) β†’ k₁ ∈ m.insert kβ‚‚ v := Ξ» h, to_rbmap_mem (rbtree.mem_insert_of_incomp m (eqv_entries_of_eqv_keys v v h)) lemma mem_insert [is_strict_weak_order Ξ± lt] (k : Ξ±) (m : rbmap Ξ± Ξ² lt) (v : Ξ²) : k ∈ m.insert k v := to_rbmap_mem (rbtree.mem_insert (k, v) m) lemma mem_insert_of_equiv [is_strict_weak_order Ξ± lt] {k₁ kβ‚‚ : Ξ±} (m : rbmap Ξ± Ξ² lt) (v : Ξ²) : k₁ β‰ˆ[lt] kβ‚‚ β†’ k₁ ∈ m.insert kβ‚‚ v := mem_insert_of_incomp m v lemma mem_insert_of_mem [is_strict_weak_order Ξ± lt] {k₁ : Ξ±} {m : rbmap Ξ± Ξ² lt} (kβ‚‚ : Ξ±) (v : Ξ²) : k₁ ∈ m β†’ k₁ ∈ m.insert kβ‚‚ v := Ξ» h, to_rbmap_mem (rbtree.mem_insert_of_mem (kβ‚‚, v) (to_rbtree_mem' v h)) lemma equiv_or_mem_of_mem_insert [is_strict_weak_order Ξ± lt] {k₁ kβ‚‚ : Ξ±} {v : Ξ²} {m : rbmap Ξ± Ξ² lt} : k₁ ∈ m.insert kβ‚‚ v β†’ k₁ β‰ˆ[lt] kβ‚‚ ∨ k₁ ∈ m := Ξ» h, or.elim (rbtree.equiv_or_mem_of_mem_insert (to_rbtree_mem' v h)) (Ξ» h, or.inl (eqv_keys_of_eqv_entries h)) (Ξ» h, or.inr (to_rbmap_mem h)) lemma incomp_or_mem_of_mem_ins [is_strict_weak_order Ξ± lt] {k₁ kβ‚‚ : Ξ±} {v : Ξ²} {m : rbmap Ξ± Ξ² lt} : k₁ ∈ m.insert kβ‚‚ v β†’ (Β¬ lt k₁ kβ‚‚ ∧ Β¬ lt kβ‚‚ k₁) ∨ k₁ ∈ m := equiv_or_mem_of_mem_insert lemma eq_or_mem_of_mem_ins [is_strict_total_order Ξ± lt] {k₁ kβ‚‚ : Ξ±} {v : Ξ²} {m : rbmap Ξ± Ξ² lt} : k₁ ∈ m.insert kβ‚‚ v β†’ k₁ = kβ‚‚ ∨ k₁ ∈ m := Ξ» h, suffices k₁ β‰ˆ[lt] kβ‚‚ ∨ k₁ ∈ m, by simp [eqv_lt_iff_eq] at this; assumption, incomp_or_mem_of_mem_ins h lemma find_entry_insert_of_eqv [is_strict_weak_order Ξ± lt] (m : rbmap Ξ± Ξ² lt) {k₁ kβ‚‚ : Ξ±} (v : Ξ²) : k₁ β‰ˆ[lt] kβ‚‚ β†’ (m.insert k₁ v).find_entry kβ‚‚ = some (k₁, v) := begin intro h, generalize h₁ : m.insert k₁ v = m', cases m' with t p, cases t, { have := mem_insert k₁ m v, rw [h₁] at this, apply absurd this, apply not_mem_mk_rbmap }, all_goals { simp [find_entry], rw [←h₁, insert], apply rbtree.find_insert_of_eqv, apply eqv_entries_of_eqv_keys _ _ h } end lemma find_entry_insert [is_strict_weak_order Ξ± lt] (m : rbmap Ξ± Ξ² lt) (k : Ξ±) (v : Ξ²) : (m.insert k v).find_entry k = some (k, v) := find_entry_insert_of_eqv m v (refl k) lemma find_insert_of_eqv [is_strict_weak_order Ξ± lt] (m : rbmap Ξ± Ξ² lt) {k₁ kβ‚‚ : Ξ±} (v : Ξ²) : k₁ β‰ˆ[lt] kβ‚‚ β†’ (m.insert k₁ v).find kβ‚‚ = some v := begin intro h, have := find_entry_insert_of_eqv m v h, simp [find, this, to_value] end lemma find_insert [is_strict_weak_order Ξ± lt] (m : rbmap Ξ± Ξ² lt) (k : Ξ±) (v : Ξ²) : (m.insert k v).find k = some v := find_insert_of_eqv m v (refl k) lemma find_entry_insert_of_disj [is_strict_weak_order Ξ± lt] {k₁ kβ‚‚ : Ξ±} (m : rbmap Ξ± Ξ² lt) (v : Ξ²) : lt k₁ kβ‚‚ ∨ lt kβ‚‚ k₁ β†’ (m.insert k₁ v).find_entry kβ‚‚ = m.find_entry kβ‚‚ := begin intro h, have h' : βˆ€ {v₁ vβ‚‚ : Ξ²}, (rbmap_lt lt) (k₁, v₁) (kβ‚‚, vβ‚‚) ∨ (rbmap_lt lt) (kβ‚‚, vβ‚‚) (k₁, v₁) := Ξ» _ _, h, generalize h₁ : m = m₁, generalize hβ‚‚ : insert m₁ k₁ v = mβ‚‚, rw [←h₁] at hβ‚‚ ⊒, rw [←hβ‚‚], cases m₁ with t₁ p₁; cases t₁; cases mβ‚‚ with tβ‚‚ pβ‚‚; cases tβ‚‚, { rw [hβ‚‚, h₁] }, iterate 2 { rw [hβ‚‚], conv { to_lhs, simp [find_entry] }, rw [←hβ‚‚, insert, rbtree.find_insert_of_disj _ h', h₁], refl }, any_goals { simp [insert] at hβ‚‚, exact absurd hβ‚‚ (rbtree.insert_ne_mk_rbtree m (k₁, v)) }, any_goals { rw [hβ‚‚, h₁], simp [find_entry], rw [←hβ‚‚, ←h₁, insert, rbtree.find_insert_of_disj _ h'], apply rbtree.find_eq_find_of_eqv, apply eqv_entries } end lemma find_entry_insert_of_not_eqv [is_strict_weak_order Ξ± lt] {k₁ kβ‚‚ : Ξ±} (m : rbmap Ξ± Ξ² lt) (v : Ξ²) : Β¬ k₁ β‰ˆ[lt] kβ‚‚ β†’ (m.insert k₁ v).find_entry kβ‚‚ = m.find_entry kβ‚‚ := begin intro hn, have he : lt k₁ kβ‚‚ ∨ lt kβ‚‚ k₁, { simp [strict_weak_order.equiv, decidable.not_and_iff_or_not, decidable.not_not_iff] at hn, assumption }, apply find_entry_insert_of_disj _ _ he end lemma find_entry_insert_of_ne [is_strict_total_order Ξ± lt] {k₁ kβ‚‚ : Ξ±} (m : rbmap Ξ± Ξ² lt) (v : Ξ²) : k₁ β‰  kβ‚‚ β†’ (m.insert k₁ v).find_entry kβ‚‚ = m.find_entry kβ‚‚ := begin intro h, have : Β¬ k₁ β‰ˆ[lt] kβ‚‚ := Ξ» h', h (eq_of_eqv_lt h'), apply find_entry_insert_of_not_eqv _ _ this end lemma find_insert_of_disj [is_strict_weak_order Ξ± lt] {k₁ kβ‚‚ : Ξ±} (m : rbmap Ξ± Ξ² lt) (v : Ξ²) : lt k₁ kβ‚‚ ∨ lt kβ‚‚ k₁ β†’ (m.insert k₁ v).find kβ‚‚ = m.find kβ‚‚ := begin intro h, have := find_entry_insert_of_disj m v h, simp [find, this] end lemma find_insert_of_not_eqv [is_strict_weak_order Ξ± lt] {k₁ kβ‚‚ : Ξ±} (m : rbmap Ξ± Ξ² lt) (v : Ξ²) : Β¬ k₁ β‰ˆ[lt] kβ‚‚ β†’ (m.insert k₁ v).find kβ‚‚ = m.find kβ‚‚ := begin intro h, have := find_entry_insert_of_not_eqv m v h, simp [find, this] end lemma find_insert_of_ne [is_strict_total_order Ξ± lt] {k₁ kβ‚‚ : Ξ±} (m : rbmap Ξ± Ξ² lt) (v : Ξ²) : k₁ β‰  kβ‚‚ β†’ (m.insert k₁ v).find kβ‚‚ = m.find kβ‚‚ := begin intro h, have := find_entry_insert_of_ne m v h, simp [find, this] end end decidable lemma mem_of_min_eq [is_strict_total_order Ξ± lt] {k : Ξ±} {v : Ξ²} {m : rbmap Ξ± Ξ² lt} : m.min = some (k, v) β†’ k ∈ m := Ξ» h, to_rbmap_mem (rbtree.mem_of_min_eq h) lemma mem_of_max_eq [is_strict_total_order Ξ± lt] {k : Ξ±} {v : Ξ²} {m : rbmap Ξ± Ξ² lt} : m.max = some (k, v) β†’ k ∈ m := Ξ» h, to_rbmap_mem (rbtree.mem_of_max_eq h) lemma eq_leaf_of_min_eq_none {m : rbmap Ξ± Ξ² lt} : m.min = none β†’ m = mk_rbmap Ξ± Ξ² lt := rbtree.eq_leaf_of_min_eq_none lemma eq_leaf_of_max_eq_none {m : rbmap Ξ± Ξ² lt} : m.max = none β†’ m = mk_rbmap Ξ± Ξ² lt := rbtree.eq_leaf_of_max_eq_none lemma min_is_minimal [is_strict_weak_order Ξ± lt] {k : Ξ±} {v : Ξ²} {m : rbmap Ξ± Ξ² lt} : m.min = some (k, v) β†’ βˆ€ {k'}, k' ∈ m β†’ k β‰ˆ[lt] k' ∨ lt k k' := Ξ» h k' hm, or.elim (rbtree.min_is_minimal h (to_rbtree_mem' v hm)) (Ξ» h, or.inl (eqv_keys_of_eqv_entries h)) (Ξ» h, or.inr h) lemma max_is_maximal [is_strict_weak_order Ξ± lt] {k : Ξ±} {v : Ξ²} {m : rbmap Ξ± Ξ² lt} : m.max = some (k, v) β†’ βˆ€ {k'}, k' ∈ m β†’ k β‰ˆ[lt] k' ∨ lt k' k := Ξ» h k' hm, or.elim (rbtree.max_is_maximal h (to_rbtree_mem' v hm)) (Ξ» h, or.inl (eqv_keys_of_eqv_entries h)) (Ξ» h, or.inr h) lemma min_is_minimal_of_total [is_strict_total_order Ξ± lt] {k : Ξ±} {v : Ξ²} {m : rbmap Ξ± Ξ² lt} : m.min = some (k, v) β†’ βˆ€ {k'}, k' ∈ m β†’ k = k' ∨ lt k k' := Ξ» h k' hm, match min_is_minimal h hm with | or.inl h := or.inl (eq_of_eqv_lt h) | or.inr h := or.inr h end lemma max_is_maximal_of_total [is_strict_total_order Ξ± lt] {k : Ξ±} {v : Ξ²} {m : rbmap Ξ± Ξ² lt} : m.max = some (k, v) β†’ βˆ€ {k'}, k' ∈ m β†’ k = k' ∨ lt k' k := Ξ» h k' hm, match max_is_maximal h hm with | or.inl h := or.inl (eq_of_eqv_lt h) | or.inr h := or.inr h end end rbmap
b98e8d3f1a310a8eb7bbe15428cfcc99ea209407
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/12_Axioms.org.12.lean
7efda64ecc078f641fe0d47963994200626aec6e
[]
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
1,922
lean
import standard import data.prod open prod prod.ops quot private definition eqv {A : Type} (p₁ pβ‚‚ : A Γ— A) : Prop := (p₁.1 = pβ‚‚.1 ∧ p₁.2 = pβ‚‚.2) ∨ (p₁.1 = pβ‚‚.2 ∧ p₁.2 = pβ‚‚.1) infix `~` := eqv open eq or local notation `⟨` H₁ `,` Hβ‚‚ `⟩` := and.intro H₁ Hβ‚‚ private theorem eqv.refl {A : Type} : βˆ€ p : A Γ— A, p ~ p := take p, inl ⟨rfl, rfl⟩ private theorem eqv.symm {A : Type} : βˆ€ p₁ pβ‚‚ : A Γ— A, p₁ ~ pβ‚‚ β†’ pβ‚‚ ~ p₁ | (a₁, aβ‚‚) (b₁, bβ‚‚) (inl ⟨a₁b₁, aβ‚‚bβ‚‚βŸ©) := inl ⟨symm a₁b₁, symm aβ‚‚bβ‚‚βŸ© | (a₁, aβ‚‚) (b₁, bβ‚‚) (inr ⟨a₁bβ‚‚, aβ‚‚bβ‚βŸ©) := inr ⟨symm aβ‚‚b₁, symm a₁bβ‚‚βŸ© private theorem eqv.trans {A : Type} : βˆ€ p₁ pβ‚‚ p₃ : A Γ— A, p₁ ~ pβ‚‚ β†’ pβ‚‚ ~ p₃ β†’ p₁ ~ p₃ | (a₁, aβ‚‚) (b₁, bβ‚‚) (c₁, cβ‚‚) (inl ⟨a₁b₁, aβ‚‚bβ‚‚βŸ©) (inl ⟨b₁c₁, bβ‚‚cβ‚‚βŸ©) := inl ⟨trans a₁b₁ b₁c₁, trans aβ‚‚bβ‚‚ bβ‚‚cβ‚‚βŸ© | (a₁, aβ‚‚) (b₁, bβ‚‚) (c₁, cβ‚‚) (inl ⟨a₁b₁, aβ‚‚bβ‚‚βŸ©) (inr ⟨b₁cβ‚‚, bβ‚‚cβ‚βŸ©) := inr ⟨trans a₁b₁ b₁cβ‚‚, trans aβ‚‚bβ‚‚ bβ‚‚cβ‚βŸ© | (a₁, aβ‚‚) (b₁, bβ‚‚) (c₁, cβ‚‚) (inr ⟨a₁bβ‚‚, aβ‚‚bβ‚βŸ©) (inl ⟨b₁c₁, bβ‚‚cβ‚‚βŸ©) := inr ⟨trans a₁bβ‚‚ bβ‚‚cβ‚‚, trans aβ‚‚b₁ b₁cβ‚βŸ© | (a₁, aβ‚‚) (b₁, bβ‚‚) (c₁, cβ‚‚) (inr ⟨a₁bβ‚‚, aβ‚‚bβ‚βŸ©) (inr ⟨b₁cβ‚‚, bβ‚‚cβ‚βŸ©) := inl ⟨trans a₁bβ‚‚ bβ‚‚c₁, trans aβ‚‚b₁ b₁cβ‚‚βŸ© private theorem is_equivalence (A : Type) : equivalence (@eqv A) := mk_equivalence (@eqv A) (@eqv.refl A) (@eqv.symm A) (@eqv.trans A) -- BEGIN definition uprod.setoid [instance] (A : Type) : setoid (A Γ— A) := setoid.mk (@eqv A) (is_equivalence A) definition uprod (A : Type) : Type := quot (uprod.setoid A) namespace uprod definition mk {A : Type} (a₁ aβ‚‚ : A) : uprod A := ⟦(a₁, aβ‚‚)⟧ notation `{` a₁ `,` aβ‚‚ `}` := mk a₁ aβ‚‚ end uprod -- END
84514738ac7bb37a77ca15dc7dc5d7b11816e92d
618003631150032a5676f229d13a079ac875ff77
/src/topology/algebra/ordered.lean
c10a71d995c4595276d59fbdaf06c8f28cceb259
[ "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
78,906
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, Yury Kudryashov -/ import tactic.tfae import order.liminf_limsup import data.set.intervals import topology.algebra.group /-! # Theory of topology on ordered spaces ## Main definitions The order topology on an ordered space is the topology generated by all open intervals (or equivalently by those of the form `(-∞, a)` and `(b, +∞)`). We define it as `preorder.topology Ξ±`. However, we do *not* register it as an instance (as many existing ordered types already have topologies, which would be equal but not definitionally equal to `preorder.topology Ξ±`). Instead, we introduce a class `order_topology Ξ±`(which is a `Prop`, also known as a mixin) saying that on the type `Ξ±` having already a topological space structure and a preorder structure, the topological structure is equal to the order topology. We also introduce another (mixin) class `order_closed_topology Ξ±` saying that the set of points `(x, y)` with `x ≀ y` is closed in the product space. This is automatically satisfied on a linear order with the order topology. We prove many basic properties of such topologies. ## Main statements This file contains the proofs of the following facts. For exact requirements (`order_closed_topology` vs `order_topology`, `preorder` vs `partial_order` vs `linear_order` etc) see their statements. ### Open / closed sets * `is_open_lt` : if `f` and `g` are continuous functions, then `{x | f x < g x}` is open; * `is_open_Iio`, `is_open_Ioi`, `is_open_Ioo` : open intervals are open; * `is_closed_le` : if `f` and `g` are continuous functions, then `{x | f x ≀ g x}` is closed; * `is_closed_Iic`, `is_closed_Ici`, `is_closed_Icc` : closed intervals are closed; * `frontier_le_subset_eq`, `frontier_lt_subset_eq` : frontiers of both `{x | f x ≀ g x}` and `{x | f x < g x}` are included by `{x | f x = g x}`; * `exists_Ioc_subset_of_mem_nhds`, `exists_Ico_subset_of_mem_nhds` : if `x < y`, then any neighborhood of `x` includes an interval `[x, z)` for some `z ∈ (x, y]`, and any neighborhood of `y` includes an interval `(z, y]` for some `z ∈ [x, y)`. ### Convergence and inequalities * `le_of_tendsto_of_tendsto` : if `f` converges to `a`, `g` converges to `b`, and eventually `f x ≀ g x`, then `a ≀ b` * `le_of_tendsto`, `ge_of_tendsto` : if `f` converges to `a` and eventually `f x ≀ b` (resp., `b ≀ f x`), then `a ≀ b` (resp., `b ≀ a); we also provide primed versions that assume the inequalities to hold for all `x`. ### Min, max, `Sup` and `Inf` * `continuous.min`, `continuous.max`: pointwise `min`/`max` of two continuous functions is continuous. * `tendsto.min`, `tendsto.max` : if `f` tends to `a` and `g` tends to `b`, then their pointwise `min`/`max` tend to `min a b` and `max a b`, respectively. * `tendsto_of_tendsto_of_tendsto_of_le_of_le` : theorem known as squeeze theorem, sandwich theorem, theorem of Carabinieri, and two policemen (and a drunk) theorem; if `g` and `h` both converge to `a`, and eventually `g x ≀ f x ≀ h x`, then `f` converges to `a`. ### Connected sets and Intermediate Value Theorem * `is_connected_I??` : all intervals `I??` are connected, * `is_connected.intermediate_value`, `intermediate_value_univ` : Intermediate Value Theorem for connected sets and connected spaces, respectively; * `intermediate_value_Icc`, `intermediate_value_Icc'`: Intermediate Value Theorem for functions on closed intervals. ### Miscellaneous facts * `compact.exists_forall_le`, `compact.exists_forall_ge` : extreme value theorem, a continuous function on a compact set takes its minimum and maximum values. * `is_closed.Icc_subset_of_forall_mem_nhds_within` : β€œContinuous induction” principle; if `s ∩ [a, b]` is closed, `a ∈ s`, and for each `x ∈ [a, b) ∩ s` some of its right neighborhoods is included `s`, then `[a, b] βŠ† s`. * `is_closed.Icc_subset_of_forall_exists_gt`, `is_closed.mem_of_ge_of_forall_exists_gt` : two other versions of the β€œcontinuous induction” principle. ## Implementation We do _not_ register the order topology as an instance on a preorder (or even on a linear order). Indeed, on many such spaces, a topology has already been constructed in a different way (think of the discrete spaces `β„•` or `β„€`, or `ℝ` that could inherit a topology as the completion of `β„š`), and is in general not defeq to the one generated by the intervals. We make it available as a definition `preorder.topology Ξ±` though, that can be registered as an instance when necessary, or for specific types. -/ open classical set filter topological_space open_locale topological_space classical universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} /-- A topology on a set which is both a topological space and a preorder is _order-closed_ if the set of points `(x, y)` with `x ≀ y` is closed in the product space. We introduce this as a mixin. This property is satisfied for the order topology on a linear order, but it can be satisfied more generally, and suffices to derive many interesting properties relating order and topology. -/ class order_closed_topology (Ξ± : Type*) [topological_space Ξ±] [preorder Ξ±] : Prop := (is_closed_le' : is_closed (Ξ»p:Ξ±Γ—Ξ±, p.1 ≀ p.2)) instance : Ξ  [topological_space Ξ±], topological_space (order_dual Ξ±) := id section order_closed_topology section preorder variables [topological_space Ξ±] [preorder Ξ±] [t : order_closed_topology Ξ±] include t lemma is_closed_le [topological_space Ξ²] {f g : Ξ² β†’ Ξ±} (hf : continuous f) (hg : continuous g) : is_closed {b | f b ≀ g b} := continuous_iff_is_closed.mp (hf.prod_mk hg) _ t.is_closed_le' lemma is_closed_le' (a : Ξ±) : is_closed {b | b ≀ a} := is_closed_le continuous_id continuous_const lemma is_closed_Iic {a : Ξ±} : is_closed (Iic a) := is_closed_le' a lemma is_closed_ge' (a : Ξ±) : is_closed {b | a ≀ b} := is_closed_le continuous_const continuous_id lemma is_closed_Ici {a : Ξ±} : is_closed (Ici a) := is_closed_ge' a instance : order_closed_topology (order_dual Ξ±) := ⟨continuous_swap _ (@order_closed_topology.is_closed_le' Ξ± _ _ _)⟩ lemma is_closed_Icc {a b : Ξ±} : is_closed (Icc a b) := is_closed_inter is_closed_Ici is_closed_Iic lemma le_of_tendsto_of_tendsto {f g : Ξ² β†’ Ξ±} {b : filter Ξ²} {a₁ aβ‚‚ : Ξ±} (hb : b β‰  βŠ₯) (hf : tendsto f b (𝓝 a₁)) (hg : tendsto g b (𝓝 aβ‚‚)) (h : βˆ€αΆ  x in b, f x ≀ g x) : a₁ ≀ aβ‚‚ := have tendsto (Ξ»b, (f b, g b)) b (𝓝 (a₁, aβ‚‚)), by rw [nhds_prod_eq]; exact hf.prod_mk hg, show (a₁, aβ‚‚) ∈ {p:Ξ±Γ—Ξ± | p.1 ≀ p.2}, from mem_of_closed_of_tendsto hb this t.is_closed_le' h lemma le_of_tendsto_of_tendsto' {f g : Ξ² β†’ Ξ±} {b : filter Ξ²} {a₁ aβ‚‚ : Ξ±} (hb : b β‰  βŠ₯) (hf : tendsto f b (𝓝 a₁)) (hg : tendsto g b (𝓝 aβ‚‚)) (h : βˆ€ x, f x ≀ g x) : a₁ ≀ aβ‚‚ := le_of_tendsto_of_tendsto hb hf hg (eventually_of_forall _ h) lemma le_of_tendsto {f : Ξ² β†’ Ξ±} {a b : Ξ±} {x : filter Ξ²} (nt : x β‰  βŠ₯) (lim : tendsto f x (𝓝 a)) (h : βˆ€αΆ  c in x, f c ≀ b) : a ≀ b := le_of_tendsto_of_tendsto nt lim tendsto_const_nhds h lemma le_of_tendsto' {f : Ξ² β†’ Ξ±} {a b : Ξ±} {x : filter Ξ²} (nt : x β‰  βŠ₯) (lim : tendsto f x (𝓝 a)) (h : βˆ€ c, f c ≀ b) : a ≀ b := le_of_tendsto nt lim (eventually_of_forall _ h) lemma ge_of_tendsto {f : Ξ² β†’ Ξ±} {a b : Ξ±} {x : filter Ξ²} (nt : x β‰  βŠ₯) (lim : tendsto f x (𝓝 a)) (h : βˆ€αΆ  c in x, b ≀ f c) : b ≀ a := le_of_tendsto_of_tendsto nt tendsto_const_nhds lim h lemma ge_of_tendsto' {f : Ξ² β†’ Ξ±} {a b : Ξ±} {x : filter Ξ²} (nt : x β‰  βŠ₯) (lim : tendsto f x (𝓝 a)) (h : βˆ€ c, b ≀ f c) : b ≀ a := ge_of_tendsto nt lim (eventually_of_forall _ h) @[simp] lemma closure_le_eq [topological_space Ξ²] {f g : Ξ² β†’ Ξ±} (hf : continuous f) (hg : continuous g) : closure {b | f b ≀ g b} = {b | f b ≀ g b} := closure_eq_iff_is_closed.mpr $ is_closed_le hf hg lemma closure_lt_subset_le [topological_space Ξ²] {f g : Ξ² β†’ Ξ±} (hf : continuous f) (hg : continuous g) : closure {b | f b < g b} βŠ† {b | f b ≀ g b} := by { rw [←closure_le_eq hf hg], exact closure_mono (Ξ» b, le_of_lt) } lemma continuous_within_at.closure_le [topological_space Ξ²] {f g : Ξ² β†’ Ξ±} {s : set Ξ²} {x : Ξ²} (hx : x ∈ closure s) (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) (h : βˆ€ y ∈ s, f y ≀ g y) : f x ≀ g x := begin show (f x, g x) ∈ {p : Ξ± Γ— Ξ± | p.1 ≀ p.2}, suffices : (f x, g x) ∈ closure {p : Ξ± Γ— Ξ± | p.1 ≀ p.2}, begin rwa closure_eq_iff_is_closed.2 at this, exact order_closed_topology.is_closed_le' end, exact (continuous_within_at.prod hf hg).mem_closure hx h end end preorder section partial_order variables [topological_space Ξ±] [partial_order Ξ±] [t : order_closed_topology Ξ±] include t private lemma is_closed_eq : is_closed {p : Ξ± Γ— Ξ± | p.1 = p.2} := by simp [le_antisymm_iff]; exact is_closed_inter t.is_closed_le' (is_closed_le continuous_snd continuous_fst) @[priority 90] -- see Note [lower instance priority] instance order_closed_topology.to_t2_space : t2_space Ξ± := { t2 := have is_open {p : Ξ± Γ— Ξ± | p.1 β‰  p.2}, from is_closed_eq, assume a b h, let ⟨u, v, hu, hv, ha, hb, h⟩ := is_open_prod_iff.mp this a b h in ⟨u, v, hu, hv, ha, hb, set.eq_empty_iff_forall_not_mem.2 $ assume a ⟨h₁, hβ‚‚βŸ©, have a β‰  a, from @h (a, a) ⟨h₁, hβ‚‚βŸ©, this rfl⟩ } end partial_order section linear_order variables [topological_space Ξ±] [linear_order Ξ±] [order_closed_topology Ξ±] lemma is_open_lt [topological_space Ξ²] {f g : Ξ² β†’ Ξ±} (hf : continuous f) (hg : continuous g) : is_open {b | f b < g b} := by simp [lt_iff_not_ge, -not_le]; exact is_closed_le hg hf variables {a b : Ξ±} lemma is_open_Iio : is_open (Iio a) := is_open_lt continuous_id continuous_const lemma is_open_Ioi : is_open (Ioi a) := is_open_lt continuous_const continuous_id lemma is_open_Ioo : is_open (Ioo a b) := is_open_inter is_open_Ioi is_open_Iio @[simp] lemma interior_Ioi : interior (Ioi a) = Ioi a := interior_eq_of_open is_open_Ioi @[simp] lemma interior_Iio : interior (Iio a) = Iio a := interior_eq_of_open is_open_Iio @[simp] lemma interior_Ioo : interior (Ioo a b) = Ioo a b := interior_eq_of_open is_open_Ioo lemma is_preconnected.forall_Icc_subset {s : set Ξ±} (hs : is_preconnected s) {a b : Ξ±} (ha : a ∈ s) (hb : b ∈ s) : Icc a b βŠ† s := begin assume x hx, obtain ⟨y, hy, hy'⟩ : (s ∩ ((Iic x) ∩ (Ici x))).nonempty, from is_preconnected_closed_iff.1 hs (Iic x) (Ici x) is_closed_Iic is_closed_Ici (Ξ» y _, le_total y x) ⟨a, ha, hx.1⟩ ⟨b, hb, hx.2⟩, exact le_antisymm hy'.1 hy'.2 β–Έ hy end /-- Intermediate Value Theorem for continuous functions on connected sets. -/ lemma is_preconnected.intermediate_value {Ξ³ : Type*} [topological_space Ξ³] {s : set Ξ³} (hs : is_preconnected s) {a b : Ξ³} (ha : a ∈ s) (hb : b ∈ s) {f : Ξ³ β†’ Ξ±} (hf : continuous_on f s) : Icc (f a) (f b) βŠ† f '' s := (hs.image f hf).forall_Icc_subset (mem_image_of_mem f ha) (mem_image_of_mem f hb) /-- Intermediate Value Theorem for continuous functions on connected spaces. -/ lemma intermediate_value_univ {Ξ³ : Type*} [topological_space Ξ³] [H : preconnected_space Ξ³] (a b : Ξ³) {f : Ξ³ β†’ Ξ±} (hf : continuous f) : Icc (f a) (f b) βŠ† range f := @image_univ _ _ f β–Έ H.is_preconnected_univ.intermediate_value trivial trivial hf.continuous_on end linear_order section decidable_linear_order variables [topological_space Ξ±] [decidable_linear_order Ξ±] [order_closed_topology Ξ±] {f g : Ξ² β†’ Ξ±} section variables [topological_space Ξ²] (hf : continuous f) (hg : continuous g) include hf hg lemma frontier_le_subset_eq : frontier {b | f b ≀ g b} βŠ† {b | f b = g b} := begin rw [frontier_eq_closure_inter_closure, closure_le_eq hf hg], rintros b ⟨hb₁, hbβ‚‚βŸ©, refine le_antisymm hb₁ (closure_lt_subset_le hg hf _), convert hbβ‚‚ using 2, simp only [not_le.symm], refl end lemma frontier_lt_subset_eq : frontier {b | f b < g b} βŠ† {b | f b = g b} := by rw ← frontier_compl; convert frontier_le_subset_eq hg hf; simp [ext_iff, eq_comm] lemma continuous.max : continuous (Ξ»b, max (f b) (g b)) := have βˆ€b∈frontier {b | f b ≀ g b}, g b = f b, from assume b hb, (frontier_le_subset_eq hf hg hb).symm, continuous_if this hg hf lemma continuous.min : continuous (Ξ»b, min (f b) (g b)) := have βˆ€b∈frontier {b | f b ≀ g b}, f b = g b, from assume b hb, frontier_le_subset_eq hf hg hb, continuous_if this hf hg end lemma tendsto.max {b : filter Ξ²} {a₁ aβ‚‚ : Ξ±} (hf : tendsto f b (𝓝 a₁)) (hg : tendsto g b (𝓝 aβ‚‚)) : tendsto (Ξ»b, max (f b) (g b)) b (𝓝 (max a₁ aβ‚‚)) := show tendsto ((Ξ»p:Ξ±Γ—Ξ±, max p.1 p.2) ∘ (Ξ»b, (f b, g b))) b (𝓝 (max a₁ aβ‚‚)), from tendsto.comp begin rw [←nhds_prod_eq], from continuous_iff_continuous_at.mp (continuous_fst.max continuous_snd) _ end (hf.prod_mk hg) lemma tendsto.min {b : filter Ξ²} {a₁ aβ‚‚ : Ξ±} (hf : tendsto f b (𝓝 a₁)) (hg : tendsto g b (𝓝 aβ‚‚)) : tendsto (Ξ»b, min (f b) (g b)) b (𝓝 (min a₁ aβ‚‚)) := show tendsto ((Ξ»p:Ξ±Γ—Ξ±, min p.1 p.2) ∘ (Ξ»b, (f b, g b))) b (𝓝 (min a₁ aβ‚‚)), from tendsto.comp begin rw [←nhds_prod_eq], from continuous_iff_continuous_at.mp (continuous_fst.min continuous_snd) _ end (hf.prod_mk hg) end decidable_linear_order end order_closed_topology /-- The order topology on an ordered type is the topology generated by open intervals. We register it on a preorder, but it is mostly interesting in linear orders, where it is also order-closed. We define it as a mixin. If you want to introduce the order topology on a preorder, use `preorder.topology`. -/ class order_topology (Ξ± : Type*) [t : topological_space Ξ±] [preorder Ξ±] : Prop := (topology_eq_generate_intervals : t = generate_from {s | βˆƒa, s = Ioi a ∨ s = Iio a}) /-- (Order) topology on a partial order `Ξ±` generated by the subbase of open intervals `(a, ∞) = { x ∣ a < x }, (-∞ , b) = {x ∣ x < b}` for all `a, b` in `Ξ±`. We do not register it as an instance as many ordered sets are already endowed with the same topology, most often in a non-defeq way though. Register as a local instance when necessary. -/ def preorder.topology (Ξ± : Type*) [preorder Ξ±] : topological_space Ξ± := generate_from {s : set Ξ± | βˆƒ (a : Ξ±), s = {b : Ξ± | a < b} ∨ s = {b : Ξ± | b < a}} section order_topology instance {Ξ± : Type*} [topological_space Ξ±] [partial_order Ξ±] [order_topology Ξ±] : order_topology (order_dual Ξ±) := ⟨by convert @order_topology.topology_eq_generate_intervals Ξ± _ _ _; conv in (_ ∨ _) { rw or.comm }; refl⟩ section partial_order variables [topological_space Ξ±] [partial_order Ξ±] [t : order_topology Ξ±] include t lemma is_open_iff_generate_intervals {s : set Ξ±} : is_open s ↔ generate_open {s | βˆƒa, s = Ioi a ∨ s = Iio a} s := by rw [t.topology_eq_generate_intervals]; refl lemma is_open_lt' (a : Ξ±) : is_open {b:Ξ± | a < b} := by rw [@is_open_iff_generate_intervals Ξ± _ _ t]; exact generate_open.basic _ ⟨a, or.inl rfl⟩ lemma is_open_gt' (a : Ξ±) : is_open {b:Ξ± | b < a} := by rw [@is_open_iff_generate_intervals Ξ± _ _ t]; exact generate_open.basic _ ⟨a, or.inr rfl⟩ lemma lt_mem_nhds {a b : Ξ±} (h : a < b) : βˆ€αΆ  x in 𝓝 b, a < x := mem_nhds_sets (is_open_lt' _) h lemma le_mem_nhds {a b : Ξ±} (h : a < b) : βˆ€αΆ  x in 𝓝 b, a ≀ x := (𝓝 b).sets_of_superset (lt_mem_nhds h) $ assume b hb, le_of_lt hb lemma gt_mem_nhds {a b : Ξ±} (h : a < b) : βˆ€αΆ  x in 𝓝 a, x < b := mem_nhds_sets (is_open_gt' _) h lemma ge_mem_nhds {a b : Ξ±} (h : a < b) : βˆ€αΆ  x in 𝓝 a, x ≀ b := (𝓝 a).sets_of_superset (gt_mem_nhds h) $ assume b hb, le_of_lt hb lemma nhds_eq_order (a : Ξ±) : 𝓝 a = (β¨…b ∈ Iio a, principal (Ioi b)) βŠ“ (β¨…b ∈ Ioi a, principal (Iio b)) := by rw [t.topology_eq_generate_intervals, nhds_generate_from]; from le_antisymm (le_inf (le_infi $ assume b, le_infi $ assume hb, infi_le_of_le {c : Ξ± | b < c} $ infi_le _ ⟨hb, b, or.inl rfl⟩) (le_infi $ assume b, le_infi $ assume hb, infi_le_of_le {c : Ξ± | c < b} $ infi_le _ ⟨hb, b, or.inr rfl⟩)) (le_infi $ assume s, le_infi $ assume ⟨ha, b, hs⟩, match s, ha, hs with | _, h, (or.inl rfl) := inf_le_left_of_le $ infi_le_of_le b $ infi_le _ h | _, h, (or.inr rfl) := inf_le_right_of_le $ infi_le_of_le b $ infi_le _ h end) @[nolint ge_or_gt] -- see Note [nolint_ge] lemma tendsto_order {f : Ξ² β†’ Ξ±} {a : Ξ±} {x : filter Ξ²} : tendsto f x (𝓝 a) ↔ (βˆ€ a' < a, βˆ€αΆ  b in x, a' < f b) ∧ (βˆ€ a' > a, βˆ€αΆ  b in x, f b < a') := by simp [nhds_eq_order a, tendsto_inf, tendsto_infi, tendsto_principal] /-- Also known as squeeze or sandwich theorem. This version assumes that inequalities hold eventually for the filter. -/ lemma tendsto_of_tendsto_of_tendsto_of_le_of_le' {f g h : Ξ² β†’ Ξ±} {b : filter Ξ²} {a : Ξ±} (hg : tendsto g b (𝓝 a)) (hh : tendsto h b (𝓝 a)) (hgf : βˆ€αΆ  b in b, g b ≀ f b) (hfh : βˆ€αΆ  b in b, f b ≀ h b) : tendsto f b (𝓝 a) := tendsto_order.2 ⟨assume a' h', have βˆ€αΆ  b in b, a' < g b, from (tendsto_order.1 hg).left a' h', by filter_upwards [this, hgf] assume a, lt_of_lt_of_le, assume a' h', have βˆ€αΆ  b in b, h b < a', from (tendsto_order.1 hh).right a' h', by filter_upwards [this, hfh] assume a h₁ hβ‚‚, lt_of_le_of_lt hβ‚‚ hβ‚βŸ© /-- Also known as squeeze or sandwich theorem. This version assumes that inequalities hold everywhere. -/ lemma tendsto_of_tendsto_of_tendsto_of_le_of_le {f g h : Ξ² β†’ Ξ±} {b : filter Ξ²} {a : Ξ±} (hg : tendsto g b (𝓝 a)) (hh : tendsto h b (𝓝 a)) (hgf : g ≀ f) (hfh : f ≀ h) : tendsto f b (𝓝 a) := tendsto_of_tendsto_of_tendsto_of_le_of_le' hg hh (eventually_of_forall _ hgf) (eventually_of_forall _ hfh) lemma nhds_order_unbounded {a : Ξ±} (hu : βˆƒu, a < u) (hl : βˆƒl, l < a) : 𝓝 a = (β¨…l (hβ‚‚ : l < a) u (hβ‚‚ : a < u), principal (Ioo l u)) := let ⟨u, hu⟩ := hu, ⟨l, hl⟩ := hl in calc 𝓝 a = (β¨…b<a, principal {c | b < c}) βŠ“ (β¨…b>a, principal {c | c < b}) : nhds_eq_order a ... = (β¨…b<a, principal {c | b < c} βŠ“ (β¨…b>a, principal {c | c < b})) : binfi_inf hl ... = (β¨…l<a, (β¨…u>a, principal {c | c < u} βŠ“ principal {c | l < c})) : begin congr, funext x, congr, funext hx, rw [inf_comm], apply binfi_inf hu end ... = _ : by simp [inter_comm]; refl lemma tendsto_order_unbounded {f : Ξ² β†’ Ξ±} {a : Ξ±} {x : filter Ξ²} (hu : βˆƒu, a < u) (hl : βˆƒl, l < a) (h : βˆ€l u, l < a β†’ a < u β†’ βˆ€αΆ  b in x, l < f b ∧ f b < u) : tendsto f x (𝓝 a) := by rw [nhds_order_unbounded hu hl]; from (tendsto_infi.2 $ assume l, tendsto_infi.2 $ assume hl, tendsto_infi.2 $ assume u, tendsto_infi.2 $ assume hu, tendsto_principal.2 $ h l u hl hu) end partial_order @[nolint ge_or_gt] -- see Note [nolint_ge] theorem induced_order_topology' {Ξ± : Type u} {Ξ² : Type v} [partial_order Ξ±] [ta : topological_space Ξ²] [partial_order Ξ²] [order_topology Ξ²] (f : Ξ± β†’ Ξ²) (hf : βˆ€ {x y}, f x < f y ↔ x < y) (H₁ : βˆ€ {a x}, x < f a β†’ βˆƒ b < a, x ≀ f b) (Hβ‚‚ : βˆ€ {a x}, f a < x β†’ βˆƒ b > a, f b ≀ x) : @order_topology _ (induced f ta) _ := begin letI := induced f ta, refine ⟨eq_of_nhds_eq_nhds (Ξ» a, _)⟩, rw [nhds_induced, nhds_generate_from, nhds_eq_order (f a)], apply le_antisymm, { refine le_infi (Ξ» s, le_infi $ Ξ» hs, le_principal_iff.2 _), rcases hs with ⟨ab, b, rfl|rfl⟩, { exact mem_comap_sets.2 ⟨{x | f b < x}, mem_inf_sets_of_left $ mem_infi_sets _ $ mem_infi_sets (hf.2 ab) $ mem_principal_self _, Ξ» x, hf.1⟩ }, { exact mem_comap_sets.2 ⟨{x | x < f b}, mem_inf_sets_of_right $ mem_infi_sets _ $ mem_infi_sets (hf.2 ab) $ mem_principal_self _, Ξ» x, hf.1⟩ } }, { rw [← map_le_iff_le_comap], refine le_inf _ _; refine le_infi (Ξ» x, le_infi $ Ξ» h, le_principal_iff.2 _); simp, { rcases H₁ h with ⟨b, ab, xb⟩, refine mem_infi_sets _ (mem_infi_sets ⟨ab, b, or.inl rfl⟩ (mem_principal_sets.2 _)), exact Ξ» c hc, lt_of_le_of_lt xb (hf.2 hc) }, { rcases Hβ‚‚ h with ⟨b, ab, xb⟩, refine mem_infi_sets _ (mem_infi_sets ⟨ab, b, or.inr rfl⟩ (mem_principal_sets.2 _)), exact Ξ» c hc, lt_of_lt_of_le (hf.2 hc) xb } }, end theorem induced_order_topology {Ξ± : Type u} {Ξ² : Type v} [partial_order Ξ±] [ta : topological_space Ξ²] [partial_order Ξ²] [order_topology Ξ²] (f : Ξ± β†’ Ξ²) (hf : βˆ€ {x y}, f x < f y ↔ x < y) (H : βˆ€ {x y}, x < y β†’ βˆƒ a, x < f a ∧ f a < y) : @order_topology _ (induced f ta) _ := induced_order_topology' f @hf (Ξ» a x xa, let ⟨b, xb, ba⟩ := H xa in ⟨b, hf.1 ba, le_of_lt xb⟩) (Ξ» a x ax, let ⟨b, ab, bx⟩ := H ax in ⟨b, hf.1 ab, le_of_lt bx⟩) lemma nhds_top_order [topological_space Ξ±] [order_top Ξ±] [order_topology Ξ±] : 𝓝 (⊀:Ξ±) = (β¨…l (hβ‚‚ : l < ⊀), principal (Ioi l)) := by simp [nhds_eq_order (⊀:Ξ±)] lemma nhds_bot_order [topological_space Ξ±] [order_bot Ξ±] [order_topology Ξ±] : 𝓝 (βŠ₯:Ξ±) = (β¨…l (hβ‚‚ : βŠ₯ < l), principal (Iio l)) := by simp [nhds_eq_order (βŠ₯:Ξ±)] section linear_order variables [topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±] lemma exists_Ioc_subset_of_mem_nhds' {a : Ξ±} {s : set Ξ±} (hs : s ∈ 𝓝 a) {l : Ξ±} (hl : l < a) : βˆƒ l' ∈ Ico l a, Ioc l' a βŠ† s := begin rw [nhds_eq_order a] at hs, rcases hs with ⟨t₁, ht₁, tβ‚‚, htβ‚‚, hts⟩, -- First we show that `tβ‚‚` includes `(-∞, a]`, so it suffices to show `(l', ∞) βŠ† t₁` suffices : βˆƒ l' ∈ Ico l a, Ioi l' βŠ† t₁, { have A : principal (Iic a) ≀ β¨… b ∈ Ioi a, principal (Iio b), from (le_infi $ Ξ» b, le_infi $ Ξ» hb, principal_mono.2 $ Iic_subset_Iio.2 hb), have B : t₁ ∩ Iic a βŠ† s, from subset.trans (inter_subset_inter_right _ (A htβ‚‚)) hts, from this.imp (Ξ» l', Exists.imp $ Ξ» hl' hl x hx, B ⟨hl hx.1, hx.2⟩) }, clear hts htβ‚‚ tβ‚‚, -- Now we find `l` such that `(l', ∞) βŠ† t₁` letI := classical.DLO Ξ±, rw [mem_binfi] at ht₁, { rcases ht₁ with ⟨b, hb, hb'⟩, exact ⟨max b l, ⟨le_max_right _ _, max_lt hb hl⟩, Ξ» x hx, hb' $ Ioi_subset_Ioi (le_max_left _ _) hx⟩ }, { intros b hb b' hb', simp only [mem_Iio] at hb hb', use [max b b', max_lt hb hb'], simp [le_refl] }, exact ⟨l, hl⟩ end lemma exists_Ico_subset_of_mem_nhds' {a : Ξ±} {s : set Ξ±} (hs : s ∈ 𝓝 a) {u : Ξ±} (hu : a < u) : βˆƒ u' ∈ Ioc a u, Ico a u' βŠ† s := begin convert @exists_Ioc_subset_of_mem_nhds' (order_dual Ξ±) _ _ _ _ _ hs _ hu, ext, rw [dual_Ico, dual_Ioc] end lemma exists_Ioc_subset_of_mem_nhds {a : Ξ±} {s : set Ξ±} (hs : s ∈ 𝓝 a) (h : βˆƒ l, l < a) : βˆƒ l < a, Ioc l a βŠ† s := let ⟨l', hl'⟩ := h in let ⟨l, hl⟩ := exists_Ioc_subset_of_mem_nhds' hs hl' in ⟨l, hl.fst.2, hl.snd⟩ lemma exists_Ico_subset_of_mem_nhds {a : Ξ±} {s : set Ξ±} (hs : s ∈ 𝓝 a) (h : βˆƒ u, a < u) : βˆƒ u (_ : a < u), Ico a u βŠ† s := let ⟨l', hl'⟩ := h in let ⟨l, hl⟩ := exists_Ico_subset_of_mem_nhds' hs hl' in ⟨l, hl.fst.1, hl.snd⟩ lemma mem_nhds_unbounded {a : Ξ±} {s : set Ξ±} (hu : βˆƒu, a < u) (hl : βˆƒl, l < a) : s ∈ 𝓝 a ↔ (βˆƒl u, l < a ∧ a < u ∧ βˆ€b, l < b β†’ b < u β†’ b ∈ s) := let ⟨l, hl'⟩ := hl, ⟨u, hu'⟩ := hu in have 𝓝 a = (β¨…p : {l // l < a} Γ— {u // a < u}, principal (Ioo p.1.val p.2.val)), by simp [nhds_order_unbounded hu hl, infi_subtype, infi_prod], iff.intro (assume hs, by rw [this] at hs; from infi_sets_induct hs ⟨l, u, hl', hu', by simp⟩ begin intro p, rcases p with ⟨⟨l, hl⟩, ⟨u, hu⟩⟩, simp [set.subset_def], intros s₁ sβ‚‚ hs₁ l' hl' u' hu' hsβ‚‚, letI := classical.DLO Ξ±, refine ⟨max l l', _, min u u', _⟩; simp [*, lt_min_iff, max_lt_iff] {contextual := tt} end (assume s₁ sβ‚‚ h ⟨l, u, h₁, hβ‚‚, hβ‚ƒβŸ©, ⟨l, u, h₁, hβ‚‚, assume b hu hl, h $ h₃ _ hu hl⟩)) (assume ⟨l, u, hl, hu, h⟩, by rw [this]; exact mem_infi_sets ⟨⟨l, hl⟩, ⟨u, hu⟩⟩ (assume b ⟨h₁, hβ‚‚βŸ©, h b h₁ hβ‚‚)) lemma order_separated {a₁ aβ‚‚ : Ξ±} (h : a₁ < aβ‚‚) : βˆƒu v : set Ξ±, is_open u ∧ is_open v ∧ a₁ ∈ u ∧ aβ‚‚ ∈ v ∧ (βˆ€bβ‚βˆˆu, βˆ€bβ‚‚βˆˆv, b₁ < bβ‚‚) := match dense_or_discrete a₁ aβ‚‚ with | or.inl ⟨a, ha₁, haβ‚‚βŸ© := ⟨{a' | a' < a}, {a' | a < a'}, is_open_gt' a, is_open_lt' a, ha₁, haβ‚‚, assume b₁ h₁ bβ‚‚ hβ‚‚, lt_trans h₁ hβ‚‚βŸ© | or.inr ⟨h₁, hβ‚‚βŸ© := ⟨{a | a < aβ‚‚}, {a | a₁ < a}, is_open_gt' aβ‚‚, is_open_lt' a₁, h, h, assume b₁ hb₁ bβ‚‚ hbβ‚‚, calc b₁ ≀ a₁ : hβ‚‚ _ hb₁ ... < aβ‚‚ : h ... ≀ bβ‚‚ : h₁ _ hbβ‚‚βŸ© end @[priority 100] -- see Note [lower instance priority] instance order_topology.to_order_closed_topology : order_closed_topology Ξ± := { is_closed_le' := is_open_prod_iff.mpr $ assume a₁ aβ‚‚ (h : Β¬ a₁ ≀ aβ‚‚), have h : aβ‚‚ < a₁, from lt_of_not_ge h, let ⟨u, v, hu, hv, ha₁, haβ‚‚, h⟩ := order_separated h in ⟨v, u, hv, hu, haβ‚‚, ha₁, assume ⟨b₁, bβ‚‚βŸ© ⟨h₁, hβ‚‚βŸ©, not_le_of_gt $ h bβ‚‚ hβ‚‚ b₁ hβ‚βŸ© } lemma order_topology.t2_space : t2_space Ξ± := by apply_instance @[priority 100] -- see Note [lower instance priority] instance order_topology.regular_space : regular_space Ξ± := { regular := assume s a hs ha, have hs' : -s ∈ 𝓝 a, from mem_nhds_sets hs ha, have βˆƒt:set Ξ±, is_open t ∧ (βˆ€l∈ s, l < a β†’ l ∈ t) ∧ 𝓝 a βŠ“ principal t = βŠ₯, from by_cases (assume h : βˆƒl, l < a, let ⟨l, hl, h⟩ := exists_Ioc_subset_of_mem_nhds hs' h in match dense_or_discrete l a with | or.inl ⟨b, hb₁, hbβ‚‚βŸ© := ⟨{a | a < b}, is_open_gt' _, assume c hcs hca, show c < b, from lt_of_not_ge $ assume hbc, h ⟨lt_of_lt_of_le hb₁ hbc, le_of_lt hca⟩ hcs, inf_principal_eq_bot $ (𝓝 a).sets_of_superset (mem_nhds_sets (is_open_lt' _) hbβ‚‚) $ assume x (hx : b < x), show Β¬ x < b, from not_lt.2 $ le_of_lt hx⟩ | or.inr ⟨h₁, hβ‚‚βŸ© := ⟨{a' | a' < a}, is_open_gt' _, assume b hbs hba, hba, inf_principal_eq_bot $ (𝓝 a).sets_of_superset (mem_nhds_sets (is_open_lt' _) hl) $ assume x (hx : l < x), show Β¬ x < a, from not_lt.2 $ h₁ _ hx⟩ end) (assume : Β¬ βˆƒl, l < a, βŸ¨βˆ…, is_open_empty, assume l _ hl, (this ⟨l, hl⟩).elim, by rw [principal_empty, inf_bot_eq]⟩), let ⟨t₁, ht₁o, ht₁s, ht₁a⟩ := this in have βˆƒt:set Ξ±, is_open t ∧ (βˆ€u∈ s, u>a β†’ u ∈ t) ∧ 𝓝 a βŠ“ principal t = βŠ₯, from by_cases (assume h : βˆƒu, u > a, let ⟨u, hu, h⟩ := exists_Ico_subset_of_mem_nhds hs' h in match dense_or_discrete a u with | or.inl ⟨b, hb₁, hbβ‚‚βŸ© := ⟨{a | b < a}, is_open_lt' _, assume c hcs hca, show c > b, from lt_of_not_ge $ assume hbc, h ⟨le_of_lt hca, lt_of_le_of_lt hbc hbβ‚‚βŸ© hcs, inf_principal_eq_bot $ (𝓝 a).sets_of_superset (mem_nhds_sets (is_open_gt' _) hb₁) $ assume x (hx : b > x), show Β¬ x > b, from not_lt.2 $ le_of_lt hx⟩ | or.inr ⟨h₁, hβ‚‚βŸ© := ⟨{a' | a' > a}, is_open_lt' _, assume b hbs hba, hba, inf_principal_eq_bot $ (𝓝 a).sets_of_superset (mem_nhds_sets (is_open_gt' _) hu) $ assume x (hx : u > x), show Β¬ x > a, from not_lt.2 $ hβ‚‚ _ hx⟩ end) (assume : Β¬ βˆƒu, u > a, βŸ¨βˆ…, is_open_empty, assume l _ hl, (this ⟨l, hl⟩).elim, by rw [principal_empty, inf_bot_eq]⟩), let ⟨tβ‚‚, htβ‚‚o, htβ‚‚s, htβ‚‚a⟩ := this in ⟨t₁ βˆͺ tβ‚‚, is_open_union ht₁o htβ‚‚o, assume x hx, have x β‰  a, from assume eq, ha $ eq β–Έ hx, (ne_iff_lt_or_gt.mp this).imp (ht₁s _ hx) (htβ‚‚s _ hx), by rw [←sup_principal, inf_sup_left, ht₁a, htβ‚‚a, bot_sup_eq]⟩, ..order_topology.t2_space } /-- A set is a neighborhood of `a` if and only if it contains an interval `(l, u)` containing `a`, provided `a` is neither a bottom element nor a top element. -/ lemma mem_nhds_iff_exists_Ioo_subset' {a l' u' : Ξ±} {s : set Ξ±} (hl' : l' < a) (hu' : a < u') : s ∈ 𝓝 a ↔ βˆƒl u, a ∈ Ioo l u ∧ Ioo l u βŠ† s := begin split, { assume h, rcases exists_Ico_subset_of_mem_nhds' h hu' with ⟨u, au, hu⟩, rcases exists_Ioc_subset_of_mem_nhds' h hl' with ⟨l, la, hl⟩, refine ⟨l, u, ⟨la.2, au.1⟩, Ξ»x hx, _⟩, cases le_total a x with hax hax, { exact hu ⟨hax, hx.2⟩ }, { exact hl ⟨hx.1, hax⟩ } }, { rintros ⟨l, u, ha, h⟩, apply mem_sets_of_superset (mem_nhds_sets is_open_Ioo ha) h } end /-- A set is a neighborhood of `a` if and only if it contains an interval `(l, u)` containing `a`. -/ lemma mem_nhds_iff_exists_Ioo_subset [no_top_order Ξ±] [no_bot_order Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ 𝓝 a ↔ βˆƒl u, a ∈ Ioo l u ∧ Ioo l u βŠ† s := let ⟨l', hl'⟩ := no_bot a in let ⟨u', hu'⟩ := no_top a in mem_nhds_iff_exists_Ioo_subset' hl' hu' /-! ### Neighborhoods to the left and to the right Limits to the left and to the right of real functions are defined in terms of neighborhoods to the left and to the right, either open or closed, i.e., members of `nhds_within a (Ioi a)` and `nhds_wihin a (Ici a)` on the right, and similarly on the left. Such neighborhoods can be characterized as the sets containing suitable intervals to the right or to the left of `a`. We give now these characterizations. -/ -- NB: If you extend the list, append to the end please to avoid breaking the API /-- The following statements are equivalent: 0. `s` is a neighborhood of `a` within `(a, +∞)` 1. `s` is a neighborhood of `a` within `(a, b]` 2. `s` is a neighborhood of `a` within `(a, b)` 3. `s` includes `(a, u)` for some `u ∈ (a, b]` 4. `s` includes `(a, u)` for some `u > a` -/ lemma tfae_mem_nhds_within_Ioi {a b : Ξ±} (hab : a < b) (s : set Ξ±) : tfae [s ∈ nhds_within a (Ioi a), -- 0 : `s` is a neighborhood of `a` within `(a, +∞)` s ∈ nhds_within a (Ioc a b), -- 1 : `s` is a neighborhood of `a` within `(a, b]` s ∈ nhds_within a (Ioo a b), -- 2 : `s` is a neighborhood of `a` within `(a, b)` βˆƒ u ∈ Ioc a b, Ioo a u βŠ† s, -- 3 : `s` includes `(a, u)` for some `u ∈ (a, b]` βˆƒ u ∈ Ioi a, Ioo a u βŠ† s] := -- 4 : `s` includes `(a, u)` for some `u > a` begin tfae_have : 1 β†’ 2, from Ξ» h, nhds_within_mono _ Ioc_subset_Ioi_self h, tfae_have : 2 β†’ 3, from Ξ» h, nhds_within_mono _ Ioo_subset_Ioc_self h, tfae_have : 4 β†’ 5, from Ξ» ⟨u, umem, hu⟩, ⟨u, umem.1, hu⟩, tfae_have : 5 β†’ 1, { rintros ⟨u, hau, hu⟩, exact mem_nhds_within.2 ⟨Iio u, is_open_Iio, hau, by rwa [inter_comm, Ioi_inter_Iio]⟩ }, tfae_have : 3 β†’ 4, { assume h, rcases mem_nhds_within_iff_exists_mem_nhds_inter.1 h with ⟨v, va, hv⟩, rcases exists_Ico_subset_of_mem_nhds' va hab with ⟨u, au, hu⟩, refine ⟨u, au, Ξ»x hx, _⟩, refine hv ⟨hu ⟨le_of_lt hx.1, hx.2⟩, _⟩, exact Ioo_subset_Ioo_right au.2 hx }, tfae_finish end @[simp] lemma nhds_within_Ioc_eq_nhds_within_Ioi {a b : Ξ±} (h : a < b) : nhds_within a (Ioc a b) = nhds_within a (Ioi a) := filter.ext $ Ξ» s, (tfae_mem_nhds_within_Ioi h s).out 1 0 @[simp] lemma nhds_within_Ioo_eq_nhds_within_Ioi {a b : Ξ±} (hu : a < b) : nhds_within a (Ioo a b) = nhds_within a (Ioi a) := filter.ext $ Ξ» s, (tfae_mem_nhds_within_Ioi hu s).out 2 0 lemma mem_nhds_within_Ioi_iff_exists_mem_Ioc_Ioo_subset {a u' : Ξ±} {s : set Ξ±} (hu' : a < u') : s ∈ nhds_within a (Ioi a) ↔ βˆƒu ∈ Ioc a u', Ioo a u βŠ† s := (tfae_mem_nhds_within_Ioi hu' s).out 0 3 /-- A set is a neighborhood of `a` within `(a, +∞)` if and only if it contains an interval `(a, u)` with `a < u < u'`, provided `a` is not a top element. -/ lemma mem_nhds_within_Ioi_iff_exists_Ioo_subset' {a u' : Ξ±} {s : set Ξ±} (hu' : a < u') : s ∈ nhds_within a (Ioi a) ↔ βˆƒu ∈ Ioi a, Ioo a u βŠ† s := (tfae_mem_nhds_within_Ioi hu' s).out 0 4 /-- A set is a neighborhood of `a` within `(a, +∞)` if and only if it contains an interval `(a, u)` with `a < u`. -/ lemma mem_nhds_within_Ioi_iff_exists_Ioo_subset [no_top_order Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ nhds_within a (Ioi a) ↔ βˆƒu ∈ Ioi a, Ioo a u βŠ† s := let ⟨u', hu'⟩ := no_top a in mem_nhds_within_Ioi_iff_exists_Ioo_subset' hu' /-- A set is a neighborhood of `a` within `(a, +∞)` if and only if it contains an interval `(a, u]` with `a < u`. -/ lemma mem_nhds_within_Ioi_iff_exists_Ioc_subset [no_top_order Ξ±] [densely_ordered Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ nhds_within a (Ioi a) ↔ βˆƒu ∈ Ioi a, Ioc a u βŠ† s := begin rw mem_nhds_within_Ioi_iff_exists_Ioo_subset, split, { rintros ⟨u, au, as⟩, rcases dense au with ⟨v, hv⟩, exact ⟨v, hv.1, Ξ»x hx, as ⟨hx.1, lt_of_le_of_lt hx.2 hv.2⟩⟩ }, { rintros ⟨u, au, as⟩, exact ⟨u, au, subset.trans Ioo_subset_Ioc_self as⟩ } end lemma Ioo_mem_nhds_within_Ioi {a b c : Ξ±} (H : b ∈ Ico a c) : Ioo a c ∈ nhds_within b (Ioi b) := (mem_nhds_within_Ioi_iff_exists_Ioo_subset' H.2).2 ⟨c, H.2, Ioo_subset_Ioo_left H.1⟩ lemma Ioc_mem_nhds_within_Ioi {a b c : Ξ±} (H : b ∈ Ico a c) : Ioc a c ∈ nhds_within b (Ioi b) := mem_sets_of_superset (Ioo_mem_nhds_within_Ioi H) Ioo_subset_Ioc_self lemma Ico_mem_nhds_within_Ioi {a b c : Ξ±} (H : b ∈ Ico a c) : Ico a c ∈ nhds_within b (Ioi b) := mem_sets_of_superset (Ioo_mem_nhds_within_Ioi H) Ioo_subset_Ico_self lemma Icc_mem_nhds_within_Ioi {a b c : Ξ±} (H : b ∈ Ico a c) : Icc a c ∈ nhds_within b (Ioi b) := mem_sets_of_superset (Ioo_mem_nhds_within_Ioi H) Ioo_subset_Icc_self /-- The following statements are equivalent: 0. `s` is a neighborhood of `b` within `(-∞, b)` 1. `s` is a neighborhood of `b` within `[a, b)` 2. `s` is a neighborhood of `b` within `(a, b)` 3. `s` includes `(l, b)` for some `l ∈ [a, b)` 4. `s` includes `(l, b)` for some `l < b` -/ lemma tfae_mem_nhds_within_Iio {a b : Ξ±} (h : a < b) (s : set Ξ±) : tfae [s ∈ nhds_within b (Iio b), -- 0 : `s` is a neighborhood of `b` within `(-∞, b)` s ∈ nhds_within b (Ico a b), -- 1 : `s` is a neighborhood of `b` within `[a, b)` s ∈ nhds_within b (Ioo a b), -- 2 : `s` is a neighborhood of `b` within `(a, b)` βˆƒ l ∈ Ico a b, Ioo l b βŠ† s, -- 3 : `s` includes `(l, b)` for some `l ∈ [a, b)` βˆƒ l ∈ Iio b, Ioo l b βŠ† s] := -- 4 : `s` includes `(l, b)` for some `l < b` begin have := @tfae_mem_nhds_within_Ioi (order_dual Ξ±) _ _ _ _ _ h s, -- If we call `convert` here, it generates wrong equations, so we need to simplify first simp only [exists_prop] at this ⊒, rw [dual_Ioi, dual_Ioc, dual_Ioo] at this, convert this; ext l; rw [dual_Ioo] end @[simp] lemma nhds_within_Ico_eq_nhds_within_Iio {a b : Ξ±} (h : a < b) : nhds_within b (Ico a b) = nhds_within b (Iio b) := filter.ext $ Ξ» s, (tfae_mem_nhds_within_Iio h s).out 1 0 @[simp] lemma nhds_within_Ioo_eq_nhds_within_Iio {a b : Ξ±} (h : a < b) : nhds_within b (Ioo a b) = nhds_within b (Iio b) := filter.ext $ Ξ» s, (tfae_mem_nhds_within_Iio h s).out 2 0 lemma mem_nhds_within_Iio_iff_exists_mem_Ico_Ioo_subset {a l' : Ξ±} {s : set Ξ±} (hl' : l' < a) : s ∈ nhds_within a (Iio a) ↔ βˆƒl ∈ Ico l' a, Ioo l a βŠ† s := (tfae_mem_nhds_within_Iio hl' s).out 0 3 /-- A set is a neighborhood of `a` within `(-∞, a)` if and only if it contains an interval `(l, a)` with `l < a`, provided `a` is not a bottom element. -/ lemma mem_nhds_within_Iio_iff_exists_Ioo_subset' {a l' : Ξ±} {s : set Ξ±} (hl' : l' < a) : s ∈ nhds_within a (Iio a) ↔ βˆƒl ∈ Iio a, Ioo l a βŠ† s := (tfae_mem_nhds_within_Iio hl' s).out 0 4 /-- A set is a neighborhood of `a` within `(-∞, a)` if and only if it contains an interval `(l, a)` with `l < a`. -/ lemma mem_nhds_within_Iio_iff_exists_Ioo_subset [no_bot_order Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ nhds_within a (Iio a) ↔ βˆƒl ∈ Iio a, Ioo l a βŠ† s := let ⟨l', hl'⟩ := no_bot a in mem_nhds_within_Iio_iff_exists_Ioo_subset' hl' /-- A set is a neighborhood of `a` within `(-∞, a)` if and only if it contains an interval `[l, a)` with `l < a`. -/ lemma mem_nhds_within_Iio_iff_exists_Ico_subset [no_bot_order Ξ±] [densely_ordered Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ nhds_within a (Iio a) ↔ βˆƒl ∈ Iio a, Ico l a βŠ† s := begin convert @mem_nhds_within_Ioi_iff_exists_Ioc_subset (order_dual Ξ±) _ _ _ _ _ _ _, simp only [dual_Ioc], refl end lemma Ioo_mem_nhds_within_Iio {a b c : Ξ±} (h : b ∈ Ioc a c) : Ioo a c ∈ nhds_within b (Iio b) := (mem_nhds_within_Iio_iff_exists_Ioo_subset' h.1).2 ⟨a, h.1, Ioo_subset_Ioo_right h.2⟩ lemma Ioc_mem_nhds_within_Iio {a b c : Ξ±} (h : b ∈ Ioc a c) : Ioc a c ∈ nhds_within b (Iio b) := mem_sets_of_superset (Ioo_mem_nhds_within_Iio h) Ioo_subset_Ioc_self lemma Ico_mem_nhds_within_Iio {a b c : Ξ±} (h : b ∈ Ioc a c) : Ico a c ∈ nhds_within b (Iio b) := mem_sets_of_superset (Ioo_mem_nhds_within_Iio h) Ioo_subset_Ico_self lemma Icc_mem_nhds_within_Iio {a b c : Ξ±} (h : b ∈ Ioc a c) : Icc a c ∈ nhds_within b (Iio b) := mem_sets_of_superset (Ioo_mem_nhds_within_Iio h) Ioo_subset_Icc_self /-- A set is a neighborhood of `a` within `[a, +∞)` if and only if it contains an interval `[a, u)` with `a < u`, provided `a` is not a top element. -/ lemma mem_nhds_within_Ici_iff_exists_Ico_subset' {a u' : Ξ±} {s : set Ξ±} (hu' : a < u') : s ∈ nhds_within a (Ici a) ↔ βˆƒu, a < u ∧ Ico a u βŠ† s := begin split, { assume h, rcases mem_nhds_within_iff_exists_mem_nhds_inter.1 h with ⟨v, va, hv⟩, rcases exists_Ico_subset_of_mem_nhds va ⟨u', hu'⟩ with ⟨u, au, hu⟩, refine ⟨u, au, Ξ»x hx, _⟩, refine hv ⟨_, hx.1⟩, exact hu hx }, { rintros ⟨u, au, hu⟩, rw mem_nhds_within_iff_exists_mem_nhds_inter, refine ⟨Iio u, mem_nhds_sets is_open_Iio au, _⟩, rwa [inter_comm, Ici_inter_Iio] } end /-- A set is a neighborhood of `a` within `[a, +∞)` if and only if it contains an interval `[a, u)` with `a < u`. -/ lemma mem_nhds_within_Ici_iff_exists_Ico_subset [no_top_order Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ nhds_within a (Ici a) ↔ βˆƒu, a < u ∧ Ico a u βŠ† s := let ⟨u', hu'⟩ := no_top a in mem_nhds_within_Ici_iff_exists_Ico_subset' hu' /-- A set is a neighborhood of `a` within `[a, +∞)` if and only if it contains an interval `[a, u]` with `a < u`. -/ lemma mem_nhds_within_Ici_iff_exists_Icc_subset [no_top_order Ξ±] [densely_ordered Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ nhds_within a (Ici a) ↔ βˆƒu, a < u ∧ Icc a u βŠ† s := begin rw mem_nhds_within_Ici_iff_exists_Ico_subset, split, { rintros ⟨u, au, as⟩, rcases dense au with ⟨v, hv⟩, exact ⟨v, hv.1, Ξ»x hx, as ⟨hx.1, lt_of_le_of_lt hx.2 hv.2⟩⟩ }, { rintros ⟨u, au, as⟩, exact ⟨u, au, subset.trans Ico_subset_Icc_self as⟩ } end /-- A set is a neighborhood of `a` within `(-∞, a]` if and only if it contains an interval `(l, a]` with `l < a`, provided `a` is not a bottom element. -/ lemma mem_nhds_within_Iic_iff_exists_Ioc_subset' {a l' : Ξ±} {s : set Ξ±} (hl' : l' < a) : s ∈ nhds_within a (Iic a) ↔ βˆƒl, l < a ∧ Ioc l a βŠ† s := begin split, { assume h, rcases mem_nhds_within_iff_exists_mem_nhds_inter.1 h with ⟨v, va, hv⟩, rcases exists_Ioc_subset_of_mem_nhds va ⟨l', hl'⟩ with ⟨l, la, hl⟩, refine ⟨l, la, Ξ»x hx, _⟩, refine hv ⟨_, hx.2⟩, exact hl hx }, { rintros ⟨l, la, ha⟩, rw mem_nhds_within_iff_exists_mem_nhds_inter, refine ⟨Ioi l, mem_nhds_sets is_open_Ioi la, _⟩, rwa [Ioi_inter_Iic] } end /-- A set is a neighborhood of `a` within `(-∞, a]` if and only if it contains an interval `(l, a]` with `l < a`. -/ lemma mem_nhds_within_Iic_iff_exists_Ioc_subset [no_bot_order Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ nhds_within a (Iic a) ↔ βˆƒl, l < a ∧ Ioc l a βŠ† s := let ⟨l', hl'⟩ := no_bot a in mem_nhds_within_Iic_iff_exists_Ioc_subset' hl' /-- A set is a neighborhood of `a` within `(-∞, a]` if and only if it contains an interval `[l, a]` with `l < a`. -/ lemma mem_nhds_within_Iic_iff_exists_Icc_subset [no_bot_order Ξ±] [densely_ordered Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ nhds_within a (Iic a) ↔ βˆƒl, l < a ∧ Icc l a βŠ† s := begin rw mem_nhds_within_Iic_iff_exists_Ioc_subset, split, { rintros ⟨l, la, as⟩, rcases dense la with ⟨v, hv⟩, refine ⟨v, hv.2, Ξ»x hx, as ⟨lt_of_lt_of_le hv.1 hx.1, hx.2⟩⟩, }, { rintros ⟨l, la, as⟩, exact ⟨l, la, subset.trans Ioc_subset_Icc_self as⟩ } end end linear_order lemma preimage_neg [add_group Ξ±] : preimage (has_neg.neg : Ξ± β†’ Ξ±) = image (has_neg.neg : Ξ± β†’ Ξ±) := (image_eq_preimage_of_inverse neg_neg neg_neg).symm lemma filter.map_neg [add_group Ξ±] : map (has_neg.neg : Ξ± β†’ Ξ±) = comap (has_neg.neg : Ξ± β†’ Ξ±) := funext $ assume f, map_eq_comap_of_inverse (funext neg_neg) (funext neg_neg) section topological_add_group variables [topological_space Ξ±] [ordered_add_comm_group Ξ±] [topological_add_group Ξ±] lemma neg_preimage_closure {s : set Ξ±} : (Ξ»r:Ξ±, -r) ⁻¹' closure s = closure ((Ξ»r:Ξ±, -r) '' s) := have (Ξ»r:Ξ±, -r) ∘ (Ξ»r:Ξ±, -r) = id, from funext neg_neg, by rw [preimage_neg]; exact (subset.antisymm (image_closure_subset_closure_image continuous_neg) $ calc closure ((Ξ» (r : Ξ±), -r) '' s) = (Ξ»r, -r) '' ((Ξ»r, -r) '' closure ((Ξ» (r : Ξ±), -r) '' s)) : by rw [←image_comp, this, image_id] ... βŠ† (Ξ»r, -r) '' closure ((Ξ»r, -r) '' ((Ξ» (r : Ξ±), -r) '' s)) : monotone_image $ image_closure_subset_closure_image continuous_neg ... = _ : by rw [←image_comp, this, image_id]) end topological_add_group section order_topology variables [topological_space Ξ±] [topological_space Ξ²] [linear_order Ξ±] [linear_order Ξ²] [order_topology Ξ±] [order_topology Ξ²] lemma nhds_principal_ne_bot_of_is_lub {a : Ξ±} {s : set Ξ±} (ha : is_lub s a) (hs : s.nonempty) : 𝓝 a βŠ“ principal s β‰  βŠ₯ := let ⟨a', ha'⟩ := hs in forall_sets_nonempty_iff_ne_bot.mp $ assume t ht, let ⟨t₁, ht₁, tβ‚‚, htβ‚‚, ht⟩ := mem_inf_sets.mp ht in by_cases (assume h : a = a', have a ∈ t₁, from mem_of_nhds ht₁, have a ∈ tβ‚‚, from htβ‚‚ $ by rwa [h], ⟨a, ht βŸ¨β€Ήa ∈ t₁›, β€Ήa ∈ tβ‚‚β€ΊβŸ©βŸ©) (assume : a β‰  a', have a' < a, from lt_of_le_of_ne (ha.left β€Ήa' ∈ sβ€Ί) this.symm, let ⟨l, hl, hltβ‚βŸ© := exists_Ioc_subset_of_mem_nhds ht₁ ⟨a', this⟩ in have βˆƒa'∈s, l < a', from classical.by_contradiction $ assume : Β¬ βˆƒa'∈s, l < a', have βˆ€a'∈s, a' ≀ l, from assume a ha, not_lt.1 $ assume ha', this ⟨a, ha, ha'⟩, have Β¬ l < a, from not_lt.2 $ ha.right this, this β€Ήl < aβ€Ί, let ⟨a', ha', ha'l⟩ := this in have a' ∈ t₁, from hlt₁ βŸ¨β€Ήl < a'β€Ί, ha.left ha'⟩, ⟨a', ht βŸ¨β€Ήa' ∈ t₁›, htβ‚‚ β€Ήa' ∈ sβ€ΊβŸ©βŸ©) lemma nhds_principal_ne_bot_of_is_glb : βˆ€ {a : Ξ±} {s : set Ξ±}, is_glb s a β†’ s.nonempty β†’ 𝓝 a βŠ“ principal s β‰  βŠ₯ := @nhds_principal_ne_bot_of_is_lub (order_dual Ξ±) _ _ _ lemma is_lub_of_mem_nhds {s : set Ξ±} {a : Ξ±} {f : filter Ξ±} (hsa : a ∈ upper_bounds s) (hsf : s ∈ f) (hfa : f βŠ“ 𝓝 a β‰  βŠ₯) : is_lub s a := ⟨hsa, assume b hb, not_lt.1 $ assume hba, have s ∩ {a | b < a} ∈ f βŠ“ 𝓝 a, from inter_mem_inf_sets hsf (mem_nhds_sets (is_open_lt' _) hba), let ⟨x, ⟨hxs, hxb⟩⟩ := nonempty_of_mem_sets hfa this in have b < b, from lt_of_lt_of_le hxb $ hb hxs, lt_irrefl b this⟩ lemma is_glb_of_mem_nhds : βˆ€ {s : set Ξ±} {a : Ξ±} {f : filter Ξ±}, a ∈ lower_bounds s β†’ s ∈ f β†’ f βŠ“ 𝓝 a β‰  βŠ₯ β†’ is_glb s a := @is_lub_of_mem_nhds (order_dual Ξ±) _ _ _ lemma is_lub_of_is_lub_of_tendsto {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} {b : Ξ²} (hf : βˆ€x∈s, βˆ€y∈s, x ≀ y β†’ f x ≀ f y) (ha : is_lub s a) (hs : s.nonempty) (hb : tendsto f (𝓝 a βŠ“ principal s) (𝓝 b)) : is_lub (f '' s) b := have hnbot : (𝓝 a βŠ“ principal s) β‰  βŠ₯, from nhds_principal_ne_bot_of_is_lub ha hs, have βˆ€a'∈s, Β¬ b < f a', from assume a' ha' h, have βˆ€αΆ  x in 𝓝 b, x < f a', from mem_nhds_sets (is_open_gt' _) h, let ⟨t₁, ht₁, tβ‚‚, htβ‚‚, hs⟩ := mem_inf_sets.mp (hb this) in by_cases (assume h : a = a', have a ∈ t₁ ∩ tβ‚‚, from ⟨mem_of_nhds ht₁, htβ‚‚ $ by rwa [h]⟩, have f a < f a', from hs this, lt_irrefl (f a') $ by rwa [h] at this) (assume h : a β‰  a', have a' < a, from lt_of_le_of_ne (ha.left ha') h.symm, have {x | a' < x} ∈ 𝓝 a, from mem_nhds_sets (is_open_lt' _) this, have {x | a' < x} ∩ t₁ ∈ 𝓝 a, from inter_mem_sets this ht₁, have ({x | a' < x} ∩ t₁) ∩ s ∈ 𝓝 a βŠ“ principal s, from inter_mem_inf_sets this (subset.refl s), let ⟨x, ⟨hx₁, hxβ‚‚βŸ©, hxβ‚ƒβŸ© := nonempty_of_mem_sets hnbot this in have hxa' : f x < f a', from hs ⟨hxβ‚‚, htβ‚‚ hxβ‚ƒβŸ©, have ha'x : f a' ≀ f x, from hf _ ha' _ hx₃ $ le_of_lt hx₁, lt_irrefl _ (lt_of_le_of_lt ha'x hxa')), and.intro (assume b' ⟨a', ha', h_eq⟩, h_eq β–Έ not_lt.1 $ this _ ha') (assume b' hb', le_of_tendsto hnbot hb $ mem_inf_sets_of_right $ assume x hx, hb' $ mem_image_of_mem _ hx) lemma is_glb_of_is_glb_of_tendsto {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} {b : Ξ²} (hf : βˆ€x∈s, βˆ€y∈s, x ≀ y β†’ f x ≀ f y) : is_glb s a β†’ s.nonempty β†’ tendsto f (𝓝 a βŠ“ principal s) (𝓝 b) β†’ is_glb (f '' s) b := @is_lub_of_is_lub_of_tendsto (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ f s a b (Ξ» x hx y hy, hf y hy x hx) lemma is_glb_of_is_lub_of_tendsto : βˆ€ {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} {b : Ξ²}, (βˆ€x∈s, βˆ€y∈s, x ≀ y β†’ f y ≀ f x) β†’ is_lub s a β†’ s.nonempty β†’ tendsto f (𝓝 a βŠ“ principal s) (𝓝 b) β†’ is_glb (f '' s) b := @is_lub_of_is_lub_of_tendsto Ξ± (order_dual Ξ²) _ _ _ _ _ _ lemma is_lub_of_is_glb_of_tendsto : βˆ€ {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} {b : Ξ²}, (βˆ€x∈s, βˆ€y∈s, x ≀ y β†’ f y ≀ f x) β†’ is_glb s a β†’ s.nonempty β†’ tendsto f (𝓝 a βŠ“ principal s) (𝓝 b) β†’ is_lub (f '' s) b := @is_glb_of_is_glb_of_tendsto Ξ± (order_dual Ξ²) _ _ _ _ _ _ lemma mem_closure_of_is_lub {a : Ξ±} {s : set Ξ±} (ha : is_lub s a) (hs : s.nonempty) : a ∈ closure s := by rw closure_eq_nhds; exact nhds_principal_ne_bot_of_is_lub ha hs lemma mem_of_is_lub_of_is_closed {a : Ξ±} {s : set Ξ±} (ha : is_lub s a) (hs : s.nonempty) (sc : is_closed s) : a ∈ s := by rw ←closure_eq_of_is_closed sc; exact mem_closure_of_is_lub ha hs lemma mem_closure_of_is_glb {a : Ξ±} {s : set Ξ±} (ha : is_glb s a) (hs : s.nonempty) : a ∈ closure s := by rw closure_eq_nhds; exact nhds_principal_ne_bot_of_is_glb ha hs lemma mem_of_is_glb_of_is_closed {a : Ξ±} {s : set Ξ±} (ha : is_glb s a) (hs : s.nonempty) (sc : is_closed s) : a ∈ s := by rw ←closure_eq_of_is_closed sc; exact mem_closure_of_is_glb ha hs /-- A compact set is bounded below -/ lemma bdd_below_of_compact {Ξ± : Type u} [topological_space Ξ±] [linear_order Ξ±] [order_closed_topology Ξ±] [nonempty Ξ±] {s : set Ξ±} (hs : compact s) : bdd_below s := begin by_contra H, letI := classical.DLO Ξ±, rcases hs.elim_finite_subcover_image (Ξ» x (_ : x ∈ s), @is_open_Ioi _ _ _ _ x) _ with ⟨t, st, ft, ht⟩, { refine H ((bdd_below_finite ft).imp $ Ξ» C hC y hy, _), rcases mem_bUnion_iff.1 (ht hy) with ⟨x, hx, xy⟩, exact le_trans (hC hx) (le_of_lt xy) }, { refine Ξ» x hx, mem_bUnion_iff.2 (not_imp_comm.1 _ H), exact Ξ» h, ⟨x, Ξ» y hy, le_of_not_lt (h.imp $ Ξ» ys, ⟨_, hy, ys⟩)⟩ } end /-- A compact set is bounded above -/ lemma bdd_above_of_compact {Ξ± : Type u} [topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±] : Ξ  [nonempty Ξ±] {s : set Ξ±}, compact s β†’ bdd_above s := @bdd_below_of_compact (order_dual Ξ±) _ _ _ end order_topology section linear_order variables [topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±] [densely_ordered Ξ±] /-- The closure of the interval `(a, +∞)` is the closed interval `[a, +∞)`, unless `a` is a top element. -/ lemma closure_Ioi' {a b : Ξ±} (hab : a < b) : closure (Ioi a) = Ici a := begin apply subset.antisymm, { exact closure_minimal Ioi_subset_Ici_self is_closed_Ici }, { assume x hx, by_cases h : x = a, { rw h, exact mem_closure_of_is_glb is_glb_Ioi ⟨_, hab⟩ }, { exact subset_closure (lt_of_le_of_ne hx (ne.symm h)) } } end /-- The closure of the interval `(a, +∞)` is the closed interval `[a, +∞)`. -/ lemma closure_Ioi (a : Ξ±) [no_top_order Ξ±] : closure (Ioi a) = Ici a := let ⟨b, hb⟩ := no_top a in closure_Ioi' hb /-- The closure of the interval `(-∞, a)` is the closed interval `(-∞, a]`, unless `a` is a bottom element. -/ lemma closure_Iio' {a b : Ξ±} (hab : b < a) : closure (Iio a) = Iic a := begin apply subset.antisymm, { exact closure_minimal Iio_subset_Iic_self is_closed_Iic }, { assume x hx, by_cases h : x = a, { rw h, exact mem_closure_of_is_lub is_lub_Iio ⟨_, hab⟩ }, { apply subset_closure, by simpa [h] using lt_or_eq_of_le hx } } end /-- The closure of the interval `(-∞, a)` is the interval `(-∞, a]`. -/ lemma closure_Iio (a : Ξ±) [no_bot_order Ξ±] : closure (Iio a) = Iic a := let ⟨b, hb⟩ := no_bot a in closure_Iio' hb /-- The closure of the open interval `(a, b)` is the closed interval `[a, b]`. -/ lemma closure_Ioo {a b : Ξ±} (hab : a < b) : closure (Ioo a b) = Icc a b := begin apply subset.antisymm, { exact closure_minimal Ioo_subset_Icc_self is_closed_Icc }, { have hab' : (Ioo a b).nonempty, from nonempty_Ioo.2 hab, assume x hx, by_cases h : x = a, { rw h, exact mem_closure_of_is_glb (is_glb_Ioo hab) hab' }, by_cases h' : x = b, { rw h', refine mem_closure_of_is_lub (is_lub_Ioo hab) hab' }, exact subset_closure ⟨lt_of_le_of_ne hx.1 (ne.symm h), by simpa [h'] using lt_or_eq_of_le hx.2⟩ } end /-- The closure of the interval `(a, b]` is the closed interval `[a, b]`. -/ lemma closure_Ioc {a b : Ξ±} (hab : a < b) : closure (Ioc a b) = Icc a b := begin apply subset.antisymm, { exact closure_minimal Ioc_subset_Icc_self is_closed_Icc }, { apply subset.trans _ (closure_mono Ioo_subset_Ioc_self), rw closure_Ioo hab } end /-- The closure of the interval `[a, b)` is the closed interval `[a, b]`. -/ lemma closure_Ico {a b : Ξ±} (hab : a < b) : closure (Ico a b) = Icc a b := begin apply subset.antisymm, { exact closure_minimal Ico_subset_Icc_self is_closed_Icc }, { apply subset.trans _ (closure_mono Ioo_subset_Ico_self), rw closure_Ioo hab } end @[simp] lemma interior_Ici [no_bot_order Ξ±] {a : Ξ±} : interior (Ici a) = Ioi a := by rw [← compl_Iio, interior_compl, closure_Iio, compl_Iic] @[simp] lemma interior_Iic [no_top_order Ξ±] {a : Ξ±} : interior (Iic a) = Iio a := by rw [← compl_Ioi, interior_compl, closure_Ioi, compl_Ici] @[simp] lemma interior_Icc [no_bot_order Ξ±] [no_top_order Ξ±] {a b : Ξ±}: interior (Icc a b) = Ioo a b := by rw [← Ici_inter_Iic, interior_inter, interior_Ici, interior_Iic, Ioi_inter_Iio] @[simp] lemma interior_Ico [no_bot_order Ξ±] {a b : Ξ±} : interior (Ico a b) = Ioo a b := by rw [← Ici_inter_Iio, interior_inter, interior_Ici, interior_Iio, Ioi_inter_Iio] @[simp] lemma interior_Ioc [no_top_order Ξ±] {a b : Ξ±} : interior (Ioc a b) = Ioo a b := by rw [← Ioi_inter_Iic, interior_inter, interior_Ioi, interior_Iic, Ioi_inter_Iio] lemma nhds_within_Ioi_ne_bot' {a b c : Ξ±} (H₁ : a < c) (Hβ‚‚ : a ≀ b) : nhds_within b (Ioi a) β‰  βŠ₯ := mem_closure_iff_nhds_within_ne_bot.1 $ by { rw [closure_Ioi' H₁], exact Hβ‚‚ } lemma nhds_within_Ioi_ne_bot [no_top_order Ξ±] {a b : Ξ±} (H : a ≀ b) : nhds_within b (Ioi a) β‰  βŠ₯ := let ⟨c, hc⟩ := no_top a in nhds_within_Ioi_ne_bot' hc H lemma nhds_within_Ioi_self_ne_bot' {a b : Ξ±} (H : a < b) : nhds_within a (Ioi a) β‰  βŠ₯ := nhds_within_Ioi_ne_bot' H (le_refl a) lemma nhds_within_Ioi_self_ne_bot [no_top_order Ξ±] (a : Ξ±) : nhds_within a (Ioi a) β‰  βŠ₯ := nhds_within_Ioi_ne_bot (le_refl a) lemma nhds_within_Iio_ne_bot' {a b c : Ξ±} (H₁ : a < c) (Hβ‚‚ : b ≀ c) : nhds_within b (Iio c) β‰  βŠ₯ := mem_closure_iff_nhds_within_ne_bot.1 $ by { rw [closure_Iio' H₁], exact Hβ‚‚ } lemma nhds_within_Iio_ne_bot [no_bot_order Ξ±] {a b : Ξ±} (H : a ≀ b) : nhds_within a (Iio b) β‰  βŠ₯ := let ⟨c, hc⟩ := no_bot b in nhds_within_Iio_ne_bot' hc H lemma nhds_within_Iio_self_ne_bot' {a b : Ξ±} (H : a < b) : nhds_within b (Iio b) β‰  βŠ₯ := nhds_within_Iio_ne_bot' H (le_refl b) lemma nhds_within_Iio_self_ne_bot [no_bot_order Ξ±] (a : Ξ±) : nhds_within a (Iio a) β‰  βŠ₯ := nhds_within_Iio_ne_bot (le_refl a) end linear_order section complete_linear_order variables [complete_linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±] [complete_linear_order Ξ²] [topological_space Ξ²] [order_topology Ξ²] [nonempty Ξ³] lemma Sup_mem_closure {Ξ± : Type u} [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) : Sup s ∈ closure s := mem_closure_of_is_lub (is_lub_Sup _) hs lemma Inf_mem_closure {Ξ± : Type u} [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) : Inf s ∈ closure s := mem_closure_of_is_glb (is_glb_Inf _) hs lemma Sup_mem_of_is_closed {Ξ± : Type u} [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) (hc : is_closed s) : Sup s ∈ s := mem_of_is_lub_of_is_closed (is_lub_Sup _) hs hc lemma Inf_mem_of_is_closed {Ξ± : Type u} [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) (hc : is_closed s) : Inf s ∈ s := mem_of_is_glb_of_is_closed (is_glb_Inf _) hs hc /-- A continuous monotone function sends supremum to supremum for nonempty sets. -/ lemma Sup_of_continuous' {f : Ξ± β†’ Ξ²} (Mf : continuous f) (Cf : monotone f) {s : set Ξ±} (hs : s.nonempty) : f (Sup s) = Sup (f '' s) := --This is a particular case of the more general is_lub_of_is_lub_of_tendsto (is_lub_of_is_lub_of_tendsto (Ξ» x hx y hy xy, Cf xy) (is_lub_Sup _) hs $ tendsto_le_left inf_le_left (Mf.tendsto _)).Sup_eq.symm /-- A continuous monotone function sending bot to bot sends supremum to supremum. -/ lemma Sup_of_continuous {f : Ξ± β†’ Ξ²} (Mf : continuous f) (Cf : monotone f) (fbot : f βŠ₯ = βŠ₯) {s : set Ξ±} : f (Sup s) = Sup (f '' s) := begin cases s.eq_empty_or_nonempty with h h, { simp [h, fbot] }, { exact Sup_of_continuous' Mf Cf h } end /-- A continuous monotone function sends indexed supremum to indexed supremum. -/ lemma supr_of_continuous' {ΞΉ : Sort*} [nonempty ΞΉ] {f : Ξ± β†’ Ξ²} {g : ΞΉ β†’ Ξ±} (Mf : continuous f) (Cf : monotone f) : f (supr g) = supr (f ∘ g) := by rw [supr, Sup_of_continuous' Mf Cf (range_nonempty g), ← range_comp, supr] /-- A continuous monotone function sends indexed supremum to indexed supremum. -/ lemma supr_of_continuous {ΞΉ : Sort*} {f : Ξ± β†’ Ξ²} {g : ΞΉ β†’ Ξ±} (Mf : continuous f) (Cf : monotone f) (fbot : f βŠ₯ = βŠ₯) : f (supr g) = supr (f ∘ g) := by rw [supr, Sup_of_continuous Mf Cf fbot, ← range_comp, supr] /-- A continuous monotone function sends infimum to infimum for nonempty sets. -/ lemma Inf_of_continuous' {f : Ξ± β†’ Ξ²} (Mf : continuous f) (Cf : monotone f) {s : set Ξ±} (hs : s.nonempty) : f (Inf s) = Inf (f '' s) := (is_glb_of_is_glb_of_tendsto (Ξ» x hx y hy xy, Cf xy) (is_glb_Inf _) hs $ tendsto_le_left inf_le_left (Mf.tendsto _)).Inf_eq.symm /-- A continuous monotone function sending top to top sends infimum to infimum. -/ lemma Inf_of_continuous {f : Ξ± β†’ Ξ²} (Mf : continuous f) (Cf : monotone f) (ftop : f ⊀ = ⊀) {s : set Ξ±} : f (Inf s) = Inf (f '' s) := begin cases s.eq_empty_or_nonempty with h h, { simpa [h] }, { exact Inf_of_continuous' Mf Cf h } end /-- A continuous monotone function sends indexed infimum to indexed infimum. -/ lemma infi_of_continuous' {ΞΉ : Sort*} [nonempty ΞΉ] {f : Ξ± β†’ Ξ²} {g : ΞΉ β†’ Ξ±} (Mf : continuous f) (Cf : monotone f) : f (infi g) = infi (f ∘ g) := by rw [infi, Inf_of_continuous' Mf Cf (range_nonempty g), ← range_comp, infi] /-- A continuous monotone function sends indexed infimum to indexed infimum. -/ lemma infi_of_continuous {ΞΉ : Sort*} {f : Ξ± β†’ Ξ²} {g : ΞΉ β†’ Ξ±} (Mf : continuous f) (Cf : monotone f) (ftop : f ⊀ = ⊀) : f (infi g) = infi (f ∘ g) := by rw [infi, Inf_of_continuous Mf Cf ftop, ← range_comp, infi] end complete_linear_order section conditionally_complete_linear_order variables [conditionally_complete_linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±] [conditionally_complete_linear_order Ξ²] [topological_space Ξ²] [order_topology Ξ²] [nonempty Ξ³] lemma cSup_mem_closure {Ξ± : Type u} [topological_space Ξ±] [conditionally_complete_linear_order Ξ±] [order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) (B : bdd_above s) : Sup s ∈ closure s := mem_closure_of_is_lub (is_lub_cSup hs B) hs lemma cInf_mem_closure {Ξ± : Type u} [topological_space Ξ±] [conditionally_complete_linear_order Ξ±] [order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) (B : bdd_below s) : Inf s ∈ closure s := mem_closure_of_is_glb (is_glb_cInf hs B) hs lemma cSup_mem_of_is_closed {Ξ± : Type u} [topological_space Ξ±] [conditionally_complete_linear_order Ξ±] [order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) (hc : is_closed s) (B : bdd_above s) : Sup s ∈ s := mem_of_is_lub_of_is_closed (is_lub_cSup hs B) hs hc lemma cInf_mem_of_is_closed {Ξ± : Type u} [topological_space Ξ±] [conditionally_complete_linear_order Ξ±] [order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) (hc : is_closed s) (B : bdd_below s) : Inf s ∈ s := mem_of_is_glb_of_is_closed (is_glb_cInf hs B) hs hc /-- A continuous monotone function sends supremum to supremum in conditionally complete lattices, under a boundedness assumption. -/ lemma cSup_of_cSup_of_monotone_of_continuous {f : Ξ± β†’ Ξ²} (Mf : continuous f) (Cf : monotone f) {s : set Ξ±} (ne : s.nonempty) (H : bdd_above s) : f (Sup s) = Sup (f '' s) := begin refine ((is_lub_cSup (ne.image f) (Cf.map_bdd_above H)).unique _).symm, refine is_lub_of_is_lub_of_tendsto (Ξ»x hx y hy xy, Cf xy) (is_lub_cSup ne H) ne _, exact tendsto_le_left inf_le_left (Mf.tendsto _) end /-- A continuous monotone function sends indexed supremum to indexed supremum in conditionally complete lattices, under a boundedness assumption. -/ lemma csupr_of_csupr_of_monotone_of_continuous {f : Ξ± β†’ Ξ²} {g : Ξ³ β†’ Ξ±} (Mf : continuous f) (Cf : monotone f) (H : bdd_above (range g)) : f (supr g) = supr (f ∘ g) := by rw [supr, cSup_of_cSup_of_monotone_of_continuous Mf Cf (range_nonempty _) H, ← range_comp, supr] /-- A continuous monotone function sends infimum to infimum in conditionally complete lattices, under a boundedness assumption. -/ lemma cInf_of_cInf_of_monotone_of_continuous {f : Ξ± β†’ Ξ²} (Mf : continuous f) (Cf : monotone f) {s : set Ξ±} (ne : s.nonempty) (H : bdd_below s) : f (Inf s) = Inf (f '' s) := begin refine ((is_glb_cInf (ne.image _) (Cf.map_bdd_below H)).unique _).symm, refine is_glb_of_is_glb_of_tendsto (Ξ»x hx y hy xy, Cf xy) (is_glb_cInf ne H) ne _, exact tendsto_le_left inf_le_left (Mf.tendsto _) end /-- A continuous monotone function sends indexed infimum to indexed infimum in conditionally complete lattices, under a boundedness assumption. -/ lemma cinfi_of_cinfi_of_monotone_of_continuous {f : Ξ± β†’ Ξ²} {g : Ξ³ β†’ Ξ±} (Mf : continuous f) (Cf : monotone f) (H : bdd_below (range g)) : f (infi g) = infi (f ∘ g) := by rw [infi, cInf_of_cInf_of_monotone_of_continuous Mf Cf (range_nonempty _) H, ← range_comp, infi] /-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]` on a closed subset, contains `a`, and the set `s ∩ [a, b)` has no maximal point, then `b ∈ s`. -/ lemma is_closed.mem_of_ge_of_forall_exists_gt {a b : Ξ±} {s : set Ξ±} (hs : is_closed (s ∩ Icc a b)) (ha : a ∈ s) (hab : a ≀ b) (hgt : βˆ€ x ∈ s ∩ Ico a b, (s ∩ Ioc x b).nonempty) : b ∈ s := begin let S := s ∩ Icc a b, replace ha : a ∈ S, from ⟨ha, left_mem_Icc.2 hab⟩, have Sbd : bdd_above S, from ⟨b, Ξ» z hz, hz.2.2⟩, let c := Sup (s ∩ Icc a b), have c_mem : c ∈ S, from cSup_mem_of_is_closed ⟨_, ha⟩ hs Sbd, have c_le : c ≀ b, from cSup_le ⟨_, ha⟩ (Ξ» x hx, hx.2.2), cases eq_or_lt_of_le c_le with hc hc, from hc β–Έ c_mem.1, exfalso, rcases hgt c ⟨c_mem.1, c_mem.2.1, hc⟩ with ⟨x, xs, cx, xb⟩, exact not_lt_of_le (le_cSup Sbd ⟨xs, le_trans (le_cSup Sbd ha) (le_of_lt cx), xb⟩) cx end /-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]` on a closed subset, contains `a`, and for any `a ≀ x < y ≀ b`, `x ∈ s`, the set `s ∩ (x, y]` is not empty, then `[a, b] βŠ† s`. -/ lemma is_closed.Icc_subset_of_forall_exists_gt {a b : Ξ±} {s : set Ξ±} (hs : is_closed (s ∩ Icc a b)) (ha : a ∈ s) (hgt : βˆ€ x ∈ s ∩ Ico a b, βˆ€ y ∈ Ioi x, (s ∩ Ioc x y).nonempty) : Icc a b βŠ† s := begin assume y hy, have : is_closed (s ∩ Icc a y), { suffices : s ∩ Icc a y = s ∩ Icc a b ∩ Icc a y, { rw this, exact is_closed_inter hs is_closed_Icc }, rw [inter_assoc], congr, exact (inter_eq_self_of_subset_right $ Icc_subset_Icc_right hy.2).symm }, exact is_closed.mem_of_ge_of_forall_exists_gt this ha hy.1 (Ξ» x hx, hgt x ⟨hx.1, Ico_subset_Ico_right hy.2 hx.2⟩ y hx.2.2) end section densely_ordered variables [densely_ordered Ξ±] {a b : Ξ±} /-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]` on a closed subset, contains `a`, and for any `x ∈ s ∩ [a, b)` the set `s` includes some open neighborhood of `x` within `(x, +∞)`, then `[a, b] βŠ† s`. -/ lemma is_closed.Icc_subset_of_forall_mem_nhds_within {a b : Ξ±} {s : set Ξ±} (hs : is_closed (s ∩ Icc a b)) (ha : a ∈ s) (hgt : βˆ€ x ∈ s ∩ Ico a b, s ∈ nhds_within x (Ioi x)) : Icc a b βŠ† s := begin apply hs.Icc_subset_of_forall_exists_gt ha, rintros x ⟨hxs, hxab⟩ y hyxb, have : s ∩ Ioc x y ∈ nhds_within x (Ioi x), from inter_mem_sets (hgt x ⟨hxs, hxab⟩) (Ioc_mem_nhds_within_Ioi ⟨le_refl _, hyxb⟩), exact nonempty_of_mem_sets (nhds_within_Ioi_self_ne_bot' hxab.2) this end /-- A closed interval is preconnected. -/ lemma is_connected_Icc : is_preconnected (Icc a b) := is_preconnected_closed_iff.2 begin rintros s t hs ht hab ⟨x, hx⟩ ⟨y, hy⟩, wlog hxy : x ≀ y := le_total x y using [x y s t, y x t s], have xyab : Icc x y βŠ† Icc a b := Icc_subset_Icc hx.1.1 hy.1.2, by_contradiction hst, suffices : Icc x y βŠ† s, from hst ⟨y, xyab $ right_mem_Icc.2 hxy, this $ right_mem_Icc.2 hxy, hy.2⟩, apply (is_closed_inter hs is_closed_Icc).Icc_subset_of_forall_mem_nhds_within hx.2, rintros z ⟨zs, hz⟩, have zt : z ∈ -t, from Ξ» zt, hst ⟨z, xyab $ Ico_subset_Icc_self hz, zs, zt⟩, have : -t ∩ Ioc z y ∈ nhds_within z (Ioi z), { rw [← nhds_within_Ioc_eq_nhds_within_Ioi hz.2], exact mem_nhds_within.2 ⟨-t, ht, zt, subset.refl _⟩}, apply mem_sets_of_superset this, have : Ioc z y βŠ† s βˆͺ t, from Ξ» w hw, hab (xyab ⟨le_trans hz.1 (le_of_lt hw.1), hw.2⟩), exact Ξ» w ⟨wt, wzy⟩, (this wzy).elim id (Ξ» h, (wt h).elim) end lemma is_preconnected_iff_forall_Icc_subset {s : set Ξ±} : is_preconnected s ↔ βˆ€ x y ∈ s, x ≀ y β†’ Icc x y βŠ† s := ⟨λ h x y hx hy hxy, h.forall_Icc_subset hx hy, Ξ» h, is_preconnected_of_forall_pair $ Ξ» x y hx hy, ⟨Icc (min x y) (max x y), h (min x y) (max x y) ((min_choice x y).elim (Ξ» h', by rwa h') (Ξ» h', by rwa h')) ((max_choice x y).elim (Ξ» h', by rwa h') (Ξ» h', by rwa h')) min_le_max, ⟨min_le_left x y, le_max_left x y⟩, ⟨min_le_right x y, le_max_right x y⟩, is_connected_Icc⟩⟩ lemma is_preconnected_Ici : is_preconnected (Ici a) := is_preconnected_iff_forall_Icc_subset.2 $ Ξ» x y hx hy hxy, (Icc_subset_Ici_iff hxy).2 hx lemma is_preconnected_Iic : is_preconnected (Iic a) := is_preconnected_iff_forall_Icc_subset.2 $ Ξ» x y hx hy hxy, (Icc_subset_Iic_iff hxy).2 hy lemma is_preconnected_Iio : is_preconnected (Iio a) := is_preconnected_iff_forall_Icc_subset.2 $ Ξ» x y hx hy hxy, (Icc_subset_Iio_iff hxy).2 hy lemma is_preconnected_Ioi : is_preconnected (Ioi a) := is_preconnected_iff_forall_Icc_subset.2 $ Ξ» x y hx hy hxy, (Icc_subset_Ioi_iff hxy).2 hx lemma is_connected_Ioo : is_preconnected (Ioo a b) := is_preconnected_iff_forall_Icc_subset.2 $ Ξ» x y hx hy hxy, (Icc_subset_Ioo_iff hxy).2 ⟨hx.1, hy.2⟩ lemma is_preconnected_Ioc : is_preconnected (Ioc a b) := is_preconnected_iff_forall_Icc_subset.2 $ Ξ» x y hx hy hxy, (Icc_subset_Ioc_iff hxy).2 ⟨hx.1, hy.2⟩ lemma is_preconnected_Ico : is_preconnected (Ico a b) := is_preconnected_iff_forall_Icc_subset.2 $ Ξ» x y hx hy hxy, (Icc_subset_Ico_iff hxy).2 ⟨hx.1, hy.2⟩ @[priority 100] instance ordered_connected_space : preconnected_space Ξ± := ⟨is_preconnected_iff_forall_Icc_subset.2 $ Ξ» x y hx hy hxy, subset_univ _⟩ /--Intermediate Value Theorem for continuous functions on closed intervals, case `f a ≀ t ≀ f b`.-/ lemma intermediate_value_Icc {a b : Ξ±} (hab : a ≀ b) {f : Ξ± β†’ Ξ²} (hf : continuous_on f (Icc a b)) : Icc (f a) (f b) βŠ† f '' (Icc a b) := is_connected_Icc.intermediate_value (left_mem_Icc.2 hab) (right_mem_Icc.2 hab) hf /--Intermediate Value Theorem for continuous functions on closed intervals, case `f a β‰₯ t β‰₯ f b`.-/ lemma intermediate_value_Icc' {a b : Ξ±} (hab : a ≀ b) {f : Ξ± β†’ Ξ²} (hf : continuous_on f (Icc a b)) : Icc (f b) (f a) βŠ† f '' (Icc a b) := is_connected_Icc.intermediate_value (right_mem_Icc.2 hab) (left_mem_Icc.2 hab) hf end densely_ordered /-- The extreme value theorem: a continuous function realizes its minimum on a compact set -/ lemma compact.exists_forall_le {Ξ± : Type u} [topological_space Ξ±] {s : set Ξ±} (hs : compact s) (ne_s : s.nonempty) {f : Ξ± β†’ Ξ²} (hf : continuous_on f s) : βˆƒx∈s, βˆ€y∈s, f x ≀ f y := begin have C : compact (f '' s) := hs.image_of_continuous_on hf, haveI := has_Inf_to_nonempty Ξ², have B : bdd_below (f '' s) := bdd_below_of_compact C, have : Inf (f '' s) ∈ f '' s := cInf_mem_of_is_closed (ne_s.image _) (closed_of_compact _ C) B, rcases (mem_image _ _ _).1 this with ⟨x, xs, hx⟩, exact ⟨x, xs, Ξ» y hy, hx.symm β–Έ cInf_le B ⟨_, hy, rfl⟩⟩ end /-- The extreme value theorem: a continuous function realizes its maximum on a compact set -/ lemma compact.exists_forall_ge {Ξ± : Type u} [topological_space Ξ±]: βˆ€ {s : set Ξ±}, compact s β†’ s.nonempty β†’ βˆ€ {f : Ξ± β†’ Ξ²}, continuous_on f s β†’ βˆƒx∈s, βˆ€y∈s, f y ≀ f x := @compact.exists_forall_le (order_dual Ξ²) _ _ _ _ _ end conditionally_complete_linear_order section liminf_limsup section order_closed_topology variables [semilattice_sup Ξ±] [topological_space Ξ±] [order_topology Ξ±] lemma is_bounded_le_nhds (a : Ξ±) : (𝓝 a).is_bounded (≀) := match forall_le_or_exists_lt_sup a with | or.inl h := ⟨a, eventually_of_forall _ h⟩ | or.inr ⟨b, hb⟩ := ⟨b, ge_mem_nhds hb⟩ end lemma is_bounded_under_le_of_tendsto {f : filter Ξ²} {u : Ξ² β†’ Ξ±} {a : Ξ±} (h : tendsto u f (𝓝 a)) : f.is_bounded_under (≀) u := is_bounded_of_le h (is_bounded_le_nhds a) @[nolint ge_or_gt] -- see Note [nolint_ge] lemma is_cobounded_ge_nhds (a : Ξ±) : (𝓝 a).is_cobounded (β‰₯) := is_cobounded_of_is_bounded nhds_ne_bot (is_bounded_le_nhds a) @[nolint ge_or_gt] -- see Note [nolint_ge] lemma is_cobounded_under_ge_of_tendsto {f : filter Ξ²} {u : Ξ² β†’ Ξ±} {a : Ξ±} (hf : f β‰  βŠ₯) (h : tendsto u f (𝓝 a)) : f.is_cobounded_under (β‰₯) u := is_cobounded_of_is_bounded (map_ne_bot hf) (is_bounded_under_le_of_tendsto h) end order_closed_topology section order_closed_topology variables [semilattice_inf Ξ±] [topological_space Ξ±] [order_topology Ξ±] @[nolint ge_or_gt] -- see Note [nolint_ge] lemma is_bounded_ge_nhds (a : Ξ±) : (𝓝 a).is_bounded (β‰₯) := match forall_le_or_exists_lt_inf a with | or.inl h := ⟨a, eventually_of_forall _ h⟩ | or.inr ⟨b, hb⟩ := ⟨b, le_mem_nhds hb⟩ end @[nolint ge_or_gt] -- see Note [nolint_ge] lemma is_bounded_under_ge_of_tendsto {f : filter Ξ²} {u : Ξ² β†’ Ξ±} {a : Ξ±} (h : tendsto u f (𝓝 a)) : f.is_bounded_under (β‰₯) u := is_bounded_of_le h (is_bounded_ge_nhds a) lemma is_cobounded_le_nhds (a : Ξ±) : (𝓝 a).is_cobounded (≀) := is_cobounded_of_is_bounded nhds_ne_bot (is_bounded_ge_nhds a) lemma is_cobounded_under_le_of_tendsto {f : filter Ξ²} {u : Ξ² β†’ Ξ±} {a : Ξ±} (hf : f β‰  βŠ₯) (h : tendsto u f (𝓝 a)) : f.is_cobounded_under (≀) u := is_cobounded_of_is_bounded (map_ne_bot hf) (is_bounded_under_ge_of_tendsto h) end order_closed_topology section conditionally_complete_linear_order variables [conditionally_complete_linear_order Ξ±] theorem lt_mem_sets_of_Limsup_lt {f : filter Ξ±} {b} (h : f.is_bounded (≀)) (l : f.Limsup < b) : βˆ€αΆ  a in f, a < b := let ⟨c, (h : βˆ€αΆ  a in f, a ≀ c), hcb⟩ := exists_lt_of_cInf_lt h l in mem_sets_of_superset h $ assume a hac, lt_of_le_of_lt hac hcb @[nolint ge_or_gt] -- see Note [nolint_ge] theorem gt_mem_sets_of_Liminf_gt : βˆ€ {f : filter Ξ±} {b}, f.is_bounded (β‰₯) β†’ f.Liminf > b β†’ βˆ€αΆ  a in f, a > b := @lt_mem_sets_of_Limsup_lt (order_dual Ξ±) _ variables [topological_space Ξ±] [order_topology Ξ±] /-- If the liminf and the limsup of a filter coincide, then this filter converges to their common value, at least if the filter is eventually bounded above and below. -/ @[nolint ge_or_gt] -- see Note [nolint_ge] theorem le_nhds_of_Limsup_eq_Liminf {f : filter Ξ±} {a : Ξ±} (hl : f.is_bounded (≀)) (hg : f.is_bounded (β‰₯)) (hs : f.Limsup = a) (hi : f.Liminf = a) : f ≀ 𝓝 a := tendsto_order.2 $ and.intro (assume b hb, gt_mem_sets_of_Liminf_gt hg $ hi.symm β–Έ hb) (assume b hb, lt_mem_sets_of_Limsup_lt hl $ hs.symm β–Έ hb) theorem Limsup_nhds (a : Ξ±) : Limsup (𝓝 a) = a := cInf_intro (is_bounded_le_nhds a) (assume a' (h : {n : Ξ± | n ≀ a'} ∈ 𝓝 a), show a ≀ a', from @mem_of_nhds Ξ± _ a _ h) (assume b (hba : a < b), show βˆƒc (h : {n : Ξ± | n ≀ c} ∈ 𝓝 a), c < b, from match dense_or_discrete a b with | or.inl ⟨c, hac, hcb⟩ := ⟨c, ge_mem_nhds hac, hcb⟩ | or.inr ⟨_, h⟩ := ⟨a, (𝓝 a).sets_of_superset (gt_mem_nhds hba) h, hba⟩ end) theorem Liminf_nhds : βˆ€ (a : Ξ±), Liminf (𝓝 a) = a := @Limsup_nhds (order_dual Ξ±) _ _ _ /-- If a filter is converging, its limsup coincides with its limit. -/ theorem Liminf_eq_of_le_nhds {f : filter Ξ±} {a : Ξ±} (hf : f β‰  βŠ₯) (h : f ≀ 𝓝 a) : f.Liminf = a := have hb_ge : is_bounded (β‰₯) f, from is_bounded_of_le h (is_bounded_ge_nhds a), have hb_le : is_bounded (≀) f, from is_bounded_of_le h (is_bounded_le_nhds a), le_antisymm (calc f.Liminf ≀ f.Limsup : Liminf_le_Limsup hf hb_le hb_ge ... ≀ (𝓝 a).Limsup : Limsup_le_Limsup_of_le h (is_cobounded_of_is_bounded hf hb_ge) (is_bounded_le_nhds a) ... = a : Limsup_nhds a) (calc a = (𝓝 a).Liminf : (Liminf_nhds a).symm ... ≀ f.Liminf : Liminf_le_Liminf_of_le h (is_bounded_ge_nhds a) (is_cobounded_of_is_bounded hf hb_le)) /-- If a filter is converging, its liminf coincides with its limit. -/ theorem Limsup_eq_of_le_nhds : βˆ€ {f : filter Ξ±} {a : Ξ±}, f β‰  βŠ₯ β†’ f ≀ 𝓝 a β†’ f.Limsup = a := @Liminf_eq_of_le_nhds (order_dual Ξ±) _ _ _ end conditionally_complete_linear_order section complete_linear_order variables [complete_linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±] -- In complete_linear_order, the above theorems take a simpler form /-- If the liminf and the limsup of a function coincide, then the limit of the function exists and has the same value -/ theorem tendsto_of_liminf_eq_limsup {f : filter Ξ²} {u : Ξ² β†’ Ξ±} {a : Ξ±} (h : liminf f u = a ∧ limsup f u = a) : tendsto u f (𝓝 a) := le_nhds_of_Limsup_eq_Liminf is_bounded_le_of_top is_bounded_ge_of_bot h.2 h.1 /-- If a function has a limit, then its limsup coincides with its limit-/ theorem limsup_eq_of_tendsto {f : filter Ξ²} {u : Ξ² β†’ Ξ±} {a : Ξ±} (hf : f β‰  βŠ₯) (h : tendsto u f (𝓝 a)) : limsup f u = a := Limsup_eq_of_le_nhds (map_ne_bot hf) h /-- If a function has a limit, then its liminf coincides with its limit-/ theorem liminf_eq_of_tendsto {f : filter Ξ²} {u : Ξ² β†’ Ξ±} {a : Ξ±} (hf : f β‰  βŠ₯) (h : tendsto u f (𝓝 a)) : liminf f u = a := Liminf_eq_of_le_nhds (map_ne_bot hf) h end complete_linear_order end liminf_limsup end order_topology @[nolint ge_or_gt] -- see Note [nolint_ge] lemma order_topology_of_nhds_abs {Ξ± : Type*} [decidable_linear_ordered_add_comm_group Ξ±] [topological_space Ξ±] (h_nhds : βˆ€a:Ξ±, 𝓝 a = (β¨…r>0, principal {b | abs (a - b) < r})) : order_topology Ξ± := order_topology.mk $ eq_of_nhds_eq_nhds $ assume a:Ξ±, le_antisymm_iff.mpr begin simp [infi_and, topological_space.nhds_generate_from, h_nhds, le_infi_iff, -le_principal_iff, and_comm], refine ⟨λ s ha b hs, _, Ξ» r hr, _⟩, { rcases hs with rfl | rfl, { refine infi_le_of_le (a - b) (infi_le_of_le (lt_sub_left_of_add_lt $ by simpa using ha) $ principal_mono.mpr $ assume c (hc : abs (a - c) < a - b), _), have : a - c < a - b := lt_of_le_of_lt (le_abs_self _) hc, exact lt_of_neg_lt_neg (lt_of_add_lt_add_left this) }, { refine infi_le_of_le (b - a) (infi_le_of_le (lt_sub_left_of_add_lt $ by simpa using ha) $ principal_mono.mpr $ assume c (hc : abs (a - c) < b - a), _), have : abs (c - a) < b - a, {rw abs_sub; simpa using hc}, have : c - a < b - a := lt_of_le_of_lt (le_abs_self _) this, exact lt_of_add_lt_add_right this } }, { have h : {b | abs (a - b) < r} = {b | a - r < b} ∩ {b | b < a + r}, from set.ext (assume b, by simp [abs_lt, sub_lt, lt_sub_iff_add_lt, sub_lt_iff_lt_add']; cc), rw [h, ← inf_principal], apply le_inf _ _, { exact infi_le_of_le {b : Ξ± | a - r < b} (infi_le_of_le (sub_lt_self a hr) $ infi_le_of_le (a - r) $ infi_le _ (or.inl rfl)) }, { exact infi_le_of_le {b : Ξ± | b < a + r} (infi_le_of_le (lt_add_of_pos_right _ hr) $ infi_le_of_le (a + r) $ infi_le _ (or.inr rfl)) } } end lemma tendsto_at_top_supr_nat [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±] (f : β„• β†’ Ξ±) (hf : monotone f) : tendsto f at_top (𝓝 (⨆i, f i)) := tendsto_order.2 $ and.intro (assume a ha, let ⟨n, hn⟩ := lt_supr_iff.1 ha in mem_at_top_sets.2 ⟨n, assume i hi, lt_of_lt_of_le hn (hf hi)⟩) (assume a ha, univ_mem_sets' (assume n, lt_of_le_of_lt (le_supr _ n) ha)) lemma tendsto_at_top_infi_nat [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±] (f : β„• β†’ Ξ±) (hf : βˆ€{n m}, n ≀ m β†’ f m ≀ f n) : tendsto f at_top (𝓝 (β¨…i, f i)) := @tendsto_at_top_supr_nat (order_dual Ξ±) _ _ _ _ @hf lemma supr_eq_of_tendsto {Ξ±} [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±] {f : β„• β†’ Ξ±} {a : Ξ±} (hf : monotone f) : tendsto f at_top (𝓝 a) β†’ supr f = a := tendsto_nhds_unique at_top_ne_bot (tendsto_at_top_supr_nat f hf) lemma infi_eq_of_tendsto {Ξ±} [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±] {f : β„• β†’ Ξ±} {a : Ξ±} (hf : βˆ€n m, n ≀ m β†’ f m ≀ f n) : tendsto f at_top (𝓝 a) β†’ infi f = a := tendsto_nhds_unique at_top_ne_bot (tendsto_at_top_infi_nat f hf) lemma tendsto_abs_at_top_at_top [decidable_linear_ordered_add_comm_group Ξ±] : tendsto (abs : Ξ± β†’ Ξ±) at_top at_top := tendsto_at_top_mono _ (Ξ» n, le_abs_self _) tendsto_id local notation `|` x `|` := abs x @[nolint ge_or_gt] -- see Note [nolint_ge] lemma decidable_linear_ordered_add_comm_group.tendsto_nhds [decidable_linear_ordered_add_comm_group Ξ±] [topological_space Ξ±] [order_topology Ξ±] {Ξ² : Type*} (f : Ξ² β†’ Ξ±) (x : filter Ξ²) (a : Ξ±) : filter.tendsto f x (nhds a) ↔ βˆ€ Ξ΅ > (0 : Ξ±), βˆ€αΆ  b in x, |f b - a| < Ξ΅ := begin rw (show _, from @tendsto_order Ξ±), -- does not work without `show` for some reason split, { rintros ⟨hyp_lt_a, hyp_gt_a⟩ Ξ΅ Ξ΅_pos, suffices : {b : Ξ² | f b - a < Ξ΅ ∧ a - f b < Ξ΅} ∈ x, by simpa only [abs_sub_lt_iff], have set1 : {b : Ξ² | a - f b < Ξ΅} ∈ x, { have : {b : Ξ² | a - Ξ΅ < f b} ∈ x, from hyp_lt_a (a - Ξ΅) (sub_lt_self a Ξ΅_pos), have : βˆ€ b, a - f b < Ξ΅ ↔ a - Ξ΅ < f b, by { intro _, exact sub_lt }, simpa only [this] }, have set2 : {b : Ξ² | f b - a < Ξ΅} ∈ x, { have : {b : Ξ² | a + Ξ΅ > f b} ∈ x, from hyp_gt_a (a + Ξ΅) (lt_add_of_pos_right a Ξ΅_pos), have : βˆ€ b, f b - a < Ξ΅ ↔ a + Ξ΅ > f b, by { intro _, exact sub_lt_iff_lt_add' }, simpa only [this] }, exact (x.inter_sets set2 set1) }, { assume hyp_Ξ΅_pos, split, { assume a' a'_lt_a, let Ξ΅ := a - a', have : {b : Ξ² | |f b - a| < Ξ΅} ∈ x, from hyp_Ξ΅_pos Ξ΅ (sub_pos.elim_right a'_lt_a), have : {b : Ξ² | f b - a < Ξ΅ ∧ a - f b < Ξ΅} ∈ x, by simpa only [abs_sub_lt_iff] using this, have : {b : Ξ² | a - f b < Ξ΅} ∈ x, from x.sets_of_superset this (set.inter_subset_right _ _), have : βˆ€ b, a' < f b ↔ a - f b < Ξ΅, by {intro b, rw [sub_lt, sub_sub_self] }, simpa only [this] }, { assume a' a'_gt_a, let Ξ΅ := a' - a, have : {b : Ξ² | |f b - a| < Ξ΅} ∈ x, from hyp_Ξ΅_pos Ξ΅ (sub_pos.elim_right a'_gt_a), have : {b : Ξ² | f b - a < Ξ΅ ∧ a - f b < Ξ΅} ∈ x, by simpa only [abs_sub_lt_iff] using this, have : {b : Ξ² | f b - a < Ξ΅} ∈ x, from x.sets_of_superset this (set.inter_subset_left _ _), have : βˆ€ b, f b < a' ↔ f b - a < Ξ΅, by { intro b, simp [lt_sub_iff_add_lt] }, simpa only [this] }} end
ff46a7b4d80544cf63d8a8bcd2c204f003b3c4c0
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/algebra/group/hom.lean
24f8a2efae4f96ed90a9ddfc3200fffd4c9d3a7e
[ "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
13,868
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Kevin Buzzard, Scott Morrison, Johan Commelin, Chris Hughes, Johannes HΓΆlzl, Yury Kudryashov -/ import algebra.group.commute /-! # monoid and group homomorphisms This file defines the bundled structures for monoid and group homomorphisms. Namely, we define `monoid_hom` (resp., `add_monoid_hom`) to be bundled homomorphisms between multiplicative (resp., additive) monoids or groups. We also define coercion to a function, and usual operations: composition, identity homomorphism, pointwise multiplication and pointwise inversion. ## Notations * `β†’*` for bundled monoid homs (also use for group homs) * `β†’+` for bundled add_monoid homs (also use for add_group homs) ## implementation notes There's a coercion from bundled homs to fun, and the canonical notation is to use the bundled hom as a function via this coercion. There is no `group_hom` -- the idea is that `monoid_hom` is used. The constructor for `monoid_hom` needs a proof of `map_one` as well as `map_mul`; a separate constructor `monoid_hom.mk'` will construct group homs (i.e. monoid homs between groups) given only a proof that multiplication is preserved, Implicit `{}` brackets are often used instead of type class `[]` brackets. This is done when the instances can be inferred because they are implicit arguments to the type `monoid_hom`. When they can be inferred from the type it is faster to use this method than to use type class inference. Historically this file also included definitions of unbundled homomorphism classes; they were deprecated and moved to `deprecated/group`. ## Tags monoid_hom, add_monoid_hom -/ variables {M : Type*} {N : Type*} {P : Type*} -- monoids {G : Type*} {H : Type*} -- groups /-- Bundled add_monoid homomorphisms; use this for bundled add_group homomorphisms too. -/ structure add_monoid_hom (M : Type*) (N : Type*) [add_monoid M] [add_monoid N] := (to_fun : M β†’ N) (map_zero' : to_fun 0 = 0) (map_add' : βˆ€ x y, to_fun (x + y) = to_fun x + to_fun y) infixr ` β†’+ `:25 := add_monoid_hom /-- Bundled monoid homomorphisms; use this for bundled group homomorphisms too. -/ @[to_additive] structure monoid_hom (M : Type*) (N : Type*) [monoid M] [monoid N] := (to_fun : M β†’ N) (map_one' : to_fun 1 = 1) (map_mul' : βˆ€ x y, to_fun (x * y) = to_fun x * to_fun y) infixr ` β†’* `:25 := monoid_hom @[to_additive] instance {M : Type*} {N : Type*} {mM : monoid M} {mN : monoid N} : has_coe_to_fun (M β†’* N) := ⟨_, monoid_hom.to_fun⟩ namespace monoid_hom variables {mM : monoid M} {mN : monoid N} {mP : monoid P} variables [group G] [comm_group H] include mM mN @[simp, to_additive] lemma to_fun_eq_coe (f : M β†’* N) : f.to_fun = f := rfl @[simp, to_additive] lemma coe_mk (f : M β†’ N) (h1 hmul) : ⇑(monoid_hom.mk f h1 hmul) = f := rfl @[to_additive] lemma coe_inj ⦃f g : M β†’* N⦄ (h : (f : M β†’ N) = g) : f = g := by cases f; cases g; cases h; refl @[ext, to_additive] lemma ext ⦃f g : M β†’* N⦄ (h : βˆ€ x, f x = g x) : f = g := coe_inj (funext h) attribute [ext] _root_.add_monoid_hom.ext @[to_additive] lemma ext_iff {f g : M β†’* N} : f = g ↔ βˆ€ x, f x = g x := ⟨λ h x, h β–Έ rfl, Ξ» h, ext h⟩ /-- If `f` is a monoid homomorphism then `f 1 = 1`. -/ @[simp, to_additive] lemma map_one (f : M β†’* N) : f 1 = 1 := f.map_one' /-- If `f` is an additive monoid homomorphism then `f 0 = 0`. -/ add_decl_doc add_monoid_hom.map_zero /-- If `f` is a monoid homomorphism then `f (a * b) = f a * f b`. -/ @[simp, to_additive] lemma map_mul (f : M β†’* N) (a b : M) : f (a * b) = f a * f b := f.map_mul' a b /-- If `f` is an additive monoid homomorphism then `f (a + b) = f a + f b`. -/ add_decl_doc add_monoid_hom.map_add @[to_additive] lemma map_mul_eq_one (f : M β†’* N) {a b : M} (h : a * b = 1) : f a * f b = 1 := by rw [← f.map_mul, h, f.map_one] /-- Given a monoid homomorphism `f : M β†’* N` and an element `x : M`, if `x` has a right inverse, then `f x` has a right inverse too. For elements invertible on both sides see `is_unit.map`. -/ @[to_additive "Given an add_monoid homomorphism `f : M β†’+ N` and an element `x : M`, if `x` has a right inverse, then `f x` has a right inverse too."] lemma map_exists_right_inv (f : M β†’* N) {x : M} (hx : βˆƒ y, x * y = 1) : βˆƒ y, f x * y = 1 := let ⟨y, hy⟩ := hx in ⟨f y, f.map_mul_eq_one hy⟩ /-- Given a monoid homomorphism `f : M β†’* N` and an element `x : M`, if `x` has a left inverse, then `f x` has a left inverse too. For elements invertible on both sides see `is_unit.map`. -/ @[to_additive "Given an add_monoid homomorphism `f : M β†’+ N` and an element `x : M`, if `x` has a left inverse, then `f x` has a left inverse too. For elements invertible on both sides see `is_add_unit.map`."] lemma map_exists_left_inv (f : M β†’* N) {x : M} (hx : βˆƒ y, y * x = 1) : βˆƒ y, y * f x = 1 := let ⟨y, hy⟩ := hx in ⟨f y, f.map_mul_eq_one hy⟩ omit mN mM /-- The identity map from a monoid to itself. -/ @[to_additive] def id (M : Type*) [monoid M] : M β†’* M := { to_fun := id, map_one' := rfl, map_mul' := Ξ» _ _, rfl } /-- The identity map from an additive monoid to itself. -/ add_decl_doc add_monoid_hom.id @[simp, to_additive] lemma id_apply {M : Type*} [monoid M] (x : M) : id M x = x := rfl include mM mN mP /-- Composition of monoid morphisms as a monoid morphism. -/ @[to_additive] def comp (hnp : N β†’* P) (hmn : M β†’* N) : M β†’* P := { to_fun := hnp ∘ hmn, map_one' := by simp, map_mul' := by simp } /-- Composition of additive monoid morphisms as an additive monoid morphism. -/ add_decl_doc add_monoid_hom.comp @[simp, to_additive] lemma comp_apply (g : N β†’* P) (f : M β†’* N) (x : M) : g.comp f x = g (f x) := rfl /-- Composition of monoid homomorphisms is associative. -/ @[to_additive] lemma comp_assoc {Q : Type*} [monoid Q] (f : M β†’* N) (g : N β†’* P) (h : P β†’* Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[to_additive] lemma cancel_right {g₁ gβ‚‚ : N β†’* P} {f : M β†’* N} (hf : function.surjective f) : g₁.comp f = gβ‚‚.comp f ↔ g₁ = gβ‚‚ := ⟨λ h, monoid_hom.ext $ (forall_iff_forall_surj hf).1 (ext_iff.1 h), Ξ» h, h β–Έ rfl⟩ @[to_additive] lemma cancel_left {g : N β†’* P} {f₁ fβ‚‚ : M β†’* N} (hg : function.injective g) : g.comp f₁ = g.comp fβ‚‚ ↔ f₁ = fβ‚‚ := ⟨λ h, monoid_hom.ext $ Ξ» x, hg $ by rw [← comp_apply, h, comp_apply], Ξ» h, h β–Έ rfl⟩ omit mP @[simp, to_additive] lemma comp_id (f : M β†’* N) : f.comp (id M) = f := ext $ Ξ» x, rfl @[simp, to_additive] lemma id_comp (f : M β†’* N) : (id N).comp f = f := ext $ Ξ» x, rfl end monoid_hom section End namespace monoid variables (M) [monoid M] /-- The monoid of endomorphisms. -/ protected def End := M β†’* M namespace End instance : monoid (monoid.End M) := { mul := monoid_hom.comp, one := monoid_hom.id M, mul_assoc := Ξ» _ _ _, monoid_hom.comp_assoc _ _ _, mul_one := monoid_hom.comp_id, one_mul := monoid_hom.id_comp } instance : inhabited (monoid.End M) := ⟨1⟩ instance : has_coe_to_fun (monoid.End M) := ⟨_, monoid_hom.to_fun⟩ end End @[simp] lemma coe_one : ((1 : monoid.End M) : M β†’ M) = id := rfl @[simp] lemma coe_mul (f g) : ((f * g : monoid.End M) : M β†’ M) = f ∘ g := rfl end monoid namespace add_monoid variables (A : Type*) [add_monoid A] /-- The monoid of endomorphisms. -/ protected def End := A β†’+ A namespace End instance : monoid (add_monoid.End A) := { mul := add_monoid_hom.comp, one := add_monoid_hom.id A, mul_assoc := Ξ» _ _ _, add_monoid_hom.comp_assoc _ _ _, mul_one := add_monoid_hom.comp_id, one_mul := add_monoid_hom.id_comp } instance : inhabited (add_monoid.End A) := ⟨1⟩ instance : has_coe_to_fun (add_monoid.End A) := ⟨_, add_monoid_hom.to_fun⟩ end End @[simp] lemma coe_one : ((1 : add_monoid.End A) : A β†’ A) = id := rfl @[simp] lemma coe_mul (f g) : ((f * g : add_monoid.End A) : A β†’ A) = f ∘ g := rfl end add_monoid end End namespace monoid_hom variables [mM : monoid M] [mN : monoid N] [mP : monoid P] variables [group G] [comm_group H] include mM mN /-- `1` is the monoid homomorphism sending all elements to `1`. -/ @[to_additive] instance : has_one (M β†’* N) := ⟨⟨λ _, 1, rfl, Ξ» _ _, (one_mul 1).symm⟩⟩ /-- `0` is the additive monoid homomorphism sending all elements to `0`. -/ add_decl_doc add_monoid_hom.has_zero @[simp, to_additive] lemma one_apply (x : M) : (1 : M β†’* N) x = 1 := rfl @[to_additive] instance : inhabited (M β†’* N) := ⟨1⟩ omit mM mN /-- Given two monoid morphisms `f`, `g` to a commutative monoid, `f * g` is the monoid morphism sending `x` to `f x * g x`. -/ @[to_additive] instance {M N} {mM : monoid M} [comm_monoid N] : has_mul (M β†’* N) := ⟨λ f g, { to_fun := Ξ» m, f m * g m, map_one' := show f 1 * g 1 = 1, by simp, map_mul' := begin intros, show f (x * y) * g (x * y) = f x * g x * (f y * g y), rw [f.map_mul, g.map_mul, ←mul_assoc, ←mul_assoc, mul_right_comm (f x)], end }⟩ /-- Given two additive monoid morphisms `f`, `g` to an additive commutative monoid, `f + g` is the additive monoid morphism sending `x` to `f x + g x`. -/ add_decl_doc add_monoid_hom.has_add @[simp, to_additive] lemma mul_apply {M N} {mM : monoid M} {mN : comm_monoid N} (f g : M β†’* N) (x : M) : (f * g) x = f x * g x := rfl /-- (M β†’* N) is a comm_monoid if N is commutative. -/ @[to_additive] instance {M N} [monoid M] [comm_monoid N] : comm_monoid (M β†’* N) := { mul := (*), mul_assoc := by intros; ext; apply mul_assoc, one := 1, one_mul := by intros; ext; apply one_mul, mul_one := by intros; ext; apply mul_one, mul_comm := by intros; ext; apply mul_comm } /-- `flip` arguments of `f : M β†’* N β†’* P` -/ @[to_additive "`flip` arguments of `f : M β†’+ N β†’+ P`"] def flip {mM : monoid M} {mN : monoid N} {mP : comm_monoid P} (f : M β†’* N β†’* P) : N β†’* M β†’* P := { to_fun := Ξ» y, ⟨λ x, f x y, by rw [f.map_one, one_apply], Ξ» x₁ xβ‚‚, by rw [f.map_mul, mul_apply]⟩, map_one' := ext $ Ξ» x, (f x).map_one, map_mul' := Ξ» y₁ yβ‚‚, ext $ Ξ» x, (f x).map_mul y₁ yβ‚‚ } @[simp, to_additive] lemma flip_apply {mM : monoid M} {mN : monoid N} {mP : comm_monoid P} (f : M β†’* N β†’* P) (x : M) (y : N) : f.flip y x = f x y := rfl /-- If two homomorphism from a group to a monoid are equal at `x`, then they are equal at `x⁻¹`. -/ @[to_additive "If two homomorphism from an additive group to an additive monoid are equal at `x`, then they are equal at `-x`." ] lemma eq_on_inv {G} [group G] [monoid M] {f g : G β†’* M} {x : G} (h : f x = g x) : f x⁻¹ = g x⁻¹ := left_inv_eq_right_inv (f.map_mul_eq_one $ inv_mul_self x) $ h.symm β–Έ g.map_mul_eq_one $ mul_inv_self x /-- Group homomorphisms preserve inverse. -/ @[simp, to_additive] theorem map_inv {G H} [group G] [group H] (f : G β†’* H) (g : G) : f g⁻¹ = (f g)⁻¹ := eq_inv_of_mul_eq_one $ f.map_mul_eq_one $ inv_mul_self g /-- Group homomorphisms preserve division. -/ @[simp, to_additive] theorem map_mul_inv {G H} [group G] [group H] (f : G β†’* H) (g h : G) : f (g * h⁻¹) = (f g) * (f h)⁻¹ := by rw [f.map_mul, f.map_inv] /-- A homomorphism from a group to a monoid is injective iff its kernel is trivial. -/ @[to_additive] lemma injective_iff {G H} [group G] [monoid H] (f : G β†’* H) : function.injective f ↔ (βˆ€ a, f a = 1 β†’ a = 1) := ⟨λ h x hfx, h $ hfx.trans f.map_one.symm, Ξ» h x y hxy, mul_inv_eq_one.1 $ h _ $ by rw [f.map_mul, hxy, ← f.map_mul, mul_inv_self, f.map_one]⟩ include mM /-- Makes a group homomomorphism from a proof that the map preserves multiplication. -/ @[to_additive] def mk' (f : M β†’ G) (map_mul : βˆ€ a b : M, f (a * b) = f a * f b) : M β†’* G := { to_fun := f, map_mul' := map_mul, map_one' := mul_self_iff_eq_one.1 $ by rw [←map_mul, mul_one] } /-- Makes an additive group homomomorphism from a proof that the map preserves multiplication. -/ add_decl_doc add_monoid_hom.mk' @[simp, to_additive] lemma coe_mk' {f : M β†’ G} (map_mul : βˆ€ a b : M, f (a * b) = f a * f b) : ⇑(mk' f map_mul) = f := rfl omit mM /-- If `f` is a monoid homomorphism to a commutative group, then `f⁻¹` is the homomorphism sending `x` to `(f x)⁻¹`. -/ @[to_additive] instance {M G} [monoid M] [comm_group G] : has_inv (M β†’* G) := ⟨λ f, mk' (Ξ» g, (f g)⁻¹) $ Ξ» a b, by rw [←mul_inv, f.map_mul]⟩ /-- If `f` is an additive monoid homomorphism to an additive commutative group, then `-f` is the homomorphism sending `x` to `-(f x)`. -/ add_decl_doc add_monoid_hom.has_neg @[simp, to_additive] lemma inv_apply {M G} {mM : monoid M} {gG : comm_group G} (f : M β†’* G) (x : M) : f⁻¹ x = (f x)⁻¹ := rfl /-- If `G` is a commutative group, then `M β†’* G` a commutative group too. -/ @[to_additive] instance {M G} [monoid M] [comm_group G] : comm_group (M β†’* G) := { inv := has_inv.inv, mul_left_inv := by intros; ext; apply mul_left_inv, ..monoid_hom.comm_monoid } /-- If `G` is an additive commutative group, then `M β†’+ G` an additive commutative group too. -/ add_decl_doc add_monoid_hom.add_comm_group end monoid_hom namespace add_monoid_hom /-- Additive group homomorphisms preserve subtraction. -/ @[simp] theorem map_sub {G H} [add_group G] [add_group H] (f : G β†’+ H) (g h : G) : f (g - h) = (f g) - (f h) := f.map_add_neg g h end add_monoid_hom section commute variables [monoid M] [monoid N] {a x y : M} @[simp, to_additive] protected lemma semiconj_by.map (h : semiconj_by a x y) (f : M β†’* N) : semiconj_by (f a) (f x) (f y) := by simpa only [semiconj_by, f.map_mul] using congr_arg f h @[simp, to_additive] protected lemma commute.map (h : commute x y) (f : M β†’* N) : commute (f x) (f y) := h.map f end commute
f10155085593d41c951fceb1fc88dc63847936f3
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/subtype/instances_auto.lean
9aa2f1880eac605d1664e9e89e7d6255d75087b4
[]
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
938
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.meta.mk_dec_eq_instance import Mathlib.Lean3Lib.init.data.subtype.basic universes u namespace Mathlib protected instance subtype.decidable_eq {Ξ± : Type u} {p : Ξ± β†’ Prop} [DecidableEq Ξ±] : DecidableEq (Subtype fun (x : Ξ±) => p x) := id fun (_v : Subtype fun (x : Ξ±) => p x) => subtype.cases_on _v fun (val : Ξ±) (property : p val) (w : Subtype fun (x : Ξ±) => p x) => subtype.cases_on w fun (w_val : Ξ±) (w_property : p w_val) => decidable.by_cases (fun (αΎ° : val = w_val) => Eq._oldrec (fun (w_property : p val) => is_true sorry) αΎ° w_property) fun (αΎ° : Β¬val = w_val) => isFalse sorry end Mathlib
73feeb94040c240dcb76c4e9d007a88e2549c286
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/hom/units.lean
40746ceff609ee39361c9b0c16ddeb19f16b1185
[ "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
12,353
lean
/- Copyright (c) 2018 Johan Commelin All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Chris Hughes, Kevin Buzzard -/ import algebra.hom.group /-! # Monoid homomorphisms and units This file allows to lift monoid homomorphisms to group homomorphisms of their units subgroups. It also contains unrelated results about `units` that depend on `monoid_hom`. ## Main declarations * `units.map`: Turn an homomorphism from `Ξ±` to `Ξ²` monoids into an homomorphism from `Ξ±Λ£` to `Ξ²Λ£`. * `monoid_hom.to_hom_units`: Turn an homomorphism from a group `Ξ±` to `Ξ²` into an homomorphism from `Ξ±` to `Ξ²Λ£`. ## TODO The results that don't mention homomorphisms should be proved (earlier?) in a different file and be used to golf the basic `group` lemmas. -/ open function universes u v w namespace units variables {Ξ± : Type*} {M : Type u} {N : Type v} {P : Type w} [monoid M] [monoid N] [monoid P] /-- The group homomorphism on units induced by a `monoid_hom`. -/ @[to_additive "The `add_group` homomorphism on `add_unit`s induced by an `add_monoid_hom`."] def map (f : M β†’* N) : MΛ£ β†’* NΛ£ := monoid_hom.mk' (Ξ» u, ⟨f u.val, f u.inv, by rw [← f.map_mul, u.val_inv, f.map_one], by rw [← f.map_mul, u.inv_val, f.map_one]⟩) (Ξ» x y, ext (f.map_mul x y)) @[simp, to_additive] lemma coe_map (f : M β†’* N) (x : MΛ£) : ↑(map f x) = f x := rfl @[simp, to_additive] lemma coe_map_inv (f : M β†’* N) (u : MΛ£) : ↑(map f u)⁻¹ = f ↑u⁻¹ := rfl @[simp, to_additive] lemma map_comp (f : M β†’* N) (g : N β†’* P) : map (g.comp f) = (map g).comp (map f) := rfl variables (M) @[simp, to_additive] lemma map_id : map (monoid_hom.id M) = monoid_hom.id MΛ£ := by ext; refl /-- Coercion `MΛ£ β†’ M` as a monoid homomorphism. -/ @[to_additive "Coercion `add_units M β†’ M` as an add_monoid homomorphism."] def coe_hom : MΛ£ β†’* M := ⟨coe, coe_one, coe_mul⟩ variable {M} @[simp, to_additive] lemma coe_hom_apply (x : MΛ£) : coe_hom M x = ↑x := rfl @[simp, norm_cast, to_additive] lemma coe_pow (u : MΛ£) (n : β„•) : ((u ^ n : MΛ£) : M) = u ^ n := (units.coe_hom M).map_pow u n section division_monoid variables [division_monoid Ξ±] @[simp, norm_cast, to_additive] lemma coe_div : βˆ€ u₁ uβ‚‚ : Ξ±Λ£, ↑(u₁ / uβ‚‚) = (u₁ / uβ‚‚ : Ξ±) := (units.coe_hom Ξ±).map_div @[simp, norm_cast, to_additive] lemma coe_zpow : βˆ€ (u : Ξ±Λ£) (n : β„€), ((u ^ n : Ξ±Λ£) : Ξ±) = u ^ n := (units.coe_hom Ξ±).map_zpow @[field_simps] lemma _root_.divp_eq_div (a : Ξ±) (u : Ξ±Λ£) : a /β‚š u = a / u := by rw [div_eq_mul_inv, divp, u.coe_inv] @[simp, to_additive] lemma _root_.map_units_inv {F : Type*} [monoid_hom_class F M Ξ±] (f : F) (u : units M) : f ↑u⁻¹ = (f u)⁻¹ := ((f : M β†’* Ξ±).comp (units.coe_hom M)).map_inv u end division_monoid /-- If a map `g : M β†’ NΛ£` agrees with a homomorphism `f : M β†’* N`, then this map is a monoid homomorphism too. -/ @[to_additive "If a map `g : M β†’ add_units N` agrees with a homomorphism `f : M β†’+ N`, then this map is an add_monoid homomorphism too."] def lift_right (f : M β†’* N) (g : M β†’ NΛ£) (h : βˆ€ x, ↑(g x) = f x) : M β†’* NΛ£ := { to_fun := g, map_one' := units.ext $ (h 1).symm β–Έ f.map_one, map_mul' := Ξ» x y, units.ext $ by simp only [h, coe_mul, f.map_mul] } @[simp, to_additive] lemma coe_lift_right {f : M β†’* N} {g : M β†’ NΛ£} (h : βˆ€ x, ↑(g x) = f x) (x) : (lift_right f g h x : N) = f x := h x @[simp, to_additive] lemma mul_lift_right_inv {f : M β†’* N} {g : M β†’ NΛ£} (h : βˆ€ x, ↑(g x) = f x) (x) : f x * ↑(lift_right f g h x)⁻¹ = 1 := by rw [units.mul_inv_eq_iff_eq_mul, one_mul, coe_lift_right] @[simp, to_additive] lemma lift_right_inv_mul {f : M β†’* N} {g : M β†’ NΛ£} (h : βˆ€ x, ↑(g x) = f x) (x) : ↑(lift_right f g h x)⁻¹ * f x = 1 := by rw [units.inv_mul_eq_iff_eq_mul, mul_one, coe_lift_right] end units namespace monoid_hom /-- If `f` is a homomorphism from a group `G` to a monoid `M`, then its image lies in the units of `M`, and `f.to_hom_units` is the corresponding monoid homomorphism from `G` to `MΛ£`. -/ @[to_additive "If `f` is a homomorphism from an additive group `G` to an additive monoid `M`, then its image lies in the `add_units` of `M`, and `f.to_hom_units` is the corresponding homomorphism from `G` to `add_units M`."] def to_hom_units {G M : Type*} [group G] [monoid M] (f : G β†’* M) : G β†’* MΛ£ := units.lift_right f (Ξ» g, ⟨f g, f g⁻¹, map_mul_eq_one f (mul_inv_self _), map_mul_eq_one f (inv_mul_self _)⟩) (Ξ» g, rfl) @[simp] lemma coe_to_hom_units {G M : Type*} [group G] [monoid M] (f : G β†’* M) (g : G): (f.to_hom_units g : M) = f g := rfl end monoid_hom namespace is_unit variables {F G Ξ± M N : Type*} section monoid variables [monoid M] [monoid N] @[to_additive] lemma map [monoid_hom_class F M N] (f : F) {x : M} (h : is_unit x) : is_unit (f x) := by rcases h with ⟨y, rfl⟩; exact (units.map (f : M β†’* N) y).is_unit @[to_additive] lemma of_left_inverse [monoid_hom_class F M N] [monoid_hom_class G N M] {f : F} {x : M} (g : G) (hfg : function.left_inverse g f) (h : is_unit (f x)) : is_unit x := by simpa only [hfg x] using h.map g @[to_additive] lemma _root_.is_unit_map_of_left_inverse [monoid_hom_class F M N] [monoid_hom_class G N M] {f : F} {x : M} (g : G) (hfg : function.left_inverse g f) : is_unit (f x) ↔ is_unit x := ⟨of_left_inverse g hfg, map _⟩ /-- If a homomorphism `f : M β†’* N` sends each element to an `is_unit`, then it can be lifted to `f : M β†’* NΛ£`. See also `units.lift_right` for a computable version. -/ @[to_additive "If a homomorphism `f : M β†’+ N` sends each element to an `is_add_unit`, then it can be lifted to `f : M β†’+ add_units N`. See also `add_units.lift_right` for a computable version."] noncomputable def lift_right (f : M β†’* N) (hf : βˆ€ x, is_unit (f x)) : M β†’* NΛ£ := units.lift_right f (Ξ» x, (hf x).unit) $ Ξ» x, rfl @[to_additive] lemma coe_lift_right (f : M β†’* N) (hf : βˆ€ x, is_unit (f x)) (x) : (is_unit.lift_right f hf x : N) = f x := rfl @[simp, to_additive] lemma mul_lift_right_inv (f : M β†’* N) (h : βˆ€ x, is_unit (f x)) (x) : f x * ↑(is_unit.lift_right f h x)⁻¹ = 1 := units.mul_lift_right_inv (Ξ» y, rfl) x @[simp, to_additive] lemma lift_right_inv_mul (f : M β†’* N) (h : βˆ€ x, is_unit (f x)) (x) : ↑(is_unit.lift_right f h x)⁻¹ * f x = 1 := units.lift_right_inv_mul (Ξ» y, rfl) x end monoid section division_monoid variables [division_monoid Ξ±] {a b c : Ξ±} /-- The element of the group of units, corresponding to an element of a monoid which is a unit. As opposed to `is_unit.unit`, the inverse is computable and comes from the inversion on `Ξ±`. This is useful to transfer properties of inversion in `units Ξ±` to `Ξ±`. See also `to_units`. -/ @[to_additive "The element of the additive group of additive units, corresponding to an element of an additive monoid which is an additive unit. As opposed to `is_add_unit.add_unit`, the negation is computable and comes from the negation on `Ξ±`. This is useful to transfer properties of negation in `add_units Ξ±` to `Ξ±`. See also `to_add_units`.", simps] def unit' (h : is_unit a) : Ξ±Λ£ := ⟨a, a⁻¹, h.mul_inv_cancel, h.inv_mul_cancel⟩ @[simp, to_additive] protected lemma mul_inv_cancel_left (h : is_unit a) : βˆ€ b, a * (a⁻¹ * b) = b := h.unit'.mul_inv_cancel_left @[simp, to_additive] protected lemma inv_mul_cancel_left (h : is_unit a) : βˆ€ b, a⁻¹ * (a * b) = b := h.unit'.inv_mul_cancel_left @[simp, to_additive] protected lemma mul_inv_cancel_right (h : is_unit b) (a : Ξ±) : a * b * b⁻¹ = a := h.unit'.mul_inv_cancel_right _ @[simp, to_additive] protected lemma inv_mul_cancel_right (h : is_unit b) (a : Ξ±) : a * b⁻¹ * b = a := h.unit'.inv_mul_cancel_right _ @[to_additive] protected lemma div_self (h : is_unit a) : a / a = 1 := by rw [div_eq_mul_inv, h.mul_inv_cancel] @[to_additive] protected lemma eq_mul_inv_iff_mul_eq (h : is_unit c) : a = b * c⁻¹ ↔ a * c = b := h.unit'.eq_mul_inv_iff_mul_eq @[to_additive] protected lemma eq_inv_mul_iff_mul_eq (h : is_unit b) : a = b⁻¹ * c ↔ b * a = c := h.unit'.eq_inv_mul_iff_mul_eq @[to_additive] protected lemma inv_mul_eq_iff_eq_mul (h : is_unit a) : a⁻¹ * b = c ↔ b = a * c := h.unit'.inv_mul_eq_iff_eq_mul @[to_additive] protected lemma mul_inv_eq_iff_eq_mul (h : is_unit b) : a * b⁻¹ = c ↔ a = c * b := h.unit'.mul_inv_eq_iff_eq_mul @[to_additive] protected lemma mul_inv_eq_one (h : is_unit b) : a * b⁻¹ = 1 ↔ a = b := @units.mul_inv_eq_one _ _ h.unit' _ @[to_additive] protected lemma inv_mul_eq_one (h : is_unit a) : a⁻¹ * b = 1 ↔ a = b := @units.inv_mul_eq_one _ _ h.unit' _ @[to_additive] protected lemma mul_eq_one_iff_eq_inv (h : is_unit b) : a * b = 1 ↔ a = b⁻¹ := @units.mul_eq_one_iff_eq_inv _ _ h.unit' _ @[to_additive] protected lemma mul_eq_one_iff_inv_eq (h : is_unit a) : a * b = 1 ↔ a⁻¹ = b := @units.mul_eq_one_iff_inv_eq _ _ h.unit' _ @[simp, to_additive] protected lemma div_mul_cancel (h : is_unit b) (a : Ξ±) : a / b * b = a := by rw [div_eq_mul_inv, h.inv_mul_cancel_right] @[simp, to_additive] protected lemma mul_div_cancel (h : is_unit b) (a : Ξ±) : a * b / b = a := by rw [div_eq_mul_inv, h.mul_inv_cancel_right] @[to_additive] protected lemma mul_one_div_cancel (h : is_unit a) : a * (1 / a) = 1 := by simp [h] @[to_additive] protected lemma one_div_mul_cancel (h : is_unit a) : (1 / a) * a = 1 := by simp [h] @[to_additive] lemma inv : is_unit a β†’ is_unit a⁻¹ := by { rintro ⟨u, rfl⟩, rw ←units.coe_inv, exact units.is_unit _ } @[to_additive] lemma div (ha : is_unit a) (hb : is_unit b) : is_unit (a / b) := by { rw div_eq_mul_inv, exact ha.mul hb.inv } @[to_additive] protected lemma div_left_inj (h : is_unit c) : a / c = b / c ↔ a = b := by { simp_rw div_eq_mul_inv, exact units.mul_left_inj h.inv.unit' } @[to_additive] protected lemma div_eq_iff (h : is_unit b) : a / b = c ↔ a = c * b := by rw [div_eq_mul_inv, h.mul_inv_eq_iff_eq_mul] @[to_additive] protected lemma eq_div_iff (h : is_unit c) : a = b / c ↔ a * c = b := by rw [div_eq_mul_inv, h.eq_mul_inv_iff_mul_eq] @[to_additive] protected lemma div_eq_of_eq_mul (h : is_unit b) : a = c * b β†’ a / b = c := h.div_eq_iff.2 @[to_additive] protected lemma eq_div_of_mul_eq (h : is_unit c) : a * c = b β†’ a = b / c := h.eq_div_iff.2 @[to_additive] protected lemma div_eq_one_iff_eq (h : is_unit b) : a / b = 1 ↔ a = b := ⟨eq_of_div_eq_one, Ξ» hab, hab.symm β–Έ h.div_self⟩ @[to_additive] protected lemma div_mul_left (h : is_unit b) : b / (a * b) = 1 / a := by rw [div_eq_mul_inv, mul_inv_rev, h.mul_inv_cancel_left, one_div] @[to_additive] protected lemma mul_div_mul_right (h : is_unit c) (a b : Ξ±) : (a * c) / (b * c) = a / b := by simp only [div_eq_mul_inv, mul_inv_rev, mul_assoc, h.mul_inv_cancel_left] @[to_additive] protected lemma mul_mul_div (a : Ξ±) (h : is_unit b) : a * b * (1 / b) = a := by simp [h] end division_monoid section division_comm_monoid variables [division_comm_monoid Ξ±] {a b c d : Ξ±} @[to_additive] protected lemma div_mul_right (h : is_unit a) (b : Ξ±) : a / (a * b) = 1 / b := by rw [mul_comm, h.div_mul_left] @[to_additive] protected lemma mul_div_cancel_left (h : is_unit a) (b : Ξ±) : a * b / a = b := by rw [mul_comm, h.mul_div_cancel] @[to_additive] protected lemma mul_div_cancel' (h : is_unit a) (b : Ξ±) : a * (b / a) = b := by rw [mul_comm, h.div_mul_cancel] @[to_additive] protected lemma mul_div_mul_left (h : is_unit c) (a b : Ξ±) : (c * a) / (c * b) = a / b := by rw [mul_comm c, mul_comm c, h.mul_div_mul_right] @[to_additive] protected lemma mul_eq_mul_of_div_eq_div (hb : is_unit b) (hd : is_unit d) (a c : Ξ±) (h : a / b = c / d) : a * d = c * b := by rw [←mul_one a, ←hb.div_self, ←mul_comm_div, h, div_mul_eq_mul_div, hd.div_mul_cancel] @[to_additive] protected lemma div_eq_div_iff (hb : is_unit b) (hd : is_unit d) : a / b = c / d ↔ a * d = c * b := by rw [←(hb.mul hd).mul_left_inj, ←mul_assoc, hb.div_mul_cancel, ←mul_assoc, mul_right_comm, hd.div_mul_cancel] @[to_additive] protected lemma div_div_cancel (h : is_unit a) : a / (a / b) = b := by rw [div_div_eq_mul_div, h.mul_div_cancel_left] end division_comm_monoid end is_unit
00d72716ab828024628f2446bb88db36fde72562
4727251e0cd73359b15b664c3170e5d754078599
/src/group_theory/perm/list.lean
72889079344dd4f7d4f7580ae438d67fefb58d13
[ "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
16,095
lean
/- Copyright (c) 2021 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky -/ import data.list.rotate import group_theory.perm.support /-! # Permutations from a list A list `l : list Ξ±` can be interpreted as a `equiv.perm Ξ±` where each element in the list is permuted to the next one, defined as `form_perm`. When we have that `nodup l`, we prove that `equiv.perm.support (form_perm l) = l.to_finset`, and that `form_perm l` is rotationally invariant, in `form_perm_rotate`. When there are duplicate elements in `l`, how and in what arrangement with respect to the other elements they appear in the list determines the formed permutation. This is because `list.form_perm` is implemented as a product of `equiv.swap`s. That means that presence of a sublist of two adjacent duplicates like `[..., x, x, ...]` will produce the same permutation as if the adjacent duplicates were not present. The `list.form_perm` definition is meant to primarily be used with `nodup l`, so that the resulting permutation is cyclic (if `l` has at least two elements). The presence of duplicates in a particular placement can lead `list.form_perm` to produce a nontrivial permutation that is noncyclic. -/ namespace list variables {Ξ± Ξ² : Type*} section form_perm variables [decidable_eq Ξ±] (l : list Ξ±) open equiv equiv.perm /-- A list `l : list Ξ±` can be interpreted as a `equiv.perm Ξ±` where each element in the list is permuted to the next one, defined as `form_perm`. When we have that `nodup l`, we prove that `equiv.perm.support (form_perm l) = l.to_finset`, and that `form_perm l` is rotationally invariant, in `form_perm_rotate`. -/ def form_perm : equiv.perm Ξ± := (zip_with equiv.swap l l.tail).prod @[simp] lemma form_perm_nil : form_perm ([] : list Ξ±) = 1 := rfl @[simp] lemma form_perm_singleton (x : Ξ±) : form_perm [x] = 1 := rfl @[simp] lemma form_perm_cons_cons (x y : Ξ±) (l : list Ξ±) : form_perm (x :: y :: l) = swap x y * form_perm (y :: l) := prod_cons lemma form_perm_pair (x y : Ξ±) : form_perm [x, y] = swap x y := rfl lemma form_perm_apply_of_not_mem (x : Ξ±) (l : list Ξ±) (h : x βˆ‰ l) : form_perm l x = x := begin cases l with y l, { simp }, induction l with z l IH generalizing x y, { simp }, { specialize IH x z (mt (mem_cons_of_mem y) h), simp only [not_or_distrib, mem_cons_iff] at h, simp [IH, swap_apply_of_ne_of_ne, h] } end lemma form_perm_apply_mem_of_mem (x : Ξ±) (l : list Ξ±) (h : x ∈ l) : form_perm l x ∈ l := begin cases l with y l, { simpa }, induction l with z l IH generalizing x y, { simpa using h }, { by_cases hx : x ∈ z :: l, { rw [form_perm_cons_cons, mul_apply, swap_apply_def], split_ifs; simp [IH _ _ hx] }, { replace h : x = y := or.resolve_right h hx, simp [form_perm_apply_of_not_mem _ _ hx, ←h] } } end @[simp] lemma form_perm_cons_concat_apply_last (x y : Ξ±) (xs : list Ξ±) : form_perm (x :: (xs ++ [y])) y = x := begin induction xs with z xs IH generalizing x y, { simp }, { simp [IH] } end @[simp] lemma form_perm_apply_last (x : Ξ±) (xs : list Ξ±) : form_perm (x :: xs) ((x :: xs).last (cons_ne_nil x xs)) = x := begin induction xs using list.reverse_rec_on with xs y IH generalizing x; simp end @[simp] lemma form_perm_apply_nth_le_length (x : Ξ±) (xs : list Ξ±) : form_perm (x :: xs) ((x :: xs).nth_le xs.length (by simp)) = x := by rw [nth_le_cons_length, form_perm_apply_last] lemma form_perm_apply_head (x y : Ξ±) (xs : list Ξ±) (h : nodup (x :: y :: xs)) : form_perm (x :: y :: xs) x = y := by simp [form_perm_apply_of_not_mem _ _ h.not_mem] lemma form_perm_apply_nth_le_zero (l : list Ξ±) (h : nodup l) (hl : 1 < l.length) : form_perm l (l.nth_le 0 (zero_lt_one.trans hl)) = l.nth_le 1 hl := begin rcases l with (_|⟨x, _|⟨y, tl⟩⟩), { simp }, { simp }, { simpa using form_perm_apply_head _ _ _ h } end lemma form_perm_eq_head_iff_eq_last (x y : Ξ±) : form_perm (y :: l) x = y ↔ x = last (y :: l) (cons_ne_nil _ _) := iff.trans (by rw form_perm_apply_last) (form_perm (y :: l)).injective.eq_iff lemma zip_with_swap_prod_support' (l l' : list Ξ±) : {x | (zip_with swap l l').prod x β‰  x} ≀ l.to_finset βŠ” l'.to_finset := begin simp only [set.sup_eq_union, set.le_eq_subset], induction l with y l hl generalizing l', { simp }, { cases l' with z l', { simp }, { intro x, simp only [set.union_subset_iff, mem_cons_iff, zip_with_cons_cons, foldr, prod_cons, mul_apply], intro hx, by_cases h : x ∈ {x | (zip_with swap l l').prod x β‰  x}, { specialize hl l' h, refine set.mem_union.elim hl (Ξ» hm, _) (Ξ» hm, _); { simp only [finset.coe_insert, set.mem_insert_iff, finset.mem_coe, to_finset_cons, mem_to_finset] at hm ⊒, simp [hm] } }, { simp only [not_not, set.mem_set_of_eq] at h, simp only [h, set.mem_set_of_eq] at hx, rw swap_apply_ne_self_iff at hx, rcases hx with ⟨hyz, rfl|rfl⟩; simp } } } end lemma zip_with_swap_prod_support [fintype Ξ±] (l l' : list Ξ±) : (zip_with swap l l').prod.support ≀ l.to_finset βŠ” l'.to_finset := begin intros x hx, have hx' : x ∈ {x | (zip_with swap l l').prod x β‰  x} := by simpa using hx, simpa using zip_with_swap_prod_support' _ _ hx' end lemma support_form_perm_le' : {x | form_perm l x β‰  x} ≀ l.to_finset := begin refine (zip_with_swap_prod_support' l l.tail).trans _, simpa [finset.subset_iff] using tail_subset l end lemma support_form_perm_le [fintype Ξ±] : support (form_perm l) ≀ l.to_finset := begin intros x hx, have hx' : x ∈ {x | form_perm l x β‰  x} := by simpa using hx, simpa using support_form_perm_le' _ hx' end lemma form_perm_apply_lt (xs : list Ξ±) (h : nodup xs) (n : β„•) (hn : n + 1 < xs.length) : form_perm xs (xs.nth_le n ((nat.lt_succ_self n).trans hn)) = xs.nth_le (n + 1) hn := begin induction n with n IH generalizing xs, { simpa using form_perm_apply_nth_le_zero _ h _ }, { rcases xs with (_|⟨x, _|⟨y, l⟩⟩), { simp }, { simp }, { specialize IH (y :: l) h.of_cons _, { simpa [nat.succ_lt_succ_iff] using hn }, simp only [swap_apply_eq_iff, coe_mul, form_perm_cons_cons, nth_le], generalize_proofs at IH, rw [IH, swap_apply_of_ne_of_ne, nth_le]; { rintro rfl, simpa [nth_le_mem _ _ _] using h } } } end lemma form_perm_apply_nth_le (xs : list Ξ±) (h : nodup xs) (n : β„•) (hn : n < xs.length) : form_perm xs (xs.nth_le n hn) = xs.nth_le ((n + 1) % xs.length) (nat.mod_lt _ (n.zero_le.trans_lt hn)) := begin cases xs with x xs, { simp }, { have : n ≀ xs.length, { refine nat.le_of_lt_succ _, simpa using hn }, rcases this.eq_or_lt with rfl|hn', { simp }, { simp [form_perm_apply_lt, h, nat.mod_eq_of_lt, nat.succ_lt_succ hn'] } } end lemma support_form_perm_of_nodup' (l : list Ξ±) (h : nodup l) (h' : βˆ€ (x : Ξ±), l β‰  [x]) : {x | form_perm l x β‰  x} = l.to_finset := begin apply le_antisymm, { exact support_form_perm_le' l }, { intros x hx, simp only [finset.mem_coe, mem_to_finset] at hx, obtain ⟨n, hn, rfl⟩ := nth_le_of_mem hx, rw [set.mem_set_of_eq, form_perm_apply_nth_le _ h], intro H, rw nodup_iff_nth_le_inj at h, specialize h _ _ _ _ H, cases (nat.succ_le_of_lt hn).eq_or_lt with hn' hn', { simp only [←hn', nat.mod_self] at h, refine not_exists.mpr h' _, simpa [←h, eq_comm, length_eq_one] using hn' }, { simpa [nat.mod_eq_of_lt hn'] using h } } end lemma support_form_perm_of_nodup [fintype Ξ±] (l : list Ξ±) (h : nodup l) (h' : βˆ€ (x : Ξ±), l β‰  [x]) : support (form_perm l) = l.to_finset := begin rw ←finset.coe_inj, convert support_form_perm_of_nodup' _ h h', simp [set.ext_iff] end lemma form_perm_rotate_one (l : list Ξ±) (h : nodup l) : form_perm (l.rotate 1) = form_perm l := begin have h' : nodup (l.rotate 1), { simpa using h }, ext x, by_cases hx : x ∈ l.rotate 1, { obtain ⟨k, hk, rfl⟩ := nth_le_of_mem hx, rw [form_perm_apply_nth_le _ h', nth_le_rotate l, nth_le_rotate l, form_perm_apply_nth_le _ h], simp }, { rw [form_perm_apply_of_not_mem _ _ hx, form_perm_apply_of_not_mem], simpa using hx } end lemma form_perm_rotate (l : list Ξ±) (h : nodup l) (n : β„•) : form_perm (l.rotate n) = form_perm l := begin induction n with n hn, { simp }, { rw [nat.succ_eq_add_one, ←rotate_rotate, form_perm_rotate_one, hn], rwa is_rotated.nodup_iff, exact is_rotated.forall l n } end lemma form_perm_eq_of_is_rotated {l l' : list Ξ±} (hd : nodup l) (h : l ~r l') : form_perm l = form_perm l' := begin obtain ⟨n, rfl⟩ := h, exact (form_perm_rotate l hd n).symm end lemma form_perm_reverse (l : list Ξ±) (h : nodup l) : form_perm l.reverse = (form_perm l)⁻¹ := begin -- Let's show `form_perm l` is an inverse to `form_perm l.reverse`. rw [eq_comm, inv_eq_iff_mul_eq_one], ext x, -- We only have to check for `x ∈ l` that `form_perm l (form_perm l.reverse x)` rw [mul_apply, one_apply], by_cases hx : x ∈ l, swap, { rw [form_perm_apply_of_not_mem x l.reverse, form_perm_apply_of_not_mem _ _ hx], simpa using hx }, { obtain ⟨k, hk, rfl⟩ := nth_le_of_mem (mem_reverse.mpr hx), rw [form_perm_apply_nth_le l.reverse (nodup_reverse.mpr h), nth_le_reverse', form_perm_apply_nth_le _ h, nth_le_reverse'], { congr, rw [length_reverse, ←nat.succ_le_iff, nat.succ_eq_add_one] at hk, cases hk.eq_or_lt with hk' hk', { simp [←hk'] }, { rw [length_reverse, nat.mod_eq_of_lt hk', tsub_add_eq_add_tsub (nat.le_pred_of_lt hk'), nat.mod_eq_of_lt], { simp }, { rw tsub_add_cancel_of_le, refine tsub_lt_self _ (nat.zero_lt_succ _), all_goals { simpa using (nat.zero_le _).trans_lt hk' } } } }, all_goals { rw [← tsub_add_eq_tsub_tsub, ←length_reverse], refine tsub_lt_self _ (zero_lt_one.trans_le (le_add_right le_rfl)), exact k.zero_le.trans_lt hk } }, end lemma form_perm_pow_apply_nth_le (l : list Ξ±) (h : nodup l) (n k : β„•) (hk : k < l.length) : (form_perm l ^ n) (l.nth_le k hk) = l.nth_le ((k + n) % l.length) (nat.mod_lt _ (k.zero_le.trans_lt hk)) := begin induction n with n hn, { simp [nat.mod_eq_of_lt hk] }, { simp [pow_succ, mul_apply, hn, form_perm_apply_nth_le _ h, nat.succ_eq_add_one, ←nat.add_assoc] } end lemma form_perm_pow_apply_head (x : Ξ±) (l : list Ξ±) (h : nodup (x :: l)) (n : β„•) : (form_perm (x :: l) ^ n) x = (x :: l).nth_le (n % (x :: l).length) (nat.mod_lt _ (nat.zero_lt_succ _)) := by { convert form_perm_pow_apply_nth_le _ h n 0 _; simp } lemma form_perm_ext_iff {x y x' y' : Ξ±} {l l' : list Ξ±} (hd : nodup (x :: y :: l)) (hd' : nodup (x' :: y' :: l')) : form_perm (x :: y :: l) = form_perm (x' :: y' :: l') ↔ (x :: y :: l) ~r (x' :: y' :: l') := begin refine ⟨λ h, _, Ξ» hr, form_perm_eq_of_is_rotated hd hr⟩, rw equiv.perm.ext_iff at h, have hx : x' ∈ (x :: y :: l), { have : x' ∈ {z | form_perm (x :: y :: l) z β‰  z}, { rw [set.mem_set_of_eq, h x', form_perm_apply_head _ _ _ hd'], simp only [mem_cons_iff, nodup_cons] at hd', push_neg at hd', exact hd'.left.left.symm }, simpa using support_form_perm_le' _ this }, obtain ⟨n, hn, hx'⟩ := nth_le_of_mem hx, have hl : (x :: y :: l).length = (x' :: y' :: l').length, { rw [←dedup_eq_self.mpr hd, ←dedup_eq_self.mpr hd', ←card_to_finset, ←card_to_finset], refine congr_arg finset.card _, rw [←finset.coe_inj, ←support_form_perm_of_nodup' _ hd (by simp), ←support_form_perm_of_nodup' _ hd' (by simp)], simp only [h] }, use n, apply list.ext_le, { rw [length_rotate, hl] }, { intros k hk hk', rw nth_le_rotate, induction k with k IH, { simp_rw [nat.zero_add, nat.mod_eq_of_lt hn], simpa }, { have : k.succ = (k + 1) % (x' :: y' :: l').length, { rw [←nat.succ_eq_add_one, nat.mod_eq_of_lt hk'] }, simp_rw this, rw [←form_perm_apply_nth_le _ hd' k (k.lt_succ_self.trans hk'), ←IH (k.lt_succ_self.trans hk), ←h, form_perm_apply_nth_le _ hd], congr' 1, have h1 : 1 = 1 % (x' :: y' :: l').length := by simp, rw [hl, nat.mod_eq_of_lt hk', h1, ←nat.add_mod, nat.succ_add] } } end lemma form_perm_apply_mem_eq_self_iff (hl : nodup l) (x : Ξ±) (hx : x ∈ l) : form_perm l x = x ↔ length l ≀ 1 := begin obtain ⟨k, hk, rfl⟩ := nth_le_of_mem hx, rw [form_perm_apply_nth_le _ hl, hl.nth_le_inj_iff], cases hn : l.length, { exact absurd k.zero_le (hk.trans_le hn.le).not_le }, { rw hn at hk, cases (nat.le_of_lt_succ hk).eq_or_lt with hk' hk', { simp [←hk', nat.succ_le_succ_iff, eq_comm] }, { simpa [nat.mod_eq_of_lt (nat.succ_lt_succ hk'), nat.succ_lt_succ_iff] using k.zero_le.trans_lt hk' } } end lemma form_perm_apply_mem_ne_self_iff (hl : nodup l) (x : Ξ±) (hx : x ∈ l) : form_perm l x β‰  x ↔ 2 ≀ l.length := begin rw [ne.def, form_perm_apply_mem_eq_self_iff _ hl x hx, not_le], exact ⟨nat.succ_le_of_lt, nat.lt_of_succ_le⟩ end lemma mem_of_form_perm_ne_self (l : list Ξ±) (x : Ξ±) (h : form_perm l x β‰  x) : x ∈ l := begin suffices : x ∈ {y | form_perm l y β‰  y}, { rw ←mem_to_finset, exact support_form_perm_le' _ this }, simpa using h end lemma form_perm_eq_self_of_not_mem (l : list Ξ±) (x : Ξ±) (h : x βˆ‰ l) : form_perm l x = x := by_contra (Ξ» H, h $ mem_of_form_perm_ne_self _ _ H) lemma form_perm_eq_one_iff (hl : nodup l) : form_perm l = 1 ↔ l.length ≀ 1 := begin cases l with hd tl, { simp }, { rw ←form_perm_apply_mem_eq_self_iff _ hl hd (mem_cons_self _ _), split, { simp {contextual := tt} }, { intro h, simp only [(hd :: tl).form_perm_apply_mem_eq_self_iff hl hd (mem_cons_self hd tl), add_le_iff_nonpos_left, length, nonpos_iff_eq_zero, length_eq_zero] at h, simp [h] } } end lemma form_perm_eq_form_perm_iff {l l' : list Ξ±} (hl : l.nodup) (hl' : l'.nodup) : l.form_perm = l'.form_perm ↔ l ~r l' ∨ l.length ≀ 1 ∧ l'.length ≀ 1 := begin rcases l with (_ | ⟨x, _ | ⟨y, l⟩⟩), { suffices : l'.length ≀ 1 ↔ l' = nil ∨ l'.length ≀ 1, { simpa [eq_comm, form_perm_eq_one_iff, hl, hl', length_eq_zero] }, refine ⟨λ h, or.inr h, _⟩, rintro (rfl | h), { simp }, { exact h } }, { suffices : l'.length ≀ 1 ↔ [x] ~r l' ∨ l'.length ≀ 1, { simpa [eq_comm, form_perm_eq_one_iff, hl, hl', length_eq_zero, le_rfl] }, refine ⟨λ h, or.inr h, _⟩, rintro (h | h), { simp [←h.perm.length_eq] }, { exact h } }, { rcases l' with (_ | ⟨x', _ | ⟨y', l'⟩⟩), { simp [form_perm_eq_one_iff, hl, -form_perm_cons_cons] }, { suffices : Β¬ (x :: y :: l) ~r [x'], { simp [form_perm_eq_one_iff, hl, -form_perm_cons_cons] }, intro h, simpa using h.perm.length_eq }, { simp [-form_perm_cons_cons, form_perm_ext_iff hl hl'] } } end lemma form_perm_zpow_apply_mem_imp_mem (l : list Ξ±) (x : Ξ±) (hx : x ∈ l) (n : β„€) : ((form_perm l) ^ n) x ∈ l := begin by_cases h : (l.form_perm ^ n) x = x, { simpa [h] using hx }, { have : x ∈ {x | (l.form_perm ^ n) x β‰  x} := h, rw ←set_support_apply_mem at this, replace this := set_support_zpow_subset _ _ this, simpa using support_form_perm_le' _ this } end lemma form_perm_pow_length_eq_one_of_nodup (hl : nodup l) : (form_perm l) ^ (length l) = 1 := begin ext x, by_cases hx : x ∈ l, { obtain ⟨k, hk, rfl⟩ := nth_le_of_mem hx, simp [form_perm_pow_apply_nth_le _ hl, nat.mod_eq_of_lt hk] }, { have : x βˆ‰ {x | (l.form_perm ^ l.length) x β‰  x}, { intros H, refine hx _, replace H := set_support_zpow_subset l.form_perm l.length H, simpa using support_form_perm_le' _ H }, simpa } end end form_perm end list
c76faa74f871e029105dcf602fb748b2a31064c2
c777c32c8e484e195053731103c5e52af26a25d1
/src/measure_theory/function/l2_space.lean
ee1620be1b073368d765d2bd855dcf320acce55f
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
10,834
lean
/- Copyright (c) 2021 RΓ©my Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: RΓ©my Degenne -/ import analysis.inner_product_space.basic import measure_theory.integral.set_integral /-! # `L^2` space If `E` is an inner product space over `π•œ` (`ℝ` or `β„‚`), then `Lp E 2 ΞΌ` (defined in `lp_space.lean`) is also an inner product space, with inner product defined as `inner f g = ∫ a, βŸͺf a, g a⟫ βˆ‚ΞΌ`. ### Main results * `mem_L1_inner` : for `f` and `g` in `Lp E 2 ΞΌ`, the pointwise inner product `Ξ» x, βŸͺf x, g x⟫` belongs to `Lp π•œ 1 ΞΌ`. * `integrable_inner` : for `f` and `g` in `Lp E 2 ΞΌ`, the pointwise inner product `Ξ» x, βŸͺf x, g x⟫` is integrable. * `L2.inner_product_space` : `Lp E 2 ΞΌ` is an inner product space. -/ noncomputable theory open topological_space measure_theory measure_theory.Lp open_locale nnreal ennreal measure_theory namespace measure_theory section variables {Ξ± F : Type*} {m : measurable_space Ξ±} {ΞΌ : measure Ξ±} [normed_add_comm_group F] lemma mem_β„’p.integrable_sq {f : Ξ± β†’ ℝ} (h : mem_β„’p f 2 ΞΌ) : integrable (Ξ» x, (f x)^2) ΞΌ := by simpa [← mem_β„’p_one_iff_integrable] using h.norm_rpow two_ne_zero ennreal.two_ne_top lemma mem_β„’p_two_iff_integrable_sq_norm {f : Ξ± β†’ F} (hf : ae_strongly_measurable f ΞΌ) : mem_β„’p f 2 ΞΌ ↔ integrable (Ξ» x, β€–f xβ€–^2) ΞΌ := begin rw ← mem_β„’p_one_iff_integrable, convert (mem_β„’p_norm_rpow_iff hf two_ne_zero ennreal.two_ne_top).symm, { simp }, { rw [div_eq_mul_inv, ennreal.mul_inv_cancel two_ne_zero ennreal.two_ne_top] } end lemma mem_β„’p_two_iff_integrable_sq {f : Ξ± β†’ ℝ} (hf : ae_strongly_measurable f ΞΌ) : mem_β„’p f 2 ΞΌ ↔ integrable (Ξ» x, (f x)^2) ΞΌ := begin convert mem_β„’p_two_iff_integrable_sq_norm hf, ext x, simp, end end namespace L2 variables {Ξ± E F π•œ : Type*} [is_R_or_C π•œ] [measurable_space Ξ±] {ΞΌ : measure Ξ±} [normed_add_comm_group E] [inner_product_space π•œ E] [normed_add_comm_group F] local notation `βŸͺ`x`, `y`⟫` := @inner π•œ _ _ x y lemma snorm_rpow_two_norm_lt_top (f : Lp F 2 ΞΌ) : snorm (Ξ» x, β€–f xβ€– ^ (2 : ℝ)) 1 ΞΌ < ∞ := begin have h_two : ennreal.of_real (2 : ℝ) = 2, by simp [zero_le_one], rw [snorm_norm_rpow f zero_lt_two, one_mul, h_two], exact ennreal.rpow_lt_top_of_nonneg zero_le_two (Lp.snorm_ne_top f), end lemma snorm_inner_lt_top (f g : Ξ± β†’β‚‚[ΞΌ] E) : snorm (Ξ» (x : Ξ±), βŸͺf x, g x⟫) 1 ΞΌ < ∞ := begin have h : βˆ€ x, β€–βŸͺf x, g xβŸ«β€– ≀ β€–β€–f xβ€– ^ (2 : ℝ) + β€–g xβ€– ^ (2 : ℝ)β€–, { intro x, rw [← @nat.cast_two ℝ, real.rpow_nat_cast, real.rpow_nat_cast], calc β€–βŸͺf x, g xβŸ«β€– ≀ β€–f xβ€– * β€–g xβ€– : norm_inner_le_norm _ _ ... ≀ 2 * β€–f xβ€– * β€–g xβ€– : mul_le_mul_of_nonneg_right (le_mul_of_one_le_left (norm_nonneg _) one_le_two) (norm_nonneg _) ... ≀ β€–β€–f xβ€–^2 + β€–g xβ€–^2β€– : (two_mul_le_add_sq _ _).trans (le_abs_self _) }, refine (snorm_mono_ae (ae_of_all _ h)).trans_lt ((snorm_add_le _ _ le_rfl).trans_lt _), { exact ((Lp.ae_strongly_measurable f).norm.ae_measurable.pow_const _).ae_strongly_measurable }, { exact ((Lp.ae_strongly_measurable g).norm.ae_measurable.pow_const _).ae_strongly_measurable }, rw [ennreal.add_lt_top], exact ⟨snorm_rpow_two_norm_lt_top f, snorm_rpow_two_norm_lt_top g⟩, end section inner_product_space open_locale complex_conjugate include π•œ instance : has_inner π•œ (Ξ± β†’β‚‚[ΞΌ] E) := ⟨λ f g, ∫ a, βŸͺf a, g a⟫ βˆ‚ΞΌβŸ© lemma inner_def (f g : Ξ± β†’β‚‚[ΞΌ] E) : βŸͺf, g⟫ = ∫ a : Ξ±, βŸͺf a, g a⟫ βˆ‚ΞΌ := rfl lemma integral_inner_eq_sq_snorm (f : Ξ± β†’β‚‚[ΞΌ] E) : ∫ a, βŸͺf a, f a⟫ βˆ‚ΞΌ = ennreal.to_real ∫⁻ a, (β€–f aβ€–β‚Š : ℝβ‰₯0∞) ^ (2:ℝ) βˆ‚ΞΌ := begin simp_rw inner_self_eq_norm_sq_to_K, norm_cast, rw integral_eq_lintegral_of_nonneg_ae, rotate, { exact filter.eventually_of_forall (Ξ» x, sq_nonneg _), }, { exact ((Lp.ae_strongly_measurable f).norm.ae_measurable.pow_const _).ae_strongly_measurable }, congr, ext1 x, have h_two : (2 : ℝ) = ((2 : β„•) : ℝ), by simp, rw [← real.rpow_nat_cast _ 2, ← h_two, ← ennreal.of_real_rpow_of_nonneg (norm_nonneg _) zero_le_two, of_real_norm_eq_coe_nnnorm], norm_cast, end private lemma norm_sq_eq_inner' (f : Ξ± β†’β‚‚[ΞΌ] E) : β€–fβ€– ^ 2 = is_R_or_C.re βŸͺf, f⟫ := begin have h_two : (2 : ℝβ‰₯0∞).to_real = 2 := by simp, rw [inner_def, integral_inner_eq_sq_snorm, norm_def, ← ennreal.to_real_pow, is_R_or_C.of_real_re, ennreal.to_real_eq_to_real (ennreal.pow_ne_top (Lp.snorm_ne_top f)) _], { rw [←ennreal.rpow_nat_cast, snorm_eq_snorm' two_ne_zero ennreal.two_ne_top, snorm', ← ennreal.rpow_mul, one_div, h_two], simp, }, { refine (lintegral_rpow_nnnorm_lt_top_of_snorm'_lt_top zero_lt_two _).ne, rw [← h_two, ← snorm_eq_snorm' two_ne_zero ennreal.two_ne_top], exact Lp.snorm_lt_top f, }, end lemma mem_L1_inner (f g : Ξ± β†’β‚‚[ΞΌ] E) : ae_eq_fun.mk (Ξ» x, βŸͺf x, g x⟫) ((Lp.ae_strongly_measurable f).inner (Lp.ae_strongly_measurable g)) ∈ Lp π•œ 1 ΞΌ := by { simp_rw [mem_Lp_iff_snorm_lt_top, snorm_ae_eq_fun], exact snorm_inner_lt_top f g, } lemma integrable_inner (f g : Ξ± β†’β‚‚[ΞΌ] E) : integrable (Ξ» x : Ξ±, βŸͺf x, g x⟫) ΞΌ := (integrable_congr (ae_eq_fun.coe_fn_mk (Ξ» x, βŸͺf x, g x⟫) ((Lp.ae_strongly_measurable f).inner (Lp.ae_strongly_measurable g)))).mp (ae_eq_fun.integrable_iff_mem_L1.mpr (mem_L1_inner f g)) private lemma add_left' (f f' g : Ξ± β†’β‚‚[ΞΌ] E) : βŸͺf + f', g⟫ = inner f g + inner f' g := begin simp_rw [inner_def, ← integral_add (integrable_inner f g) (integrable_inner f' g), ←inner_add_left], refine integral_congr_ae ((coe_fn_add f f').mono (Ξ» x hx, _)), congr, rwa pi.add_apply at hx, end private lemma smul_left' (f g : Ξ± β†’β‚‚[ΞΌ] E) (r : π•œ) : βŸͺr β€’ f, g⟫ = conj r * inner f g := begin rw [inner_def, inner_def, ← smul_eq_mul, ← integral_smul], refine integral_congr_ae ((coe_fn_smul r f).mono (Ξ» x hx, _)), rw [smul_eq_mul, ← inner_smul_left], congr, rwa pi.smul_apply at hx, end instance inner_product_space : inner_product_space π•œ (Ξ± β†’β‚‚[ΞΌ] E) := { norm_sq_eq_inner := norm_sq_eq_inner', conj_symm := Ξ» _ _, by simp_rw [inner_def, ← integral_conj, inner_conj_symm], add_left := add_left', smul_left := smul_left', } end inner_product_space section indicator_const_Lp variables (π•œ) {s : set Ξ±} /-- The inner product in `L2` of the indicator of a set `indicator_const_Lp 2 hs hΞΌs c` and `f` is equal to the integral of the inner product over `s`: `∫ x in s, βŸͺc, f x⟫ βˆ‚ΞΌ`. -/ lemma inner_indicator_const_Lp_eq_set_integral_inner (f : Lp E 2 ΞΌ) (hs : measurable_set s) (c : E) (hΞΌs : ΞΌ s β‰  ∞) : (βŸͺindicator_const_Lp 2 hs hΞΌs c, f⟫ : π•œ) = ∫ x in s, βŸͺc, f x⟫ βˆ‚ΞΌ := begin rw [inner_def, ← integral_add_compl hs (L2.integrable_inner _ f)], have h_left : ∫ x in s, βŸͺ(indicator_const_Lp 2 hs hΞΌs c) x, f x⟫ βˆ‚ΞΌ = ∫ x in s, βŸͺc, f x⟫ βˆ‚ΞΌ, { suffices h_ae_eq : βˆ€α΅ x βˆ‚ΞΌ, x ∈ s β†’ βŸͺindicator_const_Lp 2 hs hΞΌs c x, f x⟫ = βŸͺc, f x⟫, from set_integral_congr_ae hs h_ae_eq, have h_indicator : βˆ€α΅ (x : Ξ±) βˆ‚ΞΌ, x ∈ s β†’ (indicator_const_Lp 2 hs hΞΌs c x) = c, from indicator_const_Lp_coe_fn_mem, refine h_indicator.mono (Ξ» x hx hxs, _), congr, exact hx hxs, }, have h_right : ∫ x in sᢜ, βŸͺ(indicator_const_Lp 2 hs hΞΌs c) x, f x⟫ βˆ‚ΞΌ = 0, { suffices h_ae_eq : βˆ€α΅ x βˆ‚ΞΌ, x βˆ‰ s β†’ βŸͺindicator_const_Lp 2 hs hΞΌs c x, f x⟫ = 0, { simp_rw ← set.mem_compl_iff at h_ae_eq, suffices h_int_zero : ∫ x in sᢜ, inner (indicator_const_Lp 2 hs hΞΌs c x) (f x) βˆ‚ΞΌ = ∫ x in sᢜ, (0 : π•œ) βˆ‚ΞΌ, { rw h_int_zero, simp, }, exact set_integral_congr_ae hs.compl h_ae_eq, }, have h_indicator : βˆ€α΅ (x : Ξ±) βˆ‚ΞΌ, x βˆ‰ s β†’ (indicator_const_Lp 2 hs hΞΌs c x) = 0, from indicator_const_Lp_coe_fn_nmem, refine h_indicator.mono (Ξ» x hx hxs, _), rw hx hxs, exact inner_zero_left _, }, rw [h_left, h_right, add_zero], end /-- The inner product in `L2` of the indicator of a set `indicator_const_Lp 2 hs hΞΌs c` and `f` is equal to the inner product of the constant `c` and the integral of `f` over `s`. -/ lemma inner_indicator_const_Lp_eq_inner_set_integral [complete_space E] [normed_space ℝ E] (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (c : E) (f : Lp E 2 ΞΌ) : (βŸͺindicator_const_Lp 2 hs hΞΌs c, f⟫ : π•œ) = βŸͺc, ∫ x in s, f x βˆ‚ΞΌβŸ« := by rw [← integral_inner (integrable_on_Lp_of_measure_ne_top f fact_one_le_two_ennreal.elim hΞΌs), L2.inner_indicator_const_Lp_eq_set_integral_inner] variables {π•œ} /-- The inner product in `L2` of the indicator of a set `indicator_const_Lp 2 hs hΞΌs (1 : π•œ)` and a real or complex function `f` is equal to the integral of `f` over `s`. -/ lemma inner_indicator_const_Lp_one (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (f : Lp π•œ 2 ΞΌ) : βŸͺindicator_const_Lp 2 hs hΞΌs (1 : π•œ), f⟫ = ∫ x in s, f x βˆ‚ΞΌ := by { rw L2.inner_indicator_const_Lp_eq_inner_set_integral π•œ hs hΞΌs (1 : π•œ) f, simp, } end indicator_const_Lp end L2 section inner_continuous variables {Ξ± : Type*} [topological_space Ξ±] [measure_space Ξ±] [borel_space Ξ±] {π•œ : Type*} [is_R_or_C π•œ] variables (ΞΌ : measure Ξ±) [is_finite_measure ΞΌ] open_locale bounded_continuous_function complex_conjugate local notation `βŸͺ`x`, `y`⟫` := @inner π•œ (Ξ± β†’β‚‚[ΞΌ] π•œ) _ x y /-- For bounded continuous functions `f`, `g` on a finite-measure topological space `Ξ±`, the L^2 inner product is the integral of their pointwise inner product. -/ lemma bounded_continuous_function.inner_to_Lp (f g : Ξ± →ᡇ π•œ) : βŸͺbounded_continuous_function.to_Lp 2 ΞΌ π•œ f, bounded_continuous_function.to_Lp 2 ΞΌ π•œ g⟫ = ∫ x, conj (f x) * g x βˆ‚ΞΌ := begin apply integral_congr_ae, have hf_ae := f.coe_fn_to_Lp 2 ΞΌ π•œ, have hg_ae := g.coe_fn_to_Lp 2 ΞΌ π•œ, filter_upwards [hf_ae, hg_ae] with _ hf hg, rw [hf, hg], simp end variables [compact_space Ξ±] /-- For continuous functions `f`, `g` on a compact, finite-measure topological space `Ξ±`, the L^2 inner product is the integral of their pointwise inner product. -/ lemma continuous_map.inner_to_Lp (f g : C(Ξ±, π•œ)) : βŸͺcontinuous_map.to_Lp 2 ΞΌ π•œ f, continuous_map.to_Lp 2 ΞΌ π•œ g⟫ = ∫ x, conj (f x) * g x βˆ‚ΞΌ := begin apply integral_congr_ae, have hf_ae := f.coe_fn_to_Lp ΞΌ, have hg_ae := g.coe_fn_to_Lp ΞΌ, filter_upwards [hf_ae, hg_ae] with _ hf hg, rw [hf, hg], simp end end inner_continuous end measure_theory
2b1a27a4f7ed503bc04f193e21e05b045c6bbb1a
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/equiv/local_equiv.lean
401ae4a4fe1852f9db09f988ddbb66369dc1c343
[ "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
24,965
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 data.equiv.basic /-! # Local equivalences This files defines equivalences between subsets of given types. An element `e` of `local_equiv Ξ± Ξ²` is made of two maps `e.to_fun` and `e.inv_fun` respectively from Ξ± to Ξ² and from Ξ² to Ξ± (just like equivs), which are inverse to each other on the subsets `e.source` and `e.target` of respectively Ξ± and Ξ². They are designed in particular to define charts on manifolds. The main functionality is `e.trans f`, which composes the two local equivalences by restricting the source and target to the maximal set where the composition makes sense. As for equivs, we register a coercion to functions and use it in our simp normal form: we write `e x` and `e.symm y` instead of `e.to_fun x` and `e.inv_fun y`. ## Main definitions `equiv.to_local_equiv`: associating a local equiv to an equiv, with source = target = univ `local_equiv.symm` : the inverse of a local equiv `local_equiv.trans` : the composition of two local equivs `local_equiv.refl` : the identity local equiv `local_equiv.of_set` : the identity on a set `s` `eq_on_source` : equivalence relation describing the "right" notion of equality for local equivs (see below in implementation notes) ## Implementation notes There are at least three possible implementations of local equivalences: * equivs on subtypes * pairs of functions taking values in `option Ξ±` and `option Ξ²`, equal to none where the local equivalence is not defined * pairs of functions defined everywhere, keeping the source and target as additional data Each of these implementations has pros and cons. * When dealing with subtypes, one still need to define additional API for composition and restriction of domains. Checking that one always belongs to the right subtype makes things very tedious, and leads quickly to DTT hell (as the subtype `u ∩ v` is not the "same" as `v ∩ u`, for instance). * With option-valued functions, the composition is very neat (it is just the usual composition, and the domain is restricted automatically). These are implemented in `pequiv.lean`. For manifolds, where one wants to discuss thoroughly the smoothness of the maps, this creates however a lot of overhead as one would need to extend all classes of smoothness to option-valued maps. * The local_equiv version as explained above is easier to use for manifolds. The drawback is that there is extra useless data (the values of `to_fun` and `inv_fun` outside of `source` and `target`). In particular, the equality notion between local equivs is not "the right one", i.e., coinciding source and target and equality there. Moreover, there are no local equivs in this sense between an empty type and a nonempty type. Since empty types are not that useful, and since one almost never needs to talk about equal local equivs, this is not an issue in practice. Still, we introduce an equivalence relation `eq_on_source` that captures this right notion of equality, and show that many properties are invariant under this equivalence relation. -/ mk_simp_attribute mfld_simps "The simpset `mfld_simps` records several simp lemmas that are especially useful in manifolds. It is a subset of the whole set of simp lemmas, but it makes it possible to have quicker proofs (when used with `squeeze_simp` or `simp only`) while retaining readability. The typical use case is the following, in a file on manifolds: If `simp [foo, bar]` is slow, replace it with `squeeze_simp [foo, bar] with mfld_simps` and paste its output. The list of lemmas should be reasonable (contrary to the output of `squeeze_simp [foo, bar]` which might contain tens of lemmas), and the outcome should be quick enough. " -- register in the simpset `mfld_simps` several lemmas that are often useful when dealing -- with manifolds attribute [mfld_simps] id.def function.comp.left_id set.mem_set_of_eq set.image_eq_empty set.univ_inter set.preimage_univ set.prod_mk_mem_set_prod_eq and_true set.mem_univ set.mem_image_of_mem true_and set.mem_inter_eq set.mem_preimage function.comp_app set.inter_subset_left set.mem_prod set.range_id and_self set.mem_range_self eq_self_iff_true forall_const forall_true_iff set.inter_univ set.preimage_id function.comp.right_id not_false_iff and_imp set.prod_inter_prod set.univ_prod_univ true_or or_true prod.map_mk set.preimage_inter heq_iff_eq equiv.sigma_equiv_prod_apply equiv.sigma_equiv_prod_symm_apply subtype.coe_mk equiv.to_fun_as_coe equiv.inv_fun_as_coe namespace tactic.interactive /-- A very basic tactic to show that sets showing up in manifolds coincide or are included in one another. -/ meta def mfld_set_tac : tactic unit := do goal ← tactic.target, match goal with | `(%%e₁ = %%eβ‚‚) := `[ext my_y, split; { assume h_my_y, try { simp only [*, -h_my_y] with mfld_simps at h_my_y }, simp only [*] with mfld_simps }] | `(%%e₁ βŠ† %%eβ‚‚) := `[assume my_y h_my_y, try { simp only [*, -h_my_y] with mfld_simps at h_my_y }, simp only [*] with mfld_simps] | _ := tactic.fail "goal should be an equality or an inclusion" end end tactic.interactive open function set variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} {Ξ΄ : Type*} /-- Local equivalence between subsets `source` and `target` of Ξ± and Ξ² respectively. The (global) maps `to_fun : Ξ± β†’ Ξ²` and `inv_fun : Ξ² β†’ Ξ±` map `source` to `target` and conversely, and are inverse to each other there. The values of `to_fun` outside of `source` and of `inv_fun` outside of `target` are irrelevant. -/ @[nolint has_inhabited_instance] structure local_equiv (Ξ± : Type*) (Ξ² : Type*) := (to_fun : Ξ± β†’ Ξ²) (inv_fun : Ξ² β†’ Ξ±) (source : set Ξ±) (target : set Ξ²) (map_source' : βˆ€{x}, x ∈ source β†’ to_fun x ∈ target) (map_target' : βˆ€{x}, x ∈ target β†’ inv_fun x ∈ source) (left_inv' : βˆ€{x}, x ∈ source β†’ inv_fun (to_fun x) = x) (right_inv' : βˆ€{x}, x ∈ target β†’ to_fun (inv_fun x) = x) /-- Associating a local_equiv to an equiv-/ def equiv.to_local_equiv (e : equiv Ξ± Ξ²) : local_equiv Ξ± Ξ² := { to_fun := e.to_fun, inv_fun := e.inv_fun, source := univ, target := univ, map_source' := Ξ»x hx, mem_univ _, map_target' := Ξ»y hy, mem_univ _, left_inv' := Ξ»x hx, e.left_inv x, right_inv' := Ξ»x hx, e.right_inv x } namespace local_equiv variables (e : local_equiv Ξ± Ξ²) (e' : local_equiv Ξ² Ξ³) /-- The inverse of a local equiv -/ protected def symm : local_equiv Ξ² Ξ± := { to_fun := e.inv_fun, inv_fun := e.to_fun, source := e.target, target := e.source, map_source' := e.map_target', map_target' := e.map_source', left_inv' := e.right_inv', right_inv' := e.left_inv' } instance : has_coe_to_fun (local_equiv Ξ± Ξ²) := ⟨_, local_equiv.to_fun⟩ @[simp, mfld_simps] theorem coe_mk (f : Ξ± β†’ Ξ²) (g s t ml mr il ir) : (local_equiv.mk f g s t ml mr il ir : Ξ± β†’ Ξ²) = f := rfl @[simp, mfld_simps] theorem coe_symm_mk (f : Ξ± β†’ Ξ²) (g s t ml mr il ir) : ((local_equiv.mk f g s t ml mr il ir).symm : Ξ² β†’ Ξ±) = g := rfl @[simp, mfld_simps] lemma to_fun_as_coe : e.to_fun = e := rfl @[simp, mfld_simps] lemma inv_fun_as_coe : e.inv_fun = e.symm := rfl @[simp, mfld_simps] lemma map_source {x : Ξ±} (h : x ∈ e.source) : e x ∈ e.target := e.map_source' h protected lemma maps_to : maps_to e e.source e.target := Ξ» _, e.map_source @[simp, mfld_simps] lemma map_target {x : Ξ²} (h : x ∈ e.target) : e.symm x ∈ e.source := e.map_target' h lemma symm_maps_to : maps_to e.symm e.target e.source := e.symm.maps_to @[simp, mfld_simps] lemma left_inv {x : Ξ±} (h : x ∈ e.source) : e.symm (e x) = x := e.left_inv' h protected lemma left_inv_on : left_inv_on e.symm e e.source := Ξ» _, e.left_inv @[simp, mfld_simps] lemma right_inv {x : Ξ²} (h : x ∈ e.target) : e (e.symm x) = x := e.right_inv' h protected lemma right_inv_on : right_inv_on e.symm e e.target := Ξ» _, e.right_inv /-- Associating to a local_equiv an equiv between the source and the target -/ protected def to_equiv : equiv (e.source) (e.target) := { to_fun := Ξ» x, ⟨e x, e.map_source x.mem⟩, inv_fun := Ξ» y, ⟨e.symm y, e.map_target y.mem⟩, left_inv := λ⟨x, hx⟩, subtype.eq $ e.left_inv hx, right_inv := λ⟨y, hy⟩, subtype.eq $ e.right_inv hy } @[simp, mfld_simps] lemma symm_source : e.symm.source = e.target := rfl @[simp, mfld_simps] lemma symm_target : e.symm.target = e.source := rfl @[simp, mfld_simps] lemma symm_symm : e.symm.symm = e := by { cases e, refl } /-- A local equiv induces a bijection between its source and target -/ lemma bij_on_source : bij_on e e.source e.target := inv_on.bij_on ⟨e.left_inv_on, e.right_inv_on⟩ e.maps_to e.symm_maps_to lemma image_eq_target_inter_inv_preimage {s : set Ξ±} (h : s βŠ† e.source) : e '' s = e.target ∩ e.symm ⁻¹' s := begin refine subset.antisymm (Ξ»x hx, _) (Ξ»x hx, _), { rcases (mem_image _ _ _).1 hx with ⟨y, ys, hy⟩, rw ← hy, split, { apply e.map_source, exact h ys }, { rwa [mem_preimage, e.left_inv (h ys)] } }, { rw ← e.right_inv hx.1, exact mem_image_of_mem _ hx.2 } end lemma image_inter_source_eq (s : set Ξ±) : e '' (s ∩ e.source) = e.target ∩ e.symm ⁻¹' (s ∩ e.source) := e.image_eq_target_inter_inv_preimage (inter_subset_right _ _) lemma image_inter_source_eq' (s : set Ξ±) : e '' (s ∩ e.source) = e.target ∩ e.symm ⁻¹' s := begin rw e.image_eq_target_inter_inv_preimage (inter_subset_right _ _), ext x, split; { assume hx, simp at hx, simp [hx] } end lemma symm_image_eq_source_inter_preimage {s : set Ξ²} (h : s βŠ† e.target) : e.symm '' s = e.source ∩ e ⁻¹' s := e.symm.image_eq_target_inter_inv_preimage h lemma symm_image_inter_target_eq (s : set Ξ²) : e.symm '' (s ∩ e.target) = e.source ∩ e ⁻¹' (s ∩ e.target) := e.symm.image_inter_source_eq _ lemma symm_image_inter_target_eq' (s : set Ξ²) : e.symm '' (s ∩ e.target) = e.source ∩ e ⁻¹' s := e.symm.image_inter_source_eq' _ lemma source_inter_preimage_inv_preimage (s : set Ξ±) : e.source ∩ e ⁻¹' (e.symm ⁻¹' s) = e.source ∩ s := begin ext x, split, { rintros ⟨hx, xs⟩, simp only [mem_preimage, hx, e.left_inv, mem_preimage] at xs, exact ⟨hx, xs⟩ }, { rintros ⟨hx, xs⟩, simp [hx, xs] } end lemma target_inter_inv_preimage_preimage (s : set Ξ²) : e.target ∩ e.symm ⁻¹' (e ⁻¹' s) = e.target ∩ s := e.symm.source_inter_preimage_inv_preimage _ lemma image_source_eq_target : e '' e.source = e.target := e.bij_on_source.image_eq lemma source_subset_preimage_target : e.source βŠ† e ⁻¹' e.target := Ξ»x hx, e.map_source hx lemma inv_image_target_eq_source : e.symm '' e.target = e.source := e.symm.bij_on_source.image_eq lemma target_subset_preimage_source : e.target βŠ† e.symm ⁻¹' e.source := Ξ»x hx, e.map_target hx /-- Two local equivs that have the same `source`, same `to_fun` and same `inv_fun`, coincide. -/ @[ext] protected lemma ext {e e' : local_equiv Ξ± Ξ²} (h : βˆ€x, e x = e' x) (hsymm : βˆ€x, e.symm x = e'.symm x) (hs : e.source = e'.source) : e = e' := begin have A : (e : Ξ± β†’ Ξ²) = e', by { ext x, exact h x }, have B : (e.symm : Ξ² β†’ Ξ±) = e'.symm, by { ext x, exact hsymm x }, have I : e '' e.source = e.target := e.image_source_eq_target, have I' : e' '' e'.source = e'.target := e'.image_source_eq_target, rw [A, hs, I'] at I, cases e; cases e', simp * at * end /-- Restricting a local equivalence to e.source ∩ s -/ protected def restr (s : set Ξ±) : local_equiv Ξ± Ξ² := { to_fun := e, inv_fun := e.symm, source := e.source ∩ s, target := e.target ∩ e.symm⁻¹' s, map_source' := Ξ»x hx, begin simp only with mfld_simps at hx, simp only [hx] with mfld_simps, end, map_target' := Ξ»y hy, begin simp only with mfld_simps at hy, simp only [hy] with mfld_simps, end, left_inv' := Ξ»x hx, e.left_inv hx.1, right_inv' := Ξ»y hy, e.right_inv hy.1 } @[simp, mfld_simps] lemma restr_coe (s : set Ξ±) : (e.restr s : Ξ± β†’ Ξ²) = e := rfl @[simp, mfld_simps] lemma restr_coe_symm (s : set Ξ±) : ((e.restr s).symm : Ξ² β†’ Ξ±) = e.symm := rfl @[simp, mfld_simps] lemma restr_source (s : set Ξ±) : (e.restr s).source = e.source ∩ s := rfl @[simp, mfld_simps] lemma restr_target (s : set Ξ±) : (e.restr s).target = e.target ∩ e.symm ⁻¹' s := rfl lemma restr_eq_of_source_subset {e : local_equiv Ξ± Ξ²} {s : set Ξ±} (h : e.source βŠ† s) : e.restr s = e := local_equiv.ext (Ξ»_, rfl) (Ξ»_, rfl) (by simp [inter_eq_self_of_subset_left h]) @[simp, mfld_simps] lemma restr_univ {e : local_equiv Ξ± Ξ²} : e.restr univ = e := restr_eq_of_source_subset (subset_univ _) /-- The identity local equiv -/ protected def refl (Ξ± : Type*) : local_equiv Ξ± Ξ± := (equiv.refl Ξ±).to_local_equiv @[simp, mfld_simps] lemma refl_source : (local_equiv.refl Ξ±).source = univ := rfl @[simp, mfld_simps] lemma refl_target : (local_equiv.refl Ξ±).target = univ := rfl @[simp, mfld_simps] lemma refl_coe : (local_equiv.refl Ξ± : Ξ± β†’ Ξ±) = id := rfl @[simp, mfld_simps] lemma refl_symm : (local_equiv.refl Ξ±).symm = local_equiv.refl Ξ± := rfl @[simp, mfld_simps] lemma refl_restr_source (s : set Ξ±) : ((local_equiv.refl Ξ±).restr s).source = s := by simp @[simp, mfld_simps] lemma refl_restr_target (s : set Ξ±) : ((local_equiv.refl Ξ±).restr s).target = s := by { change univ ∩ id⁻¹' s = s, simp } /-- The identity local equiv on a set `s` -/ def of_set (s : set Ξ±) : local_equiv Ξ± Ξ± := { to_fun := id, inv_fun := id, source := s, target := s, map_source' := Ξ»x hx, hx, map_target' := Ξ»x hx, hx, left_inv' := Ξ»x hx, rfl, right_inv' := Ξ»x hx, rfl } @[simp, mfld_simps] lemma of_set_source (s : set Ξ±) : (local_equiv.of_set s).source = s := rfl @[simp, mfld_simps] lemma of_set_target (s : set Ξ±) : (local_equiv.of_set s).target = s := rfl @[simp, mfld_simps] lemma of_set_coe (s : set Ξ±) : (local_equiv.of_set s : Ξ± β†’ Ξ±) = id := rfl @[simp, mfld_simps] lemma of_set_symm (s : set Ξ±) : (local_equiv.of_set s).symm = local_equiv.of_set s := rfl /-- Composing two local equivs if the target of the first coincides with the source of the second. -/ protected def trans' (e' : local_equiv Ξ² Ξ³) (h : e.target = e'.source) : local_equiv Ξ± Ξ³ := { to_fun := e' ∘ e, inv_fun := e.symm ∘ e'.symm, source := e.source, target := e'.target, map_source' := Ξ»x hx, by simp [h.symm, hx], map_target' := Ξ»y hy, by simp [h, hy], left_inv' := Ξ»x hx, by simp [hx, h.symm], right_inv' := Ξ»y hy, by simp [hy, h] } /-- Composing two local equivs, by restricting to the maximal domain where their composition is well defined. -/ protected def trans : local_equiv Ξ± Ξ³ := local_equiv.trans' (e.symm.restr (e'.source)).symm (e'.restr (e.target)) (inter_comm _ _) @[simp, mfld_simps] lemma coe_trans : (e.trans e' : Ξ± β†’ Ξ³) = e' ∘ e := rfl @[simp, mfld_simps] lemma coe_trans_symm : ((e.trans e').symm : Ξ³ β†’ Ξ±) = e.symm ∘ e'.symm := rfl lemma trans_symm_eq_symm_trans_symm : (e.trans e').symm = e'.symm.trans e.symm := by cases e; cases e'; refl @[simp, mfld_simps] lemma trans_source : (e.trans e').source = e.source ∩ e ⁻¹' e'.source := rfl lemma trans_source' : (e.trans e').source = e.source ∩ e ⁻¹' (e.target ∩ e'.source) := by mfld_set_tac lemma trans_source'' : (e.trans e').source = e.symm '' (e.target ∩ e'.source) := by rw [e.trans_source', inter_comm e.target, e.symm_image_inter_target_eq] lemma image_trans_source : e '' (e.trans e').source = e.target ∩ e'.source := image_source_eq_target (local_equiv.symm (local_equiv.restr (local_equiv.symm e) (e'.source))) @[simp, mfld_simps] lemma trans_target : (e.trans e').target = e'.target ∩ e'.symm ⁻¹' e.target := rfl lemma trans_target' : (e.trans e').target = e'.target ∩ e'.symm ⁻¹' (e'.source ∩ e.target) := trans_source' e'.symm e.symm lemma trans_target'' : (e.trans e').target = e' '' (e'.source ∩ e.target) := trans_source'' e'.symm e.symm lemma inv_image_trans_target : e'.symm '' (e.trans e').target = e'.source ∩ e.target := image_trans_source e'.symm e.symm lemma trans_assoc (e'' : local_equiv Ξ³ Ξ΄) : (e.trans e').trans e'' = e.trans (e'.trans e'') := local_equiv.ext (Ξ»x, rfl) (Ξ»x, rfl) (by simp [trans_source, @preimage_comp Ξ± Ξ² Ξ³, inter_assoc]) @[simp, mfld_simps] lemma trans_refl : e.trans (local_equiv.refl Ξ²) = e := local_equiv.ext (Ξ»x, rfl) (Ξ»x, rfl) (by simp [trans_source]) @[simp, mfld_simps] lemma refl_trans : (local_equiv.refl Ξ±).trans e = e := local_equiv.ext (Ξ»x, rfl) (Ξ»x, rfl) (by simp [trans_source, preimage_id]) lemma trans_refl_restr (s : set Ξ²) : e.trans ((local_equiv.refl Ξ²).restr s) = e.restr (e ⁻¹' s) := local_equiv.ext (Ξ»x, rfl) (Ξ»x, rfl) (by simp [trans_source]) lemma trans_refl_restr' (s : set Ξ²) : e.trans ((local_equiv.refl Ξ²).restr s) = e.restr (e.source ∩ e ⁻¹' s) := local_equiv.ext (Ξ»x, rfl) (Ξ»x, rfl) $ by { simp [trans_source], rw [← inter_assoc, inter_self] } lemma restr_trans (s : set Ξ±) : (e.restr s).trans e' = (e.trans e').restr s := local_equiv.ext (Ξ»x, rfl) (Ξ»x, rfl) $ by { simp [trans_source, inter_comm], rwa inter_assoc } /-- `eq_on_source e e'` means that `e` and `e'` have the same source, and coincide there. Then `e` and `e'` should really be considered the same local equiv. -/ def eq_on_source (e e' : local_equiv Ξ± Ξ²) : Prop := e.source = e'.source ∧ (e.source.eq_on e e') /-- `eq_on_source` is an equivalence relation -/ instance eq_on_source_setoid : setoid (local_equiv Ξ± Ξ²) := { r := eq_on_source, iseqv := ⟨ Ξ»e, by simp [eq_on_source], Ξ»e e' h, by { simp [eq_on_source, h.1.symm], exact Ξ»x hx, (h.2 hx).symm }, Ξ»e e' e'' h h', ⟨by rwa [← h'.1, ← h.1], Ξ»x hx, by { rw [← h'.2, h.2 hx], rwa ← h.1 }⟩⟩ } lemma eq_on_source_refl : e β‰ˆ e := setoid.refl _ /-- Two equivalent local equivs have the same source -/ lemma eq_on_source.source_eq {e e' : local_equiv Ξ± Ξ²} (h : e β‰ˆ e') : e.source = e'.source := h.1 /-- Two equivalent local equivs coincide on the source -/ lemma eq_on_source.eq_on {e e' : local_equiv Ξ± Ξ²} (h : e β‰ˆ e') : e.source.eq_on e e' := h.2 /-- Two equivalent local equivs have the same target -/ lemma eq_on_source.target_eq {e e' : local_equiv Ξ± Ξ²} (h : e β‰ˆ e') : e.target = e'.target := by simp only [← image_source_eq_target, ← h.source_eq, h.2.image_eq] /-- If two local equivs are equivalent, so are their inverses. -/ lemma eq_on_source.symm' {e e' : local_equiv Ξ± Ξ²} (h : e β‰ˆ e') : e.symm β‰ˆ e'.symm := begin refine ⟨h.target_eq, eq_on_of_left_inv_on_of_right_inv_on e.left_inv_on _ _⟩; simp only [symm_source, h.target_eq, h.source_eq, e'.symm_maps_to], exact e'.right_inv_on.congr_right e'.symm_maps_to (h.source_eq β–Έ h.eq_on.symm), end /-- Two equivalent local equivs have coinciding inverses on the target -/ lemma eq_on_source.symm_eq_on {e e' : local_equiv Ξ± Ξ²} (h : e β‰ˆ e') : eq_on e.symm e'.symm e.target := h.symm'.eq_on /-- Composition of local equivs respects equivalence -/ lemma eq_on_source.trans' {e e' : local_equiv Ξ± Ξ²} {f f' : local_equiv Ξ² Ξ³} (he : e β‰ˆ e') (hf : f β‰ˆ f') : e.trans f β‰ˆ e'.trans f' := begin split, { rw [trans_source'', trans_source'', ← he.target_eq, ← hf.1], exact (he.symm'.eq_on.mono $ inter_subset_left _ _).image_eq }, { assume x hx, rw trans_source at hx, simp [(he.2 hx.1).symm, hf.2 hx.2] } end /-- Restriction of local equivs respects equivalence -/ lemma eq_on_source.restr {e e' : local_equiv Ξ± Ξ²} (he : e β‰ˆ e') (s : set Ξ±) : e.restr s β‰ˆ e'.restr s := begin split, { simp [he.1] }, { assume x hx, simp only [mem_inter_eq, restr_source] at hx, exact he.2 hx.1 } end /-- Preimages are respected by equivalence -/ lemma eq_on_source.source_inter_preimage_eq {e e' : local_equiv Ξ± Ξ²} (he : e β‰ˆ e') (s : set Ξ²) : e.source ∩ e ⁻¹' s = e'.source ∩ e' ⁻¹' s := begin ext x, simp only [mem_inter_eq, mem_preimage], split, { assume hx, rwa [← he.2 hx.1, ← he.source_eq] }, { assume hx, rwa [← (setoid.symm he).2 hx.1, he.source_eq] } end /-- Composition of a local equiv and its inverse is equivalent to the restriction of the identity to the source -/ lemma trans_self_symm : e.trans e.symm β‰ˆ local_equiv.of_set e.source := begin have A : (e.trans e.symm).source = e.source, by mfld_set_tac, refine ⟨by simp [A], Ξ»x hx, _⟩, rw A at hx, simp only [hx] with mfld_simps end /-- Composition of the inverse of a local equiv and this local equiv is equivalent to the restriction of the identity to the target -/ lemma trans_symm_self : e.symm.trans e β‰ˆ local_equiv.of_set e.target := trans_self_symm (e.symm) /-- Two equivalent local equivs are equal when the source and target are univ -/ lemma eq_of_eq_on_source_univ (e e' : local_equiv Ξ± Ξ²) (h : e β‰ˆ e') (s : e.source = univ) (t : e.target = univ) : e = e' := begin apply local_equiv.ext (Ξ»x, _) (Ξ»x, _) h.1, { apply h.2, rw s, exact mem_univ _ }, { apply h.symm'.2, rw [symm_source, t], exact mem_univ _ } end section prod /-- The product of two local equivs, as a local equiv on the product. -/ def prod (e : local_equiv Ξ± Ξ²) (e' : local_equiv Ξ³ Ξ΄) : local_equiv (Ξ± Γ— Ξ³) (Ξ² Γ— Ξ΄) := { source := set.prod e.source e'.source, target := set.prod e.target e'.target, to_fun := Ξ»p, (e p.1, e' p.2), inv_fun := Ξ»p, (e.symm p.1, e'.symm p.2), map_source' := Ξ»p hp, by { simp at hp, simp [hp] }, map_target' := Ξ»p hp, by { simp at hp, simp [map_target, hp] }, left_inv' := Ξ»p hp, by { simp at hp, simp [hp] }, right_inv' := Ξ»p hp, by { simp at hp, simp [hp] } } @[simp, mfld_simps] lemma prod_source (e : local_equiv Ξ± Ξ²) (e' : local_equiv Ξ³ Ξ΄) : (e.prod e').source = set.prod e.source e'.source := rfl @[simp, mfld_simps] lemma prod_target (e : local_equiv Ξ± Ξ²) (e' : local_equiv Ξ³ Ξ΄) : (e.prod e').target = set.prod e.target e'.target := rfl @[simp, mfld_simps] lemma prod_coe (e : local_equiv Ξ± Ξ²) (e' : local_equiv Ξ³ Ξ΄) : ((e.prod e') : Ξ± Γ— Ξ³ β†’ Ξ² Γ— Ξ΄) = (Ξ»p, (e p.1, e' p.2)) := rfl lemma prod_coe_symm (e : local_equiv Ξ± Ξ²) (e' : local_equiv Ξ³ Ξ΄) : ((e.prod e').symm : Ξ² Γ— Ξ΄ β†’ Ξ± Γ— Ξ³) = (Ξ»p, (e.symm p.1, e'.symm p.2)) := rfl @[simp, mfld_simps] lemma prod_symm (e : local_equiv Ξ± Ξ²) (e' : local_equiv Ξ³ Ξ΄) : (e.prod e').symm = (e.symm.prod e'.symm) := by ext x; simp [prod_coe_symm] @[simp, mfld_simps] lemma prod_trans {Ξ· : Type*} {Ξ΅ : Type*} (e : local_equiv Ξ± Ξ²) (f : local_equiv Ξ² Ξ³) (e' : local_equiv Ξ΄ Ξ·) (f' : local_equiv Ξ· Ξ΅) : (e.prod e').trans (f.prod f') = (e.trans f).prod (e'.trans f') := by ext x; simp [ext_iff]; tauto end prod end local_equiv namespace set -- All arguments are explicit to avoid missing information in the pretty printer output /-- A bijection between two sets `s : set Ξ±` and `t : set Ξ²` provides a local equivalence between `Ξ±` and `Ξ²`. -/ @[simps] noncomputable def bij_on.to_local_equiv [nonempty Ξ±] (f : Ξ± β†’ Ξ²) (s : set Ξ±) (t : set Ξ²) (hf : bij_on f s t) : local_equiv Ξ± Ξ² := { to_fun := f, inv_fun := inv_fun_on f s, source := s, target := t, map_source' := hf.maps_to, map_target' := hf.surj_on.maps_to_inv_fun_on, left_inv' := hf.inv_on_inv_fun_on.1, right_inv' := hf.inv_on_inv_fun_on.2 } /-- A map injective on a subset of its domain provides a local equivalence. -/ @[simp, mfld_simps] noncomputable def inj_on.to_local_equiv [nonempty Ξ±] (f : Ξ± β†’ Ξ²) (s : set Ξ±) (hf : inj_on f s) : local_equiv Ξ± Ξ² := hf.bij_on_image.to_local_equiv f s (f '' s) end set namespace equiv /- equivs give rise to local_equiv. We set up simp lemmas to reduce most properties of the local equiv to that of the equiv. -/ variables (e : equiv Ξ± Ξ²) (e' : equiv Ξ² Ξ³) @[simp, mfld_simps] lemma to_local_equiv_coe : (e.to_local_equiv : Ξ± β†’ Ξ²) = e := rfl @[simp, mfld_simps] lemma to_local_equiv_symm_coe : (e.to_local_equiv.symm : Ξ² β†’ Ξ±) = e.symm := rfl @[simp, mfld_simps] lemma to_local_equiv_source : e.to_local_equiv.source = univ := rfl @[simp, mfld_simps] lemma to_local_equiv_target : e.to_local_equiv.target = univ := rfl @[simp, mfld_simps] lemma refl_to_local_equiv : (equiv.refl Ξ±).to_local_equiv = local_equiv.refl Ξ± := rfl @[simp, mfld_simps] lemma symm_to_local_equiv : e.symm.to_local_equiv = e.to_local_equiv.symm := rfl @[simp, mfld_simps] lemma trans_to_local_equiv : (e.trans e').to_local_equiv = e.to_local_equiv.trans e'.to_local_equiv := local_equiv.ext (Ξ»x, rfl) (Ξ»x, rfl) (by simp [local_equiv.trans_source, equiv.to_local_equiv]) end equiv
79cb9438d13b7a26bf6a7da3dd79909860fc19fc
be30445afb85fcb2041b437059d21f9a84dff894
/reflexive_graphs.hlean
ec8fb8d1b90e311237b676d9c3c4e219e83badde
[ "Apache-2.0" ]
permissive
EgbertRijke/GraphModel
784efde97299c4f3cb1760369e8a260e02caafd5
2a473880764093dd151554a913292ed465e80e62
refs/heads/master
1,609,477,731,119
1,476,299,056,000
1,476,299,056,000
58,213,801
6
0
null
null
null
null
UTF-8
Lean
false
false
368
hlean
import graphmodel namespace rgraph structure ctx extends graph.ctx := ( rfx : Ξ  (i : pts), edg i i) structure fam (Ξ“ : ctx) extends graph.fam Ξ“ := ( rfx : Ξ  (i : ctx.pts Ξ“) (x : pts i), edg (ctx.rfx Ξ“ i) x x) structure tm {Ξ“ : ctx} (A : fam Ξ“) extends graph.tm A := ( rfx : Ξ  (i : ctx.pts Ξ“), fam.edg A (ctx.rfx Ξ“ i) (pts i) (pts i)) end rgraph
3c4cb4932492375cf3279dbbab22efa93da1ea9d
2731214ea32f2a1a985300e281fb3117640a16c3
/portmanteau_topological_lemmas.lean
90c057f79b2dad5bf9c9c4474306bc81f9962cd3
[ "Apache-2.0" ]
permissive
kkytola/lean_portmanteau
5d6a156db959974ebc4f5bed9118a7a2438a33fa
ac55eb4e24be43032cbc082e2b68d8fb8bd63f22
refs/heads/main
1,686,107,117,334
1,625,177,052,000
1,625,177,052,000
381,514,032
1
0
null
null
null
null
UTF-8
Lean
false
false
3,403
lean
/- Copyright (c) 2021 Kalle KytΓΆlΓ€. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kalle KytΓΆlΓ€ -/ import tactic import measure_theory.measurable_space import measure_theory.borel_space noncomputable theory open set open measure_theory open measurable_space open borel_space namespace portmanteau section portmanteau_topological_lemmas variables {Ξ± : Type*} [topological_space Ξ±] lemma meas_eq_various_of_null_bdry (ΞΌ : @measure_theory.measure Ξ± (borel Ξ±)) (E : set Ξ±) : (ΞΌ(frontier E) = 0) β†’ (ΞΌ(interior E) = ΞΌ(E)) ∧ (ΞΌ(closure E) = ΞΌ(E)) := begin intro hEnullbdry , have ineq_E_le_Ecl := @measure_mono Ξ± (borel(Ξ±)) ΞΌ E (closure E) subset_closure , have ineq_Eint_le_E := @measure_mono Ξ± (borel(Ξ±)) ΞΌ (interior E) E interior_subset , have surpr := @measure_union_le Ξ± (borel(Ξ±)) ΞΌ (interior E) (frontier E) , rw ← closure_eq_interior_union_frontier at surpr , simp [hEnullbdry] at surpr , have mono := @measure_mono Ξ± (borel(Ξ±)) ΞΌ (interior E) (closure E) interior_subset_closure , have key := le_antisymm mono surpr , split , { apply le_antisymm , { assumption , } , rw key , assumption , } , { apply le_antisymm , { rw ← key , assumption , } , assumption , } , end lemma open_imp_borel {Ξ³ : Type*} [top_Ξ³ : topological_space Ξ³] {G : set Ξ³} : is_open G β†’ (borel Ξ³).measurable_set' G := measurable_set_generate_from lemma closed_imp_borel {Ξ³ : Type*} [top_Ξ³ : topological_space Ξ³] {F : set Ξ³} : is_closed F β†’ (borel Ξ³).measurable_set' F := begin set G := Fᢜ with hG , have FeqGc := compl_compl F , rw ← hG at FeqGc , intro hFclosed , have Gmble := open_imp_borel (is_open_compl_iff.mpr hFclosed) , rw ← FeqGc , exact (borel Ξ³).measurable_set_compl G Gmble , end lemma compl_frontier {Ξ³ : Type*} [topological_space Ξ³] (A : set Ξ³) : (frontier A)ᢜ = (interior A) βˆͺ (interior (Aᢜ)) := begin have fact := @closure_eq_compl_interior_compl Ξ³ _ Aᢜ , rw compl_compl at fact , suffices : frontier A = (interior A)ᢜ ∩ (interior (Aᢜ))ᢜ , { rw this , rw compl_inter (interior A)ᢜ (interior (Aᢜ))ᢜ , repeat {rw compl_compl ,} , } , rw [←frontier_compl A , ←fact] , exact diff_eq (closure (Aᢜ)) (interior (Aᢜ)) , end lemma interior_preimage {Ξ² Ξ³ : Type*} [topological_space Ξ²] [topological_space Ξ³] (G : set Ξ³) (f : Ξ² β†’ Ξ³) (hf : continuous f) : f⁻¹' (interior G) βŠ† interior (f⁻¹'(G)) := begin apply interior_maximal , { have Gint_ss_G : (interior G) βŠ† G := interior_subset , exact preimage_mono Gint_ss_G , } , { have interior_open : is_open (interior G) := is_open_interior , exact continuous_def.mp hf (interior G) interior_open , } , end lemma frontier_preimage {Ξ² Ξ³ : Type*} [topological_space Ξ²] [topological_space Ξ³] (A : set Ξ³) (f : Ξ² β†’ Ξ³) (hf : continuous f) : frontier (f⁻¹'(A)) βŠ† f⁻¹'(frontier A) := begin apply (@compl_subset_compl Ξ² (f⁻¹'(frontier A)) (frontier (f⁻¹'(A)))).mp, rw [compl_frontier _ , ←preimage_compl , compl_frontier _ , preimage_union ] , have one := @interior_preimage Ξ² Ξ³ _ _ A f hf , have two := @interior_preimage Ξ² Ξ³ _ _ (Aᢜ) f hf , exact union_subset_union one two , end end portmanteau_topological_lemmas end portmanteau
0d3458b0517ae0c91508a6c9fa9a5d7a3e38d76c
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/nat/with_bot.lean
c33918ac8ada0a80bcfc8f3ef5472fa7ead1c24f
[ "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,185
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.nat.basic import algebra.order.group /-! # `with_bot β„•` Lemmas about the type of natural numbers with a bottom element adjoined. -/ namespace nat lemma with_bot.add_eq_zero_iff : βˆ€ {n m : with_bot β„•}, n + m = 0 ↔ n = 0 ∧ m = 0 | none m := iff_of_false dec_trivial (Ξ» h, absurd h.1 dec_trivial) | n none := iff_of_false (by cases n; exact dec_trivial) (Ξ» h, absurd h.2 dec_trivial) | (some n) (some m) := show (n + m : with_bot β„•) = (0 : β„•) ↔ (n : with_bot β„•) = (0 : β„•) ∧ (m : with_bot β„•) = (0 : β„•), by rw [← with_bot.coe_add, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, add_eq_zero_iff' (nat.zero_le _) (nat.zero_le _)] lemma with_bot.add_eq_one_iff : βˆ€ {n m : with_bot β„•}, n + m = 1 ↔ (n = 0 ∧ m = 1) ∨ (n = 1 ∧ m = 0) | none none := dec_trivial | none (some m) := dec_trivial | (some n) none := iff_of_false dec_trivial (Ξ» h, h.elim (Ξ» h, absurd h.2 dec_trivial) (Ξ» h, absurd h.2 dec_trivial)) | (some n) (some 0) := by erw [with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe]; simp | (some n) (some (m + 1)) := by erw [with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe]; simp [nat.add_succ, nat.succ_inj', nat.succ_ne_zero] @[simp] lemma with_bot.coe_nonneg {n : β„•} : 0 ≀ (n : with_bot β„•) := by rw [← with_bot.coe_zero, with_bot.coe_le_coe]; exact nat.zero_le _ @[simp] lemma with_bot.lt_zero_iff (n : with_bot β„•) : n < 0 ↔ n = βŠ₯ := option.cases_on n dec_trivial (Ξ» n, iff_of_false (by simp [with_bot.some_eq_coe]) (Ξ» h, option.no_confusion h)) lemma with_bot.one_le_iff_zero_lt {x : with_bot β„•} : 1 ≀ x ↔ 0 < x := begin refine ⟨λ h, lt_of_lt_of_le (with_bot.coe_lt_coe.mpr zero_lt_one) h, Ξ» h, _⟩, induction x using with_bot.rec_bot_coe, { exact (not_lt_bot h).elim }, { exact with_bot.coe_le_coe.mpr (nat.succ_le_iff.mpr (with_bot.coe_lt_coe.mp h)) } end end nat
e94c45f5aa42d0162a06c5882cad6fd1eecf038c
1b8f093752ba748c5ca0083afef2959aaa7dace5
/src/category_theory/universal/continuous.lean
fab59ab7c80b5552d47b8ddd68411979bc5c8b54
[]
no_license
khoek/lean-category-theory
7ec4cda9cc64a5a4ffeb84712ac7d020dbbba386
63dcb598e9270a3e8b56d1769eb4f825a177cd95
refs/heads/master
1,585,251,725,759
1,539,344,445,000
1,539,344,445,000
145,281,070
0
0
null
1,534,662,376,000
1,534,662,376,000
null
UTF-8
Lean
false
false
1,442
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison import category_theory.limits.limits open category_theory.limits namespace category_theory universes u v variables {C : Type u} [π’ž : category.{u v} C] {D : Type u} [π’Ÿ : category.{u v} D] section include π’ž π’Ÿ class continuous (F : C β₯€ D) := (preserves_limits : βˆ€ {J : Type v} [small_category J] (G : J β₯€ C) (c : cone G) (L : is_limit c), is_limit ((cones.functoriality G F) c)) class cocontinuous (F : C β₯€ D) := (preserves_colimits : βˆ€ {J : Type v} [small_category J] (G : J β₯€ C) (c : cocone G) (L : is_colimit c), is_colimit ((cocones.functoriality G F) c)) end section include π’ž instance : continuous (functor.id C) := { preserves_limits := Ξ» J π’₯ G c L, begin resetI, exact { lift := Ξ» s, @is_limit.lift _ _ _ _ _ c L { X := s.X, Ο€ := s.Ο€ }, -- We need to do a little work here because `G β‹™ (functor.id _) β‰  G`. uniq' := Ξ» s m w, @is_limit.uniq _ _ _ _ _ c L { X := s.X, Ο€ := s.Ο€ } m w, } end } end -- instance HomFunctorPreservesLimits (a : A) : preserves_limits ((coyoneda A) a) := { -- preserves := Ξ» I D q, sorry -- } -- instance RepresentableFunctorPreservesLimits (F : A β₯€ (Type u)) [representable F] : preserves_limits F := sorry -- PROJECT right adjoints are continuous end category_theory
853de1b0a11f3a1701b5fd7af2d16030be32a8ac
b561a44b48979a98df50ade0789a21c79ee31288
/stage0/src/Lean/Elab/Util.lean
d3be2bbbda83e40611ce3f378a245af790caa82b
[ "Apache-2.0" ]
permissive
3401ijk/lean4
97659c475ebd33a034fed515cb83a85f75ccfb06
a5b1b8de4f4b038ff752b9e607b721f15a9a4351
refs/heads/master
1,693,933,007,651
1,636,424,845,000
1,636,424,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,766
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.Trace import Lean.Parser.Syntax import Lean.Parser.Extension import Lean.KeyedDeclsAttribute import Lean.Elab.Exception namespace Lean def Syntax.prettyPrint (stx : Syntax) : Format := match stx.unsetTrailing.reprint with -- TODO use syntax pretty printer | some str => format str.toFormat | none => format stx def MacroScopesView.format (view : MacroScopesView) (mainModule : Name) : Format := Std.format $ if view.scopes.isEmpty then view.name else if view.mainModule == mainModule then view.scopes.foldl Name.mkNum (view.name ++ view.imported) else view.scopes.foldl Name.mkNum (view.name ++ view.imported ++ view.mainModule) namespace Elab def expandOptNamedPrio (stx : Syntax) : MacroM Nat := if stx.isNone then return eval_prio default else match stx[0] with | `(Parser.Command.namedPrio| (priority := $prio)) => evalPrio prio | _ => Macro.throwUnsupported structure MacroStackElem where before : Syntax after : Syntax abbrev MacroStack := List MacroStackElem /- If `ref` does not have position information, then try to use macroStack -/ def getBetterRef (ref : Syntax) (macroStack : MacroStack) : Syntax := match ref.getPos? with | some _ => ref | none => match macroStack.find? (Β·.before.getPos? != none) with | some elem => elem.before | none => ref register_builtin_option pp.macroStack : Bool := { defValue := false group := "pp" descr := "dispaly macro expansion stack" } def addMacroStack {m} [Monad m] [MonadOptions m] (msgData : MessageData) (macroStack : MacroStack) : m MessageData := do if !pp.macroStack.get (← getOptions) then pure msgData else match macroStack with | [] => pure msgData | stack@(top::_) => let msgData := msgData ++ Format.line ++ "with resulting expansion" ++ indentD top.after pure $ stack.foldl (fun (msgData : MessageData) (elem : MacroStackElem) => msgData ++ Format.line ++ "while expanding" ++ indentD elem.before) msgData def checkSyntaxNodeKind [Monad m] [MonadEnv m] [MonadError m] (k : Name) : m Name := do if Parser.isValidSyntaxNodeKind (← getEnv) k then pure k else throwError "failed" def checkSyntaxNodeKindAtNamespaces [Monad m] [MonadEnv m] [MonadError m] (k : Name) : Name β†’ m Name | n@(Name.str p _ _) => checkSyntaxNodeKind (n ++ k) <|> checkSyntaxNodeKindAtNamespaces k p | Name.anonymous => checkSyntaxNodeKind k | _ => throwError "failed" def checkSyntaxNodeKindAtCurrentNamespaces (k : Name) : AttrM Name := do let ctx ← read checkSyntaxNodeKindAtNamespaces k ctx.currNamespace def syntaxNodeKindOfAttrParam (defaultParserNamespace : Name) (stx : Syntax) : AttrM SyntaxNodeKind := do let k ← Attribute.Builtin.getId stx checkSyntaxNodeKindAtCurrentNamespaces k <|> checkSyntaxNodeKind (defaultParserNamespace ++ k) <|> throwError "invalid syntax node kind '{k}'" private unsafe def evalSyntaxConstantUnsafe (env : Environment) (opts : Options) (constName : Name) : ExceptT String Id Syntax := env.evalConstCheck Syntax opts `Lean.Syntax constName @[implementedBy evalSyntaxConstantUnsafe] constant evalSyntaxConstant (env : Environment) (opts : Options) (constName : Name) : ExceptT String Id Syntax := throw "" unsafe def mkElabAttribute (Ξ³) (attrDeclName attrBuiltinName attrName : Name) (parserNamespace : Name) (typeName : Name) (kind : String) : IO (KeyedDeclsAttribute Ξ³) := KeyedDeclsAttribute.init { builtinName := attrBuiltinName name := attrName descr := kind ++ " elaborator" valueTypeName := typeName evalKey := fun _ stx => syntaxNodeKindOfAttrParam parserNamespace stx } attrDeclName unsafe def mkMacroAttributeUnsafe : IO (KeyedDeclsAttribute Macro) := mkElabAttribute Macro `Lean.Elab.macroAttribute `builtinMacro `macro Name.anonymous `Lean.Macro "macro" @[implementedBy mkMacroAttributeUnsafe] constant mkMacroAttribute : IO (KeyedDeclsAttribute Macro) builtin_initialize macroAttribute : KeyedDeclsAttribute Macro ← mkMacroAttribute /-- Try to expand macro at syntax tree root and return macro declaration name and new syntax if successful. Return none if all macros threw `Macro.Exception.unsupportedSyntax`. -/ def expandMacroImpl? (env : Environment) : Syntax β†’ MacroM (Option (Name Γ— Syntax)) := fun stx => do for e in macroAttribute.getEntries env stx.getKind do try let stx' ← withFreshMacroScope (e.value stx) return (e.declName, stx') catch | Macro.Exception.unsupportedSyntax => pure () | ex => throw ex return none class MonadMacroAdapter (m : Type β†’ Type) where getCurrMacroScope : m MacroScope getNextMacroScope : m MacroScope setNextMacroScope : MacroScope β†’ m Unit instance (m n) [MonadLift m n] [MonadMacroAdapter m] : MonadMacroAdapter n := { getCurrMacroScope := liftM (MonadMacroAdapter.getCurrMacroScope : m _), getNextMacroScope := liftM (MonadMacroAdapter.getNextMacroScope : m _), setNextMacroScope := fun s => liftM (MonadMacroAdapter.setNextMacroScope s : m _) } def liftMacroM {Ξ±} {m : Type β†’ Type} [Monad m] [MonadMacroAdapter m] [MonadEnv m] [MonadRecDepth m] [MonadError m] [MonadResolveName m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] (x : MacroM Ξ±) : m Ξ± := do let env ← getEnv let currNamespace ← getCurrNamespace let openDecls ← getOpenDecls let methods := Macro.mkMethods { -- TODO: record recursive expansions in info tree? expandMacro? := fun stx => do match (← expandMacroImpl? env stx) with | some (_, stx) => some stx | none => none hasDecl := fun declName => return env.contains declName getCurrNamespace := return currNamespace resolveNamespace? := fun n => return ResolveName.resolveNamespace? env currNamespace openDecls n resolveGlobalName := fun n => return ResolveName.resolveGlobalName env currNamespace openDecls n } match x { methods := methods ref := ← getRef currMacroScope := ← MonadMacroAdapter.getCurrMacroScope mainModule := env.mainModule currRecDepth := ← MonadRecDepth.getRecDepth maxRecDepth := ← MonadRecDepth.getMaxRecDepth } { macroScope := (← MonadMacroAdapter.getNextMacroScope) } with | EStateM.Result.error Macro.Exception.unsupportedSyntax _ => throwUnsupportedSyntax | EStateM.Result.error (Macro.Exception.error ref msg) _ => throwErrorAt ref msg | EStateM.Result.ok a s => MonadMacroAdapter.setNextMacroScope s.macroScope s.traceMsgs.reverse.forM fun (clsName, msg) => trace clsName fun _ => msg pure a @[inline] def adaptMacro {m : Type β†’ Type} [Monad m] [MonadMacroAdapter m] [MonadEnv m] [MonadRecDepth m] [MonadError m] [MonadResolveName m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] (x : Macro) (stx : Syntax) : m Syntax := liftMacroM (x stx) partial def mkUnusedBaseName (baseName : Name) : MacroM Name := do let currNamespace ← Macro.getCurrNamespace if ← Macro.hasDecl (currNamespace ++ baseName) then let rec loop (idx : Nat) := do let name := baseName.appendIndexAfter idx if ← Macro.hasDecl (currNamespace ++ name) then loop (idx+1) else name loop 1 else return baseName builtin_initialize registerTraceClass `Elab registerTraceClass `Elab.step end Lean.Elab
6ab75c0e62b771b22ec06196f222c8500a91a125
e151e9053bfd6d71740066474fc500a087837323
/src/hott/types/prod.lean
10e11e876c74181f0862f7cdde92586542b8878f
[ "Apache-2.0" ]
permissive
daniel-carranza/hott3
15bac2d90589dbb952ef15e74b2837722491963d
913811e8a1371d3a5751d7d32ff9dec8aa6815d9
refs/heads/master
1,610,091,349,670
1,596,222,336,000
1,596,222,336,000
241,957,822
0
0
Apache-2.0
1,582,222,839,000
1,582,222,838,000
null
UTF-8
Lean
false
false
12,647
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Jakob von Raumer Ported from Coq HoTT Theorems about products -/ import ..init universes u v w hott_theory namespace hott open hott.eq hott.equiv hott.is_equiv hott.is_trunc prod unit variables {A : Type _} {A': Type _} {B : Type _} {B' : Type _} {C : Type _} {D : Type _} {P : A β†’ Type _} {Q : A β†’ Type _} {a a' a'' : A} {b b₁ bβ‚‚ b' b'' : B} {u v w : A Γ— B} namespace prod /- Paths in a product space -/ @[hott] protected def eta (u : A Γ— B) : (fst u, snd u) = u := by cases u; reflexivity @[hott] def pair_eq (pa : a = a') (pb : b = b') : (a, b) = (a', b') := ap011 prod.mk pa pb @[hott] def prod_eq (H₁ : u.1 = v.1) (Hβ‚‚ : u.2 = v.2) : u = v := by cases u; cases v; exact pair_eq H₁ Hβ‚‚ @[hott] def eq_fst (p : u = v) : u.1 = v.1 := ap fst p @[hott] def eq_snd (p : u = v) : u.2 = v.2 := ap snd p postfix `..1`:(max+1) := eq_fst postfix `..2`:(max+1) := eq_snd @[hott] protected def ap_fst (p : u = v) : ap fst p = p..1 := idp @[hott] protected def ap_snd (p : u = v) : ap snd p = p..2 := idp @[hott] def pair_prod_eq (p : u.1 = v.1) (q : u.2 = v.2) : ((prod_eq p q)..1, (prod_eq p q)..2) = (p, q) := by induction u; induction v; dsimp at *; induction p; induction q; reflexivity @[hott] def prod_eq_fst (p : u.1 = v.1) (q : u.2 = v.2) : (prod_eq p q)..1 = p := (pair_prod_eq p q)..1 @[hott] def prod_eq_snd (p : u.1 = v.1) (q : u.2 = v.2) : (prod_eq p q)..2 = q := (pair_prod_eq p q)..2 @[hott] def prod_eq_eta (p : u = v) : prod_eq (p..1) (p..2) = p := by induction p; induction u; reflexivity -- the uncurried version of prod_eq. We will prove that this is an equivalence @[hott] def prod_eq_unc (H : u.1 = v.1 Γ— u.2 = v.2) : u = v := by cases H with H₁ Hβ‚‚; exact prod_eq H₁ Hβ‚‚ @[hott] def pair_prod_eq_unc : Ξ (pq : u.1 = v.1 Γ— u.2 = v.2), ((prod_eq_unc pq)..1, (prod_eq_unc pq)..2) = pq | (pq₁, pqβ‚‚) := pair_prod_eq pq₁ pqβ‚‚ @[hott] def prod_eq_unc_fst (pq : u.1 = v.1 Γ— u.2 = v.2) : (prod_eq_unc pq)..1 = pq.1 := (pair_prod_eq_unc pq)..1 @[hott] def prod_eq_unc_snd (pq : u.1 = v.1 Γ— u.2 = v.2) : (prod_eq_unc pq)..2 = pq.2 := (pair_prod_eq_unc pq)..2 @[hott] def prod_eq_unc_eta (p : u = v) : prod_eq_unc (p..1, p..2) = p := prod_eq_eta p @[hott, instance] def is_equiv_prod_eq (u v : A Γ— B) : is_equiv (prod_eq_unc : u.1 = v.1 Γ— u.2 = v.2 β†’ u = v) := adjointify prod_eq_unc (Ξ»p, (p..1, p..2)) prod_eq_unc_eta pair_prod_eq_unc @[hott] def prod_eq_equiv (u v : A Γ— B) : (u = v) ≃ (u.1 = v.1 Γ— u.2 = v.2) := (equiv.mk prod_eq_unc (by apply_instance))⁻¹ᡉ @[hott] def pair_eq_pair_equiv (a a' : A) (b b' : B) : ((a, b) = (a', b')) ≃ (a = a' Γ— b = b') := prod_eq_equiv (a, b) (a', b') @[hott] def ap_prod_mk_left (p : a = a') : ap (Ξ»a, prod.mk a b) p = prod_eq p idp := ap_eq_ap011_left prod.mk p b @[hott] def ap_prod_mk_right (p : b = b') : ap (Ξ»b, prod.mk a b) p = prod_eq idp p := ap_eq_ap011_right prod.mk a p @[hott] def pair_eq_eta {A B : Type _} {u v : A Γ— B} (p : u = v) : pair_eq (p..1) (p..2) = prod.eta u ⬝ p ⬝ (prod.eta v)⁻¹ := by induction p; induction u; reflexivity @[hott] def prod_eq_eq {A B : Type _} {u v : A Γ— B} {p₁ q₁ : u.1 = v.1} {pβ‚‚ qβ‚‚ : u.2 = v.2} (α₁ : p₁ = q₁) (Ξ±β‚‚ : pβ‚‚ = qβ‚‚) : prod_eq p₁ pβ‚‚ = prod_eq q₁ qβ‚‚ := by induction α₁; induction Ξ±β‚‚; reflexivity @[hott] def prod_eq_assemble {A B : Type _} {u v : A Γ— B} {p q : u = v} (α₁ : p..1 = q..1) (Ξ±β‚‚ : p..2 = q..2) : p = q := (prod_eq_eta p)⁻¹ ⬝ prod.prod_eq_eq α₁ Ξ±β‚‚ ⬝ prod_eq_eta q @[hott] def eq_fst_concat {A B : Type _} {u v w : A Γ— B} (p : u = v) (q : v = w) : (p ⬝ q)..1 = p..1 ⬝ q..1 := by induction q; reflexivity @[hott] def eq_snd_concat {A B : Type _} {u v w : A Γ— B} (p : u = v) (q : v = w) : (p ⬝ q)..2 = p..2 ⬝ q..2 := by induction q; reflexivity /- Groupoid structure -/ @[hott] def prod_eq_inv (p : a = a') (q : b = b') : (@prod_eq _ _ (a,b) (a',b') p q)⁻¹ = prod_eq p⁻¹ q⁻¹ := by induction p; induction q; reflexivity @[hott] def prod_eq_concat (p : a = a') (p' : a' = a'') (q : b = b') (q' : b' = b'') : @prod_eq _ _ (a,b) (a',b') p q ⬝ @prod_eq _ _ (a',b') (a'',b'') p' q' = prod_eq (p ⬝ p') (q ⬝ q') := by induction p; induction q; induction p'; induction q'; reflexivity @[hott] def prod_eq_concat_idp (p : a = a') (q : b = b') : @prod_eq _ _ (a,b) (a',b) p idp ⬝ @prod_eq _ _ (a',b) (a',b') idp q = prod_eq p q := by induction p; induction q; reflexivity /- Transport -/ @[hott] def prod_transport (p : a = a') (u : P a Γ— Q a) : p β–Έ u = (p β–Έ u.1, p β–Έ u.2) := by induction p; induction u; reflexivity @[hott] def prod_eq_transport (p : a = a') (q : b = b') {R : A Γ— B β†’ Type _} (r : R (a, b)) : @prod_eq _ _ (a,b) (a',b') p q β–Έ r = p β–Έ (q β–Έ r) := by induction p; induction q; reflexivity /- Pathovers -/ @[hott] def etao (p : a = a') (bc : P a Γ— Q a) : bc =[p; Ξ» a, P a Γ— Q a] (p β–Έ bc.1, p β–Έ bc.2) := by induction p; induction bc; apply idpo @[hott] def prod_pathover (p : a = a') (u : P a Γ— Q a) (v : P a' Γ— Q a') (r : u.1 =[p] v.1) (s : u.2 =[p] v.2) : u =[p; Ξ» a, P a Γ— Q a] v := begin induction u, induction v, dsimp at *, induction r, refine idp_rec_on s _, apply idpo end @[hott] def prod_pathover_equiv {A : Type _} {B C : A β†’ Type _} {a a' : A} (p : a = a') (x : B a Γ— C a) (x' : B a' Γ— C a') : x =[p; Ξ» a, B a Γ— C a] x' ≃ x.1 =[p] x'.1 Γ— x.2 =[p] x'.2 := begin fapply equiv.MK, { intro q, induction q, constructor; constructor }, { intro v, induction v with q r, exact prod_pathover _ _ _ q r }, { intro v, induction v with q r, induction x with b c, induction x' with b' c', dsimp at *, induction q, refine idp_rec_on r _, reflexivity }, { intro q, induction q, induction x with b c, reflexivity } end /- TODO: * define the projections from the type u =[p] v * show that the uncurried version of prod_pathover is an equivalence -/ /- Functorial action -/ variables (f : A β†’ A') (g : B β†’ B') @[hott] def prod_functor (u : A Γ— B) : A' Γ— B' := (f u.1, g u.2) @[hott] def ap_prod_functor (p : u.1 = v.1) (q : u.2 = v.2) : ap (prod_functor f g) (prod_eq p q) = prod_eq (ap f p) (ap g q) := by induction u; induction v; dsimp at *; induction p; induction q; reflexivity /- Helpers for functions of two arguments -/ @[hott] def ap_diagonal {a a' : A} (p : a = a') : ap (Ξ»x : A, (x,x)) p = prod_eq p p := by induction p; constructor @[hott] def ap_binary (m : A β†’ B β†’ C) (p : a = a') (q : b = b') : ap (Ξ»z : A Γ— B, m z.1 z.2) (@prod_eq _ _ (a,b) (a',b') p q) = ap (m a) q ⬝ ap (Ξ»x : A, m x b') p := by induction p; induction q; constructor @[hott] def ap_prod_elim {A B C : Type _} {a a' : A} {b b' : B} (m : A β†’ B β†’ C) (p : a = a') (q : b = b') : @ap (A Γ— B) C (Ξ» ab, prod.rec m ab) (a,b) (a',b') (prod_eq p q) = ap (m a) q ⬝ ap (Ξ»x : A, m x b') p := by induction p; induction q; constructor @[hott] def ap_prod_elim_idp {A B C : Type _} {a a' : A} (m : A β†’ B β†’ C) (p : a = a') (b : B) : @ap (A Γ— B) C (Ξ» ab, prod.rec m ab) (a,b) (a',b) (prod_eq p idp) = ap (Ξ»x : A, m x b) p := ap_prod_elim m p idp ⬝ idp_con _ /- Equivalences -/ @[hott, instance] def is_equiv_prod_functor [H : is_equiv f] [H : is_equiv g] : is_equiv (prod_functor f g) := begin apply adjointify _ (prod_functor f⁻¹ᢠ g⁻¹ᢠ); intro u; induction u; dsimp [prod_functor], {rwr [right_inv f, right_inv g]}, {rwr [left_inv f, left_inv g]}, end @[hott] def prod_equiv_prod_of_is_equiv [H : is_equiv f] [H : is_equiv g] : A Γ— B ≃ A' Γ— B' := equiv.mk (prod_functor f g) (by apply_instance) @[hott] def prod_equiv_prod (f : A ≃ A') (g : B ≃ B') : A Γ— B ≃ A' Γ— B' := equiv.mk (prod_functor f g) (by apply_instance) -- rename @[hott] def prod_equiv_prod_left (g : B ≃ B') : A Γ— B ≃ A Γ— B' := prod_equiv_prod equiv.rfl g -- rename @[hott] def prod_equiv_prod_right (f : A ≃ A') : A Γ— B ≃ A' Γ— B := prod_equiv_prod f equiv.rfl /- Symmetry -/ @[hott, instance] def is_equiv_flip (A B : Type _) : is_equiv (flip : A Γ— B β†’ B Γ— A) := adjointify flip flip (Ξ» ⟨b,a⟩, idp) (Ξ» ⟨a,b⟩, idp) @[hott] def prod_comm_equiv (A B : Type _) : A Γ— B ≃ B Γ— A := equiv.mk flip (by apply_instance) /- Associativity -/ @[hott] def prod_assoc_equiv (A B C : Type _) : A Γ— (B Γ— C) ≃ (A Γ— B) Γ— C := begin fapply equiv.MK, { intro z, induction z with a z, induction z with b c, exact ⟨⟨a, b⟩, c⟩}, { intro z, induction z with z c, induction z with a b, exact ⟨a, ⟨b, c⟩⟩}, { intro z, induction z with z c, induction z with a b, reflexivity}, { intro z, induction z with a z, induction z with b c, reflexivity}, end @[hott] def prod_contr_equiv (A : Type u) (B : Type v) [H : is_contr B] : A Γ— B ≃ A := equiv.MK fst (Ξ»x, (x, by apply center)) (Ξ»x, idp) (Ξ» ⟨a,b⟩, pair_eq idp (center_eq _)) @[hott] def prod_unit_equiv (A : Type _) : A Γ— unit ≃ A := by apply prod_contr_equiv @[hott] def prod_empty_equiv (A : Type _) : A Γ— empty ≃ empty := begin fapply equiv.MK, { intro x, cases x with a e, cases e }, { intro e, cases e }, { intro e, cases e }, { intro x, cases x with a e, cases e } end /- Universal mapping properties -/ @[hott, instance] def is_equiv_prod_rec (P : A Γ— B β†’ Type _) : is_equiv (prod.rec : (Ξ a b, P (a, b)) β†’ Ξ u, P u) := adjointify _ (Ξ»g a b, g (a, b)) (Ξ»g, eq_of_homotopy (Ξ»u, by induction u;reflexivity)) (Ξ»f, idp) @[hott] def equiv_prod_rec (P : A Γ— B β†’ Type _) : (Ξ a b, P (a, b)) ≃ (Ξ u, P u) := equiv.mk prod.rec (by apply_instance) @[hott] def imp_imp_equiv_prod_imp (A B C : Type _) : (A β†’ B β†’ C) ≃ (A Γ— B β†’ C) := equiv_prod_rec (Ξ» _, C) @[hott] def prod_corec_unc {P Q : A β†’ Type _} (u : (Ξ a, P a) Γ— (Ξ a, Q a)) (a : A) : P a Γ— Q a := (u.1 a, u.2 a) @[hott] def is_equiv_prod_corec (P Q : A β†’ Type _) : is_equiv (prod_corec_unc : (Ξ a, P a) Γ— (Ξ a, Q a) β†’ Ξ a, P a Γ— Q a) := adjointify _ (Ξ»g, (Ξ»a, (g a).1, Ξ»a, (g a).2)) (by intro g; apply eq_of_homotopy; intro a; dsimp [prod_corec_unc]; induction g a; reflexivity) (by intro h; induction h with f g; reflexivity) @[hott] def equiv_prod_corec (P Q : A β†’ Type _) : ((Ξ a, P a) Γ— (Ξ a, Q a)) ≃ (Ξ a, P a Γ— Q a) := equiv.mk _ (by apply is_equiv_prod_corec) @[hott] def imp_prod_imp_equiv_imp_prod (A B C : Type _) : (A β†’ B) Γ— (A β†’ C) ≃ (A β†’ (B Γ— C)) := by apply equiv_prod_corec @[hott] def is_trunc_prod (A B : Type _) (n : trunc_index) [HA : is_trunc n A] [HB : is_trunc n B] : is_trunc n (A Γ— B) := begin unfreezeI, induction n with n IH generalizing A B HA HB; resetI, { fapply is_contr.mk, constructor; apply center, intro u, apply prod_eq; apply center_eq}, { apply @is_trunc_succ_intro _ _ _, intros u v, apply @is_trunc_equiv_closed_rev (u=v) ((u.fst = v.fst) Γ— (u.snd = v.snd)) _ _ _, apply prod_eq_equiv, exact @IH _ _ _ _} end end prod attribute [instance] prod.is_trunc_prod namespace prod /- pointed products -/ open pointed @[hott, instance] def pointed_prod (A B : Type _) [H1 : pointed A] [H2 : pointed B] : pointed (A Γ— B) := pointed.mk (pt,pt) @[hott] def pprod (A B : Type*) : Type* := pointed.mk' (A Γ— B) infixr ` Γ—* `:35 := pprod @[hott] def pfst {A B : Type*} : A Γ—* B β†’* A := pmap.mk fst idp @[hott] def psnd {A B : Type*} : A Γ—* B β†’* B := pmap.mk snd idp @[hott] def tprod {n : trunc_index} (A B : n-Type) : n-Type := trunctype.mk (A Γ— B) (by apply_instance) infixr `Γ—t`:30 := tprod @[hott] def ptprod {n : β„•β‚‹β‚‚} (A B : n-Type*) : n-Type* := ptrunctype.mk' n (A Γ— B) @[hott] def pprod_functor {A B C D : Type*} (f : A β†’* C) (g : B β†’* D) : A Γ—* B β†’* C Γ—* D := pmap.mk (prod_functor f g) (prod_eq (respect_pt f) (respect_pt g)) end prod end hott
00c5ea60a09b94318da0444e5094de511c47f410
60bf3fa4185ec5075eaea4384181bfbc7e1dc319
/src/game/order/level09.lean
2d46e6587003afe6062c03f230688cab3db051ad
[ "Apache-2.0" ]
permissive
anrddh/real-number-game
660f1127d03a78fd35986c771d65c3132c5f4025
c708c4e02ec306c657e1ea67862177490db041b0
refs/heads/master
1,668,214,277,092
1,593,105,075,000
1,593,105,075,000
264,269,218
0
0
null
1,589,567,264,000
1,589,567,264,000
null
UTF-8
Lean
false
false
2,803
lean
import data.real.basic open real namespace xena -- hide /- # Chapter 2 : Order ## Level 9 This level invites you to work out a property of the absolute value. In Lean the absolute value of $x$ is denoted by `abs x`. For ease of use, a notation can be used around that definition as below. Feel free to use the triangle inequality on the real numbers, `abs_add : βˆ€ (a b : ?M_1), |a + b| ≀ |a| + |b|` together with the `linarith` and `norm_num` tactics. -/ notation `|` x `|` := abs x -- begin hide -- this to go in the side bar lemma eq_sqr_to_eq (a b : ℝ) (ha : 0 ≀ a) (hb : 0 ≀ b) : a^2 = b^2 β†’ a = b := begin intro H, have A : sqrt (a ^ 2) = sqrt (a ^ 2), refl, rw H at A {occs := occurrences.pos [2]}, have G := sqrt_sqr ha, rw G at A, have F := sqrt_sqr hb, rw F at A, exact A, done end -- end hide /- Lemma For any two real numbers $a$ and $b$, we have that $$|a + b| = |a| + |b|$$ if and only if $ab \ge 0$ . -/ theorem abs_sub_eq_sum_abs (a b : ℝ) : |a + b| = |a| + |b| ↔ a * b β‰₯ 0 := begin have H0 : (a+b)^2 = |a+b|^2, have h01 := abs_mul_abs_self (a+b), rw pow_two _, rw pow_two _, symmetry, exact h01, have H1 : 0 ≀ (a + b) ^ 2, exact pow_two_nonneg (a+b), have H2 : (a+b) ^ 2 = a ^2 + 2 * a * b + b^2, ring, have H3 : ( |a| + |b| )^2 = |a|^2 + 2*|a|*|b| + |b|^2, ring, rw H0 at H2, have Ha : a^2 = |a|^2, have h01 := abs_mul_abs_self a, rw pow_two _, rw pow_two _, symmetry, exact h01, have Hb : b^2 = |b|^2, have h01 := abs_mul_abs_self b, rw pow_two _, rw pow_two _, symmetry, exact h01, rw [Ha, Hb] at H2, split, intro h, rw h at H2, rw H3 at H2, simp at H2, rw mul_assoc at H2, rw mul_assoc at H2, have g1 : ( |a| * |b| ) = (a * b), linarith, have g2 : |a * b| = ( |a| * |b| ), exact abs_mul _ _, rw ← g2 at g1, by_contradiction hn, push_neg at hn, have g3 : | a * b | = - (a *b), exact abs_of_neg hn, rw g1 at g3, linarith, -- the right-left direction intro h, have g1 : |a * b| = a * b, exact abs_of_nonneg h, have g2 : |a * b| = ( |a| * |b| ), exact abs_mul _ _, rw g2 at g1, rw mul_assoc 2 a b at H2, rw ← g1 at H2, have g3 : |a| ^ 2 + 2 * ( |a| * |b| ) + |b| ^ 2 = ( |a| + |b| )^2, ring, rw g3 at H2, have g4 : sqrt ( |a + b| ^ 2 ) = sqrt ( |a + b| ^ 2), refl, rw H2 at g4 {occs := occurrences.pos [2]}, have hab : 0 ≀ |a + b|, exact is_absolute_value.abv_nonneg abs (a+b), have ha : 0 ≀ |a|, exact is_absolute_value.abv_nonneg abs a, have hb : 0 ≀ |b|, exact is_absolute_value.abv_nonneg abs b, have hc : 0 ≀ |a| + |b|, linarith, have G := eq_sqr_to_eq ( |a + b| ) ( |a| + |b| ) hab hc H2, exact G, done end end xena -- hide
79f7108f8ad50c2dd377c2223121c5e0de12798c
4727251e0cd73359b15b664c3170e5d754078599
/src/combinatorics/simple_graph/matching.lean
7fc17adc471d2b25269f4ed096552eb0125406cf
[ "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,592
lean
/- Copyright (c) 2020 Alena Gusakov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alena Gusakov, Arthur Paulino, Kyle Miller -/ import combinatorics.simple_graph.degree_sum import combinatorics.simple_graph.subgraph /-! # Matchings A *matching* for a simple graph is a set of disjoint pairs of adjacent vertices, and the set of all the vertices in a matching is called its *support* (and sometimes the vertices in the support are said to be *saturated* by the matching). A *perfect matching* is a matching whose support contains every vertex of the graph. In this module, we represent a matching as a subgraph whose vertices are each incident to at most one edge, and the edges of the subgraph represent the paired vertices. ## Main definitions * `simple_graph.subgraph.is_matching`: `M.is_matching` means that `M` is a matching of its underlying graph. denoted `M.is_matching`. * `simple_graph.subgraph.is_perfect_matching` defines when a subgraph `M` of a simple graph is a perfect matching, denoted `M.is_perfect_matching`. ## TODO * Define an `other` function and prove useful results about it (https://leanprover.zulipchat.com/#narrow/stream/252551-graph-theory/topic/matchings/near/266205863) * Provide a bicoloring for matchings (https://leanprover.zulipchat.com/#narrow/stream/252551-graph-theory/topic/matchings/near/265495120) * Tutte's Theorem * Hall's Marriage Theorem (see combinatorics.hall) -/ universe u namespace simple_graph variables {V : Type u} {G : simple_graph V} (M : subgraph G) namespace subgraph /-- The subgraph `M` of `G` is a matching if every vertex of `M` is incident to exactly one edge in `M`. We say that the vertices in `M.support` are *matched* or *saturated*. -/ def is_matching : Prop := βˆ€ ⦃v⦄, v ∈ M.verts β†’ βˆƒ! w, M.adj v w /-- Given a vertex, returns the unique edge of the matching it is incident to. -/ noncomputable def is_matching.to_edge {M : subgraph G} (h : M.is_matching) (v : M.verts) : M.edge_set := ⟨⟦(v, (h v.property).some)⟧, (h v.property).some_spec.1⟩ lemma is_matching.to_edge_eq_of_adj {M : subgraph G} (h : M.is_matching) {v w : V} (hv : v ∈ M.verts) (hvw : M.adj v w) : h.to_edge ⟨v, hv⟩ = ⟨⟦(v, w)⟧, hvw⟩ := begin simp only [is_matching.to_edge, subtype.mk_eq_mk], congr, exact ((h (M.edge_vert hvw)).some_spec.2 w hvw).symm, end lemma is_matching.to_edge.surjective {M : subgraph G} (h : M.is_matching) : function.surjective h.to_edge := begin rintro ⟨e, he⟩, refine sym2.ind (Ξ» x y he, _) e he, exact ⟨⟨x, M.edge_vert he⟩, h.to_edge_eq_of_adj _ he⟩, end lemma is_matching.to_edge_eq_to_edge_of_adj {M : subgraph G} {v w : V} (h : M.is_matching) (hv : v ∈ M.verts) (hw : w ∈ M.verts) (ha : M.adj v w) : h.to_edge ⟨v, hv⟩ = h.to_edge ⟨w, hw⟩ := by rw [h.to_edge_eq_of_adj hv ha, h.to_edge_eq_of_adj hw (M.symm ha), subtype.mk_eq_mk, sym2.eq_swap] /-- The subgraph `M` of `G` is a perfect matching on `G` if it's a matching and every vertex `G` is matched. -/ def is_perfect_matching : Prop := M.is_matching ∧ M.is_spanning lemma is_matching.support_eq_verts {M : subgraph G} (h : M.is_matching) : M.support = M.verts := begin refine M.support_subset_verts.antisymm (Ξ» v hv, _), obtain ⟨w, hvw, -⟩ := h hv, exact ⟨_, hvw⟩, end lemma is_matching_iff_forall_degree {M : subgraph G} [Ξ  (v : V), fintype (M.neighbor_set v)] : M.is_matching ↔ βˆ€ (v : V), v ∈ M.verts β†’ M.degree v = 1 := by simpa [degree_eq_one_iff_unique_adj] lemma is_matching.even_card {M : subgraph G} [fintype M.verts] (h : M.is_matching) : even M.verts.to_finset.card := begin classical, rw is_matching_iff_forall_degree at h, use M.coe.edge_finset.card, rw [← two_mul, ← M.coe.sum_degrees_eq_twice_card_edges], simp [h, finset.card_univ], end lemma is_perfect_matching_iff : M.is_perfect_matching ↔ βˆ€ v, βˆƒ! w, M.adj v w := begin refine ⟨_, Ξ» hm, ⟨λ v hv, hm v, Ξ» v, _⟩⟩, { rintro ⟨hm, hs⟩ v, exact hm (hs v) }, { obtain ⟨w, hw, -⟩ := hm v, exact M.edge_vert hw } end lemma is_perfect_matching_iff_forall_degree {M : subgraph G} [Ξ  v, fintype (M.neighbor_set v)] : M.is_perfect_matching ↔ βˆ€ v, M.degree v = 1 := by simp [degree_eq_one_iff_unique_adj, is_perfect_matching_iff] lemma is_perfect_matching.even_card {M : subgraph G} [fintype V] (h : M.is_perfect_matching) : even (fintype.card V) := by { classical, simpa [h.2.card_verts] using is_matching.even_card h.1 } end subgraph end simple_graph
9680778bf43ed5f159c1675092696542e69b960f
7a76361040c55ae1eba5856c1a637593117a6556
/src/lovelib.lean
241a3e4cf21add516f1a16497165953364a49522
[]
no_license
rgreenblatt/fpv2021
c2cbe7b664b648cef7d240a654d6bdf97a559272
c65d72e48c8fa827d2040ed6ea86c2be62db36fa
refs/heads/main
1,692,245,693,819
1,633,364,621,000
1,633,364,621,000
407,231,487
0
0
null
1,631,808,608,000
1,631,808,608,000
null
UTF-8
Lean
false
false
6,942
lean
import algebra import data.real.basic import data.vector import tactic.explode import tactic.find import tactic.induction import tactic.linarith import tactic.rcases import tactic.rewrite import tactic.ring_exp import tactic.tidy import tactic.where /-! # LoVe Library This files contains a few extensions on top of Lean's core libraries and `mathlib`. -/ namespace LoVe /-! ## Structured Proofs -/ notation `fix ` binders `, ` r:(scoped f, f) := r /-! ## Logical Connectives -/ meta def tactic.dec_trivial := `[exact dec_trivial] lemma not_def (a : Prop) : Β¬ a ↔ a β†’ false := by refl @[simp] lemma not_not_iff (a : Prop) [decidable a] : ¬¬ a ↔ a := by by_cases a; simp [h] @[simp] lemma and_imp_distrib (a b c : Prop) : (a ∧ b β†’ c) ↔ (a β†’ b β†’ c) := iff.intro (assume h ha hb, h ⟨ha, hb⟩) (assume h ⟨ha, hb⟩, h ha hb) @[simp] lemma or_imp_distrib {a b c : Prop} : a ∨ b β†’ c ↔ (a β†’ c) ∧ (b β†’ c) := iff.intro (assume h, ⟨assume ha, h (or.intro_left _ ha), assume hb, h (or.intro_right _ hb)⟩) (assume ⟨ha, hb⟩ h, match h with or.inl h := ha h | or.inr h := hb h end) @[simp] lemma exists_imp_distrib {Ξ± : Sort*} {p : Ξ± β†’ Prop} {a : Prop} : ((βˆƒx, p x) β†’ a) ↔ (βˆ€x, p x β†’ a) := iff.intro (assume h hp ha, h ⟨hp, ha⟩) (assume h ⟨hp, ha⟩, h hp ha) lemma and_exists {Ξ± : Sort*} {p : Ξ± β†’ Prop} {a : Prop} : (a ∧ (βˆƒx, p x)) ↔ (βˆƒx, a ∧ p x) := iff.intro (assume ⟨ha, x, hp⟩, ⟨x, ha, hp⟩) (assume ⟨x, ha, hp⟩, ⟨ha, x, hp⟩) @[simp] lemma exists_false {Ξ± : Sort*} : (βˆƒx : Ξ±, false) ↔ false := iff.intro (assume ⟨a, f⟩, f) (assume h, h.elim) /-! ## Natural Numbers -/ attribute [simp] nat.add /-! ## Integers -/ @[simp] lemma int.neg_comp_neg : int.neg ∘ int.neg = id := begin apply funext, apply neg_neg end /-! ## Reflexive Transitive Closure -/ namespace rtc inductive star {Ξ± : Sort*} (r : Ξ± β†’ Ξ± β†’ Prop) (a : Ξ±) : Ξ± β†’ Prop | refl {} : star a | tail {b c} : star b β†’ r b c β†’ star c attribute [refl] star.refl namespace star variables {Ξ± : Sort*} {r : Ξ± β†’ Ξ± β†’ Prop} {a b c d : Ξ±} @[trans] lemma trans (hab : star r a b) (hbc : star r b c) : star r a c := begin induction' hbc, case refl { assumption }, case tail : c d hbc hcd hac { exact (tail (hac hab)) hcd } end lemma single (hab : r a b) : star r a b := refl.tail hab lemma head (hab : r a b) (hbc : star r b c) : star r a c := begin induction' hbc, case refl { exact (tail refl) hab }, case tail : c d hbc hcd hac { exact (tail (hac hab)) hcd } end lemma head_induction_on {Ξ± : Sort*} {r : Ξ± β†’ Ξ± β†’ Prop} {b : Ξ±} {P : βˆ€a : Ξ±, star r a b β†’ Prop} {a : Ξ±} (h : star r a b) (refl : P b refl) (head : βˆ€{a c} (h' : r a c) (h : star r c b), P c h β†’ P a (h.head h')) : P a h := begin induction' h, case refl { exact refl }, case tail : b c hab hbc ih { apply ih, show P b _, from head hbc _ refl, show βˆ€a a', r a a' β†’ star r a' b β†’ P a' _ β†’ P a _, from assume a a' hab hbc, head hab _ } end lemma trans_induction_on {Ξ± : Sort*} {r : Ξ± β†’ Ξ± β†’ Prop} {p : βˆ€{a b : Ξ±}, star r a b β†’ Prop} {a b : Ξ±} (h : star r a b) (ih₁ : βˆ€a, @p a a refl) (ihβ‚‚ : βˆ€{a b} (h : r a b), p (single h)) (ih₃ : βˆ€{a b c} (h₁ : star r a b) (hβ‚‚ : star r b c), p h₁ β†’ p hβ‚‚ β†’ p (h₁.trans hβ‚‚)) : p h := begin induction' h, case refl { exact ih₁ a }, case tail : b c hab hbc ih { exact ih₃ hab (single hbc) (ih ih₁ @ihβ‚‚ @ih₃) (ihβ‚‚ hbc) } end lemma lift {Ξ² : Sort*} {s : Ξ² β†’ Ξ² β†’ Prop} (f : Ξ± β†’ Ξ²) (h : βˆ€a b, r a b β†’ s (f a) (f b)) (hab : star r a b) : star s (f a) (f b) := hab.trans_induction_on (assume a, refl) (assume a b, single ∘ h _ _) (assume a b c _ _, trans) lemma mono {p : Ξ± β†’ Ξ± β†’ Prop} : (βˆ€a b, r a b β†’ p a b) β†’ star r a b β†’ star p a b := lift id lemma star_star_eq : star (star r) = star r := funext (assume a, funext (assume b, propext (iff.intro (assume h, begin induction' h, { refl }, { transitivity; assumption } end) (star.mono (assume a b, single))))) end star end rtc export rtc /-! ## States -/ def state : Type := string β†’ β„• def state.update (name : string) (val : β„•) (s : state) : state := Ξ»name', if name' = name then val else s name' notation s `{` name ` ↦ ` val `}` := state.update name val s instance : has_emptyc state := { emptyc := Ξ»_, 0 } @[simp] lemma update_apply (name : string) (val : β„•) (s : state) : s{name ↦ val} name = val := if_pos rfl @[simp] lemma update_apply_ne (name name' : string) (val : β„•) (s : state) (h : name' β‰  name . tactic.dec_trivial) : s{name ↦ val} name' = s name' := if_neg h @[simp] lemma update_override (name : string) (val₁ valβ‚‚ : β„•) (s : state) : s{name ↦ valβ‚‚}{name ↦ val₁} = s{name ↦ val₁} := begin apply funext, intro name', by_cases name' = name; simp [h] end @[simp] lemma update_swap (name₁ nameβ‚‚ : string) (val₁ valβ‚‚ : β„•) (s : state) (h : name₁ β‰  nameβ‚‚ . tactic.dec_trivial) : s{nameβ‚‚ ↦ valβ‚‚}{name₁ ↦ val₁} = s{name₁ ↦ val₁}{nameβ‚‚ ↦ valβ‚‚} := begin apply funext, intro name', by_cases name' = name₁; by_cases name' = nameβ‚‚; simp * at * end @[simp] lemma update_id (name : string) (s : state) : s{name ↦ s name} = s := begin apply funext, intro name', by_cases name' = name; simp * at * end @[simp] lemma update_same_const (name : string) (val : β„•) : (Ξ»_, val){name ↦ val} = (Ξ»_, val) := by apply funext; simp example (s : state) : s{"a" ↦ 0}{"a" ↦ 2} = s{"a" ↦ 2} := by simp example (s : state) : s{"a" ↦ 0}{"b" ↦ 2} = s{"b" ↦ 2}{"a" ↦ 0} := by simp example (s : state) : s{"a" ↦ s "a"}{"b" ↦ 0} = s{"b" ↦ 0} := by simp /-! ## Relations -/ def Id {Ξ± : Type} : set (Ξ± Γ— Ξ±) := {ab | prod.snd ab = prod.fst ab} @[simp] lemma mem_Id {Ξ± : Type} (a b : Ξ±) : (a, b) ∈ @Id Ξ± ↔ b = a := by refl def comp {Ξ± : Type} (r₁ rβ‚‚ : set (Ξ± Γ— Ξ±)) : set (Ξ± Γ— Ξ±) := {ac | βˆƒb, (prod.fst ac, b) ∈ r₁ ∧ (b, prod.snd ac) ∈ rβ‚‚} infixl ` β—― ` : 90 := comp @[simp] lemma mem_comp {Ξ± : Type} (r₁ rβ‚‚ : set (Ξ± Γ— Ξ±)) (a b : Ξ±) : (a, b) ∈ r₁ β—― rβ‚‚ ↔ (βˆƒc, (a, c) ∈ r₁ ∧ (c, b) ∈ rβ‚‚) := by refl def restrict {Ξ± : Type} (r : set (Ξ± Γ— Ξ±)) (p : Ξ± β†’ Prop) : set (Ξ± Γ— Ξ±) := {ab | ab ∈ r ∧ p (prod.fst ab)} infixl ` ⇃ ` : 90 := restrict @[simp] lemma mem_restrict {Ξ± : Type} (r : set (Ξ± Γ— Ξ±)) (p : Ξ± β†’ Prop) (a b : Ξ±) : (a, b) ∈ r ⇃ p ↔ (a, b) ∈ r ∧ p a := by refl /-! ## Setoids -/ def equivalence_rel : Type β†’ Type := setoid end LoVe
d10873b3a74ab6b6637b6013946e7ee30bb76245
4727251e0cd73359b15b664c3170e5d754078599
/src/dynamics/flow.lean
5103f6a58c727e029568db91c2e51f0489840c9f
[ "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,165
lean
/- Copyright (c) 2020 Jean Lo. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jean Lo -/ import topology.algebra.group import logic.function.iterate /-! # Flows and invariant sets This file defines a flow on a topological space `Ξ±` by a topological monoid `Ο„` as a continuous monoid-act of `Ο„` on `Ξ±`. Anticipating the cases where `Ο„` is one of `β„•`, `β„€`, `ℝ⁺`, or `ℝ`, we use additive notation for the monoids, though the definition does not require commutativity. A subset `s` of `Ξ±` is invariant under a family of maps `Ο•β‚œ : Ξ± β†’ Ξ±` if `Ο•β‚œ s βŠ† s` for all `t`. In many cases `Ο•` will be a flow on `Ξ±`. For the cases where `Ο•` is a flow by an ordered (additive, commutative) monoid, we additionally define forward invariance, where `t` ranges over those elements which are nonnegative. Additionally, we define such constructions as the restriction of a flow onto an invariant subset, and the time-reveral of a flow by a group. -/ open set function filter /-! ### Invariant sets -/ section invariant variables {Ο„ : Type*} {Ξ± : Type*} /-- A set `s βŠ† Ξ±` is invariant under `Ο• : Ο„ β†’ Ξ± β†’ Ξ±` if `Ο• t s βŠ† s` for all `t` in `Ο„`. -/ def is_invariant (Ο• : Ο„ β†’ Ξ± β†’ Ξ±) (s : set Ξ±): Prop := βˆ€ t, maps_to (Ο• t) s s variables (Ο• : Ο„ β†’ Ξ± β†’ Ξ±) (s : set Ξ±) lemma is_invariant_iff_image : is_invariant Ο• s ↔ βˆ€ t, Ο• t '' s βŠ† s := by simp_rw [is_invariant, maps_to'] /-- A set `s βŠ† Ξ±` is forward-invariant under `Ο• : Ο„ β†’ Ξ± β†’ Ξ±` if `Ο• t s βŠ† s` for all `t β‰₯ 0`. -/ def is_fw_invariant [preorder Ο„] [has_zero Ο„] (Ο• : Ο„ β†’ Ξ± β†’ Ξ±) (s : set Ξ±): Prop := βˆ€ ⦃t⦄, 0 ≀ t β†’ maps_to (Ο• t) s s lemma is_invariant.is_fw_invariant [preorder Ο„] [has_zero Ο„] {Ο• : Ο„ β†’ Ξ± β†’ Ξ±} {s : set Ξ±} (h : is_invariant Ο• s) : is_fw_invariant Ο• s := Ξ» t ht, h t /-- If `Ο„` is a `canonically_ordered_add_monoid` (e.g., `β„•` or `ℝβ‰₯0`), then the notions `is_fw_invariant` and `is_invariant` are equivalent. -/ lemma is_fw_invariant.is_invariant [canonically_ordered_add_monoid Ο„] {Ο• : Ο„ β†’ Ξ± β†’ Ξ±} {s : set Ξ±} (h : is_fw_invariant Ο• s) : is_invariant Ο• s := Ξ» t, h (zero_le t) /-- If `Ο„` is a `canonically_ordered_add_monoid` (e.g., `β„•` or `ℝβ‰₯0`), then the notions `is_fw_invariant` and `is_invariant` are equivalent. -/ lemma is_fw_invariant_iff_is_invariant [canonically_ordered_add_monoid Ο„] {Ο• : Ο„ β†’ Ξ± β†’ Ξ±} {s : set Ξ±} : is_fw_invariant Ο• s ↔ is_invariant Ο• s := ⟨is_fw_invariant.is_invariant, is_invariant.is_fw_invariant⟩ end invariant /-! ### Flows -/ /-- A flow on a topological space `Ξ±` by an a additive topological monoid `Ο„` is a continuous monoid action of `Ο„` on `Ξ±`.-/ structure flow (Ο„ : Type*) [topological_space Ο„] [add_monoid Ο„] [has_continuous_add Ο„] (Ξ± : Type*) [topological_space Ξ±] := (to_fun : Ο„ β†’ Ξ± β†’ Ξ±) (cont' : continuous (uncurry to_fun)) (map_add' : βˆ€ t₁ tβ‚‚ x, to_fun (t₁ + tβ‚‚) x = to_fun t₁ (to_fun tβ‚‚ x)) (map_zero' : βˆ€ x, to_fun 0 x = x) namespace flow variables {Ο„ : Type*} [add_monoid Ο„] [topological_space Ο„] [has_continuous_add Ο„] {Ξ± : Type*} [topological_space Ξ±] (Ο• : flow Ο„ Ξ±) instance : inhabited (flow Ο„ Ξ±) := ⟨{ to_fun := Ξ» _ x, x, cont' := continuous_snd, map_add' := Ξ» _ _ _, rfl, map_zero' := Ξ» _, rfl }⟩ instance : has_coe_to_fun (flow Ο„ Ξ±) (Ξ» _, Ο„ β†’ Ξ± β†’ Ξ±) := ⟨flow.to_fun⟩ @[ext] lemma ext : βˆ€ {ϕ₁ Ο•β‚‚ : flow Ο„ Ξ±}, (βˆ€ t x, ϕ₁ t x = Ο•β‚‚ t x) β†’ ϕ₁ = Ο•β‚‚ | ⟨f₁, _, _, _⟩ ⟨fβ‚‚, _, _, _⟩ h := by { congr, funext, exact h _ _ } @[continuity] protected lemma continuous {Ξ² : Type*} [topological_space Ξ²] {t : Ξ² β†’ Ο„} (ht : continuous t) {f : Ξ² β†’ Ξ±} (hf : continuous f) : continuous (Ξ» x, Ο• (t x) (f x)) := Ο•.cont'.comp (ht.prod_mk hf) alias flow.continuous ← continuous.flow lemma map_add (t₁ tβ‚‚ : Ο„) (x : Ξ±) : Ο• (t₁ + tβ‚‚) x = Ο• t₁ (Ο• tβ‚‚ x) := Ο•.map_add' _ _ _ @[simp] lemma map_zero : Ο• 0 = id := funext Ο•.map_zero' lemma map_zero_apply (x : Ξ±) : Ο• 0 x = x := Ο•.map_zero' x /-- Iterations of a continuous function from a topological space `Ξ±` to itself defines a semiflow by `β„•` on `Ξ±`. -/ def from_iter {g : Ξ± β†’ Ξ±} (h : continuous g) : flow β„• Ξ± := { to_fun := Ξ» n x, g^[n] x, cont' := continuous_uncurry_of_discrete_topology_left (continuous.iterate h), map_add' := iterate_add_apply _, map_zero' := Ξ» x, rfl } /-- Restriction of a flow onto an invariant set. -/ def restrict {s : set Ξ±} (h : is_invariant Ο• s) : flow Ο„ β†₯s := { to_fun := Ξ» t, (h t).restrict _ _ _, cont' := continuous_subtype_mk _ (Ο•.continuous continuous_fst (continuous_subtype_coe.comp continuous_snd)), map_add' := Ξ» _ _ _, subtype.ext (map_add _ _ _ _), map_zero' := Ξ» _, subtype.ext (map_zero_apply _ _)} end flow namespace flow variables {Ο„ : Type*} [add_comm_group Ο„] [topological_space Ο„] [topological_add_group Ο„] {Ξ± : Type*} [topological_space Ξ±] (Ο• : flow Ο„ Ξ±) lemma is_invariant_iff_image_eq (s : set Ξ±) : is_invariant Ο• s ↔ βˆ€ t, Ο• t '' s = s := (is_invariant_iff_image _ _).trans (iff.intro (Ξ» h t, subset.antisymm (h t) (Ξ» _ hx, ⟨_, h (-t) ⟨_, hx, rfl⟩, by simp [← map_add]⟩)) (Ξ» h t, by rw h t)) /-- The time-reversal of a flow `Ο•` by a (commutative, additive) group is defined `Ο•.reverse t x = Ο• (-t) x`. -/ def reverse : flow Ο„ Ξ± := { to_fun := Ξ» t, Ο• (-t), cont' := Ο•.continuous continuous_fst.neg continuous_snd, map_add' := Ξ» _ _ _, by rw [neg_add, map_add], map_zero' := Ξ» _, by rw [neg_zero, map_zero_apply] } /-- The map `Ο• t` as a homeomorphism. -/ def to_homeomorph (t : Ο„) : Ξ± β‰ƒβ‚œ Ξ± := { to_fun := Ο• t, inv_fun := Ο• (-t), left_inv := Ξ» x, by rw [← map_add, neg_add_self, map_zero_apply], right_inv := Ξ» x, by rw [← map_add, add_neg_self, map_zero_apply] } lemma image_eq_preimage (t : Ο„) (s : set Ξ±) : Ο• t '' s = Ο• (-t) ⁻¹' s := (Ο•.to_homeomorph t).to_equiv.image_eq_preimage s end flow
17b770fae4b84c126379d6cb6ecd3493ab26107c
a3db153d66921f9d5a199b60b52ab3c21cf0e023
/src/sia.lean
383a2a04f7a7630e862175c8c21033467b6077be
[ "MIT" ]
permissive
metalogical/sia-lean
3bd87a76b3ee6d910cbf182eb2a0e6d9eca3c056
f8e354dd2ff6c09c4e001c1f80f6112c62da8592
refs/heads/master
1,629,775,811,523
1,512,684,874,000
1,512,684,874,000
106,763,566
4
0
null
null
null
null
UTF-8
Lean
false
false
1,337
lean
import .ordered_field import .util universe u variable {R : Type u} section -- define Delta in terms of ring to avoid circularity in the definition of sia variable [ring R] @[reducible] private def DeltaT := { x:R // x * x = 0 } @[reducible] private def zDeltaT : DeltaT := { val := (0 : R), property := ring.mul_zero (0 : R) } end -- Smooth Infinitesimal Analysis class sia R extends st_ordered_field R := (exists_unique_sqrt : forall a : { r: R // r > 0 }, exists! b, b * b = a.val) -- we have at least the real numbers (? is this equivalent to LUB) (kock_lawvere : forall f: DeltaT -> R, exists! a: R, forall d: DeltaT, f d = f zDeltaT + a * d.val) namespace sia -- intervals variable [sia R] @[reducible] def microstable (A : set R) : Prop := forall a : subtype A, forall d : DeltaT, set.mem (a.val + d.val) A end sia namespace sia -- export Delta in terms of sia; type parameter R is explicit because Lean cannot typically infer it @[reducible] def Delta (R: Type u) [sia R] := fun r : R, r * r = 0 @[reducible] def DeltaT (R: Type u) [sia R] := subtype (Delta R) instance {R : Type u} [sia R] : has_zero (DeltaT R) := (| zDeltaT |) -- instance {R : Type u} [sia R] : has_neg (DeltaT R) := { neg := fun (a : DeltaT R), { val := -a.val, property := sorry } } end sia
a2abd0310b7d49e5d50c227e61a2e97afbb5c3f2
8b9f17008684d796c8022dab552e42f0cb6fb347
/tests/lean/run/tactic14.lean
e0defea4e5d248da97819bf465b858c25c377e13
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
462
lean
import logic open tactic notation `(` h `|` r:(foldl `|` (e r, tactic.or_else r e) h) `)` := r definition basic_tac : tactic := repeat (apply @and.intro | assumption) set_begin_end_tactic basic_tac -- basic_tac is automatically applied to each element of a proof-qed block theorem tst (a b : Prop) (H : ¬ a ∨ ¬ b) (Hb : b) : ¬ a ∧ b := begin assume Ha, or.elim H (assume Hna, @absurd _ false Ha Hna) (assume Hnb, @absurd _ false Hb Hnb) end
9fcea9540fdbf493361ede1d09811295571d8683
a4673261e60b025e2c8c825dfa4ab9108246c32e
/tests/lean/run/sharecommon.lean
5d93d5b3e94660ad9131228d72b3d2a9d641fdb7
[ "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
3,901
lean
import Std.ShareCommon open Std def check (b : Bool) : ShareCommonT IO Unit := do unless b do throw $ IO.userError "check failed" unsafe def tst1 : ShareCommonT IO Unit := do let x := [1] let y := [0].map (fun x => x + 1) check $ ptrAddrUnsafe x != ptrAddrUnsafe y let x ← shareCommonM x let y ← shareCommonM y check $ ptrAddrUnsafe x == ptrAddrUnsafe y let z ← shareCommonM [2] let x ← shareCommonM x check $ ptrAddrUnsafe x == ptrAddrUnsafe y check $ ptrAddrUnsafe x != ptrAddrUnsafe z IO.println x IO.println y IO.println z #eval tst1.run unsafe def tst2 : ShareCommonT IO Unit := do let x := [1, 2] let y := [0, 1].map (fun x => x + 1) check $ ptrAddrUnsafe x != ptrAddrUnsafe y let x ← shareCommonM x let y ← shareCommonM y check $ ptrAddrUnsafe x == ptrAddrUnsafe y let z ← shareCommonM [2] let x ← shareCommonM x check $ ptrAddrUnsafe x == ptrAddrUnsafe y check $ ptrAddrUnsafe x != ptrAddrUnsafe z IO.println x IO.println y IO.println z #eval tst2.run structure Foo := (x : Nat) (y : Bool) (z : Bool) @[noinline] def mkFoo1 (x : Nat) (z : Bool) : Foo := { x := x, y := true, z := z } @[noinline] def mkFoo2 (x : Nat) (z : Bool) : Foo := { x := x, y := true, z := z } unsafe def tst3 : ShareCommonT IO Unit := do let o1 := mkFoo1 10 true let o2 := mkFoo2 10 true let o3 := mkFoo2 10 false check $ ptrAddrUnsafe o1 != ptrAddrUnsafe o2 check $ ptrAddrUnsafe o1 != ptrAddrUnsafe o3 let o1 ← shareCommonM o1 let o2 ← shareCommonM o2 let o3 ← shareCommonM o3 check $ o1.x == 10 && o1.y == true && o1.z == true && o3.z == false && ptrAddrUnsafe o1 == ptrAddrUnsafe o2 && ptrAddrUnsafe o1 != ptrAddrUnsafe o3 IO.println o1.x pure () #eval tst3.run unsafe def tst4 : ShareCommonT IO Unit := do let x := ["hello"] let y := ["ello"].map (fun x => "h" ++ x) check $ ptrAddrUnsafe x != ptrAddrUnsafe y let x ← shareCommonM x let y ← shareCommonM y check $ ptrAddrUnsafe x == ptrAddrUnsafe y let z ← shareCommonM ["world"] let x ← shareCommonM x check $ ptrAddrUnsafe x == ptrAddrUnsafe y && ptrAddrUnsafe x != ptrAddrUnsafe z IO.println x IO.println y IO.println z #eval tst4.run @[noinline] def mkList1 (x : Nat) : List Nat := List.replicate x x @[noinline] def mkList2 (x : Nat) : List Nat := List.replicate x x @[noinline] def mkArray1 (x : Nat) : Array (List Nat) := #[ mkList1 x, mkList2 x, mkList2 (x+1) ] @[noinline] def mkArray2 (x : Nat) : Array (List Nat) := mkArray1 x unsafe def tst5 : ShareCommonT IO Unit := do let a := mkArray1 3 let b := mkArray2 3 let c := mkArray2 4 IO.println a IO.println b IO.println c check $ ptrAddrUnsafe a != ptrAddrUnsafe b && ptrAddrUnsafe a != ptrAddrUnsafe c && ptrAddrUnsafe a[0] != ptrAddrUnsafe a[1] && ptrAddrUnsafe a[0] != ptrAddrUnsafe a[2] && ptrAddrUnsafe b[0] != ptrAddrUnsafe b[1] && ptrAddrUnsafe c[0] != ptrAddrUnsafe c[1] let a ← shareCommonM a let b ← shareCommonM b let c ← shareCommonM c check $ ptrAddrUnsafe a == ptrAddrUnsafe b && ptrAddrUnsafe a != ptrAddrUnsafe c && ptrAddrUnsafe a[0] == ptrAddrUnsafe a[1] && ptrAddrUnsafe a[0] != ptrAddrUnsafe a[2] && ptrAddrUnsafe b[0] == ptrAddrUnsafe b[1] && ptrAddrUnsafe c[0] == ptrAddrUnsafe c[1] pure () #eval tst5.run @[noinline] def mkByteArray1 (x : Nat) : ByteArray := let r := ByteArray.empty let r := r.push x.toUInt8 let r := r.push (x+1).toUInt8 let r := r.push (x+2).toUInt8 r @[noinline] def mkByteArray2 (x : Nat) : ByteArray := mkByteArray1 x unsafe def tst6 (x : Nat) : ShareCommonT IO Unit := do let a := [mkByteArray1 x] let b := [mkByteArray2 x] let c := [mkByteArray2 (x+1)] IO.println a IO.println b IO.println c check $ ptrAddrUnsafe a != ptrAddrUnsafe b check $ ptrAddrUnsafe a != ptrAddrUnsafe c let a ← shareCommonM a let b ← shareCommonM b let c ← shareCommonM c check $ ptrAddrUnsafe a == ptrAddrUnsafe b check $ ptrAddrUnsafe a != ptrAddrUnsafe c pure () #eval (tst6 2).run