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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
4cfe13750872bc19d0bc773a54616a6be2fef69d | dc253be9829b840f15d96d986e0c13520b085033 | /colimit/omega_compact_sum.hlean | faa0db77308bb0995458dddd781468cb7ef331d3 | [
"Apache-2.0"
] | permissive | cmu-phil/Spectral | 4ce68e5c1ef2a812ffda5260e9f09f41b85ae0ea | 3b078f5f1de251637decf04bd3fc8aa01930a6b3 | refs/heads/master | 1,685,119,195,535 | 1,684,169,772,000 | 1,684,169,772,000 | 46,450,197 | 42 | 13 | null | 1,505,516,767,000 | 1,447,883,921,000 | Lean | UTF-8 | Lean | false | false | 6,662 | hlean | import .omega_compact ..homotopy.fwedge
open eq nat seq_colim is_trunc equiv is_equiv trunc sigma sum pi function algebra sigma.ops
variables {A A' : ℕ → Type} (f : seq_diagram A) (f' : seq_diagram A') {n : ℕ} (a : A n)
universe variable u
definition kshift_up (k : ℕ) (x : seq_colim f) : seq_colim (kshift_diag f k) :=
begin
induction x with n a n a,
{ apply ι' (kshift_diag f k) n, exact lrep f (le_add_left n k) a },
{ exact ap (ι _) (lrep_f f _ a ⬝ lrep_irrel f _ _ a ⬝ !f_lrep⁻¹) ⬝ !glue }
end
definition kshift_down [unfold 4] (k : ℕ) (x : seq_colim (kshift_diag f k)) : seq_colim f :=
begin
induction x with n a n a,
{ exact ι' f (k + n) a },
{ exact glue f a }
end
definition kshift_equiv_eq_kshift_up (k : ℕ) (a : A n) : kshift_equiv f k (ι f a) = kshift_up f k (ι f a) :=
begin
induction k with k p,
{ exact ap (ι _) !lrep_eq_transport⁻¹ },
{ exact sorry }
end
definition kshift_equiv2 [constructor] (k : ℕ) : seq_colim f ≃ seq_colim (kshift_diag f k) :=
begin
refine equiv_change_fun (kshift_equiv f k) _,
exact kshift_up f k,
intro x, induction x with n a n a,
{ exact kshift_equiv_eq_kshift_up f k a },
{ exact sorry }
end
definition kshift_equiv_inv_eq_kshift_down (k : ℕ) (a : A (k + n)) :
kshift_equiv_inv f k (ι' (kshift_diag f k) n a) = kshift_down f k (ι' (kshift_diag f k) n a) :=
begin
induction k with k p,
{ exact apd011 (ι' _) _ !pathover_tr⁻¹ᵒ },
{ exact sorry }
end
definition kshift_equiv_inv2 [constructor] (k : ℕ) : seq_colim (kshift_diag f k) ≃ seq_colim f :=
begin
refine equiv_change_fun (equiv_change_inv (kshift_equiv_inv f k) _) _,
{ exact kshift_up f k },
{ intro x, induction x with n a n a,
{ exact kshift_equiv_eq_kshift_up f k a },
{ exact sorry }},
{ exact kshift_down f k },
{ intro x, induction x with n a n a,
{ exact !kshift_equiv_inv_eq_kshift_down },
{ exact sorry }}
end
definition seq_colim_over_weakened_sequence [unfold 5] (x : seq_colim f) :
seq_colim_over (weakened_sequence f f') x ≃ seq_colim f' :=
begin
induction x with n a n a,
{ exact kshift_equiv_inv2 f' n },
{ apply equiv_pathover_inv, apply arrow_pathover_constant_left, intro x,
apply pathover_of_tr_eq, refine !seq_colim_over_glue ⬝ _, exact sorry }
end
definition seq_colim_prod' [constructor] : seq_colim (seq_diagram_prod f f') ≃ seq_colim f × seq_colim f' :=
calc
seq_colim (seq_diagram_prod f f') ≃ seq_colim (seq_diagram_sigma (weakened_sequence f f')) :
by exact seq_colim_equiv (λn, !sigma.equiv_prod⁻¹ᵉ) (λn a, idp)
... ≃ Σ(x : seq_colim f), seq_colim_over (weakened_sequence f f') x :
by exact (sigma_seq_colim_over_equiv _ (weakened_sequence f f'))⁻¹ᵉ
... ≃ Σ(x : seq_colim f), seq_colim f' :
by exact sigma_equiv_sigma_right (seq_colim_over_weakened_sequence f f')
... ≃ seq_colim f × seq_colim f' :
by exact sigma.equiv_prod (seq_colim f) (seq_colim f')
open prod prod.ops
example {a' : A' n} : seq_colim_prod' f f' (ι _ (a, a')) = (ι f a, ι f' a') := idp
definition seq_colim_prod_inv {a' : A' n} : (seq_colim_prod' f f')⁻¹ᵉ (ι f a, ι f' a') = (ι _ (a, a')) :=
begin
exact sorry
end
definition prod_seq_colim_of_seq_colim_prod (x : seq_colim (seq_diagram_prod f f')) :
seq_colim f × seq_colim f' :=
begin
induction x with n x n x,
{ exact (ι f x.1, ι f' x.2) },
{ exact prod_eq (glue f x.1) (glue f' x.2) }
end
definition seq_colim_prod [constructor] :
seq_colim (seq_diagram_prod f f') ≃ seq_colim f × seq_colim f' :=
begin
refine equiv_change_fun (seq_colim_prod' f f') _,
exact prod_seq_colim_of_seq_colim_prod f f',
intro x, induction x with n x n x,
{ reflexivity },
{ induction x with a a', apply eq_pathover, apply hdeg_square, esimp,
refine _ ⬝ !elim_glue⁻¹,
refine ap_compose ((sigma.equiv_prod (seq_colim f) (seq_colim f') ∘
sigma_equiv_sigma_right (seq_colim_over_weakened_sequence f f')) ∘
sigma_colim_of_colim_sigma (weakened_sequence f f')) _ _ ⬝ _,
refine ap02 _ (!elim_glue ⬝ !idp_con) ⬝ _,
refine !ap_compose ⬝ _, refine ap02 _ !elim_glue ⬝ _,
refine !ap_compose ⬝ _, esimp, refine ap02 _ !ap_sigma_functor_id_sigma_eq ⬝ _,
apply inj (prod_eq_equiv _ _), apply pair_eq,
{ exact !ap_compose' ⬝ !sigma_eq_pr1 ⬝ !prod_eq_pr1⁻¹ },
{ refine !ap_compose' ⬝ _ ⬝ !prod_eq_pr2⁻¹, esimp,
refine !sigma_eq_pr2_constant ⬝ _,
refine !eq_of_pathover_apo ⬝ _, exact sorry }}
end
local attribute equiv_of_omega_compact [constructor]
definition omega_compact_sum [instance] [constructor] {X Y : Type} [omega_compact.{_ u} X]
[omega_compact.{u u} Y] : omega_compact.{_ u} (X ⊎ Y) :=
begin
fapply omega_compact_of_equiv,
{ intro A f,
exact calc
seq_colim (seq_diagram_arrow_left f (X ⊎ Y))
≃ seq_colim (seq_diagram_prod (seq_diagram_arrow_left f X) (seq_diagram_arrow_left f Y)) :
by exact seq_colim_equiv (λn, !imp_prod_imp_equiv_sum_imp⁻¹ᵉ) (λn f, idp)
... ≃ seq_colim (seq_diagram_arrow_left f X) × seq_colim (seq_diagram_arrow_left f Y) :
by apply seq_colim_prod
... ≃ (X → seq_colim f) × (Y → seq_colim f) :
by exact prod_equiv_prod (equiv_of_omega_compact X f) (equiv_of_omega_compact Y f)
... ≃ ((X ⊎ Y) → seq_colim f) :
by exact !imp_prod_imp_equiv_sum_imp },
{ intros, induction x with x y: reflexivity },
{ intros, induction x with x y: apply hdeg_square,
{ refine ap_compose (((λz, arrow_colim_of_colim_arrow f z _) ∘ pr1) ∘
seq_colim_prod _ _) _ _ ⬝ _, refine ap02 _ (!elim_glue ⬝ !idp_con) ⬝ _,
refine !ap_compose ⬝ _, refine ap02 _ !elim_glue ⬝ _,
refine !ap_compose ⬝ _, refine ap02 _ !prod_eq_pr1 ⬝ !elim_glue },
{ refine ap_compose (((λz, arrow_colim_of_colim_arrow f z _) ∘ pr2) ∘
seq_colim_prod _ _) _ _ ⬝ _, refine ap02 _ (!elim_glue ⬝ !idp_con) ⬝ _,
refine !ap_compose ⬝ _, refine ap02 _ !elim_glue ⬝ _,
refine !ap_compose ⬝ _, refine ap02 _ !prod_eq_pr2 ⬝ !elim_glue }},
end
open wedge pointed circle
/- needs fwedge! -/
definition seq_diagram_fwedge (X : Type*) : seq_diagram (λn, @fwedge (A n) (λa, X)) :=
sorry f
definition seq_colim_fwedge_equiv (X : Type*) [is_trunc 1 X] :
seq_colim (seq_diagram_fwedge f X) ≃ @fwedge (seq_colim f) (λn, X) :=
sorry
definition not_omega_compact_fwedge_nat_circle : ¬(omega_compact.{0 0} (@fwedge ℕ (λn, S¹*))) :=
assume H,
sorry
|
f232b83168599677549c4a6dbef97a65c09cad41 | 7bf54883c04ff2856c37f76a79599ceb380c1996 | /non-mathlib/field_results.lean | ee675bc6a78bdc32f8d7d08043bc981d7e8a2bff | [] | no_license | anonymousLeanDocsHosting/lean-polynomials | 4094466bf19acc0d1a47b4cabb60d8c21ddb2b00 | 361ef4cb7b68ef47d43b85cfa2d13f2ea0a47613 | refs/heads/main | 1,691,112,899,935 | 1,631,819,522,000 | 1,631,819,522,000 | 405,448,439 | 0 | 2 | null | null | null | null | UTF-8 | Lean | false | false | 23,031 | lean | import field_definition
lemma mul_cancel (f : Type) [myfld f] (a b x : f) (x_ne_zero : x ≠ myfld.zero) : (a .* x) = (b .* x) -> a = b :=
begin
intros h,
have h1 : (a .* x) .* (myfld.reciprocal x x_ne_zero) = (b .* x) .* (myfld.reciprocal x x_ne_zero) ,
rw h,
rw <- myfld.mul_assoc at h1, rw <- myfld.mul_assoc at h1, rw myfld.mul_reciprocal x x_ne_zero at h1,
rw <- myfld.mul_one a at h1, rw <- myfld.mul_one b at h1, exact h1,
end
lemma add_cancel (f : Type) [myfld f] (a b x : f) : (a = b) -> (x .+ a) = (x .+ b) :=
begin
intros h, rw h,
end
lemma add_cancel_inv (f : Type) [myfld f] (a b x : f) : (a .+ x) = (b .+ x) -> (a = b) :=
begin
intros h,
have h1 : (a .+ x) .+ (.- x) = (b .+ x) .+ (.- x) ,
rw h, clear h, rename h1 h,
repeat {rw <- myfld.add_assoc at h}, repeat {rw myfld.add_negate x at h}, repeat {rw zero_simp at h},
exact h,
end
/- Start by proving various elementary notions about general fields.-/
lemma only_one_reciprocal (f : Type) [myfld f] (x : f) (a b : x ≠ myfld.zero) :
(myfld.reciprocal x a) = (myfld.reciprocal x b) :=
begin
have h1 : x .* (myfld.reciprocal x a) = myfld.one,
exact myfld.mul_reciprocal x a,
have h2 : (myfld.reciprocal x b) .* myfld.one = myfld.reciprocal x b,
exact simp_mul_one f (myfld.reciprocal x b),
rw <- h1 at h2,
end
@[simp] lemma mul_zero (f : Type) [myfld f] (x : f) : (myfld.zero : f) .* x = (myfld.zero : f) :=
begin
have h1 : (myfld.zero : f) .* x =
((myfld.zero : f) .* x) .+ (myfld.zero : f) ,
exact myfld.add_zero ((myfld.zero : f) .* x),
have h2 : myfld.zero = (myfld.zero .* x) .+ (.- (myfld.zero .* x)) ,
symmetry, exact myfld.add_negate (myfld.zero .* x),
rewrite [h2] at h1 {occs := occurrences.pos [3]},
rw myfld.add_assoc at h1, rw myfld.distrib at h1, rw <- myfld.add_zero myfld.zero at h1,
rw myfld.add_negate at h1, exact h1,
end
@[simp] lemma zero_mul (f : Type) [myfld f] (x : f) : x .* myfld.zero = myfld.zero :=
begin
rw myfld.mul_comm, exact mul_zero f x,
end
lemma only_one_reciprocal_alt (f : Type) [myfld f] (a b : f) (a_ne_zero : a ≠ myfld.zero) :
(a .* b = myfld.one) -> b = (myfld.reciprocal a a_ne_zero) :=
begin
intro h,
have h1 : (a .* b) .* (myfld.reciprocal a a_ne_zero) = myfld.one .* (myfld.reciprocal a a_ne_zero) ,
rw h,
clear h, rename h1 h,
rw myfld.mul_comm (a .* b) _ at h, rw myfld.mul_assoc at h,
rw myfld.mul_comm (myfld.reciprocal _ _) a at h, rw myfld.mul_reciprocal a a_ne_zero at h,
repeat {rw one_mul f _ at h}, exact h,
end
@[simp] lemma negate_zero_zero (f : Type) [myfld f] : .- myfld.zero = (myfld.zero : f) :=
begin
symmetry,
have h1 : ((myfld.zero : f) .+ (.- (myfld.zero : f))) = (myfld.zero : f),
exact (myfld.add_negate : ∀ (x : f), (x .+ (.- x)) = myfld.zero) (myfld.zero : f),
have h2 : ((myfld.zero : f) .+ (.- (myfld.zero : f))) =
.- (myfld.zero : f),
symmetry, exact zero_add f (.- (myfld.zero : f)),
rw h1 at h2, exact h2,
end
lemma negate_ne_zero (f : Type) [myfld f] (a : f) : (a ≠ myfld.zero) -> ((.- a) ≠ myfld.zero) :=
begin
intros h1 h2,
have h3 : myfld.zero .+ a = (.- a) .+ a,
rw h2,
rw simp_zero at h3, rw myfld.add_comm at h3, rw myfld.add_negate a at h3, cc,
end
@[simp] lemma recip_one_one (f : Type) [myfld f] (one_ne_zero : (myfld.one : f) ≠ (myfld.zero : f)) :
(myfld.reciprocal myfld.one one_ne_zero) = (myfld.one : f) :=
begin
have h1 : myfld.one .* (myfld.reciprocal myfld.one one_ne_zero) = myfld.one,
exact myfld.mul_reciprocal myfld.one (one_ne_zero : ((myfld.one : f) ≠ (myfld.zero : f))),
have h2 : myfld.one .* (myfld.reciprocal myfld.one one_ne_zero) = (myfld.reciprocal myfld.one one_ne_zero),
exact one_mul f (myfld.reciprocal myfld.one one_ne_zero),
rw h1 at h2, symmetry, exact h2,
end
@[simp] lemma double_negative (f : Type) [myfld f] (x : f) : .- .- x = x :=
begin
have h1 : x .+ (myfld.zero : f) = x,
symmetry, exact (myfld.add_zero : ∀ (x : f), x = x .+ (myfld.zero : f)) x,
have h2 : (.- x) .+ (.- .- x) =
(myfld.zero : f),
exact (myfld.add_negate : ∀ (x : f), (x .+ (.- x)) = (myfld.zero : f))
(.- x),
rw <- h2 at h1,
rw myfld.add_assoc at h1, rw myfld.add_negate at h1, rw <- zero_add f (.- (.- x)) at h1,
exact h1,
end
lemma reciprocal_ne_zero (f : Type) [myfld f] (x : f) (x_ne_zero : x ≠ myfld.zero) :
myfld.reciprocal x x_ne_zero ≠ myfld.zero :=
begin
have x_mul_zero : x .* myfld.zero = myfld.zero,
exact zero_mul f x,
intro recip_eq_zero,
have x_mul_recip_eq_zero : myfld.zero = x .* (myfld.reciprocal x x_ne_zero) ,
rw <- recip_eq_zero at x_mul_zero, rw [recip_eq_zero] at x_mul_zero {occs := occurrences.pos [2]},
symmetry, exact x_mul_zero,
rw [x_mul_recip_eq_zero] at x_mul_zero {occs := occurrences.pos [1]},
rw myfld.mul_reciprocal x x_ne_zero at x_mul_zero, rw <- myfld.mul_one x at x_mul_zero,
exact x_ne_zero x_mul_zero,
end
@[simp] lemma double_reciprocal (f : Type) [myfld f] (x : f) (x_ne_zero : x ≠ myfld.zero)
: (myfld.reciprocal (myfld.reciprocal x x_ne_zero) (reciprocal_ne_zero f x x_ne_zero)) = x :=
begin
have h1 : x .* (myfld.one : f) = x,
symmetry, exact myfld.mul_one x,
rw <- myfld.mul_reciprocal (myfld.reciprocal x x_ne_zero) (reciprocal_ne_zero f x x_ne_zero) at h1,
rw myfld.mul_assoc x (myfld.reciprocal x x_ne_zero) at h1,
rw myfld.mul_reciprocal x at h1, rw one_mul f (myfld.reciprocal (myfld.reciprocal x x_ne_zero) (reciprocal_ne_zero f x x_ne_zero)) at h1, exact h1,
end
lemma double_reciprocal_inv (f : Type) [myfld f] (x : f) (x_ne_zero : x ≠ myfld.zero) :
x = myfld.reciprocal (myfld.reciprocal x x_ne_zero) (reciprocal_ne_zero f x x_ne_zero) :=
begin
symmetry, exact double_reciprocal f x x_ne_zero,
end
@[simp] lemma mul_negate (f : Type) [myfld f] (x y : f) : .- x .* y =
.- (x .* y) :=
begin
have h1 : y .* (myfld.zero : f) = (myfld.zero : f),
rw myfld.mul_comm, exact mul_zero f y,
have h2 : (myfld.zero : f) = x .+ .- x ,
symmetry, exact myfld.add_negate x,
rw [h2] at h1 {occs := occurrences.pos [1]},
rw myfld.mul_comm at h1, rw <- myfld.distrib at h1,
have h3 : (.- (y .* x)) .+ myfld.zero = (.- (y .* x)),
symmetry, exact myfld.add_zero (.- (y .* x)),
rw <- h1 at h3, rw myfld.add_assoc at h3, rw myfld.add_comm (.- (y .* x)) (x .* y) at h3,
rw myfld.mul_comm y x at h3, rw myfld.add_negate at h3, rw <- zero_add f ((.- x) .* y) at h3,
exact h3,
end
@[simp] lemma mul_negate_alt_simp (f : Type) [myfld f] (x y : f) : (x .* (.- y)) = .- (x .* y) :=
begin
rw myfld.mul_comm, rw myfld.mul_comm x y, exact mul_negate f y x,
end
lemma mul_negate_alt (f : Type) [myfld f] (x y : f) : .- (x .* y) = (x .* (.- y)) :=
begin
symmetry, rw myfld.mul_comm, rw myfld.mul_comm x y, exact mul_negate f y x,
end
@[simp] lemma add_negate (f : Type) [myfld f] (x y : f) : (.- (x .+ y)) =
((.- x) .+ (.- y)) :=
begin
have h1 : ((x .+ y) .+ (.- (x .+ y))) = myfld.zero,
exact myfld.add_negate (x .+ y),
have h2 : (((.- x) .+ (.- y)) .+ myfld.zero) = ((.- x) .+ (.- y)),
symmetry, exact myfld.add_zero ((.- x) .+ (.- y)),
rw <- h1 at h2,
rw myfld.add_comm x y at h2, repeat {rw myfld.add_assoc _ _ _ at h2},
rw myfld.add_comm (((.- x) .+ (.- y)) .+ y) x at h2,
simp at h2, exact h2,
end
lemma mul_nonzero (f : Type) [myfld f] (x y : f) (x_ne_zero : x ≠ myfld.zero) (y_ne_zero : y ≠ myfld.zero) :
(x .* y) ≠ myfld.zero :=
begin
intro h,
have h1 : x .* (myfld.reciprocal x x_ne_zero) = myfld.one,
exact myfld.mul_reciprocal x x_ne_zero,
have h2 : y = y .* myfld.one,
exact myfld.mul_one y,
rw <- h1 at h2, rw myfld.mul_assoc _ _ _ at h2,
rw myfld.mul_comm y x at h2, rw h at h2,
rw mul_zero f _ at h2, exact y_ne_zero h2,
end
@[simp] lemma mul_two_reciprocals (f : Type) [myfld f] (x y : f) (x_ne_zero : x ≠ myfld.zero) (y_ne_zero : y ≠ myfld.zero) :
((myfld.reciprocal x x_ne_zero) .* (myfld.reciprocal y y_ne_zero)) =
(myfld.reciprocal (x .* y) (mul_nonzero f x y x_ne_zero y_ne_zero)) :=
begin
have h1 : (x .* y) .* (myfld.reciprocal (x .* y) (mul_nonzero f x y x_ne_zero y_ne_zero)) = myfld.one,
exact myfld.mul_reciprocal (x .* y) (mul_nonzero f x y x_ne_zero y_ne_zero),
have h2 : ((myfld.reciprocal x x_ne_zero) .* (myfld.reciprocal y y_ne_zero)) .* myfld.one =
(myfld.reciprocal x x_ne_zero) .* (myfld.reciprocal y y_ne_zero) ,
symmetry, exact myfld.mul_one ((myfld.reciprocal x x_ne_zero) .* (myfld.reciprocal y y_ne_zero)),
rw <- h1 at h2,
repeat {rw myfld.mul_assoc _ _ _ at h2},
rw myfld.mul_comm ((((myfld.reciprocal x x_ne_zero) .* (myfld.reciprocal y y_ne_zero)) .* x) .* y)
(myfld.reciprocal (x .* y) (mul_nonzero f x y x_ne_zero y_ne_zero)) at h2,
symmetry, simp at h2, exact h2,
end
lemma factor_1_ne_zero (f : Type) [myfld f] (x y : f) : (x .* y) ≠ myfld.zero -> x ≠ myfld.zero :=
begin
intro prod_n_zero, intro x_zero, rw x_zero at prod_n_zero, rw mul_zero f y at prod_n_zero,
have tmp : myfld.zero = myfld.zero,
refl, exact prod_n_zero tmp,
end
lemma factor_2_ne_zero (f : Type) [myfld f] (x y : f) : (x .* y) ≠ myfld.zero -> y ≠ myfld.zero :=
begin
rw myfld.mul_comm x y, exact factor_1_ne_zero f y x,
end
lemma split_reciprocal (f : Type) [myfld f] (x y : f) (xy_ne_zero : (x .* y ≠ myfld.zero)) :
(myfld.reciprocal (x .* y) xy_ne_zero) =
(myfld.reciprocal x (factor_1_ne_zero f x y xy_ne_zero)) .* (myfld.reciprocal y (factor_2_ne_zero f x y xy_ne_zero)) :=
begin
rw only_one_reciprocal f (x .* y) xy_ne_zero
(mul_nonzero f x y (factor_1_ne_zero f x y xy_ne_zero) (factor_2_ne_zero f x y xy_ne_zero)),
symmetry,
exact mul_two_reciprocals f x y (factor_1_ne_zero f x y xy_ne_zero) (factor_2_ne_zero f x y xy_ne_zero),
end
lemma cancel_from_reciprocal (f : Type) [myfld f] (x y : f) (xy_ne_zero : (x .* y ≠ myfld.zero)) :
((myfld.reciprocal (x .* y) xy_ne_zero) .* x) =
(myfld.reciprocal y (factor_2_ne_zero f x y xy_ne_zero)) :=
begin
rw split_reciprocal f x y xy_ne_zero,
rw myfld.mul_comm (myfld.reciprocal x _) (myfld.reciprocal y _), rw <- myfld.mul_assoc,
rw myfld.mul_comm (myfld.reciprocal x _) x, rw myfld.mul_reciprocal x _,
rw simp_mul_one f _,
end
lemma cancel_from_reciprocal_alt (f : Type) [myfld f] (x y : f) (xy_ne_zero : (x .* y ≠ myfld.zero)) :
(x .* (myfld.reciprocal (x .* y) xy_ne_zero)) =
(myfld.reciprocal y (factor_2_ne_zero f x y xy_ne_zero)) :=
begin
rw myfld.mul_comm x (myfld.reciprocal _ _), exact cancel_from_reciprocal f x y xy_ne_zero,
end
lemma add_two_reciprocals (f : Type) [myfld f] (x y : f) (x_ne_zero : x ≠ myfld.zero) (y_ne_zero : y ≠ myfld.zero) :
((myfld.reciprocal x x_ne_zero) .+ (myfld.reciprocal y y_ne_zero)) =
(x .+ y) .* (myfld.reciprocal (x .* y) (mul_nonzero f x y x_ne_zero y_ne_zero)) :=
begin
have h1 : (myfld.reciprocal x x_ne_zero) = y .* (myfld.reciprocal (x .* y) (mul_nonzero f x y x_ne_zero y_ne_zero)) ,
rw split_reciprocal f x y _, rw myfld.mul_comm (myfld.reciprocal x _) (myfld.reciprocal y _),
rw myfld.mul_assoc y _ _, rw myfld.mul_reciprocal y _, rw one_mul_simp f _,
have h2 : (myfld.reciprocal y y_ne_zero) = x .* (myfld.reciprocal (x .* y) (mul_nonzero f x y x_ne_zero y_ne_zero)) ,
rw split_reciprocal f x y _,
rw myfld.mul_assoc x _ _, rw myfld.mul_reciprocal x _, rw one_mul_simp f _,
rw h1, rw h2, clear h1, clear h2,
rw <- distrib_simp f y x _, rw myfld.add_comm y x,
end
lemma only_one_negation (f : Type) [myfld f] (a b : f) : a .+ b = myfld.zero -> b = .- a :=
begin
intros h, have h1 : (a .+ b) .+ (.- b) = .- b,
rw h, rw simp_zero,
rw <- myfld.add_assoc _ _ _ at h1, rw myfld.add_negate at h1, rw zero_simp at h1,
have h2 : .- (.- b) = .- a,
have triv : .- a = .- a, refl,
rw h1 at triv {occs := occurrences.pos [1]}, exact triv,
rw double_negative f _ at h2, exact h2,
end
/- We need this later.
By default Lean doesn't let us use rewrites on reciprocals.
This is because myfld.reciprocal takes both an element of the field and a proof that it is not zero.
Ergo, if we have (reciprocal x x_ne_zero) and try to rewrite it with the hypothesis x = y...
we end up with (reciprocal y x_ne_zero) which is nonsense and isn't allowed.
This lemma will let us get around that by utilizing the fact that x = y means that x ≠ 0 -> y ≠ 0.-/
lemma reciprocal_rewrite (f : Type) [myfld f] (x y : f) (equal_proof : x = y) (x_ne_zero : x ≠ myfld.zero) :
(myfld.reciprocal x x_ne_zero) = (myfld.reciprocal y (equal_ne_zero f x y equal_proof x_ne_zero)) :=
begin
have h1 : x .* (myfld.reciprocal x x_ne_zero) = myfld.one,
exact myfld.mul_reciprocal x x_ne_zero,
rw equal_proof at h1 {occs := occurrences.pos[1]},
have h2 : (myfld.reciprocal y (equal_ne_zero f x y equal_proof x_ne_zero)) =
(myfld.reciprocal y (equal_ne_zero f x y equal_proof x_ne_zero)) .* myfld.one,
exact myfld.mul_one (myfld.reciprocal y (equal_ne_zero f x y equal_proof x_ne_zero)),
rw <- h1 at h2,
rw myfld.mul_assoc at h2, rw myfld.mul_comm _ y at h2, rw myfld.mul_reciprocal y _ at h2,
rw one_mul f _ at h2, symmetry, exact h2,
end
/- We need this later and doing it here will end up saving time.-/
lemma assoc_tree (f : Type) [myfld f] (a b c d : f) :
((a .+ b) .+ (c .+ d)) = (a .+ ((b .+ c) .+ d)) :=
begin
rw <- myfld.add_assoc a b (c .+ d), rw myfld.add_assoc b c d,
end
lemma assoc_tree_mul (f : Type) [myfld f] (a b c d : f) :
((a .* b) .* (c .* d)) = (a .* ((b .* c) .* d)) :=
begin
rw <- myfld.mul_assoc a b (c .* d), rw myfld.mul_assoc b c d,
end
lemma multiply_two_squares (f : Type) [myfld f] (a b : f) :
(square f a) .* (square f b) = square f (a .* b) :=
begin
unfold square, rw myfld.mul_assoc (a .* a) b b,
rw <- myfld.mul_assoc a a b,
have tmp : (a .* (a .* b)) .* b = (a .* (b .* a)) .* b,
rw myfld.mul_comm a b,
rw tmp, clear tmp,
rw myfld.mul_assoc a b a, rw <- myfld.mul_assoc (a .* b) a b,
end
lemma mul_zero_by_reciprocal (f : Type) [myfld f] (a b : f) (a_ne_zero : a ≠ myfld.zero) :
b = myfld.zero <-> b .* (myfld.reciprocal a a_ne_zero) = myfld.zero :=
begin
split,
intros h, rw h, exact mul_zero f _,
intros h, have h1 : (b .* (myfld.reciprocal a a_ne_zero)) .* a = myfld.zero,
rw h, rw mul_zero,
rw <- myfld.mul_assoc at h1, rw myfld.mul_comm _ a at h1, rw myfld.mul_reciprocal a a_ne_zero at h1,
rw simp_mul_one at h1, exact h1,
end
lemma negate_zero (f : Type) [myfld f] (a : f) : a = myfld.zero <-> (.- a) = myfld.zero :=
begin
split, intros h,
have h1 : (.- a) .+ myfld.zero = myfld.zero,
rw h, rw myfld.add_comm, exact myfld.add_negate (myfld.zero),
rw <- myfld.add_zero (.- a) at h1, exact h1,
intros h, have h1 : .- .- a = myfld.zero, rw h, exact negate_zero_zero f,
rw double_negative at h1, exact h1,
end
lemma negate_zero_a (f : Type) [myfld f] (a : f) : a = myfld.zero -> (.- a) = myfld.zero :=
begin
rw negate_zero, cc,
end
lemma carry_term_across (f : Type) [myfld f] (a b x : f) : a .+ x = b <-> a = b .+ (.- x) :=
begin
split,
intros h,
have h1 : (a .+ x) .+ (.- x) = b .+ (.- x) ,
rw h,
rw <- myfld.add_assoc _ _ _ at h1, rw myfld.add_negate _ at h1, rw zero_simp f _ at h1, exact h1,
intros h,
have h1 : a .+ x = (b .+ (.- x)) .+ x,
rw h,
rw <- myfld.add_assoc _ _ _ at h1, rw myfld.add_comm (.- x) x at h1,
rw myfld.add_negate x at h1, rw zero_simp f _ at h1, exact h1,
end
lemma carry_term_across_alt (f : Type) [myfld f] (a b : f) : a = b <-> myfld.zero = b .+ (.- a) :=
begin
split,
intros h, rw <- h, rw myfld.add_negate,
intros h, rw myfld.add_zero a, rw h, rw myfld.add_comm b, rw myfld.add_assoc, rw myfld.add_negate, rw simp_zero,
end
lemma mul_both_sides (f : Type) [myfld f] (a b x : f) (x_ne_zero : x ≠ myfld.zero) :
a = b <-> (a .* x) = (b .* x) :=
begin
split,
intros h, rw h,
intros h, have h1 : (a .* x) .* (myfld.reciprocal x x_ne_zero) = (b .* x) .* (myfld.reciprocal x x_ne_zero) ,
rw h,
repeat {rw <- myfld.mul_assoc at h1}, repeat {rw myfld.mul_reciprocal at h1}, repeat {rw simp_mul_one at h1},
exact h1,
end
lemma mul_both_sides_ne (f : Type) [myfld f] (a b x : f) : a ≠ b -> x ≠ myfld.zero -> a .* x ≠ b .* x :=
begin
intros h1 x_ne_zero h2,
have cancel : a = b,
exact mul_cancel f a b x x_ne_zero h2,
cc,
end
lemma carry_term_across_ne (f : Type) [myfld f] (a b x : f) : a .+ x ≠ b <-> a ≠ b .+ (.- x) :=
begin
split,
intros h1 h2, rw <- carry_term_across f _ _ _ at h2, cc,
intros h1 h2, rw carry_term_across f _ _ _ at h2, cc,
end
def cubed (f : Type) [myfld f] : f -> f
| a := (a .* (a .* a))
def cube_of_product (f : Type) [myfld f] (a b : f) : (cubed f (a .* b)) = (cubed f a) .* (cubed f b) :=
begin
unfold cubed, simp only [mul_assoc f a _ _, myfld.mul_comm b a, myfld.mul_assoc b _ _],
end
def cube_nonzero (f : Type) [myfld f] (a : f) : a ≠ myfld.zero -> (cubed f a) ≠ myfld.zero :=
begin
intros a_ne_zero, unfold cubed, have a_sq_ne_zero : a .* a ≠ myfld.zero,
exact mul_nonzero f a a a_ne_zero a_ne_zero,
exact mul_nonzero f a (a .* a) a_ne_zero a_sq_ne_zero,
end
def cube_of_negative (f : Type) [myfld f] (a : f) : (cubed f (.- a)) = .- (cubed f a) :=
begin
unfold cubed, rw mul_negate_alt_simp f _ _, rw mul_negate f _ _, rw mul_negate f _ _, rw double_negative f _,
end
def cube_of_reciprocal (f : Type) [myfld f] (a : f) (a_ne_zero : a ≠ myfld.zero) :
(cubed f (myfld.reciprocal a a_ne_zero)) = myfld.reciprocal (cubed f a) (cube_nonzero f a a_ne_zero) :=
begin
unfold cubed, simp only [mul_two_reciprocals, only_one_reciprocal],
end
def factorized_cubic_expression (f : Type) [myfld f] (x a b c : f) : f :=
(((x .+ (.- a)) .* (x .+ (.- b))) .* (x .+ (.- c)))
lemma ne_diff_not_zero (f : Type) [myfld f] (x y : f) : x ≠ y -> x .+ (.- y) ≠ myfld.zero :=
begin
intros ne h,
have tmp : (x .+ (.- y)) .+ y = myfld.zero .+ y,
rw h, clear h, rename tmp h,
rw <- zero_add f y at h, rw <- myfld.add_assoc at h, rw myfld.add_comm _ y at h, rw myfld.add_negate y at h,
rw <- myfld.add_zero x at h, cc,
end
lemma multiply_out_cubic (f : Type) [myfld f] (x a b c : f) : (factorized_cubic_expression f x a b c) =
((cubed f x) .+ ((.- ((a .+ (b .+ c)) .* (x .* x))) .+ ((x .* (((a .* b) .+ (a .* c)) .+ (b .* c))) .+ (.- (a .* (b .* c)))))) :=
begin
unfold cubed, unfold factorized_cubic_expression,
simp only [mul_negate, mul_negate_alt_simp, distrib_simp, distrib_simp_alt],
repeat {rw <- myfld.mul_assoc},
simp only [myfld.mul_comm x _, myfld.mul_assoc _ x _, myfld.mul_assoc _ x x],
repeat {rw myfld.mul_assoc}, repeat {rw double_negative},
repeat {rw <- myfld.add_assoc}, repeat {rw myfld.add_comm ((a .* b) .* x) },
repeat {rw myfld.add_assoc}, repeat {rw add_negate}, repeat {rw <- myfld.add_assoc},
end
/- Ergo we have demonstrated that if a field has a notion of cube roots (and a notion of square roots) then
every nonzero entry must have exactly three different cube roots.-/
/- (a + b) (a - b) = a^2 - b^2
Well-known, easy to prove, and useful.-/
lemma difference_of_squares (f : Type) [myfld f] (a b : f) :
(a .+ b) .* (a .+ (.- b)) = (square f a) .+ (.- (square f b)) :=
begin
unfold square, rw distrib_simp f _ _ _, repeat {rw distrib_simp_alt f _ _ _},
repeat {rw <- mul_negate_alt f _ _}, rw myfld.mul_comm b a,
rw myfld.add_assoc _ (a .* b) _, rw <- myfld.add_assoc _ _ (a .* b),
rw myfld.add_comm (.- (a .* b)) (a .* b),
rw myfld.add_negate _, rw <- myfld.add_zero (a .* a),
end
/- This situation arises in one of my proof later and for some reason I can't get simp
to do it automatically. It be like that sometimes. -/
lemma mul_complex (f : Type) [myfld f] (a b c d : f) :
(a .* (b .* c)) .* (a .* (d .* c)) =
((a .* c) .* (a .* c)) .* (b .* d) :=
begin
repeat {rw <- myfld.mul_assoc _ _ _}, rw myfld.mul_comm c (b .* d),
rw <- myfld.mul_assoc b d c, repeat {rw myfld.mul_assoc _ _ _},
rw myfld.mul_comm (_ .* _) b, repeat {rw myfld.mul_assoc _ _ _}, rw myfld.mul_comm b a,
end
lemma subtract_ne (f : Type) [myfld f] (a b : f) :
a ≠ b -> a .+ (.- b) ≠ myfld.zero :=
begin
intros h1 h2,
have h3 : (a .+ (.- b)) .+ b = (myfld.zero .+ b),
rw h2,
rw simp_zero at h3, rw <- myfld.add_assoc at h3, rw myfld.add_comm (.- b) b at h3,
rw myfld.add_negate b at h3, rw zero_simp at h3, cc,
end
lemma move_negation_ne (f : Type) [myfld f] (a b : f) :
a ≠ (.- b) <-> (.- a) ≠ b :=
begin
split,
intros h1 h2, rw <- h2 at h1, rw double_negative at h1, cc,
intros h1 h2, rw h2 at h1, rw double_negative at h1, cc,
end
lemma move_across (f : Type) [myfld f] (a b : f) :
a .+ b = myfld.zero <-> (.- a) = b :=
begin
split,
intros h, have h1 : (a .+ b) .+ (.- a) = myfld.zero .+ (.- a) ,
rw h,
rw simp_zero at h1, rw <- myfld.add_assoc at h1, rw myfld.add_comm b _ at h1,
rw myfld.add_assoc at h1, rw myfld.add_negate at h1, rw simp_zero at h1, cc,
intros h, rw <- h, exact myfld.add_negate a,
end
@[simp] lemma square_negate (f : Type) [myfld f] (a : f) : (square f (.- a)) = (square f a) :=
begin
unfold square, rw mul_negate, rw mul_negate_alt_simp, rw double_negative,
end
@[simp] lemma cube_negate (f : Type) [myfld f] (a : f) : (cubed f (.- a)) = .- (cubed f a) :=
begin
unfold cubed, repeat {rw mul_negate}, repeat {rw mul_negate_alt_simp}, rw double_negative,
end
lemma mul_two_squares (f : Type) [myfld f] (a b : f) : (square f a) .* (square f b) =
square f (a .* b) :=
begin
unfold square,repeat {rw myfld.mul_assoc}, rw myfld.mul_comm (a .* b) a, repeat {rw myfld.mul_assoc},
end
lemma square_ne_zero (f : Type) [myfld f] (a : f) (a_ne_zero : a ≠ myfld.zero) : (square f a) ≠ myfld.zero :=
begin
unfold square, exact mul_nonzero f a a a_ne_zero a_ne_zero,
end
lemma reciprocal_squared (f : Type) [myfld f] (a : f) (a_ne_zero : a ≠ myfld.zero) :
(square f (myfld.reciprocal a a_ne_zero)) = (myfld.reciprocal (square f a) (square_ne_zero f a a_ne_zero)) :=
begin
unfold square, rw mul_two_reciprocals f a a a_ne_zero a_ne_zero,
end
|
dab0703560f4203470d6fa95618e0eb33aa84996 | 82e44445c70db0f03e30d7be725775f122d72f3e | /src/category_theory/skeletal.lean | 49ef674450f9e24426eb06d71eb4f5bf6349d6c4 | [
"Apache-2.0"
] | permissive | stjordanis/mathlib | 51e286d19140e3788ef2c470bc7b953e4991f0c9 | 2568d41bca08f5d6bf39d915434c8447e21f42ee | refs/heads/master | 1,631,748,053,501 | 1,627,938,886,000 | 1,627,938,886,000 | 228,728,358 | 0 | 0 | Apache-2.0 | 1,576,630,588,000 | 1,576,630,587,000 | null | UTF-8 | Lean | false | false | 10,880 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import category_theory.isomorphism_classes
import category_theory.thin
/-!
# Skeleton of a category
Define skeletal categories as categories in which any two isomorphic objects are equal.
Construct the skeleton of an arbitrary category by taking isomorphism classes, and show it is a
skeleton of the original category.
In addition, construct the skeleton of a thin category as a partial ordering, and (noncomputably)
show it is a skeleton of the original category. The advantage of this special case being handled
separately is that lemmas and definitions about orderings can be used directly, for example for the
subobject lattice. In addition, some of the commutative diagrams about the functors commute
definitionally on the nose which is convenient in practice.
-/
universes v₁ v₂ v₃ u₁ u₂ u₃
namespace category_theory
open category
variables (C : Type u₁) [category.{v₁} C]
variables (D : Type u₂) [category.{v₂} D]
variables {E : Type u₃} [category.{v₃} E]
/-- A category is skeletal if isomorphic objects are equal. -/
def skeletal : Prop := ∀ ⦃X Y : C⦄, is_isomorphic X Y → X = Y
/--
`is_skeleton_of C D F` says that `F : D ⥤ C` exhibits `D` as a skeletal full subcategory of `C`,
in particular `F` is a (strong) equivalence and `D` is skeletal.
-/
structure is_skeleton_of (F : D ⥤ C) :=
(skel : skeletal D)
(eqv : is_equivalence F)
local attribute [instance] is_isomorphic_setoid
variables {C D}
/-- If `C` is thin and skeletal, then any naturally isomorphic functors to `C` are equal. -/
lemma functor.eq_of_iso {F₁ F₂ : D ⥤ C} [∀ X Y : C, subsingleton (X ⟶ Y)] (hC : skeletal C)
(hF : F₁ ≅ F₂) : F₁ = F₂ :=
functor.ext (λ X, hC ⟨hF.app X⟩) (λ _ _ _, subsingleton.elim _ _)
/--
If `C` is thin and skeletal, `D ⥤ C` is skeletal.
`category_theory.functor_thin` shows it is thin also.
-/
lemma functor_skeletal [∀ X Y : C, subsingleton (X ⟶ Y)] (hC : skeletal C) : skeletal (D ⥤ C) :=
λ F₁ F₂ h, h.elim (functor.eq_of_iso hC)
variables (C D)
/--
Construct the skeleton category as the induced category on the isomorphism classes, and derive
its category structure.
-/
@[derive category]
def skeleton : Type u₁ := induced_category C quotient.out
instance [inhabited C] : inhabited (skeleton C) := ⟨⟦default C⟧⟩
/-- The functor from the skeleton of `C` to `C`. -/
@[simps, derive [full, faithful]]
noncomputable def from_skeleton : skeleton C ⥤ C := induced_functor _
instance : ess_surj (from_skeleton C) :=
{ mem_ess_image := λ X, ⟨quotient.mk X, quotient.mk_out X⟩ }
noncomputable instance : is_equivalence (from_skeleton C) :=
equivalence.of_fully_faithfully_ess_surj (from_skeleton C)
/-- The equivalence between the skeleton and the category itself. -/
noncomputable def skeleton_equivalence : skeleton C ≌ C :=
(from_skeleton C).as_equivalence
lemma skeleton_skeletal : skeletal (skeleton C) :=
begin
rintro X Y ⟨h⟩,
have : X.out ≈ Y.out := ⟨(from_skeleton C).map_iso h⟩,
simpa using quotient.sound this,
end
/-- The `skeleton` of `C` given by choice is a skeleton of `C`. -/
noncomputable def skeleton_is_skeleton : is_skeleton_of C (skeleton C) (from_skeleton C) :=
{ skel := skeleton_skeletal C,
eqv := from_skeleton.is_equivalence C }
section
variables {C D}
/--
Two categories which are categorically equivalent have skeletons with equivalent objects.
-/
noncomputable
def equivalence.skeleton_equiv (e : C ≌ D) : skeleton C ≃ skeleton D :=
let f := ((skeleton_equivalence C).trans e).trans (skeleton_equivalence D).symm in
{ to_fun := f.functor.obj,
inv_fun := f.inverse.obj,
left_inv := λ X, skeleton_skeletal C ⟨(f.unit_iso.app X).symm⟩,
right_inv := λ Y, skeleton_skeletal D ⟨(f.counit_iso.app Y)⟩, }
end
/--
Construct the skeleton category by taking the quotient of objects. This construction gives a
preorder with nice definitional properties, but is only really appropriate for thin categories.
If your original category is not thin, you probably want to be using `skeleton` instead of this.
-/
def thin_skeleton : Type u₁ := quotient (is_isomorphic_setoid C)
instance inhabited_thin_skeleton [inhabited C] : inhabited (thin_skeleton C) :=
⟨quotient.mk (default _)⟩
instance thin_skeleton.preorder : preorder (thin_skeleton C) :=
{ le := quotient.lift₂ (λ X Y, nonempty (X ⟶ Y))
begin
rintros _ _ _ _ ⟨i₁⟩ ⟨i₂⟩,
exact propext ⟨nonempty.map (λ f, i₁.inv ≫ f ≫ i₂.hom),
nonempty.map (λ f, i₁.hom ≫ f ≫ i₂.inv)⟩,
end,
le_refl :=
begin
refine quotient.ind (λ a, _),
exact ⟨𝟙 _⟩,
end,
le_trans := λ a b c, quotient.induction_on₃ a b c $ λ A B C, nonempty.map2 (≫) }
/-- The functor from a category to its thin skeleton. -/
@[simps]
def to_thin_skeleton : C ⥤ thin_skeleton C :=
{ obj := quotient.mk,
map := λ X Y f, hom_of_le (nonempty.intro f) }
/-!
The constructions here are intended to be used when the category `C` is thin, even though
some of the statements can be shown without this assumption.
-/
namespace thin_skeleton
/-- The thin skeleton is thin. -/
instance thin {X Y : thin_skeleton C} : subsingleton (X ⟶ Y) :=
⟨by { rintros ⟨⟨f₁⟩⟩ ⟨⟨f₂⟩⟩, refl }⟩
variables {C} {D}
/-- A functor `C ⥤ D` computably lowers to a functor `thin_skeleton C ⥤ thin_skeleton D`. -/
@[simps]
def map (F : C ⥤ D) : thin_skeleton C ⥤ thin_skeleton D :=
{ obj := quotient.map F.obj $ λ X₁ X₂ ⟨hX⟩, ⟨F.map_iso hX⟩,
map := λ X Y, quotient.rec_on_subsingleton₂ X Y $
λ x y k, hom_of_le (k.le.elim (λ t, ⟨F.map t⟩)) }
lemma comp_to_thin_skeleton (F : C ⥤ D) : F ⋙ to_thin_skeleton D = to_thin_skeleton C ⋙ map F :=
rfl
/-- Given a natural transformation `F₁ ⟶ F₂`, induce a natural transformation `map F₁ ⟶ map F₂`.-/
def map_nat_trans {F₁ F₂ : C ⥤ D} (k : F₁ ⟶ F₂) : map F₁ ⟶ map F₂ :=
{ app := λ X, quotient.rec_on_subsingleton X (λ x, ⟨⟨⟨k.app x⟩⟩⟩) }
-- TODO: state the lemmas about what happens when you compose with `to_thin_skeleton`
/-- A functor `C ⥤ D ⥤ E` computably lowers to a functor
`thin_skeleton C ⥤ thin_skeleton D ⥤ thin_skeleton E` -/
@[simps]
def map₂ (F : C ⥤ D ⥤ E) :
thin_skeleton C ⥤ thin_skeleton D ⥤ thin_skeleton E :=
{ obj := λ x,
{ obj := λ y, quotient.map₂ (λ X Y, (F.obj X).obj Y)
(λ X₁ X₂ ⟨hX⟩ Y₁ Y₂ ⟨hY⟩, ⟨(F.obj X₁).map_iso hY ≪≫ (F.map_iso hX).app Y₂⟩) x y,
map := λ y₁ y₂, quotient.rec_on_subsingleton x $
λ X, quotient.rec_on_subsingleton₂ y₁ y₂ $
λ Y₁ Y₂ hY, hom_of_le (hY.le.elim (λ g, ⟨(F.obj X).map g⟩)) },
map := λ x₁ x₂, quotient.rec_on_subsingleton₂ x₁ x₂ $
λ X₁ X₂ f,
{ app := λ y, quotient.rec_on_subsingleton y
(λ Y, hom_of_le (f.le.elim (λ f', ⟨(F.map f').app Y⟩))) } }
variables (C)
section
variables [∀ X Y : C, subsingleton (X ⟶ Y)]
instance to_thin_skeleton_faithful : faithful (to_thin_skeleton C) := {}
/-- Use `quotient.out` to create a functor out of the thin skeleton. -/
@[simps]
noncomputable def from_thin_skeleton : thin_skeleton C ⥤ C :=
{ obj := quotient.out,
map := λ x y, quotient.rec_on_subsingleton₂ x y $
λ X Y f,
(nonempty.some (quotient.mk_out X)).hom
≫ f.le.some
≫ (nonempty.some (quotient.mk_out Y)).inv }
noncomputable instance from_thin_skeleton_equivalence : is_equivalence (from_thin_skeleton C) :=
{ inverse := to_thin_skeleton C,
counit_iso := nat_iso.of_components (λ X, (nonempty.some (quotient.mk_out X))) (by tidy),
unit_iso :=
nat_iso.of_components
(λ x, quotient.rec_on_subsingleton x
(λ X, eq_to_iso (quotient.sound ⟨(nonempty.some (quotient.mk_out X)).symm⟩)))
(by tidy) }
/-- The equivalence between the thin skeleton and the category itself. -/
noncomputable def equivalence : thin_skeleton C ≌ C :=
(from_thin_skeleton C).as_equivalence
variables {C}
lemma equiv_of_both_ways {X Y : C} (f : X ⟶ Y) (g : Y ⟶ X) : X ≈ Y :=
⟨iso_of_both_ways f g⟩
instance thin_skeleton_partial_order : partial_order (thin_skeleton C) :=
{ le_antisymm := quotient.ind₂
begin
rintros _ _ ⟨f⟩ ⟨g⟩,
apply quotient.sound (equiv_of_both_ways f g),
end,
..category_theory.thin_skeleton.preorder C }
lemma skeletal : skeletal (thin_skeleton C) :=
λ X Y, quotient.induction_on₂ X Y $ λ x y h, h.elim $ λ i, i.1.le.antisymm i.2.le
lemma map_comp_eq (F : E ⥤ D) (G : D ⥤ C) : map (F ⋙ G) = map F ⋙ map G :=
functor.eq_of_iso skeletal $
nat_iso.of_components (λ X, quotient.rec_on_subsingleton X (λ x, iso.refl _)) (by tidy)
lemma map_id_eq : map (𝟭 C) = 𝟭 (thin_skeleton C) :=
functor.eq_of_iso skeletal $
nat_iso.of_components (λ X, quotient.rec_on_subsingleton X (λ x, iso.refl _)) (by tidy)
lemma map_iso_eq {F₁ F₂ : D ⥤ C} (h : F₁ ≅ F₂) : map F₁ = map F₂ :=
functor.eq_of_iso skeletal { hom := map_nat_trans h.hom, inv := map_nat_trans h.inv }
/-- `from_thin_skeleton C` exhibits the thin skeleton as a skeleton. -/
noncomputable def thin_skeleton_is_skeleton : is_skeleton_of C (thin_skeleton C)
(from_thin_skeleton C) :=
{ skel := skeletal,
eqv := thin_skeleton.from_thin_skeleton_equivalence C }
noncomputable instance is_skeleton_of_inhabited :
inhabited (is_skeleton_of C (thin_skeleton C) (from_thin_skeleton C)) :=
⟨thin_skeleton_is_skeleton⟩
end
variables {C}
/-- An adjunction between thin categories gives an adjunction between their thin skeletons. -/
def lower_adjunction
(R : D ⥤ C) (L : C ⥤ D) (h : L ⊣ R) :
thin_skeleton.map L ⊣ thin_skeleton.map R :=
adjunction.mk_of_unit_counit
{ unit :=
{ app := λ X,
begin
letI := is_isomorphic_setoid C,
refine quotient.rec_on_subsingleton X (λ x, hom_of_le ⟨h.unit.app x⟩),
-- TODO: make quotient.rec_on_subsingleton' so the letI isn't needed
end },
counit :=
{ app := λ X,
begin
letI := is_isomorphic_setoid D,
refine quotient.rec_on_subsingleton X (λ x, hom_of_le ⟨h.counit.app x⟩),
end } }
end thin_skeleton
open thin_skeleton
section
variables {C} {α : Type*} [partial_order α]
/--
When `e : C ≌ α` is a categorical equivalence from a thin category `C` to some partial order `α`,
the `thin_skeleton C` is order isomorphic to `α`.
-/
noncomputable
def equivalence.thin_skeleton_order_iso
[∀ X Y : C, subsingleton (X ⟶ Y)] (e : C ≌ α) : thin_skeleton C ≃o α :=
((thin_skeleton.equivalence C).trans e).to_order_iso
end
end category_theory
|
c431a56e51ad55f55a48fb7049cad369ff306fe2 | 94e33a31faa76775069b071adea97e86e218a8ee | /src/linear_algebra/tensor_product.lean | ca7ac3854b5197eab86dcc4ddad265041e009c90 | [
"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 | 42,803 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Mario Carneiro
-/
import group_theory.congruence
import algebra.module.submodule.bilinear
/-!
# Tensor product of modules over commutative semirings.
This file constructs the tensor product of modules over commutative semirings. Given a semiring
`R` and modules over it `M` and `N`, the standard construction of the tensor product is
`tensor_product R M N`. It is also a module over `R`.
It comes with a canonical bilinear map `M → N → tensor_product R M N`.
Given any bilinear map `M → N → P`, there is a unique linear map `tensor_product R M N → P` whose
composition with the canonical bilinear map `M → N → tensor_product R M N` is the given bilinear
map `M → N → P`.
We start by proving basic lemmas about bilinear maps.
## Notations
This file uses the localized notation `M ⊗ N` and `M ⊗[R] N` for `tensor_product R M N`, as well
as `m ⊗ₜ n` and `m ⊗ₜ[R] n` for `tensor_product.tmul R m n`.
## Tags
bilinear, tensor, tensor product
-/
section semiring
variables {R : Type*} [comm_semiring R]
variables {R' : Type*} [monoid R']
variables {R'' : Type*} [semiring R'']
variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*}
variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] [add_comm_monoid Q]
[add_comm_monoid S]
variables [module R M] [module R N] [module R P] [module R Q] [module R S]
variables [distrib_mul_action R' M]
variables [module R'' M]
include R
variables (M N)
namespace tensor_product
section
-- open free_add_monoid
variables (R)
/-- The relation on `free_add_monoid (M × N)` that generates a congruence whose quotient is
the tensor product. -/
inductive eqv : free_add_monoid (M × N) → free_add_monoid (M × N) → Prop
| of_zero_left : ∀ n : N, eqv (free_add_monoid.of (0, n)) 0
| of_zero_right : ∀ m : M, eqv (free_add_monoid.of (m, 0)) 0
| of_add_left : ∀ (m₁ m₂ : M) (n : N), eqv
(free_add_monoid.of (m₁, n) + free_add_monoid.of (m₂, n)) (free_add_monoid.of (m₁ + m₂, n))
| of_add_right : ∀ (m : M) (n₁ n₂ : N), eqv
(free_add_monoid.of (m, n₁) + free_add_monoid.of (m, n₂)) (free_add_monoid.of (m, n₁ + n₂))
| of_smul : ∀ (r : R) (m : M) (n : N), eqv
(free_add_monoid.of (r • m, n)) (free_add_monoid.of (m, r • n))
| add_comm : ∀ x y, eqv (x + y) (y + x)
end
end tensor_product
variables (R)
/-- The tensor product of two modules `M` and `N` over the same commutative semiring `R`.
The localized notations are `M ⊗ N` and `M ⊗[R] N`, accessed by `open_locale tensor_product`. -/
def tensor_product : Type* :=
(add_con_gen (tensor_product.eqv R M N)).quotient
variables {R}
localized "infix ` ⊗ `:100 := tensor_product _" in tensor_product
localized "notation M ` ⊗[`:100 R `] `:0 N:100 := tensor_product R M N" in tensor_product
namespace tensor_product
section module
instance : add_zero_class (M ⊗[R] N) :=
{ .. (add_con_gen (tensor_product.eqv R M N)).add_monoid }
instance : add_comm_semigroup (M ⊗[R] N) :=
{ add_comm := λ x y, add_con.induction_on₂ x y $ λ x y, quotient.sound' $
add_con_gen.rel.of _ _ $ eqv.add_comm _ _,
.. (add_con_gen (tensor_product.eqv R M N)).add_monoid }
instance : inhabited (M ⊗[R] N) := ⟨0⟩
variables (R) {M N}
/-- The canonical function `M → N → M ⊗ N`. The localized notations are `m ⊗ₜ n` and `m ⊗ₜ[R] n`,
accessed by `open_locale tensor_product`. -/
def tmul (m : M) (n : N) : M ⊗[R] N := add_con.mk' _ $ free_add_monoid.of (m, n)
variables {R}
infix ` ⊗ₜ `:100 := tmul _
notation x ` ⊗ₜ[`:100 R `] `:0 y:100 := tmul R x y
@[elab_as_eliminator]
protected theorem induction_on
{C : (M ⊗[R] N) → Prop}
(z : M ⊗[R] N)
(C0 : C 0)
(C1 : ∀ {x y}, C $ x ⊗ₜ[R] y)
(Cp : ∀ {x y}, C x → C y → C (x + y)) : C z :=
add_con.induction_on z $ λ x, free_add_monoid.rec_on x C0 $ λ ⟨m, n⟩ y ih,
by { rw add_con.coe_add, exact Cp C1 ih }
variables (M)
@[simp] lemma zero_tmul (n : N) : (0 : M) ⊗ₜ[R] n = 0 :=
quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero_left _
variables {M}
lemma add_tmul (m₁ m₂ : M) (n : N) : (m₁ + m₂) ⊗ₜ n = m₁ ⊗ₜ n + m₂ ⊗ₜ[R] n :=
eq.symm $ quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_add_left _ _ _
variables (N)
@[simp] lemma tmul_zero (m : M) : m ⊗ₜ[R] (0 : N) = 0 :=
quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero_right _
variables {N}
lemma tmul_add (m : M) (n₁ n₂ : N) : m ⊗ₜ (n₁ + n₂) = m ⊗ₜ n₁ + m ⊗ₜ[R] n₂ :=
eq.symm $ quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_add_right _ _ _
section
variables (R R' M N)
/--
A typeclass for `has_smul` structures which can be moved across a tensor product.
This typeclass is generated automatically from a `is_scalar_tower` instance, but exists so that
we can also add an instance for `add_comm_group.int_module`, allowing `z •` to be moved even if
`R` does not support negation.
Note that `module R' (M ⊗[R] N)` is available even without this typeclass on `R'`; it's only
needed if `tensor_product.smul_tmul`, `tensor_product.smul_tmul'`, or `tensor_product.tmul_smul` is
used.
-/
class compatible_smul [distrib_mul_action R' N] :=
(smul_tmul : ∀ (r : R') (m : M) (n : N), (r • m) ⊗ₜ n = m ⊗ₜ[R] (r • n))
end
/-- Note that this provides the default `compatible_smul R R M N` instance through
`mul_action.is_scalar_tower.left`. -/
@[priority 100]
instance compatible_smul.is_scalar_tower
[has_smul R' R] [is_scalar_tower R' R M] [distrib_mul_action R' N] [is_scalar_tower R' R N] :
compatible_smul R R' M N :=
⟨λ r m n, begin
conv_lhs {rw ← one_smul R m},
conv_rhs {rw ← one_smul R n},
rw [←smul_assoc, ←smul_assoc],
exact (quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_smul _ _ _),
end⟩
/-- `smul` can be moved from one side of the product to the other .-/
lemma smul_tmul [distrib_mul_action R' N] [compatible_smul R R' M N] (r : R') (m : M) (n : N) :
(r • m) ⊗ₜ n = m ⊗ₜ[R] (r • n) :=
compatible_smul.smul_tmul _ _ _
/-- Auxiliary function to defining scalar multiplication on tensor product. -/
def smul.aux {R' : Type*} [has_smul R' M] (r : R') : free_add_monoid (M × N) →+ M ⊗[R] N :=
free_add_monoid.lift $ λ p : M × N, (r • p.1) ⊗ₜ p.2
theorem smul.aux_of {R' : Type*} [has_smul R' M] (r : R') (m : M) (n : N) :
smul.aux r (free_add_monoid.of (m, n)) = (r • m) ⊗ₜ[R] n :=
rfl
variables [smul_comm_class R R' M]
variables [smul_comm_class R R'' M]
/-- Given two modules over a commutative semiring `R`, if one of the factors carries a
(distributive) action of a second type of scalars `R'`, which commutes with the action of `R`, then
the tensor product (over `R`) carries an action of `R'`.
This instance defines this `R'` action in the case that it is the left module which has the `R'`
action. Two natural ways in which this situation arises are:
* Extension of scalars
* A tensor product of a group representation with a module not carrying an action
Note that in the special case that `R = R'`, since `R` is commutative, we just get the usual scalar
action on a tensor product of two modules. This special case is important enough that, for
performance reasons, we define it explicitly below. -/
instance left_has_smul : has_smul R' (M ⊗[R] N) :=
⟨λ r, (add_con_gen (tensor_product.eqv R M N)).lift (smul.aux r : _ →+ M ⊗[R] N) $
add_con.add_con_gen_le $ λ x y hxy, match x, y, hxy with
| _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, smul.aux_of, smul_zero, zero_tmul]
| _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, smul.aux_of, tmul_zero]
| _, _, (eqv.of_add_left m₁ m₂ n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, smul.aux_of, smul_add, add_tmul]
| _, _, (eqv.of_add_right m n₁ n₂) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, smul.aux_of, tmul_add]
| _, _, (eqv.of_smul s m n) := (add_con.ker_rel _).2 $
by rw [smul.aux_of, smul.aux_of, ←smul_comm, smul_tmul]
| _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, add_comm]
end⟩
instance : has_smul R (M ⊗[R] N) := tensor_product.left_has_smul
protected theorem smul_zero (r : R') : (r • 0 : M ⊗[R] N) = 0 :=
add_monoid_hom.map_zero _
protected theorem smul_add (r : R') (x y : M ⊗[R] N) :
r • (x + y) = r • x + r • y :=
add_monoid_hom.map_add _ _ _
protected theorem zero_smul (x : M ⊗[R] N) : (0 : R'') • x = 0 :=
have ∀ (r : R'') (m : M) (n : N), r • (m ⊗ₜ[R] n) = (r • m) ⊗ₜ n := λ _ _ _, rfl,
tensor_product.induction_on x
(by rw tensor_product.smul_zero)
(λ m n, by rw [this, zero_smul, zero_tmul])
(λ x y ihx ihy, by rw [tensor_product.smul_add, ihx, ihy, add_zero])
protected theorem one_smul (x : M ⊗[R] N) : (1 : R') • x = x :=
have ∀ (r : R') (m : M) (n : N), r • (m ⊗ₜ[R] n) = (r • m) ⊗ₜ n := λ _ _ _, rfl,
tensor_product.induction_on x
(by rw tensor_product.smul_zero)
(λ m n, by rw [this, one_smul])
(λ x y ihx ihy, by rw [tensor_product.smul_add, ihx, ihy])
protected theorem add_smul (r s : R'') (x : M ⊗[R] N) : (r + s) • x = r • x + s • x :=
have ∀ (r : R'') (m : M) (n : N), r • (m ⊗ₜ[R] n) = (r • m) ⊗ₜ n := λ _ _ _, rfl,
tensor_product.induction_on x
(by simp_rw [tensor_product.smul_zero, add_zero])
(λ m n, by simp_rw [this, add_smul, add_tmul])
(λ x y ihx ihy, by { simp_rw tensor_product.smul_add, rw [ihx, ihy, add_add_add_comm] })
instance : add_comm_monoid (M ⊗[R] N) :=
{ nsmul := λ n v, n • v,
nsmul_zero' := by simp [tensor_product.zero_smul],
nsmul_succ' := by simp [nat.succ_eq_one_add, tensor_product.one_smul, tensor_product.add_smul],
.. tensor_product.add_comm_semigroup _ _, .. tensor_product.add_zero_class _ _}
instance left_distrib_mul_action : distrib_mul_action R' (M ⊗[R] N) :=
have ∀ (r : R') (m : M) (n : N), r • (m ⊗ₜ[R] n) = (r • m) ⊗ₜ n := λ _ _ _, rfl,
{ smul := (•),
smul_add := λ r x y, tensor_product.smul_add r x y,
mul_smul := λ r s x, tensor_product.induction_on x
(by simp_rw tensor_product.smul_zero)
(λ m n, by simp_rw [this, mul_smul])
(λ x y ihx ihy, by { simp_rw tensor_product.smul_add, rw [ihx, ihy] }),
one_smul := tensor_product.one_smul,
smul_zero := tensor_product.smul_zero }
instance : distrib_mul_action R (M ⊗[R] N) := tensor_product.left_distrib_mul_action
theorem smul_tmul' (r : R') (m : M) (n : N) :
r • (m ⊗ₜ[R] n) = (r • m) ⊗ₜ n :=
rfl
@[simp] lemma tmul_smul
[distrib_mul_action R' N] [compatible_smul R R' M N] (r : R') (x : M) (y : N) :
x ⊗ₜ (r • y) = r • (x ⊗ₜ[R] y) :=
(smul_tmul _ _ _).symm
lemma smul_tmul_smul (r s : R) (m : M) (n : N) : (r • m) ⊗ₜ[R] (s • n) = (r * s) • (m ⊗ₜ[R] n) :=
by simp only [tmul_smul, smul_tmul, mul_smul]
instance left_module : module R'' (M ⊗[R] N) :=
{ smul := (•),
add_smul := tensor_product.add_smul,
zero_smul := tensor_product.zero_smul,
..tensor_product.left_distrib_mul_action }
instance : module R (M ⊗[R] N) := tensor_product.left_module
instance [module R''ᵐᵒᵖ M] [is_central_scalar R'' M] : is_central_scalar R'' (M ⊗[R] N) :=
{ op_smul_eq_smul := λ r x,
tensor_product.induction_on x
(by rw [smul_zero, smul_zero])
(λ x y, by rw [smul_tmul', smul_tmul', op_smul_eq_smul])
(λ x y hx hy, by rw [smul_add, smul_add, hx, hy]) }
section
-- Like `R'`, `R'₂` provides a `distrib_mul_action R'₂ (M ⊗[R] N)`
variables {R'₂ : Type*} [monoid R'₂] [distrib_mul_action R'₂ M]
variables [smul_comm_class R R'₂ M] [has_smul R'₂ R']
/-- `is_scalar_tower R'₂ R' M` implies `is_scalar_tower R'₂ R' (M ⊗[R] N)` -/
instance is_scalar_tower_left [is_scalar_tower R'₂ R' M] :
is_scalar_tower R'₂ R' (M ⊗[R] N) :=
⟨λ s r x, tensor_product.induction_on x
(by simp)
(λ m n, by rw [smul_tmul', smul_tmul', smul_tmul', smul_assoc])
(λ x y ihx ihy, by rw [smul_add, smul_add, smul_add, ihx, ihy])⟩
variables [distrib_mul_action R'₂ N] [distrib_mul_action R' N]
variables [compatible_smul R R'₂ M N] [compatible_smul R R' M N]
/-- `is_scalar_tower R'₂ R' N` implies `is_scalar_tower R'₂ R' (M ⊗[R] N)` -/
instance is_scalar_tower_right [is_scalar_tower R'₂ R' N] :
is_scalar_tower R'₂ R' (M ⊗[R] N) :=
⟨λ s r x, tensor_product.induction_on x
(by simp)
(λ m n, by rw [←tmul_smul, ←tmul_smul, ←tmul_smul, smul_assoc])
(λ x y ihx ihy, by rw [smul_add, smul_add, smul_add, ihx, ihy])⟩
end
/-- A short-cut instance for the common case, where the requirements for the `compatible_smul`
instances are sufficient. -/
instance is_scalar_tower [has_smul R' R] [is_scalar_tower R' R M] :
is_scalar_tower R' R (M ⊗[R] N) :=
tensor_product.is_scalar_tower_left -- or right
variables (R M N)
/-- The canonical bilinear map `M → N → M ⊗[R] N`. -/
def mk : M →ₗ[R] N →ₗ[R] M ⊗[R] N :=
linear_map.mk₂ R (⊗ₜ) add_tmul (λ c m n, by rw [smul_tmul, tmul_smul]) tmul_add tmul_smul
variables {R M N}
@[simp] lemma mk_apply (m : M) (n : N) : mk R M N m n = m ⊗ₜ n := rfl
lemma ite_tmul (x₁ : M) (x₂ : N) (P : Prop) [decidable P] :
(if P then x₁ else 0) ⊗ₜ[R] x₂ = if P then x₁ ⊗ₜ x₂ else 0 :=
by { split_ifs; simp }
lemma tmul_ite (x₁ : M) (x₂ : N) (P : Prop) [decidable P] :
x₁ ⊗ₜ[R] (if P then x₂ else 0) = if P then x₁ ⊗ₜ x₂ else 0 :=
by { split_ifs; simp }
section
open_locale big_operators
lemma sum_tmul {α : Type*} (s : finset α) (m : α → M) (n : N) :
(∑ a in s, m a) ⊗ₜ[R] n = ∑ a in s, m a ⊗ₜ[R] n :=
begin
classical,
induction s using finset.induction with a s has ih h,
{ simp, },
{ simp [finset.sum_insert has, add_tmul, ih], },
end
lemma tmul_sum (m : M) {α : Type*} (s : finset α) (n : α → N) :
m ⊗ₜ[R] (∑ a in s, n a) = ∑ a in s, m ⊗ₜ[R] n a :=
begin
classical,
induction s using finset.induction with a s has ih h,
{ simp, },
{ simp [finset.sum_insert has, tmul_add, ih], },
end
end
variables (R M N)
/-- The simple (aka pure) elements span the tensor product. -/
lemma span_tmul_eq_top :
submodule.span R { t : M ⊗[R] N | ∃ m n, m ⊗ₜ n = t } = ⊤ :=
begin
ext t, simp only [submodule.mem_top, iff_true],
apply t.induction_on,
{ exact submodule.zero_mem _, },
{ intros m n, apply submodule.subset_span, use [m, n], },
{ intros t₁ t₂ ht₁ ht₂, exact submodule.add_mem _ ht₁ ht₂, },
end
@[simp] lemma map₂_mk_top_top_eq_top : submodule.map₂ (mk R M N) ⊤ ⊤ = ⊤ :=
begin
rw [← top_le_iff, ← span_tmul_eq_top, submodule.map₂_eq_span_image2],
exact submodule.span_mono (λ _ ⟨m, n, h⟩, ⟨m, n, trivial, trivial, h⟩),
end
end module
section UMP
variables {M N P Q}
variables (f : M →ₗ[R] N →ₗ[R] P)
/-- Auxiliary function to constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P`
with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def lift_aux : (M ⊗[R] N) →+ P :=
(add_con_gen (tensor_product.eqv R M N)).lift (free_add_monoid.lift $ λ p : M × N, f p.1 p.2) $
add_con.add_con_gen_le $ λ x y hxy, match x, y, hxy with
| _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, free_add_monoid.lift_eval_of, f.map_zero₂]
| _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, free_add_monoid.lift_eval_of, (f m).map_zero]
| _, _, (eqv.of_add_left m₁ m₂ n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, free_add_monoid.lift_eval_of, f.map_add₂]
| _, _, (eqv.of_add_right m n₁ n₂) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, free_add_monoid.lift_eval_of, (f m).map_add]
| _, _, (eqv.of_smul r m n) := (add_con.ker_rel _).2 $
by simp_rw [free_add_monoid.lift_eval_of, f.map_smul₂, (f m).map_smul]
| _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, add_comm]
end
lemma lift_aux_tmul (m n) : lift_aux f (m ⊗ₜ n) = f m n :=
zero_add _
variable {f}
@[simp] lemma lift_aux.smul (r : R) (x) : lift_aux f (r • x) = r • lift_aux f x :=
tensor_product.induction_on x (smul_zero _).symm
(λ p q, by rw [← tmul_smul, lift_aux_tmul, lift_aux_tmul, (f p).map_smul])
(λ p q ih1 ih2, by rw [smul_add, (lift_aux f).map_add, ih1, ih2, (lift_aux f).map_add, smul_add])
variable (f)
/-- Constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P` with the property that
its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def lift : M ⊗ N →ₗ[R] P :=
{ map_smul' := lift_aux.smul,
.. lift_aux f }
variable {f}
@[simp] lemma lift.tmul (x y) : lift f (x ⊗ₜ y) = f x y :=
zero_add _
@[simp] lemma lift.tmul' (x y) : (lift f).1 (x ⊗ₜ y) = f x y :=
lift.tmul _ _
theorem ext' {g h : (M ⊗[R] N) →ₗ[R] P}
(H : ∀ x y, g (x ⊗ₜ y) = h (x ⊗ₜ y)) : g = h :=
linear_map.ext $ λ z, tensor_product.induction_on z (by simp_rw linear_map.map_zero) H $
λ x y ihx ihy, by rw [g.map_add, h.map_add, ihx, ihy]
theorem lift.unique {g : (M ⊗[R] N) →ₗ[R] P} (H : ∀ x y, g (x ⊗ₜ y) = f x y) :
g = lift f :=
ext' $ λ m n, by rw [H, lift.tmul]
theorem lift_mk : lift (mk R M N) = linear_map.id :=
eq.symm $ lift.unique $ λ x y, rfl
theorem lift_compr₂ (g : P →ₗ[R] Q) : lift (f.compr₂ g) = g.comp (lift f) :=
eq.symm $ lift.unique $ λ x y, by simp
theorem lift_mk_compr₂ (f : M ⊗ N →ₗ[R] P) : lift ((mk R M N).compr₂ f) = f :=
by rw [lift_compr₂ f, lift_mk, linear_map.comp_id]
/--
This used to be an `@[ext]` lemma, but it fails very slowly when the `ext` tactic tries to apply
it in some cases, notably when one wants to show equality of two linear maps. The `@[ext]`
attribute is now added locally where it is needed. Using this as the `@[ext]` lemma instead of
`tensor_product.ext'` allows `ext` to apply lemmas specific to `M →ₗ _` and `N →ₗ _`.
See note [partially-applied ext lemmas]. -/
theorem ext {g h : M ⊗ N →ₗ[R] P}
(H : (mk R M N).compr₂ g = (mk R M N).compr₂ h) : g = h :=
by rw [← lift_mk_compr₂ g, H, lift_mk_compr₂]
local attribute [ext] ext
example : M → N → (M → N → P) → P :=
λ m, flip $ λ f, f m
variables (R M N P)
/-- Linearly constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P`
with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def uncurry : (M →ₗ[R] N →ₗ[R] P) →ₗ[R] M ⊗[R] N →ₗ[R] P :=
linear_map.flip $ lift $ (linear_map.lflip _ _ _ _).comp (linear_map.flip linear_map.id)
variables {R M N P}
@[simp] theorem uncurry_apply (f : M →ₗ[R] N →ₗ[R] P) (m : M) (n : N) :
uncurry R M N P f (m ⊗ₜ n) = f m n :=
by rw [uncurry, linear_map.flip_apply, lift.tmul]; refl
variables (R M N P)
/-- A linear equivalence constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P`
with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def lift.equiv : (M →ₗ[R] N →ₗ[R] P) ≃ₗ[R] (M ⊗ N →ₗ[R] P) :=
{ inv_fun := λ f, (mk R M N).compr₂ f,
left_inv := λ f, linear_map.ext₂ $ λ m n, lift.tmul _ _,
right_inv := λ f, ext' $ λ m n, lift.tmul _ _,
.. uncurry R M N P }
@[simp] lemma lift.equiv_apply (f : M →ₗ[R] N →ₗ[R] P) (m : M) (n : N) :
lift.equiv R M N P f (m ⊗ₜ n) = f m n :=
uncurry_apply f m n
@[simp] lemma lift.equiv_symm_apply (f : M ⊗[R] N →ₗ[R] P) (m : M) (n : N) :
(lift.equiv R M N P).symm f m n = f (m ⊗ₜ n) :=
rfl
/-- Given a linear map `M ⊗ N → P`, compose it with the canonical bilinear map `M → N → M ⊗ N` to
form a bilinear map `M → N → P`. -/
def lcurry : (M ⊗[R] N →ₗ[R] P) →ₗ[R] M →ₗ[R] N →ₗ[R] P :=
(lift.equiv R M N P).symm
variables {R M N P}
@[simp] theorem lcurry_apply (f : M ⊗[R] N →ₗ[R] P) (m : M) (n : N) :
lcurry R M N P f m n = f (m ⊗ₜ n) := rfl
/-- Given a linear map `M ⊗ N → P`, compose it with the canonical bilinear map `M → N → M ⊗ N` to
form a bilinear map `M → N → P`. -/
def curry (f : M ⊗ N →ₗ[R] P) : M →ₗ[R] N →ₗ[R] P := lcurry R M N P f
@[simp] theorem curry_apply (f : M ⊗ N →ₗ[R] P) (m : M) (n : N) :
curry f m n = f (m ⊗ₜ n) := rfl
lemma curry_injective : function.injective (curry : (M ⊗[R] N →ₗ[R] P) → (M →ₗ[R] N →ₗ[R] P)) :=
λ g h H, ext H
theorem ext_threefold {g h : (M ⊗[R] N) ⊗[R] P →ₗ[R] Q}
(H : ∀ x y z, g ((x ⊗ₜ y) ⊗ₜ z) = h ((x ⊗ₜ y) ⊗ₜ z)) : g = h :=
begin
ext x y z,
exact H x y z
end
-- We'll need this one for checking the pentagon identity!
theorem ext_fourfold {g h : ((M ⊗[R] N) ⊗[R] P) ⊗[R] Q →ₗ[R] S}
(H : ∀ w x y z, g (((w ⊗ₜ x) ⊗ₜ y) ⊗ₜ z) = h (((w ⊗ₜ x) ⊗ₜ y) ⊗ₜ z)) : g = h :=
begin
ext w x y z,
exact H w x y z,
end
end UMP
variables {M N}
section
variables (R M)
/--
The base ring is a left identity for the tensor product of modules, up to linear equivalence.
-/
protected def lid : R ⊗ M ≃ₗ[R] M :=
linear_equiv.of_linear (lift $ linear_map.lsmul R M) (mk R R M 1)
(linear_map.ext $ λ _, by simp)
(ext' $ λ r m, by simp; rw [← tmul_smul, ← smul_tmul, smul_eq_mul, mul_one])
end
@[simp] theorem lid_tmul (m : M) (r : R) :
((tensor_product.lid R M) : (R ⊗ M → M)) (r ⊗ₜ m) = r • m :=
begin
dsimp [tensor_product.lid],
simp,
end
@[simp] lemma lid_symm_apply (m : M) :
(tensor_product.lid R M).symm m = 1 ⊗ₜ m := rfl
section
variables (R M N)
/--
The tensor product of modules is commutative, up to linear equivalence.
-/
protected def comm : M ⊗ N ≃ₗ[R] N ⊗ M :=
linear_equiv.of_linear (lift (mk R N M).flip) (lift (mk R M N).flip)
(ext' $ λ m n, rfl)
(ext' $ λ m n, rfl)
@[simp] theorem comm_tmul (m : M) (n : N) :
(tensor_product.comm R M N) (m ⊗ₜ n) = n ⊗ₜ m := rfl
@[simp] theorem comm_symm_tmul (m : M) (n : N) :
(tensor_product.comm R M N).symm (n ⊗ₜ m) = m ⊗ₜ n := rfl
end
section
variables (R M)
/--
The base ring is a right identity for the tensor product of modules, up to linear equivalence.
-/
protected def rid : M ⊗[R] R ≃ₗ[R] M :=
linear_equiv.trans (tensor_product.comm R M R) (tensor_product.lid R M)
end
@[simp] theorem rid_tmul (m : M) (r : R) :
(tensor_product.rid R M) (m ⊗ₜ r) = r • m :=
begin
dsimp [tensor_product.rid, tensor_product.comm, tensor_product.lid],
simp,
end
@[simp] lemma rid_symm_apply (m : M) :
(tensor_product.rid R M).symm m = m ⊗ₜ 1 := rfl
open linear_map
section
variables (R M N P)
/-- The associator for tensor product of R-modules, as a linear equivalence. -/
protected def assoc : (M ⊗[R] N) ⊗[R] P ≃ₗ[R] M ⊗[R] (N ⊗[R] P) :=
begin
refine linear_equiv.of_linear
(lift $ lift $ comp (lcurry R _ _ _) $ mk _ _ _)
(lift $ comp (uncurry R _ _ _) $ curry $ mk _ _ _)
(ext $ linear_map.ext $ λ m, ext' $ λ n p, _)
(ext $ flip_inj $ linear_map.ext $ λ p, ext' $ λ m n, _);
repeat { rw lift.tmul <|> rw compr₂_apply <|> rw comp_apply <|>
rw mk_apply <|> rw flip_apply <|> rw lcurry_apply <|>
rw uncurry_apply <|> rw curry_apply <|> rw id_apply }
end
end
@[simp] theorem assoc_tmul (m : M) (n : N) (p : P) :
(tensor_product.assoc R M N P) ((m ⊗ₜ n) ⊗ₜ p) = m ⊗ₜ (n ⊗ₜ p) := rfl
@[simp] theorem assoc_symm_tmul (m : M) (n : N) (p : P) :
(tensor_product.assoc R M N P).symm (m ⊗ₜ (n ⊗ₜ p)) = (m ⊗ₜ n) ⊗ₜ p := rfl
/-- The tensor product of a pair of linear maps between modules. -/
def map (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : M ⊗ N →ₗ[R] P ⊗ Q :=
lift $ comp (compl₂ (mk _ _ _) g) f
@[simp] theorem map_tmul (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (m : M) (n : N) :
map f g (m ⊗ₜ n) = f m ⊗ₜ g n :=
rfl
lemma map_range_eq_span_tmul (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(map f g).range = submodule.span R { t | ∃ m n, (f m) ⊗ₜ (g n) = t } :=
begin
simp only [← submodule.map_top, ← span_tmul_eq_top, submodule.map_span, set.mem_image,
set.mem_set_of_eq],
congr, ext t,
split,
{ rintros ⟨_, ⟨⟨m, n, rfl⟩, rfl⟩⟩, use [m, n], simp only [map_tmul], },
{ rintros ⟨m, n, rfl⟩, use [m ⊗ₜ n, m, n], simp only [map_tmul], },
end
/-- Given submodules `p ⊆ P` and `q ⊆ Q`, this is the natural map: `p ⊗ q → P ⊗ Q`. -/
@[simp] def map_incl (p : submodule R P) (q : submodule R Q) : p ⊗[R] q →ₗ[R] P ⊗[R] Q :=
map p.subtype q.subtype
section
variables {P' Q' : Type*}
variables [add_comm_monoid P'] [module R P']
variables [add_comm_monoid Q'] [module R Q']
lemma map_comp (f₂ : P →ₗ[R] P') (f₁ : M →ₗ[R] P) (g₂ : Q →ₗ[R] Q') (g₁ : N →ₗ[R] Q) :
map (f₂.comp f₁) (g₂.comp g₁) = (map f₂ g₂).comp (map f₁ g₁) :=
ext' $ λ _ _, by simp only [linear_map.comp_apply, map_tmul]
lemma lift_comp_map (i : P →ₗ[R] Q →ₗ[R] Q') (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(lift i).comp (map f g) = lift ((i.comp f).compl₂ g) :=
ext' $ λ _ _, by simp only [lift.tmul, map_tmul, linear_map.compl₂_apply, linear_map.comp_apply]
local attribute [ext] ext
@[simp] lemma map_id : map (id : M →ₗ[R] M) (id : N →ₗ[R] N) = id :=
by { ext, simp only [mk_apply, id_coe, compr₂_apply, id.def, map_tmul], }
@[simp] lemma map_one : map (1 : M →ₗ[R] M) (1 : N →ₗ[R] N) = 1 := map_id
lemma map_mul (f₁ f₂ : M →ₗ[R] M) (g₁ g₂ : N →ₗ[R] N) :
map (f₁ * f₂) (g₁ * g₂) = (map f₁ g₁) * (map f₂ g₂) :=
map_comp f₁ f₂ g₁ g₂
@[simp] protected lemma map_pow (f : M →ₗ[R] M) (g : N →ₗ[R] N) (n : ℕ) :
(map f g)^n = map (f^n) (g^n) :=
begin
induction n with n ih,
{ simp only [pow_zero, map_one], },
{ simp only [pow_succ', ih, map_mul], },
end
lemma map_add_left (f₁ f₂ : M →ₗ[R] P) (g : N →ₗ[R] Q) : map (f₁ + f₂) g = map f₁ g + map f₂ g :=
by {ext, simp only [add_tmul, compr₂_apply, mk_apply, map_tmul, add_apply]}
lemma map_add_right (f : M →ₗ[R] P) (g₁ g₂ : N →ₗ[R] Q) : map f (g₁ + g₂) = map f g₁ + map f g₂ :=
by {ext, simp only [tmul_add, compr₂_apply, mk_apply, map_tmul, add_apply]}
lemma map_smul_left (r : R) (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : map (r • f) g = r • map f g :=
by {ext, simp only [smul_tmul, compr₂_apply, mk_apply, map_tmul, smul_apply, tmul_smul]}
lemma map_smul_right (r : R) (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : map f (r • g) = r • map f g :=
by {ext, simp only [smul_tmul, compr₂_apply, mk_apply, map_tmul, smul_apply, tmul_smul]}
variables (R M N P Q)
/-- The tensor product of a pair of linear maps between modules, bilinear in both maps. -/
def map_bilinear : (M →ₗ[R] P) →ₗ[R] (N →ₗ[R] Q) →ₗ[R] (M ⊗[R] N →ₗ[R] P ⊗[R] Q) :=
linear_map.mk₂ R map map_add_left map_smul_left map_add_right map_smul_right
/-- The canonical linear map from `P ⊗[R] (M →ₗ[R] Q)` to `(M →ₗ[R] P ⊗[R] Q)` -/
def ltensor_hom_to_hom_ltensor : P ⊗[R] (M →ₗ[R] Q) →ₗ[R] (M →ₗ[R] P ⊗[R] Q) :=
tensor_product.lift (llcomp R M Q _ ∘ₗ mk R P Q)
/-- The canonical linear map from `(M →ₗ[R] P) ⊗[R] Q` to `(M →ₗ[R] P ⊗[R] Q)` -/
def rtensor_hom_to_hom_rtensor : (M →ₗ[R] P) ⊗[R] Q →ₗ[R] (M →ₗ[R] P ⊗[R] Q) :=
tensor_product.lift (llcomp R M P _ ∘ₗ (mk R P Q).flip).flip
/-- The linear map from `(M →ₗ P) ⊗ (N →ₗ Q)` to `(M ⊗ N →ₗ P ⊗ Q)` sending `f ⊗ₜ g` to
the `tensor_product.map f g`, the tensor product of the two maps. -/
def hom_tensor_hom_map : (M →ₗ[R] P) ⊗[R] (N →ₗ[R] Q) →ₗ[R] (M ⊗[R] N →ₗ[R] P ⊗[R] Q) :=
lift (map_bilinear R M N P Q)
variables {R M N P Q}
@[simp]
lemma map_bilinear_apply (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
map_bilinear R M N P Q f g = map f g := rfl
@[simp]
lemma ltensor_hom_to_hom_ltensor_apply (p : P) (f : M →ₗ[R] Q) (m : M) :
ltensor_hom_to_hom_ltensor R M P Q (p ⊗ₜ f) m = p ⊗ₜ f m := rfl
@[simp]
lemma rtensor_hom_to_hom_rtensor_apply (f : M →ₗ[R] P) (q : Q) (m : M) :
rtensor_hom_to_hom_rtensor R M P Q (f ⊗ₜ q) m = f m ⊗ₜ q := rfl
@[simp]
lemma hom_tensor_hom_map_apply (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
hom_tensor_hom_map R M N P Q (f ⊗ₜ g) = map f g :=
by simp only [hom_tensor_hom_map, lift.tmul, map_bilinear_apply]
end
/-- If `M` and `P` are linearly equivalent and `N` and `Q` are linearly equivalent
then `M ⊗ N` and `P ⊗ Q` are linearly equivalent. -/
def congr (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) : M ⊗ N ≃ₗ[R] P ⊗ Q :=
linear_equiv.of_linear (map f g) (map f.symm g.symm)
(ext' $ λ m n, by simp; simp only [linear_equiv.apply_symm_apply])
(ext' $ λ m n, by simp; simp only [linear_equiv.symm_apply_apply])
@[simp] theorem congr_tmul (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) (m : M) (n : N) :
congr f g (m ⊗ₜ n) = f m ⊗ₜ g n :=
rfl
@[simp] theorem congr_symm_tmul (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) (p : P) (q : Q) :
(congr f g).symm (p ⊗ₜ q) = f.symm p ⊗ₜ g.symm q :=
rfl
variables (R M N P Q)
/-- A tensor product analogue of `mul_left_comm`. -/
def left_comm : M ⊗[R] (N ⊗[R] P) ≃ₗ[R] N ⊗[R] (M ⊗[R] P) :=
let e₁ := (tensor_product.assoc R M N P).symm,
e₂ := congr (tensor_product.comm R M N) (1 : P ≃ₗ[R] P),
e₃ := (tensor_product.assoc R N M P) in
e₁ ≪≫ₗ (e₂ ≪≫ₗ e₃)
variables {M N P Q}
@[simp] lemma left_comm_tmul (m : M) (n : N) (p : P) :
left_comm R M N P (m ⊗ₜ (n ⊗ₜ p)) = n ⊗ₜ (m ⊗ₜ p) :=
rfl
@[simp] lemma left_comm_symm_tmul (m : M) (n : N) (p : P) :
(left_comm R M N P).symm (n ⊗ₜ (m ⊗ₜ p)) = m ⊗ₜ (n ⊗ₜ p) :=
rfl
variables (M N P Q)
/-- This special case is worth defining explicitly since it is useful for defining multiplication
on tensor products of modules carrying multiplications (e.g., associative rings, Lie rings, ...).
E.g., suppose `M = P` and `N = Q` and that `M` and `N` carry bilinear multiplications:
`M ⊗ M → M` and `N ⊗ N → N`. Using `map`, we can define `(M ⊗ M) ⊗ (N ⊗ N) → M ⊗ N` which, when
combined with this definition, yields a bilinear multiplication on `M ⊗ N`:
`(M ⊗ N) ⊗ (M ⊗ N) → M ⊗ N`. In particular we could use this to define the multiplication in
the `tensor_product.semiring` instance (currently defined "by hand" using `tensor_product.mul`).
See also `mul_mul_mul_comm`. -/
def tensor_tensor_tensor_comm : (M ⊗[R] N) ⊗[R] (P ⊗[R] Q) ≃ₗ[R] (M ⊗[R] P) ⊗[R] (N ⊗[R] Q) :=
let e₁ := tensor_product.assoc R M N (P ⊗[R] Q),
e₂ := congr (1 : M ≃ₗ[R] M) (left_comm R N P Q),
e₃ := (tensor_product.assoc R M P (N ⊗[R] Q)).symm in
e₁ ≪≫ₗ (e₂ ≪≫ₗ e₃)
variables {M N P Q}
@[simp] lemma tensor_tensor_tensor_comm_tmul (m : M) (n : N) (p : P) (q : Q) :
tensor_tensor_tensor_comm R M N P Q ((m ⊗ₜ n) ⊗ₜ (p ⊗ₜ q)) = (m ⊗ₜ p) ⊗ₜ (n ⊗ₜ q) :=
rfl
@[simp] lemma tensor_tensor_tensor_comm_symm_tmul (m : M) (n : N) (p : P) (q : Q) :
(tensor_tensor_tensor_comm R M N P Q).symm ((m ⊗ₜ p) ⊗ₜ (n ⊗ₜ q)) = (m ⊗ₜ n) ⊗ₜ (p ⊗ₜ q) :=
rfl
variables (M N P Q)
/-- This special case is useful for describing the interplay between `dual_tensor_hom_equiv` and
composition of linear maps.
E.g., composition of linear maps gives a map `(M → N) ⊗ (N → P) → (M → P)`, and applying
`dual_tensor_hom_equiv.symm` to the three hom-modules gives a map
`(M.dual ⊗ N) ⊗ (N.dual ⊗ P) → (M.dual ⊗ P)`, which agrees with the application of `contract_right`
on `N ⊗ N.dual` after the suitable rebracketting.
-/
def tensor_tensor_tensor_assoc : (M ⊗[R] N) ⊗[R] (P ⊗[R] Q) ≃ₗ[R] M ⊗[R] (N ⊗[R] P) ⊗[R] Q :=
(tensor_product.assoc R (M ⊗[R] N) P Q).symm ≪≫ₗ
congr (tensor_product.assoc R M N P) (1 : Q ≃ₗ[R] Q)
variables {M N P Q}
@[simp] lemma tensor_tensor_tensor_assoc_tmul (m : M) (n : N) (p : P) (q : Q) :
tensor_tensor_tensor_assoc R M N P Q ((m ⊗ₜ n) ⊗ₜ (p ⊗ₜ q)) = m ⊗ₜ (n ⊗ₜ p) ⊗ₜ q := rfl
@[simp] lemma tensor_tensor_tensor_assoc_symm_tmul (m : M) (n : N) (p : P) (q : Q) :
(tensor_tensor_tensor_assoc R M N P Q).symm (m ⊗ₜ (n ⊗ₜ p) ⊗ₜ q) = (m ⊗ₜ n) ⊗ₜ (p ⊗ₜ q) :=
rfl
end tensor_product
namespace linear_map
variables {R} (M) {N P Q}
/-- `ltensor M f : M ⊗ N →ₗ M ⊗ P` is the natural linear map induced by `f : N →ₗ P`. -/
def ltensor (f : N →ₗ[R] P) : M ⊗ N →ₗ[R] M ⊗ P :=
tensor_product.map id f
/-- `rtensor f M : N₁ ⊗ M →ₗ N₂ ⊗ M` is the natural linear map induced by `f : N₁ →ₗ N₂`. -/
def rtensor (f : N →ₗ[R] P) : N ⊗ M →ₗ[R] P ⊗ M :=
tensor_product.map f id
variables (g : P →ₗ[R] Q) (f : N →ₗ[R] P)
@[simp] lemma ltensor_tmul (m : M) (n : N) : f.ltensor M (m ⊗ₜ n) = m ⊗ₜ (f n) := rfl
@[simp] lemma rtensor_tmul (m : M) (n : N) : f.rtensor M (n ⊗ₜ m) = (f n) ⊗ₜ m := rfl
open tensor_product
local attribute [ext] tensor_product.ext
/-- `ltensor_hom M` is the natural linear map that sends a linear map `f : N →ₗ P` to `M ⊗ f`. -/
def ltensor_hom : (N →ₗ[R] P) →ₗ[R] (M ⊗[R] N →ₗ[R] M ⊗[R] P) :=
{ to_fun := ltensor M,
map_add' := λ f g, by
{ ext x y, simp only [compr₂_apply, mk_apply, add_apply, ltensor_tmul, tmul_add] },
map_smul' := λ r f, by
{ dsimp, ext x y, simp only [compr₂_apply, mk_apply, tmul_smul, smul_apply, ltensor_tmul] } }
/-- `rtensor_hom M` is the natural linear map that sends a linear map `f : N →ₗ P` to `M ⊗ f`. -/
def rtensor_hom : (N →ₗ[R] P) →ₗ[R] (N ⊗[R] M →ₗ[R] P ⊗[R] M) :=
{ to_fun := λ f, f.rtensor M,
map_add' := λ f g, by
{ ext x y, simp only [compr₂_apply, mk_apply, add_apply, rtensor_tmul, add_tmul] },
map_smul' := λ r f, by
{ dsimp, ext x y, simp only [compr₂_apply, mk_apply, smul_tmul, tmul_smul, smul_apply,
rtensor_tmul] } }
@[simp] lemma coe_ltensor_hom :
(ltensor_hom M : (N →ₗ[R] P) → (M ⊗[R] N →ₗ[R] M ⊗[R] P)) = ltensor M := rfl
@[simp] lemma coe_rtensor_hom :
(rtensor_hom M : (N →ₗ[R] P) → (N ⊗[R] M →ₗ[R] P ⊗[R] M)) = rtensor M := rfl
@[simp] lemma ltensor_add (f g : N →ₗ[R] P) : (f + g).ltensor M = f.ltensor M + g.ltensor M :=
(ltensor_hom M).map_add f g
@[simp] lemma rtensor_add (f g : N →ₗ[R] P) : (f + g).rtensor M = f.rtensor M + g.rtensor M :=
(rtensor_hom M).map_add f g
@[simp] lemma ltensor_zero : ltensor M (0 : N →ₗ[R] P) = 0 :=
(ltensor_hom M).map_zero
@[simp] lemma rtensor_zero : rtensor M (0 : N →ₗ[R] P) = 0 :=
(rtensor_hom M).map_zero
@[simp] lemma ltensor_smul (r : R) (f : N →ₗ[R] P) : (r • f).ltensor M = r • (f.ltensor M) :=
(ltensor_hom M).map_smul r f
@[simp] lemma rtensor_smul (r : R) (f : N →ₗ[R] P) : (r • f).rtensor M = r • (f.rtensor M) :=
(rtensor_hom M).map_smul r f
lemma ltensor_comp : (g.comp f).ltensor M = (g.ltensor M).comp (f.ltensor M) :=
by { ext m n, simp only [compr₂_apply, mk_apply, comp_apply, ltensor_tmul] }
lemma rtensor_comp : (g.comp f).rtensor M = (g.rtensor M).comp (f.rtensor M) :=
by { ext m n, simp only [compr₂_apply, mk_apply, comp_apply, rtensor_tmul] }
lemma ltensor_mul (f g : module.End R N) : (f * g).ltensor M = (f.ltensor M) * (g.ltensor M) :=
ltensor_comp M f g
lemma rtensor_mul (f g : module.End R N) : (f * g).rtensor M = (f.rtensor M) * (g.rtensor M) :=
rtensor_comp M f g
variables (N)
@[simp] lemma ltensor_id : (id : N →ₗ[R] N).ltensor M = id := map_id
@[simp] lemma rtensor_id : (id : N →ₗ[R] N).rtensor M = id := map_id
variables {N}
@[simp] lemma ltensor_comp_rtensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(g.ltensor P).comp (f.rtensor N) = map f g :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
@[simp] lemma rtensor_comp_ltensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(f.rtensor Q).comp (g.ltensor M) = map f g :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
@[simp] lemma map_comp_rtensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (f' : S →ₗ[R] M) :
(map f g).comp (f'.rtensor _) = map (f.comp f') g :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
@[simp] lemma map_comp_ltensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (g' : S →ₗ[R] N) :
(map f g).comp (g'.ltensor _) = map f (g.comp g') :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
@[simp] lemma rtensor_comp_map (f' : P →ₗ[R] S) (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(f'.rtensor _).comp (map f g) = map (f'.comp f) g :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
@[simp] lemma ltensor_comp_map (g' : Q →ₗ[R] S) (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(g'.ltensor _).comp (map f g) = map f (g'.comp g) :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
variables {M}
@[simp] lemma rtensor_pow (f : M →ₗ[R] M) (n : ℕ) : (f.rtensor N)^n = (f^n).rtensor N :=
by { have h := tensor_product.map_pow f (id : N →ₗ[R] N) n, rwa id_pow at h, }
@[simp] lemma ltensor_pow (f : N →ₗ[R] N) (n : ℕ) : (f.ltensor M)^n = (f^n).ltensor M :=
by { have h := tensor_product.map_pow (id : M →ₗ[R] M) f n, rwa id_pow at h, }
end linear_map
end semiring
section ring
variables {R : Type*} [comm_semiring R]
variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*}
variables [add_comm_group M] [add_comm_group N] [add_comm_group P] [add_comm_group Q]
[add_comm_group S]
variables [module R M] [module R N] [module R P] [module R Q] [module R S]
namespace tensor_product
open_locale tensor_product
open linear_map
variables (R)
/-- Auxiliary function to defining negation multiplication on tensor product. -/
def neg.aux : free_add_monoid (M × N) →+ M ⊗[R] N :=
free_add_monoid.lift $ λ p : M × N, (-p.1) ⊗ₜ p.2
variables {R}
theorem neg.aux_of (m : M) (n : N) :
neg.aux R (free_add_monoid.of (m, n)) = (-m) ⊗ₜ[R] n :=
rfl
instance : has_neg (M ⊗[R] N) :=
{ neg := (add_con_gen (tensor_product.eqv R M N)).lift (neg.aux R) $ add_con.add_con_gen_le $
λ x y hxy, match x, y, hxy with
| _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, neg.aux_of, neg_zero, zero_tmul]
| _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, neg.aux_of, tmul_zero]
| _, _, (eqv.of_add_left m₁ m₂ n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, neg.aux_of, neg_add, add_tmul]
| _, _, (eqv.of_add_right m n₁ n₂) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, neg.aux_of, tmul_add]
| _, _, (eqv.of_smul s m n) := (add_con.ker_rel _).2 $
by simp_rw [neg.aux_of, tmul_smul s, smul_tmul', smul_neg]
| _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, add_comm]
end }
protected theorem add_left_neg (x : M ⊗[R] N) : -x + x = 0 :=
tensor_product.induction_on x
(by { rw [add_zero], apply (neg.aux R).map_zero, })
(λ x y, by { convert (add_tmul (-x) x y).symm, rw [add_left_neg, zero_tmul], })
(λ x y hx hy, by
{ unfold has_neg.neg sub_neg_monoid.neg,
rw add_monoid_hom.map_add,
ac_change (-x + x) + (-y + y) = 0,
rw [hx, hy, add_zero], })
instance : add_comm_group (M ⊗[R] N) :=
{ neg := has_neg.neg,
sub := _,
sub_eq_add_neg := λ _ _, rfl,
add_left_neg := λ x, by exact tensor_product.add_left_neg x,
zsmul := λ n v, n • v,
zsmul_zero' := by simp [tensor_product.zero_smul],
zsmul_succ' := by simp [nat.succ_eq_one_add, tensor_product.one_smul, tensor_product.add_smul],
zsmul_neg' := λ n x, begin
change (- n.succ : ℤ) • x = - (((n : ℤ) + 1) • x),
rw [← zero_add (-↑(n.succ) • x), ← tensor_product.add_left_neg (↑(n.succ) • x), add_assoc,
← add_smul, ← sub_eq_add_neg, sub_self, zero_smul, add_zero],
refl,
end,
.. tensor_product.add_comm_monoid }
lemma neg_tmul (m : M) (n : N) : (-m) ⊗ₜ n = -(m ⊗ₜ[R] n) := rfl
lemma tmul_neg (m : M) (n : N) : m ⊗ₜ (-n) = -(m ⊗ₜ[R] n) := (mk R M N _).map_neg _
lemma tmul_sub (m : M) (n₁ n₂ : N) : m ⊗ₜ (n₁ - n₂) = (m ⊗ₜ[R] n₁) - (m ⊗ₜ[R] n₂) :=
(mk R M N _).map_sub _ _
lemma sub_tmul (m₁ m₂ : M) (n : N) : (m₁ - m₂) ⊗ₜ n = (m₁ ⊗ₜ[R] n) - (m₂ ⊗ₜ[R] n) :=
(mk R M N).map_sub₂ _ _ _
/--
While the tensor product will automatically inherit a ℤ-module structure from
`add_comm_group.int_module`, that structure won't be compatible with lemmas like `tmul_smul` unless
we use a `ℤ-module` instance provided by `tensor_product.left_module`.
When `R` is a `ring` we get the required `tensor_product.compatible_smul` instance through
`is_scalar_tower`, but when it is only a `semiring` we need to build it from scratch.
The instance diamond in `compatible_smul` doesn't matter because it's in `Prop`.
-/
instance compatible_smul.int : compatible_smul R ℤ M N :=
⟨λ r m n, int.induction_on r
(by simp)
(λ r ih, by simpa [add_smul, tmul_add, add_tmul] using ih)
(λ r ih, by simpa [sub_smul, tmul_sub, sub_tmul] using ih)⟩
instance compatible_smul.unit {S} [monoid S] [distrib_mul_action S M] [distrib_mul_action S N]
[compatible_smul R S M N] :
compatible_smul R Sˣ M N :=
⟨λ s m n, (compatible_smul.smul_tmul (s : S) m n : _)⟩
end tensor_product
namespace linear_map
@[simp] lemma ltensor_sub (f g : N →ₗ[R] P) : (f - g).ltensor M = f.ltensor M - g.ltensor M :=
by simp only [← coe_ltensor_hom, map_sub]
@[simp] lemma rtensor_sub (f g : N →ₗ[R] P) : (f - g).rtensor M = f.rtensor M - g.rtensor M :=
by simp only [← coe_rtensor_hom, map_sub]
@[simp] lemma ltensor_neg (f : N →ₗ[R] P) : (-f).ltensor M = -(f.ltensor M) :=
by simp only [← coe_ltensor_hom, map_neg]
@[simp] lemma rtensor_neg (f : N →ₗ[R] P) : (-f).rtensor M = -(f.rtensor M) :=
by simp only [← coe_rtensor_hom, map_neg]
end linear_map
end ring
|
d75d6d4fca89c7acbc922d1b0c0b794f0fdadcc7 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/topology/metric_space/emetric_paracompact.lean | cb0a62325bc362bdd82d43ac72a2a83247ef4180 | [
"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 | 8,366 | lean | /-
Copyright (c) 202 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import set_theory.ordinal.basic
import topology.metric_space.emetric_space
import topology.paracompact
/-!
# (Extended) metric spaces are paracompact
In this file we provide two instances:
* `emetric.paracompact_space`: a `pseudo_emetric_space` is paracompact; formalization is based
on [MR0236876];
* `emetric.normal_of_metric`: an `emetric_space` is a normal topological space.
## Tags
metric space, paracompact space, normal space
-/
variable {α : Type*}
open_locale ennreal topological_space
open set
namespace emetric
/-- A `pseudo_emetric_space` is always a paracompact space. Formalization is based
on [MR0236876]. -/
@[priority 100] -- See note [lower instance priority]
instance [pseudo_emetric_space α] : paracompact_space α :=
begin
classical,
/- We start with trivial observations about `1 / 2 ^ k`. Here and below we use `1 / 2 ^ k` in
the comments and `2⁻¹ ^ k` in the code. -/
have pow_pos : ∀ k : ℕ, (0 : ℝ≥0∞) < 2⁻¹ ^ k,
from λ k, ennreal.pow_pos (ennreal.inv_pos.2 ennreal.two_ne_top) _,
have hpow_le : ∀ {m n : ℕ}, m ≤ n → (2⁻¹ : ℝ≥0∞) ^ n ≤ 2⁻¹ ^ m,
from λ m n h, ennreal.pow_le_pow_of_le_one (ennreal.inv_le_one.2 ennreal.one_lt_two.le) h,
have h2pow : ∀ n : ℕ, 2 * (2⁻¹ : ℝ≥0∞) ^ (n + 1) = 2⁻¹ ^ n,
by { intro n, simp [pow_succ, ← mul_assoc, ennreal.mul_inv_cancel] },
-- Consider an open covering `S : set (set α)`
refine ⟨λ ι s ho hcov, _⟩,
simp only [Union_eq_univ_iff] at hcov,
-- choose a well founded order on `S`
letI : linear_order ι := linear_order_of_STO well_ordering_rel,
have wf : well_founded ((<) : ι → ι → Prop) := @is_well_founded.wf ι well_ordering_rel _,
-- Let `ind x` be the minimal index `s : S` such that `x ∈ s`.
set ind : α → ι := λ x, wf.min {i : ι | x ∈ s i} (hcov x),
have mem_ind : ∀ x, x ∈ s (ind x), from λ x, wf.min_mem _ (hcov x),
have nmem_of_lt_ind : ∀ {x i}, i < (ind x) → x ∉ s i,
from λ x i hlt hxi, wf.not_lt_min _ (hcov x) hxi hlt,
/- The refinement `D : ℕ → ι → set α` is defined recursively. For each `n` and `i`, `D n i`
is the union of balls `ball x (1 / 2 ^ n)` over all points `x` such that
* `ind x = i`;
* `x` does not belong to any `D m j`, `m < n`;
* `ball x (3 / 2 ^ n) ⊆ s i`;
We define this sequence using `nat.strong_rec_on'`, then restate it as `Dn` and `memD`.
-/
set D : ℕ → ι → set α :=
λ n, nat.strong_rec_on' n (λ n D' i,
⋃ (x : α) (hxs : ind x = i) (hb : ball x (3 * 2⁻¹ ^ n) ⊆ s i)
(hlt : ∀ (m < n) (j : ι), x ∉ D' m ‹_› j), ball x (2⁻¹ ^ n)),
have Dn : ∀ n i, D n i = ⋃ (x : α) (hxs : ind x = i) (hb : ball x (3 * 2⁻¹ ^ n) ⊆ s i)
(hlt : ∀ (m < n) (j : ι), x ∉ D m j), ball x (2⁻¹ ^ n),
from λ n s, by { simp only [D], rw nat.strong_rec_on_beta' },
have memD : ∀ {n i y}, y ∈ D n i ↔ ∃ x (hi : ind x = i) (hb : ball x (3 * 2⁻¹ ^ n) ⊆ s i)
(hlt : ∀ (m < n) (j : ι), x ∉ D m j), edist y x < 2⁻¹ ^ n,
{ intros n i y, rw [Dn n i], simp only [mem_Union, mem_ball] },
-- The sets `D n i` cover the whole space. Indeed, for each `x` we can choose `n` such that
-- `ball x (3 / 2 ^ n) ⊆ s (ind x)`, then either `x ∈ D n i`, or `x ∈ D m i` for some `m < n`.
have Dcov : ∀ x, ∃ n i, x ∈ D n i,
{ intro x,
obtain ⟨n, hn⟩ : ∃ n : ℕ, ball x (3 * 2⁻¹ ^ n) ⊆ s (ind x),
{ -- This proof takes 5 lines because we can't import `specific_limits` here
rcases is_open_iff.1 (ho $ ind x) x (mem_ind x) with ⟨ε, ε0, hε⟩,
have : 0 < ε / 3 := ennreal.div_pos_iff.2 ⟨ε0.lt.ne', ennreal.coe_ne_top⟩,
rcases ennreal.exists_inv_two_pow_lt this.ne' with ⟨n, hn⟩,
refine ⟨n, subset.trans (ball_subset_ball _) hε⟩,
simpa only [div_eq_mul_inv, mul_comm] using (ennreal.mul_lt_of_lt_div hn).le },
by_contra' h,
apply h n (ind x),
exact memD.2 ⟨x, rfl, hn, λ _ _ _, h _ _, mem_ball_self (pow_pos _)⟩ },
-- Each `D n i` is a union of open balls, hence it is an open set
have Dopen : ∀ n i, is_open (D n i),
{ intros n i,
rw Dn,
iterate 4 { refine is_open_Union (λ _, _) },
exact is_open_ball },
-- the covering `D n i` is a refinement of the original covering: `D n i ⊆ s i`
have HDS : ∀ n i, D n i ⊆ s i,
{ intros n s x,
rw memD,
rintro ⟨y, rfl, hsub, -, hyx⟩,
refine hsub (lt_of_lt_of_le hyx _),
calc 2⁻¹ ^ n = 1 * 2⁻¹ ^ n : (one_mul _).symm
... ≤ 3 * 2⁻¹ ^ n : ennreal.mul_le_mul _ le_rfl,
-- TODO: use `norm_num`
have : ((1 : ℕ) : ℝ≥0∞) ≤ (3 : ℕ), from ennreal.coe_nat_le_coe_nat.2 (by norm_num1),
exact_mod_cast this },
-- Let us show the rest of the properties. Since the definition expects a family indexed
-- by a single parameter, we use `ℕ × ι` as the domain.
refine ⟨ℕ × ι, λ ni, D ni.1 ni.2, λ _, Dopen _ _, _, _, λ ni, ⟨ni.2, HDS _ _⟩⟩,
-- The sets `D n i` cover the whole space as we proved earlier
{ refine Union_eq_univ_iff.2 (λ x, _),
rcases Dcov x with ⟨n, i, h⟩,
exact ⟨⟨n, i⟩, h⟩ },
{ /- Let us prove that the covering `D n i` is locally finite. Take a point `x` and choose
`n`, `i` so that `x ∈ D n i`. Since `D n i` is an open set, we can choose `k` so that
`B = ball x (1 / 2 ^ (n + k + 1)) ⊆ D n i`. -/
intro x,
rcases Dcov x with ⟨n, i, hn⟩,
have : D n i ∈ 𝓝 x, from is_open.mem_nhds (Dopen _ _) hn,
rcases (nhds_basis_uniformity uniformity_basis_edist_inv_two_pow).mem_iff.1 this
with ⟨k, -, hsub : ball x (2⁻¹ ^ k) ⊆ D n i⟩,
set B := ball x (2⁻¹ ^ (n + k + 1)),
refine ⟨B, ball_mem_nhds _ (pow_pos _), _⟩,
-- The sets `D m i`, `m > n + k`, are disjoint with `B`
have Hgt : ∀ (m ≥ n + k + 1) (i : ι), disjoint (D m i) B,
{ rintros m hm i y ⟨hym, hyx⟩,
rcases memD.1 hym with ⟨z, rfl, hzi, H, hz⟩,
have : z ∉ ball x (2⁻¹ ^ k), from λ hz, H n (by linarith) i (hsub hz), apply this,
calc edist z x ≤ edist y z + edist y x : edist_triangle_left _ _ _
... < (2⁻¹ ^ m) + (2⁻¹ ^ (n + k + 1)) : ennreal.add_lt_add hz hyx
... ≤ (2⁻¹ ^ (k + 1)) + (2⁻¹ ^ (k + 1)) :
add_le_add (hpow_le $ by linarith) (hpow_le $ by linarith)
... = (2⁻¹ ^ k) : by rw [← two_mul, h2pow] },
-- For each `m ≤ n + k` there is at most one `j` such that `D m j ∩ B` is nonempty.
have Hle : ∀ m ≤ n + k, set.subsingleton {j | (D m j ∩ B).nonempty},
{ rintros m hm j₁ ⟨y, hyD, hyB⟩ j₂ ⟨z, hzD, hzB⟩,
by_contra h,
wlog h : j₁ < j₂ := ne.lt_or_lt h using [j₁ j₂ y z, j₂ j₁ z y],
rcases memD.1 hyD with ⟨y', rfl, hsuby, -, hdisty⟩,
rcases memD.1 hzD with ⟨z', rfl, -, -, hdistz⟩,
suffices : edist z' y' < 3 * 2⁻¹ ^ m, from nmem_of_lt_ind h (hsuby this),
calc edist z' y' ≤ edist z' x + edist x y' : edist_triangle _ _ _
... ≤ (edist z z' + edist z x) + (edist y x + edist y y') :
add_le_add (edist_triangle_left _ _ _) (edist_triangle_left _ _ _)
... < (2⁻¹ ^ m + 2⁻¹ ^ (n + k + 1)) + (2⁻¹ ^ (n + k + 1) + 2⁻¹ ^ m) :
by apply_rules [ennreal.add_lt_add]
... = 2 * (2⁻¹ ^ m + 2⁻¹ ^ (n + k + 1)) : by simp only [two_mul, add_comm]
... ≤ 2 * (2⁻¹ ^ m + 2⁻¹ ^ (m + 1)) :
ennreal.mul_le_mul le_rfl $ add_le_add le_rfl $ hpow_le (add_le_add hm le_rfl)
... = 3 * 2⁻¹ ^ m : by rw [mul_add, h2pow, bit1, add_mul, one_mul] },
-- Finally, we glue `Hgt` and `Hle`
have : (⋃ (m ≤ n + k) (i ∈ {i : ι | (D m i ∩ B).nonempty}), {(m, i)}).finite :=
(finite_le_nat _).bUnion' (λ i hi, (Hle i hi).finite.bUnion' (λ _ _, finite_singleton _)),
refine this.subset (λ I hI, _), simp only [mem_Union],
refine ⟨I.1, _, I.2, hI, prod.mk.eta.symm⟩,
exact not_lt.1 (λ hlt, Hgt I.1 hlt I.2 hI.some_spec) }
end
@[priority 100] -- see Note [lower instance priority]
instance normal_of_emetric [emetric_space α] : normal_space α := normal_of_paracompact_t2
end emetric
|
b5f892d8e981cfc39dd5f23a5575c7a0ef8657f0 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/limits/constructions/equalizers_auto.lean | e4926296ecac0e96a694fdfbb99c44dfedaf69d6 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,638 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.limits.shapes.equalizers
import Mathlib.category_theory.limits.shapes.binary_products
import Mathlib.category_theory.limits.shapes.pullbacks
import Mathlib.PostPort
universes u u_1 v
namespace Mathlib
/-!
# Constructing equalizers from pullbacks and binary products.
If a category has pullbacks and binary products, then it has equalizers.
TODO: provide the dual result.
-/
namespace category_theory.limits
-- We hide the "implementation details" inside a namespace
namespace has_equalizers_of_pullbacks_and_binary_products
/-- Define the equalizing object -/
def construct_equalizer {C : Type u} [category C] [has_binary_products C] [has_pullbacks C]
(F : walking_parallel_pair ⥤ C) : C :=
pullback (prod.lift 𝟙 (functor.map F walking_parallel_pair_hom.left))
(prod.lift 𝟙 (functor.map F walking_parallel_pair_hom.right))
/-- Define the equalizing morphism -/
def pullback_fst {C : Type u} [category C] [has_binary_products C] [has_pullbacks C]
(F : walking_parallel_pair ⥤ C) :
construct_equalizer F ⟶ functor.obj F walking_parallel_pair.zero :=
pullback.fst
theorem pullback_fst_eq_pullback_snd {C : Type u} [category C] [has_binary_products C]
[has_pullbacks C] (F : walking_parallel_pair ⥤ C) : pullback_fst F = pullback.snd :=
sorry
/-- Define the equalizing cone -/
def equalizer_cone {C : Type u} [category C] [has_binary_products C] [has_pullbacks C]
(F : walking_parallel_pair ⥤ C) : cone F :=
cone.of_fork (fork.of_ι (pullback_fst F) sorry)
/-- Show the equalizing cone is a limit -/
def equalizer_cone_is_limit {C : Type u} [category C] [has_binary_products C] [has_pullbacks C]
(F : walking_parallel_pair ⥤ C) : is_limit (equalizer_cone F) :=
is_limit.mk
fun (c : cone F) =>
pullback.lift (nat_trans.app (cone.π c) walking_parallel_pair.zero)
(nat_trans.app (cone.π c) walking_parallel_pair.zero) sorry
end has_equalizers_of_pullbacks_and_binary_products
/-- Any category with pullbacks and binary products, has equalizers. -/
-- This is not an instance, as it is not always how one wants to construct equalizers!
theorem has_equalizers_of_pullbacks_and_binary_products {C : Type u} [category C]
[has_binary_products C] [has_pullbacks C] : has_equalizers C :=
has_limits_of_shape.mk
fun (F : walking_parallel_pair ⥤ C) => has_limit.mk (limit_cone.mk sorry sorry)
end Mathlib |
89503efca87f8bd5b31d298970dc33ed18cb880a | 4727251e0cd73359b15b664c3170e5d754078599 | /src/computability/partrec_code.lean | 71d8e9d285f3a49445964b63ef5dee060908af77 | [
"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 | 43,311 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import computability.partrec
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `nat.partrec.code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evalution of these codes as partial functions using `pfun`, and proves that a
function is partially recursive (as defined by `nat.partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `nat.partrec.code`: Inductive datatype for partial recursive codes.
* `nat.partrec.code.encode_code`: A (computable) encoding of codes as natural numbers.
* `nat.partrec.code.of_nat_code`: The inverse of this encoding.
* `nat.partrec.code.eval`: The interpretation of a `nat.partrec.code` as a partial function.
## Main Results
* `nat.partrec.code.rec_prim`: Recursion on `nat.partrec.code` is primitive recursive.
* `nat.partrec.code.rec_computable`: Recursion on `nat.partrec.code` is computable.
* `nat.partrec.code.smn`: The $S_n^m$ theorem.
* `nat.partrec.code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `nat.partrec.code.evaln_prim`: `evaln` is primitive recursive.
* `nat.partrec.code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open encodable denumerable
namespace nat.partrec
open nat (mkpair)
theorem rfind' {f} (hf : nat.partrec f) : nat.partrec (nat.unpaired (λ a m,
(nat.rfind (λ n, (λ m, m = 0) <$> f (mkpair a (n + m)))).map (+ m))) :=
partrec₂.unpaired'.2 $
begin
refine partrec.map
((@partrec₂.unpaired' (λ (a b : ℕ),
nat.rfind (λ n, (λ m, m = 0) <$> f (mkpair a (n + b))))).1 _)
(primrec.nat_add.comp primrec.snd $
primrec.snd.comp primrec.fst).to_comp.to₂,
have := rfind (partrec₂.unpaired'.2 ((partrec.nat_iff.2 hf).comp
(primrec₂.mkpair.comp
(primrec.fst.comp $ primrec.unpair.comp primrec.fst)
(primrec.nat_add.comp primrec.snd
(primrec.snd.comp $ primrec.unpair.comp primrec.fst))).to_comp).to₂),
simp at this, exact this
end
/--
Code for partial recursive functions from ℕ to ℕ.
See `nat.partrec.code.eval` for the interpretation of these constructors.
-/
inductive code : Type
| zero : code
| succ : code
| left : code
| right : code
| pair : code → code → code
| comp : code → code → code
| prec : code → code → code
| rfind' : code → code
end nat.partrec
namespace nat.partrec.code
open nat (mkpair unpair)
open nat.partrec (code)
instance : inhabited code := ⟨zero⟩
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → code
| 0 := zero
| (n+1) := comp succ (const n)
theorem const_inj : Π {n₁ n₂}, nat.partrec.code.const n₁ = nat.partrec.code.const n₂ → n₁ = n₂
| 0 0 h := by simp
| (n₁+1) (n₂+1) h := by { dsimp [nat.partrec.code.const] at h,
injection h with h₁ h₂,
simp only [const_inj h₂] }
/-- A code for the identity function. -/
protected def id : code := pair left right
/--
Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : code) (n : ℕ) : code :=
comp c (pair (code.const n) code.id)
/-- An encoding of a `nat.partrec.code` as a ℕ. -/
def encode_code : code → ℕ
| zero := 0
| succ := 1
| left := 2
| right := 3
| (pair cf cg) := bit0 (bit0 $ mkpair (encode_code cf) (encode_code cg)) + 4
| (comp cf cg) := bit0 (bit1 $ mkpair (encode_code cf) (encode_code cg)) + 4
| (prec cf cg) := bit1 (bit0 $ mkpair (encode_code cf) (encode_code cg)) + 4
| (rfind' cf) := bit1 (bit1 $ encode_code cf) + 4
/--
A decoder for `nat.partrec.code.encode_code`, taking any ℕ to the `nat.partrec.code` it represents.
-/
def of_nat_code : ℕ → code
| 0 := zero
| 1 := succ
| 2 := left
| 3 := right
| (n+4) := let m := n.div2.div2 in
have hm : m < n + 4, by simp [m, nat.div2_val];
from lt_of_le_of_lt
(le_trans (nat.div_le_self _ _) (nat.div_le_self _ _))
(nat.succ_le_succ (nat.le_add_right _ _)),
have m1 : m.unpair.1 < n + 4, from lt_of_le_of_lt m.unpair_left_le hm,
have m2 : m.unpair.2 < n + 4, from lt_of_le_of_lt m.unpair_right_le hm,
match n.bodd, n.div2.bodd with
| ff, ff := pair (of_nat_code m.unpair.1) (of_nat_code m.unpair.2)
| ff, tt := comp (of_nat_code m.unpair.1) (of_nat_code m.unpair.2)
| tt, ff := prec (of_nat_code m.unpair.1) (of_nat_code m.unpair.2)
| tt, tt := rfind' (of_nat_code m)
end
/-- Proof that `nat.partrec.code.of_nat_code` is the inverse of `nat.partrec.code.encode_code`-/
private theorem encode_of_nat_code : ∀ n, encode_code (of_nat_code n) = n
| 0 := by simp [of_nat_code, encode_code]
| 1 := by simp [of_nat_code, encode_code]
| 2 := by simp [of_nat_code, encode_code]
| 3 := by simp [of_nat_code, encode_code]
| (n+4) := let m := n.div2.div2 in
have hm : m < n + 4, by simp [m, nat.div2_val];
from lt_of_le_of_lt
(le_trans (nat.div_le_self _ _) (nat.div_le_self _ _))
(nat.succ_le_succ (nat.le_add_right _ _)),
have m1 : m.unpair.1 < n + 4, from lt_of_le_of_lt m.unpair_left_le hm,
have m2 : m.unpair.2 < n + 4, from lt_of_le_of_lt m.unpair_right_le hm,
have IH : _ := encode_of_nat_code m,
have IH1 : _ := encode_of_nat_code m.unpair.1,
have IH2 : _ := encode_of_nat_code m.unpair.2,
begin
transitivity, swap,
rw [← nat.bit_decomp n, ← nat.bit_decomp n.div2],
simp [encode_code, of_nat_code, -add_comm],
cases n.bodd; cases n.div2.bodd;
simp [encode_code, of_nat_code, -add_comm, IH, IH1, IH2, m, nat.bit]
end
instance : denumerable code :=
mk' ⟨encode_code, of_nat_code,
λ c, by induction c; try {refl}; simp [
encode_code, of_nat_code, -add_comm, *],
encode_of_nat_code⟩
theorem encode_code_eq : encode = encode_code := rfl
theorem of_nat_code_eq : of_nat code = of_nat_code := rfl
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧
encode cg < encode (pair cf cg) :=
begin
simp [encode_code_eq, encode_code, -add_comm],
have := nat.mul_le_mul_right _ (dec_trivial : 1 ≤ 2*2),
rw [one_mul, mul_assoc, ← bit0_eq_two_mul, ← bit0_eq_two_mul] at this,
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (dec_trivial:0<4)),
exact ⟨
lt_of_le_of_lt (nat.left_le_mkpair _ _) this,
lt_of_le_of_lt (nat.right_le_mkpair _ _) this⟩
end
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧
encode cg < encode (comp cf cg) :=
begin
suffices, exact (encode_lt_pair cf cg).imp
(λ h, lt_trans h this) (λ h, lt_trans h this),
change _, simp [encode_code_eq, encode_code]
end
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧
encode cg < encode (prec cf cg) :=
begin
suffices, exact (encode_lt_pair cf cg).imp
(λ h, lt_trans h this) (λ h, lt_trans h this),
change _, simp [encode_code_eq, encode_code],
end
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) :=
begin
simp [encode_code_eq, encode_code, -add_comm],
have := nat.mul_le_mul_right _ (dec_trivial : 1 ≤ 2*2),
rw [one_mul, mul_assoc, ← bit0_eq_two_mul, ← bit0_eq_two_mul] at this,
refine lt_of_le_of_lt (le_trans this _)
(lt_add_of_pos_right _ (dec_trivial:0<4)),
exact le_of_lt (nat.bit0_lt_bit1 $ le_of_lt $
nat.bit0_lt_bit1 $ le_rfl),
end
section
open primrec
theorem pair_prim : primrec₂ pair :=
primrec₂.of_nat_iff.2 $ primrec₂.encode_iff.1 $ nat_add.comp
(nat_bit0.comp $ nat_bit0.comp $
primrec₂.mkpair.comp
(encode_iff.2 $ (primrec.of_nat code).comp fst)
(encode_iff.2 $ (primrec.of_nat code).comp snd))
(primrec₂.const 4)
theorem comp_prim : primrec₂ comp :=
primrec₂.of_nat_iff.2 $ primrec₂.encode_iff.1 $ nat_add.comp
(nat_bit0.comp $ nat_bit1.comp $
primrec₂.mkpair.comp
(encode_iff.2 $ (primrec.of_nat code).comp fst)
(encode_iff.2 $ (primrec.of_nat code).comp snd))
(primrec₂.const 4)
theorem prec_prim : primrec₂ prec :=
primrec₂.of_nat_iff.2 $ primrec₂.encode_iff.1 $ nat_add.comp
(nat_bit1.comp $ nat_bit0.comp $
primrec₂.mkpair.comp
(encode_iff.2 $ (primrec.of_nat code).comp fst)
(encode_iff.2 $ (primrec.of_nat code).comp snd))
(primrec₂.const 4)
theorem rfind_prim : primrec rfind' :=
of_nat_iff.2 $ encode_iff.1 $ nat_add.comp
(nat_bit1.comp $ nat_bit1.comp $
encode_iff.2 $ primrec.of_nat code)
(const 4)
theorem rec_prim' {α σ} [primcodable α] [primcodable σ]
{c : α → code} (hc : primrec c)
{z : α → σ} (hz : primrec z)
{s : α → σ} (hs : primrec s)
{l : α → σ} (hl : primrec l)
{r : α → σ} (hr : primrec r)
{pr : α → code × code × σ × σ → σ} (hpr : primrec₂ pr)
{co : α → code × code × σ × σ → σ} (hco : primrec₂ co)
{pc : α → code × code × σ × σ → σ} (hpc : primrec₂ pc)
{rf : α → code × σ → σ} (hrf : primrec₂ rf) :
let PR (a) := λ cf cg hf hg, pr a (cf, cg, hf, hg),
CO (a) := λ cf cg hf hg, co a (cf, cg, hf, hg),
PC (a) := λ cf cg hf hg, pc a (cf, cg, hf, hg),
RF (a) := λ cf hf, rf a (cf, hf),
F (a : α) (c : code) : σ := nat.partrec.code.rec_on c
(z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a) in
primrec (λ a, F a (c a) : α → σ) :=
begin
intros,
let G₁ : (α × list σ) × ℕ × ℕ → option σ := λ p,
let a := p.1.1, IH := p.1.2, n := p.2.1, m := p.2.2 in
(IH.nth m).bind $ λ s,
(IH.nth m.unpair.1).bind $ λ s₁,
(IH.nth m.unpair.2).map $ λ s₂,
cond n.bodd
(cond n.div2.bodd
(rf a (of_nat code m, s))
(pc a (of_nat code m.unpair.1, of_nat code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd
(co a (of_nat code m.unpair.1, of_nat code m.unpair.2, s₁, s₂))
(pr a (of_nat code m.unpair.1, of_nat code m.unpair.2, s₁, s₂))),
have : primrec G₁,
{ refine option_bind (list_nth.comp (snd.comp fst) (snd.comp snd)) _,
refine option_bind ((list_nth.comp (snd.comp fst)
(fst.comp $ primrec.unpair.comp (snd.comp snd))).comp fst) _,
refine option_map ((list_nth.comp (snd.comp fst)
(snd.comp $ primrec.unpair.comp (snd.comp snd))).comp $ fst.comp fst) _,
have a := fst.comp (fst.comp $ fst.comp $ fst.comp fst),
have n := fst.comp (snd.comp $ fst.comp $ fst.comp fst),
have m := snd.comp (snd.comp $ fst.comp $ fst.comp fst),
have m₁ := fst.comp (primrec.unpair.comp m),
have m₂ := snd.comp (primrec.unpair.comp m),
have s := snd.comp (fst.comp fst),
have s₁ := snd.comp fst,
have s₂ := snd,
exact (nat_bodd.comp n).cond
((nat_bodd.comp $ nat_div2.comp n).cond
(hrf.comp a (((primrec.of_nat code).comp m).pair s))
(hpc.comp a (((primrec.of_nat code).comp m₁).pair $
((primrec.of_nat code).comp m₂).pair $ s₁.pair s₂)))
(primrec.cond (nat_bodd.comp $ nat_div2.comp n)
(hco.comp a (((primrec.of_nat code).comp m₁).pair $
((primrec.of_nat code).comp m₂).pair $ s₁.pair s₂))
(hpr.comp a (((primrec.of_nat code).comp m₁).pair $
((primrec.of_nat code).comp m₂).pair $ s₁.pair s₂))) },
let G : α → list σ → option σ := λ a IH,
IH.length.cases (some (z a)) $ λ n,
n.cases (some (s a)) $ λ n,
n.cases (some (l a)) $ λ n,
n.cases (some (r a)) $ λ n,
G₁ ((a, IH), n, n.div2.div2),
have : primrec₂ G := (nat_cases
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) $
nat_cases snd (option_some_iff.2 (hs.comp (fst.comp fst))) $
nat_cases snd (option_some_iff.2 (hl.comp (fst.comp $ fst.comp fst))) $
nat_cases snd (option_some_iff.2 (hr.comp (fst.comp $ fst.comp $ fst.comp fst)))
(this.comp $
((fst.pair snd).comp $ fst.comp $ fst.comp $ fst.comp $ fst).pair $
snd.pair $ nat_div2.comp $ nat_div2.comp snd)),
refine ((nat_strong_rec
(λ a n, F a (of_nat code n)) this.to₂ $ λ a n, _).comp
primrec.id $ encode_iff.2 hc).of_eq (λ a, by simp),
simp,
iterate 4 {cases n with n, {simp [of_nat_code_eq, of_nat_code]; refl}},
simp [G], rw [list.length_map, list.length_range],
let m := n.div2.div2,
show G₁ ((a, (list.range (n+4)).map (λ n, F a (of_nat code n))), n, m)
= some (F a (of_nat code (n+4))),
have hm : m < n + 4, by simp [nat.div2_val, m];
from lt_of_le_of_lt
(le_trans (nat.div_le_self _ _) (nat.div_le_self _ _))
(nat.succ_le_succ (nat.le_add_right _ _)),
have m1 : m.unpair.1 < n + 4, from lt_of_le_of_lt m.unpair_left_le hm,
have m2 : m.unpair.2 < n + 4, from lt_of_le_of_lt m.unpair_right_le hm,
simp [G₁], simp [list.nth_map, list.nth_range, hm, m1, m2],
change of_nat code (n+4) with of_nat_code (n+4),
simp [of_nat_code],
cases n.bodd; cases n.div2.bodd; refl
end
/-- Recursion on `nat.partrec.code` is primitive recursive. -/
theorem rec_prim {α σ} [primcodable α] [primcodable σ]
{c : α → code} (hc : primrec c)
{z : α → σ} (hz : primrec z)
{s : α → σ} (hs : primrec s)
{l : α → σ} (hl : primrec l)
{r : α → σ} (hr : primrec r)
{pr : α → code → code → σ → σ → σ}
(hpr : primrec (λ a : α × code × code × σ × σ,
pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2))
{co : α → code → code → σ → σ → σ}
(hco : primrec (λ a : α × code × code × σ × σ,
co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2))
{pc : α → code → code → σ → σ → σ}
(hpc : primrec (λ a : α × code × code × σ × σ,
pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2))
{rf : α → code → σ → σ}
(hrf : primrec (λ a : α × code × σ, rf a.1 a.2.1 a.2.2)) :
let F (a : α) (c : code) : σ := nat.partrec.code.rec_on c
(z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a) in
primrec (λ a, F a (c a)) :=
begin
intros,
let G₁ : (α × list σ) × ℕ × ℕ → option σ := λ p,
let a := p.1.1, IH := p.1.2, n := p.2.1, m := p.2.2 in
(IH.nth m).bind $ λ s,
(IH.nth m.unpair.1).bind $ λ s₁,
(IH.nth m.unpair.2).map $ λ s₂,
cond n.bodd
(cond n.div2.bodd
(rf a (of_nat code m) s)
(pc a (of_nat code m.unpair.1) (of_nat code m.unpair.2) s₁ s₂))
(cond n.div2.bodd
(co a (of_nat code m.unpair.1) (of_nat code m.unpair.2) s₁ s₂)
(pr a (of_nat code m.unpair.1) (of_nat code m.unpair.2) s₁ s₂)),
have : primrec G₁,
{ refine option_bind (list_nth.comp (snd.comp fst) (snd.comp snd)) _,
refine option_bind ((list_nth.comp (snd.comp fst)
(fst.comp $ primrec.unpair.comp (snd.comp snd))).comp fst) _,
refine option_map ((list_nth.comp (snd.comp fst)
(snd.comp $ primrec.unpair.comp (snd.comp snd))).comp $ fst.comp fst) _,
have a := fst.comp (fst.comp $ fst.comp $ fst.comp fst),
have n := fst.comp (snd.comp $ fst.comp $ fst.comp fst),
have m := snd.comp (snd.comp $ fst.comp $ fst.comp fst),
have m₁ := fst.comp (primrec.unpair.comp m),
have m₂ := snd.comp (primrec.unpair.comp m),
have s := snd.comp (fst.comp fst),
have s₁ := snd.comp fst,
have s₂ := snd,
exact (nat_bodd.comp n).cond
((nat_bodd.comp $ nat_div2.comp n).cond
(hrf.comp $ a.pair (((primrec.of_nat code).comp m).pair s))
(hpc.comp $ a.pair (((primrec.of_nat code).comp m₁).pair $
((primrec.of_nat code).comp m₂).pair $ s₁.pair s₂)))
(primrec.cond (nat_bodd.comp $ nat_div2.comp n)
(hco.comp $ a.pair (((primrec.of_nat code).comp m₁).pair $
((primrec.of_nat code).comp m₂).pair $ s₁.pair s₂))
(hpr.comp $ a.pair (((primrec.of_nat code).comp m₁).pair $
((primrec.of_nat code).comp m₂).pair $ s₁.pair s₂))) },
let G : α → list σ → option σ := λ a IH,
IH.length.cases (some (z a)) $ λ n,
n.cases (some (s a)) $ λ n,
n.cases (some (l a)) $ λ n,
n.cases (some (r a)) $ λ n,
G₁ ((a, IH), n, n.div2.div2),
have : primrec₂ G := (nat_cases
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) $
nat_cases snd (option_some_iff.2 (hs.comp (fst.comp fst))) $
nat_cases snd (option_some_iff.2 (hl.comp (fst.comp $ fst.comp fst))) $
nat_cases snd (option_some_iff.2 (hr.comp (fst.comp $ fst.comp $ fst.comp fst)))
(this.comp $
((fst.pair snd).comp $ fst.comp $ fst.comp $ fst.comp $ fst).pair $
snd.pair $ nat_div2.comp $ nat_div2.comp snd)),
refine ((nat_strong_rec
(λ a n, F a (of_nat code n)) this.to₂ $ λ a n, _).comp
primrec.id $ encode_iff.2 hc).of_eq (λ a, by simp),
simp,
iterate 4 {cases n with n, {simp [of_nat_code_eq, of_nat_code]; refl}},
simp [G], rw [list.length_map, list.length_range],
let m := n.div2.div2,
show G₁ ((a, (list.range (n+4)).map (λ n, F a (of_nat code n))), n, m)
= some (F a (of_nat code (n+4))),
have hm : m < n + 4, by simp [nat.div2_val, m];
from lt_of_le_of_lt
(le_trans (nat.div_le_self _ _) (nat.div_le_self _ _))
(nat.succ_le_succ (nat.le_add_right _ _)),
have m1 : m.unpair.1 < n + 4, from lt_of_le_of_lt m.unpair_left_le hm,
have m2 : m.unpair.2 < n + 4, from lt_of_le_of_lt m.unpair_right_le hm,
simp [G₁], simp [list.nth_map, list.nth_range, hm, m1, m2],
change of_nat code (n+4) with of_nat_code (n+4),
simp [of_nat_code],
cases n.bodd; cases n.div2.bodd; refl
end
end
section
open computable
/-- Recursion on `nat.partrec.code` is computable. -/
theorem rec_computable {α σ} [primcodable α] [primcodable σ]
{c : α → code} (hc : computable c)
{z : α → σ} (hz : computable z)
{s : α → σ} (hs : computable s)
{l : α → σ} (hl : computable l)
{r : α → σ} (hr : computable r)
{pr : α → code × code × σ × σ → σ} (hpr : computable₂ pr)
{co : α → code × code × σ × σ → σ} (hco : computable₂ co)
{pc : α → code × code × σ × σ → σ} (hpc : computable₂ pc)
{rf : α → code × σ → σ} (hrf : computable₂ rf) :
let PR (a) := λ cf cg hf hg, pr a (cf, cg, hf, hg),
CO (a) := λ cf cg hf hg, co a (cf, cg, hf, hg),
PC (a) := λ cf cg hf hg, pc a (cf, cg, hf, hg),
RF (a) := λ cf hf, rf a (cf, hf),
F (a : α) (c : code) : σ := nat.partrec.code.rec_on c
(z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a) in
computable (λ a, F a (c a)) :=
begin
-- TODO(Mario): less copy-paste from previous proof
intros,
let G₁ : (α × list σ) × ℕ × ℕ → option σ := λ p,
let a := p.1.1, IH := p.1.2, n := p.2.1, m := p.2.2 in
(IH.nth m).bind $ λ s,
(IH.nth m.unpair.1).bind $ λ s₁,
(IH.nth m.unpair.2).map $ λ s₂,
cond n.bodd
(cond n.div2.bodd
(rf a (of_nat code m, s))
(pc a (of_nat code m.unpair.1, of_nat code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd
(co a (of_nat code m.unpair.1, of_nat code m.unpair.2, s₁, s₂))
(pr a (of_nat code m.unpair.1, of_nat code m.unpair.2, s₁, s₂))),
have : computable G₁,
{ refine option_bind (list_nth.comp (snd.comp fst) (snd.comp snd)) _,
refine option_bind ((list_nth.comp (snd.comp fst)
(fst.comp $ computable.unpair.comp (snd.comp snd))).comp fst) _,
refine option_map ((list_nth.comp (snd.comp fst)
(snd.comp $ computable.unpair.comp (snd.comp snd))).comp $ fst.comp fst) _,
have a := fst.comp (fst.comp $ fst.comp $ fst.comp fst),
have n := fst.comp (snd.comp $ fst.comp $ fst.comp fst),
have m := snd.comp (snd.comp $ fst.comp $ fst.comp fst),
have m₁ := fst.comp (computable.unpair.comp m),
have m₂ := snd.comp (computable.unpair.comp m),
have s := snd.comp (fst.comp fst),
have s₁ := snd.comp fst,
have s₂ := snd,
exact (nat_bodd.comp n).cond
((nat_bodd.comp $ nat_div2.comp n).cond
(hrf.comp a (((computable.of_nat code).comp m).pair s))
(hpc.comp a (((computable.of_nat code).comp m₁).pair $
((computable.of_nat code).comp m₂).pair $ s₁.pair s₂)))
(computable.cond (nat_bodd.comp $ nat_div2.comp n)
(hco.comp a (((computable.of_nat code).comp m₁).pair $
((computable.of_nat code).comp m₂).pair $ s₁.pair s₂))
(hpr.comp a (((computable.of_nat code).comp m₁).pair $
((computable.of_nat code).comp m₂).pair $ s₁.pair s₂))) },
let G : α → list σ → option σ := λ a IH,
IH.length.cases (some (z a)) $ λ n,
n.cases (some (s a)) $ λ n,
n.cases (some (l a)) $ λ n,
n.cases (some (r a)) $ λ n,
G₁ ((a, IH), n, n.div2.div2),
have : computable₂ G := (nat_cases
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) $
nat_cases snd (option_some_iff.2 (hs.comp (fst.comp fst))) $
nat_cases snd (option_some_iff.2 (hl.comp (fst.comp $ fst.comp fst))) $
nat_cases snd (option_some_iff.2 (hr.comp (fst.comp $ fst.comp $ fst.comp fst)))
(this.comp $
((fst.pair snd).comp $ fst.comp $ fst.comp $ fst.comp $ fst).pair $
snd.pair $ nat_div2.comp $ nat_div2.comp snd)),
refine ((nat_strong_rec
(λ a n, F a (of_nat code n)) this.to₂ $ λ a n, _).comp
computable.id $ encode_iff.2 hc).of_eq (λ a, by simp),
simp,
iterate 4 {cases n with n, {simp [of_nat_code_eq, of_nat_code]; refl}},
simp [G], rw [list.length_map, list.length_range],
let m := n.div2.div2,
show G₁ ((a, (list.range (n+4)).map (λ n, F a (of_nat code n))), n, m)
= some (F a (of_nat code (n+4))),
have hm : m < n + 4, by simp [nat.div2_val, m];
from lt_of_le_of_lt
(le_trans (nat.div_le_self _ _) (nat.div_le_self _ _))
(nat.succ_le_succ (nat.le_add_right _ _)),
have m1 : m.unpair.1 < n + 4, from lt_of_le_of_lt m.unpair_left_le hm,
have m2 : m.unpair.2 < n + 4, from lt_of_le_of_lt m.unpair_right_le hm,
simp [G₁], simp [list.nth_map, list.nth_range, hm, m1, m2],
change of_nat code (n+4) with of_nat_code (n+4),
simp [of_nat_code],
cases n.bodd; cases n.div2.bodd; refl
end
end
/--
The interpretation of a `nat.partrec.code` as a partial function.
* `nat.partrec.code.zero`: The constant zero function.
* `nat.partrec.code.succ`: The successor function.
* `nat.partrec.code.left`: Left unpairing of a pair of ℕ (encoded by `nat.mkpair`)
* `nat.partrec.code.right`: Right unpairing of a pair of ℕ (encoded by `nat.mkpair`)
* `nat.partrec.code.pair`: Pairs the outputs of argument codes using `nat.mkpair`.
* `nat.partrec.code.comp`: Composition of two argument codes.
* `nat.partrec.code.prec`: Primitive recursion. Given an argument of the form `nat.mkpair a n`:
* If `n = 0`, returns `eval cf a`.
* If `n = succ k`, returns `eval cg (mkpair a (mkpair k (eval (prec cf cg) (mkpair a k))))`
* `nat.partrec.code.rfind'`: Minimization. For `f` an argument of the form `nat.mkpair a m`,
`rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates
for `b < a`
-/
def eval : code → ℕ →. ℕ
| zero := pure 0
| succ := nat.succ
| left := ↑(λ n : ℕ, n.unpair.1)
| right := ↑(λ n : ℕ, n.unpair.2)
| (pair cf cg) := λ n, mkpair <$> eval cf n <*> eval cg n
| (comp cf cg) := λ n, eval cg n >>= eval cf
| (prec cf cg) := nat.unpaired (λ a n,
n.elim (eval cf a) (λ y IH, do i ← IH, eval cg (mkpair a (mkpair y i))))
| (rfind' cf) := nat.unpaired (λ a m,
(nat.rfind (λ n, (λ m, m = 0) <$>
eval cf (mkpair a (n + m)))).map (+ m))
/-- Helper lemma for the evaluation of `prec` in the base case. -/
@[simp] lemma eval_prec_zero (cf cg : code) (a : ℕ) : eval (prec cf cg) (mkpair a 0) = eval cf a :=
by rw [eval, nat.unpaired, nat.unpair_mkpair, nat.elim_zero]
/-- Helper lemma for the evaluation of `prec` in the recursive case. -/
lemma eval_prec_succ (cf cg : code) (a k : ℕ) :
eval (prec cf cg) (mkpair a (nat.succ k)) =
do ih <- eval (prec cf cg) (mkpair a k), eval cg (mkpair a (mkpair k ih)) :=
begin
rw [eval, nat.unpaired, part.bind_eq_bind, nat.unpair_mkpair, nat.elim_succ],
simp,
end
instance : has_mem (ℕ →. ℕ) code := ⟨λ f c, eval c = f⟩
@[simp] theorem eval_const : ∀ n m, eval (code.const n) m = part.some n
| 0 m := rfl
| (n+1) m := by simp! *
@[simp] theorem eval_id (n) : eval code.id n = part.some n := by simp! [(<*>)]
@[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (mkpair n x) :=
by simp! [(<*>)]
theorem const_prim : primrec code.const :=
(primrec.id.nat_iterate (primrec.const zero)
(comp_prim.comp (primrec.const succ) primrec.snd).to₂).of_eq $
λ n, by simp; induction n; simp [*, code.const, function.iterate_succ']
theorem curry_prim : primrec₂ curry :=
comp_prim.comp primrec.fst $
pair_prim.comp (const_prim.comp primrec.snd) (primrec.const code.id)
theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ :=
⟨by injection h, by { injection h,
injection h with h₁ h₂,
injection h₂ with h₃ h₄,
exact const_inj h₃ }⟩
/--
The $S_n^m$ theorem: There is a computable function, namely `nat.partrec.code.curry`, that takes a
program and a ℕ `n`, and returns a new program using `n` as the first argument.
-/
theorem smn : ∃ f : code → ℕ → code,
computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (mkpair n x) :=
⟨curry, primrec₂.to_comp curry_prim, eval_curry⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/
theorem exists_code {f : ℕ →. ℕ} : nat.partrec f ↔ ∃ c : code, eval c = f :=
⟨λ h, begin
induction h,
case nat.partrec.zero { exact ⟨zero, rfl⟩ },
case nat.partrec.succ { exact ⟨succ, rfl⟩ },
case nat.partrec.left { exact ⟨left, rfl⟩ },
case nat.partrec.right { exact ⟨right, rfl⟩ },
case nat.partrec.pair : f g pf pg hf hg
{ rcases hf with ⟨cf, rfl⟩, rcases hg with ⟨cg, rfl⟩,
exact ⟨pair cf cg, rfl⟩ },
case nat.partrec.comp : f g pf pg hf hg
{ rcases hf with ⟨cf, rfl⟩, rcases hg with ⟨cg, rfl⟩,
exact ⟨comp cf cg, rfl⟩ },
case nat.partrec.prec : f g pf pg hf hg
{ rcases hf with ⟨cf, rfl⟩, rcases hg with ⟨cg, rfl⟩,
exact ⟨prec cf cg, rfl⟩ },
case nat.partrec.rfind : f pf hf
{ rcases hf with ⟨cf, rfl⟩,
refine ⟨comp (rfind' cf) (pair code.id zero), _⟩,
simp [eval, (<*>), pure, pfun.pure, part.map_id'] },
end, λ h, begin
rcases h with ⟨c, rfl⟩, induction c,
case nat.partrec.code.zero { exact nat.partrec.zero },
case nat.partrec.code.succ { exact nat.partrec.succ },
case nat.partrec.code.left { exact nat.partrec.left },
case nat.partrec.code.right { exact nat.partrec.right },
case nat.partrec.code.pair : cf cg pf pg { exact pf.pair pg },
case nat.partrec.code.comp : cf cg pf pg { exact pf.comp pg },
case nat.partrec.code.prec : cf cg pf pg { exact pf.prec pg },
case nat.partrec.code.rfind' : cf pf { exact pf.rfind' },
end⟩
/--
A modified evaluation for the code which returns an `option ℕ` instead of a `part ℕ`. To avoid
undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course
of its execution. Other than this, the semantics are the same as in `nat.partrec.code.eval`.
-/
def evaln : ∀ k : ℕ, code → ℕ → option ℕ
| 0 _ := λ m, none
| (k+1) zero := λ n, guard (n ≤ k) >> pure 0
| (k+1) succ := λ n, guard (n ≤ k) >> pure (nat.succ n)
| (k+1) left := λ n, guard (n ≤ k) >> pure n.unpair.1
| (k+1) right := λ n, guard (n ≤ k) >> pure n.unpair.2
| (k+1) (pair cf cg) := λ n, guard (n ≤ k) >>
mkpair <$> evaln (k+1) cf n <*> evaln (k+1) cg n
| (k+1) (comp cf cg) := λ n, guard (n ≤ k) >>
do x ← evaln (k+1) cg n, evaln (k+1) cf x
| (k+1) (prec cf cg) := λ n, guard (n ≤ k) >>
n.unpaired (λ a n,
n.cases (evaln (k+1) cf a) $ λ y, do
i ← evaln k (prec cf cg) (mkpair a y),
evaln (k+1) cg (mkpair a (mkpair y i)))
| (k+1) (rfind' cf) := λ n, guard (n ≤ k) >>
n.unpaired (λ a m, do
x ← evaln (k+1) cf (mkpair a m),
if x = 0 then pure m else
evaln k (rfind' cf) (mkpair a (m+1)))
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k
| 0 c n x h := by simp [evaln] at h; cases h
| (k+1) c n x h := begin
suffices : ∀ {o : option ℕ}, x ∈ guard (n ≤ k) >> o → n < k + 1,
{ cases c; rw [evaln] at h; exact this h },
simpa [(>>)] using nat.lt_succ_of_le
end
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0 k₂ c n x hl h := by simp [evaln] at h; cases h
| (k+1) (k₂+1) c n x hl h := begin
have hl' := nat.le_of_succ_le_succ hl,
have : ∀ {k k₂ n x : ℕ} {o₁ o₂ : option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) → x ∈ guard (n ≤ k) >> o₁ → x ∈ guard (n ≤ k₂) >> o₂,
{ simp [(>>)], introv h h₁ h₂ h₃, exact ⟨le_trans h₂ h, h₁ h₃⟩ },
simp at h ⊢,
induction c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n;
rw [evaln] at h ⊢; refine this hl' (λ h, _) h,
iterate 4 {exact h},
{ -- pair cf cg
simp [(<*>)] at h ⊢,
exact h.imp (λ a, and.imp (hf _ _) $ Exists.imp $ λ b, and.imp_left (hg _ _)) },
{ -- comp cf cg
simp at h ⊢,
exact h.imp (λ a, and.imp (hg _ _) (hf _ _)) },
{ -- prec cf cg
revert h, simp,
induction n.unpair.2; simp,
{ apply hf },
{ exact λ y h₁ h₂, ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩ } },
{ -- rfind' cf
simp at h ⊢,
refine h.imp (λ x, and.imp (hf _ _) _),
by_cases x0 : x = 0; simp [x0],
exact evaln_mono hl' }
end
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0 _ n x h := by simp [evaln] at h; cases h
| (k+1) c n x h := begin
induction c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n;
simp [eval, evaln, (>>), (<*>)] at h ⊢; cases h with _ h,
iterate 4 {simpa [pure, pfun.pure, eq_comm] using h},
{ -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩,
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩ },
{ --comp hf hg
rcases h with ⟨y, eg, ef⟩,
exact ⟨_, hg _ _ eg, hf _ _ ef⟩ },
{ -- prec cf cg
revert h,
induction n.unpair.2 with m IH generalizing x; simp,
{ apply hf },
{ refine λ y h₁ h₂, ⟨y, IH _ _, _⟩,
{ have := evaln_mono k.le_succ h₁,
simp [evaln, (>>)] at this,
exact this.2 },
{ exact hg _ _ h₂ } } },
{ -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩,
by_cases m0 : m = 0; simp [m0] at h₂,
{ exact ⟨0,
⟨by simpa [m0] using hf _ _ h₁,
λ m, (nat.not_lt_zero _).elim⟩,
by injection h₂ with h₂; simp [h₂]⟩ },
{ have := evaln_sound h₂, simp [eval] at this,
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩,
refine ⟨ y+1, ⟨by simpa [add_comm, add_left_comm] using hy₁, λ i im, _⟩,
by simp [add_comm, add_left_comm] ⟩,
cases i with i,
{ exact ⟨m, by simpa using hf _ _ h₁, m0⟩ },
{ rcases hy₂ (nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩,
exact ⟨z, by simpa [nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩ } } }
end
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨λ h, begin
suffices : ∃ k, x ∈ evaln (k+1) c n,
{ exact let ⟨k, h⟩ := this in ⟨k+1, h⟩ },
induction c generalizing n x;
simp [eval, evaln, pure, pfun.pure, (<*>), (>>)] at h ⊢,
iterate 4 { exact ⟨⟨_, le_rfl⟩, h.symm⟩ },
case nat.partrec.code.pair : cf cg hf hg
{ rcases h with ⟨x, hx, y, hy, rfl⟩,
rcases hf hx with ⟨k₁, hk₁⟩, rcases hg hy with ⟨k₂, hk₂⟩,
refine ⟨max k₁ k₂, _⟩,
refine ⟨le_max_of_le_left $ nat.le_of_lt_succ $ evaln_bound hk₁,
_, evaln_mono (nat.succ_le_succ $ le_max_left _ _) hk₁,
_, evaln_mono (nat.succ_le_succ $ le_max_right _ _) hk₂, rfl⟩ },
case nat.partrec.code.comp : cf cg hf hg
{ rcases h with ⟨y, hy, hx⟩,
rcases hg hy with ⟨k₁, hk₁⟩, rcases hf hx with ⟨k₂, hk₂⟩,
refine ⟨max k₁ k₂, _⟩,
exact ⟨le_max_of_le_left $ nat.le_of_lt_succ $ evaln_bound hk₁, _,
evaln_mono (nat.succ_le_succ $ le_max_left _ _) hk₁,
evaln_mono (nat.succ_le_succ $ le_max_right _ _) hk₂⟩ },
case nat.partrec.code.prec : cf cg hf hg
{ revert h,
generalize : n.unpair.1 = n₁, generalize : n.unpair.2 = n₂,
induction n₂ with m IH generalizing x n; simp,
{ intro, rcases hf h with ⟨k, hk⟩,
exact ⟨_, le_max_left _ _,
evaln_mono (nat.succ_le_succ $ le_max_right _ _) hk⟩ },
{ intros y hy hx,
rcases IH hy with ⟨k₁, nk₁, hk₁⟩, rcases hg hx with ⟨k₂, hk₂⟩,
refine ⟨(max k₁ k₂).succ, nat.le_succ_of_le $ le_max_of_le_left $
le_trans (le_max_left _ (mkpair n₁ m)) nk₁, y,
evaln_mono (nat.succ_le_succ $ le_max_left _ _) _,
evaln_mono (nat.succ_le_succ $ nat.le_succ_of_le $ le_max_right _ _) hk₂⟩,
simp [evaln, (>>)],
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩ } },
case nat.partrec.code.rfind' : cf hf
{ rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩,
suffices : ∃ k, y + n.unpair.2 ∈ evaln (k+1) (rfind' cf)
(mkpair n.unpair.1 n.unpair.2), {simpa [evaln, (>>)]},
revert hy₁ hy₂, generalize : n.unpair.2 = m, intros,
induction y with y IH generalizing m; simp [evaln, (>>)],
{ simp at hy₁, rcases hf hy₁ with ⟨k, hk⟩,
exact ⟨_, nat.le_of_lt_succ $ evaln_bound hk, _, hk, by simp; refl⟩ },
{ rcases hy₂ (nat.succ_pos _) with ⟨a, ha, a0⟩,
rcases hf ha with ⟨k₁, hk₁⟩,
rcases IH m.succ
(by simpa [nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
(λ i hi, by simpa [nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (nat.succ_lt_succ hi))
with ⟨k₂, hk₂⟩,
use (max k₁ k₂).succ,
rw [zero_add] at hk₁,
use (nat.le_succ_of_le $ le_max_of_le_left $ nat.le_of_lt_succ $ evaln_bound hk₁),
use a,
use evaln_mono (nat.succ_le_succ $ nat.le_succ_of_le $ le_max_left _ _) hk₁,
simpa [nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (nat.succ_le_succ $ le_max_right _ _) hk₂ } }
end, λ ⟨k, h⟩, evaln_sound h⟩
section
open primrec
private def lup (L : list (list (option ℕ))) (p : ℕ × code) (n : ℕ) :=
do l ← L.nth (encode p), o ← l.nth n, o
private lemma hlup : primrec (λ p:_×(_×_)×_, lup p.1 p.2.1 p.2.2) :=
option_bind
(list_nth.comp fst (primrec.encode.comp $ fst.comp snd))
(option_bind (list_nth.comp snd $ snd.comp $ snd.comp fst) snd)
private def G (L : list (list (option ℕ))) : option (list (option ℕ)) :=
option.some $
let a := of_nat (ℕ × code) L.length,
k := a.1, c := a.2 in
(list.range k).map (λ n,
k.cases none $ λ k',
nat.partrec.code.rec_on c
(some 0) -- zero
(some (nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(λ cf cg _ _, do
x ← lup L (k, cf) n,
y ← lup L (k, cg) n,
some (mkpair x y))
(λ cf cg _ _, do
x ← lup L (k, cg) n,
lup L (k, cf) x)
(λ cf cg _ _,
let z := n.unpair.1 in
n.unpair.2.cases
(lup L (k, cf) z)
(λ y, do
i ← lup L (k', c) (mkpair z y),
lup L (k, cg) (mkpair z (mkpair y i))))
(λ cf _,
let z := n.unpair.1, m := n.unpair.2 in do
x ← lup L (k, cf) (mkpair z m),
x.cases
(some m)
(λ _, lup L (k', c) (mkpair z (m+1)))))
private lemma hG : primrec G :=
begin
have a := (primrec.of_nat (ℕ × code)).comp list_length,
have k := fst.comp a,
refine option_some.comp
(list_map (list_range.comp k) (_ : primrec _)),
replace k := k.comp fst, have n := snd,
refine nat_cases k (const none) (_ : primrec _),
have k := k.comp fst, have n := n.comp fst, have k' := snd,
have c := snd.comp (a.comp $ fst.comp fst),
apply rec_prim c
(const (some 0))
(option_some.comp (primrec.succ.comp n))
(option_some.comp (fst.comp $ primrec.unpair.comp n))
(option_some.comp (snd.comp $ primrec.unpair.comp n)),
{ have L := (fst.comp fst).comp fst,
have k := k.comp fst, have n := n.comp fst,
have cf := fst.comp snd,
have cg := (fst.comp snd).comp snd,
exact option_bind
(hlup.comp $ L.pair $ (k.pair cf).pair n)
(option_map ((hlup.comp $
L.pair $ (k.pair cg).pair n).comp fst)
(primrec₂.mkpair.comp (snd.comp fst) snd)) },
{ have L := (fst.comp fst).comp fst,
have k := k.comp fst, have n := n.comp fst,
have cf := fst.comp snd,
have cg := (fst.comp snd).comp snd,
exact option_bind
(hlup.comp $ L.pair $ (k.pair cg).pair n)
(hlup.comp ((L.comp fst).pair $
((k.pair cf).comp fst).pair snd)) },
{ have L := (fst.comp fst).comp fst,
have k := k.comp fst, have n := n.comp fst,
have cf := fst.comp snd,
have cg := (fst.comp snd).comp snd,
have z := fst.comp (primrec.unpair.comp n),
refine nat_cases
(snd.comp (primrec.unpair.comp n))
(hlup.comp $ L.pair $ (k.pair cf).pair z)
(_ : primrec _),
have L := L.comp fst, have z := z.comp fst, have y := snd,
refine option_bind
(hlup.comp $ L.pair $
(((k'.pair c).comp fst).comp fst).pair
(primrec₂.mkpair.comp z y))
(_ : primrec _),
have z := z.comp fst, have y := y.comp fst, have i := snd,
exact hlup.comp ((L.comp fst).pair $
((k.pair cg).comp $ fst.comp fst).pair $
primrec₂.mkpair.comp z $ primrec₂.mkpair.comp y i) },
{ have L := (fst.comp fst).comp fst,
have k := k.comp fst, have n := n.comp fst,
have cf := fst.comp snd,
have z := fst.comp (primrec.unpair.comp n),
have m := snd.comp (primrec.unpair.comp n),
refine option_bind
(hlup.comp $ L.pair $ (k.pair cf).pair (primrec₂.mkpair.comp z m))
(_ : primrec _),
have m := m.comp fst,
exact nat_cases snd (option_some.comp m)
((hlup.comp ((L.comp fst).pair $
((k'.pair c).comp $ fst.comp fst).pair
(primrec₂.mkpair.comp (z.comp fst)
(primrec.succ.comp m)))).comp fst) }
end
private lemma evaln_map (k c n) :
(((list.range k).nth n).map (evaln k c)).bind (λ b, b) = evaln k c n :=
begin
by_cases kn : n < k,
{ simp [list.nth_range kn] },
{ rw list.nth_len_le,
{ cases e : evaln k c n, {refl},
exact kn.elim (evaln_bound e) },
simpa using kn }
end
/-- The `nat.partrec.code.evaln` function is primitive recursive. -/
theorem evaln_prim : primrec (λ (a : (ℕ × code) × ℕ), evaln a.1.1 a.1.2 a.2) :=
have primrec₂ (λ (_:unit) (n : ℕ),
let a := of_nat (ℕ × code) n in
(list.range a.1).map (evaln a.1 a.2)), from
primrec.nat_strong_rec _ (hG.comp snd).to₂ $
λ _ p, begin
simp [G],
rw (_ : (of_nat (ℕ × code) _).snd =
of_nat code p.unpair.2), swap, {simp},
apply list.map_congr (λ n, _),
rw (by simp : list.range p = list.range
(mkpair p.unpair.1 (encode (of_nat code p.unpair.2)))),
generalize : p.unpair.1 = k,
generalize : of_nat code p.unpair.2 = c,
intro nk,
cases k with k', {simp [evaln]},
let k := k'+1, change k'.succ with k,
simp [nat.lt_succ_iff] at nk,
have hg : ∀ {k' c' n},
mkpair k' (encode c') < mkpair k (encode c) →
lup ((list.range (mkpair k (encode c))).map (λ n,
(list.range n.unpair.1).map
(evaln n.unpair.1 (of_nat code n.unpair.2))))
(k', c') n = evaln k' c' n,
{ intros k₁ c₁ n₁ hl,
simp [lup, list.nth_range hl, evaln_map, (>>=)] },
cases c with cf cg cf cg cf cg cf;
simp [evaln, nk, (>>), (>>=), (<$>), (<*>), pure],
{ cases encode_lt_pair cf cg with lf lg,
rw [hg (nat.mkpair_lt_mkpair_right _ lf),
hg (nat.mkpair_lt_mkpair_right _ lg)],
cases evaln k cf n, {refl},
cases evaln k cg n; refl },
{ cases encode_lt_comp cf cg with lf lg,
rw hg (nat.mkpair_lt_mkpair_right _ lg),
cases evaln k cg n, {refl},
simp [hg (nat.mkpair_lt_mkpair_right _ lf)] },
{ cases encode_lt_prec cf cg with lf lg,
rw hg (nat.mkpair_lt_mkpair_right _ lf),
cases n.unpair.2, {refl},
simp,
rw hg (nat.mkpair_lt_mkpair_left _ k'.lt_succ_self),
cases evaln k' _ _, {refl},
simp [hg (nat.mkpair_lt_mkpair_right _ lg)] },
{ have lf := encode_lt_rfind' cf,
rw hg (nat.mkpair_lt_mkpair_right _ lf),
cases evaln k cf n with x, {refl},
simp,
cases x; simp [nat.succ_ne_zero],
rw hg (nat.mkpair_lt_mkpair_left _ k'.lt_succ_self) }
end,
(option_bind (list_nth.comp
(this.comp (const ()) (encode_iff.2 fst)) snd)
snd.to₂).of_eq $ λ ⟨⟨k, c⟩, n⟩, by simp [evaln_map]
end
section
open partrec computable
theorem eval_eq_rfind_opt (c n) :
eval c n = nat.rfind_opt (λ k, evaln k c n) :=
part.ext $ λ x, begin
refine evaln_complete.trans (nat.rfind_opt_mono _).symm,
intros a m n hl, apply evaln_mono hl,
end
theorem eval_part : partrec₂ eval :=
(rfind_opt (evaln_prim.to_comp.comp
((snd.pair (fst.comp fst)).pair (snd.comp fst))).to₂).of_eq $
λ a, by simp [eval_eq_rfind_opt]
/--
Roger's fixed-point theorem: Any total, computable `f` has a fixed point: That is, under the
interpretation given by `nat.partrec.code.eval`, there is a code `c` such that `c` and `f c` have
the same evaluation.
-/
theorem fixed_point
{f : code → code} (hf : computable f) : ∃ c : code, eval (f c) = eval c :=
let g (x y : ℕ) : part ℕ :=
eval (of_nat code x) x >>= λ b, eval (of_nat code b) y in
have partrec₂ g :=
(eval_part.comp ((computable.of_nat _).comp fst) fst).bind
(eval_part.comp ((computable.of_nat _).comp snd) (snd.comp fst)).to₂,
let ⟨cg, eg⟩ := exists_code.1 this in
have eg' : ∀ a n, eval cg (mkpair a n) = part.map encode (g a n) :=
by simp [eg],
let F (x : ℕ) : code := f (curry cg x) in
have computable F :=
hf.comp (curry_prim.comp (primrec.const cg) primrec.id).to_comp,
let ⟨cF, eF⟩ := exists_code.1 this in
have eF' : eval cF (encode cF) = part.some (encode (F (encode cF))),
by simp [eF],
⟨curry cg (encode cF), funext (λ n,
show eval (f (curry cg (encode cF))) n = eval (curry cg (encode cF)) n,
by simp [eg', eF', part.map_id', g])⟩
theorem fixed_point₂
{f : code → ℕ →. ℕ} (hf : partrec₂ f) : ∃ c : code, eval c = f c :=
let ⟨cf, ef⟩ := exists_code.1 hf in
(fixed_point (curry_prim.comp
(primrec.const cf) primrec.encode).to_comp).imp $
λ c e, funext $ λ n, by simp [e.symm, ef, part.map_id']
end
end nat.partrec.code
|
a0feeb419144e712b861b11ddac6c4d943ef9bce | e8d53a7b78545d183a23dd7bd921bc7ff312989f | /helpers.hlean | 49291614c1214f9f30b138837f43a25eab7591b6 | [] | no_license | Sumit0730/Impredicative | 857007626592440a27cf4440aa9a226d0ede7f3e | a75cb9989a684133d31d4889a746ee4fa7b66cea | refs/heads/master | 1,631,994,804,745 | 1,531,980,761,000 | 1,531,980,761,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,196 | hlean | import imp_prop_trunc
open funext eq trunc is_trunc prod sum pi function is_equiv sigma sigma.ops
notation `↑` := eq_of_homotopy -- type \u
variables {A : Type} {B : Type} {C : A → B → Type}
{a a' : A} (p : a = a')
{a0 a1 a2 : A} {p2 : a0 = a1} {p0 : a1 = a2} {p1 : a0 = a2}
(b b' : B)
(f : Π b, C a b)(f' : Π b, C a' b) {X Y Z: Type} {x x' : X}
(q : x = x') (h : X → Y) (k : Y → Z) (y : Y)
{D : A → Type} {u v : Π a, D a}
definition pa (p : u = v) (a : A) : u a = v a := p ▸ rfl
infix ` ▻ `:76 := pa -- type \t4
infix ` ◅ `:76 := ap -- type \t8
definition ap_const : rfl = (λ x, y) ◅ q := eq.rec rfl q
definition path_lift : b =[p] p▸b := begin induction p, constructor end
definition path_lift' : p⁻¹ ▸ b' =[p] b' := begin induction p, constructor end
definition trans_fun : (p ▸ f) (p ▸ b) = path_lift p b ▸o f b
:= begin induction p, esimp end
definition trans_fun' : (p ▸ f) b' = path_lift' p b' ▸o f (p⁻¹ ▸ b')
:= begin induction p, esimp end
definition po_of_eq {d d' : D a} (r : d = d') : d =[refl a] d'
:= begin induction r, constructor end
definition pathover_of_homotopy : (Π b, f b =[p] f' b) → f=[p] f'
:= begin
intro H,induction p, apply po_of_eq, apply eq_of_homotopy, intro b,
apply @eq_of_pathover_idp _ _ a, apply H,
end
definition nfext0 {A : Type} {B : A → Type} {f g : Π a, B a} (p q : f ~ g)
: (Π a, p a = q a) → p = q
:= begin intro H, apply eq_of_homotopy, assumption end
definition nfext {A : Type} {B : A → Type} {f g : Π a, B a} (p q : f = g)
: (Π a, p ▻ a = q ▻ a) → p = q
:= begin intro H,
note z := nfext0 (apd10 p) (apd10 q),
assert K : apd10 p = apd10 q → p = q,intro,
refine _ ⬝ is_equiv.left_inv apd10 q,
symmetry,
refine _ ⬝ is_equiv.left_inv apd10 p,
symmetry,
exact ap _ a, apply K, clear K, apply z, intro a, apply H
end
definition aux2 {A : Type} {B : A → Type} {f g : Π a, B a} (H : f ~ g) (a : A)
: ↑H ▻ a = H a := right_inv apd10 H ▻ a
definition apap {A B : Type} (f : A → B) {x y : A} {p q : x = y} : p = q → f ◅ p = f ◅ q
:= begin intro, apply ap (ap f), assumption end
infix ` ◅◅ `:76 := apap -- type \t8
-- flipping equalities between compositions
definition frri : p2 = p1 ⬝ p0⁻¹ → p2 ⬝ p0 = p1
:= begin induction p0, exact id end
definition frli : p0 = p2⁻¹ ⬝ p1 → p2 ⬝ p0 = p1
:= begin induction p2, intro, refine !idp_con⬝_, exact a⬝!idp_con end
definition flri : p1 ⬝ p0⁻¹ = p2 → p1 = p2 ⬝ p0
:= begin induction p0, exact id end
definition flli : p2⁻¹ ⬝ p1 = p0 → p1 = p2 ⬝ p0
:= begin induction p2, intro, refine _⬝!idp_con⁻¹, exact !idp_con⁻¹⬝a end
definition flr : p2 ⬝ p0 = p1 → p2 = p1 ⬝ p0⁻¹
:= begin induction p0, exact id end
definition fll : p2 ⬝ p0 = p1 → p0 = p2⁻¹ ⬝ p1
:= begin induction p2, intro, refine _⬝!idp_con⁻¹, exact !idp_con⁻¹⬝a end
definition frr : p1 = p2 ⬝ p0 → p1 ⬝ p0⁻¹ = p2
:= begin induction p0, exact id end
definition frl : p1 = p2 ⬝ p0 → p2⁻¹ ⬝ p1 = p0
:= begin induction p2, intro, refine !idp_con⬝_, exact a⬝!idp_con end
|
617ccc2c90e671f4fb1e37ee05dae9d4d98c1280 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/playground/unsafe.lean | 9e1373eacf39867e640f5ac99351c5981e3c5523 | [
"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 | 161 | lean | unsafe def test (x : Nat) : Option Nat :=
unsafeIO (IO.println x *> pure (x+1))
unsafe def main (xs : List String) : IO Unit :=
IO.println $ test xs.head.toNat
|
021dfbde6ae7f676de7275ccaea310a7f6bb9428 | bb31430994044506fa42fd667e2d556327e18dfe | /src/data/num/bitwise.lean | d88123716468b9cb3e8c55087fd401a94fdae126 | [
"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 | 11,028 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import data.num.basic
import data.bitvec.core
/-!
# Bitwise operations using binary representation of integers
## Definitions
* bitwise operations for `pos_num` and `num`,
* `snum`, a type that represents integers as a bit string with a sign bit at the end,
* arithmetic operations for `snum`.
-/
namespace pos_num
/-- Bitwise "or" for `pos_num`. -/
def lor : pos_num → pos_num → pos_num
| 1 (bit0 q) := bit1 q
| 1 q := q
| (bit0 p) 1 := bit1 p
| p 1 := p
| (bit0 p) (bit0 q) := bit0 (lor p q)
| (bit0 p) (bit1 q) := bit1 (lor p q)
| (bit1 p) (bit0 q) := bit1 (lor p q)
| (bit1 p) (bit1 q) := bit1 (lor p q)
/-- Bitwise "and" for `pos_num`. -/
def land : pos_num → pos_num → num
| 1 (bit0 q) := 0
| 1 _ := 1
| (bit0 p) 1 := 0
| _ 1 := 1
| (bit0 p) (bit0 q) := num.bit0 (land p q)
| (bit0 p) (bit1 q) := num.bit0 (land p q)
| (bit1 p) (bit0 q) := num.bit0 (land p q)
| (bit1 p) (bit1 q) := num.bit1 (land p q)
/-- Bitwise `λ a b, a && !b` for `pos_num`. For example, `ldiff 5 9 = 4`:
101
1001
----
100
-/
def ldiff : pos_num → pos_num → num
| 1 (bit0 q) := 1
| 1 _ := 0
| (bit0 p) 1 := num.pos (bit0 p)
| (bit1 p) 1 := num.pos (bit0 p)
| (bit0 p) (bit0 q) := num.bit0 (ldiff p q)
| (bit0 p) (bit1 q) := num.bit0 (ldiff p q)
| (bit1 p) (bit0 q) := num.bit1 (ldiff p q)
| (bit1 p) (bit1 q) := num.bit0 (ldiff p q)
/-- Bitwise "xor" for `pos_num`. -/
def lxor : pos_num → pos_num → num
| 1 1 := 0
| 1 (bit0 q) := num.pos (bit1 q)
| 1 (bit1 q) := num.pos (bit0 q)
| (bit0 p) 1 := num.pos (bit1 p)
| (bit1 p) 1 := num.pos (bit0 p)
| (bit0 p) (bit0 q) := num.bit0 (lxor p q)
| (bit0 p) (bit1 q) := num.bit1 (lxor p q)
| (bit1 p) (bit0 q) := num.bit1 (lxor p q)
| (bit1 p) (bit1 q) := num.bit0 (lxor p q)
/-- `a.test_bit n` is `tt` iff the `n`-th bit (starting from the LSB) in the binary representation
of `a` is active. If the size of `a` is less than `n`, this evaluates to `ff`. -/
def test_bit : pos_num → nat → bool
| 1 0 := tt
| 1 (n+1) := ff
| (bit0 p) 0 := ff
| (bit0 p) (n+1) := test_bit p n
| (bit1 p) 0 := tt
| (bit1 p) (n+1) := test_bit p n
/-- `n.one_bits 0` is the list of indices of active bits in the binary representation of `n`. -/
def one_bits : pos_num → nat → list nat
| 1 d := [d]
| (bit0 p) d := one_bits p (d+1)
| (bit1 p) d := d :: one_bits p (d+1)
/-- Left-shift the binary representation of a `pos_num`. -/
def shiftl (p : pos_num) : nat → pos_num
| 0 := p
| (n+1) := bit0 (shiftl n)
/-- Right-shift the binary representation of a `pos_num`. -/
def shiftr : pos_num → nat → num
| p 0 := num.pos p
| 1 (n+1) := 0
| (bit0 p) (n+1) := shiftr p n
| (bit1 p) (n+1) := shiftr p n
end pos_num
namespace num
/-- Bitwise "or" for `num`. -/
def lor : num → num → num
| 0 q := q
| p 0 := p
| (pos p) (pos q) := pos (p.lor q)
/-- Bitwise "and" for `num`. -/
def land : num → num → num
| 0 q := 0
| p 0 := 0
| (pos p) (pos q) := p.land q
/-- Bitwise `λ a b, a && !b` for `num`. For example, `ldiff 5 9 = 4`:
101
1001
----
100
-/
def ldiff : num → num → num
| 0 q := 0
| p 0 := p
| (pos p) (pos q) := p.ldiff q
/-- Bitwise "xor" for `num`. -/
def lxor : num → num → num
| 0 q := q
| p 0 := p
| (pos p) (pos q) := p.lxor q
/-- Left-shift the binary representation of a `num`. -/
def shiftl : num → nat → num
| 0 n := 0
| (pos p) n := pos (p.shiftl n)
/-- Right-shift the binary representation of a `pos_num`. -/
def shiftr : num → nat → num
| 0 n := 0
| (pos p) n := p.shiftr n
/-- `a.test_bit n` is `tt` iff the `n`-th bit (starting from the LSB) in the binary representation
of `a` is active. If the size of `a` is less than `n`, this evaluates to `ff`. -/
def test_bit : num → nat → bool
| 0 n := ff
| (pos p) n := p.test_bit n
/-- `n.one_bits` is the list of indices of active bits in the binary representation of `n`. -/
def one_bits : num → list nat
| 0 := []
| (pos p) := p.one_bits 0
end num
/-- This is a nonzero (and "non minus one") version of `snum`.
See the documentation of `snum` for more details. -/
@[derive has_reflect, derive decidable_eq]
inductive nzsnum : Type
| msb : bool → nzsnum
| bit : bool → nzsnum → nzsnum
/-- Alternative representation of integers using a sign bit at the end.
The convention on sign here is to have the argument to `msb` denote
the sign of the MSB itself, with all higher bits set to the negation
of this sign. The result is interpreted in two's complement.
13 = ..0001101(base 2) = nz (bit1 (bit0 (bit1 (msb tt))))
-13 = ..1110011(base 2) = nz (bit1 (bit1 (bit0 (msb ff))))
As with `num`, a special case must be added for zero, which has no msb,
but by two's complement symmetry there is a second special case for -1.
Here the `bool` field indicates the sign of the number.
0 = ..0000000(base 2) = zero ff
-1 = ..1111111(base 2) = zero tt -/
@[derive has_reflect, derive decidable_eq]
inductive snum : Type
| zero : bool → snum
| nz : nzsnum → snum
instance : has_coe nzsnum snum := ⟨snum.nz⟩
instance : has_zero snum := ⟨snum.zero ff⟩
instance : has_one nzsnum := ⟨nzsnum.msb tt⟩
instance : has_one snum := ⟨snum.nz 1⟩
instance : inhabited nzsnum := ⟨1⟩
instance : inhabited snum := ⟨0⟩
/-!
The `snum` representation uses a bit string, essentially a list of 0 (`ff`) and 1 (`tt`) bits,
and the negation of the MSB is sign-extended to all higher bits.
-/
namespace nzsnum
notation (name := nznum.bit) a :: b := bit a b
/-- Sign of a `nzsnum`. -/
def sign : nzsnum → bool
| (msb b) := bnot b
| (b :: p) := sign p
/-- Bitwise `not` for `nzsnum`. -/
@[pattern] def not : nzsnum → nzsnum
| (msb b) := msb (bnot b)
| (b :: p) := bnot b :: not p
prefix `~`:100 := not
/-- Add an inactive bit at the end of a `nzsnum`. This mimics `pos_num.bit0`. -/
def bit0 : nzsnum → nzsnum := bit ff
/-- Add an active bit at the end of a `nzsnum`. This mimics `pos_num.bit1`. -/
def bit1 : nzsnum → nzsnum := bit tt
/-- The `head` of a `nzsnum` is the boolean value of its LSB. -/
def head : nzsnum → bool
| (msb b) := b
| (b :: p) := b
/-- The `tail` of a `nzsnum` is the `snum` obtained by removing the LSB.
Edge cases: `tail 1 = 0` and `tail (-2) = -1`. -/
def tail : nzsnum → snum
| (msb b) := snum.zero (bnot b)
| (b :: p) := p
end nzsnum
namespace snum
open nzsnum
/-- Sign of a `snum`. -/
def sign : snum → bool
| (zero z) := z
| (nz p) := p.sign
/-- Bitwise `not` for `snum`. -/
@[pattern] def not : snum → snum
| (zero z) := zero (bnot z)
| (nz p) := ~p
prefix (name := snum.not) ~ := not
/-- Add a bit at the end of a `snum`. This mimics `nzsnum.bit`. -/
@[pattern] def bit : bool → snum → snum
| b (zero z) := if b = z then zero b else msb b
| b (nz p) := p.bit b
notation (name := snum.bit) a :: b := bit a b
/-- Add an inactive bit at the end of a `snum`. This mimics `znum.bit0`. -/
def bit0 : snum → snum := bit ff
/-- Add an active bit at the end of a `snum`. This mimics `znum.bit1`. -/
def bit1 : snum → snum := bit tt
theorem bit_zero (b) : b :: zero b = zero b := by cases b; refl
theorem bit_one (b) : b :: zero (bnot b) = msb b := by cases b; refl
end snum
namespace nzsnum
open snum
/-- A dependent induction principle for `nzsnum`, with base cases
`0 : snum` and `(-1) : snum`. -/
def drec' {C : snum → Sort*} (z : Π b, C (snum.zero b))
(s : Π b p, C p → C (b :: p)) : Π p : nzsnum, C p
| (msb b) := by rw ←bit_one; exact s b (snum.zero (bnot b)) (z (bnot b))
| (bit b p) := s b p (drec' p)
end nzsnum
namespace snum
open nzsnum
/-- The `head` of a `snum` is the boolean value of its LSB. -/
def head : snum → bool
| (zero z) := z
| (nz p) := p.head
/-- The `tail` of a `snum` is obtained by removing the LSB.
Edge cases: `tail 1 = 0`, `tail (-2) = -1`, `tail 0 = 0` and `tail (-1) = -1`. -/
def tail : snum → snum
| (zero z) := zero z
| (nz p) := p.tail
/-- A dependent induction principle for `snum` which avoids relying on `nzsnum`. -/
def drec' {C : snum → Sort*} (z : Π b, C (snum.zero b))
(s : Π b p, C p → C (b :: p)) : Π p, C p
| (zero b) := z b
| (nz p) := p.drec' z s
/-- An induction principle for `snum` which avoids relying on `nzsnum`. -/
def rec' {α} (z : bool → α) (s : bool → snum → α → α) : snum → α :=
drec' z s
/-- `snum.test_bit n a` is `tt` iff the `n`-th bit (starting from the LSB) of `a` is active.
If the size of `a` is less than `n`, this evaluates to `ff`. -/
def test_bit : nat → snum → bool
| 0 p := head p
| (n+1) p := test_bit n (tail p)
/-- The successor of a `snum` (i.e. the operation adding one). -/
def succ : snum → snum :=
rec' (λ b, cond b 0 1) (λb p succp, cond b (ff :: succp) (tt :: p))
/-- The predecessor of a `snum` (i.e. the operation of removing one). -/
def pred : snum → snum :=
rec' (λ b, cond b (~1) ~0) (λb p predp, cond b (ff :: p) (tt :: predp))
/-- The opposite of a `snum`. -/
protected def neg (n : snum) : snum := succ ~n
instance : has_neg snum := ⟨snum.neg⟩
/-- `snum.czadd a b n` is `n + a - b` (where `a` and `b` should be read as either 0 or 1).
This is useful to implement the carry system in `cadd`. -/
def czadd : bool → bool → snum → snum
| ff ff p := p
| ff tt p := pred p
| tt ff p := succ p
| tt tt p := p
end snum
namespace snum
/-- `a.bits n` is the vector of the `n` first bits of `a` (starting from the LSB). -/
def bits : snum → Π n, vector bool n
| p 0 := vector.nil
| p (n+1) := head p ::ᵥ bits (tail p) n
def cadd : snum → snum → bool → snum :=
rec' (λ a p c, czadd c a p) $ λa p IH,
rec' (λb c, czadd c b (a :: p)) $ λb q _ c,
bitvec.xor3 a b c :: IH q (bitvec.carry a b c)
/-- Add two `snum`s. -/
protected def add (a b : snum) : snum := cadd a b ff
instance : has_add snum := ⟨snum.add⟩
/-- subtract two `snum`s. -/
protected def sub (a b : snum) : snum := a + -b
instance : has_sub snum := ⟨snum.sub⟩
/-- Multiply two `snum`s. -/
protected def mul (a : snum) : snum → snum :=
rec' (λ b, cond b (-a) 0) $ λb q IH,
cond b (bit0 IH + a) (bit0 IH)
instance : has_mul snum := ⟨snum.mul⟩
end snum
|
60b953023d20cc416b10dbac0948987aef7457a0 | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /03_Propositions_and_Proofs.org.11.lean | b186c11aebc99063f140bb3a254a422a3c5c8cb7 | [] | 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 | 153 | lean | /- page 37 -/
import standard
constants p q : Prop
theorem t1 (Hp : p) (Hq : q) : p := Hp
-- BEGIN
axiom Hp : p
theorem t2 : q → p := t1 Hp
-- END
|
b3b3bbcdb802875572205d0ea0087d1e80c6aefb | 46125763b4dbf50619e8846a1371029346f4c3db | /src/tactic/norm_num.lean | c8f754bb6bd51b6e69aa9546b162f2c7600df7b4 | [
"Apache-2.0"
] | permissive | thjread/mathlib | a9d97612cedc2c3101060737233df15abcdb9eb1 | 7cffe2520a5518bba19227a107078d83fa725ddc | refs/heads/master | 1,615,637,696,376 | 1,583,953,063,000 | 1,583,953,063,000 | 246,680,271 | 0 | 0 | Apache-2.0 | 1,583,960,875,000 | 1,583,960,875,000 | null | UTF-8 | Lean | false | false | 18,581 | lean | /-
Copyright (c) 2017 Simon Hudon All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon, Mario Carneiro
Evaluating arithmetic expressions including *, +, -, ^, ≤
-/
import algebra.group_power data.rat.order data.rat.cast data.rat.meta_defs data.nat.prime
import tactic.interactive tactic.converter.interactive
universes u v w
namespace tactic
meta def refl_conv (e : expr) : tactic (expr × expr) :=
do p ← mk_eq_refl e, return (e, p)
meta def trans_conv (t₁ t₂ : expr → tactic (expr × expr)) (e : expr) :
tactic (expr × expr) :=
(do (e₁, p₁) ← t₁ e,
(do (e₂, p₂) ← t₂ e₁,
p ← mk_eq_trans p₁ p₂, return (e₂, p)) <|>
return (e₁, p₁)) <|> t₂ e
end tactic
open tactic
namespace norm_num
variable {α : Type u}
lemma subst_into_neg {α} [has_neg α] (a ta t : α) (pra : a = ta) (prt : -ta = t) : -a = t :=
by simp [pra, prt]
theorem bit0_zero [add_group α] : bit0 (0 : α) = 0 := add_zero _
theorem bit1_zero [add_group α] [has_one α] : bit1 (0 : α) = 1 :=
by rw [bit1, bit0_zero, zero_add]
lemma pow_bit0_helper [monoid α] (a t : α) (b : ℕ) (h : a ^ b = t) :
a ^ bit0 b = t * t :=
by simp [pow_bit0, h]
lemma pow_bit1_helper [monoid α] (a t : α) (b : ℕ) (h : a ^ b = t) :
a ^ bit1 b = t * t * a :=
by simp [pow_bit1, h]
lemma lt_add_of_pos_helper [ordered_cancel_comm_monoid α]
(a b c : α) (h : a + b = c) (h₂ : 0 < b) : a < c :=
h ▸ (lt_add_iff_pos_right _).2 h₂
lemma nat_div_helper (a b q r : ℕ) (h : r + q * b = a) (h₂ : r < b) : a / b = q :=
by rw [← h, nat.add_mul_div_right _ _ (lt_of_le_of_lt (nat.zero_le _) h₂),
nat.div_eq_of_lt h₂, zero_add]
lemma int_div_helper (a b q r : ℤ) (h : r + q * b = a) (h₁ : 0 ≤ r) (h₂ : r < b) : a / b = q :=
by rw [← h, int.add_mul_div_right _ _ (ne_of_gt (lt_of_le_of_lt h₁ h₂)),
int.div_eq_zero_of_lt h₁ h₂, zero_add]
lemma nat_mod_helper (a b q r : ℕ) (h : r + q * b = a) (h₂ : r < b) : a % b = r :=
by rw [← h, nat.add_mul_mod_self_right, nat.mod_eq_of_lt h₂]
lemma int_mod_helper (a b q r : ℤ) (h : r + q * b = a) (h₁ : 0 ≤ r) (h₂ : r < b) : a % b = r :=
by rw [← h, int.add_mul_mod_self, int.mod_eq_of_lt h₁ h₂]
meta def eval_pow (simp : expr → tactic (expr × expr)) : expr → tactic (expr × expr)
| `(@has_pow.pow %%α _ %%m %%e₁ %%e₂) :=
match m with
| `(nat.has_pow) :=
mk_app ``nat.pow [e₁, e₂] >>= eval_pow
| `(@monoid.has_pow %%α %%m) :=
mk_app ``monoid.pow [e₁, e₂] >>= eval_pow
| _ := failed
end
| `(monoid.pow %%e₁ 0) := do
p ← mk_app ``pow_zero [e₁],
a ← infer_type e₁,
o ← mk_app ``has_one.one [a],
return (o, p)
| `(monoid.pow %%e₁ 1) := do
p ← mk_app ``pow_one [e₁],
return (e₁, p)
| `(monoid.pow %%e₁ (bit0 %%e₂)) := do
e ← mk_app ``monoid.pow [e₁, e₂],
(e', p) ← simp e,
p' ← mk_app ``norm_num.pow_bit0_helper [e₁, e', e₂, p],
e'' ← mk_app ``has_mul.mul [e', e'],
(e'', p₂) ← simp e'',
p'' ← mk_eq_trans p' p₂,
return (e'', p'')
| `(monoid.pow %%e₁ (bit1 %%e₂)) := do
e ← mk_app ``monoid.pow [e₁, e₂],
(e', p) ← simp e,
p' ← mk_app ``norm_num.pow_bit1_helper [e₁, e', e₂, p],
e'' ← mk_app ``has_mul.mul [e', e'],
e'' ← mk_app ``has_mul.mul [e'', e₁],
(e'', p₂) ← simp e'',
p'' ← mk_eq_trans p' p₂,
return (e'', p'')
| `(nat.pow %%e₁ %%e₂) := do
p₁ ← mk_app ``nat.pow_eq_pow [e₁, e₂] >>= mk_eq_symm,
e ← mk_app ``monoid.pow [e₁, e₂],
(e', p₂) ← simp e,
p ← mk_eq_trans p₁ p₂,
return (e', p)
| _ := failed
meta def prove_pos : instance_cache → expr → tactic (instance_cache × expr)
| c `(has_one.one _) := do (c, p) ← c.mk_app ``zero_lt_one [], return (c, p)
| c `(bit0 %%e) := do (c, p) ← prove_pos c e, (c, p) ← c.mk_app ``bit0_pos [e, p], return (c, p)
| c `(bit1 %%e) := do (c, p) ← prove_pos c e, (c, p) ← c.mk_app ``bit1_pos' [e, p], return (c, p)
| c `(%%e₁ / %%e₂) := do
(c, p₁) ← prove_pos c e₁, (c, p₂) ← prove_pos c e₂,
(c, p) ← c.mk_app ``div_pos_of_pos_of_pos [e₁, e₂, p₁, p₂],
return (c, p)
| c e := failed
meta def prove_lt (simp : expr → tactic (expr × expr)) : instance_cache → expr → expr → tactic (instance_cache × expr)
| c `(- %%e₁) `(- %%e₂) := do
(c, p) ← prove_lt c e₁ e₂,
(c, p) ← c.mk_app ``neg_lt_neg [e₁, e₂, p],
return (c, p)
| c `(- %%e₁) `(has_zero.zero _) := do
(c, p) ← prove_pos c e₁,
(c, p) ← c.mk_app ``neg_neg_of_pos [e₁, p],
return (c, p)
| c `(- %%e₁) e₂ := do
(c, p₁) ← prove_pos c e₁,
(c, me₁) ← c.mk_app ``has_neg.neg [e₁],
(c, p₁) ← c.mk_app ``neg_neg_of_pos [e₁, p₁],
(c, p₂) ← prove_pos c e₂,
(c, z) ← c.mk_app ``has_zero.zero [],
(c, p) ← c.mk_app ``lt_trans [me₁, z, e₂, p₁, p₂],
return (c, p)
| c `(has_zero.zero _) e₂ := prove_pos c e₂
| c e₁ e₂ := do
n₁ ← e₁.to_rat, n₂ ← e₂.to_rat,
d ← expr.of_rat c.α (n₂ - n₁),
(c, e₃) ← c.mk_app ``has_add.add [e₁, d],
(e₂', p) ← norm_num e₃,
guard (e₂'.is_num_eq e₂),
(c, p') ← prove_pos c d,
(c, p) ← c.mk_app ``norm_num.lt_add_of_pos_helper [e₁, d, e₂, p, p'],
return (c, p)
private meta def true_intro (p : expr) : tactic (expr × expr) :=
prod.mk <$> mk_const `true <*> mk_app ``eq_true_intro [p]
private meta def false_intro (p : expr) : tactic (expr × expr) :=
prod.mk <$> mk_const `false <*> mk_app ``eq_false_intro [p]
meta def eval_ineq (simp : expr → tactic (expr × expr)) : expr → tactic (expr × expr)
| `(%%e₁ < %%e₂) := do
n₁ ← e₁.to_rat, n₂ ← e₂.to_rat,
c ← infer_type e₁ >>= mk_instance_cache,
if n₁ < n₂ then
do (_, p) ← prove_lt simp c e₁ e₂, true_intro p
else do
(c, p) ← if n₁ = n₂ then c.mk_app ``lt_irrefl [e₁] else
(do (c, p') ← prove_lt simp c e₂ e₁,
c.mk_app ``not_lt_of_gt [e₁, e₂, p']),
false_intro p
| `(%%e₁ ≤ %%e₂) := do
n₁ ← e₁.to_rat, n₂ ← e₂.to_rat,
c ← infer_type e₁ >>= mk_instance_cache,
if n₁ ≤ n₂ then do
(c, p) ← if n₁ = n₂ then c.mk_app ``le_refl [e₁] else
(do (c, p') ← prove_lt simp c e₁ e₂,
c.mk_app ``le_of_lt [e₁, e₂, p']),
true_intro p
else do
(c, p) ← prove_lt simp c e₂ e₁,
(c, p) ← c.mk_app ``not_le_of_gt [e₁, e₂, p],
false_intro p
| `(%%e₁ = %%e₂) := do
n₁ ← e₁.to_rat, n₂ ← e₂.to_rat,
c ← infer_type e₁ >>= mk_instance_cache,
if n₁ < n₂ then do
(c, p) ← prove_lt simp c e₁ e₂,
(c, p) ← c.mk_app ``ne_of_lt [e₁, e₂, p],
false_intro p
else if n₂ < n₁ then do
(c, p) ← prove_lt simp c e₂ e₁,
(c, p) ← c.mk_app ``ne_of_gt [e₁, e₂, p],
false_intro p
else mk_eq_refl e₁ >>= true_intro
| `(%%e₁ > %%e₂) := mk_app ``has_lt.lt [e₂, e₁] >>= simp
| `(%%e₁ ≥ %%e₂) := mk_app ``has_le.le [e₂, e₁] >>= simp
| `(%%e₁ ≠ %%e₂) := do e ← mk_app ``eq [e₁, e₂], mk_app ``not [e] >>= simp
| _ := failed
meta def eval_div_ext (simp : expr → tactic (expr × expr)) : expr → tactic (expr × expr)
| `(has_inv.inv %%e) := do
c ← infer_type e >>= mk_instance_cache,
(c, p₁) ← c.mk_app ``inv_eq_one_div [e],
(c, o) ← c.mk_app ``has_one.one [],
(c, e') ← c.mk_app ``has_div.div [o, e],
(do (e'', p₂) ← simp e',
p ← mk_eq_trans p₁ p₂,
return (e'', p)) <|> return (e', p₁)
| `(%%e₁ / %%e₂) := do
α ← infer_type e₁,
c ← mk_instance_cache α,
match α with
| `(nat) := do
n₁ ← e₁.to_nat, n₂ ← e₂.to_nat,
q ← expr.of_nat α (n₁ / n₂),
r ← expr.of_nat α (n₁ % n₂),
(c, e₃) ← c.mk_app ``has_mul.mul [q, e₂],
(c, e₃) ← c.mk_app ``has_add.add [r, e₃],
(e₁', p) ← norm_num e₃,
guard (e₁' =ₐ e₁),
(c, p') ← prove_lt simp c r e₂,
p ← mk_app ``norm_num.nat_div_helper [e₁, e₂, q, r, p, p'],
return (q, p)
| `(int) := match e₂ with
| `(- %%e₂') := do
(c, p₁) ← c.mk_app ``int.div_neg [e₁, e₂'],
(c, e) ← c.mk_app ``has_div.div [e₁, e₂'],
(c, e) ← c.mk_app ``has_neg.neg [e],
(e', p₂) ← simp e,
p ← mk_eq_trans p₁ p₂,
return (e', p)
| _ := do
n₁ ← e₁.to_int,
n₂ ← e₂.to_int,
q ← expr.of_rat α $ rat.of_int (n₁ / n₂),
r ← expr.of_rat α $ rat.of_int (n₁ % n₂),
(c, e₃) ← c.mk_app ``has_mul.mul [q, e₂],
(c, e₃) ← c.mk_app ``has_add.add [r, e₃],
(e₁', p) ← norm_num e₃,
guard (e₁' =ₐ e₁),
(c, r0) ← c.mk_app ``has_zero.zero [],
(c, r0) ← c.mk_app ``has_le.le [r0, r],
(_, p₁) ← simp r0,
p₁ ← mk_app ``of_eq_true [p₁],
(c, p₂) ← prove_lt simp c r e₂,
p ← mk_app ``norm_num.int_div_helper [e₁, e₂, q, r, p, p₁, p₂],
return (q, p)
end
| _ := failed
end
| `(%%e₁ % %%e₂) := do
α ← infer_type e₁,
c ← mk_instance_cache α,
match α with
| `(nat) := do
n₁ ← e₁.to_nat, n₂ ← e₂.to_nat,
q ← expr.of_nat α (n₁ / n₂),
r ← expr.of_nat α (n₁ % n₂),
(c, e₃) ← c.mk_app ``has_mul.mul [q, e₂],
(c, e₃) ← c.mk_app ``has_add.add [r, e₃],
(e₁', p) ← norm_num e₃,
guard (e₁' =ₐ e₁),
(c, p') ← prove_lt simp c r e₂,
p ← mk_app ``norm_num.nat_mod_helper [e₁, e₂, q, r, p, p'],
return (r, p)
| `(int) := match e₂ with
| `(- %%e₂') := do
let p₁ := (expr.const ``int.mod_neg []).mk_app [e₁, e₂'],
(c, e) ← c.mk_app ``has_mod.mod [e₁, e₂'],
(e', p₂) ← simp e,
p ← mk_eq_trans p₁ p₂,
return (e', p)
| _ := do
n₁ ← e₁.to_int,
n₂ ← e₂.to_int,
q ← expr.of_rat α $ rat.of_int (n₁ / n₂),
r ← expr.of_rat α $ rat.of_int (n₁ % n₂),
(c, e₃) ← c.mk_app ``has_mul.mul [q, e₂],
(c, e₃) ← c.mk_app ``has_add.add [r, e₃],
(e₁', p) ← norm_num e₃,
guard (e₁' =ₐ e₁),
(c, r0) ← c.mk_app ``has_zero.zero [],
(c, r0) ← c.mk_app ``has_le.le [r0, r],
(_, p₁) ← simp r0,
p₁ ← mk_app ``of_eq_true [p₁],
(c, p₂) ← prove_lt simp c r e₂,
p ← mk_app ``norm_num.int_mod_helper [e₁, e₂, q, r, p, p₁, p₂],
return (r, p)
end
| _ := failed
end
| `(%%e₁ ∣ %%e₂) := do
α ← infer_type e₁,
c ← mk_instance_cache α,
n ← match α with
| `(nat) := return ``nat.dvd_iff_mod_eq_zero
| `(int) := return ``int.dvd_iff_mod_eq_zero
| _ := failed
end,
p₁ ← mk_app ``propext [@expr.const tt n [] e₁ e₂],
(e', p₂) ← simp `(%%e₂ % %%e₁ = 0),
p' ← mk_eq_trans p₁ p₂,
return (e', p')
| _ := failed
lemma not_prime_helper (a b n : ℕ)
(h : a * b = n) (h₁ : 1 < a) (h₂ : 1 < b) : ¬ nat.prime n :=
by rw ← h; exact nat.not_prime_mul h₁ h₂
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]]
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 :=
nat.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 _)))
dec_trivial
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' : ℕ) (e : k + 1 = k')
(nd : bit1 k ∣ bit1 n = false)
(h : min_fac_helper n k) : min_fac_helper n k' :=
begin
refine min_fac_helper_1 e _ h,
intro e₁, rw [eq_false, ← e₁] at nd,
exact nd (nat.min_fac_dvd _)
end
lemma min_fac_helper_4 (n k : ℕ) (hd : bit1 k ∣ bit1 n = true)
(h : min_fac_helper n k) : nat.min_fac (bit1 n) = bit1 k :=
by rw eq_true 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
meta def prove_non_prime (simp : expr → tactic (expr × expr)) (e : expr) (n d₁ : ℕ) : tactic expr :=
do let e₁ := reflect d₁,
c ← mk_instance_cache `(nat),
(c, p₁) ← prove_lt simp c `(1) e₁,
let d₂ := n / d₁, let e₂ := reflect d₂,
(e', p) ← mk_app ``has_mul.mul [e₁, e₂] >>= norm_num,
guard (e' =ₐ e),
(c, p₂) ← prove_lt simp c `(1) e₂,
return $ (expr.const ``not_prime_helper []).mk_app [e₁, e₂, e, p, p₁, p₂]
meta def prove_min_fac (simp : expr → tactic (expr × expr))
(e₁ : expr) (n1 : ℕ) : expr → expr → tactic (expr × expr)
| e₂ p := do
k ← e₂.to_nat,
let k1 := bit1 k,
e₁1 ← mk_app ``bit1 [e₁],
e₂1 ← mk_app ``bit1 [e₂],
if n1 < k1*k1 then do
c ← mk_instance_cache `(nat),
(c, e') ← c.mk_app ``has_mul.mul [e₂1, e₂1],
(e', p₁) ← norm_num e',
(c, p₂) ← prove_lt simp c e₁1 e',
p' ← mk_app ``min_fac_helper_5 [e₁, e₂, e', p₁, p₂, p],
return (e₁1, p')
else let d := k1.min_fac in
if to_bool (d < k1) then do
(e', p₁) ← norm_num `(%%e₂ + 1),
p₂ ← prove_non_prime simp e₂1 k1 d,
mk_app ``min_fac_helper_2 [e₁, e₂, e', p₁, p₂, p] >>= prove_min_fac e'
else do
(_, p₂) ← simp `((%%e₂1 : ℕ) ∣ %%e₁1),
if k1 ∣ n1 then do
p' ← mk_app ``min_fac_helper_4 [e₁, e₂, p₂, p],
return (e₂1, p')
else do
(e', p₁) ← norm_num `(%%e₂ + 1),
mk_app ``min_fac_helper_3 [e₁, e₂, e', p₁, p₂, p] >>= prove_min_fac e'
meta def eval_prime (simp : expr → tactic (expr × expr)) : 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 simp e n d₁ >>= false_intro
else do
let e₁ := reflect d₁,
c ← mk_instance_cache `(nat),
(c, p₁) ← prove_lt simp c `(1) e₁,
(e₁, p) ← simp `(nat.min_fac %%e),
true_intro $ (expr.const ``is_prime_helper []).mk_app [e, p₁, p]
end
| `(nat.min_fac 0) := refl_conv (reflect (0:ℕ))
| `(nat.min_fac 1) := refl_conv (reflect (1:ℕ))
| `(nat.min_fac (bit0 %%e)) := prod.mk `(2) <$> mk_app ``min_fac_bit0 [e]
| `(nat.min_fac (bit1 %%e)) := do
n ← e.to_nat,
c ← mk_instance_cache `(nat),
(c, p) ← prove_pos c e,
mk_app ``min_fac_helper_0 [e, p] >>= prove_min_fac simp e (bit1 n) `(1)
| _ := failed
meta def derive1 (simp : expr → tactic (expr × expr)) (e : expr) :
tactic (expr × expr) :=
norm_num e <|> eval_div_ext simp e <|>
eval_pow simp e <|> eval_ineq simp e <|> eval_prime simp e
meta def derive : expr → tactic (expr × expr) | e :=
do e ← instantiate_mvars e,
(_, e', pr) ←
ext_simplify_core () {} simp_lemmas.mk (λ _, failed) (λ _ _ _ _ _, failed)
(λ _ _ _ _ e,
do (new_e, pr) ← derive1 derive e,
guard (¬ new_e =ₐ e),
return ((), new_e, some pr, tt))
`eq e,
return (e', pr)
/-- This version of `derive` does not fail when the input is already a numeral -/
meta def derive' : expr → tactic (expr × expr) := derive1 derive
end norm_num
namespace tactic.interactive
open norm_num interactive interactive.types
/-- Basic version of `norm_num` that does not call `simp`. -/
meta def norm_num1 (loc : parse location) : tactic unit :=
do ns ← loc.get_locals,
tt ← tactic.replace_at derive ns loc.include_goal
| fail "norm_num failed to simplify",
when loc.include_goal $ try tactic.triv,
when (¬ ns.empty) $ try tactic.contradiction
/-- Normalize numerical expressions. Supports the operations
`+` `-` `*` `/` `^` and `%` over numerical types such as
`ℕ`, `ℤ`, `ℚ`, `ℝ`, `ℂ` and some general algebraic types,
and can prove goals of the form `A = B`, `A ≠ B`, `A < B` and `A ≤ B`,
where `A` and `B` are numerical expressions.
It also has a relatively simple primality prover. -/
meta def norm_num (hs : parse simp_arg_list) (l : parse location) : tactic unit :=
repeat1 $ orelse' (norm_num1 l) $
simp_core {} (norm_num1 (loc.ns [none])) ff hs [] l
add_hint_tactic "norm_num"
meta def apply_normed (x : parse texpr) : tactic unit :=
do x₁ ← to_expr x,
(x₂,_) ← derive x₁,
tactic.exact x₂
end tactic.interactive
namespace conv.interactive
open conv interactive tactic.interactive
open norm_num (derive)
meta def norm_num1 : conv unit := replace_lhs derive
meta def norm_num (hs : parse simp_arg_list) : conv unit :=
repeat1 $ orelse' norm_num1 $
simp_core {} norm_num1 ff hs [] (loc.ns [none])
end conv.interactive
|
ab4feda7cbc62c736f1e54e22198338aa21245dc | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/category_theory/monoidal/category.lean | 01a80d8c870c40ae495d539041037c96fd352d01 | [
"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 | 16,143 | 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
import category_theory.natural_isomorphism
import tactic.basic
import tactic.slice
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.
-/
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 [simp] monoidal_category.tensor_comp
restate_axiom monoidal_category.associator_naturality'
restate_axiom monoidal_category.left_unitor_naturality'
restate_axiom monoidal_category.right_unitor_naturality'
restate_axiom monoidal_category.pentagon'
restate_axiom monoidal_category.triangle'
attribute [simp] 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. -/
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]
include 𝒞
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] lemma id_tensor_comp_tensor_id (f : W ⟶ X) (g : Y ⟶ Z) :
((𝟙 Y) ⊗ f) ≫ (g ⊗ (𝟙 X)) = g ⊗ f :=
by { rw [←tensor_comp], simp }
@[simp] 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 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>
@[simp] 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_inv (X Y : C) :
((λ_ (X ⊗ Y)).inv) ≫ ((α_ (𝟙_ C) X Y).inv) =
((λ_ X).inv ⊗ (𝟙 Y)) :=
eq_of_inv_eq_inv (by simp)
@[simp] lemma right_unitor_tensor (X Y : C) :
((α_ X Y (𝟙_ C)).hom) ≫ ((𝟙 X) ⊗ (ρ_ Y).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 (𝟙_ C)).inv) =
((ρ_ (X ⊗ Y)).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
@[simp] lemma triangle_assoc_comp_left (X Y : C) :
(α_ X (𝟙_ C) Y).hom ≫ ((𝟙 X) ⊗ (λ_ Y).hom) = (ρ_ X).hom ⊗ 𝟙 Y :=
monoidal_category.triangle C 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]
include 𝒞
/-- 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. -/
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. -/
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. -/
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 })
end
end monoidal_category
end category_theory
|
c8152ae855d47368b685fe4ecda99d7c51914b02 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/linear_algebra/adic_completion.lean | 42bd77ac00df3a8d112173e0dcf2d00fb7450811 | [
"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 | 10,115 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import linear_algebra.smodeq
import ring_theory.ideal.quotient
import ring_theory.jacobson_ideal
/-!
# Completion of a module with respect to an ideal.
In this file we define the notions of Hausdorff, precomplete, and complete for an `R`-module `M`
with respect to an ideal `I`:
## Main definitions
- `is_Hausdorff I M`: this says that the intersection of `I^n M` is `0`.
- `is_precomplete I M`: this says that every Cauchy sequence converges.
- `is_adic_complete I M`: this says that `M` is Hausdorff and precomplete.
- `Hausdorffification I M`: this is the universal Hausdorff module with a map from `M`.
- `completion I M`: if `I` is finitely generated, then this is the universal complete module (TODO)
with a map from `M`. This map is injective iff `M` is Hausdorff and surjective iff `M` is
precomplete.
-/
open submodule
variables {R : Type*} [comm_ring R] (I : ideal R)
variables (M : Type*) [add_comm_group M] [module R M]
variables {N : Type*} [add_comm_group N] [module R N]
/-- A module `M` is Hausdorff with respect to an ideal `I` if `⋂ I^n M = 0`. -/
class is_Hausdorff : Prop :=
(haus' : ∀ x : M, (∀ n : ℕ, x ≡ 0 [SMOD (I ^ n • ⊤ : submodule R M)]) → x = 0)
/-- A module `M` is precomplete with respect to an ideal `I` if every Cauchy sequence converges. -/
class is_precomplete : Prop :=
(prec' : ∀ f : ℕ → M,
(∀ {m n}, m ≤ n → f m ≡ f n [SMOD (I ^ m • ⊤ : submodule R M)]) →
∃ L : M, ∀ n, f n ≡ L [SMOD (I ^ n • ⊤ : submodule R M)])
/-- A module `M` is `I`-adically complete if it is Hausdorff and precomplete. -/
class is_adic_complete extends is_Hausdorff I M, is_precomplete I M : Prop
variables {I M}
theorem is_Hausdorff.haus (h : is_Hausdorff I M) :
∀ x : M, (∀ n : ℕ, x ≡ 0 [SMOD (I ^ n • ⊤ : submodule R M)]) → x = 0 := is_Hausdorff.haus'
theorem is_Hausdorff_iff : is_Hausdorff I M ↔
∀ x : M, (∀ n : ℕ, x ≡ 0 [SMOD (I ^ n • ⊤ : submodule R M)]) → x = 0 :=
⟨is_Hausdorff.haus, λ h, ⟨h⟩⟩
theorem is_precomplete.prec (h : is_precomplete I M) {f : ℕ → M} :
(∀ {m n}, m ≤ n → f m ≡ f n [SMOD (I ^ m • ⊤ : submodule R M)]) →
∃ L : M, ∀ n, f n ≡ L [SMOD (I ^ n • ⊤ : submodule R M)] := is_precomplete.prec' _
theorem is_precomplete_iff : is_precomplete I M ↔ ∀ f : ℕ → M,
(∀ {m n}, m ≤ n → f m ≡ f n [SMOD (I ^ m • ⊤ : submodule R M)]) →
∃ L : M, ∀ n, f n ≡ L [SMOD (I ^ n • ⊤ : submodule R M)] :=
⟨λ h, h.1, λ h, ⟨h⟩⟩
variables (I M)
/-- The Hausdorffification of a module with respect to an ideal. -/
@[reducible] def Hausdorffification : Type* :=
(⨅ n : ℕ, I ^ n • ⊤ : submodule R M).quotient
/-- The completion of a module with respect to an ideal. This is not necessarily Hausdorff.
In fact, this is only complete if the ideal is finitely generated. -/
def adic_completion : submodule R (Π n : ℕ, (I ^ n • ⊤ : submodule R M).quotient) :=
{ carrier := { f | ∀ {m n} (h : m ≤ n), liftq _ (mkq _)
(by { rw ker_mkq, exact smul_mono (ideal.pow_le_pow h) (le_refl _) }) (f n) = f m },
zero_mem' := λ m n hmn, by rw [pi.zero_apply, pi.zero_apply, linear_map.map_zero],
add_mem' := λ f g hf hg m n hmn, by rw [pi.add_apply, pi.add_apply,
linear_map.map_add, hf hmn, hg hmn],
smul_mem' := λ c f hf m n hmn, by rw [pi.smul_apply, pi.smul_apply, linear_map.map_smul, hf hmn] }
namespace is_Hausdorff
instance bot : is_Hausdorff (⊥ : ideal R) M :=
⟨λ x hx, by simpa only [pow_one ⊥, bot_smul, smodeq.bot] using hx 1⟩
variables {M}
protected theorem subsingleton (h : is_Hausdorff (⊤ : ideal R) M) : subsingleton M :=
⟨λ x y, eq_of_sub_eq_zero $ h.haus (x - y) $ λ n,
by { rw [ideal.top_pow, top_smul], exact smodeq.top }⟩
variables (M)
@[priority 100] instance of_subsingleton [subsingleton M] : is_Hausdorff I M :=
⟨λ x _, subsingleton.elim _ _⟩
variables {I M}
theorem infi_pow_smul (h : is_Hausdorff I M) :
(⨅ n : ℕ, I ^ n • ⊤ : submodule R M) = ⊥ :=
eq_bot_iff.2 $ λ x hx, (mem_bot _).2 $ h.haus x $ λ n, smodeq.zero.2 $
(mem_infi $ λ n : ℕ, I ^ n • ⊤).1 hx n
end is_Hausdorff
namespace Hausdorffification
/-- The canonical linear map to the Hausdorffification. -/
def of : M →ₗ[R] Hausdorffification I M := mkq _
variables {I M}
@[elab_as_eliminator]
lemma induction_on {C : Hausdorffification I M → Prop} (x : Hausdorffification I M)
(ih : ∀ x, C (of I M x)) : C x :=
quotient.induction_on' x ih
variables (I M)
instance : is_Hausdorff I (Hausdorffification I M) :=
⟨λ x, quotient.induction_on' x $ λ x hx, (quotient.mk_eq_zero _).2 $ (mem_infi _).2 $ λ n, begin
have := comap_map_mkq (⨅ n : ℕ, I ^ n • ⊤ : submodule R M) (I ^ n • ⊤),
simp only [sup_of_le_right (infi_le (λ n, (I ^ n • ⊤ : submodule R M)) n)] at this,
rw [← this, map_smul'', mem_comap, map_top, range_mkq, ← smodeq.zero], exact hx n
end⟩
variables {M} [h : is_Hausdorff I N]
include h
/-- universal property of Hausdorffification: any linear map to a Hausdorff module extends to a
unique map from the Hausdorffification. -/
def lift (f : M →ₗ[R] N) : Hausdorffification I M →ₗ[R] N :=
liftq _ f $ map_le_iff_le_comap.1 $ h.infi_pow_smul ▸ le_infi (λ n,
le_trans (map_mono $ infi_le _ n) $ by { rw map_smul'', exact smul_mono (le_refl _) le_top })
theorem lift_of (f : M →ₗ[R] N) (x : M) : lift I f (of I M x) = f x := rfl
theorem lift_comp_of (f : M →ₗ[R] N) : (lift I f).comp (of I M) = f :=
linear_map.ext $ λ _, rfl
/-- Uniqueness of lift. -/
theorem lift_eq (f : M →ₗ[R] N) (g : Hausdorffification I M →ₗ[R] N) (hg : g.comp (of I M) = f) :
g = lift I f :=
linear_map.ext $ λ x, induction_on x $ λ x, by rw [lift_of, ← hg, linear_map.comp_apply]
end Hausdorffification
namespace is_precomplete
instance bot : is_precomplete (⊥ : ideal R) M :=
begin
refine ⟨λ f hf, ⟨f 1, λ n, _⟩⟩, cases n,
{ rw [pow_zero, ideal.one_eq_top, top_smul], exact smodeq.top },
specialize hf (nat.le_add_left 1 n),
rw [pow_one, bot_smul, smodeq.bot] at hf, rw hf
end
instance top : is_precomplete (⊤ : ideal R) M :=
⟨λ f hf, ⟨0, λ n, by { rw [ideal.top_pow, top_smul], exact smodeq.top }⟩⟩
@[priority 100] instance of_subsingleton [subsingleton M] : is_precomplete I M :=
⟨λ f hf, ⟨0, λ n, by rw subsingleton.elim (f n) 0⟩⟩
end is_precomplete
namespace adic_completion
/-- The canonical linear map to the completion. -/
def of : M →ₗ[R] adic_completion I M :=
{ to_fun := λ x, ⟨λ n, mkq _ x, λ m n hmn, rfl⟩,
map_add' := λ x y, rfl,
map_smul' := λ c x, rfl }
@[simp] lemma of_apply (x : M) (n : ℕ) : (of I M x).1 n = mkq _ x := rfl
/-- Linearly evaluating a sequence in the completion at a given input. -/
def eval (n : ℕ) : adic_completion I M →ₗ[R] (I ^ n • ⊤ : submodule R M).quotient :=
{ to_fun := λ f, f.1 n,
map_add' := λ f g, rfl,
map_smul' := λ c f, rfl }
@[simp] lemma coe_eval (n : ℕ) :
(eval I M n : adic_completion I M → (I ^ n • ⊤ : submodule R M).quotient) = λ f, f.1 n := rfl
lemma eval_apply (n : ℕ) (f : adic_completion I M) : eval I M n f = f.1 n := rfl
lemma eval_of (n : ℕ) (x : M) : eval I M n (of I M x) = mkq _ x := rfl
@[simp] lemma eval_comp_of (n : ℕ) : (eval I M n).comp (of I M) = mkq _ := rfl
@[simp] lemma range_eval (n : ℕ) : (eval I M n).range = ⊤ :=
linear_map.range_eq_top.2 $ λ x, quotient.induction_on' x $ λ x, ⟨of I M x, rfl⟩
variables {I M}
@[ext] lemma ext {x y : adic_completion I M} (h : ∀ n, eval I M n x = eval I M n y) : x = y :=
subtype.eq $ funext h
variables (I M)
instance : is_Hausdorff I (adic_completion I M) :=
⟨λ x hx, ext $ λ n, smul_induction_on (smodeq.zero.1 $ hx n)
(λ r hr x _, ((eval I M n).map_smul r x).symm ▸ quotient.induction_on' (eval I M n x)
(λ x, smodeq.zero.2 $ smul_mem_smul hr mem_top))
rfl
(λ _ _ ih1 ih2, by rw [linear_map.map_add, ih1, ih2, linear_map.map_zero, add_zero])
(λ c _ ih, by rw [linear_map.map_smul, ih, linear_map.map_zero, smul_zero])⟩
end adic_completion
namespace is_adic_complete
instance bot : is_adic_complete (⊥ : ideal R) M := {}
protected theorem subsingleton (h : is_adic_complete (⊤ : ideal R) M) : subsingleton M :=
h.1.subsingleton
@[priority 100] instance of_subsingleton [subsingleton M] : is_adic_complete I M := {}
open_locale big_operators
lemma le_jacobson_bot [is_adic_complete I R] : I ≤ (⊥ : ideal R).jacobson :=
begin
intros x hx,
rw [← ideal.neg_mem_iff, ideal.mem_jacobson_bot],
intros y,
rw add_comm,
let f : ℕ → R := geom_sum (x * y),
have hf : ∀ m n, m ≤ n → f m ≡ f n [SMOD I ^ m • (⊤ : submodule R R)],
{ intros m n h,
simp only [f, geom_sum_def, algebra.id.smul_eq_mul, ideal.mul_top, smodeq.sub_mem],
rw [← add_tsub_cancel_of_le h, finset.sum_range_add, ← sub_sub, sub_self, zero_sub,
neg_mem_iff],
apply submodule.sum_mem,
intros n hn,
rw [mul_pow, pow_add, mul_assoc],
exact ideal.mul_mem_right _ (I ^ m) (ideal.pow_mem_pow hx m) },
obtain ⟨L, hL⟩ := is_precomplete.prec to_is_precomplete hf,
{ rw is_unit_iff_exists_inv,
use L,
rw [← sub_eq_zero, neg_mul_eq_neg_mul_symm],
apply is_Hausdorff.haus (to_is_Hausdorff : is_Hausdorff I R),
intros n,
specialize hL n,
rw [smodeq.sub_mem, algebra.id.smul_eq_mul, ideal.mul_top] at ⊢ hL,
rw sub_zero,
suffices : (1 - x * y) * (f n) - 1 ∈ I ^ n,
{ convert (ideal.sub_mem _ this (ideal.mul_mem_left _ (1 + - (x * y)) hL)) using 1,
ring },
cases n,
{ simp only [ideal.one_eq_top, pow_zero] },
{ dsimp [f],
rw [← neg_sub _ (1:R), neg_mul_eq_neg_mul_symm, mul_geom_sum, neg_sub,
sub_sub, add_comm, ← sub_sub, sub_self, zero_sub, neg_mem_iff, mul_pow],
exact ideal.mul_mem_right _ (I ^ _) (ideal.pow_mem_pow hx _), } },
end
end is_adic_complete
|
f77ca56e8f4cd7189aa073b4aed14f3788fdad8d | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/tactic/default.lean | 98e52cbea316d5ed4e368248b587dea71a31e8ba | [
"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 | 852 | lean | /-
This file imports many useful tactics ("the kitchen sink").
You can use `import tactic` at the beginning of your file to get everything.
(Although you may want to strip things down when you're polishing.)
Because this file imports some complicated tactics, it has many transitive dependencies
(which of course may not use `import tactic`, and must import selectively).
As (non-exhaustive) examples, these includes things like:
* algebra.group_power
* algebra.ordered_ring
* data.rat
* data.nat.prime
* data.list.perm
* data.set.lattice
* data.equiv.encodable
* order.complete_lattice
-/
import
tactic.basic
tactic.monotonicity.interactive
tactic.finish
tactic.tauto
tactic.tidy
tactic.abel
tactic.ring
tactic.ring_exp
tactic.linarith
tactic.omega
tactic.wlog
tactic.tfae
tactic.apply_fun
tactic.apply
tactic.suggest
|
d08e3512cfdf14ffe0d8564a18b2846a16fc3f94 | 7fc0ec5c526f706107537a6e2e34ac4cdf88d232 | /src/group_theory/representation/examples.lean | 72c7d410f1a7262ae02ab4becbdf971764b564e8 | [
"Apache-2.0"
] | permissive | fpvandoorn/group-representations | 7440a81f2ac9e0d2defa44dc1643c3167f7a2d99 | bd9d72311749187d3bd4f542d5eab83e8341856c | refs/heads/master | 1,684,128,141,684 | 1,623,338,135,000 | 1,623,338,135,000 | 256,117,709 | 0 | 2 | null | null | null | null | UTF-8 | Lean | false | false | 468 | lean | import group_theory.subgroup data.equiv.basic data.fin group_theory.perm.sign
import group_theory.presented_group group_theory.order_of_element
import group_theory.free_abelian_group
import algebra.group.hom algebra.group_power
import group_theory.group_action
import group_theory.representation.basic
#print group_representation
inductive generator : Type
| x | y
variables a b : generator
def zgroup := free_group generator
def gx := free_group.of generator.x
|
1e6cb5c4df694dc699b346285747d7040081d59f | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /docs/tutorial/representation_theory/etingof.lean | 61e788b05c04f23aeae09a36b2f7916e42673778 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 7,755 | lean | /-
Copyright (c) 2022 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.simple
import category_theory.subobject.basic
import category_theory.preadditive.schur
import algebra.algebra.restrict_scalars
import algebra.algebra.tower
import algebra.category.Module.algebra
import algebra.category.Module.images
import algebra.category.Module.biproducts
import algebra.category.Module.simple
import linear_algebra.matrix.finite_dimensional
import data.mv_polynomial.basic
import algebra.free_algebra
import data.complex.module
/-!
# "Introduction to representation theory" by Etingof
This tutorial file follows along with the lecture notes "Introduction to representation theory",
by Pavel Etingof and other contributors.
These lecture notes are available freely online at <https://klein.mit.edu/~etingof/repb.pdf>.
This tutorial is (extremely) incomplete at present.
The goal is to work through the lecture notes,
showing how to use the definitions and results from mathlib
to establish the results in Etingof's notes. (We are not giving separate proofs here!)
Our intention is (sadly) to skip all the problems, and many of the examples.
Often results are proved by reference to (much) more general facts in mathlib.
-/
axiom skipped {p : Sort*} : p
universes u
open category_theory finite_dimensional
noncomputable theory
/-!
## Chapter 2 "Basic notions of representation theory"
-/
/-!
### 2.2 "Algebras"
-/
-- Definition 2.2.1: An associative algebra.
variables {k : Type*} [field k]
variables {A : Type*} [ring A] [algebra k A]
-- Etingof uses the word "unit" to refer to the identity in an algebra.
-- Currently in mathlib all algebras are unital
-- (although non-unital rings exists as `non_unital_ring`)
-- Thus we skip Definition 2.2.2 and Proposition 2.2.3
-- Example 2.2.4 (1)-(5)
example : algebra k k := by apply_instance
example {X : Type*} [fintype X] : algebra k (mv_polynomial X k) := by apply_instance
example {V : Type*} [add_comm_group V] [module k V] : algebra k (V →ₗ[k] V) := by apply_instance
example {X : Type*} : algebra k (free_algebra k X) := by apply_instance
example {G : Type*} [group G] : algebra k (monoid_algebra k G) := by apply_instance
-- Definition 2.2.5: A commutative algebra is described as:
example {A : Type*} [comm_ring A] [algebra k A] := true
-- Definition 2.2.6: algebra homomorphisms:
example {B : Type*} [ring B] [algebra k B] (f : A →ₐ[k] B) := true
/-!
## 2.3 "Representations"
-/
-- Definition 2.3.1
-- A representation (of an associative algebra) will usually be described as a module.
variables {M : Type*} [add_comm_group M] [module k M] [module A M] [is_scalar_tower k A M]
-- or we can use `Module A`, for a "bundled" `A`-module,
-- which is useful when we want access to the category theory library.
variables (N : Module.{u} A)
-- We can translate between these easily:
-- "bundle" a type with appropriate typeclasses
example : Module A := Module.of A M
-- a "bundled" module has a coercion to `Type`,
-- that comes equipped with the appropriate typeclasses:
example : module A N := by apply_instance
-- Remark 2.3.2: Right `A`-modules are handled as left `Aᵐᵒᵖ`-modules:
example : Module Aᵐᵒᵖ := Module.of Aᵐᵒᵖ A
-- Right modules are not extensively developed in mathlib at this point,
-- and you may run into difficulty using them.
-- It is helpful when working with `Module` to run
open_locale Module
-- which adds some instances.
-- Example 2.3.3
-- (1) The zero module
example : Module A := Module.of A punit
-- (2) The left regular module
example : Module A := Module.of A A
-- (3) Modules over a field are vector spaces.
-- (Because we handle vector spaces as modules in mathlib, this is empty of content.)
example (V : Type*) [add_comm_group V] [module k V] : Module k := Module.of k V
-- (4) is trickier,
-- and we would probably want to formalise as an equivalence of categories,
-- because "it's hard to get back to where we started".
example (X : Type*) : Module (free_algebra k X) ≃ Σ (V : Module k), X → (V ⟶ V) := skipped
-- Definition 2.3.4
-- A subrepresentation can be described using `submodule`,
variables (S : submodule A M)
-- or using the category theory library either as a monomorphism
variables (S' : Module.{u} A) (i : S' ⟶ N) [mono i]
-- or a subobject (defined as an isomorphism class of monomorphisms)
variables (S'' : subobject N)
-- Definition 2.3.5: We express that a representation is "irreducible" using `simple`.
example (N : Module A) : Prop := simple N
-- there's also a predicate for unbundled modules:
example : simple (Module.of A M) ↔ is_simple_module A M := simple_iff_is_simple_module
-- Definition 2.3.6: homomorphisms, intertwiners, isomorphisms
-- For unbundled representations, we use linear maps:
variables {M' : Type*} [add_comm_group M'] [module k M'] [module A M'] [is_scalar_tower k A M']
variables (f : M →ₗ[A] M')
-- while for bundled representations we use the categorical morphism arrow:
variables (N₁ N₂ : Module.{u} A) (g : N₁ ⟶ N₂)
-- For isomorphisms, use one of
variables (e : M ≃ₗ[A] M') (j : N₁ ≅ N₂)
-- Definition 2.3.7: direct sum
example : module A (M × M') := by apply_instance
example (N₁ N₂ : Module.{u} A) : Module.{u} A := N₁ ⊞ N₂
example (N₁ N₂ : Module.{u} A) : N₁ ⊞ N₂ ≅ Module.of A (N₁ × N₂) := Module.biprod_iso_prod N₁ N₂
-- Definition 2.3.8: indecomposable
example (N : Module A) : Prop := indecomposable N
example (N : Module A) [simple N] : indecomposable N := indecomposable_of_simple N
-- Proposition 2.3.9 (Schur's lemma)
example (N₁ N₂ : Module.{u} A) [simple N₁] (f : N₁ ⟶ N₂) (w : f ≠ 0) : mono f :=
mono_of_nonzero_from_simple w
example (N₁ N₂ : Module.{u} A) [simple N₂] (f : N₁ ⟶ N₂) (w : f ≠ 0) : epi f :=
epi_of_nonzero_to_simple w
example (N₁ N₂ : Module.{u} A) [simple N₁] [simple N₂] (f : N₁ ⟶ N₂) (w : f ≠ 0) : is_iso f :=
is_iso_of_hom_simple w
-- Corollary 2.3.10 (Schur's lemma over an algebraically closed field)
-- Unfortunately these can't be global instances
example [is_alg_closed k] (V : Module.{u} A) [simple V] [finite_dimensional k V] (f : V ⟶ V) :
∃ φ : k, φ • 𝟙 V = f :=
endomorphism_simple_eq_smul_id k f
-- Note that some magic is going on behind the scenes in this proof.
-- We're using a version of Schur's lemma that applies to any `k`-linear category,
-- and its hypotheses include `finite_dimensional k (V ⟶ V)`
-- rather than `finite_dimensional k V` (because `V` need not even be a vector space).
-- Typeclass inference is automatically generating this fact.
-- Remark 2.3.11 (Schur's lemma doesn't hold over a non-algebraically closed field)
example : simple (Module.of ℂ ℂ) := simple_of_finrank_eq_one (finrank_self ℂ)
example : finite_dimensional ℝ (Module.of ℂ ℂ) := by { dsimp, apply_instance, }
example :
let V := Module.of ℂ ℂ in
∃ (f : V ⟶ V), ∀ φ : ℝ, (φ : ℂ) • 𝟙 V ≠ f :=
⟨algebra.lsmul ℂ ℂ complex.I,
λ φ w, by simpa using congr_arg complex.im (linear_map.congr_fun w (1 : ℂ))⟩
-- Corollary 2.3.12
-- Every irreducible finite dimensional representation of a commutative algebra is 1-dimensional
example (A : Type*) [comm_ring A] [algebra k A] (V : Module A) [finite_dimensional k V] [simple V] :
finrank k V = 1 :=
skipped
-- Remark 2.3.13: Every 1-dimensional representation is irreducible
example (V : Module A) [finite_dimensional k V] (h : finrank k V = 1) : simple V :=
simple_of_finrank_eq_one h
-- Example 2.3.14: skipped (1 and 3 we can do, 2 requires Jordan normal form)
/-!
## 2.4 "Ideals"
-/
-- To be continued...
|
a6a31b64d992d7621120e6f44b2e6cedbdcf9580 | b7b549d2cf38ac9d4e49372b7ad4d37f70449409 | /src/LeanLLVM/PP.lean | f3aec2e67f954d8072ccb5ced65695de24beafea | [
"Apache-2.0"
] | permissive | GaloisInc/lean-llvm | 7cc196172fe02ff3554edba6cc82f333c30fdc2b | 36e2ec604ae22d8ec1b1b66eca0f8887880db6c6 | refs/heads/master | 1,637,359,020,356 | 1,629,332,114,000 | 1,629,402,464,000 | 146,700,234 | 29 | 1 | Apache-2.0 | 1,631,225,695,000 | 1,535,607,191,000 | Lean | UTF-8 | Lean | false | false | 22,023 | lean | import Init.Data.List
import Std.Data.RBMap
import Init.Data.String
import LeanLLVM.AST
open Std (RBMap)
namespace LLVM
structure Doc : Type := (compose : String → String)
class HasPP (α:Type _) := (pp : α → Doc)
export HasPP (pp)
namespace Doc
--reserve infixl ` <+> `: 50
--reserve infixl ` $+$ `: 60
def text (x:String) := Doc.mk (fun z => x ++ z)
def render (x:Doc) : String := x.compose ""
def toDoc {a:Type} [ToString a] : a → Doc := text ∘ toString
def empty : Doc := Doc.mk id
def next_to (x y : Doc) : Doc := Doc.mk (x.compose ∘ y.compose)
infixl:50 " <> " => next_to
instance : Inhabited Doc := ⟨empty⟩
def spacesep (x y:Doc) : Doc := x <> text " " <> y
def linesep (x y:Doc) : Doc := x <> text "\n" <> y
infixl:50 " <+> " => spacesep
infixl:60 " $+$ " => linesep
def hcat (xs:List Doc) : Doc := List.foldr next_to empty xs
def hsep (xs:List Doc) : Doc := List.foldr spacesep empty xs
def vcat (xs:List Doc) : Doc := List.foldr linesep empty xs
def punctuate (p:Doc) : List Doc → List Doc
| [ ] => []
| (x::xs) => x :: (List.foldr (fun a b => p :: a :: b) [] xs)
def nat : Nat → Doc := text ∘ toString
def int : Int → Doc := text ∘ toString
def pp_nonzero : Nat → Doc
| Nat.zero => empty
| n => nat n
def surrounding (first last : String) (x:Doc) : Doc :=
text first <> x <> text last
def parens : Doc → Doc := surrounding "(" ")"
def brackets : Doc → Doc := surrounding "[" "]"
def angles : Doc → Doc := surrounding "<" ">"
def braces : Doc → Doc := surrounding "{" "}"
def comma : Doc := text ","
def commas : List Doc → Doc := hcat ∘ punctuate comma
def quotes : Doc → Doc := surrounding "\'" "\'"
def dquotes : Doc → Doc := surrounding "\"" "\""
def pp_opt {A:Type} (f:A → Doc) : Option A → Doc
| some a => f a
| none => empty
end Doc
end LLVM
namespace LLVM
open Doc
namespace AlignType
def ppDoc : AlignType → Doc
| integer => text "i"
| vector => text "v"
| float => text "f"
instance : HasPP AlignType := ⟨ppDoc⟩
end AlignType
namespace Mangling
def ppDoc : Mangling → Doc
| elf => text "e"
| mips => text "m"
| mach_o => text "o"
| windows_coff => text "w"
| windows_coff_x86 => text "x"
instance : HasPP Mangling := ⟨ppDoc⟩
end Mangling
namespace LayoutSpec
def ppDoc : LayoutSpec → Doc
| endianness Endian.big => text "E"
| endianness Endian.little => text "e"
| pointerSize addrSpace sz abi pref idx =>
text "p" <> pp_nonzero addrSpace <> text ":"
<> nat sz <> text ":"
<> nat abi <> text ":"
<> nat pref <> text ":"
<> pp_opt (λi => text ":" <> nat i) idx
| alignSize tp sz abi pref =>
pp tp <> nat sz <> text ":" <> nat abi <> pp_opt (λx => text ":" <> nat x) pref
| nativeIntSize szs => text "n" <> hcat (punctuate (text ":") (List.map nat szs))
| stackAlign n => text "S" <> nat n
| aggregateAlign abi pref => text "a:" <> nat abi <> text ":" <> nat pref
| mangling m => text "m:" <> pp m
| functionAddressSpace n => text "P" <> nat n
| stackAlloca n => text "A" <> nat n
instance : HasPP LayoutSpec := ⟨ppDoc⟩
end LayoutSpec
section LayoutSpec
open LayoutSpec
def pp_layout (xs:List LayoutSpec) : Doc := hcat (punctuate (text "-") (xs.map pp))
def l1 : List LayoutSpec :=
[ endianness Endian.big,
mangling Mangling.mach_o,
pointerSize 0 64 64 64 none,
alignSize AlignType.integer 64 64 none,
alignSize AlignType.float 80 128 none,
alignSize AlignType.float 64 64 none,
nativeIntSize [8,16,32,64],
stackAlign 128
]
end LayoutSpec
-- FIXME! We need to handle escaping...
def pp_string_literal : String → Doc := dquotes ∘ text
namespace Ident
def ppDoc : Ident → Doc
| named nm => text "%" <> text nm -- FIXME! deal with the 'validIdentifier' question
| anon i => text "%" <> toDoc i
instance : HasPP Ident := ⟨ppDoc⟩
end Ident
namespace Symbol
def ppDoc (n:Symbol) : Doc := text "@" <> text (n.symbol)
instance : HasPP Symbol := ⟨ppDoc⟩
end Symbol
namespace BlockLabel
instance : HasPP BlockLabel := ⟨λl => pp l.label⟩
end BlockLabel
def packed_braces : Doc → Doc := surrounding "<{" "}>"
namespace FloatType
def ppDoc : FloatType → Doc
| half => text "half"
| float => text "float"
| double => text "double"
| fp128 => text "fp128"
| x86FP80 => text "x86_fp80"
| ppcFP128 => text "ppcfp128"
instance : HasPP FloatType := ⟨ppDoc⟩
end FloatType
namespace PrimType
def ppDoc : PrimType → Doc
| label => text "label"
| token => text "token"
| void => text "void"
| x86mmx => text "x86mmx"
| metadata => text "metadata"
| floatType f => pp f
| integer n => text "i" <> int n
instance : HasPP PrimType := ⟨ppDoc⟩
end PrimType
def pp_arg_list (va:Bool) (xs:List Doc) : Doc :=
parens (commas (xs ++ if va then [text "..."] else []))
/-
meta def pp_type_tac :=
`[unfold has_well_founded.r measure inv_image sizeof has_sizeof.sizeof
llvm_type.sizeof
at *,
try { linarith }
].
-/
namespace LLVMType
partial def ppDoc : LLVMType → Doc
| prim pt => pp pt
| alias nm => text "%" <> text nm
| array len ty => brackets (int len <+> text "x" <+> ppDoc ty)
| funType ret args va => ppDoc ret <> pp_arg_list va (args.toList.map ppDoc)
| ptr ty => ppDoc ty <> text "*"
| struct false ts => braces (commas (ts.toList.map ppDoc))
| struct true ts => packed_braces (commas (ts.toList.map ppDoc))
| vector len ty => angles (int len <+> text "x" <+> ppDoc ty)
instance : HasPP LLVMType := ⟨ppDoc⟩
end LLVMType
namespace Typed
def pp_with {α} (f:α → Doc) (x:Typed α) := pp x.type <+> f x.value
instance {α} [HasPP α] : HasPP (Typed α) := ⟨pp_with pp⟩
end Typed
namespace ConvOp
def ppDoc : ConvOp → Doc
| trunc => text "trunc"
| zext => text "zext"
| sext => text "sext"
| fp_trunc => text "fptrunc"
| fp_ext => text "fpext"
| fp_to_ui => text "fptoui"
| fp_to_si => text "fptosi"
| ui_to_fp => text "uitofp"
| si_to_fp => text "sitofp"
| ptr_to_int => text "ptrtoint"
| int_to_ptr => text "inttoptr"
| bit_cast => text "bitcast"
instance : HasPP ConvOp := ⟨ppDoc⟩
end ConvOp
def pp_wrap_bits (nuw nsw:Bool) : Doc :=
(if nuw then text " nuw" else empty) <>
(if nsw then text " nsw" else empty)
def pp_exact_bit (exact:Bool) : Doc :=
if exact then text " exact" else empty
namespace ArithOp
def ppDoc : ArithOp → Doc
| add nuw nsw => text "add" <> pp_wrap_bits nuw nsw
| sub nuw nsw => text "sub" <> pp_wrap_bits nuw nsw
| mul nuw nsw => text "mul" <> pp_wrap_bits nuw nsw
| udiv exact => text "udiv" <> pp_exact_bit exact
| sdiv exact => text "sdiv" <> pp_exact_bit exact
| urem => text "urem"
| srem => text "srem"
| fadd => text "fadd"
| fsub => text "fsub"
| fmul => text "fmul"
| fdiv => text "fdiv"
| frem => text "frem"
instance : HasPP ArithOp := ⟨ppDoc⟩
end ArithOp
namespace ICmpOp
def ppDoc : ICmpOp → Doc
| ieq => text "eq"
| ine => text "ne"
| iugt => text "ugt"
| iult => text "ult"
| iuge => text "uge"
| iule => text "ule"
| isgt => text "sgt"
| islt => text "slt"
| isge => text "sge"
| isle => text "sle"
instance : HasPP ICmpOp := ⟨ppDoc⟩
end ICmpOp
namespace FCmpOp
def ppDoc : FCmpOp → Doc
| ffalse => text "false"
| ftrue => text "true"
| foeq => text "oeq"
| fogt => text "ogt"
| foge => text "oge"
| folt => text "olt"
| fole => text "ole"
| fone => text "one"
| ford => text "ord"
| fueq => text "ueq"
| fugt => text "ugt"
| fuge => text "uge"
| fult => text "ult"
| fule => text "ule"
| fune => text "une"
| funo => text "uno"
instance : HasPP FCmpOp := ⟨ppDoc⟩
end FCmpOp
namespace BitOp
def ppDoc : BitOp → Doc
| shl nuw nsw => text "shl" <> pp_wrap_bits nuw nsw
| lshr exact => text "lshr" <> pp_exact_bit exact
| ashr exact => text "ashr" <> pp_exact_bit exact
| and => text "and"
| or => text "or"
| xor => text "xor"
instance : HasPP BitOp := ⟨ppDoc⟩
end BitOp
/-
meta def pp_val_tac :=
`[unfold has_well_founded.r measure inv_image sized.psum_size sizeof has_sizeof.sizeof
typed.sizeof const_expr.sizeof value.sizeof val_md.sizeof debug_loc.sizeof option.sizeof
at *,
repeat { rw llvm.typed.sizeof_spec' at *, unfold sizeof has_sizeof.sizeof at * },
try { linarith }
].
-/
section pp
open ConstExpr
def pp_const_expr (pp_value : Value → Doc) : ConstExpr → Doc
| select cond x y =>
text "select" <+> cond.pp_with pp_value <+> text ","
<+> x.pp_with pp_value <+> text ","
<+> pp_value y.value
| gep inbounds inrange tp vs =>
text "getelementpointer"
<+> (if inbounds then text "inbounds " else empty)
<> parens (pp tp <> comma <+> commas (vs.toList.map (λv => v.pp_with pp_value)))
| conv op x tp => pp op <+> parens (x.pp_with pp_value <+> text "to" <+> pp tp)
| arith op x y => pp op <+> parens (x.pp_with pp_value <> comma <+> pp_value y)
| fcmp op x y =>
text "fcmp"
<+> pp op
<+> parens (x.pp_with pp_value <> comma <+> y.pp_with pp_value)
| icmp op x y =>
text "icmp" <+> pp op
<+> parens (x.pp_with pp_value <> comma <+> y.pp_with pp_value)
| bit op x y =>
pp op <+> parens (x.pp_with pp_value <> comma <+> pp_value y)
| blockAddr sym lab =>
text "blockaddress" <+> parens (pp sym <> comma <+> pp lab)
open DebugLoc
def pp_debug_loc (pp_md : ValMD → Doc) : DebugLoc → Doc
| debugLoc line col scope none =>
text "!DILocation" <> parens (commas
[ text "line:" <+> int line
, text "column:" <+> int col
, text "scope:" <+> pp_md scope
])
| debugLoc line col scope (some ia) =>
text "!DILocation" <> parens (commas
[ text "line:" <+> int line
, text "column:" <+> int col
, text "scope:" <+> pp_md scope
, text "inlinedAt:" <+> pp_md ia
])
open ValMD
partial def pp_md (pp_value : Value → Doc) : ValMD → Doc
| string s => text "!" <> pp_string_literal s
| value x => x.pp_with pp_value
| ref i => text "!" <> int i
| node xs =>
text "!" <> braces (commas (xs.map (λ mx => (mx.map (pp_md pp_value)).getD (text "null"))))
| loc l => pp_debug_loc (pp_md pp_value) l
| debugInfo => empty
open Value
partial def pp_value : Value → Doc
| null => text "null"
| undef => text "undef"
| zeroInit => text "0"
| integer i => toDoc i
| Value.bool b => toDoc b
| Value.string s => text "c" <> pp_string_literal s
| ident n => pp n
| symbol n => pp n
| constExpr e => pp_const_expr pp_value e
| label l => pp l
| array tp vs => brackets (commas (vs.toList.map (λv => pp tp <+> pp_value v)))
| vector tp vs => angles (commas (vs.toList.map (λv => pp tp <+> pp_value v)))
| struct fs => braces (commas (fs.toList.map (λf => f.pp_with pp_value)))
| packedStruct fs => packed_braces (commas (fs.toList.map (λf => f.pp_with pp_value)))
| md d => pp_md pp_value d
| asm se astk a c => text "asm"
<> (if se then text " sideeffect" else empty)
<> (if astk then text " alignstack" else empty)
<+> dquotes (text a)
<> text ","
<+> dquotes (text c)
instance : HasPP Value := ⟨pp_value⟩
end pp
namespace AtomicOrdering
protected def pp : AtomicOrdering → Doc
| unordered => text "unordered"
| monotonic => text "monotonic"
| acquire => text "acquire"
| release => text "release"
| acqRel => text "acq_rel"
| seqCst => text "seq_cst"
instance : HasPP AtomicOrdering := ⟨AtomicOrdering.pp⟩
end AtomicOrdering
def pp_align : Option Nat → Doc
| some a => comma <+> text "align" <+> nat a
| none => empty
def pp_scope : Option String → Doc :=
pp_opt (λnm => text "syncscope" <> parens (pp_string_literal nm))
def pp_call (tailcall:Bool) (mty:Option LLVMType) (f:Value) (args:List (Typed Value)) : Doc :=
(if tailcall then text "tail call" else text "call") <+>
match mty with
| none => text "void"
| some ty => pp ty <+> pp f <+> parens (commas (args.map pp))
def pp_alloca (tp:LLVMType) (len:Option (Typed Value)) (align:Option Nat) : Doc :=
text "alloca" <+> pp tp <>
pp_opt (λv => comma <+> pp v) len <>
pp_align align
def pp_load (ptr:Typed Value) (ord:Option AtomicOrdering) (align : Option Nat) : Doc :=
text "load" <>
(if Option.isSome ord then text " atomic" else empty) <+>
pp ptr <>
pp_opt pp ord <>
pp_opt pp_align align
def pp_store (val:Typed Value) (ptr:Typed Value) (align:Option Nat) : Doc :=
text "store" <+> pp val <> comma <+> pp ptr <> pp_align align
def pp_phi_arg (vl:Value × BlockLabel) : Doc :=
brackets (pp vl.1 <> comma <+> pp vl.2)
def pp_gep (bounds:Bool) (base:Typed Value) (xs:List (Typed Value)) : Doc :=
text "getelementpointer" <>
(if bounds then text " inbounds" else empty) <+>
commas (pp base :: xs.map pp)
def pp_vector_index (v:Value) : Doc := text "i" <> int 32 <+> pp v
def pp_typed_label (l:BlockLabel) : Doc := text "label" <+> pp l
def pp_invoke (ty:LLVMType) (f:Value) (args:List (Typed Value)) (to:BlockLabel) (uw:BlockLabel) : Doc :=
text "invoke" <+> pp ty <+> pp f <>
parens (commas (args.map pp)) <+>
text "to label" <+> pp to <+>
text "unwind label" <+> pp uw
def pp_clause : (Clause × Typed Value)→ Doc
| (Clause.catchExc, v) => text "catch" <+> pp v
| (Clause.filterExc, v) => text "filter" <+> pp v
def pp_clauses (is_cleanup:Bool) (cs:List (Clause × Typed Value) ): Doc :=
hsep ((if is_cleanup then [text "cleanup"] else []) ++ cs.map pp_clause)
def pp_switch_entry (ty:LLVMType) : (Nat × BlockLabel) → Doc
| (i, l) => pp ty <+> nat i <> comma <+> pp l
namespace Instruction
protected
def ppDoc : Instruction → Doc
| ret v => text "ret" <+> pp v
| retVoid => text "ret void"
| arith op x y => pp op <+> pp x <> comma <+> pp y
| bit op x y => pp op <+> pp x <> comma <+> pp y
| conv op x ty => pp op <+> pp x <+> text "to" <+> pp ty
| call tailcall ty f args => pp_call tailcall ty f args.toList
| alloca tp len align => pp_alloca tp len align
| load ptr ord align => pp_load ptr ord align
| store val ptr align => pp_store val ptr align
| icmp op x y => text "icmp" <+> pp op <+> pp x <> comma <+> pp y
| fcmp op x y => text "fcmp" <+> pp op <+> pp x <> comma <+> pp y
| phi ty vls => text "phi" <+> pp ty <+> commas (vls.toList.map pp_phi_arg)
| gep bounds base args => pp_gep bounds base args.toList
| select cond x y =>
text "select" <+> pp cond <> comma <+> pp x <> comma <+> pp x.type <+> pp y
| extractvalue v i =>
text "extractvalue" <+> pp v <> comma <+> commas (i.toList.map nat)
| insertvalue v e i =>
text "insertvalue" <+> pp v <> comma <+> pp e <> comma <+> commas (i.toList.map nat)
| extractelement v i =>
text "extractelement" <+> pp v <> comma <+> pp_vector_index i
| insertelement v e i =>
text "insertelement" <+> pp v <> comma <+> pp e <> comma <+> pp_vector_index i
| shufflevector a b m =>
text "shufflevector" <+> pp a <> comma <+> pp a.type <+> pp b <> comma <+> pp m
| jump l =>
text "jump label" <+> pp l
| br cond thn els =>
text "br" <+> pp cond <> comma <+>
text "label" <+> pp thn <> comma <+> text "label" <+> pp els
| invoke tp f args to uw => pp_invoke tp f args to uw
| comment str =>
text ";" <> text str
| unreachable =>
text "unreachable"
| unwind => text "unwind"
| va_arg v tp => text "va_arg" <+> pp v <> comma <+> pp tp
| indirectbr d ls =>
text "indirectbr" <+> pp d <> comma <+> commas (ls.map pp_typed_label)
| switch c d ls =>
text "switch" <+> pp c <> comma <+>
pp_typed_label d <+>
brackets (hcat (ls.map (pp_switch_entry c.type)))
| landingpad ty mfn c cs =>
text "landingpad" <+> pp ty <>
pp_opt (λv => text " personality" <+> pp v) mfn <+>
pp_clauses c cs
| resume v => text "resume" <+> pp v
instance : HasPP Instruction := ⟨Instruction.ppDoc⟩
end Instruction
namespace Stmt
def ppDoc (s:Stmt) : Doc :=
text " " <>
match s.assign with
| none => pp s.instr
| some i => pp i <+> text "=" <+> pp s.instr
-- <> pp_attached_metadata s.metadata
instance : HasPP Stmt := ⟨ppDoc⟩
end Stmt
namespace BasicBlock
def ppDoc (bb:BasicBlock) := vcat ([ pp bb.label <> text ":" ] ++ bb.stmts.toList.map pp)
instance : HasPP BasicBlock := ⟨ppDoc⟩
end BasicBlock
def pp_comdat_name (nm:String) : Doc :=
text "comdat" <> parens (text "$" <> text nm)
namespace FunAttr
protected def pp : FunAttr → Doc
| align_stack w => text "alignstack" <> parens (nat w)
| alwaysinline => text "alwaysinline"
| builtin => text "builtin"
| cold => text "cold"
| inlinehint => text "inlinehint"
| jumptable => text "jumptable"
| minsize => text "minsize"
| naked => text "naked"
| nobuiltin => text "nobuiltin"
| noduplicate => text "noduplicate"
| noimplicitfloat => text "noimplicitfloat"
| noinline => text "noinline"
| nonlazybind => text "nonlazybind"
| noredzone => text "noredzone"
| noreturn => text "noreturn"
| nounwind => text "nounwind"
| optnone => text "optnone"
| optsize => text "optsize"
| readnone => text "readnone"
| readonly => text "readonly"
| returns_twice => text "returns_twice"
| sanitize_address => text "sanitize_address"
| sanitize_memory => text "sanitize_memory"
| sanitize_thread => text "sanitize_thread"
| ssp => text "ssp"
| ssp_req => text "sspreq"
| ssp_strong => text "sspstrong"
| uwtable => text "uwtable"
instance : HasPP FunAttr := ⟨FunAttr.pp⟩
end FunAttr
namespace Declare
def ppDoc (d:Declare) : Doc :=
text "declare" <+>
pp d.retType <+>
pp d.name <>
pp_arg_list d.varArgs (d.args.toList.map pp) <+>
hsep (d.attrs.toList.map pp) <>
pp_opt (λnm => text " " <> pp_comdat_name nm) d.comdat
instance : HasPP Declare := ⟨ppDoc⟩
end Declare
namespace Linkage
protected def pp : Linkage → Doc
| private_linkage => text "private"
| linker_private => text "linker_private"
| linker_private_weak => text "linker_private_weak"
| linker_private_weak_def_auto => text "linker_private_weak_def_auto"
| internal => text "internal"
| available_externally => text "available_externally"
| linkonce => text "linkonce"
| weak => text "weak"
| common => text "common"
| appending => text "appending"
| extern_weak => text "extern_weak"
| linkonce_odr => text "linkonce_odr"
| weak_odr => text "weak_odr"
| external => text "external"
| dll_import => text "dllimport"
| dll_export => text "dllexport"
instance : HasPP Linkage := ⟨Linkage.pp⟩
end Linkage
namespace Visibility
def pp : Visibility → Doc
| default => text "default"
| hidden => text "hidden"
| protected_visibility => text "protected"
instance : HasPP Visibility := ⟨Visibility.pp⟩
end Visibility
namespace GlobalAttrs
protected def pp (ga:GlobalAttrs) : Doc :=
pp_opt pp ga.linkage <+>
pp_opt pp ga.visibility <+>
(if ga.const then text "const" else text "global")
instance : HasPP GlobalAttrs := ⟨GlobalAttrs.pp⟩
end GlobalAttrs
namespace Global
def ppDoc (g:Global) : Doc :=
pp g.sym <+> text "=" <+>
pp g.attrs <+>
pp g.type <+>
pp_opt pp_value g.value <>
pp_align g.align
-- <> pp_attached_metadata g.metadata
instance : HasPP Global := ⟨ppDoc⟩
end Global
namespace GlobalAlias
def ppDoc (ga:GlobalAlias) : Doc :=
let tgtd := match ga.target with
| Value.symbol _ => pp ga.type <> text " "
| _ => empty;
pp ga.name <+> text "=" <+> tgtd <> pp ga.target
instance : HasPP GlobalAlias := ⟨ppDoc⟩
end GlobalAlias
namespace TypeDeclBody
def ppDoc : TypeDeclBody -> Doc
| opaque => text "opaque"
| defn tp => pp tp
instance : HasPP TypeDeclBody := ⟨TypeDeclBody.ppDoc⟩
end TypeDeclBody
namespace TypeDecl
def ppDoc (t:TypeDecl) := text "%" <> text t.name <+> text "= type" <+> pp t.decl
instance : HasPP TypeDecl := ⟨TypeDecl.ppDoc⟩
end TypeDecl
namespace GC
def ppDoc (x:GC) : Doc := pp_string_literal x.gc
instance : HasPP GC := ⟨ppDoc⟩
end GC
namespace Define
def ppDoc (d:Define) : Doc :=
text "define" <+>
pp_opt pp d.linkage <+>
pp d.retType <+>
pp d.name <>
pp_arg_list d.varArgs (d.args.toList.map pp) <+>
hsep (d.attrs.toList.map pp) <>
pp_opt (λs => text " section" <+> pp_string_literal s) d.sec <>
pp_opt (λg => text " gc" <+> pp g) d.gc <+>
-- pp_mds d.metadata <+>
vcat ([ text "{" ] ++ List.map pp d.body.toList ++ [ text "}" ])
instance : HasPP Define := ⟨ppDoc⟩
end Define
namespace Module
def ppDoc (m:Module) : Doc :=
pp_opt (λnm => text "source_filename = " <> pp_string_literal nm) m.sourceName $+$
text "target datalayout = " <> dquotes (pp_layout m.dataLayout) $+$
vcat (List.join
[ m.types.toList.map pp
, m.globals.toList.map pp
, m.aliases.toList.map pp
, m.declares.toList.map pp
, m.defines.toList.map pp
-- , list.map pp_named_md m.named_md
-- , list.map pp_unnamed_md m.unnamed_md
-- , list.map pp_comdat m.comdat
])
instance : HasPP Module := ⟨ppDoc⟩
end Module
end LLVM
def ppLLVM {α} [LLVM.HasPP α] (a : α) : String := LLVM.Doc.render $ LLVM.HasPP.pp a
|
05a680ac79fdc623ea6bcdf463a11737d8767d7b | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/binderCacheIssue2.lean | 87281d84bbbb2781c013e02dab462f5752cc4280 | [
"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 | 204 | lean | universe u
class Bar.{w} (P : Sort u) :=
fn : P -> Sort w
variable {P : Sort u} (B : Bar P)
structure Foo (X Y : Sort u) := f : Unit
def foo := Foo ((p : P) -> B.fn p) ({p : P} -> B.fn p)
#print foo
|
71b96fb3773ee8892dfddfc1d6f39d9a897108b7 | ebbdcbd7ddc89a9ef7c3b397b301d5f5272a918f | /qp/p1_categories/c6_topos_of_trees/s1_topos_of_trees.lean | a5b2796b7f2689fc1545438e5076abc5496e3cbb | [] | no_license | intoverflow/qvr | 34b9ef23604738381ca20b7d622fd0399d88f2dd | 0cfcd33fe4bf8d93851a00cec5bfd21e77105d74 | refs/heads/master | 1,616,591,570,371 | 1,492,575,772,000 | 1,492,575,772,000 | 80,061,627 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,570 | lean | /- -----------------------------------------------------------------------
The topos of trees.
----------------------------------------------------------------------- -/
import ..c1_basic
import ..c2_limits
namespace qp
open stdaux
universe variables ℓobjx ℓhomx
/-! #brief The topos of trees.
-/
definition TreeTopos : Cat
:= PreShCat NatCat
/-! #brief Action of the later endofunctor on objects.
-/
definition LaterObj.obj
(F : TreeTopos^.obj)
: ∀ (n : ℕ), Type
| 0 := punit
| (nat.succ n) := F^.obj n
/-! #brief Action of the later endofunctor on objects.
-/
definition LaterObj.hom
(F : TreeTopos^.obj)
: ∀ (n₂ n₁ : ℕ) (ωn : n₁ ≤ n₂)
, LaterObj.obj F n₂ → LaterObj.obj F n₁
| n₂ 0 ωn x := punit.star
| 0 (nat.succ n₁) ωn x := false.rec _ (by cases ωn)
| (nat.succ n₂) (nat.succ n₁) ωn x := F^.hom (nat.le_of_succ_le_succ ωn) x
/-! #brief Action of the later endofunctor on objects.
-/
theorem LaterObj.hom.id
(F : TreeTopos^.obj)
: ∀ (n : ℕ)
, LaterObj.hom F n n (nat.le_refl n) = @id (LaterObj.obj F n)
| 0 := begin apply funext, intro u, cases u, trivial end
| (nat.succ n) := F^.hom_id
/-! #brief Action of the later endofunctor on objects.
-/
theorem LaterObj.hom.circ
(F : TreeTopos^.obj)
: ∀ (n₃ n₂ n₁ : ℕ) (ωn₁₂ : n₁ ≤ n₂) (ωn₂₃ : n₂ ≤ n₃)
, LaterObj.hom F n₃ n₁ (nat.le_trans ωn₁₂ ωn₂₃)
= λ x, LaterObj.hom F n₂ n₁ ωn₁₂ (LaterObj.hom F n₃ n₂ ωn₂₃ x)
| 0 0 0 ωn₁₂ ωn₂₃ := rfl
| 0 0 (nat.succ n₁) ωn₁₂ ωn₂₃ := rfl
| 0 (nat.succ n₂) 0 ωn₁₂ ωn₂₃ := rfl
| (nat.succ n₃) 0 0 ωn₁₂ ωn₂₃ := rfl
| 0 (nat.succ n₂) (nat.succ n₁) ωn₁₂ ωn₂₃ := by cases ωn₂₃
| (nat.succ n₃) 0 (nat.succ n₁) ωn₁₂ ωn₂₃ := by cases ωn₁₂
| (nat.succ n₃) (nat.succ n₂) 0 ωn₁₂ ωn₂₃ := rfl
| (nat.succ n₃) (nat.succ n₂) (nat.succ n₁) ωn₁₂ ωn₂₃ := F^.hom_circ
/-! #brief Action of the later endofunctor on objects.
-/
definition LaterObj
(F : TreeTopos^.obj)
: TreeTopos^.obj
:= { obj := LaterObj.obj F
, hom := LaterObj.hom F
, hom_id := LaterObj.hom.id F
, hom_circ := LaterObj.hom.circ F
}
/-! #brief Action of the later endofunctor on homs.
-/
definition LaterHom.com
{F₁ F₂ : TreeTopos^.obj}
(η : TreeTopos^.hom F₁ F₂)
: ∀ (n : ℕ)
, LaterObj.obj F₁ n → LaterObj.obj F₂ n
| 0 := id
| (nat.succ n) := η^.com n
/-! #brief Action of the later endofunctor on homs.
-/
theorem LaterHom.natural
{F₁ F₂ : TreeTopos^.obj}
(η : TreeTopos^.hom F₁ F₂)
: ∀ (n₂ n₁ : ℕ) (ωn : n₁ ≤ n₂)
, (λ x, LaterHom.com η n₁ (LaterObj.hom F₁ n₂ n₁ ωn x))
= λ x, LaterObj.hom F₂ n₂ n₁ ωn (LaterHom.com η n₂ x)
| 0 0 ωn := rfl
| 0 (nat.succ n₁) ωn := by cases ωn
| (nat.succ n₂) 0 ωn := rfl
| (nat.succ n₂) (nat.succ n₁) ωn := η^.natural _
/-! #brief Action of the later endofunctor on homs.
-/
definition LaterHom
(F₁ F₂ : TreeTopos^.obj)
(η : TreeTopos^.hom F₁ F₂)
: TreeTopos^.hom (LaterObj F₁) (LaterObj F₂)
:= { com := LaterHom.com η
, natural := LaterHom.natural η
}
/-! #brief The later endofunctor.
-/
definition LaterFun : Fun TreeTopos TreeTopos
:= { obj := LaterObj
, hom := LaterHom
, hom_id
:= λ F
, begin
apply NatTrans.eq,
apply funext, intro n, cases n,
{ trivial },
{ trivial }
end
, hom_circ
:= λ F₁ F₂ F₃ η₂₃ η₁₂
, begin
apply NatTrans.eq,
apply funext, intro n, cases n,
{ trivial },
{ trivial }
end
}
/-! #brief The left adjoint of the later endofunctor.
-/
definition SoonerFun
: Fun TreeTopos TreeTopos
:= { obj := λ F, { obj := λ n, F^.obj (nat.succ n)
, hom := λ n₂ n₁ ωn, F^.hom (nat.succ_le_succ ωn)
, hom_id := λ n, F^.hom_id
, hom_circ := λ n₃ n₂ n₁ ωn₁₂ ωn₂₃, F^.hom_circ
}
, hom := λ F₁ F₂ η, { com := λ n, η^.com (nat.succ n)
, natural := λ n₂ n₁ ωn, η^.natural _
}
, hom_id
:= λ F
, begin
apply NatTrans.eq,
apply funext, intro n,
trivial
end
, hom_circ
:= λ F₁ F₂ F₃ η₂₃ η₁₂
, begin
apply NatTrans.eq,
apply funext, intro n,
trivial
end
}
/-! #brief SoonerFun and LaterFun are adjoint.
-/
definition SoonerFun_LaterFun.adj
: Adj SoonerFun LaterFun
:= { counit
:= { com := λ F, { com := λ n x, x
, natural := λ n₂ n₁ f, funext (λ x, rfl)
}
, natural := λ F₁ F₂ η, NatTrans.eq (funext (λ n, funext (λ x, rfl)))
}
, unit
:= { com := λ F, { com := λ n x, cast sorry x
, natural := λ n₂ n₁ f, funext (λ x, sorry)
}
, natural := λ F₁ F₂ η, NatTrans.eq (funext (λ n, funext (λ x, sorry)))
}
, id_left := λ F, NatTrans.eq sorry
, id_right := λ F, NatTrans.eq sorry
}
/-! #brief LaterFun preserves all limits.
-/
definition LaterFun.PresLimit
{X : Cat.{ℓobjx ℓhomx}} (F : Fun X TreeTopos)
: PresLimit F LaterFun
:= Adj.right.PresLimit SoonerFun_LaterFun.adj F
/-! #brief The next natural transformation.
-/
definition NextTrans.com (X : TreeTopos^.obj)
: ∀ (n : ℕ)
, X^.obj n → LaterObj.obj X n
| 0 := λ u, punit.star
| (nat.succ n) := X^.hom (nat.le_succ n)
/-! #brief The next natural transformation.
-/
theorem NextTrans.natural (X : TreeTopos^.obj)
: ∀ (n₂ n₁ : ℕ) (ωn : n₁ ≤ n₂)
, Cat.circ LeanCat (NextTrans.com X n₁) (X^.hom ωn)
= Cat.circ LeanCat (LaterObj.hom X n₂ n₁ ωn) (NextTrans.com X n₂)
| 0 0 ωn := rfl
| 0 (nat.succ n₁) ωn := by cases ωn
| (nat.succ n₂) 0 ωn := rfl
| (nat.succ n₂) (nat.succ n₁) ωn
:= begin
refine eq.trans (eq.symm X^.hom_circ) _,
refine eq.trans _ X^.hom_circ,
trivial
end
/-! #brief The next natural transformation.
-/
definition NextTrans (X : TreeTopos^.obj)
: TreeTopos^.hom X (LaterFun^.obj X)
:= { com := NextTrans.com X
, natural := NextTrans.natural X
}
end qp
|
6303e188bc8b96d588f4558363efb93f58228e78 | f3849be5d845a1cb97680f0bbbe03b85518312f0 | /tests/lean/print_ax3.lean | 5558ed190b2a0bb509ecedf4a815618083e58fa0 | [
"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 | 315 | lean | theorem foo1 : 0 = (0:num) :=
rfl
theorem foo2 : 0 = (0:num) :=
rfl
theorem foo3 : 0 = (0:num) :=
foo2
definition foo4 : 0 = (0:num) :=
eq.trans foo2 foo1
lemma foo5 : true = false :=
propext sorry
#print axioms foo4
#print "------"
#print axioms
#print "------"
#print foo3
#print "------"
#print axioms foo5
|
56736650ed91b42ac69c71a40d4fa9bf6b9fb4f4 | 2f2aa789c57653a0a047d209e0401875a9427cfc | /src/submissions/hw7.lean | 9e1f2241ea1b174fbbf7fa4a9f9afb614db3e1f2 | [] | no_license | AlexFetea/cs2120f21 | dc288aa4a907b116555758b7f63c37aa14f952ae | a2cf406a33e66aac2287341b1c3e9954db9b36ab | refs/heads/main | 1,693,910,053,038 | 1,636,678,382,000 | 1,636,678,382,000 | 399,946,831 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,245 | lean | import data.set
import tactic.ring
namespace relation
-- PRELIMINARY SETUP
--GROUP: Sinan Seslikaya ss6gw and Alex Fetea pvn5nv
/-
Preliminary set up. For the rest of this file,
we specify an arbitrary binary relation, r, on
an arbitrary type, β, as a two-place predicate,
with infix notation x ≺ y for (r x y). One can
pronounce these expressions in English as "x is
related to y".
-/
variables {α β : Type} (r : β → β → Prop)
local infix `≺` : 50 := r
/-
The default Lean libraries are missing definitions
for the assympetric property of relations and for
the notion of a powerset. We define these terms for
use in the rest of this file.
-/
def asymmetric := ∀ ⦃x y⦄, x ≺ y → ¬ y ≺ x
def powerset (s : set β) := { s' | s' ⊆ s}
-- PROBLEMS
/-
#1: Give both a formal and an English-language proof. Then
answer the question, is the proposition true if you remove
the first condition, that β is inhabited? Briefly explain
your answer (in English).
-/
example : (∃ (b : β), true) → asymmetric r → ¬reflexive r :=
begin
unfold asymmetric reflexive,
assume prop prop1 prop2,
cases prop with beta t,
have prop3:= prop2 beta,
have prop4:= prop1 prop3,
contradiction,
end
/-
First, we begin by unfolding the asymmetric and reflexive
and assign each of the propositions names. We then do case
analysis on the exists statement, giving us a proof of beta.
Next we apply the second proposition to beta, giving us a proof
of the relation between beta and beta. Now we apply the first
proposition to the relation between beta and beta, giving us a
proof that their is not a relation between beta and beta. This
gives us a contradiction, or a proof of false.
The proposition would not be true if b was not inhabited, because
an empty set would be a counterexample
-/
/-
#2. Logic, like programming, is subtle. It's very easy for humans
to miss subtle corner cases. As an example, today I ran across
a problem in a textbook by Paul Traiger, a professor emeritus
of philosophy and cognitive science at Occidental College. He
asks students to prove that if a relation is both transitive and
reflexive that it cannot be anti-symmetric. See the question at
the very bottom of the page here:
https://sites.oxy.edu/traiger/logic/exercises/chapter13/properties_of_relations_exercise.html
Is the conjecture actually logically valid? If not, what condition
needs to be added to make it so? Try prove this/his version of the
conjecture, as articulated slightly differently below. If you get
stuck, then you need to figure out an additional condition that needs
to be added as a premise to make the proposition true. In that case,
add the premise and then show that the updated conjecture is true.
-/
example :¬symmetric r →transitive r → reflexive r → ¬ asymmetric r :=
begin
unfold symmetric transitive reflexive asymmetric,
intros s t i a,
apply not.intro s,
assume x y,
assume rxy,
have rxx:= i x,
have nrxx:= a rxx,
contradiction,
end
/-
The initial conjecture that a relation that is transitive and reflexive is
also asymmetric is not true. A counter example to this would be a set
in which every element is only related to itself. this would be transitive
reflexive and antisymmetric.
-/
/-
#3: Prove that the subset relation on the powerset of any
set, s, is antisymmetric. Formally state and prove, and
then give an informal proof of, this proposition.
-/
example : ∀ (s : set β)
(s1 s2 ∈ powerset s),
s1 ⊆ s2 →
s2 ⊆ s1 →
s1 = s2 :=
begin
intros s s1 s2 prop1 prop2 s12 s21,
apply set.ext,
assume beta,
apply iff.intro,
assume bs2,
have a:= s12 bs2,
exact a,
assume bs1,
have b:= s21 bs1,
exact b,
end
/-
First we start by assigning variable names to all of the propositions.
We then apply set.ext, which breaks down the statement of equality. We
then apply the intro rule for if and only if, in order to seperate the
biimplication. We then apply the proposition that s1 is a subset of s2
to beta is a subset of s1, giving us a proof that beta is a subset of
s2. Then we apply the proposition that s2 is a subset of s1 to beta
is a subset of s1, giving us a proposition that beta is a subset of s1.
-/
/-
Given two natural numbers, n and m, we will say that m divides n
if there is a natural number, k, such that n = k*m. Here's a formal
definition of this relation.
-/
def divides (m n : ℕ) := ∃ k, n = k * m
/-
#4: Formally and informally state and prove each of the following
propositions. Remember that the ring tactic is useful for producing
proofs of simple algebraic equalities involving + and *. You can use
the phrase, "by basic algebra" when translating the use of this tactic
into English. (Or if you wanted to be truly Hobbit-like you could say
"by the ring axioms!")
-/
-- A: For any n, 1 divides n.
example : ∀ n, divides 1 n :=
begin
assume n,
unfold divides,
apply exists.intro n,
ring,
end
/-
First we assume the variable n and unfold the divides statement. Next,
we apply the intro rule for exists to n, giving us a proof of n = n * 1.
Finally, we use the ring function to simplify.
-/
-- B. For any n, n divides n
example : ∀ n, divides n n :=
begin
assume n,
unfold divides,
apply exists.intro 1,
ring,
end
/-
First we assume the variable n and unfold the divides statement. Next,
we apply the intro rule for exists to 1, giving us a proof of n = n * 1.
Finally, we use the ring function to simplify.
-/
-- #C. prove that divides is reflexive
example : reflexive divides :=
begin
assume n,
unfold reflexive divides,
apply exists.intro 1,
ring,
end
/-
First we assume the variable n and unfold the reflexive divides statement.
Next, we apply the intro rule for exists to 1, giving us a proof of
n = n * 1. Finally, we use the ring function to simplify.
-/
-- #D. prove that divides is transitive
example : transitive divides :=
begin
unfold transitive divides,
assume x y z prop prop1,
apply exists.intro 1,
ring,
cases prop with k c,
cases prop1 with k1 d,
have k2: k = 1 :=sorry,
have k3: k1 = 1 :=sorry,
rw d,
rw c,
ring,
rw k2,
rw k3,
ring,
end
/-
First we unfold the transitive divides statement. Next, we apply the intro
rule for exists to 1. We now need to prove that z=x. We do cases on the
first prop, giving us a proof of y = k * x. We then do cases on the second
prop, giving us a proof of z = k1 * y. We then assign values of 1 to both k
and k1 and rewrite the proofs, giving us a proof of y=x and z=y. We then
rewrite these proofs to give us a proof that z=x.
-/
/-
E. Is divides symmetric? if yes, give a proof, otherwise
give a counterexample and a brief explanation to show that
it's not.
-/
example : symmetric divides :=
begin
unfold symmetric divides,
assume x y a,
apply exists.intro 1,
ring,
cases a with k prop,
have k1: k = 1 :=sorry,
rw prop,
rw k1,
ring,
end
/-
First we unfold the transitive divides statement. We then apply the intro
rule for exists to 1. We now have to prove that x=y. Next, we conduct cases
on a, giving us a proof that y=k*x. Next we create a proof that k=1, and
rewrite the first proof, giving us a proof that x=y.
-/
/-
#F. Prove that divides is antisymmetric.
-/
example : anti_symmetric divides :=
begin
unfold anti_symmetric divides,
assume x y a b,
cases a with k yeq,
have k1: k=1:=sorry,
rw yeq,
rw k1,
ring,
end
/-
First we unfold the asymmetric divides statement. We then conduce cases on
the first proposition, giving us a proof that y = k*x. Next we create a proof
that k=1, and rewrite the first proof, giving us a proof that x=y.
-/
/- #5
Prove the following propositions. Remember that
throughout this file, each definition implicitly
includes β as a type and r as an arbitrary binary
relation on β. In addition to formal proofs, give
an English language proof of the last of the three
problems.
-/
-- A
example : asymmetric r → irreflexive r :=
begin
unfold asymmetric irreflexive,
assume prop x rxx,
have nrxx:= prop rxx,
contradiction
end
/-
First we unfold the asymmetric irreflexive statement. We get a proof of
¬rxx by apply the asymmetric relation to rxx. We then have a proof of
false.
-/
-- B
example : irreflexive r → transitive r → asymmetric r :=
begin
unfold irreflexive transitive asymmetric,
assume prop prop1 x y rxy ryx,
have rxx:= prop1 rxy ryx,
have nrxx:= prop x,
contradiction,
end
/-
First we unfold the irreflexive transitive asymmetric statement. We get a
proof of rxx by applying the transitive relation to rxy and ryx. We then
get a proof of ¬rxx by applying the irreflexive relation to x. We now
have a proof of flase by contraction.
-/
-- C
example : transitive r → ¬ symmetric r → ¬ irreflexive r :=
begin
unfold transitive symmetric irreflexive,
assume t s i,
apply not.intro s,
assume x y,
assume rxy,
end
/-
We don't believe this problem is provable; reflexivity describes an object's
relation with itself. A counterexample would be a completely empty set except
element A is related to element B. This would be be ¬ symmetric transitive and
irreflexive.
-/
end relation
|
e0d122291ed64b2d3822b19ee3355c23420e6851 | 92b1c7f0343a6a5cd36bc0f623a7490da3f1e0f3 | /src/lib/basic.lean | 24cca0f81fcb3f4bd90b2d549d90a83dfc9db5b9 | [
"Apache-2.0"
] | permissive | jtristan/stump-learnable | 717453eb590af16e60c7d3806cc9e66492fab091 | aa3c089f41602efa08d31ef6b41e549456186d57 | refs/heads/master | 1,625,630,634,360 | 1,607,552,106,000 | 1,607,552,106,000 | 218,629,406 | 15 | 2 | null | null | null | null | UTF-8 | Lean | false | false | 11,384 | lean | /-
Copyright © 2019, Oracle and/or its affiliates. All rights reserved.
-/
import lib.attributed.probability_theory
import lib.attributed.dvector lib.attributed.to_mathlib
import measure_theory.giry_monad
import measure_theory.measure_space
import data.complex.exponential
local attribute [instance] classical.prop_decidable
universes u v
open nnreal measure_theory nat list measure_theory.measure to_integration probability_measure set dfin lattice ennreal
variables {α : Type u} {β : Type u} {γ : Type v}[measurable_space α]
infixl ` >>=ₐ `:55 := measure.bind
infixl ` <$>ₐ `:55 := measure.map
local notation `doₐ` binders ` ←ₐ ` m ` ; ` t:(scoped p, m >>=ₐ p) := t
local notation `ret` := measure.dirac
lemma split_set {α β : Type u} (Pa : α → Prop) (Pb : β → Prop) : {m : α × β | Pa m.fst} = {x : α | Pa x}.prod univ := by ext1; cases x; dsimp at *; simp at *
instance has_zero_dfin {n} : has_zero $ dfin (n+1) := ⟨dfin.fz⟩
lemma vec.split_set {n : ℕ} (P : α → Prop) (μ : probability_measure α) :
{x : vec α (n+2)| P (kth_projn x 1)} = set.prod univ {x : vec α (n+1) | P (kth_projn x 0)} :=
begin
ext1, cases x, cases x_snd, dsimp at *, simp at *, refl,
end
lemma vec.prod_measure_univ' {n : ℕ} [nonempty α] [ne : ∀ n, nonempty (vec α n)](μ : probability_measure α) : (vec.prod_measure μ n : measure (vec α (n))) (univ) = 1
:=
by exact measure_univ _
noncomputable def vec.prob_measure (n : ℕ) [nonempty α] (μ : probability_measure α) : probability_measure (vec α n) :=
⟨ vec.prod_measure μ n , vec.prod_measure_univ' μ ⟩
lemma vec.prob_measure_apply (n : ℕ) [nonempty α] {μ : probability_measure α} {S : set (vec α n)} (hS : is_measurable S) : (vec.prob_measure n μ) S = ((vec.prod_measure μ n) S) := rfl
lemma measure_kth_projn' {n : ℕ} [nonempty α] {P : α → Prop} (μ : probability_measure α) (hP : is_measurable {x : α | P x}) (hp' : is_measurable {x : vec α (n + 1) | P (kth_projn x 0)}) :
(vec.prod_measure μ (n+2) : probability_measure (vec α (n+2))) {x : vec α (n + 2) | P (kth_projn x 1)} = μ {x : α | P x} :=
begin
rw vec.split_set _ μ,
rw vec.prod_measure_eq,
rw prod.prob_measure_apply _ _ is_measurable.univ _, rw prob_univ,rw one_mul,
have h: {x : vec α (n + 1) | P (kth_projn x 0)} = {x : vec α (n + 1) | P (x.fst)}, {
ext1, cases x, refl,
},
rw h, clear h,
induction n with k ih, rw vec.prod_measure_eq,
have h₁: {x : vec α (0 + 1) | P (x.fst)} = set.prod {x:α | P(x)} univ,by ext1; cases x; dsimp at *;simp at *,
rw h₁, rw vec.prod_measure,rw prod.prob_measure_apply _ _ _ is_measurable.univ, rw prob_univ, rw mul_one, assumption,assumption, exact hP,
have h₂ : {x : vec α (succ k + 1) | P (x.fst)} = {x : vec α (k + 2) | P (x.fst)},by refl,
rw h₂, clear h₂,
have h₃ : {x : vec α (k + 2) | P (x.fst)} = set.prod {x : α | P(x)} univ, {
ext1, cases x, dsimp at *, simp at *,
},
rw h₃, rw vec.prod_measure_eq,rw prod.prob_measure_apply _ _ _ is_measurable.univ, rw prob_univ, rw mul_one, assumption, apply nonempty.vec, exact hP, assumption, apply nonempty.vec, assumption,
end
@[simp] lemma measure_kth_projn {n : ℕ} [nonempty α] {P : α → Prop} (μ : probability_measure α) (hP : is_measurable {x : α | P x}) (hp' : ∀ n i, is_measurable {x : vec α n | P (kth_projn x i)}) : ∀ (i : dfin (n+1)),
(vec.prod_measure μ n : probability_measure (vec α n)) {x : vec α n | P (kth_projn x i)} = μ {x : α | P x} :=
begin
intros i,
induction n with n b dk ih, rw vec.prod_measure,
have g : {x : vec α 0 | P (kth_projn x i)} = {x | P x}, by tidy, rw g, refl,
have h: {x : vec α (n + 1) | P (kth_projn x fz)} = {x : vec α (n + 1) | P (x.fst)}, {
ext1, cases x, refl,
},
cases i,
rw h, clear h,
have h₃ : {x : vec α (n + 1) | P (x.fst)} = set.prod {x : α | P(x)} univ, {
ext1, cases x, dsimp at *, simp at *,
},
rw h₃, rw vec.prod_measure_eq, rw prod.prob_measure_apply _ _ hP is_measurable.univ, rw prob_univ, rw mul_one, exact (nonempty.vec n), rw vec.prod_measure_eq,
have h₄ : {x : vec α (succ n) | P (kth_projn x (fs i_a))} = set.prod univ {x : vec α n | P (kth_projn x i_a)}, {
ext1, cases x, dsimp at *, simp at *,
},
rw h₄, rw prod.prob_measure_apply _ _ is_measurable.univ _, rw prob_univ, rw one_mul, rw b,
assumption, exact (nonempty.vec n),
apply hp',
end
lemma dfin_succ_prop_iff_fst_and_rst {α : Type u} (P : α → Prop) {k : ℕ} (x : vec α (succ k)) : (∀ (i : dfin (succ k + 1)), P (kth_projn x i)) ↔ P (x.fst) ∧ ∀ (i : dfin (succ k)), P (kth_projn (x.snd) i) :=
begin
fsplit,
intros h, split, have := h fz, have : kth_projn x 0 = x.fst, cases x, refl, rw ←this, assumption,
intro i₀, cases x, have := h (fs i₀), rwa kth_projn at this,
intros g i₁, cases g with l r, cases i₁ with ifz ifs, have : kth_projn x fz = x.fst, cases x, refl, rw this, assumption,
cases x, rw kth_projn, exact r i₁_a,
end
lemma independence {n : ℕ} [nonempty α] {P : α → Prop} (μ : probability_measure α) (hP : is_measurable {x : α | P x}) (hp' : ∀ n, is_measurable {x : vec α n | ∀ i, P (kth_projn x i)}) :
(vec.prod_measure μ n : probability_measure (vec α n)) {x : vec α n | ∀ (i : dfin (n + 1)), P (kth_projn x i)} = μ {x : α | P x} ^ (n+1) :=
begin
induction n with k ih,
simp only [nat.pow_zero, nat_zero_eq_zero],
have g : {x : vec α 0 | ∀ i : dfin 1, P (kth_projn x i)} = {x | P x}, {
ext1, dsimp at *, fsplit, intros a, exact (a fz), intros a i, assumption,
},
rw g, simp, refl,
have h₂ : {x : vec α (succ k) | ∀ (i : dfin (succ k + 1)), P (kth_projn x i)} = set.prod {x | P x} {x : vec α k | ∀ (i : dfin (succ k)), P (kth_projn x i)},{
ext1, apply dfin_succ_prop_iff_fst_and_rst,
},
rw [h₂],
rw vec.prod_measure_eq, rw vec.prod_measure_apply _ _ hP (hp' k),rw ih,refl,
end
@[simp] lemma prob_independence {n : ℕ} [nonempty α] {P : α → Prop} (μ : probability_measure α) (hP : is_measurable {x : α | P x}) (hp' : ∀ n, is_measurable {x : vec α n | ∀ i, P (kth_projn x i)}) :
(vec.prob_measure n μ : probability_measure (vec α n)) {x : vec α n | ∀ (i : dfin (succ n)), P (kth_projn x i)} = (μ {x : α | P x}) ^ (n+1) :=
begin
induction n with k ih,
simp only [nat.pow_zero, nat_zero_eq_zero],
have g : {x : vec α 0 | ∀ i : dfin 1, P (kth_projn x i)} = {x | P x}, {
ext1, dsimp at *, fsplit, intros a, exact (a fz), intros a i, assumption,
},
rw g, rw vec.prob_measure, simp, refl,
have h₂ : {x : vec α (succ k) | ∀ (i : dfin (succ (succ k))), P (kth_projn x i)} = set.prod {x | P x} {x : vec α k | ∀ (i : dfin (succ k)), P (kth_projn x i)},{
ext1, apply dfin_succ_prop_iff_fst_and_rst,
},
rw h₂,
rw vec.prob_measure_apply _ _,
rw [vec.prod_measure_eq], rw vec.prod_measure_apply _ _ hP, rw pow_succ', rw ←ih, rw mul_comm, rw vec.prob_measure_apply, exact hp' k, exact hp' k, exact is_measurable_set_prod hP (hp' k),
end
noncomputable def point_indicators {f h: α → bool} {n : ℕ} (hf : measurable f) (hh : measurable h) (i : dfin (succ n)) := χ ⟦{x : vec α n | h(kth_projn x i) ≠ f (kth_projn x i)}⟧
lemma integral_point_indicators {f h: α → bool} {n : ℕ} [ne : nonempty (vec α n)] (hf : measurable f) (hh : measurable h) (hA : ∀ n i, is_measurable ({x : vec α n | h(kth_projn x i) ≠ f (kth_projn x i)})) (μ : measure (vec α n)) :
∀ i : dfin (succ n),
(∫ χ ⟦{x : vec α n | h(kth_projn x i) ≠ f (kth_projn x i)}⟧ ðμ) = μ ({x : vec α n | h(kth_projn x i) ≠ f (kth_projn x i)}) := assume i, integral_char_fun μ (hA n i)
lemma finally {f h : α → bool} {n : ℕ} [nonempty α] (hf : measurable f) (hh : measurable h) (hA : ∀ n i, is_measurable ({x : vec α n | h(kth_projn x i) ≠ f (kth_projn x i)})) (hB : is_measurable {x : α | h x ≠ f x}) (μ : probability_measure α) : let η := (vec.prod_measure μ n).to_measure in
∀ i, (∫ (χ ⟦{x : vec α n | h(kth_projn x i) ≠ f (kth_projn x i)}⟧) ðη) = μ.to_measure {x : α | h x ≠ f x} :=
begin
intros η i₀,
rw [integral_point_indicators hf hh hA (vec.prod_measure μ n).to_measure i₀], rw ←coe_eq_to_measure, rw ←coe_eq_to_measure,
rw measure_kth_projn μ hB,
intro n₀, apply hA,
end
lemma integral_char_fun_finset_sum {f h : α → bool} {n : ℕ} [nonempty α] (hf : measurable f) (hh : measurable h) (hA : ∀ n i, is_measurable ({x : vec α n | h(kth_projn x i) ≠ f (kth_projn x i)})) (hB : is_measurable {x : α | h x ≠ f x}) (μ : probability_measure α) (m : finset(dfin (succ n))):
(∫finset.sum m (λ (i : dfin (succ n)), ⇑χ⟦{x : vec α n | h (kth_projn x i) ≠ f (kth_projn x i)}⟧)ð((vec.prod_measure μ n).to_measure)) = m.sum (λ i, ((vec.prod_measure μ n)) ({x : vec α n | h(kth_projn x i) ≠ f (kth_projn x i)})) :=
begin
rw integral,
refine finset.induction_on m _ _,
{ simp, erw lintegral_zero },
{ assume a s has ih, simp [has], erw [lintegral_add],
erw simple_func.lintegral_eq_integral,unfold char_fun,
erw simple_func.restrict_const_integral, dsimp, rw ←ih, rw one_mul, rw coe_eq_to_measure, refl, exact(hA n a),
exact measurable.comp (simple_func.measurable _) measurable_id,
refine measurable.comp _ measurable_id,
refine finset.induction_on s _ _,
{simp, exact simple_func.measurable 0,},
{intros a b c d, simp [c], apply measure_theory.measurable_add, exact simple_func.measurable _, exact d,}
},
end
lemma integral_sum_dfin {f h : α → bool} {n : ℕ} [nonempty α] (hf : measurable f) (hh : measurable h) (hA : ∀ n i, is_measurable ({x : vec α n | h(kth_projn x i) ≠ f (kth_projn x i)})) (hB : is_measurable {x : α | h x ≠ f x}) (μ : probability_measure α) (m : finset(dfin (succ n))) : let η := (vec.prod_measure μ n) in
(∫ m.sum (λ i, χ ⟦{x : vec α n | h(kth_projn x i) ≠ f (kth_projn x i)}⟧) ðη.to_measure )= m.sum (λ i, (μ.to_measure : measure α) {x : α | h x ≠ f x}) :=
begin
intros η,
rw [integral_char_fun_finset_sum hf hh hA hB],
congr, funext, rw measure_kth_projn μ hB hA, rw coe_eq_to_measure,
end
lemma measure_sum_const {f h : α → bool} {n : ℕ} (hf : measurable f) (hh : measurable h) (m : finset (fin n)) (μ : probability_measure α) :
m.sum (λ i, (μ : measure α) {x : α | h x ≠ f x}) = (m.card : ℕ) * ((μ : measure α) {x : α | h x ≠ f x}) :=
begin
apply finset.induction_on m, simp,
intros a b c d,
simp [c], rw add_monoid.add_smul, rw [add_monoid.smul],
simp [monoid.pow], rw right_distrib, rw monoid.one_mul, rw ←d,
simp,
end
namespace hoeffding
open complex real
noncomputable def exp_fun (f : α → ennreal) : α → ℝ := λ x, exp $ (f x).to_real
local notation `∫` f `𝒹`m := integral m.to_measure f
lemma integral_char_rect [measurable_space α] [measurable_space β] [n₁ : nonempty α] [n₂ : nonempty β](μ : probability_measure α) (ν : probability_measure β) {A : set α} {B : set β} (hA : is_measurable A) (hB : is_measurable B) :
(∫ χ ⟦ A.prod B ⟧ 𝒹(μ ⊗ₚ ν)) = (μ A) * (ν B) :=
begin
haveI := (nonempty_prod.2 (and.intro n₁ n₂)),
rw [integral_char_fun _ (is_measurable_set_prod hA hB),←coe_eq_to_measure,
(prod.prob_measure_apply _ _ hA hB)], simp,
end
end hoeffding
|
b7075389b74cd2455be0f753ae7723b0b82c642b | 969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb | /src/category_theory/eq_to_hom.lean | ee2ab301a683906052d646b10b8d319da8f040d9 | [
"Apache-2.0"
] | permissive | SAAluthwela/mathlib | 62044349d72dd63983a8500214736aa7779634d3 | 83a4b8b990907291421de54a78988c024dc8a552 | refs/heads/master | 1,679,433,873,417 | 1,615,998,031,000 | 1,615,998,031,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,951 | lean | /-
Copyright (c) 2018 Reid Barton. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Reid Barton, Scott Morrison
-/
import category_theory.opposites
universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation
namespace category_theory
open opposite
variables {C : Type u₁} [category.{v₁} C]
/--
An equality `X = Y` gives us a morphism `X ⟶ Y`.
It is typically better to use this, rather than rewriting by the equality then using `𝟙 _`
which usually leads to dependent type theory hell.
-/
def eq_to_hom {X Y : C} (p : X = Y) : X ⟶ Y := by rw p; exact 𝟙 _
@[simp] lemma eq_to_hom_refl (X : C) (p : X = X) : eq_to_hom p = 𝟙 X := rfl
@[simp, reassoc] lemma eq_to_hom_trans {X Y Z : C} (p : X = Y) (q : Y = Z) :
eq_to_hom p ≫ eq_to_hom q = eq_to_hom (p.trans q) :=
by { cases p, cases q, simp, }
/--
If we (perhaps unintentionally) perform equational rewriting on
the source object of a morphism,
we can replace the resulting `_.mpr f` term by a composition with an `eq_to_hom`.
It may be advisable to introduce any necessary `eq_to_hom` morphisms manually,
rather than relying on this lemma firing.
-/
@[simp]
lemma congr_arg_mpr_hom_left {X Y Z : C} (p : X = Y) (q : Y ⟶ Z) :
(congr_arg (λ W : C, W ⟶ Z) p).mpr q = eq_to_hom p ≫ q :=
by { cases p, simp, }
/--
If we (perhaps unintentionally) perform equational rewriting on
the target object of a morphism,
we can replace the resulting `_.mpr f` term by a composition with an `eq_to_hom`.
It may be advisable to introduce any necessary `eq_to_hom` morphisms manually,
rather than relying on this lemma firing.
-/
@[simp]
lemma congr_arg_mpr_hom_right {X Y Z : C} (p : X ⟶ Y) (q : Z = Y) :
(congr_arg (λ W : C, X ⟶ W) q).mpr p = p ≫ eq_to_hom q.symm :=
by { cases q, simp, }
/--
An equality `X = Y` gives us a morphism `X ⟶ Y`.
It is typically better to use this, rather than rewriting by the equality then using `iso.refl _`
which usually leads to dependent type theory hell.
-/
def eq_to_iso {X Y : C} (p : X = Y) : X ≅ Y :=
⟨eq_to_hom p, eq_to_hom p.symm, by simp, by simp⟩
@[simp] lemma eq_to_iso.hom {X Y : C} (p : X = Y) : (eq_to_iso p).hom = eq_to_hom p :=
rfl
@[simp] lemma eq_to_iso.inv {X Y : C} (p : X = Y) : (eq_to_iso p).inv = eq_to_hom p.symm :=
rfl
@[simp] lemma eq_to_iso_refl {X : C} (p : X = X) : eq_to_iso p = iso.refl X := rfl
@[simp] lemma eq_to_iso_trans {X Y Z : C} (p : X = Y) (q : Y = Z) :
eq_to_iso p ≪≫ eq_to_iso q = eq_to_iso (p.trans q) :=
by ext; simp
@[simp] lemma eq_to_hom_op {X Y : C} (h : X = Y) :
(eq_to_hom h).op = eq_to_hom (congr_arg op h.symm) :=
by { cases h, refl, }
@[simp] lemma eq_to_hom_unop {X Y : Cᵒᵖ} (h : X = Y) :
(eq_to_hom h).unop = eq_to_hom (congr_arg unop h.symm) :=
by { cases h, refl, }
instance {X Y : C} (h : X = Y) : is_iso (eq_to_hom h) := is_iso.of_iso (eq_to_iso h)
@[simp] lemma inv_eq_to_hom {X Y : C} (h : X = Y) : inv (eq_to_hom h) = eq_to_hom h.symm :=
by { ext, simp, }
variables {D : Type u₂} [category.{v₂} D]
namespace functor
/-- Proving equality between functors. This isn't an extensionality lemma,
because usually you don't really want to do this. -/
lemma ext {F G : C ⥤ D} (h_obj : ∀ X, F.obj X = G.obj X)
(h_map : ∀ X Y f, F.map f = eq_to_hom (h_obj X) ≫ G.map f ≫ eq_to_hom (h_obj Y).symm) :
F = G :=
begin
cases F with F_obj _ _ _, cases G with G_obj _ _ _,
have : F_obj = G_obj, by ext X; apply h_obj,
subst this,
congr,
funext X Y f,
simpa using h_map X Y f
end
/-- Proving equality between functors using heterogeneous equality. -/
lemma hext {F G : C ⥤ D} (h_obj : ∀ X, F.obj X = G.obj X)
(h_map : ∀ X Y (f : X ⟶ Y), F.map f == G.map f) : F = G :=
begin
cases F with F_obj _ _ _, cases G with G_obj _ _ _,
have : F_obj = G_obj, by ext X; apply h_obj,
subst this,
congr,
funext X Y f,
exact eq_of_heq (h_map X Y f)
end
-- Using equalities between functors.
lemma congr_obj {F G : C ⥤ D} (h : F = G) (X) : F.obj X = G.obj X :=
by subst h
lemma congr_hom {F G : C ⥤ D} (h : F = G) {X Y} (f : X ⟶ Y) :
F.map f = eq_to_hom (congr_obj h X) ≫ G.map f ≫ eq_to_hom (congr_obj h Y).symm :=
by subst h; simp
end functor
@[simp] lemma eq_to_hom_map (F : C ⥤ D) {X Y : C} (p : X = Y) :
F.map (eq_to_hom p) = eq_to_hom (congr_arg F.obj p) :=
by cases p; simp
@[simp] lemma eq_to_iso_map (F : C ⥤ D) {X Y : C} (p : X = Y) :
F.map_iso (eq_to_iso p) = eq_to_iso (congr_arg F.obj p) :=
by ext; cases p; simp
@[simp] lemma eq_to_hom_app {F G : C ⥤ D} (h : F = G) (X : C) :
(eq_to_hom h : F ⟶ G).app X = eq_to_hom (functor.congr_obj h X) :=
by subst h; refl
lemma nat_trans.congr {F G : C ⥤ D} (α : F ⟶ G) {X Y : C} (h : X = Y) :
α.app X = F.map (eq_to_hom h) ≫ α.app Y ≫ G.map (eq_to_hom h.symm) :=
by { rw [α.naturality_assoc], simp }
end category_theory
|
7a9e86f4599cb131f994a4fe2d343531c1258a9e | b04a2b9331022307985cb4dee8a75dd804625569 | /bertrand.lean | 8119d3fc35f0809fb0ca8e7e4407b83f588410fa | [] | no_license | tjhance/lean-project | 558de3b6f88b5b12241ec8d0803027a32eaea13f | c00a01e02ae2fee4e90d48bfd8504d2d45fdb43c | refs/heads/master | 1,588,096,917,016 | 1,556,580,095,000 | 1,556,580,106,000 | 175,465,730 | 3 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 34,474 | lean | import data.nat.choose
import data.nat.prime
import data.nat.gcd
import data.list
import tactic.linarith
import data.set.finite
import data.multiset
import data.finset
import data.finsupp
open classical
local attribute [instance] nat.decidable_prime_1
/-
https://en.wikipedia.org/wiki/Proof_of_Bertrand%27s_postulate
-/
/- Misc lemmas -/
lemma prod_le_prod : ∀ (l : list ℕ) (f g : ℕ → ℕ) ,
(∀ (x:ℕ) , x ∈ l → f x ≤ g x) →
(l.map f).prod ≤ (l.map g).prod := sorry
lemma sum_le_sum : ∀ (l : list ℕ) (f g : ℕ → ℕ) ,
(∀ (x:ℕ) , x ∈ l → f x ≤ g x) →
(l.map f).sum ≤ (l.map g).sum := sorry
/- Primes dividing choose -/
/- Define a floor log function for natural numbers.
TODO does this already exist somewhere? -/
def log : ℕ → ℕ → ℕ
| 0 _ := 0
| 1 _ := 0
| _ 0 := 0
| (p+2) (n+1) := if (n+1) < (p+2) then 0 else (
have h : (n+1)/(p+2) < n+1, from begin
calc (n + 1) / (p + 2) ≤ (n+1) / 2 : sorry
... < (n+1) / 1 : sorry
... = n+1 : by simp
end,
log (p+2) ((n+1)/(p+2)) + 1
)
theorem pow_log_le : ∀ (a b : ℕ) ,
a ^ (log a b) ≤ b := sorry
/- This is a closed form for the largest exponent
r such that p^r divides (choose n k). -/
def lpdc (n:ℕ) (k:ℕ) (p:ℕ) :=
((list.range' 1 (log p n)).map
(λ (i:ℕ) , if (k % (p^i)) + ((n-k) % (p^i)) ≥ p^i then 1 else 0)).sum
theorem p_pow_lpdc_dvd_choose : ∀ (n k p : ℕ) ,
p ^ lpdc n k p ∣ choose n k := sorry
theorem exp_le_lpdc_of_dvd_choose : ∀ (n k p r : ℕ) ,
p ^ r ∣ choose n k → r ≤ lpdc n k p := sorry
-- Which has this corollary:
lemma exp_le_log_of_dvd_choose : ∀ (n k p r : ℕ) ,
p ^ r ∣ choose n k → r ≤ log p n :=
begin
intros ,
have h : r ≤ lpdc n k p := exp_le_lpdc_of_dvd_choose n k p r a,
rw [lpdc] at h ,
have h' := sum_le_sum (list.range' 1 (log p n))
(λ (i : ℕ), ite (k % p ^ i + (n - k) % p ^ i ≥ p ^ i) 1 0)
(λ (i : ℕ), 1)
(begin
intros , intros , simp , split_ifs , linarith , linarith ,
end),
simp at h' ,
linarith ,
end
/- "lemma 1" -/
lemma four_n_bound : ∀ (n : ℕ) ,
4 ^ n ≤ 2 * n * (choose (2*n) n) := sorry
/- "lemma 2" -/
lemma p_to_R_bound : ∀ (p:ℕ) (r:ℕ) (n:ℕ) ,
nat.prime p →
p ^ r ∣ (choose (2*n) n) →
p ^ r ≤ 2*n :=
begin
intros ,
have h : r ≤ log p (2*n) := exp_le_log_of_dvd_choose (2*n) n p r a_1,
calc p ^ r ≤ p ^ (log p (2*n)) : begin
apply nat.pow_le_pow_of_le_right ,
cases p ,
have q : ¬ nat.prime 0 := dec_trivial , contradiction ,
rw nat.succ_eq_add_one , linarith ,
assumption ,
end
... ≤ 2*n : begin
apply pow_log_le ,
end
end
/- "lemma 3" -/
theorem log_p_eq_1 : ∀ (p n) ,
p ≥ 3 → 2*n / 3 < p → p ≤ n →
log p (2*n) = 1 := sorry
lemma central_primes_do_not_divide : ∀ (p n : ℕ) ,
nat.prime p →
p ≠ 2 →
2*n / 3 < p →
p ≤ n →
¬ (p ∣ (choose (2*n) n)) :=
begin
intros , by_contradiction ,
have h := exp_le_lpdc_of_dvd_choose (2*n) n p 1 (begin
simp , assumption ,
end) ,
have log_1 : log p (2*n) = 1 := begin
apply log_p_eq_1 ,
cases p ,
have h : ¬ (nat.prime 0) := dec_trivial ,
contradiction ,
cases p ,
have h : ¬ (nat.prime 1) := dec_trivial ,
contradiction ,
cases p ,
contradiction ,
rw nat.succ_eq_add_one ,
rw nat.succ_eq_add_one ,
rw nat.succ_eq_add_one ,
linarith , assumption, assumption ,
end,
have j : lpdc (2 * n) n p = 0 := begin
rw [lpdc] , rw log_1 , rw [list.range'] ,
simp ,
have l : ¬(n % p + (2 * n - n) % p ≥ p) := begin
simp , rw two_mul , rw nat.add_sub_cancel ,
rw <- two_mul ,
have t : n = (n - p) + p := begin
rw nat.sub_add_cancel , assumption ,
end,
rw t , simp ,
have s : ((n - p) % p) ≤ n-p := begin
apply nat.mod_le ,
end,
have r : 2*n < 3*p := begin
rw [@nat.mul_comm 3 p] ,
rw <- nat.div_lt_iff_lt_mul' , assumption ,
norm_num ,
end ,
have q : 2*(n-p) < p := begin
rw nat.mul_sub_left_distrib ,
calc 2*n - 2*p < 3*p - 2*p :
begin
rw nat.sub_lt_sub_right_iff , assumption , linarith ,
end
... = (3-2)*p : by rw nat.mul_sub_right_distrib
... = p : by norm_num
end,
linarith ,
end,
simp [l] ,
end,
rw j at h , linarith ,
end
/- range-related lemmas -/
def range_to (n:ℕ) (m:ℕ) := list.range' n (m-(n-1))
theorem range_to_append : ∀ (n m k : ℕ) ,
n ≤ m+1 → m ≤ k →
range_to n m ++ range_to (m+1) k = range_to n k := sorry
/- "lemma 4" -/
def primorial (n : ℕ) : ℕ :=
(list.filter nat.prime (range_to 1 n)).prod
lemma primorial_induction : ∀ (n : ℕ) ,
primorial (n+1) = (if nat.prime (n+1) then primorial n * (n+1) else primorial n) := sorry
lemma zero_lt_primorial : ∀ (n : ℕ) ,
0 < primorial n :=
begin
intros , induction n,
simp [primorial, list.filter, range_to, list.prod] , linarith ,
rw primorial_induction ,
split_ifs , rw mul_add , simp , linarith ,
assumption ,
end
lemma primorial_eq_primorial_mul_primes : ∀ (n m : ℕ) ,
m ≤ n →
primorial n = primorial m *
(list.filter nat.prime (range_to (m+1) n)).prod :=
begin
intros ,
rw [primorial, primorial] ,
rw <- list.prod_append,
rw <- list.filter_append ,
rw <- range_to_append ,
linarith, linarith ,
end
lemma primorial_ratio_eq_prod : ∀ (n m : ℕ) ,
m ≤ n →
primorial n / primorial m =
(list.filter nat.prime (range_to (m+1) n)).prod :=
begin
intros ,
have h : primorial n = primorial m * (list.filter nat.prime (range_to (m+1) n)).prod := primorial_eq_primorial_mul_primes n m a,
rw h , rw nat.mul_div_cancel_left ,
apply zero_lt_primorial ,
end
lemma coprime_list : ∀ (m n p : ℕ) ,
n < p →
nat.prime p →
nat.coprime (list.prod (list.filter nat.prime (range_to (m + 1) n))) p :=
begin
intros , rw [range_to] , rw [nat.add_sub_cancel] ,
by_cases (n < m),
{
have q : n-m = 0 := begin rw nat.sub_eq_zero_iff_le , apply nat.le_of_lt , assumption end,
rw q , simp ,
},
have j : n - m + m = n := begin
rw nat.sub_add_cancel , simp at h , assumption ,
end,
rw <- j at a,
revert a , induction (n-m) ,
{
intros , simp ,
},
{
intros , rw nat.succ_eq_add_one ,
have k : list.range' (m + 1) (n_1 + 1) =
list.range' (m + 1) n_1 ++
list.range' (m + 1 + n_1) 1 := begin
rw list.range'_append , rw nat.add_comm ,
rw [@nat.add_comm 1 n_1] ,
end,
have k' : list.range' (m + 1 + n_1) 1 = [m + 1 + n_1] := by simp ,
rw k' at k , rw k , rw list.filter_append , rw list.prod_append ,
apply nat.coprime.mul ,
apply ih , rw nat.succ_eq_add_one at a , linarith ,
rw [list.filter] , split_ifs , simp ,
rw [@nat.add_comm n_1 1] , rw <- nat.add_assoc ,
rw nat.coprime_primes , rw nat.succ_eq_add_one at a ,
by_contradiction , simp at a_2 , linarith ,
assumption , assumption ,
simp ,
}
end
lemma prime_list_dvd : ∀ (n m k : ℕ) ,
(∀ (p:ℕ) , nat.prime p → m+1 ≤ p → p ≤ n → p ∣ k) →
(list.filter nat.prime (range_to (m+1) n)).prod ∣ k :=
begin
intros, induction n ,
{
rw [range_to] , simp ,
},
{
by_cases (m ≤ n_n) ,
{
have h : range_to (m + 1) (nat.succ n_n) = range_to (m+1) n_n ++ range_to (n_n + 1) (n_n + 1) := begin
rw range_to_append , linarith , linarith ,
end,
rw h , rw list.filter_append , rw list.prod_append ,
have h1 : range_to (n_n + 1) (n_n + 1) = [n_n + 1] := by simp [range_to] ,
rw h1 , simp [list.filter] ,
split_ifs ,
{
have h2 : list.prod [n_n + 1] = n_n + 1 := by simp ,
rw h2 ,
have n_n_plus_1_dvd_k := a (n_n + 1) h_1 (by linarith) (begin
have t : nat.succ n_n = n_n + 1 := rfl , rw t ,
end),
apply nat.coprime.mul_dvd_of_dvd_of_dvd ,
apply coprime_list , linarith , assumption ,
apply n_ih , intros , apply a , assumption, assumption ,
have h_3 : nat.succ n_n = n_n + 1 := rfl , rw h_3 , linarith ,
assumption ,
},
{
simp , apply n_ih , intros , apply a , assumption, assumption, have h_3 : nat.succ n_n = n_n + 1 := rfl , rw h_3 , linarith ,
}
},
{
have h_3 : nat.succ n_n = n_n + 1 := rfl , rw h_3 ,
have t : n_n + 1 - m = 0 := begin
rw nat.sub_eq_zero_iff_le , simp at h ,
apply nat.le_of_lt_succ ,
have h_4 : nat.succ m = m + 1 := rfl , rw h_4 ,
linarith ,
end,
have h : range_to (m + 1) (n_n + 1) = [] := begin
rw [range_to] , simp at h , rw nat.add_sub_cancel ,
rw t , simp ,
end,
rw h , simp ,
}
}
end
lemma p_dvd_choose_2m_plus_1_of_ge_m_plus_2 : ∀ (m p : ℕ) ,
nat.prime p →
m + 2 ≤ p →
p ≤ 2*m + 1 →
p ∣ choose (2*m+1) (m+1) :=
begin
intros ,
have q := @choose_mul_fact_mul_fact (2*m + 1) (m + 1) (by linarith) ,
have r : p ∣ nat.fact (2 * m + 1) := begin
rw nat.prime.dvd_fact , assumption , assumption ,
end,
have r1 : ¬ (p ∣ nat.fact (m + 1)) := begin
rw nat.prime.dvd_fact , simp , linarith , assumption ,
end,
have r2 : ¬ (p ∣ nat.fact (2 * m + 1 - (m + 1))) := begin
rw nat.prime.dvd_fact , simp ,
have h1 : 1 + 2 * m = (m + (m+1)) := by ring ,
rw h1 , rw nat.add_sub_cancel , linarith ,
assumption ,
end,
rw <- q at r ,
rw nat.prime.dvd_mul at r ,
cases r ,
rw nat.prime.dvd_mul at r ,
cases r , assumption , contradiction , assumption , contradiction ,
assumption ,
end
lemma primorial_ratio_le_choose : ∀ (m : ℕ) ,
m ≥ 2 →
primorial (2*m + 1) / primorial (m+1) ≤ (choose (2*m + 1) (m+1)) :=
begin
intros ,
rw primorial_ratio_eq_prod ,
apply nat.le_of_dvd ,
{
apply choose_pos , linarith ,
},
{
apply prime_list_dvd , intros , apply p_dvd_choose_2m_plus_1_of_ge_m_plus_2 , assumption, linarith , assumption ,
},
{
linarith ,
},
end
lemma primorial_2m_plus_1_le_choose : ∀ (m : ℕ) ,
m ≥ 2 →
primorial (2*m + 1) ≤ primorial (m+1) * (choose (2*m + 1) (m+1)) :=
begin
intros ,
calc primorial (2*m + 1) =
primorial (m+1) * (primorial (2*m + 1) / primorial (m+1)) :
begin
rw nat.mul_div_cancel' ,
rw [primorial_eq_primorial_mul_primes (2*m+1) (m+1)] , simp ,
linarith ,
end
... ≤ primorial (m+1) * (choose (2*m + 1) (m+1)) :
begin
apply nat.mul_le_mul_left , apply primorial_ratio_le_choose , assumption ,
end
end
lemma finset_sum_finset_range_eq_sum_range : ∀ (f: ℕ → ℕ) (n : ℕ) ,
finset.sum (finset.range n) f = ((list.range n).map f).sum :=
begin
intros , induction n , simp , rw[list.range] , rw [list.range_core] ,
simp ,
have h1 : list.sum (list.map f (list.range (nat.succ n_n)))
= list.sum (list.map f (list.range n_n)) + f n_n :=
begin
rw [list.range_concat] , simp ,
end,
have h2 : finset.sum (finset.range (nat.succ n_n)) f
= finset.sum (finset.range n_n) f + f n_n :=
begin
rw [finset.sum_range_succ] , rw nat.add_comm ,
end,
rw h1 , rw h2 , rw n_ih ,
end
lemma choose_2m_plus_1_le_power_2 : ∀ (m : ℕ) ,
choose (2*m + 1) (m+1) ≤ 2^(2*m) :=
begin
intros ,
have t := (
calc 2 * 2^(2*m) = 2^(2*m+1) :
begin
rw nat.pow_add , norm_num , ring ,
end
... = ((1:ℕ) + (1:ℕ))^(2*m+1) : by simp
),
have h := add_pow 1 1 (2*m+1) , simp at h ,
simp at t , rw h at t , clear h,
rw finset_sum_finset_range_eq_sum_range at t,
have l : list.range (nat.succ (1 + 2 * m)) =
list.range' 0 m ++
list.range' m 2 ++
list.range' (m+2) m :=
begin
have t1 : list.range' m 2 = list.range' (0+m) 2 := by simp , rw t1 ,
rw [list.range_eq_range'] , rw [list.range'_append] ,
have t2 : list.range' (m + 2) m = list.range' (0 + (2 + m)) m := by simp , rw t2 ,
rw [list.range'_append] , rw [nat.succ_eq_add_one] ,
have t3 : (1 + 2 * m + 1) = (m + (2 + m)) := by ring,
rw t3 ,
end ,
rw l at t ,
rw [list.map_append, list.map_append, list.sum_append, list.sum_append] at t ,
have t' : 2 * 2 ^ (2 * m) ≥
list.sum (list.map (choose (1 + 2 * m)) (list.range' m 2)) := by linarith ,
clear t,
simp at t' ,
have q : choose (1 + 2 * m) m = choose (1 + 2 * m) (m+1) :=
begin
rw choose_eq_fact_div_fact , rw choose_eq_fact_div_fact ,
have t1 : (1 + 2 * m) = m + (m+1) := by ring,
rw t1 , rw nat.add_sub_cancel ,
rw [@nat.add_comm m (m+1)] , rw nat.add_sub_cancel ,
rw [@nat.mul_comm (nat.fact m) (nat.fact (m + 1))] ,
linarith , linarith ,
end ,
rw q at t' ,
have q' : choose (1 + 2 * m) (m + 1) + choose (1 + 2 * m) (m + 1) = 2 * choose (1 + 2 * m) (m + 1) := by ring ,
rw q' at t' ,
simp , linarith ,
end
lemma primorial_2m_plus_1_le_power_2 : ∀ (m : ℕ) ,
m ≥ 2 →
primorial (2*m + 1) ≤ primorial (m+1) * 2^(2*m) :=
begin
intros ,
calc primorial (2*m + 1) ≤
primorial (m+1) * (choose (2*m + 1) (m+1)) :
primorial_2m_plus_1_le_choose m a
... ≤ primorial (m+1) * 2^(2*m) :
begin
apply nat.mul_le_mul_left ,
apply choose_2m_plus_1_le_power_2 ,
end
end
lemma case_by_parity : ∀ (n : ℕ) ,
(∃ (m:ℕ) , n = 2*m) ∨ (∃ (m:ℕ) , n = 2*m + 1) :=
begin
intros , induction n ,
left , existsi 0, norm_num ,
cases n_ih ,
cases n_ih , right , existsi n_ih_w , rw n_ih_h ,
cases n_ih , left , existsi (n_ih_w + 1) , rw n_ih_h ,
rw nat.succ_eq_add_one , ring ,
end
lemma le_2_pow_2_mul_2_mul_plus_1 : ∀ (m : ℕ) ,
2 ^ (2 * (2 * m + 1)) ≤ 2 ^ (2 * (2 * (m + 1))) :=
begin
intros ,
apply nat.pow_le_pow_of_le_right , linarith ,
linarith ,
end
lemma primorial_bound_aux : ∀ (bound n : ℕ) ,
n < bound →
n ≥ 3 →
8 * primorial n < 2 ^ (2*n)
| 0 n :=
begin
intros , linarith ,
end
| (bound+1) 0 := begin intros , linarith end
| (bound+1) 1 := begin intros , linarith end
| (bound+1) 2 := begin intros , linarith end
| (bound+1) 3 :=
begin
intros ,
have p1 : ¬ (nat.prime 1) := dec_trivial ,
have p2 : (nat.prime 2) := dec_trivial ,
have p3 : (nat.prime 3) := dec_trivial ,
rw [primorial, range_to] , norm_num ,
simp [list.filter, list.prod, p1, p2, p3],
norm_num,
end
| (bound+1) 4 :=
begin
intros ,
have p1 : ¬ (nat.prime 1) := dec_trivial ,
have p2 : (nat.prime 2) := dec_trivial ,
have p3 : (nat.prime 3) := dec_trivial ,
have p4 : ¬ (nat.prime 4) := dec_trivial ,
rw [primorial, range_to] , norm_num ,
simp [list.filter, list.prod, p1, p2, p3, p4],
norm_num,
end
| (bound+1) (n+5) := begin
intros ,
cases (case_by_parity (n+5)) ,
{
cases h , rename h_w m , rw h_h at * ,
cases m , simp at a , linarith ,
have q : (nat.succ m) = (m+1) := rfl , rw q at * , clear q ,
have m_ge_1 : m ≥ 1 := by linarith ,
rw primorial ,
rw [<- @range_to_append 1 (2*m + 1) (2*(m+1))] ,
{
rw list.filter_append ,
have r : range_to (2 * m + 1 + 1) (2 * (m + 1)) = [2 * (m+1)] :=
begin
rw [range_to] , rw nat.add_sub_cancel ,
have q' : 2 * (m + 1) = 1 + (2 * m + 1) := by ring ,
have q : 2 * (m + 1) - (2 * m + 1) = 1 := begin
rw q', rw nat.add_sub_cancel ,
end,
rw q ,
rw [list.range', list.range'],
have l : (2 * m + 1 + 1) = 2 * (m + 1) := by ring,
rw l ,
end,
rw r ,
have s : list.filter nat.prime [2 * (m + 1)] = [] :=
begin
rw [list.filter] , split_ifs ,
have t : ¬ (nat.prime (2 * (m+1))) := begin
apply nat.not_prime_mul, linarith , linarith ,
end,
contradiction ,
rw [list.filter] ,
end ,
rw s ,
simp ,
have ih_bound : (2*m + 1 < bound) := by linarith ,
have ih := primorial_bound_aux
bound (2*m + 1) ih_bound (by linarith) ,
rw [<- primorial] ,
rw nat.add_comm ,
calc 8 * primorial (2 * m + 1) < 2 ^ (2 * (2 * m + 1)) : ih
... ≤ 2 ^ (2 * (2 * (m + 1))) : (le_2_pow_2_mul_2_mul_plus_1 m)
},
linarith , linarith ,
},
{
cases h , rename h_w m , rw h_h at * ,
have m_ge_2 : m ≥ 2 := by linarith ,
have h1 := primorial_2m_plus_1_le_power_2 m m_ge_2 ,
have ih_bound : (m+1 < bound) := by linarith ,
have h2 := primorial_bound_aux bound (m+1) ih_bound (by linarith) ,
calc 8 * primorial (2 * m + 1) ≤ 8 * primorial (m + 1) * 2 ^ (2*m) : begin
rw nat.mul_assoc ,
apply nat.mul_le_mul_left , assumption ,
end
... < 2 ^ (2 * (m+1)) * 2 ^ (2*m) :
begin
rw mul_lt_mul_right , assumption ,
apply nat.pos_pow_of_pos , linarith ,
end
... = 2 ^ (2 * (2*m+1)) : begin
rw <- nat.pow_add , ring ,
end
},
end
lemma primorial_bound : ∀ (n : ℕ) ,
n ≥ 3 →
8 * primorial n < 2 ^ (2*n) :=
begin
intros ,
apply (primorial_bound_aux (n+1) n) ,
linarith , assumption ,
end
/- prime factorization -/
theorem factorize : ∀ (n m : ℕ) ,
(∀ p , nat.prime p → p ∣ n → p ≤ m) →
(∃ (r : ℕ → ℕ) , (∀ (p:ℕ) , nat.prime p → p ^ r p ∣ n) ∧
n = (((range_to 1 m).filter nat.prime).map
(λ p , p ^ (r p))).prod
) := sorry
lemma p_le_of_p_dvd_choose : ∀ (p n k : ℕ) ,
k ≤ n → nat.prime p → p ∣ (choose n k) → p ≤ n :=
begin
intros ,
have q := @choose_mul_fact_mul_fact n k a,
have r : (p ∣ nat.fact n) := begin
cases a_2 , rw [a_2_h] at q , rw nat.mul_assoc at q ,
rw nat.mul_assoc at q , rw <- q , apply dvd_mul_right ,
end ,
rw [nat.prime.dvd_fact] at r , assumption ,
assumption ,
end
/- main result (bertrand's postulate for n ≥ 432 case) -/
lemma two_thirds_n_bound : ∀ (n : ℕ) ,
n ≥ 468 →
2 * n / 3 < 2 →
false :=
begin
intros ,
have t := (
calc 2 < 2 * 468 / 3 : by norm_num
... ≤ 2 * n / 3 : begin
apply nat.div_le_div_right , linarith ,
end
... < 2 : a_1
),
linarith ,
end
lemma le_of_sqr_le_sqr : ∀ (n m : ℕ) ,
n*n ≤ m*m → n ≤ m :=
begin
intros , by_contradiction , simp at a_1 ,
have h : m*m < n*n := nat.mul_self_lt_mul_self a_1 ,
linarith ,
end
lemma le_div_mul : ∀ (n m : ℕ) ,
m > 0 →
n ≤ (n / m + 1) * m := sorry
lemma sqrt_le_2_3 : ∀ (n : ℕ) ,
468 ≤ n → nat.sqrt (2 * n) ≤ 2 * n / 3 :=
begin
intros , apply le_of_sqr_le_sqr ,
have h : (2*n / 3 ≥ 4) := begin
calc 2*n / 3 ≥ 2*468 / 3 :
begin
apply nat.div_le_div_right ,
apply nat.mul_le_mul_left , assumption ,
end
... ≥ 4 : by norm_num
end ,
calc nat.sqrt (2 * n) * nat.sqrt (2 * n) ≤ (2*n) : nat.sqrt_le (2*n)
... ≤ (2*n / 3 + 1) * 3 : le_div_mul (2*n) 3 (by linarith)
... = (2*n / 3) * 3 + 3 : begin rw add_mul , simp , end
... ≤ (2*n / 3) * 3 + (2*n / 3) : by linarith
... = (2*n / 3) * 4 :
begin
have q : 4 = 3+1 := by norm_num , rw q ,
rw mul_add, simp ,
end
... ≤ (2 * n / 3) * (2 * n / 3) :
begin
apply nat.mul_le_mul_left , assumption ,
end
end
lemma p_le_2n_over_3 : ∀ (n p : ℕ) ,
(∀ (x : ℕ), nat.prime x → n < x → 2 * n < x) →
n ≥ 468 →
nat.prime p →
p ∣ (choose (2*n) n) →
p ≤ (2*n / 3) :=
begin
intros ,
by_cases (p ≤ 2*n / 3) ,
{
assumption ,
},
simp at h , rename h p_bound ,
by_cases (p ≤ n) ,
{
have q := central_primes_do_not_divide p n a_2 (begin
/- show p > 2 -/
by_contradiction , simp at a_4 , rw a_4 at * ,
apply two_thirds_n_bound , assumption, assumption,
end) p_bound h ,
contradiction ,
},
simp at h , clear p_bound , rename h p_bound ,
{
have q := a p a_2 p_bound ,
have r := p_le_of_p_dvd_choose p (2*n) n (by linarith) a_2 a_3 ,
linarith ,
},
end
lemma log_le_1_of_gt_sqrt : ∀ (n p : ℕ) ,
p > nat.sqrt n → log p n ≤ 1 := sorry
lemma p_at_most_1_in_choose_of_gt_sqrt : ∀ (p n r : ℕ) ,
nat.prime p →
p > nat.sqrt (2*n) →
p ^ r ∣ choose (2*n) n →
r ≤ 1 :=
begin
intros ,
have h : r ≤ log p (2*n) := exp_le_log_of_dvd_choose (2*n) n p r a_2 ,
have j : log p (2*n) ≤ 1 := log_le_1_of_gt_sqrt (2*n) p a_1 ,
exact (nat.le_trans h j) ,
end
lemma factorize_choose_2n_n : ∀ (n : ℕ) ,
(∀ (x : ℕ), nat.prime x → n < x → 2 * n < x) →
n ≥ 468 →
∃ (r : ℕ → ℕ) ,
(∀ (p:ℕ) , nat.prime p → p ^ r p ∣ choose (2*n) n) ∧
(choose (2*n) n =
(((range_to 1 (2*n/3)).filter nat.prime).map
(λ p , p ^ (r p))).prod) :=
begin
intros ,
have f := factorize (choose (2*n) n) (2*n/3) (begin
intros , apply p_le_2n_over_3 , assumption, assumption, assumption,
assumption ,
end),
cases f , rename f_w r , cases f_h , rename f_h_left dvd_condition ,
rename f_h_right prod_condition ,
existsi r ,
split , assumption ,
rw prod_condition ,
end
lemma prod_repeat_eq_pow : ∀ (a b : ℕ) ,
list.prod (list.repeat a b) = a ^ b :=
begin
intros ,
induction b ,
simp , simp ,
rw nat.pow_succ , rw nat.mul_comm ,
end
lemma length_filter_le_length {α : Type} (p : α → Prop) [decidable_pred p] : ∀ (l : list α) ,
list.length (l.filter p) ≤ list.length l :=
begin
intros ,
induction l , simp , simp , rw [list.filter] , split_ifs ,
simp , assumption , linarith ,
end
lemma prime_bounds_1 : ∀ (n : ℕ) (r : ℕ → ℕ),
(∀ p , nat.prime p → p ^ r p ∣ (choose (2*n) n)) →
(((range_to 1 (nat.sqrt (2*n))).filter nat.prime).map
(λ p , p ^ (r p))).prod
≤ (2*n) ^ (nat.sqrt (2*n)) :=
begin
intros ,
by_cases (n ≥ 1) ,
{
have h := prod_le_prod
((range_to 1 (nat.sqrt (2*n))).filter nat.prime)
(λ p , p ^ (r p))
(λ p , 2 * n)
(begin
intros, simp ,
simp at a_1 , cases a_1 ,
apply p_to_R_bound , assumption , apply a , assumption,
end),
exact (nat.le_trans h (begin
rw list.map_const , rw prod_repeat_eq_pow ,
apply nat.pow_le_pow_of_le_right,
linarith ,
have l : (nat.sqrt (2 * n)) = list.length (range_to 1 (nat.sqrt (2 * n))) := begin
rw [range_to] , rw [list.length_range'] , ring ,
end,
have len_le := length_filter_le_length nat.prime (range_to 1 (nat.sqrt (2 * n))) ,
rw <- l at len_le ,
assumption ,
end)),
},
{
simp at h ,
have t : (nat.sqrt 0 = 0) := rfl ,
rw h , norm_num , rw [range_to] , rw t , simp ,
}
end
lemma prime_bounds_2 : ∀ (n : ℕ) (r : ℕ → ℕ),
n ≥ 468 →
(∀ p , nat.prime p → p ^ r p ∣ (choose (2*n) n)) →
(((range_to (nat.sqrt (2*n) + 1) (2*n/3)).filter nat.prime).map
(λ p , p ^ (r p))).prod
≤ primorial (2*n / 3) :=
begin
intros ,
have h := prod_le_prod
((range_to (nat.sqrt (2*n) + 1) (2*n/3)).filter nat.prime)
(λ p , p ^ (r p))
id
(begin
intros , simp at a_2, rw [range_to] at a_2 , simp at a_2 ,
cases a_2 , cases a_2_left ,
simp ,
have t : x ^ r x ≤ x ^ 1 := begin
apply nat.pow_le_pow_of_le_right , linarith ,
apply p_at_most_1_in_choose_of_gt_sqrt x n (r x) ,
assumption , linarith , apply a_1 , assumption ,
end,
simp at t,
assumption ,
end),
exact (nat.le_trans h (begin
rw list.map_id ,
rw primorial ,
have rsplit : range_to 1 (2 * n / 3) =
range_to 1 (nat.sqrt (2 * n)) ++
range_to (nat.sqrt (2 * n) + 1) (2 * n / 3) :=
begin
rw range_to_append , linarith ,
apply sqrt_le_2_3 , assumption ,
end,
rw rsplit ,
rw list.filter_append ,
rw list.prod_append ,
calc
list.prod (list.filter nat.prime (range_to (nat.sqrt (2 * n) + 1) (2 * n / 3))) =
1 * list.prod (list.filter nat.prime (range_to (nat.sqrt (2 * n) + 1) (2 * n / 3))) :
begin
ring ,
end
... ≤
primorial (nat.sqrt (2 * n)) *
list.prod (list.filter nat.prime (range_to (nat.sqrt (2 * n) + 1) (2 * n / 3))) :
begin
apply nat.mul_le_mul_right,
apply nat.le_of_lt_succ ,
have x : nat.succ (primorial (nat.sqrt (2 * n))) = primorial (nat.sqrt (2 * n)) + 1 := rfl ,
rw x ,
have t := zero_lt_primorial (nat.sqrt (2 * n)) ,
linarith ,
end
... =
list.prod (list.filter nat.prime (range_to 1 (nat.sqrt (2 * n)))) *
list.prod (list.filter nat.prime (range_to (nat.sqrt (2 * n) + 1) (2 * n / 3))) :
begin
rw [primorial] ,
end
end))
end
lemma main_bound : ∀ (n : ℕ) ,
n ≥ 468 →
2*n * ((2*n) ^ (nat.sqrt (2*n))) * (4 ^ (2*n/3)) < 4^n := sorry
lemma b_lt_c_of_ab_lt_c : ∀ (a b c : ℕ) ,
a > 0 →
a*b < c → b < c :=
begin
intros ,
calc b = 1*b : by simp
... ≤ a*b :
begin
apply nat.mul_le_mul_right ,
cases a , linarith , rw nat.succ_eq_add_one , linarith ,
end
... < c : by assumption
end
/- copied from assignment 3, surprised I couldn't find this built-in? -/
theorem e1 {α : Type} : ∀ (p : α → Prop) , (¬ ∃ x, p x) → ∀ x, ¬ p x :=
assume p : α → Prop ,
assume h : (¬ ∃ x, p x) ,
assume x : α ,
not.intro (
assume g : p x ,
absurd (exists.intro x g) h
)
theorem exists_of_not_forall {α : Type} : ∀ (p : α → Prop) , (¬ ∀ x, ¬ p x) → ∃ x, p x :=
assume p : α → Prop ,
assume g : (¬ ∀ x, ¬ p x) ,
or.elim (em (∃ x, p x))
(
assume h : (∃ x, p x) ,
h
)
(
assume h : ¬ (∃ x, p x) ,
absurd (e1 p h) g
)
lemma bertrands_postulate_main : ∀ (n : ℕ) ,
n ≥ 468 →
∃ p , nat.prime p ∧ n < p ∧ p ≤ 2*n :=
begin
intros ,
apply exists_of_not_forall ,
by_contradiction ,
simp at a_1 ,
have h := factorize_choose_2n_n n a_1 a ,
cases h , rename h_w r , cases h_h ,
rename h_h_left r_divides ,
rename h_h_right choose_2n_n_factorization ,
have t := (
calc 4^n ≤ 2*n * (choose (2*n) n) : four_n_bound n
... = 2*n * (((range_to 1 (2*n/3)).filter nat.prime).map
(λ p , p ^ (r p))).prod : begin
rw <- choose_2n_n_factorization,
end
... = 2*n *
(((range_to 1 (nat.sqrt (2*n))).filter nat.prime).map
(λ p , p ^ (r p))).prod *
(((range_to (nat.sqrt (2*n) + 1) (2*n/3)).filter nat.prime).map
(λ p , p ^ (r p))).prod :
begin
rw [@nat.mul_assoc (2*n) _ _] ,
rw <- list.prod_append ,
rw <- list.map_append ,
rw <- list.filter_append ,
rw <- range_to_append ,
linarith ,
apply sqrt_le_2_3 , assumption ,
end
... ≤ 2*n *
(2*n) ^ (nat.sqrt (2*n)) *
(((range_to (nat.sqrt (2*n) + 1) (2*n/3)).filter nat.prime).map
(λ p , p ^ (r p))).prod :
begin
apply nat.mul_le_mul_right ,
apply nat.mul_le_mul_left ,
exact (prime_bounds_1 n r r_divides) ,
end
... ≤ 2*n *
(2*n) ^ (nat.sqrt (2*n)) *
primorial (2*n / 3) :
begin
apply nat.mul_le_mul_left ,
exact (prime_bounds_2 n r a r_divides) ,
end
... ≤ 2*n *
(2*n) ^ (nat.sqrt (2*n)) *
4 ^ (2*n/3) :
begin
have q := (calc
(3:ℕ) ≤ 312 : (by linarith)
... = 2 * 468 / 3 : by norm_num
... ≤ 2 * n / 3 : begin
apply nat.div_le_div_right ,
apply nat.mul_le_mul_left ,
assumption ,
end
),
have h := primorial_bound (2*n / 3) q,
have h' := b_lt_c_of_ab_lt_c _ _ _ (by linarith) h,
rw nat.pow_mul at h' ,
have h'' : (2 ^ 2 = 4) := by refl ,
rw h'' at h' ,
apply nat.mul_le_mul_left ,
apply le_of_lt , assumption ,
end
... < 4^n : main_bound n a
),
linarith ,
end
/- full bertrand's postulate -/
lemma prime_2 : nat.prime 2 := dec_trivial
lemma prime_3 : nat.prime 3 := dec_trivial
lemma prime_5 : nat.prime 5 := dec_trivial
lemma prime_7 : nat.prime 7 := dec_trivial
lemma prime_13 : nat.prime 13 := dec_trivial
lemma prime_23 : nat.prime 23 := dec_trivial
lemma prime_43 : nat.prime 43 := dec_trivial
lemma prime_83 : nat.prime 83 := dec_trivial
lemma prime_163 : nat.prime 163 := sorry
lemma prime_317 : nat.prime 317 := sorry
lemma prime_631 : nat.prime 631 := sorry
theorem bertrands_postulate : ∀ (n : ℕ) ,
n ≥ 1 →
∃ p , nat.prime p ∧ n < p ∧ p ≤ 2*n :=
begin
intros ,
by_cases (n < 2) , existsi 2 ,
exact (and.intro prime_2 (and.intro (by assumption) (by linarith))) ,
simp at h , rename h _2_le_n ,
by_cases (n < 3) , existsi 3 ,
exact (and.intro prime_3 (and.intro (by assumption) (by linarith))) ,
simp at h , rename h _3_le_n ,
by_cases (n < 5) , existsi 5 ,
exact (and.intro prime_5 (and.intro (by assumption) (by linarith))) ,
simp at h , rename h _5_le_n ,
by_cases (n < 7) , existsi 7 ,
exact (and.intro prime_7 (and.intro (by assumption) (by linarith))) ,
simp at h , rename h _7_le_n ,
by_cases (n < 13) , existsi 13 ,
exact (and.intro prime_13 (and.intro (by assumption) (by linarith))) ,
simp at h , rename h _13_le_n ,
by_cases (n < 23) , existsi 23 ,
exact (and.intro prime_23 (and.intro (by assumption) (by linarith))) ,
simp at h , rename h _23_le_n ,
by_cases (n < 43) , existsi 43 ,
have p_le_2n : 43 ≤ 2*n :=
(calc 43 ≤ 2*23 : by norm_num
... ≤ 2*n : begin apply nat.mul_le_mul_left , assumption end),
exact (and.intro prime_43 (and.intro (by assumption) (by assumption))) ,
simp at h , rename h _43_le_n ,
by_cases (n < 83) , existsi 83 ,
have p_le_2n : 83 ≤ 2*n :=
(calc 83 ≤ 2*43 : by norm_num
... ≤ 2*n : begin apply nat.mul_le_mul_left , assumption end),
exact (and.intro prime_83 (and.intro (by assumption) (by assumption))) ,
simp at h , rename h _83_le_n ,
by_cases (n < 163) , existsi 163 ,
have p_le_2n : 163 ≤ 2*n :=
(calc 163 ≤ 2*83 : by norm_num
... ≤ 2*n : begin apply nat.mul_le_mul_left , assumption end),
exact (and.intro prime_163 (and.intro (by assumption) (by assumption))) ,
simp at h , rename h _163_le_n ,
by_cases (n < 317) , existsi 317 ,
have p_le_2n : 317 ≤ 2*n :=
(calc 317 ≤ 2*163 : by norm_num
... ≤ 2*n : begin apply nat.mul_le_mul_left , assumption end),
exact (and.intro prime_317 (and.intro (by assumption) (by assumption))) ,
simp at h , rename h _317_le_n ,
by_cases (n < 631) , existsi 631 ,
have p_le_2n : 631 ≤ 2*n :=
(calc 631 ≤ 2*317 : by norm_num
... ≤ 2*n : begin apply nat.mul_le_mul_left , assumption end),
exact (and.intro prime_631 (and.intro (by assumption) (by assumption))) ,
simp at h , rename h _631_le_n ,
have t := (calc 468 ≤ 631 : by norm_num ... ≤ n : by assumption) ,
exact (bertrands_postulate_main n t) ,
end |
e15a6063fde49b472e9af3d5d7b40b814b2c2a47 | 82e44445c70db0f03e30d7be725775f122d72f3e | /src/algebra/category/Algebra/basic.lean | 7946a385d2a7e664b3a84ae849ae53fac7293614 | [
"Apache-2.0"
] | permissive | stjordanis/mathlib | 51e286d19140e3788ef2c470bc7b953e4991f0c9 | 2568d41bca08f5d6bf39d915434c8447e21f42ee | refs/heads/master | 1,631,748,053,501 | 1,627,938,886,000 | 1,627,938,886,000 | 228,728,358 | 0 | 0 | Apache-2.0 | 1,576,630,588,000 | 1,576,630,587,000 | null | UTF-8 | Lean | false | false | 5,502 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import algebra.algebra.basic
import algebra.algebra.subalgebra
import algebra.free_algebra
import algebra.category.CommRing.basic
import algebra.category.Module.basic
/-!
# Category instance for algebras over a commutative ring
We introduce the bundled category `Algebra` of algebras over a fixed commutative ring `R ` along
with the forgetful functors to `Ring` and `Module`. We furthermore show that the functor associating
to a type the free `R`-algebra on that type is left adjoint to the forgetful functor.
-/
open category_theory
open category_theory.limits
universes v u
variables (R : Type u) [comm_ring R]
/-- The category of R-algebras and their morphisms. -/
structure Algebra :=
(carrier : Type v)
[is_ring : ring carrier]
[is_algebra : algebra R carrier]
attribute [instance] Algebra.is_ring Algebra.is_algebra
namespace Algebra
instance : has_coe_to_sort (Algebra R) :=
{ S := Type v, coe := Algebra.carrier }
instance : category (Algebra.{v} R) :=
{ hom := λ A B, A →ₐ[R] B,
id := λ A, alg_hom.id R A,
comp := λ A B C f g, g.comp f }
instance : concrete_category.{v} (Algebra.{v} R) :=
{ forget := { obj := λ R, R, map := λ R S f, (f : R → S) },
forget_faithful := { } }
instance has_forget_to_Ring : has_forget₂ (Algebra.{v} R) Ring.{v} :=
{ forget₂ :=
{ obj := λ A, Ring.of A,
map := λ A₁ A₂ f, alg_hom.to_ring_hom f, } }
instance has_forget_to_Module : has_forget₂ (Algebra.{v} R) (Module.{v} R) :=
{ forget₂ :=
{ obj := λ M, Module.of R M,
map := λ M₁ M₂ f, alg_hom.to_linear_map f, } }
/-- The object in the category of R-algebras associated to a type equipped with the appropriate
typeclasses. -/
def of (X : Type v) [ring X] [algebra R X] : Algebra.{v} R := ⟨X⟩
instance : inhabited (Algebra R) := ⟨of R R⟩
@[simp]
lemma coe_of (X : Type u) [ring X] [algebra R X] : (of R X : Type u) = X := rfl
variables {R}
/-- Forgetting to the underlying type and then building the bundled object returns the original
algebra. -/
@[simps]
def of_self_iso (M : Algebra.{v} R) : Algebra.of R M ≅ M :=
{ hom := 𝟙 M, inv := 𝟙 M }
variables {R} {M N U : Module.{v} R}
@[simp] lemma id_apply (m : M) : (𝟙 M : M → M) m = m := rfl
@[simp] lemma coe_comp (f : M ⟶ N) (g : N ⟶ U) :
((f ≫ g) : M → U) = g ∘ f := rfl
variables (R)
/-- The "free algebra" functor, sending a type `S` to the free algebra on `S`. -/
@[simps]
def free : Type u ⥤ Algebra.{u} R :=
{ obj := λ S,
{ carrier := free_algebra R S,
is_ring := algebra.semiring_to_ring R },
map := λ S T f, free_algebra.lift _ $ (free_algebra.ι _) ∘ f,
-- obviously can fill the next two goals, but it is slow
map_id' := by { intros X, ext1, simp only [free_algebra.ι_comp_lift], refl },
map_comp' := by { intros, ext1, simp only [free_algebra.ι_comp_lift], ext1,
simp only [free_algebra.lift_ι_apply, category_theory.coe_comp, function.comp_app,
types_comp_apply] } }
/-- The free/forget adjunction for `R`-algebras. -/
def adj : free.{u} R ⊣ forget (Algebra.{u} R) :=
adjunction.mk_of_hom_equiv
{ hom_equiv := λ X A, (free_algebra.lift _).symm,
-- Relying on `obviously` to fill out these proofs is very slow :(
hom_equiv_naturality_left_symm' := by { intros, ext,
simp only [free_map, equiv.symm_symm, free_algebra.lift_ι_apply, category_theory.coe_comp,
function.comp_app, types_comp_apply] },
hom_equiv_naturality_right' := by { intros, ext,
simp only [forget_map_eq_coe, category_theory.coe_comp, function.comp_app,
free_algebra.lift_symm_apply, types_comp_apply] } }
instance : is_right_adjoint (forget (Algebra.{u} R)) := ⟨_, adj R⟩
end Algebra
variables {R}
variables {X₁ X₂ : Type u}
/-- Build an isomorphism in the category `Algebra R` from a `alg_equiv` between `algebra`s. -/
@[simps]
def alg_equiv.to_Algebra_iso
{g₁ : ring X₁} {g₂ : ring X₂} {m₁ : algebra R X₁} {m₂ : algebra R X₂} (e : X₁ ≃ₐ[R] X₂) :
Algebra.of R X₁ ≅ Algebra.of R X₂ :=
{ hom := (e : X₁ →ₐ[R] X₂),
inv := (e.symm : X₂ →ₐ[R] X₁),
hom_inv_id' := begin ext, exact e.left_inv x, end,
inv_hom_id' := begin ext, exact e.right_inv x, end, }
namespace category_theory.iso
/-- Build a `alg_equiv` from an isomorphism in the category `Algebra R`. -/
@[simps]
def to_alg_equiv {X Y : Algebra R} (i : X ≅ Y) : X ≃ₐ[R] Y :=
{ to_fun := i.hom,
inv_fun := i.inv,
left_inv := by tidy,
right_inv := by tidy,
map_add' := by tidy,
map_mul' := by tidy,
commutes' := by tidy, }.
end category_theory.iso
/-- Algebra equivalences between `algebras`s are the same as (isomorphic to) isomorphisms in
`Algebra`. -/
@[simps]
def alg_equiv_iso_Algebra_iso {X Y : Type u}
[ring X] [ring Y] [algebra R X] [algebra R Y] :
(X ≃ₐ[R] Y) ≅ (Algebra.of R X ≅ Algebra.of R Y) :=
{ hom := λ e, e.to_Algebra_iso,
inv := λ i, i.to_alg_equiv, }
instance (X : Type u) [ring X] [algebra R X] : has_coe (subalgebra R X) (Algebra R) :=
⟨ λ N, Algebra.of R N ⟩
instance Algebra.forget_reflects_isos : reflects_isomorphisms (forget (Algebra.{u} R)) :=
{ reflects := λ X Y f _,
begin
resetI,
let i := as_iso ((forget (Algebra.{u} R)).map f),
let e : X ≃ₐ[R] Y := { ..f, ..i.to_equiv },
exact ⟨(is_iso.of_iso e.to_Algebra_iso).1⟩,
end }
|
7de3d6c5d53831e312cc6ccee4460aa1a3b5a091 | 1446f520c1db37e157b631385707cc28a17a595e | /tests/lean/run/IO_test.lean | 9920d65ba033aa08a65b0dc9521c452621a185a2 | [
"Apache-2.0"
] | permissive | bdbabiak/lean4 | cab06b8a2606d99a168dd279efdd404edb4e825a | 3f4d0d78b2ce3ef541cb643bbe21496bd6b057ac | refs/heads/master | 1,615,045,275,530 | 1,583,793,696,000 | 1,583,793,696,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,442 | lean | prelude
import Init.System.IO
import Init.Data.List.Control
open IO.FS
instance : HasRepr UInt8 := ⟨ toString ⟩
def check_eq {α} [HasBeq α] [HasRepr α] (tag : String) (expected actual : α) : IO Unit :=
unless (expected == actual) $ throw $ IO.userError $
"assertion failure \"" ++ tag ++
"\":\n expected: " ++ repr expected ++
"\n actual: " ++ repr actual
def test : IO Unit := do
let xs : ByteArray := ⟨#[1,2,3,4]⟩;
let fn := "foo.txt";
withFile fn Mode.write $ fun h => do
{ h.write xs;
h.write xs;
pure () };
ys ← withFile "foo.txt" Mode.read $ fun h => h.read 10;
check_eq "1" (xs.toList ++ xs.toList) ys.toList;
withFile fn Mode.append $ fun h => do
{ h.write ⟨#[5,6,7,8]⟩;
pure () };
withFile "foo.txt" Mode.read $ fun h => do
{ ys ← h.read 10;
check_eq "2" [1,2,3,4,1,2,3,4,5,6] ys.toList;
ys ← h.read 2;
check_eq "3" [7,8] ys.toList;
b ← h.isEof;
unless (!b)
(throw $ IO.userError $ "wrong (4): ");
ys ← h.read 2;
check_eq "5" [] ys.toList;
b ← h.isEof;
unless b
(throw $ IO.userError $ "wrong (6): ") };
pure ()
#eval test
def test2 : IO Unit := do
let fn2 := "foo2.txt";
let xs₀ : String := "⟨[₂,α]⟩";
let xs₁ := "⟨[6,8,@]⟩";
let xs₂ := "/* Handle.getLine : Handle → IO Unit */" ++
"/* The line returned by `lean_io_prim_handle_get_line` */" ++
"/* is truncated at the first \'\\0\' character and the */" ++
"/* rest of the line is discarded. */";
-- multi-buffer line
withFile fn2 Mode.write $ fun h => pure ();
withFile fn2 Mode.write $ fun h => do
{ h.putStr xs₀;
h.putStrLn xs₀;
h.putStrLn xs₂;
h.putStrLn xs₁;
pure () };
ys ← withFile fn2 Mode.read $ fun h => h.getLine;
check_eq "1" (xs₀ ++ xs₀ ++ "\n") ys;
withFile fn2 Mode.append $ fun h => do
{ h.putStrLn xs₁;
pure () };
ys ← withFile fn2 Mode.read $ fun h => do
{ ys ← (List.iota 5).mapM $ fun i => do
{ ln ← h.getLine;
-- IO.println ∘ repr $ ln;
b ← h.isEof;
unless (i == 1 || !b) (throw $ IO.userError "isEof");
pure ln };
b ← h.isEof;
unless b (throw $ IO.userError "not isEof");
pure ys };
let rs := [xs₀ ++ xs₀ ++ "\n", xs₂ ++ "\n", xs₁ ++ "\n", xs₁ ++ "\n", ""];
check_eq "2" rs ys;
ys ← readFile fn2;
check_eq "3" (String.join rs) ys;
pure ()
#eval test2
|
c4664f2d07f383cabd8350b64fc7dbf63f33f18d | 1abd1ed12aa68b375cdef28959f39531c6e95b84 | /src/order/conditionally_complete_lattice.lean | b32f8e616afeac7006fda529656d9039c5153f96 | [
"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 | 49,770 | lean | /-
Copyright (c) 2018 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import data.set.intervals.ord_connected
/-!
# Theory of conditionally complete lattices.
A conditionally complete lattice is a lattice in which every non-empty bounded subset s
has a least upper bound and a greatest lower bound, denoted below by Sup s and Inf s.
Typical examples are real, nat, int with their usual orders.
The theory is very comparable to the theory of complete lattices, except that suitable
boundedness and nonemptiness assumptions have to be added to most statements.
We introduce two predicates bdd_above and bdd_below to express this boundedness, prove
their basic properties, and then go on to prove most useful properties of Sup and Inf
in conditionally complete lattices.
To differentiate the statements between complete lattices and conditionally complete
lattices, we prefix Inf and Sup in the statements by c, giving cInf and cSup. For instance,
Inf_le is a statement in complete lattices ensuring Inf s ≤ x, while cInf_le is the same
statement in conditionally complete lattices with an additional assumption that s is
bounded below.
-/
set_option old_structure_cmd true
open set
variables {α β : Type*} {ι : Sort*}
section
/-!
Extension of Sup and Inf from a preorder `α` to `with_top α` and `with_bot α`
-/
open_locale classical
noncomputable instance {α : Type*} [preorder α] [has_Sup α] : has_Sup (with_top α) :=
⟨λ S, if ⊤ ∈ S then ⊤ else
if bdd_above (coe ⁻¹' S : set α) then ↑(Sup (coe ⁻¹' S : set α)) else ⊤⟩
noncomputable instance {α : Type*} [has_Inf α] : has_Inf (with_top α) :=
⟨λ S, if S ⊆ {⊤} then ⊤ else ↑(Inf (coe ⁻¹' S : set α))⟩
noncomputable instance {α : Type*} [has_Sup α] : has_Sup (with_bot α) :=
⟨(@with_top.has_Inf (order_dual α) _).Inf⟩
noncomputable instance {α : Type*} [preorder α] [has_Inf α] : has_Inf (with_bot α) :=
⟨(@with_top.has_Sup (order_dual α) _ _).Sup⟩
@[simp]
theorem with_top.cInf_empty {α : Type*} [has_Inf α] : Inf (∅ : set (with_top α)) = ⊤ :=
if_pos $ set.empty_subset _
@[simp]
theorem with_bot.cSup_empty {α : Type*} [has_Sup α] : Sup (∅ : set (with_bot α)) = ⊥ :=
if_pos $ set.empty_subset _
end -- section
/-- A conditionally complete lattice is a lattice in which
every nonempty subset which is bounded above has a supremum, and
every nonempty subset which is bounded below has an infimum.
Typical examples are real numbers or natural numbers.
To differentiate the statements from the corresponding statements in (unconditional)
complete lattices, we prefix Inf and Sup by a c everywhere. The same statements should
hold in both worlds, sometimes with additional assumptions of nonemptiness or
boundedness.-/
class conditionally_complete_lattice (α : Type*) extends lattice α, has_Sup α, has_Inf α :=
(le_cSup : ∀s a, bdd_above s → a ∈ s → a ≤ Sup s)
(cSup_le : ∀ s a, set.nonempty s → a ∈ upper_bounds s → Sup s ≤ a)
(cInf_le : ∀s a, bdd_below s → a ∈ s → Inf s ≤ a)
(le_cInf : ∀s a, set.nonempty s → a ∈ lower_bounds s → a ≤ Inf s)
/-- A conditionally complete linear order is a linear order in which
every nonempty subset which is bounded above has a supremum, and
every nonempty subset which is bounded below has an infimum.
Typical examples are real numbers or natural numbers.
To differentiate the statements from the corresponding statements in (unconditional)
complete linear orders, we prefix Inf and Sup by a c everywhere. The same statements should
hold in both worlds, sometimes with additional assumptions of nonemptiness or
boundedness.-/
class conditionally_complete_linear_order (α : Type*)
extends conditionally_complete_lattice α, linear_order α
/-- A conditionally complete linear order with `bot` is a linear order with least element, in which
every nonempty subset which is bounded above has a supremum, and every nonempty subset (necessarily
bounded below) has an infimum. A typical example is the natural numbers.
To differentiate the statements from the corresponding statements in (unconditional)
complete linear orders, we prefix Inf and Sup by a c everywhere. The same statements should
hold in both worlds, sometimes with additional assumptions of nonemptiness or
boundedness.-/
class conditionally_complete_linear_order_bot (α : Type*)
extends conditionally_complete_linear_order α, order_bot α :=
(cSup_empty : Sup ∅ = ⊥)
/- A complete lattice is a conditionally complete lattice, as there are no restrictions
on the properties of Inf and Sup in a complete lattice.-/
@[priority 100] -- see Note [lower instance priority]
instance conditionally_complete_lattice_of_complete_lattice [complete_lattice α]:
conditionally_complete_lattice α :=
{ le_cSup := by intros; apply le_Sup; assumption,
cSup_le := by intros; apply Sup_le; assumption,
cInf_le := by intros; apply Inf_le; assumption,
le_cInf := by intros; apply le_Inf; assumption,
..‹complete_lattice α› }
@[priority 100] -- see Note [lower instance priority]
instance conditionally_complete_linear_order_of_complete_linear_order [complete_linear_order α]:
conditionally_complete_linear_order α :=
{ ..conditionally_complete_lattice_of_complete_lattice, .. ‹complete_linear_order α› }
section
open_locale classical
/-- A well founded linear order is conditionally complete, with a bottom element. -/
@[reducible] noncomputable def well_founded.conditionally_complete_linear_order_with_bot
{α : Type*} [i : linear_order α] (h : well_founded ((<) : α → α → Prop))
(c : α) (hc : c = h.min set.univ ⟨c, mem_univ c⟩) :
conditionally_complete_linear_order_bot α :=
{ sup := max,
le_sup_left := le_max_left,
le_sup_right := le_max_right,
sup_le := λ a b c, max_le,
inf := min,
inf_le_left := min_le_left,
inf_le_right := min_le_right,
le_inf := λ a b c, le_min,
Inf := λ s, if hs : s.nonempty then h.min s hs else c,
cInf_le := begin
assume s a hs has,
have s_ne : s.nonempty := ⟨a, has⟩,
simpa [s_ne] using not_lt.1 (h.not_lt_min s s_ne has),
end,
le_cInf := begin
assume s a hs has,
simp only [hs, dif_pos],
exact has (h.min_mem s hs),
end,
Sup := λ s, if hs : (upper_bounds s).nonempty then h.min _ hs else c,
le_cSup := begin
assume s a hs has,
have h's : (upper_bounds s).nonempty := hs,
simp only [h's, dif_pos],
exact h.min_mem _ h's has,
end,
cSup_le := begin
assume s a hs has,
have h's : (upper_bounds s).nonempty := ⟨a, has⟩,
simp only [h's, dif_pos],
simpa using h.not_lt_min _ h's has,
end,
bot := c,
bot_le := λ x, by convert not_lt.1 (h.not_lt_min set.univ ⟨c, mem_univ c⟩ (mem_univ x)),
cSup_empty := begin
have : (set.univ : set α).nonempty := ⟨c, mem_univ c⟩,
simp only [this, dif_pos, upper_bounds_empty],
exact hc.symm
end,
.. i }
end
section order_dual
instance (α : Type*) [conditionally_complete_lattice α] :
conditionally_complete_lattice (order_dual α) :=
{ le_cSup := @conditionally_complete_lattice.cInf_le α _,
cSup_le := @conditionally_complete_lattice.le_cInf α _,
le_cInf := @conditionally_complete_lattice.cSup_le α _,
cInf_le := @conditionally_complete_lattice.le_cSup α _,
..order_dual.has_Inf α,
..order_dual.has_Sup α,
..order_dual.lattice α }
instance (α : Type*) [conditionally_complete_linear_order α] :
conditionally_complete_linear_order (order_dual α) :=
{ ..order_dual.conditionally_complete_lattice α,
..order_dual.linear_order α }
end order_dual
section conditionally_complete_lattice
variables [conditionally_complete_lattice α] {s t : set α} {a b : α}
theorem le_cSup (h₁ : bdd_above s) (h₂ : a ∈ s) : a ≤ Sup s :=
conditionally_complete_lattice.le_cSup s a h₁ h₂
theorem cSup_le (h₁ : s.nonempty) (h₂ : ∀b∈s, b ≤ a) : Sup s ≤ a :=
conditionally_complete_lattice.cSup_le s a h₁ h₂
theorem cInf_le (h₁ : bdd_below s) (h₂ : a ∈ s) : Inf s ≤ a :=
conditionally_complete_lattice.cInf_le s a h₁ h₂
theorem le_cInf (h₁ : s.nonempty) (h₂ : ∀b∈s, a ≤ b) : a ≤ Inf s :=
conditionally_complete_lattice.le_cInf s a h₁ h₂
theorem le_cSup_of_le (_ : bdd_above s) (hb : b ∈ s) (h : a ≤ b) : a ≤ Sup s :=
le_trans h (le_cSup ‹bdd_above s› hb)
theorem cInf_le_of_le (_ : bdd_below s) (hb : b ∈ s) (h : b ≤ a) : Inf s ≤ a :=
le_trans (cInf_le ‹bdd_below s› hb) h
theorem cSup_le_cSup (_ : bdd_above t) (_ : s.nonempty) (h : s ⊆ t) : Sup s ≤ Sup t :=
cSup_le ‹_› (assume (a) (ha : a ∈ s), le_cSup ‹bdd_above t› (h ha))
theorem cInf_le_cInf (_ : bdd_below t) (_ : s.nonempty) (h : s ⊆ t) : Inf t ≤ Inf s :=
le_cInf ‹_› (assume (a) (ha : a ∈ s), cInf_le ‹bdd_below t› (h ha))
lemma is_lub_cSup (ne : s.nonempty) (H : bdd_above s) : is_lub s (Sup s) :=
⟨assume x, le_cSup H, assume x, cSup_le ne⟩
lemma is_lub_csupr [nonempty ι] {f : ι → α} (H : bdd_above (range f)) :
is_lub (range f) (⨆ i, f i) :=
is_lub_cSup (range_nonempty f) H
lemma is_lub_csupr_set {f : β → α} {s : set β} (H : bdd_above (f '' s)) (Hne : s.nonempty) :
is_lub (f '' s) (⨆ i : s, f i) :=
by { rw ← Sup_image', exact is_lub_cSup (Hne.image _) H }
lemma is_glb_cInf (ne : s.nonempty) (H : bdd_below s) : is_glb s (Inf s) :=
⟨assume x, cInf_le H, assume x, le_cInf ne⟩
lemma is_glb_cinfi [nonempty ι] {f : ι → α} (H : bdd_below (range f)) :
is_glb (range f) (⨅ i, f i) :=
is_glb_cInf (range_nonempty f) H
lemma is_glb_cinfi_set {f : β → α} {s : set β} (H : bdd_below (f '' s)) (Hne : s.nonempty) :
is_glb (f '' s) (⨅ i : s, f i) :=
@is_lub_csupr_set (order_dual α) _ _ _ _ H Hne
lemma is_lub.cSup_eq (H : is_lub s a) (ne : s.nonempty) : Sup s = a :=
(is_lub_cSup ne ⟨a, H.1⟩).unique H
lemma is_lub.csupr_eq [nonempty ι] {f : ι → α} (H : is_lub (range f) a) : (⨆ i, f i) = a :=
H.cSup_eq (range_nonempty f)
lemma is_lub.csupr_set_eq {s : set β} {f : β → α} (H : is_lub (f '' s) a) (Hne : s.nonempty) :
(⨆ i : s, f i) = a :=
is_lub.cSup_eq (image_eq_range f s ▸ H) (image_eq_range f s ▸ Hne.image f)
/-- A greatest element of a set is the supremum of this set. -/
lemma is_greatest.cSup_eq (H : is_greatest s a) : Sup s = a :=
H.is_lub.cSup_eq H.nonempty
lemma is_greatest.Sup_mem (H : is_greatest s a) : Sup s ∈ s :=
H.cSup_eq.symm ▸ H.1
lemma is_glb.cInf_eq (H : is_glb s a) (ne : s.nonempty) : Inf s = a :=
(is_glb_cInf ne ⟨a, H.1⟩).unique H
lemma is_glb.cinfi_eq [nonempty ι] {f : ι → α} (H : is_glb (range f) a) : (⨅ i, f i) = a :=
H.cInf_eq (range_nonempty f)
lemma is_glb.cinfi_set_eq {s : set β} {f : β → α} (H : is_glb (f '' s) a) (Hne : s.nonempty) :
(⨅ i : s, f i) = a :=
is_glb.cInf_eq (image_eq_range f s ▸ H) (image_eq_range f s ▸ Hne.image f)
/-- A least element of a set is the infimum of this set. -/
lemma is_least.cInf_eq (H : is_least s a) : Inf s = a :=
H.is_glb.cInf_eq H.nonempty
lemma is_least.Inf_mem (H : is_least s a) : Inf s ∈ s :=
H.cInf_eq.symm ▸ H.1
lemma subset_Icc_cInf_cSup (hb : bdd_below s) (ha : bdd_above s) :
s ⊆ Icc (Inf s) (Sup s) :=
λ x hx, ⟨cInf_le hb hx, le_cSup ha hx⟩
theorem cSup_le_iff (hb : bdd_above s) (ne : s.nonempty) : Sup s ≤ a ↔ (∀b ∈ s, b ≤ a) :=
is_lub_le_iff (is_lub_cSup ne hb)
theorem le_cInf_iff (hb : bdd_below s) (ne : s.nonempty) : a ≤ Inf s ↔ (∀b ∈ s, a ≤ b) :=
le_is_glb_iff (is_glb_cInf ne hb)
lemma cSup_lower_bounds_eq_cInf {s : set α} (h : bdd_below s) (hs : s.nonempty) :
Sup (lower_bounds s) = Inf s :=
(is_lub_cSup h $ hs.mono $ λ x hx y hy, hy hx).unique (is_glb_cInf hs h).is_lub
lemma cInf_upper_bounds_eq_cSup {s : set α} (h : bdd_above s) (hs : s.nonempty) :
Inf (upper_bounds s) = Sup s :=
(is_glb_cInf h $ hs.mono $ λ x hx y hy, hy hx).unique (is_lub_cSup hs h).is_glb
lemma not_mem_of_lt_cInf {x : α} {s : set α} (h : x < Inf s) (hs : bdd_below s) : x ∉ s :=
λ hx, lt_irrefl _ (h.trans_le (cInf_le hs hx))
lemma not_mem_of_cSup_lt {x : α} {s : set α} (h : Sup s < x) (hs : bdd_above s) : x ∉ s :=
@not_mem_of_lt_cInf (order_dual α) _ x s h hs
/--Introduction rule to prove that `b` is the supremum of `s`: it suffices to check that `b`
is larger than all elements of `s`, and that this is not the case of any `w<b`.
See `Sup_eq_of_forall_le_of_forall_lt_exists_gt` for a version in complete lattices. -/
theorem cSup_eq_of_forall_le_of_forall_lt_exists_gt (_ : s.nonempty)
(_ : ∀a∈s, a ≤ b) (H : ∀w, w < b → (∃a∈s, w < a)) : Sup s = b :=
have bdd_above s := ⟨b, by assumption⟩,
have (Sup s < b) ∨ (Sup s = b) := lt_or_eq_of_le (cSup_le ‹_› ‹∀a∈s, a ≤ b›),
have ¬(Sup s < b) :=
assume: Sup s < b,
let ⟨a, _, _⟩ := (H (Sup s) ‹Sup s < b›) in /- a ∈ s, Sup s < a-/
have Sup s < Sup s := lt_of_lt_of_le ‹Sup s < a› (le_cSup ‹bdd_above s› ‹a ∈ s›),
show false, by finish [lt_irrefl (Sup s)],
show Sup s = b, by finish
/--Introduction rule to prove that `b` is the infimum of `s`: it suffices to check that `b`
is smaller than all elements of `s`, and that this is not the case of any `w>b`.
See `Inf_eq_of_forall_ge_of_forall_gt_exists_lt` for a version in complete lattices. -/
theorem cInf_eq_of_forall_ge_of_forall_gt_exists_lt (_ : s.nonempty) (_ : ∀a∈s, b ≤ a)
(H : ∀w, b < w → (∃a∈s, a < w)) : Inf s = b :=
@cSup_eq_of_forall_le_of_forall_lt_exists_gt (order_dual α) _ _ _ ‹_› ‹_› ‹_›
/--b < Sup s when there is an element a in s with b < a, when s is bounded above.
This is essentially an iff, except that the assumptions for the two implications are
slightly different (one needs boundedness above for one direction, nonemptiness and linear
order for the other one), so we formulate separately the two implications, contrary to
the complete_lattice case.-/
lemma lt_cSup_of_lt (_ : bdd_above s) (_ : a ∈ s) (_ : b < a) : b < Sup s :=
lt_of_lt_of_le ‹b < a› (le_cSup ‹bdd_above s› ‹a ∈ s›)
/--Inf s < b when there is an element a in s with a < b, when s is bounded below.
This is essentially an iff, except that the assumptions for the two implications are
slightly different (one needs boundedness below for one direction, nonemptiness and linear
order for the other one), so we formulate separately the two implications, contrary to
the complete_lattice case.-/
lemma cInf_lt_of_lt (_ : bdd_below s) (_ : a ∈ s) (_ : a < b) : Inf s < b :=
@lt_cSup_of_lt (order_dual α) _ _ _ _ ‹_› ‹_› ‹_›
/-- If all elements of a nonempty set `s` are less than or equal to all elements
of a nonempty set `t`, then there exists an element between these sets. -/
lemma exists_between_of_forall_le (sne : s.nonempty) (tne : t.nonempty)
(hst : ∀ (x ∈ s) (y ∈ t), x ≤ y) :
(upper_bounds s ∩ lower_bounds t).nonempty :=
⟨Inf t, λ x hx, le_cInf tne $ hst x hx, λ y hy, cInf_le (sne.mono hst) hy⟩
/--The supremum of a singleton is the element of the singleton-/
@[simp] theorem cSup_singleton (a : α) : Sup {a} = a :=
is_greatest_singleton.cSup_eq
/--The infimum of a singleton is the element of the singleton-/
@[simp] theorem cInf_singleton (a : α) : Inf {a} = a :=
is_least_singleton.cInf_eq
/--If a set is bounded below and above, and nonempty, its infimum is less than or equal to
its supremum.-/
theorem cInf_le_cSup (hb : bdd_below s) (ha : bdd_above s) (ne : s.nonempty) : Inf s ≤ Sup s :=
is_glb_le_is_lub (is_glb_cInf ne hb) (is_lub_cSup ne ha) ne
/--The sup of a union of two sets is the max of the suprema of each subset, under the assumptions
that all sets are bounded above and nonempty.-/
theorem cSup_union (hs : bdd_above s) (sne : s.nonempty) (ht : bdd_above t) (tne : t.nonempty) :
Sup (s ∪ t) = Sup s ⊔ Sup t :=
((is_lub_cSup sne hs).union (is_lub_cSup tne ht)).cSup_eq sne.inl
/--The inf of a union of two sets is the min of the infima of each subset, under the assumptions
that all sets are bounded below and nonempty.-/
theorem cInf_union (hs : bdd_below s) (sne : s.nonempty) (ht : bdd_below t) (tne : t.nonempty) :
Inf (s ∪ t) = Inf s ⊓ Inf t :=
@cSup_union (order_dual α) _ _ _ hs sne ht tne
/--The supremum of an intersection of two sets is bounded by the minimum of the suprema of each
set, if all sets are bounded above and nonempty.-/
theorem cSup_inter_le (_ : bdd_above s) (_ : bdd_above t) (hst : (s ∩ t).nonempty) :
Sup (s ∩ t) ≤ Sup s ⊓ Sup t :=
begin
apply cSup_le hst, simp only [le_inf_iff, and_imp, set.mem_inter_eq], intros b _ _, split,
apply le_cSup ‹bdd_above s› ‹b ∈ s›,
apply le_cSup ‹bdd_above t› ‹b ∈ t›
end
/--The infimum of an intersection of two sets is bounded below by the maximum of the
infima of each set, if all sets are bounded below and nonempty.-/
theorem le_cInf_inter (_ : bdd_below s) (_ : bdd_below t) (hst : (s ∩ t).nonempty) :
Inf s ⊔ Inf t ≤ Inf (s ∩ t) :=
@cSup_inter_le (order_dual α) _ _ _ ‹_› ‹_› hst
/-- The supremum of insert a s is the maximum of a and the supremum of s, if s is
nonempty and bounded above.-/
theorem cSup_insert (hs : bdd_above s) (sne : s.nonempty) : Sup (insert a s) = a ⊔ Sup s :=
((is_lub_cSup sne hs).insert a).cSup_eq (insert_nonempty a s)
/-- The infimum of insert a s is the minimum of a and the infimum of s, if s is
nonempty and bounded below.-/
theorem cInf_insert (hs : bdd_below s) (sne : s.nonempty) : Inf (insert a s) = a ⊓ Inf s :=
@cSup_insert (order_dual α) _ _ _ hs sne
@[simp] lemma cInf_Icc (h : a ≤ b) : Inf (Icc a b) = a :=
(is_glb_Icc h).cInf_eq (nonempty_Icc.2 h)
@[simp] lemma cInf_Ici : Inf (Ici a) = a := is_least_Ici.cInf_eq
@[simp] lemma cInf_Ico (h : a < b) : Inf (Ico a b) = a :=
(is_glb_Ico h).cInf_eq (nonempty_Ico.2 h)
@[simp] lemma cInf_Ioc [densely_ordered α] (h : a < b) : Inf (Ioc a b) = a :=
(is_glb_Ioc h).cInf_eq (nonempty_Ioc.2 h)
@[simp] lemma cInf_Ioi [no_top_order α] [densely_ordered α] : Inf (Ioi a) = a :=
cInf_eq_of_forall_ge_of_forall_gt_exists_lt nonempty_Ioi (λ _, le_of_lt)
(λ w hw, by simpa using exists_between hw)
@[simp] lemma cInf_Ioo [densely_ordered α] (h : a < b) : Inf (Ioo a b) = a :=
(is_glb_Ioo h).cInf_eq (nonempty_Ioo.2 h)
@[simp] lemma cSup_Icc (h : a ≤ b) : Sup (Icc a b) = b :=
(is_lub_Icc h).cSup_eq (nonempty_Icc.2 h)
@[simp] lemma cSup_Ico [densely_ordered α] (h : a < b) : Sup (Ico a b) = b :=
(is_lub_Ico h).cSup_eq (nonempty_Ico.2 h)
@[simp] lemma cSup_Iic : Sup (Iic a) = a := is_greatest_Iic.cSup_eq
@[simp] lemma cSup_Iio [no_bot_order α] [densely_ordered α] : Sup (Iio a) = a :=
cSup_eq_of_forall_le_of_forall_lt_exists_gt nonempty_Iio (λ _, le_of_lt)
(λ w hw, by simpa [and_comm] using exists_between hw)
@[simp] lemma cSup_Ioc (h : a < b) : Sup (Ioc a b) = b :=
(is_lub_Ioc h).cSup_eq (nonempty_Ioc.2 h)
@[simp] lemma cSup_Ioo [densely_ordered α] (h : a < b) : Sup (Ioo a b) = b :=
(is_lub_Ioo h).cSup_eq (nonempty_Ioo.2 h)
/--The indexed supremum of a function is bounded above by a uniform bound-/
lemma csupr_le [nonempty ι] {f : ι → α} {c : α} (H : ∀x, f x ≤ c) : supr f ≤ c :=
cSup_le (range_nonempty f) (by rwa forall_range_iff)
/--The indexed supremum of a function is bounded below by the value taken at one point-/
lemma le_csupr {f : ι → α} (H : bdd_above (range f)) (c : ι) : f c ≤ supr f :=
le_cSup H (mem_range_self _)
lemma le_csupr_of_le {f : ι → α} (H : bdd_above (range f)) (c : ι) (h : a ≤ f c) : a ≤ supr f :=
le_trans h (le_csupr H c)
/--The indexed supremum of two functions are comparable if the functions are pointwise comparable-/
lemma csupr_le_csupr {f g : ι → α} (B : bdd_above (range g)) (H : ∀x, f x ≤ g x) :
supr f ≤ supr g :=
begin
casesI is_empty_or_nonempty ι,
{ rw [supr_of_empty', supr_of_empty'] },
{ exact csupr_le (λ x, le_csupr_of_le B x (H x)) },
end
/--The indexed infimum of two functions are comparable if the functions are pointwise comparable-/
lemma cinfi_le_cinfi {f g : ι → α} (B : bdd_below (range f)) (H : ∀x, f x ≤ g x) :
infi f ≤ infi g :=
@csupr_le_csupr (order_dual α) _ _ _ _ B H
/--The indexed minimum of a function is bounded below by a uniform lower bound-/
lemma le_cinfi [nonempty ι] {f : ι → α} {c : α} (H : ∀x, c ≤ f x) : c ≤ infi f :=
@csupr_le (order_dual α) _ _ _ _ _ H
/--The indexed infimum of a function is bounded above by the value taken at one point-/
lemma cinfi_le {f : ι → α} (H : bdd_below (range f)) (c : ι) : infi f ≤ f c :=
@le_csupr (order_dual α) _ _ _ H c
lemma cinfi_le_of_le {f : ι → α} (H : bdd_below (range f)) (c : ι) (h : f c ≤ a) : infi f ≤ a :=
@le_csupr_of_le (order_dual α) _ _ _ _ H c h
@[simp] theorem csupr_const [hι : nonempty ι] {a : α} : (⨆ b:ι, a) = a :=
by rw [supr, range_const, cSup_singleton]
@[simp] theorem cinfi_const [hι : nonempty ι] {a : α} : (⨅ b:ι, a) = a :=
@csupr_const (order_dual α) _ _ _ _
theorem supr_unique [unique ι] {s : ι → α} : (⨆ i, s i) = s (default ι) :=
have ∀ i, s i = s (default ι) := λ i, congr_arg s (unique.eq_default i),
by simp only [this, csupr_const]
theorem infi_unique [unique ι] {s : ι → α} : (⨅ i, s i) = s (default ι) :=
@supr_unique (order_dual α) _ _ _ _
@[simp] theorem supr_unit {f : unit → α} : (⨆ x, f x) = f () :=
by { convert supr_unique, apply_instance }
@[simp] theorem infi_unit {f : unit → α} : (⨅ x, f x) = f () :=
@supr_unit (order_dual α) _ _
@[simp] lemma csupr_pos {p : Prop} {f : p → α} (hp : p) : (⨆ h : p, f h) = f hp :=
by haveI := unique_prop hp; exact supr_unique
@[simp] lemma cinfi_pos {p : Prop} {f : p → α} (hp : p) : (⨅ h : p, f h) = f hp :=
@csupr_pos (order_dual α) _ _ _ hp
lemma csupr_set {s : set β} {f : β → α} : (⨆ x : s, f x) = Sup (f '' s) :=
begin
rw supr,
congr,
ext,
rw [mem_image, mem_range, set_coe.exists],
simp_rw [subtype.coe_mk, exists_prop],
end
lemma cinfi_set {s : set β} {f : β → α} : (⨅ x : s, f x) = Inf (f '' s) :=
@csupr_set (order_dual α) _ _ _ _
/--Introduction rule to prove that `b` is the supremum of `f`: it suffices to check that `b`
is larger than `f i` for all `i`, and that this is not the case of any `w<b`.
See `supr_eq_of_forall_le_of_forall_lt_exists_gt` for a version in complete lattices. -/
theorem csupr_eq_of_forall_le_of_forall_lt_exists_gt [nonempty ι] {f : ι → α} (h₁ : ∀ i, f i ≤ b)
(h₂ : ∀ w, w < b → (∃ i, w < f i)) : (⨆ (i : ι), f i) = b :=
cSup_eq_of_forall_le_of_forall_lt_exists_gt (range_nonempty f) (forall_range_iff.mpr h₁)
(λ w hw, exists_range_iff.mpr $ h₂ w hw)
/--Introduction rule to prove that `b` is the infimum of `f`: it suffices to check that `b`
is smaller than `f i` for all `i`, and that this is not the case of any `w>b`.
See `infi_eq_of_forall_ge_of_forall_gt_exists_lt` for a version in complete lattices. -/
theorem cinfi_eq_of_forall_ge_of_forall_gt_exists_lt [nonempty ι] {f : ι → α} (h₁ : ∀ i, b ≤ f i)
(h₂ : ∀ w, b < w → (∃ i, f i < w)) : (⨅ (i : ι), f i) = b :=
@csupr_eq_of_forall_le_of_forall_lt_exists_gt (order_dual α) _ _ _ _ ‹_› ‹_› ‹_›
/-- Nested intervals lemma: if `f` is a monotone sequence, `g` is an antitone sequence, and
`f n ≤ g n` for all `n`, then `⨆ n, f n` belongs to all the intervals `[f n, g n]`. -/
lemma monotone.csupr_mem_Inter_Icc_of_antitone [semilattice_sup β]
{f g : β → α} (hf : monotone f) (hg : antitone g) (h : f ≤ g) :
(⨆ n, f n) ∈ ⋂ n, Icc (f n) (g n) :=
begin
refine mem_Inter.2 (λ n, _),
haveI : nonempty β := ⟨n⟩,
have : ∀ m, f m ≤ g n := λ m, hf.forall_le_of_antitone hg h m n,
exact ⟨le_csupr ⟨g $ n, forall_range_iff.2 this⟩ _, csupr_le this⟩
end
/-- Nested intervals lemma: if `[f n, g n]` is an antitone sequence of nonempty
closed intervals, then `⨆ n, f n` belongs to all the intervals `[f n, g n]`. -/
lemma csupr_mem_Inter_Icc_of_antitone_Icc [semilattice_sup β]
{f g : β → α} (h : antitone (λ n, Icc (f n) (g n))) (h' : ∀ n, f n ≤ g n) :
(⨆ n, f n) ∈ ⋂ n, Icc (f n) (g n) :=
monotone.csupr_mem_Inter_Icc_of_antitone (λ m n hmn, ((Icc_subset_Icc_iff (h' n)).1 (h hmn)).1)
(λ m n hmn, ((Icc_subset_Icc_iff (h' n)).1 (h hmn)).2) h'
lemma finset.nonempty.sup'_eq_cSup_image {s : finset β} (hs : s.nonempty) (f : β → α) :
s.sup' hs f = Sup (f '' s) :=
eq_of_forall_ge_iff $ λ a,
by simp [cSup_le_iff (s.finite_to_set.image f).bdd_above (hs.to_set.image f)]
lemma finset.nonempty.sup'_id_eq_cSup {s : finset α} (hs : s.nonempty) :
s.sup' hs id = Sup s :=
by rw [hs.sup'_eq_cSup_image, image_id]
end conditionally_complete_lattice
instance pi.conditionally_complete_lattice {ι : Type*} {α : Π i : ι, Type*}
[Π i, conditionally_complete_lattice (α i)] :
conditionally_complete_lattice (Π i, α i) :=
{ le_cSup := λ s f ⟨g, hg⟩ hf i, le_cSup ⟨g i, set.forall_range_iff.2 $ λ ⟨f', hf'⟩, hg hf' i⟩
⟨⟨f, hf⟩, rfl⟩,
cSup_le := λ s f hs hf i, cSup_le (by haveI := hs.to_subtype; apply range_nonempty) $
λ b ⟨⟨g, hg⟩, hb⟩, hb ▸ hf hg i,
cInf_le := λ s f ⟨g, hg⟩ hf i, cInf_le ⟨g i, set.forall_range_iff.2 $ λ ⟨f', hf'⟩, hg hf' i⟩
⟨⟨f, hf⟩, rfl⟩,
le_cInf := λ s f hs hf i, le_cInf (by haveI := hs.to_subtype; apply range_nonempty) $
λ b ⟨⟨g, hg⟩, hb⟩, hb ▸ hf hg i,
.. pi.lattice, .. pi.has_Sup, .. pi.has_Inf }
section conditionally_complete_linear_order
variables [conditionally_complete_linear_order α] {s t : set α} {a b : α}
lemma finset.nonempty.cSup_eq_max' {s : finset α} (h : s.nonempty) : Sup ↑s = s.max' h :=
eq_of_forall_ge_iff $ λ a, (cSup_le_iff s.bdd_above h.to_set).trans (s.max'_le_iff h).symm
lemma finset.nonempty.cInf_eq_min' {s : finset α} (h : s.nonempty) : Inf ↑s = s.min' h :=
@finset.nonempty.cSup_eq_max' (order_dual α) _ s h
lemma finset.nonempty.cSup_mem {s : finset α} (h : s.nonempty) : Sup (s : set α) ∈ s :=
by { rw h.cSup_eq_max', exact s.max'_mem _ }
lemma finset.nonempty.cInf_mem {s : finset α} (h : s.nonempty) : Inf (s : set α) ∈ s :=
@finset.nonempty.cSup_mem (order_dual α) _ _ h
lemma set.nonempty.cSup_mem (h : s.nonempty) (hs : finite s) : Sup s ∈ s :=
by { lift s to finset α using hs, exact finset.nonempty.cSup_mem h }
lemma set.nonempty.cInf_mem (h : s.nonempty) (hs : finite s) : Inf s ∈ s :=
@set.nonempty.cSup_mem (order_dual α) _ _ h hs
lemma set.finite.cSup_lt_iff (hs : finite s) (h : s.nonempty) : Sup s < a ↔ ∀ x ∈ s, x < a :=
⟨λ h x hx, (le_cSup hs.bdd_above hx).trans_lt h, λ H, H _ $ h.cSup_mem hs⟩
lemma set.finite.lt_cInf_iff (hs : finite s) (h : s.nonempty) : a < Inf s ↔ ∀ x ∈ s, a < x :=
@set.finite.cSup_lt_iff (order_dual α) _ _ _ hs h
/-- When b < Sup s, there is an element a in s with b < a, if s is nonempty and the order is
a linear order. -/
lemma exists_lt_of_lt_cSup (hs : s.nonempty) (hb : b < Sup s) : ∃a∈s, b < a :=
begin
classical, contrapose! hb,
exact cSup_le hs hb
end
/--
Indexed version of the above lemma `exists_lt_of_lt_cSup`.
When `b < supr f`, there is an element `i` such that `b < f i`.
-/
lemma exists_lt_of_lt_csupr [nonempty ι] {f : ι → α} (h : b < supr f) :
∃i, b < f i :=
let ⟨_, ⟨i, rfl⟩, h⟩ := exists_lt_of_lt_cSup (range_nonempty f) h in ⟨i, h⟩
/--When Inf s < b, there is an element a in s with a < b, if s is nonempty and the order is
a linear order.-/
lemma exists_lt_of_cInf_lt (hs : s.nonempty) (hb : Inf s < b) : ∃a∈s, a < b :=
@exists_lt_of_lt_cSup (order_dual α) _ _ _ hs hb
/--
Indexed version of the above lemma `exists_lt_of_cInf_lt`
When `infi f < a`, there is an element `i` such that `f i < a`.
-/
lemma exists_lt_of_cinfi_lt [nonempty ι] {f : ι → α} (h : infi f < a) :
(∃i, f i < a) :=
@exists_lt_of_lt_csupr (order_dual α) _ _ _ _ _ h
/--Introduction rule to prove that b is the supremum of s: it suffices to check that
1) b is an upper bound
2) every other upper bound b' satisfies b ≤ b'.-/
theorem cSup_eq_of_is_forall_le_of_forall_le_imp_ge (_ : s.nonempty)
(h_is_ub : ∀ a ∈ s, a ≤ b) (h_b_le_ub : ∀ub, (∀ a ∈ s, a ≤ ub) → (b ≤ ub)) : Sup s = b :=
le_antisymm
(show Sup s ≤ b, from cSup_le ‹s.nonempty› h_is_ub)
(show b ≤ Sup s, from h_b_le_ub _ $ assume a, le_cSup ⟨b, h_is_ub⟩)
open function
variables [is_well_order α (<)]
lemma Inf_eq_argmin_on (hs : s.nonempty) : Inf s = argmin_on id (@is_well_order.wf α (<) _) s hs :=
is_least.cInf_eq ⟨argmin_on_mem _ _ _ _, λ a ha, argmin_on_le id _ _ ha⟩
lemma is_least_Inf (hs : s.nonempty) : is_least s (Inf s) :=
by { rw Inf_eq_argmin_on hs, exact ⟨argmin_on_mem _ _ _ _, λ a ha, argmin_on_le id _ _ ha⟩ }
lemma le_cInf_iff' (hs : s.nonempty) : b ≤ Inf s ↔ b ∈ lower_bounds s :=
le_is_glb_iff (is_least_Inf hs).is_glb
lemma Inf_mem (hs : s.nonempty) : Inf s ∈ s := (is_least_Inf hs).1
end conditionally_complete_linear_order
/-!
### Lemmas about a conditionally complete linear order with bottom element
In this case we have `Sup ∅ = ⊥`, so we can drop some `nonempty`/`set.nonempty` assumptions.
-/
section conditionally_complete_linear_order_bot
variables [conditionally_complete_linear_order_bot α]
lemma cSup_empty : (Sup ∅ : α) = ⊥ :=
conditionally_complete_linear_order_bot.cSup_empty
lemma csupr_of_empty [is_empty ι] (f : ι → α) : (⨆ i, f i) = ⊥ :=
by rw [supr_of_empty', cSup_empty]
@[simp] lemma csupr_false (f : false → α) : (⨆ i, f i) = ⊥ := csupr_of_empty f
lemma is_lub_cSup' {s : set α} (hs : bdd_above s) : is_lub s (Sup s) :=
begin
rcases eq_empty_or_nonempty s with (rfl|hne),
{ simp only [cSup_empty, is_lub_empty] },
{ exact is_lub_cSup hne hs }
end
lemma cSup_le_iff' {s : set α} (hs : bdd_above s) {a : α} : Sup s ≤ a ↔ ∀ x ∈ s, x ≤ a :=
is_lub_le_iff (is_lub_cSup' hs)
lemma cSup_le' {s : set α} {a : α} (h : a ∈ upper_bounds s) : Sup s ≤ a :=
(cSup_le_iff' ⟨a, h⟩).2 h
lemma exists_lt_of_lt_cSup' {s : set α} {a : α} (h : a < Sup s) : ∃ b ∈ s, a < b :=
by { contrapose! h, exact cSup_le' h }
lemma csupr_le_iff' {f : ι → α} (h : bdd_above (range f)) {a : α} :
(⨆ i, f i) ≤ a ↔ ∀ i, f i ≤ a :=
(cSup_le_iff' h).trans forall_range_iff
lemma csupr_le' {f : ι → α} {a : α} (h : ∀ i, f i ≤ a) : (⨆ i, f i) ≤ a :=
cSup_le' $ forall_range_iff.2 h
lemma exists_lt_of_lt_csupr' {f : ι → α} {a : α} (h : a < ⨆ i, f i) : ∃ i, a < f i :=
by { contrapose! h, exact csupr_le' h }
end conditionally_complete_linear_order_bot
namespace with_top
open_locale classical
variables [conditionally_complete_linear_order_bot α]
/-- The Sup of a non-empty set is its least upper bound for a conditionally
complete lattice with a top. -/
lemma is_lub_Sup' {β : Type*} [conditionally_complete_lattice β]
{s : set (with_top β)} (hs : s.nonempty) : is_lub s (Sup s) :=
begin
split,
{ show ite _ _ _ ∈ _,
split_ifs,
{ intros _ _, exact le_top },
{ rintro (⟨⟩|a) ha,
{ contradiction },
apply some_le_some.2,
exact le_cSup h_1 ha },
{ intros _ _, exact le_top } },
{ show ite _ _ _ ∈ _,
split_ifs,
{ rintro (⟨⟩|a) ha,
{ exact _root_.le_refl _ },
{ exact false.elim (not_top_le_coe a (ha h)) } },
{ rintro (⟨⟩|b) hb,
{ exact le_top },
refine some_le_some.2 (cSup_le _ _),
{ rcases hs with ⟨⟨⟩|b, hb⟩,
{ exact absurd hb h },
{ exact ⟨b, hb⟩ } },
{ intros a ha, exact some_le_some.1 (hb ha) } },
{ rintro (⟨⟩|b) hb,
{ exact _root_.le_refl _ },
{ exfalso, apply h_1, use b, intros a ha, exact some_le_some.1 (hb ha) } } }
end
lemma is_lub_Sup (s : set (with_top α)) : is_lub s (Sup s) :=
begin
cases s.eq_empty_or_nonempty with hs hs,
{ rw hs,
show is_lub ∅ (ite _ _ _),
split_ifs,
{ cases h },
{ rw [preimage_empty, cSup_empty], exact is_lub_empty },
{ exfalso, apply h_1, use ⊥, rintro a ⟨⟩ } },
exact is_lub_Sup' hs,
end
/-- The Inf of a bounded-below set is its greatest lower bound for a conditionally
complete lattice with a top. -/
lemma is_glb_Inf' {β : Type*} [conditionally_complete_lattice β]
{s : set (with_top β)} (hs : bdd_below s) : is_glb s (Inf s) :=
begin
split,
{ show ite _ _ _ ∈ _,
split_ifs,
{ intros a ha, exact top_le_iff.2 (set.mem_singleton_iff.1 (h ha)) },
{ rintro (⟨⟩|a) ha,
{ exact le_top },
refine some_le_some.2 (cInf_le _ ha),
rcases hs with ⟨⟨⟩|b, hb⟩,
{ exfalso,
apply h,
intros c hc,
rw [mem_singleton_iff, ←top_le_iff],
exact hb hc },
use b,
intros c hc,
exact some_le_some.1 (hb hc) } },
{ show ite _ _ _ ∈ _,
split_ifs,
{ intros _ _, exact le_top },
{ rintro (⟨⟩|a) ha,
{ exfalso, apply h, intros b hb, exact set.mem_singleton_iff.2 (top_le_iff.1 (ha hb)) },
{ refine some_le_some.2 (le_cInf _ _),
{ classical, contrapose! h,
rintros (⟨⟩|a) ha,
{ exact mem_singleton ⊤ },
{ exact (h ⟨a, ha⟩).elim }},
{ intros b hb,
rw ←some_le_some,
exact ha hb } } } }
end
lemma is_glb_Inf (s : set (with_top α)) : is_glb s (Inf s) :=
begin
by_cases hs : bdd_below s,
{ exact is_glb_Inf' hs },
{ exfalso, apply hs, use ⊥, intros _ _, exact bot_le },
end
noncomputable instance : complete_linear_order (with_top α) :=
{ Sup := Sup, le_Sup := assume s, (is_lub_Sup s).1, Sup_le := assume s, (is_lub_Sup s).2,
Inf := Inf, le_Inf := assume s, (is_glb_Inf s).2, Inf_le := assume s, (is_glb_Inf s).1,
.. with_top.linear_order, ..with_top.lattice, ..with_top.order_top, ..with_top.order_bot }
lemma coe_Sup {s : set α} (hb : bdd_above s) : (↑(Sup s) : with_top α) = (⨆a∈s, ↑a) :=
begin
cases s.eq_empty_or_nonempty with hs hs,
{ rw [hs, cSup_empty], simp only [set.mem_empty_eq, supr_bot, supr_false], refl },
apply le_antisymm,
{ refine (coe_le_iff.2 $ assume b hb, cSup_le hs $ assume a has, coe_le_coe.1 $ hb ▸ _),
exact (le_supr_of_le a $ le_supr_of_le has $ _root_.le_refl _) },
{ exact (supr_le $ assume a, supr_le $ assume ha, coe_le_coe.2 $ le_cSup hb ha) }
end
lemma coe_Inf {s : set α} (hs : s.nonempty) : (↑(Inf s) : with_top α) = (⨅a∈s, ↑a) :=
let ⟨x, hx⟩ := hs in
have (⨅a∈s, ↑a : with_top α) ≤ x, from infi_le_of_le x $ infi_le_of_le hx $ _root_.le_refl _,
let ⟨r, r_eq, hr⟩ := le_coe_iff.1 this in
le_antisymm
(le_infi $ assume a, le_infi $ assume ha, coe_le_coe.2 $ cInf_le (order_bot.bdd_below s) ha)
begin
refine (r_eq.symm ▸ coe_le_coe.2 $ le_cInf hs $ assume a has, coe_le_coe.1 $ _),
refine (r_eq ▸ infi_le_of_le a _),
exact (infi_le_of_le has $ _root_.le_refl _),
end
end with_top
namespace monotone
variables [preorder α] [conditionally_complete_lattice β] {f : α → β} (h_mono : monotone f)
/-! A monotone function into a conditionally complete lattice preserves the ordering properties of
`Sup` and `Inf`. -/
lemma le_cSup_image {s : set α} {c : α} (hcs : c ∈ s) (h_bdd : bdd_above s) :
f c ≤ Sup (f '' s) :=
le_cSup (map_bdd_above h_mono h_bdd) (mem_image_of_mem f hcs)
lemma cSup_image_le {s : set α} (hs : s.nonempty) {B : α} (hB: B ∈ upper_bounds s) :
Sup (f '' s) ≤ f B :=
cSup_le (nonempty.image f hs) (h_mono.mem_upper_bounds_image hB)
lemma cInf_image_le {s : set α} {c : α} (hcs : c ∈ s) (h_bdd : bdd_below s) :
Inf (f '' s) ≤ f c :=
@le_cSup_image (order_dual α) (order_dual β) _ _ _ (λ x y hxy, h_mono hxy) _ _ hcs h_bdd
lemma le_cInf_image {s : set α} (hs : s.nonempty) {B : α} (hB: B ∈ lower_bounds s) :
f B ≤ Inf (f '' s) :=
@cSup_image_le (order_dual α) (order_dual β) _ _ _ (λ x y hxy, h_mono hxy) _ hs _ hB
end monotone
namespace galois_connection
variables {γ : Type*} [conditionally_complete_lattice α] [conditionally_complete_lattice β]
[nonempty ι] {l : α → β} {u : β → α}
lemma l_cSup (gc : galois_connection l u) {s : set α} (hne : s.nonempty)
(hbdd : bdd_above s) :
l (Sup s) = ⨆ x : s, l x :=
eq.symm $ is_lub.csupr_set_eq (gc.is_lub_l_image $ is_lub_cSup hne hbdd) hne
lemma l_cSup' (gc : galois_connection l u) {s : set α} (hne : s.nonempty) (hbdd : bdd_above s) :
l (Sup s) = Sup (l '' s) :=
by rw [gc.l_cSup hne hbdd, csupr_set]
lemma l_csupr (gc : galois_connection l u) {f : ι → α}
(hf : bdd_above (range f)) :
l (⨆ i, f i) = ⨆ i, l (f i) :=
by rw [supr, gc.l_cSup (range_nonempty _) hf, supr_range']
lemma l_csupr_set (gc : galois_connection l u) {s : set γ} {f : γ → α}
(hf : bdd_above (f '' s)) (hne : s.nonempty) :
l (⨆ i : s, f i) = ⨆ i : s, l (f i) :=
by { haveI := hne.to_subtype, rw image_eq_range at hf, exact gc.l_csupr hf }
lemma u_cInf (gc : galois_connection l u) {s : set β} (hne : s.nonempty)
(hbdd : bdd_below s) :
u (Inf s) = ⨅ x : s, u x :=
gc.dual.l_cSup hne hbdd
lemma u_cInf' (gc : galois_connection l u) {s : set β} (hne : s.nonempty) (hbdd : bdd_below s) :
u (Inf s) = Inf (u '' s) :=
gc.dual.l_cSup' hne hbdd
lemma u_cinfi (gc : galois_connection l u) {f : ι → β}
(hf : bdd_below (range f)) :
u (⨅ i, f i) = ⨅ i, u (f i) :=
gc.dual.l_csupr hf
lemma u_cinfi_set (gc : galois_connection l u) {s : set γ} {f : γ → β}
(hf : bdd_below (f '' s)) (hne : s.nonempty) :
u (⨅ i : s, f i) = ⨅ i : s, u (f i) :=
gc.dual.l_csupr_set hf hne
end galois_connection
namespace order_iso
variables {γ : Type*} [conditionally_complete_lattice α] [conditionally_complete_lattice β]
[nonempty ι]
lemma map_cSup (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_above s) :
e (Sup s) = ⨆ x : s, e x :=
e.to_galois_connection.l_cSup hne hbdd
lemma map_cSup' (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_above s) :
e (Sup s) = Sup (e '' s) :=
e.to_galois_connection.l_cSup' hne hbdd
lemma map_csupr (e : α ≃o β) {f : ι → α} (hf : bdd_above (range f)) :
e (⨆ i, f i) = ⨆ i, e (f i) :=
e.to_galois_connection.l_csupr hf
lemma map_csupr_set (e : α ≃o β) {s : set γ} {f : γ → α}
(hf : bdd_above (f '' s)) (hne : s.nonempty) :
e (⨆ i : s, f i) = ⨆ i : s, e (f i) :=
e.to_galois_connection.l_csupr_set hf hne
lemma map_cInf (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_below s) :
e (Inf s) = ⨅ x : s, e x :=
e.dual.map_cSup hne hbdd
lemma map_cInf' (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_below s) :
e (Inf s) = Inf (e '' s) :=
e.dual.map_cSup' hne hbdd
lemma map_cinfi (e : α ≃o β) {f : ι → α} (hf : bdd_below (range f)) :
e (⨅ i, f i) = ⨅ i, e (f i) :=
e.dual.map_csupr hf
lemma map_cinfi_set (e : α ≃o β) {s : set γ} {f : γ → α}
(hf : bdd_below (f '' s)) (hne : s.nonempty) :
e (⨅ i : s, f i) = ⨅ i : s, e (f i) :=
e.dual.map_csupr_set hf hne
end order_iso
/-!
### Relation between `Sup` / `Inf` and `finset.sup'` / `finset.inf'`
Like the `Sup` of a `conditionally_complete_lattice`, `finset.sup'` also requires the set to be
non-empty. As a result, we can translate between the two.
-/
namespace finset
lemma sup'_eq_cSup_image [conditionally_complete_lattice β] (s : finset α) (H) (f : α → β) :
s.sup' H f = Sup (f '' s) :=
begin
apply le_antisymm,
{ refine (finset.sup'_le _ _ $ λ a ha, _),
refine le_cSup ⟨s.sup' H f, _⟩ ⟨a, ha, rfl⟩,
rintros i ⟨j, hj, rfl⟩,
exact finset.le_sup' _ hj },
{ apply cSup_le ((coe_nonempty.mpr H).image _),
rintros _ ⟨a, ha, rfl⟩,
exact finset.le_sup' _ ha, }
end
lemma inf'_eq_cInf_image [conditionally_complete_lattice β] (s : finset α) (H) (f : α → β) :
s.inf' H f = Inf (f '' s) :=
@sup'_eq_cSup_image _ (order_dual β) _ _ _ _
lemma sup'_id_eq_cSup [conditionally_complete_lattice α] (s : finset α) (H) :
s.sup' H id = Sup s :=
by rw [sup'_eq_cSup_image s H, set.image_id]
lemma inf'_id_eq_cInf [conditionally_complete_lattice α] (s : finset α) (H) :
s.inf' H id = Inf s :=
@sup'_id_eq_cSup (order_dual α) _ _ _
end finset
section with_top_bot
/-!
### Complete lattice structure on `with_top (with_bot α)`
If `α` is a `conditionally_complete_lattice`, then we show that `with_top α` and `with_bot α`
also inherit the structure of conditionally complete lattices. Furthermore, we show
that `with_top (with_bot α)` naturally inherits the structure of a complete lattice. Note that
for α a conditionally complete lattice, `Sup` and `Inf` both return junk values
for sets which are empty or unbounded. The extension of `Sup` to `with_top α` fixes
the unboundedness problem and the extension to `with_bot α` fixes the problem with
the empty set.
This result can be used to show that the extended reals [-∞, ∞] are a complete lattice.
-/
open_locale classical
/-- Adding a top element to a conditionally complete lattice
gives a conditionally complete lattice -/
noncomputable instance with_top.conditionally_complete_lattice
{α : Type*} [conditionally_complete_lattice α] :
conditionally_complete_lattice (with_top α) :=
{ le_cSup := λ S a hS haS, (with_top.is_lub_Sup' ⟨a, haS⟩).1 haS,
cSup_le := λ S a hS haS, (with_top.is_lub_Sup' hS).2 haS,
cInf_le := λ S a hS haS, (with_top.is_glb_Inf' hS).1 haS,
le_cInf := λ S a hS haS, (with_top.is_glb_Inf' ⟨a, haS⟩).2 haS,
..with_top.lattice,
..with_top.has_Sup,
..with_top.has_Inf }
/-- Adding a bottom element to a conditionally complete lattice
gives a conditionally complete lattice -/
noncomputable instance with_bot.conditionally_complete_lattice
{α : Type*} [conditionally_complete_lattice α] :
conditionally_complete_lattice (with_bot α) :=
{ le_cSup := (@with_top.conditionally_complete_lattice (order_dual α) _).cInf_le,
cSup_le := (@with_top.conditionally_complete_lattice (order_dual α) _).le_cInf,
cInf_le := (@with_top.conditionally_complete_lattice (order_dual α) _).le_cSup,
le_cInf := (@with_top.conditionally_complete_lattice (order_dual α) _).cSup_le,
..with_bot.lattice,
..with_bot.has_Sup,
..with_bot.has_Inf }
/-- Adding a bottom and a top to a conditionally complete lattice gives a bounded lattice-/
noncomputable instance with_top.with_bot.bounded_lattice {α : Type*}
[conditionally_complete_lattice α] : bounded_lattice (with_top (with_bot α)) :=
{ ..with_top.order_bot,
..with_top.order_top,
..conditionally_complete_lattice.to_lattice _ }
noncomputable instance with_top.with_bot.complete_lattice {α : Type*}
[conditionally_complete_lattice α] : complete_lattice (with_top (with_bot α)) :=
{ le_Sup := λ S a haS, (with_top.is_lub_Sup' ⟨a, haS⟩).1 haS,
Sup_le := λ S a ha,
begin
cases S.eq_empty_or_nonempty with h,
{ show ite _ _ _ ≤ a,
split_ifs,
{ rw h at h_1, cases h_1 },
{ convert bot_le, convert with_bot.cSup_empty, rw h, refl },
{ exfalso, apply h_2, use ⊥, rw h, rintro b ⟨⟩ } },
{ refine (with_top.is_lub_Sup' h).2 ha }
end,
Inf_le := λ S a haS,
show ite _ _ _ ≤ a,
begin
split_ifs,
{ cases a with a, exact _root_.le_refl _,
cases (h haS); tauto },
{ cases a,
{ exact le_top },
{ apply with_top.some_le_some.2, refine cInf_le _ haS, use ⊥, intros b hb, exact bot_le } }
end,
le_Inf := λ S a haS, (with_top.is_glb_Inf' ⟨a, haS⟩).2 haS,
..with_top.has_Inf,
..with_top.has_Sup,
..with_top.with_bot.bounded_lattice }
noncomputable instance with_top.with_bot.complete_linear_order {α : Type*}
[conditionally_complete_linear_order α] : complete_linear_order (with_top (with_bot α)) :=
{ .. with_top.with_bot.complete_lattice,
.. with_top.linear_order }
end with_top_bot
section subtype
variables (s : set α)
/-! ### Subtypes of conditionally complete linear orders
In this section we give conditions on a subset of a conditionally complete linear order, to ensure
that the subtype is itself conditionally complete.
We check that an `ord_connected` set satisfies these conditions.
TODO There are several possible variants; the `conditionally_complete_linear_order` could be changed
to `conditionally_complete_linear_order_bot` or `complete_linear_order`.
-/
open_locale classical
section has_Sup
variables [has_Sup α]
/-- `has_Sup` structure on a nonempty subset `s` of an object with `has_Sup`. This definition is
non-canonical (it uses `default s`); it should be used only as here, as an auxiliary instance in the
construction of the `conditionally_complete_linear_order` structure. -/
noncomputable def subset_has_Sup [inhabited s] : has_Sup s := {Sup := λ t,
if ht : Sup (coe '' t : set α) ∈ s then ⟨Sup (coe '' t : set α), ht⟩ else default s}
local attribute [instance] subset_has_Sup
@[simp] lemma subset_Sup_def [inhabited s] :
@Sup s _ = λ t,
if ht : Sup (coe '' t : set α) ∈ s then ⟨Sup (coe '' t : set α), ht⟩ else default s :=
rfl
lemma subset_Sup_of_within [inhabited s] {t : set s} (h : Sup (coe '' t : set α) ∈ s) :
Sup (coe '' t : set α) = (@Sup s _ t : α) :=
by simp [dif_pos h]
end has_Sup
section has_Inf
variables [has_Inf α]
/-- `has_Inf` structure on a nonempty subset `s` of an object with `has_Inf`. This definition is
non-canonical (it uses `default s`); it should be used only as here, as an auxiliary instance in the
construction of the `conditionally_complete_linear_order` structure. -/
noncomputable def subset_has_Inf [inhabited s] : has_Inf s := {Inf := λ t,
if ht : Inf (coe '' t : set α) ∈ s then ⟨Inf (coe '' t : set α), ht⟩ else default s}
local attribute [instance] subset_has_Inf
@[simp] lemma subset_Inf_def [inhabited s] :
@Inf s _ = λ t,
if ht : Inf (coe '' t : set α) ∈ s then ⟨Inf (coe '' t : set α), ht⟩ else default s :=
rfl
lemma subset_Inf_of_within [inhabited s] {t : set s} (h : Inf (coe '' t : set α) ∈ s) :
Inf (coe '' t : set α) = (@Inf s _ t : α) :=
by simp [dif_pos h]
end has_Inf
variables [conditionally_complete_linear_order α]
local attribute [instance] subset_has_Sup
local attribute [instance] subset_has_Inf
/-- For a nonempty subset of a conditionally complete linear order to be a conditionally complete
linear order, it suffices that it contain the `Sup` of all its nonempty bounded-above subsets, and
the `Inf` of all its nonempty bounded-below subsets.
See note [reducible non-instances]. -/
@[reducible]
noncomputable def subset_conditionally_complete_linear_order [inhabited s]
(h_Sup : ∀ {t : set s} (ht : t.nonempty) (h_bdd : bdd_above t), Sup (coe '' t : set α) ∈ s)
(h_Inf : ∀ {t : set s} (ht : t.nonempty) (h_bdd : bdd_below t), Inf (coe '' t : set α) ∈ s) :
conditionally_complete_linear_order s :=
{ le_cSup := begin
rintros t c h_bdd hct,
-- The following would be a more natural way to finish, but gives a "deep recursion" error:
-- simpa [subset_Sup_of_within (h_Sup t)] using
-- (strict_mono_coe s).monotone.le_cSup_image hct h_bdd,
have := (subtype.mono_coe s).le_cSup_image hct h_bdd,
rwa subset_Sup_of_within s (h_Sup ⟨c, hct⟩ h_bdd) at this,
end,
cSup_le := begin
rintros t B ht hB,
have := (subtype.mono_coe s).cSup_image_le ht hB,
rwa subset_Sup_of_within s (h_Sup ht ⟨B, hB⟩) at this,
end,
le_cInf := begin
intros t B ht hB,
have := (subtype.mono_coe s).le_cInf_image ht hB,
rwa subset_Inf_of_within s (h_Inf ht ⟨B, hB⟩) at this,
end,
cInf_le := begin
rintros t c h_bdd hct,
have := (subtype.mono_coe s).cInf_image_le hct h_bdd,
rwa subset_Inf_of_within s (h_Inf ⟨c, hct⟩ h_bdd) at this,
end,
..subset_has_Sup s,
..subset_has_Inf s,
..distrib_lattice.to_lattice s,
..(infer_instance : linear_order s) }
section ord_connected
/-- The `Sup` function on a nonempty `ord_connected` set `s` in a conditionally complete linear
order takes values within `s`, for all nonempty bounded-above subsets of `s`. -/
lemma Sup_within_of_ord_connected
{s : set α} [hs : ord_connected s] ⦃t : set s⦄ (ht : t.nonempty) (h_bdd : bdd_above t) :
Sup (coe '' t : set α) ∈ s :=
begin
obtain ⟨c, hct⟩ : ∃ c, c ∈ t := ht,
obtain ⟨B, hB⟩ : ∃ B, B ∈ upper_bounds t := h_bdd,
refine hs.out c.2 B.2 ⟨_, _⟩,
{ exact (subtype.mono_coe s).le_cSup_image hct ⟨B, hB⟩ },
{ exact (subtype.mono_coe s).cSup_image_le ⟨c, hct⟩ hB },
end
/-- The `Inf` function on a nonempty `ord_connected` set `s` in a conditionally complete linear
order takes values within `s`, for all nonempty bounded-below subsets of `s`. -/
lemma Inf_within_of_ord_connected
{s : set α} [hs : ord_connected s] ⦃t : set s⦄ (ht : t.nonempty) (h_bdd : bdd_below t) :
Inf (coe '' t : set α) ∈ s :=
begin
obtain ⟨c, hct⟩ : ∃ c, c ∈ t := ht,
obtain ⟨B, hB⟩ : ∃ B, B ∈ lower_bounds t := h_bdd,
refine hs.out B.2 c.2 ⟨_, _⟩,
{ exact (subtype.mono_coe s).le_cInf_image ⟨c, hct⟩ hB },
{ exact (subtype.mono_coe s).cInf_image_le hct ⟨B, hB⟩ },
end
/-- A nonempty `ord_connected` set in a conditionally complete linear order is naturally a
conditionally complete linear order. -/
noncomputable instance ord_connected_subset_conditionally_complete_linear_order
[inhabited s] [ord_connected s] :
conditionally_complete_linear_order s :=
subset_conditionally_complete_linear_order s Sup_within_of_ord_connected Inf_within_of_ord_connected
end ord_connected
end subtype
|
1d7905286dd5c982a11ee23c55c5e9946302d483 | a8c03ed21a1bd6fc45901943b79dd6574ea3f0c2 | /subsumption.lean | c759206132761705b20789a134165051386a07bd | [] | no_license | gebner/resolution.lean | 716c355fbb5204e5c4d0c5a7f3f3cc825892a2bf | c6fafe06fba1cfad73db68f2aa474b29fe892a2b | refs/heads/master | 1,601,111,444,528 | 1,475,256,701,000 | 1,475,256,701,000 | 67,711,151 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,246 | lean | import clause prover_state
open tactic monad
private meta def try_subsume_core : list clause.literal → list clause.literal → tactic unit
| [] _ := skip
| small large := first $ do
i ← small↣zip_with_index, j ← large↣zip_with_index,
return $ do
unify_lit i.1 j.1,
try_subsume_core (small↣remove i.2) (large↣remove j.2)
-- FIXME: this is incorrect if a quantifier is unused
meta def try_subsume (small large : clause) : tactic unit := do
small_open ← clause.open_metan small (clause.num_quants small),
large_open ← clause.open_constn large (clause.num_quants large),
guard $ small↣num_lits ≤ large↣num_lits,
try_subsume_core small_open↣1↣get_lits large_open↣1↣get_lits
meta def does_subsume (small large : clause) : tactic bool :=
(try_subsume small large >> return tt) <|> return ff
meta def does_subsume_with_assertions (small large : clause) : resolution_prover bool := do
small_ass ← collect_ass_hyps small,
large_ass ← collect_ass_hyps large,
if small_ass↣subset_of large_ass then do
does_subsume small large
else
return ff
meta def any_tt {m : Type → Type} [monad m] (active : rb_map name active_cls) (pred : active_cls → m bool) : m bool :=
active↣fold (return ff) $ λk a cont, do
v ← pred a, if v then return tt else cont
meta def any_tt_list {m : Type → Type} [monad m] {A} (pred : A → m bool) : list A → m bool
| [] := return ff
| (x::xs) := do v ← pred x, if v then return tt else any_tt_list xs
meta def forward_subsumption : inference := take given, do
active ← get_active,
any_tt active (λa,
if a↣id = given↣id then return ff else do
ss : bool ← ↑(does_subsume a↣c given↣c),
if ss then do
remove_redundant given↣id [a],
return tt
else
return ff),
return ()
meta def forward_subsumption_pre : resolution_prover unit := preprocessing_rule $ λnew, do
active ← get_active, filterM (λn, do
n_ass ← collect_ass_hyps n,
do ss ← any_tt active (λa,
if a↣assertions↣subset_of n_ass then do
↑(does_subsume a↣c n)
else
return ff),
return (bnot ss)) new
meta def subsumption_interreduction : list clause → resolution_prover (list clause)
| (c::cs) := do
c_subsumed_by_cs ← any_tt_list (λd, does_subsume_with_assertions d c) cs,
if c_subsumed_by_cs then
subsumption_interreduction cs
else do
cs_not_subsumed_by_c ← filterM (λd, liftM bnot (does_subsume_with_assertions c d)) cs,
cs' ← subsumption_interreduction cs_not_subsumed_by_c,
return (c::cs')
| [] := return []
meta def subsumption_interreduction_pre : resolution_prover unit :=
preprocessing_rule $ λnew,
let new' := list.sort_on clause.num_lits new in
subsumption_interreduction new'
meta def keys_where_tt {m} [monad m] (active : rb_map name active_cls) (pred : active_cls → m bool) : m (list name) :=
@rb_map.fold _ _ (m (list name)) active (return []) $ λk a cont, do
v ← pred a, rest ← cont, return $ if v then k::rest else rest
meta def backward_subsumption : inference := λgiven, do
active ← get_active,
ss ← keys_where_tt active (λa, does_subsume given↣c a↣c),
sequence' $ do id ← ss, guard (id ≠ given↣id), [remove_redundant id [given]]
|
d17ac14b1c97179e93cfcca551c7c208c97859b5 | 649957717d58c43b5d8d200da34bf374293fe739 | /src/order/filter/basic.lean | 41961c2b0d8e16b3c95178a18fdcbbd98876c5e0 | [
"Apache-2.0"
] | permissive | Vtec234/mathlib | b50c7b21edea438df7497e5ed6a45f61527f0370 | fb1848bbbfce46152f58e219dc0712f3289d2b20 | refs/heads/master | 1,592,463,095,113 | 1,562,737,749,000 | 1,562,737,749,000 | 196,202,858 | 0 | 0 | Apache-2.0 | 1,562,762,338,000 | 1,562,762,337,000 | null | UTF-8 | Lean | false | false | 82,015 | 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, Jeremy Avigad
Theory of filters on sets.
-/
import order.galois_connection order.zorn
import data.set.finite
open lattice set
universes u v w x y
local attribute [instance] classical.prop_decidable
namespace lattice
variables {α : Type u} {ι : Sort v}
def complete_lattice.copy (c : complete_lattice α)
(le : α → α → Prop) (eq_le : le = @complete_lattice.le α c)
(top : α) (eq_top : top = @complete_lattice.top α c)
(bot : α) (eq_bot : bot = @complete_lattice.bot α c)
(sup : α → α → α) (eq_sup : sup = @complete_lattice.sup α c)
(inf : α → α → α) (eq_inf : inf = @complete_lattice.inf α c)
(Sup : set α → α) (eq_Sup : Sup = @complete_lattice.Sup α c)
(Inf : set α → α) (eq_Inf : Inf = @complete_lattice.Inf α c) :
complete_lattice α :=
begin
refine { le := le, top := top, bot := bot, sup := sup, inf := inf, Sup := Sup, Inf := Inf, ..};
subst_vars,
exact @complete_lattice.le_refl α c,
exact @complete_lattice.le_trans α c,
exact @complete_lattice.le_antisymm α c,
exact @complete_lattice.le_sup_left α c,
exact @complete_lattice.le_sup_right α c,
exact @complete_lattice.sup_le α c,
exact @complete_lattice.inf_le_left α c,
exact @complete_lattice.inf_le_right α c,
exact @complete_lattice.le_inf α c,
exact @complete_lattice.le_top α c,
exact @complete_lattice.bot_le α c,
exact @complete_lattice.le_Sup α c,
exact @complete_lattice.Sup_le α c,
exact @complete_lattice.Inf_le α c,
exact @complete_lattice.le_Inf α c
end
end lattice
open set lattice
section order
variables {α : Type u} (r : α → α → Prop)
local infix `≼` : 50 := r
lemma directed_on_Union {r} {ι : Sort v} {f : ι → set α} (hd : directed (⊆) f)
(h : ∀x, directed_on r (f x)) : directed_on r (⋃x, f x) :=
by simp only [directed_on, exists_prop, mem_Union, exists_imp_distrib]; exact
assume a₁ b₁ fb₁ a₂ b₂ fb₂,
let ⟨z, zb₁, zb₂⟩ := hd b₁ b₂,
⟨x, xf, xa₁, xa₂⟩ := h z a₁ (zb₁ fb₁) a₂ (zb₂ fb₂) in
⟨x, ⟨z, xf⟩, xa₁, xa₂⟩
end order
theorem directed_of_chain {α β r} [is_refl β r] {f : α → β} {c : set α}
(h : zorn.chain (f ⁻¹'o r) c) :
directed r (λx:{a:α // a ∈ c}, f (x.val)) :=
assume ⟨a, ha⟩ ⟨b, hb⟩, classical.by_cases
(assume : a = b, by simp only [this, exists_prop, and_self, subtype.exists];
exact ⟨b, hb, refl _⟩)
(assume : a ≠ b, (h a ha b hb this).elim
(λ h : r (f a) (f b), ⟨⟨b, hb⟩, h, refl _⟩)
(λ h : r (f b) (f a), ⟨⟨a, ha⟩, refl _, h⟩))
structure filter (α : Type*) :=
(sets : set (set α))
(univ_sets : set.univ ∈ sets)
(sets_of_superset {x y} : x ∈ sets → x ⊆ y → y ∈ sets)
(inter_sets {x y} : x ∈ sets → y ∈ sets → x ∩ y ∈ sets)
/-- If `F` is a filter on `α`, and `U` a subset of `α` then we can write `U ∈ F` as on paper. -/
@[reducible]
instance {α : Type*}: has_mem (set α) (filter α) := ⟨λ U F, U ∈ F.sets⟩
namespace filter
variables {α : Type u} {f g : filter α} {s t : set α}
lemma filter_eq : ∀{f g : filter α}, f.sets = g.sets → f = g
| ⟨a, _, _, _⟩ ⟨._, _, _, _⟩ rfl := rfl
lemma filter_eq_iff : f = g ↔ f.sets = g.sets :=
⟨congr_arg _, filter_eq⟩
protected lemma ext_iff : f = g ↔ ∀ s, s ∈ f ↔ s ∈ g :=
by rw [filter_eq_iff, ext_iff]
@[extensionality]
protected lemma ext : (∀ s, s ∈ f ↔ s ∈ g) → f = g :=
filter.ext_iff.2
lemma univ_mem_sets : univ ∈ f :=
f.univ_sets
lemma mem_sets_of_superset : ∀{x y : set α}, x ∈ f → x ⊆ y → y ∈ f :=
f.sets_of_superset
lemma inter_mem_sets : ∀{s t}, s ∈ f → t ∈ f → s ∩ t ∈ f :=
f.inter_sets
lemma univ_mem_sets' (h : ∀ a, a ∈ s) : s ∈ f :=
mem_sets_of_superset univ_mem_sets (assume x _, h x)
lemma mp_sets (hs : s ∈ f) (h : {x | x ∈ s → x ∈ t} ∈ f) : t ∈ f :=
mem_sets_of_superset (inter_mem_sets hs h) $ assume x ⟨h₁, h₂⟩, h₂ h₁
lemma congr_sets (h : {x | x ∈ s ↔ x ∈ t} ∈ f) : s ∈ f ↔ t ∈ f :=
⟨λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mp)),
λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mpr))⟩
lemma Inter_mem_sets {β : Type v} {s : β → set α} {is : set β} (hf : finite is) :
(∀i∈is, s i ∈ f) → (⋂i∈is, s i) ∈ f :=
finite.induction_on hf
(assume hs, by simp only [univ_mem_sets, mem_empty_eq, Inter_neg, Inter_univ, not_false_iff])
(assume i is _ hf hi hs,
have h₁ : s i ∈ f, from hs i (by simp),
have h₂ : (⋂x∈is, s x) ∈ f, from hi $ assume a ha, hs _ $ by simp only [ha, mem_insert_iff, or_true],
by simp [inter_mem_sets h₁ h₂])
lemma exists_sets_subset_iff : (∃t ∈ f, t ⊆ s) ↔ s ∈ f :=
⟨assume ⟨t, ht, ts⟩, mem_sets_of_superset ht ts, assume hs, ⟨s, hs, subset.refl _⟩⟩
lemma monotone_mem_sets {f : filter α} : monotone (λs, s ∈ f) :=
assume s t hst h, mem_sets_of_superset h hst
end filter
namespace tactic.interactive
open tactic interactive
/-- `filter_upwards [h1, ⋯, hn]` replaces a goal of the form `s ∈ f`
and terms `h1 : t1 ∈ f, ⋯, hn : tn ∈ f` with `∀x, x ∈ t1 → ⋯ → x ∈ tn → x ∈ s`.
`filter_upwards [h1, ⋯, hn] e` is a short form for `{ filter_upwards [h1, ⋯, hn], exact e }`.
-/
meta def filter_upwards
(s : parse types.pexpr_list)
(e' : parse $ optional types.texpr) : tactic unit :=
do
s.reverse.mmap (λ e, eapplyc `filter.mp_sets >> eapply e),
eapplyc `filter.univ_mem_sets',
match e' with
| some e := interactive.exact e
| none := skip
end
end tactic.interactive
namespace filter
variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x}
section principal
/-- The principal filter of `s` is the collection of all supersets of `s`. -/
def principal (s : set α) : filter α :=
{ sets := {t | s ⊆ t},
univ_sets := subset_univ s,
sets_of_superset := assume x y hx hy, subset.trans hx hy,
inter_sets := assume x y, subset_inter }
instance : inhabited (filter α) :=
⟨principal ∅⟩
@[simp] lemma mem_principal_sets {s t : set α} : s ∈ principal t ↔ t ⊆ s := iff.rfl
lemma mem_principal_self (s : set α) : s ∈ principal s := subset.refl _
end principal
section join
/-- The join of a filter of filters is defined by the relation `s ∈ join f ↔ {t | s ∈ t} ∈ f`. -/
def join (f : filter (filter α)) : filter α :=
{ sets := {s | {t : filter α | s ∈ t} ∈ f},
univ_sets := by simp only [univ_mem_sets, mem_set_of_eq]; exact univ_mem_sets,
sets_of_superset := assume x y hx xy,
mem_sets_of_superset hx $ assume f h, mem_sets_of_superset h xy,
inter_sets := assume x y hx hy,
mem_sets_of_superset (inter_mem_sets hx hy) $ assume f ⟨h₁, h₂⟩, inter_mem_sets h₁ h₂ }
@[simp] lemma mem_join_sets {s : set α} {f : filter (filter α)} :
s ∈ join f ↔ {t | s ∈ filter.sets t} ∈ f := iff.rfl
end join
section lattice
instance : partial_order (filter α) :=
{ le := λf g, g.sets ⊆ f.sets,
le_antisymm := assume a b h₁ h₂, filter_eq $ subset.antisymm h₂ h₁,
le_refl := assume a, subset.refl _,
le_trans := assume a b c h₁ h₂, subset.trans h₂ h₁ }
theorem le_def {f g : filter α} : f ≤ g ↔ ∀ x ∈ g, x ∈ f := iff.rfl
/-- `generate_sets g s`: `s` is in the filter closure of `g`. -/
inductive generate_sets (g : set (set α)) : set α → Prop
| basic {s : set α} : s ∈ g → generate_sets s
| univ {} : generate_sets univ
| superset {s t : set α} : generate_sets s → s ⊆ t → generate_sets t
| inter {s t : set α} : generate_sets s → generate_sets t → generate_sets (s ∩ t)
/-- `generate g` is the smallest filter containing the sets `g`. -/
def generate (g : set (set α)) : filter α :=
{ sets := {s | generate_sets g s},
univ_sets := generate_sets.univ,
sets_of_superset := assume x y, generate_sets.superset,
inter_sets := assume s t, generate_sets.inter }
lemma sets_iff_generate {s : set (set α)} {f : filter α} : f ≤ filter.generate s ↔ s ⊆ f.sets :=
iff.intro
(assume h u hu, h $ generate_sets.basic $ hu)
(assume h u hu, hu.rec_on h univ_mem_sets
(assume x y _ hxy hx, mem_sets_of_superset hx hxy)
(assume x y _ _ hx hy, inter_mem_sets hx hy))
protected def mk_of_closure (s : set (set α)) (hs : (generate s).sets = s) : filter α :=
{ sets := s,
univ_sets := hs ▸ (univ_mem_sets : univ ∈ generate s),
sets_of_superset := assume x y, hs ▸ (mem_sets_of_superset : x ∈ generate s → x ⊆ y → y ∈ generate s),
inter_sets := assume x y, hs ▸ (inter_mem_sets : x ∈ generate s → y ∈ generate s → x ∩ y ∈ generate s) }
lemma mk_of_closure_sets {s : set (set α)} {hs : (generate s).sets = s} :
filter.mk_of_closure s hs = generate s :=
filter.ext $ assume u,
show u ∈ (filter.mk_of_closure s hs).sets ↔ u ∈ (generate s).sets, from hs.symm ▸ iff.refl _
/- Galois insertion from sets of sets into a filters. -/
def gi_generate (α : Type*) :
@galois_insertion (set (set α)) (order_dual (filter α)) _ _ filter.generate filter.sets :=
{ gc := assume s f, sets_iff_generate,
le_l_u := assume f u, generate_sets.basic,
choice := λs hs, filter.mk_of_closure s (le_antisymm hs $ sets_iff_generate.1 $ le_refl _),
choice_eq := assume s hs, mk_of_closure_sets }
/-- The infimum of filters is the filter generated by intersections
of elements of the two filters. -/
instance : has_inf (filter α) := ⟨λf g : filter α,
{ sets := {s | ∃ (a ∈ f) (b ∈ g), a ∩ b ⊆ s },
univ_sets := ⟨_, univ_mem_sets, _, univ_mem_sets, inter_subset_left _ _⟩,
sets_of_superset := assume x y ⟨a, ha, b, hb, h⟩ xy, ⟨a, ha, b, hb, subset.trans h xy⟩,
inter_sets := assume x y ⟨a, ha, b, hb, hx⟩ ⟨c, hc, d, hd, hy⟩,
⟨_, inter_mem_sets ha hc, _, inter_mem_sets hb hd,
calc a ∩ c ∩ (b ∩ d) = (a ∩ b) ∩ (c ∩ d) : by ac_refl
... ⊆ x ∩ y : inter_subset_inter hx hy⟩ }⟩
@[simp] lemma mem_inf_sets {f g : filter α} {s : set α} :
s ∈ f ⊓ g ↔ ∃t₁∈f.sets, ∃t₂∈g.sets, t₁ ∩ t₂ ⊆ s := iff.rfl
lemma mem_inf_sets_of_left {f g : filter α} {s : set α} (h : s ∈ f) : s ∈ f ⊓ g :=
⟨s, h, univ, univ_mem_sets, inter_subset_left _ _⟩
lemma mem_inf_sets_of_right {f g : filter α} {s : set α} (h : s ∈ g) : s ∈ f ⊓ g :=
⟨univ, univ_mem_sets, s, h, inter_subset_right _ _⟩
lemma inter_mem_inf_sets {α : Type u} {f g : filter α} {s t : set α}
(hs : s ∈ f) (ht : t ∈ g) : s ∩ t ∈ f ⊓ g :=
inter_mem_sets (mem_inf_sets_of_left hs) (mem_inf_sets_of_right ht)
instance : has_top (filter α) :=
⟨{ sets := {s | ∀x, x ∈ s},
univ_sets := assume x, mem_univ x,
sets_of_superset := assume x y hx hxy a, hxy (hx a),
inter_sets := assume x y hx hy a, mem_inter (hx _) (hy _) }⟩
lemma mem_top_sets_iff_forall {s : set α} : s ∈ (⊤ : filter α) ↔ (∀x, x ∈ s) :=
iff.refl _
@[simp] lemma mem_top_sets {s : set α} : s ∈ (⊤ : filter α) ↔ s = univ :=
by rw [mem_top_sets_iff_forall, eq_univ_iff_forall]
section complete_lattice
/- We lift the complete lattice along the Galois connection `generate` / `sets`. Unfortunately,
we want to have different definitional equalities for the lattice operations. So we define them
upfront and change the lattice operations for the complete lattice instance. -/
private def original_complete_lattice : complete_lattice (filter α) :=
@order_dual.lattice.complete_lattice _ (gi_generate α).lift_complete_lattice
local attribute [instance] original_complete_lattice
instance : complete_lattice (filter α) := original_complete_lattice.copy
/- le -/ filter.partial_order.le rfl
/- top -/ (filter.lattice.has_top).1
(top_unique $ assume s hs, by have := univ_mem_sets ; finish)
/- bot -/ _ rfl
/- sup -/ _ rfl
/- inf -/ (filter.lattice.has_inf).1
begin
ext f g : 2,
exact le_antisymm
(le_inf (assume s, mem_inf_sets_of_left) (assume s, mem_inf_sets_of_right))
(assume s ⟨a, ha, b, hb, hs⟩, show s ∈ complete_lattice.inf f g, from
mem_sets_of_superset (inter_mem_sets
(@inf_le_left (filter α) _ _ _ _ ha)
(@inf_le_right (filter α) _ _ _ _ hb)) hs)
end
/- Sup -/ (join ∘ principal) (by ext s x; exact (@mem_bInter_iff _ _ s filter.sets x).symm)
/- Inf -/ _ rfl
end complete_lattice
lemma bot_sets_eq : (⊥ : filter α).sets = univ := rfl
lemma sup_sets_eq {f g : filter α} : (f ⊔ g).sets = f.sets ∩ g.sets :=
(gi_generate α).gc.u_inf
lemma Sup_sets_eq {s : set (filter α)} : (Sup s).sets = (⋂f∈s, (f:filter α).sets) :=
(gi_generate α).gc.u_Inf
lemma supr_sets_eq {f : ι → filter α} : (supr f).sets = (⋂i, (f i).sets) :=
(gi_generate α).gc.u_infi
lemma generate_empty : filter.generate ∅ = (⊤ : filter α) :=
(gi_generate α).gc.l_bot
lemma generate_univ : filter.generate univ = (⊥ : filter α) :=
mk_of_closure_sets.symm
lemma generate_union {s t : set (set α)} :
filter.generate (s ∪ t) = filter.generate s ⊓ filter.generate t :=
(gi_generate α).gc.l_sup
lemma generate_Union {s : ι → set (set α)} :
filter.generate (⋃ i, s i) = (⨅ i, filter.generate (s i)) :=
(gi_generate α).gc.l_supr
@[simp] lemma mem_bot_sets {s : set α} : s ∈ (⊥ : filter α) :=
trivial
@[simp] lemma mem_sup_sets {f g : filter α} {s : set α} :
s ∈ f ⊔ g ↔ s ∈ f ∧ s ∈ g :=
iff.rfl
@[simp] lemma mem_Sup_sets {x : set α} {s : set (filter α)} :
x ∈ Sup s ↔ (∀f∈s, x ∈ (f:filter α)) :=
iff.rfl
@[simp] lemma mem_supr_sets {x : set α} {f : ι → filter α} :
x ∈ supr f ↔ (∀i, x ∈ f i) :=
by simp only [supr_sets_eq, iff_self, mem_Inter]
@[simp] lemma le_principal_iff {s : set α} {f : filter α} : f ≤ principal s ↔ s ∈ f :=
show (∀{t}, s ⊆ t → t ∈ f) ↔ s ∈ f,
from ⟨assume h, h (subset.refl s), assume hs t ht, mem_sets_of_superset hs ht⟩
lemma principal_mono {s t : set α} : principal s ≤ principal t ↔ s ⊆ t :=
by simp only [le_principal_iff, iff_self, mem_principal_sets]
lemma monotone_principal : monotone (principal : set α → filter α) :=
by simp only [monotone, principal_mono]; exact assume a b h, h
@[simp] lemma principal_eq_iff_eq {s t : set α} : principal s = principal t ↔ s = t :=
by simp only [le_antisymm_iff, le_principal_iff, mem_principal_sets]; refl
@[simp] lemma join_principal_eq_Sup {s : set (filter α)} : join (principal s) = Sup s := rfl
/- lattice equations -/
lemma empty_in_sets_eq_bot {f : filter α} : ∅ ∈ f ↔ f = ⊥ :=
⟨assume h, bot_unique $ assume s _, mem_sets_of_superset h (empty_subset s),
assume : f = ⊥, this.symm ▸ mem_bot_sets⟩
lemma inhabited_of_mem_sets {f : filter α} {s : set α} (hf : f ≠ ⊥) (hs : s ∈ f) :
∃x, x ∈ s :=
have ∅ ∉ f.sets, from assume h, hf $ empty_in_sets_eq_bot.mp h,
have s ≠ ∅, from assume h, this (h ▸ hs),
exists_mem_of_ne_empty this
lemma filter_eq_bot_of_not_nonempty {f : filter α} (ne : ¬ nonempty α) : f = ⊥ :=
empty_in_sets_eq_bot.mp $ univ_mem_sets' $ assume x, false.elim (ne ⟨x⟩)
lemma forall_sets_neq_empty_iff_neq_bot {f : filter α} :
(∀ (s : set α), s ∈ f → s ≠ ∅) ↔ f ≠ ⊥ :=
by
simp only [(@empty_in_sets_eq_bot α f).symm, ne.def];
exact ⟨assume h hs, h _ hs rfl, assume h s hs eq, h $ eq ▸ hs⟩
lemma mem_sets_of_neq_bot {f : filter α} {s : set α} (h : f ⊓ principal (-s) = ⊥) : s ∈ f :=
have ∅ ∈ f ⊓ principal (- s), from h.symm ▸ mem_bot_sets,
let ⟨s₁, hs₁, s₂, (hs₂ : -s ⊆ s₂), (hs : s₁ ∩ s₂ ⊆ ∅)⟩ := this in
by filter_upwards [hs₁] assume a ha, classical.by_contradiction $ assume ha', hs ⟨ha, hs₂ ha'⟩
lemma infi_sets_eq {f : ι → filter α} (h : directed (≥) f) (ne : nonempty ι) :
(infi f).sets = (⋃ i, (f i).sets) :=
let ⟨i⟩ := ne, u := { filter .
sets := (⋃ i, (f i).sets),
univ_sets := by simp only [mem_Union]; exact ⟨i, univ_mem_sets⟩,
sets_of_superset := by simp only [mem_Union, exists_imp_distrib];
intros x y i hx hxy; exact ⟨i, mem_sets_of_superset hx hxy⟩,
inter_sets :=
begin
simp only [mem_Union, exists_imp_distrib],
assume x y a hx b hy,
rcases h a b with ⟨c, ha, hb⟩,
exact ⟨c, inter_mem_sets (ha hx) (hb hy)⟩
end } in
subset.antisymm
(show u ≤ infi f, from le_infi $ assume i, le_supr (λi, (f i).sets) i)
(Union_subset $ assume i, infi_le f i)
lemma mem_infi {f : ι → filter α} (h : directed (≥) f) (ne : nonempty ι) (s) :
s ∈ infi f ↔ s ∈ ⋃ i, (f i).sets :=
show s ∈ (infi f).sets ↔ s ∈ ⋃ i, (f i).sets, by rw infi_sets_eq h ne
lemma infi_sets_eq' {f : β → filter α} {s : set β}
(h : directed_on (f ⁻¹'o (≥)) s) (ne : ∃i, i ∈ s) :
(⨅ i∈s, f i).sets = (⋃ i ∈ s, (f i).sets) :=
let ⟨i, hi⟩ := ne in
calc (⨅ i ∈ s, f i).sets = (⨅ t : {t // t ∈ s}, (f t.val)).sets : by rw [infi_subtype]; refl
... = (⨆ t : {t // t ∈ s}, (f t.val).sets) : infi_sets_eq
(assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end)
⟨⟨i, hi⟩⟩
... = (⨆ t ∈ {t | t ∈ s}, (f t).sets) : by rw [supr_subtype]; refl
lemma infi_sets_eq_finite (f : ι → filter α) :
(⨅i, f i).sets = (⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets) :=
begin
rw [infi_eq_infi_finset, infi_sets_eq],
exact (directed_of_sup $ λs₁ s₂ hs, infi_le_infi $ λi, infi_le_infi_const $ λh, hs h),
apply_instance
end
lemma mem_infi_finite {f : ι → filter α} (s) :
s ∈ infi f ↔ s ∈ ⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets :=
show s ∈ (infi f).sets ↔ s ∈ ⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets,
by rw infi_sets_eq_finite
@[simp] lemma sup_join {f₁ f₂ : filter (filter α)} : (join f₁ ⊔ join f₂) = join (f₁ ⊔ f₂) :=
filter_eq $ set.ext $ assume x,
by simp only [supr_sets_eq, join, mem_sup_sets, iff_self, mem_set_of_eq]
@[simp] lemma supr_join {ι : Sort w} {f : ι → filter (filter α)} :
(⨆x, join (f x)) = join (⨆x, f x) :=
filter_eq $ set.ext $ assume x,
by simp only [supr_sets_eq, join, iff_self, mem_Inter, mem_set_of_eq]
instance : bounded_distrib_lattice (filter α) :=
{ le_sup_inf :=
begin
assume x y z s,
simp only [and_assoc, mem_inf_sets, mem_sup_sets, exists_prop, exists_imp_distrib, and_imp],
intros hs t₁ ht₁ t₂ ht₂ hts,
exact ⟨s ∪ t₁,
x.sets_of_superset hs $ subset_union_left _ _,
y.sets_of_superset ht₁ $ subset_union_right _ _,
s ∪ t₂,
x.sets_of_superset hs $ subset_union_left _ _,
z.sets_of_superset ht₂ $ subset_union_right _ _,
subset.trans (@le_sup_inf (set α) _ _ _ _) (union_subset (subset.refl _) hts)⟩
end,
..filter.lattice.complete_lattice }
/- the complementary version with ⨆i, f ⊓ g i does not hold! -/
lemma infi_sup_eq {f : filter α} {g : ι → filter α} : (⨅ x, f ⊔ g x) = f ⊔ infi g :=
begin
refine le_antisymm _ (le_infi $ assume i, sup_le_sup (le_refl f) $ infi_le _ _),
rintros t ⟨h₁, h₂⟩,
rw [infi_sets_eq_finite] at h₂,
simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at h₂,
rcases h₂ with ⟨s, hs⟩,
suffices : (⨅i, f ⊔ g i) ≤ f ⊔ s.inf (λi, g i.down), { exact this ⟨h₁, hs⟩ },
refine finset.induction_on s _ _,
{ exact le_sup_right_of_le le_top },
{ rintros ⟨i⟩ s his ih,
rw [finset.inf_insert, sup_inf_left],
exact le_inf (infi_le _ _) ih }
end
lemma mem_infi_sets_finset {s : finset α} {f : α → filter β} :
∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⋂a∈s, p a) ⊆ t) :=
show ∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⨅a∈s, p a) ≤ t),
begin
simp only [(finset.inf_eq_infi _ _).symm],
refine finset.induction_on s _ _,
{ simp only [finset.not_mem_empty, false_implies_iff, finset.inf_empty, top_le_iff,
imp_true_iff, mem_top_sets, true_and, exists_const],
intros; refl },
{ intros a s has ih t,
simp only [ih, finset.forall_mem_insert, finset.inf_insert, mem_inf_sets,
exists_prop, iff_iff_implies_and_implies, exists_imp_distrib, and_imp, and_assoc] {contextual := tt},
split,
{ intros t₁ ht₁ t₂ p hp ht₂ ht,
existsi function.update p a t₁,
have : ∀a'∈s, function.update p a t₁ a' = p a',
from assume a' ha',
have a' ≠ a, from assume h, has $ h ▸ ha',
function.update_noteq this,
have eq : s.inf (λj, function.update p a t₁ j) = s.inf (λj, p j) :=
finset.inf_congr rfl this,
simp only [this, ht₁, hp, function.update_same, true_and, imp_true_iff, eq] {contextual := tt},
exact subset.trans (inter_subset_inter (subset.refl _) ht₂) ht },
assume p hpa hp ht,
exact ⟨p a, hpa, (s.inf p), ⟨⟨p, hp, le_refl _⟩, ht⟩⟩ }
end
/- principal equations -/
@[simp] lemma inf_principal {s t : set α} : principal s ⊓ principal t = principal (s ∩ t) :=
le_antisymm
(by simp; exact ⟨s, subset.refl s, t, subset.refl t, by simp⟩)
(by simp [le_inf_iff, inter_subset_left, inter_subset_right])
@[simp] lemma sup_principal {s t : set α} : principal s ⊔ principal t = principal (s ∪ t) :=
filter_eq $ set.ext $
by simp only [union_subset_iff, union_subset_iff, mem_sup_sets, forall_const, iff_self, mem_principal_sets]
@[simp] lemma supr_principal {ι : Sort w} {s : ι → set α} : (⨆x, principal (s x)) = principal (⋃i, s i) :=
filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, mem_principal_sets, mem_Inter];
exact (@supr_le_iff (set α) _ _ _ _).symm
lemma principal_univ : principal (univ : set α) = ⊤ :=
top_unique $ by simp only [le_principal_iff, mem_top_sets, eq_self_iff_true]
lemma principal_empty : principal (∅ : set α) = ⊥ :=
bot_unique $ assume s _, empty_subset _
@[simp] lemma principal_eq_bot_iff {s : set α} : principal s = ⊥ ↔ s = ∅ :=
⟨assume h, principal_eq_iff_eq.mp $ by simp only [principal_empty, h, eq_self_iff_true],
assume h, by simp only [h, principal_empty, eq_self_iff_true]⟩
lemma inf_principal_eq_bot {f : filter α} {s : set α} (hs : -s ∈ f) : f ⊓ principal s = ⊥ :=
empty_in_sets_eq_bot.mp ⟨_, hs, s, mem_principal_self s, assume x ⟨h₁, h₂⟩, h₁ h₂⟩
theorem mem_inf_principal (f : filter α) (s t : set α) :
s ∈ f ⊓ principal t ↔ { x | x ∈ t → x ∈ s } ∈ f :=
begin
simp only [mem_inf_sets, mem_principal_sets, exists_prop], split,
{ rintros ⟨u, ul, v, tsubv, uvinter⟩,
apply filter.mem_sets_of_superset ul,
intros x xu xt, exact uvinter ⟨xu, tsubv xt⟩ },
intro h, refine ⟨_, h, t, set.subset.refl t, _⟩,
rintros x ⟨hx, xt⟩,
exact hx xt
end
end lattice
section map
/-- The forward map of a filter -/
def map (m : α → β) (f : filter α) : filter β :=
{ sets := preimage m ⁻¹' f.sets,
univ_sets := univ_mem_sets,
sets_of_superset := assume s t hs st, mem_sets_of_superset hs $ preimage_mono st,
inter_sets := assume s t hs ht, inter_mem_sets hs ht }
@[simp] lemma map_principal {s : set α} {f : α → β} :
map f (principal s) = principal (set.image f s) :=
filter_eq $ set.ext $ assume a, image_subset_iff.symm
variables {f : filter α} {m : α → β} {m' : β → γ} {s : set α} {t : set β}
@[simp] lemma mem_map : t ∈ map m f ↔ {x | m x ∈ t} ∈ f := iff.rfl
lemma image_mem_map (hs : s ∈ f) : m '' s ∈ map m f :=
f.sets_of_superset hs $ subset_preimage_image m s
lemma range_mem_map : range m ∈ map m f :=
by rw ←image_univ; exact image_mem_map univ_mem_sets
lemma mem_map_sets_iff : t ∈ map m f ↔ (∃s∈f, m '' s ⊆ t) :=
iff.intro
(assume ht, ⟨set.preimage m t, ht, image_preimage_subset _ _⟩)
(assume ⟨s, hs, ht⟩, mem_sets_of_superset (image_mem_map hs) ht)
@[simp] lemma map_id : filter.map id f = f :=
filter_eq $ rfl
@[simp] lemma map_compose : filter.map m' ∘ filter.map m = filter.map (m' ∘ m) :=
funext $ assume _, filter_eq $ rfl
@[simp] lemma map_map : filter.map m' (filter.map m f) = filter.map (m' ∘ m) f :=
congr_fun (@@filter.map_compose m m') f
end map
section comap
/-- The inverse map of a filter -/
def comap (m : α → β) (f : filter β) : filter α :=
{ sets := { s | ∃t∈ f, m ⁻¹' t ⊆ s },
univ_sets := ⟨univ, univ_mem_sets, by simp only [subset_univ, preimage_univ]⟩,
sets_of_superset := assume a b ⟨a', ha', ma'a⟩ ab,
⟨a', ha', subset.trans ma'a ab⟩,
inter_sets := assume a b ⟨a', ha₁, ha₂⟩ ⟨b', hb₁, hb₂⟩,
⟨a' ∩ b', inter_mem_sets ha₁ hb₁, inter_subset_inter ha₂ hb₂⟩ }
end comap
/-- The cofinite filter is the filter of subsets whose complements are finite. -/
def cofinite : filter α :=
{ sets := {s | finite (- s)},
univ_sets := by simp only [compl_univ, finite_empty, mem_set_of_eq],
sets_of_superset := assume s t (hs : finite (-s)) (st: s ⊆ t),
finite_subset hs $ @lattice.neg_le_neg (set α) _ _ _ st,
inter_sets := assume s t (hs : finite (-s)) (ht : finite (-t)),
by simp only [compl_inter, finite_union, ht, hs, mem_set_of_eq] }
lemma cofinite_ne_bot (hi : set.infinite (@set.univ α)) : @cofinite α ≠ ⊥ :=
forall_sets_neq_empty_iff_neq_bot.mp
$ λ s hs hn, by change set.finite _ at hs;
rw [hn, set.compl_empty] at hs; exact hi hs
/-- The monadic bind operation on filter is defined the usual way in terms of `map` and `join`.
Unfortunately, this `bind` does not result in the expected applicative. See `filter.seq` for the
applicative instance. -/
def bind (f : filter α) (m : α → filter β) : filter β := join (map m f)
/-- The applicative sequentiation operation. This is not induced by the bind operation. -/
def seq (f : filter (α → β)) (g : filter α) : filter β :=
⟨{ s | ∃u∈ f, ∃t∈ g, (∀m∈u, ∀x∈t, (m : α → β) x ∈ s) },
⟨univ, univ_mem_sets, univ, univ_mem_sets, by simp only [forall_prop_of_true, mem_univ, forall_true_iff]⟩,
assume s₀ s₁ ⟨t₀, t₁, h₀, h₁, h⟩ hst, ⟨t₀, t₁, h₀, h₁, assume x hx y hy, hst $ h _ hx _ hy⟩,
assume s₀ s₁ ⟨t₀, ht₀, t₁, ht₁, ht⟩ ⟨u₀, hu₀, u₁, hu₁, hu⟩,
⟨t₀ ∩ u₀, inter_mem_sets ht₀ hu₀, t₁ ∩ u₁, inter_mem_sets ht₁ hu₁,
assume x ⟨hx₀, hx₁⟩ x ⟨hy₀, hy₁⟩, ⟨ht _ hx₀ _ hy₀, hu _ hx₁ _ hy₁⟩⟩⟩
instance : has_pure filter := ⟨λ(α : Type u) x, principal {x}⟩
instance : has_bind filter := ⟨@filter.bind⟩
instance : has_seq filter := ⟨@filter.seq⟩
instance : functor filter := { map := @filter.map }
section
-- this section needs to be before applicative, otherwise the wrong instance will be chosen
protected def monad : monad filter := { map := @filter.map }
local attribute [instance] filter.monad
protected def is_lawful_monad : is_lawful_monad filter :=
{ id_map := assume α f, filter_eq rfl,
pure_bind := assume α β a f, by simp only [bind, Sup_image, image_singleton,
join_principal_eq_Sup, lattice.Sup_singleton, map_principal, eq_self_iff_true],
bind_assoc := assume α β γ f m₁ m₂, filter_eq rfl,
bind_pure_comp_eq_map := assume α β f x, filter_eq $
by simp only [bind, join, map, preimage, principal, set.subset_univ, eq_self_iff_true,
function.comp_app, mem_set_of_eq, singleton_subset_iff] }
end
instance : applicative filter := { map := @filter.map, seq := @filter.seq }
instance : alternative filter :=
{ failure := λα, ⊥,
orelse := λα x y, x ⊔ y }
@[simp] lemma pure_def (x : α) : pure x = principal {x} := rfl
@[simp] lemma mem_pure {a : α} {s : set α} : a ∈ s → s ∈ (pure a : filter α) :=
by simp only [imp_self, pure_def, mem_principal_sets, singleton_subset_iff]; exact id
@[simp] lemma mem_pure_iff {a : α} {s : set α} : s ∈ (pure a : filter α) ↔ a ∈ s :=
by rw [pure_def, mem_principal_sets, set.singleton_subset_iff]
@[simp] lemma map_def {α β} (m : α → β) (f : filter α) : m <$> f = map m f := rfl
@[simp] lemma bind_def {α β} (f : filter α) (m : α → filter β) : f >>= m = bind f m := rfl
/- map and comap equations -/
section map
variables {f f₁ f₂ : filter α} {g g₁ g₂ : filter β} {m : α → β} {m' : β → γ} {s : set α} {t : set β}
@[simp] theorem mem_comap_sets : s ∈ comap m g ↔ ∃t∈ g, m ⁻¹' t ⊆ s := iff.rfl
theorem preimage_mem_comap (ht : t ∈ g) : m ⁻¹' t ∈ comap m g :=
⟨t, ht, subset.refl _⟩
lemma comap_id : comap id f = f :=
le_antisymm (assume s, preimage_mem_comap) (assume s ⟨t, ht, hst⟩, mem_sets_of_superset ht hst)
lemma comap_comap_comp {m : γ → β} {n : β → α} : comap m (comap n f) = comap (n ∘ m) f :=
le_antisymm
(assume c ⟨b, hb, (h : preimage (n ∘ m) b ⊆ c)⟩, ⟨preimage n b, preimage_mem_comap hb, h⟩)
(assume c ⟨b, ⟨a, ha, (h₁ : preimage n a ⊆ b)⟩, (h₂ : preimage m b ⊆ c)⟩,
⟨a, ha, show preimage m (preimage n a) ⊆ c, from subset.trans (preimage_mono h₁) h₂⟩)
@[simp] theorem comap_principal {t : set β} : comap m (principal t) = principal (m ⁻¹' t) :=
filter_eq $ set.ext $ assume s,
⟨assume ⟨u, (hu : t ⊆ u), (b : preimage m u ⊆ s)⟩, subset.trans (preimage_mono hu) b,
assume : preimage m t ⊆ s, ⟨t, subset.refl t, this⟩⟩
lemma map_le_iff_le_comap : map m f ≤ g ↔ f ≤ comap m g :=
⟨assume h s ⟨t, ht, hts⟩, mem_sets_of_superset (h ht) hts, assume h s ht, h ⟨_, ht, subset.refl _⟩⟩
lemma gc_map_comap (m : α → β) : galois_connection (map m) (comap m) :=
assume f g, map_le_iff_le_comap
lemma map_mono (h : f₁ ≤ f₂) : map m f₁ ≤ map m f₂ := (gc_map_comap m).monotone_l h
lemma monotone_map : monotone (map m) | a b := map_mono
lemma comap_mono (h : g₁ ≤ g₂) : comap m g₁ ≤ comap m g₂ := (gc_map_comap m).monotone_u h
lemma monotone_comap : monotone (comap m) | a b := comap_mono
@[simp] lemma map_bot : map m ⊥ = ⊥ := (gc_map_comap m).l_bot
@[simp] lemma map_sup : map m (f₁ ⊔ f₂) = map m f₁ ⊔ map m f₂ := (gc_map_comap m).l_sup
@[simp] lemma map_supr {f : ι → filter α} : map m (⨆i, f i) = (⨆i, map m (f i)) :=
(gc_map_comap m).l_supr
@[simp] lemma comap_top : comap m ⊤ = ⊤ := (gc_map_comap m).u_top
@[simp] lemma comap_inf : comap m (g₁ ⊓ g₂) = comap m g₁ ⊓ comap m g₂ := (gc_map_comap m).u_inf
@[simp] lemma comap_infi {f : ι → filter β} : comap m (⨅i, f i) = (⨅i, comap m (f i)) :=
(gc_map_comap m).u_infi
lemma le_comap_top (f : α → β) (l : filter α) : l ≤ comap f ⊤ :=
by rw [comap_top]; exact le_top
lemma map_comap_le : map m (comap m g) ≤ g := (gc_map_comap m).l_u_le _
lemma le_comap_map : f ≤ comap m (map m f) := (gc_map_comap m).le_u_l _
@[simp] lemma comap_bot : comap m ⊥ = ⊥ :=
bot_unique $ assume s _, ⟨∅, by simp only [mem_bot_sets], by simp only [empty_subset, preimage_empty]⟩
lemma comap_supr {ι} {f : ι → filter β} {m : α → β} :
comap m (supr f) = (⨆i, comap m (f i)) :=
le_antisymm
(assume s hs,
have ∀i, ∃t, t ∈ f i ∧ m ⁻¹' t ⊆ s, by simpa only [mem_comap_sets, exists_prop, mem_supr_sets] using mem_supr_sets.1 hs,
let ⟨t, ht⟩ := classical.axiom_of_choice this in
⟨⋃i, t i, mem_supr_sets.2 $ assume i, (f i).sets_of_superset (ht i).1 (subset_Union _ _),
begin
rw [preimage_Union, Union_subset_iff],
assume i,
exact (ht i).2
end⟩)
(supr_le $ assume i, monotone_comap $ le_supr _ _)
lemma comap_Sup {s : set (filter β)} {m : α → β} : comap m (Sup s) = (⨆f∈s, comap m f) :=
by simp only [Sup_eq_supr, comap_supr, eq_self_iff_true]
lemma comap_sup : comap m (g₁ ⊔ g₂) = comap m g₁ ⊔ comap m g₂ :=
le_antisymm
(assume s ⟨⟨t₁, ht₁, hs₁⟩, ⟨t₂, ht₂, hs₂⟩⟩,
⟨t₁ ∪ t₂,
⟨g₁.sets_of_superset ht₁ (subset_union_left _ _), g₂.sets_of_superset ht₂ (subset_union_right _ _)⟩,
union_subset hs₁ hs₂⟩)
(sup_le (comap_mono le_sup_left) (comap_mono le_sup_right))
lemma map_comap {f : filter β} {m : α → β} (hf : range m ∈ f) : (f.comap m).map m = f :=
le_antisymm
map_comap_le
(assume t' ⟨t, ht, sub⟩, by filter_upwards [ht, hf]; rintros x hxt ⟨y, rfl⟩; exact sub hxt)
lemma comap_map {f : filter α} {m : α → β} (h : ∀ x y, m x = m y → x = y) :
comap m (map m f) = f :=
have ∀s, preimage m (image m s) = s,
from assume s, preimage_image_eq s h,
le_antisymm
(assume s hs, ⟨
image m s,
f.sets_of_superset hs $ by simp only [this, subset.refl],
by simp only [this, subset.refl]⟩)
le_comap_map
lemma le_of_map_le_map_inj' {f g : filter α} {m : α → β} {s : set α}
(hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y)
(h : map m f ≤ map m g) : f ≤ g :=
assume t ht, by filter_upwards [hsf, h $ image_mem_map (inter_mem_sets hsg ht)]
assume a has ⟨b, ⟨hbs, hb⟩, h⟩,
have b = a, from hm _ hbs _ has h,
this ▸ hb
lemma le_of_map_le_map_inj_iff {f g : filter α} {m : α → β} {s : set α}
(hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) :
map m f ≤ map m g ↔ f ≤ g :=
iff.intro (le_of_map_le_map_inj' hsf hsg hm) map_mono
lemma eq_of_map_eq_map_inj' {f g : filter α} {m : α → β} {s : set α}
(hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y)
(h : map m f = map m g) : f = g :=
le_antisymm
(le_of_map_le_map_inj' hsf hsg hm $ le_of_eq h)
(le_of_map_le_map_inj' hsg hsf hm $ le_of_eq h.symm)
lemma map_inj {f g : filter α} {m : α → β} (hm : ∀ x y, m x = m y → x = y) (h : map m f = map m g) :
f = g :=
have comap m (map m f) = comap m (map m g), by rw h,
by rwa [comap_map hm, comap_map hm] at this
theorem le_map_comap_of_surjective' {f : α → β} {l : filter β} {u : set β} (ul : u ∈ l)
(hf : ∀ y ∈ u, ∃ x, f x = y) :
l ≤ map f (comap f l) :=
assume s ⟨t, tl, ht⟩,
have t ∩ u ⊆ s, from
assume x ⟨xt, xu⟩,
exists.elim (hf x xu) $ λ a faeq,
by { rw ←faeq, apply ht, change f a ∈ t, rw faeq, exact xt },
mem_sets_of_superset (inter_mem_sets tl ul) this
theorem map_comap_of_surjective' {f : α → β} {l : filter β} {u : set β} (ul : u ∈ l)
(hf : ∀ y ∈ u, ∃ x, f x = y) :
map f (comap f l) = l :=
le_antisymm map_comap_le (le_map_comap_of_surjective' ul hf)
theorem le_map_comap_of_surjective {f : α → β} (hf : function.surjective f) (l : filter β) :
l ≤ map f (comap f l) :=
le_map_comap_of_surjective' univ_mem_sets (λ y _, hf y)
theorem map_comap_of_surjective {f : α → β} (hf : function.surjective f) (l : filter β) :
map f (comap f l) = l :=
le_antisymm map_comap_le (le_map_comap_of_surjective hf l)
lemma comap_neq_bot {f : filter β} {m : α → β}
(hm : ∀t∈ f, ∃a, m a ∈ t) : comap m f ≠ ⊥ :=
forall_sets_neq_empty_iff_neq_bot.mp $ assume s ⟨t, ht, t_s⟩,
let ⟨a, (ha : a ∈ preimage m t)⟩ := hm t ht in
neq_bot_of_le_neq_bot (ne_empty_of_mem ha) t_s
lemma comap_neq_bot_of_surj {f : filter β} {m : α → β}
(hf : f ≠ ⊥) (hm : ∀b, ∃a, m a = b) : comap m f ≠ ⊥ :=
comap_neq_bot $ assume t ht,
let
⟨b, (hx : b ∈ t)⟩ := inhabited_of_mem_sets hf ht,
⟨a, (ha : m a = b)⟩ := hm b
in ⟨a, ha.symm ▸ hx⟩
@[simp] lemma map_eq_bot_iff : map m f = ⊥ ↔ f = ⊥ :=
⟨by rw [←empty_in_sets_eq_bot, ←empty_in_sets_eq_bot]; exact id,
assume h, by simp only [h, eq_self_iff_true, map_bot]⟩
lemma map_ne_bot (hf : f ≠ ⊥) : map m f ≠ ⊥ :=
assume h, hf $ by rwa [map_eq_bot_iff] at h
lemma sInter_comap_sets (f : α → β) (F : filter β) :
⋂₀(comap f F).sets = ⋂ U ∈ F, f ⁻¹' U :=
begin
ext x,
suffices : (∀ (A : set α) (B : set β), B ∈ F → f ⁻¹' B ⊆ A → x ∈ A) ↔
∀ (B : set β), B ∈ F → f x ∈ B,
by simp only [mem_sInter, mem_Inter, mem_comap_sets, this, and_imp, mem_comap_sets, exists_prop, mem_sInter,
iff_self, mem_Inter, mem_preimage, exists_imp_distrib],
split,
{ intros h U U_in,
simpa only [set.subset.refl, forall_prop_of_true, mem_preimage] using h (f ⁻¹' U) U U_in },
{ intros h V U U_in f_U_V,
exact f_U_V (h U U_in) },
end
end map
lemma map_cong {m₁ m₂ : α → β} {f : filter α} (h : {x | m₁ x = m₂ x} ∈ f) :
map m₁ f = map m₂ f :=
have ∀(m₁ m₂ : α → β) (h : {x | m₁ x = m₂ x} ∈ f), map m₁ f ≤ map m₂ f,
begin
intros m₁ m₂ h s hs,
show {x | m₁ x ∈ s} ∈ f,
filter_upwards [h, hs],
simp only [subset_def, mem_preimage, mem_set_of_eq, forall_true_iff] {contextual := tt}
end,
le_antisymm (this m₁ m₂ h) (this m₂ m₁ $ mem_sets_of_superset h $ assume x, eq.symm)
-- this is a generic rule for monotone functions:
lemma map_infi_le {f : ι → filter α} {m : α → β} :
map m (infi f) ≤ (⨅ i, map m (f i)) :=
le_infi $ assume i, map_mono $ infi_le _ _
lemma map_infi_eq {f : ι → filter α} {m : α → β} (hf : directed (≥) f) (hι : nonempty ι) :
map m (infi f) = (⨅ i, map m (f i)) :=
le_antisymm
map_infi_le
(assume s (hs : preimage m s ∈ infi f),
have ∃i, preimage m s ∈ f i,
by simp only [infi_sets_eq hf hι, mem_Union] at hs; assumption,
let ⟨i, hi⟩ := this in
have (⨅ i, map m (f i)) ≤ principal s, from
infi_le_of_le i $ by simp only [le_principal_iff, mem_map]; assumption,
by simp only [filter.le_principal_iff] at this; assumption)
lemma map_binfi_eq {ι : Type w} {f : ι → filter α} {m : α → β} {p : ι → Prop}
(h : directed_on (f ⁻¹'o (≥)) {x | p x}) (ne : ∃i, p i) :
map m (⨅i (h : p i), f i) = (⨅i (h: p i), map m (f i)) :=
let ⟨i, hi⟩ := ne in
calc map m (⨅i (h : p i), f i) = map m (⨅i:subtype p, f i.val) : by simp only [infi_subtype, eq_self_iff_true]
... = (⨅i:subtype p, map m (f i.val)) : map_infi_eq
(assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end)
⟨⟨i, hi⟩⟩
... = (⨅i (h : p i), map m (f i)) : by simp only [infi_subtype, eq_self_iff_true]
lemma map_inf' {f g : filter α} {m : α → β} {t : set α} (htf : t ∈ f) (htg : t ∈ g)
(h : ∀x∈t, ∀y∈t, m x = m y → x = y) : map m (f ⊓ g) = map m f ⊓ map m g :=
begin
refine le_antisymm
(le_inf (map_mono inf_le_left) (map_mono inf_le_right))
(assume s hs, _),
simp only [map, mem_inf_sets, exists_prop, mem_map, mem_preimage, mem_inf_sets] at hs ⊢,
rcases hs with ⟨t₁, h₁, t₂, h₂, hs⟩,
refine ⟨m '' (t₁ ∩ t), _, m '' (t₂ ∩ t), _, _⟩,
{ filter_upwards [h₁, htf] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ },
{ filter_upwards [h₂, htg] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ },
{ rw [image_inter_on],
{ refine image_subset_iff.2 _,
exact λ x ⟨⟨h₁, _⟩, h₂, _⟩, hs ⟨h₁, h₂⟩ },
{ exact λ x ⟨_, hx⟩ y ⟨_, hy⟩, h x hx y hy } }
end
lemma map_inf {f g : filter α} {m : α → β} (h : ∀ x y, m x = m y → x = y) :
map m (f ⊓ g) = map m f ⊓ map m g :=
map_inf' univ_mem_sets univ_mem_sets (assume x _ y _, h x y)
lemma map_eq_comap_of_inverse {f : filter α} {m : α → β} {n : β → α}
(h₁ : m ∘ n = id) (h₂ : n ∘ m = id) : map m f = comap n f :=
le_antisymm
(assume b ⟨a, ha, (h : preimage n a ⊆ b)⟩, f.sets_of_superset ha $
calc a = preimage (n ∘ m) a : by simp only [h₂, preimage_id, eq_self_iff_true]
... ⊆ preimage m b : preimage_mono h)
(assume b (hb : preimage m b ∈ f),
⟨preimage m b, hb, show preimage (m ∘ n) b ⊆ b, by simp only [h₁]; apply subset.refl⟩)
lemma map_swap_eq_comap_swap {f : filter (α × β)} : prod.swap <$> f = comap prod.swap f :=
map_eq_comap_of_inverse prod.swap_swap_eq prod.swap_swap_eq
lemma le_map {f : filter α} {m : α → β} {g : filter β} (h : ∀s∈ f, m '' s ∈ g) :
g ≤ f.map m :=
assume s hs, mem_sets_of_superset (h _ hs) $ image_preimage_subset _ _
section applicative
@[simp] lemma mem_pure_sets {a : α} {s : set α} :
s ∈ (pure a : filter α) ↔ a ∈ s :=
by simp only [iff_self, pure_def, mem_principal_sets, singleton_subset_iff]
lemma singleton_mem_pure_sets {a : α} : {a} ∈ (pure a : filter α) :=
by simp only [mem_singleton, pure_def, mem_principal_sets, singleton_subset_iff]
@[simp] lemma pure_neq_bot {α : Type u} {a : α} : pure a ≠ (⊥ : filter α) :=
by simp only [pure, has_pure.pure, ne.def, not_false_iff, singleton_ne_empty, principal_eq_bot_iff]
lemma mem_seq_sets_def {f : filter (α → β)} {g : filter α} {s : set β} :
s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, ∀x∈u, ∀y∈t, (x : α → β) y ∈ s) :=
iff.refl _
lemma mem_seq_sets_iff {f : filter (α → β)} {g : filter α} {s : set β} :
s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, set.seq u t ⊆ s) :=
by simp only [mem_seq_sets_def, seq_subset, exists_prop, iff_self]
lemma mem_map_seq_iff {f : filter α} {g : filter β} {m : α → β → γ} {s : set γ} :
s ∈ (f.map m).seq g ↔ (∃t u, t ∈ g ∧ u ∈ f ∧ ∀x∈u, ∀y∈t, m x y ∈ s) :=
iff.intro
(assume ⟨t, ht, s, hs, hts⟩, ⟨s, m ⁻¹' t, hs, ht, assume a, hts _⟩)
(assume ⟨t, s, ht, hs, hts⟩, ⟨m '' s, image_mem_map hs, t, ht, assume f ⟨a, has, eq⟩, eq ▸ hts _ has⟩)
lemma seq_mem_seq_sets {f : filter (α → β)} {g : filter α} {s : set (α → β)} {t : set α}
(hs : s ∈ f) (ht : t ∈ g) : s.seq t ∈ f.seq g :=
⟨s, hs, t, ht, assume f hf a ha, ⟨f, hf, a, ha, rfl⟩⟩
lemma le_seq {f : filter (α → β)} {g : filter α} {h : filter β}
(hh : ∀t ∈ f, ∀u ∈ g, set.seq t u ∈ h) : h ≤ seq f g :=
assume s ⟨t, ht, u, hu, hs⟩, mem_sets_of_superset (hh _ ht _ hu) $
assume b ⟨m, hm, a, ha, eq⟩, eq ▸ hs _ hm _ ha
lemma seq_mono {f₁ f₂ : filter (α → β)} {g₁ g₂ : filter α}
(hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.seq g₁ ≤ f₂.seq g₂ :=
le_seq $ assume s hs t ht, seq_mem_seq_sets (hf hs) (hg ht)
@[simp] lemma pure_seq_eq_map (g : α → β) (f : filter α) : seq (pure g) f = f.map g :=
begin
refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _),
{ rw ← singleton_seq, apply seq_mem_seq_sets _ hs,
simp only [mem_singleton, pure_def, mem_principal_sets, singleton_subset_iff] },
{ rw mem_pure_sets at hs,
refine sets_of_superset (map g f) (image_mem_map ht) _,
rintros b ⟨a, ha, rfl⟩, exact ⟨g, hs, a, ha, rfl⟩ }
end
@[simp] lemma map_pure (f : α → β) (a : α) : map f (pure a) = pure (f a) :=
le_antisymm
(le_principal_iff.2 $ sets_of_superset (map f (pure a)) (image_mem_map singleton_mem_pure_sets) $
by simp only [image_singleton, mem_singleton, singleton_subset_iff])
(le_map $ assume s, begin
simp only [mem_image, pure_def, mem_principal_sets, singleton_subset_iff],
exact assume has, ⟨a, has, rfl⟩
end)
@[simp] lemma seq_pure (f : filter (α → β)) (a : α) : seq f (pure a) = map (λg:α → β, g a) f :=
begin
refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _),
{ rw ← seq_singleton, exact seq_mem_seq_sets hs
(by simp only [mem_singleton, pure_def, mem_principal_sets, singleton_subset_iff]) },
{ rw mem_pure_sets at ht,
refine sets_of_superset (map (λg:α→β, g a) f) (image_mem_map hs) _,
rintros b ⟨g, hg, rfl⟩, exact ⟨g, hg, a, ht, rfl⟩ }
end
@[simp] lemma seq_assoc (x : filter α) (g : filter (α → β)) (h : filter (β → γ)) :
seq h (seq g x) = seq (seq (map (∘) h) g) x :=
begin
refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _),
{ rcases mem_seq_sets_iff.1 hs with ⟨u, hu, v, hv, hs⟩,
rcases mem_map_sets_iff.1 hu with ⟨w, hw, hu⟩,
refine mem_sets_of_superset _
(set.seq_mono (subset.trans (set.seq_mono hu (subset.refl _)) hs) (subset.refl _)),
rw ← set.seq_seq,
exact seq_mem_seq_sets hw (seq_mem_seq_sets hv ht) },
{ rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩,
refine mem_sets_of_superset _ (set.seq_mono (subset.refl _) ht),
rw set.seq_seq,
exact seq_mem_seq_sets (seq_mem_seq_sets (image_mem_map hs) hu) hv }
end
lemma prod_map_seq_comm (f : filter α) (g : filter β) :
(map prod.mk f).seq g = seq (map (λb a, (a, b)) g) f :=
begin
refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _),
{ rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩,
refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)),
rw ← set.prod_image_seq_comm,
exact seq_mem_seq_sets (image_mem_map ht) hu },
{ rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩,
refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)),
rw set.prod_image_seq_comm,
exact seq_mem_seq_sets (image_mem_map ht) hu }
end
instance : is_lawful_functor (filter : Type u → Type u) :=
{ id_map := assume α f, map_id,
comp_map := assume α β γ f g a, map_map.symm }
instance : is_lawful_applicative (filter : Type u → Type u) :=
{ pure_seq_eq_map := assume α β, pure_seq_eq_map,
map_pure := assume α β, map_pure,
seq_pure := assume α β, seq_pure,
seq_assoc := assume α β γ, seq_assoc }
instance : is_comm_applicative (filter : Type u → Type u) :=
⟨assume α β f g, prod_map_seq_comm f g⟩
lemma {l} seq_eq_filter_seq {α β : Type l} (f : filter (α → β)) (g : filter α) :
f <*> g = seq f g := rfl
end applicative
/- bind equations -/
section bind
@[simp] lemma mem_bind_sets {s : set β} {f : filter α} {m : α → filter β} :
s ∈ bind f m ↔ ∃t ∈ f, ∀x ∈ t, s ∈ m x :=
calc s ∈ bind f m ↔ {a | s ∈ m a} ∈ f : by simp only [bind, mem_map, iff_self, mem_join_sets, mem_set_of_eq]
... ↔ (∃t ∈ f, t ⊆ {a | s ∈ m a}) : exists_sets_subset_iff.symm
... ↔ (∃t ∈ f, ∀x ∈ t, s ∈ m x) : iff.refl _
lemma bind_mono {f : filter α} {g h : α → filter β} (h₁ : {a | g a ≤ h a} ∈ f) :
bind f g ≤ bind f h :=
assume x h₂, show (_ ∈ f), by filter_upwards [h₁, h₂] assume s gh' h', gh' h'
lemma bind_sup {f g : filter α} {h : α → filter β} :
bind (f ⊔ g) h = bind f h ⊔ bind g h :=
by simp only [bind, sup_join, map_sup, eq_self_iff_true]
lemma bind_mono2 {f g : filter α} {h : α → filter β} (h₁ : f ≤ g) :
bind f h ≤ bind g h :=
assume s h', h₁ h'
lemma principal_bind {s : set α} {f : α → filter β} :
(bind (principal s) f) = (⨆x ∈ s, f x) :=
show join (map f (principal s)) = (⨆x ∈ s, f x),
by simp only [Sup_image, join_principal_eq_Sup, map_principal, eq_self_iff_true]
end bind
lemma infi_neq_bot_of_directed {f : ι → filter α}
(hn : nonempty α) (hd : directed (≥) f) (hb : ∀i, f i ≠ ⊥) : (infi f) ≠ ⊥ :=
let ⟨x⟩ := hn in
assume h, have he: ∅ ∈ (infi f), from h.symm ▸ (mem_bot_sets : ∅ ∈ (⊥ : filter α)),
classical.by_cases
(assume : nonempty ι,
have ∃i, ∅ ∈ f i,
by rw [mem_infi hd this] at he; simp only [mem_Union] at he; assumption,
let ⟨i, hi⟩ := this in
hb i $ bot_unique $
assume s _, (f i).sets_of_superset hi $ empty_subset _)
(assume : ¬ nonempty ι,
have univ ⊆ (∅ : set α),
begin
rw [←principal_mono, principal_univ, principal_empty, ←h],
exact (le_infi $ assume i, false.elim $ this ⟨i⟩)
end,
this $ mem_univ x)
lemma infi_neq_bot_iff_of_directed {f : ι → filter α}
(hn : nonempty α) (hd : directed (≥) f) : (infi f) ≠ ⊥ ↔ (∀i, f i ≠ ⊥) :=
⟨assume neq_bot i eq_bot, neq_bot $ bot_unique $ infi_le_of_le i $ eq_bot ▸ le_refl _,
infi_neq_bot_of_directed hn hd⟩
lemma mem_infi_sets {f : ι → filter α} (i : ι) : ∀{s}, s ∈ f i → s ∈ ⨅i, f i :=
show (⨅i, f i) ≤ f i, from infi_le _ _
@[elab_as_eliminator]
lemma infi_sets_induct {f : ι → filter α} {s : set α} (hs : s ∈ infi f) {p : set α → Prop}
(uni : p univ)
(ins : ∀{i s₁ s₂}, s₁ ∈ f i → p s₂ → p (s₁ ∩ s₂))
(upw : ∀{s₁ s₂}, s₁ ⊆ s₂ → p s₁ → p s₂) : p s :=
begin
rw [mem_infi_finite] at hs,
simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at hs,
rcases hs with ⟨is, his⟩,
revert s,
refine finset.induction_on is _ _,
{ assume s hs, rwa [mem_top_sets.1 hs] },
{ rintros ⟨i⟩ js his ih s hs,
rw [finset.inf_insert, mem_inf_sets] at hs,
rcases hs with ⟨s₁, hs₁, s₂, hs₂, hs⟩,
exact upw hs (ins hs₁ (ih hs₂)) }
end
/- tendsto -/
/-- `tendsto` is the generic "limit of a function" predicate.
`tendsto f l₁ l₂` asserts that for every `l₂` neighborhood `a`,
the `f`-preimage of `a` is an `l₁` neighborhood. -/
def tendsto (f : α → β) (l₁ : filter α) (l₂ : filter β) := l₁.map f ≤ l₂
lemma tendsto_def {f : α → β} {l₁ : filter α} {l₂ : filter β} :
tendsto f l₁ l₂ ↔ ∀ s ∈ l₂, f ⁻¹' s ∈ l₁ := iff.rfl
lemma tendsto_iff_comap {f : α → β} {l₁ : filter α} {l₂ : filter β} :
tendsto f l₁ l₂ ↔ l₁ ≤ l₂.comap f :=
map_le_iff_le_comap
lemma tendsto.congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β}
(hl : {x | f₁ x = f₂ x} ∈ l₁) (h : tendsto f₁ l₁ l₂) : tendsto f₂ l₁ l₂ :=
by rwa [tendsto, ←map_cong hl]
theorem tendsto.congr'r {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β}
(h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ :=
iff_of_eq (by congr'; exact funext h)
theorem tendsto.congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β}
(h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ → tendsto f₂ l₁ l₂ :=
(tendsto.congr'r h).1
lemma tendsto_id' {x y : filter α} : x ≤ y → tendsto id x y :=
by simp only [tendsto, map_id, forall_true_iff] {contextual := tt}
lemma tendsto_id {x : filter α} : tendsto id x x := tendsto_id' $ le_refl x
lemma tendsto.comp {f : α → β} {g : β → γ} {x : filter α} {y : filter β} {z : filter γ}
(hg : tendsto g y z) (hf : tendsto f x y) : tendsto (g ∘ f) x z :=
calc map (g ∘ f) x = map g (map f x) : by rw [map_map]
... ≤ map g y : map_mono hf
... ≤ z : hg
lemma tendsto_le_left {f : α → β} {x y : filter α} {z : filter β}
(h : y ≤ x) : tendsto f x z → tendsto f y z :=
le_trans (map_mono h)
lemma tendsto_le_right {f : α → β} {x : filter α} {y z : filter β}
(h₁ : y ≤ z) (h₂ : tendsto f x y) : tendsto f x z :=
le_trans h₂ h₁
lemma tendsto_map {f : α → β} {x : filter α} : tendsto f x (map f x) := le_refl (map f x)
lemma tendsto_map' {f : β → γ} {g : α → β} {x : filter α} {y : filter γ}
(h : tendsto (f ∘ g) x y) : tendsto f (map g x) y :=
by rwa [tendsto, map_map]
lemma tendsto_map'_iff {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} :
tendsto f (map g x) y ↔ tendsto (f ∘ g) x y :=
by rw [tendsto, map_map]; refl
lemma tendsto_comap {f : α → β} {x : filter β} : tendsto f (comap f x) x :=
map_comap_le
lemma tendsto_comap_iff {f : α → β} {g : β → γ} {a : filter α} {c : filter γ} :
tendsto f a (c.comap g) ↔ tendsto (g ∘ f) a c :=
⟨assume h, tendsto_comap.comp h, assume h, map_le_iff_le_comap.mp $ by rwa [map_map]⟩
lemma tendsto_comap'_iff {m : α → β} {f : filter α} {g : filter β} {i : γ → α}
(h : range i ∈ f) : tendsto (m ∘ i) (comap i f) g ↔ tendsto m f g :=
by rw [tendsto, ← map_compose]; simp only [(∘), map_comap h, tendsto]
lemma comap_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α)
(eq : ψ ∘ φ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : comap φ g = f :=
begin
refine le_antisymm (le_trans (comap_mono $ map_le_iff_le_comap.1 hψ) _) (map_le_iff_le_comap.1 hφ),
rw [comap_comap_comp, eq, comap_id],
exact le_refl _
end
lemma map_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α)
(eq : φ ∘ ψ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : map φ f = g :=
begin
refine le_antisymm hφ (le_trans _ (map_mono hψ)),
rw [map_map, eq, map_id],
exact le_refl _
end
lemma tendsto_inf {f : α → β} {x : filter α} {y₁ y₂ : filter β} :
tendsto f x (y₁ ⊓ y₂) ↔ tendsto f x y₁ ∧ tendsto f x y₂ :=
by simp only [tendsto, lattice.le_inf_iff, iff_self]
lemma tendsto_inf_left {f : α → β} {x₁ x₂ : filter α} {y : filter β}
(h : tendsto f x₁ y) : tendsto f (x₁ ⊓ x₂) y :=
le_trans (map_mono inf_le_left) h
lemma tendsto_inf_right {f : α → β} {x₁ x₂ : filter α} {y : filter β}
(h : tendsto f x₂ y) : tendsto f (x₁ ⊓ x₂) y :=
le_trans (map_mono inf_le_right) h
lemma tendsto_infi {f : α → β} {x : filter α} {y : ι → filter β} :
tendsto f x (⨅i, y i) ↔ ∀i, tendsto f x (y i) :=
by simp only [tendsto, iff_self, lattice.le_infi_iff]
lemma tendsto_infi' {f : α → β} {x : ι → filter α} {y : filter β} (i : ι) :
tendsto f (x i) y → tendsto f (⨅i, x i) y :=
tendsto_le_left (infi_le _ _)
lemma tendsto_principal {f : α → β} {a : filter α} {s : set β} :
tendsto f a (principal s) ↔ {a | f a ∈ s} ∈ a :=
by simp only [tendsto, le_principal_iff, mem_map, iff_self]
lemma tendsto_principal_principal {f : α → β} {s : set α} {t : set β} :
tendsto f (principal s) (principal t) ↔ ∀a∈s, f a ∈ t :=
by simp only [tendsto, image_subset_iff, le_principal_iff, map_principal, mem_principal_sets]; refl
lemma tendsto_pure_pure (f : α → β) (a : α) :
tendsto f (pure a) (pure (f a)) :=
show filter.map f (pure a) ≤ pure (f a),
by rw [filter.map_pure]; exact le_refl _
lemma tendsto_const_pure {a : filter α} {b : β} : tendsto (λa, b) a (pure b) :=
by simp [tendsto]; exact univ_mem_sets
lemma tendsto_if {l₁ : filter α} {l₂ : filter β}
{f g : α → β} {p : α → Prop} [decidable_pred p]
(h₀ : tendsto f (l₁ ⊓ principal p) l₂)
(h₁ : tendsto g (l₁ ⊓ principal { x | ¬ p x }) l₂) :
tendsto (λ x, if p x then f x else g x) l₁ l₂ :=
begin
revert h₀ h₁, simp only [tendsto_def, mem_inf_principal],
intros h₀ h₁ s hs,
apply mem_sets_of_superset (inter_mem_sets (h₀ s hs) (h₁ s hs)),
rintros x ⟨hp₀, hp₁⟩, simp only [mem_preimage],
by_cases h : p x,
{ rw if_pos h, exact hp₀ h },
rw if_neg h, exact hp₁ h
end
section prod
variables {s : set α} {t : set β} {f : filter α} {g : filter β}
/- The product filter cannot be defined using the monad structure on filters. For example:
F := do {x <- seq, y <- top, return (x, y)}
hence:
s ∈ F <-> ∃n, [n..∞] × univ ⊆ s
G := do {y <- top, x <- seq, return (x, y)}
hence:
s ∈ G <-> ∀i:ℕ, ∃n, [n..∞] × {i} ⊆ s
Now ⋃i, [i..∞] × {i} is in G but not in F.
As product filter we want to have F as result.
-/
/-- Product of filters. This is the filter generated by cartesian products
of elements of the component filters. -/
protected def prod (f : filter α) (g : filter β) : filter (α × β) :=
f.comap prod.fst ⊓ g.comap prod.snd
lemma prod_mem_prod {s : set α} {t : set β} {f : filter α} {g : filter β}
(hs : s ∈ f) (ht : t ∈ g) : set.prod s t ∈ filter.prod f g :=
inter_mem_inf_sets (preimage_mem_comap hs) (preimage_mem_comap ht)
lemma mem_prod_iff {s : set (α×β)} {f : filter α} {g : filter β} :
s ∈ filter.prod f g ↔ (∃ t₁ ∈ f, ∃ t₂ ∈ g, set.prod t₁ t₂ ⊆ s) :=
begin
simp only [filter.prod],
split,
exact assume ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, h⟩,
⟨s₁, hs₁, s₂, hs₂, subset.trans (inter_subset_inter hts₁ hts₂) h⟩,
exact assume ⟨t₁, ht₁, t₂, ht₂, h⟩,
⟨prod.fst ⁻¹' t₁, ⟨t₁, ht₁, subset.refl _⟩, prod.snd ⁻¹' t₂, ⟨t₂, ht₂, subset.refl _⟩, h⟩
end
lemma tendsto_fst {f : filter α} {g : filter β} : tendsto prod.fst (filter.prod f g) f :=
tendsto_inf_left tendsto_comap
lemma tendsto_snd {f : filter α} {g : filter β} : tendsto prod.snd (filter.prod f g) g :=
tendsto_inf_right tendsto_comap
lemma tendsto.prod_mk {f : filter α} {g : filter β} {h : filter γ} {m₁ : α → β} {m₂ : α → γ}
(h₁ : tendsto m₁ f g) (h₂ : tendsto m₂ f h) : tendsto (λx, (m₁ x, m₂ x)) f (filter.prod g h) :=
tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩
lemma prod_infi_left {f : ι → filter α} {g : filter β} (i : ι) :
filter.prod (⨅i, f i) g = (⨅i, filter.prod (f i) g) :=
by rw [filter.prod, comap_infi, infi_inf i]; simp only [filter.prod, eq_self_iff_true]
lemma prod_infi_right {f : filter α} {g : ι → filter β} (i : ι) :
filter.prod f (⨅i, g i) = (⨅i, filter.prod f (g i)) :=
by rw [filter.prod, comap_infi, inf_infi i]; simp only [filter.prod, eq_self_iff_true]
lemma prod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) :
filter.prod f₁ g₁ ≤ filter.prod f₂ g₂ :=
inf_le_inf (comap_mono hf) (comap_mono hg)
lemma prod_comap_comap_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x}
{f₁ : filter α₁} {f₂ : filter α₂} {m₁ : β₁ → α₁} {m₂ : β₂ → α₂} :
filter.prod (comap m₁ f₁) (comap m₂ f₂) = comap (λp:β₁×β₂, (m₁ p.1, m₂ p.2)) (filter.prod f₁ f₂) :=
by simp only [filter.prod, comap_comap_comp, eq_self_iff_true, comap_inf]
lemma prod_comm' : filter.prod f g = comap (prod.swap) (filter.prod g f) :=
by simp only [filter.prod, comap_comap_comp, (∘), inf_comm, prod.fst_swap,
eq_self_iff_true, prod.snd_swap, comap_inf]
lemma prod_comm : filter.prod f g = map (λp:β×α, (p.2, p.1)) (filter.prod g f) :=
by rw [prod_comm', ← map_swap_eq_comap_swap]; refl
lemma prod_map_map_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x}
{f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} :
filter.prod (map m₁ f₁) (map m₂ f₂) = map (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) (filter.prod f₁ f₂) :=
le_antisymm
(assume s hs,
let ⟨s₁, hs₁, s₂, hs₂, h⟩ := mem_prod_iff.mp hs in
filter.sets_of_superset _ (prod_mem_prod (image_mem_map hs₁) (image_mem_map hs₂)) $
calc set.prod (m₁ '' s₁) (m₂ '' s₂) = (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) '' set.prod s₁ s₂ :
set.prod_image_image_eq
... ⊆ _ : by rwa [image_subset_iff])
((tendsto.comp (le_refl _) tendsto_fst).prod_mk (tendsto.comp (le_refl _) tendsto_snd))
lemma map_prod (m : α × β → γ) (f : filter α) (g : filter β) :
map m (f.prod g) = (f.map (λa b, m (a, b))).seq g :=
begin
simp [filter.ext_iff, mem_prod_iff, mem_map_seq_iff],
assume s,
split,
exact assume ⟨t, ht, s, hs, h⟩, ⟨s, hs, t, ht, assume x hx y hy, @h ⟨x, y⟩ ⟨hx, hy⟩⟩,
exact assume ⟨s, hs, t, ht, h⟩, ⟨t, ht, s, hs, assume ⟨x, y⟩ ⟨hx, hy⟩, h x hx y hy⟩
end
lemma prod_eq {f : filter α} {g : filter β} : f.prod g = (f.map prod.mk).seq g :=
have h : _ := map_prod id f g, by rwa [map_id] at h
lemma prod_inf_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} :
filter.prod f₁ g₁ ⊓ filter.prod f₂ g₂ = filter.prod (f₁ ⊓ f₂) (g₁ ⊓ g₂) :=
by simp only [filter.prod, comap_inf, inf_comm, inf_assoc, lattice.inf_left_comm]
@[simp] lemma prod_bot {f : filter α} : filter.prod f (⊥ : filter β) = ⊥ := by simp [filter.prod]
@[simp] lemma bot_prod {g : filter β} : filter.prod (⊥ : filter α) g = ⊥ := by simp [filter.prod]
@[simp] lemma prod_principal_principal {s : set α} {t : set β} :
filter.prod (principal s) (principal t) = principal (set.prod s t) :=
by simp only [filter.prod, comap_principal, principal_eq_iff_eq, comap_principal, inf_principal]; refl
@[simp] lemma prod_pure_pure {a : α} {b : β} : filter.prod (pure a) (pure b) = pure (a, b) :=
by simp
lemma prod_eq_bot {f : filter α} {g : filter β} : filter.prod f g = ⊥ ↔ (f = ⊥ ∨ g = ⊥) :=
begin
split,
{ assume h,
rcases mem_prod_iff.1 (empty_in_sets_eq_bot.2 h) with ⟨s, hs, t, ht, hst⟩,
rw [subset_empty_iff, set.prod_eq_empty_iff] at hst,
cases hst with s_eq t_eq,
{ left, exact empty_in_sets_eq_bot.1 (s_eq ▸ hs) },
{ right, exact empty_in_sets_eq_bot.1 (t_eq ▸ ht) } },
{ rintros (rfl | rfl),
exact bot_prod,
exact prod_bot }
end
lemma prod_neq_bot {f : filter α} {g : filter β} : filter.prod f g ≠ ⊥ ↔ (f ≠ ⊥ ∧ g ≠ ⊥) :=
by rw [(≠), prod_eq_bot, not_or_distrib]
lemma tendsto_prod_iff {f : α × β → γ} {x : filter α} {y : filter β} {z : filter γ} :
filter.tendsto f (filter.prod x y) z ↔
∀ W ∈ z, ∃ U ∈ x, ∃ V ∈ y, ∀ x y, x ∈ U → y ∈ V → f (x, y) ∈ W :=
by simp only [tendsto_def, mem_prod_iff, prod_sub_preimage_iff, exists_prop, iff_self]
end prod
/- at_top and at_bot -/
/-- `at_top` is the filter representing the limit `→ ∞` on an ordered set.
It is generated by the collection of up-sets `{b | a ≤ b}`.
(The preorder need not have a top element for this to be well defined,
and indeed is trivial when a top element exists.) -/
def at_top [preorder α] : filter α := ⨅ a, principal {b | a ≤ b}
/-- `at_bot` is the filter representing the limit `→ -∞` on an ordered set.
It is generated by the collection of down-sets `{b | b ≤ a}`.
(The preorder need not have a bottom element for this to be well defined,
and indeed is trivial when a bottom element exists.) -/
def at_bot [preorder α] : filter α := ⨅ a, principal {b | b ≤ a}
lemma mem_at_top [preorder α] (a : α) : {b : α | a ≤ b} ∈ @at_top α _ :=
mem_infi_sets a $ subset.refl _
@[simp] lemma at_top_ne_bot [nonempty α] [semilattice_sup α] : (at_top : filter α) ≠ ⊥ :=
infi_neq_bot_of_directed (by apply_instance)
(assume a b, ⟨a ⊔ b, by simp only [ge, le_principal_iff, forall_const, set_of_subset_set_of,
mem_principal_sets, and_self, sup_le_iff, forall_true_iff] {contextual := tt}⟩)
(assume a, by simp only [principal_eq_bot_iff, ne.def, principal_eq_bot_iff]; exact ne_empty_of_mem (le_refl a))
@[simp] lemma mem_at_top_sets [nonempty α] [semilattice_sup α] {s : set α} :
s ∈ (at_top : filter α) ↔ ∃a:α, ∀b≥a, b ∈ s :=
let ⟨a⟩ := ‹nonempty α› in
iff.intro
(assume h, infi_sets_induct h ⟨a, by simp only [forall_const, mem_univ, forall_true_iff]⟩
(assume a s₁ s₂ ha ⟨b, hb⟩, ⟨a ⊔ b,
assume c hc, ⟨ha $ le_trans le_sup_left hc, hb _ $ le_trans le_sup_right hc⟩⟩)
(assume s₁ s₂ h ⟨a, ha⟩, ⟨a, assume b hb, h $ ha _ hb⟩))
(assume ⟨a, h⟩, mem_infi_sets a $ assume x, h x)
lemma map_at_top_eq [nonempty α] [semilattice_sup α] {f : α → β} :
at_top.map f = (⨅a, principal $ f '' {a' | a ≤ a'}) :=
calc map f (⨅a, principal {a' | a ≤ a'}) = (⨅a, map f $ principal {a' | a ≤ a'}) :
map_infi_eq (assume a b, ⟨a ⊔ b, by simp only [ge, le_principal_iff, forall_const, set_of_subset_set_of,
mem_principal_sets, and_self, sup_le_iff, forall_true_iff] {contextual := tt}⟩)
(by apply_instance)
... = (⨅a, principal $ f '' {a' | a ≤ a'}) : by simp only [map_principal, eq_self_iff_true]
lemma tendsto_at_top [preorder β] (m : α → β) (f : filter α) :
tendsto m f at_top ↔ (∀b, {a | b ≤ m a} ∈ f) :=
by simp only [at_top, tendsto_infi, tendsto_principal]; refl
lemma tendsto_at_top' [nonempty α] [semilattice_sup α] (f : α → β) (l : filter β) :
tendsto f at_top l ↔ (∀s ∈ l, ∃a, ∀b≥a, f b ∈ s) :=
by simp only [tendsto_def, mem_at_top_sets]; refl
theorem tendsto_at_top_principal [nonempty β] [semilattice_sup β] {f : β → α} {s : set α} :
tendsto f at_top (principal s) ↔ ∃N, ∀n≥N, f n ∈ s :=
by rw [tendsto_iff_comap, comap_principal, le_principal_iff, mem_at_top_sets]; refl
/-- A function `f` grows to infinity independent of an order-preserving embedding `e`. -/
lemma tendsto_at_top_embedding {α β γ : Type*} [preorder β] [preorder γ]
{f : α → β} {e : β → γ} {l : filter α}
(hm : ∀b₁ b₂, e b₁ ≤ e b₂ ↔ b₁ ≤ b₂) (hu : ∀c, ∃b, c ≤ e b) :
tendsto (e ∘ f) l at_top ↔ tendsto f l at_top :=
begin
rw [tendsto_at_top, tendsto_at_top],
split,
{ assume hc b,
filter_upwards [hc (e b)] assume a, (hm b (f a)).1 },
{ assume hb c,
rcases hu c with ⟨b, hc⟩,
filter_upwards [hb b] assume a ha, le_trans hc ((hm b (f a)).2 ha) }
end
lemma tendsto_at_top_at_top [nonempty α] [semilattice_sup α] [preorder β] (f : α → β) :
tendsto f at_top at_top ↔ ∀ b : β, ∃ i : α, ∀ a : α, i ≤ a → b ≤ f a :=
iff.trans tendsto_infi $ forall_congr $ assume b, tendsto_at_top_principal
lemma tendsto_at_top_at_bot [nonempty α] [decidable_linear_order α] [preorder β] (f : α → β) :
tendsto f at_top at_bot ↔ ∀ (b : β), ∃ (i : α), ∀ (a : α), i ≤ a → b ≥ f a :=
@tendsto_at_top_at_top α (order_dual β) _ _ _ f
lemma tendsto_finset_image_at_top_at_top {i : β → γ} {j : γ → β} (h : ∀x, j (i x) = x) :
tendsto (λs:finset γ, s.image j) at_top at_top :=
tendsto_infi.2 $ assume s, tendsto_infi' (s.image i) $ tendsto_principal_principal.2 $
assume t (ht : s.image i ⊆ t),
calc s = (s.image i).image j :
by simp only [finset.image_image, (∘), h]; exact finset.image_id.symm
... ⊆ t.image j : finset.image_subset_image ht
lemma prod_at_top_at_top_eq {β₁ β₂ : Type*} [inhabited β₁] [inhabited β₂] [semilattice_sup β₁]
[semilattice_sup β₂] : filter.prod (@at_top β₁ _) (@at_top β₂ _) = @at_top (β₁ × β₂) _ :=
by simp [at_top, prod_infi_left (default β₁), prod_infi_right (default β₂), infi_prod];
exact infi_comm
lemma prod_map_at_top_eq {α₁ α₂ β₁ β₂ : Type*} [inhabited β₁] [inhabited β₂]
[semilattice_sup β₁] [semilattice_sup β₂] (u₁ : β₁ → α₁) (u₂ : β₂ → α₂) :
filter.prod (map u₁ at_top) (map u₂ at_top) = map (prod.map u₁ u₂) at_top :=
by rw [prod_map_map_eq, prod_at_top_at_top_eq, prod.map_def]
/-- A function `f` maps upwards closed sets (at_top sets) to upwards closed sets when it is a
Galois insertion. The Galois "insertion" and "connection" is weakened to only require it to be an
insertion and a connetion above `b'`. -/
lemma map_at_top_eq_of_gc [semilattice_sup α] [semilattice_sup β] {f : α → β} (g : β → α) (b' : β)(hf : monotone f) (gc : ∀a, ∀b≥b', f a ≤ b ↔ a ≤ g b) (hgi : ∀b≥b', b ≤ f (g b)) :
map f at_top = at_top :=
begin
rw [@map_at_top_eq α _ ⟨g b'⟩],
refine le_antisymm
(le_infi $ assume b, infi_le_of_le (g (b ⊔ b')) $ principal_mono.2 $ image_subset_iff.2 _)
(le_infi $ assume a, infi_le_of_le (f a ⊔ b') $ principal_mono.2 _),
{ assume a ha, exact (le_trans le_sup_left $ le_trans (hgi _ le_sup_right) $ hf ha) },
{ assume b hb,
have hb' : b' ≤ b := le_trans le_sup_right hb,
exact ⟨g b, (gc _ _ hb').1 (le_trans le_sup_left hb),
le_antisymm ((gc _ _ hb').2 (le_refl _)) (hgi _ hb')⟩ }
end
lemma map_add_at_top_eq_nat (k : ℕ) : map (λa, a + k) at_top = at_top :=
map_at_top_eq_of_gc (λa, a - k) k
(assume a b h, add_le_add_right h k)
(assume a b h, (nat.le_sub_right_iff_add_le h).symm)
(assume a h, by rw [nat.sub_add_cancel h])
lemma map_sub_at_top_eq_nat (k : ℕ) : map (λa, a - k) at_top = at_top :=
map_at_top_eq_of_gc (λa, a + k) 0
(assume a b h, nat.sub_le_sub_right h _)
(assume a b _, nat.sub_le_right_iff_le_add)
(assume b _, by rw [nat.add_sub_cancel])
lemma tendso_add_at_top_nat (k : ℕ) : tendsto (λa, a + k) at_top at_top :=
le_of_eq (map_add_at_top_eq_nat k)
lemma tendso_sub_at_top_nat (k : ℕ) : tendsto (λa, a - k) at_top at_top :=
le_of_eq (map_sub_at_top_eq_nat k)
lemma tendsto_add_at_top_iff_nat {f : ℕ → α} {l : filter α} (k : ℕ) :
tendsto (λn, f (n + k)) at_top l ↔ tendsto f at_top l :=
show tendsto (f ∘ (λn, n + k)) at_top l ↔ tendsto f at_top l,
by rw [← tendsto_map'_iff, map_add_at_top_eq_nat]
lemma map_div_at_top_eq_nat (k : ℕ) (hk : k > 0) : map (λa, a / k) at_top = at_top :=
map_at_top_eq_of_gc (λb, b * k + (k - 1)) 1
(assume a b h, nat.div_le_div_right h)
(assume a b _,
calc a / k ≤ b ↔ a / k < b + 1 : by rw [← nat.succ_eq_add_one, nat.lt_succ_iff]
... ↔ a < (b + 1) * k : nat.div_lt_iff_lt_mul _ _ hk
... ↔ _ :
begin
cases k,
exact (lt_irrefl _ hk).elim,
simp [mul_add, add_mul, nat.succ_add, nat.lt_succ_iff]
end)
(assume b _,
calc b = (b * k) / k : by rw [nat.mul_div_cancel b hk]
... ≤ (b * k + (k - 1)) / k : nat.div_le_div_right $ nat.le_add_right _ _)
/- ultrafilter -/
section ultrafilter
open zorn
variables {f g : filter α}
/-- An ultrafilter is a minimal (maximal in the set order) proper filter. -/
def is_ultrafilter (f : filter α) := f ≠ ⊥ ∧ ∀g, g ≠ ⊥ → g ≤ f → f ≤ g
lemma ultrafilter_unique (hg : is_ultrafilter g) (hf : f ≠ ⊥) (h : f ≤ g) : f = g :=
le_antisymm h (hg.right _ hf h)
lemma le_of_ultrafilter {g : filter α} (hf : is_ultrafilter f) (h : f ⊓ g ≠ ⊥) :
f ≤ g :=
le_of_inf_eq $ ultrafilter_unique hf h inf_le_left
/-- Equivalent characterization of ultrafilters:
A filter f is an ultrafilter if and only if for each set s,
-s belongs to f if and only if s does not belong to f. -/
lemma ultrafilter_iff_compl_mem_iff_not_mem :
is_ultrafilter f ↔ (∀ s, -s ∈ f ↔ s ∉ f) :=
⟨assume hf s,
⟨assume hns hs,
hf.1 $ empty_in_sets_eq_bot.mp $ by convert f.inter_sets hs hns; rw [inter_compl_self],
assume hs,
have f ≤ principal (-s), from
le_of_ultrafilter hf $ assume h, hs $ mem_sets_of_neq_bot $
by simp only [h, eq_self_iff_true, lattice.neg_neg],
by simp only [le_principal_iff] at this; assumption⟩,
assume hf,
⟨mt empty_in_sets_eq_bot.mpr ((hf ∅).mp (by convert f.univ_sets; rw [compl_empty])),
assume g hg g_le s hs, classical.by_contradiction $ mt (hf s).mpr $
assume : - s ∈ f,
have s ∩ -s ∈ g, from inter_mem_sets hs (g_le this),
by simp only [empty_in_sets_eq_bot, hg, inter_compl_self] at this; contradiction⟩⟩
lemma mem_or_compl_mem_of_ultrafilter (hf : is_ultrafilter f) (s : set α) :
s ∈ f ∨ - s ∈ f :=
classical.or_iff_not_imp_left.2 (ultrafilter_iff_compl_mem_iff_not_mem.mp hf s).mpr
lemma mem_or_mem_of_ultrafilter {s t : set α} (hf : is_ultrafilter f) (h : s ∪ t ∈ f) :
s ∈ f ∨ t ∈ f :=
(mem_or_compl_mem_of_ultrafilter hf s).imp_right
(assume : -s ∈ f, by filter_upwards [this, h] assume x hnx hx, hx.resolve_left hnx)
lemma mem_of_finite_sUnion_ultrafilter {s : set (set α)} (hf : is_ultrafilter f) (hs : finite s)
: ⋃₀ s ∈ f → ∃t∈s, t ∈ f :=
finite.induction_on hs (by simp only [empty_in_sets_eq_bot, hf.left, mem_empty_eq, sUnion_empty,
forall_prop_of_false, exists_false, not_false_iff, exists_prop_of_false]) $
λ t s' ht' hs' ih, by simp only [exists_prop, mem_insert_iff, set.sUnion_insert]; exact
assume h, (mem_or_mem_of_ultrafilter hf h).elim
(assume : t ∈ f, ⟨t, or.inl rfl, this⟩)
(assume h, let ⟨t, hts', ht⟩ := ih h in ⟨t, or.inr hts', ht⟩)
lemma mem_of_finite_Union_ultrafilter {is : set β} {s : β → set α}
(hf : is_ultrafilter f) (his : finite is) (h : (⋃i∈is, s i) ∈ f) : ∃i∈is, s i ∈ f :=
have his : finite (image s is), from finite_image s his,
have h : (⋃₀ image s is) ∈ f, from by simp only [sUnion_image, set.sUnion_image]; assumption,
let ⟨t, ⟨i, hi, h_eq⟩, (ht : t ∈ f)⟩ := mem_of_finite_sUnion_ultrafilter hf his h in
⟨i, hi, h_eq.symm ▸ ht⟩
lemma ultrafilter_map {f : filter α} {m : α → β} (h : is_ultrafilter f) : is_ultrafilter (map m f) :=
by rw ultrafilter_iff_compl_mem_iff_not_mem at ⊢ h; exact assume s, h (m ⁻¹' s)
lemma ultrafilter_pure {a : α} : is_ultrafilter (pure a) :=
begin
rw ultrafilter_iff_compl_mem_iff_not_mem, intro s,
rw [mem_pure_sets, mem_pure_sets], exact iff.rfl
end
lemma ultrafilter_bind {f : filter α} (hf : is_ultrafilter f) {m : α → filter β}
(hm : ∀ a, is_ultrafilter (m a)) : is_ultrafilter (f.bind m) :=
begin
simp only [ultrafilter_iff_compl_mem_iff_not_mem] at ⊢ hf hm, intro s,
dsimp [bind, join, map, preimage],
simp only [hm], apply hf
end
/-- The ultrafilter lemma: Any proper filter is contained in an ultrafilter. -/
lemma exists_ultrafilter (h : f ≠ ⊥) : ∃u, u ≤ f ∧ is_ultrafilter u :=
let
τ := {f' // f' ≠ ⊥ ∧ f' ≤ f},
r : τ → τ → Prop := λt₁ t₂, t₂.val ≤ t₁.val,
⟨a, ha⟩ := inhabited_of_mem_sets h univ_mem_sets,
top : τ := ⟨f, h, le_refl f⟩,
sup : Π(c:set τ), chain r c → τ :=
λc hc, ⟨⨅a:{a:τ // a ∈ insert top c}, a.val.val,
infi_neq_bot_of_directed ⟨a⟩
(directed_of_chain $ chain_insert hc $ assume ⟨b, _, hb⟩ _ _, or.inl hb)
(assume ⟨⟨a, ha, _⟩, _⟩, ha),
infi_le_of_le ⟨top, mem_insert _ _⟩ (le_refl _)⟩
in
have ∀c (hc: chain r c) a (ha : a ∈ c), r a (sup c hc),
from assume c hc a ha, infi_le_of_le ⟨a, mem_insert_of_mem _ ha⟩ (le_refl _),
have (∃ (u : τ), ∀ (a : τ), r u a → r a u),
from zorn (assume c hc, ⟨sup c hc, this c hc⟩) (assume f₁ f₂ f₃ h₁ h₂, le_trans h₂ h₁),
let ⟨uτ, hmin⟩ := this in
⟨uτ.val, uτ.property.right, uτ.property.left, assume g hg₁ hg₂,
hmin ⟨g, hg₁, le_trans hg₂ uτ.property.right⟩ hg₂⟩
/-- Construct an ultrafilter extending a given filter.
The ultrafilter lemma is the assertion that such a filter exists;
we use the axiom of choice to pick one. -/
noncomputable def ultrafilter_of (f : filter α) : filter α :=
if h : f = ⊥ then ⊥ else classical.epsilon (λu, u ≤ f ∧ is_ultrafilter u)
lemma ultrafilter_of_spec (h : f ≠ ⊥) : ultrafilter_of f ≤ f ∧ is_ultrafilter (ultrafilter_of f) :=
begin
have h' := classical.epsilon_spec (exists_ultrafilter h),
simp only [ultrafilter_of, dif_neg, h, dif_neg, not_false_iff],
simp only at h',
assumption
end
lemma ultrafilter_of_le : ultrafilter_of f ≤ f :=
if h : f = ⊥ then by simp only [ultrafilter_of, dif_pos, h, dif_pos, eq_self_iff_true, le_bot_iff]; exact le_refl _
else (ultrafilter_of_spec h).left
lemma ultrafilter_ultrafilter_of (h : f ≠ ⊥) : is_ultrafilter (ultrafilter_of f) :=
(ultrafilter_of_spec h).right
lemma ultrafilter_of_ultrafilter (h : is_ultrafilter f) : ultrafilter_of f = f :=
ultrafilter_unique h (ultrafilter_ultrafilter_of h.left).left ultrafilter_of_le
/-- A filter equals the intersection of all the ultrafilters which contain it. -/
lemma sup_of_ultrafilters (f : filter α) : f = ⨆ (g) (u : is_ultrafilter g) (H : g ≤ f), g :=
begin
refine le_antisymm _ (supr_le $ λ g, supr_le $ λ u, supr_le $ λ H, H),
intros s hs,
-- If s ∉ f.sets, we'll apply the ultrafilter lemma to the restriction of f to -s.
by_contradiction hs',
let j : (-s) → α := subtype.val,
have j_inv_s : j ⁻¹' s = ∅, by
erw [←preimage_inter_range, subtype.val_range, inter_compl_self, preimage_empty],
let f' := comap j f,
have : f' ≠ ⊥,
{ apply mt empty_in_sets_eq_bot.mpr,
rintro ⟨t, htf, ht⟩,
suffices : t ⊆ s, from absurd (f.sets_of_superset htf this) hs',
rw [subset_empty_iff] at ht,
have : j '' (j ⁻¹' t) = ∅, by rw [ht, image_empty],
erw [image_preimage_eq_inter_range, subtype.val_range, ←subset_compl_iff_disjoint,
set.compl_compl] at this,
exact this },
rcases exists_ultrafilter this with ⟨g', g'f', u'⟩,
simp only [supr_sets_eq, mem_Inter] at hs,
have := hs (g'.map subtype.val) (ultrafilter_map u') (map_le_iff_le_comap.mpr g'f'),
rw [←le_principal_iff, map_le_iff_le_comap, comap_principal, j_inv_s, principal_empty,
le_bot_iff] at this,
exact absurd this u'.1
end
/-- The `tendsto` relation can be checked on ultrafilters. -/
lemma tendsto_iff_ultrafilter (f : α → β) (l₁ : filter α) (l₂ : filter β) :
tendsto f l₁ l₂ ↔ ∀ g, is_ultrafilter g → g ≤ l₁ → g.map f ≤ l₂ :=
⟨assume h g u gx, le_trans (map_mono gx) h,
assume h, by rw [sup_of_ultrafilters l₁]; simpa only [tendsto, map_supr, supr_le_iff]⟩
/- The ultrafilter monad. The monad structure on ultrafilters is the
restriction of the one on filters. -/
def ultrafilter (α : Type u) : Type u := {f : filter α // is_ultrafilter f}
def ultrafilter.map (m : α → β) (u : ultrafilter α) : ultrafilter β :=
⟨u.val.map m, ultrafilter_map u.property⟩
def ultrafilter.pure (x : α) : ultrafilter α := ⟨pure x, ultrafilter_pure⟩
def ultrafilter.bind (u : ultrafilter α) (m : α → ultrafilter β) : ultrafilter β :=
⟨u.val.bind (λ a, (m a).val), ultrafilter_bind u.property (λ a, (m a).property)⟩
instance ultrafilter.has_pure : has_pure ultrafilter := ⟨@ultrafilter.pure⟩
instance ultrafilter.has_bind : has_bind ultrafilter := ⟨@ultrafilter.bind⟩
instance ultrafilter.functor : functor ultrafilter := { map := @ultrafilter.map }
instance ultrafilter.monad : monad ultrafilter := { map := @ultrafilter.map }
noncomputable def hyperfilter : filter α := ultrafilter_of cofinite
lemma hyperfilter_le_cofinite (hi : set.infinite (@set.univ α)) : @hyperfilter α ≤ cofinite :=
(ultrafilter_of_spec (cofinite_ne_bot hi)).1
lemma is_ultrafilter_hyperfilter (hi : set.infinite (@set.univ α)) : is_ultrafilter (@hyperfilter α) :=
(ultrafilter_of_spec (cofinite_ne_bot hi)).2
theorem nmem_hyperfilter_of_finite (hi : set.infinite (@set.univ α)) {s : set α} (hf : set.finite s) :
s ∉ @hyperfilter α :=
λ hy,
have hx : -s ∉ hyperfilter :=
λ hs, (ultrafilter_iff_compl_mem_iff_not_mem.mp (is_ultrafilter_hyperfilter hi) s).mp hs hy,
have ht : -s ∈ cofinite.sets := by show -s ∈ {s | _}; rwa [set.mem_set_of_eq, lattice.neg_neg],
hx $ hyperfilter_le_cofinite hi ht
theorem compl_mem_hyperfilter_of_finite (hi : set.infinite (@set.univ α)) {s : set α} (hf : set.finite s) :
-s ∈ @hyperfilter α :=
(ultrafilter_iff_compl_mem_iff_not_mem.mp (is_ultrafilter_hyperfilter hi) s).mpr $
nmem_hyperfilter_of_finite hi hf
theorem mem_hyperfilter_of_finite_compl (hi : set.infinite (@set.univ α)) {s : set α} (hf : set.finite (-s)) :
s ∈ @hyperfilter α :=
have h : _ := compl_mem_hyperfilter_of_finite hi hf,
by rwa [lattice.neg_neg] at h
section
local attribute [instance] filter.monad filter.is_lawful_monad
instance ultrafilter.is_lawful_monad : is_lawful_monad ultrafilter :=
{ id_map := assume α f, subtype.eq (id_map f.val),
pure_bind := assume α β a f, subtype.eq (pure_bind a (subtype.val ∘ f)),
bind_assoc := assume α β γ f m₁ m₂, subtype.eq (filter_eq rfl),
bind_pure_comp_eq_map := assume α β f x, subtype.eq (bind_pure_comp_eq_map _ f x.val) }
end
lemma ultrafilter.eq_iff_val_le_val {u v : ultrafilter α} : u = v ↔ u.val ≤ v.val :=
⟨assume h, by rw h; exact le_refl _,
assume h, by rw subtype.ext; apply ultrafilter_unique v.property u.property.1 h⟩
lemma exists_ultrafilter_iff (f : filter α) : (∃ (u : ultrafilter α), u.val ≤ f) ↔ f ≠ ⊥ :=
⟨assume ⟨u, uf⟩, lattice.neq_bot_of_le_neq_bot u.property.1 uf,
assume h, let ⟨u, uf, hu⟩ := exists_ultrafilter h in ⟨⟨u, hu⟩, uf⟩⟩
end ultrafilter
end filter
namespace filter
variables {α β γ : Type u} {f : β → filter α} {s : γ → set α}
open list
lemma mem_traverse_sets :
∀(fs : list β) (us : list γ),
forall₂ (λb c, s c ∈ f b) fs us → traverse s us ∈ traverse f fs
| [] [] forall₂.nil := mem_pure_sets.2 $ mem_singleton _
| (f::fs) (u::us) (forall₂.cons h hs) := seq_mem_seq_sets (image_mem_map h) (mem_traverse_sets fs us hs)
lemma mem_traverse_sets_iff (fs : list β) (t : set (list α)) :
t ∈ traverse f fs ↔
(∃us:list (set α), forall₂ (λb (s : set α), s ∈ f b) fs us ∧ sequence us ⊆ t) :=
begin
split,
{ induction fs generalizing t,
case nil { simp only [sequence, pure_def, imp_self, forall₂_nil_left_iff, pure_def,
exists_eq_left, mem_principal_sets, set.pure_def, singleton_subset_iff, traverse_nil] },
case cons : b fs ih t {
assume ht,
rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩,
rcases mem_map_sets_iff.1 hu with ⟨w, hw, hwu⟩,
rcases ih v hv with ⟨us, hus, hu⟩,
exact ⟨w :: us, forall₂.cons hw hus, subset.trans (set.seq_mono hwu hu) ht⟩ } },
{ rintros ⟨us, hus, hs⟩,
exact mem_sets_of_superset (mem_traverse_sets _ _ hus) hs }
end
lemma sequence_mono :
∀(as bs : list (filter α)), forall₂ (≤) as bs → sequence as ≤ sequence bs
| [] [] forall₂.nil := le_refl _
| (a::as) (b::bs) (forall₂.cons h hs) := seq_mono (map_mono h) (sequence_mono as bs hs)
end filter
|
e0eef670946b6f6606919aeb0729e427d2ef113e | 037dba89703a79cd4a4aec5e959818147f97635d | /src/2020/integers/int_def.lean | 38529940bb7d0a23b207ee270de3437c20b520ec | [] | no_license | ImperialCollegeLondon/M40001_lean | 3a6a09298da395ab51bc220a535035d45bbe919b | 62a76fa92654c855af2b2fc2bef8e60acd16ccec | refs/heads/master | 1,666,750,403,259 | 1,665,771,117,000 | 1,665,771,117,000 | 209,141,835 | 115 | 12 | null | 1,640,270,596,000 | 1,568,749,174,000 | Lean | UTF-8 | Lean | false | false | 9,119 | lean | import tactic
/-- The equivalence relation on ℕ² such that equivalence classes are ℤ -/
def nat2.R (a b : ℕ × ℕ) : Prop :=
a.1 + b.2 = b.1 + a.2
-- here a and b are pairs, so a = (a.1, a.2) etc.
-- introduce ≈ (type with `\~~`) notation for this relation
instance : has_equiv (ℕ × ℕ) := ⟨nat2.R⟩
-- let's prove some lemmas about this binary relation
namespace nat2.R
-- The following lemma is true by definition, but it's useful to
-- have it around so you can rewrite with it
lemma equiv_def {i j k l : ℕ} : (i, j) ≈ (k, l) ↔ i + l = k + j :=
begin
refl
end
-- try rewriting `equiv_def`
lemma practice : (3, 5) ≈ (4, 6) :=
begin
sorry
end
-- Now let's prove that this binary relation is an equivalence relation
lemma reflexive : ∀ x : ℕ × ℕ, x ≈ x :=
begin
-- let x be (i,j)
rintro ⟨i, j⟩,
sorry
end
lemma symmetric : ∀ x y : ℕ × ℕ, (x ≈ y) → (y ≈ x) :=
begin
-- here are a couple of tricks
rintro ⟨i, j⟩ ⟨k, l⟩ h,
-- type `⊢` with `\|-`
rw equiv_def at h ⊢,
sorry
end
-- sub-boss
lemma transitive : ∀ x y z : ℕ × ℕ, (x ≈ y) → (y ≈ z) → (x ≈ z) :=
begin
-- this is a little trickier
-- recall `add_left_inj a` says `b + a = c + a ↔ b = c`
-- and you might want to consider rewriting it in the ← direction
sorry
end
-- This line tells Lean that the binary relation is an equivalence
-- relation and hence we can take the "quotient", i.e. the
-- type of equivalence classes
instance setoid : setoid (ℕ × ℕ) :=
{ r := nat2.R,
iseqv := ⟨reflexive, symmetric, transitive⟩ }
-- end of lemmas about the binary relation
end nat2.R
-- ...but we're still going to be using them
open nat2.R
/-- The integers are the equivalence classes of the equivalence relation
we just defined on ℕ² -/
def myint := quotient nat2.R.setoid
-- let's make some definitions, and prove some theorems, about integers
namespace myint
-- The first goal is to get a good interface for addition.
-- To do this we need to define a+b, and -a, and 0. Let's do
-- them in reverse order.
/-! ## zero -/
-- Notation: ⟦(a,b)⟧ ∈ ℤ is the equivalence class of (a,b) ∈ ℕ²
/-- 0 is the equivalence class of (0,0) -/
def zero := ⟦(0,0)⟧
-- Notation 0 for zero
instance : has_zero myint := ⟨myint.zero⟩
-- true by definition
lemma zero_def : (0 : myint) = ⟦(0, 0)⟧ :=
begin
sorry
end
/-! ## negation (additive inverse) -/
-- First we define an "auxiliary" map from ℕ² to ℤ
-- sending (a,b) to the equivalence class of (b,a).
def neg_aux (x : ℕ × ℕ) : myint := ⟦(x.2, x.1)⟧
-- true by definition
lemma neg_aux_def (i j : ℕ) : neg_aux (i, j) = ⟦(j, i)⟧ :=
begin
sorry
end
/-! ### Well-definedness of negation
OK now here's the concrete problem. We would like to define
a negation map `ℤ → ℤ` sending `z` to `-z`. We want to do this in
the following way: Say `z ∈ ℤ`. Choose `a=(i,j) ∈ ℕ²` representing `z`
(i.e. such that `cl(i,j) = ⟦(i,j)⟧ = z`)
Now apply `neg_aux` to `a`, and define `-z` to be the result.
The problem with this is that what if `b` is a different
element of the equivalence class? Then we also want `-z` to be `neg_aux b`.
Indeed, in Lean this construction is called `quotient.lift`, and
if you uncomment the below code
-/
--def neg : myint → myint :=
--quotient.lift neg_aux _
/-
you'll see an error, and if you put your cursor on the error you'll
see that Lean wants a proof that if two elements `a` and `b` are in the
same equivalence class, then `neg_aux a = neg_aux b`. So let's prove this now.
You'll need to know `quotient.sound : a ≈ b → ⟦a⟧ = ⟦b⟧`
-/
-- negation on the integers, defined via neg_aux, is well-defined.
lemma neg_aux_lemma : ∀ x y : ℕ × ℕ, x ≈ y → neg_aux x = neg_aux y :=
begin
rintro ⟨i,j⟩ ⟨k,l⟩ h,
rw [neg_aux_def, neg_aux_def],
-- ⊢ ⟦(j, i)⟧ = ⟦(l, k)⟧
-- next step: if ⟦a⟧=⟦b⟧ then a ≈ b
apply quotient.sound,
-- ⊢ (j, i) ≈ (l, k)
-- take it from here.
sorry
end
-- Note that we use `neg_aux_lemma` in the definition below
-- to justify well-definedness of `neg`
/-- Negation on on the integers. The function sending `z` to `-z`. -/
def neg : myint → myint :=
quotient.lift neg_aux neg_aux_lemma
-- notation for negation
instance : has_neg myint := ⟨neg⟩
-- We can now write `-z` if `z : myint`
-- this is true by definition
lemma neg_def (i j : ℕ) : (-⟦(i, j)⟧ : myint) = ⟦(j, i)⟧ :=
begin
sorry
end
/-! ## addition
Our final construction: we want to define addition on `myint`.
Here we have the same problem. Say z₁ and z₂ are integers.
Choose elements a₁=(i,j) and a₂=(k,l) in ℕ². We want to define
z₁ + z₂ to be ⟦(i+k,j+l)⟧, the equivalence class of a₁ + a₂.
We will need to check this is well-defined.
-/
/-- An auxiliary function taking two elements of ℕ² and returning
the equivalence class of their sum. -/
def add_aux (x y : ℕ × ℕ) : myint := ⟦(x.1 + y.1, x.2 + y.2)⟧
-- true by definition
lemma add_aux_def (i j k l : ℕ) : add_aux (i, j) (k, l) = ⟦(i + k, j + l)⟧ :=
begin
sorry
end
/-
We want the definition of addition to look like the below.
Uncomment it to see the problem.
-/
--def add : myint → myint → myint :=
--quotient.lift₂ add_aux _
/-
We had better check that choosing different elements in the same
equivalence class gives the same definition.
-/
lemma add_aux_lemma : ∀ x₁ x₂ y₁ y₂ : ℕ × ℕ,
(x₁ ≈ y₁) → (x₂ ≈ y₂) → add_aux x₁ x₂ = add_aux y₁ y₂ :=
begin
sorry
end
-- Now this is checked, we can define addition: it's well-defined.
/-- Addition on the integers -/
def add : myint → myint → myint :=
quotient.lift₂ add_aux add_aux_lemma
-- notation for addition
instance : has_add myint := ⟨add⟩
-- true by definition
lemma add_def (i j k l : ℕ) :
(⟦(i, j)⟧ + ⟦(k, l)⟧ : myint) = ⟦(i + k, j + l)⟧ :=
begin
sorry
end
/-
The four fundamental facts about addition on the integers are:
1) associativity
2) commutativity
3) zero is an additive identity
4) negation is an additive inverse.
Let's prove these now.
-/
lemma zero_add (x : myint) : 0 + x = x :=
begin
-- need to get from ℤ back to ℕ²
apply quotient.induction_on x,
sorry,
end
lemma add_zero (x : myint) : x + 0 = x :=
begin
sorry
end
lemma add_left_neg (x : myint) : -x + x = 0 :=
begin
sorry
end
-- here we need to change both x and y into elements of ℕ²
lemma add_comm (x y : myint) : x + y = y + x :=
begin
apply quotient.induction_on₂ x y,
sorry
end
lemma add_assoc (x y z : myint) : (x + y) + z = x + (y + z) :=
begin
sorry,
end
-- The lemmas above are the axioms for a commutative group.
-- Hence we just proved that the integers are a
-- commutative group under addition!
instance : add_comm_group myint :=
{ add := (+),
add_assoc := add_assoc,
zero := 0,
zero_add := zero_add,
add_zero := add_zero,
neg := has_neg.neg,
add_left_neg := add_left_neg,
add_comm := add_comm }
-- woohoo!
/-! ## multiplication
What's left to define is 1 and multiplication (note that we don't need multiplicative
inverses -- if a is a non-zero integer then a⁻¹ is typially not an integer)
-/
def mul_aux (x y : ℕ × ℕ) : myint := ⟦(sorry, sorry)⟧
-- true by definition
lemma mul_aux_def (i j k l : ℕ) : mul_aux (i, j) (k, l) = sorry :=
begin
sorry
end
-- Boss level.
-- Dr. Lawn: "We leave the similar verification for multiplication as an exercise."
-- This is what we need to check for multiplication to "descend" (or "lift" as Lean
-- calls it) to a well-defined function on the quotient.
lemma mul_aux_lemma : ∀ x₁ x₂ y₁ y₂ : ℕ × ℕ,
(x₁ ≈ y₁) → (x₂ ≈ y₂) → mul_aux x₁ x₂ = mul_aux y₁ y₂ :=
begin
sorry
end
-- It's much easier from here on
-- definition of multiplication
def mul : myint → myint → myint :=
quotient.lift₂ mul_aux mul_aux_lemma
instance : has_mul myint := ⟨mul⟩
-- true by definition
lemma mul_def (i j k l : ℕ) : (⟦(i, j)⟧ * ⟦(k, l)⟧ : myint) = ⟦(sorry, sorry)⟧ :=
begin
sorry
end
lemma mul_assoc (x y z : myint) : (x * y) * z = x * (y * z) :=
begin
sorry
end
def one : myint := ⟦(sorry, sorry)⟧
instance : has_one myint := ⟨myint.one⟩
-- true by definition
lemma one_def : (1 : myint) = sorry :=
begin
sorry
end
lemma one_mul (x : myint) : 1 * x = x :=
begin
sorry
end
lemma mul_one (x : myint) : x * 1 = x :=
begin
sorry
end
lemma mul_comm (x y : myint) : x * y = y * x :=
begin
sorry
end
lemma mul_add (x y z : myint) : x * (y + z) = x * y + x * z :=
begin
sorry
end
lemma add_mul (x y z : myint) : (x + y) * z = x * z + y * z :=
begin
sorry
end
-- The integers are a commutative ring
-- (that is, they satisfy the axioms we just proved)
instance : comm_ring myint :=
{ mul := (*),
mul_assoc := mul_assoc,
one := 1,
one_mul := one_mul,
mul_one := mul_one,
left_distrib := mul_add,
right_distrib := add_mul,
mul_comm := mul_comm,
..myint.add_comm_group }
end myint
|
50044b7543d8699df7701a01289884def8dfca62 | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /src/Init/Data/BinomialHeap.lean | 48d33def3bfcc70a75a4529770a2c6c44c6b181d | [
"Apache-2.0"
] | permissive | mhuisi/lean4 | 28d35a4febc2e251c7f05492e13f3b05d6f9b7af | dda44bc47f3e5d024508060dac2bcb59fd12e4c0 | refs/heads/master | 1,621,225,489,283 | 1,585,142,689,000 | 1,585,142,689,000 | 250,590,438 | 0 | 2 | Apache-2.0 | 1,602,443,220,000 | 1,585,327,814,000 | C | UTF-8 | Lean | false | false | 208 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Init.Data.BinomialHeap.Basic
|
7293cc5964f67f3908926df83798d4e5748a8b7b | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/ring_theory/power_basis.lean | a2399d66bbc59be6818abe341bfa4e4ca071a767 | [
"Apache-2.0"
] | permissive | waynemunro/mathlib | e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552 | 065a70810b5480d584033f7bbf8e0409480c2118 | refs/heads/master | 1,693,417,182,397 | 1,634,644,781,000 | 1,634,644,781,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 20,140 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen
-/
import field_theory.minpoly
/-!
# Power basis
This file defines a structure `power_basis R S`, giving a basis of the
`R`-algebra `S` as a finite list of powers `1, x, ..., x^n`.
For example, if `x` is algebraic over a ring/field, adjoining `x`
gives a `power_basis` structure generated by `x`.
## Definitions
* `power_basis R A`: a structure containing an `x` and an `n` such that
`1, x, ..., x^n` is a basis for the `R`-algebra `A` (viewed as an `R`-module).
* `finrank (hf : f ≠ 0) : finite_dimensional.finrank K (adjoin_root f) = f.nat_degree`,
the dimension of `adjoin_root f` equals the degree of `f`
* `power_basis.lift (pb : power_basis R S)`: if `y : S'` satisfies the same
equations as `pb.gen`, this is the map `S →ₐ[R] S'` sending `pb.gen` to `y`
* `power_basis.equiv`: if two power bases satisfy the same equations, they are
equivalent as algebras
## Implementation notes
Throughout this file, `R`, `S`, ... are `comm_ring`s, `A`, `B`, ... are
`integral_domain`s and `K`, `L`, ... are `field`s.
`S` is an `R`-algebra, `B` is an `A`-algebra, `L` is a `K`-algebra.
## Tags
power basis, powerbasis
-/
open polynomial
variables {R S T : Type*} [comm_ring R] [comm_ring S] [comm_ring T]
variables [algebra R S] [algebra S T] [algebra R T] [is_scalar_tower R S T]
variables {A B : Type*} [comm_ring A]
[comm_ring B] [integral_domain B] [algebra A B]
variables {K L : Type*} [field K] [field L] [algebra K L]
/-- `pb : power_basis R S` states that `1, pb.gen, ..., pb.gen ^ (pb.dim - 1)`
is a basis for the `R`-algebra `S` (viewed as `R`-module).
This is a structure, not a class, since the same algebra can have many power bases.
For the common case where `S` is defined by adjoining an integral element to `R`,
the canonical power basis is given by `{algebra,intermediate_field}.adjoin.power_basis`.
-/
@[nolint has_inhabited_instance]
structure power_basis (R S : Type*) [comm_ring R] [ring S] [algebra R S] :=
(gen : S)
(dim : ℕ)
(basis : basis (fin dim) R S)
(basis_eq_pow : ∀ i, basis i = gen ^ (i : ℕ))
namespace power_basis
@[simp] lemma coe_basis (pb : power_basis R S) :
⇑pb.basis = λ (i : fin pb.dim), pb.gen ^ (i : ℕ) :=
funext pb.basis_eq_pow
/-- Cannot be an instance because `power_basis` cannot be a class. -/
lemma finite_dimensional [algebra K S] (pb : power_basis K S) : finite_dimensional K S :=
finite_dimensional.of_fintype_basis pb.basis
lemma finrank [algebra K S] (pb : power_basis K S) : finite_dimensional.finrank K S = pb.dim :=
by rw [finite_dimensional.finrank_eq_card_basis pb.basis, fintype.card_fin]
lemma mem_span_pow' {x y : S} {d : ℕ} :
y ∈ submodule.span R (set.range (λ (i : fin d), x ^ (i : ℕ))) ↔
∃ f : polynomial R, f.degree < d ∧ y = aeval x f :=
begin
have : set.range (λ (i : fin d), x ^ (i : ℕ)) = (λ (i : ℕ), x ^ i) '' ↑(finset.range d),
{ ext n,
simp_rw [set.mem_range, set.mem_image, finset.mem_coe, finset.mem_range],
exact ⟨λ ⟨⟨i, hi⟩, hy⟩, ⟨i, hi, hy⟩, λ ⟨i, hi, hy⟩, ⟨⟨i, hi⟩, hy⟩⟩ },
simp only [this, finsupp.mem_span_image_iff_total, degree_lt_iff_coeff_zero,
exists_iff_exists_finsupp, coeff, aeval, eval₂_ring_hom', eval₂_eq_sum, polynomial.sum, support,
finsupp.mem_supported', finsupp.total, finsupp.sum, algebra.smul_def, eval₂_zero, exists_prop,
linear_map.id_coe, eval₂_one, id.def, not_lt, finsupp.coe_lsum, linear_map.coe_smul_right,
finset.mem_range, alg_hom.coe_mk, finset.mem_coe],
simp_rw [@eq_comm _ y],
exact iff.rfl
end
lemma mem_span_pow {x y : S} {d : ℕ} (hd : d ≠ 0) :
y ∈ submodule.span R (set.range (λ (i : fin d), x ^ (i : ℕ))) ↔
∃ f : polynomial R, f.nat_degree < d ∧ y = aeval x f :=
begin
rw mem_span_pow',
split;
{ rintros ⟨f, h, hy⟩,
refine ⟨f, _, hy⟩,
by_cases hf : f = 0,
{ simp only [hf, nat_degree_zero, degree_zero] at h ⊢,
exact lt_of_le_of_ne (nat.zero_le d) hd.symm <|> exact with_bot.bot_lt_coe d },
simpa only [degree_eq_nat_degree hf, with_bot.coe_lt_coe] using h },
end
lemma dim_ne_zero [h : nontrivial S] (pb : power_basis R S) : pb.dim ≠ 0 :=
λ h, not_nonempty_iff.mpr (h.symm ▸ fin.is_empty : is_empty (fin pb.dim)) pb.basis.index_nonempty
lemma dim_pos [nontrivial S] (pb : power_basis R S) : 0 < pb.dim :=
nat.pos_of_ne_zero pb.dim_ne_zero
lemma exists_eq_aeval [nontrivial S] (pb : power_basis R S) (y : S) :
∃ f : polynomial R, f.nat_degree < pb.dim ∧ y = aeval pb.gen f :=
(mem_span_pow pb.dim_ne_zero).mp (by simpa using pb.basis.mem_span y)
lemma exists_eq_aeval' (pb : power_basis R S) (y : S) :
∃ f : polynomial R, y = aeval pb.gen f :=
begin
nontriviality S,
obtain ⟨f, _, hf⟩ := exists_eq_aeval pb y,
exact ⟨f, hf⟩
end
lemma alg_hom_ext {S' : Type*} [semiring S'] [algebra R S']
(pb : power_basis R S) ⦃f g : S →ₐ[R] S'⦄ (h : f pb.gen = g pb.gen) :
f = g :=
begin
ext x,
obtain ⟨f, rfl⟩ := pb.exists_eq_aeval' x,
rw [← polynomial.aeval_alg_hom_apply, ← polynomial.aeval_alg_hom_apply, h]
end
section minpoly
open_locale big_operators
variable [algebra A S]
/-- `pb.minpoly_gen` is a minimal polynomial for `pb.gen`.
If `A` is not a field, it might not necessarily be *the* minimal polynomial,
however `nat_degree_minpoly` shows its degree is indeed minimal.
-/
noncomputable def minpoly_gen (pb : power_basis A S) : polynomial A :=
X ^ pb.dim -
∑ (i : fin pb.dim), C (pb.basis.repr (pb.gen ^ pb.dim) i) * X ^ (i : ℕ)
@[simp]
lemma aeval_minpoly_gen (pb : power_basis A S) : aeval pb.gen (minpoly_gen pb) = 0 :=
begin
simp_rw [minpoly_gen, alg_hom.map_sub, alg_hom.map_sum, alg_hom.map_mul, alg_hom.map_pow,
aeval_C, ← algebra.smul_def, aeval_X],
refine sub_eq_zero.mpr ((pb.basis.total_repr (pb.gen ^ pb.dim)).symm.trans _),
rw [finsupp.total_apply, finsupp.sum_fintype];
simp only [pb.coe_basis, zero_smul, eq_self_iff_true, implies_true_iff]
end
lemma dim_le_nat_degree_of_root (h : power_basis A S) {p : polynomial A}
(ne_zero : p ≠ 0) (root : aeval h.gen p = 0) :
h.dim ≤ p.nat_degree :=
begin
refine le_of_not_lt (λ hlt, ne_zero _),
let p_coeff : fin (h.dim) → A := λ i, p.coeff i,
suffices : ∀ i, p_coeff i = 0,
{ ext i,
by_cases hi : i < h.dim,
{ exact this ⟨i, hi⟩ },
exact coeff_eq_zero_of_nat_degree_lt (lt_of_lt_of_le hlt (le_of_not_gt hi)) },
intro i,
refine linear_independent_iff'.mp h.basis.linear_independent _ _ _ i (finset.mem_univ _),
rw aeval_eq_sum_range' hlt at root,
rw finset.sum_fin_eq_sum_range,
convert root,
ext i,
split_ifs with hi,
{ simp_rw [coe_basis, p_coeff, fin.coe_mk] },
{ rw [coeff_eq_zero_of_nat_degree_lt (lt_of_lt_of_le hlt (le_of_not_gt hi)),
zero_smul] }
end
lemma dim_le_degree_of_root (h : power_basis A S) {p : polynomial A}
(ne_zero : p ≠ 0) (root : aeval h.gen p = 0) :
↑h.dim ≤ p.degree :=
by { rw [degree_eq_nat_degree ne_zero, with_bot.coe_le_coe],
exact h.dim_le_nat_degree_of_root ne_zero root }
variables [integral_domain A]
@[simp]
lemma degree_minpoly_gen (pb : power_basis A S) :
degree (minpoly_gen pb) = pb.dim :=
begin
unfold minpoly_gen,
rw degree_sub_eq_left_of_degree_lt; rw degree_X_pow,
apply degree_sum_fin_lt
end
@[simp]
lemma nat_degree_minpoly_gen (pb : power_basis A S) :
nat_degree (minpoly_gen pb) = pb.dim :=
nat_degree_eq_of_degree_eq_some pb.degree_minpoly_gen
lemma minpoly_gen_monic (pb : power_basis A S) : monic (minpoly_gen pb) :=
begin
apply monic_sub_of_left (monic_pow (monic_X) _),
rw degree_X_pow,
exact degree_sum_fin_lt _
end
lemma is_integral_gen (pb : power_basis A S) : is_integral A pb.gen :=
⟨minpoly_gen pb, minpoly_gen_monic pb, aeval_minpoly_gen pb⟩
@[simp]
lemma nat_degree_minpoly (pb : power_basis A S) :
(minpoly A pb.gen).nat_degree = pb.dim :=
begin
refine le_antisymm _
(dim_le_nat_degree_of_root pb (minpoly.ne_zero pb.is_integral_gen) (minpoly.aeval _ _)),
rw ← nat_degree_minpoly_gen,
apply nat_degree_le_of_degree_le,
rw ← degree_eq_nat_degree (minpoly_gen_monic pb).ne_zero,
exact minpoly.min _ _ (minpoly_gen_monic pb) (aeval_minpoly_gen pb)
end
@[simp]
lemma minpoly_gen_eq [algebra K S] (pb : power_basis K S) :
pb.minpoly_gen = minpoly K pb.gen :=
minpoly.unique K pb.gen pb.minpoly_gen_monic pb.aeval_minpoly_gen (λ p p_monic p_root,
pb.degree_minpoly_gen.symm ▸ pb.dim_le_degree_of_root p_monic.ne_zero p_root)
end minpoly
section equiv
variables [algebra A S] {S' : Type*} [comm_ring S'] [algebra A S']
lemma nat_degree_lt_nat_degree {p q : polynomial R} (hp : p ≠ 0) (hpq : p.degree < q.degree) :
p.nat_degree < q.nat_degree :=
begin
by_cases hq : q = 0, { rw [hq, degree_zero] at hpq, have := not_lt_bot hpq, contradiction },
rwa [degree_eq_nat_degree hp, degree_eq_nat_degree hq, with_bot.coe_lt_coe] at hpq
end
variables [integral_domain A]
lemma constr_pow_aeval (pb : power_basis A S) {y : S'}
(hy : aeval y (minpoly A pb.gen) = 0) (f : polynomial A) :
pb.basis.constr A (λ i, y ^ (i : ℕ)) (aeval pb.gen f) = aeval y f :=
begin
rw [← aeval_mod_by_monic_eq_self_of_root (minpoly.monic pb.is_integral_gen) (minpoly.aeval _ _),
← @aeval_mod_by_monic_eq_self_of_root _ _ _ _ _ f _ (minpoly.monic pb.is_integral_gen) y hy],
by_cases hf : f %ₘ minpoly A pb.gen = 0,
{ simp only [hf, alg_hom.map_zero, linear_map.map_zero] },
have : (f %ₘ minpoly A pb.gen).nat_degree < pb.dim,
{ rw ← pb.nat_degree_minpoly,
apply nat_degree_lt_nat_degree hf,
exact degree_mod_by_monic_lt _ (minpoly.monic pb.is_integral_gen) },
rw [aeval_eq_sum_range' this, aeval_eq_sum_range' this, linear_map.map_sum],
refine finset.sum_congr rfl (λ i (hi : i ∈ finset.range pb.dim), _),
rw finset.mem_range at hi,
rw linear_map.map_smul,
congr,
rw [← fin.coe_mk hi, ← pb.basis_eq_pow ⟨i, hi⟩, basis.constr_basis]
end
lemma constr_pow_gen (pb : power_basis A S) {y : S'}
(hy : aeval y (minpoly A pb.gen) = 0) :
pb.basis.constr A (λ i, y ^ (i : ℕ)) pb.gen = y :=
by { convert pb.constr_pow_aeval hy X; rw aeval_X }
lemma constr_pow_algebra_map (pb : power_basis A S) {y : S'}
(hy : aeval y (minpoly A pb.gen) = 0) (x : A) :
pb.basis.constr A (λ i, y ^ (i : ℕ)) (algebra_map A S x) = algebra_map A S' x :=
by { convert pb.constr_pow_aeval hy (C x); rw aeval_C }
lemma constr_pow_mul (pb : power_basis A S) {y : S'}
(hy : aeval y (minpoly A pb.gen) = 0) (x x' : S) :
pb.basis.constr A (λ i, y ^ (i : ℕ)) (x * x') =
pb.basis.constr A (λ i, y ^ (i : ℕ)) x * pb.basis.constr A (λ i, y ^ (i : ℕ)) x' :=
begin
obtain ⟨f, rfl⟩ := pb.exists_eq_aeval' x,
obtain ⟨g, rfl⟩ := pb.exists_eq_aeval' x',
simp only [← aeval_mul, pb.constr_pow_aeval hy]
end
/-- `pb.lift y hy` is the algebra map sending `pb.gen` to `y`,
where `hy` states the higher powers of `y` are the same as the higher powers of `pb.gen`.
See `power_basis.lift_equiv` for a bundled equiv sending `⟨y, hy⟩` to the algebra map.
-/
noncomputable def lift (pb : power_basis A S) (y : S')
(hy : aeval y (minpoly A pb.gen) = 0) :
S →ₐ[A] S' :=
{ map_one' := by { convert pb.constr_pow_algebra_map hy 1 using 2; rw ring_hom.map_one },
map_zero' := by { convert pb.constr_pow_algebra_map hy 0 using 2; rw ring_hom.map_zero },
map_mul' := pb.constr_pow_mul hy,
commutes' := pb.constr_pow_algebra_map hy,
.. pb.basis.constr A (λ i, y ^ (i : ℕ)) }
@[simp] lemma lift_gen (pb : power_basis A S) (y : S')
(hy : aeval y (minpoly A pb.gen) = 0) :
pb.lift y hy pb.gen = y :=
pb.constr_pow_gen hy
@[simp] lemma lift_aeval (pb : power_basis A S) (y : S')
(hy : aeval y (minpoly A pb.gen) = 0) (f : polynomial A) :
pb.lift y hy (aeval pb.gen f) = aeval y f :=
pb.constr_pow_aeval hy f
/-- `pb.lift_equiv` states that roots of the minimal polynomial of `pb.gen` correspond to
maps sending `pb.gen` to that root.
This is the bundled equiv version of `power_basis.lift`.
If the codomain of the `alg_hom`s is an integral domain, then the roots form a multiset,
see `lift_equiv'` for the corresponding statement.
-/
@[simps]
noncomputable def lift_equiv (pb : power_basis A S) :
(S →ₐ[A] S') ≃ {y : S' // aeval y (minpoly A pb.gen) = 0} :=
{ to_fun := λ f, ⟨f pb.gen, by rw [aeval_alg_hom_apply, minpoly.aeval, f.map_zero]⟩,
inv_fun := λ y, pb.lift y y.2,
left_inv := λ f, pb.alg_hom_ext $ lift_gen _ _ _,
right_inv := λ y, subtype.ext $ lift_gen _ _ y.prop }
/-- `pb.lift_equiv'` states that elements of the root set of the minimal
polynomial of `pb.gen` correspond to maps sending `pb.gen` to that root. -/
@[simps {fully_applied := ff}]
noncomputable def lift_equiv' (pb : power_basis A S) :
(S →ₐ[A] B) ≃ {y : B // y ∈ ((minpoly A pb.gen).map (algebra_map A B)).roots} :=
pb.lift_equiv.trans ((equiv.refl _).subtype_equiv (λ x,
begin
rw [mem_roots, is_root.def, equiv.refl_apply, ← eval₂_eq_eval_map, ← aeval_def],
exact map_monic_ne_zero (minpoly.monic pb.is_integral_gen)
end))
/-- There are finitely many algebra homomorphisms `S →ₐ[A] B` if `S` is of the form `A[x]`
and `B` is an integral domain. -/
noncomputable def alg_hom.fintype (pb : power_basis A S) :
fintype (S →ₐ[A] B) :=
by letI := classical.dec_eq B; exact
fintype.of_equiv _ pb.lift_equiv'.symm
local attribute [irreducible] power_basis.lift
/-- `pb.equiv_of_root pb' h₁ h₂` is an equivalence of algebras with the same power basis,
where "the same" means that `pb` is a root of `pb'`s minimal polynomial and vice versa.
See also `power_basis.equiv_of_minpoly` which takes the hypothesis that the
minimal polynomials are identical.
-/
noncomputable def equiv_of_root
(pb : power_basis A S) (pb' : power_basis A S')
(h₁ : aeval pb.gen (minpoly A pb'.gen) = 0) (h₂ : aeval pb'.gen (minpoly A pb.gen) = 0) :
S ≃ₐ[A] S' :=
alg_equiv.of_alg_hom
(pb.lift pb'.gen h₂)
(pb'.lift pb.gen h₁)
(by { ext x, obtain ⟨f, hf, rfl⟩ := pb'.exists_eq_aeval' x, simp })
(by { ext x, obtain ⟨f, hf, rfl⟩ := pb.exists_eq_aeval' x, simp })
@[simp]
lemma equiv_of_root_aeval
(pb : power_basis A S) (pb' : power_basis A S')
(h₁ : aeval pb.gen (minpoly A pb'.gen) = 0) (h₂ : aeval pb'.gen (minpoly A pb.gen) = 0)
(f : polynomial A) :
pb.equiv_of_root pb' h₁ h₂ (aeval pb.gen f) = aeval pb'.gen f :=
pb.lift_aeval _ h₂ _
@[simp]
lemma equiv_of_root_gen
(pb : power_basis A S) (pb' : power_basis A S')
(h₁ : aeval pb.gen (minpoly A pb'.gen) = 0) (h₂ : aeval pb'.gen (minpoly A pb.gen) = 0) :
pb.equiv_of_root pb' h₁ h₂ pb.gen = pb'.gen :=
pb.lift_gen _ h₂
@[simp]
lemma equiv_of_root_symm
(pb : power_basis A S) (pb' : power_basis A S')
(h₁ : aeval pb.gen (minpoly A pb'.gen) = 0) (h₂ : aeval pb'.gen (minpoly A pb.gen) = 0) :
(pb.equiv_of_root pb' h₁ h₂).symm = pb'.equiv_of_root pb h₂ h₁ :=
rfl
/-- `pb.equiv_of_minpoly pb' h` is an equivalence of algebras with the same power basis,
where "the same" means that they have identical minimal polynomials.
See also `power_basis.equiv_of_root` which takes the hypothesis that each generator is a root of the
other basis' minimal polynomial; `power_basis.equiv_root` is more general if `A` is not a field.
-/
noncomputable def equiv_of_minpoly
(pb : power_basis A S) (pb' : power_basis A S')
(h : minpoly A pb.gen = minpoly A pb'.gen) :
S ≃ₐ[A] S' :=
pb.equiv_of_root pb' (h ▸ minpoly.aeval _ _) (h.symm ▸ minpoly.aeval _ _)
@[simp]
lemma equiv_of_minpoly_aeval
(pb : power_basis A S) (pb' : power_basis A S')
(h : minpoly A pb.gen = minpoly A pb'.gen)
(f : polynomial A) :
pb.equiv_of_minpoly pb' h (aeval pb.gen f) = aeval pb'.gen f :=
pb.equiv_of_root_aeval pb' _ _ _
@[simp]
lemma equiv_of_minpoly_gen
(pb : power_basis A S) (pb' : power_basis A S')
(h : minpoly A pb.gen = minpoly A pb'.gen) :
pb.equiv_of_minpoly pb' h pb.gen = pb'.gen :=
pb.equiv_of_root_gen pb' _ _
@[simp]
lemma equiv_of_minpoly_symm
(pb : power_basis A S) (pb' : power_basis A S')
(h : minpoly A pb.gen = minpoly A pb'.gen) :
(pb.equiv_of_minpoly pb' h).symm = pb'.equiv_of_minpoly pb h.symm :=
rfl
end equiv
end power_basis
open power_basis
/-- Useful lemma to show `x` generates a power basis:
the powers of `x` less than the degree of `x`'s minimal polynomial are linearly independent. -/
lemma is_integral.linear_independent_pow [algebra K S] {x : S} (hx : is_integral K x) :
linear_independent K (λ (i : fin (minpoly K x).nat_degree), x ^ (i : ℕ)) :=
begin
rw linear_independent_iff,
intros p hp,
set f : polynomial K := p.sum (λ i, monomial i) with hf0,
have f_def : ∀ (i : fin _), f.coeff i = p i,
{ intro i,
simp only [f, finsupp.sum, coeff_monomial, finset_sum_coeff],
rw [finset.sum_eq_single, if_pos rfl],
{ intros b _ hb,
rw if_neg (mt (λ h, _) hb),
exact fin.coe_injective h },
{ intro hi,
split_ifs; { exact finsupp.not_mem_support_iff.mp hi } } },
have f_def' : ∀ i, f.coeff i = if hi : i < _ then p ⟨i, hi⟩ else 0,
{ intro i,
split_ifs with hi,
{ exact f_def ⟨i, hi⟩ },
simp only [f, finsupp.sum, coeff_monomial, finset_sum_coeff],
apply finset.sum_eq_zero,
rintro ⟨j, hj⟩ -,
apply if_neg (mt _ hi),
rintro rfl,
exact hj },
suffices : f = 0,
{ ext i, rw [← f_def, this, coeff_zero, finsupp.zero_apply] },
contrapose hp with hf,
intro h,
have : (minpoly K x).degree ≤ f.degree,
{ apply minpoly.degree_le_of_ne_zero K x hf,
convert h,
simp_rw [finsupp.total_apply, aeval_def, hf0, finsupp.sum, eval₂_finset_sum],
apply finset.sum_congr rfl,
rintro i -,
simp only [algebra.smul_def, eval₂_monomial] },
have : ¬ (minpoly K x).degree ≤ f.degree,
{ apply not_le_of_lt,
rw [degree_eq_nat_degree (minpoly.ne_zero hx), degree_lt_iff_coeff_zero],
intros i hi,
rw [f_def' i, dif_neg],
exact hi.not_lt },
contradiction
end
lemma is_integral.mem_span_pow [nontrivial R] {x y : S} (hx : is_integral R x)
(hy : ∃ f : polynomial R, y = aeval x f) :
y ∈ submodule.span R (set.range (λ (i : fin (minpoly R x).nat_degree),
x ^ (i : ℕ))) :=
begin
obtain ⟨f, rfl⟩ := hy,
apply mem_span_pow'.mpr _,
have := minpoly.monic hx,
refine ⟨f.mod_by_monic (minpoly R x),
lt_of_lt_of_le (degree_mod_by_monic_lt _ this) degree_le_nat_degree,
_⟩,
conv_lhs { rw ← mod_by_monic_add_div f this },
simp only [add_zero, zero_mul, minpoly.aeval, aeval_add, alg_hom.map_mul]
end
namespace power_basis
section map
variables {S' : Type*} [comm_ring S'] [algebra R S']
/-- `power_basis.map pb (e : S ≃ₐ[R] S')` is the power basis for `S'` generated by `e pb.gen`. -/
@[simps]
noncomputable def map (pb : power_basis R S) (e : S ≃ₐ[R] S') : power_basis R S' :=
{ dim := pb.dim,
basis := pb.basis.map e.to_linear_equiv,
gen := e pb.gen,
basis_eq_pow :=
λ i, by rw [basis.map_apply, pb.basis_eq_pow, e.to_linear_equiv_apply, e.map_pow] }
variables [algebra A S] [algebra A S']
@[simp]
lemma minpoly_gen_map (pb : power_basis A S) (e : S ≃ₐ[A] S') :
(pb.map e).minpoly_gen = pb.minpoly_gen :=
by { dsimp only [minpoly_gen, map_dim], -- Turn `fin (pb.map e).dim` into `fin pb.dim`
simp only [linear_equiv.trans_apply, map_basis, basis.map_repr,
map_gen, alg_equiv.to_linear_equiv_apply, e.to_linear_equiv_symm, alg_equiv.map_pow,
alg_equiv.symm_apply_apply, sub_right_inj] }
variables [integral_domain A]
@[simp]
lemma equiv_of_root_map (pb : power_basis A S) (e : S ≃ₐ[A] S')
(h₁ h₂) :
pb.equiv_of_root (pb.map e) h₁ h₂ = e :=
by { ext x, obtain ⟨f, rfl⟩ := pb.exists_eq_aeval' x, simp [aeval_alg_equiv] }
@[simp]
lemma equiv_of_minpoly_map (pb : power_basis A S) (e : S ≃ₐ[A] S')
(h : minpoly A pb.gen = minpoly A (pb.map e).gen) :
pb.equiv_of_minpoly (pb.map e) h = e :=
pb.equiv_of_root_map _ _ _
end map
end power_basis
|
5ccc95cc8832be6b39ff2cc897e01c9dc171a2c6 | 88892181780ff536a81e794003fe058062f06758 | /src/100_theorems/t052.lean | 2599b502b5dcca17ee189392a2ef457081d66ca7 | [] | no_license | AtnNn/lean-sandbox | fe2c44280444e8bb8146ab8ac391c82b480c0a2e | 8c68afbdc09213173aef1be195da7a9a86060a97 | refs/heads/master | 1,623,004,395,876 | 1,579,969,507,000 | 1,579,969,507,000 | 146,666,368 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 163 | lean | import data.finset
-- The Number of Subsets of a Set
open finset
theorem t052 {α} : Π (s : finset α), card (powerset s) = 2 ^ card s
:= finset.card_powerset
|
9d94213889e0ecdacbda0ec26e74bd8d2e9ea342 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/algebra/gcd_monoid/integrally_closed.lean | 8e7d2ee0bf31ab1da611dd896b55bb0ef5954782 | [
"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 | 1,696 | lean | /-
Copyright (c) 2022 Andrew Yang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Andrew Yang
-/
import algebra.gcd_monoid.basic
import ring_theory.integrally_closed
import ring_theory.polynomial.scale_roots
import ring_theory.polynomial.eisenstein
/-!
# GCD domains are integrally closed
-/
open_locale big_operators polynomial
variables {R A : Type*} [comm_ring R] [is_domain R] [gcd_monoid R] [comm_ring A] [algebra R A]
lemma is_localization.surj_of_gcd_domain (M : submonoid R) [is_localization M A] (z : A) :
∃ a b : R, is_unit (gcd a b) ∧ z * algebra_map R A b = algebra_map R A a :=
begin
obtain ⟨x, ⟨y, hy⟩, rfl⟩ := is_localization.mk'_surjective M z,
obtain ⟨x', y', d, rfl, rfl, hu⟩ := extract_gcd x y,
use [x', y', hu],
rw [mul_comm, is_localization.mul_mk'_eq_mk'_of_mul],
convert is_localization.mk'_mul_cancel_left _ _ using 2,
{ rw [← mul_assoc, mul_comm y'], refl }, { apply_instance },
end
@[priority 100]
instance gcd_monoid.to_is_integrally_closed : is_integrally_closed R :=
⟨λ X ⟨p, hp₁, hp₂⟩, begin
obtain ⟨x, y, hg, he⟩ := is_localization.surj_of_gcd_domain (non_zero_divisors R) X,
have := polynomial.dvd_pow_nat_degree_of_eval₂_eq_zero
(is_fraction_ring.injective R $ fraction_ring R) hp₁ y x _ hp₂ (by rw [mul_comm, he]),
have : is_unit y,
{ rw [is_unit_iff_dvd_one, ← one_pow],
exact (dvd_gcd this $ dvd_refl y).trans (gcd_pow_left_dvd_pow_gcd.trans $
pow_dvd_pow_of_dvd (is_unit_iff_dvd_one.1 hg) _) },
use x * (this.unit⁻¹ : _),
erw [map_mul, ← units.coe_map_inv, eq_comm, units.eq_mul_inv_iff_mul_eq],
exact he,
end⟩
|
78fefa7efaacceddf0ee1cc9d7a08c4474dd84b7 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/category_theory/category/Cat.lean | a20b8968e00455c47cd07c4a808e6d9029bfc5d9 | [
"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,506 | lean | /-
Copyright (c) 2019 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import category_theory.concrete_category
import category_theory.discrete_category
import category_theory.eq_to_hom
/-!
# Category of categories
This file contains the definition of the category `Cat` of all categories.
In this category objects are categories and
morphisms are functors between these categories.
## Implementation notes
Though `Cat` is not a concrete category, we use `bundled` to define
its carrier type.
-/
universes v u
namespace category_theory
/-- Category of categories. -/
@[nolint check_univs] -- intended to be used with explicit universe parameters
def Cat := bundled category.{v u}
namespace Cat
instance : inhabited Cat := ⟨⟨Type u, category_theory.types⟩⟩
instance : has_coe_to_sort Cat (Type u) := ⟨bundled.α⟩
instance str (C : Cat.{v u}) : category.{v u} C := C.str
/-- Construct a bundled `Cat` from the underlying type and the typeclass. -/
def of (C : Type u) [category.{v} C] : Cat.{v u} := bundled.of C
/-- Category structure on `Cat` -/
instance category : large_category.{max v u} Cat.{v u} :=
{ hom := λ C D, C ⥤ D,
id := λ C, 𝟭 C,
comp := λ C D E F G, F ⋙ G,
id_comp' := λ C D F, by cases F; refl,
comp_id' := λ C D F, by cases F; refl,
assoc' := by intros; refl }
/-- Functor that gets the set of objects of a category. It is not
called `forget`, because it is not a faithful functor. -/
def objects : Cat.{v u} ⥤ Type u :=
{ obj := λ C, C,
map := λ C D F, F.obj }
/-- Any isomorphism in `Cat` induces an equivalence of the underlying categories. -/
def equiv_of_iso {C D : Cat} (γ : C ≅ D) : C ≌ D :=
{ functor := γ.hom,
inverse := γ.inv,
unit_iso := eq_to_iso $ eq.symm γ.hom_inv_id,
counit_iso := eq_to_iso γ.inv_hom_id }
end Cat
/--
Embedding `Type` into `Cat` as discrete categories.
This ought to be modelled as a 2-functor!
-/
@[simps]
def Type_to_Cat : Type u ⥤ Cat :=
{ obj := λ X, Cat.of (discrete X),
map := λ X Y f, discrete.functor f,
map_id' := λ X, begin apply functor.ext, tidy, end,
map_comp' := λ X Y Z f g, begin apply functor.ext, tidy, end }
instance : faithful Type_to_Cat.{u} := {}
instance : full Type_to_Cat.{u} :=
{ preimage := λ X Y F, F.obj,
witness' :=
begin
intros X Y F,
apply functor.ext,
{ intros x y f, dsimp, ext, },
{ intros x, refl, }
end }
end category_theory
|
4249dc4fe487aa52cfdae8109321c5675c213aab | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/topology/algebra/valuation.lean | f840430c07a3beb83c219088a3f5113347b948a4 | [
"Apache-2.0"
] | permissive | robertylewis/mathlib | 3d16e3e6daf5ddde182473e03a1b601d2810952c | 1d13f5b932f5e40a8308e3840f96fc882fae01f0 | refs/heads/master | 1,651,379,945,369 | 1,644,276,960,000 | 1,644,276,960,000 | 98,875,504 | 0 | 0 | Apache-2.0 | 1,644,253,514,000 | 1,501,495,700,000 | Lean | UTF-8 | Lean | false | false | 4,064 | lean | /-
Copyright (c) 2021 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot
-/
import topology.algebra.nonarchimedean.bases
import topology.algebra.uniform_filter_basis
import ring_theory.valuation.basic
/-!
# The topology on a valued ring
In this file, we define the non archimedean topology induced by a valuation on a ring.
The main definition is a `valued` type class which equips a ring with a valuation taking
values in a group with zero (living in the same universe). Other instances are then deduced from
this.
-/
open_locale classical topological_space
open set valuation
noncomputable theory
universe u
/-- A valued ring is a ring that comes equipped with a distinguished valuation.-/
class valued (R : Type u) [ring R] :=
(Γ₀ : Type u)
[grp : linear_ordered_comm_group_with_zero Γ₀]
(v : valuation R Γ₀)
attribute [instance] valued.grp
namespace valued
variables {R : Type*} [ring R] [valued R]
/-- The basis of open subgroups for the topology on a valued ring.-/
lemma subgroups_basis : ring_subgroups_basis (λ γ : (Γ₀ R)ˣ, valued.v.lt_add_subgroup γ) :=
{ inter := begin
rintros γ₀ γ₁,
use min γ₀ γ₁,
simp [valuation.lt_add_subgroup] ; tauto
end,
mul := begin
rintros γ,
cases exists_square_le γ with γ₀ h,
use γ₀,
rintro - ⟨r, s, r_in, s_in, rfl⟩,
calc v (r*s) = v r * v s : valuation.map_mul _ _ _
... < γ₀*γ₀ : mul_lt_mul₀ r_in s_in
... ≤ γ : by exact_mod_cast h
end,
left_mul := begin
rintros x γ,
rcases group_with_zero.eq_zero_or_unit (v x) with Hx | ⟨γx, Hx⟩,
{ use 1,
rintros y (y_in : v y < 1),
change v (x * y) < _,
rw [valuation.map_mul, Hx, zero_mul],
exact units.zero_lt γ },
{ simp only [image_subset_iff, set_of_subset_set_of, preimage_set_of_eq, valuation.map_mul],
use γx⁻¹*γ,
rintros y (vy_lt : v y < ↑(γx⁻¹ * γ)),
change v (x * y) < γ,
rw [valuation.map_mul, Hx, mul_comm],
rw [units.coe_mul, mul_comm] at vy_lt,
simpa using mul_inv_lt_of_lt_mul₀ vy_lt }
end,
right_mul := begin
rintros x γ,
rcases group_with_zero.eq_zero_or_unit (v x) with Hx | ⟨γx, Hx⟩,
{ use 1,
rintros y (y_in : v y < 1),
change v (y * x) < _,
rw [valuation.map_mul, Hx, mul_zero],
exact units.zero_lt γ },
{ use γx⁻¹*γ,
rintros y (vy_lt : v y < ↑(γx⁻¹ * γ)),
change v (y * x) < γ,
rw [valuation.map_mul, Hx],
rw [units.coe_mul, mul_comm] at vy_lt,
simpa using mul_inv_lt_of_lt_mul₀ vy_lt }
end }
@[priority 100]
instance : topological_space R := subgroups_basis.topology
lemma mem_nhds {s : set R} {x : R} :
(s ∈ 𝓝 x) ↔ ∃ γ : (valued.Γ₀ R)ˣ, {y | v (y - x) < γ } ⊆ s :=
by simpa [(subgroups_basis.has_basis_nhds x).mem_iff]
lemma mem_nhds_zero {s : set R} :
(s ∈ 𝓝 (0 : R)) ↔ ∃ γ : (Γ₀ R)ˣ, {x | v x < (γ : Γ₀ R) } ⊆ s :=
by simp [valued.mem_nhds, sub_zero]
lemma loc_const {x : R} (h : v x ≠ 0) : {y : R | v y = v x} ∈ 𝓝 x :=
begin
rw valued.mem_nhds,
rcases units.exists_iff_ne_zero.mpr h with ⟨γ, hx⟩,
use γ,
rw hx,
intros y y_in,
exact valuation.map_eq_of_sub_lt _ y_in
end
/-- The uniform structure on a valued ring.-/
@[priority 100]
instance uniform_space : uniform_space R := topological_add_group.to_uniform_space R
/-- A valued ring is a uniform additive group.-/
@[priority 100]
instance uniform_add_group : uniform_add_group R := topological_add_group_is_uniform
lemma cauchy_iff {F : filter R} :
cauchy F ↔ F.ne_bot ∧ ∀ γ : (Γ₀ R)ˣ, ∃ M ∈ F, ∀ x y ∈ M, v (y - x) < γ :=
begin
rw add_group_filter_basis.cauchy_iff,
apply and_congr iff.rfl,
simp_rw subgroups_basis.mem_add_group_filter_basis_iff,
split,
{ intros h γ,
exact h _ (subgroups_basis.mem_add_group_filter_basis _) },
{ rintros h - ⟨γ, rfl⟩,
exact h γ }
end
end valued
|
73e4dcb72422b9c45970392149ef7ddba6779a5d | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Attributes.lean | 004d2d8fdaa0100a8fc38ce7347fa0e901bf1abe | [
"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 | 20,484 | 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.CoreM
import Lean.MonadEnv
namespace Lean
inductive AttributeApplicationTime where
| afterTypeChecking | afterCompilation | beforeElaboration
deriving Inhabited, BEq
abbrev AttrM := CoreM
instance : MonadLift ImportM AttrM where
monadLift x := do liftM (m := IO) (x { env := (← getEnv), opts := (← getOptions) })
structure AttributeImplCore where
/-- This is used as the target for go-to-definition queries for simple attributes -/
ref : Name := by exact decl_name%
name : Name
descr : String
applicationTime := AttributeApplicationTime.afterTypeChecking
deriving Inhabited
/-- You can tag attributes with the 'local' or 'scoped' kind.
For example: `attribute [local myattr, scoped yourattr, theirattr]`.
This is used to indicate how an attribute should be scoped.
- local means that the attribute should only be applied in the current scope and forgotten once the current section, namespace, or file is closed.
- scoped means that the attribute should only be applied while the namespace is open.
- global means that the attribute should always be applied.
Note that the attribute handler (`AttributeImpl.add`) is responsible for interpreting the kind and
making sure that these kinds are respected.
-/
inductive AttributeKind
| global | local | scoped
deriving BEq, Inhabited
instance : ToString AttributeKind where
toString
| .global => "global"
| .local => "local"
| .scoped => "scoped"
structure AttributeImpl extends AttributeImplCore where
/-- This is run when the attribute is applied to a declaration `decl`. `stx` is the syntax of the attribute including arguments. -/
add (decl : Name) (stx : Syntax) (kind : AttributeKind) : AttrM Unit
erase (decl : Name) : AttrM Unit := throwError "attribute cannot be erased"
deriving Inhabited
builtin_initialize attributeMapRef : IO.Ref (PersistentHashMap Name AttributeImpl) ← IO.mkRef {}
/-- Low level attribute registration function. -/
def registerBuiltinAttribute (attr : AttributeImpl) : IO Unit := do
let m ← attributeMapRef.get
if m.contains attr.name then throw (IO.userError ("invalid builtin attribute declaration, '" ++ toString attr.name ++ "' has already been used"))
unless (← initializing) do
throw (IO.userError "failed to register attribute, attributes can only be registered during initialization")
attributeMapRef.modify fun m => m.insert attr.name attr
/-!
Helper methods for decoding the parameters of builtin attributes that are defined before `Lean.Parser`.
We have the following ones:
```
@[builtin_attr_parser] def simple := leading_parser ident >> optional ident >> optional priorityParser
/- We can't use `simple` for `class`, `instance`, `export` and `macro` because they are keywords. -/
@[builtin_attr_parser] def «class» := leading_parser "class"
@[builtin_attr_parser] def «instance» := leading_parser "instance" >> optional priorityParser
@[builtin_attr_parser] def «macro» := leading_parser "macro " >> ident
```
Note that we need the parsers for `class`, `instance`, and `macros` because they are keywords.
-/
def Attribute.Builtin.ensureNoArgs (stx : Syntax) : AttrM Unit := do
if stx.getKind == `Lean.Parser.Attr.simple && stx[1].isNone && stx[2].isNone then
return ()
else if stx.getKind == `Lean.Parser.Attr.«class» then
return ()
else match stx with
| Syntax.missing => return () -- In the elaborator, we use `Syntax.missing` when creating attribute views for simple attributes such as `class and `inline
| _ => throwErrorAt stx "unexpected attribute argument"
def Attribute.Builtin.getIdent? (stx : Syntax) : AttrM (Option Syntax) := do
if stx.getKind == `Lean.Parser.Attr.simple then
if !stx[1].isNone && stx[1][0].isIdent then
return some stx[1][0]
else
return none
/- We handle `macro` here because it is handled by the generic `KeyedDeclsAttribute -/
else if stx.getKind == `Lean.Parser.Attr.«macro» || stx.getKind == `Lean.Parser.Attr.«export» then
return some stx[1]
else
throwErrorAt stx "unexpected attribute argument"
def Attribute.Builtin.getIdent (stx : Syntax) : AttrM Syntax := do
match (← getIdent? stx) with
| some id => return id
| none => throwErrorAt stx "unexpected attribute argument, identifier expected"
def Attribute.Builtin.getId? (stx : Syntax) : AttrM (Option Name) := do
let ident? ← getIdent? stx
return Syntax.getId <$> ident?
def Attribute.Builtin.getId (stx : Syntax) : AttrM Name := do
return (← getIdent stx).getId
def getAttrParamOptPrio (optPrioStx : Syntax) : AttrM Nat :=
if optPrioStx.isNone then
return eval_prio default
else match optPrioStx[0].isNatLit? with
| some prio => return prio
| none => throwErrorAt optPrioStx "priority expected"
def Attribute.Builtin.getPrio (stx : Syntax) : AttrM Nat := do
if stx.getKind == `Lean.Parser.Attr.simple then
getAttrParamOptPrio stx[1]
else
throwErrorAt stx "unexpected attribute argument, optional priority expected"
/--
Tag attributes are simple and efficient. They are useful for marking declarations in the modules where
they were defined.
The startup cost for this kind of attribute is very small since `addImportedFn` is a constant function.
They provide the predicate `tagAttr.hasTag env decl` which returns true iff declaration `decl`
is tagged in the environment `env`. -/
structure TagAttribute where
attr : AttributeImpl
ext : PersistentEnvExtension Name Name NameSet
deriving Inhabited
def registerTagAttribute (name : Name) (descr : String)
(validate : Name → AttrM Unit := fun _ => pure ()) (ref : Name := by exact decl_name%) (applicationTime := AttributeApplicationTime.afterTypeChecking) : IO TagAttribute := do
let ext : PersistentEnvExtension Name Name NameSet ← registerPersistentEnvExtension {
name := ref
mkInitial := pure {}
addImportedFn := fun _ _ => pure {}
addEntryFn := fun (s : NameSet) n => s.insert n
exportEntriesFn := fun es =>
let r : Array Name := es.fold (fun a e => a.push e) #[]
r.qsort Name.quickLt
statsFn := fun s => "tag attribute" ++ Format.line ++ "number of local entries: " ++ format s.size
}
let attrImpl : AttributeImpl := {
ref, name, descr, applicationTime
add := fun decl stx kind => do
Attribute.Builtin.ensureNoArgs stx
unless kind == AttributeKind.global do throwError "invalid attribute '{name}', must be global"
let env ← getEnv
unless (env.getModuleIdxFor? decl).isNone do
throwError "invalid attribute '{name}', declaration is in an imported module"
validate decl
modifyEnv fun env => ext.addEntry env decl
}
registerBuiltinAttribute attrImpl
return { attr := attrImpl, ext := ext }
namespace TagAttribute
def hasTag (attr : TagAttribute) (env : Environment) (decl : Name) : Bool :=
match env.getModuleIdxFor? decl with
| some modIdx => (attr.ext.getModuleEntries env modIdx).binSearchContains decl Name.quickLt
| none => (attr.ext.getState env).contains decl
end TagAttribute
/--
A `TagAttribute` variant where we can attach parameters to attributes.
It is slightly more expensive and consumes a little bit more memory than `TagAttribute`.
They provide the function `pAttr.getParam env decl` which returns `some p` iff declaration `decl`
contains the attribute `pAttr` with parameter `p`. -/
structure ParametricAttribute (α : Type) where
attr : AttributeImpl
ext : PersistentEnvExtension (Name × α) (Name × α) (NameMap α)
deriving Inhabited
structure ParametricAttributeImpl (α : Type) extends AttributeImplCore where
/-- This is used as the target for go-to-definition queries for simple attributes -/
getParam : Name → Syntax → AttrM α
afterSet : Name → α → AttrM Unit := fun _ _ _ => pure ()
afterImport : Array (Array (Name × α)) → ImportM Unit := fun _ => pure ()
def registerParametricAttribute [Inhabited α] (impl : ParametricAttributeImpl α) : IO (ParametricAttribute α) := do
let ext : PersistentEnvExtension (Name × α) (Name × α) (NameMap α) ← registerPersistentEnvExtension {
name := impl.ref
mkInitial := pure {}
addImportedFn := fun s => impl.afterImport s *> pure {}
addEntryFn := fun (s : NameMap α) (p : Name × α) => s.insert p.1 p.2
exportEntriesFn := fun m =>
let r : Array (Name × α) := m.fold (fun a n p => a.push (n, p)) #[]
r.qsort (fun a b => Name.quickLt a.1 b.1)
statsFn := fun s => "parametric attribute" ++ Format.line ++ "number of local entries: " ++ format s.size
}
let attrImpl : AttributeImpl := {
impl.toAttributeImplCore with
add := fun decl stx kind => do
unless kind == AttributeKind.global do throwError "invalid attribute '{impl.name}', must be global"
let env ← getEnv
unless (env.getModuleIdxFor? decl).isNone do
throwError "invalid attribute '{impl.name}', declaration is in an imported module"
let val ← impl.getParam decl stx
modifyEnv fun env => ext.addEntry env (decl, val)
try impl.afterSet decl val catch _ => setEnv env
}
registerBuiltinAttribute attrImpl
pure { attr := attrImpl, ext := ext }
namespace ParametricAttribute
def getParam? [Inhabited α] (attr : ParametricAttribute α) (env : Environment) (decl : Name) : Option α :=
match env.getModuleIdxFor? decl with
| some modIdx =>
match (attr.ext.getModuleEntries env modIdx).binSearch (decl, default) (fun a b => Name.quickLt a.1 b.1) with
| some (_, val) => some val
| none => none
| none => (attr.ext.getState env).find? decl
def setParam (attr : ParametricAttribute α) (env : Environment) (decl : Name) (param : α) : Except String Environment :=
if (env.getModuleIdxFor? decl).isSome then
Except.error ("invalid '" ++ toString attr.attr.name ++ "'.setParam, declaration is in an imported module")
else if ((attr.ext.getState env).find? decl).isSome then
Except.error ("invalid '" ++ toString attr.attr.name ++ "'.setParam, attribute has already been set")
else
Except.ok (attr.ext.addEntry env (decl, param))
end ParametricAttribute
/--
Given a list `[a₁, ..., a_n]` of elements of type `α`, `EnumAttributes` provides an attribute `Attr_i` for
associating a value `a_i` with an declaration. `α` is usually an enumeration type.
Note that whenever we register an `EnumAttributes`, we create `n` attributes, but only one environment extension. -/
structure EnumAttributes (α : Type) where
attrs : List AttributeImpl
ext : PersistentEnvExtension (Name × α) (Name × α) (NameMap α)
deriving Inhabited
def registerEnumAttributes [Inhabited α] (attrDescrs : List (Name × String × α))
(validate : Name → α → AttrM Unit := fun _ _ => pure ())
(applicationTime := AttributeApplicationTime.afterTypeChecking)
(ref : Name := by exact decl_name%) : IO (EnumAttributes α) := do
let ext : PersistentEnvExtension (Name × α) (Name × α) (NameMap α) ← registerPersistentEnvExtension {
name := ref
mkInitial := pure {}
addImportedFn := fun _ _ => pure {}
addEntryFn := fun (s : NameMap α) (p : Name × α) => s.insert p.1 p.2
exportEntriesFn := fun m =>
let r : Array (Name × α) := m.fold (fun a n p => a.push (n, p)) #[]
r.qsort (fun a b => Name.quickLt a.1 b.1)
statsFn := fun s => "enumeration attribute extension" ++ Format.line ++ "number of local entries: " ++ format s.size
}
let attrs := attrDescrs.map fun (name, descr, val) => {
ref := ref
name := name
descr := descr
add := fun decl stx kind => do
Attribute.Builtin.ensureNoArgs stx
unless kind == AttributeKind.global do throwError "invalid attribute '{name}', must be global"
let env ← getEnv
unless (env.getModuleIdxFor? decl).isNone do
throwError "invalid attribute '{name}', declaration is in an imported module"
validate decl val
modifyEnv fun env => ext.addEntry env (decl, val)
applicationTime := applicationTime
: AttributeImpl
}
attrs.forM registerBuiltinAttribute
pure { ext := ext, attrs := attrs }
namespace EnumAttributes
def getValue [Inhabited α] (attr : EnumAttributes α) (env : Environment) (decl : Name) : Option α :=
match env.getModuleIdxFor? decl with
| some modIdx =>
match (attr.ext.getModuleEntries env modIdx).binSearch (decl, default) (fun a b => Name.quickLt a.1 b.1) with
| some (_, val) => some val
| none => none
| none => (attr.ext.getState env).find? decl
def setValue (attrs : EnumAttributes α) (env : Environment) (decl : Name) (val : α) : Except String Environment :=
if (env.getModuleIdxFor? decl).isSome then
Except.error ("invalid '" ++ toString attrs.ext.name ++ "'.setValue, declaration is in an imported module")
else if ((attrs.ext.getState env).find? decl).isSome then
Except.error ("invalid '" ++ toString attrs.ext.name ++ "'.setValue, attribute has already been set")
else
Except.ok (attrs.ext.addEntry env (decl, val))
end EnumAttributes
/-!
Attribute extension and builders. We use builders to implement attribute factories for parser categories.
-/
abbrev AttributeImplBuilder := Name → List DataValue → Except String AttributeImpl
abbrev AttributeImplBuilderTable := HashMap Name AttributeImplBuilder
builtin_initialize attributeImplBuilderTableRef : IO.Ref AttributeImplBuilderTable ← IO.mkRef {}
def registerAttributeImplBuilder (builderId : Name) (builder : AttributeImplBuilder) : IO Unit := do
let table ← attributeImplBuilderTableRef.get
if table.contains builderId then throw (IO.userError ("attribute implementation builder '" ++ toString builderId ++ "' has already been declared"))
attributeImplBuilderTableRef.modify fun table => table.insert builderId builder
def mkAttributeImplOfBuilder (builderId ref : Name) (args : List DataValue) : IO AttributeImpl := do
let table ← attributeImplBuilderTableRef.get
match table.find? builderId with
| none => throw (IO.userError ("unknown attribute implementation builder '" ++ toString builderId ++ "'"))
| some builder => IO.ofExcept <| builder ref args
inductive AttributeExtensionOLeanEntry where
| decl (declName : Name) -- `declName` has type `AttributeImpl`
| builder (builderId ref : Name) (args : List DataValue)
structure AttributeExtensionState where
newEntries : List AttributeExtensionOLeanEntry := []
map : PersistentHashMap Name AttributeImpl
deriving Inhabited
abbrev AttributeExtension := PersistentEnvExtension AttributeExtensionOLeanEntry (AttributeExtensionOLeanEntry × AttributeImpl) AttributeExtensionState
private def AttributeExtension.mkInitial : IO AttributeExtensionState := do
let map ← attributeMapRef.get
pure { map := map }
unsafe def mkAttributeImplOfConstantUnsafe (env : Environment) (opts : Options) (declName : Name) : Except String AttributeImpl :=
match env.find? declName with
| none => throw ("unknow constant '" ++ toString declName ++ "'")
| some info =>
match info.type with
| Expr.const `Lean.AttributeImpl _ => env.evalConst AttributeImpl opts declName
| _ => throw ("unexpected attribute implementation type at '" ++ toString declName ++ "' (`AttributeImpl` expected")
@[implemented_by mkAttributeImplOfConstantUnsafe]
opaque mkAttributeImplOfConstant (env : Environment) (opts : Options) (declName : Name) : Except String AttributeImpl
def mkAttributeImplOfEntry (env : Environment) (opts : Options) (e : AttributeExtensionOLeanEntry) : IO AttributeImpl :=
match e with
| .decl declName => IO.ofExcept <| mkAttributeImplOfConstant env opts declName
| .builder builderId ref args => mkAttributeImplOfBuilder builderId ref args
private def AttributeExtension.addImported (es : Array (Array AttributeExtensionOLeanEntry)) : ImportM AttributeExtensionState := do
let ctx ← read
let map ← attributeMapRef.get
let map ← es.foldlM
(fun map entries =>
entries.foldlM
(fun (map : PersistentHashMap Name AttributeImpl) entry => do
let attrImpl ← mkAttributeImplOfEntry ctx.env ctx.opts entry
return map.insert attrImpl.name attrImpl)
map)
map
pure { map := map }
private def addAttrEntry (s : AttributeExtensionState) (e : AttributeExtensionOLeanEntry × AttributeImpl) : AttributeExtensionState :=
{ s with map := s.map.insert e.2.name e.2, newEntries := e.1 :: s.newEntries }
builtin_initialize attributeExtension : AttributeExtension ←
registerPersistentEnvExtension {
mkInitial := AttributeExtension.mkInitial
addImportedFn := AttributeExtension.addImported
addEntryFn := addAttrEntry
exportEntriesFn := fun s => s.newEntries.reverse.toArray
statsFn := fun s => format "number of local entries: " ++ format s.newEntries.length
}
/-- Return true iff `n` is the name of a registered attribute. -/
@[export lean_is_attribute]
def isBuiltinAttribute (n : Name) : IO Bool := do
let m ← attributeMapRef.get; pure (m.contains n)
/-- Return the name of all registered attributes. -/
def getBuiltinAttributeNames : IO (List Name) :=
return (← attributeMapRef.get).foldl (init := []) fun r n _ => n::r
def getBuiltinAttributeImpl (attrName : Name) : IO AttributeImpl := do
let m ← attributeMapRef.get
match m.find? attrName with
| some attr => pure attr
| none => throw (IO.userError ("unknown attribute '" ++ toString attrName ++ "'"))
@[export lean_attribute_application_time]
def getBuiltinAttributeApplicationTime (n : Name) : IO AttributeApplicationTime := do
let attr ← getBuiltinAttributeImpl n
pure attr.applicationTime
def isAttribute (env : Environment) (attrName : Name) : Bool :=
(attributeExtension.getState env).map.contains attrName
def getAttributeNames (env : Environment) : List Name :=
let m := (attributeExtension.getState env).map
m.foldl (fun r n _ => n::r) []
def getAttributeImpl (env : Environment) (attrName : Name) : Except String AttributeImpl :=
let m := (attributeExtension.getState env).map
match m.find? attrName with
| some attr => pure attr
| none => throw ("unknown attribute '" ++ toString attrName ++ "'")
def registerAttributeOfDecl (env : Environment) (opts : Options) (attrDeclName : Name) : Except String Environment := do
let attrImpl ← mkAttributeImplOfConstant env opts attrDeclName
if isAttribute env attrImpl.name then
throw ("invalid builtin attribute declaration, '" ++ toString attrImpl.name ++ "' has already been used")
else
return attributeExtension.addEntry env (.decl attrDeclName, attrImpl)
def registerAttributeOfBuilder (env : Environment) (builderId ref : Name) (args : List DataValue) : IO Environment := do
let attrImpl ← mkAttributeImplOfBuilder builderId ref args
if isAttribute env attrImpl.name then
throw (IO.userError ("invalid builtin attribute declaration, '" ++ toString attrImpl.name ++ "' has already been used"))
else
return attributeExtension.addEntry env (.builder builderId ref args, attrImpl)
def Attribute.add (declName : Name) (attrName : Name) (stx : Syntax) (kind := AttributeKind.global) : AttrM Unit := do
let attr ← ofExcept <| getAttributeImpl (← getEnv) attrName
attr.add declName stx kind
def Attribute.erase (declName : Name) (attrName : Name) : AttrM Unit := do
let attr ← ofExcept <| getAttributeImpl (← getEnv) attrName
attr.erase declName
/-- `updateEnvAttributes` implementation -/
@[export lean_update_env_attributes]
def updateEnvAttributesImpl (env : Environment) : IO Environment := do
let map ← attributeMapRef.get
let s := attributeExtension.getState env
let s := map.foldl (init := s) fun s attrName attrImpl =>
if s.map.contains attrName then
s
else
{ s with map := s.map.insert attrName attrImpl }
return attributeExtension.setState env s
/-- `getNumBuiltinAttributes` implementation -/
@[export lean_get_num_attributes] def getNumBuiltinAttributesImpl : IO Nat :=
return (← attributeMapRef.get).size
end Lean
|
ad92cb7a0729f839fcb68cca75d4b296974ee74e | bb31430994044506fa42fd667e2d556327e18dfe | /src/topology/algebra/order/proj_Icc.lean | a9c3162be3515232d92c38796642748bf4e996f0 | [
"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 | 2,395 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov, Patrick Massot
-/
import data.set.intervals.proj_Icc
import topology.order.basic
/-!
# Projection onto a closed interval
In this file we prove that the projection `set.proj_Icc f a b h` is a quotient map, and use it
to show that `Icc_extend h f` is continuous if and only if `f` is continuous.
-/
open set filter
open_locale filter topological_space
variables {α β γ : Type*} [linear_order α] [topological_space γ] {a b c : α} {h : a ≤ b}
lemma filter.tendsto.Icc_extend (f : γ → Icc a b → β) {z : γ} {l : filter α} {l' : filter β}
(hf : tendsto ↿f (𝓝 z ×ᶠ l.map (proj_Icc a b h)) l') :
tendsto ↿(Icc_extend h ∘ f) (𝓝 z ×ᶠ l) l' :=
show tendsto (↿f ∘ prod.map id (proj_Icc a b h)) (𝓝 z ×ᶠ l) l', from
hf.comp $ tendsto_id.prod_map tendsto_map
variables [topological_space α] [order_topology α] [topological_space β]
@[continuity]
lemma continuous_proj_Icc : continuous (proj_Icc a b h) :=
(continuous_const.max $ continuous_const.min continuous_id).subtype_mk _
lemma quotient_map_proj_Icc : quotient_map (proj_Icc a b h) :=
quotient_map_iff.2 ⟨proj_Icc_surjective h, λ s,
⟨λ hs, hs.preimage continuous_proj_Icc,
λ hs, ⟨_, hs, by { ext, simp }⟩⟩⟩
@[simp] lemma continuous_Icc_extend_iff {f : Icc a b → β} :
continuous (Icc_extend h f) ↔ continuous f :=
quotient_map_proj_Icc.continuous_iff.symm
/-- See Note [continuity lemma statement]. -/
lemma continuous.Icc_extend {f : γ → Icc a b → β} {g : γ → α}
(hf : continuous ↿f) (hg : continuous g) : continuous (λ a, Icc_extend h (f a) (g a)) :=
hf.comp $ continuous_id.prod_mk $ continuous_proj_Icc.comp hg
/-- A useful special case of `continuous.Icc_extend`. -/
@[continuity]
lemma continuous.Icc_extend' {f : Icc a b → β} (hf : continuous f) : continuous (Icc_extend h f) :=
hf.comp continuous_proj_Icc
lemma continuous_at.Icc_extend {x : γ} (f : γ → Icc a b → β) {g : γ → α}
(hf : continuous_at ↿f (x, proj_Icc a b h (g x))) (hg : continuous_at g x) :
continuous_at (λ a, Icc_extend h (f a) (g a)) x :=
show continuous_at (↿f ∘ λ x, (x, proj_Icc a b h (g x))) x, from
continuous_at.comp hf $ continuous_at_id.prod $ continuous_proj_Icc.continuous_at.comp hg
|
7126205a67d90ecba2c8c91f45df57ff2925a9d3 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/dynamics/ergodic/conservative.lean | 94280422b3db54369822bbe37fe23005814e6e4c | [
"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,029 | lean | /-
Copyright (c) 2021 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import measure_theory.constructions.borel_space
import dynamics.ergodic.measure_preserving
import combinatorics.pigeonhole
/-!
# Conservative systems
In this file we define `f : α → α` to be a *conservative* system w.r.t a measure `μ` if `f` is
non-singular (`measure_theory.quasi_measure_preserving`) and for every measurable set `s` of
positive measure at least one point `x ∈ s` returns back to `s` after some number of iterations of
`f`. There are several properties that look like they are stronger than this one but actually follow
from it:
* `measure_theory.conservative.frequently_measure_inter_ne_zero`,
`measure_theory.conservative.exists_gt_measure_inter_ne_zero`: if `μ s ≠ 0`, then for infinitely
many `n`, the measure of `s ∩ (f^[n]) ⁻¹' s` is positive.
* `measure_theory.conservative.measure_mem_forall_ge_image_not_mem_eq_zero`,
`measure_theory.conservative.ae_mem_imp_frequently_image_mem`: a.e. every point of `s` visits `s`
infinitely many times (Poincaré recurrence theorem).
We also prove the topological Poincaré recurrence theorem
`measure_theory.conservative.ae_frequently_mem_of_mem_nhds`. Let `f : α → α` be a conservative
dynamical system on a topological space with second countable topology and measurable open
sets. Then almost every point `x : α` is recurrent: it visits every neighborhood `s ∈ 𝓝 x`
infinitely many times.
## Tags
conservative dynamical system, Poincare recurrence theorem
-/
noncomputable theory
open classical set filter measure_theory finset function topological_space
open_locale classical topology
variables {ι : Type*} {α : Type*} [measurable_space α] {f : α → α} {s : set α} {μ : measure α}
namespace measure_theory
open measure
/-- We say that a non-singular (`measure_theory.quasi_measure_preserving`) self-map is
*conservative* if for any measurable set `s` of positive measure there exists `x ∈ s` such that `x`
returns back to `s` under some iteration of `f`. -/
structure conservative (f : α → α) (μ : measure α . volume_tac)
extends quasi_measure_preserving f μ μ : Prop :=
(exists_mem_image_mem : ∀ ⦃s⦄, measurable_set s → μ s ≠ 0 → ∃ (x ∈ s) (m ≠ 0), f^[m] x ∈ s)
/-- A self-map preserving a finite measure is conservative. -/
protected lemma measure_preserving.conservative [is_finite_measure μ]
(h : measure_preserving f μ μ) :
conservative f μ :=
⟨h.quasi_measure_preserving, λ s hsm h0, h.exists_mem_image_mem hsm h0⟩
namespace conservative
/-- The identity map is conservative w.r.t. any measure. -/
protected lemma id (μ : measure α) : conservative id μ :=
{ to_quasi_measure_preserving := quasi_measure_preserving.id μ,
exists_mem_image_mem := λ s hs h0, let ⟨x, hx⟩ := nonempty_of_measure_ne_zero h0 in
⟨x, hx, 1, one_ne_zero, hx⟩ }
/-- If `f` is a conservative map and `s` is a measurable set of nonzero measure, then
for infinitely many values of `m` a positive measure of points `x ∈ s` returns back to `s`
after `m` iterations of `f`. -/
lemma frequently_measure_inter_ne_zero (hf : conservative f μ) (hs : measurable_set s)
(h0 : μ s ≠ 0) :
∃ᶠ m in at_top, μ (s ∩ (f^[m]) ⁻¹' s) ≠ 0 :=
begin
by_contra H, simp only [not_frequently, eventually_at_top, ne.def, not_not] at H,
rcases H with ⟨N, hN⟩,
induction N with N ihN,
{ apply h0, simpa using hN 0 le_rfl },
rw [imp_false] at ihN, push_neg at ihN,
rcases ihN with ⟨n, hn, hμn⟩,
set T := s ∩ ⋃ n ≥ N + 1, (f^[n]) ⁻¹' s,
have hT : measurable_set T,
from hs.inter (measurable_set.bUnion (to_countable _)
(λ _ _, hf.measurable.iterate _ hs)),
have hμT : μ T = 0,
{ convert (measure_bUnion_null_iff $ to_countable _).2 hN,
rw ←inter_Union₂, refl },
have : μ ((s ∩ (f^[n]) ⁻¹' s) \ T) ≠ 0, by rwa [measure_diff_null hμT],
rcases hf.exists_mem_image_mem ((hs.inter (hf.measurable.iterate n hs)).diff hT) this
with ⟨x, ⟨⟨hxs, hxn⟩, hxT⟩, m, hm0, ⟨hxms, hxm⟩, hxx⟩,
refine hxT ⟨hxs, mem_Union₂.2 ⟨n + m, _, _⟩⟩,
{ exact add_le_add hn (nat.one_le_of_lt $ pos_iff_ne_zero.2 hm0) },
{ rwa [set.mem_preimage, ← iterate_add_apply] at hxm }
end
/-- If `f` is a conservative map and `s` is a measurable set of nonzero measure, then
for an arbitrarily large `m` a positive measure of points `x ∈ s` returns back to `s`
after `m` iterations of `f`. -/
lemma exists_gt_measure_inter_ne_zero (hf : conservative f μ) (hs : measurable_set s) (h0 : μ s ≠ 0)
(N : ℕ) :
∃ m > N, μ (s ∩ (f^[m]) ⁻¹' s) ≠ 0 :=
let ⟨m, hm, hmN⟩ :=
((hf.frequently_measure_inter_ne_zero hs h0).and_eventually (eventually_gt_at_top N)).exists
in ⟨m, hmN, hm⟩
/-- Poincaré recurrence theorem: given a conservative map `f` and a measurable set `s`, the set
of points `x ∈ s` such that `x` does not return to `s` after `≥ n` iterations has measure zero. -/
lemma measure_mem_forall_ge_image_not_mem_eq_zero (hf : conservative f μ) (hs : measurable_set s)
(n : ℕ) :
μ {x ∈ s | ∀ m ≥ n, f^[m] x ∉ s} = 0 :=
begin
by_contradiction H,
have : measurable_set (s ∩ {x | ∀ m ≥ n, f^[m] x ∉ s}),
{ simp only [set_of_forall, ← compl_set_of],
exact hs.inter (measurable_set.bInter (to_countable _)
(λ m _, hf.measurable.iterate m hs.compl)) },
rcases (hf.exists_gt_measure_inter_ne_zero this H) n with ⟨m, hmn, hm⟩,
rcases nonempty_of_measure_ne_zero hm with ⟨x, ⟨hxs, hxn⟩, hxm, -⟩,
exact hxn m hmn.lt.le hxm
end
/-- Poincaré recurrence theorem: given a conservative map `f` and a measurable set `s`,
almost every point `x ∈ s` returns back to `s` infinitely many times. -/
lemma ae_mem_imp_frequently_image_mem (hf : conservative f μ) (hs : measurable_set s) :
∀ᵐ x ∂μ, x ∈ s → ∃ᶠ n in at_top, (f^[n] x) ∈ s :=
begin
simp only [frequently_at_top, @forall_swap (_ ∈ s), ae_all_iff],
intro n,
filter_upwards [measure_zero_iff_ae_nmem.1 (hf.measure_mem_forall_ge_image_not_mem_eq_zero hs n)],
simp,
end
lemma inter_frequently_image_mem_ae_eq (hf : conservative f μ) (hs : measurable_set s) :
(s ∩ {x | ∃ᶠ n in at_top, f^[n] x ∈ s} : set α) =ᵐ[μ] s :=
inter_eventually_eq_left.2 $ hf.ae_mem_imp_frequently_image_mem hs
lemma measure_inter_frequently_image_mem_eq (hf : conservative f μ) (hs : measurable_set s) :
μ (s ∩ {x | ∃ᶠ n in at_top, f^[n] x ∈ s}) = μ s :=
measure_congr (hf.inter_frequently_image_mem_ae_eq hs)
/-- Poincaré recurrence theorem: if `f` is a conservative dynamical system and `s` is a measurable
set, then for `μ`-a.e. `x`, if the orbit of `x` visits `s` at least once, then it visits `s`
infinitely many times. -/
lemma ae_forall_image_mem_imp_frequently_image_mem (hf : conservative f μ) (hs : measurable_set s) :
∀ᵐ x ∂μ, ∀ k, f^[k] x ∈ s → ∃ᶠ n in at_top, (f^[n] x) ∈ s :=
begin
refine ae_all_iff.2 (λ k, _),
refine (hf.ae_mem_imp_frequently_image_mem (hf.measurable.iterate k hs)).mono (λ x hx hk, _),
rw [← map_add_at_top_eq_nat k, frequently_map],
refine (hx hk).mono (λ n hn, _),
rwa [add_comm, iterate_add_apply]
end
/-- If `f` is a conservative self-map and `s` is a measurable set of positive measure, then
`μ.ae`-frequently we have `x ∈ s` and `s` returns to `s` under infinitely many iterations of `f`. -/
lemma frequently_ae_mem_and_frequently_image_mem (hf : conservative f μ) (hs : measurable_set s)
(h0 : μ s ≠ 0) :
∃ᵐ x ∂μ, x ∈ s ∧ ∃ᶠ n in at_top, (f^[n] x) ∈ s :=
((frequently_ae_mem_iff.2 h0).and_eventually (hf.ae_mem_imp_frequently_image_mem hs)).mono $ λ x hx,
⟨hx.1, hx.2 hx.1⟩
/-- Poincaré recurrence theorem. Let `f : α → α` be a conservative dynamical system on a topological
space with second countable topology and measurable open sets. Then almost every point `x : α`
is recurrent: it visits every neighborhood `s ∈ 𝓝 x` infinitely many times. -/
lemma ae_frequently_mem_of_mem_nhds [topological_space α] [second_countable_topology α]
[opens_measurable_space α] {f : α → α} {μ : measure α} (h : conservative f μ) :
∀ᵐ x ∂μ, ∀ s ∈ 𝓝 x, ∃ᶠ n in at_top, f^[n] x ∈ s :=
begin
have : ∀ s ∈ countable_basis α, ∀ᵐ x ∂μ, x ∈ s → ∃ᶠ n in at_top, (f^[n] x) ∈ s,
from λ s hs, h.ae_mem_imp_frequently_image_mem
(is_open_of_mem_countable_basis hs).measurable_set,
refine ((ae_ball_iff $ countable_countable_basis α).2 this).mono (λ x hx s hs, _),
rcases (is_basis_countable_basis α).mem_nhds_iff.1 hs with ⟨o, hoS, hxo, hos⟩,
exact (hx o hoS hxo).mono (λ n hn, hos hn)
end
/-- Iteration of a conservative system is a conservative system. -/
protected lemma iterate (hf : conservative f μ) (n : ℕ) : conservative (f^[n]) μ :=
begin
cases n, { exact conservative.id μ }, -- Discharge the trivial case `n = 0`
refine ⟨hf.1.iterate _, λ s hs hs0, _⟩,
rcases (hf.frequently_ae_mem_and_frequently_image_mem hs hs0).exists with ⟨x, hxs, hx⟩,
/- We take a point `x ∈ s` such that `f^[k] x ∈ s` for infinitely many values of `k`,
then we choose two of these values `k < l` such that `k ≡ l [MOD (n + 1)]`.
Then `f^[k] x ∈ s` and `(f^[n + 1])^[(l - k) / (n + 1)] (f^[k] x) = f^[l] x ∈ s`. -/
rw nat.frequently_at_top_iff_infinite at hx,
rcases nat.exists_lt_modeq_of_infinite hx n.succ_pos with ⟨k, hk, l, hl, hkl, hn⟩,
set m := (l - k) / (n + 1),
have : (n + 1) * m = l - k,
{ apply nat.mul_div_cancel',
exact (nat.modeq_iff_dvd' hkl.le).1 hn },
refine ⟨f^[k] x, hk, m, _, _⟩,
{ intro hm,
rw [hm, mul_zero, eq_comm, tsub_eq_zero_iff_le] at this,
exact this.not_lt hkl },
{ rwa [← iterate_mul, this, ← iterate_add_apply, tsub_add_cancel_of_le],
exact hkl.le }
end
end conservative
end measure_theory
|
0aa9f19dc1768088b74f77485398c858747ed59b | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/mrw.lean | 1d31aa4c1a9c4bdd5dc3469edc7e7c8db8a5bfc0 | [
"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 | 142 | lean | example (n : nat) : ∃ x, x + n = n + 1 :=
begin
constructor,
fail_if_success {rw [nat.zero_add] {unify := ff}},
rw [nat.add_comm]
end
|
27ef3797daf6f75df3ffb2ceb36a1ef46ad30925 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/analysis/normed_space/triv_sq_zero_ext.lean | 4f10169ef1883c7977226823127c6a81d50141d7 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 7,389 | lean | /-
Copyright (c) 2023 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
import analysis.normed_space.basic
import analysis.normed_space.exponential
import topology.instances.triv_sq_zero_ext
/-!
# Results on `triv_sq_zero_ext R M` related to the norm
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
For now, this file contains results about `exp` for this type.
## Main results
* `triv_sq_zero_ext.fst_exp`
* `triv_sq_zero_ext.snd_exp`
* `triv_sq_zero_ext.exp_inl`
* `triv_sq_zero_ext.exp_inr`
## TODO
* Actually define a sensible norm on `triv_sq_zero_ext R M`, so that we have access to lemmas
like `exp_add`.
* Generalize more of these results to non-commutative `R`. In principle, under sufficient conditions
we should expect
`(exp 𝕜 x).snd = ∫ t in 0..1, exp 𝕜 (t • x.fst) • op (exp 𝕜 ((1 - t) • x.fst)) • x.snd`
([Physics.SE](https://physics.stackexchange.com/a/41671/185147), and
https://link.springer.com/chapter/10.1007/978-3-540-44953-9_2).
-/
variables (𝕜 : Type*) {R M : Type*}
local notation `tsze` := triv_sq_zero_ext
namespace triv_sq_zero_ext
section topology
variables [topological_space R] [topological_space M]
/-- If `exp R x.fst` converges to `e` then `(exp R x).fst` converges to `e`. -/
lemma has_sum_fst_exp_series [field 𝕜] [ring R]
[add_comm_group M] [algebra 𝕜 R]
[module R M] [module Rᵐᵒᵖ M] [smul_comm_class R Rᵐᵒᵖ M]
[module 𝕜 M] [is_scalar_tower 𝕜 R M] [is_scalar_tower 𝕜 Rᵐᵒᵖ M]
[topological_ring R] [topological_add_group M]
[has_continuous_smul R M] [has_continuous_smul Rᵐᵒᵖ M]
(x : tsze R M)
{e : R} (h : has_sum (λ n, exp_series 𝕜 R n (λ _, x.fst)) e) :
has_sum (λ n, fst (exp_series 𝕜 (tsze R M) n (λ _, x))) e :=
by simpa [exp_series_apply_eq] using h
/-- If `exp R x.fst` converges to `e` then `(exp R x).snd` converges to `e • x.snd`. -/
lemma has_sum_snd_exp_series_of_smul_comm [field 𝕜] [char_zero 𝕜] [ring R]
[add_comm_group M] [algebra 𝕜 R]
[module R M] [module Rᵐᵒᵖ M] [smul_comm_class R Rᵐᵒᵖ M]
[module 𝕜 M] [is_scalar_tower 𝕜 R M] [is_scalar_tower 𝕜 Rᵐᵒᵖ M]
[topological_ring R] [topological_add_group M]
[has_continuous_smul R M] [has_continuous_smul Rᵐᵒᵖ M]
(x : tsze R M) (hx : mul_opposite.op x.fst • x.snd = x.fst • x.snd)
{e : R} (h : has_sum (λ n, exp_series 𝕜 R n (λ _, x.fst)) e) :
has_sum (λ n, snd (exp_series 𝕜 (tsze R M) n (λ _, x))) (e • x.snd) :=
begin
simp_rw [exp_series_apply_eq] at *,
conv
{ congr,
funext,
rw [snd_smul, snd_pow_of_smul_comm _ _ hx, nsmul_eq_smul_cast 𝕜 n, smul_smul, inv_mul_eq_div,
←inv_div, ←smul_assoc], },
apply has_sum.smul_const,
rw [←has_sum_nat_add_iff' 1], swap, apply_instance,
rw [finset.range_one, finset.sum_singleton, nat.cast_zero, div_zero, inv_zero, zero_smul,
sub_zero],
simp_rw [←nat.succ_eq_add_one, nat.pred_succ, nat.factorial_succ, nat.cast_mul,
←nat.succ_eq_add_one,
mul_div_cancel_left _ ((@nat.cast_ne_zero 𝕜 _ _ _).mpr $ nat.succ_ne_zero _)],
exact h,
end
/-- If `exp R x.fst` converges to `e` then `exp R x` converges to `inl e + inr (e • x.snd)`. -/
lemma has_sum_exp_series_of_smul_comm [field 𝕜] [char_zero 𝕜] [ring R]
[add_comm_group M] [algebra 𝕜 R]
[module R M] [module Rᵐᵒᵖ M] [smul_comm_class R Rᵐᵒᵖ M]
[module 𝕜 M] [is_scalar_tower 𝕜 R M] [is_scalar_tower 𝕜 Rᵐᵒᵖ M]
[topological_ring R] [topological_add_group M]
[has_continuous_smul R M] [has_continuous_smul Rᵐᵒᵖ M]
(x : tsze R M) (hx : mul_opposite.op x.fst • x.snd = x.fst • x.snd)
{e : R} (h : has_sum (λ n, exp_series 𝕜 R n (λ _, x.fst)) e) :
has_sum (λ n, exp_series 𝕜 (tsze R M) n (λ _, x)) (inl e + inr (e • x.snd)) :=
by simpa only [inl_fst_add_inr_snd_eq] using
(has_sum_inl _ $ has_sum_fst_exp_series 𝕜 x h).add
(has_sum_inr _ $ has_sum_snd_exp_series_of_smul_comm 𝕜 x hx h)
end topology
section normed_ring
variables [is_R_or_C 𝕜] [normed_ring R] [add_comm_group M]
variables [normed_algebra 𝕜 R] [module R M] [module Rᵐᵒᵖ M] [smul_comm_class R Rᵐᵒᵖ M]
variables [module 𝕜 M] [is_scalar_tower 𝕜 R M] [is_scalar_tower 𝕜 Rᵐᵒᵖ M]
variables [topological_space M] [topological_ring R]
variables [topological_add_group M] [has_continuous_smul R M] [has_continuous_smul Rᵐᵒᵖ M]
variables [complete_space R] [t2_space R] [t2_space M]
lemma exp_def_of_smul_comm (x : tsze R M) (hx : mul_opposite.op x.fst • x.snd = x.fst • x.snd) :
exp 𝕜 x = inl (exp 𝕜 x.fst) + inr (exp 𝕜 x.fst • x.snd) :=
begin
simp_rw [exp, formal_multilinear_series.sum],
refine (has_sum_exp_series_of_smul_comm 𝕜 x hx _).tsum_eq,
exact exp_series_has_sum_exp _,
end
@[simp] lemma exp_inl (x : R) : exp 𝕜 (inl x : tsze R M) = inl (exp 𝕜 x) :=
begin
rw [exp_def_of_smul_comm, snd_inl, fst_inl, smul_zero, inr_zero, add_zero],
{ rw [snd_inl, fst_inl, smul_zero, smul_zero] }
end
@[simp] lemma exp_inr (m : M) : exp 𝕜 (inr m : tsze R M) = 1 + inr m :=
begin
rw [exp_def_of_smul_comm, snd_inr, fst_inr, exp_zero, one_smul, inl_one],
{ rw [snd_inr, fst_inr, mul_opposite.op_zero, zero_smul, zero_smul] }
end
end normed_ring
section normed_comm_ring
variables [is_R_or_C 𝕜] [normed_comm_ring R] [add_comm_group M]
variables [normed_algebra 𝕜 R] [module R M] [module Rᵐᵒᵖ M] [is_central_scalar R M]
variables [module 𝕜 M] [is_scalar_tower 𝕜 R M]
variables [topological_space M] [topological_ring R]
variables [topological_add_group M] [has_continuous_smul R M]
variables [complete_space R] [t2_space R] [t2_space M]
lemma exp_def (x : tsze R M) : exp 𝕜 x = inl (exp 𝕜 x.fst) + inr (exp 𝕜 x.fst • x.snd) :=
exp_def_of_smul_comm 𝕜 x (op_smul_eq_smul _ _)
@[simp] lemma fst_exp (x : tsze R M) : fst (exp 𝕜 x) = exp 𝕜 x.fst :=
by rw [exp_def, fst_add, fst_inl, fst_inr, add_zero]
@[simp] lemma snd_exp (x : tsze R M) : snd (exp 𝕜 x) = exp 𝕜 x.fst • x.snd :=
by rw [exp_def, snd_add, snd_inl, snd_inr, zero_add]
/-- Polar form of trivial-square-zero extension. -/
lemma eq_smul_exp_of_invertible (x : tsze R M) [invertible x.fst] :
x = x.fst • exp 𝕜 (⅟x.fst • inr x.snd) :=
by rw [←inr_smul, exp_inr, smul_add, ←inl_one, ←inl_smul, ←inr_smul, smul_eq_mul, mul_one,
smul_smul, mul_inv_of_self, one_smul, inl_fst_add_inr_snd_eq]
end normed_comm_ring
section normed_field
variables [is_R_or_C 𝕜] [normed_field R] [add_comm_group M]
variables [normed_algebra 𝕜 R] [module R M] [module Rᵐᵒᵖ M] [is_central_scalar R M]
variables [module 𝕜 M] [is_scalar_tower 𝕜 R M]
variables [topological_space M] [topological_ring R]
variables [topological_add_group M] [has_continuous_smul R M]
variables [complete_space R] [t2_space R] [t2_space M]
/-- More convenient version of `triv_sq_zero_ext.eq_smul_exp_of_invertible` for when `R` is a
field. -/
lemma eq_smul_exp_of_ne_zero (x : tsze R M) (hx : x.fst ≠ 0) :
x = x.fst • exp 𝕜 (x.fst⁻¹ • inr x.snd) :=
begin
letI : invertible x.fst := invertible_of_nonzero hx,
exact eq_smul_exp_of_invertible _ _
end
end normed_field
end triv_sq_zero_ext
|
b0a8af2a55e9cd525538f47027ebb1b14fbd7361 | 367134ba5a65885e863bdc4507601606690974c1 | /test/where.lean | 64d74383dcadaf9b45790eb4a71f1e7e45949947 | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 8,408 | lean | import data.multiset.basic
import tactic.where
-- First set up the testing framework...
section framework
-- Note that we cannot have any explicit `open`s, we are currently working around this bug:
-- https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/What's.20the.20deal.20with.20.60open.60
@[user_command]
meta def run_parser_from_command_cmd
(_ : interactive.parse $ lean.parser.tk "run_parser_from_command")
: lean.parser unit :=
do ns ← lean.parser.ident,
let ns := if ns = `NONE then name.anonymous else ns,
n ← lean.parser.ident,
prog ← lean.parser.of_tactic $ tactic.mk_const (ns ++ n) >>= tactic.eval_expr (lean.parser unit),
if ns = name.anonymous then tactic.skip else lean.parser.emit_code_here $
"namespace " ++ ns.to_string,
prog,
if ns = name.anonymous then tactic.skip else lean.parser.emit_code_here $
"end " ++ ns.to_string
meta def remove_dot_aux : list char → list char
| [] := []
| (c :: rest) := (if c = '.' then '_' else c) :: remove_dot_aux rest
meta def remove_dot (s : string) : string :=
(remove_dot_aux s.data).as_string
-- NOTE We must emit fully qualified names below in order to not influence the real tests!
@[user_command]
meta def run_parser_from_tactic_cmd
(_ : interactive.parse $ lean.parser.tk "run_parser_from_tactic")
: lean.parser unit :=
do ns ← lean.parser.ident,
let ns := if ns = `NONE then name.anonymous else ns,
n ← lean.parser.ident,
let tac_name := "try_test_" ++ (remove_dot (ns ++ n).to_string),
lean.parser.emit_code_here $
"meta def tactic.interactive." ++ tac_name ++ " (_ : interactive.parse " ++ (ns ++ n).to_string ++ ")"
++ ": tactic unit := tactic.triv",
if ns = name.anonymous then tactic.skip else lean.parser.emit_code_here $
"namespace " ++ ns.to_string,
lean.parser.emit_code_here $
"example : true := by " ++ (name.mk_string tac_name name.anonymous).to_string,
if ns = name.anonymous then tactic.skip else lean.parser.emit_code_here $
"end " ++ ns.to_string
meta def assert_name_eq (n₁ n₂ : name) : lean.parser unit :=
if n₁ = n₂ then return () else tactic.fail sformat!"violation: '{n₁}' ≠ '{n₂}'!"
meta def assert_list_noorder_eq {α : Type} [decidable_eq α] [has_to_string α]
(l₁ l₂ : list α) : lean.parser unit :=
if (l₁ : multiset α) = (l₂ : multiset α) then return ()
else tactic.fail sformat!"violation: '{l₁}' ≠ '{l₂}'!"
meta def assert_where_msg_eq (s : string) : lean.parser unit :=
do tw ← where.build_msg,
if s = tw then return ()
else tactic.fail sformat!"violation:\n'\n{tw}\n'\n\n ≠\n\n'\n{s}\n'!"
-- Test the test framework...
meta def dummy : lean.parser unit := return ()
run_parser_from_command NONE dummy
run_parser_from_tactic NONE dummy
end framework
-- TESTS START
-- TEST: `#where` output
-- NOTE: This section must come first because of the `open_namespaces` bug referenced above.
-- NOTE: All other sections have correct answers, but the order of variables (say) may be safely
-- reordered here: changes to `#where` which break this set of tests are possible, without an
-- error.
meta def test_output_1 : lean.parser unit :=
assert_where_msg_eq "namespace [root namespace]\n\n\n\n\nend [root namespace]\n"
meta def test_output_2 : lean.parser unit :=
assert_where_msg_eq "namespace [root namespace]\n\nopen list nat\n\n\n\nend [root namespace]\n"
meta def test_output_3 : lean.parser unit :=
assert_where_msg_eq "namespace [root namespace]\n\nopen list nat\nvariables {c : ℕ → list ℕ} (b a : ℕ) [decidable_eq : ℕ]\n\n\n\nend [root namespace]\n"
meta def test_output_4 : lean.parser unit :=
assert_where_msg_eq "namespace [root namespace]\n\nopen list nat\nvariables {c : ℕ → list ℕ} (b a : ℕ) [decidable_eq : ℕ]\ninclude c a\n\n\n\nend [root namespace]\n"
meta def test_output_5 : lean.parser unit :=
assert_where_msg_eq "namespace a\n\nopen list nat\nvariables {c : ℕ → list ℕ} (b a : ℕ) [decidable_eq : ℕ]\ninclude c a\n\n\n\nend a\n"
meta def test_output_6 : lean.parser unit :=
assert_where_msg_eq "namespace a.b\n\nopen a list nat\nvariables {c : ℕ → list ℕ} (b a : ℕ) [decidable_eq : ℕ]\ninclude c a\n\n\n\nend a.b\n"
section b1
-- #where
run_parser_from_command NONE test_output_1
open nat list
-- #where
run_parser_from_command NONE test_output_2
variables (a b : ℕ) [decidable_eq : ℕ] {c : ℕ → list ℕ}
-- #where
run_parser_from_command NONE test_output_3
include a c
-- #where
run_parser_from_command NONE test_output_4
end b1
namespace a
variables (a b : ℕ) [decidable_eq : ℕ] {c : ℕ → list ℕ}
include a c
open nat list
-- #where
run_parser_from_command NONE test_output_5
namespace b
-- #where
run_parser_from_command NONE test_output_6
end b
end a
-- TEST: `lean.parser.get_current_namespace`
-- Check no namespace
meta def test_no_namespace : lean.parser unit :=
do ns ← lean.parser.get_current_namespace,
assert_name_eq ns name.anonymous,
return ()
run_parser_from_command NONE test_no_namespace
run_parser_from_tactic NONE test_no_namespace
section a1
open nat list
-- Check no namespace with opens
meta def test_no_namespace_w_opens : lean.parser unit :=
do ns ← lean.parser.get_current_namespace,
assert_name_eq ns name.anonymous,
return ()
run_parser_from_command NONE test_no_namespace_w_opens
run_parser_from_tactic NONE test_no_namespace_w_opens
end a1
namespace test1
-- Check a namespace
meta def test_1 : lean.parser unit :=
do ns ← lean.parser.get_current_namespace,
assert_name_eq ns `test1,
return ()
open nat list
-- Check a 2 namespaces with opens
meta def test_2 : lean.parser unit :=
do ns ← lean.parser.get_current_namespace,
assert_name_eq ns `test1,
return ()
end test1
run_parser_from_command test1 test_1
run_parser_from_command test1 test_2
run_parser_from_tactic test1 test_1
run_parser_from_tactic test1 test_2
namespace test1.test2
-- Check a 2 namespaces
meta def test_1 : lean.parser unit :=
do ns ← lean.parser.get_current_namespace,
assert_name_eq ns `test1.test2,
return ()
open nat list
-- Check a 2 namespaces with opens
meta def test_2 : lean.parser unit :=
do ns ← lean.parser.get_current_namespace,
assert_name_eq ns `test1.test2,
return ()
end test1.test2
run_parser_from_command test1.test2 test_1
run_parser_from_command test1.test2 test_2
run_parser_from_tactic test1.test2 test_1
run_parser_from_tactic test1.test2 test_2
-- TEST: `lean.parser.get_variables` and `lean.parser.get_included_variables`
-- Check no variables
meta def test_no_variables : lean.parser unit :=
do ns ← lean.parser.get_variables,
assert_list_noorder_eq (ns.map prod.fst) [],
return ()
run_parser_from_command NONE test_no_variables
run_parser_from_tactic NONE test_no_variables
section a1
variables (a : ℕ)
-- Check 1 variable from command
meta def test_1_variable_from_command : lean.parser unit :=
do ns ← lean.parser.get_variables,
assert_list_noorder_eq (ns.map prod.fst) [`a],
return ()
run_parser_from_command NONE test_1_variable_from_command
-- Check 1 variable from tactic
meta def test_1_variable_from_tactic : lean.parser unit :=
do ns ← lean.parser.get_variables,
assert_list_noorder_eq (ns.map prod.fst) [],
return ()
run_parser_from_tactic NONE test_1_variable_from_tactic
end a1
namespace a2
variables (a : ℕ)
-- Check 1 variable from command inside namespace
meta def test_1_variable_from_command : lean.parser unit :=
do ns ← lean.parser.get_variables,
assert_list_noorder_eq (ns.map prod.fst) [`a],
return ()
run_parser_from_command NONE test_1_variable_from_command
-- Check 1 variable from tactic inside namespace
meta def test_1_variable_from_tactic : lean.parser unit :=
do ns ← lean.parser.get_variables,
assert_list_noorder_eq (ns.map prod.fst) [],
return ()
run_parser_from_tactic NONE test_1_variable_from_tactic
end a2
section a3
-- Check 3 variables with 1 include
meta def test_2_variable_from_command : lean.parser unit :=
do ns ← lean.parser.get_variables,
assert_list_noorder_eq (ns.map prod.fst) [`a, `b, `c],
ns ← lean.parser.get_included_variables,
assert_list_noorder_eq (ns.map prod.fst) [`b],
return ()
variables (a b c : ℕ)
include b
run_parser_from_command NONE test_2_variable_from_command
end a3
|
ca3698e74c3e4771f9ef8d449567238bd110e087 | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /library/data/nat/order.lean | 85595c613cce240c0fc4ac151594462e09ba1ffd | [
"Apache-2.0"
] | permissive | davidmueller13/lean | 65a3ed141b4088cd0a268e4de80eb6778b21a0e9 | c626e2e3c6f3771e07c32e82ee5b9e030de5b050 | refs/heads/master | 1,611,278,313,401 | 1,444,021,177,000 | 1,444,021,177,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 21,521 | 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, Leonardo de Moura, Jeremy Avigad
The order relation on the natural numbers.
-/
import data.nat.basic algebra.ordered_ring
open eq.ops
namespace nat
/- lt and le -/
theorem le_of_lt_or_eq {m n : ℕ} (H : m < n ∨ m = n) : m ≤ n :=
le_of_eq_or_lt (or.swap H)
theorem lt_or_eq_of_le {m n : ℕ} (H : m ≤ n) : m < n ∨ m = n :=
or.swap (eq_or_lt_of_le H)
theorem le_iff_lt_or_eq (m n : ℕ) : m ≤ n ↔ m < n ∨ m = n :=
iff.intro lt_or_eq_of_le le_of_lt_or_eq
theorem lt_of_le_and_ne {m n : ℕ} (H1 : m ≤ n) : m ≠ n → m < n :=
or_resolve_right (eq_or_lt_of_le H1)
theorem lt_iff_le_and_ne (m n : ℕ) : m < n ↔ m ≤ n ∧ m ≠ n :=
iff.intro
(take H, and.intro (le_of_lt H) (take H1, !lt.irrefl (H1 ▸ H)))
(and.rec lt_of_le_and_ne)
theorem le_add_right (n k : ℕ) : n ≤ n + k :=
nat.rec !le.refl (λ k, le_succ_of_le) k
theorem le_add_left (n m : ℕ): n ≤ m + n :=
!add.comm ▸ !le_add_right
theorem le.intro {n m k : ℕ} (h : n + k = m) : n ≤ m :=
h ▸ !le_add_right
theorem le.elim {n m : ℕ} : n ≤ m → ∃k, n + k = m :=
le.rec (exists.intro 0 rfl) (λm h, Exists.rec
(λ k H, exists.intro (succ k) (H ▸ rfl)))
theorem le.total {m n : ℕ} : m ≤ n ∨ n ≤ m :=
or.imp_left le_of_lt !lt_or_ge
/- addition -/
theorem add_le_add_left {n m : ℕ} (H : n ≤ m) (k : ℕ) : k + n ≤ k + m :=
obtain l Hl, from le.elim H, le.intro (Hl ▸ !add.assoc)
theorem add_le_add_right {n m : ℕ} (H : n ≤ m) (k : ℕ) : n + k ≤ m + k :=
!add.comm ▸ !add.comm ▸ add_le_add_left H k
theorem le_of_add_le_add_left {k n m : ℕ} (H : k + n ≤ k + m) : n ≤ m :=
obtain l Hl, from le.elim H, le.intro (add.cancel_left (!add.assoc⁻¹ ⬝ Hl))
theorem lt_of_add_lt_add_left {k n m : ℕ} (H : k + n < k + m) : n < m :=
let H' := le_of_lt H in
lt_of_le_and_ne (le_of_add_le_add_left H') (assume Heq, !lt.irrefl (Heq ▸ H))
theorem add_lt_add_left {n m : ℕ} (H : n < m) (k : ℕ) : k + n < k + m :=
lt_of_succ_le (!add_succ ▸ add_le_add_left (succ_le_of_lt H) k)
theorem add_lt_add_right {n m : ℕ} (H : n < m) (k : ℕ) : n + k < m + k :=
!add.comm ▸ !add.comm ▸ add_lt_add_left H k
theorem lt_add_of_pos_right {n k : ℕ} (H : k > 0) : n < n + k :=
!add_zero ▸ add_lt_add_left H n
/- multiplication -/
theorem mul_le_mul_left {n m : ℕ} (k : ℕ) (H : n ≤ m) : k * n ≤ k * m :=
obtain (l : ℕ) (Hl : n + l = m), from le.elim H,
have k * n + k * l = k * m, by rewrite [-mul.left_distrib, Hl],
le.intro this
theorem mul_le_mul_right {n m : ℕ} (k : ℕ) (H : n ≤ m) : n * k ≤ m * k :=
!mul.comm ▸ !mul.comm ▸ !mul_le_mul_left H
theorem mul_le_mul {n m k l : ℕ} (H1 : n ≤ k) (H2 : m ≤ l) : n * m ≤ k * l :=
le.trans (!mul_le_mul_right H1) (!mul_le_mul_left H2)
theorem mul_lt_mul_of_pos_left {n m k : ℕ} (H : n < m) (Hk : k > 0) : k * n < k * m :=
calc k * n < k * n + k : lt_add_of_pos_right Hk
... ≤ k * m : !mul_succ ▸ mul_le_mul_left k (succ_le_of_lt H)
theorem mul_lt_mul_of_pos_right {n m k : ℕ} (H : n < m) (Hk : k > 0) : n * k < m * k :=
!mul.comm ▸ !mul.comm ▸ mul_lt_mul_of_pos_left H Hk
/- min and max -/
/-
definition max (a b : ℕ) : ℕ := if a < b then b else a
definition min (a b : ℕ) : ℕ := if a < b then a else b
theorem max_self [simp] (a : ℕ) : max a a = a :=
eq.rec_on !if_t_t rfl
theorem max_le {n m k : ℕ} (H₁ : n ≤ k) (H₂ : m ≤ k) : max n m ≤ k :=
if H : n < m then by rewrite [↑max, if_pos H]; apply H₂
else by rewrite [↑max, if_neg H]; apply H₁
theorem min_le_left (n m : ℕ) : min n m ≤ n :=
if H : n < m then by rewrite [↑min, if_pos H]
else assert H' : m ≤ n, from or_resolve_right !lt_or_ge H,
by rewrite [↑min, if_neg H]; apply H'
theorem min_le_right (n m : ℕ) : min n m ≤ m :=
if H : n < m then by rewrite [↑min, if_pos H]; apply le_of_lt H
else assert H' : m ≤ n, from or_resolve_right !lt_or_ge H,
by rewrite [↑min, if_neg H]
theorem le_min {n m k : ℕ} (H₁ : k ≤ n) (H₂ : k ≤ m) : k ≤ min n m :=
if H : n < m then by rewrite [↑min, if_pos H]; apply H₁
else by rewrite [↑min, if_neg H]; apply H₂
theorem eq_max_right {a b : ℕ} (H : a < b) : b = max a b :=
(if_pos H)⁻¹
theorem eq_max_left {a b : ℕ} (H : ¬ a < b) : a = max a b :=
(if_neg H)⁻¹
open decidable
theorem le_max_right (a b : ℕ) : b ≤ max a b :=
lt.by_cases
(suppose a < b, (eq_max_right this) ▸ !le.refl)
(suppose a = b, this ▸ !max_self⁻¹ ▸ !le.refl)
(suppose b < a, (eq_max_left (lt.asymm this)) ▸ (le_of_lt this))
theorem le_max_left (a b : ℕ) : a ≤ max a b :=
if h : a < b then le_of_lt (eq.rec_on (eq_max_right h) h)
else (eq_max_left h) ▸ !le.refl
-/
/- nat is an instance of a linearly ordered semiring and a lattice -/
section migrate_algebra
open [classes] algebra
local attribute nat.comm_semiring [instance]
protected definition decidable_linear_ordered_semiring [reducible] :
algebra.decidable_linear_ordered_semiring nat :=
⦃ algebra.decidable_linear_ordered_semiring, nat.comm_semiring,
add_left_cancel := @add.cancel_left,
add_right_cancel := @add.cancel_right,
lt := lt,
le := le,
le_refl := le.refl,
le_trans := @le.trans,
le_antisymm := @le.antisymm,
le_total := @le.total,
le_iff_lt_or_eq := @le_iff_lt_or_eq,
le_of_lt := @le_of_lt,
lt_irrefl := @lt.irrefl,
lt_of_lt_of_le := @lt_of_lt_of_le,
lt_of_le_of_lt := @lt_of_le_of_lt,
lt_of_add_lt_add_left := @lt_of_add_lt_add_left,
add_lt_add_left := @add_lt_add_left,
add_le_add_left := @add_le_add_left,
le_of_add_le_add_left := @le_of_add_le_add_left,
zero_lt_one := zero_lt_succ 0,
mul_le_mul_of_nonneg_left := (take a b c H1 H2, mul_le_mul_left c H1),
mul_le_mul_of_nonneg_right := (take a b c H1 H2, mul_le_mul_right c H1),
mul_lt_mul_of_pos_left := @mul_lt_mul_of_pos_left,
mul_lt_mul_of_pos_right := @mul_lt_mul_of_pos_right,
decidable_lt := nat.decidable_lt ⦄
/-
protected definition lattice [reducible] : algebra.lattice nat :=
⦃ algebra.lattice, nat.linear_ordered_semiring,
min := min,
max := max,
min_le_left := min_le_left,
min_le_right := min_le_right,
le_min := @le_min,
le_max_left := le_max_left,
le_max_right := le_max_right,
max_le := @max_le ⦄
local attribute nat.linear_ordered_semiring [instance]
local attribute nat.lattice [instance]
-/
local attribute nat.decidable_linear_ordered_semiring [instance]
definition min : ℕ → ℕ → ℕ := algebra.min
definition max : ℕ → ℕ → ℕ := algebra.max
migrate from algebra with nat
replacing dvd → dvd, has_le.ge → ge, has_lt.gt → gt, min → min, max → max
hiding add_pos_of_pos_of_nonneg, add_pos_of_nonneg_of_pos,
add_eq_zero_iff_eq_zero_and_eq_zero_of_nonneg_of_nonneg, le_add_of_nonneg_of_le,
le_add_of_le_of_nonneg, lt_add_of_nonneg_of_lt, lt_add_of_lt_of_nonneg,
lt_of_mul_lt_mul_left, lt_of_mul_lt_mul_right, pos_of_mul_pos_left, pos_of_mul_pos_right,
mul_lt_mul
attribute le.trans ge.trans lt.trans gt.trans [trans]
attribute lt_of_lt_of_le lt_of_le_of_lt gt_of_gt_of_ge gt_of_ge_of_gt [trans]
theorem add_pos_left {a : ℕ} (H : 0 < a) (b : ℕ) : 0 < a + b :=
@algebra.add_pos_of_pos_of_nonneg _ _ a b H !zero_le
theorem add_pos_right {a : ℕ} (H : 0 < a) (b : ℕ) : 0 < b + a :=
!add.comm ▸ add_pos_left H b
theorem add_eq_zero_iff_eq_zero_and_eq_zero {a b : ℕ} :
a + b = 0 ↔ a = 0 ∧ b = 0 :=
@algebra.add_eq_zero_iff_eq_zero_and_eq_zero_of_nonneg_of_nonneg _ _ a b !zero_le !zero_le
theorem le_add_of_le_left {a b c : ℕ} (H : b ≤ c) : b ≤ a + c :=
@algebra.le_add_of_nonneg_of_le _ _ a b c !zero_le H
theorem le_add_of_le_right {a b c : ℕ} (H : b ≤ c) : b ≤ c + a :=
@algebra.le_add_of_le_of_nonneg _ _ a b c H !zero_le
theorem lt_add_of_lt_left {b c : ℕ} (H : b < c) (a : ℕ) : b < a + c :=
@algebra.lt_add_of_nonneg_of_lt _ _ a b c !zero_le H
theorem lt_add_of_lt_right {b c : ℕ} (H : b < c) (a : ℕ) : b < c + a :=
@algebra.lt_add_of_lt_of_nonneg _ _ a b c H !zero_le
theorem lt_of_mul_lt_mul_left {a b c : ℕ} (H : c * a < c * b) : a < b :=
@algebra.lt_of_mul_lt_mul_left _ _ a b c H !zero_le
theorem lt_of_mul_lt_mul_right {a b c : ℕ} (H : a * c < b * c) : a < b :=
@algebra.lt_of_mul_lt_mul_right _ _ a b c H !zero_le
theorem pos_of_mul_pos_left {a b : ℕ} (H : 0 < a * b) : 0 < b :=
@algebra.pos_of_mul_pos_left _ _ a b H !zero_le
theorem pos_of_mul_pos_right {a b : ℕ} (H : 0 < a * b) : 0 < a :=
@algebra.pos_of_mul_pos_right _ _ a b H !zero_le
end migrate_algebra
theorem zero_le_one : 0 ≤ 1 := dec_trivial
/- properties specific to nat -/
theorem lt_intro {n m k : ℕ} (H : succ n + k = m) : n < m :=
lt_of_succ_le (le.intro H)
theorem lt_elim {n m : ℕ} (H : n < m) : ∃k, succ n + k = m :=
le.elim (succ_le_of_lt H)
theorem lt_add_succ (n m : ℕ) : n < n + succ m :=
lt_intro !succ_add_eq_succ_add
theorem eq_zero_of_le_zero {n : ℕ} (H : n ≤ 0) : n = 0 :=
obtain (k : ℕ) (Hk : n + k = 0), from le.elim H,
eq_zero_of_add_eq_zero_right Hk
/- succ and pred -/
theorem le_of_lt_succ {m n : nat} : m < succ n → m ≤ n :=
le_of_succ_le_succ
theorem lt_iff_succ_le (m n : nat) : m < n ↔ succ m ≤ n :=
iff.rfl
theorem lt_succ_iff_le (m n : nat) : m < succ n ↔ m ≤ n :=
iff.intro le_of_lt_succ lt_succ_of_le
theorem self_le_succ (n : ℕ) : n ≤ succ n :=
le.intro !add_one
theorem succ_le_or_eq_of_le {n m : ℕ} : n ≤ m → succ n ≤ m ∨ n = m :=
lt_or_eq_of_le
theorem pred_le_of_le_succ {n m : ℕ} : n ≤ succ m → pred n ≤ m :=
pred_le_pred
theorem succ_le_of_le_pred {n m : ℕ} : succ n ≤ m → n ≤ pred m :=
pred_le_pred
theorem pred_le_pred_of_le {n m : ℕ} : n ≤ m → pred n ≤ pred m :=
pred_le_pred
theorem pre_lt_of_lt {n m : ℕ} : n < m → pred n < m :=
lt_of_le_of_lt !pred_le
theorem lt_of_pred_lt_pred {n m : ℕ} (H : pred n < pred m) : n < m :=
lt_of_not_ge
(suppose m ≤ n,
not_lt_of_ge (pred_le_pred_of_le this) H)
theorem le_or_eq_succ_of_le_succ {n m : ℕ} (H : n ≤ succ m) : n ≤ m ∨ n = succ m :=
or.imp_left le_of_succ_le_succ (succ_le_or_eq_of_le H)
theorem le_pred_self (n : ℕ) : pred n ≤ n :=
!pred_le
theorem succ_pos (n : ℕ) : 0 < succ n :=
!zero_lt_succ
theorem succ_pred_of_pos {n : ℕ} (H : n > 0) : succ (pred n) = n :=
(or_resolve_right (eq_zero_or_eq_succ_pred n) (ne.symm (ne_of_lt H)))⁻¹
theorem exists_eq_succ_of_lt {n : ℕ} : Π {m : ℕ}, n < m → ∃k, m = succ k
| 0 H := absurd H !not_lt_zero
| (succ k) H := exists.intro k rfl
theorem lt_succ_self (n : ℕ) : n < succ n :=
lt.base n
lemma lt_succ_of_lt {i j : nat} : i < j → i < succ j :=
assume Plt, lt.trans Plt (self_lt_succ j)
/- other forms of induction -/
protected definition strong_rec_on {P : nat → Type} (n : ℕ) (H : ∀n, (∀m, m < n → P m) → P n) : P n :=
nat.rec (λm h, absurd h !not_lt_zero)
(λn' (IH : ∀ {m : ℕ}, m < n' → P m) m l,
or.by_cases (lt_or_eq_of_le (le_of_lt_succ l))
IH (λ e, eq.rec (H n' @IH) e⁻¹)) (succ n) n !lt_succ_self
protected theorem strong_induction_on {P : nat → Prop} (n : ℕ) (H : ∀n, (∀m, m < n → P m) → P n) :
P n :=
nat.strong_rec_on n H
protected theorem case_strong_induction_on {P : nat → Prop} (a : nat) (H0 : P 0)
(Hind : ∀(n : nat), (∀m, m ≤ n → P m) → P (succ n)) : P a :=
nat.strong_induction_on a
(take n,
show (∀ m, m < n → P m) → P n, from
nat.cases_on n
(suppose (∀ m, m < 0 → P m), show P 0, from H0)
(take n,
suppose (∀ m, m < succ n → P m),
show P (succ n), from
Hind n (take m, assume H1 : m ≤ n, this _ (lt_succ_of_le H1))))
/- pos -/
theorem by_cases_zero_pos {P : ℕ → Prop} (y : ℕ) (H0 : P 0) (H1 : ∀ {y : nat}, y > 0 → P y) :
P y :=
nat.cases_on y H0 (take y, H1 !succ_pos)
theorem eq_zero_or_pos (n : ℕ) : n = 0 ∨ n > 0 :=
or_of_or_of_imp_left
(or.swap (lt_or_eq_of_le !zero_le))
(suppose 0 = n, by subst n)
theorem pos_of_ne_zero {n : ℕ} (H : n ≠ 0) : n > 0 :=
or.elim !eq_zero_or_pos (take H2 : n = 0, by contradiction) (take H2 : n > 0, H2)
theorem ne_zero_of_pos {n : ℕ} (H : n > 0) : n ≠ 0 :=
ne.symm (ne_of_lt H)
theorem exists_eq_succ_of_pos {n : ℕ} (H : n > 0) : exists l, n = succ l :=
exists_eq_succ_of_lt H
theorem pos_of_dvd_of_pos {m n : ℕ} (H1 : m ∣ n) (H2 : n > 0) : m > 0 :=
pos_of_ne_zero
(suppose m = 0,
assert n = 0, from eq_zero_of_zero_dvd (this ▸ H1),
ne_of_lt H2 (by subst n))
/- multiplication -/
theorem mul_lt_mul_of_le_of_lt {n m k l : ℕ} (Hk : k > 0) (H1 : n ≤ k) (H2 : m < l) :
n * m < k * l :=
lt_of_le_of_lt (mul_le_mul_right m H1) (mul_lt_mul_of_pos_left H2 Hk)
theorem mul_lt_mul_of_lt_of_le {n m k l : ℕ} (Hl : l > 0) (H1 : n < k) (H2 : m ≤ l) :
n * m < k * l :=
lt_of_le_of_lt (mul_le_mul_left n H2) (mul_lt_mul_of_pos_right H1 Hl)
theorem mul_lt_mul_of_le_of_le {n m k l : ℕ} (H1 : n < k) (H2 : m < l) : n * m < k * l :=
have H3 : n * m ≤ k * m, from mul_le_mul_right m (le_of_lt H1),
have H4 : k * m < k * l, from mul_lt_mul_of_pos_left H2 (lt_of_le_of_lt !zero_le H1),
lt_of_le_of_lt H3 H4
theorem eq_of_mul_eq_mul_left {m k n : ℕ} (Hn : n > 0) (H : n * m = n * k) : m = k :=
have n * m ≤ n * k, by rewrite H,
have m ≤ k, from le_of_mul_le_mul_left this Hn,
have n * k ≤ n * m, by rewrite H,
have k ≤ m, from le_of_mul_le_mul_left this Hn,
le.antisymm `m ≤ k` this
theorem eq_of_mul_eq_mul_right {n m k : ℕ} (Hm : m > 0) (H : n * m = k * m) : n = k :=
eq_of_mul_eq_mul_left Hm (!mul.comm ▸ !mul.comm ▸ H)
theorem eq_zero_or_eq_of_mul_eq_mul_left {n m k : ℕ} (H : n * m = n * k) : n = 0 ∨ m = k :=
or_of_or_of_imp_right !eq_zero_or_pos
(assume Hn : n > 0, eq_of_mul_eq_mul_left Hn H)
theorem eq_zero_or_eq_of_mul_eq_mul_right {n m k : ℕ} (H : n * m = k * m) : m = 0 ∨ n = k :=
eq_zero_or_eq_of_mul_eq_mul_left (!mul.comm ▸ !mul.comm ▸ H)
theorem eq_one_of_mul_eq_one_right {n m : ℕ} (H : n * m = 1) : n = 1 :=
have H2 : n * m > 0, by rewrite H; apply succ_pos,
or.elim (le_or_gt n 1)
(suppose n ≤ 1,
have n > 0, from pos_of_mul_pos_right H2,
show n = 1, from le.antisymm `n ≤ 1` (succ_le_of_lt this))
(suppose n > 1,
have m > 0, from pos_of_mul_pos_left H2,
have n * m ≥ 2 * 1, from mul_le_mul (succ_le_of_lt `n > 1`) (succ_le_of_lt this),
have 1 ≥ 2, from !mul_one ▸ H ▸ this,
absurd !lt_succ_self (not_lt_of_ge this))
theorem eq_one_of_mul_eq_one_left {n m : ℕ} (H : n * m = 1) : m = 1 :=
eq_one_of_mul_eq_one_right (!mul.comm ▸ H)
theorem eq_one_of_mul_eq_self_left {n m : ℕ} (Hpos : n > 0) (H : m * n = n) : m = 1 :=
eq_of_mul_eq_mul_right Hpos (H ⬝ !one_mul⁻¹)
theorem eq_one_of_mul_eq_self_right {n m : ℕ} (Hpos : m > 0) (H : m * n = m) : n = 1 :=
eq_one_of_mul_eq_self_left Hpos (!mul.comm ▸ H)
theorem eq_one_of_dvd_one {n : ℕ} (H : n ∣ 1) : n = 1 :=
dvd.elim H
(take m, suppose 1 = n * m,
eq_one_of_mul_eq_one_right this⁻¹)
/- min and max -/
open decidable
theorem le_max_left_iff_true [simp] (a b : ℕ) : a ≤ max a b ↔ true :=
iff_true_intro (le_max_left a b)
theorem le_max_right_iff_true [simp] (a b : ℕ) : b ≤ max a b ↔ true :=
iff_true_intro (le_max_right a b)
theorem min_zero [simp] (a : ℕ) : min a 0 = 0 :=
by rewrite [min_eq_right !zero_le]
theorem zero_min [simp] (a : ℕ) : min 0 a = 0 :=
by rewrite [min_eq_left !zero_le]
theorem max_zero [simp] (a : ℕ) : max a 0 = a :=
by rewrite [max_eq_left !zero_le]
theorem zero_max [simp] (a : ℕ) : max 0 a = a :=
by rewrite [max_eq_right !zero_le]
theorem min_succ_succ [simp] (a b : ℕ) : min (succ a) (succ b) = succ (min a b) :=
or.elim !lt_or_ge
(suppose a < b, by rewrite [min_eq_left_of_lt this, min_eq_left_of_lt (succ_lt_succ this)])
(suppose a ≥ b, by rewrite [min_eq_right this, min_eq_right (succ_le_succ this)])
theorem max_succ_succ [simp] (a b : ℕ) : max (succ a) (succ b) = succ (max a b) :=
or.elim !lt_or_ge
(suppose a < b, by rewrite [max_eq_right_of_lt this, max_eq_right_of_lt (succ_lt_succ this)])
(suppose a ≥ b, by rewrite [max_eq_left this, max_eq_left (succ_le_succ this)])
/- In algebra.ordered_group, these next four are only proved for additive groups, not additive
semigroups. -/
theorem min_add_add_left (a b c : ℕ) : min (a + b) (a + c) = a + min b c :=
decidable.by_cases
(suppose b ≤ c,
assert a + b ≤ a + c, from add_le_add_left this _,
by rewrite [min_eq_left `b ≤ c`, min_eq_left this])
(suppose ¬ b ≤ c,
assert c ≤ b, from le_of_lt (lt_of_not_ge this),
assert a + c ≤ a + b, from add_le_add_left this _,
by rewrite [min_eq_right `c ≤ b`, min_eq_right this])
theorem min_add_add_right (a b c : ℕ) : min (a + c) (b + c) = min a b + c :=
by rewrite [add.comm a c, add.comm b c, add.comm _ c]; apply min_add_add_left
theorem max_add_add_left (a b c : ℕ) : max (a + b) (a + c) = a + max b c :=
decidable.by_cases
(suppose b ≤ c,
assert a + b ≤ a + c, from add_le_add_left this _,
by rewrite [max_eq_right `b ≤ c`, max_eq_right this])
(suppose ¬ b ≤ c,
assert c ≤ b, from le_of_lt (lt_of_not_ge this),
assert a + c ≤ a + b, from add_le_add_left this _,
by rewrite [max_eq_left `c ≤ b`, max_eq_left this])
theorem max_add_add_right (a b c : ℕ) : max (a + c) (b + c) = max a b + c :=
by rewrite [add.comm a c, add.comm b c, add.comm _ c]; apply max_add_add_left
/- least and greatest -/
section least_and_greatest
variable (P : ℕ → Prop)
variable [decP : ∀ n, decidable (P n)]
include decP
-- returns the least i < n satisfying P, or n if there is none
definition least : ℕ → ℕ
| 0 := 0
| (succ n) := if P (least n) then least n else succ n
theorem least_of_bound {n : ℕ} (H : P n) : P (least P n) :=
begin
induction n with [m, ih],
rewrite ↑least,
apply H,
rewrite ↑least,
cases decidable.em (P (least P m)) with [Hlp, Hlp],
rewrite [if_pos Hlp],
apply Hlp,
rewrite [if_neg Hlp],
apply H
end
theorem least_le (n : ℕ) : least P n ≤ n:=
begin
induction n with [m, ih],
{rewrite ↑least},
rewrite ↑least,
cases decidable.em (P (least P m)) with [Psm, Pnsm],
rewrite [if_pos Psm],
apply le.trans ih !le_succ,
rewrite [if_neg Pnsm]
end
theorem least_of_lt {i n : ℕ} (ltin : i < n) (H : P i) : P (least P n) :=
begin
induction n with [m, ih],
exact absurd ltin !not_lt_zero,
rewrite ↑least,
cases decidable.em (P (least P m)) with [Psm, Pnsm],
rewrite [if_pos Psm],
apply Psm,
rewrite [if_neg Pnsm],
cases (lt_or_eq_of_le (le_of_lt_succ ltin)) with [Hlt, Heq],
exact absurd (ih Hlt) Pnsm,
rewrite Heq at H,
exact absurd (least_of_bound P H) Pnsm
end
theorem ge_least_of_lt {i n : ℕ} (ltin : i < n) (Hi : P i) : i ≥ least P n :=
begin
induction n with [m, ih],
exact absurd ltin !not_lt_zero,
rewrite ↑least,
cases decidable.em (P (least P m)) with [Psm, Pnsm],
rewrite [if_pos Psm],
cases (lt_or_eq_of_le (le_of_lt_succ ltin)) with [Hlt, Heq],
apply ih Hlt,
rewrite Heq,
apply least_le,
rewrite [if_neg Pnsm],
cases (lt_or_eq_of_le (le_of_lt_succ ltin)) with [Hlt, Heq],
apply absurd (least_of_lt P Hlt Hi) Pnsm,
rewrite Heq at Hi,
apply absurd (least_of_bound P Hi) Pnsm
end
theorem least_lt {n i : ℕ} (ltin : i < n) (Hi : P i) : least P n < n :=
lt_of_le_of_lt (ge_least_of_lt P ltin Hi) ltin
-- returns the largest i < n satisfying P, or n if there is none.
definition greatest : ℕ → ℕ
| 0 := 0
| (succ n) := if P n then n else greatest n
theorem greatest_of_lt {i n : ℕ} (ltin : i < n) (Hi : P i) : P (greatest P n) :=
begin
induction n with [m, ih],
{exact absurd ltin !not_lt_zero},
{cases (decidable.em (P m)) with [Psm, Pnsm],
{rewrite [↑greatest, if_pos Psm]; exact Psm},
{rewrite [↑greatest, if_neg Pnsm],
have neim : i ≠ m, from assume H : i = m, absurd (H ▸ Hi) Pnsm,
have ltim : i < m, from lt_of_le_of_ne (le_of_lt_succ ltin) neim,
apply ih ltim}}
end
theorem le_greatest_of_lt {i n : ℕ} (ltin : i < n) (Hi : P i) : i ≤ greatest P n :=
begin
induction n with [m, ih],
{exact absurd ltin !not_lt_zero},
{cases (decidable.em (P m)) with [Psm, Pnsm],
{rewrite [↑greatest, if_pos Psm], apply le_of_lt_succ ltin},
{rewrite [↑greatest, if_neg Pnsm],
have neim : i ≠ m, from assume H : i = m, absurd (H ▸ Hi) Pnsm,
have ltim : i < m, from lt_of_le_of_ne (le_of_lt_succ ltin) neim,
apply ih ltim}}
end
end least_and_greatest
end nat
|
90eb8713b7ec0581c91e53eb9104da07cc15401b | 5ec8f5218a7c8e87dd0d70dc6b715b36d61a8d61 | /archi/x86_32.lean | 59bbcc31a39a6bd8322d82a517395b056b9b4b00 | [] | no_license | mbrodersen/kremlin | f9f2f9dd77b9744fe0ffd5f70d9fa0f1f8bd8cec | d4665929ce9012e93a0b05fc7063b96256bab86f | refs/heads/master | 1,624,057,268,130 | 1,496,957,084,000 | 1,496,957,084,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 745 | lean | import ..flocq
/- Architecture-dependent parameters for x86 in 32-bit mode -/
namespace archi
open flocq
def ptr64 : bool := ff
def big_endian : bool := ff
def align_int64 := 4
def align_float64 := 4
def splitlong := bnot ptr64
lemma splitlong_ptr32 : splitlong = tt → ptr64 = ff := λ_, rfl
def default_pl_64 : bool × nan_pl 53 :=
(ff, word.repr (2^51))
def choose_binop_pl_64 (s1 : bool) (pl1 : nan_pl 53) (s2 : bool) (pl2 : nan_pl 53) : bool :=
ff /- always choose first NaN -/
def default_pl_32 : bool × nan_pl 24 :=
(ff, word.repr (2^22))
def choose_binop_pl_32 (s1 : bool) (pl1 : nan_pl 24) (s2 : bool) (pl2 : nan_pl 24) : bool :=
ff /- always choose first NaN -/
def float_of_single_preserves_sNaN := ff
end archi |
40042c61131d9d5c2301d9a0c3c4871368b2b798 | cf39355caa609c0f33405126beee2739aa3cb77e | /library/init/meta/task.lean | 9843ec0d2ba4f1bc7d6d4509c346530534b81eb1 | [
"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 | 741 | lean | prelude
import init.logic
/-- A task is a promise to produce a value later. They perform the same role as promises in JavaScript. -/
meta constant {u} task : Type u → Type u
namespace task
meta constant {u} get {α : Type u} (t : task α) : α
protected meta constant {u} pure {α : Type u} (t : α) : task α
protected meta constant {u v} map {α : Type u} {β : Type v} (f : α → β) (t : task α) : task β
protected meta constant {u} flatten {α : Type u} : task (task α) → task α
protected meta def {u v} bind {α : Type u} {β : Type v} (t : task α) (f : α → task β) : task β :=
task.flatten (task.map f t)
@[inline]
meta def {u} delay {α : Type u} (f : unit → α) : task α :=
task.map f (task.pure ())
end task
|
7369bd4df4a94c34ccbb11db75f59251e92370d8 | 26bff4ed296b8373c92b6b025f5d60cdf02104b9 | /library/algebra/group.lean | eafe3b1b8af9d6ae4d1a2ecf270d0dd6cea3d522 | [
"Apache-2.0"
] | permissive | guiquanz/lean | b8a878ea24f237b84b0e6f6be2f300e8bf028229 | 242f8ba0486860e53e257c443e965a82ee342db3 | refs/heads/master | 1,526,680,092,098 | 1,427,492,833,000 | 1,427,493,281,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 16,344 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Module: algebra.group
Authors: Jeremy Avigad, Leonardo de Moura
Various multiplicative and additive structures. Partially modeled on Isabelle's library.
-/
import logic.eq data.unit data.sigma data.prod
import algebra.function algebra.binary
open eq eq.ops -- note: ⁻¹ will be overloaded
open binary
namespace algebra
variable {A : Type}
/- overloaded symbols -/
structure has_mul [class] (A : Type) :=
(mul : A → A → A)
structure has_add [class] (A : Type) :=
(add : A → A → A)
structure has_one [class] (A : Type) :=
(one : A)
structure has_zero [class] (A : Type) :=
(zero : A)
structure has_inv [class] (A : Type) :=
(inv : A → A)
structure has_neg [class] (A : Type) :=
(neg : A → A)
infixl `*` := has_mul.mul
infixl `+` := has_add.add
postfix `⁻¹` := has_inv.inv
prefix `-` := has_neg.neg
notation 1 := !has_one.one
notation 0 := !has_zero.zero
/- semigroup -/
structure semigroup [class] (A : Type) extends has_mul A :=
(mul_assoc : ∀a b c, mul (mul a b) c = mul a (mul b c))
theorem mul.assoc [s : semigroup A] (a b c : A) : a * b * c = a * (b * c) :=
!semigroup.mul_assoc
structure comm_semigroup [class] (A : Type) extends semigroup A :=
(mul_comm : ∀a b, mul a b = mul b a)
theorem mul.comm [s : comm_semigroup A] (a b : A) : a * b = b * a :=
!comm_semigroup.mul_comm
theorem mul.left_comm [s : comm_semigroup A] (a b c : A) : a * (b * c) = b * (a * c) :=
binary.left_comm (@mul.comm A s) (@mul.assoc A s) a b c
theorem mul.right_comm [s : comm_semigroup A] (a b c : A) : (a * b) * c = (a * c) * b :=
binary.right_comm (@mul.comm A s) (@mul.assoc A s) a b c
structure left_cancel_semigroup [class] (A : Type) extends semigroup A :=
(mul_left_cancel : ∀a b c, mul a b = mul a c → b = c)
theorem mul.left_cancel [s : left_cancel_semigroup A] {a b c : A} :
a * b = a * c → b = c :=
!left_cancel_semigroup.mul_left_cancel
structure right_cancel_semigroup [class] (A : Type) extends semigroup A :=
(mul_right_cancel : ∀a b c, mul a b = mul c b → a = c)
theorem mul.right_cancel [s : right_cancel_semigroup A] {a b c : A} :
a * b = c * b → a = c :=
!right_cancel_semigroup.mul_right_cancel
/- additive semigroup -/
structure add_semigroup [class] (A : Type) extends has_add A :=
(add_assoc : ∀a b c, add (add a b) c = add a (add b c))
theorem add.assoc [s : add_semigroup A] (a b c : A) : a + b + c = a + (b + c) :=
!add_semigroup.add_assoc
structure add_comm_semigroup [class] (A : Type) extends add_semigroup A :=
(add_comm : ∀a b, add a b = add b a)
theorem add.comm [s : add_comm_semigroup A] (a b : A) : a + b = b + a :=
!add_comm_semigroup.add_comm
theorem add.left_comm [s : add_comm_semigroup A] (a b c : A) :
a + (b + c) = b + (a + c) :=
binary.left_comm (@add.comm A s) (@add.assoc A s) a b c
theorem add.right_comm [s : add_comm_semigroup A] (a b c : A) : (a + b) + c = (a + c) + b :=
binary.right_comm (@add.comm A s) (@add.assoc A s) a b c
structure add_left_cancel_semigroup [class] (A : Type) extends add_semigroup A :=
(add_left_cancel : ∀a b c, add a b = add a c → b = c)
theorem add.left_cancel [s : add_left_cancel_semigroup A] {a b c : A} :
a + b = a + c → b = c :=
!add_left_cancel_semigroup.add_left_cancel
structure add_right_cancel_semigroup [class] (A : Type) extends add_semigroup A :=
(add_right_cancel : ∀a b c, add a b = add c b → a = c)
theorem add.right_cancel [s : add_right_cancel_semigroup A] {a b c : A} :
a + b = c + b → a = c :=
!add_right_cancel_semigroup.add_right_cancel
/- monoid -/
structure monoid [class] (A : Type) extends semigroup A, has_one A :=
(one_mul : ∀a, mul one a = a) (mul_one : ∀a, mul a one = a)
theorem one_mul [s : monoid A] (a : A) : 1 * a = a := !monoid.one_mul
theorem mul_one [s : monoid A] (a : A) : a * 1 = a := !monoid.mul_one
structure comm_monoid [class] (A : Type) extends monoid A, comm_semigroup A
/- additive monoid -/
structure add_monoid [class] (A : Type) extends add_semigroup A, has_zero A :=
(zero_add : ∀a, add zero a = a) (add_zero : ∀a, add a zero = a)
theorem zero_add [s : add_monoid A] (a : A) : 0 + a = a := !add_monoid.zero_add
theorem add_zero [s : add_monoid A] (a : A) : a + 0 = a := !add_monoid.add_zero
structure add_comm_monoid [class] (A : Type) extends add_monoid A, add_comm_semigroup A
/- group -/
structure group [class] (A : Type) extends monoid A, has_inv A :=
(mul_left_inv : ∀a, mul (inv a) a = one)
-- Note: with more work, we could derive the axiom one_mul
section group
variable [s : group A]
include s
theorem mul.left_inv (a : A) : a⁻¹ * a = 1 := !group.mul_left_inv
theorem inv_mul_cancel_left (a b : A) : a⁻¹ * (a * b) = b :=
by rewrite ⟨-mul.assoc, mul.left_inv, one_mul⟩
theorem inv_mul_cancel_right (a b : A) : a * b⁻¹ * b = a :=
by rewrite ⟨mul.assoc, mul.left_inv, mul_one⟩
theorem inv_eq_of_mul_eq_one {a b : A} (H : a * b = 1) : a⁻¹ = b :=
by rewrite ⟨-mul_one a⁻¹, -H, inv_mul_cancel_left⟩
theorem inv_one : 1⁻¹ = 1 := inv_eq_of_mul_eq_one (one_mul 1)
theorem inv_inv (a : A) : (a⁻¹)⁻¹ = a := inv_eq_of_mul_eq_one (mul.left_inv a)
theorem inv.inj {a b : A} (H : a⁻¹ = b⁻¹) : a = b :=
by rewrite ⟨-inv_inv, H, inv_inv⟩
theorem inv_eq_inv_iff_eq (a b : A) : a⁻¹ = b⁻¹ ↔ a = b :=
iff.intro (assume H, inv.inj H) (assume H, congr_arg _ H)
theorem inv_eq_one_iff_eq_one (a b : A) : a⁻¹ = 1 ↔ a = 1 :=
inv_one ▸ inv_eq_inv_iff_eq a 1
theorem eq_inv_of_eq_inv {a b : A} (H : a = b⁻¹) : b = a⁻¹ :=
by rewrite [H, inv_inv]
theorem eq_inv_iff_eq_inv (a b : A) : a = b⁻¹ ↔ b = a⁻¹ :=
iff.intro !eq_inv_of_eq_inv !eq_inv_of_eq_inv
theorem mul.right_inv (a : A) : a * a⁻¹ = 1 :=
calc
a * a⁻¹ = (a⁻¹)⁻¹ * a⁻¹ : inv_inv
... = 1 : mul.left_inv
theorem mul_inv_cancel_left (a b : A) : a * (a⁻¹ * b) = b :=
calc
a * (a⁻¹ * b) = a * a⁻¹ * b : by rewrite mul.assoc
... = 1 * b : mul.right_inv
... = b : one_mul
theorem mul_inv_cancel_right (a b : A) : a * b * b⁻¹ = a :=
calc
a * b * b⁻¹ = a * (b * b⁻¹) : mul.assoc
... = a * 1 : mul.right_inv
... = a : mul_one
theorem inv_mul (a b : A) : (a * b)⁻¹ = b⁻¹ * a⁻¹ :=
inv_eq_of_mul_eq_one
(calc
a * b * (b⁻¹ * a⁻¹) = a * (b * (b⁻¹ * a⁻¹)) : mul.assoc
... = a * a⁻¹ : mul_inv_cancel_left
... = 1 : mul.right_inv)
theorem eq_of_mul_inv_eq_one {a b : A} (H : a * b⁻¹ = 1) : a = b :=
calc
a = a * b⁻¹ * b : by rewrite inv_mul_cancel_right
... = 1 * b : H
... = b : one_mul
theorem eq_mul_inv_of_mul_eq {a b c : A} (H : a * c = b) : a = b * c⁻¹ :=
by rewrite [-H, mul_inv_cancel_right]
theorem eq_inv_mul_of_mul_eq {a b c : A} (H : b * a = c) : a = b⁻¹ * c :=
by rewrite [-H, inv_mul_cancel_left]
theorem inv_mul_eq_of_eq_mul {a b c : A} (H : b = a * c) : a⁻¹ * b = c :=
by rewrite [H, inv_mul_cancel_left]
theorem mul_inv_eq_of_eq_mul {a b c : A} (H : a = c * b) : a * b⁻¹ = c :=
by rewrite [H, mul_inv_cancel_right]
theorem eq_mul_of_mul_inv_eq {a b c : A} (H : a * c⁻¹ = b) : a = b * c :=
!inv_inv ▸ (eq_mul_inv_of_mul_eq H)
theorem eq_mul_of_inv_mul_eq {a b c : A} (H : b⁻¹ * a = c) : a = b * c :=
!inv_inv ▸ (eq_inv_mul_of_mul_eq H)
theorem mul_eq_of_eq_inv_mul {a b c : A} (H : b = a⁻¹ * c) : a * b = c :=
!inv_inv ▸ (inv_mul_eq_of_eq_mul H)
theorem mul_eq_of_eq_mul_inv {a b c : A} (H : a = c * b⁻¹) : a * b = c :=
!inv_inv ▸ (mul_inv_eq_of_eq_mul H)
theorem mul_eq_iff_eq_inv_mul (a b c : A) : a * b = c ↔ b = a⁻¹ * c :=
iff.intro eq_inv_mul_of_mul_eq mul_eq_of_eq_inv_mul
theorem mul_eq_iff_eq_mul_inv (a b c : A) : a * b = c ↔ a = c * b⁻¹ :=
iff.intro eq_mul_inv_of_mul_eq mul_eq_of_eq_mul_inv
theorem mul_left_cancel {a b c : A} (H : a * b = a * c) : b = c :=
by rewrite ⟨-inv_mul_cancel_left a b, H, inv_mul_cancel_left⟩
theorem mul_right_cancel {a b c : A} (H : a * b = c * b) : a = c :=
by rewrite ⟨-mul_inv_cancel_right a b, H, mul_inv_cancel_right⟩
definition group.to_left_cancel_semigroup [instance] [coercion] [reducible] : left_cancel_semigroup A :=
⦃ left_cancel_semigroup, s,
mul_left_cancel := @mul_left_cancel A s ⦄
definition group.to_right_cancel_semigroup [instance] [coercion] [reducible] : right_cancel_semigroup A :=
⦃ right_cancel_semigroup, s,
mul_right_cancel := @mul_right_cancel A s ⦄
end group
structure comm_group [class] (A : Type) extends group A, comm_monoid A
/- additive group -/
structure add_group [class] (A : Type) extends add_monoid A, has_neg A :=
(add_left_inv : ∀a, add (neg a) a = zero)
section add_group
variables [s : add_group A]
include s
theorem add.left_inv (a : A) : -a + a = 0 := !add_group.add_left_inv
theorem neg_add_cancel_left (a b : A) : -a + (a + b) = b :=
by rewrite ⟨-add.assoc, add.left_inv, zero_add⟩
theorem neg_add_cancel_right (a b : A) : a + -b + b = a :=
by rewrite ⟨add.assoc, add.left_inv, add_zero⟩
theorem neg_eq_of_add_eq_zero {a b : A} (H : a + b = 0) : -a = b :=
by rewrite ⟨-add_zero, -H, neg_add_cancel_left⟩
theorem neg_zero : -0 = 0 := neg_eq_of_add_eq_zero (zero_add 0)
theorem neg_neg (a : A) : -(-a) = a := neg_eq_of_add_eq_zero (add.left_inv a)
theorem neg.inj {a b : A} (H : -a = -b) : a = b :=
calc
a = -(-a) : neg_neg
... = b : neg_eq_of_add_eq_zero (H⁻¹ ▸ (add.left_inv _))
theorem neg_eq_neg_iff_eq (a b : A) : -a = -b ↔ a = b :=
iff.intro (assume H, neg.inj H) (assume H, congr_arg _ H)
theorem neg_eq_zero_iff_eq_zero (a : A) : -a = 0 ↔ a = 0 :=
neg_zero ▸ !neg_eq_neg_iff_eq
theorem eq_neg_of_eq_neg {a b : A} (H : a = -b) : b = -a :=
H⁻¹ ▸ (neg_neg b)⁻¹
theorem eq_neg_iff_eq_neg (a b : A) : a = -b ↔ b = -a :=
iff.intro !eq_neg_of_eq_neg !eq_neg_of_eq_neg
theorem add.right_inv (a : A) : a + -a = 0 :=
calc
a + -a = -(-a) + -a : neg_neg
... = 0 : add.left_inv
theorem add_neg_cancel_left (a b : A) : a + (-a + b) = b :=
by rewrite ⟨-add.assoc, add.right_inv, zero_add⟩
theorem add_neg_cancel_right (a b : A) : a + b + -b = a :=
by rewrite ⟨add.assoc, add.right_inv, add_zero⟩
theorem neg_add_rev (a b : A) : -(a + b) = -b + -a :=
neg_eq_of_add_eq_zero
begin
rewrite ⟨add.assoc, add_neg_cancel_left, add.right_inv⟩
end
-- TODO: delete these in favor of sub rules?
theorem eq_add_neg_of_add_eq {a b c : A} (H : a + c = b) : a = b + -c :=
H ▸ !add_neg_cancel_right⁻¹
theorem eq_neg_add_of_add_eq {a b c : A} (H : b + a = c) : a = -b + c :=
H ▸ !neg_add_cancel_left⁻¹
theorem neg_add_eq_of_eq_add {a b c : A} (H : b = a + c) : -a + b = c :=
H⁻¹ ▸ !neg_add_cancel_left
theorem add_neg_eq_of_eq_add {a b c : A} (H : a = c + b) : a + -b = c :=
H⁻¹ ▸ !add_neg_cancel_right
theorem eq_add_of_add_neg_eq {a b c : A} (H : a + -c = b) : a = b + c :=
!neg_neg ▸ (eq_add_neg_of_add_eq H)
theorem eq_add_of_neg_add_eq {a b c : A} (H : -b + a = c) : a = b + c :=
!neg_neg ▸ (eq_neg_add_of_add_eq H)
theorem add_eq_of_eq_neg_add {a b c : A} (H : b = -a + c) : a + b = c :=
!neg_neg ▸ (neg_add_eq_of_eq_add H)
theorem add_eq_of_eq_add_neg {a b c : A} (H : a = c + -b) : a + b = c :=
!neg_neg ▸ (add_neg_eq_of_eq_add H)
theorem add_eq_iff_eq_neg_add (a b c : A) : a + b = c ↔ b = -a + c :=
iff.intro eq_neg_add_of_add_eq add_eq_of_eq_neg_add
theorem add_eq_iff_eq_add_neg (a b c : A) : a + b = c ↔ a = c + -b :=
iff.intro eq_add_neg_of_add_eq add_eq_of_eq_add_neg
theorem add_left_cancel {a b c : A} (H : a + b = a + c) : b = c :=
calc b = -a + (a + b) : !neg_add_cancel_left⁻¹
... = -a + (a + c) : H
... = c : neg_add_cancel_left
theorem add_right_cancel {a b c : A} (H : a + b = c + b) : a = c :=
calc a = (a + b) + -b : !add_neg_cancel_right⁻¹
... = (c + b) + -b : H
... = c : add_neg_cancel_right
definition add_group.to_left_cancel_semigroup [instance] [coercion] [reducible] :
add_left_cancel_semigroup A :=
⦃ add_left_cancel_semigroup, s,
add_left_cancel := @add_left_cancel A s ⦄
definition add_group.to_add_right_cancel_semigroup [instance] [coercion] [reducible] :
add_right_cancel_semigroup A :=
⦃ add_right_cancel_semigroup, s,
add_right_cancel := @add_right_cancel A s ⦄
/- sub -/
-- TODO: derive corresponding facts for div in a field
definition sub [reducible] (a b : A) : A := a + -b
infix `-` := sub
theorem sub_eq_add_neg (a b : A) : a - b = a + -b := rfl
theorem sub_self (a : A) : a - a = 0 := !add.right_inv
theorem sub_add_cancel (a b : A) : a - b + b = a := !neg_add_cancel_right
theorem add_sub_cancel (a b : A) : a + b - b = a := !add_neg_cancel_right
theorem eq_of_sub_eq_zero {a b : A} (H : a - b = 0) : a = b :=
calc
a = (a - b) + b : !sub_add_cancel⁻¹
... = 0 + b : H
... = b : zero_add
theorem eq_iff_sub_eq_zero (a b : A) : a = b ↔ a - b = 0 :=
iff.intro (assume H, H ▸ !sub_self) (assume H, eq_of_sub_eq_zero H)
theorem zero_sub (a : A) : 0 - a = -a := !zero_add
theorem sub_zero (a : A) : a - 0 = a := subst (eq.symm neg_zero) !add_zero
theorem sub_neg_eq_add (a b : A) : a - (-b) = a + b := !neg_neg ▸ rfl
theorem neg_sub (a b : A) : -(a - b) = b - a :=
neg_eq_of_add_eq_zero
(calc
a - b + (b - a) = a - b + b - a : by rewrite -add.assoc
... = a - a : sub_add_cancel
... = 0 : sub_self)
theorem add_sub (a b c : A) : a + (b - c) = a + b - c := !add.assoc⁻¹
theorem sub_add_eq_sub_sub_swap (a b c : A) : a - (b + c) = a - c - b :=
calc
a - (b + c) = a + (-c - b) : neg_add_rev
... = a - c - b : by rewrite add.assoc
theorem sub_eq_iff_eq_add (a b c : A) : a - b = c ↔ a = c + b :=
iff.intro (assume H, eq_add_of_add_neg_eq H) (assume H, add_neg_eq_of_eq_add H)
theorem eq_sub_iff_add_eq (a b c : A) : a = b - c ↔ a + c = b :=
iff.intro (assume H, add_eq_of_eq_add_neg H) (assume H, eq_add_neg_of_add_eq H)
theorem eq_iff_eq_of_sub_eq_sub {a b c d : A} (H : a - b = c - d) : a = b ↔ c = d :=
calc
a = b ↔ a - b = 0 : eq_iff_sub_eq_zero
... = (c - d = 0) : H
... ↔ c = d : iff.symm (eq_iff_sub_eq_zero c d)
theorem eq_sub_of_add_eq {a b c : A} (H : a + c = b) : a = b - c :=
!eq_add_neg_of_add_eq H
theorem sub_eq_of_eq_add {a b c : A} (H : a = c + b) : a - b = c :=
!add_neg_eq_of_eq_add H
theorem eq_add_of_sub_eq {a b c : A} (H : a - c = b) : a = b + c :=
eq_add_of_add_neg_eq H
theorem add_eq_of_eq_sub {a b c : A} (H : a = c - b) : a + b = c :=
add_eq_of_eq_add_neg H
end add_group
structure add_comm_group [class] (A : Type) extends add_group A, add_comm_monoid A
section add_comm_group
variable [s : add_comm_group A]
include s
theorem sub_add_eq_sub_sub (a b c : A) : a - (b + c) = a - b - c :=
!add.comm ▸ !sub_add_eq_sub_sub_swap
theorem neg_add_eq_sub (a b : A) : -a + b = b - a := !add.comm
theorem neg_add (a b : A) : -(a + b) = -a + -b := add.comm (-b) (-a) ▸ neg_add_rev a b
theorem sub_add_eq_add_sub (a b c : A) : a - b + c = a + c - b := !add.right_comm
theorem sub_sub (a b c : A) : a - b - c = a - (b + c) :=
by rewrite [▸ a + -b + -c = _, add.assoc, -neg_add]
theorem add_sub_add_left_eq_sub (a b c : A) : (c + a) - (c + b) = a - b :=
by rewrite ⟨sub_add_eq_sub_sub, (add.comm c a), add_sub_cancel⟩
theorem eq_sub_of_add_eq' {a b c : A} (H : c + a = b) : a = b - c :=
!eq_sub_of_add_eq (!add.comm ▸ H)
theorem sub_eq_of_eq_add' {a b c : A} (H : a = b + c) : a - b = c :=
!sub_eq_of_eq_add (!add.comm ▸ H)
theorem eq_add_of_sub_eq' {a b c : A} (H : a - b = c) : a = b + c :=
!add.comm ▸ eq_add_of_sub_eq H
theorem add_eq_of_eq_sub' {a b c : A} (H : b = c - a) : a + b = c :=
!add.comm ▸ add_eq_of_eq_sub H
end add_comm_group
end algebra
|
942c6f70aa3dc1602851b14241e92c34544fc935 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/analysis/normed_space/lp_space.lean | 9cfaeab54c8aa2549b5b7d2dffb0f71c85f6bb58 | [
"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 | 42,870 | lean | /-
Copyright (c) 2021 Heather Macbeth. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Heather Macbeth
-/
import analysis.mean_inequalities
import analysis.mean_inequalities_pow
import topology.algebra.order.liminf_limsup
/-!
# ℓp space
This file describes properties of elements `f` of a pi-type `Π i, E i` with finite "norm",
defined for `p:ℝ≥0∞` as the size of the support of `f` if `p=0`, `(∑' a, ‖f a‖^p) ^ (1/p)` for
`0 < p < ∞` and `⨆ a, ‖f a‖` for `p=∞`.
The Prop-valued `mem_ℓp f p` states that a function `f : Π i, E i` has finite norm according
to the above definition; that is, `f` has finite support if `p = 0`, `summable (λ a, ‖f a‖^p)` if
`0 < p < ∞`, and `bdd_above (norm '' (set.range f))` if `p = ∞`.
The space `lp E p` is the subtype of elements of `Π i : α, E i` which satisfy `mem_ℓp f p`. For
`1 ≤ p`, the "norm" is genuinely a norm and `lp` is a complete metric space.
## Main definitions
* `mem_ℓp f p` : property that the function `f` satisfies, as appropriate, `f` finitely supported
if `p = 0`, `summable (λ a, ‖f a‖^p)` if `0 < p < ∞`, and `bdd_above (norm '' (set.range f))` if
`p = ∞`.
* `lp E p` : elements of `Π i : α, E i` such that `mem_ℓp f p`. Defined as an `add_subgroup` of
a type synonym `pre_lp` for `Π i : α, E i`, and equipped with a `normed_add_comm_group` structure.
Under appropriate conditions, this is also equipped with the instances `lp.normed_space`,
`lp.complete_space`. For `p=∞`, there is also `lp.infty_normed_ring`,
`lp.infty_normed_algebra`, `lp.infty_star_ring` and `lp.infty_cstar_ring`.
## Main results
* `mem_ℓp.of_exponent_ge`: For `q ≤ p`, a function which is `mem_ℓp` for `q` is also `mem_ℓp` for
`p`
* `lp.mem_ℓp_of_tendsto`, `lp.norm_le_of_tendsto`: A pointwise limit of functions in `lp`, all with
`lp` norm `≤ C`, is itself in `lp` and has `lp` norm `≤ C`.
* `lp.tsum_mul_le_mul_norm`: basic form of Hölder's inequality
## Implementation
Since `lp` is defined as an `add_subgroup`, dot notation does not work. Use `lp.norm_neg f` to
say that `‖-f‖ = ‖f‖`, instead of the non-working `f.norm_neg`.
## TODO
* More versions of Hölder's inequality (for example: the case `p = 1`, `q = ∞`; a version for normed
rings which has `‖∑' i, f i * g i‖` rather than `∑' i, ‖f i‖ * g i‖` on the RHS; a version for
three exponents satisfying `1 / r = 1 / p + 1 / q`)
-/
noncomputable theory
open_locale nnreal ennreal big_operators
variables {α : Type*} {E : α → Type*} {p q : ℝ≥0∞} [Π i, normed_add_comm_group (E i)]
/-!
### `mem_ℓp` predicate
-/
/-- The property that `f : Π i : α, E i`
* is finitely supported, if `p = 0`, or
* admits an upper bound for `set.range (λ i, ‖f i‖)`, if `p = ∞`, or
* has the series `∑' i, ‖f i‖ ^ p` be summable, if `0 < p < ∞`. -/
def mem_ℓp (f : Π i, E i) (p : ℝ≥0∞) : Prop :=
if p = 0 then (set.finite {i | f i ≠ 0}) else
(if p = ∞ then bdd_above (set.range (λ i, ‖f i‖)) else summable (λ i, ‖f i‖ ^ p.to_real))
lemma mem_ℓp_zero_iff {f : Π i, E i} : mem_ℓp f 0 ↔ set.finite {i | f i ≠ 0} :=
by dsimp [mem_ℓp]; rw [if_pos rfl]
lemma mem_ℓp_zero {f : Π i, E i} (hf : set.finite {i | f i ≠ 0}) : mem_ℓp f 0 :=
mem_ℓp_zero_iff.2 hf
lemma mem_ℓp_infty_iff {f : Π i, E i} : mem_ℓp f ∞ ↔ bdd_above (set.range (λ i, ‖f i‖)) :=
by dsimp [mem_ℓp]; rw [if_neg ennreal.top_ne_zero, if_pos rfl]
lemma mem_ℓp_infty {f : Π i, E i} (hf : bdd_above (set.range (λ i, ‖f i‖))) : mem_ℓp f ∞ :=
mem_ℓp_infty_iff.2 hf
lemma mem_ℓp_gen_iff (hp : 0 < p.to_real) {f : Π i, E i} :
mem_ℓp f p ↔ summable (λ i, ‖f i‖ ^ p.to_real) :=
begin
rw ennreal.to_real_pos_iff at hp,
dsimp [mem_ℓp],
rw [if_neg hp.1.ne', if_neg hp.2.ne],
end
lemma mem_ℓp_gen {f : Π i, E i} (hf : summable (λ i, ‖f i‖ ^ p.to_real)) :
mem_ℓp f p :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ apply mem_ℓp_zero,
have H : summable (λ i : α, (1:ℝ)) := by simpa using hf,
exact (finite_of_summable_const (by norm_num) H).subset (set.subset_univ _) },
{ apply mem_ℓp_infty,
have H : summable (λ i : α, (1:ℝ)) := by simpa using hf,
simpa using ((finite_of_summable_const (by norm_num) H).image (λ i, ‖f i‖)).bdd_above },
exact (mem_ℓp_gen_iff hp).2 hf
end
lemma mem_ℓp_gen' {C : ℝ} {f : Π i, E i} (hf : ∀ s : finset α, ∑ i in s, ‖f i‖ ^ p.to_real ≤ C) :
mem_ℓp f p :=
begin
apply mem_ℓp_gen,
use ⨆ s : finset α, ∑ i in s, ‖f i‖ ^ p.to_real,
apply has_sum_of_is_lub_of_nonneg,
{ intros b,
exact real.rpow_nonneg_of_nonneg (norm_nonneg _) _ },
apply is_lub_csupr,
use C,
rintros - ⟨s, rfl⟩,
exact hf s
end
lemma zero_mem_ℓp : mem_ℓp (0 : Π i, E i) p :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ apply mem_ℓp_zero,
simp },
{ apply mem_ℓp_infty,
simp only [norm_zero, pi.zero_apply],
exact bdd_above_singleton.mono set.range_const_subset, },
{ apply mem_ℓp_gen,
simp [real.zero_rpow hp.ne', summable_zero], }
end
lemma zero_mem_ℓp' : mem_ℓp (λ i : α, (0 : E i)) p := zero_mem_ℓp
namespace mem_ℓp
lemma finite_dsupport {f : Π i, E i} (hf : mem_ℓp f 0) : set.finite {i | f i ≠ 0} :=
mem_ℓp_zero_iff.1 hf
lemma bdd_above {f : Π i, E i} (hf : mem_ℓp f ∞) : bdd_above (set.range (λ i, ‖f i‖)) :=
mem_ℓp_infty_iff.1 hf
lemma summable (hp : 0 < p.to_real) {f : Π i, E i} (hf : mem_ℓp f p) :
summable (λ i, ‖f i‖ ^ p.to_real) :=
(mem_ℓp_gen_iff hp).1 hf
lemma neg {f : Π i, E i} (hf : mem_ℓp f p) : mem_ℓp (-f) p :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ apply mem_ℓp_zero,
simp [hf.finite_dsupport] },
{ apply mem_ℓp_infty,
simpa using hf.bdd_above },
{ apply mem_ℓp_gen,
simpa using hf.summable hp },
end
@[simp] lemma neg_iff {f : Π i, E i} : mem_ℓp (-f) p ↔ mem_ℓp f p :=
⟨λ h, neg_neg f ▸ h.neg, mem_ℓp.neg⟩
lemma of_exponent_ge {p q : ℝ≥0∞} {f : Π i, E i}
(hfq : mem_ℓp f q) (hpq : q ≤ p) :
mem_ℓp f p :=
begin
rcases ennreal.trichotomy₂ hpq with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, hp⟩ | ⟨rfl, rfl⟩ | ⟨hq, rfl⟩
| ⟨hq, hp, hpq'⟩,
{ exact hfq },
{ apply mem_ℓp_infty,
obtain ⟨C, hC⟩ := (hfq.finite_dsupport.image (λ i, ‖f i‖)).bdd_above,
use max 0 C,
rintros x ⟨i, rfl⟩,
by_cases hi : f i = 0,
{ simp [hi] },
{ exact (hC ⟨i, hi, rfl⟩).trans (le_max_right _ _) } },
{ apply mem_ℓp_gen,
have : ∀ i ∉ hfq.finite_dsupport.to_finset, ‖f i‖ ^ p.to_real = 0,
{ intros i hi,
have : f i = 0 := by simpa using hi,
simp [this, real.zero_rpow hp.ne'] },
exact summable_of_ne_finset_zero this },
{ exact hfq },
{ apply mem_ℓp_infty,
obtain ⟨A, hA⟩ := (hfq.summable hq).tendsto_cofinite_zero.bdd_above_range_of_cofinite,
use A ^ (q.to_real⁻¹),
rintros x ⟨i, rfl⟩,
have : 0 ≤ ‖f i‖ ^ q.to_real := real.rpow_nonneg_of_nonneg (norm_nonneg _) _,
simpa [← real.rpow_mul, mul_inv_cancel hq.ne'] using
real.rpow_le_rpow this (hA ⟨i, rfl⟩) (inv_nonneg.mpr hq.le) },
{ apply mem_ℓp_gen,
have hf' := hfq.summable hq,
refine summable_of_norm_bounded_eventually _ hf' (@set.finite.subset _ {i | 1 ≤ ‖f i‖} _ _ _),
{ have H : {x : α | 1 ≤ ‖f x‖ ^ q.to_real}.finite,
{ simpa using eventually_lt_of_tendsto_lt (by norm_num : (0:ℝ) < 1)
hf'.tendsto_cofinite_zero },
exact H.subset (λ i hi, real.one_le_rpow hi hq.le) },
{ show ∀ i, ¬ (|‖f i‖ ^ p.to_real| ≤ ‖f i‖ ^ q.to_real) → 1 ≤ ‖f i‖,
intros i hi,
have : 0 ≤ ‖f i‖ ^ p.to_real := real.rpow_nonneg_of_nonneg (norm_nonneg _) p.to_real,
simp only [abs_of_nonneg, this] at hi,
contrapose! hi,
exact real.rpow_le_rpow_of_exponent_ge' (norm_nonneg _) hi.le hq.le hpq' } }
end
lemma add {f g : Π i, E i} (hf : mem_ℓp f p) (hg : mem_ℓp g p) : mem_ℓp (f + g) p :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ apply mem_ℓp_zero,
refine (hf.finite_dsupport.union hg.finite_dsupport).subset (λ i, _),
simp only [pi.add_apply, ne.def, set.mem_union, set.mem_set_of_eq],
contrapose!,
rintros ⟨hf', hg'⟩,
simp [hf', hg'] },
{ apply mem_ℓp_infty,
obtain ⟨A, hA⟩ := hf.bdd_above,
obtain ⟨B, hB⟩ := hg.bdd_above,
refine ⟨A + B, _⟩,
rintros a ⟨i, rfl⟩,
exact le_trans (norm_add_le _ _) (add_le_add (hA ⟨i, rfl⟩) (hB ⟨i, rfl⟩)) },
apply mem_ℓp_gen,
let C : ℝ := if p.to_real < 1 then 1 else 2 ^ (p.to_real - 1),
refine summable_of_nonneg_of_le _ (λ i, _) (((hf.summable hp).add (hg.summable hp)).mul_left C),
{ exact λ b, real.rpow_nonneg_of_nonneg (norm_nonneg (f b + g b)) p.to_real },
{ refine (real.rpow_le_rpow (norm_nonneg _) (norm_add_le _ _) hp.le).trans _,
dsimp [C],
split_ifs with h h,
{ simpa using nnreal.coe_le_coe.2 (nnreal.rpow_add_le_add_rpow (‖f i‖₊) (‖g i‖₊) hp.le h.le) },
{ let F : fin 2 → ℝ≥0 := ![‖f i‖₊, ‖g i‖₊],
have : ∀ i, (0:ℝ) ≤ F i := λ i, (F i).coe_nonneg,
simp only [not_lt] at h,
simpa [F, fin.sum_univ_succ] using
real.rpow_sum_le_const_mul_sum_rpow_of_nonneg (finset.univ : finset (fin 2)) h
(λ i _, (F i).coe_nonneg) } }
end
lemma sub {f g : Π i, E i} (hf : mem_ℓp f p) (hg : mem_ℓp g p) : mem_ℓp (f - g) p :=
by { rw sub_eq_add_neg, exact hf.add hg.neg }
lemma finset_sum {ι} (s : finset ι) {f : ι → Π i, E i} (hf : ∀ i ∈ s, mem_ℓp (f i) p) :
mem_ℓp (λ a, ∑ i in s, f i a) p :=
begin
haveI : decidable_eq ι := classical.dec_eq _,
revert hf,
refine finset.induction_on s _ _,
{ simp only [zero_mem_ℓp', finset.sum_empty, implies_true_iff], },
{ intros i s his ih hf,
simp only [his, finset.sum_insert, not_false_iff],
exact (hf i (s.mem_insert_self i)).add (ih (λ j hj, hf j (finset.mem_insert_of_mem hj))), },
end
section normed_space
variables {𝕜 : Type*} [normed_field 𝕜] [Π i, normed_space 𝕜 (E i)]
lemma const_smul {f : Π i, E i} (hf : mem_ℓp f p) (c : 𝕜) : mem_ℓp (c • f) p :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ apply mem_ℓp_zero,
refine hf.finite_dsupport.subset (λ i, (_ : ¬c • f i = 0 → ¬f i = 0)),
exact not_imp_not.mpr (λ hf', hf'.symm ▸ (smul_zero c)) },
{ obtain ⟨A, hA⟩ := hf.bdd_above,
refine mem_ℓp_infty ⟨‖c‖ * A, _⟩,
rintros a ⟨i, rfl⟩,
simpa [norm_smul] using mul_le_mul_of_nonneg_left (hA ⟨i, rfl⟩) (norm_nonneg c) },
{ apply mem_ℓp_gen,
convert (hf.summable hp).mul_left (‖c‖ ^ p.to_real),
ext i,
simp [norm_smul, real.mul_rpow (norm_nonneg c) (norm_nonneg (f i))] },
end
lemma const_mul {f : α → 𝕜} (hf : mem_ℓp f p) (c : 𝕜) : mem_ℓp (λ x, c * f x) p :=
@mem_ℓp.const_smul α (λ i, 𝕜) _ _ 𝕜 _ _ _ hf c
end normed_space
end mem_ℓp
/-!
### lp space
The space of elements of `Π i, E i` satisfying the predicate `mem_ℓp`.
-/
/-- We define `pre_lp E` to be a type synonym for `Π i, E i` which, importantly, does not inherit
the `pi` topology on `Π i, E i` (otherwise this topology would descend to `lp E p` and conflict
with the normed group topology we will later equip it with.)
We choose to deal with this issue by making a type synonym for `Π i, E i` rather than for the `lp`
subgroup itself, because this allows all the spaces `lp E p` (for varying `p`) to be subgroups of
the same ambient group, which permits lemma statements like `lp.monotone` (below). -/
@[derive add_comm_group, nolint unused_arguments]
def pre_lp (E : α → Type*) [Π i, normed_add_comm_group (E i)] : Type* := Π i, E i
instance pre_lp.unique [is_empty α] : unique (pre_lp E) := pi.unique_of_is_empty E
/-- lp space -/
def lp (E : α → Type*) [Π i, normed_add_comm_group (E i)]
(p : ℝ≥0∞) : add_subgroup (pre_lp E) :=
{ carrier := {f | mem_ℓp f p},
zero_mem' := zero_mem_ℓp,
add_mem' := λ f g, mem_ℓp.add,
neg_mem' := λ f, mem_ℓp.neg }
namespace lp
instance : has_coe (lp E p) (Π i, E i) := coe_subtype
instance : has_coe_to_fun (lp E p) (λ _, Π i, E i) := ⟨λ f, ((f : Π i, E i) : Π i, E i)⟩
@[ext] lemma ext {f g : lp E p} (h : (f : Π i, E i) = g) : f = g :=
subtype.ext h
protected lemma ext_iff {f g : lp E p} : f = g ↔ (f : Π i, E i) = g :=
subtype.ext_iff
lemma eq_zero' [is_empty α] (f : lp E p) : f = 0 := subsingleton.elim f 0
protected lemma monotone {p q : ℝ≥0∞} (hpq : q ≤ p) : lp E q ≤ lp E p :=
λ f hf, mem_ℓp.of_exponent_ge hf hpq
protected lemma mem_ℓp (f : lp E p) : mem_ℓp f p := f.prop
variables (E p)
@[simp] lemma coe_fn_zero : ⇑(0 : lp E p) = 0 := rfl
variables {E p}
@[simp] lemma coe_fn_neg (f : lp E p) : ⇑(-f) = -f := rfl
@[simp] lemma coe_fn_add (f g : lp E p) : ⇑(f + g) = f + g := rfl
@[simp] lemma coe_fn_sum {ι : Type*} (f : ι → lp E p) (s : finset ι) :
⇑(∑ i in s, f i) = ∑ i in s, ⇑(f i) :=
begin
classical,
refine finset.induction _ _ s,
{ simp },
intros i s his,
simp [finset.sum_insert his],
end
@[simp] lemma coe_fn_sub (f g : lp E p) : ⇑(f - g) = f - g := rfl
instance : has_norm (lp E p) :=
{ norm := λ f, if hp : p = 0 then by subst hp; exact (lp.mem_ℓp f).finite_dsupport.to_finset.card
else (if p = ∞ then ⨆ i, ‖f i‖ else (∑' i, ‖f i‖ ^ p.to_real) ^ (1/p.to_real)) }
lemma norm_eq_card_dsupport (f : lp E 0) : ‖f‖ = (lp.mem_ℓp f).finite_dsupport.to_finset.card :=
dif_pos rfl
lemma norm_eq_csupr (f : lp E ∞) : ‖f‖ = ⨆ i, ‖f i‖ :=
begin
dsimp [norm],
rw [dif_neg ennreal.top_ne_zero, if_pos rfl]
end
lemma is_lub_norm [nonempty α] (f : lp E ∞) : is_lub (set.range (λ i, ‖f i‖)) ‖f‖ :=
begin
rw lp.norm_eq_csupr,
exact is_lub_csupr (lp.mem_ℓp f)
end
lemma norm_eq_tsum_rpow (hp : 0 < p.to_real) (f : lp E p) :
‖f‖ = (∑' i, ‖f i‖ ^ p.to_real) ^ (1/p.to_real) :=
begin
dsimp [norm],
rw ennreal.to_real_pos_iff at hp,
rw [dif_neg hp.1.ne', if_neg hp.2.ne],
end
lemma norm_rpow_eq_tsum (hp : 0 < p.to_real) (f : lp E p) :
‖f‖ ^ p.to_real = ∑' i, ‖f i‖ ^ p.to_real :=
begin
rw [norm_eq_tsum_rpow hp, ← real.rpow_mul],
{ field_simp [hp.ne'] },
apply tsum_nonneg,
intros i,
calc (0:ℝ) = 0 ^ p.to_real : by rw real.zero_rpow hp.ne'
... ≤ _ : real.rpow_le_rpow rfl.le (norm_nonneg (f i)) hp.le
end
lemma has_sum_norm (hp : 0 < p.to_real) (f : lp E p) :
has_sum (λ i, ‖f i‖ ^ p.to_real) (‖f‖ ^ p.to_real) :=
begin
rw norm_rpow_eq_tsum hp,
exact ((lp.mem_ℓp f).summable hp).has_sum
end
lemma norm_nonneg' (f : lp E p) : 0 ≤ ‖f‖ :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ simp [lp.norm_eq_card_dsupport f] },
{ cases is_empty_or_nonempty α with _i _i; resetI,
{ rw lp.norm_eq_csupr,
simp [real.csupr_empty] },
inhabit α,
exact (norm_nonneg (f default)).trans ((lp.is_lub_norm f).1 ⟨default, rfl⟩) },
{ rw lp.norm_eq_tsum_rpow hp f,
refine real.rpow_nonneg_of_nonneg (tsum_nonneg _) _,
exact λ i, real.rpow_nonneg_of_nonneg (norm_nonneg _) _ },
end
@[simp] lemma norm_zero : ‖(0 : lp E p)‖ = 0 :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ simp [lp.norm_eq_card_dsupport] },
{ simp [lp.norm_eq_csupr] },
{ rw lp.norm_eq_tsum_rpow hp,
have hp' : 1 / p.to_real ≠ 0 := one_div_ne_zero hp.ne',
simpa [real.zero_rpow hp.ne'] using real.zero_rpow hp' }
end
lemma norm_eq_zero_iff {f : lp E p} : ‖f‖ = 0 ↔ f = 0 :=
begin
classical,
refine ⟨λ h, _, by { rintros rfl, exact norm_zero }⟩,
rcases p.trichotomy with rfl | rfl | hp,
{ ext i,
have : {i : α | ¬f i = 0} = ∅ := by simpa [lp.norm_eq_card_dsupport f] using h,
have : (¬ (f i = 0)) = false := congr_fun this i,
tauto },
{ cases is_empty_or_nonempty α with _i _i; resetI,
{ simp },
have H : is_lub (set.range (λ i, ‖f i‖)) 0,
{ simpa [h] using lp.is_lub_norm f },
ext i,
have : ‖f i‖ = 0 := le_antisymm (H.1 ⟨i, rfl⟩) (norm_nonneg _),
simpa using this },
{ have hf : has_sum (λ (i : α), ‖f i‖ ^ p.to_real) 0,
{ have := lp.has_sum_norm hp f,
rwa [h, real.zero_rpow hp.ne'] at this },
have : ∀ i, 0 ≤ ‖f i‖ ^ p.to_real := λ i, real.rpow_nonneg_of_nonneg (norm_nonneg _) _,
rw has_sum_zero_iff_of_nonneg this at hf,
ext i,
have : f i = 0 ∧ p.to_real ≠ 0,
{ simpa [real.rpow_eq_zero_iff_of_nonneg (norm_nonneg (f i))] using congr_fun hf i },
exact this.1 },
end
lemma eq_zero_iff_coe_fn_eq_zero {f : lp E p} : f = 0 ↔ ⇑f = 0 :=
by rw [lp.ext_iff, coe_fn_zero]
@[simp] lemma norm_neg ⦃f : lp E p⦄ : ‖-f‖ = ‖f‖ :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ simp [lp.norm_eq_card_dsupport] },
{ cases is_empty_or_nonempty α; resetI,
{ simp [lp.eq_zero' f], },
apply (lp.is_lub_norm (-f)).unique,
simpa using lp.is_lub_norm f },
{ suffices : ‖-f‖ ^ p.to_real = ‖f‖ ^ p.to_real,
{ exact real.rpow_left_inj_on hp.ne' (norm_nonneg' _) (norm_nonneg' _) this },
apply (lp.has_sum_norm hp (-f)).unique,
simpa using lp.has_sum_norm hp f }
end
instance [hp : fact (1 ≤ p)] : normed_add_comm_group (lp E p) :=
add_group_norm.to_normed_add_comm_group
{ to_fun := norm,
map_zero' := norm_zero,
neg' := norm_neg,
add_le' := λ f g, begin
unfreezingI { rcases p.dichotomy with rfl | hp' },
{ casesI is_empty_or_nonempty α,
{ simp [lp.eq_zero' f] },
refine (lp.is_lub_norm (f + g)).2 _,
rintros x ⟨i, rfl⟩,
refine le_trans _ (add_mem_upper_bounds_add (lp.is_lub_norm f).1 (lp.is_lub_norm g).1
⟨_, _, ⟨i, rfl⟩, ⟨i, rfl⟩, rfl⟩),
exact norm_add_le (f i) (g i) },
{ have hp'' : 0 < p.to_real := zero_lt_one.trans_le hp',
have hf₁ : ∀ i, 0 ≤ ‖f i‖ := λ i, norm_nonneg _,
have hg₁ : ∀ i, 0 ≤ ‖g i‖ := λ i, norm_nonneg _,
have hf₂ := lp.has_sum_norm hp'' f,
have hg₂ := lp.has_sum_norm hp'' g,
-- apply Minkowski's inequality
obtain ⟨C, hC₁, hC₂, hCfg⟩ :=
real.Lp_add_le_has_sum_of_nonneg hp' hf₁ hg₁ (norm_nonneg' _) (norm_nonneg' _) hf₂ hg₂,
refine le_trans _ hC₂,
rw ← real.rpow_le_rpow_iff (norm_nonneg' (f + g)) hC₁ hp'',
refine has_sum_le _ (lp.has_sum_norm hp'' (f + g)) hCfg,
intros i,
exact real.rpow_le_rpow (norm_nonneg _) (norm_add_le _ _) hp''.le },
end,
eq_zero_of_map_eq_zero' := λ f, norm_eq_zero_iff.1 }
-- TODO: define an `ennreal` version of `is_conjugate_exponent`, and then express this inequality
-- in a better version which also covers the case `p = 1, q = ∞`.
/-- Hölder inequality -/
protected lemma tsum_mul_le_mul_norm {p q : ℝ≥0∞}
(hpq : p.to_real.is_conjugate_exponent q.to_real) (f : lp E p) (g : lp E q) :
summable (λ i, ‖f i‖ * ‖g i‖) ∧ ∑' i, ‖f i‖ * ‖g i‖ ≤ ‖f‖ * ‖g‖ :=
begin
have hf₁ : ∀ i, 0 ≤ ‖f i‖ := λ i, norm_nonneg _,
have hg₁ : ∀ i, 0 ≤ ‖g i‖ := λ i, norm_nonneg _,
have hf₂ := lp.has_sum_norm hpq.pos f,
have hg₂ := lp.has_sum_norm hpq.symm.pos g,
obtain ⟨C, -, hC', hC⟩ :=
real.inner_le_Lp_mul_Lq_has_sum_of_nonneg hpq (norm_nonneg' _) (norm_nonneg' _) hf₁ hg₁ hf₂ hg₂,
rw ← hC.tsum_eq at hC',
exact ⟨hC.summable, hC'⟩
end
protected lemma summable_mul {p q : ℝ≥0∞}
(hpq : p.to_real.is_conjugate_exponent q.to_real) (f : lp E p) (g : lp E q) :
summable (λ i, ‖f i‖ * ‖g i‖) :=
(lp.tsum_mul_le_mul_norm hpq f g).1
protected lemma tsum_mul_le_mul_norm' {p q : ℝ≥0∞}
(hpq : p.to_real.is_conjugate_exponent q.to_real) (f : lp E p) (g : lp E q) :
∑' i, ‖f i‖ * ‖g i‖ ≤ ‖f‖ * ‖g‖ :=
(lp.tsum_mul_le_mul_norm hpq f g).2
section compare_pointwise
lemma norm_apply_le_norm (hp : p ≠ 0) (f : lp E p) (i : α) : ‖f i‖ ≤ ‖f‖ :=
begin
rcases eq_or_ne p ∞ with rfl | hp',
{ haveI : nonempty α := ⟨i⟩,
exact (is_lub_norm f).1 ⟨i, rfl⟩ },
have hp'' : 0 < p.to_real := ennreal.to_real_pos hp hp',
have : ∀ i, 0 ≤ ‖f i‖ ^ p.to_real,
{ exact λ i, real.rpow_nonneg_of_nonneg (norm_nonneg _) _ },
rw ← real.rpow_le_rpow_iff (norm_nonneg _) (norm_nonneg' _) hp'',
convert le_has_sum (has_sum_norm hp'' f) i (λ i hi, this i),
end
lemma sum_rpow_le_norm_rpow (hp : 0 < p.to_real) (f : lp E p) (s : finset α) :
∑ i in s, ‖f i‖ ^ p.to_real ≤ ‖f‖ ^ p.to_real :=
begin
rw lp.norm_rpow_eq_tsum hp f,
have : ∀ i, 0 ≤ ‖f i‖ ^ p.to_real,
{ exact λ i, real.rpow_nonneg_of_nonneg (norm_nonneg _) _ },
refine sum_le_tsum _ (λ i hi, this i) _,
exact (lp.mem_ℓp f).summable hp
end
lemma norm_le_of_forall_le' [nonempty α] {f : lp E ∞} (C : ℝ) (hCf : ∀ i, ‖f i‖ ≤ C) : ‖f‖ ≤ C :=
begin
refine (is_lub_norm f).2 _,
rintros - ⟨i, rfl⟩,
exact hCf i,
end
lemma norm_le_of_forall_le {f : lp E ∞} {C : ℝ} (hC : 0 ≤ C) (hCf : ∀ i, ‖f i‖ ≤ C) : ‖f‖ ≤ C :=
begin
casesI is_empty_or_nonempty α,
{ simpa [eq_zero' f] using hC, },
{ exact norm_le_of_forall_le' C hCf },
end
lemma norm_le_of_tsum_le (hp : 0 < p.to_real) {C : ℝ} (hC : 0 ≤ C) {f : lp E p}
(hf : ∑' i, ‖f i‖ ^ p.to_real ≤ C ^ p.to_real) :
‖f‖ ≤ C :=
begin
rw [← real.rpow_le_rpow_iff (norm_nonneg' _) hC hp, norm_rpow_eq_tsum hp],
exact hf,
end
lemma norm_le_of_forall_sum_le (hp : 0 < p.to_real) {C : ℝ} (hC : 0 ≤ C) {f : lp E p}
(hf : ∀ s : finset α, ∑ i in s, ‖f i‖ ^ p.to_real ≤ C ^ p.to_real) :
‖f‖ ≤ C :=
norm_le_of_tsum_le hp hC (tsum_le_of_sum_le ((lp.mem_ℓp f).summable hp) hf)
end compare_pointwise
section normed_space
variables {𝕜 : Type*} [normed_field 𝕜] [Π i, normed_space 𝕜 (E i)]
instance : module 𝕜 (pre_lp E) := pi.module α E 𝕜
lemma mem_lp_const_smul (c : 𝕜) (f : lp E p) : c • (f : pre_lp E) ∈ lp E p :=
(lp.mem_ℓp f).const_smul c
variables (E p 𝕜)
/-- The `𝕜`-submodule of elements of `Π i : α, E i` whose `lp` norm is finite. This is `lp E p`,
with extra structure. -/
def _root_.lp_submodule : submodule 𝕜 (pre_lp E) :=
{ smul_mem' := λ c f hf, by simpa using mem_lp_const_smul c ⟨f, hf⟩,
.. lp E p }
variables {E p 𝕜}
lemma coe_lp_submodule : (lp_submodule E p 𝕜).to_add_subgroup = lp E p := rfl
instance : module 𝕜 (lp E p) :=
{ .. (lp_submodule E p 𝕜).module }
@[simp] lemma coe_fn_smul (c : 𝕜) (f : lp E p) : ⇑(c • f) = c • f := rfl
lemma norm_const_smul (hp : p ≠ 0) {c : 𝕜} (f : lp E p) : ‖c • f‖ = ‖c‖ * ‖f‖ :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ exact absurd rfl hp },
{ cases is_empty_or_nonempty α; resetI,
{ simp [lp.eq_zero' f], },
apply (lp.is_lub_norm (c • f)).unique,
convert (lp.is_lub_norm f).mul_left (norm_nonneg c),
ext a,
simp [coe_fn_smul, norm_smul] },
{ suffices : ‖c • f‖ ^ p.to_real = (‖c‖ * ‖f‖) ^ p.to_real,
{ refine real.rpow_left_inj_on hp.ne' _ _ this,
{ exact norm_nonneg' _ },
{ exact mul_nonneg (norm_nonneg _) (norm_nonneg' _) } },
apply (lp.has_sum_norm hp (c • f)).unique,
convert (lp.has_sum_norm hp f).mul_left (‖c‖ ^ p.to_real),
{ simp [coe_fn_smul, norm_smul, real.mul_rpow (norm_nonneg c) (norm_nonneg _)] },
have hf : 0 ≤ ‖f‖ := lp.norm_nonneg' f,
simp [coe_fn_smul, norm_smul, real.mul_rpow (norm_nonneg c) hf] }
end
instance [fact (1 ≤ p)] : normed_space 𝕜 (lp E p) :=
{ norm_smul_le := λ c f, begin
have hp : 0 < p := zero_lt_one.trans_le (fact.out _),
simp [norm_const_smul hp.ne']
end }
variables {𝕜' : Type*} [normed_field 𝕜']
instance [Π i, normed_space 𝕜' (E i)] [has_smul 𝕜' 𝕜] [Π i, is_scalar_tower 𝕜' 𝕜 (E i)] :
is_scalar_tower 𝕜' 𝕜 (lp E p) :=
begin
refine ⟨λ r c f, _⟩,
ext1,
exact (lp.coe_fn_smul _ _).trans (smul_assoc _ _ _)
end
end normed_space
section normed_star_group
variables [Π i, star_add_monoid (E i)] [Π i, normed_star_group (E i)]
lemma _root_.mem_ℓp.star_mem {f : Π i, E i}
(hf : mem_ℓp f p) : mem_ℓp (star f) p :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ apply mem_ℓp_zero,
simp [hf.finite_dsupport] },
{ apply mem_ℓp_infty,
simpa using hf.bdd_above },
{ apply mem_ℓp_gen,
simpa using hf.summable hp },
end
@[simp] lemma _root_.mem_ℓp.star_iff {f : Π i, E i} : mem_ℓp (star f) p ↔ mem_ℓp f p :=
⟨λ h, star_star f ▸ mem_ℓp.star_mem h ,mem_ℓp.star_mem⟩
instance : has_star (lp E p) :=
{ star := λ f, ⟨(star f : Π i, E i), f.property.star_mem⟩}
@[simp] lemma coe_fn_star (f : lp E p) : ⇑(star f) = star f := rfl
@[simp] protected theorem star_apply (f : lp E p) (i : α) : star f i = star (f i) := rfl
instance : has_involutive_star (lp E p) := { star_involutive := λ x, by {ext, simp} }
instance : star_add_monoid (lp E p) := { star_add := λ f g, ext $ star_add _ _ }
instance [hp : fact (1 ≤ p)] : normed_star_group (lp E p) :=
{ norm_star := λ f,
begin
unfreezingI { rcases p.trichotomy with rfl | rfl | h },
{ exfalso,
have := ennreal.to_real_mono ennreal.zero_ne_top hp.elim,
norm_num at this,},
{ simp only [lp.norm_eq_csupr, lp.star_apply, norm_star] },
{ simp only [lp.norm_eq_tsum_rpow h, lp.star_apply, norm_star] }
end }
variables {𝕜 : Type*} [has_star 𝕜] [normed_field 𝕜]
variables [Π i, normed_space 𝕜 (E i)] [Π i, star_module 𝕜 (E i)]
instance : star_module 𝕜 (lp E p) := { star_smul := λ r f, ext $ star_smul _ _ }
end normed_star_group
section non_unital_normed_ring
variables {I : Type*} {B : I → Type*} [Π i, non_unital_normed_ring (B i)]
lemma _root_.mem_ℓp.infty_mul {f g : Π i, B i} (hf : mem_ℓp f ∞) (hg : mem_ℓp g ∞) :
mem_ℓp (f * g) ∞ :=
begin
rw mem_ℓp_infty_iff,
obtain ⟨⟨Cf, hCf⟩, ⟨Cg, hCg⟩⟩ := ⟨hf.bdd_above, hg.bdd_above⟩,
refine ⟨Cf * Cg, _⟩,
rintros _ ⟨i, rfl⟩,
calc ‖(f * g) i‖ ≤ ‖f i‖ * ‖g i‖ : norm_mul_le (f i) (g i)
... ≤ Cf * Cg : mul_le_mul (hCf ⟨i, rfl⟩) (hCg ⟨i, rfl⟩) (norm_nonneg _)
((norm_nonneg _).trans (hCf ⟨i, rfl⟩))
end
instance : has_mul (lp B ∞) :=
{ mul := λ f g, ⟨(f * g : Π i, B i) , f.property.infty_mul g.property⟩}
@[simp] lemma infty_coe_fn_mul (f g : lp B ∞) : ⇑(f * g) = f * g := rfl
instance : non_unital_ring (lp B ∞) :=
function.injective.non_unital_ring lp.has_coe_to_fun.coe (subtype.coe_injective)
(lp.coe_fn_zero B ∞) lp.coe_fn_add infty_coe_fn_mul lp.coe_fn_neg lp.coe_fn_sub
(λ _ _, rfl) (λ _ _,rfl)
instance : non_unital_normed_ring (lp B ∞) :=
{ norm_mul := λ f g, lp.norm_le_of_forall_le (mul_nonneg (norm_nonneg f) (norm_nonneg g))
(λ i, calc ‖(f * g) i‖ ≤ ‖f i‖ * ‖g i‖ : norm_mul_le _ _
... ≤ ‖f‖ * ‖g‖
: mul_le_mul (lp.norm_apply_le_norm ennreal.top_ne_zero f i)
(lp.norm_apply_le_norm ennreal.top_ne_zero g i) (norm_nonneg _) (norm_nonneg _)),
.. lp.normed_add_comm_group }
-- we also want a `non_unital_normed_comm_ring` instance, but this has to wait for #13719
instance infty_is_scalar_tower {𝕜} [normed_field 𝕜] [Π i, normed_space 𝕜 (B i)]
[Π i, is_scalar_tower 𝕜 (B i) (B i)] :
is_scalar_tower 𝕜 (lp B ∞) (lp B ∞) :=
⟨λ r f g, lp.ext $ smul_assoc r ⇑f ⇑g⟩
instance infty_smul_comm_class {𝕜} [normed_field 𝕜] [Π i, normed_space 𝕜 (B i)]
[Π i, smul_comm_class 𝕜 (B i) (B i)] :
smul_comm_class 𝕜 (lp B ∞) (lp B ∞) :=
⟨λ r f g, lp.ext $ smul_comm r ⇑f ⇑g⟩
section star_ring
variables [Π i, star_ring (B i)] [Π i, normed_star_group (B i)]
instance infty_star_ring : star_ring (lp B ∞) :=
{ star_mul := λ f g, ext $ star_mul (_ : Π i, B i) _,
.. (show star_add_monoid (lp B ∞),
by { letI : Π i, star_add_monoid (B i) := λ i, infer_instance, apply_instance }) }
instance infty_cstar_ring [∀ i, cstar_ring (B i)] : cstar_ring (lp B ∞) :=
{ norm_star_mul_self := λ f,
begin
apply le_antisymm,
{ rw ←sq,
refine lp.norm_le_of_forall_le (sq_nonneg ‖ f ‖) (λ i, _),
simp only [lp.star_apply, cstar_ring.norm_star_mul_self, ←sq, infty_coe_fn_mul, pi.mul_apply],
refine sq_le_sq' _ (lp.norm_apply_le_norm ennreal.top_ne_zero _ _),
linarith [norm_nonneg (f i), norm_nonneg f] },
{ rw [←sq, ←real.le_sqrt (norm_nonneg _) (norm_nonneg _)],
refine lp.norm_le_of_forall_le (‖star f * f‖.sqrt_nonneg) (λ i, _),
rw [real.le_sqrt (norm_nonneg _) (norm_nonneg _), sq, ←cstar_ring.norm_star_mul_self],
exact lp.norm_apply_le_norm ennreal.top_ne_zero (star f * f) i, }
end }
end star_ring
end non_unital_normed_ring
section normed_ring
variables {I : Type*} {B : I → Type*} [Π i, normed_ring (B i)]
instance _root_.pre_lp.ring : ring (pre_lp B) := pi.ring
variables [Π i, norm_one_class (B i)]
lemma _root_.one_mem_ℓp_infty : mem_ℓp (1 : Π i, B i) ∞ :=
⟨1, by { rintros i ⟨i, rfl⟩, exact norm_one.le,}⟩
variables (B)
/-- The `𝕜`-subring of elements of `Π i : α, B i` whose `lp` norm is finite. This is `lp E ∞`,
with extra structure. -/
def _root_.lp_infty_subring : subring (pre_lp B) :=
{ carrier := {f | mem_ℓp f ∞},
one_mem' := one_mem_ℓp_infty,
mul_mem' := λ f g hf hg, hf.infty_mul hg,
.. lp B ∞ }
variables {B}
instance infty_ring : ring (lp B ∞) := (lp_infty_subring B).to_ring
lemma _root_.mem_ℓp.infty_pow {f : Π i, B i} (hf : mem_ℓp f ∞) (n : ℕ) : mem_ℓp (f ^ n) ∞ :=
(lp_infty_subring B).pow_mem hf n
lemma _root_.nat_cast_mem_ℓp_infty (n : ℕ) : mem_ℓp (n : Π i, B i) ∞ :=
nat_cast_mem (lp_infty_subring B) n
lemma _root_.int_cast_mem_ℓp_infty (z : ℤ) : mem_ℓp (z : Π i, B i) ∞ :=
coe_int_mem (lp_infty_subring B) z
@[simp] lemma infty_coe_fn_one : ⇑(1 : lp B ∞) = 1 := rfl
@[simp] lemma infty_coe_fn_pow (f : lp B ∞) (n : ℕ) : ⇑(f ^ n) = f ^ n := rfl
@[simp] lemma infty_coe_fn_nat_cast (n : ℕ) : ⇑(n : lp B ∞) = n := rfl
@[simp] lemma infty_coe_fn_int_cast (z : ℤ) : ⇑(z : lp B ∞) = z := rfl
instance [nonempty I] : norm_one_class (lp B ∞) :=
{ norm_one := by simp_rw [lp.norm_eq_csupr, infty_coe_fn_one, pi.one_apply, norm_one, csupr_const]}
instance infty_normed_ring : normed_ring (lp B ∞) :=
{ .. lp.infty_ring, .. lp.non_unital_normed_ring }
end normed_ring
section normed_comm_ring
variables {I : Type*} {B : I → Type*} [Π i, normed_comm_ring (B i)] [∀ i, norm_one_class (B i)]
instance infty_comm_ring : comm_ring (lp B ∞) :=
{ mul_comm := λ f g, by { ext, simp only [lp.infty_coe_fn_mul, pi.mul_apply, mul_comm] },
.. lp.infty_ring }
instance infty_normed_comm_ring : normed_comm_ring (lp B ∞) :=
{ .. lp.infty_comm_ring, .. lp.infty_normed_ring }
end normed_comm_ring
section algebra
variables {I : Type*} {𝕜 : Type*} {B : I → Type*}
variables [normed_field 𝕜] [Π i, normed_ring (B i)] [Π i, normed_algebra 𝕜 (B i)]
/-- A variant of `pi.algebra` that lean can't find otherwise. -/
instance _root_.pi.algebra_of_normed_algebra : algebra 𝕜 (Π i, B i) :=
@pi.algebra I 𝕜 B _ _ $ λ i, normed_algebra.to_algebra
instance _root_.pre_lp.algebra : algebra 𝕜 (pre_lp B) := _root_.pi.algebra_of_normed_algebra
variables [∀ i, norm_one_class (B i)]
lemma _root_.algebra_map_mem_ℓp_infty (k : 𝕜) : mem_ℓp (algebra_map 𝕜 (Π i, B i) k) ∞ :=
begin
rw algebra.algebra_map_eq_smul_one,
exact (one_mem_ℓp_infty.const_smul k : mem_ℓp (k • 1 : Π i, B i) ∞)
end
variables (𝕜 B)
/-- The `𝕜`-subalgebra of elements of `Π i : α, B i` whose `lp` norm is finite. This is `lp E ∞`,
with extra structure. -/
def _root_.lp_infty_subalgebra : subalgebra 𝕜 (pre_lp B) :=
{ carrier := {f | mem_ℓp f ∞},
algebra_map_mem' := algebra_map_mem_ℓp_infty,
.. lp_infty_subring B }
variables {𝕜 B}
instance infty_normed_algebra : normed_algebra 𝕜 (lp B ∞) :=
{ ..(lp_infty_subalgebra 𝕜 B).algebra,
..(lp.normed_space : normed_space 𝕜 (lp B ∞)) }
end algebra
section single
variables {𝕜 : Type*} [normed_field 𝕜] [Π i, normed_space 𝕜 (E i)]
variables [decidable_eq α]
/-- The element of `lp E p` which is `a : E i` at the index `i`, and zero elsewhere. -/
protected def single (p) (i : α) (a : E i) : lp E p :=
⟨ λ j, if h : j = i then eq.rec a h.symm else 0,
begin
refine (mem_ℓp_zero _).of_exponent_ge (zero_le p),
refine (set.finite_singleton i).subset _,
intros j,
simp only [forall_exists_index, set.mem_singleton_iff, ne.def, dite_eq_right_iff,
set.mem_set_of_eq, not_forall],
rintros rfl,
simp,
end ⟩
protected lemma single_apply (p) (i : α) (a : E i) (j : α) :
lp.single p i a j = if h : j = i then eq.rec a h.symm else 0 :=
rfl
protected lemma single_apply_self (p) (i : α) (a : E i) :
lp.single p i a i = a :=
by rw [lp.single_apply, dif_pos rfl]
protected lemma single_apply_ne (p) (i : α) (a : E i) {j : α} (hij : j ≠ i) :
lp.single p i a j = 0 :=
by rw [lp.single_apply, dif_neg hij]
@[simp] protected lemma single_neg (p) (i : α) (a : E i) :
lp.single p i (- a) = - lp.single p i a :=
begin
ext j,
by_cases hi : j = i,
{ subst hi,
simp [lp.single_apply_self] },
{ simp [lp.single_apply_ne p i _ hi] }
end
@[simp] protected lemma single_smul (p) (i : α) (a : E i) (c : 𝕜) :
lp.single p i (c • a) = c • lp.single p i a :=
begin
ext j,
by_cases hi : j = i,
{ subst hi,
simp [lp.single_apply_self] },
{ simp [lp.single_apply_ne p i _ hi] }
end
protected lemma norm_sum_single (hp : 0 < p.to_real) (f : Π i, E i) (s : finset α) :
‖∑ i in s, lp.single p i (f i)‖ ^ p.to_real = ∑ i in s, ‖f i‖ ^ p.to_real :=
begin
refine (has_sum_norm hp (∑ i in s, lp.single p i (f i))).unique _,
simp only [lp.single_apply, coe_fn_sum, finset.sum_apply, finset.sum_dite_eq],
have h : ∀ i ∉ s, ‖ite (i ∈ s) (f i) 0‖ ^ p.to_real = 0,
{ intros i hi,
simp [if_neg hi, real.zero_rpow hp.ne'], },
have h' : ∀ i ∈ s, ‖f i‖ ^ p.to_real = ‖ite (i ∈ s) (f i) 0‖ ^ p.to_real,
{ intros i hi,
rw if_pos hi },
simpa [finset.sum_congr rfl h'] using has_sum_sum_of_ne_finset_zero h,
end
protected lemma norm_single (hp : 0 < p.to_real) (f : Π i, E i) (i : α) :
‖lp.single p i (f i)‖ = ‖f i‖ :=
begin
refine real.rpow_left_inj_on hp.ne' (norm_nonneg' _) (norm_nonneg _) _,
simpa using lp.norm_sum_single hp f {i},
end
protected lemma norm_sub_norm_compl_sub_single (hp : 0 < p.to_real) (f : lp E p) (s : finset α) :
‖f‖ ^ p.to_real - ‖f - ∑ i in s, lp.single p i (f i)‖ ^ p.to_real = ∑ i in s, ‖f i‖ ^ p.to_real :=
begin
refine ((has_sum_norm hp f).sub (has_sum_norm hp (f - ∑ i in s, lp.single p i (f i)))).unique _,
let F : α → ℝ := λ i, ‖f i‖ ^ p.to_real - ‖(f - ∑ i in s, lp.single p i (f i)) i‖ ^ p.to_real,
have hF : ∀ i ∉ s, F i = 0,
{ intros i hi,
suffices : ‖f i‖ ^ p.to_real - ‖f i - ite (i ∈ s) (f i) 0‖ ^ p.to_real = 0,
{ simpa only [F, coe_fn_sum, lp.single_apply, coe_fn_sub, pi.sub_apply, finset.sum_apply,
finset.sum_dite_eq] using this, },
simp only [if_neg hi, sub_zero, sub_self] },
have hF' : ∀ i ∈ s, F i = ‖f i‖ ^ p.to_real,
{ intros i hi,
simp only [F, coe_fn_sum, lp.single_apply, if_pos hi, sub_self, eq_self_iff_true,
coe_fn_sub, pi.sub_apply, finset.sum_apply, finset.sum_dite_eq, sub_eq_self],
simp [real.zero_rpow hp.ne'], },
have : has_sum F (∑ i in s, F i) := has_sum_sum_of_ne_finset_zero hF,
rwa [finset.sum_congr rfl hF'] at this,
end
protected lemma norm_compl_sum_single (hp : 0 < p.to_real) (f : lp E p) (s : finset α) :
‖f - ∑ i in s, lp.single p i (f i)‖ ^ p.to_real = ‖f‖ ^ p.to_real - ∑ i in s, ‖f i‖ ^ p.to_real :=
by linarith [lp.norm_sub_norm_compl_sub_single hp f s]
/-- The canonical finitely-supported approximations to an element `f` of `lp` converge to it, in the
`lp` topology. -/
protected lemma has_sum_single [fact (1 ≤ p)] (hp : p ≠ ⊤) (f : lp E p) :
has_sum (λ i : α, lp.single p i (f i : E i)) f :=
begin
have hp₀ : 0 < p := zero_lt_one.trans_le (fact.out _),
have hp' : 0 < p.to_real := ennreal.to_real_pos hp₀.ne' hp,
have := lp.has_sum_norm hp' f,
rw [has_sum, metric.tendsto_nhds] at this ⊢,
intros ε hε,
refine (this _ (real.rpow_pos_of_pos hε p.to_real)).mono _,
intros s hs,
rw ← real.rpow_lt_rpow_iff dist_nonneg (le_of_lt hε) hp',
rw dist_comm at hs,
simp only [dist_eq_norm, real.norm_eq_abs] at hs ⊢,
have H : ‖∑ i in s, lp.single p i (f i : E i) - f‖ ^ p.to_real
= ‖f‖ ^ p.to_real - ∑ i in s, ‖f i‖ ^ p.to_real,
{ simpa only [coe_fn_neg, pi.neg_apply, lp.single_neg, finset.sum_neg_distrib,
neg_sub_neg, norm_neg, _root_.norm_neg] using lp.norm_compl_sum_single hp' (-f) s },
rw ← H at hs,
have : |‖∑ i in s, lp.single p i (f i : E i) - f‖ ^ p.to_real|
= ‖∑ i in s, lp.single p i (f i : E i) - f‖ ^ p.to_real,
{ simp only [real.abs_rpow_of_nonneg (norm_nonneg _), abs_norm] },
linarith
end
end single
section topology
open filter
open_locale topology uniformity
/-- The coercion from `lp E p` to `Π i, E i` is uniformly continuous. -/
lemma uniform_continuous_coe [_i : fact (1 ≤ p)] : uniform_continuous (coe : lp E p → Π i, E i) :=
begin
have hp : p ≠ 0 := (zero_lt_one.trans_le _i.elim).ne',
rw uniform_continuous_pi,
intros i,
rw normed_add_comm_group.uniformity_basis_dist.uniform_continuous_iff
normed_add_comm_group.uniformity_basis_dist,
intros ε hε,
refine ⟨ε, hε, _⟩,
rintros f g (hfg : ‖f - g‖ < ε),
have : ‖f i - g i‖ ≤ ‖f - g‖ := norm_apply_le_norm hp (f - g) i,
exact this.trans_lt hfg,
end
variables {ι : Type*} {l : filter ι} [filter.ne_bot l]
lemma norm_apply_le_of_tendsto {C : ℝ} {F : ι → lp E ∞} (hCF : ∀ᶠ k in l, ‖F k‖ ≤ C)
{f : Π a, E a} (hf : tendsto (id (λ i, F i) : ι → Π a, E a) l (𝓝 f)) (a : α) :
‖f a‖ ≤ C :=
begin
have : tendsto (λ k, ‖F k a‖) l (𝓝 ‖f a‖) :=
(tendsto.comp (continuous_apply a).continuous_at hf).norm,
refine le_of_tendsto this (hCF.mono _),
intros k hCFk,
exact (norm_apply_le_norm ennreal.top_ne_zero (F k) a).trans hCFk,
end
variables [_i : fact (1 ≤ p)]
include _i
lemma sum_rpow_le_of_tendsto (hp : p ≠ ∞) {C : ℝ} {F : ι → lp E p} (hCF : ∀ᶠ k in l, ‖F k‖ ≤ C)
{f : Π a, E a} (hf : tendsto (id (λ i, F i) : ι → Π a, E a) l (𝓝 f)) (s : finset α) :
∑ (i : α) in s, ‖f i‖ ^ p.to_real ≤ C ^ p.to_real :=
begin
have hp' : p ≠ 0 := (zero_lt_one.trans_le _i.elim).ne',
have hp'' : 0 < p.to_real := ennreal.to_real_pos hp' hp,
let G : (Π a, E a) → ℝ := λ f, ∑ a in s, ‖f a‖ ^ p.to_real,
have hG : continuous G,
{ refine continuous_finset_sum s _,
intros a ha,
have : continuous (λ f : Π a, E a, f a):= continuous_apply a,
exact this.norm.rpow_const (λ _, or.inr hp''.le) },
refine le_of_tendsto (hG.continuous_at.tendsto.comp hf) _,
refine hCF.mono _,
intros k hCFk,
refine (lp.sum_rpow_le_norm_rpow hp'' (F k) s).trans _,
exact real.rpow_le_rpow (norm_nonneg _) hCFk hp''.le,
end
/-- "Semicontinuity of the `lp` norm": If all sufficiently large elements of a sequence in `lp E p`
have `lp` norm `≤ C`, then the pointwise limit, if it exists, also has `lp` norm `≤ C`. -/
lemma norm_le_of_tendsto {C : ℝ} {F : ι → lp E p} (hCF : ∀ᶠ k in l, ‖F k‖ ≤ C) {f : lp E p}
(hf : tendsto (id (λ i, F i) : ι → Π a, E a) l (𝓝 f)) :
‖f‖ ≤ C :=
begin
obtain ⟨i, hi⟩ := hCF.exists,
have hC : 0 ≤ C := (norm_nonneg _).trans hi,
unfreezingI { rcases eq_top_or_lt_top p with rfl | hp },
{ apply norm_le_of_forall_le hC,
exact norm_apply_le_of_tendsto hCF hf, },
{ have : 0 < p := zero_lt_one.trans_le _i.elim,
have hp' : 0 < p.to_real := ennreal.to_real_pos this.ne' hp.ne,
apply norm_le_of_forall_sum_le hp' hC,
exact sum_rpow_le_of_tendsto hp.ne hCF hf, }
end
/-- If `f` is the pointwise limit of a bounded sequence in `lp E p`, then `f` is in `lp E p`. -/
lemma mem_ℓp_of_tendsto {F : ι → lp E p} (hF : metric.bounded (set.range F)) {f : Π a, E a}
(hf : tendsto (id (λ i, F i) : ι → Π a, E a) l (𝓝 f)) :
mem_ℓp f p :=
begin
obtain ⟨C, hC, hCF'⟩ := hF.exists_pos_norm_le,
have hCF : ∀ k, ‖F k‖ ≤ C := λ k, hCF' _ ⟨k, rfl⟩,
unfreezingI { rcases eq_top_or_lt_top p with rfl | hp },
{ apply mem_ℓp_infty,
use C,
rintros _ ⟨a, rfl⟩,
refine norm_apply_le_of_tendsto (eventually_of_forall hCF) hf a, },
{ apply mem_ℓp_gen',
exact sum_rpow_le_of_tendsto hp.ne (eventually_of_forall hCF) hf },
end
/-- If a sequence is Cauchy in the `lp E p` topology and pointwise convergent to a element `f` of
`lp E p`, then it converges to `f` in the `lp E p` topology. -/
lemma tendsto_lp_of_tendsto_pi {F : ℕ → lp E p} (hF : cauchy_seq F) {f : lp E p}
(hf : tendsto (id (λ i, F i) : ℕ → Π a, E a) at_top (𝓝 f)) :
tendsto F at_top (𝓝 f) :=
begin
rw metric.nhds_basis_closed_ball.tendsto_right_iff,
intros ε hε,
have hε' : {p : (lp E p) × (lp E p) | ‖p.1 - p.2‖ < ε} ∈ 𝓤 (lp E p),
{ exact normed_add_comm_group.uniformity_basis_dist.mem_of_mem hε },
refine (hF.eventually_eventually hε').mono _,
rintros n (hn : ∀ᶠ l in at_top, ‖(λ f, F n - f) (F l)‖ < ε),
refine norm_le_of_tendsto (hn.mono (λ k hk, hk.le)) _,
rw tendsto_pi_nhds,
intros a,
exact (hf.apply a).const_sub (F n a),
end
variables [Π a, complete_space (E a)]
instance : complete_space (lp E p) :=
metric.complete_of_cauchy_seq_tendsto
begin
intros F hF,
-- A Cauchy sequence in `lp E p` is pointwise convergent; let `f` be the pointwise limit.
obtain ⟨f, hf⟩ := cauchy_seq_tendsto_of_complete (uniform_continuous_coe.comp_cauchy_seq hF),
-- Since the Cauchy sequence is bounded, its pointwise limit `f` is in `lp E p`.
have hf' : mem_ℓp f p := mem_ℓp_of_tendsto hF.bounded_range hf,
-- And therefore `f` is its limit in the `lp E p` topology as well as pointwise.
exact ⟨⟨f, hf'⟩, tendsto_lp_of_tendsto_pi hF hf⟩
end
end topology
end lp
|
3e92dd7b2b0781e6b8f631d05928af666cf0ac47 | 64874bd1010548c7f5a6e3e8902efa63baaff785 | /tests/lean/run/fib_brec.lean | 2f2462c71cedf6efee5ebf17a4e6f10a873ad713 | [
"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 | 1,037 | lean | import data.nat.basic data.prod
open prod
namespace nat
namespace manual
definition brec_on {C : nat → Type} (n : nat) (F : Π (n : nat), @below C n → C n) : C n :=
have general : C n × @below C n, from
rec_on n
(pair (F zero unit.star) unit.star)
(λ (n₁ : nat) (r₁ : C n₁ × @below C n₁),
have b : @below C (succ n₁), from
r₁,
have c : C (succ n₁), from
F (succ n₁) b,
pair c b),
pr₁ general
end manual
definition fib (n : nat) :=
brec_on n (λ (n : nat),
cases_on n
(λ (b₀ : below zero), succ zero)
(λ (n₁ : nat), cases_on n₁
(λ b₁ : below (succ zero), succ zero)
(λ (n₂ : nat) (b₂ : below (succ (succ n₂))), pr₁ b₂ + pr₁ (pr₂ b₂))))
theorem fib_0 : fib 0 = 1 :=
rfl
theorem fib_1 : fib 1 = 1 :=
rfl
theorem fib_s_s (n : nat) : fib (succ (succ n)) = fib (succ n) + fib n :=
rfl
example : fib 5 = 8 :=
rfl
example : fib 9 = 55 :=
rfl
end nat
|
1361be731a3f4419ad2c145c08a95b6f7f77635f | 367134ba5a65885e863bdc4507601606690974c1 | /src/data/list/pairwise.lean | 52f09c5d10914d6173c37b2d8b99612335df249f | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 15,298 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import data.list.basic
open nat function
universes u v
variables {α : Type u} {β : Type v}
namespace list
/- pairwise relation (generalized no duplicate) -/
mk_iff_of_inductive_prop list.pairwise list.pairwise_iff
variable {R : α → α → Prop}
theorem rel_of_pairwise_cons {a : α} {l : list α}
(p : pairwise R (a::l)) : ∀ {a'}, a' ∈ l → R a a' :=
(pairwise_cons.1 p).1
theorem pairwise_of_pairwise_cons {a : α} {l : list α}
(p : pairwise R (a::l)) : pairwise R l :=
(pairwise_cons.1 p).2
theorem pairwise.tail : ∀ {l : list α} (p : pairwise R l), pairwise R l.tail
| [] h := h
| (a :: l) h := pairwise_of_pairwise_cons h
theorem pairwise.imp_of_mem {S : α → α → Prop} {l : list α}
(H : ∀ {a b}, a ∈ l → b ∈ l → R a b → S a b) (p : pairwise R l) : pairwise S l :=
begin
induction p with a l r p IH generalizing H; constructor,
{ exact ball.imp_right
(λ x h, H (mem_cons_self _ _) (mem_cons_of_mem _ h)) r },
{ exact IH (λ a b m m', H
(mem_cons_of_mem _ m) (mem_cons_of_mem _ m')) }
end
theorem pairwise.imp {S : α → α → Prop}
(H : ∀ a b, R a b → S a b) {l : list α} : pairwise R l → pairwise S l :=
pairwise.imp_of_mem (λ a b _ _, H a b)
theorem pairwise.and {S : α → α → Prop} {l : list α} :
pairwise (λ a b, R a b ∧ S a b) l ↔ pairwise R l ∧ pairwise S l :=
⟨λ h, ⟨h.imp (λ a b h, h.1), h.imp (λ a b h, h.2)⟩,
λ ⟨hR, hS⟩, begin
clear_, induction hR with a l R1 R2 IH;
simp only [pairwise.nil, pairwise_cons] at *,
exact ⟨λ b bl, ⟨R1 b bl, hS.1 b bl⟩, IH hS.2⟩
end⟩
theorem pairwise.imp₂ {S : α → α → Prop} {T : α → α → Prop}
(H : ∀ a b, R a b → S a b → T a b) {l : list α}
(hR : pairwise R l) (hS : pairwise S l) : pairwise T l :=
(pairwise.and.2 ⟨hR, hS⟩).imp $ λ a b, and.rec (H a b)
theorem pairwise.iff_of_mem {S : α → α → Prop} {l : list α}
(H : ∀ {a b}, a ∈ l → b ∈ l → (R a b ↔ S a b)) : pairwise R l ↔ pairwise S l :=
⟨pairwise.imp_of_mem (λ a b m m', (H m m').1),
pairwise.imp_of_mem (λ a b m m', (H m m').2)⟩
theorem pairwise.iff {S : α → α → Prop}
(H : ∀ a b, R a b ↔ S a b) {l : list α} : pairwise R l ↔ pairwise S l :=
pairwise.iff_of_mem (λ a b _ _, H a b)
theorem pairwise_of_forall {l : list α} (H : ∀ x y, R x y) : pairwise R l :=
by induction l; [exact pairwise.nil,
simp only [*, pairwise_cons, forall_2_true_iff, and_true]]
theorem pairwise.and_mem {l : list α} :
pairwise R l ↔ pairwise (λ x y, x ∈ l ∧ y ∈ l ∧ R x y) l :=
pairwise.iff_of_mem (by simp only [true_and, iff_self, forall_2_true_iff] {contextual := tt})
theorem pairwise.imp_mem {l : list α} :
pairwise R l ↔ pairwise (λ x y, x ∈ l → y ∈ l → R x y) l :=
pairwise.iff_of_mem
(by simp only [forall_prop_of_true, iff_self, forall_2_true_iff] {contextual := tt})
theorem pairwise_of_sublist : Π {l₁ l₂ : list α}, l₁ <+ l₂ → pairwise R l₂ → pairwise R l₁
| ._ ._ sublist.slnil h := h
| ._ ._ (sublist.cons l₁ l₂ a s) (pairwise.cons i n) := pairwise_of_sublist s n
| ._ ._ (sublist.cons2 l₁ l₂ a s) (pairwise.cons i n) :=
(pairwise_of_sublist s n).cons (ball.imp_left s.subset i)
theorem forall_of_forall_of_pairwise (H : symmetric R)
{l : list α} (H₁ : ∀ x ∈ l, R x x) (H₂ : pairwise R l) :
∀ (x ∈ l) (y ∈ l), R x y :=
begin
induction l with a l IH, { exact forall_mem_nil _ },
cases forall_mem_cons.1 H₁ with H₁₁ H₁₂,
cases pairwise_cons.1 H₂ with H₂₁ H₂₂,
rintro x (rfl | hx) y (rfl | hy),
exacts [H₁₁, H₂₁ _ hy, H (H₂₁ _ hx), IH H₁₂ H₂₂ _ hx _ hy]
end
lemma forall_of_pairwise (H : symmetric R) {l : list α}
(hl : pairwise R l) : (∀a∈l, ∀b∈l, a ≠ b → R a b) :=
forall_of_forall_of_pairwise
(λ a b h hne, H (h hne.symm))
(λ _ _ h, (h rfl).elim)
(pairwise.imp (λ _ _ h _, h) hl)
theorem pairwise_singleton (R) (a : α) : pairwise R [a] :=
by simp only [pairwise_cons, mem_singleton, forall_prop_of_false (not_mem_nil _), forall_true_iff,
pairwise.nil, and_true]
theorem pairwise_pair {a b : α} : pairwise R [a, b] ↔ R a b :=
by simp only [pairwise_cons, mem_singleton, forall_eq, forall_prop_of_false (not_mem_nil _),
forall_true_iff, pairwise.nil, and_true]
theorem pairwise_append {l₁ l₂ : list α} : pairwise R (l₁++l₂) ↔
pairwise R l₁ ∧ pairwise R l₂ ∧ ∀ x ∈ l₁, ∀ y ∈ l₂, R x y :=
by induction l₁ with x l₁ IH; [simp only [list.pairwise.nil, forall_prop_of_false (not_mem_nil _),
forall_true_iff, and_true, true_and, nil_append],
simp only [cons_append, pairwise_cons, forall_mem_append, IH, forall_mem_cons, forall_and_distrib,
and_assoc, and.left_comm]]
theorem pairwise_append_comm (s : symmetric R) {l₁ l₂ : list α} :
pairwise R (l₁++l₂) ↔ pairwise R (l₂++l₁) :=
have ∀ l₁ l₂ : list α,
(∀ (x : α), x ∈ l₁ → ∀ (y : α), y ∈ l₂ → R x y) →
(∀ (x : α), x ∈ l₂ → ∀ (y : α), y ∈ l₁ → R x y),
from λ l₁ l₂ a x xm y ym, s (a y ym x xm),
by simp only [pairwise_append, and.left_comm]; rw iff.intro (this l₁ l₂) (this l₂ l₁)
theorem pairwise_middle (s : symmetric R) {a : α} {l₁ l₂ : list α} :
pairwise R (l₁ ++ a::l₂) ↔ pairwise R (a::(l₁++l₂)) :=
show pairwise R (l₁ ++ ([a] ++ l₂)) ↔ pairwise R ([a] ++ l₁ ++ l₂),
by rw [← append_assoc, pairwise_append, @pairwise_append _ _ ([a] ++ l₁), pairwise_append_comm s];
simp only [mem_append, or_comm]
theorem pairwise_map (f : β → α) :
∀ {l : list β}, pairwise R (map f l) ↔ pairwise (λ a b : β, R (f a) (f b)) l
| [] := by simp only [map, pairwise.nil]
| (b::l) :=
have (∀ a b', b' ∈ l → f b' = a → R (f b) a) ↔ ∀ (b' : β), b' ∈ l → R (f b) (f b'), from
forall_swap.trans $ forall_congr $ λ a, forall_swap.trans $ by simp only [forall_eq'],
by simp only [map, pairwise_cons, mem_map, exists_imp_distrib, and_imp, this, pairwise_map]
theorem pairwise_of_pairwise_map {S : β → β → Prop} (f : α → β)
(H : ∀ a b : α, S (f a) (f b) → R a b) {l : list α}
(p : pairwise S (map f l)) : pairwise R l :=
((pairwise_map f).1 p).imp H
theorem pairwise_map_of_pairwise {S : β → β → Prop} (f : α → β)
(H : ∀ a b : α, R a b → S (f a) (f b)) {l : list α}
(p : pairwise R l) : pairwise S (map f l) :=
(pairwise_map f).2 $ p.imp H
theorem pairwise_filter_map (f : β → option α) {l : list β} :
pairwise R (filter_map f l) ↔ pairwise (λ a a' : β, ∀ (b ∈ f a) (b' ∈ f a'), R b b') l :=
let S (a a' : β) := ∀ (b ∈ f a) (b' ∈ f a'), R b b' in
begin
simp only [option.mem_def], induction l with a l IH,
{ simp only [filter_map, pairwise.nil] },
cases e : f a with b,
{ rw [filter_map_cons_none _ _ e, IH, pairwise_cons],
simp only [e, forall_prop_of_false not_false, forall_3_true_iff, true_and] },
rw [filter_map_cons_some _ _ _ e],
simp only [pairwise_cons, mem_filter_map, exists_imp_distrib, and_imp, IH, e, forall_eq'],
show (∀ (a' : α) (x : β), x ∈ l → f x = some a' → R b a') ∧ pairwise S l ↔
(∀ (a' : β), a' ∈ l → ∀ (b' : α), f a' = some b' → R b b') ∧ pairwise S l,
from and_congr ⟨λ h b mb a ma, h a b mb ma, λ h a b mb ma, h b mb a ma⟩ iff.rfl
end
theorem pairwise_filter_map_of_pairwise {S : β → β → Prop} (f : α → option β)
(H : ∀ (a a' : α), R a a' → ∀ (b ∈ f a) (b' ∈ f a'), S b b') {l : list α}
(p : pairwise R l) : pairwise S (filter_map f l) :=
(pairwise_filter_map _).2 $ p.imp H
theorem pairwise_filter (p : α → Prop) [decidable_pred p] {l : list α} :
pairwise R (filter p l) ↔ pairwise (λ x y, p x → p y → R x y) l :=
begin
rw [← filter_map_eq_filter, pairwise_filter_map],
apply pairwise.iff, intros, simp only [option.mem_def, option.guard_eq_some, and_imp, forall_eq'],
end
theorem pairwise_filter_of_pairwise (p : α → Prop) [decidable_pred p] {l : list α}
: pairwise R l → pairwise R (filter p l) :=
pairwise_of_sublist (filter_sublist _)
theorem pairwise_pmap {p : β → Prop} {f : Π b, p b → α} {l : list β} (h : ∀ x ∈ l, p x) :
pairwise R (l.pmap f h) ↔
pairwise (λ b₁ b₂, ∀ (h₁ : p b₁) (h₂ : p b₂), R (f b₁ h₁) (f b₂ h₂)) l :=
begin
induction l with a l ihl, { simp },
obtain ⟨ha, hl⟩ : p a ∧ ∀ b, b ∈ l → p b, by simpa using h,
simp only [ihl hl, pairwise_cons, bex_imp_distrib, pmap, and.congr_left_iff, mem_pmap],
refine λ _, ⟨λ H b hb hpa hpb, H _ _ hb rfl, _⟩,
rintro H _ b hb rfl,
exact H b hb _ _
end
theorem pairwise.pmap {l : list α} (hl : pairwise R l)
{p : α → Prop} {f : Π a, p a → β} (h : ∀ x ∈ l, p x) {S : β → β → Prop}
(hS : ∀ ⦃x⦄ (hx : p x) ⦃y⦄ (hy : p y), R x y → S (f x hx) (f y hy)) :
pairwise S (l.pmap f h) :=
begin
refine (pairwise_pmap h).2 (pairwise.imp_of_mem _ hl),
intros, apply hS, assumption
end
theorem pairwise_join {L : list (list α)} : pairwise R (join L) ↔
(∀ l ∈ L, pairwise R l) ∧ pairwise (λ l₁ l₂, ∀ (x ∈ l₁) (y ∈ l₂), R x y) L :=
begin
induction L with l L IH,
{simp only [join, pairwise.nil, forall_prop_of_false (not_mem_nil _), forall_const, and_self]},
have : (∀ (x : α), x ∈ l → ∀ (y : α) (x_1 : list α), x_1 ∈ L → y ∈ x_1 → R x y) ↔
∀ (a' : list α), a' ∈ L → ∀ (x : α), x ∈ l → ∀ (y : α), y ∈ a' → R x y :=
⟨λ h a b c d e, h c d e a b, λ h c d e a b, h a b c d e⟩,
simp only [join, pairwise_append, IH, mem_join, exists_imp_distrib, and_imp, this,
forall_mem_cons, pairwise_cons],
simp only [and_assoc, and_comm, and.left_comm],
end
@[simp] theorem pairwise_reverse : ∀ {R} {l : list α},
pairwise R (reverse l) ↔ pairwise (λ x y, R y x) l :=
suffices ∀ {R l}, @pairwise α R l → pairwise (λ x y, R y x) (reverse l),
from λ R l, ⟨λ p, reverse_reverse l ▸ this p, this⟩,
λ R l p, by induction p with a l h p IH;
[apply pairwise.nil, simpa only [reverse_cons, pairwise_append, IH,
pairwise_cons, forall_prop_of_false (not_mem_nil _), forall_true_iff,
pairwise.nil, mem_reverse, mem_singleton, forall_eq, true_and] using h]
theorem pairwise_iff_nth_le {R} : ∀ {l : list α},
pairwise R l ↔ ∀ i j (h₁ : j < length l) (h₂ : i < j),
R (nth_le l i (lt_trans h₂ h₁)) (nth_le l j h₁)
| [] := by simp only [pairwise.nil, true_iff]; exact λ i j h, (not_lt_zero j).elim h
| (a::l) := begin
rw [pairwise_cons, pairwise_iff_nth_le],
refine ⟨λ H i j h₁ h₂, _, λ H, ⟨λ a' m, _,
λ i j h₁ h₂, H _ _ (succ_lt_succ h₁) (succ_lt_succ h₂)⟩⟩,
{ cases j with j, {exact (not_lt_zero _).elim h₂},
cases i with i,
{ exact H.1 _ (nth_le_mem l _ _) },
{ exact H.2 _ _ (lt_of_succ_lt_succ h₁) (lt_of_succ_lt_succ h₂) } },
{ rcases nth_le_of_mem m with ⟨n, h, rfl⟩,
exact H _ _ (succ_lt_succ h) (succ_pos _) }
end
theorem pairwise_sublists' {R} : ∀ {l : list α}, pairwise R l →
pairwise (lex (swap R)) (sublists' l)
| _ pairwise.nil := pairwise_singleton _ _
| _ (@pairwise.cons _ _ a l H₁ H₂) :=
begin
simp only [sublists'_cons, pairwise_append, pairwise_map, mem_sublists', mem_map,
exists_imp_distrib, and_imp],
have IH := pairwise_sublists' H₂,
refine ⟨IH, IH.imp (λ l₁ l₂, lex.cons), _⟩,
intros l₁ sl₁ x l₂ sl₂ e, subst e,
cases l₁ with b l₁, {constructor},
exact lex.rel (H₁ _ $ sl₁.subset $ mem_cons_self _ _)
end
theorem pairwise_sublists {R} {l : list α} (H : pairwise R l) :
pairwise (λ l₁ l₂, lex R (reverse l₁) (reverse l₂)) (sublists l) :=
by have := pairwise_sublists' (pairwise_reverse.2 H);
rwa [sublists'_reverse, pairwise_map] at this
/- pairwise reduct -/
variable [decidable_rel R]
@[simp] theorem pw_filter_nil : pw_filter R [] = [] := rfl
@[simp] theorem pw_filter_cons_of_pos {a : α} {l : list α} (h : ∀ b ∈ pw_filter R l, R a b) :
pw_filter R (a::l) = a :: pw_filter R l := if_pos h
@[simp] theorem pw_filter_cons_of_neg {a : α} {l : list α} (h : ¬ ∀ b ∈ pw_filter R l, R a b) :
pw_filter R (a::l) = pw_filter R l := if_neg h
theorem pw_filter_map (f : β → α) :
Π (l : list β), pw_filter R (map f l) = map f (pw_filter (λ x y, R (f x) (f y)) l)
| [] := rfl
| (x :: xs) :=
if h : ∀ b ∈ pw_filter R (map f xs), R (f x) b
then have h' : ∀ (b : β), b ∈ pw_filter (λ (x y : β), R (f x) (f y)) xs → R (f x) (f b),
from λ b hb, h _ (by rw [pw_filter_map]; apply mem_map_of_mem _ hb),
by rw [map,pw_filter_cons_of_pos h,pw_filter_cons_of_pos h',pw_filter_map,map]
else have h' : ¬∀ (b : β), b ∈ pw_filter (λ (x y : β), R (f x) (f y)) xs → R (f x) (f b),
from λ hh, h $ λ a ha,
by { rw [pw_filter_map,mem_map] at ha, rcases ha with ⟨b,hb₀,hb₁⟩,
subst a, exact hh _ hb₀, },
by rw [map,pw_filter_cons_of_neg h,pw_filter_cons_of_neg h',pw_filter_map]
theorem pw_filter_sublist : ∀ (l : list α), pw_filter R l <+ l
| [] := nil_sublist _
| (x::l) := begin
by_cases (∀ y ∈ pw_filter R l, R x y),
{ rw [pw_filter_cons_of_pos h],
exact cons_sublist_cons _ (pw_filter_sublist l) },
{ rw [pw_filter_cons_of_neg h],
exact sublist_cons_of_sublist _ (pw_filter_sublist l) },
end
theorem pw_filter_subset (l : list α) : pw_filter R l ⊆ l :=
(pw_filter_sublist _).subset
theorem pairwise_pw_filter : ∀ (l : list α), pairwise R (pw_filter R l)
| [] := pairwise.nil
| (x::l) := begin
by_cases (∀ y ∈ pw_filter R l, R x y),
{ rw [pw_filter_cons_of_pos h],
exact pairwise_cons.2 ⟨h, pairwise_pw_filter l⟩ },
{ rw [pw_filter_cons_of_neg h],
exact pairwise_pw_filter l },
end
theorem pw_filter_eq_self {l : list α} : pw_filter R l = l ↔ pairwise R l :=
⟨λ e, e ▸ pairwise_pw_filter l, λ p, begin
induction l with x l IH, {refl},
cases pairwise_cons.1 p with al p,
rw [pw_filter_cons_of_pos (ball.imp_left (pw_filter_subset l) al), IH p],
end⟩
@[simp] theorem pw_filter_idempotent {l : list α} :
pw_filter R (pw_filter R l) = pw_filter R l :=
pw_filter_eq_self.mpr (pairwise_pw_filter l)
theorem forall_mem_pw_filter (neg_trans : ∀ {x y z}, R x z → R x y ∨ R y z)
(a : α) (l : list α) : (∀ b ∈ pw_filter R l, R a b) ↔ (∀ b ∈ l, R a b) :=
⟨begin
induction l with x l IH, { exact λ _ _, false.elim },
simp only [forall_mem_cons],
by_cases (∀ y ∈ pw_filter R l, R x y); dsimp at h,
{ simp only [pw_filter_cons_of_pos h, forall_mem_cons, and_imp],
exact λ r H, ⟨r, IH H⟩ },
{ rw [pw_filter_cons_of_neg h],
refine λ H, ⟨_, IH H⟩,
cases e : find (λ y, ¬ R x y) (pw_filter R l) with k,
{ refine h.elim (ball.imp_right _ (find_eq_none.1 e)),
exact λ y _, not_not.1 },
{ have := find_some e,
exact (neg_trans (H k (find_mem e))).resolve_right this } }
end, ball.imp_left (pw_filter_subset l)⟩
end list
|
dccb86540eaf768a8240004eb828d0e7b79cc54d | 8cb37a089cdb4af3af9d8bf1002b417e407a8e9e | /library/init/meta/attribute.lean | 5513cf125cf8853d3c318706c87c26e09772b67f | [
"Apache-2.0"
] | permissive | kbuzzard/lean | ae3c3db4bb462d750dbf7419b28bafb3ec983ef7 | ed1788fd674bb8991acffc8fca585ec746711928 | refs/heads/master | 1,620,983,366,617 | 1,618,937,600,000 | 1,618,937,600,000 | 359,886,396 | 1 | 0 | Apache-2.0 | 1,618,936,987,000 | 1,618,936,987,000 | null | UTF-8 | Lean | false | false | 5,887 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich
-/
prelude
import init.meta.tactic init.meta.rb_map init.meta.has_reflect init.meta.lean.parser
/-- Get all of the declaration names that have the given attribute.
Eg. ``get_instances `simp`` returns a list with the names of all of the lemmas in the environment tagged with the `@[simp]` attribute.
-/
meta constant attribute.get_instances : name → tactic (list name)
/-- Returns a hash of `get_instances`. You can use this to tell if your attribute instances have changed. -/
meta constant attribute.fingerprint : name → tactic nat
/-- Configuration for a user attribute cache. For example, the `simp` attribute has a cache of type simp_lemmas.
- `mk_cache` is a function where you are given all of the declarations tagged with your attribute and you return the new value for the cache.
That is, `mk_cache` makes the object you want to be cached.
- `dependencies` is a list of other attributes whose caches need to be computed first.
-/
meta structure user_attribute_cache_cfg (cache_ty : Type) :=
(mk_cache : list name → tactic cache_ty)
(dependencies : list name)
/-- Close the current goal by filling it with the trivial `user_attribute_cache_cfg unit`. -/
meta def user_attribute.dflt_cache_cfg : tactic unit :=
tactic.exact `(⟨λ _, pure (), []⟩ : user_attribute_cache_cfg unit)
meta def user_attribute.dflt_parser : tactic unit :=
tactic.exact `(pure () : lean.parser unit)
/--
A __user attribute__ is an attribute defined by the user (ie, not built in to Lean).
### Type parameters
- `cache_ty` is the type of a cached VM object that is computed from all of the declarations in the environment tagged with this attribute.
- `param_ty` is an argument for the attribute when it is used. For instance with `param_ty` being `ℕ` you could write `@[my_attribute 4]`.
### Data
A `user_attribute` consists of the following pieces of data:
- `name` is the name of the attribute, eg ```simp```
- `descr` is a plaintext description of the attribute for humans.
- `after_set` is an optional handler that will be called after the attribute has been applied to a declaration.
Failing the tactic will fail the entire `attribute/def/...` command, i.e. the attribute will
not be applied after all.
Declaring an `after_set` handler without a `before_unset` handler will make the attribute
non-removable.
- `before_unset` Optional handler that will be called before the attribute is removed from a declaration.
- `cache_cfg` describes how to construct the user attribute's cache. See docstring for `user_attribute_cache_cfg`
- `reflect_param` demands that `param_ty` can be reflected.
This means we have a function from `param_ty` to an expr.
See the docstring for `has_reflect`.
- `parser` Parser that will be invoked after parsing the attribute's name. The parse result will be reflected
and stored and can be retrieved with `user_attribute.get_param`.
-/
meta structure user_attribute (cache_ty : Type := unit) (param_ty : Type := unit) :=
(name : name)
(descr : string)
(after_set : option (Π (decl : _root_.name) (prio : nat) (persistent : bool), command) := none)
(before_unset : option (Π (decl : _root_.name) (persistent : bool), command) := none)
(cache_cfg : user_attribute_cache_cfg cache_ty . user_attribute.dflt_cache_cfg)
[reflect_param : has_reflect param_ty]
(parser : lean.parser param_ty . user_attribute.dflt_parser)
/-- Registers a new user-defined attribute. The argument must be the name of a definition of type
`user_attribute α β`. Once registered, you may tag declarations with this attribute. -/
meta def attribute.register (decl : name) : command :=
tactic.set_basic_attribute ``user_attribute decl tt
/-- Returns the attribute cache for the given user attribute. -/
meta constant user_attribute.get_cache {α β : Type} (attr : user_attribute α β) : tactic α
meta def user_attribute.parse_reflect {α β : Type} (attr : user_attribute α β) : lean.parser expr :=
(λ a, attr.reflect_param a) <$> attr.parser
meta constant user_attribute.get_param_untyped {α β : Type} (attr : user_attribute α β) (decl : name)
: tactic expr
meta constant user_attribute.set_untyped {α β : Type} [reflected β] (attr : user_attribute α β) (decl : name)
(val : expr) (persistent : bool) (prio : option nat := none) : tactic unit
/-- Get the value of the parameter for the attribute on a given declatation. Will fail if the attribute does not exist.-/
meta def user_attribute.get_param {α β : Type} [reflected β] (attr : user_attribute α β) (n : name) : tactic β :=
attr.get_param_untyped n >>= tactic.eval_expr β
meta def user_attribute.set {α β : Type} [reflected β] (attr : user_attribute α β) (n : name)
(val : β) (persistent : bool) (prio : option nat := none) : tactic unit :=
attr.set_untyped n (attr.reflect_param val) persistent prio
open tactic
/-- Alias for attribute.register -/
meta def register_attribute := attribute.register
meta def get_attribute_cache_dyn {α : Type} [reflected α] (attr_decl_name : name) : tactic α :=
let attr : pexpr := expr.const attr_decl_name [] in
do e ← to_expr ``(user_attribute.get_cache %%attr),
t ← eval_expr (tactic α) e,
t
meta def mk_name_set_attr (attr_name : name) : command :=
do let t := `(user_attribute name_set),
let v := `({name := attr_name,
descr := "name_set attribute",
cache_cfg := {
mk_cache := λ ns, return (name_set.of_list ns),
dependencies := []}} : user_attribute name_set),
add_meta_definition attr_name [] t v,
register_attribute attr_name
meta def get_name_set_for_attr (name : name) : tactic name_set :=
get_attribute_cache_dyn name
|
424322edb9e594eea1db91a65b69ff3a7407ad47 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/real/conjugate_exponents.lean | cadc87acfcfe9335c02604c6c42bece37a07dea4 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,786 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel, Yury Kudryashov
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.real.ennreal
import Mathlib.PostPort
universes l
namespace Mathlib
/-!
# Real conjugate exponents
`p.is_conjugate_exponent q` registers the fact that the real numbers `p` and `q` are `> 1` and
satisfy `1/p + 1/q = 1`. This property shows up often in analysis, especially when dealing with
`L^p` spaces.
We make several basic facts available through dot notation in this situation.
We also introduce `p.conjugate_exponent` for `p / (p-1)`. When `p > 1`, it is conjugate to `p`.
-/
namespace real
/-- Two real exponents `p, q` are conjugate if they are `> 1` and satisfy the equality
`1/p + 1/q = 1`. This condition shows up in many theorems in analysis, notably related to `L^p`
norms. -/
structure is_conjugate_exponent (p : ℝ) (q : ℝ)
where
one_lt : 1 < p
inv_add_inv_conj : 1 / p + 1 / q = 1
/-- The conjugate exponent of `p` is `q = p/(p-1)`, so that `1/p + 1/q = 1`. -/
def conjugate_exponent (p : ℝ) : ℝ :=
p / (p - 1)
namespace is_conjugate_exponent
/- Register several non-vanishing results following from the fact that `p` has a conjugate exponent
`q`: many computations using these exponents require clearing out denominators, which can be done
with `field_simp` given a proof that these denominators are non-zero, so we record the most usual
ones. -/
theorem pos {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 0 < p :=
lt_trans zero_lt_one (one_lt h)
theorem nonneg {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 0 ≤ p :=
le_of_lt (pos h)
theorem ne_zero {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : p ≠ 0 :=
ne_of_gt (pos h)
theorem sub_one_pos {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 0 < p - 1 :=
iff.mpr sub_pos (one_lt h)
theorem sub_one_ne_zero {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : p - 1 ≠ 0 :=
ne_of_gt (sub_one_pos h)
theorem one_div_pos {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 0 < 1 / p :=
iff.mpr one_div_pos (pos h)
theorem one_div_nonneg {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 0 ≤ 1 / p :=
le_of_lt (one_div_pos h)
theorem one_div_ne_zero {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 1 / p ≠ 0 :=
ne_of_gt (one_div_pos h)
theorem conj_eq {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : q = p / (p - 1) := sorry
theorem conjugate_eq {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : conjugate_exponent p = q :=
Eq.symm (conj_eq h)
theorem sub_one_mul_conj {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : (p - 1) * q = p :=
mul_comm q (p - 1) ▸ iff.mp (eq_div_iff (sub_one_ne_zero h)) (conj_eq h)
theorem mul_eq_add {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : p * q = p + q := sorry
protected theorem symm {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : is_conjugate_exponent q p := sorry
theorem one_lt_nnreal {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 1 < nnreal.of_real p := sorry
theorem inv_add_inv_conj_nnreal {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 1 / nnreal.of_real p + 1 / nnreal.of_real q = 1 := sorry
theorem inv_add_inv_conj_ennreal {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 1 / ennreal.of_real p + 1 / ennreal.of_real q = 1 := sorry
end is_conjugate_exponent
theorem is_conjugate_exponent_iff {p : ℝ} {q : ℝ} (h : 1 < p) : is_conjugate_exponent p q ↔ q = p / (p - 1) := sorry
theorem is_conjugate_exponent_conjugate_exponent {p : ℝ} (h : 1 < p) : is_conjugate_exponent p (conjugate_exponent p) :=
iff.mpr (is_conjugate_exponent_iff h) rfl
|
a38ef5745e45497d4ab4008ecccdfb36936d559a | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/logic/is_empty.lean | 9af86a2035601dc2af97b55b88b6843c084b2664 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 6,196 | lean | /-
Copyright (c) 2021 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import logic.function.basic
import tactic.protected
/-!
# Types that are empty
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> https://github.com/leanprover-community/mathlib4/pull/486
> Any changes to this file require a corresponding PR to mathlib4.
In this file we define a typeclass `is_empty`, which expresses that a type has no elements.
## Main declaration
* `is_empty`: a typeclass that expresses that a type is empty.
-/
variables {α β γ : Sort*}
/-- `is_empty α` expresses that `α` is empty. -/
@[protect_proj]
class is_empty (α : Sort*) : Prop :=
(false : α → false)
instance : is_empty empty := ⟨empty.elim⟩
instance : is_empty pempty := ⟨pempty.elim⟩
instance : is_empty false := ⟨id⟩
instance : is_empty (fin 0) := ⟨λ n, nat.not_lt_zero n.1 n.2⟩
protected lemma function.is_empty [is_empty β] (f : α → β) : is_empty α :=
⟨λ x, is_empty.false (f x)⟩
instance {p : α → Sort*} [h : nonempty α] [∀ x, is_empty (p x)] : is_empty (Π x, p x) :=
h.elim $ λ x, function.is_empty $ function.eval x
instance pprod.is_empty_left [is_empty α] : is_empty (pprod α β) :=
function.is_empty pprod.fst
instance pprod.is_empty_right [is_empty β] : is_empty (pprod α β) :=
function.is_empty pprod.snd
instance prod.is_empty_left {α β} [is_empty α] : is_empty (α × β) :=
function.is_empty prod.fst
instance prod.is_empty_right {α β} [is_empty β] : is_empty (α × β) :=
function.is_empty prod.snd
instance [is_empty α] [is_empty β] : is_empty (psum α β) :=
⟨λ x, psum.rec is_empty.false is_empty.false x⟩
instance {α β} [is_empty α] [is_empty β] : is_empty (α ⊕ β) :=
⟨λ x, sum.rec is_empty.false is_empty.false x⟩
/-- subtypes of an empty type are empty -/
instance [is_empty α] (p : α → Prop) : is_empty (subtype p) :=
⟨λ x, is_empty.false x.1⟩
/-- subtypes by an all-false predicate are false. -/
lemma subtype.is_empty_of_false {p : α → Prop} (hp : ∀ a, ¬(p a)) : is_empty (subtype p) :=
⟨λ x, hp _ x.2⟩
/-- subtypes by false are false. -/
instance subtype.is_empty_false : is_empty {a : α // false} :=
subtype.is_empty_of_false (λ a, id)
instance sigma.is_empty_left {α} [is_empty α] {E : α → Type*} : is_empty (sigma E) :=
function.is_empty sigma.fst
/- Test that `pi.is_empty` finds this instance. -/
example [h : nonempty α] [is_empty β] : is_empty (α → β) := by apply_instance
/-- Eliminate out of a type that `is_empty` (without using projection notation). -/
@[elab_as_eliminator]
def is_empty_elim [is_empty α] {p : α → Sort*} (a : α) : p a :=
(is_empty.false a).elim
lemma is_empty_iff : is_empty α ↔ α → false :=
⟨@is_empty.false α, is_empty.mk⟩
namespace is_empty
open function
/-- Eliminate out of a type that `is_empty` (using projection notation). -/
protected def elim (h : is_empty α) {p : α → Sort*} (a : α) : p a :=
is_empty_elim a
/-- Non-dependent version of `is_empty.elim`. Helpful if the elaborator cannot elaborate `h.elim a`
correctly. -/
protected def elim' {β : Sort*} (h : is_empty α) (a : α) : β :=
h.elim a
protected lemma prop_iff {p : Prop} : is_empty p ↔ ¬ p :=
is_empty_iff
variables [is_empty α]
@[simp] lemma forall_iff {p : α → Prop} : (∀ a, p a) ↔ true :=
iff_true_intro is_empty_elim
@[simp] lemma exists_iff {p : α → Prop} : (∃ a, p a) ↔ false :=
iff_false_intro $ λ ⟨x, hx⟩, is_empty.false x
@[priority 100] -- see Note [lower instance priority]
instance : subsingleton α := ⟨is_empty_elim⟩
end is_empty
@[simp] lemma not_nonempty_iff : ¬ nonempty α ↔ is_empty α :=
⟨λ h, ⟨λ x, h ⟨x⟩⟩, λ h1 h2, h2.elim h1.elim⟩
@[simp] lemma not_is_empty_iff : ¬ is_empty α ↔ nonempty α :=
not_iff_comm.mp not_nonempty_iff
@[simp] lemma is_empty_Prop {p : Prop} : is_empty p ↔ ¬p :=
by simp only [← not_nonempty_iff, nonempty_Prop]
@[simp] lemma is_empty_pi {π : α → Sort*} : is_empty (Π a, π a) ↔ ∃ a, is_empty (π a) :=
by simp only [← not_nonempty_iff, classical.nonempty_pi, not_forall]
@[simp] lemma is_empty_sigma {α} {E : α → Type*} :
is_empty (sigma E) ↔ ∀ a, is_empty (E a) :=
by simp only [← not_nonempty_iff, nonempty_sigma, not_exists]
@[simp] lemma is_empty_psigma {α} {E : α → Sort*} :
is_empty (psigma E) ↔ ∀ a, is_empty (E a) :=
by simp only [← not_nonempty_iff, nonempty_psigma, not_exists]
@[simp] lemma is_empty_subtype (p : α → Prop) : is_empty (subtype p) ↔ ∀ x, ¬p x :=
by simp only [← not_nonempty_iff, nonempty_subtype, not_exists]
@[simp] lemma is_empty_prod {α β : Type*} : is_empty (α × β) ↔ is_empty α ∨ is_empty β :=
by simp only [← not_nonempty_iff, nonempty_prod, not_and_distrib]
@[simp] lemma is_empty_pprod : is_empty (pprod α β) ↔ is_empty α ∨ is_empty β :=
by simp only [← not_nonempty_iff, nonempty_pprod, not_and_distrib]
@[simp] lemma is_empty_sum {α β} : is_empty (α ⊕ β) ↔ is_empty α ∧ is_empty β :=
by simp only [← not_nonempty_iff, nonempty_sum, not_or_distrib]
@[simp] lemma is_empty_psum {α β} : is_empty (psum α β) ↔ is_empty α ∧ is_empty β :=
by simp only [← not_nonempty_iff, nonempty_psum, not_or_distrib]
@[simp] lemma is_empty_ulift {α} : is_empty (ulift α) ↔ is_empty α :=
by simp only [← not_nonempty_iff, nonempty_ulift]
@[simp] lemma is_empty_plift {α} : is_empty (plift α) ↔ is_empty α :=
by simp only [← not_nonempty_iff, nonempty_plift]
lemma well_founded_of_empty {α} [is_empty α] (r : α → α → Prop) : well_founded r :=
⟨is_empty_elim⟩
variables (α)
lemma is_empty_or_nonempty : is_empty α ∨ nonempty α :=
(em $ is_empty α).elim or.inl $ or.inr ∘ not_is_empty_iff.mp
@[simp] lemma not_is_empty_of_nonempty [h : nonempty α] : ¬ is_empty α :=
not_is_empty_iff.mpr h
variable {α}
lemma function.extend_of_empty [is_empty α] (f : α → β) (g : α → γ) (h : β → γ) :
function.extend f g h = h :=
funext $ λ x, function.extend_apply' _ _ _ $ λ ⟨a, h⟩, is_empty_elim a
|
99628fe0f6b65665671ac1151ab36abb2815cab0 | c31182a012eec69da0a1f6c05f42b0f0717d212d | /src/pseudo_normed_group/with_Tinv.lean | c78691d5a7e6a99a5be59760b29aacfe5d8417c9 | [] | no_license | Ja1941/lean-liquid | fbec3ffc7fc67df1b5ca95b7ee225685ab9ffbdc | 8e80ed0cbdf5145d6814e833a674eaf05a1495c1 | refs/heads/master | 1,689,437,983,362 | 1,628,362,719,000 | 1,628,362,719,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 12,584 | lean | import pseudo_normed_group.profinitely_filtered
/-!
# Profinitely filtered pseudo-normed groups with T⁻¹
The definition of `profinitely_filtered_pseudo_normed_group_with_Tinv`,
and morphisms between them.
-/
open pseudo_normed_group profinitely_filtered_pseudo_normed_group
open_locale nnreal big_operators
local attribute [instance] type_pow
/-- A *profinitely filtered pseudo-normed topological group with action by `T⁻¹`* is
a profinitely filtered pseudo-normed topological group `M` together with a
nonnegative real `r'` and homomorphism `Tinv : M → M` such that
`Tinv x ∈ filtration M (r'⁻¹ * c)` for all `x ∈ filtration M c`.
Morphisms are continuous and strict homomorphisms. -/
class profinitely_filtered_pseudo_normed_group_with_Tinv (r' : out_param $ ℝ≥0) (M : Type*)
extends profinitely_filtered_pseudo_normed_group M :=
(Tinv : profinitely_filtered_pseudo_normed_group_hom M M)
(Tinv_mem_filtration : ∀ c x, x ∈ filtration c → Tinv x ∈ filtration (r'⁻¹ * c))
namespace profinitely_filtered_pseudo_normed_group_with_Tinv
variables {r' : ℝ≥0} {M M₁ M₂ M₃ : Type*}
variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M]
variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M₁]
variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M₂]
variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M₃]
lemma aux {r' c c₂ : ℝ≥0} (h : c ≤ r' * c₂) : r'⁻¹ * c ≤ c₂ :=
begin
by_cases hr' : r' = 0,
{ subst r', rw [inv_zero, zero_mul], exact zero_le' },
{ rwa [nnreal.mul_le_iff_le_inv, inv_inv'], exact inv_ne_zero hr' }
end
@[simps]
def Tinv₀ (c c₂ : ℝ≥0) [h : fact (c ≤ r' * c₂)] (x : filtration M c) : filtration M c₂ :=
⟨Tinv (x : M), filtration_mono (aux h.1) (Tinv_mem_filtration _ _ x.2)⟩
lemma Tinv₀_continuous (c c₂ : ℝ≥0) [fact (c ≤ r' * c₂)] :
continuous (@Tinv₀ r' M _ c c₂ _) :=
Tinv.continuous _ $ λ x, rfl
lemma Tinv_bound_by : (@Tinv _ M _).bound_by (r'⁻¹) := Tinv_mem_filtration
end profinitely_filtered_pseudo_normed_group_with_Tinv
section
set_option old_structure_cmd true
open profinitely_filtered_pseudo_normed_group_with_Tinv
structure profinitely_filtered_pseudo_normed_group_with_Tinv_hom (r' : ℝ≥0) (M₁ M₂ : Type*)
[profinitely_filtered_pseudo_normed_group_with_Tinv r' M₁]
[profinitely_filtered_pseudo_normed_group_with_Tinv r' M₂]
extends M₁ →+ M₂ :=
(strict' : ∀ ⦃c x⦄, x ∈ filtration M₁ c → to_fun x ∈ filtration M₂ c)
(continuous' : ∀ c, continuous (pseudo_normed_group.level to_fun strict' c))
(map_Tinv' : ∀ x, to_fun (Tinv x) = Tinv (to_fun x))
end
attribute [nolint doc_blame] profinitely_filtered_pseudo_normed_group_with_Tinv_hom.mk
profinitely_filtered_pseudo_normed_group_with_Tinv_hom.to_add_monoid_hom
namespace profinitely_filtered_pseudo_normed_group_with_Tinv_hom
open profinitely_filtered_pseudo_normed_group_with_Tinv
variables {r' : ℝ≥0} {M M₁ M₂ M₃ : Type*}
variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M]
variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M₁]
variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M₂]
variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M₃]
variables (f g : profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₁ M₂)
instance : has_coe_to_fun (profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₁ M₂) :=
⟨_, profinitely_filtered_pseudo_normed_group_with_Tinv_hom.to_fun⟩
@[simp] lemma coe_mk (f) (h₁) (h₂) (h₃) (h₄) (h₅) :
⇑(⟨f, h₁, h₂, h₃, h₄, h₅⟩ : profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₁ M₂) = f :=
rfl
@[simp] lemma mk_to_monoid_hom (f) (h₁) (h₂) (h₃) (h₄) (h₅) :
(⟨f, h₁, h₂, h₃, h₄, h₅⟩ :
profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₁ M₂).to_add_monoid_hom =
⟨f, h₁, h₂⟩ := rfl
@[simp] lemma coe_to_add_monoid_hom : ⇑f.to_add_monoid_hom = f := rfl
@[simp] lemma map_zero : f 0 = 0 := f.to_add_monoid_hom.map_zero
@[simp] lemma map_add (x y) : f (x + y) = f x + f y := f.to_add_monoid_hom.map_add _ _
@[simp] lemma map_sum {ι : Type*} (x : ι → M₁) (s : finset ι) :
f (∑ i in s, x i) = ∑ i in s, f (x i) :=
f.to_add_monoid_hom.map_sum _ _
@[simp] lemma map_sub (x y) : f (x - y) = f x - f y := f.to_add_monoid_hom.map_sub _ _
@[simp] lemma map_neg (x) : f (-x) = -(f x) := f.to_add_monoid_hom.map_neg _
@[simp] lemma map_gsmul (n : ℤ) (x) : f (n • x) = n • (f x) := f.to_add_monoid_hom.map_gsmul _ _
lemma strict : ∀ ⦃c x⦄, x ∈ filtration M₁ c → f x ∈ filtration M₂ c := f.strict'
/-- `f.level c` is the function `filtration M₁ c → filtration M₂ c`
induced by a `profinitely_filtered_pseudo_normed_group_with_Tinv_hom M₁ M₂`. -/
@[simps] def level : ∀ (c : ℝ≥0), filtration M₁ c → filtration M₂ c :=
pseudo_normed_group.level f f.strict
lemma level_continuous (c : ℝ≥0) : continuous (f.level c) := f.continuous' c
lemma map_Tinv (x : M₁) : f (Tinv x) = Tinv (f x) := f.map_Tinv' x
variables {f g}
@[ext] theorem ext (H : ∀ x, f x = g x) : f = g :=
by cases f; cases g; congr'; exact funext H
instance : has_zero (profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₁ M₂) :=
⟨{ strict' := λ c x h, zero_mem_filtration _,
continuous' := λ c, @continuous_const _ (filtration M₂ c) _ _ 0,
map_Tinv' := λ x, show 0 = Tinv (0 : M₂), from Tinv.map_zero.symm,
.. (0 : M₁ →+ M₂) }⟩
instance : inhabited (profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₁ M₂) := ⟨0⟩
lemma coe_inj ⦃f g : profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₁ M₂⦄
(h : (f : M₁ → M₂) = g) :
f = g :=
by cases f; cases g; cases h; refl
/-- The identity function as `profinitely_filtered_pseudo_normed_group_with_Tinv_hom`. -/
@[simps] def id : profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M M :=
{ strict' := λ c x, id,
continuous' := λ c, by { convert continuous_id, ext, refl },
map_Tinv' := λ x, rfl,
.. add_monoid_hom.id _ }
/-- The composition of `profinitely_filtered_pseudo_normed_group_with_Tinv_hom`s. -/
@[simps] def comp
(g : profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₂ M₃)
(f : profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₁ M₂) :
profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₁ M₃ :=
{ strict' := λ c x hx, g.strict (f.strict hx),
continuous' := λ c, (g.level_continuous c).comp (f.level_continuous c),
map_Tinv' := λ x,
calc g (f (Tinv x)) = g (Tinv (f x)) : by rw f.map_Tinv
... = Tinv (g (f x)) : by rw g.map_Tinv,
.. (g.to_add_monoid_hom.comp f.to_add_monoid_hom) }
variables (f)
/-- The `profinitely_filtered_pseudo_normed_group_hom` underlying a
`profinitely_filtered_pseudo_normed_group_with_Tinv_hom`. -/
def to_profinitely_filtered_pseudo_normed_group_hom :
profinitely_filtered_pseudo_normed_group_hom M₁ M₂ :=
profinitely_filtered_pseudo_normed_group_hom.mk_of_strict f.to_add_monoid_hom
(λ c, ⟨λ x h, f.strict h, f.level_continuous c⟩)
lemma to_profinitely_filtered_pseudo_normed_group_hom_strict :
f.to_profinitely_filtered_pseudo_normed_group_hom.strict :=
profinitely_filtered_pseudo_normed_group_hom.mk_of_strict_strict _ _
variables {f}
def mk' (f : profinitely_filtered_pseudo_normed_group_hom M₁ M₂)
(hf1 : f.bound_by 1) (hfT) :
profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₁ M₂ :=
{ to_fun := f,
strict' := λ c x hx, by simpa only [one_mul] using hf1 hx,
continuous' := λ c, f.continuous _ (λ x, rfl),
map_Tinv' := hfT,
.. f }
@[simp] lemma mk'_apply
(f : profinitely_filtered_pseudo_normed_group_hom M₁ M₂) (hf1) (hfT) (x : M₁) :
@mk' r' _ _ _ _ f hf1 hfT x = f x := rfl
/-- If the inverse of `profinitely_filtered_pseudo_normed_group_with_Tinv_hom` is strict, then it
is a `profinitely_filtered_pseudo_normed_group_with_Tinv_hom`. -/
def inv_of_equiv_of_strict (e : M₁ ≃+ M₂) (he : ∀ x, f x = e x)
(strict : ∀ ⦃c x⦄, x ∈ filtration M₂ c → e.symm x ∈ filtration M₁ c) :
profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₂ M₁ :=
{ strict' := strict,
continuous' := λ c,
begin
simp only [add_equiv.coe_to_add_monoid_hom, add_monoid_hom.to_fun_eq_coe],
have hcont := f.continuous' c,
let g : (filtration M₁ c) ≃ (filtration M₂ c) :=
⟨λ x, ⟨f x, f.strict x.2⟩, λ x, ⟨e.symm x, strict x.2⟩, λ x, by simp [he], λ x, by simp [he]⟩,
change continuous g.symm,
rw continuous_iff_is_closed,
intros U hU,
rw [← g.image_eq_preimage],
exact (hcont.closed_embedding g.injective).is_closed_map U hU,
end,
map_Tinv' := λ x,
begin
apply e.injective,
simp only [add_equiv.coe_to_add_monoid_hom, add_monoid_hom.to_fun_eq_coe],
rw [e.apply_symm_apply, ← he, map_Tinv, he, e.apply_symm_apply],
end,
.. e.symm.to_add_monoid_hom }
@[simp]
lemma inv_of_equiv_of_strict.apply (x : M₁) (e : M₁ ≃+ M₂) (he : ∀ x, f x = e x)
(strict : ∀ ⦃c x⦄, x ∈ filtration M₂ c → e.symm x ∈ filtration M₁ c) :
(inv_of_equiv_of_strict e he strict) (f x) = x := by simp [inv_of_equiv_of_strict, he]
@[simp]
lemma inv_of_equiv_of_strict_symm.apply (x : M₂) (e : M₁ ≃+ M₂) (he : ∀ x, f x = e x)
(strict : ∀ ⦃c x⦄, x ∈ filtration M₂ c → e.symm x ∈ filtration M₁ c) :
f (inv_of_equiv_of_strict e he strict x) = x := by simp [inv_of_equiv_of_strict, he]
end profinitely_filtered_pseudo_normed_group_with_Tinv_hom
namespace punit
instance profinitely_filtered_pseudo_normed_group_with_Tinv (r' : ℝ≥0) :
profinitely_filtered_pseudo_normed_group_with_Tinv r' punit :=
{ Tinv := profinitely_filtered_pseudo_normed_group_hom.id,
Tinv_mem_filtration := λ c x h, set.mem_univ _,
.. punit.profinitely_filtered_pseudo_normed_group }
end punit
namespace profinitely_filtered_pseudo_normed_group_with_Tinv
section
/-! ## Powers -/
noncomputable theory
variables (r' : ℝ≥0) {ι : Type*} (M M₁ M₂ : ι → Type*)
variables [Π i, profinitely_filtered_pseudo_normed_group_with_Tinv r' (M i)]
variables [Π i, profinitely_filtered_pseudo_normed_group_with_Tinv r' (M₁ i)]
variables [Π i, profinitely_filtered_pseudo_normed_group_with_Tinv r' (M₂ i)]
instance pi : profinitely_filtered_pseudo_normed_group_with_Tinv r' (Π i, M i) :=
{ Tinv := profinitely_filtered_pseudo_normed_group.pi_map (λ i, Tinv)
⟨r'⁻¹, λ i, Tinv_bound_by⟩,
Tinv_mem_filtration := λ c x hx i, Tinv_mem_filtration _ _ (hx i),
.. profinitely_filtered_pseudo_normed_group.pi _ }
instance pi' (M : Type*) [profinitely_filtered_pseudo_normed_group_with_Tinv r' M] (N : ℕ) :
profinitely_filtered_pseudo_normed_group_with_Tinv r' (M^N) :=
profinitely_filtered_pseudo_normed_group_with_Tinv.pi r' (λ i, M)
include r'
@[simp] lemma pi_Tinv_apply (x : Π i, M i) (i : ι) : Tinv x i = Tinv (x i) := rfl
omit r'
@[simps {fully_applied := ff}]
def pi_proj (i : ι) : profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' (Π i, M i) (M i) :=
{ to_fun := pi.eval_add_monoid_hom M i,
strict' := λ c x hx, hx i,
continuous' := λ c, (continuous_apply i).comp (filtration_pi_homeo M c).continuous,
map_Tinv' := λ x, rfl,
.. pi.eval_add_monoid_hom M i }
/-- Universal property of the product of
profinitely filtered pseudo-normed groups with `T⁻¹`-action -/
@[simps {fully_applied := ff}]
def pi_lift {N : Type*} [profinitely_filtered_pseudo_normed_group_with_Tinv r' N]
(f : Π i, profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' N (M i)) :
profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' N (Π i, M i) :=
{ to_fun := add_monoid_hom.mk_to_pi (λ i, (f i).to_add_monoid_hom),
strict' := λ c x hx i, (f i).strict hx,
continuous' :=
begin
intros c,
apply continuous_induced_rng,
apply continuous_pi,
intro i,
exact (f i).continuous' c,
end,
map_Tinv' := λ x, by { ext i, exact (f i).map_Tinv x },
.. add_monoid_hom.mk_to_pi (λ i, (f i).to_add_monoid_hom) }
@[simps {fully_applied := ff}]
def pi_map (f : Π i, profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' (M₁ i) (M₂ i)) :
profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' (Π i, M₁ i) (Π i, M₂ i) :=
pi_lift r' _ $ λ i, (f i).comp (pi_proj r' _ i)
end
end profinitely_filtered_pseudo_normed_group_with_Tinv
|
1656089b7279ee3e3ef3a13d3f31fd772170f589 | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/algebra/group/conj.lean | 977d04f0a68cb7b6db6bc4044adaaf69ae56436d | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,536 | lean | /-
Copyright (c) 2018 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Chris Hughes, Michael Howes
-/
import data.fintype.basic
import algebra.group.hom
import algebra.group.semiconj
import data.equiv.mul_add_aut
import algebra.group_with_zero.basic
/-!
# Conjugacy of group elements
See also `mul_aut.conj` and `quandle.conj`.
-/
universes u v
variables {α : Type u} {β : Type v}
section monoid
variables [monoid α] [monoid β]
/-- We say that `a` is conjugate to `b` if for some unit `c` we have `c * a * c⁻¹ = b`. -/
def is_conj (a b : α) := ∃ c : units α, semiconj_by ↑c a b
@[refl] lemma is_conj.refl (a : α) : is_conj a a :=
⟨1, semiconj_by.one_left a⟩
@[symm] lemma is_conj.symm {a b : α} : is_conj a b → is_conj b a
| ⟨c, hc⟩ := ⟨c⁻¹, hc.units_inv_symm_left⟩
@[trans] lemma is_conj.trans {a b c : α} : is_conj a b → is_conj b c → is_conj a c
| ⟨c₁, hc₁⟩ ⟨c₂, hc₂⟩ := ⟨c₂ * c₁, hc₂.mul_left hc₁⟩
@[simp] lemma is_conj_iff_eq {α : Type*} [comm_monoid α] {a b : α} : is_conj a b ↔ a = b :=
⟨λ ⟨c, hc⟩, begin
rw [semiconj_by, mul_comm, ← units.mul_inv_eq_iff_eq_mul, mul_assoc, c.mul_inv, mul_one] at hc,
exact hc,
end, λ h, by rw h⟩
protected lemma monoid_hom.map_is_conj (f : α →* β) {a b : α} : is_conj a b → is_conj (f a) (f b)
| ⟨c, hc⟩ := ⟨units.map f c, by rw [units.coe_map, semiconj_by, ← f.map_mul, hc.eq, f.map_mul]⟩
end monoid
section group
variables [group α]
@[simp] lemma is_conj_iff {a b : α} :
is_conj a b ↔ ∃ c : α, c * a * c⁻¹ = b :=
⟨λ ⟨c, hc⟩, ⟨c, mul_inv_eq_iff_eq_mul.2 hc⟩, λ ⟨c, hc⟩,
⟨⟨c, c⁻¹, mul_inv_self c, inv_mul_self c⟩, mul_inv_eq_iff_eq_mul.1 hc⟩⟩
@[simp] lemma is_conj_one_right {a : α} : is_conj 1 a ↔ a = 1 :=
⟨λ ⟨c, hc⟩, mul_right_cancel (hc.symm.trans ((mul_one _).trans (one_mul _).symm)), λ h, by rw [h]⟩
@[simp] lemma is_conj_one_left {a : α} : is_conj a 1 ↔ a = 1 :=
calc is_conj a 1 ↔ is_conj 1 a : ⟨is_conj.symm, is_conj.symm⟩
... ↔ a = 1 : is_conj_one_right
@[simp] lemma conj_inv {a b : α} : (b * a * b⁻¹)⁻¹ = b * a⁻¹ * b⁻¹ :=
((mul_aut.conj b).map_inv a).symm
@[simp] lemma conj_mul {a b c : α} : (b * a * b⁻¹) * (b * c * b⁻¹) = b * (a * c) * b⁻¹ :=
((mul_aut.conj b).map_mul a c).symm
@[simp] lemma conj_pow {i : ℕ} {a b : α} : (a * b * a⁻¹) ^ i = a * (b ^ i) * a⁻¹ :=
begin
induction i with i hi,
{ simp },
{ simp [pow_succ, hi] }
end
@[simp] lemma conj_gpow {i : ℤ} {a b : α} : (a * b * a⁻¹) ^ i = a * (b ^ i) * a⁻¹ :=
begin
induction i,
{ simp },
{ simp [gpow_neg_succ_of_nat, conj_pow] }
end
lemma conj_injective {x : α} : function.injective (λ (g : α), x * g * x⁻¹) :=
(mul_aut.conj x).injective
end group
@[simp] lemma is_conj_iff₀ [group_with_zero α] {a b : α} :
is_conj a b ↔ ∃ c : α, c ≠ 0 ∧ c * a * c⁻¹ = b :=
⟨λ ⟨c, hc⟩, ⟨c, begin
rw [← units.coe_inv', units.mul_inv_eq_iff_eq_mul],
exact ⟨c.ne_zero, hc⟩,
end⟩, λ ⟨c, c0, hc⟩,
⟨units.mk0 c c0, begin
rw [semiconj_by, ← units.mul_inv_eq_iff_eq_mul, units.coe_inv', units.coe_mk0],
exact hc
end⟩⟩
namespace is_conj
/- This small quotient API is largely copied from the API of `associates`;
where possible, try to keep them in sync -/
/-- The setoid of the relation `is_conj` iff there is a unit `u` such that `u * x = y * u` -/
protected def setoid (α : Type*) [monoid α] : setoid α :=
{ r := is_conj, iseqv := ⟨is_conj.refl, λa b, is_conj.symm, λa b c, is_conj.trans⟩ }
end is_conj
local attribute [instance, priority 100] is_conj.setoid
/-- The quotient type of conjugacy classes of a group. -/
def conj_classes (α : Type*) [monoid α] : Type* :=
quotient (is_conj.setoid α)
namespace conj_classes
section monoid
variables [monoid α] [monoid β]
/-- The canonical quotient map from a monoid `α` into the `conj_classes` of `α` -/
protected def mk {α : Type*} [monoid α] (a : α) : conj_classes α :=
⟦a⟧
instance : inhabited (conj_classes α) := ⟨⟦1⟧⟩
theorem mk_eq_mk_iff_is_conj {a b : α} :
conj_classes.mk a = conj_classes.mk b ↔ is_conj a b :=
iff.intro quotient.exact quot.sound
theorem quotient_mk_eq_mk (a : α) : ⟦ a ⟧ = conj_classes.mk a := rfl
theorem quot_mk_eq_mk (a : α) : quot.mk setoid.r a = conj_classes.mk a := rfl
theorem forall_is_conj {p : conj_classes α → Prop} :
(∀a, p a) ↔ (∀a, p (conj_classes.mk a)) :=
iff.intro
(assume h a, h _)
(assume h a, quotient.induction_on a h)
theorem mk_surjective : function.surjective (@conj_classes.mk α _) :=
forall_is_conj.2 (λ a, ⟨a, rfl⟩)
instance : has_one (conj_classes α) := ⟨⟦ 1 ⟧⟩
theorem one_eq_mk_one : (1 : conj_classes α) = conj_classes.mk 1 := rfl
lemma exists_rep (a : conj_classes α) : ∃ a0 : α, conj_classes.mk a0 = a :=
quot.exists_rep a
/-- A `monoid_hom` maps conjugacy classes of one group to conjugacy classes of another. -/
def map (f : α →* β) : conj_classes α → conj_classes β :=
quotient.lift (conj_classes.mk ∘ f) (λ a b ab, mk_eq_mk_iff_is_conj.2 (f.map_is_conj ab))
instance [fintype α] [decidable_rel (is_conj : α → α → Prop)] :
fintype (conj_classes α) :=
quotient.fintype (is_conj.setoid α)
end monoid
section comm_monoid
variable [comm_monoid α]
lemma mk_injective : function.injective (@conj_classes.mk α _) :=
λ _ _, (mk_eq_mk_iff_is_conj.trans is_conj_iff_eq).1
lemma mk_bijective : function.bijective (@conj_classes.mk α _) :=
⟨mk_injective, mk_surjective⟩
/-- The bijection between a `comm_group` and its `conj_classes`. -/
def mk_equiv : α ≃ conj_classes α :=
⟨conj_classes.mk, quotient.lift id (λ (a : α) b, is_conj_iff_eq.1), quotient.lift_mk _ _,
begin
rw [function.right_inverse, function.left_inverse, forall_is_conj],
intro x,
rw [← quotient_mk_eq_mk, ← quotient_mk_eq_mk, quotient.lift_mk, id.def],
end⟩
end comm_monoid
end conj_classes
section monoid
variables [monoid α]
/-- Given an element `a`, `conjugates a` is the set of conjugates. -/
def conjugates_of (a : α) : set α := {b | is_conj a b}
lemma mem_conjugates_of_self {a : α} : a ∈ conjugates_of a := is_conj.refl _
lemma is_conj.conjugates_of_eq {a b : α} (ab : is_conj a b) :
conjugates_of a = conjugates_of b :=
set.ext (λ g, ⟨λ ag, (ab.symm).trans ag, λ bg, ab.trans bg⟩)
lemma is_conj_iff_conjugates_of_eq {a b : α} :
is_conj a b ↔ conjugates_of a = conjugates_of b :=
⟨is_conj.conjugates_of_eq, λ h, begin
have ha := mem_conjugates_of_self,
rwa ← h at ha,
end⟩
end monoid
namespace conj_classes
variables [monoid α]
local attribute [instance] is_conj.setoid
/-- Given a conjugacy class `a`, `carrier a` is the set it represents. -/
def carrier : conj_classes α → set α :=
quotient.lift conjugates_of (λ (a : α) b ab, is_conj.conjugates_of_eq ab)
lemma mem_carrier_mk {a : α} : a ∈ carrier (conj_classes.mk a) := is_conj.refl _
lemma mem_carrier_iff_mk_eq {a : α} {b : conj_classes α} :
a ∈ carrier b ↔ conj_classes.mk a = b :=
begin
revert b,
rw forall_is_conj,
intro b,
rw [carrier, eq_comm, mk_eq_mk_iff_is_conj, ← quotient_mk_eq_mk, quotient.lift_mk],
refl,
end
lemma carrier_eq_preimage_mk {a : conj_classes α} :
a.carrier = conj_classes.mk ⁻¹' {a} :=
set.ext (λ x, mem_carrier_iff_mk_eq)
end conj_classes
|
a143375bf95a34192457888c10f92def9a2c8220 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/tac1.lean | 38607dee4e94961ee8e8feb2364a2ce8438f8986 | [
"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 | 197 | lean | import logic
open tactic
notation `(` h `|` r:(foldl `|` (e r, tactic.or_else r e) h) `)` := r
infixl `;`:15 := tactic.and_then
definition mytac := apply @and.intro; apply @eq.refl
check @mytac
|
0a0072f72ec1d2e538a4958694ce207f165a4742 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/struct_auto_at_simp.lean | 490368da462a8ab4f0e1fc8a4d7edfb9c30d62c4 | [
"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 | 318 | lean | structure S (f : nat → nat) :=
(ax : ∀ x, f x = x . tactic.assumption)
example (g : nat → nat) (s : S g) (x : nat) : g (g x) = x :=
by simp [s.ax]
constant h : nat → nat
axiom h_ax (x : nat) (p : x > 0 . tactic.comp_val) : h x = x
example (a : nat) : a > 0 → h a = a :=
begin
intros, simp [h_ax, *]
end
|
b95b1668e9aedfa9cf107c00715b8b94a9b281c6 | 626e312b5c1cb2d88fca108f5933076012633192 | /src/topology/algebra/ordered/basic.lean | fc050578a2efa993a012d039ae33dc5771a75018 | [
"Apache-2.0"
] | permissive | Bioye97/mathlib | 9db2f9ee54418d29dd06996279ba9dc874fd6beb | 782a20a27ee83b523f801ff34efb1a9557085019 | refs/heads/master | 1,690,305,956,488 | 1,631,067,774,000 | 1,631,067,774,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 188,746 | 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 algebra.group_with_zero.power
import data.set.intervals.pi
import order.filter.interval
import topology.algebra.group
import tactic.linarith
import tactic.tfae
/-!
# 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_preconnected_I??` : all intervals `I??` are preconnected,
* `is_preconnected.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
* `is_compact.exists_forall_le`, `is_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 function
open_locale topological_space classical filter
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
instance [topological_space α] [h : first_countable_topology α] :
first_countable_topology (order_dual α) := h
@[to_additive]
instance [topological_space α] [has_mul α] [h : has_continuous_mul α] :
has_continuous_mul (order_dual α) := h
section order_closed_topology
section preorder
variables [topological_space α] [preorder α] [t : order_closed_topology α]
include t
namespace subtype
instance {p : α → Prop} : order_closed_topology (subtype p) :=
have this : continuous (λ (p : (subtype p) × (subtype p)), ((p.fst : α), (p.snd : α))) :=
(continuous_subtype_coe.comp continuous_fst).prod_mk
(continuous_subtype_coe.comp continuous_snd),
order_closed_topology.mk (t.is_closed_le'.preimage this)
end subtype
lemma is_closed_le_prod : is_closed {p : α × α | p.1 ≤ p.2} :=
t.is_closed_le'
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) _ is_closed_le_prod
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 α) :=
⟨(@order_closed_topology.is_closed_le' α _ _ _).preimage continuous_swap⟩
lemma is_closed_Icc {a b : α} : is_closed (Icc a b) :=
is_closed.inter is_closed_Ici is_closed_Iic
@[simp] lemma closure_Icc (a b : α) : closure (Icc a b) = Icc a b :=
is_closed_Icc.closure_eq
@[simp] lemma closure_Iic (a : α) : closure (Iic a) = Iic a :=
is_closed_Iic.closure_eq
@[simp] lemma closure_Ici (a : α) : closure (Ici a) = Ici a :=
is_closed_Ici.closure_eq
lemma le_of_tendsto_of_tendsto {f g : β → α} {b : filter β} {a₁ a₂ : α} [ne_bot b]
(hf : tendsto f b (𝓝 a₁)) (hg : tendsto g b (𝓝 a₂)) (h : f ≤ᶠ[b] g) :
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 t.is_closed_le'.mem_of_tendsto this h
lemma le_of_tendsto_of_tendsto' {f g : β → α} {b : filter β} {a₁ a₂ : α} [ne_bot b]
(hf : tendsto f b (𝓝 a₁)) (hg : tendsto g b (𝓝 a₂)) (h : ∀ x, f x ≤ g x) :
a₁ ≤ a₂ :=
le_of_tendsto_of_tendsto hf hg (eventually_of_forall h)
lemma le_of_tendsto {f : β → α} {a b : α} {x : filter β}
[ne_bot x] (lim : tendsto f x (𝓝 a)) (h : ∀ᶠ c in x, f c ≤ b) : a ≤ b :=
le_of_tendsto_of_tendsto lim tendsto_const_nhds h
lemma le_of_tendsto' {f : β → α} {a b : α} {x : filter β}
[ne_bot x] (lim : tendsto f x (𝓝 a)) (h : ∀ c, f c ≤ b) : a ≤ b :=
le_of_tendsto lim (eventually_of_forall h)
lemma ge_of_tendsto {f : β → α} {a b : α} {x : filter β} [ne_bot x]
(lim : tendsto f x (𝓝 a)) (h : ∀ᶠ c in x, b ≤ f c) : b ≤ a :=
le_of_tendsto_of_tendsto tendsto_const_nhds lim h
lemma ge_of_tendsto' {f : β → α} {a b : α} {x : filter β} [ne_bot x]
(lim : tendsto f x (𝓝 a)) (h : ∀ c, b ≤ f c) : b ≤ a :=
ge_of_tendsto 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} :=
(is_closed_le hf hg).closure_eq
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 :=
show (f x, g x) ∈ {p : α × α | p.1 ≤ p.2},
from order_closed_topology.is_closed_le'.closure_subset ((hf.prod hg).mem_closure hx h)
/-- If `s` is a closed set and two functions `f` and `g` are continuous on `s`,
then the set `{x ∈ s | f x ≤ g x}` is a closed set. -/
lemma is_closed.is_closed_le [topological_space β] {f g : β → α} {s : set β} (hs : is_closed s)
(hf : continuous_on f s) (hg : continuous_on g s) :
is_closed {x ∈ s | f x ≤ g x} :=
(hf.prod hg).preimage_closed_of_closed hs order_closed_topology.is_closed_le'
omit t
lemma nhds_within_Ici_ne_bot {a b : α} (H₂ : a ≤ b) :
ne_bot (𝓝[Ici a] b) :=
nhds_within_ne_bot_of_mem H₂
@[instance] lemma nhds_within_Ici_self_ne_bot (a : α) :
ne_bot (𝓝[Ici a] a) :=
nhds_within_Ici_ne_bot (le_refl a)
lemma nhds_within_Iic_ne_bot {a b : α} (H : a ≤ b) :
ne_bot (𝓝[Iic b] a) :=
nhds_within_ne_bot_of_mem H
@[instance] lemma nhds_within_Iic_self_ne_bot (a : α) :
ne_bot (𝓝[Iic a] a) :=
nhds_within_Iic_ne_bot (le_refl a)
end preorder
section partial_order
variables [topological_space α] [partial_order α] [t : order_closed_topology α]
include t
private lemma is_closed_eq_aux : is_closed {p : α × α | p.1 = p.2} :=
by simp only [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} := is_closed_eq_aux.is_open_compl,
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_prod : is_open {p : α × α | p.1 < p.2} :=
by { simp_rw [← is_closed_compl_iff, compl_set_of, not_lt],
exact is_closed_le continuous_snd continuous_fst }
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).is_open_compl
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 :=
is_open_Ioi.interior_eq
@[simp] lemma interior_Iio : interior (Iio a) = Iio a :=
is_open_Iio.interior_eq
@[simp] lemma interior_Ioo : interior (Ioo a b) = Ioo a b :=
is_open_Ioo.interior_eq
lemma eventually_le_of_tendsto_lt {l : filter γ} {f : γ → α} {u v : α} (hv : v < u)
(h : tendsto f l (𝓝 v)) : ∀ᶠ a in l, f a ≤ u :=
eventually.mono (tendsto_nhds.1 h (< u) is_open_Iio hv) (λ v, le_of_lt)
lemma eventually_ge_of_tendsto_gt {l : filter γ} {f : γ → α} {u v : α} (hv : u < v)
(h : tendsto f l (𝓝 v)) : ∀ᶠ a in l, u ≤ f a :=
eventually.mono (tendsto_nhds.1 h (> u) is_open_Ioi hv) (λ v, le_of_lt)
variables [topological_space γ]
/-- Intermediate value theorem for two functions: if `f` and `g` are two continuous functions
on a preconnected space and `f a ≤ g a` and `g b ≤ f b`, then for some `x` we have `f x = g x`. -/
lemma intermediate_value_univ₂ [preconnected_space γ] {a b : γ} {f g : γ → α} (hf : continuous f)
(hg : continuous g) (ha : f a ≤ g a) (hb : g b ≤ f b) :
∃ x, f x = g x :=
begin
obtain ⟨x, h, hfg, hgf⟩ : (univ ∩ {x | f x ≤ g x ∧ g x ≤ f x}).nonempty,
from is_preconnected_closed_iff.1 preconnected_space.is_preconnected_univ _ _
(is_closed_le hf hg) (is_closed_le hg hf) (λ x hx, le_total _ _) ⟨a, trivial, ha⟩
⟨b, trivial, hb⟩,
exact ⟨x, le_antisymm hfg hgf⟩
end
lemma intermediate_value_univ₂_eventually₁ [preconnected_space γ] {a : γ} {l : filter γ} [ne_bot l]
{f g : γ → α} (hf : continuous f) (hg : continuous g) (ha : f a ≤ g a) (he : g ≤ᶠ[l] f) :
∃ x, f x = g x :=
let ⟨c, hc⟩ := he.frequently.exists in intermediate_value_univ₂ hf hg ha hc
lemma intermediate_value_univ₂_eventually₂ [preconnected_space γ] {l₁ l₂ : filter γ}
[ne_bot l₁] [ne_bot l₂] {f g : γ → α} (hf : continuous f) (hg : continuous g)
(he₁ : f ≤ᶠ[l₁] g ) (he₂ : g ≤ᶠ[l₂] f) :
∃ x, f x = g x :=
let ⟨c₁, hc₁⟩ := he₁.frequently.exists, ⟨c₂, hc₂⟩ := he₂.frequently.exists in
intermediate_value_univ₂ hf hg hc₁ hc₂
/-- Intermediate value theorem for two functions: if `f` and `g` are two functions continuous
on a preconnected set `s` and for some `a b ∈ s` we have `f a ≤ g a` and `g b ≤ f b`,
then for some `x ∈ s` we have `f x = g x`. -/
lemma is_preconnected.intermediate_value₂ {s : set γ} (hs : is_preconnected s)
{a b : γ} (ha : a ∈ s) (hb : b ∈ s) {f g : γ → α}
(hf : continuous_on f s) (hg : continuous_on g s) (ha' : f a ≤ g a) (hb' : g b ≤ f b) :
∃ x ∈ s, f x = g x :=
let ⟨x, hx⟩ := @intermediate_value_univ₂ α s _ _ _ _ (subtype.preconnected_space hs) ⟨a, ha⟩ ⟨b, hb⟩
_ _ (continuous_on_iff_continuous_restrict.1 hf) (continuous_on_iff_continuous_restrict.1 hg)
ha' hb'
in ⟨x, x.2, hx⟩
lemma is_preconnected.intermediate_value₂_eventually₁ {s : set γ} (hs : is_preconnected s)
{a : γ} {l : filter γ} (ha : a ∈ s) [ne_bot l] (hl : l ≤ 𝓟 s) {f g : γ → α}
(hf : continuous_on f s) (hg : continuous_on g s) (ha' : f a ≤ g a) (he : g ≤ᶠ[l] f) :
∃ x ∈ s, f x = g x :=
begin
rw continuous_on_iff_continuous_restrict at hf hg,
obtain ⟨b, h⟩ := @intermediate_value_univ₂_eventually₁ _ _ _ _ _ _ (subtype.preconnected_space hs)
⟨a, ha⟩ _ (comap_coe_ne_bot_of_le_principal hl) _ _ hf hg ha' (eventually_comap' he),
exact ⟨b, b.prop, h⟩,
end
lemma is_preconnected.intermediate_value₂_eventually₂ {s : set γ} (hs : is_preconnected s)
{l₁ l₂ : filter γ} [ne_bot l₁] [ne_bot l₂] (hl₁ : l₁ ≤ 𝓟 s) (hl₂ : l₂ ≤ 𝓟 s) {f g : γ → α}
(hf : continuous_on f s) (hg : continuous_on g s) (he₁ : f ≤ᶠ[l₁] g) (he₂ : g ≤ᶠ[l₂] f) :
∃ x ∈ s, f x = g x :=
begin
rw continuous_on_iff_continuous_restrict at hf hg,
obtain ⟨b, h⟩ := @intermediate_value_univ₂_eventually₂ _ _ _ _ _ _ (subtype.preconnected_space hs)
_ _ (comap_coe_ne_bot_of_le_principal hl₁) (comap_coe_ne_bot_of_le_principal hl₂)
_ _ hf hg (eventually_comap' he₁) (eventually_comap' he₂),
exact ⟨b, b.prop, h⟩,
end
/-- **Intermediate Value Theorem** for continuous functions on connected sets. -/
lemma is_preconnected.intermediate_value {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 :=
λ x hx, mem_image_iff_bex.2 $ hs.intermediate_value₂ ha hb hf continuous_on_const hx.1 hx.2
lemma is_preconnected.intermediate_value_Ico {s : set γ} (hs : is_preconnected s)
{a : γ} {l : filter γ} (ha : a ∈ s) [ne_bot l] (hl : l ≤ 𝓟 s) {f : γ → α}
(hf : continuous_on f s) {v : α} (ht : tendsto f l (𝓝 v)) :
Ico (f a) v ⊆ f '' s :=
λ y h, bex_def.1 $ hs.intermediate_value₂_eventually₁ ha hl
hf continuous_on_const h.1 (eventually_ge_of_tendsto_gt h.2 ht)
lemma is_preconnected.intermediate_value_Ioc {s : set γ} (hs : is_preconnected s)
{a : γ} {l : filter γ} (ha : a ∈ s) [ne_bot l] (hl : l ≤ 𝓟 s) {f : γ → α}
(hf : continuous_on f s) {v : α} (ht : tendsto f l (𝓝 v)) :
Ioc v (f a) ⊆ f '' s :=
λ y h, bex_def.1 $ bex.imp_right (λ x _, eq.symm) $ hs.intermediate_value₂_eventually₁ ha hl
continuous_on_const hf h.2 (eventually_le_of_tendsto_lt h.1 ht)
lemma is_preconnected.intermediate_value_Ioo {s : set γ} (hs : is_preconnected s)
{l₁ l₂ : filter γ} [ne_bot l₁] [ne_bot l₂] (hl₁ : l₁ ≤ 𝓟 s) (hl₂ : l₂ ≤ 𝓟 s) {f : γ → α}
(hf : continuous_on f s) {v₁ v₂ : α} (ht₁ : tendsto f l₁ (𝓝 v₁)) (ht₂ : tendsto f l₂ (𝓝 v₂)) :
Ioo v₁ v₂ ⊆ f '' s :=
λ y h, bex_def.1 $ hs.intermediate_value₂_eventually₂ hl₁ hl₂
hf continuous_on_const (eventually_le_of_tendsto_lt h.1 ht₁) (eventually_ge_of_tendsto_gt h.2 ht₂)
lemma is_preconnected.intermediate_value_Ici {s : set γ} (hs : is_preconnected s)
{a : γ} {l : filter γ} (ha : a ∈ s) [ne_bot l] (hl : l ≤ 𝓟 s) {f : γ → α}
(hf : continuous_on f s) (ht : tendsto f l at_top) :
Ici (f a) ⊆ f '' s :=
λ y h, bex_def.1 $ hs.intermediate_value₂_eventually₁ ha hl
hf continuous_on_const h (tendsto_at_top.1 ht y)
lemma is_preconnected.intermediate_value_Iic {s : set γ} (hs : is_preconnected s)
{a : γ} {l : filter γ} (ha : a ∈ s) [ne_bot l] (hl : l ≤ 𝓟 s) {f : γ → α}
(hf : continuous_on f s) (ht : tendsto f l at_bot) :
Iic (f a) ⊆ f '' s :=
λ y h, bex_def.1 $ bex.imp_right (λ x _, eq.symm) $ hs.intermediate_value₂_eventually₁ ha hl
continuous_on_const hf h (tendsto_at_bot.1 ht y)
lemma is_preconnected.intermediate_value_Ioi {s : set γ} (hs : is_preconnected s)
{l₁ l₂ : filter γ} [ne_bot l₁] [ne_bot l₂] (hl₁ : l₁ ≤ 𝓟 s) (hl₂ : l₂ ≤ 𝓟 s) {f : γ → α}
(hf : continuous_on f s) {v : α} (ht₁ : tendsto f l₁ (𝓝 v)) (ht₂ : tendsto f l₂ at_top) :
Ioi v ⊆ f '' s :=
λ y h, bex_def.1 $ hs.intermediate_value₂_eventually₂ hl₁ hl₂
hf continuous_on_const (eventually_le_of_tendsto_lt h ht₁) (tendsto_at_top.1 ht₂ y)
lemma is_preconnected.intermediate_value_Iio {s : set γ} (hs : is_preconnected s)
{l₁ l₂ : filter γ} [ne_bot l₁] [ne_bot l₂] (hl₁ : l₁ ≤ 𝓟 s) (hl₂ : l₂ ≤ 𝓟 s) {f : γ → α}
(hf : continuous_on f s) {v : α} (ht₁ : tendsto f l₁ at_bot) (ht₂ : tendsto f l₂ (𝓝 v)) :
Iio v ⊆ f '' s :=
λ y h, bex_def.1 $ hs.intermediate_value₂_eventually₂ hl₁ hl₂
hf continuous_on_const (tendsto_at_bot.1 ht₁ y) (eventually_ge_of_tendsto_gt h ht₂)
lemma is_preconnected.intermediate_value_Iii {s : set γ} (hs : is_preconnected s)
{l₁ l₂ : filter γ} [ne_bot l₁] [ne_bot l₂] (hl₁ : l₁ ≤ 𝓟 s) (hl₂ : l₂ ≤ 𝓟 s) {f : γ → α}
(hf : continuous_on f s) (ht₁ : tendsto f l₁ at_bot) (ht₂ : tendsto f l₂ at_top) :
univ ⊆ f '' s :=
λ y h, bex_def.1 $ hs.intermediate_value₂_eventually₂ hl₁ hl₂
hf continuous_on_const (tendsto_at_bot.1 ht₁ y) (tendsto_at_top.1 ht₂ y)
/-- **Intermediate Value Theorem** for continuous functions on connected spaces. -/
lemma intermediate_value_univ [preconnected_space γ] (a b : γ) {f : γ → α} (hf : continuous f) :
Icc (f a) (f b) ⊆ range f :=
λ x hx, intermediate_value_univ₂ hf continuous_const hx.1 hx.2
/-- **Intermediate Value Theorem** for continuous functions on connected spaces. -/
lemma mem_range_of_exists_le_of_exists_ge [preconnected_space γ] {c : α} {f : γ → α}
(hf : continuous f) (h₁ : ∃ a, f a ≤ c) (h₂ : ∃ b, c ≤ f b) :
c ∈ range f :=
let ⟨a, ha⟩ := h₁, ⟨b, hb⟩ := h₂ in intermediate_value_univ a b hf ⟨ha, hb⟩
/-- If a preconnected set contains endpoints of an interval, then it includes the whole interval. -/
lemma is_preconnected.Icc_subset {s : set α} (hs : is_preconnected s)
{a b : α} (ha : a ∈ s) (hb : b ∈ s) :
Icc a b ⊆ s :=
by simpa only [image_id] using hs.intermediate_value ha hb continuous_on_id
/-- If a preconnected set contains endpoints of an interval, then it includes the whole interval. -/
lemma is_connected.Icc_subset {s : set α} (hs : is_connected s)
{a b : α} (ha : a ∈ s) (hb : b ∈ s) :
Icc a b ⊆ s :=
hs.2.Icc_subset ha hb
/-- If preconnected set in a linear order space is unbounded below and above, then it is the whole
space. -/
lemma is_preconnected.eq_univ_of_unbounded {s : set α} (hs : is_preconnected s) (hb : ¬bdd_below s)
(ha : ¬bdd_above s) :
s = univ :=
begin
refine eq_univ_of_forall (λ x, _),
obtain ⟨y, ys, hy⟩ : ∃ y ∈ s, y < x := not_bdd_below_iff.1 hb x,
obtain ⟨z, zs, hz⟩ : ∃ z ∈ s, x < z := not_bdd_above_iff.1 ha x,
exact hs.Icc_subset ys zs ⟨le_of_lt hy, le_of_lt hz⟩
end
/-!
### Neighborhoods to the left and to the right on an `order_closed_topology`
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 `𝓝[Ioi a] a` and
`𝓝[Ici a] a` on the right, and similarly on the left. Here we simply prove that all
right-neighborhoods of a point are equal, and we'll prove later other useful characterizations which
require the stronger hypothesis `order_topology α` -/
/-!
#### Right neighborhoods, point excluded
-/
lemma Ioo_mem_nhds_within_Ioi {a b c : α} (H : b ∈ Ico a c) :
Ioo a c ∈ 𝓝[Ioi b] b :=
mem_nhds_within.2 ⟨Iio c, is_open_Iio, H.2,
by rw [inter_comm, Ioi_inter_Iio]; exact Ioo_subset_Ioo_left H.1⟩
lemma Ioc_mem_nhds_within_Ioi {a b c : α} (H : b ∈ Ico a c) :
Ioc a c ∈ 𝓝[Ioi b] b :=
mem_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 ∈ 𝓝[Ioi b] b :=
mem_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 ∈ 𝓝[Ioi b] b :=
mem_of_superset (Ioo_mem_nhds_within_Ioi H) Ioo_subset_Icc_self
@[simp] lemma nhds_within_Ioc_eq_nhds_within_Ioi {a b : α} (h : a < b) :
𝓝[Ioc a b] a = 𝓝[Ioi a] a :=
le_antisymm (nhds_within_mono _ Ioc_subset_Ioi_self) $
nhds_within_le_of_mem $ Ioc_mem_nhds_within_Ioi $ left_mem_Ico.2 h
@[simp] lemma nhds_within_Ioo_eq_nhds_within_Ioi {a b : α} (h : a < b) :
𝓝[Ioo a b] a = 𝓝[Ioi a] a :=
le_antisymm (nhds_within_mono _ Ioo_subset_Ioi_self) $
nhds_within_le_of_mem $ Ioo_mem_nhds_within_Ioi $ left_mem_Ico.2 h
@[simp]
lemma continuous_within_at_Ioc_iff_Ioi [topological_space β] {a b : α} {f : α → β} (h : a < b) :
continuous_within_at f (Ioc a b) a ↔ continuous_within_at f (Ioi a) a :=
by simp only [continuous_within_at, nhds_within_Ioc_eq_nhds_within_Ioi h]
@[simp]
lemma continuous_within_at_Ioo_iff_Ioi [topological_space β] {a b : α} {f : α → β} (h : a < b) :
continuous_within_at f (Ioo a b) a ↔ continuous_within_at f (Ioi a) a :=
by simp only [continuous_within_at, nhds_within_Ioo_eq_nhds_within_Ioi h]
/-!
#### Left neighborhoods, point excluded
-/
lemma Ioo_mem_nhds_within_Iio {a b c : α} (H : b ∈ Ioc a c) :
Ioo a c ∈ 𝓝[Iio b] b :=
by simpa only [dual_Ioo] using @Ioo_mem_nhds_within_Ioi (order_dual α) _ _ _ _ _ _ ⟨H.2, H.1⟩
lemma Ico_mem_nhds_within_Iio {a b c : α} (H : b ∈ Ioc a c) :
Ico a c ∈ 𝓝[Iio b] b :=
mem_of_superset (Ioo_mem_nhds_within_Iio H) Ioo_subset_Ico_self
lemma Ioc_mem_nhds_within_Iio {a b c : α} (H : b ∈ Ioc a c) :
Ioc a c ∈ 𝓝[Iio b] b :=
mem_of_superset (Ioo_mem_nhds_within_Iio H) Ioo_subset_Ioc_self
lemma Icc_mem_nhds_within_Iio {a b c : α} (H : b ∈ Ioc a c) :
Icc a c ∈ 𝓝[Iio b] b :=
mem_of_superset (Ioo_mem_nhds_within_Iio H) Ioo_subset_Icc_self
@[simp] lemma nhds_within_Ico_eq_nhds_within_Iio {a b : α} (h : a < b) :
𝓝[Ico a b] b = 𝓝[Iio b] b :=
by simpa only [dual_Ioc] using @nhds_within_Ioc_eq_nhds_within_Ioi (order_dual α) _ _ _ _ _ h
@[simp] lemma nhds_within_Ioo_eq_nhds_within_Iio {a b : α} (h : a < b) :
𝓝[Ioo a b] b = 𝓝[Iio b] b :=
by simpa only [dual_Ioo] using @nhds_within_Ioo_eq_nhds_within_Ioi (order_dual α) _ _ _ _ _ h
@[simp] lemma continuous_within_at_Ico_iff_Iio {a b : α} {f : α → γ} (h : a < b) :
continuous_within_at f (Ico a b) b ↔ continuous_within_at f (Iio b) b :=
by simp only [continuous_within_at, nhds_within_Ico_eq_nhds_within_Iio h]
@[simp] lemma continuous_within_at_Ioo_iff_Iio {a b : α} {f : α → γ} (h : a < b) :
continuous_within_at f (Ioo a b) b ↔ continuous_within_at f (Iio b) b :=
by simp only [continuous_within_at, nhds_within_Ioo_eq_nhds_within_Iio h]
/-!
#### Right neighborhoods, point included
-/
lemma Ioo_mem_nhds_within_Ici {a b c : α} (H : b ∈ Ioo a c) :
Ioo a c ∈ 𝓝[Ici b] b :=
mem_nhds_within_of_mem_nhds $ is_open.mem_nhds is_open_Ioo H
lemma Ioc_mem_nhds_within_Ici {a b c : α} (H : b ∈ Ioo a c) :
Ioc a c ∈ 𝓝[Ici b] b :=
mem_of_superset (Ioo_mem_nhds_within_Ici H) Ioo_subset_Ioc_self
lemma Ico_mem_nhds_within_Ici {a b c : α} (H : b ∈ Ico a c) :
Ico a c ∈ 𝓝[Ici b] b :=
mem_nhds_within.2 ⟨Iio c, is_open_Iio, H.2,
by simp only [inter_comm, Ici_inter_Iio, Ico_subset_Ico_left H.1]⟩
lemma Icc_mem_nhds_within_Ici {a b c : α} (H : b ∈ Ico a c) :
Icc a c ∈ 𝓝[Ici b] b :=
mem_of_superset (Ico_mem_nhds_within_Ici H) Ico_subset_Icc_self
@[simp] lemma nhds_within_Icc_eq_nhds_within_Ici {a b : α} (h : a < b) :
𝓝[Icc a b] a = 𝓝[Ici a] a :=
le_antisymm (nhds_within_mono _ Icc_subset_Ici_self) $
nhds_within_le_of_mem $ Icc_mem_nhds_within_Ici $ left_mem_Ico.2 h
@[simp] lemma nhds_within_Ico_eq_nhds_within_Ici {a b : α} (h : a < b) :
𝓝[Ico a b] a = 𝓝[Ici a] a :=
le_antisymm (nhds_within_mono _ (λ x, and.left)) $
nhds_within_le_of_mem $ Ico_mem_nhds_within_Ici $ left_mem_Ico.2 h
@[simp]
lemma continuous_within_at_Icc_iff_Ici [topological_space β] {a b : α} {f : α → β} (h : a < b) :
continuous_within_at f (Icc a b) a ↔ continuous_within_at f (Ici a) a :=
by simp only [continuous_within_at, nhds_within_Icc_eq_nhds_within_Ici h]
@[simp]
lemma continuous_within_at_Ico_iff_Ici [topological_space β] {a b : α} {f : α → β} (h : a < b) :
continuous_within_at f (Ico a b) a ↔ continuous_within_at f (Ici a) a :=
by simp only [continuous_within_at, nhds_within_Ico_eq_nhds_within_Ici h]
/-!
#### Left neighborhoods, point included
-/
lemma Ioo_mem_nhds_within_Iic {a b c : α} (H : b ∈ Ioo a c) :
Ioo a c ∈ 𝓝[Iic b] b :=
mem_nhds_within_of_mem_nhds $ is_open.mem_nhds is_open_Ioo H
lemma Ico_mem_nhds_within_Iic {a b c : α} (H : b ∈ Ioo a c) :
Ico a c ∈ 𝓝[Iic b] b :=
mem_of_superset (Ioo_mem_nhds_within_Iic H) Ioo_subset_Ico_self
lemma Ioc_mem_nhds_within_Iic {a b c : α} (H : b ∈ Ioc a c) :
Ioc a c ∈ 𝓝[Iic b] b :=
by simpa only [dual_Ico] using @Ico_mem_nhds_within_Ici (order_dual α) _ _ _ _ _ _ ⟨H.2, H.1⟩
lemma Icc_mem_nhds_within_Iic {a b c : α} (H : b ∈ Ioc a c) :
Icc a c ∈ 𝓝[Iic b] b :=
mem_of_superset (Ioc_mem_nhds_within_Iic H) Ioc_subset_Icc_self
@[simp] lemma nhds_within_Icc_eq_nhds_within_Iic {a b : α} (h : a < b) :
𝓝[Icc a b] b = 𝓝[Iic b] b :=
by simpa only [dual_Icc] using @nhds_within_Icc_eq_nhds_within_Ici (order_dual α) _ _ _ _ _ h
@[simp] lemma nhds_within_Ioc_eq_nhds_within_Iic {a b : α} (h : a < b) :
𝓝[Ioc a b] b = 𝓝[Iic b] b :=
by simpa only [dual_Ico] using @nhds_within_Ico_eq_nhds_within_Ici (order_dual α) _ _ _ _ _ h
@[simp]
lemma continuous_within_at_Icc_iff_Iic [topological_space β] {a b : α} {f : α → β} (h : a < b) :
continuous_within_at f (Icc a b) b ↔ continuous_within_at f (Iic b) b :=
by simp only [continuous_within_at, nhds_within_Icc_eq_nhds_within_Iic h]
@[simp]
lemma continuous_within_at_Ioc_iff_Iic [topological_space β] {a b : α} {f : α → β} (h : a < b) :
continuous_within_at f (Ioc a b) b ↔ continuous_within_at f (Iic b) b :=
by simp only [continuous_within_at, nhds_within_Ioc_eq_nhds_within_Iic h]
end linear_order
section linear_order
variables [topological_space α] [linear_order α] [order_closed_topology α] {f g : β → α}
section
variables [topological_space β]
lemma frontier_le_subset_eq (hf : continuous f) (hg : continuous g) :
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_Iic_subset (a : α) : frontier (Iic a) ⊆ {a} :=
frontier_le_subset_eq (@continuous_id α _) continuous_const
lemma frontier_Ici_subset (a : α) : frontier (Ici a) ⊆ {a} :=
@frontier_Iic_subset (order_dual α) _ _ _ _
lemma frontier_lt_subset_eq (hf : continuous f) (hg : continuous g) :
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_if_le [topological_space γ] [Π x, decidable (f x ≤ g x)]
{f' g' : β → γ} (hf : continuous f) (hg : continuous g)
(hf' : continuous_on f' {x | f x ≤ g x}) (hg' : continuous_on g' {x | g x ≤ f x})
(hfg : ∀ x, f x = g x → f' x = g' x) :
continuous (λ x, if f x ≤ g x then f' x else g' x) :=
begin
refine continuous_if (λ a ha, hfg _ (frontier_le_subset_eq hf hg ha)) _ (hg'.mono _),
{ rwa [(is_closed_le hf hg).closure_eq] },
{ simp only [not_le], exact closure_lt_subset_le hg hf }
end
lemma continuous.if_le [topological_space γ] [Π x, decidable (f x ≤ g x)] {f' g' : β → γ}
(hf' : continuous f') (hg' : continuous g') (hf : continuous f) (hg : continuous g)
(hfg : ∀ x, f x = g x → f' x = g' x) :
continuous (λ x, if f x ≤ g x then f' x else g' x) :=
continuous_if_le hf hg hf'.continuous_on hg'.continuous_on hfg
@[continuity] lemma continuous.min (hf : continuous f) (hg : continuous g) :
continuous (λb, min (f b) (g b)) :=
hf.if_le hg hf hg (λ x, id)
@[continuity] lemma continuous.max (hf : continuous f) (hg : continuous g) :
continuous (λb, max (f b) (g b)) :=
@continuous.min (order_dual α) _ _ _ _ _ _ _ hf hg
end
lemma continuous_min : continuous (λ p : α × α, min p.1 p.2) := continuous_fst.min continuous_snd
lemma continuous_max : continuous (λ p : α × α, max p.1 p.2) := continuous_fst.max continuous_snd
lemma filter.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₂)) :=
(continuous_max.tendsto (a₁, a₂)).comp (hf.prod_mk_nhds hg)
lemma filter.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₂)) :=
(continuous_min.tendsto (a₁, a₂)).comp (hf.prod_mk_nhds hg)
lemma is_preconnected.ord_connected {s : set α} (h : is_preconnected s) :
ord_connected s :=
⟨λ x hx y hy, h.Icc_subset hx hy⟩
end 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 :=
is_open.mem_nhds (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 :=
is_open.mem_nhds (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, 𝓟 (Ioi b)) ⊓ (⨅b ∈ Ioi a, 𝓟 (Iio b)) :=
by rw [t.topology_eq_generate_intervals, nhds_generate_from];
from le_antisymm
(le_inf
(le_binfi $ assume b hb, infi_le_of_le {c : α | b < c} $ infi_le _ ⟨hb, b, or.inl rfl⟩)
(le_binfi $ assume b 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_of_left_le $ infi_le_of_le b $ infi_le _ h
| _, h, (or.inr rfl) := inf_le_of_right_le $ infi_le_of_le b $ infi_le _ h
end)
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]
instance tendsto_Icc_class_nhds (a : α) : tendsto_Ixx_class Icc (𝓝 a) (𝓝 a) :=
begin
simp only [nhds_eq_order, infi_subtype'],
refine ((has_basis_infi_principal_finite _).inf
(has_basis_infi_principal_finite _)).tendsto_Ixx_class (λ s hs, _),
refine ((ord_connected_bInter _).inter (ord_connected_bInter _)).out; intros _ _,
exacts [ord_connected_Ioi, ord_connected_Iio]
end
instance tendsto_Ico_class_nhds (a : α) : tendsto_Ixx_class Ico (𝓝 a) (𝓝 a) :=
tendsto_Ixx_class_of_subset (λ _ _, Ico_subset_Icc_self)
instance tendsto_Ioc_class_nhds (a : α) : tendsto_Ixx_class Ioc (𝓝 a) (𝓝 a) :=
tendsto_Ixx_class_of_subset (λ _ _, Ioc_subset_Icc_self)
instance tendsto_Ioo_class_nhds (a : α) : tendsto_Ixx_class Ioo (𝓝 a) (𝓝 a) :=
tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Icc_self)
/-- 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), 𝓟 (Ioo l u)) :=
have ∃ u, u ∈ Ioi a, from hu, have ∃ l, l ∈ Iio a, from hl,
by { simp only [nhds_eq_order, inf_binfi, binfi_inf, *, inf_principal, Ioi_inter_Iio], 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
instance tendsto_Ixx_nhds_within {α : Type*} [preorder α] [topological_space α]
(a : α) {s t : set α} {Ixx}
[tendsto_Ixx_class Ixx (𝓝 a) (𝓝 a)] [tendsto_Ixx_class Ixx (𝓟 s) (𝓟 t)]:
tendsto_Ixx_class Ixx (𝓝[s] a) (𝓝[t] a) :=
filter.tendsto_Ixx_class_inf
instance tendsto_Icc_class_nhds_pi {ι : Type*} {α : ι → Type*}
[Π i, partial_order (α i)] [Π i, topological_space (α i)] [∀ i, order_topology (α i)]
(f : Π i, α i) :
tendsto_Ixx_class Icc (𝓝 f) (𝓝 f) :=
begin
constructor,
conv in ((𝓝 f).lift' powerset) { rw [nhds_pi] },
simp only [lift'_infi_powerset, comap_lift'_eq2 monotone_powerset, tendsto_infi, tendsto_lift',
mem_powerset_iff, subset_def, mem_preimage],
intros i s hs,
have : tendsto (λ g : Π i, α i, g i) (𝓝 f) (𝓝 (f i)) := ((continuous_apply i).tendsto f),
refine (tendsto_lift'.1 ((this.comp tendsto_fst).Icc (this.comp tendsto_snd)) s hs).mono _,
exact λ p hp g hg, hp ⟨hg.1 _, hg.2 _⟩
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₁ : ∀ {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.2 ⟨{x | f b < x},
mem_inf_of_left $ mem_infi_of_mem _ $ mem_infi_of_mem (hf.2 ab) $ mem_principal_self _,
λ x, hf.1⟩ },
{ exact mem_comap.2 ⟨{x | x < f b},
mem_inf_of_right $ mem_infi_of_mem _ $ mem_infi_of_mem (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_of_mem _ (mem_infi_of_mem ⟨ab, b, or.inl rfl⟩ (mem_principal.2 _)),
exact λ c hc, lt_of_le_of_lt xb (hf.2 hc) },
{ rcases H₂ h with ⟨b, ab, xb⟩,
refine mem_infi_of_mem _ (mem_infi_of_mem ⟨ab, b, or.inr rfl⟩ (mem_principal.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⟩)
/-- On an `ord_connected` subset of a linear order, the order topology for the restriction of the
order is the same as the restriction to the subset of the order topology. -/
instance order_topology_of_ord_connected {α : Type u}
[ta : topological_space α] [linear_order α] [order_topology α]
{t : set α} [ht : ord_connected t] :
order_topology t :=
begin
letI := induced (coe : t → α) ta,
refine ⟨eq_of_nhds_eq_nhds (λ a, _)⟩,
rw [nhds_induced, nhds_generate_from, nhds_eq_order (a : α)],
apply le_antisymm,
{ refine le_infi (λ s, le_infi $ λ hs, le_principal_iff.2 _),
rcases hs with ⟨ab, b, rfl|rfl⟩,
{ refine ⟨Ioi b, _, λ _, id⟩,
refine mem_inf_of_left (mem_infi_of_mem b _),
exact mem_infi_of_mem ab (mem_principal_self (Ioi ↑b)) },
{ refine ⟨Iio b, _, λ _, id⟩,
refine mem_inf_of_right (mem_infi_of_mem b _),
exact mem_infi_of_mem ab (mem_principal_self (Iio b)) } },
{ rw [← map_le_iff_le_comap],
refine le_inf _ _,
{ refine le_infi (λ x, le_infi $ λ h, le_principal_iff.2 _),
by_cases hx : x ∈ t,
{ refine mem_infi_of_mem (Ioi ⟨x, hx⟩) (mem_infi_of_mem ⟨h, ⟨⟨x, hx⟩, or.inl rfl⟩⟩ _),
exact λ _, id },
simp only [set_coe.exists, mem_set_of_eq, mem_map'],
convert univ_sets _,
suffices hx' : ∀ (y : t), ↑y ∈ Ioi x,
{ simp [hx'] },
intros y,
revert hx,
contrapose!,
-- here we use the `ord_connected` hypothesis
exact λ hx, ht.out y.2 a.2 ⟨le_of_not_gt hx, le_of_lt h⟩ },
{ refine le_infi (λ x, le_infi $ λ h, le_principal_iff.2 _),
by_cases hx : x ∈ t,
{ refine mem_infi_of_mem (Iio ⟨x, hx⟩) (mem_infi_of_mem ⟨h, ⟨⟨x, hx⟩, or.inr rfl⟩⟩ _),
exact λ _, id },
simp only [set_coe.exists, mem_set_of_eq, mem_map'],
convert univ_sets _,
suffices hx' : ∀ (y : t), ↑y ∈ Iio x,
{ simp [hx'] },
intros y,
revert hx,
contrapose!,
-- here we use the `ord_connected` hypothesis
exact λ hx, ht.out a.2 y.2 ⟨le_of_lt h, le_of_not_gt hx⟩ } }
end
lemma nhds_top_order [topological_space α] [order_top α] [order_topology α] :
𝓝 (⊤:α) = (⨅l (h₂ : l < ⊤), 𝓟 (Ioi l)) :=
by simp [nhds_eq_order (⊤:α)]
lemma nhds_bot_order [topological_space α] [order_bot α] [order_topology α] :
𝓝 (⊥:α) = (⨅l (h₂ : ⊥ < l), 𝓟 (Iio l)) :=
by simp [nhds_eq_order (⊥:α)]
lemma nhds_top_basis [topological_space α] [semilattice_sup_top α] [is_total α has_le.le]
[order_topology α] [nontrivial α] :
(𝓝 ⊤).has_basis (λ a : α, a < ⊤) (λ a : α, Ioi a) :=
⟨ begin
simp only [nhds_top_order],
refine @filter.mem_binfi_of_directed α α (λ a, 𝓟 (Ioi a)) (λ a, a < ⊤) _ _,
{ rintros a (ha : a < ⊤) b (hb : b < ⊤),
use a ⊔ b,
simp only [filter.le_principal_iff, ge_iff_le, order.preimage],
exact ⟨sup_lt_iff.mpr ⟨ha, hb⟩, Ioi_subset_Ioi le_sup_left, Ioi_subset_Ioi le_sup_right⟩ },
{ obtain ⟨a, ha⟩ : ∃ a : α, a ≠ ⊤ := exists_ne ⊤,
exact ⟨a, lt_top_iff_ne_top.mpr ha⟩ }
end ⟩
lemma nhds_bot_basis [topological_space α] [semilattice_inf_bot α] [is_total α has_le.le]
[order_topology α] [nontrivial α] :
(𝓝 ⊥).has_basis (λ a : α, ⊥ < a) (λ a : α, Iio a) :=
@nhds_top_basis (order_dual α) _ _ _ _ _
lemma nhds_top_basis_Ici [topological_space α] [semilattice_sup_top α] [is_total α has_le.le]
[order_topology α] [nontrivial α] [densely_ordered α] :
(𝓝 ⊤).has_basis (λ a : α, a < ⊤) Ici :=
nhds_top_basis.to_has_basis
(λ a ha, let ⟨b, hab, hb⟩ := exists_between ha in ⟨b, hb, Ici_subset_Ioi.mpr hab⟩)
(λ a ha, ⟨a, ha, Ioi_subset_Ici_self⟩)
lemma nhds_bot_basis_Iic [topological_space α] [semilattice_inf_bot α] [is_total α has_le.le]
[order_topology α] [nontrivial α] [densely_ordered α] :
(𝓝 ⊥).has_basis (λ a : α, ⊥ < a) Iic :=
@nhds_top_basis_Ici (order_dual α) _ _ _ _ _ _
lemma tendsto_nhds_top_mono [topological_space β] [order_top β] [order_topology β] {l : filter α}
{f g : α → β} (hf : tendsto f l (𝓝 ⊤)) (hg : f ≤ᶠ[l] g) :
tendsto g l (𝓝 ⊤) :=
begin
simp only [nhds_top_order, tendsto_infi, tendsto_principal] at hf ⊢,
intros x hx,
filter_upwards [hf x hx, hg],
exact λ x, lt_of_lt_of_le
end
lemma tendsto_nhds_bot_mono [topological_space β] [order_bot β] [order_topology β] {l : filter α}
{f g : α → β} (hf : tendsto f l (𝓝 ⊥)) (hg : g ≤ᶠ[l] f) :
tendsto g l (𝓝 ⊥) :=
@tendsto_nhds_top_mono α (order_dual β) _ _ _ _ _ _ hf hg
lemma tendsto_nhds_top_mono' [topological_space β] [order_top β] [order_topology β] {l : filter α}
{f g : α → β} (hf : tendsto f l (𝓝 ⊤)) (hg : f ≤ g) :
tendsto g l (𝓝 ⊤) :=
tendsto_nhds_top_mono hf (eventually_of_forall hg)
lemma tendsto_nhds_bot_mono' [topological_space β] [order_bot β] [order_topology β] {l : filter α}
{f g : α → β} (hf : tendsto f l (𝓝 ⊥)) (hg : g ≤ f) :
tendsto g l (𝓝 ⊥) :=
tendsto_nhds_bot_mono hf (eventually_of_forall hg)
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₂, rfl⟩,
-- First we show that `t₂` includes `(-∞, a]`, so it suffices to show `(l', ∞) ⊆ t₁`
suffices : ∃ l' ∈ Ico l a, Ioi l' ⊆ t₁,
{ have A : 𝓟 (Iic a) ≤ ⨅ b ∈ Ioi a, 𝓟 (Iio b),
from (le_infi $ λ b, le_infi $ λ hb, principal_mono.2 $ Iic_subset_Iio.2 hb),
have B : t₁ ∩ Iic a ⊆ t₁ ∩ t₂,
from inter_subset_inter_right _ (A ht₂),
from this.imp (λ l', Exists.imp $ λ hl' hl x hx, B ⟨hl hx.1, hx.2⟩) },
clear ht₂ t₂,
-- Now we find `l` such that `(l', ∞) ⊆ t₁`
rw [mem_binfi_of_directed] 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 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_compl_iff.1 $ 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 is_open.mem_nhds hs.is_open_compl ha,
have ∃t:set α, is_open t ∧ (∀l∈ s, l < a → l ∈ t) ∧ 𝓝[t] a = ⊥,
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.2 $ (𝓝 a).sets_of_superset ((is_open_lt' _).mem_nhds 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.2 $ (𝓝 a).sets_of_superset ((is_open_lt' _).mem_nhds 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,
nhds_within_empty _⟩),
let ⟨t₁, ht₁o, ht₁s, ht₁a⟩ := this in
have ∃t:set α, is_open t ∧ (∀u∈ s, u>a → u ∈ t) ∧ 𝓝[t] a = ⊥,
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.2 $ (𝓝 a).sets_of_superset ((is_open_gt' _).mem_nhds 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.2 $ (𝓝 a).sets_of_superset ((is_open_gt' _).mem_nhds 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,
nhds_within_empty _⟩),
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 [nhds_within_union, 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 : α} {s : set α} (hl : ∃ l, l < a) (hu : ∃ u, 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, au⟩, λ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_of_superset (is_open.mem_nhds 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 :=
mem_nhds_iff_exists_Ioo_subset' (no_bot a) (no_top a)
lemma nhds_basis_Ioo' {a : α} (hl : ∃ l, l < a) (hu : ∃ u, a < u) :
(𝓝 a).has_basis (λ b : α × α, b.1 < a ∧ a < b.2) (λ b, Ioo b.1 b.2) :=
⟨λ s, (mem_nhds_iff_exists_Ioo_subset' hl hu).trans $ by simp⟩
lemma nhds_basis_Ioo [no_top_order α] [no_bot_order α] (a : α) :
(𝓝 a).has_basis (λ b : α × α, b.1 < a ∧ a < b.2) (λ b, Ioo b.1 b.2) :=
nhds_basis_Ioo' (no_bot a) (no_top a)
lemma filter.eventually.exists_Ioo_subset [no_top_order α] [no_bot_order α] {a : α} {p : α → Prop}
(hp : ∀ᶠ x in 𝓝 a, p x) :
∃ l u, a ∈ Ioo l u ∧ Ioo l u ⊆ {x | p x} :=
mem_nhds_iff_exists_Ioo_subset.1 hp
lemma Iio_mem_nhds {a b : α} (h : a < b) : Iio b ∈ 𝓝 a :=
is_open.mem_nhds is_open_Iio h
lemma Ioi_mem_nhds {a b : α} (h : a < b) : Ioi a ∈ 𝓝 b :=
is_open.mem_nhds is_open_Ioi h
lemma Iic_mem_nhds {a b : α} (h : a < b) : Iic b ∈ 𝓝 a :=
mem_of_superset (Iio_mem_nhds h) Iio_subset_Iic_self
lemma Ici_mem_nhds {a b : α} (h : a < b) : Ici a ∈ 𝓝 b :=
mem_of_superset (Ioi_mem_nhds h) Ioi_subset_Ici_self
lemma Ioo_mem_nhds {a b x : α} (ha : a < x) (hb : x < b) : Ioo a b ∈ 𝓝 x :=
is_open.mem_nhds is_open_Ioo ⟨ha, hb⟩
lemma Ioc_mem_nhds {a b x : α} (ha : a < x) (hb : x < b) : Ioc a b ∈ 𝓝 x :=
mem_of_superset (Ioo_mem_nhds ha hb) Ioo_subset_Ioc_self
lemma Ico_mem_nhds {a b x : α} (ha : a < x) (hb : x < b) : Ico a b ∈ 𝓝 x :=
mem_of_superset (Ioo_mem_nhds ha hb) Ioo_subset_Ico_self
lemma Icc_mem_nhds {a b x : α} (ha : a < x) (hb : x < b) : Icc a b ∈ 𝓝 x :=
mem_of_superset (Ioo_mem_nhds ha hb) Ioo_subset_Icc_self
section pi
/-!
### Intervals in `Π i, π i` belong to `𝓝 x`
For each lemma `pi_Ixx_mem_nhds` we add a non-dependent version `pi_Ixx_mem_nhds'` because
sometimes Lean fails to unify different instances while trying to apply the dependent version to,
e.g., `ι → ℝ`.
-/
variables {ι : Type*} {π : ι → Type*} [fintype ι] [Π i, linear_order (π i)]
[Π i, topological_space (π i)] [∀ i, order_topology (π i)] {a b x : Π i, π i} {a' b' x' : ι → α}
lemma pi_Iic_mem_nhds (ha : ∀ i, x i < a i) : Iic a ∈ 𝓝 x :=
pi_univ_Iic a ▸ set_pi_mem_nhds (finite.of_fintype _) (λ i _, Iic_mem_nhds (ha _))
lemma pi_Iic_mem_nhds' (ha : ∀ i, x' i < a' i) : Iic a' ∈ 𝓝 x' :=
pi_Iic_mem_nhds ha
lemma pi_Ici_mem_nhds (ha : ∀ i, a i < x i) : Ici a ∈ 𝓝 x :=
pi_univ_Ici a ▸ set_pi_mem_nhds (finite.of_fintype _) (λ i _, Ici_mem_nhds (ha _))
lemma pi_Ici_mem_nhds' (ha : ∀ i, a' i < x' i) : Ici a' ∈ 𝓝 x' :=
pi_Ici_mem_nhds ha
lemma pi_Icc_mem_nhds (ha : ∀ i, a i < x i) (hb : ∀ i, x i < b i) : Icc a b ∈ 𝓝 x :=
pi_univ_Icc a b ▸ set_pi_mem_nhds (finite.of_fintype _) (λ i _, Icc_mem_nhds (ha _) (hb _))
lemma pi_Icc_mem_nhds' (ha : ∀ i, a' i < x' i) (hb : ∀ i, x' i < b' i) : Icc a' b' ∈ 𝓝 x' :=
pi_Icc_mem_nhds ha hb
variables [nonempty ι]
lemma pi_Iio_mem_nhds (ha : ∀ i, x i < a i) : Iio a ∈ 𝓝 x :=
begin
refine mem_of_superset (set_pi_mem_nhds (finite.of_fintype _) (λ i _, _))
(pi_univ_Iio_subset a),
exact Iio_mem_nhds (ha i)
end
lemma pi_Iio_mem_nhds' (ha : ∀ i, x' i < a' i) : Iio a' ∈ 𝓝 x' :=
pi_Iio_mem_nhds ha
lemma pi_Ioi_mem_nhds (ha : ∀ i, a i < x i) : Ioi a ∈ 𝓝 x :=
@pi_Iio_mem_nhds ι (λ i, order_dual (π i)) _ _ _ _ _ _ _ ha
lemma pi_Ioi_mem_nhds' (ha : ∀ i, a' i < x' i) : Ioi a' ∈ 𝓝 x' :=
pi_Ioi_mem_nhds ha
lemma pi_Ioc_mem_nhds (ha : ∀ i, a i < x i) (hb : ∀ i, x i < b i) : Ioc a b ∈ 𝓝 x :=
begin
refine mem_of_superset (set_pi_mem_nhds (finite.of_fintype _) (λ i _, _))
(pi_univ_Ioc_subset a b),
exact Ioc_mem_nhds (ha i) (hb i)
end
lemma pi_Ioc_mem_nhds' (ha : ∀ i, a' i < x' i) (hb : ∀ i, x' i < b' i) : Ioc a' b' ∈ 𝓝 x' :=
pi_Ioc_mem_nhds ha hb
lemma pi_Ico_mem_nhds (ha : ∀ i, a i < x i) (hb : ∀ i, x i < b i) : Ico a b ∈ 𝓝 x :=
begin
refine mem_of_superset (set_pi_mem_nhds (finite.of_fintype _) (λ i _, _))
(pi_univ_Ico_subset a b),
exact Ico_mem_nhds (ha i) (hb i)
end
lemma pi_Ico_mem_nhds' (ha : ∀ i, a' i < x' i) (hb : ∀ i, x' i < b' i) : Ico a' b' ∈ 𝓝 x' :=
pi_Ico_mem_nhds ha hb
lemma pi_Ioo_mem_nhds (ha : ∀ i, a i < x i) (hb : ∀ i, x i < b i) : Ioo a b ∈ 𝓝 x :=
begin
refine mem_of_superset (set_pi_mem_nhds (finite.of_fintype _) (λ i _, _))
(pi_univ_Ioo_subset a b),
exact Ioo_mem_nhds (ha i) (hb i)
end
lemma pi_Ioo_mem_nhds' (ha : ∀ i, a' i < x' i) (hb : ∀ i, x' i < b' i) : Ioo a' b' ∈ 𝓝 x' :=
pi_Ioo_mem_nhds ha hb
end pi
lemma disjoint_nhds_at_top [no_top_order α] (x : α) :
disjoint (𝓝 x) at_top :=
begin
rw filter.disjoint_iff,
cases no_top x with a ha,
use [Iio a, Iio_mem_nhds ha, Ici a, mem_at_top a],
rw [inter_comm, Ici_inter_Iio, Ico_self]
end
@[simp] lemma inf_nhds_at_top [no_top_order α] (x : α) :
𝓝 x ⊓ at_top = ⊥ :=
disjoint_iff.1 (disjoint_nhds_at_top x)
lemma disjoint_nhds_at_bot [no_bot_order α] (x : α) :
disjoint (𝓝 x) at_bot :=
@disjoint_nhds_at_top (order_dual α) _ _ _ _ x
@[simp] lemma inf_nhds_at_bot [no_bot_order α] (x : α) :
𝓝 x ⊓ at_bot = ⊥ :=
@inf_nhds_at_top (order_dual α) _ _ _ _ x
lemma not_tendsto_nhds_of_tendsto_at_top [no_top_order α]
{F : filter β} [ne_bot F] {f : β → α} (hf : tendsto f F at_top) (x : α) :
¬ tendsto f F (𝓝 x) :=
hf.not_tendsto (disjoint_nhds_at_top x).symm
lemma not_tendsto_at_top_of_tendsto_nhds [no_top_order α]
{F : filter β} [ne_bot F] {f : β → α} {x : α} (hf : tendsto f F (𝓝 x)) :
¬ tendsto f F at_top :=
hf.not_tendsto (disjoint_nhds_at_top x)
lemma not_tendsto_nhds_of_tendsto_at_bot [no_bot_order α]
{F : filter β} [ne_bot F] {f : β → α} (hf : tendsto f F at_bot) (x : α) :
¬ tendsto f F (𝓝 x) :=
hf.not_tendsto (disjoint_nhds_at_bot x).symm
lemma not_tendsto_at_bot_of_tendsto_nhds [no_bot_order α]
{F : filter β} [ne_bot F] {f : β → α} {x : α} (hf : tendsto f F (𝓝 x)) :
¬ tendsto f F at_bot :=
hf.not_tendsto (disjoint_nhds_at_bot x)
/-!
### Neighborhoods to the left and to the right on an `order_topology`
We've seen some properties of left and right neighborhood of a point in an `order_closed_topology`.
In an `order_topology`, 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 ∈ 𝓝[Ioi a] a, -- 0 : `s` is a neighborhood of `a` within `(a, +∞)`
s ∈ 𝓝[Ioc a b] a, -- 1 : `s` is a neighborhood of `a` within `(a, b]`
s ∈ 𝓝[Ioo a b] a, -- 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, by rw [nhds_within_Ioc_eq_nhds_within_Ioi hab],
tfae_have : 1 ↔ 3, by rw [nhds_within_Ioo_eq_nhds_within_Ioi hab],
tfae_have : 4 → 5, from λ ⟨u, umem, hu⟩, ⟨u, umem.1, hu⟩,
tfae_have : 5 → 1,
{ rintros ⟨u, hau, hu⟩,
exact mem_of_superset (Ioo_mem_nhds_within_Ioi ⟨le_refl a, hau⟩) hu },
tfae_have : 1 → 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 hx.1 },
tfae_finish
end
lemma mem_nhds_within_Ioi_iff_exists_mem_Ioc_Ioo_subset {a u' : α} {s : set α} (hu' : a < u') :
s ∈ 𝓝[Ioi a] 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 ∈ 𝓝[Ioi a] 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 ∈ 𝓝[Ioi a] 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 ∈ 𝓝[Ioi a] a ↔ ∃u ∈ Ioi a, Ioc a u ⊆ s :=
begin
rw mem_nhds_within_Ioi_iff_exists_Ioo_subset,
split,
{ rintros ⟨u, au, as⟩,
rcases exists_between 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
/-- 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 ∈ 𝓝[Iio b] b, -- 0 : `s` is a neighborhood of `b` within `(-∞, b)`
s ∈ 𝓝[Ico a b] b, -- 1 : `s` is a neighborhood of `b` within `[a, b)`
s ∈ 𝓝[Ioo a b] 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
lemma mem_nhds_within_Iio_iff_exists_mem_Ico_Ioo_subset {a l' : α} {s : set α} (hl' : l' < a) :
s ∈ 𝓝[Iio a] 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 ∈ 𝓝[Iio a] 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 ∈ 𝓝[Iio a] 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 ∈ 𝓝[Iio a] 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
/-- 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_Ici {a b : α} (hab : a < b) (s : set α) :
tfae [s ∈ 𝓝[Ici a] a, -- 0 : `s` is a neighborhood of `a` within `[a, +∞)`
s ∈ 𝓝[Icc a b] a, -- 1 : `s` is a neighborhood of `a` within `[a, b]`
s ∈ 𝓝[Ico a b] a, -- 2 : `s` is a neighborhood of `a` within `[a, b)`
∃ u ∈ Ioc a b, Ico a u ⊆ s, -- 3 : `s` includes `[a, u)` for some `u ∈ (a, b]`
∃ u ∈ Ioi a, Ico a u ⊆ s] := -- 4 : `s` includes `[a, u)` for some `u > a`
begin
tfae_have : 1 ↔ 2, by rw [nhds_within_Icc_eq_nhds_within_Ici hab],
tfae_have : 1 ↔ 3, by rw [nhds_within_Ico_eq_nhds_within_Ici hab],
tfae_have : 4 → 5, from λ ⟨u, umem, hu⟩, ⟨u, umem.1, hu⟩,
tfae_have : 5 → 1,
{ rintros ⟨u, hau, hu⟩,
exact mem_of_superset (Ico_mem_nhds_within_Ici ⟨le_refl a, hau⟩) hu },
tfae_have : 1 → 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 ⟨hx.1, hx.2⟩, _⟩,
exact hx.1 },
tfae_finish
end
lemma mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset {a u' : α} {s : set α} (hu' : a < u') :
s ∈ 𝓝[Ici a] a ↔ ∃u ∈ Ioc a u', Ico a u ⊆ s :=
(tfae_mem_nhds_within_Ici hu' s).out 0 3 (by norm_num) (by norm_num)
/-- 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_Ici_iff_exists_Ico_subset' {a u' : α} {s : set α} (hu' : a < u') :
s ∈ 𝓝[Ici a] a ↔ ∃u ∈ Ioi a, Ico a u ⊆ s :=
(tfae_mem_nhds_within_Ici hu' s).out 0 4 (by norm_num) (by norm_num)
/-- 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 ∈ 𝓝[Ici a] a ↔ ∃u ∈ Ioi a, 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 ∈ 𝓝[Ici a] a ↔ ∃u ∈ Ioi a, Icc a u ⊆ s :=
begin
rw mem_nhds_within_Ici_iff_exists_Ico_subset,
split,
{ rintros ⟨u, au, as⟩,
rcases exists_between 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
/-- 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_Iic {a b : α} (h : a < b) (s : set α) :
tfae [s ∈ 𝓝[Iic b] b, -- 0 : `s` is a neighborhood of `b` within `(-∞, b]`
s ∈ 𝓝[Icc a b] b, -- 1 : `s` is a neighborhood of `b` within `[a, b]`
s ∈ 𝓝[Ioc a b] b, -- 2 : `s` is a neighborhood of `b` within `(a, b]`
∃ l ∈ Ico a b, Ioc l b ⊆ s, -- 3 : `s` includes `(l, b]` for some `l ∈ [a, b)`
∃ l ∈ Iio b, Ioc l b ⊆ s] := -- 4 : `s` includes `(l, b]` for some `l < b`
begin
have := @tfae_mem_nhds_within_Ici (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_Icc, dual_Ioc, dual_Ioi] at this,
convert this; ext l; rw [dual_Ico]
end
lemma mem_nhds_within_Iic_iff_exists_mem_Ico_Ioc_subset {a l' : α} {s : set α} (hl' : l' < a) :
s ∈ 𝓝[Iic a] a ↔ ∃l ∈ Ico l' a, Ioc l a ⊆ s :=
(tfae_mem_nhds_within_Iic hl' s).out 0 3 (by norm_num) (by norm_num)
/-- 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 ∈ 𝓝[Iic a] a ↔ ∃l ∈ Iio a, Ioc l a ⊆ s :=
(tfae_mem_nhds_within_Iic hl' s).out 0 4 (by norm_num) (by norm_num)
/-- 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 ∈ 𝓝[Iic a] a ↔ ∃l ∈ Iio 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 ∈ 𝓝[Iic a] a ↔ ∃l ∈ Iio a, Icc l a ⊆ s :=
begin
convert @mem_nhds_within_Ici_iff_exists_Icc_subset' (order_dual α) _ _ _ _ _ _ _,
simp_rw (show ∀ u : order_dual α, @Icc (order_dual α) _ a u = @Icc α _ u a, from λ u, dual_Icc),
refl,
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_Icc_subset [no_top_order α] [densely_ordered α]
{a : α} {s : set α} : s ∈ 𝓝[Ici a] a ↔ ∃u, a < u ∧ Icc a u ⊆ s :=
begin
rw mem_nhds_within_Ici_iff_exists_Ico_subset,
split,
{ rintros ⟨u, au, as⟩,
rcases exists_between 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`. -/
lemma mem_nhds_within_Iic_iff_exists_Icc_subset [no_bot_order α] [densely_ordered α]
{a : α} {s : set α} : s ∈ 𝓝[Iic a] a ↔ ∃l, l < a ∧ Icc l a ⊆ s :=
begin
rw mem_nhds_within_Iic_iff_exists_Ioc_subset,
split,
{ rintros ⟨l, la, as⟩,
rcases exists_between 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
section linear_ordered_add_comm_group
variables [topological_space α] [linear_ordered_add_comm_group α] [order_topology α]
variables {l : filter β} {f g : β → α}
local notation `|` x `|` := abs x
lemma nhds_eq_infi_abs_sub (a : α) : 𝓝 a = (⨅r>0, 𝓟 {b | |a - b| < r}) :=
begin
simp only [le_antisymm_iff, nhds_eq_order, le_inf_iff, le_infi_iff, le_principal_iff, mem_Ioi,
mem_Iio, abs_sub_lt_iff, @sub_lt_iff_lt_add _ _ _ _ _ _ a, @sub_lt _ _ _ _ a, set_of_and],
refine ⟨_, _, _⟩,
{ intros ε ε0,
exact inter_mem_inf
(mem_infi_of_mem (a - ε) $ mem_infi_of_mem (sub_lt_self a ε0) (mem_principal_self _))
(mem_infi_of_mem (ε + a) $ mem_infi_of_mem (by simpa) (mem_principal_self _)) },
{ intros b hb,
exact mem_infi_of_mem (a - b) (mem_infi_of_mem (sub_pos.2 hb) (by simp [Ioi])) },
{ intros b hb,
exact mem_infi_of_mem (b - a) (mem_infi_of_mem (sub_pos.2 hb) (by simp [Iio])) }
end
lemma order_topology_of_nhds_abs {α : Type*} [topological_space α] [linear_ordered_add_comm_group α]
(h_nhds : ∀a:α, 𝓝 a = (⨅r>0, 𝓟 {b | |a - b| < r})) : order_topology α :=
begin
refine ⟨eq_of_nhds_eq_nhds $ λ a, _⟩,
rw [h_nhds],
letI := preorder.topology α, letI : order_topology α := ⟨rfl⟩,
exact (nhds_eq_infi_abs_sub a).symm
end
lemma linear_ordered_add_comm_group.tendsto_nhds {x : filter β} {a : α} :
tendsto f x (𝓝 a) ↔ ∀ ε > (0 : α), ∀ᶠ b in x, |f b - a| < ε :=
by simp [nhds_eq_infi_abs_sub, abs_sub_comm a]
lemma eventually_abs_sub_lt (a : α) {ε : α} (hε : 0 < ε) : ∀ᶠ x in 𝓝 a, |x - a| < ε :=
(nhds_eq_infi_abs_sub a).symm ▸ mem_infi_of_mem ε
(mem_infi_of_mem hε $ by simp only [abs_sub_comm, mem_principal_self])
@[priority 100] -- see Note [lower instance priority]
instance linear_ordered_add_comm_group.topological_add_group : topological_add_group α :=
{ continuous_add :=
begin
refine continuous_iff_continuous_at.2 _,
rintro ⟨a, b⟩,
refine linear_ordered_add_comm_group.tendsto_nhds.2 (λ ε ε0, _),
rcases dense_or_discrete 0 ε with (⟨δ, δ0, δε⟩|⟨h₁, h₂⟩),
{ -- If there exists `δ ∈ (0, ε)`, then we choose `δ`-nhd of `a` and `(ε-δ)`-nhd of `b`
filter_upwards [prod_is_open.mem_nhds (eventually_abs_sub_lt a δ0)
(eventually_abs_sub_lt b (sub_pos.2 δε))],
rintros ⟨x, y⟩ ⟨hx : |x - a| < δ, hy : |y - b| < ε - δ⟩,
rw [add_sub_comm],
calc |x - a + (y - b)| ≤ |x - a| + |y - b| : abs_add _ _
... < δ + (ε - δ) : add_lt_add hx hy
... = ε : add_sub_cancel'_right _ _ },
{ -- Otherewise `ε`-nhd of each point `a` is `{a}`
have hε : ∀ {x y}, abs (x - y) < ε → x = y,
{ intros x y h,
simpa [sub_eq_zero] using h₂ _ h },
filter_upwards [prod_is_open.mem_nhds (eventually_abs_sub_lt a ε0)
(eventually_abs_sub_lt b ε0)],
rintros ⟨x, y⟩ ⟨hx : |x - a| < ε, hy : |y - b| < ε⟩,
simpa [hε hx, hε hy] }
end,
continuous_neg := continuous_iff_continuous_at.2 $ λ a,
linear_ordered_add_comm_group.tendsto_nhds.2 $ λ ε ε0,
(eventually_abs_sub_lt a ε0).mono $ λ x hx, by rwa [neg_sub_neg, abs_sub_comm] }
@[continuity]
lemma continuous_abs : continuous (abs : α → α) := continuous_id.max continuous_neg
lemma filter.tendsto.abs {f : β → α} {a : α} {l : filter β} (h : tendsto f l (𝓝 a)) :
tendsto (λ x, |f x|) l (𝓝 (|a|)) :=
(continuous_abs.tendsto _).comp h
lemma nhds_basis_Ioo_pos [no_bot_order α] [no_top_order α] (a : α) :
(𝓝 a).has_basis (λ ε : α, (0 : α) < ε) (λ ε, Ioo (a-ε) (a+ε)) :=
⟨begin
refine λ t, (nhds_basis_Ioo a).mem_iff.trans ⟨_, _⟩,
{ rintros ⟨⟨l, u⟩, ⟨hl : l < a, hu : a < u⟩, h' : Ioo l u ⊆ t⟩,
refine ⟨min (a-l) (u-a), by apply lt_min; rwa sub_pos, _⟩,
rintros x ⟨hx, hx'⟩,
apply h',
rw [sub_lt, lt_min_iff, sub_lt_sub_iff_left] at hx,
rw [← sub_lt_iff_lt_add', lt_min_iff, sub_lt_sub_iff_right] at hx',
exact ⟨hx.1, hx'.2⟩ },
{ rintros ⟨ε, ε_pos, h⟩,
exact ⟨(a-ε, a+ε), by simp [ε_pos], h⟩ },
end⟩
lemma nhds_basis_abs_sub_lt [no_bot_order α] [no_top_order α] (a : α) :
(𝓝 a).has_basis (λ ε : α, (0 : α) < ε) (λ ε, {b | abs (b - a) < ε}) :=
begin
convert nhds_basis_Ioo_pos a,
{ ext ε,
change abs (x - a) < ε ↔ a - ε < x ∧ x < a + ε,
simp [abs_lt, sub_lt_iff_lt_add, add_comm ε a, add_comm x ε] }
end
variable (α)
lemma nhds_basis_zero_abs_sub_lt [no_bot_order α] [no_top_order α] :
(𝓝 (0 : α)).has_basis (λ ε : α, (0 : α) < ε) (λ ε, {b | abs b < ε}) :=
by simpa using nhds_basis_abs_sub_lt (0 : α)
variable {α}
/-- If `a` is positive we can form a basis from only nonnegative `Ioo` intervals -/
lemma nhds_basis_Ioo_pos_of_pos [no_bot_order α] [no_top_order α]
{a : α} (ha : 0 < a) :
(𝓝 a).has_basis (λ ε : α, (0 : α) < ε ∧ ε ≤ a) (λ ε, Ioo (a-ε) (a+ε)) :=
⟨ λ t, (nhds_basis_Ioo_pos a).mem_iff.trans
⟨λ h, let ⟨i, hi, hit⟩ := h in
⟨min i a, ⟨lt_min hi ha, min_le_right i a⟩, trans (Ioo_subset_Ioo
(sub_le_sub_left (min_le_left i a) a) (add_le_add_left (min_le_left i a) a)) hit⟩,
λ h, let ⟨i, hi, hit⟩ := h in ⟨i, hi.1, hit⟩ ⟩ ⟩
section
variables [topological_space β] {b : β} {a : α} {s : set β}
lemma continuous.abs (h : continuous f) : continuous (λ x, |f x|) := continuous_abs.comp h
lemma continuous_at.abs (h : continuous_at f b) : continuous_at (λ x, |f x|) b := h.abs
lemma continuous_within_at.abs (h : continuous_within_at f s b) :
continuous_within_at (λ x, |f x|) s b := h.abs
lemma continuous_on.abs (h : continuous_on f s) : continuous_on (λ x, |f x|) s :=
λ x hx, (h x hx).abs
lemma tendsto_abs_nhds_within_zero : tendsto (abs : α → α) (𝓝[{0}ᶜ] 0) (𝓝[Ioi 0] 0) :=
(continuous_abs.tendsto' (0 : α) 0 abs_zero).inf $ tendsto_principal_principal.2 $ λ x, abs_pos.2
end
/-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `C`
and `g` tends to `at_top` then `f + g` tends to `at_top`. -/
lemma filter.tendsto.add_at_top {C : α} (hf : tendsto f l (𝓝 C)) (hg : tendsto g l at_top) :
tendsto (λ x, f x + g x) l at_top :=
begin
nontriviality α,
obtain ⟨C', hC'⟩ : ∃ C', C' < C := no_bot C,
refine tendsto_at_top_add_left_of_le' _ C' _ hg,
exact (hf.eventually (lt_mem_nhds hC')).mono (λ x, le_of_lt)
end
/-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `C`
and `g` tends to `at_bot` then `f + g` tends to `at_bot`. -/
lemma filter.tendsto.add_at_bot {C : α} (hf : tendsto f l (𝓝 C)) (hg : tendsto g l at_bot) :
tendsto (λ x, f x + g x) l at_bot :=
@filter.tendsto.add_at_top (order_dual α) _ _ _ _ _ _ _ _ hf hg
/-- In a linearly ordered additive commutative group with the order topology, if `f` tends to
`at_top` and `g` tends to `C` then `f + g` tends to `at_top`. -/
lemma filter.tendsto.at_top_add {C : α} (hf : tendsto f l at_top) (hg : tendsto g l (𝓝 C)) :
tendsto (λ x, f x + g x) l at_top :=
by { conv in (_ + _) { rw add_comm }, exact hg.add_at_top hf }
/-- In a linearly ordered additive commutative group with the order topology, if `f` tends to
`at_bot` and `g` tends to `C` then `f + g` tends to `at_bot`. -/
lemma filter.tendsto.at_bot_add {C : α} (hf : tendsto f l at_bot) (hg : tendsto g l (𝓝 C)) :
tendsto (λ x, f x + g x) l at_bot :=
by { conv in (_ + _) { rw add_comm }, exact hg.add_at_bot hf }
end linear_ordered_add_comm_group
section linear_ordered_field
variables [linear_ordered_field α] [topological_space α] [order_topology α]
variables {l : filter β} {f g : β → α}
section continuous_mul
lemma mul_tendsto_nhds_zero_right (x : α) :
tendsto (uncurry ((*) : α → α → α)) (𝓝 0 ×ᶠ 𝓝 x) $ 𝓝 0 :=
begin
have hx : 0 < 2 * (1 + abs x) := (mul_pos (zero_lt_two) $
lt_of_lt_of_le zero_lt_one $ le_add_of_le_of_nonneg le_rfl (abs_nonneg x)),
rw ((nhds_basis_zero_abs_sub_lt α).prod $ nhds_basis_abs_sub_lt x).tendsto_iff
(nhds_basis_zero_abs_sub_lt α),
refine λ ε ε_pos, ⟨(ε/(2 * (1 + abs x)), 1), ⟨div_pos ε_pos hx, zero_lt_one⟩, _⟩,
suffices : ∀ (a b : α), abs a < ε / (2 * (1 + abs x)) → abs (b - x) < 1 → (abs a) * (abs b) < ε,
by simpa only [and_imp, prod.forall, mem_prod, ← abs_mul],
intros a b h h',
refine lt_of_le_of_lt (mul_le_mul_of_nonneg_left _ (abs_nonneg a)) ((lt_div_iff hx).1 h),
calc abs b = abs ((b - x) + x) : by rw sub_add_cancel b x
... ≤ abs (b - x) + abs x : abs_add (b - x) x
... ≤ 1 + abs x : add_le_add_right (le_of_lt h') (abs x)
... ≤ 2 * (1 + abs x) : by linarith,
end
lemma mul_tendsto_nhds_zero_left (x : α) :
tendsto (uncurry ((*) : α → α → α)) (𝓝 x ×ᶠ 𝓝 0) $ 𝓝 0 :=
begin
intros s hs,
have := mul_tendsto_nhds_zero_right x hs,
rw [filter.mem_map, mem_prod_iff] at this ⊢,
obtain ⟨U, hU, V, hV, h⟩ := this,
exact ⟨V, hV, U, hU, λ y hy, ((mul_comm y.2 y.1) ▸
h (⟨hy.2, hy.1⟩ : (prod.mk y.2 y.1) ∈ (U.prod V)) : y.1 * y.2 ∈ s)⟩,
end
lemma nhds_eq_map_mul_left_nhds_one {x₀ : α} (hx₀ : x₀ ≠ 0) :
𝓝 x₀ = map (λ x, x₀*x) (𝓝 1) :=
begin
have hx₀' : 0 < abs x₀ := abs_pos.2 hx₀,
refine filter.ext (λ t, _),
simp only [exists_prop, set_of_subset_set_of, (nhds_basis_abs_sub_lt x₀).mem_iff,
(nhds_basis_abs_sub_lt (1 : α)).mem_iff, filter.mem_map'],
refine ⟨λ h, _, λ h, _⟩,
{ obtain ⟨i, hi, hit⟩ := h,
refine ⟨i / (abs x₀), div_pos hi (abs_pos.2 hx₀), λ x hx, hit _⟩,
calc abs (x₀ * x - x₀) = abs (x₀ * (x - 1)) : congr_arg abs (by ring_nf)
... = abs x₀ * abs (x - 1) : abs_mul x₀ (x - 1)
... < abs x₀ * (i / abs x₀) : mul_lt_mul' le_rfl hx (abs_nonneg (x - 1)) (abs_pos.2 hx₀)
... = abs x₀ * i / abs x₀ : by ring
... = i : mul_div_cancel_left i (λ h, hx₀ (abs_eq_zero.1 h)) },
{ obtain ⟨i, hi, hit⟩ := h,
refine ⟨i * (abs x₀), mul_pos hi (abs_pos.2 hx₀), λ x hx, _⟩,
have : abs (x / x₀ - 1) < i,
calc abs (x / x₀ - 1) = abs (x / x₀ - x₀ / x₀) : (by rw div_self hx₀)
... = abs ((x - x₀) / x₀) : congr_arg abs (sub_div x x₀ x₀).symm
... = abs (x - x₀) / abs x₀ : abs_div (x - x₀) x₀
... < i * abs x₀ / abs x₀ : div_lt_div hx le_rfl
(mul_nonneg (le_of_lt hi) (abs_nonneg x₀)) (abs_pos.2 hx₀)
... = i : by rw [← mul_div_assoc', div_self (ne_of_lt $ abs_pos.2 hx₀).symm, mul_one],
specialize hit (x / x₀) this,
rwa [mul_div_assoc', mul_div_cancel_left x hx₀] at hit }
end
lemma nhds_eq_map_mul_right_nhds_one {x₀ : α} (hx₀ : x₀ ≠ 0) :
𝓝 x₀ = map (λ x, x*x₀) (𝓝 1) :=
by simp_rw [mul_comm _ x₀, nhds_eq_map_mul_left_nhds_one hx₀]
lemma mul_tendsto_nhds_one_nhds_one :
tendsto (uncurry ((*) : α → α → α)) (𝓝 1 ×ᶠ 𝓝 1) $ 𝓝 1 :=
begin
rw ((nhds_basis_Ioo_pos (1 : α)).prod $ nhds_basis_Ioo_pos (1 : α)).tendsto_iff
(nhds_basis_Ioo_pos_of_pos (zero_lt_one : (0 : α) < 1)),
intros ε hε,
have hε' : 0 ≤ 1 - ε / 4 := by linarith,
have ε_pos : 0 < ε / 4 := by linarith,
have ε_pos' : 0 < ε / 2 := by linarith,
simp only [and_imp, prod.forall, mem_Ioo, function.uncurry_apply_pair, mem_prod, prod.exists],
refine ⟨ε/4, ε/4, ⟨ε_pos, ε_pos⟩, λ a b ha ha' hb hb', _⟩,
have ha0 : 0 ≤ a := le_trans hε' (le_of_lt ha),
have hb0 : 0 ≤ b := le_trans hε' (le_of_lt hb),
refine ⟨lt_of_le_of_lt _ (mul_lt_mul'' ha hb hε' hε'),
lt_of_lt_of_le (mul_lt_mul'' ha' hb' ha0 hb0) _⟩,
{ calc 1 - ε = 1 - ε / 2 - ε/2 : by ring_nf
... ≤ 1 - ε/2 - ε/2 + (ε/2)*(ε/2) : le_add_of_nonneg_right (le_of_lt (mul_pos ε_pos' ε_pos'))
... = (1 - ε/2) * (1 - ε/2) : by ring_nf
... ≤ (1 - ε/4) * (1 - ε/4) : mul_le_mul (by linarith) (by linarith) (by linarith) hε' },
{ calc (1 + ε/4) * (1 + ε/4) = 1 + ε/2 + (ε/4)*(ε/4) : by ring_nf
... = 1 + ε/2 + (ε * ε) / 16 : by ring_nf
... ≤ 1 + ε/2 + ε/2 : add_le_add_left (div_le_div (le_of_lt hε.1) (le_trans
((mul_le_mul_left hε.1).2 hε.2) (le_of_eq $ mul_one ε)) zero_lt_two (by linarith)) (1 + ε/2)
... ≤ 1 + ε : by ring_nf }
end
@[priority 100]
instance linear_ordered_field.has_continuous_mul : has_continuous_mul α :=
⟨begin
rw continuous_iff_continuous_at,
rintro ⟨x₀, y₀⟩,
by_cases hx₀ : x₀ = 0,
{ rw [hx₀, continuous_at, zero_mul, nhds_prod_eq],
exact mul_tendsto_nhds_zero_right y₀ },
by_cases hy₀ : y₀ = 0,
{ rw [hy₀, continuous_at, mul_zero, nhds_prod_eq],
exact mul_tendsto_nhds_zero_left x₀ },
have hxy : x₀ * y₀ ≠ 0 := mul_ne_zero hx₀ hy₀,
have key : (λ p : α × α, x₀ * p.1 * (p.2 * y₀)) = ((λ x, x₀*x) ∘ (λ x, x*y₀)) ∘ (uncurry (*)),
{ ext p, simp [uncurry, mul_assoc] },
have key₂ : (λ x, x₀*x) ∘ (λ x, y₀*x) = λ x, (x₀ *y₀)*x,
{ ext x, simp },
calc map (uncurry (*)) (𝓝 (x₀, y₀))
= map (uncurry (*)) (𝓝 x₀ ×ᶠ 𝓝 y₀) : by rw nhds_prod_eq
... = map (λ (p : α × α), x₀ * p.1 * (p.2 * y₀)) ((𝓝 1) ×ᶠ (𝓝 1))
: by rw [uncurry, nhds_eq_map_mul_left_nhds_one hx₀, nhds_eq_map_mul_right_nhds_one hy₀,
prod_map_map_eq, filter.map_map]
... = map ((λ x, x₀ * x) ∘ λ x, x * y₀) (map (uncurry (*)) (𝓝 1 ×ᶠ 𝓝 1))
: by rw [key, ← filter.map_map]
... ≤ map ((λ (x : α), x₀ * x) ∘ λ x, x * y₀) (𝓝 1) : map_mono (mul_tendsto_nhds_one_nhds_one)
... = 𝓝 (x₀*y₀) : by rw [← filter.map_map, ← nhds_eq_map_mul_right_nhds_one hy₀,
nhds_eq_map_mul_left_nhds_one hy₀, filter.map_map, key₂, ← nhds_eq_map_mul_left_nhds_one hxy],
end⟩
end continuous_mul
/-- In a linearly ordered field with the order topology, if `f` tends to `at_top` and `g` tends to
a positive constant `C` then `f * g` tends to `at_top`. -/
lemma filter.tendsto.at_top_mul {C : α} (hC : 0 < C) (hf : tendsto f l at_top)
(hg : tendsto g l (𝓝 C)) :
tendsto (λ x, (f x * g x)) l at_top :=
begin
refine tendsto_at_top_mono' _ _ (hf.at_top_mul_const (half_pos hC)),
filter_upwards [hg.eventually (lt_mem_nhds (half_lt_self hC)),
hf.eventually (eventually_ge_at_top 0)],
exact λ x hg hf, mul_le_mul_of_nonneg_left hg.le hf
end
/-- In a linearly ordered field with the order topology, if `f` tends to a positive constant `C` and
`g` tends to `at_top` then `f * g` tends to `at_top`. -/
lemma filter.tendsto.mul_at_top {C : α} (hC : 0 < C) (hf : tendsto f l (𝓝 C))
(hg : tendsto g l at_top) :
tendsto (λ x, (f x * g x)) l at_top :=
by simpa only [mul_comm] using hg.at_top_mul hC hf
/-- In a linearly ordered field with the order topology, if `f` tends to `at_top` and `g` tends to
a negative constant `C` then `f * g` tends to `at_bot`. -/
lemma filter.tendsto.at_top_mul_neg {C : α} (hC : C < 0) (hf : tendsto f l at_top)
(hg : tendsto g l (𝓝 C)) :
tendsto (λ x, (f x * g x)) l at_bot :=
by simpa only [(∘), neg_mul_eq_mul_neg, neg_neg]
using tendsto_neg_at_top_at_bot.comp (hf.at_top_mul (neg_pos.2 hC) hg.neg)
/-- In a linearly ordered field with the order topology, if `f` tends to a negative constant `C` and
`g` tends to `at_top` then `f * g` tends to `at_bot`. -/
lemma filter.tendsto.neg_mul_at_top {C : α} (hC : C < 0) (hf : tendsto f l (𝓝 C))
(hg : tendsto g l at_top) :
tendsto (λ x, (f x * g x)) l at_bot :=
by simpa only [mul_comm] using hg.at_top_mul_neg hC hf
/-- In a linearly ordered field with the order topology, if `f` tends to `at_bot` and `g` tends to
a positive constant `C` then `f * g` tends to `at_bot`. -/
lemma filter.tendsto.at_bot_mul {C : α} (hC : 0 < C) (hf : tendsto f l at_bot)
(hg : tendsto g l (𝓝 C)) :
tendsto (λ x, (f x * g x)) l at_bot :=
by simpa [(∘)]
using tendsto_neg_at_top_at_bot.comp ((tendsto_neg_at_bot_at_top.comp hf).at_top_mul hC hg)
/-- In a linearly ordered field with the order topology, if `f` tends to `at_bot` and `g` tends to
a negative constant `C` then `f * g` tends to `at_top`. -/
lemma filter.tendsto.at_bot_mul_neg {C : α} (hC : C < 0) (hf : tendsto f l at_bot)
(hg : tendsto g l (𝓝 C)) :
tendsto (λ x, (f x * g x)) l at_top :=
by simpa [(∘)]
using tendsto_neg_at_bot_at_top.comp ((tendsto_neg_at_bot_at_top.comp hf).at_top_mul_neg hC hg)
/-- In a linearly ordered field with the order topology, if `f` tends to a positive constant `C` and
`g` tends to `at_bot` then `f * g` tends to `at_bot`. -/
lemma filter.tendsto.mul_at_bot {C : α} (hC : 0 < C) (hf : tendsto f l (𝓝 C))
(hg : tendsto g l at_bot) :
tendsto (λ x, (f x * g x)) l at_bot :=
by simpa only [mul_comm] using hg.at_bot_mul hC hf
/-- In a linearly ordered field with the order topology, if `f` tends to a negative constant `C` and
`g` tends to `at_bot` then `f * g` tends to `at_top`. -/
lemma filter.tendsto.neg_mul_at_bot {C : α} (hC : C < 0) (hf : tendsto f l (𝓝 C))
(hg : tendsto g l at_bot) :
tendsto (λ x, (f x * g x)) l at_top :=
by simpa only [mul_comm] using hg.at_bot_mul_neg hC hf
/-- The function `x ↦ x⁻¹` tends to `+∞` on the right of `0`. -/
lemma tendsto_inv_zero_at_top : tendsto (λx:α, x⁻¹) (𝓝[set.Ioi (0:α)] 0) at_top :=
begin
refine (at_top_basis' 1).tendsto_right_iff.2 (λ b hb, _),
have hb' : 0 < b := zero_lt_one.trans_le hb,
filter_upwards [Ioc_mem_nhds_within_Ioi ⟨le_rfl, inv_pos.2 hb'⟩],
exact λ x hx, (le_inv hx.1 hb').1 hx.2
end
/-- The function `r ↦ r⁻¹` tends to `0` on the right as `r → +∞`. -/
lemma tendsto_inv_at_top_zero' : tendsto (λr:α, r⁻¹) at_top (𝓝[set.Ioi (0:α)] 0) :=
begin
refine (has_basis.tendsto_iff at_top_basis ⟨λ s, mem_nhds_within_Ioi_iff_exists_Ioc_subset⟩).2 _,
refine λ b hb, ⟨b⁻¹, trivial, λ x hx, _⟩,
have : 0 < x := lt_of_lt_of_le (inv_pos.2 hb) hx,
exact ⟨inv_pos.2 this, (inv_le this hb).2 hx⟩
end
lemma tendsto_inv_at_top_zero : tendsto (λr:α, r⁻¹) at_top (𝓝 0) :=
tendsto_inv_at_top_zero'.mono_right inf_le_left
lemma filter.tendsto.div_at_top [has_continuous_mul α] {f g : β → α} {l : filter β} {a : α}
(h : tendsto f l (𝓝 a)) (hg : tendsto g l at_top) : tendsto (λ x, f x / g x) l (𝓝 0) :=
by { simp only [div_eq_mul_inv], exact mul_zero a ▸ h.mul (tendsto_inv_at_top_zero.comp hg) }
lemma filter.tendsto.inv_tendsto_at_top (h : tendsto f l at_top) : tendsto (f⁻¹) l (𝓝 0) :=
tendsto_inv_at_top_zero.comp h
lemma filter.tendsto.inv_tendsto_zero (h : tendsto f l (𝓝[set.Ioi 0] 0)) :
tendsto (f⁻¹) l at_top :=
tendsto_inv_zero_at_top.comp h
/-- The function `x^(-n)` tends to `0` at `+∞` for any positive natural `n`.
A version for positive real powers exists as `tendsto_rpow_neg_at_top`. -/
lemma tendsto_pow_neg_at_top {n : ℕ} (hn : 1 ≤ n) : tendsto (λ x : α, x ^ (-(n:ℤ))) at_top (𝓝 0) :=
tendsto.congr (λ x, (fpow_neg x n).symm)
(filter.tendsto.inv_tendsto_at_top (by simpa [gpow_coe_nat] using tendsto_pow_at_top hn))
lemma tendsto_fpow_at_top_zero {n : ℤ} (hn : n < 0) :
tendsto (λ x : α, x^n) at_top (𝓝 0) :=
begin
have : 1 ≤ -n := le_neg.mp (int.le_of_lt_add_one (hn.trans_le (neg_add_self 1).symm.le)),
apply tendsto.congr (show ∀ x : α, x^-(-n) = x^n, by simp),
lift -n to ℕ using le_of_lt (neg_pos.mpr hn) with N,
exact tendsto_pow_neg_at_top (by exact_mod_cast this)
end
lemma tendsto_const_mul_fpow_at_top_zero {n : ℤ} {c : α} (hn : n < 0) :
tendsto (λ x, c * x ^ n) at_top (𝓝 0) :=
(mul_zero c) ▸ (filter.tendsto.const_mul c (tendsto_fpow_at_top_zero hn))
lemma tendsto_const_mul_pow_nhds_iff {n : ℕ} {c d : α} (hc : c ≠ 0) :
tendsto (λ x : α, c * x ^ n) at_top (𝓝 d) ↔ n = 0 ∧ c = d :=
begin
refine ⟨λ h, _, λ h, _⟩,
{ have hn : n = 0,
{ by_contradiction hn,
have hn : 1 ≤ n := nat.succ_le_iff.2 (lt_of_le_of_ne (zero_le _) (ne.symm hn)),
by_cases hc' : 0 < c,
{ have := (tendsto_const_mul_pow_at_top_iff c n).2 ⟨hn, hc'⟩,
exact not_tendsto_nhds_of_tendsto_at_top this d h },
{ have := (tendsto_neg_const_mul_pow_at_top_iff c n).2 ⟨hn, lt_of_le_of_ne (not_lt.1 hc') hc⟩,
exact not_tendsto_nhds_of_tendsto_at_bot this d h } },
have : (λ x : α, c * x ^ n) = (λ x : α, c), by simp [hn],
rw [this, tendsto_const_nhds_iff] at h,
exact ⟨hn, h⟩ },
{ obtain ⟨hn, hcd⟩ := h,
simpa [hn, hcd] using tendsto_const_nhds }
end
lemma tendsto_const_mul_fpow_at_top_zero_iff {n : ℤ} {c d : α} (hc : c ≠ 0) :
tendsto (λ x : α, c * x ^ n) at_top (𝓝 d) ↔
(n = 0 ∧ c = d) ∨ (n < 0 ∧ d = 0) :=
begin
refine ⟨λ h, _, λ h, _⟩,
{ by_cases hn : 0 ≤ n,
{ lift n to ℕ using hn,
simp only [gpow_coe_nat] at h,
rw [tendsto_const_mul_pow_nhds_iff hc, ← int.coe_nat_eq_zero] at h,
exact or.inl h },
{ rw not_le at hn,
refine or.inr ⟨hn, tendsto_nhds_unique h (tendsto_const_mul_fpow_at_top_zero hn)⟩ } },
{ cases h,
{ simp only [h.left, h.right, gpow_zero, mul_one],
exact tendsto_const_nhds },
{ exact h.2.symm ▸ tendsto_const_mul_fpow_at_top_zero h.1} }
end
end linear_ordered_field
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 order_topology
variables [topological_space α] [topological_space β]
[linear_order α] [linear_order β] [order_topology α] [order_topology β]
lemma is_lub.frequently_mem {a : α} {s : set α} (ha : is_lub s a) (hs : s.nonempty) :
∃ᶠ x in 𝓝[Iic a] a, x ∈ s :=
begin
rcases hs with ⟨a', ha'⟩,
intro h,
rcases (ha.1 ha').eq_or_lt with (rfl|ha'a),
{ exact h.self_of_nhds_within le_rfl ha' },
{ rcases (mem_nhds_within_Iic_iff_exists_Ioc_subset' ha'a).1 h
with ⟨b, hba, hb⟩,
rcases ha.exists_between hba with ⟨b', hb's, hb'⟩,
exact hb hb' hb's },
end
lemma is_lub.frequently_nhds_mem {a : α} {s : set α} (ha : is_lub s a) (hs : s.nonempty) :
∃ᶠ x in 𝓝 a, x ∈ s :=
(ha.frequently_mem hs).filter_mono inf_le_left
lemma is_glb.frequently_mem {a : α} {s : set α} (ha : is_glb s a) (hs : s.nonempty) :
∃ᶠ x in 𝓝[Ici a] a, x ∈ s :=
@is_lub.frequently_mem (order_dual α) _ _ _ _ _ ha hs
lemma is_glb.frequently_nhds_mem {a : α} {s : set α} (ha : is_glb s a) (hs : s.nonempty) :
∃ᶠ x in 𝓝 a, x ∈ s :=
(ha.frequently_mem hs).filter_mono inf_le_left
lemma is_lub.mem_closure {a : α} {s : set α} (ha : is_lub s a) (hs : s.nonempty) :
a ∈ closure s :=
(ha.frequently_nhds_mem hs).mem_closure
lemma is_glb.mem_closure {a : α} {s : set α} (ha : is_glb s a) (hs : s.nonempty) :
a ∈ closure s :=
(ha.frequently_nhds_mem hs).mem_closure
lemma is_lub.nhds_within_ne_bot {a : α} {s : set α} (ha : is_lub s a) (hs : s.nonempty) :
ne_bot (𝓝[s] a) :=
mem_closure_iff_nhds_within_ne_bot.1 (ha.mem_closure hs)
lemma is_glb.nhds_within_ne_bot : ∀ {a : α} {s : set α}, is_glb s a → s.nonempty →
ne_bot (𝓝[s] a) :=
@is_lub.nhds_within_ne_bot (order_dual α) _ _ _
lemma is_lub_of_mem_nhds {s : set α} {a : α} {f : filter α}
(hsa : a ∈ upper_bounds s) (hsf : s ∈ f) [ne_bot (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 hsf (is_open.mem_nhds (is_open_lt' _) hba),
let ⟨x, ⟨hxs, hxb⟩⟩ := filter.nonempty_of_mem this in
have b < b, from lt_of_lt_of_le hxb $ hb hxs,
lt_irrefl b this⟩
lemma is_lub_of_mem_closure {s : set α} {a : α} (hsa : a ∈ upper_bounds s) (hsf : a ∈ closure s) :
is_lub s a :=
begin
rw [mem_closure_iff_cluster_pt, cluster_pt, inf_comm] at hsf,
haveI : (𝓟 s ⊓ 𝓝 a).ne_bot := hsf,
exact is_lub_of_mem_nhds hsa (mem_principal_self s),
end
lemma is_glb_of_mem_nhds : ∀ {s : set α} {a : α} {f : filter α},
a ∈ lower_bounds s → s ∈ f → ne_bot (f ⊓ 𝓝 a) → is_glb s a :=
@is_lub_of_mem_nhds (order_dual α) _ _ _
lemma is_glb_of_mem_closure {s : set α} {a : α} (hsa : a ∈ lower_bounds s) (hsf : a ∈ closure s) :
is_glb s a :=
@is_lub_of_mem_closure (order_dual α) _ _ _ s a hsa hsf
lemma is_lub.mem_upper_bounds_of_tendsto [preorder γ] [topological_space γ]
[order_closed_topology γ] {f : α → γ} {s : set α} {a : α} {b : γ}
(hf : ∀x∈s, ∀y∈s, x ≤ y → f x ≤ f y) (ha : is_lub s a)
(hb : tendsto f (𝓝[s] a) (𝓝 b)) : b ∈ upper_bounds (f '' s) :=
begin
rintro _ ⟨x, hx, rfl⟩,
replace ha := ha.inter_Ici_of_mem hx,
haveI := ha.nhds_within_ne_bot ⟨x, hx, le_rfl⟩,
refine ge_of_tendsto (hb.mono_left (nhds_within_mono _ (inter_subset_left s (Ici x)))) _,
exact mem_of_superset self_mem_nhds_within (λ y hy, hf _ hx _ hy.1 hy.2)
end
-- For a version of this theorem in which the convergence considered on the domain `α` is as
-- `x : α` tends to infinity, rather than tending to a point `x` in `α`, see `is_lub_of_tendsto`,
-- below
lemma is_lub.is_lub_of_tendsto [preorder γ] [topological_space γ]
[order_closed_topology γ] {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 (𝓝[s] a) (𝓝 b)) : is_lub (f '' s) b :=
begin
haveI := ha.nhds_within_ne_bot hs,
exact ⟨ha.mem_upper_bounds_of_tendsto hf hb, λ b' hb', le_of_tendsto hb
(mem_of_superset self_mem_nhds_within $ λ x hx, hb' $ mem_image_of_mem _ hx)⟩
end
lemma is_glb.mem_lower_bounds_of_tendsto [preorder γ] [topological_space γ]
[order_closed_topology γ] {f : α → γ} {s : set α} {a : α} {b : γ}
(hf : ∀x∈s, ∀y∈s, x ≤ y → f x ≤ f y) (ha : is_glb s a)
(hb : tendsto f (𝓝[s] a) (𝓝 b)) : b ∈ lower_bounds (f '' s) :=
@is_lub.mem_upper_bounds_of_tendsto (order_dual α) (order_dual γ) _ _ _ _ _ _ _ _ _ _
(λ x hx y hy, hf y hy x hx) ha hb
-- For a version of this theorem in which the convergence considered on the domain `α` is as
-- `x : α` tends to negative infinity, rather than tending to a point `x` in `α`, see
-- `is_glb_of_tendsto`, below
lemma is_glb.is_glb_of_tendsto [preorder γ] [topological_space γ]
[order_closed_topology γ] {f : α → γ} {s : set α} {a : α} {b : γ}
(hf : ∀x∈s, ∀y∈s, x ≤ y → f x ≤ f y) : is_glb s a → s.nonempty →
tendsto f (𝓝[s] a) (𝓝 b) → is_glb (f '' s) b :=
@is_lub.is_lub_of_tendsto (order_dual α) (order_dual γ) _ _ _ _ _ _ f s a b
(λ x hx y hy, hf y hy x hx)
lemma is_lub.mem_lower_bounds_of_tendsto [preorder γ] [topological_space γ]
[order_closed_topology γ] {f : α → γ} {s : set α} {a : α} {b : γ}
(hf : ∀x∈s, ∀y∈s, x ≤ y → f y ≤ f x) (ha : is_lub s a)
(hb : tendsto f (𝓝[s] a) (𝓝 b)) : b ∈ lower_bounds (f '' s) :=
@is_lub.mem_upper_bounds_of_tendsto α (order_dual γ) _ _ _ _ _ _ _ _ _ _ hf ha hb
lemma is_lub.is_glb_of_tendsto [preorder γ] [topological_space γ]
[order_closed_topology γ] : ∀ {f : α → γ} {s : set α} {a : α} {b : γ},
(∀x∈s, ∀y∈s, x ≤ y → f y ≤ f x) → is_lub s a → s.nonempty →
tendsto f (𝓝[s] a) (𝓝 b) → is_glb (f '' s) b :=
@is_lub.is_lub_of_tendsto α (order_dual γ) _ _ _ _ _ _
lemma is_glb.mem_upper_bounds_of_tendsto [preorder γ] [topological_space γ]
[order_closed_topology γ] {f : α → γ} {s : set α} {a : α} {b : γ}
(hf : ∀x∈s, ∀y∈s, x ≤ y → f y ≤ f x) (ha : is_glb s a)
(hb : tendsto f (𝓝[s] a) (𝓝 b)) : b ∈ upper_bounds (f '' s) :=
@is_glb.mem_lower_bounds_of_tendsto α (order_dual γ) _ _ _ _ _ _ _ _ _ _ hf ha hb
lemma is_glb.is_lub_of_tendsto [preorder γ] [topological_space γ]
[order_closed_topology γ] : ∀ {f : α → γ} {s : set α} {a : α} {b : γ},
(∀x∈s, ∀y∈s, x ≤ y → f y ≤ f x) → is_glb s a → s.nonempty →
tendsto f (𝓝[s] a) (𝓝 b) → is_lub (f '' s) b :=
@is_glb.is_glb_of_tendsto α (order_dual γ) _ _ _ _ _ _
lemma is_lub.mem_of_is_closed {a : α} {s : set α} (ha : is_lub s a) (hs : s.nonempty)
(sc : is_closed s) : a ∈ s :=
sc.closure_subset $ ha.mem_closure hs
alias is_lub.mem_of_is_closed ← is_closed.is_lub_mem
lemma is_glb.mem_of_is_closed {a : α} {s : set α} (ha : is_glb s a) (hs : s.nonempty)
(sc : is_closed s) : a ∈ s :=
sc.closure_subset $ ha.mem_closure hs
alias is_glb.mem_of_is_closed ← is_closed.is_glb_mem
/-!
### Existence of sequences tending to Inf or Sup of a given set
-/
lemma is_lub.exists_seq_strict_mono_tendsto_of_not_mem' {t : set α} {x : α}
(htx : is_lub t x) (not_mem : x ∉ t) (ht : t.nonempty) (hx : is_countably_generated (𝓝 x)) :
∃ u : ℕ → α, strict_mono u ∧ (∀ n, u n < x) ∧ tendsto u at_top (𝓝 x) ∧ (∀ n, u n ∈ t) :=
begin
rcases ht with ⟨l, hl⟩,
have hl : l < x,
{ rcases lt_or_eq_of_le (htx.1 hl) with h|rfl,
{ exact h },
{ exact (not_mem hl).elim } },
obtain ⟨s, hs⟩ : ∃ s : ℕ → set α, (𝓝 x).has_basis (λ (_x : ℕ), true) s :=
let ⟨s, hs⟩ := hx.exists_antimono_basis in ⟨s, hs.to_has_basis⟩,
have : ∀ n k, k < x → ∃ y, Icc y x ⊆ s n ∧ k < y ∧ y < x ∧ y ∈ t,
{ assume n k hk,
obtain ⟨L, hL, h⟩ : ∃ (L : α) (hL : L ∈ Ico k x), Ioc L x ⊆ s n :=
exists_Ioc_subset_of_mem_nhds' (hs.mem_of_mem trivial) hk,
obtain ⟨y, hy⟩ : ∃ (y : α), L < y ∧ y < x ∧ y ∈ t,
{ rcases htx.exists_between' not_mem hL.2 with ⟨y, yt, hy⟩,
refine ⟨y, hy.1, hy.2, yt⟩ },
exact ⟨y, λ z hz, h ⟨hy.1.trans_le hz.1, hz.2⟩, hL.1.trans_lt hy.1, hy.2⟩ },
choose! f hf using this,
let u : ℕ → α := λ n, nat.rec_on n (f 0 l) (λ n h, f n.succ h),
have I : ∀ n, u n < x,
{ assume n,
induction n with n IH,
{ exact (hf 0 l hl).2.2.1 },
{ exact (hf n.succ _ IH).2.2.1 } },
have S : strict_mono u := strict_mono_nat_of_lt_succ (λ n, (hf n.succ _ (I n)).2.1),
refine ⟨u, S, I, hs.tendsto_right_iff.2 (λ n _, _), (λ n, _)⟩,
{ simp only [ge_iff_le, eventually_at_top],
refine ⟨n, λ p hp, _⟩,
have up : u p ∈ Icc (u n) x := ⟨S.monotone hp, (I p).le⟩,
have : Icc (u n) x ⊆ s n,
by { cases n, { exact (hf 0 l hl).1 }, { exact (hf n.succ (u n) (I n)).1 } },
exact this up },
{ cases n,
{ exact (hf 0 l hl).2.2.2 },
{ exact (hf n.succ _ (I n)).2.2.2 } }
end
lemma is_lub.exists_seq_monotone_tendsto' {t : set α} {x : α}
(htx : is_lub t x) (ht : t.nonempty) (hx : is_countably_generated (𝓝 x)) :
∃ u : ℕ → α, monotone u ∧ (∀ n, u n ≤ x) ∧ tendsto u at_top (𝓝 x) ∧ (∀ n, u n ∈ t) :=
begin
by_cases h : x ∈ t,
{ exact ⟨λ n, x, monotone_const, λ n, le_rfl, tendsto_const_nhds, λ n, h⟩ },
{ rcases htx.exists_seq_strict_mono_tendsto_of_not_mem' h ht hx with ⟨u, hu⟩,
exact ⟨u, hu.1.monotone, λ n, (hu.2.1 n).le, hu.2.2⟩ }
end
lemma is_lub.exists_seq_strict_mono_tendsto_of_not_mem [first_countable_topology α]
{t : set α} {x : α} (htx : is_lub t x) (ht : t.nonempty) (not_mem : x ∉ t) :
∃ u : ℕ → α, strict_mono u ∧ (∀ n, u n < x) ∧ tendsto u at_top (𝓝 x) ∧ (∀ n, u n ∈ t) :=
htx.exists_seq_strict_mono_tendsto_of_not_mem' not_mem ht (is_countably_generated_nhds x)
lemma is_lub.exists_seq_monotone_tendsto [first_countable_topology α]
{t : set α} {x : α} (htx : is_lub t x) (ht : t.nonempty) :
∃ u : ℕ → α, monotone u ∧ (∀ n, u n ≤ x) ∧ tendsto u at_top (𝓝 x) ∧ (∀ n, u n ∈ t) :=
htx.exists_seq_monotone_tendsto' ht (is_countably_generated_nhds x)
lemma exists_seq_strict_mono_tendsto' {α : Type*} [linear_order α] [topological_space α]
[densely_ordered α] [order_topology α]
[first_countable_topology α] {x y : α} (hy : y < x) :
∃ u : ℕ → α, strict_mono u ∧ (∀ n, u n < x) ∧ tendsto u at_top (𝓝 x) :=
begin
have hx : x ∉ Iio x := λ h, (lt_irrefl x h).elim,
have ht : set.nonempty (Iio x) := ⟨y, hy⟩,
rcases is_lub_Iio.exists_seq_strict_mono_tendsto_of_not_mem ht hx with ⟨u, hu⟩,
exact ⟨u, hu.1, hu.2.1, hu.2.2.1⟩,
end
lemma exists_seq_strict_mono_tendsto [densely_ordered α] [no_bot_order α]
[first_countable_topology α] (x : α) :
∃ u : ℕ → α, strict_mono u ∧ (∀ n, u n < x) ∧ tendsto u at_top (𝓝 x) :=
begin
obtain ⟨y, hy⟩ : ∃ y, y < x := no_bot _,
exact exists_seq_strict_mono_tendsto' hy
end
lemma exists_seq_tendsto_Sup {α : Type*} [conditionally_complete_linear_order α]
[topological_space α] [order_topology α] [first_countable_topology α]
{S : set α} (hS : S.nonempty) (hS' : bdd_above S) :
∃ (u : ℕ → α), monotone u ∧ tendsto u at_top (𝓝 (Sup S)) ∧ (∀ n, u n ∈ S) :=
begin
rcases (is_lub_cSup hS hS').exists_seq_monotone_tendsto hS with ⟨u, hu⟩,
exact ⟨u, hu.1, hu.2.2⟩,
end
lemma is_glb.exists_seq_strict_mono_tendsto_of_not_mem' {t : set α} {x : α}
(htx : is_glb t x) (not_mem : x ∉ t) (ht : t.nonempty) (hx : is_countably_generated (𝓝 x)) :
∃ u : ℕ → α, (∀ m n, m < n → u n < u m) ∧ (∀ n, x < u n) ∧
tendsto u at_top (𝓝 x) ∧ (∀ n, u n ∈ t) :=
@is_lub.exists_seq_strict_mono_tendsto_of_not_mem' (order_dual α) _ _ _ t x htx not_mem ht hx
lemma is_glb.exists_seq_monotone_tendsto' {t : set α} {x : α}
(htx : is_glb t x) (ht : t.nonempty) (hx : is_countably_generated (𝓝 x)) :
∃ u : ℕ → α, (∀ m n, m ≤ n → u n ≤ u m) ∧ (∀ n, x ≤ u n) ∧
tendsto u at_top (𝓝 x) ∧ (∀ n, u n ∈ t) :=
@is_lub.exists_seq_monotone_tendsto' (order_dual α) _ _ _ t x htx ht hx
lemma is_glb.exists_seq_strict_mono_tendsto_of_not_mem [first_countable_topology α]
{t : set α} {x : α} (htx : is_glb t x) (ht : t.nonempty) (not_mem : x ∉ t) :
∃ u : ℕ → α, (∀ m n, m < n → u n < u m) ∧ (∀ n, x < u n) ∧
tendsto u at_top (𝓝 x) ∧ (∀ n, u n ∈ t) :=
htx.exists_seq_strict_mono_tendsto_of_not_mem' not_mem ht (is_countably_generated_nhds x)
lemma is_glb.exists_seq_monotone_tendsto [first_countable_topology α]
{t : set α} {x : α} (htx : is_glb t x) (ht : t.nonempty) :
∃ u : ℕ → α, (∀ m n, m ≤ n → u n ≤ u m) ∧ (∀ n, x ≤ u n) ∧
tendsto u at_top (𝓝 x) ∧ (∀ n, u n ∈ t) :=
htx.exists_seq_monotone_tendsto' ht (is_countably_generated_nhds x)
lemma exists_seq_strict_antimono_tendsto' [densely_ordered α]
[first_countable_topology α] {x y : α} (hy : x < y) :
∃ u : ℕ → α, (∀ m n, m < n → u n < u m) ∧ (∀ n, x < u n) ∧ tendsto u at_top (𝓝 x) :=
@exists_seq_strict_mono_tendsto' (order_dual α) _ _ _ _ _ x y hy
lemma exists_seq_strict_antimono_tendsto [densely_ordered α] [no_top_order α]
[first_countable_topology α] (x : α) :
∃ u : ℕ → α, (∀ m n, m < n → u n < u m) ∧ (∀ n, x < u n) ∧ tendsto u at_top (𝓝 x) :=
@exists_seq_strict_mono_tendsto (order_dual α) _ _ _ _ _ _ x
lemma exists_seq_tendsto_Inf {α : Type*} [conditionally_complete_linear_order α]
[topological_space α] [order_topology α] [first_countable_topology α]
{S : set α} (hS : S.nonempty) (hS' : bdd_below S) :
∃ (u : ℕ → α), (∀ m n, m ≤ n → u n ≤ u m) ∧ tendsto u at_top (𝓝 (Inf S)) ∧ (∀ n, u n ∈ S) :=
@exists_seq_tendsto_Sup (order_dual α) _ _ _ _ S hS hS'
/-- A compact set is bounded below -/
lemma is_compact.bdd_below {α : Type u} [topological_space α] [linear_order α]
[order_closed_topology α] [nonempty α] {s : set α} (hs : is_compact s) : bdd_below s :=
begin
by_contra H,
rcases hs.elim_finite_subcover_image (λ x (_ : x ∈ s), @is_open_Ioi _ _ _ _ x) _
with ⟨t, st, ft, ht⟩,
{ refine H (ft.bdd_below.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 is_compact.bdd_above {α : Type u} [topological_space α] [linear_order α]
[order_topology α] : Π [nonempty α] {s : set α}, is_compact s → bdd_above s :=
@is_compact.bdd_below (order_dual α) _ _ _
end order_topology
section densely_ordered
variables [topological_space α] [linear_order α] [order_topology α] [densely_ordered α]
{a b : α} {s : set α}
/-- 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 },
{ rw [← diff_subset_closure_iff, Ici_diff_Ioi_same, singleton_subset_iff],
exact is_glb_Ioi.mem_closure ⟨_, hab⟩ }
end
/-- The closure of the interval `(a, +∞)` is the closed interval `[a, +∞)`. -/
@[simp] 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 :=
@closure_Ioi' (order_dual α) _ _ _ _ _ _ hab
/-- The closure of the interval `(-∞, a)` is the interval `(-∞, a]`. -/
@[simp] 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]`. -/
@[simp] 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 },
{ rw [← diff_subset_closure_iff, Icc_diff_Ioo_same hab.le],
have hab' : (Ioo a b).nonempty, from nonempty_Ioo.2 hab,
simp only [insert_subset, singleton_subset_iff],
exact ⟨(is_glb_Ioo hab).mem_closure hab', (is_lub_Ioo hab).mem_closure hab'⟩ }
end
/-- The closure of the interval `(a, b]` is the closed interval `[a, b]`. -/
@[simp] 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]`. -/
@[simp] 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]
@[simp] lemma frontier_Ici [no_bot_order α] {a : α} : frontier (Ici a) = {a} :=
by simp [frontier]
@[simp] lemma frontier_Iic [no_top_order α] {a : α} : frontier (Iic a) = {a} :=
by simp [frontier]
@[simp] lemma frontier_Ioi [no_top_order α] {a : α} : frontier (Ioi a) = {a} :=
by simp [frontier]
@[simp] lemma frontier_Iio [no_bot_order α] {a : α} : frontier (Iio a) = {a} :=
by simp [frontier]
@[simp] lemma frontier_Icc [no_bot_order α] [no_top_order α] {a b : α} (h : a < b) :
frontier (Icc a b) = {a, b} :=
by simp [frontier, le_of_lt h, Icc_diff_Ioo_same]
@[simp] lemma frontier_Ioo {a b : α} (h : a < b) : frontier (Ioo a b) = {a, b} :=
by simp [frontier, h, le_of_lt h, Icc_diff_Ioo_same]
@[simp] lemma frontier_Ico [no_bot_order α] {a b : α} (h : a < b) : frontier (Ico a b) = {a, b} :=
by simp [frontier, h, le_of_lt h, Icc_diff_Ioo_same]
@[simp] lemma frontier_Ioc [no_top_order α] {a b : α} (h : a < b) : frontier (Ioc a b) = {a, b} :=
by simp [frontier, h, le_of_lt h, Icc_diff_Ioo_same]
lemma nhds_within_Ioi_ne_bot' {a b c : α} (H₁ : a < c) (H₂ : a ≤ b) :
ne_bot (𝓝[Ioi a] b) :=
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) :
ne_bot (𝓝[Ioi a] b) :=
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) :
ne_bot (𝓝[Ioi a] a) :=
nhds_within_Ioi_ne_bot' H (le_refl a)
@[instance]
lemma nhds_within_Ioi_self_ne_bot [no_top_order α] (a : α) :
ne_bot (𝓝[Ioi a] a) :=
nhds_within_Ioi_ne_bot (le_refl a)
lemma nhds_within_Iio_ne_bot' {a b c : α} (H₁ : a < c) (H₂ : b ≤ c) :
ne_bot (𝓝[Iio c] b) :=
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) :
ne_bot (𝓝[Iio b] a) :=
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) :
ne_bot (𝓝[Iio b] b) :=
nhds_within_Iio_ne_bot' H (le_refl b)
@[instance]
lemma nhds_within_Iio_self_ne_bot [no_bot_order α] (a : α) :
ne_bot (𝓝[Iio a] a) :=
nhds_within_Iio_ne_bot (le_refl a)
lemma right_nhds_within_Ico_ne_bot {a b : α} (H : a < b) : ne_bot (𝓝[Ico a b] b) :=
(is_lub_Ico H).nhds_within_ne_bot (nonempty_Ico.2 H)
lemma left_nhds_within_Ioc_ne_bot {a b : α} (H : a < b) : ne_bot (𝓝[Ioc a b] a) :=
(is_glb_Ioc H).nhds_within_ne_bot (nonempty_Ioc.2 H)
lemma left_nhds_within_Ioo_ne_bot {a b : α} (H : a < b) : ne_bot (𝓝[Ioo a b] a) :=
(is_glb_Ioo H).nhds_within_ne_bot (nonempty_Ioo.2 H)
lemma right_nhds_within_Ioo_ne_bot {a b : α} (H : a < b) : ne_bot (𝓝[Ioo a b] b) :=
(is_lub_Ioo H).nhds_within_ne_bot (nonempty_Ioo.2 H)
lemma comap_coe_nhds_within_Iio_of_Ioo_subset (hb : s ⊆ Iio b)
(hs : s.nonempty → ∃ a < b, Ioo a b ⊆ s) :
comap (coe : s → α) (𝓝[Iio b] b) = at_top :=
begin
nontriviality,
haveI : nonempty s := nontrivial_iff_nonempty.1 ‹_›,
rcases hs (nonempty_subtype.1 ‹_›) with ⟨a, h, hs⟩,
ext u, split,
{ rintros ⟨t, ht, hts⟩,
obtain ⟨x, ⟨hxa : a ≤ x, hxb : x < b⟩, hxt : Ioo x b ⊆ t⟩ :=
(mem_nhds_within_Iio_iff_exists_mem_Ico_Ioo_subset h).mp ht,
obtain ⟨y, hxy, hyb⟩ := exists_between hxb,
refine mem_of_superset (mem_at_top ⟨y, hs ⟨hxa.trans_lt hxy, hyb⟩⟩) _,
rintros ⟨z, hzs⟩ (hyz : y ≤ z),
refine hts (hxt ⟨hxy.trans_le _, hb _⟩); assumption },
{ intros hu,
obtain ⟨x : s, hx : ∀ z, x ≤ z → z ∈ u⟩ := mem_at_top_sets.1 hu,
exact ⟨Ioo x b, Ioo_mem_nhds_within_Iio (right_mem_Ioc.2 $ hb x.2), λ z hz, hx _ hz.1.le⟩ }
end
lemma comap_coe_nhds_within_Ioi_of_Ioo_subset (ha : s ⊆ Ioi a)
(hs : s.nonempty → ∃ b > a, Ioo a b ⊆ s) :
comap (coe : s → α) (𝓝[Ioi a] a) = at_bot :=
begin
refine @comap_coe_nhds_within_Iio_of_Ioo_subset (order_dual α) _ _ _ _ _ _ ha (λ h, _),
rcases hs h with ⟨b, hab, h⟩,
use [b, hab],
rwa dual_Ioo
end
lemma map_coe_at_top_of_Ioo_subset (hb : s ⊆ Iio b)
(hs : ∀ a' < b, ∃ a < b, Ioo a b ⊆ s) :
map (coe : s → α) at_top = 𝓝[Iio b] b :=
begin
rcases eq_empty_or_nonempty (Iio b) with (hb'|⟨a, ha⟩),
{ rw [filter_eq_bot_of_is_empty at_top, map_bot, hb', nhds_within_empty],
exact ⟨λ x, hb'.subset (hb x.2)⟩ },
{ rw [← comap_coe_nhds_within_Iio_of_Ioo_subset hb (λ _, hs a ha), map_comap_of_mem],
rw subtype.range_coe,
exact (mem_nhds_within_Iio_iff_exists_Ioo_subset' ha).2 (hs a ha) },
end
lemma map_coe_at_bot_of_Ioo_subset (ha : s ⊆ Ioi a)
(hs : ∀ b' > a, ∃ b > a, Ioo a b ⊆ s) :
map (coe : s → α) at_bot = (𝓝[Ioi a] a) :=
begin
refine @map_coe_at_top_of_Ioo_subset (order_dual α) _ _ _ _ a s ha (λ b' hb', _),
rcases hs b' hb' with ⟨b, hab, hbs⟩,
use [b, hab],
rwa dual_Ioo
end
/-- The `at_top` filter for an open interval `Ioo a b` comes from the left-neighbourhoods filter at
the right endpoint in the ambient order. -/
lemma comap_coe_Ioo_nhds_within_Iio (a b : α) :
comap (coe : Ioo a b → α) (𝓝[Iio b] b) = at_top :=
comap_coe_nhds_within_Iio_of_Ioo_subset Ioo_subset_Iio_self $
λ h, ⟨a, nonempty_Ioo.1 h, subset.refl _⟩
/-- The `at_bot` filter for an open interval `Ioo a b` comes from the right-neighbourhoods filter at
the left endpoint in the ambient order. -/
lemma comap_coe_Ioo_nhds_within_Ioi (a b : α) :
comap (coe : Ioo a b → α) (𝓝[Ioi a] a) = at_bot :=
comap_coe_nhds_within_Ioi_of_Ioo_subset Ioo_subset_Ioi_self $
λ h, ⟨b, nonempty_Ioo.1 h, subset.refl _⟩
lemma comap_coe_Ioi_nhds_within_Ioi (a : α) : comap (coe : Ioi a → α) (𝓝[Ioi a] a) = at_bot :=
comap_coe_nhds_within_Ioi_of_Ioo_subset (subset.refl _) $
λ ⟨x, hx⟩, ⟨x, hx, Ioo_subset_Ioi_self⟩
lemma comap_coe_Iio_nhds_within_Iio (a : α) :
comap (coe : Iio a → α) (𝓝[Iio a] a) = at_top :=
@comap_coe_Ioi_nhds_within_Ioi (order_dual α) _ _ _ _ a
@[simp] lemma map_coe_Ioo_at_top {a b : α} (h : a < b) :
map (coe : Ioo a b → α) at_top = 𝓝[Iio b] b :=
map_coe_at_top_of_Ioo_subset Ioo_subset_Iio_self $ λ _ _, ⟨_, h, subset.refl _⟩
@[simp] lemma map_coe_Ioo_at_bot {a b : α} (h : a < b) :
map (coe : Ioo a b → α) at_bot = 𝓝[Ioi a] a :=
map_coe_at_bot_of_Ioo_subset Ioo_subset_Ioi_self $ λ _ _, ⟨_, h, subset.refl _⟩
@[simp] lemma map_coe_Ioi_at_bot (a : α) :
map (coe : Ioi a → α) at_bot = 𝓝[Ioi a] a :=
map_coe_at_bot_of_Ioo_subset (subset.refl _) $ λ b hb, ⟨b, hb, Ioo_subset_Ioi_self⟩
@[simp] lemma map_coe_Iio_at_top (a : α) :
map (coe : Iio a → α) at_top = 𝓝[Iio a] a :=
@map_coe_Ioi_at_bot (order_dual α) _ _ _ _ _
variables {l : filter β} {f : α → β}
@[simp] lemma tendsto_comp_coe_Ioo_at_top (h : a < b) :
tendsto (λ x : Ioo a b, f x) at_top l ↔ tendsto f (𝓝[Iio b] b) l :=
by rw [← map_coe_Ioo_at_top h, tendsto_map'_iff]
@[simp] lemma tendsto_comp_coe_Ioo_at_bot (h : a < b) :
tendsto (λ x : Ioo a b, f x) at_bot l ↔ tendsto f (𝓝[Ioi a] a) l :=
by rw [← map_coe_Ioo_at_bot h, tendsto_map'_iff]
@[simp] lemma tendsto_comp_coe_Ioi_at_bot :
tendsto (λ x : Ioi a, f x) at_bot l ↔ tendsto f (𝓝[Ioi a] a) l :=
by rw [← map_coe_Ioi_at_bot, tendsto_map'_iff]
@[simp] lemma tendsto_comp_coe_Iio_at_top :
tendsto (λ x : Iio a, f x) at_top l ↔ tendsto f (𝓝[Iio a] a) l :=
by rw [← map_coe_Iio_at_top, tendsto_map'_iff]
@[simp] lemma tendsto_Ioo_at_top {f : β → Ioo a b} :
tendsto f l at_top ↔ tendsto (λ x, (f x : α)) l (𝓝[Iio b] b) :=
by rw [← comap_coe_Ioo_nhds_within_Iio, tendsto_comap_iff]
@[simp] lemma tendsto_Ioo_at_bot {f : β → Ioo a b} :
tendsto f l at_bot ↔ tendsto (λ x, (f x : α)) l (𝓝[Ioi a] a) :=
by rw [← comap_coe_Ioo_nhds_within_Ioi, tendsto_comap_iff]
@[simp] lemma tendsto_Ioi_at_bot {f : β → Ioi a} :
tendsto f l at_bot ↔ tendsto (λ x, (f x : α)) l (𝓝[Ioi a] a) :=
by rw [← comap_coe_Ioi_nhds_within_Ioi, tendsto_comap_iff]
@[simp] lemma tendsto_Iio_at_top {f : β → Iio a} :
tendsto f l at_top ↔ tendsto (λ x, (f x : α)) l (𝓝[Iio a] a) :=
by rw [← comap_coe_Iio_nhds_within_Iio, tendsto_comap_iff]
end densely_ordered
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 :=
(is_lub_Sup s).mem_closure hs
lemma Inf_mem_closure {α : Type u} [topological_space α] [complete_linear_order α]
[order_topology α] {s : set α} (hs : s.nonempty) :
Inf s ∈ closure s :=
(is_glb_Inf s).mem_closure hs
lemma is_closed.Sup_mem {α : Type u} [topological_space α] [complete_linear_order α]
[order_topology α] {s : set α} (hs : s.nonempty) (hc : is_closed s) :
Sup s ∈ s :=
(is_lub_Sup s).mem_of_is_closed hs hc
lemma is_closed.Inf_mem {α : Type u} [topological_space α] [complete_linear_order α]
[order_topology α] {s : set α} (hs : s.nonempty) (hc : is_closed s) :
Inf s ∈ s :=
(is_glb_Inf s).mem_of_is_closed hs hc
/-- A monotone function continuous at the supremum of a nonempty set sends this supremum to
the supremum of the image of this set. -/
lemma map_Sup_of_continuous_at_of_monotone' {f : α → β} {s : set α} (Cf : continuous_at f (Sup s))
(Mf : monotone f) (hs : s.nonempty) :
f (Sup s) = Sup (f '' s) :=
--This is a particular case of the more general is_lub.is_lub_of_tendsto
((is_lub_Sup _).is_lub_of_tendsto (λ x hx y hy xy, Mf xy) hs $
Cf.mono_left inf_le_left).Sup_eq.symm
/-- A monotone function `s` sending `bot` to `bot` and continuous at the supremum of a set sends
this supremum to the supremum of the image of this set. -/
lemma map_Sup_of_continuous_at_of_monotone {f : α → β} {s : set α} (Cf : continuous_at f (Sup s))
(Mf : monotone f) (fbot : f ⊥ = ⊥) :
f (Sup s) = Sup (f '' s) :=
begin
cases s.eq_empty_or_nonempty with h h,
{ simp [h, fbot] },
{ exact map_Sup_of_continuous_at_of_monotone' Cf Mf h }
end
/-- A monotone function continuous at the indexed supremum over a nonempty `Sort` sends this indexed
supremum to the indexed supremum of the composition. -/
lemma map_supr_of_continuous_at_of_monotone' {ι : Sort*} [nonempty ι] {f : α → β} {g : ι → α}
(Cf : continuous_at f (supr g)) (Mf : monotone f) :
f (⨆ i, g i) = ⨆ i, f (g i) :=
by rw [supr, map_Sup_of_continuous_at_of_monotone' Cf Mf (range_nonempty g), ← range_comp, supr]
/-- If a monotone function sending `bot` to `bot` is continuous at the indexed supremum over
a `Sort`, then it sends this indexed supremum to the indexed supremum of the composition. -/
lemma map_supr_of_continuous_at_of_monotone {ι : Sort*} {f : α → β} {g : ι → α}
(Cf : continuous_at f (supr g)) (Mf : monotone f) (fbot : f ⊥ = ⊥) :
f (⨆ i, g i) = ⨆ i, f (g i) :=
by rw [supr, map_Sup_of_continuous_at_of_monotone Cf Mf fbot, ← range_comp, supr]
/-- A monotone function continuous at the infimum of a nonempty set sends this infimum to
the infimum of the image of this set. -/
lemma map_Inf_of_continuous_at_of_monotone' {f : α → β} {s : set α} (Cf : continuous_at f (Inf s))
(Mf : monotone f) (hs : s.nonempty) :
f (Inf s) = Inf (f '' s) :=
@map_Sup_of_continuous_at_of_monotone' (order_dual α) (order_dual β) _ _ _ _ _ _ f s Cf
Mf.order_dual hs
/-- A monotone function `s` sending `top` to `top` and continuous at the infimum of a set sends
this infimum to the infimum of the image of this set. -/
lemma map_Inf_of_continuous_at_of_monotone {f : α → β} {s : set α} (Cf : continuous_at f (Inf s))
(Mf : monotone f) (ftop : f ⊤ = ⊤) :
f (Inf s) = Inf (f '' s) :=
@map_Sup_of_continuous_at_of_monotone (order_dual α) (order_dual β) _ _ _ _ _ _ f s Cf
Mf.order_dual ftop
/-- A monotone function continuous at the indexed infimum over a nonempty `Sort` sends this indexed
infimum to the indexed infimum of the composition. -/
lemma map_infi_of_continuous_at_of_monotone' {ι : Sort*} [nonempty ι] {f : α → β} {g : ι → α}
(Cf : continuous_at f (infi g)) (Mf : monotone f) :
f (⨅ i, g i) = ⨅ i, f (g i) :=
@map_supr_of_continuous_at_of_monotone' (order_dual α) (order_dual β) _ _ _ _ _ _ ι _ f g Cf
Mf.order_dual
/-- If a monotone function sending `top` to `top` is continuous at the indexed infimum over
a `Sort`, then it sends this indexed infimum to the indexed infimum of the composition. -/
lemma map_infi_of_continuous_at_of_monotone {ι : Sort*} {f : α → β} {g : ι → α}
(Cf : continuous_at f (infi g)) (Mf : monotone f) (ftop : f ⊤ = ⊤) :
f (infi g) = infi (f ∘ g) :=
@map_supr_of_continuous_at_of_monotone (order_dual α) (order_dual β) _ _ _ _ _ _ ι f g Cf
Mf.order_dual ftop
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 {s : set α} (hs : s.nonempty) (B : bdd_above s) : Sup s ∈ closure s :=
(is_lub_cSup hs B).mem_closure hs
lemma cInf_mem_closure {s : set α} (hs : s.nonempty) (B : bdd_below s) : Inf s ∈ closure s :=
(is_glb_cInf hs B).mem_closure hs
lemma is_closed.cSup_mem {s : set α} (hc : is_closed s) (hs : s.nonempty) (B : bdd_above s) :
Sup s ∈ s :=
(is_lub_cSup hs B).mem_of_is_closed hs hc
lemma is_closed.cInf_mem {s : set α} (hc : is_closed s) (hs : s.nonempty) (B : bdd_below s) :
Inf s ∈ s :=
(is_glb_cInf hs B).mem_of_is_closed hs hc
/-- If a monotone function is continuous at the supremum of a nonempty bounded above set `s`,
then it sends this supremum to the supremum of the image of `s`. -/
lemma map_cSup_of_continuous_at_of_monotone {f : α → β} {s : set α} (Cf : continuous_at f (Sup s))
(Mf : monotone f) (ne : s.nonempty) (H : bdd_above s) :
f (Sup s) = Sup (f '' s) :=
begin
refine ((is_lub_cSup (ne.image f) (Mf.map_bdd_above H)).unique _).symm,
refine (is_lub_cSup ne H).is_lub_of_tendsto (λx hx y hy xy, Mf xy) ne _,
exact Cf.mono_left inf_le_left
end
/-- If a monotone function is continuous at the indexed supremum of a bounded function on
a nonempty `Sort`, then it sends this supremum to the supremum of the composition. -/
lemma map_csupr_of_continuous_at_of_monotone {f : α → β} {g : γ → α}
(Cf : continuous_at f (⨆ i, g i)) (Mf : monotone f) (H : bdd_above (range g)) :
f (⨆ i, g i) = ⨆ i, f (g i) :=
by rw [supr, map_cSup_of_continuous_at_of_monotone Cf Mf (range_nonempty _) H, ← range_comp, supr]
/-- If a monotone function is continuous at the infimum of a nonempty bounded below set `s`,
then it sends this infimum to the infimum of the image of `s`. -/
lemma map_cInf_of_continuous_at_of_monotone {f : α → β} {s : set α} (Cf : continuous_at f (Inf s))
(Mf : monotone f) (ne : s.nonempty) (H : bdd_below s) :
f (Inf s) = Inf (f '' s) :=
@map_cSup_of_continuous_at_of_monotone (order_dual α) (order_dual β) _ _ _ _ _ _ f s Cf
Mf.order_dual ne H
/-- A continuous monotone function sends indexed infimum to indexed infimum in conditionally
complete linear order, under a boundedness assumption. -/
lemma map_cinfi_of_continuous_at_of_monotone {f : α → β} {g : γ → α}
(Cf : continuous_at f (⨅ i, g i)) (Mf : monotone f) (H : bdd_below (range g)) :
f (⨅ i, g i) = ⨅ i, f (g i) :=
@map_csupr_of_continuous_at_of_monotone (order_dual α) (order_dual β) _ _ _ _ _ _ _ _ _ _
Cf Mf.order_dual H
/-- A monotone map has a limit to the left of any point `x`, equal to `Sup (f '' (Iio x))`. -/
lemma monotone.tendsto_nhds_within_Iio
{α : Type*} [linear_order α] [topological_space α] [order_topology α]
{f : α → β} (Mf : monotone f) (x : α) :
tendsto f (𝓝[Iio x] x) (𝓝 (Sup (f '' (Iio x)))) :=
begin
rcases eq_empty_or_nonempty (Iio x) with h|h, { simp [h] },
refine tendsto_order.2 ⟨λ l hl, _, λ m hm, _⟩,
{ obtain ⟨z, zx, lz⟩ : ∃ (a : α), a < x ∧ l < f a,
by simpa only [mem_image, exists_prop, exists_exists_and_eq_and]
using exists_lt_of_lt_cSup (nonempty_image_iff.2 h) hl,
exact (mem_nhds_within_Iio_iff_exists_Ioo_subset' zx).2
⟨z, zx, λ y hy, lz.trans_le (Mf (hy.1.le))⟩ },
{ filter_upwards [self_mem_nhds_within],
assume y hy,
apply lt_of_le_of_lt _ hm,
exact le_cSup (Mf.map_bdd_above bdd_above_Iio) (mem_image_of_mem _ hy) }
end
/-- A monotone map has a limit to the right of any point `x`, equal to `Inf (f '' (Ioi x))`. -/
lemma monotone.tendsto_nhds_within_Ioi
{α : Type*} [linear_order α] [topological_space α] [order_topology α]
{f : α → β} (Mf : monotone f) (x : α) :
tendsto f (𝓝[Ioi x] x) (𝓝 (Inf (f '' (Ioi x)))) :=
@monotone.tendsto_nhds_within_Iio (order_dual β) _ _ _ (order_dual α) _ _ _ f Mf.order_dual x
/-- A bounded connected subset of a conditionally complete linear order includes the open interval
`(Inf s, Sup s)`. -/
lemma is_connected.Ioo_cInf_cSup_subset {s : set α} (hs : is_connected s) (hb : bdd_below s)
(ha : bdd_above s) :
Ioo (Inf s) (Sup s) ⊆ s :=
λ x hx, let ⟨y, ys, hy⟩ := (is_glb_lt_iff (is_glb_cInf hs.nonempty hb)).1 hx.1 in
let ⟨z, zs, hz⟩ := (lt_is_lub_iff (is_lub_cSup hs.nonempty ha)).1 hx.2 in
hs.Icc_subset ys zs ⟨le_of_lt hy, le_of_lt hz⟩
lemma eq_Icc_cInf_cSup_of_connected_bdd_closed {s : set α} (hc : is_connected s) (hb : bdd_below s)
(ha : bdd_above s) (hcl : is_closed s) :
s = Icc (Inf s) (Sup s) :=
subset.antisymm (subset_Icc_cInf_cSup hb ha) $
hc.Icc_subset (hcl.cInf_mem hc.nonempty hb) (hcl.cSup_mem hc.nonempty ha)
lemma is_preconnected.Ioi_cInf_subset {s : set α} (hs : is_preconnected s) (hb : bdd_below s)
(ha : ¬bdd_above s) :
Ioi (Inf s) ⊆ s :=
begin
have sne : s.nonempty := @nonempty_of_not_bdd_above α _ s ⟨Inf ∅⟩ ha,
intros x hx,
obtain ⟨y, ys, hy⟩ : ∃ y ∈ s, y < x := (is_glb_lt_iff (is_glb_cInf sne hb)).1 hx,
obtain ⟨z, zs, hz⟩ : ∃ z ∈ s, x < z := not_bdd_above_iff.1 ha x,
exact hs.Icc_subset ys zs ⟨le_of_lt hy, le_of_lt hz⟩
end
lemma is_preconnected.Iio_cSup_subset {s : set α} (hs : is_preconnected s) (hb : ¬bdd_below s)
(ha : bdd_above s) :
Iio (Sup s) ⊆ s :=
@is_preconnected.Ioi_cInf_subset (order_dual α) _ _ _ s hs ha hb
/-- A preconnected set in a conditionally complete linear order is either one of the intervals
`[Inf s, Sup s]`, `[Inf s, Sup s)`, `(Inf s, Sup s]`, `(Inf s, Sup s)`, `[Inf s, +∞)`,
`(Inf s, +∞)`, `(-∞, Sup s]`, `(-∞, Sup s)`, `(-∞, +∞)`, or `∅`. The converse statement requires
`α` to be densely ordererd. -/
lemma is_preconnected.mem_intervals {s : set α} (hs : is_preconnected s) :
s ∈ ({Icc (Inf s) (Sup s), Ico (Inf s) (Sup s), Ioc (Inf s) (Sup s), Ioo (Inf s) (Sup s),
Ici (Inf s), Ioi (Inf s), Iic (Sup s), Iio (Sup s), univ, ∅} : set (set α)) :=
begin
rcases s.eq_empty_or_nonempty with rfl|hne,
{ apply_rules [or.inr, mem_singleton] },
have hs' : is_connected s := ⟨hne, hs⟩,
by_cases hb : bdd_below s; by_cases ha : bdd_above s,
{ rcases mem_Icc_Ico_Ioc_Ioo_of_subset_of_subset (hs'.Ioo_cInf_cSup_subset hb ha)
(subset_Icc_cInf_cSup hb ha) with hs|hs|hs|hs,
{ exact (or.inl hs) },
{ exact (or.inr $ or.inl hs) },
{ exact (or.inr $ or.inr $ or.inl hs) },
{ exact (or.inr $ or.inr $ or.inr $ or.inl hs) } },
{ refine (or.inr $ or.inr $ or.inr $ or.inr _),
cases mem_Ici_Ioi_of_subset_of_subset (hs.Ioi_cInf_subset hb ha) (λ x hx, cInf_le hb hx)
with hs hs,
{ exact or.inl hs },
{ exact or.inr (or.inl hs) } },
{ iterate 6 { apply or.inr },
cases mem_Iic_Iio_of_subset_of_subset (hs.Iio_cSup_subset hb ha) (λ x hx, le_cSup ha hx)
with hs hs,
{ exact or.inl hs },
{ exact or.inr (or.inl hs) } },
{ iterate 8 { apply or.inr },
exact or.inl (hs.eq_univ_of_unbounded hb ha) }
end
/-- A preconnected set is either one of the intervals `Icc`, `Ico`, `Ioc`, `Ioo`, `Ici`, `Ioi`,
`Iic`, `Iio`, or `univ`, or `∅`. The converse statement requires `α` to be densely ordered. Though
one can represent `∅` as `(Inf s, Inf s)`, we include it into the list of possible cases to improve
readability. -/
lemma set_of_is_preconnected_subset_of_ordered :
{s : set α | is_preconnected s} ⊆
-- bounded intervals
(range (uncurry Icc) ∪ range (uncurry Ico) ∪ range (uncurry Ioc) ∪ range (uncurry Ioo)) ∪
-- unbounded intervals and `univ`
(range Ici ∪ range Ioi ∪ range Iic ∪ range Iio ∪ {univ, ∅}) :=
begin
intros s hs,
rcases hs.mem_intervals with hs|hs|hs|hs|hs|hs|hs|hs|hs|hs,
{ exact (or.inl $ or.inl $ or.inl $ or.inl ⟨(Inf s, Sup s), hs.symm⟩) },
{ exact (or.inl $ or.inl $ or.inl $ or.inr ⟨(Inf s, Sup s), hs.symm⟩) },
{ exact (or.inl $ or.inl $ or.inr ⟨(Inf s, Sup s), hs.symm⟩) },
{ exact (or.inl $ or.inr ⟨(Inf s, Sup s), hs.symm⟩) },
{ exact (or.inr $ or.inl $ or.inl $ or.inl $ or.inl ⟨Inf s, hs.symm⟩) },
{ exact (or.inr $ or.inl $ or.inl $ or.inl $ or.inr ⟨Inf s, hs.symm⟩) },
{ exact (or.inr $ or.inl $ or.inl $ or.inr ⟨Sup s, hs.symm⟩) },
{ exact (or.inr $ or.inl $ or.inr ⟨Sup s, hs.symm⟩) },
{ exact (or.inr $ or.inr $ or.inl hs) },
{ exact (or.inr $ or.inr $ or.inr hs) }
end
/-- 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 hs.cSup_mem ⟨_, ha⟩ 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 ∈ 𝓝[Ioi x] 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 ∈ 𝓝[Ioi x] x,
from inter_mem (hgt x ⟨hxs, hxab⟩) (Ioc_mem_nhds_within_Ioi ⟨le_refl _, hyxb⟩),
exact (nhds_within_Ioi_self_ne_bot' hxab.2).nonempty_of_mem this
end
/-- A closed interval in a densely ordered conditionally complete linear order is preconnected. -/
lemma is_preconnected_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 ∈ 𝓝[Ioi z] z,
{ rw [← nhds_within_Ioc_eq_nhds_within_Ioi hz.2],
exact mem_nhds_within.2 ⟨tᶜ, ht.is_open_compl, zt, subset.refl _⟩},
apply mem_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_interval : is_preconnected (interval a b) := is_preconnected_Icc
lemma set.ord_connected.is_preconnected {s : set α} (h : s.ord_connected) :
is_preconnected s :=
is_preconnected_of_forall_pair $ λ x y hx hy, ⟨interval x y, h.interval_subset hx hy,
left_mem_interval, right_mem_interval, is_preconnected_interval⟩
lemma is_preconnected_iff_ord_connected {s : set α} :
is_preconnected s ↔ ord_connected s :=
⟨is_preconnected.ord_connected, set.ord_connected.is_preconnected⟩
lemma is_preconnected_Ici : is_preconnected (Ici a) := ord_connected_Ici.is_preconnected
lemma is_preconnected_Iic : is_preconnected (Iic a) := ord_connected_Iic.is_preconnected
lemma is_preconnected_Iio : is_preconnected (Iio a) := ord_connected_Iio.is_preconnected
lemma is_preconnected_Ioi : is_preconnected (Ioi a) := ord_connected_Ioi.is_preconnected
lemma is_preconnected_Ioo : is_preconnected (Ioo a b) := ord_connected_Ioo.is_preconnected
lemma is_preconnected_Ioc : is_preconnected (Ioc a b) := ord_connected_Ioc.is_preconnected
lemma is_preconnected_Ico : is_preconnected (Ico a b) := ord_connected_Ico.is_preconnected
@[priority 100]
instance ordered_connected_space : preconnected_space α :=
⟨ord_connected_univ.is_preconnected⟩
/-- In a dense conditionally complete linear order, the set of preconnected sets is exactly
the set of the intervals `Icc`, `Ico`, `Ioc`, `Ioo`, `Ici`, `Ioi`, `Iic`, `Iio`, `(-∞, +∞)`,
or `∅`. Though one can represent `∅` as `(Inf s, Inf s)`, we include it into the list of
possible cases to improve readability. -/
lemma set_of_is_preconnected_eq_of_ordered :
{s : set α | is_preconnected s} =
-- bounded intervals
(range (uncurry Icc) ∪ range (uncurry Ico) ∪ range (uncurry Ioc) ∪ range (uncurry Ioo)) ∪
-- unbounded intervals and `univ`
(range Ici ∪ range Ioi ∪ range Iic ∪ range Iio ∪ {univ, ∅}) :=
begin
refine subset.antisymm set_of_is_preconnected_subset_of_ordered _,
simp only [subset_def, -mem_range, forall_range_iff, uncurry, or_imp_distrib, forall_and_distrib,
mem_union, mem_set_of_eq, insert_eq, mem_singleton_iff, forall_eq, forall_true_iff, and_true,
is_preconnected_Icc, is_preconnected_Ico, is_preconnected_Ioc,
is_preconnected_Ioo, is_preconnected_Ioi, is_preconnected_Iio, is_preconnected_Ici,
is_preconnected_Iic, is_preconnected_univ, is_preconnected_empty],
end
variables {δ : Type*} [linear_order δ] [topological_space δ] [order_closed_topology δ]
/-- **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_preconnected_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_preconnected_Icc.intermediate_value (right_mem_Icc.2 hab) (left_mem_Icc.2 hab) hf
/-- **Intermediate Value Theorem** for continuous functions on closed intervals, unordered case. -/
lemma intermediate_value_interval {a b : α} {f : α → δ} (hf : continuous_on f (interval a b)) :
interval (f a) (f b) ⊆ f '' interval a b :=
by cases le_total (f a) (f b); simp [*, is_preconnected_interval.intermediate_value]
lemma intermediate_value_Ico {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) :
Ico (f a) (f b) ⊆ f '' (Ico a b) :=
or.elim (eq_or_lt_of_le hab) (λ he y h, absurd h.2 (not_lt_of_le (he ▸ h.1)))
(λ hlt, @is_preconnected.intermediate_value_Ico _ _ _ _ _ _ _ (is_preconnected_Ico)
_ _ ⟨refl a, hlt⟩ (right_nhds_within_Ico_ne_bot hlt) inf_le_right _ (hf.mono Ico_subset_Icc_self)
_ ((hf.continuous_within_at ⟨hab, refl b⟩).mono Ico_subset_Icc_self))
lemma intermediate_value_Ico' {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) :
Ioc (f b) (f a) ⊆ f '' (Ico a b) :=
or.elim (eq_or_lt_of_le hab) (λ he y h, absurd h.1 (not_lt_of_le (he ▸ h.2)))
(λ hlt, @is_preconnected.intermediate_value_Ioc _ _ _ _ _ _ _ (is_preconnected_Ico)
_ _ ⟨refl a, hlt⟩ (right_nhds_within_Ico_ne_bot hlt) inf_le_right _ (hf.mono Ico_subset_Icc_self)
_ ((hf.continuous_within_at ⟨hab, refl b⟩).mono Ico_subset_Icc_self))
lemma intermediate_value_Ioc {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) :
Ioc (f a) (f b) ⊆ f '' (Ioc a b) :=
or.elim (eq_or_lt_of_le hab) (λ he y h, absurd h.2 (not_le_of_lt (he ▸ h.1)))
(λ hlt, @is_preconnected.intermediate_value_Ioc _ _ _ _ _ _ _ (is_preconnected_Ioc)
_ _ ⟨hlt, refl b⟩ (left_nhds_within_Ioc_ne_bot hlt) inf_le_right _ (hf.mono Ioc_subset_Icc_self)
_ ((hf.continuous_within_at ⟨refl a, hab⟩).mono Ioc_subset_Icc_self))
lemma intermediate_value_Ioc' {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) :
Ico (f b) (f a) ⊆ f '' (Ioc a b) :=
or.elim (eq_or_lt_of_le hab) (λ he y h, absurd h.1 (not_le_of_lt (he ▸ h.2)))
(λ hlt, @is_preconnected.intermediate_value_Ico _ _ _ _ _ _ _ (is_preconnected_Ioc)
_ _ ⟨hlt, refl b⟩ (left_nhds_within_Ioc_ne_bot hlt) inf_le_right _ (hf.mono Ioc_subset_Icc_self)
_ ((hf.continuous_within_at ⟨refl a, hab⟩).mono Ioc_subset_Icc_self))
lemma intermediate_value_Ioo {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) :
Ioo (f a) (f b) ⊆ f '' (Ioo a b) :=
or.elim (eq_or_lt_of_le hab) (λ he y h, absurd h.2 (not_lt_of_lt (he ▸ h.1)))
(λ hlt, @is_preconnected.intermediate_value_Ioo _ _ _ _ _ _ _ (is_preconnected_Ioo)
_ _ (left_nhds_within_Ioo_ne_bot hlt) (right_nhds_within_Ioo_ne_bot hlt)
inf_le_right inf_le_right _ (hf.mono Ioo_subset_Icc_self)
_ _ ((hf.continuous_within_at ⟨refl a, hab⟩).mono Ioo_subset_Icc_self)
((hf.continuous_within_at ⟨hab, refl b⟩).mono Ioo_subset_Icc_self))
lemma intermediate_value_Ioo' {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) :
Ioo (f b) (f a) ⊆ f '' (Ioo a b) :=
or.elim (eq_or_lt_of_le hab) (λ he y h, absurd h.1 (not_lt_of_lt (he ▸ h.2)))
(λ hlt, @is_preconnected.intermediate_value_Ioo _ _ _ _ _ _ _ (is_preconnected_Ioo)
_ _ (right_nhds_within_Ioo_ne_bot hlt) (left_nhds_within_Ioo_ne_bot hlt)
inf_le_right inf_le_right _ (hf.mono Ioo_subset_Icc_self)
_ _ ((hf.continuous_within_at ⟨hab, refl b⟩).mono Ioo_subset_Icc_self)
((hf.continuous_within_at ⟨refl a, hab⟩).mono Ioo_subset_Icc_self))
/-- A continuous function which tendsto `at_top` `at_top` and to `at_bot` `at_bot` is surjective. -/
lemma continuous.surjective {f : α → δ} (hf : continuous f) (h_top : tendsto f at_top at_top)
(h_bot : tendsto f at_bot at_bot) :
function.surjective f :=
λ p, mem_range_of_exists_le_of_exists_ge hf
(h_bot.eventually (eventually_le_at_bot p)).exists
(h_top.eventually (eventually_ge_at_top p)).exists
/-- A continuous function which tendsto `at_bot` `at_top` and to `at_top` `at_bot` is surjective. -/
lemma continuous.surjective' {f : α → δ} (hf : continuous f) (h_top : tendsto f at_bot at_top)
(h_bot : tendsto f at_top at_bot) :
function.surjective f :=
@continuous.surjective (order_dual α) _ _ _ _ _ _ _ _ _ hf h_top h_bot
/-- If a function `f : α → β` is continuous on a nonempty interval `s`, its restriction to `s`
tends to `at_bot : filter β` along `at_bot : filter ↥s` and tends to `at_top : filter β` along
`at_top : filter ↥s`, then the restriction of `f` to `s` is surjective. We formulate the
conclusion as `surj_on f s univ`. -/
lemma continuous_on.surj_on_of_tendsto {f : α → β} {s : set α} [ord_connected s]
(hs : s.nonempty) (hf : continuous_on f s) (hbot : tendsto (λ x : s, f x) at_bot at_bot)
(htop : tendsto (λ x : s, f x) at_top at_top) :
surj_on f s univ :=
by haveI := inhabited_of_nonempty hs.to_subtype;
exact (surj_on_iff_surjective.2 $
(continuous_on_iff_continuous_restrict.1 hf).surjective htop hbot)
/-- If a function `f : α → β` is continuous on a nonempty interval `s`, its restriction to `s`
tends to `at_top : filter β` along `at_bot : filter ↥s` and tends to `at_bot : filter β` along
`at_top : filter ↥s`, then the restriction of `f` to `s` is surjective. We formulate the
conclusion as `surj_on f s univ`. -/
lemma continuous_on.surj_on_of_tendsto' {f : α → β} {s : set α} [ord_connected s]
(hs : s.nonempty) (hf : continuous_on f s) (hbot : tendsto (λ x : s, f x) at_bot at_top)
(htop : tendsto (λ x : s, f x) at_top at_bot) :
surj_on f s univ :=
@continuous_on.surj_on_of_tendsto α (order_dual β) _ _ _ _ _ _ _ _ _ _ hs hf hbot htop
end densely_ordered
/-- A closed interval in a conditionally complete linear order is compact. -/
lemma is_compact_Icc {a b : α} : is_compact (Icc a b) :=
begin
cases le_or_lt a b with hab hab, swap, { simp [hab] },
refine is_compact_iff_ultrafilter_le_nhds.2 (λ f hf, _),
contrapose! hf,
rw [le_principal_iff],
have hpt : ∀ x ∈ Icc a b, {x} ∉ f,
from λ x hx hxf, hf x hx ((le_pure_iff.2 hxf).trans (pure_le_nhds x)),
set s := {x ∈ Icc a b | Icc a x ∉ f},
have hsb : b ∈ upper_bounds s, from λ x hx, hx.1.2,
have sbd : bdd_above s, from ⟨b, hsb⟩,
have ha : a ∈ s, by simp [hpt, hab],
rcases hab.eq_or_lt with rfl|hlt, { exact ha.2 },
set c := Sup s,
have hsc : is_lub s c, from is_lub_cSup ⟨a, ha⟩ sbd,
have hc : c ∈ Icc a b, from ⟨hsc.1 ha, hsc.2 hsb⟩,
specialize hf c hc,
have hcs : c ∈ s,
{ cases hc.1.eq_or_lt with heq hlt, { rwa ← heq },
refine ⟨hc, λ hcf, hf (λ U hU, _)⟩,
rcases (mem_nhds_within_Iic_iff_exists_Ioc_subset' hlt).1 (mem_nhds_within_of_mem_nhds hU)
with ⟨x, hxc, hxU⟩,
rcases ((hsc.frequently_mem ⟨a, ha⟩).and_eventually
(Ioc_mem_nhds_within_Iic ⟨hxc, le_rfl⟩)).exists
with ⟨y, ⟨hyab, hyf⟩, hy⟩,
refine mem_of_superset(f.diff_mem_iff.2 ⟨hcf, hyf⟩) (subset.trans _ hxU),
rw diff_subset_iff,
exact subset.trans Icc_subset_Icc_union_Ioc
(union_subset_union subset.rfl $ Ioc_subset_Ioc_left hy.1.le) },
cases hc.2.eq_or_lt with heq hlt, { rw ← heq, exact hcs.2 },
contrapose! hf,
intros U hU,
rcases (mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset hlt).1 (mem_nhds_within_of_mem_nhds hU)
with ⟨y, hxy, hyU⟩,
refine mem_of_superset _ hyU, clear_dependent U,
have hy : y ∈ Icc a b, from ⟨hc.1.trans hxy.1.le, hxy.2⟩,
by_cases hay : Icc a y ∈ f,
{ refine mem_of_superset (f.diff_mem_iff.2 ⟨f.diff_mem_iff.2 ⟨hay, hcs.2⟩, hpt y hy⟩) _,
rw [diff_subset_iff, union_comm, Ico_union_right hxy.1.le, diff_subset_iff],
exact Icc_subset_Icc_union_Icc },
{ exact ((hsc.1 ⟨hy, hay⟩).not_lt hxy.1).elim },
end
/-- An unordered closed interval in a conditionally complete linear order is compact. -/
lemma is_compact_interval {a b : α} : is_compact (interval a b) := is_compact_Icc
lemma is_compact_pi_Icc {ι : Type*} {α : ι → Type*} [Π i, conditionally_complete_linear_order (α i)]
[Π i, topological_space (α i)] [Π i, order_topology (α i)] (a b : Π i, α i) :
is_compact (Icc a b) :=
pi_univ_Icc a b ▸ is_compact_univ_pi $ λ i, is_compact_Icc
instance compact_space_Icc (a b : α) : compact_space (Icc a b) :=
is_compact_iff_compact_space.mp is_compact_Icc
instance compact_space_pi_Icc {ι : Type*} {α : ι → Type*}
[Π i, conditionally_complete_linear_order (α i)] [Π i, topological_space (α i)]
[Π i, order_topology (α i)] (a b : Π i, α i) : compact_space (Icc a b) :=
is_compact_iff_compact_space.mp (is_compact_pi_Icc a b)
@[priority 100] -- See note [lower instance priority]
instance compact_space_of_complete_linear_order {α : Type*} [complete_linear_order α]
[topological_space α] [order_topology α] :
compact_space α :=
⟨by simp only [← Icc_bot_top, is_compact_Icc]⟩
lemma is_compact.Inf_mem {s : set α} (hs : is_compact s) (ne_s : s.nonempty) :
Inf s ∈ s :=
hs.is_closed.cInf_mem ne_s hs.bdd_below
lemma is_compact.Sup_mem {s : set α} (hs : is_compact s) (ne_s : s.nonempty) :
Sup s ∈ s :=
@is_compact.Inf_mem (order_dual α) _ _ _ _ hs ne_s
lemma is_compact.is_glb_Inf {s : set α} (hs : is_compact s) (ne_s : s.nonempty) :
is_glb s (Inf s) :=
is_glb_cInf ne_s hs.bdd_below
lemma is_compact.is_lub_Sup {s : set α} (hs : is_compact s) (ne_s : s.nonempty) :
is_lub s (Sup s) :=
@is_compact.is_glb_Inf (order_dual α) _ _ _ _ hs ne_s
lemma is_compact.is_least_Inf {s : set α} (hs : is_compact s) (ne_s : s.nonempty) :
is_least s (Inf s) :=
⟨hs.Inf_mem ne_s, (hs.is_glb_Inf ne_s).1⟩
lemma is_compact.is_greatest_Sup {s : set α} (hs : is_compact s) (ne_s : s.nonempty) :
is_greatest s (Sup s) :=
@is_compact.is_least_Inf (order_dual α) _ _ _ _ hs ne_s
lemma is_compact.exists_is_least {s : set α} (hs : is_compact s) (ne_s : s.nonempty) :
∃ x, is_least s x :=
⟨_, hs.is_least_Inf ne_s⟩
lemma is_compact.exists_is_greatest {s : set α} (hs : is_compact s) (ne_s : s.nonempty) :
∃ x, is_greatest s x :=
⟨_, hs.is_greatest_Sup ne_s⟩
lemma is_compact.exists_is_glb {s : set α} (hs : is_compact s) (ne_s : s.nonempty) :
∃ x ∈ s, is_glb s x :=
⟨_, hs.Inf_mem ne_s, hs.is_glb_Inf ne_s⟩
lemma is_compact.exists_is_lub {s : set α} (hs : is_compact s) (ne_s : s.nonempty) :
∃ x ∈ s, is_lub s x :=
⟨_, hs.Sup_mem ne_s, hs.is_lub_Sup ne_s⟩
lemma is_compact.exists_Inf_image_eq {α : Type u} [topological_space α]
{s : set α} (hs : is_compact s) (ne_s : s.nonempty) {f : α → β} (hf : continuous_on f s) :
∃ x ∈ s, Inf (f '' s) = f x :=
let ⟨x, hxs, hx⟩ := (hs.image_of_continuous_on hf).Inf_mem (ne_s.image f)
in ⟨x, hxs, hx.symm⟩
lemma is_compact.exists_Sup_image_eq {α : Type u} [topological_space α]:
∀ {s : set α}, is_compact s → s.nonempty → ∀ {f : α → β}, continuous_on f s →
∃ x ∈ s, Sup (f '' s) = f x :=
@is_compact.exists_Inf_image_eq (order_dual β) _ _ _ _ _
lemma eq_Icc_of_connected_compact {s : set α} (h₁ : is_connected s) (h₂ : is_compact s) :
s = Icc (Inf s) (Sup s) :=
eq_Icc_cInf_cSup_of_connected_bdd_closed h₁ h₂.bdd_below h₂.bdd_above h₂.is_closed
/-- The extreme value theorem: a continuous function realizes its minimum on a compact set -/
lemma is_compact.exists_forall_le {α : Type u} [topological_space α]
{s : set α} (hs : is_compact s) (ne_s : s.nonempty) {f : α → β} (hf : continuous_on f s) :
∃x∈s, ∀y∈s, f x ≤ f y :=
begin
rcases hs.exists_Inf_image_eq ne_s hf with ⟨x, hxs, hx⟩,
refine ⟨x, hxs, λ y hy, _⟩,
rw ← hx,
exact ((hs.image_of_continuous_on hf).is_glb_Inf (ne_s.image f)).1 (mem_image_of_mem _ hy)
end
/-- The extreme value theorem: a continuous function realizes its maximum on a compact set -/
lemma is_compact.exists_forall_ge {α : Type u} [topological_space α]:
∀ {s : set α}, is_compact s → s.nonempty → ∀ {f : α → β}, continuous_on f s →
∃x∈s, ∀y∈s, f y ≤ f x :=
@is_compact.exists_forall_le (order_dual β) _ _ _ _ _
/-- The extreme value theorem: if a continuous function `f` tends to infinity away from compact
sets, then it has a global minimum. -/
lemma continuous.exists_forall_le {α : Type*} [topological_space α] [nonempty α] {f : α → β}
(hf : continuous f) (hlim : tendsto f (cocompact α) at_top) :
∃ x, ∀ y, f x ≤ f y :=
begin
inhabit α,
obtain ⟨s : set α, hsc : is_compact s, hsf : ∀ x ∉ s, f (default α) ≤ f x⟩ :=
(has_basis_cocompact.tendsto_iff at_top_basis).1 hlim (f $ default α) trivial,
obtain ⟨x, -, hx⟩ :=
(hsc.insert (default α)).exists_forall_le (nonempty_insert _ _) hf.continuous_on,
refine ⟨x, λ y, _⟩,
by_cases hy : y ∈ s,
exacts [hx y (or.inr hy), (hx _ (or.inl rfl)).trans (hsf y hy)]
end
/-- The extreme value theorem: if a continuous function `f` tends to negative infinity away from
compactx sets, then it has a global maximum. -/
lemma continuous.exists_forall_ge {α : Type*} [topological_space α] [nonempty α] {f : α → β}
(hf : continuous f) (hlim : tendsto f (cocompact α) at_bot) :
∃ x, ∀ y, f y ≤ f x :=
@continuous.exists_forall_le (order_dual β) _ _ _ _ _ _ _ hf hlim
end conditionally_complete_linear_order
end order_topology
/-!
### Bounded monotone sequences converge
In this section we prove a few theorems of the form “if the range of a monotone function `f : ι → α`
admits a least upper bound `a`, then `f x` tends to `a` as `x → ∞`”, as well as version of this
statement for (conditionally) complete lattices that use `⨆ x, f x` instead of `is_lub`.
These theorems work for linear orders with order topologies as well as their products (both in terms
of `prod` and in terms of function types). In order to reduce code duplication, we introduce two
typeclasses (one for the property formulated above and one for the dual property), prove theorems
assuming one of these typeclasses, and provide instances for linear orders and their products.
-/
/-- We say that `α` is a `Sup_convergence_class` if the following holds. Let `f : ι → α` be a
monotone function, let `a : α` be a least upper bound of `set.range f`. Then `f x` tends to `𝓝 a` as
`x → ∞` (formally, at the filter `filter.at_top`). We require this for `ι = (s : set α)`, `f = coe`
in the definition, then prove it for any `f` in `tendsto_at_top_is_lub`.
This property holds for linear orders with order topology as well as their products. -/
class Sup_convergence_class (α : Type*) [preorder α] [topological_space α] : Prop :=
(tendsto_coe_at_top_is_lub : ∀ (a : α) (s : set α), is_lub s a → tendsto (coe : s → α) at_top (𝓝 a))
/-- We say that `α` is an `Inf_convergence_class` if the following holds. Let `f : ι → α` be a
monotone function, let `a : α` be a greatest lower bound of `set.range f`. Then `f x` tends to `𝓝 a`
as `x → -∞` (formally, at the filter `filter.at_bot`). We require this for `ι = (s : set α)`,
`f = coe` in the definition, then prove it for any `f` in `tendsto_at_bot_is_glb`.
This property holds for linear orders with order topology as well as their products. -/
class Inf_convergence_class (α : Type*) [preorder α] [topological_space α] : Prop :=
(tendsto_coe_at_bot_is_glb : ∀ (a : α) (s : set α), is_glb s a → tendsto (coe : s → α) at_bot (𝓝 a))
instance order_dual.Sup_convergence_class [preorder α] [topological_space α]
[Inf_convergence_class α] : Sup_convergence_class (order_dual α) :=
⟨‹Inf_convergence_class α›.1⟩
instance order_dual.Inf_convergence_class [preorder α] [topological_space α]
[Sup_convergence_class α] : Inf_convergence_class (order_dual α) :=
⟨‹Sup_convergence_class α›.1⟩
@[priority 100] -- see Note [lower instance priority]
instance linear_order.Sup_convergence_class [topological_space α] [linear_order α]
[order_topology α] : Sup_convergence_class α :=
begin
refine ⟨λ a s ha, tendsto_order.2 ⟨λ b hb, _, λ b hb, _⟩⟩,
{ rcases ha.exists_between hb with ⟨c, hcs, bc, bca⟩,
lift c to s using hcs,
refine (eventually_ge_at_top c).mono (λ x hx, bc.trans_le hx) },
{ exact eventually_of_forall (λ x, (ha.1 x.2).trans_lt hb) }
end
@[priority 100] -- see Note [lower instance priority]
instance linear_order.Inf_convergence_class [topological_space α] [linear_order α]
[order_topology α] : Inf_convergence_class α :=
show Inf_convergence_class (order_dual $ order_dual α), from order_dual.Inf_convergence_class
section
variables {ι : Type*} [preorder ι] [topological_space α]
section is_lub
variables [preorder α] [Sup_convergence_class α] {f : ι → α} {a : α}
lemma tendsto_at_top_is_lub (h_mono : monotone f) (ha : is_lub (set.range f) a) :
tendsto f at_top (𝓝 a) :=
begin
suffices : tendsto (range_factorization f) at_top at_top,
from (Sup_convergence_class.tendsto_coe_at_top_is_lub _ _ ha).comp this,
exact h_mono.range_factorization.tendsto_at_top_at_top (λ b, b.2.imp $ λ a ha, ha.ge)
end
lemma tendsto_at_bot_is_lub (h_mono : monotone (order_dual.to_dual ∘ f))
(ha : is_lub (set.range f) a) : tendsto f at_bot (𝓝 a) :=
@tendsto_at_top_is_lub α (order_dual ι) _ _ _ _ f a h_mono.order_dual ha
end is_lub
section is_glb
variables [preorder α] [Inf_convergence_class α] {f : ι → α} {a : α}
lemma tendsto_at_bot_is_glb (h_mono : monotone f) (ha : is_glb (set.range f) a) :
tendsto f at_bot (𝓝 a) :=
@tendsto_at_top_is_lub (order_dual α) (order_dual ι) _ _ _ _ f a h_mono.order_dual ha
lemma tendsto_at_top_is_glb (h_mono : monotone (order_dual.to_dual ∘ f))
(ha : is_glb (set.range f) a) :
tendsto f at_top (𝓝 a) :=
@tendsto_at_top_is_lub (order_dual α) ι _ _ _ _ f a h_mono ha
end is_glb
section csupr
variables [conditionally_complete_lattice α] [Sup_convergence_class α] {f : ι → α} {a : α}
lemma tendsto_at_top_csupr (h_mono : monotone f) (hbdd : bdd_above $ range f) :
tendsto f at_top (𝓝 (⨆i, f i)) :=
begin
casesI is_empty_or_nonempty ι,
exacts [tendsto_of_is_empty, tendsto_at_top_is_lub h_mono (is_lub_csupr hbdd)]
end
lemma tendsto_at_bot_csupr (h_mono : monotone (order_dual.to_dual ∘ f))
(hbdd : bdd_above $ range f) :
tendsto f at_bot (𝓝 (⨆i, f i)) :=
@tendsto_at_top_csupr α (order_dual ι) _ _ _ _ _ h_mono.order_dual hbdd
end csupr
section cinfi
variables [conditionally_complete_lattice α] [Inf_convergence_class α] {f : ι → α} {a : α}
lemma tendsto_at_bot_cinfi (h_mono : monotone f) (hbdd : bdd_below $ range f) :
tendsto f at_bot (𝓝 (⨅i, f i)) :=
@tendsto_at_top_csupr (order_dual α) (order_dual ι) _ _ _ _ _ h_mono.order_dual hbdd
lemma tendsto_at_top_cinfi (h_mono : monotone (order_dual.to_dual ∘ f))
(hbdd : bdd_below $ range f) :
tendsto f at_top (𝓝 (⨅i, f i)) :=
@tendsto_at_top_csupr (order_dual α) ι _ _ _ _ _ h_mono hbdd
end cinfi
section supr
variables [complete_lattice α] [Sup_convergence_class α] {f : ι → α} {a : α}
lemma tendsto_at_top_supr (h_mono : monotone f) : tendsto f at_top (𝓝 (⨆i, f i)) :=
tendsto_at_top_csupr h_mono (order_top.bdd_above _)
lemma tendsto_at_bot_supr (h_mono : monotone (order_dual.to_dual ∘ f)) :
tendsto f at_bot (𝓝 (⨆i, f i)) :=
tendsto_at_bot_csupr h_mono (order_top.bdd_above _)
end supr
section infi
variables [complete_lattice α] [Inf_convergence_class α] {f : ι → α} {a : α}
lemma tendsto_at_bot_infi (h_mono : monotone f) : tendsto f at_bot (𝓝 (⨅i, f i)) :=
tendsto_at_bot_cinfi h_mono (order_bot.bdd_below _)
lemma tendsto_at_top_infi (h_mono : monotone (order_dual.to_dual ∘ f)) :
tendsto f at_top (𝓝 (⨅i, f i)) :=
tendsto_at_top_cinfi h_mono (order_bot.bdd_below _)
end infi
end
instance [preorder α] [preorder β] [topological_space α] [topological_space β]
[Sup_convergence_class α] [Sup_convergence_class β] : Sup_convergence_class (α × β) :=
begin
constructor,
rintro ⟨a, b⟩ s h,
rw [is_lub_prod, ← range_restrict, ← range_restrict] at h,
have A : tendsto (λ x : s, (x : α × β).1) at_top (𝓝 a),
from tendsto_at_top_is_lub (monotone_fst.restrict s) h.1,
have B : tendsto (λ x : s, (x : α × β).2) at_top (𝓝 b),
from tendsto_at_top_is_lub (monotone_snd.restrict s) h.2,
convert A.prod_mk_nhds B,
ext1 ⟨⟨x, y⟩, h⟩, refl
end
instance [preorder α] [preorder β] [topological_space α] [topological_space β]
[Inf_convergence_class α] [Inf_convergence_class β] : Inf_convergence_class (α × β) :=
show Inf_convergence_class (order_dual $ (order_dual α × order_dual β)),
from order_dual.Inf_convergence_class
instance {ι : Type*} {α : ι → Type*} [Π i, preorder (α i)] [Π i, topological_space (α i)]
[Π i, Sup_convergence_class (α i)] : Sup_convergence_class (Π i, α i) :=
begin
refine ⟨λ f s h, _⟩,
simp only [is_lub_pi, ← range_restrict] at h,
exact tendsto_pi.2 (λ i, tendsto_at_top_is_lub ((monotone_eval _).restrict _) (h i))
end
instance {ι : Type*} {α : ι → Type*} [Π i, preorder (α i)] [Π i, topological_space (α i)]
[Π i, Inf_convergence_class (α i)] : Inf_convergence_class (Π i, α i) :=
show Inf_convergence_class (order_dual $ Π i, order_dual (α i)),
from order_dual.Inf_convergence_class
instance pi.Sup_convergence_class' {ι : Type*} [preorder α] [topological_space α]
[Sup_convergence_class α] : Sup_convergence_class (ι → α) :=
pi.Sup_convergence_class
instance pi.Inf_convergence_class' {ι : Type*} [preorder α] [topological_space α]
[Inf_convergence_class α] : Inf_convergence_class (ι → α) :=
pi.Inf_convergence_class
lemma tendsto_of_monotone {ι α : Type*} [preorder ι] [topological_space α]
[conditionally_complete_linear_order α] [order_topology α] {f : ι → α} (h_mono : monotone f) :
tendsto f at_top at_top ∨ (∃ l, tendsto f at_top (𝓝 l)) :=
if H : bdd_above (range f) then or.inr ⟨_, tendsto_at_top_csupr h_mono H⟩
else or.inl $ tendsto_at_top_at_top_of_monotone' h_mono H
lemma tendsto_iff_tendsto_subseq_of_monotone {ι₁ ι₂ α : Type*} [semilattice_sup ι₁] [preorder ι₂]
[nonempty ι₁] [topological_space α] [conditionally_complete_linear_order α] [order_topology α]
[no_top_order α] {f : ι₂ → α} {φ : ι₁ → ι₂} {l : α} (hf : monotone f)
(hg : tendsto φ at_top at_top) :
tendsto f at_top (𝓝 l) ↔ tendsto (f ∘ φ) at_top (𝓝 l) :=
begin
split; intro h,
{ exact h.comp hg },
{ rcases tendsto_of_monotone hf with h' | ⟨l', hl'⟩,
{ exact (not_tendsto_at_top_of_tendsto_nhds h (h'.comp hg)).elim },
{ rwa tendsto_nhds_unique h (hl'.comp hg) } }
end
/-! The next family of results, such as `is_lub_of_tendsto` and `supr_eq_of_tendsto`, are converses
to the standard fact that bounded monotone functions converge. They state, that if a monotone
function `f` tends to `a` along `at_top`, then that value `a` is a least upper bound for the range
of `f`.
Related theorems above (`is_lub.is_lub_of_tendsto`, `is_glb.is_glb_of_tendsto` etc) cover the case
when `f x` tends to `a` as `x` tends to some point `b` in the domain. -/
lemma monotone.ge_of_tendsto {α β : Type*} [topological_space α] [preorder α]
[order_closed_topology α] [semilattice_sup β] {f : β → α} {a : α} (hf : monotone f)
(ha : tendsto f at_top (𝓝 a)) (b : β) :
f b ≤ a :=
begin
haveI : nonempty β := nonempty.intro b,
exact ge_of_tendsto ha ((eventually_ge_at_top b).mono (λ _ hxy, hf hxy))
end
lemma monotone.le_of_tendsto {α β : Type*} [topological_space α] [preorder α]
[order_closed_topology α] [semilattice_inf β] {f : β → α} {a : α} (hf : monotone f)
(ha : tendsto f at_bot (𝓝 a)) (b : β) :
a ≤ f b :=
@monotone.ge_of_tendsto (order_dual α) (order_dual β) _ _ _ _ f _ hf.order_dual ha b
lemma is_lub_of_tendsto {α β : Type*} [topological_space α] [preorder α] [order_closed_topology α]
[nonempty β] [semilattice_sup β] {f : β → α} {a : α} (hf : monotone f)
(ha : tendsto f at_top (𝓝 a)) :
is_lub (set.range f) a :=
begin
split,
{ rintros _ ⟨b, rfl⟩,
exact hf.ge_of_tendsto ha b },
{ exact λ _ hb, le_of_tendsto' ha (λ x, hb (set.mem_range_self x)) }
end
lemma is_glb_of_tendsto {α β : Type*} [topological_space α] [preorder α] [order_closed_topology α]
[nonempty β] [semilattice_inf β] {f : β → α} {a : α} (hf : monotone f)
(ha : tendsto f at_bot (𝓝 a)) :
is_glb (set.range f) a :=
@is_lub_of_tendsto (order_dual α) (order_dual β) _ _ _ _ _ _ _ hf.order_dual ha
lemma supr_eq_of_tendsto {α β} [topological_space α] [complete_linear_order α] [order_topology α]
[nonempty β] [semilattice_sup β] {f : β → α} {a : α} (hf : monotone f) :
tendsto f at_top (𝓝 a) → supr f = a :=
tendsto_nhds_unique (tendsto_at_top_supr hf)
lemma infi_eq_of_tendsto {α} [topological_space α] [complete_linear_order α] [order_topology α]
[nonempty β] [semilattice_sup β] {f : β → α} {a : α} (hf : ∀n m, n ≤ m → f m ≤ f n) :
tendsto f at_top (𝓝 a) → infi f = a :=
tendsto_nhds_unique (tendsto_at_top_infi hf)
lemma supr_eq_supr_subseq_of_monotone {ι₁ ι₂ α : Type*} [preorder ι₂] [complete_lattice α]
{l : filter ι₁} [l.ne_bot] {f : ι₂ → α} {φ : ι₁ → ι₂} (hf : monotone f)
(hφ : tendsto φ l at_top) :
(⨆ i, f i) = (⨆ i, f (φ i)) :=
le_antisymm
(supr_le_supr2 $ λ i, exists_imp_exists (λ j (hj : i ≤ φ j), hf hj)
(hφ.eventually $ eventually_ge_at_top i).exists)
(supr_le_supr2 $ λ i, ⟨φ i, le_refl _⟩)
lemma infi_eq_infi_subseq_of_monotone {ι₁ ι₂ α : Type*} [preorder ι₂] [complete_lattice α]
{l : filter ι₁} [l.ne_bot] {f : ι₂ → α} {φ : ι₁ → ι₂} (hf : monotone f)
(hφ : tendsto φ l at_bot) :
(⨅ i, f i) = (⨅ i, f (φ i)) :=
supr_eq_supr_subseq_of_monotone hf.order_dual hφ
@[to_additive] lemma tendsto_inv_nhds_within_Ioi [ordered_comm_group α]
[topological_space α] [topological_group α] {a : α} :
tendsto has_inv.inv (𝓝[Ioi a] a) (𝓝[Iio (a⁻¹)] (a⁻¹)) :=
(continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal]
@[to_additive] lemma tendsto_inv_nhds_within_Iio [ordered_comm_group α]
[topological_space α] [topological_group α] {a : α} :
tendsto has_inv.inv (𝓝[Iio a] a) (𝓝[Ioi (a⁻¹)] (a⁻¹)) :=
(continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal]
@[to_additive] lemma tendsto_inv_nhds_within_Ioi_inv [ordered_comm_group α]
[topological_space α] [topological_group α] {a : α} :
tendsto has_inv.inv (𝓝[Ioi (a⁻¹)] (a⁻¹)) (𝓝[Iio a] a) :=
by simpa only [inv_inv] using @tendsto_inv_nhds_within_Ioi _ _ _ _ (a⁻¹)
@[to_additive] lemma tendsto_inv_nhds_within_Iio_inv [ordered_comm_group α]
[topological_space α] [topological_group α] {a : α} :
tendsto has_inv.inv (𝓝[Iio (a⁻¹)] (a⁻¹)) (𝓝[Ioi a] a) :=
by simpa only [inv_inv] using @tendsto_inv_nhds_within_Iio _ _ _ _ (a⁻¹)
@[to_additive] lemma tendsto_inv_nhds_within_Ici [ordered_comm_group α]
[topological_space α] [topological_group α] {a : α} :
tendsto has_inv.inv (𝓝[Ici a] a) (𝓝[Iic (a⁻¹)] (a⁻¹)) :=
(continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal]
@[to_additive] lemma tendsto_inv_nhds_within_Iic [ordered_comm_group α]
[topological_space α] [topological_group α] {a : α} :
tendsto has_inv.inv (𝓝[Iic a] a) (𝓝[Ici (a⁻¹)] (a⁻¹)) :=
(continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal]
@[to_additive] lemma tendsto_inv_nhds_within_Ici_inv [ordered_comm_group α]
[topological_space α] [topological_group α] {a : α} :
tendsto has_inv.inv (𝓝[Ici (a⁻¹)] (a⁻¹)) (𝓝[Iic a] a) :=
by simpa only [inv_inv] using @tendsto_inv_nhds_within_Ici _ _ _ _ (a⁻¹)
@[to_additive] lemma tendsto_inv_nhds_within_Iic_inv [ordered_comm_group α]
[topological_space α] [topological_group α] {a : α} :
tendsto has_inv.inv (𝓝[Iic (a⁻¹)] (a⁻¹)) (𝓝[Ici a] a) :=
by simpa only [inv_inv] using @tendsto_inv_nhds_within_Iic _ _ _ _ (a⁻¹)
lemma nhds_left_sup_nhds_right (a : α) [topological_space α] [linear_order α] :
𝓝[Iic a] a ⊔ 𝓝[Ici a] a = 𝓝 a :=
by rw [← nhds_within_union, Iic_union_Ici, nhds_within_univ]
lemma nhds_left'_sup_nhds_right (a : α) [topological_space α] [linear_order α] :
𝓝[Iio a] a ⊔ 𝓝[Ici a] a = 𝓝 a :=
by rw [← nhds_within_union, Iio_union_Ici, nhds_within_univ]
lemma nhds_left_sup_nhds_right' (a : α) [topological_space α] [linear_order α] :
𝓝[Iic a] a ⊔ 𝓝[Ioi a] a = 𝓝 a :=
by rw [← nhds_within_union, Iic_union_Ioi, nhds_within_univ]
lemma continuous_at_iff_continuous_left_right [topological_space α] [linear_order α]
[topological_space β] {a : α} {f : α → β} :
continuous_at f a ↔ continuous_within_at f (Iic a) a ∧ continuous_within_at f (Ici a) a :=
by simp only [continuous_within_at, continuous_at, ← tendsto_sup, nhds_left_sup_nhds_right]
lemma continuous_within_at_Ioi_iff_Ici {α β : Type*} [topological_space α] [partial_order α]
[topological_space β] {a : α} {f : α → β} :
continuous_within_at f (Ioi a) a ↔ continuous_within_at f (Ici a) a :=
by simp only [← Ici_diff_left, continuous_within_at_diff_self]
lemma continuous_within_at_Iio_iff_Iic
{α β : Type*} [topological_space α] [linear_order α] [topological_space β] {a : α} {f : α → β} :
continuous_within_at f (Iio a) a ↔ continuous_within_at f (Iic a) a :=
begin
have := @continuous_within_at_Ioi_iff_Ici (order_dual α) _ _ _ _ _ f,
erw [dual_Ici, dual_Ioi] at this,
exact this,
end
lemma continuous_at_iff_continuous_left'_right' [topological_space α] [linear_order α]
[topological_space β] {a : α} {f : α → β} :
continuous_at f a ↔ continuous_within_at f (Iio a) a ∧ continuous_within_at f (Ioi a) a :=
by rw [continuous_within_at_Ioi_iff_Ici, continuous_within_at_Iio_iff_Iic,
continuous_at_iff_continuous_left_right]
/-!
### Continuity of monotone functions
In this section we prove the following fact: if `f` is a monotone function on a neighborhood of `a`
and the image of this neighborhood is a neighborhood of `f a`, then `f` is continuous at `a`, see
`continuous_at_of_mono_incr_on_of_image_mem_nhds`, as well as several similar facts.
-/
section linear_order
variables [linear_order α] [topological_space α] [order_topology α]
variables [linear_order β] [topological_space β] [order_topology β]
/-- If `f` is a function strictly monotonically increasing on a right neighborhood of `a` and the
image of this neighborhood under `f` meets every interval `(f a, b]`, `b > f a`, then `f` is
continuous at `a` from the right.
The assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b` is required because otherwise the
function `f : ℝ → ℝ` given by `f x = if x ≤ 0 then x else x + 1` would be a counter-example at
`a = 0`. -/
lemma strict_mono_incr_on.continuous_at_right_of_exists_between {f : α → β} {s : set α} {a : α}
(h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Ici a] a)
(hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b) :
continuous_within_at f (Ici a) a :=
begin
have ha : a ∈ Ici a := left_mem_Ici,
have has : a ∈ s := mem_of_mem_nhds_within ha hs,
refine tendsto_order.2 ⟨λ b hb, _, λ b hb, _⟩,
{ filter_upwards [hs, self_mem_nhds_within],
intros x hxs hxa,
exact hb.trans_le ((h_mono.le_iff_le has hxs).2 hxa) },
{ rcases hfs b hb with ⟨c, hcs, hac, hcb⟩,
rw [h_mono.lt_iff_lt has hcs] at hac,
filter_upwards [hs, Ico_mem_nhds_within_Ici (left_mem_Ico.2 hac)],
rintros x hx ⟨hax, hxc⟩,
exact ((h_mono.lt_iff_lt hx hcs).2 hxc).trans_le hcb }
end
/-- If `f` is a function monotonically increasing function on a right neighborhood of `a` and the
image of this neighborhood under `f` meets every interval `(f a, b)`, `b > f a`, then `f` is
continuous at `a` from the right.
The assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b` cannot be replaced by the weaker
assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b` we use for strictly monotone functions
because otherwise the function `ceil : ℝ → ℤ` would be a counter-example at `a = 0`. -/
lemma continuous_at_right_of_mono_incr_on_of_exists_between {f : α → β} {s : set α} {a : α}
(h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y) (hs : s ∈ 𝓝[Ici a] a)
(hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b) :
continuous_within_at f (Ici a) a :=
begin
have ha : a ∈ Ici a := left_mem_Ici,
have has : a ∈ s := mem_of_mem_nhds_within ha hs,
refine tendsto_order.2 ⟨λ b hb, _, λ b hb, _⟩,
{ filter_upwards [hs, self_mem_nhds_within],
intros x hxs hxa,
exact hb.trans_le (h_mono _ has _ hxs hxa) },
{ rcases hfs b hb with ⟨c, hcs, hac, hcb⟩,
have : a < c, from not_le.1 (λ h, hac.not_le $ h_mono _ hcs _ has h),
filter_upwards [hs, Ico_mem_nhds_within_Ici (left_mem_Ico.2 this)],
rintros x hx ⟨hax, hxc⟩,
exact (h_mono _ hx _ hcs hxc.le).trans_lt hcb }
end
/-- If a function `f` with a densely ordered codomain is monotonically increasing on a right
neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right
neighborhood of `f a`, then `f` is continuous at `a` from the right. -/
lemma continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within [densely_ordered β]
{f : α → β} {s : set α} {a : α} (h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y)
(hs : s ∈ 𝓝[Ici a] a) (hfs : closure (f '' s) ∈ 𝓝[Ici (f a)] (f a)) :
continuous_within_at f (Ici a) a :=
begin
refine continuous_at_right_of_mono_incr_on_of_exists_between h_mono hs (λ b hb, _),
rcases (mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset hb).1 hfs with ⟨b', ⟨hab', hbb'⟩, hb'⟩,
rcases exists_between hab' with ⟨c', hc'⟩,
rcases mem_closure_iff.1 (hb' ⟨hc'.1.le, hc'.2⟩) (Ioo (f a) b') is_open_Ioo hc'
with ⟨_, hc, ⟨c, hcs, rfl⟩⟩,
exact ⟨c, hcs, hc.1, hc.2.trans_le hbb'⟩
end
/-- If a function `f` with a densely ordered codomain is monotonically increasing on a right
neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of `f a`,
then `f` is continuous at `a` from the right. -/
lemma continuous_at_right_of_mono_incr_on_of_image_mem_nhds_within [densely_ordered β] {f : α → β}
{s : set α} {a : α} (h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y) (hs : s ∈ 𝓝[Ici a] a)
(hfs : f '' s ∈ 𝓝[Ici (f a)] (f a)) :
continuous_within_at f (Ici a) a :=
continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono hs $
mem_of_superset hfs subset_closure
/-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a
right neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right
neighborhood of `f a`, then `f` is continuous at `a` from the right. -/
lemma strict_mono_incr_on.continuous_at_right_of_closure_image_mem_nhds_within [densely_ordered β]
{f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Ici a] a)
(hfs : closure (f '' s) ∈ 𝓝[Ici (f a)] (f a)) :
continuous_within_at f (Ici a) a :=
continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within
(λ x hx y hy, (h_mono.le_iff_le hx hy).2) hs hfs
/-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a
right neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of
`f a`, then `f` is continuous at `a` from the right. -/
lemma strict_mono_incr_on.continuous_at_right_of_image_mem_nhds_within [densely_ordered β]
{f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Ici a] a)
(hfs : f '' s ∈ 𝓝[Ici (f a)] (f a)) :
continuous_within_at f (Ici a) a :=
h_mono.continuous_at_right_of_closure_image_mem_nhds_within hs
(mem_of_superset hfs subset_closure)
/-- If a function `f` is strictly monotonically increasing on a right neighborhood of `a` and the
image of this neighborhood under `f` includes `Ioi (f a)`, then `f` is continuous at `a` from the
right. -/
lemma strict_mono_incr_on.continuous_at_right_of_surj_on {f : α → β} {s : set α} {a : α}
(h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : surj_on f s (Ioi (f a))) :
continuous_within_at f (Ici a) a :=
h_mono.continuous_at_right_of_exists_between hs $ λ b hb, let ⟨c, hcs, hcb⟩ := hfs hb in
⟨c, hcs, hcb.symm ▸ hb, hcb.le⟩
/-- If `f` is a function strictly monotonically increasing on a left neighborhood of `a` and the
image of this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, then `f` is
continuous at `a` from the left.
The assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)` is required because otherwise the
function `f : ℝ → ℝ` given by `f x = if x < 0 then x else x + 1` would be a counter-example at
`a = 0`. -/
lemma strict_mono_incr_on.continuous_at_left_of_exists_between {f : α → β} {s : set α} {a : α}
(h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Iic a] a)
(hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)) :
continuous_within_at f (Iic a) a :=
h_mono.dual.continuous_at_right_of_exists_between hs $
λ b hb, let ⟨c, hcs, hcb, hca⟩ := hfs b hb in ⟨c, hcs, hca, hcb⟩
/-- If `f` is a function monotonically increasing function on a left neighborhood of `a` and the
image of this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, then `f` is
continuous at `a` from the left.
The assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)` cannot be replaced by the weaker
assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)` we use for strictly monotone functions
because otherwise the function `floor : ℝ → ℤ` would be a counter-example at `a = 0`. -/
lemma continuous_at_left_of_mono_incr_on_of_exists_between {f : α → β} {s : set α} {a : α}
(h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y) (hs : s ∈ 𝓝[Iic a] a)
(hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)) :
continuous_within_at f (Iic a) a :=
@continuous_at_right_of_mono_incr_on_of_exists_between (order_dual α) (order_dual β) _ _ _ _ _ _
f s a (λ x hx y hy, h_mono y hy x hx) hs $
λ b hb, let ⟨c, hcs, hcb, hca⟩ := hfs b hb in ⟨c, hcs, hca, hcb⟩
/-- If a function `f` with a densely ordered codomain is monotonically increasing on a left
neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left
neighborhood of `f a`, then `f` is continuous at `a` from the left -/
lemma continuous_at_left_of_mono_incr_on_of_closure_image_mem_nhds_within [densely_ordered β]
{f : α → β} {s : set α} {a : α} (h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y)
(hs : s ∈ 𝓝[Iic a] a) (hfs : closure (f '' s) ∈ 𝓝[Iic (f a)] (f a)) :
continuous_within_at f (Iic a) a :=
@continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within (order_dual α) (order_dual β)
_ _ _ _ _ _ _ f s a (λ x hx y hy, h_mono y hy x hx) hs hfs
/-- If a function `f` with a densely ordered codomain is monotonically increasing on a left
neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of `f a`,
then `f` is continuous at `a` from the left. -/
lemma continuous_at_left_of_mono_incr_on_of_image_mem_nhds_within [densely_ordered β]
{f : α → β} {s : set α} {a : α} (h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y)
(hs : s ∈ 𝓝[Iic a] a) (hfs : f '' s ∈ 𝓝[Iic (f a)] (f a)) :
continuous_within_at f (Iic a) a :=
continuous_at_left_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono hs
(mem_of_superset hfs subset_closure)
/-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a
left neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left
neighborhood of `f a`, then `f` is continuous at `a` from the left. -/
lemma strict_mono_incr_on.continuous_at_left_of_closure_image_mem_nhds_within [densely_ordered β]
{f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Iic a] a)
(hfs : closure (f '' s) ∈ 𝓝[Iic (f a)] (f a)) :
continuous_within_at f (Iic a) a :=
h_mono.dual.continuous_at_right_of_closure_image_mem_nhds_within hs hfs
/-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a
left neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of
`f a`, then `f` is continuous at `a` from the left. -/
lemma strict_mono_incr_on.continuous_at_left_of_image_mem_nhds_within [densely_ordered β]
{f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Iic a] a)
(hfs : f '' s ∈ 𝓝[Iic (f a)] (f a)) :
continuous_within_at f (Iic a) a :=
h_mono.dual.continuous_at_right_of_image_mem_nhds_within hs hfs
/-- If a function `f` is strictly monotonically increasing on a left neighborhood of `a` and the
image of this neighborhood under `f` includes `Iio (f a)`, then `f` is continuous at `a` from the
left. -/
lemma strict_mono_incr_on.continuous_at_left_of_surj_on {f : α → β} {s : set α} {a : α}
(h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : surj_on f s (Iio (f a))) :
continuous_within_at f (Iic a) a :=
h_mono.dual.continuous_at_right_of_surj_on hs hfs
/-- If a function `f` is strictly monotonically increasing on a neighborhood of `a` and the image of
this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, and every interval
`(f a, b]`, `b > f a`, then `f` is continuous at `a`. -/
lemma strict_mono_incr_on.continuous_at_of_exists_between {f : α → β} {s : set α} {a : α}
(h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝 a)
(hfs_l : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)) (hfs_r : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b) :
continuous_at f a :=
continuous_at_iff_continuous_left_right.2
⟨h_mono.continuous_at_left_of_exists_between (mem_nhds_within_of_mem_nhds hs) hfs_l,
h_mono.continuous_at_right_of_exists_between (mem_nhds_within_of_mem_nhds hs) hfs_r⟩
/-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a
neighborhood of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of
`f a`, then `f` is continuous at `a`. -/
lemma strict_mono_incr_on.continuous_at_of_closure_image_mem_nhds [densely_ordered β] {f : α → β}
{s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝 a)
(hfs : closure (f '' s) ∈ 𝓝 (f a)) :
continuous_at f a :=
continuous_at_iff_continuous_left_right.2
⟨h_mono.continuous_at_left_of_closure_image_mem_nhds_within (mem_nhds_within_of_mem_nhds hs)
(mem_nhds_within_of_mem_nhds hfs),
h_mono.continuous_at_right_of_closure_image_mem_nhds_within (mem_nhds_within_of_mem_nhds hs)
(mem_nhds_within_of_mem_nhds hfs)⟩
/-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a
neighborhood of `a` and the image of this set under `f` is a neighborhood of `f a`, then `f` is
continuous at `a`. -/
lemma strict_mono_incr_on.continuous_at_of_image_mem_nhds [densely_ordered β] {f : α → β}
{s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝 a) (hfs : f '' s ∈ 𝓝 (f a)) :
continuous_at f a :=
h_mono.continuous_at_of_closure_image_mem_nhds hs (mem_of_superset hfs subset_closure)
/-- If `f` is a function monotonically increasing function on a neighborhood of `a` and the image of
this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, and every interval `(f a,
b)`, `b > f a`, then `f` is continuous at `a`. -/
lemma continuous_at_of_mono_incr_on_of_exists_between {f : α → β} {s : set α} {a : α}
(h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y) (hs : s ∈ 𝓝 a)
(hfs_l : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)) (hfs_r : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b) :
continuous_at f a :=
continuous_at_iff_continuous_left_right.2
⟨continuous_at_left_of_mono_incr_on_of_exists_between h_mono
(mem_nhds_within_of_mem_nhds hs) hfs_l,
continuous_at_right_of_mono_incr_on_of_exists_between h_mono
(mem_nhds_within_of_mem_nhds hs) hfs_r⟩
/-- If a function `f` with a densely ordered codomain is monotonically increasing on a neighborhood
of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of `f a`, then
`f` is continuous at `a`. -/
lemma continuous_at_of_mono_incr_on_of_closure_image_mem_nhds [densely_ordered β] {f : α → β}
{s : set α} {a : α} (h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y) (hs : s ∈ 𝓝 a)
(hfs : closure (f '' s) ∈ 𝓝 (f a)) :
continuous_at f a :=
continuous_at_iff_continuous_left_right.2
⟨continuous_at_left_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono
(mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs),
continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono
(mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs)⟩
/-- If a function `f` with a densely ordered codomain is monotonically increasing on a neighborhood
of `a` and the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is
continuous at `a`. -/
lemma continuous_at_of_mono_incr_on_of_image_mem_nhds [densely_ordered β] {f : α → β}
{s : set α} {a : α} (h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y) (hs : s ∈ 𝓝 a)
(hfs : f '' s ∈ 𝓝 (f a)) :
continuous_at f a :=
continuous_at_of_mono_incr_on_of_closure_image_mem_nhds h_mono hs
(mem_of_superset hfs subset_closure)
/-- A monotone function with densely ordered codomain and a dense range is continuous. -/
lemma monotone.continuous_of_dense_range [densely_ordered β] {f : α → β}
(h_mono : monotone f) (h_dense : dense_range f) :
continuous f :=
continuous_iff_continuous_at.mpr $ λ a,
continuous_at_of_mono_incr_on_of_closure_image_mem_nhds (λ x hx y hy hxy, h_mono hxy)
univ_mem $ by simp only [image_univ, h_dense.closure_eq, univ_mem]
/-- A monotone surjective function with a densely ordered codomain is surjective. -/
lemma monotone.continuous_of_surjective [densely_ordered β] {f : α → β} (h_mono : monotone f)
(h_surj : function.surjective f) :
continuous f :=
h_mono.continuous_of_dense_range h_surj.dense_range
end linear_order
/-!
### Continuity of order isomorphisms
In this section we prove that an `order_iso` is continuous, hence it is a `homeomorph`. We prove
this for an `order_iso` between to partial orders with order topology.
-/
namespace order_iso
variables [partial_order α] [partial_order β] [topological_space α] [topological_space β]
[order_topology α] [order_topology β]
protected lemma continuous (e : α ≃o β) : continuous e :=
begin
rw [‹order_topology β›.topology_eq_generate_intervals],
refine continuous_generated_from (λ s hs, _),
rcases hs with ⟨a, rfl|rfl⟩,
{ rw e.preimage_Ioi, apply is_open_lt' },
{ rw e.preimage_Iio, apply is_open_gt' }
end
/-- An order isomorphism between two linear order `order_topology` spaces is a homeomorphism. -/
def to_homeomorph (e : α ≃o β) : α ≃ₜ β :=
{ continuous_to_fun := e.continuous,
continuous_inv_fun := e.symm.continuous,
.. e }
@[simp] lemma coe_to_homeomorph (e : α ≃o β) : ⇑e.to_homeomorph = e := rfl
@[simp] lemma coe_to_homeomorph_symm (e : α ≃o β) : ⇑e.to_homeomorph.symm = e.symm := rfl
end order_iso
|
ef70e9394ec7fe2e98865f519481053b08ed7062 | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/analysis/analytic/linear.lean | 71bea5f12b33438b23d37231c3c0dae4fa99bdf8 | [
"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 | 4,272 | lean | /-
Copyright (c) 2021 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import analysis.analytic.basic
/-!
# Linear functions are analytic
In this file we prove that a `continuous_linear_map` defines an analytic function with
the formal power series `f x = f a + f (x - a)`.
-/
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{F : Type*} [normed_group F] [normed_space 𝕜 F]
{G : Type*} [normed_group G] [normed_space 𝕜 G]
open_locale topological_space classical big_operators nnreal ennreal
open set filter asymptotics
noncomputable theory
namespace continuous_linear_map
/-- Formal power series of a continuous linear map `f : E →L[𝕜] F` at `x : E`:
`f y = f x + f (y - x)`. -/
@[simp] def fpower_series (f : E →L[𝕜] F) (x : E) : formal_multilinear_series 𝕜 E F
| 0 := continuous_multilinear_map.curry0 𝕜 _ (f x)
| 1 := (continuous_multilinear_curry_fin1 𝕜 E F).symm f
| _ := 0
@[simp] lemma fpower_series_apply_add_two (f : E →L[𝕜] F) (x : E) (n : ℕ) :
f.fpower_series x (n + 2) = 0 := rfl
@[simp] lemma fpower_series_radius (f : E →L[𝕜] F) (x : E) : (f.fpower_series x).radius = ∞ :=
(f.fpower_series x).radius_eq_top_of_forall_image_add_eq_zero 2 $ λ n, rfl
protected theorem has_fpower_series_on_ball (f : E →L[𝕜] F) (x : E) :
has_fpower_series_on_ball f (f.fpower_series x) x ∞ :=
{ r_le := by simp,
r_pos := ennreal.coe_lt_top,
has_sum := λ y _, (has_sum_nat_add_iff' 2).1 $
by simp [finset.sum_range_succ, ← sub_sub, has_sum_zero] }
protected theorem has_fpower_series_at (f : E →L[𝕜] F) (x : E) :
has_fpower_series_at f (f.fpower_series x) x :=
⟨∞, f.has_fpower_series_on_ball x⟩
protected theorem analytic_at (f : E →L[𝕜] F) (x : E) : analytic_at 𝕜 f x :=
(f.has_fpower_series_at x).analytic_at
/-- Reinterpret a bilinear map `f : E →L[𝕜] F →L[𝕜] G` as a multilinear map
`(E × F) [×2]→L[𝕜] G`. This multilinear map is the second term in the formal
multilinear series expansion of `uncurry f`. It is given by
`f.uncurry_bilinear ![(x, y), (x', y')] = f x y'`. -/
def uncurry_bilinear (f : E →L[𝕜] F →L[𝕜] G) : (E × F) [×2]→L[𝕜] G :=
@continuous_linear_map.uncurry_left 𝕜 1 (λ _, E × F) G _ _ _ _ _ $
(↑(continuous_multilinear_curry_fin1 𝕜 (E × F) G).symm : (E × F →L[𝕜] G) →L[𝕜] _).comp $
f.bilinear_comp (fst _ _ _) (snd _ _ _)
@[simp] lemma uncurry_bilinear_apply (f : E →L[𝕜] F →L[𝕜] G) (m : fin 2 → E × F) :
f.uncurry_bilinear m = f (m 0).1 (m 1).2 :=
rfl
/-- Formal multilinear series expansion of a bilinear function `f : E →L[𝕜] F →L[𝕜] G`. -/
@[simp] def fpower_series_bilinear (f : E →L[𝕜] F →L[𝕜] G) (x : E × F) :
formal_multilinear_series 𝕜 (E × F) G
| 0 := continuous_multilinear_map.curry0 𝕜 _ (f x.1 x.2)
| 1 := (continuous_multilinear_curry_fin1 𝕜 (E × F) G).symm (f.deriv₂ x)
| 2 := f.uncurry_bilinear
| _ := 0
@[simp] lemma fpower_series_bilinear_radius (f : E →L[𝕜] F →L[𝕜] G) (x : E × F) :
(f.fpower_series_bilinear x).radius = ∞ :=
(f.fpower_series_bilinear x).radius_eq_top_of_forall_image_add_eq_zero 3 $ λ n, rfl
protected theorem has_fpower_series_on_ball_bilinear (f : E →L[𝕜] F →L[𝕜] G) (x : E × F) :
has_fpower_series_on_ball (λ x : E × F, f x.1 x.2) (f.fpower_series_bilinear x) x ∞ :=
{ r_le := by simp,
r_pos := ennreal.coe_lt_top,
has_sum := λ y _, (has_sum_nat_add_iff' 3).1 $
begin
simp only [finset.sum_range_succ, finset.sum_range_one, prod.fst_add, prod.snd_add,
f.map_add₂],
dsimp, simp only [add_comm, add_left_comm, sub_self, has_sum_zero]
end }
protected theorem has_fpower_series_at_bilinear (f : E →L[𝕜] F →L[𝕜] G) (x : E × F) :
has_fpower_series_at (λ x : E × F, f x.1 x.2) (f.fpower_series_bilinear x) x :=
⟨∞, f.has_fpower_series_on_ball_bilinear x⟩
protected theorem analytic_at_bilinear (f : E →L[𝕜] F →L[𝕜] G) (x : E × F) :
analytic_at 𝕜 (λ x : E × F, f x.1 x.2) x :=
(f.has_fpower_series_at_bilinear x).analytic_at
end continuous_linear_map
|
443682843582c912d96454f0db64e34356c607cd | a7602958ab456501ff85db8cf5553f7bcab201d7 | /Notes/Theorem_Proving_in_Lean/Chapter2/2.3.2.lean | 642370c875641ba9c47c78b948a313747d63b137 | [] | 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,294 | lean | -- 2.3 Function Abstraction and Evaluation ...continued
-- So far we've only seen how to #check the type of functions,
-- and the type of their evaluations, when applied to some
-- expression. But...what about the value? Well, we can use
-- #reduce.
constants α β γ: Type
constant f: α → β
constant g: β → β → γ
constant h: α → α
constants (a: α) (b: β)
#check fun (a: α), a -- Of type: α → α
#check (fun (a: α), a) a -- Of type: α
-- Wait for it...
#reduce (fun (x: α), x) a -- Value: a. Woo!
-- Another...
#check (fun (x: α) (y: β), g (f x) y) -- α → β → γ
-- Since we're applying the function on `a b` variables, the type
-- we get back should be γ.
#check (fun (x: α) (y: β), g (f x) y) a b
-- And more specifically, the value should be the function invocations
-- with the specific variables we've given:
#reduce (fun (x: α) (y: β), g (f x) y) a b
-------------------------------------------------------
constants m n: ℕ
#check (m, n).1 -- ℕ.
#reduce (m, n).1 -- m.
-- The important bit about all of this is that every term in dependent
-- type theory supports the notion of reduction, or
-- normalization. This reduction is essentially "carrying out all the
-- computational reductions sanctioned by the underlying logic". |
dd39b11675ea05c01d99089e97cb5f9f0ada0f94 | 9d2e3d5a2e2342a283affd97eead310c3b528a24 | /src/hints/thursday/afternoon/category_theory/exercise2/hint1.lean | 57ab254413a3dec6fd04d20ba695f50ebfd7a00d | [] | 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 | 554 | lean | import algebra.category.CommRing.basic
import data.polynomial
noncomputable theory -- the default implementation of polynomials is noncomputable
local attribute [irreducible] polynomial.eval₂
def Ring.polynomial : Ring ⥤ Ring :=
-- We need to build a functor, so let's fill in the data fields with `sorry`
{ obj := sorry,
map := sorry, }
def CommRing.polynomial : CommRing ⥤ CommRing :=
sorry
open category_theory
def commutes :
(forget₂ CommRing Ring) ⋙ Ring.polynomial ≅ CommRing.polynomial ⋙ (forget₂ CommRing Ring) :=
sorry
|
e33361ccebca6fe717d178b2b6a4df48cc94e9b5 | 2bafba05c98c1107866b39609d15e849a4ca2bb8 | /src/week_2/kb_solutions/Part_B_subgroups_solutions.lean | 9d0740f3f8843d667b7e0cfca1daaff5d2d0504a | [
"Apache-2.0"
] | permissive | ImperialCollegeLondon/formalising-mathematics | b54c83c94b5c315024ff09997fcd6b303892a749 | 7cf1d51c27e2038d2804561d63c74711924044a1 | refs/heads/master | 1,651,267,046,302 | 1,638,888,459,000 | 1,638,888,459,000 | 331,592,375 | 284 | 24 | Apache-2.0 | 1,669,593,705,000 | 1,611,224,849,000 | Lean | UTF-8 | Lean | false | false | 20,454 | lean | import week_2.kb_solutions.Part_A_groups_solutions
/-!
## Subgroups
We define the structure `subgroup G`, whose terms are subgroups of `G`.
A subgroup of `G` is implemented as a subset of `G` closed under
`1`, `*` and `⁻¹`.
-/
namespace xena
/-- A subgroup of a group G is a subset containing 1
and closed under multiplication and inverse. -/
structure subgroup (G : Type) [group G] :=
(carrier : set G)
(one_mem' : (1 : G) ∈ carrier)
(mul_mem' {x y} : x ∈ carrier → y ∈ carrier → x * y ∈ carrier)
(inv_mem' {x} : x ∈ carrier → x⁻¹ ∈ carrier)
/-
At this point, here's what we have.
A term `H` of type `subgroup G`, written `H : subgroup G`, is a
*quadruple*. To give a term `H : subgroup G` is to give the following four things:
1) `H.carrier` (a subset of `G`),
2) `H.one_mem'` (a proof that `1 ∈ H.carrier`),
3) `H.mul_mem'` (a proof that `H` is closed under multiplication)
4) `H.inv_mem'` (a proof that `H` is closed under inverses).
Note in particular that Lean, being super-pedantic, *distinguishes* between
the subgroup `H` and the subset `H.carrier`. One is a subgroup, one is
a subset. When we get going we will start by setting up some infrastructure
so that this difference will be hard to notice.
Note also that if `x` is in the subgroup `H` of `H` then the _type_ of `x` is still `G`,
and `x ∈ carrier` is a Proposition. Note also that `x : carrier` doesn't
make sense (`carrier` is a term, not a type, rather counterintuitively).
-/
namespace subgroup
open xena.group
-- let G be a group and let H and K be subgroups
variables {G : Type} [group G] (H J K : subgroup G)
/-
This `h ∈ H.carrier` notation kind of stinks. I don't want to write `H.carrier`
everywhere, because I want to be able to identify the subgroup `H` with
its underlying subset `H.carrier`. Note that these things are not _equal_,
firstly because `H` contains the proof that `H.carrier` is a subgroup, and
secondly because these terms have different types! `H` has type `subgroup G`
and `H.carrier` has type `set G`. Let's start by sorting this out.
-/
-- If `x : G` and `H : subgroup G` then let's define the notation
-- `x ∈ H` to mean `x ∈ H.carrier`
instance : has_mem G (subgroup G) := ⟨λ m H, m ∈ H.carrier⟩
-- Let's also define a "coercion", a.k.a. an "invisible map"
-- from subgroups of `G` to subsets of `G`, sending `H` to `H.carrier`.
-- The map is not completely invisible -- it's a little ↑. So
-- if you see `↑H` in the future, it means the subset `H.carrier` by definition.
instance : has_coe (subgroup G) (set G) := ⟨λ H, H.carrier⟩
-- `λ` is just computer science notation for ↦ (mapsto); so
-- `λ H, H.carrier` is the function `H ↦ H.carrier`.
-- Let's check we have this working, and also tell the simplifier that we
-- would rather talk about `g ∈ H` than any other way of saying it.
/-- `g` is in the underlying subset of `H` iff `g ∈ H`. -/
@[simp] lemma mem_carrier {g : G} : g ∈ H.carrier ↔ g ∈ H :=
begin
-- true by definition
refl
end
/-- `g` is in `H` considered as a subset of `G`, iff `g` is in `H` considered
as subgroup of `G`. -/
@[simp] lemma mem_coe {g : G} : g ∈ (↑H : set G) ↔ g ∈ H :=
begin
-- true by definition
refl
end
-- Now let's define theorems without the `'`s in, which use this
-- more natural notation
/-- A subgroup contains the group's 1. -/
theorem one_mem : (1 : G) ∈ H :=
begin
apply H.one_mem',
end
/-- A subgroup is closed under multiplication. -/
theorem mul_mem {x y : G} : x ∈ H → y ∈ H → x * y ∈ H :=
begin
apply H.mul_mem',
end
/-- A subgroup is closed under inverse -/
theorem inv_mem {x : G} : x ∈ H → x⁻¹ ∈ H :=
begin
apply H.inv_mem',
end
/-
So here are the three theorems which you need to remember about subgroups.
Say `H : subgroup G`. Then:
`H.one_mem : (1 : G) ∈ H`
`H.mul_mem {x y : G} : x ∈ H → y ∈ H → x * y ∈ H`
`H.inv_mem {x : G} : x ∈ H → x⁻¹ ∈ H`
These now look like the way a mathematician would write things.
Now let's start to prove basic theorems about subgroups (or, as a the computer
scientists would say, make a basic _interface_ or _API_ for subgroups),
using this sensible notation.
Here's an example; let's prove `x ∈ H ↔ x⁻¹ ∈ H`. Let's put the more
complicated expression on the left hand side of the `↔` though, because then
we can make it a `simp` lemma.
-/
-- Remember that `xena.group.inv_inv x` is the statement that `x⁻¹⁻¹ = x`
@[simp] theorem inv_mem_iff {x : G} : x⁻¹ ∈ H ↔ x ∈ H :=
begin
split,
{ intro h,
rw ← xena.group.inv_inv x,
apply H.inv_mem,
assumption },
{ apply H.inv_mem },
end
-- We could prove a bunch more theorems here. Let's just do one more.
-- Let's show that if x and xy are in H then so is y.
theorem mem_of_mem_mul_mem {x y : G} (hx : x ∈ H) (hxy : x * y ∈ H) : y ∈ H :=
begin
rw ← inv_mem_iff at hx,
convert H.mul_mem hx hxy, -- I claim that this proof that x⁻¹ * (x * y) ∈ H is what we want
-- so now it just remains to prove x⁻¹ * (x * y) = y, which we can do because of Knuth-Bendix
simp,
end
/-
Subgroups are extensional objects (like most things in mathematics):
two subgroups are equal if they have the same underlying subset,
and also if they have the same underlying elements.
Let's prove variants of this triviality now. The first one is rather
un-mathematical: it takes a subgroup apart into its pieces. I'll see if
you can do the other two!
-/
/-- Two subgroups are equal if the underlying subsets are equal. -/
theorem ext' {H K : subgroup G} (h : H.carrier = K.carrier) : H = K :=
begin
-- first take H and K apart
cases H, -- H now broken up into its underlying 3-tuple.
cases K,
-- and now it must be obvious, so let's see if the simplifier can do it.
simp * at *, -- it can!
end
-- here's a variant. You can prove it using `ext'`.
/-- Two subgroups are equal if and only if the underlying subsets are equal. -/
theorem ext'_iff {H K : subgroup G} :
H.carrier = K.carrier ↔ H = K :=
begin
split,
{ exact ext' },
{ intro h,
rw h, }
end
-- to do this next one, first apply the `ext'` theorem we just proved,
-- and then use the `ext` tactic (which works on sets)
/-- Two subgroups are equal if they have the same elements. -/
@[ext] theorem ext {H K : subgroup G} (h : ∀ x, x ∈ H ↔ x ∈ K) : H = K :=
begin
-- first apply ext' to reduce to checking the carriers are equal
apply ext',
-- now use the ext tactic to reduce to checking the elements of
-- the carriers are equal
ext,
-- and now this is by definition what h says
apply h,
end
/-
We tagged that theorem with `ext`, so now the `ext` tactic works on subgroups
too: if you ever have a goal of proving that two subgroups are equal, you can
use the `ext` tactic to reduce to showing that they have the same elements.
-/
/-
## The lattice structure on subgroups
Subgroups of a group form what is known as a *lattice*.
This is a partially ordered set with a sensible notion of
max and min. We partially order subgroups by saying `H ≤ K` if
`H.carrier ⊆ K.carrier`. Subgroups even have a good notion of an infinite
Sup and Inf (the Inf of a bunch of subgroups is just their intersection;
their Sup is the subgroup generated by their union).
This combinatorial structure (a partially ordered set with good
finite and infinite notions of Sup and Inf) is called a "complete lattice",
and Lean has this structure inbuilt into it. We will construct
a complete lattice structure on `subgroup G`.
We start by defining a relation ≤ on the type of subgroups of a group.
We say H ≤ K iff H.carrier ⊆ K.carrier .
-/
/-- If `H` and `K` are subgroups of `G`, we write `H ≤ K` to
mean `H.carrier ⊆ K.carrier` -/
instance : has_le (subgroup G) := ⟨λ H K, H.carrier ⊆ K.carrier⟩
-- useful to restate the definition so we can `rw` it
lemma le_def : H ≤ K ↔ H.carrier ⊆ K.carrier :=
begin
-- true by definition
refl
end
-- another useful variant
lemma le_iff : H ≤ K ↔ ∀ g, g ∈ H → g ∈ K :=
begin
-- true by definition
refl,
end
-- Now let's check the axioms for a partial order.
-- These are not hard, they just reduce immediately to the
-- fact that ⊆ is a partial order
@[refl] lemma le_refl : H ≤ H :=
begin
rw le_def,
-- Lean knows ⊆ is reflexive so the sneaky `refl` which Lean tries after `rw`
-- has closed the goal!
end
lemma le_antisymm : H ≤ K → K ≤ H → H = K :=
begin
rw [le_def, le_def, ← ext'_iff],
-- now this is antisymmetry of ⊆, which Lean knows
exact set.subset.antisymm,
end
@[trans] lemma le_trans : H ≤ J → J ≤ K → H ≤ K :=
begin
rw [le_def, le_def, le_def],
-- now this is transitivity of ⊆, which Lean knows
exact set.subset.trans,
end
-- We've made `subgroup G` into a partial order!
instance : partial_order (subgroup G) :=
{ le := (≤),
le_refl := le_refl,
le_antisymm := le_antisymm,
le_trans := le_trans }
/-
### intersections
Let's prove that the intersection of two subgroups is a subgroup. In Lean
this is a definition: given two subgroups, we define a new subgroup whose
underlying subset is the intersection of the subsets, and then prove
the axioms.
-/
/-- The intersection of two subgroups is also a subgroup -/
def inf (H K : subgroup G) : subgroup G :=
{ carrier := H.carrier ∩ K.carrier, -- the carrier is the intersection
one_mem' :=
begin
-- recall that x ∈ Y ∩ Z is _by definition_ x ∈ Y ∧ x ∈ Z, so you can `split` this.
split,
{ exact H.one_mem },
{ exact K.one_mem },
end,
mul_mem' :=
begin
intros x y hx hy,
cases hx with hxH hxK,
cases hy with hyH hyK,
split,
{ exact H.mul_mem hxH hyH },
{ exact K.mul_mem hxK hyK },
end,
inv_mem' :=
begin
rintro x ⟨hxH, hxK⟩, -- rintro does intro and cases all in one go
exact ⟨H.inv_mem hxH, K.inv_mem hxK⟩, -- ⟨⟩ instead of split
end }
-- Note that using "term mode" one can really compactify all this.
def inf_term_mode (H K : subgroup G) : subgroup G :=
{ carrier := H.carrier ∩ K.carrier,
one_mem' := ⟨H.one_mem, K.one_mem⟩,
mul_mem' := λ _ _ ⟨hhx, hkx⟩ ⟨hhy, hky⟩,
⟨H.mul_mem hhx hhy, K.mul_mem hkx hky⟩,
inv_mem' := λ x ⟨hhx, hhy⟩,
⟨H.inv_mem hhx, K.inv_mem hhy⟩}
-- Notation for `inf` in computer science circles is ⊓ .
instance : has_inf (subgroup G) := ⟨inf⟩
/-- The underlying set of the inf of two subgroups is just their intersection -/
lemma inf_def (H K : subgroup G) : (H ⊓ K : set G) = (H : set G) ∩ K :=
begin
-- true by definition
refl
end
/-
## Subgroup generated by a subset.
To do the sup of two subgroups is harder, because we don't just take
the union, we need to then look at the subgroup generated by this union
(e.g. the union of the x and y axes in ℝ² is not a subgroup). So we need
to have a machine to spit out the subgroup of `G` generated by a subset `S : set G`.
There are two completely different ways to do this. The first is a "top-down"
approach. We could define the subgroup generated by `S` to be the intersection of
all the subgroups of `G` that contain `S`. The second is a "bottom-up" approach.
We could define the subgroup generated by `S` "by induction" (or more precisely
by recursion), saying that `S` is in the subgroup, 1 is in the subgroup,
the product of two things in the subgroup is in the subgroup, the
inverse of something in the subgroup is in the subgroup, and that's it.
Both methods come out rather nicely in Lean. Let's do the first one.
We are going to be using a bunch of theorems about "bounded intersections",
a.k.a. `set.bInter`. We will soon get tired of writing `set.blah` so let's
`open set` so that we can skip it.
-/
open set
/-
Here is the API for `set.bInter` (or `bInter`, as we can now call it):
Notation: `⋂` (type with `\I`)
If `X : set (subgroup G)`, i.e. if `X` is a set of subgroups of `G`, then
`⋂ K ∈ X, (K : set G)` means "take the intersection of the underlying subsets".
-- mem_bInter_iff says you're in the intersection iff you're in
-- all the things you're intersecting. Very useful for rewriting.
`mem_bInter_iff : (g ∈ ⋂ (K ∈ S), f K) ↔ (∀ K, K ∈ s → g ∈ f K)`
-- mem_bInter is just the one way implication. Very useful for `apply`ing.
`mem_bInter : (∀ K, K ∈ s → g ∈ f K) → (g ∈ ⋂ (K ∈ S), f K)`
-/
/-
We will consider the closure of a set as the intersect of all subgroups
containing the set
-/
/-- The Inf of a set of subgroups of G is their intersection. -/
def Inf (X : set (subgroup G)) : subgroup G :=
{ carrier := ⋂ K ∈ X, (K : set G), -- carrier is the intersection of the underlying sets
one_mem' := begin
apply mem_bInter,
intros H hH,
apply H.one_mem,
end,
mul_mem' := begin
intros x y hx hy,
rw mem_bInter_iff at *,
intros H hH,
apply H.mul_mem,
{ apply hx,
exact hH },
{ exact hy H hH }
end,
inv_mem' := begin
intros x hX,
rw mem_bInter_iff at *,
intros H hH,
exact H.inv_mem (hX H hH),
end,
}
-- again note that term mode can do this much more quickly
def Inf' (X : set (subgroup G)) : subgroup G :=
{ carrier := ⋂ t ∈ X, (t : set G),
one_mem' := mem_bInter $ λ i h, i.one_mem,
mul_mem' := λ x y hx hy, mem_bInter $ λ i h,
i.mul_mem (by apply mem_bInter_iff.1 hx i h)
(by apply mem_bInter_iff.1 hy i h),
inv_mem' := λ x hx, mem_bInter $ λ i h,
i.inv_mem (by apply mem_bInter_iff.1 hx i h) }
/-- The *closure* of a subset `S` of `G` is the `Inf` of the subgroups of `G`
which contain `S`. -/
def closure (S : set G) : subgroup G := Inf {H : subgroup G | S ⊆ H}
-- we can restate mem_bInter_iff using our new "closure" language:
lemma mem_closure_iff {S : set G} {x : G} :
x ∈ closure S ↔ ∀ H : subgroup G, S ⊆ H → x ∈ H := mem_bInter_iff
/-
There is an underlying abstraction here, which you may not know about.
A "closure operator" in mathematics
https://en.wikipedia.org/wiki/Closure_operator
is something mapping subsets of a set X to subsets of X, and satisfying three
axioms:
1) `subset_closure : S ⊆ closure S`
2) `closure_mono : (S ⊆ T) → (closure S ⊆ closure T)`
3) `closure_closure : closure (closure S) = closure S`
It works for closure in topological spaces, and it works here too.
It also works for algebraic closures of fields, and there are several
other places in mathematics where it shows up. This idea, of "abstracting"
and axiomatising a phenomenon which shows up in more than one place,
is really key in Lean.
Let's prove these three lemmas in the case where `X = G` and `closure S`
is the subgroup generated by `S`.
Here are some things you might find helpful.
Remember
`mem_coe : g ∈ ↑H ↔ g ∈ H`
`mem_carrier : g ∈ H.carrier ↔ g ∈ H`
There's
`mem_closure_iff : x ∈ closure S ↔ ∀ (H : subgroup G), S ⊆ ↑H → x ∈ H`
(`closure S` is a subgroup so you might need to use `mem_coe` or `mem_carrier` first)
For subsets there's
`subset.trans : X ⊆ Y → Y ⊆ Z → X ⊆ Z`
You might find `le_antisymm : H ≤ K → K ≤ H → H = K` from above useful
-/
/-
Reminder: X ⊆ Y means `∀ g, g ∈ X → g ∈ Y` and it's definitional,
so you can just start this with `intro g`.
-/
lemma subset_closure (S : set G) : S ⊆ ↑(closure S) :=
begin
intro g,
intro hgS,
rw [mem_coe, mem_closure_iff],
intros H hH,
apply hH,
assumption
end
-- fancy term mode proof
lemma subset_closure' (S : set G) : S ⊆ closure S :=
λ s hs H ⟨y, hy⟩, by {rw [←hy, mem_Inter], exact λ hS, hS hs}
-- It's useful to know `subset.trans : X ⊆ Y → Y ⊆ Z → X ⊆ Z`
lemma closure_mono {S T : set G} (hST : S ⊆ T) : closure S ≤ closure T :=
begin
intros x hx,
rw [mem_carrier, mem_closure_iff] at *,
intros H hTH,
apply hx,
exact subset.trans hST hTH,
end
-- not one of the axioms, but sometimes handy
lemma closure_le (S : set G) (H : subgroup G) : closure S ≤ H ↔ S ⊆ ↑H :=
begin
split,
{ intro hSH,
exact subset.trans (subset_closure S) hSH },
{ intros hSH g hg,
rw [mem_carrier, mem_closure_iff] at hg,
exact hg _ hSH }
end
-- You can start this one by applying `le_antisymm`,
lemma closure_closure (S : set G) : closure S = closure (closure S) :=
begin
apply le_antisymm,
{ apply subset_closure },
{ rw closure_le,
intros x hx,
exact hx },
end
-- This shows that every subgroup is the closure of something, namely its
-- underlying subset.
lemma closure_self {H : subgroup G} : closure ↑H = H :=
begin
apply le_antisymm,
{ rw le_iff,
intro g,
rw mem_closure_iff,
intro h,
apply h,
refl },
{ exact subset_closure H }
end
/-
Recall the second proposed construction of the subgroup closure of a subset `S`;
it is the smallest subgroup `H` of `G` such that `S ⊆ H` and which contains
`1` and is closed under `*` and `⁻¹`. This inductive constuction (which we
did not make) comes with a so-called "recursor": if we have a true/false
statement `p g` attached to each element `g` of G with the following properties:
1) `p s` is true for all `s ∈ S`,
2) `p 1` is true,
3) If `p x` and `p y` then `p (x * y)`,
4) If `p x` then `p x⁻¹`
Then `p` is true on all of `closure S`.
If we had made an inductive definition of `closure S` then this would have been true
by definition! We used another definition, so we will have to prove it
ourselves.
-/
/-- An induction principle for closures. -/
lemma closure_induction {p : G → Prop} {S : set G}
(HS : ∀ x ∈ S, p x)
(H1 : p 1)
(Hmul : ∀ x y, p x → p y → p (x * y))
(Hinv : ∀ x, p x → p x⁻¹) :
-- conclusion after colon
∀ x, x ∈ closure S → p x :=
begin
-- the subset of G where `p` is true is a subgroup. Let's call it H
let H : subgroup G :=
{ carrier := p,
one_mem' := H1,
mul_mem' := Hmul,
inv_mem' := Hinv },
-- The goal is just that closure S ≤ H, by definition.
change closure S ≤ H,
-- Our hypothesis HS is just that S ⊆ ↑H, by definition
change S ⊆ ↑H at HS,
-- So this is just closure_le in disguise
rw closure_le,
assumption,
end
-- this is even briefer in term mode:
lemma closure_induction' {p : G → Prop} {S : set G}
(HS : ∀ x ∈ S, p x)
(H1 : p 1)
(Hmul : ∀ x y, p x → p y → p (x * y))
(Hinv : ∀ x, p x → p x⁻¹) :
∀ x, x ∈ closure S → p x :=
λ x h, (@closure_le _ _ _ ⟨p, H1, Hmul, Hinv⟩).2 HS h
/-
Finally we prove that the `closure` and `coe` maps form a `galois_insertion`.
This is another abstraction, it generalises `galois_connection`, which is
something that shows up all over the place (algebraic geometry, Galois theory etc).
See
https://en.wikipedia.org/wiki/Galois_connection
A partial order can be considered as a category, with Hom(A,B) having
one element if A ≤ B and no elements otherwise. A Galois connection between
two partial orders is just a pair of adjoint functors between the categories.
Adjointness in our case is `S ⊆ ↑H ↔ closure S ≤ H`.
The reason it's an insertion and not just a connection is that if you start
with a subgroup, take the underlying subset, and then look at the subgroup
generated by that set, you get back to where you started. So it's like one
of the adjoint functors being a forgetful functor.
-/
def gi : galois_insertion (closure : set G → subgroup G) (coe : subgroup G → set G) :=
{ choice := λ S _, closure S,
gc := closure_le,
le_l_u := λ H, subset_closure (H : set G),
choice_eq := λ _ _, rfl }
/-
One use of this abstraction is that now we can pull back the complete
lattice structure on `set G` to get a complete lattice structure on `subgroup G`.
-/
instance : complete_lattice (subgroup G) :=
{.. galois_insertion.lift_complete_lattice gi}
/-
We just proved loads of lemmas about Infs and Sups of subgroups automatically,
and have access to a ton more because the `complete_lattice` structure in
Lean has a big API. See for example
https://leanprover-community.github.io/mathlib_docs/order/complete_lattice.html#complete_lattice
All those theorems are now true for subgroups. None are particularly hard to prove,
but the point is that we don't now have to prove any of them ourselves.
-/
end subgroup
end xena
/-
Further work: `bot` and `top` (would have to explain the
API for `singleton` and `univ`)
-/
|
75b83b57f204f6b094f8f1410186f9e82faa90f9 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/logic/unique.lean | bafbd62abe35497cf618ebd4820a28bb59f8eb42 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 7,424 | lean | /-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import tactic.basic
import logic.is_empty
/-!
# Types with a unique term
In this file we define a typeclass `unique`,
which expresses that a type has a unique term.
In other words, a type that is `inhabited` and a `subsingleton`.
## Main declaration
* `unique`: a typeclass that expresses that a type has a unique term.
## Main statements
* `unique.mk'`: an inhabited subsingleton type is `unique`. This can not be an instance because it
would lead to loops in typeclass inference.
* `function.surjective.unique`: if the domain of a surjective function is `unique`, then its
codomain is `unique` as well.
* `function.injective.subsingleton`: if the codomain of an injective function is `subsingleton`,
then its domain is `subsingleton` as well.
* `function.injective.unique`: if the codomain of an injective function is `subsingleton` and its
domain is `inhabited`, then its domain is `unique`.
## Implementation details
The typeclass `unique α` is implemented as a type,
rather than a `Prop`-valued predicate,
for good definitional properties of the default term.
-/
universes u v w
variables {α : Sort u} {β : Sort v} {γ : Sort w}
/-- `unique α` expresses that `α` is a type with a unique term `default`.
This is implemented as a type, rather than a `Prop`-valued predicate,
for good definitional properties of the default term. -/
@[ext]
structure unique (α : Sort u) extends inhabited α :=
(uniq : ∀ a : α, a = default)
attribute [class] unique
lemma unique_iff_exists_unique (α : Sort u) : nonempty (unique α) ↔ ∃! a : α, true :=
⟨λ ⟨u⟩, ⟨u.default, trivial, λ a _, u.uniq a⟩, λ ⟨a,_,h⟩, ⟨⟨⟨a⟩, λ _, h _ trivial⟩⟩⟩
lemma unique_subtype_iff_exists_unique {α} (p : α → Prop) :
nonempty (unique (subtype p)) ↔ ∃! a, p a :=
⟨λ ⟨u⟩, ⟨u.default.1, u.default.2, λ a h, congr_arg subtype.val (u.uniq ⟨a,h⟩)⟩,
λ ⟨a,ha,he⟩, ⟨⟨⟨⟨a,ha⟩⟩, λ ⟨b,hb⟩, by { congr, exact he b hb }⟩⟩⟩
/-- Given an explicit `a : α` with `[subsingleton α]`, we can construct
a `[unique α]` instance. This is a def because the typeclass search cannot
arbitrarily invent the `a : α` term. Nevertheless, these instances are all
equivalent by `unique.subsingleton.unique`.
See note [reducible non-instances]. -/
@[reducible] def unique_of_subsingleton {α : Sort*} [subsingleton α] (a : α) : unique α :=
{ default := a,
uniq := λ _, subsingleton.elim _ _ }
instance punit.unique : unique punit.{u} :=
{ default := punit.star,
uniq := λ x, punit_eq x _ }
@[simp] lemma punit.default_eq_star : (default : punit) = punit.star := rfl
/-- Every provable proposition is unique, as all proofs are equal. -/
def unique_prop {p : Prop} (h : p) : unique p :=
{ default := h, uniq := λ x, rfl }
instance : unique true := unique_prop trivial
lemma fin.eq_zero : ∀ n : fin 1, n = 0
| ⟨n, hn⟩ := fin.eq_of_veq (nat.eq_zero_of_le_zero (nat.le_of_lt_succ hn))
instance {n : ℕ} : inhabited (fin n.succ) := ⟨0⟩
instance inhabited_fin_one_add (n : ℕ) : inhabited (fin (1 + n)) := ⟨⟨0, nat.zero_lt_one_add n⟩⟩
@[simp] lemma fin.default_eq_zero (n : ℕ) : (default : fin n.succ) = 0 := rfl
instance fin.unique : unique (fin 1) :=
{ uniq := fin.eq_zero, .. fin.inhabited }
namespace unique
open function
section
variables [unique α]
@[priority 100] -- see Note [lower instance priority]
instance : inhabited α := to_inhabited ‹unique α›
lemma eq_default (a : α) : a = default := uniq _ a
lemma default_eq (a : α) : default = a := (uniq _ a).symm
@[priority 100] -- see Note [lower instance priority]
instance : subsingleton α := subsingleton_of_forall_eq _ eq_default
lemma forall_iff {p : α → Prop} : (∀ a, p a) ↔ p default :=
⟨λ h, h _, λ h x, by rwa [unique.eq_default x]⟩
lemma exists_iff {p : α → Prop} : Exists p ↔ p default :=
⟨λ ⟨a, ha⟩, eq_default a ▸ ha, exists.intro default⟩
end
@[ext] protected lemma subsingleton_unique' : ∀ (h₁ h₂ : unique α), h₁ = h₂
| ⟨⟨x⟩, h⟩ ⟨⟨y⟩, _⟩ := by congr; rw [h x, h y]
instance subsingleton_unique : subsingleton (unique α) :=
⟨unique.subsingleton_unique'⟩
/-- Construct `unique` from `inhabited` and `subsingleton`. Making this an instance would create
a loop in the class inheritance graph. -/
@[reducible] def mk' (α : Sort u) [h₁ : inhabited α] [subsingleton α] : unique α :=
{ uniq := λ x, subsingleton.elim _ _, .. h₁ }
end unique
@[simp] lemma pi.default_def {β : Π a : α, Sort v} [Π a, inhabited (β a)] :
@default (Π a, β a) _ = λ a : α, @default (β a) _ := rfl
lemma pi.default_apply {β : Π a : α, Sort v} [Π a, inhabited (β a)] (a : α) :
@default (Π a, β a) _ a = default := rfl
instance pi.unique {β : Π a : α, Sort v} [Π a, unique (β a)] : unique (Π a, β a) :=
{ uniq := λ f, funext $ λ x, unique.eq_default _,
.. pi.inhabited α }
/-- There is a unique function on an empty domain. -/
instance pi.unique_of_is_empty [is_empty α] (β : Π a : α, Sort v) :
unique (Π a, β a) :=
{ default := is_empty_elim,
uniq := λ f, funext is_empty_elim }
namespace function
variable {f : α → β}
/-- If the domain of a surjective function is a singleton,
then the codomain is a singleton as well. -/
protected def surjective.unique (hf : surjective f) [unique α] : unique β :=
{ default := f default,
uniq := λ b, let ⟨a, ha⟩ := hf b in ha ▸ congr_arg f (unique.eq_default _) }
/-- If the codomain of an injective function is a subsingleton, then the domain
is a subsingleton as well. -/
protected lemma injective.subsingleton (hf : injective f) [subsingleton β] :
subsingleton α :=
⟨λ x y, hf $ subsingleton.elim _ _⟩
/-- If `α` is inhabited and admits an injective map to a subsingleton type, then `α` is `unique`. -/
protected def injective.unique [inhabited α] [subsingleton β] (hf : injective f) : unique α :=
@unique.mk' _ _ hf.subsingleton
/-- If a constant function is surjective, then the codomain is a singleton. -/
def surjective.unique_of_surjective_const (α : Type*) {β : Type*} (b : β)
(h : function.surjective (function.const α b)) : unique β :=
@unique_of_subsingleton _ (subsingleton_of_forall_eq b $ h.forall.mpr (λ _, rfl)) b
end function
lemma unique.bijective {A B} [unique A] [unique B] {f : A → B} : function.bijective f :=
begin
rw function.bijective_iff_has_inverse,
refine ⟨λ x, default, _, _⟩; intro x; simp
end
namespace option
/-- `option α` is a `subsingleton` if and only if `α` is empty. -/
lemma subsingleton_iff_is_empty {α} : subsingleton (option α) ↔ is_empty α :=
⟨λ h, ⟨λ x, option.no_confusion $ @subsingleton.elim _ h x none⟩,
λ h, ⟨λ x y, option.cases_on x (option.cases_on y rfl (λ x, h.elim x)) (λ x, h.elim x)⟩⟩
instance {α} [is_empty α] : unique (option α) := @unique.mk' _ _ (subsingleton_iff_is_empty.2 ‹_›)
end option
section subtype
instance unique.subtype_eq (y : α) : unique {x // x = y} :=
{ default := ⟨y, rfl⟩,
uniq := λ ⟨x, hx⟩, by simpa using hx }
instance unique.subtype_eq' (y : α) : unique {x // y = x} :=
{ default := ⟨y, rfl⟩,
uniq := λ ⟨x, hx⟩, by simpa using hx.symm }
end subtype
|
071d5a73792285e6fc3b828614a94cd292a934da | 1b8f093752ba748c5ca0083afef2959aaa7dace5 | /src/category_theory/universal/comparisons/cones.lean | a648813ac39f885e2ef841d7d045bd50ed7da38d | [] | 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 | 2,146 | lean | -- Copyright (c) 2017 Scott Morrison. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Stephen Morgan, Scott Morrison
import category_theory.equivalence
import category_theory.universal.cones
import category_theory.universal.comma_categories
open category_theory
open category_theory.comma
namespace category_theory.limits
universes u v u₁ v₁ u₂ v₂
variables {J : Type v} [small_category J] {C : Type u} [𝒞 : category.{u v} C]
variable {F : J ⥤ C}
section
include 𝒞
@[simp] lemma comma.Cone.commutativity (F : J ⥤ C) (X : C) (cone : ((DiagonalFunctor J C) X) ⟶ ((ObjectAsFunctor.{(max u v) v} F).obj punit.star)) {j k : J} (f : j ⟶ k) : cone j ≫ (F.map f) = cone k :=
by obviously
def comma_Cone_to_Cone (c : (comma.Cone F)) : cone F :=
{ X := c.1.1,
π := λ j : J, (c.2) j }
@[simp] lemma comma_Cone_to_Cone_cone_maps (c : (comma.Cone F)) (j : J) : (comma_Cone_to_Cone c).π j = (c.2) j := rfl
def comma_ConeMorphism_to_ConeMorphism {X Y : (comma.Cone F)} (f : comma.comma_morphism X Y) : (comma_Cone_to_Cone X) ⟶ (comma_Cone_to_Cone Y) :=
{ hom := f.left }
def Cone_to_comma_Cone (c : cone F) : comma.Cone F :=
⟨ (c.X, by obviously), { app := λ j, c.π j } ⟩
def ConeMorphism_to_comma_ConeMorphism {X Y : cone F} (f : cone_morphism X Y) : (Cone_to_comma_Cone X) ⟶ (Cone_to_comma_Cone Y) :=
{ left := f.hom,
right := by obviously }
def comma_Cones_to_Cones (F : J ⥤ C) : (comma.Cone F) ⥤ (cone F) :=
{ obj := comma_Cone_to_Cone,
map' := λ X Y f, comma_ConeMorphism_to_ConeMorphism f }
def Cones_to_comma_Cones (F : J ⥤ C) : (cone F) ⥤ (comma.Cone F) :=
{ obj := Cone_to_comma_Cone,
map' := λ X Y f, ConeMorphism_to_comma_ConeMorphism f }.
end /- end `include 𝒞` -/
local attribute [back] category.id
private meta def dsimp' := `[dsimp at * {unfold_reducible := tt, md := semireducible}]
local attribute [tidy] dsimp'
include 𝒞
def Cones_agree (F : J ⥤ C) : equivalence (comma.Cone F) (cone F) :=
{ functor := comma_Cones_to_Cones F,
inverse := Cones_to_comma_Cones F }
end category_theory.limits |
91eacb80d775f1862571e3c0fb541bed1cb2fe95 | 02005f45e00c7ecf2c8ca5db60251bd1e9c860b5 | /src/data/set/finite.lean | c17ba4521e8298cb301e6ed1bb1207d724d34f2b | [
"Apache-2.0"
] | permissive | anthony2698/mathlib | 03cd69fe5c280b0916f6df2d07c614c8e1efe890 | 407615e05814e98b24b2ff322b14e8e3eb5e5d67 | refs/heads/master | 1,678,792,774,873 | 1,614,371,563,000 | 1,614,371,563,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 28,141 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import data.fintype.basic
/-!
# Finite sets
This file defines predicates `finite : set α → Prop` and `infinite : set α → Prop` and proves some
basic facts about finite sets.
-/
open set function
universes u v w x
variables {α : Type u} {β : Type v} {ι : Sort w} {γ : Type x}
namespace set
/-- A set is finite if the subtype is a fintype, i.e. there is a
list that enumerates its members. -/
def finite (s : set α) : Prop := nonempty (fintype s)
/-- A set is infinite if it is not finite. -/
def infinite (s : set α) : Prop := ¬ finite s
/-- The subtype corresponding to a finite set is a finite type. Note
that because `finite` isn't a typeclass, this will not fire if it
is made into an instance -/
noncomputable def finite.fintype {s : set α} (h : finite s) : fintype s :=
classical.choice h
/-- Get a finset from a finite set -/
noncomputable def finite.to_finset {s : set α} (h : finite s) : finset α :=
@set.to_finset _ _ h.fintype
@[simp] theorem finite.mem_to_finset {s : set α} {h : finite s} {a : α} : a ∈ h.to_finset ↔ a ∈ s :=
@mem_to_finset _ _ h.fintype _
@[simp] theorem finite.to_finset.nonempty {s : set α} (h : finite s) :
h.to_finset.nonempty ↔ s.nonempty :=
show (∃ x, x ∈ h.to_finset) ↔ (∃ x, x ∈ s),
from exists_congr (λ _, finite.mem_to_finset)
@[simp] lemma finite.coe_to_finset {α} {s : set α} (h : finite s) : ↑h.to_finset = s :=
@set.coe_to_finset _ s h.fintype
theorem finite.exists_finset {s : set α} : finite s →
∃ s' : finset α, ∀ a : α, a ∈ s' ↔ a ∈ s
| ⟨h⟩ := by exactI ⟨to_finset s, λ _, mem_to_finset⟩
theorem finite.exists_finset_coe {s : set α} (hs : finite s) :
∃ s' : finset α, ↑s' = s :=
⟨hs.to_finset, hs.coe_to_finset⟩
/-- Finite sets can be lifted to finsets. -/
instance : can_lift (set α) (finset α) :=
{ coe := coe,
cond := finite,
prf := λ s hs, hs.exists_finset_coe }
theorem finite_mem_finset (s : finset α) : finite {a | a ∈ s} :=
⟨fintype.of_finset s (λ _, iff.rfl)⟩
theorem finite.of_fintype [fintype α] (s : set α) : finite s :=
by classical; exact ⟨set_fintype s⟩
theorem exists_finite_iff_finset {p : set α → Prop} :
(∃ s, finite s ∧ p s) ↔ ∃ s : finset α, p ↑s :=
⟨λ ⟨s, hs, hps⟩, ⟨hs.to_finset, hs.coe_to_finset.symm ▸ hps⟩,
λ ⟨s, hs⟩, ⟨↑s, finite_mem_finset s, hs⟩⟩
/-- Membership of a subset of a finite type is decidable.
Using this as an instance leads to potential loops with `subtype.fintype` under certain decidability
assumptions, so it should only be declared a local instance. -/
def decidable_mem_of_fintype [decidable_eq α] (s : set α) [fintype s] (a) : decidable (a ∈ s) :=
decidable_of_iff _ mem_to_finset
instance fintype_empty : fintype (∅ : set α) :=
fintype.of_finset ∅ $ by simp
theorem empty_card : fintype.card (∅ : set α) = 0 := rfl
@[simp] theorem empty_card' {h : fintype.{u} (∅ : set α)} :
@fintype.card (∅ : set α) h = 0 :=
eq.trans (by congr) empty_card
@[simp] theorem finite_empty : @finite α ∅ := ⟨set.fintype_empty⟩
instance finite.inhabited : inhabited {s : set α // finite s} := ⟨⟨∅, finite_empty⟩⟩
/-- A `fintype` structure on `insert a s`. -/
def fintype_insert' {a : α} (s : set α) [fintype s] (h : a ∉ s) : fintype (insert a s : set α) :=
fintype.of_finset ⟨a ::ₘ s.to_finset.1,
multiset.nodup_cons_of_nodup (by simp [h]) s.to_finset.2⟩ $ by simp
theorem card_fintype_insert' {a : α} (s : set α) [fintype s] (h : a ∉ s) :
@fintype.card _ (fintype_insert' s h) = fintype.card s + 1 :=
by rw [fintype_insert', fintype.card_of_finset];
simp [finset.card, to_finset]; refl
@[simp] theorem card_insert {a : α} (s : set α)
[fintype s] (h : a ∉ s) {d : fintype.{u} (insert a s : set α)} :
@fintype.card _ d = fintype.card s + 1 :=
by rw ← card_fintype_insert' s h; congr
lemma card_image_of_inj_on {s : set α} [fintype s]
{f : α → β} [fintype (f '' s)] (H : ∀x∈s, ∀y∈s, f x = f y → x = y) :
fintype.card (f '' s) = fintype.card s :=
by haveI := classical.prop_decidable; exact
calc fintype.card (f '' s) = (s.to_finset.image f).card : fintype.card_of_finset' _ (by simp)
... = s.to_finset.card : finset.card_image_of_inj_on
(λ x hx y hy hxy, H x (mem_to_finset.1 hx) y (mem_to_finset.1 hy) hxy)
... = fintype.card s : (fintype.card_of_finset' _ (λ a, mem_to_finset)).symm
lemma card_image_of_injective (s : set α) [fintype s]
{f : α → β} [fintype (f '' s)] (H : function.injective f) :
fintype.card (f '' s) = fintype.card s :=
card_image_of_inj_on $ λ _ _ _ _ h, H h
section
local attribute [instance] decidable_mem_of_fintype
instance fintype_insert [decidable_eq α] (a : α) (s : set α) [fintype s] :
fintype (insert a s : set α) :=
if h : a ∈ s then by rwa [insert_eq, union_eq_self_of_subset_left (singleton_subset_iff.2 h)]
else fintype_insert' _ h
end
@[simp] theorem finite.insert (a : α) {s : set α} : finite s → finite (insert a s)
| ⟨h⟩ := ⟨@set.fintype_insert _ (classical.dec_eq α) _ _ h⟩
lemma to_finset_insert [decidable_eq α] {a : α} {s : set α} (hs : finite s) :
(hs.insert a).to_finset = insert a hs.to_finset :=
finset.ext $ by simp
@[elab_as_eliminator]
theorem finite.induction_on {C : set α → Prop} {s : set α} (h : finite s)
(H0 : C ∅) (H1 : ∀ {a s}, a ∉ s → finite s → C s → C (insert a s)) : C s :=
let ⟨t⟩ := h in by exactI
match s.to_finset, @mem_to_finset _ s _ with
| ⟨l, nd⟩, al := begin
change ∀ a, a ∈ l ↔ a ∈ s at al,
clear _let_match _match t h, revert s nd al,
refine multiset.induction_on l _ (λ a l IH, _); intros s nd al,
{ rw show s = ∅, from eq_empty_iff_forall_not_mem.2 (by simpa using al),
exact H0 },
{ rw ← show insert a {x | x ∈ l} = s, from set.ext (by simpa using al),
cases multiset.nodup_cons.1 nd with m nd',
refine H1 _ ⟨finset.subtype.fintype ⟨l, nd'⟩⟩ (IH nd' (λ _, iff.rfl)),
exact m }
end
end
@[elab_as_eliminator]
theorem finite.dinduction_on {C : ∀s:set α, finite s → Prop} {s : set α} (h : finite s)
(H0 : C ∅ finite_empty)
(H1 : ∀ {a s}, a ∉ s → ∀h:finite s, C s h → C (insert a s) (h.insert a)) :
C s h :=
have ∀h:finite s, C s h,
from finite.induction_on h (assume h, H0) (assume a s has hs ih h, H1 has hs (ih _)),
this h
instance fintype_singleton (a : α) : fintype ({a} : set α) :=
unique.fintype
@[simp] theorem card_singleton (a : α) :
fintype.card ({a} : set α) = 1 :=
fintype.card_of_subsingleton _
@[simp] theorem finite_singleton (a : α) : finite ({a} : set α) :=
⟨set.fintype_singleton _⟩
instance fintype_pure : ∀ a : α, fintype (pure a : set α) :=
set.fintype_singleton
theorem finite_pure (a : α) : finite (pure a : set α) :=
⟨set.fintype_pure a⟩
instance fintype_univ [fintype α] : fintype (@univ α) :=
fintype.of_equiv α $ (equiv.set.univ α).symm
theorem finite_univ [fintype α] : finite (@univ α) := ⟨set.fintype_univ⟩
/-- If `(set.univ : set α)` is finite then `α` is a finite type. -/
noncomputable def fintype_of_univ_finite (H : (univ : set α).finite ) :
fintype α :=
@fintype.of_equiv _ (univ : set α) H.fintype (equiv.set.univ _)
lemma univ_finite_iff_nonempty_fintype :
(univ : set α).finite ↔ nonempty (fintype α) :=
begin
split,
{ intro h, exact ⟨fintype_of_univ_finite h⟩ },
{ rintro ⟨_i⟩, exactI finite_univ }
end
theorem infinite_univ_iff : (@univ α).infinite ↔ _root_.infinite α :=
⟨λ h₁, ⟨λ h₂, h₁ $ @finite_univ α h₂⟩, λ ⟨h₁⟩ h₂, h₁ (fintype_of_univ_finite h₂)⟩
theorem infinite_univ [h : _root_.infinite α] : infinite (@univ α) :=
infinite_univ_iff.2 h
theorem infinite_coe_iff {s : set α} : _root_.infinite s ↔ infinite s :=
⟨λ ⟨h₁⟩ h₂, h₁ h₂.some, λ h₁, ⟨λ h₂, h₁ ⟨h₂⟩⟩⟩
theorem infinite.to_subtype {s : set α} (h : infinite s) : _root_.infinite s :=
infinite_coe_iff.2 h
/-- Embedding of `ℕ` into an infinite set. -/
noncomputable def infinite.nat_embedding (s : set α) (h : infinite s) : ℕ ↪ s :=
by { haveI := h.to_subtype, exact infinite.nat_embedding s }
lemma infinite.exists_subset_card_eq {s : set α} (hs : infinite s) (n : ℕ) :
∃ t : finset α, ↑t ⊆ s ∧ t.card = n :=
⟨((finset.range n).map (hs.nat_embedding _)).map (embedding.subtype _), by simp⟩
lemma infinite.nonempty {s : set α} (h : s.infinite) : s.nonempty :=
let a := infinite.nat_embedding s h 37 in ⟨a.1, a.2⟩
instance fintype_union [decidable_eq α] (s t : set α) [fintype s] [fintype t] :
fintype (s ∪ t : set α) :=
fintype.of_finset (s.to_finset ∪ t.to_finset) $ by simp
theorem finite.union {s t : set α} : finite s → finite t → finite (s ∪ t)
| ⟨hs⟩ ⟨ht⟩ := ⟨@set.fintype_union _ (classical.dec_eq α) _ _ hs ht⟩
lemma infinite_of_finite_compl {α : Type} [_root_.infinite α] {s : set α}
(hs : sᶜ.finite) : s.infinite :=
λ h, set.infinite_univ (by simpa using hs.union h)
lemma finite.infinite_compl {α : Type} [_root_.infinite α] {s : set α}
(hs : s.finite) : sᶜ.infinite :=
λ h, set.infinite_univ (by simpa using hs.union h)
instance fintype_sep (s : set α) (p : α → Prop) [fintype s] [decidable_pred p] :
fintype ({a ∈ s | p a} : set α) :=
fintype.of_finset (s.to_finset.filter p) $ by simp
instance fintype_inter (s t : set α) [fintype s] [decidable_pred t] : fintype (s ∩ t : set α) :=
set.fintype_sep s t
/-- A `fintype` structure on a set defines a `fintype` structure on its subset. -/
def fintype_subset (s : set α) {t : set α} [fintype s] [decidable_pred t] (h : t ⊆ s) : fintype t :=
by rw ← inter_eq_self_of_subset_right h; apply_instance
theorem finite.subset {s : set α} : finite s → ∀ {t : set α}, t ⊆ s → finite t
| ⟨hs⟩ t h := ⟨@set.fintype_subset _ _ _ hs (classical.dec_pred t) h⟩
theorem infinite_mono {s t : set α} (h : s ⊆ t) : infinite s → infinite t :=
mt (λ ht, ht.subset h)
instance fintype_image [decidable_eq β] (s : set α) (f : α → β) [fintype s] : fintype (f '' s) :=
fintype.of_finset (s.to_finset.image f) $ by simp
instance fintype_range [decidable_eq β] (f : α → β) [fintype α] : fintype (range f) :=
fintype.of_finset (finset.univ.image f) $ by simp [range]
theorem finite_range (f : α → β) [fintype α] : finite (range f) :=
by haveI := classical.dec_eq β; exact ⟨by apply_instance⟩
theorem finite.image {s : set α} (f : α → β) : finite s → finite (f '' s)
| ⟨h⟩ := ⟨@set.fintype_image _ _ (classical.dec_eq β) _ _ h⟩
theorem infinite_of_infinite_image (f : α → β) {s : set α} (hs : (f '' s).infinite) :
s.infinite :=
mt (finite.image f) hs
lemma finite.dependent_image {s : set α} (hs : finite s) {F : Π i ∈ s, β} {t : set β}
(H : ∀ y ∈ t, ∃ x (hx : x ∈ s), y = F x hx) : set.finite t :=
begin
let G : s → β := λ x, F x.1 x.2,
have A : t ⊆ set.range G,
{ assume y hy,
rcases H y hy with ⟨x, hx, xy⟩,
refine ⟨⟨x, hx⟩, xy.symm⟩ },
letI : fintype s := finite.fintype hs,
exact (finite_range G).subset A
end
instance fintype_map {α β} [decidable_eq β] :
∀ (s : set α) (f : α → β) [fintype s], fintype (f <$> s) := set.fintype_image
theorem finite.map {α β} {s : set α} :
∀ (f : α → β), finite s → finite (f <$> s) := finite.image
/-- If a function `f` has a partial inverse and sends a set `s` to a set with `[fintype]` instance,
then `s` has a `fintype` structure as well. -/
def fintype_of_fintype_image (s : set α)
{f : α → β} {g} (I : is_partial_inv f g) [fintype (f '' s)] : fintype s :=
fintype.of_finset ⟨_, @multiset.nodup_filter_map β α g _
(@injective_of_partial_inv_right _ _ f g I) (f '' s).to_finset.2⟩ $ λ a,
begin
suffices : (∃ b x, f x = b ∧ g b = some a ∧ x ∈ s) ↔ a ∈ s,
by simpa [exists_and_distrib_left.symm, and.comm, and.left_comm, and.assoc],
rw exists_swap,
suffices : (∃ x, x ∈ s ∧ g (f x) = some a) ↔ a ∈ s, {simpa [and.comm, and.left_comm, and.assoc]},
simp [I _, (injective_of_partial_inv I).eq_iff]
end
theorem finite_of_finite_image {s : set α} {f : α → β} (hi : set.inj_on f s) :
finite (f '' s) → finite s | ⟨h⟩ :=
⟨@fintype.of_injective _ _ h (λa:s, ⟨f a.1, mem_image_of_mem f a.2⟩) $
assume a b eq, subtype.eq $ hi a.2 b.2 $ subtype.ext_iff_val.1 eq⟩
theorem finite_image_iff {s : set α} {f : α → β} (hi : inj_on f s) :
finite (f '' s) ↔ finite s :=
⟨finite_of_finite_image hi, finite.image _⟩
theorem infinite_image_iff {s : set α} {f : α → β} (hi : inj_on f s) :
infinite (f '' s) ↔ infinite s :=
not_congr $ finite_image_iff hi
theorem infinite_of_inj_on_maps_to {s : set α} {t : set β} {f : α → β}
(hi : inj_on f s) (hm : maps_to f s t) (hs : infinite s) : infinite t :=
infinite_mono (maps_to'.mp hm) $ (infinite_image_iff hi).2 hs
theorem infinite_range_of_injective [_root_.infinite α] {f : α → β} (hi : injective f) :
infinite (range f) :=
by { rw [←image_univ, infinite_image_iff (inj_on_of_injective hi _)], exact infinite_univ }
theorem infinite_of_injective_forall_mem [_root_.infinite α] {s : set β} {f : α → β}
(hi : injective f) (hf : ∀ x : α, f x ∈ s) : infinite s :=
by { rw ←range_subset_iff at hf, exact infinite_mono hf (infinite_range_of_injective hi) }
theorem finite.preimage {s : set β} {f : α → β}
(I : set.inj_on f (f⁻¹' s)) (h : finite s) : finite (f ⁻¹' s) :=
finite_of_finite_image I (h.subset (image_preimage_subset f s))
theorem finite.preimage_embedding {s : set β} (f : α ↪ β) (h : s.finite) : (f ⁻¹' s).finite :=
finite.preimage (λ _ _ _ _ h', f.injective h') h
instance fintype_Union [decidable_eq α] {ι : Type*} [fintype ι]
(f : ι → set α) [∀ i, fintype (f i)] : fintype (⋃ i, f i) :=
fintype.of_finset (finset.univ.bUnion (λ i, (f i).to_finset)) $ by simp
theorem finite_Union {ι : Type*} [fintype ι] {f : ι → set α} (H : ∀i, finite (f i)) :
finite (⋃ i, f i) :=
⟨@set.fintype_Union _ (classical.dec_eq α) _ _ _ (λ i, finite.fintype (H i))⟩
/-- A union of sets with `fintype` structure over a set with `fintype` structure has a `fintype`
structure. -/
def fintype_set_bUnion [decidable_eq α] {ι : Type*} {s : set ι} [fintype s]
(f : ι → set α) (H : ∀ i ∈ s, fintype (f i)) : fintype (⋃ i ∈ s, f i) :=
by rw bUnion_eq_Union; exact
@set.fintype_Union _ _ _ _ _ (by rintro ⟨i, hi⟩; exact H i hi)
instance fintype_set_bUnion' [decidable_eq α] {ι : Type*} {s : set ι} [fintype s]
(f : ι → set α) [H : ∀ i, fintype (f i)] : fintype (⋃ i ∈ s, f i) :=
fintype_set_bUnion _ (λ i _, H i)
theorem finite.sUnion {s : set (set α)} (h : finite s) (H : ∀t∈s, finite t) : finite (⋃₀ s) :=
by rw sUnion_eq_Union; haveI := finite.fintype h;
apply finite_Union; simpa using H
theorem finite.bUnion {α} {ι : Type*} {s : set ι} {f : Π i ∈ s, set α} :
finite s → (∀ i ∈ s, finite (f i ‹_›)) → finite (⋃ i∈s, f i ‹_›)
| ⟨hs⟩ h := by rw [bUnion_eq_Union]; exactI finite_Union (λ i, h _ _)
instance fintype_lt_nat (n : ℕ) : fintype {i | i < n} :=
fintype.of_finset (finset.range n) $ by simp
instance fintype_le_nat (n : ℕ) : fintype {i | i ≤ n} :=
by simpa [nat.lt_succ_iff] using set.fintype_lt_nat (n+1)
lemma finite_le_nat (n : ℕ) : finite {i | i ≤ n} := ⟨set.fintype_le_nat _⟩
lemma finite_lt_nat (n : ℕ) : finite {i | i < n} := ⟨set.fintype_lt_nat _⟩
instance fintype_prod (s : set α) (t : set β) [fintype s] [fintype t] : fintype (set.prod s t) :=
fintype.of_finset (s.to_finset.product t.to_finset) $ by simp
lemma finite.prod {s : set α} {t : set β} : finite s → finite t → finite (set.prod s t)
| ⟨hs⟩ ⟨ht⟩ := by exactI ⟨set.fintype_prod s t⟩
/-- `image2 f s t` is finitype if `s` and `t` are. -/
instance fintype_image2 [decidable_eq γ] (f : α → β → γ) (s : set α) (t : set β)
[hs : fintype s] [ht : fintype t] : fintype (image2 f s t : set γ) :=
by { rw ← image_prod, apply set.fintype_image }
lemma finite.image2 (f : α → β → γ) {s : set α} {t : set β} (hs : finite s) (ht : finite t) :
finite (image2 f s t) :=
by { rw ← image_prod, exact (hs.prod ht).image _ }
/-- If `s : set α` is a set with `fintype` instance and `f : α → set β` is a function such that
each `f a`, `a ∈ s`, has a `fintype` structure, then `s >>= f` has a `fintype` structure. -/
def fintype_bUnion {α β} [decidable_eq β] (s : set α) [fintype s]
(f : α → set β) (H : ∀ a ∈ s, fintype (f a)) : fintype (s >>= f) :=
set.fintype_set_bUnion _ H
instance fintype_bUnion' {α β} [decidable_eq β] (s : set α) [fintype s]
(f : α → set β) [H : ∀ a, fintype (f a)] : fintype (s >>= f) :=
fintype_bUnion _ _ (λ i _, H i)
theorem finite_bUnion {α β} {s : set α} {f : α → set β} :
finite s → (∀ a ∈ s, finite (f a)) → finite (s >>= f)
| ⟨hs⟩ H := ⟨@fintype_bUnion _ _ (classical.dec_eq β) _ hs _ (λ a ha, (H a ha).fintype)⟩
instance fintype_seq {α β : Type u} [decidable_eq β]
(f : set (α → β)) (s : set α) [fintype f] [fintype s] :
fintype (f <*> s) :=
by rw seq_eq_bind_map; apply set.fintype_bUnion'
theorem finite.seq {α β : Type u} {f : set (α → β)} {s : set α} :
finite f → finite s → finite (f <*> s)
| ⟨hf⟩ ⟨hs⟩ := by { haveI := classical.dec_eq β, exactI ⟨set.fintype_seq _ _⟩ }
/-- There are finitely many subsets of a given finite set -/
lemma finite.finite_subsets {α : Type u} {a : set α} (h : finite a) : finite {b | b ⊆ a} :=
begin
-- we just need to translate the result, already known for finsets,
-- to the language of finite sets
let s : set (set α) := coe '' (↑(finset.powerset (finite.to_finset h)) : set (finset α)),
have : finite s := (finite_mem_finset _).image _,
apply this.subset,
refine λ b hb, ⟨(h.subset hb).to_finset, _, finite.coe_to_finset _⟩,
simpa [finset.subset_iff]
end
lemma exists_min_image [linear_order β] (s : set α) (f : α → β) (h1 : finite s) :
s.nonempty → ∃ a ∈ s, ∀ b ∈ s, f a ≤ f b
| ⟨x, hx⟩ := by simpa only [exists_prop, finite.mem_to_finset]
using (finite.to_finset h1).exists_min_image f ⟨x, finite.mem_to_finset.2 hx⟩
lemma exists_max_image [linear_order β] (s : set α) (f : α → β) (h1 : finite s) :
s.nonempty → ∃ a ∈ s, ∀ b ∈ s, f b ≤ f a
| ⟨x, hx⟩ := by simpa only [exists_prop, finite.mem_to_finset]
using (finite.to_finset h1).exists_max_image f ⟨x, finite.mem_to_finset.2 hx⟩
end set
namespace finset
variables [decidable_eq β]
variables {s : finset α}
lemma finite_to_set (s : finset α) : set.finite (↑s : set α) :=
set.finite_mem_finset s
@[simp] lemma coe_bUnion {f : α → finset β} : ↑(s.bUnion f) = (⋃x ∈ (↑s : set α), ↑(f x) : set β) :=
by simp [set.ext_iff]
@[simp] lemma finite_to_set_to_finset {α : Type*} (s : finset α) :
(finite_to_set s).to_finset = s :=
by { ext, rw [set.finite.mem_to_finset, mem_coe] }
end finset
namespace set
lemma finite_subset_Union {s : set α} (hs : finite s)
{ι} {t : ι → set α} (h : s ⊆ ⋃ i, t i) : ∃ I : set ι, finite I ∧ s ⊆ ⋃ i ∈ I, t i :=
begin
casesI hs,
choose f hf using show ∀ x : s, ∃ i, x.1 ∈ t i, {simpa [subset_def] using h},
refine ⟨range f, finite_range f, _⟩,
rintro x hx,
simp,
exact ⟨x, ⟨hx, hf _⟩⟩,
end
lemma eq_finite_Union_of_finite_subset_Union {ι} {s : ι → set α} {t : set α} (tfin : finite t)
(h : t ⊆ ⋃ i, s i) :
∃ I : set ι, (finite I) ∧ ∃ σ : {i | i ∈ I} → set α,
(∀ i, finite (σ i)) ∧ (∀ i, σ i ⊆ s i) ∧ t = ⋃ i, σ i :=
let ⟨I, Ifin, hI⟩ := finite_subset_Union tfin h in
⟨I, Ifin, λ x, s x ∩ t,
λ i, tfin.subset (inter_subset_right _ _),
λ i, inter_subset_left _ _,
begin
ext x,
rw mem_Union,
split,
{ intro x_in,
rcases mem_Union.mp (hI x_in) with ⟨i, _, ⟨hi, rfl⟩, H⟩,
use [i, hi, H, x_in] },
{ rintros ⟨i, hi, H⟩,
exact H }
end⟩
/-- An increasing union distributes over finite intersection. -/
lemma Union_Inter_of_monotone {ι ι' α : Type*} [fintype ι] [linear_order ι']
[nonempty ι'] {s : ι → ι' → set α} (hs : ∀ i, monotone (s i)) :
(⋃ j : ι', ⋂ i : ι, s i j) = ⋂ i : ι, ⋃ j : ι', s i j :=
begin
ext x, refine ⟨λ hx, Union_Inter_subset hx, λ hx, _⟩,
simp only [mem_Inter, mem_Union, mem_Inter] at hx ⊢, choose j hj using hx,
obtain ⟨j₀⟩ := show nonempty ι', by apply_instance,
refine ⟨finset.univ.fold max j₀ j, λ i, hs i _ (hj i)⟩,
rw [finset.fold_op_rel_iff_or (@le_max_iff _ _)],
exact or.inr ⟨i, finset.mem_univ i, le_rfl⟩
end
instance nat.fintype_Iio (n : ℕ) : fintype (Iio n) :=
fintype.of_finset (finset.range n) $ by simp
/--
If `P` is some relation between terms of `γ` and sets in `γ`,
such that every finite set `t : set γ` has some `c : γ` related to it,
then there is a recursively defined sequence `u` in `γ`
so `u n` is related to the image of `{0, 1, ..., n-1}` under `u`.
(We use this later to show sequentially compact sets
are totally bounded.)
-/
lemma seq_of_forall_finite_exists {γ : Type*}
{P : γ → set γ → Prop} (h : ∀ t, finite t → ∃ c, P c t) :
∃ u : ℕ → γ, ∀ n, P (u n) (u '' Iio n) :=
⟨λ n, @nat.strong_rec_on' (λ _, γ) n $ λ n ih, classical.some $ h
(range $ λ m : Iio n, ih m.1 m.2)
(finite_range _),
λ n, begin
classical,
refine nat.strong_rec_on' n (λ n ih, _),
rw nat.strong_rec_on_beta', convert classical.some_spec (h _ _),
ext x, split,
{ rintros ⟨m, hmn, rfl⟩, exact ⟨⟨m, hmn⟩, rfl⟩ },
{ rintros ⟨⟨m, hmn⟩, rfl⟩, exact ⟨m, hmn, rfl⟩ }
end⟩
lemma finite_range_ite {p : α → Prop} [decidable_pred p] {f g : α → β} (hf : finite (range f))
(hg : finite (range g)) : finite (range (λ x, if p x then f x else g x)) :=
(hf.union hg).subset range_ite_subset
lemma finite_range_const {c : β} : finite (range (λ x : α, c)) :=
(finite_singleton c).subset range_const_subset
lemma range_find_greatest_subset {P : α → ℕ → Prop} [∀ x, decidable_pred (P x)] {b : ℕ}:
range (λ x, nat.find_greatest (P x) b) ⊆ ↑(finset.range (b + 1)) :=
by { rw range_subset_iff, assume x, simp [nat.lt_succ_iff, nat.find_greatest_le] }
lemma finite_range_find_greatest {P : α → ℕ → Prop} [∀ x, decidable_pred (P x)] {b : ℕ} :
finite (range (λ x, nat.find_greatest (P x) b)) :=
(finset.range (b + 1)).finite_to_set.subset range_find_greatest_subset
lemma card_lt_card {s t : set α} [fintype s] [fintype t] (h : s ⊂ t) :
fintype.card s < fintype.card t :=
fintype.card_lt_of_injective_not_surjective (set.inclusion h.1) (set.inclusion_injective h.1) $
λ hst, (ssubset_iff_subset_ne.1 h).2 (eq_of_inclusion_surjective hst)
lemma card_le_of_subset {s t : set α} [fintype s] [fintype t] (hsub : s ⊆ t) :
fintype.card s ≤ fintype.card t :=
fintype.card_le_of_injective (set.inclusion hsub) (set.inclusion_injective hsub)
lemma eq_of_subset_of_card_le {s t : set α} [fintype s] [fintype t]
(hsub : s ⊆ t) (hcard : fintype.card t ≤ fintype.card s) : s = t :=
(eq_or_ssubset_of_subset hsub).elim id
(λ h, absurd hcard $ not_le_of_lt $ card_lt_card h)
lemma subset_iff_to_finset_subset (s t : set α) [fintype s] [fintype t] :
s ⊆ t ↔ s.to_finset ⊆ t.to_finset :=
⟨λ h x hx, set.mem_to_finset.mpr $ h $ set.mem_to_finset.mp hx,
λ h x hx, set.mem_to_finset.mp $ h $ set.mem_to_finset.mpr hx⟩
lemma card_range_of_injective [fintype α] {f : α → β} (hf : injective f)
[fintype (range f)] : fintype.card (range f) = fintype.card α :=
eq.symm $ fintype.card_congr $ equiv.set.range f hf
lemma finite.exists_maximal_wrt [partial_order β] (f : α → β) (s : set α) (h : set.finite s) :
s.nonempty → ∃a∈s, ∀a'∈s, f a ≤ f a' → f a = f a' :=
begin
classical,
refine h.induction_on _ _,
{ assume h, exact absurd h empty_not_nonempty },
assume a s his _ ih _,
cases s.eq_empty_or_nonempty with h h,
{ use a, simp [h] },
rcases ih h with ⟨b, hb, ih⟩,
by_cases f b ≤ f a,
{ refine ⟨a, set.mem_insert _ _, assume c hc hac, le_antisymm hac _⟩,
rcases set.mem_insert_iff.1 hc with rfl | hcs,
{ refl },
{ rwa [← ih c hcs (le_trans h hac)] } },
{ refine ⟨b, set.mem_insert_of_mem _ hb, assume c hc hbc, _⟩,
rcases set.mem_insert_iff.1 hc with rfl | hcs,
{ exact (h hbc).elim },
{ exact ih c hcs hbc } }
end
lemma finite.card_to_finset {s : set α} [fintype s] (h : s.finite) :
h.to_finset.card = fintype.card s :=
by { rw [← finset.card_attach, finset.attach_eq_univ, ← fintype.card], congr' 2, funext,
rw set.finite.mem_to_finset }
section decidable_eq
lemma to_finset_compl {α : Type*} [fintype α] [decidable_eq α]
(s : set α) [fintype (sᶜ : set α)] [fintype s] : sᶜ.to_finset = (s.to_finset)ᶜ :=
by ext; simp
lemma to_finset_inter {α : Type*} [decidable_eq α] (s t : set α) [fintype (s ∩ t : set α)]
[fintype s] [fintype t] : (s ∩ t).to_finset = s.to_finset ∩ t.to_finset :=
by ext; simp
lemma to_finset_union {α : Type*} [decidable_eq α] (s t : set α) [fintype (s ∪ t : set α)]
[fintype s] [fintype t] : (s ∪ t).to_finset = s.to_finset ∪ t.to_finset :=
by ext; simp
lemma to_finset_ne_eq_erase {α : Type*} [decidable_eq α] [fintype α] (a : α)
[fintype {x : α | x ≠ a}] : {x : α | x ≠ a}.to_finset = finset.univ.erase a :=
by ext; simp
lemma card_ne_eq [fintype α] (a : α) [fintype {x : α | x ≠ a}] :
fintype.card {x : α | x ≠ a} = fintype.card α - 1 :=
begin
haveI := classical.dec_eq α,
rw [←to_finset_card, to_finset_ne_eq_erase, finset.card_erase_of_mem (finset.mem_univ _),
finset.card_univ, nat.pred_eq_sub_one],
end
end decidable_eq
section
variables [semilattice_sup α] [nonempty α] {s : set α}
/--A finite set is bounded above.-/
protected lemma finite.bdd_above (hs : finite s) : bdd_above s :=
finite.induction_on hs bdd_above_empty $ λ a s _ _ h, h.insert a
/--A finite union of sets which are all bounded above is still bounded above.-/
lemma finite.bdd_above_bUnion {I : set β} {S : β → set α} (H : finite I) :
(bdd_above (⋃i∈I, S i)) ↔ (∀i ∈ I, bdd_above (S i)) :=
finite.induction_on H
(by simp only [bUnion_empty, bdd_above_empty, ball_empty_iff])
(λ a s ha _ hs, by simp only [bUnion_insert, ball_insert_iff, bdd_above_union, hs])
end
section
variables [semilattice_inf α] [nonempty α] {s : set α}
/--A finite set is bounded below.-/
protected lemma finite.bdd_below (hs : finite s) : bdd_below s :=
@finite.bdd_above (order_dual α) _ _ _ hs
/--A finite union of sets which are all bounded below is still bounded below.-/
lemma finite.bdd_below_bUnion {I : set β} {S : β → set α} (H : finite I) :
(bdd_below (⋃i∈I, S i)) ↔ (∀i ∈ I, bdd_below (S i)) :=
@finite.bdd_above_bUnion (order_dual α) _ _ _ _ _ H
end
end set
namespace finset
/-- A finset is bounded above. -/
protected lemma bdd_above [semilattice_sup α] [nonempty α] (s : finset α) :
bdd_above (↑s : set α) :=
s.finite_to_set.bdd_above
/-- A finset is bounded below. -/
protected lemma bdd_below [semilattice_inf α] [nonempty α] (s : finset α) :
bdd_below (↑s : set α) :=
s.finite_to_set.bdd_below
end finset
lemma fintype.exists_max [fintype α] [nonempty α]
{β : Type*} [linear_order β] (f : α → β) :
∃ x₀ : α, ∀ x, f x ≤ f x₀ :=
begin
rcases set.finite_univ.exists_maximal_wrt f _ univ_nonempty with ⟨x, _, hx⟩,
exact ⟨x, λ y, (le_total (f x) (f y)).elim (λ h, ge_of_eq $ hx _ trivial h) id⟩
end
|
98750d055a5396ba55434b102d71546097c25df1 | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /src/Init/Data/Hashable.lean | 3811ff7ebf50f539975338fb28adafd5b415fa4c | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | EdAyers/lean4 | 57ac632d6b0789cb91fab2170e8c9e40441221bd | 37ba0df5841bde51dbc2329da81ac23d4f6a4de4 | refs/heads/master | 1,676,463,245,298 | 1,660,619,433,000 | 1,660,619,433,000 | 183,433,437 | 1 | 0 | Apache-2.0 | 1,657,612,672,000 | 1,556,196,574,000 | Lean | UTF-8 | Lean | false | false | 1,394 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Init.Data.UInt
import Init.Data.String
universe u
instance : Hashable Nat where
hash n := UInt64.ofNat n
instance : Hashable String.Pos where
hash p := UInt64.ofNat p.byteIdx
instance [Hashable α] [Hashable β] : Hashable (α × β) where
hash | (a, b) => mixHash (hash a) (hash b)
instance : Hashable Bool where
hash
| true => 11
| false => 13
instance [Hashable α] : Hashable (Option α) where
hash
| none => 11
| some a => mixHash (hash a) 13
instance [Hashable α] : Hashable (List α) where
hash as := as.foldl (fun r a => mixHash r (hash a)) 7
instance [Hashable α] : Hashable (Array α) where
hash as := as.foldl (fun r a => mixHash r (hash a)) 7
instance : Hashable UInt8 where
hash n := n.toUInt64
instance : Hashable UInt16 where
hash n := n.toUInt64
instance : Hashable UInt32 where
hash n := n.toUInt64
instance : Hashable UInt64 where
hash n := n
instance : Hashable USize where
hash n := n.toUInt64
instance : Hashable (Fin n) where
hash v := v.val.toUInt64
instance : Hashable Int where
hash
| Int.ofNat n => UInt64.ofNat (2 * n)
| Int.negSucc n => UInt64.ofNat (2 * n + 1)
instance (P : Prop) : Hashable P where
hash _ := 0
|
080ebc0c598c8a642acdb8192fd133978f6178cf | 453dcd7c0d1ef170b0843a81d7d8caedc9741dce | /analysis/ennreal.lean | 109bc84bf45e4921af789668d56461c54b18ab52 | [
"Apache-2.0"
] | permissive | amswerdlow/mathlib | 9af77a1f08486d8fa059448ae2d97795bd12ec0c | 27f96e30b9c9bf518341705c99d641c38638dfd0 | refs/heads/master | 1,585,200,953,598 | 1,534,275,532,000 | 1,534,275,532,000 | 144,564,700 | 0 | 0 | null | 1,534,156,197,000 | 1,534,156,197,000 | null | UTF-8 | Lean | false | false | 24,171 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Johannes Hölzl
Extended non-negative reals
TODO: base ennreal on nnreal!
-/
import algebra.ordered_group analysis.nnreal analysis.limits data.real.ennreal
noncomputable theory
open classical set lattice filter
local attribute [instance] prop_decidable
variables {α : Type*} {β : Type*}
local notation `∞` := ennreal.infinity
namespace ennreal
variables {a b c d : ennreal} {r p q : ℝ}
section topological_space
open topological_space
instance : topological_space ennreal :=
topological_space.generate_from {s | ∃a, s = {b | a < b} ∨ s = {b | b < a}}
instance : orderable_topology ennreal := ⟨rfl⟩
instance : t2_space ennreal := by apply_instance
instance : second_countable_topology ennreal :=
⟨⟨⋃q ≥ (0:ℚ), {{a : ennreal | a < of_real q}, {a : ennreal | of_real ↑q < a}},
countable_bUnion (countable_encodable _) $ assume a ha, countable_insert (countable_singleton _),
le_antisymm
(generate_from_le $ λ s h, begin
rcases h with ⟨a, hs | hs⟩;
[ rw show s = ⋃q∈{q:ℚ | 0 ≤ q ∧ a < of_real q}, {b | of_real q < b},
from set.ext (assume b, by simp [hs, @ennreal.lt_iff_exists_rat_btwn a b, and_assoc]),
rw show s = ⋃q∈{q:ℚ | 0 ≤ q ∧ of_real q < a}, {b | b < of_real q},
from set.ext (assume b, by simp [hs, @ennreal.lt_iff_exists_rat_btwn b a, and_comm, and_assoc])];
{ apply is_open_Union, intro q,
apply is_open_Union, intro hq,
exact generate_open.basic _ (mem_bUnion hq.1 $ by simp) }
end)
(generate_from_le $ by simp [or_imp_distrib, is_open_lt', is_open_gt'] {contextual := tt})⟩⟩
lemma continuous_of_real : continuous of_real :=
have ∀x:ennreal, is_open {a : ℝ | x < of_real a},
from forall_ennreal.mpr ⟨assume r hr,
by simp [of_real_lt_of_real_iff_cases]; exact is_open_and (is_open_lt' 0) (is_open_lt' r),
by simp⟩,
have ∀x:ennreal, is_open {a : ℝ | of_real a < x},
from forall_ennreal.mpr ⟨assume r hr,
by simp [of_real_lt_of_real_iff_cases]; exact is_open_and is_open_const (is_open_gt' r),
by simp [is_open_const]⟩,
continuous_generated_from $ begin simp [or_imp_distrib, *] {contextual := tt} end
lemma tendsto_of_real : tendsto of_real (nhds r) (nhds (of_real r)) :=
continuous_iff_tendsto.mp continuous_of_real r
lemma tendsto_of_ennreal (hr : 0 ≤ r) : tendsto of_ennreal (nhds (of_real r)) (nhds r) :=
tendsto_orderable_unbounded (no_top _) (no_bot _) $
assume l u hl hu,
by_cases
(assume hr : r = 0,
have hl : l < 0, by rw [hr] at hl; exact hl,
have hu : 0 < u, by rw [hr] at hu; exact hu,
have nhds (of_real r) = (⨅l (h₂ : 0 < l), principal {x | x < l}),
from calc nhds (of_real r) = nhds ⊥ : by simp [hr]; refl
... = (⨅u (h₂ : 0 < u), principal {x | x < u}) : nhds_bot_orderable,
have {x | x < of_real u} ∈ (nhds (of_real r)).sets,
by rw [this];
from mem_infi_sets (of_real u) (mem_infi_sets (by simp *) (subset.refl _)),
((nhds (of_real r)).upwards_sets this $ forall_ennreal.mpr $
by simp [le_of_lt, hu, hl] {contextual := tt}; exact assume p hp _, lt_of_lt_of_le hl hp))
(assume hr_ne : r ≠ 0,
have hu0 : 0 < u, from lt_of_le_of_lt hr hu,
have hu_nn: 0 ≤ u, from le_of_lt hu0,
have hr' : 0 < r, from lt_of_le_of_ne hr hr_ne.symm,
have hl' : ∃l, l < of_real r, from ⟨0, by simp [hr, hr']⟩,
have hu' : ∃u, of_real r < u, from ⟨of_real u, by simp [hr, hu_nn, hu]⟩,
begin
rw [mem_nhds_unbounded hu' hl'],
existsi (of_real l), existsi (of_real u),
simp [*, of_real_lt_of_real_iff_cases, forall_ennreal] {contextual := tt}
end)
lemma nhds_of_real_eq_map_of_real_nhds {r : ℝ} (hr : 0 ≤ r) :
nhds (of_real r) = (nhds r).map of_real :=
have h₁ : {x | x < ∞} ∈ (nhds (of_real r)).sets,
from mem_nhds_sets (is_open_gt' ∞) of_real_lt_infty,
have h₂ : {x | x < ∞} ∈ ((nhds r).map of_real).sets,
from mem_map.mpr $ univ_mem_sets' $ assume a, of_real_lt_infty,
have h : ∀x<∞, ∀y<∞, of_ennreal x = of_ennreal y → x = y,
by simp [forall_ennreal] {contextual:=tt},
le_antisymm
(by_cases
(assume (hr : r = 0) s (hs : {x | of_real x ∈ s} ∈ (nhds r).sets),
have hs : {x | of_real x ∈ s} ∈ (nhds (0:ℝ)).sets, from hr ▸ hs,
let ⟨l, u, hl, hu, h⟩ := (mem_nhds_unbounded (no_top 0) (no_bot 0)).mp hs in
have nhds (of_real r) = nhds ⊥, by simp [hr]; refl,
begin
rw [this, nhds_bot_orderable],
apply mem_infi_sets (of_real u) _,
apply mem_infi_sets (zero_lt_of_real_iff.mpr hu) _,
simp [set.subset_def],
intro x, rw [lt_iff_exists_of_real],
simp [le_of_lt hu] {contextual := tt},
exact assume p hp _ hpu, h _ (lt_of_lt_of_le hl hp) hpu
end)
(assume : r ≠ 0,
have hr' : 0 < r, from lt_of_le_of_ne hr this.symm,
have h' : map (of_ennreal ∘ of_real) (nhds r) = map id (nhds r),
from map_cong $ (nhds r).upwards_sets (mem_nhds_sets (is_open_lt' 0) hr') $
assume r hr, by simp [le_of_lt hr, (∘)],
le_of_map_le_map_inj' h₁ h₂ h $ le_trans (tendsto_of_ennreal hr) $ by simp [h']))
tendsto_of_real
lemma nhds_of_real_eq_map_of_real_nhds_nonneg {r : ℝ} (hr : 0 ≤ r) :
nhds (of_real r) = (nhds r ⊓ principal {x | 0 ≤ x}).map of_real :=
by rw [nhds_of_real_eq_map_of_real_nhds hr];
from by_cases
(assume : r = 0,
le_antisymm
(assume s (hs : {a | of_real a ∈ s} ∈ (nhds r ⊓ principal {x | 0 ≤ x}).sets),
let ⟨t₁, ht₁, t₂, ht₂, ht⟩ := mem_inf_sets.mp hs in
show {a | of_real a ∈ s} ∈ (nhds r).sets,
from (nhds r).upwards_sets ht₁ $ assume a ha,
match le_total 0 a with
| or.inl h := have a ∈ t₂, from ht₂ h, ht ⟨ha, this⟩
| or.inr h :=
have r ∈ t₁ ∩ t₂, from ⟨mem_of_nhds ht₁, ht₂ (le_of_eq ‹r = 0›.symm)⟩,
have of_real 0 ∈ s, from ‹r = 0› ▸ ht this,
by simp [of_real_of_nonpos h]; assumption
end)
(map_mono inf_le_left))
(assume : r ≠ 0,
have 0 < r, from lt_of_le_of_ne hr this.symm,
have nhds r ⊓ principal {x : ℝ | 0 ≤ x} = nhds r,
from inf_of_le_left $ le_principal_iff.mpr $ le_mem_nhds this,
by simp [*])
instance : topological_add_monoid ennreal :=
have hinf : ∀a, tendsto (λ(p : ennreal × ennreal), p.1 + p.2) ((nhds ∞).prod (nhds a)) (nhds ⊤),
begin
intro a,
rw [nhds_top_orderable],
apply tendsto_infi.2 _, intro b,
apply tendsto_infi.2 _, intro hb,
apply tendsto_principal.2 _,
revert b,
simp [forall_ennreal],
exact assume r hr, mem_prod_iff.mpr ⟨
{a | of_real r < a}, mem_nhds_sets (is_open_lt' _) of_real_lt_infty,
univ, univ_mem_sets, assume ⟨c, d⟩ ⟨hc, _⟩, lt_of_lt_of_le hc $ le_add_right $ le_refl _⟩
end,
have h : ∀{p r : ℝ}, 0 ≤ p → 0 ≤ r → tendsto (λp:ennreal×ennreal, p.1 + p.2)
((nhds (of_real r)).prod (nhds (of_real p))) (nhds (of_real (r + p))),
from assume p r hp hr,
begin
rw [nhds_of_real_eq_map_of_real_nhds_nonneg hp, nhds_of_real_eq_map_of_real_nhds_nonneg hr,
prod_map_map_eq, ←prod_inf_prod, prod_principal_principal, ←nhds_prod_eq],
exact tendsto_map' (tendsto_cong
(tendsto_le_left inf_le_left $ tendsto_add'.comp tendsto_of_real)
(mem_inf_sets_of_right $ mem_principal_sets.mpr $ by simp [subset_def, (∘)] {contextual:=tt}))
end,
have ∀{a₁ a₂ : ennreal}, tendsto (λp:ennreal×ennreal, p.1 + p.2) (nhds (a₁, a₂)) (nhds (a₁ + a₂)),
from forall_ennreal.mpr ⟨assume r hr, forall_ennreal.mpr
⟨assume p hp, by simp [*, nhds_prod_eq]; exact h _ _,
begin
rw [nhds_prod_eq, prod_comm],
apply tendsto_map' _,
simp [(∘)],
exact hinf _
end⟩,
by simp [nhds_prod_eq]; exact hinf⟩,
⟨continuous_iff_tendsto.mpr $ assume ⟨a₁, a₂⟩, this⟩
protected lemma tendsto_mul : ∀{a b : ennreal}, b ≠ 0 → tendsto ((*) a) (nhds b) (nhds (a * b)) :=
forall_ennreal.mpr $ and.intro
(assume p hp, forall_ennreal.mpr $ and.intro
(assume r hr hr0,
have r ≠ 0, from assume h, by simp [h] at hr0; contradiction,
have 0 < r, from lt_of_le_of_ne hr this.symm,
have tendsto (λr, of_real (p * r)) (nhds r ⊓ principal {x : ℝ | 0 ≤ x}) (nhds (of_real (p * r))),
from tendsto.comp (tendsto_mul tendsto_const_nhds $ tendsto_id' inf_le_left) tendsto_of_real,
begin
rw [nhds_of_real_eq_map_of_real_nhds_nonneg hr, of_real_mul_of_real hp hr],
apply tendsto_map' (tendsto_cong this $ mem_inf_sets_of_right $ mem_principal_sets.mpr _),
simp [subset_def, (∘), hp] {contextual := tt}
end)
(assume _, by_cases
(assume : p = 0,
tendsto_cong tendsto_const_nhds $
(nhds ∞).upwards_sets (mem_nhds_sets (is_open_lt' _) (@of_real_lt_infty 1)) $
by simp [this])
(assume p0 : p ≠ 0,
have p_pos : 0 < p, from lt_of_le_of_ne hp p0.symm,
suffices tendsto ((*) (of_real p)) (nhds ⊤) (nhds ⊤), { simpa [hp, p0] },
by rw [nhds_top_orderable];
from (tendsto_infi.2 $ assume l, tendsto_infi.2 $ assume hl,
let ⟨q, hq, hlq, _⟩ := ennreal.lt_iff_exists_of_real.mp hl in
tendsto_infi' (of_real (q / p)) $ tendsto_infi' of_real_lt_infty $ tendsto_principal_principal.2 $
forall_ennreal.mpr $ and.intro
begin
have : ∀r:ℝ, 0 < r → q / p < r → q < p * r ∧ 0 < p * r,
from assume r r_pos qpr,
have q < p * r,
from calc q = (q / p) * p : by rw [div_mul_cancel _ (ne_of_gt p_pos)]
... < r * p : mul_lt_mul_of_pos_right qpr p_pos
... = p * r : mul_comm _ _,
⟨this, mul_pos p_pos r_pos⟩,
simp [hlq, hp, of_real_lt_of_real_iff_cases, this] {contextual := tt}
end
begin simp [hp, p0]; exact hl end))))
begin
assume b hb0,
have : 0 < b, from lt_of_le_of_ne ennreal.zero_le hb0.symm,
suffices : tendsto ((*) ∞) (nhds b) (nhds ∞), { simpa [hb0] },
apply (tendsto_cong tendsto_const_nhds $
(nhds b).upwards_sets (mem_nhds_sets (is_open_lt' _) this) _),
{ assume c hc,
have : c ≠ 0, from assume h, by simp [h] at hc; contradiction,
simp [this] }
end
lemma supr_of_real {s : set ℝ} {a : ℝ} (h : is_lub s a) : (⨆a∈s, of_real a) = of_real a :=
suffices Sup (of_real '' s) = of_real a, by simpa [Sup_image],
is_lub_iff_Sup_eq.mp $ is_lub_of_is_lub_of_tendsto
(assume x _ y _, of_real_le_of_real) h (ne_empty_of_is_lub h)
(tendsto.comp (tendsto_id' inf_le_left) tendsto_of_real)
lemma infi_of_real {s : set ℝ} {a : ℝ} (h : is_glb s a) : (⨅a∈s, of_real a) = of_real a :=
suffices Inf (of_real '' s) = of_real a, by simpa [Inf_image],
is_glb_iff_Inf_eq.mp $ is_glb_of_is_glb_of_tendsto
(assume x _ y _, of_real_le_of_real) h (ne_empty_of_is_glb h)
(tendsto.comp (tendsto_id' inf_le_left) tendsto_of_real)
lemma Inf_add {s : set ennreal} : Inf s + a = ⨅b∈s, b + a :=
by_cases
(assume : s = ∅, by simp [this, ennreal.top_eq_infty])
(assume : s ≠ ∅,
have Inf ((λb, b + a) '' s) = Inf s + a,
from is_glb_iff_Inf_eq.mp $ is_glb_of_is_glb_of_tendsto
(assume x _ y _ h, add_le_add' h (le_refl _))
is_glb_Inf
this
(tendsto_add (tendsto_id' inf_le_left) tendsto_const_nhds),
by simp [Inf_image, -add_comm] at this; exact this.symm)
lemma Sup_add {s : set ennreal} (hs : s ≠ ∅) : Sup s + a = ⨆b∈s, b + a :=
have Sup ((λb, b + a) '' s) = Sup s + a,
from is_lub_iff_Sup_eq.mp $ is_lub_of_is_lub_of_tendsto
(assume x _ y _ h, add_le_add' h (le_refl _))
is_lub_Sup
hs
(tendsto_add (tendsto_id' inf_le_left) tendsto_const_nhds),
by simp [Sup_image, -add_comm] at this; exact this.symm
lemma supr_add {ι : Sort*} {s : ι → ennreal} [h : nonempty ι] : supr s + a = ⨆b, s b + a :=
let ⟨x⟩ := h in
calc supr s + a = Sup (range s) + a : by simp [Sup_range]
... = (⨆b∈range s, b + a) : Sup_add $ ne_empty_iff_exists_mem.mpr ⟨s x, x, rfl⟩
... = _ : by simp [supr_range, -mem_range]
lemma add_supr {ι : Sort*} {s : ι → ennreal} [h : nonempty ι] : a + supr s = ⨆b, a + s b :=
by rw [add_comm, supr_add]; simp
lemma infi_add {ι : Sort*} {s : ι → ennreal} {a : ennreal} : infi s + a = ⨅b, s b + a :=
calc infi s + a = Inf (range s) + a : by simp [Inf_range]
... = (⨅b∈range s, b + a) : Inf_add
... = _ : by simp [infi_range, -mem_range]
lemma add_infi {ι : Sort*} {s : ι → ennreal} {a : ennreal} : a + infi s = ⨅b, a + s b :=
by rw [add_comm, infi_add]; simp
lemma infi_add_infi {ι : Sort*} {f g : ι → ennreal} (h : ∀i j, ∃k, f k + g k ≤ f i + g j) :
infi f + infi g = (⨅a, f a + g a) :=
suffices (⨅a, f a + g a) ≤ infi f + infi g,
from le_antisymm (le_infi $ assume a, add_le_add' (infi_le _ _) (infi_le _ _)) this,
calc (⨅a, f a + g a) ≤ (⨅ a a', f a + g a') :
le_infi $ assume a, le_infi $ assume a',
let ⟨k, h⟩ := h a a' in infi_le_of_le k h
... ≤ infi f + infi g :
by simp [add_infi, infi_add, -add_comm, -le_infi_iff]
lemma infi_sum {α : Type*} {ι : Sort*} {f : ι → α → ennreal} {s : finset α} [inhabited ι]
(h : ∀(t : finset α) (i j : ι), ∃k, ∀a∈t, f k a ≤ f i a ∧ f k a ≤ f j a) :
(⨅i, s.sum (f i)) = s.sum (λa, ⨅i, f i a) :=
finset.induction_on s (by simp) $ assume a s ha ih,
have ∀ (i j : ι), ∃ (k : ι), f k a + s.sum (f k) ≤ f i a + s.sum (f j),
from assume i j,
let ⟨k, hk⟩ := h (insert a s) i j in
⟨k, add_le_add' (hk a (finset.mem_insert_self _ _)).left $ finset.sum_le_sum' $
assume a ha, (hk _ $ finset.mem_insert_of_mem ha).right⟩,
by simp [ha, ih.symm, infi_add_infi this]
lemma supr_add_supr {ι : Sort*} [nonempty ι]
{f g : ι → ennreal} (h : ∀i j, ∃k, f i + g j ≤ f k + g k) :
supr f + supr g = (⨆ a, f a + g a) :=
begin
refine le_antisymm _ (supr_le $ λ a, add_le_add' (le_supr _ _) (le_supr _ _)),
simpa [add_supr, supr_add] using
λ i j, show f i + g j ≤ ⨆ a, f a + g a, from
let ⟨k, hk⟩ := h i j in le_supr_of_le k hk,
end
protected lemma tendsto_of_real_sub (hr : 0 ≤ r) :
tendsto (λb, of_real r - b) (nhds b) (nhds (of_real r - b)) :=
by_cases
(assume h : of_real r < b,
suffices tendsto (λb, of_real r - b) (nhds b) (nhds ⊥),
by simpa [le_of_lt h],
by rw [nhds_bot_orderable];
from (tendsto_infi.2 $ assume p, tendsto_infi.2 $ assume hp : 0 < p, tendsto_principal.2 $
(nhds b).upwards_sets (mem_nhds_sets (is_open_lt' (of_real r)) h) $
by simp [forall_ennreal, hr, le_of_lt, hp] {contextual := tt}))
(assume h : ¬ of_real r < b,
let ⟨p, hp, hpr, eq⟩ := (le_of_real_iff hr).mp $ not_lt.1 h in
have tendsto (λb, of_real ((r - b))) (nhds p ⊓ principal {x | 0 ≤ x}) (nhds (of_real (r - p))),
from tendsto.comp (tendsto_sub tendsto_const_nhds (tendsto_id' inf_le_left)) tendsto_of_real,
have tendsto (λb, of_real r - b) (map of_real (nhds p ⊓ principal {x | 0 ≤ x}))
(nhds (of_real (r - p))),
from tendsto_map' $ tendsto_cong this $ mem_inf_sets_of_right $
by simp [(∘), -sub_eq_add_neg] {contextual:=tt},
by simp at this; simp [eq, hr, hp, hpr, nhds_of_real_eq_map_of_real_nhds_nonneg, this])
lemma sub_supr {ι : Sort*} [hι : nonempty ι] {b : ι → ennreal} (hr : a < ∞) :
a - (⨆i, b i) = (⨅i, a - b i) :=
let ⟨i⟩ := hι in
let ⟨r, hr, eq, _⟩ := lt_iff_exists_of_real.mp hr in
have Inf ((λb, of_real r - b) '' range b) = of_real r - (⨆i, b i),
from is_glb_iff_Inf_eq.mp $ is_glb_of_is_lub_of_tendsto
(assume x _ y _, sub_le_sub (le_refl _))
is_lub_supr
(ne_empty_of_mem ⟨i, rfl⟩)
(tendsto.comp (tendsto_id' inf_le_left) (ennreal.tendsto_of_real_sub hr)),
by rw [eq, ←this]; simp [Inf_image, infi_range, -mem_range]
lemma sub_infi {ι : Sort*} {b : ι → ennreal} : a - (⨅i, b i) = (⨆i, a - b i) :=
eq_of_forall_ge_iff $ λ c, begin
rw [ennreal.sub_le_iff_le_add, add_comm, infi_add],
simp [ennreal.sub_le_iff_le_add]
end
end topological_space
section tsum
variables {f g : α → ennreal}
protected lemma is_sum : is_sum f (⨆s:finset α, s.sum f) :=
tendsto_orderable.2
⟨assume a' ha',
let ⟨s, hs⟩ := lt_supr_iff.mp ha' in
mem_at_top_sets.mpr ⟨s, assume t ht, lt_of_lt_of_le hs $ finset.sum_le_sum_of_subset ht⟩,
assume a' ha',
univ_mem_sets' $ assume s,
have s.sum f ≤ ⨆(s : finset α), s.sum f,
from le_supr (λ(s : finset α), s.sum f) s,
lt_of_le_of_lt this ha'⟩
@[simp] protected lemma has_sum : has_sum f := ⟨_, ennreal.is_sum⟩
protected lemma tsum_eq_supr_sum : (∑a, f a) = (⨆s:finset α, s.sum f) :=
tsum_eq_is_sum ennreal.is_sum
protected lemma tsum_sigma {β : α → Type*} (f : Πa, β a → ennreal) :
(∑p:Σa, β a, f p.1 p.2) = (∑a b, f a b) :=
tsum_sigma (assume b, ennreal.has_sum) ennreal.has_sum
protected lemma tsum_prod {f : α → β → ennreal} : (∑p:α×β, f p.1 p.2) = (∑a, ∑b, f a b) :=
let j : α × β → (Σa:α, β) := λp, sigma.mk p.1 p.2 in
let i : (Σa:α, β) → α × β := λp, (p.1, p.2) in
let f' : (Σa:α, β) → ennreal := λp, f p.1 p.2 in
calc (∑p:α×β, f' (j p)) = (∑p:Σa:α, β, f p.1 p.2) :
tsum_eq_tsum_of_iso j i (assume ⟨a, b⟩, rfl) (assume ⟨a, b⟩, rfl)
... = (∑a, ∑b, f a b) : ennreal.tsum_sigma f
protected lemma tsum_of_real {f : α → ℝ} (h : is_sum f r) (hf : ∀a, 0 ≤ f a) :
(∑a, of_real (f a)) = of_real r :=
have (λs:finset α, s.sum (of_real ∘ f)) = of_real ∘ (λs:finset α, s.sum f),
from funext $ assume s, sum_of_real $ assume a _, hf a,
have tendsto (λs:finset α, s.sum (of_real ∘ f)) at_top (nhds (of_real r)),
by rw [this]; exact h.comp tendsto_of_real,
tsum_eq_is_sum this
protected lemma tsum_comm {f : α → β → ennreal} : (∑a, ∑b, f a b) = (∑b, ∑a, f a b) :=
let f' : α×β → ennreal := λp, f p.1 p.2 in
calc (∑a, ∑b, f a b) = (∑p:α×β, f' p) : ennreal.tsum_prod.symm
... = (∑p:β×α, f' (prod.swap p)) :
(tsum_eq_tsum_of_iso prod.swap (@prod.swap α β) (assume ⟨a, b⟩, rfl) (assume ⟨a, b⟩, rfl)).symm
... = (∑b, ∑a, f' (prod.swap (b, a))) : @ennreal.tsum_prod β α (λb a, f' (prod.swap (b, a)))
protected lemma tsum_add : (∑a, f a + g a) = (∑a, f a) + (∑a, g a) :=
tsum_add ennreal.has_sum ennreal.has_sum
protected lemma tsum_le_tsum (h : ∀a, f a ≤ g a) : (∑a, f a) ≤ (∑a, g a) :=
tsum_le_tsum h ennreal.has_sum ennreal.has_sum
protected lemma tsum_eq_supr_nat {f : ℕ → ennreal} :
(∑i:ℕ, f i) = (⨆i:ℕ, (finset.range i).sum f) :=
calc _ = (⨆s:finset ℕ, s.sum f) : ennreal.tsum_eq_supr_sum
... = (⨆i:ℕ, (finset.range i).sum f) : le_antisymm
(supr_le_supr2 $ assume s,
let ⟨n, hn⟩ := finset.exists_nat_subset_range s in
⟨n, finset.sum_le_sum_of_subset hn⟩)
(supr_le_supr2 $ assume i, ⟨finset.range i, le_refl _⟩)
protected lemma le_tsum (a : α) : f a ≤ (∑a, f a) :=
calc f a = ({a} : finset α).sum f : by simp
... ≤ (⨆s:finset α, s.sum f) : le_supr (λs:finset α, s.sum f) _
... = (∑a, f a) : by rw [ennreal.tsum_eq_supr_sum]
protected lemma mul_tsum : (∑i, a * f i) = a * (∑i, f i) :=
if h : ∀i, f i = 0 then by simp [h] else
let ⟨i, (hi : f i ≠ 0)⟩ := classical.not_forall.mp h in
have sum_ne_0 : (∑i, f i) ≠ 0, from ne_of_gt $
calc 0 < f i : lt_of_le_of_ne ennreal.zero_le hi.symm
... ≤ (∑i, f i) : ennreal.le_tsum _,
have tendsto (λs:finset α, s.sum ((*) a ∘ f)) at_top (nhds (a * (∑i, f i))),
by rw [← show (*) a ∘ (λs:finset α, s.sum f) = λs, s.sum ((*) a ∘ f),
from funext $ λ s, finset.mul_sum];
exact (is_sum_tsum ennreal.has_sum).comp (ennreal.tendsto_mul sum_ne_0),
tsum_eq_is_sum this
protected lemma tsum_mul : (∑i, f i * a) = (∑i, f i) * a :=
by simp [mul_comm, ennreal.mul_tsum]
@[simp] lemma tsum_supr_eq {α : Type*} (a : α) {f : α → ennreal} :
(∑b:α, ⨆ (h : a = b), f b) = f a :=
le_antisymm
(by rw [ennreal.tsum_eq_supr_sum]; exact supr_le (assume s,
calc s.sum (λb, ⨆ (h : a = b), f b) ≤ (finset.singleton a).sum (λb, ⨆ (h : a = b), f b) :
finset.sum_le_sum_of_ne_zero $ assume b _ hb,
suffices a = b, by simpa using this.symm,
classical.by_contradiction $ assume h,
by simpa [h] using hb
... = f a : by simp))
(calc f a ≤ (⨆ (h : a = a), f a) : le_supr (λh:a=a, f a) rfl
... ≤ (∑b:α, ⨆ (h : a = b), f b) : ennreal.le_tsum _)
theorem exists_pos_sum_of_encodable {ε : ennreal} (hε : 0 < ε)
(ι) [encodable ι] : ∃ ε' : ι → ℝ, (∀ i, 0 < ε' i) ∧ (∑ i, of_real (ε' i)) < ε :=
let ⟨a, a0, aε⟩ := dense hε,
⟨p, _, e, pε⟩ := lt_iff_exists_of_real.1 aε,
⟨ε', ε'0, c, hc, cp⟩ := pos_sum_of_encodable (zero_lt_of_real_iff.1 (e ▸ a0):0<p) ι in
⟨ε', ε'0, by rw ennreal.tsum_of_real hc (λ i, le_of_lt (ε'0 i));
exact lt_of_le_of_lt (of_real_le_of_real cp) pε⟩
end tsum
section nnreal
-- TODO: use nnreal to define ennreal
instance : has_coe nnreal ennreal := ⟨ennreal.of_real ∘ coe⟩
lemma tendsto_of_real_iff {f : filter α} {m : α → ℝ} {r : ℝ} (hm : ∀a, 0 ≤ m a) (hr : 0 ≤ r) :
tendsto (λx, of_real (m x)) f (nhds (of_real r)) ↔ tendsto m f (nhds r) :=
iff.intro
(assume h,
have tendsto (λ (x : α), of_ennreal (of_real (m x))) f (nhds r), from
h.comp (tendsto_of_ennreal hr),
by simpa [hm])
(assume h, h.comp tendsto_of_real)
lemma tendsto_coe_iff {f : filter α} {m : α → nnreal} {r : nnreal} :
tendsto (λx, (m x : ennreal)) f (nhds r) ↔ tendsto m f (nhds r) :=
iff.trans (tendsto_of_real_iff (assume a, (m a).2) r.2) nnreal.tendsto_coe
protected lemma is_sum_of_real_iff {f : α → ℝ} {r : ℝ} (hf : ∀a, 0 ≤ f a) (hr : 0 ≤ r) :
is_sum (λa, of_real (f a)) (of_real r) ↔ is_sum f r :=
by simp [is_sum, sum_of_real, hf];
exact tendsto_of_real_iff (assume s, finset.zero_le_sum $ assume a ha, hf a) hr
protected lemma is_sum_coe_iff {f : α → nnreal} {r : nnreal} :
is_sum (λa, (f a : ennreal)) r ↔ is_sum f r :=
iff.trans (ennreal.is_sum_of_real_iff (assume a, (f a).2) r.2) nnreal.is_sum_coe
protected lemma coe_tsum {f : α → nnreal} (h : has_sum f) : ↑(∑a, f a) = (∑a, f a : ennreal) :=
eq.symm (tsum_eq_is_sum $ ennreal.is_sum_coe_iff.2 $ is_sum_tsum h)
@[simp] lemma coe_mul (a b : nnreal) : ↑(a * b) = (a * b : ennreal) :=
(ennreal.of_real_mul_of_real a.2 b.2).symm
@[simp] lemma coe_one : ↑(1 : nnreal) = (1 : ennreal) := rfl
@[simp] lemma coe_eq_coe {n m : nnreal} : (↑n : ennreal) = m ↔ n = m :=
iff.trans (of_real_eq_of_real_of n.2 m.2) (iff.intro subtype.eq $ assume eq, eq ▸ rfl)
end nnreal
end ennreal
lemma has_sum_of_nonneg_of_le {f g : β → ℝ} (hg : ∀b, 0 ≤ g b) (hgf : ∀b, g b ≤ f b) :
has_sum f → has_sum g
| ⟨r, hfr⟩ :=
have hf : ∀a, 0 ≤ f a, from assume a, le_trans (hg a) (hgf a),
have hr : 0 ≤ r, from is_sum_le hf is_sum_zero hfr,
have is_sum (λa, ennreal.of_real (f a)) (ennreal.of_real r), from
(ennreal.is_sum_of_real_iff hf hr).2 hfr,
have (∑b, ennreal.of_real (g b)) ≤ ennreal.of_real r,
begin
refine is_sum_le (assume b, _) (is_sum_tsum ennreal.has_sum) this,
exact ennreal.of_real_le_of_real (hgf _)
end,
let ⟨p, hp, hpr, eq⟩ := (ennreal.le_of_real_iff hr).1 this in
have is_sum g p, from
(ennreal.is_sum_of_real_iff hg hp).1 (eq ▸ is_sum_tsum ennreal.has_sum),
has_sum_spec this
lemma nnreal.has_sum_of_le {f g : β → nnreal} (hgf : ∀b, g b ≤ f b) (hf : has_sum f) : has_sum g :=
nnreal.has_sum_coe.1 $ has_sum_of_nonneg_of_le (assume b, (g b).2) hgf $ nnreal.has_sum_coe.2 hf
|
b800c48e2ec0c79dee0f719ac9486ef0d4163ada | 205f0fc16279a69ea36e9fd158e3a97b06834ce2 | /HOMEWORK/HW7.lean | 65e805509bea184cbc58d549a2cc0817f200d95d | [] | no_license | kevinsullivan/cs-dm-lean | b21d3ca1a9b2a0751ba13fcb4e7b258010a5d124 | a06a94e98be77170ca1df486c8189338b16cf6c6 | refs/heads/master | 1,585,948,743,595 | 1,544,339,346,000 | 1,544,339,346,000 | 155,570,767 | 1 | 3 | null | 1,541,540,372,000 | 1,540,995,993,000 | Lean | UTF-8 | Lean | false | false | 5,419 | lean | /-
Homework #7. Proper subset.
-/
/-
We have studied the subset relation on
sets. A set x is said to be a subset of
y if ∀ e, e ∈ x → e in y.
By contrast, we say that x is a *proper*
subset of y if x is a subset of y and
there is some f in y that is not in x.
Here's a formal definition of this binary
relation on sets.
-/
def proper_subset :
∀ { α : Type },
set α → set α → Prop :=
λ α x y, forall (e : α),
(e ∈ x → e ∈ y) ∧ (∃ f, f ∈ y ∧ f ∉ x)
/-
Your homework, should you choose to
complete it (in preparation for the
postponed quiz), is to use "example"
to assert and prove the proposition
that { 1, 3 } is a proper subset of
the odd numbers.
-/
/-
We now introduce some new concepts
to help you through this proof. The
first has to do with the direction
of rewriting, the second explains
what the cases tactic actually does
and shows how it can then be used to
do false elimination on hypotheses
that assert incorrect equalities. We
will also remind you about how to
deal with an assumed proof of the
form, e ∈ ∅, which is obviously a
contradiction.
-/
/--/
First, a note on the rewrite tactic.
If e : x = y is an assumed proof of
x = y, then the tactic, "rewrite e"
rewrites each occurence of x in the
goal with y. Click through the proof
of the following trivial proposition
to see the idea in action.
-/
example :
∀ a b c : ℕ, a = b → b = c → a = c
:=
begin
intros a b c,
assume ab bc,
rewrite ab,
rw bc, -- shorthand
-- rw does "apply rfl" for us automatically
end
/-
If you want the rewriting to go in
the other direction, use "rw ←e"
-/
example :
∀ a b c : ℕ, a = b → b = c → a = c
:=
begin
intros a b c,
assume ab bc,
rewrite ←bc,
rw ←ab,
end
/-
Second, let's talk more about cases.
Whenever we have any data value, even
if its a proof, it must have been built
by one of the constructors defined for
that type of data.
With natural numbers, for example, a
given ℕ must be either 0 (constructed
by nat.zero) or 1 + a smaller natural
number (built by applying nat.succ to
the next smaller ℕ).
So if we have some value, n : ℕ,
even though we don't know what its
value is, we do know there are only
two ways it could be been produced.
So, if we want to prove something
about n, it suffices to show it is
true no matter which constructor
was used to "build" n. Case analysis
(and the cases tactic) replaces an
assumed value in the context with
each of the constructors that could
have been used to build it. Click
through the following proof to see
how this plays out, not when we
apply cases to a disjunction, but
to a natural number.
-/
example : ∀ n : ℕ, true :=
begin
assume n,
cases n, -- whoa, that's cool
trivial,
trivial,
end
/-
The same idea holds for values that
are proofs. Suppose we have a proof
of, say, a disjunction, P ∨ Q. There
are only two ways that proof could
have been built: by or.inl or or.inr.
So when we use cases on a proof of a
disjunction, even though we don't
know exactly how it was proved, if
we can prove our goal for each of
the two ways in which it *could* have
been built, then we've proved our
goal in either case, as there are no
other ways in which we could have had
the value in the first place.
-/
example : ∀ P Q : Prop, P ∨ Q → true :=
begin
intros P Q,
assume h,
cases h, -- whoa, now I get it!
trivial,
trivial
end
/-
Okay, so here's the cool thing. If you
end up with an assumption, such as 2 = 3,
in your context, which is to say a wrong
proof of an equality, you can do case
analysis on it. We've already seen that
there is only one way to construct a proof
of an equality, using eq.refl applied to one
value. So if you have assumed a proof
of 2 = 3, when you do case analysis on
it, Lean will see that there are *no*
constructors that could have been used
to construct that proof. You thus have
zero cases to prove and you're done! It
is almost magic. But it's not. Having no
cases to prove is just false elimination
under a different guise.
-/
example : 4 = 5 → 0 = 1 :=
begin
assume four_equals_five,
cases four_equals_five, --whoa!
end
/-
Third if you have an assumption of
the form, h : e ∈ ∅, remember you
can use have f : false := h. That
gets you what you need to be done.
Oh, and don't forget that e ∉ x is
the same as ¬ (e ∈ x), and that of
course is just (e ∈ x) → false. Or,
just try cases!
And here are a few final hints.
(1) Remember that a set in Lean is
really represented as a membership
predicate.
(1A) When a set, such as { 1 , 3 },
viewed as a membership predicate,
is applied to a value, it reduces
to a proposition, here in the form
of a disjunction. So of you end up
with something like e ∈ { 1, 3 } as
an assumption, treat it as a proof
of a disjunction.
(1B) If you end up with a goal of
the form of a membership proposition,
such as the following, for example,
α ∈ {n : ℕ | ∃ (m : ℕ), n = m }
think of it as the application of
the predicate/function defined by
the set, taking argument, n, to α.
The overall expression reduces to
∃ (m : ℕ), α = m. You need to see
this to know what inference rule
to use to prove such a goal.
-/
example :
proper_subset
{ 1, 3 }
{ n | ∃ m, n = 2 * m + 1 }
:=
begin
_
end
example : ∀ n : ℕ, n - n = 0:=
begin
intro n,
have es := add_eq_of_eq_sub,
apply eq.symm,
end |
0017044215a04942b2ac798f43213650e402204e | a4673261e60b025e2c8c825dfa4ab9108246c32e | /stage0/src/Lean/Elab/Frontend.lean | f62e8ef279fba1916f22891028384d74fcd745b1 | [
"Apache-2.0"
] | permissive | jcommelin/lean4 | c02dec0cc32c4bccab009285475f265f17d73228 | 2909313475588cc20ac0436e55548a4502050d0a | refs/heads/master | 1,674,129,550,893 | 1,606,415,348,000 | 1,606,415,348,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,261 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Sebastian Ullrich
-/
import Lean.Elab.Import
import Lean.Elab.Command
import Lean.Util.Profile
namespace Lean.Elab.Frontend
structure State :=
(commandState : Command.State)
(parserState : Parser.ModuleParserState)
(cmdPos : String.Pos)
(commands : Array Syntax := #[])
structure Context :=
(inputCtx : Parser.InputContext)
abbrev FrontendM := ReaderT Context $ StateRefT State IO
def setCommandState (commandState : Command.State) : FrontendM Unit :=
modify fun s => { s with commandState := commandState }
@[inline] def runCommandElabM (x : Command.CommandElabM Unit) : FrontendM Unit := do
let ctx ← read
let s ← get
let cmdCtx : Command.Context := { cmdPos := s.cmdPos, fileName := ctx.inputCtx.fileName, fileMap := ctx.inputCtx.fileMap }
let sNew? ← liftM $ EIO.toIO (fun _ => IO.Error.userError "unexpected error") (do let (_, s) ← (x cmdCtx).run s.commandState; pure $ some s)
match sNew? with
| some sNew => setCommandState sNew
| none => pure ()
def elabCommandAtFrontend (stx : Syntax) : FrontendM Unit := do
runCommandElabM (Command.elabCommand stx)
def updateCmdPos : FrontendM Unit := do
modify fun s => { s with cmdPos := s.parserState.pos }
def getParserState : FrontendM Parser.ModuleParserState := do pure (← get).parserState
def getCommandState : FrontendM Command.State := do pure (← get).commandState
def setParserState (ps : Parser.ModuleParserState) : FrontendM Unit := modify fun s => { s with parserState := ps }
def setMessages (msgs : MessageLog) : FrontendM Unit := modify fun s => { s with commandState := { s.commandState with messages := msgs } }
def getInputContext : FrontendM Parser.InputContext := do pure (← read).inputCtx
def processCommand : FrontendM Bool := do
updateCmdPos
let cmdState ← getCommandState
let ictx ← getInputContext
let pstate ← getParserState
let pos := ictx.fileMap.toPosition pstate.pos
match profileit "parsing" pos fun _ => Parser.parseCommand cmdState.env ictx pstate cmdState.messages with
| (cmd, ps, messages) =>
modify fun s => { s with commands := s.commands.push cmd }
setParserState ps
setMessages messages
if Parser.isEOI cmd || Parser.isExitCommand cmd then
pure true -- Done
else
profileitM IO.Error "elaboration" pos $ elabCommandAtFrontend cmd
pure false
partial def processCommands : FrontendM Unit := do
let done ← processCommand
unless done do
processCommands
end Frontend
open Frontend
def IO.processCommands (inputCtx : Parser.InputContext) (parserState : Parser.ModuleParserState) (commandState : Command.State) : IO State := do
let (_, s) ← (Frontend.processCommands.run { inputCtx := inputCtx }).run { commandState := commandState, parserState := parserState, cmdPos := parserState.pos }
pure s
def process (input : String) (env : Environment) (opts : Options) (fileName : Option String := none) : IO (Environment × MessageLog) := do
let fileName := fileName.getD "<input>"
let inputCtx := Parser.mkInputContext input fileName
let s ← IO.processCommands inputCtx { : Parser.ModuleParserState } (Command.mkState env {} opts)
pure (s.commandState.env, s.commandState.messages)
@[export lean_process_input]
def processExport (env : Environment) (input : String) (opts : Options) (fileName : String) : IO (Environment × List Message) := do
let (env, messages) ← process input env opts fileName
pure (env, messages.toList)
@[export lean_run_frontend]
def runFrontend (input : String) (opts : Options) (fileName : String) (mainModuleName : Name) : IO (Environment × List Message × Module) := do
let inputCtx := Parser.mkInputContext input fileName
let (header, parserState, messages) ← Parser.parseHeader inputCtx
let (env, messages) ← processHeader header opts messages inputCtx
let env := env.setMainModule mainModuleName
let s ← IO.processCommands inputCtx parserState (Command.mkState env messages opts)
pure (s.commandState.env, s.commandState.messages.toList, { header := header, commands := s.commands })
end Lean.Elab
|
504eee2fc7665c15b57ce04afdb7caddd3c3fcfd | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/zsqrtd/to_real_auto.lean | 906543913b24ee0b649c076f22d5a8404a5b49d1 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,078 | lean | /-
Copyright (c) 2021 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.real.sqrt
import Mathlib.data.zsqrtd.basic
import Mathlib.PostPort
namespace Mathlib
/-!
# Image of `zsqrtd` in `ℝ`
This file defines `zsqrtd.to_real` and related lemmas.
It is in a separate file to avoid pulling in all of `data.real` into `data.zsqrtd`.
-/
namespace zsqrtd
/-- The image of `zsqrtd` in `ℝ`, using `real.sqrt` which takes the positive root of `d`.
If the negative root is desired, use `to_real h a.conj`. -/
@[simp] theorem to_real_apply {d : ℤ} (h : 0 ≤ d) (a : ℤ√d) :
coe_fn (to_real h) a = ↑(re a) + ↑(im a) * real.sqrt ↑d :=
Eq.refl (↑(re a) + ↑(im a) * real.sqrt ↑d)
theorem to_real_injective {d : ℤ} (h0d : 0 ≤ d) (hd : ∀ (n : ℤ), d ≠ n * n) :
function.injective ⇑(to_real h0d) :=
lift_injective { val := real.sqrt ↑d, property := to_real._proof_1 h0d } hd
end Mathlib |
cf901a23ad2078a1c891b654d8021d1f80be9c86 | 618003631150032a5676f229d13a079ac875ff77 | /src/logic/unique.lean | 656553e16a5393a77e4d347bc4e0007558beeb0b | [
"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 | 2,561 | lean | /-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import tactic.ext
universes u v w
variables {α : Sort u} {β : Sort v} {γ : Sort w}
@[ext]
structure unique (α : Sort u) extends inhabited α :=
(uniq : ∀ a:α, a = default)
attribute [class] unique
instance punit.unique : unique punit.{u} :=
{ default := punit.star,
uniq := λ x, punit_eq x _ }
instance fin.unique : unique (fin 1) :=
{ default := 0,
uniq := λ ⟨n, hn⟩, fin.eq_of_veq
(nat.eq_zero_of_le_zero (nat.le_of_lt_succ hn)) }
namespace unique
open function
section
variables [unique α]
@[priority 100] -- see Note [lower instance priority]
instance : inhabited α := to_inhabited ‹unique α›
lemma eq_default (a : α) : a = default α := uniq _ a
lemma default_eq (a : α) : default α = a := (uniq _ a).symm
@[priority 100] -- see Note [lower instance priority]
instance : subsingleton α := ⟨λ a b, by rw [eq_default a, eq_default b]⟩
lemma forall_iff {p : α → Prop} : (∀ a, p a) ↔ p (default α) :=
⟨λ h, h _, λ h x, by rwa [unique.eq_default x]⟩
lemma exists_iff {p : α → Prop} : Exists p ↔ p (default α) :=
⟨λ ⟨a, ha⟩, eq_default a ▸ ha, exists.intro (default α)⟩
end
protected lemma subsingleton_unique' : ∀ (h₁ h₂ : unique α), h₁ = h₂
| ⟨⟨x⟩, h⟩ ⟨⟨y⟩, _⟩ := by congr; rw [h x, h y]
instance subsingleton_unique : subsingleton (unique α) :=
⟨unique.subsingleton_unique'⟩
end unique
namespace function
variable {f : α → β}
/-- If the domain of a surjective function is a singleton,
then the codomain is a singleton as well. -/
def surjective.unique (hf : surjective f) [unique α] : unique β :=
{ default := f (default _),
uniq := λ b, let ⟨a, ha⟩ := hf b in ha ▸ congr_arg f (unique.eq_default _) }
/-- If the codomain of an injective function is a subsingleton, then the domain
is a subsingleton as well. -/
lemma injective.comap_subsingleton (hf : injective f) [subsingleton β] :
subsingleton α :=
⟨λ x y, hf $ subsingleton.elim _ _⟩
end function
lemma nonempty_unique_or_exists_ne (x : α) : nonempty (unique α) ∨ ∃ y, y ≠ x :=
classical.by_cases or.inr
(λ h, or.inl ⟨{ default := x,
uniq := λ y, classical.by_contradiction $ λ hy, h ⟨y, hy⟩ }⟩)
lemma subsingleton_or_exists_ne (x : α) : subsingleton α ∨ ∃ y, y ≠ x :=
(nonempty_unique_or_exists_ne x).elim
(λ ⟨h⟩, or.inl $ @unique.subsingleton _ h)
or.inr
|
570099437199986afd8e32b077551e9a48258315 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/data/pi.lean | 6a2864ab0735fa2c4295ac9e6dca9b9d7dd9471b | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,668 | lean | /-
Copyright (c) 2020 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon, Patrick Massot, Eric Wieser
-/
import tactic.split_ifs
import tactic.simpa
import tactic.congr
import algebra.group.to_additive
/-!
# Instances and theorems on pi types
This file provides basic definitions and notation instances for Pi types.
Instances of more sophisticated classes are defined in `pi.lean` files elsewhere.
-/
universes u v₁ v₂ v₃
variable {I : Type u} -- The indexing type
-- The families of types already equipped with instances
variables {f : I → Type v₁} {g : I → Type v₂} {h : I → Type v₃}
variables (x y : Π i, f i) (i : I)
namespace pi
/-! `1`, `0`, `+`, `*`, `-`, `⁻¹`, and `/` are defined pointwise. -/
@[to_additive] instance has_one [∀ i, has_one $ f i] :
has_one (Π i : I, f i) :=
⟨λ _, 1⟩
@[simp, to_additive] lemma one_apply [∀ i, has_one $ f i] : (1 : Π i, f i) i = 1 := rfl
@[to_additive] lemma one_def [Π i, has_one $ f i] : (1 : Π i, f i) = λ i, 1 := rfl
@[to_additive]
instance has_mul [∀ i, has_mul $ f i] :
has_mul (Π i : I, f i) :=
⟨λ f g i, f i * g i⟩
@[simp, to_additive] lemma mul_apply [∀ i, has_mul $ f i] : (x * y) i = x i * y i := rfl
@[to_additive] lemma mul_def [Π i, has_mul $ f i] : x * y = λ i, x i * y i := rfl
@[to_additive] instance has_inv [∀ i, has_inv $ f i] :
has_inv (Π i : I, f i) :=
⟨λ f i, (f i)⁻¹⟩
@[simp, to_additive] lemma inv_apply [∀ i, has_inv $ f i] : x⁻¹ i = (x i)⁻¹ := rfl
@[to_additive] lemma inv_def [Π i, has_inv $ f i] : x⁻¹ = λ i, (x i)⁻¹ := rfl
@[to_additive] instance has_div [Π i, has_div $ f i] :
has_div (Π i : I, f i) :=
⟨λ f g i, f i / g i⟩
@[simp, to_additive] lemma div_apply [Π i, has_div $ f i] : (x / y) i = x i / y i := rfl
@[to_additive] lemma div_def [Π i, has_div $ f i] : x / y = λ i, x i / y i := rfl
section
variables [decidable_eq I]
variables [Π i, has_zero (f i)] [Π i, has_zero (g i)] [Π i, has_zero (h i)]
/-- The function supported at `i`, with value `x` there. -/
def single (i : I) (x : f i) : Π i, f i :=
function.update 0 i x
@[simp] lemma single_eq_same (i : I) (x : f i) : single i x i = x :=
function.update_same i x _
@[simp] lemma single_eq_of_ne {i i' : I} (h : i' ≠ i) (x : f i) : single i x i' = 0 :=
function.update_noteq h x _
/-- Abbreviation for `single_eq_of_ne h.symm`, for ease of use by `simp`. -/
@[simp] lemma single_eq_of_ne' {i i' : I} (h : i ≠ i') (x : f i) : single i x i' = 0 :=
single_eq_of_ne h.symm x
@[simp] lemma single_zero (i : I) : single i (0 : f i) = 0 :=
function.update_eq_self _ _
/-- On non-dependent functions, `pi.single` can be expressed as an `ite` -/
lemma single_apply {β : Sort*} [has_zero β] (i : I) (x : β) (i' : I) :
single i x i' = if i' = i then x else 0 :=
function.update_apply 0 i x i'
/-- On non-dependent functions, `pi.single` is symmetric in the two indices. -/
lemma single_comm {β : Sort*} [has_zero β] (i : I) (x : β) (i' : I) :
single i x i' = single i' x i :=
by simp only [single_apply, eq_comm]; congr -- deal with `decidable_eq`
lemma apply_single (f' : Π i, f i → g i) (hf' : ∀ i, f' i 0 = 0) (i : I) (x : f i) (j : I):
f' j (single i x j) = single i (f' i x) j :=
by simpa only [pi.zero_apply, hf', single] using function.apply_update f' 0 i x j
lemma apply_single₂ (f' : Π i, f i → g i → h i) (hf' : ∀ i, f' i 0 0 = 0)
(i : I) (x : f i) (y : g i) (j : I):
f' j (single i x j) (single i y j) = single i (f' i x y) j :=
begin
by_cases h : j = i,
{ subst h, simp only [single_eq_same] },
{ simp only [single_eq_of_ne h, hf'] },
end
lemma single_op {g : I → Type*} [Π i, has_zero (g i)] (op : Π i, f i → g i) (h : ∀ i, op i 0 = 0)
(i : I) (x : f i) :
single i (op i x) = λ j, op j (single i x j) :=
eq.symm $ funext $ apply_single op h i x
lemma single_op₂ {g₁ g₂ : I → Type*} [Π i, has_zero (g₁ i)] [Π i, has_zero (g₂ i)]
(op : Π i, g₁ i → g₂ i → f i) (h : ∀ i, op i 0 0 = 0) (i : I) (x₁ : g₁ i) (x₂ : g₂ i) :
single i (op i x₁ x₂) = λ j, op j (single i x₁ j) (single i x₂ j) :=
eq.symm $ funext $ apply_single₂ op h i x₁ x₂
variables (f)
lemma single_injective (i : I) : function.injective (single i : f i → Π i, f i) :=
function.update_injective _ i
end
end pi
section extend
namespace function
variables {α β γ : Type*}
@[to_additive]
lemma extend_one [has_one γ] (f : α → β) :
function.extend f (1 : α → γ) (1 : β → γ) = 1 :=
funext $ λ _, by apply if_t_t _ _
@[to_additive]
lemma extend_mul [has_mul γ] (f : α → β) (g₁ g₂ : α → γ) (e₁ e₂ : β → γ) :
function.extend f (g₁ * g₂) (e₁ * e₂) = function.extend f g₁ e₁ * function.extend f g₂ e₂ :=
funext $ λ _, by convert (apply_dite2 (*) _ _ _ _ _).symm
@[to_additive]
lemma extend_inv [has_inv γ] (f : α → β) (g : α → γ) (e : β → γ) :
function.extend f (g⁻¹) (e⁻¹) = (function.extend f g e)⁻¹ :=
funext $ λ _, by convert (apply_dite has_inv.inv _ _ _).symm
@[to_additive]
lemma extend_div [has_div γ] (f : α → β) (g₁ g₂ : α → γ) (e₁ e₂ : β → γ) :
function.extend f (g₁ / g₂) (e₁ / e₂) = function.extend f g₁ e₁ / function.extend f g₂ e₂ :=
funext $ λ _, by convert (apply_dite2 (/) _ _ _ _ _).symm
end function
end extend
lemma subsingleton.pi_single_eq {α : Type*} [decidable_eq I] [subsingleton I] [has_zero α]
(i : I) (x : α) :
pi.single i x = λ _, x :=
funext $ λ j, by rw [subsingleton.elim j i, pi.single_eq_same]
|
15e7779a791bfa32ade436846e8a5796927ea0a2 | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/analysis/special_functions/exp_log.lean | 714a723261e59f7782a92727b5dbba4632c70041 | [
"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 | 34,897 | 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
-/
import data.complex.exponential
import analysis.calculus.inverse
import measure_theory.borel_space
import analysis.complex.real_deriv
/-!
# Complex and real exponential, real logarithm
## Main statements
This file establishes the basic analytical properties of the complex and real exponential functions
(continuity, differentiability, computation of the derivative).
It also contains the definition of the real logarithm function (as the inverse of the
exponential on `(0, +∞)`, extended to `ℝ` by setting `log (-x) = log x`) and its basic
properties (continuity, differentiability, formula for the derivative).
The complex logarithm is *not* defined in this file as it relies on trigonometric functions. See
instead `trigonometric.lean`.
## Tags
exp, log
-/
noncomputable theory
open finset filter metric asymptotics set function
open_locale classical topological_space
namespace complex
lemma measurable_re : measurable re := continuous_re.measurable
lemma measurable_im : measurable im := continuous_im.measurable
lemma measurable_of_real : measurable (coe : ℝ → ℂ) := continuous_of_real.measurable
/-- The complex exponential is everywhere differentiable, with the derivative `exp x`. -/
lemma has_deriv_at_exp (x : ℂ) : has_deriv_at exp (exp x) x :=
begin
rw has_deriv_at_iff_is_o_nhds_zero,
have : (1 : ℕ) < 2 := by norm_num,
refine (is_O.of_bound (∥exp x∥) _).trans_is_o (is_o_pow_id this),
filter_upwards [metric.ball_mem_nhds (0 : ℂ) zero_lt_one],
simp only [metric.mem_ball, dist_zero_right, normed_field.norm_pow],
intros z hz,
calc ∥exp (x + z) - exp x - z * exp x∥
= ∥exp x * (exp z - 1 - z)∥ : by { congr, rw [exp_add], ring }
... = ∥exp x∥ * ∥exp z - 1 - z∥ : normed_field.norm_mul _ _
... ≤ ∥exp x∥ * ∥z∥^2 :
mul_le_mul_of_nonneg_left (abs_exp_sub_one_sub_id_le (le_of_lt hz)) (norm_nonneg _)
end
lemma differentiable_exp : differentiable ℂ exp :=
λx, (has_deriv_at_exp x).differentiable_at
lemma differentiable_at_exp {x : ℂ} : differentiable_at ℂ exp x :=
differentiable_exp x
@[simp] lemma deriv_exp : deriv exp = exp :=
funext $ λ x, (has_deriv_at_exp x).deriv
@[simp] lemma iter_deriv_exp : ∀ n : ℕ, (deriv^[n] exp) = exp
| 0 := rfl
| (n+1) := by rw [iterate_succ_apply, deriv_exp, iter_deriv_exp n]
lemma continuous_exp : continuous exp :=
differentiable_exp.continuous
lemma times_cont_diff_exp : ∀ {n}, times_cont_diff ℂ n exp :=
begin
refine times_cont_diff_all_iff_nat.2 (λ n, _),
induction n with n ihn,
{ exact times_cont_diff_zero.2 continuous_exp },
{ rw times_cont_diff_succ_iff_deriv,
use differentiable_exp,
rwa deriv_exp }
end
lemma has_strict_deriv_at_exp (x : ℂ) : has_strict_deriv_at exp (exp x) x :=
times_cont_diff_exp.times_cont_diff_at.has_strict_deriv_at' (has_deriv_at_exp x) le_rfl
lemma is_open_map_exp : is_open_map exp :=
open_map_of_strict_deriv has_strict_deriv_at_exp exp_ne_zero
lemma measurable_exp : measurable exp := continuous_exp.measurable
end complex
section
variables {f : ℂ → ℂ} {f' x : ℂ} {s : set ℂ}
lemma has_strict_deriv_at.cexp (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, complex.exp (f x)) (complex.exp (f x) * f') x :=
(complex.has_strict_deriv_at_exp (f x)).comp x hf
lemma has_deriv_at.cexp (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, complex.exp (f x)) (complex.exp (f x) * f') x :=
(complex.has_deriv_at_exp (f x)).comp x hf
lemma has_deriv_within_at.cexp (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, complex.exp (f x)) (complex.exp (f x) * f') s x :=
(complex.has_deriv_at_exp (f x)).comp_has_deriv_within_at x hf
lemma deriv_within_cexp (hf : differentiable_within_at ℂ f s x)
(hxs : unique_diff_within_at ℂ s x) :
deriv_within (λx, complex.exp (f x)) s x = complex.exp (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.cexp.deriv_within hxs
@[simp] lemma deriv_cexp (hc : differentiable_at ℂ f x) :
deriv (λx, complex.exp (f x)) x = complex.exp (f x) * (deriv f x) :=
hc.has_deriv_at.cexp.deriv
end
section
variables {E : Type*} [normed_group E] [normed_space ℂ E] {f : E → ℂ} {f' : E →L[ℂ] ℂ}
{x : E} {s : set E}
lemma measurable.cexp {α : Type*} [measurable_space α] {f : α → ℂ} (hf : measurable f) :
measurable (λ x, complex.exp (f x)) :=
complex.measurable_exp.comp hf
lemma has_strict_fderiv_at.cexp (hf : has_strict_fderiv_at f f' x) :
has_strict_fderiv_at (λ x, complex.exp (f x)) (complex.exp (f x) • f') x :=
(complex.has_strict_deriv_at_exp (f x)).comp_has_strict_fderiv_at x hf
lemma has_fderiv_within_at.cexp (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ x, complex.exp (f x)) (complex.exp (f x) • f') s x :=
(complex.has_deriv_at_exp (f x)).comp_has_fderiv_within_at x hf
lemma has_fderiv_at.cexp (hf : has_fderiv_at f f' x) :
has_fderiv_at (λ x, complex.exp (f x)) (complex.exp (f x) • f') x :=
has_fderiv_within_at_univ.1 $ hf.has_fderiv_within_at.cexp
lemma differentiable_within_at.cexp (hf : differentiable_within_at ℂ f s x) :
differentiable_within_at ℂ (λ x, complex.exp (f x)) s x :=
hf.has_fderiv_within_at.cexp.differentiable_within_at
@[simp] lemma differentiable_at.cexp (hc : differentiable_at ℂ f x) :
differentiable_at ℂ (λx, complex.exp (f x)) x :=
hc.has_fderiv_at.cexp.differentiable_at
lemma differentiable_on.cexp (hc : differentiable_on ℂ f s) :
differentiable_on ℂ (λx, complex.exp (f x)) s :=
λx h, (hc x h).cexp
@[simp] lemma differentiable.cexp (hc : differentiable ℂ f) :
differentiable ℂ (λx, complex.exp (f x)) :=
λx, (hc x).cexp
lemma times_cont_diff.cexp {n} (h : times_cont_diff ℂ n f) :
times_cont_diff ℂ n (λ x, complex.exp (f x)) :=
complex.times_cont_diff_exp.comp h
lemma times_cont_diff_at.cexp {n} (hf : times_cont_diff_at ℂ n f x) :
times_cont_diff_at ℂ n (λ x, complex.exp (f x)) x :=
complex.times_cont_diff_exp.times_cont_diff_at.comp x hf
lemma times_cont_diff_on.cexp {n} (hf : times_cont_diff_on ℂ n f s) :
times_cont_diff_on ℂ n (λ x, complex.exp (f x)) s :=
complex.times_cont_diff_exp.comp_times_cont_diff_on hf
lemma times_cont_diff_within_at.cexp {n} (hf : times_cont_diff_within_at ℂ n f s x) :
times_cont_diff_within_at ℂ n (λ x, complex.exp (f x)) s x :=
complex.times_cont_diff_exp.times_cont_diff_at.comp_times_cont_diff_within_at x hf
end
namespace real
variables {x y z : ℝ}
lemma has_strict_deriv_at_exp (x : ℝ) : has_strict_deriv_at exp (exp x) x :=
(complex.has_strict_deriv_at_exp x).real_of_complex
lemma has_deriv_at_exp (x : ℝ) : has_deriv_at exp (exp x) x :=
(complex.has_deriv_at_exp x).real_of_complex
lemma times_cont_diff_exp {n} : times_cont_diff ℝ n exp :=
complex.times_cont_diff_exp.real_of_complex
lemma differentiable_exp : differentiable ℝ exp :=
λx, (has_deriv_at_exp x).differentiable_at
lemma differentiable_at_exp : differentiable_at ℝ exp x :=
differentiable_exp x
@[simp] lemma deriv_exp : deriv exp = exp :=
funext $ λ x, (has_deriv_at_exp x).deriv
@[simp] lemma iter_deriv_exp : ∀ n : ℕ, (deriv^[n] exp) = exp
| 0 := rfl
| (n+1) := by rw [iterate_succ_apply, deriv_exp, iter_deriv_exp n]
lemma continuous_exp : continuous exp :=
differentiable_exp.continuous
lemma measurable_exp : measurable exp := continuous_exp.measurable
end real
section
/-! Register lemmas for the derivatives of the composition of `real.exp` with a differentiable
function, for standalone use and use with `simp`. -/
variables {f : ℝ → ℝ} {f' x : ℝ} {s : set ℝ}
lemma has_strict_deriv_at.exp (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, real.exp (f x)) (real.exp (f x) * f') x :=
(real.has_strict_deriv_at_exp (f x)).comp x hf
lemma has_deriv_at.exp (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, real.exp (f x)) (real.exp (f x) * f') x :=
(real.has_deriv_at_exp (f x)).comp x hf
lemma has_deriv_within_at.exp (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, real.exp (f x)) (real.exp (f x) * f') s x :=
(real.has_deriv_at_exp (f x)).comp_has_deriv_within_at x hf
lemma deriv_within_exp (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
deriv_within (λx, real.exp (f x)) s x = real.exp (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.exp.deriv_within hxs
@[simp] lemma deriv_exp (hc : differentiable_at ℝ f x) :
deriv (λx, real.exp (f x)) x = real.exp (f x) * (deriv f x) :=
hc.has_deriv_at.exp.deriv
end
section
/-! Register lemmas for the derivatives of the composition of `real.exp` with a differentiable
function, for standalone use and use with `simp`. -/
variables {E : Type*} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {f' : E →L[ℝ] ℝ}
{x : E} {s : set E}
lemma measurable.exp {α : Type*} [measurable_space α] {f : α → ℝ} (hf : measurable f) :
measurable (λ x, real.exp (f x)) :=
real.measurable_exp.comp hf
lemma times_cont_diff.exp {n} (hf : times_cont_diff ℝ n f) :
times_cont_diff ℝ n (λ x, real.exp (f x)) :=
real.times_cont_diff_exp.comp hf
lemma times_cont_diff_at.exp {n} (hf : times_cont_diff_at ℝ n f x) :
times_cont_diff_at ℝ n (λ x, real.exp (f x)) x :=
real.times_cont_diff_exp.times_cont_diff_at.comp x hf
lemma times_cont_diff_on.exp {n} (hf : times_cont_diff_on ℝ n f s) :
times_cont_diff_on ℝ n (λ x, real.exp (f x)) s :=
real.times_cont_diff_exp.comp_times_cont_diff_on hf
lemma times_cont_diff_within_at.exp {n} (hf : times_cont_diff_within_at ℝ n f s x) :
times_cont_diff_within_at ℝ n (λ x, real.exp (f x)) s x :=
real.times_cont_diff_exp.times_cont_diff_at.comp_times_cont_diff_within_at x hf
lemma has_fderiv_within_at.exp (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ x, real.exp (f x)) (real.exp (f x) • f') s x :=
(real.has_deriv_at_exp (f x)).comp_has_fderiv_within_at x hf
lemma has_fderiv_at.exp (hf : has_fderiv_at f f' x) :
has_fderiv_at (λ x, real.exp (f x)) (real.exp (f x) • f') x :=
(real.has_deriv_at_exp (f x)).comp_has_fderiv_at x hf
lemma has_strict_fderiv_at.exp (hf : has_strict_fderiv_at f f' x) :
has_strict_fderiv_at (λ x, real.exp (f x)) (real.exp (f x) • f') x :=
(real.has_strict_deriv_at_exp (f x)).comp_has_strict_fderiv_at x hf
lemma differentiable_within_at.exp (hf : differentiable_within_at ℝ f s x) :
differentiable_within_at ℝ (λ x, real.exp (f x)) s x :=
hf.has_fderiv_within_at.exp.differentiable_within_at
@[simp] lemma differentiable_at.exp (hc : differentiable_at ℝ f x) :
differentiable_at ℝ (λx, real.exp (f x)) x :=
hc.has_fderiv_at.exp.differentiable_at
lemma differentiable_on.exp (hc : differentiable_on ℝ f s) :
differentiable_on ℝ (λx, real.exp (f x)) s :=
λ x h, (hc x h).exp
@[simp] lemma differentiable.exp (hc : differentiable ℝ f) :
differentiable ℝ (λx, real.exp (f x)) :=
λ x, (hc x).exp
lemma fderiv_within_exp (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
fderiv_within ℝ (λx, real.exp (f x)) s x = real.exp (f x) • (fderiv_within ℝ f s x) :=
hf.has_fderiv_within_at.exp.fderiv_within hxs
@[simp] lemma fderiv_exp (hc : differentiable_at ℝ f x) :
fderiv ℝ (λx, real.exp (f x)) x = real.exp (f x) • (fderiv ℝ f x) :=
hc.has_fderiv_at.exp.fderiv
end
namespace real
variables {x y z : ℝ}
/-- The real exponential function tends to `+∞` at `+∞`. -/
lemma tendsto_exp_at_top : tendsto exp at_top at_top :=
begin
have A : tendsto (λx:ℝ, x + 1) at_top at_top :=
tendsto_at_top_add_const_right at_top 1 tendsto_id,
have B : ∀ᶠ x in at_top, x + 1 ≤ exp x :=
eventually_at_top.2 ⟨0, λx hx, add_one_le_exp_of_nonneg hx⟩,
exact tendsto_at_top_mono' at_top B A
end
/-- The real exponential function tends to `0` at `-∞` or, equivalently, `exp(-x)` tends to `0`
at `+∞` -/
lemma tendsto_exp_neg_at_top_nhds_0 : tendsto (λx, exp (-x)) at_top (𝓝 0) :=
(tendsto_inv_at_top_zero.comp tendsto_exp_at_top).congr (λx, (exp_neg x).symm)
/-- The real exponential function tends to `1` at `0`. -/
lemma tendsto_exp_nhds_0_nhds_1 : tendsto exp (𝓝 0) (𝓝 1) :=
by { convert continuous_exp.tendsto 0, simp }
lemma tendsto_exp_at_bot : tendsto exp at_bot (𝓝 0) :=
(tendsto_exp_neg_at_top_nhds_0.comp tendsto_neg_at_bot_at_top).congr $
λ x, congr_arg exp $ neg_neg x
lemma tendsto_exp_at_bot_nhds_within : tendsto exp at_bot (𝓝[Ioi 0] 0) :=
tendsto_inf.2 ⟨tendsto_exp_at_bot, tendsto_principal.2 $ eventually_of_forall exp_pos⟩
/-- `real.exp` as an order isomorphism between `ℝ` and `(0, +∞)`. -/
def exp_order_iso : ℝ ≃o Ioi (0 : ℝ) :=
strict_mono.order_iso_of_surjective _ (exp_strict_mono.cod_restrict exp_pos) $
(continuous_subtype_mk _ continuous_exp).surjective
(by simp only [tendsto_Ioi_at_top, subtype.coe_mk, tendsto_exp_at_top])
(by simp [tendsto_exp_at_bot_nhds_within])
@[simp] lemma coe_exp_order_iso_apply (x : ℝ) : (exp_order_iso x : ℝ) = exp x := rfl
@[simp] lemma coe_comp_exp_order_iso : coe ∘ exp_order_iso = exp := rfl
@[simp] lemma range_exp : range exp = Ioi 0 :=
by rw [← coe_comp_exp_order_iso, range_comp, exp_order_iso.range_eq, image_univ, subtype.range_coe]
@[simp] lemma map_exp_at_top : map exp at_top = at_top :=
by rw [← coe_comp_exp_order_iso, ← filter.map_map, order_iso.map_at_top, map_coe_Ioi_at_top]
@[simp] lemma comap_exp_at_top : comap exp at_top = at_top :=
by rw [← map_exp_at_top, comap_map exp_injective, map_exp_at_top]
@[simp] lemma tendsto_exp_comp_at_top {α : Type*} {l : filter α} {f : α → ℝ} :
tendsto (λ x, exp (f x)) l at_top ↔ tendsto f l at_top :=
by rw [← tendsto_comap_iff, comap_exp_at_top]
lemma tendsto_comp_exp_at_top {α : Type*} {l : filter α} {f : ℝ → α} :
tendsto (λ x, f (exp x)) at_top l ↔ tendsto f at_top l :=
by rw [← tendsto_map'_iff, map_exp_at_top]
@[simp] lemma map_exp_at_bot : map exp at_bot = 𝓝[Ioi 0] 0 :=
by rw [← coe_comp_exp_order_iso, ← filter.map_map, exp_order_iso.map_at_bot, ← map_coe_Ioi_at_bot]
lemma comap_exp_nhds_within_Ioi_zero : comap exp (𝓝[Ioi 0] 0) = at_bot :=
by rw [← map_exp_at_bot, comap_map exp_injective]
lemma tendsto_comp_exp_at_bot {α : Type*} {l : filter α} {f : ℝ → α} :
tendsto (λ x, f (exp x)) at_bot l ↔ tendsto f (𝓝[Ioi 0] 0) l :=
by rw [← map_exp_at_bot, tendsto_map'_iff]
/-- The real logarithm function, equal to the inverse of the exponential for `x > 0`,
to `log |x|` for `x < 0`, and to `0` for `0`. We use this unconventional extension to
`(-∞, 0]` as it gives the formula `log (x * y) = log x + log y` for all nonzero `x` and `y`, and
the derivative of `log` is `1/x` away from `0`. -/
@[pp_nodot] noncomputable def log (x : ℝ) : ℝ :=
if hx : x = 0 then 0 else exp_order_iso.symm ⟨abs x, abs_pos.2 hx⟩
lemma log_of_ne_zero (hx : x ≠ 0) : log x = exp_order_iso.symm ⟨abs x, abs_pos.2 hx⟩ := dif_neg hx
lemma log_of_pos (hx : 0 < x) : log x = exp_order_iso.symm ⟨x, hx⟩ :=
by { rw [log_of_ne_zero hx.ne'], congr, exact abs_of_pos hx }
lemma exp_log_eq_abs (hx : x ≠ 0) : exp (log x) = abs x :=
by rw [log_of_ne_zero hx, ← coe_exp_order_iso_apply, order_iso.apply_symm_apply, subtype.coe_mk]
lemma exp_log (hx : 0 < x) : exp (log x) = x :=
by { rw exp_log_eq_abs hx.ne', exact abs_of_pos hx }
lemma exp_log_of_neg (hx : x < 0) : exp (log x) = -x :=
by { rw exp_log_eq_abs (ne_of_lt hx), exact abs_of_neg hx }
@[simp] lemma log_exp (x : ℝ) : log (exp x) = x :=
exp_injective $ exp_log (exp_pos x)
lemma surj_on_log : surj_on log (Ioi 0) univ :=
λ x _, ⟨exp x, exp_pos x, log_exp x⟩
lemma log_surjective : surjective log :=
λ x, ⟨exp x, log_exp x⟩
@[simp] lemma range_log : range log = univ :=
log_surjective.range_eq
@[simp] lemma log_zero : log 0 = 0 := dif_pos rfl
@[simp] lemma log_one : log 1 = 0 :=
exp_injective $ by rw [exp_log zero_lt_one, exp_zero]
@[simp] lemma log_abs (x : ℝ) : log (abs x) = log x :=
begin
by_cases h : x = 0,
{ simp [h] },
{ rw [← exp_eq_exp, exp_log_eq_abs h, exp_log_eq_abs (abs_pos.2 h).ne', abs_abs] }
end
@[simp] lemma log_neg_eq_log (x : ℝ) : log (-x) = log x :=
by rw [← log_abs x, ← log_abs (-x), abs_neg]
lemma surj_on_log' : surj_on log (Iio 0) univ :=
λ x _, ⟨-exp x, neg_lt_zero.2 $ exp_pos x, by rw [log_neg_eq_log, log_exp]⟩
lemma log_mul (hx : x ≠ 0) (hy : y ≠ 0) : log (x * y) = log x + log y :=
exp_injective $
by rw [exp_log_eq_abs (mul_ne_zero hx hy), exp_add, exp_log_eq_abs hx, exp_log_eq_abs hy, abs_mul]
lemma log_div (hx : x ≠ 0) (hy : y ≠ 0) : log (x / y) = log x - log y :=
exp_injective $
by rw [exp_log_eq_abs (div_ne_zero hx hy), exp_sub, exp_log_eq_abs hx, exp_log_eq_abs hy, abs_div]
@[simp] lemma log_inv (x : ℝ) : log (x⁻¹) = -log x :=
begin
by_cases hx : x = 0, { simp [hx] },
rw [← exp_eq_exp, exp_log_eq_abs (inv_ne_zero hx), exp_neg, exp_log_eq_abs hx, abs_inv]
end
lemma log_le_log (h : 0 < x) (h₁ : 0 < y) : real.log x ≤ real.log y ↔ x ≤ y :=
by rw [← exp_le_exp, exp_log h, exp_log h₁]
lemma log_lt_log (hx : 0 < x) : x < y → log x < log y :=
by { intro h, rwa [← exp_lt_exp, exp_log hx, exp_log (lt_trans hx h)] }
lemma log_lt_log_iff (hx : 0 < x) (hy : 0 < y) : log x < log y ↔ x < y :=
by { rw [← exp_lt_exp, exp_log hx, exp_log hy] }
lemma log_pos_iff (hx : 0 < x) : 0 < log x ↔ 1 < x :=
by { rw ← log_one, exact log_lt_log_iff zero_lt_one hx }
lemma log_pos (hx : 1 < x) : 0 < log x :=
(log_pos_iff (lt_trans zero_lt_one hx)).2 hx
lemma log_neg_iff (h : 0 < x) : log x < 0 ↔ x < 1 :=
by { rw ← log_one, exact log_lt_log_iff h zero_lt_one }
lemma log_neg (h0 : 0 < x) (h1 : x < 1) : log x < 0 := (log_neg_iff h0).2 h1
lemma log_nonneg_iff (hx : 0 < x) : 0 ≤ log x ↔ 1 ≤ x :=
by rw [← not_lt, log_neg_iff hx, not_lt]
lemma log_nonneg (hx : 1 ≤ x) : 0 ≤ log x :=
(log_nonneg_iff (zero_lt_one.trans_le hx)).2 hx
lemma log_nonpos_iff (hx : 0 < x) : log x ≤ 0 ↔ x ≤ 1 :=
by rw [← not_lt, log_pos_iff hx, not_lt]
lemma log_nonpos_iff' (hx : 0 ≤ x) : log x ≤ 0 ↔ x ≤ 1 :=
begin
rcases hx.eq_or_lt with (rfl|hx),
{ simp [le_refl, zero_le_one] },
exact log_nonpos_iff hx
end
lemma log_nonpos (hx : 0 ≤ x) (h'x : x ≤ 1) : log x ≤ 0 :=
(log_nonpos_iff' hx).2 h'x
lemma strict_mono_incr_on_log : strict_mono_incr_on log (set.Ioi 0) :=
λ x hx y hy hxy, log_lt_log hx hxy
lemma strict_mono_decr_on_log : strict_mono_decr_on log (set.Iio 0) :=
begin
rintros x (hx : x < 0) y (hy : y < 0) hxy,
rw [← log_abs y, ← log_abs x],
refine log_lt_log (abs_pos.2 hy.ne) _,
rwa [abs_of_neg hy, abs_of_neg hx, neg_lt_neg_iff]
end
lemma log_inj_on_pos : set.inj_on log (set.Ioi 0) :=
strict_mono_incr_on_log.inj_on
lemma eq_one_of_pos_of_log_eq_zero {x : ℝ} (h₁ : 0 < x) (h₂ : log x = 0) : x = 1 :=
log_inj_on_pos (set.mem_Ioi.2 h₁) (set.mem_Ioi.2 zero_lt_one) (h₂.trans real.log_one.symm)
lemma log_ne_zero_of_pos_of_ne_one {x : ℝ} (hx_pos : 0 < x) (hx : x ≠ 1) : log x ≠ 0 :=
mt (eq_one_of_pos_of_log_eq_zero hx_pos) hx
/-- The real logarithm function tends to `+∞` at `+∞`. -/
lemma tendsto_log_at_top : tendsto log at_top at_top :=
tendsto_comp_exp_at_top.1 $ by simpa only [log_exp] using tendsto_id
lemma tendsto_log_nhds_within_zero : tendsto log (𝓝[{0}ᶜ] 0) at_bot :=
begin
rw [← (show _ = log, from funext log_abs)],
refine tendsto.comp _ tendsto_abs_nhds_within_zero,
simpa [← tendsto_comp_exp_at_bot] using tendsto_id
end
lemma continuous_on_log : continuous_on log {0}ᶜ :=
begin
rw [continuous_on_iff_continuous_restrict, restrict],
conv in (log _) { rw [log_of_ne_zero (show (x : ℝ) ≠ 0, from x.2)] },
exact exp_order_iso.symm.continuous.comp (continuous_subtype_mk _ continuous_subtype_coe.norm)
end
lemma continuous_log' : continuous (λ x : {x : ℝ // 0 < x}, log x) :=
continuous_on_iff_continuous_restrict.1 $ continuous_on_log.mono $ λ x hx, ne_of_gt hx
lemma continuous_at_log (hx : x ≠ 0) : continuous_at log x :=
(continuous_on_log x hx).continuous_at $ mem_nhds_sets is_open_compl_singleton hx
@[simp] lemma continuous_at_log_iff : continuous_at log x ↔ x ≠ 0 :=
begin
refine ⟨_, continuous_at_log⟩,
rintros h rfl,
exact not_tendsto_nhds_of_tendsto_at_bot tendsto_log_nhds_within_zero _
(h.tendsto.mono_left inf_le_left)
end
lemma has_strict_deriv_at_log_of_pos (hx : 0 < x) : has_strict_deriv_at log x⁻¹ x :=
have has_strict_deriv_at log (exp $ log x)⁻¹ x,
from (has_strict_deriv_at_exp $ log x).of_local_left_inverse (continuous_at_log hx.ne')
(ne_of_gt $ exp_pos _) $ eventually.mono (lt_mem_nhds hx) @exp_log,
by rwa [exp_log hx] at this
lemma has_strict_deriv_at_log (hx : x ≠ 0) : has_strict_deriv_at log x⁻¹ x :=
begin
cases hx.lt_or_lt with hx hx,
{ convert (has_strict_deriv_at_log_of_pos (neg_pos.mpr hx)).comp x (has_strict_deriv_at_neg x),
{ ext y, exact (log_neg_eq_log y).symm },
{ field_simp [hx.ne] } },
{ exact has_strict_deriv_at_log_of_pos hx }
end
lemma has_deriv_at_log (hx : x ≠ 0) : has_deriv_at log x⁻¹ x :=
(has_strict_deriv_at_log hx).has_deriv_at
lemma differentiable_at_log (hx : x ≠ 0) : differentiable_at ℝ log x :=
(has_deriv_at_log hx).differentiable_at
lemma differentiable_on_log : differentiable_on ℝ log {0}ᶜ :=
λ x hx, (differentiable_at_log hx).differentiable_within_at
@[simp] lemma differentiable_at_log_iff : differentiable_at ℝ log x ↔ x ≠ 0 :=
⟨λ h, continuous_at_log_iff.1 h.continuous_at, differentiable_at_log⟩
lemma deriv_log (x : ℝ) : deriv log x = x⁻¹ :=
if hx : x = 0 then
by rw [deriv_zero_of_not_differentiable_at (mt differentiable_at_log_iff.1 (not_not.2 hx)), hx,
inv_zero]
else (has_deriv_at_log hx).deriv
@[simp] lemma deriv_log' : deriv log = has_inv.inv := funext deriv_log
lemma measurable_log : measurable log :=
measurable_of_measurable_on_compl_singleton 0 $ continuous.measurable $
continuous_on_iff_continuous_restrict.1 continuous_on_log
lemma times_cont_diff_on_log {n : with_top ℕ} : times_cont_diff_on ℝ n log {0}ᶜ :=
begin
suffices : times_cont_diff_on ℝ ⊤ log {0}ᶜ, from this.of_le le_top,
refine (times_cont_diff_on_top_iff_deriv_of_open is_open_compl_singleton).2 _,
simp [differentiable_on_log, times_cont_diff_on_inv]
end
lemma times_cont_diff_at_log {n : with_top ℕ} : times_cont_diff_at ℝ n log x ↔ x ≠ 0 :=
⟨λ h, continuous_at_log_iff.1 h.continuous_at,
λ hx, (times_cont_diff_on_log x hx).times_cont_diff_at $
mem_nhds_sets is_open_compl_singleton hx⟩
end real
section log_differentiable
open real
section continuity
variables {α : Type*}
lemma filter.tendsto.log {f : α → ℝ} {l : filter α} {x : ℝ} (h : tendsto f l (𝓝 x)) (hx : x ≠ 0) :
tendsto (λ x, log (f x)) l (𝓝 (log x)) :=
(continuous_at_log hx).tendsto.comp h
variables [topological_space α] {f : α → ℝ} {s : set α} {a : α}
lemma continuous.log (hf : continuous f) (h₀ : ∀ x, f x ≠ 0) : continuous (λ x, log (f x)) :=
continuous_on_log.comp_continuous hf h₀
lemma continuous_at.log (hf : continuous_at f a) (h₀ : f a ≠ 0) :
continuous_at (λ x, log (f x)) a :=
hf.log h₀
lemma continuous_within_at.log (hf : continuous_within_at f s a) (h₀ : f a ≠ 0) :
continuous_within_at (λ x, log (f x)) s a :=
hf.log h₀
lemma continuous_on.log (hf : continuous_on f s) (h₀ : ∀ x ∈ s, f x ≠ 0) :
continuous_on (λ x, log (f x)) s :=
λ x hx, (hf x hx).log (h₀ x hx)
end continuity
section deriv
variables {f : ℝ → ℝ} {x f' : ℝ} {s : set ℝ}
lemma measurable.log {α : Type*} [measurable_space α] {f : α → ℝ} (hf : measurable f) :
measurable (λ x, log (f x)) :=
measurable_log.comp hf
lemma has_deriv_within_at.log (hf : has_deriv_within_at f f' s x) (hx : f x ≠ 0) :
has_deriv_within_at (λ y, log (f y)) (f' / (f x)) s x :=
begin
rw div_eq_inv_mul,
exact (has_deriv_at_log hx).comp_has_deriv_within_at x hf
end
lemma has_deriv_at.log (hf : has_deriv_at f f' x) (hx : f x ≠ 0) :
has_deriv_at (λ y, log (f y)) (f' / f x) x :=
begin
rw ← has_deriv_within_at_univ at *,
exact hf.log hx
end
lemma has_strict_deriv_at.log (hf : has_strict_deriv_at f f' x) (hx : f x ≠ 0) :
has_strict_deriv_at (λ y, log (f y)) (f' / f x) x :=
begin
rw div_eq_inv_mul,
exact (has_strict_deriv_at_log hx).comp x hf
end
lemma deriv_within.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0)
(hxs : unique_diff_within_at ℝ s x) :
deriv_within (λx, log (f x)) s x = (deriv_within f s x) / (f x) :=
(hf.has_deriv_within_at.log hx).deriv_within hxs
@[simp] lemma deriv.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) :
deriv (λx, log (f x)) x = (deriv f x) / (f x) :=
(hf.has_deriv_at.log hx).deriv
end deriv
section fderiv
variables {E : Type*} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {x : E} {f' : E →L[ℝ] ℝ}
{s : set E}
lemma has_fderiv_within_at.log (hf : has_fderiv_within_at f f' s x) (hx : f x ≠ 0) :
has_fderiv_within_at (λ x, log (f x)) ((f x)⁻¹ • f') s x :=
(has_deriv_at_log hx).comp_has_fderiv_within_at x hf
lemma has_fderiv_at.log (hf : has_fderiv_at f f' x) (hx : f x ≠ 0) :
has_fderiv_at (λ x, log (f x)) ((f x)⁻¹ • f') x :=
(has_deriv_at_log hx).comp_has_fderiv_at x hf
lemma has_strict_fderiv_at.log (hf : has_strict_fderiv_at f f' x) (hx : f x ≠ 0) :
has_strict_fderiv_at (λ x, log (f x)) ((f x)⁻¹ • f') x :=
(has_strict_deriv_at_log hx).comp_has_strict_fderiv_at x hf
lemma differentiable_within_at.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) :
differentiable_within_at ℝ (λx, log (f x)) s x :=
(hf.has_fderiv_within_at.log hx).differentiable_within_at
@[simp] lemma differentiable_at.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) :
differentiable_at ℝ (λx, log (f x)) x :=
(hf.has_fderiv_at.log hx).differentiable_at
lemma times_cont_diff_at.log {n} (hf : times_cont_diff_at ℝ n f x) (hx : f x ≠ 0) :
times_cont_diff_at ℝ n (λ x, log (f x)) x :=
(times_cont_diff_at_log.2 hx).comp x hf
lemma times_cont_diff_within_at.log {n} (hf : times_cont_diff_within_at ℝ n f s x) (hx : f x ≠ 0) :
times_cont_diff_within_at ℝ n (λ x, log (f x)) s x :=
(times_cont_diff_at_log.2 hx).comp_times_cont_diff_within_at x hf
lemma times_cont_diff_on.log {n} (hf : times_cont_diff_on ℝ n f s) (hs : ∀ x ∈ s, f x ≠ 0) :
times_cont_diff_on ℝ n (λ x, log (f x)) s :=
λ x hx, (hf x hx).log (hs x hx)
lemma times_cont_diff.log {n} (hf : times_cont_diff ℝ n f) (h : ∀ x, f x ≠ 0) :
times_cont_diff ℝ n (λ x, log (f x)) :=
times_cont_diff_iff_times_cont_diff_at.2 $ λ x, hf.times_cont_diff_at.log (h x)
lemma differentiable_on.log (hf : differentiable_on ℝ f s) (hx : ∀ x ∈ s, f x ≠ 0) :
differentiable_on ℝ (λx, log (f x)) s :=
λx h, (hf x h).log (hx x h)
@[simp] lemma differentiable.log (hf : differentiable ℝ f) (hx : ∀ x, f x ≠ 0) :
differentiable ℝ (λx, log (f x)) :=
λx, (hf x).log (hx x)
lemma fderiv_within.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0)
(hxs : unique_diff_within_at ℝ s x) :
fderiv_within ℝ (λx, log (f x)) s x = (f x)⁻¹ • fderiv_within ℝ f s x :=
(hf.has_fderiv_within_at.log hx).fderiv_within hxs
@[simp] lemma fderiv.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) :
fderiv ℝ (λx, log (f x)) x = (f x)⁻¹ • fderiv ℝ f x :=
(hf.has_fderiv_at.log hx).fderiv
end fderiv
end log_differentiable
namespace real
/-- The function `exp(x)/x^n` tends to `+∞` at `+∞`, for any natural number `n` -/
lemma tendsto_exp_div_pow_at_top (n : ℕ) : tendsto (λx, exp x / x^n) at_top at_top :=
begin
refine (at_top_basis_Ioi.tendsto_iff (at_top_basis' 1)).2 (λ C hC₁, _),
have hC₀ : 0 < C, from zero_lt_one.trans_le hC₁,
have : 0 < (exp 1 * C)⁻¹ := inv_pos.2 (mul_pos (exp_pos _) hC₀),
obtain ⟨N, hN⟩ : ∃ N, ∀ k ≥ N, (↑k ^ n : ℝ) / exp 1 ^ k < (exp 1 * C)⁻¹ :=
eventually_at_top.1 ((tendsto_pow_const_div_const_pow_of_one_lt n
(one_lt_exp_iff.2 zero_lt_one)).eventually (gt_mem_nhds this)),
simp only [← exp_nat_mul, mul_one, div_lt_iff, exp_pos, ← div_eq_inv_mul] at hN,
refine ⟨N, trivial, λ x hx, _⟩, rw mem_Ioi at hx,
have hx₀ : 0 < x, from N.cast_nonneg.trans_lt hx,
rw [mem_Ici, le_div_iff (pow_pos hx₀ _), ← le_div_iff' hC₀],
calc x ^ n ≤ (nat_ceil x) ^ n : pow_le_pow_of_le_left hx₀.le (le_nat_ceil _) _
... ≤ exp (nat_ceil x) / (exp 1 * C) : (hN _ (lt_nat_ceil.2 hx).le).le
... ≤ exp (x + 1) / (exp 1 * C) : div_le_div_of_le (mul_pos (exp_pos _) hC₀).le
(exp_le_exp.2 $ (nat_ceil_lt_add_one hx₀.le).le)
... = exp x / C : by rw [add_comm, exp_add, mul_div_mul_left _ _ (exp_pos _).ne']
end
/-- The function `x^n * exp(-x)` tends to `0` at `+∞`, for any natural number `n`. -/
lemma tendsto_pow_mul_exp_neg_at_top_nhds_0 (n : ℕ) : tendsto (λx, x^n * exp (-x)) at_top (𝓝 0) :=
(tendsto_inv_at_top_zero.comp (tendsto_exp_div_pow_at_top n)).congr $ λx,
by rw [comp_app, inv_eq_one_div, div_div_eq_mul_div, one_mul, div_eq_mul_inv, exp_neg]
/-- The function `(b * exp x + c) / (x ^ n)` tends to `+∞` at `+∞`, for any positive natural number
`n` and any real numbers `b` and `c` such that `b` is positive. -/
lemma tendsto_mul_exp_add_div_pow_at_top (b c : ℝ) (n : ℕ) (hb : 0 < b) (hn : 1 ≤ n) :
tendsto (λ x, (b * (exp x) + c) / (x^n)) at_top at_top :=
begin
refine tendsto.congr' (eventually_eq_of_mem (Ioi_mem_at_top 0) _)
(((tendsto_exp_div_pow_at_top n).const_mul_at_top hb).at_top_add
((tendsto_pow_neg_at_top hn).mul (@tendsto_const_nhds _ _ _ c _))),
intros x hx,
simp only [fpow_neg x n],
ring,
end
/-- The function `(x ^ n) / (b * exp x + c)` tends to `0` at `+∞`, for any positive natural number
`n` and any real numbers `b` and `c` such that `b` is nonzero. -/
lemma tendsto_div_pow_mul_exp_add_at_top (b c : ℝ) (n : ℕ) (hb : 0 ≠ b) (hn : 1 ≤ n) :
tendsto (λ x, x^n / (b * (exp x) + c)) at_top (𝓝 0) :=
begin
have H : ∀ d e, 0 < d → tendsto (λ (x:ℝ), x^n / (d * (exp x) + e)) at_top (𝓝 0),
{ intros b' c' h,
convert (tendsto_mul_exp_add_div_pow_at_top b' c' n h hn).inv_tendsto_at_top ,
ext x,
simpa only [pi.inv_apply] using inv_div.symm },
cases lt_or_gt_of_ne hb,
{ exact H b c h },
{ convert (H (-b) (-c) (neg_pos.mpr h)).neg,
{ ext x,
field_simp,
rw [← neg_add (b * exp x) c, neg_div_neg_eq] },
{ exact neg_zero.symm } },
end
open_locale big_operators
/-- A crude lemma estimating the difference between `log (1-x)` and its Taylor series at `0`,
where the main point of the bound is that it tends to `0`. The goal is to deduce the series
expansion of the logarithm, in `has_sum_pow_div_log_of_abs_lt_1`.
-/
lemma abs_log_sub_add_sum_range_le {x : ℝ} (h : abs x < 1) (n : ℕ) :
abs ((∑ i in range n, x^(i+1)/(i+1)) + log (1-x)) ≤ (abs x)^(n+1) / (1 - abs x) :=
begin
/- For the proof, we show that the derivative of the function to be estimated is small,
and then apply the mean value inequality. -/
let F : ℝ → ℝ := λ x, ∑ i in range n, x^(i+1)/(i+1) + log (1-x),
-- First step: compute the derivative of `F`
have A : ∀ y ∈ Ioo (-1 : ℝ) 1, deriv F y = - (y^n) / (1 - y),
{ assume y hy,
have : (∑ i in range n, (↑i + 1) * y ^ i / (↑i + 1)) = (∑ i in range n, y ^ i),
{ congr' with i,
have : (i : ℝ) + 1 ≠ 0 := ne_of_gt (nat.cast_add_one_pos i),
field_simp [this, mul_comm] },
field_simp [F, this, ← geom_sum_def, geom_sum_eq (ne_of_lt hy.2),
sub_ne_zero_of_ne (ne_of_gt hy.2), sub_ne_zero_of_ne (ne_of_lt hy.2)],
ring },
-- second step: show that the derivative of `F` is small
have B : ∀ y ∈ Icc (-abs x) (abs x), abs (deriv F y) ≤ (abs x)^n / (1 - abs x),
{ assume y hy,
have : y ∈ Ioo (-(1 : ℝ)) 1 := ⟨lt_of_lt_of_le (neg_lt_neg h) hy.1, lt_of_le_of_lt hy.2 h⟩,
calc abs (deriv F y) = abs (-(y^n) / (1 - y)) : by rw [A y this]
... ≤ (abs x)^n / (1 - abs x) :
begin
have : abs y ≤ abs x := abs_le.2 hy,
have : 0 < 1 - abs x, by linarith,
have : 1 - abs x ≤ abs (1 - y) := le_trans (by linarith [hy.2]) (le_abs_self _),
simp only [← pow_abs, abs_div, abs_neg],
apply_rules [div_le_div, pow_nonneg, abs_nonneg, pow_le_pow_of_le_left]
end },
-- third step: apply the mean value inequality
have C : ∥F x - F 0∥ ≤ ((abs x)^n / (1 - abs x)) * ∥x - 0∥,
{ have : ∀ y ∈ Icc (- abs x) (abs x), differentiable_at ℝ F y,
{ assume y hy,
have : 1 - y ≠ 0 := sub_ne_zero_of_ne (ne_of_gt (lt_of_le_of_lt hy.2 h)),
simp [F, this] },
apply convex.norm_image_sub_le_of_norm_deriv_le this B (convex_Icc _ _) _ _,
{ simpa using abs_nonneg x },
{ simp [le_abs_self x, neg_le.mp (neg_le_abs_self x)] } },
-- fourth step: conclude by massaging the inequality of the third step
simpa [F, norm_eq_abs, div_mul_eq_mul_div, pow_succ'] using C
end
/-- Power series expansion of the logarithm around `1`. -/
theorem has_sum_pow_div_log_of_abs_lt_1 {x : ℝ} (h : abs x < 1) :
has_sum (λ (n : ℕ), x ^ (n + 1) / (n + 1)) (-log (1 - x)) :=
begin
rw summable.has_sum_iff_tendsto_nat,
show tendsto (λ (n : ℕ), ∑ (i : ℕ) in range n, x ^ (i + 1) / (i + 1)) at_top (𝓝 (-log (1 - x))),
{ rw [tendsto_iff_norm_tendsto_zero],
simp only [norm_eq_abs, sub_neg_eq_add],
refine squeeze_zero (λ n, abs_nonneg _) (abs_log_sub_add_sum_range_le h) _,
suffices : tendsto (λ (t : ℕ), abs x ^ (t + 1) / (1 - abs x)) at_top
(𝓝 (abs x * 0 / (1 - abs x))), by simpa,
simp only [pow_succ],
refine (tendsto_const_nhds.mul _).div_const,
exact tendsto_pow_at_top_nhds_0_of_lt_1 (abs_nonneg _) h },
show summable (λ (n : ℕ), x ^ (n + 1) / (n + 1)),
{ refine summable_of_norm_bounded _ (summable_geometric_of_lt_1 (abs_nonneg _) h) (λ i, _),
calc ∥x ^ (i + 1) / (i + 1)∥
= abs x ^ (i+1) / (i+1) :
begin
have : (0 : ℝ) ≤ i + 1 := le_of_lt (nat.cast_add_one_pos i),
rw [norm_eq_abs, abs_div, ← pow_abs, abs_of_nonneg this],
end
... ≤ abs x ^ (i+1) / (0 + 1) :
begin
apply_rules [div_le_div_of_le_left, pow_nonneg, abs_nonneg, add_le_add_right,
i.cast_nonneg],
norm_num,
end
... ≤ abs x ^ i :
by simpa [pow_succ'] using mul_le_of_le_one_right (pow_nonneg (abs_nonneg x) i) (le_of_lt h) }
end
end real
|
a8378eabd6b97ad32f2cc2c65ab7eadd5d89aec1 | 37a833c924892ee3ecb911484775a6d6ebb8984d | /src/category_theory/examples/groups/forgetful.lean | b8a8f36f2dd81813173446e61524f3c3b02ad74c | [] | no_license | silky/lean-category-theory | 28126e80564a1f99e9c322d86b3f7d750da0afa1 | 0f029a2364975f56ac727d31d867a18c95c22fd8 | refs/heads/master | 1,589,555,811,646 | 1,554,673,665,000 | 1,554,673,665,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,053 | lean | -- Copyright (c) 2018 Scott Morrison. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Scott Morrison
import category_theory.examples.groups
import category_theory.types
import category_theory.limits
import algebra.group_power
namespace category_theory.examples.groups
open category_theory
open category_theory.yoneda
universes u₁ u₂
def ForgetfulFunctor_Groups_to_Types : Group ⥤ (Type u₁) :=
{ obj := λ s, s.1,
map := λ s t f x, f.map x }
local attribute [search] semigroup.mul_assoc
instance ulift_group {α : Type u₁} [group α] : group (ulift.{u₂} α) :=
begin
refine { one := ulift.up 1,
mul := λ x y, ulift.up (x.down * y.down),
inv := λ x, ulift.up ((x.down)⁻¹),
.. } ; obviously
end
instance integers_as_group : group ℤ :=
begin
refine {
one := 0,
mul := λ x y, x + y,
inv := λ x, -x,
..
} ; obviously
end
-- TODO fix inconsistent naming of monoid.pow and gpow in mathlib
local attribute [class] is_group_hom
instance exponentiation_is_hom (G : Group) (g : G.1) : is_group_hom (λ (n : ℤ), gpow g n) := begin tidy, sorry end
instance exponentiation_is_hom' (G : Group) (g : G.1) : is_group_hom (λ (n : _root_.ulift ℤ), gpow g (n.down)) := begin tidy, sorry end
@[simp] lemma hom_exponentiation (G : Group) (f : _root_.ulift ℤ → G.1) [is_group_hom f] (n : ℤ) : gpow (f (ulift.up int.one)) n = f (ulift.up n) := sorry
@[simp] lemma monoid_pow_one {G} [group G] (x : G) : monoid.pow x 1 = x := begin sorry, end
-- instance Groups_ForgetfulFunctor_Representable : representable (ForgetfulFunctor_Groups_to_Types.{u₁}) :=
-- { c := ⟨ _root_.ulift ℤ, by apply_instance ⟩,
-- Φ := { hom := { app := λ G g, ⟨λ n, gpow g n.down, begin tidy, sorry end⟩,
-- naturality' := sorry },
-- inv := { app := λ G f, f.map (ulift.up (1 : ℤ)), },
-- hom_inv_id' := sorry,
-- inv_hom_id' := sorry, } }
end category_theory.examples.groups |
65a48337b85b7a9fcff577f326e4a09adffbc96b | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/set.lean | 82c24a9b0c1c33c71b88dea5ff4103c2e7c85203 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | leanprover/lean4 | 4bdf9790294964627eb9be79f5e8f6157780b4cc | f1f9dc0f2f531af3312398999d8b8303fa5f096b | refs/heads/master | 1,693,360,665,786 | 1,693,350,868,000 | 1,693,350,868,000 | 129,571,436 | 2,827 | 311 | Apache-2.0 | 1,694,716,156,000 | 1,523,760,560,000 | Lean | UTF-8 | Lean | false | false | 1,088 | lean | def Set (α : Type u) := α → Prop
def Set.in (s : Set α) (a : α) := s a
notation:50 a " ∈ " s:50 => Set.in s a
def Set.pred (p : α → Prop) : Set α := p
notation "{" a "|" p "}" => Set.pred (fun a => p)
theorem ex1 : (1, 3) ∈ { (n, m) | n < 2 ∧ m < 5 } := by
simp [Set.in, Set.pred]
def Set.union (s₁ s₂ : Set α) : Set α :=
{ a | a ∈ s₁ ∨ a ∈ s₂ }
infix:65 " ∪ " => Set.union
def Set.inter (s₁ s₂ : Set α) : Set α :=
{ a | a ∈ s₁ ∧ a ∈ s₂ }
infix:70 " ∩ " => Set.inter
instance (s : Set α) [h : Decidable (s a)] : Decidable (a ∈ Set.pred s) :=
h
instance (s₁ s₂ : Set α) [Decidable (a ∈ s₁)] [Decidable (a ∈ s₂)] : Decidable (a ∈ s₁ ∩ s₂) :=
inferInstanceAs (Decidable (_ ∧ _))
instance (s₁ s₂ : Set α) [Decidable (a ∈ s₁)] [Decidable (a ∈ s₂)] : Decidable (a ∈ s₁ ∪ s₂) :=
inferInstanceAs (Decidable (_ ∨ _))
theorem ex2 : (1, 3) ∈ { (x, y) | x < y } :=
by decide
theorem ex3 : (10000, 300000) ∈ { (x, y) | x < y } ∩ { (x, y) | x = 10000 } :=
by decide
|
5782c419e94a9997bd19529e895d143e4c3c52a4 | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /library/data/list/set.lean | e8e6ffa61d078c67721eb33bd82916fddc1a7298 | [
"Apache-2.0"
] | permissive | davidmueller13/lean | 65a3ed141b4088cd0a268e4de80eb6778b21a0e9 | c626e2e3c6f3771e07c32e82ee5b9e030de5b050 | refs/heads/master | 1,611,278,313,401 | 1,444,021,177,000 | 1,444,021,177,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 37,085 | lean | /-
Copyright (c) 2015 Leonardo de Moura. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
Set-like operations on lists
-/
import data.list.basic data.list.comb
open nat function decidable helper_tactics eq.ops
namespace list
section erase
variable {A : Type}
variable [H : decidable_eq A]
include H
definition erase (a : A) : list A → list A
| [] := []
| (b::l) :=
match H a b with
| inl e := l
| inr n := b :: erase l
end
lemma erase_nil (a : A) : erase a [] = [] :=
rfl
lemma erase_cons_head (a : A) (l : list A) : erase a (a :: l) = l :=
show match H a a with | inl e := l | inr n := a :: erase a l end = l,
by rewrite decidable_eq_inl_refl
lemma erase_cons_tail {a b : A} (l : list A) : a ≠ b → erase a (b::l) = b :: erase a l :=
assume h : a ≠ b,
show match H a b with | inl e := l | inr n₁ := b :: erase a l end = b :: erase a l,
by rewrite (decidable_eq_inr_neg h)
lemma length_erase_of_mem {a : A} : ∀ {l}, a ∈ l → length (erase a l) = pred (length l)
| [] h := rfl
| [x] h := by rewrite [mem_singleton h, erase_cons_head]
| (x::y::xs) h :=
by_cases
(suppose a = x, by rewrite [this, erase_cons_head])
(suppose a ≠ x,
assert ainyxs : a ∈ y::xs, from or_resolve_right h this,
by rewrite [erase_cons_tail _ this, *length_cons, length_erase_of_mem ainyxs])
lemma length_erase_of_not_mem {a : A} : ∀ {l}, a ∉ l → length (erase a l) = length l
| [] h := rfl
| (x::xs) h :=
assert anex : a ≠ x, from λ aeqx : a = x, absurd (or.inl aeqx) h,
assert aninxs : a ∉ xs, from λ ainxs : a ∈ xs, absurd (or.inr ainxs) h,
by rewrite [erase_cons_tail _ anex, length_cons, length_erase_of_not_mem aninxs]
lemma erase_append_left {a : A} : ∀ {l₁} (l₂), a ∈ l₁ → erase a (l₁++l₂) = erase a l₁ ++ l₂
| [] l₂ h := absurd h !not_mem_nil
| (x::xs) l₂ h :=
by_cases
(λ aeqx : a = x, by rewrite [aeqx, append_cons, *erase_cons_head])
(λ anex : a ≠ x,
assert ainxs : a ∈ xs, from mem_of_ne_of_mem anex h,
by rewrite [append_cons, *erase_cons_tail _ anex, erase_append_left l₂ ainxs])
lemma erase_append_right {a : A} : ∀ {l₁} (l₂), a ∉ l₁ → erase a (l₁++l₂) = l₁ ++ erase a l₂
| [] l₂ h := rfl
| (x::xs) l₂ h :=
by_cases
(λ aeqx : a = x, by rewrite aeqx at h; exact (absurd !mem_cons h))
(λ anex : a ≠ x,
assert nainxs : a ∉ xs, from not_mem_of_not_mem_cons h,
by rewrite [append_cons, *erase_cons_tail _ anex, erase_append_right l₂ nainxs])
lemma erase_sub (a : A) : ∀ l, erase a l ⊆ l
| [] := λ x xine, xine
| (x::xs) := λ y xine,
by_cases
(λ aeqx : a = x, by rewrite [aeqx at xine, erase_cons_head at xine]; exact (or.inr xine))
(λ anex : a ≠ x,
assert yinxe : y ∈ x :: erase a xs, by rewrite [erase_cons_tail _ anex at xine]; exact xine,
assert subxs : erase a xs ⊆ xs, from erase_sub xs,
by_cases
(λ yeqx : y = x, by rewrite yeqx; apply mem_cons)
(λ ynex : y ≠ x,
assert yine : y ∈ erase a xs, from mem_of_ne_of_mem ynex yinxe,
assert yinxs : y ∈ xs, from subxs yine,
or.inr yinxs))
theorem mem_erase_of_ne_of_mem {a b : A} : ∀ {l : list A}, a ≠ b → a ∈ l → a ∈ erase b l
| [] n i := absurd i !not_mem_nil
| (c::l) n i := by_cases
(λ beqc : b = c,
assert ainl : a ∈ l, from or.elim (eq_or_mem_of_mem_cons i)
(λ aeqc : a = c, absurd aeqc (beqc ▸ n))
(λ ainl : a ∈ l, ainl),
by rewrite [beqc, erase_cons_head]; exact ainl)
(λ bnec : b ≠ c, by_cases
(λ aeqc : a = c,
assert aux : a ∈ c :: erase b l, by rewrite [aeqc]; exact !mem_cons,
by rewrite [erase_cons_tail _ bnec]; exact aux)
(λ anec : a ≠ c,
have ainl : a ∈ l, from mem_of_ne_of_mem anec i,
have ainel : a ∈ erase b l, from mem_erase_of_ne_of_mem n ainl,
assert aux : a ∈ c :: erase b l, from mem_cons_of_mem _ ainel,
by rewrite [erase_cons_tail _ bnec]; exact aux)) --
theorem mem_of_mem_erase {a b : A} : ∀ {l}, a ∈ erase b l → a ∈ l
| [] i := absurd i !not_mem_nil
| (c::l) i := by_cases
(λ beqc : b = c, by rewrite [beqc at i, erase_cons_head at i]; exact (mem_cons_of_mem _ i))
(λ bnec : b ≠ c,
have i₁ : a ∈ c :: erase b l, by rewrite [erase_cons_tail _ bnec at i]; exact i,
or.elim (eq_or_mem_of_mem_cons i₁)
(λ aeqc : a = c, by rewrite [aeqc]; exact !mem_cons)
(λ ainel : a ∈ erase b l,
have ainl : a ∈ l, from mem_of_mem_erase ainel,
mem_cons_of_mem _ ainl))
theorem all_erase_of_all {p : A → Prop} (a : A) : ∀ {l}, all l p → all (erase a l) p
| [] h := by rewrite [erase_nil]; exact h
| (b::l) h :=
assert h₁ : all l p, from all_of_all_cons h,
have h₂ : all (erase a l) p, from all_erase_of_all h₁,
have pb : p b, from of_all_cons h,
assert h₃ : all (b :: erase a l) p, from all_cons_of_all pb h₂,
by_cases
(λ aeqb : a = b, by rewrite [aeqb, erase_cons_head]; exact h₁)
(λ aneb : a ≠ b, by rewrite [erase_cons_tail _ aneb]; exact h₃)
end erase
/- disjoint -/
section disjoint
variable {A : Type}
definition disjoint (l₁ l₂ : list A) : Prop := ∀ ⦃a⦄, (a ∈ l₁ → a ∈ l₂ → false)
lemma disjoint_left {l₁ l₂ : list A} : disjoint l₁ l₂ → ∀ {a}, a ∈ l₁ → a ∉ l₂ :=
λ d a, d a
lemma disjoint_right {l₁ l₂ : list A} : disjoint l₁ l₂ → ∀ {a}, a ∈ l₂ → a ∉ l₁ :=
λ d a i₂ i₁, d a i₁ i₂
lemma disjoint.comm {l₁ l₂ : list A} : disjoint l₁ l₂ → disjoint l₂ l₁ :=
λ d a i₂ i₁, d a i₁ i₂
lemma disjoint_of_disjoint_cons_left {a : A} {l₁ l₂} : disjoint (a::l₁) l₂ → disjoint l₁ l₂ :=
λ d x xinl₁, disjoint_left d (or.inr xinl₁)
lemma disjoint_of_disjoint_cons_right {a : A} {l₁ l₂} : disjoint l₁ (a::l₂) → disjoint l₁ l₂ :=
λ d, disjoint.comm (disjoint_of_disjoint_cons_left (disjoint.comm d))
lemma disjoint_nil_left (l : list A) : disjoint [] l :=
λ a ab, absurd ab !not_mem_nil
lemma disjoint_nil_right (l : list A) : disjoint l [] :=
disjoint.comm (disjoint_nil_left l)
lemma disjoint_cons_of_not_mem_of_disjoint {a : A} {l₁ l₂} : a ∉ l₂ → disjoint l₁ l₂ → disjoint (a::l₁) l₂ :=
λ nainl₂ d x (xinal₁ : x ∈ a::l₁),
or.elim (eq_or_mem_of_mem_cons xinal₁)
(λ xeqa : x = a, xeqa⁻¹ ▸ nainl₂)
(λ xinl₁ : x ∈ l₁, disjoint_left d xinl₁)
lemma disjoint_of_disjoint_append_left_left : ∀ {l₁ l₂ l : list A}, disjoint (l₁++l₂) l → disjoint l₁ l
| [] l₂ l d := disjoint_nil_left l
| (x::xs) l₂ l d :=
have nxinl : x ∉ l, from disjoint_left d !mem_cons,
have d₁ : disjoint (xs++l₂) l, from disjoint_of_disjoint_cons_left d,
have d₂ : disjoint xs l, from disjoint_of_disjoint_append_left_left d₁,
disjoint_cons_of_not_mem_of_disjoint nxinl d₂
lemma disjoint_of_disjoint_append_left_right : ∀ {l₁ l₂ l : list A}, disjoint (l₁++l₂) l → disjoint l₂ l
| [] l₂ l d := d
| (x::xs) l₂ l d :=
have d₁ : disjoint (xs++l₂) l, from disjoint_of_disjoint_cons_left d,
disjoint_of_disjoint_append_left_right d₁
lemma disjoint_of_disjoint_append_right_left : ∀ {l₁ l₂ l : list A}, disjoint l (l₁++l₂) → disjoint l l₁ :=
λ l₁ l₂ l d, disjoint.comm (disjoint_of_disjoint_append_left_left (disjoint.comm d))
lemma disjoint_of_disjoint_append_right_right : ∀ {l₁ l₂ l : list A}, disjoint l (l₁++l₂) → disjoint l l₂ :=
λ l₁ l₂ l d, disjoint.comm (disjoint_of_disjoint_append_left_right (disjoint.comm d))
end disjoint
/- no duplicates predicate -/
inductive nodup {A : Type} : list A → Prop :=
| ndnil : nodup []
| ndcons : ∀ {a l}, a ∉ l → nodup l → nodup (a::l)
section nodup
open nodup
variables {A B : Type}
theorem nodup_nil : @nodup A [] :=
ndnil
theorem nodup_cons {a : A} {l : list A} : a ∉ l → nodup l → nodup (a::l) :=
λ i n, ndcons i n
theorem nodup_singleton (a : A) : nodup [a] :=
nodup_cons !not_mem_nil nodup_nil
theorem nodup_of_nodup_cons : ∀ {a : A} {l : list A}, nodup (a::l) → nodup l
| a xs (ndcons i n) := n
theorem not_mem_of_nodup_cons : ∀ {a : A} {l : list A}, nodup (a::l) → a ∉ l
| a xs (ndcons i n) := i
theorem not_nodup_cons_of_mem {a : A} {l : list A} : a ∈ l → ¬ nodup (a :: l) :=
λ ainl d, absurd ainl (not_mem_of_nodup_cons d)
theorem not_nodup_cons_of_not_nodup {a : A} {l : list A} : ¬ nodup l → ¬ nodup (a :: l) :=
λ nd d, absurd (nodup_of_nodup_cons d) nd
theorem nodup_of_nodup_append_left : ∀ {l₁ l₂ : list A}, nodup (l₁++l₂) → nodup l₁
| [] l₂ n := nodup_nil
| (x::xs) l₂ n :=
have ndxs : nodup xs, from nodup_of_nodup_append_left (nodup_of_nodup_cons n),
have nxinxsl₂ : x ∉ xs++l₂, from not_mem_of_nodup_cons n,
have nxinxs : x ∉ xs, from not_mem_of_not_mem_append_left nxinxsl₂,
nodup_cons nxinxs ndxs
theorem nodup_of_nodup_append_right : ∀ {l₁ l₂ : list A}, nodup (l₁++l₂) → nodup l₂
| [] l₂ n := n
| (x::xs) l₂ n := nodup_of_nodup_append_right (nodup_of_nodup_cons n)
theorem disjoint_of_nodup_append : ∀ {l₁ l₂ : list A}, nodup (l₁++l₂) → disjoint l₁ l₂
| [] l₂ d := disjoint_nil_left l₂
| (x::xs) l₂ d :=
have nodup (x::(xs++l₂)), from d,
have x ∉ xs++l₂, from not_mem_of_nodup_cons this,
have nxinl₂ : x ∉ l₂, from not_mem_of_not_mem_append_right this,
take a, suppose a ∈ x::xs,
or.elim (eq_or_mem_of_mem_cons this)
(suppose a = x, this⁻¹ ▸ nxinl₂)
(suppose ainxs : a ∈ xs,
have nodup (x::(xs++l₂)), from d,
have nodup (xs++l₂), from nodup_of_nodup_cons this,
have disjoint xs l₂, from disjoint_of_nodup_append this,
disjoint_left this ainxs)
theorem nodup_append_of_nodup_of_nodup_of_disjoint : ∀ {l₁ l₂ : list A}, nodup l₁ → nodup l₂ → disjoint l₁ l₂ → nodup (l₁++l₂)
| [] l₂ d₁ d₂ dsj := by rewrite [append_nil_left]; exact d₂
| (x::xs) l₂ d₁ d₂ dsj :=
have ndxs : nodup xs, from nodup_of_nodup_cons d₁,
have disjoint xs l₂, from disjoint_of_disjoint_cons_left dsj,
have ndxsl₂ : nodup (xs++l₂), from nodup_append_of_nodup_of_nodup_of_disjoint ndxs d₂ this,
have nxinxs : x ∉ xs, from not_mem_of_nodup_cons d₁,
have x ∉ l₂, from disjoint_left dsj !mem_cons,
have x ∉ xs++l₂, from not_mem_append nxinxs this,
nodup_cons this ndxsl₂
theorem nodup_app_comm {l₁ l₂ : list A} (d : nodup (l₁++l₂)) : nodup (l₂++l₁) :=
have d₁ : nodup l₁, from nodup_of_nodup_append_left d,
have d₂ : nodup l₂, from nodup_of_nodup_append_right d,
have dsj : disjoint l₁ l₂, from disjoint_of_nodup_append d,
nodup_append_of_nodup_of_nodup_of_disjoint d₂ d₁ (disjoint.comm dsj)
theorem nodup_head {a : A} {l₁ l₂ : list A} (d : nodup (l₁++(a::l₂))) : nodup (a::(l₁++l₂)) :=
have d₁ : nodup (a::(l₂++l₁)), from nodup_app_comm d,
have d₂ : nodup (l₂++l₁), from nodup_of_nodup_cons d₁,
have d₃ : nodup (l₁++l₂), from nodup_app_comm d₂,
have nain : a ∉ l₂++l₁, from not_mem_of_nodup_cons d₁,
have nain₂ : a ∉ l₂, from not_mem_of_not_mem_append_left nain,
have nain₁ : a ∉ l₁, from not_mem_of_not_mem_append_right nain,
nodup_cons (not_mem_append nain₁ nain₂) d₃
theorem nodup_middle {a : A} {l₁ l₂ : list A} (d : nodup (a::(l₁++l₂))) : nodup (l₁++(a::l₂)) :=
have d₁ : nodup (l₁++l₂), from nodup_of_nodup_cons d,
have nain : a ∉ l₁++l₂, from not_mem_of_nodup_cons d,
have disj : disjoint l₁ l₂, from disjoint_of_nodup_append d₁,
have d₂ : nodup l₁, from nodup_of_nodup_append_left d₁,
have d₃ : nodup l₂, from nodup_of_nodup_append_right d₁,
have nain₂ : a ∉ l₂, from not_mem_of_not_mem_append_right nain,
have nain₁ : a ∉ l₁, from not_mem_of_not_mem_append_left nain,
have d₄ : nodup (a::l₂), from nodup_cons nain₂ d₃,
have disj₂ : disjoint l₁ (a::l₂), from disjoint.comm (disjoint_cons_of_not_mem_of_disjoint nain₁ (disjoint.comm disj)),
nodup_append_of_nodup_of_nodup_of_disjoint d₂ d₄ disj₂
theorem nodup_map {f : A → B} (inj : injective f) : ∀ {l : list A}, nodup l → nodup (map f l)
| [] n := begin rewrite [map_nil], apply nodup_nil end
| (x::xs) n :=
assert nxinxs : x ∉ xs, from not_mem_of_nodup_cons n,
assert ndxs : nodup xs, from nodup_of_nodup_cons n,
assert ndmfxs : nodup (map f xs), from nodup_map ndxs,
assert nfxinm : f x ∉ map f xs, from
λ ab : f x ∈ map f xs,
obtain (y : A) (yinxs : y ∈ xs) (fyfx : f y = f x), from exists_of_mem_map ab,
assert yeqx : y = x, from inj fyfx,
by subst y; contradiction,
nodup_cons nfxinm ndmfxs
theorem nodup_erase_of_nodup [h : decidable_eq A] (a : A) : ∀ {l}, nodup l → nodup (erase a l)
| [] n := nodup_nil
| (b::l) n := by_cases
(λ aeqb : a = b, by rewrite [aeqb, erase_cons_head]; exact (nodup_of_nodup_cons n))
(λ aneb : a ≠ b,
have nbinl : b ∉ l, from not_mem_of_nodup_cons n,
have ndl : nodup l, from nodup_of_nodup_cons n,
have ndeal : nodup (erase a l), from nodup_erase_of_nodup ndl,
have nbineal : b ∉ erase a l, from λ i, absurd (erase_sub _ _ i) nbinl,
assert aux : nodup (b :: erase a l), from nodup_cons nbineal ndeal,
by rewrite [erase_cons_tail _ aneb]; exact aux)
theorem mem_erase_of_nodup [h : decidable_eq A] (a : A) : ∀ {l}, nodup l → a ∉ erase a l
| [] n := !not_mem_nil
| (b::l) n :=
have ndl : nodup l, from nodup_of_nodup_cons n,
have naineal : a ∉ erase a l, from mem_erase_of_nodup ndl,
assert nbinl : b ∉ l, from not_mem_of_nodup_cons n,
by_cases
(λ aeqb : a = b, by rewrite [aeqb, erase_cons_head]; exact nbinl)
(λ aneb : a ≠ b,
assert aux : a ∉ b :: erase a l, from
assume ainbeal : a ∈ b :: erase a l, or.elim (eq_or_mem_of_mem_cons ainbeal)
(λ aeqb : a = b, absurd aeqb aneb)
(λ aineal : a ∈ erase a l, absurd aineal naineal),
by rewrite [erase_cons_tail _ aneb]; exact aux)
definition erase_dup [H : decidable_eq A] : list A → list A
| [] := []
| (x :: xs) := if x ∈ xs then erase_dup xs else x :: erase_dup xs
theorem erase_dup_nil [H : decidable_eq A] : erase_dup [] = ([] : list A)
theorem erase_dup_cons_of_mem [H : decidable_eq A] {a : A} {l : list A} : a ∈ l → erase_dup (a::l) = erase_dup l :=
assume ainl, calc
erase_dup (a::l) = if a ∈ l then erase_dup l else a :: erase_dup l : rfl
... = erase_dup l : if_pos ainl
theorem erase_dup_cons_of_not_mem [H : decidable_eq A] {a : A} {l : list A} : a ∉ l → erase_dup (a::l) = a :: erase_dup l :=
assume nainl, calc
erase_dup (a::l) = if a ∈ l then erase_dup l else a :: erase_dup l : rfl
... = a :: erase_dup l : if_neg nainl
theorem mem_erase_dup [H : decidable_eq A] {a : A} : ∀ {l}, a ∈ l → a ∈ erase_dup l
| [] h := absurd h !not_mem_nil
| (b::l) h := by_cases
(λ binl : b ∈ l, or.elim (eq_or_mem_of_mem_cons h)
(λ aeqb : a = b, by rewrite [erase_dup_cons_of_mem binl, -aeqb at binl]; exact (mem_erase_dup binl))
(λ ainl : a ∈ l, by rewrite [erase_dup_cons_of_mem binl]; exact (mem_erase_dup ainl)))
(λ nbinl : b ∉ l, or.elim (eq_or_mem_of_mem_cons h)
(λ aeqb : a = b, by rewrite [erase_dup_cons_of_not_mem nbinl, aeqb]; exact !mem_cons)
(λ ainl : a ∈ l, by rewrite [erase_dup_cons_of_not_mem nbinl]; exact (or.inr (mem_erase_dup ainl))))
theorem mem_of_mem_erase_dup [H : decidable_eq A] {a : A} : ∀ {l}, a ∈ erase_dup l → a ∈ l
| [] h := by rewrite [erase_dup_nil at h]; exact h
| (b::l) h := by_cases
(λ binl : b ∈ l,
have h₁ : a ∈ erase_dup l, by rewrite [erase_dup_cons_of_mem binl at h]; exact h,
or.inr (mem_of_mem_erase_dup h₁))
(λ nbinl : b ∉ l,
have h₁ : a ∈ b :: erase_dup l, by rewrite [erase_dup_cons_of_not_mem nbinl at h]; exact h,
or.elim (eq_or_mem_of_mem_cons h₁)
(λ aeqb : a = b, by rewrite aeqb; exact !mem_cons)
(λ ainel : a ∈ erase_dup l, or.inr (mem_of_mem_erase_dup ainel)))
theorem erase_dup_sub [H : decidable_eq A] (l : list A) : erase_dup l ⊆ l :=
λ a i, mem_of_mem_erase_dup i
theorem sub_erase_dup [H : decidable_eq A] (l : list A) : l ⊆ erase_dup l :=
λ a i, mem_erase_dup i
theorem nodup_erase_dup [H : decidable_eq A] : ∀ l : list A, nodup (erase_dup l)
| [] := by rewrite erase_dup_nil; exact nodup_nil
| (a::l) := by_cases
(λ ainl : a ∈ l, by rewrite [erase_dup_cons_of_mem ainl]; exact (nodup_erase_dup l))
(λ nainl : a ∉ l,
assert r : nodup (erase_dup l), from nodup_erase_dup l,
assert nin : a ∉ erase_dup l, from
assume ab : a ∈ erase_dup l, absurd (mem_of_mem_erase_dup ab) nainl,
by rewrite [erase_dup_cons_of_not_mem nainl]; exact (nodup_cons nin r))
theorem erase_dup_eq_of_nodup [H : decidable_eq A] : ∀ {l : list A}, nodup l → erase_dup l = l
| [] d := rfl
| (a::l) d :=
assert nainl : a ∉ l, from not_mem_of_nodup_cons d,
assert dl : nodup l, from nodup_of_nodup_cons d,
by rewrite [erase_dup_cons_of_not_mem nainl, erase_dup_eq_of_nodup dl]
definition decidable_nodup [instance] [h : decidable_eq A] : ∀ (l : list A), decidable (nodup l)
| [] := inl nodup_nil
| (a::l) :=
match decidable_mem a l with
| inl p := inr (not_nodup_cons_of_mem p)
| inr n :=
match decidable_nodup l with
| inl nd := inl (nodup_cons n nd)
| inr d := inr (not_nodup_cons_of_not_nodup d)
end
end
theorem nodup_product : ∀ {l₁ : list A} {l₂ : list B}, nodup l₁ → nodup l₂ → nodup (product l₁ l₂)
| [] l₂ n₁ n₂ := nodup_nil
| (a::l₁) l₂ n₁ n₂ :=
have nainl₁ : a ∉ l₁, from not_mem_of_nodup_cons n₁,
have n₃ : nodup l₁, from nodup_of_nodup_cons n₁,
have n₄ : nodup (product l₁ l₂), from nodup_product n₃ n₂,
have dgen : ∀ l, nodup l → nodup (map (λ b, (a, b)) l)
| [] h := nodup_nil
| (x::l) h :=
have dl : nodup l, from nodup_of_nodup_cons h,
have dm : nodup (map (λ b, (a, b)) l), from dgen l dl,
have nxin : x ∉ l, from not_mem_of_nodup_cons h,
have npin : (a, x) ∉ map (λ b, (a, b)) l, from
assume pin, absurd (mem_of_mem_map_pair₁ pin) nxin,
nodup_cons npin dm,
have dm : nodup (map (λ b, (a, b)) l₂), from dgen l₂ n₂,
have dsj : disjoint (map (λ b, (a, b)) l₂) (product l₁ l₂), from
λ p, match p with
| (a₁, b₁) :=
λ (i₁ : (a₁, b₁) ∈ map (λ b, (a, b)) l₂) (i₂ : (a₁, b₁) ∈ product l₁ l₂),
have a₁inl₁ : a₁ ∈ l₁, from mem_of_mem_product_left i₂,
have a₁eqa : a₁ = a, from eq_of_mem_map_pair₁ i₁,
absurd (a₁eqa ▸ a₁inl₁) nainl₁
end,
nodup_append_of_nodup_of_nodup_of_disjoint dm n₄ dsj
theorem nodup_filter (p : A → Prop) [h : decidable_pred p] : ∀ {l : list A}, nodup l → nodup (filter p l)
| [] nd := nodup_nil
| (a::l) nd :=
have nainl : a ∉ l, from not_mem_of_nodup_cons nd,
have ndl : nodup l, from nodup_of_nodup_cons nd,
assert ndf : nodup (filter p l), from nodup_filter ndl,
assert nainf : a ∉ filter p l, from
assume ainf, absurd (mem_of_mem_filter ainf) nainl,
by_cases
(λ pa : p a, by rewrite [filter_cons_of_pos _ pa]; exact (nodup_cons nainf ndf))
(λ npa : ¬ p a, by rewrite [filter_cons_of_neg _ npa]; exact ndf)
lemma dmap_nodup_of_dinj {p : A → Prop} [h : decidable_pred p] {f : Π a, p a → B} (Pdi : dinj p f):
∀ {l : list A}, nodup l → nodup (dmap p f l)
| [] := take P, nodup.ndnil
| (a::l) := take Pnodup,
decidable.rec_on (h a)
(λ Pa,
begin
rewrite [dmap_cons_of_pos Pa],
apply nodup_cons,
apply (not_mem_dmap_of_dinj_of_not_mem Pdi Pa),
exact not_mem_of_nodup_cons Pnodup,
exact dmap_nodup_of_dinj (nodup_of_nodup_cons Pnodup)
end)
(λ nPa,
begin
rewrite [dmap_cons_of_neg nPa],
exact dmap_nodup_of_dinj (nodup_of_nodup_cons Pnodup)
end)
end nodup
/- upto -/
definition upto : nat → list nat
| 0 := []
| (n+1) := n :: upto n
theorem upto_nil : upto 0 = nil
theorem upto_succ (n : nat) : upto (succ n) = n :: upto n
theorem length_upto : ∀ n, length (upto n) = n
| 0 := rfl
| (succ n) := by rewrite [upto_succ, length_cons, length_upto]
theorem upto_less : ∀ n, all (upto n) (λ i, i < n)
| 0 := trivial
| (succ n) :=
have alln : all (upto n) (λ i, i < n), from upto_less n,
all_cons_of_all (lt.base n) (all_implies alln (λ x h, lt.step h))
theorem nodup_upto : ∀ n, nodup (upto n)
| 0 := nodup_nil
| (n+1) :=
have d : nodup (upto n), from nodup_upto n,
have n : n ∉ upto n, from
assume i : n ∈ upto n, absurd (of_mem_of_all i (upto_less n)) (lt.irrefl n),
nodup_cons n d
theorem lt_of_mem_upto {n i : nat} : i ∈ upto n → i < n :=
assume i, of_mem_of_all i (upto_less n)
theorem mem_upto_succ_of_mem_upto {n i : nat} : i ∈ upto n → i ∈ upto (succ n) :=
assume i, mem_cons_of_mem _ i
theorem mem_upto_of_lt : ∀ {n i : nat}, i < n → i ∈ upto n
| 0 i h := absurd h !not_lt_zero
| (succ n) i h :=
begin
cases h with m h',
{ rewrite upto_succ, apply mem_cons},
{ exact mem_upto_succ_of_mem_upto (mem_upto_of_lt h')}
end
lemma upto_step : ∀ {n : nat}, upto (succ n) = (map succ (upto n))++[0]
| 0 := rfl
| (succ n) := begin rewrite [upto_succ n, map_cons, append_cons, -upto_step] end
/- union -/
section union
variable {A : Type}
variable [H : decidable_eq A]
include H
definition union : list A → list A → list A
| [] l₂ := l₂
| (a::l₁) l₂ := if a ∈ l₂ then union l₁ l₂ else a :: union l₁ l₂
theorem nil_union (l : list A) : union [] l = l
theorem union_cons_of_mem {a : A} {l₂} : ∀ (l₁), a ∈ l₂ → union (a::l₁) l₂ = union l₁ l₂ :=
take l₁, assume ainl₂, calc
union (a::l₁) l₂ = if a ∈ l₂ then union l₁ l₂ else a :: union l₁ l₂ : rfl
... = union l₁ l₂ : if_pos ainl₂
theorem union_cons_of_not_mem {a : A} {l₂} : ∀ (l₁), a ∉ l₂ → union (a::l₁) l₂ = a :: union l₁ l₂ :=
take l₁, assume nainl₂, calc
union (a::l₁) l₂ = if a ∈ l₂ then union l₁ l₂ else a :: union l₁ l₂ : rfl
... = a :: union l₁ l₂ : if_neg nainl₂
theorem union_nil : ∀ (l : list A), union l [] = l
| [] := !nil_union
| (a::l) := by rewrite [union_cons_of_not_mem _ !not_mem_nil, union_nil]
theorem mem_or_mem_of_mem_union : ∀ {l₁ l₂} {a : A}, a ∈ union l₁ l₂ → a ∈ l₁ ∨ a ∈ l₂
| [] l₂ a ainl₂ := by rewrite nil_union at ainl₂; exact (or.inr (ainl₂))
| (b::l₁) l₂ a ainbl₁l₂ := by_cases
(λ binl₂ : b ∈ l₂,
have ainl₁l₂ : a ∈ union l₁ l₂, by rewrite [union_cons_of_mem l₁ binl₂ at ainbl₁l₂]; exact ainbl₁l₂,
or.elim (mem_or_mem_of_mem_union ainl₁l₂)
(λ ainl₁, or.inl (mem_cons_of_mem _ ainl₁))
(λ ainl₂, or.inr ainl₂))
(λ nbinl₂ : b ∉ l₂,
have ainb_l₁l₂ : a ∈ b :: union l₁ l₂, by rewrite [union_cons_of_not_mem l₁ nbinl₂ at ainbl₁l₂]; exact ainbl₁l₂,
or.elim (eq_or_mem_of_mem_cons ainb_l₁l₂)
(λ aeqb, by rewrite aeqb; exact (or.inl !mem_cons))
(λ ainl₁l₂,
or.elim (mem_or_mem_of_mem_union ainl₁l₂)
(λ ainl₁, or.inl (mem_cons_of_mem _ ainl₁))
(λ ainl₂, or.inr ainl₂)))
theorem mem_union_right {a : A} : ∀ (l₁) {l₂}, a ∈ l₂ → a ∈ union l₁ l₂
| [] l₂ h := by rewrite nil_union; exact h
| (b::l₁) l₂ h := by_cases
(λ binl₂ : b ∈ l₂, by rewrite [union_cons_of_mem _ binl₂]; exact (mem_union_right _ h))
(λ nbinl₂ : b ∉ l₂, by rewrite [union_cons_of_not_mem _ nbinl₂]; exact (mem_cons_of_mem _ (mem_union_right _ h)))
theorem mem_union_left {a : A} : ∀ {l₁} (l₂), a ∈ l₁ → a ∈ union l₁ l₂
| [] l₂ h := absurd h !not_mem_nil
| (b::l₁) l₂ h := by_cases
(λ binl₂ : b ∈ l₂, or.elim (eq_or_mem_of_mem_cons h)
(λ aeqb : a = b,
by rewrite [union_cons_of_mem l₁ binl₂, -aeqb at binl₂]; exact (mem_union_right _ binl₂))
(λ ainl₁ : a ∈ l₁,
by rewrite [union_cons_of_mem l₁ binl₂]; exact (mem_union_left _ ainl₁)))
(λ nbinl₂ : b ∉ l₂, or.elim (eq_or_mem_of_mem_cons h)
(λ aeqb : a = b,
by rewrite [union_cons_of_not_mem l₁ nbinl₂, aeqb]; exact !mem_cons)
(λ ainl₁ : a ∈ l₁,
by rewrite [union_cons_of_not_mem l₁ nbinl₂]; exact (mem_cons_of_mem _ (mem_union_left _ ainl₁))))
theorem mem_union_cons (a : A) (l₁ : list A) (l₂ : list A) : a ∈ union (a::l₁) l₂ :=
by_cases
(λ ainl₂ : a ∈ l₂, mem_union_right _ ainl₂)
(λ nainl₂ : a ∉ l₂, by rewrite [union_cons_of_not_mem _ nainl₂]; exact !mem_cons)
theorem nodup_union_of_nodup_of_nodup : ∀ {l₁ l₂ : list A}, nodup l₁ → nodup l₂ → nodup (union l₁ l₂)
| [] l₂ n₁ nl₂ := by rewrite nil_union; exact nl₂
| (a::l₁) l₂ nal₁ nl₂ :=
assert nl₁ : nodup l₁, from nodup_of_nodup_cons nal₁,
assert nl₁l₂ : nodup (union l₁ l₂), from nodup_union_of_nodup_of_nodup nl₁ nl₂,
by_cases
(λ ainl₂ : a ∈ l₂,
by rewrite [union_cons_of_mem l₁ ainl₂]; exact nl₁l₂)
(λ nainl₂ : a ∉ l₂,
have nainl₁ : a ∉ l₁, from not_mem_of_nodup_cons nal₁,
assert nainl₁l₂ : a ∉ union l₁ l₂, from
assume ainl₁l₂ : a ∈ union l₁ l₂, or.elim (mem_or_mem_of_mem_union ainl₁l₂)
(λ ainl₁, absurd ainl₁ nainl₁)
(λ ainl₂, absurd ainl₂ nainl₂),
by rewrite [union_cons_of_not_mem l₁ nainl₂]; exact (nodup_cons nainl₁l₂ nl₁l₂))
theorem union_eq_append : ∀ {l₁ l₂ : list A}, disjoint l₁ l₂ → union l₁ l₂ = append l₁ l₂
| [] l₂ d := rfl
| (a::l₁) l₂ d :=
assert nainl₂ : a ∉ l₂, from disjoint_left d !mem_cons,
assert d₁ : disjoint l₁ l₂, from disjoint_of_disjoint_cons_left d,
by rewrite [union_cons_of_not_mem _ nainl₂, append_cons, union_eq_append d₁]
theorem all_union {p : A → Prop} : ∀ {l₁ l₂ : list A}, all l₁ p → all l₂ p → all (union l₁ l₂) p
| [] l₂ h₁ h₂ := h₂
| (a::l₁) l₂ h₁ h₂ :=
have h₁' : all l₁ p, from all_of_all_cons h₁,
have pa : p a, from of_all_cons h₁,
assert au : all (union l₁ l₂) p, from all_union h₁' h₂,
assert au' : all (a :: union l₁ l₂) p, from all_cons_of_all pa au,
by_cases
(λ ainl₂ : a ∈ l₂, by rewrite [union_cons_of_mem _ ainl₂]; exact au)
(λ nainl₂ : a ∉ l₂, by rewrite [union_cons_of_not_mem _ nainl₂]; exact au')
theorem all_of_all_union_left {p : A → Prop} : ∀ {l₁ l₂ : list A}, all (union l₁ l₂) p → all l₁ p
| [] l₂ h := trivial
| (a::l₁) l₂ h :=
have ain : a ∈ union (a::l₁) l₂, from !mem_union_cons,
have pa : p a, from of_mem_of_all ain h,
by_cases
(λ ainl₂ : a ∈ l₂,
have al₁l₂ : all (union l₁ l₂) p, by rewrite [union_cons_of_mem _ ainl₂ at h]; exact h,
have al₁ : all l₁ p, from all_of_all_union_left al₁l₂,
all_cons_of_all pa al₁)
(λ nainl₂ : a ∉ l₂,
have aal₁l₂ : all (a::union l₁ l₂) p, by rewrite [union_cons_of_not_mem _ nainl₂ at h]; exact h,
have al₁l₂ : all (union l₁ l₂) p, from all_of_all_cons aal₁l₂,
have al₁ : all l₁ p, from all_of_all_union_left al₁l₂,
all_cons_of_all pa al₁)
theorem all_of_all_union_right {p : A → Prop} : ∀ {l₁ l₂ : list A}, all (union l₁ l₂) p → all l₂ p
| [] l₂ h := by rewrite [nil_union at h]; exact h
| (a::l₁) l₂ h := by_cases
(λ ainl₂ : a ∈ l₂, by rewrite [union_cons_of_mem _ ainl₂ at h]; exact (all_of_all_union_right h))
(λ nainl₂ : a ∉ l₂,
have h₁ : all (a :: union l₁ l₂) p, by rewrite [union_cons_of_not_mem _ nainl₂ at h]; exact h,
all_of_all_union_right (all_of_all_cons h₁))
variable {B : Type}
theorem foldl_union_of_disjoint (f : B → A → B) (b : B) {l₁ l₂ : list A} (d : disjoint l₁ l₂)
: foldl f b (union l₁ l₂) = foldl f (foldl f b l₁) l₂ :=
by rewrite [union_eq_append d, foldl_append]
theorem foldr_union_of_dijoint (f : A → B → B) (b : B) {l₁ l₂ : list A} (d : disjoint l₁ l₂)
: foldr f b (union l₁ l₂) = foldr f (foldr f b l₂) l₁ :=
by rewrite [union_eq_append d, foldr_append]
end union
/- insert -/
section insert
variable {A : Type}
variable [H : decidable_eq A]
include H
definition insert (a : A) (l : list A) : list A :=
if a ∈ l then l else a::l
theorem insert_eq_of_mem {a : A} {l : list A} : a ∈ l → insert a l = l :=
assume ainl, if_pos ainl
theorem insert_eq_of_not_mem {a : A} {l : list A} : a ∉ l → insert a l = a::l :=
assume nainl, if_neg nainl
theorem mem_insert (a : A) (l : list A) : a ∈ insert a l :=
by_cases
(λ ainl : a ∈ l, by rewrite [insert_eq_of_mem ainl]; exact ainl)
(λ nainl : a ∉ l, by rewrite [insert_eq_of_not_mem nainl]; exact !mem_cons)
theorem mem_insert_of_mem {a : A} (b : A) {l : list A} : a ∈ l → a ∈ insert b l :=
assume ainl, by_cases
(λ binl : b ∈ l, by rewrite [insert_eq_of_mem binl]; exact ainl)
(λ nbinl : b ∉ l, by rewrite [insert_eq_of_not_mem nbinl]; exact (mem_cons_of_mem _ ainl))
theorem eq_or_mem_of_mem_insert {x a : A} {l : list A} (H : x ∈ insert a l) : x = a ∨ x ∈ l :=
decidable.by_cases
(assume H3: a ∈ l, or.inr (insert_eq_of_mem H3 ▸ H))
(assume H3: a ∉ l,
have H4: x ∈ a :: l, from insert_eq_of_not_mem H3 ▸ H,
iff.mp !mem_cons_iff H4)
theorem mem_insert_iff (x a : A) (l : list A) : x ∈ insert a l ↔ x = a ∨ x ∈ l :=
iff.intro
(!eq_or_mem_of_mem_insert)
(assume H, or.elim H
(assume H' : x = a, H'⁻¹ ▸ !mem_insert)
(assume H' : x ∈ l, !mem_insert_of_mem H'))
theorem nodup_insert (a : A) {l : list A} : nodup l → nodup (insert a l) :=
assume n, by_cases
(λ ainl : a ∈ l, by rewrite [insert_eq_of_mem ainl]; exact n)
(λ nainl : a ∉ l, by rewrite [insert_eq_of_not_mem nainl]; exact (nodup_cons nainl n))
theorem length_insert_of_mem {a : A} {l : list A} : a ∈ l → length (insert a l) = length l :=
assume ainl, by rewrite [insert_eq_of_mem ainl]
theorem length_insert_of_not_mem {a : A} {l : list A} : a ∉ l → length (insert a l) = length l + 1 :=
assume nainl, by rewrite [insert_eq_of_not_mem nainl]
theorem all_insert_of_all {p : A → Prop} {a : A} {l} : p a → all l p → all (insert a l) p :=
assume h₁ h₂, by_cases
(λ ainl : a ∈ l, by rewrite [insert_eq_of_mem ainl]; exact h₂)
(λ nainl : a ∉ l, by rewrite [insert_eq_of_not_mem nainl]; exact (all_cons_of_all h₁ h₂))
end insert
/- inter -/
section inter
variable {A : Type}
variable [H : decidable_eq A]
include H
definition inter : list A → list A → list A
| [] l₂ := []
| (a::l₁) l₂ := if a ∈ l₂ then a :: inter l₁ l₂ else inter l₁ l₂
theorem inter_nil (l : list A) : inter [] l = []
theorem inter_cons_of_mem {a : A} (l₁ : list A) {l₂} : a ∈ l₂ → inter (a::l₁) l₂ = a :: inter l₁ l₂ :=
assume i, if_pos i
theorem inter_cons_of_not_mem {a : A} (l₁ : list A) {l₂} : a ∉ l₂ → inter (a::l₁) l₂ = inter l₁ l₂ :=
assume i, if_neg i
theorem mem_of_mem_inter_left : ∀ {l₁ l₂} {a : A}, a ∈ inter l₁ l₂ → a ∈ l₁
| [] l₂ a i := absurd i !not_mem_nil
| (b::l₁) l₂ a i := by_cases
(λ binl₂ : b ∈ l₂,
have aux : a ∈ b :: inter l₁ l₂, by rewrite [inter_cons_of_mem _ binl₂ at i]; exact i,
or.elim (eq_or_mem_of_mem_cons aux)
(λ aeqb : a = b, by rewrite [aeqb]; exact !mem_cons)
(λ aini, mem_cons_of_mem _ (mem_of_mem_inter_left aini)))
(λ nbinl₂ : b ∉ l₂,
have ainl₁ : a ∈ l₁, by rewrite [inter_cons_of_not_mem _ nbinl₂ at i]; exact (mem_of_mem_inter_left i),
mem_cons_of_mem _ ainl₁)
theorem mem_of_mem_inter_right : ∀ {l₁ l₂} {a : A}, a ∈ inter l₁ l₂ → a ∈ l₂
| [] l₂ a i := absurd i !not_mem_nil
| (b::l₁) l₂ a i := by_cases
(λ binl₂ : b ∈ l₂,
have aux : a ∈ b :: inter l₁ l₂, by rewrite [inter_cons_of_mem _ binl₂ at i]; exact i,
or.elim (eq_or_mem_of_mem_cons aux)
(λ aeqb : a = b, by rewrite [aeqb]; exact binl₂)
(λ aini : a ∈ inter l₁ l₂, mem_of_mem_inter_right aini))
(λ nbinl₂ : b ∉ l₂,
by rewrite [inter_cons_of_not_mem _ nbinl₂ at i]; exact (mem_of_mem_inter_right i))
theorem mem_inter_of_mem_of_mem : ∀ {l₁ l₂} {a : A}, a ∈ l₁ → a ∈ l₂ → a ∈ inter l₁ l₂
| [] l₂ a i₁ i₂ := absurd i₁ !not_mem_nil
| (b::l₁) l₂ a i₁ i₂ := by_cases
(λ binl₂ : b ∈ l₂,
or.elim (eq_or_mem_of_mem_cons i₁)
(λ aeqb : a = b,
by rewrite [inter_cons_of_mem _ binl₂, aeqb]; exact !mem_cons)
(λ ainl₁ : a ∈ l₁,
by rewrite [inter_cons_of_mem _ binl₂];
apply mem_cons_of_mem;
exact (mem_inter_of_mem_of_mem ainl₁ i₂)))
(λ nbinl₂ : b ∉ l₂,
or.elim (eq_or_mem_of_mem_cons i₁)
(λ aeqb : a = b, absurd (aeqb ▸ i₂) nbinl₂)
(λ ainl₁ : a ∈ l₁,
by rewrite [inter_cons_of_not_mem _ nbinl₂]; exact (mem_inter_of_mem_of_mem ainl₁ i₂)))
theorem nodup_inter_of_nodup : ∀ {l₁ : list A} (l₂), nodup l₁ → nodup (inter l₁ l₂)
| [] l₂ d := nodup_nil
| (a::l₁) l₂ d :=
have d₁ : nodup l₁, from nodup_of_nodup_cons d,
assert d₂ : nodup (inter l₁ l₂), from nodup_inter_of_nodup _ d₁,
have nainl₁ : a ∉ l₁, from not_mem_of_nodup_cons d,
assert naini : a ∉ inter l₁ l₂, from λ i, absurd (mem_of_mem_inter_left i) nainl₁,
by_cases
(λ ainl₂ : a ∈ l₂, by rewrite [inter_cons_of_mem _ ainl₂]; exact (nodup_cons naini d₂))
(λ nainl₂ : a ∉ l₂, by rewrite [inter_cons_of_not_mem _ nainl₂]; exact d₂)
theorem inter_eq_nil_of_disjoint : ∀ {l₁ l₂ : list A}, disjoint l₁ l₂ → inter l₁ l₂ = []
| [] l₂ d := rfl
| (a::l₁) l₂ d :=
assert aux_eq : inter l₁ l₂ = [], from inter_eq_nil_of_disjoint (disjoint_of_disjoint_cons_left d),
assert nainl₂ : a ∉ l₂, from disjoint_left d !mem_cons,
by rewrite [inter_cons_of_not_mem _ nainl₂, aux_eq]
theorem all_inter_of_all_left {p : A → Prop} : ∀ {l₁} (l₂), all l₁ p → all (inter l₁ l₂) p
| [] l₂ h := trivial
| (a::l₁) l₂ h :=
have h₁ : all l₁ p, from all_of_all_cons h,
assert h₂ : all (inter l₁ l₂) p, from all_inter_of_all_left _ h₁,
have pa : p a, from of_all_cons h,
assert h₃ : all (a :: inter l₁ l₂) p, from all_cons_of_all pa h₂,
by_cases
(λ ainl₂ : a ∈ l₂, by rewrite [inter_cons_of_mem _ ainl₂]; exact h₃)
(λ nainl₂ : a ∉ l₂, by rewrite [inter_cons_of_not_mem _ nainl₂]; exact h₂)
theorem all_inter_of_all_right {p : A → Prop} : ∀ (l₁) {l₂}, all l₂ p → all (inter l₁ l₂) p
| [] l₂ h := trivial
| (a::l₁) l₂ h :=
assert h₁ : all (inter l₁ l₂) p, from all_inter_of_all_right _ h,
by_cases
(λ ainl₂ : a ∈ l₂,
have pa : p a, from of_mem_of_all ainl₂ h,
assert h₂ : all (a :: inter l₁ l₂) p, from all_cons_of_all pa h₁,
by rewrite [inter_cons_of_mem _ ainl₂]; exact h₂)
(λ nainl₂ : a ∉ l₂, by rewrite [inter_cons_of_not_mem _ nainl₂]; exact h₁)
end inter
end list
|
a0d89d70dc859cffec259bd61cca1eefa18635c1 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/topology/uniform_space/pi.lean | 7f7880e29dad1ee26f64eec448005efc4c722c93 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 2,012 | lean | /-
Copyright (c) 2019 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot
-/
import topology.uniform_space.cauchy
import topology.uniform_space.separation
/-!
# Indexed product of uniform spaces
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
-/
noncomputable theory
open_locale uniformity topology
section
open filter uniform_space
universe u
variables {ι : Type*} (α : ι → Type u) [U : Πi, uniform_space (α i)]
include U
instance Pi.uniform_space : uniform_space (Πi, α i) :=
uniform_space.of_core_eq
(⨅i, uniform_space.comap (λ a : Πi, α i, a i) (U i)).to_core
Pi.topological_space $ eq.symm to_topological_space_infi
lemma Pi.uniformity :
𝓤 (Π i, α i) = ⨅ i : ι, filter.comap (λ a, (a.1 i, a.2 i)) $ 𝓤 (α i) :=
infi_uniformity
variable {α}
lemma uniform_continuous_pi {β : Type*} [uniform_space β] {f : β → Π i, α i} :
uniform_continuous f ↔ ∀ i, uniform_continuous (λ x, f x i) :=
by simp only [uniform_continuous, Pi.uniformity, tendsto_infi, tendsto_comap_iff]
variable (α)
lemma Pi.uniform_continuous_proj (i : ι) : uniform_continuous (λ (a : Π (i : ι), α i), a i) :=
uniform_continuous_pi.1 uniform_continuous_id i
instance Pi.complete [∀ i, complete_space (α i)] : complete_space (Π i, α i) :=
⟨begin
intros f hf,
haveI := hf.1,
have : ∀ i, ∃ x : α i, filter.map (λ a : Πi, α i, a i) f ≤ 𝓝 x,
{ intro i,
have key : cauchy (map (λ (a : Π (i : ι), α i), a i) f),
from hf.map (Pi.uniform_continuous_proj α i),
exact cauchy_iff_exists_le_nhds.1 key },
choose x hx using this,
use x,
rwa [nhds_pi, le_pi],
end⟩
instance Pi.separated [∀ i, separated_space (α i)] : separated_space (Π i, α i) :=
separated_def.2 $ assume x y H,
begin
ext i,
apply eq_of_separated_of_uniform_continuous (Pi.uniform_continuous_proj α i),
apply H,
end
end
|
ef883dfafce55e41cd21b6d586ad8756d9535df0 | c3f2fcd060adfa2ca29f924839d2d925e8f2c685 | /library/algebra/ring.lean | 84f050724dc0aa0add40ce9cedab6c05eb346174 | [
"Apache-2.0"
] | permissive | respu/lean | 6582d19a2f2838a28ecd2b3c6f81c32d07b5341d | 8c76419c60b63d0d9f7bc04ebb0b99812d0ec654 | refs/heads/master | 1,610,882,451,231 | 1,427,747,084,000 | 1,427,747,429,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 13,174 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Module: algebra.ring
Authors: Jeremy Avigad, Leonardo de Moura
Structures with multiplicative and additive components, including semirings, rings, and fields.
The development is modeled after Isabelle's library.
-/
import logic.eq logic.connectives data.unit data.sigma data.prod
import algebra.function algebra.binary algebra.group
open eq eq.ops
namespace algebra
variable {A : Type}
/- auxiliary classes -/
structure distrib [class] (A : Type) extends has_mul A, has_add A :=
(left_distrib : ∀a b c, mul a (add b c) = add (mul a b) (mul a c))
(right_distrib : ∀a b c, mul (add a b) c = add (mul a c) (mul b c))
theorem left_distrib [s : distrib A] (a b c : A) : a * (b + c) = a * b + a * c :=
!distrib.left_distrib
theorem right_distrib [s: distrib A] (a b c : A) : (a + b) * c = a * c + b * c :=
!distrib.right_distrib
structure mul_zero_class [class] (A : Type) extends has_mul A, has_zero A :=
(zero_mul : ∀a, mul zero a = zero)
(mul_zero : ∀a, mul a zero = zero)
theorem zero_mul [s : mul_zero_class A] (a : A) : 0 * a = 0 := !mul_zero_class.zero_mul
theorem mul_zero [s : mul_zero_class A] (a : A) : a * 0 = 0 := !mul_zero_class.mul_zero
structure zero_ne_one_class [class] (A : Type) extends has_zero A, has_one A :=
(zero_ne_one : zero ≠ one)
theorem zero_ne_one [s: zero_ne_one_class A] : 0 ≠ 1 := @zero_ne_one_class.zero_ne_one A s
/- semiring -/
structure semiring [class] (A : Type) extends add_comm_monoid A, monoid A, distrib A,
mul_zero_class A
section semiring
variables [s : semiring A] (a b c : A)
include s
theorem ne_zero_of_mul_ne_zero_right {a b : A} (H : a * b ≠ 0) : a ≠ 0 :=
assume H1 : a = 0,
have H2 : a * b = 0, from H1⁻¹ ▸ zero_mul b,
H H2
theorem ne_zero_of_mul_ne_zero_left {a b : A} (H : a * b ≠ 0) : b ≠ 0 :=
assume H1 : b = 0,
have H2 : a * b = 0, from H1⁻¹ ▸ mul_zero a,
H H2
end semiring
/- comm semiring -/
structure comm_semiring [class] (A : Type) extends semiring A, comm_semigroup A
-- TODO: we could also define a cancelative comm_semiring, i.e. satisfying
-- c ≠ 0 → c * a = c * b → a = b.
section comm_semiring
variables [s : comm_semiring A] (a b c : A)
include s
definition dvd (a b : A) : Prop := ∃c, b = a * c
notation ( a | b ) := dvd a b
theorem dvd.intro {a b c : A} (H : a * c = b) : (a | b) :=
exists.intro _ H⁻¹
theorem dvd.intro_left {a b c : A} (H : c * a = b) : (a | b) :=
dvd.intro (!mul.comm ▸ H)
theorem exists_eq_mul_right_of_dvd {a b : A} (H : (a | b)) : ∃c, b = a * c := H
theorem dvd.elim {P : Prop} {a b : A} (H₁ : (a | b)) (H₂ : ∀c, b = a * c → P) : P :=
exists.elim H₁ H₂
theorem exists_eq_mul_left_of_dvd {a b : A} (H : (a | b)) : ∃c, b = c * a :=
dvd.elim H (take c, assume H1 : b = a * c, exists.intro c (H1 ⬝ !mul.comm))
theorem dvd.elim_left {P : Prop} {a b : A} (H₁ : (a | b)) (H₂ : ∀c, b = c * a → P) : P :=
exists.elim (exists_eq_mul_left_of_dvd H₁) (take c, assume H₃ : b = c * a, H₂ c H₃)
theorem dvd.refl : (a | a) := dvd.intro !mul_one
theorem dvd.trans {a b c : A} (H₁ : (a | b)) (H₂ : (b | c)) : (a | c) :=
dvd.elim H₁
(take d, assume H₃ : b = a * d,
dvd.elim H₂
(take e, assume H₄ : c = b * e,
dvd.intro
(show a * (d * e) = c, by rewrite [-mul.assoc, -H₃, H₄])))
theorem eq_zero_of_zero_dvd {a : A} (H : (0 | a)) : a = 0 :=
dvd.elim H (take c, assume H' : a = 0 * c, H' ⬝ !zero_mul)
theorem dvd_zero : (a | 0) := dvd.intro !mul_zero
theorem one_dvd : (1 | a) := dvd.intro !one_mul
theorem dvd_mul_right : (a | a * b) := dvd.intro rfl
theorem dvd_mul_left : (a | b * a) := mul.comm a b ▸ dvd_mul_right a b
theorem dvd_mul_of_dvd_left {a b : A} (H : (a | b)) (c : A) : (a | b * c) :=
dvd.elim H
(take d,
assume H₁ : b = a * d,
dvd.intro
(show a * (d * c) = b * c, from by rewrite [-mul.assoc, H₁]))
theorem dvd_mul_of_dvd_right {a b : A} (H : (a | b)) (c : A) : (a | c * b) :=
!mul.comm ▸ (dvd_mul_of_dvd_left H _)
theorem mul_dvd_mul {a b c d : A} (dvd_ab : (a | b)) (dvd_cd : (c | d)) : (a * c | b * d) :=
dvd.elim dvd_ab
(take e, assume Haeb : b = a * e,
dvd.elim dvd_cd
(take f, assume Hcfd : d = c * f,
dvd.intro
(show a * c * (e * f) = b * d,
by rewrite [mul.assoc, {c*_}mul.left_comm, -mul.assoc, Haeb, Hcfd])))
theorem dvd_of_mul_right_dvd {a b c : A} (H : (a * b | c)) : (a | c) :=
dvd.elim H (take d, assume Habdc : c = a * b * d, dvd.intro (!mul.assoc⁻¹ ⬝ Habdc⁻¹))
theorem dvd_of_mul_left_dvd {a b c : A} (H : (a * b | c)) : (b | c) :=
dvd_of_mul_right_dvd (mul.comm a b ▸ H)
theorem dvd_add {a b c : A} (Hab : (a | b)) (Hac : (a | c)) : (a | b + c) :=
dvd.elim Hab
(take d, assume Hadb : b = a * d,
dvd.elim Hac
(take e, assume Haec : c = a * e,
dvd.intro (show a * (d + e) = b + c,
by rewrite [left_distrib, -Hadb, -Haec])))
end comm_semiring
/- ring -/
structure ring [class] (A : Type) extends add_comm_group A, monoid A, distrib A
theorem ring.mul_zero [s : ring A] (a : A) : a * 0 = 0 :=
have H : a * 0 + 0 = a * 0 + a * 0, from calc
a * 0 + 0 = a * 0 : by rewrite add_zero
... = a * (0 + 0) : by rewrite add_zero
... = a * 0 + a * 0 : by rewrite {a*_}ring.left_distrib,
show a * 0 = 0, from (add.left_cancel H)⁻¹
theorem ring.zero_mul [s : ring A] (a : A) : 0 * a = 0 :=
have H : 0 * a + 0 = 0 * a + 0 * a, from calc
0 * a + 0 = 0 * a : by rewrite add_zero
... = (0 + 0) * a : by rewrite add_zero
... = 0 * a + 0 * a : by rewrite {_*a}ring.right_distrib,
show 0 * a = 0, from (add.left_cancel H)⁻¹
definition ring.to_semiring [instance] [coercion] [reducible] [s : ring A] : semiring A :=
⦃ semiring, s,
mul_zero := ring.mul_zero,
zero_mul := ring.zero_mul ⦄
section
variables [s : ring A] (a b c d e : A)
include s
theorem neg_mul_eq_neg_mul : -(a * b) = -a * b :=
neg_eq_of_add_eq_zero
begin
rewrite [-right_distrib, add.right_inv, zero_mul]
end
theorem neg_mul_eq_mul_neg : -(a * b) = a * -b :=
neg_eq_of_add_eq_zero
begin
rewrite [-left_distrib, add.right_inv, mul_zero]
end
theorem neg_mul_neg : -a * -b = a * b :=
calc
-a * -b = -(a * -b) : by rewrite -neg_mul_eq_neg_mul
... = - -(a * b) : by rewrite -neg_mul_eq_mul_neg
... = a * b : by rewrite neg_neg
theorem neg_mul_comm : -a * b = a * -b := !neg_mul_eq_neg_mul⁻¹ ⬝ !neg_mul_eq_mul_neg
theorem neg_eq_neg_one_mul : -a = -1 * a :=
calc
-a = -(1 * a) : by rewrite one_mul
... = -1 * a : by rewrite neg_mul_eq_neg_mul
theorem mul_sub_left_distrib : a * (b - c) = a * b - a * c :=
calc
a * (b - c) = a * b + a * -c : left_distrib
... = a * b + - (a * c) : by rewrite -neg_mul_eq_mul_neg
... = a * b - a * c : rfl
theorem mul_sub_right_distrib : (a - b) * c = a * c - b * c :=
calc
(a - b) * c = a * c + -b * c : right_distrib
... = a * c + - (b * c) : by rewrite neg_mul_eq_neg_mul
... = a * c - b * c : rfl
-- TODO: can calc mode be improved to make this easier?
-- TODO: there is also the other direction. It will be easier when we
-- have the simplifier.
theorem mul_add_eq_mul_add_iff_sub_mul_add_eq : a * e + c = b * e + d ↔ (a - b) * e + c = d :=
calc
a * e + c = b * e + d ↔ a * e + c = d + b * e : by rewrite {b*e+_}add.comm
... ↔ a * e + c - b * e = d : iff.symm !sub_eq_iff_eq_add
... ↔ a * e - b * e + c = d : by rewrite sub_add_eq_add_sub
... ↔ (a - b) * e + c = d : by rewrite mul_sub_right_distrib
theorem mul_neg_one_eq_neg : a * (-1) = -a :=
have H : a + a * -1 = 0, from calc
a + a * -1 = a * 1 + a * -1 : mul_one
... = a * (1 + -1) : left_distrib
... = a * 0 : add.right_inv
... = 0 : mul_zero,
symm (neg_eq_of_add_eq_zero H)
theorem mul_ne_zero_imp_ne_zero {a b} (H : a * b ≠ 0) : a ≠ 0 ∧ b ≠ 0 :=
have Ha : a ≠ 0, from
(assume Ha1 : a = 0,
have H1 : a * b = 0, by rewrite [Ha1, zero_mul],
absurd H1 H),
have Hb : b ≠ 0, from
(assume Hb1 : b = 0,
have H1 : a * b = 0, by rewrite [Hb1, mul_zero],
absurd H1 H),
and.intro Ha Hb
end
structure comm_ring [class] (A : Type) extends ring A, comm_semigroup A
definition comm_ring.to_comm_semiring [instance] [coercion] [reducible] [s : comm_ring A] : comm_semiring A :=
⦃ comm_semiring, s,
mul_zero := mul_zero,
zero_mul := zero_mul ⦄
section
variables [s : comm_ring A] (a b c d e : A)
include s
theorem mul_self_sub_mul_self_eq : a * a - b * b = (a + b) * (a - b) :=
by rewrite [left_distrib, *right_distrib, add.assoc, -{b*a + _}add.assoc,
-*neg_mul_eq_mul_neg, {a*b}mul.comm, add.right_inv, zero_add]
theorem mul_self_sub_one_eq : a * a - 1 = (a + 1) * (a - 1) :=
mul_one 1 ▸ mul_self_sub_mul_self_eq a 1
theorem dvd_neg_iff_dvd : (a | -b) ↔ (a | b) :=
iff.intro
(assume H : (a | -b),
dvd.elim H
(take c, assume H' : -b = a * c,
dvd.intro
(show a * -c = b,
by rewrite [-neg_mul_eq_mul_neg, -H', neg_neg])))
(assume H : (a | b),
dvd.elim H
(take c, assume H' : b = a * c,
dvd.intro
(show a * -c = -b,
by rewrite [-neg_mul_eq_mul_neg, -H'])))
theorem neg_dvd_iff_dvd : (-a | b) ↔ (a | b) :=
iff.intro
(assume H : (-a | b),
dvd.elim H
(take c, assume H' : b = -a * c,
dvd.intro
(show a * -c = b, by rewrite [-neg_mul_comm, H'])))
(assume H : (a | b),
dvd.elim H
(take c, assume H' : b = a * c,
dvd.intro
(show -a * -c = b, by rewrite [neg_mul_neg, H'])))
theorem dvd_sub (H₁ : (a | b)) (H₂ : (a | c)) : (a | b - c) :=
dvd_add H₁ (iff.elim_right !dvd_neg_iff_dvd H₂)
end
/- integral domains -/
structure no_zero_divisors [class] (A : Type) extends has_mul A, has_zero A :=
(eq_zero_or_eq_zero_of_mul_eq_zero : ∀a b, mul a b = zero → a = zero ∨ b = zero)
theorem eq_zero_or_eq_zero_of_mul_eq_zero {A : Type} [s : no_zero_divisors A] {a b : A}
(H : a * b = 0) :
a = 0 ∨ b = 0 := !no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero H
structure integral_domain [class] (A : Type) extends comm_ring A, no_zero_divisors A
section
variables [s : integral_domain A] (a b c d e : A)
include s
theorem mul_ne_zero {a b : A} (H1 : a ≠ 0) (H2 : b ≠ 0) : a * b ≠ 0 :=
assume H : a * b = 0,
or.elim (eq_zero_or_eq_zero_of_mul_eq_zero H) (assume H3, H1 H3) (assume H4, H2 H4)
theorem mul.cancel_right {a b c : A} (Ha : a ≠ 0) (H : b * a = c * a) : b = c :=
have H1 : b * a - c * a = 0, from iff.mp !eq_iff_sub_eq_zero H,
have H2 : (b - c) * a = 0, using H1, by rewrite [mul_sub_right_distrib, H1],
have H3 : b - c = 0, from or_resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero H2) Ha,
iff.elim_right !eq_iff_sub_eq_zero H3
theorem mul.cancel_left {a b c : A} (Ha : a ≠ 0) (H : a * b = a * c) : b = c :=
have H1 : a * b - a * c = 0, from iff.mp !eq_iff_sub_eq_zero H,
have H2 : a * (b - c) = 0, using H1, by rewrite [mul_sub_left_distrib, H1],
have H3 : b - c = 0, from or_resolve_right (eq_zero_or_eq_zero_of_mul_eq_zero H2) Ha,
iff.elim_right !eq_iff_sub_eq_zero H3
-- TODO: do we want the iff versions?
theorem mul_self_eq_mul_self_iff (a b : A) : a * a = b * b ↔ a = b ∨ a = -b :=
iff.intro
(λ H : a * a = b * b,
have aux₁ : (a - b) * (a + b) = 0,
by rewrite [mul.comm, -mul_self_sub_mul_self_eq, H, sub_self],
assert aux₂ : a - b = 0 ∨ a + b = 0, from !eq_zero_or_eq_zero_of_mul_eq_zero aux₁,
or.elim aux₂
(λ H : a - b = 0, or.inl (eq_of_sub_eq_zero H))
(λ H : a + b = 0, or.inr (eq_neg_of_add_eq_zero H)))
(λ H : a = b ∨ a = -b, or.elim H
(λ a_eq_b, by rewrite a_eq_b)
(λ a_eq_mb, by rewrite [a_eq_mb, neg_mul_neg]))
theorem mul_self_eq_one_iff (a : A) : a * a = 1 ↔ a = 1 ∨ a = -1 :=
assert aux : a * a = 1 * 1 ↔ a = 1 ∨ a = -1, from mul_self_eq_mul_self_iff a 1,
by rewrite mul_one at aux; exact aux
-- TODO: c - b * c → c = 0 ∨ b = 1 and variants
theorem dvd_of_mul_dvd_mul_left {a b c : A} (Ha : a ≠ 0) (Hdvd : (a * b | a * c)) : (b | c) :=
dvd.elim Hdvd
(take d,
assume H : a * c = a * b * d,
have H1 : b * d = c, from mul.cancel_left Ha (mul.assoc a b d ▸ H⁻¹),
dvd.intro H1)
theorem dvd_of_mul_dvd_mul_right {a b c : A} (Ha : a ≠ 0) (Hdvd : (b * a | c * a)) : (b | c) :=
dvd.elim Hdvd
(take d,
assume H : c * a = b * a * d,
have H1 : b * d * a = c * a, from by rewrite [mul.right_comm, -H],
have H2 : b * d = c, from mul.cancel_right Ha H1,
dvd.intro H2)
end
end algebra
|
71c0f288e93fd3979f46fa34f08396928b7516c7 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Meta/GlobalInstances.lean | db9b7e5c3e16e667c750fbc7c89348fa348ffbc1 | [
"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 | 758 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Basic
import Lean.ScopedEnvExtension
namespace Lean.Meta
builtin_initialize globalInstanceExtension : SimpleScopedEnvExtension Name (PersistentHashMap Name Unit) ←
registerSimpleScopedEnvExtension {
initial := {}
addEntry := fun s n => s.insert n ()
}
def addGlobalInstance (declName : Name) (attrKind : AttributeKind) : MetaM Unit := do
globalInstanceExtension.add declName attrKind
@[export lean_is_instance]
def isGlobalInstance (env : Environment) (declName : Name) : Bool :=
globalInstanceExtension.getState env |>.contains declName
end Lean.Meta
|
454dc7eea81f8031a55fad3cb9a56273834148e3 | 618003631150032a5676f229d13a079ac875ff77 | /src/tactic/chain.lean | fa30e001a1b0a620dc84b036ecf8ad7c77b9a10e | [
"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 | 4,923 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Mario Carneiro
-/
import tactic.ext
open interactive
namespace tactic
/-
This file defines a `chain` tactic, which takes a list of tactics, and exhaustively tries to apply them
to the goals, until no tactic succeeds on any goal.
Along the way, it generates auxiliary declarations, in order to speed up elaboration time
of the resulting (sometimes long!) proofs.
This tactic is used by the `tidy` tactic.
-/
-- α is the return type of our tactics. When `chain` is called by `tidy`, this is string,
-- describing what that tactic did as an interactive tactic.
variable {α : Type}
/-
Because chain sometimes pauses work on the first goal and works on later goals, we need a method
for combining a list of results generated while working on a later goal into a single result.
This enables `tidy {trace_result := tt}` to output faithfully reproduces its operation, e.g.
````
intros,
simp,
apply lemma_1,
work_on_goal 2 {
dsimp,
simp
},
refl
````
-/
namespace interactive
open lean.parser
meta def work_on_goal : parse small_nat → itactic → tactic unit
| n t := do goals ← get_goals,
let earlier_goals := goals.take n,
let later_goals := goals.drop (n+1),
set_goals (goals.nth n).to_list,
t,
new_goals ← get_goals,
set_goals (earlier_goals ++ new_goals ++ later_goals)
end interactive
inductive tactic_script (α : Type) : Type
| base : α → tactic_script
| work (index : ℕ) (first : α) (later : list tactic_script) (closed : bool) : tactic_script
meta def tactic_script.to_string : tactic_script string → string
| (tactic_script.base a) := a
| (tactic_script.work n a l c) := "work_on_goal " ++ (to_string n) ++ " { " ++ (", ".intercalate (a :: l.map tactic_script.to_string)) ++ " }"
meta instance : has_to_string (tactic_script string) :=
{ to_string := λ s, s.to_string }
meta instance tactic_script_unit_has_to_string : has_to_string (tactic_script unit) :=
{ to_string := λ s, "[chain tactic]" }
meta def abstract_if_success (tac : expr → tactic α) (g : expr) : tactic α :=
do
type ← infer_type g,
is_lemma ← is_prop type,
if is_lemma then -- there's no point making the abstraction, and indeed it's slower
tac g
else do
m ← mk_meta_var type,
a ← tac m,
do {
val ← instantiate_mvars m,
guard (val.list_meta_vars = []),
c ← new_aux_decl_name,
gs ← get_goals,
set_goals [g],
add_aux_decl c type val ff >>= unify g,
set_goals gs }
<|> unify m g,
return a
/--
`chain_many tac` recursively tries `tac` on all goals, working depth-first on generated subgoals,
until it no longer succeeds on any goal. `chain_many` automatically makes auxiliary definitions.
-/
meta mutual def chain_single, chain_many, chain_iter {α} (tac : tactic α)
with chain_single : expr → tactic (α × list (tactic_script α)) | g :=
do set_goals [g],
a ← tac,
l ← get_goals >>= chain_many,
return (a, l)
with chain_many : list expr → tactic (list (tactic_script α))
| [] := return []
| [g] := do {
(a, l) ← chain_single g,
return (tactic_script.base a :: l) } <|> return []
| gs := chain_iter gs []
with chain_iter : list expr → list expr → tactic (list (tactic_script α))
| [] _ := return []
| (g :: later_goals) stuck_goals := do {
(a, l) ← abstract_if_success chain_single g,
new_goals ← get_goals,
let w := tactic_script.work stuck_goals.length a l (new_goals = []),
let current_goals := stuck_goals.reverse ++ new_goals ++ later_goals,
set_goals current_goals, -- we keep the goals up to date, so they are correct at the end
l' ← chain_many current_goals,
return (w :: l') } <|> chain_iter later_goals (g :: stuck_goals)
meta def chain_core {α : Type} [has_to_string (tactic_script α)] (tactics : list (tactic α)) : tactic (list string) :=
do results ← (get_goals >>= chain_many (first tactics)),
when results.empty (fail "`chain` tactic made no progress"),
return (results.map to_string)
variables [has_to_string (tactic_script α)] [has_to_format α]
declare_trace chain
meta def trace_output (t : tactic α) : tactic α :=
do tgt ← target,
r ← t,
name ← decl_name,
trace format!"`chain` successfully applied a tactic during elaboration of {name}:",
tgt ← pp tgt,
trace format!"previous target: {tgt}",
trace format!"tactic result: {r}",
tgt ← try_core target,
tgt ← match tgt with
| (some tgt) := pp tgt
| none := return "no goals"
end,
trace format!"new target: {tgt}",
pure r
meta def chain (tactics : list (tactic α)) : tactic (list string) :=
chain_core
(if is_trace_enabled_for `chain then (tactics.map trace_output) else tactics)
end tactic
|
b26de98824692d10d8e535aab8cbe303421cc468 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/category_theory/limits/shapes/wide_pullbacks.lean | 1bca7d2a7fa8c7e58b77c5a8243a1bf1dbe24496 | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 11,514 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import category_theory.limits.has_limits
import category_theory.thin
/-!
# Wide pullbacks
We define the category `wide_pullback_shape`, (resp. `wide_pushout_shape`) which is the category
obtained from a discrete category of type `J` by adjoining a terminal (resp. initial) element.
Limits of this shape are wide pullbacks (pushouts).
The convenience method `wide_cospan` (`wide_span`) constructs a functor from this category, hitting
the given morphisms.
We use `wide_pullback_shape` to define ordinary pullbacks (pushouts) by using `J := walking_pair`,
which allows easy proofs of some related lemmas.
Furthermore, wide pullbacks are used to show the existence of limits in the slice category.
Namely, if `C` has wide pullbacks then `C/B` has limits for any object `B` in `C`.
Typeclasses `has_wide_pullbacks` and `has_finite_wide_pullbacks` assert the existence of wide
pullbacks and finite wide pullbacks.
-/
universes v u
open category_theory category_theory.limits
namespace category_theory.limits
variable (J : Type v)
/-- A wide pullback shape for any type `J` can be written simply as `option J`. -/
@[derive inhabited]
def wide_pullback_shape := option J
/-- A wide pushout shape for any type `J` can be written simply as `option J`. -/
@[derive inhabited]
def wide_pushout_shape := option J
namespace wide_pullback_shape
variable {J}
/-- The type of arrows for the shape indexing a wide pullback. -/
@[derive decidable_eq]
inductive hom : wide_pullback_shape J → wide_pullback_shape J → Type v
| id : Π X, hom X X
| term : Π (j : J), hom (some j) none
attribute [nolint unused_arguments] hom.decidable_eq
instance struct : category_struct (wide_pullback_shape J) :=
{ hom := hom,
id := λ j, hom.id j,
comp := λ j₁ j₂ j₃ f g,
begin
cases f,
exact g,
cases g,
apply hom.term _
end }
instance hom.inhabited : inhabited (hom none none) := ⟨hom.id (none : wide_pullback_shape J)⟩
local attribute [tidy] tactic.case_bash
instance subsingleton_hom (j j' : wide_pullback_shape J) : subsingleton (j ⟶ j') :=
⟨by tidy⟩
instance category : small_category (wide_pullback_shape J) := thin_category
@[simp] lemma hom_id (X : wide_pullback_shape J) : hom.id X = 𝟙 X := rfl
variables {C : Type u} [category.{v} C]
/--
Construct a functor out of the wide pullback shape given a J-indexed collection of arrows to a
fixed object.
-/
@[simps]
def wide_cospan (B : C) (objs : J → C) (arrows : Π (j : J), objs j ⟶ B) :
wide_pullback_shape J ⥤ C :=
{ obj := λ j, option.cases_on j B objs,
map := λ X Y f,
begin
cases f with _ j,
{ apply (𝟙 _) },
{ exact arrows j }
end }
/-- Every diagram is naturally isomorphic (actually, equal) to a `wide_cospan` -/
def diagram_iso_wide_cospan (F : wide_pullback_shape J ⥤ C) :
F ≅ wide_cospan (F.obj none) (λ j, F.obj (some j)) (λ j, F.map (hom.term j)) :=
nat_iso.of_components (λ j, eq_to_iso $ by tidy) $ by tidy
/-- Construct a cone over a wide cospan. -/
@[simps]
def mk_cone {F : wide_pullback_shape J ⥤ C} {X : C}
(f : X ⟶ F.obj none) (π : Π j, X ⟶ F.obj (some j))
(w : ∀ j, π j ≫ F.map (hom.term j) = f) : cone F :=
{ X := X,
π :=
{ app := λ j, match j with
| none := f
| (some j) := π j
end,
naturality' := λ j j' f, by { cases j; cases j'; cases f; unfold_aux; dsimp; simp [w], }, } }
end wide_pullback_shape
namespace wide_pushout_shape
variable {J}
/-- The type of arrows for the shape indexing a wide psuhout. -/
@[derive decidable_eq]
inductive hom : wide_pushout_shape J → wide_pushout_shape J → Type v
| id : Π X, hom X X
| init : Π (j : J), hom none (some j)
attribute [nolint unused_arguments] hom.decidable_eq
instance struct : category_struct (wide_pushout_shape J) :=
{ hom := hom,
id := λ j, hom.id j,
comp := λ j₁ j₂ j₃ f g,
begin
cases f,
exact g,
cases g,
apply hom.init _
end }
instance hom.inhabited : inhabited (hom none none) := ⟨hom.id (none : wide_pushout_shape J)⟩
local attribute [tidy] tactic.case_bash
instance subsingleton_hom (j j' : wide_pushout_shape J) : subsingleton (j ⟶ j') :=
⟨by tidy⟩
instance category : small_category (wide_pushout_shape J) := thin_category
@[simp] lemma hom_id (X : wide_pushout_shape J) : hom.id X = 𝟙 X := rfl
variables {C : Type u} [category.{v} C]
/--
Construct a functor out of the wide pushout shape given a J-indexed collection of arrows from a
fixed object.
-/
@[simps]
def wide_span (B : C) (objs : J → C) (arrows : Π (j : J), B ⟶ objs j) : wide_pushout_shape J ⥤ C :=
{ obj := λ j, option.cases_on j B objs,
map := λ X Y f,
begin
cases f with _ j,
{ apply (𝟙 _) },
{ exact arrows j }
end }
/-- Every diagram is naturally isomorphic (actually, equal) to a `wide_span` -/
def diagram_iso_wide_span (F : wide_pushout_shape J ⥤ C) :
F ≅ wide_span (F.obj none) (λ j, F.obj (some j)) (λ j, F.map (hom.init j)) :=
nat_iso.of_components (λ j, eq_to_iso $ by tidy) $ by tidy
/-- Construct a cocone over a wide span. -/
@[simps]
def mk_cocone {F : wide_pushout_shape J ⥤ C} {X : C}
(f : F.obj none ⟶ X) (ι : Π j, F.obj (some j) ⟶ X)
(w : ∀ j, F.map (hom.init j) ≫ ι j = f) : cocone F :=
{ X := X,
ι :=
{ app := λ j, match j with
| none := f
| (some j) := ι j
end,
naturality' := λ j j' f, by { cases j; cases j'; cases f; unfold_aux; dsimp; simp [w], }, } }
end wide_pushout_shape
variables (C : Type u) [category.{v} C]
/-- `has_wide_pullbacks` represents a choice of wide pullback for every collection of morphisms -/
abbreviation has_wide_pullbacks : Prop :=
Π (J : Type v), has_limits_of_shape (wide_pullback_shape J) C
/-- `has_wide_pushouts` represents a choice of wide pushout for every collection of morphisms -/
abbreviation has_wide_pushouts : Prop :=
Π (J : Type v), has_colimits_of_shape (wide_pushout_shape J) C
variables {C J}
/-- `has_wide_pullback B objs arrows` means that `wide_cospan B objs arrows` has a limit. -/
abbreviation has_wide_pullback (B : C) (objs : J → C)
(arrows : Π (j : J), objs j ⟶ B) : Prop :=
has_limit (wide_pullback_shape.wide_cospan B objs arrows)
/-- `has_wide_pushout B objs arrows` means that `wide_span B objs arrows` has a colimit. -/
abbreviation has_wide_pushout (B : C) (objs : J → C)
(arrows : Π (j : J), B ⟶ objs j) : Prop :=
has_colimit (wide_pushout_shape.wide_span B objs arrows)
/-- A choice of wide pullback. -/
noncomputable
abbreviation wide_pullback (B : C) (objs : J → C) (arrows : Π (j : J), objs j ⟶ B)
[has_wide_pullback B objs arrows] : C :=
limit (wide_pullback_shape.wide_cospan B objs arrows)
/-- A choice of wide pushout. -/
noncomputable
abbreviation wide_pushout (B : C) (objs : J → C) (arrows : Π (j : J), B ⟶ objs j)
[has_wide_pushout B objs arrows] : C :=
colimit (wide_pushout_shape.wide_span B objs arrows)
variable (C)
namespace wide_pullback
variables {C} {B : C} {objs : J → C} (arrows : Π (j : J), objs j ⟶ B)
variables [has_wide_pullback B objs arrows]
/-- The `j`-th projection from the pullback. -/
noncomputable
abbreviation π (j : J) : wide_pullback _ _ arrows ⟶ objs j :=
limit.π (wide_pullback_shape.wide_cospan _ _ _) (option.some j)
/-- The unique map to the base from the pullback. -/
noncomputable
abbreviation base : wide_pullback _ _ arrows ⟶ B :=
limit.π (wide_pullback_shape.wide_cospan _ _ _) option.none
@[simp, reassoc]
lemma π_arrow (j : J) : π arrows j ≫ arrows _ = base arrows :=
by apply limit.w (wide_pullback_shape.wide_cospan _ _ _) (wide_pullback_shape.hom.term j)
variables {arrows}
/-- Lift a collection of morphisms to a morphism to the pullback. -/
noncomputable
abbreviation lift {X : C} (f : X ⟶ B) (fs : Π (j : J), X ⟶ objs j)
(w : ∀ j, fs j ≫ arrows j = f) : X ⟶ wide_pullback _ _ arrows :=
limit.lift (wide_pullback_shape.wide_cospan _ _ _)
(wide_pullback_shape.mk_cone f fs $ by exact w)
variables (arrows)
variables {X : C} (f : X ⟶ B) (fs : Π (j : J), X ⟶ objs j)
(w : ∀ j, fs j ≫ arrows j = f)
@[simp, reassoc]
lemma lift_π (j : J) : lift f fs w ≫ π arrows j = fs _ :=
by { simp, refl }
@[simp, reassoc]
lemma lift_base : lift f fs w ≫ base arrows = f :=
by { simp, refl }
lemma eq_lift_of_comp_eq (g : X ⟶ wide_pullback _ _ arrows) :
(∀ j : J, g ≫ π arrows j = fs j) → g ≫ base arrows = f → g = lift f fs w :=
begin
intros h1 h2,
apply (limit.is_limit (wide_pullback_shape.wide_cospan B objs arrows)).uniq
(wide_pullback_shape.mk_cone f fs $ by exact w),
rintro (_|_),
{ apply h2 },
{ apply h1 }
end
lemma hom_eq_lift (g : X ⟶ wide_pullback _ _ arrows) :
g = lift (g ≫ base arrows) (λ j, g ≫ π arrows j) (by tidy) :=
begin
apply eq_lift_of_comp_eq,
tidy,
end
@[ext]
lemma hom_ext (g1 g2 : X ⟶ wide_pullback _ _ arrows) :
(∀ j : J, g1 ≫ π arrows j = g2 ≫ π arrows j) →
g1 ≫ base arrows = g2 ≫ base arrows → g1 = g2 :=
begin
intros h1 h2,
apply limit.hom_ext,
rintros (_|_),
{ apply h2 },
{ apply h1 },
end
end wide_pullback
namespace wide_pushout
variables {C} {B : C} {objs : J → C} (arrows : Π (j : J), B ⟶ objs j)
variables [has_wide_pushout B objs arrows]
/-- The `j`-th inclusion to the pushout. -/
noncomputable
abbreviation ι (j : J) : objs j ⟶ wide_pushout _ _ arrows :=
colimit.ι (wide_pushout_shape.wide_span _ _ _) (option.some j)
/-- The unique map from the head to the pushout. -/
noncomputable
abbreviation head : B ⟶ wide_pushout B objs arrows :=
colimit.ι (wide_pushout_shape.wide_span _ _ _) option.none
@[simp, reassoc]
lemma arrow_ι (j : J) : arrows j ≫ ι arrows j = head arrows :=
by apply colimit.w (wide_pushout_shape.wide_span _ _ _) (wide_pushout_shape.hom.init j)
variables {arrows}
/-- Descend a collection of morphisms to a morphism from the pushout. -/
noncomputable
abbreviation desc {X : C} (f : B ⟶ X) (fs : Π (j : J), objs j ⟶ X)
(w : ∀ j, arrows j ≫ fs j = f) : wide_pushout _ _ arrows ⟶ X :=
colimit.desc (wide_pushout_shape.wide_span B objs arrows)
(wide_pushout_shape.mk_cocone f fs $ by exact w)
variables (arrows)
variables {X : C} (f : B ⟶ X) (fs : Π (j : J), objs j ⟶ X)
(w : ∀ j, arrows j ≫ fs j = f)
@[simp, reassoc]
lemma ι_desc (j : J) : ι arrows j ≫ desc f fs w = fs _ :=
by { simp, refl }
@[simp, reassoc]
lemma head_desc : head arrows ≫ desc f fs w = f :=
by { simp, refl }
lemma eq_desc_of_comp_eq (g : wide_pushout _ _ arrows ⟶ X) :
(∀ j : J, ι arrows j ≫ g = fs j) → head arrows ≫ g = f → g = desc f fs w :=
begin
intros h1 h2,
apply (colimit.is_colimit (wide_pushout_shape.wide_span B objs arrows)).uniq
(wide_pushout_shape.mk_cocone f fs $ by exact w),
rintro (_|_),
{ apply h2 },
{ apply h1 }
end
lemma hom_eq_desc (g : wide_pushout _ _ arrows ⟶ X) :
g = desc (head arrows ≫ g) (λ j, ι arrows j ≫ g) (λ j, by { rw ← category.assoc, simp }) :=
begin
apply eq_desc_of_comp_eq,
tidy,
end
@[ext]
lemma hom_ext (g1 g2 : wide_pushout _ _ arrows ⟶ X) :
(∀ j : J, ι arrows j ≫ g1 = ι arrows j ≫ g2) →
head arrows ≫ g1 = head arrows ≫ g2 → g1 = g2 :=
begin
intros h1 h2,
apply colimit.hom_ext,
rintros (_|_),
{ apply h2 },
{ apply h1 },
end
end wide_pushout
end category_theory.limits
|
49846e09b5489cc384ffcc9a7d5861a2b690798f | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /tests/playground/lowtech_expander.lean | 78e2005e20dd647fa098ad85082b78ddfca291d4 | [
"Apache-2.0"
] | permissive | mhuisi/lean4 | 28d35a4febc2e251c7f05492e13f3b05d6f9b7af | dda44bc47f3e5d024508060dac2bcb59fd12e4c0 | refs/heads/master | 1,621,225,489,283 | 1,585,142,689,000 | 1,585,142,689,000 | 250,590,438 | 0 | 2 | Apache-2.0 | 1,602,443,220,000 | 1,585,327,814,000 | C | UTF-8 | Lean | false | false | 10,557 | lean | import init.lean.name
open Lean (Name NameMap)
def MacroScope := Nat
abbrev MacroScopes := List MacroScope
structure SourceInfo :=
(leading : Substring)
(pos : Nat)
(trailing : Substring)
def mkUniqIdRef : IO (IO.Ref Nat) :=
IO.mkRef 0
@[init mkUniqIdRef]
constant nextUniqId : IO.Ref Nat := default _
structure SyntaxNodeKind :=
(name : Name) (id : Nat)
instance : Inhabited SyntaxNodeKind :=
⟨{name := default _, id := default _}⟩
instance : HasBeq SyntaxNodeKind :=
⟨λ k₁ k₂, k₁.id == k₂.id⟩
def mkNameToKindTable : IO (IO.Ref (NameMap Nat)) :=
IO.mkRef {}
@[init mkNameToKindTable]
constant nameToKindTable : IO.Ref (NameMap Nat) := default _
def nextKind (k : Name) : IO SyntaxNodeKind :=
do m ← nameToKindTable.get,
when (m.contains k) (throw $ IO.userError ("Error kind '" ++ toString k ++ "' already exists")),
id ← nextUniqId.get,
nameToKindTable.set (m.insert k id),
nextUniqId.set (id+1),
pure { name := k, id := id }
def mkNullKind : IO SyntaxNodeKind := nextKind `null
@[init mkNullKind] constant nullKind : SyntaxNodeKind := default _
inductive Syntax
| missing
| node (kind : SyntaxNodeKind) (args : Array Syntax) (scopes : MacroScopes)
| atom (info : Option SourceInfo) (val : String)
| ident (info : Option SourceInfo) (rawVal : Substring) (val : Name) (preresolved : List Name) (scopes : MacroScopes)
instance : Inhabited Syntax :=
⟨Syntax.missing⟩
def SyntaxNodeKind.fix : SyntaxNodeKind → IO SyntaxNodeKind
| {name := n, ..} := do
m ← nameToKindTable.get,
match m.find n with
| some id := pure {name := n, id := id}
| none := throw $ IO.userError ("Error unknown Syntax kind '" ++ toString n ++ "'")
partial def Syntax.fixKinds : Syntax → IO Syntax
| (Syntax.node k args scopes) := do
k ← k.fix,
args ← args.mmap Syntax.fixKinds,
pure (Syntax.node k args scopes)
| other := pure other
inductive IsNode : Syntax → Prop
| mk (kind : SyntaxNodeKind) (args : Array Syntax) (scopes : MacroScopes) : IsNode (Syntax.node kind args scopes)
def SyntaxNode : Type := {s : Syntax // IsNode s }
def notIsNodeMissing (h : IsNode Syntax.missing) : False := match h with end
def notIsNodeAtom {info val} (h : IsNode (Syntax.atom info val)) : False := match h with end
def notIsNodeIdent {info rawVal val preresolved scopes} (h : IsNode (Syntax.ident info rawVal val preresolved scopes)) : False := match h with end
def unreachIsNodeMissing {α : Type} (h : IsNode Syntax.missing) : α := False.elim (notIsNodeMissing h)
def unreachIsNodeAtom {α : Type} {info val} (h : IsNode (Syntax.atom info val)) : α := False.elim (notIsNodeAtom h)
def unreachIsNodeIdent {α : Type} {info rawVal val preresolved scopes} (h : IsNode (Syntax.ident info rawVal val preresolved scopes)) : α := False.elim (match h with end)
@[inline] def toSyntaxNode {α : Type} (s : Syntax) (base : α) (fn : SyntaxNode → α) : α :=
match s with
| Syntax.node kind args scopes := fn ⟨Syntax.node kind args scopes, IsNode.mk kind args scopes⟩
| other := base
@[inline] def toSyntaxNodeOf {α : Type} (kind : SyntaxNodeKind) (s : Syntax) (base : α) (fn : SyntaxNode → α) : α :=
match s with
| Syntax.node k args scopes :=
if k == kind then fn ⟨Syntax.node kind args scopes, IsNode.mk kind args scopes⟩
else base
| other := base
@[inline] def mkAtom (val : String) : Syntax :=
Syntax.atom none val
def mkOptionSomeKind : IO SyntaxNodeKind := nextKind `some
@[init mkOptionSomeKind] constant optionSomeKind : SyntaxNodeKind := default _
def mkOptionNoneKind : IO SyntaxNodeKind := nextKind `none
@[init mkOptionSomeKind] constant optionNoneKind : SyntaxNodeKind := default _
def mkManyKind : IO SyntaxNodeKind := nextKind `many
@[init mkManyKind] constant manyKind : SyntaxNodeKind := default _
def mkHoleKind : IO SyntaxNodeKind := nextKind `hole
@[init mkHoleKind] constant holeKind : SyntaxNodeKind := default _
def mkNotKind : IO SyntaxNodeKind := nextKind `not
@[init mkNotKind] constant notKind : SyntaxNodeKind := default _
def mkIfKind : IO SyntaxNodeKind := nextKind `if
@[init mkIfKind] constant ifKind : SyntaxNodeKind := default _
def mkLetKind : IO SyntaxNodeKind := nextKind `let
@[init mkLetKind] constant letKind : SyntaxNodeKind := default _
def mkLetLhsIdKind : IO SyntaxNodeKind := nextKind `letLhsId
@[init mkLetLhsIdKind] constant letLhsIdKind : SyntaxNodeKind := default _
def mkLetLhsPatternKind : IO SyntaxNodeKind := nextKind `letLhsPattern
@[init mkLetLhsPatternKind] constant letLhsPatternKind : SyntaxNodeKind := default _
@[inline] def withArgs {α : Type} (n : SyntaxNode) (fn : Array Syntax → α) : α :=
match n with
| ⟨Syntax.node _ args _, _⟩ := fn args
| ⟨Syntax.missing, h⟩ := unreachIsNodeMissing h
| ⟨Syntax.atom _ _, h⟩ := unreachIsNodeAtom h
| ⟨Syntax.ident _ _ _ _ _, h⟩ := unreachIsNodeIdent h
@[inline] def updateArgs (n : SyntaxNode) (fn : Array Syntax → Array Syntax) : Syntax :=
match n with
| ⟨Syntax.node kind args scopes, _⟩ := Syntax.node kind (fn args) scopes
| ⟨Syntax.missing, h⟩ := unreachIsNodeMissing h
| ⟨Syntax.atom _ _, h⟩ := unreachIsNodeAtom h
| ⟨Syntax.ident _ _ _ _ _, h⟩ := unreachIsNodeIdent h
@[inline] def mkNotAux (tk : Syntax) (c : Syntax) : Syntax :=
Syntax.node notKind [tk, c].toArray []
@[inline] def mkNot (c : Syntax) : Syntax :=
mkNotAux (mkAtom "not") c
@[inline] def withNot {α : Type} (n : SyntaxNode) (fn : Syntax → α) : α :=
withArgs n $ λ args, fn (args.get 1)
@[inline] def updateNot (src : SyntaxNode) (c : Syntax) : Syntax :=
updateArgs src $ λ args, args.set 1 c
@[inline] def mkIfAux (ifTk : Syntax) (condNode : Syntax) (thenTk : Syntax) (thenNode : Syntax) (elseTk : Syntax) (elseNode: Syntax) : Syntax :=
Syntax.node ifKind [ifTk, condNode, thenTk, thenNode, elseTk, elseNode].toArray []
@[inline] def mkIf (c t e : Syntax) : Syntax :=
mkIfAux (mkAtom "if") c (mkAtom "then") t (mkAtom "else") e
@[inline] def withIf {α : Type} (n : SyntaxNode) (fn : Syntax → Syntax → Syntax → α) : α :=
withArgs n $ λ args, fn (args.get 1) (args.get 3) (args.get 5)
@[inline] def updateIf (src : SyntaxNode) (c t e : Syntax) : Syntax :=
updateArgs src $ λ args,
let args := args.set 1 c in
let args := args.set 3 t in
let args := args.set 5 e in
args
@[inline] def mkLetAux (letTk : Syntax) (lhs : Syntax) (assignTk : Syntax) (val : Syntax) (inTk : Syntax) (body : Syntax) : Syntax :=
Syntax.node letKind [letTk, lhs, assignTk, val, inTk, body].toArray []
@[inline] def mkLet (lhs : Syntax) (val : Syntax) (body : Syntax) : Syntax :=
mkLetAux (mkAtom "let") lhs (mkAtom ":=") val (mkAtom "in") body
@[inline] def withLet {α : Type} (n : SyntaxNode) (fn : Syntax → Syntax → Syntax → α) : α :=
withArgs n $ λ args, fn (args.get 1) (args.get 3) (args.get 5)
@[inline] def updateLet (src : SyntaxNode) (lhs val body : Syntax) : Syntax :=
updateArgs src $ λ args,
let args := args.set 1 lhs in
let args := args.set 3 val in
let args := args.set 5 body in
args
@[inline] def mkLetLhsId (id : Syntax) (binders : Syntax) (type : Syntax) : Syntax :=
Syntax.node letLhsIdKind [id, binders, type].toArray []
@[inline] def withLetLhsId {α : Type} (n : SyntaxNode) (fn : Syntax → Syntax → Syntax → α) : α :=
withArgs n $ λ args, fn (args.get 0) (args.get 1) (args.get 2)
@[inline] def updateLhsId (src : SyntaxNode) (id binders type : Syntax) : Syntax :=
updateArgs src $ λ args,
let args := args.set 0 id in
let args := args.set 1 binders in
let args := args.set 2 type in
args
@[inline] def mkLetLhsPattern (pattern : Syntax) : Syntax :=
Syntax.node letLhsPatternKind [pattern].toArray []
@[inline] def withLetLhsPattern {α : Type} (n : SyntaxNode) (fn : Syntax → α) : α :=
withArgs n $ λ args, fn (args.get 0)
@[inline] def withOptionSome {α : Type} (n : SyntaxNode) (fn : Syntax → α) : α :=
withArgs n $ λ args, fn (args.get 0)
def Syntax.getNumChildren (n : Syntax) : Nat :=
match n with
| Syntax.node _ args _ := args.size
| _ := 0
def hole : Syntax := Syntax.node holeKind ∅ []
def mkOptionSome (s : Syntax) := Syntax.node optionSomeKind [s].toArray []
abbrev FrontendConfig := Bool -- placeholder
abbrev Message := String -- placeholder
abbrev TransformM := ReaderT FrontendConfig $ ExceptT Message Id
abbrev Transformer := SyntaxNode → TransformM (Option Syntax)
def noExpansion : TransformM (Option Syntax) := pure none
@[inline] def Syntax.case {α : Type} (n : Syntax) (k : SyntaxNodeKind) (fn : SyntaxNode → TransformM (Option α)) : TransformM (Option α) :=
match n with
| Syntax.node k' args s := if k == k' then fn ⟨Syntax.node k' args s, IsNode.mk _ _ _⟩ else pure none
| _ := pure none
@[inline] def TransformM.orCase {α : Type} (x y : TransformM (Option α)) : TransformM (Option α) :=
λ cfg, match x cfg with
| Except.ok none := y cfg
| other := other
infix `<?>`:2 := TransformM.orCase
set_option pp.implicit true
set_option trace.compiler.boxed true
def flipIf : Transformer :=
λ n, withIf n $ λ c t e,
c.case notKind $ λ c, withNot c $ λ c',
pure $ updateIf n c' e t
def letTransformer : Transformer :=
λ n, withLet n $ λ lhs val body,
(lhs.case letLhsIdKind $ λ lhs, withLetLhsId lhs $ λ id binders type,
if binders.getNumChildren == 0 then
type.case optionNoneKind $ λ _,
let newLhs := updateLhsId lhs id binders (mkOptionSome hole) in
pure (some (updateLet n newLhs val body))
else
-- TODO
noExpansion)
<?>
(lhs.case letLhsPatternKind $ λ lhs,
-- TODO
noExpansion)
@[inline] def Syntax.isNode {α : Type} (n : Syntax) (fn : SyntaxNodeKind → SyntaxNode → TransformM (Option α)) : TransformM (Option α) :=
match n with
| Syntax.node k args s := fn k ⟨Syntax.node k args s, IsNode.mk _ _ _⟩
| other := pure none
/- Version without using the combinator <?>. -/
def letTransformer' : Transformer :=
λ n, withLet n $ λ lhs val body,
lhs.isNode $ λ k lhs, -- lhs is now a SyntaxNode
if k == letLhsIdKind then withLetLhsId lhs $ λ id binders type,
if binders.getNumChildren == 0 then
type.case optionNoneKind $ λ _,
let newLhs := updateLhsId lhs id binders (mkOptionSome hole) in
pure (some (updateLet n newLhs val body))
else
-- TODO
noExpansion
else withLetLhsPattern lhs $ λ pattern,
-- TODO
noExpansion
|
e703392fb787e8ec82ff91b3d980e63e645a2a43 | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/analysis/normed/group/basic.lean | f893d68663ecf9dc05ca31d27311293620651c78 | [
"Apache-2.0"
] | permissive | ramonfmir/mathlib | c5dc8b33155473fab97c38bd3aa6723dc289beaa | 14c52e990c17f5a00c0cc9e09847af16fabbed25 | refs/heads/master | 1,661,979,343,526 | 1,660,830,384,000 | 1,660,830,384,000 | 182,072,989 | 0 | 0 | null | 1,555,585,876,000 | 1,555,585,876,000 | null | UTF-8 | Lean | false | false | 52,646 | lean | /-
Copyright (c) 2018 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Johannes Hölzl
-/
import algebra.module.ulift
import order.liminf_limsup
import topology.algebra.uniform_group
import topology.metric_space.algebra
import topology.metric_space.isometry
import topology.sequences
/-!
# Normed (semi)groups
In this file we define four classes:
* `has_norm`, `has_nnnorm`: auxiliary classes endowing a type `α` with a function `norm : α → ℝ`
(notation: `∥x∥`) and `nnnorm : α → ℝ≥0` (notation: `∥x∥₊`), respectively;
* `seminormed_add_comm_group`: a seminormed group is an additive group with a norm and a
pseudo-metric space structures that agree with each other: `∀ x y, dist x y = ∥x - y∥`;
* `normed_add_comm_group`: a normed group is an additive group with a norm and a metric space
structures that agree with each other: `∀ x y, dist x y = ∥x - y∥`.
We also prove basic properties of (semi)normed groups and provide some instances.
## Tags
normed group
-/
variables {α ι E F G : Type*}
open filter metric
open_locale topological_space big_operators nnreal ennreal uniformity pointwise
/-- Auxiliary class, endowing a type `E` with a function `norm : E → ℝ` with notation `∥x∥`. This
class is designed to be extended in more interesting classes specifying the properties of the norm.
-/
class has_norm (E : Type*) := (norm : E → ℝ)
export has_norm (norm)
notation `∥` e `∥` := norm e
/-- A seminormed group is an additive group endowed with a norm for which `dist x y = ∥x - y∥`
defines a pseudometric space structure. -/
class seminormed_add_comm_group (E : Type*)
extends has_norm E, add_comm_group E, pseudo_metric_space E :=
(dist_eq : ∀ x y : E, dist x y = norm (x - y))
/-- A normed group is an additive group endowed with a norm for which `dist x y = ∥x - y∥` defines
a metric space structure. -/
class normed_add_comm_group (E : Type*) extends has_norm E, add_comm_group E, metric_space E :=
(dist_eq : ∀ x y : E, dist x y = norm (x - y))
/-- A normed group is a seminormed group. -/
@[priority 100] -- see Note [lower instance priority]
instance normed_add_comm_group.to_seminormed_add_comm_group [h : normed_add_comm_group E] :
seminormed_add_comm_group E :=
{ .. h }
/-- Construct a seminormed group from a translation invariant pseudodistance. -/
def seminormed_add_comm_group.of_add_dist [has_norm E] [add_comm_group E] [pseudo_metric_space E]
(H1 : ∀ x : E, ∥x∥ = dist x 0)
(H2 : ∀ x y z : E, dist x y ≤ dist (x + z) (y + z)) : seminormed_add_comm_group E :=
{ dist_eq := λ x y, begin
rw H1, apply le_antisymm,
{ rw [sub_eq_add_neg, ← add_right_neg y], apply H2 },
{ have := H2 (x - y) 0 y, rwa [sub_add_cancel, zero_add] at this }
end }
/-- Construct a seminormed group from a translation invariant pseudodistance -/
def seminormed_add_comm_group.of_add_dist' [has_norm E] [add_comm_group E] [pseudo_metric_space E]
(H1 : ∀ x : E, ∥x∥ = dist x 0)
(H2 : ∀ x y z : E, dist (x + z) (y + z) ≤ dist x y) : seminormed_add_comm_group E :=
{ dist_eq := λ x y, begin
rw H1, apply le_antisymm,
{ have := H2 (x - y) 0 y, rwa [sub_add_cancel, zero_add] at this },
{ rw [sub_eq_add_neg, ← add_right_neg y], apply H2 }
end }
/-- A seminormed group can be built from a seminorm that satisfies algebraic properties. This is
formalised in this structure. -/
structure seminormed_add_comm_group.core (E : Type*) [add_comm_group E] [has_norm E] : Prop :=
(norm_zero : ∥(0 : E)∥ = 0)
(triangle : ∀ x y : E, ∥x + y∥ ≤ ∥x∥ + ∥y∥)
(norm_neg : ∀ x : E, ∥-x∥ = ∥x∥)
/-- Constructing a seminormed group from core properties of a seminorm, i.e., registering the
pseudodistance and the pseudometric space structure from the seminorm properties. Note that in most
cases this instance creates bad definitional equalities (e.g., it does not take into account
a possibly existing `uniform_space` instance on `E`). -/
def seminormed_add_comm_group.of_core (E : Type*) [add_comm_group E] [has_norm E]
(C : seminormed_add_comm_group.core E) : seminormed_add_comm_group E :=
{ dist := λ x y, ∥x - y∥,
dist_eq := assume x y, by refl,
dist_self := assume x, by simp [C.norm_zero],
dist_triangle := assume x y z,
calc ∥x - z∥ = ∥x - y + (y - z)∥ : by rw sub_add_sub_cancel
... ≤ ∥x - y∥ + ∥y - z∥ : C.triangle _ _,
dist_comm := assume x y,
calc ∥x - y∥ = ∥ -(y - x)∥ : by simp
... = ∥y - x∥ : by { rw [C.norm_neg] } }
instance : normed_add_comm_group punit :=
{ norm := function.const _ 0,
dist_eq := λ _ _, rfl, }
@[simp] lemma punit.norm_eq_zero (r : punit) : ∥r∥ = 0 := rfl
noncomputable instance : normed_add_comm_group ℝ :=
{ norm := λ x, |x|,
dist_eq := assume x y, rfl }
@[simp] lemma real.norm_eq_abs (r : ℝ) : ∥r∥ = |r| := rfl
section seminormed_add_comm_group
variables [seminormed_add_comm_group E] [seminormed_add_comm_group F] [seminormed_add_comm_group G]
lemma dist_eq_norm (g h : E) : dist g h = ∥g - h∥ :=
seminormed_add_comm_group.dist_eq _ _
lemma dist_eq_norm' (g h : E) : dist g h = ∥h - g∥ :=
by rw [dist_comm, dist_eq_norm]
@[simp] lemma dist_zero_right (g : E) : dist g 0 = ∥g∥ :=
by rw [dist_eq_norm, sub_zero]
@[simp] lemma dist_zero_left : dist (0 : E) = norm :=
funext $ λ g, by rw [dist_comm, dist_zero_right]
lemma isometry.norm_map_of_map_zero {f : E → F} (hi : isometry f) (h0 : f 0 = 0) (x : E) :
∥f x∥ = ∥x∥ :=
by rw [←dist_zero_right, ←h0, hi.dist_eq, dist_zero_right]
lemma tendsto_norm_cocompact_at_top [proper_space E] :
tendsto norm (cocompact E) at_top :=
by simpa only [dist_zero_right] using tendsto_dist_right_cocompact_at_top (0 : E)
lemma norm_sub_rev (g h : E) : ∥g - h∥ = ∥h - g∥ :=
by simpa only [dist_eq_norm] using dist_comm g h
@[simp] lemma norm_neg (g : E) : ∥-g∥ = ∥g∥ :=
by simpa using norm_sub_rev 0 g
@[simp] lemma dist_add_left (g h₁ h₂ : E) : dist (g + h₁) (g + h₂) = dist h₁ h₂ :=
by simp [dist_eq_norm]
@[simp] lemma dist_add_right (g₁ g₂ h : E) : dist (g₁ + h) (g₂ + h) = dist g₁ g₂ :=
by simp [dist_eq_norm]
lemma dist_neg (x y : E) : dist (-x) y = dist x (-y) :=
by simp_rw [dist_eq_norm, ←norm_neg (-x - y), neg_sub, sub_neg_eq_add, add_comm]
@[simp] lemma dist_neg_neg (g h : E) : dist (-g) (-h) = dist g h := by rw [dist_neg, neg_neg]
@[simp] lemma dist_sub_left (g h₁ h₂ : E) : dist (g - h₁) (g - h₂) = dist h₁ h₂ :=
by simp only [sub_eq_add_neg, dist_add_left, dist_neg_neg]
@[simp] lemma dist_sub_right (g₁ g₂ h : E) : dist (g₁ - h) (g₂ - h) = dist g₁ g₂ :=
by simpa only [sub_eq_add_neg] using dist_add_right _ _ _
@[simp] theorem dist_self_add_right (g h : E) : dist g (g + h) = ∥h∥ :=
by rw [← dist_zero_left, ← dist_add_left g 0 h, add_zero]
@[simp] theorem dist_self_add_left (g h : E) : dist (g + h) g = ∥h∥ :=
by rw [dist_comm, dist_self_add_right]
@[simp] theorem dist_self_sub_right (g h : E) : dist g (g - h) = ∥h∥ :=
by rw [sub_eq_add_neg, dist_self_add_right, norm_neg]
@[simp] theorem dist_self_sub_left (g h : E) : dist (g - h) g = ∥h∥ :=
by rw [dist_comm, dist_self_sub_right]
/-- In a (semi)normed group, negation `x ↦ -x` tends to infinity at infinity. TODO: use
`bornology.cobounded` instead of `filter.comap has_norm.norm filter.at_top`. -/
lemma filter.tendsto_neg_cobounded :
tendsto (has_neg.neg : E → E) (comap norm at_top) (comap norm at_top) :=
by simpa only [norm_neg, tendsto_comap_iff, (∘)] using tendsto_comap
/-- **Triangle inequality** for the norm. -/
lemma norm_add_le (g h : E) : ∥g + h∥ ≤ ∥g∥ + ∥h∥ :=
by simpa [dist_eq_norm] using dist_triangle g 0 (-h)
lemma norm_add_le_of_le {g₁ g₂ : E} {n₁ n₂ : ℝ} (H₁ : ∥g₁∥ ≤ n₁) (H₂ : ∥g₂∥ ≤ n₂) :
∥g₁ + g₂∥ ≤ n₁ + n₂ :=
le_trans (norm_add_le g₁ g₂) (add_le_add H₁ H₂)
lemma norm_add₃_le (x y z : E) : ∥x + y + z∥ ≤ ∥x∥ + ∥y∥ + ∥z∥ :=
norm_add_le_of_le (norm_add_le _ _) le_rfl
lemma dist_add_add_le (g₁ g₂ h₁ h₂ : E) :
dist (g₁ + g₂) (h₁ + h₂) ≤ dist g₁ h₁ + dist g₂ h₂ :=
by simpa only [dist_add_left, dist_add_right] using dist_triangle (g₁ + g₂) (h₁ + g₂) (h₁ + h₂)
lemma dist_add_add_le_of_le {g₁ g₂ h₁ h₂ : E} {d₁ d₂ : ℝ}
(H₁ : dist g₁ h₁ ≤ d₁) (H₂ : dist g₂ h₂ ≤ d₂) :
dist (g₁ + g₂) (h₁ + h₂) ≤ d₁ + d₂ :=
le_trans (dist_add_add_le g₁ g₂ h₁ h₂) (add_le_add H₁ H₂)
lemma dist_sub_sub_le (g₁ g₂ h₁ h₂ : E) :
dist (g₁ - g₂) (h₁ - h₂) ≤ dist g₁ h₁ + dist g₂ h₂ :=
by simpa only [sub_eq_add_neg, dist_neg_neg] using dist_add_add_le g₁ (-g₂) h₁ (-h₂)
lemma dist_sub_sub_le_of_le {g₁ g₂ h₁ h₂ : E} {d₁ d₂ : ℝ}
(H₁ : dist g₁ h₁ ≤ d₁) (H₂ : dist g₂ h₂ ≤ d₂) :
dist (g₁ - g₂) (h₁ - h₂) ≤ d₁ + d₂ :=
le_trans (dist_sub_sub_le g₁ g₂ h₁ h₂) (add_le_add H₁ H₂)
lemma abs_dist_sub_le_dist_add_add (g₁ g₂ h₁ h₂ : E) :
|dist g₁ h₁ - dist g₂ h₂| ≤ dist (g₁ + g₂) (h₁ + h₂) :=
by simpa only [dist_add_left, dist_add_right, dist_comm h₂]
using abs_dist_sub_le (g₁ + g₂) (h₁ + h₂) (h₁ + g₂)
@[simp] lemma norm_nonneg (g : E) : 0 ≤ ∥g∥ :=
by { rw[←dist_zero_right], exact dist_nonneg }
section
open tactic tactic.positivity
/-- Extension for the `positivity` tactic: norms are nonnegative. -/
@[positivity]
meta def _root_.tactic.positivity_norm : expr → tactic strictness
| `(∥%%a∥) := nonnegative <$> mk_app ``norm_nonneg [a]
| _ := failed
end
@[simp] lemma norm_zero : ∥(0 : E)∥ = 0 := by rw [← dist_zero_right, dist_self]
lemma ne_zero_of_norm_ne_zero {g : E} : ∥g∥ ≠ 0 → g ≠ 0 := mt $ by { rintro rfl, exact norm_zero }
@[nontriviality] lemma norm_of_subsingleton [subsingleton E] (x : E) : ∥x∥ = 0 :=
by rw [subsingleton.elim x 0, norm_zero]
lemma norm_sum_le (s : finset ι) (f : ι → E) : ∥∑ i in s, f i∥ ≤ ∑ i in s, ∥f i∥ :=
s.le_sum_of_subadditive norm norm_zero norm_add_le f
lemma norm_sum_le_of_le (s : finset ι) {f : ι → E} {n : ι → ℝ} (h : ∀ b ∈ s, ∥f b∥ ≤ n b) :
∥∑ b in s, f b∥ ≤ ∑ b in s, n b :=
le_trans (norm_sum_le s f) (finset.sum_le_sum h)
lemma dist_sum_sum_le_of_le (s : finset ι) {f g : ι → E} {d : ι → ℝ}
(h : ∀ b ∈ s, dist (f b) (g b) ≤ d b) :
dist (∑ b in s, f b) (∑ b in s, g b) ≤ ∑ b in s, d b :=
begin
simp only [dist_eq_norm, ← finset.sum_sub_distrib] at *,
exact norm_sum_le_of_le s h
end
lemma dist_sum_sum_le (s : finset ι) (f g : ι → E) :
dist (∑ b in s, f b) (∑ b in s, g b) ≤ ∑ b in s, dist (f b) (g b) :=
dist_sum_sum_le_of_le s (λ _ _, le_rfl)
lemma norm_sub_le (g h : E) : ∥g - h∥ ≤ ∥g∥ + ∥h∥ :=
by simpa [dist_eq_norm] using dist_triangle g 0 h
lemma norm_sub_le_of_le {g₁ g₂ : E} {n₁ n₂ : ℝ} (H₁ : ∥g₁∥ ≤ n₁) (H₂ : ∥g₂∥ ≤ n₂) :
∥g₁ - g₂∥ ≤ n₁ + n₂ :=
le_trans (norm_sub_le g₁ g₂) (add_le_add H₁ H₂)
lemma dist_le_norm_add_norm (g h : E) : dist g h ≤ ∥g∥ + ∥h∥ :=
by { rw dist_eq_norm, apply norm_sub_le }
lemma abs_norm_sub_norm_le (g h : E) : |∥g∥ - ∥h∥| ≤ ∥g - h∥ :=
by simpa [dist_eq_norm] using abs_dist_sub_le g h 0
lemma norm_sub_norm_le (g h : E) : ∥g∥ - ∥h∥ ≤ ∥g - h∥ :=
le_trans (le_abs_self _) (abs_norm_sub_norm_le g h)
lemma dist_norm_norm_le (g h : E) : dist ∥g∥ ∥h∥ ≤ ∥g - h∥ :=
abs_norm_sub_norm_le g h
/-- The direct path from `0` to `v` is shorter than the path with `u` inserted in between. -/
lemma norm_le_insert (u v : E) : ∥v∥ ≤ ∥u∥ + ∥u - v∥ :=
calc ∥v∥ = ∥u - (u - v)∥ : by abel
... ≤ ∥u∥ + ∥u - v∥ : norm_sub_le u _
lemma norm_le_insert' (u v : E) : ∥u∥ ≤ ∥v∥ + ∥u - v∥ :=
by { rw norm_sub_rev, exact norm_le_insert v u }
lemma norm_le_add_norm_add (u v : E) :
∥u∥ ≤ ∥u + v∥ + ∥v∥ :=
calc ∥u∥ = ∥u + v - v∥ : by rw add_sub_cancel
... ≤ ∥u + v∥ + ∥v∥ : norm_sub_le _ _
lemma ball_eq (y : E) (ε : ℝ) : metric.ball y ε = { x | ∥x - y∥ < ε} :=
by { ext, simp [dist_eq_norm], }
lemma ball_zero_eq (ε : ℝ) : ball (0 : E) ε = {x | ∥x∥ < ε} :=
set.ext $ assume a, by simp
lemma mem_ball_iff_norm {g h : E} {r : ℝ} :
h ∈ ball g r ↔ ∥h - g∥ < r :=
by rw [mem_ball, dist_eq_norm]
lemma add_mem_ball_iff_norm {g h : E} {r : ℝ} :
g + h ∈ ball g r ↔ ∥h∥ < r :=
by rw [mem_ball_iff_norm, add_sub_cancel']
lemma mem_ball_iff_norm' {g h : E} {r : ℝ} :
h ∈ ball g r ↔ ∥g - h∥ < r :=
by rw [mem_ball', dist_eq_norm]
@[simp] lemma mem_ball_zero_iff {ε : ℝ} {x : E} : x ∈ ball (0 : E) ε ↔ ∥x∥ < ε :=
by rw [mem_ball, dist_zero_right]
lemma mem_closed_ball_iff_norm {g h : E} {r : ℝ} :
h ∈ closed_ball g r ↔ ∥h - g∥ ≤ r :=
by rw [mem_closed_ball, dist_eq_norm]
@[simp] lemma mem_closed_ball_zero_iff {ε : ℝ} {x : E} : x ∈ closed_ball (0 : E) ε ↔ ∥x∥ ≤ ε :=
by rw [mem_closed_ball, dist_zero_right]
lemma add_mem_closed_ball_iff_norm {g h : E} {r : ℝ} :
g + h ∈ closed_ball g r ↔ ∥h∥ ≤ r :=
by rw [mem_closed_ball_iff_norm, add_sub_cancel']
lemma mem_closed_ball_iff_norm' {g h : E} {r : ℝ} :
h ∈ closed_ball g r ↔ ∥g - h∥ ≤ r :=
by rw [mem_closed_ball', dist_eq_norm]
lemma norm_le_of_mem_closed_ball {g h : E} {r : ℝ} (H : h ∈ closed_ball g r) :
∥h∥ ≤ ∥g∥ + r :=
calc
∥h∥ = ∥g + (h - g)∥ : by rw [add_sub_cancel'_right]
... ≤ ∥g∥ + ∥h - g∥ : norm_add_le _ _
... ≤ ∥g∥ + r : by { apply add_le_add_left, rw ← dist_eq_norm, exact H }
lemma norm_le_norm_add_const_of_dist_le {a b : E} {c : ℝ} (h : dist a b ≤ c) :
∥a∥ ≤ ∥b∥ + c :=
norm_le_of_mem_closed_ball h
lemma norm_lt_of_mem_ball {g h : E} {r : ℝ} (H : h ∈ ball g r) :
∥h∥ < ∥g∥ + r :=
calc
∥h∥ = ∥g + (h - g)∥ : by rw [add_sub_cancel'_right]
... ≤ ∥g∥ + ∥h - g∥ : norm_add_le _ _
... < ∥g∥ + r : by { apply add_lt_add_left, rw ← dist_eq_norm, exact H }
lemma norm_lt_norm_add_const_of_dist_lt {a b : E} {c : ℝ} (h : dist a b < c) :
∥a∥ < ∥b∥ + c :=
norm_lt_of_mem_ball h
lemma bounded_iff_forall_norm_le {s : set E} : bounded s ↔ ∃ C, ∀ x ∈ s, ∥x∥ ≤ C :=
by simpa only [set.subset_def, mem_closed_ball_iff_norm, sub_zero]
using bounded_iff_subset_ball (0 : E)
@[simp] lemma preimage_add_ball (x y : E) (r : ℝ) : ((+) y) ⁻¹' (ball x r) = ball (x - y) r :=
begin
ext z,
simp only [dist_eq_norm, set.mem_preimage, mem_ball],
abel
end
@[simp] lemma preimage_add_closed_ball (x y : E) (r : ℝ) :
((+) y) ⁻¹' (closed_ball x r) = closed_ball (x - y) r :=
begin
ext z,
simp only [dist_eq_norm, set.mem_preimage, mem_closed_ball],
abel
end
@[simp] lemma mem_sphere_iff_norm (v w : E) (r : ℝ) : w ∈ sphere v r ↔ ∥w - v∥ = r :=
by simp [dist_eq_norm]
@[simp] lemma mem_sphere_zero_iff_norm {w : E} {r : ℝ} : w ∈ sphere (0:E) r ↔ ∥w∥ = r :=
by simp [dist_eq_norm]
@[simp] lemma norm_eq_of_mem_sphere {r : ℝ} (x : sphere (0:E) r) : ∥(x:E)∥ = r :=
mem_sphere_zero_iff_norm.mp x.2
lemma preimage_add_sphere (x y : E) (r : ℝ) :
((+) y) ⁻¹' (sphere x r) = sphere (x - y) r :=
begin
ext z,
simp only [set.mem_preimage, mem_sphere_iff_norm],
abel
end
lemma ne_zero_of_mem_sphere {r : ℝ} (hr : r ≠ 0) (x : sphere (0 : E) r) : (x : E) ≠ 0 :=
ne_zero_of_norm_ne_zero $ by rwa norm_eq_of_mem_sphere x
lemma ne_zero_of_mem_unit_sphere (x : sphere (0:E) 1) : (x:E) ≠ 0 :=
ne_zero_of_mem_sphere one_ne_zero _
namespace isometric
-- TODO This material is superseded by similar constructions such as
-- `affine_isometry_equiv.const_vadd`; deduplicate
/-- Addition `y ↦ y + x` as an `isometry`. -/
protected def add_right (x : E) : E ≃ᵢ E :=
{ isometry_to_fun := isometry.of_dist_eq $ λ y z, dist_add_right _ _ _,
.. equiv.add_right x }
@[simp] lemma add_right_to_equiv (x : E) :
(isometric.add_right x).to_equiv = equiv.add_right x := rfl
@[simp] lemma coe_add_right (x : E) : (isometric.add_right x : E → E) = λ y, y + x := rfl
lemma add_right_apply (x y : E) : (isometric.add_right x : E → E) y = y + x := rfl
@[simp] lemma add_right_symm (x : E) :
(isometric.add_right x).symm = isometric.add_right (-x) :=
ext $ λ y, rfl
/-- Addition `y ↦ x + y` as an `isometry`. -/
protected def add_left (x : E) : E ≃ᵢ E :=
{ isometry_to_fun := isometry.of_dist_eq $ λ y z, dist_add_left _ _ _,
to_equiv := equiv.add_left x }
@[simp] lemma add_left_to_equiv (x : E) :
(isometric.add_left x).to_equiv = equiv.add_left x := rfl
@[simp] lemma coe_add_left (x : E) : ⇑(isometric.add_left x) = (+) x := rfl
@[simp] lemma add_left_symm (x : E) :
(isometric.add_left x).symm = isometric.add_left (-x) :=
ext $ λ y, rfl
variable (E)
/-- Negation `x ↦ -x` as an `isometry`. -/
protected def neg : E ≃ᵢ E :=
{ isometry_to_fun := isometry.of_dist_eq $ λ x y, dist_neg_neg _ _,
to_equiv := equiv.neg E }
variable {E}
@[simp] lemma neg_symm : (isometric.neg E).symm = isometric.neg E := rfl
@[simp] lemma neg_to_equiv : (isometric.neg E).to_equiv = equiv.neg E := rfl
@[simp] lemma coe_neg : ⇑(isometric.neg E) = has_neg.neg := rfl
end isometric
theorem normed_add_comm_group.tendsto_nhds_zero {f : α → E} {l : filter α} :
tendsto f l (𝓝 0) ↔ ∀ ε > 0, ∀ᶠ x in l, ∥ f x ∥ < ε :=
metric.tendsto_nhds.trans $ by simp only [dist_zero_right]
lemma normed_add_comm_group.tendsto_nhds_nhds {f : E → F} {x : E} {y : F} :
tendsto f (𝓝 x) (𝓝 y) ↔ ∀ ε > 0, ∃ δ > 0, ∀ x', ∥x' - x∥ < δ → ∥f x' - y∥ < ε :=
by simp_rw [metric.tendsto_nhds_nhds, dist_eq_norm]
lemma normed_add_comm_group.cauchy_seq_iff [nonempty α] [semilattice_sup α] {u : α → E} :
cauchy_seq u ↔ ∀ ε > 0, ∃ N, ∀ m, N ≤ m → ∀ n, N ≤ n → ∥u m - u n∥ < ε :=
by simp [metric.cauchy_seq_iff, dist_eq_norm]
lemma normed_add_comm_group.nhds_basis_norm_lt (x : E) :
(𝓝 x).has_basis (λ (ε : ℝ), 0 < ε) (λ (ε : ℝ), { y | ∥y - x∥ < ε }) :=
begin
simp_rw ← ball_eq,
exact metric.nhds_basis_ball,
end
lemma normed_add_comm_group.nhds_zero_basis_norm_lt :
(𝓝 (0 : E)).has_basis (λ (ε : ℝ), 0 < ε) (λ (ε : ℝ), { y | ∥y∥ < ε }) :=
begin
convert normed_add_comm_group.nhds_basis_norm_lt (0 : E),
simp,
end
lemma normed_add_comm_group.uniformity_basis_dist :
(𝓤 E).has_basis (λ (ε : ℝ), 0 < ε) (λ ε, {p : E × E | ∥p.fst - p.snd∥ < ε}) :=
begin
convert metric.uniformity_basis_dist,
simp [dist_eq_norm]
end
open finset
/-- A homomorphism `f` of seminormed groups is Lipschitz, if there exists a constant `C` such that
for all `x`, one has `∥f x∥ ≤ C * ∥x∥`. The analogous condition for a linear map of
(semi)normed spaces is in `normed_space.operator_norm`. -/
lemma add_monoid_hom_class.lipschitz_of_bound {𝓕 : Type*} [add_monoid_hom_class 𝓕 E F] (f : 𝓕)
(C : ℝ) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) : lipschitz_with (real.to_nnreal C) f :=
lipschitz_with.of_dist_le' $ λ x y, by simpa only [dist_eq_norm, map_sub] using h (x - y)
lemma lipschitz_on_with_iff_norm_sub_le {f : E → F} {C : ℝ≥0} {s : set E} :
lipschitz_on_with C f s ↔ ∀ (x ∈ s) (y ∈ s), ∥f x - f y∥ ≤ C * ∥x - y∥ :=
by simp only [lipschitz_on_with_iff_dist_le_mul, dist_eq_norm]
lemma lipschitz_on_with.norm_sub_le {f : E → F} {C : ℝ≥0} {s : set E} (h : lipschitz_on_with C f s)
{x y : E} (x_in : x ∈ s) (y_in : y ∈ s) : ∥f x - f y∥ ≤ C * ∥x - y∥ :=
lipschitz_on_with_iff_norm_sub_le.mp h x x_in y y_in
lemma lipschitz_on_with.norm_sub_le_of_le {f : E → F} {C : ℝ≥0} {s : set E}
(h : lipschitz_on_with C f s){x y : E} (x_in : x ∈ s) (y_in : y ∈ s) {d : ℝ} (hd : ∥x - y∥ ≤ d) :
∥f x - f y∥ ≤ C * d :=
(h.norm_sub_le x_in y_in).trans $ mul_le_mul_of_nonneg_left hd C.2
lemma lipschitz_with_iff_norm_sub_le {f : E → F} {C : ℝ≥0} :
lipschitz_with C f ↔ ∀ x y, ∥f x - f y∥ ≤ C * ∥x - y∥ :=
by simp only [lipschitz_with_iff_dist_le_mul, dist_eq_norm]
alias lipschitz_with_iff_norm_sub_le ↔ lipschitz_with.norm_sub_le _
lemma lipschitz_with.norm_sub_le_of_le {f : E → F} {C : ℝ≥0} (h : lipschitz_with C f)
{x y : E} {d : ℝ} (hd : ∥x - y∥ ≤ d) :
∥f x - f y∥ ≤ C * d :=
(h.norm_sub_le x y).trans $ mul_le_mul_of_nonneg_left hd C.2
/-- A homomorphism `f` of seminormed groups is continuous, if there exists a constant `C` such that
for all `x`, one has `∥f x∥ ≤ C * ∥x∥`. -/
lemma add_monoid_hom_class.continuous_of_bound {𝓕 : Type*} [add_monoid_hom_class 𝓕 E F] (f : 𝓕)
(C : ℝ) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) : continuous f :=
(add_monoid_hom_class.lipschitz_of_bound f C h).continuous
lemma add_monoid_hom_class.uniform_continuous_of_bound {𝓕 : Type*} [add_monoid_hom_class 𝓕 E F]
(f : 𝓕) (C : ℝ) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) : uniform_continuous f :=
(add_monoid_hom_class.lipschitz_of_bound f C h).uniform_continuous
lemma is_compact.exists_bound_of_continuous_on [topological_space α]
{s : set α} (hs : is_compact s) {f : α → E} (hf : continuous_on f s) :
∃ C, ∀ x ∈ s, ∥f x∥ ≤ C :=
begin
have : bounded (f '' s) := (hs.image_of_continuous_on hf).bounded,
rcases bounded_iff_forall_norm_le.1 this with ⟨C, hC⟩,
exact ⟨C, λ x hx, hC _ (set.mem_image_of_mem _ hx)⟩,
end
lemma add_monoid_hom_class.isometry_iff_norm {𝓕 : Type*} [add_monoid_hom_class 𝓕 E F]
(f : 𝓕) : isometry f ↔ ∀ x, ∥f x∥ = ∥x∥ :=
begin
simp only [isometry_iff_dist_eq, dist_eq_norm, ←map_sub],
refine ⟨λ h x, _, λ h x y, h _⟩,
simpa using h x 0
end
lemma add_monoid_hom_class.isometry_of_norm {𝓕 : Type*} [add_monoid_hom_class 𝓕 E F]
(f : 𝓕) (hf : ∀ x, ∥f x∥ = ∥x∥) : isometry f :=
(add_monoid_hom_class.isometry_iff_norm f).2 hf
lemma controlled_sum_of_mem_closure {s : add_subgroup E} {g : E}
(hg : g ∈ closure (s : set E)) {b : ℕ → ℝ} (b_pos : ∀ n, 0 < b n) :
∃ v : ℕ → E,
tendsto (λ n, ∑ i in range (n+1), v i) at_top (𝓝 g) ∧
(∀ n, v n ∈ s) ∧
∥v 0 - g∥ < b 0 ∧
∀ n > 0, ∥v n∥ < b n :=
begin
obtain ⟨u : ℕ → E, u_in : ∀ n, u n ∈ s, lim_u : tendsto u at_top (𝓝 g)⟩ :=
mem_closure_iff_seq_limit.mp hg,
obtain ⟨n₀, hn₀⟩ : ∃ n₀, ∀ n ≥ n₀, ∥u n - g∥ < b 0,
{ have : {x | ∥x - g∥ < b 0} ∈ 𝓝 g,
{ simp_rw ← dist_eq_norm,
exact metric.ball_mem_nhds _ (b_pos _) },
exact filter.tendsto_at_top'.mp lim_u _ this },
set z : ℕ → E := λ n, u (n + n₀),
have lim_z : tendsto z at_top (𝓝 g) := lim_u.comp (tendsto_add_at_top_nat n₀),
have mem_𝓤 : ∀ n, {p : E × E | ∥p.1 - p.2∥ < b (n + 1)} ∈ 𝓤 E :=
λ n, by simpa [← dist_eq_norm] using metric.dist_mem_uniformity (b_pos $ n+1),
obtain ⟨φ : ℕ → ℕ, φ_extr : strict_mono φ,
hφ : ∀ n, ∥z (φ $ n + 1) - z (φ n)∥ < b (n + 1)⟩ :=
lim_z.cauchy_seq.subseq_mem mem_𝓤,
set w : ℕ → E := z ∘ φ,
have hw : tendsto w at_top (𝓝 g),
from lim_z.comp φ_extr.tendsto_at_top,
set v : ℕ → E := λ i, if i = 0 then w 0 else w i - w (i - 1),
refine ⟨v, tendsto.congr (finset.eq_sum_range_sub' w) hw , _,
hn₀ _ (n₀.le_add_left _), _⟩,
{ rintro ⟨⟩,
{ change w 0 ∈ s,
apply u_in },
{ apply s.sub_mem ; apply u_in }, },
{ intros l hl,
obtain ⟨k, rfl⟩ : ∃ k, l = k+1, exact nat.exists_eq_succ_of_ne_zero (ne_of_gt hl),
apply hφ },
end
lemma controlled_sum_of_mem_closure_range {j : E →+ F} {h : F}
(Hh : h ∈ (closure $ (j.range : set F))) {b : ℕ → ℝ} (b_pos : ∀ n, 0 < b n) :
∃ g : ℕ → E,
tendsto (λ n, ∑ i in range (n+1), j (g i)) at_top (𝓝 h) ∧
∥j (g 0) - h∥ < b 0 ∧
∀ n > 0, ∥j (g n)∥ < b n :=
begin
rcases controlled_sum_of_mem_closure Hh b_pos with ⟨v, sum_v, v_in, hv₀, hv_pos⟩,
choose g hg using v_in,
change ∀ (n : ℕ), j (g n) = v n at hg,
refine ⟨g, by simpa [← hg] using sum_v, by simpa [hg 0] using hv₀, λ n hn,
by simpa [hg] using hv_pos n hn⟩
end
section nnnorm
/-- Auxiliary class, endowing a type `α` with a function `nnnorm : α → ℝ≥0` with notation `∥x∥₊`. -/
class has_nnnorm (E : Type*) := (nnnorm : E → ℝ≥0)
export has_nnnorm (nnnorm)
notation `∥`e`∥₊` := nnnorm e
@[priority 100] -- see Note [lower instance priority]
instance seminormed_add_comm_group.to_has_nnnorm : has_nnnorm E := ⟨λ a, ⟨norm a, norm_nonneg a⟩⟩
@[simp, norm_cast] lemma coe_nnnorm (a : E) : (∥a∥₊ : ℝ) = norm a := rfl
@[simp] lemma coe_comp_nnnorm : (coe : ℝ≥0 → ℝ) ∘ (nnnorm : E → ℝ≥0) = norm := rfl
lemma norm_to_nnreal {a : E} : ∥a∥.to_nnreal = ∥a∥₊ :=
@real.to_nnreal_coe ∥a∥₊
lemma nndist_eq_nnnorm (a b : E) : nndist a b = ∥a - b∥₊ := nnreal.eq $ dist_eq_norm _ _
@[simp] lemma nnnorm_zero : ∥(0 : E)∥₊ = 0 :=
nnreal.eq norm_zero
lemma ne_zero_of_nnnorm_ne_zero {g : E} : ∥g∥₊ ≠ 0 → g ≠ 0 :=
mt $ by { rintro rfl, exact nnnorm_zero }
lemma nnnorm_add_le (g h : E) : ∥g + h∥₊ ≤ ∥g∥₊ + ∥h∥₊ :=
nnreal.coe_le_coe.1 $ norm_add_le g h
@[simp] lemma nnnorm_neg (g : E) : ∥-g∥₊ = ∥g∥₊ :=
nnreal.eq $ norm_neg g
lemma nndist_nnnorm_nnnorm_le (g h : E) : nndist ∥g∥₊ ∥h∥₊ ≤ ∥g - h∥₊ :=
nnreal.coe_le_coe.1 $ dist_norm_norm_le g h
/-- The direct path from `0` to `v` is shorter than the path with `u` inserted in between. -/
lemma nnnorm_le_insert (u v : E) : ∥v∥₊ ≤ ∥u∥₊ + ∥u - v∥₊ := norm_le_insert u v
lemma nnnorm_le_insert' (u v : E) : ∥u∥₊ ≤ ∥v∥₊ + ∥u - v∥₊ := norm_le_insert' u v
lemma nnnorm_le_add_nnnorm_add (u v : E) : ∥u∥₊ ≤ ∥u + v∥₊ + ∥v∥₊ := norm_le_add_norm_add u v
lemma of_real_norm_eq_coe_nnnorm (x : E) : ennreal.of_real ∥x∥ = (∥x∥₊ : ℝ≥0∞) :=
ennreal.of_real_eq_coe_nnreal _
lemma edist_eq_coe_nnnorm_sub (x y : E) : edist x y = (∥x - y∥₊ : ℝ≥0∞) :=
by rw [edist_dist, dist_eq_norm, of_real_norm_eq_coe_nnnorm]
lemma edist_eq_coe_nnnorm (x : E) : edist x 0 = (∥x∥₊ : ℝ≥0∞) :=
by rw [edist_eq_coe_nnnorm_sub, _root_.sub_zero]
lemma mem_emetric_ball_zero_iff {x : E} {r : ℝ≥0∞} : x ∈ emetric.ball (0 : E) r ↔ ↑∥x∥₊ < r :=
by rw [emetric.mem_ball, edist_eq_coe_nnnorm]
lemma nndist_add_add_le (g₁ g₂ h₁ h₂ : E) :
nndist (g₁ + g₂) (h₁ + h₂) ≤ nndist g₁ h₁ + nndist g₂ h₂ :=
nnreal.coe_le_coe.1 $ dist_add_add_le g₁ g₂ h₁ h₂
lemma edist_add_add_le (g₁ g₂ h₁ h₂ : E) :
edist (g₁ + g₂) (h₁ + h₂) ≤ edist g₁ h₁ + edist g₂ h₂ :=
by { simp only [edist_nndist], norm_cast, apply nndist_add_add_le }
@[simp] lemma edist_add_left (g h₁ h₂ : E) : edist (g + h₁) (g + h₂) = edist h₁ h₂ :=
by simp [edist_dist]
@[simp] lemma edist_add_right (g₁ g₂ h : E) : edist (g₁ + h) (g₂ + h) = edist g₁ g₂ :=
by simp [edist_dist]
lemma edist_neg (x y : E) : edist (-x) y = edist x (-y) := by simp_rw [edist_dist, dist_neg]
@[simp] lemma edist_neg_neg (x y : E) : edist (-x) (-y) = edist x y := by rw [edist_neg, neg_neg]
@[simp] lemma edist_sub_left (g h₁ h₂ : E) : edist (g - h₁) (g - h₂) = edist h₁ h₂ :=
by simp only [sub_eq_add_neg, edist_add_left, edist_neg_neg]
@[simp] lemma edist_sub_right (g₁ g₂ h : E) : edist (g₁ - h) (g₂ - h) = edist g₁ g₂ :=
by simpa only [sub_eq_add_neg] using edist_add_right _ _ _
lemma nnnorm_sum_le (s : finset ι) (f : ι → E) :
∥∑ a in s, f a∥₊ ≤ ∑ a in s, ∥f a∥₊ :=
s.le_sum_of_subadditive nnnorm nnnorm_zero nnnorm_add_le f
lemma nnnorm_sum_le_of_le (s : finset ι) {f : ι → E} {n : ι → ℝ≥0} (h : ∀ b ∈ s, ∥f b∥₊ ≤ n b) :
∥∑ b in s, f b∥₊ ≤ ∑ b in s, n b :=
(norm_sum_le_of_le s h).trans_eq nnreal.coe_sum.symm
lemma add_monoid_hom_class.lipschitz_of_bound_nnnorm {𝓕 : Type*} [add_monoid_hom_class 𝓕 E F]
(f : 𝓕) (C : ℝ≥0) (h : ∀ x, ∥f x∥₊ ≤ C * ∥x∥₊) : lipschitz_with C f :=
@real.to_nnreal_coe C ▸ add_monoid_hom_class.lipschitz_of_bound f C h
lemma add_monoid_hom_class.antilipschitz_of_bound {𝓕 : Type*}
[add_monoid_hom_class 𝓕 E F] (f : 𝓕) {K : ℝ≥0} (h : ∀ x, ∥x∥ ≤ K * ∥f x∥) :
antilipschitz_with K f :=
antilipschitz_with.of_le_mul_dist $
λ x y, by simpa only [dist_eq_norm, map_sub] using h (x - y)
lemma add_monoid_hom_class.bound_of_antilipschitz {𝓕 : Type*} [add_monoid_hom_class 𝓕 E F] (f : 𝓕)
{K : ℝ≥0} (h : antilipschitz_with K f) (x) : ∥x∥ ≤ K * ∥f x∥ :=
by simpa only [dist_zero_right, map_zero] using h.le_mul_dist x 0
end nnnorm
namespace lipschitz_with
variables [pseudo_emetric_space α] {K Kf Kg : ℝ≥0} {f g : α → E}
lemma neg (hf : lipschitz_with K f) : lipschitz_with K (λ x, -f x) :=
λ x y, (edist_neg_neg _ _).trans_le $ hf x y
lemma add (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) :
lipschitz_with (Kf + Kg) (λ x, f x + g x) :=
λ x y,
calc edist (f x + g x) (f y + g y) ≤ edist (f x) (f y) + edist (g x) (g y) :
edist_add_add_le _ _ _ _
... ≤ Kf * edist x y + Kg * edist x y :
add_le_add (hf x y) (hg x y)
... = (Kf + Kg) * edist x y :
(add_mul _ _ _).symm
lemma sub (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) :
lipschitz_with (Kf + Kg) (λ x, f x - g x) :=
by simpa only [sub_eq_add_neg] using hf.add hg.neg
end lipschitz_with
namespace antilipschitz_with
variables [pseudo_emetric_space α] {K Kf Kg : ℝ≥0} {f g : α → E}
lemma add_lipschitz_with (hf : antilipschitz_with Kf f) (hg : lipschitz_with Kg g)
(hK : Kg < Kf⁻¹) : antilipschitz_with (Kf⁻¹ - Kg)⁻¹ (λ x, f x + g x) :=
begin
letI : pseudo_metric_space α := pseudo_emetric_space.to_pseudo_metric_space hf.edist_ne_top,
refine antilipschitz_with.of_le_mul_dist (λ x y, _),
rw [nnreal.coe_inv, ← div_eq_inv_mul],
rw le_div_iff (nnreal.coe_pos.2 $ tsub_pos_iff_lt.2 hK),
rw [mul_comm, nnreal.coe_sub hK.le, sub_mul],
calc ↑Kf⁻¹ * dist x y - Kg * dist x y ≤ dist (f x) (f y) - dist (g x) (g y) :
sub_le_sub (hf.mul_le_dist x y) (hg.dist_le_mul x y)
... ≤ _ : le_trans (le_abs_self _) (abs_dist_sub_le_dist_add_add _ _ _ _)
end
lemma add_sub_lipschitz_with (hf : antilipschitz_with Kf f) (hg : lipschitz_with Kg (g - f))
(hK : Kg < Kf⁻¹) : antilipschitz_with (Kf⁻¹ - Kg)⁻¹ g :=
by simpa only [pi.sub_apply, add_sub_cancel'_right] using hf.add_lipschitz_with hg hK
lemma le_mul_norm_sub {f : E → F} (hf : antilipschitz_with K f) (x y : E) :
∥x - y∥ ≤ K * ∥f x - f y∥ :=
by simp [← dist_eq_norm, hf.le_mul_dist x y]
end antilipschitz_with
/-- A group homomorphism from an `add_comm_group` to a `seminormed_add_comm_group` induces a
`seminormed_add_comm_group` structure on the domain.
See note [reducible non-instances] -/
@[reducible]
def seminormed_add_comm_group.induced {E} [add_comm_group E] (f : E →+ F) :
seminormed_add_comm_group E :=
{ norm := λ x, ∥f x∥,
dist_eq := λ x y, by simpa only [add_monoid_hom.map_sub, ← dist_eq_norm],
.. pseudo_metric_space.induced f seminormed_add_comm_group.to_pseudo_metric_space, }
/-- A subgroup of a seminormed group is also a seminormed group,
with the restriction of the norm. -/
instance add_subgroup.seminormed_add_comm_group (s : add_subgroup E) :
seminormed_add_comm_group s :=
seminormed_add_comm_group.induced s.subtype
/-- If `x` is an element of a subgroup `s` of a seminormed group `E`, its norm in `s` is equal to
its norm in `E`. -/
@[simp] lemma add_subgroup.coe_norm {E : Type*} [seminormed_add_comm_group E]
{s : add_subgroup E} (x : s) :
∥(x : s)∥ = ∥(x:E)∥ :=
rfl
/-- If `x` is an element of a subgroup `s` of a seminormed group `E`, its norm in `s` is equal to
its norm in `E`.
This is a reversed version of the `simp` lemma `add_subgroup.coe_norm` for use by `norm_cast`.
-/
@[norm_cast] lemma add_subgroup.norm_coe {E : Type*} [seminormed_add_comm_group E]
{s : add_subgroup E} (x : s) :
∥(x : E)∥ = ∥(x : s)∥ :=
rfl
/-- A submodule of a seminormed group is also a seminormed group, with the restriction of the norm.
See note [implicit instance arguments]. -/
instance submodule.seminormed_add_comm_group {𝕜 E : Type*} {_ : ring 𝕜}
[seminormed_add_comm_group E] {_ : module 𝕜 E} (s : submodule 𝕜 E) :
seminormed_add_comm_group s :=
{ norm := λx, norm (x : E),
dist_eq := λx y, dist_eq_norm (x : E) (y : E) }
/-- If `x` is an element of a submodule `s` of a normed group `E`, its norm in `s` is equal to its
norm in `E`.
See note [implicit instance arguments]. -/
@[simp] lemma submodule.coe_norm {𝕜 : Type*} {_ : ring 𝕜}
{E : Type*} [seminormed_add_comm_group E] {_ : module 𝕜 E} {s : submodule 𝕜 E} (x : s) :
∥(x : s)∥ = ∥(x : E)∥ :=
rfl
/-- If `x` is an element of a submodule `s` of a normed group `E`, its norm in `E` is equal to its
norm in `s`.
This is a reversed version of the `simp` lemma `submodule.coe_norm` for use by `norm_cast`.
See note [implicit instance arguments]. -/
@[norm_cast] lemma submodule.norm_coe {𝕜 : Type*} {_ : ring 𝕜}
{E : Type*} [seminormed_add_comm_group E] {_ : module 𝕜 E} {s : submodule 𝕜 E} (x : s) :
∥(x : E)∥ = ∥(x : s)∥ :=
rfl
instance ulift.seminormed_add_comm_group : seminormed_add_comm_group (ulift E) :=
seminormed_add_comm_group.induced ⟨ulift.down, rfl, λ _ _, rfl⟩
lemma ulift.norm_def (x : ulift E) : ∥x∥ = ∥x.down∥ := rfl
lemma ulift.nnnorm_def (x : ulift E) : ∥x∥₊ = ∥x.down∥₊ := rfl
@[simp] lemma ulift.norm_up (x : E) : ∥ulift.up x∥ = ∥x∥ := rfl
@[simp] lemma ulift.nnnorm_up (x : E) : ∥ulift.up x∥₊ = ∥x∥₊ := rfl
/-- seminormed group instance on the product of two seminormed groups, using the sup norm. -/
noncomputable instance prod.seminormed_add_comm_group : seminormed_add_comm_group (E × F) :=
{ norm := λx, max ∥x.1∥ ∥x.2∥,
dist_eq := assume (x y : E × F),
show max (dist x.1 y.1) (dist x.2 y.2) = (max ∥(x - y).1∥ ∥(x - y).2∥), by simp [dist_eq_norm] }
lemma prod.norm_def (x : E × F) : ∥x∥ = (max ∥x.1∥ ∥x.2∥) := rfl
lemma prod.nnnorm_def (x : E × F) : ∥x∥₊ = max (∥x.1∥₊) (∥x.2∥₊) :=
by { have := x.norm_def, simp only [← coe_nnnorm] at this, exact_mod_cast this }
lemma norm_fst_le (x : E × F) : ∥x.1∥ ≤ ∥x∥ :=
le_max_left _ _
lemma norm_snd_le (x : E × F) : ∥x.2∥ ≤ ∥x∥ :=
le_max_right _ _
lemma norm_prod_le_iff {x : E × F} {r : ℝ} :
∥x∥ ≤ r ↔ ∥x.1∥ ≤ r ∧ ∥x.2∥ ≤ r :=
max_le_iff
section pi
variables {π : ι → Type*} [fintype ι] [Π i, seminormed_add_comm_group (π i)] (f : Π i, π i)
/-- seminormed group instance on the product of finitely many seminormed groups,
using the sup norm. -/
noncomputable instance pi.seminormed_add_comm_group : seminormed_add_comm_group (Π i, π i) :=
{ norm := λ f, ↑(finset.univ.sup (λ b, ∥f b∥₊)),
dist_eq := assume x y,
congr_arg (coe : ℝ≥0 → ℝ) $ congr_arg (finset.sup finset.univ) $ funext $ assume a,
show nndist (x a) (y a) = ∥x a - y a∥₊, from nndist_eq_nnnorm _ _ }
lemma pi.norm_def : ∥f∥ = ↑(finset.univ.sup (λ b, ∥f b∥₊)) := rfl
lemma pi.nnnorm_def : ∥f∥₊ = finset.univ.sup (λ b, ∥f b∥₊) := subtype.eta _ _
/-- The seminorm of an element in a product space is `≤ r` if and only if the norm of each
component is. -/
lemma pi_norm_le_iff {r : ℝ} (hr : 0 ≤ r) {x : Π i, π i} : ∥x∥ ≤ r ↔ ∀ i, ∥x i∥ ≤ r :=
by simp only [← dist_zero_right, dist_pi_le_iff hr, pi.zero_apply]
lemma pi_nnnorm_le_iff {r : ℝ≥0} {x : Π i, π i} : ∥x∥₊ ≤ r ↔ ∀i, ∥x i∥₊ ≤ r :=
pi_norm_le_iff r.coe_nonneg
/-- The seminorm of an element in a product space is `< r` if and only if the norm of each
component is. -/
lemma pi_norm_lt_iff {r : ℝ} (hr : 0 < r) {x : Π i, π i} : ∥x∥ < r ↔ ∀ i, ∥x i∥ < r :=
by simp only [← dist_zero_right, dist_pi_lt_iff hr, pi.zero_apply]
lemma pi_nnnorm_lt_iff {r : ℝ≥0} (hr : 0 < r) {x : Π i, π i} : ∥x∥₊ < r ↔ ∀ i, ∥x i∥₊ < r :=
pi_norm_lt_iff hr
lemma norm_le_pi_norm (i : ι) : ∥f i∥ ≤ ∥f∥ := (pi_norm_le_iff $ norm_nonneg _).1 le_rfl i
lemma nnnorm_le_pi_nnnorm (i : ι) : ∥f i∥₊ ≤ ∥f∥₊ := norm_le_pi_norm _ i
@[simp] lemma pi_norm_const [nonempty ι] (a : E) : ∥(λ i : ι, a)∥ = ∥a∥ :=
by simpa only [← dist_zero_right] using dist_pi_const a 0
@[simp] lemma pi_nnnorm_const [nonempty ι] (a : E) : ∥(λ i : ι, a)∥₊ = ∥a∥₊ :=
nnreal.eq $ pi_norm_const a
/-- The $L^1$ norm is less than the $L^\infty$ norm scaled by the cardinality. -/
lemma pi.sum_norm_apply_le_norm : ∑ i, ∥f i∥ ≤ fintype.card ι • ∥f∥ :=
finset.sum_le_card_nsmul _ _ _ $ λ i hi, norm_le_pi_norm _ i
/-- The $L^1$ norm is less than the $L^\infty$ norm scaled by the cardinality. -/
lemma pi.sum_nnnorm_apply_le_nnnorm : ∑ i, ∥f i∥₊ ≤ fintype.card ι • ∥f∥₊ :=
nnreal.coe_sum.trans_le $ pi.sum_norm_apply_le_norm _
end pi
lemma tendsto_iff_norm_tendsto_zero {f : α → E} {a : filter α} {b : E} :
tendsto f a (𝓝 b) ↔ tendsto (λ e, ∥f e - b∥) a (𝓝 0) :=
by { convert tendsto_iff_dist_tendsto_zero, simp [dist_eq_norm] }
lemma tendsto_zero_iff_norm_tendsto_zero {f : α → E} {a : filter α} :
tendsto f a (𝓝 0) ↔ tendsto (λ e, ∥f e∥) a (𝓝 0) :=
by { rw [tendsto_iff_norm_tendsto_zero], simp only [sub_zero] }
lemma comap_norm_nhds_zero : comap norm (𝓝 0) = 𝓝 (0 : E) :=
by simpa only [dist_zero_right] using nhds_comap_dist (0 : E)
/-- Special case of the sandwich theorem: if the norm of `f` is eventually bounded by a real
function `g` which tends to `0`, then `f` tends to `0`.
In this pair of lemmas (`squeeze_zero_norm'` and `squeeze_zero_norm`), following a convention of
similar lemmas in `topology.metric_space.basic` and `topology.algebra.order`, the `'` version is
phrased using "eventually" and the non-`'` version is phrased absolutely. -/
lemma squeeze_zero_norm' {f : α → E} {g : α → ℝ} {t₀ : filter α}
(h : ∀ᶠ n in t₀, ∥f n∥ ≤ g n)
(h' : tendsto g t₀ (𝓝 0)) : tendsto f t₀ (𝓝 0) :=
tendsto_zero_iff_norm_tendsto_zero.mpr
(squeeze_zero' (eventually_of_forall (λ n, norm_nonneg _)) h h')
/-- Special case of the sandwich theorem: if the norm of `f` is bounded by a real function `g` which
tends to `0`, then `f` tends to `0`. -/
lemma squeeze_zero_norm {f : α → E} {g : α → ℝ} {t₀ : filter α}
(h : ∀ n, ∥f n∥ ≤ g n) (h' : tendsto g t₀ (𝓝 0)) :
tendsto f t₀ (𝓝 0) :=
squeeze_zero_norm' (eventually_of_forall h) h'
lemma tendsto_norm_sub_self (x : E) : tendsto (λ g : E, ∥g - x∥) (𝓝 x) (𝓝 0) :=
by simpa [dist_eq_norm] using tendsto_id.dist (tendsto_const_nhds : tendsto (λ g, (x:E)) (𝓝 x) _)
lemma tendsto_norm {x : E} : tendsto (λg : E, ∥g∥) (𝓝 x) (𝓝 ∥x∥) :=
by simpa using tendsto_id.dist (tendsto_const_nhds : tendsto (λ g, (0:E)) _ _)
lemma tendsto_norm_zero : tendsto (λg : E, ∥g∥) (𝓝 0) (𝓝 0) :=
by simpa using tendsto_norm_sub_self (0:E)
@[continuity]
lemma continuous_norm : continuous (λg:E, ∥g∥) :=
by simpa using continuous_id.dist (continuous_const : continuous (λ g, (0:E)))
@[continuity]
lemma continuous_nnnorm : continuous (λ (a : E), ∥a∥₊) :=
continuous_subtype_mk _ continuous_norm
lemma lipschitz_with_one_norm : lipschitz_with 1 (norm : E → ℝ) :=
by simpa only [dist_zero_left] using lipschitz_with.dist_right (0 : E)
lemma lipschitz_with_one_nnnorm : lipschitz_with 1 (has_nnnorm.nnnorm : E → ℝ≥0) :=
lipschitz_with_one_norm
lemma uniform_continuous_norm : uniform_continuous (norm : E → ℝ) :=
lipschitz_with_one_norm.uniform_continuous
lemma uniform_continuous_nnnorm : uniform_continuous (λ (a : E), ∥a∥₊) :=
uniform_continuous_subtype_mk uniform_continuous_norm _
/-- A helper lemma used to prove that the (scalar or usual) product of a function that tends to zero
and a bounded function tends to zero. This lemma is formulated for any binary operation
`op : E → F → G` with an estimate `∥op x y∥ ≤ A * ∥x∥ * ∥y∥` for some constant A instead of
multiplication so that it can be applied to `(*)`, `flip (*)`, `(•)`, and `flip (•)`. -/
lemma filter.tendsto.op_zero_is_bounded_under_le' {f : α → E} {g : α → F} {l : filter α}
(hf : tendsto f l (𝓝 0)) (hg : is_bounded_under (≤) l (norm ∘ g)) (op : E → F → G)
(h_op : ∃ A, ∀ x y, ∥op x y∥ ≤ A * ∥x∥ * ∥y∥) :
tendsto (λ x, op (f x) (g x)) l (𝓝 0) :=
begin
cases h_op with A h_op,
rcases hg with ⟨C, hC⟩, rw eventually_map at hC,
rw normed_add_comm_group.tendsto_nhds_zero at hf ⊢,
intros ε ε₀,
rcases exists_pos_mul_lt ε₀ (A * C) with ⟨δ, δ₀, hδ⟩,
filter_upwards [hf δ δ₀, hC] with i hf hg,
refine (h_op _ _).trans_lt _,
cases le_total A 0 with hA hA,
{ exact (mul_nonpos_of_nonpos_of_nonneg (mul_nonpos_of_nonpos_of_nonneg hA (norm_nonneg _))
(norm_nonneg _)).trans_lt ε₀ },
calc A * ∥f i∥ * ∥g i∥ ≤ A * δ * C :
mul_le_mul (mul_le_mul_of_nonneg_left hf.le hA) hg (norm_nonneg _) (mul_nonneg hA δ₀.le)
... = A * C * δ : mul_right_comm _ _ _
... < ε : hδ
end
/-- A helper lemma used to prove that the (scalar or usual) product of a function that tends to zero
and a bounded function tends to zero. This lemma is formulated for any binary operation
`op : E → F → G` with an estimate `∥op x y∥ ≤ ∥x∥ * ∥y∥` instead of multiplication so that it
can be applied to `(*)`, `flip (*)`, `(•)`, and `flip (•)`. -/
lemma filter.tendsto.op_zero_is_bounded_under_le {f : α → E} {g : α → F} {l : filter α}
(hf : tendsto f l (𝓝 0)) (hg : is_bounded_under (≤) l (norm ∘ g)) (op : E → F → G)
(h_op : ∀ x y, ∥op x y∥ ≤ ∥x∥ * ∥y∥) :
tendsto (λ x, op (f x) (g x)) l (𝓝 0) :=
hf.op_zero_is_bounded_under_le' hg op ⟨1, λ x y, (one_mul (∥x∥)).symm ▸ h_op x y⟩
section
variables {l : filter α} {f : α → E} {a : E}
lemma filter.tendsto.norm (h : tendsto f l (𝓝 a)) : tendsto (λ x, ∥f x∥) l (𝓝 ∥a∥) :=
tendsto_norm.comp h
lemma filter.tendsto.nnnorm (h : tendsto f l (𝓝 a)) :
tendsto (λ x, ∥f x∥₊) l (𝓝 (∥a∥₊)) :=
tendsto.comp continuous_nnnorm.continuous_at h
end
section
variables [topological_space α] {f : α → E} {s : set α} {a : α} {b : E}
lemma continuous.norm (h : continuous f) : continuous (λ x, ∥f x∥) := continuous_norm.comp h
lemma continuous.nnnorm (h : continuous f) : continuous (λ x, ∥f x∥₊) :=
continuous_nnnorm.comp h
lemma continuous_at.norm (h : continuous_at f a) : continuous_at (λ x, ∥f x∥) a := h.norm
lemma continuous_at.nnnorm (h : continuous_at f a) : continuous_at (λ x, ∥f x∥₊) a := h.nnnorm
lemma continuous_within_at.norm (h : continuous_within_at f s a) :
continuous_within_at (λ x, ∥f x∥) s a :=
h.norm
lemma continuous_within_at.nnnorm (h : continuous_within_at f s a) :
continuous_within_at (λ x, ∥f x∥₊) s a :=
h.nnnorm
lemma continuous_on.norm (h : continuous_on f s) : continuous_on (λ x, ∥f x∥) s :=
λ x hx, (h x hx).norm
lemma continuous_on.nnnorm (h : continuous_on f s) : continuous_on (λ x, ∥f x∥₊) s :=
λ x hx, (h x hx).nnnorm
end
/-- If `∥y∥→∞`, then we can assume `y≠x` for any fixed `x`. -/
lemma eventually_ne_of_tendsto_norm_at_top {l : filter α} {f : α → E}
(h : tendsto (λ y, ∥f y∥) l at_top) (x : E) :
∀ᶠ y in l, f y ≠ x :=
(h.eventually_ne_at_top _).mono $ λ x, ne_of_apply_ne norm
@[priority 100] -- see Note [lower instance priority]
instance seminormed_add_comm_group.has_lipschitz_add : has_lipschitz_add E :=
{ lipschitz_add := ⟨2, lipschitz_with.prod_fst.add lipschitz_with.prod_snd⟩ }
/-- A seminormed group is a uniform additive group, i.e., addition and subtraction are uniformly
continuous. -/
@[priority 100] -- see Note [lower instance priority]
instance normed_uniform_group : uniform_add_group E :=
⟨(lipschitz_with.prod_fst.sub lipschitz_with.prod_snd).uniform_continuous⟩
@[priority 100] -- see Note [lower instance priority]
instance normed_top_group : topological_add_group E :=
by apply_instance -- short-circuit type class inference
lemma seminormed_add_comm_group.mem_closure_iff {s : set E} {x : E} :
x ∈ closure s ↔ ∀ ε > 0, ∃ y ∈ s, ∥x - y∥ < ε :=
by simp [metric.mem_closure_iff, dist_eq_norm]
lemma norm_le_zero_iff' [t0_space E] {g : E} :
∥g∥ ≤ 0 ↔ g = 0 :=
begin
letI : normed_add_comm_group E := { to_metric_space := metric.of_t0_pseudo_metric_space E,
.. ‹seminormed_add_comm_group E› },
rw [← dist_zero_right], exact dist_le_zero
end
lemma norm_eq_zero_iff' [t0_space E] {g : E} : ∥g∥ = 0 ↔ g = 0 :=
(norm_nonneg g).le_iff_eq.symm.trans norm_le_zero_iff'
lemma norm_pos_iff' [t0_space E] {g : E} : 0 < ∥g∥ ↔ g ≠ 0 :=
by rw [← not_le, norm_le_zero_iff']
lemma cauchy_seq_sum_of_eventually_eq {u v : ℕ → E} {N : ℕ} (huv : ∀ n ≥ N, u n = v n)
(hv : cauchy_seq (λ n, ∑ k in range (n+1), v k)) : cauchy_seq (λ n, ∑ k in range (n + 1), u k) :=
begin
let d : ℕ → E := λ n, ∑ k in range (n + 1), (u k - v k),
rw show (λ n, ∑ k in range (n + 1), u k) = d + (λ n, ∑ k in range (n + 1), v k),
by { ext n, simp [d] },
have : ∀ n ≥ N, d n = d N,
{ intros n hn,
dsimp [d],
rw eventually_constant_sum _ hn,
intros m hm,
simp [huv m hm] },
exact (tendsto_at_top_of_eventually_const this).cauchy_seq.add hv
end
end seminormed_add_comm_group
section normed_add_comm_group
/-- Construct a `normed_add_comm_group` from a `seminormed_add_comm_group` satisfying
`∀ x, ∥x∥ = 0 → x = 0`. This avoids having to go back to the `(pseudo_)metric_space` level
when declaring a `normed_add_comm_group` instance as a special case of a more general
`seminormed_add_comm_group` instance. -/
def normed_add_comm_group.of_separation [h₁ : seminormed_add_comm_group E]
(h₂ : ∀ x : E, ∥x∥ = 0 → x = 0) : normed_add_comm_group E :=
{ to_metric_space :=
{ eq_of_dist_eq_zero := λ x y hxy, by rw h₁.dist_eq at hxy; rw ← sub_eq_zero; exact h₂ _ hxy },
..h₁ }
/-- Construct a normed group from a translation invariant distance -/
def normed_add_comm_group.of_add_dist [has_norm E] [add_comm_group E] [metric_space E]
(H1 : ∀ x : E, ∥x∥ = dist x 0)
(H2 : ∀ x y z : E, dist x y ≤ dist (x + z) (y + z)) : normed_add_comm_group E :=
{ dist_eq := λ x y, begin
rw H1, apply le_antisymm,
{ rw [sub_eq_add_neg, ← add_right_neg y], apply H2 },
{ have := H2 (x-y) 0 y, rwa [sub_add_cancel, zero_add] at this }
end }
/-- A normed group can be built from a norm that satisfies algebraic properties. This is
formalised in this structure. -/
structure normed_add_comm_group.core (E : Type*) [add_comm_group E] [has_norm E] : Prop :=
(norm_eq_zero_iff : ∀ x : E, ∥x∥ = 0 ↔ x = 0)
(triangle : ∀ x y : E, ∥x + y∥ ≤ ∥x∥ + ∥y∥)
(norm_neg : ∀ x : E, ∥-x∥ = ∥x∥)
/-- The `seminormed_add_comm_group.core` induced by a `normed_add_comm_group.core`. -/
lemma normed_add_comm_group.core.to_seminormed_add_comm_group.core {E : Type*} [add_comm_group E]
[has_norm E]
(C : normed_add_comm_group.core E) : seminormed_add_comm_group.core E :=
{ norm_zero := (C.norm_eq_zero_iff 0).2 rfl,
triangle := C.triangle,
norm_neg := C.norm_neg }
/-- Constructing a normed group from core properties of a norm, i.e., registering the distance and
the metric space structure from the norm properties. -/
def normed_add_comm_group.of_core (E : Type*) [add_comm_group E] [has_norm E]
(C : normed_add_comm_group.core E) : normed_add_comm_group E :=
{ eq_of_dist_eq_zero := λ x y h,
begin
rw [dist_eq_norm] at h,
exact sub_eq_zero.mp ((C.norm_eq_zero_iff _).1 h)
end
..seminormed_add_comm_group.of_core E
(normed_add_comm_group.core.to_seminormed_add_comm_group.core C) }
variables [normed_add_comm_group E] [normed_add_comm_group F] {x y : E}
@[simp] lemma norm_eq_zero {g : E} : ∥g∥ = 0 ↔ g = 0 := norm_eq_zero_iff'
lemma norm_ne_zero_iff {g : E} : ∥g∥ ≠ 0 ↔ g ≠ 0 := not_congr norm_eq_zero
@[simp] lemma norm_pos_iff {g : E} : 0 < ∥ g ∥ ↔ g ≠ 0 := norm_pos_iff'
@[simp] lemma norm_le_zero_iff {g : E} : ∥g∥ ≤ 0 ↔ g = 0 := norm_le_zero_iff'
lemma norm_sub_eq_zero_iff {u v : E} : ∥u - v∥ = 0 ↔ u = v :=
by rw [norm_eq_zero, sub_eq_zero]
lemma norm_sub_pos_iff : 0 < ∥x - y∥ ↔ x ≠ y :=
by { rw [(norm_nonneg _).lt_iff_ne, ne_comm], exact norm_sub_eq_zero_iff.not }
lemma eq_of_norm_sub_le_zero {g h : E} (a : ∥g - h∥ ≤ 0) : g = h :=
by rwa [← sub_eq_zero, ← norm_le_zero_iff]
lemma eq_of_norm_sub_eq_zero {u v : E} (h : ∥u - v∥ = 0) : u = v :=
norm_sub_eq_zero_iff.1 h
@[simp] lemma nnnorm_eq_zero {a : E} : ∥a∥₊ = 0 ↔ a = 0 :=
by rw [← nnreal.coe_eq_zero, coe_nnnorm, norm_eq_zero]
lemma nnnorm_ne_zero_iff {g : E} : ∥g∥₊ ≠ 0 ↔ g ≠ 0 := not_congr nnnorm_eq_zero
/-- An injective group homomorphism from an `add_comm_group` to a `normed_add_comm_group` induces a
`normed_add_comm_group` structure on the domain.
See note [reducible non-instances]. -/
@[reducible]
def normed_add_comm_group.induced {E} [add_comm_group E]
(f : E →+ F) (h : function.injective f) : normed_add_comm_group E :=
{ .. seminormed_add_comm_group.induced f,
.. metric_space.induced f h normed_add_comm_group.to_metric_space, }
/-- A subgroup of a normed group is also a normed group, with the restriction of the norm. -/
instance add_subgroup.normed_add_comm_group (s : add_subgroup E) : normed_add_comm_group s :=
normed_add_comm_group.induced s.subtype subtype.coe_injective
/-- A submodule of a normed group is also a normed group, with the restriction of the norm.
See note [implicit instance arguments]. -/
instance submodule.normed_add_comm_group {𝕜 E : Type*} {_ : ring 𝕜} [normed_add_comm_group E]
{_ : module 𝕜 E} (s : submodule 𝕜 E) : normed_add_comm_group s :=
{ ..submodule.seminormed_add_comm_group s }
instance ulift.normed_add_comm_group : normed_add_comm_group (ulift E) :=
{ ..ulift.seminormed_add_comm_group }
/-- normed group instance on the product of two normed groups, using the sup norm. -/
noncomputable instance prod.normed_add_comm_group : normed_add_comm_group (E × F) :=
{ ..prod.seminormed_add_comm_group }
/-- normed group instance on the product of finitely many normed groups, using the sup norm. -/
noncomputable instance pi.normed_add_comm_group {π : ι → Type*} [fintype ι]
[Π i, normed_add_comm_group (π i)] :
normed_add_comm_group (Πi, π i) := { ..pi.seminormed_add_comm_group }
lemma tendsto_norm_sub_self_punctured_nhds (a : E) : tendsto (λ x, ∥x - a∥) (𝓝[≠] a) (𝓝[>] 0) :=
(tendsto_norm_sub_self a).inf $ tendsto_principal_principal.2 $ λ x hx,
norm_pos_iff.2 $ sub_ne_zero.2 hx
lemma tendsto_norm_nhds_within_zero : tendsto (norm : E → ℝ) (𝓝[≠] 0) (𝓝[>] 0) :=
tendsto_norm_zero.inf $ tendsto_principal_principal.2 $ λ x, norm_pos_iff.2
/-! Some relations with `has_compact_support` -/
lemma has_compact_support_norm_iff [topological_space α] {f : α → E} :
has_compact_support (λ x, ∥ f x ∥) ↔ has_compact_support f :=
has_compact_support_comp_left $ λ x, norm_eq_zero
alias has_compact_support_norm_iff ↔ _ has_compact_support.norm
lemma continuous.bounded_above_of_compact_support [topological_space α] {f : α → E}
(hf : continuous f) (hsupp : has_compact_support f) : ∃ C, ∀ x, ∥f x∥ ≤ C :=
by simpa [bdd_above_def] using hf.norm.bdd_above_range_of_has_compact_support hsupp.norm
end normed_add_comm_group
|
59b669dab958b8aeb260b14f6ff0ea8fef821bd2 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/module/projective.lean | 3cc28e42cf214b002fe24054498931136d14e2cd | [
"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,571 | lean | /-
Copyright (c) 2021 Kevin Buzzard. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Antoine Labelle
-/
import algebra.module.basic
import linear_algebra.finsupp
import linear_algebra.free_module.basic
/-!
# Projective modules
This file contains a definition of a projective module, the proof that
our definition is equivalent to a lifting property, and the
proof that all free modules are projective.
## Main definitions
Let `R` be a ring (or a semiring) and let `M` be an `R`-module.
* `is_projective R M` : the proposition saying that `M` is a projective `R`-module.
## Main theorems
* `is_projective.lifting_property` : a map from a projective module can be lifted along
a surjection.
* `is_projective.of_lifting_property` : If for all R-module surjections `A →ₗ B`, all
maps `M →ₗ B` lift to `M →ₗ A`, then `M` is projective.
* `is_projective.of_free` : Free modules are projective
## Implementation notes
The actual definition of projective we use is that the natural R-module map
from the free R-module on the type M down to M splits. This is more convenient
than certain other definitions which involve quantifying over universes,
and also universe-polymorphic (the ring and module can be in different universes).
We require that the module sits in at least as high a universe as the ring:
without this, free modules don't even exist,
and it's unclear if projective modules are even a useful notion.
## References
https://en.wikipedia.org/wiki/Projective_module
## TODO
- Direct sum of two projective modules is projective.
- Arbitrary sum of projective modules is projective.
All of these should be relatively straightforward.
## Tags
projective module
-/
universes u v
open linear_map finsupp
/- The actual implementation we choose: `P` is projective if the natural surjection
from the free `R`-module on `P` to `P` splits. -/
/-- An R-module is projective if it is a direct summand of a free module, or equivalently
if maps from the module lift along surjections. There are several other equivalent
definitions. -/
class module.projective (R : Type*) [semiring R] (P : Type*) [add_comm_monoid P]
[module R P] : Prop :=
(out : ∃ s : P →ₗ[R] (P →₀ R), function.left_inverse (finsupp.total P P R id) s)
namespace module
section semiring
variables {R : Type*} [semiring R] {P : Type*} [add_comm_monoid P] [module R P]
{M : Type*} [add_comm_monoid M] [module R M] {N : Type*} [add_comm_monoid N] [module R N]
lemma projective_def : projective R P ↔
(∃ s : P →ₗ[R] (P →₀ R), function.left_inverse (finsupp.total P P R id) s) :=
⟨λ h, h.1, λ h, ⟨h⟩⟩
theorem projective_def' : projective R P ↔
(∃ s : P →ₗ[R] (P →₀ R), (finsupp.total P P R id) ∘ₗ s = id) :=
by simp_rw [projective_def, fun_like.ext_iff, function.left_inverse, coe_comp, id_coe, id.def]
/-- A projective R-module has the property that maps from it lift along surjections. -/
theorem projective_lifting_property [h : projective R P] (f : M →ₗ[R] N) (g : P →ₗ[R] N)
(hf : function.surjective f) : ∃ (h : P →ₗ[R] M), f.comp h = g :=
begin
/-
Here's the first step of the proof.
Recall that `X →₀ R` is Lean's way of talking about the free `R`-module
on a type `X`. The universal property `finsupp.total` says that to a map
`X → N` from a type to an `R`-module, we get an associated R-module map
`(X →₀ R) →ₗ N`. Apply this to a (noncomputable) map `P → M` coming from the map
`P →ₗ N` and a random splitting of the surjection `M →ₗ N`, and we get
a map `φ : (P →₀ R) →ₗ M`.
-/
let φ : (P →₀ R) →ₗ[R] M := finsupp.total _ _ _ (λ p, function.surj_inv hf (g p)),
-- By projectivity we have a map `P →ₗ (P →₀ R)`;
cases h.out with s hs,
-- Compose to get `P →ₗ M`. This works.
use φ.comp s,
ext p,
conv_rhs {rw ← hs p},
simp [φ, finsupp.total_apply, function.surj_inv_eq hf],
end
variables {Q : Type*} [add_comm_monoid Q] [module R Q]
instance [hP : projective R P] [hQ : projective R Q] : projective R (P × Q) :=
begin
rw module.projective_def',
cases hP.out with sP hsP,
cases hQ.out with sQ hsQ,
use coprod (lmap_domain R R (inl R P Q)) (lmap_domain R R (inr R P Q)) ∘ₗ sP.prod_map sQ,
ext; simp only [coe_inl, coe_inr, coe_comp, function.comp_app, prod_map_apply, map_zero,
coprod_apply, lmap_domain_apply, map_domain_zero, add_zero, zero_add, id_comp,
total_map_domain],
{ rw [←fst_apply _, apply_total R], exact hsP x, },
{ rw [←snd_apply _, apply_total R], exact finsupp.total_zero_apply _ (sP x), },
{ rw [←fst_apply _, apply_total R], exact finsupp.total_zero_apply _ (sQ x), },
{ rw [←snd_apply _, apply_total R], exact hsQ x, },
end
variables {ι : Type*} (A : ι → Type*) [Π (i : ι), add_comm_monoid (A i)]
[Π (i : ι), module R (A i)]
instance [h : Π (i : ι), projective R (A i)] : projective R (Π₀ i, A i) :=
begin
classical,
rw module.projective_def',
simp_rw projective_def at h, choose s hs using h,
letI : Π (i : ι), add_comm_monoid (A i →₀ R) := λ i, by apply_instance,
letI : Π (i : ι), module R (A i →₀ R) := λ i, by apply_instance,
letI : add_comm_monoid (Π₀ (i : ι), A i →₀ R) := @dfinsupp.add_comm_monoid ι (λ i, A i →₀ R) _,
letI : module R (Π₀ (i : ι), A i →₀ R) := @dfinsupp.module ι R (λ i, A i →₀ R) _ _ _,
let f := λ i, lmap_domain R R (dfinsupp.single i : A i → Π₀ i, A i),
use dfinsupp.coprod_map f ∘ₗ dfinsupp.map_range.linear_map s,
ext i x j,
simp only [dfinsupp.coprod_map, direct_sum.lof, total_map_domain,
coe_comp, coe_lsum, id_coe, linear_equiv.coe_to_linear_map, finsupp_lequiv_dfinsupp_symm_apply,
function.comp_app, dfinsupp.lsingle_apply, dfinsupp.map_range.linear_map_apply,
dfinsupp.map_range_single, lmap_domain_apply, dfinsupp.to_finsupp_single,
finsupp.sum_single_index, id.def, function.comp.left_id, dfinsupp.single_apply],
rw [←dfinsupp.lapply_apply j, apply_total R],
obtain rfl | hij := eq_or_ne i j,
{ convert (hs i) x,
{ ext, simp },
{ simp } },
{ convert finsupp.total_zero_apply _ ((s i) x),
{ ext, simp [hij] },
{ simp [hij] } }
end
end semiring
section ring
variables {R : Type*} [ring R] {P : Type*} [add_comm_group P] [module R P]
/-- Free modules are projective. -/
theorem projective_of_basis {ι : Type*} (b : basis ι R P) : projective R P :=
begin
-- need P →ₗ (P →₀ R) for definition of projective.
-- get it from `ι → (P →₀ R)` coming from `b`.
use b.constr ℕ (λ i, finsupp.single (b i) (1 : R)),
intro m,
simp only [b.constr_apply, mul_one, id.def, finsupp.smul_single', finsupp.total_single,
linear_map.map_finsupp_sum],
exact b.total_repr m,
end
@[priority 100]
instance projective_of_free [module.free R P] : module.projective R P :=
projective_of_basis $ module.free.choose_basis R P
end ring
--This is in a different section because special universe restrictions are required.
section of_lifting_property
/-- A module which satisfies the universal property is projective. Note that the universe variables
in `huniv` are somewhat restricted. -/
theorem projective_of_lifting_property'
{R : Type u} [semiring R] {P : Type (max u v)} [add_comm_monoid P] [module R P]
-- If for all surjections of `R`-modules `M →ₗ N`, all maps `P →ₗ N` lift to `P →ₗ M`,
(huniv : ∀ {M : Type (max v u)} {N : Type (max u v)} [add_comm_monoid M] [add_comm_monoid N],
by exactI
∀ [module R M] [module R N],
by exactI
∀ (f : M →ₗ[R] N) (g : P →ₗ[R] N),
function.surjective f → ∃ (h : P →ₗ[R] M), f.comp h = g) :
-- then `P` is projective.
projective R P :=
begin
-- let `s` be the universal map `(P →₀ R) →ₗ P` coming from the identity map `P →ₗ P`.
obtain ⟨s, hs⟩ : ∃ (s : P →ₗ[R] P →₀ R),
(finsupp.total P P R id).comp s = linear_map.id :=
huniv (finsupp.total P P R (id : P → P)) (linear_map.id : P →ₗ[R] P) _,
-- This `s` works.
{ use s,
rwa linear_map.ext_iff at hs },
{ intro p,
use finsupp.single p 1,
simp },
end
/-- A variant of `of_lifting_property'` when we're working over a `[ring R]`,
which only requires quantifying over modules with an `add_comm_group` instance. -/
theorem projective_of_lifting_property
{R : Type u} [ring R] {P : Type (max u v)} [add_comm_group P] [module R P]
-- If for all surjections of `R`-modules `M →ₗ N`, all maps `P →ₗ N` lift to `P →ₗ M`,
(huniv : ∀ {M : Type (max v u)} {N : Type (max u v)} [add_comm_group M] [add_comm_group N],
by exactI
∀ [module R M] [module R N],
by exactI
∀ (f : M →ₗ[R] N) (g : P →ₗ[R] N),
function.surjective f → ∃ (h : P →ₗ[R] M), f.comp h = g) :
-- then `P` is projective.
projective R P :=
-- We could try and prove this *using* `of_lifting_property`,
-- but this quickly leads to typeclass hell,
-- so we just prove it over again.
begin
-- let `s` be the universal map `(P →₀ R) →ₗ P` coming from the identity map `P →ₗ P`.
obtain ⟨s, hs⟩ : ∃ (s : P →ₗ[R] P →₀ R),
(finsupp.total P P R id).comp s = linear_map.id :=
huniv (finsupp.total P P R (id : P → P)) (linear_map.id : P →ₗ[R] P) _,
-- This `s` works.
{ use s,
rwa linear_map.ext_iff at hs },
{ intro p,
use finsupp.single p 1,
simp },
end
end of_lifting_property
end module
|
eaf8752d73a2a3b274b2e064e32b01df2d03712b | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/group_theory/abelianization.lean | 79ab63fa8f176ccd72a9c1135e876a569a945f72 | [
"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 | 1,935 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Michael Howes
The functor Grp → Ab which is the left adjoint
of the forgetful functor Ab → Grp.
-/
import group_theory.quotient_group
universes u v
variables (α : Type u) [group α]
def commutator : set α :=
group.normal_closure {x | ∃ p q, p * q * p⁻¹ * q⁻¹ = x}
instance : normal_subgroup (commutator α) :=
group.normal_closure.is_normal
def abelianization : Type u :=
quotient_group.quotient $ commutator α
namespace abelianization
local attribute [instance] quotient_group.left_rel normal_subgroup.to_is_subgroup
instance : comm_group (abelianization α) :=
{ mul_comm := λ x y, quotient.induction_on₂ x y $ λ a b, quotient.sound
(group.subset_normal_closure ⟨b⁻¹,a⁻¹, by simp [mul_inv_rev, inv_inv, mul_assoc]⟩),
.. quotient_group.group _}
instance : inhabited (abelianization α) := ⟨1⟩
variable {α}
def of (x : α) : abelianization α :=
quotient.mk x
instance of.is_group_hom : is_group_hom (@of α _) :=
{ map_mul := λ _ _, rfl }
section lift
variables {β : Type v} [comm_group β] (f : α → β) [is_group_hom f]
lemma commutator_subset_ker : commutator α ⊆ is_group_hom.ker f :=
group.normal_closure_subset (λ x ⟨p,q,w⟩, (is_group_hom.mem_ker f).2
(by {rw ←w, simp [is_mul_hom.map_mul f, is_group_hom.map_inv f, mul_comm]}))
def lift : abelianization α → β :=
quotient_group.lift _ f (λ x h, (is_group_hom.mem_ker f).1 (commutator_subset_ker f h))
instance lift.is_group_hom : is_group_hom (lift f) :=
quotient_group.is_group_hom_quotient_lift _ _ _
@[simp] lemma lift.of (x : α) : lift f (of x) = f x :=
rfl
theorem lift.unique
(g : abelianization α → β) [is_group_hom g]
(hg : ∀ x, g (of x) = f x) {x} :
g x = lift f x :=
quotient_group.induction_on x hg
end lift
end abelianization
|
779026559fd385f47b03936e1a235144a8761b20 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/resolve_open.lean | a50834e1d30dd17fb4024230802d5d9211676bbc | [
"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 | 363 | lean | prelude
import init.core
def A.a := 1
def B.b := 1
def C.c := 1
def foo.A.a := 2
def foo.B.b := 2
def foo.bar.A.a := 3
namespace foo
namespace bar
section open A B C
example : a = 3 := rfl
example : b = 2 := rfl
example : c = 1 := rfl
end
section open _root_.A
example : a = 1 := rfl
end
section open _root_.foo.A
example : a = 2 := rfl
end
end bar
end foo
|
0154c44e847f03f592c677d433bab083552e8b83 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/data/rat/basic.lean | 4a2115ae6f7a1217378423c33193fce8f720e23a | [
"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 | 27,897 | lean | /-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import data.equiv.encodable.basic
import algebra.euclidean_domain
import data.nat.gcd
import data.int.cast
/-!
# Basics for the Rational Numbers
## Summary
We define a rational number `q` as a structure `{ num, denom, pos, cop }`, where
- `num` is the numerator of `q`,
- `denom` is the denominator of `q`,
- `pos` is a proof that `denom > 0`, and
- `cop` is a proof `num` and `denom` are coprime.
We then define the expected (discrete) field structure on `ℚ` and prove basic lemmas about it.
Moreoever, we provide the expected casts from `ℕ` and `ℤ` into `ℚ`, i.e. `(↑n : ℚ) = n / 1`.
## Main Definitions
- `rat` is the structure encoding `ℚ`.
- `rat.mk n d` constructs a rational number `q = n / d` from `n d : ℤ`.
## Notations
- `/.` is infix notation for `rat.mk`.
## Tags
rat, rationals, field, ℚ, numerator, denominator, num, denom
-/
/-- `rat`, or `ℚ`, is the type of rational numbers. It is defined
as the set of pairs ⟨n, d⟩ of integers such that `d` is positive and `n` and
`d` are coprime. This representation is preferred to the quotient
because without periodic reduction, the numerator and denominator can grow
exponentially (for example, adding 1/2 to itself repeatedly). -/
structure rat := mk' ::
(num : ℤ)
(denom : ℕ)
(pos : 0 < denom)
(cop : num.nat_abs.coprime denom)
notation `ℚ` := rat
namespace rat
protected def repr : ℚ → string
| ⟨n, d, _, _⟩ := if d = 1 then _root_.repr n else
_root_.repr n ++ "/" ++ _root_.repr d
instance : has_repr ℚ := ⟨rat.repr⟩
instance : has_to_string ℚ := ⟨rat.repr⟩
meta instance : has_to_format ℚ := ⟨coe ∘ rat.repr⟩
instance : encodable ℚ := encodable.of_equiv (Σ n : ℤ, {d : ℕ // 0 < d ∧ n.nat_abs.coprime d})
⟨λ ⟨a, b, c, d⟩, ⟨a, b, c, d⟩, λ⟨a, b, c, d⟩, ⟨a, b, c, d⟩,
λ ⟨a, b, c, d⟩, rfl, λ⟨a, b, c, d⟩, rfl⟩
/-- Embed an integer as a rational number -/
def of_int (n : ℤ) : ℚ :=
⟨n, 1, nat.one_pos, nat.coprime_one_right _⟩
instance : has_zero ℚ := ⟨of_int 0⟩
instance : has_one ℚ := ⟨of_int 1⟩
instance : inhabited ℚ := ⟨0⟩
/-- Form the quotient `n / d` where `n:ℤ` and `d:ℕ+` (not necessarily coprime) -/
def mk_pnat (n : ℤ) : ℕ+ → ℚ | ⟨d, dpos⟩ :=
let n' := n.nat_abs, g := n'.gcd d in
⟨n / g, d / g, begin
apply (nat.le_div_iff_mul_le _ _ (nat.gcd_pos_of_pos_right _ dpos)).2,
simp, exact nat.le_of_dvd dpos (nat.gcd_dvd_right _ _)
end, begin
have : int.nat_abs (n / ↑g) = n' / g,
{ cases int.nat_abs_eq n with e e; rw e, { refl },
rw [int.neg_div_of_dvd, int.nat_abs_neg], { refl },
exact int.coe_nat_dvd.2 (nat.gcd_dvd_left _ _) },
rw this,
exact nat.coprime_div_gcd_div_gcd (nat.gcd_pos_of_pos_right _ dpos)
end⟩
/-- Form the quotient `n / d` where `n:ℤ` and `d:ℕ`. In the case `d = 0`, we
define `n / 0 = 0` by convention. -/
def mk_nat (n : ℤ) (d : ℕ) : ℚ :=
if d0 : d = 0 then 0 else mk_pnat n ⟨d, nat.pos_of_ne_zero d0⟩
/-- Form the quotient `n / d` where `n d : ℤ`. -/
def mk : ℤ → ℤ → ℚ
| n (d : ℕ) := mk_nat n d
| n -[1+ d] := mk_pnat (-n) d.succ_pnat
localized "infix ` /. `:70 := rat.mk" in rat
theorem mk_pnat_eq (n d h) : mk_pnat n ⟨d, h⟩ = n /. d :=
by change n /. d with dite _ _ _; simp [ne_of_gt h]
theorem mk_nat_eq (n d) : mk_nat n d = n /. d := rfl
@[simp] theorem mk_zero (n) : n /. 0 = 0 := rfl
@[simp] theorem zero_mk_pnat (n) : mk_pnat 0 n = 0 :=
by cases n; simp [mk_pnat]; change int.nat_abs 0 with 0; simp *; refl
@[simp] theorem zero_mk_nat (n) : mk_nat 0 n = 0 :=
by by_cases n = 0; simp [*, mk_nat]
@[simp] theorem zero_mk (n) : 0 /. n = 0 :=
by cases n; simp [mk]
private lemma gcd_abs_dvd_left {a b} : (nat.gcd (int.nat_abs a) b : ℤ) ∣ a :=
int.dvd_nat_abs.1 $ int.coe_nat_dvd.2 $ nat.gcd_dvd_left (int.nat_abs a) b
@[simp] theorem mk_eq_zero {a b : ℤ} (b0 : b ≠ 0) : a /. b = 0 ↔ a = 0 :=
begin
constructor; intro h; [skip, {subst a, simp}],
have : ∀ {a b}, mk_pnat a b = 0 → a = 0,
{ intros a b e, cases b with b h,
injection e with e,
apply int.eq_mul_of_div_eq_right gcd_abs_dvd_left e },
cases b with b; simp [mk, mk_nat] at h,
{ simp [mt (congr_arg int.of_nat) b0] at h,
exact this h },
{ apply neg_injective, simp [this h] }
end
theorem mk_eq : ∀ {a b c d : ℤ} (hb : b ≠ 0) (hd : d ≠ 0),
a /. b = c /. d ↔ a * d = c * b :=
suffices ∀ a b c d hb hd, mk_pnat a ⟨b, hb⟩ = mk_pnat c ⟨d, hd⟩ ↔ a * d = c * b,
begin
intros, cases b with b b; simp [mk, mk_nat, nat.succ_pnat],
simp [mt (congr_arg int.of_nat) hb],
all_goals {
cases d with d d; simp [mk, mk_nat, nat.succ_pnat],
simp [mt (congr_arg int.of_nat) hd],
all_goals { rw this, try {refl} } },
{ change a * ↑(d.succ) = -c * ↑b ↔ a * -(d.succ) = c * b,
constructor; intro h; apply neg_injective; simpa [left_distrib, neg_add_eq_iff_eq_add,
eq_neg_iff_add_eq_zero, neg_eq_iff_add_eq_zero] using h },
{ change -a * ↑d = c * b.succ ↔ a * d = c * -b.succ,
constructor; intro h; apply neg_injective; simpa [left_distrib, eq_comm] using h },
{ change -a * d.succ = -c * b.succ ↔ a * -d.succ = c * -b.succ,
simp [left_distrib, sub_eq_add_neg], cc }
end,
begin
intros, simp [mk_pnat], constructor; intro h,
{ cases h with ha hb,
have ha, {
have dv := @gcd_abs_dvd_left,
have := int.eq_mul_of_div_eq_right dv ha,
rw ← int.mul_div_assoc _ dv at this,
exact int.eq_mul_of_div_eq_left (dvd_mul_of_dvd_right dv _) this.symm },
have hb, {
have dv := λ {a b}, nat.gcd_dvd_right (int.nat_abs a) b,
have := nat.eq_mul_of_div_eq_right dv hb,
rw ← nat.mul_div_assoc _ dv at this,
exact nat.eq_mul_of_div_eq_left (dvd_mul_of_dvd_right dv _) this.symm },
have m0 : (a.nat_abs.gcd b * c.nat_abs.gcd d : ℤ) ≠ 0, {
refine int.coe_nat_ne_zero.2 (ne_of_gt _),
apply mul_pos; apply nat.gcd_pos_of_pos_right; assumption },
apply mul_right_cancel' m0,
simpa [mul_comm, mul_left_comm] using
congr (congr_arg (*) ha.symm) (congr_arg coe hb) },
{ suffices : ∀ a c, a * d = c * b →
a / a.gcd b = c / c.gcd d ∧ b / a.gcd b = d / c.gcd d,
{ cases this a.nat_abs c.nat_abs
(by simpa [int.nat_abs_mul] using congr_arg int.nat_abs h) with h₁ h₂,
have hs := congr_arg int.sign h,
simp [int.sign_eq_one_of_pos (int.coe_nat_lt.2 hb),
int.sign_eq_one_of_pos (int.coe_nat_lt.2 hd)] at hs,
conv in a { rw ← int.sign_mul_nat_abs a },
conv in c { rw ← int.sign_mul_nat_abs c },
rw [int.mul_div_assoc, int.mul_div_assoc],
exact ⟨congr (congr_arg (*) hs) (congr_arg coe h₁), h₂⟩,
all_goals { exact int.coe_nat_dvd.2 (nat.gcd_dvd_left _ _) } },
intros a c h,
suffices bd : b / a.gcd b = d / c.gcd d,
{ refine ⟨_, bd⟩,
apply nat.eq_of_mul_eq_mul_left hb,
rw [← nat.mul_div_assoc _ (nat.gcd_dvd_left _ _), mul_comm,
nat.mul_div_assoc _ (nat.gcd_dvd_right _ _), bd,
← nat.mul_div_assoc _ (nat.gcd_dvd_right _ _), h, mul_comm,
nat.mul_div_assoc _ (nat.gcd_dvd_left _ _)] },
suffices : ∀ {a c : ℕ} (b>0) (d>0),
a * d = c * b → b / a.gcd b ≤ d / c.gcd d,
{ exact le_antisymm (this _ hb _ hd h) (this _ hd _ hb h.symm) },
intros a c b hb d hd h,
have gb0 := nat.gcd_pos_of_pos_right a hb,
have gd0 := nat.gcd_pos_of_pos_right c hd,
apply nat.le_of_dvd,
apply (nat.le_div_iff_mul_le _ _ gd0).2,
simp, apply nat.le_of_dvd hd (nat.gcd_dvd_right _ _),
apply (nat.coprime_div_gcd_div_gcd gb0).symm.dvd_of_dvd_mul_left,
refine ⟨c / c.gcd d, _⟩,
rw [← nat.mul_div_assoc _ (nat.gcd_dvd_left _ _),
← nat.mul_div_assoc _ (nat.gcd_dvd_right _ _)],
apply congr_arg (/ c.gcd d),
rw [mul_comm, ← nat.mul_div_assoc _ (nat.gcd_dvd_left _ _),
mul_comm, h, nat.mul_div_assoc _ (nat.gcd_dvd_right _ _), mul_comm] }
end
@[simp] theorem div_mk_div_cancel_left {a b c : ℤ} (c0 : c ≠ 0) :
(a * c) /. (b * c) = a /. b :=
begin
by_cases b0 : b = 0, { subst b0, simp },
apply (mk_eq (mul_ne_zero b0 c0) b0).2, simp [mul_comm, mul_assoc]
end
@[simp] theorem num_denom : ∀ {a : ℚ}, a.num /. a.denom = a
| ⟨n, d, h, (c:_=1)⟩ := show mk_nat n d = _,
by simp [mk_nat, ne_of_gt h, mk_pnat, c]
theorem num_denom' {n d h c} : (⟨n, d, h, c⟩ : ℚ) = n /. d := num_denom.symm
theorem of_int_eq_mk (z : ℤ) : of_int z = z /. 1 := num_denom'
@[elab_as_eliminator] def {u} num_denom_cases_on {C : ℚ → Sort u}
: ∀ (a : ℚ) (H : ∀ n d, 0 < d → (int.nat_abs n).coprime d → C (n /. d)), C a
| ⟨n, d, h, c⟩ H := by rw num_denom'; exact H n d h c
@[elab_as_eliminator] def {u} num_denom_cases_on' {C : ℚ → Sort u}
(a : ℚ) (H : ∀ (n:ℤ) (d:ℕ), d ≠ 0 → C (n /. d)) : C a :=
num_denom_cases_on a $ λ n d h c,
H n d $ ne_of_gt h
theorem num_dvd (a) {b : ℤ} (b0 : b ≠ 0) : (a /. b).num ∣ a :=
begin
cases e : a /. b with n d h c,
rw [rat.num_denom', rat.mk_eq b0
(ne_of_gt (int.coe_nat_pos.2 h))] at e,
refine (int.nat_abs_dvd.1 $ int.dvd_nat_abs.1 $ int.coe_nat_dvd.2 $
c.dvd_of_dvd_mul_right _),
have := congr_arg int.nat_abs e,
simp [int.nat_abs_mul, int.nat_abs_of_nat] at this, simp [this]
end
theorem denom_dvd (a b : ℤ) : ((a /. b).denom : ℤ) ∣ b :=
begin
by_cases b0 : b = 0, {simp [b0]},
cases e : a /. b with n d h c,
rw [num_denom', mk_eq b0 (ne_of_gt (int.coe_nat_pos.2 h))] at e,
refine (int.dvd_nat_abs.1 $ int.coe_nat_dvd.2 $ c.symm.dvd_of_dvd_mul_left _),
rw [← int.nat_abs_mul, ← int.coe_nat_dvd, int.dvd_nat_abs, ← e], simp
end
protected def add : ℚ → ℚ → ℚ
| ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ := mk_pnat (n₁ * d₂ + n₂ * d₁) ⟨d₁ * d₂, mul_pos h₁ h₂⟩
instance : has_add ℚ := ⟨rat.add⟩
theorem lift_binop_eq (f : ℚ → ℚ → ℚ) (f₁ : ℤ → ℤ → ℤ → ℤ → ℤ) (f₂ : ℤ → ℤ → ℤ → ℤ → ℤ)
(fv : ∀ {n₁ d₁ h₁ c₁ n₂ d₂ h₂ c₂},
f ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ = f₁ n₁ d₁ n₂ d₂ /. f₂ n₁ d₁ n₂ d₂)
(f0 : ∀ {n₁ d₁ n₂ d₂} (d₁0 : d₁ ≠ 0) (d₂0 : d₂ ≠ 0), f₂ n₁ d₁ n₂ d₂ ≠ 0)
(a b c d : ℤ) (b0 : b ≠ 0) (d0 : d ≠ 0)
(H : ∀ {n₁ d₁ n₂ d₂} (h₁ : a * d₁ = n₁ * b) (h₂ : c * d₂ = n₂ * d),
f₁ n₁ d₁ n₂ d₂ * f₂ a b c d = f₁ a b c d * f₂ n₁ d₁ n₂ d₂) :
f (a /. b) (c /. d) = f₁ a b c d /. f₂ a b c d :=
begin
generalize ha : a /. b = x, cases x with n₁ d₁ h₁ c₁, rw num_denom' at ha,
generalize hc : c /. d = x, cases x with n₂ d₂ h₂ c₂, rw num_denom' at hc,
rw fv,
have d₁0 := ne_of_gt (int.coe_nat_lt.2 h₁),
have d₂0 := ne_of_gt (int.coe_nat_lt.2 h₂),
exact (mk_eq (f0 d₁0 d₂0) (f0 b0 d0)).2 (H ((mk_eq b0 d₁0).1 ha) ((mk_eq d0 d₂0).1 hc))
end
@[simp] theorem add_def {a b c d : ℤ} (b0 : b ≠ 0) (d0 : d ≠ 0) :
a /. b + c /. d = (a * d + c * b) /. (b * d) :=
begin
apply lift_binop_eq rat.add; intros; try {assumption},
{ apply mk_pnat_eq },
{ apply mul_ne_zero d₁0 d₂0 },
calc (n₁ * d₂ + n₂ * d₁) * (b * d) =
(n₁ * b) * d₂ * d + (n₂ * d) * (d₁ * b) : by simp [mul_add, mul_comm, mul_left_comm]
... = (a * d₁) * d₂ * d + (c * d₂) * (d₁ * b) : by rw [h₁, h₂]
... = (a * d + c * b) * (d₁ * d₂) : by simp [mul_add, mul_comm, mul_left_comm]
end
protected def neg : ℚ → ℚ
| ⟨n, d, h, c⟩ := ⟨-n, d, h, by simp [c]⟩
instance : has_neg ℚ := ⟨rat.neg⟩
@[simp] theorem neg_def {a b : ℤ} : -(a /. b) = -a /. b :=
begin
by_cases b0 : b = 0, { subst b0, simp, refl },
generalize ha : a /. b = x, cases x with n₁ d₁ h₁ c₁, rw num_denom' at ha,
show rat.mk' _ _ _ _ = _, rw num_denom',
have d0 := ne_of_gt (int.coe_nat_lt.2 h₁),
apply (mk_eq d0 b0).2, have h₁ := (mk_eq b0 d0).1 ha,
simp only [neg_mul_eq_neg_mul_symm, congr_arg has_neg.neg h₁]
end
protected def mul : ℚ → ℚ → ℚ
| ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ := mk_pnat (n₁ * n₂) ⟨d₁ * d₂, mul_pos h₁ h₂⟩
instance : has_mul ℚ := ⟨rat.mul⟩
@[simp] theorem mul_def {a b c d : ℤ} (b0 : b ≠ 0) (d0 : d ≠ 0) :
(a /. b) * (c /. d) = (a * c) /. (b * d) :=
begin
apply lift_binop_eq rat.mul; intros; try {assumption},
{ apply mk_pnat_eq },
{ apply mul_ne_zero d₁0 d₂0 },
cc
end
protected def inv : ℚ → ℚ
| ⟨(n+1:ℕ), d, h, c⟩ := ⟨d, n+1, n.succ_pos, c.symm⟩
| ⟨0, d, h, c⟩ := 0
| ⟨-[1+ n], d, h, c⟩ := ⟨-d, n+1, n.succ_pos, nat.coprime.symm $ by simp; exact c⟩
instance : has_inv ℚ := ⟨rat.inv⟩
@[simp] theorem inv_def {a b : ℤ} : (a /. b)⁻¹ = b /. a :=
begin
by_cases a0 : a = 0, { subst a0, simp, refl },
by_cases b0 : b = 0, { subst b0, simp, refl },
generalize ha : a /. b = x, cases x with n d h c, rw num_denom' at ha,
refine eq.trans (_ : rat.inv ⟨n, d, h, c⟩ = d /. n) _,
{ cases n with n; [cases n with n, skip],
{ refl },
{ change int.of_nat n.succ with (n+1:ℕ),
unfold rat.inv, rw num_denom' },
{ unfold rat.inv, rw num_denom', refl } },
have n0 : n ≠ 0,
{ refine mt (λ (n0 : n = 0), _) a0,
subst n0, simp at ha,
exact (mk_eq_zero b0).1 ha },
have d0 := ne_of_gt (int.coe_nat_lt.2 h),
have ha := (mk_eq b0 d0).1 ha,
apply (mk_eq n0 a0).2,
cc
end
variables (a b c : ℚ)
protected theorem add_zero : a + 0 = a :=
num_denom_cases_on' a $ λ n d h,
by rw [← zero_mk d]; simp [h, -zero_mk]
protected theorem zero_add : 0 + a = a :=
num_denom_cases_on' a $ λ n d h,
by rw [← zero_mk d]; simp [h, -zero_mk]
protected theorem add_comm : a + b = b + a :=
num_denom_cases_on' a $ λ n₁ d₁ h₁,
num_denom_cases_on' b $ λ n₂ d₂ h₂,
by simp [h₁, h₂]; cc
protected theorem add_assoc : a + b + c = a + (b + c) :=
num_denom_cases_on' a $ λ n₁ d₁ h₁,
num_denom_cases_on' b $ λ n₂ d₂ h₂,
num_denom_cases_on' c $ λ n₃ d₃ h₃,
by simp [h₁, h₂, h₃, mul_ne_zero, mul_add, mul_comm, mul_left_comm, add_left_comm, add_assoc]
protected theorem add_left_neg : -a + a = 0 :=
num_denom_cases_on' a $ λ n d h,
by simp [h]
protected theorem mul_one : a * 1 = a :=
num_denom_cases_on' a $ λ n d h,
by change (1:ℚ) with 1 /. 1; simp [h]
protected theorem one_mul : 1 * a = a :=
num_denom_cases_on' a $ λ n d h,
by change (1:ℚ) with 1 /. 1; simp [h]
protected theorem mul_comm : a * b = b * a :=
num_denom_cases_on' a $ λ n₁ d₁ h₁,
num_denom_cases_on' b $ λ n₂ d₂ h₂,
by simp [h₁, h₂, mul_comm]
protected theorem mul_assoc : a * b * c = a * (b * c) :=
num_denom_cases_on' a $ λ n₁ d₁ h₁,
num_denom_cases_on' b $ λ n₂ d₂ h₂,
num_denom_cases_on' c $ λ n₃ d₃ h₃,
by simp [h₁, h₂, h₃, mul_ne_zero, mul_comm, mul_left_comm]
protected theorem add_mul : (a + b) * c = a * c + b * c :=
num_denom_cases_on' a $ λ n₁ d₁ h₁,
num_denom_cases_on' b $ λ n₂ d₂ h₂,
num_denom_cases_on' c $ λ n₃ d₃ h₃,
by simp [h₁, h₂, h₃, mul_ne_zero];
refine (div_mk_div_cancel_left (int.coe_nat_ne_zero.2 h₃)).symm.trans _;
simp [mul_add, mul_comm, mul_assoc, mul_left_comm]
protected theorem mul_add : a * (b + c) = a * b + a * c :=
by rw [rat.mul_comm, rat.add_mul, rat.mul_comm, rat.mul_comm c a]
protected theorem zero_ne_one : 0 ≠ (1:ℚ) :=
mt (λ (h : 0 = 1 /. 1), (mk_eq_zero one_ne_zero).1 h.symm) one_ne_zero
protected theorem mul_inv_cancel : a ≠ 0 → a * a⁻¹ = 1 :=
num_denom_cases_on' a $ λ n d h a0,
have n0 : n ≠ 0, from mt (by intro e; subst e; simp) a0,
by simp [h, n0, mul_comm]; exact
eq.trans (by simp) (@div_mk_div_cancel_left 1 1 _ n0)
protected theorem inv_mul_cancel (h : a ≠ 0) : a⁻¹ * a = 1 :=
eq.trans (rat.mul_comm _ _) (rat.mul_inv_cancel _ h)
instance : decidable_eq ℚ := by tactic.mk_dec_eq_instance
instance : field ℚ :=
{ zero := 0,
add := rat.add,
neg := rat.neg,
one := 1,
mul := rat.mul,
inv := rat.inv,
zero_add := rat.zero_add,
add_zero := rat.add_zero,
add_comm := rat.add_comm,
add_assoc := rat.add_assoc,
add_left_neg := rat.add_left_neg,
mul_one := rat.mul_one,
one_mul := rat.one_mul,
mul_comm := rat.mul_comm,
mul_assoc := rat.mul_assoc,
left_distrib := rat.mul_add,
right_distrib := rat.add_mul,
exists_pair_ne := ⟨0, 1, rat.zero_ne_one⟩,
mul_inv_cancel := rat.mul_inv_cancel,
inv_zero := rfl }
/- Extra instances to short-circuit type class resolution -/
instance : division_ring ℚ := by apply_instance
instance : integral_domain ℚ := by apply_instance
-- TODO(Mario): this instance slows down data.real.basic
--instance : domain ℚ := by apply_instance
instance : nontrivial ℚ := by apply_instance
instance : comm_ring ℚ := by apply_instance
--instance : ring ℚ := by apply_instance
instance : comm_semiring ℚ := by apply_instance
instance : semiring ℚ := by apply_instance
instance : add_comm_group ℚ := by apply_instance
instance : add_group ℚ := by apply_instance
instance : add_comm_monoid ℚ := by apply_instance
instance : add_monoid ℚ := by apply_instance
instance : add_left_cancel_semigroup ℚ := by apply_instance
instance : add_right_cancel_semigroup ℚ := by apply_instance
instance : add_comm_semigroup ℚ := by apply_instance
instance : add_semigroup ℚ := by apply_instance
instance : comm_monoid ℚ := by apply_instance
instance : monoid ℚ := by apply_instance
instance : comm_semigroup ℚ := by apply_instance
instance : semigroup ℚ := by apply_instance
theorem sub_def {a b c d : ℤ} (b0 : b ≠ 0) (d0 : d ≠ 0) :
a /. b - c /. d = (a * d - c * b) /. (b * d) :=
by simp [b0, d0, sub_eq_add_neg]
@[simp] lemma denom_neg_eq_denom : ∀ q : ℚ, (-q).denom = q.denom
| ⟨_, d, _, _⟩ := rfl
@[simp] lemma num_neg_eq_neg_num : ∀ q : ℚ, (-q).num = -(q.num)
| ⟨n, _, _, _⟩ := rfl
@[simp] lemma num_zero : rat.num 0 = 0 := rfl
lemma zero_of_num_zero {q : ℚ} (hq : q.num = 0) : q = 0 :=
have q = q.num /. q.denom, from num_denom.symm,
by simpa [hq]
lemma zero_iff_num_zero {q : ℚ} : q = 0 ↔ q.num = 0 :=
⟨λ _, by simp *, zero_of_num_zero⟩
lemma num_ne_zero_of_ne_zero {q : ℚ} (h : q ≠ 0) : q.num ≠ 0 :=
assume : q.num = 0,
h $ zero_of_num_zero this
@[simp] lemma num_one : (1 : ℚ).num = 1 := rfl
@[simp] lemma denom_one : (1 : ℚ).denom = 1 := rfl
lemma denom_ne_zero (q : ℚ) : q.denom ≠ 0 :=
ne_of_gt q.pos
lemma eq_iff_mul_eq_mul {p q : ℚ} : p = q ↔ p.num * q.denom = q.num * p.denom :=
begin
conv_lhs { rw [←(@num_denom p), ←(@num_denom q)] },
apply rat.mk_eq,
{ exact_mod_cast p.denom_ne_zero },
{ exact_mod_cast q.denom_ne_zero }
end
lemma mk_num_ne_zero_of_ne_zero {q : ℚ} {n d : ℤ} (hq : q ≠ 0) (hqnd : q = n /. d) : n ≠ 0 :=
assume : n = 0,
hq $ by simpa [this] using hqnd
lemma mk_denom_ne_zero_of_ne_zero {q : ℚ} {n d : ℤ} (hq : q ≠ 0) (hqnd : q = n /. d) : d ≠ 0 :=
assume : d = 0,
hq $ by simpa [this] using hqnd
lemma mk_ne_zero_of_ne_zero {n d : ℤ} (h : n ≠ 0) (hd : d ≠ 0) : n /. d ≠ 0 :=
assume : n /. d = 0,
h $ (mk_eq_zero hd).1 this
lemma mul_num_denom (q r : ℚ) : q * r = (q.num * r.num) /. ↑(q.denom * r.denom) :=
have hq' : (↑q.denom : ℤ) ≠ 0, by have := denom_ne_zero q; simpa,
have hr' : (↑r.denom : ℤ) ≠ 0, by have := denom_ne_zero r; simpa,
suffices (q.num /. ↑q.denom) * (r.num /. ↑r.denom) = (q.num * r.num) /. ↑(q.denom * r.denom),
by simpa using this,
by simp [mul_def hq' hr', -num_denom]
lemma div_num_denom (q r : ℚ) : q / r = (q.num * r.denom) /. (q.denom * r.num) :=
if hr : r.num = 0 then
have hr' : r = 0, from zero_of_num_zero hr,
by simp *
else
calc q / r = q * r⁻¹ : div_eq_mul_inv
... = (q.num /. q.denom) * (r.num /. r.denom)⁻¹ : by simp
... = (q.num /. q.denom) * (r.denom /. r.num) : by rw inv_def
... = (q.num * r.denom) /. (q.denom * r.num) : mul_def (by simpa using denom_ne_zero q) hr
lemma num_denom_mk {q : ℚ} {n d : ℤ} (hn : n ≠ 0) (hd : d ≠ 0) (qdf : q = n /. d) :
∃ c : ℤ, n = c * q.num ∧ d = c * q.denom :=
have hq : q ≠ 0, from
assume : q = 0,
hn $ (rat.mk_eq_zero hd).1 (by cc),
have q.num /. q.denom = n /. d, by rwa [num_denom],
have q.num * d = n * ↑(q.denom), from (rat.mk_eq (by simp [rat.denom_ne_zero]) hd).1 this,
begin
existsi n / q.num,
have hqdn : q.num ∣ n, begin rw qdf, apply rat.num_dvd, assumption end,
split,
{ rw int.div_mul_cancel hqdn },
{ apply int.eq_mul_div_of_mul_eq_mul_of_dvd_left,
{ apply rat.num_ne_zero_of_ne_zero hq },
repeat { assumption } }
end
theorem mk_pnat_num (n : ℤ) (d : ℕ+) :
(mk_pnat n d).num = n / nat.gcd n.nat_abs d :=
by cases d; refl
theorem mk_pnat_denom (n : ℤ) (d : ℕ+) :
(mk_pnat n d).denom = d / nat.gcd n.nat_abs d :=
by cases d; refl
theorem mul_num (q₁ q₂ : ℚ) : (q₁ * q₂).num =
(q₁.num * q₂.num) / nat.gcd (q₁.num * q₂.num).nat_abs (q₁.denom * q₂.denom) :=
by cases q₁; cases q₂; refl
theorem mul_denom (q₁ q₂ : ℚ) : (q₁ * q₂).denom =
(q₁.denom * q₂.denom) / nat.gcd (q₁.num * q₂.num).nat_abs (q₁.denom * q₂.denom) :=
by cases q₁; cases q₂; refl
theorem mul_self_num (q : ℚ) : (q * q).num = q.num * q.num :=
by rw [mul_num, int.nat_abs_mul, nat.coprime.gcd_eq_one, int.coe_nat_one, int.div_one];
exact (q.cop.mul_right q.cop).mul (q.cop.mul_right q.cop)
theorem mul_self_denom (q : ℚ) : (q * q).denom = q.denom * q.denom :=
by rw [rat.mul_denom, int.nat_abs_mul, nat.coprime.gcd_eq_one, nat.div_one];
exact (q.cop.mul_right q.cop).mul (q.cop.mul_right q.cop)
lemma add_num_denom (q r : ℚ) : q + r =
((q.num * r.denom + q.denom * r.num : ℤ)) /. (↑q.denom * ↑r.denom : ℤ) :=
have hqd : (q.denom : ℤ) ≠ 0, from int.coe_nat_ne_zero_iff_pos.2 q.3,
have hrd : (r.denom : ℤ) ≠ 0, from int.coe_nat_ne_zero_iff_pos.2 r.3,
by conv_lhs { rw [←@num_denom q, ←@num_denom r, rat.add_def hqd hrd] };
simp [mul_comm]
section casts
theorem coe_int_eq_mk : ∀ (z : ℤ), ↑z = z /. 1
| (n : ℕ) := show (n:ℚ) = n /. 1,
by induction n with n IH n; simp [*, show (1:ℚ) = 1 /. 1, from rfl]
| -[1+ n] := show (-(n + 1) : ℚ) = -[1+ n] /. 1, begin
induction n with n IH, {refl},
show -(n + 1 + 1 : ℚ) = -[1+ n.succ] /. 1,
rw [neg_add, IH],
simpa [show -1 = (-1) /. 1, from rfl]
end
theorem mk_eq_div (n d : ℤ) : n /. d = ((n : ℚ) / d) :=
begin
by_cases d0 : d = 0, {simp [d0, div_zero]},
simp [division_def, coe_int_eq_mk, mul_def one_ne_zero d0]
end
theorem coe_int_eq_of_int (z : ℤ) : ↑z = of_int z :=
(coe_int_eq_mk z).trans (of_int_eq_mk z).symm
@[simp, norm_cast] theorem coe_int_num (n : ℤ) : (n : ℚ).num = n :=
by rw coe_int_eq_of_int; refl
@[simp, norm_cast] theorem coe_int_denom (n : ℤ) : (n : ℚ).denom = 1 :=
by rw coe_int_eq_of_int; refl
lemma coe_int_num_of_denom_eq_one {q : ℚ} (hq : q.denom = 1) : ↑(q.num) = q :=
by { conv_rhs { rw [←(@num_denom q), hq] }, rw [coe_int_eq_mk], refl }
lemma denom_eq_one_iff (r : ℚ) : r.denom = 1 ↔ ↑r.num = r :=
⟨rat.coe_int_num_of_denom_eq_one, λ h, h ▸ rat.coe_int_denom r.num⟩
instance : can_lift ℚ ℤ :=
⟨coe, λ q, q.denom = 1, λ q hq, ⟨q.num, coe_int_num_of_denom_eq_one hq⟩⟩
theorem coe_nat_eq_mk (n : ℕ) : ↑n = n /. 1 :=
by rw [← int.cast_coe_nat, coe_int_eq_mk]
@[simp, norm_cast] theorem coe_nat_num (n : ℕ) : (n : ℚ).num = n :=
by rw [← int.cast_coe_nat, coe_int_num]
@[simp, norm_cast] theorem coe_nat_denom (n : ℕ) : (n : ℚ).denom = 1 :=
by rw [← int.cast_coe_nat, coe_int_denom]
-- Will be subsumed by `int.coe_inj` after we have defined
-- `linear_ordered_field ℚ` (which implies characteristic zero).
lemma coe_int_inj (m n : ℤ) : (m : ℚ) = n ↔ m = n :=
⟨λ h, by simpa using congr_arg num h, congr_arg _⟩
end casts
lemma inv_def' {q : ℚ} : q⁻¹ = (q.denom : ℚ) / q.num :=
by { conv_lhs { rw ←(@num_denom q) }, cases q, simp [div_num_denom] }
@[simp] lemma mul_denom_eq_num {q : ℚ} : q * q.denom = q.num :=
begin
suffices : mk (q.num) ↑(q.denom) * mk ↑(q.denom) 1 = mk (q.num) 1, by
{ conv { for q [1] { rw ←(@num_denom q) }}, rwa [coe_int_eq_mk, coe_nat_eq_mk] },
have : (q.denom : ℤ) ≠ 0, from ne_of_gt (by exact_mod_cast q.pos),
rw [(rat.mul_def this one_ne_zero), (mul_comm (q.denom : ℤ) 1), (div_mk_div_cancel_left this)]
end
lemma denom_div_cast_eq_one_iff (m n : ℤ) (hn : n ≠ 0) :
((m : ℚ) / n).denom = 1 ↔ n ∣ m :=
begin
replace hn : (n:ℚ) ≠ 0, by rwa [ne.def, ← int.cast_zero, coe_int_inj],
split,
{ intro h,
lift ((m : ℚ) / n) to ℤ using h with k hk,
use k,
rwa [eq_div_iff_mul_eq hn, ← int.cast_mul, mul_comm, eq_comm, coe_int_inj] at hk },
{ rintros ⟨d, rfl⟩,
rw [int.cast_mul, mul_comm, mul_div_cancel _ hn, rat.coe_int_denom] }
end
lemma num_div_eq_of_coprime {a b : ℤ} (hb0 : 0 < b) (h : nat.coprime a.nat_abs b.nat_abs) :
(a / b : ℚ).num = a :=
begin
lift b to ℕ using le_of_lt hb0,
norm_cast at hb0 h,
rw [← rat.mk_eq_div, ← rat.mk_pnat_eq a b hb0, rat.mk_pnat_num, pnat.mk_coe, h.gcd_eq_one,
int.coe_nat_one, int.div_one]
end
lemma denom_div_eq_of_coprime {a b : ℤ} (hb0 : 0 < b) (h : nat.coprime a.nat_abs b.nat_abs) :
((a / b : ℚ).denom : ℤ) = b :=
begin
lift b to ℕ using le_of_lt hb0,
norm_cast at hb0 h,
rw [← rat.mk_eq_div, ← rat.mk_pnat_eq a b hb0, rat.mk_pnat_denom, pnat.mk_coe, h.gcd_eq_one,
nat.div_one]
end
lemma div_int_inj {a b c d : ℤ} (hb0 : 0 < b) (hd0 : 0 < d)
(h1 : nat.coprime a.nat_abs b.nat_abs) (h2 : nat.coprime c.nat_abs d.nat_abs)
(h : (a : ℚ) / b = (c : ℚ) / d) : a = c ∧ b = d :=
begin
apply and.intro,
{ rw [← (num_div_eq_of_coprime hb0 h1), h, num_div_eq_of_coprime hd0 h2] },
{ rw [← (denom_div_eq_of_coprime hb0 h1), h, denom_div_eq_of_coprime hd0 h2] }
end
@[norm_cast] lemma coe_int_div_self (n : ℤ) : ((n / n : ℤ) : ℚ) = n / n :=
begin
by_cases hn : n = 0,
{ subst hn, simp only [int.cast_zero, euclidean_domain.zero_div] },
{ have : (n : ℚ) ≠ 0, { rwa ← coe_int_inj at hn },
simp only [int.div_self hn, int.cast_one, ne.def, not_false_iff, div_self this] }
end
@[norm_cast] lemma coe_nat_div_self (n : ℕ) : ((n / n : ℕ) : ℚ) = n / n :=
coe_int_div_self n
lemma coe_int_div (a b : ℤ) (h : b ∣ a) : ((a / b : ℤ) : ℚ) = a / b :=
begin
rcases h with ⟨c, rfl⟩,
simp only [mul_comm b, int.mul_div_assoc c (dvd_refl b), int.cast_mul, mul_div_assoc, coe_int_div_self]
end
lemma coe_nat_div (a b : ℕ) (h : b ∣ a) : ((a / b : ℕ) : ℚ) = a / b :=
begin
rcases h with ⟨c, rfl⟩,
simp only [mul_comm b, nat.mul_div_assoc c (dvd_refl b), nat.cast_mul, mul_div_assoc, coe_nat_div_self]
end
protected lemma «forall» {p : ℚ → Prop} : (∀ r, p r) ↔ ∀ a b : ℤ, p (a / b) :=
⟨λ h _ _, h _,
λ h q, (show q = q.num / q.denom, from by simp [rat.div_num_denom]).symm ▸ (h q.1 q.2)⟩
protected lemma «exists» {p : ℚ → Prop} : (∃ r, p r) ↔ ∃ a b : ℤ, p (a / b) :=
⟨λ ⟨r, hr⟩, ⟨r.num, r.denom, by rwa [← mk_eq_div, num_denom]⟩, λ ⟨a, b, h⟩, ⟨_, h⟩⟩
end rat
|
042880c7a2302aca25cc7b944c12c5f9cfcbc578 | 020c82b947b28c4d255384a0466715fbb44e5c1e | /src/fifteenwidget.lean | a0482395682fae248fe1d36b2fac7c1f259c975f | [] | no_license | SnobbyDragon/leanfifteen | 79ceb751749fa0185c4f9e60ffa2f128e64fbc3c | 4583ab44e1de89a25e693e5e611472a9ba1147b6 | refs/heads/main | 1,675,887,746,364 | 1,609,534,902,000 | 1,609,534,902,000 | 325,708,959 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,538 | lean | import fifteen fifteentactics
open fifteen fifteen.tile fifteen.position
open widget widget.html widget.attr
open tactic fifteentactics
variable {α : Type}
/-
As always...
DUOLINGO COLORS ARE AESTHETIC <3
https://design.duolingo.com/identity/color
goal_position will be a rainbow :)
-/
inductive color : Type
| cardinal -- red
| fox -- orange
| bee -- yellow
| mask_green -- green
| macaw -- blue
| beetle -- purple
| wolf -- dark mode background
| white -- light mode background
open color
instance : has_to_string color :=
⟨ λ c, match c with
| cardinal := "#ff4b4b"
| fox := "#ff9600"
| bee := "#ffc800"
| mask_green := "#89e219"
| macaw := "#1cb0f6"
| beetle := "#ce82ff"
| wolf := "#777777"
| white := "#ffffff"
end
⟩
-- TASTE THE RAINBOW !
def tile_colors' : fin 16 → color
| ⟨0,_⟩ := wolf -- empty tile in dark mode
| ⟨1,_⟩ := cardinal
| ⟨2,_⟩ := fox --| ⟨5,_⟩ := fox
| ⟨3,_⟩ := bee --| ⟨6,_⟩ := bee | ⟨9,_⟩ := bee
| ⟨4,_⟩ := mask_green --| ⟨7,_⟩ := mask_green | ⟨10,_⟩ := mask_green | ⟨13,_⟩ := mask_green
| ⟨8,_⟩ := macaw --| ⟨11,_⟩ := macaw | ⟨14,_⟩ := macaw
| ⟨12,_⟩ := beetle --| ⟨15,_⟩ := beetle
| ⟨n+3,_⟩ := tile_colors' ⟨n,by linarith⟩ using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf subtype.val⟩]}
def tile_colors (t : tile) (p : position) : color := tile_colors' $ p.map t
-- raised tile look :)
def tile_border_styles (t : tile) (p : position) : string :=
if hole t p then "none" else "outset"
def tile_border_widths' : tile → string
| aa := "3px 1px 1px 3px" -- top left corner
| ab := "3px 1px 1px 1px" | ac := "3px 1px 1px 1px" -- top edges
| ad := "3px 3px 1px 1px" -- top right corner
| bd := "1px 3px 1px 1px" | cd := "1px 3px 1px 1px" -- right edges
| dd := "1px 3px 3px 1px" -- bottom right corner
| dc := "1px 1px 3px 1px" | db := "1px 1px 3px 1px" -- bottom edges
| da := "1px 1px 3px 3px" -- bottom left corner
| ca := "1px 1px 1px 3px" | ba := "1px 1px 1px 3px" -- left edges
| _ := "1px" -- inner
-- TODO: need higher outset near empty tile; need to calculate edges bordering empty tile
def tile_border_widths (t : tile) (p : position) : string := tile_border_widths' t
def tile_text (t : tile) (p : position) : string :=
if hole t p then "" else to_string $ p.map t
def tile_html_style (t : tile) (p : position) : list (string × string) :=
[
("background-color", to_string $ tile_colors t p),
("width", "50px"),
("height", "50px"),
("border-color", to_string white),
("border-style", tile_border_styles t p),
("border-width", tile_border_widths t p),
("text-align", "center"), -- horizontally center text
("line-height", "45px"), -- vertically center text
("color", to_string white),
("font", "24px Comic Sans MS") -- THE BEST FONT
]
def position_html_style (p : position) : list (string × string) :=
[
("display", "grid"),
("grid-template", "repeat(4, 1fr) / repeat(4, 1fr)"),
("width", "200px"),
("margin", "10px")
]
section static
meta def tile_html (t : tile) (p : position) : html empty :=
h "div" [
style $ tile_html_style t p
] [tile_text t p]
meta def tiles_html (p : position) : list (html empty) :=
list.map (λ t : tile, tile_html t p) tiles_list
meta def position_html (p : position) : html empty :=
h "div" [
style $ position_html_style p
] (tiles_html p)
#html position_html goal_position
meta def solved : tactic (list (html empty)) :=
do { gs ← get_goals,
if gs.length = 0 then return [widget_override.goals_accomplished_message]
else tactic.fail "there are still goals!"
} <|> tactic.fail "bad vibes"
meta def fifteen_widget : tactic (list (html empty)) :=
do { p ← get_position,
return [position_html p]
} <|> solved
meta def fifteen_component : tc unit empty := tc.stateless (λ u, fifteen_widget)
meta def fifteen_save_info (p : pos) : tactic unit :=
do tactic.save_widget p (widget.tc.to_component fifteen_component)
end static
/-
Super thank you to Ed Ayers!
This is basically a copy of his interactive Hanoi widget.
-/
section interactive
-- TODO: make a sliding animation :)
meta def tile_html' (t : tile) (p : position) : html tile :=
h "div" [
style $ tile_html_style t p,
on_click (λ x, t)
] [tile_text t p]
meta def tiles_html' (p : position) : list (html tile) :=
list.map (λ t : tile, widget.html.map_action (λ t', t') (tile_html' t p)) tiles_list
meta def position_html' (p : position) : html tile :=
h "div" [
style $ position_html_style p
] (tiles_html' p)
meta structure fifteen_state :=
(slides : list (tile))
(initpos : position)
(inithole : tile)
(pos : position)
(hole : tile)
meta inductive fifteen_action
| click_tile (t : tile)
| commit
| reset
open fifteen_action
meta def fifteen_view : fifteen_state → list (html fifteen_action)
| s := [widget.html.map_action click_tile $ position_html' s.pos]
++ (if s.initpos = goal_position then [widget_override.goals_accomplished_message]
else [button "yeehaw" (fifteen_action.commit), button "aw hell naw" (fifteen_action.reset)])
meta def fifteen_update : fifteen_state → fifteen_action → fifteen_state × option widget.effects
| s (click_tile t) :=
if t ∈ get_adjacent s.hole -- checks if the move is valid
then ({slides := s.slides ++ [t], initpos := s.initpos, inithole := s.inithole, pos := slide t s.hole s.pos, hole := t}, none)
else (s, none)
| s commit := (s, some [effect.insert_text $ string.join $ s.slides.map (λ t, "slide_tile " ++ to_string t ++ ",\n")])
| s reset := ({slides := [], initpos := s.initpos, inithole := s.inithole, pos := s.initpos, hole := s.inithole}, none)
meta def solved' : tactic fifteen_state :=
do { gs ← get_goals,
if gs.length = 0
then return {slides := [], initpos := goal_position, inithole := dd, pos := goal_position, hole := dd}
else tactic.fail "there are still goals !"
} <|> tactic.fail "bad vibes"
meta def fifteen_init : unit → tactic fifteen_state
| () := do {
p ← get_position,
h ← get_hole p,
return {slides := [], initpos := p, inithole := h, pos := p, hole := h}
} <|> solved'
meta def fifteen_component' : tc unit empty :=
component.ignore_action
$ component.with_effects (λ _ e, e)
$ tc.mk_simple fifteen_action fifteen_state fifteen_init (λ _ s a, pure $ fifteen_update s a) (λ _ s, pure $ fifteen_view s)
meta def fifteen_save_info' (p : pos) : tactic unit :=
do tactic.save_widget p (widget.tc.to_component fifteen_component')
end interactive |
3a78ed606167c815a67a20bc9c821499ef4f2dda | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/nonAtomicFieldName.lean | afe031250b035e41955349bb091a3937a0f8ffb7 | [
"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 | 30 | lean | structure A where
a.b : Nat
|
a70da91dea03c0b32cce3a1912fe508508a06d13 | 8e31b9e0d8cec76b5aa1e60a240bbd557d01047c | /src/tableau.lean | 142bc6ad6a3c1f673f5c7e54eef539fdd8c7bf2c | [] | no_license | ChrisHughes24/LP | 7bdd62cb648461c67246457f3ddcb9518226dd49 | e3ed64c2d1f642696104584e74ae7226d8e916de | refs/heads/master | 1,685,642,642,858 | 1,578,070,602,000 | 1,578,070,602,000 | 195,268,102 | 4 | 3 | null | 1,569,229,518,000 | 1,562,255,287,000 | Lean | UTF-8 | Lean | false | false | 33,040 | lean | import data.matrix.pequiv data.rat.basic tactic.fin_cases data.list.min_max partition
open matrix fintype finset function pequiv
local notation `rvec`:2000 n := matrix (fin 1) (fin n) ℚ
local notation `cvec`:2000 m := matrix (fin m) (fin 1) ℚ
local infix ` ⬝ `:70 := matrix.mul
local postfix `ᵀ` : 1500 := transpose
section
universes u v
variables {l m n o : Type u} [fintype l] [fintype m] [fintype n] [fintype o] {R : Type v}
/- Belongs in mathlib -/
lemma mul_right_eq_of_mul_eq [semiring R] {M : matrix l m R} {N : matrix m n R} {O : matrix l n R}
{P : matrix n o R} (h : M ⬝ N = O) : M ⬝ (N ⬝ P) = O ⬝ P :=
by rw [← matrix.mul_assoc, h]
end
variables {m n : ℕ}
/-- The tableau consists of a matrix and a kant `const` column.
`to_partition` stores the indices of the current row and column variables.
`restricted` is the set of variables that are restricted to be nonnegative -/
structure tableau (m n : ℕ) extends partition m n :=
(to_matrix : matrix (fin m) (fin n) ℚ)
(const : cvec m)
(restricted : finset (fin (m + n)))
(dead : finset (fin n))
namespace tableau
open partition
section predicates
variable (T : tableau m n)
/-- The affine subspace represented by the tableau ignoring nonnegativity restrictiions -/
def flat : set (cvec (m + n)) :=
{ x | T.to_partition.rowp.to_matrixᵀ ⬝ x = T.to_matrix ⬝ T.to_partition.colp.to_matrixᵀ ⬝ x + T.const }
/-- The res_set is the subset of ℚ^(m+n) that satisifies the nonnegativity constraints of
the tableau, and the affine conditions -/
def res_set : set (cvec (m + n)) := flat T ∩ { x | ∀ i, i ∈ T.restricted → 0 ≤ x i 0 }
/-- The dead_set is the subset of ℚ^(m+n) such that all dead variables are zero, and satisfies
the affine conditions -/
def dead_set : set (cvec (m + n)) :=
flat T ∩ { x | ∀ j, j ∈ T.dead → x (T.to_partition.colg j) 0 = 0 }
/-- The `sol_set` is the set of vector that satisfy the affine constraints the dead variable
constraints, and the nonnegativity constraints. -/
def sol_set : set (cvec (m + n)) :=
res_set T ∩ { x | ∀ j, j ∈ T.dead → x (T.to_partition.colg j) 0 = 0 }
lemma sol_set_eq_res_set_inter :
T.sol_set = res_set T ∩ { x | ∀ j, j ∈ T.dead → x (T.to_partition.colg j) 0 = 0 } := rfl
lemma sol_set_eq_dead_set_inter :
T.sol_set = dead_set T ∩ { x | ∀ i, i ∈ T.restricted → 0 ≤ x i 0 } :=
set.inter_right_comm _ _ _
lemma sol_set_eq_res_set_inter_dead_set : T.sol_set = T.res_set ∩ T.dead_set :=
by simp [sol_set, res_set, dead_set, set.ext_iff]; tauto
/-- Predicate for a variable being unbounded above in the `res_set` -/
def is_unbounded_above (i : fin (m + n)) : Prop :=
∀ q : ℚ, ∃ x : cvec (m + n), x ∈ sol_set T ∧ q ≤ x i 0
/-- Predicate for a variable being unbounded below in the `res_set` -/
def is_unbounded_below (i : fin (m + n)) : Prop :=
∀ q : ℚ, ∃ x : cvec (m + n), x ∈ sol_set T ∧ x i 0 ≤ q
def is_optimal (x : cvec (m + n)) (i : fin (m + n)) : Prop :=
x ∈ T.sol_set ∧ ∀ y : cvec (m + n), y ∈ sol_set T → y i 0 ≤ x i 0
/-- Is this equivalent to `∀ (x : cvec (m + n)), x ∈ res_set T → x i 0 = x j 0`? No -/
def equal_in_flat (i j : fin (m + n)) : Prop :=
∀ (x : cvec (m + n)), x ∈ flat T → x i 0 = x j 0
/-- Returns an element of the `flat` after assigning values to the column variables -/
def of_col (T : tableau m n) (x : cvec n) : cvec (m + n) :=
T.to_partition.colp.to_matrix ⬝ x + T.to_partition.rowp.to_matrix ⬝ (T.to_matrix ⬝ x + T.const)
/-- A `tableau` is feasible if its `const` column is nonnegative in restricted rows -/
def feasible : Prop :=
∀ i, T.to_partition.rowg i ∈ T.restricted → 0 ≤ T.const i 0
instance : decidable_pred (@feasible m n) := λ _, by dunfold feasible; apply_instance
/-- Given a row index `r` and a column index `s` it returns a tableau with `r` and `s` switched,
but with the same `res_set` -/
def pivot (i : fin m) (j : fin n) : tableau m n :=
let p := (T.to_matrix i j)⁻¹ in
{ to_matrix := λ i' j',
if i' = i
then if j' = j
then p
else -T.to_matrix i' j' * p
else if j' = j
then T.to_matrix i' j * p
else T.to_matrix i' j' - T.to_matrix i' j * T.to_matrix i j' * p,
to_partition := T.to_partition.swap i j,
const := λ i' k,
if i' = i
then -T.const i k * p
else T.const i' k - T.to_matrix i' j * T.const i k * p,
restricted := T.restricted,
dead := T.dead }
def restrict (T : tableau m n) (v : fin (m + n)) : tableau m n :=
{ restricted := insert v T.restricted,
..T }
-- def kill_col (T : tableau m n) (j : fin n) : tableau m n :=
-- { dead := insert c T.dead,
-- ..T }
end predicates
section predicate_lemmas
variable {T : tableau m n}
@[simp] lemma eta : tableau.mk T.to_partition T.to_matrix T.const T.restricted T.dead = T :=
by cases T; refl
lemma mem_flat_iff {x : cvec (m + n)} : x ∈ T.flat ↔
∀ i, x (T.to_partition.rowg i) 0 = univ.sum
(λ j : fin n, T.to_matrix i j * x (T.to_partition.colg j) 0) +
T.const i 0 :=
have hx : x ∈ T.flat ↔ ∀ i, (T.to_partition.rowp.to_matrixᵀ ⬝ x) i 0 =
(T.to_matrix ⬝ T.to_partition.colp.to_matrixᵀ ⬝ x + T.const) i 0,
by rw [flat, set.mem_set_of_eq, matrix.ext_iff.symm, forall_swap,
unique.forall_iff];
refl,
begin
rw hx,
refine forall_congr (λ i, _),
rw [← to_matrix_symm, mul_matrix_apply, add_val, rowp_symm_eq_some_rowg,
matrix.mul_assoc, matrix.mul],
conv in (T.to_matrix _ _ * (T.to_partition.colp.to_matrixᵀ ⬝ x) _ _)
{ rw [← to_matrix_symm, mul_matrix_apply, colp_symm_eq_some_colg] }
end
variable (T)
@[simp] lemma colp_mul_of_col (x : cvec n) :
T.to_partition.colp.to_matrixᵀ ⬝ of_col T x = x :=
by simp [matrix.mul_assoc, matrix.mul_add, of_col, flat,
mul_right_eq_of_mul_eq (rowp_transpose_mul_colp _),
mul_right_eq_of_mul_eq (rowp_transpose_mul_rowp _),
mul_right_eq_of_mul_eq (colp_transpose_mul_colp _),
mul_right_eq_of_mul_eq (colp_transpose_mul_rowp _)]
@[simp] lemma rowp_mul_of_col (x : cvec n) :
T.to_partition.rowp.to_matrixᵀ ⬝ of_col T x = T.to_matrix ⬝ x + T.const :=
by simp [matrix.mul_assoc, matrix.mul_add, of_col, flat,
mul_right_eq_of_mul_eq (rowp_transpose_mul_colp _),
mul_right_eq_of_mul_eq (rowp_transpose_mul_rowp _),
mul_right_eq_of_mul_eq (colp_transpose_mul_colp _),
mul_right_eq_of_mul_eq (colp_transpose_mul_rowp _)]
lemma of_col_mem_flat (x : cvec n) : T.of_col x ∈ T.flat :=
by simp [matrix.mul_assoc, matrix.mul_add, flat]
@[simp] lemma of_col_colg (x : cvec n) (j : fin n) :
of_col T x (T.to_partition.colg j) = x j :=
funext $ λ v,
calc of_col T x (T.to_partition.colg j) v =
(T.to_partition.colp.to_matrixᵀ ⬝ of_col T x) j v :
by rw [← to_matrix_symm, mul_matrix_apply, colp_symm_eq_some_colg]
... = x j v : by rw [colp_mul_of_col]
lemma of_col_rowg (c : cvec n) (i : fin m) :
of_col T c (T.to_partition.rowg i) = (T.to_matrix ⬝ c + T.const) i :=
funext $ λ v,
calc of_col T c (T.to_partition.rowg i) v =
(T.to_partition.rowp.to_matrixᵀ ⬝ of_col T c) i v :
by rw [← to_matrix_symm, mul_matrix_apply, rowp_symm_eq_some_rowg]
... = (T.to_matrix ⬝ c + T.const) i v : by rw [rowp_mul_of_col]
variable {T}
lemma of_col_single_rowg {q : ℚ} {i j} {k} :
T.of_col (q • (single j 0).to_matrix) (T.to_partition.rowg i) k =
q * T.to_matrix i j + T.const i k:=
begin
fin_cases k,
erw [of_col_rowg, matrix.mul_smul, matrix.add_val, matrix.smul_val,
matrix_mul_apply, pequiv.symm_single_apply]
end
/-- Condition for the solution given by setting column index `j` to `q` and all other columns to
zero being in the `res_set` -/
lemma of_col_single_mem_res_set {q : ℚ} {j : fin n} (hT : T.feasible)
(hi : ∀ i, T.to_partition.rowg i ∈ T.restricted → 0 ≤ q * T.to_matrix i j)
(hj : T.to_partition.colg j ∉ T.restricted ∨ 0 ≤ q) :
T.of_col (q • (single j 0).to_matrix) ∈ T.res_set :=
⟨of_col_mem_flat _ _,
λ v, (T.to_partition.eq_rowg_or_colg v).elim
begin
rintros ⟨i', hi'⟩ hres,
subst hi',
rw [of_col_single_rowg],
exact add_nonneg (hi _ hres) (hT _ hres)
end
begin
rintros ⟨j', hj'⟩ hres,
subst hj',
simp [of_col_colg, smul_val, pequiv.single, pequiv.to_matrix],
by_cases hjc : j' = j; simp [*, le_refl] at *
end⟩
lemma of_col_single_mem_sol_set {q : ℚ} {j : fin n} (hT : T.feasible)
(hi : ∀ i, T.to_partition.rowg i ∈ T.restricted → 0 ≤ q * T.to_matrix i j)
(hj : T.to_partition.colg j ∉ T.restricted ∨ 0 ≤ q) (hdead : j ∉ T.dead ∨ q = 0):
T.of_col (q • (single j 0).to_matrix) ∈ T.sol_set :=
⟨of_col_single_mem_res_set hT hi hj,
λ j' hj', classical.by_cases
(λ hjj' : j' = j, by subst hjj'; simp * at *)
(λ hjj' : j' ≠ j, by simp [smul_val, pequiv.single, pequiv.to_matrix, hjj'])⟩
@[simp] lemma of_col_zero_mem_res_set_iff : T.of_col 0 ∈ T.res_set ↔ T.feasible :=
suffices (∀ v : fin (m + n), v ∈ T.restricted → (0 : ℚ) ≤ T.of_col 0 v 0) ↔
(∀ i : fin m, T.to_partition.rowg i ∈ T.restricted → 0 ≤ T.const i 0),
by simpa [res_set, feasible, of_col_mem_flat],
⟨λ h i hi, by simpa [of_col_rowg] using h _ hi,
λ h v hv, (T.to_partition.eq_rowg_or_colg v).elim
(by rintros ⟨i, hi⟩; subst hi; simp [of_col_rowg]; tauto)
(by rintros ⟨j, hj⟩; subst hj; simp)⟩
@[simp] lemma of_col_zero_mem_sol_set_iff : T.of_col 0 ∈ T.sol_set ↔ T.feasible :=
by simp [sol_set, of_col_zero_mem_res_set_iff]
@[simp] lemma to_matrix_restrict (v : fin (m + n)) : (T.restrict v).to_matrix = T.to_matrix := rfl
@[simp] lemma const_restrict (v : fin (m + n)) : (T.restrict v).const = T.const := rfl
@[simp] lemma to_partition_restrict (v : fin (m + n)) :
(T.restrict v).to_partition = T.to_partition := rfl
@[simp] lemma dead_restrict (v : fin (m + n)) : (T.restrict v).dead = T.dead := rfl
@[simp] lemma restricted_restrict (v : fin (m + n)) :
(T.restrict v).restricted = insert v T.restricted := rfl
@[simp] lemma flat_restrict (v : fin (m + n)) : (T.restrict v).flat = T.flat := by simp [flat]
@[simp] lemma dead_set_restrict (v : fin (m + n)) : (T.restrict v).dead_set = T.dead_set :=
by simp [dead_set]
lemma res_set_restrict (v : fin (m + n)) : (T.restrict v).res_set =
T.res_set ∩ {x | 0 ≤ x v 0} :=
begin
simp only [res_set, flat_restrict, set.inter_assoc],
ext x,
exact ⟨λ h, ⟨h.1, λ i hi, h.2 _ $ by simp [hi], h.2 _ $ by simp⟩,
λ h, ⟨h.1, λ i hi, (mem_insert.1 hi).elim (λ hv, hv.symm ▸ h.2.2) $ h.2.1 _⟩⟩
end
lemma sol_set_restrict (v : fin (m + n)) : (T.restrict v).sol_set =
T.sol_set ∩ {x | 0 ≤ x v 0} :=
by simp [sol_set_eq_res_set_inter_dead_set, res_set_restrict, set.inter_comm, set.inter_assoc,
set.inter_left_comm]
lemma feasible_restrict {v : fin (m + n)} (hfT : T.feasible)
(h : (∃ c, v = T.to_partition.colg c) ∨ ∃ i, v = T.to_partition.rowg i ∧ 0 ≤ T.const i 0) :
(T.restrict v).feasible :=
h.elim
(λ ⟨c, hc⟩, hc.symm ▸ λ i hi, hfT _ $ by simpa using hi)
(λ ⟨i, hiv, hi⟩, hiv.symm ▸ λ i' hi', (mem_insert.1 hi').elim
(λ h, by simp [T.to_partition.injective_rowg.eq_iff, *] at *)
(λ hres, hfT _ $ by simpa using hres))
lemma is_unbounded_above_colg_aux {j : fin n} (hT : T.feasible) (hdead : j ∉ T.dead)
(h : ∀ i : fin m, T.to_partition.rowg i ∈ T.restricted → 0 ≤ T.to_matrix i j) (q : ℚ) :
of_col T (max q 0 • (single j 0).to_matrix) ∈ sol_set T ∧
q ≤ of_col T (max q 0 • (single j 0).to_matrix) (T.to_partition.colg j) 0 :=
⟨of_col_single_mem_sol_set hT (λ i hi, mul_nonneg (le_max_right _ _) (h _ hi))
(or.inr (le_max_right _ _)) (or.inl hdead),
by simp [of_col_colg, smul_val, pequiv.single, pequiv.to_matrix, le_refl q]⟩
/-- A column variable is unbounded above if it is in a column where every negative entry is
in a row owned by an unrestricted variable -/
lemma is_unbounded_above_colg {j : fin n} (hT : T.feasible) (hdead : j ∉ T.dead)
(h : ∀ i : fin m, T.to_partition.rowg i ∈ T.restricted → 0 ≤ T.to_matrix i j) :
T.is_unbounded_above (T.to_partition.colg j) :=
λ q, ⟨_, is_unbounded_above_colg_aux hT hdead h q⟩
lemma is_unbounded_below_colg_aux {j : fin n} (hT : T.feasible)
(hres : T.to_partition.colg j ∉ T.restricted) (hdead : j ∉ T.dead)
(h : ∀ i : fin m, T.to_partition.rowg i ∈ T.restricted → T.to_matrix i j ≤ 0) (q : ℚ) :
of_col T (min q 0 • (single j 0).to_matrix) ∈ sol_set T ∧
of_col T (min q 0 • (single j 0).to_matrix) (T.to_partition.colg j) 0 ≤ q :=
⟨of_col_single_mem_sol_set hT
(λ i hi, mul_nonneg_of_nonpos_of_nonpos (min_le_right _ _) (h _ hi))
(or.inl hres) (or.inl hdead),
by simp [of_col_colg, smul_val, pequiv.single, pequiv.to_matrix, le_refl q]⟩
/-- A column variable is unbounded below if it is unrestricted and it is in a column where every
positive entry is in a row owned by an unrestricted variable -/
lemma is_unbounded_below_colg {j : fin n} (hT : T.feasible)
(hres : T.to_partition.colg j ∉ T.restricted) (hdead : j ∉ T.dead)
(h : ∀ i : fin m, T.to_partition.rowg i ∈ T.restricted → T.to_matrix i j ≤ 0) :
T.is_unbounded_below (T.to_partition.colg j) :=
λ q, ⟨_, is_unbounded_below_colg_aux hT hres hdead h q⟩
/-- A row variable `r` is unbounded above if it is unrestricted and there is a column `s`
where every restricted row variable has a nonpositive entry in that column, and
`r` has a negative entry in that column. -/
lemma is_unbounded_above_rowg_of_nonpos {i : fin m} (hT : T.feasible) (j : fin n)
(hres : T.to_partition.colg j ∉ T.restricted) (hdead : j ∉ T.dead)
(h : ∀ i : fin m, T.to_partition.rowg i ∈ T.restricted → T.to_matrix i j ≤ 0)
(his : T.to_matrix i j < 0) : is_unbounded_above T (T.to_partition.rowg i) :=
λ q, ⟨T.of_col (min ((q - T.const i 0) / T.to_matrix i j) 0 • (single j 0).to_matrix),
of_col_single_mem_sol_set hT
(λ i' hi', mul_nonneg_of_nonpos_of_nonpos (min_le_right _ _) (h _ hi'))
(or.inl hres) (or.inl hdead),
begin
rw [of_col_rowg, add_val, matrix.mul_smul, smul_val, matrix_mul_apply,
symm_single_apply],
cases le_total 0 ((q - T.const i 0) / T.to_matrix i j) with hq hq,
{ rw [min_eq_right hq],
rw [le_div_iff_of_neg his, zero_mul, sub_nonpos] at hq,
simpa },
{ rw [min_eq_left hq, div_mul_cancel _ (ne_of_lt his)],
simp }
end⟩
/-- A row variable `i` is unbounded above if there is a column `s`
where every restricted row variable has a nonpositive entry in that column, and
`i` has a positive entry in that column. -/
lemma is_unbounded_above_rowg_of_nonneg {i : fin m} (hT : T.feasible) (j : fin n)
(hs : ∀ i' : fin m, T.to_partition.rowg i' ∈ T.restricted → 0 ≤ T.to_matrix i' j) (hdead : j ∉ T.dead)
(his : 0 < T.to_matrix i j) : is_unbounded_above T (T.to_partition.rowg i) :=
λ q, ⟨T.of_col (max ((q - T.const i 0) / T.to_matrix i j) 0 • (single j 0).to_matrix),
of_col_single_mem_sol_set hT
(λ i hi, mul_nonneg (le_max_right _ _) (hs i hi))
(or.inr (le_max_right _ _)) (or.inl hdead),
begin
rw [of_col_rowg, add_val, matrix.mul_smul, smul_val, matrix_mul_apply,
symm_single_apply],
cases le_total ((q - T.const i 0) / T.to_matrix i j) 0 with hq hq,
{ rw [max_eq_right hq],
rw [div_le_iff his, zero_mul, sub_nonpos] at hq,
simpa },
{ rw [max_eq_left hq, div_mul_cancel _ (ne_of_gt his)],
simp }
end⟩
/-- The sample solution of a feasible tableau maximises the variable in row `r`,
if every entry in that row is nonpositive and every entry in that row owned by a restricted
variable is `0` -/
lemma is_optimal_of_col_zero {obj : fin m} (hf : T.feasible)
(h : ∀ j, j ∉ T.dead → T.to_matrix obj j ≤ 0 ∧
(T.to_partition.colg j ∉ T.restricted → T.to_matrix obj j = 0)) :
T.is_optimal (T.of_col 0) (T.to_partition.rowg obj) :=
⟨of_col_zero_mem_sol_set_iff.2 hf, λ x hx, begin
rw [of_col_rowg, matrix.mul_zero, zero_add, mem_flat_iff.1 hx.1.1],
refine add_le_of_nonpos_of_le _ (le_refl _),
refine sum_nonpos (λ j _, _),
by_cases hj : (T.to_partition.colg j) ∈ T.restricted,
{ by_cases hdead : j ∈ T.dead,
{ simp [hx.2 _ hdead] },
{ exact mul_nonpos_of_nonpos_of_nonneg (h _ hdead).1 (hx.1.2 _ hj) } },
{ by_cases hdead : j ∈ T.dead,
{ simp [hx.2 _ hdead] },
{ rw [(h _ hdead).2 hj, zero_mul] } }
end⟩
lemma not_optimal_of_unbounded_above {v : fin (m + n)} {x : cvec (m + n)}
(hu : is_unbounded_above T v) : ¬is_optimal T x v :=
λ hm, let ⟨y, hy⟩ := hu (x v 0 + 1) in
not_le_of_gt (lt_add_one (x v 0)) (le_trans hy.2 (hm.2 y hy.1))
/-- Expression for the sum of all but one entries in the a row of a tableau. -/
lemma row_sum_erase_eq {x : cvec (m + n)} (hx : x ∈ T.flat) {i : fin m} {j : fin n} :
(univ.erase j).sum (λ j' : fin n, T.to_matrix i j' * x (T.to_partition.colg j') 0) =
x (T.to_partition.rowg i) 0 - T.const i 0 - T.to_matrix i j * x (T.to_partition.colg j) 0 :=
begin
rw [mem_flat_iff] at hx,
conv_rhs { rw [hx i, ← insert_erase (mem_univ j), sum_insert (not_mem_erase _ _)] },
simp
end
/-- An expression for a column variable in terms of row variables. -/
lemma colg_eq {x : cvec (m + n)} (hx : x ∈ T.flat) {i : fin m} {j : fin n}
(his : T.to_matrix i j ≠ 0) : x (T.to_partition.colg j) 0 =
(x (T.to_partition.rowg i) 0
-(univ.erase j).sum (λ j' : fin n, T.to_matrix i j' * x (T.to_partition.colg j') 0)
- T.const i 0) * (T.to_matrix i j)⁻¹ :=
by simp [row_sum_erase_eq hx, mul_left_comm (T.to_matrix i j)⁻¹, mul_assoc,
mul_left_comm (T.to_matrix i j), mul_inv_cancel his]
/-- Another expression for a column variable in terms of row variables. -/
lemma colg_eq' {x : cvec (m + n)} (hx : x ∈ T.flat) {i : fin m} {j : fin n}
(his : T.to_matrix i j ≠ 0) : x (T.to_partition.colg j) 0 =
univ.sum (λ j' : fin n, (if j' = j then (T.to_matrix i j)⁻¹
else (-(T.to_matrix i j' * (T.to_matrix i j)⁻¹))) *
x (colg (swap (T.to_partition) i j) j') 0) -
(T.const i 0) * (T.to_matrix i j)⁻¹ :=
have (univ.erase j).sum
(λ j' : fin n, ite (j' = j) (T.to_matrix i j)⁻¹ (-(T.to_matrix i j' * (T.to_matrix i j)⁻¹)) *
x (colg (swap (T.to_partition) i j) j') 0) =
(univ.erase j).sum (λ j' : fin n,
-T.to_matrix i j' * x (T.to_partition.colg j') 0 * (T.to_matrix i j)⁻¹),
from finset.sum_congr rfl $ λ j' hj',
by simp [if_neg (mem_erase.1 hj').1, colg_swap_of_ne _ (mem_erase.1 hj').1,
mul_comm, mul_assoc, mul_left_comm],
by rw [← finset.insert_erase (mem_univ j), finset.sum_insert (not_mem_erase _ _),
if_pos rfl, colg_swap, colg_eq hx his, this, ← finset.sum_mul];
simp [_root_.add_mul, mul_comm, _root_.mul_add]
/-- Pivoting twice in the same place does nothing -/
@[simp] lemma pivot_pivot {i : fin m} {j : fin n} (his : T.to_matrix i j ≠ 0) :
(T.pivot i j).pivot i j = T :=
begin
cases T,
simp [pivot, function.funext_iff],
split; intros; split_ifs;
simp [*, mul_assoc, mul_left_comm (T_to_matrix i j), mul_left_comm (T_to_matrix i j)⁻¹,
mul_comm (T_to_matrix i j), inv_mul_cancel his]
end
/- These two sets are equal_in_flat, the stronger lemma is `flat_pivot` -/
private lemma subset_flat_pivot {i : fin m} {j : fin n} (h : T.to_matrix i j ≠ 0) :
T.flat ⊆ (T.pivot i j).flat :=
λ x hx,
have ∀ i' : fin m, (univ.erase j).sum (λ j' : fin n,
ite (j' = j) (T.to_matrix i' j * (T.to_matrix i j)⁻¹)
(T.to_matrix i' j' + -(T.to_matrix i' j * T.to_matrix i j' * (T.to_matrix i j)⁻¹))
* x ((T.to_partition.swap i j).colg j') 0) =
(univ.erase j).sum (λ j' : fin n, T.to_matrix i' j' * x (T.to_partition.colg j') 0 -
T.to_matrix i j' *
x (T.to_partition.colg j') 0 * T.to_matrix i' j * (T.to_matrix i j)⁻¹),
from λ i', finset.sum_congr rfl (λ j' hj',
by rw [if_neg (mem_erase.1 hj').1, colg_swap_of_ne _ (mem_erase.1 hj').1];
simp [mul_add, add_mul, mul_comm, mul_assoc, mul_left_comm]),
begin
rw mem_flat_iff,
assume i',
by_cases hi'i : i' = i,
{ rw eq_comm at hi'i,
subst hi'i,
dsimp [pivot],
simp [mul_inv_cancel h, neg_mul_eq_neg_mul_symm, if_true,
add_comm, mul_inv_cancel, rowg_swap, eq_self_iff_true, colg_eq' hx h],
congr, funext, congr },
{ dsimp [pivot],
simp only [if_neg hi'i],
rw [← insert_erase (mem_univ j), sum_insert (not_mem_erase _ _),
if_pos rfl, colg_swap, this, sum_sub_distrib, ← sum_mul, ← sum_mul,
row_sum_erase_eq hx, rowg_swap_of_ne _ hi'i],
simp [row_sum_erase_eq hx, mul_add, add_mul,
mul_comm, mul_left_comm, mul_assoc],
simp [mul_assoc, mul_left_comm (T.to_matrix i j), mul_left_comm (T.to_matrix i j)⁻¹,
mul_comm (T.to_matrix i j), inv_mul_cancel h] }
end
variable (T)
@[simp] lemma pivot_pivot_element (i : fin m) (j : fin n) :
(T.pivot i j).to_matrix i j = (T.to_matrix i j)⁻¹ :=
by simp [pivot, if_pos rfl]
@[simp] lemma pivot_pivot_row {i : fin m} {j j' : fin n} (h : j' ≠ j) :
(T.pivot i j).to_matrix i j' = -T.to_matrix i j' / T.to_matrix i j :=
by dsimp [pivot]; rw [if_pos rfl, if_neg h, div_eq_mul_inv]
@[simp] lemma pivot_pivot_column {i' i : fin m} {j : fin n} (h : i' ≠ i) :
(T.pivot i j).to_matrix i' j = T.to_matrix i' j / T.to_matrix i j :=
by dsimp [pivot]; rw [if_neg h, if_pos rfl, div_eq_mul_inv]
@[simp] lemma pivot_of_ne_of_ne {i i' : fin m} {j j' : fin n} (hi'i : i' ≠ i)
(hjs : j' ≠ j) : (T.pivot i j).to_matrix i' j' =
T.to_matrix i' j' - T.to_matrix i' j * T.to_matrix i j' / T.to_matrix i j :=
by dsimp [pivot]; rw [if_neg hi'i, if_neg hjs, div_eq_mul_inv]
@[simp] lemma const_pivot_row {i : fin m} {j : fin n} : (T.pivot i j).const i 0 =
-T.const i 0 / T.to_matrix i j :=
by simp [pivot, if_pos rfl, div_eq_mul_inv]
@[simp] lemma const_pivot_of_ne {i i' : fin m} {j : fin n} (hi'i : i' ≠ i) : (T.pivot i j).const i' 0
= T.const i' 0 - T.to_matrix i' j * T.const i 0 / T.to_matrix i j :=
by dsimp [pivot]; rw [if_neg hi'i, div_eq_mul_inv]
@[simp] lemma restricted_pivot (i s) : (T.pivot i s).restricted = T.restricted := rfl
@[simp] lemma to_partition_pivot (i s) : (T.pivot i s).to_partition = T.to_partition.swap i s := rfl
@[simp] lemma dead_pivot (i c) : (T.pivot i c).dead = T.dead := rfl
variable {T}
@[simp] lemma flat_pivot {i : fin m} {j : fin n} (hij : T.to_matrix i j ≠ 0) :
(T.pivot i j).flat = T.flat :=
set.subset.antisymm
(by conv_rhs { rw ← pivot_pivot hij };
exact subset_flat_pivot (by simp [hij]))
(subset_flat_pivot hij)
@[simp] lemma res_set_pivot {i : fin m} {j : fin n} (hij : T.to_matrix i j ≠ 0) :
(T.pivot i j).res_set = T.res_set :=
by rw [res_set, flat_pivot hij]; refl
@[simp] lemma dead_set_pivot {i : fin m} {j : fin n} (hij : T.to_matrix i j ≠ 0)
(hdead : j ∉ T.dead) : (T.pivot i j).dead_set = T.dead_set :=
begin
rw [dead_set, dead_set, flat_pivot hij],
congr,
funext x,
refine forall_congr_eq (λ j', forall_congr_eq (λ hj', _)),
have hjj' : j' ≠ j, from λ hjj', by simp * at *,
simp [colg_swap_of_ne _ hjj']
end
@[simp] lemma sol_set_pivot {i : fin m} {j : fin n} (hij : T.to_matrix i j ≠ 0)
(hdead : j ∉ T.dead) : (T.pivot i j).sol_set = T.sol_set :=
by simp [sol_set_eq_dead_set_inter, dead_set_pivot hij hdead, flat_pivot hij]
/-- Two row variables are `equal_in_flat` iff the corresponding rows of the tableau are equal -/
lemma equal_in_flat_row_row {i i' : fin m} :
T.equal_in_flat (T.to_partition.rowg i) (T.to_partition.rowg i')
↔ (T.const i 0 = T.const i' 0 ∧ ∀ j : fin n, T.to_matrix i j = T.to_matrix i' j) :=
⟨λ h, have Hconst : T.const i 0 = T.const i' 0,
by simpa [of_col_rowg] using h (T.of_col 0) (of_col_mem_flat _ _),
⟨Hconst,
λ j, begin
have := h (T.of_col (single j (0 : fin 1)).to_matrix) (of_col_mem_flat _ _),
rwa [of_col_rowg, of_col_rowg, add_val, add_val, matrix_mul_apply, matrix_mul_apply,
symm_single_apply, Hconst, add_right_cancel_iff] at this,
end⟩,
λ h x hx, by simp [mem_flat_iff.1 hx, h.1, h.2]⟩
/-- A row variable is equal_in_flat to a column variable iff its row has zeros, and a single
one in that column. -/
lemma equal_in_flat_row_col {i : fin m} {j : fin n} :
T.equal_in_flat (T.to_partition.rowg i) (T.to_partition.colg j)
↔ (∀ j', j' ≠ j → T.to_matrix i j' = 0) ∧ T.const i 0 = 0 ∧ T.to_matrix i j = 1 :=
⟨λ h, have Hconst : T.const i 0 = 0,
by simpa [of_col_rowg] using h (T.of_col 0) (of_col_mem_flat _ _),
⟨assume j' hj', begin
have := h (T.of_col (single j' (0 : fin 1)).to_matrix) (of_col_mem_flat _ _),
rwa [of_col_rowg, of_col_colg, add_val, Hconst, add_zero, matrix_mul_apply,
symm_single_apply, pequiv.to_matrix, single_apply_of_ne hj',
if_neg (option.not_mem_none _)] at this
end,
Hconst,
begin
have := h (T.of_col (single j (0 : fin 1)).to_matrix) (of_col_mem_flat _ _),
rwa [of_col_rowg, of_col_colg, add_val, Hconst, add_zero, matrix_mul_apply,
symm_single_apply, pequiv.to_matrix, single_apply] at this
end⟩,
by rintros ⟨h₁, h₂, h₃⟩ x hx;
rw [mem_flat_iff.1 hx, h₂, sum_eq_single j]; simp *; tauto⟩
lemma mul_single_ext {R : Type*} {m n : ℕ} [semiring R]
{A B : matrix (fin m) (fin n) R} (h : ∀ j : fin n, A ⬝ (single j (0 : fin 1)).to_matrix = B ⬝
(single j (0 : fin 1)).to_matrix) : A = B :=
by ext i j; simpa [matrix_mul_apply] using congr_fun (congr_fun (h j) i) 0
lemma single_mul_ext {R : Type*} {m n : ℕ} [semiring R]
{A B : matrix (fin m) (fin n) R} (h : ∀ i, (single (0 : fin 1) i).to_matrix ⬝ A =
(single (0 : fin 1) i).to_matrix ⬝ B) : A = B :=
by ext i j; simpa [mul_matrix_apply] using congr_fun (congr_fun (h i) 0) j
lemma ext {T₁ T₂ : tableau m n} (hflat : T₁.flat = T₂.flat)
(hpartition : T₁.to_partition = T₂.to_partition)
(hdead : T₁.dead = T₂.dead)
(hres : T₁.restricted = T₂.restricted) : T₁ = T₂ :=
have hconst : T₁.const = T₂.const,
by rw [set.ext_iff] at hflat; simpa [of_col, flat, hpartition, matrix.mul_assoc,
mul_right_eq_of_mul_eq (rowp_transpose_mul_rowp _),
mul_right_eq_of_mul_eq (colp_transpose_mul_rowp _)] using
(hflat (T₁.of_col 0)).1 (of_col_mem_flat _ _),
have hmatrix : T₁.to_matrix = T₂.to_matrix,
from mul_single_ext $ λ j, begin
rw [set.ext_iff] at hflat,
have := (hflat (T₁.of_col (single j 0).to_matrix)).1 (of_col_mem_flat _ _),
simpa [of_col, hconst, hpartition, flat, matrix.mul_add, matrix.mul_assoc,
mul_right_eq_of_mul_eq (rowp_transpose_mul_colp _),
mul_right_eq_of_mul_eq (rowp_transpose_mul_rowp _),
mul_right_eq_of_mul_eq (colp_transpose_mul_colp _),
mul_right_eq_of_mul_eq (colp_transpose_mul_rowp _)] using
(hflat (T₁.of_col (single j 0).to_matrix)).1 (of_col_mem_flat _ _)
end,
by cases T₁; cases T₂; simp * at *
end predicate_lemmas
/-- Conditions for unboundedness based on reading the tableau. The conditions are equivalent to the
simplex pivot rule failing to find a pivot row. -/
lemma unbounded_of_tableau {T : tableau m n} {obj : fin m} {j : fin n} (hT : T.feasible)
(hdead : j ∉ T.dead)
(hrow : ∀ i, obj ≠ i → T.to_partition.rowg i ∈ T.restricted →
0 ≤ T.to_matrix obj j / T.to_matrix i j)
(hc : (T.to_matrix obj j ≠ 0 ∧ T.to_partition.colg j ∉ T.restricted)
∨ (0 < T.to_matrix obj j ∧ T.to_partition.colg j ∈ T.restricted)) :
T.is_unbounded_above (T.to_partition.rowg obj) :=
have hToj : T.to_matrix obj j ≠ 0, from λ h, by simpa [h, lt_irrefl] using hc,
(lt_or_gt_of_ne hToj).elim
(λ hToj : T.to_matrix obj j < 0, is_unbounded_above_rowg_of_nonpos hT j
(hc.elim and.right (λ h, (not_lt_of_gt hToj h.1).elim)) hdead
(λ i hi, classical.by_cases
(λ hoi : obj = i, le_of_lt (hoi ▸ hToj))
(λ hoi : obj ≠ i, inv_nonpos.1 $ nonpos_of_mul_nonneg_right (hrow _ hoi hi) hToj))
hToj)
(λ hToj : 0 < T.to_matrix obj j, is_unbounded_above_rowg_of_nonneg hT j
(λ i hi, classical.by_cases
(λ hoi : obj = i, le_of_lt (hoi ▸ hToj))
(λ hoi : obj ≠ i, inv_nonneg.1 $ nonneg_of_mul_nonneg_left (hrow _ hoi hi) hToj))
hdead hToj)
/-- Conditions for the tableau being feasible, that must be satisified by a simplex pivot rule -/
lemma feasible_pivot {T : tableau m n} (hT : T.feasible) {i j}
(hres : T.to_partition.rowg i ∈ T.restricted)
(hpos : T.to_partition.colg j ∈ T.restricted → T.to_matrix i j < 0)
(himin : ∀ (i' : fin m), T.to_partition.rowg i' ∈ T.restricted →
0 < T.to_matrix i' j / T.to_matrix i j →
abs (T.const i 0 / T.to_matrix i j) ≤ abs (T.const i' 0 / T.to_matrix i' j)) :
(T.pivot i j).feasible :=
begin
assume i' hi',
dsimp only [pivot],
by_cases hii : i' = i,
{ subst i',
rw [if_pos rfl, neg_mul_eq_neg_mul_symm, neg_nonneg],
exact mul_nonpos_of_nonneg_of_nonpos (hT _ hres) (inv_nonpos.2 $ le_of_lt (by simp * at *)) },
{ rw if_neg hii,
rw [to_partition_pivot, rowg_swap_of_ne _ hii, restricted_pivot] at hi',
by_cases hTii : 0 < T.to_matrix i' j / T.to_matrix i j,
{ have hTi'c0 : T.to_matrix i' j ≠ 0, from λ h, by simpa [h, lt_irrefl] using hTii,
have hTic0 : T.to_matrix i j ≠ 0, from λ h, by simpa [h, lt_irrefl] using hTii,
have := himin _ hi' hTii,
rwa [abs_div, abs_div, abs_of_nonneg (hT _ hres), abs_of_nonneg (hT _ hi'),
le_div_iff (abs_pos_iff.2 hTi'c0), div_eq_mul_inv, mul_right_comm, ← abs_inv, mul_assoc,
← abs_mul, ← div_eq_mul_inv, abs_of_nonneg (le_of_lt hTii), ← sub_nonneg,
← mul_div_assoc, div_eq_mul_inv, mul_comm (T.const i 0)] at this },
{ refine add_nonneg (hT _ hi') (neg_nonneg.2 _),
rw [mul_assoc, mul_left_comm],
exact mul_nonpos_of_nonneg_of_nonpos (hT _ hres) (le_of_not_gt hTii) } }
end
lemma feasible_simplex_pivot {T : tableau m n} {obj : fin m} (hT : T.feasible) {i j}
(hres : T.to_partition.rowg i ∈ T.restricted)
(hineg : T.to_matrix obj j / T.to_matrix i j < 0)
(himin : ∀ (i' : fin m), obj ≠ i' → T.to_partition.rowg i' ∈ T.restricted →
T.to_matrix obj j / T.to_matrix i' j < 0 →
abs (T.const i 0 / T.to_matrix i j) ≤ abs (T.const i' 0 / T.to_matrix i' j))
(hc : T.to_partition.colg j ∈ T.restricted → 0 < T.to_matrix obj j) :
(T.pivot i j).feasible :=
feasible_pivot hT hres (λ hcres, inv_neg'.1 (neg_of_mul_neg_left hineg (le_of_lt (hc hcres))))
(λ i' hi'res hii,
have hobji : obj ≠ i',
from λ hobji, not_lt_of_gt hii (hobji ▸ hineg),
(himin _ hobji hi'res $
have hTrc0 : T.to_matrix i j ≠ 0, from λ _, by simp [*, lt_irrefl] at *,
suffices (T.to_matrix obj j / T.to_matrix i j) / (T.to_matrix i' j / T.to_matrix i j) < 0,
by rwa [div_div_div_cancel_right _ _ hTrc0] at this,
div_neg_of_neg_of_pos hineg hii))
/-- Used in sign_of_max -/
lemma feasible_pivot_obj_of_nonpos {T : tableau m n} {obj : fin m} (hT : T.feasible) {j}
(hc : T.to_partition.colg j ∈ T.restricted → 0 < T.to_matrix obj j)
(hr : ∀ i, obj ≠ i → T.to_partition.rowg i ∈ T.restricted →
0 ≤ T.to_matrix obj j / T.to_matrix i j)
(hobj : T.const obj 0 ≤ 0) : feasible (T.pivot obj j) :=
λ i hi,
if hiobj : i = obj
then by rw [hiobj, const_pivot_row, neg_div, neg_nonneg];
exact mul_nonpos_of_nonpos_of_nonneg hobj (le_of_lt $ by simp [*, inv_pos'] at *)
else begin
rw [const_pivot_of_ne _ hiobj],
rw [to_partition_pivot, rowg_swap_of_ne _ hiobj, restricted_pivot] at hi,
refine add_nonneg (hT _ hi) (neg_nonneg.2 _),
rw [div_eq_mul_inv, mul_right_comm],
exact mul_nonpos_of_nonneg_of_nonpos
(inv_nonneg.1 (by simpa [mul_inv'] using hr _ (ne.symm hiobj) hi))
hobj
end
lemma simplex_const_obj_le {T : tableau m n} {obj : fin m} (hT : T.feasible) {i j}
(hres : T.to_partition.rowg i ∈ T.restricted)
(hineg : T.to_matrix obj j / T.to_matrix i j < 0) :
T.const obj 0 ≤ (T.pivot i j).const obj 0 :=
have obj ≠ i, from λ hor, begin
subst hor,
by_cases h0 : T.to_matrix obj j = 0,
{ simp [lt_irrefl, *] at * },
{ simp [div_self h0, not_lt_of_le zero_le_one, *] at * }
end,
by simp only [le_add_iff_nonneg_right, sub_eq_add_neg, neg_nonneg, mul_assoc, div_eq_mul_inv,
mul_left_comm (T.to_matrix obj j), const_pivot_of_ne _ this];
exact mul_nonpos_of_nonneg_of_nonpos (hT _ hres) (le_of_lt hineg)
lemma const_eq_zero_of_const_obj_eq {T : tableau m n} {obj : fin m} (hT : T.feasible) {i j}
(hc : T.to_matrix obj j ≠ 0) (hrc : T.to_matrix i j ≠ 0) (hobjr : obj ≠ i)
(hobj : (T.pivot i j).const obj 0 = T.const obj 0) :
T.const i 0 = 0 :=
by rw [const_pivot_of_ne _ hobjr, sub_eq_iff_eq_add, ← sub_eq_iff_eq_add', sub_self,
eq_comm, div_eq_mul_inv, mul_eq_zero, mul_eq_zero, inv_eq_zero] at hobj; tauto
end tableau
|
e7acdb6c44458d788eba74661f9bb394626289cb | 618003631150032a5676f229d13a079ac875ff77 | /src/topology/sheaves/stalks.lean | f4169a38de7e3ca33066cf71f33276554b5f96ce | [
"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,750 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import topology.category.Top.open_nhds
import topology.sheaves.presheaf
import category_theory.limits.limits
universes v u v' u'
open category_theory
open Top
open category_theory.limits
open topological_space
variables {C : Type u} [𝒞 : category.{v} C]
include 𝒞
variables [has_colimits.{v} C]
variables {X Y Z : Top.{v}}
namespace Top.presheaf
variables (C)
/-- Stalks are functorial with respect to morphisms of presheaves over a fixed `X`. -/
def stalk_functor (x : X) : X.presheaf C ⥤ C :=
((whiskering_left _ _ C).obj (open_nhds.inclusion x).op) ⋙ colim
variables {C}
/--
The stalk of a presheaf `F` at a point `x` is calculated as the colimit of the functor
nbhds x ⥤ opens F.X ⥤ C
-/
def stalk (ℱ : X.presheaf C) (x : X) : C :=
(stalk_functor C x).obj ℱ -- -- colimit (nbhds_inclusion x ⋙ ℱ)
@[simp] lemma stalk_functor_obj (ℱ : X.presheaf C) (x : X) : (stalk_functor C x).obj ℱ = ℱ.stalk x := rfl
variables (C)
def stalk_pushforward (f : X ⟶ Y) (ℱ : X.presheaf C) (x : X) : (f _* ℱ).stalk (f x) ⟶ ℱ.stalk x :=
begin
-- This is a hack; Lean doesn't like to elaborate the term written directly.
transitivity,
swap,
exact colimit.pre _ (open_nhds.map f x).op,
exact colim.map (whisker_right (nat_trans.op (open_nhds.inclusion_map_iso f x).inv) ℱ),
end
-- Here are two other potential solutions, suggested by @fpvandoorn at
-- <https://github.com/leanprover-community/mathlib/pull/1018#discussion_r283978240>
-- However, I can't get the subsequent two proofs to work with either one.
-- def stalk_pushforward (f : X ⟶ Y) (ℱ : X.presheaf C) (x : X) : (f _* ℱ).stalk (f x) ⟶ ℱ.stalk x :=
-- colim.map ((functor.associator _ _ _).inv ≫
-- whisker_right (nat_trans.op (open_nhds.inclusion_map_iso f x).inv) ℱ) ≫
-- colimit.pre ((open_nhds.inclusion x).op ⋙ ℱ) (open_nhds.map f x).op
-- def stalk_pushforward (f : X ⟶ Y) (ℱ : X.presheaf C) (x : X) : (f _* ℱ).stalk (f x) ⟶ ℱ.stalk x :=
-- (colim.map (whisker_right (nat_trans.op (open_nhds.inclusion_map_iso f x).inv) ℱ) :
-- colim.obj ((open_nhds.inclusion (f x) ⋙ opens.map f).op ⋙ ℱ) ⟶ _) ≫
-- colimit.pre ((open_nhds.inclusion x).op ⋙ ℱ) (open_nhds.map f x).op
namespace stalk_pushforward
local attribute [tidy] tactic.op_induction'
@[simp] lemma id (ℱ : X.presheaf C) (x : X) :
ℱ.stalk_pushforward C (𝟙 X) x = (stalk_functor C x).map ((pushforward.id ℱ).hom) :=
begin
dsimp [stalk_pushforward, stalk_functor],
ext1,
tactic.op_induction',
cases j, cases j_val,
rw [colimit.ι_map_assoc, colimit.ι_map, colimit.ι_pre, whisker_left_app, whisker_right_app,
pushforward.id_hom_app, eq_to_hom_map, eq_to_hom_refl],
dsimp,
-- FIXME A simp lemma which unfortunately doesn't fire:
erw [category_theory.functor.map_id],
end
-- This proof is sadly not at all robust:
-- having to use `erw` at all is a bad sign.
@[simp] lemma comp (ℱ : X.presheaf C) (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) :
ℱ.stalk_pushforward C (f ≫ g) x =
((f _* ℱ).stalk_pushforward C g (f x)) ≫ (ℱ.stalk_pushforward C f x) :=
begin
dsimp [stalk_pushforward, stalk_functor, pushforward],
ext U,
op_induction U,
cases U,
cases U_val,
simp only [colimit.ι_map_assoc, colimit.ι_pre_assoc,
whisker_right_app, category.assoc],
dsimp,
-- FIXME: Some of these are simp lemmas, but don't fire successfully:
erw [category_theory.functor.map_id, category.id_comp, category.id_comp, category.id_comp,
colimit.ι_pre, colimit.ι_pre],
refl,
end
end stalk_pushforward
end Top.presheaf
|
fe2861381e0829a37bbb8734af71131b72dbb32f | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/ring/fin.lean | 0b9379c02c61cc6f0cd586eb39123a6901d8ab08 | [
"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 | 873 | lean | /-
Copyright (c) 2022 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen
-/
import logic.equiv.fin
import algebra.ring.equiv
import algebra.group.prod
/-!
# Rings and `fin`
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file collects some basic results involving rings and the `fin` type
## Main results
* `ring_equiv.fin_two`: The product over `fin 2` of some rings is the cartesian product
-/
/-- The product over `fin 2` of some rings is just the cartesian product of these rings. -/
@[simps]
def ring_equiv.pi_fin_two (R : fin 2 → Type*) [Π i, semiring (R i)] :
(Π (i : fin 2), R i) ≃+* R 0 × R 1 :=
{ to_fun := pi_fin_two_equiv R,
map_add' := λ a b, rfl,
map_mul' := λ a b, rfl,
.. pi_fin_two_equiv R }
|
aa5c8f0a6236556995ac64828a9d1b4cab0e5ae9 | 5756a081670ba9c1d1d3fca7bd47cb4e31beae66 | /Test/importLeanbin/ImportLeanbin.lean | e424f4a597520b5cc13398d0d4f30a7ddd55bb61 | [
"Apache-2.0"
] | permissive | leanprover-community/mathport | 2c9bdc8292168febf59799efdc5451dbf0450d4a | 13051f68064f7638970d39a8fecaede68ffbf9e1 | refs/heads/master | 1,693,841,364,079 | 1,693,813,111,000 | 1,693,813,111,000 | 379,357,010 | 27 | 10 | Apache-2.0 | 1,691,309,132,000 | 1,624,384,521,000 | Lean | UTF-8 | Lean | false | false | 40 | lean | import Leanbin
#lookup3 set
#check Set
|
3ae50293937c797b8c052ca6df2a0348a8b0c9a6 | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/data/polynomial/coeff.lean | 3f859a9442cd621f4917ef092889d8c89fa46ab0 | [
"Apache-2.0"
] | permissive | hikari0108/mathlib | b7ea2b7350497ab1a0b87a09d093ecc025a50dfa | a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901 | refs/heads/master | 1,690,483,608,260 | 1,631,541,580,000 | 1,631,541,580,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,543 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker
-/
import data.polynomial.basic
import data.finset.nat_antidiagonal
import data.nat.choose.sum
/-!
# Theory of univariate polynomials
The theorems include formulas for computing coefficients, such as
`coeff_add`, `coeff_sum`, `coeff_mul`
-/
noncomputable theory
open finsupp finset add_monoid_algebra
open_locale big_operators
namespace polynomial
universes u v
variables {R : Type u} {S : Type v} {a b : R} {n m : ℕ}
variables [semiring R] {p q r : polynomial R}
section coeff
lemma coeff_one (n : ℕ) : coeff (1 : polynomial R) n = if 0 = n then 1 else 0 :=
coeff_monomial
@[simp]
lemma coeff_add (p q : polynomial R) (n : ℕ) : coeff (p + q) n = coeff p n + coeff q n :=
by { rcases p, rcases q, simp [coeff, add_to_finsupp] }
@[simp] lemma coeff_smul [monoid S] [distrib_mul_action S R] (r : S) (p : polynomial R) (n : ℕ) :
coeff (r • p) n = r • coeff p n :=
by { rcases p, simp [coeff, smul_to_finsupp] }
lemma support_smul [monoid S] [distrib_mul_action S R] (r : S) (p : polynomial R) :
support (r • p) ⊆ support p :=
begin
assume i hi,
simp [mem_support_iff] at hi ⊢,
contrapose! hi,
simp [hi]
end
/-- `polynomial.sum` as a linear map. -/
@[simps] def lsum {R A M : Type*} [semiring R] [semiring A] [add_comm_monoid M]
[module R A] [module R M] (f : ℕ → A →ₗ[R] M) :
polynomial A →ₗ[R] M :=
{ to_fun := λ p, p.sum (λ n r, f n r),
map_add' := λ p q, sum_add_index p q _ (λ n, (f n).map_zero) (λ n _ _, (f n).map_add _ _),
map_smul' := λ c p,
begin
rw [sum_eq_of_subset _ (λ n r, f n r) (λ n, (f n).map_zero) _ (support_smul c p)],
simp only [sum_def, finset.smul_sum, coeff_smul, linear_map.map_smul],
end }
variable (R)
/-- The nth coefficient, as a linear map. -/
def lcoeff (n : ℕ) : polynomial R →ₗ[R] R :=
{ to_fun := λ p, coeff p n,
map_add' := λ p q, coeff_add p q n,
map_smul' := λ r p, coeff_smul r p n }
variable {R}
@[simp] lemma lcoeff_apply (n : ℕ) (f : polynomial R) : lcoeff R n f = coeff f n := rfl
@[simp] lemma finset_sum_coeff {ι : Type*} (s : finset ι) (f : ι → polynomial R) (n : ℕ) :
coeff (∑ b in s, f b) n = ∑ b in s, coeff (f b) n :=
(lcoeff R n).map_sum
lemma coeff_sum [semiring S] (n : ℕ) (f : ℕ → R → polynomial S) :
coeff (p.sum f) n = p.sum (λ a b, coeff (f a b) n) :=
by { rcases p, simp [polynomial.sum, support, coeff] }
/-- Decomposes the coefficient of the product `p * q` as a sum
over `nat.antidiagonal`. A version which sums over `range (n + 1)` can be obtained
by using `finset.nat.sum_antidiagonal_eq_sum_range_succ`. -/
lemma coeff_mul (p q : polynomial R) (n : ℕ) :
coeff (p * q) n = ∑ x in nat.antidiagonal n, coeff p x.1 * coeff q x.2 :=
begin
rcases p, rcases q,
simp only [coeff, mul_to_finsupp],
exact add_monoid_algebra.mul_apply_antidiagonal p q n _ (λ x, nat.mem_antidiagonal)
end
@[simp] lemma mul_coeff_zero (p q : polynomial R) : coeff (p * q) 0 = coeff p 0 * coeff q 0 :=
by simp [coeff_mul]
lemma coeff_mul_X_zero (p : polynomial R) : coeff (p * X) 0 = 0 :=
by simp
lemma coeff_X_mul_zero (p : polynomial R) : coeff (X * p) 0 = 0 :=
by simp
lemma coeff_C_mul_X (x : R) (k n : ℕ) :
coeff (C x * X^k : polynomial R) n = if n = k then x else 0 :=
by { rw [← monomial_eq_C_mul_X, coeff_monomial], congr' 1, simp [eq_comm] }
@[simp] lemma coeff_C_mul (p : polynomial R) : coeff (C a * p) n = a * coeff p n :=
by { rcases p, simp only [C, monomial, monomial_fun, mul_to_finsupp, ring_hom.coe_mk,
coeff, add_monoid_algebra.single_zero_mul_apply p a n] }
lemma C_mul' (a : R) (f : polynomial R) : C a * f = a • f :=
by { ext, rw [coeff_C_mul, coeff_smul, smul_eq_mul] }
@[simp] lemma coeff_mul_C (p : polynomial R) (n : ℕ) (a : R) :
coeff (p * C a) n = coeff p n * a :=
by { rcases p, simp only [C, monomial, monomial_fun, mul_to_finsupp, ring_hom.coe_mk,
coeff, add_monoid_algebra.mul_single_zero_apply p a n] }
lemma coeff_X_pow (k n : ℕ) :
coeff (X^k : polynomial R) n = if n = k then 1 else 0 :=
by simp only [one_mul, ring_hom.map_one, ← coeff_C_mul_X]
@[simp]
lemma coeff_X_pow_self (n : ℕ) :
coeff (X^n : polynomial R) n = 1 :=
by simp [coeff_X_pow]
theorem coeff_mul_X_pow (p : polynomial R) (n d : ℕ) :
coeff (p * polynomial.X ^ n) (d + n) = coeff p d :=
begin
rw [coeff_mul, sum_eq_single (d,n), coeff_X_pow, if_pos rfl, mul_one],
{ rintros ⟨i,j⟩ h1 h2, rw [coeff_X_pow, if_neg, mul_zero], rintro rfl, apply h2,
rw [nat.mem_antidiagonal, add_right_cancel_iff] at h1, subst h1 },
{ exact λ h1, (h1 (nat.mem_antidiagonal.2 rfl)).elim }
end
lemma coeff_mul_X_pow' (p : polynomial R) (n d : ℕ) :
(p * X ^ n).coeff d = ite (n ≤ d) (p.coeff (d - n)) 0 :=
begin
split_ifs,
{ rw [←@nat.sub_add_cancel d n h, coeff_mul_X_pow, nat.add_sub_cancel] },
{ refine (coeff_mul _ _ _).trans (finset.sum_eq_zero (λ x hx, _)),
rw [coeff_X_pow, if_neg, mul_zero],
exact ne_of_lt (lt_of_le_of_lt (nat.le_of_add_le_right
(le_of_eq (finset.nat.mem_antidiagonal.mp hx))) (not_le.mp h)) },
end
@[simp] theorem coeff_mul_X (p : polynomial R) (n : ℕ) :
coeff (p * X) (n + 1) = coeff p n :=
by simpa only [pow_one] using coeff_mul_X_pow p 1 n
theorem mul_X_pow_eq_zero {p : polynomial R} {n : ℕ}
(H : p * X ^ n = 0) : p = 0 :=
ext $ λ k, (coeff_mul_X_pow p n k).symm.trans $ ext_iff.1 H (k+n)
lemma C_mul_X_pow_eq_monomial (c : R) (n : ℕ) : C c * X^n = monomial n c :=
by { ext1, rw [monomial_eq_smul_X, coeff_smul, coeff_C_mul, smul_eq_mul] }
lemma support_mul_X_pow (c : R) (n : ℕ) (H : c ≠ 0) : (C c * X^n).support = singleton n :=
by rw [C_mul_X_pow_eq_monomial, support_monomial n c H]
lemma support_C_mul_X_pow' {c : R} {n : ℕ} : (C c * X^n).support ⊆ singleton n :=
by { rw [C_mul_X_pow_eq_monomial], exact support_monomial' n c }
lemma coeff_X_add_one_pow (R : Type*) [semiring R] (n k : ℕ) :
((X + 1) ^ n).coeff k = (n.choose k : R) :=
begin
rw [(commute_X (1 : polynomial R)).add_pow, ← lcoeff_apply, linear_map.map_sum],
simp only [one_pow, mul_one, lcoeff_apply, ← C_eq_nat_cast, coeff_mul_C, nat.cast_id],
rw [finset.sum_eq_single k, coeff_X_pow_self, one_mul],
{ intros _ _,
simp only [coeff_X_pow, boole_mul, ite_eq_right_iff, ne.def] {contextual := tt},
rintro h rfl, contradiction },
{ simp only [coeff_X_pow_self, one_mul, not_lt, finset.mem_range],
intro h, rw [nat.choose_eq_zero_of_lt h, nat.cast_zero], }
end
lemma coeff_one_add_X_pow (R : Type*) [semiring R] (n k : ℕ) :
((1 + X) ^ n).coeff k = (n.choose k : R) :=
by rw [add_comm _ X, coeff_X_add_one_pow]
lemma C_dvd_iff_dvd_coeff (r : R) (φ : polynomial R) :
C r ∣ φ ↔ ∀ i, r ∣ φ.coeff i :=
begin
split,
{ rintros ⟨φ, rfl⟩ c, rw coeff_C_mul, apply dvd_mul_right },
{ intro h,
choose c hc using h,
classical,
let c' : ℕ → R := λ i, if i ∈ φ.support then c i else 0,
let ψ : polynomial R := ∑ i in φ.support, monomial i (c' i),
use ψ,
ext i,
simp only [ψ, c', coeff_C_mul, mem_support_iff, coeff_monomial,
finset_sum_coeff, finset.sum_ite_eq'],
split_ifs with hi hi,
{ rw hc },
{ rw [not_not] at hi, rwa mul_zero } },
end
lemma coeff_bit0_mul (P Q : polynomial R) (n : ℕ) :
coeff (bit0 P * Q) n = 2 * coeff (P * Q) n :=
by simp [bit0, add_mul]
lemma coeff_bit1_mul (P Q : polynomial R) (n : ℕ) :
coeff (bit1 P * Q) n = 2 * coeff (P * Q) n + coeff Q n :=
by simp [bit1, add_mul, coeff_bit0_mul]
end coeff
section cast
@[simp] lemma nat_cast_coeff_zero {n : ℕ} {R : Type*} [semiring R] :
(n : polynomial R).coeff 0 = n :=
begin
induction n with n ih,
{ simp, },
{ simp [ih], },
end
@[simp, norm_cast] theorem nat_cast_inj
{m n : ℕ} {R : Type*} [semiring R] [char_zero R] : (↑m : polynomial R) = ↑n ↔ m = n :=
begin
fsplit,
{ intro h,
apply_fun (λ p, p.coeff 0) at h,
simpa using h, },
{ rintro rfl, refl, },
end
@[simp] lemma int_cast_coeff_zero {i : ℤ} {R : Type*} [ring R] :
(i : polynomial R).coeff 0 = i :=
by cases i; simp
@[simp, norm_cast] theorem int_cast_inj
{m n : ℤ} {R : Type*} [ring R] [char_zero R] : (↑m : polynomial R) = ↑n ↔ m = n :=
begin
fsplit,
{ intro h,
apply_fun (λ p, p.coeff 0) at h,
simpa using h, },
{ rintro rfl, refl, },
end
end cast
end polynomial
|
2d20a0bf7079f79d6021babd3dc42b9b548b02e2 | 8cb37a089cdb4af3af9d8bf1002b417e407a8e9e | /tests/lean/run/u_eq_max_u_v.lean | 35395e1b73b47d53826a37f19f269d754af61de7 | [
"Apache-2.0"
] | permissive | kbuzzard/lean | ae3c3db4bb462d750dbf7419b28bafb3ec983ef7 | ed1788fd674bb8991acffc8fca585ec746711928 | refs/heads/master | 1,620,983,366,617 | 1,618,937,600,000 | 1,618,937,600,000 | 359,886,396 | 1 | 0 | Apache-2.0 | 1,618,936,987,000 | 1,618,936,987,000 | null | UTF-8 | Lean | false | false | 3,210 | lean | universe variables u v u1 u2 v1 v2
set_option pp.universes true
class semigroup (α : Type u) extends has_mul α :=
(mul_assoc : ∀ a b c : α, a * b * c = a * (b * c))
open smt_tactic
meta def blast : tactic unit := using_smt $ intros >> add_lemmas_from_facts >> iterate_at_most 3 ematch
notation `♮` := by blast
structure semigroup_morphism { α β : Type u } ( s : semigroup α ) ( t: semigroup β ) :=
(map: α → β)
(multiplicative : ∀ x y : α, map(x * y) = map(x) * map(y))
attribute [simp] semigroup_morphism.multiplicative
@[reducible] instance semigroup_morphism_to_map { α β : Type u } { s : semigroup α } { t: semigroup β } : has_coe_to_fun (semigroup_morphism s t) :=
{ F := λ f, Π x : α, β,
coe := semigroup_morphism.map }
@[reducible] definition semigroup_identity { α : Type u } ( s: semigroup α ) : semigroup_morphism s s := ⟨ id, ♮ ⟩
@[reducible] definition semigroup_morphism_composition
{ α β γ : Type u } { s: semigroup α } { t: semigroup β } { u: semigroup γ}
( f: semigroup_morphism s t ) ( g: semigroup_morphism t u ) : semigroup_morphism s u :=
{
map := λ x, g (f x),
multiplicative := begin intros, simp [coe_fn] end
}
local attribute [simp] semigroup.mul_assoc
@[reducible] definition semigroup_product { α β : Type u } ( s : semigroup α ) ( t: semigroup β ) : semigroup (α × β) := {
mul := λ p q, (p^.fst * q^.fst, p^.snd * q^.snd),
mul_assoc := begin
intros,
simp [@has_mul.mul (α × β)]
end
}
definition semigroup_morphism_product
{ α β γ δ : Type u }
{ s_f : semigroup α } { s_g: semigroup β } { t_f : semigroup γ} { t_g: semigroup δ }
( f : semigroup_morphism s_f t_f ) ( g : semigroup_morphism s_g t_g )
: semigroup_morphism (semigroup_product s_f s_g) (semigroup_product t_f t_g) := {
map := λ p, (f p.1, g p.2),
multiplicative :=
begin
-- cf https://groups.google.com/d/msg/lean-user/bVs5FdjClp4/tfHiVjLIBAAJ
intros,
unfold has_mul.mul,
dsimp [coe_fn],
simp
end
}
structure Category :=
(Obj : Type u)
(Hom : Obj → Obj → Type v)
structure Functor (C : Category.{ u1 v1 }) (D : Category.{ u2 v2 }) :=
(onObjects : C^.Obj → D^.Obj)
(onMorphisms : Π { X Y : C^.Obj },
C^.Hom X Y → D^.Hom (onObjects X) (onObjects Y))
@[reducible] definition ProductCategory (C : Category) (D : Category) :
Category :=
{
Obj := C^.Obj × D^.Obj,
Hom := (λ X Y : C^.Obj × D^.Obj, C^.Hom (X^.fst) (Y^.fst) × D^.Hom (X^.snd) (Y^.snd))
}
namespace ProductCategory
notation C `×` D := ProductCategory C D
end ProductCategory
structure PreMonoidalCategory
extends carrier : Category :=
(tensor : Functor (carrier × carrier) carrier)
definition CategoryOfSemigroups : Category :=
{
Obj := Σ α : Type u, semigroup α,
Hom := λ s t, semigroup_morphism s.2 t.2
}
definition PreMonoidalCategoryOfSemigroups : PreMonoidalCategory := {
CategoryOfSemigroups with
tensor := {
onObjects := λ p, sigma.mk (p.1.1 × p.2.1) (semigroup_product p.1.2 p.2.2),
onMorphisms := λ s t f, semigroup_morphism_product f.1 f.2
}
}
|
6f7fba8d4ac40bfa0c54e114b573256e05df695c | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /library/data/finset/card.lean | fff8184b4a054a9d996d010bb02bbba97726ab97 | [
"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 | 10,407 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Jeremy Avigad
Cardinality calculations for finite sets.
-/
import .to_set .bigops data.set.function data.nat.power
open nat eq.ops
namespace finset
variables {A B : Type}
variables [deceqA : decidable_eq A] [deceqB : decidable_eq B]
include deceqA
theorem card_add_card (s₁ s₂ : finset A) : card s₁ + card s₂ = card (s₁ ∪ s₂) + card (s₁ ∩ s₂) :=
begin
induction s₂ with a s₂ ans2 IH,
show card s₁ + card (∅:finset A) = card (s₁ ∪ ∅) + card (s₁ ∩ ∅),
by rewrite [union_empty, card_empty, inter_empty],
show card s₁ + card (insert a s₂) = card (s₁ ∪ (insert a s₂)) + card (s₁ ∩ (insert a s₂)),
from decidable.by_cases
(assume as1 : a ∈ s₁,
have H : a ∉ s₁ ∩ s₂, from assume H', ans2 (mem_of_mem_inter_right H'),
begin
rewrite [card_insert_of_not_mem ans2, union_comm, -insert_union, union_comm],
rewrite [insert_union, insert_eq_of_mem as1, insert_eq, inter_distrib_left, inter_comm],
rewrite [singleton_inter_of_mem as1, -insert_eq, card_insert_of_not_mem H, -*add.assoc],
rewrite IH
end)
(assume ans1 : a ∉ s₁,
have H : a ∉ s₁ ∪ s₂, from assume H',
or.elim (mem_or_mem_of_mem_union H') (assume as1, ans1 as1) (assume as2, ans2 as2),
begin
rewrite [card_insert_of_not_mem ans2, union_comm, -insert_union, union_comm],
rewrite [card_insert_of_not_mem H, insert_eq, inter_distrib_left, inter_comm],
rewrite [singleton_inter_of_not_mem ans1, empty_union, add.right_comm],
rewrite [-add.assoc, IH]
end)
end
theorem card_union (s₁ s₂ : finset A) : card (s₁ ∪ s₂) = card s₁ + card s₂ - card (s₁ ∩ s₂) :=
calc
card (s₁ ∪ s₂) = card (s₁ ∪ s₂) + card (s₁ ∩ s₂) - card (s₁ ∩ s₂) : nat.add_sub_cancel
... = card s₁ + card s₂ - card (s₁ ∩ s₂) : card_add_card
theorem card_union_of_disjoint {s₁ s₂ : finset A} (H : s₁ ∩ s₂ = ∅) :
card (s₁ ∪ s₂) = card s₁ + card s₂ :=
by rewrite [card_union, H]
theorem card_eq_card_add_card_diff {s₁ s₂ : finset A} (H : s₁ ⊆ s₂) :
card s₂ = card s₁ + card (s₂ \ s₁) :=
have H1 : s₁ ∩ (s₂ \ s₁) = ∅,
from inter_eq_empty (take x, assume H1 H2, not_mem_of_mem_diff H2 H1),
calc
card s₂ = card (s₁ ∪ (s₂ \ s₁)) : union_diff_cancel H
... = card s₁ + card (s₂ \ s₁) : card_union_of_disjoint H1
theorem card_le_card_of_subset {s₁ s₂ : finset A} (H : s₁ ⊆ s₂) : card s₁ ≤ card s₂ :=
calc
card s₂ = card s₁ + card (s₂ \ s₁) : card_eq_card_add_card_diff H
... ≥ card s₁ : le_add_right
section card_image
open set
include deceqB
theorem card_image_eq_of_inj_on {f : A → B} {s : finset A} (H1 : inj_on f (ts s)) :
card (image f s) = card s :=
begin
induction s with a t H IH,
{ rewrite [card_empty] },
{ have H2 : ts t ⊆ ts (insert a t), by rewrite [-subset_eq_to_set_subset]; apply subset_insert,
have H3 : card (image f t) = card t, from IH (inj_on_of_inj_on_of_subset H1 H2),
have H4 : f a ∉ image f t,
proof
assume H5 : f a ∈ image f t,
obtain x (H6l : x ∈ t) (H6r : f x = f a), from exists_of_mem_image H5,
have H7 : x = a, from H1 (mem_insert_of_mem _ H6l) !mem_insert H6r,
show false, from H (H7 ▸ H6l)
qed,
calc
card (image f (insert a t)) = card (insert (f a) (image f t)) : image_insert
... = card (image f t) + 1 : card_insert_of_not_mem H4
... = card t + 1 : H3
... = card (insert a t) : card_insert_of_not_mem H
}
end
lemma card_le_of_inj_on (a : finset A) (b : finset B)
(Pex : ∃ f : A → B, set.inj_on f (ts a) ∧ (image f a ⊆ b)):
card a ≤ card b :=
obtain f Pinj, from Pex,
have Psub : _, from and.right Pinj,
have Ple : card (image f a) ≤ card b, from card_le_card_of_subset Psub,
by rewrite [(card_image_eq_of_inj_on (and.left Pinj))⁻¹]; exact Ple
theorem card_image_le (f : A → B) (s : finset A) : card (image f s) ≤ card s :=
finset.induction_on s
(by rewrite finset.image_empty)
(take a s',
assume Ha : a ∉ s',
assume IH : card (image f s') ≤ card s',
begin
rewrite [image_insert, card_insert_of_not_mem Ha],
apply le.trans !card_insert_le,
apply add_le_add_right IH
end)
theorem inj_on_of_card_image_eq {f : A → B} {s : finset A} :
card (image f s) = card s → inj_on f (ts s) :=
finset.induction_on s
(by intro H; rewrite to_set_empty; apply inj_on_empty)
(begin
intro a s' Ha IH,
rewrite [image_insert, card_insert_of_not_mem Ha, to_set_insert],
assume H1 : card (insert (f a) (image f s')) = card s' + 1,
show inj_on f (set.insert a (ts s')), from
decidable.by_cases
(assume Hfa : f a ∈ image f s',
have H2 : card (image f s') = card s' + 1,
by rewrite [card_insert_of_mem Hfa at H1]; assumption,
absurd
(calc
card (image f s') ≤ card s' : !card_image_le
... < card s' + 1 : lt_succ_self
... = card (image f s') : H2)
!lt.irrefl)
(assume Hnfa : f a ∉ image f s',
have H2 : card (image f s') + 1 = card s' + 1,
by rewrite [card_insert_of_not_mem Hnfa at H1]; assumption,
have H3 : card (image f s') = card s', from add.right_cancel H2,
have injf : inj_on f (ts s'), from IH H3,
show inj_on f (set.insert a (ts s')), from
take x1 x2,
assume Hx1 : x1 ∈ set.insert a (ts s'),
assume Hx2 : x2 ∈ set.insert a (ts s'),
assume feq : f x1 = f x2,
or.elim Hx1
(assume Hx1' : x1 = a,
or.elim Hx2
(assume Hx2' : x2 = a, by rewrite [Hx1', Hx2'])
(assume Hx2' : x2 ∈ ts s',
have Hfa : f a ∈ image f s',
by rewrite [-Hx1', feq]; apply mem_image_of_mem f Hx2',
absurd Hfa Hnfa))
(assume Hx1' : x1 ∈ ts s',
or.elim Hx2
(assume Hx2' : x2 = a,
have Hfa : f a ∈ image f s',
by rewrite [-Hx2', -feq]; apply mem_image_of_mem f Hx1',
absurd Hfa Hnfa)
(assume Hx2' : x2 ∈ ts s', injf Hx1' Hx2' feq)))
end)
end card_image
theorem card_pos_of_mem {a : A} {s : finset A} (H : a ∈ s) : card s > 0 :=
begin
induction s with a s' H1 IH,
{ contradiction },
{ rewrite (card_insert_of_not_mem H1), apply succ_pos }
end
theorem eq_of_card_eq_of_subset {s₁ s₂ : finset A} (Hcard : card s₁ = card s₂) (Hsub : s₁ ⊆ s₂) :
s₁ = s₂ :=
have H : card s₁ + 0 = card s₁ + card (s₂ \ s₁),
by rewrite [Hcard at {1}, card_eq_card_add_card_diff Hsub],
have H1 : s₂ \ s₁ = ∅, from eq_empty_of_card_eq_zero (add.left_cancel H)⁻¹,
by rewrite [-union_diff_cancel Hsub, H1, union_empty]
lemma exists_two_of_card_gt_one {s : finset A} : 1 < card s → ∃ a b, a ∈ s ∧ b ∈ s ∧ a ≠ b :=
begin
intro h,
induction s with a s nain ih₁,
{exact absurd h dec_trivial},
{induction s with b s nbin ih₂,
{exact absurd h dec_trivial},
clear ih₁ ih₂,
existsi a, existsi b, split,
{apply mem_insert},
split,
{apply mem_insert_of_mem _ !mem_insert},
{intro aeqb, subst a, exact absurd !mem_insert nain}}
end
theorem Sum_const_eq_card_mul (s : finset A) (n : nat) : (∑ x ∈ s, n) = card s * n :=
begin
induction s with a s' H IH,
rewrite [Sum_empty, card_empty, zero_mul],
rewrite [Sum_insert_of_not_mem _ H, IH, card_insert_of_not_mem H, add.comm,
right_distrib, one_mul]
end
theorem Sum_one_eq_card (s : finset A) : (∑ x ∈ s, (1 : nat)) = card s :=
eq.trans !Sum_const_eq_card_mul !mul_one
section deceqB
include deceqB
theorem card_Union_of_disjoint (s : finset A) (f : A → finset B) :
(∀{a₁ a₂}, a₁ ∈ s → a₂ ∈ s → a₁ ≠ a₂ → f a₁ ∩ f a₂ = ∅) →
card (⋃ x ∈ s, f x) = ∑ x ∈ s, card (f x) :=
finset.induction_on s
(assume H, by rewrite [Union_empty, Sum_empty, card_empty])
(take a s', assume H : a ∉ s',
assume IH,
assume H1 : ∀ {a₁ a₂ : A}, a₁ ∈ insert a s' → a₂ ∈ insert a s' → a₁ ≠ a₂ → f a₁ ∩ f a₂ = ∅,
have H2 : ∀ a₁ a₂ : A, a₁ ∈ s' → a₂ ∈ s' → a₁ ≠ a₂ → f a₁ ∩ f a₂ = ∅, from
take a₁ a₂, assume H3 H4 H5,
H1 (!mem_insert_of_mem H3) (!mem_insert_of_mem H4) H5,
have H6 : card (⋃ (x : A) ∈ s', f x) = ∑ (x : A) ∈ s', card (f x), from IH H2,
have H7 : ∀ x, x ∈ s' → f a ∩ f x = ∅, from
take x, assume xs',
have anex : a ≠ x, from assume aex, (eq.subst aex H) xs',
H1 !mem_insert (!mem_insert_of_mem xs') anex,
have H8 : f a ∩ (⋃ (x : A) ∈ s', f x) = ∅, from
calc
f a ∩ (⋃ (x : A) ∈ s', f x) = (⋃ (x : A) ∈ s', f a ∩ f x) : by rewrite inter_Union
... = (⋃ (x : A) ∈ s', ∅) : by rewrite [Union_ext H7]
... = ∅ : by rewrite Union_empty',
by rewrite [Union_insert, Sum_insert_of_not_mem _ H,
card_union_of_disjoint H8, H6])
end deceqB
lemma dvd_Sum_of_dvd (f : A → nat) (n : nat) (s : finset A) : (∀ a, a ∈ s → n ∣ f a) → n ∣ Sum s f :=
begin
induction s with a s nain ih,
{intros, rewrite [Sum_empty], apply dvd_zero},
{intro h,
have h₁ : ∀ a, a ∈ s → n ∣ f a, from
take a, assume ains, h a (mem_insert_of_mem _ ains),
have h₂ : n ∣ Sum s f, from ih h₁,
have h₃ : n ∣ f a, from h a !mem_insert,
rewrite [Sum_insert_of_not_mem f nain],
apply dvd_add h₃ h₂}
end
end finset
|
170f5bce7af3510cee00e1e6f030099982de8a73 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/compiler/bigctor.lean | 84bd189bf805c7aa17dc1e6b7a85f712197365c4 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | leanprover/lean4 | 4bdf9790294964627eb9be79f5e8f6157780b4cc | f1f9dc0f2f531af3312398999d8b8303fa5f096b | refs/heads/master | 1,693,360,665,786 | 1,693,350,868,000 | 1,693,350,868,000 | 129,571,436 | 2,827 | 311 | Apache-2.0 | 1,694,716,156,000 | 1,523,760,560,000 | Lean | UTF-8 | Lean | false | false | 1,382 | lean | set_option genInjectivity false
structure Foo where
x1 : Nat := 0
x2 : Nat := 0
x3 : Nat := 0
x4 : Nat := 0
x5 : Nat := 0
x6 : Nat := 0
x7 : Nat := 0
x8 : Nat := 0
x9 : Nat := 0
x10 : Nat := 0
y1 : Nat := 0
y2 : Nat := 0
y3 : Nat := 0
y4 : Nat := 0
y5 : Nat := 0
y6 : Nat := 0
y7 : Nat := 0
y8 : Nat := 0
y9 : Nat := 0
y10 : Nat := 0
z1 : Nat := 0
z2 : Nat := 0
z3 : Nat := 0
z4 : Nat := 0
z5 : Nat := 0
z6 : Nat := 0
z7 : Nat := 0
z8 : Nat := 0
z9 : Nat := 0
z10 : Nat := 0
w1 : Nat := 0
w2 : Nat := 0
w3 : Nat := 0
w4 : Nat := 0
w5 : Nat := 0
w6 : Nat := 0
w7 : Nat := 0
w8 : Nat := 0
w9 : Nat := 0
w10 : Nat := 0
xx1 : Nat := 0
xx2 : Nat := 0
xx3 : Nat := 0
xx4 : Nat := 0
xx5 : Nat := 0
xx6 : Nat := 0
xx7 : Nat := 0
xx8 : Nat := 0
xx9 : Nat := 0
xx10 : Nat := 0
yy1 : Nat := 0
yy2 : Nat := 0
yy3 : Nat := 0
yy4 : Nat := 0
yy5 : Nat := 0
yy6 : Nat := 0
yy7 : Nat := 0
yy8 : Nat := 0
yy9 : Nat := 0
yy10 : Nat := 0
ww1 : Nat := 0
ww2 : Nat := 0
ww3 : Nat := 0
ww4 : Nat := 0
ww5 : Nat := 0
ww6 : Nat := 0
ww7 : Nat := 0
ww8 : Nat := 0
ww9 : Nat := 0
ww10 : Nat := 0
@[noinline] def mkFoo (x : Nat) : Foo :=
{ yy10 := x }
def main : IO Unit :=
IO.println (mkFoo 10).yy10
|
6bded45969fbaeaac412fd3b0c0411860151e2f0 | bf532e3e865883a676110e756f800e0ddeb465be | /tactic/norm_num.lean | 4a4de34591bba571c55ada68abe193599afbcee6 | [
"Apache-2.0"
] | permissive | aqjune/mathlib | da42a97d9e6670d2efaa7d2aa53ed3585dafc289 | f7977ff5a6bcf7e5c54eec908364ceb40dafc795 | refs/heads/master | 1,631,213,225,595 | 1,521,089,840,000 | 1,521,089,840,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 11,968 | lean | /-
Copyright (c) 2017 Simon Hudon All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon, Mario Carneiro
Evaluating arithmetic expressions including *, +, -, ^, ≤
-/
import algebra.group_power data.rat tactic.interactive
universes u v w
open tactic
namespace expr
protected meta def to_pos_rat : expr → option ℚ
| `(%%e₁ / %%e₂) := do m ← e₁.to_nat, n ← e₂.to_nat, some (rat.mk m n)
| e := do n ← e.to_nat, return (rat.of_int n)
protected meta def to_rat : expr → option ℚ
| `(has_neg.neg %%e) := do q ← e.to_pos_rat, some (-q)
| e := e.to_pos_rat
protected meta def of_rat (α : expr) : ℚ → tactic expr
| ⟨(n:ℕ), d, h, c⟩ := do
e₁ ← expr.of_nat α n,
if d = 1 then return e₁ else
do e₂ ← expr.of_nat α d,
tactic.mk_app ``has_div.div [e₁, e₂]
| ⟨-[1+n], d, h, c⟩ := do
e₁ ← expr.of_nat α (n+1),
e ← (if d = 1 then return e₁ else do
e₂ ← expr.of_nat α d,
tactic.mk_app ``has_div.div [e₁, e₂]),
tactic.mk_app ``has_neg.neg [e]
end expr
namespace norm_num
variable {α : Type u}
theorem bit0_zero [add_group α] : bit0 (0 : α) = 0 := add_zero _
theorem bit1_zero [add_group α] [has_one α] : bit1 (0 : α) = 1 :=
by rw [bit1, bit0_zero, zero_add]
local infix ` ^ ` := monoid.pow
lemma pow_bit0_helper [monoid α] (a t : α) (b : ℕ) (h : a ^ b = t) :
a ^ bit0 b = t * t :=
by simp [pow_bit0, h]
lemma pow_bit1_helper [monoid α] (a t : α) (b : ℕ) (h : a ^ b = t) :
a ^ bit1 b = t * t * a :=
by simp [pow_bit1, h]
lemma lt_add_of_pos_helper [ordered_cancel_comm_monoid α]
(a b c : α) (h : a + b = c) (h₂ : 0 < b) : a < c :=
h ▸ (lt_add_iff_pos_right _).2 h₂
lemma nat_div_helper (a b q r : ℕ) (h : r + q * b = a) (h₂ : r < b) : a / b = q :=
by rw [← h, nat.add_mul_div_right _ _ (lt_of_le_of_lt (nat.zero_le _) h₂),
nat.div_eq_of_lt h₂, zero_add]
lemma int_div_helper (a b q r : ℤ) (h : r + q * b = a) (h₁ : 0 ≤ r) (h₂ : r < b) : a / b = q :=
by rw [← h, int.add_mul_div_right _ _ (ne_of_gt (lt_of_le_of_lt h₁ h₂)),
int.div_eq_zero_of_lt h₁ h₂, zero_add]
lemma nat_mod_helper (a b q r : ℕ) (h : r + q * b = a) (h₂ : r < b) : a % b = r :=
by rw [← h, nat.add_mul_mod_self_right, nat.mod_eq_of_lt h₂]
lemma int_mod_helper (a b q r : ℤ) (h : r + q * b = a) (h₁ : 0 ≤ r) (h₂ : r < b) : a % b = r :=
by rw [← h, int.add_mul_mod_self, int.mod_eq_of_lt h₁ h₂]
meta def eval_pow (simp : expr → tactic (expr × expr)) : expr → tactic (expr × expr)
| `(monoid.pow %%e₁ 0) := do
p ← mk_app ``pow_zero [e₁],
a ← infer_type e₁,
o ← mk_app ``has_one.one [a],
return (o, p)
| `(monoid.pow %%e₁ 1) := do
p ← mk_app ``pow_one [e₁],
return (e₁, p)
| `(monoid.pow %%e₁ (bit0 %%e₂)) := do
e ← mk_app ``monoid.pow [e₁, e₂],
(e', p) ← simp e,
p' ← mk_app ``norm_num.pow_bit0_helper [e₁, e', e₂, p],
e'' ← to_expr ``(%%e' * %%e'),
return (e'', p')
| `(monoid.pow %%e₁ (bit1 %%e₂)) := do
e ← mk_app ``monoid.pow [e₁, e₂],
(e', p) ← simp e,
p' ← mk_app ``norm_num.pow_bit1_helper [e₁, e', e₂, p],
e'' ← to_expr ``(%%e' * %%e' * %%e₁),
return (e'', p')
| `(nat.pow %%e₁ %%e₂) := do
p₁ ← mk_app ``nat.pow_eq_pow [e₁, e₂],
e ← mk_app ``monoid.pow [e₁, e₂],
(e', p₂) ← simp e,
p ← mk_eq_trans p₁ p₂,
return (e', p)
| _ := failed
meta def prove_pos : instance_cache → expr → tactic (instance_cache × expr)
| c `(has_one.one _) := do (c, p) ← c.mk_app ``zero_lt_one [], return (c, p)
| c `(bit0 %%e) := do (c, p) ← prove_pos c e, (c, p) ← c.mk_app ``bit0_pos [e, p], return (c, p)
| c `(bit1 %%e) := do (c, p) ← prove_pos c e, (c, p) ← c.mk_app ``bit1_pos' [e, p], return (c, p)
| c `(%%e₁ / %%e₂) := do
(c, p₁) ← prove_pos c e₁, (c, p₂) ← prove_pos c e₂,
(c, p) ← c.mk_app ``div_pos_of_pos_of_pos [e₁, e₂, p₁, p₂],
return (c, p)
| c e := failed
meta def prove_lt (simp : expr → tactic (expr × expr)) : instance_cache → expr → expr → tactic (instance_cache × expr)
| c `(- %%e₁) `(- %%e₂) := do
(c, p) ← prove_lt c e₁ e₂,
(c, p) ← c.mk_app ``neg_lt_neg [e₁, e₂, p],
return (c, p)
| c `(- %%e₁) `(has_zero.zero _) := do
(c, p) ← prove_pos c e₁,
(c, p) ← c.mk_app ``neg_neg_of_pos [e₁, p],
return (c, p)
| c `(- %%e₁) e₂ := do
(c, p₁) ← prove_pos c e₁,
(c, me₁) ← c.mk_app ``has_neg.neg [e₁],
(c, p₁) ← c.mk_app ``neg_neg_of_pos [e₁, p₁],
(c, p₂) ← prove_pos c e₂,
(c, z) ← c.mk_app ``has_zero.zero [],
(c, p) ← c.mk_app ``lt_trans [me₁, z, e₂, p₁, p₂],
return (c, p)
| c `(has_zero.zero _) e₂ := prove_pos c e₂
| c e₁ e₂ := do
n₁ ← e₁.to_rat, n₂ ← e₂.to_rat,
d ← expr.of_rat c.α (n₂ - n₁),
(c, e₃) ← c.mk_app ``has_add.add [e₁, d],
(e₂', p) ← norm_num e₃,
guard (e₂' =ₐ e₂),
(c, p') ← prove_pos c d,
(c, p) ← c.mk_app ``norm_num.lt_add_of_pos_helper [e₁, d, e₂, p, p'],
return (c, p)
private meta def true_intro (p : expr) : tactic (expr × expr) :=
prod.mk <$> mk_const `true <*> mk_app ``eq_true_intro [p]
private meta def false_intro (p : expr) : tactic (expr × expr) :=
prod.mk <$> mk_const `false <*> mk_app ``eq_false_intro [p]
meta def eval_ineq (simp : expr → tactic (expr × expr)) : expr → tactic (expr × expr)
| `(%%e₁ < %%e₂) := do
n₁ ← e₁.to_rat, n₂ ← e₂.to_rat,
c ← infer_type e₁ >>= mk_instance_cache,
if n₁ < n₂ then
do (_, p) ← prove_lt simp c e₁ e₂, true_intro p
else do
(c, p) ← if n₁ = n₂ then c.mk_app ``lt_irrefl [e₁] else
(do (c, p') ← prove_lt simp c e₂ e₁,
c.mk_app ``not_lt_of_gt [e₁, e₂, p']),
false_intro p
| `(%%e₁ ≤ %%e₂) := do
n₁ ← e₁.to_rat, n₂ ← e₂.to_rat,
c ← infer_type e₁ >>= mk_instance_cache,
if n₁ ≤ n₂ then do
(c, p) ← if n₁ = n₂ then c.mk_app ``le_refl [e₁] else
(do (c, p') ← prove_lt simp c e₁ e₂,
c.mk_app ``le_of_lt [e₁, e₂, p']),
true_intro p
else do
(c, p) ← prove_lt simp c e₂ e₁,
(c, p) ← c.mk_app ``not_le_of_gt [e₁, e₂, p],
false_intro p
| `(%%e₁ = %%e₂) := do
n₁ ← e₁.to_rat, n₂ ← e₂.to_rat,
c ← infer_type e₁ >>= mk_instance_cache,
if n₁ < n₂ then do
(c, p) ← prove_lt simp c e₁ e₂,
(c, p) ← c.mk_app ``ne_of_lt [e₁, e₂, p],
false_intro p
else if n₂ < n₁ then do
(c, p) ← prove_lt simp c e₂ e₁,
(c, p) ← c.mk_app ``ne_of_gt [e₁, e₂, p],
false_intro p
else mk_eq_refl e₁ >>= true_intro
| `(%%e₁ > %%e₂) := mk_app ``has_lt.lt [e₂, e₁] >>= simp
| `(%%e₁ ≥ %%e₂) := mk_app ``has_le.le [e₂, e₁] >>= simp
| `(%%e₁ ≠ %%e₂) := do e ← mk_app ``eq [e₁, e₂], mk_app ``not [e] >>= simp
| _ := failed
meta def eval_div_ext (simp : expr → tactic (expr × expr)) : expr → tactic (expr × expr)
| `(has_inv.inv %%e) := do
c ← infer_type e >>= mk_instance_cache,
(c, p₁) ← c.mk_app ``inv_eq_one_div [e],
(c, o) ← c.mk_app ``has_one.one [],
(c, e') ← c.mk_app ``has_div.div [o, e],
(do (e'', p₂) ← simp e',
p ← mk_eq_trans p₁ p₂,
return (e'', p)) <|> return (e', p₁)
| `(%%e₁ / %%e₂) := do
α ← infer_type e₁,
c ← mk_instance_cache α,
match α with
| `(nat) := do
n₁ ← e₁.to_nat, n₂ ← e₂.to_nat,
q ← expr.of_nat α (n₁ / n₂),
r ← expr.of_nat α (n₁ % n₂),
(c, e₃) ← c.mk_app ``has_mul.mul [q, e₂],
(c, e₃) ← c.mk_app ``has_add.add [r, e₃],
(e₁', p) ← norm_num e₃,
guard (e₁' =ₐ e₁),
(c, p') ← prove_lt simp c r e₂,
p ← mk_app ``norm_num.nat_div_helper [e₁, e₂, q, r, p, p'],
return (q, p)
| `(int) := match e₂ with
| `(- %%e₂') := do
(c, p₁) ← c.mk_app ``int.div_neg [e₁, e₂'],
(c, e) ← c.mk_app ``has_div.div [e₁, e₂'],
(c, e) ← c.mk_app ``has_neg.neg [e],
(e', p₂) ← simp e,
p ← mk_eq_trans p₁ p₂,
return (e', p)
| _ := do
n₁ ← e₁.to_int,
n₂ ← e₂.to_int,
q ← expr.of_rat α $ rat.of_int (n₁ / n₂),
r ← expr.of_rat α $ rat.of_int (n₁ % n₂),
(c, e₃) ← c.mk_app ``has_mul.mul [q, e₂],
(c, e₃) ← c.mk_app ``has_add.add [r, e₃],
(e₁', p) ← norm_num e₃,
guard (e₁' =ₐ e₁),
(c, r0) ← c.mk_app ``has_zero.zero [],
(c, r0) ← c.mk_app ``has_le.le [r0, r],
(_, p₁) ← simp r0,
p₁ ← mk_app ``of_eq_true [p₁],
(c, p₂) ← prove_lt simp c r e₂,
p ← mk_app ``norm_num.int_div_helper [e₁, e₂, q, r, p, p₁, p₂],
return (q, p)
end
| _ := failed
end
| `(%%e₁ % %%e₂) := do
α ← infer_type e₁,
c ← mk_instance_cache α,
match α with
| `(nat) := do
n₁ ← e₁.to_nat, n₂ ← e₂.to_nat,
q ← expr.of_nat α (n₁ / n₂),
r ← expr.of_nat α (n₁ % n₂),
(c, e₃) ← c.mk_app ``has_mul.mul [q, e₂],
(c, e₃) ← c.mk_app ``has_add.add [r, e₃],
(e₁', p) ← norm_num e₃,
guard (e₁' =ₐ e₁),
(c, p') ← prove_lt simp c r e₂,
p ← mk_app ``norm_num.nat_mod_helper [e₁, e₂, q, r, p, p'],
return (r, p)
| `(int) := match e₂ with
| `(- %%e₂') := do
(c, p₁) ← c.mk_app ``int.mod_neg [e₁, e₂'],
(c, e) ← c.mk_app ``has_mod.mod [e₁, e₂'],
(e', p₂) ← simp e,
p ← mk_eq_trans p₁ p₂,
return (e', p)
| _ := do
n₁ ← e₁.to_int,
n₂ ← e₂.to_int,
q ← expr.of_rat α $ rat.of_int (n₁ / n₂),
r ← expr.of_rat α $ rat.of_int (n₁ % n₂),
(c, e₃) ← c.mk_app ``has_mul.mul [q, e₂],
(c, e₃) ← c.mk_app ``has_add.add [r, e₃],
(e₁', p) ← norm_num e₃,
guard (e₁' =ₐ e₁),
(c, r0) ← c.mk_app ``has_zero.zero [],
(c, r0) ← c.mk_app ``has_le.le [r0, r],
(_, p₁) ← simp r0,
p₁ ← mk_app ``of_eq_true [p₁],
(c, p₂) ← prove_lt simp c r e₂,
p ← mk_app ``norm_num.int_mod_helper [e₁, e₂, q, r, p, p₁, p₂],
return (r, p)
end
| _ := failed
end
| _ := failed
meta def derive1 (simp : expr → tactic (expr × expr)) (e : expr) :
tactic (expr × expr) :=
norm_num e <|> eval_div_ext simp e <|> eval_pow simp e <|> eval_ineq simp e
meta def derive : expr → tactic (expr × expr) | e :=
do (_, e', pr) ←
ext_simplify_core () {} simp_lemmas.mk (λ _, failed) (λ _ _ _ _ _, failed)
(λ _ _ _ _ e,
do (new_e, pr) ← derive1 derive e,
guard (¬ new_e =ₐ e),
return ((), new_e, some pr, tt))
`eq e,
return (e', pr)
end norm_num
namespace tactic.interactive
open norm_num interactive interactive.types
/-- Basic version of `norm_num` that does not call `simp`. -/
meta def norm_num1 (loc : parse location) : tactic unit :=
do ns ← loc.get_locals,
tt ← tactic.replace_at derive ns loc.include_goal
| fail "norm_num failed to simplify",
when loc.include_goal $ try tactic.triv,
when (¬ ns.empty) $ try tactic.contradiction
/-- Normalize numerical expressions. Supports the operations
`+` `-` `*` `/` `^` `<` `≤` over ordered fields (or other
appropriate classes), as well as `-` `/` `%` over `ℤ` and `ℕ`. -/
meta def norm_num (loc : parse location) : tactic unit :=
let t := orelse' (norm_num1 loc) $ simp_core {} failed ff [] [] loc in
t >> repeat t
meta def apply_normed (x : parse texpr) : tactic unit :=
do x₁ ← to_expr x,
(x₂,_) ← derive x₁,
tactic.exact x₂
end tactic.interactive
|
320db5366de003c06ca8c1783f7e6b0c43fbaaad | 618003631150032a5676f229d13a079ac875ff77 | /src/data/setoid/basic.lean | 5b8ee12e22f0a9d55941372079b11daae5109bf8 | [
"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 | 16,475 | lean | /-
Copyright (c) 2019 Amelia Livingston. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Amelia Livingston, Bryan Gin-ge Chen
-/
import order.galois_connection
/-!
# Equivalence relations
This file defines the complete lattice of equivalence relations on a type, results about the
inductively defined equivalence closure of a binary relation, and the analogues of some isomorphism
theorems for quotients of arbitrary types.
## Implementation notes
The function `rel` and lemmas ending in ' make it easier to talk about different
equivalence relations on the same type.
The complete lattice instance for equivalence relations could have been defined by lifting
the Galois insertion of equivalence relations on α into binary relations on α, and then using
`complete_lattice.copy` to define a complete lattice instance with more appropriate
definitional equalities (a similar example is `filter.complete_lattice` in
`order/filter/basic.lean`). This does not save space, however, and is less clear.
Partitions are not defined as a separate structure here; users are encouraged to
reason about them using the existing `setoid` and its infrastructure.
## Tags
setoid, equivalence, iseqv, relation, equivalence relation
-/
variables {α : Type*} {β : Type*}
/-- A version of `setoid.r` that takes the equivalence relation as an explicit argument. -/
def setoid.rel (r : setoid α) : α → α → Prop := @setoid.r _ r
/-- A version of `quotient.eq'` compatible with `setoid.rel`, to make rewriting possible. -/
lemma quotient.eq_rel {r : setoid α} {x y} : ⟦x⟧ = ⟦y⟧ ↔ r.rel x y := quotient.eq'
namespace setoid
@[ext] lemma ext' {r s : setoid α} (H : ∀ a b, r.rel a b ↔ s.rel a b) :
r = s := ext H
lemma ext_iff {r s : setoid α} : r = s ↔ ∀ a b, r.rel a b ↔ s.rel a b :=
⟨λ h a b, h ▸ iff.rfl, ext'⟩
/-- Two equivalence relations are equal iff their underlying binary operations are equal. -/
theorem eq_iff_rel_eq {r₁ r₂ : setoid α} : r₁ = r₂ ↔ r₁.rel = r₂.rel :=
⟨λ h, h ▸ rfl, λ h, setoid.ext' $ λ x y, h ▸ iff.rfl⟩
/-- Defining `≤` for equivalence relations. -/
instance : has_le (setoid α) := ⟨λ r s, ∀ ⦃x y⦄, r.rel x y → s.rel x y⟩
theorem le_def {r s : setoid α} : r ≤ s ↔ ∀ {x y}, r.rel x y → s.rel x y := iff.rfl
@[refl] lemma refl' (r : setoid α) (x) : r.rel x x := r.2.1 x
@[symm] lemma symm' (r : setoid α) : ∀ {x y}, r.rel x y → r.rel y x := λ _ _ h, r.2.2.1 h
@[trans] lemma trans' (r : setoid α) : ∀ {x y z}, r.rel x y → r.rel y z → r.rel x z :=
λ _ _ _ hx, r.2.2.2 hx
/-- The kernel of a function is an equivalence relation. -/
def ker (f : α → β) : setoid α :=
⟨λ x y, f x = f y, ⟨λ _, rfl, λ _ _ h, h.symm, λ _ _ _ h, h.trans⟩⟩
/-- The kernel of the quotient map induced by an equivalence relation r equals r. -/
@[simp] lemma ker_mk_eq (r : setoid α) : ker (@quotient.mk _ r) = r :=
ext' $ λ x y, quotient.eq
lemma ker_def {f : α → β} {x y : α} : (ker f).rel x y ↔ f x = f y := iff.rfl
/-- Given types `α`, `β`, the product of two equivalence relations `r` on `α` and `s` on `β`:
`(x₁, x₂), (y₁, y₂) ∈ α × β` are related by `r.prod s` iff `x₁` is related to `y₁`
by `r` and `x₂` is related to `y₂` by `s`. -/
protected def prod (r : setoid α) (s : setoid β) : setoid (α × β) :=
{ r := λ x y, r.rel x.1 y.1 ∧ s.rel x.2 y.2,
iseqv := ⟨λ x, ⟨r.refl' x.1, s.refl' x.2⟩, λ _ _ h, ⟨r.symm' h.1, s.symm' h.2⟩,
λ _ _ _ h1 h2, ⟨r.trans' h1.1 h2.1, s.trans' h1.2 h2.2⟩⟩ }
/-- The infimum of two equivalence relations. -/
instance : has_inf (setoid α) :=
⟨λ r s, ⟨λ x y, r.rel x y ∧ s.rel x y, ⟨λ x, ⟨r.refl' x, s.refl' x⟩,
λ _ _ h, ⟨r.symm' h.1, s.symm' h.2⟩,
λ _ _ _ h1 h2, ⟨r.trans' h1.1 h2.1, s.trans' h1.2 h2.2⟩⟩⟩⟩
/-- The infimum of 2 equivalence relations r and s is the same relation as the infimum
of the underlying binary operations. -/
lemma inf_def {r s : setoid α} : (r ⊓ s).rel = r.rel ⊓ s.rel := rfl
theorem inf_iff_and {r s : setoid α} {x y} :
(r ⊓ s).rel x y ↔ r.rel x y ∧ s.rel x y := iff.rfl
/-- The infimum of a set of equivalence relations. -/
instance : has_Inf (setoid α) :=
⟨λ S, ⟨λ x y, ∀ r ∈ S, rel r x y,
⟨λ x r hr, r.refl' x, λ _ _ h r hr, r.symm' $ h r hr,
λ _ _ _ h1 h2 r hr, r.trans' (h1 r hr) $ h2 r hr⟩⟩⟩
/-- The underlying binary operation of the infimum of a set of equivalence relations
is the infimum of the set's image under the map to the underlying binary operation. -/
theorem Inf_def {s : set (setoid α)} : (Inf s).rel = Inf (rel '' s) :=
by { ext, simp only [Inf_image, infi_apply, infi_Prop_eq], refl }
instance : partial_order (setoid α) :=
{ le := (≤),
lt := λ r s, r ≤ s ∧ ¬s ≤ r,
le_refl := λ _ _ _, id,
le_trans := λ _ _ _ hr hs _ _ h, hs $ hr h,
lt_iff_le_not_le := λ _ _, iff.rfl,
le_antisymm := λ r s h1 h2, setoid.ext' $ λ x y, ⟨λ h, h1 h, λ h, h2 h⟩ }
/-- The complete lattice of equivalence relations on a type, with bottom element `=`
and top element the trivial equivalence relation. -/
instance complete_lattice : complete_lattice (setoid α) :=
{ inf := has_inf.inf,
inf_le_left := λ _ _ _ _ h, h.1,
inf_le_right := λ _ _ _ _ h, h.2,
le_inf := λ _ _ _ h1 h2 _ _ h, ⟨h1 h, h2 h⟩,
top := ⟨λ _ _, true, ⟨λ _, trivial, λ _ _ h, h, λ _ _ _ h1 h2, h1⟩⟩,
le_top := λ _ _ _ _, trivial,
bot := ⟨(=), ⟨λ _, rfl, λ _ _ h, h.symm, λ _ _ _ h1 h2, h1.trans h2⟩⟩,
bot_le := λ r x y h, h ▸ r.2.1 x,
.. complete_lattice_of_Inf (setoid α) $ assume s,
⟨λ r hr x y h, h _ hr, λ r hr x y h r' hr', hr hr' h⟩ }
/-- The inductively defined equivalence closure of a binary relation r is the infimum
of the set of all equivalence relations containing r. -/
theorem eqv_gen_eq (r : α → α → Prop) :
eqv_gen.setoid r = Inf {s : setoid α | ∀ ⦃x y⦄, r x y → s.rel x y} :=
le_antisymm
(λ _ _ H, eqv_gen.rec (λ _ _ h _ hs, hs h) (refl' _)
(λ _ _ _, symm' _) (λ _ _ _ _ _, trans' _) H)
(Inf_le $ λ _ _ h, eqv_gen.rel _ _ h)
/-- The supremum of two equivalence relations r and s is the equivalence closure of the binary
relation `x is related to y by r or s`. -/
lemma sup_eq_eqv_gen (r s : setoid α) :
r ⊔ s = eqv_gen.setoid (λ x y, r.rel x y ∨ s.rel x y) :=
begin
rw eqv_gen_eq,
apply congr_arg Inf,
simp only [le_def, or_imp_distrib, ← forall_and_distrib]
end
/-- The supremum of 2 equivalence relations r and s is the equivalence closure of the
supremum of the underlying binary operations. -/
lemma sup_def {r s : setoid α} : r ⊔ s = eqv_gen.setoid (r.rel ⊔ s.rel) :=
by rw sup_eq_eqv_gen; refl
/-- The supremum of a set S of equivalence relations is the equivalence closure of the binary
relation `there exists r ∈ S relating x and y`. -/
lemma Sup_eq_eqv_gen (S : set (setoid α)) :
Sup S = eqv_gen.setoid (λ x y, ∃ r : setoid α, r ∈ S ∧ r.rel x y) :=
begin
rw eqv_gen_eq,
apply congr_arg Inf,
simp only [upper_bounds, le_def, and_imp, exists_imp_distrib],
ext,
exact ⟨λ H x y r hr, H hr, λ H r hr x y, H r hr⟩
end
/-- The supremum of a set of equivalence relations is the equivalence closure of the
supremum of the set's image under the map to the underlying binary operation. -/
lemma Sup_def {s : set (setoid α)} : Sup s = eqv_gen.setoid (Sup (rel '' s)) :=
begin
rw Sup_eq_eqv_gen,
congr,
ext x y,
erw [Sup_image, supr_apply, supr_apply, supr_Prop_eq],
simp only [Sup_image, supr_Prop_eq, supr_apply, supr_Prop_eq, exists_prop]
end
/-- The equivalence closure of an equivalence relation r is r. -/
@[simp] lemma eqv_gen_of_setoid (r : setoid α) : eqv_gen.setoid r.r = r :=
le_antisymm (by rw eqv_gen_eq; exact Inf_le (λ _ _, id)) eqv_gen.rel
/-- Equivalence closure is idempotent. -/
@[simp] lemma eqv_gen_idem (r : α → α → Prop) :
eqv_gen.setoid (eqv_gen.setoid r).rel = eqv_gen.setoid r :=
eqv_gen_of_setoid _
/-- The equivalence closure of a binary relation r is contained in any equivalence
relation containing r. -/
theorem eqv_gen_le {r : α → α → Prop} {s : setoid α} (h : ∀ x y, r x y → s.rel x y) :
eqv_gen.setoid r ≤ s :=
by rw eqv_gen_eq; exact Inf_le h
/-- Equivalence closure of binary relations is monotonic. -/
theorem eqv_gen_mono {r s : α → α → Prop} (h : ∀ x y, r x y → s x y) :
eqv_gen.setoid r ≤ eqv_gen.setoid s :=
eqv_gen_le $ λ _ _ hr, eqv_gen.rel _ _ $ h _ _ hr
/-- There is a Galois insertion of equivalence relations on α into binary relations
on α, with equivalence closure the lower adjoint. -/
def gi : @galois_insertion (α → α → Prop) (setoid α) _ _ eqv_gen.setoid rel :=
{ choice := λ r h, eqv_gen.setoid r,
gc := λ r s, ⟨λ H _ _ h, H $ eqv_gen.rel _ _ h, λ H, eqv_gen_of_setoid s ▸ eqv_gen_mono H⟩,
le_l_u := λ x, (eqv_gen_of_setoid x).symm ▸ le_refl x,
choice_eq := λ _ _, rfl }
open function
/-- A function from α to β is injective iff its kernel is the bottom element of the complete lattice
of equivalence relations on α. -/
theorem injective_iff_ker_bot (f : α → β) :
injective f ↔ ker f = ⊥ :=
(@eq_bot_iff (setoid α) _ (ker f)).symm
/-- The elements related to x ∈ α by the kernel of f are those in the preimage of f(x) under f. -/
lemma ker_iff_mem_preimage {f : α → β} {x y} : (ker f).rel x y ↔ x ∈ f ⁻¹' {f y} :=
iff.rfl
/-- Equivalence between functions `α → β` such that `r x y → f x = f y` and functions
`quotient r → β`. -/
def lift_equiv (r : setoid α) : {f : α → β // r ≤ ker f} ≃ (quotient r → β) :=
{ to_fun := λ f, quotient.lift (f : α → β) f.2,
inv_fun := λ f, ⟨f ∘ quotient.mk, λ x y h, by simp [ker_def, quotient.sound h]⟩,
left_inv := λ ⟨f, hf⟩, subtype.eq $ funext $ λ x, rfl,
right_inv := λ f, funext $ λ x, quotient.induction_on' x $ λ x, rfl }
/-- The uniqueness part of the universal property for quotients of an arbitrary type. -/
theorem lift_unique {r : setoid α} {f : α → β} (H : r ≤ ker f) (g : quotient r → β)
(Hg : f = g ∘ quotient.mk) : quotient.lift f H = g :=
begin
ext ⟨x⟩,
erw [quotient.lift_beta f H, Hg],
refl
end
/-- Given a map f from α to β, the natural map from the quotient of α by the kernel of f is
injective. -/
lemma injective_ker_lift (f : α → β) : injective (@quotient.lift _ _ (ker f) f (λ _ _ h, h)) :=
λ x y, quotient.induction_on₂' x y $ λ a b h, quotient.sound' h
/-- Given a map f from α to β, the kernel of f is the unique equivalence relation on α whose
induced map from the quotient of α to β is injective. -/
lemma ker_eq_lift_of_injective {r : setoid α} (f : α → β) (H : ∀ x y, r.rel x y → f x = f y)
(h : injective (quotient.lift f H)) : ker f = r :=
le_antisymm
(λ x y hk, quotient.exact $ h $ show quotient.lift f H ⟦x⟧ = quotient.lift f H ⟦y⟧, from hk)
H
variables (r : setoid α) (f : α → β)
/-- The first isomorphism theorem for sets: the quotient of α by the kernel of a function f
bijects with f's image. -/
noncomputable def quotient_ker_equiv_range :
quotient (ker f) ≃ set.range f :=
@equiv.of_bijective _ (set.range f) (@quotient.lift _ (set.range f) (ker f)
(λ x, ⟨f x, set.mem_range_self x⟩) $ λ _ _ h, subtype.eq' h)
⟨λ x y h, injective_ker_lift f $ by rcases x; rcases y; injections,
λ ⟨w, z, hz⟩, ⟨@quotient.mk _ (ker f) z, by rw quotient.lift_beta; exact subtype.ext.2 hz⟩⟩
/-- The quotient of α by the kernel of a surjective function f bijects with f's codomain. -/
noncomputable def quotient_ker_equiv_of_surjective (hf : surjective f) :
quotient (ker f) ≃ β :=
(quotient_ker_equiv_range f).trans $ equiv.subtype_univ_equiv hf
variables {r f}
/-- Given a function `f : α → β` and equivalence relation `r` on `α`, the equivalence
closure of the relation on `f`'s image defined by '`x ≈ y` iff the elements of `f⁻¹(x)` are
related to the elements of `f⁻¹(y)` by `r`.' -/
def map (r : setoid α) (f : α → β) : setoid β :=
eqv_gen.setoid $ λ x y, ∃ a b, f a = x ∧ f b = y ∧ r.rel a b
/-- Given a surjective function f whose kernel is contained in an equivalence relation r, the
equivalence relation on f's codomain defined by x ≈ y ↔ the elements of f⁻¹(x) are related to
the elements of f⁻¹(y) by r. -/
def map_of_surjective (r) (f : α → β) (h : ker f ≤ r) (hf : surjective f) :
setoid β :=
⟨λ x y, ∃ a b, f a = x ∧ f b = y ∧ r.rel a b,
⟨λ x, let ⟨y, hy⟩ := hf x in ⟨y, y, hy, hy, r.refl' y⟩,
λ _ _ ⟨x, y, hx, hy, h⟩, ⟨y, x, hy, hx, r.symm' h⟩,
λ _ _ _ ⟨x, y, hx, hy, h₁⟩ ⟨y', z, hy', hz, h₂⟩,
⟨x, z, hx, hz, r.trans' h₁ $ r.trans' (h $ by rwa ←hy' at hy) h₂⟩⟩⟩
/-- A special case of the equivalence closure of an equivalence relation r equalling r. -/
lemma map_of_surjective_eq_map (h : ker f ≤ r) (hf : surjective f) :
map r f = map_of_surjective r f h hf :=
by rw ←eqv_gen_of_setoid (map_of_surjective r f h hf); refl
/-- Given a function `f : α → β`, an equivalence relation `r` on `β` induces an equivalence
relation on `α` defined by '`x ≈ y` iff `f(x)` is related to `f(y)` by `r`'. -/
def comap (f : α → β) (r : setoid β) : setoid α :=
⟨λ x y, r.rel (f x) (f y), ⟨λ _, r.refl' _, λ _ _ h, r.symm' h, λ _ _ _ h1, r.trans' h1⟩⟩
/-- Given a map `f : N → M` and an equivalence relation `r` on `β`, the equivalence relation
induced on `α` by `f` equals the kernel of `r`'s quotient map composed with `f`. -/
lemma comap_eq {f : α → β} {r : setoid β} : comap f r = ker (@quotient.mk _ r ∘ f) :=
ext $ λ x y, show _ ↔ ⟦_⟧ = ⟦_⟧, by rw quotient.eq; refl
/-- The second isomorphism theorem for sets. -/
noncomputable def comap_quotient_equiv (f : α → β) (r : setoid β) :
quotient (comap f r) ≃ set.range (@quotient.mk _ r ∘ f) :=
(quotient.congr_right $ ext_iff.1 comap_eq).trans $ quotient_ker_equiv_range $ quotient.mk ∘ f
variables (r f)
/-- The third isomorphism theorem for sets. -/
def quotient_quotient_equiv_quotient (s : setoid α) (h : r ≤ s) :
quotient (ker (quot.map_right h)) ≃ quotient s :=
{ to_fun := λ x, quotient.lift_on' x (λ w, quotient.lift_on' w (@quotient.mk _ s) $
λ x y H, quotient.sound $ h H) $ λ x y, quotient.induction_on₂' x y $ λ w z H,
show @quot.mk _ _ _ = @quot.mk _ _ _, from H,
inv_fun := λ x, quotient.lift_on' x
(λ w, @quotient.mk _ (ker $ quot.map_right h) $ @quotient.mk _ r w) $
λ x y H, quotient.sound' $ show @quot.mk _ _ _ = @quot.mk _ _ _, from quotient.sound H,
left_inv := λ x, quotient.induction_on' x $ λ y, quotient.induction_on' y $
λ w, by show ⟦_⟧ = _; refl,
right_inv := λ x, quotient.induction_on' x $ λ y, by show ⟦_⟧ = _; refl }
variables {r f}
open quotient
/-- Given an equivalence relation r on α, the order-preserving bijection between the set of
equivalence relations containing r and the equivalence relations on the quotient of α by r. -/
def correspondence (r : setoid α) : ((≤) : {s // r ≤ s} → {s // r ≤ s} → Prop) ≃o
((≤) : setoid (quotient r) → setoid (quotient r) → Prop) :=
{ to_fun := λ s, map_of_surjective s.1 quotient.mk ((ker_mk_eq r).symm ▸ s.2) exists_rep,
inv_fun := λ s, ⟨comap quotient.mk s, λ x y h, show s.rel ⟦x⟧ ⟦y⟧, by rw eq_rel.2 h⟩,
left_inv := λ s, subtype.ext.2 $ ext' $ λ _ _,
⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in
s.1.trans' (s.1.symm' $ s.2 $ eq_rel.1 hx) $ s.1.trans' H $ s.2 $ eq_rel.1 hy,
λ h, ⟨_, _, rfl, rfl, h⟩⟩,
right_inv := λ s, let Hm : ker quotient.mk ≤ comap quotient.mk s :=
λ x y h, show s.rel ⟦x⟧ ⟦y⟧, by rw (@eq_rel _ r x y).2 ((ker_mk_eq r) ▸ h) in
ext' $ λ x y, ⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in hx ▸ hy ▸ H,
quotient.induction_on₂ x y $ λ w z h, ⟨w, z, rfl, rfl, h⟩⟩,
ord' := λ s t, ⟨λ h x y hs, let ⟨a, b, hx, hy, Hs⟩ := hs in ⟨a, b, hx, hy, h Hs⟩,
λ h x y hs, let ⟨a, b, hx, hy, ht⟩ := h ⟨x, y, rfl, rfl, hs⟩ in
t.1.trans' (t.1.symm' $ t.2 $ eq_rel.1 hx) $ t.1.trans' ht $ t.2 $ eq_rel.1 hy⟩ }
end setoid
|
bf3e30205712626121d715976fbba9be946bb1c1 | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /src/Init/Lean/Declaration.lean | 50771041c4be6bbad9c6e4f6b8c39f133a79c0a3 | [
"Apache-2.0"
] | permissive | mhuisi/lean4 | 28d35a4febc2e251c7f05492e13f3b05d6f9b7af | dda44bc47f3e5d024508060dac2bcb59fd12e4c0 | refs/heads/master | 1,621,225,489,283 | 1,585,142,689,000 | 1,585,142,689,000 | 250,590,438 | 0 | 2 | Apache-2.0 | 1,602,443,220,000 | 1,585,327,814,000 | C | UTF-8 | Lean | false | false | 12,143 | 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
-/
prelude
import Init.Lean.Expr
namespace Lean
/--
Reducibility hints are used in the convertibility checker.
When trying to solve a constraint such a
(f ...) =?= (g ...)
where f and g are definitions, the checker has to decide which one will be unfolded.
If f (g) is opaque, then g (f) is unfolded if it is also not marked as opaque,
Else if f (g) is abbrev, then f (g) is unfolded if g (f) is also not marked as abbrev,
Else if f and g are regular, then we unfold the one with the biggest definitional height.
Otherwise both are unfolded.
The arguments of the `regular` Constructor are: the definitional height and the flag `selfOpt`.
The definitional height is by default computed by the kernel. It only takes into account
other regular definitions used in a definition. When creating declarations using meta-programming,
we can specify the definitional depth manually.
Remark: the hint only affects performance. None of the hints prevent the kernel from unfolding a
declaration during Type checking.
Remark: the ReducibilityHints are not related to the attributes: reducible/irrelevance/semireducible.
These attributes are used by the Elaborator. The ReducibilityHints are used by the kernel (and Elaborator).
Moreover, the ReducibilityHints cannot be changed after a declaration is added to the kernel. -/
inductive ReducibilityHints
| opaque : ReducibilityHints
| «abbrev» : ReducibilityHints
| regular : UInt32 → ReducibilityHints
@[export lean_mk_reducibility_hints_regular]
def mkReducibilityHintsRegularEx (h : UInt32) : ReducibilityHints := ReducibilityHints.regular h
@[export lean_reducibility_hints_get_height]
def ReducibilityHints.getHeightEx (h : ReducibilityHints) : UInt32 :=
match h with
| ReducibilityHints.regular h => h
| _ => 0
namespace ReducibilityHints
instance : Inhabited ReducibilityHints := ⟨opaque⟩
def lt : ReducibilityHints → ReducibilityHints → Bool
| «abbrev», «abbrev» => false
| «abbrev», _ => true
| regular d₁, regular d₂ => d₁ < d₂
| regular _, opaque => true
| _, _ => false
end ReducibilityHints
/-- Base structure for `AxiomVal`, `DefinitionVal`, `TheoremVal`, `InductiveVal`, `ConstructorVal`, `RecursorVal` and `QuotVal`. -/
structure ConstantVal :=
(name : Name) (lparams : List Name) (type : Expr)
instance ConstantVal.inhabited : Inhabited ConstantVal := ⟨{ name := arbitrary _, lparams := arbitrary _, type := arbitrary _ }⟩
structure AxiomVal extends ConstantVal :=
(isUnsafe : Bool)
@[export lean_mk_axiom_val]
def mkAxiomValEx (name : Name) (lparams : List Name) (type : Expr) (isUnsafe : Bool) : AxiomVal :=
{ name := name, lparams := lparams, type := type, isUnsafe := isUnsafe }
@[export lean_axiom_val_is_unsafe] def AxiomVal.isUnsafeEx (v : AxiomVal) : Bool := v.isUnsafe
structure DefinitionVal extends ConstantVal :=
(value : Expr) (hints : ReducibilityHints) (isUnsafe : Bool)
@[export lean_mk_definition_val]
def mkDefinitionValEx (name : Name) (lparams : List Name) (type : Expr) (val : Expr) (hints : ReducibilityHints) (isUnsafe : Bool) : DefinitionVal :=
{ name := name, lparams := lparams, type := type, value := val, hints := hints, isUnsafe := isUnsafe }
@[export lean_definition_val_is_unsafe] def DefinitionVal.isUnsafeEx (v : DefinitionVal) : Bool := v.isUnsafe
structure TheoremVal extends ConstantVal :=
(value : Task Expr)
/- Value for an opaque constant declaration `constant x : t := e` -/
structure OpaqueVal extends ConstantVal :=
(value : Expr) (isUnsafe : Bool)
@[export lean_mk_opaque_val]
def mkOpaqueValEx (name : Name) (lparams : List Name) (type : Expr) (val : Expr) (isUnsafe : Bool) : OpaqueVal :=
{ name := name, lparams := lparams, type := type, value := val, isUnsafe := isUnsafe }
@[export lean_opaque_val_is_unsafe] def OpaqueVal.isUnsafeEx (v : OpaqueVal) : Bool := v.isUnsafe
structure Constructor :=
(name : Name) (type : Expr)
structure InductiveType :=
(name : Name) (type : Expr) (ctors : List Constructor)
/-- Declaration object that can be sent to the kernel. -/
inductive Declaration
| axiomDecl (val : AxiomVal)
| defnDecl (val : DefinitionVal)
| thmDecl (val : TheoremVal)
| opaqueDecl (val : OpaqueVal)
| quotDecl
| mutualDefnDecl (defns : List DefinitionVal) -- All definitions must be marked as `unsafe`
| inductDecl (lparams : List Name) (nparams : Nat) (types : List InductiveType) (isUnsafe : Bool)
@[export lean_mk_inductive_decl]
def mkInductiveDeclEs (lparams : List Name) (nparams : Nat) (types : List InductiveType) (isUnsafe : Bool) : Declaration :=
Declaration.inductDecl lparams nparams types isUnsafe
@[export lean_is_unsafe_inductive_decl]
def Declaration.isUnsafeInductiveDeclEx : Declaration → Bool
| Declaration.inductDecl _ _ _ isUnsafe => isUnsafe
| _ => false
/-- The kernel compiles (mutual) inductive declarations (see `inductiveDecls`) into a set of
- `Declaration.inductDecl` (for each inductive datatype in the mutual Declaration),
- `Declaration.ctorDecl` (for each Constructor in the mutual Declaration),
- `Declaration.recDecl` (automatically generated recursors).
This data is used to implement iota-reduction efficiently and compile nested inductive
declarations.
A series of checks are performed by the kernel to check whether a `inductiveDecls`
is valid or not. -/
structure InductiveVal extends ConstantVal :=
(nparams : Nat) -- Number of parameters
(nindices : Nat) -- Number of indices
(all : List Name) -- List of all (including this one) inductive datatypes in the mutual declaration containing this one
(ctors : List Name) -- List of all constructors for this inductive datatype
(isRec : Bool) -- `true` Iff it is recursive
(isUnsafe : Bool)
(isReflexive : Bool)
@[export lean_mk_inductive_val]
def mkInductiveValEx (name : Name) (lparams : List Name) (type : Expr) (nparams nindices : Nat)
(all ctors : List Name) (isRec isUnsafe isReflexive : Bool) : InductiveVal :=
{ name := name, lparams := lparams, type := type, nparams := nparams, nindices := nindices, all := all, ctors := ctors,
isRec := isRec, isUnsafe := isUnsafe, isReflexive := isReflexive }
@[export lean_inductive_val_is_rec] def InductiveVal.isRecEx (v : InductiveVal) : Bool := v.isRec
@[export lean_inductive_val_is_unsafe] def InductiveVal.isUnsafeEx (v : InductiveVal) : Bool := v.isUnsafe
@[export lean_inductive_val_is_reflexive] def InductiveVal.isReflexiveEx (v : InductiveVal) : Bool := v.isReflexive
namespace InductiveVal
def nctors (v : InductiveVal) : Nat := v.ctors.length
end InductiveVal
structure ConstructorVal extends ConstantVal :=
(induct : Name) -- Inductive Type this Constructor is a member of
(cidx : Nat) -- Constructor index (i.e., Position in the inductive declaration)
(nparams : Nat) -- Number of parameters in inductive datatype `induct`
(nfields : Nat) -- Number of fields (i.e., arity - nparams)
(isUnsafe : Bool)
@[export lean_mk_constructor_val]
def mkConstructorValEx (name : Name) (lparams : List Name) (type : Expr) (induct : Name) (cidx nparams nfields : Nat) (isUnsafe : Bool) : ConstructorVal :=
{ name := name, lparams := lparams, type := type, induct := induct, cidx := cidx, nparams := nparams, nfields := nfields, isUnsafe := isUnsafe }
@[export lean_constructor_val_is_unsafe] def ConstructorVal.isUnsafeEx (v : ConstructorVal) : Bool := v.isUnsafe
instance ConstructorVal.inhabited : Inhabited ConstructorVal :=
⟨{ toConstantVal := arbitrary _, induct := arbitrary _, cidx := 0, nparams := 0, nfields := 0, isUnsafe := true }⟩
/-- Information for reducing a recursor -/
structure RecursorRule :=
(ctor : Name) -- Reduction rule for this Constructor
(nfields : Nat) -- Number of fields (i.e., without counting inductive datatype parameters)
(rhs : Expr) -- Right hand side of the reduction rule
structure RecursorVal extends ConstantVal :=
(all : List Name) -- List of all inductive datatypes in the mutual declaration that generated this recursor
(nparams : Nat) -- Number of parameters
(nindices : Nat) -- Number of indices
(nmotives : Nat) -- Number of motives
(nminors : Nat) -- Number of minor premises
(rules : List RecursorRule) -- A reduction for each Constructor
(k : Bool) -- It supports K-like reduction
(isUnsafe : Bool)
@[export lean_mk_recursor_val]
def mkRecursorValEx (name : Name) (lparams : List Name) (type : Expr) (all : List Name) (nparams nindices nmotives nminors : Nat)
(rules : List RecursorRule) (k isUnsafe : Bool) : RecursorVal :=
{ name := name, lparams := lparams, type := type, all := all, nparams := nparams, nindices := nindices,
nmotives := nmotives, nminors := nminors, rules := rules, k := k, isUnsafe := isUnsafe }
@[export lean_recursor_k] def RecursorVal.kEx (v : RecursorVal) : Bool := v.k
@[export lean_recursor_is_unsafe] def RecursorVal.isUnsafeEx (v : RecursorVal) : Bool := v.isUnsafe
namespace RecursorVal
def getMajorIdx (v : RecursorVal) : Nat :=
v.nparams + v.nmotives + v.nminors + v.nindices
def getInduct (v : RecursorVal) : Name :=
v.name.getPrefix
end RecursorVal
inductive QuotKind
| type -- `Quot`
| ctor -- `Quot.mk`
| lift -- `Quot.lift`
| ind -- `Quot.ind`
structure QuotVal extends ConstantVal :=
(kind : QuotKind)
@[export lean_mk_quot_val]
def mkQuotValEx (name : Name) (lparams : List Name) (type : Expr) (kind : QuotKind) : QuotVal :=
{ name := name, lparams := lparams, type := type, kind := kind }
@[export lean_quot_val_kind] def QuotVal.kindEx (v : QuotVal) : QuotKind := v.kind
/-- Information associated with constant declarations. -/
inductive ConstantInfo
| axiomInfo (val : AxiomVal)
| defnInfo (val : DefinitionVal)
| thmInfo (val : TheoremVal)
| opaqueInfo (val : OpaqueVal)
| quotInfo (val : QuotVal)
| inductInfo (val : InductiveVal)
| ctorInfo (val : ConstructorVal)
| recInfo (val : RecursorVal)
namespace ConstantInfo
def toConstantVal : ConstantInfo → ConstantVal
| defnInfo {toConstantVal := d, ..} => d
| axiomInfo {toConstantVal := d, ..} => d
| thmInfo {toConstantVal := d, ..} => d
| opaqueInfo {toConstantVal := d, ..} => d
| quotInfo {toConstantVal := d, ..} => d
| inductInfo {toConstantVal := d, ..} => d
| ctorInfo {toConstantVal := d, ..} => d
| recInfo {toConstantVal := d, ..} => d
def isUnsafe : ConstantInfo → Bool
| defnInfo v => v.isUnsafe
| axiomInfo v => v.isUnsafe
| thmInfo v => false
| opaqueInfo v => v.isUnsafe
| quotInfo v => false
| inductInfo v => v.isUnsafe
| ctorInfo v => v.isUnsafe
| recInfo v => v.isUnsafe
def name (d : ConstantInfo) : Name :=
d.toConstantVal.name
def lparams (d : ConstantInfo) : List Name :=
d.toConstantVal.lparams
def type (d : ConstantInfo) : Expr :=
d.toConstantVal.type
def value? : ConstantInfo → Option Expr
| defnInfo {value := r, ..} => some r
| thmInfo {value := r, ..} => some r.get
| _ => none
def hasValue : ConstantInfo → Bool
| defnInfo {value := r, ..} => true
| thmInfo {value := r, ..} => true
| _ => false
def value! : ConstantInfo → Expr
| defnInfo {value := r, ..} => r
| thmInfo {value := r, ..} => r.get
| _ => panic! "declaration with value expected"
def hints : ConstantInfo → ReducibilityHints
| defnInfo {hints := r, ..} => r
| _ => ReducibilityHints.opaque
def isCtor : ConstantInfo → Bool
| ctorInfo _ => true
| _ => false
@[extern "lean_instantiate_type_lparams"]
constant instantiateTypeLevelParams (c : @& ConstantInfo) (ls : @& List Level) : Expr := arbitrary _
@[extern "lean_instantiate_value_lparams"]
constant instantiateValueLevelParams (c : @& ConstantInfo) (ls : @& List Level) : Expr := arbitrary _
end ConstantInfo
def mkRecFor (declName : Name) : Name :=
mkNameStr declName "rec"
end Lean
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.