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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
a0e09ba22e00e2c2b07dcd1789bb357da3bca57b | f5f7e6fae601a5fe3cac7cc3ed353ed781d62419 | /src/analysis/specific_limits.lean | 20eaeab6355585d3d2d6328c7d04bd729ce90839 | [
"Apache-2.0"
] | permissive | EdAyers/mathlib | 9ecfb2f14bd6caad748b64c9c131befbff0fb4e0 | ca5d4c1f16f9c451cf7170b10105d0051db79e1b | refs/heads/master | 1,626,189,395,845 | 1,555,284,396,000 | 1,555,284,396,000 | 144,004,030 | 0 | 0 | Apache-2.0 | 1,533,727,664,000 | 1,533,727,663,000 | null | UTF-8 | Lean | false | false | 7,352 | 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
A collection of specific limit computations.
-/
import analysis.normed_space.basic
import topology.instances.ennreal
noncomputable theory
local attribute [instance] classical.prop_decidable
open classical function lattice filter finset metric
variables {α : Type*} {β : Type*} {ι : Type*}
lemma summable_of_absolute_convergence_real {f : ℕ → ℝ} :
(∃r, tendsto (λn, (range n).sum (λi, abs (f i))) at_top (nhds r)) → summable f
| ⟨r, hr⟩ :=
begin
refine summable_of_summable_norm ⟨r, (has_sum_iff_tendsto_nat_of_nonneg _ _).2 _⟩,
exact assume i, norm_nonneg _,
simpa only using hr
end
lemma tendsto_pow_at_top_at_top_of_gt_1 {r : ℝ} (h : r > 1) : tendsto (λn:ℕ, r ^ n) at_top at_top :=
tendsto_infi.2 $ assume p, tendsto_principal.2 $
let ⟨n, hn⟩ := exists_nat_gt (p / (r - 1)) in
have hn_nn : (0:ℝ) ≤ n, from nat.cast_nonneg n,
have r - 1 > 0, from sub_lt_iff_lt_add.mp $ by simp; assumption,
have p ≤ r ^ n,
from calc p = (p / (r - 1)) * (r - 1) : (div_mul_cancel _ $ ne_of_gt this).symm
... ≤ n * (r - 1) : mul_le_mul (le_of_lt hn) (le_refl _) (le_of_lt this) hn_nn
... ≤ 1 + n * (r - 1) : le_add_of_nonneg_of_le zero_le_one (le_refl _)
... = 1 + add_monoid.smul n (r - 1) : by rw [add_monoid.smul_eq_mul]
... ≤ (1 + (r - 1)) ^ n : pow_ge_one_add_mul (le_of_lt this) _
... ≤ r ^ n : by simp; exact le_refl _,
show {n | p ≤ r ^ n} ∈ at_top,
from mem_at_top_sets.mpr ⟨n, assume m hnm, le_trans this (pow_le_pow (le_of_lt h) hnm)⟩
lemma tendsto_inverse_at_top_nhds_0 : tendsto (λr:ℝ, r⁻¹) at_top (nhds 0) :=
tendsto_orderable_unbounded (no_top 0) (no_bot 0) $ assume l u hl hu,
mem_at_top_sets.mpr ⟨u⁻¹ + 1, assume b hb,
have u⁻¹ < b, from lt_of_lt_of_le (lt_add_of_pos_right _ zero_lt_one) hb,
⟨lt_trans hl $ inv_pos $ lt_trans (inv_pos hu) this,
lt_of_one_div_lt_one_div hu $
begin
rw [inv_eq_one_div],
simp [-one_div_eq_inv, div_div_eq_mul_div, div_one],
simp [this]
end⟩⟩
lemma tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) :
tendsto (λn:ℕ, r^n) at_top (nhds 0) :=
by_cases
(assume : r = 0, (tendsto_add_at_top_iff_nat 1).mp $ by simp [pow_succ, this, tendsto_const_nhds])
(assume : r ≠ 0,
have tendsto (λn, (r⁻¹ ^ n)⁻¹) at_top (nhds 0),
from (tendsto_pow_at_top_at_top_of_gt_1 $ one_lt_inv (lt_of_le_of_ne h₁ this.symm) h₂).comp
tendsto_inverse_at_top_nhds_0,
tendsto.congr' (univ_mem_sets' $ by simp *) this)
lemma tendsto_pow_at_top_nhds_0_of_lt_1_normed_field {K : Type*} [normed_field K] {ξ : K}
(_ : ∥ξ∥ < 1) : tendsto (λ n : ℕ, ξ^n) at_top (nhds 0) :=
begin
rw[tendsto_iff_norm_tendsto_zero],
convert tendsto_pow_at_top_nhds_0_of_lt_1 (norm_nonneg ξ) ‹∥ξ∥ < 1›,
ext n,
simp
end
lemma tendsto_pow_at_top_at_top_of_gt_1_nat {k : ℕ} (h : 1 < k) :
tendsto (λn:ℕ, k ^ n) at_top at_top :=
tendsto_coe_nat_real_at_top_iff.1 $
have hr : 1 < (k : ℝ), by rw [← nat.cast_one, nat.cast_lt]; exact h,
by simpa using tendsto_pow_at_top_at_top_of_gt_1 hr
lemma tendsto_inverse_at_top_nhds_0_nat : tendsto (λ n : ℕ, (n : ℝ)⁻¹) at_top (nhds 0) :=
tendsto.comp (tendsto_coe_nat_real_at_top_iff.2 tendsto_id) tendsto_inverse_at_top_nhds_0
lemma tendsto_one_div_at_top_nhds_0_nat : tendsto (λ n : ℕ, 1/(n : ℝ)) at_top (nhds 0) :=
by simpa only [inv_eq_one_div] using tendsto_inverse_at_top_nhds_0_nat
lemma tendsto_one_div_add_at_top_nhds_0_nat :
tendsto (λ n : ℕ, 1 / ((n : ℝ) + 1)) at_top (nhds 0) :=
suffices tendsto (λ n : ℕ, 1 / (↑(n + 1) : ℝ)) at_top (nhds 0), by simpa,
(tendsto_add_at_top_iff_nat 1).2 tendsto_one_div_at_top_nhds_0_nat
lemma has_sum_geometric {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) :
has_sum (λn:ℕ, r ^ n) (1 / (1 - r)) :=
have r ≠ 1, from ne_of_lt h₂,
have r + -1 ≠ 0,
by rw [←sub_eq_add_neg, ne, sub_eq_iff_eq_add]; simp; assumption,
have tendsto (λn, (r ^ n - 1) * (r - 1)⁻¹) at_top (nhds ((0 - 1) * (r - 1)⁻¹)),
from tendsto_mul
(tendsto_sub (tendsto_pow_at_top_nhds_0_of_lt_1 h₁ h₂) tendsto_const_nhds) tendsto_const_nhds,
(has_sum_iff_tendsto_nat_of_nonneg (pow_nonneg h₁) _).mpr $
by simp [neg_inv, geom_sum, div_eq_mul_inv, *] at *
lemma has_sum_geometric_two (a : ℝ) : has_sum (λn:ℕ, (a / 2) / 2 ^ n) a :=
begin
convert has_sum_mul_left (a / 2) (has_sum_geometric
(le_of_lt one_half_pos) one_half_lt_one),
{ funext n, simp,
rw ← pow_inv; [refl, exact two_ne_zero] },
{ norm_num, rw div_mul_cancel _ two_ne_zero }
end
def pos_sum_of_encodable {ε : ℝ} (hε : 0 < ε)
(ι) [encodable ι] : {ε' : ι → ℝ // (∀ i, 0 < ε' i) ∧ ∃ c, has_sum ε' c ∧ c ≤ ε} :=
begin
let f := λ n, (ε / 2) / 2 ^ n,
have hf : has_sum f ε := has_sum_geometric_two _,
have f0 : ∀ n, 0 < f n := λ n, div_pos (half_pos hε) (pow_pos two_pos _),
refine ⟨f ∘ encodable.encode, λ i, f0 _, _⟩,
rcases summable_comp_of_summable_of_injective f (summable_spec hf) (@encodable.encode_injective ι _)
with ⟨c, hg⟩,
refine ⟨c, hg, has_sum_le_inj _ (@encodable.encode_injective ι _) _ _ hg hf⟩,
{ assume i _, exact le_of_lt (f0 _) },
{ assume n, exact le_refl _ }
end
lemma cauchy_seq_of_le_geometric [metric_space α] (r C : ℝ) (hr : r < 1) {f : ℕ → α}
(hu : ∀n, dist (f n) (f (n+1)) ≤ C * r^n) : cauchy_seq f :=
begin
refine cauchy_seq_of_summable_dist (summable_of_norm_bounded (λn, C * r^n) _ _),
{ by_cases h : C = 0,
{ simp [h, summable_zero] },
{ have Cpos : C > 0,
{ have := le_trans dist_nonneg (hu 0),
simp only [mul_one, pow_zero] at this,
exact lt_of_le_of_ne this (ne.symm h) },
have rnonneg: r ≥ 0,
{ have := le_trans dist_nonneg (hu 1),
simp only [pow_one] at this,
exact nonneg_of_mul_nonneg_left this Cpos },
refine summable_mul_left C _,
exact summable_spec (@has_sum_geometric r rnonneg hr) }},
show ∀n, abs (dist (f n) (f (n+1))) ≤ C * r^n,
{ assume n, rw abs_of_nonneg (dist_nonneg), exact hu n }
end
namespace nnreal
theorem exists_pos_sum_of_encodable {ε : nnreal} (hε : 0 < ε) (ι) [encodable ι] :
∃ ε' : ι → nnreal, (∀ i, 0 < ε' i) ∧ ∃c, has_sum ε' c ∧ c < ε :=
let ⟨a, a0, aε⟩ := dense hε in
let ⟨ε', hε', c, hc, hcε⟩ := pos_sum_of_encodable a0 ι in
⟨ λi, ⟨ε' i, le_of_lt $ hε' i⟩, assume i, nnreal.coe_lt.2 $ hε' i,
⟨c, has_sum_le (assume i, le_of_lt $ hε' i) has_sum_zero hc ⟩, nnreal.has_sum_coe.1 hc,
lt_of_le_of_lt (nnreal.coe_le.1 hcε) aε ⟩
end nnreal
namespace ennreal
theorem exists_pos_sum_of_encodable {ε : ennreal} (hε : 0 < ε) (ι) [encodable ι] :
∃ ε' : ι → nnreal, (∀ i, 0 < ε' i) ∧ (∑ i, (ε' i : ennreal)) < ε :=
begin
rcases dense hε with ⟨r, h0r, hrε⟩,
rcases lt_iff_exists_coe.1 hrε with ⟨x, rfl, hx⟩,
rcases nnreal.exists_pos_sum_of_encodable (coe_lt_coe.1 h0r) ι with ⟨ε', hp, c, hc, hcr⟩,
exact ⟨ε', hp, (ennreal.tsum_coe_eq hc).symm ▸ lt_trans (coe_lt_coe.2 hcr) hrε⟩
end
end ennreal
|
1d30f8db60655f06891b3069e9434c6a9dc8f02b | a45212b1526d532e6e83c44ddca6a05795113ddc | /src/ring_theory/principal_ideal_domain.lean | 4f98208caaf3489b83b41ff7e9948cebde2c5d94 | [
"Apache-2.0"
] | permissive | fpvandoorn/mathlib | b21ab4068db079cbb8590b58fda9cc4bc1f35df4 | b3433a51ea8bc07c4159c1073838fc0ee9b8f227 | refs/heads/master | 1,624,791,089,608 | 1,556,715,231,000 | 1,556,715,231,000 | 165,722,980 | 5 | 0 | Apache-2.0 | 1,552,657,455,000 | 1,547,494,646,000 | Lean | UTF-8 | Lean | false | false | 6,740 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Chris Hughes, Morenikeji Neri
-/
import algebra.euclidean_domain
import ring_theory.ideals ring_theory.noetherian ring_theory.unique_factorization_domain
variables {α : Type*}
open set function ideal
local attribute [instance] classical.prop_decidable
class ideal.is_principal [comm_ring α] (S : ideal α) : Prop :=
(principal : ∃ a, S = span {a})
class principal_ideal_domain (α : Type*) extends integral_domain α :=
(principal : ∀ (S : ideal α), S.is_principal)
attribute [instance] principal_ideal_domain.principal
namespace ideal.is_principal
variable [comm_ring α]
noncomputable def generator (S : ideal α) [S.is_principal] : α :=
classical.some (principal S)
lemma span_singleton_generator (S : ideal α) [S.is_principal] : span {generator S} = S :=
eq.symm (classical.some_spec (principal S))
@[simp] lemma generator_mem (S : ideal α) [S.is_principal] : generator S ∈ S :=
by conv {to_rhs, rw ← span_singleton_generator S}; exact subset_span (mem_singleton _)
lemma mem_iff_generator_dvd (S : ideal α) [S.is_principal] {x : α} : x ∈ S ↔ generator S ∣ x :=
by rw [← mem_span_singleton, span_singleton_generator]
lemma eq_bot_iff_generator_eq_zero (S : ideal α) [S.is_principal] :
S = ⊥ ↔ generator S = 0 :=
by rw [← span_singleton_eq_bot, span_singleton_generator]
end ideal.is_principal
namespace is_prime
open ideal.is_principal ideal
lemma to_maximal_ideal [principal_ideal_domain α] {S : ideal α}
[hpi : is_prime S] (hS : S ≠ ⊥) : is_maximal S :=
is_maximal_iff.2 ⟨(ne_top_iff_one S).1 hpi.1, begin
assume T x hST hxS hxT,
haveI := principal_ideal_domain.principal S,
haveI := principal_ideal_domain.principal T,
cases (mem_iff_generator_dvd _).1 (hST $ generator_mem S) with z hz,
cases hpi.2 (show generator T * z ∈ S, from hz ▸ generator_mem S),
{ have hTS : T ≤ S, rwa [← span_singleton_generator T, span_le, singleton_subset_iff],
exact (hxS $ hTS hxT).elim },
cases (mem_iff_generator_dvd _).1 h with y hy,
have : generator S ≠ 0 := mt (eq_bot_iff_generator_eq_zero _).2 hS,
rw [← mul_one (generator S), hy, mul_left_comm, domain.mul_left_inj this] at hz,
exact hz.symm ▸ ideal.mul_mem_right _ (generator_mem T)
end⟩
end is_prime
section
open euclidean_domain
variable [euclidean_domain α]
lemma mod_mem_iff {S : ideal α} {x y : α} (hy : y ∈ S) : x % y ∈ S ↔ x ∈ S :=
⟨λ hxy, div_add_mod x y ▸ ideal.add_mem S (mul_mem_right S hy) hxy,
λ hx, (mod_eq_sub_mul_div x y).symm ▸ ideal.sub_mem S hx (ideal.mul_mem_right S hy)⟩
instance euclidean_domain.to_principal_ideal_domain : principal_ideal_domain α :=
{ principal := λ S, by exactI
⟨if h : {x : α | x ∈ S ∧ x ≠ 0} = ∅
then ⟨0, submodule.ext $ λ a, by rw [← @submodule.bot_coe α α _ _ ring.to_module, span_eq, submodule.mem_bot]; exact
⟨λ haS, by_contradiction $ λ ha0, eq_empty_iff_forall_not_mem.1 h a ⟨haS, ha0⟩,
λ h₁, h₁.symm ▸ S.zero_mem⟩⟩
else
have wf : well_founded euclidean_domain.r := euclidean_domain.r_well_founded α,
have hmin : well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h ∈ S ∧
well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h ≠ 0,
from well_founded.min_mem wf {x : α | x ∈ S ∧ x ≠ 0} h,
⟨well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h,
submodule.ext $ λ x,
⟨λ hx, div_add_mod x (well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h) ▸
(mem_span_singleton.2 $ dvd_add (dvd_mul_right _ _) $
have (x % (well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h) ∉ {x : α | x ∈ S ∧ x ≠ 0}),
from λ h₁, well_founded.not_lt_min wf _ h h₁ (mod_lt x hmin.2),
have x % well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h = 0, by finish [(mod_mem_iff hmin.1).2 hx],
by simp *),
λ hx, let ⟨y, hy⟩ := mem_span_singleton.1 hx in hy.symm ▸ ideal.mul_mem_right _ hmin.1⟩⟩⟩ }
end
namespace principal_ideal_domain
variables [principal_ideal_domain α]
instance is_noetherian_ring : is_noetherian_ring α :=
⟨assume s : ideal α,
begin
cases (principal s).principal with a hs,
refine ⟨finset.singleton a, submodule.ext' _⟩, rw hs, refl
end⟩
section
local attribute [instance] classical.prop_decidable
open submodule
lemma factors_decreasing (b₁ b₂ : α) (h₁ : b₁ ≠ 0) (h₂ : ¬ is_unit b₂) :
submodule.span α ({b₁ * b₂} : set α) < submodule.span α {b₁} :=
lt_of_le_not_le (ideal.span_le.2 $ singleton_subset_iff.2 $
ideal.mem_span_singleton.2 ⟨b₂, rfl⟩) $ λ h,
h₂ $ is_unit_of_dvd_one _ $ (mul_dvd_mul_iff_left h₁).1 $
by rwa [mul_one, ← ideal.span_singleton_le_span_singleton]
end
lemma is_maximal_of_irreducible {p : α} (hp : irreducible p) :
is_maximal (span ({p} : set α)) :=
⟨mt span_singleton_eq_top.1 hp.1, λ I hI, begin
rcases principal I with ⟨a, rfl⟩,
rw span_singleton_eq_top,
unfreezeI,
rcases span_singleton_le_span_singleton.1 (le_of_lt hI) with ⟨b, rfl⟩,
refine (of_irreducible_mul hp).resolve_right (mt (λ hb, _) (not_le_of_lt hI)),
rw [span_singleton_le_span_singleton, mul_dvd_of_is_unit_right hb]
end⟩
lemma irreducible_iff_prime {p : α} : irreducible p ↔ prime p :=
⟨λ hp, (span_singleton_prime $ nonzero_of_irreducible hp).1 $
(is_maximal_of_irreducible hp).is_prime,
irreducible_of_prime⟩
lemma associates_irreducible_iff_prime : ∀{p : associates α}, irreducible p ↔ p.prime :=
associates.forall_associated.2 $ assume a,
by rw [associates.irreducible_mk_iff, associates.prime_mk, irreducible_iff_prime]
section
local attribute [instance] classical.prop_decidable
noncomputable def factors (a : α) : multiset α :=
if h : a = 0 then ∅ else classical.some
(is_noetherian_ring.exists_factors a h)
lemma factors_spec (a : α) (h : a ≠ 0) :
(∀b∈factors a, irreducible b) ∧ associated a (factors a).prod :=
begin
unfold factors, rw [dif_neg h],
exact classical.some_spec
(is_noetherian_ring.exists_factors a h)
end
/-- The unique factorization domain structure given by the principal ideal domain.
This is not added as type class instance, since the `factors` might be computed in a different way.
E.g. factors could return normalized values.
-/
noncomputable def to_unique_factorization_domain : unique_factorization_domain α :=
{ factors := factors,
factors_prod := assume a ha, associated.symm (factors_spec a ha).2,
prime_factors := assume a ha, by simpa [irreducible_iff_prime] using (factors_spec a ha).1 }
end
end principal_ideal_domain
|
a64b70a7d1ab1a399f832300badea8926df89805 | 65b579fba1b0b66add04cccd4529add645eff597 | /lambda/pure/src/unicode.lean | 66c093e877b8b2184ba5bc1d5c7347a926e5a1af | [] | no_license | teodorov/sf_lean | ba637ca8ecc538aece4d02c8442d03ef713485db | cd4832d6bee9c606014c977951f6aebc4c8d611b | refs/heads/master | 1,632,890,232,054 | 1,543,005,745,000 | 1,543,005,745,000 | 108,566,115 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,650 | lean | import data.buffer system.io
import data.bitvec
namespace unicode
private def utf_8_convert_bitvec : list (bitvec 8) → option (list char)
-- 0xxxxxxx -- symbols from 0 to 7Fh
| (⟨ff :: tl₁, _⟩ :: chars) :=
list.cons (char.of_nat (bitvec.bits_to_nat tl₁)) <$> utf_8_convert_bitvec chars
-- 110xxxxx 10xxxxxx -- symbols from 80h to 7FFh
| (⟨tt :: tt :: ff :: tl₁, _⟩ :: ⟨tt :: ff :: tl₂, _⟩ :: chars) :=
list.cons (char.of_nat (bitvec.bits_to_nat $ tl₁ ++ tl₂)) <$>
utf_8_convert_bitvec chars
-- 1110xxxx 10xxxxxx 10xxxxxx -- symbols from 800h to FFFFh
| (⟨tt :: tt :: tt :: ff :: tl₁, _⟩ :: ⟨tt :: ff :: tl₂, _⟩ ::
⟨tt :: ff :: tl₃, _⟩ :: chars) :=
list.cons (char.of_nat (bitvec.bits_to_nat $ tl₁ ++ tl₂ ++ tl₃)) <$>
utf_8_convert_bitvec chars
-- 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx -- symbols from 10000h to 10FFFFh
| (⟨tt :: tt :: tt :: tt :: ff :: tl₁, _⟩ :: ⟨tt :: ff :: tl₂, _⟩ ::
⟨tt :: ff :: tl₃, _⟩ :: ⟨tt :: ff :: tl₄, _⟩ :: chars) :=
list.cons (char.of_nat (bitvec.bits_to_nat $ tl₁ ++ tl₂ ++ tl₃ ++ tl₄)) <$>
utf_8_convert_bitvec chars
| [] := some []
| _ := none
-- Default Lean’s convert from char_buffer to string is incorrect
-- (when we get a string using get_line, for example),
-- because it does not work correctly with Unicode and UTF-8, in particular.
-- This is simple realisation of correct convert from char_buffer to string
-- for UTF-8 encoding.
def utf8_to_string (buff : char_buffer) : option string :=
list.as_string <$> utf_8_convert_bitvec (list.map (bitvec.of_nat 8 ∘ char.to_nat) buff.to_list)
end unicode |
f3b540689d93383fa66f6a5d9fa8fbad4b741c6c | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/topology/sheaves/local_predicate.lean | 4acca6972ccd2412e72b62cf6ae38ef45897052d | [
"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 | 11,697 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Scott Morrison, Adam Topaz
-/
import topology.sheaves.sheaf_of_functions
import topology.sheaves.stalks
import topology.local_homeomorph
import topology.sheaves.sheaf_condition.unique_gluing
/-!
# Functions satisfying a local predicate form a sheaf.
At this stage, in `topology/sheaves/sheaf_of_functions.lean`
we've proved that not-necessarily-continuous functions from a topological space
into some type (or type family) form a sheaf.
Why do the continuous functions form a sheaf?
The point is just that continuity is a local condition,
so one can use the lifting condition for functions to provide a candidate lift,
then verify that the lift is actually continuous by using the factorisation condition for the lift
(which guarantees that on each open set it agrees with the functions being lifted,
which were assumed to be continuous).
This file abstracts this argument to work for
any collection of dependent functions on a topological space
satisfying a "local predicate".
As an application, we check that continuity is a local predicate in this sense, and provide
* `Top.sheaf_to_Top`: continuous functions into a topological space form a sheaf
A sheaf constructed in this way has a natural map `stalk_to_fiber` from the stalks
to the types in the ambient type family.
We give conditions sufficient to show that this map is injective and/or surjective.
-/
universe v
noncomputable theory
variables {X : Top.{v}}
variables (T : X → Type v)
open topological_space
open opposite
open category_theory
open category_theory.limits
open category_theory.limits.types
namespace Top
/--
Given a topological space `X : Top` and a type family `T : X → Type`,
a `P : prelocal_predicate T` consists of:
* a family of predicates `P.pred`, one for each `U : opens X`, of the form `(Π x : U, T x) → Prop`
* a proof that if `f : Π x : V, T x` satisfies the predicate on `V : opens X`, then
the restriction of `f` to any open subset `U` also satisfies the predicate.
-/
structure prelocal_predicate :=
(pred : Π {U : opens X}, (Π x : U, T x) → Prop)
(res : ∀ {U V : opens X} (i : U ⟶ V) (f : Π x : V, T x) (h : pred f), pred (λ x : U, f (i x)))
variables (X)
/--
Continuity is a "prelocal" predicate on functions to a fixed topological space `T`.
-/
@[simps]
def continuous_prelocal (T : Top.{v}) : prelocal_predicate (λ x : X, T) :=
{ pred := λ U f, continuous f,
res := λ U V i f h, continuous.comp h (opens.open_embedding_of_le i.le).continuous, }
/-- Satisfying the inhabited linter. -/
instance inhabited_prelocal_predicate (T : Top.{v}) : inhabited (prelocal_predicate (λ x : X, T)) :=
⟨continuous_prelocal X T⟩
variables {X}
/--
Given a topological space `X : Top` and a type family `T : X → Type`,
a `P : local_predicate T` consists of:
* a family of predicates `P.pred`, one for each `U : opens X`, of the form `(Π x : U, T x) → Prop`
* a proof that if `f : Π x : V, T x` satisfies the predicate on `V : opens X`, then
the restriction of `f` to any open subset `U` also satisfies the predicate, and
* a proof that given some `f : Π x : U, T x`,
if for every `x : U` we can find an open set `x ∈ V ≤ U`
so that the restriction of `f` to `V` satisfies the predicate,
then `f` itself satisfies the predicate.
-/
structure local_predicate extends prelocal_predicate T :=
(locality : ∀ {U : opens X} (f : Π x : U, T x)
(w : ∀ x : U, ∃ (V : opens X) (m : x.1 ∈ V) (i : V ⟶ U), pred (λ x : V, f (i x : U))), pred f)
variables (X)
/--
Continuity is a "local" predicate on functions to a fixed topological space `T`.
-/
def continuous_local (T : Top.{v}) : local_predicate (λ x : X, T) :=
{ locality := λ U f w,
begin
apply continuous_iff_continuous_at.2,
intro x,
specialize w x,
rcases w with ⟨V, m, i, w⟩,
dsimp at w,
rw continuous_iff_continuous_at at w,
specialize w ⟨x, m⟩,
simpa using (opens.open_embedding_of_le i.le).continuous_at_iff.1 w,
end,
..continuous_prelocal X T }
/-- Satisfying the inhabited linter. -/
instance inhabited_local_predicate (T : Top.{v}) : inhabited (local_predicate _) :=
⟨continuous_local X T⟩
variables {X T}
/--
Given a `P : prelocal_predicate`, we can always construct a `local_predicate`
by asking that the condition from `P` holds locally near every point.
-/
def prelocal_predicate.sheafify {T : X → Type v} (P : prelocal_predicate T) : local_predicate T :=
{ pred := λ U f, ∀ x : U, ∃ (V : opens X) (m : x.1 ∈ V) (i : V ⟶ U), P.pred (λ x : V, f (i x : U)),
res := λ V U i f w x,
begin
specialize w (i x),
rcases w with ⟨V', m', i', p⟩,
refine ⟨V ⊓ V', ⟨x.2,m'⟩, opens.inf_le_left _ _, _⟩,
convert P.res (opens.inf_le_right V V') _ p,
end,
locality := λ U f w x,
begin
specialize w x,
rcases w with ⟨V, m, i, p⟩,
specialize p ⟨x.1, m⟩,
rcases p with ⟨V', m', i', p'⟩,
exact ⟨V', m', i' ≫ i, p'⟩,
end }
lemma prelocal_predicate.sheafify_of {T : X → Type v} {P : prelocal_predicate T}
{U : opens X} {f : Π x : U, T x} (h : P.pred f) :
P.sheafify.pred f :=
λ x, ⟨U, x.2, 𝟙 _, by { convert h, ext ⟨y, w⟩, refl, }⟩
/--
The subpresheaf of dependent functions on `X` satisfying the "pre-local" predicate `P`.
-/
@[simps]
def subpresheaf_to_Types (P : prelocal_predicate T) : presheaf (Type v) X :=
{ obj := λ U, { f : Π x : unop U, T x // P.pred f },
map := λ U V i f, ⟨λ x, f.1 (i.unop x), P.res i.unop f.1 f.2⟩ }.
namespace subpresheaf_to_Types
variables (P : prelocal_predicate T)
/--
The natural transformation including the subpresheaf of functions satisfying a local predicate
into the presheaf of all functions.
-/
def subtype : subpresheaf_to_Types P ⟶ presheaf_to_Types X T :=
{ app := λ U f, f.1 }
open Top.presheaf
/--
The functions satisfying a local predicate satisfy the sheaf condition.
-/
lemma is_sheaf (P : local_predicate T) :
(subpresheaf_to_Types P.to_prelocal_predicate).is_sheaf :=
presheaf.is_sheaf_of_is_sheaf_unique_gluing_types _ $ λ ι U sf sf_comp, begin
-- We show the sheaf condition in terms of unique gluing.
-- First we obtain a family of sections for the underlying sheaf of functions,
-- by forgetting that the prediacte holds
let sf' : Π i : ι, (presheaf_to_Types X T).obj (op (U i)) := λ i, (sf i).val,
-- Since our original family is compatible, this one is as well
have sf'_comp : (presheaf_to_Types X T).is_compatible U sf' := λ i j,
congr_arg subtype.val (sf_comp i j),
-- So, we can obtain a unique gluing
obtain ⟨gl,gl_spec,gl_uniq⟩ := (sheaf_to_Types X T).exists_unique_gluing U sf' sf'_comp,
refine ⟨⟨gl,_⟩,_,_⟩,
{ -- Our first goal is to show that this chosen gluing satisfies the
-- predicate. Of course, we use locality of the predicate.
apply P.locality,
rintros ⟨x, mem⟩,
-- Once we're at a particular point `x`, we can select some open set `x ∈ U i`.
choose i hi using opens.mem_supr.mp mem,
-- We claim that the predicate holds in `U i`
use [U i, hi, opens.le_supr U i],
-- This follows, since our original family `sf` satisfies the predicate
convert (sf i).property,
exact gl_spec i },
-- It remains to show that the chosen lift is really a gluing for the subsheaf and
-- that it is unique. Both of which follow immediately from the corresponding facts
-- in the sheaf of functions without the local predicate.
{ intro i,
ext1,
exact (gl_spec i) },
{ intros gl' hgl',
ext1,
exact gl_uniq gl'.1 (λ i, congr_arg subtype.val (hgl' i)) },
end
end subpresheaf_to_Types
/--
The subsheaf of the sheaf of all dependently typed functions satisfying the local predicate `P`.
-/
@[simps]
def subsheaf_to_Types (P : local_predicate T) : sheaf (Type v) X :=
⟨subpresheaf_to_Types P.to_prelocal_predicate, subpresheaf_to_Types.is_sheaf P⟩
/--
There is a canonical map from the stalk to the original fiber, given by evaluating sections.
-/
def stalk_to_fiber (P : local_predicate T) (x : X) :
(subsheaf_to_Types P).presheaf.stalk x ⟶ T x :=
begin
refine colimit.desc _
{ X := T x, ι := { app := λ U f, _, naturality' := _ } },
{ exact f.1 ⟨x, (unop U).2⟩, },
{ tidy, }
end
@[simp] lemma stalk_to_fiber_germ (P : local_predicate T) (U : opens X) (x : U) (f) :
stalk_to_fiber P x ((subsheaf_to_Types P).presheaf.germ x f) = f.1 x :=
begin
dsimp [presheaf.germ, stalk_to_fiber],
cases x,
simp,
refl,
end
/--
The `stalk_to_fiber` map is surjective at `x` if
every point in the fiber `T x` has an allowed section passing through it.
-/
lemma stalk_to_fiber_surjective (P : local_predicate T) (x : X)
(w : ∀ (t : T x), ∃ (U : open_nhds x) (f : Π y : U.1, T y) (h : P.pred f), f ⟨x, U.2⟩ = t) :
function.surjective (stalk_to_fiber P x) :=
λ t,
begin
rcases w t with ⟨U, f, h, rfl⟩,
fsplit,
{ exact (subsheaf_to_Types P).presheaf.germ ⟨x, U.2⟩ ⟨f, h⟩, },
{ exact stalk_to_fiber_germ _ U.1 ⟨x, U.2⟩ ⟨f, h⟩, }
end
/--
The `stalk_to_fiber` map is injective at `x` if any two allowed sections which agree at `x`
agree on some neighborhood of `x`.
-/
lemma stalk_to_fiber_injective (P : local_predicate T) (x : X)
(w : ∀ (U V : open_nhds x) (fU : Π y : U.1, T y) (hU : P.pred fU)
(fV : Π y : V.1, T y) (hV : P.pred fV) (e : fU ⟨x, U.2⟩ = fV ⟨x, V.2⟩),
∃ (W : open_nhds x) (iU : W ⟶ U) (iV : W ⟶ V), ∀ (w : W.1), fU (iU w : U.1) = fV (iV w : V.1)) :
function.injective (stalk_to_fiber P x) :=
λ tU tV h,
begin
-- We promise to provide all the ingredients of the proof later:
let Q :
∃ (W : (open_nhds x)ᵒᵖ) (s : Π w : (unop W).1, T w) (hW : P.pred s),
tU = (subsheaf_to_Types P).presheaf.germ ⟨x, (unop W).2⟩ ⟨s, hW⟩ ∧
tV = (subsheaf_to_Types P).presheaf.germ ⟨x, (unop W).2⟩ ⟨s, hW⟩ := _,
{ choose W s hW e using Q,
exact e.1.trans e.2.symm, },
-- Then use induction to pick particular representatives of `tU tV : stalk x`
obtain ⟨U, ⟨fU, hU⟩, rfl⟩ := jointly_surjective'.{v v} tU,
obtain ⟨V, ⟨fV, hV⟩, rfl⟩ := jointly_surjective'.{v v} tV,
{ -- Decompose everything into its constituent parts:
dsimp,
simp only [stalk_to_fiber, types.colimit.ι_desc_apply'] at h,
specialize w (unop U) (unop V) fU hU fV hV h,
rcases w with ⟨W, iU, iV, w⟩,
-- and put it back together again in the correct order.
refine ⟨(op W), (λ w, fU (iU w : (unop U).1)), P.res _ _ hU, _⟩,
rcases W with ⟨W, m⟩,
exact ⟨colimit_sound iU.op (subtype.eq rfl),
colimit_sound iV.op (subtype.eq (funext w).symm)⟩, },
end
/--
Some repackaging:
the presheaf of functions satisfying `continuous_prelocal` is just the same thing as
the presheaf of continuous functions.
-/
def subpresheaf_continuous_prelocal_iso_presheaf_to_Top (T : Top.{v}) :
subpresheaf_to_Types (continuous_prelocal X T) ≅ presheaf_to_Top X T :=
nat_iso.of_components
(λ X,
{ hom := by { rintro ⟨f, c⟩, exact ⟨f, c⟩, },
inv := by { rintro ⟨f, c⟩, exact ⟨f, c⟩, },
hom_inv_id' := by { ext ⟨f, p⟩ x, refl, },
inv_hom_id' := by { ext ⟨f, p⟩ x, refl, }, })
(by tidy)
/--
The sheaf of continuous functions on `X` with values in a space `T`.
-/
def sheaf_to_Top (T : Top.{v}) : sheaf (Type v) X :=
⟨presheaf_to_Top X T,
presheaf.is_sheaf_of_iso (subpresheaf_continuous_prelocal_iso_presheaf_to_Top T)
(subpresheaf_to_Types.is_sheaf (continuous_local X T))⟩
end Top
|
ec5cc144dfc47a271ce36eb1e9d19d24b1301ee7 | 3dc4623269159d02a444fe898d33e8c7e7e9461b | /.github/workflows/project_1_a_decrire/lean-scheme-submission/src/spectrum_of_a_ring/structure_sheaf.lean | 173368f11f1a97adac7cba15279572d524b71e98 | [] | no_license | Or7ando/lean | cc003e6c41048eae7c34aa6bada51c9e9add9e66 | d41169cf4e416a0d42092fb6bdc14131cee9dd15 | refs/heads/master | 1,650,600,589,722 | 1,587,262,906,000 | 1,587,262,906,000 | 255,387,160 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,125 | lean | /-
The structure presheaf is a sheaf of rings.
https://stacks.math.columbia.edu/tag/01HR
-/
import topology.basic
import to_mathlib.localization.localization_alt
import sheaves.sheaf_on_standard_basis
import spectrum_of_a_ring.induced_homeomorphism
import spectrum_of_a_ring.quasi_compact
import spectrum_of_a_ring.structure_presheaf
import spectrum_of_a_ring.structure_sheaf_condition
import spectrum_of_a_ring.structure_sheaf_locality
import spectrum_of_a_ring.structure_sheaf_gluing
universes u v
local attribute [instance] classical.prop_decidable
variables {R : Type u} [comm_ring R]
open topological_space
open localization_alt
open localization
open classical
section structure_sheaf
theorem structure_presheaf_on_basis_is_sheaf_on_standard_basis_cofinal_system
: sheaf_on_standard_basis.is_sheaf_on_standard_basis_cofinal_system
(D_fs_standard_basis R)
(structure_presheaf_on_basis R).to_presheaf_on_basis :=
begin
intros U BU OC Hγ,
let f := some BU,
let Rf := localization R (S U),
have HRf : is_localization_data (powers f) (localization.of : R → Rf)
:= structure_presheaf.localization BU,
let fi : OC.γ → R := λ i, classical.some (OC.BUis i),
let Hfi : ∀ i, OC.Uis i = Spec.DO R (fi i) := λ i, classical.some_spec (OC.BUis i),
let fi' : OC.γ → Rf := λ i, localization.of (fi i),
let F : set Rf := set.range fi',
-- Lemma: ⋃ D(gᵢ') = Spec Rf.
have Hcov : ⋃₀ (Spec.D' '' F) = set.univ,
{ let φ : Spec Rf → Spec R := Zariski.induced localization.of,
dsimp [F],
apply set.eq_univ_of_univ_subset,
rintros P HP,
have H : φ P ∈ U,
suffices : φ P ∈ Spec.DO R (f),
rw some_spec BU,
exact this,
show φ P ∈ Spec.D'(f),
rw ←phi_image_Df HRf,
use P,
split,
{ trivial, },
{ refl, },
rw ←OC.Hcov at H,
rcases H with ⟨UiS, ⟨⟨UiO, ⟨⟨i, Hi⟩, HUiO⟩⟩, HPUiS⟩⟩,
use [φ ⁻¹' UiS],
have Hin : φ ⁻¹' UiS ∈ Spec.D' '' set.range fi',
rw [←HUiO, ←Hi, Hfi],
dsimp only [Spec.DO],
use [fi' i],
split,
{ use [i], },
{ rw [←Zariski.induced.preimage_D localization.of _], },
use [Hin],
exact HPUiS, },
-- We deduce: 1 ∈ <fi>
have Hone : (1 : Rf) ∈ ideal.span F,
erw [←ideal.eq_top_iff_one, ←Spec.V.empty_iff_ideal_top, ←Spec.V.set_eq_span],
rw [Spec.D'.union F, ←set.compl_compl set.univ, set.compl_univ] at Hcov,
apply set.ext,
intros x,
rw set.ext_iff at Hcov,
replace Hcov := Hcov x,
iterate 2 { rw set.mem_compl_iff at Hcov, },
rw not_iff_not at Hcov,
exact Hcov,
-- α is injective.
let αi := λ i, structure_presheaf_on_basis.res BU (OC.BUis i) (subset_covering i),
let Rfi := λ i, localization R (S (OC.Uis i)),
let α' := @α Rf _ _ Hγ Rfi _ αi _,
have Hlocα : Π i, is_localization_data (powers (fi' i)) (αi i)
:= λ i, structure_presheaf.res.localization BU (OC.BUis i) (subset_covering i),
have Hsc₁ := @standard_covering₁ Rf _ _ Hγ fi' Rfi _ αi _ Hlocα Hone,
-- ker β = im α.
let Rfij := λ i j, localization R (S ((OC.Uis i) ∩ (OC.Uis j))),
let βij
:= λ i j, structure_presheaf_on_basis.res_to_inter BU (OC.BUis i) (OC.BUis j) (subset_covering i),
have Hlocβ
:= λ i j, structure_presheaf.res_to_inter.localization BU (OC.BUis i) (OC.BUis j) (subset_covering i),
let β' := @β Rf _ _ Hγ fi' Rfi _ αi _ Hlocα Rfij _ βij _ Hlocβ,
have Hsc₂ := @standard_covering₂ Rf _ _ Hγ fi' Rfi _ αi _ Hlocα Rfij _ βij _ Hlocβ Hone,
constructor,
{ -- Locality.
intros s t Hst,
dsimp [structure_presheaf_on_basis, coe_fn, has_coe_to_fun.coe] at s,
dsimp [structure_presheaf_on_basis, coe_fn, has_coe_to_fun.coe] at t,
apply Hsc₁,
dsimp [α, αi],
apply funext,
intros i,
rw ←structure_presheaf_on_basis.res_eq,
exact (Hst i), },
{ -- Gluing.
intros s Hs,
have Hβ : β' s = 0,
simp [β', β, -sub_eq_add_neg, sub_eq_zero, β1, β2],
apply funext, intro j,
apply funext, intro k,
have Hsjk := Hs j k,
dsimp only [sheaf_on_standard_basis.res_to_inter_left] at Hsjk,
dsimp only [sheaf_on_standard_basis.res_to_inter_right] at Hsjk,
rw structure_presheaf_on_basis.res_eq at Hsjk,
-- The restriction to the left is the unique localization map from R[1/fj] to R[1/fjfk].
let β1 := structure_presheaf_on_basis.res_to_inter_right (OC.BUis j) (OC.BUis k),
let Hβ1 := @inverts_powers1 Rf _ _ Hγ fi' Rfij _ βij _ Hlocβ j k,
have Hcompβ1 : βij j k = β1 ∘ (αi k),
dsimp only [βij, β1, αi],
dsimp only [structure_presheaf_on_basis.res_to_inter_right],
dsimp only [structure_presheaf_on_basis.res_to_inter],
erw ←structure_presheaf_on_basis.res_comp,
refl,
have Huniqueβ1
:= is_localization_unique.of_eq (powers (fi' k)) (αi k) (Hlocα k) β1 (s k) (βij j k) Hβ1 Hcompβ1,
rw Huniqueβ1,
-- The restriction to the right is the unique localization map from R[1/fk] to R[1/fjfk].
let β2 := structure_presheaf_on_basis.res_to_inter_left (OC.BUis j) (OC.BUis k),
let Hβ2 := @inverts_powers2 Rf _ _ Hγ fi' Rfij _ βij _ Hlocβ j k,
have Hcompβ2 : βij j k = β2 ∘ (αi j),
dsimp only [βij, β2, αi],
dsimp only [structure_presheaf_on_basis.res_to_inter_left],
dsimp only [structure_presheaf_on_basis.res_to_inter],
erw ←structure_presheaf_on_basis.res_comp,
refl,
have Huniqueβ2
:= is_localization_unique.of_eq (powers (fi' j)) (αi j) (Hlocα j) β2 (s j) (βij j k) Hβ2 Hcompβ2,
rw Huniqueβ2,
-- Now we have it in the desired form.
exact Hsjk.symm,
-- Use global section found.
rcases ((Hsc₂ s).1 Hβ) with ⟨S, HS⟩,
use S,
intros i,
replace HS := (congr_fun HS) i,
dsimp [α, αi] at HS,
rw structure_presheaf_on_basis.res_eq,
exact HS, }
end
theorem structure_presheaf_on_basis_is_sheaf_on_basis
: sheaf_on_standard_basis.is_sheaf_on_standard_basis
(D_fs_standard_basis R)
(structure_presheaf_on_basis R).to_presheaf_on_basis :=
begin
apply sheaf_on_standard_basis.cofinal_systems_coverings_standard_case,
{ apply structure_presheaf_on_basis_is_compact, },
{ apply structure_presheaf_on_basis_is_sheaf_on_standard_basis_cofinal_system, },
end
-- Structure sheaf.
def structure_sheaf.presheaf (R : Type u) [comm_ring R] :=
presheaf_of_rings_extension
(D_fs_standard_basis R)
(structure_presheaf_on_basis R)
theorem strucutre_presheaf_is_sheaf_of_rings (R : Type u) [comm_ring R]
: is_sheaf_of_rings (structure_sheaf.presheaf R) :=
begin
apply extension_is_sheaf_of_rings,
intros U BU OC,
exact structure_presheaf_on_basis_is_sheaf_on_basis BU OC,
end
def structure_sheaf (R : Type u) [comm_ring R] : sheaf_of_rings (Spec R) :=
{ F := structure_sheaf.presheaf R,
locality := (strucutre_presheaf_is_sheaf_of_rings R).1,
gluing := (strucutre_presheaf_is_sheaf_of_rings R).2, }
end structure_sheaf
|
a7a624744791ac5ac69fe75194e5d3204bdbc49b | 969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb | /src/category_theory/sites/pretopology.lean | 2dc74b4d5ac36469a68fbd5b34fb8706fd034265 | [
"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 | 8,512 | 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.sites.grothendieck
/-!
# Grothendieck pretopologies
Definition and lemmas about Grothendieck pretopologies.
A Grothendieck pretopology for a category `C` is a set of families of morphisms with fixed codomain,
satisfying certain closure conditions.
We show that a pretopology generates a genuine Grothendieck topology, and every topology has
a maximal pretopology which generates it.
The pretopology associated to a topological space is defined in `spaces.lean`.
## Tags
coverage, pretopology, site
## References
* [https://ncatlab.org/nlab/show/Grothendieck+pretopology][nlab]
* [S. MacLane, I. Moerdijk, *Sheaves in Geometry and Logic*][MM92]
* [https://stacks.math.columbia.edu/tag/00VG][Stacks]
-/
universes v u
noncomputable theory
namespace category_theory
open category_theory category limits presieve
variables {C : Type u} [category.{v} C] [has_pullbacks C]
/--
Pullback a set of arrows with given codomain along a fixed map, by taking the pullback in the
category.
This is not the same as the arrow set of `sieve.pullback`, but there is a relation between them
in `pullback_arrows_comm`.
-/
inductive pullback_arrows {X Y : C} (f : Y ⟶ X) (S : presieve X) :
presieve Y
| mk (Z : C) (h : Z ⟶ X) : S h → pullback_arrows (pullback.snd : pullback h f ⟶ Y)
lemma pullback_arrows_comm {X Y : C} (f : Y ⟶ X)
(R : presieve X) :
sieve.generate (pullback_arrows f R) = (sieve.generate R).pullback f :=
begin
ext Z g,
split,
{ rintro ⟨_, h, k, hk, rfl⟩,
cases hk with W g hg,
change (sieve.generate R).pullback f (h ≫ pullback.snd),
rw [sieve.pullback_apply, assoc, ← pullback.condition, ← assoc],
exact sieve.downward_closed _ (sieve.le_generate R W hg) (h ≫ pullback.fst)},
{ rintro ⟨W, h, k, hk, comm⟩,
exact ⟨_, _, _, pullback_arrows.mk _ _ hk, pullback.lift_snd _ _ comm⟩ },
end
lemma pullback_singleton {X Y Z : C} (f : Y ⟶ X) (g : Z ⟶ X) :
pullback_arrows f (singleton g) = singleton (pullback.snd : pullback g f ⟶ _) :=
begin
ext W h,
split,
{ rintro ⟨W, _, _, _⟩,
exact singleton.mk },
{ rintro ⟨_⟩,
exact pullback_arrows.mk Z g singleton.mk }
end
variables (C)
/--
A (Grothendieck) pretopology on `C` consists of a collection of families of morphisms with a fixed
target `X` for every object `X` in `C`, called "coverings" of `X`, which satisfies the following
three axioms:
1. Every family consisting of a single isomorphism is a covering family.
2. The collection of covering families is stable under pullback.
3. Given a covering family, and a covering family on each domain of the former, the composition
is a covering family.
In some sense, a pretopology can be seen as Grothendieck topology with weaker saturation conditions,
in that each covering is not necessarily downward closed.
See: https://ncatlab.org/nlab/show/Grothendieck+pretopology, or
https://stacks.math.columbia.edu/tag/00VH, or [MM92] Chapter III, Section 2, Definition 2.
Note that Stacks calls a category together with a pretopology a site, and [MM92] calls this
a basis for a topology.
-/
@[ext]
structure pretopology :=
(coverings : Π (X : C), set (presieve X))
(has_isos : ∀ ⦃X Y⦄ (f : Y ⟶ X) [is_iso f], presieve.singleton f ∈ coverings X)
(pullbacks : ∀ ⦃X Y⦄ (f : Y ⟶ X) S, S ∈ coverings X → pullback_arrows f S ∈ coverings Y)
(transitive : ∀ ⦃X : C⦄ (S : presieve X)
(Ti : Π ⦃Y⦄ (f : Y ⟶ X), S f → presieve Y), S ∈ coverings X →
(∀ ⦃Y⦄ f (H : S f), Ti f H ∈ coverings Y) → S.bind Ti ∈ coverings X)
namespace pretopology
instance : has_coe_to_fun (pretopology C) :=
⟨_, λ J, J.coverings⟩
instance : partial_order (pretopology C) :=
{ le := λ K₁ K₂, (K₁ : Π (X : C), set _) ≤ K₂,
le_refl := λ K, le_refl _,
le_trans := λ K₁ K₂ K₃ h₁₂ h₂₃, le_trans h₁₂ h₂₃,
le_antisymm := λ K₁ K₂ h₁₂ h₂₁, pretopology.ext _ _ (le_antisymm h₁₂ h₂₁) }
instance : order_top (pretopology C) :=
{ top :=
{ coverings := λ _, set.univ,
has_isos := λ _ _ _ _, set.mem_univ _,
pullbacks := λ _ _ _ _ _, set.mem_univ _,
transitive := λ _ _ _ _ _, set.mem_univ _ },
le_top := λ K X S hS, set.mem_univ _,
..pretopology.partial_order C }
instance : inhabited (pretopology C) := ⟨⊤⟩
/--
A pretopology `K` can be completed to a Grothendieck topology `J` by declaring a sieve to be
`J`-covering if it contains a family in `K`.
See https://stacks.math.columbia.edu/tag/00ZC, or [MM92] Chapter III, Section 2, Equation (2).
-/
def to_grothendieck (K : pretopology C) : grothendieck_topology C :=
{ sieves := λ X S, ∃ R ∈ K X, R ≤ (S : presieve _),
top_mem' := λ X, ⟨presieve.singleton (𝟙 _), K.has_isos _, λ _ _ _, ⟨⟩⟩,
pullback_stable' := λ X Y S g,
begin
rintro ⟨R, hR, RS⟩,
refine ⟨_, K.pullbacks g _ hR, _⟩,
rw [← sieve.sets_iff_generate, pullback_arrows_comm],
apply sieve.pullback_monotone,
rwa sieve.gi_generate.gc,
end,
transitive' :=
begin
rintro X S ⟨R', hR', RS⟩ R t,
choose t₁ t₂ t₃ using t,
refine ⟨_, K.transitive _ _ hR' (λ _ f hf, t₂ (RS _ hf)), _⟩,
rintro Y _ ⟨Z, g, f, hg, hf, rfl⟩,
apply t₃ (RS _ hg) _ hf,
end }
lemma mem_to_grothendieck (K : pretopology C) (X S) :
S ∈ to_grothendieck C K X ↔ ∃ R ∈ K X, R ≤ (S : presieve X) :=
iff.rfl
/--
The largest pretopology generating the given Grothendieck topology.
See [MM92] Chapter III, Section 2, Equations (3,4).
-/
def of_grothendieck (J : grothendieck_topology C) : pretopology C :=
{ coverings := λ X R, sieve.generate R ∈ J X,
has_isos := λ X Y f i, by exactI J.covering_of_eq_top (by simp),
pullbacks := λ X Y f R hR,
begin
rw [set.mem_def, pullback_arrows_comm],
apply J.pullback_stable f hR,
end,
transitive := λ X S Ti hS hTi,
begin
apply J.transitive hS,
intros Y f,
rintros ⟨Z, g, f, hf, rfl⟩,
rw sieve.pullback_comp,
apply J.pullback_stable g,
apply J.superset_covering _ (hTi _ hf),
rintro Y g ⟨W, h, g, hg, rfl⟩,
exact ⟨_, h, _, ⟨_, _, _, hf, hg, rfl⟩, by simp⟩,
end }
/-- We have a galois insertion from pretopologies to Grothendieck topologies. -/
def gi : galois_insertion (to_grothendieck C) (of_grothendieck C) :=
{ gc :=
λ K J,
begin
split,
{ intros h X R hR,
exact h _ ⟨_, hR, sieve.le_generate R⟩ },
{ rintro h X S ⟨R, hR, RS⟩,
apply J.superset_covering _ (h _ hR),
rwa sieve.gi_generate.gc }
end,
le_l_u := λ J X S hS, ⟨S, J.superset_covering S.le_generate hS, le_refl _⟩,
choice := λ x hx, to_grothendieck C x,
choice_eq := λ _ _, rfl }
/--
The trivial pretopology, in which the coverings are exactly singleton isomorphisms. This topology is
also known as the indiscrete, coarse, or chaotic topology.
See https://stacks.math.columbia.edu/tag/07GE
-/
def trivial : pretopology C :=
{ coverings := λ X S, ∃ Y (f : Y ⟶ X) (h : is_iso f), S = presieve.singleton f,
has_isos := λ X Y f i, ⟨_, _, i, rfl⟩,
pullbacks := λ X Y f S,
begin
rintro ⟨Z, g, i, rfl⟩,
refine ⟨pullback g f, pullback.snd, _, _⟩,
{ resetI, refine ⟨pullback.lift (f ≫ inv g) (𝟙 _) (by simp), ⟨_, by tidy⟩⟩,
apply pullback.hom_ext,
{ rw [assoc, pullback.lift_fst, ←pullback.condition_assoc],
simp },
{ simp } },
{ apply pullback_singleton },
end,
transitive :=
begin
rintro X S Ti ⟨Z, g, i, rfl⟩ hS,
rcases hS g (singleton_self g) with ⟨Y, f, i, hTi⟩,
refine ⟨_, f ≫ g, _, _⟩,
{ resetI, apply_instance },
ext W k,
split,
{ rintro ⟨V, h, k, ⟨_⟩, hh, rfl⟩,
rw hTi at hh,
cases hh,
apply singleton.mk },
{ rintro ⟨_⟩,
refine bind_comp g presieve.singleton.mk _,
rw hTi,
apply presieve.singleton.mk }
end }
instance : order_bot (pretopology C) :=
{ bot := trivial C,
bot_le := λ K X R,
begin
rintro ⟨Y, f, hf, rfl⟩,
exactI K.has_isos f,
end,
..pretopology.partial_order C }
/-- The trivial pretopology induces the trivial grothendieck topology. -/
lemma to_grothendieck_bot : to_grothendieck C ⊥ = ⊥ :=
(gi C).gc.l_bot
end pretopology
end category_theory
|
83e65720fc31a9bf9c0d8d907cc7cf18f0ba965c | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/basic.lean | 3bb228e2e5bd9def3531363e621272cdc8ce2a85 | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 1,809 | lean | prelude
constant {l1 l2} A : Type l1 → Type l2
#check A
definition {l} tst (A : Type*) (B : Type*) (C : Type l) : Type* := A → B → C
#check tst
constant {l} group : Type (l+1)
constant {l} carrier : group.{l} → Type l
noncomputable definition to_carrier (g : group) := carrier g
#check to_carrier.{1}
section
variable A : Type*
#check A
definition B := A → A
end
constant N : Type 1
#check B N
constant f : B N
#check f
constant a : N
#check f a
section
variable T1 : Type*
variable T2 : Type*
variable f : T1 → T2 → T2
definition double (a : T1) (b : T2) := f a (f a b)
end
#check double
#check double.{1 2}
definition Prop := Type 0
constant Eq : Π {A : Type*}, A → A → Prop
infix `=`:50 := Eq
#check Eq.{1}
section
universe variable l
universe variable u
variable {T1 : Type l}
variable {T2 : Type l}
variable {T3 : Type u}
variable f : T1 → T2 → T2
noncomputable definition is_proj2 := ∀ x y, f x y = y
noncomputable definition is_proj3 (f : T1 → T2 → T3 → T3) := ∀ x y z, f x y z = z
end
#check @is_proj2.{1}
#check @is_proj3.{1 2}
namespace foo
section
universe variables u v
variables {T1 T2 : Type u}
variable {T3 : Type v}
variable f : T1 → T2 → T2
noncomputable definition is_proj2 := ∀ x y, f x y = y
noncomputable definition is_proj3 (f : T1 → T2 → T3 → T3) := ∀ x y z, f x y z = z
end
#check @foo.is_proj2.{1}
#check @foo.is_proj3.{1 2}
end foo
namespace bla
section
variable {T1 : Type*}
variable {T2 : Type*}
variable {T3 : Type*}
variable f : T1 → T2 → T2
noncomputable definition is_proj2 := ∀ x y, f x y = y
noncomputable definition is_proj3 (f : T1 → T2 → T3 → T3) := ∀ x y z, f x y z = z
end
#check @bla.is_proj2.{1 2}
#check @bla.is_proj3.{1 2 3}
end bla
|
47018e1e453b319dde8595cc208b84ac88646d33 | 947b78d97130d56365ae2ec264df196ce769371a | /src/Lean/Util/CollectFVars.lean | f353cd80b2dbebe76e3b3151bf4b7fa016f73a5f | [
"Apache-2.0"
] | permissive | shyamalschandra/lean4 | 27044812be8698f0c79147615b1d5090b9f4b037 | 6e7a883b21eaf62831e8111b251dc9b18f40e604 | refs/heads/master | 1,671,417,126,371 | 1,601,859,995,000 | 1,601,860,020,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,178 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Expr
namespace Lean
namespace CollectFVars
structure State :=
(visitedExpr : ExprSet := {})
(fvarSet : NameSet := {})
instance State.inhabited : Inhabited State := ⟨{}⟩
abbrev Visitor := State → State
@[inline] def visit (f : Expr → Visitor) (e : Expr) : Visitor :=
fun s =>
if !e.hasFVar || s.visitedExpr.contains e then s
else f e { s with visitedExpr := s.visitedExpr.insert e }
partial def main : Expr → Visitor
| Expr.proj _ _ e _ => visit main e
| Expr.forallE _ d b _ => visit main b ∘ visit main d
| Expr.lam _ d b _ => visit main b ∘ visit main d
| Expr.letE _ t v b _ => visit main b ∘ visit main v ∘ visit main t
| Expr.app f a _ => visit main a ∘ visit main f
| Expr.mdata _ b _ => visit main b
| Expr.fvar fvarId _ => fun s => { s with fvarSet := s.fvarSet.insert fvarId }
| _ => id
end CollectFVars
def collectFVars (s : CollectFVars.State) (e : Expr) : CollectFVars.State :=
CollectFVars.main e s
end Lean
|
fea7c75497146837ad2a41193593d789aa472554 | 4aca55eba10c989f0d58647d3c2f371e7da44355 | /src/srg.lean | 493d889eaeebcdee209250110420d840cbced437 | [] | no_license | eric-wieser/l534zhan-my_project | f9fc75fb5454405e1a2fa9b56cf96c355f6f2336 | febc91e76b7b00fe2517f258ca04d27b7f35fcf3 | refs/heads/master | 1,689,218,910,420 | 1,630,439,440,000 | 1,630,439,440,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,255 | lean | import main2
import combinatorics.simple_graph.adj_matrix
import combinatorics.simple_graph.strongly_regular
/-!
# Strong regular graphs
This file attempts to construct strong regular graphs from
regular symmetric Hadamard matrices with constant diagonal (RSHCD).
-/
set_option pp.beta true
variables {α I R V : Type*}
variables [fintype V] [fintype I] -- [semiring R]
open matrix simple_graph fintype finset
open_locale big_operators matrix
local notation `n` := (fintype.card V : ℚ)
namespace matrix
class adj_matrix
[mul_zero_one_class α] [nontrivial α]
(A : matrix I I α) : Prop :=
(zero_or_one [] : ∀ i j, (A i j) = 0 ∨ (A i j) = 1 . obviously)
(sym [] : A.is_sym . obviously)
(loopless [] : ∀ i, A i i = 0 . obviously)
lemma is_sym_of_adj_matrix
[semiring R] (G : simple_graph I) [decidable_rel G.adj] :
(G.adj_matrix R).is_sym := transpose_adj_matrix G
instance is_adj_matrix_of_adj_matrix
[semiring R] [nontrivial R] (G : simple_graph I) [decidable_rel G.adj] :
adj_matrix (G.adj_matrix R) :=
{ zero_or_one := λ i j, by by_cases G.adj i j; simp*,
sym := is_sym_of_adj_matrix G,
loopless := λ i, by simp }
#check compl_adj
def compl
[mul_zero_one_class α] [nontrivial α] [decidable_eq α] [decidable_eq V]
(A : matrix V V α) [adj_matrix A] : matrix V V α :=
λ i j, ite (i = i) 0 (ite (A i j = 0) 1 0)
@[simp]
lemma diag_ne_one_of_adj_matrix
[mul_zero_one_class α] [nontrivial α]
(A : matrix V V α) [c : adj_matrix A] (i : V) :
A i i ≠ 1 :=
by simp [c.loopless]
def to_graph
[mul_zero_one_class α] [nontrivial α] [decidable_eq α]
(A : matrix V V α) [c : adj_matrix A]:
simple_graph V :=
{ adj := λ i j, ite (A i j = 1) true false,
sym := λ i j h, by simp only [c.sym.apply' i j]; convert h,
loopless := λ i, by simp }
instance
[mul_zero_one_class α] [nontrivial α] [decidable_eq α]
(A : matrix V V α) [c : adj_matrix A] :
decidable_rel A.to_graph.adj :=
by {simp [to_graph], apply_instance}
#check is_regular_of_degree
lemma to_graph_is_SRG_of
[non_assoc_semiring α] [nontrivial α] [decidable_eq α] [decidable_eq V]
(A : matrix V V α) [adj_matrix A] {k l m : ℕ}
(eq₁ : A ⬝ (𝟙 : matrix V V α) = k • 𝟙)
(eq₂ : A ⬝ A = k • 1 + l • A + m • A.compl):
is_SRG_of A.to_graph (card I) k l m := sorry
-------------------------------------------------------------------------------
class RSHCD (H : matrix I I ℚ) extends Hadamard_matrix H : Prop :=
(regular [] : ∀ i j, ∑ b, H i b = ∑ a, H a j)
(sym [] : H.is_sym)
(const_diag [] : ∀ i j, H i i = H i j)
namespace RSHCD
def diag [inhabited I] (H : matrix I I ℚ) [RSHCD H] : ℚ :=
H (default I) (default I)
lemma regular_row
(H : matrix I I ℚ) [RSHCD H] (a b : I) :
∑ j : I, H a j = ∑ j : I, H b j :=
by rw [regular H a a, regular H b a]
def row_sum [inhabited I] (H : matrix I I ℚ) [RSHCD H] : ℚ :=
∑ j : I, H (default I) j
@[simp] lemma eq_row_sum
[inhabited I] (H : matrix I I ℚ) [RSHCD H] (i : I) :
∑ j : I, H i j = ∑ j : I, H (default I) j :=
regular_row H i (default I)
def to_adj [inhabited V] (H : matrix V V ℚ) [RSHCD H] :
matrix V V ℚ :=
((1 : ℚ) / 2) • (𝟙 - (diag H) • H)
def to_adj_eq₁
[inhabited V] (H : matrix V V ℚ) [RSHCD H] :
(to_adj H) ⬝ (𝟙 : matrix V V ℚ) =
((n - (diag H) * (row_sum H)) / 2) • 𝟙 :=
begin
have : (n - (diag H) * (row_sum H)) / 2 =
((1 : ℚ) / 2) * (n - (diag H) * (row_sum H)) := by field_simp,
rw[this], ext i j,
simp [matrix.mul, all_one, to_adj, row_sum, ←finset.mul_sum],
congr,
end
end RSHCD
open RSHCD
instance [inhabited V] (H : matrix V V ℚ) [RSHCD H] :
adj_matrix (to_adj H) := {..}
def to_graph_of_RSHD [inhabited V] (H : matrix V V ℚ) [RSHCD H] :
simple_graph V := (to_adj H).to_graph
instance adj.decidable_rel'
[inhabited V] (H : matrix V V ℚ) [RSHCD H] :
decidable_rel H.to_graph_of_RSHD.adj :=
by simp [to_graph_of_RSHD]; apply_instance
lemma to_graph_is_SRG_of_RSHD
[inhabited V] [decidable_eq V] (H : matrix V V ℚ) [RSHCD H] :
is_SRG_of H.to_graph_of_RSHD sorry sorry sorry sorry := sorry
end matrix
#check transpose_adj_matrix
#check simple_graph
#check adj_matrix
#check from_rel
#check is_SRG_of
#check is_regular_of_degree
|
d6f0ed60e385ec308e09915cfb453383fd2ebb2a | 159fed64bfae88f3b6a6166836d6278f953bcbf9 | /Structure/UniverseFunctor.lean | 4769378cd17e9bd27d1df8f269fbd4c929df5377 | [
"MIT"
] | permissive | SReichelt/lean4-experiments | 3e56830c8b2fbe3814eda071c48e3c8810d254a8 | ff55357a01a34a91bf670d712637480089085ee4 | refs/heads/main | 1,683,977,454,907 | 1,622,991,121,000 | 1,622,991,121,000 | 340,765,677 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 14,328 | lean | -- An abstract formalization of "isomorphism is equality up to relabeling"
-- -------------------------------------------------------------------------
--
-- See `README.md` for more info.
--
-- Helpers for the construction of functors into `universeStructure`.
import Structure.Basic
import Structure.Forgetfulness
open Morphisms
open HasStructure
open Structure
open GeneralizedFunctor
open StructureFunctor
open Forgetfulness
open SetoidStructureFunctor
open SetoidStructureEquiv
set_option autoBoundImplicitLocal false
-- Aliases for functors into `universeStructure`.
@[reducible] def UniverseFunctor (S : Structure) := StructureFunctor S universeStructure
-- A special limitation of `UniverseFunctor S` for a generic `S` is that since equivalences of
-- equivalences in `S` are propositions, we can only map instances of `S` to setoid structures. The
-- definition of `UniverseFunctor` itself does not encode this knowledge, but sometimes we want to
-- restrict ourselves to this very frequent case. We can do this by replacing `UniverseFunctor` with
-- `SetoidUniverseFunctor` and then coercing it into a `UniverseFunctor`.
structure SetoidUniverseFunctor (S : Structure) :=
(map : S → universeStructure)
(functor : Functor (T := universeStructure) (setoidStructure ∘ map))
namespace SetoidUniverseFunctor
def universeFunctor {S : Structure} (F : SetoidUniverseFunctor S) : UniverseFunctor S :=
{ map := setoidStructure ∘ F.map,
functor := F.functor }
instance {S : Structure} : Coe (SetoidUniverseFunctor S) (UniverseFunctor S) := ⟨universeFunctor⟩
def toSetoidUniverseFunctor {S : Structure} (F : UniverseFunctor S) : SetoidUniverseFunctor S :=
{ map := F.map,
functor := comp.genFun' F.map F.functor toSetoidStructureEquiv.genFun }
def compFun {S T : Structure} (F : StructureFunctor S T) (G : SetoidUniverseFunctor T) : SetoidUniverseFunctor S :=
{ map := G.map ∘ F.map,
functor := comp.genFun' F.map F.functor G.functor }
def constFun {S : Structure} (T : Structure) : SetoidUniverseFunctor S :=
{ map := Function.const (IsType.type S) T,
functor := const.genFun (setoidStructure T) }
end SetoidUniverseFunctor
-- The function `setoidStructure`, which truncates the equivalences of a structure to setoids, is a
-- `UniverseFunctor`.
def structureToSetoidStructureFunctor' : SetoidUniverseFunctor universeStructure :=
{ map := id,
functor := toSetoidStructureEquiv.genFun }
def setoidUniverseFunctor {S : Structure} (F : UniverseFunctor S) : SetoidUniverseFunctor S :=
SetoidUniverseFunctor.compFun F structureToSetoidStructureFunctor'
def structureToSetoidStructureFunctor : UniverseFunctor universeStructure :=
SetoidUniverseFunctor.universeFunctor structureToSetoidStructureFunctor'
-- A helper structure for constructing a `SetoidUniverseFunctor`, and thus indirectly also a
-- `UniverseFunctor`.
--
-- Instead of directly specifying the resulting structure equivalence, we only give its `toFun` because
-- the `invFun` can be obtained by inverting the input equivalence. By stating the functor axioms in terms
-- of `toFun`, we can ensure that the result is indeed a functor.
--
-- The limitation that we can only define functors to setoid structures is reflected in
-- `SetoidUniverseFunctorDesc` in two ways:
-- * `toFun` yields a `SetoidStructureFunctor` because for a given equivalence `e`, it would be
-- unrealistic to obtain a regular `StructureFunctor`.
-- * The `respects...` functions return proofs. Especially, `respectsSetoid` can only return a proof since
-- it takes a proof as an input.
structure SetoidUniverseFunctorDesc (S : Structure) where
(map : S → Structure)
(toFun {a b : S} : a ≃ b → SetoidStructureFunctor (map a) (map b))
(respectsSetoid {a b : S} {e₁ e₂ : a ≃ b} : e₁ ≈ e₂ → ∀ T, toFun e₁ T ≈ toFun e₂ T)
(respectsComp {a b c : S} (e : a ≃ b) (f : b ≃ c) : ∀ T, toFun (f • e) T ≈ toFun f (toFun e T))
(respectsId (a : S) : ∀ T, toFun (id_ a) T ≈ T)
namespace SetoidUniverseFunctorDesc
variable {S : Structure} (D : SetoidUniverseFunctorDesc S)
def targetLeftInv {a b : S} (e : a ≃ b) : D.toFun e⁻¹ ⊙ D.toFun e ≃ @idFun (setoidStructure (D.map a)) :=
makeToSetoidStructureFunctorEquiv (λ T => let h₁ := D.respectsComp e e⁻¹ T;
let h₂ := D.respectsSetoid (leftInv e) T;
let h₃ := Setoid.trans (Setoid.symm h₁) h₂;
let h₄ := D.respectsId a T;
Setoid.trans h₃ h₄)
def targetRightInv {a b : S} (e : a ≃ b) : D.toFun e ⊙ D.toFun e⁻¹ ≃ @idFun (setoidStructure (D.map b)) :=
makeToSetoidStructureFunctorEquiv (λ T => let h₁ := D.respectsComp e⁻¹ e T;
let h₂ := D.respectsSetoid (rightInv e) T;
let h₃ := Setoid.trans (Setoid.symm h₁) h₂
let h₄ := D.respectsId b T;
Setoid.trans h₃ h₄)
def targetEquiv {a b : S} (e : a ≃ b) : SetoidStructureEquiv (D.map a) (D.map b) :=
{ toFun := D.toFun e,
invFun := D.toFun e⁻¹,
isInv := makeSetoidStructureFunctorInverse (targetLeftInv D e) (targetRightInv D e) }
theorem targetRespectsEquiv {a b : S} {e₁ e₂ : a ≃ b} :
e₁ ≈ e₂ → targetEquiv D e₁ ≈ targetEquiv D e₂ :=
λ h => ⟨makeSetoidStructureEquivEquiv (D.respectsSetoid h)
(D.respectsSetoid (inv_congrArg h))⟩
theorem targetRespectsComp {a b c : S} (e : a ≃ b) (f : b ≃ c) :
targetEquiv D (f • e) ≈ StructureEquiv.trans (targetEquiv D e) (targetEquiv D f) :=
⟨makeSetoidStructureEquivEquiv (D.respectsComp e f)
(λ T => let h₁ := D.respectsComp f⁻¹ e⁻¹ T;
let h₂ := D.respectsSetoid (compInv e f) T;
Setoid.trans h₂ h₁)⟩
theorem targetRespectsId (a : S) :
targetEquiv D (id_ a) ≈ StructureEquiv.refl (setoidStructure (D.map a)) :=
⟨makeSetoidStructureEquivEquiv (D.respectsId a)
(λ T => let h₁ := D.respectsId a T;
let h₂ := D.respectsSetoid (idInv a) T;
Setoid.trans h₂ h₁)⟩
theorem targetRespectsInv {a b : S} (e : a ≃ b) :
targetEquiv D e⁻¹ ≈ StructureEquiv.symm (targetEquiv D e) :=
⟨makeSetoidStructureEquivEquiv (λ T => Setoid.refl (D.toFun e⁻¹ T))
(D.respectsSetoid (invInv e))⟩
def setoidUniverseFunctor : SetoidUniverseFunctor S :=
{ map := D.map,
functor := { mapEquiv := targetEquiv D,
isFunctor := { respectsEquiv := targetRespectsEquiv D,
respectsComp := targetRespectsComp D,
respectsId := targetRespectsId D,
respectsInv := targetRespectsInv D } } }
def universeFunctor : UniverseFunctor S := SetoidUniverseFunctor.universeFunctor (setoidUniverseFunctor D)
end SetoidUniverseFunctorDesc
instance {S : Structure} : Coe (SetoidUniverseFunctorDesc S) (SetoidUniverseFunctor S) := ⟨SetoidUniverseFunctorDesc.setoidUniverseFunctor⟩
instance {S : Structure} : Coe (SetoidUniverseFunctorDesc S) (UniverseFunctor S) := ⟨SetoidUniverseFunctorDesc.universeFunctor⟩
-- A 2-functor specifically between universe structures. I.e. we have a functor between structures and a
-- functor between equivalences.
structure UniverseStructureFunctor where
(map : Structure → Structure)
(mapEquiv {S T : Structure} : S ≃ T → map S ≃ map T)
(respectsEquiv {S T : Structure} : GeneralizedFunctor.Functor (S := StructureEquiv.equivStructure S T) (T := StructureEquiv.equivStructure (map S) (map T)) mapEquiv)
(respectsComp {S T U : Structure} (e : S ≃ T) (f : T ≃ U) : mapEquiv (f • e) ≃ mapEquiv f • mapEquiv e)
-- TODO: Why don't `≃` and `id_` work as they should here?
(respectsId (S : Structure) : StructureEquiv.EquivEquiv (mapEquiv (id_ S)) (StructureEquiv.refl (map S)))
(respectsInv {S T : Structure} (e : S ≃ T) : mapEquiv e⁻¹ ≃ (mapEquiv e)⁻¹)
namespace UniverseStructureFunctor
instance universeStructureFunctorCoeFun : CoeFun UniverseStructureFunctor (λ _ => Structure → Structure) := ⟨UniverseStructureFunctor.map⟩
variable (F : UniverseStructureFunctor)
def congrArg {S T : Structure} (e : S ≃ T) : F S ≃ F T := F.mapEquiv e
def universeFunctor : UniverseFunctor universeStructure :=
{ map := F.map,
functor := { mapEquiv := F.mapEquiv,
isFunctor := { respectsEquiv := λ ⟨η⟩ => ⟨F.respectsEquiv η⟩,
respectsComp := λ e f => ⟨F.respectsComp e f⟩,
respectsId := λ S => ⟨F.respectsId S⟩,
respectsInv := λ e => ⟨F.respectsInv e⟩ } } }
end UniverseStructureFunctor
instance : Coe UniverseStructureFunctor (UniverseFunctor universeStructure) := ⟨UniverseStructureFunctor.universeFunctor⟩
-- Similar to `SetoidUniverseFunctorDesc`, but constructs a `UniverseStructureFunctor` producing regular
-- structures without any truncation to setoids. This is possible because equivalences of structure
-- equivalences also come in a non-setoid form.
structure UniverseStructureFunctorDesc where
(map : Structure → Structure)
(toFun {S T : Structure} : S ≃ T → StructureFunctor (map S) (map T))
(respectsEquiv {S T : Structure} : GeneralizedFunctor.Functor (S := StructureEquiv.equivStructure S T) (T := functorStructure (map S) (map T)) toFun)
(respectsComp {S T U : Structure} (e : S ≃ T) (f : T ≃ U) : toFun (f • e) ≃ toFun f ⊙ toFun e)
(respectsCompNat {S T U : Structure} {e₁ e₂ : S ≃ T} {f₁ f₂ : T ≃ U} (η : e₁ ≃ e₂) (θ : f₁ ≃ f₂) :
compFun.congrArg (respectsEquiv η) (respectsEquiv θ) • respectsComp e₁ f₁ ≈ respectsComp e₂ f₂ • respectsEquiv (StructureEquiv.comp_congrArg η θ))
(respectsId (S : Structure) : toFun (id_ S) ≃ @idFun (map S))
namespace UniverseStructureFunctorDesc
variable (D : UniverseStructureFunctorDesc)
-- TODO: We can probably make use of `toFunFunctor` somehow in order to make the missing proofs easier.
def toFunFunctor (S T : Structure) : StructureFunctor (StructureEquiv.equivStructure S T) (functorStructure (D.map S) (D.map T)) :=
{ map := D.toFun,
functor := D.respectsEquiv }
def targetLeftInv {S T : Structure} (e : S ≃ T) : D.toFun e⁻¹ ⊙ D.toFun e ≃ @idFun (D.map S) :=
let η₁ := FunctorEquiv.symm (D.respectsComp e e⁻¹);
let η₂ := D.respectsEquiv (StructureEquiv.leftInv' e);
FunctorEquiv.trans (FunctorEquiv.trans η₁ η₂) (D.respectsId S)
def targetRightInv {S T : Structure} (e : S ≃ T) : D.toFun e ⊙ D.toFun e⁻¹ ≃ @idFun (D.map T) :=
let η₁ := FunctorEquiv.symm (D.respectsComp e⁻¹ e);
let η₂ := D.respectsEquiv (StructureEquiv.rightInv' e);
FunctorEquiv.trans (FunctorEquiv.trans η₁ η₂) (D.respectsId T)
-- This might simplify some proofs.
theorem targetInv {S T : Structure} (e : S ≃ T) :
targetRightInv D e ≈ StructureEquiv.symm_symm e ▸ targetLeftInv D e⁻¹ :=
sorry
def targetEquiv {S T : Structure} (e : S ≃ T) : StructureEquiv (D.map S) (D.map T) :=
{ toFun := D.toFun e,
invFun := D.toFun e⁻¹,
isInv := { leftInv := targetLeftInv D e,
rightInv := targetRightInv D e,
lrCompat := sorry,
rlCompat := sorry } }
def targetRespectsEquiv {S T : Structure} {e₁ e₂ : S ≃ T} (η : e₁ ≃ e₂) :
targetEquiv D e₁ ≃ targetEquiv D e₂ :=
{ toFunEquiv := D.respectsEquiv η,
invFunEquiv := D.respectsEquiv (StructureEquiv.inv_congrArg η),
leftInvEquiv := sorry,
rightInvEquiv := sorry }
def targetEquiv.functor {S T : Structure} :
GeneralizedFunctor.Functor (S := StructureEquiv.equivStructure S T) (T := StructureEquiv.equivStructure (D.map S) (D.map T)) (targetEquiv D) :=
{ mapEquiv := targetRespectsEquiv D,
isFunctor := sorry }
def targetRespectsComp {S T U : Structure} (e : S ≃ T) (f : T ≃ U) :
targetEquiv D (StructureEquiv.trans e f) ≃ StructureEquiv.trans (targetEquiv D e) (targetEquiv D f) :=
{ toFunEquiv := D.respectsComp e f,
invFunEquiv := D.respectsComp f⁻¹ e⁻¹,
leftInvEquiv := sorry,
rightInvEquiv := sorry }
def targetRespectsId (S : Structure) :
targetEquiv D (StructureEquiv.refl S) ≃ StructureEquiv.refl (D.map S) :=
{ toFunEquiv := D.respectsId S,
invFunEquiv := D.respectsId S,
leftInvEquiv := sorry,
rightInvEquiv := sorry }
def targetRespectsInv {S T : Structure} (e : S ≃ T) :
targetEquiv D (StructureEquiv.symm e) ≃ StructureEquiv.symm (targetEquiv D e) :=
{ toFunEquiv := FunctorEquiv.refl (D.toFun e⁻¹),
invFunEquiv := D.respectsEquiv (StructureEquiv.invInv e),
leftInvEquiv := sorry,
rightInvEquiv := sorry }
def universeStructureFunctor : UniverseStructureFunctor :=
{ map := D.map,
mapEquiv := targetEquiv D,
respectsEquiv := targetEquiv.functor D,
respectsComp := targetRespectsComp D,
respectsId := targetRespectsId D,
respectsInv := targetRespectsInv D }
def universeFunctor : UniverseFunctor universeStructure := UniverseStructureFunctor.universeFunctor (universeStructureFunctor D)
end UniverseStructureFunctorDesc
instance : Coe UniverseStructureFunctorDesc UniverseStructureFunctor := ⟨UniverseStructureFunctorDesc.universeStructureFunctor⟩
instance : Coe UniverseStructureFunctorDesc (UniverseFunctor universeStructure) := ⟨UniverseStructureFunctorDesc.universeFunctor⟩
|
4dbda01f9d44e65e1d7c1c6681762ce6c1c421a1 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/topology/uniform_space/uniform_convergence.lean | d6a61d1095bfc60599e9bcd9550a4a60d3acf0dc | [
"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 | 16,649 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import topology.uniform_space.basic
/-!
# Uniform convergence
A sequence of functions `Fₙ` (with values in a metric space) converges uniformly on a set `s` to a
function `f` if, for all `ε > 0`, for all large enough `n`, one has for all `y ∈ s` the inequality
`dist (f y, Fₙ y) < ε`. Under uniform convergence, many properties of the `Fₙ` pass to the limit,
most notably continuity. We prove this in the file, defining the notion of uniform convergence
in the more general setting of uniform spaces, and with respect to an arbitrary indexing set
endowed with a filter (instead of just `ℕ` with `at_top`).
## Main results
Let `α` be a topological space, `β` a uniform space, `Fₙ` and `f` be functions from `α`to `β`
(where the index `n` belongs to an indexing type `ι` endowed with a filter `p`).
* `tendsto_uniformly_on F f p s`: the fact that `Fₙ` converges uniformly to `f` on `s`. This means
that, for any entourage `u` of the diagonal, for large enough `n` (with respect to `p`), one has
`(f y, Fₙ y) ∈ u` for all `y ∈ s`.
* `tendsto_uniformly F f p`: same notion with `s = univ`.
* `tendsto_uniformly_on.continuous_on`: a uniform limit on a set of functions which are continuous
on this set is itself continuous on this set.
* `tendsto_uniformly.continuous`: a uniform limit of continuous functions is continuous.
* `tendsto_uniformly_on.tendsto_comp`: If `Fₙ` tends uniformly to `f` on a set `s`, and `gₙ` tends
to `x` within `s`, then `Fₙ gₙ` tends to `f x` if `f` is continuous at `x` within `s`.
* `tendsto_uniformly.tendsto_comp`: If `Fₙ` tends uniformly to `f`, and `gₙ` tends to `x`, then
`Fₙ gₙ` tends to `f x`.
We also define notions where the convergence is locally uniform, called
`tendsto_locally_uniformly_on F f p s` and `tendsto_locally_uniformly F f p`. The previous theorems
all have corresponding versions under locally uniform convergence.
## Implementation notes
Most results hold under weaker assumptions of locally uniform approximation. In a first section,
we prove the results under these weaker assumptions. Then, we derive the results on uniform
convergence from them.
## Tags
Uniform limit, uniform convergence, tends uniformly to
-/
noncomputable theory
open_locale topological_space classical uniformity
open set filter
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
/-!
### Different notions of uniform convergence
We define uniform convergence and locally uniform convergence, on a set or in the whole space.
-/
variables {ι : Type*} [uniform_space β]
{F : ι → α → β} {f : α → β} {s s' : set α} {x : α} {p : filter ι} {g : ι → α}
/-- A sequence of functions `Fₙ` converges uniformly on a set `s` to a limiting function `f` with
respect to the filter `p` if, for any entourage of the diagonal `u`, one has `p`-eventually
`(f x, Fₙ x) ∈ u` for all `x ∈ s`. -/
def tendsto_uniformly_on (F : ι → α → β) (f : α → β) (p : filter ι) (s : set α) :=
∀ u ∈ 𝓤 β, ∀ᶠ n in p, ∀ x ∈ s, (f x, F n x) ∈ u
/-- A sequence of functions `Fₙ` converges uniformly to a limiting function `f` with respect to a
filter `p` if, for any entourage of the diagonal `u`, one has `p`-eventually
`(f x, Fₙ x) ∈ u` for all `x`. -/
def tendsto_uniformly (F : ι → α → β) (f : α → β) (p : filter ι) :=
∀ u ∈ 𝓤 β, ∀ᶠ n in p, ∀ x, (f x, F n x) ∈ u
lemma tendsto_uniformly_on_univ :
tendsto_uniformly_on F f p univ ↔ tendsto_uniformly F f p :=
by simp [tendsto_uniformly_on, tendsto_uniformly]
lemma tendsto_uniformly_on.mono {s' : set α}
(h : tendsto_uniformly_on F f p s) (h' : s' ⊆ s) : tendsto_uniformly_on F f p s' :=
λ u hu, (h u hu).mono (λ n hn x hx, hn x (h' hx))
lemma tendsto_uniformly.tendsto_uniformly_on
(h : tendsto_uniformly F f p) : tendsto_uniformly_on F f p s :=
(tendsto_uniformly_on_univ.2 h).mono (subset_univ s)
/-- Composing on the right by a function preserves uniform convergence on a set -/
lemma tendsto_uniformly_on.comp (h : tendsto_uniformly_on F f p s) (g : γ → α) :
tendsto_uniformly_on (λ n, F n ∘ g) (f ∘ g) p (g ⁻¹' s) :=
begin
assume u hu,
apply (h u hu).mono (λ n hn, _),
exact λ x hx, hn _ hx
end
/-- Composing on the right by a function preserves uniform convergence -/
lemma tendsto_uniformly.comp (h : tendsto_uniformly F f p) (g : γ → α) :
tendsto_uniformly (λ n, F n ∘ g) (f ∘ g) p :=
begin
assume u hu,
apply (h u hu).mono (λ n hn, _),
exact λ x, hn _
end
variable [topological_space α]
/-- A sequence of functions `Fₙ` converges locally uniformly on a set `s` to a limiting function
`f` with respect to a filter `p` if, for any entourage of the diagonal `u`, for any `x ∈ s`, one
has `p`-eventually `(f x, Fₙ x) ∈ u` for all `y` in a neighborhood of `x` in `s`. -/
def tendsto_locally_uniformly_on (F : ι → α → β) (f : α → β) (p : filter ι) (s : set α) :=
∀ u ∈ 𝓤 β, ∀ x ∈ s, ∃ t ∈ 𝓝[s] x, ∀ᶠ n in p, ∀ y ∈ t, (f y, F n y) ∈ u
/-- A sequence of functions `Fₙ` converges locally uniformly to a limiting function `f` with respect
to a filter `p` if, for any entourage of the diagonal `u`, for any `x`, one has `p`-eventually
`(f x, Fₙ x) ∈ u` for all `y` in a neighborhood of `x`. -/
def tendsto_locally_uniformly (F : ι → α → β) (f : α → β) (p : filter ι) :=
∀ u ∈ 𝓤 β, ∀ (x : α), ∃ t ∈ 𝓝 x, ∀ᶠ n in p, ∀ y ∈ t, (f y, F n y) ∈ u
lemma tendsto_uniformly_on.tendsto_locally_uniformly_on
(h : tendsto_uniformly_on F f p s) : tendsto_locally_uniformly_on F f p s :=
λ u hu x hx, ⟨s, self_mem_nhds_within, h u hu⟩
lemma tendsto_uniformly.tendsto_locally_uniformly
(h : tendsto_uniformly F f p) : tendsto_locally_uniformly F f p :=
λ u hu x, ⟨univ, univ_mem_sets, by simpa using h u hu⟩
lemma tendsto_locally_uniformly_on.mono (h : tendsto_locally_uniformly_on F f p s) (h' : s' ⊆ s) :
tendsto_locally_uniformly_on F f p s' :=
begin
assume u hu x hx,
rcases h u hu x (h' hx) with ⟨t, ht, H⟩,
exact ⟨t, nhds_within_mono x h' ht, H.mono (λ n, id)⟩
end
lemma tendsto_locally_uniformly_on_univ :
tendsto_locally_uniformly_on F f p univ ↔ tendsto_locally_uniformly F f p :=
by simp [tendsto_locally_uniformly_on, tendsto_locally_uniformly, nhds_within_univ]
lemma tendsto_locally_uniformly_on.comp [topological_space γ] {t : set γ}
(h : tendsto_locally_uniformly_on F f p s)
(g : γ → α) (hg : maps_to g t s) (cg : continuous_on g t) :
tendsto_locally_uniformly_on (λ n, (F n) ∘ g) (f ∘ g) p t :=
begin
assume u hu x hx,
rcases h u hu (g x) (hg hx) with ⟨a, ha, H⟩,
have : g⁻¹' a ∈ 𝓝[t] x :=
((cg x hx).preimage_mem_nhds_within' (nhds_within_mono (g x) hg.image_subset ha)),
exact ⟨g ⁻¹' a, this, H.mono (λ n hn y hy, hn _ hy)⟩
end
lemma tendsto_locally_uniformly.comp [topological_space γ]
(h : tendsto_locally_uniformly F f p) (g : γ → α) (cg : continuous g) :
tendsto_locally_uniformly (λ n, (F n) ∘ g) (f ∘ g) p :=
begin
rw ← tendsto_locally_uniformly_on_univ at h ⊢,
rw continuous_iff_continuous_on_univ at cg,
exact h.comp _ (maps_to_univ _ _) cg
end
/-!
### Uniform approximation
In this section, we give lemmas ensuring that a function is continuous if it can be approximated
uniformly by continuous functions. We give various versions, within a set or the whole space, at
a single point or at all points, with locally uniform approximation or uniform approximation. All
the statements are derived from a statement about locally uniform approximation within a set at
a point, called `continuous_within_at_of_locally_uniform_approx_of_continuous_within_at`. -/
/-- A function which can be locally uniformly approximated by functions which are continuous
within a set at a point is continuous within this set at this point. -/
lemma continuous_within_at_of_locally_uniform_approx_of_continuous_within_at
(hx : x ∈ s) (L : ∀ u ∈ 𝓤 β, ∃ t ∈ 𝓝[s] x, ∃ n, ∀ y ∈ t, (f y, F n y) ∈ u)
(C : ∀ n, continuous_within_at (F n) s x) : continuous_within_at f s x :=
begin
apply uniform.continuous_within_at_iff'_left.2 (λ u₀ hu₀, _),
obtain ⟨u₁, h₁, u₁₀⟩ : ∃ (u : set (β × β)) (H : u ∈ 𝓤 β), comp_rel u u ⊆ u₀ :=
comp_mem_uniformity_sets hu₀,
obtain ⟨u₂, h₂, hsymm, u₂₁⟩ : ∃ (u : set (β × β)) (H : u ∈ 𝓤 β),
(∀{a b}, (a, b) ∈ u → (b, a) ∈ u) ∧ comp_rel u u ⊆ u₁ := comp_symm_of_uniformity h₁,
rcases L u₂ h₂ with ⟨t, tx, n, ht⟩,
have A : ∀ᶠ y in 𝓝[s] x, (f y, F n y) ∈ u₂ := eventually.mono tx ht,
have B : ∀ᶠ y in 𝓝[s] x, (F n y, F n x) ∈ u₂ :=
uniform.continuous_within_at_iff'_left.1 (C n) h₂,
have C : ∀ᶠ y in 𝓝[s] x, (f y, F n x) ∈ u₁ :=
(A.and B).mono (λ y hy, u₂₁ (prod_mk_mem_comp_rel hy.1 hy.2)),
have : (F n x, f x) ∈ u₁ :=
u₂₁ (prod_mk_mem_comp_rel (refl_mem_uniformity h₂) (hsymm (A.self_of_nhds_within hx))),
exact C.mono (λ y hy, u₁₀ (prod_mk_mem_comp_rel hy this))
end
/-- A function which can be locally uniformly approximated by functions which are continuous at
a point is continuous at this point. -/
lemma continuous_at_of_locally_uniform_approx_of_continuous_at
(L : ∀ u ∈ 𝓤 β, ∃ t ∈ 𝓝 x, ∃ n, ∀ y ∈ t, (f y, F n y) ∈ u) (C : ∀ n, continuous_at (F n) x) :
continuous_at f x :=
begin
simp only [← continuous_within_at_univ] at C ⊢,
apply continuous_within_at_of_locally_uniform_approx_of_continuous_within_at (mem_univ _) _ C,
simpa [nhds_within_univ] using L
end
/-- A function which can be locally uniformly approximated by functions which are continuous
on a set is continuous on this set. -/
lemma continuous_on_of_locally_uniform_approx_of_continuous_on
(L : ∀ (x ∈ s) (u ∈ 𝓤 β), ∃t ∈ 𝓝[s] x, ∃n, ∀ y ∈ t, (f y, F n y) ∈ u)
(C : ∀ n, continuous_on (F n) s) : continuous_on f s :=
λ x hx, continuous_within_at_of_locally_uniform_approx_of_continuous_within_at hx
(L x hx) (λ n, C n x hx)
/-- A function which can be uniformly approximated by functions which are continuous on a set
is continuous on this set. -/
lemma continuous_on_of_uniform_approx_of_continuous_on
(L : ∀ u ∈ 𝓤 β, ∃ n, ∀ y ∈ s, (f y, F n y) ∈ u) :
(∀ n, continuous_on (F n) s) → continuous_on f s :=
continuous_on_of_locally_uniform_approx_of_continuous_on
(λ x hx u hu, ⟨s, self_mem_nhds_within, L u hu⟩)
/-- A function which can be locally uniformly approximated by continuous functions is continuous. -/
lemma continuous_of_locally_uniform_approx_of_continuous
(L : ∀ (x : α), ∀ u ∈ 𝓤 β, ∃ t ∈ 𝓝 x, ∃ n, ∀ y ∈ t, (f y, F n y) ∈ u)
(C : ∀ n, continuous (F n)) : continuous f :=
begin
simp only [continuous_iff_continuous_on_univ] at ⊢ C,
apply continuous_on_of_locally_uniform_approx_of_continuous_on _ C,
simpa [nhds_within_univ] using L
end
/-- A function which can be uniformly approximated by continuous functions is continuous. -/
lemma continuous_of_uniform_approx_of_continuous (L : ∀ u ∈ 𝓤 β, ∃ N, ∀ y, (f y, F N y) ∈ u) :
(∀ n, continuous (F n)) → continuous f :=
continuous_of_locally_uniform_approx_of_continuous $ λx u hu,
⟨univ, by simpa [filter.univ_mem_sets] using L u hu⟩
/-!
### Uniform limits
From the previous statements on uniform approximation, we deduce continuity results for uniform
limits.
-/
/-- A locally uniform limit on a set of functions which are continuous on this set is itself
continuous on this set. -/
lemma tendsto_locally_uniformly_on.continuous_on (h : tendsto_locally_uniformly_on F f p s)
(hc : ∀ n, continuous_on (F n) s) [ne_bot p] : continuous_on f s :=
begin
apply continuous_on_of_locally_uniform_approx_of_continuous_on (λ x hx u hu, _) hc,
rcases h u hu x hx with ⟨t, ht, H⟩,
exact ⟨t, ht, H.exists⟩
end
/-- A uniform limit on a set of functions which are continuous on this set is itself continuous
on this set. -/
lemma tendsto_uniformly_on.continuous_on (h : tendsto_uniformly_on F f p s)
(hc : ∀ n, continuous_on (F n) s) [ne_bot p] : continuous_on f s :=
h.tendsto_locally_uniformly_on.continuous_on hc
/-- A locally uniform limit of continuous functions is continuous. -/
lemma tendsto_locally_uniformly.continuous (h : tendsto_locally_uniformly F f p)
(hc : ∀ n, continuous (F n)) [ne_bot p] : continuous f :=
begin
apply continuous_of_locally_uniform_approx_of_continuous (λ x u hu, _) hc,
rcases h u hu x with ⟨t, ht, H⟩,
exact ⟨t, ht, H.exists⟩
end
/-- A uniform limit of continuous functions is continuous. -/
lemma tendsto_uniformly.continuous (h : tendsto_uniformly F f p)
(hc : ∀ n, continuous (F n)) [ne_bot p] : continuous f :=
h.tendsto_locally_uniformly.continuous hc
/-!
### Composing limits under uniform convergence
In general, if `Fₙ` converges pointwise to a function `f`, and `gₙ` tends to `x`, it is not true
that `Fₙ gₙ` tends to `f x`. It is true however if the convergence of `Fₙ` to `f` is uniform. In
this paragraph, we prove variations around this statement.
-/
/-- If `Fₙ` converges locally uniformly on a neighborhood of `x` within a set `s` to a function `f`
which is continuous at `x` within `s `, and `gₙ` tends to `x` within `s`, then `Fₙ (gₙ)` tends
to `f x`. -/
lemma tendsto_comp_of_locally_uniform_limit_within
(h : continuous_within_at f s x) (hg : tendsto g p (𝓝[s] x))
(hunif : ∀ u ∈ 𝓤 β, ∃ t ∈ 𝓝[s] x, ∀ᶠ n in p, ∀ y ∈ t, (f y, F n y) ∈ u) :
tendsto (λ n, F n (g n)) p (𝓝 (f x)) :=
begin
apply uniform.tendsto_nhds_right.2 (λ u₀ hu₀, _),
obtain ⟨u₁, h₁, u₁₀⟩ : ∃ (u : set (β × β)) (H : u ∈ 𝓤 β), comp_rel u u ⊆ u₀ :=
comp_mem_uniformity_sets hu₀,
rcases hunif u₁ h₁ with ⟨s, sx, hs⟩,
have A : ∀ᶠ n in p, g n ∈ s := hg sx,
have B : ∀ᶠ n in p, (f x, f (g n)) ∈ u₁ := hg (uniform.continuous_within_at_iff'_right.1 h h₁),
refine ((hs.and A).and B).mono (λ y hy, _),
rcases hy with ⟨⟨H1, H2⟩, H3⟩,
exact u₁₀ (prod_mk_mem_comp_rel H3 (H1 _ H2))
end
/-- If `Fₙ` converges locally uniformly on a neighborhood of `x` to a function `f` which is
continuous at `x`, and `gₙ` tends to `x`, then `Fₙ (gₙ)` tends to `f x`. -/
lemma tendsto_comp_of_locally_uniform_limit (h : continuous_at f x) (hg : tendsto g p (𝓝 x))
(hunif : ∀ u ∈ 𝓤 β, ∃ t ∈ 𝓝 x, ∀ᶠ n in p, ∀ y ∈ t, (f y, F n y) ∈ u) :
tendsto (λ n, F n (g n)) p (𝓝 (f x)) :=
begin
rw ← continuous_within_at_univ at h,
rw ← nhds_within_univ at hunif hg,
exact tendsto_comp_of_locally_uniform_limit_within h hg hunif
end
/-- If `Fₙ` tends locally uniformly to `f` on a set `s`, and `gₙ` tends to `x` within `s`, then
`Fₙ gₙ` tends to `f x` if `f` is continuous at `x` within `s` and `x ∈ s`. -/
lemma tendsto_locally_uniformly_on.tendsto_comp (h : tendsto_locally_uniformly_on F f p s)
(hf : continuous_within_at f s x) (hx : x ∈ s) (hg : tendsto g p (𝓝[s] x)) :
tendsto (λ n, F n (g n)) p (𝓝 (f x)) :=
tendsto_comp_of_locally_uniform_limit_within hf hg (λ u hu, h u hu x hx)
/-- If `Fₙ` tends uniformly to `f` on a set `s`, and `gₙ` tends to `x` within `s`, then `Fₙ gₙ`
tends to `f x` if `f` is continuous at `x` within `s`. -/
lemma tendsto_uniformly_on.tendsto_comp (h : tendsto_uniformly_on F f p s)
(hf : continuous_within_at f s x) (hg : tendsto g p (𝓝[s] x)) :
tendsto (λ n, F n (g n)) p (𝓝 (f x)) :=
tendsto_comp_of_locally_uniform_limit_within hf hg (λ u hu, ⟨s, self_mem_nhds_within, h u hu⟩)
/-- If `Fₙ` tends locally uniformly to `f`, and `gₙ` tends to `x`, then `Fₙ gₙ` tends to `f x`. -/
lemma tendsto_locally_uniformly.tendsto_comp (h : tendsto_locally_uniformly F f p)
(hf : continuous_at f x) (hg : tendsto g p (𝓝 x)) : tendsto (λ n, F n (g n)) p (𝓝 (f x)) :=
tendsto_comp_of_locally_uniform_limit hf hg (λ u hu, h u hu x)
/-- If `Fₙ` tends uniformly to `f`, and `gₙ` tends to `x`, then `Fₙ gₙ` tends to `f x`. -/
lemma tendsto_uniformly.tendsto_comp (h : tendsto_uniformly F f p)
(hf : continuous_at f x) (hg : tendsto g p (𝓝 x)) : tendsto (λ n, F n (g n)) p (𝓝 (f x)) :=
h.tendsto_locally_uniformly.tendsto_comp hf hg
|
7397caf9b52a1a398699341a1438cbc50311c570 | 222111140d2023e142b07e9cd5119ac2829b36f5 | /src/finset_sum.lean | ab71f2cfa50de98b77de74ce15aa9b02ef5f9a28 | [] | no_license | jjcrawford/lean-gaussian-elimination | 740dfdf2de89dc99c088c53e5221d9dfad3c0b92 | c473d33c07fa6f141d17d9dc42ad07956c33dd03 | refs/heads/master | 1,585,793,867,436 | 1,540,443,865,000 | 1,540,443,865,000 | 153,901,939 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 14,483 | lean | import data.finsupp
universes u v
variable (α : Type u)
variable [ring α]
variable [decidable_eq α]
@[simp] lemma ite.map (P : Prop) [decidable P] {β γ : Type*} (a b : β) (f : β → γ) :
f (ite P a b) = ite P (f a) (f b) :=
begin
split_ifs; refl
end
@[simp] lemma ite.mul_right (P : Prop) [decidable P] (a b c : α) :
(ite P a b) * c = ite P (a * c) (b * c) := ite.map P a b (λ x, x * c)
-- theorem ite_single {α : Type*} [fintype α] [decidable_eq α] (a₀ : α) {β : Type*} [add_comm_monoid β] (f : α → β) :
def finsupp_of_ite {α : Type*} [fintype α] [decidable_eq α] (a₀ : α) {β : Type*} [decidable_eq β] [add_comm_monoid β] (f : α → β) : finsupp α β :=
begin
by_cases h₁ : f a₀ ≠ 0,
{ constructor,
show α → β,
from (λ a, ite (a₀ = a) (f a) 0),
show finset α,
from finset.singleton a₀,
intros,
simp,
split_ifs,
rw ←h,
simp[h₁],
simp[h],
from λ h₂, h (eq.symm h₂),
},
exact 0
end
-- finsupp of double ite
def finsupp_of_double_ite {α : Type*} [fintype α] [decidable_eq α] (a₀ a₁ : α) {β : Type*} [add_comm_monoid β] (f g : α → β) (h₁ : f a₀ ≠ 0) (h₂ : g a₁ ≠ 0) : finsupp α β :=
begin
constructor,
show α → β,
from (λ a, ite (a₀ = a) (f a) (ite (a₁ = a) (g a) 0)),
show finset α,
from finset.singleton a₀ ∪ finset.singleton a₁,
intros,
simp,
split_ifs,
rw ←h,
simp[h₁],
rw ←h_1,
simp[h₂],
simp,
intros h_n,
cases h_n,
from h (eq.symm h_n),
from h_1 (eq.symm h_n),
end
@[simp] lemma finsupp_of_double_ite_eq_double_ite {α : Type*} [fintype α] [decidable_eq α] (a₀ a₁ : α) {β : Type*} [add_comm_monoid β] (f g : α → β) (h₁ : f a₀ ≠ 0) (h₂ : g a₁ ≠ 0) : (λ a, ite (a₀ = a) (f a) (ite (a₁ = a) (g a) 0)) = (finsupp_of_double_ite a₀ a₁ f g h₁ h₂).to_fun :=
begin
funext,
by_cases h₁ : (a₀ = a),
simp[h₁],
unfold finsupp_of_double_ite,
simp,
unfold finsupp_of_double_ite,
end
@[simp] lemma coe_to_fun_to_fun {α : Type*} {β : Type*} [add_comm_monoid β] {fs : finsupp α β} : ⇑(fs) = fs.to_fun := rfl
lemma temp {α : Type*} [fintype α] [decidable_eq α] (a₀ a₁ : α) {β : Type*} [add_comm_monoid β] [decidable_eq β] (f g : α → β) (h₁ : f a₀ ≠ 0) (h₂ : g a₁ ≠ 0) (h_neq : a₀ ≠ a₁) (a : α) (h_p : a = a₀) : (a ∈ ((finsupp.single a₀ (f a₀)).support).val ∧
¬(finsupp.single a₀ (f a₀)).to_fun a + (finsupp.single a₁ (g a₁)).to_fun a = 0 ∨
a ∈ ((finsupp.single a₁ (g a₁)).support).val ∧
¬(finsupp.single a₀ (f a₀)).to_fun a + (finsupp.single a₁ (g a₁)).to_fun a = 0) :=
begin
simp[h_p],
constructor,
constructor,
simp[finsupp.single, h₁],
intros h_q,
--(by simp[finsupp.single,h₂, h_neq])
have H₁ : (finsupp.single a₁ (g a₁)).to_fun a₀ = 0,
from iff.elim_left (@finsupp.not_mem_support_iff α β _ (finsupp.single a₁ (g a₁)) a₀) (by simp[finsupp.single,h₂, h_neq]),
-- rw ←h_p at H₁,
simp[H₁] at h_q,
have H₂ : (finsupp.single a₀ (f a₀)).to_fun a₀ ≠ 0,
from iff.elim_left (@finsupp.mem_support_iff α β _ (finsupp.single a₀ (f a₀)) a₀) (by simp[finsupp.single, h₁]),
from H₂ h_q,
end
@[simp] lemma finsupp_of_double_ite_zip_singles {α : Type*} [fintype α] [decidable_eq α] (a₀ a₁ : α) {β : Type*} [add_comm_monoid β] [decidable_eq β] (f g : α → β) (h₁ : f a₀ ≠ 0) (h₂ : g a₁ ≠ 0) (h_neq : a₀ ≠ a₁) : (finsupp_of_double_ite a₀ a₁ f g h₁ h₂) = (finsupp.single a₀ (f a₀)) + (finsupp.single a₁ (g a₁)) :=
begin
-- congr,
unfold finsupp_of_double_ite,
-- unfold finsupp.zip_with,
congr,
show (λ a, _) = (λ a, _),
funext,
by_cases h_t : (a₀ = a);
simp[h_t],
rw ←h_t,
have H₁ : (finsupp.single a₁ (g a₁)).to_fun a₀ = 0,
from iff.elim_left (@finsupp.not_mem_support_iff α β _ (finsupp.single a₁ (g a₁)) a₀) (by
simp[finsupp.single,h₂, h_neq]),
rw H₁,
simp,
simp[finsupp.single],
simp,
by_cases h_t₀ : (a₁ = a),
simp[h_t₀],
rw ←h_t₀,
have H₁ : (finsupp.single a₀ (f a₀)).to_fun a₁ = 0,
from iff.elim_left (@finsupp.not_mem_support_iff α β _ (finsupp.single a₀ (f a₀)) a₁) (by simp[finsupp.single,h₁, λ x, h_neq (eq.symm x)]),
rw H₁,
simp,
simp[finsupp.single],
simp,
simp[h_t₀],
have H₁ : (finsupp.single a₁ (g a₁)).to_fun a = 0,
from iff.elim_left (@finsupp.not_mem_support_iff α β _ (finsupp.single a₁ (g a₁)) a) (by simp[finsupp.single, h₂, (λ x, h_t₀ (eq.symm x))]),
have H₂ : (finsupp.single a₀ (f a₀)).to_fun a = 0,
from iff.elim_left (@finsupp.not_mem_support_iff α β _ (finsupp.single a₀ (f a₀)) a) (by simp[finsupp.single, h₁, (λ x, h_t (eq.symm x))]),
rw H₁,
rw H₂,
simp,
-- simp[finset.singleton],
ext,
constructor,
intros h_p,
simp at h_p,
cases h_p with h_p h_p,
{
simp,
apply temp; assumption,
},
{simp,
apply or.swap,
rw add_comm,
apply temp,
assumption,
assumption,
from λ x, h_neq (eq.symm x),
assumption,
},
intros h_p,
simp at h_p,
cases h_p with h_p h_p,
have H₁, from and.elim_left h_p,
simp,
constructor,
-- by_contra,
-- have H₄ :
have H₃ : ((finsupp.single a₀ (f a₀)).support) = finset.singleton a₀,
simp[finsupp.single, h₁],
rw H₃ at H₁,
-- ext,
simp[finsupp.single, finsupp.support, finset.val] at H₁,
-- constructor,
from H₁,
simp,
apply or.swap,
constructor,
have H₃ : ((finsupp.single a₁ (g a₁)).support) = finset.singleton a₁,
simp[finsupp.single, h₂],
-- simp[H₃] at h_p,
rw H₃ at h_p,
simp[finsupp.single, finsupp.support, finset.val] at h_p,
from and.elim_left h_p,
end
@[simp] lemma finsupp_of_ite_single {α : Type*} [fintype α] [decidable_eq α] (a₀ : α) {β : Type*} [add_comm_monoid β] [decidable_eq β] (f : α → β) : (finsupp_of_ite a₀ f) = finsupp.single a₀ (f a₀) :=
begin
simp[finsupp.single],
simp[finsupp_of_ite],
simp[finset.singleton],
by_cases h₁ : f a₀ ≠ 0,
{ simp[h₁],
funext,
-- congr,
by_cases h₂ : (a₀ = a);
simp[h₂], },
simp at h₁,
simp [h₁],
refl,
end
@[simp] lemma finsupp_of_ite_eq_ite {α : Type*} [fintype α] [decidable_eq α] (a₀ : α) {β : Type*} [decidable_eq β] [add_comm_monoid β] (f : α → β) : (λ a, ite (a₀ = a) (f a) 0) = (finsupp_of_ite a₀ f).to_fun :=
begin
funext,
by_cases h₂ : (a₀ = a),
simp[h₂],
dsimp [finsupp.single],
simp,
unfold finsupp_of_ite,
simp,
simp[h₂],
by_cases h₁ : f a₀ ≠ 0,
simp[h₁],
simp[h₂],
simp[h₁],
refl,
end
section
open finset
-- open finsupp
lemma finsupp_sum_support_subset {α : Type*} [fintype α] [decidable_eq α] {β : Type*} [add_comm_monoid β] (fs : finsupp α β) : Π (S : finset α), fs.support ⊆ S → S.sum (fs.to_fun) = fs.sum (λ x y, y) := begin
intros S hS,
by_cases hT : (S ⊆ fs.support),
congr,
apply finset.subset.antisymm; assumption,
unfold finsupp.sum,
simp,
-- have h₇ : ⇑fs = (fs).to_fun,
-- refl,
have h₄, from eq.symm (@finset.sum_sdiff α β fs.support S fs.to_fun _ _ hS),
apply eq.trans,
from h₄,
have h₅ : sum (S \ fs.support) (fs.to_fun) = 0,
apply @finset.sum_eq_zero α β _ _ fs.to_fun (S \ fs.support),
intros x,
have h₆, from iff.elim_left ((@finsupp.not_mem_support_iff α β _ fs) x),
-- simp at h₆,
intros h₈,
apply h₆,
intros h₉,
simp at h₈,
simp at h₉,
apply h₈.elim_right,
from h₉,
-- rw h₇,
rw h₅,
simp,
end
end
lemma finset.sum_ite_zero {α : Type*} [fintype α] [decidable_eq α] (a₀ : α) {β : Type*} [add_comm_monoid β] [decidable_eq β] (f : α → β):
finset.sum finset.univ (λ a, ite (a₀ = a) (f a) 0) = f a₀ := begin
-- TODO update: cases have been moved earlier.
-- Outline of proof: ⊢ finset.sum finset.univ (λ (a : α), ite (a₀ = a) (f a) 0) = f a₀
/-
Two cases, h : (f a₀ = 0):
Case 1: If (f a₀ = 0) :
The goal is:
⊢ finset.sum finset.univ (λ (a : α), ite (a₀ = a) (f a) 0) = 0
We may show that (λ (a : α), ite (a₀ = a) (f a) 0) = (λ (a : α), 0) by cases.
⊢ finset.sum finset.univ (λ (a : α), 0) = 0
which is resolved by the fact that summation over constant zero is zero.
Case 2: If (f a₀ ≠ 0) :
Then (λ (a : α), ite (a₀ = a) (f a) 0) is a finitely-supported (single) function : α →₀ β
We then have
⊢ finset.sum finset.univ ((finsupp.single a₀ (f a₀)).to_fun) = f a₀
Then summing over the entire finset with a single-supported function is the same
as summing the result of the function mapped over the support of that function, so
⊢ finsupp.sum (finsupp.single a₀ (f a₀)) (λ (x : α) (y : β), y) = f a₀
Then we may apply the fact that summing over a (single) supported function just picks the value at the point of interest, so
⊢ (λ (x : α) (y : β), y) a₀ (f a₀) = f a₀
which is resolved by refl
-/
-- by_cases h : (f a₀ ≠ 0),
-- Case 1: --------------------------------
rw finsupp_of_ite_eq_ite,
rw finsupp_of_ite_single,
apply eq.trans (@finsupp_sum_support_subset α _ _ β _ (finsupp.single a₀ (f a₀)) finset.univ (by apply finset.subset_univ)),
-- from H₃,
-- show f a₀ ≠ 0, by assumption,
apply eq.trans (@finsupp.sum_single_index α β β _ _ _ _ a₀ (f a₀) (λ x y, y) (by refl)),
-- from @finsupp.sum_single_index α β β _ _ _ _ a₀ (f a₀) (λ x y, y) (by refl),
simp,
-- Case 2: --------------------------------
-- simp at h,
-- simp[h],
-- have H₅ : (λ (a : α), ite (a₀ = a) (f a) 0) = (λ (a : α), 0),
-- funext,
-- by_cases h₁ : (a₀ = a); simp[h₁],
-- subst h₁, assumption,
-- rw H₅,
-- from @finset.sum_const_zero α β finset.univ _,
end
-- set_option pp.proofs true
-- lemma finset.sum_ite : finset.sum X (λ a, ite (a₀ = a) (f a) (g a)) = ite (a₀ ∈ X) (f a₀) 0 + finset.sum (sorry) g
lemma finset.sum_ite_zero₂ {α : Type*} [fintype α] [decidable_eq α] (a₀ a₁ : α) {β : Type*} [add_comm_monoid β] [decidable_eq β] (f g : α → β) (h_ne : a₀ ≠ a₁):
finset.sum finset.univ (λ a, ite (a₀ = a) (f a) (ite (a₁ = a) (g a) 0)) = f a₀ + g a₁ := begin
by_cases h₁ : (g a₁ = 0),
-- Case 1:
have H₁ : (λ (a : α), ite (a₀ = a) (f a) (ite (a₁ = a) (g a) 0)) = (λ (a : α), ite (a₀ = a) (f a) 0),
funext,
by_cases h_t : (a₀ = a); simp[h_t],
by_cases h_t₀ : (a₁ = a), {subst h_t₀, simp[h_t, h₁]}, simp[h_t₀],
rw H₁,
rw finset.sum_ite_zero,
simp[h₁],
-- Case 2:
by_cases h₂ : (f a₀ = 0),
have H₁ : (λ (a : α), ite (a₀ = a) (f a) (ite (a₁ = a) (g a) 0)) = (λ (a : α), ite (a₁ = a) (g a) 0),
funext,
by_cases h_t : (a₀ = a); simp[h_t],
by_cases h_t₀ : (a₁ = a),
subst h_t₀,
exfalso, from h_ne h_t,
simp[h_t₀],
subst h_t,
from h₂,
rw H₁,
-- rw h₂,
rw finset.sum_ite_zero,
simp[h₂],
-- Case 3:
rw finsupp_of_double_ite_eq_double_ite,
rw finsupp_of_double_ite_zip_singles,
repeat{show _ ≠ _, by assumption},
apply eq.trans (@finsupp_sum_support_subset α _ _ β _ (finsupp.single a₀ (f a₀) + finsupp.single a₁ (g a₁)) finset.univ (by apply finset.subset_univ)),
unfold finsupp.sum,
have H₄ : disjoint ((finsupp.single a₀ (f a₀)).support) ((finsupp.single a₁ (g a₁)).support),
dsimp[finsupp.single],
-- simp[finsupp.single],
-- simp[finsupp.single],
simp[h₁, h₂],
from λ x, h_ne (eq.symm x),
have H₂, from @finsupp.support_add_eq α β _ _ _ (finsupp.single a₀ (f a₀)) (finsupp.single a₁ (g a₁)) H₄,
have H₃ : finset.sum ((finsupp.single a₀ (f a₀) + finsupp.single a₁ (g a₁)).support)
(λ (a : α), (finsupp.single a₀ (f a₀) + finsupp.single a₁ (g a₁)).to_fun a) = finset.sum (((finsupp.single a₀ (f a₀)).support) ∪ ((finsupp.single a₁ (g a₁)).support))
(λ (a : α), (finsupp.single a₀ (f a₀) + finsupp.single a₁ (g a₁)).to_fun a),
simp,
congr,
from H₂,
simp,
rw H₃,
clear H₃ H₂,
have H₅, from @finset.sum_union α β (finsupp.single a₀ (f a₀)).support (finsupp.single a₁ (g a₁)).support (λ (a : α), (finsupp.single a₀ (f a₀) + finsupp.single a₁ (g a₁)).to_fun a) _ _ ((iff.elim_left finset.disjoint_iff_inter_eq_empty) H₄),
apply eq.trans,
from H₅,
clear H₅,
-- have H₁ : finset.sum ((finsupp.single a₀ (f a₀)).support)
-- (λ (a : α), (finsupp.single a₀ (f a₀) + finsupp.single a₁ (g a₁)).to_fun a) = f a₀,
have H₄, from @finsupp.add_apply α β _ _ _ (finsupp.single a₀ (f a₀)) (finsupp.single a₁ (g a₁)),
rw ←coe_to_fun_to_fun,
have H₅ :
(λ (x : α), (finsupp.single a₀ (f a₀) + finsupp.single a₁ (g a₁)).to_fun x) =
(λ (x : α), (finsupp.single a₀ (f a₀)).to_fun x + (finsupp.single a₁ (g a₁)).to_fun x),
funext,
have H₄_temp, from @H₄ x,
simp at H₄_temp,
from H₄_temp,
simp,
rw H₅,
clear H₄,
have H₁ : finset.sum ((finsupp.single a₀ (f a₀)).support)
(λ (x : α), (finsupp.single a₀ (f a₀)).to_fun x + (finsupp.single a₁ (g a₁)).to_fun x) = f a₀,
-- simp,
dsimp[finsupp.single],
simp[h₁, h₂, λ x, h_ne (eq.symm x)],
simp[H₁],
clear H₁,
have H₁ : finset.sum ((finsupp.single a₁ (g a₁)).support)
(λ (x : α), (finsupp.single a₀ (f a₀)).to_fun x + (finsupp.single a₁ (g a₁)).to_fun x) = g a₁,
dsimp[finsupp.single],
simp[h₁, h₂, h_ne],
simp[H₁],
end
|
bf43f749ff4e12c0dabbd83c6c26792636e6c4a2 | b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77 | /src/algebra/group_with_zero/defs.lean | 943f63e50c16df0f938ee597529cce1d1cc0b4ac | [
"Apache-2.0"
] | permissive | molodiuc/mathlib | cae2ba3ef1601c1f42ca0b625c79b061b63fef5b | 98ebe5a6739fbe254f9ee9d401882d4388f91035 | refs/heads/master | 1,674,237,127,059 | 1,606,353,533,000 | 1,606,353,533,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,305 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import algebra.group.defs
import logic.nontrivial
/-!
# Typeclasses for groups with an adjoined zero element
This file provides just the typeclass definitions, and the projection lemmas that expose their
members.
## Main definitions
* `group_with_zero`
* `comm_group_with_zero`
-/
set_option old_structure_cmd true
variables {M₀ G₀ M₀' G₀' : Type*}
section
/-- Typeclass for expressing that a type `M₀` with multiplication and a zero satisfies
`0 * a = 0` and `a * 0 = 0` for all `a : M₀`. -/
@[protect_proj, ancestor has_mul has_zero]
class mul_zero_class (M₀ : Type*) extends has_mul M₀, has_zero M₀ :=
(zero_mul : ∀ a : M₀, 0 * a = 0)
(mul_zero : ∀ a : M₀, a * 0 = 0)
section mul_zero_class
variables [mul_zero_class M₀] {a b : M₀}
@[ematch, simp] lemma zero_mul (a : M₀) : 0 * a = 0 :=
mul_zero_class.zero_mul a
@[ematch, simp] lemma mul_zero (a : M₀) : a * 0 = 0 :=
mul_zero_class.mul_zero a
end mul_zero_class
/-- Predicate typeclass for expressing that `a * b = 0` implies `a = 0` or `b = 0`
for all `a` and `b` of type `G₀`. -/
class no_zero_divisors (M₀ : Type*) [has_mul M₀] [has_zero M₀] : Prop :=
(eq_zero_or_eq_zero_of_mul_eq_zero : ∀ {a b : M₀}, a * b = 0 → a = 0 ∨ b = 0)
export no_zero_divisors (eq_zero_or_eq_zero_of_mul_eq_zero)
/-- A type `M` is a “monoid with zero” if it is a monoid with zero element, and `0` is left
and right absorbing. -/
@[protect_proj] class monoid_with_zero (M₀ : Type*) extends monoid M₀, mul_zero_class M₀.
/-- A type `M` is a `cancel_monoid_with_zero` if it is a monoid with zero element, `0` is left
and right absorbing, and left/right multiplication by a non-zero element is injective. -/
@[protect_proj] class cancel_monoid_with_zero (M₀ : Type*) extends monoid_with_zero M₀ :=
(mul_left_cancel_of_ne_zero : ∀ {a b c : M₀}, a ≠ 0 → a * b = a * c → b = c)
(mul_right_cancel_of_ne_zero : ∀ {a b c : M₀}, b ≠ 0 → a * b = c * b → a = c)
section cancel_monoid_with_zero
variables [cancel_monoid_with_zero M₀] {a b c : M₀}
lemma mul_left_cancel' (ha : a ≠ 0) (h : a * b = a * c) : b = c :=
cancel_monoid_with_zero.mul_left_cancel_of_ne_zero ha h
lemma mul_right_cancel' (hb : b ≠ 0) (h : a * b = c * b) : a = c :=
cancel_monoid_with_zero.mul_right_cancel_of_ne_zero hb h
end cancel_monoid_with_zero
/-- A type `M` is a commutative “monoid with zero” if it is a commutative monoid with zero
element, and `0` is left and right absorbing. -/
@[protect_proj]
class comm_monoid_with_zero (M₀ : Type*) extends comm_monoid M₀, monoid_with_zero M₀.
/-- A type `M` is a `comm_cancel_monoid_with_zero` if it is a commutative monoid with zero element,
`0` is left and right absorbing,
and left/right multiplication by a non-zero element is injective. -/
@[protect_proj] class comm_cancel_monoid_with_zero (M₀ : Type*) extends
comm_monoid_with_zero M₀, cancel_monoid_with_zero M₀.
/-- A type `G₀` is a “group with zero” if it is a monoid with zero element (distinct from `1`)
such that every nonzero element is invertible.
The type is required to come with an “inverse” function, and the inverse of `0` must be `0`.
Examples include division rings and the ordered monoids that are the
target of valuations in general valuation theory.-/
class group_with_zero (G₀ : Type*) extends monoid_with_zero G₀, has_inv G₀, nontrivial G₀ :=
(inv_zero : (0 : G₀)⁻¹ = 0)
(mul_inv_cancel : ∀ a:G₀, a ≠ 0 → a * a⁻¹ = 1)
section group_with_zero
variables [group_with_zero G₀]
@[simp] lemma inv_zero : (0 : G₀)⁻¹ = 0 :=
group_with_zero.inv_zero
@[simp] lemma mul_inv_cancel {a : G₀} (h : a ≠ 0) : a * a⁻¹ = 1 :=
group_with_zero.mul_inv_cancel a h
end group_with_zero
/-- A type `G₀` is a commutative “group with zero”
if it is a commutative monoid with zero element (distinct from `1`)
such that every nonzero element is invertible.
The type is required to come with an “inverse” function, and the inverse of `0` must be `0`. -/
class comm_group_with_zero (G₀ : Type*) extends comm_monoid_with_zero G₀, group_with_zero G₀.
end
|
8e959833de575da17b3075789c0dc7cb10778ee0 | 957a80ea22c5abb4f4670b250d55534d9db99108 | /library/init/meta/backward.lean | 8781719616decdd292dc913737f445d54258cdb7 | [
"Apache-2.0"
] | permissive | GaloisInc/lean | aa1e64d604051e602fcf4610061314b9a37ab8cd | f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0 | refs/heads/master | 1,592,202,909,807 | 1,504,624,387,000 | 1,504,624,387,000 | 75,319,626 | 2 | 1 | Apache-2.0 | 1,539,290,164,000 | 1,480,616,104,000 | C++ | UTF-8 | Lean | false | false | 3,018 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.meta.tactic init.meta.set_get_option_tactics
namespace tactic
meta constant back_lemmas : Type
/- Create a datastructure containing all lemmas tagged as [intro].
Lemmas are indexed using their head-symbol.
The head-symbol is computed with respect to the given transparency setting. -/
meta constant mk_back_lemmas_core : transparency → tactic back_lemmas
/- (back_lemmas_insert_core m lemmas lemma) adds the given lemma to the set back_lemmas.
It infers the type of the lemma, and uses its head-symbol as an index.
The head-symbol is computed with respect to the given transparency setting. -/
meta constant back_lemmas_insert_core : transparency → back_lemmas → expr → tactic back_lemmas
/- Return the lemmas that have the same head symbol of the given expression -/
meta constant back_lemmas_find : back_lemmas → expr → tactic (list expr)
meta def mk_back_lemmas : tactic back_lemmas :=
mk_back_lemmas_core reducible
meta def back_lemmas_insert : back_lemmas → expr → tactic back_lemmas :=
back_lemmas_insert_core reducible
/- (backward_chaining_core t insts max_depth pre_tactic leaf_tactic lemmas): perform backward chaining using
the lemmas marked as [intro] and extra_lemmas.
The search maximum depth is \c max_depth.
Before processing each goal, the tactic pre_tactic is invoked. The possible outcomes are:
1) it closes the goal
2) it does nothing, and backward_chaining_core tries applicable lemmas.
3) it fails, and backward_chaining_core backtracks.
Whenever no lemma is applicable, the leaf_tactic is invoked, to try to close the goal.
If insts is tt, then type class resolution is used to discharge goals.
Remark pre_tactic may also be used to trace the execution of backward_chaining_core -/
meta constant backward_chaining_core : transparency → bool → nat → tactic unit → tactic unit → back_lemmas → tactic unit
meta def back_lemmas_add_extra : transparency → back_lemmas → list expr → tactic back_lemmas
| m bls [] := return bls
| m bls (l::ls) := do
new_bls ← back_lemmas_insert_core m bls l,
back_lemmas_add_extra m new_bls ls
meta def back_chaining_core (pre_tactic : tactic unit) (leaf_tactic : tactic unit) (extra_lemmas : list expr) : tactic unit :=
do intro_lemmas ← mk_back_lemmas_core reducible,
new_lemmas ← back_lemmas_add_extra reducible intro_lemmas extra_lemmas,
max ← get_nat_option `back_chaining.max_depth 8,
backward_chaining_core reducible tt max pre_tactic leaf_tactic new_lemmas
meta def back_chaining : tactic unit :=
back_chaining_core skip assumption []
meta def back_chaining_using : list expr → tactic unit :=
back_chaining_core skip assumption
meta def back_chaining_using_hs : tactic unit :=
local_context >>= back_chaining_core skip failed
end tactic
|
b5bcc36e7c5b4bf9dc7bb7198bd9bb4fca9e0c20 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/topology/instances/rat.lean | 766fc107b595d13a90f4a7860e63a24fb9c7fd32 | [
"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 | 4,063 | 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 topology.metric_space.basic
import topology.algebra.order.archimedean
import topology.instances.int
import topology.instances.nat
import topology.instances.real
/-!
# Topology on the ratonal numbers
The structure of a metric space on `ℚ` is introduced in this file, induced from `ℝ`.
-/
open metric set filter
namespace rat
noncomputable instance : metric_space ℚ :=
metric_space.induced coe rat.cast_injective real.metric_space
theorem dist_eq (x y : ℚ) : dist x y = |x - y| := rfl
@[norm_cast, simp] lemma dist_cast (x y : ℚ) : dist (x : ℝ) y = dist x y := rfl
theorem uniform_continuous_coe_real : uniform_continuous (coe : ℚ → ℝ) :=
uniform_continuous_comap
theorem uniform_embedding_coe_real : uniform_embedding (coe : ℚ → ℝ) :=
uniform_embedding_comap rat.cast_injective
theorem dense_embedding_coe_real : dense_embedding (coe : ℚ → ℝ) :=
uniform_embedding_coe_real.dense_embedding rat.dense_range_cast
theorem embedding_coe_real : embedding (coe : ℚ → ℝ) := dense_embedding_coe_real.to_embedding
theorem continuous_coe_real : continuous (coe : ℚ → ℝ) := uniform_continuous_coe_real.continuous
end rat
@[norm_cast, simp] theorem nat.dist_cast_rat (x y : ℕ) : dist (x : ℚ) y = dist x y :=
by rw [← nat.dist_cast_real, ← rat.dist_cast]; congr' 1; norm_cast
lemma nat.uniform_embedding_coe_rat : uniform_embedding (coe : ℕ → ℚ) :=
uniform_embedding_bot_of_pairwise_le_dist zero_lt_one $ by simpa using nat.pairwise_one_le_dist
lemma nat.closed_embedding_coe_rat : closed_embedding (coe : ℕ → ℚ) :=
closed_embedding_of_pairwise_le_dist zero_lt_one $ by simpa using nat.pairwise_one_le_dist
@[norm_cast, simp] theorem int.dist_cast_rat (x y : ℤ) : dist (x : ℚ) y = dist x y :=
by rw [← int.dist_cast_real, ← rat.dist_cast]; congr' 1; norm_cast
lemma int.uniform_embedding_coe_rat : uniform_embedding (coe : ℤ → ℚ) :=
uniform_embedding_bot_of_pairwise_le_dist zero_lt_one $ by simpa using int.pairwise_one_le_dist
lemma int.closed_embedding_coe_rat : closed_embedding (coe : ℤ → ℚ) :=
closed_embedding_of_pairwise_le_dist zero_lt_one $ by simpa using int.pairwise_one_le_dist
namespace rat
instance : noncompact_space ℚ := int.closed_embedding_coe_rat.noncompact_space
-- TODO(Mario): Find a way to use rat_add_continuous_lemma
theorem uniform_continuous_add : uniform_continuous (λp : ℚ × ℚ, p.1 + p.2) :=
rat.uniform_embedding_coe_real.to_uniform_inducing.uniform_continuous_iff.2 $
by simp only [(∘), rat.cast_add]; exact real.uniform_continuous_add.comp
(rat.uniform_continuous_coe_real.prod_map rat.uniform_continuous_coe_real)
theorem uniform_continuous_neg : uniform_continuous (@has_neg.neg ℚ _) :=
metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h,
by rw dist_comm at h; simpa [rat.dist_eq] using h⟩
instance : uniform_add_group ℚ :=
uniform_add_group.mk' rat.uniform_continuous_add rat.uniform_continuous_neg
instance : topological_add_group ℚ := by apply_instance
instance : order_topology ℚ :=
induced_order_topology _ (λ x y, rat.cast_lt) (@exists_rat_btwn _ _ _)
lemma uniform_continuous_abs : uniform_continuous (abs : ℚ → ℚ) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
⟨ε, ε0, λ a b h, lt_of_le_of_lt
(by simpa [rat.dist_eq] using abs_abs_sub_abs_le_abs_sub _ _) h⟩
lemma continuous_mul : continuous (λp : ℚ × ℚ, p.1 * p.2) :=
rat.embedding_coe_real.continuous_iff.2 $ by simp [(∘)]; exact
real.continuous_mul.comp ((rat.continuous_coe_real.prod_map rat.continuous_coe_real))
instance : topological_ring ℚ :=
{ continuous_mul := rat.continuous_mul, ..rat.topological_add_group }
lemma totally_bounded_Icc (a b : ℚ) : totally_bounded (Icc a b) :=
by simpa only [preimage_cast_Icc]
using totally_bounded_preimage rat.uniform_embedding_coe_real (totally_bounded_Icc a b)
end rat
|
63202ae15e260940ac63f8c503eba94f80515dbe | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/set/intervals/infinite.lean | cdca43d3557a78957c0a7add1b2b8a7b9547013e | [
"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 | 1,883 | lean | /-
Copyright (c) 2020 Reid Barton. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Reid Barton
-/
import data.set.finite
/-!
# Infinitude of intervals
Bounded intervals in dense orders are infinite, as are unbounded intervals
in orders that are unbounded on the appropriate side.
-/
namespace set
variables {α : Type*} [preorder α]
section bounded
variables [densely_ordered α]
lemma Ioo.infinite {a b : α} (h : a < b) : infinite (Ioo a b) :=
begin
rintro (f : finite (Ioo a b)),
obtain ⟨m, hm₁, hm₂⟩ : ∃ m ∈ Ioo a b, ∀ x ∈ Ioo a b, ¬x < m,
{ simpa [h] using finset.exists_minimal f.to_finset },
obtain ⟨z, hz₁, hz₂⟩ : ∃ z, a < z ∧ z < m := exists_between hm₁.1,
exact hm₂ z ⟨hz₁, lt_trans hz₂ hm₁.2⟩ hz₂,
end
lemma Ico.infinite {a b : α} (h : a < b) : infinite (Ico a b) :=
(Ioo.infinite h).mono Ioo_subset_Ico_self
lemma Ioc.infinite {a b : α} (h : a < b) : infinite (Ioc a b) :=
(Ioo.infinite h).mono Ioo_subset_Ioc_self
lemma Icc.infinite {a b : α} (h : a < b) : infinite (Icc a b) :=
(Ioo.infinite h).mono Ioo_subset_Icc_self
end bounded
section unbounded_below
variables [no_min_order α]
lemma Iio.infinite {b : α} : infinite (Iio b) :=
begin
rintro (f : finite (Iio b)),
obtain ⟨m, hm₁, hm₂⟩ : ∃ m < b, ∀ x < b, ¬x < m,
{ simpa using finset.exists_minimal f.to_finset },
obtain ⟨z, hz⟩ : ∃ z, z < m := exists_lt _,
exact hm₂ z (lt_trans hz hm₁) hz
end
lemma Iic.infinite {b : α} : infinite (Iic b) :=
Iio.infinite.mono Iio_subset_Iic_self
end unbounded_below
section unbounded_above
variables [no_max_order α]
lemma Ioi.infinite {a : α} : infinite (Ioi a) := @Iio.infinite αᵒᵈ _ _ _
lemma Ici.infinite {a : α} : infinite (Ici a) :=
Ioi.infinite.mono Ioi_subset_Ici_self
end unbounded_above
end set
|
8e250ec1d34b9af7942a7d28f86af30e77f91253 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/algebraic_topology/simplex_category.lean | 984d5cd0b9f3acfe44bcf84e4173941b24ee9cf7 | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 17,695 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Scott Morrison, Adam Topaz
-/
import order.category.NonemptyFinLinOrd
import category_theory.skeletal
import data.finset.sort
import tactic.linarith
/-! # The simplex category
We construct a skeletal model of the simplex category, with objects `ℕ` and the
morphism `n ⟶ m` being the monotone maps from `fin (n+1)` to `fin (m+1)`.
We show that this category is equivalent to `NonemptyFinLinOrd`.
## Remarks
The definitions `simplex_category` and `simplex_category.hom` are marked as irreducible.
We provide the following functions to work with these objects:
1. `simplex_category.mk` creates an object of `simplex_category` out of a natural number.
Use the notation `[n]` in the `simplicial` locale.
2. `simplex_category.len` gives the "length" of an object of `simplex_category`, as a natural.
3. `simplex_category.hom.mk` makes a morphism out of a monotone map between `fin`'s.
4. `simplex_category.hom.to_preorder_hom` gives the underlying monotone map associated to a
term of `simplex_category.hom`.
-/
universes u v
open category_theory
/-- The simplex category:
* objects are natural numbers `n : ℕ`
* morphisms from `n` to `m` are monotone functions `fin (n+1) → fin (m+1)`
-/
@[derive inhabited, irreducible]
def simplex_category := ulift.{u} ℕ
namespace simplex_category
section
local attribute [semireducible] simplex_category
-- TODO: Make `mk` irreducible.
/-- Interpet a natural number as an object of the simplex category. -/
def mk (n : ℕ) : simplex_category.{u} := ulift.up n
localized "notation `[`n`]` := simplex_category.mk n" in simplicial
-- TODO: Make `len` irreducible.
/-- The length of an object of `simplex_category`. -/
def len (n : simplex_category.{u}) : ℕ := n.down
@[ext] lemma ext (a b : simplex_category.{u}) : a.len = b.len → a = b := ulift.ext a b
@[simp] lemma len_mk (n : ℕ) : [n].len = n := rfl
@[simp] lemma mk_len (n : simplex_category.{u}) : [n.len] = n := by {cases n, refl}
/-- Morphisms in the simplex_category. -/
@[irreducible, nolint has_inhabited_instance]
protected def hom (a b : simplex_category.{u}) : Type u :=
ulift (fin (a.len + 1) →ₘ fin (b.len + 1))
namespace hom
local attribute [semireducible] simplex_category.hom
/-- Make a moprhism in `simplex_category` from a monotone map of fin's. -/
def mk {a b : simplex_category.{u}} (f : fin (a.len + 1) →ₘ fin (b.len + 1)) :
simplex_category.hom a b :=
ulift.up f
/-- Recover the monotone map from a morphism in the simplex category. -/
def to_preorder_hom {a b : simplex_category.{u}} (f : simplex_category.hom a b) :
fin (a.len + 1) →ₘ fin (b.len + 1) :=
ulift.down f
@[ext] lemma ext {a b : simplex_category.{u}} (f g : simplex_category.hom a b) :
f.to_preorder_hom = g.to_preorder_hom → f = g := ulift.ext _ _
@[simp] lemma mk_to_preorder_hom {a b : simplex_category.{u}}
(f : simplex_category.hom a b) : mk (f.to_preorder_hom) = f :=
by {cases f, refl}
@[simp] lemma to_preorder_hom_mk {a b : simplex_category.{u}}
(f : fin (a.len + 1) →ₘ fin (b.len + 1)) : (mk f).to_preorder_hom = f :=
by simp [to_preorder_hom, mk]
lemma mk_to_preorder_hom_apply {a b : simplex_category.{u}}
(f : fin (a.len + 1) →ₘ fin (b.len + 1)) (i : fin (a.len + 1)) :
(mk f).to_preorder_hom i = f i := rfl
/-- Identity morphisms of `simplex_category`. -/
@[simp]
def id (a : simplex_category.{u}) :
simplex_category.hom a a :=
mk preorder_hom.id
/-- Composition of morphisms of `simplex_category`. -/
@[simp]
def comp {a b c : simplex_category.{u}} (f : simplex_category.hom b c)
(g : simplex_category.hom a b) : simplex_category.hom a c :=
mk $ f.to_preorder_hom.comp g.to_preorder_hom
end hom
@[simps]
instance small_category : small_category.{u} simplex_category :=
{ hom := λ n m, simplex_category.hom n m,
id := λ m, simplex_category.hom.id _,
comp := λ _ _ _ f g, simplex_category.hom.comp g f, }
/-- The constant morphism from [0]. -/
def const (x : simplex_category.{u}) (i : fin (x.len+1)) : [0] ⟶ x :=
hom.mk $ ⟨λ _, i, by tauto⟩
@[simp]
lemma const_comp (x y : simplex_category.{u}) (i : fin (x.len + 1)) (f : x ⟶ y) :
const x i ≫ f = const y (f.to_preorder_hom i) := rfl
/--
Make a morphism `[n] ⟶ [m]` from a monotone map between fin's.
This is useful for constructing morphisms beetween `[n]` directly
without identifying `n` with `[n].len`.
-/
@[simp]
def mk_hom {n m : ℕ} (f : (fin (n+1)) →ₘ (fin (m+1))) : [n] ⟶ [m] :=
simplex_category.hom.mk f
lemma hom_zero_zero (f : [0] ⟶ [0]) : f = 𝟙 _ :=
by { ext : 2, dsimp, apply subsingleton.elim }
end
open_locale simplicial
section generators
/-!
## Generating maps for the simplex category
TODO: prove that the simplex category is equivalent to
one given by the following generators and relations.
-/
/-- The `i`-th face map from `[n]` to `[n+1]` -/
def δ {n} (i : fin (n+2)) : [n] ⟶ [n+1] :=
mk_hom (fin.succ_above i).to_preorder_hom
/-- The `i`-th degeneracy map from `[n+1]` to `[n]` -/
def σ {n} (i : fin (n+1)) : [n+1] ⟶ [n] := mk_hom
{ to_fun := fin.pred_above i,
monotone' := fin.pred_above_right_monotone i }
/-- The generic case of the first simplicial identity -/
lemma δ_comp_δ {n} {i j : fin (n+2)} (H : i ≤ j) :
δ i ≫ δ j.succ = δ j ≫ δ i.cast_succ :=
begin
ext k,
dsimp [δ, fin.succ_above],
simp only [order_embedding.to_preorder_hom_coe,
order_embedding.coe_of_strict_mono,
function.comp_app,
simplex_category.hom.to_preorder_hom_mk,
preorder_hom.comp_coe],
rcases i with ⟨i, _⟩,
rcases j with ⟨j, _⟩,
rcases k with ⟨k, _⟩,
split_ifs; { simp at *; linarith },
end
/-- The special case of the first simplicial identity -/
lemma δ_comp_δ_self {n} {i : fin (n+2)} : δ i ≫ δ i.cast_succ = δ i ≫ δ i.succ :=
(δ_comp_δ (le_refl i)).symm
/-- The second simplicial identity -/
lemma δ_comp_σ_of_le {n} {i : fin (n+2)} {j : fin (n+1)} (H : i ≤ j.cast_succ) :
δ i.cast_succ ≫ σ j.succ = σ j ≫ δ i :=
begin
ext k,
suffices : ite (j.succ.cast_succ < ite (k < i) k.cast_succ k.succ)
(ite (k < i) (k:ℕ) (k + 1) - 1) (ite (k < i) k (k + 1)) =
ite ((if h : (j:ℕ) < k
then k.pred (by { rintro rfl, exact nat.not_lt_zero _ h })
else k.cast_lt (by { cases j, cases k, simp only [len_mk], linarith })).cast_succ < i)
(ite (j.cast_succ < k) (k - 1) k) (ite (j.cast_succ < k) (k - 1) k + 1),
{ dsimp [δ, σ, fin.succ_above, fin.pred_above],
simpa [fin.pred_above] with push_cast },
rcases i with ⟨i, _⟩,
rcases j with ⟨j, _⟩,
rcases k with ⟨k, _⟩,
simp only [subtype.mk_le_mk, fin.cast_succ_mk] at H,
dsimp, simp only [if_congr, subtype.mk_lt_mk, dif_ctx_congr],
split_ifs,
-- Most of the goals can now be handled by `linarith`,
-- but we have to deal with two of them by hand.
swap 8,
{ exact (nat.succ_pred_eq_of_pos (lt_of_le_of_lt (zero_le _) ‹_›)).symm, },
swap 7,
{ have : k ≤ i := nat.le_of_pred_lt ‹_›, linarith, },
-- Hope for the best from `linarith`:
all_goals { try { refl <|> simp at * }; linarith, },
end
/-- The first part of the third simplicial identity -/
lemma δ_comp_σ_self {n} {i : fin (n+1)} :
δ i.cast_succ ≫ σ i = 𝟙 [n] :=
begin
ext j,
suffices : ite (fin.cast_succ i < ite (j < i) (fin.cast_succ j) j.succ)
(ite (j < i) (j:ℕ) (j + 1) - 1) (ite (j < i) j (j + 1)) = j,
{ dsimp [δ, σ, fin.succ_above, fin.pred_above], simpa [fin.pred_above] with push_cast },
rcases i with ⟨i, _⟩,
rcases j with ⟨j, _⟩,
dsimp, simp only [if_congr, subtype.mk_lt_mk],
split_ifs; { simp at *; linarith, },
end
/-- The second part of the third simplicial identity -/
lemma δ_comp_σ_succ {n} {i : fin (n+1)} :
δ i.succ ≫ σ i = 𝟙 [n] :=
begin
ext j,
rcases i with ⟨i, _⟩,
rcases j with ⟨j, _⟩,
dsimp [δ, σ, fin.succ_above, fin.pred_above],
simp [fin.pred_above] with push_cast,
split_ifs; { simp at *; linarith, },
end
/-- The fourth simplicial identity -/
lemma δ_comp_σ_of_gt {n} {i : fin (n+2)} {j : fin (n+1)} (H : j.cast_succ < i) :
δ i.succ ≫ σ j.cast_succ = σ j ≫ δ i :=
begin
ext k,
dsimp [δ, σ, fin.succ_above, fin.pred_above],
rcases i with ⟨i, _⟩,
rcases j with ⟨j, _⟩,
rcases k with ⟨k, _⟩,
simp only [subtype.mk_lt_mk, fin.cast_succ_mk] at H,
suffices : ite (_ < ite (k < i + 1) _ _) _ _ =
ite _ (ite (j < k) (k - 1) k) (ite (j < k) (k - 1) k + 1),
{ simpa [apply_dite fin.cast_succ, fin.pred_above] with push_cast, },
split_ifs,
-- Most of the goals can now be handled by `linarith`,
-- but we have to deal with three of them by hand.
swap 2,
{ simp only [subtype.mk_lt_mk] at h_1,
simp only [not_lt] at h_2,
simp only [self_eq_add_right, one_ne_zero],
exact lt_irrefl (k - 1) (lt_of_lt_of_le
(nat.pred_lt (ne_of_lt (lt_of_le_of_lt (zero_le _) h_1)).symm)
(le_trans (nat.le_of_lt_succ h) h_2)) },
swap 4,
{ simp only [subtype.mk_lt_mk] at h_1,
simp only [not_lt] at h,
simp only [nat.add_succ_sub_one, add_zero],
exfalso,
exact lt_irrefl _ (lt_of_le_of_lt (nat.le_pred_of_lt (nat.lt_of_succ_le h)) h_3), },
swap 4,
{ simp only [subtype.mk_lt_mk] at h_1,
simp only [not_lt] at h_3,
simp only [nat.add_succ_sub_one, add_zero],
exact (nat.succ_pred_eq_of_pos (lt_of_le_of_lt (zero_le _) h_2)).symm, },
-- Hope for the best from `linarith`:
all_goals { simp at h_1 h_2 ⊢; linarith, },
end
local attribute [simp] fin.pred_mk
/-- The fifth simplicial identity -/
lemma σ_comp_σ {n} {i j : fin (n+1)} (H : i ≤ j) :
σ i.cast_succ ≫ σ j = σ j.succ ≫ σ i :=
begin
ext k,
dsimp [σ, fin.pred_above],
rcases i with ⟨i, _⟩,
rcases j with ⟨j, _⟩,
rcases k with ⟨k, _⟩,
simp only [subtype.mk_le_mk] at H,
-- At this point `simp with push_cast` makes good progress, but neither `simp?` nor `squeeze_simp`
-- return usable sets of lemmas.
-- To avoid using a non-terminal simp, we make a `suffices` statement indicating the shape
-- of the goal we're looking for, and then use `simpa with push_cast`.
-- I'm not sure this is actually much more robust that a non-terminal simp.
suffices : ite (_ < dite (i < k) _ _) _ _ =
ite (_ < dite (j + 1 < k) _ _) _ _,
{ simpa [fin.pred_above] with push_cast, },
split_ifs,
-- `split_ifs` created 12 goals.
-- Most of them are dealt with `by simp at *; linarith`,
-- but we pull out two harder ones to do by hand.
swap 3,
{ simp only [not_lt] at h_2,
exact false.elim
(lt_irrefl (k - 1)
(lt_of_lt_of_le (nat.pred_lt (id (ne_of_lt (lt_of_le_of_lt (zero_le i) h)).symm))
(le_trans h_2 (nat.succ_le_of_lt h_1)))) },
swap 3,
{ simp only [subtype.mk_lt_mk, not_lt] at h_1,
exact false.elim
(lt_irrefl j (lt_of_lt_of_le (nat.pred_lt_pred (nat.succ_ne_zero j) h_2) h_1)) },
-- Deal with the rest automatically.
all_goals { simp at *; linarith, },
end
end generators
section skeleton
/-- The functor that exhibits `simplex_category` as skeleton
of `NonemptyFinLinOrd` -/
@[simps obj map]
def skeletal_functor : simplex_category.{u} ⥤ NonemptyFinLinOrd.{v} :=
{ obj := λ a, NonemptyFinLinOrd.of $ ulift (fin (a.len + 1)),
map := λ a b f,
⟨λ i, ulift.up (f.to_preorder_hom i.down), λ i j h, f.to_preorder_hom.monotone h⟩,
map_id' := λ a, by { ext, simp, },
map_comp' := λ a b c f g, by { ext, simp, }, }
lemma skeletal : skeletal simplex_category.{u} :=
λ X Y ⟨I⟩,
begin
suffices : fintype.card (fin (X.len+1)) = fintype.card (fin (Y.len+1)),
{ ext, simpa },
{ apply fintype.card_congr,
refine equiv.ulift.symm.trans (((skeletal_functor ⋙ forget _).map_iso I).to_equiv.trans _),
apply equiv.ulift }
end
namespace skeletal_functor
instance : full skeletal_functor.{u v} :=
{ preimage := λ a b f, simplex_category.hom.mk ⟨λ i, (f (ulift.up i)).down, λ i j h, f.monotone h⟩,
witness' := by { intros m n f, dsimp at *, ext1 ⟨i⟩, ext1, ext1, cases x, simp, } }
instance : faithful skeletal_functor.{u v} :=
{ map_injective' := λ m n f g h,
begin
ext1, ext1, ext1 i, apply ulift.up.inj,
change (skeletal_functor.map f) ⟨i⟩ = (skeletal_functor.map g) ⟨i⟩,
rw h,
end }
instance : ess_surj skeletal_functor.{u v} :=
{ mem_ess_image := λ X, ⟨mk (fintype.card X - 1 : ℕ), ⟨begin
have aux : fintype.card X = fintype.card X - 1 + 1,
{ exact (nat.succ_pred_eq_of_pos $ fintype.card_pos_iff.mpr ⟨⊥⟩).symm, },
let f := mono_equiv_of_fin X aux,
have hf := (finset.univ.order_emb_of_fin aux).strict_mono,
refine
{ hom := ⟨λ i, f i.down, _⟩,
inv := ⟨λ i, ⟨f.symm i⟩, _⟩,
hom_inv_id' := _,
inv_hom_id' := _ },
{ rintro ⟨i⟩ ⟨j⟩ h, show f i ≤ f j, exact hf.monotone h, },
{ intros i j h, show f.symm i ≤ f.symm j, rw ← hf.le_iff_le,
show f (f.symm i) ≤ f (f.symm j), simpa only [order_iso.apply_symm_apply], },
{ ext1, ext1 ⟨i⟩, ext1, exact f.symm_apply_apply i },
{ ext1, ext1 i, exact f.apply_symm_apply i },
end⟩⟩, }
noncomputable instance is_equivalence : is_equivalence skeletal_functor.{u v} :=
equivalence.of_fully_faithfully_ess_surj skeletal_functor
end skeletal_functor
/-- The equivalence that exhibits `simplex_category` as skeleton
of `NonemptyFinLinOrd` -/
noncomputable def skeletal_equivalence : simplex_category.{u} ≌ NonemptyFinLinOrd.{v} :=
functor.as_equivalence skeletal_functor
end skeleton
/--
`simplex_category` is a skeleton of `NonemptyFinLinOrd`.
-/
noncomputable
def is_skeleton_of : is_skeleton_of NonemptyFinLinOrd simplex_category skeletal_functor.{u v} :=
{ skel := skeletal,
eqv := skeletal_functor.is_equivalence }
/-- The truncated simplex category. -/
@[derive small_category]
def truncated (n : ℕ) := {a : simplex_category.{u} // a.len ≤ n}
namespace truncated
instance {n} : inhabited (truncated n) := ⟨⟨[0],by simp⟩⟩
/--
The fully faithful inclusion of the truncated simplex category into the usual
simplex category.
-/
@[derive [full, faithful]]
def inclusion {n : ℕ} : simplex_category.truncated.{u} n ⥤ simplex_category.{u} :=
full_subcategory_inclusion _
end truncated
section concrete
instance : concrete_category.{0} simplex_category.{u} :=
{ forget :=
{ obj := λ i, fin (i.len + 1),
map := λ i j f, f.to_preorder_hom },
forget_faithful := {} }
end concrete
section epi_mono
/-- A morphism in `simplex_category` is a monomorphism precisely when it is an injective function
-/
theorem mono_iff_injective {n m : simplex_category.{u}} {f : n ⟶ m} :
mono f ↔ function.injective f.to_preorder_hom :=
begin
split,
{ introsI m x y h,
have H : const n x ≫ f = const n y ≫ f,
{ dsimp, rw h },
change (n.const x).to_preorder_hom 0 = (n.const y).to_preorder_hom 0,
rw cancel_mono f at H,
rw H },
{ exact concrete_category.mono_of_injective f }
end
/-- A morphism in `simplex_category` is an epimorphism if and only if it is a surjective function
-/
lemma epi_iff_surjective {n m : simplex_category.{u}} {f: n ⟶ m} :
epi f ↔ function.surjective f.to_preorder_hom :=
begin
split,
{ introsI hyp_f_epi x,
by_contradiction h_ab,
rw not_exists at h_ab,
-- The proof is by contradiction: assume f is not surjective,
-- then introduce two non-equal auxiliary functions equalizing f, and get a contradiction.
-- First we define the two auxiliary functions.
set chi_1 : m ⟶ [1] := hom.mk ⟨λ u, if u ≤ x then 0 else 1, begin
intros a b h,
dsimp only [],
split_ifs with h1 h2 h3,
any_goals { exact le_refl _ },
{ exact bot_le },
{ exact false.elim (h1 (le_trans h h3)) }
end ⟩,
set chi_2 : m ⟶ [1] := hom.mk ⟨λ u, if u < x then 0 else 1, begin
intros a b h,
dsimp only [],
split_ifs with h1 h2 h3,
any_goals { exact le_refl _ },
{ exact bot_le },
{ exact false.elim (h1 (lt_of_le_of_lt h h3)) }
end ⟩,
-- The two auxiliary functions equalize f
have f_comp_chi_i : f ≫ chi_1 = f ≫ chi_2,
{ dsimp,
ext,
simp [le_iff_lt_or_eq, h_ab x_1] },
-- We now just have to show the two auxiliary functions are not equal.
rw category_theory.cancel_epi f at f_comp_chi_i, rename f_comp_chi_i eq_chi_i,
apply_fun (λ e, e.to_preorder_hom x) at eq_chi_i,
suffices : (0 : fin 2) = 1, by exact bot_ne_top this,
simpa using eq_chi_i },
{ exact concrete_category.epi_of_surjective f }
end
/-- A monomorphism in `simplex_category` must increase lengths-/
lemma len_le_of_mono {x y : simplex_category.{u}} {f : x ⟶ y} :
mono f → (x.len ≤ y.len) :=
begin
intro hyp_f_mono,
have f_inj : function.injective f.to_preorder_hom.to_fun,
{ exact mono_iff_injective.elim_left (hyp_f_mono) },
simpa using fintype.card_le_of_injective f.to_preorder_hom.to_fun f_inj,
end
lemma le_of_mono {n m : ℕ} {f : [n] ⟶ [m]} : (category_theory.mono f) → (n ≤ m) :=
len_le_of_mono
/-- An epimorphism in `simplex_category` must decrease lengths-/
lemma len_le_of_epi {x y : simplex_category.{u}} {f : x ⟶ y} :
epi f → y.len ≤ x.len :=
begin
intro hyp_f_epi,
have f_surj : function.surjective f.to_preorder_hom.to_fun,
{ exact epi_iff_surjective.elim_left (hyp_f_epi) },
simpa using fintype.card_le_of_surjective f.to_preorder_hom.to_fun f_surj,
end
lemma le_of_epi {n m : ℕ} {f : [n] ⟶ [m]} : epi f → (m ≤ n) :=
len_le_of_epi
end epi_mono
end simplex_category
|
579f03d36c9cee67180960f7a080413777108911 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/cc1.lean | 2a393cd981790e7a41986ba5ba0e345f3efb46fd | [
"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 | 2,155 | lean | open tactic
set_option pp.implicit true
example (a b c d : nat) (f : nat → nat → nat) : a = b → b = c → d + (if b > 0 then a else b) = 0 → f (b + b) b ≠ f (a + c) c → false :=
by do intros,
s ← cc_state.mk_using_hs,
trace s,
t₁ ← to_expr ```(f (b + b) b),
t₂ ← to_expr ```(f (a + c) c),
b ← to_expr ```(b),
d ← to_expr ```(d),
guard (s^.inconsistent),
guard (s^.eqc_size b = 4),
guard (not (s^.in_singlenton_eqc b)),
guard (s^.in_singlenton_eqc d),
trace ">>> Equivalence roots",
trace s^.roots,
trace ">>> b's equivalence class",
trace (s^.eqc_of b),
pr ← s^.eqv_proof t₁ t₂,
note `h none pr,
contradiction
example (a b : nat) (f : nat → nat) : a = b → f a = f b :=
by cc
example (a b : nat) (f : nat → nat) : a = b → f a ≠ f b → false :=
by cc
example (a b : nat) (f : nat → nat) : a = b → f (f a) ≠ f (f b) → false :=
by cc
example (a b c : nat) (f : nat → nat) : a = b → c = b → f (f a) ≠ f (f c) → false :=
by cc
example (a b c : nat) (f : nat → nat → nat) : a = b → c = b → f (f a b) a ≠ f (f c c) c → false :=
by cc
example (a b c : nat) (f : nat → nat → nat) : a = b → c = b → f (f a b) a = f (f c c) c :=
by cc
example (a b c d : nat) : a == b → b = c → c == d → a == d :=
by cc
example (a b c d : nat) : a = b → b = c → c == d → a == d :=
by cc
example (a b c d : nat) : a = b → b == c → c == d → a == d :=
by cc
example (a b c d : nat) : a == b → b == c → c = d → a == d :=
by cc
example (a b c d : nat) : a == b → b = c → c = d → a == d :=
by cc
example (a b c d : nat) : a = b → b = c → c = d → a == d :=
by cc
example (a b c d : nat) : a = b → b == c → c = d → a == d :=
by cc
constant f {α : Type} : α → α → α
constant g : nat → nat
example (a b c : nat) : a = b → g a == g b :=
by cc
example (a b c : nat) : a = b → c = b → f (f a b) (g c) = f (f c a) (g b) :=
by cc
example (a b c d e x y : nat) : a = b → a = x → b = y → c = d → c = e → c = b → a = e :=
by cc
|
3bfb41737b6623188bb061f76e1f658c832781a7 | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /archive/sensitivity.lean | 408a2291020aaa9294e164ac0e8ece10798591a2 | [
"Apache-2.0"
] | permissive | dupuisf/mathlib | 62de4ec6544bf3b79086afd27b6529acfaf2c1bb | 8582b06b0a5d06c33ee07d0bdf7c646cae22cf36 | refs/heads/master | 1,669,494,854,016 | 1,595,692,409,000 | 1,595,692,409,000 | 272,046,630 | 0 | 0 | Apache-2.0 | 1,592,066,143,000 | 1,592,066,142,000 | null | UTF-8 | Lean | false | false | 15,505 | lean | /-
Copyright (c) 2019 Reid Barton, Johan Commelin, Jesse Han, Chris Hughes, Robert Y. Lewis, and
Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Reid Barton, Johan Commelin, Jesse Han, Chris Hughes, Robert Y. Lewis, and Patrick Massot
-/
import tactic.fin_cases
import tactic.apply_fun
import linear_algebra.finite_dimensional
import linear_algebra.dual
import analysis.normed_space.basic
/-!
# Huang's sensitivity theorem
A formalization of Hao Huang's sensitivity theorem: in the hypercube of
dimension n ≥ 1, if one colors more than half the vertices then at least one
vertex has at least √n colored neighbors.
A fun summer collaboration by
Reid Barton, Johan Commelin, Jesse Han, Chris Hughes, Robert Y. Lewis, and Patrick Massot,
based on Don Knuth's account of the story
(https://www.cs.stanford.edu/~knuth/papers/huang.pdf),
using the Lean theorem prover (https://leanprover.github.io/),
by Leonardo de Moura at Microsoft Research, and his collaborators
(https://leanprover.github.io/people/),
and using Lean's user maintained mathematics library
(https://github.com/leanprover-community/mathlib).
The project was developed at https://github.com/leanprover-community/lean-sensitivity
and is now archived at https://github.com/leanprover-community/mathlib/blob/master/archive/sensitivity.lean
-/
/-! The next two lines assert we do not want to give a constructive proof,
but rather use classical logic. -/
noncomputable theory
open_locale classical
/-! We also want to use the notation `∑` for sums. -/
open_locale big_operators
notation `|`x`|` := abs x
notation `√` := real.sqrt
open function bool linear_map fintype finite_dimensional dual_pair
/-!
### The hypercube
Notations:
- `ℕ` denotes natural numbers (including zero).
- `fin n` = {0, ⋯ , n - 1}.
- `bool` = {`tt`, `ff`}.
-/
/-- The hypercube in dimension `n`. -/
@[derive [inhabited, fintype]] def Q (n : ℕ) := fin n → bool
/-- The projection from `Q (n + 1)` to `Q n` forgetting the first value
(ie. the image of zero). -/
def π {n : ℕ} : Q (n + 1) → Q n := λ p, p ∘ fin.succ
namespace Q
/-! `n` will always denote a natural number. -/
variable (n : ℕ)
/-- `Q 0` has a unique element. -/
instance : unique (Q 0) :=
⟨⟨λ _, tt⟩, by { intro, ext x, fin_cases x }⟩
/-- `Q n` has 2^n elements. -/
lemma card : card (Q n) = 2^n :=
by simp [Q]
/-! Until the end of this namespace, `n` will be an implicit argument (still
a natural number). -/
variable {n}
lemma succ_n_eq (p q : Q (n+1)) : p = q ↔ (p 0 = q 0 ∧ π p = π q) :=
begin
split,
{ rintro rfl, exact ⟨rfl, rfl⟩, },
{ rintros ⟨h₀, h⟩,
ext x,
by_cases hx : x = 0,
{ rwa hx },
{ rw ← fin.succ_pred x hx,
convert congr_fun h (fin.pred x hx) } }
end
/-- The adjacency relation defining the graph structure on `Q n`:
`p.adjacent q` if there is an edge from `p` to `q` in `Q n`. -/
def adjacent {n : ℕ} (p : Q n) : set (Q n) := λ q, ∃! i, p i ≠ q i
/-- In `Q 0`, no two vertices are adjacent. -/
lemma not_adjacent_zero (p q : Q 0) : ¬ p.adjacent q :=
by rintros ⟨v, _⟩; apply fin_zero_elim v
/-- If `p` and `q` in `Q (n+1)` have different values at zero then they are adjacent
iff their projections to `Q n` are equal. -/
lemma adj_iff_proj_eq {p q : Q (n+1)} (h₀ : p 0 ≠ q 0) :
p.adjacent q ↔ π p = π q :=
begin
split,
{ rintros ⟨i, h_eq, h_uni⟩,
ext x, by_contradiction hx,
apply fin.succ_ne_zero x,
rw [h_uni _ hx, h_uni _ h₀] },
{ intro heq,
use [0, h₀],
intros y hy,
contrapose! hy,
rw ←fin.succ_pred _ hy,
apply congr_fun heq }
end
/-- If `p` and `q` in `Q (n+1)` have the same value at zero then they are adjacent
iff their projections to `Q n` are adjacent. -/
lemma adj_iff_proj_adj {p q : Q (n+1)} (h₀ : p 0 = q 0) :
p.adjacent q ↔ (π p).adjacent (π q) :=
begin
split,
{ rintros ⟨i, h_eq, h_uni⟩,
have h_i : i ≠ 0, from λ h_i, absurd h₀ (by rwa h_i at h_eq),
use [i.pred h_i,
show p (fin.succ (fin.pred i _)) ≠ q (fin.succ (fin.pred i _)),
by rwa fin.succ_pred],
intros y hy,
simp [eq.symm (h_uni _ hy)] },
{ rintros ⟨i, h_eq, h_uni⟩,
use [i.succ, h_eq],
intros y hy,
rw [←fin.pred_inj, fin.pred_succ],
{ apply h_uni,
change p (fin.pred _ _).succ ≠ q (fin.pred _ _).succ,
simp [hy] },
{ contrapose! hy,
rw [hy, h₀] },
{ apply fin.succ_ne_zero } }
end
@[symm] lemma adjacent.symm {p q : Q n} : p.adjacent q ↔ q.adjacent p :=
by simp only [adjacent, ne_comm]
end Q
/-! ### The vector space -/
/-- The free vector space on vertices of a hypercube, defined inductively. -/
def V : ℕ → Type
| 0 := ℝ
| (n+1) := V n × V n
namespace V
variables (n : ℕ)
/-! `V n` is a real vector space whose equality relation is computable. -/
instance : decidable_eq (V n) :=
by { induction n ; { dunfold V, resetI, apply_instance } }
instance : add_comm_group (V n) :=
by { induction n ; { dunfold V, resetI, apply_instance } }
instance : vector_space ℝ (V n) :=
by { induction n ; { dunfold V, resetI, apply_instance } }
end V
/-- The basis of `V` indexed by the hypercube, defined inductively. -/
noncomputable def e : Π {n}, Q n → V n
| 0 := λ _, (1:ℝ)
| (n+1) := λ x, cond (x 0) (e (π x), 0) (0, e (π x))
@[simp] lemma e_zero_apply (x : Q 0) : e x = (1 : ℝ) := rfl
/-- The dual basis to `e`, defined inductively. -/
noncomputable def ε : Π {n : ℕ} (p : Q n), V n →ₗ[ℝ] ℝ
| 0 _ := linear_map.id
| (n+1) p := cond (p 0) ((ε $ π p).comp $ linear_map.fst _ _ _) ((ε $ π p).comp $ linear_map.snd _ _ _)
variable {n : ℕ}
lemma duality (p q : Q n) : ε p (e q) = if p = q then 1 else 0 :=
begin
induction n with n IH,
{ rw (show p = q, from subsingleton.elim p q),
dsimp [ε, e],
simp },
{ dsimp [ε, e],
cases hp : p 0 ; cases hq : q 0,
all_goals {
repeat {rw cond_tt},
repeat {rw cond_ff},
simp only [linear_map.fst_apply, linear_map.snd_apply, linear_map.comp_apply, IH],
try { congr' 1, rw Q.succ_n_eq, finish },
try {
erw (ε _).map_zero,
have : p ≠ q, { intro h, rw p.succ_n_eq q at h, finish },
simp [this] } } }
end
/-- Any vector in `V n` annihilated by all `ε p`'s is zero. -/
lemma epsilon_total {v : V n} (h : ∀ p : Q n, (ε p) v = 0) : v = 0 :=
begin
induction n with n ih,
{ dsimp [ε] at h, exact h (λ _, tt) },
{ cases v with v₁ v₂,
ext ; change _ = (0 : V n) ; simp only ; apply ih ; intro p ;
[ let q : Q (n+1) := λ i, if h : i = 0 then tt else p (i.pred h),
let q : Q (n+1) := λ i, if h : i = 0 then ff else p (i.pred h)],
all_goals {
specialize h q,
rw [ε, show q 0 = tt, from rfl, cond_tt] at h <|> rw [ε, show q 0 = ff, from rfl, cond_ff] at h,
rwa show p = π q, by { ext, simp [q, fin.succ_ne_zero, π] } } }
end
/-- `e` and `ε` are dual families of vectors. It implies that `e` is indeed a basis
and `ε` computes coefficients of decompositions of vectors on that basis. -/
def dual_pair_e_ε (n : ℕ) : dual_pair (@e n) (@ε n) :=
{ eval := duality,
total := @epsilon_total _ }
/-! We will now derive the dimension of `V`, first as a cardinal in `dim_V` and,
since this cardinal is finite, as a natural number in `findim_V` -/
lemma dim_V : vector_space.dim ℝ (V n) = 2^n :=
have vector_space.dim ℝ (V n) = (2^n : ℕ),
by { rw [dim_eq_card_basis (dual_pair_e_ε _).is_basis, Q.card]; apply_instance },
by assumption_mod_cast
instance : finite_dimensional ℝ (V n) :=
finite_dimensional.of_finite_basis (dual_pair_e_ε _).is_basis
lemma findim_V : findim ℝ (V n) = 2^n :=
have _ := @dim_V n,
by rw ←findim_eq_dim at this; assumption_mod_cast
/-! ### The linear map -/
/-- The linear operator $f_n$ corresponding to Huang's matrix $A_n$,
defined inductively as a ℝ-linear map from `V n` to `V n`. -/
noncomputable def f : Π n, V n →ₗ[ℝ] V n
| 0 := 0
| (n+1) := linear_map.prod
(linear_map.coprod (f n) linear_map.id)
(linear_map.coprod linear_map.id (-f n))
/-! The preceding definition uses linear map constructions to automatically
get that `f` is linear, but its values are somewhat buried as a side-effect.
The next two lemmas unbury them. -/
@[simp] lemma f_zero : f 0 = 0 := rfl
lemma f_succ_apply (v : V (n+1)) :
f (n+1) v = (f n v.1 + v.2, v.1 - f n v.2) :=
begin
cases v,
rw f,
simp only [linear_map.id_apply, linear_map.prod_apply, prod.mk.inj_iff,
linear_map.neg_apply, sub_eq_add_neg, linear_map.coprod_apply],
exact ⟨rfl, rfl⟩
end
/-! In the next statement, the explicit conversion `(n : ℝ)` of `n` to a real number
is necessary since otherwise `n • v` refers to the multiplication defined
using only the addition of `V`. -/
lemma f_squared : ∀ v : V n, (f n) (f n v) = (n : ℝ) • v :=
begin
induction n with n IH; intro,
{ simpa only [nat.cast_zero, zero_smul] },
{ cases v, simp [f_succ_apply, IH, add_smul, add_assoc], abel }
end
/-! We now compute the matrix of `f` in the `e` basis (`p` is the line index,
`q` the column index). -/
lemma f_matrix :
∀ p q : Q n, |ε q (f n (e p))| = if q.adjacent p then 1 else 0 :=
begin
induction n with n IH,
{ intros p q,
dsimp [f],
simp [Q.not_adjacent_zero] },
{ intros p q,
have ite_nonneg : ite (π q = π p) (1 : ℝ) 0 ≥ 0,
{ split_ifs ; norm_num },
have f_map_zero := (show linear_map ℝ (V (n+0)) (V n), from f n).map_zero,
dsimp [e, ε, f], cases hp : p 0 ; cases hq : q 0,
all_goals
{ repeat {rw cond_tt}, repeat {rw cond_ff},
simp [f_map_zero, hp, hq, IH, duality, abs_of_nonneg ite_nonneg, Q.adj_iff_proj_eq,
Q.adj_iff_proj_adj] } }
end
/-- The linear operator $g_m$ corresponding to Knuth's matrix $B_m$. -/
noncomputable def g (m : ℕ) : V m →ₗ[ℝ] V (m+1) :=
linear_map.prod (f m + √(m+1) • linear_map.id) linear_map.id
/-! In the following lemmas, `m` will denote a natural number. -/
variables {m : ℕ}
/-! Again we unpack what are the values of `g`. -/
lemma g_apply : ∀ v, g m v = (f m v + √(m+1) • v, v) :=
by delta g; simp
lemma g_injective : injective (g m) :=
begin
rw g,
intros x₁ x₂ h,
simp only [linear_map.prod_apply, linear_map.id_apply, prod.mk.inj_iff] at h,
exact h.right
end
lemma f_image_g (w : V (m + 1)) (hv : ∃ v, g m v = w) :
f (m + 1) w = √(m + 1) • w :=
begin
rcases hv with ⟨v, rfl⟩,
have : √(m+1) * √(m+1) = m+1 :=
real.mul_self_sqrt (by exact_mod_cast zero_le _),
simp [this, f_succ_apply, g_apply, f_squared, smul_add, add_smul, smul_smul],
abel
end
/-!
### The main proof
In this section, in order to enforce that `n` is positive, we write it as
`m + 1` for some natural number `m`. -/
/-! `dim X` will denote the dimension of a subspace `X` as a cardinal. -/
notation `dim` X:70 := vector_space.dim ℝ ↥X
/-! `fdim X` will denote the (finite) dimension of a subspace `X` as a natural number. -/
notation `fdim` := findim ℝ
/-! `Span S` will denote the ℝ-subspace spanned by `S`. -/
notation `Span` := submodule.span ℝ
/-! `Card X` will denote the cardinal of a subset of a finite type, as a
natural number. -/
notation `Card` X:70 := X.to_finset.card
/-! In the following, `⊓` and `⊔` will denote intersection and sums of ℝ-subspaces,
equipped with their subspace structures. The notations come from the general
theory of lattices, with inf and sup (also known as meet and join). -/
/-- If a subset `H` of `Q (m+1)` has cardinal at least `2^m + 1` then the
subspace of `V (m+1)` spanned by the corresponding basis vectors non-trivially
intersects the range of `g m`. -/
lemma exists_eigenvalue (H : set (Q (m + 1))) (hH : Card H ≥ 2^m + 1) :
∃ y ∈ Span (e '' H) ⊓ (g m).range, y ≠ (0 : _) :=
begin
let W := Span (e '' H),
let img := (g m).range,
suffices : 0 < dim (W ⊓ img),
{ simp only [exists_prop],
exact_mod_cast exists_mem_ne_zero_of_dim_pos this },
have dim_le : dim (W ⊔ img) ≤ 2^(m + 1),
{ convert ← dim_submodule_le (W ⊔ img),
apply dim_V },
have dim_add : dim (W ⊔ img) + dim (W ⊓ img) = dim W + 2^m,
{ convert ← dim_sup_add_dim_inf_eq W img,
rw ← dim_eq_of_injective (g m) g_injective,
apply dim_V },
have dimW : dim W = card H,
{ have li : linear_independent ℝ (set.restrict e H) :=
linear_independent.comp (dual_pair_e_ε _).is_basis.1 _ subtype.val_injective,
have hdW := dim_span li,
rw set.range_restrict at hdW,
convert hdW,
rw [cardinal.mk_image_eq ((dual_pair_e_ε _).is_basis.injective zero_ne_one), cardinal.fintype_card] },
rw ← findim_eq_dim ℝ at ⊢ dim_le dim_add dimW,
rw [← findim_eq_dim ℝ, ← findim_eq_dim ℝ] at dim_add,
norm_cast at ⊢ dim_le dim_add dimW,
rw nat.pow_succ at dim_le,
rw set.to_finset_card at hH,
linarith
end
theorem huang_degree_theorem (H : set (Q (m + 1))) (hH : Card H ≥ 2^m + 1) :
∃ q, q ∈ H ∧ √(m + 1) ≤ Card (H ∩ q.adjacent) :=
begin
rcases exists_eigenvalue H hH with ⟨y, ⟨⟨y_mem_H, y_mem_g⟩, y_ne⟩⟩,
have coeffs_support : ((dual_pair_e_ε (m+1)).coeffs y).support ⊆ H.to_finset,
{ intros p p_in,
rw finsupp.mem_support_iff at p_in,
rw set.mem_to_finset,
exact (dual_pair_e_ε _).mem_of_mem_span y_mem_H p p_in },
obtain ⟨q, H_max⟩ : ∃ q : Q (m+1), ∀ q' : Q (m+1), |(ε q' : _) y| ≤ |ε q y|,
from fintype.exists_max _,
have H_q_pos : 0 < |ε q y|,
{ contrapose! y_ne,
exact epsilon_total (λ p, abs_nonpos_iff.mp (le_trans (H_max p) y_ne)) },
refine ⟨q, (dual_pair_e_ε _).mem_of_mem_span y_mem_H q (abs_pos_iff.mp H_q_pos), _⟩,
let s := √(m+1),
suffices : s * |ε q y| ≤ ↑(_) * |ε q y|,
from (mul_le_mul_right H_q_pos).mp ‹_›,
let coeffs := (dual_pair_e_ε (m+1)).coeffs,
calc
s * (abs (ε q y))
= abs (ε q (s • y)) : by rw [map_smul, smul_eq_mul, abs_mul, abs_of_nonneg (real.sqrt_nonneg _)]
... = abs (ε q (f (m+1) y)) : by rw [← f_image_g y (by simpa using y_mem_g)]
... = abs (ε q (f (m+1) (lc _ (coeffs y)))) : by rw (dual_pair_e_ε _).decomposition y
... = abs ((coeffs y).sum (λ (i : Q (m + 1)) (a : ℝ), a • ((ε q) ∘ (f (m + 1)) ∘ λ (i : Q (m + 1)), e i) i)): by
{ erw [(f $ m+1).map_finsupp_total, (ε q).map_finsupp_total, finsupp.total_apply] ; apply_instance }
... ≤ ∑ p in (coeffs y).support, |(coeffs y p) * (ε q $ f (m+1) $ e p)| : norm_sum_le _ $ λ p, coeffs y p * _
... = ∑ p in (coeffs y).support, |coeffs y p| * ite (q.adjacent p) 1 0 : by simp only [abs_mul, f_matrix]
... = ∑ p in (coeffs y).support.filter (Q.adjacent q), |coeffs y p| : by simp [finset.sum_filter]
... ≤ ∑ p in (coeffs y).support.filter (Q.adjacent q), |coeffs y q| : finset.sum_le_sum (λ p _, H_max p)
... = (finset.card ((coeffs y).support.filter (Q.adjacent q)): ℝ) * |coeffs y q| : by rw [finset.sum_const, nsmul_eq_mul]
... = (finset.card ((coeffs y).support ∩ (Q.adjacent q).to_finset): ℝ) * |coeffs y q| : by {congr, ext, simp, refl}
... ≤ (finset.card ((H ∩ Q.adjacent q).to_finset )) * |ε q y| :
(mul_le_mul_right H_q_pos).mpr (by {
norm_cast,
exact finset.card_le_of_subset (by rw set.to_finset_inter; convert finset.inter_subset_inter_right coeffs_support) })
end
|
a745d21ab9deaa3b3f6052ff6fa402f158bed950 | 03577f7aaac416af2e8f734149669af300dc499c | /lean_exercises/lcp_lcpo.lean | 042877392c5d8bf10879ba0180b42a3bbae4c765 | [] | no_license | nymarya/algorithms | b4dc73389de588e6934da15b0e0b67ae354907aa | 1df987f82273a8ffe2b2042552bbb1428bf165bf | refs/heads/master | 1,609,629,118,479 | 1,600,087,537,000 | 1,600,087,537,000 | 99,489,969 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 1,242 | lean | variables ( A B C D P Q : Prop )
variable R: Prop → Prop
-- LCP-003
example ( h1: A→(B→C)) (h2 : A→B) : (A→C) :=
assume h3: A,
show C, from h1 (h3) (h2 (h3))
-- LCP-007
example ( h: A→(B→(C→D))) : (C→(B→(A→D))) :=
assume hc: C,
assume hb: B,
assume ha: A,
show D , from h (ha) (hb) (hc)
-- LCP-032
example (h : (A ∨ B) → C) : (A -> C) :=
assume ha: A,
show C, from h (or.inl ha)
-- LCP-093
example ( h : A ∨ B): ¬(¬A∧¬B) :=
assume h1 : ¬A∧¬B,
show false, from
or.elim h (
assume ha: A,
absurd ha h1.left
)
(
assume hb: B,
absurd hb h1.right
)
-- LCPO-07
example (h : P -> ∀x,R(x)) : ∀x,(P -> R(x)) :=
assume y,
assume P,
show R y, from h (P) y
-- LCPO-22
example (h: ∀x,R(x) ) : ∀y,( P ∨ R(y) ∨ Q ) :=
assume y,
show P ∨ R y ∨ Q, from or.inr ( or.inl (h y) )
-- LCPO-40
example ( h: ∃x,R(x)) : (∃x,∃y,(R(x)∧R(y))) :=
exists.elim h
(assume (y ) (h1 : R y),
show ∃x, ∃y, R x ∧ R y , from
exists.intro y ( exists.intro y (and.intro (h1) (h1)) )
)
-- LCPO-58
example ( h: ¬∃x,R(x) ) : ( ∀ x,¬R(x)) :=
assume x,
assume hp: R x,
have ∃ x, R x, from exists.intro x hp ,
h this
|
1e5ef012f685b72045c575318639104e63e5466a | 8cb37a089cdb4af3af9d8bf1002b417e407a8e9e | /library/init/data/int/comp_lemmas.lean | 762ec3e734fb5d407d551cb1d4470289f8724bc8 | [
"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,041 | 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
Auxiliary lemmas used to compare int numerals.
-/
prelude
import init.data.int.order
namespace int
/- Auxiliary lemmas for proving that to int numerals are different -/
/- 1. Lemmas for reducing the problem to the case where the numerals are positive -/
protected lemma ne_neg_of_ne {a b : ℤ} : a ≠ b → -a ≠ -b :=
λ h₁ h₂, absurd (int.neg_inj h₂) h₁
protected lemma neg_ne_zero_of_ne {a : ℤ} : a ≠ 0 → -a ≠ 0 :=
λ h₁ h₂,
have -a = -0, by rwa int.neg_zero,
have a = 0, from int.neg_inj this,
by contradiction
protected lemma zero_ne_neg_of_ne {a : ℤ} (h : 0 ≠ a) : 0 ≠ -a :=
ne.symm (int.neg_ne_zero_of_ne (ne.symm h))
protected lemma neg_ne_of_pos {a b : ℤ} : 0 < a → 0 < b → -a ≠ b :=
λ h₁ h₂ h,
begin
rw [← h] at h₂,
change 0 < a at h₁,
have := le_of_lt h₁,
exact absurd (le_of_lt h₁) (not_le_of_gt (int.neg_of_neg_pos h₂))
end
protected lemma ne_neg_of_pos {a b : ℤ} : 0 < a → 0 < b → a ≠ -b :=
λ h₁ h₂, ne.symm (int.neg_ne_of_pos h₂ h₁)
/- 2. Lemmas for proving that positive int numerals are nonneg and positive -/
protected lemma one_pos : 0 < (1:int) :=
int.zero_lt_one
protected lemma bit0_pos {a : ℤ} : 0 < a → 0 < bit0 a :=
λ h, int.add_pos h h
protected lemma bit1_pos {a : ℤ} : 0 ≤ a → 0 < bit1 a :=
λ h, int.lt_add_of_le_of_pos (int.add_nonneg h h) int.zero_lt_one
protected lemma zero_nonneg : 0 ≤ (0 : ℤ) :=
le_refl 0
protected lemma one_nonneg : 0 ≤ (1 : ℤ) :=
le_of_lt (int.zero_lt_one)
protected lemma bit0_nonneg {a : ℤ} : 0 ≤ a → 0 ≤ bit0 a :=
λ h, int.add_nonneg h h
protected lemma bit1_nonneg {a : ℤ} : 0 ≤ a → 0 ≤ bit1 a :=
λ h, le_of_lt (int.bit1_pos h)
protected lemma nonneg_of_pos {a : ℤ} : 0 < a → 0 ≤ a :=
le_of_lt
/- 3. nat_abs auxiliary lemmas -/
lemma neg_succ_of_nat_lt_zero (n : ℕ) : neg_succ_of_nat n < 0 :=
@lt.intro _ _ n (by simp [neg_succ_of_nat_coe, int.coe_nat_succ, int.coe_nat_add, int.coe_nat_one,
int.add_comm, int.add_left_comm, int.neg_add, int.add_right_neg, int.zero_add])
lemma zero_le_of_nat (n : ℕ) : 0 ≤ of_nat n :=
@le.intro _ _ n (by rw [int.zero_add, int.coe_nat_eq])
lemma of_nat_nat_abs_eq_of_nonneg : ∀ {a : ℤ}, 0 ≤ a → of_nat (nat_abs a) = a
| (of_nat n) h := rfl
| (neg_succ_of_nat n) h := absurd (neg_succ_of_nat_lt_zero n) (not_lt_of_ge h)
lemma ne_of_nat_abs_ne_nat_abs_of_nonneg {a b : ℤ} (ha : 0 ≤ a) (hb : 0 ≤ b)
(h : nat_abs a ≠ nat_abs b) : a ≠ b :=
assume h,
have of_nat (nat_abs a) = of_nat (nat_abs b),
by rwa [of_nat_nat_abs_eq_of_nonneg ha, of_nat_nat_abs_eq_of_nonneg hb],
begin injection this, contradiction end
protected lemma ne_of_nat_ne_nonneg_case {a b : ℤ} {n m : nat} (ha : 0 ≤ a) (hb : 0 ≤ b)
(e1 : nat_abs a = n) (e2 : nat_abs b = m) (h : n ≠ m) : a ≠ b :=
have nat_abs a ≠ nat_abs b, by rwa [e1, e2],
ne_of_nat_abs_ne_nat_abs_of_nonneg ha hb this
/- 4. Aux lemmas for pushing nat_abs inside numerals
nat_abs_zero and nat_abs_one are defined at init/data/int/basic.lean -/
lemma nat_abs_of_nat_core (n : ℕ) : nat_abs (of_nat n) = n :=
rfl
lemma nat_abs_of_neg_succ_of_nat (n : ℕ) : nat_abs (neg_succ_of_nat n) = nat.succ n :=
rfl
protected lemma nat_abs_add_nonneg :
∀ {a b : int}, 0 ≤ a → 0 ≤ b → nat_abs (a + b) = nat_abs a + nat_abs b
| (of_nat n) (of_nat m) h₁ h₂ :=
have of_nat n + of_nat m = of_nat (n + m), from rfl,
by simp [nat_abs_of_nat_core, this]
| _ (neg_succ_of_nat m) h₁ h₂ := absurd (neg_succ_of_nat_lt_zero m) (not_lt_of_ge h₂)
| (neg_succ_of_nat n) _ h₁ h₂ := absurd (neg_succ_of_nat_lt_zero n) (not_lt_of_ge h₁)
protected lemma nat_abs_add_neg :
∀ {a b : int}, a < 0 → b < 0 → nat_abs (a + b) = nat_abs a + nat_abs b
| (neg_succ_of_nat n) (neg_succ_of_nat m) h₁ h₂ :=
have -[1+ n] + -[1+ m] = -[1+ nat.succ (n + m)], from rfl,
begin simp [nat_abs_of_neg_succ_of_nat, this, nat.succ_add, nat.add_succ] end
protected lemma nat_abs_bit0 : ∀ (a : int), nat_abs (bit0 a) = bit0 (nat_abs a)
| (of_nat n) := int.nat_abs_add_nonneg (zero_le_of_nat n) (zero_le_of_nat n)
| (neg_succ_of_nat n) := int.nat_abs_add_neg (neg_succ_of_nat_lt_zero n) (neg_succ_of_nat_lt_zero n)
protected lemma nat_abs_bit0_step {a : int} {n : nat} (h : nat_abs a = n) :
nat_abs (bit0 a) = bit0 n :=
begin rw [← h], apply int.nat_abs_bit0 end
protected lemma nat_abs_bit1_nonneg {a : int} (h : 0 ≤ a) : nat_abs (bit1 a) = bit1 (nat_abs a) :=
show nat_abs (bit0 a + 1) = bit0 (nat_abs a) + nat_abs 1, from
by rw [int.nat_abs_add_nonneg (int.bit0_nonneg h) (le_of_lt (int.zero_lt_one)), int.nat_abs_bit0]
protected lemma nat_abs_bit1_nonneg_step {a : int} {n : nat} (h₁ : 0 ≤ a) (h₂ : nat_abs a = n) :
nat_abs (bit1 a) = bit1 n :=
begin rw [← h₂], apply int.nat_abs_bit1_nonneg h₁ end
end int
|
039e1003a61bec057347059c05be0e77f162ba75 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/combinatorics/simple_graph/finsubgraph.lean | dc13b8d50a0a56e591e63bd4542eb375a24ca55c | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 6,006 | lean | /-
Copyright (c) 2022 Joanna Choules. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Joanna Choules
-/
import category_theory.cofiltered_system
import combinatorics.simple_graph.subgraph
/-!
# Homomorphisms from finite subgraphs
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines the type of finite subgraphs of a `simple_graph` and proves a compactness result
for homomorphisms to a finite codomain.
## Main statements
* `simple_graph.exists_hom_of_all_finite_homs`: If every finite subgraph of a (possibly infinite)
graph `G` has a homomorphism to some finite graph `F`, then there is also a homomorphism `G →g F`.
## Notations
`→fg` is a module-local variant on `→g` where the domain is a finite subgraph of some supergraph
`G`.
## Implementation notes
The proof here uses compactness as formulated in `nonempty_sections_of_finite_inverse_system`. For
finite subgraphs `G'' ≤ G'`, the inverse system `finsubgraph_hom_functor` restricts homomorphisms
`G' →fg F` to domain `G''`.
-/
open set
universes u v
variables {V : Type u} {W : Type v} {G : simple_graph V} {F : simple_graph W}
namespace simple_graph
/-- The subtype of `G.subgraph` comprising those subgraphs with finite vertex sets. -/
abbreviation finsubgraph (G : simple_graph V) := { G' : G.subgraph // G'.verts.finite }
/-- A graph homomorphism from a finite subgraph of G to F. -/
abbreviation finsubgraph_hom (G' : G.finsubgraph) (F : simple_graph W) := G'.val.coe →g F
local infix ` →fg ` : 50 := finsubgraph_hom
instance : order_bot G.finsubgraph :=
{ bot := ⟨⊥, finite_empty⟩,
bot_le := λ _, bot_le }
instance : has_sup G.finsubgraph := ⟨λ G₁ G₂, ⟨G₁ ⊔ G₂, G₁.2.union G₂.2⟩⟩
instance : has_inf G.finsubgraph := ⟨λ G₁ G₂, ⟨G₁ ⊓ G₂, G₁.2.subset $ inter_subset_left _ _⟩⟩
instance : distrib_lattice G.finsubgraph :=
subtype.coe_injective.distrib_lattice _ (λ _ _, rfl) (λ _ _, rfl)
instance [finite V] : has_top G.finsubgraph := ⟨⟨⊤, finite_univ⟩⟩
instance [finite V] : has_Sup G.finsubgraph := ⟨λ s, ⟨⨆ G ∈ s, ↑G, set.to_finite _⟩⟩
instance [finite V] : has_Inf G.finsubgraph := ⟨λ s, ⟨⨅ G ∈ s, ↑G, set.to_finite _⟩⟩
instance [finite V] : complete_distrib_lattice G.finsubgraph :=
subtype.coe_injective.complete_distrib_lattice _ (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) rfl
rfl
/-- The finite subgraph of G generated by a single vertex. -/
def singleton_finsubgraph (v : V) : G.finsubgraph := ⟨simple_graph.singleton_subgraph _ v, by simp⟩
/-- The finite subgraph of G generated by a single edge. -/
def finsubgraph_of_adj {u v : V} (e : G.adj u v) : G.finsubgraph :=
⟨simple_graph.subgraph_of_adj _ e, by simp⟩
/- Lemmas establishing the ordering between edge- and vertex-generated subgraphs. -/
lemma singleton_finsubgraph_le_adj_left {u v : V} {e : G.adj u v} :
singleton_finsubgraph u ≤ finsubgraph_of_adj e :=
by simp [singleton_finsubgraph, finsubgraph_of_adj]
lemma singleton_finsubgraph_le_adj_right {u v : V} {e : G.adj u v} :
singleton_finsubgraph v ≤ finsubgraph_of_adj e :=
by simp [singleton_finsubgraph, finsubgraph_of_adj]
/-- Given a homomorphism from a subgraph to `F`, construct its restriction to a sub-subgraph. -/
def finsubgraph_hom.restrict {G' G'' : G.finsubgraph} (h : G'' ≤ G') (f : G' →fg F) : G'' →fg F :=
begin
refine ⟨λ ⟨v, hv⟩, f.to_fun ⟨v, h.1 hv⟩, _⟩,
rintros ⟨u, hu⟩ ⟨v, hv⟩ huv,
exact f.map_rel' (h.2 huv),
end
/-- The inverse system of finite homomorphisms. -/
def finsubgraph_hom_functor (G : simple_graph V) (F : simple_graph W) :
(G.finsubgraph)ᵒᵖ ⥤ Type (max u v) :=
{ obj := λ G', G'.unop →fg F,
map := λ G' G'' g f, f.restrict (category_theory.le_of_hom g.unop), }
/-- If every finite subgraph of a graph `G` has a homomorphism to a finite graph `F`, then there is
a homomorphism from the whole of `G` to `F`. -/
lemma nonempty_hom_of_forall_finite_subgraph_hom [finite W]
(h : Π (G' : G.subgraph), G'.verts.finite → G'.coe →g F) : nonempty (G →g F) :=
begin
/- Obtain a `fintype` instance for `W`. -/
casesI nonempty_fintype W,
/- Establish the required interface instances. -/
haveI : ∀ (G' : (G.finsubgraph)ᵒᵖ), nonempty ((finsubgraph_hom_functor G F).obj G') :=
λ G', ⟨h G'.unop G'.unop.property⟩,
haveI : Π (G' : (G.finsubgraph)ᵒᵖ), fintype ((finsubgraph_hom_functor G F).obj G') :=
begin
intro G',
haveI : fintype (↥(G'.unop.val.verts)) := G'.unop.property.fintype,
haveI : fintype (↥(G'.unop.val.verts) → W) := begin
classical,
exact pi.fintype
end,
exact fintype.of_injective (λ f, f.to_fun) rel_hom.coe_fn_injective
end,
/- Use compactness to obtain a section. -/
obtain ⟨u, hu⟩ := nonempty_sections_of_finite_inverse_system (finsubgraph_hom_functor G F),
refine ⟨⟨λ v, _, _⟩⟩,
{ /- Map each vertex using the homomorphism provided for its singleton subgraph. -/
exact (u (opposite.op (singleton_finsubgraph v))).to_fun
⟨v, by {unfold singleton_finsubgraph, simp}⟩, },
{ /- Prove that the above mapping preserves adjacency. -/
intros v v' e,
/- The homomorphism for each edge's singleton subgraph agrees with those for its source and
target vertices. -/
have hv : opposite.op (finsubgraph_of_adj e) ⟶ opposite.op (singleton_finsubgraph v) :=
quiver.hom.op (category_theory.hom_of_le singleton_finsubgraph_le_adj_left),
have hv' : opposite.op (finsubgraph_of_adj e) ⟶ opposite.op (singleton_finsubgraph v') :=
quiver.hom.op (category_theory.hom_of_le singleton_finsubgraph_le_adj_right),
rw [← (hu hv), ← (hu hv')],
apply simple_graph.hom.map_adj,
/- `v` and `v'` are definitionally adjacent in `finsubgraph_of_adj e` -/
simp [finsubgraph_of_adj], }
end
end simple_graph
|
937820da112e33374145caa01067acfee0c276be | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /hott/algebra/category/functor/default.hlean | 8274c00c1c4d40d48b3d116c49ef3d60867377cc | [
"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 | 173 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import .basic
|
0daeffa21105275864b90f2f2cf50943e9a91c28 | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/data/nat/factorial/cast.lean | 0340d3652207fe4708461d450ec0805ded8a19b1 | [
"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 | 2,239 | lean | /-
Copyright (c) 2021 Yaël Dillies. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies
-/
import ring_theory.polynomial.pochhammer
/-!
# Cast of factorials
This file allows calculating factorials (including ascending and descending ones) as elements of a
semiring.
This is particularly crucial for `nat.desc_factorial` as substraction on `ℕ` does **not** correspond
to substraction on a general semiring. For example, we can't rely on existing cast lemmas to prove
`↑(a.desc_factorial 2) = ↑a * (↑a - 1)`. We must use the fact that, whenever `↑(a - 1)` is not equal
to `↑a - 1`, the other factor is `0` anyway.
-/
open_locale nat
variables (S : Type*)
namespace nat
section semiring
variables [semiring S] (a b : ℕ)
lemma cast_asc_factorial :
(a.asc_factorial b : S) = (pochhammer S b).eval (a + 1) :=
by rw [←pochhammer_nat_eq_asc_factorial, pochhammer_eval_cast, nat.cast_add, nat.cast_one]
lemma cast_desc_factorial :
(a.desc_factorial b : S) = (pochhammer S b).eval (a - (b - 1) : ℕ) :=
begin
rw [←pochhammer_eval_cast, pochhammer_nat_eq_desc_factorial],
cases b,
{ simp_rw desc_factorial_zero },
simp_rw [add_succ, succ_sub_one],
obtain h | h := le_total a b,
{ rw [desc_factorial_of_lt (lt_succ_of_le h), desc_factorial_of_lt (lt_succ_of_le _)],
rw [nat.sub_eq_zero_of_le h, zero_add] },
{ rw nat.sub_add_cancel h }
end
lemma cast_factorial :
(a! : S) = (pochhammer S a).eval 1 :=
by rw [←zero_asc_factorial, cast_asc_factorial, cast_zero, zero_add]
end semiring
section ring
variables [ring S] (a b : ℕ)
/-- Convenience lemma. The `a - 1` is not using truncated substraction, as opposed to the definition
of `nat.desc_factorial` as a natural. -/
lemma cast_desc_factorial_two :
(a.desc_factorial 2 : S) = a * (a - 1) :=
begin
rw cast_desc_factorial,
cases a,
{ rw [nat.zero_sub, cast_zero, pochhammer_ne_zero_eval_zero _ (two_ne_zero), zero_mul] },
{ rw [succ_sub_succ, nat.sub_zero, cast_succ, add_sub_cancel, pochhammer_succ_right,
pochhammer_one, polynomial.X_mul, polynomial.eval_mul_X, polynomial.eval_add,
polynomial.eval_X, cast_one, polynomial.eval_one] }
end
end ring
end nat
|
639a2e9abba50decf3ed6c8ecbe14d57cdee2cdc | 4727251e0cd73359b15b664c3170e5d754078599 | /src/ring_theory/power_series/well_known.lean | 0e1ee81f3f6fdc4b8c3d644c9b7faf84be4e9e05 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 4,968 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import ring_theory.power_series.basic
import data.nat.parity
import algebra.big_operators.nat_antidiagonal
/-!
# Definition of well-known power series
In this file we define the following power series:
* `power_series.inv_units_sub`: given `u : Rˣ`, this is the series for `1 / (u - x)`.
It is given by `∑ n, x ^ n /ₚ u ^ (n + 1)`.
* `power_series.sin`, `power_series.cos`, `power_series.exp` : power series for sin, cosine, and
exponential functions.
-/
namespace power_series
section ring
variables {R S : Type*} [ring R] [ring S]
/-- The power series for `1 / (u - x)`. -/
def inv_units_sub (u : Rˣ) : power_series R := mk $ λ n, 1 /ₚ u ^ (n + 1)
@[simp] lemma coeff_inv_units_sub (u : Rˣ) (n : ℕ) :
coeff R n (inv_units_sub u) = 1 /ₚ u ^ (n + 1) :=
coeff_mk _ _
@[simp] lemma constant_coeff_inv_units_sub (u : Rˣ) :
constant_coeff R (inv_units_sub u) = 1 /ₚ u :=
by rw [← coeff_zero_eq_constant_coeff_apply, coeff_inv_units_sub, zero_add, pow_one]
@[simp] lemma inv_units_sub_mul_X (u : Rˣ) :
inv_units_sub u * X = inv_units_sub u * C R u - 1 :=
begin
ext (_|n),
{ simp },
{ simp [n.succ_ne_zero, pow_succ] }
end
@[simp] lemma inv_units_sub_mul_sub (u : Rˣ) : inv_units_sub u * (C R u - X) = 1 :=
by simp [mul_sub, sub_sub_cancel]
lemma map_inv_units_sub (f : R →+* S) (u : Rˣ) :
map f (inv_units_sub u) = inv_units_sub (units.map (f : R →* S) u) :=
by { ext, simp [← map_pow] }
end ring
section field
variables (A A' : Type*) [ring A] [ring A'] [algebra ℚ A] [algebra ℚ A']
open_locale nat
/-- Power series for the exponential function at zero. -/
def exp : power_series A := mk $ λ n, algebra_map ℚ A (1 / n!)
/-- Power series for the sine function at zero. -/
def sin : power_series A :=
mk $ λ n, if even n then 0 else algebra_map ℚ A ((-1) ^ (n / 2) / n!)
/-- Power series for the cosine function at zero. -/
def cos : power_series A :=
mk $ λ n, if even n then algebra_map ℚ A ((-1) ^ (n / 2) / n!) else 0
variables {A A'} (n : ℕ) (f : A →+* A')
@[simp] lemma coeff_exp : coeff A n (exp A) = algebra_map ℚ A (1 / n!) := coeff_mk _ _
@[simp] lemma constant_coeff_exp : constant_coeff A (exp A) = 1 :=
by { rw [← coeff_zero_eq_constant_coeff_apply, coeff_exp], simp }
@[simp] lemma map_exp : map (f : A →+* A') (exp A) = exp A' := by { ext, simp }
@[simp] lemma map_sin : map f (sin A) = sin A' := by { ext, simp [sin, apply_ite f] }
@[simp] lemma map_cos : map f (cos A) = cos A' := by { ext, simp [cos, apply_ite f] }
end field
open ring_hom
open finset nat
variables {A : Type*} [comm_ring A]
/-- Shows that $e^{aX} * e^{bX} = e^{(a + b)X}$ -/
theorem exp_mul_exp_eq_exp_add [algebra ℚ A] (a b : A) :
rescale a (exp A) * rescale b (exp A) = rescale (a + b) (exp A) :=
begin
ext,
simp only [coeff_mul, exp, rescale, coeff_mk, coe_mk, factorial,
nat.sum_antidiagonal_eq_sum_range_succ_mk, add_pow, sum_mul],
apply sum_congr rfl,
rintros x hx,
suffices : a^x * b^(n - x) * (algebra_map ℚ A (1 / ↑(x.factorial)) * algebra_map ℚ A
(1 / ↑((n - x).factorial))) =
a^x * b^(n - x) * ((↑(n.choose x) * (algebra_map ℚ A) (1 / ↑(n.factorial)))),
{ convert this using 1; ring },
congr' 1,
rw [←map_nat_cast (algebra_map ℚ A) (n.choose x), ←map_mul, ←map_mul],
refine ring_hom.congr_arg _ _,
rw [mul_one_div ↑(n.choose x) _, one_div_mul_one_div],
symmetry,
rw [div_eq_iff, div_mul_eq_mul_div, one_mul, choose_eq_factorial_div_factorial],
norm_cast,
rw cast_div_char_zero,
{ apply factorial_mul_factorial_dvd_factorial (mem_range_succ_iff.1 hx), },
{ apply mem_range_succ_iff.1 hx, },
{ rintros h, apply factorial_ne_zero n, rw cast_eq_zero.1 h, },
end
/-- Shows that $e^{x} * e^{-x} = 1$ -/
theorem exp_mul_exp_neg_eq_one [algebra ℚ A] : exp A * eval_neg_hom (exp A) = 1 :=
by convert exp_mul_exp_eq_exp_add (1 : A) (-1); simp
/-- Shows that $(e^{X})^k = e^{kX}$. -/
theorem exp_pow_eq_rescale_exp [algebra ℚ A] (k : ℕ) : (exp A)^k = rescale (k : A) (exp A) :=
begin
induction k with k h,
{ simp only [rescale_zero, constant_coeff_exp, function.comp_app, map_one, cast_zero,
pow_zero, coe_comp], },
simpa only [succ_eq_add_one, cast_add, ←exp_mul_exp_eq_exp_add (k : A), ←h, cast_one,
id_apply, rescale_one] using pow_succ' (exp A) k,
end
/-- Shows that
$\sum_{k = 0}^{n - 1} (e^{X})^k = \sum_{p = 0}^{\infty} \sum_{k = 0}^{n - 1} \frac{k^p}{p!}X^p$. -/
theorem exp_pow_sum [algebra ℚ A] (n : ℕ) : (finset.range n).sum (λ k, (exp A)^k) =
power_series.mk (λ p, (finset.range n).sum (λ k, k^p * algebra_map ℚ A p.factorial⁻¹)) :=
begin
simp only [exp_pow_eq_rescale_exp, rescale],
ext,
simp only [one_div, coeff_mk, coe_mk, coeff_exp, factorial, linear_map.map_sum],
end
end power_series
|
aedd4e0d87aeda36e81548797da566f27c93f602 | b2fe74b11b57d362c13326bc5651244f111fa6f4 | /src/number_theory/pythagorean_triples.lean | c447e0d93b4545c7f34e84bf86394be1834d71d8 | [
"Apache-2.0"
] | permissive | midfield/mathlib | c4db5fa898b5ac8f2f80ae0d00c95eb6f745f4c7 | 775edc615ecec631d65b6180dbcc7bc26c3abc26 | refs/heads/master | 1,675,330,551,921 | 1,608,304,514,000 | 1,608,304,514,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 26,067 | lean | /-
Copyright (c) 2020 Paul van Wamelen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Paul van Wamelen.
-/
import algebra.field
import ring_theory.int.basic
import algebra.group_with_zero.power
import tactic.ring
import tactic.ring_exp
/-!
# Pythagorean Triples
The main result is the classification of pythagorean triples. The final result is for general
pythagorean triples. It follows from the more interesting relatively prime case. We use the
"rational parametrization of the circle" method for the proof. The parametrization maps the point
`(x / z, y / z)` to the slope of the line through `(-1 , 0)` and `(x / z, y / z)`. This quickly
shows that `(x / z, y / z) = (2 * m * n / (m ^ 2 + n ^ 2), (m ^ 2 - n ^ 2) / (m ^ 2 + n ^ 2))` where
`m / n` is the slope. In order to identify numerators and denominators we now need results showing
that these are coprime. This is easy except for the prime 2. In order to deal with that we have to
analyze the parity of `x`, `y`, `m` and `n` and eliminate all the impossible cases. This takes up
the bulk of the proof below.
-/
noncomputable theory
open_locale classical
/-- Three integers `x`, `y`, and `z` form a Pythagorean triple if `x * x + y * y = z * z`. -/
def pythagorean_triple (x y z : ℤ) : Prop := x * x + y * y = z * z
/-- Pythagorean triples are interchangable, i.e `x * x + y * y = y * y + x * x = z * z`.
This comes from additive commutativity. -/
lemma pythagorean_triple_comm {x y z : ℤ} :
(pythagorean_triple x y z) ↔ (pythagorean_triple y x z) :=
by { delta pythagorean_triple, rw add_comm }
/-- The zeroth Pythagorean triple is all zeros. -/
lemma pythagorean_triple.zero : pythagorean_triple 0 0 0 :=
by simp only [pythagorean_triple, zero_mul, zero_add]
namespace pythagorean_triple
variables {x y z : ℤ} (h : pythagorean_triple x y z)
include h
lemma eq : x * x + y * y = z * z := h
@[symm]
lemma symm :
pythagorean_triple y x z :=
by rwa [pythagorean_triple_comm]
/-- A triple is still a triple if you multiply `x`, `y` and `z`
by a constant `k`. -/
lemma mul (k : ℤ) : pythagorean_triple (k * x) (k * y) (k * z) :=
begin
by_cases hk : k = 0,
{ simp only [pythagorean_triple, hk, zero_mul, zero_add], },
{ calc (k * x) * (k * x) + (k * y) * (k * y)
= k ^ 2 * (x * x + y * y) : by ring
... = k ^ 2 * (z * z) : by rw h.eq
... = (k * z) * (k * z) : by ring }
end
omit h
/-- `(k*x, k*y, k*z)` is a Pythagorean triple if and only if
`(x, y, z)` is also a triple. -/
lemma mul_iff (k : ℤ) (hk : k ≠ 0) :
pythagorean_triple (k * x) (k * y) (k * z) ↔ pythagorean_triple x y z :=
begin
refine ⟨_, λ h, h.mul k⟩,
simp only [pythagorean_triple],
intro h,
rw ← mul_left_inj' (mul_ne_zero hk hk),
convert h using 1; ring,
end
include h
/-- A pythogorean triple `x, y, z` is “classified” if there exist integers `k, m, n` such that either
* `x = k * (m ^ 2 - n ^ 2)` and `y = k * (2 * m * n)`, or
* `x = k * (2 * m * n)` and `y = k * (m ^ 2 - n ^ 2)`. -/
@[nolint unused_arguments] def is_classified := ∃ (k m n : ℤ),
((x = k * (m ^ 2 - n ^ 2) ∧ y = k * (2 * m * n))
∨ (x = k * (2 * m * n) ∧ y = k * (m ^ 2 - n ^ 2)))
∧ int.gcd m n = 1
/-- A primitive pythogorean triple `x, y, z` is a pythagorean triple with `x` and `y` coprime.
Such a triple is “primitively classified” if there exist coprime integers `m, n` such that either
* `x = m ^ 2 - n ^ 2` and `y = 2 * m * n`, or
* `x = 2 * m * n` and `y = m ^ 2 - n ^ 2`.
-/
@[nolint unused_arguments] def is_primitive_classified := ∃ (m n : ℤ),
((x = m ^ 2 - n ^ 2 ∧ y = 2 * m * n)
∨ (x = 2 * m * n ∧ y = m ^ 2 - n ^ 2))
∧ int.gcd m n = 1
∧ ((m % 2 = 0 ∧ n % 2 = 1) ∨ (m % 2 = 1 ∧ n % 2 = 0))
lemma mul_is_classified (k : ℤ) (hc : h.is_classified) : (h.mul k).is_classified :=
begin
obtain ⟨l, m, n, ⟨⟨rfl, rfl⟩ | ⟨rfl, rfl⟩, co⟩⟩ := hc,
{ use [k * l, m, n], apply and.intro _ co, left, split; ring },
{ use [k * l, m, n], apply and.intro _ co, right, split; ring },
end
lemma even_odd_of_coprime (hc : int.gcd x y = 1) :
(x % 2 = 0 ∧ y % 2 = 1) ∨ (x % 2 = 1 ∧ y % 2 = 0) :=
begin
cases int.mod_two_eq_zero_or_one x with hx hx;
cases int.mod_two_eq_zero_or_one y with hy hy,
{ -- x even, y even
exfalso,
apply nat.not_coprime_of_dvd_of_dvd (dec_trivial : 1 < 2) _ _ hc,
{ apply int.dvd_nat_abs_of_of_nat_dvd, apply int.dvd_of_mod_eq_zero hx },
{ apply int.dvd_nat_abs_of_of_nat_dvd, apply int.dvd_of_mod_eq_zero hy } },
{ left, exact ⟨hx, hy⟩ }, -- x even, y odd
{ right, exact ⟨hx, hy⟩ }, -- x odd, y even
{ -- x odd, y odd
exfalso,
obtain ⟨x0, y0, rfl, rfl⟩ : ∃ x0 y0, x = x0* 2 + 1 ∧ y = y0 * 2 + 1,
{ cases exists_eq_mul_left_of_dvd (int.dvd_sub_of_mod_eq hx) with x0 hx2,
cases exists_eq_mul_left_of_dvd (int.dvd_sub_of_mod_eq hy) with y0 hy2,
rw sub_eq_iff_eq_add at hx2 hy2, exact ⟨x0, y0, hx2, hy2⟩ },
have hz : (z * z) % 4 = 2,
{ rw show z * z = 4 * (x0 * x0 + x0 + y0 * y0 + y0) + 2, by { rw ← h.eq, ring },
simp only [int.add_mod, int.mul_mod_right, int.mod_mod, zero_add], refl },
have : ∀ (k : ℤ), 0 ≤ k → k < 4 → k * k % 4 ≠ 2 := dec_trivial,
have h4 : (4 : ℤ) ≠ 0 := dec_trivial,
apply this (z % 4) (int.mod_nonneg z h4) (int.mod_lt z h4),
rwa [← int.mul_mod] },
end
lemma gcd_dvd : (int.gcd x y : ℤ) ∣ z :=
begin
by_cases h0 : int.gcd x y = 0,
{ have hx : x = 0, { apply int.nat_abs_eq_zero.mp, apply nat.eq_zero_of_gcd_eq_zero_left h0 },
have hy : y = 0, { apply int.nat_abs_eq_zero.mp, apply nat.eq_zero_of_gcd_eq_zero_right h0 },
have hz : z = 0,
{ simpa only [pythagorean_triple, hx, hy, add_zero, zero_eq_mul, mul_zero, or_self] using h },
simp only [hz, dvd_zero], },
obtain ⟨k, x0, y0, k0, h2, rfl, rfl⟩ :
∃ (k : ℕ) x0 y0, 0 < k ∧ int.gcd x0 y0 = 1 ∧ x = x0 * k ∧ y = y0 * k :=
int.exists_gcd_one' (nat.pos_of_ne_zero h0),
rw [int.gcd_mul_right, h2, int.nat_abs_of_nat, one_mul],
rw [← int.pow_dvd_pow_iff (dec_trivial : 0 < 2), pow_two z, ← h.eq],
rw (by ring : x0 * k * (x0 * k) + y0 * k * (y0 * k) = k ^ 2 * (x0 * x0 + y0 * y0)),
exact dvd_mul_right _ _
end
lemma normalize : pythagorean_triple (x / int.gcd x y) (y / int.gcd x y) (z / int.gcd x y) :=
begin
by_cases h0 : int.gcd x y = 0,
{ have hx : x = 0, { apply int.nat_abs_eq_zero.mp, apply nat.eq_zero_of_gcd_eq_zero_left h0 },
have hy : y = 0, { apply int.nat_abs_eq_zero.mp, apply nat.eq_zero_of_gcd_eq_zero_right h0 },
have hz : z = 0,
{ simpa only [pythagorean_triple, hx, hy, add_zero, zero_eq_mul, mul_zero, or_self] using h },
simp only [hx, hy, hz, int.zero_div], exact zero },
rcases h.gcd_dvd with ⟨z0, rfl⟩,
obtain ⟨k, x0, y0, k0, h2, rfl, rfl⟩ :
∃ (k : ℕ) x0 y0, 0 < k ∧ int.gcd x0 y0 = 1 ∧ x = x0 * k ∧ y = y0 * k :=
int.exists_gcd_one' (nat.pos_of_ne_zero h0),
have hk : (k : ℤ) ≠ 0, { norm_cast, rwa nat.pos_iff_ne_zero at k0 },
rw [int.gcd_mul_right, h2, int.nat_abs_of_nat, one_mul] at h ⊢,
rw [mul_comm x0, mul_comm y0, mul_iff k hk] at h,
rwa [int.mul_div_cancel _ hk, int.mul_div_cancel _ hk, int.mul_div_cancel_left _ hk],
end
lemma is_classified_of_is_primitive_classified (hp : h.is_primitive_classified) :
h.is_classified :=
begin
obtain ⟨m, n, H⟩ := hp,
use [1, m, n],
rcases H with ⟨⟨rfl, rfl⟩ | ⟨rfl, rfl⟩, co, pp⟩;
{ apply and.intro _ co, rw one_mul, rw one_mul, tauto }
end
lemma is_classified_of_normalize_is_primitive_classified (hc : h.normalize.is_primitive_classified) :
h.is_classified :=
begin
convert h.normalize.mul_is_classified (int.gcd x y)
(is_classified_of_is_primitive_classified h.normalize hc);
rw int.mul_div_cancel',
{ exact int.gcd_dvd_left x y },
{ exact int.gcd_dvd_right x y },
{ exact h.gcd_dvd }
end
lemma ne_zero_of_coprime (hc : int.gcd x y = 1) : z ≠ 0 :=
begin
suffices : 0 < z * z, { rintro rfl, norm_num at this },
rw [← h.eq, ← pow_two, ← pow_two],
have hc' : int.gcd x y ≠ 0, { rw hc, exact one_ne_zero },
cases int.ne_zero_of_gcd hc' with hxz hyz,
{ apply lt_add_of_pos_of_le (pow_two_pos_of_ne_zero x hxz) (pow_two_nonneg y) },
{ apply lt_add_of_le_of_pos (pow_two_nonneg x) (pow_two_pos_of_ne_zero y hyz) }
end
lemma is_primitive_classified_of_coprime_of_zero_left (hc : int.gcd x y = 1) (hx : x = 0) :
h.is_primitive_classified :=
begin
subst x,
change nat.gcd 0 (int.nat_abs y) = 1 at hc,
rw [nat.gcd_zero_left (int.nat_abs y)] at hc,
cases int.nat_abs_eq y with hy hy,
{ use [1, 0], rw [hy, hc, int.gcd_zero_right], norm_num },
{ use [0, 1], rw [hy, hc, int.gcd_zero_left], norm_num }
end
lemma coprime_of_coprime (hc : int.gcd x y = 1) : int.gcd y z = 1 :=
begin
by_contradiction H,
obtain ⟨p, hp, hpy, hpz⟩ := nat.prime.not_coprime_iff_dvd.mp H,
apply hp.not_dvd_one,
rw [← hc],
apply nat.dvd_gcd (int.prime.dvd_nat_abs_of_coe_dvd_pow_two hp _ _) hpy,
rw [pow_two, eq_sub_of_add_eq h],
rw [← int.coe_nat_dvd_left] at hpy hpz,
exact dvd_sub (dvd_mul_of_dvd_left (hpz) _) (dvd_mul_of_dvd_left (hpy) _),
end
end pythagorean_triple
section circle_equiv_gen
/-!
### A parametrization of the unit circle
For the classification of pythogorean triples, we will use a parametrization of the unit circle.
-/
variables {K : Type*} [field K]
/-- A parameterization of the unit circle that is useful for classifying Pythagorean triples.
(To be applied in the case where `K = ℚ`.) -/
def circle_equiv_gen (hk : ∀ x : K, 1 + x^2 ≠ 0) :
K ≃ {p : K × K // p.1^2 + p.2^2 = 1 ∧ p.2 ≠ -1} :=
{ to_fun := λ x, ⟨⟨2 * x / (1 + x^2), (1 - x^2) / (1 + x^2)⟩,
by { field_simp [hk x, div_pow], ring },
begin
simp only [ne.def, div_eq_iff (hk x), ←neg_mul_eq_neg_mul, one_mul, neg_add,
sub_eq_add_neg, add_left_inj],
simpa only [eq_neg_iff_add_eq_zero, one_pow] using hk 1,
end⟩,
inv_fun := λ p, (p : K × K).1 / ((p : K × K).2 + 1),
left_inv := λ x,
begin
have h2 : (1 + 1 : K) = 2 := rfl,
have h3 : (2 : K) ≠ 0, { convert hk 1, rw [one_pow 2, h2] },
field_simp [hk x, h2, h3, add_assoc, add_comm, add_sub_cancel'_right, mul_comm],
end,
right_inv := λ ⟨⟨x, y⟩, hxy, hy⟩,
begin
change x ^ 2 + y ^ 2 = 1 at hxy,
have h2 : y + 1 ≠ 0, { apply mt eq_neg_of_add_eq_zero, exact hy },
have h3 : (y + 1) ^ 2 + x ^ 2 = 2 * (y + 1),
{ rw [(add_neg_eq_iff_eq_add.mpr hxy.symm).symm], ring },
have h4 : (2 : K) ≠ 0, { convert hk 1, rw one_pow 2, refl },
simp only [prod.mk.inj_iff, subtype.mk_eq_mk],
split,
{ field_simp [h2, h3, h4], ring },
{ field_simp [h2, h3, h4], rw [← add_neg_eq_iff_eq_add.mpr hxy.symm], ring }
end }
@[simp] lemma circle_equiv_apply (hk : ∀ x : K, 1 + x^2 ≠ 0) (x : K) :
(circle_equiv_gen hk x : K × K) = ⟨2 * x / (1 + x^2), (1 - x^2) / (1 + x^2)⟩ := rfl
@[simp] lemma circle_equiv_symm_apply (hk : ∀ x : K, 1 + x^2 ≠ 0)
(v : {p : K × K // p.1^2 + p.2^2 = 1 ∧ p.2 ≠ -1}) :
(circle_equiv_gen hk).symm v = (v : K × K).1 / ((v : K × K).2 + 1) := rfl
end circle_equiv_gen
private lemma coprime_pow_two_sub_pow_two_add_of_even_odd {m n : ℤ} (h : int.gcd m n = 1)
(hm : m % 2 = 0) (hn : n % 2 = 1) :
int.gcd (m ^ 2 - n ^ 2) (m ^ 2 + n ^ 2) = 1 :=
begin
by_contradiction H,
obtain ⟨p, hp, hp1, hp2⟩ := nat.prime.not_coprime_iff_dvd.mp H,
rw ← int.coe_nat_dvd_left at hp1 hp2,
have h2m : (p : ℤ) ∣ 2 * m ^ 2, { convert dvd_add hp2 hp1, ring },
have h2n : (p : ℤ) ∣ 2 * n ^ 2, { convert dvd_sub hp2 hp1, ring },
have hmc : p = 2 ∨ p ∣ int.nat_abs m, { exact prime_two_or_dvd_of_dvd_two_mul_pow_self_two hp h2m },
have hnc : p = 2 ∨ p ∣ int.nat_abs n, { exact prime_two_or_dvd_of_dvd_two_mul_pow_self_two hp h2n },
by_cases h2 : p = 2,
{ have h3 : (m ^ 2 + n ^ 2) % 2 = 1, { norm_num [pow_two, int.add_mod, int.mul_mod, hm, hn] },
have h4 : (m ^ 2 + n ^ 2) % 2 = 0, { apply int.mod_eq_zero_of_dvd, rwa h2 at hp2 },
rw h4 at h3, exact zero_ne_one h3 },
{ apply hp.not_dvd_one,
rw ← h,
exact nat.dvd_gcd (or.resolve_left hmc h2) (or.resolve_left hnc h2), }
end
private lemma coprime_pow_two_sub_pow_two_add_of_odd_even {m n : ℤ} (h : int.gcd m n = 1)
(hm : m % 2 = 1) (hn : n % 2 = 0):
int.gcd (m ^ 2 - n ^ 2) (m ^ 2 + n ^ 2) = 1 :=
begin
rw [int.gcd, ← int.nat_abs_neg (m ^ 2 - n ^ 2)],
rw [(by ring : -(m ^ 2 - n ^ 2) = n ^ 2 - m ^ 2), add_comm],
apply coprime_pow_two_sub_pow_two_add_of_even_odd _ hn hm, rwa [int.gcd_comm],
end
private lemma coprime_pow_two_sub_mul_of_even_odd {m n : ℤ} (h : int.gcd m n = 1)
(hm : m % 2 = 0) (hn : n % 2 = 1) :
int.gcd (m ^ 2 - n ^ 2) (2 * m * n) = 1 :=
begin
by_contradiction H,
obtain ⟨p, hp, hp1, hp2⟩ := nat.prime.not_coprime_iff_dvd.mp H,
rw ← int.coe_nat_dvd_left at hp1 hp2,
have hnp : ¬ (p : ℤ) ∣ int.gcd m n,
{ rw h, norm_cast, exact mt nat.dvd_one.mp (nat.prime.ne_one hp) },
cases int.prime.dvd_mul hp hp2 with hp2m hpn,
{ rw int.nat_abs_mul at hp2m,
cases (nat.prime.dvd_mul hp).mp hp2m with hp2 hpm,
{ have hp2' : p = 2, { exact le_antisymm (nat.le_of_dvd zero_lt_two hp2) (nat.prime.two_le hp) },
revert hp1, rw hp2',
apply mt int.mod_eq_zero_of_dvd,
norm_num [pow_two, int.sub_mod, int.mul_mod, hm, hn],
},
apply mt (int.dvd_gcd (int.coe_nat_dvd_left.mpr hpm)) hnp,
apply (or_self _).mp, apply int.prime.dvd_mul' hp,
rw (by ring : n * n = - (m ^ 2 - n ^ 2) + m * m),
apply dvd_add (dvd_neg_of_dvd hp1),
exact dvd_mul_of_dvd_left (int.coe_nat_dvd_left.mpr hpm) m
},
rw int.gcd_comm at hnp,
apply mt (int.dvd_gcd (int.coe_nat_dvd_left.mpr hpn)) hnp,
apply (or_self _).mp, apply int.prime.dvd_mul' hp,
rw (by ring : m * m = (m ^ 2 - n ^ 2) + n * n),
apply dvd_add hp1,
exact dvd_mul_of_dvd_left (int.coe_nat_dvd_left.mpr hpn) n
end
private lemma coprime_pow_two_sub_mul_of_odd_even {m n : ℤ} (h : int.gcd m n = 1)
(hm : m % 2 = 1) (hn : n % 2 = 0) :
int.gcd (m ^ 2 - n ^ 2) (2 * m * n) = 1 :=
begin
rw [int.gcd, ← int.nat_abs_neg (m ^ 2 - n ^ 2)],
rw [(by ring : 2 * m * n = 2 * n * m), (by ring : -(m ^ 2 - n ^ 2) = n ^ 2 - m ^ 2)],
apply coprime_pow_two_sub_mul_of_even_odd _ hn hm, rwa [int.gcd_comm]
end
private lemma coprime_pow_two_sub_mul {m n : ℤ} (h : int.gcd m n = 1)
(hmn : (m % 2 = 0 ∧ n % 2 = 1) ∨ (m % 2 = 1 ∧ n % 2 = 0)) :
int.gcd (m ^ 2 - n ^ 2) (2 * m * n) = 1 :=
begin
cases hmn with h1 h2,
{ exact coprime_pow_two_sub_mul_of_even_odd h h1.left h1.right },
{ exact coprime_pow_two_sub_mul_of_odd_even h h2.left h2.right }
end
private lemma coprime_pow_two_sub_pow_two_sum_of_odd_odd {m n : ℤ} (h : int.gcd m n = 1)
(hm : m % 2 = 1) (hn : n % 2 = 1) :
2 ∣ m ^ 2 + n ^ 2
∧ 2 ∣ m ^ 2 - n ^ 2
∧ ((m ^ 2 - n ^ 2) / 2) % 2 = 0
∧ int.gcd ((m ^ 2 - n ^ 2) / 2) ((m ^ 2 + n ^ 2) / 2) = 1 :=
begin
cases exists_eq_mul_left_of_dvd (int.dvd_sub_of_mod_eq hm) with m0 hm2,
cases exists_eq_mul_left_of_dvd (int.dvd_sub_of_mod_eq hn) with n0 hn2,
rw sub_eq_iff_eq_add at hm2 hn2, subst m, subst n,
have h1 : (m0 * 2 + 1) ^ 2 + (n0 * 2 + 1) ^ 2 = 2 * (2 * (m0 ^ 2 + n0 ^ 2 + m0 + n0) + 1),
by ring_exp,
have h2 : (m0 * 2 + 1) ^ 2 - (n0 * 2 + 1) ^ 2 = 2 * (2 * (m0 ^ 2 - n0 ^ 2 + m0 - n0)),
by ring_exp,
have h3 : ((m0 * 2 + 1) ^ 2 - (n0 * 2 + 1) ^ 2) / 2 % 2 = 0,
{ rw [h2, int.mul_div_cancel_left, int.mul_mod_right], exact dec_trivial },
refine ⟨⟨_, h1⟩, ⟨_, h2⟩, h3, _⟩,
have h20 : (2:ℤ) ≠ 0 := dec_trivial,
rw [h1, h2, int.mul_div_cancel_left _ h20, int.mul_div_cancel_left _ h20],
by_contra h4,
obtain ⟨p, hp, hp1, hp2⟩ := nat.prime.not_coprime_iff_dvd.mp h4,
apply hp.not_dvd_one,
rw ← h,
rw ← int.coe_nat_dvd_left at hp1 hp2,
apply nat.dvd_gcd,
{ apply int.prime.dvd_nat_abs_of_coe_dvd_pow_two hp,
convert dvd_add hp1 hp2, ring_exp },
{ apply int.prime.dvd_nat_abs_of_coe_dvd_pow_two hp,
convert dvd_sub hp2 hp1, ring_exp },
end
namespace pythagorean_triple
variables {x y z : ℤ} (h : pythagorean_triple x y z)
include h
lemma is_primitive_classified_aux (hc : x.gcd y = 1) (hzpos : 0 < z)
{m n : ℤ} (hm2n2 : 0 < m ^ 2 + n ^ 2)
(hv2 : (x : ℚ) / z = 2 * m * n / (m ^ 2 + n ^ 2))
(hw2 : (y : ℚ) / z = (m ^ 2 - n ^ 2) / (m ^ 2 + n ^ 2))
(H : int.gcd (m ^ 2 - n ^ 2) (m ^ 2 + n ^ 2) = 1)
(co : int.gcd m n = 1)
(pp : (m % 2 = 0 ∧ n % 2 = 1) ∨ (m % 2 = 1 ∧ n % 2 = 0)):
h.is_primitive_classified :=
begin
have hz : z ≠ 0, apply ne_of_gt hzpos,
have h2 : y = m ^ 2 - n ^ 2 ∧ z = m ^ 2 + n ^ 2,
{ apply rat.div_int_inj hzpos hm2n2 (h.coprime_of_coprime hc) H, rw [hw2], norm_cast },
use [m, n], apply and.intro _ (and.intro co pp), right,
refine ⟨_, h2.left⟩,
rw [← rat.coe_int_inj _ _, ← div_left_inj' ((mt (rat.coe_int_inj z 0).mp) hz), hv2, h2.right],
norm_cast
end
theorem is_primitive_classified_of_coprime_of_odd_of_pos
(hc : int.gcd x y = 1) (hyo : y % 2 = 1) (hzpos : 0 < z) :
h.is_primitive_classified :=
begin
by_cases h0 : x = 0, { exact h.is_primitive_classified_of_coprime_of_zero_left hc h0 },
let v := (x : ℚ) / z,
let w := (y : ℚ) / z,
have hz : z ≠ 0, apply ne_of_gt hzpos,
have hq : v ^ 2 + w ^ 2 = 1,
{ field_simp [hz, pow_two], norm_cast, exact h },
have hvz : v ≠ 0, { field_simp [hz], exact h0 },
have hw1 : w ≠ -1,
{ contrapose! hvz with hw1, rw [hw1, neg_square, one_pow, add_left_eq_self] at hq, exact pow_eq_zero hq, },
have hQ : ∀ x : ℚ, 1 + x^2 ≠ 0,
{ intro q, apply ne_of_gt, exact lt_add_of_pos_of_le zero_lt_one (pow_two_nonneg q) },
have hp : (⟨v, w⟩ : ℚ × ℚ) ∈ {p : ℚ × ℚ | p.1^2 + p.2^2 = 1 ∧ p.2 ≠ -1} := ⟨hq, hw1⟩,
let q := (circle_equiv_gen hQ).symm ⟨⟨v, w⟩, hp⟩,
have ht4 : v = 2 * q / (1 + q ^ 2) ∧ w = (1 - q ^ 2) / (1 + q ^ 2),
{ apply prod.mk.inj,
have := ((circle_equiv_gen hQ).apply_symm_apply ⟨⟨v, w⟩, hp⟩).symm,
exact congr_arg subtype.val this, },
let m := (q.denom : ℤ),
let n := q.num,
have hm0 : m ≠ 0, { norm_cast, apply rat.denom_ne_zero q },
have hq2 : q = n / m, { rw [int.cast_coe_nat], exact (rat.cast_id q).symm },
have hm2n2 : 0 < m ^ 2 + n ^ 2,
{ apply lt_add_of_pos_of_le _ (pow_two_nonneg n),
exact lt_of_le_of_ne (pow_two_nonneg m) (ne.symm (pow_ne_zero 2 hm0)) },
have hw2 : w = (m ^ 2 - n ^ 2) / (m ^ 2 + n ^ 2),
{ rw [ht4.2, hq2], field_simp [hm2n2, (rat.denom_ne_zero q)] },
have hm2n20 : (m : ℚ) ^ 2 + (n : ℚ) ^ 2 ≠ 0,
{ norm_cast, simpa only [int.coe_nat_pow] using ne_of_gt hm2n2 },
have hv2 : v = 2 * m * n / (m ^ 2 + n ^ 2),
{ apply eq.symm, apply (div_eq_iff hm2n20).mpr, rw [ht4.1], field_simp [hQ q],
rw [hq2] {occs := occurrences.pos [2, 3]}, field_simp [rat.denom_ne_zero q], ring },
have hnmcp : int.gcd n m = 1 := q.cop,
have hmncp : int.gcd m n = 1, { rw int.gcd_comm, exact hnmcp },
cases int.mod_two_eq_zero_or_one m with hm2 hm2;
cases int.mod_two_eq_zero_or_one n with hn2 hn2,
{ -- m even, n even
exfalso,
have h1 : 2 ∣ (int.gcd n m : ℤ),
{ exact int.dvd_gcd (int.dvd_of_mod_eq_zero hn2) (int.dvd_of_mod_eq_zero hm2) },
rw hnmcp at h1, revert h1, norm_num },
{ -- m even, n odd
apply h.is_primitive_classified_aux hc hzpos hm2n2 hv2 hw2 _ hmncp,
{ apply or.intro_left, exact and.intro hm2 hn2 },
{ apply coprime_pow_two_sub_pow_two_add_of_even_odd hmncp hm2 hn2 } },
{ -- m odd, n even
apply h.is_primitive_classified_aux hc hzpos hm2n2 hv2 hw2 _ hmncp,
{ apply or.intro_right, exact and.intro hm2 hn2 },
apply coprime_pow_two_sub_pow_two_add_of_odd_even hmncp hm2 hn2 },
{ -- m odd, n odd
exfalso,
have h1 : 2 ∣ m ^ 2 + n ^ 2 ∧ 2 ∣ m ^ 2 - n ^ 2
∧ ((m ^ 2 - n ^ 2) / 2) % 2 = 0 ∧ int.gcd ((m ^ 2 - n ^ 2) / 2) ((m ^ 2 + n ^ 2) / 2) = 1,
{ exact coprime_pow_two_sub_pow_two_sum_of_odd_odd hmncp hm2 hn2 },
have h2 : y = (m ^ 2 - n ^ 2) / 2 ∧ z = (m ^ 2 + n ^ 2) / 2,
{ apply rat.div_int_inj hzpos _ (h.coprime_of_coprime hc) h1.2.2.2,
{ show w = _, rw [←rat.mk_eq_div, ←(rat.div_mk_div_cancel_left (by norm_num : (2 : ℤ) ≠ 0))],
rw [int.div_mul_cancel h1.1, int.div_mul_cancel h1.2.1, hw2], norm_cast },
{ apply (mul_lt_mul_right (by norm_num : 0 < (2 : ℤ))).mp,
rw [int.div_mul_cancel h1.1, zero_mul], exact hm2n2 } },
rw [h2.1, h1.2.2.1] at hyo,
revert hyo,
norm_num }
end
theorem is_primitive_classified_of_coprime_of_pos (hc : int.gcd x y = 1) (hzpos : 0 < z):
h.is_primitive_classified :=
begin
cases h.even_odd_of_coprime hc with h1 h2,
{ exact (h.is_primitive_classified_of_coprime_of_odd_of_pos hc h1.right hzpos) },
rw int.gcd_comm at hc,
obtain ⟨m, n, H⟩ := (h.symm.is_primitive_classified_of_coprime_of_odd_of_pos hc h2.left hzpos),
use [m, n], tauto
end
theorem is_primitive_classified_of_coprime (hc : int.gcd x y = 1) : h.is_primitive_classified :=
begin
by_cases hz : 0 < z,
{ exact h.is_primitive_classified_of_coprime_of_pos hc hz },
have h' : pythagorean_triple x y (-z),
{ simpa [pythagorean_triple, neg_mul_neg] using h.eq, },
apply h'.is_primitive_classified_of_coprime_of_pos hc,
apply lt_of_le_of_ne _ (h'.ne_zero_of_coprime hc).symm,
exact le_neg.mp (not_lt.mp hz)
end
theorem classified : h.is_classified :=
begin
by_cases h0 : int.gcd x y = 0,
{ have hx : x = 0, { apply int.nat_abs_eq_zero.mp, apply nat.eq_zero_of_gcd_eq_zero_left h0 },
have hy : y = 0, { apply int.nat_abs_eq_zero.mp, apply nat.eq_zero_of_gcd_eq_zero_right h0 },
use [0, 1, 0], norm_num [hx, hy], },
apply h.is_classified_of_normalize_is_primitive_classified,
apply h.normalize.is_primitive_classified_of_coprime,
apply int.gcd_div_gcd_div_gcd (nat.pos_of_ne_zero h0),
end
omit h
theorem coprime_classification :
pythagorean_triple x y z ∧ int.gcd x y = 1 ↔
∃ m n, ((x = m ^ 2 - n ^ 2 ∧ y = 2 * m * n) ∨
(x = 2 * m * n ∧ y = m ^ 2 - n ^ 2))
∧ (z = m ^ 2 + n ^ 2 ∨ z = - (m ^ 2 + n ^ 2))
∧ int.gcd m n = 1
∧ ((m % 2 = 0 ∧ n % 2 = 1) ∨ (m % 2 = 1 ∧ n % 2 = 0)) :=
begin
split,
{ intro h,
obtain ⟨m, n, H⟩ := h.left.is_primitive_classified_of_coprime h.right,
use [m, n],
rcases H with ⟨⟨rfl, rfl⟩ | ⟨rfl, rfl⟩, co, pp⟩,
{ refine ⟨or.inl ⟨rfl, rfl⟩, _, co, pp⟩,
have : z ^ 2 = (m ^ 2 + n ^ 2) ^ 2,
{ rw [pow_two, ← h.left.eq], ring },
simpa using eq_or_eq_neg_of_pow_two_eq_pow_two _ _ this },
{ refine ⟨or.inr ⟨rfl, rfl⟩, _, co, pp⟩,
have : z ^ 2 = (m ^ 2 + n ^ 2) ^ 2,
{ rw [pow_two, ← h.left.eq], ring },
simpa using eq_or_eq_neg_of_pow_two_eq_pow_two _ _ this } },
{ delta pythagorean_triple,
rintro ⟨m, n, ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩, rfl | rfl, co, pp⟩;
{ split, { ring }, exact coprime_pow_two_sub_mul co pp }
<|>
{ split, { ring }, rw int.gcd_comm, exact coprime_pow_two_sub_mul co pp } }
end
/-- by assuming `x` is odd and `z` is positive we get a slightly more precise classification of
the pythagorean triple `x ^ 2 + y ^ 2 = z ^ 2`-/
theorem coprime_classification' {x y z : ℤ} (h : pythagorean_triple x y z)
(h_coprime : int.gcd x y = 1) (h_parity : x % 2 = 1) (h_pos : 0 < z) :
∃ m n, x = m ^ 2 - n ^ 2
∧ y = 2 * m * n
∧ z = m ^ 2 + n ^ 2
∧ int.gcd m n = 1
∧ ((m % 2 = 0 ∧ n % 2 = 1) ∨ (m % 2 = 1 ∧ n % 2 = 0))
∧ 0 ≤ m :=
begin
obtain ⟨m, n, ht1, ht2, ht3, ht4⟩ :=
pythagorean_triple.coprime_classification.mp (and.intro h h_coprime),
cases le_or_lt 0 m with hm hm,
{ use [m, n],
cases ht1 with h_odd h_even,
{ apply and.intro h_odd.1,
apply and.intro h_odd.2,
cases ht2 with h_pos h_neg,
{ apply and.intro h_pos (and.intro ht3 (and.intro ht4 hm)) },
{ exfalso, revert h_pos, rw h_neg,
exact imp_false.mpr (not_lt.mpr (neg_nonpos.mpr (add_nonneg (pow_two_nonneg m)
(pow_two_nonneg n)))) } },
exfalso,
rcases h_even with ⟨rfl, -⟩,
rw [mul_assoc, int.mul_mod_right] at h_parity,
exact zero_ne_one h_parity },
{ use [-m, -n],
cases ht1 with h_odd h_even,
{ rw [neg_square m],
rw [neg_square n],
apply and.intro h_odd.1,
split, { rw h_odd.2, ring },
cases ht2 with h_pos h_neg,
{ apply and.intro h_pos,
split,
{ delta int.gcd, rw [int.nat_abs_neg, int.nat_abs_neg], exact ht3 },
{ rw [int.neg_mod_two, int.neg_mod_two],
apply and.intro ht4, linarith } },
{ exfalso, revert h_pos, rw h_neg,
exact imp_false.mpr (not_lt.mpr (neg_nonpos.mpr (add_nonneg (pow_two_nonneg m)
(pow_two_nonneg n)))) } },
exfalso,
rcases h_even with ⟨rfl, -⟩,
rw [mul_assoc, int.mul_mod_right] at h_parity,
exact zero_ne_one h_parity }
end
theorem classification :
pythagorean_triple x y z ↔
∃ k m n, ((x = k * (m ^ 2 - n ^ 2) ∧ y = k * (2 * m * n)) ∨
(x = k * (2 * m * n) ∧ y = k * (m ^ 2 - n ^ 2)))
∧ (z = k * (m ^ 2 + n ^ 2) ∨ z = - k * (m ^ 2 + n ^ 2)) :=
begin
split,
{ intro h,
obtain ⟨k, m, n, H⟩ := h.classified,
use [k, m, n],
rcases H with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩,
{ refine ⟨or.inl ⟨rfl, rfl⟩, _⟩,
have : z ^ 2 = (k * (m ^ 2 + n ^ 2)) ^ 2,
{ rw [pow_two, ← h.eq], ring },
simpa using eq_or_eq_neg_of_pow_two_eq_pow_two _ _ this },
{ refine ⟨or.inr ⟨rfl, rfl⟩, _⟩,
have : z ^ 2 = (k * (m ^ 2 + n ^ 2)) ^ 2,
{ rw [pow_two, ← h.eq], ring },
simpa using eq_or_eq_neg_of_pow_two_eq_pow_two _ _ this } },
{ rintro ⟨k, m, n, ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩, rfl | rfl⟩;
delta pythagorean_triple; ring }
end
end pythagorean_triple
|
af94e7ab9e36466114df2d4051f919045a463631 | a3db153d66921f9d5a199b60b52ab3c21cf0e023 | /src/ordered_field.lean | d4bc5eb0e25e49d2a20b0ec1293969ef9cd9ec04 | [
"MIT"
] | permissive | metalogical/sia-lean | 3bd87a76b3ee6d910cbf182eb2a0e6d9eca3c056 | f8e354dd2ff6c09c4e001c1f80f6112c62da8592 | refs/heads/master | 1,629,775,811,523 | 1,512,684,874,000 | 1,512,684,874,000 | 106,763,566 | 4 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,190 | lean | import .util
universe u
variable {R : Type u}
class st_order R extends has_lt R := -- strict total order
(lt_irrefl : forall a : R, not (a < a))
(lt_trans : forall {a b c : R}, a < b -> b < c -> a < c)
(ne_lt : forall {a b : R}, a != b -> a < b \/ b < a)
-- Bell 1.2, but we use it as an axiom for better structure
(lt_far : forall {a b : R}, a < b -> forall c : R, a < c \/ c < b)
attribute [trans] st_order.lt_trans -- allow use of transitivity in calc proofs
-- ordered field
class st_ordered_field R extends field R, st_order R :=
(lt_zero_one : (0: R) < 1)
(lt_add_left : forall {a b : R}, a < b -> forall c : R, c + a < c + b)
(lt_mul_pos_left : forall {a b c : R}, 0 < c -> a < b -> c * a < c * b)
-- lemmas & theorems regarding ordering
namespace st_order
variable [st_order R]
variables {a b c : R}
-- intervals
definition lt_interval [has_lt R] (a: R) (b: R) : set R := fun r: R, a < r /\ r < b
definition le_interval [has_le R] (a: R) (b: R) : set R := fun r: R, a <= r /\ r <= b
notation `[` a `...` b `]` := lt_interval a b
notation `[[` a `...` b `]]` := le_interval a b
-- non-strict total order
instance has_le : has_le R := {
le := fun x, fun y, not (y < x)
}
attribute [reducible] has_le.le
lemma lt_ne : a < b -> a != b :=
assume a_lt_b,
assume bad_a_eq_b: a = b,
st_order.lt_irrefl a (calc
a < b : a_lt_b
... = a : by rw bad_a_eq_b
)
lemma le_refl : a <= a := lt_irrefl a
@[trans]
lemma le_trans : a <= b -> b <= c -> a <= c :=
assume a_le_b b_le_c,
assume bad_c_lt_a,
have bad_or: c < b \/ b < a, from (st_order.lt_far bad_c_lt_a b),
or.elim bad_or b_le_c a_le_b
@[trans]
lemma le_lt_trans : a <= b -> b < c -> a < c :=
assume le,
assume lt,
have a != c, from
assume bad,
have b < a, by {rw bad, assumption},
le this,
have a < c \/ c < a, from ne_lt this,
have right_bad : not (c < a), from
assume bad,
have b < a, from lt_trans lt bad,
le this,
or.resolve_right this right_bad
end st_order
namespace st_ordered_field
variable [st_ordered_field R]
variables {a b c : R}
lemma zero_one_far : 0 < a \/ a < 1 := st_order.lt_far (lt_zero_one R) a
lemma lt_neg_flip : a < b -> -b < -a :=
assume lt: a < b,
calc
-b = -b + (-a + a) : by simp
... = (-b + -a) + a : by rw add_assoc
... < (-b + -a) + b : lt_add_left lt _
... = -a : by simp
lemma one_div_pos_of_pos : 0 < c -> 0 < 1 / c :=
assume c_pos,
have c_ne_zero : c != 0, from ne.symm (st_order.lt_ne c_pos),
have 1 / c != 0, from one_div_ne_zero c_ne_zero,
have disj: 1 / c < 0 \/ 0 < 1 / c, from st_order.ne_lt this,
have left: 1 / c < 0 -> 0 < 1 / c, from
assume one_div_c_neg,
have (1: R) < 1, from (calc
1 = c * (1 / c) : by rw mul_div_cancel' _ c_ne_zero
... < c * 0 : lt_mul_pos_left c_pos one_div_c_neg
... = 0 : mul_zero _
... < (1: R) : (lt_zero_one R)
),
absurd this (st_order.lt_irrefl 1),
or.elim disj left (fun x, x)
lemma le_add_left : a <= b -> c + a <= c + b :=
assume a_le_b,
assume almost_bad,
have bad: b < a, from calc
b = -c + (c + b) : by simp
... < -c + (c + a) : lt_add_left almost_bad (-c)
... = a : by simp,
a_le_b bad
lemma le_zero_one : (0: R) <= 1 :=
assume bad : (1: R) < 0,
st_order.lt_irrefl 0 (calc
0 < 1 : lt_zero_one R
... < 0 : bad
)
lemma le_mul_pos_left : a <= b -> 0 <= c -> c * a <= c * b :=
assume a_le_b,
assume zero_le_c,
assume bc_lt_ac,
have c_ne_zero: c != 0, from
assume bad: c = 0,
have c * b = c * a, by simp [bad, mul_zero],
absurd this (st_order.lt_ne bc_lt_ac),
have c < 0 \/ 0 < c, from st_order.ne_lt c_ne_zero,
have right : not (0 < c), from
assume c_pos,
have b < a, from (calc
b = (1 / c) * c * b : by rw [one_div_mul_cancel c_ne_zero, one_mul]
... = (1 / c) * (c * b) : by simp [mul_assoc]
... < (1 / c) * (c * a) : lt_mul_pos_left (one_div_pos_of_pos c_pos) bc_lt_ac
... = (1 / c) * c * a : by simp [mul_assoc]
... = a : by rw [one_div_mul_cancel c_ne_zero, one_mul]
),
a_le_b this,
or.elim this zero_le_c right
lemma le_neg_flip : a <= b -> -b <= -a :=
assume a_le_b,
assume neg_a_lt_neg_b,
have b < a, from (calc
b = -(-b) : by rw neg_neg
... < -(-a) : lt_neg_flip neg_a_lt_neg_b
... = a : by rw neg_neg
),
absurd this a_le_b
end st_ordered_field
|
a8cbefa69c4e1c82d59d8cfe68f26464ecc18161 | e2fc96178628c7451e998a0db2b73877d0648be5 | /src/classes/unrestricted/closure_properties/concatenation.lean | a731e28a9fa9662da3491bdf4617e087cfae66fb | [
"BSD-2-Clause"
] | permissive | madvorak/grammars | cd324ae19b28f7b8be9c3ad010ef7bf0fabe5df2 | 1447343a45fcb7821070f1e20b57288d437323a6 | refs/heads/main | 1,692,383,644,884 | 1,692,032,429,000 | 1,692,032,429,000 | 453,948,141 | 7 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 93,156 | lean | import classes.unrestricted.basics.toolbox
import utilities.list_utils
section list_technicalities
variables {α β : Type}
lemma list_take_one_drop {l : list α} {i : ℕ} (hil : i < l.length) :
list.take 1 (list.drop i l) = [l.nth_le i hil] :=
begin
have l_split : l = list.take i l ++ list.drop i l,
{
rw list.take_append_drop,
},
rw list.nth_le_of_eq l_split,
rw list.nth_le_append_right,
{
have smaller_i : min i l.length = i,
{
exact min_eq_left (le_of_lt hil),
},
simp only [list.length_take, smaller_i, nat.sub_self],
have underscore : 0 < (list.drop i l).length,
{
finish,
},
cases (list.drop i l) with d x,
{
exfalso,
exact false_of_ne (ne_of_lt underscore),
},
{
refl,
},
},
{
apply list.length_take_le,
},
end
lemma list_drop_take_succ {l : list α} {i : ℕ} (hil : i < l.length) :
list.drop i (list.take (i + 1) l) = [l.nth_le i hil] :=
begin
rw list.drop_take,
apply list_take_one_drop,
end
lemma list_forall₂_nth_le {R : α → β → Prop} :
∀ {x : list α}, ∀ {y : list β}, list.forall₂ R x y →
∀ {i : ℕ}, ∀ i_lt_len_x : i < x.length, ∀ i_lt_len_y : i < y.length,
R (x.nth_le i i_lt_len_x) (y.nth_le i i_lt_len_y)
| [] [] := by { intros hyp i hx, exfalso, apply nat.not_lt_zero, exact hx, }
| [] (a₂::l₂) := by { intro hyp, exfalso, cases hyp, }
| (a₁::l₁) [] := by { intro hyp, exfalso, cases hyp, }
| (a₁::l₁) (a₂::l₂) :=
begin
intros ass i i_lt_len_x i_lt_len_y,
rw list.forall₂_cons at ass,
cases i,
{
unfold list.nth_le,
exact ass.1,
},
unfold list.nth_le,
apply list_forall₂_nth_le,
exact ass.2,
end
lemma list_filter_map_eq_of_map_eq_map_some {f : α → option β} :
∀ {x : list α}, ∀ {y : list β},
list.map f x = list.map option.some y →
list.filter_map f x = y
| [] [] := λ _, rfl
| (a₁::l₁) [] := by { intro hyp, exfalso, apply list.cons_ne_nil, exact hyp, }
| [] (a₂::l₂) := by { intro hyp, exfalso, apply list.cons_ne_nil, exact hyp.symm, }
| (a₁::l₁) (a₂::l₂) :=
begin
intro ass,
rw list.map at ass,
rw list.map at ass,
rw list.cons.inj_eq at ass,
rw list.filter_map_cons_some _ _ _ ass.1,
congr,
apply list_filter_map_eq_of_map_eq_map_some,
exact ass.2,
end
end list_technicalities
-- new nonterminal type
protected def nnn (T N₁ N₂ : Type) : Type :=
option (N₁ ⊕ N₂) ⊕ (T ⊕ T)
-- new symbol type
protected def nst (T N₁ N₂ : Type) : Type :=
symbol T (nnn T N₁ N₂)
variables {T : Type}
section the_construction
protected def wrap_symbol₁ {N₁ : Type} (N₂ : Type) : symbol T N₁ → nst T N₁ N₂
| (symbol.terminal t) := symbol.nonterminal (sum.inr (sum.inl t))
| (symbol.nonterminal n) := symbol.nonterminal (sum.inl (some (sum.inl n)))
protected def wrap_symbol₂ {N₂ : Type} (N₁ : Type) : symbol T N₂ → nst T N₁ N₂
| (symbol.terminal t) := symbol.nonterminal (sum.inr (sum.inr t))
| (symbol.nonterminal n) := symbol.nonterminal (sum.inl (some (sum.inr n)))
private def wrap_grule₁ {N₁ : Type} (N₂ : Type) (r : grule T N₁) : grule T (nnn T N₁ N₂) :=
grule.mk
(list.map (wrap_symbol₁ N₂) r.input_L)
(sum.inl (some (sum.inl r.input_N)))
(list.map (wrap_symbol₁ N₂) r.input_R)
(list.map (wrap_symbol₁ N₂) r.output_string)
private def wrap_grule₂ {N₂ : Type} (N₁ : Type) (r : grule T N₂) : grule T (nnn T N₁ N₂) :=
grule.mk
(list.map (wrap_symbol₂ N₁) r.input_L)
(sum.inl (some (sum.inr r.input_N)))
(list.map (wrap_symbol₂ N₁) r.input_R)
(list.map (wrap_symbol₂ N₁) r.output_string)
protected def rules_for_terminals₁ (N₂ : Type) (g : grammar T) : list (grule T (nnn T g.nt N₂)) :=
list.map (λ t, grule.mk [] (sum.inr (sum.inl t)) [] [symbol.terminal t]) (all_used_terminals g)
protected def rules_for_terminals₂ (N₁ : Type) (g : grammar T) : list (grule T (nnn T N₁ g.nt)) :=
list.map (λ t, grule.mk [] (sum.inr (sum.inr t)) [] [symbol.terminal t]) (all_used_terminals g)
-- the grammar for concatenation of `g₁` and `g₂` languages
protected def big_grammar (g₁ g₂ : grammar T) : grammar T :=
grammar.mk (nnn T g₁.nt g₂.nt) (sum.inl none) (
(grule.mk [] (sum.inl none) [] [
symbol.nonterminal (sum.inl (some (sum.inl g₁.initial))),
symbol.nonterminal (sum.inl (some (sum.inr g₂.initial)))]
) :: (
(list.map (wrap_grule₁ g₂.nt) g₁.rules ++ list.map (wrap_grule₂ g₁.nt) g₂.rules) ++
(rules_for_terminals₁ g₂.nt g₁ ++ rules_for_terminals₂ g₁.nt g₂)
)
)
end the_construction
section easy_direction
lemma grammar_generates_only_legit_terminals
{g : grammar T}
{w : list (symbol T g.nt)}
(ass : grammar_derives g [symbol.nonterminal g.initial] w)
{s : symbol T g.nt}
(symbol_derived : s ∈ w) :
(∃ r : grule T g.nt, r ∈ g.rules ∧ s ∈ r.output_string) ∨
(s = symbol.nonterminal g.initial) :=
begin
induction ass with x y trash orig ih,
{
rw list.mem_singleton at symbol_derived,
right,
exact symbol_derived,
},
rcases orig with ⟨r, rin, u, v, bef, aft⟩,
rw aft at symbol_derived,
rw list.mem_append at symbol_derived,
rw list.mem_append at symbol_derived,
cases symbol_derived,
cases symbol_derived,
{
apply ih,
rw bef,
repeat {
rw list.mem_append,
left,
},
exact symbol_derived,
},
{
left,
use r,
split,
{
exact rin,
},
{
exact symbol_derived,
},
},
{
apply ih,
rw bef,
rw list.mem_append,
right,
exact symbol_derived,
},
end
private lemma first_transformation {g₁ g₂ : grammar T} :
grammar_transforms (big_grammar g₁ g₂) [symbol.nonterminal (big_grammar g₁ g₂).initial] [
symbol.nonterminal (sum.inl (some (sum.inl g₁.initial))),
symbol.nonterminal (sum.inl (some (sum.inr g₂.initial)))
] :=
begin
use (big_grammar g₁ g₂).rules.nth_le 0 (by dec_trivial),
split,
{
change _ ∈ list.cons _ _,
finish,
},
use [[], []],
split;
refl,
end
private lemma substitute_terminals
{g₁ g₂ : grammar T}
{side : T → T ⊕ T}
{w : list T}
(rule_for_each_terminal : ∀ t ∈ w,
(grule.mk [] (sum.inr (side t)) [] [symbol.terminal t]) ∈
(rules_for_terminals₁ g₂.nt g₁ ++ rules_for_terminals₂ g₁.nt g₂)) :
grammar_derives (big_grammar g₁ g₂)
(list.map (symbol.nonterminal ∘ sum.inr ∘ side) w)
(list.map symbol.terminal w) :=
begin
induction w with d l ih,
{
apply grammar_deri_self,
},
rw list.map,
rw list.map,
rw ←list.singleton_append,
rw ←list.singleton_append,
have step_head :
grammar_transforms (big_grammar g₁ g₂)
([(symbol.nonterminal ∘ sum.inr ∘ side) d] ++ list.map (symbol.nonterminal ∘ sum.inr ∘ side) l)
([symbol.terminal d] ++ list.map (symbol.nonterminal ∘ sum.inr ∘ side) l),
{
use grule.mk [] (sum.inr (side d)) [] [symbol.terminal d],
split,
{
change _ ∈ list.cons _ _,
apply list.mem_cons_of_mem,
apply list.mem_append_right,
apply rule_for_each_terminal,
apply list.mem_cons_self,
},
use [[], list.map (symbol.nonterminal ∘ sum.inr ∘ side) l],
split;
refl,
},
apply grammar_deri_of_tran_deri step_head,
apply grammar_deri_with_prefix,
apply ih,
{
intros t tin,
apply rule_for_each_terminal t,
exact list.mem_cons_of_mem d tin,
},
end
protected lemma in_big_of_in_concatenated
{g₁ g₂ : grammar T}
{w : list T}
(ass : w ∈ grammar_language g₁ * grammar_language g₂) :
w ∈ grammar_language (big_grammar g₁ g₂) :=
begin
rw language.mem_mul at ass,
rcases ass with ⟨u, v, hu, hv, hw⟩,
unfold grammar_language at *,
rw set.mem_set_of_eq at *,
unfold grammar_generates at *,
apply grammar_deri_of_tran_deri first_transformation,
rw ←hw,
rw list.map_append,
apply @grammar_deri_of_deri_deri T (big_grammar g₁ g₂) _
(list.map symbol.terminal u ++ [symbol.nonterminal (sum.inl (some (sum.inr g₂.initial)))]) _,
{
clear_except hu,
rw ←list.singleton_append,
apply grammar_deri_with_postfix,
apply @grammar_deri_of_deri_deri _ _ _ (list.map (
(@symbol.nonterminal T (big_grammar g₁ g₂).nt) ∘ sum.inr ∘ sum.inl
) u) _,
{
have upgrade_deri₁ :
∀ w : list (symbol T g₁.nt),
grammar_derives g₁ [symbol.nonterminal g₁.initial] w →
grammar_derives (big_grammar g₁ g₂)
[symbol.nonterminal (sum.inl (some (sum.inl g₁.initial)))]
(list.map (wrap_symbol₁ g₂.nt) w),
{
clear_except,
intros w deri₁,
induction deri₁ with x y trash orig ih,
{
apply grammar_deri_self,
},
apply grammar_deri_of_deri_tran ih,
clear_except orig,
rcases orig with ⟨r, rin, u, v, bef, aft⟩,
use wrap_grule₁ g₂.nt r,
split,
{
change wrap_grule₁ g₂.nt r ∈ (_ :: ((
(list.map (wrap_grule₁ g₂.nt) g₁.rules) ++
(list.map (wrap_grule₂ g₁.nt) g₂.rules)
) ++ _)),
apply list.mem_cons_of_mem,
apply list.mem_append_left,
apply list.mem_append_left,
rw list.mem_map,
use r,
split,
{
exact rin,
},
{
refl,
},
},
use list.map (wrap_symbol₁ g₂.nt) u,
use list.map (wrap_symbol₁ g₂.nt) v,
split,
{
convert congr_arg (list.map (wrap_symbol₁ g₂.nt)) bef,
rw list.map_append_append,
rw list.map_append_append,
refl,
},
{
convert congr_arg (list.map (wrap_symbol₁ g₂.nt)) aft,
rw list.map_append_append,
refl,
},
},
have upgraded := upgrade_deri₁ _ hu,
rw list.map_map at upgraded,
exact upgraded,
},
{
have legit_terminals₁ :
∀ t ∈ u, ∃ r : grule T g₁.nt,
r ∈ g₁.rules ∧ symbol.terminal t ∈ r.output_string,
{
intros t tin,
have tin' : symbol.terminal t ∈ list.map symbol.terminal u,
{
rw list.mem_map,
use t,
split,
{
exact tin,
},
{
refl,
},
},
have legit := grammar_generates_only_legit_terminals hu tin',
cases legit,
{
exact legit,
},
{
exfalso,
exact symbol.no_confusion legit,
},
},
apply substitute_terminals,
{
intros t tin,
apply list.mem_append_left,
unfold rules_for_terminals₁,
rw list.mem_map,
use t,
split,
{
unfold all_used_terminals,
rw list.mem_filter_map,
use symbol.terminal t,
split,
{
rw list.mem_join,
obtain ⟨r, rin, sttin⟩ := legit_terminals₁ t tin,
use r.output_string,
split,
{
apply list.mem_map_of_mem,
exact rin,
},
{
exact sttin,
},
},
{
refl,
},
},
{
refl,
},
},
},
},
{
clear_except hv,
apply grammar_deri_with_prefix,
apply @grammar_deri_of_deri_deri _ _ _ (list.map (
(@symbol.nonterminal T (big_grammar g₁ g₂).nt) ∘ sum.inr ∘ sum.inr
) v) _,
{
have upgrade_deri₂ :
∀ w : list (symbol T g₂.nt),
grammar_derives g₂ [symbol.nonterminal g₂.initial] w →
grammar_derives (big_grammar g₁ g₂)
[symbol.nonterminal (sum.inl (some (sum.inr g₂.initial)))]
(list.map (wrap_symbol₂ g₁.nt) w),
{
clear_except,
intros w deri₁,
induction deri₁ with x y trash orig ih,
{
apply grammar_deri_self,
},
apply grammar_deri_of_deri_tran ih,
clear_except orig,
rcases orig with ⟨r, rin, u, v, bef, aft⟩,
use wrap_grule₂ g₁.nt r,
split,
{
change wrap_grule₂ g₁.nt r ∈ (_ :: ((
(list.map (wrap_grule₁ g₂.nt) g₁.rules) ++
(list.map (wrap_grule₂ g₁.nt) g₂.rules)
) ++ _)),
apply list.mem_cons_of_mem,
apply list.mem_append_left,
apply list.mem_append_right,
rw list.mem_map,
use r,
split,
{
exact rin,
},
{
refl,
},
},
use list.map (wrap_symbol₂ g₁.nt) u,
use list.map (wrap_symbol₂ g₁.nt) v,
split,
{
convert congr_arg (list.map (wrap_symbol₂ g₁.nt)) bef,
rw list.map_append_append,
rw list.map_append_append,
refl,
},
{
convert congr_arg (list.map (wrap_symbol₂ g₁.nt)) aft,
rw list.map_append_append,
refl,
},
},
have upgraded := upgrade_deri₂ _ hv,
rw list.map_map at upgraded,
exact upgraded,
},
{
have legit_terminals₂ :
∀ t ∈ v, ∃ r : grule T g₂.nt,
r ∈ g₂.rules ∧ symbol.terminal t ∈ r.output_string,
{
intros t tin,
have tin' : symbol.terminal t ∈ list.map symbol.terminal v,
{
rw list.mem_map,
use t,
split,
{
exact tin,
},
{
refl,
},
},
have legit := grammar_generates_only_legit_terminals hv tin',
cases legit,
{
exact legit,
},
{
exfalso,
exact symbol.no_confusion legit,
},
},
apply substitute_terminals,
{
intros t tin,
apply list.mem_append_right,
unfold rules_for_terminals₂,
rw list.mem_map,
use t,
split,
{
unfold all_used_terminals,
rw list.mem_filter_map,
use symbol.terminal t,
split,
{
rw list.mem_join,
obtain ⟨r, rin, sttin⟩ := legit_terminals₂ t tin,
use r.output_string,
split,
{
apply list.mem_map_of_mem,
exact rin,
},
{
exact sttin,
},
},
{
refl,
},
},
{
refl,
},
},
},
},
end
end easy_direction
section hard_direction
section correspondence_for_terminals
private def corresponding_symbols {N₁ N₂ : Type} : nst T N₁ N₂ → nst T N₁ N₂ → Prop
| (symbol.terminal t) (symbol.terminal t') := t = t'
| (symbol.nonterminal (sum.inr (sum.inl a))) (symbol.nonterminal (sum.inr (sum.inl a'))) := a = a'
| (symbol.nonterminal (sum.inr (sum.inr a))) (symbol.nonterminal (sum.inr (sum.inr a'))) := a = a'
| (symbol.nonterminal (sum.inr (sum.inl a))) (symbol.terminal t) := t = a
| (symbol.nonterminal (sum.inr (sum.inr a))) (symbol.terminal t) := t = a
| (symbol.nonterminal (sum.inl (some (sum.inl n)))) (symbol.nonterminal (sum.inl (some (sum.inl n')))) := n = n'
| (symbol.nonterminal (sum.inl (some (sum.inr n)))) (symbol.nonterminal (sum.inl (some (sum.inr n')))) := n = n'
| (symbol.nonterminal (sum.inl (none))) (symbol.nonterminal (sum.inl (none))) := true
| _ _ := false
private lemma corresponding_symbols_self {N₁ N₂ : Type} (s : nst T N₁ N₂) : corresponding_symbols s s :=
begin
repeat {
try {
cases s,
},
try {
unfold corresponding_symbols,
},
},
end
private lemma corresponding_symbols_never₁ {N₁ N₂ : Type} {s₁ : symbol T N₁} {s₂ : symbol T N₂} :
¬ corresponding_symbols (wrap_symbol₁ N₂ s₁) (wrap_symbol₂ N₁ s₂) :=
begin
cases s₁;
cases s₂;
{
unfold wrap_symbol₁,
unfold wrap_symbol₂,
unfold corresponding_symbols,
exact not_false,
},
end
private lemma corresponding_symbols_never₂ {N₁ N₂ : Type} {s₁ : symbol T N₁} {s₂ : symbol T N₂} :
¬ corresponding_symbols (wrap_symbol₂ N₁ s₂) (wrap_symbol₁ N₂ s₁) :=
begin
cases s₁;
cases s₂;
{
unfold wrap_symbol₁,
unfold wrap_symbol₂,
unfold corresponding_symbols,
exact not_false,
},
end
private def corresponding_strings {N₁ N₂ : Type} : list (nst T N₁ N₂) → list (nst T N₁ N₂) → Prop :=
list.forall₂ corresponding_symbols
private lemma corresponding_strings_self {N₁ N₂ : Type} {x : list (nst T N₁ N₂)} :
corresponding_strings x x :=
begin
apply list.forall₂_same,
intros s trash,
exact corresponding_symbols_self s,
end
private lemma corresponding_strings_singleton {N₁ N₂ : Type} {s₁ s₂ : nst T N₁ N₂}
(ass : corresponding_symbols s₁ s₂) :
corresponding_strings [s₁] [s₂] :=
begin
unfold corresponding_strings,
rw list.forall₂_cons,
split,
{
exact ass,
},
{
exact list.forall₂.nil,
},
end
private lemma corresponding_strings_append {N₁ N₂ : Type} {x₁ x₂ y₁ y₂ : list (nst T N₁ N₂)}
(ass₁ : corresponding_strings x₁ y₁)
(ass₂ : corresponding_strings x₂ y₂) :
corresponding_strings (x₁ ++ x₂) (y₁ ++ y₂) :=
begin
unfold corresponding_strings at *,
exact list.rel_append ass₁ ass₂,
end
private lemma corresponding_strings_length {N₁ N₂ : Type} {x y : list (nst T N₁ N₂)}
(ass : corresponding_strings x y) :
x.length = y.length :=
begin
unfold corresponding_strings at ass,
exact list.forall₂_length_eq ass,
end
private lemma corresponding_strings_nth_le {N₁ N₂ : Type} {x y : list (nst T N₁ N₂)} {i : ℕ}
(i_lt_len_x : i < x.length) (i_lt_len_y : i < y.length)
(ass : corresponding_strings x y) :
corresponding_symbols (x.nth_le i i_lt_len_x) (y.nth_le i i_lt_len_y) :=
begin
apply list_forall₂_nth_le,
exact ass,
end
private lemma corresponding_strings_reverse {N₁ N₂ : Type} {x y : list (nst T N₁ N₂)}
(ass : corresponding_strings x y) :
corresponding_strings x.reverse y.reverse :=
begin
unfold corresponding_strings at *,
rw list.forall₂_reverse_iff,
exact ass,
end
private lemma corresponding_strings_of_reverse {N₁ N₂ : Type} {x y : list (nst T N₁ N₂)}
(ass : corresponding_strings x.reverse y.reverse) :
corresponding_strings x y :=
begin
unfold corresponding_strings at *,
rw list.forall₂_reverse_iff at ass,
exact ass,
end
private lemma corresponding_strings_take {N₁ N₂ : Type} {x y : list (nst T N₁ N₂)}
(n : ℕ) (ass : corresponding_strings x y) :
corresponding_strings (list.take n x) (list.take n y) :=
begin
unfold corresponding_strings at *,
exact list.forall₂_take n ass,
end
private lemma corresponding_strings_drop {N₁ N₂ : Type} {x y : list (nst T N₁ N₂)}
(n : ℕ) (ass : corresponding_strings x y) :
corresponding_strings (list.drop n x) (list.drop n y) :=
begin
unfold corresponding_strings at *,
exact list.forall₂_drop n ass,
end
private lemma corresponding_strings_split {N₁ N₂ : Type} {x y : list (nst T N₁ N₂)}
(n : ℕ) (ass : corresponding_strings x y) :
corresponding_strings (list.take n x) (list.take n y) ∧
corresponding_strings (list.drop n x) (list.drop n y) :=
begin
split,
{
exact corresponding_strings_take n ass,
},
{
exact corresponding_strings_drop n ass,
},
end
end correspondence_for_terminals
section unwrapping_nst
private def unwrap_symbol₁ {N₁ N₂ : Type} : nst T N₁ N₂ → option (symbol T N₁)
| (symbol.terminal t) := some (symbol.terminal t)
| (symbol.nonterminal (sum.inr (sum.inl a))) := some (symbol.terminal a)
| (symbol.nonterminal (sum.inr (sum.inr a))) := none
| (symbol.nonterminal (sum.inl (some (sum.inl n)))) := some (symbol.nonterminal n)
| (symbol.nonterminal (sum.inl (some (sum.inr n)))) := none
| (symbol.nonterminal (sum.inl (none))) := none
private def unwrap_symbol₂ {N₁ N₂ : Type} : nst T N₁ N₂ → option (symbol T N₂)
| (symbol.terminal t) := some (symbol.terminal t)
| (symbol.nonterminal (sum.inr (sum.inl a))) := none
| (symbol.nonterminal (sum.inr (sum.inr a))) := some (symbol.terminal a)
| (symbol.nonterminal (sum.inl (some (sum.inl n)))) := none
| (symbol.nonterminal (sum.inl (some (sum.inr n)))) := some (symbol.nonterminal n)
| (symbol.nonterminal (sum.inl (none))) := none
private lemma unwrap_wrap₁_symbol {N₁ N₂ : Type} : @unwrap_symbol₁ T N₁ N₂ ∘ wrap_symbol₁ N₂ = option.some :=
begin
ext1 a,
cases a;
refl,
end
private lemma unwrap_wrap₂_symbol {N₁ N₂ : Type} : @unwrap_symbol₂ T N₁ N₂ ∘ wrap_symbol₂ N₁ = option.some :=
begin
ext1 a,
cases a;
refl,
end
private lemma unwrap_wrap₁_string {N₁ N₂ : Type} {w : list (symbol T N₁)} :
list.filter_map unwrap_symbol₁ (list.map (wrap_symbol₁ N₂) w) = w :=
begin
rw list.filter_map_map,
rw unwrap_wrap₁_symbol,
apply list.filter_map_some,
end
private lemma unwrap_wrap₂_string {N₁ N₂ : Type} {w : list (symbol T N₂)} :
list.filter_map unwrap_symbol₂ (list.map (wrap_symbol₂ N₁) w) = w :=
begin
rw list.filter_map_map,
rw unwrap_wrap₂_symbol,
apply list.filter_map_some,
end
private lemma unwrap_eq_some_of_corresponding_symbols₁ {N₁ N₂ : Type} {s₁ : symbol T N₁} {s : nst T N₁ N₂}
(ass : corresponding_symbols (wrap_symbol₁ N₂ s₁) s) :
unwrap_symbol₁ s = some s₁ :=
begin
cases s₁;
{
unfold wrap_symbol₁ at ass,
repeat {
try {
cases s,
},
try {
unfold corresponding_symbols at ass,
rw ass,
refl,
},
try {
unfold corresponding_symbols at ass,
exfalso,
exact ass,
},
},
},
end
private lemma unwrap_eq_some_of_corresponding_symbols₂ {N₁ N₂ : Type} {s₂ : symbol T N₂} {s : nst T N₁ N₂}
(ass : corresponding_symbols (wrap_symbol₂ N₁ s₂) s) :
unwrap_symbol₂ s = some s₂ :=
begin
cases s₂;
{
unfold wrap_symbol₂ at ass,
repeat {
try {
cases s,
},
try {
unfold corresponding_symbols at ass,
rw ass,
refl,
},
try {
unfold corresponding_symbols at ass,
exfalso,
exact ass,
},
},
},
end
private lemma map_unwrap_eq_map_some_of_corresponding_strings₁ {N₁ N₂ : Type} :
∀ {v : list (symbol T N₁)}, ∀ {w : list (nst T N₁ N₂)},
corresponding_strings (list.map (wrap_symbol₁ N₂) v) w →
list.map unwrap_symbol₁ w = list.map option.some v
| [] [] := λ _, rfl
| [] (b::y) := by { intro hyp, exfalso, unfold corresponding_strings at hyp, unfold list.map at hyp, finish, }
| (a::x) [] := by { intro hyp, exfalso, unfold corresponding_strings at hyp, unfold list.map at hyp, finish, }
| (a::x) (b::y) :=
begin
intro ass,
unfold corresponding_strings at ass,
rw list.map_cons at ass,
rw list.forall₂_cons at ass,
rw list.map,
rw list.map,
apply congr_arg2,
{
exact unwrap_eq_some_of_corresponding_symbols₁ ass.1,
},
{
apply map_unwrap_eq_map_some_of_corresponding_strings₁,
exact ass.2
},
end
private lemma map_unwrap_eq_map_some_of_corresponding_strings₂ {N₁ N₂ : Type} :
∀ {v : list (symbol T N₂)}, ∀ {w : list (nst T N₁ N₂)},
corresponding_strings (list.map (wrap_symbol₂ N₁) v) w →
list.map unwrap_symbol₂ w = list.map option.some v
| [] [] := λ _, rfl
| [] (b::y) := by { intro hyp, exfalso, unfold corresponding_strings at hyp, unfold list.map at hyp, finish, }
| (a::x) [] := by { intro hyp, exfalso, unfold corresponding_strings at hyp, unfold list.map at hyp, finish, }
| (a::x) (b::y) :=
begin
intro ass,
unfold corresponding_strings at ass,
rw list.map_cons at ass,
rw list.forall₂_cons at ass,
rw list.map,
rw list.map,
apply congr_arg2,
{
exact unwrap_eq_some_of_corresponding_symbols₂ ass.1,
},
{
apply map_unwrap_eq_map_some_of_corresponding_strings₂,
exact ass.2
},
end
private lemma filter_map_unwrap_of_corresponding_strings₁ {N₁ N₂ : Type}
{v : list (symbol T N₁)} {w : list (nst T N₁ N₂)}
(ass : corresponding_strings (list.map (wrap_symbol₁ N₂) v) w) :
list.filter_map unwrap_symbol₁ w = v :=
begin
apply list_filter_map_eq_of_map_eq_map_some,
exact map_unwrap_eq_map_some_of_corresponding_strings₁ ass,
end
private lemma filter_map_unwrap_of_corresponding_strings₂ {N₁ N₂ : Type}
{v : list (symbol T N₂)} {w : list (nst T N₁ N₂)}
(ass : corresponding_strings (list.map (wrap_symbol₂ N₁) v) w) :
list.filter_map unwrap_symbol₂ w = v :=
begin
apply list_filter_map_eq_of_map_eq_map_some,
exact map_unwrap_eq_map_some_of_corresponding_strings₂ ass,
end
private lemma corresponding_string_after_wrap_unwrap_self₁ {N₁ N₂ : Type} {w : list (nst T N₁ N₂)}
(ass : ∃ z : list (symbol T N₁), corresponding_strings (list.map (wrap_symbol₁ N₂) z) w) :
corresponding_strings (list.map (wrap_symbol₁ N₂) (list.filter_map unwrap_symbol₁ w)) w :=
begin
induction w with d l ih,
{
unfold corresponding_strings,
unfold list.filter_map,
unfold list.map,
exact list.forall₂.nil,
},
specialize ih (by {
cases ass with z hyp,
unfold corresponding_strings at *,
cases z with z₀ z',
{
exfalso,
finish,
},
{
use z',
finish,
},
}),
unfold corresponding_strings,
cases d,
{
have unwrap_first_t :
list.filter_map unwrap_symbol₁ (symbol.terminal d :: l) =
symbol.terminal d :: list.filter_map unwrap_symbol₁ l,
{
refl,
},
rw unwrap_first_t,
unfold list.map,
unfold wrap_symbol₁,
rw list.forall₂_cons,
split,
{
unfold corresponding_symbols,
},
{
exact ih,
},
},
cases d,
cases d, swap,
cases d,
{
have unwrap_first_nlsl :
list.filter_map unwrap_symbol₁ (symbol.nonterminal (sum.inl (some (sum.inl d))) :: l) =
symbol.nonterminal d :: list.filter_map unwrap_symbol₁ l,
{
refl,
},
rw unwrap_first_nlsl,
unfold list.map,
unfold wrap_symbol₁,
rw list.forall₂_cons,
split,
{
unfold corresponding_symbols,
},
{
exact ih,
},
},
swap 3,
cases d,
{
have unwrap_first_nrl :
list.filter_map unwrap_symbol₁ (symbol.nonterminal (sum.inr (sum.inl d)) :: l) =
symbol.terminal d :: list.filter_map unwrap_symbol₁ l,
{
refl,
},
rw unwrap_first_nrl,
unfold list.map,
unfold wrap_symbol₁,
rw list.forall₂_cons,
split,
{
unfold corresponding_symbols,
},
{
exact ih,
},
},
any_goals {
exfalso,
cases ass with z hyp,
cases z with z₀ z',
{
have imposs := corresponding_strings_length hyp,
clear_except imposs,
rw list.length at imposs,
rw list.length_map at imposs,
rw list.length at imposs,
linarith,
},
{
rw list.map_cons at hyp,
unfold corresponding_strings at hyp,
rw list.forall₂_cons at hyp,
have impos := hyp.left,
clear_except impos,
cases z₀;
{
unfold wrap_symbol₁ at impos,
unfold corresponding_symbols at impos,
exact impos,
},
},
},
end
private lemma corresponding_string_after_wrap_unwrap_self₂ {N₁ N₂ : Type} {w : list (nst T N₁ N₂)}
(ass : ∃ z : list (symbol T N₂), corresponding_strings (list.map (wrap_symbol₂ N₁) z) w) :
corresponding_strings (list.map (wrap_symbol₂ N₁) (list.filter_map unwrap_symbol₂ w)) w :=
begin
induction w with d l ih,
{
unfold corresponding_strings,
unfold list.filter_map,
unfold list.map,
exact list.forall₂.nil,
},
specialize ih (by {
cases ass with z hyp,
unfold corresponding_strings at *,
cases z with z₀ z',
{
exfalso,
finish,
},
{
use z',
finish,
},
}),
unfold corresponding_strings,
cases d,
{
have unwrap_first_t :
list.filter_map unwrap_symbol₂ (symbol.terminal d :: l) =
symbol.terminal d :: list.filter_map unwrap_symbol₂ l,
{
refl,
},
rw unwrap_first_t,
unfold list.map,
unfold wrap_symbol₂,
rw list.forall₂_cons,
split,
{
unfold corresponding_symbols,
},
{
exact ih,
},
},
cases d,
cases d, swap,
cases d, swap,
{
have unwrap_first_nlsr :
list.filter_map unwrap_symbol₂ (symbol.nonterminal (sum.inl (some (sum.inr d))) :: l) =
symbol.nonterminal d :: list.filter_map unwrap_symbol₂ l,
{
refl,
},
rw unwrap_first_nlsr,
unfold list.map,
unfold wrap_symbol₂,
rw list.forall₂_cons,
split,
{
unfold corresponding_symbols,
},
{
exact ih,
},
},
swap 3,
cases d, swap,
{
have unwrap_first_nrr :
list.filter_map unwrap_symbol₂ (symbol.nonterminal (sum.inr (sum.inr d)) :: l) =
symbol.terminal d :: list.filter_map unwrap_symbol₂ l,
{
refl,
},
rw unwrap_first_nrr,
unfold list.map,
unfold wrap_symbol₂,
rw list.forall₂_cons,
split,
{
unfold corresponding_symbols,
},
{
exact ih,
},
},
any_goals {
exfalso,
cases ass with z hyp,
cases z with z₀ z',
{
have imposs := corresponding_strings_length hyp,
clear_except imposs,
rw list.length at imposs,
rw list.length_map at imposs,
rw list.length at imposs,
linarith,
},
{
rw list.map_cons at hyp,
unfold corresponding_strings at hyp,
rw list.forall₂_cons at hyp,
have impos := hyp.left,
clear_except impos,
cases z₀;
{
unfold wrap_symbol₂ at impos,
unfold corresponding_symbols at impos,
exact impos,
},
},
},
end
end unwrapping_nst
section very_complicated
private lemma induction_step_for_lifted_rule_from_g₁
{g₁ g₂ : grammar T}
{a b u v : list (nst T g₁.nt g₂.nt)}
{x : list (symbol T g₁.nt)}
{y : list (symbol T g₂.nt)}
{r : grule T (nnn T g₁.nt g₂.nt)}
(rin : r ∈ list.map (wrap_grule₁ g₂.nt) g₁.rules)
(bef : a = u ++ r.input_L ++ [symbol.nonterminal r.input_N] ++ r.input_R ++ v)
(aft : b = u ++ r.output_string ++ v)
(ih_x : grammar_derives g₁ [symbol.nonterminal g₁.initial] x)
(ih_y : grammar_derives g₂ [symbol.nonterminal g₂.initial] y)
(ih_concat :
corresponding_strings
(list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y)
a) :
∃ (x' : list (symbol T g₁.nt)),
and
(and
(grammar_derives g₁ [symbol.nonterminal g₁.initial] x')
(grammar_derives g₂ [symbol.nonterminal g₂.initial] y)
)
(corresponding_strings (list.map (wrap_symbol₁ g₂.nt) x' ++ list.map (wrap_symbol₂ g₁.nt) y) b) :=
begin
rw list.mem_map at rin,
rcases rin with ⟨r₁, rin₁, wrap_r₁_eq_r⟩,
rw ←wrap_r₁_eq_r at *,
clear wrap_r₁_eq_r,
simp [wrap_grule₁] at *,
rw ←list.singleton_append at bef,
let m := (list.map (wrap_symbol₁ g₂.nt) r₁.input_L).length + 1 +
(list.map (wrap_symbol₁ g₂.nt) r₁.input_R).length,
let b' := u ++ list.map (wrap_symbol₁ g₂.nt) r₁.output_string ++ list.take (x.length - u.length - m) v,
use list.filter_map unwrap_symbol₁ b',
have critical :
(list.map (wrap_symbol₁ g₂.nt) r₁.input_L).length + 1 +
(list.map (wrap_symbol₁ g₂.nt) r₁.input_R).length ≤
x.length - u.length,
{
clear_except ih_concat bef,
have as_positive :
u.length + (
(list.map (wrap_symbol₁ g₂.nt) r₁.input_L).length + 1 +
(list.map (wrap_symbol₁ g₂.nt) r₁.input_R).length
) ≤
x.length,
{
by_contradiction contra,
push_neg at contra,
rw bef at ih_concat,
clear bef,
repeat {
rw ←list.append_assoc at ih_concat
},
have len_pos :
(u ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_L ++
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))] ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_R
).length > 0,
{
repeat {
rw list.length_append,
},
rw list.length_singleton,
clear_except,
linarith,
},
have equal_total_len := corresponding_strings_length ih_concat,
have inequality_m1 :
(u ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_L ++
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))] ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_R
).length - 1 <
(u ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_L ++
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))] ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_R
).length,
{
exact buffer.lt_aux_2 len_pos,
},
have inequality_cat :
(u ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_L ++
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))] ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_R
).length - 1 <
(u ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_L ++
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))] ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_R ++ v
).length,
{
rw list.length_append _ v,
apply lt_of_lt_of_le (buffer.lt_aux_2 len_pos),
exact le_self_add,
},
have inequality_map :
(u ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_L ++
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))] ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_R
).length - 1 <
((list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y)).length,
{
rw equal_total_len,
exact inequality_cat,
},
have inequality_map_opp :
(u ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_L ++
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))] ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_R
).length - 1 ≥
(list.map (wrap_symbol₁ g₂.nt) x).length,
{
clear_except contra,
apply nat.le_pred_of_lt,
repeat {
rw list.length_append,
},
repeat {
rw list.length_map,
},
rw list.length_map at contra,
rw list.length_map at contra,
rw list.length_singleton,
rw add_assoc,
rw add_assoc,
rw add_assoc at contra,
exact contra,
},
have clash := corresponding_strings_nth_le inequality_map inequality_cat ih_concat,
rw list.nth_le_append inequality_cat inequality_m1 at clash,
rw list.nth_le_append_right inequality_map_opp inequality_map at clash,
rw list.nth_le_map at clash, swap,
{
have inequality_map := inequality_map,
rw list.length_append _ (list.map (wrap_symbol₂ g₁.nt) y) at inequality_map,
rw list.length_map _ y at inequality_map,
rw tsub_lt_iff_left inequality_map_opp,
exact inequality_map,
},
by_cases (list.map (wrap_symbol₁ g₂.nt) r₁.input_R).length ≥ 1,
{
rw list.nth_le_append_right at clash, swap,
{
rw list.length_append _ (list.map (wrap_symbol₁ g₂.nt) r₁.input_R),
have trivi_ineq : ∀ m k : ℕ, k ≥ 1 → m ≤ m + k - 1,
{
clear_except,
omega,
},
convert trivi_ineq (u ++ _ ++ [_]).length _ h,
},
rw list.nth_le_map at clash, swap,
{
rw list.length_map at h,
repeat {
rw list.length_append,
},
repeat {
rw list.length_map,
},
rw list.length_singleton,
have easy_ineq : ∀ m k : ℕ, k ≥ 1 → m + k - 1 - m < k,
{
clear_except,
omega,
},
convert easy_ineq (u.length + r₁.input_L.length + 1) _ h,
},
exact corresponding_symbols_never₂ clash,
},
{
push_neg at h,
have ris_third_is_nil : list.map (wrap_symbol₁ g₂.nt) r₁.input_R = [],
{
rw ←list.length_eq_zero,
rw ←nat.lt_one_iff,
exact h,
},
have inequality_m0 :
(u ++ list.map (wrap_symbol₁ g₂.nt) r₁.input_L ++
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))]).length - 1 <
(u ++ list.map (wrap_symbol₁ g₂.nt) r₁.input_L ++
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))]).length,
{
rw ris_third_is_nil at inequality_m1,
rw list.append_nil at inequality_m1,
exact inequality_m1,
},
simp_rw [ris_third_is_nil] at clash,
simp only [list.append_nil] at clash,
rw list.nth_le_append_right at clash,
swap, {
apply le_of_eq,
rw list.length_append _ [_],
rw list.length_singleton,
apply nat.succ_sub_one,
},
rw list.nth_le_singleton at clash,
change
corresponding_symbols _ (wrap_symbol₁ g₂.nt (symbol.nonterminal r₁.input_N)) at clash,
exact corresponding_symbols_never₂ clash,
},
},
omega,
},
split,
{
split,
{
apply grammar_deri_of_deri_tran ih_x,
use r₁,
split,
{
exact rin₁,
},
use list.filter_map unwrap_symbol₁ u,
use list.filter_map unwrap_symbol₁ (list.take (x.length - u.length - m) v),
split,
{
have x_equiv :
corresponding_strings
(list.map (wrap_symbol₁ g₂.nt) x)
(list.take x.length (u
++ list.map (wrap_symbol₁ g₂.nt) r₁.input_L
++ [symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))]
++ list.map (wrap_symbol₁ g₂.nt) r₁.input_R
++ v)),
{
rw bef at ih_concat,
clear_except ih_concat,
rw ←list.append_assoc _ _ v at ih_concat,
rw ←list.append_assoc _ _ v at ih_concat,
rw list.append_assoc u,
rw list.append_assoc u,
rw list.append_assoc u,
rw list.append_assoc (list.map (wrap_symbol₁ g₂.nt) r₁.input_L),
convert corresponding_strings_take x.length ih_concat,
{
have x_len_eq : x.length = (list.map (wrap_symbol₁ g₂.nt) x).length,
{
rw list.length_map,
},
rw x_len_eq,
rw list.take_left,
},
},
clear_except x_equiv critical,
have ul_le_xl : u.length ≤ x.length,
{
clear_except critical,
have weaker_le : 1 ≤ x.length - u.length,
{
omega,
},
have stupid_le : u.length + 1 ≤ x.length,
{
omega,
},
exact nat.le_of_succ_le stupid_le,
},
repeat {
rw list.take_append_eq_append_take at x_equiv,
},
rw list.take_all_of_le ul_le_xl at x_equiv,
repeat {
rw list.append_assoc,
},
have chunk2 :
list.take (x.length - u.length) (list.map (wrap_symbol₁ g₂.nt) r₁.input_L) =
list.map (wrap_symbol₁ g₂.nt) r₁.input_L,
{
apply list.take_all_of_le,
clear_except critical,
omega,
},
have chunk3 :
list.take (x.length - (u ++ list.map (wrap_symbol₁ g₂.nt) r₁.input_L).length)
[@symbol.nonterminal T (nnn T g₁.nt g₂.nt) (sum.inl (some (sum.inl r₁.input_N)))] =
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))],
{
apply list.take_all_of_le,
clear_except critical,
change 1 ≤ x.length - (u ++ list.map (wrap_symbol₁ g₂.nt) r₁.input_L).length,
rw list.length_append,
have weakened :
(list.map (wrap_symbol₁ g₂.nt) r₁.input_L).length + 1 ≤
x.length - u.length,
{
omega,
},
have goal_as_le_sub_sub :
1 ≤ (x.length - u.length) - (list.map (wrap_symbol₁ g₂.nt) r₁.input_L).length,
{
omega,
},
rw tsub_add_eq_tsub_tsub,
exact goal_as_le_sub_sub,
},
have chunk4 :
list.take (x.length - (
u ++ list.map (wrap_symbol₁ g₂.nt) r₁.input_L ++
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))]
).length) (list.map (wrap_symbol₁ g₂.nt) r₁.input_R) =
list.map (wrap_symbol₁ g₂.nt) r₁.input_R,
{
apply list.take_all_of_le,
clear_except critical,
rw list.length_append_append,
change
(list.map (wrap_symbol₁ g₂.nt) r₁.input_R).length ≤
x.length - (u.length + (list.map (wrap_symbol₁ g₂.nt) r₁.input_L).length + 1),
omega,
},
have chunk5 :
list.take (x.length - (
u ++ list.map (wrap_symbol₁ g₂.nt) r₁.input_L ++
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))] ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_R).length
) v =
list.take (x.length - u.length - m) v,
{
repeat {
rw list.length_append,
},
apply congr_arg2, swap,
{
refl,
},
have rearrange_sum_of_four : ∀ a b c d : ℕ, a + b + c + d = a + (b + c + d),
{
omega,
},
rw rearrange_sum_of_four,
change x.length - (u.length + m) = x.length - u.length - m,
clear_except,
omega,
},
rw [chunk2, chunk3, chunk4, chunk5] at x_equiv,
clear chunk2 chunk3 chunk4 chunk5,
obtain ⟨temp_5, equiv_segment_5⟩ :=
corresponding_strings_split (
u ++ list.map (wrap_symbol₁ g₂.nt) r₁.input_L ++
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))] ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_R
).length x_equiv,
clear x_equiv,
rw list.drop_left at equiv_segment_5,
rw list.take_left at temp_5,
obtain ⟨temp_4, equiv_segment_4⟩ :=
corresponding_strings_split (
u ++ list.map (wrap_symbol₁ g₂.nt) r₁.input_L ++
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))]
).length temp_5,
clear temp_5,
rw list.drop_left at equiv_segment_4,
rw list.take_left at temp_4,
rw list.take_take at temp_4,
obtain ⟨temp_3, equiv_segment_3⟩ :=
corresponding_strings_split (
u ++ list.map (wrap_symbol₁ g₂.nt) r₁.input_L
).length temp_4,
clear temp_4,
rw list.drop_left at equiv_segment_3,
rw list.take_left at temp_3,
rw list.take_take at temp_3,
obtain ⟨equiv_segment_1, equiv_segment_2⟩ :=
corresponding_strings_split u.length temp_3,
clear temp_3,
rw list.drop_left at equiv_segment_2,
rw list.take_left at equiv_segment_1,
rw list.take_take at equiv_segment_1,
have equiv_sgmnt_1 :
corresponding_strings (list.take u.length (list.map (wrap_symbol₁ g₂.nt) x)) u,
{
simpa using equiv_segment_1,
},
have equiv_sgmnt_2 :
corresponding_strings
(list.drop u.length (list.take (u.length + r₁.input_L.length)
(list.map (wrap_symbol₁ g₂.nt) x)))
(list.map (wrap_symbol₁ g₂.nt) r₁.input_L),
{
simpa using equiv_segment_2,
},
have equiv_sgmnt_3 :
corresponding_strings
(list.drop (u.length + r₁.input_L.length)
(list.take (u.length + (r₁.input_L.length + 1)) (list.map (wrap_symbol₁ g₂.nt) x)))
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))],
{
simpa using equiv_segment_3,
},
have equiv_sgmnt_4 :
corresponding_strings
(list.drop (u.length + (r₁.input_L.length + 1))
(list.take (u.length + (r₁.input_L.length + (r₁.input_R.length + 1)))
(list.map (wrap_symbol₁ g₂.nt) x)))
(list.map (wrap_symbol₁ g₂.nt) r₁.input_R),
{
simpa using equiv_segment_4,
},
have equiv_sgmnt_5 :
corresponding_strings
(list.drop (u.length + (r₁.input_L.length + (r₁.input_R.length + 1)))
(list.map (wrap_symbol₁ g₂.nt) x))
(list.take (x.length - u.length - m) v),
{
simpa using equiv_segment_5,
},
clear equiv_segment_1 equiv_segment_2 equiv_segment_3 equiv_segment_4 equiv_segment_5,
have segment_1_eqi : corresponding_strings (list.map (wrap_symbol₁ g₂.nt) (list.take u.length x)) u,
{
convert equiv_sgmnt_1,
rw list.map_take,
},
have segment_1_equ := (filter_map_unwrap_of_corresponding_strings₁ segment_1_eqi).symm,
rw ←list.take_append_drop u.length x,
apply congr_arg2,
{
exact segment_1_equ,
},
clear segment_1_equ segment_1_eqi equiv_sgmnt_1,
have segment_2_eqi :
corresponding_strings
(list.map (wrap_symbol₁ g₂.nt) (list.take r₁.input_L.length (list.drop u.length x)))
(list.map (wrap_symbol₁ g₂.nt) r₁.input_L),
{
convert equiv_sgmnt_2,
rw list.map_take,
rw list.map_drop,
rw list.drop_take,
},
have segment_2_equ := (filter_map_unwrap_of_corresponding_strings₁ segment_2_eqi).symm,
rw unwrap_wrap₁_string at segment_2_equ,
rw ←list.take_append_drop r₁.input_L.length (list.drop u.length x),
apply congr_arg2,
{
exact segment_2_equ,
},
clear segment_2_equ segment_2_eqi equiv_sgmnt_2,
rw list.drop_drop,
have segment_3_eqi :
corresponding_strings
(list.map (wrap_symbol₁ g₂.nt) (list.take 1 (list.drop (r₁.input_L.length + u.length) x)))
(list.map (wrap_symbol₁ g₂.nt) [symbol.nonterminal r₁.input_N]),
{
convert equiv_sgmnt_3,
rw list.map_take,
rw list.map_drop,
rw ←add_assoc,
rw list.drop_take,
rw add_comm,
},
have segment_3_equ := (filter_map_unwrap_of_corresponding_strings₁ segment_3_eqi).symm,
rw unwrap_wrap₁_string at segment_3_equ,
rw ←list.take_append_drop 1 (list.drop (r₁.input_L.length + u.length) x),
apply congr_arg2,
{
exact segment_3_equ,
},
clear segment_3_equ segment_3_eqi equiv_sgmnt_3,
rw list.drop_drop,
have segment_4_eqi :
corresponding_strings
(list.map (wrap_symbol₁ g₂.nt) (list.take r₁.input_R.length
(list.drop (1 + (r₁.input_L.length + u.length)) x)))
(list.map (wrap_symbol₁ g₂.nt) r₁.input_R),
{
convert equiv_sgmnt_4,
rw list.map_take,
rw list.map_drop,
have sum_rearrange :
u.length + (r₁.input_L.length + (r₁.input_R.length + 1)) =
(u.length + (r₁.input_L.length + 1)) + r₁.input_R.length,
{
linarith,
},
rw sum_rearrange,
rw list.drop_take,
have small_sum_rearr :
1 + (r₁.input_L.length + u.length) =
u.length + (r₁.input_L.length + 1),
{
linarith,
},
rw small_sum_rearr,
},
have segment_4_equ := (filter_map_unwrap_of_corresponding_strings₁ segment_4_eqi).symm,
rw unwrap_wrap₁_string at segment_4_equ,
rw ←list.take_append_drop r₁.input_R.length
(list.drop (1 + (r₁.input_L.length + u.length)) x),
apply congr_arg2,
{
exact segment_4_equ,
},
clear segment_4_equ segment_4_eqi equiv_sgmnt_4,
rw list.drop_drop,
repeat {
rw list.length_append,
},
repeat {
rw list.length_take,
},
repeat {
rw list.length_drop,
},
have sum_of_min_lengths :
min u.length x.length +
(min r₁.input_L.length (x.length - u.length) +
(min 1 (x.length - (r₁.input_L.length + u.length)) +
(min r₁.input_R.length (x.length - (1 + (r₁.input_L.length + u.length))) +
(x.length - (r₁.input_R.length + (1 + (r₁.input_L.length + u.length))))))) =
x.length,
{
have add_mirror :
r₁.input_R.length + 1 + r₁.input_L.length =
r₁.input_L.length + 1 + r₁.input_R.length,
{
ring,
},
rw [list.length_map, list.length_map, ←add_mirror] at critical,
have min1 : min u.length x.length = u.length,
{
apply min_eq_left,
exact ul_le_xl,
},
have min2 : min r₁.input_L.length (x.length - u.length) = r₁.input_L.length,
{
clear_except critical,
apply min_eq_left,
apply le_trans _ critical,
apply le_add_self,
},
have min3 : min 1 (x.length - (r₁.input_L.length + u.length)) = 1,
{
clear_except critical,
apply min_eq_left,
omega,
},
have min4 :
min r₁.input_R.length (x.length - (1 + (r₁.input_L.length + u.length))) =
r₁.input_R.length,
{
clear_except critical,
apply min_eq_left,
omega,
},
rw [min1, min2, min3, min4],
rw le_tsub_iff_right ul_le_xl at critical,
clear_except critical add_mirror,
repeat {
rw ←add_assoc,
},
have sum_eq_sum :
u.length + r₁.input_L.length + 1 + r₁.input_R.length =
r₁.input_R.length + 1 + r₁.input_L.length + u.length,
{
rw add_mirror,
rw add_assoc,
rw add_assoc,
rw add_comm,
rw ←add_assoc _ 1 _,
},
rw sum_eq_sum,
exact nat.add_sub_of_le critical,
},
rw sum_of_min_lengths,
clear_except equiv_sgmnt_5,
have another_rearranging :
r₁.input_R.length + (1 + (r₁.input_L.length + u.length)) =
u.length + (r₁.input_L.length + (r₁.input_R.length + 1)),
{
ring,
},
rw another_rearranging,
rw ←list.map_drop at equiv_sgmnt_5,
symmetry,
exact filter_map_unwrap_of_corresponding_strings₁ equiv_sgmnt_5,
},
{
rw list.filter_map_append_append,
congr,
rw unwrap_wrap₁_string,
},
},
{
exact ih_y,
},
},
rw aft,
rw bef at ih_concat,
rw list.filter_map_append_append,
rw list.map_append_append,
rw list.append_assoc,
rw list.append_assoc,
apply corresponding_strings_append,
{
have part_for_u := corresponding_strings_take u.length ih_concat,
rw list.take_left at part_for_u,
have trivi : u.length ≤ (list.map (wrap_symbol₁ g₂.nt) x).length,
{
clear_except critical,
rw list.length_map,
omega,
},
rw list.take_append_of_le_length trivi at part_for_u,
clear_except part_for_u,
rw ←list.map_take at part_for_u,
apply corresponding_string_after_wrap_unwrap_self₁,
use list.take u.length x,
exact part_for_u,
},
apply corresponding_strings_append,
{
rw unwrap_wrap₁_string,
apply corresponding_strings_self,
},
convert_to
corresponding_strings _ (list.take (x.length - u.length - m) v ++ list.drop (x.length - u.length - m) v),
{
rw list.take_append_drop,
},
apply corresponding_strings_append,
{
have eqi := corresponding_strings_take (list.map (wrap_symbol₁ g₂.nt) x).length ih_concat,
rw list.take_left at eqi,
have part_for_v_beginning := corresponding_strings_drop (u.length + m) eqi,
clear_except part_for_v_beginning critical,
rw ←list.map_drop at part_for_v_beginning,
apply corresponding_string_after_wrap_unwrap_self₁,
use list.drop (u.length + m) x,
convert part_for_v_beginning,
clear part_for_v_beginning,
rw list.length_map,
rw list.take_append_eq_append_take,
rw list.drop_append_eq_append_drop,
have tul_lt : (list.take x.length u).length ≤ u.length + m,
{
rw list.length_take,
calc min x.length u.length
≤ u.length : min_le_right _ _
... ≤ u.length + m : le_self_add,
},
rw list.drop_eq_nil_of_le tul_lt,
rw list.nil_append,
rw ←list.append_assoc _ _ v,
rw ←list.append_assoc _ _ v,
rw ←list.append_assoc,
rw list.take_append_eq_append_take,
rw list.drop_append_eq_append_drop,
have rul_inp_len :
(list.map (wrap_symbol₁ g₂.nt) r₁.input_L ++
[symbol.nonterminal (sum.inl (some (sum.inl r₁.input_N)))] ++
list.map (wrap_symbol₁ g₂.nt) r₁.input_R
).length = m,
{
rw list.length_append_append,
rw list.length_singleton,
},
have u_is_shorter : min x.length u.length = u.length,
{
apply min_eq_right,
clear_except critical,
omega,
},
rw list.drop_eq_nil_of_le, swap,
{
rw list.length_take,
rw rul_inp_len,
rw list.length_take,
rw u_is_shorter,
calc min (x.length - u.length) m ≤ m : min_le_right _ _
... ≤ u.length + m - u.length : le_add_tsub_swap,
},
rw list.nil_append,
rw list.length_take,
rw list.length_take,
rw rul_inp_len,
have zero_dropping : u.length + m - min x.length u.length - min (x.length - u.length) m = 0,
{
have middle_cannot_exceed : min (x.length - u.length) m = m,
{
exact min_eq_right critical,
},
rw [u_is_shorter, middle_cannot_exceed],
clear_except,
omega,
},
rw zero_dropping,
unfold list.drop,
},
-- now we have what `g₂` generated
have reverse_concat := corresponding_strings_reverse ih_concat,
repeat {
rw list.reverse_append at reverse_concat,
},
have the_part := corresponding_strings_take y.length reverse_concat,
apply corresponding_strings_of_reverse,
have len_sum : y.length + (x.length - u.length - m) = v.length,
{
change
y.length + (x.length - u.length - (
(list.map (wrap_symbol₁ g₂.nt) r₁.input_L).length + 1 +
(list.map (wrap_symbol₁ g₂.nt) r₁.input_R).length
)) =
v.length,
have len_concat := corresponding_strings_length ih_concat,
clear_except len_concat critical,
repeat {
rw list.length_append at len_concat,
},
rw list.length_map at len_concat,
rw list.length_map at len_concat,
rw list.length_singleton at len_concat,
rw ←nat.add_sub_assoc, swap,
{
exact critical,
},
rw ←nat.add_sub_assoc, swap,
{
clear_except critical,
omega,
},
rw add_comm at len_concat,
rw len_concat,
clear len_concat,
rw add_tsub_cancel_left,
rw ←nat.add_assoc,
rw ←nat.add_assoc,
rw add_tsub_cancel_left,
},
have yl_lt_vl : y.length ≤ v.length,
{
exact nat.le.intro len_sum,
},
convert_to corresponding_strings _ (list.take y.length v.reverse),
{
convert_to (list.drop (v.length - y.length) v).reverse = list.take y.length v.reverse,
{
apply congr_arg,
apply congr_arg2,
{
clear_except len_sum,
omega,
},
{
refl,
},
},
rw list.reverse_take,
exact yl_lt_vl,
},
clear_except the_part yl_lt_vl,
rw list.take_append_of_le_length at the_part, swap,
{
rw list.length_reverse,
rw list.length_map,
},
repeat {
rw list.append_assoc at the_part,
},
rw list.take_append_of_le_length at the_part, swap,
{
rw list.length_reverse,
exact yl_lt_vl,
},
rw list.take_all_of_le at the_part, swap,
{
rw list.length_reverse,
rw list.length_map,
},
exact the_part,
end
private lemma induction_step_for_lifted_rule_from_g₂
{g₁ g₂ : grammar T}
{a b u v : list (nst T g₁.nt g₂.nt)}
{x : list (symbol T g₁.nt)}
{y : list (symbol T g₂.nt)}
{r : grule T (nnn T g₁.nt g₂.nt)}
(rin : r ∈ list.map (wrap_grule₂ g₁.nt) g₂.rules)
(bef : a = u ++ r.input_L ++ [symbol.nonterminal r.input_N] ++ r.input_R ++ v)
(aft : b = u ++ r.output_string ++ v)
(ih_x : grammar_derives g₁ [symbol.nonterminal g₁.initial] x)
(ih_y : grammar_derives g₂ [symbol.nonterminal g₂.initial] y)
(ih_concat :
corresponding_strings
(list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y)
a) :
∃ (y' : list (symbol T g₂.nt)),
and
(and
(grammar_derives g₁ [symbol.nonterminal g₁.initial] x)
(grammar_derives g₂ [symbol.nonterminal g₂.initial] y')
)
(corresponding_strings (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y') b) :=
begin
rw list.mem_map at rin,
rcases rin with ⟨r₂, rin₂, wrap_r₂_eq_r⟩,
rw ←wrap_r₂_eq_r at *,
clear wrap_r₂_eq_r,
simp [wrap_grule₂] at *,
rw ←list.singleton_append at bef,
rw bef at ih_concat,
let b' := list.drop x.length u ++ list.map (wrap_symbol₂ g₁.nt) r₂.output_string ++ v,
use list.filter_map unwrap_symbol₂ b',
have total_len := corresponding_strings_length ih_concat,
repeat {
rw list.length_append at total_len,
},
repeat {
rw list.length_map at total_len,
},
have matched_right : u.length ≥ x.length,
{
clear_except ih_concat total_len,
by_contradiction,
push_neg at h,
rename h ul_lt_xl,
have ul_lt_ihls : u.length < (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y).length,
{
rw list.length_append,
rw list.length_map,
rw list.length_map,
exact nat.lt_add_right _ _ _ ul_lt_xl,
},
have ul_lt_ihrs :
u.length <
(u
++ (list.map (wrap_symbol₂ g₁.nt) r₂.input_L
++ ([symbol.nonterminal (sum.inl (some (sum.inr r₂.input_N)))]
++ (list.map (wrap_symbol₂ g₁.nt) r₂.input_R
++ v)))
).length,
{
repeat {
rw list.length_append,
},
rw list.length_singleton,
clear_except,
linarith,
},
have ulth := corresponding_strings_nth_le ul_lt_ihls ul_lt_ihrs ih_concat,
rw list.nth_le_append ul_lt_ihls at ulth, swap,
{
rw list.length_map,
exact ul_lt_xl,
},
rw list.nth_le_append_right at ulth, swap,
{
refl,
},
rw list.nth_le_map at ulth, swap,
{
exact ul_lt_xl,
},
by_cases (list.map (wrap_symbol₂ g₁.nt) r₂.input_L).length > u.length - u.length,
{
rw list.nth_le_append _ h at ulth,
rw list.nth_le_map at ulth, swap,
{
rw list.length_map at h,
exact h,
},
exact corresponding_symbols_never₁ ulth,
},
push_neg at h,
rw list.nth_le_append_right h at ulth,
have matched_central_nt :
corresponding_symbols
(wrap_symbol₁ g₂.nt (x.nth_le u.length ul_lt_xl))
(wrap_symbol₂ g₁.nt (symbol.nonterminal r₂.input_N)),
{
clear_except ulth,
finish,
},
exact corresponding_symbols_never₁ matched_central_nt,
},
split,
{
split,
{
exact ih_x,
},
{
apply grammar_deri_of_deri_tran ih_y,
use r₂,
split,
{
exact rin₂,
},
use list.filter_map unwrap_symbol₂ (list.drop x.length u),
use list.filter_map unwrap_symbol₂ v,
split,
{
have corres_y := corresponding_strings_drop (list.map (wrap_symbol₁ g₂.nt) x).length ih_concat,
rw list.drop_left at corres_y,
rw list.drop_append_of_le_length at corres_y, swap,
{
rw list.length_map,
exact matched_right,
},
clear_except corres_y total_len,
repeat {
rw list.append_assoc,
},
obtain ⟨seg1, rest1⟩ :=
corresponding_strings_split
(list.drop (list.map (wrap_symbol₁ g₂.nt) x).length u).length
corres_y,
clear corres_y,
rw list.take_left at seg1,
rw list.drop_left at rest1,
rw ←list.take_append_drop (list.filter_map unwrap_symbol₂ (list.drop x.length u)).length y,
rw ←list.map_take at seg1,
have min_uxy : min (u.length - x.length) y.length = u.length - x.length,
{
rw min_eq_left,
clear_except total_len,
omega,
},
have tuxy : list.take (list.take (u.length - x.length) y).length y = list.take (u.length - x.length) y,
{
rw list.length_take,
rw min_uxy,
},
have fmu1 := filter_map_unwrap_of_corresponding_strings₂ seg1,
rw list.length_map at fmu1,
have fml :
(list.filter_map unwrap_symbol₂ (list.drop x.length u)).length =
(list.drop x.length u).length,
{
rw congr_arg list.length fmu1,
rw list.length_take at tuxy,
rw list.length_drop,
rw min_uxy at tuxy,
rw congr_arg list.length tuxy,
rw list.length_take,
exact min_uxy,
},
apply congr_arg2,
{
rw fmu1,
rw list.length_drop,
exact tuxy,
},
clear seg1 fmu1 tuxy min_uxy,
rw list.length_map at rest1,
obtain ⟨seg2, rest2⟩ :=
corresponding_strings_split
(list.map (wrap_symbol₂ g₁.nt) r₂.input_L).length
rest1,
clear rest1,
rw list.take_left at seg2,
rw list.drop_left at rest2,
rw ←list.take_append_drop
(list.map (wrap_symbol₂ g₁.nt) r₂.input_L).length
(list.drop (list.filter_map unwrap_symbol₂ (list.drop x.length u)).length y),
apply congr_arg2,
{
clear_except seg2 fml,
rw ←list.map_drop at seg2,
rw ←list.map_take at seg2,
have fmu2 := filter_map_unwrap_of_corresponding_strings₂ seg2,
rw list.length_map at fmu2,
rw list.length_map,
rw unwrap_wrap₂_string at fmu2,
rw fml,
exact fmu2.symm,
},
clear seg2,
rw list.length_map at rest2,
rw list.drop_drop at rest2 ⊢,
obtain ⟨seg3, rest3⟩ :=
corresponding_strings_split 1 rest2,
clear rest2,
rw list.take_left' (list.length_singleton _) at seg3,
rw list.drop_left' (list.length_singleton _) at rest3,
rw list.length_map,
rw fml,
rw ←list.take_append_drop
1
(list.drop (r₂.input_L.length + (list.drop x.length u).length) y),
apply congr_arg2,
{
rw ←list.map_drop at seg3,
rw ←list.map_take at seg3,
have fmu3 := filter_map_unwrap_of_corresponding_strings₂ seg3,
exact fmu3.symm,
},
clear seg3,
rw list.drop_drop at rest3 ⊢,
rw ←list.map_drop at rest3,
rw ←filter_map_unwrap_of_corresponding_strings₂ rest3,
rw list.filter_map_append,
rw unwrap_wrap₂_string,
},
{
rw list.filter_map_append_append,
congr,
apply unwrap_wrap₂_string,
}
},
},
rw aft,
rw list.filter_map_append_append,
rw list.map_append_append,
rw list.append_assoc,
rw ←list.append_assoc (list.map (wrap_symbol₁ g₂.nt) x),
apply corresponding_strings_append, swap,
{
rw unwrap_wrap₂_string,
apply corresponding_strings_append,
{
apply corresponding_strings_self,
},
apply corresponding_string_after_wrap_unwrap_self₂,
repeat {
rw ←list.append_assoc at ih_concat,
},
have rev := corresponding_strings_reverse ih_concat,
rw list.reverse_append _ v at rev,
have tak := corresponding_strings_take v.reverse.length rev,
rw list.take_left at tak,
have rtr := corresponding_strings_reverse tak,
have nec : v.reverse.length ≤ (list.map (wrap_symbol₂ g₁.nt) y).reverse.length,
{
clear_except matched_right total_len,
rw list.length_reverse,
rw list.length_reverse,
rw list.length_map,
linarith,
},
clear_except rtr nec,
rw list.reverse_reverse at rtr,
rw list.reverse_append at rtr,
rw list.take_append_of_le_length nec at rtr,
rw list.reverse_take at rtr, swap,
{
rw list.length_reverse (list.map (wrap_symbol₂ g₁.nt) y) at nec,
exact nec,
},
rw ←list.map_drop at rtr,
rw list.reverse_reverse at rtr,
exact ⟨_, rtr⟩,
},
rw ←list.take_append_drop x.length u,
apply corresponding_strings_append,
{
have almost := corresponding_strings_take x.length ih_concat,
rw list.take_append_of_le_length matched_right at almost,
convert almost,
have xl_eq : x.length = (list.map (wrap_symbol₁ g₂.nt) x).length,
{
rw list.length_map,
},
rw xl_eq,
rw list.take_left,
},
{
rw list.take_append_drop,
apply corresponding_string_after_wrap_unwrap_self₂,
have tdc := corresponding_strings_drop x.length (corresponding_strings_take u.length ih_concat),
rw list.take_left at tdc,
have ul_eq : u.length = x.length + (u.length - x.length),
{
rw ←nat.add_sub_assoc matched_right,
rw add_comm,
rw nat.add_sub_assoc, swap,
{
refl,
},
rw nat.sub_self,
rw add_zero,
},
rw ul_eq at tdc,
clear_except tdc,
rw list.drop_take at tdc,
rw list.drop_left' at tdc, swap,
{
apply list.length_map,
},
rw ←list.map_take at tdc,
exact ⟨_, tdc⟩,
},
end
private lemma big_induction
{g₁ g₂ : grammar T}
{w : list (nst T g₁.nt g₂.nt)}
(ass :
grammar_derives (big_grammar g₁ g₂)
[symbol.nonterminal (sum.inl (some (sum.inl g₁.initial))),
symbol.nonterminal (sum.inl (some (sum.inr g₂.initial)))]
w) :
∃ x : list (symbol T g₁.nt),
∃ y : list (symbol T g₂.nt),
and
(and
(grammar_derives g₁ [symbol.nonterminal g₁.initial] x)
(grammar_derives g₂ [symbol.nonterminal g₂.initial] y)
)
(corresponding_strings (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y) w) :=
begin
induction ass with a b trash orig ih,
{
use [[symbol.nonterminal g₁.initial], [symbol.nonterminal g₂.initial]],
split,
{
split;
apply grammar_deri_self,
},
{
rw list.map_singleton,
rw list.map_singleton,
unfold wrap_symbol₁,
unfold wrap_symbol₂,
rw list.singleton_append,
unfold corresponding_strings,
rw list.forall₂_cons,
split,
{
unfold corresponding_symbols,
},
rw list.forall₂_cons,
split,
{
unfold corresponding_symbols,
},
exact list.forall₂.nil,
},
},
rcases ih with ⟨x, y, ⟨ih_x, ih_y⟩, ih_concat⟩,
rcases orig with ⟨r, rin, u, v, bef, aft⟩,
change _ ∈ list.cons _ _ at rin,
rw list.mem_cons_eq at rin,
cases rin,
{
exfalso,
rw rin at bef,
clear_except ih_concat bef,
simp only [list.append_nil] at bef,
rw bef at ih_concat,
have same_lengths := corresponding_strings_length ih_concat,
clear bef,
have ulen₁ : u.length < (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y).length,
{
rw list.length_append _ v at same_lengths,
rw list.length_append u _ at same_lengths,
rw list.length_singleton at same_lengths,
clear_except same_lengths,
linarith,
},
have ulen₂ : u.length < (u ++ ([symbol.nonterminal (sum.inl none)] ++ v)).length,
{
rw list.length_append,
rw list.length_append,
rw list.length_singleton,
clear_except,
linarith,
},
have ulen_tauto : u.length ≤ u.length,
{
refl,
},
rw list.append_assoc at ih_concat,
have eqi_symb := corresponding_strings_nth_le ulen₁ ulen₂ ih_concat,
rw list.nth_le_append_right ulen_tauto at eqi_symb,
simp only [nat.sub_self, list.singleton_append, list.nth_le] at eqi_symb,
have eq_none :
(list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y).nth_le u.length ulen₁ =
(symbol.nonterminal (sum.inl (none))),
{
clear_except eqi_symb,
cases (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y).nth_le u.length ulen₁ with t s,
{
exfalso,
unfold corresponding_symbols at eqi_symb,
exact eqi_symb,
},
cases s,
{
cases s,
{
refl,
},
exfalso,
clear_except eqi_symb,
cases s;
tauto,
},
{
exfalso,
clear_except eqi_symb,
cases s;
tauto,
},
},
have impossible_in :
symbol.nonterminal (sum.inl none) ∈
(list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y),
{
rw list.mem_iff_nth_le,
use u.length,
use ulen₁,
exact eq_none,
},
rw list.mem_append at impossible_in,
cases impossible_in;
{
rw list.mem_map at impossible_in,
rcases impossible_in with ⟨s, -, contradic⟩,
clear_except contradic,
cases s,
{
have imposs := symbol.nonterminal.inj contradic,
exact sum.no_confusion imposs,
},
{
have impos := sum.inl.inj (symbol.nonterminal.inj contradic),
exact option.no_confusion impos,
},
},
},
rw list.mem_append at rin,
cases rin,
{
rw list.mem_append at rin,
cases rin,
{
cases induction_step_for_lifted_rule_from_g₁ rin bef aft ih_x ih_y ih_concat with x' pros,
exact ⟨x', y, pros⟩,
},
{
use x,
exact induction_step_for_lifted_rule_from_g₂ rin bef aft ih_x ih_y ih_concat,
}
},
{
use [x, y],
split,
{
split,
{
exact ih_x,
},
{
exact ih_y,
},
},
rw aft,
rw bef at ih_concat,
rw list.mem_append at rin,
cases rin,
{
unfold rules_for_terminals₁ at rin,
rw list.mem_map at rin,
rcases rin with ⟨t, -, eq_r⟩,
rw ←eq_r at *,
clear eq_r,
rw list.append_nil at ih_concat,
rw list.append_nil at ih_concat,
have xy_split_u :
list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y =
list.take u.length (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y) ++
list.drop u.length (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y),
{
rw list.take_append_drop,
},
rw xy_split_u,
have part_for_u := corresponding_strings_take u.length ih_concat,
rw list.append_assoc,
apply corresponding_strings_append,
{
convert part_for_u,
rw list.append_assoc,
rw list.take_left,
},
have ul_lt_len_um : u.length < (u ++ [symbol.nonterminal (sum.inr (sum.inl t))]).length,
{
rw list.length_append,
rw list.length_singleton,
apply lt_add_one,
},
have ul_lt_len_umv : u.length < (u ++ [symbol.nonterminal (sum.inr (sum.inl t))] ++ v).length,
{
rw list.length_append,
apply lt_of_lt_of_le ul_lt_len_um,
apply le_self_add,
},
have ul_lt_len_xy : u.length < (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y).length,
{
have same_len := corresponding_strings_length ih_concat,
rw same_len,
exact ul_lt_len_umv,
},
have middle_nt := corresponding_strings_nth_le ul_lt_len_xy ul_lt_len_umv ih_concat,
rw list.nth_le_append ul_lt_len_umv ul_lt_len_um at middle_nt,
rw list.nth_le_append_right (by refl) ul_lt_len_um at middle_nt,
have middle_nt_elem :
corresponding_symbols
((list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y).nth_le u.length ul_lt_len_xy)
(symbol.nonterminal (sum.inr (sum.inl t))),
{
convert middle_nt,
finish,
},
have xy_split_nt :
list.drop u.length (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y) =
list.take 1 (list.drop u.length (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y)) ++
list.drop 1 (list.drop u.length (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y)),
{
rw list.take_append_drop,
},
rw xy_split_nt,
apply corresponding_strings_append, swap,
{
rw list.drop_drop,
have part_for_v := corresponding_strings_drop (1 + u.length) ih_concat,
convert part_for_v,
have correct_len : 1 + u.length = (u ++ [symbol.nonterminal (sum.inr (sum.inl t))]).length,
{
rw add_comm,
rw list.length_append,
rw list.length_singleton,
},
rw correct_len,
rw list.drop_left,
},
{
convert_to
corresponding_strings
[list.nth_le (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y) u.length ul_lt_len_xy]
[symbol.terminal t],
{
apply list_take_one_drop,
},
clear_except middle_nt_elem,
apply corresponding_strings_singleton,
cases
(list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y).nth_le
u.length ul_lt_len_xy with e s,
{
exfalso,
unfold corresponding_symbols at middle_nt_elem,
exact middle_nt_elem,
},
cases s,
{
exfalso,
cases s, swap,
cases s,
all_goals {
unfold corresponding_symbols at middle_nt_elem,
exact middle_nt_elem,
},
},
{
cases s,
{
unfold corresponding_symbols at middle_nt_elem,
rw middle_nt_elem,
unfold corresponding_symbols,
},
{
exfalso,
unfold corresponding_symbols at middle_nt_elem,
exact middle_nt_elem,
},
}
},
},
{
unfold rules_for_terminals₂ at rin,
rw list.mem_map at rin,
rcases rin with ⟨t, -, eq_r⟩,
rw ←eq_r at *,
clear eq_r,
rw list.append_nil at ih_concat,
rw list.append_nil at ih_concat,
have xy_split_v :
list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y =
list.take (u.length + 1) (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y) ++
list.drop (u.length + 1) (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y),
{
rw list.take_append_drop,
},
rw xy_split_v,
have part_for_v := corresponding_strings_drop (u.length + 1) ih_concat,
apply corresponding_strings_append, swap,
{
convert part_for_v,
have rewr_len : u.length + 1 = (u ++ [symbol.nonterminal (sum.inr (sum.inr t))]).length,
{
rw list.length_append,
rw list.length_singleton,
},
rw rewr_len,
rw list.drop_left,
},
have ul_lt_len_um : u.length < (u ++ [symbol.nonterminal (sum.inr (sum.inr t))]).length,
{
rw list.length_append,
rw list.length_singleton,
apply lt_add_one,
},
have ul_lt_len_umv : u.length < (u ++ [symbol.nonterminal (sum.inr (sum.inr t))] ++ v).length,
{
rw list.length_append,
apply lt_of_lt_of_le ul_lt_len_um,
apply le_self_add,
},
have ul_lt_len_xy : u.length < (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y).length,
{
have same_len := corresponding_strings_length ih_concat,
rw same_len,
exact ul_lt_len_umv,
},
have middle_nt := corresponding_strings_nth_le ul_lt_len_xy ul_lt_len_umv ih_concat,
rw list.nth_le_append ul_lt_len_umv ul_lt_len_um at middle_nt,
rw list.nth_le_append_right (by refl) ul_lt_len_um at middle_nt,
have middle_nt_elem :
corresponding_symbols
((list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y).nth_le u.length ul_lt_len_xy)
(symbol.nonterminal (sum.inr (sum.inr t))),
{
convert middle_nt,
finish,
},
have xy_split_nt :
list.take (u.length + 1) (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y) =
list.take u.length (list.take (u.length + 1) (
list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y)
) ++
list.drop u.length (list.take (u.length + 1) (
list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y)
),
{
rw list.take_append_drop u.length,
},
rw xy_split_nt,
apply corresponding_strings_append,
{
rw list.take_take,
have part_for_u := corresponding_strings_take u.length ih_concat,
convert part_for_u,
{
apply min_eq_left,
apply nat.le_succ,
},
rw list.append_assoc,
rw list.take_left,
},
{
convert_to
corresponding_strings
[list.nth_le (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y) u.length ul_lt_len_xy]
[symbol.terminal t],
{
apply list_drop_take_succ,
},
clear_except middle_nt_elem,
apply corresponding_strings_singleton,
cases
(list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y).nth_le
u.length ul_lt_len_xy with e s,
{
exfalso,
unfold corresponding_symbols at middle_nt_elem,
exact middle_nt_elem,
},
cases s,
{
exfalso,
cases s, swap,
cases s,
all_goals {
unfold corresponding_symbols at middle_nt_elem,
exact middle_nt_elem,
},
},
{
cases s,
{
exfalso,
unfold corresponding_symbols at middle_nt_elem,
exact middle_nt_elem,
},
{
unfold corresponding_symbols at middle_nt_elem,
rw middle_nt_elem,
unfold corresponding_symbols,
},
}
},
},
},
end
protected lemma in_concatenated_of_in_big
{g₁ g₂ : grammar T}
{w : list T}
(ass : w ∈ grammar_language (big_grammar g₁ g₂)) :
w ∈ grammar_language g₁ * grammar_language g₂ :=
begin
rw language.mem_mul,
cases grammar_tran_or_id_of_deri ass,
{
exfalso,
have nonmatch := congr_fun (congr_arg list.nth h) 0,
clear_except nonmatch,
unfold list.nth at nonmatch,
cases w,
{
rw list.map_nil at nonmatch,
exact option.no_confusion nonmatch,
},
{
unfold list.map at nonmatch,
unfold list.nth at nonmatch,
have imposs := option.some.inj nonmatch,
exact symbol.no_confusion imposs,
},
},
clear ass,
rcases h with ⟨w₁, hyp_tran, hyp_deri⟩,
have w₁eq : w₁ = [
symbol.nonterminal (sum.inl (some (sum.inl g₁.initial))),
symbol.nonterminal (sum.inl (some (sum.inr g₂.initial)))
],
{
clear_except hyp_tran,
-- only the first rule is applicable
rcases hyp_tran with ⟨r, rin, u, v, bef, aft⟩,
have bef_len := congr_arg list.length bef,
rw list.length_append_append at bef_len,
rw list.length_append_append at bef_len,
rw list.length_singleton at bef_len,
rw list.length_singleton at bef_len,
have u_nil : u = [], swap,
have v_nil : v = [], swap,
have rif_nil : r.input_L = [], swap,
any_goals {
clear_except bef_len,
rw ←list.length_eq_zero,
linarith,
},
change _ ∈ list.cons _ _ at rin,
rw list.mem_cons_iff at rin,
cases rin,
{
rw rin at bef aft,
dsimp only at bef aft,
rw [u_nil, v_nil] at aft,
rw [list.nil_append, list.append_nil] at aft,
exact aft,
},
exfalso,
have nt_match : symbol.nonterminal (big_grammar g₁ g₂).initial = symbol.nonterminal r.input_N,
{
have bef_fst := congr_fun (congr_arg list.nth bef) 0,
rw [u_nil, rif_nil] at bef_fst,
rw ←option.some_inj,
exact bef_fst,
},
clear_except rin nt_match,
repeat {
rw list.mem_append at rin,
},
cases rin,
{
cases rin,
{
rw list.mem_map at rin,
rcases rin with ⟨r₀, hr₀g₁, wrap_eq_r⟩,
rw ←wrap_eq_r at nt_match,
unfold wrap_grule₁ at nt_match,
have inl_match := symbol.nonterminal.inj nt_match,
change sum.inl none = sum.inl (some (sum.inl r₀.input_N)) at inl_match,
have none_eq_some := sum.inl.inj inl_match,
exact option.no_confusion none_eq_some,
},
{
rw list.mem_map at rin,
rcases rin with ⟨r₀, hr₀g₂, wrap_eq_r⟩,
rw ←wrap_eq_r at nt_match,
unfold wrap_grule₂ at nt_match,
have inl_match := symbol.nonterminal.inj nt_match,
change sum.inl none = sum.inl (some (sum.inr r₀.input_N)) at inl_match,
have none_eq_some := sum.inl.inj inl_match,
exact option.no_confusion none_eq_some,
},
},
{
cases rin,
{
unfold rules_for_terminals₁ at rin,
rw list.mem_map at rin,
rcases rin with ⟨t, htg₁, tt_eq_r⟩,
rw ←tt_eq_r at nt_match,
have inl_eq_inr := symbol.nonterminal.inj nt_match,
exact sum.no_confusion inl_eq_inr,
},
{
unfold rules_for_terminals₂ at rin,
rw list.mem_map at rin,
rcases rin with ⟨t, htg₂, tt_eq_r⟩,
rw ←tt_eq_r at nt_match,
have inl_eq_inr := symbol.nonterminal.inj nt_match,
exact sum.no_confusion inl_eq_inr,
},
},
},
clear hyp_tran,
rw w₁eq at hyp_deri,
have hope_result := big_induction hyp_deri,
clear_except hope_result,
rcases hope_result with ⟨x, y, ⟨deri_x, deri_y⟩, concat_xy⟩,
use list.take x.length w,
use list.drop x.length w,
split,
{
clear deri_y,
unfold grammar_language,
rw set.mem_set_of_eq,
unfold grammar_generates,
convert deri_x,
clear deri_x,
have xylen := corresponding_strings_length concat_xy,
rw list.length_append at xylen,
repeat {
rw list.length_map at xylen,
},
ext1 i,
by_cases i ≥ x.length,
{
convert_to none = none,
{
have xlen : x.length = (list.map (@symbol.terminal T g₁.nt) (list.take x.length w)).length,
{
clear_except xylen,
rw list.length_map,
rw list.length_take,
symmetry,
apply min_eq_left,
exact nat.le.intro xylen,
},
rw xlen at h,
clear_except h,
rw list.nth_eq_none_iff,
exact h,
},
{
clear_except h,
rw list.nth_eq_none_iff,
exact h,
},
refl,
},
push_neg at h,
rename h i_lt_len_x,
have i_lt_len_lwx : i < (list.map (wrap_symbol₁ g₂.nt) x).length,
{
rw list.length_map,
exact i_lt_len_x,
},
have i_lt_len_w : i < w.length,
{
apply lt_of_lt_of_le i_lt_len_x,
exact nat.le.intro xylen,
},
have i_lt_len₁ : i < (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y).length,
{
rw list.length_append,
apply lt_of_lt_of_le i_lt_len_lwx,
apply le_self_add,
},
have i_lt_len₂ : i < (list.map symbol.terminal w).length,
{
rw list.length_map,
exact i_lt_len_w,
},
rw list.nth_map,
rw list.nth_take i_lt_len_x,
have equivalent_ith :
corresponding_symbols
(list.nth_le (list.map (wrap_symbol₁ g₂.nt) x ++ list.map (wrap_symbol₂ g₁.nt) y) i i_lt_len₁)
(list.nth_le (list.map symbol.terminal w) i i_lt_len₂),
{
apply corresponding_strings_nth_le,
exact concat_xy,
},
rw list.nth_le_map at equivalent_ith, swap,
{
exact i_lt_len_w,
},
rw list.nth_le_append at equivalent_ith, swap,
{
exact i_lt_len_lwx,
},
rw list.nth_le_map at equivalent_ith, swap,
{
exact i_lt_len_x,
},
clear_except equivalent_ith,
rw list.nth_le_nth i_lt_len_x,
cases x.nth_le i i_lt_len_x with t n;
unfold wrap_symbol₁ at equivalent_ith;
unfold corresponding_symbols at equivalent_ith,
{
have symbol_ith := congr_arg (@symbol.terminal T g₁.nt) equivalent_ith,
rw list.nth_le_nth i_lt_len_w,
rw option.map_some',
exact congr_arg option.some symbol_ith,
},
{
exfalso,
exact equivalent_ith,
},
},
split,
{
clear deri_x,
unfold grammar_language,
rw set.mem_set_of_eq,
unfold grammar_generates,
convert deri_y,
clear deri_y,
have xylen := corresponding_strings_length concat_xy,
rw list.length_append at xylen,
repeat {
rw list.length_map at xylen,
},
ext1 i,
by_cases i ≥ y.length,
{
convert_to none = none,
{
have ylen : y.length = (list.map (@symbol.terminal T g₁.nt) (list.drop x.length w)).length,
{
clear_except xylen,
rw list.length_map,
rw list.length_drop,
omega,
},
rw ylen at h,
clear_except h,
rw list.nth_eq_none_iff,
rw list.length_map at *,
exact h,
},
{
clear_except h,
rw list.nth_eq_none_iff,
exact h,
},
refl,
},
push_neg at h,
rename h i_lt_len_y,
rw ←list.take_append_drop (list.map (wrap_symbol₁ g₂.nt) x).length (list.map symbol.terminal w) at concat_xy,
rw list.nth_map,
have equivalent_second_parts :
corresponding_strings
(list.map (wrap_symbol₂ g₁.nt) y)
(list.drop (list.map (wrap_symbol₁ g₂.nt) x).length (list.map symbol.terminal w)),
{
have llen_eq_llen :
(list.map (wrap_symbol₁ g₂.nt) x).length =
(list.take (list.map (wrap_symbol₁ g₂.nt) x).length (list.map symbol.terminal w)).length,
{
rw list.length_take,
symmetry,
apply min_eq_left,
rw list.length_map,
rw list.length_map,
exact nat.le.intro xylen,
},
convert corresponding_strings_drop (list.map (wrap_symbol₁ g₂.nt) x).length concat_xy,
{
rw list.drop_left,
},
{
rw list.take_append_drop,
},
exact T,
},
clear concat_xy,
symmetry,
have i_lt_len_lwy : i < (list.map (wrap_symbol₂ g₁.nt) y).length,
{
rw list.length_map,
exact i_lt_len_y,
},
have i_lt_len_dxw : i < (list.drop x.length (list.map symbol.terminal w)).length,
{
rw list.length_drop,
rw list.length_map,
rw ←xylen,
convert i_lt_len_lwy,
rw list.length_map,
rw add_comm,
rw nat.add_sub_assoc,
rw nat.sub_self,
rw nat.add_zero,
refl,
},
have i_lt_len_mtw : i < (list.map symbol.terminal (list.drop x.length w)).length,
{
convert i_lt_len_dxw,
apply list.map_drop,
},
have i_lt_len_dlmxw :
i < (list.drop (list.map (wrap_symbol₁ g₂.nt) x).length (list.map symbol.terminal w)).length,
{
rw list.length_map,
-- DO NOT call `i_lt_len_dxw` even though it looks like a good idea!
rw list.length_drop,
rw list.length_map,
rw ←xylen,
convert i_lt_len_lwy,
rw list.length_map,
rw add_comm,
rw nat.add_sub_assoc,
rw nat.sub_self,
rw nat.add_zero,
refl,
},
have eqiv_symb := corresponding_strings_nth_le i_lt_len_lwy i_lt_len_dlmxw equivalent_second_parts,
have goal_as_ith_drop :
y.nth_le i i_lt_len_y = (list.drop x.length (list.map symbol.terminal w)).nth_le i i_lt_len_dxw,
{
have xli_lt_len_w : x.length + i < w.length,
{
clear_except i_lt_len_y xylen,
linarith,
},
rw list.nth_le_map _ _ i_lt_len_y at eqiv_symb,
rw list.nth_le_drop' at *,
rw list.nth_le_map, swap,
{
exact xli_lt_len_w,
},
rw list.nth_le_map at eqiv_symb, swap,
{
rw list.length_map,
exact xli_lt_len_w,
},
clear_except eqiv_symb,
cases y.nth_le i i_lt_len_y with t n,
{
unfold wrap_symbol₂ at eqiv_symb,
unfold corresponding_symbols at eqiv_symb,
have eq_symb := congr_arg symbol.terminal eqiv_symb,
rw ←eq_symb,
apply congr_arg symbol.terminal,
simp only [list.length_map],
},
{
exfalso,
unfold wrap_symbol₂ at eqiv_symb,
unfold corresponding_symbols at eqiv_symb,
exact eqiv_symb,
},
},
have goal_as_some_ith :
some (y.nth_le i i_lt_len_y) =
some ((list.map symbol.terminal (list.drop x.length w)).nth_le i i_lt_len_mtw),
{
rw goal_as_ith_drop,
clear_except,
congr,
rw list.map_drop,
},
clear_except goal_as_some_ith,
rw ←list.nth_le_nth i_lt_len_y at goal_as_some_ith,
rw ←list.nth_le_nth i_lt_len_mtw at goal_as_some_ith,
convert goal_as_some_ith,
rw list.nth_map,
},
apply list.take_append_drop,
end
end very_complicated
end hard_direction
/-- The class of recursively-enumerable languages is closed under concatenation. -/
theorem RE_of_RE_c_RE (L₁ : language T) (L₂ : language T) :
is_RE L₁ ∧ is_RE L₂ → is_RE (L₁ * L₂) :=
begin
rintro ⟨⟨g₁, eq_L₁⟩, ⟨g₂, eq_L₂⟩⟩,
use big_grammar g₁ g₂,
apply set.eq_of_subset_of_subset,
{
-- prove `L₁ * L₂ ⊇` here
intros w hyp,
rw ←eq_L₁,
rw ←eq_L₂,
exact in_concatenated_of_in_big hyp,
},
{
-- prove `L₁ * L₂ ⊆` here
intros w hyp,
rw ←eq_L₁ at hyp,
rw ←eq_L₂ at hyp,
exact in_big_of_in_concatenated hyp,
},
end
|
48f3575f57c8802dab38ceed77bbe5bda3b78a57 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/algebra/big_operators/basic.lean | 4c941cb016542e88cb80f11eacfdfa8fddcb77be | [
"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 | 70,837 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import algebra.group.pi
import algebra.hom.equiv
import algebra.ring.opposite
import data.finset.fold
import data.fintype.basic
import data.set.pairwise
/-!
# Big operators
In this file we define products and sums indexed by finite sets (specifically, `finset`).
## Notation
We introduce the following notation, localized in `big_operators`.
To enable the notation, use `open_locale big_operators`.
Let `s` be a `finset α`, and `f : α → β` a function.
* `∏ x in s, f x` is notation for `finset.prod s f` (assuming `β` is a `comm_monoid`)
* `∑ x in s, f x` is notation for `finset.sum s f` (assuming `β` is an `add_comm_monoid`)
* `∏ x, f x` is notation for `finset.prod finset.univ f`
(assuming `α` is a `fintype` and `β` is a `comm_monoid`)
* `∑ x, f x` is notation for `finset.sum finset.univ f`
(assuming `α` is a `fintype` and `β` is an `add_comm_monoid`)
## Implementation Notes
The first arguments in all definitions and lemmas is the codomain of the function of the big
operator. This is necessary for the heuristic in `@[to_additive]`.
See the documentation of `to_additive.attr` for more information.
-/
universes u v w
variables {β : Type u} {α : Type v} {γ : Type w}
namespace finset
/--
`∏ x in s, f x` is the product of `f x`
as `x` ranges over the elements of the finite set `s`.
-/
@[to_additive "`∑ x in s, f x` is the sum of `f x` as `x` ranges over the elements
of the finite set `s`."]
protected def prod [comm_monoid β] (s : finset α) (f : α → β) : β := (s.1.map f).prod
@[simp, to_additive] lemma prod_mk [comm_monoid β] (s : multiset α) (hs : s.nodup) (f : α → β) :
(⟨s, hs⟩ : finset α).prod f = (s.map f).prod :=
rfl
end finset
/--
There is no established mathematical convention
for the operator precedence of big operators like `∏` and `∑`.
We will have to make a choice.
Online discussions, such as https://math.stackexchange.com/q/185538/30839
seem to suggest that `∏` and `∑` should have the same precedence,
and that this should be somewhere between `*` and `+`.
The latter have precedence levels `70` and `65` respectively,
and we therefore choose the level `67`.
In practice, this means that parentheses should be placed as follows:
```lean
∑ k in K, (a k + b k) = ∑ k in K, a k + ∑ k in K, b k →
∏ k in K, a k * b k = (∏ k in K, a k) * (∏ k in K, b k)
```
(Example taken from page 490 of Knuth's *Concrete Mathematics*.)
-/
library_note "operator precedence of big operators"
localized "notation `∑` binders `, ` r:(scoped:67 f, finset.sum finset.univ f) := r"
in big_operators
localized "notation `∏` binders `, ` r:(scoped:67 f, finset.prod finset.univ f) := r"
in big_operators
localized "notation `∑` binders ` in ` s `, ` r:(scoped:67 f, finset.sum s f) := r"
in big_operators
localized "notation `∏` binders ` in ` s `, ` r:(scoped:67 f, finset.prod s f) := r"
in big_operators
open_locale big_operators
namespace finset
variables {s s₁ s₂ : finset α} {a : α} {f g : α → β}
@[to_additive] lemma prod_eq_multiset_prod [comm_monoid β] (s : finset α) (f : α → β) :
∏ x in s, f x = (s.1.map f).prod := rfl
@[to_additive]
theorem prod_eq_fold [comm_monoid β] (s : finset α) (f : α → β) :
∏ x in s, f x = s.fold (*) 1 f :=
rfl
@[simp] lemma sum_multiset_singleton (s : finset α) :
s.sum (λ x, {x}) = s.val :=
by simp only [sum_eq_multiset_sum, multiset.sum_map_singleton]
end finset
@[to_additive]
lemma map_prod [comm_monoid β] [comm_monoid γ] {G : Type*} [monoid_hom_class G β γ] (g : G)
(f : α → β) (s : finset α) :
g (∏ x in s, f x) = ∏ x in s, g (f x) :=
by simp only [finset.prod_eq_multiset_prod, map_multiset_prod, multiset.map_map]
section deprecated
/-- Deprecated: use `_root_.map_prod` instead. -/
@[to_additive "Deprecated: use `_root_.map_sum` instead."]
protected lemma monoid_hom.map_prod [comm_monoid β] [comm_monoid γ] (g : β →* γ) (f : α → β)
(s : finset α) : g (∏ x in s, f x) = ∏ x in s, g (f x) :=
map_prod g f s
/-- Deprecated: use `_root_.map_prod` instead. -/
@[to_additive "Deprecated: use `_root_.map_sum` instead."]
protected lemma mul_equiv.map_prod [comm_monoid β] [comm_monoid γ] (g : β ≃* γ) (f : α → β)
(s : finset α) : g (∏ x in s, f x) = ∏ x in s, g (f x) :=
map_prod g f s
/-- Deprecated: use `_root_.map_list_prod` instead. -/
protected lemma ring_hom.map_list_prod [semiring β] [semiring γ] (f : β →+* γ) (l : list β) :
f l.prod = (l.map f).prod :=
map_list_prod f l
/-- Deprecated: use `_root_.map_list_sum` instead. -/
protected lemma ring_hom.map_list_sum [non_assoc_semiring β] [non_assoc_semiring γ]
(f : β →+* γ) (l : list β) :
f l.sum = (l.map f).sum :=
map_list_sum f l
/-- A morphism into the opposite ring acts on the product by acting on the reversed elements.
Deprecated: use `_root_.unop_map_list_prod` instead.
-/
protected lemma ring_hom.unop_map_list_prod [semiring β] [semiring γ] (f : β →+* γᵐᵒᵖ)
(l : list β) : mul_opposite.unop (f l.prod) = (l.map (mul_opposite.unop ∘ f)).reverse.prod :=
unop_map_list_prod f l
/-- Deprecated: use `_root_.map_multiset_prod` instead. -/
protected lemma ring_hom.map_multiset_prod [comm_semiring β] [comm_semiring γ] (f : β →+* γ)
(s : multiset β) :
f s.prod = (s.map f).prod :=
map_multiset_prod f s
/-- Deprecated: use `_root_.map_multiset_sum` instead. -/
protected lemma ring_hom.map_multiset_sum [non_assoc_semiring β] [non_assoc_semiring γ]
(f : β →+* γ) (s : multiset β) :
f s.sum = (s.map f).sum :=
map_multiset_sum f s
/-- Deprecated: use `_root_.map_prod` instead. -/
protected lemma ring_hom.map_prod [comm_semiring β] [comm_semiring γ] (g : β →+* γ) (f : α → β)
(s : finset α) :
g (∏ x in s, f x) = ∏ x in s, g (f x) :=
map_prod g f s
/-- Deprecated: use `_root_.map_sum` instead. -/
protected lemma ring_hom.map_sum [non_assoc_semiring β] [non_assoc_semiring γ]
(g : β →+* γ) (f : α → β) (s : finset α) :
g (∑ x in s, f x) = ∑ x in s, g (f x) :=
map_sum g f s
end deprecated
@[to_additive]
lemma monoid_hom.coe_finset_prod [mul_one_class β] [comm_monoid γ] (f : α → β →* γ) (s : finset α) :
⇑(∏ x in s, f x) = ∏ x in s, f x :=
(monoid_hom.coe_fn β γ).map_prod _ _
-- See also `finset.prod_apply`, with the same conclusion
-- but with the weaker hypothesis `f : α → β → γ`.
@[simp, to_additive]
lemma monoid_hom.finset_prod_apply [mul_one_class β] [comm_monoid γ] (f : α → β →* γ)
(s : finset α) (b : β) : (∏ x in s, f x) b = ∏ x in s, f x b :=
(monoid_hom.eval b).map_prod _ _
variables {s s₁ s₂ : finset α} {a : α} {f g : α → β}
namespace finset
section comm_monoid
variables [comm_monoid β]
@[simp, to_additive]
lemma prod_empty {f : α → β} : (∏ x in (∅:finset α), f x) = 1 := rfl
@[simp, to_additive]
lemma prod_cons (h : a ∉ s) : (∏ x in (cons a s h), f x) = f a * ∏ x in s, f x :=
fold_cons h
@[simp, to_additive]
lemma prod_insert [decidable_eq α] : a ∉ s → (∏ x in (insert a s), f x) = f a * ∏ x in s, f x :=
fold_insert
/--
The product of `f` over `insert a s` is the same as
the product over `s`, as long as `a` is in `s` or `f a = 1`.
-/
@[simp, to_additive "The sum of `f` over `insert a s` is the same as
the sum over `s`, as long as `a` is in `s` or `f a = 0`."]
lemma prod_insert_of_eq_one_if_not_mem [decidable_eq α] (h : a ∉ s → f a = 1) :
∏ x in insert a s, f x = ∏ x in s, f x :=
begin
by_cases hm : a ∈ s,
{ simp_rw insert_eq_of_mem hm },
{ rw [prod_insert hm, h hm, one_mul] },
end
/--
The product of `f` over `insert a s` is the same as the product over `s`, as long as `f a = 1`.
-/
@[simp, to_additive "The sum of `f` over `insert a s` is the same as
the sum over `s`, as long as `f a = 0`."]
lemma prod_insert_one [decidable_eq α] (h : f a = 1) :
∏ x in insert a s, f x = ∏ x in s, f x :=
prod_insert_of_eq_one_if_not_mem (λ _, h)
@[simp, to_additive]
lemma prod_singleton : (∏ x in (singleton a), f x) = f a :=
eq.trans fold_singleton $ mul_one _
@[to_additive]
lemma prod_pair [decidable_eq α] {a b : α} (h : a ≠ b) :
(∏ x in ({a, b} : finset α), f x) = f a * f b :=
by rw [prod_insert (not_mem_singleton.2 h), prod_singleton]
@[simp, priority 1100, to_additive]
lemma prod_const_one : (∏ x in s, (1 : β)) = 1 :=
by simp only [finset.prod, multiset.map_const, multiset.prod_repeat, one_pow]
@[simp, to_additive]
lemma prod_image [decidable_eq α] {s : finset γ} {g : γ → α} :
(∀ x ∈ s, ∀ y ∈ s, g x = g y → x = y) → (∏ x in (s.image g), f x) = ∏ x in s, f (g x) :=
fold_image
@[simp, to_additive]
lemma prod_map (s : finset α) (e : α ↪ γ) (f : γ → β) :
(∏ x in (s.map e), f x) = ∏ x in s, f (e x) :=
by rw [finset.prod, finset.map_val, multiset.map_map]; refl
@[congr, to_additive]
lemma prod_congr (h : s₁ = s₂) : (∀ x ∈ s₂, f x = g x) → s₁.prod f = s₂.prod g :=
by rw [h]; exact fold_congr
attribute [congr] finset.sum_congr
@[to_additive]
lemma prod_union_inter [decidable_eq α] :
(∏ x in (s₁ ∪ s₂), f x) * (∏ x in (s₁ ∩ s₂), f x) = (∏ x in s₁, f x) * (∏ x in s₂, f x) :=
fold_union_inter
@[to_additive]
lemma prod_union [decidable_eq α] (h : disjoint s₁ s₂) :
(∏ x in (s₁ ∪ s₂), f x) = (∏ x in s₁, f x) * (∏ x in s₂, f x) :=
by rw [←prod_union_inter, (disjoint_iff_inter_eq_empty.mp h)]; exact (mul_one _).symm
@[to_additive]
lemma prod_filter_mul_prod_filter_not (s : finset α) (p : α → Prop) [decidable_pred p]
[decidable_pred (λ x, ¬p x)] (f : α → β) :
(∏ x in s.filter p, f x) * (∏ x in s.filter (λ x, ¬p x), f x) = ∏ x in s, f x :=
begin
haveI := classical.dec_eq α,
rw [← prod_union (filter_inter_filter_neg_eq p s).le, filter_union_filter_neg_eq]
end
section to_list
@[simp, to_additive]
lemma prod_to_list (s : finset α) (f : α → β) : (s.to_list.map f).prod = s.prod f :=
by rw [finset.prod, ← multiset.coe_prod, ← multiset.coe_map, finset.coe_to_list]
end to_list
@[to_additive]
lemma _root_.equiv.perm.prod_comp (σ : equiv.perm α) (s : finset α) (f : α → β)
(hs : {a | σ a ≠ a} ⊆ s) :
(∏ x in s, f (σ x)) = ∏ x in s, f x :=
by { convert (prod_map _ σ.to_embedding _).symm, exact (map_perm hs).symm }
@[to_additive]
lemma _root_.equiv.perm.prod_comp' (σ : equiv.perm α) (s : finset α) (f : α → α → β)
(hs : {a | σ a ≠ a} ⊆ s) :
(∏ x in s, f (σ x) x) = ∏ x in s, f x (σ.symm x) :=
by { convert σ.prod_comp s (λ x, f x (σ.symm x)) hs, ext, rw equiv.symm_apply_apply }
end comm_monoid
end finset
section
open finset
variables [fintype α] [decidable_eq α] [comm_monoid β]
@[to_additive]
lemma is_compl.prod_mul_prod {s t : finset α} (h : is_compl s t) (f : α → β) :
(∏ i in s, f i) * (∏ i in t, f i) = ∏ i, f i :=
(finset.prod_union h.disjoint).symm.trans $ by rw [← finset.sup_eq_union, h.sup_eq_top]; refl
end
namespace finset
section comm_monoid
variables [comm_monoid β]
/-- Multiplying the products of a function over `s` and over `sᶜ` gives the whole product.
For a version expressed with subtypes, see `fintype.prod_subtype_mul_prod_subtype`. -/
@[to_additive "Adding the sums of a function over `s` and over `sᶜ` gives the whole sum.
For a version expressed with subtypes, see `fintype.sum_subtype_add_sum_subtype`. "]
lemma prod_mul_prod_compl [fintype α] [decidable_eq α] (s : finset α) (f : α → β) :
(∏ i in s, f i) * (∏ i in sᶜ, f i) = ∏ i, f i :=
is_compl.prod_mul_prod is_compl_compl f
@[to_additive]
lemma prod_compl_mul_prod [fintype α] [decidable_eq α] (s : finset α) (f : α → β) :
(∏ i in sᶜ, f i) * (∏ i in s, f i) = ∏ i, f i :=
(@is_compl_compl _ s _).symm.prod_mul_prod f
@[to_additive]
lemma prod_sdiff [decidable_eq α] (h : s₁ ⊆ s₂) :
(∏ x in (s₂ \ s₁), f x) * (∏ x in s₁, f x) = (∏ x in s₂, f x) :=
by rw [←prod_union sdiff_disjoint, sdiff_union_of_subset h]
@[simp, to_additive]
lemma prod_sum_elim [decidable_eq (α ⊕ γ)]
(s : finset α) (t : finset γ) (f : α → β) (g : γ → β) :
∏ x in s.map function.embedding.inl ∪ t.map function.embedding.inr, sum.elim f g x =
(∏ x in s, f x) * (∏ x in t, g x) :=
begin
rw [prod_union, prod_map, prod_map],
{ simp only [sum.elim_inl, function.embedding.inl_apply, function.embedding.inr_apply,
sum.elim_inr] },
{ simp only [disjoint_left, finset.mem_map, finset.mem_map],
rintros _ ⟨i, hi, rfl⟩ ⟨j, hj, H⟩,
cases H }
end
@[simp, to_additive]
lemma prod_on_sum [fintype α] [fintype γ] (f : α ⊕ γ → β) :
∏ (x : α ⊕ γ), f x =
(∏ (x : α), f (sum.inl x)) * (∏ (x : γ), f (sum.inr x)) :=
begin
haveI := classical.dec_eq (α ⊕ γ),
convert prod_sum_elim univ univ (λ x, f (sum.inl x)) (λ x, f (sum.inr x)),
{ ext a,
split,
{ intro x,
cases a,
{ simp only [mem_union, mem_map, mem_univ, function.embedding.inl_apply, or_false,
exists_true_left, exists_apply_eq_apply, function.embedding.inr_apply, exists_false], },
{ simp only [mem_union, mem_map, mem_univ, function.embedding.inl_apply, false_or,
exists_true_left, exists_false, function.embedding.inr_apply,
exists_apply_eq_apply], }, },
{ simp only [mem_univ, implies_true_iff], }, },
{ simp only [sum.elim_comp_inl_inr], },
end
@[to_additive]
lemma prod_bUnion [decidable_eq α] {s : finset γ} {t : γ → finset α}
(hs : set.pairwise_disjoint ↑s t) :
(∏ x in (s.bUnion t), f x) = ∏ x in s, ∏ i in t x, f i :=
begin
haveI := classical.dec_eq γ,
induction s using finset.induction_on with x s hxs ih hd,
{ simp_rw [bUnion_empty, prod_empty] },
{ simp_rw [coe_insert, set.pairwise_disjoint_insert, mem_coe] at hs,
have : disjoint (t x) (finset.bUnion s t),
{ exact (disjoint_bUnion_right _ _ _).mpr (λ y hy, hs.2 y hy $ λ H, hxs $ H.substr hy) },
rw [bUnion_insert, prod_insert hxs, prod_union this, ih hs.1] }
end
/-- Product over a sigma type equals the product of fiberwise products. For rewriting
in the reverse direction, use `finset.prod_sigma'`. -/
@[to_additive "Sum over a sigma type equals the sum of fiberwise sums. For rewriting
in the reverse direction, use `finset.sum_sigma'`"]
lemma prod_sigma {σ : α → Type*}
(s : finset α) (t : Π a, finset (σ a)) (f : sigma σ → β) :
(∏ x in s.sigma t, f x) = ∏ a in s, ∏ s in (t a), f ⟨a, s⟩ :=
by classical;
calc (∏ x in s.sigma t, f x) =
∏ x in s.bUnion (λ a, (t a).map (function.embedding.sigma_mk a)), f x : by rw sigma_eq_bUnion
... = ∏ a in s, ∏ x in (t a).map (function.embedding.sigma_mk a), f x :
prod_bUnion $ assume a₁ ha a₂ ha₂ h x hx,
by { simp only [inf_eq_inter, mem_inter, mem_map, function.embedding.sigma_mk_apply] at hx,
rcases hx with ⟨⟨y, hy, rfl⟩, ⟨z, hz, hz'⟩⟩, cc }
... = ∏ a in s, ∏ s in t a, f ⟨a, s⟩ :
prod_congr rfl $ λ _ _, prod_map _ _ _
@[to_additive]
lemma prod_sigma' {σ : α → Type*}
(s : finset α) (t : Π a, finset (σ a)) (f : Π a, σ a → β) :
(∏ a in s, ∏ s in (t a), f a s) = ∏ x in s.sigma t, f x.1 x.2 :=
eq.symm $ prod_sigma s t (λ x, f x.1 x.2)
/--
Reorder a product.
The difference with `prod_bij'` is that the bijection is specified as a surjective injection,
rather than by an inverse function.
-/
@[to_additive "
Reorder a sum.
The difference with `sum_bij'` is that the bijection is specified as a surjective injection,
rather than by an inverse function.
"]
lemma prod_bij {s : finset α} {t : finset γ} {f : α → β} {g : γ → β}
(i : Π a ∈ s, γ) (hi : ∀ a ha, i a ha ∈ t) (h : ∀ a ha, f a = g (i a ha))
(i_inj : ∀ a₁ a₂ ha₁ ha₂, i a₁ ha₁ = i a₂ ha₂ → a₁ = a₂) (i_surj : ∀ b ∈ t, ∃ a ha, b = i a ha) :
(∏ x in s, f x) = (∏ x in t, g x) :=
congr_arg multiset.prod
(multiset.map_eq_map_of_bij_of_nodup f g s.2 t.2 i hi h i_inj i_surj)
/--
Reorder a product.
The difference with `prod_bij` is that the bijection is specified with an inverse, rather than
as a surjective injection.
-/
@[to_additive "
Reorder a sum.
The difference with `sum_bij` is that the bijection is specified with an inverse, rather than
as a surjective injection.
"]
lemma prod_bij' {s : finset α} {t : finset γ} {f : α → β} {g : γ → β}
(i : Π a ∈ s, γ) (hi : ∀ a ha, i a ha ∈ t) (h : ∀ a ha, f a = g (i a ha))
(j : Π a ∈ t, α) (hj : ∀ a ha, j a ha ∈ s) (left_inv : ∀ a ha, j (i a ha) (hi a ha) = a)
(right_inv : ∀ a ha, i (j a ha) (hj a ha) = a) :
(∏ x in s, f x) = (∏ x in t, g x) :=
begin
refine prod_bij i hi h _ _,
{intros a1 a2 h1 h2 eq, rw [←left_inv a1 h1, ←left_inv a2 h2], cc,},
{intros b hb, use j b hb, use hj b hb, exact (right_inv b hb).symm,},
end
@[to_additive] lemma prod_finset_product
(r : finset (γ × α)) (s : finset γ) (t : γ → finset α)
(h : ∀ p : γ × α, p ∈ r ↔ p.1 ∈ s ∧ p.2 ∈ t p.1) {f : γ × α → β} :
∏ p in r, f p = ∏ c in s, ∏ a in t c, f (c, a) :=
begin
refine eq.trans _ (prod_sigma s t (λ p, f (p.1, p.2))),
exact prod_bij' (λ p hp, ⟨p.1, p.2⟩) (λ p, mem_sigma.mpr ∘ (h p).mp)
(λ p hp, congr_arg f prod.mk.eta.symm) (λ p hp, (p.1, p.2))
(λ p, (h (p.1, p.2)).mpr ∘ mem_sigma.mp) (λ p hp, prod.mk.eta) (λ p hp, p.eta),
end
@[to_additive] lemma prod_finset_product'
(r : finset (γ × α)) (s : finset γ) (t : γ → finset α)
(h : ∀ p : γ × α, p ∈ r ↔ p.1 ∈ s ∧ p.2 ∈ t p.1) {f : γ → α → β} :
∏ p in r, f p.1 p.2 = ∏ c in s, ∏ a in t c, f c a :=
prod_finset_product r s t h
@[to_additive] lemma prod_finset_product_right
(r : finset (α × γ)) (s : finset γ) (t : γ → finset α)
(h : ∀ p : α × γ, p ∈ r ↔ p.2 ∈ s ∧ p.1 ∈ t p.2) {f : α × γ → β} :
∏ p in r, f p = ∏ c in s, ∏ a in t c, f (a, c) :=
begin
refine eq.trans _ (prod_sigma s t (λ p, f (p.2, p.1))),
exact prod_bij' (λ p hp, ⟨p.2, p.1⟩) (λ p, mem_sigma.mpr ∘ (h p).mp)
(λ p hp, congr_arg f prod.mk.eta.symm) (λ p hp, (p.2, p.1))
(λ p, (h (p.2, p.1)).mpr ∘ mem_sigma.mp) (λ p hp, prod.mk.eta) (λ p hp, p.eta),
end
@[to_additive] lemma prod_finset_product_right'
(r : finset (α × γ)) (s : finset γ) (t : γ → finset α)
(h : ∀ p : α × γ, p ∈ r ↔ p.2 ∈ s ∧ p.1 ∈ t p.2) {f : α → γ → β} :
∏ p in r, f p.1 p.2 = ∏ c in s, ∏ a in t c, f a c :=
prod_finset_product_right r s t h
@[to_additive]
lemma prod_fiberwise_of_maps_to [decidable_eq γ] {s : finset α} {t : finset γ} {g : α → γ}
(h : ∀ x ∈ s, g x ∈ t) (f : α → β) :
(∏ y in t, ∏ x in s.filter (λ x, g x = y), f x) = ∏ x in s, f x :=
begin
letI := classical.dec_eq α,
rw [← bUnion_filter_eq_of_maps_to h] {occs := occurrences.pos [2]},
refine (prod_bUnion $ λ x' hx y' hy hne, _).symm,
rw [function.on_fun, disjoint_filter],
rintros x hx rfl,
exact hne
end
@[to_additive]
lemma prod_image' [decidable_eq α] {s : finset γ} {g : γ → α} (h : γ → β)
(eq : ∀ c ∈ s, f (g c) = ∏ x in s.filter (λ c', g c' = g c), h x) :
(∏ x in s.image g, f x) = ∏ x in s, h x :=
calc (∏ x in s.image g, f x) = ∏ x in s.image g, ∏ x in s.filter (λ c', g c' = x), h x :
prod_congr rfl $ λ x hx, let ⟨c, hcs, hc⟩ := mem_image.1 hx in hc ▸ (eq c hcs)
... = ∏ x in s, h x : prod_fiberwise_of_maps_to (λ x, mem_image_of_mem g) _
@[to_additive]
lemma prod_mul_distrib : ∏ x in s, (f x * g x) = (∏ x in s, f x) * (∏ x in s, g x) :=
eq.trans (by rw one_mul; refl) fold_op_distrib
@[to_additive]
lemma prod_comm {s : finset γ} {t : finset α} {f : γ → α → β} :
(∏ x in s, ∏ y in t, f x y) = (∏ y in t, ∏ x in s, f x y) :=
begin
classical,
apply finset.induction_on s,
{ simp only [prod_empty, prod_const_one] },
{ intros _ _ H ih,
simp only [prod_insert H, prod_mul_distrib, ih] }
end
@[to_additive]
lemma prod_product {s : finset γ} {t : finset α} {f : γ×α → β} :
(∏ x in s.product t, f x) = ∏ x in s, ∏ y in t, f (x, y) :=
prod_finset_product (s.product t) s (λ a, t) (λ p, mem_product)
/-- An uncurried version of `finset.prod_product`. -/
@[to_additive "An uncurried version of `finset.sum_product`"]
lemma prod_product' {s : finset γ} {t : finset α} {f : γ → α → β} :
(∏ x in s.product t, f x.1 x.2) = ∏ x in s, ∏ y in t, f x y :=
prod_product
@[to_additive]
lemma prod_product_right {s : finset γ} {t : finset α} {f : γ×α → β} :
(∏ x in s.product t, f x) = ∏ y in t, ∏ x in s, f (x, y) :=
by rw [prod_product, prod_comm]
/-- An uncurried version of `finset.prod_product_right`. -/
@[to_additive "An uncurried version of `finset.prod_product_right`"]
lemma prod_product_right' {s : finset γ} {t : finset α} {f : γ → α → β} :
(∏ x in s.product t, f x.1 x.2) = ∏ y in t, ∏ x in s, f x y :=
prod_product_right
@[to_additive]
lemma prod_hom_rel [comm_monoid γ] {r : β → γ → Prop} {f : α → β} {g : α → γ} {s : finset α}
(h₁ : r 1 1) (h₂ : ∀ a b c, r b c → r (f a * b) (g a * c)) : r (∏ x in s, f x) (∏ x in s, g x) :=
by { delta finset.prod, apply multiset.prod_hom_rel; assumption }
@[to_additive]
lemma prod_eq_one {f : α → β} {s : finset α} (h : ∀ x ∈ s, f x = 1) : (∏ x in s, f x) = 1 :=
calc (∏ x in s, f x) = ∏ x in s, 1 : finset.prod_congr rfl h
... = 1 : finset.prod_const_one
@[to_additive]
lemma prod_subset_one_on_sdiff [decidable_eq α] (h : s₁ ⊆ s₂) (hg : ∀ x ∈ (s₂ \ s₁), g x = 1)
(hfg : ∀ x ∈ s₁, f x = g x) : ∏ i in s₁, f i = ∏ i in s₂, g i :=
begin
rw [← prod_sdiff h, prod_eq_one hg, one_mul],
exact prod_congr rfl hfg
end
@[to_additive]
lemma prod_subset (h : s₁ ⊆ s₂) (hf : ∀ x ∈ s₂, x ∉ s₁ → f x = 1) :
(∏ x in s₁, f x) = ∏ x in s₂, f x :=
by haveI := classical.dec_eq α; exact prod_subset_one_on_sdiff h (by simpa) (λ _ _, rfl)
@[to_additive]
lemma prod_filter_of_ne {p : α → Prop} [decidable_pred p] (hp : ∀ x ∈ s, f x ≠ 1 → p x) :
(∏ x in (s.filter p), f x) = (∏ x in s, f x) :=
prod_subset (filter_subset _ _) $ λ x,
by { classical, rw [not_imp_comm, mem_filter], exact λ h₁ h₂, ⟨h₁, hp _ h₁ h₂⟩ }
-- If we use `[decidable_eq β]` here, some rewrites fail because they find a wrong `decidable`
-- instance first; `{∀ x, decidable (f x ≠ 1)}` doesn't work with `rw ← prod_filter_ne_one`
@[to_additive]
lemma prod_filter_ne_one [∀ x, decidable (f x ≠ 1)] :
(∏ x in (s.filter $ λ x, f x ≠ 1), f x) = (∏ x in s, f x) :=
prod_filter_of_ne $ λ _ _, id
@[to_additive]
lemma prod_filter (p : α → Prop) [decidable_pred p] (f : α → β) :
(∏ a in s.filter p, f a) = (∏ a in s, if p a then f a else 1) :=
calc (∏ a in s.filter p, f a) = ∏ a in s.filter p, if p a then f a else 1 :
prod_congr rfl (assume a h, by rw [if_pos (mem_filter.1 h).2])
... = ∏ a in s, if p a then f a else 1 :
begin
refine prod_subset (filter_subset _ s) (assume x hs h, _),
rw [mem_filter, not_and] at h,
exact if_neg (h hs)
end
@[to_additive]
lemma prod_eq_single_of_mem {s : finset α} {f : α → β} (a : α) (h : a ∈ s)
(h₀ : ∀ b ∈ s, b ≠ a → f b = 1) : (∏ x in s, f x) = f a :=
begin
haveI := classical.dec_eq α,
calc (∏ x in s, f x) = ∏ x in {a}, f x :
begin
refine (prod_subset _ _).symm,
{ intros _ H, rwa mem_singleton.1 H },
{ simpa only [mem_singleton] }
end
... = f a : prod_singleton
end
@[to_additive]
lemma prod_eq_single {s : finset α} {f : α → β} (a : α)
(h₀ : ∀ b ∈ s, b ≠ a → f b = 1) (h₁ : a ∉ s → f a = 1) : (∏ x in s, f x) = f a :=
by haveI := classical.dec_eq α;
from classical.by_cases
(assume : a ∈ s, prod_eq_single_of_mem a this h₀)
(assume : a ∉ s,
(prod_congr rfl $ λ b hb, h₀ b hb $ by rintro rfl; cc).trans $
prod_const_one.trans (h₁ this).symm)
@[to_additive]
lemma prod_eq_mul_of_mem {s : finset α} {f : α → β} (a b : α) (ha : a ∈ s) (hb : b ∈ s) (hn : a ≠ b)
(h₀ : ∀ c ∈ s, c ≠ a ∧ c ≠ b → f c = 1) : (∏ x in s, f x) = (f a) * (f b) :=
begin
haveI := classical.dec_eq α;
let s' := ({a, b} : finset α),
have hu : s' ⊆ s,
{ refine insert_subset.mpr _, apply and.intro ha, apply singleton_subset_iff.mpr hb },
have hf : ∀ c ∈ s, c ∉ s' → f c = 1,
{ intros c hc hcs,
apply h₀ c hc,
apply not_or_distrib.mp,
intro hab,
apply hcs,
apply mem_insert.mpr,
rw mem_singleton,
exact hab },
rw ←prod_subset hu hf,
exact finset.prod_pair hn
end
@[to_additive]
lemma prod_eq_mul {s : finset α} {f : α → β} (a b : α) (hn : a ≠ b)
(h₀ : ∀ c ∈ s, c ≠ a ∧ c ≠ b → f c = 1) (ha : a ∉ s → f a = 1) (hb : b ∉ s → f b = 1) :
(∏ x in s, f x) = (f a) * (f b) :=
begin
haveI := classical.dec_eq α;
by_cases h₁ : a ∈ s; by_cases h₂ : b ∈ s,
{ exact prod_eq_mul_of_mem a b h₁ h₂ hn h₀ },
{ rw [hb h₂, mul_one],
apply prod_eq_single_of_mem a h₁,
exact λ c hc hca, h₀ c hc ⟨hca, ne_of_mem_of_not_mem hc h₂⟩ },
{ rw [ha h₁, one_mul],
apply prod_eq_single_of_mem b h₂,
exact λ c hc hcb, h₀ c hc ⟨ne_of_mem_of_not_mem hc h₁, hcb⟩ },
{ rw [ha h₁, hb h₂, mul_one],
exact trans
(prod_congr rfl (λ c hc, h₀ c hc ⟨ne_of_mem_of_not_mem hc h₁, ne_of_mem_of_not_mem hc h₂⟩))
prod_const_one }
end
@[to_additive]
lemma prod_attach {f : α → β} : (∏ x in s.attach, f x) = (∏ x in s, f x) :=
by haveI := classical.dec_eq α; exact
calc (∏ x in s.attach, f x.val) = (∏ x in (s.attach).image subtype.val, f x) :
by rw [prod_image]; exact assume x _ y _, subtype.eq
... = _ : by rw [attach_image_val]
/-- A product over `s.subtype p` equals one over `s.filter p`. -/
@[simp, to_additive "A sum over `s.subtype p` equals one over `s.filter p`."]
lemma prod_subtype_eq_prod_filter (f : α → β) {p : α → Prop} [decidable_pred p] :
∏ x in s.subtype p, f x = ∏ x in s.filter p, f x :=
begin
conv_lhs { erw ←prod_map (s.subtype p) (function.embedding.subtype _) f },
exact prod_congr (subtype_map _) (λ x hx, rfl)
end
/-- If all elements of a `finset` satisfy the predicate `p`, a product
over `s.subtype p` equals that product over `s`. -/
@[to_additive "If all elements of a `finset` satisfy the predicate `p`, a sum
over `s.subtype p` equals that sum over `s`."]
lemma prod_subtype_of_mem (f : α → β) {p : α → Prop} [decidable_pred p]
(h : ∀ x ∈ s, p x) : ∏ x in s.subtype p, f x = ∏ x in s, f x :=
by simp_rw [prod_subtype_eq_prod_filter, filter_true_of_mem h]
/-- A product of a function over a `finset` in a subtype equals a
product in the main type of a function that agrees with the first
function on that `finset`. -/
@[to_additive "A sum of a function over a `finset` in a subtype equals a
sum in the main type of a function that agrees with the first
function on that `finset`."]
lemma prod_subtype_map_embedding {p : α → Prop} {s : finset {x // p x}} {f : {x // p x} → β}
{g : α → β} (h : ∀ x : {x // p x}, x ∈ s → g x = f x) :
∏ x in s.map (function.embedding.subtype _), g x = ∏ x in s, f x :=
begin
rw finset.prod_map,
exact finset.prod_congr rfl h
end
variables (f s)
@[to_additive]
lemma prod_coe_sort_eq_attach (f : s → β) :
∏ (i : s), f i = ∏ i in s.attach, f i :=
rfl
@[to_additive]
lemma prod_coe_sort :
∏ (i : s), f i = ∏ i in s, f i :=
prod_attach
@[to_additive]
lemma prod_finset_coe (f : α → β) (s : finset α) :
∏ (i : (s : set α)), f i = ∏ i in s, f i :=
prod_coe_sort s f
variables {f s}
@[to_additive]
lemma prod_subtype {p : α → Prop} {F : fintype (subtype p)} (s : finset α)
(h : ∀ x, x ∈ s ↔ p x) (f : α → β) :
∏ a in s, f a = ∏ a : subtype p, f a :=
have (∈ s) = p, from set.ext h, by { substI p, rw ← prod_coe_sort, congr }
@[to_additive] lemma prod_apply_dite {s : finset α} {p : α → Prop} {hp : decidable_pred p}
[decidable_pred (λ x, ¬ p x)] (f : Π (x : α), p x → γ) (g : Π (x : α), ¬p x → γ)
(h : γ → β) :
(∏ x in s, h (if hx : p x then f x hx else g x hx)) =
(∏ x in (s.filter p).attach, h (f x.1 (mem_filter.mp x.2).2)) *
(∏ x in (s.filter (λ x, ¬ p x)).attach, h (g x.1 (mem_filter.mp x.2).2)) :=
calc ∏ x in s, h (if hx : p x then f x hx else g x hx)
= (∏ x in s.filter p, h (if hx : p x then f x hx else g x hx)) *
(∏ x in s.filter (λ x, ¬ p x), h (if hx : p x then f x hx else g x hx)) :
(prod_filter_mul_prod_filter_not s p _).symm
... = (∏ x in (s.filter p).attach, h (if hx : p x.1 then f x.1 hx else g x.1 hx)) *
(∏ x in (s.filter (λ x, ¬ p x)).attach, h (if hx : p x.1 then f x.1 hx else g x.1 hx)) :
congr_arg2 _ prod_attach.symm prod_attach.symm
... = (∏ x in (s.filter p).attach, h (f x.1 (mem_filter.mp x.2).2)) *
(∏ x in (s.filter (λ x, ¬ p x)).attach, h (g x.1 (mem_filter.mp x.2).2)) :
congr_arg2 _
(prod_congr rfl (λ x hx, congr_arg h (dif_pos (mem_filter.mp x.2).2)))
(prod_congr rfl (λ x hx, congr_arg h (dif_neg (mem_filter.mp x.2).2)))
@[to_additive] lemma prod_apply_ite {s : finset α}
{p : α → Prop} {hp : decidable_pred p} (f g : α → γ) (h : γ → β) :
(∏ x in s, h (if p x then f x else g x)) =
(∏ x in s.filter p, h (f x)) * (∏ x in s.filter (λ x, ¬ p x), h (g x)) :=
trans (prod_apply_dite _ _ _)
(congr_arg2 _ (@prod_attach _ _ _ _ (h ∘ f)) (@prod_attach _ _ _ _ (h ∘ g)))
@[to_additive] lemma prod_dite {s : finset α} {p : α → Prop} {hp : decidable_pred p}
(f : Π (x : α), p x → β) (g : Π (x : α), ¬p x → β) :
(∏ x in s, if hx : p x then f x hx else g x hx) =
(∏ x in (s.filter p).attach, f x.1 (mem_filter.mp x.2).2) *
(∏ x in (s.filter (λ x, ¬ p x)).attach, g x.1 (mem_filter.mp x.2).2) :=
by simp [prod_apply_dite _ _ (λ x, x)]
@[to_additive] lemma prod_ite {s : finset α}
{p : α → Prop} {hp : decidable_pred p} (f g : α → β) :
(∏ x in s, if p x then f x else g x) =
(∏ x in s.filter p, f x) * (∏ x in s.filter (λ x, ¬ p x), g x) :=
by simp [prod_apply_ite _ _ (λ x, x)]
@[to_additive] lemma prod_ite_of_false {p : α → Prop} {hp : decidable_pred p} (f g : α → β)
(h : ∀ x ∈ s, ¬p x) : (∏ x in s, if p x then f x else g x) = (∏ x in s, g x) :=
by { rw prod_ite, simp [filter_false_of_mem h, filter_true_of_mem h] }
@[to_additive] lemma prod_ite_of_true {p : α → Prop} {hp : decidable_pred p} (f g : α → β)
(h : ∀ x ∈ s, p x) : (∏ x in s, if p x then f x else g x) = (∏ x in s, f x) :=
by { simp_rw ←(ite_not (p _)), apply prod_ite_of_false, simpa }
@[to_additive] lemma prod_apply_ite_of_false {p : α → Prop} {hp : decidable_pred p} (f g : α → γ)
(k : γ → β) (h : ∀ x ∈ s, ¬p x) :
(∏ x in s, k (if p x then f x else g x)) = (∏ x in s, k (g x)) :=
by { simp_rw apply_ite k, exact prod_ite_of_false _ _ h }
@[to_additive] lemma prod_apply_ite_of_true {p : α → Prop} {hp : decidable_pred p} (f g : α → γ)
(k : γ → β) (h : ∀ x ∈ s, p x) :
(∏ x in s, k (if p x then f x else g x)) = (∏ x in s, k (f x)) :=
by { simp_rw apply_ite k, exact prod_ite_of_true _ _ h }
@[to_additive]
lemma prod_extend_by_one [decidable_eq α] (s : finset α) (f : α → β) :
∏ i in s, (if i ∈ s then f i else 1) = ∏ i in s, f i :=
prod_congr rfl $ λ i hi, if_pos hi
@[simp, to_additive]
lemma prod_dite_eq [decidable_eq α] (s : finset α) (a : α) (b : Π x : α, a = x → β) :
(∏ x in s, (if h : a = x then b x h else 1)) = ite (a ∈ s) (b a rfl) 1 :=
begin
split_ifs with h,
{ rw [finset.prod_eq_single a, dif_pos rfl],
{ intros, rw dif_neg, cc },
{ cc } },
{ rw finset.prod_eq_one,
intros, rw dif_neg, intro, cc }
end
@[simp, to_additive]
lemma prod_dite_eq' [decidable_eq α] (s : finset α) (a : α) (b : Π x : α, x = a → β) :
(∏ x in s, (if h : x = a then b x h else 1)) = ite (a ∈ s) (b a rfl) 1 :=
begin
split_ifs with h,
{ rw [finset.prod_eq_single a, dif_pos rfl],
{ intros, rw dif_neg, cc },
{ cc } },
{ rw finset.prod_eq_one,
intros, rw dif_neg, intro, cc }
end
@[simp, to_additive] lemma prod_ite_eq [decidable_eq α] (s : finset α) (a : α) (b : α → β) :
(∏ x in s, (ite (a = x) (b x) 1)) = ite (a ∈ s) (b a) 1 :=
prod_dite_eq s a (λ x _, b x)
/-- A product taken over a conditional whose condition is an equality test on the index and whose
alternative is `1` has value either the term at that index or `1`.
The difference with `finset.prod_ite_eq` is that the arguments to `eq` are swapped. -/
@[simp, to_additive "A sum taken over a conditional whose condition is an equality test on the index
and whose alternative is `0` has value either the term at that index or `0`.
The difference with `finset.sum_ite_eq` is that the arguments to `eq` are swapped."]
lemma prod_ite_eq' [decidable_eq α] (s : finset α) (a : α) (b : α → β) :
(∏ x in s, (ite (x = a) (b x) 1)) = ite (a ∈ s) (b a) 1 :=
prod_dite_eq' s a (λ x _, b x)
@[to_additive]
lemma prod_ite_index (p : Prop) [decidable p] (s t : finset α) (f : α → β) :
(∏ x in if p then s else t, f x) = if p then ∏ x in s, f x else ∏ x in t, f x :=
apply_ite (λ s, ∏ x in s, f x) _ _ _
@[simp, to_additive]
lemma prod_dite_irrel (p : Prop) [decidable p] (s : finset α) (f : p → α → β) (g : ¬p → α → β):
(∏ x in s, if h : p then f h x else g h x) = if h : p then ∏ x in s, f h x else ∏ x in s, g h x :=
by { split_ifs with h; refl }
@[simp] lemma sum_pi_single' {ι M : Type*} [decidable_eq ι] [add_comm_monoid M]
(i : ι) (x : M) (s : finset ι) :
∑ j in s, pi.single i x j = if i ∈ s then x else 0 :=
sum_dite_eq' _ _ _
@[simp] lemma sum_pi_single {ι : Type*} {M : ι → Type*}
[decidable_eq ι] [Π i, add_comm_monoid (M i)] (i : ι) (f : Π i, M i) (s : finset ι) :
∑ j in s, pi.single j (f j) i = if i ∈ s then f i else 0 :=
sum_dite_eq _ _ _
@[to_additive]
lemma prod_bij_ne_one {s : finset α} {t : finset γ} {f : α → β} {g : γ → β}
(i : Π a ∈ s, f a ≠ 1 → γ) (hi : ∀ a h₁ h₂, i a h₁ h₂ ∈ t)
(i_inj : ∀ a₁ a₂ h₁₁ h₁₂ h₂₁ h₂₂, i a₁ h₁₁ h₁₂ = i a₂ h₂₁ h₂₂ → a₁ = a₂)
(i_surj : ∀ b ∈ t, g b ≠ 1 → ∃ a h₁ h₂, b = i a h₁ h₂)
(h : ∀ a h₁ h₂, f a = g (i a h₁ h₂)) :
(∏ x in s, f x) = (∏ x in t, g x) :=
by classical; exact
calc (∏ x in s, f x) = ∏ x in (s.filter $ λ x, f x ≠ 1), f x : prod_filter_ne_one.symm
... = ∏ x in (t.filter $ λ x, g x ≠ 1), g x :
prod_bij (assume a ha, i a (mem_filter.mp ha).1 (mem_filter.mp ha).2)
(assume a ha, (mem_filter.mp ha).elim $ λ h₁ h₂, mem_filter.mpr
⟨hi a h₁ h₂, λ hg, h₂ (hg ▸ h a h₁ h₂)⟩)
(assume a ha, (mem_filter.mp ha).elim $ h a)
(assume a₁ a₂ ha₁ ha₂,
(mem_filter.mp ha₁).elim $ λ ha₁₁ ha₁₂,
(mem_filter.mp ha₂).elim $ λ ha₂₁ ha₂₂, i_inj a₁ a₂ _ _ _ _)
(assume b hb, (mem_filter.mp hb).elim $ λ h₁ h₂,
let ⟨a, ha₁, ha₂, eq⟩ := i_surj b h₁ h₂ in ⟨a, mem_filter.mpr ⟨ha₁, ha₂⟩, eq⟩)
... = (∏ x in t, g x) : prod_filter_ne_one
@[to_additive] lemma prod_dite_of_false {p : α → Prop} {hp : decidable_pred p}
(h : ∀ x ∈ s, ¬ p x) (f : Π (x : α), p x → β) (g : Π (x : α), ¬p x → β) :
(∏ x in s, if hx : p x then f x hx else g x hx) =
∏ (x : s), g x.val (h x.val x.property) :=
prod_bij (λ x hx, ⟨x,hx⟩) (λ x hx, by simp) (λ a ha, by { dsimp, rw dif_neg })
(λ a₁ a₂ h₁ h₂ hh, congr_arg coe hh) (λ b hb, ⟨b.1, b.2, by simp⟩)
@[to_additive] lemma prod_dite_of_true {p : α → Prop} {hp : decidable_pred p}
(h : ∀ x ∈ s, p x) (f : Π (x : α), p x → β) (g : Π (x : α), ¬p x → β) :
(∏ x in s, if hx : p x then f x hx else g x hx) =
∏ (x : s), f x.val (h x.val x.property) :=
prod_bij (λ x hx, ⟨x,hx⟩) (λ x hx, by simp) (λ a ha, by { dsimp, rw dif_pos })
(λ a₁ a₂ h₁ h₂ hh, congr_arg coe hh) (λ b hb, ⟨b.1, b.2, by simp⟩)
@[to_additive]
lemma nonempty_of_prod_ne_one (h : (∏ x in s, f x) ≠ 1) : s.nonempty :=
s.eq_empty_or_nonempty.elim (λ H, false.elim $ h $ H.symm ▸ prod_empty) id
@[to_additive]
lemma exists_ne_one_of_prod_ne_one (h : (∏ x in s, f x) ≠ 1) : ∃ a ∈ s, f a ≠ 1 :=
begin
classical,
rw ← prod_filter_ne_one at h,
rcases nonempty_of_prod_ne_one h with ⟨x, hx⟩,
exact ⟨x, (mem_filter.1 hx).1, (mem_filter.1 hx).2⟩
end
@[to_additive]
lemma prod_range_succ_comm (f : ℕ → β) (n : ℕ) :
∏ x in range (n + 1), f x = f n * ∏ x in range n, f x :=
by rw [range_succ, prod_insert not_mem_range_self]
@[to_additive]
lemma prod_range_succ (f : ℕ → β) (n : ℕ) :
∏ x in range (n + 1), f x = (∏ x in range n, f x) * f n :=
by simp only [mul_comm, prod_range_succ_comm]
@[to_additive]
lemma prod_range_succ' (f : ℕ → β) :
∀ n : ℕ, (∏ k in range (n + 1), f k) = (∏ k in range n, f (k+1)) * f 0
| 0 := prod_range_succ _ _
| (n + 1) := by rw [prod_range_succ _ n, mul_right_comm, ← prod_range_succ', prod_range_succ]
@[to_additive]
lemma eventually_constant_prod {u : ℕ → β} {N : ℕ} (hu : ∀ n ≥ N, u n = 1) {n : ℕ} (hn : N ≤ n) :
∏ k in range (n + 1), u k = ∏ k in range (N + 1), u k :=
begin
obtain ⟨m, rfl : n = N + m⟩ := le_iff_exists_add.mp hn,
clear hn,
induction m with m hm,
{ simp },
erw [prod_range_succ, hm],
simp [hu]
end
@[to_additive]
lemma prod_range_add (f : ℕ → β) (n m : ℕ) :
∏ x in range (n + m), f x =
(∏ x in range n, f x) * (∏ x in range m, f (n + x)) :=
begin
induction m with m hm,
{ simp },
{ rw [nat.add_succ, prod_range_succ, hm, prod_range_succ, mul_assoc], },
end
@[to_additive]
lemma prod_range_add_div_prod_range {α : Type*} [comm_group α] (f : ℕ → α) (n m : ℕ) :
(∏ k in range (n + m), f k) / (∏ k in range n, f k) = ∏ k in finset.range m, f (n + k) :=
div_eq_of_eq_mul' (prod_range_add f n m)
@[to_additive]
lemma prod_range_zero (f : ℕ → β) :
∏ k in range 0, f k = 1 :=
by rw [range_zero, prod_empty]
@[to_additive sum_range_one]
lemma prod_range_one (f : ℕ → β) :
∏ k in range 1, f k = f 0 :=
by { rw [range_one], apply @prod_singleton β ℕ 0 f }
open list
@[to_additive] lemma prod_list_map_count [decidable_eq α] (l : list α)
{M : Type*} [comm_monoid M] (f : α → M) :
(l.map f).prod = ∏ m in l.to_finset, (f m) ^ (l.count m) :=
begin
induction l with a s IH, { simp only [map_nil, prod_nil, count_nil, pow_zero, prod_const_one] },
simp only [list.map, list.prod_cons, to_finset_cons, IH],
by_cases has : a ∈ s.to_finset,
{ rw [insert_eq_of_mem has, ← insert_erase has, prod_insert (not_mem_erase _ _),
prod_insert (not_mem_erase _ _), ← mul_assoc, count_cons_self, pow_succ],
congr' 1,
refine prod_congr rfl (λ x hx, _),
rw [count_cons_of_ne (ne_of_mem_erase hx)] },
rw [prod_insert has, count_cons_self, count_eq_zero_of_not_mem (mt mem_to_finset.2 has), pow_one],
congr' 1,
refine prod_congr rfl (λ x hx, _),
rw count_cons_of_ne,
rintro rfl,
exact has hx,
end
@[to_additive]
lemma prod_list_count [decidable_eq α] [comm_monoid α] (s : list α) :
s.prod = ∏ m in s.to_finset, m ^ (s.count m) :=
by simpa using prod_list_map_count s id
@[to_additive]
lemma prod_list_count_of_subset [decidable_eq α] [comm_monoid α]
(m : list α) (s : finset α) (hs : m.to_finset ⊆ s) :
m.prod = ∏ i in s, i ^ (m.count i) :=
begin
rw prod_list_count,
refine prod_subset hs (λ x _ hx, _),
rw [mem_to_finset] at hx,
rw [count_eq_zero_of_not_mem hx, pow_zero],
end
open multiset
@[to_additive] lemma prod_multiset_map_count [decidable_eq α] (s : multiset α)
{M : Type*} [comm_monoid M] (f : α → M) :
(s.map f).prod = ∏ m in s.to_finset, (f m) ^ (s.count m) :=
by { refine quot.induction_on s (λ l, _), simpa [prod_list_map_count l f] }
@[to_additive]
lemma prod_multiset_count [decidable_eq α] [comm_monoid α] (s : multiset α) :
s.prod = ∏ m in s.to_finset, m ^ (s.count m) :=
by { convert prod_multiset_map_count s id, rw multiset.map_id }
@[to_additive]
lemma prod_multiset_count_of_subset [decidable_eq α] [comm_monoid α]
(m : multiset α) (s : finset α) (hs : m.to_finset ⊆ s) :
m.prod = ∏ i in s, i ^ (m.count i) :=
begin
revert hs,
refine quot.induction_on m (λ l, _),
simp only [quot_mk_to_coe'', coe_prod, coe_count],
apply prod_list_count_of_subset l s
end
@[to_additive] lemma prod_mem_multiset [decidable_eq α]
(m : multiset α) (f : {x // x ∈ m} → β) (g : α → β)
(hfg : ∀ x, f x = g x) :
∏ (x : {x // x ∈ m}), f x = ∏ x in m.to_finset, g x :=
prod_bij (λ x _, x.1) (λ x _, multiset.mem_to_finset.mpr x.2)
(λ _ _, hfg _)
(λ _ _ _ _ h, by { ext, assumption })
(λ y hy, ⟨⟨y, multiset.mem_to_finset.mp hy⟩, finset.mem_univ _, rfl⟩)
/--
To prove a property of a product, it suffices to prove that
the property is multiplicative and holds on factors.
-/
@[to_additive "To prove a property of a sum, it suffices to prove that
the property is additive and holds on summands."]
lemma prod_induction {M : Type*} [comm_monoid M] (f : α → M) (p : M → Prop)
(p_mul : ∀ a b, p a → p b → p (a * b)) (p_one : p 1) (p_s : ∀ x ∈ s, p $ f x) :
p $ ∏ x in s, f x :=
multiset.prod_induction _ _ p_mul p_one (multiset.forall_mem_map_iff.mpr p_s)
/--
To prove a property of a product, it suffices to prove that
the property is multiplicative and holds on factors.
-/
@[to_additive "To prove a property of a sum, it suffices to prove that
the property is additive and holds on summands."]
lemma prod_induction_nonempty {M : Type*} [comm_monoid M] (f : α → M) (p : M → Prop)
(p_mul : ∀ a b, p a → p b → p (a * b)) (hs_nonempty : s.nonempty) (p_s : ∀ x ∈ s, p $ f x) :
p $ ∏ x in s, f x :=
multiset.prod_induction_nonempty p p_mul (by simp [nonempty_iff_ne_empty.mp hs_nonempty])
(multiset.forall_mem_map_iff.mpr p_s)
/--
For any product along `{0, ..., n-1}` of a commutative-monoid-valued function, we can verify that
it's equal to a different function just by checking ratios of adjacent terms.
This is a multiplicative discrete analogue of the fundamental theorem of calculus. -/
lemma prod_range_induction {M : Type*} [comm_monoid M]
(f s : ℕ → M) (h0 : s 0 = 1) (h : ∀ n, s (n + 1) = s n * f n) (n : ℕ) :
∏ k in finset.range n, f k = s n :=
begin
induction n with k hk,
{ simp only [h0, finset.prod_range_zero] },
{ simp only [hk, finset.prod_range_succ, h, mul_comm] }
end
/--
For any sum along `{0, ..., n-1}` of a commutative-monoid-valued function,
we can verify that it's equal to a different function
just by checking differences of adjacent terms.
This is a discrete analogue
of the fundamental theorem of calculus.
-/
lemma sum_range_induction {M : Type*} [add_comm_monoid M]
(f s : ℕ → M) (h0 : s 0 = 0) (h : ∀ n, s (n + 1) = s n + f n) (n : ℕ) :
∑ k in finset.range n, f k = s n :=
@prod_range_induction (multiplicative M) _ f s h0 h n
/-- A telescoping sum along `{0, ..., n - 1}` of an additive commutative group valued function
reduces to the difference of the last and first terms.-/
lemma sum_range_sub {G : Type*} [add_comm_group G] (f : ℕ → G) (n : ℕ) :
∑ i in range n, (f (i+1) - f i) = f n - f 0 :=
by { apply sum_range_induction; simp }
lemma sum_range_sub' {G : Type*} [add_comm_group G] (f : ℕ → G) (n : ℕ) :
∑ i in range n, (f i - f (i+1)) = f 0 - f n :=
by { apply sum_range_induction; simp }
/-- A telescoping product along `{0, ..., n - 1}` of a commutative group valued function reduces to
the ratio of the last and first factors. -/
@[to_additive]
lemma prod_range_div {M : Type*} [comm_group M] (f : ℕ → M) (n : ℕ) :
∏ i in range n, (f (i+1) * (f i)⁻¹) = f n * (f 0)⁻¹ :=
by simpa only [← div_eq_mul_inv] using @sum_range_sub (additive M) _ f n
@[to_additive]
lemma prod_range_div' {M : Type*} [comm_group M] (f : ℕ → M) (n : ℕ) :
∏ i in range n, (f i * (f (i+1))⁻¹) = f 0 * (f n)⁻¹ :=
by simpa only [← div_eq_mul_inv] using @sum_range_sub' (additive M) _ f n
/--
A telescoping sum along `{0, ..., n-1}` of an `ℕ`-valued function
reduces to the difference of the last and first terms
when the function we are summing is monotone.
-/
lemma sum_range_sub_of_monotone {f : ℕ → ℕ} (h : monotone f) (n : ℕ) :
∑ i in range n, (f (i+1) - f i) = f n - f 0 :=
begin
refine sum_range_induction _ _ (tsub_self _) (λ n, _) _,
have h₁ : f n ≤ f (n+1) := h (nat.le_succ _),
have h₂ : f 0 ≤ f n := h (nat.zero_le _),
rw [tsub_add_eq_add_tsub h₂, add_tsub_cancel_of_le h₁],
end
@[simp, to_additive] lemma prod_const (b : β) : (∏ x in s, b) = b ^ s.card :=
by haveI := classical.dec_eq α; exact
finset.induction_on s (by simp) (λ a s has ih,
by rw [prod_insert has, card_insert_of_not_mem has, pow_succ, ih])
@[to_additive]
lemma pow_eq_prod_const (b : β) : ∀ n, b ^ n = ∏ k in range n, b := by simp
@[to_additive]
lemma prod_pow (s : finset α) (n : ℕ) (f : α → β) :
∏ x in s, f x ^ n = (∏ x in s, f x) ^ n :=
by haveI := classical.dec_eq α; exact
finset.induction_on s (by simp) (by simp [mul_pow] {contextual := tt})
@[to_additive]
lemma prod_flip {n : ℕ} (f : ℕ → β) :
∏ r in range (n + 1), f (n - r) = ∏ k in range (n + 1), f k :=
begin
induction n with n ih,
{ rw [prod_range_one, prod_range_one] },
{ rw [prod_range_succ', prod_range_succ _ (nat.succ n)],
simp [← ih] }
end
@[to_additive]
lemma prod_involution {s : finset α} {f : α → β} :
∀ (g : Π a ∈ s, α)
(h : ∀ a ha, f a * f (g a ha) = 1)
(g_ne : ∀ a ha, f a ≠ 1 → g a ha ≠ a)
(g_mem : ∀ a ha, g a ha ∈ s)
(g_inv : ∀ a ha, g (g a ha) (g_mem a ha) = a),
(∏ x in s, f x) = 1 :=
by haveI := classical.dec_eq α;
haveI := classical.dec_eq β; exact
finset.strong_induction_on s
(λ s ih g h g_ne g_mem g_inv,
s.eq_empty_or_nonempty.elim (λ hs, hs.symm ▸ rfl)
(λ ⟨x, hx⟩,
have hmem : ∀ y ∈ (s.erase x).erase (g x hx), y ∈ s,
from λ y hy, (mem_of_mem_erase (mem_of_mem_erase hy)),
have g_inj : ∀ {x hx y hy}, g x hx = g y hy → x = y,
from λ x hx y hy h, by rw [← g_inv x hx, ← g_inv y hy]; simp [h],
have ih': ∏ y in erase (erase s x) (g x hx), f y = (1 : β) :=
ih ((s.erase x).erase (g x hx))
⟨subset.trans (erase_subset _ _) (erase_subset _ _),
λ h, not_mem_erase (g x hx) (s.erase x) (h (g_mem x hx))⟩
(λ y hy, g y (hmem y hy))
(λ y hy, h y (hmem y hy))
(λ y hy, g_ne y (hmem y hy))
(λ y hy, mem_erase.2 ⟨λ (h : g y _ = g x hx), by simpa [g_inj h] using hy,
mem_erase.2 ⟨λ (h : g y _ = x),
have y = g x hx, from g_inv y (hmem y hy) ▸ by simp [h],
by simpa [this] using hy, g_mem y (hmem y hy)⟩⟩)
(λ y hy, g_inv y (hmem y hy)),
if hx1 : f x = 1
then ih' ▸ eq.symm (prod_subset hmem
(λ y hy hy₁,
have y = x ∨ y = g x hx, by simp [hy] at hy₁; tauto,
this.elim (λ hy, hy.symm ▸ hx1)
(λ hy, h x hx ▸ hy ▸ hx1.symm ▸ (one_mul _).symm)))
else by rw [← insert_erase hx, prod_insert (not_mem_erase _ _),
← insert_erase (mem_erase.2 ⟨g_ne x hx hx1, g_mem x hx⟩),
prod_insert (not_mem_erase _ _), ih', mul_one, h x hx]))
/-- The product of the composition of functions `f` and `g`, is the product over `b ∈ s.image g` of
`f b` to the power of the cardinality of the fibre of `b`. See also `finset.prod_image`. -/
@[to_additive "The sum of the composition of functions `f` and `g`, is the sum over `b ∈ s.image g`
of `f b` times of the cardinality of the fibre of `b`. See also `finset.sum_image`."]
lemma prod_comp [decidable_eq γ] (f : γ → β) (g : α → γ) :
∏ a in s, f (g a) = ∏ b in s.image g, f b ^ (s.filter (λ a, g a = b)).card :=
calc ∏ a in s, f (g a)
= ∏ x in (s.image g).sigma (λ b : γ, s.filter (λ a, g a = b)), f (g x.2) :
prod_bij (λ a ha, ⟨g a, a⟩) (by simp; tauto) (λ _ _, rfl) (by simp) -- `(by finish)` closes this
(by { rintro ⟨b_fst, b_snd⟩ H,
simp only [mem_image, exists_prop, mem_filter, mem_sigma] at H,
tauto })
... = ∏ b in s.image g, ∏ a in s.filter (λ a, g a = b), f (g a) : prod_sigma _ _ _
... = ∏ b in s.image g, ∏ a in s.filter (λ a, g a = b), f b :
prod_congr rfl (λ b hb, prod_congr rfl (by simp {contextual := tt}))
... = ∏ b in s.image g, f b ^ (s.filter (λ a, g a = b)).card :
prod_congr rfl (λ _ _, prod_const _)
@[to_additive]
lemma prod_piecewise [decidable_eq α] (s t : finset α) (f g : α → β) :
(∏ x in s, (t.piecewise f g) x) = (∏ x in s ∩ t, f x) * (∏ x in s \ t, g x) :=
by { rw [piecewise, prod_ite, filter_mem_eq_inter, ← sdiff_eq_filter], }
@[to_additive]
lemma prod_inter_mul_prod_diff [decidable_eq α] (s t : finset α) (f : α → β) :
(∏ x in s ∩ t, f x) * (∏ x in s \ t, f x) = (∏ x in s, f x) :=
by { convert (s.prod_piecewise t f f).symm, simp [finset.piecewise] }
@[to_additive]
lemma prod_eq_mul_prod_diff_singleton [decidable_eq α] {s : finset α} {i : α} (h : i ∈ s)
(f : α → β) : ∏ x in s, f x = f i * ∏ x in s \ {i}, f x :=
by { convert (s.prod_inter_mul_prod_diff {i} f).symm, simp [h] }
@[to_additive]
lemma prod_eq_prod_diff_singleton_mul [decidable_eq α] {s : finset α} {i : α} (h : i ∈ s)
(f : α → β) : ∏ x in s, f x = (∏ x in s \ {i}, f x) * f i :=
by { rw [prod_eq_mul_prod_diff_singleton h, mul_comm] }
@[to_additive]
lemma _root_.fintype.prod_eq_mul_prod_compl [decidable_eq α] [fintype α] (a : α) (f : α → β) :
∏ i, f i = (f a) * ∏ i in {a}ᶜ, f i :=
prod_eq_mul_prod_diff_singleton (mem_univ a) f
@[to_additive]
lemma _root_.fintype.prod_eq_prod_compl_mul [decidable_eq α] [fintype α] (a : α) (f : α → β) :
∏ i, f i = (∏ i in {a}ᶜ, f i) * f a :=
prod_eq_prod_diff_singleton_mul (mem_univ a) f
lemma dvd_prod_of_mem (f : α → β) {a : α} {s : finset α} (ha : a ∈ s) :
f a ∣ ∏ i in s, f i :=
begin
classical,
rw finset.prod_eq_mul_prod_diff_singleton ha,
exact dvd_mul_right _ _,
end
/-- A product can be partitioned into a product of products, each equivalent under a setoid. -/
@[to_additive "A sum can be partitioned into a sum of sums, each equivalent under a setoid."]
lemma prod_partition (R : setoid α) [decidable_rel R.r] :
(∏ x in s, f x) = ∏ xbar in s.image quotient.mk, ∏ y in s.filter (λ y, ⟦y⟧ = xbar), f y :=
begin
refine (finset.prod_image' f (λ x hx, _)).symm,
refl,
end
/-- If we can partition a product into subsets that cancel out, then the whole product cancels. -/
@[to_additive "If we can partition a sum into subsets that cancel out, then the whole sum cancels."]
lemma prod_cancels_of_partition_cancels (R : setoid α) [decidable_rel R.r]
(h : ∀ x ∈ s, (∏ a in s.filter (λ y, y ≈ x), f a) = 1) : (∏ x in s, f x) = 1 :=
begin
rw [prod_partition R, ←finset.prod_eq_one],
intros xbar xbar_in_s,
obtain ⟨x, x_in_s, xbar_eq_x⟩ := mem_image.mp xbar_in_s,
rw [←xbar_eq_x, filter_congr (λ y _, @quotient.eq _ R y x)],
apply h x x_in_s,
end
@[to_additive]
lemma prod_update_of_not_mem [decidable_eq α] {s : finset α} {i : α}
(h : i ∉ s) (f : α → β) (b : β) : (∏ x in s, function.update f i b x) = (∏ x in s, f x) :=
begin
apply prod_congr rfl (λ j hj, _),
have : j ≠ i, by { assume eq, rw eq at hj, exact h hj },
simp [this]
end
@[to_additive]
lemma prod_update_of_mem [decidable_eq α] {s : finset α} {i : α} (h : i ∈ s) (f : α → β) (b : β) :
(∏ x in s, function.update f i b x) = b * (∏ x in s \ (singleton i), f x) :=
by { rw [update_eq_piecewise, prod_piecewise], simp [h] }
/-- If a product of a `finset` of size at most 1 has a given value, so
do the terms in that product. -/
@[to_additive eq_of_card_le_one_of_sum_eq "If a sum of a `finset` of size at most 1 has a given
value, so do the terms in that sum."]
lemma eq_of_card_le_one_of_prod_eq {s : finset α} (hc : s.card ≤ 1) {f : α → β} {b : β}
(h : ∏ x in s, f x = b) : ∀ x ∈ s, f x = b :=
begin
intros x hx,
by_cases hc0 : s.card = 0,
{ exact false.elim (card_ne_zero_of_mem hx hc0) },
{ have h1 : s.card = 1 := le_antisymm hc (nat.one_le_of_lt (nat.pos_of_ne_zero hc0)),
rw card_eq_one at h1,
cases h1 with x2 hx2,
rw [hx2, mem_singleton] at hx,
simp_rw hx2 at h,
rw hx,
rw prod_singleton at h,
exact h }
end
/-- Taking a product over `s : finset α` is the same as multiplying the value on a single element
`f a` by the product of `s.erase a`. -/
@[to_additive "Taking a sum over `s : finset α` is the same as adding the value on a single element
`f a` to the sum over `s.erase a`."]
lemma mul_prod_erase [decidable_eq α] (s : finset α) (f : α → β) {a : α} (h : a ∈ s) :
f a * (∏ x in s.erase a, f x) = ∏ x in s, f x :=
by rw [← prod_insert (not_mem_erase a s), insert_erase h]
/-- A variant of `finset.mul_prod_erase` with the multiplication swapped. -/
@[to_additive "A variant of `finset.add_sum_erase` with the addition swapped."]
lemma prod_erase_mul [decidable_eq α] (s : finset α) (f : α → β) {a : α} (h : a ∈ s) :
(∏ x in s.erase a, f x) * f a = ∏ x in s, f x :=
by rw [mul_comm, mul_prod_erase s f h]
/-- If a function applied at a point is 1, a product is unchanged by
removing that point, if present, from a `finset`. -/
@[to_additive "If a function applied at a point is 0, a sum is unchanged by
removing that point, if present, from a `finset`."]
lemma prod_erase [decidable_eq α] (s : finset α) {f : α → β} {a : α} (h : f a = 1) :
∏ x in s.erase a, f x = ∏ x in s, f x :=
begin
rw ←sdiff_singleton_eq_erase,
refine prod_subset (sdiff_subset _ _) (λ x hx hnx, _),
rw sdiff_singleton_eq_erase at hnx,
rwa eq_of_mem_of_not_mem_erase hx hnx
end
/-- If a product is 1 and the function is 1 except possibly at one
point, it is 1 everywhere on the `finset`. -/
@[to_additive "If a sum is 0 and the function is 0 except possibly at one
point, it is 0 everywhere on the `finset`."]
lemma eq_one_of_prod_eq_one {s : finset α} {f : α → β} {a : α} (hp : ∏ x in s, f x = 1)
(h1 : ∀ x ∈ s, x ≠ a → f x = 1) : ∀ x ∈ s, f x = 1 :=
begin
intros x hx,
classical,
by_cases h : x = a,
{ rw h,
rw h at hx,
rw [←prod_subset (singleton_subset_iff.2 hx)
(λ t ht ha, h1 t ht (not_mem_singleton.1 ha)),
prod_singleton] at hp,
exact hp },
{ exact h1 x hx h }
end
lemma prod_pow_boole [decidable_eq α] (s : finset α) (f : α → β) (a : α) :
(∏ x in s, (f x)^(ite (a = x) 1 0)) = ite (a ∈ s) (f a) 1 :=
by simp
lemma prod_dvd_prod_of_dvd {S : finset α} (g1 g2 : α → β) (h : ∀ a ∈ S, g1 a ∣ g2 a) :
S.prod g1 ∣ S.prod g2 :=
begin
classical,
apply finset.induction_on' S, { simp },
intros a T haS _ haT IH,
repeat {rw finset.prod_insert haT},
exact mul_dvd_mul (h a haS) IH,
end
lemma prod_dvd_prod_of_subset {ι M : Type*} [comm_monoid M] (s t : finset ι) (f : ι → M)
(h : s ⊆ t) : ∏ i in s, f i ∣ ∏ i in t, f i :=
multiset.prod_dvd_prod_of_le $ multiset.map_le_map $ by simpa
end comm_monoid
/-- If `f = g = h` everywhere but at `i`, where `f i = g i + h i`, then the product of `f` over `s`
is the sum of the products of `g` and `h`. -/
lemma prod_add_prod_eq [comm_semiring β] {s : finset α} {i : α} {f g h : α → β}
(hi : i ∈ s) (h1 : g i + h i = f i) (h2 : ∀ j ∈ s, j ≠ i → g j = f j)
(h3 : ∀ j ∈ s, j ≠ i → h j = f j) : ∏ i in s, g i + ∏ i in s, h i = ∏ i in s, f i :=
by { classical, simp_rw [prod_eq_mul_prod_diff_singleton hi, ← h1, right_distrib],
congr' 2; apply prod_congr rfl; simpa }
lemma card_eq_sum_ones (s : finset α) : s.card = ∑ _ in s, 1 :=
by simp
lemma sum_const_nat {m : ℕ} {f : α → ℕ} (h₁ : ∀ x ∈ s, f x = m) :
(∑ x in s, f x) = card s * m :=
begin
rw [← nat.nsmul_eq_mul, ← sum_const],
apply sum_congr rfl h₁
end
@[simp]
lemma sum_boole {s : finset α} {p : α → Prop} [non_assoc_semiring β] {hp : decidable_pred p} :
(∑ x in s, if p x then (1 : β) else (0 : β)) = (s.filter p).card :=
by simp [sum_ite]
lemma eq_sum_range_sub [add_comm_group β] (f : ℕ → β) (n : ℕ) :
f n = f 0 + ∑ i in range n, (f (i+1) - f i) :=
by rw [finset.sum_range_sub, add_sub_cancel'_right]
lemma eq_sum_range_sub' [add_comm_group β] (f : ℕ → β) (n : ℕ) :
f n = ∑ i in range (n + 1), if i = 0 then f 0 else f i - f (i - 1) :=
begin
conv_lhs { rw [finset.eq_sum_range_sub f] },
simp [finset.sum_range_succ', add_comm]
end
lemma _root_.commute.sum_right [non_unital_non_assoc_semiring β] (s : finset α)
(f : α → β) (b : β) (h : ∀ i ∈ s, commute b (f i)) :
commute b (∑ i in s, f i) :=
commute.multiset_sum_right _ _ $ λ b hb, begin
obtain ⟨i, hi, rfl⟩ := multiset.mem_map.mp hb,
exact h _ hi
end
lemma _root_.commute.sum_left [non_unital_non_assoc_semiring β] (s : finset α)
(f : α → β) (b : β) (h : ∀ i ∈ s, commute (f i) b) :
commute (∑ i in s, f i) b :=
(commute.sum_right _ _ _ $ λ i hi, (h _ hi).symm).symm
section opposite
open mul_opposite
/-- Moving to the opposite additive commutative monoid commutes with summing. -/
@[simp] lemma op_sum [add_comm_monoid β] {s : finset α} (f : α → β) :
op (∑ x in s, f x) = ∑ x in s, op (f x) :=
(op_add_equiv : β ≃+ βᵐᵒᵖ).map_sum _ _
@[simp] lemma unop_sum [add_comm_monoid β] {s : finset α} (f : α → βᵐᵒᵖ) :
unop (∑ x in s, f x) = ∑ x in s, unop (f x) :=
(op_add_equiv : β ≃+ βᵐᵒᵖ).symm.map_sum _ _
end opposite
section comm_group
variables [comm_group β]
@[simp, to_additive]
lemma prod_inv_distrib : (∏ x in s, (f x)⁻¹) = (∏ x in s, f x)⁻¹ := multiset.prod_map_inv'
@[to_additive zsmul_sum]
lemma prod_zpow (f : α → β) (s : finset α) (n : ℤ) :
(∏ a in s, f a) ^ n = ∏ a in s, (f a) ^ n :=
multiset.prod_map_zpow.symm
@[to_additive]
lemma prod_sdiff_div_prod_sdiff [decidable_eq α] :
(∏ (x : α) in s₂ \ s₁, f x) / (∏ (x : α) in s₁ \ s₂, f x)
= (∏ (x : α) in s₂, f x) / (∏ (x : α) in s₁, f x) :=
by simp [← finset.prod_sdiff (@inf_le_left _ _ s₁ s₂),
← finset.prod_sdiff (@inf_le_right _ _ s₁ s₂)]
end comm_group
@[simp] theorem card_sigma {σ : α → Type*} (s : finset α) (t : Π a, finset (σ a)) :
card (s.sigma t) = ∑ a in s, card (t a) :=
multiset.card_sigma _ _
lemma card_bUnion [decidable_eq β] {s : finset α} {t : α → finset β}
(h : ∀ x ∈ s, ∀ y ∈ s, x ≠ y → disjoint (t x) (t y)) :
(s.bUnion t).card = ∑ u in s, card (t u) :=
calc (s.bUnion t).card = ∑ i in s.bUnion t, 1 : by simp
... = ∑ a in s, ∑ i in t a, 1 : finset.sum_bUnion h
... = ∑ u in s, card (t u) : by simp
lemma card_bUnion_le [decidable_eq β] {s : finset α} {t : α → finset β} :
(s.bUnion t).card ≤ ∑ a in s, (t a).card :=
by haveI := classical.dec_eq α; exact
finset.induction_on s (by simp)
(λ a s has ih,
calc ((insert a s).bUnion t).card ≤ (t a).card + (s.bUnion t).card :
by rw bUnion_insert; exact finset.card_union_le _ _
... ≤ ∑ a in insert a s, card (t a) :
by rw sum_insert has; exact add_le_add_left ih _)
theorem card_eq_sum_card_fiberwise [decidable_eq β] {f : α → β} {s : finset α} {t : finset β}
(H : ∀ x ∈ s, f x ∈ t) :
s.card = ∑ a in t, (s.filter (λ x, f x = a)).card :=
by simp only [card_eq_sum_ones, sum_fiberwise_of_maps_to H]
theorem card_eq_sum_card_image [decidable_eq β] (f : α → β) (s : finset α) :
s.card = ∑ a in s.image f, (s.filter (λ x, f x = a)).card :=
card_eq_sum_card_fiberwise (λ _, mem_image_of_mem _)
@[simp] lemma sum_sub_distrib [add_comm_group β] :
∑ x in s, (f x - g x) = (∑ x in s, f x) - (∑ x in s, g x) :=
by simpa only [sub_eq_add_neg] using sum_add_distrib.trans (congr_arg _ sum_neg_distrib)
lemma mem_sum {f : α → multiset β} (s : finset α) (b : β) :
b ∈ ∑ x in s, f x ↔ ∃ a ∈ s, b ∈ f a :=
begin
classical,
refine s.induction_on (by simp) _,
{ intros a t hi ih,
simp [sum_insert hi, ih, or_and_distrib_right, exists_or_distrib] }
end
section prod_eq_zero
variables [comm_monoid_with_zero β]
lemma prod_eq_zero (ha : a ∈ s) (h : f a = 0) : (∏ x in s, f x) = 0 :=
by { haveI := classical.dec_eq α, rw [←prod_erase_mul _ _ ha, h, mul_zero] }
lemma prod_boole {s : finset α} {p : α → Prop} [decidable_pred p] :
∏ i in s, ite (p i) (1 : β) (0 : β) = ite (∀ i ∈ s, p i) 1 0 :=
begin
split_ifs,
{ apply prod_eq_one,
intros i hi,
rw if_pos (h i hi) },
{ push_neg at h,
rcases h with ⟨i, hi, hq⟩,
apply prod_eq_zero hi,
rw [if_neg hq] },
end
variables [nontrivial β] [no_zero_divisors β]
lemma prod_eq_zero_iff : (∏ x in s, f x) = 0 ↔ (∃ a ∈ s, f a = 0) :=
begin
classical,
apply finset.induction_on s,
exact ⟨not.elim one_ne_zero, λ ⟨_, H, _⟩, H.elim⟩,
assume a s ha ih,
rw [prod_insert ha, mul_eq_zero, bex_def, exists_mem_insert, ih, ← bex_def]
end
theorem prod_ne_zero_iff : (∏ x in s, f x) ≠ 0 ↔ (∀ a ∈ s, f a ≠ 0) :=
by { rw [ne, prod_eq_zero_iff], push_neg }
end prod_eq_zero
section comm_group_with_zero
variables [comm_group_with_zero β]
@[simp]
lemma prod_inv_distrib' : (∏ x in s, (f x)⁻¹) = (∏ x in s, f x)⁻¹ :=
begin
classical,
by_cases h : ∃ x ∈ s, f x = 0,
{ simpa [prod_eq_zero_iff.mpr h, prod_eq_zero_iff] using h },
{ push_neg at h,
have h' := prod_ne_zero_iff.mpr h,
have hf : ∀ x ∈ s, (f x)⁻¹ * f x = 1 := λ x hx, inv_mul_cancel (h x hx),
apply mul_right_cancel₀ h',
simp [h, h', ← finset.prod_mul_distrib, prod_congr rfl hf] }
end
end comm_group_with_zero
@[to_additive]
lemma prod_unique_nonempty {α β : Type*} [comm_monoid β] [unique α]
(s : finset α) (f : α → β) (h : s.nonempty) :
(∏ x in s, f x) = f default :=
begin
obtain ⟨a, ha⟩ := h,
have : s = {a},
{ ext b,
simpa [subsingleton.elim a b] using ha },
rw [this, finset.prod_singleton, subsingleton.elim a default]
end
end finset
namespace fintype
open finset
/-- `fintype.prod_bijective` is a variant of `finset.prod_bij` that accepts `function.bijective`.
See `function.bijective.prod_comp` for a version without `h`. -/
@[to_additive "`fintype.sum_equiv` is a variant of `finset.sum_bij` that accepts
`function.bijective`.
See `function.bijective.sum_comp` for a version without `h`. "]
lemma prod_bijective {α β M : Type*} [fintype α] [fintype β] [comm_monoid M]
(e : α → β) (he : function.bijective e) (f : α → M) (g : β → M) (h : ∀ x, f x = g (e x)) :
∏ x : α, f x = ∏ x : β, g x :=
prod_bij
(λ x _, e x)
(λ x _, mem_univ (e x))
(λ x _, h x)
(λ x x' _ _ h, he.injective h)
(λ y _, (he.surjective y).imp $ λ a h, ⟨mem_univ _, h.symm⟩)
/-- `fintype.prod_equiv` is a specialization of `finset.prod_bij` that
automatically fills in most arguments.
See `equiv.prod_comp` for a version without `h`.
-/
@[to_additive "`fintype.sum_equiv` is a specialization of `finset.sum_bij` that
automatically fills in most arguments.
See `equiv.sum_comp` for a version without `h`.
"]
lemma prod_equiv {α β M : Type*} [fintype α] [fintype β] [comm_monoid M]
(e : α ≃ β) (f : α → M) (g : β → M) (h : ∀ x, f x = g (e x)) :
∏ x : α, f x = ∏ x : β, g x :=
prod_bijective e e.bijective f g h
variables {f s}
@[to_additive]
lemma prod_unique {α β : Type*} [comm_monoid β] [unique α] (f : α → β) :
(∏ x : α, f x) = f default :=
by rw [univ_unique, prod_singleton]
@[to_additive] lemma prod_empty {α β : Type*} [comm_monoid β] [is_empty α] (f : α → β) :
(∏ x : α, f x) = 1 :=
by rw [eq_empty_of_is_empty (univ : finset α), finset.prod_empty]
@[to_additive]
lemma prod_subsingleton {α β : Type*} [comm_monoid β] [subsingleton α] (f : α → β) (a : α) :
(∏ x : α, f x) = f a :=
begin
haveI : unique α := unique_of_subsingleton a,
convert prod_unique f
end
@[to_additive]
lemma prod_subtype_mul_prod_subtype {α β : Type*} [fintype α] [comm_monoid β]
(p : α → Prop) (f : α → β) [decidable_pred p] :
(∏ (i : {x // p x}), f i) * (∏ i : {x // ¬ p x}, f i) = ∏ i, f i :=
begin
classical,
let s := {x | p x}.to_finset,
rw [← finset.prod_subtype s, ← finset.prod_subtype sᶜ],
{ exact finset.prod_mul_prod_compl _ _ },
{ simp },
{ simp }
end
end fintype
namespace list
@[to_additive] lemma prod_to_finset {M : Type*} [decidable_eq α] [comm_monoid M]
(f : α → M) : ∀ {l : list α} (hl : l.nodup), l.to_finset.prod f = (l.map f).prod
| [] _ := by simp
| (a :: l) hl := let ⟨not_mem, hl⟩ := list.nodup_cons.mp hl in
by simp [finset.prod_insert (mt list.mem_to_finset.mp not_mem), prod_to_finset hl]
end list
namespace multiset
variables [decidable_eq α]
@[simp] lemma to_finset_sum_count_eq (s : multiset α) :
(∑ a in s.to_finset, s.count a) = s.card :=
multiset.induction_on s rfl
(assume a s ih,
calc (∑ x in to_finset (a ::ₘ s), count x (a ::ₘ s)) =
∑ x in to_finset (a ::ₘ s), ((if x = a then 1 else 0) + count x s) :
finset.sum_congr rfl $ λ _ _, by split_ifs;
[simp only [h, count_cons_self, nat.one_add], simp only [count_cons_of_ne h, zero_add]]
... = card (a ::ₘ s) :
begin
by_cases a ∈ s.to_finset,
{ have : ∑ x in s.to_finset, ite (x = a) 1 0 = ∑ x in {a}, ite (x = a) 1 0,
{ rw [finset.sum_ite_eq', if_pos h, finset.sum_singleton, if_pos rfl], },
rw [to_finset_cons, finset.insert_eq_of_mem h, finset.sum_add_distrib, ih, this,
finset.sum_singleton, if_pos rfl, add_comm, card_cons] },
{ have ha : a ∉ s, by rwa mem_to_finset at h,
have : ∑ x in to_finset s, ite (x = a) 1 0 = ∑ x in to_finset s, 0, from
finset.sum_congr rfl (λ x hx, if_neg $ by rintro rfl; cc),
rw [to_finset_cons, finset.sum_insert h, if_pos rfl, finset.sum_add_distrib, this,
finset.sum_const_zero, ih, count_eq_zero_of_not_mem ha, zero_add, add_comm, card_cons] }
end)
lemma count_sum' {s : finset β} {a : α} {f : β → multiset α} :
count a (∑ x in s, f x) = ∑ x in s, count a (f x) :=
by { dunfold finset.sum, rw count_sum }
@[simp] lemma to_finset_sum_count_nsmul_eq (s : multiset α) :
(∑ a in s.to_finset, s.count a • {a}) = s :=
begin
apply ext', intro b,
rw count_sum',
have h : count b s = count b (count b s • {b}),
{ rw [count_nsmul, count_singleton_self, mul_one] },
rw h, clear h,
apply finset.sum_eq_single b,
{ intros c h hcb, rw count_nsmul, convert mul_zero (count c s),
apply count_eq_zero.mpr, exact finset.not_mem_singleton.mpr (ne.symm hcb) },
{ intro hb, rw [count_eq_zero_of_not_mem (mt mem_to_finset.2 hb), count_nsmul, zero_mul]}
end
theorem exists_smul_of_dvd_count (s : multiset α) {k : ℕ}
(h : ∀ (a : α), a ∈ s → k ∣ multiset.count a s) :
∃ (u : multiset α), s = k • u :=
begin
use ∑ a in s.to_finset, (s.count a / k) • {a},
have h₂ : ∑ (x : α) in s.to_finset, k • (count x s / k) • ({x} : multiset α) =
∑ (x : α) in s.to_finset, count x s • {x},
{ apply finset.sum_congr rfl,
intros x hx,
rw [← mul_nsmul, nat.mul_div_cancel' (h x (mem_to_finset.mp hx))] },
rw [← finset.sum_nsmul, h₂, to_finset_sum_count_nsmul_eq]
end
lemma to_finset_prod_dvd_prod [comm_monoid α] (S : multiset α) : S.to_finset.prod id ∣ S.prod :=
begin
rw finset.prod_eq_multiset_prod,
refine multiset.prod_dvd_prod_of_le _,
simp [multiset.dedup_le S],
end
@[to_additive]
lemma prod_sum {α : Type*} {ι : Type*} [comm_monoid α] (f : ι → multiset α) (s : finset ι) :
(∑ x in s, f x).prod = ∏ x in s, (f x).prod :=
begin
classical,
induction s using finset.induction_on with a t hat ih,
{ rw [finset.sum_empty, finset.prod_empty, multiset.prod_zero] },
{ rw [finset.sum_insert hat, finset.prod_insert hat, multiset.prod_add, ih] }
end
end multiset
@[simp, norm_cast] lemma nat.cast_sum [add_comm_monoid β] [has_one β] (s : finset α) (f : α → ℕ) :
↑(∑ x in s, f x : ℕ) = (∑ x in s, (f x : β)) :=
(nat.cast_add_monoid_hom β).map_sum f s
@[simp, norm_cast] lemma int.cast_sum [add_comm_group β] [has_one β] (s : finset α) (f : α → ℤ) :
↑(∑ x in s, f x : ℤ) = (∑ x in s, (f x : β)) :=
(int.cast_add_hom β).map_sum f s
@[simp, norm_cast] lemma nat.cast_prod {R : Type*} [comm_semiring R] (f : α → ℕ) (s : finset α) :
(↑∏ i in s, f i : R) = ∏ i in s, f i :=
(nat.cast_ring_hom R).map_prod _ _
@[simp, norm_cast] lemma int.cast_prod {R : Type*} [comm_ring R] (f : α → ℤ) (s : finset α) :
(↑∏ i in s, f i : R) = ∏ i in s, f i :=
(int.cast_ring_hom R).map_prod _ _
@[simp, norm_cast] lemma units.coe_prod {M : Type*} [comm_monoid M] (f : α → Mˣ)
(s : finset α) : (↑∏ i in s, f i : M) = ∏ i in s, f i :=
(units.coe_hom M).map_prod _ _
lemma units.mk0_prod [comm_group_with_zero β] (s : finset α) (f : α → β) (h) :
units.mk0 (∏ b in s, f b) h =
∏ b in s.attach, units.mk0 (f b) (λ hh, h (finset.prod_eq_zero b.2 hh)) :=
by { classical, induction s using finset.induction_on; simp* }
lemma nat_abs_sum_le {ι : Type*} (s : finset ι) (f : ι → ℤ) :
(∑ i in s, f i).nat_abs ≤ ∑ i in s, (f i).nat_abs :=
begin
classical,
apply finset.induction_on s,
{ simp only [finset.sum_empty, int.nat_abs_zero] },
{ intros i s his IH,
simp only [his, finset.sum_insert, not_false_iff],
exact (int.nat_abs_add_le _ _).trans (add_le_add le_rfl IH) }
end
|
f6bf8cc0e76d84aa26ab60059470869052932383 | 07c76fbd96ea1786cc6392fa834be62643cea420 | /hott/hit/pushout.hlean | a21df3f4d8a8b55cdc71ca8773efb9876cba2dd3 | [
"Apache-2.0"
] | permissive | fpvandoorn/lean2 | 5a430a153b570bf70dc8526d06f18fc000a60ad9 | 0889cf65b7b3cebfb8831b8731d89c2453dd1e9f | refs/heads/master | 1,592,036,508,364 | 1,545,093,958,000 | 1,545,093,958,000 | 75,436,854 | 0 | 0 | null | 1,480,718,780,000 | 1,480,718,780,000 | null | UTF-8 | Lean | false | false | 14,456 | hlean | /-
Copyright (c) 2015-16 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Ulrik Buchholtz, Jakob von Raumer
Declaration and properties of the pushout
-/
import .quotient types.sigma types.arrow_2
open quotient eq sum equiv is_trunc pointed
namespace pushout
section
parameters {TL BL TR : Type} (f : TL → BL) (g : TL → TR)
local abbreviation A := BL + TR
inductive pushout_rel : A → A → Type :=
| Rmk : Π(x : TL), pushout_rel (inl (f x)) (inr (g x))
open pushout_rel
local abbreviation R := pushout_rel
definition pushout : Type := quotient R -- TODO: define this in root namespace
parameters {f g}
definition inl (x : BL) : pushout :=
class_of R (inl x)
definition inr (x : TR) : pushout :=
class_of R (inr x)
definition glue (x : TL) : inl (f x) = inr (g x) :=
eq_of_rel pushout_rel (Rmk f g x)
protected definition rec {P : pushout → Type} (Pinl : Π(x : BL), P (inl x))
(Pinr : Π(x : TR), P (inr x)) (Pglue : Π(x : TL), Pinl (f x) =[glue x] Pinr (g x))
(y : pushout) : P y :=
begin
induction y,
{ cases a,
apply Pinl,
apply Pinr},
{ cases H, apply Pglue}
end
protected definition rec_on [reducible] {P : pushout → Type} (y : pushout)
(Pinl : Π(x : BL), P (inl x)) (Pinr : Π(x : TR), P (inr x))
(Pglue : Π(x : TL), Pinl (f x) =[glue x] Pinr (g x)) : P y :=
rec Pinl Pinr Pglue y
theorem rec_glue {P : pushout → Type} (Pinl : Π(x : BL), P (inl x))
(Pinr : Π(x : TR), P (inr x)) (Pglue : Π(x : TL), Pinl (f x) =[glue x] Pinr (g x))
(x : TL) : apd (rec Pinl Pinr Pglue) (glue x) = Pglue x :=
!rec_eq_of_rel
protected definition elim {P : Type} (Pinl : BL → P) (Pinr : TR → P)
(Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) (y : pushout) : P :=
rec Pinl Pinr (λx, pathover_of_eq _ (Pglue x)) y
protected definition elim_on [reducible] {P : Type} (y : pushout) (Pinl : BL → P)
(Pinr : TR → P) (Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) : P :=
elim Pinl Pinr Pglue y
theorem elim_glue {P : Type} (Pinl : BL → P) (Pinr : TR → P)
(Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) (x : TL)
: ap (elim Pinl Pinr Pglue) (glue x) = Pglue x :=
begin
apply inj_inv !(pathover_constant (glue x)),
rewrite [▸*,-apd_eq_pathover_of_eq_ap,↑pushout.elim,rec_glue],
end
protected definition elim_type (Pinl : BL → Type) (Pinr : TR → Type)
(Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) : pushout → Type :=
quotient.elim_type (sum.rec Pinl Pinr)
begin intro v v' r, induction r, apply Pglue end
protected definition elim_type_on [reducible] (y : pushout) (Pinl : BL → Type)
(Pinr : TR → Type) (Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) : Type :=
elim_type Pinl Pinr Pglue y
theorem elim_type_glue (Pinl : BL → Type) (Pinr : TR → Type)
(Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) (x : TL)
: transport (elim_type Pinl Pinr Pglue) (glue x) = Pglue x :=
!elim_type_eq_of_rel_fn
theorem elim_type_glue_inv (Pinl : BL → Type) (Pinr : TR → Type)
(Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) (x : TL)
: transport (elim_type Pinl Pinr Pglue) (glue x)⁻¹ = to_inv (Pglue x) :=
!elim_type_eq_of_rel_inv
protected definition rec_prop {P : pushout → Type} [H : Πx, is_prop (P x)]
(Pinl : Π(x : BL), P (inl x)) (Pinr : Π(x : TR), P (inr x)) (y : pushout) :=
rec Pinl Pinr (λx, !is_prop.elimo) y
protected definition elim_prop {P : Type} [H : is_prop P] (Pinl : BL → P) (Pinr : TR → P)
(y : pushout) : P :=
elim Pinl Pinr (λa, !is_prop.elim) y
end
end pushout
attribute pushout.inl pushout.inr [constructor]
attribute pushout.rec pushout.elim [unfold 10] [recursor 10]
attribute pushout.elim_type [unfold 9]
attribute pushout.rec_on pushout.elim_on [unfold 7]
attribute pushout.elim_type_on [unfold 6]
open sigma
namespace pushout
variables {TL BL TR : Type} (f : TL → BL) (g : TL → TR)
protected theorem elim_inl {P : Type} (Pinl : BL → P) (Pinr : TR → P)
(Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) {b b' : BL} (p : b = b')
: ap (pushout.elim Pinl Pinr Pglue) (ap inl p) = ap Pinl p :=
!ap_compose⁻¹
protected theorem elim_inr {P : Type} (Pinl : BL → P) (Pinr : TR → P)
(Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) {b b' : TR} (p : b = b')
: ap (pushout.elim Pinl Pinr Pglue) (ap inr p) = ap Pinr p :=
!ap_compose⁻¹
/- The non-dependent universal property -/
definition pushout_arrow_equiv (C : Type)
: (pushout f g → C) ≃ (Σ(i : BL → C) (j : TR → C), Πc, i (f c) = j (g c)) :=
begin
fapply equiv.MK,
{ intro f, exact ⟨λx, f (inl x), λx, f (inr x), λx, ap f (glue x)⟩},
{ intro v x, induction v with i w, induction w with j p, induction x,
exact (i a), exact (j a), exact (p x)},
{ intro v, induction v with i w, induction w with j p, esimp,
apply ap (λp, ⟨i, j, p⟩), apply eq_of_homotopy, intro x, apply elim_glue},
{ intro f, apply eq_of_homotopy, intro x, induction x: esimp,
apply eq_pathover, apply hdeg_square, esimp, apply elim_glue},
end
/- glue squares -/
protected definition glue_square {x x' : TL} (p : x = x')
: square (glue x) (glue x') (ap inl (ap f p)) (ap inr (ap g p)) :=
by cases p; apply vrefl
end pushout
open function sigma.ops
namespace pushout
/- The flattening lemma -/
section
universe variable u
parameters {TL BL TR : Type} (f : TL → BL) (g : TL → TR)
(Pinl : BL → Type.{u}) (Pinr : TR → Type.{u})
(Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x))
include Pglue
local abbreviation A := BL + TR
local abbreviation R : A → A → Type := pushout_rel f g
local abbreviation P [unfold 5] := pushout.elim_type Pinl Pinr Pglue
local abbreviation F : sigma (Pinl ∘ f) → sigma Pinl :=
λz, ⟨ f z.1 , z.2 ⟩
local abbreviation G : sigma (Pinl ∘ f) → sigma Pinr :=
λz, ⟨ g z.1 , Pglue z.1 z.2 ⟩
protected definition flattening : sigma P ≃ pushout F G :=
begin
apply equiv.trans !quotient.flattening.flattening_lemma,
fapply equiv.MK,
{ intro q, induction q with z z z' fr,
{ induction z with a p, induction a with x x,
{ exact inl ⟨x, p⟩ },
{ exact inr ⟨x, p⟩ } },
{ induction fr with a a' r p, induction r with x,
exact glue ⟨x, p⟩ } },
{ intro q, induction q with xp xp xp,
{ exact class_of _ ⟨sum.inl xp.1, xp.2⟩ },
{ exact class_of _ ⟨sum.inr xp.1, xp.2⟩ },
{ apply eq_of_rel, constructor } },
{ intro q, induction q with xp xp xp: induction xp with x p,
{ apply ap inl, reflexivity },
{ apply ap inr, reflexivity },
{ unfold F, unfold G, apply eq_pathover,
rewrite [ap_id,-ap_compose' (quotient.elim _ _)],
krewrite elim_glue, krewrite elim_eq_of_rel, apply hrefl } },
{ intro q, induction q with z z z' fr,
{ induction z with a p, induction a with x x,
{ reflexivity },
{ reflexivity } },
{ induction fr with a a' r p, induction r with x,
esimp, apply eq_pathover,
rewrite [ap_id,-ap_compose' (pushout.elim _ _ _)],
krewrite elim_eq_of_rel, krewrite elim_glue, apply hrefl } }
end
end
-- Commutativity of pushouts
section
variables {TL BL TR : Type} (f : TL → BL) (g : TL → TR)
protected definition transpose [unfold 6] : pushout f g → pushout g f :=
begin
intro x, induction x, apply inr a, apply inl a, apply !glue⁻¹
end
--TODO prove without krewrite?
protected definition transpose_involutive (x : pushout f g) :
pushout.transpose g f (pushout.transpose f g x) = x :=
begin
induction x, apply idp, apply idp,
apply eq_pathover, refine _ ⬝hp !ap_id⁻¹,
refine !(ap_compose (pushout.transpose _ _)) ⬝ph _, esimp[pushout.transpose],
krewrite [elim_glue, ap_inv, elim_glue, inv_inv], apply hrfl
end
protected definition symm [constructor] : pushout f g ≃ pushout g f :=
begin
fapply equiv.MK, do 2 exact !pushout.transpose,
do 2 (intro x; apply pushout.transpose_involutive),
end
end
-- Functoriality of pushouts
section
section lemmas
variables {X : Type} {x₀ x₁ x₂ x₃ : X}
(p : x₀ = x₁) (q : x₁ = x₂) (r : x₂ = x₃)
private definition is_equiv_functor_lemma₁
: (r ⬝ ((p ⬝ q ⬝ r)⁻¹ ⬝ p)) = q⁻¹ :=
by cases p; cases r; cases q; reflexivity
private definition is_equiv_functor_lemma₂
: (p ⬝ q ⬝ r)⁻¹ ⬝ (p ⬝ q) = r⁻¹ :=
by cases p; cases r; cases q; reflexivity
end lemmas
variables {TL BL TR : Type} {f : TL → BL} {g : TL → TR}
{TL' BL' TR' : Type} {f' : TL' → BL'} {g' : TL' → TR'}
(tl : TL → TL') (bl : BL → BL') (tr : TR → TR')
(fh : bl ∘ f ~ f' ∘ tl) (gh : tr ∘ g ~ g' ∘ tl)
include fh gh
protected definition functor [unfold 16] : pushout f g → pushout f' g' :=
begin
intro x, induction x with a b z,
{ exact inl (bl a) },
{ exact inr (tr b) },
{ exact (ap inl (fh z)) ⬝ glue (tl z) ⬝ (ap inr (gh z)⁻¹) }
end
protected definition ap_functor_inl [unfold 18] {x x' : BL} (p : x = x')
: ap (pushout.functor tl bl tr fh gh) (ap inl p) = ap inl (ap bl p) :=
by cases p; reflexivity
protected definition ap_functor_inr [unfold 18] {x x' : TR} (p : x = x')
: ap (pushout.functor tl bl tr fh gh) (ap inr p) = ap inr (ap tr p) :=
by cases p; reflexivity
variables [ietl : is_equiv tl] [iebl : is_equiv bl] [ietr : is_equiv tr]
include ietl iebl ietr
open equiv is_equiv arrow
protected definition is_equiv_functor [instance] [constructor]
: is_equiv (pushout.functor tl bl tr fh gh) :=
adjointify
(pushout.functor tl bl tr fh gh)
(pushout.functor tl⁻¹ bl⁻¹ tr⁻¹
(inv_commute_of_commute tl bl f f' fh)
(inv_commute_of_commute tl tr g g' gh))
abstract begin
intro x', induction x' with a' b' z',
{ apply ap inl, apply right_inv },
{ apply ap inr, apply right_inv },
{ apply eq_pathover,
rewrite [ap_id,-ap_compose' (pushout.functor tl bl tr fh gh)],
krewrite elim_glue,
rewrite [ap_inv,ap_con,ap_inv],
krewrite [pushout.ap_functor_inr], rewrite ap_con,
krewrite [pushout.ap_functor_inl,elim_glue],
apply transpose,
apply move_top_of_right, apply move_top_of_left',
krewrite [-(ap_inv inl),-ap_con,-(ap_inv inr),-ap_con],
apply move_top_of_right, apply move_top_of_left',
krewrite [-ap_con,-(ap_inv inl),-ap_con],
rewrite ap_bot_inv_commute_of_commute,
apply eq_hconcat (ap02 inl
(is_equiv_functor_lemma₁
(right_inv bl (f' z'))
(ap f' (right_inv tl z')⁻¹)
(fh (tl⁻¹ z'))⁻¹)),
rewrite [ap_inv f',inv_inv],
rewrite ap_bot_inv_commute_of_commute,
refine hconcat_eq _ (ap02 inr
(is_equiv_functor_lemma₁
(right_inv tr (g' z'))
(ap g' (right_inv tl z')⁻¹)
(gh (tl⁻¹ z'))⁻¹))⁻¹,
rewrite [ap_inv g',inv_inv],
apply pushout.glue_square }
end end
abstract begin
intro x, induction x with a b z,
{ apply ap inl, apply left_inv },
{ apply ap inr, apply left_inv },
{ apply eq_pathover,
rewrite [ap_id,-ap_compose'
(pushout.functor tl⁻¹ bl⁻¹ tr⁻¹ _ _)
(pushout.functor tl bl tr _ _)],
krewrite elim_glue,
rewrite [ap_inv,ap_con,ap_inv],
krewrite [pushout.ap_functor_inr], rewrite ap_con,
krewrite [pushout.ap_functor_inl,elim_glue],
apply transpose,
apply move_top_of_right, apply move_top_of_left',
krewrite [-(ap_inv inl),-ap_con,-(ap_inv inr),-ap_con],
apply move_top_of_right, apply move_top_of_left',
krewrite [-ap_con,-(ap_inv inl),-ap_con],
rewrite inv_commute_of_commute_top,
apply eq_hconcat (ap02 inl
(is_equiv_functor_lemma₂
(ap bl⁻¹ (fh z))⁻¹
(left_inv bl (f z))
(ap f (left_inv tl z)⁻¹))),
rewrite [ap_inv f,inv_inv],
rewrite inv_commute_of_commute_top,
refine hconcat_eq _ (ap02 inr
(is_equiv_functor_lemma₂
(ap tr⁻¹ (gh z))⁻¹
(left_inv tr (g z))
(ap g (left_inv tl z)⁻¹)))⁻¹,
rewrite [ap_inv g,inv_inv],
apply pushout.glue_square }
end end
end
/- version giving the equivalence -/
section
variables {TL BL TR : Type} (f : TL → BL) (g : TL → TR)
{TL' BL' TR' : Type} (f' : TL' → BL') (g' : TL' → TR')
(tl : TL ≃ TL') (bl : BL ≃ BL') (tr : TR ≃ TR')
(fh : bl ∘ f ~ f' ∘ tl) (gh : tr ∘ g ~ g' ∘ tl)
include fh gh
protected definition equiv [constructor] : pushout f g ≃ pushout f' g' :=
equiv.mk (pushout.functor tl bl tr fh gh) _
end
definition pointed_pushout [instance] [constructor] {TL BL TR : Type} [HTL : pointed TL]
[HBL : pointed BL] [HTR : pointed TR] (f : TL → BL) (g : TL → TR) : pointed (pushout f g) :=
pointed.mk (inl (point _))
end pushout open pushout
definition ppushout [constructor] {TL BL TR : Type*} (f : TL →* BL) (g : TL →* TR) : Type* :=
pointed.mk' (pushout f g)
namespace pushout
section
parameters {TL BL TR : Type*} (f : TL →* BL) (g : TL →* TR)
parameters {f g}
definition pinl [constructor] : BL →* ppushout f g :=
pmap.mk inl idp
definition pinr [constructor] : TR →* ppushout f g :=
pmap.mk inr ((ap inr (respect_pt g))⁻¹ ⬝ !glue⁻¹ ⬝ (ap inl (respect_pt f)))
definition pglue (x : TL) : pinl (f x) = pinr (g x) := -- TODO do we need this?
!glue
end
section
variables {TL BL TR : Type*} (f : TL →* BL) (g : TL →* TR)
protected definition psymm [constructor] : ppushout f g ≃* ppushout g f :=
begin
fapply pequiv_of_equiv,
{ apply pushout.symm },
{ exact ap inr (respect_pt f)⁻¹ ⬝ !glue⁻¹ ⬝ ap inl (respect_pt g) }
end
end
end pushout
|
1d3cd1ca5d72f3bfbedbaaf2147ad54dbce4c0de | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/topology/order.lean | cd8b6cbe65cc36af1117184831d7cda3725e3ec3 | [
"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 | 27,489 | 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 topology.basic
/-!
# Ordering on topologies and (co)induced topologies
Topologies on a fixed type `α` are ordered, by reverse inclusion.
That is, for topologies `t₁` and `t₂` on `α`, we write `t₁ ≤ t₂`
if every set open in `t₂` is also open in `t₁`.
(One also calls `t₁` finer than `t₂`, and `t₂` coarser than `t₁`.)
Any function `f : α → β` induces
`induced f : topological_space β → topological_space α`
and `coinduced f : topological_space α → topological_space β`.
Continuity, the ordering on topologies and (co)induced topologies are
related as follows:
* The identity map (α, t₁) → (α, t₂) is continuous iff t₁ ≤ t₂.
* A map f : (α, t) → (β, u) is continuous
iff t ≤ induced f u (`continuous_iff_le_induced`)
iff coinduced f t ≤ u (`continuous_iff_coinduced_le`).
Topologies on α form a complete lattice, with ⊥ the discrete topology
and ⊤ the indiscrete topology.
For a function f : α → β, (coinduced f, induced f) is a Galois connection
between topologies on α and topologies on β.
## Implementation notes
There is a Galois insertion between topologies on α (with the inclusion ordering)
and all collections of sets in α. The complete lattice structure on topologies
on α is defined as the reverse of the one obtained via this Galois insertion.
## Tags
finer, coarser, induced topology, coinduced topology
-/
open set filter lattice classical
open_locale classical topological_space
universes u v w
namespace topological_space
variables {α : Type u}
/-- The open sets of the least topology containing a collection of basic sets. -/
inductive generate_open (g : set (set α)) : set α → Prop
| basic : ∀s∈g, generate_open s
| univ : generate_open univ
| inter : ∀s t, generate_open s → generate_open t → generate_open (s ∩ t)
| sUnion : ∀k, (∀s∈k, generate_open s) → generate_open (⋃₀ k)
/-- The smallest topological space containing the collection `g` of basic sets -/
def generate_from (g : set (set α)) : topological_space α :=
{ is_open := generate_open g,
is_open_univ := generate_open.univ g,
is_open_inter := generate_open.inter,
is_open_sUnion := generate_open.sUnion }
lemma nhds_generate_from {g : set (set α)} {a : α} :
@nhds α (generate_from g) a = (⨅s∈{s | a ∈ s ∧ s ∈ g}, principal s) :=
by rw nhds_def; exact le_antisymm
(infi_le_infi $ assume s, infi_le_infi_const $ assume ⟨as, sg⟩, ⟨as, generate_open.basic _ sg⟩)
(le_infi $ assume s, le_infi $ assume ⟨as, hs⟩,
begin
revert as, clear_, induction hs,
case generate_open.basic : s hs
{ exact assume as, infi_le_of_le s $ infi_le _ ⟨as, hs⟩ },
case generate_open.univ
{ rw [principal_univ],
exact assume _, le_top },
case generate_open.inter : s t hs' ht' hs ht
{ exact assume ⟨has, hat⟩, calc _ ≤ principal s ⊓ principal t : le_inf (hs has) (ht hat)
... = _ : inf_principal },
case generate_open.sUnion : k hk' hk
{ exact λ ⟨t, htk, hat⟩, calc _ ≤ principal t : hk t htk hat
... ≤ _ : le_principal_iff.2 $ subset_sUnion_of_mem htk }
end)
lemma tendsto_nhds_generate_from {β : Type*} {m : α → β} {f : filter α} {g : set (set β)} {b : β}
(h : ∀s∈g, b ∈ s → m ⁻¹' s ∈ f) : tendsto m f (@nhds β (generate_from g) b) :=
by rw [nhds_generate_from]; exact
(tendsto_infi.2 $ assume s, tendsto_infi.2 $ assume ⟨hbs, hsg⟩, tendsto_principal.2 $ h s hsg hbs)
/-- Construct a topology on α given the filter of neighborhoods of each point of α. -/
protected def mk_of_nhds (n : α → filter α) : topological_space α :=
{ is_open := λs, ∀a∈s, s ∈ n a,
is_open_univ := assume x h, univ_mem_sets,
is_open_inter := assume s t hs ht x ⟨hxs, hxt⟩, inter_mem_sets (hs x hxs) (ht x hxt),
is_open_sUnion := assume s hs a ⟨x, hx, hxa⟩, mem_sets_of_superset (hs x hx _ hxa) (set.subset_sUnion_of_mem hx) }
lemma nhds_mk_of_nhds (n : α → filter α) (a : α)
(h₀ : pure ≤ n) (h₁ : ∀{a s}, s ∈ n a → ∃ t ∈ n a, t ⊆ s ∧ ∀a' ∈ t, s ∈ n a') :
@nhds α (topological_space.mk_of_nhds n) a = n a :=
begin
letI := topological_space.mk_of_nhds n,
refine le_antisymm (assume s hs, _) (assume s hs, _),
{ have h₀ : {b | s ∈ n b} ⊆ s := assume b hb, mem_pure_sets.1 $ h₀ b hb,
have h₁ : {b | s ∈ n b} ∈ 𝓝 a,
{ refine mem_nhds_sets (assume b (hb : s ∈ n b), _) hs,
rcases h₁ hb with ⟨t, ht, hts, h⟩,
exact mem_sets_of_superset ht h },
exact mem_sets_of_superset h₁ h₀ },
{ rcases (@mem_nhds_sets_iff α (topological_space.mk_of_nhds n) _ _).1 hs with ⟨t, hts, ht, hat⟩,
exact (n a).sets_of_superset (ht _ hat) hts },
end
end topological_space
section lattice
variables {α : Type u} {β : Type v}
/-- The inclusion ordering on topologies on α. We use it to get a complete
lattice instance via the Galois insertion method, but the partial order
that we will eventually impose on `topological_space α` is the reverse one. -/
def tmp_order : partial_order (topological_space α) :=
{ le := λt s, t.is_open ≤ s.is_open,
le_antisymm := assume t s h₁ h₂, topological_space_eq $ le_antisymm h₁ h₂,
le_refl := assume t, le_refl t.is_open,
le_trans := assume a b c h₁ h₂, @le_trans _ _ a.is_open b.is_open c.is_open h₁ h₂ }
local attribute [instance] tmp_order
/- We'll later restate this lemma in terms of the correct order on `topological_space α`. -/
private lemma generate_from_le_iff_subset_is_open {g : set (set α)} {t : topological_space α} :
topological_space.generate_from g ≤ t ↔ g ⊆ {s | t.is_open s} :=
iff.intro
(assume ht s hs, ht _ $ topological_space.generate_open.basic s hs)
(assume hg s hs, hs.rec_on (assume v hv, hg hv)
t.is_open_univ (assume u v _ _, t.is_open_inter u v) (assume k _, t.is_open_sUnion k))
/-- If `s` equals the collection of open sets in the topology it generates,
then `s` defines a topology. -/
protected def mk_of_closure (s : set (set α))
(hs : {u | (topological_space.generate_from s).is_open u} = s) : topological_space α :=
{ is_open := λu, u ∈ s,
is_open_univ := hs ▸ topological_space.generate_open.univ _,
is_open_inter := hs ▸ topological_space.generate_open.inter,
is_open_sUnion := hs ▸ topological_space.generate_open.sUnion }
lemma mk_of_closure_sets {s : set (set α)}
{hs : {u | (topological_space.generate_from s).is_open u} = s} :
mk_of_closure s hs = topological_space.generate_from s :=
topological_space_eq hs.symm
/-- The Galois insertion between `set (set α)` and `topological_space α` whose lower part
sends a collection of subsets of α to the topology they generate, and whose upper part
sends a topology to its collection of open subsets. -/
def gi_generate_from (α : Type*) :
galois_insertion topological_space.generate_from (λt:topological_space α, {s | t.is_open s}) :=
{ gc := assume g t, generate_from_le_iff_subset_is_open,
le_l_u := assume ts s hs, topological_space.generate_open.basic s hs,
choice := λg hg, mk_of_closure g
(subset.antisymm hg $ generate_from_le_iff_subset_is_open.1 $ le_refl _),
choice_eq := assume s hs, mk_of_closure_sets }
lemma generate_from_mono {α} {g₁ g₂ : set (set α)} (h : g₁ ⊆ g₂) :
topological_space.generate_from g₁ ≤ topological_space.generate_from g₂ :=
(gi_generate_from _).gc.monotone_l h
/-- The complete lattice of topological spaces, but built on the inclusion ordering. -/
def tmp_complete_lattice {α : Type u} : complete_lattice (topological_space α) :=
(gi_generate_from α).lift_complete_lattice
/-- The ordering on topologies on the type `α`.
`t ≤ s` if every set open in `s` is also open in `t` (`t` is finer than `s`). -/
instance : partial_order (topological_space α) :=
{ le := λ t s, s.is_open ≤ t.is_open,
le_antisymm := assume t s h₁ h₂, topological_space_eq $ le_antisymm h₂ h₁,
le_refl := assume t, le_refl t.is_open,
le_trans := assume a b c h₁ h₂, le_trans h₂ h₁ }
lemma le_generate_from_iff_subset_is_open {g : set (set α)} {t : topological_space α} :
t ≤ topological_space.generate_from g ↔ g ⊆ {s | t.is_open s} :=
generate_from_le_iff_subset_is_open
/-- Topologies on `α` form a complete lattice, with `⊥` the discrete topology
and `⊤` the indiscrete topology. The infimum of a collection of topologies
is the topology generated by all their open sets, while the supremem is the
topology whose open sets are those sets open in every member of the collection. -/
instance : complete_lattice (topological_space α) :=
@order_dual.lattice.complete_lattice _ tmp_complete_lattice
/-- A topological space is discrete if every set is open, that is,
its topology equals the discrete topology `⊥`. -/
class discrete_topology (α : Type*) [t : topological_space α] : Prop :=
(eq_bot : t = ⊥)
@[simp] lemma is_open_discrete [topological_space α] [discrete_topology α] (s : set α) :
is_open s :=
(discrete_topology.eq_bot α).symm ▸ trivial
lemma continuous_of_discrete_topology [topological_space α] [discrete_topology α] [topological_space β] {f : α → β} : continuous f :=
λs hs, is_open_discrete _
lemma nhds_bot (α : Type*) : (@nhds α ⊥) = pure :=
begin
ext a s,
rw [mem_nhds_sets_iff, mem_pure_iff],
split,
{ exact assume ⟨t, ht, _, hta⟩, ht hta },
{ exact assume h, ⟨{a}, set.singleton_subset_iff.2 h, trivial, set.mem_singleton a⟩ }
end
lemma nhds_discrete (α : Type*) [topological_space α] [discrete_topology α] : (@nhds α _) = pure :=
(discrete_topology.eq_bot α).symm ▸ nhds_bot α
lemma le_of_nhds_le_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₁ x ≤ @nhds α t₂ x) :
t₁ ≤ t₂ :=
assume s, show @is_open α t₂ s → @is_open α t₁ s,
by { simp only [is_open_iff_nhds, le_principal_iff], exact assume hs a ha, h _ $ hs _ ha }
lemma eq_of_nhds_eq_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₁ x = @nhds α t₂ x) :
t₁ = t₂ :=
le_antisymm
(le_of_nhds_le_nhds $ assume x, le_of_eq $ h x)
(le_of_nhds_le_nhds $ assume x, le_of_eq $ (h x).symm)
lemma eq_bot_of_singletons_open {t : topological_space α} (h : ∀ x, t.is_open {x}) : t = ⊥ :=
bot_unique $ le_of_nhds_le_nhds $ assume x,
have 𝓝 x ≤ pure x, from nhds_le_of_le (mem_singleton _) (h x) (by simp),
le_trans this (@pure_le_nhds _ ⊥ x)
end lattice
section galois_connection
variables {α : Type*} {β : Type*} {γ : Type*}
/-- Given `f : α → β` and a topology on `β`, the induced topology on `α` is the collection of
sets that are preimages of some open set in `β`. This is the coarsest topology that
makes `f` continuous. -/
def topological_space.induced {α : Type u} {β : Type v} (f : α → β) (t : topological_space β) :
topological_space α :=
{ is_open := λs, ∃s', t.is_open s' ∧ f ⁻¹' s' = s,
is_open_univ := ⟨univ, t.is_open_univ, preimage_univ⟩,
is_open_inter := by rintro s₁ s₂ ⟨s'₁, hs₁, rfl⟩ ⟨s'₂, hs₂, rfl⟩;
exact ⟨s'₁ ∩ s'₂, t.is_open_inter _ _ hs₁ hs₂, preimage_inter⟩,
is_open_sUnion := assume s h,
begin
simp only [classical.skolem] at h,
cases h with f hf,
apply exists.intro (⋃(x : set α) (h : x ∈ s), f x h),
simp only [sUnion_eq_bUnion, preimage_Union, (λx h, (hf x h).right)], refine ⟨_, rfl⟩,
exact (@is_open_Union β _ t _ $ assume i,
show is_open (⋃h, f i h), from @is_open_Union β _ t _ $ assume h, (hf i h).left)
end }
lemma is_open_induced_iff [t : topological_space β] {s : set α} {f : α → β} :
@is_open α (t.induced f) s ↔ (∃t, is_open t ∧ f ⁻¹' t = s) :=
iff.rfl
lemma is_closed_induced_iff [t : topological_space β] {s : set α} {f : α → β} :
@is_closed α (t.induced f) s ↔ (∃t, is_closed t ∧ s = f ⁻¹' t) :=
⟨assume ⟨t, ht, heq⟩, ⟨-t, is_closed_compl_iff.2 ht,
by simp only [preimage_compl, heq, lattice.neg_neg]⟩,
assume ⟨t, ht, heq⟩, ⟨-t, ht, by simp only [preimage_compl, heq.symm]⟩⟩
/-- Given `f : α → β` and a topology on `α`, the coinduced topology on `β` is defined
such that `s:set β` is open if the preimage of `s` is open. This is the finest topology that
makes `f` continuous. -/
def topological_space.coinduced {α : Type u} {β : Type v} (f : α → β) (t : topological_space α) :
topological_space β :=
{ is_open := λs, t.is_open (f ⁻¹' s),
is_open_univ := by rw preimage_univ; exact t.is_open_univ,
is_open_inter := assume s₁ s₂ h₁ h₂, by rw preimage_inter; exact t.is_open_inter _ _ h₁ h₂,
is_open_sUnion := assume s h, by rw [preimage_sUnion]; exact (@is_open_Union _ _ t _ $ assume i,
show is_open (⋃ (H : i ∈ s), f ⁻¹' i), from
@is_open_Union _ _ t _ $ assume hi, h i hi) }
lemma is_open_coinduced {t : topological_space α} {s : set β} {f : α → β} :
@is_open β (topological_space.coinduced f t) s ↔ is_open (f ⁻¹' s) :=
iff.rfl
variables {t t₁ t₂ : topological_space α} {t' : topological_space β} {f : α → β} {g : β → α}
lemma coinduced_le_iff_le_induced {f : α → β } {tα : topological_space α} {tβ : topological_space β} :
tα.coinduced f ≤ tβ ↔ tα ≤ tβ.induced f :=
iff.intro
(assume h s ⟨t, ht, hst⟩, hst ▸ h _ ht)
(assume h s hs, show tα.is_open (f ⁻¹' s), from h _ ⟨s, hs, rfl⟩)
lemma gc_coinduced_induced (f : α → β) :
galois_connection (topological_space.coinduced f) (topological_space.induced f) :=
assume f g, coinduced_le_iff_le_induced
lemma induced_mono (h : t₁ ≤ t₂) : t₁.induced g ≤ t₂.induced g :=
(gc_coinduced_induced g).monotone_u h
lemma coinduced_mono (h : t₁ ≤ t₂) : t₁.coinduced f ≤ t₂.coinduced f :=
(gc_coinduced_induced f).monotone_l h
@[simp] lemma induced_top : (⊤ : topological_space α).induced g = ⊤ :=
(gc_coinduced_induced g).u_top
@[simp] lemma induced_inf : (t₁ ⊓ t₂).induced g = t₁.induced g ⊓ t₂.induced g :=
(gc_coinduced_induced g).u_inf
@[simp] lemma induced_infi {ι : Sort w} {t : ι → topological_space α} :
(⨅i, t i).induced g = (⨅i, (t i).induced g) :=
(gc_coinduced_induced g).u_infi
@[simp] lemma coinduced_bot : (⊥ : topological_space α).coinduced f = ⊥ :=
(gc_coinduced_induced f).l_bot
@[simp] lemma coinduced_sup : (t₁ ⊔ t₂).coinduced f = t₁.coinduced f ⊔ t₂.coinduced f :=
(gc_coinduced_induced f).l_sup
@[simp] lemma coinduced_supr {ι : Sort w} {t : ι → topological_space α} :
(⨆i, t i).coinduced f = (⨆i, (t i).coinduced f) :=
(gc_coinduced_induced f).l_supr
lemma induced_id [t : topological_space α] : t.induced id = t :=
topological_space_eq $ funext $ assume s, propext $
⟨assume ⟨s', hs, h⟩, h ▸ hs, assume hs, ⟨s, hs, rfl⟩⟩
lemma induced_compose [tγ : topological_space γ]
{f : α → β} {g : β → γ} : (tγ.induced g).induced f = tγ.induced (g ∘ f) :=
topological_space_eq $ funext $ assume s, propext $
⟨assume ⟨s', ⟨s, hs, h₂⟩, h₁⟩, h₁ ▸ h₂ ▸ ⟨s, hs, rfl⟩,
assume ⟨s, hs, h⟩, ⟨preimage g s, ⟨s, hs, rfl⟩, h ▸ rfl⟩⟩
lemma coinduced_id [t : topological_space α] : t.coinduced id = t :=
topological_space_eq rfl
lemma coinduced_compose [tα : topological_space α]
{f : α → β} {g : β → γ} : (tα.coinduced f).coinduced g = tα.coinduced (g ∘ f) :=
topological_space_eq rfl
end galois_connection
/- constructions using the complete lattice structure -/
section constructions
open topological_space
variables {α : Type u} {β : Type v}
instance inhabited_topological_space {α : Type u} : inhabited (topological_space α) :=
⟨⊤⟩
instance : topological_space empty := ⊥
instance : discrete_topology empty := ⟨rfl⟩
instance : topological_space unit := ⊥
instance : discrete_topology unit := ⟨rfl⟩
instance : topological_space bool := ⊥
instance : discrete_topology bool := ⟨rfl⟩
instance : topological_space ℕ := ⊥
instance : discrete_topology ℕ := ⟨rfl⟩
instance : topological_space ℤ := ⊥
instance : discrete_topology ℤ := ⟨rfl⟩
instance sierpinski_space : topological_space Prop :=
generate_from {{true}}
lemma le_generate_from {t : topological_space α} { g : set (set α) } (h : ∀s∈g, is_open s) :
t ≤ generate_from g :=
le_generate_from_iff_subset_is_open.2 h
lemma induced_generate_from_eq {α β} {b : set (set β)} {f : α → β} :
(generate_from b).induced f = topological_space.generate_from (preimage f '' b) :=
le_antisymm
(le_generate_from $ ball_image_iff.2 $ assume s hs, ⟨s, generate_open.basic _ hs, rfl⟩)
(coinduced_le_iff_le_induced.1 $ le_generate_from $ assume s hs,
generate_open.basic _ $ mem_image_of_mem _ hs)
/-- This construction is left adjoint to the operation sending a topology on `α`
to its neighborhood filter at a fixed point `a : α`. -/
protected def topological_space.nhds_adjoint (a : α) (f : filter α) : topological_space α :=
{ is_open := λs, a ∈ s → s ∈ f,
is_open_univ := assume s, univ_mem_sets,
is_open_inter := assume s t hs ht ⟨has, hat⟩, inter_mem_sets (hs has) (ht hat),
is_open_sUnion := assume k hk ⟨u, hu, hau⟩, mem_sets_of_superset (hk u hu hau) (subset_sUnion_of_mem hu) }
lemma gc_nhds (a : α) :
galois_connection (topological_space.nhds_adjoint a) (λt, @nhds α t a) :=
assume f t, by { rw le_nhds_iff, exact ⟨λ H s hs has, H _ has hs, λ H s has hs, H _ hs has⟩ }
lemma nhds_mono {t₁ t₂ : topological_space α} {a : α} (h : t₁ ≤ t₂) :
@nhds α t₁ a ≤ @nhds α t₂ a := (gc_nhds a).monotone_u h
lemma nhds_infi {ι : Sort*} {t : ι → topological_space α} {a : α} :
@nhds α (infi t) a = (⨅i, @nhds α (t i) a) := (gc_nhds a).u_infi
lemma nhds_Inf {s : set (topological_space α)} {a : α} :
@nhds α (Inf s) a = (⨅t∈s, @nhds α t a) := (gc_nhds a).u_Inf
lemma nhds_inf {t₁ t₂ : topological_space α} {a : α} :
@nhds α (t₁ ⊓ t₂) a = @nhds α t₁ a ⊓ @nhds α t₂ a := (gc_nhds a).u_inf
lemma nhds_top {a : α} : @nhds α ⊤ a = ⊤ := (gc_nhds a).u_top
local notation `cont` := @continuous _ _
local notation `tspace` := topological_space
open topological_space
variables {γ : Type*} {f : α → β} {ι : Sort*}
lemma continuous_iff_coinduced_le {t₁ : tspace α} {t₂ : tspace β} :
cont t₁ t₂ f ↔ coinduced f t₁ ≤ t₂ := iff.rfl
lemma continuous_iff_le_induced {t₁ : tspace α} {t₂ : tspace β} :
cont t₁ t₂ f ↔ t₁ ≤ induced f t₂ :=
iff.trans continuous_iff_coinduced_le (gc_coinduced_induced f _ _)
theorem continuous_generated_from {t : tspace α} {b : set (set β)}
(h : ∀s∈b, is_open (f ⁻¹' s)) : cont t (generate_from b) f :=
continuous_iff_coinduced_le.2 $ le_generate_from h
lemma continuous_induced_dom {t : tspace β} : cont (induced f t) t f :=
assume s h, ⟨_, h, rfl⟩
lemma continuous_induced_rng {g : γ → α} {t₂ : tspace β} {t₁ : tspace γ}
(h : cont t₁ t₂ (f ∘ g)) : cont t₁ (induced f t₂) g :=
assume s ⟨t, ht, s_eq⟩, s_eq ▸ h t ht
lemma continuous_coinduced_rng {t : tspace α} : cont t (coinduced f t) f :=
assume s h, h
lemma continuous_coinduced_dom {g : β → γ} {t₁ : tspace α} {t₂ : tspace γ}
(h : cont t₁ t₂ (g ∘ f)) : cont (coinduced f t₁) t₂ g :=
assume s hs, h s hs
lemma continuous_le_dom {t₁ t₂ : tspace α} {t₃ : tspace β}
(h₁ : t₂ ≤ t₁) (h₂ : cont t₁ t₃ f) : cont t₂ t₃ f :=
assume s h, h₁ _ (h₂ s h)
lemma continuous_le_rng {t₁ : tspace α} {t₂ t₃ : tspace β}
(h₁ : t₂ ≤ t₃) (h₂ : cont t₁ t₂ f) : cont t₁ t₃ f :=
assume s h, h₂ s (h₁ s h)
lemma continuous_sup_dom {t₁ t₂ : tspace α} {t₃ : tspace β}
(h₁ : cont t₁ t₃ f) (h₂ : cont t₂ t₃ f) : cont (t₁ ⊔ t₂) t₃ f :=
assume s h, ⟨h₁ s h, h₂ s h⟩
lemma continuous_sup_rng_left {t₁ : tspace α} {t₃ t₂ : tspace β} :
cont t₁ t₂ f → cont t₁ (t₂ ⊔ t₃) f :=
continuous_le_rng le_sup_left
lemma continuous_sup_rng_right {t₁ : tspace α} {t₃ t₂ : tspace β} :
cont t₁ t₃ f → cont t₁ (t₂ ⊔ t₃) f :=
continuous_le_rng le_sup_right
lemma continuous_Sup_dom {t₁ : set (tspace α)} {t₂ : tspace β}
(h : ∀t∈t₁, cont t t₂ f) : cont (Sup t₁) t₂ f :=
continuous_iff_le_induced.2 $ Sup_le $ assume t ht, continuous_iff_le_induced.1 $ h t ht
lemma continuous_Sup_rng {t₁ : tspace α} {t₂ : set (tspace β)} {t : tspace β}
(h₁ : t ∈ t₂) (hf : cont t₁ t f) : cont t₁ (Sup t₂) f :=
continuous_iff_coinduced_le.2 $ le_Sup_of_le h₁ $ continuous_iff_coinduced_le.1 hf
lemma continuous_supr_dom {t₁ : ι → tspace α} {t₂ : tspace β}
(h : ∀i, cont (t₁ i) t₂ f) : cont (supr t₁) t₂ f :=
continuous_Sup_dom $ assume t ⟨i, (t_eq : t₁ i = t)⟩, t_eq ▸ h i
lemma continuous_supr_rng {t₁ : tspace α} {t₂ : ι → tspace β} {i : ι}
(h : cont t₁ (t₂ i) f) : cont t₁ (supr t₂) f :=
continuous_Sup_rng ⟨i, rfl⟩ h
lemma continuous_inf_rng {t₁ : tspace α} {t₂ t₃ : tspace β}
(h₁ : cont t₁ t₂ f) (h₂ : cont t₁ t₃ f) : cont t₁ (t₂ ⊓ t₃) f :=
continuous_iff_coinduced_le.2 $ le_inf
(continuous_iff_coinduced_le.1 h₁)
(continuous_iff_coinduced_le.1 h₂)
lemma continuous_inf_dom_left {t₁ t₂ : tspace α} {t₃ : tspace β} :
cont t₁ t₃ f → cont (t₁ ⊓ t₂) t₃ f :=
continuous_le_dom inf_le_left
lemma continuous_inf_dom_right {t₁ t₂ : tspace α} {t₃ : tspace β} :
cont t₂ t₃ f → cont (t₁ ⊓ t₂) t₃ f :=
continuous_le_dom inf_le_right
lemma continuous_Inf_dom {t₁ : set (tspace α)} {t₂ : tspace β} {t : tspace α} (h₁ : t ∈ t₁) :
cont t t₂ f → cont (Inf t₁) t₂ f :=
continuous_le_dom $ Inf_le h₁
lemma continuous_Inf_rng {t₁ : tspace α} {t₂ : set (tspace β)}
(h : ∀t∈t₂, cont t₁ t f) : cont t₁ (Inf t₂) f :=
continuous_iff_coinduced_le.2 $ le_Inf $ assume b hb, continuous_iff_coinduced_le.1 $ h b hb
lemma continuous_infi_dom {t₁ : ι → tspace α} {t₂ : tspace β} {i : ι} :
cont (t₁ i) t₂ f → cont (infi t₁) t₂ f :=
continuous_le_dom $ infi_le _ _
lemma continuous_infi_rng {t₁ : tspace α} {t₂ : ι → tspace β}
(h : ∀i, cont t₁ (t₂ i) f) : cont t₁ (infi t₂) f :=
continuous_iff_coinduced_le.2 $ le_infi $ assume i, continuous_iff_coinduced_le.1 $ h i
lemma continuous_bot {t : tspace β} : cont ⊥ t f :=
continuous_iff_le_induced.2 $ bot_le
lemma continuous_top {t : tspace α} : cont t ⊤ f :=
continuous_iff_coinduced_le.2 $ le_top
/- 𝓝 in the induced topology -/
theorem mem_nhds_induced [T : topological_space α] (f : β → α) (a : β) (s : set β) :
s ∈ @nhds β (topological_space.induced f T) a ↔ ∃ u ∈ 𝓝 (f a), f ⁻¹' u ⊆ s :=
begin
simp only [nhds_sets, is_open_induced_iff, exists_prop, set.mem_set_of_eq],
split,
{ rintros ⟨u, usub, ⟨v, openv, ueq⟩, au⟩,
exact ⟨v, ⟨v, set.subset.refl v, openv, by rwa ←ueq at au⟩, by rw ueq; exact usub⟩ },
rintros ⟨u, ⟨v, vsubu, openv, amem⟩, finvsub⟩,
exact ⟨f ⁻¹' v, set.subset.trans (set.preimage_mono vsubu) finvsub, ⟨⟨v, openv, rfl⟩, amem⟩⟩
end
theorem nhds_induced [T : topological_space α] (f : β → α) (a : β) :
@nhds β (topological_space.induced f T) a = comap f (𝓝 (f a)) :=
filter_eq $ by ext s; rw mem_nhds_induced; rw mem_comap_sets
lemma induced_iff_nhds_eq [tα : topological_space α] [tβ : topological_space β] (f : β → α) :
tβ = tα.induced f ↔ ∀ b, 𝓝 b = comap f (𝓝 $ f b) :=
⟨λ h a, h.symm ▸ nhds_induced f a, λ h, eq_of_nhds_eq_nhds $ λ x, by rw [h, nhds_induced]⟩
theorem map_nhds_induced_of_surjective [T : topological_space α]
{f : β → α} (hf : function.surjective f) (a : β) :
map f (@nhds β (topological_space.induced f T) a) = 𝓝 (f a) :=
by rw [nhds_induced, map_comap_of_surjective hf]
end constructions
section induced
open topological_space
variables {α : Type*} {β : Type*}
variables [t : topological_space β] {f : α → β}
theorem is_open_induced_eq {s : set α} :
@_root_.is_open _ (induced f t) s ↔ s ∈ preimage f '' {s | is_open s} :=
iff.rfl
theorem is_open_induced {s : set β} (h : is_open s) : (induced f t).is_open (f ⁻¹' s) :=
⟨s, h, rfl⟩
lemma map_nhds_induced_eq {a : α} (h : range f ∈ 𝓝 (f a)) :
map f (@nhds α (induced f t) a) = 𝓝 (f a) :=
by rw [nhds_induced, filter.map_comap h]
lemma closure_induced [t : topological_space β] {f : α → β} {a : α} {s : set α}
(hf : ∀x y, f x = f y → x = y) :
a ∈ @closure α (topological_space.induced f t) s ↔ f a ∈ closure (f '' s) :=
have comap f (𝓝 (f a) ⊓ principal (f '' s)) ≠ ⊥ ↔ 𝓝 (f a) ⊓ principal (f '' s) ≠ ⊥,
from ⟨assume h₁ h₂, h₁ $ h₂.symm ▸ comap_bot,
assume h,
forall_sets_neq_empty_iff_neq_bot.mp $
assume s₁ ⟨s₂, hs₂, (hs : f ⁻¹' s₂ ⊆ s₁)⟩,
have f '' s ∈ 𝓝 (f a) ⊓ principal (f '' s),
from mem_inf_sets_of_right $ by simp [subset.refl],
have s₂ ∩ f '' s ∈ 𝓝 (f a) ⊓ principal (f '' s),
from inter_mem_sets hs₂ this,
let ⟨b, hb₁, ⟨a, ha, ha₂⟩⟩ := inhabited_of_mem_sets h this in
ne_empty_of_mem $ hs $ by rwa [←ha₂] at hb₁⟩,
calc a ∈ @closure α (topological_space.induced f t) s
↔ (@nhds α (topological_space.induced f t) a) ⊓ principal s ≠ ⊥ : by rw [closure_eq_nhds]; refl
... ↔ comap f (𝓝 (f a)) ⊓ principal (f ⁻¹' (f '' s)) ≠ ⊥ : by rw [nhds_induced, preimage_image_eq _ hf]
... ↔ comap f (𝓝 (f a) ⊓ principal (f '' s)) ≠ ⊥ : by rw [comap_inf, ←comap_principal]
... ↔ _ : by rwa [closure_eq_nhds]
end induced
section sierpinski
variables {α : Type*} [topological_space α]
@[simp] lemma is_open_singleton_true : is_open ({true} : set Prop) :=
topological_space.generate_open.basic _ (by simp)
lemma continuous_Prop {p : α → Prop} : continuous p ↔ is_open {x | p x} :=
⟨assume h : continuous p,
have is_open (p ⁻¹' {true}),
from h _ is_open_singleton_true,
by simp [preimage, eq_true] at this; assumption,
assume h : is_open {x | p x},
continuous_generated_from $ assume s (hs : s ∈ {{true}}),
by simp at hs; simp [hs, preimage, eq_true, h]⟩
end sierpinski
section infi
variables {α : Type u} {ι : Type v} {t : ι → topological_space α}
lemma is_open_supr_iff {s : set α} : @is_open _ (⨆ i, t i) s ↔ ∀ i, @is_open _ (t i) s :=
begin
-- s defines a map from α to Prop, which is continuous iff s is open.
suffices : @continuous _ _ (⨆ i, t i) _ s ↔ ∀ i, @continuous _ _ (t i) _ s,
{ simpa only [continuous_Prop] using this },
simp only [continuous_iff_le_induced, supr_le_iff]
end
lemma is_closed_infi_iff {s : set α} : @is_closed _ (⨆ i, t i) s ↔ ∀ i, @is_closed _ (t i) s :=
is_open_supr_iff
end infi
|
63c5704b5992350e72cfa91e909ee0610d8d940c | bb31430994044506fa42fd667e2d556327e18dfe | /src/ring_theory/adjoin_root.lean | d3f088f947c3ce660b4eb1963c7d7614595fd948 | [
"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 | 29,545 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Chris Hughes
-/
import algebra.algebra.basic
import data.polynomial.field_division
import field_theory.minpoly.basic
import ring_theory.adjoin.basic
import ring_theory.finite_presentation
import ring_theory.finite_type
import ring_theory.power_basis
import ring_theory.principal_ideal_domain
/-!
# Adjoining roots of polynomials
This file defines the commutative ring `adjoin_root f`, the ring R[X]/(f) obtained from a
commutative ring `R` and a polynomial `f : R[X]`. If furthermore `R` is a field and `f` is
irreducible, the field structure on `adjoin_root f` is constructed.
We suggest stating results on `is_adjoin_root` instead of `adjoin_root` to achieve higher
generality, since `is_adjoin_root` works for all different constructions of `R[α]`
including `adjoin_root f = R[X]/(f)` itself.
## Main definitions and results
The main definitions are in the `adjoin_root` namespace.
* `mk f : R[X] →+* adjoin_root f`, the natural ring homomorphism.
* `of f : R →+* adjoin_root f`, the natural ring homomorphism.
* `root f : adjoin_root f`, the image of X in R[X]/(f).
* `lift (i : R →+* S) (x : S) (h : f.eval₂ i x = 0) : (adjoin_root f) →+* S`, the ring
homomorphism from R[X]/(f) to S extending `i : R →+* S` and sending `X` to `x`.
* `lift_hom (x : S) (hfx : aeval x f = 0) : adjoin_root f →ₐ[R] S`, the algebra
homomorphism from R[X]/(f) to S extending `algebra_map R S` and sending `X` to `x`
* `equiv : (adjoin_root f →ₐ[F] E) ≃ {x // x ∈ (f.map (algebra_map F E)).roots}` a
bijection between algebra homomorphisms from `adjoin_root` and roots of `f` in `S`
-/
noncomputable theory
open_locale classical
open_locale big_operators polynomial
universes u v w
variables {R : Type u} {S : Type v} {K : Type w}
open polynomial ideal
/-- Adjoin a root of a polynomial `f` to a commutative ring `R`. We define the new ring
as the quotient of `R[X]` by the principal ideal generated by `f`. -/
def adjoin_root [comm_ring R] (f : R[X]) : Type u :=
polynomial R ⧸ (span {f} : ideal R[X])
namespace adjoin_root
section comm_ring
variables [comm_ring R] (f : R[X])
instance : comm_ring (adjoin_root f) := ideal.quotient.comm_ring _
instance : inhabited (adjoin_root f) := ⟨0⟩
instance : decidable_eq (adjoin_root f) := classical.dec_eq _
protected lemma nontrivial [is_domain R] (h : degree f ≠ 0) : nontrivial (adjoin_root f) :=
ideal.quotient.nontrivial
begin
simp_rw [ne.def, span_singleton_eq_top, polynomial.is_unit_iff, not_exists, not_and],
rintro x hx rfl,
exact h (degree_C hx.ne_zero),
end
/-- Ring homomorphism from `R[x]` to `adjoin_root f` sending `X` to the `root`. -/
def mk : R[X] →+* adjoin_root f := ideal.quotient.mk _
@[elab_as_eliminator]
theorem induction_on {C : adjoin_root f → Prop} (x : adjoin_root f)
(ih : ∀ p : R[X], C (mk f p)) : C x :=
quotient.induction_on' x ih
/-- Embedding of the original ring `R` into `adjoin_root f`. -/
def of : R →+* adjoin_root f := (mk f).comp C
instance [comm_semiring S] [algebra S R] : algebra S (adjoin_root f) :=
ideal.quotient.algebra S
instance [comm_semiring S] [comm_semiring K] [has_smul S K] [algebra S R] [algebra K R]
[is_scalar_tower S K R] :
is_scalar_tower S K (adjoin_root f) :=
submodule.quotient.is_scalar_tower _ _
instance [comm_semiring S] [comm_semiring K] [algebra S R] [algebra K R] [smul_comm_class S K R] :
smul_comm_class S K (adjoin_root f) :=
submodule.quotient.smul_comm_class _ _
@[simp] lemma algebra_map_eq : algebra_map R (adjoin_root f) = of f := rfl
variables (S)
lemma algebra_map_eq' [comm_semiring S] [algebra S R] :
algebra_map S (adjoin_root f) = (of f).comp (algebra_map S R) := rfl
variables {S}
lemma finite_type : algebra.finite_type R (adjoin_root f) :=
(algebra.finite_type.polynomial R).of_surjective _ (ideal.quotient.mkₐ_surjective R _)
lemma finite_presentation : algebra.finite_presentation R (adjoin_root f) :=
(algebra.finite_presentation.polynomial R).quotient (submodule.fg_span_singleton f)
/-- The adjoined root. -/
def root : adjoin_root f := mk f X
variables {f}
instance has_coe_t : has_coe_t R (adjoin_root f) := ⟨of f⟩
/-- Two `R`-`alg_hom` from `adjoin_root f` to the same `R`-algebra are the same iff
they agree on `root f`. -/
@[ext] lemma alg_hom_ext [semiring S] [algebra R S] {g₁ g₂ : adjoin_root f →ₐ[R] S}
(h : g₁ (root f) = g₂ (root f)) : g₁ = g₂ :=
ideal.quotient.alg_hom_ext R $ polynomial.alg_hom_ext h
@[simp] lemma mk_eq_mk {g h : R[X]} : mk f g = mk f h ↔ f ∣ g - h :=
ideal.quotient.eq.trans ideal.mem_span_singleton
@[simp] lemma mk_eq_zero {g : R[X]} : mk f g = 0 ↔ f ∣ g :=
mk_eq_mk.trans $ by rw sub_zero
@[simp] lemma mk_self : mk f f = 0 :=
quotient.sound' $ quotient_add_group.left_rel_apply.mpr (mem_span_singleton.2 $ by simp)
@[simp] lemma mk_C (x : R) : mk f (C x) = x := rfl
@[simp] lemma mk_X : mk f X = root f := rfl
lemma mk_ne_zero_of_degree_lt (hf : monic f)
{g : R[X]} (h0 : g ≠ 0) (hd : degree g < degree f) : mk f g ≠ 0 :=
mk_eq_zero.not.2 $ hf.not_dvd_of_degree_lt h0 hd
lemma mk_ne_zero_of_nat_degree_lt (hf : monic f)
{g : R[X]} (h0 : g ≠ 0) (hd : nat_degree g < nat_degree f) : mk f g ≠ 0 :=
mk_eq_zero.not.2 $ hf.not_dvd_of_nat_degree_lt h0 hd
@[simp] lemma aeval_eq (p : R[X]) : aeval (root f) p = mk f p :=
polynomial.induction_on p (λ x, by { rw aeval_C, refl })
(λ p q ihp ihq, by rw [alg_hom.map_add, ring_hom.map_add, ihp, ihq])
(λ n x ih, by { rw [alg_hom.map_mul, aeval_C, alg_hom.map_pow, aeval_X,
ring_hom.map_mul, mk_C, ring_hom.map_pow, mk_X], refl })
theorem adjoin_root_eq_top : algebra.adjoin R ({root f} : set (adjoin_root f)) = ⊤ :=
algebra.eq_top_iff.2 $ λ x, induction_on f x $ λ p,
(algebra.adjoin_singleton_eq_range_aeval R (root f)).symm ▸ ⟨p, aeval_eq p⟩
@[simp] lemma eval₂_root (f : R[X]) : f.eval₂ (of f) (root f) = 0 :=
by rw [← algebra_map_eq, ← aeval_def, aeval_eq, mk_self]
lemma is_root_root (f : R[X]) : is_root (f.map (of f)) (root f) :=
by rw [is_root, eval_map, eval₂_root]
lemma is_algebraic_root (hf : f ≠ 0) : is_algebraic R (root f) :=
⟨f, hf, eval₂_root f⟩
variables [comm_ring S]
/-- Lift a ring homomorphism `i : R →+* S` to `adjoin_root f →+* S`. -/
def lift (i : R →+* S) (x : S) (h : f.eval₂ i x = 0) : (adjoin_root f) →+* S :=
begin
apply ideal.quotient.lift _ (eval₂_ring_hom i x),
intros g H,
rcases mem_span_singleton.1 H with ⟨y, hy⟩,
rw [hy, ring_hom.map_mul, coe_eval₂_ring_hom, h, zero_mul]
end
variables {i : R →+* S} {a : S} (h : f.eval₂ i a = 0)
@[simp] lemma lift_mk (g : R[X]) : lift i a h (mk f g) = g.eval₂ i a :=
ideal.quotient.lift_mk _ _ _
@[simp] lemma lift_root : lift i a h (root f) = a := by rw [root, lift_mk, eval₂_X]
@[simp] lemma lift_of {x : R} : lift i a h x = i x :=
by rw [← mk_C x, lift_mk, eval₂_C]
@[simp] lemma lift_comp_of : (lift i a h).comp (of f) = i :=
ring_hom.ext $ λ _, @lift_of _ _ _ _ _ _ _ h _
variables (f) [algebra R S]
/-- Produce an algebra homomorphism `adjoin_root f →ₐ[R] S` sending `root f` to
a root of `f` in `S`. -/
def lift_hom (x : S) (hfx : aeval x f = 0) : adjoin_root f →ₐ[R] S :=
{ commutes' := λ r, show lift _ _ hfx r = _, from lift_of hfx,
.. lift (algebra_map R S) x hfx }
@[simp] lemma coe_lift_hom (x : S) (hfx : aeval x f = 0) :
(lift_hom f x hfx : adjoin_root f →+* S) = lift (algebra_map R S) x hfx := rfl
@[simp] lemma aeval_alg_hom_eq_zero (ϕ : adjoin_root f →ₐ[R] S) : aeval (ϕ (root f)) f = 0 :=
begin
have h : ϕ.to_ring_hom.comp (of f) = algebra_map R S := ring_hom.ext_iff.mpr (ϕ.commutes),
rw [aeval_def, ←h, ←ring_hom.map_zero ϕ.to_ring_hom, ←eval₂_root f, hom_eval₂],
refl,
end
@[simp] lemma lift_hom_eq_alg_hom (f : R[X]) (ϕ : adjoin_root f →ₐ[R] S) :
lift_hom f (ϕ (root f)) (aeval_alg_hom_eq_zero f ϕ) = ϕ :=
begin
suffices : ϕ.equalizer (lift_hom f (ϕ (root f)) (aeval_alg_hom_eq_zero f ϕ)) = ⊤,
{ exact (alg_hom.ext (λ x, (set_like.ext_iff.mp (this) x).mpr algebra.mem_top)).symm },
rw [eq_top_iff, ←adjoin_root_eq_top, algebra.adjoin_le_iff, set.singleton_subset_iff],
exact (@lift_root _ _ _ _ _ _ _ (aeval_alg_hom_eq_zero f ϕ)).symm,
end
variables (hfx : aeval a f = 0)
@[simp] lemma lift_hom_mk {g : R[X]} : lift_hom f a hfx (mk f g) = aeval a g :=
lift_mk hfx g
@[simp] lemma lift_hom_root : lift_hom f a hfx (root f) = a :=
lift_root hfx
@[simp] lemma lift_hom_of {x : R} : lift_hom f a hfx (of f x) = algebra_map _ _ x :=
lift_of hfx
section adjoin_inv
@[simp] lemma root_is_inv (r : R) : of _ r * root (C r * X - 1) = 1 :=
by convert sub_eq_zero.1 ((eval₂_sub _).symm.trans $ eval₂_root $ C r * X - 1);
simp only [eval₂_mul, eval₂_C, eval₂_X, eval₂_one]
lemma alg_hom_subsingleton {S : Type*} [comm_ring S] [algebra R S] {r : R} :
subsingleton (adjoin_root (C r * X - 1) →ₐ[R] S) :=
⟨λ f g, alg_hom_ext (@inv_unique _ _ (algebra_map R S r) _ _
(by rw [← f.commutes, ← f.map_mul, algebra_map_eq, root_is_inv, map_one])
(by rw [← g.commutes, ← g.map_mul, algebra_map_eq, root_is_inv, map_one]))⟩
end adjoin_inv
end comm_ring
section irreducible
variables [field K] {f : K[X]}
instance span_maximal_of_irreducible [fact (irreducible f)] : (span {f}).is_maximal :=
principal_ideal_ring.is_maximal_of_irreducible $ fact.out _
noncomputable instance field [fact (irreducible f)] : field (adjoin_root f) :=
{ ..adjoin_root.comm_ring f,
..ideal.quotient.field (span {f} : ideal K[X]) }
lemma coe_injective (h : degree f ≠ 0) : function.injective (coe : K → adjoin_root f) :=
have _ := adjoin_root.nontrivial f h, by exactI (of f).injective
lemma coe_injective' [fact (irreducible f)] : function.injective (coe : K → adjoin_root f) :=
(of f).injective
variable (f)
lemma mul_div_root_cancel [fact (irreducible f)] :
((X - C (root f)) * (f.map (of f) / (X - C (root f)))) = f.map (of f) :=
mul_div_eq_iff_is_root.2 $ is_root_root _
end irreducible
section is_noetherian_ring
instance [comm_ring R] [is_noetherian_ring R] {f : R[X]} : is_noetherian_ring (adjoin_root f) :=
ideal.quotient.is_noetherian_ring _
end is_noetherian_ring
section power_basis
variables [comm_ring R] {g : R[X]}
lemma is_integral_root' (hg : g.monic) : is_integral R (root g) :=
⟨g, hg, eval₂_root g⟩
/-- `adjoin_root.mod_by_monic_hom` sends the equivalence class of `f` mod `g` to `f %ₘ g`.
This is a well-defined right inverse to `adjoin_root.mk`, see `adjoin_root.mk_left_inverse`. -/
def mod_by_monic_hom (hg : g.monic) :
adjoin_root g →ₗ[R] R[X] :=
(submodule.liftq _ (polynomial.mod_by_monic_hom g)
(λ f (hf : f ∈ (ideal.span {g}).restrict_scalars R),
(mem_ker_mod_by_monic hg).mpr (ideal.mem_span_singleton.mp hf))).comp $
(submodule.quotient.restrict_scalars_equiv R (ideal.span {g} : ideal R[X]))
.symm.to_linear_map
@[simp] lemma mod_by_monic_hom_mk (hg : g.monic) (f : R[X]) :
mod_by_monic_hom hg (mk g f) = f %ₘ g := rfl
lemma mk_left_inverse (hg : g.monic) :
function.left_inverse (mk g) (mod_by_monic_hom hg) :=
λ f, induction_on g f $ λ f, begin
rw [mod_by_monic_hom_mk hg, mk_eq_mk, mod_by_monic_eq_sub_mul_div _ hg,
sub_sub_cancel_left, dvd_neg],
apply dvd_mul_right
end
lemma mk_surjective (hg : g.monic) : function.surjective (mk g) :=
(mk_left_inverse hg).surjective
/-- The elements `1, root g, ..., root g ^ (d - 1)` form a basis for `adjoin_root g`,
where `g` is a monic polynomial of degree `d`. -/
def power_basis_aux' (hg : g.monic) :
basis (fin g.nat_degree) R (adjoin_root g) :=
basis.of_equiv_fun
{ to_fun := λ f i, (mod_by_monic_hom hg f).coeff i,
inv_fun := λ c, mk g $ ∑ (i : fin g.nat_degree), monomial i (c i),
map_add' := λ f₁ f₂, funext $ λ i,
by simp only [(mod_by_monic_hom hg).map_add, coeff_add, pi.add_apply],
map_smul' := λ f₁ f₂, funext $ λ i,
by simp only [(mod_by_monic_hom hg).map_smul, coeff_smul, pi.smul_apply, ring_hom.id_apply],
left_inv := λ f, induction_on g f (λ f, eq.symm $ mk_eq_mk.mpr $
by { simp only [mod_by_monic_hom_mk, sum_mod_by_monic_coeff hg degree_le_nat_degree],
rw [mod_by_monic_eq_sub_mul_div _ hg, sub_sub_cancel],
exact dvd_mul_right _ _ }),
right_inv := λ x, funext $ λ i, begin
nontriviality R,
simp only [mod_by_monic_hom_mk],
rw [(mod_by_monic_eq_self_iff hg).mpr, finset_sum_coeff, finset.sum_eq_single i];
try { simp only [coeff_monomial, eq_self_iff_true, if_true] },
{ intros j _ hj, exact if_neg (fin.coe_injective.ne hj) },
{ intros, have := finset.mem_univ i, contradiction },
{ refine (degree_sum_le _ _).trans_lt ((finset.sup_lt_iff _).mpr (λ j _, _)),
{ exact bot_lt_iff_ne_bot.mpr (mt degree_eq_bot.mp hg.ne_zero) },
{ refine (degree_monomial_le _ _).trans_lt _,
rw [degree_eq_nat_degree hg.ne_zero, with_bot.coe_lt_coe],
exact j.2 } },
end}
-- This was moved after the definition to prevent a timeout
attribute [simps] power_basis_aux'
/-- The power basis `1, root g, ..., root g ^ (d - 1)` for `adjoin_root g`,
where `g` is a monic polynomial of degree `d`. -/
@[simps] def power_basis' (hg : g.monic) : power_basis R (adjoin_root g) :=
{ gen := root g,
dim := g.nat_degree,
basis := power_basis_aux' hg,
basis_eq_pow := λ i, begin
simp only [power_basis_aux', basis.coe_of_equiv_fun, linear_equiv.coe_symm_mk],
rw finset.sum_eq_single i,
{ rw [function.update_same, monomial_one_right_eq_X_pow, (mk g).map_pow, mk_X] },
{ intros j _ hj,
rw ← monomial_zero_right _,
convert congr_arg _ (function.update_noteq hj _ _) }, -- Fix `decidable_eq` mismatch
{ intros, have := finset.mem_univ i, contradiction },
end}
variables [field K] {f : K[X]}
lemma is_integral_root (hf : f ≠ 0) : is_integral K (root f) :=
is_algebraic_iff_is_integral.mp (is_algebraic_root hf)
lemma minpoly_root (hf : f ≠ 0) : minpoly K (root f) = f * C (f.leading_coeff⁻¹) :=
begin
have f'_monic : monic _ := monic_mul_leading_coeff_inv hf,
refine (minpoly.unique K _ f'_monic _ _).symm,
{ rw [alg_hom.map_mul, aeval_eq, mk_self, zero_mul] },
intros q q_monic q_aeval,
have commutes : (lift (algebra_map K (adjoin_root f)) (root f) q_aeval).comp (mk q) = mk f,
{ ext,
{ simp only [ring_hom.comp_apply, mk_C, lift_of], refl },
{ simp only [ring_hom.comp_apply, mk_X, lift_root] } },
rw [degree_eq_nat_degree f'_monic.ne_zero, degree_eq_nat_degree q_monic.ne_zero,
with_bot.coe_le_coe, nat_degree_mul hf, nat_degree_C, add_zero],
apply nat_degree_le_of_dvd,
{ have : mk f q = 0, by rw [←commutes, ring_hom.comp_apply, mk_self, ring_hom.map_zero],
rwa [←ideal.mem_span_singleton, ←ideal.quotient.eq_zero_iff_mem] },
{ exact q_monic.ne_zero },
{ rwa [ne.def, C_eq_zero, inv_eq_zero, leading_coeff_eq_zero] },
end
/-- The elements `1, root f, ..., root f ^ (d - 1)` form a basis for `adjoin_root f`,
where `f` is an irreducible polynomial over a field of degree `d`. -/
def power_basis_aux (hf : f ≠ 0) : basis (fin f.nat_degree) K (adjoin_root f) :=
begin
set f' := f * C (f.leading_coeff⁻¹) with f'_def,
have deg_f' : f'.nat_degree = f.nat_degree,
{ rw [nat_degree_mul hf, nat_degree_C, add_zero],
{ rwa [ne.def, C_eq_zero, inv_eq_zero, leading_coeff_eq_zero] } },
have minpoly_eq : minpoly K (root f) = f' := minpoly_root hf,
apply @basis.mk _ _ _ (λ (i : fin f.nat_degree), (root f ^ i.val)),
{ rw [← deg_f', ← minpoly_eq],
exact (is_integral_root hf).linear_independent_pow },
{ rintros y -,
rw [← deg_f', ← minpoly_eq],
apply (is_integral_root hf).mem_span_pow,
obtain ⟨g⟩ := y,
use g,
rw aeval_eq,
refl }
end
/-- The power basis `1, root f, ..., root f ^ (d - 1)` for `adjoin_root f`,
where `f` is an irreducible polynomial over a field of degree `d`. -/
@[simps] def power_basis (hf : f ≠ 0) :
power_basis K (adjoin_root f) :=
{ gen := root f,
dim := f.nat_degree,
basis := power_basis_aux hf,
basis_eq_pow := basis.mk_apply _ _ }
lemma minpoly_power_basis_gen (hf : f ≠ 0) :
minpoly K (power_basis hf).gen = f * C (f.leading_coeff⁻¹) :=
by rw [power_basis_gen, minpoly_root hf]
lemma minpoly_power_basis_gen_of_monic (hf : f.monic) (hf' : f ≠ 0 := hf.ne_zero) :
minpoly K (power_basis hf').gen = f :=
by rw [minpoly_power_basis_gen hf', hf.leading_coeff, inv_one, C.map_one, mul_one]
end power_basis
section equiv
section minpoly
variables [comm_ring R] [comm_ring S] [algebra R S] (x : S) (R)
open algebra polynomial
/-- The surjective algebra morphism `R[X]/(minpoly R x) → R[x]`.
If `R` is a GCD domain and `x` is integral, this is an isomorphism,
see `adjoin_root.minpoly.equiv_adjoin`. -/
@[simps] def minpoly.to_adjoin : adjoin_root (minpoly R x) →ₐ[R] adjoin R ({x} : set S) :=
lift_hom _ ⟨x, self_mem_adjoin_singleton R x⟩
(by simp [← subalgebra.coe_eq_zero, aeval_subalgebra_coe])
variables {R x}
lemma minpoly.to_adjoin_apply' (a : adjoin_root (minpoly R x)) : minpoly.to_adjoin R x a =
lift_hom (minpoly R x) (⟨x, self_mem_adjoin_singleton R x⟩ : adjoin R ({x} : set S))
(by simp [← subalgebra.coe_eq_zero, aeval_subalgebra_coe]) a := rfl
lemma minpoly.to_adjoin.apply_X : minpoly.to_adjoin R x (mk (minpoly R x) X) =
⟨x, self_mem_adjoin_singleton R x⟩ :=
by simp
variables (R x)
lemma minpoly.to_adjoin.surjective : function.surjective (minpoly.to_adjoin R x) :=
begin
rw [← range_top_iff_surjective, _root_.eq_top_iff, ← adjoin_adjoin_coe_preimage],
refine adjoin_le _,
simp only [alg_hom.coe_range, set.mem_range],
rintro ⟨y₁, y₂⟩ h,
refine ⟨mk (minpoly R x) X, by simpa using h.symm⟩
end
end minpoly
section is_domain
variables [comm_ring R] [is_domain R] [comm_ring S] [is_domain S] [algebra R S]
variables (g : R[X]) (pb : _root_.power_basis R S)
/-- If `S` is an extension of `R` with power basis `pb` and `g` is a monic polynomial over `R`
such that `pb.gen` has a minimal polynomial `g`, then `S` is isomorphic to `adjoin_root g`.
Compare `power_basis.equiv_of_root`, which would require
`h₂ : aeval pb.gen (minpoly R (root g)) = 0`; that minimal polynomial is not
guaranteed to be identical to `g`. -/
@[simps {fully_applied := ff}]
def equiv' (h₁ : aeval (root g) (minpoly R pb.gen) = 0) (h₂ : aeval pb.gen g = 0) :
adjoin_root g ≃ₐ[R] S :=
{ to_fun := adjoin_root.lift_hom g pb.gen h₂,
inv_fun := pb.lift (root g) h₁,
left_inv := λ x, induction_on g x $ λ f, by rw [lift_hom_mk, pb.lift_aeval, aeval_eq],
right_inv := λ x, begin
obtain ⟨f, hf, rfl⟩ := pb.exists_eq_aeval x,
rw [pb.lift_aeval, aeval_eq, lift_hom_mk]
end,
.. adjoin_root.lift_hom g pb.gen h₂ }
@[simp] lemma equiv'_to_alg_hom
(h₁ : aeval (root g) (minpoly R pb.gen) = 0) (h₂ : aeval pb.gen g = 0) :
(equiv' g pb h₁ h₂).to_alg_hom = adjoin_root.lift_hom g pb.gen h₂ :=
rfl
@[simp] lemma equiv'_symm_to_alg_hom
(h₁ : aeval (root g) (minpoly R pb.gen) = 0) (h₂ : aeval pb.gen g = 0) :
(equiv' g pb h₁ h₂).symm.to_alg_hom = pb.lift (root g) h₁ :=
rfl
end is_domain
section field
variables (K) (L F : Type*) [field F] [field K] [field L] [algebra F K] [algebra F L]
variables (pb : _root_.power_basis F K)
/-- If `L` is a field extension of `F` and `f` is a polynomial over `F` then the set
of maps from `F[x]/(f)` into `L` is in bijection with the set of roots of `f` in `L`. -/
def equiv (f : F[X]) (hf : f ≠ 0) :
(adjoin_root f →ₐ[F] L) ≃ {x // x ∈ (f.map (algebra_map F L)).roots} :=
(power_basis hf).lift_equiv'.trans ((equiv.refl _).subtype_equiv (λ x,
begin
rw [power_basis_gen, minpoly_root hf, polynomial.map_mul, roots_mul,
polynomial.map_C, roots_C, add_zero, equiv.refl_apply],
{ rw ← polynomial.map_mul, exact map_monic_ne_zero (monic_mul_leading_coeff_inv hf) }
end))
end field
end equiv
section
open ideal double_quot polynomial
variables [comm_ring R] (I : ideal R) (f : R[X])
/-- The natural isomorphism `R[α]/(I[α]) ≅ R[α]/((I[x] ⊔ (f)) / (f))` for `α` a root of
`f : R[X]` and `I : ideal R`.
See `adjoin_root.quot_map_of_equiv` for the isomorphism with `(R/I)[X] / (f mod I)`. -/
def quot_map_of_equiv_quot_map_C_map_span_mk :
adjoin_root f ⧸ I.map (of f) ≃+*
adjoin_root f ⧸ (I.map (C : R →+* R[X])).map (span {f})^.quotient.mk :=
ideal.quot_equiv_of_eq (by rw [of, adjoin_root.mk, ideal.map_map])
@[simp]
lemma quot_map_of_equiv_quot_map_C_map_span_mk_mk (x : adjoin_root f) :
quot_map_of_equiv_quot_map_C_map_span_mk I f (ideal.quotient.mk (I.map (of f)) x) =
ideal.quotient.mk _ x :=
rfl
--this lemma should have the simp tag but this causes a lint issue
lemma quot_map_of_equiv_quot_map_C_map_span_mk_symm_mk (x : adjoin_root f) :
(quot_map_of_equiv_quot_map_C_map_span_mk I f).symm
(ideal.quotient.mk ((I.map (C : R →+* R[X])).map (span {f})^.quotient.mk) x) =
ideal.quotient.mk (I.map (of f)) x :=
by rw [quot_map_of_equiv_quot_map_C_map_span_mk, ideal.quot_equiv_of_eq_symm, quot_equiv_of_eq_mk]
/-- The natural isomorphism `R[α]/((I[x] ⊔ (f)) / (f)) ≅ (R[x]/I[x])/((f) ⊔ I[x] / I[x])`
for `α` a root of `f : R[X]` and `I : ideal R`-/
def quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk :
(adjoin_root f) ⧸ (I.map (C : R →+* R[X])).map (span ({f} : set R[X]))^.quotient.mk ≃+*
(R[X] ⧸ I.map (C : R →+* R[X])) ⧸ (span ({f} : set R[X])).map
(I.map (C : R →+* R[X]))^.quotient.mk :=
quot_quot_equiv_comm (ideal.span ({f} : set R[X])) (I.map (C : R →+* R[X]))
@[simp]
lemma quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk_mk (p : R[X]) :
quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk I f (ideal.quotient.mk _ (mk f p)) =
quot_quot_mk (I.map C) (span {f}) p :=
rfl
@[simp]
lemma quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk_symm_quot_quot_mk (p : R[X]) :
(quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk I f).symm
(quot_quot_mk (I.map C) (span {f}) p) = (ideal.quotient.mk _ (mk f p)) :=
rfl
/-- The natural isomorphism `(R/I)[x]/(f mod I) ≅ (R[x]/I*R[x])/(f mod I[x])` where
`f : R[X]` and `I : ideal R`-/
def polynomial.quot_quot_equiv_comm :
(R ⧸ I)[X] ⧸ span ({f.map (I^.quotient.mk)} : set (polynomial (R ⧸ I))) ≃+*
(R[X] ⧸ map C I) ⧸ span ({(ideal.quotient.mk (I.map C)) f} : set (R[X] ⧸ map C I)) :=
quotient_equiv (span ({f.map (I^.quotient.mk)} : set (polynomial (R ⧸ I))))
(span {ideal.quotient.mk (I.map polynomial.C) f})
(polynomial_quotient_equiv_quotient_polynomial I)
(by rw [map_span, set.image_singleton, ring_equiv.coe_to_ring_hom,
polynomial_quotient_equiv_quotient_polynomial_map_mk I f])
@[simp]
lemma polynomial.quot_quot_equiv_comm_mk (p : R[X]) :
(polynomial.quot_quot_equiv_comm I f) (ideal.quotient.mk _ (p.map I^.quotient.mk)) =
(ideal.quotient.mk _ (ideal.quotient.mk _ p)) :=
by simp only [polynomial.quot_quot_equiv_comm, quotient_equiv_mk,
polynomial_quotient_equiv_quotient_polynomial_map_mk]
@[simp]
lemma polynomial.quot_quot_equiv_comm_symm_mk_mk (p : R[X]) :
(polynomial.quot_quot_equiv_comm I f).symm (ideal.quotient.mk _ (ideal.quotient.mk _ p)) =
(ideal.quotient.mk _ (p.map I^.quotient.mk)) :=
by simp only [polynomial.quot_quot_equiv_comm, quotient_equiv_symm_mk,
polynomial_quotient_equiv_quotient_polynomial_symm_mk]
/-- The natural isomorphism `R[α]/I[α] ≅ (R/I)[X]/(f mod I)` for `α` a root of `f : R[X]`
and `I : ideal R`.-/
def quot_adjoin_root_equiv_quot_polynomial_quot : (adjoin_root f) ⧸ (I.map (of f)) ≃+*
(R ⧸ I)[X] ⧸ (span ({f.map (I^.quotient.mk)} : set (R ⧸ I)[X])) :=
(quot_map_of_equiv_quot_map_C_map_span_mk I f).trans
((quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk I f).trans
((ideal.quot_equiv_of_eq
(show (span ({f} : set R[X])).map (I.map (C : R →+* R[X]))^.quotient.mk =
span ({(ideal.quotient.mk (I.map polynomial.C)) f} : set (R[X] ⧸ map C I)),
from by rw [map_span, set.image_singleton])).trans
(polynomial.quot_quot_equiv_comm I f).symm))
@[simp]
lemma quot_adjoin_root_equiv_quot_polynomial_quot_mk_of (p : R[X]) :
quot_adjoin_root_equiv_quot_polynomial_quot I f (ideal.quotient.mk (I.map (of f)) (mk f p)) =
ideal.quotient.mk (span ({f.map (I^.quotient.mk)} : set (R ⧸ I)[X]))
(p.map I^.quotient.mk) :=
by rw [quot_adjoin_root_equiv_quot_polynomial_quot, ring_equiv.trans_apply, ring_equiv.trans_apply,
ring_equiv.trans_apply, quot_map_of_equiv_quot_map_C_map_span_mk_mk,
quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk_mk, quot_quot_mk, ring_hom.comp_apply,
quot_equiv_of_eq_mk, polynomial.quot_quot_equiv_comm_symm_mk_mk]
@[simp]
lemma quot_adjoin_root_equiv_quot_polynomial_quot_symm_mk_mk (p : R[X]) :
(quot_adjoin_root_equiv_quot_polynomial_quot I f).symm
(ideal.quotient.mk (span ({f.map (I^.quotient.mk)} : set (R ⧸ I)[X]))
(p.map I^.quotient.mk)) = (ideal.quotient.mk (I.map (of f)) (mk f p)) :=
by rw [quot_adjoin_root_equiv_quot_polynomial_quot, ring_equiv.symm_trans_apply,
ring_equiv.symm_trans_apply, ring_equiv.symm_trans_apply, ring_equiv.symm_symm,
polynomial.quot_quot_equiv_comm_mk, ideal.quot_equiv_of_eq_symm,
ideal.quot_equiv_of_eq_mk, ← ring_hom.comp_apply, ← double_quot.quot_quot_mk,
quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk_symm_quot_quot_mk,
quot_map_of_equiv_quot_map_C_map_span_mk_symm_mk]
/-- Promote `adjoin_root.quot_adjoin_root_equiv_quot_polynomial_quot` to an alg_equiv. -/
@[simps apply symm_apply]
noncomputable def quot_equiv_quot_map (f : R[X]) (I : ideal R) :
((adjoin_root f) ⧸ (ideal.map (of f) I)) ≃ₐ[R]
((R ⧸ I) [X]) ⧸ (ideal.span ({polynomial.map I^.quotient.mk f} : set ((R ⧸ I) [X]))) :=
alg_equiv.of_ring_equiv (show ∀ x, (quot_adjoin_root_equiv_quot_polynomial_quot I f)
(algebra_map R _ x) = algebra_map R _ x, from λ x, begin
have : algebra_map R ((adjoin_root f) ⧸ (ideal.map (of f) I)) x = ideal.quotient.mk
(ideal.map (adjoin_root.of f) I) ((mk f) (C x)) := rfl,
simpa only [this, quot_adjoin_root_equiv_quot_polynomial_quot_mk_of, map_C]
end)
@[simp]
lemma quot_equiv_quot_map_apply_mk (f g : R[X]) (I : ideal R) :
adjoin_root.quot_equiv_quot_map f I (ideal.quotient.mk _ (adjoin_root.mk f g)) =
ideal.quotient.mk _ (g.map I^.quotient.mk) :=
by rw [adjoin_root.quot_equiv_quot_map_apply,
adjoin_root.quot_adjoin_root_equiv_quot_polynomial_quot_mk_of]
@[simp]
lemma quot_equiv_quot_map_symm_apply_mk (f g : R[X]) (I : ideal R) :
(adjoin_root.quot_equiv_quot_map f I).symm (ideal.quotient.mk _ (map (ideal.quotient.mk I) g)) =
ideal.quotient.mk _ (adjoin_root.mk f g) :=
by rw [adjoin_root.quot_equiv_quot_map_symm_apply,
adjoin_root.quot_adjoin_root_equiv_quot_polynomial_quot_symm_mk_mk]
end
end adjoin_root
namespace power_basis
open adjoin_root alg_equiv
variables [comm_ring R] [is_domain R] [comm_ring S] [is_domain S] [algebra R S]
/-- Let `α` have minimal polynomial `f` over `R` and `I` be an ideal of `R`,
then `R[α] / (I) = (R[x] / (f)) / pS = (R/p)[x] / (f mod p)`. -/
@[simps apply symm_apply]
noncomputable def quotient_equiv_quotient_minpoly_map (pb : power_basis R S)
(I : ideal R) :
(S ⧸ I.map (algebra_map R S)) ≃ₐ[R] (polynomial (R ⧸ I)) ⧸
(ideal.span ({(minpoly R pb.gen).map I^.quotient.mk} : set (polynomial (R ⧸ I)))) :=
(of_ring_equiv
(show ∀ x, (ideal.quotient_equiv _ (ideal.map (adjoin_root.of (minpoly R pb.gen)) I)
(adjoin_root.equiv' (minpoly R pb.gen) pb
(by rw [adjoin_root.aeval_eq, adjoin_root.mk_self])
(minpoly.aeval _ _)).symm.to_ring_equiv
(by rw [ideal.map_map, alg_equiv.to_ring_equiv_eq_coe, ← alg_equiv.coe_ring_hom_commutes,
← adjoin_root.algebra_map_eq, alg_hom.comp_algebra_map]))
(algebra_map R (S ⧸ I.map (algebra_map R S)) x) = algebra_map R _ x, from
(λ x, by rw [← ideal.quotient.mk_algebra_map, ideal.quotient_equiv_apply,
ring_hom.to_fun_eq_coe, ideal.quotient_map_mk, alg_equiv.to_ring_equiv_eq_coe,
ring_equiv.coe_to_ring_hom, alg_equiv.coe_ring_equiv, alg_equiv.commutes,
quotient.mk_algebra_map]))).trans (adjoin_root.quot_equiv_quot_map _ _)
@[simp]
lemma quotient_equiv_quotient_minpoly_map_apply_mk (pb : power_basis R S) (I : ideal R)
(g : R[X]) : pb.quotient_equiv_quotient_minpoly_map I
(ideal.quotient.mk _ (aeval pb.gen g)) = ideal.quotient.mk _ (g.map I^.quotient.mk) :=
by rw [power_basis.quotient_equiv_quotient_minpoly_map, alg_equiv.trans_apply,
alg_equiv.of_ring_equiv_apply, quotient_equiv_mk, alg_equiv.coe_ring_equiv',
adjoin_root.equiv'_symm_apply, power_basis.lift_aeval,
adjoin_root.aeval_eq, adjoin_root.quot_equiv_quot_map_apply_mk]
@[simp]
lemma quotient_equiv_quotient_minpoly_map_symm_apply_mk (pb : power_basis R S) (I : ideal R)
(g : R[X]) : (pb.quotient_equiv_quotient_minpoly_map I).symm
(ideal.quotient.mk _ (g.map I^.quotient.mk)) = (ideal.quotient.mk _ (aeval pb.gen g)) :=
begin simp only [quotient_equiv_quotient_minpoly_map, to_ring_equiv_eq_coe, symm_trans_apply,
quot_equiv_quot_map_symm_apply_mk, of_ring_equiv_symm_apply, quotient_equiv_symm_mk,
to_ring_equiv_symm, ring_equiv.symm_symm, adjoin_root.equiv'_apply, coe_ring_equiv,
lift_hom_mk, symm_to_ring_equiv],
end
end power_basis
|
615c16a0a262b57b215ca6740b5f688b625ea78e | ee8cdbabf07f77e7be63a449b8483ce308d37218 | /lean/src/test/amc12a-2003-p5.lean | 8dc6117ef4cc3cc76974f54231ff0a6301fdbd16 | [
"MIT",
"Apache-2.0"
] | permissive | zeta1999/miniF2F | 6d66c75d1c18152e224d07d5eed57624f731d4b7 | c1ba9629559c5273c92ec226894baa0c1ce27861 | refs/heads/main | 1,681,897,460,642 | 1,620,646,361,000 | 1,620,646,361,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 363 | lean | /-
Copyright (c) 2021 OpenAI. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kunhao Zheng
-/
import data.nat.basic
example (a m c : ℕ) (h₀ : a ≤ 9 ∧ m ≤ 9 ∧ c ≤ 9) ( h₁ : 10*(10*(10*(10*a + m) + c) + 1) + 0 + (10*(10*(10*(10*a + m) + c) + 1) + 2) = 123422) : a + m + c = 14 :=
begin
sorry
end
|
33433390bece6ac5535924f7b345e338c91ea3f6 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/handthen.lean | c73939cc544ecb39fc37e765dc2efdeebd872945 | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 1,009 | lean | open tactic
local attribute [simp] nat.add_zero nat.zero_add
lemma ex1 (a b c : nat) : a + 0 = 0 + a ∧ b = b :=
begin
-- We use `(` to go to regular tactic mode.
constructor; [skip, constructor],
-- Remaining goal is
-- |- a + 0 = 0 + a
simp
end
lemma ex2 (a b c : nat) : a + 0 = 0 + a ∧ b = b :=
begin
constructor; [skip, constructor],
simp
end
lemma ex3 (a b c : nat) : a + 0 = 0 + a ∧ b = b :=
begin
/- We can use {} to group a sequence of tactics in the
tac ; [tac_1, ..., tac_n]
notation.
However, a {} will not force the goal to be completely solved.
Example:
The first constructor tactic will produce two goals.
The `;` combinator will apply the tactics {trace "Case1: ", trace_state} to the first goal and
constructor to the second.
-/
constructor; [{trace "Case1: ", trace_state}, constructor],
simp
end
lemma ex4 (a : nat) : a = a :=
begin
/- We can use tac;[] to make sure that tac did not produce any goal -/
refl; []
end
|
212172fbe3ddc452113cd90749881cd91c120a8d | 947b78d97130d56365ae2ec264df196ce769371a | /stage0/src/Lean/Level.lean | 3589a3c6b85620f67412485dd2995197ce114604 | [
"Apache-2.0"
] | permissive | shyamalschandra/lean4 | 27044812be8698f0c79147615b1d5090b9f4b037 | 6e7a883b21eaf62831e8111b251dc9b18f40e604 | refs/heads/master | 1,671,417,126,371 | 1,601,859,995,000 | 1,601,860,020,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 16,481 | 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
-/
import Std.Data.HashMap
import Std.Data.HashSet
import Std.Data.PersistentHashMap
import Std.Data.PersistentHashSet
import Lean.Data.Name
import Lean.Data.Format
def Nat.imax (n m : Nat) : Nat :=
if m = 0 then 0 else Nat.max n m
namespace Lean
/--
Cached hash code, cached results, and other data for `Level`.
hash : 32-bits
hasMVar : 1-bit
hasParam : 1-bit
depth : 24-bits -/
def Level.Data := UInt64
instance Level.Data.inhabited : Inhabited Level.Data :=
inferInstanceAs (Inhabited UInt64)
def Level.Data.hash (c : Level.Data) : USize :=
c.toUInt32.toUSize
instance Level.Data.hasBeq : HasBeq Level.Data :=
⟨fun (a b : UInt64) => a == b⟩
def Level.Data.depth (c : Level.Data) : UInt32 :=
(c.shiftRight 40).toUInt32
def Level.Data.hasMVar (c : Level.Data) : Bool :=
((c.shiftRight 32).land 1) == 1
def Level.Data.hasParam (c : Level.Data) : Bool :=
((c.shiftRight 33).land 1) == 1
def Level.mkData (h : USize) (depth : Nat) (hasMVar hasParam : Bool) : Level.Data :=
if depth > Nat.pow 2 24 - 1 then panic! "universe level depth is too big"
else
let r : UInt64 := h.toUInt32.toUInt64 + hasMVar.toUInt64.shiftLeft 32 + hasParam.toUInt64.shiftLeft 33 + depth.toUInt64.shiftLeft 40;
r
open Level
inductive Level
| zero : Data → Level
| succ : Level → Data → Level
| max : Level → Level → Data → Level
| imax : Level → Level → Data → Level
| param : Name → Data → Level
| mvar : Name → Data → Level
namespace Level
@[inline] def data : Level → Data
| zero d => d
| mvar _ d => d
| param _ d => d
| succ _ d => d
| max _ _ d => d
| imax _ _ d => d
def hash (u : Level) : USize :=
u.data.hash
instance : Hashable Level := ⟨hash⟩
def depth (u : Level) : Nat :=
u.data.depth.toNat
def hasMVar (u : Level) : Bool :=
u.data.hasMVar
def hasParam (u : Level) : Bool :=
u.data.hasParam
@[export lean_level_hash] def hashEx : Level → USize := hash
@[export lean_level_has_mvar] def hasMVarEx : Level → Bool := hasMVar
@[export lean_level_has_param] def hasParamEx : Level → Bool := hasParam
@[export lean_level_depth] def depthEx (u : Level) : UInt32 := u.data.depth
end Level
def levelZero :=
Level.zero $ mkData 2221 0 false false
def mkLevelMVar (mvarId : Name) :=
Level.mvar mvarId $ mkData (mixHash 2237 $ hash mvarId) 0 true false
def mkLevelParam (name : Name) :=
Level.param name $ mkData (mixHash 2239 $ hash name) 0 false true
def mkLevelSucc (u : Level) :=
Level.succ u $ mkData (mixHash 2243 $ hash u) (u.depth + 1) u.hasMVar u.hasParam
def mkLevelMax (u v : Level) :=
Level.max u v $ mkData (mixHash 2251 $ mixHash (hash u) (hash v)) (Nat.max u.depth v.depth + 1)
(u.hasMVar || v.hasMVar)
(u.hasParam || v.hasParam)
def mkLevelIMax (u v : Level) :=
Level.imax u v $ mkData (mixHash 2267 $ mixHash (hash u) (hash v)) (Nat.max u.depth v.depth + 1)
(u.hasMVar || v.hasMVar)
(u.hasParam || v.hasParam)
def levelOne := mkLevelSucc levelZero
@[export lean_level_mk_zero] def mkLevelZeroEx : Unit → Level := fun _ => levelZero
@[export lean_level_mk_succ] def mkLevelSuccEx : Level → Level := mkLevelSucc
@[export lean_level_mk_mvar] def mkLevelMVarEx : Name → Level := mkLevelMVar
@[export lean_level_mk_param] def mkLevelParamEx : Name → Level := mkLevelParam
@[export lean_level_mk_max] def mkLevelMaxEx : Level → Level → Level := mkLevelMax
@[export lean_level_mk_imax] def mkLevelIMaxEx : Level → Level → Level := mkLevelIMax
namespace Level
instance : Inhabited Level := ⟨levelZero⟩
def isZero : Level → Bool
| zero _ => true
| _ => false
def isSucc : Level → Bool
| succ _ _ => true
| _ => false
def isMax : Level → Bool
| max _ _ _ => true
| _ => false
def isIMax : Level → Bool
| imax _ _ _ => true
| _ => false
def isMaxIMax : Level → Bool
| max _ _ _ => true
| imax _ _ _ => true
| _ => false
def isParam : Level → Bool
| param _ _ => true
| _ => false
def isMVar : Level → Bool
| mvar _ _ => true
| _ => false
def mvarId! : Level → Name
| mvar mvarId _ => mvarId
| _ => panic! "metavariable expected"
/-- If result is true, then forall assignments `A` which assigns all parameters and metavariables occuring
in `l`, `l[A] != zero` -/
def isNeverZero : Level → Bool
| zero _ => false
| param _ _ => false
| mvar _ _ => false
| succ _ _ => true
| max l₁ l₂ _ => isNeverZero l₁ || isNeverZero l₂
| imax l₁ l₂ _ => isNeverZero l₂
def ofNat : Nat → Level
| 0 => levelZero
| n+1 => mkLevelSucc (ofNat n)
def addOffsetAux : Nat → Level → Level
| 0, u => u
| (n+1), u => addOffsetAux n (mkLevelSucc u)
def addOffset (u : Level) (n : Nat) : Level :=
u.addOffsetAux n
def isExplicit : Level → Bool
| zero _ => true
| succ u _ => !u.hasMVar && !u.hasParam && isExplicit u
| _ => false
def getOffsetAux : Level → Nat → Nat
| succ u _, r => getOffsetAux u (r+1)
| _, r => r
def getOffset (lvl : Level) : Nat :=
getOffsetAux lvl 0
def getLevelOffset : Level → Level
| succ u _ => getLevelOffset u
| u => u
def toNat (lvl : Level) : Option Nat :=
match lvl.getLevelOffset with
| zero _ => lvl.getOffset
| _ => none
@[extern "lean_level_eq"]
protected constant beq (a : @& Level) (b : @& Level) : Bool := arbitrary _
instance : HasBeq Level := ⟨Level.beq⟩
/-- `occurs u l` return `true` iff `u` occurs in `l`. -/
def occurs : Level → Level → Bool
| u, v@(succ v₁ _) => u == v || occurs u v₁
| u, v@(max v₁ v₂ _) => u == v || occurs u v₁ || occurs u v₂
| u, v@(imax v₁ v₂ _) => u == v || occurs u v₁ || occurs u v₂
| u, v => u == v
def ctorToNat : Level → Nat
| zero _ => 0
| param _ _ => 1
| mvar _ _ => 2
| succ _ _ => 3
| max _ _ _ => 4
| imax _ _ _ => 5
/- TODO: use well founded recursion. -/
partial def normLtAux : Level → Nat → Level → Nat → Bool
| succ l₁ _, k₁, l₂, k₂ => normLtAux l₁ (k₁+1) l₂ k₂
| l₁, k₁, succ l₂ _, k₂ => normLtAux l₁ k₁ l₂ (k₂+1)
| l₁@(max l₁₁ l₁₂ _), k₁, l₂@(max l₂₁ l₂₂ _), k₂ =>
if l₁ == l₂ then k₁ < k₂
else if l₁₁ == l₂₁ then normLtAux l₁₁ 0 l₂₁ 0
else normLtAux l₁₂ 0 l₂₂ 0
| l₁@(imax l₁₁ l₁₂ _), k₁, l₂@(imax l₂₁ l₂₂ _), k₂ =>
if l₁ == l₂ then k₁ < k₂
else if l₁₁ == l₂₁ then normLtAux l₁₁ 0 l₂₁ 0
else normLtAux l₁₂ 0 l₂₂ 0
| param n₁ _, k₁, param n₂ _, k₂ => if n₁ == n₂ then k₁ < k₂ else Name.lt n₁ n₂ -- use Name.lt because it is lexicographical
| mvar n₁ _, k₁, mvar n₂ _, k₂ => if n₁ == n₂ then k₁ < k₂ else Name.quickLt n₁ n₂ -- metavariables are temporary, the actual order doesn't matter
| l₁, k₁, l₂, k₂ => if l₁ == l₂ then k₁ < k₂ else ctorToNat l₁ < ctorToNat l₂
/--
A total order on level expressions that has the following properties
- `succ l` is an immediate successor of `l`.
- `zero` is the minimal element.
This total order is used in the normalization procedure. -/
def normLt (l₁ l₂ : Level) : Bool :=
normLtAux l₁ 0 l₂ 0
private def isAlreadyNormalizedCheap : Level → Bool
| zero _ => true
| param _ _ => true
| mvar _ _ => true
| succ u _ => isAlreadyNormalizedCheap u
| _ => false
/- Auxiliary function used at `normalize` -/
private def mkIMaxAux : Level → Level → Level
| _, u@(zero _) => u
| zero _, u => u
| u₁, u₂ => if u₁ == u₂ then u₁ else mkLevelIMax u₁ u₂
/- Auxiliary function used at `normalize` -/
@[specialize] private partial def getMaxArgsAux (normalize : Level → Level) : Level → Bool → Array Level → Array Level
| max l₁ l₂ _, alreadyNormalized, lvls => getMaxArgsAux l₂ alreadyNormalized (getMaxArgsAux l₁ alreadyNormalized lvls)
| l, false, lvls => getMaxArgsAux (normalize l) true lvls
| l, true, lvls => lvls.push l
private def accMax (result : Level) (prev : Level) (offset : Nat) : Level :=
if result.isZero then prev.addOffset offset
else mkLevelMax result (prev.addOffset offset)
/- Auxiliary function used at `normalize`.
Remarks:
- `lvls` are sorted using `normLt`
- `extraK` is the outter offset of the `max` term. We will push it inside.
- `i` is the current array index
- `prev + prevK` is the "previous" level that has not been added to `result` yet.
- `result` is the accumulator
-/
private partial def mkMaxAux (lvls : Array Level) (extraK : Nat) : Nat → Level → Nat → Level → Level
| i, prev, prevK, result =>
if h : i < lvls.size then
let lvl := lvls.get ⟨i, h⟩;
let curr := lvl.getLevelOffset;
let currK := lvl.getOffset;
if curr == prev then
mkMaxAux (i+1) curr currK result
else
mkMaxAux (i+1) curr currK (accMax result prev (extraK + prevK))
else
accMax result prev (extraK + prevK)
/-
Auxiliary function for `normalize`. It assumes `lvls` has been sorted using `normLt`.
It finds the first position that is not an explicit universe. -/
private partial def skipExplicit (lvls : Array Level) : Nat → Nat
| i =>
if h : i < lvls.size then
let lvl := lvls.get ⟨i, h⟩;
if lvl.getLevelOffset.isZero then skipExplicit (i+1) else i
else
i
/-
Auxiliary function for `normalize`.
`maxExplicit` is the maximum explicit universe level at `lvls`.
Return true if it finds a level with offset ≥ maxExplicit.
`i` starts at the first non explict level.
It assumes `lvls` has been sorted using `normLt`. -/
private partial def isExplicitSubsumedAux (lvls : Array Level) (maxExplicit : Nat) : Nat → Bool
| i =>
if h : i < lvls.size then
let lvl := lvls.get ⟨i, h⟩;
if lvl.getOffset ≥ maxExplicit then true
else isExplicitSubsumedAux (i+1)
else
false
/- Auxiliary function for `normalize`. See `isExplicitSubsumedAux` -/
private def isExplicitSubsumed (lvls : Array Level) (firstNonExplicit : Nat) : Bool :=
if firstNonExplicit == 0 then false
else
let max := (lvls.get! (firstNonExplicit - 1)).getOffset;
isExplicitSubsumedAux lvls max firstNonExplicit
partial def normalize : Level → Level
| l =>
if isAlreadyNormalizedCheap l then l
else
let k := l.getOffset;
let u := l.getLevelOffset;
match u with
| max l₁ l₂ _ =>
let lvls := getMaxArgsAux normalize l₁ false #[];
let lvls := getMaxArgsAux normalize l₂ false lvls;
let lvls := lvls.qsort normLt;
let firstNonExplicit := skipExplicit lvls 0;
let i := if isExplicitSubsumed lvls firstNonExplicit then firstNonExplicit else firstNonExplicit - 1;
let lvl₁ := lvls.get! i;
let prev := lvl₁.getLevelOffset;
let prevK := lvl₁.getOffset;
mkMaxAux lvls k (i+1) prev prevK levelZero
| imax l₁ l₂ _ =>
if l₂.isNeverZero then addOffset (normalize (mkLevelMax l₁ l₂)) k
else
let l₁ := normalize l₁;
let l₂ := normalize l₂;
addOffset (mkIMaxAux l₁ l₂) k
| _ => unreachable!
/- Return true if `u` and `v` denote the same level.
Check is currently incomplete. -/
def isEquiv (u v : Level) : Bool :=
u == v || u.normalize == v.normalize
/-- Reduce (if possible) universe level by 1 -/
def dec : Level → Option Level
| zero _ => none
| param _ _ => none
| mvar _ _ => none
| succ l _ => l
| max l₁ l₂ _ => mkLevelMax <$> dec l₁ <*> dec l₂
/- Remark: `mkLevelMax` in the following line is not a typo.
If `dec l₂` succeeds, then `imax l₁ l₂` is equivalent to `max l₁ l₂`. -/
| imax l₁ l₂ _ => mkLevelMax <$> dec l₁ <*> dec l₂
/- Level to Format -/
namespace LevelToFormat
inductive Result
| leaf : Format → Result
| num : Nat → Result
| offset : Result → Nat → Result
| maxNode : List Result → Result
| imaxNode : List Result → Result
def Result.succ : Result → Result
| Result.offset f k => Result.offset f (k+1)
| Result.num k => Result.num (k+1)
| f => Result.offset f 1
def Result.max : Result → Result → Result
| f, Result.maxNode Fs => Result.maxNode (f::Fs)
| f₁, f₂ => Result.maxNode [f₁, f₂]
def Result.imax : Result → Result → Result
| f, Result.imaxNode Fs => Result.imaxNode (f::Fs)
| f₁, f₂ => Result.imaxNode [f₁, f₂]
def parenIfFalse : Format → Bool → Format
| f, true => f
| f, false => f.paren
@[specialize] private def formatLst (fmt : Result → Format) : List Result → Format
| [] => Format.nil
| r::rs => Format.line ++ fmt r ++ formatLst rs
partial def Result.format : Result → Bool → Format
| Result.leaf f, _ => f
| Result.num k, _ => toString k
| Result.offset f 0, r => Result.format f r
| Result.offset f (k+1), r =>
let f' := Result.format f false;
parenIfFalse (f' ++ "+" ++ fmt (k+1)) r
| Result.maxNode fs, r => parenIfFalse (Format.group $ "max" ++ formatLst (fun r => Result.format r false) fs) r
| Result.imaxNode fs, r => parenIfFalse (Format.group $ "imax" ++ formatLst (fun r => Result.format r false) fs) r
def toResult : Level → Result
| zero _ => Result.num 0
| succ l _ => Result.succ (toResult l)
| max l₁ l₂ _ => Result.max (toResult l₁) (toResult l₂)
| imax l₁ l₂ _ => Result.imax (toResult l₁) (toResult l₂)
| param n _ => Result.leaf (fmt n)
| mvar n _ => Result.leaf (fmt n)
end LevelToFormat
protected def format (l : Level) : Format :=
(LevelToFormat.toResult l).format true
instance : HasFormat Level := ⟨Level.format⟩
instance : HasToString Level := ⟨Format.pretty ∘ Level.format⟩
/- The update functions here are defined using C code. They will try to avoid
allocating new values using pointer equality.
The hypotheses `(h : e.is... = true)` are used to ensure Lean will not crash
at runtime.
The `update*!` functions are inlined and provide a convenient way of using the
update proofs without providing proofs.
Note that if they are used under a match-expression, the compiler will eliminate
the double-match. -/
@[extern "lean_level_update_succ"]
def updateSucc (lvl : Level) (newLvl : Level) (h : lvl.isSucc = true) : Level :=
mkLevelSucc newLvl
@[inline] def updateSucc! (lvl : Level) (newLvl : Level) : Level :=
match lvl with
| succ lvl d => updateSucc (succ lvl d) newLvl rfl
| _ => panic! "succ level expected"
@[extern "lean_level_update_max"]
def updateMax (lvl : Level) (newLhs : Level) (newRhs : Level) (h : lvl.isMax = true) : Level :=
mkLevelMax newLhs newRhs
@[inline] def updateMax! (lvl : Level) (newLhs : Level) (newRhs : Level) : Level :=
match lvl with
| max lhs rhs d => updateMax (max lhs rhs d) newLhs newRhs rfl
| _ => panic! "max level expected"
@[extern "lean_level_update_imax"]
def updateIMax (lvl : Level) (newLhs : Level) (newRhs : Level) (h : lvl.isIMax = true) : Level :=
mkLevelIMax newLhs newRhs
@[inline] def updateIMax! (lvl : Level) (newLhs : Level) (newRhs : Level) : Level :=
match lvl with
| imax lhs rhs d => updateIMax (imax lhs rhs d) newLhs newRhs rfl
| _ => panic! "imax level expected"
def mkNaryMax : List Level → Level
| [] => levelZero
| [u] => u
| u::us => mkLevelMax u (mkNaryMax us)
/- Level to Format -/
@[specialize] def instantiateParams (s : Name → Option Level) : Level → Level
| u@(zero _) => u
| u@(succ v _) => if u.hasParam then u.updateSucc (instantiateParams v) rfl else u
| u@(max v₁ v₂ _) => if u.hasParam then u.updateMax (instantiateParams v₁) (instantiateParams v₂) rfl else u
| u@(imax v₁ v₂ _) => if u.hasParam then u.updateIMax (instantiateParams v₁) (instantiateParams v₂) rfl else u
| u@(param n _) => match s n with
| some u' => u'
| none => u
| u => u
end Level
open Std (HashMap HashSet PHashMap PHashSet)
abbrev LevelMap (α : Type) := HashMap Level α
abbrev PersistentLevelMap (α : Type) := PHashMap Level α
abbrev LevelSet := HashSet Level
abbrev PersistentLevelSet := PHashSet Level
abbrev PLevelSet := PersistentLevelSet
end Lean
abbrev Nat.toLevel (n : Nat) : Lean.Level :=
Lean.Level.ofNat n
|
9bdd7ea58b04097c5de22bc3f9644cddaa31d0a3 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/order/monoid/min_max.lean | eebcdd00179a03ebb740506a83dcb673373c3602 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 4,020 | lean | /-
Copyright (c) 2016 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl
-/
import order.min_max
import algebra.order.monoid.lemmas
/-!
# Lemmas about `min` and `max` in an ordered monoid.
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
-/
open function
variables {α β : Type*}
/-! Some lemmas about types that have an ordering and a binary operation, with no
rules relating them. -/
@[to_additive]
lemma fn_min_mul_fn_max [linear_order α] [comm_semigroup β] (f : α → β) (n m : α) :
f (min n m) * f (max n m) = f n * f m :=
by { cases le_total n m with h h; simp [h, mul_comm] }
@[to_additive]
lemma min_mul_max [linear_order α] [comm_semigroup α] (n m : α) :
min n m * max n m = n * m :=
fn_min_mul_fn_max id n m
section covariant_class_mul_le
variables [linear_order α]
section has_mul
variable [has_mul α]
section left
variable [covariant_class α α (*) (≤)]
@[to_additive] lemma min_mul_mul_left (a b c : α) : min (a * b) (a * c) = a * min b c :=
(monotone_id.const_mul' a).map_min.symm
@[to_additive]
lemma max_mul_mul_left (a b c : α) : max (a * b) (a * c) = a * max b c :=
(monotone_id.const_mul' a).map_max.symm
end left
section right
variable [covariant_class α α (function.swap (*)) (≤)]
@[to_additive]
lemma min_mul_mul_right (a b c : α) : min (a * c) (b * c) = min a b * c :=
(monotone_id.mul_const' c).map_min.symm
@[to_additive]
lemma max_mul_mul_right (a b c : α) : max (a * c) (b * c) = max a b * c :=
(monotone_id.mul_const' c).map_max.symm
end right
@[to_additive] lemma lt_or_lt_of_mul_lt_mul [covariant_class α α (*) (≤)]
[covariant_class α α (swap (*)) (≤)] {a₁ a₂ b₁ b₂ : α} :
a₁ * b₁ < a₂ * b₂ → a₁ < a₂ ∨ b₁ < b₂ :=
by { contrapose!, exact λ h, mul_le_mul' h.1 h.2 }
@[to_additive] lemma le_or_lt_of_mul_le_mul [covariant_class α α (*) (≤)]
[covariant_class α α (swap (*)) (<)] {a₁ a₂ b₁ b₂ : α} :
a₁ * b₁ ≤ a₂ * b₂ → a₁ ≤ a₂ ∨ b₁ < b₂ :=
by { contrapose!, exact λ h, mul_lt_mul_of_lt_of_le h.1 h.2 }
@[to_additive] lemma lt_or_le_of_mul_le_mul [covariant_class α α (*) (<)]
[covariant_class α α (swap (*)) (≤)] {a₁ a₂ b₁ b₂ : α} :
a₁ * b₁ ≤ a₂ * b₂ → a₁ < a₂ ∨ b₁ ≤ b₂ :=
by { contrapose!, exact λ h, mul_lt_mul_of_le_of_lt h.1 h.2 }
@[to_additive] lemma le_or_le_of_mul_le_mul [covariant_class α α (*) (<)]
[covariant_class α α (swap (*)) (<)] {a₁ a₂ b₁ b₂ : α} :
a₁ * b₁ ≤ a₂ * b₂ → a₁ ≤ a₂ ∨ b₁ ≤ b₂ :=
by { contrapose!, exact λ h, mul_lt_mul_of_lt_of_lt h.1 h.2 }
@[to_additive] lemma mul_lt_mul_iff_of_le_of_le [covariant_class α α (*) (≤)]
[covariant_class α α (swap (*)) (≤)] [covariant_class α α (*) (<)]
[covariant_class α α (swap (*)) (<)] {a₁ a₂ b₁ b₂ : α} (ha : a₁ ≤ a₂) (hb : b₁ ≤ b₂) :
a₁ * b₁ < a₂ * b₂ ↔ a₁ < a₂ ∨ b₁ < b₂ :=
begin
refine ⟨lt_or_lt_of_mul_lt_mul, _⟩,
rintro (ha | hb),
{ exact mul_lt_mul_of_lt_of_le ha hb },
{ exact mul_lt_mul_of_le_of_lt ha hb }
end
end has_mul
variable [mul_one_class α]
@[to_additive]
lemma min_le_mul_of_one_le_right [covariant_class α α (*) (≤)] {a b : α} (hb : 1 ≤ b) :
min a b ≤ a * b :=
min_le_iff.2 $ or.inl $ le_mul_of_one_le_right' hb
@[to_additive]
lemma min_le_mul_of_one_le_left [covariant_class α α (function.swap (*)) (≤)] {a b : α}
(ha : 1 ≤ a) : min a b ≤ a * b :=
min_le_iff.2 $ or.inr $ le_mul_of_one_le_left' ha
@[to_additive]
lemma max_le_mul_of_one_le [covariant_class α α (*) (≤)]
[covariant_class α α (function.swap (*)) (≤)] {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) :
max a b ≤ a * b :=
max_le_iff.2 ⟨le_mul_of_one_le_right' hb, le_mul_of_one_le_left' ha⟩
end covariant_class_mul_le
|
08059983907c31a0d1e5a37c73301f00726f83c5 | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/analysis/calculus/deriv.lean | 88f8f56871c0ff7e616aea5dae8c446b39b09205 | [
"Apache-2.0"
] | permissive | lacker/mathlib | f2439c743c4f8eb413ec589430c82d0f73b2d539 | ddf7563ac69d42cfa4a1bfe41db1fed521bd795f | refs/heads/master | 1,671,948,326,773 | 1,601,479,268,000 | 1,601,479,268,000 | 298,686,743 | 0 | 0 | Apache-2.0 | 1,601,070,794,000 | 1,601,070,794,000 | null | UTF-8 | Lean | false | false | 70,702 | lean | /-
Copyright (c) 2019 Gabriel Ebner. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Gabriel Ebner, Sébastien Gouëzel
-/
import analysis.calculus.fderiv
import data.polynomial.derivative
/-!
# One-dimensional derivatives
This file defines the derivative of a function `f : 𝕜 → F` where `𝕜` is a
normed field and `F` is a normed space over this field. The derivative of
such a function `f` at a point `x` is given by an element `f' : F`.
The theory is developed analogously to the [Fréchet
derivatives](./fderiv.lean). We first introduce predicates defined in terms
of the corresponding predicates for Fréchet derivatives:
- `has_deriv_at_filter f f' x L` states that the function `f` has the
derivative `f'` at the point `x` as `x` goes along the filter `L`.
- `has_deriv_within_at f f' s x` states that the function `f` has the
derivative `f'` at the point `x` within the subset `s`.
- `has_deriv_at f f' x` states that the function `f` has the derivative `f'`
at the point `x`.
- `has_strict_deriv_at f f' x` states that the function `f` has the derivative `f'`
at the point `x` in the sense of strict differentiability, i.e.,
`f y - f z = (y - z) • f' + o (y - z)` as `y, z → x`.
For the last two notions we also define a functional version:
- `deriv_within f s x` is a derivative of `f` at `x` within `s`. If the
derivative does not exist, then `deriv_within f s x` equals zero.
- `deriv f x` is a derivative of `f` at `x`. If the derivative does not
exist, then `deriv f x` equals zero.
The theorems `fderiv_within_deriv_within` and `fderiv_deriv` show that the
one-dimensional derivatives coincide with the general Fréchet derivatives.
We also show the existence and compute the derivatives of:
- constants
- the identity function
- linear maps
- addition
- sum of finitely many functions
- negation
- subtraction
- multiplication
- inverse `x → x⁻¹`
- multiplication of two functions in `𝕜 → 𝕜`
- multiplication of a function in `𝕜 → 𝕜` and of a function in `𝕜 → E`
- composition of a function in `𝕜 → F` with a function in `𝕜 → 𝕜`
- composition of a function in `F → E` with a function in `𝕜 → F`
- inverse function (assuming that it exists; the inverse function theorem is in `inverse.lean`)
- division
- polynomials
For most binary operations we also define `const_op` and `op_const` theorems for the cases when
the first or second argument is a constant. This makes writing chains of `has_deriv_at`'s easier,
and they more frequently lead to the desired result.
We set up the simplifier so that it can compute the derivative of simple functions. For instance,
```lean
example (x : ℝ) : deriv (λ x, cos (sin x) * exp x) x = (cos(sin(x))-sin(sin(x))*cos(x))*exp(x) :=
by { simp, ring }
```
## Implementation notes
Most of the theorems are direct restatements of the corresponding theorems
for Fréchet derivatives.
The strategy to construct simp lemmas that give the simplifier the possibility to compute
derivatives is the same as the one for differentiability statements, as explained in `fderiv.lean`.
See the explanations there.
-/
universes u v w
noncomputable theory
open_locale classical topological_space big_operators filter
open filter asymptotics set
open continuous_linear_map (smul_right smul_right_one_eq_iff)
variables {𝕜 : Type u} [nondiscrete_normed_field 𝕜]
section
variables {F : Type v} [normed_group F] [normed_space 𝕜 F]
variables {E : Type w} [normed_group E] [normed_space 𝕜 E]
/--
`f` has the derivative `f'` at the point `x` as `x` goes along the filter `L`.
That is, `f x' = f x + (x' - x) • f' + o(x' - x)` where `x'` converges along the filter `L`.
-/
def has_deriv_at_filter (f : 𝕜 → F) (f' : F) (x : 𝕜) (L : filter 𝕜) :=
has_fderiv_at_filter f (smul_right 1 f' : 𝕜 →L[𝕜] F) x L
/--
`f` has the derivative `f'` at the point `x` within the subset `s`.
That is, `f x' = f x + (x' - x) • f' + o(x' - x)` where `x'` converges to `x` inside `s`.
-/
def has_deriv_within_at (f : 𝕜 → F) (f' : F) (s : set 𝕜) (x : 𝕜) :=
has_deriv_at_filter f f' x (𝓝[s] x)
/--
`f` has the derivative `f'` at the point `x`.
That is, `f x' = f x + (x' - x) • f' + o(x' - x)` where `x'` converges to `x`.
-/
def has_deriv_at (f : 𝕜 → F) (f' : F) (x : 𝕜) :=
has_deriv_at_filter f f' x (𝓝 x)
/-- `f` has the derivative `f'` at the point `x` in the sense of strict differentiability.
That is, `f y - f z = (y - z) • f' + o(y - z)` as `y, z → x`. -/
def has_strict_deriv_at (f : 𝕜 → F) (f' : F) (x : 𝕜) :=
has_strict_fderiv_at f (smul_right 1 f' : 𝕜 →L[𝕜] F) x
/--
Derivative of `f` at the point `x` within the set `s`, if it exists. Zero otherwise.
If the derivative exists (i.e., `∃ f', has_deriv_within_at f f' s x`), then
`f x' = f x + (x' - x) • deriv_within f s x + o(x' - x)` where `x'` converges to `x` inside `s`.
-/
def deriv_within (f : 𝕜 → F) (s : set 𝕜) (x : 𝕜) :=
(fderiv_within 𝕜 f s x : 𝕜 →L[𝕜] F) 1
/--
Derivative of `f` at the point `x`, if it exists. Zero otherwise.
If the derivative exists (i.e., `∃ f', has_deriv_at f f' x`), then
`f x' = f x + (x' - x) • deriv f x + o(x' - x)` where `x'` converges to `x`.
-/
def deriv (f : 𝕜 → F) (x : 𝕜) :=
(fderiv 𝕜 f x : 𝕜 →L[𝕜] F) 1
variables {f f₀ f₁ g : 𝕜 → F}
variables {f' f₀' f₁' g' : F}
variables {x : 𝕜}
variables {s t : set 𝕜}
variables {L L₁ L₂ : filter 𝕜}
/-- Expressing `has_fderiv_at_filter f f' x L` in terms of `has_deriv_at_filter` -/
lemma has_fderiv_at_filter_iff_has_deriv_at_filter {f' : 𝕜 →L[𝕜] F} :
has_fderiv_at_filter f f' x L ↔ has_deriv_at_filter f (f' 1) x L :=
by simp [has_deriv_at_filter]
lemma has_fderiv_at_filter.has_deriv_at_filter {f' : 𝕜 →L[𝕜] F} :
has_fderiv_at_filter f f' x L → has_deriv_at_filter f (f' 1) x L :=
has_fderiv_at_filter_iff_has_deriv_at_filter.mp
/-- Expressing `has_fderiv_within_at f f' s x` in terms of `has_deriv_within_at` -/
lemma has_fderiv_within_at_iff_has_deriv_within_at {f' : 𝕜 →L[𝕜] F} :
has_fderiv_within_at f f' s x ↔ has_deriv_within_at f (f' 1) s x :=
has_fderiv_at_filter_iff_has_deriv_at_filter
/-- Expressing `has_deriv_within_at f f' s x` in terms of `has_fderiv_within_at` -/
lemma has_deriv_within_at_iff_has_fderiv_within_at {f' : F} :
has_deriv_within_at f f' s x ↔
has_fderiv_within_at f (smul_right 1 f' : 𝕜 →L[𝕜] F) s x :=
iff.rfl
lemma has_fderiv_within_at.has_deriv_within_at {f' : 𝕜 →L[𝕜] F} :
has_fderiv_within_at f f' s x → has_deriv_within_at f (f' 1) s x :=
has_fderiv_within_at_iff_has_deriv_within_at.mp
lemma has_deriv_within_at.has_fderiv_within_at {f' : F} :
has_deriv_within_at f f' s x → has_fderiv_within_at f (smul_right 1 f' : 𝕜 →L[𝕜] F) s x :=
has_deriv_within_at_iff_has_fderiv_within_at.mp
/-- Expressing `has_fderiv_at f f' x` in terms of `has_deriv_at` -/
lemma has_fderiv_at_iff_has_deriv_at {f' : 𝕜 →L[𝕜] F} :
has_fderiv_at f f' x ↔ has_deriv_at f (f' 1) x :=
has_fderiv_at_filter_iff_has_deriv_at_filter
lemma has_fderiv_at.has_deriv_at {f' : 𝕜 →L[𝕜] F} :
has_fderiv_at f f' x → has_deriv_at f (f' 1) x :=
has_fderiv_at_iff_has_deriv_at.mp
lemma has_strict_fderiv_at_iff_has_strict_deriv_at {f' : 𝕜 →L[𝕜] F} :
has_strict_fderiv_at f f' x ↔ has_strict_deriv_at f (f' 1) x :=
by simp [has_strict_deriv_at, has_strict_fderiv_at]
protected lemma has_strict_fderiv_at.has_strict_deriv_at {f' : 𝕜 →L[𝕜] F} :
has_strict_fderiv_at f f' x → has_strict_deriv_at f (f' 1) x :=
has_strict_fderiv_at_iff_has_strict_deriv_at.mp
/-- Expressing `has_deriv_at f f' x` in terms of `has_fderiv_at` -/
lemma has_deriv_at_iff_has_fderiv_at {f' : F} :
has_deriv_at f f' x ↔
has_fderiv_at f (smul_right 1 f' : 𝕜 →L[𝕜] F) x :=
iff.rfl
lemma deriv_within_zero_of_not_differentiable_within_at
(h : ¬ differentiable_within_at 𝕜 f s x) : deriv_within f s x = 0 :=
by { unfold deriv_within, rw fderiv_within_zero_of_not_differentiable_within_at, simp, assumption }
lemma deriv_zero_of_not_differentiable_at (h : ¬ differentiable_at 𝕜 f x) : deriv f x = 0 :=
by { unfold deriv, rw fderiv_zero_of_not_differentiable_at, simp, assumption }
theorem unique_diff_within_at.eq_deriv (s : set 𝕜) (H : unique_diff_within_at 𝕜 s x)
(h : has_deriv_within_at f f' s x) (h₁ : has_deriv_within_at f f₁' s x) : f' = f₁' :=
smul_right_one_eq_iff.mp $ unique_diff_within_at.eq H h h₁
theorem has_deriv_at_filter_iff_tendsto :
has_deriv_at_filter f f' x L ↔
tendsto (λ x' : 𝕜, ∥x' - x∥⁻¹ * ∥f x' - f x - (x' - x) • f'∥) L (𝓝 0) :=
has_fderiv_at_filter_iff_tendsto
theorem has_deriv_within_at_iff_tendsto : has_deriv_within_at f f' s x ↔
tendsto (λ x', ∥x' - x∥⁻¹ * ∥f x' - f x - (x' - x) • f'∥) (𝓝[s] x) (𝓝 0) :=
has_fderiv_at_filter_iff_tendsto
theorem has_deriv_at_iff_tendsto : has_deriv_at f f' x ↔
tendsto (λ x', ∥x' - x∥⁻¹ * ∥f x' - f x - (x' - x) • f'∥) (𝓝 x) (𝓝 0) :=
has_fderiv_at_filter_iff_tendsto
theorem has_strict_deriv_at.has_deriv_at (h : has_strict_deriv_at f f' x) :
has_deriv_at f f' x :=
h.has_fderiv_at
/-- If the domain has dimension one, then Fréchet derivative is equivalent to the classical
definition with a limit. In this version we have to take the limit along the subset `-{x}`,
because for `y=x` the slope equals zero due to the convention `0⁻¹=0`. -/
lemma has_deriv_at_filter_iff_tendsto_slope {x : 𝕜} {L : filter 𝕜} :
has_deriv_at_filter f f' x L ↔
tendsto (λ y, (y - x)⁻¹ • (f y - f x)) (L ⊓ 𝓟 {x}ᶜ) (𝓝 f') :=
begin
conv_lhs { simp only [has_deriv_at_filter_iff_tendsto, (normed_field.norm_inv _).symm,
(norm_smul _ _).symm, tendsto_zero_iff_norm_tendsto_zero.symm] },
conv_rhs { rw [← nhds_translation f', tendsto_comap_iff] },
refine (tendsto_inf_principal_nhds_iff_of_forall_eq $ by simp).symm.trans (tendsto_congr' _),
refine (eventually_principal.2 $ λ z hz, _).filter_mono inf_le_right,
simp only [(∘)],
rw [smul_sub, ← mul_smul, inv_mul_cancel (sub_ne_zero.2 hz), one_smul]
end
lemma has_deriv_within_at_iff_tendsto_slope {x : 𝕜} {s : set 𝕜} :
has_deriv_within_at f f' s x ↔
tendsto (λ y, (y - x)⁻¹ • (f y - f x)) (𝓝[s \ {x}] x) (𝓝 f') :=
begin
simp only [has_deriv_within_at, nhds_within, diff_eq, inf_assoc.symm, inf_principal.symm],
exact has_deriv_at_filter_iff_tendsto_slope
end
lemma has_deriv_within_at_iff_tendsto_slope' {x : 𝕜} {s : set 𝕜} (hs : x ∉ s) :
has_deriv_within_at f f' s x ↔
tendsto (λ y, (y - x)⁻¹ • (f y - f x)) (𝓝[s] x) (𝓝 f') :=
begin
convert ← has_deriv_within_at_iff_tendsto_slope,
exact diff_singleton_eq_self hs
end
lemma has_deriv_at_iff_tendsto_slope {x : 𝕜} :
has_deriv_at f f' x ↔
tendsto (λ y, (y - x)⁻¹ • (f y - f x)) (𝓝[{x}ᶜ] x) (𝓝 f') :=
has_deriv_at_filter_iff_tendsto_slope
theorem has_deriv_at_iff_is_o_nhds_zero : has_deriv_at f f' x ↔
is_o (λh, f (x + h) - f x - h • f') (λh, h) (𝓝 0) :=
has_fderiv_at_iff_is_o_nhds_zero
theorem has_deriv_at_filter.mono (h : has_deriv_at_filter f f' x L₂) (hst : L₁ ≤ L₂) :
has_deriv_at_filter f f' x L₁ :=
has_fderiv_at_filter.mono h hst
theorem has_deriv_within_at.mono (h : has_deriv_within_at f f' t x) (hst : s ⊆ t) :
has_deriv_within_at f f' s x :=
has_fderiv_within_at.mono h hst
theorem has_deriv_at.has_deriv_at_filter (h : has_deriv_at f f' x) (hL : L ≤ 𝓝 x) :
has_deriv_at_filter f f' x L :=
has_fderiv_at.has_fderiv_at_filter h hL
theorem has_deriv_at.has_deriv_within_at
(h : has_deriv_at f f' x) : has_deriv_within_at f f' s x :=
has_fderiv_at.has_fderiv_within_at h
lemma has_deriv_within_at.differentiable_within_at (h : has_deriv_within_at f f' s x) :
differentiable_within_at 𝕜 f s x :=
has_fderiv_within_at.differentiable_within_at h
lemma has_deriv_at.differentiable_at (h : has_deriv_at f f' x) : differentiable_at 𝕜 f x :=
has_fderiv_at.differentiable_at h
@[simp] lemma has_deriv_within_at_univ : has_deriv_within_at f f' univ x ↔ has_deriv_at f f' x :=
has_fderiv_within_at_univ
theorem has_deriv_at_unique
(h₀ : has_deriv_at f f₀' x) (h₁ : has_deriv_at f f₁' x) : f₀' = f₁' :=
smul_right_one_eq_iff.mp $ has_fderiv_at_unique h₀ h₁
lemma has_deriv_within_at_inter' (h : t ∈ 𝓝[s] x) :
has_deriv_within_at f f' (s ∩ t) x ↔ has_deriv_within_at f f' s x :=
has_fderiv_within_at_inter' h
lemma has_deriv_within_at_inter (h : t ∈ 𝓝 x) :
has_deriv_within_at f f' (s ∩ t) x ↔ has_deriv_within_at f f' s x :=
has_fderiv_within_at_inter h
lemma has_deriv_within_at.union (hs : has_deriv_within_at f f' s x) (ht : has_deriv_within_at f f' t x) :
has_deriv_within_at f f' (s ∪ t) x :=
begin
simp only [has_deriv_within_at, nhds_within_union],
exact hs.join ht,
end
lemma has_deriv_within_at.nhds_within (h : has_deriv_within_at f f' s x)
(ht : s ∈ 𝓝[t] x) : has_deriv_within_at f f' t x :=
(has_deriv_within_at_inter' ht).1 (h.mono (inter_subset_right _ _))
lemma has_deriv_within_at.has_deriv_at (h : has_deriv_within_at f f' s x) (hs : s ∈ 𝓝 x) :
has_deriv_at f f' x :=
has_fderiv_within_at.has_fderiv_at h hs
lemma differentiable_within_at.has_deriv_within_at (h : differentiable_within_at 𝕜 f s x) :
has_deriv_within_at f (deriv_within f s x) s x :=
show has_fderiv_within_at _ _ _ _, by { convert h.has_fderiv_within_at, simp [deriv_within] }
lemma differentiable_at.has_deriv_at (h : differentiable_at 𝕜 f x) : has_deriv_at f (deriv f x) x :=
show has_fderiv_at _ _ _, by { convert h.has_fderiv_at, simp [deriv] }
lemma has_deriv_at.deriv (h : has_deriv_at f f' x) : deriv f x = f' :=
has_deriv_at_unique h.differentiable_at.has_deriv_at h
lemma has_deriv_within_at.deriv_within
(h : has_deriv_within_at f f' s x) (hxs : unique_diff_within_at 𝕜 s x) :
deriv_within f s x = f' :=
hxs.eq_deriv _ h.differentiable_within_at.has_deriv_within_at h
lemma fderiv_within_deriv_within : (fderiv_within 𝕜 f s x : 𝕜 → F) 1 = deriv_within f s x :=
rfl
lemma deriv_within_fderiv_within :
smul_right 1 (deriv_within f s x) = fderiv_within 𝕜 f s x :=
by simp [deriv_within]
lemma fderiv_deriv : (fderiv 𝕜 f x : 𝕜 → F) 1 = deriv f x :=
rfl
lemma deriv_fderiv :
smul_right 1 (deriv f x) = fderiv 𝕜 f x :=
by simp [deriv]
lemma differentiable_at.deriv_within (h : differentiable_at 𝕜 f x)
(hxs : unique_diff_within_at 𝕜 s x) : deriv_within f s x = deriv f x :=
by { unfold deriv_within deriv, rw h.fderiv_within hxs }
lemma deriv_within_subset (st : s ⊆ t) (ht : unique_diff_within_at 𝕜 s x)
(h : differentiable_within_at 𝕜 f t x) :
deriv_within f s x = deriv_within f t x :=
((differentiable_within_at.has_deriv_within_at h).mono st).deriv_within ht
@[simp] lemma deriv_within_univ : deriv_within f univ = deriv f :=
by { ext, unfold deriv_within deriv, rw fderiv_within_univ }
lemma deriv_within_inter (ht : t ∈ 𝓝 x) (hs : unique_diff_within_at 𝕜 s x) :
deriv_within f (s ∩ t) x = deriv_within f s x :=
by { unfold deriv_within, rw fderiv_within_inter ht hs }
lemma deriv_within_of_open (hs : is_open s) (hx : x ∈ s) :
deriv_within f s x = deriv f x :=
by { unfold deriv_within, rw fderiv_within_of_open hs hx, refl }
section congr
/-! ### Congruence properties of derivatives -/
theorem filter.eventually_eq.has_deriv_at_filter_iff
(h₀ : f₀ =ᶠ[L] f₁) (hx : f₀ x = f₁ x) (h₁ : f₀' = f₁') :
has_deriv_at_filter f₀ f₀' x L ↔ has_deriv_at_filter f₁ f₁' x L :=
h₀.has_fderiv_at_filter_iff hx (by simp [h₁])
lemma has_deriv_at_filter.congr_of_eventually_eq (h : has_deriv_at_filter f f' x L)
(hL : f₁ =ᶠ[L] f) (hx : f₁ x = f x) : has_deriv_at_filter f₁ f' x L :=
by rwa hL.has_deriv_at_filter_iff hx rfl
lemma has_deriv_within_at.congr_mono (h : has_deriv_within_at f f' s x) (ht : ∀x ∈ t, f₁ x = f x)
(hx : f₁ x = f x) (h₁ : t ⊆ s) : has_deriv_within_at f₁ f' t x :=
has_fderiv_within_at.congr_mono h ht hx h₁
lemma has_deriv_within_at.congr (h : has_deriv_within_at f f' s x) (hs : ∀x ∈ s, f₁ x = f x)
(hx : f₁ x = f x) : has_deriv_within_at f₁ f' s x :=
h.congr_mono hs hx (subset.refl _)
lemma has_deriv_within_at.congr_of_eventually_eq (h : has_deriv_within_at f f' s x)
(h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : has_deriv_within_at f₁ f' s x :=
has_deriv_at_filter.congr_of_eventually_eq h h₁ hx
lemma has_deriv_at.congr_of_eventually_eq (h : has_deriv_at f f' x)
(h₁ : f₁ =ᶠ[𝓝 x] f) : has_deriv_at f₁ f' x :=
has_deriv_at_filter.congr_of_eventually_eq h h₁ (mem_of_nhds h₁ : _)
lemma filter.eventually_eq.deriv_within_eq (hs : unique_diff_within_at 𝕜 s x)
(hL : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) :
deriv_within f₁ s x = deriv_within f s x :=
by { unfold deriv_within, rw hL.fderiv_within_eq hs hx }
lemma deriv_within_congr (hs : unique_diff_within_at 𝕜 s x)
(hL : ∀y∈s, f₁ y = f y) (hx : f₁ x = f x) :
deriv_within f₁ s x = deriv_within f s x :=
by { unfold deriv_within, rw fderiv_within_congr hs hL hx }
lemma filter.eventually_eq.deriv_eq (hL : f₁ =ᶠ[𝓝 x] f) : deriv f₁ x = deriv f x :=
by { unfold deriv, rwa filter.eventually_eq.fderiv_eq }
end congr
section id
/-! ### Derivative of the identity -/
variables (s x L)
theorem has_deriv_at_filter_id : has_deriv_at_filter id 1 x L :=
(has_fderiv_at_filter_id x L).has_deriv_at_filter
theorem has_deriv_within_at_id : has_deriv_within_at id 1 s x :=
has_deriv_at_filter_id _ _
theorem has_deriv_at_id : has_deriv_at id 1 x :=
has_deriv_at_filter_id _ _
theorem has_deriv_at_id' : has_deriv_at (λ (x : 𝕜), x) 1 x :=
has_deriv_at_filter_id _ _
theorem has_strict_deriv_at_id : has_strict_deriv_at id 1 x :=
(has_strict_fderiv_at_id x).has_strict_deriv_at
lemma deriv_id : deriv id x = 1 :=
has_deriv_at.deriv (has_deriv_at_id x)
@[simp] lemma deriv_id' : deriv (@id 𝕜) = λ _, 1 :=
funext deriv_id
@[simp] lemma deriv_id'' : deriv (λ x : 𝕜, x) x = 1 :=
deriv_id x
lemma deriv_within_id (hxs : unique_diff_within_at 𝕜 s x) : deriv_within id s x = 1 :=
(has_deriv_within_at_id x s).deriv_within hxs
end id
section const
/-! ### Derivative of constant functions -/
variables (c : F) (s x L)
theorem has_deriv_at_filter_const : has_deriv_at_filter (λ x, c) 0 x L :=
(has_fderiv_at_filter_const c x L).has_deriv_at_filter
theorem has_strict_deriv_at_const : has_strict_deriv_at (λ x, c) 0 x :=
(has_strict_fderiv_at_const c x).has_strict_deriv_at
theorem has_deriv_within_at_const : has_deriv_within_at (λ x, c) 0 s x :=
has_deriv_at_filter_const _ _ _
theorem has_deriv_at_const : has_deriv_at (λ x, c) 0 x :=
has_deriv_at_filter_const _ _ _
lemma deriv_const : deriv (λ x, c) x = 0 :=
has_deriv_at.deriv (has_deriv_at_const x c)
@[simp] lemma deriv_const' : deriv (λ x:𝕜, c) = λ x, 0 :=
funext (λ x, deriv_const x c)
lemma deriv_within_const (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (λ x, c) s x = 0 :=
(has_deriv_within_at_const _ _ _).deriv_within hxs
end const
section continuous_linear_map
/-! ### Derivative of continuous linear maps -/
variables (e : 𝕜 →L[𝕜] F)
lemma continuous_linear_map.has_deriv_at_filter : has_deriv_at_filter e (e 1) x L :=
e.has_fderiv_at_filter.has_deriv_at_filter
lemma continuous_linear_map.has_strict_deriv_at : has_strict_deriv_at e (e 1) x :=
e.has_strict_fderiv_at.has_strict_deriv_at
lemma continuous_linear_map.has_deriv_at : has_deriv_at e (e 1) x :=
e.has_deriv_at_filter
lemma continuous_linear_map.has_deriv_within_at : has_deriv_within_at e (e 1) s x :=
e.has_deriv_at_filter
@[simp] lemma continuous_linear_map.deriv : deriv e x = e 1 :=
e.has_deriv_at.deriv
lemma continuous_linear_map.deriv_within (hxs : unique_diff_within_at 𝕜 s x) :
deriv_within e s x = e 1 :=
e.has_deriv_within_at.deriv_within hxs
end continuous_linear_map
section linear_map
/-! ### Derivative of bundled linear maps -/
variables (e : 𝕜 →ₗ[𝕜] F)
lemma linear_map.has_deriv_at_filter : has_deriv_at_filter e (e 1) x L :=
e.to_continuous_linear_map₁.has_deriv_at_filter
lemma linear_map.has_strict_deriv_at : has_strict_deriv_at e (e 1) x :=
e.to_continuous_linear_map₁.has_strict_deriv_at
lemma linear_map.has_deriv_at : has_deriv_at e (e 1) x :=
e.has_deriv_at_filter
lemma linear_map.has_deriv_within_at : has_deriv_within_at e (e 1) s x :=
e.has_deriv_at_filter
@[simp] lemma linear_map.deriv : deriv e x = e 1 :=
e.has_deriv_at.deriv
lemma linear_map.deriv_within (hxs : unique_diff_within_at 𝕜 s x) :
deriv_within e s x = e 1 :=
e.has_deriv_within_at.deriv_within hxs
end linear_map
section add
/-! ### Derivative of the sum of two functions -/
theorem has_deriv_at_filter.add
(hf : has_deriv_at_filter f f' x L) (hg : has_deriv_at_filter g g' x L) :
has_deriv_at_filter (λ y, f y + g y) (f' + g') x L :=
by simpa using (hf.add hg).has_deriv_at_filter
theorem has_strict_deriv_at.add
(hf : has_strict_deriv_at f f' x) (hg : has_strict_deriv_at g g' x) :
has_strict_deriv_at (λ y, f y + g y) (f' + g') x :=
by simpa using (hf.add hg).has_strict_deriv_at
theorem has_deriv_within_at.add
(hf : has_deriv_within_at f f' s x) (hg : has_deriv_within_at g g' s x) :
has_deriv_within_at (λ y, f y + g y) (f' + g') s x :=
hf.add hg
theorem has_deriv_at.add
(hf : has_deriv_at f f' x) (hg : has_deriv_at g g' x) :
has_deriv_at (λ x, f x + g x) (f' + g') x :=
hf.add hg
lemma deriv_within_add (hxs : unique_diff_within_at 𝕜 s x)
(hf : differentiable_within_at 𝕜 f s x) (hg : differentiable_within_at 𝕜 g s x) :
deriv_within (λy, f y + g y) s x = deriv_within f s x + deriv_within g s x :=
(hf.has_deriv_within_at.add hg.has_deriv_within_at).deriv_within hxs
@[simp] lemma deriv_add
(hf : differentiable_at 𝕜 f x) (hg : differentiable_at 𝕜 g x) :
deriv (λy, f y + g y) x = deriv f x + deriv g x :=
(hf.has_deriv_at.add hg.has_deriv_at).deriv
theorem has_deriv_at_filter.add_const
(hf : has_deriv_at_filter f f' x L) (c : F) :
has_deriv_at_filter (λ y, f y + c) f' x L :=
add_zero f' ▸ hf.add (has_deriv_at_filter_const x L c)
theorem has_deriv_within_at.add_const
(hf : has_deriv_within_at f f' s x) (c : F) :
has_deriv_within_at (λ y, f y + c) f' s x :=
hf.add_const c
theorem has_deriv_at.add_const
(hf : has_deriv_at f f' x) (c : F) :
has_deriv_at (λ x, f x + c) f' x :=
hf.add_const c
lemma deriv_within_add_const (hxs : unique_diff_within_at 𝕜 s x)
(hf : differentiable_within_at 𝕜 f s x) (c : F) :
deriv_within (λy, f y + c) s x = deriv_within f s x :=
(hf.has_deriv_within_at.add_const c).deriv_within hxs
lemma deriv_add_const (hf : differentiable_at 𝕜 f x) (c : F) :
deriv (λy, f y + c) x = deriv f x :=
(hf.has_deriv_at.add_const c).deriv
theorem has_deriv_at_filter.const_add (c : F) (hf : has_deriv_at_filter f f' x L) :
has_deriv_at_filter (λ y, c + f y) f' x L :=
zero_add f' ▸ (has_deriv_at_filter_const x L c).add hf
theorem has_deriv_within_at.const_add (c : F) (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ y, c + f y) f' s x :=
hf.const_add c
theorem has_deriv_at.const_add (c : F) (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, c + f x) f' x :=
hf.const_add c
lemma deriv_within_const_add (hxs : unique_diff_within_at 𝕜 s x)
(c : F) (hf : differentiable_within_at 𝕜 f s x) :
deriv_within (λy, c + f y) s x = deriv_within f s x :=
(hf.has_deriv_within_at.const_add c).deriv_within hxs
lemma deriv_const_add (c : F) (hf : differentiable_at 𝕜 f x) :
deriv (λy, c + f y) x = deriv f x :=
(hf.has_deriv_at.const_add c).deriv
end add
section sum
/-! ### Derivative of a finite sum of functions -/
open_locale big_operators
variables {ι : Type*} {u : finset ι} {A : ι → (𝕜 → F)} {A' : ι → F}
theorem has_deriv_at_filter.sum (h : ∀ i ∈ u, has_deriv_at_filter (A i) (A' i) x L) :
has_deriv_at_filter (λ y, ∑ i in u, A i y) (∑ i in u, A' i) x L :=
by simpa [continuous_linear_map.sum_apply] using (has_fderiv_at_filter.sum h).has_deriv_at_filter
theorem has_strict_deriv_at.sum (h : ∀ i ∈ u, has_strict_deriv_at (A i) (A' i) x) :
has_strict_deriv_at (λ y, ∑ i in u, A i y) (∑ i in u, A' i) x :=
by simpa [continuous_linear_map.sum_apply] using (has_strict_fderiv_at.sum h).has_strict_deriv_at
theorem has_deriv_within_at.sum (h : ∀ i ∈ u, has_deriv_within_at (A i) (A' i) s x) :
has_deriv_within_at (λ y, ∑ i in u, A i y) (∑ i in u, A' i) s x :=
has_deriv_at_filter.sum h
theorem has_deriv_at.sum (h : ∀ i ∈ u, has_deriv_at (A i) (A' i) x) :
has_deriv_at (λ y, ∑ i in u, A i y) (∑ i in u, A' i) x :=
has_deriv_at_filter.sum h
lemma deriv_within_sum (hxs : unique_diff_within_at 𝕜 s x)
(h : ∀ i ∈ u, differentiable_within_at 𝕜 (A i) s x) :
deriv_within (λ y, ∑ i in u, A i y) s x = ∑ i in u, deriv_within (A i) s x :=
(has_deriv_within_at.sum (λ i hi, (h i hi).has_deriv_within_at)).deriv_within hxs
@[simp] lemma deriv_sum (h : ∀ i ∈ u, differentiable_at 𝕜 (A i) x) :
deriv (λ y, ∑ i in u, A i y) x = ∑ i in u, deriv (A i) x :=
(has_deriv_at.sum (λ i hi, (h i hi).has_deriv_at)).deriv
end sum
section mul_vector
/-! ### Derivative of the multiplication of a scalar function and a vector function -/
variables {c : 𝕜 → 𝕜} {c' : 𝕜}
theorem has_deriv_within_at.smul
(hc : has_deriv_within_at c c' s x) (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ y, c y • f y) (c x • f' + c' • f x) s x :=
by simpa using (has_fderiv_within_at.smul hc hf).has_deriv_within_at
theorem has_deriv_at.smul
(hc : has_deriv_at c c' x) (hf : has_deriv_at f f' x) :
has_deriv_at (λ y, c y • f y) (c x • f' + c' • f x) x :=
begin
rw [← has_deriv_within_at_univ] at *,
exact hc.smul hf
end
theorem has_strict_deriv_at.smul
(hc : has_strict_deriv_at c c' x) (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ y, c y • f y) (c x • f' + c' • f x) x :=
by simpa using (hc.smul hf).has_strict_deriv_at
lemma deriv_within_smul (hxs : unique_diff_within_at 𝕜 s x)
(hc : differentiable_within_at 𝕜 c s x) (hf : differentiable_within_at 𝕜 f s x) :
deriv_within (λ y, c y • f y) s x = c x • deriv_within f s x + (deriv_within c s x) • f x :=
(hc.has_deriv_within_at.smul hf.has_deriv_within_at).deriv_within hxs
lemma deriv_smul (hc : differentiable_at 𝕜 c x) (hf : differentiable_at 𝕜 f x) :
deriv (λ y, c y • f y) x = c x • deriv f x + (deriv c x) • f x :=
(hc.has_deriv_at.smul hf.has_deriv_at).deriv
theorem has_deriv_within_at.smul_const
(hc : has_deriv_within_at c c' s x) (f : F) :
has_deriv_within_at (λ y, c y • f) (c' • f) s x :=
begin
have := hc.smul (has_deriv_within_at_const x s f),
rwa [smul_zero, zero_add] at this
end
theorem has_deriv_at.smul_const
(hc : has_deriv_at c c' x) (f : F) :
has_deriv_at (λ y, c y • f) (c' • f) x :=
begin
rw [← has_deriv_within_at_univ] at *,
exact hc.smul_const f
end
lemma deriv_within_smul_const (hxs : unique_diff_within_at 𝕜 s x)
(hc : differentiable_within_at 𝕜 c s x) (f : F) :
deriv_within (λ y, c y • f) s x = (deriv_within c s x) • f :=
(hc.has_deriv_within_at.smul_const f).deriv_within hxs
lemma deriv_smul_const (hc : differentiable_at 𝕜 c x) (f : F) :
deriv (λ y, c y • f) x = (deriv c x) • f :=
(hc.has_deriv_at.smul_const f).deriv
theorem has_deriv_within_at.const_smul
(c : 𝕜) (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ y, c • f y) (c • f') s x :=
begin
convert (has_deriv_within_at_const x s c).smul hf,
rw [zero_smul, add_zero]
end
theorem has_deriv_at.const_smul (c : 𝕜) (hf : has_deriv_at f f' x) :
has_deriv_at (λ y, c • f y) (c • f') x :=
begin
rw [← has_deriv_within_at_univ] at *,
exact hf.const_smul c
end
lemma deriv_within_const_smul (hxs : unique_diff_within_at 𝕜 s x)
(c : 𝕜) (hf : differentiable_within_at 𝕜 f s x) :
deriv_within (λ y, c • f y) s x = c • deriv_within f s x :=
(hf.has_deriv_within_at.const_smul c).deriv_within hxs
lemma deriv_const_smul (c : 𝕜) (hf : differentiable_at 𝕜 f x) :
deriv (λ y, c • f y) x = c • deriv f x :=
(hf.has_deriv_at.const_smul c).deriv
end mul_vector
section neg
/-! ### Derivative of the negative of a function -/
theorem has_deriv_at_filter.neg (h : has_deriv_at_filter f f' x L) :
has_deriv_at_filter (λ x, -f x) (-f') x L :=
by simpa using h.neg.has_deriv_at_filter
theorem has_deriv_within_at.neg (h : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, -f x) (-f') s x :=
h.neg
theorem has_deriv_at.neg (h : has_deriv_at f f' x) : has_deriv_at (λ x, -f x) (-f') x :=
h.neg
theorem has_strict_deriv_at.neg (h : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, -f x) (-f') x :=
by simpa using h.neg.has_strict_deriv_at
lemma deriv_within.neg (hxs : unique_diff_within_at 𝕜 s x)
(h : differentiable_within_at 𝕜 f s x) :
deriv_within (λy, -f y) s x = - deriv_within f s x :=
h.has_deriv_within_at.neg.deriv_within hxs
lemma deriv.neg : deriv (λy, -f y) x = - deriv f x :=
if h : differentiable_at 𝕜 f x then h.has_deriv_at.neg.deriv else
have ¬differentiable_at 𝕜 (λ y, -f y) x, from λ h', by simpa only [neg_neg] using h'.neg,
by simp only [deriv_zero_of_not_differentiable_at h,
deriv_zero_of_not_differentiable_at this, neg_zero]
@[simp] lemma deriv.neg' : deriv (λy, -f y) = (λ x, - deriv f x) :=
funext $ λ x, deriv.neg
end neg
section neg2
/-! ### Derivative of the negation function (i.e `has_neg.neg`) -/
variables (s x L)
theorem has_deriv_at_filter_neg : has_deriv_at_filter has_neg.neg (-1) x L :=
has_deriv_at_filter.neg $ has_deriv_at_filter_id _ _
theorem has_deriv_within_at_neg : has_deriv_within_at has_neg.neg (-1) s x :=
has_deriv_at_filter_neg _ _
theorem has_deriv_at_neg : has_deriv_at has_neg.neg (-1) x :=
has_deriv_at_filter_neg _ _
theorem has_deriv_at_neg' : has_deriv_at (λ x, -x) (-1) x :=
has_deriv_at_filter_neg _ _
theorem has_strict_deriv_at_neg : has_strict_deriv_at has_neg.neg (-1) x :=
has_strict_deriv_at.neg $ has_strict_deriv_at_id _
lemma deriv_neg : deriv has_neg.neg x = -1 :=
has_deriv_at.deriv (has_deriv_at_neg x)
@[simp] lemma deriv_neg' : deriv (has_neg.neg : 𝕜 → 𝕜) = λ _, -1 :=
funext deriv_neg
@[simp] lemma deriv_neg'' : deriv (λ x : 𝕜, -x) x = -1 :=
deriv_neg x
lemma deriv_within_neg (hxs : unique_diff_within_at 𝕜 s x) : deriv_within has_neg.neg s x = -1 :=
(has_deriv_within_at_neg x s).deriv_within hxs
lemma differentiable_neg : differentiable 𝕜 (has_neg.neg : 𝕜 → 𝕜) :=
differentiable.neg differentiable_id
lemma differentiable_on_neg : differentiable_on 𝕜 (has_neg.neg : 𝕜 → 𝕜) s :=
differentiable_on.neg differentiable_on_id
end neg2
section sub
/-! ### Derivative of the difference of two functions -/
theorem has_deriv_at_filter.sub
(hf : has_deriv_at_filter f f' x L) (hg : has_deriv_at_filter g g' x L) :
has_deriv_at_filter (λ x, f x - g x) (f' - g') x L :=
hf.add hg.neg
theorem has_deriv_within_at.sub
(hf : has_deriv_within_at f f' s x) (hg : has_deriv_within_at g g' s x) :
has_deriv_within_at (λ x, f x - g x) (f' - g') s x :=
hf.sub hg
theorem has_deriv_at.sub
(hf : has_deriv_at f f' x) (hg : has_deriv_at g g' x) :
has_deriv_at (λ x, f x - g x) (f' - g') x :=
hf.sub hg
theorem has_strict_deriv_at.sub
(hf : has_strict_deriv_at f f' x) (hg : has_strict_deriv_at g g' x) :
has_strict_deriv_at (λ x, f x - g x) (f' - g') x :=
hf.add hg.neg
lemma deriv_within_sub (hxs : unique_diff_within_at 𝕜 s x)
(hf : differentiable_within_at 𝕜 f s x) (hg : differentiable_within_at 𝕜 g s x) :
deriv_within (λy, f y - g y) s x = deriv_within f s x - deriv_within g s x :=
(hf.has_deriv_within_at.sub hg.has_deriv_within_at).deriv_within hxs
@[simp] lemma deriv_sub
(hf : differentiable_at 𝕜 f x) (hg : differentiable_at 𝕜 g x) :
deriv (λ y, f y - g y) x = deriv f x - deriv g x :=
(hf.has_deriv_at.sub hg.has_deriv_at).deriv
theorem has_deriv_at_filter.is_O_sub (h : has_deriv_at_filter f f' x L) :
is_O (λ x', f x' - f x) (λ x', x' - x) L :=
has_fderiv_at_filter.is_O_sub h
theorem has_deriv_at_filter.sub_const
(hf : has_deriv_at_filter f f' x L) (c : F) :
has_deriv_at_filter (λ x, f x - c) f' x L :=
hf.add_const (-c)
theorem has_deriv_within_at.sub_const
(hf : has_deriv_within_at f f' s x) (c : F) :
has_deriv_within_at (λ x, f x - c) f' s x :=
hf.sub_const c
theorem has_deriv_at.sub_const
(hf : has_deriv_at f f' x) (c : F) :
has_deriv_at (λ x, f x - c) f' x :=
hf.sub_const c
lemma deriv_within_sub_const (hxs : unique_diff_within_at 𝕜 s x)
(hf : differentiable_within_at 𝕜 f s x) (c : F) :
deriv_within (λy, f y - c) s x = deriv_within f s x :=
(hf.has_deriv_within_at.sub_const c).deriv_within hxs
lemma deriv_sub_const (c : F) (hf : differentiable_at 𝕜 f x) :
deriv (λ y, f y - c) x = deriv f x :=
(hf.has_deriv_at.sub_const c).deriv
theorem has_deriv_at_filter.const_sub (c : F) (hf : has_deriv_at_filter f f' x L) :
has_deriv_at_filter (λ x, c - f x) (-f') x L :=
hf.neg.const_add c
theorem has_deriv_within_at.const_sub (c : F) (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, c - f x) (-f') s x :=
hf.const_sub c
theorem has_deriv_at.const_sub (c : F) (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, c - f x) (-f') x :=
hf.const_sub c
lemma deriv_within_const_sub (hxs : unique_diff_within_at 𝕜 s x)
(c : F) (hf : differentiable_within_at 𝕜 f s x) :
deriv_within (λy, c - f y) s x = -deriv_within f s x :=
(hf.has_deriv_within_at.const_sub c).deriv_within hxs
lemma deriv_const_sub (c : F) (hf : differentiable_at 𝕜 f x) :
deriv (λ y, c - f y) x = -deriv f x :=
(hf.has_deriv_at.const_sub c).deriv
end sub
section continuous
/-! ### Continuity of a function admitting a derivative -/
theorem has_deriv_at_filter.tendsto_nhds
(hL : L ≤ 𝓝 x) (h : has_deriv_at_filter f f' x L) :
tendsto f L (𝓝 (f x)) :=
h.tendsto_nhds hL
theorem has_deriv_within_at.continuous_within_at
(h : has_deriv_within_at f f' s x) : continuous_within_at f s x :=
has_deriv_at_filter.tendsto_nhds inf_le_left h
theorem has_deriv_at.continuous_at (h : has_deriv_at f f' x) : continuous_at f x :=
has_deriv_at_filter.tendsto_nhds (le_refl _) h
end continuous
section cartesian_product
/-! ### Derivative of the cartesian product of two functions -/
variables {G : Type w} [normed_group G] [normed_space 𝕜 G]
variables {f₂ : 𝕜 → G} {f₂' : G}
lemma has_deriv_at_filter.prod
(hf₁ : has_deriv_at_filter f₁ f₁' x L) (hf₂ : has_deriv_at_filter f₂ f₂' x L) :
has_deriv_at_filter (λ x, (f₁ x, f₂ x)) (f₁', f₂') x L :=
show has_fderiv_at_filter _ _ _ _,
by convert has_fderiv_at_filter.prod hf₁ hf₂
lemma has_deriv_within_at.prod
(hf₁ : has_deriv_within_at f₁ f₁' s x) (hf₂ : has_deriv_within_at f₂ f₂' s x) :
has_deriv_within_at (λ x, (f₁ x, f₂ x)) (f₁', f₂') s x :=
hf₁.prod hf₂
lemma has_deriv_at.prod (hf₁ : has_deriv_at f₁ f₁' x) (hf₂ : has_deriv_at f₂ f₂' x) :
has_deriv_at (λ x, (f₁ x, f₂ x)) (f₁', f₂') x :=
hf₁.prod hf₂
end cartesian_product
section composition
/-!
### Derivative of the composition of a vector function and a scalar function
We use `scomp` in lemmas on composition of vector valued and scalar valued functions, and `comp`
in lemmas on composition of scalar valued functions, in analogy for `smul` and `mul` (and also
because the `comp` version with the shorter name will show up much more often in applications).
The formula for the derivative involves `smul` in `scomp` lemmas, which can be reduced to
usual multiplication in `comp` lemmas.
-/
variables {h h₁ h₂ : 𝕜 → 𝕜} {h' h₁' h₂' : 𝕜}
/- For composition lemmas, we put x explicit to help the elaborator, as otherwise Lean tends to
get confused since there are too many possibilities for composition -/
variable (x)
theorem has_deriv_at_filter.scomp
(hg : has_deriv_at_filter g g' (h x) (L.map h))
(hh : has_deriv_at_filter h h' x L) :
has_deriv_at_filter (g ∘ h) (h' • g') x L :=
by simpa using (hg.comp x hh).has_deriv_at_filter
theorem has_deriv_within_at.scomp {t : set 𝕜}
(hg : has_deriv_within_at g g' t (h x))
(hh : has_deriv_within_at h h' s x) (hst : s ⊆ h ⁻¹' t) :
has_deriv_within_at (g ∘ h) (h' • g') s x :=
begin
apply has_deriv_at_filter.scomp _ (has_deriv_at_filter.mono hg _) hh,
calc map h (𝓝[s] x) ≤ 𝓝[h '' s] (h x) : hh.continuous_within_at.tendsto_nhds_within_image
... ≤ 𝓝[t] (h x) : nhds_within_mono _ (image_subset_iff.mpr hst)
end
/-- The chain rule. -/
theorem has_deriv_at.scomp
(hg : has_deriv_at g g' (h x)) (hh : has_deriv_at h h' x) :
has_deriv_at (g ∘ h) (h' • g') x :=
(hg.mono hh.continuous_at).scomp x hh
theorem has_strict_deriv_at.scomp
(hg : has_strict_deriv_at g g' (h x)) (hh : has_strict_deriv_at h h' x) :
has_strict_deriv_at (g ∘ h) (h' • g') x :=
by simpa using (hg.comp x hh).has_strict_deriv_at
theorem has_deriv_at.scomp_has_deriv_within_at
(hg : has_deriv_at g g' (h x)) (hh : has_deriv_within_at h h' s x) :
has_deriv_within_at (g ∘ h) (h' • g') s x :=
begin
rw ← has_deriv_within_at_univ at hg,
exact has_deriv_within_at.scomp x hg hh subset_preimage_univ
end
lemma deriv_within.scomp
(hg : differentiable_within_at 𝕜 g t (h x)) (hh : differentiable_within_at 𝕜 h s x)
(hs : s ⊆ h ⁻¹' t) (hxs : unique_diff_within_at 𝕜 s x) :
deriv_within (g ∘ h) s x = deriv_within h s x • deriv_within g t (h x) :=
begin
apply has_deriv_within_at.deriv_within _ hxs,
exact has_deriv_within_at.scomp x (hg.has_deriv_within_at) (hh.has_deriv_within_at) hs
end
lemma deriv.scomp
(hg : differentiable_at 𝕜 g (h x)) (hh : differentiable_at 𝕜 h x) :
deriv (g ∘ h) x = deriv h x • deriv g (h x) :=
begin
apply has_deriv_at.deriv,
exact has_deriv_at.scomp x hg.has_deriv_at hh.has_deriv_at
end
/-! ### Derivative of the composition of two scalar functions -/
theorem has_deriv_at_filter.comp
(hh₁ : has_deriv_at_filter h₁ h₁' (h₂ x) (L.map h₂))
(hh₂ : has_deriv_at_filter h₂ h₂' x L) :
has_deriv_at_filter (h₁ ∘ h₂) (h₁' * h₂') x L :=
by { rw mul_comm, exact hh₁.scomp x hh₂ }
theorem has_deriv_within_at.comp {t : set 𝕜}
(hh₁ : has_deriv_within_at h₁ h₁' t (h₂ x))
(hh₂ : has_deriv_within_at h₂ h₂' s x) (hst : s ⊆ h₂ ⁻¹' t) :
has_deriv_within_at (h₁ ∘ h₂) (h₁' * h₂') s x :=
by { rw mul_comm, exact hh₁.scomp x hh₂ hst, }
/-- The chain rule. -/
theorem has_deriv_at.comp
(hh₁ : has_deriv_at h₁ h₁' (h₂ x)) (hh₂ : has_deriv_at h₂ h₂' x) :
has_deriv_at (h₁ ∘ h₂) (h₁' * h₂') x :=
(hh₁.mono hh₂.continuous_at).comp x hh₂
theorem has_strict_deriv_at.comp
(hh₁ : has_strict_deriv_at h₁ h₁' (h₂ x)) (hh₂ : has_strict_deriv_at h₂ h₂' x) :
has_strict_deriv_at (h₁ ∘ h₂) (h₁' * h₂') x :=
by { rw mul_comm, exact hh₁.scomp x hh₂ }
theorem has_deriv_at.comp_has_deriv_within_at
(hh₁ : has_deriv_at h₁ h₁' (h₂ x)) (hh₂ : has_deriv_within_at h₂ h₂' s x) :
has_deriv_within_at (h₁ ∘ h₂) (h₁' * h₂') s x :=
begin
rw ← has_deriv_within_at_univ at hh₁,
exact has_deriv_within_at.comp x hh₁ hh₂ subset_preimage_univ
end
lemma deriv_within.comp
(hh₁ : differentiable_within_at 𝕜 h₁ t (h₂ x)) (hh₂ : differentiable_within_at 𝕜 h₂ s x)
(hs : s ⊆ h₂ ⁻¹' t) (hxs : unique_diff_within_at 𝕜 s x) :
deriv_within (h₁ ∘ h₂) s x = deriv_within h₁ t (h₂ x) * deriv_within h₂ s x :=
begin
apply has_deriv_within_at.deriv_within _ hxs,
exact has_deriv_within_at.comp x (hh₁.has_deriv_within_at) (hh₂.has_deriv_within_at) hs
end
lemma deriv.comp
(hh₁ : differentiable_at 𝕜 h₁ (h₂ x)) (hh₂ : differentiable_at 𝕜 h₂ x) :
deriv (h₁ ∘ h₂) x = deriv h₁ (h₂ x) * deriv h₂ x :=
begin
apply has_deriv_at.deriv,
exact has_deriv_at.comp x hh₁.has_deriv_at hh₂.has_deriv_at
end
protected lemma has_deriv_at_filter.iterate {f : 𝕜 → 𝕜} {f' : 𝕜}
(hf : has_deriv_at_filter f f' x L) (hL : tendsto f L L) (hx : f x = x) (n : ℕ) :
has_deriv_at_filter (f^[n]) (f'^n) x L :=
begin
have := hf.iterate hL hx n,
rwa [continuous_linear_map.smul_right_one_pow] at this
end
protected lemma has_deriv_at.iterate {f : 𝕜 → 𝕜} {f' : 𝕜}
(hf : has_deriv_at f f' x) (hx : f x = x) (n : ℕ) :
has_deriv_at (f^[n]) (f'^n) x :=
begin
have := has_fderiv_at.iterate hf hx n,
rwa [continuous_linear_map.smul_right_one_pow] at this
end
protected lemma has_deriv_within_at.iterate {f : 𝕜 → 𝕜} {f' : 𝕜}
(hf : has_deriv_within_at f f' s x) (hx : f x = x) (hs : maps_to f s s) (n : ℕ) :
has_deriv_within_at (f^[n]) (f'^n) s x :=
begin
have := has_fderiv_within_at.iterate hf hx hs n,
rwa [continuous_linear_map.smul_right_one_pow] at this
end
protected lemma has_strict_deriv_at.iterate {f : 𝕜 → 𝕜} {f' : 𝕜}
(hf : has_strict_deriv_at f f' x) (hx : f x = x) (n : ℕ) :
has_strict_deriv_at (f^[n]) (f'^n) x :=
begin
have := hf.iterate hx n,
rwa [continuous_linear_map.smul_right_one_pow] at this
end
end composition
section composition_vector
/-! ### Derivative of the composition of a function between vector spaces and of a function defined on `𝕜` -/
variables {l : F → E} {l' : F →L[𝕜] E}
variable (x)
/-- The composition `l ∘ f` where `l : F → E` and `f : 𝕜 → F`, has a derivative within a set
equal to the Fréchet derivative of `l` applied to the derivative of `f`. -/
theorem has_fderiv_within_at.comp_has_deriv_within_at {t : set F}
(hl : has_fderiv_within_at l l' t (f x)) (hf : has_deriv_within_at f f' s x) (hst : s ⊆ f ⁻¹' t) :
has_deriv_within_at (l ∘ f) (l' (f')) s x :=
begin
rw has_deriv_within_at_iff_has_fderiv_within_at,
convert has_fderiv_within_at.comp x hl hf hst,
ext,
simp
end
/-- The composition `l ∘ f` where `l : F → E` and `f : 𝕜 → F`, has a derivative equal to the
Fréchet derivative of `l` applied to the derivative of `f`. -/
theorem has_fderiv_at.comp_has_deriv_at
(hl : has_fderiv_at l l' (f x)) (hf : has_deriv_at f f' x) :
has_deriv_at (l ∘ f) (l' (f')) x :=
begin
rw has_deriv_at_iff_has_fderiv_at,
convert has_fderiv_at.comp x hl hf,
ext,
simp
end
theorem has_fderiv_at.comp_has_deriv_within_at
(hl : has_fderiv_at l l' (f x)) (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (l ∘ f) (l' (f')) s x :=
begin
rw ← has_fderiv_within_at_univ at hl,
exact has_fderiv_within_at.comp_has_deriv_within_at x hl hf subset_preimage_univ
end
lemma fderiv_within.comp_deriv_within {t : set F}
(hl : differentiable_within_at 𝕜 l t (f x)) (hf : differentiable_within_at 𝕜 f s x)
(hs : s ⊆ f ⁻¹' t) (hxs : unique_diff_within_at 𝕜 s x) :
deriv_within (l ∘ f) s x = (fderiv_within 𝕜 l t (f x) : F → E) (deriv_within f s x) :=
begin
apply has_deriv_within_at.deriv_within _ hxs,
exact (hl.has_fderiv_within_at).comp_has_deriv_within_at x (hf.has_deriv_within_at) hs
end
lemma fderiv.comp_deriv
(hl : differentiable_at 𝕜 l (f x)) (hf : differentiable_at 𝕜 f x) :
deriv (l ∘ f) x = (fderiv 𝕜 l (f x) : F → E) (deriv f x) :=
begin
apply has_deriv_at.deriv _,
exact (hl.has_fderiv_at).comp_has_deriv_at x (hf.has_deriv_at)
end
end composition_vector
section mul
/-! ### Derivative of the multiplication of two scalar functions -/
variables {c d : 𝕜 → 𝕜} {c' d' : 𝕜}
theorem has_deriv_within_at.mul
(hc : has_deriv_within_at c c' s x) (hd : has_deriv_within_at d d' s x) :
has_deriv_within_at (λ y, c y * d y) (c' * d x + c x * d') s x :=
begin
convert hc.smul hd using 1,
rw [smul_eq_mul, smul_eq_mul, add_comm]
end
theorem has_deriv_at.mul (hc : has_deriv_at c c' x) (hd : has_deriv_at d d' x) :
has_deriv_at (λ y, c y * d y) (c' * d x + c x * d') x :=
begin
rw [← has_deriv_within_at_univ] at *,
exact hc.mul hd
end
theorem has_strict_deriv_at.mul
(hc : has_strict_deriv_at c c' x) (hd : has_strict_deriv_at d d' x) :
has_strict_deriv_at (λ y, c y * d y) (c' * d x + c x * d') x :=
begin
convert hc.smul hd using 1,
rw [smul_eq_mul, smul_eq_mul, add_comm]
end
lemma deriv_within_mul (hxs : unique_diff_within_at 𝕜 s x)
(hc : differentiable_within_at 𝕜 c s x) (hd : differentiable_within_at 𝕜 d s x) :
deriv_within (λ y, c y * d y) s x = deriv_within c s x * d x + c x * deriv_within d s x :=
(hc.has_deriv_within_at.mul hd.has_deriv_within_at).deriv_within hxs
@[simp] lemma deriv_mul (hc : differentiable_at 𝕜 c x) (hd : differentiable_at 𝕜 d x) :
deriv (λ y, c y * d y) x = deriv c x * d x + c x * deriv d x :=
(hc.has_deriv_at.mul hd.has_deriv_at).deriv
theorem has_deriv_within_at.mul_const (hc : has_deriv_within_at c c' s x) (d : 𝕜) :
has_deriv_within_at (λ y, c y * d) (c' * d) s x :=
begin
convert hc.mul (has_deriv_within_at_const x s d),
rw [mul_zero, add_zero]
end
theorem has_deriv_at.mul_const (hc : has_deriv_at c c' x) (d : 𝕜) :
has_deriv_at (λ y, c y * d) (c' * d) x :=
begin
rw [← has_deriv_within_at_univ] at *,
exact hc.mul_const d
end
lemma deriv_within_mul_const (hxs : unique_diff_within_at 𝕜 s x)
(hc : differentiable_within_at 𝕜 c s x) (d : 𝕜) :
deriv_within (λ y, c y * d) s x = deriv_within c s x * d :=
(hc.has_deriv_within_at.mul_const d).deriv_within hxs
lemma deriv_mul_const (hc : differentiable_at 𝕜 c x) (d : 𝕜) :
deriv (λ y, c y * d) x = deriv c x * d :=
(hc.has_deriv_at.mul_const d).deriv
theorem has_deriv_within_at.const_mul (c : 𝕜) (hd : has_deriv_within_at d d' s x) :
has_deriv_within_at (λ y, c * d y) (c * d') s x :=
begin
convert (has_deriv_within_at_const x s c).mul hd,
rw [zero_mul, zero_add]
end
theorem has_deriv_at.const_mul (c : 𝕜) (hd : has_deriv_at d d' x) :
has_deriv_at (λ y, c * d y) (c * d') x :=
begin
rw [← has_deriv_within_at_univ] at *,
exact hd.const_mul c
end
lemma deriv_within_const_mul (hxs : unique_diff_within_at 𝕜 s x)
(c : 𝕜) (hd : differentiable_within_at 𝕜 d s x) :
deriv_within (λ y, c * d y) s x = c * deriv_within d s x :=
(hd.has_deriv_within_at.const_mul c).deriv_within hxs
lemma deriv_const_mul (c : 𝕜) (hd : differentiable_at 𝕜 d x) :
deriv (λ y, c * d y) x = c * deriv d x :=
(hd.has_deriv_at.const_mul c).deriv
end mul
section inverse
/-! ### Derivative of `x ↦ x⁻¹` -/
theorem has_strict_deriv_at_inv (hx : x ≠ 0) : has_strict_deriv_at has_inv.inv (-(x^2)⁻¹) x :=
begin
suffices : is_o (λ p : 𝕜 × 𝕜, (p.1 - p.2) * ((x * x)⁻¹ - (p.1 * p.2)⁻¹))
(λ (p : 𝕜 × 𝕜), (p.1 - p.2) * 1) (𝓝 (x, x)),
{ refine this.congr' _ (eventually_of_forall $ λ _, mul_one _),
refine eventually.mono (mem_nhds_sets (is_open_prod is_open_ne is_open_ne) ⟨hx, hx⟩) _,
rintro ⟨y, z⟩ ⟨hy, hz⟩,
simp only [mem_set_of_eq] at hy hz, -- hy : y ≠ 0, hz : z ≠ 0
field_simp [hx, hy, hz], ring, },
refine (is_O_refl (λ p : 𝕜 × 𝕜, p.1 - p.2) _).mul_is_o ((is_o_one_iff _).2 _),
rw [← sub_self (x * x)⁻¹],
exact tendsto_const_nhds.sub ((continuous_mul.tendsto (x, x)).inv' $ mul_ne_zero hx hx)
end
theorem has_deriv_at_inv (x_ne_zero : x ≠ 0) :
has_deriv_at (λy, y⁻¹) (-(x^2)⁻¹) x :=
(has_strict_deriv_at_inv x_ne_zero).has_deriv_at
theorem has_deriv_within_at_inv (x_ne_zero : x ≠ 0) (s : set 𝕜) :
has_deriv_within_at (λx, x⁻¹) (-(x^2)⁻¹) s x :=
(has_deriv_at_inv x_ne_zero).has_deriv_within_at
lemma differentiable_at_inv (x_ne_zero : x ≠ 0) :
differentiable_at 𝕜 (λx, x⁻¹) x :=
(has_deriv_at_inv x_ne_zero).differentiable_at
lemma differentiable_within_at_inv (x_ne_zero : x ≠ 0) :
differentiable_within_at 𝕜 (λx, x⁻¹) s x :=
(differentiable_at_inv x_ne_zero).differentiable_within_at
lemma differentiable_on_inv : differentiable_on 𝕜 (λx:𝕜, x⁻¹) {x | x ≠ 0} :=
λx hx, differentiable_within_at_inv hx
lemma deriv_inv (x_ne_zero : x ≠ 0) :
deriv (λx, x⁻¹) x = -(x^2)⁻¹ :=
(has_deriv_at_inv x_ne_zero).deriv
lemma deriv_within_inv (x_ne_zero : x ≠ 0) (hxs : unique_diff_within_at 𝕜 s x) :
deriv_within (λx, x⁻¹) s x = -(x^2)⁻¹ :=
begin
rw differentiable_at.deriv_within (differentiable_at_inv x_ne_zero) hxs,
exact deriv_inv x_ne_zero
end
lemma has_fderiv_at_inv (x_ne_zero : x ≠ 0) :
has_fderiv_at (λx, x⁻¹) (smul_right 1 (-(x^2)⁻¹) : 𝕜 →L[𝕜] 𝕜) x :=
has_deriv_at_inv x_ne_zero
lemma has_fderiv_within_at_inv (x_ne_zero : x ≠ 0) :
has_fderiv_within_at (λx, x⁻¹) (smul_right 1 (-(x^2)⁻¹) : 𝕜 →L[𝕜] 𝕜) s x :=
(has_fderiv_at_inv x_ne_zero).has_fderiv_within_at
lemma fderiv_inv (x_ne_zero : x ≠ 0) :
fderiv 𝕜 (λx, x⁻¹) x = smul_right 1 (-(x^2)⁻¹) :=
(has_fderiv_at_inv x_ne_zero).fderiv
lemma fderiv_within_inv (x_ne_zero : x ≠ 0) (hxs : unique_diff_within_at 𝕜 s x) :
fderiv_within 𝕜 (λx, x⁻¹) s x = smul_right 1 (-(x^2)⁻¹) :=
begin
rw differentiable_at.fderiv_within (differentiable_at_inv x_ne_zero) hxs,
exact fderiv_inv x_ne_zero
end
variables {c : 𝕜 → 𝕜} {c' : 𝕜}
lemma has_deriv_within_at.inv
(hc : has_deriv_within_at c c' s x) (hx : c x ≠ 0) :
has_deriv_within_at (λ y, (c y)⁻¹) (- c' / (c x)^2) s x :=
begin
convert (has_deriv_at_inv hx).comp_has_deriv_within_at x hc,
field_simp
end
lemma has_deriv_at.inv (hc : has_deriv_at c c' x) (hx : c x ≠ 0) :
has_deriv_at (λ y, (c y)⁻¹) (- c' / (c x)^2) x :=
begin
rw ← has_deriv_within_at_univ at *,
exact hc.inv hx
end
lemma differentiable_within_at.inv (hc : differentiable_within_at 𝕜 c s x) (hx : c x ≠ 0) :
differentiable_within_at 𝕜 (λx, (c x)⁻¹) s x :=
(hc.has_deriv_within_at.inv hx).differentiable_within_at
@[simp] lemma differentiable_at.inv (hc : differentiable_at 𝕜 c x) (hx : c x ≠ 0) :
differentiable_at 𝕜 (λx, (c x)⁻¹) x :=
(hc.has_deriv_at.inv hx).differentiable_at
lemma differentiable_on.inv (hc : differentiable_on 𝕜 c s) (hx : ∀ x ∈ s, c x ≠ 0) :
differentiable_on 𝕜 (λx, (c x)⁻¹) s :=
λx h, (hc x h).inv (hx x h)
@[simp] lemma differentiable.inv (hc : differentiable 𝕜 c) (hx : ∀ x, c x ≠ 0) :
differentiable 𝕜 (λx, (c x)⁻¹) :=
λx, (hc x).inv (hx x)
lemma deriv_within_inv' (hc : differentiable_within_at 𝕜 c s x) (hx : c x ≠ 0)
(hxs : unique_diff_within_at 𝕜 s x) :
deriv_within (λx, (c x)⁻¹) s x = - (deriv_within c s x) / (c x)^2 :=
(hc.has_deriv_within_at.inv hx).deriv_within hxs
@[simp] lemma deriv_inv' (hc : differentiable_at 𝕜 c x) (hx : c x ≠ 0) :
deriv (λx, (c x)⁻¹) x = - (deriv c x) / (c x)^2 :=
(hc.has_deriv_at.inv hx).deriv
end inverse
section division
/-! ### Derivative of `x ↦ c x / d x` -/
variables {c d : 𝕜 → 𝕜} {c' d' : 𝕜}
lemma has_deriv_within_at.div
(hc : has_deriv_within_at c c' s x) (hd : has_deriv_within_at d d' s x) (hx : d x ≠ 0) :
has_deriv_within_at (λ y, c y / d y) ((c' * d x - c x * d') / (d x)^2) s x :=
begin
have A : (d x)⁻¹ * (d x)⁻¹ * (c' * d x) = (d x)⁻¹ * c',
by rw [← mul_assoc, mul_comm, ← mul_assoc, ← mul_assoc, mul_inv_cancel hx, one_mul],
convert hc.mul ((has_deriv_at_inv hx).comp_has_deriv_within_at x hd),
simp [div_eq_inv_mul, pow_two, mul_inv', mul_add, A, sub_eq_add_neg],
ring
end
lemma has_deriv_at.div (hc : has_deriv_at c c' x) (hd : has_deriv_at d d' x) (hx : d x ≠ 0) :
has_deriv_at (λ y, c y / d y) ((c' * d x - c x * d') / (d x)^2) x :=
begin
rw ← has_deriv_within_at_univ at *,
exact hc.div hd hx
end
lemma differentiable_within_at.div
(hc : differentiable_within_at 𝕜 c s x) (hd : differentiable_within_at 𝕜 d s x) (hx : d x ≠ 0) :
differentiable_within_at 𝕜 (λx, c x / d x) s x :=
((hc.has_deriv_within_at).div (hd.has_deriv_within_at) hx).differentiable_within_at
@[simp] lemma differentiable_at.div
(hc : differentiable_at 𝕜 c x) (hd : differentiable_at 𝕜 d x) (hx : d x ≠ 0) :
differentiable_at 𝕜 (λx, c x / d x) x :=
((hc.has_deriv_at).div (hd.has_deriv_at) hx).differentiable_at
lemma differentiable_on.div
(hc : differentiable_on 𝕜 c s) (hd : differentiable_on 𝕜 d s) (hx : ∀ x ∈ s, d x ≠ 0) :
differentiable_on 𝕜 (λx, c x / d x) s :=
λx h, (hc x h).div (hd x h) (hx x h)
@[simp] lemma differentiable.div
(hc : differentiable 𝕜 c) (hd : differentiable 𝕜 d) (hx : ∀ x, d x ≠ 0) :
differentiable 𝕜 (λx, c x / d x) :=
λx, (hc x).div (hd x) (hx x)
lemma deriv_within_div
(hc : differentiable_within_at 𝕜 c s x) (hd : differentiable_within_at 𝕜 d s x) (hx : d x ≠ 0)
(hxs : unique_diff_within_at 𝕜 s x) :
deriv_within (λx, c x / d x) s x
= ((deriv_within c s x) * d x - c x * (deriv_within d s x)) / (d x)^2 :=
((hc.has_deriv_within_at).div (hd.has_deriv_within_at) hx).deriv_within hxs
@[simp] lemma deriv_div
(hc : differentiable_at 𝕜 c x) (hd : differentiable_at 𝕜 d x) (hx : d x ≠ 0) :
deriv (λx, c x / d x) x = ((deriv c x) * d x - c x * (deriv d x)) / (d x)^2 :=
((hc.has_deriv_at).div (hd.has_deriv_at) hx).deriv
lemma differentiable_within_at.div_const (hc : differentiable_within_at 𝕜 c s x) {d : 𝕜} :
differentiable_within_at 𝕜 (λx, c x / d) s x :=
by simp [div_eq_inv_mul, differentiable_within_at.const_mul, hc]
@[simp] lemma differentiable_at.div_const (hc : differentiable_at 𝕜 c x) {d : 𝕜} :
differentiable_at 𝕜 (λ x, c x / d) x :=
by simp [div_eq_inv_mul, hc]
lemma differentiable_on.div_const (hc : differentiable_on 𝕜 c s) {d : 𝕜} :
differentiable_on 𝕜 (λx, c x / d) s :=
by simp [div_eq_inv_mul, differentiable_on.const_mul, hc]
@[simp] lemma differentiable.div_const (hc : differentiable 𝕜 c) {d : 𝕜} :
differentiable 𝕜 (λx, c x / d) :=
by simp [div_eq_inv_mul, differentiable.const_mul, hc]
lemma deriv_within_div_const (hc : differentiable_within_at 𝕜 c s x) {d : 𝕜}
(hxs : unique_diff_within_at 𝕜 s x) :
deriv_within (λx, c x / d) s x = (deriv_within c s x) / d :=
by simp [div_eq_inv_mul, deriv_within_const_mul, hc, hxs]
@[simp] lemma deriv_div_const (hc : differentiable_at 𝕜 c x) {d : 𝕜} :
deriv (λx, c x / d) x = (deriv c x) / d :=
by simp [div_eq_inv_mul, deriv_const_mul, hc]
end division
theorem has_strict_deriv_at.has_strict_fderiv_at_equiv {f : 𝕜 → 𝕜} {f' x : 𝕜}
(hf : has_strict_deriv_at f f' x) (hf' : f' ≠ 0) :
has_strict_fderiv_at f
(continuous_linear_equiv.units_equiv_aut 𝕜 (units.mk0 f' hf') : 𝕜 →L[𝕜] 𝕜) x :=
hf
theorem has_deriv_at.has_fderiv_at_equiv {f : 𝕜 → 𝕜} {f' x : 𝕜}
(hf : has_deriv_at f f' x) (hf' : f' ≠ 0) :
has_fderiv_at f
(continuous_linear_equiv.units_equiv_aut 𝕜 (units.mk0 f' hf') : 𝕜 →L[𝕜] 𝕜) x :=
hf
/-- If `f (g y) = y` for `y` in some neighborhood of `a`, `g` is continuous at `a`, and `f` has an
invertible derivative `f'` at `g a` in the strict sense, then `g` has the derivative `f'⁻¹` at `a`
in the strict sense.
This is one of the easy parts of the inverse function theorem: it assumes that we already have an
inverse function. -/
theorem has_strict_deriv_at.of_local_left_inverse {f g : 𝕜 → 𝕜} {f' a : 𝕜}
(hg : continuous_at g a) (hf : has_strict_deriv_at f f' (g a)) (hf' : f' ≠ 0)
(hfg : ∀ᶠ y in 𝓝 a, f (g y) = y) :
has_strict_deriv_at g f'⁻¹ a :=
(hf.has_strict_fderiv_at_equiv hf').of_local_left_inverse hg hfg
/-- If `f (g y) = y` for `y` in some neighborhood of `a`, `g` is continuous at `a`, and `f` has an
invertible derivative `f'` at `g a`, then `g` has the derivative `f'⁻¹` at `a`.
This is one of the easy parts of the inverse function theorem: it assumes that we already have
an inverse function. -/
theorem has_deriv_at.of_local_left_inverse {f g : 𝕜 → 𝕜} {f' a : 𝕜}
(hg : continuous_at g a) (hf : has_deriv_at f f' (g a)) (hf' : f' ≠ 0)
(hfg : ∀ᶠ y in 𝓝 a, f (g y) = y) :
has_deriv_at g f'⁻¹ a :=
(hf.has_fderiv_at_equiv hf').of_local_left_inverse hg hfg
end
namespace polynomial
/-! ### Derivative of a polynomial -/
variables {x : 𝕜} {s : set 𝕜}
variable (p : polynomial 𝕜)
/-- The derivative (in the analysis sense) of a polynomial `p` is given by `p.derivative`. -/
protected lemma has_strict_deriv_at (x : 𝕜) :
has_strict_deriv_at (λx, p.eval x) (p.derivative.eval x) x :=
begin
apply p.induction_on,
{ simp [has_strict_deriv_at_const] },
{ assume p q hp hq,
convert hp.add hq;
simp },
{ assume n a h,
convert h.mul (has_strict_deriv_at_id x),
{ ext y, simp [pow_add, mul_assoc] },
{ simp [pow_add], ring } }
end
/-- The derivative (in the analysis sense) of a polynomial `p` is given by `p.derivative`. -/
protected lemma has_deriv_at (x : 𝕜) : has_deriv_at (λx, p.eval x) (p.derivative.eval x) x :=
(p.has_strict_deriv_at x).has_deriv_at
protected theorem has_deriv_within_at (x : 𝕜) (s : set 𝕜) :
has_deriv_within_at (λx, p.eval x) (p.derivative.eval x) s x :=
(p.has_deriv_at x).has_deriv_within_at
protected lemma differentiable_at : differentiable_at 𝕜 (λx, p.eval x) x :=
(p.has_deriv_at x).differentiable_at
protected lemma differentiable_within_at : differentiable_within_at 𝕜 (λx, p.eval x) s x :=
p.differentiable_at.differentiable_within_at
protected lemma differentiable : differentiable 𝕜 (λx, p.eval x) :=
λx, p.differentiable_at
protected lemma differentiable_on : differentiable_on 𝕜 (λx, p.eval x) s :=
p.differentiable.differentiable_on
@[simp] protected lemma deriv : deriv (λx, p.eval x) x = p.derivative.eval x :=
(p.has_deriv_at x).deriv
protected lemma deriv_within (hxs : unique_diff_within_at 𝕜 s x) :
deriv_within (λx, p.eval x) s x = p.derivative.eval x :=
begin
rw differentiable_at.deriv_within p.differentiable_at hxs,
exact p.deriv
end
protected lemma continuous : continuous (λx, p.eval x) :=
p.differentiable.continuous
protected lemma continuous_on : continuous_on (λx, p.eval x) s :=
p.continuous.continuous_on
protected lemma continuous_at : continuous_at (λx, p.eval x) x :=
p.continuous.continuous_at
protected lemma continuous_within_at : continuous_within_at (λx, p.eval x) s x :=
p.continuous_at.continuous_within_at
protected lemma has_fderiv_at (x : 𝕜) :
has_fderiv_at (λx, p.eval x) (smul_right 1 (p.derivative.eval x) : 𝕜 →L[𝕜] 𝕜) x :=
by simpa [has_deriv_at_iff_has_fderiv_at] using p.has_deriv_at x
protected lemma has_fderiv_within_at (x : 𝕜) :
has_fderiv_within_at (λx, p.eval x) (smul_right 1 (p.derivative.eval x) : 𝕜 →L[𝕜] 𝕜) s x :=
(p.has_fderiv_at x).has_fderiv_within_at
@[simp] protected lemma fderiv : fderiv 𝕜 (λx, p.eval x) x = smul_right 1 (p.derivative.eval x) :=
(p.has_fderiv_at x).fderiv
protected lemma fderiv_within (hxs : unique_diff_within_at 𝕜 s x) :
fderiv_within 𝕜 (λx, p.eval x) s x = smul_right 1 (p.derivative.eval x) :=
begin
rw differentiable_at.fderiv_within p.differentiable_at hxs,
exact p.fderiv
end
end polynomial
section pow
/-! ### Derivative of `x ↦ x^n` for `n : ℕ` -/
variables {x : 𝕜} {s : set 𝕜} {c : 𝕜 → 𝕜} {c' : 𝕜}
variable {n : ℕ }
lemma has_strict_deriv_at_pow (n : ℕ) (x : 𝕜) :
has_strict_deriv_at (λx, x^n) ((n : 𝕜) * x^(n-1)) x :=
begin
convert (polynomial.C (1 : 𝕜) * (polynomial.X)^n).has_strict_deriv_at x,
{ simp },
{ rw [polynomial.derivative_monomial], simp }
end
lemma has_deriv_at_pow (n : ℕ) (x : 𝕜) : has_deriv_at (λx, x^n) ((n : 𝕜) * x^(n-1)) x :=
(has_strict_deriv_at_pow n x).has_deriv_at
theorem has_deriv_within_at_pow (n : ℕ) (x : 𝕜) (s : set 𝕜) :
has_deriv_within_at (λx, x^n) ((n : 𝕜) * x^(n-1)) s x :=
(has_deriv_at_pow n x).has_deriv_within_at
lemma differentiable_at_pow : differentiable_at 𝕜 (λx, x^n) x :=
(has_deriv_at_pow n x).differentiable_at
lemma differentiable_within_at_pow : differentiable_within_at 𝕜 (λx, x^n) s x :=
differentiable_at_pow.differentiable_within_at
lemma differentiable_pow : differentiable 𝕜 (λx:𝕜, x^n) :=
λx, differentiable_at_pow
lemma differentiable_on_pow : differentiable_on 𝕜 (λx, x^n) s :=
differentiable_pow.differentiable_on
lemma deriv_pow : deriv (λx, x^n) x = (n : 𝕜) * x^(n-1) :=
(has_deriv_at_pow n x).deriv
@[simp] lemma deriv_pow' : deriv (λx, x^n) = λ x, (n : 𝕜) * x^(n-1) :=
funext $ λ x, deriv_pow
lemma deriv_within_pow (hxs : unique_diff_within_at 𝕜 s x) :
deriv_within (λx, x^n) s x = (n : 𝕜) * x^(n-1) :=
(has_deriv_within_at_pow n x s).deriv_within hxs
lemma iter_deriv_pow' {k : ℕ} :
deriv^[k] (λx:𝕜, x^n) = λ x, (∏ i in finset.range k, (n - i) : ℕ) * x^(n-k) :=
begin
induction k with k ihk,
{ simp only [one_mul, finset.prod_range_zero, function.iterate_zero_apply, nat.sub_zero,
nat.cast_one] },
{ simp only [function.iterate_succ_apply', ihk, finset.prod_range_succ],
ext x,
rw [((has_deriv_at_pow (n - k) x).const_mul _).deriv, nat.cast_mul, mul_left_comm, mul_assoc,
nat.succ_eq_add_one, nat.sub_sub] }
end
lemma iter_deriv_pow {k : ℕ} :
deriv^[k] (λx:𝕜, x^n) x = (∏ i in finset.range k, (n - i) : ℕ) * x^(n-k) :=
congr_fun iter_deriv_pow' x
lemma has_deriv_within_at.pow (hc : has_deriv_within_at c c' s x) :
has_deriv_within_at (λ y, (c y)^n) ((n : 𝕜) * (c x)^(n-1) * c') s x :=
(has_deriv_at_pow n (c x)).comp_has_deriv_within_at x hc
lemma has_deriv_at.pow (hc : has_deriv_at c c' x) :
has_deriv_at (λ y, (c y)^n) ((n : 𝕜) * (c x)^(n-1) * c') x :=
by { rw ← has_deriv_within_at_univ at *, exact hc.pow }
lemma differentiable_within_at.pow (hc : differentiable_within_at 𝕜 c s x) :
differentiable_within_at 𝕜 (λx, (c x)^n) s x :=
hc.has_deriv_within_at.pow.differentiable_within_at
@[simp] lemma differentiable_at.pow (hc : differentiable_at 𝕜 c x) :
differentiable_at 𝕜 (λx, (c x)^n) x :=
hc.has_deriv_at.pow.differentiable_at
lemma differentiable_on.pow (hc : differentiable_on 𝕜 c s) :
differentiable_on 𝕜 (λx, (c x)^n) s :=
λx h, (hc x h).pow
@[simp] lemma differentiable.pow (hc : differentiable 𝕜 c) :
differentiable 𝕜 (λx, (c x)^n) :=
λx, (hc x).pow
lemma deriv_within_pow' (hc : differentiable_within_at 𝕜 c s x)
(hxs : unique_diff_within_at 𝕜 s x) :
deriv_within (λx, (c x)^n) s x = (n : 𝕜) * (c x)^(n-1) * (deriv_within c s x) :=
hc.has_deriv_within_at.pow.deriv_within hxs
@[simp] lemma deriv_pow'' (hc : differentiable_at 𝕜 c x) :
deriv (λx, (c x)^n) x = (n : 𝕜) * (c x)^(n-1) * (deriv c x) :=
hc.has_deriv_at.pow.deriv
end pow
section fpow
/-! ### Derivative of `x ↦ x^m` for `m : ℤ` -/
variables {x : 𝕜} {s : set 𝕜}
variable {m : ℤ}
lemma has_strict_deriv_at_fpow (m : ℤ) (hx : x ≠ 0) :
has_strict_deriv_at (λx, x^m) ((m : 𝕜) * x^(m-1)) x :=
begin
have : ∀ m : ℤ, 0 < m → has_strict_deriv_at (λx, x^m) ((m:𝕜) * x^(m-1)) x,
{ assume m hm,
lift m to ℕ using (le_of_lt hm),
simp only [fpow_of_nat, int.cast_coe_nat],
convert has_strict_deriv_at_pow _ _ using 2,
rw [← int.coe_nat_one, ← int.coe_nat_sub, fpow_coe_nat],
norm_cast at hm,
exact nat.succ_le_of_lt hm },
rcases lt_trichotomy m 0 with hm|hm|hm,
{ have := (has_strict_deriv_at_inv _).scomp _ (this (-m) (neg_pos.2 hm));
[skip, exact fpow_ne_zero_of_ne_zero hx _],
simp only [(∘), fpow_neg, one_div, inv_inv', smul_eq_mul] at this,
convert this using 1,
rw [pow_two, mul_inv', inv_inv', int.cast_neg, ← neg_mul_eq_neg_mul, neg_mul_neg,
← fpow_add hx, mul_assoc, ← fpow_add hx], congr, abel },
{ simp only [hm, fpow_zero, int.cast_zero, zero_mul, has_strict_deriv_at_const] },
{ exact this m hm }
end
lemma has_deriv_at_fpow (m : ℤ) (hx : x ≠ 0) :
has_deriv_at (λx, x^m) ((m : 𝕜) * x^(m-1)) x :=
(has_strict_deriv_at_fpow m hx).has_deriv_at
theorem has_deriv_within_at_fpow (m : ℤ) (hx : x ≠ 0) (s : set 𝕜) :
has_deriv_within_at (λx, x^m) ((m : 𝕜) * x^(m-1)) s x :=
(has_deriv_at_fpow m hx).has_deriv_within_at
lemma differentiable_at_fpow (hx : x ≠ 0) : differentiable_at 𝕜 (λx, x^m) x :=
(has_deriv_at_fpow m hx).differentiable_at
lemma differentiable_within_at_fpow (hx : x ≠ 0) :
differentiable_within_at 𝕜 (λx, x^m) s x :=
(differentiable_at_fpow hx).differentiable_within_at
lemma differentiable_on_fpow (hs : (0:𝕜) ∉ s) : differentiable_on 𝕜 (λx, x^m) s :=
λ x hxs, differentiable_within_at_fpow (λ hx, hs $ hx ▸ hxs)
-- TODO : this is true at `x=0` as well
lemma deriv_fpow (hx : x ≠ 0) : deriv (λx, x^m) x = (m : 𝕜) * x^(m-1) :=
(has_deriv_at_fpow m hx).deriv
lemma deriv_within_fpow (hxs : unique_diff_within_at 𝕜 s x) (hx : x ≠ 0) :
deriv_within (λx, x^m) s x = (m : 𝕜) * x^(m-1) :=
(has_deriv_within_at_fpow m hx s).deriv_within hxs
lemma iter_deriv_fpow {k : ℕ} (hx : x ≠ 0) :
deriv^[k] (λx:𝕜, x^m) x = (∏ i in finset.range k, (m - i) : ℤ) * x^(m-k) :=
begin
induction k with k ihk generalizing x hx,
{ simp only [one_mul, finset.prod_range_zero, function.iterate_zero_apply, int.coe_nat_zero,
sub_zero, int.cast_one] },
{ rw [function.iterate_succ', finset.prod_range_succ, int.cast_mul, mul_assoc, mul_left_comm,
int.coe_nat_succ, ← sub_sub, ← ((has_deriv_at_fpow _ hx).const_mul _).deriv],
exact filter.eventually_eq.deriv_eq (eventually.mono (mem_nhds_sets is_open_ne hx) @ihk) }
end
end fpow
/-! ### Upper estimates on liminf and limsup -/
section real
variables {f : ℝ → ℝ} {f' : ℝ} {s : set ℝ} {x : ℝ} {r : ℝ}
lemma has_deriv_within_at.limsup_slope_le (hf : has_deriv_within_at f f' s x) (hr : f' < r) :
∀ᶠ z in 𝓝[s \ {x}] x, (z - x)⁻¹ * (f z - f x) < r :=
has_deriv_within_at_iff_tendsto_slope.1 hf (mem_nhds_sets is_open_Iio hr)
lemma has_deriv_within_at.limsup_slope_le' (hf : has_deriv_within_at f f' s x)
(hs : x ∉ s) (hr : f' < r) :
∀ᶠ z in 𝓝[s] x, (z - x)⁻¹ * (f z - f x) < r :=
(has_deriv_within_at_iff_tendsto_slope' hs).1 hf (mem_nhds_sets is_open_Iio hr)
lemma has_deriv_within_at.liminf_right_slope_le
(hf : has_deriv_within_at f f' (Ioi x) x) (hr : f' < r) :
∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (f z - f x) < r :=
(hf.limsup_slope_le' (lt_irrefl x) hr).frequently
end real
section real_space
open metric
variables {E : Type u} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {f' : E} {s : set ℝ}
{x r : ℝ}
/-- If `f` has derivative `f'` within `s` at `x`, then for any `r > ∥f'∥` the ratio
`∥f z - f x∥ / ∥z - x∥` is less than `r` in some neighborhood of `x` within `s`.
In other words, the limit superior of this ratio as `z` tends to `x` along `s`
is less than or equal to `∥f'∥`. -/
lemma has_deriv_within_at.limsup_norm_slope_le
(hf : has_deriv_within_at f f' s x) (hr : ∥f'∥ < r) :
∀ᶠ z in 𝓝[s] x, ∥z - x∥⁻¹ * ∥f z - f x∥ < r :=
begin
have hr₀ : 0 < r, from lt_of_le_of_lt (norm_nonneg f') hr,
have A : ∀ᶠ z in 𝓝[s \ {x}] x, ∥(z - x)⁻¹ • (f z - f x)∥ ∈ Iio r,
from (has_deriv_within_at_iff_tendsto_slope.1 hf).norm (mem_nhds_sets is_open_Iio hr),
have B : ∀ᶠ z in 𝓝[{x}] x, ∥(z - x)⁻¹ • (f z - f x)∥ ∈ Iio r,
from mem_sets_of_superset self_mem_nhds_within
(singleton_subset_iff.2 $ by simp [hr₀]),
have C := mem_sup_sets.2 ⟨A, B⟩,
rw [← nhds_within_union, diff_union_self, nhds_within_union, mem_sup_sets] at C,
filter_upwards [C.1],
simp only [mem_set_of_eq, norm_smul, mem_Iio, normed_field.norm_inv],
exact λ _, id
end
/-- If `f` has derivative `f'` within `s` at `x`, then for any `r > ∥f'∥` the ratio
`(∥f z∥ - ∥f x∥) / ∥z - x∥` is less than `r` in some neighborhood of `x` within `s`.
In other words, the limit superior of this ratio as `z` tends to `x` along `s`
is less than or equal to `∥f'∥`.
This lemma is a weaker version of `has_deriv_within_at.limsup_norm_slope_le`
where `∥f z∥ - ∥f x∥` is replaced by `∥f z - f x∥`. -/
lemma has_deriv_within_at.limsup_slope_norm_le
(hf : has_deriv_within_at f f' s x) (hr : ∥f'∥ < r) :
∀ᶠ z in 𝓝[s] x, ∥z - x∥⁻¹ * (∥f z∥ - ∥f x∥) < r :=
begin
apply (hf.limsup_norm_slope_le hr).mono,
assume z hz,
refine lt_of_le_of_lt (mul_le_mul_of_nonneg_left (norm_sub_norm_le _ _) _) hz,
exact inv_nonneg.2 (norm_nonneg _)
end
/-- If `f` has derivative `f'` within `(x, +∞)` at `x`, then for any `r > ∥f'∥` the ratio
`∥f z - f x∥ / ∥z - x∥` is frequently less than `r` as `z → x+0`.
In other words, the limit inferior of this ratio as `z` tends to `x+0`
is less than or equal to `∥f'∥`. See also `has_deriv_within_at.limsup_norm_slope_le`
for a stronger version using limit superior and any set `s`. -/
lemma has_deriv_within_at.liminf_right_norm_slope_le
(hf : has_deriv_within_at f f' (Ioi x) x) (hr : ∥f'∥ < r) :
∃ᶠ z in 𝓝[Ioi x] x, ∥z - x∥⁻¹ * ∥f z - f x∥ < r :=
(hf.limsup_norm_slope_le hr).frequently
/-- If `f` has derivative `f'` within `(x, +∞)` at `x`, then for any `r > ∥f'∥` the ratio
`(∥f z∥ - ∥f x∥) / (z - x)` is frequently less than `r` as `z → x+0`.
In other words, the limit inferior of this ratio as `z` tends to `x+0`
is less than or equal to `∥f'∥`.
See also
* `has_deriv_within_at.limsup_norm_slope_le` for a stronger version using
limit superior and any set `s`;
* `has_deriv_within_at.liminf_right_norm_slope_le` for a stronger version using
`∥f z - f x∥` instead of `∥f z∥ - ∥f x∥`. -/
lemma has_deriv_within_at.liminf_right_slope_norm_le
(hf : has_deriv_within_at f f' (Ioi x) x) (hr : ∥f'∥ < r) :
∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (∥f z∥ - ∥f x∥) < r :=
begin
have := (hf.limsup_slope_norm_le hr).frequently,
refine this.mp (eventually.mono self_mem_nhds_within _),
assume z hxz hz,
rwa [real.norm_eq_abs, abs_of_pos (sub_pos_of_lt hxz)] at hz
end
end real_space
|
d7ec14524a47958e6d4ca9b95f89f2077da749fc | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/topology/category/Compactum.lean | 2a85ae47f544b8466af164b544619246a6b07b0f | [
"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 | 17,836 | lean | /-
Copyright (c) 2020 Adam Topaz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Adam Topaz
-/
import category_theory.monad.types
import category_theory.monad.limits
import category_theory.equivalence
import topology.category.CompHaus
import topology.category.Profinite
import data.set.constructions
/-!
# Compacta and Compact Hausdorff Spaces
Recall that, given a monad `M` on `Type*`, an *algebra* for `M` consists of the following data:
- A type `X : Type*`
- A "structure" map `M X → X`.
This data must also satisfy a distributivity and unit axiom, and algebras for `M` form a category
in an evident way.
See the file `category_theory.monad.algebra` for a general version, as well as the following link.
https://ncatlab.org/nlab/show/monad
This file proves the equivalence between the category of *compact Hausdorff topological spaces*
and the category of algebras for the *ultrafilter monad*.
## Notation:
Here are the main objects introduced in this file.
- `Compactum` is the type of compacta, which we define as algebras for the ultrafilter monad.
- `Compactum_to_CompHaus` is the functor `Compactum ⥤ CompHaus`. Here `CompHaus` is the usual
category of compact Hausdorff spaces.
- `Compactum_to_CompHaus.is_equivalence` is a term of type `is_equivalence Compactum_to_CompHaus`.
The proof of this equivalence is a bit technical. But the idea is quite simply that the structure
map `ultrafilter X → X` for an algebra `X` of the ultrafilter monad should be considered as the map
sending an ultrafilter to its limit in `X`. The topology on `X` is then defined by mimicking the
characterization of open sets in terms of ultrafilters.
Any `X : Compactum` is endowed with a coercion to `Type*`, as well as the following instances:
- `topological_space X`.
- `compact_space X`.
- `t2_space X`.
Any morphism `f : X ⟶ Y` of is endowed with a coercion to a function `X → Y`, which is shown to
be continuous in `continuous_of_hom`.
The function `Compactum.of_topological_space` can be used to construct a `Compactum` from a
topological space which satisfies `compact_space` and `t2_space`.
We also add wrappers around structures which already exist. Here are the main ones, all in the
`Compactum` namespace:
- `forget : Compactum ⥤ Type*` is the forgetful functor, which induces a `concrete_category`
instance for `Compactum`.
- `free : Type* ⥤ Compactum` is the left adjoint to `forget`, and the adjunction is in `adj`.
- `str : ultrafilter X → X` is the structure map for `X : Compactum`.
The notation `X.str` is preferred.
- `join : ultrafilter (ultrafilter X) → ultrafilter X` is the monadic join for `X : Compactum`.
Again, the notation `X.join` is preferred.
- `incl : X → ultrafilter X` is the unit for `X : Compactum`. The notation `X.incl` is preferred.
## References
- E. Manes, Algebraic Theories, Graduate Texts in Mathematics 26, Springer-Verlag, 1976.
- https://ncatlab.org/nlab/show/ultrafilter
-/
universe u
open category_theory filter ultrafilter topological_space category_theory.limits has_finite_inter
open_locale classical topological_space
local notation `β` := of_type_monad ultrafilter
/-- The type `Compactum` of Compacta, defined as algebras for the ultrafilter monad. -/
@[derive [category, inhabited]]
def Compactum := monad.algebra β
namespace Compactum
/-- The forgetful functor to Type* -/
@[derive [creates_limits,faithful]]
def forget : Compactum ⥤ Type* := monad.forget _
/-- The "free" Compactum functor. -/
def free : Type* ⥤ Compactum := monad.free _
/-- The adjunction between `free` and `forget`. -/
def adj : free ⊣ forget := monad.adj _
-- Basic instances
instance : concrete_category Compactum := { forget := forget }
instance : has_coe_to_sort Compactum Type* := ⟨forget.obj⟩
instance {X Y : Compactum} : has_coe_to_fun (X ⟶ Y) (λ f, X → Y) := ⟨λ f, f.f⟩
instance : has_limits Compactum := has_limits_of_has_limits_creates_limits forget
/-- The structure map for a compactum, essentially sending an ultrafilter to its limit. -/
def str (X : Compactum) : ultrafilter X → X := X.a
/-- The monadic join. -/
def join (X : Compactum) : ultrafilter (ultrafilter X) → ultrafilter X := β .μ.app _
/-- The inclusion of `X` into `ultrafilter X`. -/
def incl (X : Compactum) : X → ultrafilter X := β .η.app _
@[simp] lemma str_incl (X : Compactum) (x : X) : X.str (X.incl x) = x :=
begin
change (β .η.app _ ≫ X.a) _ = _,
rw monad.algebra.unit,
refl,
end
@[simp] lemma str_hom_commute (X Y : Compactum) (f : X ⟶ Y) (xs : ultrafilter X) :
f (X.str xs) = Y.str (map f xs) :=
begin
change (X.a ≫ f.f) _ = _,
rw ←f.h,
refl,
end
@[simp] lemma join_distrib (X : Compactum) (uux : ultrafilter (ultrafilter X)) :
X.str (X.join uux) = X.str (map X.str uux) :=
begin
change (β .μ.app _ ≫ X.a) _ = _,
rw monad.algebra.assoc,
refl,
end
instance {X : Compactum} : topological_space X :=
{ is_open := λ U, ∀ (F : ultrafilter X), X.str F ∈ U → U ∈ F,
is_open_univ := λ _ _, filter.univ_sets _,
is_open_inter := λ S T h3 h4 h5 h6,
filter.inter_sets _ (h3 _ h6.1) (h4 _ h6.2),
is_open_sUnion := λ S h1 F ⟨T,hT,h2⟩,
mem_of_superset (h1 T hT _ h2) (set.subset_sUnion_of_mem hT) }
theorem is_closed_iff {X : Compactum} (S : set X) : is_closed S ↔
(∀ F : ultrafilter X, S ∈ F → X.str F ∈ S) :=
begin
rw ← is_open_compl_iff,
split,
{ intros cond F h,
by_contradiction c,
specialize cond F c,
rw compl_mem_iff_not_mem at cond,
contradiction },
{ intros h1 F h2,
specialize h1 F,
cases F.mem_or_compl_mem S, exacts [absurd (h1 h) h2, h] }
end
instance {X : Compactum} : compact_space X :=
begin
constructor,
rw is_compact_iff_ultrafilter_le_nhds,
intros F h,
refine ⟨X.str F, by tauto, _⟩,
rw le_nhds_iff,
intros S h1 h2,
exact h2 F h1
end
/-- A local definition used only in the proofs. -/
private def basic {X : Compactum} (A : set X) : set (ultrafilter X) := {F | A ∈ F}
/-- A local definition used only in the proofs. -/
private def cl {X : Compactum} (A : set X) : set X := X.str '' (basic A)
private lemma basic_inter {X : Compactum} (A B : set X) : basic (A ∩ B) = basic A ∩ basic B :=
begin
ext G,
split,
{ intro hG,
split; filter_upwards [hG] with _,
exacts [and.left, and.right] },
{ rintros ⟨h1, h2⟩,
exact inter_mem h1 h2 }
end
private lemma subset_cl {X : Compactum} (A : set X) : A ⊆ cl A := λ a ha, ⟨X.incl a, ha,by simp⟩
private theorem cl_cl {X : Compactum} (A : set X) : cl (cl A) ⊆ cl A :=
begin
rintros _ ⟨F,hF,rfl⟩,
-- Notation to be used in this proof.
let fsu := finset (set (ultrafilter X)),
let ssu := set (set (ultrafilter X)),
let ι : fsu → ssu := coe,
let C0 : ssu := {Z | ∃ B ∈ F, X.str ⁻¹' B = Z},
let AA := {G : ultrafilter X | A ∈ G},
let C1 := insert AA C0,
let C2 := finite_inter_closure C1,
-- C0 is closed under intersections.
have claim1 : ∀ B C ∈ C0, B ∩ C ∈ C0,
{ rintros B ⟨Q,hQ,rfl⟩ C ⟨R,hR,rfl⟩,
use Q ∩ R,
simp only [and_true, eq_self_iff_true, set.preimage_inter, subtype.val_eq_coe],
exact inter_sets _ hQ hR },
-- All sets in C0 are nonempty.
have claim2 : ∀ B ∈ C0, set.nonempty B,
{ rintros B ⟨Q,hQ,rfl⟩,
obtain ⟨q⟩ := filter.nonempty_of_mem hQ,
use X.incl q,
simpa, },
-- The intersection of AA with every set in C0 is nonempty.
have claim3 : ∀ B ∈ C0, (AA ∩ B).nonempty,
{ rintros B ⟨Q,hQ,rfl⟩,
have : (Q ∩ cl A).nonempty :=
filter.nonempty_of_mem (inter_mem hQ hF),
rcases this with ⟨q,hq1,P,hq2,hq3⟩,
refine ⟨P,hq2,_⟩,
rw ←hq3 at hq1,
simpa },
-- Suffices to show that the intersection of any finite subcollection of C1 is nonempty.
suffices : ∀ (T : fsu), ι T ⊆ C1 → (⋂₀ ι T).nonempty,
{ obtain ⟨G, h1⟩ := exists_ultrafilter_of_finite_inter_nonempty _ this,
use X.join G,
have : G.map X.str = F :=
ultrafilter.coe_le_coe.1 (λ S hS, h1 (or.inr ⟨S, hS, rfl⟩)),
rw [join_distrib, this],
exact ⟨h1 (or.inl rfl), rfl⟩ },
-- C2 is closed under finite intersections (by construction!).
have claim4 := finite_inter_closure_has_finite_inter C1,
-- C0 is closed under finite intersections by claim1.
have claim5 : has_finite_inter C0 :=
⟨⟨_, univ_mem, set.preimage_univ⟩, claim1⟩,
-- Every element of C2 is nonempty.
have claim6 : ∀ P ∈ C2, (P : set (ultrafilter X)).nonempty,
{ suffices : ∀ P ∈ C2, P ∈ C0 ∨ ∃ Q ∈ C0, P = AA ∩ Q,
{ intros P hP,
cases this P hP,
{ exact claim2 _ h },
{ rcases h with ⟨Q, hQ, rfl⟩,
exact claim3 _ hQ } },
intros P hP,
exact claim5.finite_inter_closure_insert _ hP },
intros T hT,
-- Suffices to show that the intersection of the T's is contained in C2.
suffices : ⋂₀ ι T ∈ C2, by exact claim6 _ this,
-- Finish
apply claim4.finite_inter_mem,
intros t ht,
exact finite_inter_closure.basic (@hT t ht),
end
lemma is_closed_cl {X : Compactum} (A : set X) : is_closed (cl A) :=
begin
rw is_closed_iff,
intros F hF,
exact cl_cl _ ⟨F, hF, rfl⟩,
end
lemma str_eq_of_le_nhds {X : Compactum} (F : ultrafilter X) (x : X) :
↑F ≤ 𝓝 x → X.str F = x :=
begin
-- Notation to be used in this proof.
let fsu := finset (set (ultrafilter X)),
let ssu := set (set (ultrafilter X)),
let ι : fsu → ssu := coe,
let T0 : ssu := { S | ∃ A ∈ F, S = basic A },
let AA := (X.str ⁻¹' {x}),
let T1 := insert AA T0,
let T2 := finite_inter_closure T1,
intro cond,
-- If F contains a closed set A, then x is contained in A.
have claim1 : ∀ (A : set X), is_closed A → A ∈ F → x ∈ A,
{ intros A hA h,
by_contradiction H,
rw le_nhds_iff at cond,
specialize cond Aᶜ H hA.is_open_compl,
rw [ultrafilter.mem_coe, ultrafilter.compl_mem_iff_not_mem] at cond,
contradiction },
-- If A ∈ F, then x ∈ cl A.
have claim2 : ∀ (A : set X), A ∈ F → x ∈ cl A,
{ intros A hA,
exact claim1 (cl A) (is_closed_cl A) (mem_of_superset hA (subset_cl A)) },
-- T0 is closed under intersections.
have claim3 : ∀ (S1 S2 ∈ T0), S1 ∩ S2 ∈ T0,
{ rintros S1 ⟨S1, hS1, rfl⟩ S2 ⟨S2, hS2, rfl⟩,
exact ⟨S1 ∩ S2, inter_mem hS1 hS2, by simp [basic_inter]⟩ },
-- For every S ∈ T0, the intersection AA ∩ S is nonempty.
have claim4 : ∀ (S ∈ T0), (AA ∩ S).nonempty,
{ rintros S ⟨S, hS, rfl⟩,
rcases claim2 _ hS with ⟨G, hG, hG2⟩,
exact ⟨G, hG2, hG⟩ },
-- Every element of T0 is nonempty.
have claim5 : ∀ (S ∈ T0), set.nonempty S,
{ rintros S ⟨S, hS, rfl⟩,
exact ⟨F, hS⟩ },
-- Every element of T2 is nonempty.
have claim6 : ∀ (S ∈ T2), set.nonempty S,
{ suffices : ∀ S ∈ T2, S ∈ T0 ∨ ∃ Q ∈ T0, S = AA ∩ Q,
{ intros S hS,
cases this _ hS with h h,
{ exact claim5 S h },
{ rcases h with ⟨Q, hQ, rfl⟩,
exact claim4 Q hQ } },
intros S hS,
apply finite_inter_closure_insert,
{ split,
{ use set.univ,
refine ⟨filter.univ_sets _, _⟩,
ext,
refine ⟨_, by tauto⟩,
{ intro,
apply filter.univ_sets, } },
{ exact claim3 } },
{ exact hS} },
-- It suffices to show that the intersection of any finite subset of T1 is nonempty.
suffices : ∀ (F : fsu), ↑F ⊆ T1 → (⋂₀ ι F).nonempty,
{ obtain ⟨G,h1⟩ := ultrafilter.exists_ultrafilter_of_finite_inter_nonempty _ this,
have c1 : X.join G = F := ultrafilter.coe_le_coe.1 (λ P hP, h1 (or.inr ⟨P, hP, rfl⟩)),
have c2 : G.map X.str = X.incl x,
{ refine ultrafilter.coe_le_coe.1 (λ P hP, _),
apply mem_of_superset (h1 (or.inl rfl)),
rintros x ⟨rfl⟩,
exact hP },
simp [←c1, c2] },
-- Finish...
intros T hT,
refine claim6 _ (finite_inter_mem (finite_inter_closure_has_finite_inter _) _ _),
intros t ht,
exact finite_inter_closure.basic (@hT t ht)
end
lemma le_nhds_of_str_eq {X : Compactum} (F : ultrafilter X) (x : X) :
X.str F = x → ↑F ≤ 𝓝 x := λ h, le_nhds_iff.mpr (λ s hx hs, hs _ $ by rwa h)
-- All the hard work above boils down to this t2_space instance.
instance {X : Compactum} : t2_space X :=
begin
rw t2_iff_ultrafilter,
intros _ _ F hx hy,
rw [← str_eq_of_le_nhds _ _ hx, ← str_eq_of_le_nhds _ _ hy]
end
/-- The structure map of a compactum actually computes limits. -/
lemma Lim_eq_str {X : Compactum} (F : ultrafilter X) : F.Lim = X.str F :=
begin
rw [ultrafilter.Lim_eq_iff_le_nhds, le_nhds_iff],
tauto,
end
lemma cl_eq_closure {X : Compactum} (A : set X) : cl A = closure A :=
begin
ext,
rw mem_closure_iff_ultrafilter,
split,
{ rintro ⟨F, h1, h2⟩,
exact ⟨F, h1, le_nhds_of_str_eq _ _ h2⟩ },
{ rintro ⟨F, h1, h2⟩,
exact ⟨F, h1, str_eq_of_le_nhds _ _ h2⟩ }
end
/-- Any morphism of compacta is continuous. -/
lemma continuous_of_hom {X Y : Compactum} (f : X ⟶ Y) : continuous f :=
begin
rw continuous_iff_ultrafilter,
intros x _ h,
rw [tendsto, ← coe_map],
apply le_nhds_of_str_eq,
rw [← str_hom_commute, str_eq_of_le_nhds _ x h]
end
/-- Given any compact Hausdorff space, we construct a Compactum. -/
noncomputable def of_topological_space (X : Type*) [topological_space X]
[compact_space X] [t2_space X] : Compactum :=
{ A := X,
a := ultrafilter.Lim,
unit' := by {ext x, exact Lim_eq (pure_le_nhds _) },
assoc' := begin
ext FF,
change ultrafilter (ultrafilter X) at FF,
set x := (ultrafilter.map ultrafilter.Lim FF).Lim with c1,
have c2 : ∀ (U : set X) (F : ultrafilter X), F.Lim ∈ U → is_open U → U ∈ F,
{ intros U F h1 hU,
exact c1 ▸ is_open_iff_ultrafilter.mp hU _ h1 _ (ultrafilter.le_nhds_Lim _) },
have c3 : ↑(ultrafilter.map ultrafilter.Lim FF) ≤ 𝓝 x,
{ rw le_nhds_iff,
intros U hx hU,
exact mem_coe.2 (c2 _ _ (by rwa ← c1) hU) },
have c4 : ∀ (U : set X), x ∈ U → is_open U → { G : ultrafilter X | U ∈ G } ∈ FF,
{ intros U hx hU,
suffices : ultrafilter.Lim ⁻¹' U ∈ FF,
{ apply mem_of_superset this,
intros P hP,
exact c2 U P hP hU },
exact @c3 U (is_open.mem_nhds hU hx) },
apply Lim_eq,
rw le_nhds_iff,
exact c4,
end }
/-- Any continuous map between Compacta is a morphism of compacta. -/
def hom_of_continuous {X Y : Compactum} (f : X → Y) (cont : continuous f) : X ⟶ Y :=
{ f := f,
h' := begin
rw continuous_iff_ultrafilter at cont,
ext (F : ultrafilter X),
specialize cont (X.str F) F (le_nhds_of_str_eq F (X.str F) rfl),
have := str_eq_of_le_nhds (ultrafilter.map f F) _ cont,
simpa only [←this, types_comp_apply, of_type_functor_map],
end }
end Compactum
/-- The functor functor from Compactum to CompHaus. -/
def Compactum_to_CompHaus : Compactum ⥤ CompHaus :=
{ obj := λ X, { to_Top := { α := X } },
map := λ X Y f,
{ to_fun := f,
continuous_to_fun := Compactum.continuous_of_hom _ }}
namespace Compactum_to_CompHaus
/-- The functor Compactum_to_CompHaus is full. -/
def full : full Compactum_to_CompHaus.{u} :=
{ preimage := λ X Y f, Compactum.hom_of_continuous f.1 f.2 }
/-- The functor Compactum_to_CompHaus is faithful. -/
lemma faithful : faithful Compactum_to_CompHaus := {}
/-- This definition is used to prove essential surjectivity of Compactum_to_CompHaus. -/
noncomputable def iso_of_topological_space {D : CompHaus} :
Compactum_to_CompHaus.obj (Compactum.of_topological_space D) ≅ D :=
{ hom :=
{ to_fun := id,
continuous_to_fun := continuous_def.2 $ λ _ h, by {rw is_open_iff_ultrafilter' at h, exact h} },
inv :=
{ to_fun := id,
continuous_to_fun := continuous_def.2 $
λ _ h1, by {rw is_open_iff_ultrafilter', intros _ h2, exact h1 _ h2} } }
/-- The functor Compactum_to_CompHaus is essentially surjective. -/
lemma ess_surj : ess_surj Compactum_to_CompHaus :=
{ mem_ess_image := λ X, ⟨Compactum.of_topological_space X, ⟨iso_of_topological_space⟩⟩ }
/-- The functor Compactum_to_CompHaus is an equivalence of categories. -/
noncomputable instance is_equivalence : is_equivalence Compactum_to_CompHaus :=
begin
apply equivalence.of_fully_faithfully_ess_surj _,
exact Compactum_to_CompHaus.full,
exact Compactum_to_CompHaus.faithful,
exact Compactum_to_CompHaus.ess_surj,
end
end Compactum_to_CompHaus
/-- The forgetful functors of `Compactum` and `CompHaus` are compatible via
`Compactum_to_CompHaus`. -/
def Compactum_to_CompHaus_comp_forget :
Compactum_to_CompHaus ⋙ category_theory.forget CompHaus ≅ Compactum.forget :=
nat_iso.of_components (λ X, eq_to_iso rfl) $
by { intros X Y f, dsimp, simpa }
/-
TODO: `forget CompHaus` is monadic, as it is isomorphic to the composition
of an equivalence with the monadic functor `forget Compactum`.
Once we have the API to transfer monadicity of functors along such isomorphisms,
the instance `creates_limits (forget CompHaus)` can be deduced from this
monadicity.
-/
noncomputable
instance CompHaus.forget_creates_limits : creates_limits (forget CompHaus) :=
begin
let e : forget CompHaus ≅ Compactum_to_CompHaus.inv ⋙ Compactum.forget :=
_ ≪≫ iso_whisker_left _ Compactum_to_CompHaus_comp_forget,
swap,
refine _ ≪≫ functor.associator _ _ _,
refine (functor.left_unitor _).symm ≪≫ _,
refine iso_whisker_right _ _,
exact Compactum_to_CompHaus.as_equivalence.symm.unit_iso,
exact creates_limits_of_nat_iso e.symm,
end
noncomputable
instance Profinite.forget_creates_limits : creates_limits (forget Profinite) :=
begin
change creates_limits (Profinite_to_CompHaus ⋙ forget _),
apply_instance,
end
|
09782c71ed5ab8f24d161bcde4ae0faac44676c4 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/topology/algebra/uniform_mul_action.lean | e8efe7c86821f8f6ff1a9cb3425682d6f0759cd3 | [
"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 | 7,301 | lean | /-
Copyright (c) 2022 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import topology.algebra.uniform_group
import topology.uniform_space.completion
/-!
# Multiplicative action on the completion of a uniform space
In this file we define typeclasses `has_uniform_continuous_const_vadd` and
`has_uniform_continuous_const_smul` and prove that a multiplicative action on `X` with uniformly
continuous `(•) c` can be extended to a multiplicative action on `uniform_space.completion X`.
In later files once the additive group structure is set up, we provide
* `uniform_space.completion.distrib_mul_action`
* `uniform_space.completion.mul_action_with_zero`
* `uniform_space.completion.module`
TODO: Generalise the results here from the concrete `completion` to any `abstract_completion`.
-/
universes u v w x y z
noncomputable theory
variables (R : Type u) (M : Type v) (N : Type w) (X : Type x) (Y : Type y)
[uniform_space X] [uniform_space Y]
/-- An additive action such that for all `c`, the map `λ x, c +ᵥ x` is uniformly continuous. -/
class has_uniform_continuous_const_vadd [has_vadd M X] : Prop :=
(uniform_continuous_const_vadd : ∀ (c : M), uniform_continuous ((+ᵥ) c : X → X))
/-- A multiplicative action such that for all `c`, the map `λ x, c • x` is uniformly continuous. -/
@[to_additive]
class has_uniform_continuous_const_smul [has_smul M X] : Prop :=
(uniform_continuous_const_smul : ∀ (c : M), uniform_continuous ((•) c : X → X))
export has_uniform_continuous_const_vadd (uniform_continuous_const_vadd)
has_uniform_continuous_const_smul (uniform_continuous_const_smul)
instance add_monoid.has_uniform_continuous_const_smul_nat [add_group X] [uniform_add_group X] :
has_uniform_continuous_const_smul ℕ X :=
⟨uniform_continuous_const_nsmul⟩
instance add_group.has_uniform_continuous_const_smul_int [add_group X] [uniform_add_group X] :
has_uniform_continuous_const_smul ℤ X :=
⟨uniform_continuous_const_zsmul⟩
/-- A `distrib_mul_action` that is continuous on a uniform group is uniformly continuous.
This can't be an instance due to it forming a loop with
`has_uniform_continuous_const_smul.to_has_continuous_const_smul` -/
lemma has_uniform_continuous_const_smul_of_continuous_const_smul [monoid R] [add_comm_group M]
[distrib_mul_action R M] [uniform_space M] [uniform_add_group M] [has_continuous_const_smul R M] :
has_uniform_continuous_const_smul R M :=
⟨λ r, uniform_continuous_of_continuous_at_zero (distrib_mul_action.to_add_monoid_hom M r)
(continuous.continuous_at (continuous_const_smul r))⟩
/-- The action of `semiring.to_module` is uniformly continuous. -/
instance ring.has_uniform_continuous_const_smul [ring R] [uniform_space R]
[uniform_add_group R] [has_continuous_mul R] : has_uniform_continuous_const_smul R R :=
has_uniform_continuous_const_smul_of_continuous_const_smul _ _
/-- The action of `semiring.to_opposite_module` is uniformly continuous. -/
instance ring.has_uniform_continuous_const_op_smul [ring R] [uniform_space R]
[uniform_add_group R] [has_continuous_mul R] : has_uniform_continuous_const_smul Rᵐᵒᵖ R :=
has_uniform_continuous_const_smul_of_continuous_const_smul _ _
section has_smul
variable [has_smul M X]
@[priority 100, to_additive]
instance has_uniform_continuous_const_smul.to_has_continuous_const_smul
[has_uniform_continuous_const_smul M X] : has_continuous_const_smul M X :=
⟨λ c, (uniform_continuous_const_smul c).continuous⟩
variables {M X Y}
@[to_additive] lemma uniform_continuous.const_smul [has_uniform_continuous_const_smul M X]
{f : Y → X} (hf : uniform_continuous f) (c : M) :
uniform_continuous (c • f) :=
(uniform_continuous_const_smul c).comp hf
/-- If a scalar action is central, then its right action is uniform continuous when its left action
is. -/
@[priority 100, to_additive "If an additive action is central, then its right action is uniform
continuous when its left action,is."]
instance has_uniform_continuous_const_smul.op [has_smul Mᵐᵒᵖ X] [is_central_scalar M X]
[has_uniform_continuous_const_smul M X] : has_uniform_continuous_const_smul Mᵐᵒᵖ X :=
⟨mul_opposite.rec $ λ c, begin
change uniform_continuous (λ m, mul_opposite.op c • m),
simp_rw op_smul_eq_smul,
exact uniform_continuous_const_smul c,
end⟩
@[to_additive] instance mul_opposite.has_uniform_continuous_const_smul
[has_uniform_continuous_const_smul M X] : has_uniform_continuous_const_smul M Xᵐᵒᵖ :=
⟨λ c, mul_opposite.uniform_continuous_op.comp $ mul_opposite.uniform_continuous_unop.const_smul c⟩
end has_smul
@[to_additive] instance uniform_group.to_has_uniform_continuous_const_smul
{G : Type u} [group G] [uniform_space G] [uniform_group G] :
has_uniform_continuous_const_smul G G :=
⟨λ c, uniform_continuous_const.mul uniform_continuous_id⟩
namespace uniform_space
namespace completion
section has_smul
variable [has_smul M X]
@[to_additive] instance : has_smul M (completion X) :=
⟨λ c, completion.map ((•) c)⟩
@[to_additive] lemma smul_def (c : M) (x : completion X) : c • x = completion.map ((•) c) x := rfl
@[to_additive] instance : has_uniform_continuous_const_smul M (completion X) :=
⟨λ c, uniform_continuous_map⟩
@[to_additive] instance [has_smul N X] [has_smul M N]
[has_uniform_continuous_const_smul M X] [has_uniform_continuous_const_smul N X]
[is_scalar_tower M N X] : is_scalar_tower M N (completion X) :=
⟨λ m n x, begin
have : _ = (_ : completion X → completion X) :=
map_comp (uniform_continuous_const_smul m) (uniform_continuous_const_smul n),
refine eq.trans _ (congr_fun this.symm x),
exact congr_arg (λ f, completion.map f x) (by exact funext (smul_assoc _ _)),
end⟩
@[to_additive] instance [has_smul N X] [smul_comm_class M N X]
[has_uniform_continuous_const_smul M X] [has_uniform_continuous_const_smul N X] :
smul_comm_class M N (completion X) :=
⟨λ m n x, begin
have hmn : m • n • x =
(( completion.map (has_smul.smul m)) ∘ (completion.map (has_smul.smul n))) x := rfl,
have hnm : n • m • x =
(( completion.map (has_smul.smul n)) ∘ (completion.map (has_smul.smul m))) x := rfl,
rw [hmn, hnm, map_comp, map_comp],
exact congr_arg (λ f, completion.map f x) (by exact funext (smul_comm _ _)),
repeat{ exact uniform_continuous_const_smul _},
end⟩
@[to_additive]
instance [has_smul Mᵐᵒᵖ X] [is_central_scalar M X] : is_central_scalar M (completion X) :=
⟨λ c a, congr_arg (λ f, completion.map f a) $ by exact funext (op_smul_eq_smul c)⟩
variables {M X} [has_uniform_continuous_const_smul M X]
@[simp, norm_cast, to_additive]
lemma coe_smul (c : M) (x : X) : ↑(c • x) = (c • x : completion X) :=
(map_coe (uniform_continuous_const_smul c) x).symm
end has_smul
@[to_additive] instance [monoid M] [mul_action M X] [has_uniform_continuous_const_smul M X] :
mul_action M (completion X) :=
{ smul := (•),
one_smul := ext' (continuous_const_smul _) continuous_id $ λ a, by rw [← coe_smul, one_smul],
mul_smul := λ x y, ext' (continuous_const_smul _) ((continuous_const_smul _).const_smul _) $
λ a, by simp only [← coe_smul, mul_smul] }
end completion
end uniform_space
|
ccc7ea665249b795b7669693b571c36047ed1ad3 | 98beff2e97d91a54bdcee52f922c4e1866a6c9b9 | /src/category/images.lean | 7db30561f20e4f1f23a8567bf6b410f594e4d0fc | [] | no_license | b-mehta/topos | c3fc43fb04ba16bae1965ce5c26c6461172e5bc6 | c9032b11789e36038bc841a1e2b486972421b983 | refs/heads/master | 1,629,609,492,867 | 1,609,907,263,000 | 1,609,907,263,000 | 240,943,034 | 43 | 3 | null | 1,598,210,062,000 | 1,581,877,668,000 | Lean | UTF-8 | Lean | false | false | 1,286 | lean | import category_theory.limits.shapes.images
universes v u
noncomputable theory
open category_theory category_theory.limits
namespace category_theory.limits
variables {C : Type u} [category.{v} C] [has_strong_epi_mono_factorisations.{v} C]
variables {X Y : C} (f : X ⟶ Y)
@[simps]
def unique_factorise (I' : C) (e : X ⟶ I') (m : I' ⟶ Y) (comm : e ≫ m = f) [strong_epi e] [mono m] :
I' ≅ image f :=
{ hom := {strong_epi_mono_factorisation . I := I', m := m, e := e}.to_mono_is_image.lift _,
inv := image.lift {strong_epi_mono_factorisation . I := I', m := m, e := e}.to_mono_factorisation,
hom_inv_id' := by erw [← cancel_mono m, category.assoc, category.id_comp, image.lift_fac, is_image.lift_fac],
inv_hom_id' := by erw [← cancel_mono (image.ι f), category.id_comp, category.assoc, is_image.lift_fac, image.lift_fac] }
lemma unique_factorise_hom_comp_image (I' : C) (e : X ⟶ I') (m : I' ⟶ Y) (comm : e ≫ m = f) [strong_epi e] [mono m] :
(unique_factorise f I' e m comm).hom ≫ image.ι f = m :=
is_image.lift_fac _ _
lemma unique_factorise_inv_comp_mono (I' : C) (e : X ⟶ I') (m : I' ⟶ Y) (comm : e ≫ m = f) [strong_epi e] [mono m] :
(unique_factorise f I' e m comm).inv ≫ m = image.ι f :=
image.lift_fac _
end category_theory.limits |
113a0524b52004c5528ec947081d4855edf31bc2 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /hott/hit/prop_trunc.hlean | 84e6f2f82ea343489f1152ffe7741010ed5d84ec | [
"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 | 14,843 | hlean | import function types.trunc hit.colimit homotopy.connectedness --types.nat.hott hit.trunc cubical.square
open eq is_trunc unit quotient seq_colim pi nat equiv sum algebra is_conn function
/-
In this file we define the propositional truncation, which, given (X : Type)
has constructors
* tr : X → trunc X
* is_prop_trunc : is_prop (trunc X)
and with a recursor which recurses to any family of mere propositions.
The construction uses a "one step truncation" of X, with two constructors:
* tr : X → one_step_tr X
* tr_eq : Π(a b : X), tr a = tr b
This is like a truncation, but taking out the recursive part.
Martin Escardo calls this construction the generalized circle, since the one step truncation of the
unit type is the circle.
Then we can repeat this n times:
A 0 = X,
A (n + 1) = one_step_tr (A n)
We have a map
f {n : ℕ} : A n → A (n + 1) := tr
Then trunc is defined as the sequential colimit of (A, f).
Both the one step truncation and the sequential colimit can be defined as a quotient, which is a
primitive HIT in Lean. Here, with a quotient, we mean the following HIT:
Given {X : Type} (R : X → X → Type) we have the constructors
* class_of : X → quotient R
* eq_of_rel : Π{a a' : X}, R a a' → a = a'
See the comment below for a sketch of the proof that (trunc A) is actually a mere proposition.
-/
/- definition of "one step truncation" in terms of quotients -/
namespace one_step_tr
section
parameters {A : Type}
variables (a a' : A)
protected definition R (a a' : A) : Type₀ := unit
parameter (A)
definition one_step_tr : Type := quotient R
parameter {A}
definition tr : one_step_tr :=
class_of R a
definition tr_eq : tr a = tr a' :=
eq_of_rel _ star
protected definition rec {P : one_step_tr → Type} (Pt : Π(a : A), P (tr a))
(Pe : Π(a a' : A), Pt a =[tr_eq a a'] Pt a') (x : one_step_tr) : P x :=
begin
fapply (quotient.rec_on x),
{ intro a, apply Pt},
{ intro a a' H, cases H, apply Pe}
end
protected definition rec_on [reducible] {P : one_step_tr → Type} (x : one_step_tr)
(Pt : Π(a : A), P (tr a)) (Pe : Π(a a' : A), Pt a =[tr_eq a a'] Pt a') : P x :=
rec Pt Pe x
protected definition elim {P : Type} (Pt : A → P)
(Pe : Π(a a' : A), Pt a = Pt a') (x : one_step_tr) : P :=
rec Pt (λa a', pathover_of_eq _ (Pe a a')) x
protected definition elim_on [reducible] {P : Type} (x : one_step_tr) (Pt : A → P)
(Pe : Π(a a' : A), Pt a = Pt a') : P :=
elim Pt Pe x
theorem rec_tr_eq {P : one_step_tr → Type} (Pt : Π(a : A), P (tr a))
(Pe : Π(a a' : A), Pt a =[tr_eq a a'] Pt a') (a a' : A)
: apd (rec Pt Pe) (tr_eq a a') = Pe a a' :=
!rec_eq_of_rel
theorem elim_tr_eq {P : Type} (Pt : A → P)
(Pe : Π(a a' : A), Pt a = Pt a') (a a' : A)
: ap (elim Pt Pe) (tr_eq a a') = Pe a a' :=
begin
apply eq_of_fn_eq_fn_inv !(pathover_constant (tr_eq a a')),
rewrite [▸*,-apd_eq_pathover_of_eq_ap,↑elim,rec_tr_eq],
end
end
definition n_step_tr [reducible] (A : Type) (n : ℕ) : Type :=
nat.rec_on n A (λn' A', one_step_tr A')
end one_step_tr
attribute one_step_tr.rec one_step_tr.elim [recursor 5] [unfold 5]
attribute one_step_tr.rec_on one_step_tr.elim_on [unfold 2]
attribute one_step_tr.tr [constructor]
namespace one_step_tr
/- Theorems about the one-step truncation -/
open homotopy trunc prod
theorem tr_eq_ne_idp {A : Type} (a : A) : tr_eq a a ≠ idp :=
begin
intro p,
have H2 : Π{X : Type₁} {x : X} {q : x = x}, q = idp,
from λX x q, calc
q = ap (one_step_tr.elim (λa, x) (λa b, q)) (tr_eq a a) : elim_tr_eq
... = ap (one_step_tr.elim (λa, x) (λa b, q)) (refl (one_step_tr.tr a)) : by rewrite p
... = idp : idp,
exact bool.eq_bnot_ne_idp H2
end
theorem tr_eq_ne_ap_tr {A : Type} {a b : A} (p : a = b) : tr_eq a b ≠ ap tr p :=
by induction p; apply tr_eq_ne_idp
theorem not_inhabited_set_trunc_one_step_tr (A : Type)
: ¬(trunc 1 (one_step_tr A) × is_set (trunc 1 (one_step_tr A))) :=
begin
intro H, induction H with x H,
refine trunc.elim_on x _, clear x, intro x,
induction x,
{ have q : trunc -1 ((tr_eq a a) = idp),
begin
refine to_fun !tr_eq_tr_equiv _,
refine @is_prop.elim _ _ _ _, apply is_trunc_equiv_closed, apply tr_eq_tr_equiv
end,
refine trunc.elim_on q _, clear q, intro p, exact !tr_eq_ne_idp p},
{ apply is_prop.elim}
end
theorem not_is_conn_one_step_tr (A : Type) : ¬is_conn 1 (one_step_tr A) :=
λH, not_inhabited_set_trunc_one_step_tr A (!center, _)
theorem is_prop_trunc_one_step_tr (A : Type) : is_prop (trunc 0 (one_step_tr A)) :=
begin
apply is_prop.mk,
intro x y,
refine trunc.rec_on x _, refine trunc.rec_on y _, clear x y, intro y x,
induction x,
{ induction y,
{ exact ap trunc.tr !tr_eq},
{ apply is_prop.elimo}},
{ apply is_prop.elimo}
end
local attribute is_prop_trunc_one_step_tr [instance]
theorem trunc_0_one_step_tr_equiv (A : Type) : trunc 0 (one_step_tr A) ≃ ∥ A ∥ :=
begin
apply equiv_of_is_prop,
{ intro x, refine trunc.rec _ x, clear x, intro x, induction x,
{ exact trunc.tr a},
{ apply is_prop.elim}},
{ intro x, refine trunc.rec _ x, clear x, intro a, exact trunc.tr (tr a)},
end
definition one_step_tr_functor [unfold 4] {A B : Type} (f : A → B) (x : one_step_tr A)
: one_step_tr B :=
begin
induction x,
{ exact tr (f a)},
{ apply tr_eq}
end
definition one_step_tr_universal_property [constructor] (A B : Type)
: (one_step_tr A → B) ≃ Σ(f : A → B), Π(x y : A), f x = f y :=
begin
fapply equiv.MK,
{ intro f, fconstructor, intro a, exact f (tr a), intros, exact ap f !tr_eq},
{ intro v a, induction v with f p, induction a, exact f a, apply p},
{ intro v, induction v with f p, esimp, apply ap (sigma.mk _), apply eq_of_homotopy2,
intro a a', apply elim_tr_eq},
{ intro f, esimp, apply eq_of_homotopy, intro a, induction a,
reflexivity,
apply eq_pathover, apply hdeg_square, rewrite [▸*,elim_tr_eq]},
end
end one_step_tr
open one_step_tr
namespace prop_trunc
namespace hide
section
parameter {X : Type}
/- basic constructors -/
definition A [reducible] (n : ℕ) : Type := nat.rec_on n X (λn' X', one_step_tr X')
definition f [reducible] ⦃n : ℕ⦄ (a : A n) : A (succ n) := tr a
definition f_eq [reducible] {n : ℕ} (a a' : A n) : f a = f a' := tr_eq a a'
definition truncX [reducible] : Type := @seq_colim A f
definition i [reducible] {n : ℕ} (a : A n) : truncX := inclusion f a
definition g [reducible] {n : ℕ} (a : A n) : i (f a) = i a := glue f a
/- defining the normal recursor is easy -/
definition rec {P : truncX → Type} [Pt : Πx, is_prop (P x)]
(H : Π(a : X), P (@i 0 a)) (x : truncX) : P x :=
begin
induction x,
{ induction n with n IH,
{ exact H a},
{ induction a,
{ exact !g⁻¹ ▸ IH a},
{ apply is_prop.elimo}}},
{ apply is_prop.elimo}
end
/-
The main effort is to prove that truncX is a mere proposition.
We prove
Π(a b : truncX), a = b
first by induction on a, using the induction principle we just proven and then by induction on b
On the point level we need to construct
(1) a : A n, b : A m ⊢ p a b : i a = i b
On the path level (for the induction on b) we need to show that
(2) a : A n, b : A m ⊢ p a (f b) ⬝ g b = p a b
The path level for a is automatic, since (Πb, a = b) is a mere proposition
Thanks to Egbert Rijke for pointing this out
For (1) we distinguish the cases n ≤ m and n ≥ m,
and we prove that the two constructions coincide for n = m
For (2) we distinguish the cases n ≤ m and n > m
During the proof we heavily use induction on inequalities.
(n ≤ m), or (le n m), is defined as an inductive family:
inductive le (n : ℕ) : ℕ → Type₀ :=
| refl : le n n
| step : Π {m}, le n m → le n (succ m)
-/
/- point operations -/
definition fr [reducible] [unfold 2] (n : ℕ) (a : X) : A n :=
begin
induction n with n x,
{ exact a},
{ exact f x},
end
/- path operations -/
definition i_fr [unfold 2] (n : ℕ) (a : X) : i (fr n a) = @i 0 a :=
begin
induction n with n p,
{ reflexivity},
{ exact g (fr n a) ⬝ p},
end
definition eq_same {n : ℕ} (a a' : A n) : i a = i a' :=
calc
i a = i (f a) : g
... = i (f a') : ap i (f_eq a a')
... = i a' : g
definition eq_constructors {n : ℕ} (a : X) (b : A n) : @i 0 a = i b :=
calc
i a = i (fr n a) : i_fr
... = i b : eq_same
/- 2-dimensional path operations -/
theorem ap_i_ap_f {n : ℕ} {a a' : A n} (p : a = a') : !g⁻¹ ⬝ ap i (ap !f p) ⬝ !g = ap i p :=
by induction p; apply con.left_inv
theorem ap_i_eq_ap_i_same {n : ℕ} {a a' : A n} (p q : a = a') : ap i p = ap i q :=
@(is_weakly_constant_ap i) eq_same a a' p q
theorem ap_f_eq_f {n : ℕ} (a a' : A n)
: !g⁻¹ ⬝ ap i (f_eq (f a) (f a')) ⬝ !g = ap i (f_eq a a') :=
ap _ !ap_i_eq_ap_i_same ⬝ !ap_i_ap_f
theorem eq_same_f {n : ℕ} (a a' : A n)
: (g a)⁻¹ ⬝ eq_same (f a) (f a') ⬝ g a' = eq_same a a' :=
begin
esimp [eq_same],
apply (ap (λx, _ ⬝ x ⬝ _)),
apply (ap_f_eq_f a a'),
end
theorem eq_constructors_comp {n : ℕ} (a : X) (b : A n)
: eq_constructors a (f b) ⬝ g b = eq_constructors a b :=
begin
rewrite [↑eq_constructors,▸*,↓fr n a,↓i_fr n a,con_inv,+con.assoc],
apply ap (λx, _ ⬝ x),
rewrite -con.assoc, exact !eq_same_f
end
theorem is_prop_truncX : is_prop truncX :=
begin
apply is_prop_of_imp_is_contr,
intro a,
refine @rec _ _ _ a,
clear a, intro a,
fapply is_contr.mk,
exact @i 0 a,
intro b,
induction b with n b n b,
{ apply eq_constructors},
{ apply (equiv.to_inv !pathover_eq_equiv_r), apply eq_constructors_comp}
end
end
end hide
end prop_trunc
namespace prop_trunc
open hide
definition ptrunc.{u} (A : Type.{u}) : Type.{u} := @truncX A
definition ptr {A : Type} : A → ptrunc A := @i A 0
definition is_prop_trunc (A : Type) : is_prop (ptrunc A) := is_prop_truncX
protected definition ptrunc.rec {A : Type} {P : ptrunc A → Type}
[Pt : Π(x : ptrunc A), is_prop (P x)]
(H : Π(a : A), P (ptr a)) : Π(x : ptrunc A), P x := @rec A P Pt H
example {A : Type} {P : ptrunc A → Type} [Pt : Πaa, is_prop (P aa)]
(H : Πa, P (ptr a)) (a : A) : (ptrunc.rec H) (ptr a) = H a := by reflexivity
open sigma prod
-- the constructed truncation is equivalent to the "standard" propositional truncation
-- (called _root_.trunc -1 below)
open trunc
attribute is_prop_trunc [instance]
definition ptrunc_equiv_trunc (A : Type) : ptrunc A ≃ trunc -1 A :=
begin
fapply equiv.MK,
{ intro x, induction x using ptrunc.rec with a, exact tr a},
{ intro x, refine trunc.rec _ x, intro a, exact ptr a},
{ intro x, induction x with a, reflexivity},
{ intro x, induction x using ptrunc.rec with a, reflexivity}
end
-- some other recursors we get from this construction:
definition trunc.elim2 {A P : Type} (h : Π{n}, n_step_tr A n → P)
(coh : Π(n : ℕ) (a : n_step_tr A n), h (f a) = h a) (x : ptrunc A) : P :=
begin
induction x,
{ exact h a},
{ apply coh}
end
definition trunc.rec2 {A : Type} {P : truncX → Type} (h : Π{n} (a : n_step_tr A n), P (i a))
(coh : Π(n : ℕ) (a : n_step_tr A n), h (f a) =[g a] h a)
(x : ptrunc A) : P x :=
begin
induction x,
{ exact h a},
{ apply coh}
end
definition elim2_equiv [constructor] (A P : Type) : (ptrunc A → P) ≃
Σ(h : Π{n}, n_step_tr A n → P),
Π(n : ℕ) (a : n_step_tr A n), @h (succ n) (one_step_tr.tr a) = h a :=
begin
fapply equiv.MK,
{ intro h, fconstructor,
{ intro n a, refine h (i a)},
{ intro n a, exact ap h (g a)}},
{ intro x a, induction x with h p, induction a,
exact h a,
apply p},
{ intro x, induction x with h p, fapply sigma_eq,
{ reflexivity},
{ esimp, apply pathover_idp_of_eq, apply eq_of_homotopy2, intro n a, xrewrite elim_glue}},
{ intro h, apply eq_of_homotopy, intro a, esimp, induction a,
esimp,
apply eq_pathover, apply hdeg_square, esimp, rewrite elim_glue}
end
open sigma.ops
definition conditionally_constant_equiv {A P : Type} (k : A → P) :
(Σ(g : ptrunc A → P), Πa, g (ptr a) = k a) ≃
Σ(h : Π{n}, n_step_tr A n → P),
(Π(n : ℕ) (a : n_step_tr A n), h (f a) = h a) × (Πa, @h 0 a = k a) :=
calc
(Σ(g : ptrunc A → P), Πa, g (ptr a) = k a)
≃ Σ(v : Σ(h : Π{n}, n_step_tr A n → P), Π(n : ℕ) (a : n_step_tr A n), h (f a) = h a),
Πa, (v.1) 0 a = k a
: sigma_equiv_sigma !elim2_equiv (λg, equiv.rfl)
... ≃ Σ(h : Π{n}, n_step_tr A n → P) (p : Π(n : ℕ) (a : n_step_tr A n), h (f a) = h a),
Πa, @h 0 a = k a
: sigma_assoc_equiv
... ≃ Σ(h : Π{n}, n_step_tr A n → P),
(Π(n : ℕ) (a : n_step_tr A n), h (f a) = h a) × (Πa, @h 0 a = k a)
: sigma_equiv_sigma_right (λa, !equiv_prod)
definition cocone_of_is_collapsible {A : Type} (f : A → A) (p : Πa a', f a = f a')
(n : ℕ) (x : n_step_tr A n) : A :=
begin
apply f,
induction n with n h,
{ exact x},
{ apply to_inv !one_step_tr_universal_property ⟨f, p⟩, exact one_step_tr_functor h x}
end
definition has_split_support_of_is_collapsible {A : Type} (f : A → A) (p : Πa a', f a = f a')
: ptrunc A → A :=
begin
fapply to_inv !elim2_equiv,
fconstructor,
{ exact cocone_of_is_collapsible f p},
{ intro n a, apply p}
end
end prop_trunc
open prop_trunc trunc
-- Corollaries for the actual truncation.
namespace is_trunc
local attribute is_prop_trunc_one_step_tr [instance]
definition is_prop.elim_set {A : Type} {P : Type} [is_set P] (f : A → P)
(p : Πa a', f a = f a') (x : trunc -1 A) : P :=
begin
have y : trunc 0 (one_step_tr A),
by induction x; exact trunc.tr (one_step_tr.tr a),
induction y with y,
induction y,
{ exact f a},
{ exact p a a'}
end
definition is_prop.elim_set_tr {A : Type} {P : Type} {H : is_set P} (f : A → P)
(p : Πa a', f a = f a') (a : A) : is_prop.elim_set f p (tr a) = f a :=
by reflexivity
end is_trunc
|
bf6d5e58141a119595e63dc4b2254e2a5ec71394 | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /library/standard/data/list/basic.lean | 091123f292d7ce9e349a35f5fe911daa941fe3ff | [
"Apache-2.0"
] | permissive | codyroux/lean | 7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3 | 0cca265db19f7296531e339192e9b9bae4a31f8b | refs/heads/master | 1,610,909,964,159 | 1,407,084,399,000 | 1,416,857,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,853 | lean | ----------------------------------------------------------------------------------------------------
--- Copyright (c) 2014 Parikshit Khanna. All rights reserved.
--- Released under Apache 2.0 license as described in the file LICENSE.
--- Authors: Parikshit Khanna, Jeremy Avigad
----------------------------------------------------------------------------------------------------
-- Theory list
-- ===========
--
-- Basic properties of lists.
import tools.tactic
import data.nat
import logic
-- import if -- for find
using nat
using congr
using eq_proofs
namespace list
-- Type
-- ----
inductive list (T : Type) : Type :=
| nil {} : list T
| cons : T → list T → list T
infix `::` : 65 := cons
section
variable {T : Type}
theorem list_induction_on {P : list T → Prop} (l : list T) (Hnil : P nil)
(Hind : forall x : T, forall l : list T, forall H : P l, P (cons x l)) : P l :=
list_rec Hnil Hind l
theorem list_cases_on {P : list T → Prop} (l : list T) (Hnil : P nil)
(Hcons : forall x : T, forall l : list T, P (cons x l)) : P l :=
list_induction_on l Hnil (take x l IH, Hcons x l)
notation `[` l:(foldr `,` (h t, cons h t) nil) `]` := l
-- Concat
-- ------
definition concat (s t : list T) : list T :=
list_rec t (fun x : T, fun l : list T, fun u : list T, cons x u) s
infixl `++` : 65 := concat
theorem nil_concat (t : list T) : nil ++ t = t := refl _
theorem cons_concat (x : T) (s t : list T) : (x :: s) ++ t = x :: (s ++ t) := refl _
theorem concat_nil (t : list T) : t ++ nil = t :=
list_induction_on t (refl _)
(take (x : T) (l : list T) (H : concat l nil = l),
show concat (cons x l) nil = cons x l, from H ▸ refl _)
theorem concat_assoc (s t u : list T) : s ++ t ++ u = s ++ (t ++ u) :=
list_induction_on s (refl _)
(take x l,
assume H : concat (concat l t) u = concat l (concat t u),
calc
concat (concat (cons x l) t) u = cons x (concat (concat l t) u) : refl _
... = cons x (concat l (concat t u)) : { H }
... = concat (cons x l) (concat t u) : refl _)
-- Length
-- ------
definition length : list T → ℕ := list_rec 0 (fun x l m, succ m)
theorem length_nil : length (@nil T) = 0 := refl _
theorem length_cons (x : T) (t : list T) : length (x :: t) = succ (length t) := refl _
theorem length_concat (s t : list T) : length (s ++ t) = length s + length t :=
list_induction_on s
(calc
length (concat nil t) = length t : refl _
... = zero + length t : {symm (add_zero_left (length t))}
... = length (@nil T) + length t : refl _)
(take x s,
assume H : length (concat s t) = length s + length t,
calc
length (concat (cons x s) t ) = succ (length (concat s t)) : refl _
... = succ (length s + length t) : { H }
... = succ (length s) + length t : {symm (add_succ_left _ _)}
... = length (cons x s) + length t : refl _)
-- add_rewrite length_nil length_cons
-- Append
-- ------
definition append (x : T) : list T → list T := list_rec [x] (fun y l l', y :: l')
theorem append_nil (x : T) : append x nil = [x] := refl _
theorem append_cons (x : T) (y : T) (l : list T) : append x (y :: l) = y :: (append x l) := refl _
theorem append_eq_concat (x : T) (l : list T) : append x l = l ++ [x] := refl _
-- add_rewrite append_nil append_cons
-- Reverse
-- -------
definition reverse : list T → list T := list_rec nil (fun x l r, r ++ [x])
theorem reverse_nil : reverse (@nil T) = nil := refl _
theorem reverse_cons (x : T) (l : list T) : reverse (x :: l) = append x (reverse l) := refl _
theorem reverse_singleton (x : T) : reverse [x] = [x] := refl _
theorem reverse_concat (s t : list T) : reverse (s ++ t) = (reverse t) ++ (reverse s) :=
list_induction_on s (symm (concat_nil _))
(take x s,
assume IH : reverse (s ++ t) = concat (reverse t) (reverse s),
calc
reverse ((x :: s) ++ t) = reverse (s ++ t) ++ [x] : refl _
... = reverse t ++ reverse s ++ [x] : {IH}
... = reverse t ++ (reverse s ++ [x]) : concat_assoc _ _ _
... = reverse t ++ (reverse (x :: s)) : refl _)
theorem reverse_reverse (l : list T) : reverse (reverse l) = l :=
list_induction_on l (refl _)
(take x l',
assume H: reverse (reverse l') = l',
show reverse (reverse (x :: l')) = x :: l', from
calc
reverse (reverse (x :: l')) = reverse (reverse l' ++ [x]) : refl _
... = reverse [x] ++ reverse (reverse l') : reverse_concat _ _
... = [x] ++ l' : { H }
... = x :: l' : refl _)
theorem append_eq_reverse_cons (x : T) (l : list T) : append x l = reverse (x :: reverse l) :=
list_induction_on l (refl _)
(take y l',
assume H : append x l' = reverse (x :: reverse l'),
calc
append x (y :: l') = (y :: l') ++ [ x ] : append_eq_concat _ _
... = concat (reverse (reverse (y :: l'))) [ x ] : {symm (reverse_reverse _)}
... = reverse (x :: (reverse (y :: l'))) : refl _)
-- Head and tail
-- -------------
definition head (x0 : T) : list T → T := list_rec x0 (fun x l h, x)
theorem head_nil (x0 : T) : head x0 (@nil T) = x0 := refl _
theorem head_cons (x : T) (x0 : T) (t : list T) : head x0 (x :: t) = x := refl _
theorem head_concat (s t : list T) (x0 : T) : s ≠ nil → (head x0 (s ++ t) = head x0 s) :=
list_cases_on s
(take H : nil ≠ nil, absurd_elim (head x0 (concat nil t) = head x0 nil) (refl nil) H)
(take x s,
take H : cons x s ≠ nil,
calc
head x0 (concat (cons x s) t) = head x0 (cons x (concat s t)) : {cons_concat _ _ _}
... = x : {head_cons _ _ _}
... = head x0 (cons x s) : {symm ( head_cons x x0 s)})
definition tail : list T → list T := list_rec nil (fun x l b, l)
theorem tail_nil : tail (@nil T) = nil := refl _
theorem tail_cons (x : T) (l : list T) : tail (cons x l) = l := refl _
theorem cons_head_tail (x0 : T) (l : list T) : l ≠ nil → (head x0 l) :: (tail l) = l :=
list_cases_on l
(assume H : nil ≠ nil, absurd_elim _ (refl _) H)
(take x l, assume H : cons x l ≠ nil, refl _)
-- List membership
-- ---------------
definition mem (x : T) : list T → Prop := list_rec false (fun y l H, x = y ∨ H)
infix `∈` : 50 := mem
-- TODO: constructively, equality is stronger. Use that?
theorem mem_nil (x : T) : x ∈ nil ↔ false := iff_refl _
theorem mem_cons (x : T) (y : T) (l : list T) : mem x (cons y l) ↔ (x = y ∨ mem x l) := iff_refl _
theorem mem_concat_imp_or (x : T) (s t : list T) : x ∈ s ++ t → x ∈ s ∨ x ∈ t :=
list_induction_on s (or_intro_right _)
(take y s,
assume IH : x ∈ s ++ t → x ∈ s ∨ x ∈ t,
assume H1 : x ∈ (y :: s) ++ t,
have H2 : x = y ∨ x ∈ s ++ t, from H1,
have H3 : x = y ∨ x ∈ s ∨ x ∈ t, from imp_or_right H2 IH,
iff_elim_right (or_assoc _ _ _) H3)
theorem mem_or_imp_concat (x : T) (s t : list T) : x ∈ s ∨ x ∈ t → x ∈ s ++ t :=
list_induction_on s
(take H, or_elim H (false_elim _) (assume H, H))
(take y s,
assume IH : x ∈ s ∨ x ∈ t → x ∈ s ++ t,
assume H : x ∈ y :: s ∨ x ∈ t,
or_elim H
(assume H1,
or_elim H1
(take H2 : x = y, or_intro_left _ H2)
(take H2 : x ∈ s, or_intro_right _ (IH (or_intro_left _ H2))))
(assume H1 : x ∈ t, or_intro_right _ (IH (or_intro_right _ H1))))
theorem mem_concat (x : T) (s t : list T) : x ∈ s ++ t ↔ x ∈ s ∨ x ∈ t
:= iff_intro (mem_concat_imp_or _ _ _) (mem_or_imp_concat _ _ _)
theorem mem_split (x : T) (l : list T) : x ∈ l → ∃s t : list T, l = s ++ (x :: t) :=
list_induction_on l
(take H : x ∈ nil, false_elim _ (iff_elim_left (mem_nil x) H))
(take y l,
assume IH : x ∈ l → ∃s t : list T, l = s ++ (x :: t),
assume H : x ∈ y :: l,
or_elim H
(assume H1 : x = y,
exists_intro nil
(exists_intro l (subst H1 (refl _))))
(assume H1 : x ∈ l,
obtain s (H2 : ∃t : list T, l = s ++ (x :: t)), from IH H1,
obtain t (H3 : l = s ++ (x :: t)), from H2,
have H4 : y :: l = (y :: s) ++ (x :: t),
from trans (subst H3 (refl (y :: l))) (cons_concat _ _ _),
exists_intro _ (exists_intro _ H4)))
-- Find
-- ----
-- to do this: need decidability of = for nat
-- definition find (x : T) : list T → nat
-- := list_rec 0 (fun y l b, if x = y then 0 else succ b)
-- theorem find_nil (f : T) : find f nil = 0
-- :=refl _
-- theorem find_cons (x y : T) (l : list T) : find x (cons y l) =
-- if x = y then 0 else succ (find x l)
-- := refl _
-- theorem not_mem_find (l : list T) (x : T) : ¬ mem x l → find x l = length l
-- :=
-- @list_induction_on T (λl, ¬ mem x l → find x l = length l) l
-- -- list_induction_on l
-- (assume P1 : ¬ mem x nil,
-- show find x nil = length nil, from
-- calc
-- find x nil = 0 : find_nil _
-- ... = length nil : by simp)
-- (take y l,
-- assume IH : ¬ (mem x l) → find x l = length l,
-- assume P1 : ¬ (mem x (cons y l)),
-- have P2 : ¬ (mem x l ∨ (y = x)), from subst P1 (mem_cons _ _ _),
-- have P3 : ¬ (mem x l) ∧ (y ≠ x),from subst P2 (not_or _ _),
-- have P4 : x ≠ y, from ne_symm (and_elim_right P3),
-- calc
-- find x (cons y l) = succ (find x l) :
-- trans (find_cons _ _ _) (not_imp_if_eq P4 _ _)
-- ... = succ (length l) : {IH (and_elim_left P3)}
-- ... = length (cons y l) : symm (length_cons _ _))
-- nth element
-- -----------
definition nth (x0 : T) (l : list T) (n : ℕ) : T :=
nat_rec (λl, head x0 l) (λm f l, f (tail l)) n l
theorem nth_zero (x0 : T) (l : list T) : nth x0 l 0 = head x0 l := refl _
theorem nth_succ (x0 : T) (l : list T) (n : ℕ) : nth x0 l (succ n) = nth x0 (tail l) n := refl _
end
-- declare global notation outside the section
infixl `++` : 65 := concat
|
89961abaefca66f928932b3f017cf19cd74a912e | 53f7fd7840fe4979de24f9611c89f2ee1b4fc5dc | /src/zipper.lean | 27d9fa05397cf9a3882b9bd4628792425428a268 | [] | no_license | cipher1024/search-trees | 6d1925283c6ce47d84bdf41ca40d8317ff2e64d7 | 0e0ea0ee59f0b0499ebad1ef6f34f09ec9666cde | refs/heads/master | 1,673,300,158,759 | 1,603,984,112,000 | 1,603,984,112,000 | 308,132,998 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,634 | lean |
import .basic .backtrack
universes u v
variables (α : Type u) (β : Type v)
namespace two_three.tree
open two_three
inductive zipper
| nil : zipper
| node2_0 : zipper → α → tree α β → zipper
| node2_1 : tree α β → α → zipper → zipper
| node3_0 : zipper → α → tree α β → α → tree α β → zipper
| node3_1 : tree α β → α → zipper → α → tree α β → zipper
| node3_2 : tree α β → α → tree α β → α → zipper → zipper
variables {α β}
namespace zipper
@[pp_nodot]
def close : zipper α β → option α → tree α β → tree α β
| nil k t := t
| (node2_0 z x t₁) k t := close z k $ node2 t x t₁
| (node2_1 t₀ x z) k t := close z none $ node2 t₀ (k.get_or_else x) t
| (node3_0 z x t₁ y t₂) k t := close z k $ node3 t x t₁ y t₂
| (node3_1 t₀ x z y t₂) k t := close z none $ node3 t₀ (k.get_or_else x) t y t₂
| (node3_2 t₀ x t₁ y z) k t := close z none $ node3 t₀ x t₁ (k.get_or_else y) t
@[pp_nodot]
def split : zipper α β → option α → tree α β → α → tree α β → tree α β
| nil k ta a tb := node2 ta a tb
| (node2_0 z x t₁) k ta a tb := close z k $ node3 ta a tb x t₁
| (node2_1 t₀ x z) k ta a tb := close z none $ node3 t₀ (k.get_or_else x) ta a tb
| (node3_0 z x t₁ y t₂) k ta a tb := split z k (node2 ta a tb) x (node2 t₁ y t₂)
| (node3_1 t₀ x z y t₂) k ta a tb := split z none (node2 t₀ (k.get_or_else x) ta) a (node2 tb y t₂)
| (node3_2 t₀ x t₁ y z) k ta a tb := split z none (node2 t₀ x t₁) (k.get_or_else y) (node2 ta a tb)
section eqv
variables [has_lt α] [@decidable_rel α has_lt.lt]
def insert (k : α) (v : β) : zipper α β → tree α β → tree α β
| xs (leaf k' v') :=
match cmp k k' with
| ordering.lt := split xs (some k) (tree.leaf k v) k' (tree.leaf k' v')
| ordering.eq := close xs (some k') (tree.leaf k' v)
| ordering.gt := split xs (some k') (tree.leaf k' v') k (tree.leaf k v)
end
| xs (node2 t₀ x t₁) :=
if k < x
then insert (node2_0 xs x t₁) t₀
else insert (node2_1 t₀ x xs) t₁
| xs (node3 t₀ x t₁ y t₂) :=
if k < x
then insert (node3_0 xs x t₁ y t₂) t₀
else if k < y
then insert (node3_1 t₀ x xs y t₂) t₁
else insert (node3_2 t₀ x t₁ y xs) t₂
def insert' (k : α) (v : β) : tree α β → tree α β :=
insert k v zipper.nil
end eqv
open nat
variables {n m : ℕ} {pk : option α} {t t₀ t₁ : tree α β} (x : α) {z : zipper α β}
section defns
variables [has_lt α] [has_le α]
inductive zipper_height (m : ℕ) : ℕ → zipper α β → Prop
| nil : zipper_height m zipper.nil
| node2_0 {n} {z} {x t₁} :
zipper_height (succ n) z →
with_height n t₁ →
zipper_height n (zipper.node2_0 z x t₁)
| node2_1 {n} {z} {t₀ x} :
zipper_height (succ n) z →
with_height n t₀ →
zipper_height n (zipper.node2_1 t₀ x z)
| node3_0 {n} {z} {x t₁ y t₂} :
zipper_height (succ n) z →
with_height n t₁ →
with_height n t₂ →
zipper_height n (zipper.node3_0 z x t₁ y t₂)
| node3_1 {n} {z} {t₀ x y t₂} :
zipper_height (succ n) z →
with_height n t₀ →
with_height n t₂ →
zipper_height n (zipper.node3_1 t₀ x z y t₂)
| node3_2 {n} {z} {t₀ x t₁ y} :
zipper_height (succ n) z →
with_height n t₀ →
with_height n t₁ →
zipper_height n (zipper.node3_2 t₀ x t₁ y z)
lemma with_height_close
(h : with_height n t)
(h' : zipper_height m n z) :
with_height m (close z pk t) ∨ with_height (succ m) (close z pk t) :=
begin
left,
induction h' generalizing t pk; dsimp [close] at *,
repeat { apply h'_ih <|> assumption <|> constructor },
end
lemma with_height_split
(h₀ : with_height n t₀)
(h₁ : with_height n t₁)
(h' : zipper_height m n z) :
with_height m (split z pk t₀ x t₁) ∨ with_height (succ m) (split z pk t₀ x t₁) :=
begin
induction h' generalizing t₀ x t₁ pk; dsimp [split],
{ right, constructor; assumption },
repeat
{ apply with_height_close, constructor; assumption, assumption },
repeat
{ apply h'_ih, repeat { constructor; assumption } },
end
end defns
section insert_height
variables [linear_order α]
variables {k : α} {v : β}
variables
(h : zipper_height m n z)
(h' : with_height n t)
include h h'
lemma with_height_insert : with_height m (insert k v z t) ∨ with_height (succ m) (insert k v z t) :=
begin
induction h' generalizing z,
case two_three.with_height.leaf : k' v' z h
{ dsimp [insert], trichotomy k =? k',
repeat
{ assumption <|> apply with_height_close <|> apply with_height_split <|> constructor } },
case two_three.with_height.node2 : h'_n h'_x h'_t₀ h'_t₁ h'_a h'_a_1 h'_ih_a h'_ih_a_1 z h
{ dsimp [insert], split_ifs,
repeat
{ assumption <|> apply h'_ih_a <|> apply h'_ih_a_1 <|> constructor }, },
case two_three.with_height.node3 : h'_n h'_x h'_y h'_t₀ h'_t₁ h'_t₂ h'_a h'_a_1 h'_a_2 h'_ih_a h'_ih_a_1 h'_ih_a_2 z h
{ dsimp [insert], split_ifs,
repeat
{ assumption <|> apply h'_ih_a <|> apply h'_ih_a_1 <|> apply h'_ih_a_2 <|> constructor }, },
end
lemma with_height_insert' (h' : with_height m t) : with_height m (insert' k v t) ∨ with_height (succ m) (insert' k v t) :=
with_height_insert (by constructor) h'
end insert_height
section defns
variables [has_lt α] [has_le α]
inductive zipper_sorted : option α → zipper α β → option α → Prop
| nil {a b} : zipper_sorted a zipper.nil b
| node2_0 {a b x t₀ t₁} :
zipper_sorted a t₀ b →
x = first t₁ →
sorted' t₁ →
above (last t₁) b →
zipper_sorted a (node2_0 t₀ x t₁) (some x)
| node2_1 {a b x t₀ t₁} :
zipper_sorted a t₁ b →
below a (first t₀) →
sorted' t₀ →
last t₀ < x →
zipper_sorted (some x) (node2_1 t₀ x t₁) b
| node3_0 {a b x y t₀ t₁ t₂} :
zipper_sorted a t₀ b →
x = first t₁ →
sorted' t₁ →
last t₁ < y →
y = first t₂ →
sorted' t₂ →
above (last t₂) b →
zipper_sorted a (node3_0 t₀ x t₁ y t₂) (some x)
| node3_1 {a b x y t₀ t₁ t₂} :
zipper_sorted a t₁ b →
below a (first t₀) →
sorted' t₀ →
last t₀ < x →
y = first t₂ →
sorted' t₂ →
above (last t₂) b →
zipper_sorted (some x) (node3_1 t₀ x t₁ y t₂) (some y)
| node3_2 {a b x y t₀ t₁ t₂} :
zipper_sorted a t₂ b →
below a (first t₀) →
sorted' t₀ →
last t₀ < x →
x = first t₁ →
sorted' t₁ →
last t₁ < y →
zipper_sorted (some y) (node3_2 t₀ x t₁ y t₂) b
attribute [pp_nodot]
zipper_sorted.nil
zipper_sorted.node2_0
zipper_sorted.node2_1
zipper_sorted.node3_0
zipper_sorted.node3_1
zipper_sorted.node3_2
zipper.nil
zipper.node2_0
zipper.node2_1
zipper.node3_0
zipper.node3_1
zipper.node3_2
end defns
section insert_sorted
-- variables {pk : option α}
variables [linear_order α]
open two_three
lemma sorted_close {a b}
(h' : zipper_sorted a z b)
(h₀ : below a (first t))
(h₃ : below pk (first t))
(h₁ : sorted' t)
(h₂ : above (last t) b)
: sorted' (close z pk t) :=
begin
induction h' generalizing t pk,
all_goals
{ dsimp [close],
saturate, subst_vars,
simp only * { fail_if_unchanged := ff },
repeat
{ assumption <|> refl <|>
apply h'_ih <|> constructor <|> cc }, },
end
lemma sorted_split {a x b}
(h' : zipper_sorted a z b)
(h₀ : below a (first t₀))
(h₀ : below pk (first t₀))
(h₁ : sorted' t₀)
(h₂ : last t₀ < x)
(h₃ : first t₁ = x)
(h₃ : sorted' t₁)
(h₄ : above (last t₁) b)
: sorted' (split z pk t₀ x t₁) :=
begin
induction h' generalizing t₀ x t₁ pk,
all_goals
{ dsimp [split],
saturate, subst_vars,
simp only * { fail_if_unchanged := ff },
repeat
{ assumption <|> refl <|>
apply sorted_close <|>
apply h'_ih <|> constructor }, },
end
section tac
open tactic
@[interactive]
meta def simp_min : tactic unit :=
run_bt $ do
x ← var,
y ← var,
(h, _) ← hyp_with ``(min %%x %%y),
bt_lift $ do
(pr, h) ← mcond (succeeds (is_def_eq x y))
(do p₀ ← to_expr ``(min %%x %%y = %%x),
n ← get_unused_name `h,
(h, _) ← local_proof n p₀ (applyc ``min_self),
pure (h, h))
(do p₀ ← to_expr ``(%%x ≤ %%y),
p₁ ← to_expr ``(%%y ≤ %%x),
n ← get_unused_name `h,
do { (h, _) ← local_proof n p₀ (assumption <|> applyc ``le_of_lt >> assumption),
pr ← mk_app ``min_eq_left [h],
pure (pr, h) } <|>
do { (h, _) ← local_proof n p₁ (assumption <|> applyc ``le_of_lt >> assumption),
pr ← mk_app ``min_eq_right [h],
pure (pr, h) }),
interactive.loc.apply
(λ h', try $ rewrite_hyp pr h' >> skip)
(try $ rewrite_target pr)
interactive.loc.wildcard,
skip
end tac
variables {k : α} {v : β} (a b : option α)
variables (h : zipper_sorted a z b)
(h' : sorted' t)
(h₀ : below a (min k (first t)))
(h₁ : above (last t) b)
(h₂ : above k b)
include h h' h₀ h₁ h₂
lemma sorted_insert : sorted' (insert k v z t) :=
begin
induction h' generalizing z a b,
case two_three.sorted'.leaf : k' v' z a b h
{ dsimp [insert], trichotomy k =? k' with h₂,
all_goals
{ dsimp [first, last] at *,
subst_vars, simp_min, saturate,
apply sorted_split <|> apply sorted_close,
repeat
{ assumption <|>
constructor <|>
simp_min <|> above } }, },
case two_three.sorted'.node2 : h'_x h'_t₀ h'_t₁ h'_a h'_a_1 h'_a_2 h'_a_3 h'_ih_a h'_ih_a_1 z a b h h₀ h₁ h₂
{ dsimp [insert], subst_vars, split_ifs,
all_goals
{ dsimp [first] at *,
saturate, simp_min,
try { have : first h'_t₀ ≤ k, { chain_trans } },
apply h'_ih_a <|> apply h'_ih_a_1,
repeat
{ assumption <|>
constructor <|>
simp_min <|> above, } } },
case two_three.sorted'.node3 : h'_x h'_y h'_t₀ h'_t₁ h'_t₂ h'_a h'_a_1 h'_a_2 h'_a_3 h'_a_4 h'_a_5 h'_a_6 h'_ih_a h'_ih_a_1 h'_ih_a_2 z a b h h₀ h₁ h₂
{ dsimp [insert], subst_vars, split_ifs,
all_goals
{ dsimp [first] at *,
saturate, simp_min,
try { have : first h'_t₀ ≤ k, { chain_trans } },
apply h'_ih_a <|> apply h'_ih_a_1 <|> apply h'_ih_a_2,
repeat
{ assumption <|>
constructor <|>
simp_min <|> above, } } },
end
lemma sorted_insert' : sorted' (insert' k v t) :=
by apply sorted_insert none none _ h'; constructor
end insert_sorted
end zipper
end two_three.tree
|
ed19edb9181cff465c589b6596feac7ec2789193 | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /stage0/src/Lean/Elab/Inductive.lean | 83699abf8fa8dc5ac8470d61d87f11769db0f295 | [
"Apache-2.0"
] | permissive | WojciechKarpiel/lean4 | 7f89706b8e3c1f942b83a2c91a3a00b05da0e65b | f6e1314fa08293dea66a329e05b6c196a0189163 | refs/heads/master | 1,686,633,402,214 | 1,625,821,189,000 | 1,625,821,258,000 | 384,640,886 | 0 | 0 | Apache-2.0 | 1,625,903,617,000 | 1,625,903,026,000 | null | UTF-8 | Lean | false | false | 24,893 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Util.ReplaceLevel
import Lean.Util.ReplaceExpr
import Lean.Util.CollectLevelParams
import Lean.Util.Constructions
import Lean.Meta.CollectFVars
import Lean.Meta.SizeOf
import Lean.Meta.Injective
import Lean.Meta.IndPredBelow
import Lean.Elab.Command
import Lean.Elab.DefView
import Lean.Elab.DeclUtil
import Lean.Elab.Deriving.Basic
namespace Lean.Elab.Command
open Meta
builtin_initialize
registerTraceClass `Elab.inductive
def checkValidInductiveModifier [Monad m] [MonadError m] (modifiers : Modifiers) : m Unit := do
if modifiers.isNoncomputable then
throwError "invalid use of 'noncomputable' in inductive declaration"
if modifiers.isPartial then
throwError "invalid use of 'partial' in inductive declaration"
unless modifiers.attrs.size == 0 || (modifiers.attrs.size == 1 && modifiers.attrs[0].name == `class) do
throwError "invalid use of attributes in inductive declaration"
def checkValidCtorModifier [Monad m] [MonadError m] (modifiers : Modifiers) : m Unit := do
if modifiers.isNoncomputable then
throwError "invalid use of 'noncomputable' in constructor declaration"
if modifiers.isPartial then
throwError "invalid use of 'partial' in constructor declaration"
if modifiers.isUnsafe then
throwError "invalid use of 'unsafe' in constructor declaration"
if modifiers.attrs.size != 0 then
throwError "invalid use of attributes in constructor declaration"
structure CtorView where
ref : Syntax
modifiers : Modifiers
inferMod : Bool -- true if `{}` is used in the constructor declaration
declName : Name
binders : Syntax
type? : Option Syntax
deriving Inhabited
structure InductiveView where
ref : Syntax
modifiers : Modifiers
shortDeclName : Name
declName : Name
levelNames : List Name
binders : Syntax
type? : Option Syntax
ctors : Array CtorView
derivingClasses : Array DerivingClassView
deriving Inhabited
structure ElabHeaderResult where
view : InductiveView
lctx : LocalContext
localInsts : LocalInstances
params : Array Expr
type : Expr
deriving Inhabited
private partial def elabHeaderAux (views : Array InductiveView) (i : Nat) (acc : Array ElabHeaderResult) : TermElabM (Array ElabHeaderResult) := do
if h : i < views.size then
let view := views.get ⟨i, h⟩
let acc ← Term.withAutoBoundImplicit <| Term.elabBinders view.binders.getArgs fun params => do
match view.type? with
| none =>
let u ← mkFreshLevelMVar
let type := mkSort u
Term.synthesizeSyntheticMVarsNoPostponing
let params ← Term.addAutoBoundImplicits params
pure <| acc.push { lctx := (← getLCtx), localInsts := (← getLocalInstances), params := params, type := type, view := view }
| some typeStx =>
let type ← Term.elabType typeStx
unless (← isTypeFormerType type) do
throwErrorAt typeStx "invalid inductive type, resultant type is not a sort"
Term.synthesizeSyntheticMVarsNoPostponing
let params ← Term.addAutoBoundImplicits params
pure <| acc.push { lctx := (← getLCtx), localInsts := (← getLocalInstances), params := params, type := type, view := view }
elabHeaderAux views (i+1) acc
else
pure acc
private def checkNumParams (rs : Array ElabHeaderResult) : TermElabM Nat := do
let numParams := rs[0].params.size
for r in rs do
unless r.params.size == numParams do
throwErrorAt r.view.ref "invalid inductive type, number of parameters mismatch in mutually inductive datatypes"
pure numParams
private def checkUnsafe (rs : Array ElabHeaderResult) : TermElabM Unit := do
let isUnsafe := rs[0].view.modifiers.isUnsafe
for r in rs do
unless r.view.modifiers.isUnsafe == isUnsafe do
throwErrorAt r.view.ref "invalid inductive type, cannot mix unsafe and safe declarations in a mutually inductive datatypes"
private def checkLevelNames (views : Array InductiveView) : TermElabM Unit := do
if views.size > 1 then
let levelNames := views[0].levelNames
for view in views do
unless view.levelNames == levelNames do
throwErrorAt view.ref "invalid inductive type, universe parameters mismatch in mutually inductive datatypes"
private def mkTypeFor (r : ElabHeaderResult) : TermElabM Expr := do
withLCtx r.lctx r.localInsts do
mkForallFVars r.params r.type
private def throwUnexpectedInductiveType {α} : TermElabM α :=
throwError "unexpected inductive resulting type"
private def eqvFirstTypeResult (firstType type : Expr) : MetaM Bool :=
forallTelescopeReducing firstType fun _ firstTypeResult => isDefEq firstTypeResult type
-- Auxiliary function for checking whether the types in mutually inductive declaration are compatible.
private partial def checkParamsAndResultType (type firstType : Expr) (numParams : Nat) : TermElabM Unit := do
try
forallTelescopeCompatible type firstType numParams fun _ type firstType =>
forallTelescopeReducing type fun _ type =>
forallTelescopeReducing firstType fun _ firstType => do
match type with
| Expr.sort .. =>
unless (← isDefEq firstType type) do
throwError "resulting universe mismatch, given{indentExpr type}\nexpected type{indentExpr firstType}"
| _ =>
throwError "unexpected inductive resulting type"
catch
| Exception.error ref msg => throw (Exception.error ref m!"invalid mutually inductive types, {msg}")
| ex => throw ex
-- Auxiliary function for checking whether the types in mutually inductive declaration are compatible.
private def checkHeader (r : ElabHeaderResult) (numParams : Nat) (firstType? : Option Expr) : TermElabM Expr := do
let type ← mkTypeFor r
match firstType? with
| none => pure type
| some firstType =>
withRef r.view.ref $ checkParamsAndResultType type firstType numParams
pure firstType
-- Auxiliary function for checking whether the types in mutually inductive declaration are compatible.
private partial def checkHeaders (rs : Array ElabHeaderResult) (numParams : Nat) (i : Nat) (firstType? : Option Expr) : TermElabM Unit := do
if i < rs.size then
let type ← checkHeader rs[i] numParams firstType?
checkHeaders rs numParams (i+1) type
private def elabHeader (views : Array InductiveView) : TermElabM (Array ElabHeaderResult) := do
let rs ← elabHeaderAux views 0 #[]
if rs.size > 1 then
checkUnsafe rs
let numParams ← checkNumParams rs
checkHeaders rs numParams 0 none
return rs
/- Create a local declaration for each inductive type in `rs`, and execute `x params indFVars`, where `params` are the inductive type parameters and
`indFVars` are the new local declarations.
We use the local context/instances and parameters of rs[0].
Note that this method is executed after we executed `checkHeaders` and established all
parameters are compatible. -/
private partial def withInductiveLocalDecls {α} (rs : Array ElabHeaderResult) (x : Array Expr → Array Expr → TermElabM α) : TermElabM α := do
let namesAndTypes ← rs.mapM fun r => do
let type ← mkTypeFor r
pure (r.view.shortDeclName, type)
let r0 := rs[0]
let params := r0.params
withLCtx r0.lctx r0.localInsts $ withRef r0.view.ref do
let rec loop (i : Nat) (indFVars : Array Expr) := do
if h : i < namesAndTypes.size then
let (id, type) := namesAndTypes.get ⟨i, h⟩
withLocalDeclD id type fun indFVar => loop (i+1) (indFVars.push indFVar)
else
x params indFVars
loop 0 #[]
private def isInductiveFamily (numParams : Nat) (indFVar : Expr) : TermElabM Bool := do
let indFVarType ← inferType indFVar
forallTelescopeReducing indFVarType fun xs _ =>
return xs.size > numParams
/-
Elaborate constructor types.
Remark: we check whether the resulting type is correct, and the parameter occurrences are consistent, but
we currently do not check for:
- Positivity (it is a rare failure, and the kernel already checks for it).
- Universe constraints (the kernel checks for it).
-/
private def elabCtors (indFVars : Array Expr) (indFVar : Expr) (params : Array Expr) (r : ElabHeaderResult) : TermElabM (List Constructor) := withRef r.view.ref do
let indFamily ← isInductiveFamily params.size indFVar
r.view.ctors.toList.mapM fun ctorView =>
Term.withAutoBoundImplicit <| Term.elabBinders ctorView.binders.getArgs fun ctorParams =>
withRef ctorView.ref do
let rec elabCtorType (k : Expr → TermElabM Constructor) : TermElabM Constructor := do
match ctorView.type? with
| none =>
if indFamily then
throwError "constructor resulting type must be specified in inductive family declaration"
k <| mkAppN indFVar params
| some ctorType =>
let type ← Term.elabType ctorType
Term.synthesizeSyntheticMVars (mayPostpone := true)
let type ← instantiateMVars type
let type ← checkParamOccs type
forallTelescopeReducing type fun _ resultingType => do
unless resultingType.getAppFn == indFVar do
throwError "unexpected constructor resulting type{indentExpr resultingType}"
unless (← isType resultingType) do
throwError "unexpected constructor resulting type, type expected{indentExpr resultingType}"
k type
elabCtorType fun type => do
Term.synthesizeSyntheticMVarsNoPostponing
let ctorParams ← Term.addAutoBoundImplicits ctorParams
let type ← mkForallFVars ctorParams type
let type ← mkForallFVars params type
return { name := ctorView.declName, type := type }
where
checkParamOccs (ctorType : Expr) : MetaM Expr :=
let visit (e : Expr) : MetaM TransformStep := do
let f := e.getAppFn
if indFVars.contains f then
let mut args := e.getAppArgs
unless args.size ≥ params.size do
throwError "unexpected inductive type occurrence{indentExpr e}"
for i in [:params.size] do
let param := params[i]
let arg := args[i]
unless (← isDefEq param arg) do
throwError "inductive datatype parameter mismatch{indentExpr arg}\nexpected{indentExpr param}"
args := args.set! i param
return TransformStep.done (mkAppN f args)
else
return TransformStep.visit e
transform ctorType (pre := visit)
/- Convert universe metavariables occurring in the `indTypes` into new parameters.
Remark: if the resulting inductive datatype has universe metavariables, we will fix it later using
`inferResultingUniverse`. -/
private def levelMVarToParamAux (indTypes : List InductiveType) : StateRefT Nat TermElabM (List InductiveType) :=
indTypes.mapM fun indType => do
let type ← Term.levelMVarToParam' indType.type
let ctors ← indType.ctors.mapM fun ctor => do
let ctorType ← Term.levelMVarToParam' ctor.type
pure { ctor with type := ctorType }
pure { indType with ctors := ctors, type := type }
private def levelMVarToParam (indTypes : List InductiveType) : TermElabM (List InductiveType) :=
(levelMVarToParamAux indTypes).run' 1
private def getResultingUniverse : List InductiveType → TermElabM Level
| [] => throwError "unexpected empty inductive declaration"
| indType :: _ => forallTelescopeReducing indType.type fun _ r => do
match r with
| Expr.sort u _ => pure u
| _ => throwError "unexpected inductive type resulting type"
def tmpIndParam := mkLevelParam `_tmp_ind_univ_param
/--
Return true if `u` is of the form `?m + k`.
Return false if `u` does not contain universe metavariables.
Throw exception otherwise. -/
def shouldInferResultUniverse (u : Level) : TermElabM Bool := do
let u ← instantiateLevelMVars u
if u.hasMVar then
match u.getLevelOffset with
| Level.mvar mvarId _ => do
Term.assignLevelMVar mvarId tmpIndParam
pure true
| _ =>
throwError "cannot infer resulting universe level of inductive datatype, given level contains metavariables {mkSort u}, provide universe explicitly"
else
pure false
/-
Auxiliary function for `updateResultingUniverse`
`accLevelAtCtor u r rOffset us` add `u` components to `us` if they are not already there and it is different from the resulting universe level `r+rOffset`.
If `u` is a `max`, then its components are recursively processed.
If `u` is a `succ` and `rOffset > 0`, we process the `u`s child using `rOffset-1`.
This method is used to infer the resulting universe level of an inductive datatype. -/
def accLevelAtCtor : Level → Level → Nat → Array Level → TermElabM (Array Level)
| Level.max u v _, r, rOffset, us => do let us ← accLevelAtCtor u r rOffset us; accLevelAtCtor v r rOffset us
| Level.imax u v _, r, rOffset, us => do let us ← accLevelAtCtor u r rOffset us; accLevelAtCtor v r rOffset us
| Level.zero _, _, _, us => pure us
| Level.succ u _, r, rOffset+1, us => accLevelAtCtor u r rOffset us
| u, r, rOffset, us =>
if rOffset == 0 && u == r then pure us
else if r.occurs u then throwError "failed to compute resulting universe level of inductive datatype, provide universe explicitly"
else if rOffset > 0 then throwError "failed to compute resulting universe level of inductive datatype, provide universe explicitly"
else if us.contains u then pure us
else pure (us.push u)
/- Auxiliary function for `updateResultingUniverse` -/
private partial def collectUniversesFromCtorTypeAux (r : Level) (rOffset : Nat) : Nat → Expr → Array Level → TermElabM (Array Level)
| 0, Expr.forallE n d b c, us => do
let u ← getLevel d
let u ← instantiateLevelMVars u
let us ← accLevelAtCtor u r rOffset us
withLocalDecl n c.binderInfo d fun x =>
let e := b.instantiate1 x
collectUniversesFromCtorTypeAux r rOffset 0 e us
| i+1, Expr.forallE n d b c, us => do
withLocalDecl n c.binderInfo d fun x =>
let e := b.instantiate1 x
collectUniversesFromCtorTypeAux r rOffset i e us
| _, _, us => pure us
/- Auxiliary function for `updateResultingUniverse` -/
private partial def collectUniversesFromCtorType
(r : Level) (rOffset : Nat) (ctorType : Expr) (numParams : Nat) (us : Array Level) : TermElabM (Array Level) :=
collectUniversesFromCtorTypeAux r rOffset numParams ctorType us
/- Auxiliary function for `updateResultingUniverse` -/
private partial def collectUniverses (r : Level) (rOffset : Nat) (numParams : Nat) (indTypes : List InductiveType) : TermElabM (Array Level) := do
let mut us := #[]
for indType in indTypes do
for ctor in indType.ctors do
us ← collectUniversesFromCtorType r rOffset ctor.type numParams us
return us
def mkResultUniverse (us : Array Level) (rOffset : Nat) : Level :=
if us.isEmpty && rOffset == 0 then
levelOne
else
let r := Level.mkNaryMax us.toList
if rOffset == 0 && !r.isZero && !r.isNeverZero then
(mkLevelMax r levelOne).normalize
else
r.normalize
private def updateResultingUniverse (numParams : Nat) (indTypes : List InductiveType) : TermElabM (List InductiveType) := do
let r ← getResultingUniverse indTypes
let rOffset : Nat := r.getOffset
let r : Level := r.getLevelOffset
unless r.isParam do
throwError "failed to compute resulting universe level of inductive datatype, provide universe explicitly"
let us ← collectUniverses r rOffset numParams indTypes
trace[Elab.inductive] "updateResultingUniverse us: {us}, r: {r}, rOffset: {rOffset}"
let rNew := mkResultUniverse us rOffset
let updateLevel (e : Expr) : Expr := e.replaceLevel fun u => if u == tmpIndParam then some rNew else none
return indTypes.map fun indType =>
let type := updateLevel indType.type;
let ctors := indType.ctors.map fun ctor => { ctor with type := updateLevel ctor.type };
{ indType with type := type, ctors := ctors }
register_builtin_option bootstrap.inductiveCheckResultingUniverse : Bool := {
defValue := true,
group := "bootstrap",
descr := "by default the `inductive/structure commands report an error if the resulting universe is not zero, but may be zero for some universe parameters. Reason: unless this type is a subsingleton, it is hardly what the user wants since it can only eliminate into `Prop`. In the `Init` package, we define subsingletons, and we use this option to disable the check. This option may be deleted in the future after we improve the validator"
}
def checkResultingUniverse (u : Level) : TermElabM Unit := do
if bootstrap.inductiveCheckResultingUniverse.get (← getOptions) then
let u ← instantiateLevelMVars u
if !u.isZero && !u.isNeverZero then
throwError "invalid universe polymorphic type, the resultant universe is not Prop (i.e., 0), but it may be Prop for some parameter values (solution: use 'u+1' or 'max 1 u'{indentD u}"
private def checkResultingUniverses (indTypes : List InductiveType) : TermElabM Unit := do
checkResultingUniverse (← getResultingUniverse indTypes)
private def collectUsed (indTypes : List InductiveType) : StateRefT CollectFVars.State MetaM Unit := do
indTypes.forM fun indType => do
Term.collectUsedFVars indType.type
indType.ctors.forM fun ctor =>
Term.collectUsedFVars ctor.type
private def removeUnused (vars : Array Expr) (indTypes : List InductiveType) : TermElabM (LocalContext × LocalInstances × Array Expr) := do
let (_, used) ← (collectUsed indTypes).run {}
Term.removeUnused vars used
private def withUsed {α} (vars : Array Expr) (indTypes : List InductiveType) (k : Array Expr → TermElabM α) : TermElabM α := do
let (lctx, localInsts, vars) ← removeUnused vars indTypes
withLCtx lctx localInsts $ k vars
private def updateParams (vars : Array Expr) (indTypes : List InductiveType) : TermElabM (List InductiveType) :=
indTypes.mapM fun indType => do
let type ← mkForallFVars vars indType.type
let ctors ← indType.ctors.mapM fun ctor => do
let ctorType ← mkForallFVars vars ctor.type
pure { ctor with type := ctorType }
pure { indType with type := type, ctors := ctors }
private def collectLevelParamsInInductive (indTypes : List InductiveType) : Array Name := do
let mut usedParams : CollectLevelParams.State := {}
for indType in indTypes do
usedParams := collectLevelParams usedParams indType.type
for ctor in indType.ctors do
usedParams := collectLevelParams usedParams ctor.type
return usedParams.params
private def mkIndFVar2Const (views : Array InductiveView) (indFVars : Array Expr) (levelNames : List Name) : ExprMap Expr := do
let levelParams := levelNames.map mkLevelParam;
let mut m : ExprMap Expr := {}
for i in [:views.size] do
let view := views[i]
let indFVar := indFVars[i]
m := m.insert indFVar (mkConst view.declName levelParams)
return m
/- Remark: `numVars <= numParams`. `numVars` is the number of context `variables` used in the inductive declaration,
and `numParams` is `numVars` + number of explicit parameters provided in the declaration. -/
private def replaceIndFVarsWithConsts (views : Array InductiveView) (indFVars : Array Expr) (levelNames : List Name)
(numVars : Nat) (numParams : Nat) (indTypes : List InductiveType) : TermElabM (List InductiveType) :=
let indFVar2Const := mkIndFVar2Const views indFVars levelNames
indTypes.mapM fun indType => do
let ctors ← indType.ctors.mapM fun ctor => do
let type ← forallBoundedTelescope ctor.type numParams fun params type => do
let type := type.replace fun e =>
if !e.isFVar then
none
else match indFVar2Const.find? e with
| none => none
| some c => mkAppN c (params.extract 0 numVars)
mkForallFVars params type
pure { ctor with type := type }
pure { indType with ctors := ctors }
abbrev Ctor2InferMod := Std.HashMap Name Bool
private def mkCtor2InferMod (views : Array InductiveView) : Ctor2InferMod := do
let mut m := {}
for view in views do
for ctorView in view.ctors do
m := m.insert ctorView.declName ctorView.inferMod
return m
private def applyInferMod (views : Array InductiveView) (numParams : Nat) (indTypes : List InductiveType) : List InductiveType :=
let ctor2InferMod := mkCtor2InferMod views
indTypes.map fun indType =>
let ctors := indType.ctors.map fun ctor =>
let inferMod := ctor2InferMod.find! ctor.name -- true if `{}` was used
let ctorType := ctor.type.inferImplicit numParams !inferMod
{ ctor with type := ctorType }
{ indType with ctors := ctors }
private def mkAuxConstructions (views : Array InductiveView) : TermElabM Unit := do
let env ← getEnv
let hasEq := env.contains ``Eq
let hasHEq := env.contains ``HEq
let hasUnit := env.contains ``PUnit
let hasProd := env.contains ``Prod
for view in views do
let n := view.declName
mkRecOn n
if hasUnit then mkCasesOn n
if hasUnit && hasEq && hasHEq then mkNoConfusion n
if hasUnit && hasProd then mkBelow n
if hasUnit && hasProd then mkIBelow n
for view in views do
let n := view.declName;
if hasUnit && hasProd then mkBRecOn n
if hasUnit && hasProd then mkBInductionOn n
private def mkInductiveDecl (vars : Array Expr) (views : Array InductiveView) : TermElabM Unit := do
let view0 := views[0]
let scopeLevelNames ← Term.getLevelNames
checkLevelNames views
let allUserLevelNames := view0.levelNames
let isUnsafe := view0.modifiers.isUnsafe
withRef view0.ref <| Term.withLevelNames allUserLevelNames do
let rs ← elabHeader views
withInductiveLocalDecls rs fun params indFVars => do
let numExplicitParams := params.size
let mut indTypesArray := #[]
for i in [:views.size] do
let indFVar := indFVars[i]
let r := rs[i]
let type ← mkForallFVars params r.type
let ctors ← elabCtors indFVars indFVar params r
indTypesArray := indTypesArray.push { name := r.view.declName, type := type, ctors := ctors : InductiveType }
let indTypes := indTypesArray.toList
Term.synthesizeSyntheticMVarsNoPostponing
let u ← getResultingUniverse indTypes
let inferLevel ← shouldInferResultUniverse u
withUsed vars indTypes fun vars => do
let numVars := vars.size
let numParams := numVars + numExplicitParams
let indTypes ← updateParams vars indTypes
let indTypes ← levelMVarToParam indTypes
let indTypes ← if inferLevel then updateResultingUniverse numParams indTypes else checkResultingUniverses indTypes; pure indTypes
let usedLevelNames := collectLevelParamsInInductive indTypes
match sortDeclLevelParams scopeLevelNames allUserLevelNames usedLevelNames with
| Except.error msg => throwError msg
| Except.ok levelParams => do
let indTypes ← replaceIndFVarsWithConsts views indFVars levelParams numVars numParams indTypes
let indTypes := applyInferMod views numParams indTypes
let decl := Declaration.inductDecl levelParams numParams indTypes isUnsafe
Term.ensureNoUnassignedMVars decl
addDecl decl
mkAuxConstructions views
-- We need to invoke `applyAttributes` because `class` is implemented as an attribute.
for view in views do
Term.applyAttributesAt view.declName view.modifiers.attrs AttributeApplicationTime.afterTypeChecking
private def applyDerivingHandlers (views : Array InductiveView) : CommandElabM Unit := do
let mut processed : NameSet := {}
for view in views do
for classView in view.derivingClasses do
let className := classView.className
unless processed.contains className do
processed := processed.insert className
let mut declNames := #[]
for view in views do
if view.derivingClasses.any fun classView => classView.className == className then
declNames := declNames.push view.declName
classView.applyHandlers declNames
def elabInductiveViews (views : Array InductiveView) : CommandElabM Unit := do
let view0 := views[0]
let ref := view0.ref
runTermElabM view0.declName fun vars => withRef ref do
mkInductiveDecl vars views
mkSizeOfInstances view0.declName
Lean.Meta.IndPredBelow.mkBelow view0.declName
for view in views do
mkInjectiveTheorems view.declName
applyDerivingHandlers views
end Lean.Elab.Command
|
89c2649b2d86dc5405130ef57bcb39349615dd7c | 4727251e0cd73359b15b664c3170e5d754078599 | /src/logic/nontrivial.lean | cb91c974cceec74687fdeeae2056d001bde12e42 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 10,792 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import data.prod
import data.subtype
import logic.function.basic
import logic.unique
/-!
# Nontrivial types
A type is *nontrivial* if it contains at least two elements. This is useful in particular for rings
(where it is equivalent to the fact that zero is different from one) and for vector spaces
(where it is equivalent to the fact that the dimension is positive).
We introduce a typeclass `nontrivial` formalizing this property.
-/
variables {α : Type*} {β : Type*}
open_locale classical
/-- Predicate typeclass for expressing that a type is not reduced to a single element. In rings,
this is equivalent to `0 ≠ 1`. In vector spaces, this is equivalent to positive dimension. -/
class nontrivial (α : Type*) : Prop :=
(exists_pair_ne : ∃ (x y : α), x ≠ y)
lemma nontrivial_iff : nontrivial α ↔ ∃ (x y : α), x ≠ y :=
⟨λ h, h.exists_pair_ne, λ h, ⟨h⟩⟩
lemma exists_pair_ne (α : Type*) [nontrivial α] : ∃ (x y : α), x ≠ y :=
nontrivial.exists_pair_ne
-- See Note [decidable namespace]
protected lemma decidable.exists_ne [nontrivial α] [decidable_eq α] (x : α) : ∃ y, y ≠ x :=
begin
rcases exists_pair_ne α with ⟨y, y', h⟩,
by_cases hx : x = y,
{ rw ← hx at h,
exact ⟨y', h.symm⟩ },
{ exact ⟨y, ne.symm hx⟩ }
end
lemma exists_ne [nontrivial α] (x : α) : ∃ y, y ≠ x :=
by classical; exact decidable.exists_ne x
-- `x` and `y` are explicit here, as they are often needed to guide typechecking of `h`.
lemma nontrivial_of_ne (x y : α) (h : x ≠ y) : nontrivial α :=
⟨⟨x, y, h⟩⟩
-- `x` and `y` are explicit here, as they are often needed to guide typechecking of `h`.
lemma nontrivial_of_lt [preorder α] (x y : α) (h : x < y) : nontrivial α :=
⟨⟨x, y, ne_of_lt h⟩⟩
lemma exists_pair_lt (α : Type*) [nontrivial α] [linear_order α] : ∃ (x y : α), x < y :=
begin
rcases exists_pair_ne α with ⟨x, y, hxy⟩,
cases lt_or_gt_of_ne hxy;
exact ⟨_, _, h⟩
end
lemma nontrivial_iff_lt [linear_order α] : nontrivial α ↔ ∃ (x y : α), x < y :=
⟨λ h, @exists_pair_lt α h _, λ ⟨x, y, h⟩, nontrivial_of_lt x y h⟩
lemma nontrivial_iff_exists_ne (x : α) : nontrivial α ↔ ∃ y, y ≠ x :=
⟨λ h, @exists_ne α h x, λ ⟨y, hy⟩, nontrivial_of_ne _ _ hy⟩
lemma subtype.nontrivial_iff_exists_ne (p : α → Prop) (x : subtype p) :
nontrivial (subtype p) ↔ ∃ (y : α) (hy : p y), y ≠ x :=
by simp only [nontrivial_iff_exists_ne x, subtype.exists, ne.def, subtype.ext_iff, subtype.coe_mk]
instance : nontrivial Prop := ⟨⟨true, false, true_ne_false⟩⟩
/--
See Note [lower instance priority]
Note that since this and `nonempty_of_inhabited` are the most "obvious" way to find a nonempty
instance if no direct instance can be found, we give this a higher priority than the usual `100`.
-/
@[priority 500]
instance nontrivial.to_nonempty [nontrivial α] : nonempty α :=
let ⟨x, _⟩ := exists_pair_ne α in ⟨x⟩
attribute [instance, priority 500] nonempty_of_inhabited
/-- An inhabited type is either nontrivial, or has a unique element. -/
noncomputable def nontrivial_psum_unique (α : Type*) [inhabited α] :
psum (nontrivial α) (unique α) :=
if h : nontrivial α then psum.inl h else psum.inr
{ default := default,
uniq := λ (x : α),
begin
change x = default,
contrapose! h,
use [x, default]
end }
lemma subsingleton_iff : subsingleton α ↔ ∀ (x y : α), x = y :=
⟨by { introsI h, exact subsingleton.elim }, λ h, ⟨h⟩⟩
lemma not_nontrivial_iff_subsingleton : ¬(nontrivial α) ↔ subsingleton α :=
by { rw [nontrivial_iff, subsingleton_iff], push_neg, refl }
lemma not_subsingleton (α) [h : nontrivial α] : ¬subsingleton α :=
let ⟨⟨x, y, hxy⟩⟩ := h in λ ⟨h'⟩, hxy $ h' x y
/-- A type is either a subsingleton or nontrivial. -/
lemma subsingleton_or_nontrivial (α : Type*) : subsingleton α ∨ nontrivial α :=
by { rw [← not_nontrivial_iff_subsingleton, or_comm], exact classical.em _ }
lemma false_of_nontrivial_of_subsingleton (α : Type*) [nontrivial α] [subsingleton α] : false :=
let ⟨x, y, h⟩ := exists_pair_ne α in h $ subsingleton.elim x y
instance option.nontrivial [nonempty α] : nontrivial (option α) :=
by { inhabit α, use [none, some default] }
/-- Pushforward a `nontrivial` instance along an injective function. -/
protected lemma function.injective.nontrivial [nontrivial α]
{f : α → β} (hf : function.injective f) : nontrivial β :=
let ⟨x, y, h⟩ := exists_pair_ne α in ⟨⟨f x, f y, hf.ne h⟩⟩
/-- Pullback a `nontrivial` instance along a surjective function. -/
protected lemma function.surjective.nontrivial [nontrivial β]
{f : α → β} (hf : function.surjective f) : nontrivial α :=
begin
rcases exists_pair_ne β with ⟨x, y, h⟩,
rcases hf x with ⟨x', hx'⟩,
rcases hf y with ⟨y', hy'⟩,
have : x' ≠ y', by { contrapose! h, rw [← hx', ← hy', h] },
exact ⟨⟨x', y', this⟩⟩
end
/-- An injective function from a nontrivial type has an argument at
which it does not take a given value. -/
protected lemma function.injective.exists_ne [nontrivial α] {f : α → β}
(hf : function.injective f) (y : β) : ∃ x, f x ≠ y :=
begin
rcases exists_pair_ne α with ⟨x₁, x₂, hx⟩,
by_cases h : f x₂ = y,
{ exact ⟨x₁, (hf.ne_iff' h).2 hx⟩ },
{ exact ⟨x₂, h⟩ }
end
instance nontrivial_prod_right [nonempty α] [nontrivial β] : nontrivial (α × β) :=
prod.snd_surjective.nontrivial
instance nontrivial_prod_left [nontrivial α] [nonempty β] : nontrivial (α × β) :=
prod.fst_surjective.nontrivial
namespace pi
variables {I : Type*} {f : I → Type*}
/-- A pi type is nontrivial if it's nonempty everywhere and nontrivial somewhere. -/
lemma nontrivial_at (i' : I) [inst : Π i, nonempty (f i)] [nontrivial (f i')] :
nontrivial (Π i : I, f i) :=
by classical; exact
(function.update_injective (λ i, classical.choice (inst i)) i').nontrivial
/--
As a convenience, provide an instance automatically if `(f default)` is nontrivial.
If a different index has the non-trivial type, then use `haveI := nontrivial_at that_index`.
-/
instance nontrivial [inhabited I] [inst : Π i, nonempty (f i)] [nontrivial (f default)] :
nontrivial (Π i : I, f i) := nontrivial_at default
end pi
instance function.nontrivial [h : nonempty α] [nontrivial β] : nontrivial (α → β) :=
h.elim $ λ a, pi.nontrivial_at a
mk_simp_attribute nontriviality "Simp lemmas for `nontriviality` tactic"
protected lemma subsingleton.le [preorder α] [subsingleton α] (x y : α) : x ≤ y :=
le_of_eq (subsingleton.elim x y)
attribute [nontriviality] eq_iff_true_of_subsingleton subsingleton.le
namespace tactic
/--
Tries to generate a `nontrivial α` instance by performing case analysis on
`subsingleton_or_nontrivial α`,
attempting to discharge the subsingleton branch using lemmas with `@[nontriviality]` attribute,
including `subsingleton.le` and `eq_iff_true_of_subsingleton`.
-/
meta def nontriviality_by_elim (α : expr) (lems : interactive.parse simp_arg_list) : tactic unit :=
do
alternative ← to_expr ``(subsingleton_or_nontrivial %%α),
n ← get_unused_name "_inst",
tactic.cases alternative [n, n],
(solve1 $ do
reset_instance_cache,
apply_instance <|>
interactive.simp none none ff lems [`nontriviality] (interactive.loc.ns [none])) <|>
fail format!"Could not prove goal assuming `subsingleton {α}`",
reset_instance_cache
/--
Tries to generate a `nontrivial α` instance using `nontrivial_of_ne` or `nontrivial_of_lt`
and local hypotheses.
-/
meta def nontriviality_by_assumption (α : expr) : tactic unit :=
do
n ← get_unused_name "_inst",
to_expr ``(nontrivial %%α) >>= assert n,
apply_instance <|> `[solve_by_elim [nontrivial_of_ne, nontrivial_of_lt]],
reset_instance_cache
end tactic
namespace tactic.interactive
open tactic
setup_tactic_parser
/--
Attempts to generate a `nontrivial α` hypothesis.
The tactic first looks for an instance using `apply_instance`.
If the goal is an (in)equality, the type `α` is inferred from the goal.
Otherwise, the type needs to be specified in the tactic invocation, as `nontriviality α`.
The `nontriviality` tactic will first look for strict inequalities amongst the hypotheses,
and use these to derive the `nontrivial` instance directly.
Otherwise, it will perform a case split on `subsingleton α ∨ nontrivial α`, and attempt to discharge
the `subsingleton` goal using `simp [lemmas] with nontriviality`, where `[lemmas]` is a list of
additional `simp` lemmas that can be passed to `nontriviality` using the syntax
`nontriviality α using [lemmas]`.
```
example {R : Type} [ordered_ring R] {a : R} (h : 0 < a) : 0 < a :=
begin
nontriviality, -- There is now a `nontrivial R` hypothesis available.
assumption,
end
```
```
example {R : Type} [comm_ring R] {r s : R} : r * s = s * r :=
begin
nontriviality, -- There is now a `nontrivial R` hypothesis available.
apply mul_comm,
end
```
```
example {R : Type} [ordered_ring R] {a : R} (h : 0 < a) : (2 : ℕ) ∣ 4 :=
begin
nontriviality R, -- there is now a `nontrivial R` hypothesis available.
dec_trivial
end
```
```
def myeq {α : Type} (a b : α) : Prop := a = b
example {α : Type} (a b : α) (h : a = b) : myeq a b :=
begin
success_if_fail { nontriviality α }, -- Fails
nontriviality α using [myeq], -- There is now a `nontrivial α` hypothesis available
assumption
end
```
-/
meta def nontriviality (t : parse texpr?)
(lems : parse (tk "using" *> simp_arg_list <|> pure [])) :
tactic unit :=
do
α ← match t with
| some α := to_expr α
| none :=
(do t ← mk_mvar, e ← to_expr ``(@eq %%t _ _), target >>= unify e, return t) <|>
(do t ← mk_mvar, e ← to_expr ``(@has_le.le %%t _ _ _), target >>= unify e, return t) <|>
(do t ← mk_mvar, e ← to_expr ``(@ne %%t _ _), target >>= unify e, return t) <|>
(do t ← mk_mvar, e ← to_expr ``(@has_lt.lt %%t _ _ _), target >>= unify e, return t) <|>
fail "The goal is not an (in)equality, so you'll need to specify the desired `nontrivial α`
instance by invoking `nontriviality α`."
end,
nontriviality_by_assumption α <|> nontriviality_by_elim α lems
add_tactic_doc
{ name := "nontriviality",
category := doc_category.tactic,
decl_names := [`tactic.interactive.nontriviality],
tags := ["logic", "type class"] }
end tactic.interactive
namespace bool
instance : nontrivial bool := ⟨⟨tt,ff, tt_eq_ff_eq_false⟩⟩
end bool
|
5ebb1b5dfc4748ee7d6db9f842b856cf66e29925 | 5e3548e65f2c037cb94cd5524c90c623fbd6d46a | /src_icannos_totilas/aops/2005_IMO_Shortlist_Problems_A3.lean | c91261b0637cde841e1f04369e4ba114709f0668 | [] | no_license | ahayat16/lean_exos | d4f08c30adb601a06511a71b5ffb4d22d12ef77f | 682f2552d5b04a8c8eb9e4ab15f875a91b03845c | refs/heads/main | 1,693,101,073,585 | 1,636,479,336,000 | 1,636,479,336,000 | 415,000,441 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 295 | lean | import data.matrix.notation
import data.real.basic
import data.finmap
theorem IMO_Shortlist_Problems_A3_2005 (v : fin 4 -> ℝ) :
(v 0 + v 1 + v 2 + v 4 = 9) ∧ ((v 0)^2 + (v 1)^2 + (v 2)^2 + (v 3)^2=21)
→ ∃ p : (equiv.perm (fin 4)), v (p 0) * v (p 1) - v (p 2) * v (p 3) ≥ 2 := sorry |
2f63cc72f3c46ae88f206e2b8f1eea2681682b1d | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/analysis/convex/krein_milman.lean | 8ae0817cf2298c22537931de95f565a2fbaf8157 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 5,384 | lean | /-
Copyright (c) 2022 Yaël Dillies. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies
-/
import analysis.convex.exposed
import analysis.normed_space.hahn_banach.separation
/-!
# The Krein-Milman theorem
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file proves the Krein-Milman lemma and the Krein-Milman theorem.
## The lemma
The lemma states that a nonempty compact set `s` has an extreme point. The proof goes:
1. Using Zorn's lemma, find a minimal nonempty closed `t` that is an extreme subset of `s`. We will
show that `t` is a singleton, thus corresponding to an extreme point.
2. By contradiction, `t` contains two distinct points `x` and `y`.
3. With the (geometric) Hahn-Banach theorem, find an hyperplane that separates `x` and `y`.
4. Look at the extreme (actually exposed) subset of `t` obtained by going the furthest away from
the separating hyperplane in the direction of `x`. It is nonempty, closed and an extreme subset
of `s`.
5. It is a strict subset of `t` (`y` isn't in it), so `t` isn't minimal. Absurd.
## The theorem
The theorem states that a compact convex set `s` is the closure of the convex hull of its extreme
points. It is an almost immediate strengthening of the lemma. The proof goes:
1. By contradiction, `s \ closure (convex_hull ℝ (extreme_points ℝ s))` is nonempty, say with `x`.
2. With the (geometric) Hahn-Banach theorem, find an hyperplane that separates `x` from
`closure (convex_hull ℝ (extreme_points ℝ s))`.
3. Look at the extreme (actually exposed) subset of
`s \ closure (convex_hull ℝ (extreme_points ℝ s))` obtained by going the furthest away from the
separating hyperplane. It is nonempty by assumption of nonemptiness and compactness, so by the
lemma it has an extreme point.
4. This point is also an extreme point of `s`. Absurd.
## Related theorems
When the space is finite dimensional, the `closure` can be dropped to strengthen the result of the
Krein-Milman theorem. This leads to the Minkowski-Carathéodory theorem (currently not in mathlib).
Birkhoff's theorem is the Minkowski-Carathéodory theorem applied to the set of bistochastic
matrices, permutation matrices being the extreme points.
## References
See chapter 8 of [Barry Simon, *Convexity*][simon2011]
-/
open set
open_locale classical
variables {E : Type*} [add_comm_group E] [module ℝ E] [topological_space E] [t2_space E]
[topological_add_group E] [has_continuous_smul ℝ E] [locally_convex_space ℝ E] {s : set E}
/-- **Krein-Milman lemma**: In a LCTVS, any nonempty compact set has an extreme point. -/
lemma is_compact.has_extreme_point (hscomp : is_compact s) (hsnemp : s.nonempty) :
(s.extreme_points ℝ).nonempty :=
begin
let S : set (set E) := {t | t.nonempty ∧ is_closed t ∧ is_extreme ℝ s t},
rsuffices ⟨t, ⟨⟨x, hxt⟩, htclos, hst⟩, hBmin⟩ : ∃ t ∈ S, ∀ u ∈ S, u ⊆ t → u = t,
{ refine ⟨x, mem_extreme_points_iff_extreme_singleton.2 _⟩,
rwa ←eq_singleton_iff_unique_mem.2 ⟨hxt, λ y hyB, _⟩,
by_contra hyx,
obtain ⟨l, hl⟩ := geometric_hahn_banach_point_point hyx,
obtain ⟨z, hzt, hz⟩ := (is_compact_of_is_closed_subset hscomp htclos hst.1).exists_forall_ge
⟨x, hxt⟩ l.continuous.continuous_on,
have h : is_exposed ℝ t {z ∈ t | ∀ w ∈ t, l w ≤ l z} := λ h, ⟨l, rfl⟩,
rw ←hBmin {z ∈ t | ∀ w ∈ t, l w ≤ l z} ⟨⟨z, hzt, hz⟩, h.is_closed htclos, hst.trans
h.is_extreme⟩ (t.sep_subset _) at hyB,
exact hl.not_le (hyB.2 x hxt) },
refine zorn_superset _ (λ F hFS hF, _),
obtain rfl | hFnemp := F.eq_empty_or_nonempty,
{ exact ⟨s, ⟨hsnemp, hscomp.is_closed, is_extreme.rfl⟩, λ _, false.elim⟩ },
refine ⟨⋂₀ F, ⟨_, is_closed_sInter $ λ t ht, (hFS ht).2.1, is_extreme_sInter hFnemp $
λ t ht, (hFS ht).2.2⟩, λ t ht, sInter_subset_of_mem ht⟩,
haveI : nonempty ↥F := hFnemp.to_subtype,
rw sInter_eq_Inter,
refine is_compact.nonempty_Inter_of_directed_nonempty_compact_closed _ (λ t u, _)
(λ t, (hFS t.mem).1)
(λ t, is_compact_of_is_closed_subset hscomp (hFS t.mem).2.1 (hFS t.mem).2.2.1)
(λ t, (hFS t.mem).2.1),
obtain htu | hut := hF.total t.mem u.mem,
exacts [⟨t, subset.rfl, htu⟩, ⟨u, hut, subset.rfl⟩],
end
/-- **Krein-Milman theorem**: In a LCTVS, any compact convex set is the closure of the convex hull
of its extreme points. -/
lemma closure_convex_hull_extreme_points (hscomp : is_compact s) (hAconv : convex ℝ s) :
closure (convex_hull ℝ $ s.extreme_points ℝ) = s :=
begin
apply (closure_minimal (convex_hull_min extreme_points_subset hAconv) hscomp.is_closed).antisymm,
by_contra hs,
obtain ⟨x, hxA, hxt⟩ := not_subset.1 hs,
obtain ⟨l, r, hlr, hrx⟩ := geometric_hahn_banach_closed_point (convex_convex_hull _ _).closure
is_closed_closure hxt,
have h : is_exposed ℝ s {y ∈ s | ∀ z ∈ s, l z ≤ l y} := λ _, ⟨l, rfl⟩,
obtain ⟨z, hzA, hz⟩ := hscomp.exists_forall_ge ⟨x, hxA⟩ l.continuous.continuous_on,
obtain ⟨y, hy⟩ := (h.is_compact hscomp).has_extreme_point ⟨z, hzA, hz⟩,
linarith [hlr _ (subset_closure $ subset_convex_hull _ _ $
h.is_extreme.extreme_points_subset_extreme_points hy), hy.1.2 x hxA],
end
|
53143d0a7a5a19623bdbf8adb23623468ce39d70 | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/analysis/complex/basic.lean | 8e2f096e22ee520363e76b6cc7dda4e7e2a72b4a | [
"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 | 7,976 | lean | /-
Copyright (c) Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import analysis.calculus.deriv analysis.normed_space.finite_dimension
/-!
# Normed space structure on `ℂ`.
This file gathers basic facts on complex numbers of an analytic nature.
## Main results
This file registers `ℂ` as a normed field, expresses basic properties of the norm, and gives
tools on the real vector space structure of `ℂ`. Notably, in the namespace `complex`,
it defines functions:
* `linear_map.re`
* `continuous_linear_map.re`
* `linear_map.im`
* `continuous_linear_map.im`
* `linear_map.of_real`
* `continuous_linear_map.of_real`
They are bundled versions of the real part, the imaginary part, and the embedding of `ℝ` in `ℂ`,
as `ℝ`-linear maps.
`has_deriv_at_real_of_complex` expresses that, if a function on `ℂ` is differentiable (over `ℂ`),
then its restriction to `ℝ` is differentiable over `ℝ`, with derivative the real part of the
complex derivative.
-/
noncomputable theory
set_option class.instance_max_depth 40
namespace complex
instance : normed_field ℂ :=
{ norm := abs,
dist_eq := λ _ _, rfl,
norm_mul' := abs_mul,
.. complex.field }
instance : nondiscrete_normed_field ℂ :=
{ non_trivial := ⟨2, by simp [norm]; norm_num⟩ }
instance normed_algebra_over_reals : normed_algebra ℝ ℂ :=
{ norm_algebra_map_eq := abs_of_real,
..complex.algebra_over_reals }
@[simp] lemma norm_eq_abs (z : ℂ) : ∥z∥ = abs z := rfl
@[simp] lemma norm_real (r : ℝ) : ∥(r : ℂ)∥ = ∥r∥ := abs_of_real _
@[simp] lemma norm_rat (r : ℚ) : ∥(r : ℂ)∥ = _root_.abs (r : ℝ) :=
suffices ∥((r : ℝ) : ℂ)∥ = _root_.abs r, by simpa,
by rw [norm_real, real.norm_eq_abs]
@[simp] lemma norm_nat (n : ℕ) : ∥(n : ℂ)∥ = n := abs_of_nat _
@[simp] lemma norm_int {n : ℤ} : ∥(n : ℂ)∥ = _root_.abs n :=
suffices ∥((n : ℝ) : ℂ)∥ = _root_.abs n, by simpa,
by rw [norm_real, real.norm_eq_abs]
lemma norm_int_of_nonneg {n : ℤ} (hn : 0 ≤ n) : ∥(n : ℂ)∥ = n :=
by rw [norm_int, _root_.abs_of_nonneg]; exact int.cast_nonneg.2 hn
/-- Over the complex numbers, any finite-dimensional spaces is proper (and therefore complete).
We can register this as an instance, as it will not cause problems in instance resolution since
the properness of `ℂ` is already known and there is no metavariable. -/
instance finite_dimensional.proper
(E : Type) [normed_group E] [normed_space ℂ E] [finite_dimensional ℂ E] : proper_space E :=
finite_dimensional.proper ℂ E
attribute [instance, priority 900] complex.finite_dimensional.proper
/-- A complex normed vector space is also a real normed vector space. -/
instance normed_space.restrict_scalars_real (E : Type*) [normed_group E] [normed_space ℂ E] :
normed_space ℝ E := normed_space.restrict_scalars ℝ ℂ
attribute [instance, priority 900] complex.normed_space.restrict_scalars_real
/-- Linear map version of the real part function, from `ℂ` to `ℝ`. -/
def linear_map.re : ℂ →ₗ[ℝ] ℝ :=
{ to_fun := λx, x.re,
add := by simp,
smul := λc x, by { change ((c : ℂ) * x).re = c * x.re, simp } }
@[simp] lemma linear_map.re_apply (z : ℂ) : linear_map.re z = z.re := rfl
/-- Continuous linear map version of the real part function, from `ℂ` to `ℝ`. -/
def continuous_linear_map.re : ℂ →L[ℝ] ℝ :=
linear_map.re.mk_continuous 1 $ λx, begin
change _root_.abs (x.re) ≤ 1 * abs x,
rw one_mul,
exact abs_re_le_abs x
end
@[simp] lemma continuous_linear_map.re_coe :
(coe (continuous_linear_map.re) : ℂ →ₗ[ℝ] ℝ) = linear_map.re := rfl
@[simp] lemma continuous_linear_map.re_apply (z : ℂ) :
(continuous_linear_map.re : ℂ → ℝ) z = z.re := rfl
@[simp] lemma continuous_linear_map.re_norm :
∥continuous_linear_map.re∥ = 1 :=
begin
apply le_antisymm (linear_map.mk_continuous_norm_le _ zero_le_one _),
calc 1 = ∥continuous_linear_map.re (1 : ℂ)∥ : by simp
... ≤ ∥continuous_linear_map.re∥ : by { apply continuous_linear_map.unit_le_op_norm, simp }
end
/-- Linear map version of the imaginary part function, from `ℂ` to `ℝ`. -/
def linear_map.im : ℂ →ₗ[ℝ] ℝ :=
{ to_fun := λx, x.im,
add := by simp,
smul := λc x, by { change ((c : ℂ) * x).im = c * x.im, simp } }
@[simp] lemma linear_map.im_apply (z : ℂ) : linear_map.im z = z.im := rfl
/-- Continuous linear map version of the real part function, from `ℂ` to `ℝ`. -/
def continuous_linear_map.im : ℂ →L[ℝ] ℝ :=
linear_map.im.mk_continuous 1 $ λx, begin
change _root_.abs (x.im) ≤ 1 * abs x,
rw one_mul,
exact complex.abs_im_le_abs x
end
@[simp] lemma continuous_linear_map.im_coe :
(coe (continuous_linear_map.im) : ℂ →ₗ[ℝ] ℝ) = linear_map.im := rfl
@[simp] lemma continuous_linear_map.im_apply (z : ℂ) :
(continuous_linear_map.im : ℂ → ℝ) z = z.im := rfl
@[simp] lemma continuous_linear_map.im_norm :
∥continuous_linear_map.im∥ = 1 :=
begin
apply le_antisymm (linear_map.mk_continuous_norm_le _ zero_le_one _),
calc 1 = ∥continuous_linear_map.im (I : ℂ)∥ : by simp
... ≤ ∥continuous_linear_map.im∥ :
by { apply continuous_linear_map.unit_le_op_norm, rw ← abs_I, exact le_refl _ }
end
/-- Linear map version of the canonical embedding of `ℝ` in `ℂ`. -/
def linear_map.of_real : ℝ →ₗ[ℝ] ℂ :=
{ to_fun := λx, of_real x,
add := by simp,
smul := λc x, by { simp, refl } }
@[simp] lemma linear_map.of_real_apply (x : ℝ) : linear_map.of_real x = x := rfl
/-- Continuous linear map version of the canonical embedding of `ℝ` in `ℂ`. -/
def continuous_linear_map.of_real : ℝ →L[ℝ] ℂ :=
linear_map.of_real.mk_continuous 1 $ λx, by simp
@[simp] lemma continuous_linear_map.of_real_coe :
(coe (continuous_linear_map.of_real) : ℝ →ₗ[ℝ] ℂ) = linear_map.of_real := rfl
@[simp] lemma continuous_linear_map.of_real_apply (x : ℝ) :
(continuous_linear_map.of_real : ℝ → ℂ) x = x := rfl
@[simp] lemma continuous_linear_map.of_real_norm :
∥continuous_linear_map.of_real∥ = 1 :=
begin
apply le_antisymm (linear_map.mk_continuous_norm_le _ zero_le_one _),
calc 1 = ∥continuous_linear_map.of_real (1 : ℝ)∥ : by simp
... ≤ ∥continuous_linear_map.of_real∥ :
by { apply continuous_linear_map.unit_le_op_norm, simp }
end
lemma continuous_linear_map.of_real_isometry :
isometry continuous_linear_map.of_real :=
continuous_linear_map.isometry_iff_norm_image_eq_norm.2 (λx, by simp)
end complex
section real_deriv_of_complex
/-! ### Differentiability of the restriction to `ℝ` of complex functions -/
open complex
variables {e : ℂ → ℂ} {e' : ℂ} {z : ℝ}
/-- If a complex function is differentiable at a real point, then the induced real function is also
differentiable at this point, with a derivative equal to the real part of the complex derivative. -/
-- FIXME this causes a deterministic timeout with `-T50000`
theorem has_deriv_at_real_of_complex (h : has_deriv_at e e' z) :
has_deriv_at (λx:ℝ, (e x).re) e'.re z :=
begin
have : (λx:ℝ, (e x).re) = (continuous_linear_map.re : ℂ → ℝ) ∘ e ∘ (continuous_linear_map.of_real : ℝ → ℂ),
by { ext x, refl },
rw this,
have A : has_fderiv_at continuous_linear_map.of_real continuous_linear_map.of_real z :=
continuous_linear_map.of_real.has_fderiv_at,
have B : has_fderiv_at e ((continuous_linear_map.smul_right 1 e' : ℂ →L[ℂ] ℂ).restrict_scalars ℝ)
(continuous_linear_map.of_real z) :=
(has_deriv_at_iff_has_fderiv_at.1 h).restrict_scalars ℝ,
have C : has_fderiv_at continuous_linear_map.re continuous_linear_map.re
(e (continuous_linear_map.of_real z)) := continuous_linear_map.re.has_fderiv_at,
convert has_fderiv_at_iff_has_deriv_at.1 (C.comp z (B.comp z A)),
change e' = 1 * e',
rw one_mul
end
end real_deriv_of_complex
|
1511b7ec3cfde055b3cda2a4a94a284d1bec18ad | b70447c014d9e71cf619ebc9f539b262c19c2e0b | /hott/algebra/homotopy_group.hlean | ccae578598d36d6e44e91a43d3018c1912a7da53 | [
"Apache-2.0"
] | permissive | ia0/lean2 | c20d8da69657f94b1d161f9590a4c635f8dc87f3 | d86284da630acb78fa5dc3b0b106153c50ffccd0 | refs/heads/master | 1,611,399,322,751 | 1,495,751,007,000 | 1,495,751,007,000 | 93,104,167 | 0 | 0 | null | 1,496,355,488,000 | 1,496,355,487,000 | null | UTF-8 | Lean | false | false | 11,081 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
homotopy groups of a pointed space
-/
import .trunc_group types.trunc .group_theory types.nat.hott
open nat eq pointed trunc is_trunc algebra group function equiv unit is_equiv nat
-- TODO: consistently make n an argument before A
-- TODO: rename cghomotopy_group to aghomotopy_group
-- TODO: rename homotopy_group_functor_compose to homotopy_group_functor_pcompose
namespace eq
definition inf_pgroup_loop [constructor] [instance] (A : Type*) : inf_pgroup (Ω A) :=
inf_pgroup.mk concat con.assoc inverse idp_con con_idp con.left_inv
definition inf_group_loop [constructor] (A : Type*) : inf_group (Ω A) := _
definition ab_inf_group_loop [constructor] [instance] (A : Type*) : ab_inf_group (Ω (Ω A)) :=
⦃ab_inf_group, inf_group_loop _, mul_comm := eckmann_hilton⦄
definition gloop [constructor] (A : Type*) : InfGroup :=
InfGroup.mk (Ω A) (inf_group_loop A)
definition homotopy_group [reducible] [constructor] (n : ℕ) (A : Type*) : Set* :=
ptrunc 0 (Ω[n] A)
notation `π[`:95 n:0 `]`:0 := homotopy_group n
definition group_homotopy_group [instance] [constructor] [reducible] (n : ℕ) (A : Type*)
: group (π[succ n] A) :=
trunc_group (Ω[succ n] A)
definition group_homotopy_group2 [instance] (k : ℕ) (A : Type*) :
group (carrier (ptrunctype.to_pType (π[k + 1] A))) :=
group_homotopy_group k A
definition ab_group_homotopy_group [constructor] [reducible] (n : ℕ) (A : Type*)
: ab_group (π[succ (succ n)] A) :=
trunc_ab_group (Ω[succ (succ n)] A)
local attribute ab_group_homotopy_group [instance]
definition ghomotopy_group [constructor] : Π(n : ℕ) [is_succ n] (A : Type*), Group
| (succ n) x A := Group.mk (π[succ n] A) _
definition cghomotopy_group [constructor] :
Π(n : ℕ) [is_at_least_two n] (A : Type*), AbGroup
| (succ (succ n)) x A := AbGroup.mk (π[succ (succ n)] A) _
definition fundamental_group [constructor] (A : Type*) : Group :=
ghomotopy_group 1 A
notation `πg[`:95 n:0 `]`:0 := ghomotopy_group n
notation `πag[`:95 n:0 `]`:0 := cghomotopy_group n
notation `π₁` := fundamental_group -- should this be notation for the group or pointed type?
definition tr_mul_tr {n : ℕ} {A : Type*} (p q : Ω[n + 1] A) :
tr p *[πg[n+1] A] tr q = tr (p ⬝ q) :=
by reflexivity
definition tr_mul_tr' {n : ℕ} {A : Type*} (p q : Ω[succ n] A)
: tr p *[π[succ n] A] tr q = tr (p ⬝ q) :=
idp
definition homotopy_group_pequiv [constructor] (n : ℕ) {A B : Type*} (H : A ≃* B)
: π[n] A ≃* π[n] B :=
ptrunc_pequiv_ptrunc 0 (loopn_pequiv_loopn n H)
definition homotopy_group_pequiv_loop_ptrunc [constructor] (k : ℕ) (A : Type*) :
π[k] A ≃* Ω[k] (ptrunc k A) :=
begin
refine !loopn_ptrunc_pequiv⁻¹ᵉ* ⬝e* _,
exact loopn_pequiv_loopn k (pequiv_of_eq begin rewrite [trunc_index.zero_add] end)
end
open trunc_index
definition homotopy_group_ptrunc_of_le [constructor] {k n : ℕ} (H : k ≤ n) (A : Type*) :
π[k] (ptrunc n A) ≃* π[k] A :=
calc
π[k] (ptrunc n A) ≃* Ω[k] (ptrunc k (ptrunc n A))
: homotopy_group_pequiv_loop_ptrunc k (ptrunc n A)
... ≃* Ω[k] (ptrunc k A)
: loopn_pequiv_loopn k (ptrunc_ptrunc_pequiv_left A (of_nat_le_of_nat H))
... ≃* π[k] A : (homotopy_group_pequiv_loop_ptrunc k A)⁻¹ᵉ*
definition homotopy_group_ptrunc [constructor] (k : ℕ) (A : Type*) :
π[k] (ptrunc k A) ≃* π[k] A :=
homotopy_group_ptrunc_of_le (le.refl k) A
theorem trivial_homotopy_of_is_set (A : Type*) [H : is_set A] (n : ℕ) : πg[n+1] A ≃g G0 :=
begin
apply trivial_group_of_is_contr,
apply is_trunc_trunc_of_is_trunc,
apply is_contr_loop_of_is_trunc,
apply is_trunc_succ_succ_of_is_set
end
definition homotopy_group_succ_out (A : Type*) (n : ℕ) : π[n + 1] A = π₁ (Ω[n] A) := idp
definition homotopy_group_succ_in (A : Type*) (n : ℕ) : π[n + 1] A ≃* π[n] (Ω A) :=
ptrunc_pequiv_ptrunc 0 (loopn_succ_in A n)
definition ghomotopy_group_succ_out (A : Type*) (n : ℕ) : πg[n + 1] A = π₁ (Ω[n] A) := idp
definition homotopy_group_succ_in_con {A : Type*} {n : ℕ} (g h : πg[n + 2] A) :
homotopy_group_succ_in A (succ n) (g * h) =
homotopy_group_succ_in A (succ n) g * homotopy_group_succ_in A (succ n) h :=
begin
induction g with p, induction h with q, esimp,
apply ap tr, apply loopn_succ_in_con
end
definition ghomotopy_group_succ_in [constructor] (A : Type*) (n : ℕ) :
πg[n + 2] A ≃g πg[n + 1] (Ω A) :=
begin
fapply isomorphism_of_equiv,
{ exact homotopy_group_succ_in A (succ n)},
{ exact homotopy_group_succ_in_con},
end
definition homotopy_group_functor [constructor] (n : ℕ) {A B : Type*} (f : A →* B)
: π[n] A →* π[n] B :=
ptrunc_functor 0 (apn n f)
notation `π→[`:95 n:0 `]`:0 := homotopy_group_functor n
definition homotopy_group_functor_phomotopy [constructor] (n : ℕ) {A B : Type*} {f g : A →* B}
(p : f ~* g) : π→[n] f ~* π→[n] g :=
ptrunc_functor_phomotopy 0 (apn_phomotopy n p)
definition homotopy_group_functor_pid (n : ℕ) (A : Type*) : π→[n] (pid A) ~* pid (π[n] A) :=
ptrunc_functor_phomotopy 0 !apn_pid ⬝* !ptrunc_functor_pid
definition homotopy_group_functor_compose [constructor] (n : ℕ) {A B C : Type*} (g : B →* C)
(f : A →* B) : π→[n] (g ∘* f) ~* π→[n] g ∘* π→[n] f :=
ptrunc_functor_phomotopy 0 !apn_pcompose ⬝* !ptrunc_functor_pcompose
definition is_equiv_homotopy_group_functor [constructor] (n : ℕ) {A B : Type*} (f : A →* B)
[is_equiv f] : is_equiv (π→[n] f) :=
@(is_equiv_trunc_functor 0 _) !is_equiv_apn
definition homotopy_group_functor_succ_phomotopy_in (n : ℕ) {A B : Type*} (f : A →* B) :
homotopy_group_succ_in B n ∘* π→[n + 1] f ~*
π→[n] (Ω→ f) ∘* homotopy_group_succ_in A n :=
begin
refine !ptrunc_functor_pcompose⁻¹* ⬝* _ ⬝* !ptrunc_functor_pcompose,
exact ptrunc_functor_phomotopy 0 (apn_succ_phomotopy_in n f)
end
definition is_equiv_homotopy_group_functor_ap1 (n : ℕ) {A B : Type*} (f : A →* B)
[is_equiv (π→[n + 1] f)] : is_equiv (π→[n] (Ω→ f)) :=
have is_equiv (homotopy_group_succ_in B n ∘* π→[n + 1] f),
from is_equiv_compose _ (π→[n + 1] f),
have is_equiv (π→[n] (Ω→ f) ∘ homotopy_group_succ_in A n),
from is_equiv.homotopy_closed _ (homotopy_group_functor_succ_phomotopy_in n f),
is_equiv.cancel_right (homotopy_group_succ_in A n) _
definition tinverse [constructor] {X : Type*} : π[1] X →* π[1] X :=
ptrunc_functor 0 pinverse
definition is_equiv_tinverse [constructor] (A : Type*) : is_equiv (@tinverse A) :=
by apply @is_equiv_trunc_functor; apply is_equiv_eq_inverse
definition ptrunc_functor_pinverse [constructor] {X : Type*}
: ptrunc_functor 0 (@pinverse X) ~* @tinverse X :=
begin
fapply phomotopy.mk,
{ reflexivity},
{ reflexivity}
end
definition homotopy_group_functor_mul [constructor] (n : ℕ) {A B : Type*} (g : A →* B)
(p q : πg[n+1] A) :
(π→[n + 1] g) (p *[πg[n+1] A] q) = (π→[n+1] g) p *[πg[n+1] B] (π→[n + 1] g) q :=
begin
unfold [ghomotopy_group, homotopy_group] at *,
refine @trunc.rec _ _ _ (λq, !is_trunc_eq) _ p, clear p, intro p,
refine @trunc.rec _ _ _ (λq, !is_trunc_eq) _ q, clear q, intro q,
apply ap tr, apply apn_con
end
definition homotopy_group_homomorphism [constructor] (n : ℕ) [H : is_succ n] {A B : Type*}
(f : A →* B) : πg[n] A →g πg[n] B :=
begin
induction H with n, fconstructor,
{ exact homotopy_group_functor (n+1) f},
{ apply homotopy_group_functor_mul}
end
notation `π→g[`:95 n:0 `]`:0 := homotopy_group_homomorphism n
definition homotopy_group_isomorphism_of_pequiv [constructor] (n : ℕ) {A B : Type*} (f : A ≃* B)
: πg[n+1] A ≃g πg[n+1] B :=
begin
apply isomorphism.mk (homotopy_group_homomorphism (succ n) f),
esimp, apply is_equiv_trunc_functor, apply is_equiv_apn,
end
definition homotopy_group_add (A : Type*) (n m : ℕ) :
πg[n+m+1] A ≃g πg[n+1] (Ω[m] A) :=
begin
revert A, induction m with m IH: intro A,
{ reflexivity},
{ esimp [loopn, nat.add], refine !ghomotopy_group_succ_in ⬝g _, refine !IH ⬝g _,
apply homotopy_group_isomorphism_of_pequiv,
exact !loopn_succ_in⁻¹ᵉ*}
end
theorem trivial_homotopy_add_of_is_set_loopn {A : Type*} {n : ℕ} (m : ℕ)
(H : is_set (Ω[n] A)) : πg[m+n+1] A ≃g G0 :=
!homotopy_group_add ⬝g !trivial_homotopy_of_is_set
theorem trivial_homotopy_le_of_is_set_loopn {A : Type*} {n : ℕ} (m : ℕ) (H1 : n ≤ m)
(H2 : is_set (Ω[n] A)) : πg[m+1] A ≃g G0 :=
obtain (k : ℕ) (p : n + k = m), from le.elim H1,
isomorphism_of_eq (ap (λx, πg[x+1] A) (p⁻¹ ⬝ add.comm n k)) ⬝g
trivial_homotopy_add_of_is_set_loopn k H2
definition homotopy_group_pequiv_loop_ptrunc_con {k : ℕ} {A : Type*} (p q : πg[k +1] A) :
homotopy_group_pequiv_loop_ptrunc (succ k) A (p * q) =
homotopy_group_pequiv_loop_ptrunc (succ k) A p ⬝
homotopy_group_pequiv_loop_ptrunc (succ k) A q :=
begin
refine _ ⬝ !loopn_pequiv_loopn_con,
exact ap (loopn_pequiv_loopn _ _) !loopn_ptrunc_pequiv_inv_con
end
definition homotopy_group_pequiv_loop_ptrunc_inv_con {k : ℕ} {A : Type*}
(p q : Ω[succ k] (ptrunc (succ k) A)) :
(homotopy_group_pequiv_loop_ptrunc (succ k) A)⁻¹ᵉ* (p ⬝ q) =
(homotopy_group_pequiv_loop_ptrunc (succ k) A)⁻¹ᵉ* p *
(homotopy_group_pequiv_loop_ptrunc (succ k) A)⁻¹ᵉ* q :=
inv_preserve_binary (homotopy_group_pequiv_loop_ptrunc (succ k) A) mul concat
(@homotopy_group_pequiv_loop_ptrunc_con k A) p q
definition ghomotopy_group_ptrunc [constructor] (k : ℕ) (A : Type*) :
πg[k+1] (ptrunc (k+1) A) ≃g πg[k+1] A :=
begin
fapply isomorphism_of_equiv,
{ exact homotopy_group_ptrunc (k+1) A},
{ intro g₁ g₂,
refine _ ⬝ !homotopy_group_pequiv_loop_ptrunc_inv_con,
apply ap ((homotopy_group_pequiv_loop_ptrunc (k+1) A)⁻¹ᵉ*),
refine _ ⬝ !loopn_pequiv_loopn_con ,
apply ap (loopn_pequiv_loopn (k+1) _),
apply homotopy_group_pequiv_loop_ptrunc_con}
end
/- some homomorphisms -/
-- definition is_homomorphism_cast_loopn_succ_eq_in {A : Type*} (n : ℕ) :
-- is_homomorphism (loopn_succ_in A (succ n) : πg[n+1+1] A → πg[n+1] (Ω A)) :=
-- begin
-- intro g h, induction g with g, induction h with h,
-- xrewrite [tr_mul_tr, - + fn_cast_eq_cast_fn _ (λn, tr), tr_mul_tr, ↑cast, -tr_compose,
-- loopn_succ_eq_in_concat, - + tr_compose],
-- end
definition is_mul_hom_inverse (A : Type*) (n : ℕ)
: is_mul_hom (λp, p⁻¹ : (πag[n+2] A) → (πag[n+2] A)) :=
begin
intro g h, exact ap inv (mul.comm g h) ⬝ mul_inv h g,
end
end eq
|
3a3e02a97d7a679f86d4da1f8fa7e8d45d3dc1ac | c5b07d17b3c9fb19e4b302465d237fd1d988c14f | /src/natural_model.lean | 5894ca1342b67c6f73698030fd32ac0164bf77c9 | [
"MIT"
] | permissive | skaslev/papers | acaec61602b28c33d6115e53913b2002136aa29b | f15b379f3c43bbd0a37ac7bb75f4278f7e901389 | refs/heads/master | 1,665,505,770,318 | 1,660,378,602,000 | 1,660,378,602,000 | 14,101,547 | 0 | 1 | MIT | 1,595,414,522,000 | 1,383,542,702,000 | Lean | UTF-8 | Lean | false | false | 2,573 | lean | -- This is based on the talk "Polynomial Functors and Natural Models of Type Theory"
-- by Steve Awodey on the Polynomial Functor Workshop
-- https://youtu.be/RDuNIP4icKI?list=PLhgq-BqyZ7i7R-fGcAmNyWmJBQg1wzex-&t=10765
import functors.family
open fam
abbreviation U := Type*
abbreviation Ω := Prop
abbreviation P := fam
-- U' is a pointed type
def U' := Σ A : U, A
def p (x : U') : U := x.1
def fiber_p_A_iso_A {A} : fiber p A ≃ A :=
⟨λ ⟨⟨A, x⟩, rfl⟩, x,
λ x, ⟨⟨A, x⟩, rfl⟩,
λ ⟨⟨A, x⟩, rfl⟩, rfl,
λ x, rfl⟩
-- The `P` functor from the talk. We prove it's equivalent the one we use below.
def P' (A : U) : U := Σ I : U, fiber p I → A
def P'_iso_P {A} : P' A ≃ P A :=
iso.sigma_subst (λ I, iso.func_left fiber_p_A_iso_A)
def pie (c : P U) : U := Π i, c i
def sig (c : P U) : U := Σ i, c i
-- https://youtu.be/RDuNIP4icKI?t=12445
def lam' (c : P U') : U' := ⟨pie (map p c), λ i, (c i).2⟩
def p_pie_pullback' : p ∘ lam' = pie ∘ map p := rfl
-- Alternative pullback of `p` and `pie`
def R := Σ c : P U, pie c
def lam (x : R) : U' := ⟨pie x.1, x.2⟩
def p_pie_pullback : p ∘ lam = pie ∘ sigma.fst := rfl
-- https://youtu.be/RDuNIP4icKI?t=12866
def Q := Σ c : P U, sig c
def pair (x : Q) : U' := ⟨sig x.1, x.2⟩
def p_sig_pullback : p ∘ pair = sig ∘ sigma.fst := rfl
example : R = sig (of pie) := rfl
example : Q = sig (of sig) := rfl
def R_iso_P_U' : R ≃ P U' :=
⟨λ x, ⟨x.1, λ i, ⟨x.1 i, x.2 i⟩⟩,
λ x, ⟨⟨x, λ i, (x i).1⟩, λ i, (x i).2⟩,
λ ⟨⟨A, B⟩, x⟩, rfl,
λ ⟨A, x⟩, congr_arg _ $ funext $ λ i, sigma.eq rfl rfl⟩
def Q_iso_expl : Q ≃ Σ (A : U) (B : A → U) (a : A), B a :=
⟨λ ⟨⟨A, B⟩, ⟨a, b⟩⟩, ⟨A, B, a, b⟩,
λ ⟨A, B, a, b⟩, ⟨⟨A, B⟩, ⟨a, b⟩⟩,
λ ⟨⟨A, B⟩, ⟨a, b⟩⟩, rfl,
λ ⟨A, B, a, b⟩, rfl⟩
def s (A : U) : Ω := nonempty A
def i (A : Ω) : U := inhabited A
-- https://youtu.be/RDuNIP4icKI?t=13639
def trunc : U → U := i ∘ s
def s_i_eq_id : s ∘ i = id :=
funext $ λ p, propext
⟨λ ⟨⟨x⟩⟩, x,
λ x, ⟨⟨x⟩⟩⟩
def all (c : P Ω) : Ω := ∀ i, c i
def exi (c : P Ω) : Ω := ∃ i, c i
-- https://youtu.be/RDuNIP4icKI?t=13681
def all_as_pie : all = s ∘ pie ∘ map i :=
funext $ λ c, propext
⟨λ x, ⟨λ y, ⟨x y⟩⟩,
λ ⟨x⟩ y, let ⟨z⟩ := x y in z⟩
-- https://youtu.be/RDuNIP4icKI?t=13681
def exi_as_sig : exi = s ∘ sig ∘ map i :=
funext $ λ c, propext
⟨λ ⟨x, y⟩, ⟨⟨x, ⟨y⟩⟩⟩,
λ ⟨⟨x, ⟨y⟩⟩⟩, ⟨x, y⟩⟩
|
fd44bf882dd780826c91749bbbb777a0b68b0c5f | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/data/complex/is_R_or_C.lean | ebdc0c738fd2ade6bb4efba674c29f3e893e32b1 | [
"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 | 32,332 | lean | /-
Copyright (c) 2020 Frédéric Dupuis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Frédéric Dupuis
-/
import data.real.sqrt
import field_theory.tower
import analysis.normed_space.finite_dimension
/-!
# `is_R_or_C`: a typeclass for ℝ or ℂ
This file defines the typeclass `is_R_or_C` intended to have only two instances:
ℝ and ℂ. It is meant for definitions and theorems which hold for both the real and the complex case,
and in particular when the real case follows directly from the complex case by setting `re` to `id`,
`im` to zero and so on. Its API follows closely that of ℂ.
Possible applications include defining inner products and Hilbert spaces for both the real and
complex case. One would produce the definitions and proof for an arbitrary field of this
typeclass, which basically amounts to doing the complex case, and the two cases then fall out
immediately from the two instances of the class.
The instance for `ℝ` is registered in this file.
The instance for `ℂ` is declared in `analysis.complex.basic`.
## Implementation notes
The coercion from reals into an `is_R_or_C` field is done by registering `algebra_map ℝ K` as
a `has_coe_t`. For this to work, we must proceed carefully to avoid problems involving circular
coercions in the case `K=ℝ`; in particular, we cannot use the plain `has_coe` and must set
priorities carefully. This problem was already solved for `ℕ`, and we copy the solution detailed
in `data/nat/cast`. See also Note [coercion into rings] for more details.
In addition, several lemmas need to be set at priority 900 to make sure that they do not override
their counterparts in `complex.lean` (which causes linter errors).
-/
open_locale big_operators
section
local notation `𝓚` := algebra_map ℝ _
/--
This typeclass captures properties shared by ℝ and ℂ, with an API that closely matches that of ℂ.
-/
class is_R_or_C (K : Type*)
extends nondiscrete_normed_field K, normed_algebra ℝ K, complete_space K :=
(re : K →+ ℝ)
(im : K →+ ℝ)
(conj : K →+* K)
(I : K) -- Meant to be set to 0 for K=ℝ
(I_re_ax : re I = 0)
(I_mul_I_ax : I = 0 ∨ I * I = -1)
(re_add_im_ax : ∀ (z : K), 𝓚 (re z) + 𝓚 (im z) * I = z)
(of_real_re_ax : ∀ r : ℝ, re (𝓚 r) = r)
(of_real_im_ax : ∀ r : ℝ, im (𝓚 r) = 0)
(mul_re_ax : ∀ z w : K, re (z * w) = re z * re w - im z * im w)
(mul_im_ax : ∀ z w : K, im (z * w) = re z * im w + im z * re w)
(conj_re_ax : ∀ z : K, re (conj z) = re z)
(conj_im_ax : ∀ z : K, im (conj z) = -(im z))
(conj_I_ax : conj I = -I)
(norm_sq_eq_def_ax : ∀ (z : K), ∥z∥^2 = (re z) * (re z) + (im z) * (im z))
(mul_im_I_ax : ∀ (z : K), (im z) * im I = im z)
(inv_def_ax : ∀ (z : K), z⁻¹ = conj z * 𝓚 ((∥z∥^2)⁻¹))
(div_I_ax : ∀ (z : K), z / I = -(z * I))
end
namespace is_R_or_C
variables {K : Type*} [is_R_or_C K]
local postfix `†`:100 := @is_R_or_C.conj K _
/- The priority must be set at 900 to ensure that coercions are tried in the right order.
See Note [coercion into rings], or `data/nat/cast.lean` for more details. -/
@[priority 900] noncomputable instance algebra_map_coe : has_coe_t ℝ K := ⟨algebra_map ℝ K⟩
lemma of_real_alg (x : ℝ) : (x : K) = x • (1 : K) :=
algebra.algebra_map_eq_smul_one x
lemma algebra_map_eq_of_real : ⇑(algebra_map ℝ K) = coe := rfl
@[simp] lemma re_add_im (z : K) : ((re z) : K) + (im z) * I = z := is_R_or_C.re_add_im_ax z
@[simp, norm_cast] lemma of_real_re : ∀ r : ℝ, re (r : K) = r := is_R_or_C.of_real_re_ax
@[simp, norm_cast] lemma of_real_im : ∀ r : ℝ, im (r : K) = 0 := is_R_or_C.of_real_im_ax
@[simp] lemma mul_re : ∀ z w : K, re (z * w) = re z * re w - im z * im w :=
is_R_or_C.mul_re_ax
@[simp] lemma mul_im : ∀ z w : K, im (z * w) = re z * im w + im z * re w :=
is_R_or_C.mul_im_ax
theorem inv_def (z : K) : z⁻¹ = conj z * ((∥z∥^2)⁻¹:ℝ) :=
is_R_or_C.inv_def_ax z
theorem ext_iff : ∀ {z w : K}, z = w ↔ re z = re w ∧ im z = im w :=
λ z w, { mp := by { rintro rfl, cc },
mpr := by { rintro ⟨h₁,h₂⟩, rw [←re_add_im z, ←re_add_im w, h₁, h₂] } }
theorem ext : ∀ {z w : K}, re z = re w → im z = im w → z = w :=
by { simp_rw ext_iff, cc }
@[simp, norm_cast, priority 900] lemma of_real_zero : ((0 : ℝ) : K) = 0 :=
by rw [of_real_alg, zero_smul]
@[simp] lemma zero_re' : re (0 : K) = (0 : ℝ) := re.map_zero
@[simp, norm_cast, priority 900] lemma of_real_one : ((1 : ℝ) : K) = 1 :=
by rw [of_real_alg, one_smul]
@[simp] lemma one_re : re (1 : K) = 1 := by rw [←of_real_one, of_real_re]
@[simp] lemma one_im : im (1 : K) = 0 := by rw [←of_real_one, of_real_im]
@[simp, norm_cast, priority 900] theorem of_real_inj {z w : ℝ} : (z : K) = (w : K) ↔ z = w :=
{ mp := λ h, by { convert congr_arg re h; simp only [of_real_re] },
mpr := λ h, by rw h }
@[simp] lemma bit0_re (z : K) : re (bit0 z) = bit0 (re z) := by simp [bit0]
@[simp] lemma bit1_re (z : K) : re (bit1 z) = bit1 (re z) :=
by simp only [bit1, add_monoid_hom.map_add, bit0_re, add_right_inj, one_re]
@[simp] lemma bit0_im (z : K) : im (bit0 z) = bit0 (im z) := by simp [bit0]
@[simp] lemma bit1_im (z : K) : im (bit1 z) = bit0 (im z) :=
by simp only [bit1, add_right_eq_self, add_monoid_hom.map_add, bit0_im, one_im]
@[simp, priority 900] theorem of_real_eq_zero {z : ℝ} : (z : K) = 0 ↔ z = 0 :=
by rw [←of_real_zero]; exact of_real_inj
@[simp, norm_cast, priority 900] lemma of_real_add ⦃r s : ℝ⦄ : ((r + s : ℝ) : K) = r + s :=
by { apply (@is_R_or_C.ext_iff K _ ((r + s : ℝ) : K) (r + s)).mpr, simp }
@[simp, norm_cast, priority 900] lemma of_real_bit0 (r : ℝ) : ((bit0 r : ℝ) : K) = bit0 (r : K) :=
ext_iff.2 $ by simp [bit0]
@[simp, norm_cast, priority 900] lemma of_real_bit1 (r : ℝ) : ((bit1 r : ℝ) : K) = bit1 (r : K) :=
ext_iff.2 $ by simp [bit1]
/- Note: This can be proven by `norm_num` once K is proven to be of characteristic zero below. -/
lemma two_ne_zero : (2 : K) ≠ 0 :=
begin
intro h, rw [(show (2 : K) = ((2 : ℝ) : K), by norm_num), ←of_real_zero, of_real_inj] at h,
linarith,
end
@[simp, norm_cast, priority 900] lemma of_real_neg (r : ℝ) : ((-r : ℝ) : K) = -r :=
ext_iff.2 $ by simp
@[simp, norm_cast, priority 900] lemma of_real_mul (r s : ℝ) : ((r * s : ℝ) : K) = r * s :=
ext_iff.2 $ by simp
@[simp, norm_cast] lemma of_real_smul (r x : ℝ) : r • (x : K) = (r : K) * (x : K) :=
by { simp_rw [← smul_eq_mul, of_real_alg r], simp, }
lemma of_real_mul_re (r : ℝ) (z : K) : re (↑r * z) = r * re z :=
by simp only [mul_re, of_real_im, zero_mul, of_real_re, sub_zero]
lemma of_real_mul_im (r : ℝ) (z : K) : im (↑r * z) = r * (im z) :=
by simp only [add_zero, of_real_im, zero_mul, of_real_re, mul_im]
lemma smul_re : ∀ (r : ℝ) (z : K), re (r • z) = r * (re z) :=
λ r z, by { rw algebra.smul_def, apply of_real_mul_re }
lemma smul_im : ∀ (r : ℝ) (z : K), im (r • z) = r * (im z) :=
λ r z, by { rw algebra.smul_def, apply of_real_mul_im }
/-! ### The imaginary unit, `I` -/
/-- The imaginary unit. -/
@[simp] lemma I_re : re (I : K) = 0 := I_re_ax
@[simp] lemma I_im (z : K) : im z * im (I : K) = im z := mul_im_I_ax z
@[simp] lemma I_im' (z : K) : im (I : K) * im z = im z :=
by rw [mul_comm, I_im _]
lemma I_mul_re (z : K) : re (I * z) = - im z :=
by simp only [I_re, zero_sub, I_im', zero_mul, mul_re]
lemma I_mul_I : (I : K) = 0 ∨ (I : K) * I = -1 := I_mul_I_ax
@[simp] lemma conj_re (z : K) : re (conj z) = re z := is_R_or_C.conj_re_ax z
@[simp] lemma conj_im (z : K) : im (conj z) = -(im z) := is_R_or_C.conj_im_ax z
@[simp] lemma conj_I : conj (I : K) = -I := is_R_or_C.conj_I_ax
@[simp] lemma conj_of_real (r : ℝ) : conj (r : K) = (r : K) :=
by { rw ext_iff, simp only [of_real_im, conj_im, eq_self_iff_true, conj_re, and_self, neg_zero] }
@[simp] lemma conj_bit0 (z : K) : conj (bit0 z) = bit0 (conj z) := by simp [bit0, ext_iff]
@[simp] lemma conj_bit1 (z : K) : conj (bit1 z) = bit1 (conj z) := by simp [bit0, ext_iff]
@[simp] lemma conj_neg_I : conj (-I) = (I : K) := by simp [ext_iff]
@[simp] lemma conj_conj (z : K) : conj (conj z) = z := by simp [ext_iff]
lemma conj_involutive : @function.involutive K is_R_or_C.conj := conj_conj
lemma conj_bijective : @function.bijective K K is_R_or_C.conj := conj_involutive.bijective
lemma conj_inj (z w : K) : conj z = conj w ↔ z = w := conj_bijective.1.eq_iff
lemma conj_eq_zero {z : K} : conj z = 0 ↔ z = 0 :=
ring_hom.map_eq_zero conj
lemma conj_eq_re_sub_im (z : K) : conj z = re z - (im z) * I := by { rw ext_iff, simp, }
lemma conj_smul (r : ℝ) (z : K) : conj (r • z) = r • conj z :=
begin
simp_rw conj_eq_re_sub_im,
simp only [smul_re, smul_im, of_real_mul],
rw smul_sub,
simp_rw of_real_alg,
simp,
end
lemma eq_conj_iff_real {z : K} : conj z = z ↔ ∃ r : ℝ, z = (r : K) :=
begin
split,
{ intro h,
suffices : im z = 0,
{ use (re z),
rw ← add_zero (coe _),
convert (re_add_im z).symm, simp [this] },
contrapose! h,
rw ← re_add_im z,
simp only [conj_of_real, ring_hom.map_add, ring_hom.map_mul, conj_I_ax],
rw [add_left_cancel_iff, ext_iff],
simpa [neg_eq_iff_add_eq_zero, add_self_eq_zero] },
{ rintros ⟨r, rfl⟩, apply conj_of_real }
end
variables (K)
/-- Conjugation as a ring equivalence. This is used to convert the inner product into a
sesquilinear product. -/
def conj_to_ring_equiv : K ≃+* Kᵒᵖ :=
{ to_fun := opposite.op ∘ conj,
inv_fun := conj ∘ opposite.unop,
left_inv := λ x, by simp only [conj_conj, function.comp_app, opposite.unop_op],
right_inv := λ x, by simp only [conj_conj, opposite.op_unop, function.comp_app],
map_mul' := λ x y, by simp [mul_comm],
map_add' := λ x y, by simp }
variables {K}
@[simp] lemma ring_equiv_apply {x : K} : (conj_to_ring_equiv K x).unop = x† := rfl
lemma eq_conj_iff_re {z : K} : conj z = z ↔ ((re z) : K) = z :=
eq_conj_iff_real.trans ⟨by rintro ⟨r, rfl⟩; simp, λ h, ⟨_, h.symm⟩⟩
/-- The norm squared function. -/
def norm_sq : monoid_with_zero_hom K ℝ :=
{ to_fun := λ z, re z * re z + im z * im z,
map_zero' := by simp,
map_one' := by simp,
map_mul' := λ z w, by { simp, ring } }
lemma norm_sq_eq_def {z : K} : ∥z∥^2 = (re z) * (re z) + (im z) * (im z) := norm_sq_eq_def_ax z
lemma norm_sq_eq_def' (z : K) : norm_sq z = ∥z∥^2 := by { rw norm_sq_eq_def, refl }
@[simp] lemma norm_sq_of_real (r : ℝ) : ∥(r : K)∥^2 = r * r :=
by simp [norm_sq_eq_def]
lemma norm_sq_zero : norm_sq (0 : K) = 0 := norm_sq.map_zero
lemma norm_sq_one : norm_sq (1 : K) = 1 := norm_sq.map_one
lemma norm_sq_nonneg (z : K) : 0 ≤ norm_sq z :=
add_nonneg (mul_self_nonneg _) (mul_self_nonneg _)
@[simp] lemma norm_sq_eq_zero {z : K} : norm_sq z = 0 ↔ z = 0 :=
by { rw [norm_sq_eq_def'], simp [sq] }
@[simp] lemma norm_sq_pos {z : K} : 0 < norm_sq z ↔ z ≠ 0 :=
by rw [lt_iff_le_and_ne, ne, eq_comm]; simp [norm_sq_nonneg]
@[simp] lemma norm_sq_neg (z : K) : norm_sq (-z) = norm_sq z :=
by simp [norm_sq_eq_def']
@[simp] lemma norm_sq_conj (z : K) : norm_sq (conj z) = norm_sq z := by simp [norm_sq]
@[simp] lemma norm_sq_mul (z w : K) : norm_sq (z * w) = norm_sq z * norm_sq w :=
norm_sq.map_mul z w
lemma norm_sq_add (z w : K) :
norm_sq (z + w) = norm_sq z + norm_sq w + 2 * (re (z * conj w)) :=
by simp [norm_sq, sq]; ring
lemma re_sq_le_norm_sq (z : K) : re z * re z ≤ norm_sq z :=
le_add_of_nonneg_right (mul_self_nonneg _)
lemma im_sq_le_norm_sq (z : K) : im z * im z ≤ norm_sq z :=
le_add_of_nonneg_left (mul_self_nonneg _)
theorem mul_conj (z : K) : z * conj z = ((norm_sq z) : K) :=
by simp [ext_iff, norm_sq, mul_comm, sub_eq_neg_add, add_comm]
theorem add_conj (z : K) : z + conj z = 2 * (re z) :=
by simp [ext_iff, two_mul]
/-- The pseudo-coercion `of_real` as a `ring_hom`. -/
noncomputable def of_real_hom : ℝ →+* K := algebra_map ℝ K
/-- The coercion from reals as a `ring_hom`. -/
noncomputable def coe_hom : ℝ →+* K := ⟨coe, of_real_one, of_real_mul, of_real_zero, of_real_add⟩
@[simp, norm_cast, priority 900] lemma of_real_sub (r s : ℝ) : ((r - s : ℝ) : K) = r - s :=
ext_iff.2 $ by simp
@[simp, norm_cast, priority 900] lemma of_real_pow (r : ℝ) (n : ℕ) : ((r ^ n : ℝ) : K) = r ^ n :=
by induction n; simp [*, of_real_mul, pow_succ]
theorem sub_conj (z : K) : z - conj z = (2 * im z) * I :=
by simp [ext_iff, two_mul, sub_eq_add_neg, add_mul, mul_im_I_ax]
lemma norm_sq_sub (z w : K) : norm_sq (z - w) =
norm_sq z + norm_sq w - 2 * re (z * conj w) :=
by simp [-mul_re, norm_sq_add, add_comm, add_left_comm, sub_eq_add_neg]
lemma sqrt_norm_sq_eq_norm {z : K} : real.sqrt (norm_sq z) = ∥z∥ :=
begin
have h₂ : ∥z∥ = real.sqrt (∥z∥^2) := (real.sqrt_sq (norm_nonneg z)).symm,
rw [h₂],
exact congr_arg real.sqrt (norm_sq_eq_def' z)
end
/-! ### Inversion -/
@[simp] lemma inv_re (z : K) : re (z⁻¹) = re z / norm_sq z :=
by simp [inv_def, norm_sq_eq_def, norm_sq, division_def]
@[simp] lemma inv_im (z : K) : im (z⁻¹) = im (-z) / norm_sq z :=
by simp [inv_def, norm_sq_eq_def, norm_sq, division_def]
@[simp, norm_cast, priority 900] lemma of_real_inv (r : ℝ) : ((r⁻¹ : ℝ) : K) = r⁻¹ :=
begin
rw ext_iff, by_cases r = 0, { simp [h] },
{ simp; field_simp [h, norm_sq] },
end
protected lemma inv_zero : (0⁻¹ : K) = 0 :=
by rw [← of_real_zero, ← of_real_inv, inv_zero]
protected theorem mul_inv_cancel {z : K} (h : z ≠ 0) : z * z⁻¹ = 1 :=
by rw [inv_def, ←mul_assoc, mul_conj, ←of_real_mul, ←norm_sq_eq_def',
mul_inv_cancel (mt norm_sq_eq_zero.1 h), of_real_one]
lemma div_re (z w : K) : re (z / w) = re z * re w / norm_sq w + im z * im w / norm_sq w :=
by simp [div_eq_mul_inv, mul_assoc, sub_eq_add_neg]
lemma div_im (z w : K) : im (z / w) = im z * re w / norm_sq w - re z * im w / norm_sq w :=
by simp [div_eq_mul_inv, mul_assoc, sub_eq_add_neg, add_comm]
@[simp, norm_cast, priority 900] lemma of_real_div (r s : ℝ) : ((r / s : ℝ) : K) = r / s :=
(@is_R_or_C.coe_hom K _).map_div r s
lemma div_re_of_real {z : K} {r : ℝ} : re (z / r) = re z / r :=
begin
by_cases h : r = 0,
{ simp [h, of_real_zero] },
{ change r ≠ 0 at h,
rw [div_eq_mul_inv, ←of_real_inv, div_eq_mul_inv],
simp [norm_sq, div_mul_eq_div_mul_one_div, div_self h] }
end
@[simp, norm_cast, priority 900] lemma of_real_fpow (r : ℝ) (n : ℤ) : ((r ^ n : ℝ) : K) = r ^ n :=
(@is_R_or_C.coe_hom K _).map_fpow r n
lemma I_mul_I_of_nonzero : (I : K) ≠ 0 → (I : K) * I = -1 :=
by { have := I_mul_I_ax, tauto }
@[simp] lemma div_I (z : K) : z / I = -(z * I) :=
begin
by_cases h : (I : K) = 0,
{ simp [h] },
{ field_simp [mul_assoc, I_mul_I_of_nonzero h] }
end
@[simp] lemma inv_I : (I : K)⁻¹ = -I :=
by { by_cases h : (I : K) = 0; field_simp [h] }
@[simp] lemma norm_sq_inv (z : K) : norm_sq z⁻¹ = (norm_sq z)⁻¹ :=
(@norm_sq K _).map_inv z
@[simp] lemma norm_sq_div (z w : K) : norm_sq (z / w) = norm_sq z / norm_sq w :=
(@norm_sq K _).map_div z w
lemma norm_conj {z : K} : ∥conj z∥ = ∥z∥ :=
by simp only [←sqrt_norm_sq_eq_norm, norm_sq_conj]
lemma conj_inv {z : K} : conj (z⁻¹) = (conj z)⁻¹ :=
by simp only [inv_def, norm_conj, ring_hom.map_mul, conj_of_real]
lemma conj_div {z w : K} : conj (z / w) = (conj z) / (conj w) :=
by rw [div_eq_inv_mul, div_eq_inv_mul, ring_hom.map_mul]; simp only [conj_inv]
/-! ### Cast lemmas -/
@[simp, norm_cast, priority 900] theorem of_real_nat_cast (n : ℕ) : ((n : ℝ) : K) = n :=
of_real_hom.map_nat_cast n
@[simp, norm_cast] lemma nat_cast_re (n : ℕ) : re (n : K) = n :=
by rw [← of_real_nat_cast, of_real_re]
@[simp, norm_cast] lemma nat_cast_im (n : ℕ) : im (n : K) = 0 :=
by rw [← of_real_nat_cast, of_real_im]
@[simp, norm_cast, priority 900] theorem of_real_int_cast (n : ℤ) : ((n : ℝ) : K) = n :=
of_real_hom.map_int_cast n
@[simp, norm_cast] lemma int_cast_re (n : ℤ) : re (n : K) = n :=
by rw [← of_real_int_cast, of_real_re]
@[simp, norm_cast] lemma int_cast_im (n : ℤ) : im (n : K) = 0 :=
by rw [← of_real_int_cast, of_real_im]
@[simp, norm_cast, priority 900] theorem of_real_rat_cast (n : ℚ) : ((n : ℝ) : K) = n :=
(@is_R_or_C.of_real_hom K _).map_rat_cast n
@[simp, norm_cast] lemma rat_cast_re (q : ℚ) : re (q : K) = q :=
by rw [← of_real_rat_cast, of_real_re]
@[simp, norm_cast] lemma rat_cast_im (q : ℚ) : im (q : K) = 0 :=
by rw [← of_real_rat_cast, of_real_im]
/-! ### Characteristic zero -/
-- TODO: I think this can be instance, because it is a `Prop`
/--
ℝ and ℂ are both of characteristic zero.
Note: This is not registered as an instance to avoid having multiple instances on ℝ and ℂ.
-/
lemma char_zero_R_or_C : char_zero K :=
char_zero_of_inj_zero $ λ n h,
by rwa [← of_real_nat_cast, of_real_eq_zero, nat.cast_eq_zero] at h
theorem re_eq_add_conj (z : K) : ↑(re z) = (z + conj z) / 2 :=
begin
haveI : char_zero K := char_zero_R_or_C,
rw [add_conj, mul_div_cancel_left ((re z):K) two_ne_zero'],
end
theorem im_eq_conj_sub (z : K) : ↑(im z) = I * (conj z - z) / 2 :=
begin
rw [← neg_inj, ← of_real_neg, ← I_mul_re, re_eq_add_conj],
simp [mul_add, sub_eq_add_neg, neg_div']
end
/-! ### Absolute value -/
/-- The complex absolute value function, defined as the square root of the norm squared. -/
@[pp_nodot] noncomputable def abs (z : K) : ℝ := (norm_sq z).sqrt
local notation `abs'` := has_abs.abs
local notation `absK` := @abs K _
@[simp, norm_cast] lemma abs_of_real (r : ℝ) : absK r = abs' r :=
by simp [abs, norm_sq, norm_sq_of_real, real.sqrt_mul_self_eq_abs]
lemma norm_eq_abs (z : K) : ∥z∥ = absK z := by simp [abs, norm_sq_eq_def']
lemma abs_of_nonneg {r : ℝ} (h : 0 ≤ r) : absK r = r :=
(abs_of_real _).trans (abs_of_nonneg h)
lemma abs_of_nat (n : ℕ) : absK n = n :=
by { rw [← of_real_nat_cast], exact abs_of_nonneg (nat.cast_nonneg n) }
lemma mul_self_abs (z : K) : abs z * abs z = norm_sq z :=
real.mul_self_sqrt (norm_sq_nonneg _)
@[simp] lemma abs_zero : absK 0 = 0 := by simp [abs]
@[simp] lemma abs_one : absK 1 = 1 := by simp [abs]
@[simp] lemma abs_two : absK 2 = 2 :=
calc absK 2 = absK (2 : ℝ) : by rw [of_real_bit0, of_real_one]
... = (2 : ℝ) : abs_of_nonneg (by norm_num)
lemma abs_nonneg (z : K) : 0 ≤ absK z :=
real.sqrt_nonneg _
@[simp] lemma abs_eq_zero {z : K} : absK z = 0 ↔ z = 0 :=
(real.sqrt_eq_zero $ norm_sq_nonneg _).trans norm_sq_eq_zero
lemma abs_ne_zero {z : K} : abs z ≠ 0 ↔ z ≠ 0 :=
not_congr abs_eq_zero
@[simp] lemma abs_conj (z : K) : abs (conj z) = abs z :=
by simp [abs]
@[simp] lemma abs_mul (z w : K) : abs (z * w) = abs z * abs w :=
by rw [abs, norm_sq_mul, real.sqrt_mul (norm_sq_nonneg _)]; refl
lemma abs_re_le_abs (z : K) : abs' (re z) ≤ abs z :=
by rw [mul_self_le_mul_self_iff (_root_.abs_nonneg (re z)) (abs_nonneg _),
abs_mul_abs_self, mul_self_abs];
apply re_sq_le_norm_sq
lemma abs_im_le_abs (z : K) : abs' (im z) ≤ abs z :=
by rw [mul_self_le_mul_self_iff (_root_.abs_nonneg (im z)) (abs_nonneg _),
abs_mul_abs_self, mul_self_abs];
apply im_sq_le_norm_sq
lemma norm_re_le_norm (z : K) : ∥re z∥ ≤ ∥z∥ :=
by { rw [is_R_or_C.norm_eq_abs, real.norm_eq_abs], exact is_R_or_C.abs_re_le_abs _, }
lemma norm_im_le_norm (z : K) : ∥im z∥ ≤ ∥z∥ :=
by { rw [is_R_or_C.norm_eq_abs, real.norm_eq_abs], exact is_R_or_C.abs_im_le_abs _, }
lemma re_le_abs (z : K) : re z ≤ abs z :=
(abs_le.1 (abs_re_le_abs _)).2
lemma im_le_abs (z : K) : im z ≤ abs z :=
(abs_le.1 (abs_im_le_abs _)).2
lemma im_eq_zero_of_le {a : K} (h : abs a ≤ re a) : im a = 0 :=
begin
rw ← zero_eq_mul_self,
have : re a * re a = re a * re a + im a * im a,
{ convert is_R_or_C.mul_self_abs a;
linarith [re_le_abs a] },
linarith
end
lemma re_eq_self_of_le {a : K} (h : abs a ≤ re a) : (re a : K) = a :=
by { rw ← re_add_im a, simp [im_eq_zero_of_le h] }
lemma abs_add (z w : K) : abs (z + w) ≤ abs z + abs w :=
(mul_self_le_mul_self_iff (abs_nonneg _)
(add_nonneg (abs_nonneg _) (abs_nonneg _))).2 $
begin
rw [mul_self_abs, add_mul_self_eq, mul_self_abs, mul_self_abs,
add_right_comm, norm_sq_add, add_le_add_iff_left,
mul_assoc, mul_le_mul_left (@zero_lt_two ℝ _ _)],
simpa [-mul_re] using re_le_abs (z * conj w)
end
instance : is_absolute_value absK :=
{ abv_nonneg := abs_nonneg,
abv_eq_zero := λ _, abs_eq_zero,
abv_add := abs_add,
abv_mul := abs_mul }
open is_absolute_value
@[simp] lemma abs_abs (z : K) : abs' (abs z) = abs z :=
_root_.abs_of_nonneg (abs_nonneg _)
@[simp] lemma abs_pos {z : K} : 0 < abs z ↔ z ≠ 0 := abv_pos abs
@[simp] lemma abs_neg : ∀ z : K, abs (-z) = abs z := abv_neg abs
lemma abs_sub : ∀ z w : K, abs (z - w) = abs (w - z) := abv_sub abs
lemma abs_sub_le : ∀ a b c : K, abs (a - c) ≤ abs (a - b) + abs (b - c) := abv_sub_le abs
@[simp] theorem abs_inv : ∀ z : K, abs z⁻¹ = (abs z)⁻¹ := abv_inv abs
@[simp] theorem abs_div : ∀ z w : K, abs (z / w) = abs z / abs w := abv_div abs
lemma abs_abs_sub_le_abs_sub : ∀ z w : K, abs' (abs z - abs w) ≤ abs (z - w) :=
abs_abv_sub_le_abv_sub abs
lemma abs_re_div_abs_le_one (z : K) : abs' (re z / abs z) ≤ 1 :=
begin
by_cases hz : z = 0,
{ simp [hz, zero_le_one] },
{ simp_rw [_root_.abs_div, abs_abs, div_le_iff (abs_pos.2 hz), one_mul, abs_re_le_abs] }
end
lemma abs_im_div_abs_le_one (z : K) : abs' (im z / abs z) ≤ 1 :=
begin
by_cases hz : z = 0,
{ simp [hz, zero_le_one] },
{ simp_rw [_root_.abs_div, abs_abs, div_le_iff (abs_pos.2 hz), one_mul, abs_im_le_abs] }
end
@[simp, norm_cast] lemma abs_cast_nat (n : ℕ) : abs (n : K) = n :=
by rw [← of_real_nat_cast, abs_of_nonneg (nat.cast_nonneg n)]
lemma norm_sq_eq_abs (x : K) : norm_sq x = abs x ^ 2 :=
by rw [abs, sq, real.mul_self_sqrt (norm_sq_nonneg _)]
lemma re_eq_abs_of_mul_conj (x : K) : re (x * (conj x)) = abs (x * (conj x)) :=
by rw [mul_conj, of_real_re, abs_of_real, norm_sq_eq_abs, sq, _root_.abs_mul, abs_abs]
lemma abs_sq_re_add_conj (x : K) : (abs (x + x†))^2 = (re (x + x†))^2 :=
by simp [sq, ←norm_sq_eq_abs, norm_sq]
lemma abs_sq_re_add_conj' (x : K) : (abs (x† + x))^2 = (re (x† + x))^2 :=
by simp [sq, ←norm_sq_eq_abs, norm_sq]
lemma conj_mul_eq_norm_sq_left (x : K) : x† * x = ((norm_sq x) : K) :=
begin
rw ext_iff,
refine ⟨by simp [of_real_re, mul_re, conj_re, conj_im, norm_sq],_⟩,
simp [of_real_im, mul_im, conj_im, conj_re, mul_comm],
end
/-! ### Cauchy sequences -/
theorem is_cau_seq_re (f : cau_seq K abs) : is_cau_seq abs' (λ n, re (f n)) :=
λ ε ε0, (f.cauchy ε0).imp $ λ i H j ij,
lt_of_le_of_lt (by simpa using abs_re_le_abs (f j - f i)) (H _ ij)
theorem is_cau_seq_im (f : cau_seq K abs) : is_cau_seq abs' (λ n, im (f n)) :=
λ ε ε0, (f.cauchy ε0).imp $ λ i H j ij,
lt_of_le_of_lt (by simpa using abs_im_le_abs (f j - f i)) (H _ ij)
/-- The real part of a K Cauchy sequence, as a real Cauchy sequence. -/
noncomputable def cau_seq_re (f : cau_seq K abs) : cau_seq ℝ abs' :=
⟨_, is_cau_seq_re f⟩
/-- The imaginary part of a K Cauchy sequence, as a real Cauchy sequence. -/
noncomputable def cau_seq_im (f : cau_seq K abs) : cau_seq ℝ abs' :=
⟨_, is_cau_seq_im f⟩
lemma is_cau_seq_abs {f : ℕ → K} (hf : is_cau_seq abs f) :
is_cau_seq abs' (abs ∘ f) :=
λ ε ε0, let ⟨i, hi⟩ := hf ε ε0 in
⟨i, λ j hj, lt_of_le_of_lt (abs_abs_sub_le_abs_sub _ _) (hi j hj)⟩
@[simp, norm_cast, priority 900] lemma of_real_prod {α : Type*} (s : finset α) (f : α → ℝ) :
((∏ i in s, f i : ℝ) : K) = ∏ i in s, (f i : K) :=
ring_hom.map_prod _ _ _
@[simp, norm_cast, priority 900] lemma of_real_sum {α : Type*} (s : finset α) (f : α → ℝ) :
((∑ i in s, f i : ℝ) : K) = ∑ i in s, (f i : K) :=
ring_hom.map_sum _ _ _
@[simp, norm_cast] lemma of_real_finsupp_sum
{α M : Type*} [has_zero M] (f : α →₀ M) (g : α → M → ℝ) :
((f.sum (λ a b, g a b) : ℝ) : K) = f.sum (λ a b, ((g a b) : K)) :=
ring_hom.map_finsupp_sum _ f g
@[simp, norm_cast] lemma of_real_finsupp_prod
{α M : Type*} [has_zero M] (f : α →₀ M) (g : α → M → ℝ) :
((f.prod (λ a b, g a b) : ℝ) : K) = f.prod (λ a b, ((g a b) : K)) :=
ring_hom.map_finsupp_prod _ f g
end is_R_or_C
namespace finite_dimensional
variables {K : Type*} [is_R_or_C K]
open_locale classical
open is_R_or_C
/-- This instance generates a type-class problem with a metavariable `?m` that should satisfy
`is_R_or_C ?m`. Since this can only be satisfied by `ℝ` or `ℂ`, this does not cause problems. -/
library_note "is_R_or_C instance"
/-- An `is_R_or_C` field is finite-dimensional over `ℝ`, since it is spanned by `{1, I}`. -/
@[nolint dangerous_instance] instance is_R_or_C_to_real : finite_dimensional ℝ K :=
is_noetherian.iff_fg.mpr ⟨⟨{1, I},
begin
rw eq_top_iff,
intros a _,
rw [finset.coe_insert, finset.coe_singleton, submodule.mem_span_insert],
refine ⟨re a, (im a) • I, _, _⟩,
{ rw submodule.mem_span_singleton,
use im a },
simp [re_add_im a, algebra.smul_def, algebra_map_eq_of_real]
end⟩⟩
/-- Over an `is_R_or_C` field, we can register the properness of finite-dimensional normed spaces as
an instance. -/
@[priority 900, nolint dangerous_instance] instance proper_is_R_or_C -- note [is_R_or_C instance]
{E : Type*} [normed_group E] [normed_space K E] [finite_dimensional K E] :
proper_space E :=
begin
letI : normed_space ℝ E := restrict_scalars.normed_space ℝ K E,
letI : is_scalar_tower ℝ K E := restrict_scalars.is_scalar_tower _ _ _,
letI : finite_dimensional ℝ E := finite_dimensional.trans ℝ K E,
apply_instance
end
end finite_dimensional
section instances
noncomputable instance real.is_R_or_C : is_R_or_C ℝ :=
{ re := add_monoid_hom.id ℝ,
im := 0,
conj := ring_hom.id ℝ,
I := 0,
I_re_ax := by simp only [add_monoid_hom.map_zero],
I_mul_I_ax := or.intro_left _ rfl,
re_add_im_ax := λ z, by unfold_coes; simp [add_zero, id.def, mul_zero],
of_real_re_ax := λ r, by simp only [add_monoid_hom.id_apply, algebra.id.map_eq_self],
of_real_im_ax := λ r, by simp only [add_monoid_hom.zero_apply],
mul_re_ax := λ z w,
by simp only [sub_zero, mul_zero, add_monoid_hom.zero_apply, add_monoid_hom.id_apply],
mul_im_ax := λ z w, by simp only [add_zero, zero_mul, mul_zero, add_monoid_hom.zero_apply],
conj_re_ax := λ z, by simp only [ring_hom.id_apply],
conj_im_ax := λ z, by simp only [neg_zero, add_monoid_hom.zero_apply],
conj_I_ax := by simp only [ring_hom.map_zero, neg_zero],
norm_sq_eq_def_ax := λ z, by simp only [sq, norm, ←abs_mul, abs_mul_self z, add_zero,
mul_zero, add_monoid_hom.zero_apply, add_monoid_hom.id_apply],
mul_im_I_ax := λ z, by simp only [mul_zero, add_monoid_hom.zero_apply],
inv_def_ax := λ z, by simp [sq, real.norm_eq_abs, abs_mul_abs_self, ← div_eq_mul_inv],
div_I_ax := λ z, by simp only [div_zero, mul_zero, neg_zero]}
end instances
namespace is_R_or_C
section cleanup_lemmas
local notation `reR` := @is_R_or_C.re ℝ _
local notation `imR` := @is_R_or_C.im ℝ _
local notation `conjR` := @is_R_or_C.conj ℝ _
local notation `IR` := @is_R_or_C.I ℝ _
local notation `absR` := @is_R_or_C.abs ℝ _
local notation `norm_sqR` := @is_R_or_C.norm_sq ℝ _
@[simp] lemma re_to_real {x : ℝ} : reR x = x := rfl
@[simp] lemma im_to_real {x : ℝ} : imR x = 0 := rfl
@[simp] lemma conj_to_real {x : ℝ} : conjR x = x := rfl
@[simp] lemma I_to_real : IR = 0 := rfl
@[simp] lemma norm_sq_to_real {x : ℝ} : norm_sq x = x*x := by simp [is_R_or_C.norm_sq]
@[simp] lemma abs_to_real {x : ℝ} : absR x = has_abs.abs x :=
by simp [is_R_or_C.abs, abs, real.sqrt_mul_self_eq_abs]
@[simp] lemma coe_real_eq_id : @coe ℝ ℝ _ = id := rfl
end cleanup_lemmas
section linear_maps
variables {K : Type*} [is_R_or_C K]
/-- The real part in a `is_R_or_C` field, as a linear map. -/
noncomputable def re_lm : K →ₗ[ℝ] ℝ :=
{ map_smul' := smul_re, .. re }
@[simp] lemma re_lm_coe : (re_lm : K → ℝ) = re := rfl
/-- The real part in a `is_R_or_C` field, as a continuous linear map. -/
noncomputable def re_clm : K →L[ℝ] ℝ :=
linear_map.mk_continuous re_lm 1 $ by
{ simp only [norm_eq_abs, re_lm_coe, one_mul, abs_to_real], exact abs_re_le_abs, }
@[simp] lemma re_clm_norm : ∥(re_clm : K →L[ℝ] ℝ)∥ = 1 :=
begin
apply le_antisymm (linear_map.mk_continuous_norm_le _ zero_le_one _),
convert continuous_linear_map.ratio_le_op_norm _ (1 : K),
simp,
end
@[simp, norm_cast] lemma re_clm_coe : ((re_clm : K →L[ℝ] ℝ) : K →ₗ[ℝ] ℝ) = re_lm := rfl
@[simp] lemma re_clm_apply : ((re_clm : K →L[ℝ] ℝ) : K → ℝ) = re := rfl
@[continuity] lemma continuous_re : continuous (re : K → ℝ) := re_clm.continuous
/-- The imaginary part in a `is_R_or_C` field, as a linear map. -/
noncomputable def im_lm : K →ₗ[ℝ] ℝ :=
{ map_smul' := smul_im, .. im }
@[simp] lemma im_lm_coe : (im_lm : K → ℝ) = im := rfl
/-- The imaginary part in a `is_R_or_C` field, as a continuous linear map. -/
noncomputable def im_clm : K →L[ℝ] ℝ :=
linear_map.mk_continuous im_lm 1 $ by
{ simp only [norm_eq_abs, re_lm_coe, one_mul, abs_to_real], exact abs_im_le_abs, }
@[simp, norm_cast] lemma im_clm_coe : ((im_clm : K →L[ℝ] ℝ) : K →ₗ[ℝ] ℝ) = im_lm := rfl
@[simp] lemma im_clm_apply : ((im_clm : K →L[ℝ] ℝ) : K → ℝ) = im := rfl
@[continuity] lemma continuous_im : continuous (im : K → ℝ) := im_clm.continuous
/-- Conjugate as an `ℝ`-algebra equivalence -/
noncomputable def conj_ae : K ≃ₐ[ℝ] K :=
{ inv_fun := conj,
left_inv := conj_conj,
right_inv := conj_conj,
commutes' := conj_of_real,
.. conj }
@[simp] lemma conj_ae_coe : (conj_ae : K → K) = conj := rfl
/-- Conjugate as a linear isometry -/
noncomputable def conj_lie : K ≃ₗᵢ[ℝ] K := ⟨conj_ae.to_linear_equiv, λ z, by simp [norm_eq_abs]⟩
@[simp] lemma conj_lie_apply : (conj_lie : K → K) = conj := rfl
/-- Conjugate as a continuous linear equivalence -/
noncomputable def conj_cle : K ≃L[ℝ] K := @conj_lie K _
@[simp] lemma conj_cle_coe : (@conj_cle K _).to_linear_equiv = conj_ae.to_linear_equiv := rfl
@[simp] lemma conj_cle_apply : (conj_cle : K → K) = conj := rfl
@[simp] lemma conj_cle_norm : ∥(@conj_cle K _ : K →L[ℝ] K)∥ = 1 :=
(@conj_lie K _).to_linear_isometry.norm_to_continuous_linear_map
@[continuity] lemma continuous_conj : continuous (conj : K → K) := conj_lie.continuous
/-- The `ℝ → K` coercion, as a linear map -/
noncomputable def of_real_am : ℝ →ₐ[ℝ] K := algebra.of_id ℝ K
@[simp] lemma of_real_am_coe : (of_real_am : ℝ → K) = coe := rfl
/-- The ℝ → K coercion, as a linear isometry -/
noncomputable def of_real_li : ℝ →ₗᵢ[ℝ] K :=
{ to_linear_map := of_real_am.to_linear_map, norm_map' := by simp [norm_eq_abs] }
@[simp] lemma of_real_li_apply : (of_real_li : ℝ → K) = coe := rfl
/-- The `ℝ → K` coercion, as a continuous linear map -/
noncomputable def of_real_clm : ℝ →L[ℝ] K := of_real_li.to_continuous_linear_map
@[simp] lemma of_real_clm_coe : ((@of_real_clm K _) : ℝ →ₗ[ℝ] K) = of_real_am.to_linear_map := rfl
@[simp] lemma of_real_clm_apply : (of_real_clm : ℝ → K) = coe := rfl
@[simp] lemma of_real_clm_norm : ∥(of_real_clm : ℝ →L[ℝ] K)∥ = 1 :=
linear_isometry.norm_to_continuous_linear_map of_real_li
@[continuity] lemma continuous_of_real : continuous (coe : ℝ → K) := of_real_li.continuous
end linear_maps
end is_R_or_C
section normalization
variables {K : Type*} [is_R_or_C K]
variables {E : Type*} [normed_group E] [normed_space K E]
open is_R_or_C
/- Note: one might think the following lemma belongs in `analysis.normed_space.basic`. But it
can't be placed there, because that file is an import of `data.complex.is_R_or_C`! -/
/-- Lemma to normalize a vector in a normed space `E` over either `ℂ` or `ℝ` to unit length. -/
@[simp] lemma norm_smul_inv_norm {x : E} (hx : x ≠ 0) : ∥(∥x∥⁻¹ : K) • x∥ = 1 :=
begin
have h : ∥(∥x∥ : K)∥ = ∥x∥,
{ rw norm_eq_abs,
exact abs_of_nonneg (norm_nonneg _) },
have : ∥x∥ ≠ 0 := by simp [hx],
field_simp [norm_smul, h]
end
end normalization
|
9b8d550569cc00b82eca0291d2d08edfc3143622 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/elab_error_recovery.lean | 6bfb9d213c170265dbe9557fe9b559dc96e0167e | [
"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 | 454 | lean | def half_baked : ℕ → ℕ
| 3 := 2
-- type mismatches
| 0 := 1 + ""
-- placeholders
| 5 := _ + 4
-- missing typeclass instances
| 42 := if 2 ∈ 3 then 3 else _
-- exceptions during tactic evaluation
| 7 := by do undefined
-- nested elaboration errors
| 10 := begin exact [] end
-- missing cases
#print half_baked._main
#reduce half_baked 3
#reduce half_baked 5
#eval half_baked 3
-- type errors in binders
#check ∀ x : nat.zero, x = x
|
5f409221e271dfb69a9e2a5da9517151cc7f9116 | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/order/complete_lattice.lean | a61730a6820896d5fe2402e4cf0b3cd0b62d00e1 | [
"Apache-2.0"
] | permissive | lacker/mathlib | f2439c743c4f8eb413ec589430c82d0f73b2d539 | ddf7563ac69d42cfa4a1bfe41db1fed521bd795f | refs/heads/master | 1,671,948,326,773 | 1,601,479,268,000 | 1,601,479,268,000 | 298,686,743 | 0 | 0 | Apache-2.0 | 1,601,070,794,000 | 1,601,070,794,000 | null | UTF-8 | Lean | false | false | 38,187 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import order.bounds
/-!
# Theory of complete lattices
## Main definitions
* `Sup` and `Inf` are the supremum and the infimum of a set;
* `supr (f : ι → α)` and `infi (f : ι → α)` are indexed supremum and infimum of a function,
defined as `Sup` and `Inf` of the range of this function;
* `class complete_lattice`: a bounded lattice such that `Sup s` is always the least upper boundary
of `s` and `Inf s` is always the greatest lower boundary of `s`;
* `class complete_linear_order`: a linear ordered complete lattice.
## Naming conventions
We use `Sup`/`Inf`/`supr`/`infi` for the corresponding functions in the statement. Sometimes we
also use `bsupr`/`binfi` for "bounded` supremum or infimum, i.e. one of `⨆ i ∈ s, f i`,
`⨆ i (hi : p i), f i`, or more generally `⨆ i (hi : p i), f i hi`.
## Notation
* `⨆ i, f i` : `supr f`, the supremum of the range of `f`;
* `⨅ i, f i` : `infi f`, the infimum of the range of `f`.
-/
set_option old_structure_cmd true
open set
variables {α β β₂ : Type*} {ι ι₂ : Sort*}
/-- class for the `Sup` operator -/
class has_Sup (α : Type*) := (Sup : set α → α)
/-- class for the `Inf` operator -/
class has_Inf (α : Type*) := (Inf : set α → α)
export has_Sup (Sup) has_Inf (Inf)
/-- Supremum of a set -/
add_decl_doc has_Sup.Sup
/-- Infimum of a set -/
add_decl_doc has_Inf.Inf
/-- Indexed supremum -/
def supr [has_Sup α] {ι} (s : ι → α) : α := Sup (range s)
/-- Indexed infimum -/
def infi [has_Inf α] {ι} (s : ι → α) : α := Inf (range s)
@[priority 50] instance has_Inf_to_nonempty (α) [has_Inf α] : nonempty α := ⟨Inf ∅⟩
@[priority 50] instance has_Sup_to_nonempty (α) [has_Sup α] : nonempty α := ⟨Sup ∅⟩
notation `⨆` binders `, ` r:(scoped f, supr f) := r
notation `⨅` binders `, ` r:(scoped f, infi f) := r
instance (α) [has_Inf α] : has_Sup (order_dual α) := ⟨(Inf : set α → α)⟩
instance (α) [has_Sup α] : has_Inf (order_dual α) := ⟨(Sup : set α → α)⟩
/-- A complete lattice is a bounded lattice which
has suprema and infima for every subset. -/
class complete_lattice (α : Type*) extends bounded_lattice α, has_Sup α, has_Inf α :=
(le_Sup : ∀s, ∀a∈s, a ≤ Sup s)
(Sup_le : ∀s a, (∀b∈s, b ≤ a) → Sup s ≤ a)
(Inf_le : ∀s, ∀a∈s, Inf s ≤ a)
(le_Inf : ∀s a, (∀b∈s, a ≤ b) → a ≤ Inf s)
/-- Create a `complete_lattice` from a `partial_order` and `Inf` function
that returns the greatest lower bound of a set. Usually this constructor provides
poor definitional equalities, so it should be used with
`.. complete_lattice_of_Inf α _`. -/
def complete_lattice_of_Inf (α : Type*) [H1 : partial_order α]
[H2 : has_Inf α] (is_glb_Inf : ∀ s : set α, is_glb s (Inf s)) :
complete_lattice α :=
{ bot := Inf univ,
bot_le := λ x, (is_glb_Inf univ).1 trivial,
top := Inf ∅,
le_top := λ a, (is_glb_Inf ∅).2 $ by simp,
sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x},
inf := λ a b, Inf {a, b},
le_inf := λ a b c hab hac, by { apply (is_glb_Inf _).2, simp [*] },
inf_le_right := λ a b, (is_glb_Inf _).1 $ mem_insert_of_mem _ $ mem_singleton _,
inf_le_left := λ a b, (is_glb_Inf _).1 $ mem_insert _ _,
sup_le := λ a b c hac hbc, (is_glb_Inf _).1 $ by simp [*],
le_sup_left := λ a b, (is_glb_Inf _).2 $ λ x, and.left,
le_sup_right := λ a b, (is_glb_Inf _).2 $ λ x, and.right,
le_Inf := λ s a ha, (is_glb_Inf s).2 ha,
Inf_le := λ s a ha, (is_glb_Inf s).1 ha,
Sup := λ s, Inf (upper_bounds s),
le_Sup := λ s a ha, (is_glb_Inf (upper_bounds s)).2 $ λ b hb, hb ha,
Sup_le := λ s a ha, (is_glb_Inf (upper_bounds s)).1 ha,
.. H1, .. H2 }
/-- Create a `complete_lattice` from a `partial_order` and `Sup` function
that returns the least upper bound of a set. Usually this constructor provides
poor definitional equalities, so it should be used with
`.. complete_lattice_of_Sup α _`. -/
def complete_lattice_of_Sup (α : Type*) [H1 : partial_order α]
[H2 : has_Sup α] (is_lub_Sup : ∀ s : set α, is_lub s (Sup s)) :
complete_lattice α :=
{ top := Sup univ,
le_top := λ x, (is_lub_Sup univ).1 trivial,
bot := Sup ∅,
bot_le := λ x, (is_lub_Sup ∅).2 $ by simp,
sup := λ a b, Sup {a, b},
sup_le := λ a b c hac hbc, (is_lub_Sup _).2 (by simp [*]),
le_sup_left := λ a b, (is_lub_Sup _).1 $ mem_insert _ _,
le_sup_right := λ a b, (is_lub_Sup _).1 $ mem_insert_of_mem _ $ mem_singleton _,
inf := λ a b, Sup {x | x ≤ a ∧ x ≤ b},
le_inf := λ a b c hab hac, (is_lub_Sup _).1 $ by simp [*],
inf_le_left := λ a b, (is_lub_Sup _).2 (λ x, and.left),
inf_le_right := λ a b, (is_lub_Sup _).2 (λ x, and.right),
Inf := λ s, Sup (lower_bounds s),
Sup_le := λ s a ha, (is_lub_Sup s).2 ha,
le_Sup := λ s a ha, (is_lub_Sup s).1 ha,
Inf_le := λ s a ha, (is_lub_Sup (lower_bounds s)).2 (λ b hb, hb ha),
le_Inf := λ s a ha, (is_lub_Sup (lower_bounds s)).1 ha,
.. H1, .. H2 }
/-- A complete linear order is a linear order whose lattice structure is complete. -/
class complete_linear_order (α : Type*) extends complete_lattice α, decidable_linear_order α
namespace order_dual
variable (α)
instance [complete_lattice α] : complete_lattice (order_dual α) :=
{ le_Sup := @complete_lattice.Inf_le α _,
Sup_le := @complete_lattice.le_Inf α _,
Inf_le := @complete_lattice.le_Sup α _,
le_Inf := @complete_lattice.Sup_le α _,
.. order_dual.bounded_lattice α, ..order_dual.has_Sup α, ..order_dual.has_Inf α }
instance [complete_linear_order α] : complete_linear_order (order_dual α) :=
{ .. order_dual.complete_lattice α, .. order_dual.decidable_linear_order α }
end order_dual
section
variables [complete_lattice α] {s t : set α} {a b : α}
@[ematch] theorem le_Sup : a ∈ s → a ≤ Sup s := complete_lattice.le_Sup s a
theorem Sup_le : (∀b∈s, b ≤ a) → Sup s ≤ a := complete_lattice.Sup_le s a
@[ematch] theorem Inf_le : a ∈ s → Inf s ≤ a := complete_lattice.Inf_le s a
theorem le_Inf : (∀b∈s, a ≤ b) → a ≤ Inf s := complete_lattice.le_Inf s a
lemma is_lub_Sup (s : set α) : is_lub s (Sup s) := ⟨assume x, le_Sup, assume x, Sup_le⟩
lemma is_lub.Sup_eq (h : is_lub s a) : Sup s = a := (is_lub_Sup s).unique h
lemma is_glb_Inf (s : set α) : is_glb s (Inf s) := ⟨assume a, Inf_le, assume a, le_Inf⟩
lemma is_glb.Inf_eq (h : is_glb s a) : Inf s = a := (is_glb_Inf s).unique h
theorem le_Sup_of_le (hb : b ∈ s) (h : a ≤ b) : a ≤ Sup s :=
le_trans h (le_Sup hb)
theorem Inf_le_of_le (hb : b ∈ s) (h : b ≤ a) : Inf s ≤ a :=
le_trans (Inf_le hb) h
theorem Sup_le_Sup (h : s ⊆ t) : Sup s ≤ Sup t :=
(is_lub_Sup s).mono (is_lub_Sup t) h
theorem Inf_le_Inf (h : s ⊆ t) : Inf t ≤ Inf s :=
(is_glb_Inf s).mono (is_glb_Inf t) h
@[simp] theorem Sup_le_iff : Sup s ≤ a ↔ (∀b ∈ s, b ≤ a) :=
is_lub_le_iff (is_lub_Sup s)
@[simp] theorem le_Inf_iff : a ≤ Inf s ↔ (∀b ∈ s, a ≤ b) :=
le_is_glb_iff (is_glb_Inf s)
theorem Sup_le_Sup_of_forall_exists_le (h : ∀ x ∈ s, ∃ y ∈ t, x ≤ y) : Sup s ≤ Sup t :=
le_of_forall_le' (by simp only [Sup_le_iff]; introv h₀ h₁; rcases h _ h₁ with ⟨y,hy,hy'⟩; solve_by_elim [le_trans hy'])
theorem Inf_le_Inf_of_forall_exists_le (h : ∀ x ∈ s, ∃ y ∈ t, y ≤ x) : Inf t ≤ Inf s :=
le_of_forall_le (by simp only [le_Inf_iff]; introv h₀ h₁; rcases h _ h₁ with ⟨y,hy,hy'⟩; solve_by_elim [le_trans _ hy'])
theorem Inf_le_Sup (hs : s.nonempty) : Inf s ≤ Sup s :=
is_glb_le_is_lub (is_glb_Inf s) (is_lub_Sup s) hs
-- TODO: it is weird that we have to add union_def
theorem Sup_union {s t : set α} : Sup (s ∪ t) = Sup s ⊔ Sup t :=
((is_lub_Sup s).union (is_lub_Sup t)).Sup_eq
theorem Sup_inter_le {s t : set α} : Sup (s ∩ t) ≤ Sup s ⊓ Sup t :=
by finish
/-
Sup_le (assume a ⟨a_s, a_t⟩, le_inf (le_Sup a_s) (le_Sup a_t))
-/
theorem Inf_union {s t : set α} : Inf (s ∪ t) = Inf s ⊓ Inf t :=
((is_glb_Inf s).union (is_glb_Inf t)).Inf_eq
theorem le_Inf_inter {s t : set α} : Inf s ⊔ Inf t ≤ Inf (s ∩ t) :=
@Sup_inter_le (order_dual α) _ _ _
@[simp] theorem Sup_empty : Sup ∅ = (⊥ : α) :=
is_lub_empty.Sup_eq
@[simp] theorem Inf_empty : Inf ∅ = (⊤ : α) :=
(@is_glb_empty α _).Inf_eq
@[simp] theorem Sup_univ : Sup univ = (⊤ : α) :=
(@is_lub_univ α _).Sup_eq
@[simp] theorem Inf_univ : Inf univ = (⊥ : α) :=
is_glb_univ.Inf_eq
-- TODO(Jeremy): get this automatically
@[simp] theorem Sup_insert {a : α} {s : set α} : Sup (insert a s) = a ⊔ Sup s :=
((is_lub_Sup s).insert a).Sup_eq
@[simp] theorem Inf_insert {a : α} {s : set α} : Inf (insert a s) = a ⊓ Inf s :=
((is_glb_Inf s).insert a).Inf_eq
theorem Sup_le_Sup_of_subset_instert_bot (h : s ⊆ insert ⊥ t) : Sup s ≤ Sup t :=
le_trans (Sup_le_Sup h) (le_of_eq (trans Sup_insert bot_sup_eq))
theorem Inf_le_Inf_of_subset_insert_top (h : s ⊆ insert ⊤ t) : Inf t ≤ Inf s :=
le_trans (le_of_eq (trans top_inf_eq.symm Inf_insert.symm)) (Inf_le_Inf h)
-- We will generalize this to conditionally complete lattices in `cSup_singleton`.
theorem Sup_singleton {a : α} : Sup {a} = a :=
is_lub_singleton.Sup_eq
-- We will generalize this to conditionally complete lattices in `cInf_singleton`.
theorem Inf_singleton {a : α} : Inf {a} = a :=
is_glb_singleton.Inf_eq
theorem Sup_pair {a b : α} : Sup {a, b} = a ⊔ b :=
(@is_lub_pair α _ a b).Sup_eq
theorem Inf_pair {a b : α} : Inf {a, b} = a ⊓ b :=
(@is_glb_pair α _ a b).Inf_eq
@[simp] theorem Inf_eq_top : Inf s = ⊤ ↔ (∀a∈s, a = ⊤) :=
iff.intro
(assume h a ha, top_unique $ h ▸ Inf_le ha)
(assume h, top_unique $ le_Inf $ assume a ha, top_le_iff.2 $ h a ha)
@[simp] theorem Sup_eq_bot : Sup s = ⊥ ↔ (∀a∈s, a = ⊥) :=
@Inf_eq_top (order_dual α) _ _
end
section complete_linear_order
variables [complete_linear_order α] {s t : set α} {a b : α}
lemma Inf_lt_iff : Inf s < b ↔ (∃a∈s, a < b) :=
is_glb_lt_iff (is_glb_Inf s)
lemma lt_Sup_iff : b < Sup s ↔ (∃a∈s, b < a) :=
lt_is_lub_iff (is_lub_Sup s)
lemma Sup_eq_top : Sup s = ⊤ ↔ (∀b<⊤, ∃a∈s, b < a) :=
iff.intro
(assume (h : Sup s = ⊤) b hb, by rwa [←h, lt_Sup_iff] at hb)
(assume h, top_unique $ le_of_not_gt $ assume h',
let ⟨a, ha, h⟩ := h _ h' in
lt_irrefl a $ lt_of_le_of_lt (le_Sup ha) h)
lemma Inf_eq_bot : Inf s = ⊥ ↔ (∀b>⊥, ∃a∈s, a < b) :=
@Sup_eq_top (order_dual α) _ _
lemma lt_supr_iff {f : ι → α} : a < supr f ↔ (∃i, a < f i) :=
lt_Sup_iff.trans exists_range_iff
lemma infi_lt_iff {f : ι → α} : infi f < a ↔ (∃i, f i < a) :=
Inf_lt_iff.trans exists_range_iff
end complete_linear_order
/-
### supr & infi
-/
section
variables [complete_lattice α] {s t : ι → α} {a b : α}
-- TODO: this declaration gives error when starting smt state
--@[ematch]
theorem le_supr (s : ι → α) (i : ι) : s i ≤ supr s :=
le_Sup ⟨i, rfl⟩
@[ematch] theorem le_supr' (s : ι → α) (i : ι) : (: s i ≤ supr s :) :=
le_Sup ⟨i, rfl⟩
/- TODO: this version would be more powerful, but, alas, the pattern matcher
doesn't accept it.
@[ematch] theorem le_supr' (s : ι → α) (i : ι) : (: s i :) ≤ (: supr s :) :=
le_Sup ⟨i, rfl⟩
-/
lemma is_lub_supr : is_lub (range s) (⨆j, s j) := is_lub_Sup _
lemma is_lub.supr_eq (h : is_lub (range s) a) : (⨆j, s j) = a := h.Sup_eq
lemma is_glb_infi : is_glb (range s) (⨅j, s j) := is_glb_Inf _
lemma is_glb.infi_eq (h : is_glb (range s) a) : (⨅j, s j) = a := h.Inf_eq
theorem le_supr_of_le (i : ι) (h : a ≤ s i) : a ≤ supr s :=
le_trans h (le_supr _ i)
theorem le_bsupr {p : ι → Prop} {f : Π i (h : p i), α} (i : ι) (hi : p i) :
f i hi ≤ ⨆ i hi, f i hi :=
le_supr_of_le i $ le_supr (f i) hi
theorem supr_le (h : ∀i, s i ≤ a) : supr s ≤ a :=
Sup_le $ assume b ⟨i, eq⟩, eq ▸ h i
theorem bsupr_le {p : ι → Prop} {f : Π i (h : p i), α} (h : ∀ i hi, f i hi ≤ a) :
(⨆ i (hi : p i), f i hi) ≤ a :=
supr_le $ λ i, supr_le $ h i
theorem bsupr_le_supr (p : ι → Prop) (f : ι → α) : (⨆ i (H : p i), f i) ≤ ⨆ i, f i :=
bsupr_le (λ i hi, le_supr f i)
theorem supr_le_supr (h : ∀i, s i ≤ t i) : supr s ≤ supr t :=
supr_le $ assume i, le_supr_of_le i (h i)
theorem supr_le_supr2 {t : ι₂ → α} (h : ∀i, ∃j, s i ≤ t j) : supr s ≤ supr t :=
supr_le $ assume j, exists.elim (h j) le_supr_of_le
theorem bsupr_le_bsupr {p : ι → Prop} {f g : Π i (hi : p i), α} (h : ∀ i hi, f i hi ≤ g i hi) :
(⨆ i hi, f i hi) ≤ ⨆ i hi, g i hi :=
bsupr_le $ λ i hi, le_trans (h i hi) (le_bsupr i hi)
theorem supr_le_supr_const (h : ι → ι₂) : (⨆ i:ι, a) ≤ (⨆ j:ι₂, a) :=
supr_le $ le_supr _ ∘ h
@[simp] theorem supr_le_iff : supr s ≤ a ↔ (∀i, s i ≤ a) :=
(is_lub_le_iff is_lub_supr).trans forall_range_iff
theorem Sup_eq_supr {s : set α} : Sup s = (⨆a ∈ s, a) :=
le_antisymm
(Sup_le $ assume b h, le_supr_of_le b $ le_supr _ h)
(supr_le $ assume b, supr_le $ assume h, le_Sup h)
lemma le_supr_iff : (a ≤ supr s) ↔ (∀ b, (∀ i, s i ≤ b) → a ≤ b) :=
⟨λ h b hb, le_trans h (supr_le hb), λ h, h _ $ λ i, le_supr s i⟩
lemma monotone.le_map_supr [complete_lattice β] {f : α → β} (hf : monotone f) :
(⨆ i, f (s i)) ≤ f (supr s) :=
supr_le $ λ i, hf $ le_supr _ _
lemma monotone.le_map_supr2 [complete_lattice β] {f : α → β} (hf : monotone f)
{ι' : ι → Sort*} (s : Π i, ι' i → α) :
(⨆ i (h : ι' i), f (s i h)) ≤ f (⨆ i (h : ι' i), s i h) :=
calc (⨆ i h, f (s i h)) ≤ (⨆ i, f (⨆ h, s i h)) :
supr_le_supr $ λ i, hf.le_map_supr
... ≤ f (⨆ i (h : ι' i), s i h) : hf.le_map_supr
lemma monotone.le_map_Sup [complete_lattice β] {s : set α} {f : α → β} (hf : monotone f) :
(⨆a∈s, f a) ≤ f (Sup s) :=
by rw [Sup_eq_supr]; exact hf.le_map_supr2 _
lemma supr_comp_le {ι' : Sort*} (f : ι' → α) (g : ι → ι') :
(⨆ x, f (g x)) ≤ ⨆ y, f y :=
supr_le_supr2 $ λ x, ⟨_, le_refl _⟩
lemma monotone.supr_comp_eq [preorder β] {f : β → α} (hf : monotone f)
{s : ι → β} (hs : ∀ x, ∃ i, x ≤ s i) :
(⨆ x, f (s x)) = ⨆ y, f y :=
le_antisymm (supr_comp_le _ _) (supr_le_supr2 $ λ x, (hs x).imp $ λ i hi, hf hi)
lemma function.surjective.supr_comp {α : Type*} [has_Sup α] {f : ι → ι₂}
(hf : function.surjective f) (g : ι₂ → α) :
(⨆ x, g (f x)) = ⨆ y, g y :=
by simp only [supr, hf.range_comp]
lemma supr_congr {α : Type*} [has_Sup α] {f : ι → α} {g : ι₂ → α} (h : ι → ι₂)
(h1 : function.surjective h) (h2 : ∀ x, g (h x) = f x) : (⨆ x, f x) = ⨆ y, g y :=
by { convert h1.supr_comp g, exact (funext h2).symm }
-- TODO: finish doesn't do well here.
@[congr] theorem supr_congr_Prop {α : Type*} [has_Sup α] {p q : Prop} {f₁ : p → α} {f₂ : q → α}
(pq : p ↔ q) (f : ∀x, f₁ (pq.mpr x) = f₂ x) : supr f₁ = supr f₂ :=
begin
have : f₁ ∘ pq.mpr = f₂ := funext f,
rw [← this],
refine (function.surjective.supr_comp (λ h, ⟨pq.1 h, _⟩) f₁).symm,
refl
end
theorem infi_le (s : ι → α) (i : ι) : infi s ≤ s i :=
Inf_le ⟨i, rfl⟩
@[ematch] theorem infi_le' (s : ι → α) (i : ι) : (: infi s ≤ s i :) :=
Inf_le ⟨i, rfl⟩
theorem infi_le_of_le (i : ι) (h : s i ≤ a) : infi s ≤ a :=
le_trans (infi_le _ i) h
theorem binfi_le {p : ι → Prop} {f : Π i (hi : p i), α} (i : ι) (hi : p i) :
(⨅ i hi, f i hi) ≤ f i hi :=
infi_le_of_le i $ infi_le (f i) hi
theorem le_infi (h : ∀i, a ≤ s i) : a ≤ infi s :=
le_Inf $ assume b ⟨i, eq⟩, eq ▸ h i
theorem le_binfi {p : ι → Prop} {f : Π i (h : p i), α} (h : ∀ i hi, a ≤ f i hi) :
a ≤ ⨅ i hi, f i hi :=
le_infi $ λ i, le_infi $ h i
theorem infi_le_binfi (p : ι → Prop) (f : ι → α) : (⨅ i, f i) ≤ ⨅ i (H : p i), f i :=
le_binfi (λ i hi, infi_le f i)
theorem infi_le_infi (h : ∀i, s i ≤ t i) : infi s ≤ infi t :=
le_infi $ assume i, infi_le_of_le i (h i)
theorem infi_le_infi2 {t : ι₂ → α} (h : ∀j, ∃i, s i ≤ t j) : infi s ≤ infi t :=
le_infi $ assume j, exists.elim (h j) infi_le_of_le
theorem binfi_le_binfi {p : ι → Prop} {f g : Π i (h : p i), α} (h : ∀ i hi, f i hi ≤ g i hi) :
(⨅ i hi, f i hi) ≤ ⨅ i hi, g i hi :=
le_binfi $ λ i hi, le_trans (binfi_le i hi) (h i hi)
theorem infi_le_infi_const (h : ι₂ → ι) : (⨅ i:ι, a) ≤ (⨅ j:ι₂, a) :=
le_infi $ infi_le _ ∘ h
@[simp] theorem le_infi_iff : a ≤ infi s ↔ (∀i, a ≤ s i) :=
⟨assume : a ≤ infi s, assume i, le_trans this (infi_le _ _), le_infi⟩
theorem Inf_eq_infi {s : set α} : Inf s = (⨅a ∈ s, a) :=
@Sup_eq_supr (order_dual α) _ _
lemma monotone.map_infi_le [complete_lattice β] {f : α → β} (hf : monotone f) :
f (infi s) ≤ (⨅ i, f (s i)) :=
le_infi $ λ i, hf $ infi_le _ _
lemma monotone.map_infi2_le [complete_lattice β] {f : α → β} (hf : monotone f)
{ι' : ι → Sort*} (s : Π i, ι' i → α) :
f (⨅ i (h : ι' i), s i h) ≤ (⨅ i (h : ι' i), f (s i h)) :=
@monotone.le_map_supr2 (order_dual α) (order_dual β) _ _ _ f hf.order_dual _ _
lemma monotone.map_Inf_le [complete_lattice β] {s : set α} {f : α → β} (hf : monotone f) :
f (Inf s) ≤ ⨅ a∈s, f a :=
by rw [Inf_eq_infi]; exact hf.map_infi2_le _
lemma le_infi_comp {ι' : Sort*} (f : ι' → α) (g : ι → ι') :
(⨅ y, f y) ≤ ⨅ x, f (g x) :=
infi_le_infi2 $ λ x, ⟨_, le_refl _⟩
lemma monotone.infi_comp_eq [preorder β] {f : β → α} (hf : monotone f)
{s : ι → β} (hs : ∀ x, ∃ i, s i ≤ x) :
(⨅ x, f (s x)) = ⨅ y, f y :=
le_antisymm (infi_le_infi2 $ λ x, (hs x).imp $ λ i hi, hf hi) (le_infi_comp _ _)
lemma function.surjective.infi_comp {α : Type*} [has_Inf α] {f : ι → ι₂}
(hf : function.surjective f) (g : ι₂ → α) :
(⨅ x, g (f x)) = ⨅ y, g y :=
@function.surjective.supr_comp _ _ (order_dual α) _ f hf g
lemma infi_congr {α : Type*} [has_Inf α] {f : ι → α} {g : ι₂ → α} (h : ι → ι₂)
(h1 : function.surjective h) (h2 : ∀ x, g (h x) = f x) : (⨅ x, f x) = ⨅ y, g y :=
@supr_congr _ _ (order_dual α) _ _ _ h h1 h2
@[congr] theorem infi_congr_Prop {α : Type*} [has_Inf α] {p q : Prop} {f₁ : p → α} {f₂ : q → α}
(pq : p ↔ q) (f : ∀x, f₁ (pq.mpr x) = f₂ x) : infi f₁ = infi f₂ :=
@supr_congr_Prop (order_dual α) _ p q f₁ f₂ pq f
-- We will generalize this to conditionally complete lattices in `cinfi_const`.
theorem infi_const [nonempty ι] {a : α} : (⨅ b:ι, a) = a :=
by rw [infi, range_const, Inf_singleton]
-- We will generalize this to conditionally complete lattices in `csupr_const`.
theorem supr_const [nonempty ι] {a : α} : (⨆ b:ι, a) = a :=
@infi_const (order_dual α) _ _ _ _
@[simp] lemma infi_top : (⨅i:ι, ⊤ : α) = ⊤ :=
top_unique $ le_infi $ assume i, le_refl _
@[simp] lemma supr_bot : (⨆i:ι, ⊥ : α) = ⊥ :=
@infi_top (order_dual α) _ _
@[simp] lemma infi_eq_top : infi s = ⊤ ↔ (∀i, s i = ⊤) :=
Inf_eq_top.trans forall_range_iff
@[simp] lemma supr_eq_bot : supr s = ⊥ ↔ (∀i, s i = ⊥) :=
Sup_eq_bot.trans forall_range_iff
@[simp] lemma infi_pos {p : Prop} {f : p → α} (hp : p) : (⨅ h : p, f h) = f hp :=
le_antisymm (infi_le _ _) (le_infi $ assume h, le_refl _)
@[simp] lemma infi_neg {p : Prop} {f : p → α} (hp : ¬ p) : (⨅ h : p, f h) = ⊤ :=
le_antisymm le_top $ le_infi $ assume h, (hp h).elim
@[simp] lemma supr_pos {p : Prop} {f : p → α} (hp : p) : (⨆ h : p, f h) = f hp :=
le_antisymm (supr_le $ assume h, le_refl _) (le_supr _ _)
@[simp] lemma supr_neg {p : Prop} {f : p → α} (hp : ¬ p) : (⨆ h : p, f h) = ⊥ :=
le_antisymm (supr_le $ assume h, (hp h).elim) bot_le
lemma supr_eq_dif {p : Prop} [decidable p] (a : p → α) :
(⨆h:p, a h) = (if h : p then a h else ⊥) :=
by by_cases p; simp [h]
lemma supr_eq_if {p : Prop} [decidable p] (a : α) :
(⨆h:p, a) = (if p then a else ⊥) :=
supr_eq_dif (λ _, a)
lemma infi_eq_dif {p : Prop} [decidable p] (a : p → α) :
(⨅h:p, a h) = (if h : p then a h else ⊤) :=
@supr_eq_dif (order_dual α) _ _ _ _
lemma infi_eq_if {p : Prop} [decidable p] (a : α) :
(⨅h:p, a) = (if p then a else ⊤) :=
infi_eq_dif (λ _, a)
-- TODO: should this be @[simp]?
theorem infi_comm {f : ι → ι₂ → α} : (⨅i, ⨅j, f i j) = (⨅j, ⨅i, f i j) :=
le_antisymm
(le_infi $ assume i, le_infi $ assume j, infi_le_of_le j $ infi_le _ i)
(le_infi $ assume j, le_infi $ assume i, infi_le_of_le i $ infi_le _ j)
/- TODO: this is strange. In the proof below, we get exactly the desired
among the equalities, but close does not get it.
begin
apply @le_antisymm,
simp, intros,
begin [smt]
ematch, ematch, ematch, trace_state, have := le_refl (f i_1 i),
trace_state, close
end
end
-/
-- TODO: should this be @[simp]?
theorem supr_comm {f : ι → ι₂ → α} : (⨆i, ⨆j, f i j) = (⨆j, ⨆i, f i j) :=
@infi_comm (order_dual α) _ _ _ _
@[simp] theorem infi_infi_eq_left {b : β} {f : Πx:β, x = b → α} :
(⨅x, ⨅h:x = b, f x h) = f b rfl :=
le_antisymm
(infi_le_of_le b $ infi_le _ rfl)
(le_infi $ assume b', le_infi $ assume eq, match b', eq with ._, rfl := le_refl _ end)
@[simp] theorem infi_infi_eq_right {b : β} {f : Πx:β, b = x → α} :
(⨅x, ⨅h:b = x, f x h) = f b rfl :=
le_antisymm
(infi_le_of_le b $ infi_le _ rfl)
(le_infi $ assume b', le_infi $ assume eq, match b', eq with ._, rfl := le_refl _ end)
@[simp] theorem supr_supr_eq_left {b : β} {f : Πx:β, x = b → α} :
(⨆x, ⨆h : x = b, f x h) = f b rfl :=
@infi_infi_eq_left (order_dual α) _ _ _ _
@[simp] theorem supr_supr_eq_right {b : β} {f : Πx:β, b = x → α} :
(⨆x, ⨆h : b = x, f x h) = f b rfl :=
@infi_infi_eq_right (order_dual α) _ _ _ _
attribute [ematch] le_refl
theorem infi_subtype {p : ι → Prop} {f : subtype p → α} : (⨅ x, f x) = (⨅ i (h:p i), f ⟨i, h⟩) :=
le_antisymm
(le_infi $ assume i, le_infi $ assume : p i, infi_le _ _)
(le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _)
lemma infi_subtype' {p : ι → Prop} {f : ∀ i, p i → α} :
(⨅ i (h : p i), f i h) = (⨅ x : subtype p, f x x.property) :=
(@infi_subtype _ _ _ p (λ x, f x.val x.property)).symm
lemma infi_subtype'' {ι} (s : set ι) (f : ι → α) :
(⨅ i : s, f i) = ⨅ (t : ι) (H : t ∈ s), f t :=
infi_subtype
theorem infi_inf_eq {f g : ι → α} : (⨅ x, f x ⊓ g x) = (⨅ x, f x) ⊓ (⨅ x, g x) :=
le_antisymm
(le_inf
(le_infi $ assume i, infi_le_of_le i inf_le_left)
(le_infi $ assume i, infi_le_of_le i inf_le_right))
(le_infi $ assume i, le_inf
(inf_le_left_of_le $ infi_le _ _)
(inf_le_right_of_le $ infi_le _ _))
/- TODO: here is another example where more flexible pattern matching
might help.
begin
apply @le_antisymm,
safe, pose h := f a ⊓ g a, begin [smt] ematch, ematch end
end
-/
lemma infi_inf [h : nonempty ι] {f : ι → α} {a : α} : (⨅x, f x) ⊓ a = (⨅ x, f x ⊓ a) :=
by rw [infi_inf_eq, infi_const]
lemma inf_infi [nonempty ι] {f : ι → α} {a : α} : a ⊓ (⨅x, f x) = (⨅ x, a ⊓ f x) :=
by rw [inf_comm, infi_inf]; simp [inf_comm]
lemma binfi_inf {p : ι → Prop} {f : Π i (hi : p i), α} {a : α} (h : ∃ i, p i) :
(⨅i (h : p i), f i h) ⊓ a = (⨅ i (h : p i), f i h ⊓ a) :=
by haveI : nonempty {i // p i} := (let ⟨i, hi⟩ := h in ⟨⟨i, hi⟩⟩);
rw [infi_subtype', infi_subtype', infi_inf]
theorem supr_sup_eq {f g : β → α} : (⨆ x, f x ⊔ g x) = (⨆ x, f x) ⊔ (⨆ x, g x) :=
@infi_inf_eq (order_dual α) β _ _ _
/- supr and infi under Prop -/
@[simp] theorem infi_false {s : false → α} : infi s = ⊤ :=
le_antisymm le_top (le_infi $ assume i, false.elim i)
@[simp] theorem supr_false {s : false → α} : supr s = ⊥ :=
le_antisymm (supr_le $ assume i, false.elim i) bot_le
@[simp] theorem infi_true {s : true → α} : infi s = s trivial :=
le_antisymm (infi_le _ _) (le_infi $ assume ⟨⟩, le_refl _)
@[simp] theorem supr_true {s : true → α} : supr s = s trivial :=
le_antisymm (supr_le $ assume ⟨⟩, le_refl _) (le_supr _ _)
@[simp] theorem infi_exists {p : ι → Prop} {f : Exists p → α} :
(⨅ x, f x) = (⨅ i, ⨅ h:p i, f ⟨i, h⟩) :=
le_antisymm
(le_infi $ assume i, le_infi $ assume : p i, infi_le _ _)
(le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _)
@[simp] theorem supr_exists {p : ι → Prop} {f : Exists p → α} :
(⨆ x, f x) = (⨆ i, ⨆ h:p i, f ⟨i, h⟩) :=
@infi_exists (order_dual α) _ _ _ _
theorem infi_and {p q : Prop} {s : p ∧ q → α} : infi s = (⨅ h₁ h₂, s ⟨h₁, h₂⟩) :=
le_antisymm
(le_infi $ assume i, le_infi $ assume j, infi_le _ _)
(le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _)
/-- The symmetric case of `infi_and`, useful for rewriting into a infimum over a conjunction -/
lemma infi_and' {p q : Prop} {s : p → q → α} :
(⨅ (h₁ : p) (h₂ : q), s h₁ h₂) = ⨅ (h : p ∧ q), s h.1 h.2 :=
by { symmetry, exact infi_and }
theorem supr_and {p q : Prop} {s : p ∧ q → α} : supr s = (⨆ h₁ h₂, s ⟨h₁, h₂⟩) :=
@infi_and (order_dual α) _ _ _ _
/-- The symmetric case of `supr_and`, useful for rewriting into a supremum over a conjunction -/
lemma supr_and' {p q : Prop} {s : p → q → α} :
(⨆ (h₁ : p) (h₂ : q), s h₁ h₂) = ⨆ (h : p ∧ q), s h.1 h.2 :=
by { symmetry, exact supr_and }
theorem infi_or {p q : Prop} {s : p ∨ q → α} :
infi s = (⨅ h : p, s (or.inl h)) ⊓ (⨅ h : q, s (or.inr h)) :=
le_antisymm
(le_inf
(infi_le_infi2 $ assume j, ⟨_, le_refl _⟩)
(infi_le_infi2 $ assume j, ⟨_, le_refl _⟩))
(le_infi $ assume i, match i with
| or.inl i := inf_le_left_of_le $ infi_le _ _
| or.inr j := inf_le_right_of_le $ infi_le _ _
end)
theorem supr_or {p q : Prop} {s : p ∨ q → α} :
(⨆ x, s x) = (⨆ i, s (or.inl i)) ⊔ (⨆ j, s (or.inr j)) :=
@infi_or (order_dual α) _ _ _ _
lemma Sup_range {α : Type*} [has_Sup α] {f : ι → α} : Sup (range f) = supr f := rfl
lemma Inf_range {α : Type*} [has_Inf α] {f : ι → α} : Inf (range f) = infi f := rfl
lemma supr_range {g : β → α} {f : ι → β} : (⨆b∈range f, g b) = (⨆i, g (f i)) :=
le_antisymm
(supr_le $ assume b, supr_le $ assume ⟨i, (h : f i = b)⟩, h ▸ le_supr _ i)
(supr_le $ assume i, le_supr_of_le (f i) $ le_supr (λp, g (f i)) (mem_range_self _))
lemma infi_range {g : β → α} {f : ι → β} : (⨅b∈range f, g b) = (⨅i, g (f i)) :=
@supr_range (order_dual α) _ _ _ _ _
theorem Inf_image {s : set β} {f : β → α} : Inf (f '' s) = (⨅ a ∈ s, f a) :=
by rw [← infi_subtype'', infi, range_comp, subtype.range_coe]
theorem Sup_image {s : set β} {f : β → α} : Sup (f '' s) = (⨆ a ∈ s, f a) :=
@Inf_image (order_dual α) _ _ _ _
/-
### supr and infi under set constructions
-/
theorem infi_emptyset {f : β → α} : (⨅ x ∈ (∅ : set β), f x) = ⊤ :=
by simp
theorem supr_emptyset {f : β → α} : (⨆ x ∈ (∅ : set β), f x) = ⊥ :=
by simp
theorem infi_univ {f : β → α} : (⨅ x ∈ (univ : set β), f x) = (⨅ x, f x) :=
by simp
theorem supr_univ {f : β → α} : (⨆ x ∈ (univ : set β), f x) = (⨆ x, f x) :=
by simp
theorem infi_union {f : β → α} {s t : set β} : (⨅ x ∈ s ∪ t, f x) = (⨅x∈s, f x) ⊓ (⨅x∈t, f x) :=
by simp only [← infi_inf_eq, infi_or]
lemma infi_split (f : β → α) (p : β → Prop) :
(⨅ i, f i) = (⨅ i (h : p i), f i) ⊓ (⨅ i (h : ¬ p i), f i) :=
by simpa [classical.em] using @infi_union _ _ _ f {i | p i} {i | ¬ p i}
lemma infi_split_single (f : β → α) (i₀ : β) :
(⨅ i, f i) = f i₀ ⊓ (⨅ i (h : i ≠ i₀), f i) :=
by convert infi_split _ _; simp
theorem infi_le_infi_of_subset {f : β → α} {s t : set β} (h : s ⊆ t) :
(⨅ x ∈ t, f x) ≤ (⨅ x ∈ s, f x) :=
by rw [(union_eq_self_of_subset_left h).symm, infi_union]; exact inf_le_left
theorem supr_union {f : β → α} {s t : set β} : (⨆ x ∈ s ∪ t, f x) = (⨆x∈s, f x) ⊔ (⨆x∈t, f x) :=
@infi_union (order_dual α) _ _ _ _ _
lemma supr_split (f : β → α) (p : β → Prop) :
(⨆ i, f i) = (⨆ i (h : p i), f i) ⊔ (⨆ i (h : ¬ p i), f i) :=
@infi_split (order_dual α) _ _ _ _
lemma supr_split_single (f : β → α) (i₀ : β) :
(⨆ i, f i) = f i₀ ⊔ (⨆ i (h : i ≠ i₀), f i) :=
@infi_split_single (order_dual α) _ _ _ _
theorem supr_le_supr_of_subset {f : β → α} {s t : set β} (h : s ⊆ t) :
(⨆ x ∈ s, f x) ≤ (⨆ x ∈ t, f x) :=
@infi_le_infi_of_subset (order_dual α) _ _ _ _ _ h
theorem infi_insert {f : β → α} {s : set β} {b : β} :
(⨅ x ∈ insert b s, f x) = f b ⊓ (⨅x∈s, f x) :=
eq.trans infi_union $ congr_arg (λx:α, x ⊓ (⨅x∈s, f x)) infi_infi_eq_left
theorem supr_insert {f : β → α} {s : set β} {b : β} :
(⨆ x ∈ insert b s, f x) = f b ⊔ (⨆x∈s, f x) :=
eq.trans supr_union $ congr_arg (λx:α, x ⊔ (⨆x∈s, f x)) supr_supr_eq_left
theorem infi_singleton {f : β → α} {b : β} : (⨅ x ∈ (singleton b : set β), f x) = f b :=
by simp
theorem infi_pair {f : β → α} {a b : β} : (⨅ x ∈ ({a, b} : set β), f x) = f a ⊓ f b :=
by rw [infi_insert, infi_singleton]
theorem supr_singleton {f : β → α} {b : β} : (⨆ x ∈ (singleton b : set β), f x) = f b :=
@infi_singleton (order_dual α) _ _ _ _
theorem supr_pair {f : β → α} {a b : β} : (⨆ x ∈ ({a, b} : set β), f x) = f a ⊔ f b :=
by rw [supr_insert, supr_singleton]
lemma infi_image {γ} {f : β → γ} {g : γ → α} {t : set β} :
(⨅ c ∈ f '' t, g c) = (⨅ b ∈ t, g (f b)) :=
by rw [← Inf_image, ← Inf_image, ← image_comp]
lemma supr_image {γ} {f : β → γ} {g : γ → α} {t : set β} :
(⨆ c ∈ f '' t, g c) = (⨆ b ∈ t, g (f b)) :=
@infi_image (order_dual α) _ _ _ _ _ _
/-!
### `supr` and `infi` under `Type`
-/
theorem infi_of_empty' (h : ι → false) {s : ι → α} : infi s = ⊤ :=
top_unique (le_infi $ assume i, (h i).elim)
theorem supr_of_empty' (h : ι → false) {s : ι → α} : supr s = ⊥ :=
bot_unique (supr_le $ assume i, (h i).elim)
theorem infi_of_empty (h : ¬nonempty ι) {s : ι → α} : infi s = ⊤ :=
infi_of_empty' (λ i, h ⟨i⟩)
theorem supr_of_empty (h : ¬nonempty ι) {s : ι → α} : supr s = ⊥ :=
supr_of_empty' (λ i, h ⟨i⟩)
@[simp] theorem infi_empty {s : empty → α} : infi s = ⊤ :=
infi_of_empty nonempty_empty
@[simp] theorem supr_empty {s : empty → α} : supr s = ⊥ :=
supr_of_empty nonempty_empty
@[simp] theorem infi_unit {f : unit → α} : (⨅ x, f x) = f () :=
le_antisymm (infi_le _ _) (le_infi $ assume ⟨⟩, le_refl _)
@[simp] theorem supr_unit {f : unit → α} : (⨆ x, f x) = f () :=
le_antisymm (supr_le $ assume ⟨⟩, le_refl _) (le_supr _ _)
lemma supr_bool_eq {f : bool → α} : (⨆b:bool, f b) = f tt ⊔ f ff :=
le_antisymm
(supr_le $ assume b, match b with tt := le_sup_left | ff := le_sup_right end)
(sup_le (le_supr _ _) (le_supr _ _))
lemma infi_bool_eq {f : bool → α} : (⨅b:bool, f b) = f tt ⊓ f ff :=
@supr_bool_eq (order_dual α) _ _
lemma is_glb_binfi {s : set β} {f : β → α} : is_glb (f '' s) (⨅ x ∈ s, f x) :=
by simpa only [range_comp, subtype.range_coe, infi_subtype'] using @is_glb_infi α s _ (f ∘ coe)
theorem supr_subtype {p : ι → Prop} {f : subtype p → α} : (⨆ x, f x) = (⨆ i (h:p i), f ⟨i, h⟩) :=
@infi_subtype (order_dual α) _ _ _ _
lemma supr_subtype' {p : ι → Prop} {f : ∀ i, p i → α} :
(⨆ i (h : p i), f i h) = (⨆ x : subtype p, f x x.property) :=
(@supr_subtype _ _ _ p (λ x, f x.val x.property)).symm
lemma Sup_eq_supr' {s : set α} : Sup s = ⨆ x : s, (x : α) :=
by rw [Sup_eq_supr, supr_subtype']; refl
lemma is_lub_bsupr {s : set β} {f : β → α} : is_lub (f '' s) (⨆ x ∈ s, f x) :=
by simpa only [range_comp, subtype.range_coe, supr_subtype'] using @is_lub_supr α s _ (f ∘ coe)
theorem infi_sigma {p : β → Type*} {f : sigma p → α} : (⨅ x, f x) = (⨅ i (h:p i), f ⟨i, h⟩) :=
le_antisymm
(le_infi $ assume i, le_infi $ assume : p i, infi_le _ _)
(le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _)
theorem supr_sigma {p : β → Type*} {f : sigma p → α} : (⨆ x, f x) = (⨆ i (h:p i), f ⟨i, h⟩) :=
@infi_sigma (order_dual α) _ _ _ _
theorem infi_prod {γ : Type*} {f : β × γ → α} : (⨅ x, f x) = (⨅ i j, f (i, j)) :=
le_antisymm
(le_infi $ assume i, le_infi $ assume j, infi_le _ _)
(le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _)
theorem supr_prod {γ : Type*} {f : β × γ → α} : (⨆ x, f x) = (⨆ i j, f (i, j)) :=
@infi_prod (order_dual α) _ _ _ _
theorem infi_sum {γ : Type*} {f : β ⊕ γ → α} :
(⨅ x, f x) = (⨅ i, f (sum.inl i)) ⊓ (⨅ j, f (sum.inr j)) :=
le_antisymm
(le_inf
(infi_le_infi2 $ assume i, ⟨_, le_refl _⟩)
(infi_le_infi2 $ assume j, ⟨_, le_refl _⟩))
(le_infi $ assume s, match s with
| sum.inl i := inf_le_left_of_le $ infi_le _ _
| sum.inr j := inf_le_right_of_le $ infi_le _ _
end)
theorem supr_sum {γ : Type*} {f : β ⊕ γ → α} :
(⨆ x, f x) = (⨆ i, f (sum.inl i)) ⊔ (⨆ j, f (sum.inr j)) :=
@infi_sum (order_dual α) _ _ _ _
/-!
### `supr` and `infi` under `ℕ`
-/
lemma supr_ge_eq_supr_nat_add {u : ℕ → α} (n : ℕ) : (⨆ i ≥ n, u i) = ⨆ i, u (i + n) :=
begin
apply le_antisymm;
simp only [supr_le_iff],
{ exact λ i hi, le_Sup ⟨i - n, by { dsimp only, rw nat.sub_add_cancel hi }⟩ },
{ exact λ i, le_Sup ⟨i + n, supr_pos (nat.le_add_left _ _)⟩ }
end
lemma infi_ge_eq_infi_nat_add {u : ℕ → α} (n : ℕ) : (⨅ i ≥ n, u i) = ⨅ i, u (i + n) :=
@supr_ge_eq_supr_nat_add (order_dual α) _ _ _
end
section complete_linear_order
variables [complete_linear_order α]
lemma supr_eq_top (f : ι → α) : supr f = ⊤ ↔ (∀b<⊤, ∃i, b < f i) :=
by simp only [← Sup_range, Sup_eq_top, set.exists_range_iff]
lemma infi_eq_bot (f : ι → α) : infi f = ⊥ ↔ (∀b>⊥, ∃i, f i < b) :=
by simp only [← Inf_range, Inf_eq_bot, set.exists_range_iff]
end complete_linear_order
/-!
### Instances
-/
instance complete_lattice_Prop : complete_lattice Prop :=
{ Sup := λs, ∃a∈s, a,
le_Sup := assume s a h p, ⟨a, h, p⟩,
Sup_le := assume s a h ⟨b, h', p⟩, h b h' p,
Inf := λs, ∀a:Prop, a∈s → a,
Inf_le := assume s a h p, p a h,
le_Inf := assume s a h p b hb, h b hb p,
.. bounded_distrib_lattice_Prop }
lemma Inf_Prop_eq {s : set Prop} : Inf s = (∀p ∈ s, p) := rfl
lemma Sup_Prop_eq {s : set Prop} : Sup s = (∃p ∈ s, p) := rfl
lemma infi_Prop_eq {ι : Sort*} {p : ι → Prop} : (⨅i, p i) = (∀i, p i) :=
le_antisymm (assume h i, h _ ⟨i, rfl⟩ ) (assume h p ⟨i, eq⟩, eq ▸ h i)
lemma supr_Prop_eq {ι : Sort*} {p : ι → Prop} : (⨆i, p i) = (∃i, p i) :=
le_antisymm (λ ⟨q, ⟨i, (eq : p i = q)⟩, hq⟩, ⟨i, eq.symm ▸ hq⟩) (λ ⟨i, hi⟩, ⟨p i, ⟨i, rfl⟩, hi⟩)
instance pi.has_Sup {α : Type*} {β : α → Type*} [Π i, has_Sup (β i)] : has_Sup (Π i, β i) :=
⟨λ s i, ⨆ f : s, (f : Π i, β i) i⟩
instance pi.has_Inf {α : Type*} {β : α → Type*} [Π i, has_Inf (β i)] : has_Inf (Π i, β i) :=
⟨λ s i, ⨅ f : s, (f : Π i, β i) i⟩
instance pi.complete_lattice {α : Type*} {β : α → Type*} [∀ i, complete_lattice (β i)] :
complete_lattice (Π i, β i) :=
{ Sup := Sup,
Inf := Inf,
le_Sup := λ s f hf i, le_supr (λ f : s, (f : Π i, β i) i) ⟨f, hf⟩,
Inf_le := λ s f hf i, infi_le (λ f : s, (f : Π i, β i) i) ⟨f, hf⟩,
Sup_le := λ s f hf i, supr_le $ λ g, hf g g.2 i,
le_Inf := λ s f hf i, le_infi $ λ g, hf g g.2 i,
.. pi.bounded_lattice }
lemma Inf_apply {α : Type*} {β : α → Type*} [Π i, has_Inf (β i)]
{s : set (Πa, β a)} {a : α} :
(Inf s) a = (⨅ f : s, (f : Πa, β a) a) :=
rfl
lemma infi_apply {α : Type*} {β : α → Type*} {ι : Sort*} [Π i, has_Inf (β i)]
{f : ι → Πa, β a} {a : α} :
(⨅i, f i) a = (⨅i, f i a) :=
by rw [infi, Inf_apply, infi, infi, ← image_eq_range (λ f : Π i, β i, f a) (range f), ← range_comp]
lemma Sup_apply {α : Type*} {β : α → Type*} [Π i, has_Sup (β i)] {s : set (Πa, β a)} {a : α} :
(Sup s) a = (⨆f:s, (f : Πa, β a) a) :=
rfl
lemma supr_apply {α : Type*} {β : α → Type*} {ι : Sort*} [Π i, has_Sup (β i)] {f : ι → Πa, β a}
{a : α} :
(⨆i, f i) a = (⨆i, f i a) :=
@infi_apply α (λ i, order_dual (β i)) _ _ f a
section complete_lattice
variables [preorder α] [complete_lattice β]
theorem monotone_Sup_of_monotone {s : set (α → β)} (m_s : ∀f∈s, monotone f) : monotone (Sup s) :=
assume x y h, supr_le $ λ f, le_supr_of_le f $ m_s f f.2 h
theorem monotone_Inf_of_monotone {s : set (α → β)} (m_s : ∀f∈s, monotone f) : monotone (Inf s) :=
assume x y h, le_infi $ λ f, infi_le_of_le f $ m_s f f.2 h
end complete_lattice
namespace prod
variables (α β)
instance [has_Inf α] [has_Inf β] : has_Inf (α × β) :=
⟨λs, (Inf (prod.fst '' s), Inf (prod.snd '' s))⟩
instance [has_Sup α] [has_Sup β] : has_Sup (α × β) :=
⟨λs, (Sup (prod.fst '' s), Sup (prod.snd '' s))⟩
instance [complete_lattice α] [complete_lattice β] : complete_lattice (α × β) :=
{ le_Sup := assume s p hab, ⟨le_Sup $ mem_image_of_mem _ hab, le_Sup $ mem_image_of_mem _ hab⟩,
Sup_le := assume s p h,
⟨ Sup_le $ ball_image_of_ball $ assume p hp, (h p hp).1,
Sup_le $ ball_image_of_ball $ assume p hp, (h p hp).2⟩,
Inf_le := assume s p hab, ⟨Inf_le $ mem_image_of_mem _ hab, Inf_le $ mem_image_of_mem _ hab⟩,
le_Inf := assume s p h,
⟨ le_Inf $ ball_image_of_ball $ assume p hp, (h p hp).1,
le_Inf $ ball_image_of_ball $ assume p hp, (h p hp).2⟩,
.. prod.bounded_lattice α β,
.. prod.has_Sup α β,
.. prod.has_Inf α β }
end prod
|
ef798cdf4b334db300cefae9c59f495d6188debc | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/topology/subset_properties.lean | f50c9161ead9a8b820125133f56144ef1c0a1fe5 | [
"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 | 54,731 | 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 topology.continuous_on
/-!
# Properties of subsets of topological spaces
## Main definitions
`compact`, `is_clopen`, `is_irreducible`, `is_connected`, `is_totally_disconnected`, `is_totally_separated`
TODO: write better docs
## On the definition of irreducible and connected sets/spaces
In informal mathematics, irreducible and connected spaces are assumed to be nonempty.
We formalise the predicate without that assumption
as `is_preirreducible` and `is_preconnected` respectively.
In other words, the only difference is whether the empty space
counts as irreducible and/or connected.
There are good reasons to consider the empty space to be “too simple to be simple”
See also https://ncatlab.org/nlab/show/too+simple+to+be+simple,
and in particular
https://ncatlab.org/nlab/show/too+simple+to+be+simple#relationship_to_biased_definitions.
-/
open set filter classical
open_locale classical topological_space
universes u v
variables {α : Type u} {β : Type v} [topological_space α]
/- compact sets -/
section compact
/-- A set `s` is compact if for every filter `f` that contains `s`,
every set of `f` also meets every neighborhood of some `a ∈ s`. -/
def compact (s : set α) := ∀f, f ≠ ⊥ → f ≤ principal s → ∃a∈s, f ⊓ 𝓝 a ≠ ⊥
lemma compact.inter_right {s t : set α} (hs : compact s) (ht : is_closed t) : compact (s ∩ t) :=
assume f hnf hstf,
let ⟨a, hsa, (ha : f ⊓ 𝓝 a ≠ ⊥)⟩ := hs f hnf (le_trans hstf (le_principal_iff.2 (inter_subset_left _ _))) in
have a ∈ t,
from ht.mem_of_nhds_within_ne_bot $ ne_bot_of_le_ne_bot (by { rw inf_comm at ha, exact ha }) $
inf_le_inf (le_refl _) (le_trans hstf (le_principal_iff.2 (inter_subset_right _ _))),
⟨a, ⟨hsa, this⟩, ha⟩
lemma compact.inter_left {s t : set α} (ht : compact t) (hs : is_closed s) : compact (s ∩ t) :=
inter_comm t s ▸ ht.inter_right hs
lemma compact_diff {s t : set α} (hs : compact s) (ht : is_open t) : compact (s \ t) :=
hs.inter_right (is_closed_compl_iff.mpr ht)
lemma compact_of_is_closed_subset {s t : set α}
(hs : compact s) (ht : is_closed t) (h : t ⊆ s) : compact t :=
inter_eq_self_of_subset_right h ▸ hs.inter_right ht
lemma compact.adherence_nhdset {s t : set α} {f : filter α}
(hs : compact s) (hf₂ : f ≤ principal s) (ht₁ : is_open t) (ht₂ : ∀a∈s, 𝓝 a ⊓ f ≠ ⊥ → a ∈ t) :
t ∈ f :=
classical.by_cases mem_sets_of_eq_bot $
assume : f ⊓ principal (- t) ≠ ⊥,
let ⟨a, ha, (hfa : f ⊓ principal (-t) ⊓ 𝓝 a ≠ ⊥)⟩ := hs _ this $ inf_le_left_of_le hf₂ in
have a ∈ t,
from ht₂ a ha $ ne_bot_of_le_ne_bot hfa $ le_inf inf_le_right $ inf_le_left_of_le inf_le_left,
have (-t) ∩ t ∈ nhds_within a (-t),
from inter_mem_nhds_within _ (mem_nhds_sets ht₁ this),
have A : nhds_within a (-t) = ⊥,
from empty_in_sets_eq_bot.1 $ compl_inter_self t ▸ this,
have nhds_within a (-t) ≠ ⊥,
from ne_bot_of_le_ne_bot hfa $ le_inf inf_le_right $ inf_le_left_of_le inf_le_right,
absurd A this
lemma compact_iff_ultrafilter_le_nhds {s : set α} :
compact s ↔ (∀f, is_ultrafilter f → f ≤ principal s → ∃a∈s, f ≤ 𝓝 a) :=
⟨assume hs : compact s, assume f hf hfs,
let ⟨a, ha, h⟩ := hs _ hf.left hfs in
⟨a, ha, le_of_ultrafilter hf h⟩,
assume hs : (∀f, is_ultrafilter f → f ≤ principal s → ∃a∈s, f ≤ 𝓝 a),
assume f hf hfs,
let ⟨a, ha, (h : ultrafilter_of f ≤ 𝓝 a)⟩ :=
hs (ultrafilter_of f) (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hfs) in
have ultrafilter_of f ⊓ 𝓝 a ≠ ⊥,
by simp only [inf_of_le_left, h]; exact (ultrafilter_ultrafilter_of hf).left,
⟨a, ha, ne_bot_of_le_ne_bot this (inf_le_inf ultrafilter_of_le (le_refl _))⟩⟩
/-- For every open cover of a compact set, there exists a finite subcover. -/
lemma compact.elim_finite_subcover {s : set α} {ι : Type v} (hs : compact s)
(U : ι → set α) (hUo : ∀i, is_open (U i)) (hsU : s ⊆ ⋃ i, U i) :
∃ t : finset ι, s ⊆ ⋃ i ∈ t, U i :=
classical.by_contradiction $ assume h,
have h : ∀ t : finset ι, ¬ s ⊆ ⋃ i ∈ t, U i,
from assume t ht, h ⟨t, ht⟩,
let
f : filter α := (⨅t:finset ι, principal (s - ⋃ i ∈ t, U i)),
⟨a, ha⟩ := (@ne_empty_iff_nonempty α s).1 (assume h', h ∅ $ h'.symm ▸ empty_subset _)
in
have f ≠ ⊥, from infi_ne_bot_of_directed ⟨a⟩
(assume t₁ t₂, ⟨t₁ ∪ t₂,
principal_mono.mpr $ diff_subset_diff_right $
bUnion_subset_bUnion_left $ finset.subset_union_left _ _,
principal_mono.mpr $ diff_subset_diff_right $
bUnion_subset_bUnion_left $ finset.subset_union_right _ _⟩)
(assume t, show principal (s \ _) ≠ ⊥,
by simp only [ne.def, principal_eq_bot_iff, diff_eq_empty]; exact h _),
have f ≤ principal s, from infi_le_of_le ∅ $
show principal (s \ _) ≤ principal s, from le_principal_iff.2 (diff_subset _ _),
let
⟨a, ha, (h : f ⊓ 𝓝 a ≠ ⊥)⟩ := hs f ‹f ≠ ⊥› this,
⟨_, ⟨i, rfl⟩, (ha : a ∈ U i)⟩ := hsU ha
in
have f ≤ principal (- U i),
from infi_le_of_le {i} $ principal_mono.mpr $ show s - _ ⊆ - U i, by simp [diff_subset_iff],
have is_closed (- U i), from is_open_compl_iff.mp $ by rw compl_compl; exact hUo i,
have a ∈ - U i, from is_closed_iff_nhds.mp this _ $ ne_bot_of_le_ne_bot h $
le_inf inf_le_right (inf_le_left_of_le ‹f ≤ principal (- U i)›),
this ‹a ∈ U i›
/-- For every family of closed sets whose intersection avoids a compact set,
there exists a finite subfamily whose intersection avoids this compact set. -/
lemma compact.elim_finite_subfamily_closed {s : set α} {ι : Type v} (hs : compact s)
(Z : ι → set α) (hZc : ∀i, is_closed (Z i)) (hsZ : s ∩ (⋂ i, Z i) = ∅) :
∃ t : finset ι, s ∩ (⋂ i ∈ t, Z i) = ∅ :=
let ⟨t, ht⟩ := hs.elim_finite_subcover (λ i, - Z i) hZc
(by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, set.mem_Union,
exists_prop, set.mem_inter_eq, not_and, iff_self, set.mem_Inter, set.mem_compl_eq] using hsZ)
in
⟨t, by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, set.mem_Union,
exists_prop, set.mem_inter_eq, not_and, iff_self, set.mem_Inter, set.mem_compl_eq] using ht⟩
/-- Cantor's intersection theorem:
the intersection of a directed family of nonempty compact closed sets is nonempty. -/
lemma compact.nonempty_Inter_of_directed_nonempty_compact_closed
{ι : Type v} [hι : nonempty ι] (Z : ι → set α) (hZd : directed (⊇) Z)
(hZn : ∀ i, (Z i).nonempty) (hZc : ∀ i, compact (Z i)) (hZcl : ∀ i, is_closed (Z i)) :
(⋂ i, Z i).nonempty :=
begin
apply hι.elim,
intro i₀,
let Z' := λ i, Z i ∩ Z i₀,
suffices : (⋂ i, Z' i).nonempty,
{ exact nonempty.mono (Inter_subset_Inter $ assume i, inter_subset_left (Z i) (Z i₀)) this },
rw ← ne_empty_iff_nonempty,
intro H,
obtain ⟨t, ht⟩ : ∃ (t : finset ι), ((Z i₀) ∩ ⋂ (i ∈ t), Z' i) = ∅,
from (hZc i₀).elim_finite_subfamily_closed Z'
(assume i, is_closed_inter (hZcl i) (hZcl i₀)) (by rw [H, inter_empty]),
obtain ⟨i₁, hi₁⟩ : ∃ i₁ : ι, Z i₁ ⊆ Z i₀ ∧ ∀ i ∈ t, Z i₁ ⊆ Z' i,
{ rcases directed.finset_le hι hZd t with ⟨i, hi⟩,
rcases hZd i i₀ with ⟨i₁, hi₁, hi₁₀⟩,
use [i₁, hi₁₀],
intros j hj,
exact subset_inter (subset.trans hi₁ (hi j hj)) hi₁₀ },
suffices : ((Z i₀) ∩ ⋂ (i ∈ t), Z' i).nonempty,
{ rw ← ne_empty_iff_nonempty at this, contradiction },
refine nonempty.mono _ (hZn i₁),
exact subset_inter hi₁.left (subset_bInter hi₁.right)
end
/-- Cantor's intersection theorem for sequences indexed by `ℕ`:
the intersection of a decreasing sequence of nonempty compact closed sets is nonempty. -/
lemma compact.nonempty_Inter_of_sequence_nonempty_compact_closed
(Z : ℕ → set α) (hZd : ∀ i, Z (i+1) ⊆ Z i)
(hZn : ∀ i, (Z i).nonempty) (hZ0 : compact (Z 0)) (hZcl : ∀ i, is_closed (Z i)) :
(⋂ i, Z i).nonempty :=
have Zmono : _, from @monotone_of_monotone_nat (order_dual _) _ Z hZd,
have hZd : directed (⊇) Z, from directed_of_mono Z Zmono,
have ∀ i, Z i ⊆ Z 0, from assume i, Zmono $ zero_le i,
have hZc : ∀ i, compact (Z i), from assume i, compact_of_is_closed_subset hZ0 (hZcl i) (this i),
compact.nonempty_Inter_of_directed_nonempty_compact_closed Z hZd hZn hZc hZcl
/-- For every open cover of a compact set, there exists a finite subcover. -/
lemma compact.elim_finite_subcover_image {s : set α} {b : set β} {c : β → set α}
(hs : compact s) (hc₁ : ∀i∈b, is_open (c i)) (hc₂ : s ⊆ ⋃i∈b, c i) :
∃b'⊆b, finite b' ∧ s ⊆ ⋃i∈b', c i :=
begin
rcases hs.elim_finite_subcover (λ i, c i.1 : b → set α) _ _ with ⟨d, hd⟩,
refine ⟨↑(d.image subtype.val), _, finset.finite_to_set _, _⟩,
{ intros i hi,
erw finset.mem_image at hi,
rcases hi with ⟨s, hsd, rfl⟩,
exact s.property },
{ refine subset.trans hd _,
rintros x ⟨_, ⟨s, rfl⟩, ⟨_, ⟨hsd, rfl⟩, H⟩⟩,
refine ⟨c s.val, ⟨s.val, _⟩, H⟩,
simp [finset.mem_image_of_mem subtype.val hsd] },
{ rintro ⟨i, hi⟩, exact hc₁ i hi },
{ refine subset.trans hc₂ _,
rintros x ⟨_, ⟨i, rfl⟩, ⟨_, ⟨hib, rfl⟩, H⟩⟩,
exact ⟨_, ⟨⟨i, hib⟩, rfl⟩, H⟩ },
end
/-- A set `s` is compact if for every family of closed sets whose intersection avoids `s`,
there exists a finite subfamily whose intersection avoids `s`. -/
theorem compact_of_finite_subfamily_closed {s : set α}
(h : Π {ι : Type u} (Z : ι → (set α)), (∀ i, is_closed (Z i)) →
s ∩ (⋂ i, Z i) = ∅ → (∃ (t : finset ι), s ∩ (⋂ i ∈ t, Z i) = ∅)) :
compact s :=
assume f hfn hfs, classical.by_contradiction $ assume : ¬ (∃x∈s, f ⊓ 𝓝 x ≠ ⊥),
have hf : ∀x∈s, 𝓝 x ⊓ f = ⊥,
by simpa only [not_exists, not_not, inf_comm],
have ¬ ∃x∈s, ∀t∈f.sets, x ∈ closure t,
from assume ⟨x, hxs, hx⟩,
have ∅ ∈ 𝓝 x ⊓ f, by rw [empty_in_sets_eq_bot, hf x hxs],
let ⟨t₁, ht₁, t₂, ht₂, ht⟩ := by rw [mem_inf_sets] at this; exact this in
have ∅ ∈ 𝓝 x ⊓ principal t₂,
from (𝓝 x ⊓ principal t₂).sets_of_superset (inter_mem_inf_sets ht₁ (subset.refl t₂)) ht,
have 𝓝 x ⊓ principal t₂ = ⊥,
by rwa [empty_in_sets_eq_bot] at this,
by simp only [closure_eq_nhds] at hx; exact hx t₂ ht₂ this,
let ⟨t, ht⟩ := h (λ i : f.sets, closure i.1) (λ i, is_closed_closure)
(by simpa [eq_empty_iff_forall_not_mem, not_exists]) in
have (⋂i∈t, subtype.val i) ∈ f,
from Inter_mem_sets t.finite_to_set $ assume i hi, i.2,
have s ∩ (⋂i∈t, subtype.val i) ∈ f,
from inter_mem_sets (le_principal_iff.1 hfs) this,
have ∅ ∈ f,
from mem_sets_of_superset this $ assume x ⟨hxs, hx⟩,
let ⟨i, hit, hxi⟩ := (show ∃i ∈ t, x ∉ closure (subtype.val i),
by { rw [eq_empty_iff_forall_not_mem] at ht, simpa [hxs, not_forall] using ht x }) in
have x ∈ closure i.val, from subset_closure (mem_bInter_iff.mp hx i hit),
show false, from hxi this,
hfn $ by rwa [empty_in_sets_eq_bot] at this
/-- A set `s` is compact if for every open cover of `s`, there exists a finite subcover. -/
lemma compact_of_finite_subcover {s : set α}
(h : Π {ι : Type u} (U : ι → (set α)), (∀ i, is_open (U i)) →
s ⊆ (⋃ i, U i) → (∃ (t : finset ι), s ⊆ (⋃ i ∈ t, U i))) :
compact s :=
compact_of_finite_subfamily_closed $
assume ι Z hZc hsZ,
let ⟨t, ht⟩ := h (λ i, - Z i) (assume i, is_open_compl_iff.mpr $ hZc i)
(by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, set.mem_Union,
exists_prop, set.mem_inter_eq, not_and, iff_self, set.mem_Inter, set.mem_compl_eq] using hsZ)
in
⟨t, by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, set.mem_Union,
exists_prop, set.mem_inter_eq, not_and, iff_self, set.mem_Inter, set.mem_compl_eq] using ht⟩
/-- A set `s` is compact if and only if
for every open cover of `s`, there exists a finite subcover. -/
lemma compact_iff_finite_subcover {s : set α} :
compact s ↔ (Π {ι : Type u} (U : ι → (set α)), (∀ i, is_open (U i)) →
s ⊆ (⋃ i, U i) → (∃ (t : finset ι), s ⊆ (⋃ i ∈ t, U i))) :=
⟨assume hs ι, hs.elim_finite_subcover, compact_of_finite_subcover⟩
/-- A set `s` is compact if and only if
for every family of closed sets whose intersection avoids `s`,
there exists a finite subfamily whose intersection avoids `s`. -/
theorem compact_iff_finite_subfamily_closed {s : set α} :
compact s ↔ (Π {ι : Type u} (Z : ι → (set α)), (∀ i, is_closed (Z i)) →
s ∩ (⋂ i, Z i) = ∅ → (∃ (t : finset ι), s ∩ (⋂ i ∈ t, Z i) = ∅)) :=
⟨assume hs ι, hs.elim_finite_subfamily_closed, compact_of_finite_subfamily_closed⟩
@[simp]
lemma compact_empty : compact (∅ : set α) :=
assume f hnf hsf, not.elim hnf $
empty_in_sets_eq_bot.1 $ le_principal_iff.1 hsf
@[simp]
lemma compact_singleton {a : α} : compact ({a} : set α) :=
compact_of_finite_subcover $ assume ι U hUo hsU,
let ⟨i, hai⟩ := (show ∃i : ι, a ∈ U i, from mem_Union.1 $ singleton_subset_iff.1 hsU) in
⟨{i}, singleton_subset_iff.2 (by simpa only [finset.bUnion_singleton])⟩
lemma set.finite.compact_bUnion {s : set β} {f : β → set α} (hs : finite s)
(hf : ∀i ∈ s, compact (f i)) :
compact (⋃i ∈ s, f i) :=
compact_of_finite_subcover $ assume ι U hUo hsU,
have ∀i : subtype s, ∃t : finset ι, f i ⊆ (⋃ j ∈ t, U j), from
assume ⟨i, hi⟩, (hf i hi).elim_finite_subcover _ hUo
(calc f i ⊆ ⋃i ∈ s, f i : subset_bUnion_of_mem hi
... ⊆ ⋃j, U j : hsU),
let ⟨finite_subcovers, h⟩ := axiom_of_choice this in
by haveI : fintype (subtype s) := hs.fintype; exact
let t := finset.bind finset.univ finite_subcovers in
have (⋃i ∈ s, f i) ⊆ (⋃ i ∈ t, U i), from bUnion_subset $
assume i hi, calc
f i ⊆ (⋃ j ∈ finite_subcovers ⟨i, hi⟩, U j) : (h ⟨i, hi⟩)
... ⊆ (⋃ j ∈ t, U j) : bUnion_subset_bUnion_left $
assume j hj, finset.mem_bind.mpr ⟨_, finset.mem_univ _, hj⟩,
⟨t, this⟩
lemma compact_Union {f : β → set α} [fintype β]
(h : ∀i, compact (f i)) : compact (⋃i, f i) :=
by rw ← bUnion_univ; exact finite_univ.compact_bUnion (λ i _, h i)
lemma set.finite.compact {s : set α} (hs : finite s) : compact s :=
bUnion_of_singleton s ▸ hs.compact_bUnion (λ _ _, compact_singleton)
lemma compact.union {s t : set α} (hs : compact s) (ht : compact t) : compact (s ∪ t) :=
by rw union_eq_Union; exact compact_Union (λ b, by cases b; assumption)
section tube_lemma
variables [topological_space β]
/-- `nhds_contain_boxes s t` means that any open neighborhood of `s × t` in `α × β` includes
a product of an open neighborhood of `s` by an open neighborhood of `t`. -/
def nhds_contain_boxes (s : set α) (t : set β) : Prop :=
∀ (n : set (α × β)) (hn : is_open n) (hp : set.prod s t ⊆ n),
∃ (u : set α) (v : set β), is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ set.prod u v ⊆ n
lemma nhds_contain_boxes.symm {s : set α} {t : set β} :
nhds_contain_boxes s t → nhds_contain_boxes t s :=
assume H n hn hp,
let ⟨u, v, uo, vo, su, tv, p⟩ :=
H (prod.swap ⁻¹' n)
(continuous_swap n hn)
(by rwa [←image_subset_iff, prod.swap, image_swap_prod]) in
⟨v, u, vo, uo, tv, su,
by rwa [←image_subset_iff, prod.swap, image_swap_prod] at p⟩
lemma nhds_contain_boxes.comm {s : set α} {t : set β} :
nhds_contain_boxes s t ↔ nhds_contain_boxes t s :=
iff.intro nhds_contain_boxes.symm nhds_contain_boxes.symm
lemma nhds_contain_boxes_of_singleton {x : α} {y : β} :
nhds_contain_boxes ({x} : set α) ({y} : set β) :=
assume n hn hp,
let ⟨u, v, uo, vo, xu, yv, hp'⟩ :=
is_open_prod_iff.mp hn x y (hp $ by simp) in
⟨u, v, uo, vo, by simpa, by simpa, hp'⟩
lemma nhds_contain_boxes_of_compact {s : set α} (hs : compact s) (t : set β)
(H : ∀ x ∈ s, nhds_contain_boxes ({x} : set α) t) : nhds_contain_boxes s t :=
assume n hn hp,
have ∀x : subtype s, ∃uv : set α × set β,
is_open uv.1 ∧ is_open uv.2 ∧ {↑x} ⊆ uv.1 ∧ t ⊆ uv.2 ∧ set.prod uv.1 uv.2 ⊆ n,
from assume ⟨x, hx⟩,
have set.prod {x} t ⊆ n, from
subset.trans (prod_mono (by simpa) (subset.refl _)) hp,
let ⟨ux,vx,H1⟩ := H x hx n hn this in ⟨⟨ux,vx⟩,H1⟩,
let ⟨uvs, h⟩ := classical.axiom_of_choice this in
have us_cover : s ⊆ ⋃i, (uvs i).1, from
assume x hx, set.subset_Union _ ⟨x,hx⟩ (by simpa using (h ⟨x,hx⟩).2.2.1),
let ⟨s0, s0_cover⟩ :=
hs.elim_finite_subcover _ (λi, (h i).1) us_cover in
let u := ⋃(i ∈ s0), (uvs i).1 in
let v := ⋂(i ∈ s0), (uvs i).2 in
have is_open u, from is_open_bUnion (λi _, (h i).1),
have is_open v, from is_open_bInter s0.finite_to_set (λi _, (h i).2.1),
have t ⊆ v, from subset_bInter (λi _, (h i).2.2.2.1),
have set.prod u v ⊆ n, from assume ⟨x',y'⟩ ⟨hx',hy'⟩,
have ∃i ∈ s0, x' ∈ (uvs i).1, by simpa using hx',
let ⟨i,is0,hi⟩ := this in
(h i).2.2.2.2 ⟨hi, (bInter_subset_of_mem is0 : v ⊆ (uvs i).2) hy'⟩,
⟨u, v, ‹is_open u›, ‹is_open v›, s0_cover, ‹t ⊆ v›, ‹set.prod u v ⊆ n›⟩
lemma generalized_tube_lemma {s : set α} (hs : compact s) {t : set β} (ht : compact t)
{n : set (α × β)} (hn : is_open n) (hp : set.prod s t ⊆ n) :
∃ (u : set α) (v : set β), is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ set.prod u v ⊆ n :=
have _, from
nhds_contain_boxes_of_compact hs t $ assume x _, nhds_contain_boxes.symm $
nhds_contain_boxes_of_compact ht {x} $ assume y _, nhds_contain_boxes_of_singleton,
this n hn hp
end tube_lemma
/-- Type class for compact spaces. Separation is sometimes included in the definition, especially
in the French literature, but we do not include it here. -/
class compact_space (α : Type*) [topological_space α] : Prop :=
(compact_univ : compact (univ : set α))
lemma compact_univ [h : compact_space α] : compact (univ : set α) := h.compact_univ
lemma cluster_point_of_compact [compact_space α]
{f : filter α} (h : f ≠ ⊥) : ∃ x, f ⊓ 𝓝 x ≠ ⊥ :=
by simpa using compact_univ f h (by simpa using f.univ_sets)
theorem compact_space_of_finite_subfamily_closed {α : Type u} [topological_space α]
(h : Π {ι : Type u} (Z : ι → (set α)), (∀ i, is_closed (Z i)) →
(⋂ i, Z i) = ∅ → (∃ (t : finset ι), (⋂ i ∈ t, Z i) = ∅)) :
compact_space α :=
{ compact_univ :=
begin
apply compact_of_finite_subfamily_closed,
intros ι Z, specialize h Z,
simpa using h
end }
lemma is_closed.compact [compact_space α] {s : set α} (h : is_closed s) :
compact s :=
compact_of_is_closed_subset compact_univ h (subset_univ _)
variables [topological_space β]
lemma compact.image_of_continuous_on {s : set α} {f : α → β} (hs : compact s)
(hf : continuous_on f s) : compact (f '' s) :=
begin
intros l lne ls,
have ne_bot : l.comap f ⊓ principal s ≠ ⊥,
from comap_inf_principal_ne_bot_of_image_mem lne (le_principal_iff.1 ls),
rcases hs (l.comap f ⊓ principal s) ne_bot inf_le_right with ⟨a, has, ha⟩,
use [f a, mem_image_of_mem f has],
rw [inf_assoc, @inf_comm _ _ _ (𝓝 a)] at ha,
exact ne_bot_of_le_ne_bot (@@map_ne_bot f ha) (tendsto_comap.inf $ hf a has)
end
lemma compact.image {s : set α} {f : α → β} (hs : compact s) (hf : continuous f) :
compact (f '' s) :=
hs.image_of_continuous_on hf.continuous_on
lemma compact_range [compact_space α] {f : α → β} (hf : continuous f) :
compact (range f) :=
by rw ← image_univ; exact compact_univ.image hf
local notation `𝓟` := principal
/-- If X is compact then pr₂ : X × Y → Y is a closed map -/
theorem is_closed_proj_of_compact
{X : Type*} [topological_space X] [compact_space X]
{Y : Type*} [topological_space Y] :
is_closed_map (prod.snd : X × Y → Y) :=
begin
set πX := (prod.fst : X × Y → X),
set πY := (prod.snd : X × Y → Y),
assume C (hC : is_closed C),
rw is_closed_iff_nhds at hC ⊢,
assume y (y_closure : 𝓝 y ⊓ 𝓟 (πY '' C) ≠ ⊥),
have : map πX (comap πY (𝓝 y) ⊓ 𝓟 C) ≠ ⊥,
{ suffices : map πY (comap πY (𝓝 y) ⊓ 𝓟 C) ≠ ⊥,
from map_ne_bot (λ h, this $ by rw h ; exact map_bot ),
calc map πY (comap πY (𝓝 y) ⊓ 𝓟 C) =
𝓝 y ⊓ map πY (𝓟 C) : filter.push_pull' _ _ _
... = 𝓝 y ⊓ 𝓟 (πY '' C) : by rw map_principal
... ≠ ⊥ : y_closure },
obtain ⟨x, hx⟩ : ∃ x, map πX (comap πY (𝓝 y) ⊓ 𝓟 C) ⊓ 𝓝 x ≠ ⊥,
from cluster_point_of_compact this,
refine ⟨⟨x, y⟩, _, by simp [πY]⟩,
apply hC,
rw ← filter.map_ne_bot_iff πX,
calc map πX (𝓝 (x, y) ⊓ 𝓟 C)
= map πX (comap πX (𝓝 x) ⊓ comap πY (𝓝 y) ⊓ 𝓟 C) : by rw [nhds_prod_eq, filter.prod]
... = map πX (comap πY (𝓝 y) ⊓ 𝓟 C ⊓ comap πX (𝓝 x)) : by ac_refl
... = map πX (comap πY (𝓝 y) ⊓ 𝓟 C) ⊓ 𝓝 x : by rw filter.push_pull
... ≠ ⊥ : hx,
end
lemma embedding.compact_iff_compact_image {s : set α} {f : α → β} (hf : embedding f) :
compact s ↔ compact (f '' s) :=
iff.intro (assume h, h.image hf.continuous) $ assume h, begin
rw compact_iff_ultrafilter_le_nhds at ⊢ h,
intros u hu us',
let u' : filter β := map f u,
have : u' ≤ principal (f '' s), begin
rw [map_le_iff_le_comap, comap_principal], convert us',
exact preimage_image_eq _ hf.inj
end,
rcases h u' (ultrafilter_map hu) this with ⟨_, ⟨a, ha, ⟨⟩⟩, _⟩,
refine ⟨a, ha, _⟩,
rwa [hf.induced, nhds_induced, ←map_le_iff_le_comap]
end
lemma compact_iff_compact_in_subtype {p : α → Prop} {s : set {a // p a}} :
compact s ↔ compact (subtype.val '' s) :=
embedding_subtype_val.compact_iff_compact_image
lemma compact_iff_compact_univ {s : set α} : compact s ↔ compact (univ : set (subtype s)) :=
by rw [compact_iff_compact_in_subtype, image_univ, subtype.val_range]; refl
lemma compact_iff_compact_space {s : set α} : compact s ↔ compact_space s :=
compact_iff_compact_univ.trans ⟨λ h, ⟨h⟩, @compact_space.compact_univ _ _⟩
lemma compact.prod {s : set α} {t : set β} (hs : compact s) (ht : compact t) : compact (set.prod s t) :=
begin
rw compact_iff_ultrafilter_le_nhds at hs ht ⊢,
intros f hf hfs,
rw le_principal_iff at hfs,
rcases hs (map prod.fst f) (ultrafilter_map hf)
(le_principal_iff.2 (mem_map_sets_iff.2
⟨_, hfs, image_subset_iff.2 (λ s h, h.1)⟩)) with ⟨a, sa, ha⟩,
rcases ht (map prod.snd f) (ultrafilter_map hf)
(le_principal_iff.2 (mem_map_sets_iff.2
⟨_, hfs, image_subset_iff.2 (λ s h, h.2)⟩)) with ⟨b, tb, hb⟩,
rw map_le_iff_le_comap at ha hb,
refine ⟨⟨a, b⟩, ⟨sa, tb⟩, _⟩,
rw nhds_prod_eq, exact le_inf ha hb
end
/-- Finite topological spaces are compact. -/
@[priority 100] instance fintype.compact_space [fintype α] : compact_space α :=
{ compact_univ := set.finite_univ.compact }
/-- The product of two compact spaces is compact. -/
instance [compact_space α] [compact_space β] : compact_space (α × β) :=
⟨by { rw ← univ_prod_univ, exact compact_univ.prod compact_univ }⟩
/-- The disjoint union of two compact spaces is compact. -/
instance [compact_space α] [compact_space β] : compact_space (α ⊕ β) :=
⟨begin
rw ← range_inl_union_range_inr,
exact (compact_range continuous_inl).union (compact_range continuous_inr)
end⟩
section tychonoff
variables {ι : Type*} {π : ι → Type*} [∀i, topological_space (π i)]
/-- Tychonoff's theorem -/
lemma compact_pi_infinite {s : Πi:ι, set (π i)} :
(∀i, compact (s i)) → compact {x : Πi:ι, π i | ∀i, x i ∈ s i} :=
begin
simp [compact_iff_ultrafilter_le_nhds, nhds_pi],
exact assume h f hf hfs,
let p : Πi:ι, filter (π i) := λi, map (λx:Πi:ι, π i, x i) f in
have ∀i:ι, ∃a, a∈s i ∧ p i ≤ 𝓝 a,
from assume i, h i (p i) (ultrafilter_map hf) $
show (λx:Πi:ι, π i, x i) ⁻¹' s i ∈ f.sets,
from mem_sets_of_superset hfs $ assume x (hx : ∀i, x i ∈ s i), hx i,
let ⟨a, ha⟩ := classical.axiom_of_choice this in
⟨a, assume i, (ha i).left, assume i, map_le_iff_le_comap.mp $ (ha i).right⟩
end
instance pi.compact [∀i:ι, compact_space (π i)] : compact_space (Πi, π i) :=
⟨begin
have A : compact {x : Πi:ι, π i | ∀i, x i ∈ (univ : set (π i))} :=
compact_pi_infinite (λi, compact_univ),
have : {x : Πi:ι, π i | ∀i, x i ∈ (univ : set (π i))} = univ := by ext; simp,
rwa this at A,
end⟩
end tychonoff
instance quot.compact_space {r : α → α → Prop} [compact_space α] :
compact_space (quot r) :=
⟨by { rw ← range_quot_mk, exact compact_range continuous_quot_mk }⟩
instance quotient.compact_space {s : setoid α} [compact_space α] :
compact_space (quotient s) :=
quot.compact_space
/-- There are various definitions of "locally compact space" in the literature, which agree for
Hausdorff spaces but not in general. This one is the precise condition on X needed for the
evaluation `map C(X, Y) × X → Y` to be continuous for all `Y` when `C(X, Y)` is given the
compact-open topology. -/
class locally_compact_space (α : Type*) [topological_space α] : Prop :=
(local_compact_nhds : ∀ (x : α) (n ∈ 𝓝 x), ∃ s ∈ 𝓝 x, s ⊆ n ∧ compact s)
end compact
section clopen
/-- A set is clopen if it is both open and closed. -/
def is_clopen (s : set α) : Prop :=
is_open s ∧ is_closed s
theorem is_clopen_union {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∪ t) :=
⟨is_open_union hs.1 ht.1, is_closed_union hs.2 ht.2⟩
theorem is_clopen_inter {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∩ t) :=
⟨is_open_inter hs.1 ht.1, is_closed_inter hs.2 ht.2⟩
@[simp] theorem is_clopen_empty : is_clopen (∅ : set α) :=
⟨is_open_empty, is_closed_empty⟩
@[simp] theorem is_clopen_univ : is_clopen (univ : set α) :=
⟨is_open_univ, is_closed_univ⟩
theorem is_clopen_compl {s : set α} (hs : is_clopen s) : is_clopen (-s) :=
⟨hs.2, is_closed_compl_iff.2 hs.1⟩
@[simp] theorem is_clopen_compl_iff {s : set α} : is_clopen (-s) ↔ is_clopen s :=
⟨λ h, compl_compl s ▸ is_clopen_compl h, is_clopen_compl⟩
theorem is_clopen_diff {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s-t) :=
is_clopen_inter hs (is_clopen_compl ht)
end clopen
section preirreducible
/-- A preirreducible set `s` is one where there is no non-trivial pair of disjoint opens on `s`. -/
def is_preirreducible (s : set α) : Prop :=
∀ (u v : set α), is_open u → is_open v →
(s ∩ u).nonempty → (s ∩ v).nonempty → (s ∩ (u ∩ v)).nonempty
/-- An irreducible set `s` is one that is nonempty and
where there is no non-trivial pair of disjoint opens on `s`. -/
def is_irreducible (s : set α) : Prop :=
s.nonempty ∧ is_preirreducible s
lemma is_irreducible.nonempty {s : set α} (h : is_irreducible s) :
s.nonempty := h.1
lemma is_irreducible.is_preirreducible {s : set α} (h : is_irreducible s) :
is_preirreducible s := h.2
theorem is_preirreducible_empty : is_preirreducible (∅ : set α) :=
λ _ _ _ _ _ ⟨x, h1, h2⟩, h1.elim
theorem is_irreducible_singleton {x} : is_irreducible ({x} : set α) :=
⟨singleton_nonempty x,
λ u v _ _ ⟨y, h1, h2⟩ ⟨z, h3, h4⟩, by rw mem_singleton_iff at h1 h3;
substs y z; exact ⟨x, or.inl rfl, h2, h4⟩⟩
theorem is_preirreducible.closure {s : set α} (H : is_preirreducible s) :
is_preirreducible (closure s) :=
λ u v hu hv ⟨y, hycs, hyu⟩ ⟨z, hzcs, hzv⟩,
let ⟨p, hpu, hps⟩ := mem_closure_iff.1 hycs u hu hyu in
let ⟨q, hqv, hqs⟩ := mem_closure_iff.1 hzcs v hv hzv in
let ⟨r, hrs, hruv⟩ := H u v hu hv ⟨p, hps, hpu⟩ ⟨q, hqs, hqv⟩ in
⟨r, subset_closure hrs, hruv⟩
lemma is_irreducible.closure {s : set α} (h : is_irreducible s) :
is_irreducible (closure s) :=
⟨h.nonempty.closure, h.is_preirreducible.closure⟩
theorem exists_preirreducible (s : set α) (H : is_preirreducible s) :
∃ t : set α, is_preirreducible t ∧ s ⊆ t ∧ ∀ u, is_preirreducible u → t ⊆ u → u = t :=
let ⟨m, hm, hsm, hmm⟩ := zorn.zorn_subset₀ {t : set α | is_preirreducible t}
(λ c hc hcc hcn, let ⟨t, htc⟩ := hcn in
⟨⋃₀ c, λ u v hu hv ⟨y, hy, hyu⟩ ⟨z, hz, hzv⟩,
let ⟨p, hpc, hyp⟩ := mem_sUnion.1 hy,
⟨q, hqc, hzq⟩ := mem_sUnion.1 hz in
or.cases_on (zorn.chain.total hcc hpc hqc)
(assume hpq : p ⊆ q, let ⟨x, hxp, hxuv⟩ := hc hqc u v hu hv
⟨y, hpq hyp, hyu⟩ ⟨z, hzq, hzv⟩ in
⟨x, mem_sUnion_of_mem hxp hqc, hxuv⟩)
(assume hqp : q ⊆ p, let ⟨x, hxp, hxuv⟩ := hc hpc u v hu hv
⟨y, hyp, hyu⟩ ⟨z, hqp hzq, hzv⟩ in
⟨x, mem_sUnion_of_mem hxp hpc, hxuv⟩),
λ x hxc, set.subset_sUnion_of_mem hxc⟩) s H in
⟨m, hm, hsm, λ u hu hmu, hmm _ hu hmu⟩
/-- A maximal irreducible set that contains a given point. -/
def irreducible_component (x : α) : set α :=
classical.some (exists_preirreducible {x} is_irreducible_singleton.is_preirreducible)
lemma irreducible_component_property (x : α) :
is_preirreducible (irreducible_component x) ∧ {x} ⊆ (irreducible_component x) ∧
∀ u, is_preirreducible u → (irreducible_component x) ⊆ u → u = (irreducible_component x) :=
classical.some_spec (exists_preirreducible {x} is_irreducible_singleton.is_preirreducible)
theorem mem_irreducible_component {x : α} : x ∈ irreducible_component x :=
singleton_subset_iff.1 (irreducible_component_property x).2.1
theorem is_irreducible_irreducible_component {x : α} : is_irreducible (irreducible_component x) :=
⟨⟨x, mem_irreducible_component⟩, (irreducible_component_property x).1⟩
theorem eq_irreducible_component {x : α} :
∀ {s : set α}, is_preirreducible s → irreducible_component x ⊆ s → s = irreducible_component x :=
(irreducible_component_property x).2.2
theorem is_closed_irreducible_component {x : α} :
is_closed (irreducible_component x) :=
closure_eq_iff_is_closed.1 $ eq_irreducible_component
is_irreducible_irreducible_component.is_preirreducible.closure
subset_closure
/-- A preirreducible space is one where there is no non-trivial pair of disjoint opens. -/
class preirreducible_space (α : Type u) [topological_space α] : Prop :=
(is_preirreducible_univ [] : is_preirreducible (univ : set α))
section prio
set_option default_priority 100 -- see Note [default priority]
/-- An irreducible space is one that is nonempty
and where there is no non-trivial pair of disjoint opens. -/
class irreducible_space (α : Type u) [topological_space α] extends preirreducible_space α : Prop :=
(to_nonempty [] : nonempty α)
end prio
attribute [instance, priority 50] irreducible_space.to_nonempty -- see Note [lower instance priority]
theorem nonempty_preirreducible_inter [preirreducible_space α] {s t : set α} :
is_open s → is_open t → s.nonempty → t.nonempty → (s ∩ t).nonempty :=
by simpa only [univ_inter, univ_subset_iff] using
@preirreducible_space.is_preirreducible_univ α _ _ s t
theorem is_preirreducible.image [topological_space β] {s : set α} (H : is_preirreducible s)
(f : α → β) (hf : continuous_on f s) : is_preirreducible (f '' s) :=
begin
rintros u v hu hv ⟨_, ⟨⟨x, hx, rfl⟩, hxu⟩⟩ ⟨_, ⟨⟨y, hy, rfl⟩, hyv⟩⟩,
rw ← set.mem_preimage at hxu hyv,
rcases continuous_on_iff'.1 hf u hu with ⟨u', hu', u'_eq⟩,
rcases continuous_on_iff'.1 hf v hv with ⟨v', hv', v'_eq⟩,
have := H u' v' hu' hv',
rw [set.inter_comm s u', ← u'_eq] at this,
rw [set.inter_comm s v', ← v'_eq] at this,
rcases this ⟨x, hxu, hx⟩ ⟨y, hyv, hy⟩ with ⟨z, hzs, hzu', hzv'⟩,
refine ⟨f z, mem_image_of_mem f hzs, _, _⟩,
all_goals
{ rw ← set.mem_preimage,
apply set.mem_of_mem_inter_left,
show z ∈ _ ∩ s,
simp [*] }
end
theorem is_irreducible.image [topological_space β] {s : set α} (H : is_irreducible s)
(f : α → β) (hf : continuous_on f s) : is_irreducible (f '' s) :=
⟨nonempty_image_iff.mpr H.nonempty, H.is_preirreducible.image f hf⟩
lemma subtype.preirreducible_space {s : set α} (h : is_preirreducible s) :
preirreducible_space s :=
{ is_preirreducible_univ :=
begin
intros u v hu hv hsu hsv,
rw is_open_induced_iff at hu hv,
rcases hu with ⟨u, hu, rfl⟩,
rcases hv with ⟨v, hv, rfl⟩,
rcases hsu with ⟨⟨x, hxs⟩, hxs', hxu⟩,
rcases hsv with ⟨⟨y, hys⟩, hys', hyv⟩,
rcases h u v hu hv ⟨x, hxs, hxu⟩ ⟨y, hys, hyv⟩ with ⟨z, hzs, ⟨hzu, hzv⟩⟩,
exact ⟨⟨z, hzs⟩, ⟨set.mem_univ _, ⟨hzu, hzv⟩⟩⟩
end }
lemma subtype.irreducible_space {s : set α} (h : is_irreducible s) :
irreducible_space s :=
{ is_preirreducible_univ :=
(subtype.preirreducible_space h.is_preirreducible).is_preirreducible_univ,
to_nonempty := h.nonempty.to_subtype }
/-- A set `s` is irreducible if and only if
for every finite collection of open sets all of whose members intersect `s`,
`s` also intersects the intersection of the entire collection
(i.e., there is an element of `s` contained in every member of the collection). -/
lemma is_irreducible_iff_sInter {s : set α} :
is_irreducible s ↔
∀ (U : finset (set α)) (hU : ∀ u ∈ U, is_open u) (H : ∀ u ∈ U, (s ∩ u).nonempty),
(s ∩ ⋂₀ ↑U).nonempty :=
begin
split; intro h,
{ intro U, apply finset.induction_on U,
{ intros, simpa using h.nonempty },
{ intros u U hu IH hU H,
rw [finset.coe_insert, sInter_insert],
apply h.2,
{ solve_by_elim [finset.mem_insert_self] },
{ apply is_open_sInter (finset.finite_to_set U),
intros, solve_by_elim [finset.mem_insert_of_mem] },
{ solve_by_elim [finset.mem_insert_self] },
{ apply IH,
all_goals { intros, solve_by_elim [finset.mem_insert_of_mem] } } } },
{ split,
{ simpa using h ∅ _ _; intro u; simp },
intros u v hu hv hu' hv',
simpa using h {v,u} _ _,
all_goals
{ intro t,
rw [finset.insert_empty_eq_singleton,
finset.mem_insert, finset.mem_singleton],
rintro (rfl|rfl); assumption } }
end
/-- A set is preirreducible if and only if
for every cover by two closed sets, it is contained in one of the two covering sets. -/
lemma is_preirreducible_iff_closed_union_closed {s : set α} :
is_preirreducible s ↔
∀ (z₁ z₂ : set α), is_closed z₁ → is_closed z₂ → s ⊆ z₁ ∪ z₂ → s ⊆ z₁ ∨ s ⊆ z₂ :=
begin
split,
all_goals
{ intros h t₁ t₂ ht₁ ht₂,
specialize h (-t₁) (-t₂),
simp only [is_open_compl_iff, is_closed_compl_iff] at h,
specialize h ht₁ ht₂ },
{ contrapose!, simp only [not_subset],
rintro ⟨⟨x, hx, hx'⟩, ⟨y, hy, hy'⟩⟩,
rcases h ⟨x, hx, hx'⟩ ⟨y, hy, hy'⟩ with ⟨z, hz, hz'⟩,
rw ← compl_union at hz',
exact ⟨z, hz, hz'⟩ },
{ rintro ⟨x, hx, hx'⟩ ⟨y, hy, hy'⟩,
rw ← compl_inter at h,
delta set.nonempty,
rw imp_iff_not_or at h,
contrapose! h,
split,
{ intros z hz hz', exact h z ⟨hz, hz'⟩ },
{ split; intro H; refine H _ ‹_›; assumption } }
end
/-- A set is irreducible if and only if
for every cover by a finite collection of closed sets,
it is contained in one of the members of the collection. -/
lemma is_irreducible_iff_sUnion_closed {s : set α} :
is_irreducible s ↔
∀ (Z : finset (set α)) (hZ : ∀ z ∈ Z, is_closed z) (H : s ⊆ ⋃₀ ↑Z),
∃ z ∈ Z, s ⊆ z :=
begin
rw [is_irreducible, is_preirreducible_iff_closed_union_closed],
split; intro h,
{ intro Z, apply finset.induction_on Z,
{ intros, rw [finset.coe_empty, sUnion_empty] at H,
rcases h.1 with ⟨x, hx⟩,
exfalso, tauto },
{ intros z Z hz IH hZ H,
cases h.2 z (⋃₀ ↑Z) _ _ _
with h' h',
{ exact ⟨z, finset.mem_insert_self _ _, h'⟩ },
{ rcases IH _ h' with ⟨z', hz', hsz'⟩,
{ exact ⟨z', finset.mem_insert_of_mem hz', hsz'⟩ },
{ intros, solve_by_elim [finset.mem_insert_of_mem] } },
{ solve_by_elim [finset.mem_insert_self] },
{ rw sUnion_eq_bUnion,
apply is_closed_bUnion (finset.finite_to_set Z),
{ intros, solve_by_elim [finset.mem_insert_of_mem] } },
{ simpa using H } } },
{ split,
{ by_contradiction hs,
simpa using h ∅ _ _,
{ intro z, simp },
{ simpa [set.nonempty] using hs } },
intros z₁ z₂ hz₁ hz₂ H,
have := h {z₂, z₁} _ _,
simp only [exists_prop, finset.insert_empty_eq_singleton,
finset.mem_insert, finset.mem_singleton] at this,
{ rcases this with ⟨z, rfl|rfl, hz⟩; tauto },
{ intro t,
rw [finset.insert_empty_eq_singleton,
finset.mem_insert, finset.mem_singleton],
rintro (rfl|rfl); assumption },
{ simpa using H } }
end
end preirreducible
section preconnected
/-- A preconnected set is one where there is no non-trivial open partition. -/
def is_preconnected (s : set α) : Prop :=
∀ (u v : set α), is_open u → is_open v → s ⊆ u ∪ v →
(s ∩ u).nonempty → (s ∩ v).nonempty → (s ∩ (u ∩ v)).nonempty
/-- A connected set is one that is nonempty and where there is no non-trivial open partition. -/
def is_connected (s : set α) : Prop :=
s.nonempty ∧ is_preconnected s
lemma is_connected.nonempty {s : set α} (h : is_connected s) :
s.nonempty := h.1
lemma is_connected.is_preconnected {s : set α} (h : is_connected s) :
is_preconnected s := h.2
theorem is_preirreducible.is_preconnected {s : set α} (H : is_preirreducible s) : is_preconnected s :=
λ _ _ hu hv _, H _ _ hu hv
theorem is_irreducible.is_connected {s : set α} (H : is_irreducible s) : is_connected s :=
⟨H.nonempty, H.is_preirreducible.is_preconnected⟩
theorem is_preconnected_empty : is_preconnected (∅ : set α) :=
is_preirreducible_empty.is_preconnected
theorem is_connected_singleton {x} : is_connected ({x} : set α) :=
is_irreducible_singleton.is_connected
/-- If any point of a set is joined to a fixed point by a preconnected subset,
then the original set is preconnected as well. -/
theorem is_preconnected_of_forall {s : set α} (x : α)
(H : ∀ y ∈ s, ∃ t ⊆ s, x ∈ t ∧ y ∈ t ∧ is_preconnected t) :
is_preconnected s :=
begin
rintros u v hu hv hs ⟨z, zs, zu⟩ ⟨y, ys, yv⟩,
have xs : x ∈ s, by { rcases H y ys with ⟨t, ts, xt, yt, ht⟩, exact ts xt },
wlog xu : x ∈ u := hs xs using [u v y z, v u z y],
rcases H y ys with ⟨t, ts, xt, yt, ht⟩,
have := ht u v hu hv(subset.trans ts hs) ⟨x, xt, xu⟩ ⟨y, yt, yv⟩,
exact this.imp (λ z hz, ⟨ts hz.1, hz.2⟩)
end
/-- If any two points of a set are contained in a preconnected subset,
then the original set is preconnected as well. -/
theorem is_preconnected_of_forall_pair {s : set α}
(H : ∀ x y ∈ s, ∃ t ⊆ s, x ∈ t ∧ y ∈ t ∧ is_preconnected t) :
is_preconnected s :=
begin
rintros u v hu hv hs ⟨x, xs, xu⟩ ⟨y, ys, yv⟩,
rcases H x y xs ys with ⟨t, ts, xt, yt, ht⟩,
have := ht u v hu hv(subset.trans ts hs) ⟨x, xt, xu⟩ ⟨y, yt, yv⟩,
exact this.imp (λ z hz, ⟨ts hz.1, hz.2⟩)
end
/-- A union of a family of preconnected sets with a common point is preconnected as well. -/
theorem is_preconnected_sUnion (x : α) (c : set (set α)) (H1 : ∀ s ∈ c, x ∈ s)
(H2 : ∀ s ∈ c, is_preconnected s) : is_preconnected (⋃₀ c) :=
begin
apply is_preconnected_of_forall x,
rintros y ⟨s, sc, ys⟩,
exact ⟨s, subset_sUnion_of_mem sc, H1 s sc, ys, H2 s sc⟩
end
theorem is_preconnected.union (x : α) {s t : set α} (H1 : x ∈ s) (H2 : x ∈ t)
(H3 : is_preconnected s) (H4 : is_preconnected t) : is_preconnected (s ∪ t) :=
sUnion_pair s t ▸ is_preconnected_sUnion x {s, t}
(by rintro r (rfl | rfl | h); [exact H2, exact H1, exact h.elim])
(by rintro r (rfl | rfl | h); [exact H4, exact H3, exact h.elim])
theorem is_connected.union {s t : set α} (H : (s ∩ t).nonempty)
(Hs : is_connected s) (Ht : is_connected t) : is_connected (s ∪ t) :=
begin
rcases H with ⟨x, hx⟩,
refine ⟨⟨x, mem_union_left t (mem_of_mem_inter_left hx)⟩, _⟩,
exact is_preconnected.union x (mem_of_mem_inter_left hx) (mem_of_mem_inter_right hx)
Hs.is_preconnected Ht.is_preconnected
end
theorem is_preconnected.closure {s : set α} (H : is_preconnected s) :
is_preconnected (closure s) :=
λ u v hu hv hcsuv ⟨y, hycs, hyu⟩ ⟨z, hzcs, hzv⟩,
let ⟨p, hpu, hps⟩ := mem_closure_iff.1 hycs u hu hyu in
let ⟨q, hqv, hqs⟩ := mem_closure_iff.1 hzcs v hv hzv in
let ⟨r, hrs, hruv⟩ := H u v hu hv (subset.trans subset_closure hcsuv) ⟨p, hps, hpu⟩ ⟨q, hqs, hqv⟩ in
⟨r, subset_closure hrs, hruv⟩
theorem is_connected.closure {s : set α} (H : is_connected s) :
is_connected (closure s) :=
⟨H.nonempty.closure, H.is_preconnected.closure⟩
theorem is_preconnected.image [topological_space β] {s : set α} (H : is_preconnected s)
(f : α → β) (hf : continuous_on f s) : is_preconnected (f '' s) :=
begin
-- Unfold/destruct definitions in hypotheses
rintros u v hu hv huv ⟨_, ⟨x, xs, rfl⟩, xu⟩ ⟨_, ⟨y, ys, rfl⟩, yv⟩,
rcases continuous_on_iff'.1 hf u hu with ⟨u', hu', u'_eq⟩,
rcases continuous_on_iff'.1 hf v hv with ⟨v', hv', v'_eq⟩,
-- Reformulate `huv : f '' s ⊆ u ∪ v` in terms of `u'` and `v'`
replace huv : s ⊆ u' ∪ v',
{ rw [image_subset_iff, preimage_union] at huv,
replace huv := subset_inter huv (subset.refl _),
rw [inter_distrib_right, u'_eq, v'_eq, ← inter_distrib_right] at huv,
exact (subset_inter_iff.1 huv).1 },
-- Now `s ⊆ u' ∪ v'`, so we can apply `‹is_preconnected s›`
obtain ⟨z, hz⟩ : (s ∩ (u' ∩ v')).nonempty,
{ refine H u' v' hu' hv' huv ⟨x, _⟩ ⟨y, _⟩; rw inter_comm,
exacts [u'_eq ▸ ⟨xu, xs⟩, v'_eq ▸ ⟨yv, ys⟩] },
rw [← inter_self s, inter_assoc, inter_left_comm s u', ← inter_assoc,
inter_comm s, inter_comm s, ← u'_eq, ← v'_eq] at hz,
exact ⟨f z, ⟨z, hz.1.2, rfl⟩, hz.1.1, hz.2.1⟩
end
theorem is_connected.image [topological_space β] {s : set α} (H : is_connected s)
(f : α → β) (hf : continuous_on f s) : is_connected (f '' s) :=
⟨nonempty_image_iff.mpr H.nonempty, H.is_preconnected.image f hf⟩
theorem is_preconnected_closed_iff {s : set α} :
is_preconnected s ↔ ∀ t t', is_closed t → is_closed t' → s ⊆ t ∪ t' →
(s ∩ t).nonempty → (s ∩ t').nonempty → (s ∩ (t ∩ t')).nonempty :=
⟨begin
rintros h t t' ht ht' htt' ⟨x, xs, xt⟩ ⟨y, ys, yt'⟩,
by_contradiction h',
rw [← ne_empty_iff_nonempty, ne.def, not_not, ← subset_compl_iff_disjoint, compl_inter] at h',
have xt' : x ∉ t', from (h' xs).elim (absurd xt) id,
have yt : y ∉ t, from (h' ys).elim id (absurd yt'),
have := ne_empty_iff_nonempty.2 (h (-t) (-t') (is_open_compl_iff.2 ht)
(is_open_compl_iff.2 ht') h' ⟨y, ys, yt⟩ ⟨x, xs, xt'⟩),
rw [ne.def, ← compl_union, ← subset_compl_iff_disjoint, compl_compl] at this,
contradiction
end,
begin
rintros h u v hu hv huv ⟨x, xs, xu⟩ ⟨y, ys, yv⟩,
by_contradiction h',
rw [← ne_empty_iff_nonempty, ne.def, not_not,
← subset_compl_iff_disjoint, compl_inter] at h',
have xv : x ∉ v, from (h' xs).elim (absurd xu) id,
have yu : y ∉ u, from (h' ys).elim id (absurd yv),
have := ne_empty_iff_nonempty.2 (h (-u) (-v) (is_closed_compl_iff.2 hu)
(is_closed_compl_iff.2 hv) h' ⟨y, ys, yu⟩ ⟨x, xs, xv⟩),
rw [ne.def, ← compl_union, ← subset_compl_iff_disjoint, compl_compl] at this,
contradiction
end⟩
/-- The connected component of a point is the maximal connected set
that contains this point. -/
def connected_component (x : α) : set α :=
⋃₀ { s : set α | is_preconnected s ∧ x ∈ s }
theorem mem_connected_component {x : α} : x ∈ connected_component x :=
mem_sUnion_of_mem (mem_singleton x) ⟨is_connected_singleton.is_preconnected, mem_singleton x⟩
theorem is_connected_connected_component {x : α} : is_connected (connected_component x) :=
⟨⟨x, mem_connected_component⟩, is_preconnected_sUnion x _ (λ _, and.right) (λ _, and.left)⟩
theorem subset_connected_component {x : α} {s : set α} (H1 : is_preconnected s) (H2 : x ∈ s) :
s ⊆ connected_component x :=
λ z hz, mem_sUnion_of_mem hz ⟨H1, H2⟩
theorem is_closed_connected_component {x : α} :
is_closed (connected_component x) :=
closure_eq_iff_is_closed.1 $ subset.antisymm
(subset_connected_component
is_connected_connected_component.closure.is_preconnected
(subset_closure mem_connected_component))
subset_closure
theorem irreducible_component_subset_connected_component {x : α} :
irreducible_component x ⊆ connected_component x :=
subset_connected_component
is_irreducible_irreducible_component.is_connected.is_preconnected
mem_irreducible_component
/-- A preconnected space is one where there is no non-trivial open partition. -/
class preconnected_space (α : Type u) [topological_space α] : Prop :=
(is_preconnected_univ : is_preconnected (univ : set α))
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A connected space is a nonempty one where there is no non-trivial open partition. -/
class connected_space (α : Type u) [topological_space α] extends preconnected_space α : Prop :=
(to_nonempty : nonempty α)
end prio
attribute [instance, priority 50] connected_space.to_nonempty -- see Note [lower instance priority]
@[priority 100] -- see Note [lower instance priority]
instance preirreducible_space.preconnected_space (α : Type u) [topological_space α]
[preirreducible_space α] : preconnected_space α :=
⟨(preirreducible_space.is_preirreducible_univ α).is_preconnected⟩
@[priority 100] -- see Note [lower instance priority]
instance irreducible_space.connected_space (α : Type u) [topological_space α]
[irreducible_space α] : connected_space α :=
{ to_nonempty := irreducible_space.to_nonempty α }
theorem nonempty_inter [preconnected_space α] {s t : set α} :
is_open s → is_open t → s ∪ t = univ → s.nonempty → t.nonempty → (s ∩ t).nonempty :=
by simpa only [univ_inter, univ_subset_iff] using
@preconnected_space.is_preconnected_univ α _ _ s t
theorem is_clopen_iff [preconnected_space α] {s : set α} : is_clopen s ↔ s = ∅ ∨ s = univ :=
⟨λ hs, classical.by_contradiction $ λ h,
have h1 : s ≠ ∅ ∧ -s ≠ ∅, from ⟨mt or.inl h,
mt (λ h2, or.inr $ (by rw [← compl_compl s, h2, compl_empty] : s = univ)) h⟩,
let ⟨_, h2, h3⟩ := nonempty_inter hs.1 hs.2 (union_compl_self s)
(ne_empty_iff_nonempty.1 h1.1) (ne_empty_iff_nonempty.1 h1.2) in
h3 h2,
by rintro (rfl | rfl); [exact is_clopen_empty, exact is_clopen_univ]⟩
lemma subtype.preconnected_space {s : set α} (h : is_preconnected s) :
preconnected_space s :=
{ is_preconnected_univ :=
begin
intros u v hu hv hs hsu hsv,
rw is_open_induced_iff at hu hv,
rcases hu with ⟨u, hu, rfl⟩,
rcases hv with ⟨v, hv, rfl⟩,
rcases hsu with ⟨⟨x, hxs⟩, hxs', hxu⟩,
rcases hsv with ⟨⟨y, hys⟩, hys', hyv⟩,
rcases h u v hu hv _ ⟨x, hxs, hxu⟩ ⟨y, hys, hyv⟩ with ⟨z, hzs, ⟨hzu, hzv⟩⟩,
exact ⟨⟨z, hzs⟩, ⟨set.mem_univ _, ⟨hzu, hzv⟩⟩⟩,
intros z hz,
rcases hs (set.mem_univ ⟨z, hz⟩) with hzu|hzv,
{ left, assumption },
{ right, assumption }
end }
lemma subtype.connected_space {s : set α} (h : is_connected s) :
connected_space s :=
{ is_preconnected_univ :=
(subtype.preconnected_space h.is_preconnected).is_preconnected_univ,
to_nonempty := h.nonempty.to_subtype }
/-- A set `s` is preconnected if and only if
for every cover by two open sets that are disjoint on `s`,
it is contained in one of the two covering sets. -/
lemma is_preconnected_iff_subset_of_disjoint {s : set α} :
is_preconnected s ↔
∀ (u v : set α) (hu : is_open u) (hv : is_open v) (hs : s ⊆ u ∪ v) (huv : s ∩ (u ∩ v) = ∅),
s ⊆ u ∨ s ⊆ v :=
begin
split; intro h,
{ intros u v hu hv hs huv,
specialize h u v hu hv hs,
contrapose! huv,
rw ne_empty_iff_nonempty,
simp [not_subset] at huv,
rcases huv with ⟨⟨x, hxs, hxu⟩, ⟨y, hys, hyv⟩⟩,
have hxv : x ∈ v := classical.or_iff_not_imp_left.mp (hs hxs) hxu,
have hyu : y ∈ u := classical.or_iff_not_imp_right.mp (hs hys) hyv,
exact h ⟨y, hys, hyu⟩ ⟨x, hxs, hxv⟩ },
{ intros u v hu hv hs hsu hsv,
rw ← ne_empty_iff_nonempty,
intro H,
specialize h u v hu hv hs H,
contrapose H,
apply ne_empty_iff_nonempty.mpr,
cases h,
{ rcases hsv with ⟨x, hxs, hxv⟩, exact ⟨x, hxs, ⟨h hxs, hxv⟩⟩ },
{ rcases hsu with ⟨x, hxs, hxu⟩, exact ⟨x, hxs, ⟨hxu, h hxs⟩⟩ } }
end
/-- A set `s` is connected if and only if
for every cover by a finite collection of open sets that are pairwise disjoint on `s`,
it is contained in one of the members of the collection. -/
lemma is_connected_iff_sUnion_disjoint_open {s : set α} :
is_connected s ↔
∀ (U : finset (set α)) (H : ∀ (u v : set α), u ∈ U → v ∈ U → (s ∩ (u ∩ v)).nonempty → u = v)
(hU : ∀ u ∈ U, is_open u) (hs : s ⊆ ⋃₀ ↑U),
∃ u ∈ U, s ⊆ u :=
begin
rw [is_connected, is_preconnected_iff_subset_of_disjoint],
split; intro h,
{ intro U, apply finset.induction_on U,
{ rcases h.left,
suffices : s ⊆ ∅ → false, { simpa },
intro, solve_by_elim },
{ intros u U hu IH hs hU H,
rw [finset.coe_insert, sUnion_insert] at H,
cases h.2 u (⋃₀ ↑U) _ _ H _ with hsu hsU,
{ exact ⟨u, finset.mem_insert_self _ _, hsu⟩ },
{ rcases IH _ _ hsU with ⟨v, hvU, hsv⟩,
{ exact ⟨v, finset.mem_insert_of_mem hvU, hsv⟩ },
{ intros, apply hs; solve_by_elim [finset.mem_insert_of_mem] },
{ intros, solve_by_elim [finset.mem_insert_of_mem] } },
{ solve_by_elim [finset.mem_insert_self] },
{ apply is_open_sUnion,
intros, solve_by_elim [finset.mem_insert_of_mem] },
{ apply eq_empty_of_subset_empty,
rintro x ⟨hxs, hxu, hxU⟩,
rw mem_sUnion at hxU,
rcases hxU with ⟨v, hvU, hxv⟩,
rcases hs u v (finset.mem_insert_self _ _) (finset.mem_insert_of_mem hvU) _ with rfl,
{ contradiction },
{ exact ⟨x, hxs, hxu, hxv⟩ } } } },
{ split,
{ rw ← ne_empty_iff_nonempty,
by_contradiction hs, push_neg at hs, subst hs,
simpa using h ∅ _ _ _; simp },
intros u v hu hv hs hsuv,
rcases h {v, u} _ _ _ with ⟨t, ht, ht'⟩,
{ rw [finset.insert_empty_eq_singleton,
finset.mem_insert, finset.mem_singleton] at ht,
rcases ht with rfl|rfl; tauto },
{ intros t₁ t₂ ht₁ ht₂ hst,
rw ← ne_empty_iff_nonempty at hst,
rw [finset.insert_empty_eq_singleton,
finset.mem_insert, finset.mem_singleton] at ht₁ ht₂,
rcases ht₁ with rfl|rfl; rcases ht₂ with rfl|rfl,
all_goals { refl <|> contradiction <|> skip },
rw inter_comm t₁ at hst, contradiction },
{ intro t,
rw [finset.insert_empty_eq_singleton,
finset.mem_insert, finset.mem_singleton],
rintro (rfl|rfl); assumption },
{ simpa using hs } }
end
end preconnected
section totally_disconnected
/-- A set is called totally disconnected if all of its connected components are singletons. -/
def is_totally_disconnected (s : set α) : Prop :=
∀ t, t ⊆ s → is_preconnected t → subsingleton t
theorem is_totally_disconnected_empty : is_totally_disconnected (∅ : set α) :=
λ t ht _, ⟨λ ⟨_, h⟩, (ht h).elim⟩
theorem is_totally_disconnected_singleton {x} : is_totally_disconnected ({x} : set α) :=
λ t ht _, ⟨λ ⟨p, hp⟩ ⟨q, hq⟩, subtype.eq $ show p = q,
from (eq_of_mem_singleton (ht hp)).symm ▸ (eq_of_mem_singleton (ht hq)).symm⟩
/-- A space is totally disconnected if all of its connected components are singletons. -/
class totally_disconnected_space (α : Type u) [topological_space α] : Prop :=
(is_totally_disconnected_univ : is_totally_disconnected (univ : set α))
end totally_disconnected
section totally_separated
/-- A set `s` is called totally separated if any two points of this set can be separated
by two disjoint open sets covering `s`. -/
def is_totally_separated (s : set α) : Prop :=
∀ x ∈ s, ∀ y ∈ s, x ≠ y → ∃ u v : set α, is_open u ∧ is_open v ∧
x ∈ u ∧ y ∈ v ∧ s ⊆ u ∪ v ∧ u ∩ v = ∅
theorem is_totally_separated_empty : is_totally_separated (∅ : set α) :=
λ x, false.elim
theorem is_totally_separated_singleton {x} : is_totally_separated ({x} : set α) :=
λ p hp q hq hpq, (hpq $ (eq_of_mem_singleton hp).symm ▸ (eq_of_mem_singleton hq).symm).elim
theorem is_totally_disconnected_of_is_totally_separated {s : set α}
(H : is_totally_separated s) : is_totally_disconnected s :=
λ t hts ht, ⟨λ ⟨x, hxt⟩ ⟨y, hyt⟩, subtype.eq $ classical.by_contradiction $
assume hxy : x ≠ y, let ⟨u, v, hu, hv, hxu, hyv, hsuv, huv⟩ := H x (hts hxt) y (hts hyt) hxy in
let ⟨r, hrt, hruv⟩ := ht u v hu hv (subset.trans hts hsuv) ⟨x, hxt, hxu⟩ ⟨y, hyt, hyv⟩ in
((ext_iff _ _).1 huv r).1 hruv⟩
/-- A space is totally separated if any two points can be separated by two disjoint open sets
covering the whole space. -/
class totally_separated_space (α : Type u) [topological_space α] : Prop :=
(is_totally_separated_univ [] : is_totally_separated (univ : set α))
@[priority 100] -- see Note [lower instance priority]
instance totally_separated_space.totally_disconnected_space (α : Type u) [topological_space α]
[totally_separated_space α] : totally_disconnected_space α :=
⟨is_totally_disconnected_of_is_totally_separated $ totally_separated_space.is_totally_separated_univ α⟩
end totally_separated
|
e7a1be13af3cbb6acb593140e34670ae7b81a589 | 75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2 | /library/logic/examples/colog88.lean | 55e4502838227abcbcc2a17b7ce98d9ac0fbe2a1 | [
"Apache-2.0"
] | permissive | jroesch/lean | 30ef0860fa905d35b9ad6f76de1a4f65c9af6871 | 3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2 | refs/heads/master | 1,586,090,835,348 | 1,455,142,203,000 | 1,455,142,277,000 | 51,536,958 | 1 | 0 | null | 1,455,215,811,000 | 1,455,215,811,000 | null | UTF-8 | Lean | false | false | 2,741 | lean | /-
Example from "Inductively defined types",
from Thierry Coquand and Christine Paulin,
COLOG-88.
It shows it is inconsistent to allow inductive datatypes such as
inductive A : Type :=
| intro : ((A → Prop) → Prop) → A
-/
/- Phi is a positive, but not strictly positive, operator. -/
definition Phi (A : Type) := (A → Prop) → Prop
/- If we were allowed to form the inductive type
inductive A: Type :=
| introA : Phi A -> A
we would get the following
-/
universe l
-- The new type A
axiom A : Type.{l}
-- The constructor
axiom introA : Phi A → A
-- The eliminator
axiom recA : Π {C : A → Type}, (Π (p : Phi A), C (introA p)) → (Π (a : A), C a)
-- The "computational rule"
axiom recA_comp : Π {C : A → Type} (H : Π (p : Phi A), C (introA p)) (p : Phi A), recA H (introA p) = H p
-- The recursor could be used to define matchA
noncomputable definition matchA (a : A) : Phi A :=
recA (λ p, p) a
-- and the computation rule would allows us to define
lemma betaA (p : Phi A) : matchA (introA p) = p :=
!recA_comp
-- As in all inductive datatypes, we would be able to prove that constructors are injective.
lemma introA_injective : ∀ {p p' : Phi A}, introA p = introA p' → p = p' :=
λ p p' h,
assert aux : matchA (introA p) = matchA (introA p'), by rewrite h,
by rewrite [*betaA at aux]; exact aux
-- For any type T, there is an injection from T to (T → Prop)
definition i {T : Type} : T → (T → Prop) :=
λ x y, x = y
lemma i_injective {T : Type} {a b : T} : i a = i b → a = b :=
λ h,
have e₁ : i a a = i b a, by rewrite [h],
have e₂ : (a = a) = (b = a), from e₁,
have e₃ : b = a, from eq.subst e₂ rfl,
eq.symm e₃
-- Hence, by composition, we get an injection f from (A → Prop) to A
noncomputable definition f : (A → Prop) → A :=
λ p, introA (i p)
lemma f_injective : ∀ {p p' : A → Prop}, f p = f p' → p = p':=
λ (p p' : A → Prop) (h : introA (i p) = introA (i p')),
i_injective (introA_injective h)
/-
We are now back to the usual Cantor-Russel paradox.
We can define
-/
noncomputable definition P0 (a : A) : Prop :=
∃ (P : A → Prop), f P = a ∧ ¬ P a
-- i.e., P0 a := codes a set P such that x∉P
noncomputable definition x0 : A := f P0
lemma fP0_eq : f P0 = x0 :=
rfl
lemma not_P0_x0 : ¬ P0 x0 :=
λ h : P0 x0,
obtain (P : A → Prop) (hp : f P = x0 ∧ ¬ P x0), from h,
have fp_eq : f P = f P0, from and.elim_left hp,
assert p_eq : P = P0, from f_injective fp_eq,
have nh : ¬ P0 x0, by rewrite [p_eq at hp]; exact (and.elim_right hp),
absurd h nh
lemma P0_x0 : P0 x0 :=
exists.intro P0 (and.intro fP0_eq not_P0_x0)
theorem inconsistent : false :=
absurd P0_x0 not_P0_x0
|
2738fde293d77b601b36bce1fcb99c36f85b6e17 | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/data/nat/multiplicity.lean | 1598dd1289cb0597c7862dc67d10568e3aad61b6 | [
"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 | 11,118 | lean | /-
Copyright (c) 2019 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import data.nat.bitwise
import data.nat.parity
import data.nat.log
import ring_theory.int.basic
import algebra.big_operators.intervals
/-!
# Natural number multiplicity
This file contains lemmas about the multiplicity function
(the maximum prime power divding a number).
# Main results
There are natural number versions of some basic lemmas about multiplicity.
There are also lemmas about the multiplicity of primes in factorials and in binomial coefficients.
-/
open finset nat multiplicity
open_locale big_operators nat
namespace nat
/-- The multiplicity of a divisor `m` of `n`, is the cardinality of the set of
positive natural numbers `i` such that `p ^ i` divides `n`. The set is expressed
by filtering `Ico 1 b` where `b` is any bound greater than `log m n` -/
lemma multiplicity_eq_card_pow_dvd {m n b : ℕ} (hm1 : m ≠ 1) (hn0 : 0 < n) (hb : log m n < b):
multiplicity m n = ↑((finset.Ico 1 b).filter (λ i, m ^ i ∣ n)).card :=
calc multiplicity m n = ↑(Ico 1 $ ((multiplicity m n).get (finite_nat_iff.2 ⟨hm1, hn0⟩) + 1)).card :
by simp
... = ↑((finset.Ico 1 b).filter (λ i, m ^ i ∣ n)).card : congr_arg coe $ congr_arg card $
finset.ext $ λ i,
have hmn : ¬ m ^ (log m n).succ ∣ n,
from if hm0 : m = 0
then λ _, by cases n; simp [*, lt_irrefl, pow_succ'] at *
else mt (le_of_dvd hn0) (not_le_of_lt $ pow_succ_log_gt_self m n
(hm1.symm.le_iff_lt.mp (zero_lt_iff.mpr hm0.intro)) hn0),
⟨λ hi, begin
simp only [Ico.mem, mem_filter, lt_succ_iff] at *,
exact ⟨⟨hi.1, lt_of_le_of_lt hi.2 $
lt_of_lt_of_le (by rw [← enat.coe_lt_coe, enat.coe_get,
multiplicity_lt_iff_neg_dvd]; exact hmn)
hb⟩,
by rw [pow_dvd_iff_le_multiplicity];
rw [← @enat.coe_le_coe i, enat.coe_get] at hi; exact hi.2⟩
end,
begin
simp only [Ico.mem, mem_filter, lt_succ_iff, and_imp, true_and] { contextual := tt },
assume h1i hib hmin,
rwa [← enat.coe_le_coe, enat.coe_get, ← pow_dvd_iff_le_multiplicity]
end⟩
namespace prime
lemma multiplicity_one {p : ℕ} (hp : p.prime) : multiplicity p 1 = 0 :=
multiplicity.one_right (prime_iff_prime.mp hp).not_unit
lemma multiplicity_mul {p m n : ℕ} (hp : p.prime) :
multiplicity p (m * n) = multiplicity p m + multiplicity p n :=
multiplicity.mul $ prime_iff_prime.mp hp
lemma multiplicity_pow {p m n : ℕ} (hp : p.prime) :
multiplicity p (m ^ n) = n •ℕ (multiplicity p m) :=
multiplicity.pow $ prime_iff_prime.mp hp
lemma multiplicity_self {p : ℕ} (hp : p.prime) : multiplicity p p = 1 :=
multiplicity_self (prime_iff_prime.mp hp).not_unit hp.ne_zero
lemma multiplicity_pow_self {p n : ℕ} (hp : p.prime) : multiplicity p (p ^ n) = n :=
multiplicity_pow_self hp.ne_zero (prime_iff_prime.mp hp).not_unit n
/-- The multiplicity of a prime in `n!` is the sum of the quotients `n / p ^ i`.
This sum is expressed over the set `Ico 1 b` where `b` is any bound greater than `log p n` -/
lemma multiplicity_factorial {p : ℕ} (hp : p.prime) :
∀ {n b : ℕ}, log p n < b → multiplicity p n! = (∑ i in Ico 1 b, n / p ^ i : ℕ)
| 0 b hb := by simp [Ico, hp.multiplicity_one]
| (n+1) b hb :=
calc multiplicity p (n+1)! = multiplicity p n! + multiplicity p (n+1) :
by rw [factorial_succ, hp.multiplicity_mul, add_comm]
... = (∑ i in Ico 1 b, n / p ^ i : ℕ) + ((finset.Ico 1 b).filter (λ i, p ^ i ∣ n+1)).card :
by rw [multiplicity_factorial (lt_of_le_of_lt log_le_log_succ hb),
← multiplicity_eq_card_pow_dvd (ne_of_gt hp.one_lt) (succ_pos _) hb]
... = (∑ i in Ico 1 b, (n / p ^ i + if p^i ∣ n+1 then 1 else 0) : ℕ) :
by rw [sum_add_distrib, sum_boole]; simp
... = (∑ i in Ico 1 b, (n + 1) / p ^ i : ℕ) :
congr_arg coe $ finset.sum_congr rfl (by intros; simp [nat.succ_div]; congr)
/-- The multiplicity of `p` in `(p(n+1))!` is one more than the sum
of the multiplicities of `p` in `(p * n)!` and `n + 1`. -/
lemma multiplicity_factorial_mul_succ {n p : ℕ} (hp : p.prime) :
multiplicity p (p * (n + 1))! = multiplicity p (p * n)! + multiplicity p (n + 1) + 1 :=
begin
have hp' := prime_iff_prime.mp hp,
have h0 : 2 ≤ p := hp.two_le,
have h1 : 1 ≤ p * n + 1 := le_add_left _ _,
have h2 : p * n + 1 ≤ p * (n + 1), linarith,
have h3 : p * n + 1 ≤ p * (n + 1) + 1, linarith,
have hm : multiplicity p (p * n)! ≠ ⊤,
{ rw [ne.def, eq_top_iff_not_finite, not_not, finite_nat_iff],
exact ⟨hp.ne_one, factorial_pos _⟩ },
revert hm,
have h4 : ∀ m ∈ Ico (p * n + 1) (p * (n + 1)), multiplicity p m = 0,
{ intros m hm, apply multiplicity_eq_zero_of_not_dvd,
rw [← exists_lt_and_lt_iff_not_dvd _ (pos_iff_ne_zero.mpr hp.ne_zero)], rw [Ico.mem] at hm,
exact ⟨n, lt_of_succ_le hm.1, hm.2⟩ },
simp_rw [← prod_Ico_id_eq_factorial, multiplicity.finset.prod hp', ← sum_Ico_consecutive _ h1 h3,
add_assoc], intro h,
rw [enat.add_left_cancel_iff h, sum_Ico_succ_top h2, multiplicity.mul hp',
hp.multiplicity_self, sum_congr rfl h4, sum_const_zero, zero_add,
add_comm (1 : enat)]
end
/-- The multiplicity of `p` in `(pn)!` is `n` more than that of `n!`. -/
lemma multiplicity_factorial_mul {n p : ℕ} (hp : p.prime) :
multiplicity p (p * n)! = multiplicity p n! + n :=
begin
induction n with n ih,
{ simp },
{ simp [succ_eq_add_one, multiplicity.mul, hp, prime_iff_prime.mp hp, ih,
multiplicity_factorial_mul_succ, add_assoc, add_left_comm] }
end
/-- A prime power divides `n!` iff it is at most the sum of the quotients `n / p ^ i`.
This sum is expressed over the set `Ico 1 b` where `b` is any bound greater than `log p n` -/
lemma pow_dvd_factorial_iff {p : ℕ} {n r b : ℕ} (hp : p.prime) (hbn : log p n < b) :
p ^ r ∣ n! ↔ r ≤ ∑ i in Ico 1 b, n / p ^ i :=
by rw [← enat.coe_le_coe, ← hp.multiplicity_factorial hbn, ← pow_dvd_iff_le_multiplicity]
lemma multiplicity_choose_aux {p n b k : ℕ} (hp : p.prime) (hkn : k ≤ n) :
∑ i in finset.Ico 1 b, n / p ^ i =
∑ i in finset.Ico 1 b, k / p ^ i + ∑ i in finset.Ico 1 b, (n - k) / p ^ i +
((finset.Ico 1 b).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card :=
calc ∑ i in finset.Ico 1 b, n / p ^ i
= ∑ i in finset.Ico 1 b, (k + (n - k)) / p ^ i :
by simp only [nat.add_sub_cancel' hkn]
... = ∑ i in finset.Ico 1 b, (k / p ^ i + (n - k) / p ^ i +
if p ^ i ≤ k % p ^ i + (n - k) % p ^ i then 1 else 0) :
by simp only [nat.add_div (pow_pos hp.pos _)]
... = _ : by simp [sum_add_distrib, sum_boole]
/-- The multiplity of `p` in `choose n k` is the number of carries when `k` and `n - k`
are added in base `p`. The set is expressed by filtering `Ico 1 b` where `b`
is any bound greater than `log p n`. -/
lemma multiplicity_choose {p n k b : ℕ} (hp : p.prime) (hkn : k ≤ n) (hnb : log p n < b) :
multiplicity p (choose n k) =
((Ico 1 b).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card :=
have h₁ : multiplicity p (choose n k) + multiplicity p (k! * (n - k)!) =
((finset.Ico 1 b).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card +
multiplicity p (k! * (n - k)!),
begin
rw [← hp.multiplicity_mul, ← mul_assoc, choose_mul_factorial_mul_factorial hkn,
hp.multiplicity_factorial hnb, hp.multiplicity_mul,
hp.multiplicity_factorial ((log_le_log_of_le hkn).trans_lt hnb),
hp.multiplicity_factorial (lt_of_le_of_lt (log_le_log_of_le (nat.sub_le_self _ _)) hnb),
multiplicity_choose_aux hp hkn],
simp [add_comm],
end,
(enat.add_right_cancel_iff
(enat.ne_top_iff_dom.2 $
by exact finite_nat_iff.2
⟨ne_of_gt hp.one_lt, mul_pos (factorial_pos k) (factorial_pos (n - k))⟩)).1
h₁
/-- A lower bound on the multiplicity of `p` in `choose n k`. -/
lemma multiplicity_le_multiplicity_choose_add {p : ℕ} (hp : p.prime) (n k : ℕ) :
multiplicity p n ≤ multiplicity p (choose n k) + multiplicity p k :=
if hkn : n < k then by simp [choose_eq_zero_of_lt hkn]
else if hk0 : k = 0 then by simp [hk0]
else if hn0 : n = 0 then by cases k; simp [hn0, *] at *
else begin
rw [multiplicity_choose hp (le_of_not_gt hkn) (lt_succ_self _),
multiplicity_eq_card_pow_dvd (ne_of_gt hp.one_lt) (nat.pos_of_ne_zero hk0)
(lt_succ_of_le (log_le_log_of_le (le_of_not_gt hkn))),
multiplicity_eq_card_pow_dvd (ne_of_gt hp.one_lt) (nat.pos_of_ne_zero hn0) (lt_succ_self _),
← enat.coe_add, enat.coe_le_coe],
calc ((Ico 1 (log p n).succ).filter (λ i, p ^ i ∣ n)).card
≤ ((Ico 1 (log p n).succ).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i) ∪
(Ico 1 (log p n).succ).filter (λ i, p ^ i ∣ k) ).card :
card_le_of_subset $ λ i, begin
have := @le_mod_add_mod_of_dvd_add_of_not_dvd k (n - k) (p ^ i),
simp [nat.add_sub_cancel' (le_of_not_gt hkn)] at * {contextual := tt},
tauto
end
... ≤ ((Ico 1 (log p n).succ).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card +
((Ico 1 (log p n).succ).filter (λ i, p ^ i ∣ k)).card :
card_union_le _ _
end
lemma multiplicity_choose_prime_pow {p n k : ℕ} (hp : p.prime)
(hkn : k ≤ p ^ n) (hk0 : 0 < k) :
multiplicity p (choose (p ^ n) k) + multiplicity p k = n :=
le_antisymm
(have hdisj : disjoint
((Ico 1 n.succ).filter (λ i, p ^ i ≤ k % p ^ i + (p ^ n - k) % p ^ i))
((Ico 1 n.succ).filter (λ i, p ^ i ∣ k)),
by simp [disjoint_right, *, dvd_iff_mod_eq_zero, nat.mod_lt _ (pow_pos hp.pos _)]
{contextual := tt},
begin
rw [multiplicity_choose hp hkn (lt_succ_self _),
multiplicity_eq_card_pow_dvd (ne_of_gt hp.one_lt) hk0
(lt_succ_of_le (log_le_log_of_le hkn)),
← enat.coe_add, enat.coe_le_coe, log_pow _ _ hp.one_lt,
← card_disjoint_union hdisj, filter_union_right],
have filter_le_Ico := (Ico 1 n.succ).card_filter_le _,
rwa Ico.card 1 n.succ at filter_le_Ico,
end)
(by rw [← hp.multiplicity_pow_self];
exact multiplicity_le_multiplicity_choose_add hp _ _)
end prime
lemma multiplicity_two_factorial_lt : ∀ {n : ℕ} (h : n ≠ 0), multiplicity 2 n! < n :=
begin
have h2 := prime_iff_prime.mp prime_two,
refine binary_rec _ _,
{ contradiction },
{ intros b n ih h,
by_cases hn : n = 0,
{ subst hn, simp at h, simp [h, one_right h2.not_unit, enat.zero_lt_one] },
have : multiplicity 2 (2 * n)! < (2 * n : ℕ),
{ rw [prime_two.multiplicity_factorial_mul],
refine (enat.add_lt_add_right (ih hn) (enat.coe_ne_top _)).trans_le _,
rw [two_mul], norm_cast },
cases b,
{ simpa [bit0_eq_two_mul n] },
{ suffices : multiplicity 2 (2 * n + 1) + multiplicity 2 (2 * n)! < ↑(2 * n) + 1,
{ simpa [succ_eq_add_one, multiplicity.mul, h2, prime_two, nat.bit1_eq_succ_bit0,
bit0_eq_two_mul n] },
rw [multiplicity_eq_zero_of_not_dvd (two_not_dvd_two_mul_add_one n), zero_add],
refine this.trans _, exact_mod_cast lt_succ_self _ }}
end
end nat
|
89bd1932e75d255c9f22c112cf8257edb5c1847c | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/tactic/cache.lean | de1b5bc995ad5d96e5965699ed4b4891f82fa217 | [
"Apache-2.0"
] | permissive | vaibhavkarve/mathlib | a574aaf68c0a431a47fa82ce0637f0f769826bfe | 17f8340912468f49bdc30acdb9a9fa02eeb0473a | refs/heads/master | 1,621,263,802,637 | 1,585,399,588,000 | 1,585,399,588,000 | 250,833,447 | 0 | 0 | Apache-2.0 | 1,585,410,341,000 | 1,585,410,341,000 | null | UTF-8 | Lean | false | false | 4,702 | 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 tactic.doc_commands
/-!
# Instance cache tactics
For performance reasons, Lean does not automatically update its database
of class instances during a proof. The group of tactics in this file
helps to force such updates.
-/
open lean.parser
local postfix `?`:9001 := optional
local postfix *:9001 := many
namespace tactic
/-- Reset the instance cache for the main goal. -/
meta def reset_instance_cache : tactic unit := unfreeze_local_instances
namespace interactive
open interactive interactive.types
/-- Unfreeze local instances, which allows us to revert
instances in the context. -/
meta def unfreezeI := tactic.unfreeze_local_instances
/-- Reset the instance cache. This allows any new instances
added to the context to be used in typeclass inference. -/
meta def resetI := reset_instance_cache
/-- Like `intro`, but uses the introduced variable
in typeclass inference. -/
meta def introI (p : parse ident_?) : tactic unit :=
intro p >> reset_instance_cache
/-- Like `intros`, but uses the introduced variable(s)
in typeclass inference. -/
meta def introsI (p : parse ident_*) : tactic unit :=
intros p >> reset_instance_cache
/-- Used to add typeclasses to the context so that they can
be used in typeclass inference. The syntax is the same as `have`,
but the proof-omitted version is not supported. For
this one must write `have : t, { <proof> }, resetI, <proof>`. -/
meta def haveI (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse (tk ":=" *> texpr)) :
tactic unit :=
do h ← match h with
| none := get_unused_name "_inst"
| some a := return a
end,
«have» (some h) q₁ (some q₂),
match q₁ with
| none := swap >> reset_instance_cache >> swap
| some p₂ := reset_instance_cache
end
/-- Used to add typeclasses to the context so that they can
be used in typeclass inference. The syntax is the same as `let`. -/
meta def letI (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) :
tactic unit :=
do h ← match h with
| none := get_unused_name "_inst"
| some a := return a
end,
«let» (some h) q₁ q₂,
match q₁ with
| none := swap >> reset_instance_cache >> swap
| some p₂ := reset_instance_cache
end
/-- Like `exact`, but uses all variables in the context
for typeclass inference. -/
meta def exactI (q : parse texpr) : tactic unit :=
reset_instance_cache >> exact q
/--
For performance reasons, Lean does not automatically update its database
of class instances during a proof. The group of tactics described below
helps to force such updates. For a simple (but very artificial) example,
consider the function `default` from the core library. It has type
`Π (α : Sort u) [inhabited α], α`, so one can use `default α` only if Lean
can find a registered instance of `inhabited α`. Because the database of
such instance is not automatically updated during a proof, the following
attempt won't work (Lean will not pick up the instance from the local
context):
```lean
def my_id (α : Type) : α → α :=
begin
intro x,
have : inhabited α := ⟨x⟩,
exact default α, -- Won't work!
end
```
However, it will work, producing the identity function, if one replaces `have`
by its variant `haveI` described below.
* `resetI`: Reset the instance cache. This allows any instances
currently in the context to be used in typeclass inference.
* `unfreezeI`: Unfreeze local instances, which allows us to revert
instances in the context
* `introI`/`introsI`: `intro`/`intros` followed by `resetI`. Like
`intro`/`intros`, but uses the introduced variable in typeclass inference.
* `haveI`/`letI`: `have`/`let` followed by `resetI`. Used to add typeclasses
to the context so that they can be used in typeclass inference. The syntax
`haveI := <proof>` and `haveI : t := <proof>` is supported, but
`haveI : t, from _` and `haveI : t, { <proof> }` are not; in these cases
use `have : t, { <proof> }, resetI` directly.
* `exactI`: `resetI` followed by `exact`. Like `exact`, but uses all
variables in the context for typeclass inference.
-/
add_tactic_doc
{ name := "Instance cache tactics",
category := doc_category.tactic,
decl_names := [`tactic.interactive.unfreezeI, `tactic.interactive.resetI,
`tactic.interactive.introI, `tactic.interactive.introsI,
`tactic.interactive.haveI, `tactic.interactive.letI,
`tactic.interactive.exactI],
tags := ["type class", "context management"] }
end interactive
end tactic
|
683cfed0ae15a177175bd9ccc4b729ff423eb955 | c46a17c860913da8a635099f06be3a2416f6cdab | /dhs_islanders_n2.lean | aa480fedbb60d32267cf65ba1a84ea56f3024648 | [] | no_license | vladfi1/vfds | 4ed0b7fd810218c74eef933364b9aa6be4cc2551 | 664b0d1c8126c93b379115ffe5da7b943f3375d5 | refs/heads/master | 1,585,024,431,435 | 1,535,567,828,000 | 1,535,567,828,000 | 142,457,395 | 0 | 1 | null | 1,532,805,351,000 | 1,532,617,692,000 | Lean | UTF-8 | Lean | false | false | 5,370 | lean | inductive person : Type
| dan, tina
open person
@[reducible] def timestep := ℕ
constant is_marked : person → Prop
constant knows (t : timestep) (n : person) (p : Prop) : Prop
@[reducible] def mk_prec (p : timestep → person → Prop) : ℕ → timestep → person → Prop
| 0 t n := p t n
| (d+1) t₁ n₁ := ∀ {t₂ : timestep} {n₂ : person}, knows t₁ n₁ (mk_prec d t₂ n₂)
@[reducible] def pcommon_knowledge (pr : timestep → person → Prop) : Prop :=
∀ (d : ℕ) {t : timestep} {n : person}, mk_prec pr d t n
axiom knows_persists {p : Prop} : pcommon_knowledge (λ t n, knows t n p → knows (t+1) n p)
axiom knows_lam {p q : Prop} : pcommon_knowledge (λ t n, (knows t n p → knows t n q) → knows t n (p → q))
axiom knows_mp {p q : Prop} : pcommon_knowledge (λ t n, knows t n (p → q) → knows t n p → knows t n q)
axiom knows_and {p q : Prop} : pcommon_knowledge (λ t n, knows t n p → knows t n q → knows t n (p ∧ q))
axiom knows_sound {p : Prop} : pcommon_knowledge (λ t n, knows t n p → p)
axiom knows_cancel {p q : Prop} : pcommon_knowledge (λ t n, knows t n (¬ p) → knows t n (p ∨ q) → knows t n q)
axiom knows_id {p : Prop} : pcommon_knowledge (λ t n, knows t n (p → p))
axiom knows_em {p q : Prop} : pcommon_knowledge (λ t n, knows t n (p → q) → knows t n (¬ p → q) → knows t n q)
axiom oracle : pcommon_knowledge (λ t n, knows t n (is_marked dan ∨ is_marked tina))
@[reducible] def mk_rec (p : timestep → person → Prop) (t₁ t₂ : timestep) : person → ℕ → Prop
| n 0 := p t₂ n
| n₁ (d+1) := ∀ {n₂ : person}, knows t₁ n₁ (mk_rec n₂ d)
@[reducible] def common_knowledge (pr : timestep → person → Prop) (t₁ t₂ : timestep) : Prop :=
∀ (d : ℕ) {n : person}, mk_rec pr t₁ t₂ n d
axiom nobody_leaves : ∀ (t₁ t₂ : timestep), t₁ < 2 → t₁ < t₂ → common_knowledge (λ t n, ¬ knows t₁ n (is_marked n)) t₂ t₁
theorem T1 : knows 0 dan ((¬ is_marked dan) → knows 1 tina (is_marked tina)) :=
suffices H_suffices : knows 0 dan (¬ is_marked dan) → knows 0 dan (knows 1 tina (is_marked tina)), from knows_lam 0 H_suffices,
assume H1 : knows 0 dan (¬ is_marked dan),
show knows 0 dan (knows 1 tina (is_marked tina)), from
have H3a : knows 0 dan (knows 1 tina (is_marked dan → is_marked tina)), from
suffices H_suffices : knows 0 dan (knows 1 tina (is_marked dan) → knows 1 tina (is_marked tina)),
from knows_mp 0 (knows_lam 1) H_suffices,
suffices H_suffices : knows 0 dan (knows 1 tina (is_marked dan)) → knows 0 dan (knows 1 tina (is_marked tina)),
from knows_lam 0 H_suffices,
assume h1 : knows 0 dan (knows 1 tina (is_marked dan)),
absurd (knows_sound 0 (knows_sound 0 h1)) (knows_sound 0 H1),
have H3b : knows 0 dan (knows 1 tina (¬ is_marked dan → is_marked tina)), from
suffices H_suffices : knows 0 dan (knows 1 tina (¬ is_marked dan) → knows 1 tina (is_marked tina)),
from knows_mp 0 (knows_lam 1) H_suffices,
suffices H_suffices : knows 0 dan (knows 1 tina (¬ is_marked dan)) → knows 0 dan (knows 1 tina (is_marked tina)),
from knows_lam 0 H_suffices,
assume h1 : knows 0 dan (knows 1 tina (¬ is_marked dan)),
have h2 : knows 0 dan (knows 1 tina (is_marked dan ∨ is_marked tina)), from knows_mp 0 (knows_persists 1) (oracle 1),
have h3 : knows 0 dan (knows 1 tina (¬ is_marked dan))
→ knows 0 dan (knows 1 tina (is_marked dan ∨ is_marked tina) → knows 1 tina (is_marked tina)), from knows_mp 0 (knows_cancel 1),
have h4 : knows 0 dan (knows 1 tina (¬ is_marked dan))
→ knows 0 dan (knows 1 tina (is_marked dan ∨ is_marked tina))
→ knows 0 dan (knows 1 tina (is_marked tina)), from assume z1 z2, knows_mp 0 (h3 z1) z2,
h4 h1 (knows_mp 0 (knows_persists 1) (oracle 1)),
have Hem : ∀ {p q : Prop}, knows 0 dan (knows 1 tina (p → q)) → knows 0 dan (knows 1 tina ((¬ p) → q)) → knows 0 dan (knows 1 tina q), from
begin intros p q z1, apply knows_mp 0, revert z1, apply knows_mp 0, exact knows_em 1 end,
Hem H3a H3b
theorem T2 : knows 0 dan (¬ knows 1 tina (is_marked tina) → is_marked dan) :=
suffices H_suffices : knows 0 dan (¬ knows 1 tina (is_marked tina)) → knows 0 dan (is_marked dan), from knows_lam 0 H_suffices,
assume H1 : knows 0 dan (¬ knows 1 tina (is_marked tina)),
have H2 : knows 0 dan ((¬ is_marked dan) → knows 1 tina (is_marked tina)), from T1,
have H3a : knows 0 dan (is_marked dan → is_marked dan), from knows_id 0,
have H3b : knows 0 dan (¬ is_marked dan → is_marked dan), from
suffices H_suffices : knows 0 dan (¬ is_marked dan) → knows 0 dan (is_marked dan), from knows_lam 0 H_suffices,
assume h1 : knows 0 dan (¬ is_marked dan),
show knows 0 dan (is_marked dan), from
have h2 : knows 0 dan (knows 1 tina (is_marked tina)), from knows_mp 0 H2 h1,
absurd (knows_sound 0 h2) (knows_sound 0 H1),
knows_em 0 H3a H3b
theorem T3 : knows 2 dan (is_marked dan) :=
have H1 : knows 2 dan (¬ knows 1 tina (is_marked tina) → is_marked dan), from knows_persists 0 (knows_persists 0 T2),
have H2 : knows 2 dan (¬ knows 1 tina (is_marked tina)), from nobody_leaves 1 2 (nat.lt_succ_self _) (nat.lt_succ_self _) 1,
show knows 2 dan (is_marked dan), from
knows_mp 0 H1 H2
|
e6f7dadb32cf852cd6d469368e64e252107f44f7 | 618003631150032a5676f229d13a079ac875ff77 | /test/solve_by_elim.lean | 07c8f733a139ebeb1a8013ecd7c58d3a0c20d73b | [
"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,168 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon, Scott Morrison
-/
import tactic.solve_by_elim
example {a b : Prop} (h₀ : a → b) (h₁ : a) : b :=
begin
apply_assumption,
apply_assumption,
end
example {X : Type} (x : X) : x = x :=
by solve_by_elim
example : true :=
by solve_by_elim
example {a b : Prop} (h₀ : a → b) (h₁ : a) : b :=
by solve_by_elim
example {α : Type} {a b : α → Prop} (h₀ : ∀ x : α, b x = a x) (y : α) : a y = b y :=
by solve_by_elim
example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y :=
by solve_by_elim
example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y :=
begin
success_if_fail { solve_by_elim only [], },
success_if_fail { solve_by_elim only [h₀], },
solve_by_elim only [h₀, congr_fun]
end
example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y :=
by solve_by_elim [h₀]
example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y :=
begin
success_if_fail { solve_by_elim [*, -h₀] },
solve_by_elim [*]
end
example {α β : Type} (a b : α) (f : α → β) (i : function.injective f) (h : f a = f b) : a = b :=
begin
success_if_fail { solve_by_elim only [i] },
success_if_fail { solve_by_elim only [h] },
solve_by_elim only [i,h]
end
@[user_attribute]
meta def ex : user_attribute := {
name := `ex,
descr := "An example attribute for testing solve_by_elim."
}
@[ex] def f : ℕ := 0
example : ℕ := by solve_by_elim [f]
example : ℕ :=
begin
success_if_fail { solve_by_elim },
success_if_fail { solve_by_elim [-f] with ex },
solve_by_elim with ex,
end
example {α : Type} {p : α → Prop} (h₀ : ∀ x, p x) (y : α) : p y :=
begin
apply_assumption,
end
open tactic
example : true :=
begin
(do gs ← get_goals,
set_goals [],
success_if_fail `[solve_by_elim],
set_goals gs),
trivial
end
example {α : Type} (r : α → α → Prop) (f : α → α → α)
(l : ∀ a b c : α, r a b → r a (f b c) → r a c)
(a b c : α) (h₁ : r a b) (h₂ : r a (f b c)) : r a c :=
begin
solve_by_elim,
end
-- Verifying that `solve_by_elim*` acts on all remaining goals.
example (n : ℕ) : ℕ × ℕ :=
begin
split,
solve_by_elim*,
end
-- Verifying that `solve_by_elim*` backtracks when given multiple goals.
example (n m : ℕ) (f : ℕ → ℕ → Prop) (h : f n m) : ∃ p : ℕ × ℕ, f p.1 p.2 :=
begin
repeat { split },
solve_by_elim*,
end
example {a b c : ℕ} (h₁ : a ≤ b) (h₂ : b ≤ c) : a ≤ c :=
begin
apply le_trans,
solve_by_elim { backtrack_all_goals := true },
end
-- test that metavariables created for implicit arguments don't get stuck
example (P : ℕ → Type) (f : Π {n : ℕ}, P n) : P 2 × P 3 :=
begin
fsplit,
solve_by_elim* only [f],
end
example : 6 = 6 ∧ [7] = [7] :=
begin
split,
solve_by_elim* only [@rfl _],
end
example (P Q R : Prop) : P ∧ Q → P ∧ Q :=
begin
solve_by_elim [and.imp, id],
end
/-
We now test the `accept` feature of `solve_by_elim`.
Recall that the `accept` parameter has type `list expr → tactic unit`.
At each branch (not just leaf) of the backtracking search tree,
`accept` is invoked with the list of metavariables
reported by `get_goals` when `solve_by_elim` was called
(which by now may have been partially solved by previous `apply` steps),
and if it fails this branch of the search is ignored.
Non-leaf nodes of the search tree will contain metavariables,
so we can test using `expr.has_meta_var` when we're only interesting in
filtering complete solutions.
In this example, we only accept solutions that contain
a given subexpression.
-/
def solve_by_elim_use_b (a b : ℕ) : ℕ × ℕ × ℕ :=
begin
split; [skip, split],
(do
b ← get_local `b,
tactic.solve_by_elim
{ backtrack_all_goals := tt,
-- We require that in some goal, the expression `b` is used.
accept := (λ gs, gs.any_of (λ g, guard $ g.contains_expr_or_mvar b)) })
end
-- We verify that the solution did use `b`.
example : solve_by_elim_use_b 1 2 = (1, 1, 2) := rfl
|
bf2b2b7f91506ad3e082d15e1444cda01d5303ff | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/auto_include.lean | 77ec231fd2a4f2b3ef08deac0452af413073f346 | [
"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 | 438 | lean | import algebra.group
open algebra
section
variables {A : Type} [group A]
variables {B : Type} [group B]
definition foo (a b : A) : a * b = b * a :=
sorry
definition bla (b : B) : b * 1 = b :=
sorry
print foo
print bla
end
section
variable {A : Type}
variable [group A]
variable {B : Type}
variable [group B]
definition foo2 (a b : A) : a * b = b * a :=
sorry
definition bla2 (b : B) : b * 1 = b :=
sorry
print foo2
print bla2
end
|
f4af835466b8ba932545caf0f531b5acb52ca6d5 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/group_theory/archimedean.lean | 1e6c89f7fea43f96ac2ff136fe4b19c72c073985 | [] | 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,071 | lean | /-
Copyright (c) 2020 Heather Macbeth, Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Heather Macbeth, Patrick Massot
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.group_theory.subgroup
import Mathlib.algebra.archimedean
import Mathlib.PostPort
universes u_1
namespace Mathlib
/-!
# Archimedean groups
This file proves a few facts about ordered groups which satisfy the `archimedean` property, that is:
`class archimedean (α) [ordered_add_comm_monoid α] : Prop :=`
`(arch : ∀ (x : α) {y}, 0 < y → ∃ n : ℕ, x ≤ n •ℕ y)`
They are placed here in a separate file (rather than incorporated as a continuation of
`algebra.archimedean`) because they rely on some imports from `group_theory` -- bundled subgroups
in particular.
The main result is `add_subgroup.cyclic_of_min`: a subgroup of a decidable archimedean abelian
group is cyclic, if its set of positive elements has a minimal element.
This result is used in this file to deduce `int.subgroup_cyclic`, proving that every subgroup of `ℤ`
is cyclic. (There are several other methods one could use to prove this fact, including more purely
algebraic methods, but none seem to exist in mathlib as of writing. The closest is
`subgroup.is_cyclic`, but that has not been transferred to `add_subgroup`.)
The result is also used in `topology.instances.real` as an ingredient in the classification of
subgroups of `ℝ`.
-/
/-- Given a subgroup `H` of a decidable linearly ordered archimedean abelian group `G`, if there
exists a minimal element `a` of `H ∩ G_{>0}` then `H` is generated by `a`. -/
theorem add_subgroup.cyclic_of_min {G : Type u_1} [linear_ordered_add_comm_group G] [archimedean G] {H : add_subgroup G} {a : G} (ha : is_least (set_of fun (g : G) => g ∈ H ∧ 0 < g) a) : H = add_subgroup.closure (singleton a) := sorry
/-- Every subgroup of `ℤ` is cyclic. -/
theorem int.subgroup_cyclic (H : add_subgroup ℤ) : ∃ (a : ℤ), H = add_subgroup.closure (singleton a) := sorry
|
ed973331af0482f0878e0357b8ab570b6e21d88b | 82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7 | /stage0/src/Std/Data/PersistentHashMap.lean | c013a97f9ecae34418ea736296f03f1ddba68b76 | [
"Apache-2.0"
] | permissive | banksonian/lean4 | 3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc | 78da6b3aa2840693eea354a41e89fc5b212a5011 | refs/heads/master | 1,673,703,624,165 | 1,605,123,551,000 | 1,605,123,551,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 13,364 | 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
-/
namespace Std
universes u v w w'
namespace PersistentHashMap
inductive Entry (α : Type u) (β : Type v) (σ : Type w)
| entry (key : α) (val : β) : Entry α β σ
| ref (node : σ) : Entry α β σ
| null : Entry α β σ
instance {α β σ} : Inhabited (Entry α β σ) := ⟨Entry.null⟩
inductive Node (α : Type u) (β : Type v) : Type (max u v)
| entries (es : Array (Entry α β (Node α β))) : Node α β
| collision (ks : Array α) (vs : Array β) (h : ks.size = vs.size) : Node α β
instance {α β} : Inhabited (Node α β) := ⟨Node.entries #[]⟩
abbrev shift : USize := 5
abbrev branching : USize := USize.ofNat (2 ^ shift.toNat)
abbrev maxDepth : USize := 7
abbrev maxCollisions : Nat := 4
def mkEmptyEntriesArray {α β} : Array (Entry α β (Node α β)) :=
(Array.mkArray PersistentHashMap.branching.toNat PersistentHashMap.Entry.null)
end PersistentHashMap
structure PersistentHashMap (α : Type u) (β : Type v) [BEq α] [Hashable α] :=
(root : PersistentHashMap.Node α β := PersistentHashMap.Node.entries PersistentHashMap.mkEmptyEntriesArray)
(size : Nat := 0)
abbrev PHashMap (α : Type u) (β : Type v) [BEq α] [Hashable α] := PersistentHashMap α β
namespace PersistentHashMap
variables {α : Type u} {β : Type v}
def empty [BEq α] [Hashable α] : PersistentHashMap α β := {}
def isEmpty [BEq α] [Hashable α] (m : PersistentHashMap α β) : Bool :=
m.size == 0
instance [BEq α] [Hashable α] : Inhabited (PersistentHashMap α β) := ⟨{}⟩
def mkEmptyEntries {α β} : Node α β :=
Node.entries mkEmptyEntriesArray
abbrev mul2Shift (i : USize) (shift : USize) : USize := i.shiftLeft shift
abbrev div2Shift (i : USize) (shift : USize) : USize := i.shiftRight shift
abbrev mod2Shift (i : USize) (shift : USize) : USize := USize.land i ((USize.shiftLeft 1 shift) - 1)
inductive IsCollisionNode : Node α β → Prop
| mk (keys : Array α) (vals : Array β) (h : keys.size = vals.size) : IsCollisionNode (Node.collision keys vals h)
abbrev CollisionNode (α β) := { n : Node α β // IsCollisionNode n }
inductive IsEntriesNode : Node α β → Prop
| mk (entries : Array (Entry α β (Node α β))) : IsEntriesNode (Node.entries entries)
abbrev EntriesNode (α β) := { n : Node α β // IsEntriesNode n }
private theorem setSizeEq {ks : Array α} {vs : Array β} (h : ks.size = vs.size) (i : Fin ks.size) (j : Fin vs.size) (k : α) (v : β)
: (ks.set i k).size = (vs.set j v).size := by
rw [Array.szFSetEq, Array.szFSetEq vs, h]
rfl
private theorem pushSizeEq {ks : Array α} {vs : Array β} (h : ks.size = vs.size) (k : α) (v : β) : (ks.push k).size = (vs.push v).size := by
rw [Array.szPushEq, Array.szPushEq, h]
rfl
partial def insertAtCollisionNodeAux [BEq α] : CollisionNode α β → Nat → α → β → CollisionNode α β
| n@⟨Node.collision keys vals heq, _⟩, i, k, v =>
if h : i < keys.size then
let idx : Fin keys.size := ⟨i, h⟩;
let k' := keys.get idx;
if k == k' then
let j : Fin vals.size := ⟨i, by rw [←heq]; assumption⟩
⟨Node.collision (keys.set idx k) (vals.set j v) (setSizeEq heq idx j k v), IsCollisionNode.mk _ _ _⟩
else insertAtCollisionNodeAux n (i+1) k v
else
⟨Node.collision (keys.push k) (vals.push v) (pushSizeEq heq k v), IsCollisionNode.mk _ _ _⟩
| ⟨Node.entries _, h⟩, _, _, _ => False.elim (nomatch h)
def insertAtCollisionNode [BEq α] : CollisionNode α β → α → β → CollisionNode α β :=
fun n k v => insertAtCollisionNodeAux n 0 k v
def getCollisionNodeSize : CollisionNode α β → Nat
| ⟨Node.collision keys _ _, _⟩ => keys.size
| ⟨Node.entries _, h⟩ => False.elim (nomatch h)
def mkCollisionNode (k₁ : α) (v₁ : β) (k₂ : α) (v₂ : β) : Node α β :=
let ks : Array α := Array.mkEmpty maxCollisions
let ks := (ks.push k₁).push k₂
let vs : Array β := Array.mkEmpty maxCollisions
let vs := (vs.push v₁).push v₂
Node.collision ks vs rfl
partial def insertAux [BEq α] [Hashable α] : Node α β → USize → USize → α → β → Node α β
| Node.collision keys vals heq, _, depth, k, v =>
let newNode := insertAtCollisionNode ⟨Node.collision keys vals heq, IsCollisionNode.mk _ _ _⟩ k v
if depth >= maxDepth || getCollisionNodeSize newNode < maxCollisions then newNode.val
else match newNode with
| ⟨Node.entries _, h⟩ => False.elim (nomatch h)
| ⟨Node.collision keys vals heq, _⟩ =>
let rec traverse (i : Nat) (entries : Node α β) : Node α β :=
if h : i < keys.size then
let k := keys.get ⟨i, h⟩
let v := vals.get ⟨i, heq ▸ h⟩
let h := hash k
let h := div2Shift h (shift * (depth - 1))
traverse (i+1) (insertAux entries h depth k v)
else
entries
traverse 0 mkEmptyEntries
| Node.entries entries, h, depth, k, v =>
let j := (mod2Shift h shift).toNat
Node.entries $ entries.modify j fun entry =>
match entry with
| Entry.null => Entry.entry k v
| Entry.ref node => Entry.ref $ insertAux node (div2Shift h shift) (depth+1) k v
| Entry.entry k' v' =>
if k == k' then Entry.entry k v
else Entry.ref $ mkCollisionNode k' v' k v
def insert [BEq α] [Hashable α] : PersistentHashMap α β → α → β → PersistentHashMap α β
| { root := n, size := sz }, k, v => { root := insertAux n (hash k) 1 k v, size := sz + 1 }
partial def findAtAux [BEq α] (keys : Array α) (vals : Array β) (heq : keys.size = vals.size) (i : Nat) (k : α) : Option β :=
if h : i < keys.size then
let k' := keys.get ⟨i, h⟩
if k == k' then some (vals.get ⟨i, by rw [←heq]; assumption⟩)
else findAtAux keys vals heq (i+1) k
else none
partial def findAux [BEq α] : Node α β → USize → α → Option β
| Node.entries entries, h, k =>
let j := (mod2Shift h shift).toNat
match entries.get! j with
| Entry.null => none
| Entry.ref node => findAux node (div2Shift h shift) k
| Entry.entry k' v => if k == k' then some v else none
| Node.collision keys vals heq, _, k => findAtAux keys vals heq 0 k
def find? [BEq α] [Hashable α] : PersistentHashMap α β → α → Option β
| { root := n, .. }, k => findAux n (hash k) k
@[inline] def getOp [BEq α] [Hashable α] (self : PersistentHashMap α β) (idx : α) : Option β :=
self.find? idx
@[inline] def findD [BEq α] [Hashable α] (m : PersistentHashMap α β) (a : α) (b₀ : β) : β :=
(m.find? a).getD b₀
@[inline] def find! [BEq α] [Hashable α] [Inhabited β] (m : PersistentHashMap α β) (a : α) : β :=
match m.find? a with
| some b => b
| none => panic! "key is not in the map"
partial def findEntryAtAux [BEq α] (keys : Array α) (vals : Array β) (heq : keys.size = vals.size) (i : Nat) (k : α) : Option (α × β) :=
if h : i < keys.size then
let k' := keys.get ⟨i, h⟩
if k == k' then some (k', vals.get ⟨i, by rw [←heq]; assumption⟩)
else findEntryAtAux keys vals heq (i+1) k
else none
partial def findEntryAux [BEq α] : Node α β → USize → α → Option (α × β)
| Node.entries entries, h, k =>
let j := (mod2Shift h shift).toNat
match entries.get! j with
| Entry.null => none
| Entry.ref node => findEntryAux node (div2Shift h shift) k
| Entry.entry k' v => if k == k' then some (k', v) else none
| Node.collision keys vals heq, _, k => findEntryAtAux keys vals heq 0 k
def findEntry? [BEq α] [Hashable α] : PersistentHashMap α β → α → Option (α × β)
| { root := n, .. }, k => findEntryAux n (hash k) k
partial def containsAtAux [BEq α] (keys : Array α) (vals : Array β) (heq : keys.size = vals.size) (i : Nat) (k : α) : Bool :=
if h : i < keys.size then
let k' := keys.get ⟨i, h⟩
if k == k' then true
else containsAtAux keys vals heq (i+1) k
else false
partial def containsAux [BEq α] : Node α β → USize → α → Bool
| Node.entries entries, h, k =>
let j := (mod2Shift h shift).toNat
match entries.get! j with
| Entry.null => false
| Entry.ref node => containsAux node (div2Shift h shift) k
| Entry.entry k' v => k == k'
| Node.collision keys vals heq, _, k => containsAtAux keys vals heq 0 k
def contains [BEq α] [Hashable α] : PersistentHashMap α β → α → Bool
| { root := n, .. }, k => containsAux n (hash k) k
partial def isUnaryEntries (a : Array (Entry α β (Node α β))) (i : Nat) (acc : Option (α × β)) : Option (α × β) :=
if h : i < a.size then
match a.get ⟨i, h⟩ with
| Entry.null => isUnaryEntries a (i+1) acc
| Entry.ref _ => none
| Entry.entry k v =>
match acc with
| none => isUnaryEntries a (i+1) (some (k, v))
| some _ => none
else acc
def isUnaryNode : Node α β → Option (α × β)
| Node.entries entries => isUnaryEntries entries 0 none
| Node.collision keys vals heq =>
if h : 1 = keys.size then
have 0 < keys.size by rw [←h]; decide!
some (keys.get ⟨0, this⟩, vals.get ⟨0, by rw [←heq]; assumption⟩)
else
none
partial def eraseAux [BEq α] : Node α β → USize → α → Node α β × Bool
| n@(Node.collision keys vals heq), _, k =>
match keys.indexOf? k with
| some idx =>
let ⟨keys', keq⟩ := keys.eraseIdx' idx
let ⟨vals', veq⟩ := vals.eraseIdx' (Eq.ndrec idx heq)
have keys.size - 1 = vals.size - 1 by rw [heq]; rfl
(Node.collision keys' vals' (keq.trans (this.trans veq.symm)), true)
| none => (n, false)
| n@(Node.entries entries), h, k =>
let j := (mod2Shift h shift).toNat
let entry := entries.get! j
match entry with
| Entry.null => (n, false)
| Entry.entry k' v =>
if k == k' then (Node.entries (entries.set! j Entry.null), true) else (n, false)
| Entry.ref node =>
let entries := entries.set! j Entry.null
let (newNode, deleted) := eraseAux node (div2Shift h shift) k
if !deleted then (n, false)
else match isUnaryNode newNode with
| none => (Node.entries (entries.set! j (Entry.ref newNode)), true)
| some (k, v) => (Node.entries (entries.set! j (Entry.entry k v)), true)
def erase [BEq α] [Hashable α] : PersistentHashMap α β → α → PersistentHashMap α β
| { root := n, size := sz }, k =>
let h := hash k
let (n, del) := eraseAux n h k
{ root := n, size := if del then sz - 1 else sz }
section
variables {m : Type w → Type w'} [Monad m]
variables {σ : Type w}
@[specialize] partial def foldlMAux (f : σ → α → β → m σ) : Node α β → σ → m σ
| Node.collision keys vals heq, acc =>
let rec traverse (i : Nat) (acc : σ) : m σ := do
if h : i < keys.size then
let k := keys.get ⟨i, h⟩
let v := vals.get ⟨i, heq ▸ h⟩
traverse (i+1) (← f acc k v)
else
pure acc
traverse 0 acc
| Node.entries entries, acc => entries.foldlM (fun acc entry =>
match entry with
| Entry.null => pure acc
| Entry.entry k v => f acc k v
| Entry.ref node => foldlMAux f node acc)
acc
@[specialize] def foldlM [BEq α] [Hashable α] (map : PersistentHashMap α β) (f : σ → α → β → m σ) (init : σ) : m σ :=
foldlMAux f map.root init
@[specialize] def forM [BEq α] [Hashable α] (map : PersistentHashMap α β) (f : α → β → m PUnit) : m PUnit :=
map.foldlM (fun _ => f) ⟨⟩
@[specialize] def foldl [BEq α] [Hashable α] (map : PersistentHashMap α β) (f : σ → α → β → σ) (init : σ) : σ :=
Id.run $ map.foldlM f init
end
def toList [BEq α] [Hashable α] (m : PersistentHashMap α β) : List (α × β) :=
m.foldl (init := []) fun ps k v => (k, v) :: ps
structure Stats :=
(numNodes : Nat := 0)
(numNull : Nat := 0)
(numCollisions : Nat := 0)
(maxDepth : Nat := 0)
partial def collectStats : Node α β → Stats → Nat → Stats
| Node.collision keys _ _, stats, depth =>
{ stats with
numNodes := stats.numNodes + 1,
numCollisions := stats.numCollisions + keys.size - 1,
maxDepth := Nat.max stats.maxDepth depth }
| Node.entries entries, stats, depth =>
let stats :=
{ stats with
numNodes := stats.numNodes + 1,
maxDepth := Nat.max stats.maxDepth depth }
entries.foldl (fun stats entry =>
match entry with
| Entry.null => { stats with numNull := stats.numNull + 1 }
| Entry.ref node => collectStats node stats (depth + 1)
| Entry.entry _ _ => stats)
stats
def stats [BEq α] [Hashable α] (m : PersistentHashMap α β) : Stats :=
collectStats m.root {} 1
def Stats.toString (s : Stats) : String :=
s!"\{ nodes := {s.numNodes}, null := {s.numNull}, collisions := {s.numCollisions}, depth := {s.maxDepth}}"
instance : ToString Stats := ⟨Stats.toString⟩
end PersistentHashMap
end Std
|
89bfd7bc01b1e974c503ad39f1e579e4cec915e8 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/category/Module/change_of_rings.lean | 327a5e779e98a6a9c3de53fc937d4e4470f0f7fa | [
"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 | 22,698 | lean | /-
Copyright (c) 2022 Jujian Zhang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jujian Zhang
-/
import algebra.category.Module.basic
import ring_theory.tensor_product
/-!
# Change Of Rings
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
## Main definitions
* `category_theory.Module.restrict_scalars`: given rings `R, S` and a ring homomorphism `R ⟶ S`,
then `restrict_scalars : Module S ⥤ Module R` is defined by `M ↦ M` where `M : S-module` is seen
as `R-module` by `r • m := f r • m` and `S`-linear map `l : M ⟶ M'` is `R`-linear as well.
* `category_theory.Module.extend_scalars`: given **commutative** rings `R, S` and ring homomorphism
`f : R ⟶ S`, then `extend_scalars : Module R ⥤ Module S` is defined by `M ↦ S ⨂ M` where the
module structure is defined by `s • (s' ⊗ m) := (s * s') ⊗ m` and `R`-linear map `l : M ⟶ M'`
is sent to `S`-linear map `s ⊗ m ↦ s ⊗ l m : S ⨂ M ⟶ S ⨂ M'`.
* `category_theory.Module.coextend_scalars`: given rings `R, S` and a ring homomorphism `R ⟶ S`
then `coextend_scalars : Module R ⥤ Module S` is defined by `M ↦ (S →ₗ[R] M)` where `S` is seen as
`R-module` by restriction of scalars and `l ↦ l ∘ _`.
## Main results
* `category_theory.Module.extend_restrict_scalars_adj`: given commutative rings `R, S` and a ring
homomorphism `f : R →+* S`, the extension and restriction of scalars by `f` are adjoint functors.
* `category_theory.Module.restrict_coextend_scalars_adj`: given rings `R, S` and a ring homomorphism
`f : R ⟶ S` then `coextend_scalars f` is the right adjoint of `restrict_scalars f`.
## List of notations
Let `R, S` be rings and `f : R →+* S`
* if `M` is an `R`-module, `s : S` and `m : M`, then `s ⊗ₜ[R, f] m` is the pure tensor
`s ⊗ m : S ⊗[R, f] M`.
-/
namespace category_theory.Module
universes v u₁ u₂
namespace restrict_scalars
variables {R : Type u₁} {S : Type u₂} [ring R] [ring S] (f : R →+* S)
variable (M : Module.{v} S)
/-- Any `S`-module M is also an `R`-module via a ring homomorphism `f : R ⟶ S` by defining
`r • m := f r • m` (`module.comp_hom`). This is called restriction of scalars. -/
def obj' : Module R :=
{ carrier := M,
is_module := module.comp_hom M f }
/--
Given an `S`-linear map `g : M → M'` between `S`-modules, `g` is also `R`-linear between `M` and
`M'` by means of restriction of scalars.
-/
def map' {M M' : Module.{v} S} (g : M ⟶ M') :
obj' f M ⟶ obj' f M' :=
{ map_smul' := λ r, g.map_smul (f r), ..g }
end restrict_scalars
/--
The restriction of scalars operation is functorial. For any `f : R →+* S` a ring homomorphism,
* an `S`-module `M` can be considered as `R`-module by `r • m = f r • m`
* an `S`-linear map is also `R`-linear
-/
def restrict_scalars {R : Type u₁} {S : Type u₂} [ring R] [ring S] (f : R →+* S) :
Module.{v} S ⥤ Module.{v} R :=
{ obj := restrict_scalars.obj' f,
map := λ _ _, restrict_scalars.map' f,
map_id' := λ _, linear_map.ext $ λ m, rfl,
map_comp' := λ _ _ _ g h, linear_map.ext $ λ m, rfl }
instance {R : Type u₁} {S : Type u₂} [comm_ring R] [comm_ring S] (f : R →+* S) :
category_theory.faithful (restrict_scalars.{v} f) :=
{ map_injective' := λ _ _ _ _ h, linear_map.ext $ λ x, by simpa only using fun_like.congr_fun h x }
@[simp] lemma restrict_scalars.map_apply {R : Type u₁} {S : Type u₂} [ring R] [ring S] (f : R →+* S)
{M M' : Module.{v} S} (g : M ⟶ M') (x) : (restrict_scalars f).map g x = g x := rfl
@[simp] lemma restrict_scalars.smul_def {R : Type u₁} {S : Type u₂} [ring R] [ring S] (f : R →+* S)
{M : Module.{v} S} (r : R) (m : (restrict_scalars f).obj M) : r • m = (f r • m : M) := rfl
lemma restrict_scalars.smul_def' {R : Type u₁} {S : Type u₂} [ring R] [ring S] (f : R →+* S)
{M : Module.{v} S} (r : R) (m : M) : (r • m : (restrict_scalars f).obj M) = (f r • m : M) := rfl
@[priority 100]
instance smul_comm_class_mk {R : Type u₁} {S : Type u₂} [ring R] [comm_ring S] (f : R →+* S)
(M : Type v) [add_comm_group M] [module S M] :
@smul_comm_class R S M ((restrict_scalars.obj' f (Module.mk M)).is_module.to_has_smul) _ :=
{ smul_comm := λ r s m, (by simp [←mul_smul, mul_comm] : f r • s • m = s • f r • m) }
namespace extend_scalars
open tensor_product
variables {R : Type u₁} {S : Type u₂} [comm_ring R] [comm_ring S] (f : R →+* S)
section unbundled
variables (M : Type v) [add_comm_monoid M] [module R M]
-- This notation is necessary because we need to reason about `s ⊗ₜ m` where `s : S` and `m : M`;
-- without this notation, one need to work with `s : (restrict_scalars f).obj ⟨S⟩`.
localized "notation s `⊗ₜ[` R `,` f `]` m := @tensor_product.tmul R _ _ _ _ _
(module.comp_hom _ f) _ s m" in change_of_rings
end unbundled
open_locale change_of_rings
variables (M : Module.{v} R)
/--
Extension of scalars turn an `R`-module into `S`-module by M ↦ S ⨂ M
-/
def obj' : Module S :=
⟨tensor_product R ((restrict_scalars f).obj ⟨S⟩) M⟩
/--
Extension of scalars is a functor where an `R`-module `M` is sent to `S ⊗ M` and
`l : M1 ⟶ M2` is sent to `s ⊗ m ↦ s ⊗ l m`
-/
def map' {M1 M2 : Module.{v} R} (l : M1 ⟶ M2) : (obj' f M1) ⟶ (obj' f M2) :=
-- The "by apply" part makes this require 75% fewer heartbeats to process (#16371).
by apply (@linear_map.base_change R S M1 M2 _ _ ((algebra_map S _).comp f).to_algebra _ _ _ _ l)
lemma map'_id {M : Module.{v} R} : map' f (𝟙 M) = 𝟙 _ :=
linear_map.ext $ λ (x : obj' f M),
begin
dsimp only [map', Module.id_apply],
induction x using tensor_product.induction_on with _ _ m s ihx ihy,
{ simp only [map_zero], },
{ rw [linear_map.base_change_tmul, Module.id_apply], },
{ rw [map_add, ihx, ihy] },
end
lemma map'_comp {M₁ M₂ M₃ : Module.{v} R} (l₁₂ : M₁ ⟶ M₂) (l₂₃ : M₂ ⟶ M₃) :
map' f (l₁₂ ≫ l₂₃) = map' f l₁₂ ≫ map' f l₂₃ :=
linear_map.ext $ λ (x : obj' f M₁),
begin
dsimp only [map'],
induction x using tensor_product.induction_on with _ _ x y ihx ihy,
{ refl, },
{ refl, },
{ simp only [map_add, ihx, ihy], },
end
end extend_scalars
/--
Extension of scalars is a functor where an `R`-module `M` is sent to `S ⊗ M` and
`l : M1 ⟶ M2` is sent to `s ⊗ m ↦ s ⊗ l m`
-/
def extend_scalars {R : Type u₁} {S : Type u₂} [comm_ring R] [comm_ring S] (f : R →+* S) :
Module.{v} R ⥤ Module.{max v u₂} S :=
{ obj := λ M, extend_scalars.obj' f M,
map := λ M1 M2 l, extend_scalars.map' f l,
map_id' := λ _, extend_scalars.map'_id f,
map_comp' := λ _ _ _, extend_scalars.map'_comp f }
namespace extend_scalars
open_locale change_of_rings
variables {R : Type u₁} {S : Type u₂} [comm_ring R] [comm_ring S] (f : R →+* S)
@[simp] protected lemma smul_tmul {M : Module.{v} R} (s s' : S) (m : M) :
s • (s' ⊗ₜ[R, f] m : (extend_scalars f).obj M) = (s * s') ⊗ₜ[R, f] m := rfl
@[simp] lemma map_tmul {M M' : Module.{v} R} (g : M ⟶ M') (s : S) (m : M) :
(extend_scalars f).map g (s ⊗ₜ[R, f] m) = s ⊗ₜ[R, f] g m := rfl
end extend_scalars
namespace coextend_scalars
variables {R : Type u₁} {S : Type u₂} [ring R] [ring S] (f : R →+* S)
section unbundled
variables (M : Type v) [add_comm_monoid M] [module R M]
-- We use `S'` to denote `S` viewed as `R`-module, via the map `f`.
local notation `S'` := (restrict_scalars f).obj ⟨S⟩
/--
Given an `R`-module M, consider Hom(S, M) -- the `R`-linear maps between S (as an `R`-module by
means of restriction of scalars) and M. `S` acts on Hom(S, M) by `s • g = x ↦ g (x • s)`
-/
instance has_smul : has_smul S $ S' →ₗ[R] M :=
{ smul := λ s g,
{ to_fun := λ (s' : S), g (s' * s : S),
map_add' := λ (x y : S), by simp [add_mul, map_add],
map_smul' := λ r (t : S), by rw [ring_hom.id_apply, @restrict_scalars.smul_def _ _ _ _ f ⟨S⟩,
←linear_map.map_smul, @restrict_scalars.smul_def _ _ _ _ f ⟨S⟩, smul_eq_mul, smul_eq_mul,
mul_assoc] } }
@[simp] lemma smul_apply' (s : S) (g : S' →ₗ[R] M) (s' : S) :
@has_smul.smul _ _ (coextend_scalars.has_smul f _) s g s' = g (s' * s : S) := rfl
instance mul_action : mul_action S $ S' →ₗ[R] M :=
{ one_smul := λ g, linear_map.ext $ λ (s : S), by simp,
mul_smul := λ (s t : S) g, linear_map.ext $ λ (x : S), by simp [mul_assoc],
..coextend_scalars.has_smul f _ }
instance distrib_mul_action : distrib_mul_action S $ S' →ₗ[R] M :=
{ smul_add := λ s g h, linear_map.ext $ λ (t : S), by simp,
smul_zero := λ s, linear_map.ext $ λ (t : S), by simp,
..coextend_scalars.mul_action f _ }
/--
`S` acts on Hom(S, M) by `s • g = x ↦ g (x • s)`, this action defines an `S`-module structure on
Hom(S, M).
-/
instance is_module : module S $ S' →ₗ[R] M :=
{ add_smul := λ s1 s2 g, linear_map.ext $ λ (x : S), by simp [mul_add],
zero_smul := λ g, linear_map.ext $ λ (x : S), by simp,
..coextend_scalars.distrib_mul_action f _ }
end unbundled
variable (M : Module.{v} R)
/-- If `M` is an `R`-module, then the set of `R`-linear maps `S →ₗ[R] M` is an `S`-module with
scalar multiplication defined by `s • l := x ↦ l (x • s)`-/
def obj' : Module S := ⟨(restrict_scalars f).obj ⟨S⟩ →ₗ[R] M⟩
instance : has_coe_to_fun (obj' f M) (λ g, S → M) :=
{ coe := λ g, g.to_fun }
/-- If `M, M'` are `R`-modules, then any `R`-linear map `g : M ⟶ M'` induces an `S`-linear map
`(S →ₗ[R] M) ⟶ (S →ₗ[R] M')` defined by `h ↦ g ∘ h`-/
@[simps] def map' {M M' : Module R} (g : M ⟶ M') : obj' f M ⟶ obj' f M' :=
{ to_fun := λ h, g.comp h,
map_add' := λ _ _, linear_map.comp_add _ _ _,
map_smul' := λ s h, linear_map.ext $ λ (t : S), by simpa only [smul_apply'] }
end coextend_scalars
/--
For any rings `R, S` and a ring homomorphism `f : R →+* S`, there is a functor from `R`-module to
`S`-module defined by `M ↦ (S →ₗ[R] M)` where `S` is considered as an `R`-module via restriction of
scalars and `g : M ⟶ M'` is sent to `h ↦ g ∘ h`.
-/
def coextend_scalars {R : Type u₁} {S : Type u₂} [ring R] [ring S] (f : R →+* S) :
Module R ⥤ Module S :=
{ obj := coextend_scalars.obj' f,
map := λ _ _, coextend_scalars.map' f,
map_id' := λ M, linear_map.ext $ λ h, linear_map.ext $ λ x, rfl,
map_comp' := λ _ _ _ g h, linear_map.ext $ λ h, linear_map.ext $ λ x, rfl }
namespace coextend_scalars
variables {R : Type u₁} {S : Type u₂} [ring R] [ring S] (f : R →+* S)
instance (M : Module R) : has_coe_to_fun ((coextend_scalars f).obj M) (λ g, S → M) :=
(infer_instance : has_coe_to_fun (coextend_scalars.obj' f M) _)
lemma smul_apply (M : Module R) (g : (coextend_scalars f).obj M) (s s' : S) :
(s • g) s' = g (s' * s) := rfl
@[simp] lemma map_apply {M M' : Module R} (g : M ⟶ M') (x) (s : S) :
(coextend_scalars f).map g x s = g (x s) := rfl
end coextend_scalars
namespace restriction_coextension_adj
variables {R : Type u₁} {S : Type u₂} [ring R] [ring S] (f : R →+* S)
/--
Given `R`-module X and `S`-module Y, any `g : (restrict_of_scalars f).obj Y ⟶ X`
corresponds to `Y ⟶ (coextend_scalars f).obj X` by sending `y ↦ (s ↦ g (s • y))`
-/
@[simps] def hom_equiv.from_restriction {X Y} (g : (restrict_scalars f).obj Y ⟶ X) :
Y ⟶ (coextend_scalars f).obj X :=
{ to_fun := λ (y : Y),
{ to_fun := λ (s : S), g $ (s • y : Y),
map_add' := λ (s1 s2 : S), by simp [add_smul],
map_smul' := λ r (s : S), by rw [ring_hom.id_apply, ←g.map_smul,
@restrict_scalars.smul_def _ _ _ _ f ⟨S⟩, smul_eq_mul, mul_smul,
@restrict_scalars.smul_def _ _ _ _ f Y] },
map_add' := λ (y1 y2 : Y), linear_map.ext $ λ (s : S),
by rw [linear_map.add_apply, linear_map.coe_mk, linear_map.coe_mk, linear_map.coe_mk,
smul_add, map_add],
map_smul' := λ s y, linear_map.ext $ λ (t : S), by simp [mul_smul] }
/--
Given `R`-module X and `S`-module Y, any `g : Y ⟶ (coextend_scalars f).obj X`
corresponds to `(restrict_scalars f).obj Y ⟶ X` by `y ↦ g y 1`
-/
@[simps] def hom_equiv.to_restriction {X Y} (g : Y ⟶ (coextend_scalars f).obj X) :
(restrict_scalars f).obj Y ⟶ X :=
{ to_fun := λ (y : Y), (g y).to_fun (1 : S),
map_add' := λ x y, by simp only [g.map_add, linear_map.to_fun_eq_coe, linear_map.add_apply],
map_smul' := λ r (y : Y), by rw [linear_map.to_fun_eq_coe, linear_map.to_fun_eq_coe,
ring_hom.id_apply, ←linear_map.map_smul, restrict_scalars.smul_def f r y,
@restrict_scalars.smul_def _ _ _ _ f ⟨S⟩, smul_eq_mul, mul_one, linear_map.map_smul,
coextend_scalars.smul_apply, one_mul], }
/--
The natural transformation from identity functor to the composition of restriction and coextension
of scalars.
-/
@[simps] protected def unit' : 𝟭 (Module S) ⟶ restrict_scalars f ⋙ coextend_scalars f :=
{ app := λ Y,
{ to_fun := λ (y : Y),
{ to_fun := λ (s : S), (s • y : Y),
map_add' := λ s s', add_smul _ _ _,
map_smul' := λ r (s : S), by rw [ring_hom.id_apply, @restrict_scalars.smul_def _ _ _ _ f ⟨S⟩,
smul_eq_mul, mul_smul, restrict_scalars.smul_def f] },
map_add' := λ y1 y2, linear_map.ext $ λ (s : S), by rw [linear_map.add_apply, linear_map.coe_mk,
linear_map.coe_mk, linear_map.coe_mk, smul_add],
map_smul' := λ s (y : Y), linear_map.ext $ λ (t : S), by simp [mul_smul] },
naturality' := λ Y Y' g, linear_map.ext $ λ (y : Y), linear_map.ext $ λ (s : S),
by simp [coextend_scalars.map_apply] }
/--
The natural transformation from the composition of coextension and restriction of scalars to
identity functor.
-/
@[simps] protected def counit' : coextend_scalars f ⋙ restrict_scalars f ⟶ 𝟭 (Module R) :=
{ app := λ X,
{ to_fun := λ g, g.to_fun (1 : S),
map_add' := λ x1 x2, by simp [linear_map.to_fun_eq_coe],
map_smul' := λ r (g : (restrict_scalars f).obj ((coextend_scalars f).obj X)),
begin
simp only [linear_map.to_fun_eq_coe, ring_hom.id_apply],
rw [restrict_scalars.smul_def f, coextend_scalars.smul_apply, one_mul, ←linear_map.map_smul,
@restrict_scalars.smul_def _ _ _ _ f ⟨S⟩, smul_eq_mul, mul_one],
end },
naturality' := λ X X' g, linear_map.ext $ λ h, by simp [coextend_scalars.map_apply] }
end restriction_coextension_adj
/-- Restriction of scalars is left adjoint to coextension of scalars. -/
@[simps] def restrict_coextend_scalars_adj {R : Type u₁} {S : Type u₂} [ring R] [ring S]
(f : R →+* S) : restrict_scalars f ⊣ coextend_scalars f :=
{ hom_equiv := λ X Y,
{ to_fun := restriction_coextension_adj.hom_equiv.from_restriction f,
inv_fun := restriction_coextension_adj.hom_equiv.to_restriction f,
left_inv := λ g, linear_map.ext $ λ (x : X), by simp,
right_inv := λ g, linear_map.ext $ λ x, linear_map.ext $ λ (s : S), by simp },
unit := restriction_coextension_adj.unit' f,
counit := restriction_coextension_adj.counit' f,
hom_equiv_unit' := λ X Y g, linear_map.ext $ λ y, rfl,
hom_equiv_counit' := λ Y X g, linear_map.ext $ λ (y : Y), by simp }
instance {R : Type u₁} {S : Type u₂} [ring R] [ring S] (f : R →+* S) :
category_theory.is_left_adjoint (restrict_scalars f) := ⟨_, restrict_coextend_scalars_adj f⟩
instance {R : Type u₁} {S : Type u₂} [ring R] [ring S] (f : R →+* S) :
category_theory.is_right_adjoint (coextend_scalars f) := ⟨_, restrict_coextend_scalars_adj f⟩
namespace extend_restrict_scalars_adj
open_locale change_of_rings
open tensor_product
variables {R : Type u₁} {S : Type u₂} [comm_ring R] [comm_ring S] (f : R →+* S)
/--
Given `R`-module X and `S`-module Y and a map `g : (extend_scalars f).obj X ⟶ Y`, i.e. `S`-linear
map `S ⨂ X → Y`, there is a `X ⟶ (restrict_scalars f).obj Y`, i.e. `R`-linear map `X ⟶ Y` by
`x ↦ g (1 ⊗ x)`.
-/
@[simps] def hom_equiv.to_restrict_scalars {X Y} (g : (extend_scalars f).obj X ⟶ Y) :
X ⟶ (restrict_scalars f).obj Y :=
{ to_fun := λ x, g $ (1 : S) ⊗ₜ[R, f] x,
map_add' := λ _ _, by rw [tmul_add, map_add],
map_smul' := λ r x,
begin
letI : module R S := module.comp_hom S f,
letI : module R Y := module.comp_hom Y f,
rw [ring_hom.id_apply, restrict_scalars.smul_def, ←linear_map.map_smul, tmul_smul],
congr,
end }
/--
Given `R`-module X and `S`-module Y and a map `X ⟶ (restrict_scalars f).obj Y`, i.e `R`-linear map
`X ⟶ Y`, there is a map `(extend_scalars f).obj X ⟶ Y`, i.e `S`-linear map `S ⨂ X → Y` by
`s ⊗ x ↦ s • g x`.
-/
@[simps] def hom_equiv.from_extend_scalars {X Y} (g : X ⟶ (restrict_scalars f).obj Y) :
(extend_scalars f).obj X ⟶ Y :=
begin
letI m1 : module R S := module.comp_hom S f, letI m2 : module R Y := module.comp_hom Y f,
refine ⟨λ z, tensor_product.lift ⟨λ s, ⟨_, _, _⟩, _, _⟩ z, _, _⟩,
{ exact λ x, s • g x },
{ intros, rw [map_add, smul_add], },
{ intros, rw [ring_hom.id_apply, smul_comm, ←linear_map.map_smul], },
{ intros, ext, simp only [linear_map.coe_mk, linear_map.add_apply], rw ←add_smul, },
{ intros, ext,
simp only [linear_map.coe_mk, ring_hom.id_apply, linear_map.smul_apply,
restrict_scalars.smul_def, smul_eq_mul],
convert mul_smul _ _ _, },
{ intros, rw [map_add], },
{ intros r z,
rw [ring_hom.id_apply],
induction z using tensor_product.induction_on with x y x y ih1 ih2,
{ simp only [smul_zero, map_zero], },
{ simp only [linear_map.coe_mk, extend_scalars.smul_tmul, lift.tmul, ←mul_smul], },
{ rw [smul_add, map_add, ih1, ih2, map_add, smul_add], }, },
end
/--
Given `R`-module X and `S`-module Y, `S`-linear linear maps `(extend_scalars f).obj X ⟶ Y`
bijectively correspond to `R`-linear maps `X ⟶ (restrict_scalars f).obj Y`.
-/
@[simps]
def hom_equiv {X Y} : ((extend_scalars f).obj X ⟶ Y) ≃ (X ⟶ (restrict_scalars f).obj Y) :=
{ to_fun := hom_equiv.to_restrict_scalars f,
inv_fun := hom_equiv.from_extend_scalars f,
left_inv := λ g, begin
ext z,
induction z using tensor_product.induction_on with x s z1 z2 ih1 ih2,
{ simp only [map_zero], },
{ erw tensor_product.lift.tmul,
simp only [linear_map.coe_mk],
change S at x,
erw [←linear_map.map_smul, extend_scalars.smul_tmul, mul_one x], },
{ rw [map_add, map_add, ih1, ih2], }
end,
right_inv := λ g,
begin
ext,
rw [hom_equiv.to_restrict_scalars_apply, hom_equiv.from_extend_scalars_apply, lift.tmul,
linear_map.coe_mk, linear_map.coe_mk],
convert one_smul _ _,
end }
/--
For any `R`-module X, there is a natural `R`-linear map from `X` to `X ⨂ S` by sending `x ↦ x ⊗ 1`
-/
@[simps] def unit.map {X} : X ⟶ (extend_scalars f ⋙ restrict_scalars f).obj X :=
{ to_fun := λ x, (1 : S) ⊗ₜ[R, f] x,
map_add' := λ x x', by { rw tensor_product.tmul_add, },
map_smul' := λ r x, by { letI m1 : module R S := module.comp_hom S f, tidy } }
/--
The natural transformation from identity functor on `R`-module to the composition of extension and
restriction of scalars.
-/
@[simps] def unit : 𝟭 (Module R) ⟶ extend_scalars f ⋙ restrict_scalars f :=
{ app := λ _, unit.map f, naturality' := λ X X' g, by tidy }
/--
For any `S`-module Y, there is a natural `R`-linear map from `S ⨂ Y` to `Y` by
`s ⊗ y ↦ s • y`
-/
@[simps] def counit.map {Y} : (restrict_scalars f ⋙ extend_scalars f).obj Y ⟶ Y :=
begin
letI m1 : module R S := module.comp_hom S f,
letI m2 : module R Y := module.comp_hom Y f,
refine ⟨tensor_product.lift ⟨λ (s : S), ⟨λ (y : Y), s • y, smul_add _, _⟩, _, _⟩, _, _⟩,
{ intros, rw [ring_hom.id_apply, restrict_scalars.smul_def, ←mul_smul, mul_comm, mul_smul,
restrict_scalars.smul_def], },
{ intros, ext, simp only [linear_map.add_apply, linear_map.coe_mk, add_smul], },
{ intros, ext,
simpa only [ring_hom.id_apply, linear_map.smul_apply, linear_map.coe_mk,
@restrict_scalars.smul_def _ _ _ _ f ⟨S⟩, smul_eq_mul, mul_smul], },
{ intros, rw [map_add], },
{ intros s z,
rw [ring_hom.id_apply],
induction z using tensor_product.induction_on with x s' z1 z2 ih1 ih2,
{ simp only [smul_zero, map_zero], },
{ simp only [extend_scalars.smul_tmul, linear_map.coe_mk, tensor_product.lift.tmul, mul_smul] },
{ rw [smul_add, map_add, map_add, ih1, ih2, smul_add], } },
end
/--
The natural transformation from the composition of restriction and extension of scalars to the
identity functor on `S`-module.
-/
@[simps] def counit : (restrict_scalars f ⋙ extend_scalars f) ⟶ (𝟭 (Module S)) :=
{ app := λ _, counit.map f,
naturality' := λ Y Y' g,
begin
ext z, induction z using tensor_product.induction_on,
{ simp only [map_zero] },
{ simp only [category_theory.functor.comp_map, Module.coe_comp, function.comp_app,
extend_scalars.map_tmul, restrict_scalars.map_apply, counit.map_apply, lift.tmul,
linear_map.coe_mk, category_theory.functor.id_map, linear_map.map_smulₛₗ,
ring_hom.id_apply] },
{ simp only [map_add, *] },
end }
end extend_restrict_scalars_adj
/--
Given commutative rings `R, S` and a ring hom `f : R →+* S`, the extension and restriction of
scalars by `f` are adjoint to each other.
-/
@[simps]
def extend_restrict_scalars_adj {R : Type u₁} {S : Type u₂} [comm_ring R] [comm_ring S]
(f : R →+* S) : extend_scalars f ⊣ restrict_scalars f :=
{ hom_equiv := λ _ _, extend_restrict_scalars_adj.hom_equiv f,
unit := extend_restrict_scalars_adj.unit f,
counit := extend_restrict_scalars_adj.counit f,
hom_equiv_unit' := λ X Y g, linear_map.ext $ λ x, by simp,
hom_equiv_counit' := λ X Y g, linear_map.ext $ λ x,
begin
induction x using tensor_product.induction_on,
{ simp only [map_zero]},
{ simp only [extend_restrict_scalars_adj.hom_equiv_symm_apply, linear_map.coe_mk,
extend_restrict_scalars_adj.hom_equiv.from_extend_scalars_apply, tensor_product.lift.tmul,
extend_restrict_scalars_adj.counit_app, Module.coe_comp, function.comp_app,
extend_scalars.map_tmul, extend_restrict_scalars_adj.counit.map_apply] },
{ simp only [map_add, *], }
end }
instance {R : Type u₁} {S : Type u₂} [comm_ring R] [comm_ring S] (f : R →+* S) :
category_theory.is_left_adjoint (extend_scalars f) := ⟨_, extend_restrict_scalars_adj f⟩
instance {R : Type u₁} {S : Type u₂} [comm_ring R] [comm_ring S] (f : R →+* S) :
category_theory.is_right_adjoint (restrict_scalars f) := ⟨_, extend_restrict_scalars_adj f⟩
end category_theory.Module
|
dc37c5ebc7404b454b3219151aea9441d3aaccde | 271e26e338b0c14544a889c31c30b39c989f2e0f | /stage0/src/Init/System/IO.lean | e21ba6f2fc4f1f4807975e889593e2cff4a0880b | [
"Apache-2.0"
] | permissive | dgorokho/lean4 | 805f99b0b60c545b64ac34ab8237a8504f89d7d4 | e949a052bad59b1c7b54a82d24d516a656487d8a | refs/heads/master | 1,607,061,363,851 | 1,578,006,086,000 | 1,578,006,086,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,568 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Luke Nelson, Jared Roesch, Leonardo de Moura, Sebastian Ullrich
-/
prelude
import Init.Control.EState
import Init.Data.String.Basic
import Init.System.FilePath
/-- Like https://hackage.haskell.org/package/ghc-Prim-0.5.2.0/docs/GHC-Prim.html#t:RealWorld.
Makes sure we never reorder `IO` operations.
TODO: mark opaque -/
def IO.RealWorld : Type := Unit
/- TODO(Leo): mark it as an opaque definition. Reason: prevent
functions defined in other modules from accessing `IO.RealWorld`.
We don't want action such as
```
def getWorld : IO (IO.RealWorld) := get
```
-/
def EIO (ε : Type) : Type → Type := EStateM ε IO.RealWorld
instance (ε : Type) : Monad (EIO ε) := inferInstanceAs (Monad (EStateM ε IO.RealWorld))
instance (ε : Type) : MonadExcept ε (EIO ε) := inferInstanceAs (MonadExcept ε (EStateM ε IO.RealWorld))
instance (α ε : Type) : HasOrelse (EIO ε α) := ⟨MonadExcept.orelse⟩
instance {ε : Type} {α : Type} [Inhabited ε] : Inhabited (EIO ε α) :=
inferInstanceAs (Inhabited (EStateM ε IO.RealWorld α))
/-
In the future, we may want to give more concrete data
like in https://doc.rust-lang.org/std/IO/enum.ErrorKind.html
-/
def IO.Error := String
instance : HasToString IO.Error := inferInstanceAs (HasToString String)
instance : Inhabited IO.Error := inferInstanceAs (Inhabited String)
def IO.userError (s : String) : IO.Error :=
s
@[export lean_io_error_to_string]
def IO.Error.toString : IO.Error → String :=
id
abbrev IO : Type → Type := EIO IO.Error
section
/- After we inline `EState.run'`, the closed term `((), ())` is generated, where the second `()`
represents the "initial world". We don't want to cache this closed term. So, we disable
the "extract closed terms" optimization. -/
set_option compiler.extract_closed false
@[inline] unsafe def unsafeIO {α : Type} (fn : IO α) : Except IO.Error α :=
match fn.run () with
| EStateM.Result.ok a _ => Except.ok a
| EStateM.Result.error e _ => Except.error e
end
@[extern "lean_io_timeit"]
constant timeit {α : Type} (msg : @& String) (fn : IO α) : IO α := arbitrary _
@[extern "lean_io_allocprof"]
constant allocprof {α : Type} (msg : @& String) (fn : IO α) : IO α := arbitrary _
/- Programs can execute IO actions during initialization that occurs before
the `main` function is executed. The attribute `[init <action>]` specifies
which IO action is executed to set the value of an opaque constant.
The action `initializing` returns `true` iff it is invoked during initialization. -/
@[extern "lean_io_initializing"]
constant IO.initializing : IO Bool := arbitrary _
abbrev MonadIO (m : Type → Type) := HasMonadLiftT IO m
namespace IO
def ofExcept {ε α : Type} [HasToString ε] (e : Except ε α) : IO α :=
match e with
| Except.ok a => pure a
| Except.error e => throw (IO.userError (toString e))
def lazyPure {α : Type} (fn : Unit → α) : IO α :=
pure (fn ())
inductive FS.Mode
| read | write | readWrite | append
constant FS.handle : Type := Unit
namespace Prim
open FS
@[specialize] partial def iterate {α β : Type} : α → (α → IO (Sum α β)) → IO β
| a, f =>
do v ← f a;
match v with
| Sum.inl a => iterate a f
| Sum.inr b => pure b
@[extern "lean_io_prim_put_str"]
constant putStr (s: @& String) : IO Unit := arbitrary _
@[extern "lean_io_prim_read_text_file"]
constant readTextFile (s : @& String) : IO String := arbitrary _
@[extern "lean_io_prim_get_line"]
constant getLine : IO String := arbitrary _
@[extern "lean_io_prim_handle_mk"]
constant handle.mk (s : @& String) (m : Mode) (bin : Bool := false) : IO handle := arbitrary _
@[extern "lean_io_prim_handle_is_eof"]
constant handle.isEof (h : @& handle) : IO Bool := arbitrary _
@[extern "lean_io_prim_handle_flush"]
constant handle.flush (h : @& handle) : IO Unit := arbitrary _
@[extern "lean_io_prim_handle_close"]
constant handle.close (h : @& handle) : IO Unit := arbitrary _
-- TODO: replace `String` with byte buffer
-- constant handle.read : handle → Nat → EIO String
-- constant handle.write : handle → String → EIO Unit
@[extern "lean_io_prim_handle_get_line"]
constant handle.getLine (h : @& handle) : IO String := arbitrary _
@[extern "lean_io_getenv"]
constant getEnv (var : @& String) : IO (Option String) := arbitrary _
@[extern "lean_io_realpath"]
constant realPath (fname : String) : IO String := arbitrary _
@[extern "lean_io_is_dir"]
constant isDir (fname : @& String) : IO Bool := arbitrary _
@[extern "lean_io_file_exists"]
constant fileExists (fname : @& String) : IO Bool := arbitrary _
@[extern "lean_io_app_dir"]
constant appPath : IO String := arbitrary _
@[inline] def liftIO {m : Type → Type} {α : Type} [MonadIO m] (x : IO α) : m α :=
monadLift x
end Prim
section
variables {m : Type → Type} [Monad m] [MonadIO m]
private def putStr : String → m Unit :=
Prim.liftIO ∘ Prim.putStr
def print {α} [HasToString α] (s : α) : m Unit := putStr ∘ toString $ s
def println {α} [HasToString α] (s : α) : m Unit := print s *> putStr "\n"
def readTextFile : String → m String := Prim.liftIO ∘ Prim.readTextFile
def getEnv : String → m (Option String) := Prim.liftIO ∘ Prim.getEnv
def realPath : String → m String := Prim.liftIO ∘ Prim.realPath
def isDir : String → m Bool := Prim.liftIO ∘ Prim.isDir
def fileExists : String → m Bool := Prim.liftIO ∘ Prim.fileExists
def appPath : m String := Prim.liftIO Prim.appPath
def appDir : m String := do
p ← appPath;
realPath (System.FilePath.dirName p)
end
namespace FS
variables {m : Type → Type} [Monad m] [MonadIO m]
def handle.mk (s : String) (Mode : Mode) (bin : Bool := false) : m handle := Prim.liftIO (Prim.handle.mk s Mode bin)
def handle.isEof : handle → m Bool := Prim.liftIO ∘ Prim.handle.isEof
def handle.flush : handle → m Unit := Prim.liftIO ∘ Prim.handle.flush
def handle.close : handle → m Unit := Prim.liftIO ∘ Prim.handle.flush
-- def handle.read (h : handle) (bytes : Nat) : m String := Prim.liftEIO (Prim.handle.read h bytes)
-- def handle.write (h : handle) (s : String) : m Unit := Prim.liftEIO (Prim.handle.write h s)
def handle.getLine : handle → m String := Prim.liftIO ∘ Prim.handle.getLine
/-
def getChar (h : handle) : m Char := do
b ← h.read 1,
if b.isEmpty then fail "getChar failed"
else pure b.mkIterator.curr
-/
-- def handle.putChar (h : handle) (c : Char) : m Unit :=
-- h.write (toString c)
-- def handle.putStr (h : handle) (s : String) : m Unit :=
-- h.write s
-- def handle.putStrLn (h : handle) (s : String) : m Unit :=
-- h.putStr s *> h.putStr "\n"
def handle.readToEnd (h : handle) : m String :=
Prim.liftIO $ Prim.iterate "" $ fun r => do
done ← h.isEof;
if done
then pure (Sum.inr r) -- stop
else do
-- HACK: use less efficient `getLine` while `read` is broken
c ← h.getLine;
pure $ Sum.inl (r ++ c) -- continue
def readFile (fname : String) (bin := false) : m String := do
h ← handle.mk fname Mode.read bin;
r ← h.readToEnd;
h.close;
pure r
end FS
-- constant stdin : IO FS.handle
-- constant stderr : IO FS.handle
-- constant stdout : IO FS.handle
/-
namespace Proc
def child : Type :=
MonadIOProcess.child ioCore
def child.stdin : child → handle :=
MonadIOProcess.stdin
def child.stdout : child → handle :=
MonadIOProcess.stdout
def child.stderr : child → handle :=
MonadIOProcess.stderr
def spawn (p : IO.process.spawnArgs) : IO child :=
MonadIOProcess.spawn ioCore p
def wait (c : child) : IO Nat :=
MonadIOProcess.wait c
end Proc
-/
/- References -/
constant RefPointed (α : Type) : PointedType := arbitrary _
def Ref (α : Type) : Type := (RefPointed α).type
instance (α : Type) : Inhabited (Ref α) := ⟨(RefPointed α).val⟩
namespace Prim
@[extern "lean_io_mk_ref"]
constant mkRef {α : Type} (a : α) : IO (Ref α) := arbitrary _
@[extern "lean_io_ref_get"]
constant Ref.get {α : Type} (r : @& Ref α) : IO α := arbitrary _
@[extern "lean_io_ref_set"]
constant Ref.set {α : Type} (r : @& Ref α) (a : α) : IO Unit := arbitrary _
@[extern "lean_io_ref_swap"]
constant Ref.swap {α : Type} (r : @& Ref α) (a : α) : IO α := arbitrary _
@[extern "lean_io_ref_reset"]
constant Ref.reset {α : Type} (r : @& Ref α) : IO Unit := arbitrary _
end Prim
section
variables {m : Type → Type} [Monad m] [MonadIO m]
@[inline] def mkRef {α : Type} (a : α) : m (Ref α) := Prim.liftIO (Prim.mkRef a)
@[inline] def Ref.get {α : Type} (r : Ref α) : m α := Prim.liftIO (Prim.Ref.get r)
@[inline] def Ref.set {α : Type} (r : Ref α) (a : α) : m Unit := Prim.liftIO (Prim.Ref.set r a)
@[inline] def Ref.swap {α : Type} (r : Ref α) (a : α) : m α := Prim.liftIO (Prim.Ref.swap r a)
@[inline] def Ref.reset {α : Type} (r : Ref α) : m Unit := Prim.liftIO (Prim.Ref.reset r)
@[inline] def Ref.modify {α : Type} (r : Ref α) (f : α → α) : m Unit := do
v ← r.get;
r.reset;
r.set (f v)
end
end IO
universe u
namespace Lean
/-- Typeclass used for presenting the output of an `#eval` command. -/
class HasEval (α : Type u) :=
(eval : α → IO Unit)
instance HasRepr.HasEval {α : Type u} [HasRepr α] : HasEval α :=
⟨fun a => IO.println (repr a)⟩
instance IO.HasEval {α : Type} [HasEval α] : HasEval (IO α) :=
⟨fun x => do a ← x; HasEval.eval a⟩
-- special case: do not print `()`
instance IOUnit.HasEval : HasEval (IO Unit) :=
⟨fun x => x⟩
end Lean
|
7465988a5497b02c6b12fec4d22810e826770c81 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/list/nodup_equiv_fin.lean | 9ea0ba982d781f3b610a95a23bf2f0d870959c2c | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 8,520 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import data.fin.basic
import data.list.sort
import data.list.duplicate
/-!
# Equivalence between `fin (length l)` and elements of a list
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
Given a list `l`,
* if `l` has no duplicates, then `list.nodup.nth_le_equiv` is the equivalence between
`fin (length l)` and `{x // x ∈ l}` sending `⟨i, hi⟩` to `⟨nth_le l i hi, _⟩` with the inverse
sending `⟨x, hx⟩` to `⟨index_of x l, _⟩`;
* if `l` has no duplicates and contains every element of a type `α`, then
`list.nodup.nth_le_equiv_of_forall_mem_list` defines an equivalence between
`fin (length l)` and `α`; if `α` does not have decidable equality, then
there is a bijection `list.nodup.nth_le_bijection_of_forall_mem_list`;
* if `l` is sorted w.r.t. `(<)`, then `list.sorted.nth_le_iso` is the same bijection reinterpreted
as an `order_iso`.
-/
namespace list
variable {α : Type*}
namespace nodup
/-- If `l` lists all the elements of `α` without duplicates, then `list.nth_le` defines
a bijection `fin l.length → α`. See `list.nodup.nth_le_equiv_of_forall_mem_list`
for a version giving an equivalence when there is decidable equality. -/
@[simps]
def nth_le_bijection_of_forall_mem_list (l : list α) (nd : l.nodup) (h : ∀ (x : α), x ∈ l) :
{f : fin l.length → α // function.bijective f} :=
⟨λ i, l.nth_le i i.property, λ i j h, fin.ext $ (nd.nth_le_inj_iff _ _).1 h,
λ x, let ⟨i, hi, hl⟩ := list.mem_iff_nth_le.1 (h x) in ⟨⟨i, hi⟩, hl⟩⟩
variable [decidable_eq α]
/-- If `l` has no duplicates, then `list.nth_le` defines an equivalence between `fin (length l)` and
the set of elements of `l`. -/
@[simps]
def nth_le_equiv (l : list α) (H : nodup l) : fin (length l) ≃ {x // x ∈ l} :=
{ to_fun := λ i, ⟨nth_le l i i.2, nth_le_mem l i i.2⟩,
inv_fun := λ x, ⟨index_of ↑x l, index_of_lt_length.2 x.2⟩,
left_inv := λ i, by simp [H],
right_inv := λ x, by simp }
/-- If `l` lists all the elements of `α` without duplicates, then `list.nth_le` defines
an equivalence between `fin l.length` and `α`.
See `list.nodup.nth_le_bijection_of_forall_mem_list` for a version without
decidable equality. -/
@[simps]
def nth_le_equiv_of_forall_mem_list (l : list α) (nd : l.nodup) (h : ∀ (x : α), x ∈ l) :
fin l.length ≃ α :=
{ to_fun := λ i, l.nth_le i i.2,
inv_fun := λ a, ⟨_, index_of_lt_length.2 (h a)⟩,
left_inv := λ i, by simp [nd],
right_inv := λ a, by simp }
end nodup
namespace sorted
variables [preorder α] {l : list α}
lemma nth_le_mono (h : l.sorted (≤)) :
monotone (λ i : fin l.length, l.nth_le i i.2) :=
λ i j, h.rel_nth_le_of_le _ _
lemma nth_le_strict_mono (h : l.sorted (<)) :
strict_mono (λ i : fin l.length, l.nth_le i i.2) :=
λ i j, h.rel_nth_le_of_lt _ _
variable [decidable_eq α]
/-- If `l` is a list sorted w.r.t. `(<)`, then `list.nth_le` defines an order isomorphism between
`fin (length l)` and the set of elements of `l`. -/
def nth_le_iso (l : list α) (H : sorted (<) l) : fin (length l) ≃o {x // x ∈ l} :=
{ to_equiv := H.nodup.nth_le_equiv l,
map_rel_iff' := λ i j, H.nth_le_strict_mono.le_iff_le }
variables (H : sorted (<) l) {x : {x // x ∈ l}} {i : fin l.length}
@[simp] lemma coe_nth_le_iso_apply : (H.nth_le_iso l i : α) = nth_le l i i.2 := rfl
@[simp] lemma coe_nth_le_iso_symm_apply : ((H.nth_le_iso l).symm x : ℕ) = index_of ↑x l := rfl
end sorted
section sublist
/--
If there is `f`, an order-preserving embedding of `ℕ` into `ℕ` such that
any element of `l` found at index `ix` can be found at index `f ix` in `l'`,
then `sublist l l'`.
-/
lemma sublist_of_order_embedding_nth_eq {l l' : list α} (f : ℕ ↪o ℕ)
(hf : ∀ (ix : ℕ), l.nth ix = l'.nth (f ix)) :
l <+ l' :=
begin
induction l with hd tl IH generalizing l' f,
{ simp },
have : some hd = _ := hf 0,
rw [eq_comm, list.nth_eq_some] at this,
obtain ⟨w, h⟩ := this,
let f' : ℕ ↪o ℕ := order_embedding.of_map_le_iff (λ i, f (i + 1) - (f 0 + 1))
(λ a b, by simp [tsub_le_tsub_iff_right, nat.succ_le_iff, nat.lt_succ_iff]),
have : ∀ ix, tl.nth ix = (l'.drop (f 0 + 1)).nth (f' ix),
{ intro ix,
simp [list.nth_drop, add_tsub_cancel_of_le, nat.succ_le_iff, ←hf] },
rw [←list.take_append_drop (f 0 + 1) l', ←list.singleton_append],
apply list.sublist.append _ (IH _ this),
rw [list.singleton_sublist, ←h, l'.nth_le_take _ (nat.lt_succ_self _)],
apply list.nth_le_mem
end
/--
A `l : list α` is `sublist l l'` for `l' : list α` iff
there is `f`, an order-preserving embedding of `ℕ` into `ℕ` such that
any element of `l` found at index `ix` can be found at index `f ix` in `l'`.
-/
lemma sublist_iff_exists_order_embedding_nth_eq {l l' : list α} :
l <+ l' ↔ ∃ (f : ℕ ↪o ℕ), ∀ (ix : ℕ), l.nth ix = l'.nth (f ix) :=
begin
split,
{ intro H,
induction H with xs ys y H IH xs ys x H IH,
{ simp },
{ obtain ⟨f, hf⟩ := IH,
refine ⟨f.trans (order_embedding.of_strict_mono (+ 1) (λ _, by simp)), _⟩,
simpa using hf },
{ obtain ⟨f, hf⟩ := IH,
refine ⟨order_embedding.of_map_le_iff
(λ (ix : ℕ), if ix = 0 then 0 else (f ix.pred).succ) _, _⟩,
{ rintro ⟨_|a⟩ ⟨_|b⟩;
simp [nat.succ_le_succ_iff] },
{ rintro ⟨_|i⟩,
{ simp },
{ simpa using hf _ } } } },
{ rintro ⟨f, hf⟩,
exact sublist_of_order_embedding_nth_eq f hf }
end
/--
A `l : list α` is `sublist l l'` for `l' : list α` iff
there is `f`, an order-preserving embedding of `fin l.length` into `fin l'.length` such that
any element of `l` found at index `ix` can be found at index `f ix` in `l'`.
-/
lemma sublist_iff_exists_fin_order_embedding_nth_le_eq {l l' : list α} :
l <+ l' ↔ ∃ (f : fin l.length ↪o fin l'.length),
∀ (ix : fin l.length), l.nth_le ix ix.is_lt = l'.nth_le (f ix) (f ix).is_lt :=
begin
rw sublist_iff_exists_order_embedding_nth_eq,
split,
{ rintro ⟨f, hf⟩,
have h : ∀ {i : ℕ} (h : i < l.length), f i < l'.length,
{ intros i hi,
specialize hf i,
rw [nth_le_nth hi, eq_comm, nth_eq_some] at hf,
obtain ⟨h, -⟩ := hf,
exact h },
refine ⟨order_embedding.of_map_le_iff (λ ix, ⟨f ix, h ix.is_lt⟩) _, _⟩,
{ simp },
{ intro i,
apply option.some_injective,
simpa [←nth_le_nth] using hf _ } },
{ rintro ⟨f, hf⟩,
refine ⟨order_embedding.of_strict_mono
(λ i, if hi : i < l.length then f ⟨i, hi⟩ else i + l'.length) _, _⟩,
{ intros i j h,
dsimp only,
split_ifs with hi hj hj hi,
{ simpa using h },
{ rw add_comm,
exact lt_add_of_lt_of_pos (fin.is_lt _) (i.zero_le.trans_lt h) },
{ exact absurd (h.trans hj) hi },
{ simpa using h } },
{ intro i,
simp only [order_embedding.coe_of_strict_mono],
split_ifs with hi,
{ rw [nth_le_nth hi, nth_le_nth, ←hf],
simp },
{ rw [nth_len_le, nth_len_le],
{ simp },
{ simpa using hi } } } }
end
/--
An element `x : α` of `l : list α` is a duplicate iff it can be found
at two distinct indices `n m : ℕ` inside the list `l`.
-/
lemma duplicate_iff_exists_distinct_nth_le {l : list α} {x : α} :
l.duplicate x ↔ ∃ (n : ℕ) (hn : n < l.length) (m : ℕ) (hm : m < l.length) (h : n < m),
x = l.nth_le n hn ∧ x = l.nth_le m hm :=
begin
classical,
rw [duplicate_iff_two_le_count, le_count_iff_replicate_sublist,
sublist_iff_exists_fin_order_embedding_nth_le_eq],
split,
{ rintro ⟨f, hf⟩,
refine ⟨f ⟨0, by simp⟩, fin.is_lt _, f ⟨1, by simp⟩, fin.is_lt _, by simp, _, _⟩,
{ simpa using hf ⟨0, by simp⟩ },
{ simpa using hf ⟨1, by simp⟩ } },
{ rintro ⟨n, hn, m, hm, hnm, h, h'⟩,
refine ⟨order_embedding.of_strict_mono (λ i, if (i : ℕ) = 0 then ⟨n, hn⟩ else ⟨m, hm⟩) _, _⟩,
{ rintros ⟨⟨_|i⟩, hi⟩ ⟨⟨_|j⟩, hj⟩,
{ simp },
{ simp [hnm] },
{ simp },
{ simp only [nat.lt_succ_iff, nat.succ_le_succ_iff, replicate, length, nonpos_iff_eq_zero]
at hi hj,
simp [hi, hj] } },
{ rintros ⟨⟨_|i⟩, hi⟩,
{ simpa using h },
{ simpa using h' } } }
end
end sublist
end list
|
40dd83538762fc0e3130e84487149d788a62904c | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/combinatorics/derangements/basic.lean | 6d0cce36db9ec479d4073cd961c8a5cc0b8994f4 | [
"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 | 8,321 | lean | /-
Copyright (c) 2021 Henry Swanson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Henry Swanson
-/
import dynamics.fixed_points.basic
import group_theory.perm.option
import logic.equiv.defs
import logic.equiv.option
/-!
# Derangements on types
In this file we define `derangements α`, the set of derangements on a type `α`.
We also define some equivalences involving various subtypes of `perm α` and `derangements α`:
* `derangements_option_equiv_sigma_at_most_one_fixed_point`: An equivalence between
`derangements (option α)` and the sigma-type `Σ a : α, {f : perm α // fixed_points f ⊆ a}`.
* `derangements_recursion_equiv`: An equivalence between `derangements (option α)` and the
sigma-type `Σ a : α, (derangements (({a}ᶜ : set α) : Type _) ⊕ derangements α)` which is later
used to inductively count the number of derangements.
In order to prove the above, we also prove some results about the effect of `equiv.remove_none`
on derangements: `remove_none.fiber_none` and `remove_none.fiber_some`.
-/
open equiv function
/-- A permutation is a derangement if it has no fixed points. -/
def derangements (α : Type*) : set (perm α) := {f : perm α | ∀ x : α, f x ≠ x}
variables {α β : Type*}
lemma mem_derangements_iff_fixed_points_eq_empty {f : perm α} :
f ∈ derangements α ↔ fixed_points f = ∅ :=
set.eq_empty_iff_forall_not_mem.symm
/-- If `α` is equivalent to `β`, then `derangements α` is equivalent to `derangements β`. -/
def equiv.derangements_congr (e : α ≃ β) : (derangements α ≃ derangements β) :=
e.perm_congr.subtype_equiv $ λ f, e.forall_congr $ by simp
namespace derangements
/-- Derangements on a subtype are equivalent to permutations on the original type where points are
fixed iff they are not in the subtype. -/
protected def subtype_equiv (p : α → Prop) [decidable_pred p] :
derangements (subtype p) ≃ {f : perm α // ∀ a, ¬p a ↔ a ∈ fixed_points f} :=
calc
derangements (subtype p)
≃ {f : {f : perm α // ∀ a, ¬p a → a ∈ fixed_points f} // ∀ a, a ∈ fixed_points f → ¬p a}
: begin
refine (perm.subtype_equiv_subtype_perm p).subtype_equiv (λ f, ⟨λ hf a hfa ha, _, _⟩),
{ refine hf ⟨a, ha⟩ (subtype.ext _),
rwa [mem_fixed_points, is_fixed_pt, perm.subtype_equiv_subtype_perm, @coe_fn_coe_base',
equiv.coe_fn_mk, subtype.coe_mk, equiv.perm.of_subtype_apply_of_mem]
at hfa },
rintro hf ⟨a, ha⟩ hfa,
refine hf _ _ ha,
change perm.subtype_equiv_subtype_perm p f a = a,
rw [perm.subtype_equiv_subtype_perm_apply_of_mem f ha, hfa, subtype.coe_mk],
end
... ≃ {f : perm α // ∃ (h : ∀ a, ¬p a → a ∈ fixed_points f), ∀ a, a ∈ fixed_points f → ¬p a}
: subtype_subtype_equiv_subtype_exists _ _
... ≃ {f : perm α // ∀ a, ¬p a ↔ a ∈ fixed_points f}
: subtype_equiv_right (λ f, by simp_rw [exists_prop, ←forall_and_distrib,
←iff_iff_implies_and_implies])
/-- The set of permutations that fix either `a` or nothing is equivalent to the sum of:
- derangements on `α`
- derangements on `α` minus `a`. -/
def at_most_one_fixed_point_equiv_sum_derangements [decidable_eq α] (a : α) :
{f : perm α // fixed_points f ⊆ {a}} ≃ (derangements ({a}ᶜ : set α)) ⊕ derangements α :=
calc
{f : perm α // fixed_points f ⊆ {a}}
≃ {f : {f : perm α // fixed_points f ⊆ {a}} // a ∈ fixed_points f}
⊕ {f : {f : perm α // fixed_points f ⊆ {a}} // a ∉ fixed_points f}
: (equiv.sum_compl _).symm
... ≃ {f : perm α // fixed_points f ⊆ {a} ∧ a ∈ fixed_points f}
⊕ {f : perm α // fixed_points f ⊆ {a} ∧ a ∉ fixed_points f}
: begin
refine equiv.sum_congr _ _;
{ convert subtype_subtype_equiv_subtype_inter _ _, ext f, refl }
end
... ≃ {f : perm α // fixed_points f = {a}} ⊕ {f : perm α // fixed_points f = ∅}
: begin
refine equiv.sum_congr (subtype_equiv_right $ λ f, _) (subtype_equiv_right $ λ f, _),
{ rw [set.eq_singleton_iff_unique_mem, and_comm],
refl },
{ rw set.eq_empty_iff_forall_not_mem,
refine ⟨λ h x hx, h.2 (h.1 hx ▸ hx), λ h, ⟨λ x hx, (h _ hx).elim, h _⟩⟩ }
end
... ≃ (derangements ({a}ᶜ : set α)) ⊕ derangements α
: begin
refine equiv.sum_congr ((derangements.subtype_equiv _).trans $ subtype_equiv_right $ λ x,
_).symm (subtype_equiv_right $ λ f, mem_derangements_iff_fixed_points_eq_empty.symm),
rw [eq_comm, set.ext_iff],
simp_rw [set.mem_compl_iff, not_not],
end
namespace equiv
variables [decidable_eq α]
/-- The set of permutations `f` such that the preimage of `(a, f)` under
`equiv.perm.decompose_option` is a derangement. -/
def remove_none.fiber (a : option α) : set (perm α) :=
{f : perm α | (a, f) ∈ equiv.perm.decompose_option '' derangements (option α)}
lemma remove_none.mem_fiber (a : option α) (f : perm α) :
f ∈ remove_none.fiber a ↔
∃ F : perm (option α), F ∈ derangements (option α) ∧ F none = a ∧ remove_none F = f :=
by simp [remove_none.fiber, derangements]
lemma remove_none.fiber_none : remove_none.fiber (@none α) = ∅ :=
begin
rw set.eq_empty_iff_forall_not_mem,
intros f hyp,
rw remove_none.mem_fiber at hyp,
rcases hyp with ⟨F, F_derangement, F_none, _⟩,
exact F_derangement none F_none
end
/-- For any `a : α`, the fiber over `some a` is the set of permutations
where `a` is the only possible fixed point. -/
lemma remove_none.fiber_some (a : α) :
(remove_none.fiber (some a)) = {f : perm α | fixed_points f ⊆ {a}} :=
begin
ext f,
split,
{ rw remove_none.mem_fiber,
rintro ⟨F, F_derangement, F_none, rfl⟩ x x_fixed,
rw mem_fixed_points_iff at x_fixed,
apply_fun some at x_fixed,
cases Fx : F (some x) with y,
{ rwa [remove_none_none F Fx, F_none, option.some_inj, eq_comm] at x_fixed },
{ exfalso, rw remove_none_some F ⟨y, Fx⟩ at x_fixed, exact F_derangement _ x_fixed } },
{ intro h_opfp,
use equiv.perm.decompose_option.symm (some a, f),
split,
{ intro x,
apply_fun (swap none (some a)),
simp only [perm.decompose_option_symm_apply, swap_apply_self, perm.coe_mul],
cases x,
{ simp },
simp only [equiv.option_congr_apply, option.map_some'],
by_cases x_vs_a : x = a,
{ rw [x_vs_a, swap_apply_right], apply option.some_ne_none },
have ne_1 : some x ≠ none := option.some_ne_none _,
have ne_2 : some x ≠ some a := (option.some_injective α).ne_iff.mpr x_vs_a,
rw [swap_apply_of_ne_of_ne ne_1 ne_2, (option.some_injective α).ne_iff],
intro contra,
exact x_vs_a (h_opfp contra) },
{ rw apply_symm_apply } }
end
end equiv
section option
variables [decidable_eq α]
/-- The set of derangements on `option α` is equivalent to the union over `a : α`
of "permutations with `a` the only possible fixed point". -/
def derangements_option_equiv_sigma_at_most_one_fixed_point :
derangements (option α) ≃ Σ a : α, {f : perm α | fixed_points f ⊆ {a}} :=
begin
have fiber_none_is_false : (equiv.remove_none.fiber (@none α)) -> false,
{ rw equiv.remove_none.fiber_none, exact is_empty.false },
calc derangements (option α)
≃ equiv.perm.decompose_option '' derangements (option α) : equiv.image _ _
... ≃ Σ (a : option α), ↥(equiv.remove_none.fiber a) : set_prod_equiv_sigma _
... ≃ Σ (a : α), ↥(equiv.remove_none.fiber (some a))
: sigma_option_equiv_of_some _ fiber_none_is_false
... ≃ Σ (a : α), {f : perm α | fixed_points f ⊆ {a}}
: by simp_rw equiv.remove_none.fiber_some,
end
/-- The set of derangements on `option α` is equivalent to the union over all `a : α` of
"derangements on `α` ⊕ derangements on `{a}ᶜ`". -/
def derangements_recursion_equiv :
derangements (option α) ≃ Σ a : α, (derangements (({a}ᶜ : set α) : Type _) ⊕ derangements α) :=
derangements_option_equiv_sigma_at_most_one_fixed_point.trans (sigma_congr_right
at_most_one_fixed_point_equiv_sum_derangements)
end option
end derangements
|
aebbb33cfcfe08d70b2c70c4fb68d8a1bb5da759 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/doNotation3.lean | ccccf9151b49623f3005627dc583ef7fa1cf768d | [
"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 | 2,685 | lean | theorem zero_lt_of_lt : {a b : Nat} → a < b → 0 < b
| 0, _, h => h
| a+1, b, h =>
have : a < b := Nat.lt_trans (Nat.lt_succ_self _) h
zero_lt_of_lt this
def fold {m α β} [Monad m] (as : Array α) (b : β) (f : α → β → m β) : m β := do
let rec loop : (i : Nat) → i ≤ as.size → β → m β
| 0, h, b => pure b
| i+1, h, b => do
have h' : i < as.size := Nat.lt_of_lt_of_le (Nat.lt_succ_self i) h
have : as.size - 1 < as.size := Nat.sub_lt (zero_lt_of_lt h') (by decide)
have : as.size - 1 - i < as.size := Nat.lt_of_le_of_lt (Nat.sub_le (as.size - 1) i) this
let b ← f (as.get ⟨as.size - 1 - i, this⟩) b
loop i (Nat.le_of_lt h') b
loop as.size (Nat.le_refl _) b
#eval Id.run $ fold #[1, 2, 3, 4] 0 (pure $ · + ·)
theorem ex : (Id.run $ fold #[1, 2, 3, 4] 0 (pure $ · + ·)) = 10 :=
rfl
def fold2 {m α β} [Monad m] (as : Array α) (b : β) (f : α → β → m β) : m β :=
let rec loop (i : Nat) (h : i ≤ as.size) (b : β) : m β := do
match i, h with
| 0, h => return b
| i+1, h =>
have h' : i < as.size := Nat.lt_of_lt_of_le (Nat.lt_succ_self i) h
have : as.size - 1 < as.size := Nat.sub_lt (zero_lt_of_lt h') (by decide)
have : as.size - 1 - i < as.size := Nat.lt_of_le_of_lt (Nat.sub_le (as.size - 1) i) this
let b ← f (as.get ⟨as.size - 1 - i, this⟩) b
loop i (Nat.le_of_lt h') b
loop as.size (Nat.le_refl _) b
def f (x : Nat) (ref : IO.Ref Nat) : IO Nat := do
let mut x := x
if x == 0 then
x ← ref.get
IO.println x
return x + 1
def fTest : IO Unit := do
unless (← f 0 (← IO.mkRef 10)) == 11 do throw $ IO.userError "unexpected"
unless (← f 1 (← IO.mkRef 10)) == 2 do throw $ IO.userError "unexpected"
def g (x y : Nat) (ref : IO.Ref (Nat × Nat)) : IO (Nat × Nat) := do
let mut (x, y) := (x, y)
if x == 0 then
(x, y) ← ref.get
IO.println ("x: " ++ toString x ++ ", y: " ++ toString y)
return (x, y)
def gTest : IO Unit := do
unless (← g 2 1 (← IO.mkRef (10, 20))) == (2, 1) do throw $ IO.userError "unexpected"
unless (← g 0 1 (← IO.mkRef (10, 20))) == (10, 20) do throw $ IO.userError "unexpected"
return ()
#eval gTest
macro "ret!" x:term : doElem => `(doElem| return $x)
def f1 (x : Nat) : Nat := Id.run <| do
let mut x := x
if x == 0 then
ret! 100
x := x + 1
ret! x
theorem ex1 : f1 0 = 100 := rfl
theorem ex2 : f1 1 = 2 := rfl
theorem ex3 : f1 3 = 4 := rfl
syntax "inc!" ident : doElem
macro_rules
| `(doElem| inc! $x) => `(doElem| $x:ident := $x + 1)
def f2 (x : Nat) : Nat := Id.run <| do
let mut x := x
inc! x
ret! x
theorem ex4 : f2 0 = 1 := rfl
theorem ex5 : f2 3 = 4 := rfl
|
e9765857946b184c58894784f832a2066dde6ff4 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/geometry/manifold/local_invariant_properties_auto.lean | 0cdb83efe842e54182b0b01bc1a644f9e1d4e96e | [] | 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 | 28,535 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.geometry.manifold.smooth_manifold_with_corners
import Mathlib.PostPort
universes u_1 u_3 l u_2 u_4
namespace Mathlib
/-!
# Local properties invariant under a groupoid
We study properties of a triple `(g, s, x)` where `g` is a function between two spaces `H` and `H'`,
`s` is a subset of `H` and `x` is a point of `H`. Our goal is to register how such a property
should behave to make sense in charted spaces modelled on `H` and `H'`.
The main examples we have in mind are the properties "`g` is differentiable at `x` within `s`", or
"`g` is smooth at `x` within `s`". We want to develop general results that, when applied in these
specific situations, say that the notion of smooth function in a manifold behaves well under
restriction, intersection, is local, and so on.
## Main definitions
* `local_invariant_prop G G' P` says that a property `P` of a triple `(g, s, x)` is local, and
invariant under composition by elements of the groupoids `G` and `G'` of `H` and `H'`
respectively.
* `charted_space.lift_prop_within_at` (resp. `lift_prop_at`, `lift_prop_on` and `lift_prop`):
given a property `P` of `(g, s, x)` where `g : H → H'`, define the corresponding property
for functions `M → M'` where `M` and `M'` are charted spaces modelled respectively on `H` and
`H'`. We define these properties within a set at a point, or at a point, or on a set, or in the
whole space. This lifting process (obtained by restricting to suitable chart domains) can always
be done, but it only behaves well under locality and invariance assumptions.
Given `hG : local_invariant_prop G G' P`, we deduce many properties of the lifted property on the
charted spaces. For instance, `hG.lift_prop_within_at_inter` says that `P g s x` is equivalent to
`P g (s ∩ t) x` whenever `t` is a neighborhood of `x`.
## Implementation notes
We do not use dot notation for properties of the lifted property. For instance, we have
`hG.lift_prop_within_at_congr` saying that if `lift_prop_within_at P g s x` holds, and `g` and `g'`
coincide on `s`, then `lift_prop_within_at P g' s x` holds. We can't call it
`lift_prop_within_at.congr` as it is in the namespace associated to `local_invariant_prop`, not
in the one for `lift_prop_within_at`.
-/
namespace structure_groupoid
/-- Structure recording good behavior of a property of a triple `(f, s, x)` where `f` is a function,
`s` a set and `x` a point. Good behavior here means locality and invariance under given groupoids
(both in the source and in the target). Given such a good behavior, the lift of this property
to charted spaces admitting these groupoids will inherit the good behavior. -/
structure local_invariant_prop {H : Type u_1} [topological_space H] {H' : Type u_3}
[topological_space H'] (G : structure_groupoid H) (G' : structure_groupoid H')
(P : (H → H') → set H → H → Prop)
where
is_local :
∀ {s : set H} {x : H} {u : set H} {f : H → H'}, is_open u → x ∈ u → (P f s x ↔ P f (s ∩ u) x)
right_invariance :
∀ {s : set H} {x : H} {f : H → H'} {e : local_homeomorph H H},
e ∈ G →
x ∈ local_equiv.source (local_homeomorph.to_local_equiv e) →
P f s x →
P (f ∘ ⇑(local_homeomorph.symm e))
(local_equiv.target (local_homeomorph.to_local_equiv e) ∩
⇑(local_homeomorph.symm e) ⁻¹' s)
(coe_fn e x)
congr :
∀ {s : set H} {x : H} {f g : H → H'},
(∀ (y : H), y ∈ s → f y = g y) → f x = g x → P f s x → P g s x
left_invariance :
∀ {s : set H} {x : H} {f : H → H'} {e' : local_homeomorph H' H'},
e' ∈ G' →
s ⊆ f ⁻¹' local_equiv.source (local_homeomorph.to_local_equiv e') →
f x ∈ local_equiv.source (local_homeomorph.to_local_equiv e') → P f s x → P (⇑e' ∘ f) s x
end structure_groupoid
/-- Given a property of germs of functions and sets in the model space, then one defines
a corresponding property in a charted space, by requiring that it holds at the preferred chart at
this point. (When the property is local and invariant, it will in fact hold using any chart, see
`lift_prop_within_at_indep_chart`). We require continuity in the lifted property, as otherwise one
single chart might fail to capture the behavior of the function.
-/
def charted_space.lift_prop_within_at {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] (P : (H → H') → set H → H → Prop) (f : M → M')
(s : set M) (x : M) :=
continuous_within_at f s x ∧
P
(⇑(charted_space.chart_at H' (f x)) ∘
f ∘ ⇑(local_homeomorph.symm (charted_space.chart_at H x)))
(local_equiv.target (local_homeomorph.to_local_equiv (charted_space.chart_at H x)) ∩
⇑(local_homeomorph.symm (charted_space.chart_at H x)) ⁻¹'
(s ∩
f ⁻¹'
local_equiv.source
(local_homeomorph.to_local_equiv (charted_space.chart_at H' (f x)))))
(coe_fn (charted_space.chart_at H x) x)
/-- Given a property of germs of functions and sets in the model space, then one defines
a corresponding property of functions on sets in a charted space, by requiring that it holds
around each point of the set, in the preferred charts. -/
def charted_space.lift_prop_on {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] (P : (H → H') → set H → H → Prop) (f : M → M')
(s : set M) :=
∀ (x : M), x ∈ s → charted_space.lift_prop_within_at P f s x
/-- Given a property of germs of functions and sets in the model space, then one defines
a corresponding property of a function at a point in a charted space, by requiring that it holds
in the preferred chart. -/
def charted_space.lift_prop_at {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] (P : (H → H') → set H → H → Prop) (f : M → M')
(x : M) :=
charted_space.lift_prop_within_at P f set.univ x
/-- Given a property of germs of functions and sets in the model space, then one defines
a corresponding property of a function in a charted space, by requiring that it holds
in the preferred chart around every point. -/
def charted_space.lift_prop {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] (P : (H → H') → set H → H → Prop) (f : M → M') :=
∀ (x : M), charted_space.lift_prop_at P f x
namespace structure_groupoid
theorem lift_prop_within_at_univ {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {P : (H → H') → set H → H → Prop} {g : M → M'}
{x : M} : charted_space.lift_prop_within_at P g set.univ x ↔ charted_space.lift_prop_at P g x :=
iff.rfl
theorem lift_prop_on_univ {H : Type u_1} {M : Type u_2} [topological_space H] [topological_space M]
[charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {P : (H → H') → set H → H → Prop} {g : M → M'} :
charted_space.lift_prop_on P g set.univ ↔ charted_space.lift_prop P g :=
sorry
namespace local_invariant_prop
/-- If a property of a germ of function `g` on a pointed set `(s, x)` is invariant under the
structure groupoid (by composition in the source space and in the target space), then
expressing it in charted spaces does not depend on the element of the maximal atlas one uses
both in the source and in the target manifolds, provided they are defined around `x` and `g x`
respectively, and provided `g` is continuous within `s` at `x` (otherwise, the local behavior
of `g` at `x` can not be captured with a chart in the target). -/
theorem lift_prop_within_at_indep_chart_aux {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {e : local_homeomorph M H} {e' : local_homeomorph M H}
{f : local_homeomorph M' H'} {f' : local_homeomorph M' H'} {P : (H → H') → set H → H → Prop}
{g : M → M'} {s : set M} {x : M} (hG : local_invariant_prop G G' P) (he : e ∈ maximal_atlas M G)
(xe : x ∈ local_equiv.source (local_homeomorph.to_local_equiv e)) (he' : e' ∈ maximal_atlas M G)
(xe' : x ∈ local_equiv.source (local_homeomorph.to_local_equiv e'))
(hf : f ∈ maximal_atlas M' G')
(xf : g x ∈ local_equiv.source (local_homeomorph.to_local_equiv f))
(hf' : f' ∈ maximal_atlas M' G')
(xf' : g x ∈ local_equiv.source (local_homeomorph.to_local_equiv f'))
(hgs : continuous_within_at g s x)
(h :
P (⇑f ∘ g ∘ ⇑(local_homeomorph.symm e))
(local_equiv.target (local_homeomorph.to_local_equiv e) ∩
⇑(local_homeomorph.symm e) ⁻¹'
(s ∩ g ⁻¹' local_equiv.source (local_homeomorph.to_local_equiv f)))
(coe_fn e x)) :
P (⇑f' ∘ g ∘ ⇑(local_homeomorph.symm e'))
(local_equiv.target (local_homeomorph.to_local_equiv e') ∩
⇑(local_homeomorph.symm e') ⁻¹'
(s ∩ g ⁻¹' local_equiv.source (local_homeomorph.to_local_equiv f')))
(coe_fn e' x) :=
sorry
theorem lift_prop_within_at_indep_chart {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {e : local_homeomorph M H} {f : local_homeomorph M' H'}
{P : (H → H') → set H → H → Prop} {g : M → M'} {s : set M} {x : M}
(hG : local_invariant_prop G G' P) [has_groupoid M G] [has_groupoid M' G']
(he : e ∈ maximal_atlas M G) (xe : x ∈ local_equiv.source (local_homeomorph.to_local_equiv e))
(hf : f ∈ maximal_atlas M' G')
(xf : g x ∈ local_equiv.source (local_homeomorph.to_local_equiv f)) :
charted_space.lift_prop_within_at P g s x ↔
continuous_within_at g s x ∧
P (⇑f ∘ g ∘ ⇑(local_homeomorph.symm e))
(local_equiv.target (local_homeomorph.to_local_equiv e) ∩
⇑(local_homeomorph.symm e) ⁻¹'
(s ∩ g ⁻¹' local_equiv.source (local_homeomorph.to_local_equiv f)))
(coe_fn e x) :=
sorry
theorem lift_prop_on_indep_chart {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {e : local_homeomorph M H} {f : local_homeomorph M' H'}
{P : (H → H') → set H → H → Prop} {g : M → M'} {s : set M} (hG : local_invariant_prop G G' P)
[has_groupoid M G] [has_groupoid M' G'] (he : e ∈ maximal_atlas M G)
(hf : f ∈ maximal_atlas M' G') (h : charted_space.lift_prop_on P g s) (y : H) :
y ∈
local_equiv.target (local_homeomorph.to_local_equiv e) ∩
⇑(local_homeomorph.symm e) ⁻¹'
(s ∩ g ⁻¹' local_equiv.source (local_homeomorph.to_local_equiv f)) →
P (⇑f ∘ g ∘ ⇑(local_homeomorph.symm e))
(local_equiv.target (local_homeomorph.to_local_equiv e) ∩
⇑(local_homeomorph.symm e) ⁻¹'
(s ∩ g ⁻¹' local_equiv.source (local_homeomorph.to_local_equiv f)))
y :=
sorry
theorem lift_prop_within_at_inter' {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {P : (H → H') → set H → H → Prop} {g : M → M'} {s : set M}
{t : set M} {x : M} (hG : local_invariant_prop G G' P) (ht : t ∈ nhds_within x s) :
charted_space.lift_prop_within_at P g (s ∩ t) x ↔ charted_space.lift_prop_within_at P g s x :=
sorry
theorem lift_prop_within_at_inter {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {P : (H → H') → set H → H → Prop} {g : M → M'} {s : set M}
{t : set M} {x : M} (hG : local_invariant_prop G G' P) (ht : t ∈ nhds x) :
charted_space.lift_prop_within_at P g (s ∩ t) x ↔ charted_space.lift_prop_within_at P g s x :=
lift_prop_within_at_inter' hG (mem_nhds_within_of_mem_nhds ht)
theorem lift_prop_at_of_lift_prop_within_at {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {P : (H → H') → set H → H → Prop} {g : M → M'} {s : set M} {x : M}
(hG : local_invariant_prop G G' P) (h : charted_space.lift_prop_within_at P g s x)
(hs : s ∈ nhds x) : charted_space.lift_prop_at P g x :=
sorry
theorem lift_prop_within_at_of_lift_prop_at_of_mem_nhds {H : Type u_1} {M : Type u_2}
[topological_space H] [topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4}
[topological_space H'] [topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {P : (H → H') → set H → H → Prop} {g : M → M'} {s : set M} {x : M}
(hG : local_invariant_prop G G' P) (h : charted_space.lift_prop_at P g x) (hs : s ∈ nhds x) :
charted_space.lift_prop_within_at P g s x :=
sorry
theorem lift_prop_on_of_locally_lift_prop_on {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {P : (H → H') → set H → H → Prop} {g : M → M'} {s : set M}
(hG : local_invariant_prop G G' P)
(h :
∀ (x : M),
x ∈ s → ∃ (u : set M), is_open u ∧ x ∈ u ∧ charted_space.lift_prop_on P g (s ∩ u)) :
charted_space.lift_prop_on P g s :=
sorry
theorem lift_prop_of_locally_lift_prop_on {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {P : (H → H') → set H → H → Prop} {g : M → M'}
(hG : local_invariant_prop G G' P)
(h : ∀ (x : M), ∃ (u : set M), is_open u ∧ x ∈ u ∧ charted_space.lift_prop_on P g u) :
charted_space.lift_prop P g :=
sorry
theorem lift_prop_within_at_congr {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {P : (H → H') → set H → H → Prop} {g : M → M'} {g' : M → M'}
{s : set M} {x : M} (hG : local_invariant_prop G G' P)
(h : charted_space.lift_prop_within_at P g s x) (h₁ : ∀ (y : M), y ∈ s → g' y = g y)
(hx : g' x = g x) : charted_space.lift_prop_within_at P g' s x :=
sorry
theorem lift_prop_within_at_congr_iff {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {P : (H → H') → set H → H → Prop} {g : M → M'} {g' : M → M'}
{s : set M} {x : M} (hG : local_invariant_prop G G' P) (h₁ : ∀ (y : M), y ∈ s → g' y = g y)
(hx : g' x = g x) :
charted_space.lift_prop_within_at P g' s x ↔ charted_space.lift_prop_within_at P g s x :=
sorry
theorem lift_prop_within_at_congr_of_eventually_eq {H : Type u_1} {M : Type u_2}
[topological_space H] [topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4}
[topological_space H'] [topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {P : (H → H') → set H → H → Prop} {g : M → M'} {g' : M → M'}
{s : set M} {x : M} (hG : local_invariant_prop G G' P)
(h : charted_space.lift_prop_within_at P g s x)
(h₁ : filter.eventually_eq (nhds_within x s) g' g) (hx : g' x = g x) :
charted_space.lift_prop_within_at P g' s x :=
sorry
theorem lift_prop_within_at_congr_iff_of_eventually_eq {H : Type u_1} {M : Type u_2}
[topological_space H] [topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4}
[topological_space H'] [topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {P : (H → H') → set H → H → Prop} {g : M → M'} {g' : M → M'}
{s : set M} {x : M} (hG : local_invariant_prop G G' P)
(h₁ : filter.eventually_eq (nhds_within x s) g' g) (hx : g' x = g x) :
charted_space.lift_prop_within_at P g' s x ↔ charted_space.lift_prop_within_at P g s x :=
sorry
theorem lift_prop_at_congr_of_eventually_eq {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {P : (H → H') → set H → H → Prop} {g : M → M'} {g' : M → M'}
{x : M} (hG : local_invariant_prop G G' P) (h : charted_space.lift_prop_at P g x)
(h₁ : filter.eventually_eq (nhds x) g' g) : charted_space.lift_prop_at P g' x :=
sorry
theorem lift_prop_at_congr_iff_of_eventually_eq {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {P : (H → H') → set H → H → Prop} {g : M → M'} {g' : M → M'}
{x : M} (hG : local_invariant_prop G G' P) (h₁ : filter.eventually_eq (nhds x) g' g) :
charted_space.lift_prop_at P g' x ↔ charted_space.lift_prop_at P g x :=
sorry
theorem lift_prop_on_congr {H : Type u_1} {M : Type u_2} [topological_space H] [topological_space M]
[charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {P : (H → H') → set H → H → Prop} {g : M → M'} {g' : M → M'}
{s : set M} (hG : local_invariant_prop G G' P) (h : charted_space.lift_prop_on P g s)
(h₁ : ∀ (y : M), y ∈ s → g' y = g y) : charted_space.lift_prop_on P g' s :=
fun (x : M) (hx : x ∈ s) => lift_prop_within_at_congr hG (h x hx) h₁ (h₁ x hx)
theorem lift_prop_on_congr_iff {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {G : structure_groupoid H}
{G' : structure_groupoid H'} {P : (H → H') → set H → H → Prop} {g : M → M'} {g' : M → M'}
{s : set M} (hG : local_invariant_prop G G' P) (h₁ : ∀ (y : M), y ∈ s → g' y = g y) :
charted_space.lift_prop_on P g' s ↔ charted_space.lift_prop_on P g s :=
sorry
theorem lift_prop_within_at_mono {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {P : (H → H') → set H → H → Prop} {g : M → M'}
{s : set M} {t : set M} {x : M}
(mono : ∀ {s : set H} {x : H} {t : set H} {f : H → H'}, t ⊆ s → P f s x → P f t x)
(h : charted_space.lift_prop_within_at P g t x) (hst : s ⊆ t) :
charted_space.lift_prop_within_at P g s x :=
sorry
theorem lift_prop_within_at_of_lift_prop_at {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {P : (H → H') → set H → H → Prop} {g : M → M'}
{s : set M} {x : M}
(mono : ∀ {s : set H} {x : H} {t : set H} {f : H → H'}, t ⊆ s → P f s x → P f t x)
(h : charted_space.lift_prop_at P g x) : charted_space.lift_prop_within_at P g s x :=
lift_prop_within_at_mono mono
(eq.mp
(Eq._oldrec (Eq.refl (charted_space.lift_prop_at P g x))
(Eq.symm (propext lift_prop_within_at_univ)))
h)
(set.subset_univ s)
theorem lift_prop_on_mono {H : Type u_1} {M : Type u_2} [topological_space H] [topological_space M]
[charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {P : (H → H') → set H → H → Prop} {g : M → M'}
{s : set M} {t : set M}
(mono : ∀ {s : set H} {x : H} {t : set H} {f : H → H'}, t ⊆ s → P f s x → P f t x)
(h : charted_space.lift_prop_on P g t) (hst : s ⊆ t) : charted_space.lift_prop_on P g s :=
fun (x : M) (hx : x ∈ s) => lift_prop_within_at_mono mono (h x (hst hx)) hst
theorem lift_prop_on_of_lift_prop {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {H' : Type u_3} {M' : Type u_4} [topological_space H']
[topological_space M'] [charted_space H' M'] {P : (H → H') → set H → H → Prop} {g : M → M'}
{s : set M} (mono : ∀ {s : set H} {x : H} {t : set H} {f : H → H'}, t ⊆ s → P f s x → P f t x)
(h : charted_space.lift_prop P g) : charted_space.lift_prop_on P g s :=
lift_prop_on_mono mono
(eq.mp
(Eq._oldrec (Eq.refl (charted_space.lift_prop P g)) (Eq.symm (propext lift_prop_on_univ))) h)
(set.subset_univ s)
theorem lift_prop_at_of_mem_maximal_atlas {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {G : structure_groupoid H} {e : local_homeomorph M H}
{x : M} {Q : (H → H) → set H → H → Prop} [has_groupoid M G] (hG : local_invariant_prop G G Q)
(hQ : ∀ (y : H), Q id set.univ y) (he : e ∈ maximal_atlas M G)
(hx : x ∈ local_equiv.source (local_homeomorph.to_local_equiv e)) :
charted_space.lift_prop_at Q (⇑e) x :=
sorry
theorem lift_prop_on_of_mem_maximal_atlas {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {G : structure_groupoid H} {e : local_homeomorph M H}
{Q : (H → H) → set H → H → Prop} [has_groupoid M G] (hG : local_invariant_prop G G Q)
(hQ : ∀ (y : H), Q id set.univ y) (he : e ∈ maximal_atlas M G) :
charted_space.lift_prop_on Q (⇑e) (local_equiv.source (local_homeomorph.to_local_equiv e)) :=
sorry
theorem lift_prop_at_symm_of_mem_maximal_atlas {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {G : structure_groupoid H} {e : local_homeomorph M H}
{Q : (H → H) → set H → H → Prop} [has_groupoid M G] {x : H} (hG : local_invariant_prop G G Q)
(hQ : ∀ (y : H), Q id set.univ y) (he : e ∈ maximal_atlas M G)
(hx : x ∈ local_equiv.target (local_homeomorph.to_local_equiv e)) :
charted_space.lift_prop_at Q (⇑(local_homeomorph.symm e)) x :=
sorry
theorem lift_prop_on_symm_of_mem_maximal_atlas {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {G : structure_groupoid H} {e : local_homeomorph M H}
{Q : (H → H) → set H → H → Prop} [has_groupoid M G] (hG : local_invariant_prop G G Q)
(hQ : ∀ (y : H), Q id set.univ y) (he : e ∈ maximal_atlas M G) :
charted_space.lift_prop_on Q (⇑(local_homeomorph.symm e))
(local_equiv.target (local_homeomorph.to_local_equiv e)) :=
sorry
theorem lift_prop_at_chart {H : Type u_1} {M : Type u_2} [topological_space H] [topological_space M]
[charted_space H M] {G : structure_groupoid H} {x : M} {Q : (H → H) → set H → H → Prop}
[has_groupoid M G] (hG : local_invariant_prop G G Q) (hQ : ∀ (y : H), Q id set.univ y) :
charted_space.lift_prop_at Q (⇑(charted_space.chart_at H x)) x :=
lift_prop_at_of_mem_maximal_atlas hG hQ (chart_mem_maximal_atlas G x)
(charted_space.mem_chart_source H x)
theorem lift_prop_on_chart {H : Type u_1} {M : Type u_2} [topological_space H] [topological_space M]
[charted_space H M] {G : structure_groupoid H} {x : M} {Q : (H → H) → set H → H → Prop}
[has_groupoid M G] (hG : local_invariant_prop G G Q) (hQ : ∀ (y : H), Q id set.univ y) :
charted_space.lift_prop_on Q (⇑(charted_space.chart_at H x))
(local_equiv.source (local_homeomorph.to_local_equiv (charted_space.chart_at H x))) :=
lift_prop_on_of_mem_maximal_atlas hG hQ (chart_mem_maximal_atlas G x)
theorem lift_prop_at_chart_symm {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {G : structure_groupoid H} {x : M}
{Q : (H → H) → set H → H → Prop} [has_groupoid M G] (hG : local_invariant_prop G G Q)
(hQ : ∀ (y : H), Q id set.univ y) :
charted_space.lift_prop_at Q (⇑(local_homeomorph.symm (charted_space.chart_at H x)))
(coe_fn (charted_space.chart_at H x) x) :=
sorry
theorem lift_prop_on_chart_symm {H : Type u_1} {M : Type u_2} [topological_space H]
[topological_space M] [charted_space H M] {G : structure_groupoid H} {x : M}
{Q : (H → H) → set H → H → Prop} [has_groupoid M G] (hG : local_invariant_prop G G Q)
(hQ : ∀ (y : H), Q id set.univ y) :
charted_space.lift_prop_on Q (⇑(local_homeomorph.symm (charted_space.chart_at H x)))
(local_equiv.target (local_homeomorph.to_local_equiv (charted_space.chart_at H x))) :=
lift_prop_on_symm_of_mem_maximal_atlas hG hQ (chart_mem_maximal_atlas G x)
theorem lift_prop_id {H : Type u_1} {M : Type u_2} [topological_space H] [topological_space M]
[charted_space H M] {G : structure_groupoid H} {Q : (H → H) → set H → H → Prop}
(hG : local_invariant_prop G G Q) (hQ : ∀ (y : H), Q id set.univ y) :
charted_space.lift_prop Q id :=
sorry
end local_invariant_prop
/-- A function from a model space `H` to itself is a local structomorphism, with respect to a
structure groupoid `G` for `H`, relative to a set `s` in `H`, if for all points `x` in the set, the
function agrees with a `G`-structomorphism on `s` in a neighbourhood of `x`. -/
def is_local_structomorph_within_at {H : Type u_1} [topological_space H] (G : structure_groupoid H)
(f : H → H) (s : set H) (x : H) :=
x ∈ s →
∃ (e : local_homeomorph H H),
e ∈ G ∧
set.eq_on f (local_equiv.to_fun (local_homeomorph.to_local_equiv e))
(s ∩ local_equiv.source (local_homeomorph.to_local_equiv e)) ∧
x ∈ local_equiv.source (local_homeomorph.to_local_equiv e)
/-- For a groupoid `G` which is `closed_under_restriction`, being a local structomorphism is a local
invariant property. -/
theorem is_local_structomorph_within_at_local_invariant_prop {H : Type u_1} [topological_space H]
(G : structure_groupoid H) [closed_under_restriction G] :
local_invariant_prop G G (is_local_structomorph_within_at G) :=
sorry
end Mathlib |
c566d2461477ce01c906dad6eec02f8250c041ce | 076f5040b63237c6dd928c6401329ed5adcb0e44 | /instructor-notes/2019.09.17.notes.lean | 9fd04ec0792ebfd318baa0da1146ee2033e1ce1a | [] | no_license | kevinsullivan/uva-cs-dm-f19 | 0f123689cf6cb078f263950b18382a7086bf30be | 09a950752884bd7ade4be33e9e89a2c4b1927167 | refs/heads/master | 1,594,771,841,541 | 1,575,853,850,000 | 1,575,853,850,000 | 205,433,890 | 4 | 9 | null | 1,571,592,121,000 | 1,567,188,539,000 | Lean | UTF-8 | Lean | false | false | 2,367 | lean | -- Namespaces
-- Here is the global namespace
-- first namespace nested within global
namespace cs
def x := 1
#check x
#eval x
end cs
-- second namespace
namespace oc
def x := "Hi!"
#check x
#eval x
end oc
-- namespaces are closed by default
#eval cs.x
#eval oc.x
#eval x -- x not visible in global ns
open cs -- names in cs visible in global
#eval x
open oc -- names in oc now also visible
#eval x -- but x now ambiguous in global
#eval cs.x -- must disambiguate
#eval oc.x -- with namespace qualifier
-- Ambiguity even if values are same
namespace n1
def q := 1
end n1
namespace n2
def q := 1
end n2
open n1
open n2
#check q -- ambiguous
/- END OF NAMESPACE TOPIC -/
/- ON TO TYPES -/
/-
So far we have seen data types and
function types. You can think of these
as *computational* types. Later on we
will introduce *logical* types.
Types are also values and so they,
too, have types. The type of any
computation type is "Type".
-/
-- the type of a data type is Type
#check 1
#check ℕ
#check bool
#check string
-- the type of a function type is Type
#check nat → nat
#check string → nat
#check nat → nat → bool
-- Big idea: we can define new data types
-- The type of such a type will be Type
inductive day : Type
| sun : day
| mon : day
| tue -- Lean infers types
| wed
| thu
| fri
| sat
#check day.mon
/-
The constructor names of a type are
defined in a namespace with the same
name as the type.
-/
open day
#check day
#check sun
-- next_day function (by cases)
def next_day : day → day
| sun := mon
| mon := tue
| tue := wed
| wed := thu
| thu := fri
| fri := sat
| sat := sun
-- evaluate an application term
#reduce next_day mon
-- is_weekend function by cases
def is_weekend : day → bool
| sun := tt
| mon := ff
| tue := ff
| wed := ff
| thu := ff
| fri := ff
| sat := tt
-- evaluate an application term
#reduce is_weekend mon
/-
How can we define a type with an
infinite number of values, such as
nat? The key is to define constructors
that build larger values of a type from
given smaller values. One ends up with
an *inductively defined type*.
-/
inductive my_nat : Type
| zero : my_nat
| succ : my_nat → my_nat
open my_nat
#reduce zero
#reduce succ zero
#reduce succ (succ zero)
def is_zero : my_nat → bool
| zero := tt
| _ := ff
#reduce is_zero zero
#reduce is_zero (succ ( succ zero)) |
78e243bbb5953a512b9f5607bb4c9e60192cf917 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/algebra/tower.lean | 093a1ef2bb23d0e09bf2f4d61368a84f271b68b2 | [
"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 | 11,159 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Anne Baanen
-/
import algebra.algebra.basic
/-!
# Towers of algebras
In this file we prove basic facts about towers of algebra.
An algebra tower A/S/R is expressed by having instances of `algebra A S`,
`algebra R S`, `algebra R A` and `is_scalar_tower R S A`, the later asserting the
compatibility condition `(r • s) • a = r • (s • a)`.
An important definition is `to_alg_hom R S A`, the canonical `R`-algebra homomorphism `S →ₐ[R] A`.
-/
open_locale pointwise
universes u v w u₁ v₁
variables (R : Type u) (S : Type v) (A : Type w) (B : Type u₁) (M : Type v₁)
namespace algebra
variables [comm_semiring R] [semiring A] [algebra R A]
variables [add_comm_monoid M] [module R M] [module A M] [is_scalar_tower R A M]
variables {A}
/-- The `R`-algebra morphism `A → End (M)` corresponding to the representation of the algebra `A`
on the `R`-module `M`.
This is a stronger version of `distrib_mul_action.to_linear_map`, and could also have been
called `algebra.to_module_End`. -/
def lsmul : A →ₐ[R] module.End R M :=
{ to_fun := distrib_mul_action.to_linear_map R M,
map_one' := linear_map.ext $ λ _, one_smul A _,
map_mul' := λ a b, linear_map.ext $ smul_assoc a b,
map_zero' := linear_map.ext $ λ _, zero_smul A _,
map_add' := λ a b, linear_map.ext $ λ _, add_smul _ _ _,
commutes' := λ r, linear_map.ext $ algebra_map_smul A r, }
@[simp] lemma lsmul_coe (a : A) : (lsmul R M a : M → M) = (•) a := rfl
end algebra
namespace is_scalar_tower
section module
variables [comm_semiring R] [semiring A] [algebra R A]
variables [has_smul R M] [mul_action A M] [is_scalar_tower R A M]
variables {R} (A) {M}
theorem algebra_map_smul (r : R) (x : M) : algebra_map R A r • x = r • x :=
by rw [algebra.algebra_map_eq_smul_one, smul_assoc, one_smul]
end module
section semiring
variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B]
variables [algebra R S] [algebra S A] [algebra S B]
variables {R S A}
theorem of_algebra_map_eq [algebra R A]
(h : ∀ x, algebra_map R A x = algebra_map S A (algebra_map R S x)) :
is_scalar_tower R S A :=
⟨λ x y z, by simp_rw [algebra.smul_def, ring_hom.map_mul, mul_assoc, h]⟩
/-- See note [partially-applied ext lemmas]. -/
theorem of_algebra_map_eq' [algebra R A]
(h : algebra_map R A = (algebra_map S A).comp (algebra_map R S)) :
is_scalar_tower R S A :=
of_algebra_map_eq $ ring_hom.ext_iff.1 h
variables (R S A)
variables [algebra R A] [algebra R B]
variables [is_scalar_tower R S A] [is_scalar_tower R S B]
theorem algebra_map_eq :
algebra_map R A = (algebra_map S A).comp (algebra_map R S) :=
ring_hom.ext $ λ x, by simp_rw [ring_hom.comp_apply, algebra.algebra_map_eq_smul_one,
smul_assoc, one_smul]
theorem algebra_map_apply (x : R) : algebra_map R A x = algebra_map S A (algebra_map R S x) :=
by rw [algebra_map_eq R S A, ring_hom.comp_apply]
@[ext] lemma algebra.ext {S : Type u} {A : Type v} [comm_semiring S] [semiring A]
(h1 h2 : algebra S A) (h : ∀ (r : S) (x : A), (by haveI := h1; exact r • x) = r • x) : h1 = h2 :=
algebra.algebra_ext _ _ $ λ r, by
simpa only [@algebra.smul_def _ _ _ _ h1, @algebra.smul_def _ _ _ _ h2, mul_one] using h r 1
/-- In a tower, the canonical map from the middle element to the top element is an
algebra homomorphism over the bottom element. -/
def to_alg_hom : S →ₐ[R] A :=
{ commutes' := λ _, (algebra_map_apply _ _ _ _).symm,
.. algebra_map S A }
lemma to_alg_hom_apply (y : S) : to_alg_hom R S A y = algebra_map S A y := rfl
@[simp] lemma coe_to_alg_hom : ↑(to_alg_hom R S A) = algebra_map S A :=
ring_hom.ext $ λ _, rfl
@[simp] lemma coe_to_alg_hom' : (to_alg_hom R S A : S → A) = algebra_map S A :=
rfl
variables {R S A B}
@[simp, priority 900] lemma _root_.alg_hom.map_algebra_map (f : A →ₐ[S] B) (r : R) :
f (algebra_map R A r) = algebra_map R B r :=
by rw [algebra_map_apply R S A r, f.commutes, ← algebra_map_apply R S B]
variables (R)
@[simp, priority 900] lemma _root_.alg_hom.comp_algebra_map_of_tower (f : A →ₐ[S] B) :
(f : A →+* B).comp (algebra_map R A) = algebra_map R B :=
ring_hom.ext f.map_algebra_map
variables (R) {S A B}
-- conflicts with is_scalar_tower.subalgebra
@[priority 999] instance subsemiring (U : subsemiring S) : is_scalar_tower U S A :=
of_algebra_map_eq $ λ x, rfl
@[nolint instance_priority]
instance of_ring_hom {R A B : Type*} [comm_semiring R] [comm_semiring A] [comm_semiring B]
[algebra R A] [algebra R B] (f : A →ₐ[R] B) :
@is_scalar_tower R A B _ (f.to_ring_hom.to_algebra.to_has_smul) _ :=
by { letI := (f : A →+* B).to_algebra, exact of_algebra_map_eq (λ x, (f.commutes x).symm) }
end semiring
end is_scalar_tower
section homs
variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B]
variables [algebra R S] [algebra S A] [algebra S B]
variables [algebra R A] [algebra R B]
variables [is_scalar_tower R S A] [is_scalar_tower R S B]
variables (R) {A S B}
open is_scalar_tower
namespace alg_hom
/-- R ⟶ S induces S-Alg ⥤ R-Alg -/
def restrict_scalars (f : A →ₐ[S] B) : A →ₐ[R] B :=
{ commutes' := λ r, by { rw [algebra_map_apply R S A, algebra_map_apply R S B],
exact f.commutes (algebra_map R S r) },
.. (f : A →+* B) }
lemma restrict_scalars_apply (f : A →ₐ[S] B) (x : A) : f.restrict_scalars R x = f x := rfl
@[simp] lemma coe_restrict_scalars (f : A →ₐ[S] B) : (f.restrict_scalars R : A →+* B) = f := rfl
@[simp] lemma coe_restrict_scalars' (f : A →ₐ[S] B) : (restrict_scalars R f : A → B) = f := rfl
lemma restrict_scalars_injective :
function.injective (restrict_scalars R : (A →ₐ[S] B) → (A →ₐ[R] B)) :=
λ f g h, alg_hom.ext (alg_hom.congr_fun h : _)
end alg_hom
namespace alg_equiv
/-- R ⟶ S induces S-Alg ⥤ R-Alg -/
def restrict_scalars (f : A ≃ₐ[S] B) : A ≃ₐ[R] B :=
{ commutes' := λ r, by { rw [algebra_map_apply R S A, algebra_map_apply R S B],
exact f.commutes (algebra_map R S r) },
.. (f : A ≃+* B) }
lemma restrict_scalars_apply (f : A ≃ₐ[S] B) (x : A) : f.restrict_scalars R x = f x := rfl
@[simp] lemma coe_restrict_scalars (f : A ≃ₐ[S] B) : (f.restrict_scalars R : A ≃+* B) = f := rfl
@[simp] lemma coe_restrict_scalars' (f : A ≃ₐ[S] B) : (restrict_scalars R f : A → B) = f := rfl
lemma restrict_scalars_injective :
function.injective (restrict_scalars R : (A ≃ₐ[S] B) → (A ≃ₐ[R] B)) :=
λ f g h, alg_equiv.ext (alg_equiv.congr_fun h : _)
end alg_equiv
end homs
namespace algebra
variables {R A} [comm_semiring R] [semiring A] [algebra R A]
variables {M} [add_comm_monoid M] [module A M] [module R M] [is_scalar_tower R A M]
lemma span_restrict_scalars_eq_span_of_surjective
(h : function.surjective (algebra_map R A)) (s : set M) :
(submodule.span A s).restrict_scalars R = submodule.span R s :=
begin
refine le_antisymm (λ x hx, _) (submodule.span_subset_span _ _ _),
refine submodule.span_induction hx _ _ _ _,
{ exact λ x hx, submodule.subset_span hx },
{ exact submodule.zero_mem _ },
{ exact λ x y, submodule.add_mem _ },
{ intros c x hx,
obtain ⟨c', rfl⟩ := h c,
rw is_scalar_tower.algebra_map_smul,
exact submodule.smul_mem _ _ hx },
end
lemma coe_span_eq_span_of_surjective
(h : function.surjective (algebra_map R A)) (s : set M) :
(submodule.span A s : set M) = submodule.span R s :=
congr_arg coe (algebra.span_restrict_scalars_eq_span_of_surjective h s)
end algebra
section semiring
variables {R S A}
namespace submodule
section module
variables [semiring R] [semiring S] [add_comm_monoid A]
variables [module R S] [module S A] [module R A] [is_scalar_tower R S A]
open is_scalar_tower
theorem smul_mem_span_smul_of_mem {s : set S} {t : set A} {k : S} (hks : k ∈ span R s)
{x : A} (hx : x ∈ t) : k • x ∈ span R (s • t) :=
span_induction hks (λ c hc, subset_span $ set.mem_smul.2 ⟨c, x, hc, hx, rfl⟩)
(by { rw zero_smul, exact zero_mem _ })
(λ c₁ c₂ ih₁ ih₂, by { rw add_smul, exact add_mem ih₁ ih₂ })
(λ b c hc, by { rw is_scalar_tower.smul_assoc, exact smul_mem _ _ hc })
variables [smul_comm_class R S A]
theorem smul_mem_span_smul {s : set S} (hs : span R s = ⊤) {t : set A} {k : S}
{x : A} (hx : x ∈ span R t) :
k • x ∈ span R (s • t) :=
span_induction hx (λ x hx, smul_mem_span_smul_of_mem (hs.symm ▸ mem_top) hx)
(by { rw smul_zero, exact zero_mem _ })
(λ x y ihx ihy, by { rw smul_add, exact add_mem ihx ihy })
(λ c x hx, smul_comm c k x ▸ smul_mem _ _ hx)
theorem smul_mem_span_smul' {s : set S} (hs : span R s = ⊤) {t : set A} {k : S}
{x : A} (hx : x ∈ span R (s • t)) :
k • x ∈ span R (s • t) :=
span_induction hx (λ x hx, let ⟨p, q, hp, hq, hpq⟩ := set.mem_smul.1 hx in
by { rw [← hpq, smul_smul], exact smul_mem_span_smul_of_mem (hs.symm ▸ mem_top) hq })
(by { rw smul_zero, exact zero_mem _ })
(λ x y ihx ihy, by { rw smul_add, exact add_mem ihx ihy })
(λ c x hx, smul_comm c k x ▸ smul_mem _ _ hx)
theorem span_smul_of_span_eq_top {s : set S} (hs : span R s = ⊤) (t : set A) :
span R (s • t) = (span S t).restrict_scalars R :=
le_antisymm (span_le.2 $ λ x hx, let ⟨p, q, hps, hqt, hpqx⟩ := set.mem_smul.1 hx in
hpqx ▸ (span S t).smul_mem p (subset_span hqt)) $
λ p hp, span_induction hp (λ x hx, one_smul S x ▸ smul_mem_span_smul hs (subset_span hx))
(zero_mem _)
(λ _ _, add_mem)
(λ k x hx, smul_mem_span_smul' hs hx)
end module
section algebra
variables [comm_semiring R] [semiring S] [add_comm_monoid A]
variables [algebra R S] [module S A] [module R A] [is_scalar_tower R S A]
/-- A variant of `submodule.span_image` for `algebra_map`. -/
lemma span_algebra_map_image (a : set R) :
submodule.span R (algebra_map R S '' a) =
(submodule.span R a).map (algebra.linear_map R S) :=
(submodule.span_image $ algebra.linear_map R S).trans rfl
lemma span_algebra_map_image_of_tower {S T : Type*} [comm_semiring S] [semiring T]
[module R S] [is_scalar_tower R S S] [algebra R T] [algebra S T] [is_scalar_tower R S T]
(a : set S) :
submodule.span R (algebra_map S T '' a) =
(submodule.span R a).map ((algebra.linear_map S T).restrict_scalars R) :=
(submodule.span_image $ (algebra.linear_map S T).restrict_scalars R).trans rfl
lemma map_mem_span_algebra_map_image {S T : Type*} [comm_semiring S] [semiring T]
[algebra R S] [algebra R T] [algebra S T] [is_scalar_tower R S T]
(x : S) (a : set S) (hx : x ∈ submodule.span R a) :
algebra_map S T x ∈ submodule.span R (algebra_map S T '' a) :=
by { rw [span_algebra_map_image_of_tower, mem_map], exact ⟨x, hx, rfl⟩ }
end algebra
end submodule
end semiring
section ring
namespace algebra
variables [comm_semiring R] [semiring A] [algebra R A]
variables [add_comm_group M] [module A M] [module R M] [is_scalar_tower R A M]
lemma lsmul_injective [no_zero_smul_divisors A M] {x : A} (hx : x ≠ 0) :
function.injective (lsmul R M x) :=
smul_right_injective _ hx
end algebra
end ring
|
3e6a56fb0592f655786bbac8a49f1d0a12e440af | bb31430994044506fa42fd667e2d556327e18dfe | /src/algebra/hom/non_unital_alg.lean | c60ff8177307c534a1e3e6c06b79011f9c7b95fd | [
"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 | 12,429 | lean | /-
Copyright (c) 2021 Oliver Nash. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Oliver Nash
-/
import algebra.algebra.hom
/-!
# Morphisms of non-unital algebras
This file defines morphisms between two types, each of which carries:
* an addition,
* an additive zero,
* a multiplication,
* a scalar action.
The multiplications are not assumed to be associative or unital, or even to be compatible with the
scalar actions. In a typical application, the operations will satisfy compatibility conditions
making them into algebras (albeit possibly non-associative and/or non-unital) but such conditions
are not required to make this definition.
This notion of morphism should be useful for any category of non-unital algebras. The motivating
application at the time it was introduced was to be able to state the adjunction property for
magma algebras. These are non-unital, non-associative algebras obtained by applying the
group-algebra construction except where we take a type carrying just `has_mul` instead of `group`.
For a plausible future application, one could take the non-unital algebra of compactly-supported
functions on a non-compact topological space. A proper map between a pair of such spaces
(contravariantly) induces a morphism between their algebras of compactly-supported functions which
will be a `non_unital_alg_hom`.
TODO: add `non_unital_alg_equiv` when needed.
## Main definitions
* `non_unital_alg_hom`
* `alg_hom.to_non_unital_alg_hom`
## Tags
non-unital, algebra, morphism
-/
universes u v w w₁ w₂ w₃
variables (R : Type u) (A : Type v) (B : Type w) (C : Type w₁)
set_option old_structure_cmd true
/-- A morphism respecting addition, multiplication, and scalar multiplication. When these arise from
algebra structures, this is the same as a not-necessarily-unital morphism of algebras. -/
structure non_unital_alg_hom [monoid R]
[non_unital_non_assoc_semiring A] [distrib_mul_action R A]
[non_unital_non_assoc_semiring B] [distrib_mul_action R B]
extends A →+[R] B, A →ₙ* B
infixr ` →ₙₐ `:25 := non_unital_alg_hom _
notation A ` →ₙₐ[`:25 R `] ` B := non_unital_alg_hom R A B
attribute [nolint doc_blame] non_unital_alg_hom.to_distrib_mul_action_hom
attribute [nolint doc_blame] non_unital_alg_hom.to_mul_hom
/-- `non_unital_alg_hom_class F R A B` asserts `F` is a type of bundled algebra homomorphisms
from `A` to `B`. -/
class non_unital_alg_hom_class (F : Type*) (R : out_param Type*) (A : out_param Type*)
(B : out_param Type*) [monoid R]
[non_unital_non_assoc_semiring A] [non_unital_non_assoc_semiring B]
[distrib_mul_action R A] [distrib_mul_action R B]
extends distrib_mul_action_hom_class F R A B, mul_hom_class F A B
-- `R` becomes a metavariable but that's fine because it's an `out_param`
attribute [nolint dangerous_instance] non_unital_alg_hom_class.to_mul_hom_class
namespace non_unital_alg_hom_class
-- `R` becomes a metavariable but that's fine because it's an `out_param`
@[priority 100, nolint dangerous_instance] -- See note [lower instance priority]
instance non_unital_alg_hom_class.to_non_unital_ring_hom_class {F R A B : Type*} [monoid R]
[non_unital_non_assoc_semiring A] [distrib_mul_action R A]
[non_unital_non_assoc_semiring B] [distrib_mul_action R B]
[non_unital_alg_hom_class F R A B] : non_unital_ring_hom_class F A B :=
{ coe := coe_fn, ..‹non_unital_alg_hom_class F R A B› }
variables [semiring R]
[non_unital_non_assoc_semiring A] [module R A]
[non_unital_non_assoc_semiring B] [module R B]
@[priority 100] -- see Note [lower instance priority]
instance {F : Type*} [non_unital_alg_hom_class F R A B] : linear_map_class F R A B :=
{ map_smulₛₗ := distrib_mul_action_hom_class.map_smul,
..‹non_unital_alg_hom_class F R A B› }
instance {F R A B : Type*} [monoid R]
[non_unital_non_assoc_semiring A] [distrib_mul_action R A]
[non_unital_non_assoc_semiring B] [distrib_mul_action R B]
[non_unital_alg_hom_class F R A B] : has_coe_t F (A →ₙₐ[R] B) :=
{ coe := λ f,
{ to_fun := f,
map_smul' := map_smul f,
.. (f : A →ₙ+* B) } }
end non_unital_alg_hom_class
namespace non_unital_alg_hom
variables {R A B C} [monoid R]
variables [non_unital_non_assoc_semiring A] [distrib_mul_action R A]
variables [non_unital_non_assoc_semiring B] [distrib_mul_action R B]
variables [non_unital_non_assoc_semiring C] [distrib_mul_action R C]
/-- see Note [function coercion] -/
instance : has_coe_to_fun (A →ₙₐ[R] B) (λ _, A → B) := ⟨to_fun⟩
@[simp] lemma to_fun_eq_coe (f : A →ₙₐ[R] B) : f.to_fun = ⇑f := rfl
initialize_simps_projections non_unital_alg_hom (to_fun → apply)
@[simp, protected] lemma coe_coe {F : Type*} [non_unital_alg_hom_class F R A B] (f : F) :
⇑(f : A →ₙₐ[R] B) = f := rfl
lemma coe_injective :
@function.injective (A →ₙₐ[R] B) (A → B) coe_fn :=
by rintro ⟨f, _⟩ ⟨g, _⟩ ⟨h⟩; congr
instance : non_unital_alg_hom_class (A →ₙₐ[R] B) R A B :=
{ coe := to_fun,
coe_injective' := coe_injective,
map_smul := λ f, f.map_smul',
map_add := λ f, f.map_add',
map_zero := λ f, f.map_zero',
map_mul := λ f, f.map_mul' }
@[ext] lemma ext {f g : A →ₙₐ[R] B} (h : ∀ x, f x = g x) : f = g :=
coe_injective $ funext h
lemma ext_iff {f g : A →ₙₐ[R] B} : f = g ↔ ∀ x, f x = g x :=
⟨by { rintro rfl x, refl }, ext⟩
lemma congr_fun {f g : A →ₙₐ[R] B} (h : f = g) (x : A) : f x = g x := h ▸ rfl
@[simp] lemma coe_mk (f : A → B) (h₁ h₂ h₃ h₄) :
((⟨f, h₁, h₂, h₃, h₄⟩ : A →ₙₐ[R] B) : A → B) = f :=
rfl
@[simp] lemma mk_coe (f : A →ₙₐ[R] B) (h₁ h₂ h₃ h₄) :
(⟨f, h₁, h₂, h₃, h₄⟩ : A →ₙₐ[R] B) = f :=
by { ext, refl, }
instance : has_coe (A →ₙₐ[R] B) (A →+[R] B) :=
⟨to_distrib_mul_action_hom⟩
instance : has_coe (A →ₙₐ[R] B) (A →ₙ* B) := ⟨to_mul_hom⟩
@[simp] lemma to_distrib_mul_action_hom_eq_coe (f : A →ₙₐ[R] B) :
f.to_distrib_mul_action_hom = ↑f :=
rfl
@[simp] lemma to_mul_hom_eq_coe (f : A →ₙₐ[R] B) : f.to_mul_hom = ↑f :=
rfl
@[simp, norm_cast] lemma coe_to_distrib_mul_action_hom (f : A →ₙₐ[R] B) :
((f : A →+[R] B) : A → B) = f :=
rfl
@[simp, norm_cast] lemma coe_to_mul_hom (f : A →ₙₐ[R] B) :
((f : A →ₙ* B) : A → B) = f :=
rfl
lemma to_distrib_mul_action_hom_injective {f g : A →ₙₐ[R] B}
(h : (f : A →+[R] B) = (g : A →+[R] B)) : f = g :=
by { ext a, exact distrib_mul_action_hom.congr_fun h a, }
lemma to_mul_hom_injective {f g : A →ₙₐ[R] B}
(h : (f : A →ₙ* B) = (g : A →ₙ* B)) : f = g :=
by { ext a, exact mul_hom.congr_fun h a, }
@[norm_cast] lemma coe_distrib_mul_action_hom_mk (f : A →ₙₐ[R] B) (h₁ h₂ h₃ h₄) :
((⟨f, h₁, h₂, h₃, h₄⟩ : A →ₙₐ[R] B) : A →+[R] B) =
⟨f, h₁, h₂, h₃⟩ :=
by { ext, refl, }
@[norm_cast] lemma coe_mul_hom_mk (f : A →ₙₐ[R] B) (h₁ h₂ h₃ h₄) :
((⟨f, h₁, h₂, h₃, h₄⟩ : A →ₙₐ[R] B) : A →ₙ* B) = ⟨f, h₄⟩ :=
by { ext, refl, }
@[simp] protected lemma map_smul (f : A →ₙₐ[R] B) (c : R) (x : A) :
f (c • x) = c • f x := map_smul _ _ _
@[simp] protected lemma map_add (f : A →ₙₐ[R] B) (x y : A) :
f (x + y) = (f x) + (f y) := map_add _ _ _
@[simp] protected lemma map_mul (f : A →ₙₐ[R] B) (x y : A) :
f (x * y) = (f x) * (f y) := map_mul _ _ _
@[simp] protected lemma map_zero (f : A →ₙₐ[R] B) : f 0 = 0 := map_zero _
instance : has_zero (A →ₙₐ[R] B) :=
⟨{ map_mul' := by simp,
.. (0 : A →+[R] B) }⟩
instance : has_one (A →ₙₐ[R] A) :=
⟨{ map_mul' := by simp,
.. (1 : A →+[R] A) }⟩
@[simp] lemma coe_zero : ((0 : A →ₙₐ[R] B) : A → B) = 0 := rfl
@[simp] lemma coe_one : ((1 : A →ₙₐ[R] A) : A → A) = id := rfl
lemma zero_apply (a : A) : (0 : A →ₙₐ[R] B) a = 0 := rfl
lemma one_apply (a : A) : (1 : A →ₙₐ[R] A) a = a := rfl
instance : inhabited (A →ₙₐ[R] B) := ⟨0⟩
/-- The composition of morphisms is a morphism. -/
def comp (f : B →ₙₐ[R] C) (g : A →ₙₐ[R] B) : A →ₙₐ[R] C :=
{ .. (f : B →ₙ* C).comp (g : A →ₙ* B),
.. (f : B →+[R] C).comp (g : A →+[R] B) }
@[simp, norm_cast] lemma coe_comp (f : B →ₙₐ[R] C) (g : A →ₙₐ[R] B) :
(f.comp g : A → C) = (f : B → C) ∘ (g : A → B) :=
rfl
lemma comp_apply (f : B →ₙₐ[R] C) (g : A →ₙₐ[R] B) (x : A) :
f.comp g x = f (g x) :=
rfl
/-- The inverse of a bijective morphism is a morphism. -/
def inverse (f : A →ₙₐ[R] B) (g : B → A)
(h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) :
B →ₙₐ[R] A :=
{ .. (f : A →ₙ* B).inverse g h₁ h₂,
.. (f : A →+[R] B).inverse g h₁ h₂ }
@[simp] lemma coe_inverse (f : A →ₙₐ[R] B) (g : B → A)
(h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) :
(inverse f g h₁ h₂ : B → A) = g :=
rfl
/-! ### Operations on the product type
Note that much of this is copied from [`linear_algebra/prod`](../../linear_algebra/prod). -/
section prod
variables (R A B)
/-- The first projection of a product is a non-unital alg_hom. -/
@[simps]
def fst : A × B →ₙₐ[R] A :=
{ to_fun := prod.fst,
map_zero' := rfl, map_add' := λ x y, rfl, map_smul' := λ x y, rfl, map_mul' := λ x y, rfl }
/-- The second projection of a product is a non-unital alg_hom. -/
@[simps]
def snd : A × B →ₙₐ[R] B :=
{ to_fun := prod.snd,
map_zero' := rfl, map_add' := λ x y, rfl, map_smul' := λ x y, rfl, map_mul' := λ x y, rfl }
variables {R A B}
/-- The prod of two morphisms is a morphism. -/
@[simps] def prod (f : A →ₙₐ[R] B) (g : A →ₙₐ[R] C) : (A →ₙₐ[R] B × C) :=
{ to_fun := pi.prod f g,
map_zero' := by simp only [pi.prod, prod.zero_eq_mk, map_zero],
map_add' := λ x y, by simp only [pi.prod, prod.mk_add_mk, map_add],
map_mul' := λ x y, by simp only [pi.prod, prod.mk_mul_mk, map_mul],
map_smul' := λ c x, by simp only [pi.prod, prod.smul_mk, map_smul, ring_hom.id_apply] }
lemma coe_prod (f : A →ₙₐ[R] B) (g : A →ₙₐ[R] C) : ⇑(f.prod g) = pi.prod f g := rfl
@[simp] theorem fst_prod (f : A →ₙₐ[R] B) (g : A →ₙₐ[R] C) :
(fst R B C).comp (prod f g) = f := by ext; refl
@[simp] theorem snd_prod (f : A →ₙₐ[R] B) (g : A →ₙₐ[R] C) :
(snd R B C).comp (prod f g) = g := by ext; refl
@[simp] theorem prod_fst_snd : prod (fst R A B) (snd R A B) = 1 :=
coe_injective pi.prod_fst_snd
/-- Taking the product of two maps with the same domain is equivalent to taking the product of
their codomains. -/
@[simps] def prod_equiv : ((A →ₙₐ[R] B) × (A →ₙₐ[R] C)) ≃ (A →ₙₐ[R] B × C) :=
{ to_fun := λ f, f.1.prod f.2,
inv_fun := λ f, ((fst _ _ _).comp f, (snd _ _ _).comp f),
left_inv := λ f, by ext; refl,
right_inv := λ f, by ext; refl }
variables (R A B)
/-- The left injection into a product is a non-unital algebra homomorphism. -/
def inl : A →ₙₐ[R] A × B := prod 1 0
/-- The right injection into a product is a non-unital algebra homomorphism. -/
def inr : B →ₙₐ[R] A × B := prod 0 1
variables {R A B}
@[simp] theorem coe_inl : (inl R A B : A → A × B) = λ x, (x, 0) := rfl
theorem inl_apply (x : A) : inl R A B x = (x, 0) := rfl
@[simp] theorem coe_inr : (inr R A B : B → A × B) = prod.mk 0 := rfl
theorem inr_apply (x : B) : inr R A B x = (0, x) := rfl
end prod
end non_unital_alg_hom
/-! ### Interaction with `alg_hom` -/
namespace alg_hom
variables {R A B} [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B]
@[priority 100] -- see Note [lower instance priority]
instance {F : Type*} [alg_hom_class F R A B] : non_unital_alg_hom_class F R A B :=
{ map_smul := map_smul,
..‹alg_hom_class F R A B› }
/-- A unital morphism of algebras is a `non_unital_alg_hom`. -/
def to_non_unital_alg_hom (f : A →ₐ[R] B) : A →ₙₐ[R] B :=
{ map_smul' := map_smul f, .. f, }
instance non_unital_alg_hom.has_coe : has_coe (A →ₐ[R] B) (A →ₙₐ[R] B) :=
⟨to_non_unital_alg_hom⟩
@[simp] lemma to_non_unital_alg_hom_eq_coe (f : A →ₐ[R] B) : f.to_non_unital_alg_hom = f :=
rfl
@[simp, norm_cast] lemma coe_to_non_unital_alg_hom (f : A →ₐ[R] B) :
((f : A →ₙₐ[R] B) : A → B) = f :=
rfl
end alg_hom
|
1fb8c01b50e3b10de3100ca2d61af0795a91dc77 | f083c4ed5d443659f3ed9b43b1ca5bb037ddeb58 | /linear_algebra/quotient_module.lean | ed4dec1b2a42e24418e6215e237f6905b7961f27 | [
"Apache-2.0"
] | permissive | semorrison/mathlib | 1be6f11086e0d24180fec4b9696d3ec58b439d10 | 20b4143976dad48e664c4847b75a85237dca0a89 | refs/heads/master | 1,583,799,212,170 | 1,535,634,130,000 | 1,535,730,505,000 | 129,076,205 | 0 | 0 | Apache-2.0 | 1,551,697,998,000 | 1,523,442,265,000 | Lean | UTF-8 | Lean | false | false | 4,333 | 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
Quotient construction on modules
-/
import linear_algebra.basic
namespace is_submodule
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
variables [ring α] [module α β] [module α γ] (s : set β) [hs : is_submodule s]
include α s hs
open function
def quotient_rel : setoid β :=
⟨λb₁ b₂, b₁ - b₂ ∈ s,
assume b, by simp [zero],
assume b₁ b₂ hb,
have - (b₁ - b₂) ∈ s, from is_submodule.neg hb,
by simpa using this,
assume b₁ b₂ b₃ hb₁₂ hb₂₃,
have (b₁ - b₂) + (b₂ - b₃) ∈ s, from add hb₁₂ hb₂₃,
by simpa using this⟩
local attribute [instance] quotient_rel
lemma quotient_rel_eq {b₁ b₂ : β} : (b₁ ≈ b₂) = (b₁ - b₂ ∈ s) := rfl
section
variable (β)
/-- Quotient module. `quotient β s` is the quotient of the module `β` by the submodule `s`. -/
def quotient : Type v := quotient (quotient_rel s)
end
local notation ` Q ` := quotient β s
instance quotient.has_zero : has_zero Q := ⟨⟦ 0 ⟧⟩
instance quotient.has_add : has_add Q :=
⟨λa b, quotient.lift_on₂ a b (λa b, ⟦a + b⟧) $
assume a₁ a₂ b₁ b₂ (h₁ : a₁ - b₁ ∈ s) (h₂ : a₂ - b₂ ∈ s),
quotient.sound $
have (a₁ - b₁) + (a₂ - b₂) ∈ s, from add h₁ h₂,
show (a₁ + a₂) - (b₁ + b₂) ∈ s, by simpa⟩
instance quotient.has_neg : has_neg Q :=
⟨λa, quotient.lift_on a (λa, ⟦- a⟧) $ assume a b (h : a - b ∈ s),
quotient.sound $
have - (a - b) ∈ s, from neg h,
show (-a) - (-b) ∈ s, by simpa⟩
instance quotient.add_comm_group : add_comm_group Q :=
{ zero := 0,
add := (+),
neg := has_neg.neg,
add_assoc := assume a b c, quotient.induction_on₃ a b c $ assume a b c, quotient.sound $
by simp,
add_comm := assume a b, quotient.induction_on₂ a b $ assume a b, quotient.sound $
by simp,
add_zero := assume a, quotient.induction_on a $ assume a, quotient.sound $
by simp,
zero_add := assume a, quotient.induction_on a $ assume a, quotient.sound $
by simp,
add_left_neg := assume a, quotient.induction_on a $ assume a, quotient.sound $
by simp }
instance quotient.has_scalar : has_scalar α Q :=
⟨λa b, quotient.lift_on b (λb, ⟦a • b⟧) $ assume b₁ b₂ (h : b₁ - b₂ ∈ s),
quotient.sound $
have a • (b₁ - b₂) ∈ s, from is_submodule.smul a h,
show a • b₁ - a • b₂ ∈ s, by simpa [smul_add]⟩
instance quotient.module : module α Q :=
{ smul := (•),
one_smul := assume a, quotient.induction_on a $ assume a, quotient.sound $
by simp,
mul_smul := assume a b c, quotient.induction_on c $ assume c, quotient.sound $
by simp [mul_smul],
smul_add := assume a b c, quotient.induction_on₂ b c $ assume b c, quotient.sound $
by simp [smul_add],
add_smul := assume a b c, quotient.induction_on c $ assume c, quotient.sound $
by simp [add_smul] }
instance quotient.inhabited : inhabited Q := ⟨0⟩
lemma is_linear_map_quotient_mk : @is_linear_map _ _ Q _ _ _ (λb, ⟦b⟧ : β → Q) :=
by refine {..}; intros; refl
def quotient.lift {f : β → γ} (hf : is_linear_map f) (h : ∀x∈s, f x = 0) (b : Q) : γ :=
b.lift_on f $ assume a b (hab : a - b ∈ s),
have f a - f b = 0, by rw [←hf.sub]; exact h _ hab,
show f a = f b, from eq_of_sub_eq_zero this
@[simp] lemma quotient.lift_mk {f : β → γ} (hf : is_linear_map f) (h : ∀x∈s, f x = 0) (b : β) :
quotient.lift s hf h ⟦b⟧ = f b :=
rfl
lemma is_linear_map_quotient_lift {f : β → γ} {h : ∀x y, x - y ∈ s → f x = f y}
(hf : is_linear_map f) : is_linear_map (λq:Q, quotient.lift_on q f h) :=
⟨assume b₁ b₂, quotient.induction_on₂ b₁ b₂ $ assume b₁ b₂, hf.add b₁ b₂,
assume a b, quotient.induction_on b $ assume b, hf.smul a b⟩
lemma quotient.injective_lift [is_submodule s] {f : β → γ} (hf : is_linear_map f)
(hs : s = {x | f x = 0}) : injective (quotient.lift s hf $ le_of_eq hs) :=
assume a b, quotient.induction_on₂ a b $ assume a b (h : f a = f b), quotient.sound $
have f (a - b) = 0, by rw [hf.sub]; simp [h],
show a - b ∈ s, from hs.symm ▸ this
end is_submodule |
dea755cc630dff252d8a47ab4cae996832c06b07 | 57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d | /tests/lean/run/inductive1.lean | ed52ba27f883266d80169c487a0112062d8595d3 | [
"Apache-2.0"
] | permissive | collares/lean4 | 861a9269c4592bce49b71059e232ff0bfe4594cc | 52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee | refs/heads/master | 1,691,419,031,324 | 1,618,678,138,000 | 1,618,678,138,000 | 358,989,750 | 0 | 0 | Apache-2.0 | 1,618,696,333,000 | 1,618,696,333,000 | null | UTF-8 | Lean | false | false | 1,636 | lean |
inductive L1.{u} (α : Type u)
| nil
| cons : α → L1 α → L1 α
#check L1
#check @L1.cons
inductive L2.{u} (α : Type u)
| nil
| cons (head : α) (tail : L2 α)
#check @L2.cons
universes u v
variable (α : Type u)
inductive A (β : Type v)
| nil {}
| protected cons : α → β → A β → A β
#check @A.cons
#check A.nil Nat Bool
mutual
inductive isEven : Nat → Prop
| z : isEven 0
| s (n : Nat) : isOdd n → isEven (n+1)
inductive isOdd : Nat → Prop
| s (n : Nat) : isEven n → isOdd (n+1)
end
#check isEven
#check isOdd.s
#check @isEven.rec
inductive V (α : Type _) : Nat → Type _
| nil : V α 0
| cons {n : Nat} : α → V α n → V α (n+1)
#check @V.nil
#check @V.cons
#check @V.rec
#check @V.noConfusion
#check @V.brecOn
#check @V.binductionOn
#check @V.casesOn
#check @V.recOn
#check @V.below
class inductive Dec (p : Prop) : Type
| isTrue (h : p)
| isFalse (h : Not p)
instance tst : Dec True :=
Dec.isTrue True.intro
#check tst
variable (β : Type _)
inductive T1
| mk : β → β → T1
#check @T1.mk
inductive MyEq {α : Type} (a : α) : α → Prop
| refl : MyEq a a
#check @MyEq.refl
inductive ListLast {α : Type u} : List α → Type u
| empty : ListLast []
| nonEmpty : (as : List α) → (a : α) → ListLast (as ++ [a])
-- make sure to instantiate mvars in constructors
inductive Test : Nat → Type
| mk : Test ((fun n => n.succ) Nat.zero)
inductive SortedMap {α : Type u} {β : Type v} [HasLess α] : List (α × β) → Prop
| nil : SortedMap []
| cons : ∀ (k : α) (v : β) (l : List (α × β)),
SortedMap l →
SortedMap ((k,v)::l)
|
6d8ba5e5e92a664a5b07bda8726ec7054da1a012 | e9078bde91465351e1b354b353c9f9d8b8a9c8c2 | /helper_lemmas.hlean | 3250ffa2aaee6e75f79cd58f9276c8068f2fe711 | [
"Apache-2.0"
] | permissive | EgbertRijke/leansnippets | 09fb7a9813477471532fbdd50c99be8d8fe3e6c4 | 1d9a7059784c92c0281fcc7ce66ac7b3619c8661 | refs/heads/master | 1,610,743,957,626 | 1,442,532,603,000 | 1,442,532,603,000 | 41,563,379 | 0 | 0 | null | 1,440,787,514,000 | 1,440,787,514,000 | null | UTF-8 | Lean | false | false | 3,110 | hlean | /- NOT FOR BLESSED REPOSITORY -/
import types.eq types.pi hit.colimit cubical.square
open eq is_trunc unit quotient seq_colim is_equiv funext pi nat equiv
definition eq_of_homotopy_tr {A : Type} {B : A → Type} {C : Πa, B a → Type}
{f g : Πa, B a} {H : f ~ g} {a : A} (c : C a (f a)) :
eq_of_homotopy H ▸ c = H a ▸ c :=
begin
apply (homotopy.rec_on H),
intro p, apply (eq.rec_on p),
rewrite (left_inv apd10 (refl f))
end
definition eq_of_homotopy_tr2 {A : Type} {B : A → Type} {C : Πa', B a' → Type}
{f g : Πa, B a} (H : f ~ g) (c : Πa, C a (f a)) (a : A) :
(transport _ (eq_of_homotopy H) c) a = H a ▸ c a :=
begin
apply (homotopy.rec_on H),
intro p, apply (eq.rec_on p),
rewrite (left_inv apd10 (refl f))
end
definition ap_eq_idp {A B : Type} (f : A → B) {a : A} {p : a = a} (q : p = idp)
: ap f p = idp :=
ap (ap f) q
definition ap_transport {A : Type} {B : A → Type} {f g : A → A} (p : f ~ g) {i : A → A}
(h : Πa, B a → B (i a)) (a : A) (b : B (f a)) : ap i (p a) ▸ h (f a) b = h (g a) (p a ▸ b) :=
homotopy.rec_on p (λq, eq.rec_on q idp)
definition ap_pathover {A : Type} {B : A → Type} {f g : A → A} (p : f ~ g) {i : A → A}
(h : Πa, B a → B (i a)) (a : A) (b : B (f a)) (b' : B (g a))
(r : pathover B b (p a) b') : pathover B (h (f a) b) (ap i (p a)) (h (g a) b') :=
by induction p; induction r using idp_rec_on; exact idpo
definition inv_con_con_eq_of_eq_con_con_inv {A : Type} {a₁ a₂ b₁ b₂ : A} {p : a₁ = b₁}
{q : a₁ = a₂} {r : a₂ = b₂} {s : b₁ = b₂} (H : q = p ⬝ s ⬝ r⁻¹) : p⁻¹ ⬝ q ⬝ r = s :=
begin
apply con_eq_of_eq_con_inv,
apply inv_con_eq_of_eq_con,
rewrite -con.assoc,
apply H
end
definition eq_con_con_inv_of_inv_con_con_eq {A : Type} {a₁ a₂ b₁ b₂ : A} {p : a₁ = b₁}
{q : a₁ = a₂} {r : a₂ = b₂} {s : b₁ = b₂} (H : p⁻¹ ⬝ q ⬝ r = s) : q = p ⬝ s ⬝ r⁻¹ :=
begin
apply eq_con_inv_of_con_eq,
apply eq_con_of_inv_con_eq,
rewrite -con.assoc,
apply H
end
definition square_of_pi_eq {A B C : Type} (f : A → C) (g : B → C)
{a a' : A} {b b' : B} (p : a = a') (q : b = b') (r : Πa b, f a = g b)
: square (ap f p) (ap g q) (r a b) (r a' b') :=
by cases p; cases q;exact hrfl
theorem is_hprop_elim_self {A : Type} {H : is_hprop A} (x : A) : is_hprop.elim x x = idp :=
!is_hprop.elim
definition is_hset_image_of_is_hprop_image {A B : Type} {f : A → B} {a a' : A} (p q : a = a')
(H : Π(a a' : A), f a = f a') : ap f p = ap f q :=
have H' : Π{b c : A} (r : b = c), !H⁻¹ ⬝ H a c = ap f r, from
(λb c r, eq.rec_on r !con.left_inv),
!H'⁻¹ ⬝ !H'
definition apo011_inv_con {A Z : Type} {B : A → Type} (f : Πa, B a → Z) {a a' a'' : A}
{b : B a} {b' : B a'} {b'' : B a''} (Ha : a' = a) (Ha' : a' = a'')
(Hb : b' =[Ha] b) (Hb' : b' =[Ha'] b'')
: (apo011 f Ha Hb)⁻¹ ⬝ apo011 f Ha' Hb' = apo011 f (Ha⁻¹ ⬝ Ha') (Hb⁻¹ᵒ ⬝o Hb') :=
by cases Hb; cases Hb'; reflexivity
|
46e5c111ef3c59f0940825880192232de8afaea3 | ce6917c5bacabee346655160b74a307b4a5ab620 | /src/ch4/ex0606.lean | 40306c52f9b58788f47681e1ebdc44e350fa3c90 | [] | no_license | Ailrun/Theorem_Proving_in_Lean | ae6a23f3c54d62d401314d6a771e8ff8b4132db2 | 2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68 | refs/heads/master | 1,609,838,270,467 | 1,586,846,743,000 | 1,586,846,743,000 | 240,967,761 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 731 | lean | variables (real : Type) [ordered_ring real]
variables (log exp : real → real)
variable log_exp_eq : ∀ x, log (exp x) = x
variable exp_log_eq : ∀ {x}, x > 0 → exp (log x) = x
variable exp_pos : ∀ x, exp x > 0
variable exp_add : ∀ x y, exp (x + y) = exp x * exp y
-- this ensures the assumptions are available in tactic proofs
include log_exp_eq exp_log_eq exp_pos exp_add
example (x y z : real) : exp (x + y + z) = exp x * exp y * exp z :=
by rw [exp_add, exp_add]
example (y : real) (h : y > 0) : exp (log y) = y :=
exp_log_eq h
theorem log_mul {x y : real} (hx : x > 0) (hy : y > 0) : log (x * y) = log x + log y :=
by rw [←exp_log_eq hx, ←exp_log_eq hy, ←exp_add, log_exp_eq, log_exp_eq, log_exp_eq]
|
4dce9636c8b678cce98b64577f6be95b98f16340 | 761d983a78bc025071bac14a3facced881cf5e53 | /affine/eucl_norm.lean | 0707744c5a84d9fcd20f673ab0d71745e1ada7ef | [] | no_license | rohanrajnair/affine_lib | bcf22ff892cf74ccb36a95bc9b7fff8e0adb2d0d | 83076864245ac547b9d615bc6a23804b1b4a8f70 | refs/heads/master | 1,673,320,928,343 | 1,603,036,653,000 | 1,603,036,653,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 777 | lean | import analysis.normed_space.real_inner_product
import ..affine.aff_coord_space
variable n : ℕ
/-! ### rfl lemmas and prerequisite lemmas -/
/-! ### lemmas necessary to show type class instances -/
/-! ### type class instances -/
--first implement inner product (dot product)
instance : has_inner (aff_vec ℝ n) := sorry
--have to first prove all associated arguments
instance : inner_product_space (aff_vec ℝ n) := sorry
-- feature/euclid
/-!
Goal for Wednesday: fill out this file, put sorry's where you have to
Goal for Saturday: fill out as many sorry's as possible
p.s. see if the dot product is defined somewhere. Might be under the
`linear_algebra` folder, don't want to have to reinvent the wheel
-/
|
ee7b8fedde2188be9ac6e415f07d441beb3633a6 | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/data/pi/lex.lean | c6ac718a57a907a378679a82c27fb6e78507e746 | [
"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 | 5,194 | lean | /-
Copyright (c) 2019 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import order.well_founded
import algebra.group.pi
import order.min_max
/-!
# Lexicographic order on Pi types
This file defines the lexicographic order for Pi types. `a` is less than `b` if `a i = b i` for all
`i` up to some point `k`, and `a k < b k`.
## Notation
* `Πₗ i, α i`: Pi type equipped with the lexicographic order. Type synonym of `Π i, α i`.
## See also
Related files are:
* `data.finset.colex`: Colexicographic order on finite sets.
* `data.list.lex`: Lexicographic order on lists.
* `data.sigma.order`: Lexicographic order on `Σₗ i, α i`.
* `data.psigma.order`: Lexicographic order on `Σₗ' i, α i`.
* `data.prod.lex`: Lexicographic order on `α × β`.
-/
variables {ι : Type*} {β : ι → Type*} (r : ι → ι → Prop)
(s : Π {i}, β i → β i → Prop)
namespace pi
instance {α : Type*} : Π [inhabited α], inhabited (lex α) := id
/-- The lexicographic relation on `Π i : ι, β i`, where `ι` is ordered by `r`,
and each `β i` is ordered by `s`. -/
protected def lex (x y : Π i, β i) : Prop :=
∃ i, (∀ j, r j i → x j = y j) ∧ s (x i) (y i)
/- This unfortunately results in a type that isn't delta-reduced, so we keep the notation out of the
basic API, just in case -/
notation `Πₗ` binders `, ` r:(scoped p, lex (Π i, p i)) := r
@[simp] lemma to_lex_apply (x : Π i, β i) (i : ι) : to_lex x i = x i := rfl
@[simp] lemma of_lex_apply (x : lex (Π i, β i)) (i : ι) : of_lex x i = x i := rfl
lemma is_trichotomous_lex [∀ i, is_trichotomous (β i) s] (wf : well_founded r) :
is_trichotomous (Π i, β i) (pi.lex r @s) :=
{ trichotomous := λ a b,
begin
cases eq_or_ne a b with hab hab,
{ exact or.inr (or.inl hab) },
{ rw function.ne_iff at hab,
let i := wf.min _ hab,
have hri : ∀ j, r j i → a j = b j,
{ intro j, rw ← not_imp_not,
exact λ h', wf.not_lt_min _ _ h' },
have hne : a i ≠ b i, from wf.min_mem _ hab,
cases trichotomous_of s (a i) (b i) with hi hi,
exacts [or.inl ⟨i, hri, hi⟩,
or.inr $ or.inr $ ⟨i, λ j hj, (hri j hj).symm, hi.resolve_left hne⟩] },
end }
instance [has_lt ι] [Π a, has_lt (β a)] : has_lt (lex (Π i, β i)) := ⟨pi.lex (<) (λ _, (<))⟩
instance lex.is_strict_order [linear_order ι] [∀ a, partial_order (β a)] :
is_strict_order (lex (Π i, β i)) (<) :=
{ irrefl := λ a ⟨k, hk₁, hk₂⟩, lt_irrefl (a k) hk₂,
trans :=
begin
rintro a b c ⟨N₁, lt_N₁, a_lt_b⟩ ⟨N₂, lt_N₂, b_lt_c⟩,
rcases lt_trichotomy N₁ N₂ with (H|rfl|H),
exacts [⟨N₁, λ j hj, (lt_N₁ _ hj).trans (lt_N₂ _ $ hj.trans H), lt_N₂ _ H ▸ a_lt_b⟩,
⟨N₁, λ j hj, (lt_N₁ _ hj).trans (lt_N₂ _ hj), a_lt_b.trans b_lt_c⟩,
⟨N₂, λ j hj, (lt_N₁ _ (hj.trans H)).trans (lt_N₂ _ hj), (lt_N₁ _ H).symm ▸ b_lt_c⟩]
end }
instance [linear_order ι] [Π a, partial_order (β a)] : partial_order (lex (Π i, β i)) :=
partial_order_of_SO (<)
/-- `Πₗ i, α i` is a linear order if the original order is well-founded. -/
noncomputable instance [linear_order ι] [is_well_order ι (<)] [∀ a, linear_order (β a)] :
linear_order (lex (Π i, β i)) :=
@linear_order_of_STO' (Πₗ i, β i) (<)
{ to_is_trichotomous := is_trichotomous_lex _ _ is_well_founded.wf } (classical.dec_rel _)
lemma lex.le_of_forall_le [linear_order ι] [is_well_order ι (<)] [Π a, linear_order (β a)]
{a b : lex (Π i, β i)} (h : ∀ i, a i ≤ b i) : a ≤ b :=
le_of_not_lt (λ ⟨i, hi⟩, (h i).not_lt hi.2)
lemma lex.le_of_of_lex_le [linear_order ι] [is_well_order ι (<)] [Π a, linear_order (β a)]
{a b : lex (Π i, β i)} (h : of_lex a ≤ of_lex b) : a ≤ b :=
lex.le_of_forall_le h
lemma to_lex_monotone [linear_order ι] [is_well_order ι (<)] [Π a, linear_order (β a)] :
monotone (@to_lex (Π i, β i)) :=
λ _ _, lex.le_of_forall_le
instance [linear_order ι] [is_well_order ι (<)] [Π a, linear_order (β a)]
[Π a, order_bot (β a)] : order_bot (lex (Π a, β a)) :=
{ bot := to_lex ⊥,
bot_le := λ f, lex.le_of_of_lex_le bot_le }
instance [linear_order ι] [is_well_order ι (<)] [Π a, linear_order (β a)]
[Π a, order_top (β a)] : order_top (lex (Π a, β a)) :=
{ top := to_lex ⊤,
le_top := λ f, lex.le_of_of_lex_le le_top }
instance [linear_order ι] [is_well_order ι (<)] [Π a, linear_order (β a)]
[Π a, bounded_order (β a)] : bounded_order (lex (Π a, β a)) :=
{ .. pi.lex.order_bot, .. pi.lex.order_top }
--we might want the analog of `pi.ordered_cancel_comm_monoid` as well in the future
@[to_additive]
instance lex.ordered_comm_group [linear_order ι] [∀ a, ordered_comm_group (β a)] :
ordered_comm_group (lex (Π i, β i)) :=
{ mul_le_mul_left := λ x y hxy z,
hxy.elim
(λ hxyz, hxyz ▸ le_rfl)
(λ ⟨i, hi⟩,
or.inr ⟨i, λ j hji, show z j * x j = z j * y j, by rw hi.1 j hji,
mul_lt_mul_left' hi.2 _⟩),
..pi.lex.partial_order,
..pi.comm_group }
end pi
|
51e47e219c4973fcf1f693675aab84ce6bd2aba1 | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /tests/lean/run/rewrite12.lean | 45dacfeb57445c4345b7c46f9ecb19ba6b3516bc | [
"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 | 305 | lean | import data.nat
open nat
variables (f : nat → nat → nat → nat) (a b c : nat)
example (H₁ : a = b) (H₂ : f b a b = 0) : f a a a = 0 :=
by rewrite [H₁ at -{2}, H₂]
example (H₁ : a = b) (H₂ : f b a b = 0) (H₃ : c = f a a a) : c = 0 :=
by rewrite [H₁ at H₃ -{2}, H₂ at H₃, H₃]
|
5f457dff5f24c2672b9fd22547707091518b9016 | 31f556cdeb9239ffc2fad8f905e33987ff4feab9 | /stage0/src/Lean/Compiler/LCNF/ToLCNF.lean | 5c321fdf6c936d2f9b281566281f5a787e6cd794 | [
"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 | tobiasgrosser/lean4 | ce0fd9cca0feba1100656679bf41f0bffdbabb71 | ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f | refs/heads/master | 1,673,103,412,948 | 1,664,930,501,000 | 1,664,930,501,000 | 186,870,185 | 0 | 0 | Apache-2.0 | 1,665,129,237,000 | 1,557,939,901,000 | Lean | UTF-8 | Lean | false | false | 26,737 | lean | /-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.ProjFns
import Lean.Compiler.BorrowedAnnotation
import Lean.Compiler.LCNF.Types
import Lean.Compiler.LCNF.Bind
import Lean.Compiler.LCNF.InferType
import Lean.Compiler.LCNF.Util
namespace Lean.Compiler.LCNF
namespace ToLCNF
/--
Return `true` if `e` is a `lcProof` application.
Recall that we use `lcProof` to erase all nested proofs.
-/
def isLCProof (e : Expr) : Bool :=
e.isAppOfArity ``lcProof 1
/-- Create the temporary `lcProof` -/
def mkLcProof (p : Expr) :=
mkApp (mkConst ``lcProof []) p
/--
Auxiliary inductive datatype for constructing LCNF `Code` objects.
The `toLCNF` function maintains a sequence of elements that is eventually
converted into `Code`.
-/
inductive Element where
| jp (decl : FunDecl)
| fun (decl : FunDecl)
| let (decl : LetDecl)
| cases (p : Param) (cases : Cases)
| unreach (p : Param)
deriving Inhabited
/--
State for `BindCasesM` monad
Mapping from `_alt.<idx>` variables to new join points
-/
abbrev BindCasesM.State := FVarIdMap FunDecl
/-- Auxiliary monad for implementing `bindCases` -/
abbrev BindCasesM := StateRefT BindCasesM.State CompilerM
/--
This method returns code that at each exit point of `cases`, it jumps to `jpDecl`.
It is similar to `Code.bind`, but we add special support for `inlineMatcher`.
The `inlineMatcher` function inlines the auxiliary `_match_<idx>` declarations.
To make sure there is no code duplication, `inlineMatcher` creates auxiliary declarations `_alt.<idx>`.
We can say the `_alt.<idx>` declarations are pre join points. For each auxiliary declaration used at
an exit point of `cases`, this method creates an new auxiliary join point that invokes `_alt.<idx>`,
and then jumps to `jpDecl`. The goal is to make sure the auxiliary join point is the only occurrence
of `_alt.<idx>`, then `simp` will inline it.
That is, our goal is to try to promote the pre join points `_alt.<idx>` into a proper join point.
-/
partial def bindCases (jpDecl : FunDecl) (cases : Cases) : CompilerM Code := do
let (alts, s) ← visitAlts cases.alts |>.run {}
let resultType ← mkCasesResultType alts
let result := .cases { cases with alts, resultType }
let result := s.fold (init := result) fun result _ altJp => .jp altJp result
return .jp jpDecl result
where
visitAlts (alts : Array Alt) : BindCasesM (Array Alt) :=
alts.mapM fun alt => return alt.updateCode (← go alt.getCode)
findFun? (f : FVarId) : CompilerM (Option FunDecl) := do
if let some funDecl ← findFunDecl? f then
return funDecl
else if let some { value := .fvar f', .. } ← findLetDecl? f then
findFun? f'
else
return none
go (code : Code) : BindCasesM Code := do
match code with
| .let decl k =>
if let .return fvarId := k then
/-
Check whether the current let-declaration is of the form
```
let _x := _alt.<idx> args
return _x
```
where `_alt.<idx>` is an auxiliary declaration created by `inlineMatcher`
-/
if decl.fvarId == fvarId && decl.value.isApp && decl.value.getAppFn.isFVar then
let f := decl.value.getAppFn.fvarId!
let binderName ← getBinderName f
if binderName.getPrefix == `_alt then
if let some funDecl ← findFun? f then
let args := decl.value.getAppArgs
eraseLetDecl decl
if let some altJp := (← get).find? f then
/- We already have an auxiliary join point for `f`, then, we just use it. -/
return .jmp altJp.fvarId args
else
/-
We have not created a join point for `f` yet.
The join point has the form
```
jp altJp jpParams :=
let _x := f jpParams
jmp jpDecl _x
```
Then, we replace the current `let`-declaration with `jmp altJp args`
-/
let mut jpParams := #[]
let mut subst := {}
let mut jpArgs := #[]
/- Remark: `funDecl.params.size` may be greater than `args.size`. -/
for param in funDecl.params[:args.size] do
let type ← replaceExprFVars param.type subst (translator := true)
let paramNew ← mkAuxParam type
jpParams := jpParams.push paramNew
let arg := .fvar paramNew.fvarId
subst := subst.insert param.fvarId arg
jpArgs := jpArgs.push arg
let letDecl ← mkAuxLetDecl (mkAppN decl.value.getAppFn jpArgs)
let jpValue := .let letDecl (.jmp jpDecl.fvarId #[.fvar letDecl.fvarId])
let altJp ← mkAuxJpDecl jpParams jpValue
modify fun map => map.insert f altJp
return .jmp altJp.fvarId args
let k ← go k
if let some altJp := (← get).find? decl.fvarId then
-- The new join point depends on this variable. Thus, we must insert it here
modify fun s => s.erase decl.fvarId
return .let decl (.jp altJp k)
else
return .let decl k
| .fun decl k => return .fun decl (← go k)
| .jp decl k =>
let value ← go decl.value
let type ← value.inferParamType decl.params
let decl ← decl.update' type value
return .jp decl (← go k)
| .cases c =>
let alts ← c.alts.mapM fun
| .alt ctorName params k => return .alt ctorName params (← go k)
| .default k => return .default (← go k)
if alts.isEmpty then
throwError "`Code.bind` failed, empty `cases` found"
let resultType ← mkCasesResultType alts
return .cases { c with alts, resultType }
| .return fvarId => return .jmp jpDecl.fvarId #[.fvar fvarId]
| .jmp .. | .unreach .. => return code
def seqToCode (seq : Array Element) (e : Expr) : CompilerM Code := do
if let .fvar fvarId := e then
go seq seq.size (.return fvarId)
else
let decl ← mkAuxLetDecl e
let seq := seq.push (.let decl)
go seq seq.size (.return decl.fvarId)
where
go (seq : Array Element) (i : Nat) (c : Code) : CompilerM Code := do
if i > 0 then
match seq[i-1]! with
| .jp decl => go seq (i - 1) (.jp decl c)
| .fun decl => go seq (i - 1) (.fun decl c)
| .let decl => go seq (i - 1) (.let decl c)
| .unreach _ =>
let type ← c.inferType
eraseCode c
seq[:i].forM fun
| .let decl => eraseLetDecl decl
| .jp decl | .fun decl => eraseFunDecl decl
| .cases _ cs => eraseCode (.cases cs)
| .unreach auxParam => eraseParam auxParam
return .unreach type
| .cases auxParam cases =>
if let .return fvarId := c then
if auxParam.fvarId == fvarId then
eraseParam auxParam
go seq (i - 1) (.cases cases)
else
-- `cases` is dead code
go seq (i - 1) c
else
/- Create a join point for `c` and jump to it from `cases` -/
let jpDecl ← mkAuxJpDecl' auxParam c
go seq (i - 1) (← bindCases jpDecl cases)
else
return c
structure State where
/-- Local context containing the original Lean types (not LCNF ones). -/
lctx : LocalContext := {}
/-- Cache from Lean regular expression to LCNF expression. -/
cache : PHashMap Expr Expr := {}
/-- `toLCNFType` cache -/
typeCache : HashMap Expr Expr := {}
/-- isTypeFormerType cache -/
isTypeFormerTypeCache : HashMap Expr Bool := {}
/-- LCNF sequence, we chain it to create a LCNF `Code` object. -/
seq : Array Element := #[]
/--
Fields that are type formers must be replaced with `lcAny`
in the resulting code. Otherwise, we have data occurring in
types.
-/
toAny : FVarIdSet := {}
abbrev M := StateRefT State CompilerM
@[inline] def liftMetaM (x : MetaM α) : M α := do
x.run' { lctx := (← get).lctx }
/-- Create `Code` that executes the current `seq` and then returns `e` -/
def toCode (e : Expr) : M Code := do
seqToCode (← get).seq e
/-- Add LCNF element to the current sequence -/
def pushElement (elem : Element) : M Unit := do
modify fun s => { s with seq := s.seq.push elem }
def mkUnreachable (type : Expr) : M Expr := do
let p ← mkAuxParam type
pushElement (.unreach p)
return .fvar p.fvarId
def mkAuxLetDecl (e : Expr) (prefixName := `_x) : M Expr := do
if e.isFVar then
return e
else
let letDecl ← mkLetDecl (← mkFreshBinderName prefixName) (← inferType e) e
pushElement (.let letDecl)
return .fvar letDecl.fvarId
def run (x : M α) : CompilerM α :=
x |>.run' {}
/--
Return true iff `type` is `Sort _` or `As → Sort _`.
-/
private partial def isTypeFormerType (type : Expr) : M Bool := do
match quick (← getEnv) type with
| .true => return true
| .false => return false
| .undef =>
if let some result := (← get).isTypeFormerTypeCache.find? type then
return result
let result ← liftMetaM <| Meta.isTypeFormerType type
modify fun s => { s with isTypeFormerTypeCache := s.isTypeFormerTypeCache.insert type result }
return result
where
quick (env : Environment) : Expr → LBool
| .forallE _ _ b _ => quick env b
| .mdata _ b => quick env b
| .letE .. => .undef
| .sort _ => .true
| .bvar .. => .false
| type =>
match type.getAppFn with
| .bvar .. => .false
| .const declName _ =>
if let some (.inductInfo ..) := env.find? declName then
.false
else
.undef
| _ => .undef
def withNewScope (x : M α) : M α := do
let saved ← get
-- typeCache and isTypeFormerTypeCache are not backtrackable
let saved := { saved with typeCache := {}, isTypeFormerTypeCache := {} }
modify fun s => { s with seq := #[] }
try
x
finally
let saved := { saved with
typeCache := (← get).typeCache
isTypeFormerTypeCache := (← get).isTypeFormerTypeCache
}
set saved
/-
Replace free variables in `type'` that occur in `toAny` into `lcAny`.
Recall that we populate `toAny` with the free variable ids of fields that
are type formers. This can happen when we have a field whose type is, for example, `Type u`.
-/
def applyToAny (type : Expr) : M Expr := do
let toAny := (← get).toAny
return type.replace fun
| .fvar fvarId => if toAny.contains fvarId then some anyTypeExpr else none
| _ => none
def toLCNFType (type : Expr) : M Expr := do
match (← get).typeCache.find? type with
| some type' => return type'
| none =>
let type' ← liftMetaM <| LCNF.toLCNFType type
let type' ← applyToAny type'
modify fun s => { s with typeCache := s.typeCache.insert type type' }
return type'
def cleanupBinderName (binderName : Name) : CompilerM Name :=
if binderName.hasMacroScopes then
let binderName := binderName.eraseMacroScopes
mkFreshBinderName binderName
else
return binderName
/-- Create a new local declaration using a Lean regular type. -/
def mkParam (binderName : Name) (type : Expr) : M Param := do
let binderName ← cleanupBinderName binderName
let borrow := isMarkedBorrowed type
let type' ← toLCNFType type
let param ← LCNF.mkParam binderName type' borrow
modify fun s => { s with lctx := s.lctx.mkLocalDecl param.fvarId binderName type .default }
return param
def mkLetDecl (binderName : Name) (type : Expr) (value : Expr) (type' : Expr) (value' : Expr) : M LetDecl := do
let binderName ← cleanupBinderName binderName
let letDecl ← LCNF.mkLetDecl binderName type' value'
modify fun s => { s with
lctx := s.lctx.mkLetDecl letDecl.fvarId binderName type value false
seq := s.seq.push <| .let letDecl
}
return letDecl
def visitLambda (e : Expr) : M (Array Param × Expr) :=
go e #[] #[]
where
go (e : Expr) (xs : Array Expr) (ps : Array Param) := do
if let .lam binderName type body _ := e then
let type := type.instantiateRev xs
let p ← mkParam binderName type
go body (xs.push p.toExpr) (ps.push p)
else
return (ps, e.instantiateRev xs)
def visitBoundedLambda (e : Expr) (n : Nat) : M (Array Param × Expr) :=
go e n #[] #[]
where
go (e : Expr) (n : Nat) (xs : Array Expr) (ps : Array Param) := do
if n == 0 then
return (ps, e.instantiateRev xs)
else if let .lam binderName type body _ := e then
let type := type.instantiateRev xs
let p ← mkParam binderName type
go body (n-1) (xs.push p.toExpr) (ps.push p)
else
return (ps, e.instantiateRev xs)
def mustEtaExpand (env : Environment) (e : Expr) : Bool :=
if let .const declName _ := e.getAppFn then
match env.find? declName with
| some (.recInfo ..) | some (.ctorInfo ..) | some (.quotInfo ..) => true
| _ => isCasesOnRecursor env declName || isNoConfusion env declName || env.isProjectionFn declName || declName == ``Eq.ndrec
else
false
/--
Eta-expand with `n` lambdas.
-/
def etaExpandN (e : Expr) (n : Nat) : M Expr := do
if n == 0 then
return e
else liftMetaM do
Meta.forallBoundedTelescope (← Meta.inferType e) n fun xs _ =>
Meta.mkLambdaFVars xs (mkAppN e xs)
/--
Eta reduce implicits. We use this function to eliminate introduced by the implicit lambda feature,
where it generates terms such as `fun {α} => ReaderT.pure`
-/
partial def etaReduceImplicit (e : Expr) : Expr :=
match e with
| .lam _ d b bi =>
if bi.isImplicit then
let b' := etaReduceImplicit b
match b' with
| .app f (.bvar 0) =>
if !f.hasLooseBVar 0 then
f.lowerLooseBVars 1 1
else
e.updateLambdaE! d b'
| _ => e.updateLambdaE! d b'
else
e
| _ => e
/--
Put the given expression in `LCNF`.
- Nested proofs are replaced with `lcProof`-applications.
- Eta-expand applications of declarations that satisfy `shouldEtaExpand`.
- Put computationally relevant expressions in A-normal form.
-/
partial def toLCNF (e : Expr) : CompilerM Code := do
run do
let e ← visit e
toCode e
where
visitCore (e : Expr) : M Expr := withIncRecDepth do
if let some e := (← get).cache.find? e then
return e
let r ← match e with
| .app .. => visitApp e
| .const .. => visitApp e
| .proj s i e => visitProj s i e
| .mdata d e => visitMData d e
| .lam .. => visitLambda e
| .letE .. => visitLet e #[]
| .lit .. => mkAuxLetDecl e
| .forallE .. => unreachable!
| .mvar .. => throwError "unexpected occurrence of metavariable in code generator{indentExpr e}"
| .bvar .. => unreachable!
| .fvar fvarId => if (← get).toAny.contains fvarId then pure anyTypeExpr else pure e
| _ => pure e
modify fun s => { s with cache := s.cache.insert e r }
return r
visit (e : Expr) : M Expr := withIncRecDepth do
if isLCProof e then
return erasedExpr
let type ← liftMetaM <| Meta.inferType e
if (← liftMetaM <| Meta.isProp type) then
/- We erase proofs. -/
return erasedExpr
if (← isTypeFormerType type) then
/-
We erase type formers unless they occur as application arguments.
Recall that we usually do not generate code for functions that return type,
by this branch can be reachable if we cannot establish whether the function produces a type or not.
See `visitAppArg`
-/
return erasedExpr
visitCore e
visitAppArg (e : Expr) : M Expr := do
if isLCProof e then
return erasedExpr
let type ← liftMetaM <| Meta.inferType e
if (← liftMetaM <| Meta.isProp type) then
/- We erase proofs. -/
return erasedExpr
if (← isTypeFormerType type) then
/- Predicates are erased (e.g., `Eq`) -/
if isPredicateType (← toLCNFType type) then
return erasedExpr
else
/- Types and Type formers are not put into A-normal form -/
toLCNFType e
else
visitCore e
/-- Visit args, and return `f args` -/
visitAppDefault (f : Expr) (args : Array Expr) : M Expr := do
if f.isErased then
return f
else
let args ← args.mapM visitAppArg
mkAuxLetDecl <| mkAppN f args
/-- Eta expand if under applied, otherwise apply k -/
etaIfUnderApplied (e : Expr) (arity : Nat) (k : M Expr) : M Expr := do
let numArgs := e.getAppNumArgs
if numArgs < arity then
visit (← etaExpandN e (arity - numArgs))
else
k
/--
If `args.size == arity`, then just return `app`.
Otherwise return
```
let k := app
k args[arity:]
```
-/
mkOverApplication (app : Expr) (args : Array Expr) (arity : Nat) : M Expr := do
if args.size == arity then
mkAuxLetDecl app
else
let k ← mkAuxLetDecl app
let mut args := args
for i in [arity : args.size] do
args ← args.modifyM i visitAppArg
mkAuxLetDecl (mkAppN k args[arity:])
/--
Visit a `matcher`/`casesOn` alternative.
-/
visitAlt (ctorName : Name) (numParams : Nat) (e : Expr) : M (Expr × Alt) := do
withNewScope do
let mut (ps, e) ← visitBoundedLambda e numParams
if ps.size < numParams then
e ← etaExpandN e (numParams - ps.size)
let (ps', e') ← ToLCNF.visitLambda e
ps := ps ++ ps'
e := e'
/-
Insert the free variable ids of fields that are type formers into `toAny`.
Recall that we do not want to have "data" occurring in types.
-/
ps ← ps.mapM fun p => do
let type ← inferType p.toExpr
if (← isTypeFormerType type) then
trace[Meta.debug] "{p.binderName} is type former"
modify fun s => { s with toAny := s.toAny.insert p.fvarId }
/-
Recall that we may have dependent fields. Example:
```
| ctor (α : Type u) (as : List α) => ...
```
and we must use `applyToAny` to make sure the field `α` (which is data) does
not occur in the type of `as : List α`.
-/
p.update (← applyToAny p.type)
let c ← toCode (← visit e)
let altType ← c.inferType
return (altType, .alt ctorName ps c)
visitCases (casesInfo : CasesInfo) (e : Expr) : M Expr :=
etaIfUnderApplied e casesInfo.arity do
let args := e.getAppArgs
let mut resultType ← toLCNFType (← liftMetaM do Meta.inferType (mkAppN e.getAppFn args[:casesInfo.arity]))
if casesInfo.numAlts == 0 then
/- `casesOn` of an empty type. -/
mkUnreachable resultType
else
let mut alts := #[]
let typeName := casesInfo.declName.getPrefix
let discr ← visitAppArg args[casesInfo.discrPos]!
let .inductInfo indVal ← getConstInfo typeName | unreachable!
for i in casesInfo.altsRange, numParams in casesInfo.altNumParams, ctorName in indVal.ctors do
let (altType, alt) ← visitAlt ctorName numParams args[i]!
unless (← compatibleTypes altType resultType) do
resultType := anyTypeExpr
alts := alts.push alt
let cases : Cases := { typeName, discr := discr.fvarId!, resultType, alts }
let auxDecl ← mkAuxParam resultType
pushElement (.cases auxDecl cases)
let result := .fvar auxDecl.fvarId
if args.size == casesInfo.arity then
return result
else
mkOverApplication result args casesInfo.arity
visitCtor (arity : Nat) (e : Expr) : M Expr :=
etaIfUnderApplied e arity do
visitAppDefault e.getAppFn e.getAppArgs
visitQuotLift (e : Expr) : M Expr := do
let arity := 6
etaIfUnderApplied e arity do
let mut args := e.getAppArgs
let α := args[0]!
let r := args[1]!
let f ← visitAppArg args[3]!
let q ← visitAppArg args[5]!
let .const _ [u, _] := e.getAppFn | unreachable!
let invq ← mkAuxLetDecl (mkApp3 (.const ``Quot.lcInv [u]) α r q)
let r := mkApp f invq
mkOverApplication r args arity
visitEqRec (e : Expr) : M Expr :=
let arity := 6
etaIfUnderApplied e arity do
let args := e.getAppArgs
let f := e.getAppFn
let recType ← toLCNFType (← liftMetaM do Meta.inferType (mkAppN f args[:arity]))
let minor := if e.isAppOf ``Eq.rec || e.isAppOf ``Eq.ndrec then args[3]! else args[5]!
let minor ← visit minor
let minorType ← inferType minor
let cast ← if (← compatibleTypes minorType recType) then
-- Recall that many types become compatible after LCNF conversion
-- Example: `Fin 10` and `Fin n`
pure minor
else
mkLcCast (← mkAuxLetDecl minor) recType
mkOverApplication cast args arity
visitFalseRec (e : Expr) : M Expr :=
let arity := 2
etaIfUnderApplied e arity do
let type ← toLCNFType (← liftMetaM do Meta.inferType e)
mkUnreachable type
visitAndRec (e : Expr) : M Expr :=
let arity := 5
etaIfUnderApplied e arity do
let args := e.getAppArgs
let ha := mkLcProof args[0]! -- We should not use `lcErased` here since we use it to create a pre-LCNF Expr.
let hb := mkLcProof args[1]!
let minor := if e.isAppOf ``And.rec then args[3]! else args[4]!
let minor := minor.beta #[ha, hb]
visit (mkAppN minor args[arity:])
visitNoConfusion (e : Expr) : M Expr := do
let .const declName _ := e.getAppFn | unreachable!
let typeName := declName.getPrefix
let .inductInfo inductVal ← getConstInfo typeName | unreachable!
let arity := inductVal.numParams + inductVal.numIndices + 1 /- motive -/ + 2 /- lhs/rhs-/ + 1 /- equality -/
etaIfUnderApplied e arity do
let args := e.getAppArgs
let lhs ← liftMetaM do Meta.whnf args[inductVal.numParams + inductVal.numIndices + 1]!
let rhs ← liftMetaM do Meta.whnf args[inductVal.numParams + inductVal.numIndices + 2]!
let lhs := lhs.toCtorIfLit
let rhs := rhs.toCtorIfLit
match lhs.isConstructorApp? (← getEnv), rhs.isConstructorApp? (← getEnv) with
| some lhsCtorVal, some rhsCtorVal =>
if lhsCtorVal.name == rhsCtorVal.name then
etaIfUnderApplied e (arity+1) do
let major := args[arity]!
let major ← expandNoConfusionMajor major lhsCtorVal.numFields
let major := mkAppN major args[arity+1:]
visit major
else
mkUnreachable (← inferType e)
| _, _ =>
throwError "code generator failed, unsupported occurrence of `{declName}`"
expandNoConfusionMajor (major : Expr) (numFields : Nat) : M Expr := do
match numFields with
| 0 => return major
| n+1 =>
if let .lam _ d b _ := major then
let proof := mkLcProof d
expandNoConfusionMajor (b.instantiate1 proof) n
else
expandNoConfusionMajor (← etaExpandN major (n+1)) (n+1)
visitProjFn (projInfo : ProjectionFunctionInfo) (e : Expr) : M Expr := do
let typeName := projInfo.ctorName.getPrefix
if isRuntimeBultinType typeName then
let numArgs := e.getAppNumArgs
let arity := projInfo.numParams + 1
if numArgs < arity then
visit (← etaExpandN e (arity - numArgs))
else
visitAppDefault e.getAppFn e.getAppArgs
else
let .const declName us := e.getAppFn | unreachable!
let info ← getConstInfo declName
let f ← Core.instantiateValueLevelParams info us
visit (f.beta e.getAppArgs)
visitApp (e : Expr) : M Expr := do
if let .const declName _ := e.getAppFn then
if declName == ``Quot.lift then
visitQuotLift e
else if declName == ``Quot.mk then
visitCtor 3 e
else if declName == ``Eq.casesOn || declName == ``Eq.rec || declName == ``Eq.ndrec then
visitEqRec e
else if declName == ``And.rec || declName == ``And.casesOn then
visitAndRec e
else if declName == ``False.rec || declName == ``Empty.rec || declName == ``False.casesOn || declName == ``Empty.casesOn then
visitFalseRec e
else if let some casesInfo ← getCasesInfo? declName then
visitCases casesInfo e
else if let some arity ← getCtorArity? declName then
visitCtor arity e
else if isNoConfusion (← getEnv) declName then
visitNoConfusion e
else if let some projInfo ← getProjectionFnInfo? declName then
visitProjFn projInfo e
else
e.withApp visitAppDefault
else
e.withApp fun f args => do visitAppDefault (← visit f) args
visitLambda (e : Expr) : M Expr := do
let b := etaReduceImplicit e
/-
Note: we don't want to eta-reduce arbitrary lambda expressions since it can
affect the current inline heuristics. For example, suppose that `foo` is marked
as `[inline]`. If we eta-reduce
```
let f := fun b => foo a b
```
we obtain the LCNF
```
let f := foo a
```
which will be inlined everywhere in the current implementation, if we don't eta-reduce,
we obtain
```
fun f b := foo a
```
which will inline foo in the body of `f`, but will only inline `f` if it is small.
-/
if !b.isLambda && !mustEtaExpand (← getEnv) b then
/-
We use eta-reduction to make sure we avoid the overhead introduced by
the implicit lambda feature when declaring instances.
Example: `fun {α} => ReaderT.pure
-/
visit b
else
let funDecl ← withNewScope do
let (ps, e) ← ToLCNF.visitLambda e
let e ← visit e
let c ← toCode e
mkAuxFunDecl ps c
pushElement (.fun funDecl)
return .fvar funDecl.fvarId
visitMData (mdata : MData) (e : Expr) : M Expr := do
if isCompilerRelevantMData mdata then
mkAuxLetDecl <| .mdata mdata (← visit e)
else
visit e
visitProj (s : Name) (i : Nat) (e : Expr) : M Expr := do
mkAuxLetDecl <| .proj s i (← visit e)
visitLet (e : Expr) (xs : Array Expr) : M Expr := do
match e with
| .letE binderName type value body _ =>
let type := type.instantiateRev xs
let value := value.instantiateRev xs
if (← (liftMetaM <| Meta.isProp type) <||> isTypeFormerType type) then
visitLet body (xs.push value)
else
let type' ← toLCNFType type
let value' ← visit value
let letDecl ← mkLetDecl binderName type value type' value'
visitLet body (xs.push (.fvar letDecl.fvarId))
| _ =>
let e := e.instantiateRev xs
visit e
end ToLCNF
export ToLCNF (toLCNF)
end Lean.Compiler.LCNF
|
9adfeb561fe035fde3a410d62472a68b50795902 | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /src/Init/Lean/Meta/Tactic/Assumption.lean | d874b4415e16d8f95eb4369c31237e11f91e2527 | [
"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 | 791 | 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.Lean.Meta.ExprDefEq
import Init.Lean.Meta.Tactic.Util
namespace Lean
namespace Meta
def assumptionAux (mvarId : MVarId) : MetaM Bool :=
withMVarContext mvarId $ do
checkNotAssigned mvarId `assumption;
mvarType ← getMVarType mvarId;
lctx ← getLCtx;
h? ← lctx.findDeclRevM? $ fun decl => condM (isDefEq mvarType decl.type) (pure (some decl.toExpr)) (pure none);
match h? with
| some h => do assignExprMVar mvarId h; pure true
| none => pure false
def assumption (mvarId : MVarId) : MetaM Unit :=
unlessM (assumptionAux mvarId) $ throwTacticEx `assumption mvarId ""
end Meta
end Lean
|
78ebb58a6bf625e00e9314b9243273c2416e80db | 3dd1b66af77106badae6edb1c4dea91a146ead30 | /tests/lean/run/univ2.lean | 786d1a8c980e4e5ea74291c184e899e3cc417df3 | [
"Apache-2.0"
] | permissive | silky/lean | 79c20c15c93feef47bb659a2cc139b26f3614642 | df8b88dca2f8da1a422cb618cd476ef5be730546 | refs/heads/master | 1,610,737,587,697 | 1,406,574,534,000 | 1,406,574,534,000 | 22,362,176 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 278 | lean | import standard
hypothesis I : Type
definition F (X : Type) : Type := (X → Prop) → Prop
hypothesis unfold : I → F I
hypothesis fold : F I → I
hypothesis iso1 : ∀x, fold (unfold x) = x
variable sorry {A : Type} : A
theorem iso2 : ∀x, fold (unfold x) = x
:= sorry
|
6e63b65be164489fb78b30a8577861eab351b485 | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/analysis/normed_space/multilinear.lean | 215cdacd8b096b4ab4ccbd068ceef5d89a680403 | [
"Apache-2.0"
] | permissive | dupuisf/mathlib | 62de4ec6544bf3b79086afd27b6529acfaf2c1bb | 8582b06b0a5d06c33ee07d0bdf7c646cae22cf36 | refs/heads/master | 1,669,494,854,016 | 1,595,692,409,000 | 1,595,692,409,000 | 272,046,630 | 0 | 0 | Apache-2.0 | 1,592,066,143,000 | 1,592,066,142,000 | null | UTF-8 | Lean | false | false | 54,472 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import analysis.normed_space.operator_norm
import topology.algebra.multilinear
/-!
# Operator norm on the space of continuous multilinear maps
When `f` is a continuous multilinear map in finitely many variables, we define its norm `∥f∥` as the
smallest number such that `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥` for all `m`.
We show that it is indeed a norm, and prove its basic properties.
## Main results
Let `f` be a multilinear map in finitely many variables.
* `exists_bound_of_continuous` asserts that, if `f` is continuous, then there exists `C > 0`
with `∥f m∥ ≤ C * ∏ i, ∥m i∥` for all `m`.
* `continuous_of_bound`, conversely, asserts that this bound implies continuity.
* `mk_continuous` constructs the associated continuous multilinear map.
Let `f` be a continuous multilinear map in finitely many variables.
* `∥f∥` is its norm, i.e., the smallest number such that `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥` for
all `m`.
* `le_op_norm f m` asserts the fundamental inequality `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥`.
* `norm_image_sub_le_of_bound f m₁ m₂` gives a control of the difference `f m₁ - f m₂` in terms of
`∥f∥` and `∥m₁ - m₂∥`.
We also register isomorphisms corresponding to currying or uncurrying variables, transforming a
continuous multilinear function `f` in `n+1` variables into a continuous linear function taking
values in continuous multilinear functions in `n` variables, and also into a continuous multilinear
function in `n` variables taking values in continuous linear functions. These operations are called
`f.curry_left` and `f.curry_right` respectively (with inverses `f.uncurry_left` and
`f.uncurry_right`). They induce continuous linear equivalences between spaces of
continuous multilinear functions in `n+1` variables and spaces of continuous linear functions into
continuous multilinear functions in `n` variables (resp. continuous multilinear functions in `n`
variables taking values in continuous linear functions), called respectively
`continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`.
## Implementation notes
We mostly follow the API (and the proofs) of `operator_norm.lean`, with the additional complexity
that we should deal with multilinear maps in several variables. The currying/uncurrying
constructions are based on those in `multilinear.lean`.
From the mathematical point of view, all the results follow from the results on operator norm in
one variable, by applying them to one variable after the other through currying. However, this
is only well defined when there is an order on the variables (for instance on `fin n`) although
the final result is independent of the order. While everything could be done following this
approach, it turns out that direct proofs are easier and more efficient.
-/
noncomputable theory
open_locale classical big_operators
open finset
local attribute [instance, priority 1001]
add_comm_group.to_add_comm_monoid normed_group.to_add_comm_group normed_space.to_semimodule
universes u v w w₁ w₂ wG
variables {𝕜 : Type u} {ι : Type v} {n : ℕ}
{G : Type wG} {E : fin n.succ → Type w} {E₁ : ι → Type w₁} {E₂ : Type w₂}
[decidable_eq ι] [fintype ι] [nondiscrete_normed_field 𝕜]
[normed_group G] [∀i, normed_group (E i)] [∀i, normed_group (E₁ i)] [normed_group E₂]
[normed_space 𝕜 G] [∀i, normed_space 𝕜 (E i)] [∀i, normed_space 𝕜 (E₁ i)] [normed_space 𝕜 E₂]
/-!
### Continuity properties of multilinear maps
We relate continuity of multilinear maps to the inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥`, in
both directions. Along the way, we prove useful bounds on the difference `∥f m₁ - f m₂∥`.
-/
namespace multilinear_map
variable (f : multilinear_map 𝕜 E₁ E₂)
/-- If a multilinear map in finitely many variables on normed spaces is continuous, then it
satisfies the inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥`, for some `C` which can be chosen to be
positive. -/
theorem exists_bound_of_continuous (hf : continuous f) :
∃ (C : ℝ), 0 < C ∧ (∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) :=
begin
/- The proof only uses the continuity at `0`. Then, given a general point `m`, rescale each of
its coordinates to bring them to a shell of fixed width around `0`, on which one knows that `f` is
bounded, and then use the multiplicativity of `f` along each coordinate to deduce the desired
bound.-/
obtain ⟨ε, ε_pos, hε⟩ : ∃ ε > 0, ∀{m}, dist m 0 < ε → dist (f m) (f 0) < 1 :=
metric.tendsto_nhds_nhds.1 hf.continuous_at 1 zero_lt_one,
let δ := ε/2,
have δ_pos : δ > 0 := half_pos ε_pos,
/- On points of size at most `δ`, `f` is bounded (by `1 + ∥f 0∥`). -/
have H : ∀{a}, ∥a∥ ≤ δ → ∥f a∥ ≤ 1 + ∥f 0∥,
{ assume a ha,
have : dist (f a) (f 0) ≤ 1,
{ apply le_of_lt (hε _),
rw [dist_eq_norm, sub_zero],
exact lt_of_le_of_lt ha (half_lt_self ε_pos) },
calc ∥f a∥ = dist (f a) 0 : (dist_zero_right _).symm
... ≤ dist (f a) (f 0) + dist (f 0) 0 : dist_triangle _ _ _
... ≤ 1 + ∥f 0∥ : by { rw dist_zero_right, exact add_le_add_right this _ } },
obtain ⟨c, hc⟩ : ∃c : 𝕜, 1 < ∥c∥ := normed_field.exists_one_lt_norm 𝕜,
set C := (1 + ∥f 0∥) * ∏ i : ι, (δ⁻¹ * ∥c∥),
have C_pos : 0 < C :=
mul_pos (lt_of_lt_of_le zero_lt_one (by simp))
(prod_pos (λi hi, mul_pos (inv_pos.2 δ_pos) (lt_of_le_of_lt zero_le_one hc))),
refine ⟨C, C_pos, λm, _⟩,
/- Given a general point `m`, rescale each coordinate to bring it to `[δ/∥c∥, δ]` by multiplication
by a power of a scalar `c` with norm `∥c∥ > 1`.-/
by_cases h : ∃i, m i = 0,
{ rcases h with ⟨i, hi⟩,
rw [f.map_coord_zero i hi, _root_.norm_zero],
exact mul_nonneg (le_of_lt C_pos) (prod_nonneg (λi hi, norm_nonneg _)) },
{ push_neg at h,
have : ∀i, ∃d:𝕜, d ≠ 0 ∧ ∥d • m i∥ ≤ δ ∧ (δ/∥c∥ ≤ ∥d • m i∥) ∧ (∥d∥⁻¹ ≤ δ⁻¹ * ∥c∥ * ∥m i∥) :=
λi, rescale_to_shell hc δ_pos (h i),
choose d hd using this,
have A : 0 ≤ 1 + ∥f 0∥ := add_nonneg zero_le_one (norm_nonneg _),
have B : ∀ (i : ι), i ∈ univ → 0 ≤ ∥d i∥⁻¹ := λi hi, by simp,
-- use the bound on `f` on the ball of size `δ` to conclude.
calc
∥f m∥ = ∥f (λi, (d i)⁻¹ • (d i • m i))∥ :
by { unfold_coes, congr, ext i, rw [← mul_smul, inv_mul_cancel (hd i).1, one_smul] }
... = ∥(∏ i, (d i)⁻¹) • f (λi, d i • m i)∥ : by rw f.map_smul_univ
... = (∏ i, ∥d i∥⁻¹) * ∥f (λi, d i • m i)∥ :
by { rw [norm_smul, normed_field.norm_prod], congr, ext i, rw normed_field.norm_inv }
... ≤ (∏ i, ∥d i∥⁻¹) * (1 + ∥f 0∥) :
mul_le_mul_of_nonneg_left (H ((pi_norm_le_iff (le_of_lt δ_pos)).2 (λi, (hd i).2.1)))
(prod_nonneg B)
... ≤ (∏ i, δ⁻¹ * ∥c∥ * ∥m i∥) * (1 + ∥f 0∥) :
mul_le_mul_of_nonneg_right (prod_le_prod B (λi hi, (hd i).2.2.2)) A
... = (∏ i : ι, δ⁻¹ * ∥c∥) * (∏ i, ∥m i∥) * (1 + ∥f 0∥) :
by rw prod_mul_distrib
... = C * (∏ i, ∥m i∥) :
by rw [mul_comm, ← mul_assoc] }
end
/-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f m₂`
using the multilinearity. Here, we give a precise but hard to use version. See
`norm_image_sub_le_of_bound` for a less precise but more usable version. The bound reads
`∥f m - f m'∥ ≤ C * ∥m 1 - m' 1∥ * max ∥m 2∥ ∥m' 2∥ * max ∥m 3∥ ∥m' 3∥ * ... * max ∥m n∥ ∥m' n∥ + ...`,
where the other terms in the sum are the same products where `1` is replaced by any `i`. -/
lemma norm_image_sub_le_of_bound' {C : ℝ} (hC : 0 ≤ C)
(H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) (m₁ m₂ : Πi, E₁ i) :
∥f m₁ - f m₂∥ ≤
C * ∑ i, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ :=
begin
have A : ∀(s : finset ι), ∥f m₁ - f (s.piecewise m₂ m₁)∥
≤ C * ∑ i in s, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥,
{ refine finset.induction (by simp) _,
assume i s his Hrec,
have I : ∥f (s.piecewise m₂ m₁) - f ((insert i s).piecewise m₂ m₁)∥
≤ C * ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥,
{ have A : ((insert i s).piecewise m₂ m₁)
= function.update (s.piecewise m₂ m₁) i (m₂ i) := s.piecewise_insert _ _ _,
have B : s.piecewise m₂ m₁ = function.update (s.piecewise m₂ m₁) i (m₁ i),
{ ext j,
by_cases h : j = i,
{ rw h, simp [his] },
{ simp [h] } },
rw [B, A, ← f.map_sub],
apply le_trans (H _) (mul_le_mul_of_nonneg_left _ hC),
refine prod_le_prod (λj hj, norm_nonneg _) (λj hj, _),
by_cases h : j = i,
{ rw h, simp },
{ by_cases h' : j ∈ s;
simp [h', h, le_refl] } },
calc ∥f m₁ - f ((insert i s).piecewise m₂ m₁)∥ ≤
∥f m₁ - f (s.piecewise m₂ m₁)∥ + ∥f (s.piecewise m₂ m₁) - f ((insert i s).piecewise m₂ m₁)∥ :
by { rw [← dist_eq_norm, ← dist_eq_norm, ← dist_eq_norm], exact dist_triangle _ _ _ }
... ≤ (C * ∑ i in s, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥)
+ C * ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ :
add_le_add Hrec I
... = C * ∑ i in insert i s, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ :
by simp [his, add_comm, left_distrib] },
convert A univ,
simp
end
/-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f m₂`
using the multilinearity. Here, we give a usable but not very precise version. See
`norm_image_sub_le_of_bound'` for a more precise but less usable version. The bound is
`∥f m - f m'∥ ≤ C * card ι * ∥m - m'∥ * (max ∥m∥ ∥m'∥) ^ (card ι - 1)`. -/
lemma norm_image_sub_le_of_bound {C : ℝ} (hC : 0 ≤ C)
(H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) (m₁ m₂ : Πi, E₁ i) :
∥f m₁ - f m₂∥ ≤ C * (fintype.card ι) * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) * ∥m₁ - m₂∥ :=
begin
have A : ∀ (i : ι), ∏ j, (if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥)
≤ ∥m₁ - m₂∥ * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1),
{ assume i,
calc ∏ j, (if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥)
≤ ∏ j : ι, function.update (λ j, max ∥m₁∥ ∥m₂∥) i (∥m₁ - m₂∥) j :
begin
apply prod_le_prod,
{ assume j hj, by_cases h : j = i; simp [h, norm_nonneg] },
{ assume j hj,
by_cases h : j = i,
{ rw h, simp, exact norm_le_pi_norm (m₁ - m₂) i },
{ simp [h, max_le_max, norm_le_pi_norm] } }
end
... = ∥m₁ - m₂∥ * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) :
by { rw prod_update_of_mem (finset.mem_univ _), simp [card_univ_diff] } },
calc
∥f m₁ - f m₂∥
≤ C * ∑ i, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ :
f.norm_image_sub_le_of_bound' hC H m₁ m₂
... ≤ C * ∑ i, ∥m₁ - m₂∥ * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) :
mul_le_mul_of_nonneg_left (sum_le_sum (λi hi, A i)) hC
... = C * (fintype.card ι) * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) * ∥m₁ - m₂∥ :
by { rw [sum_const, card_univ, nsmul_eq_mul], ring }
end
/-- If a multilinear map satisfies an inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥`, then it is
continuous. -/
theorem continuous_of_bound (C : ℝ) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) :
continuous f :=
begin
let D := max C 1,
have D_pos : 0 ≤ D := le_trans zero_le_one (le_max_right _ _),
replace H : ∀ m, ∥f m∥ ≤ D * ∏ i, ∥m i∥,
{ assume m,
apply le_trans (H m) (mul_le_mul_of_nonneg_right (le_max_left _ _) _),
exact prod_nonneg (λ(i : ι) hi, norm_nonneg (m i)) },
refine continuous_iff_continuous_at.2 (λm, _),
refine continuous_at_of_locally_lipschitz zero_lt_one
(D * (fintype.card ι) * (∥m∥ + 1) ^ (fintype.card ι - 1)) (λm' h', _),
rw [dist_eq_norm, dist_eq_norm],
have : 0 ≤ (max ∥m'∥ ∥m∥), by simp,
have : (max ∥m'∥ ∥m∥) ≤ ∥m∥ + 1,
by simp [zero_le_one, norm_le_of_mem_closed_ball (le_of_lt h'), -add_comm],
calc
∥f m' - f m∥
≤ D * (fintype.card ι) * (max ∥m'∥ ∥m∥) ^ (fintype.card ι - 1) * ∥m' - m∥ :
f.norm_image_sub_le_of_bound D_pos H m' m
... ≤ D * (fintype.card ι) * (∥m∥ + 1) ^ (fintype.card ι - 1) * ∥m' - m∥ :
by apply_rules [mul_le_mul_of_nonneg_right, mul_le_mul_of_nonneg_left, mul_nonneg,
norm_nonneg, nat.cast_nonneg, pow_le_pow_of_le_left]
end
/-- Constructing a continuous multilinear map from a multilinear map satisfying a boundedness
condition. -/
def mk_continuous (C : ℝ) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) :
continuous_multilinear_map 𝕜 E₁ E₂ :=
{ cont := f.continuous_of_bound C H, ..f }
/-- Given a multilinear map in `n` variables, if one restricts it to `k` variables putting `z` on
the other coordinates, then the resulting restricted function satisfies an inequality
`∥f.restr v∥ ≤ C * ∥z∥^(n-k) * Π ∥v i∥` if the original function satisfies `∥f v∥ ≤ C * Π ∥v i∥`. -/
lemma restr_norm_le {k n : ℕ} (f : (multilinear_map 𝕜 (λ i : fin n, G) E₂ : _))
(s : finset (fin n)) (hk : s.card = k) (z : G) {C : ℝ}
(H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) (v : fin k → G) :
∥f.restr s hk z v∥ ≤ C * ∥z∥ ^ (n - k) * ∏ i, ∥v i∥ :=
calc ∥f.restr s hk z v∥
≤ C * ∏ j, ∥(if h : j ∈ s then v ((s.mono_equiv_of_fin hk).symm ⟨j, h⟩) else z)∥ : H _
... = C * ((∏ j in finset.univ \ s,
∥(if h : j ∈ s then v ((s.mono_equiv_of_fin hk).symm ⟨j, h⟩) else z)∥)
* (∏ j in s,
∥(if h : j ∈ s then v ((s.mono_equiv_of_fin hk).symm ⟨j, h⟩) else z)∥)) :
by rw ← finset.prod_sdiff (finset.subset_univ _)
... = C * (∥z∥ ^ (n - k) * ∏ i, ∥v i∥) :
begin
congr' 2,
{ have : ∥z∥ ^ (n - k) = ∏ j in finset.univ \ s, ∥z∥,
by simp [finset.card_sdiff (finset.subset_univ _), hk],
rw this,
exact finset.prod_congr rfl (λ i hi, by rw dif_neg (finset.mem_sdiff.1 hi).2) },
{ apply finset.prod_bij (λ (i : fin n) (hi : i ∈ s), (s.mono_equiv_of_fin hk).symm ⟨i, hi⟩),
{ exact λ _ _, finset.mem_univ _ },
{ exact λ i hi, by simp [hi] },
{ exact λ i j hi hi hij, subtype.mk.inj ((s.mono_equiv_of_fin hk).symm.injective hij) },
{ assume i hi,
rcases (s.mono_equiv_of_fin hk).symm.surjective i with ⟨j, hj⟩,
refine ⟨j.1, j.2, _⟩,
unfold_coes,
convert hj.symm,
rw subtype.ext_iff_val } }
end
... = C * ∥z∥ ^ (n - k) * ∏ i, ∥v i∥ : by rw mul_assoc
end multilinear_map
/-!
### Continuous multilinear maps
We define the norm `∥f∥` of a continuous multilinear map `f` in finitely many variables as the
smallest number such that `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥` for all `m`. We show that this
defines a normed space structure on `continuous_multilinear_map 𝕜 E₁ E₂`.
-/
namespace continuous_multilinear_map
variables (c : 𝕜) (f g : continuous_multilinear_map 𝕜 E₁ E₂) (m : Πi, E₁ i)
theorem bound : ∃ (C : ℝ), 0 < C ∧ (∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) :=
f.to_multilinear_map.exists_bound_of_continuous f.2
open real
/-- The operator norm of a continuous multilinear map is the inf of all its bounds. -/
def op_norm := Inf {c | 0 ≤ (c : ℝ) ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥}
instance has_op_norm : has_norm (continuous_multilinear_map 𝕜 E₁ E₂) := ⟨op_norm⟩
lemma norm_def : ∥f∥ = Inf {c | 0 ≤ (c : ℝ) ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} := rfl
-- So that invocations of `real.Inf_le` make sense: we show that the set of
-- bounds is nonempty and bounded below.
lemma bounds_nonempty {f : continuous_multilinear_map 𝕜 E₁ E₂} :
∃ c, c ∈ {c | 0 ≤ c ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} :=
let ⟨M, hMp, hMb⟩ := f.bound in ⟨M, le_of_lt hMp, hMb⟩
lemma bounds_bdd_below {f : continuous_multilinear_map 𝕜 E₁ E₂} :
bdd_below {c | 0 ≤ c ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} :=
⟨0, λ _ ⟨hn, _⟩, hn⟩
lemma op_norm_nonneg : 0 ≤ ∥f∥ :=
lb_le_Inf _ bounds_nonempty (λ _ ⟨hx, _⟩, hx)
/-- The fundamental property of the operator norm of a continuous multilinear map:
`∥f m∥` is bounded by `∥f∥` times the product of the `∥m i∥`. -/
theorem le_op_norm : ∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥ :=
begin
have A : 0 ≤ ∏ i, ∥m i∥ := prod_nonneg (λj hj, norm_nonneg _),
by_cases h : ∏ i, ∥m i∥ = 0,
{ rcases prod_eq_zero_iff.1 h with ⟨i, _, hi⟩,
rw norm_eq_zero at hi,
have : f m = 0 := f.map_coord_zero i hi,
rw [this, norm_zero],
exact mul_nonneg (op_norm_nonneg f) A },
{ have hlt : 0 < ∏ i, ∥m i∥ := lt_of_le_of_ne A (ne.symm h),
exact le_mul_of_div_le hlt ((le_Inf _ bounds_nonempty bounds_bdd_below).2
(λ c ⟨_, hc⟩, div_le_of_le_mul hlt (begin rw mul_comm, apply hc, end))) }
end
lemma ratio_le_op_norm : ∥f m∥ / ∏ i, ∥m i∥ ≤ ∥f∥ :=
begin
have : 0 ≤ ∏ i, ∥m i∥ := prod_nonneg (λj hj, norm_nonneg _),
cases eq_or_lt_of_le this with h h,
{ simp [h.symm, op_norm_nonneg f] },
{ rw div_le_iff h,
exact le_op_norm f m }
end
/-- The image of the unit ball under a continuous multilinear map is bounded. -/
lemma unit_le_op_norm (h : ∥m∥ ≤ 1) : ∥f m∥ ≤ ∥f∥ :=
calc
∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥ : f.le_op_norm m
... ≤ ∥f∥ * ∏ i : ι, 1 :
mul_le_mul_of_nonneg_left (prod_le_prod (λi hi, norm_nonneg _) (λi hi, le_trans (norm_le_pi_norm _ _) h))
(op_norm_nonneg f)
... = ∥f∥ : by simp
/-- If one controls the norm of every `f x`, then one controls the norm of `f`. -/
lemma op_norm_le_bound {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ m, ∥f m∥ ≤ M * ∏ i, ∥m i∥) :
∥f∥ ≤ M :=
Inf_le _ bounds_bdd_below ⟨hMp, hM⟩
/-- The operator norm satisfies the triangle inequality. -/
theorem op_norm_add_le : ∥f + g∥ ≤ ∥f∥ + ∥g∥ :=
Inf_le _ bounds_bdd_below
⟨add_nonneg (op_norm_nonneg _) (op_norm_nonneg _), λ x, by { rw add_mul,
exact norm_add_le_of_le (le_op_norm _ _) (le_op_norm _ _) }⟩
/-- A continuous linear map is zero iff its norm vanishes. -/
theorem op_norm_zero_iff : ∥f∥ = 0 ↔ f = 0 :=
begin
split,
{ assume h,
ext m,
simpa [h, norm_le_zero_iff.symm] using f.le_op_norm m },
{ assume h,
apply le_antisymm (op_norm_le_bound f (le_refl _) (λm, _)) (op_norm_nonneg _),
rw h,
simp }
end
@[simp] lemma norm_zero : ∥(0 : continuous_multilinear_map 𝕜 E₁ E₂)∥ = 0 :=
by rw op_norm_zero_iff
lemma op_norm_smul_le : ∥c • f∥ ≤ ∥c∥ * ∥f∥ :=
(Inf_le _ bounds_bdd_below
⟨mul_nonneg (norm_nonneg _) (op_norm_nonneg _), λ _,
begin
erw [norm_smul, mul_assoc],
exact mul_le_mul_of_nonneg_left (le_op_norm _ _) (norm_nonneg _)
end⟩)
lemma op_norm_neg : ∥-f∥ = ∥f∥ := by { rw norm_def, apply congr_arg, ext, simp }
/-- Continuous multilinear maps themselves form a normed space with respect to
the operator norm. -/
instance to_normed_group : normed_group (continuous_multilinear_map 𝕜 E₁ E₂) :=
normed_group.of_core _ ⟨op_norm_zero_iff, op_norm_add_le, op_norm_neg⟩
instance to_normed_space : normed_space 𝕜 (continuous_multilinear_map 𝕜 E₁ E₂) :=
⟨op_norm_smul_le⟩
/-- The difference `f m₁ - f m₂` is controlled in terms of `∥f∥` and `∥m₁ - m₂∥`, precise version.
For a less precise but more usable version, see `norm_image_sub_le_of_bound`. The bound reads
`∥f m - f m'∥ ≤ ∥f∥ * ∥m 1 - m' 1∥ * max ∥m 2∥ ∥m' 2∥ * max ∥m 3∥ ∥m' 3∥ * ... * max ∥m n∥ ∥m' n∥ + ...`,
where the other terms in the sum are the same products where `1` is replaced by any `i`.-/
lemma norm_image_sub_le_of_bound' (m₁ m₂ : Πi, E₁ i) :
∥f m₁ - f m₂∥ ≤
∥f∥ * ∑ i, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ :=
f.to_multilinear_map.norm_image_sub_le_of_bound' (norm_nonneg _) f.le_op_norm _ _
/-- The difference `f m₁ - f m₂` is controlled in terms of `∥f∥` and `∥m₁ - m₂∥`, less precise
version. For a more precise but less usable version, see `norm_image_sub_le_of_bound'`.
The bound is `∥f m - f m'∥ ≤ ∥f∥ * card ι * ∥m - m'∥ * (max ∥m∥ ∥m'∥) ^ (card ι - 1)`.-/
lemma norm_image_sub_le_of_bound (m₁ m₂ : Πi, E₁ i) :
∥f m₁ - f m₂∥ ≤ ∥f∥ * (fintype.card ι) * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) * ∥m₁ - m₂∥ :=
f.to_multilinear_map.norm_image_sub_le_of_bound (norm_nonneg _) f.le_op_norm _ _
/-- Applying a multilinear map to a vector is continuous in both coordinates. -/
lemma continuous_eval :
continuous (λ (p : (continuous_multilinear_map 𝕜 E₁ E₂ × (Πi, E₁ i))), p.1 p.2) :=
begin
apply continuous_iff_continuous_at.2 (λp, _),
apply continuous_at_of_locally_lipschitz zero_lt_one
((∥p∥ + 1) * (fintype.card ι) * (∥p∥ + 1) ^ (fintype.card ι - 1) + ∏ i, ∥p.2 i∥)
(λq hq, _),
have : 0 ≤ (max ∥q.2∥ ∥p.2∥), by simp,
have : 0 ≤ ∥p∥ + 1, by simp [le_trans zero_le_one],
have A : ∥q∥ ≤ ∥p∥ + 1 := norm_le_of_mem_closed_ball (le_of_lt hq),
have : (max ∥q.2∥ ∥p.2∥) ≤ ∥p∥ + 1 :=
le_trans (max_le_max (norm_snd_le q) (norm_snd_le p)) (by simp [A, -add_comm, zero_le_one]),
have : ∀ (i : ι), i ∈ univ → 0 ≤ ∥p.2 i∥ := λ i hi, norm_nonneg _,
calc dist (q.1 q.2) (p.1 p.2)
≤ dist (q.1 q.2) (q.1 p.2) + dist (q.1 p.2) (p.1 p.2) : dist_triangle _ _ _
... = ∥q.1 q.2 - q.1 p.2∥ + ∥q.1 p.2 - p.1 p.2∥ : by rw [dist_eq_norm, dist_eq_norm]
... ≤ ∥q.1∥ * (fintype.card ι) * (max ∥q.2∥ ∥p.2∥) ^ (fintype.card ι - 1) * ∥q.2 - p.2∥
+ ∥q.1 - p.1∥ * ∏ i, ∥p.2 i∥ :
add_le_add (norm_image_sub_le_of_bound _ _ _) ((q.1 - p.1).le_op_norm p.2)
... ≤ (∥p∥ + 1) * (fintype.card ι) * (∥p∥ + 1) ^ (fintype.card ι - 1) * ∥q - p∥
+ ∥q - p∥ * ∏ i, ∥p.2 i∥ :
by apply_rules [add_le_add, mul_le_mul, le_refl, le_trans (norm_fst_le q) A, nat.cast_nonneg,
mul_nonneg, pow_le_pow_of_le_left, pow_nonneg, norm_snd_le (q - p), norm_nonneg,
norm_fst_le (q - p), prod_nonneg]
... = ((∥p∥ + 1) * (fintype.card ι) * (∥p∥ + 1) ^ (fintype.card ι - 1)
+ (∏ i, ∥p.2 i∥)) * dist q p : by { rw dist_eq_norm, ring }
end
lemma continuous_eval_left (m : Π i, E₁ i) :
continuous (λ (p : (continuous_multilinear_map 𝕜 E₁ E₂)), (p : (Π i, E₁ i) → E₂) m) :=
continuous_eval.comp (continuous.prod_mk continuous_id continuous_const)
lemma has_sum_eval
{α : Type*} {p : α → continuous_multilinear_map 𝕜 E₁ E₂} {q : continuous_multilinear_map 𝕜 E₁ E₂}
(h : has_sum p q) (m : Π i, E₁ i) : has_sum (λ a, p a m) (q m) :=
begin
dsimp [has_sum] at h ⊢,
convert ((continuous_eval_left m).tendsto _).comp h,
ext s,
simp
end
open_locale topological_space
open filter
/-- If the target space is complete, the space of continuous multilinear maps with its norm is also
complete. The proof is essentially the same as for the space of continuous linear maps (modulo the
addition of `finset.prod` where needed. The duplication could be avoided by deducing the linear
case from the multilinear case via a currying isomorphism. However, this would mess up imports,
and it is more satisfactory to have the simplest case as a standalone proof. -/
instance [complete_space E₂] : complete_space (continuous_multilinear_map 𝕜 E₁ E₂) :=
begin
have nonneg : ∀ (v : Π i, E₁ i), 0 ≤ ∏ i, ∥v i∥ :=
λ v, finset.prod_nonneg (λ i hi, norm_nonneg _),
-- We show that every Cauchy sequence converges.
refine metric.complete_of_cauchy_seq_tendsto (λ f hf, _),
-- We now expand out the definition of a Cauchy sequence,
rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, b0, b_bound, b_lim⟩,
-- and establish that the evaluation at any point `v : Π i, E₁ i` is Cauchy.
have cau : ∀ v, cauchy_seq (λ n, f n v),
{ assume v,
apply cauchy_seq_iff_le_tendsto_0.2 ⟨λ n, b n * ∏ i, ∥v i∥, λ n, _, _, _⟩,
{ exact mul_nonneg (b0 n) (nonneg v) },
{ assume n m N hn hm,
rw dist_eq_norm,
apply le_trans ((f n - f m).le_op_norm v) _,
exact mul_le_mul_of_nonneg_right (b_bound n m N hn hm) (nonneg v) },
{ simpa using b_lim.mul tendsto_const_nhds } },
-- We assemble the limits points of those Cauchy sequences
-- (which exist as `E₂` is complete)
-- into a function which we call `F`.
choose F hF using λv, cauchy_seq_tendsto_of_complete (cau v),
-- Next, we show that this `F` is multilinear,
let Fmult : multilinear_map 𝕜 E₁ E₂ :=
{ to_fun := F,
map_add' := λ v i x y, begin
have A := hF (function.update v i (x + y)),
have B := (hF (function.update v i x)).add (hF (function.update v i y)),
simp at A B,
exact tendsto_nhds_unique A B
end,
map_smul' := λ v i c x, begin
have A := hF (function.update v i (c • x)),
have B := filter.tendsto.smul (@tendsto_const_nhds _ ℕ _ c _) (hF (function.update v i x)),
simp at A B,
exact tendsto_nhds_unique A B
end },
-- and that `F` has norm at most `(b 0 + ∥f 0∥)`.
have Fnorm : ∀ v, ∥F v∥ ≤ (b 0 + ∥f 0∥) * ∏ i, ∥v i∥,
{ assume v,
have A : ∀ n, ∥f n v∥ ≤ (b 0 + ∥f 0∥) * ∏ i, ∥v i∥,
{ assume n,
apply le_trans ((f n).le_op_norm _) _,
apply mul_le_mul_of_nonneg_right _ (nonneg v),
calc ∥f n∥ = ∥(f n - f 0) + f 0∥ : by { congr' 1, abel }
... ≤ ∥f n - f 0∥ + ∥f 0∥ : norm_add_le _ _
... ≤ b 0 + ∥f 0∥ : begin
apply add_le_add_right,
simpa [dist_eq_norm] using b_bound n 0 0 (zero_le _) (zero_le _)
end },
exact le_of_tendsto (hF v).norm (eventually_of_forall A) },
-- Thus `F` is continuous, and we propose that as the limit point of our original Cauchy sequence.
let Fcont := Fmult.mk_continuous _ Fnorm,
use Fcont,
-- Our last task is to establish convergence to `F` in norm.
have : ∀ n, ∥f n - Fcont∥ ≤ b n,
{ assume n,
apply op_norm_le_bound _ (b0 n) (λ v, _),
have A : ∀ᶠ m in at_top, ∥(f n - f m) v∥ ≤ b n * ∏ i, ∥v i∥,
{ refine eventually_at_top.2 ⟨n, λ m hm, _⟩,
apply le_trans ((f n - f m).le_op_norm _) _,
exact mul_le_mul_of_nonneg_right (b_bound n m n (le_refl _) hm) (nonneg v) },
have B : tendsto (λ m, ∥(f n - f m) v∥) at_top (𝓝 (∥(f n - Fcont) v∥)) :=
tendsto.norm (tendsto_const_nhds.sub (hF v)),
exact le_of_tendsto B A },
erw tendsto_iff_norm_tendsto_zero,
exact squeeze_zero (λ n, norm_nonneg _) this b_lim,
end
end continuous_multilinear_map
/-- If a continuous multilinear map is constructed from a multilinear map via the constructor
`mk_continuous`, then its norm is bounded by the bound given to the constructor if it is
nonnegative. -/
lemma multilinear_map.mk_continuous_norm_le (f : multilinear_map 𝕜 E₁ E₂) {C : ℝ} (hC : 0 ≤ C)
(H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : ∥f.mk_continuous C H∥ ≤ C :=
continuous_multilinear_map.op_norm_le_bound _ hC (λm, H m)
namespace continuous_multilinear_map
/-- Given a continuous multilinear map `f` on `n` variables (parameterized by `fin n`) and a subset
`s` of `k` of these variables, one gets a new continuous multilinear map on `fin k` by varying
these variables, and fixing the other ones equal to a given value `z`. It is denoted by
`f.restr s hk z`, where `hk` is a proof that the cardinality of `s` is `k`. The implicit
identification between `fin k` and `s` that we use is the canonical (increasing) bijection. -/
def restr {k n : ℕ} (f : (G [×n]→L[𝕜] E₂ : _))
(s : finset (fin n)) (hk : s.card = k) (z : G) : G [×k]→L[𝕜] E₂ :=
(f.to_multilinear_map.restr s hk z).mk_continuous
(∥f∥ * ∥z∥^(n-k)) $ λ v, multilinear_map.restr_norm_le _ _ _ _ f.le_op_norm _
lemma norm_restr {k n : ℕ} (f : G [×n]→L[𝕜] E₂) (s : finset (fin n)) (hk : s.card = k) (z : G) :
∥f.restr s hk z∥ ≤ ∥f∥ * ∥z∥ ^ (n - k) :=
begin
apply multilinear_map.mk_continuous_norm_le,
exact mul_nonneg (norm_nonneg _) (pow_nonneg (norm_nonneg _) _)
end
variables (𝕜 ι)
/-- The canonical continuous multilinear map on `𝕜^ι`, associating to `m` the product of all the
`m i` (multiplied by a fixed reference element `z` in the target module) -/
protected def mk_pi_field (z : E₂) : continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) E₂ :=
@multilinear_map.mk_continuous 𝕜 ι (λ(i : ι), 𝕜) E₂ _ _ _ _ _ _ _
(multilinear_map.mk_pi_ring 𝕜 ι z) (∥z∥)
(λ m, by simp only [multilinear_map.mk_pi_ring_apply, norm_smul, normed_field.norm_prod, mul_comm])
variables {𝕜 ι}
@[simp] lemma mk_pi_field_apply (z : E₂) (m : ι → 𝕜) :
(continuous_multilinear_map.mk_pi_field 𝕜 ι z : (ι → 𝕜) → E₂) m = (∏ i, m i) • z := rfl
lemma mk_pi_ring_apply_one_eq_self (f : continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) E₂) :
continuous_multilinear_map.mk_pi_field 𝕜 ι (f (λi, 1)) = f :=
begin
ext m,
have : m = (λi, m i • 1), by { ext j, simp },
conv_rhs { rw [this, f.map_smul_univ] },
refl
end
variables (𝕜 ι E₂)
/-- Continuous multilinear maps on `𝕜^n` with values in `E₂` are in bijection with `E₂`, as such a
continuous multilinear map is completely determined by its value on the constant vector made of
ones. We register this bijection as a linear equivalence in
`continuous_multilinear_map.pi_field_equiv_aux`. The continuous linear equivalence is
`continuous_multilinear_map.pi_field_equiv`. -/
protected def pi_field_equiv_aux : E₂ ≃ₗ[𝕜] (continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) E₂) :=
{ to_fun := λ z, continuous_multilinear_map.mk_pi_field 𝕜 ι z,
inv_fun := λ f, f (λi, 1),
map_add' := λ z z', by { ext m, simp [smul_add] },
map_smul' := λ c z, by { ext m, simp [smul_smul, mul_comm] },
left_inv := λ z, by simp,
right_inv := λ f, f.mk_pi_ring_apply_one_eq_self }
/-- Continuous multilinear maps on `𝕜^n` with values in `E₂` are in bijection with `E₂`, as such a
continuous multilinear map is completely determined by its value on the constant vector made of
ones. We register this bijection as a continuous linear equivalence in
`continuous_multilinear_map.pi_field_equiv`. -/
protected def pi_field_equiv : E₂ ≃L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) E₂) :=
{ continuous_to_fun := begin
refine (continuous_multilinear_map.pi_field_equiv_aux 𝕜 ι E₂).to_linear_map.continuous_of_bound
(1 : ℝ) (λz, _),
rw one_mul,
change ∥continuous_multilinear_map.mk_pi_field 𝕜 ι z∥ ≤ ∥z∥,
exact multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _
end,
continuous_inv_fun := begin
refine (continuous_multilinear_map.pi_field_equiv_aux 𝕜 ι E₂).symm.to_linear_map.continuous_of_bound
(1 : ℝ) (λf, _),
rw one_mul,
change ∥f (λi, 1)∥ ≤ ∥f∥,
apply @continuous_multilinear_map.unit_le_op_norm 𝕜 ι (λ (i : ι), 𝕜) E₂ _ _ _ _ _ _ _ f,
simp [pi_norm_le_iff zero_le_one, le_refl]
end,
.. continuous_multilinear_map.pi_field_equiv_aux 𝕜 ι E₂ }
end continuous_multilinear_map
section currying
/-!
### Currying
We associate to a continuous multilinear map in `n+1` variables (i.e., based on `fin n.succ`) two
curried functions, named `f.curry_left` (which is a continuous linear map on `E 0` taking values
in continuous multilinear maps in `n` variables) and `f.curry_right` (which is a continuous
multilinear map in `n` variables taking values in continuous linear maps on `E (last n)`).
The inverse operations are called `uncurry_left` and `uncurry_right`.
We also register continuous linear equiv versions of these correspondences, in
`continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`.
-/
open fin function
lemma continuous_linear_map.norm_map_tail_le
(f : E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) (m : Πi, E i) :
∥f (m 0) (tail m)∥ ≤ ∥f∥ * ∏ i, ∥m i∥ :=
calc
∥f (m 0) (tail m)∥ ≤ ∥f (m 0)∥ * ∏ i, ∥(tail m) i∥ : (f (m 0)).le_op_norm _
... ≤ (∥f∥ * ∥m 0∥) * ∏ i, ∥(tail m) i∥ :
mul_le_mul_of_nonneg_right (f.le_op_norm _) (prod_nonneg (λi hi, norm_nonneg _))
... = ∥f∥ * (∥m 0∥ * ∏ i, ∥(tail m) i∥) : by ring
... = ∥f∥ * ∏ i, ∥m i∥ : by { rw prod_univ_succ, refl }
lemma continuous_multilinear_map.norm_map_init_le
(f : continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂)) (m : Πi, E i) :
∥f (init m) (m (last n))∥ ≤ ∥f∥ * ∏ i, ∥m i∥ :=
calc
∥f (init m) (m (last n))∥ ≤ ∥f (init m)∥ * ∥m (last n)∥ : (f (init m)).le_op_norm _
... ≤ (∥f∥ * (∏ i, ∥(init m) i∥)) * ∥m (last n)∥ :
mul_le_mul_of_nonneg_right (f.le_op_norm _) (norm_nonneg _)
... = ∥f∥ * ((∏ i, ∥(init m) i∥) * ∥m (last n)∥) : mul_assoc _ _ _
... = ∥f∥ * ∏ i, ∥m i∥ : by { rw prod_univ_cast_succ, refl }
lemma continuous_multilinear_map.norm_map_cons_le
(f : continuous_multilinear_map 𝕜 E E₂) (x : E 0) (m : Π(i : fin n), E i.succ) :
∥f (cons x m)∥ ≤ ∥f∥ * ∥x∥ * ∏ i, ∥m i∥ :=
calc
∥f (cons x m)∥ ≤ ∥f∥ * ∏ i, ∥cons x m i∥ : f.le_op_norm _
... = (∥f∥ * ∥x∥) * ∏ i, ∥m i∥ : by { rw prod_univ_succ, simp [mul_assoc] }
lemma continuous_multilinear_map.norm_map_snoc_le
(f : continuous_multilinear_map 𝕜 E E₂) (m : Π(i : fin n), E i.cast_succ) (x : E (last n)) :
∥f (snoc m x)∥ ≤ ∥f∥ * (∏ i, ∥m i∥) * ∥x∥ :=
calc
∥f (snoc m x)∥ ≤ ∥f∥ * ∏ i, ∥snoc m x i∥ : f.le_op_norm _
... = ∥f∥ * (∏ i, ∥m i∥) * ∥x∥ : by { rw prod_univ_cast_succ, simp [mul_assoc] }
/-! #### Left currying -/
/-- Given a continuous linear map `f` from `E 0` to continuous multilinear maps on `n` variables,
construct the corresponding continuous multilinear map on `n+1` variables obtained by concatenating
the variables, given by `m ↦ f (m 0) (tail m)`-/
def continuous_linear_map.uncurry_left
(f : E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) :
continuous_multilinear_map 𝕜 E E₂ :=
(@linear_map.uncurry_left 𝕜 n E E₂ _ _ _ _ _
(continuous_multilinear_map.to_multilinear_map_linear.comp f.to_linear_map)).mk_continuous
(∥f∥) (λm, continuous_linear_map.norm_map_tail_le f m)
@[simp] lemma continuous_linear_map.uncurry_left_apply
(f : E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) (m : Πi, E i) :
f.uncurry_left m = f (m 0) (tail m) := rfl
/-- Given a continuous multilinear map `f` in `n+1` variables, split the first variable to obtain
a continuous linear map into continuous multilinear maps in `n` variables, given by
`x ↦ (m ↦ f (cons x m))`. -/
def continuous_multilinear_map.curry_left
(f : continuous_multilinear_map 𝕜 E E₂) :
E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂) :=
linear_map.mk_continuous
{ -- define a linear map into `n` continuous multilinear maps from an `n+1` continuous multilinear
-- map
to_fun := λx, (f.to_multilinear_map.curry_left x).mk_continuous
(∥f∥ * ∥x∥) (f.norm_map_cons_le x),
map_add' := λx y, by { ext m, exact f.cons_add m x y },
map_smul' := λc x, by { ext m, exact f.cons_smul m c x } }
-- then register its continuity thanks to its boundedness properties.
(∥f∥) (λx, multilinear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _)
@[simp] lemma continuous_multilinear_map.curry_left_apply
(f : continuous_multilinear_map 𝕜 E E₂) (x : E 0) (m : Π(i : fin n), E i.succ) :
f.curry_left x m = f (cons x m) := rfl
@[simp] lemma continuous_linear_map.curry_uncurry_left
(f : E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) :
f.uncurry_left.curry_left = f :=
begin
ext m x,
simp only [tail_cons, continuous_linear_map.uncurry_left_apply,
continuous_multilinear_map.curry_left_apply],
rw cons_zero
end
@[simp] lemma continuous_multilinear_map.uncurry_curry_left
(f : continuous_multilinear_map 𝕜 E E₂) : f.curry_left.uncurry_left = f :=
by { ext m, simp }
@[simp] lemma continuous_multilinear_map.curry_left_norm
(f : continuous_multilinear_map 𝕜 E E₂) : ∥f.curry_left∥ = ∥f∥ :=
begin
apply le_antisymm (linear_map.mk_continuous_norm_le _ (norm_nonneg _) _),
have : ∥f.curry_left.uncurry_left∥ ≤ ∥f.curry_left∥ :=
multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _,
simpa
end
@[simp] lemma continuous_linear_map.uncurry_left_norm
(f : E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) :
∥f.uncurry_left∥ = ∥f∥ :=
begin
apply le_antisymm (multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _),
have : ∥f.uncurry_left.curry_left∥ ≤ ∥f.uncurry_left∥ :=
linear_map.mk_continuous_norm_le _ (norm_nonneg _) _,
simpa
end
variables (𝕜 E E₂)
/-- The space of continuous multilinear maps on `Π(i : fin (n+1)), E i` is canonically isomorphic to
the space of continuous linear maps from `E 0` to the space of continuous multilinear maps on
`Π(i : fin n), E i.succ `, by separating the first variable. We register this isomorphism as a
linear isomorphism in `continuous_multilinear_curry_left_equiv_aux 𝕜 E E₂`.
The algebraic version (without continuity assumption on the maps) is
`multilinear_curry_left_equiv 𝕜 E E₂`, and the topological isomorphism (registering
additionally that the isomorphism is continuous) is
`continuous_multilinear_curry_left_equiv 𝕜 E E₂`.
The direct and inverse maps are given by `f.uncurry_left` and `f.curry_left`. Use these
unless you need the full framework of linear equivs. -/
def continuous_multilinear_curry_left_equiv_aux :
(E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) ≃ₗ[𝕜]
(continuous_multilinear_map 𝕜 E E₂) :=
{ to_fun := continuous_linear_map.uncurry_left,
map_add' := λf₁ f₂, by { ext m, refl },
map_smul' := λc f, by { ext m, refl },
inv_fun := continuous_multilinear_map.curry_left,
left_inv := continuous_linear_map.curry_uncurry_left,
right_inv := continuous_multilinear_map.uncurry_curry_left }
/-- The space of continuous multilinear maps on `Π(i : fin (n+1)), E i` is canonically isomorphic to
the space of continuous linear maps from `E 0` to the space of continuous multilinear maps on
`Π(i : fin n), E i.succ `, by separating the first variable. We register this isomorphism in
`continuous_multilinear_curry_left_equiv 𝕜 E E₂`. The algebraic version (without topology) is given
in `multilinear_curry_left_equiv 𝕜 E E₂`.
The direct and inverse maps are given by `f.uncurry_left` and `f.curry_left`. Use these
unless you need the full framework of continuous linear equivs. -/
def continuous_multilinear_curry_left_equiv :
(E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) ≃L[𝕜]
(continuous_multilinear_map 𝕜 E E₂) :=
{ continuous_to_fun := begin
refine (continuous_multilinear_curry_left_equiv_aux 𝕜 E E₂).to_linear_map.continuous_of_bound
(1 : ℝ) (λf, le_of_eq _),
rw one_mul,
exact f.uncurry_left_norm
end,
continuous_inv_fun := begin
refine (continuous_multilinear_curry_left_equiv_aux 𝕜 E E₂).symm.to_linear_map.continuous_of_bound
(1 : ℝ) (λf, le_of_eq _),
rw one_mul,
exact f.curry_left_norm
end,
.. continuous_multilinear_curry_left_equiv_aux 𝕜 E E₂ }
variables {𝕜 E E₂}
@[simp] lemma continuous_multilinear_curry_left_equiv_apply
(f : E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) (v : Π i, E i) :
continuous_multilinear_curry_left_equiv 𝕜 E E₂ f v = f (v 0) (tail v) := rfl
@[simp] lemma continuous_multilinear_curry_left_equiv_symm_apply
(f : continuous_multilinear_map 𝕜 E E₂) (x : E 0) (v : Π (i : fin n), E i.succ) :
(continuous_multilinear_curry_left_equiv 𝕜 E E₂).symm f x v = f (cons x v) := rfl
/-! #### Right currying -/
/-- Given a continuous linear map `f` from continuous multilinear maps on `n` variables to
continuous linear maps on `E 0`, construct the corresponding continuous multilinear map on `n+1`
variables obtained by concatenating the variables, given by `m ↦ f (init m) (m (last n))`. -/
def continuous_multilinear_map.uncurry_right
(f : continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂)) :
continuous_multilinear_map 𝕜 E E₂ :=
let f' : multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →ₗ[𝕜] E₂) :=
{ to_fun := λ m, (f m).to_linear_map,
map_add' := λ m i x y, by { simp, refl },
map_smul' := λ m i c x, by { simp, refl } } in
(@multilinear_map.uncurry_right 𝕜 n E E₂ _ _ _ _ _ f').mk_continuous
(∥f∥) (λm, f.norm_map_init_le m)
@[simp] lemma continuous_multilinear_map.uncurry_right_apply
(f : continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂)) (m : Πi, E i) :
f.uncurry_right m = f (init m) (m (last n)) := rfl
/-- Given a continuous multilinear map `f` in `n+1` variables, split the last variable to obtain
a continuous multilinear map in `n` variables into continuous linear maps, given by
`m ↦ (x ↦ f (snoc m x))`. -/
def continuous_multilinear_map.curry_right
(f : continuous_multilinear_map 𝕜 E E₂) :
continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂) :=
let f' : multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂) :=
{ to_fun := λm, (f.to_multilinear_map.curry_right m).mk_continuous
(∥f∥ * ∏ i, ∥m i∥) $ λx, f.norm_map_snoc_le m x,
map_add' := λ m i x y, by { simp, refl },
map_smul' := λ m i c x, by { simp, refl } } in
f'.mk_continuous (∥f∥) (λm, linear_map.mk_continuous_norm_le _
(mul_nonneg (norm_nonneg _) (prod_nonneg (λj hj, norm_nonneg _))) _)
@[simp] lemma continuous_multilinear_map.curry_right_apply
(f : continuous_multilinear_map 𝕜 E E₂) (m : Π(i : fin n), E i.cast_succ) (x : E (last n)) :
f.curry_right m x = f (snoc m x) := rfl
@[simp] lemma continuous_multilinear_map.curry_uncurry_right
(f : continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂)) :
f.uncurry_right.curry_right = f :=
begin
ext m x,
simp only [snoc_last, continuous_multilinear_map.curry_right_apply,
continuous_multilinear_map.uncurry_right_apply],
rw init_snoc
end
@[simp] lemma continuous_multilinear_map.uncurry_curry_right
(f : continuous_multilinear_map 𝕜 E E₂) : f.curry_right.uncurry_right = f :=
by { ext m, simp }
@[simp] lemma continuous_multilinear_map.curry_right_norm
(f : continuous_multilinear_map 𝕜 E E₂) : ∥f.curry_right∥ = ∥f∥ :=
begin
refine le_antisymm (multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _) _,
have : ∥f.curry_right.uncurry_right∥ ≤ ∥f.curry_right∥ :=
multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _,
simpa
end
@[simp] lemma continuous_multilinear_map.uncurry_right_norm
(f : continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂)) :
∥f.uncurry_right∥ = ∥f∥ :=
begin
refine le_antisymm (multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _) _,
have : ∥f.uncurry_right.curry_right∥ ≤ ∥f.uncurry_right∥ :=
multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _,
simpa
end
variables (𝕜 E E₂)
/-- The space of continuous multilinear maps on `Π(i : fin (n+1)), E i` is canonically isomorphic to
the space of continuous multilinear maps on `Π(i : fin n), E i.cast_succ` with values in the space
of continuous linear maps on `E (last n)`, by separating the last variable. We register this
isomorphism as a linear equiv in `continuous_multilinear_curry_right_equiv_aux 𝕜 E E₂`.
The algebraic version (without continuity assumption on the maps) is
`multilinear_curry_right_equiv 𝕜 E E₂`, and the topological isomorphism (registering
additionally that the isomorphism is continuous) is
`continuous_multilinear_curry_right_equiv 𝕜 E E₂`.
The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these
unless you need the full framework of linear equivs. -/
def continuous_multilinear_curry_right_equiv_aux :
(continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂)) ≃ₗ[𝕜]
(continuous_multilinear_map 𝕜 E E₂) :=
{ to_fun := continuous_multilinear_map.uncurry_right,
map_add' := λf₁ f₂, by { ext m, refl },
map_smul' := λc f, by { ext m, refl },
inv_fun := continuous_multilinear_map.curry_right,
left_inv := continuous_multilinear_map.curry_uncurry_right,
right_inv := continuous_multilinear_map.uncurry_curry_right }
/-- The space of continuous multilinear maps on `Π(i : fin (n+1)), E i` is canonically isomorphic to
the space of continuous multilinear maps on `Π(i : fin n), E i.cast_succ` with values in the space
of continuous linear maps on `E (last n)`, by separating the last variable. We register this
isomorphism as a continuous linear equiv in `continuous_multilinear_curry_right_equiv 𝕜 E E₂`.
The algebraic version (without topology) is given in `multilinear_curry_right_equiv 𝕜 E E₂`.
The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these
unless you need the full framework of continuous linear equivs. -/
def continuous_multilinear_curry_right_equiv :
(continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂)) ≃L[𝕜]
(continuous_multilinear_map 𝕜 E E₂) :=
{ continuous_to_fun := begin
refine (continuous_multilinear_curry_right_equiv_aux 𝕜 E E₂).to_linear_map.continuous_of_bound
(1 : ℝ) (λf, le_of_eq _),
rw one_mul,
exact f.uncurry_right_norm
end,
continuous_inv_fun := begin
refine (continuous_multilinear_curry_right_equiv_aux 𝕜 E E₂).symm.to_linear_map.continuous_of_bound
(1 : ℝ) (λf, le_of_eq _),
rw one_mul,
exact f.curry_right_norm
end,
.. continuous_multilinear_curry_right_equiv_aux 𝕜 E E₂ }
variables {𝕜 G E E₂}
@[simp] lemma continuous_multilinear_curry_right_equiv_apply
(f : (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂)))
(v : Π i, E i) :
(continuous_multilinear_curry_right_equiv 𝕜 E E₂) f v = f (init v) (v (last n)) := rfl
@[simp] lemma continuous_multilinear_curry_right_equiv_symm_apply
(f : continuous_multilinear_map 𝕜 E E₂)
(v : Π (i : fin n), E i.cast_succ) (x : E (last n)) :
(continuous_multilinear_curry_right_equiv 𝕜 E E₂).symm f v x = f (snoc v x) := rfl
/-!
#### Currying with `0` variables
The space of multilinear maps with `0` variables is trivial: such a multilinear map is just an
arbitrary constant (note that multilinear maps in `0` variables need not map `0` to `0`!).
Therefore, the space of continuous multilinear maps on `(fin 0) → G` with values in `E₂` is
isomorphic (and even isometric) to `E₂`. As this is the zeroth step in the construction of iterated
derivatives, we register this isomorphism. -/
variables {𝕜 G E₂}
/-- Associating to a continuous multilinear map in `0` variables the unique value it takes. -/
def continuous_multilinear_map.uncurry0
(f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) : E₂ := f 0
variables (𝕜 G)
/-- Associating to an element `x` of a vector space `E₂` the continuous multilinear map in `0`
variables taking the (unique) value `x` -/
def continuous_multilinear_map.curry0 (x : E₂) :
continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂ :=
{ to_fun := λm, x,
map_add' := λ m i, fin.elim0 i,
map_smul' := λ m i, fin.elim0 i,
cont := continuous_const }
variable {G}
@[simp] lemma continuous_multilinear_map.curry0_apply (x : E₂) (m : (fin 0) → G) :
(continuous_multilinear_map.curry0 𝕜 G x : ((fin 0) → G) → E₂) m = x := rfl
variable {𝕜}
@[simp] lemma continuous_multilinear_map.uncurry0_apply
(f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) :
f.uncurry0 = f 0 := rfl
@[simp] lemma continuous_multilinear_map.apply_zero_curry0
(f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) {x : fin 0 → G} :
continuous_multilinear_map.curry0 𝕜 G (f x) = f :=
by { ext m, simp [(subsingleton.elim _ _ : x = m)] }
lemma continuous_multilinear_map.uncurry0_curry0
(f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) :
continuous_multilinear_map.curry0 𝕜 G (f.uncurry0) = f :=
by simp
variables (𝕜 G)
@[simp] lemma continuous_multilinear_map.curry0_uncurry0 (x : E₂) :
(continuous_multilinear_map.curry0 𝕜 G x).uncurry0 = x := rfl
@[simp] lemma continuous_multilinear_map.uncurry0_norm (x : E₂) :
∥continuous_multilinear_map.curry0 𝕜 G x∥ = ∥x∥ :=
begin
apply le_antisymm,
{ exact continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λm, by simp) },
{ simpa using (continuous_multilinear_map.curry0 𝕜 G x).le_op_norm 0 }
end
variables {𝕜 G}
@[simp] lemma continuous_multilinear_map.fin0_apply_norm
(f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) {x : fin 0 → G} :
∥f x∥ = ∥f∥ :=
begin
have : x = 0 := subsingleton.elim _ _, subst this,
refine le_antisymm (by simpa using f.le_op_norm 0) _,
have : ∥continuous_multilinear_map.curry0 𝕜 G (f.uncurry0)∥ ≤ ∥f.uncurry0∥ :=
continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λm,
by simp [-continuous_multilinear_map.apply_zero_curry0]),
simpa
end
lemma continuous_multilinear_map.curry0_norm
(f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) : ∥f.uncurry0∥ = ∥f∥ :=
by simp
variables (𝕜 G E₂)
/-- The linear isomorphism between elements of a normed space, and continuous multilinear maps in
`0` variables with values in this normed space. The continuous version is given in
`continuous_multilinear_curry_fin0`.
The direct and inverse maps are `uncurry0` and `curry0`. Use these unless you need the full
framework of linear equivs. -/
def continuous_multilinear_curry_fin0_aux :
(continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) ≃ₗ[𝕜] E₂ :=
{ to_fun := λf, continuous_multilinear_map.uncurry0 f,
inv_fun := λf, continuous_multilinear_map.curry0 𝕜 G f,
map_add' := λf g, rfl,
map_smul' := λc f, rfl,
left_inv := continuous_multilinear_map.uncurry0_curry0,
right_inv := continuous_multilinear_map.curry0_uncurry0 𝕜 G }
/-- The continuous linear isomorphism between elements of a normed space, and continuous multilinear
maps in `0` variables with values in this normed space.
The direct and inverse maps are `uncurry0` and `curry0`. Use these unless you need the full
framework of continuous linear equivs. -/
def continuous_multilinear_curry_fin0 :
(continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) ≃L[𝕜] E₂ :=
{ continuous_to_fun := begin
change continuous (λ (f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂),
(f : ((fin 0) → G) → E₂) 0),
exact continuous_multilinear_map.continuous_eval.comp (continuous_id.prod_mk continuous_const)
end,
continuous_inv_fun := begin
refine (continuous_multilinear_curry_fin0_aux 𝕜 G E₂).symm.to_linear_map.continuous_of_bound
(1 : ℝ) (λf, le_of_eq _),
rw one_mul,
exact continuous_multilinear_map.uncurry0_norm _ _ _
end,
.. continuous_multilinear_curry_fin0_aux 𝕜 G E₂ }
variables {𝕜 G E₂}
@[simp] lemma continuous_multilinear_curry_fin0_apply
(f : (continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂)) :
continuous_multilinear_curry_fin0 𝕜 G E₂ f = f 0 := rfl
@[simp] lemma continuous_multilinear_curry_fin0_symm_apply
(x : E₂) (v : (fin 0) → G) :
(continuous_multilinear_curry_fin0 𝕜 G E₂).symm x v = x := rfl
/-! #### With 1 variable -/
variables (𝕜 G E₂)
/-- Continuous multilinear maps from `G^1` to `E₂` are isomorphic with continuous linear maps from
`G` to `E₂`. -/
def continuous_multilinear_curry_fin1 :
(continuous_multilinear_map 𝕜 (λ (i : fin 1), G) E₂) ≃L[𝕜] (G →L[𝕜] E₂) :=
(continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin 1), G) E₂).symm.trans
(continuous_multilinear_curry_fin0 𝕜 G (G →L[𝕜] E₂))
variables {𝕜 G E₂}
@[simp] lemma continuous_multilinear_curry_fin1_apply
(f : (continuous_multilinear_map 𝕜 (λ (i : fin 1), G) E₂)) (x : G) :
continuous_multilinear_curry_fin1 𝕜 G E₂ f x = f (fin.snoc 0 x) := rfl
@[simp] lemma continuous_multilinear_curry_fin1_symm_apply
(f : G →L[𝕜] E₂) (v : (fin 1) → G) :
(continuous_multilinear_curry_fin1 𝕜 G E₂).symm f v = f (v 0) := rfl
end currying
|
d9379e19bcec7babbf150fecd757c084fb18ecbf | 9a0b1b3a653ea926b03d1495fef64da1d14b3174 | /tidy/rewrite_search/discovery/screening.lean | 48dd2e190af7715932f3d4d5697e4800a072d38f | [
"Apache-2.0"
] | permissive | khoek/mathlib-tidy | 8623b27b4e04e7d598164e7eaf248610d58f768b | 866afa6ab597c47f1b72e8fe2b82b97fff5b980f | refs/heads/master | 1,585,598,975,772 | 1,538,659,544,000 | 1,538,659,544,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,320 | lean | import tidy.lib.list
import tidy.lib.env
import tidy.lib.tactic
import tidy.lib.pretty_print
open tactic tactic.interactive
open interactive
namespace tidy.rewrite_search.discovery
-- TODO make sure this didn't break anything
meta def is_acceptable_rewrite (t : expr) : bool :=
is_eq_or_iff_after_binders t
meta def is_acceptable_lemma (r : expr) : tactic bool :=
is_acceptable_rewrite <$> infer_type r
meta def is_acceptable_hyp (r : expr) : tactic bool :=
do t ← infer_type r, return $ is_acceptable_rewrite t ∧ ¬t.has_meta_var
meta def assert_acceptable_lemma (r : expr) : tactic unit := do
ret ← is_acceptable_lemma r,
if ret then return ()
else do
pp ← pretty_print r,
fail format!"\"{pp}\" is not a valid rewrite lemma!"
meta def load_attr_list : list name → tactic (list name)
| [] := return []
| (a :: rest) := do
names ← attribute.get_instances a,
l ← load_attr_list rest,
return $ names ++ l
meta def load_names (l : list name) : tactic (list expr) :=
l.mmap mk_const
meta def rewrite_list_from_rw_rules (rws : list rw_rule) : tactic (list (expr × bool)) :=
rws.mmap (λ r, do e ← to_expr' r.rule, pure (e, r.symm))
meta def rewrite_list_from_lemmas (l : list expr) : list (expr × bool) :=
l.map (λ e, (e, ff)) ++ l.map (λ e, (e, tt))
meta def rewrite_list_from_lemma (e : expr) : list (expr × bool) :=
rewrite_list_from_lemmas [e]
meta def rewrite_list_from_hyps : tactic (list (expr × bool)) := do
hyps ← local_context,
rewrite_list_from_lemmas <$> hyps.mfilter is_acceptable_hyp
-- TODO mk_apps recursively
meta def inflate_under_apps (locals : list expr) : expr → tactic (list expr)
| e := do
rws ← list.map prod.fst <$> mk_apps e locals,
rws_extras ← list.join <$> rws.mmap inflate_under_apps,
return $ e :: (rws ++ rws_extras)
meta def inflate_rw (locals : list expr) : expr × bool → tactic (list (expr × bool))
| (e, sy) := do
as ← inflate_under_apps locals e,
return $ as.map $ λ a, (a, sy)
meta def is_rewrite_lemma (d : declaration) : option (name × expr) :=
let t := d.type in if is_acceptable_rewrite t then some (d.to_name, t) else none
meta def find_all_rewrites : tactic (list (name × expr)) := do
e ← get_env,
return $ e.decl_omap is_rewrite_lemma
end tidy.rewrite_search.discovery |
c0f6b63aecfc1fc4d09bff215baa3f1e2175627c | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/ring_theory/henselian.lean | 90b788067f64ec371e8095bdddf710b309c5a6c5 | [
"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 | 11,812 | lean | /-
Copyright (c) 2021 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import data.polynomial.taylor
import ring_theory.ideal.local_ring
import linear_algebra.adic_completion
/-!
# Henselian rings
In this file we set up the basic theory of Henselian (local) rings.
A ring `R` is *Henselian* at an ideal `I` if the following conditions hold:
* `I` is contained in the Jacobson radical of `R`
* for every polynomial `f` over `R`, with a *simple* root `a₀` over the quotient ring `R/I`,
there exists a lift `a : R` of `a₀` that is a root of `f`.
(Here, saying that a root `b` of a polynomial `g` is *simple* means that `g.derivative.eval b` is a
unit. Warning: if `R/I` is not a field then it is not enough to assume that `g` has a factorization
into monic linear factors in which `X - b` shows up only once; for example `1` is not a simple root
of `X^2-1` over `ℤ/4ℤ`.)
A local ring `R` is *Henselian* if it is Henselian at its maximal ideal.
In this case the first condition is automatic, and in the second condition we may ask for
`f.derivative.eval a ≠ 0`, since the quotient ring `R/I` is a field in this case.
## Main declarations
* `henselian_ring`: a typeclass on commutative rings,
asserting that the ring is Henselian at the ideal `I`.
* `henselian_local_ring`: a typeclass on commutative rings,
asserting that the ring is local Henselian.
* `field.henselian`: fields are Henselian local rings
* `henselian.tfae`: equivalent ways of expressing the Henselian property for local rings
* `is_adic_complete.henselian`:
a ring `R` with ideal `I` that is `I`-adically complete is Henselian at `I`
## References
https://stacks.math.columbia.edu/tag/04GE
## Todo
After a good API for etale ring homomorphisms has been developed,
we can give more equivalent characterization os Henselian rings.
In particular, this can give a proof that factorizations into coprime polynomials can be lifted
from the residue field to the Henselian ring.
The following gist contains some code sketches in that direction.
https://gist.github.com/jcommelin/47d94e4af092641017a97f7f02bf9598
-/
noncomputable theory
universes u v
open_locale big_operators
open local_ring polynomial function
lemma is_local_ring_hom_of_le_jacobson_bot {R : Type*} [comm_ring R]
(I : ideal R) (h : I ≤ ideal.jacobson ⊥) :
is_local_ring_hom (ideal.quotient.mk I) :=
begin
constructor,
intros a h,
have : is_unit (ideal.quotient.mk (ideal.jacobson ⊥) a),
{ rw [is_unit_iff_exists_inv] at *,
obtain ⟨b, hb⟩ := h,
obtain ⟨b, rfl⟩ := ideal.quotient.mk_surjective b,
use ideal.quotient.mk _ b,
rw [←(ideal.quotient.mk _).map_one, ←(ideal.quotient.mk _).map_mul, ideal.quotient.eq] at ⊢ hb,
exact h hb },
obtain ⟨⟨x, y, h1, h2⟩, rfl : x = _⟩ := this,
obtain ⟨y, rfl⟩ := ideal.quotient.mk_surjective y,
rw [← (ideal.quotient.mk _).map_mul, ← (ideal.quotient.mk _).map_one, ideal.quotient.eq,
ideal.mem_jacobson_bot] at h1 h2,
specialize h1 1,
simp at h1,
exact h1.1,
end
/-- A ring `R` is *Henselian* at an ideal `I` if the following condition holds:
for every polynomial `f` over `R`, with a *simple* root `a₀` over the quotient ring `R/I`,
there exists a lift `a : R` of `a₀` that is a root of `f`.
(Here, saying that a root `b` of a polynomial `g` is *simple* means that `g.derivative.eval b` is a
unit. Warning: if `R/I` is not a field then it is not enough to assume that `g` has a factorization
into monic linear factors in which `X - b` shows up only once; for example `1` is not a simple root
of `X^2-1` over `ℤ/4ℤ`.) -/
class henselian_ring (R : Type*) [comm_ring R] (I : ideal R) : Prop :=
(jac : I ≤ ideal.jacobson ⊥)
(is_henselian : ∀ (f : polynomial R) (hf : f.monic) (a₀ : R) (h₁ : f.eval a₀ ∈ I)
(h₂ : is_unit (ideal.quotient.mk I (f.derivative.eval a₀))),
∃ a : R, f.is_root a ∧ (a - a₀ ∈ I))
/-- A local ring `R` is *Henselian* if the following condition holds:
for every polynomial `f` over `R`, with a *simple* root `a₀` over the residue field,
there exists a lift `a : R` of `a₀` that is a root of `f`.
(Recall that a root `b` of a polynomial `g` is *simple* if it is not a double root, so if
`g.derivative.eval b ≠ 0`.)
In other words, `R` is local Henselian if it is Henselian at the ideal `I`,
in the sense of `henselian_ring`. -/
class henselian_local_ring (R : Type*) [comm_ring R] extends local_ring R : Prop :=
(is_henselian : ∀ (f : polynomial R) (hf : f.monic) (a₀ : R) (h₁ : f.eval a₀ ∈ maximal_ideal R)
(h₂ : is_unit (f.derivative.eval a₀)),
∃ a : R, f.is_root a ∧ (a - a₀ ∈ maximal_ideal R))
@[priority 100] -- see Note [lower instance priority]
instance field.henselian (K : Type*) [field K] : henselian_local_ring K :=
{ is_henselian := λ f hf a₀ h₁ h₂,
begin
refine ⟨a₀, _, _⟩;
rwa [(maximal_ideal K).eq_bot_of_prime, ideal.mem_bot] at *,
rw sub_self,
end }
lemma henselian_local_ring.tfae (R : Type u) [comm_ring R] [local_ring R] :
tfae [
henselian_local_ring R,
∀ (f : polynomial R) (hf : f.monic) (a₀ : residue_field R) (h₁ : aeval a₀ f = 0)
(h₂ : aeval a₀ f.derivative ≠ 0),
∃ a : R, f.is_root a ∧ (residue R a = a₀),
∀ {K : Type u} [field K], by exactI ∀ (φ : R →+* K) (hφ : surjective φ)
(f : polynomial R) (hf : f.monic) (a₀ : K) (h₁ : f.eval₂ φ a₀ = 0)
(h₂ : f.derivative.eval₂ φ a₀ ≠ 0),
∃ a : R, f.is_root a ∧ (φ a = a₀)] :=
begin
tfae_have _3_2 : 3 → 2, { intro H, exact H (residue R) ideal.quotient.mk_surjective, },
tfae_have _2_1 : 2 → 1,
{ intros H, constructor, intros f hf a₀ h₁ h₂,
specialize H f hf (residue R a₀),
have aux := flip mem_nonunits_iff.mp h₂,
simp only [aeval_def, ring_hom.algebra_map_to_algebra, eval₂_at_apply,
← ideal.quotient.eq_zero_iff_mem, ← local_ring.mem_maximal_ideal] at H h₁ aux,
obtain ⟨a, ha₁, ha₂⟩ := H h₁ aux,
refine ⟨a, ha₁, _⟩,
rw ← ideal.quotient.eq_zero_iff_mem,
rwa [← sub_eq_zero, ← ring_hom.map_sub] at ha₂, },
tfae_have _1_3 : 1 → 3,
{ introsI hR K _K φ hφ f hf a₀ h₁ h₂,
obtain ⟨a₀, rfl⟩ := hφ a₀,
have H := henselian_local_ring.is_henselian f hf a₀,
simp only [← ker_eq_maximal_ideal φ hφ, eval₂_at_apply, φ.mem_ker] at H h₁ h₂,
obtain ⟨a, ha₁, ha₂⟩ := H h₁ _,
{ refine ⟨a, ha₁, _⟩, rwa [φ.map_sub, sub_eq_zero] at ha₂, },
{ contrapose! h₂,
rwa [← mem_nonunits_iff, ← local_ring.mem_maximal_ideal,
← local_ring.ker_eq_maximal_ideal φ hφ, φ.mem_ker] at h₂, } },
tfae_finish,
end
instance (R : Type*) [comm_ring R] [hR : henselian_local_ring R] :
henselian_ring R (maximal_ideal R) :=
{ jac := by { rw [ideal.jacobson, le_Inf_iff], rintro I ⟨-, hI⟩, exact (eq_maximal_ideal hI).ge },
is_henselian :=
begin
intros f hf a₀ h₁ h₂,
refine henselian_local_ring.is_henselian f hf a₀ h₁ _,
contrapose! h₂,
rw [← mem_nonunits_iff, ← local_ring.mem_maximal_ideal, ← ideal.quotient.eq_zero_iff_mem] at h₂,
rw h₂,
exact not_is_unit_zero
end }
/-- A ring `R` that is `I`-adically complete is Henselian at `I`. -/
@[priority 100] -- see Note [lower instance priority]
instance is_adic_complete.henselian_ring
(R : Type*) [comm_ring R] (I : ideal R) [is_adic_complete I R] :
henselian_ring R I :=
{ jac := is_adic_complete.le_jacobson_bot _,
is_henselian :=
begin
intros f hf a₀ h₁ h₂,
classical,
let f' := f.derivative,
-- we define a sequence `c n` by starting at `a₀` and then continually
-- applying the function sending `b` to `b - f(b)/f'(b)` (Newton's method).
-- Note that `f'.eval b` is a unit, because `b` has the same residue as `a₀` modulo `I`.
let c : ℕ → R := λ n, nat.rec_on n a₀ (λ _ b, b - f.eval b * ring.inverse (f'.eval b)),
have hc : ∀ n, c (n+1) = c n - f.eval (c n) * ring.inverse (f'.eval (c n)),
{ intro n, dsimp only [c, nat.rec_add_one], refl, },
-- we now spend some time determining properties of the sequence `c : ℕ → R`
-- `hc_mod`: for every `n`, we have `c n ≡ a₀ [SMOD I]`
-- `hf'c` : for every `n`, `f'.eval (c n)` is a unit
-- `hfcI` : for every `n`, `f.eval (c n)` is contained in `I ^ (n+1)`
have hc_mod : ∀ n, c n ≡ a₀ [SMOD I],
{ intro n, induction n with n ih, { refl },
rw [nat.succ_eq_add_one, hc, sub_eq_add_neg, ← add_zero a₀],
refine ih.add _,
rw [smodeq.zero, ideal.neg_mem_iff],
refine I.mul_mem_right _ _,
rw [← smodeq.zero] at h₁ ⊢,
exact (ih.eval f).trans h₁, },
have hf'c : ∀ n, is_unit (f'.eval (c n)),
{ intro n,
haveI := is_local_ring_hom_of_le_jacobson_bot I (is_adic_complete.le_jacobson_bot I),
apply is_unit_of_map_unit (ideal.quotient.mk I),
convert h₂ using 1,
exact smodeq.def.mp ((hc_mod n).eval _), },
have hfcI : ∀ n, f.eval (c n) ∈ I ^ (n+1),
{ intro n,
induction n with n ih, { simpa only [pow_one] },
simp only [nat.succ_eq_add_one],
rw [← taylor_eval_sub (c n), hc],
simp only [sub_eq_add_neg, add_neg_cancel_comm],
rw [eval_eq_sum, sum_over_range' _ _ _ (lt_add_of_pos_right _ zero_lt_two),
← finset.sum_range_add_sum_Ico _ (nat.le_add_left _ _)],
swap, { intro i, rw zero_mul },
refine ideal.add_mem _ _ _,
{ simp only [finset.sum_range_succ, taylor_coeff_one, mul_one, pow_one, taylor_coeff_zero,
mul_neg_eq_neg_mul_symm, finset.sum_singleton, finset.range_one, pow_zero],
rw [mul_left_comm, ring.mul_inverse_cancel _ (hf'c n), mul_one, add_neg_self],
exact ideal.zero_mem _ },
{ refine submodule.sum_mem _ _, simp only [finset.mem_Ico],
rintro i ⟨h2i, hi⟩,
have aux : n + 2 ≤ i * (n + 1), { transitivity 2 * (n + 1); nlinarith only [h2i], },
refine ideal.mul_mem_left _ _ (ideal.pow_le_pow aux _),
rw [pow_mul'],
refine ideal.pow_mem_pow ((ideal.neg_mem_iff _).2 $ ideal.mul_mem_right _ _ ih) _, } },
-- we are now in the position to show that `c : ℕ → R` is a Cauchy sequence
have aux : ∀ m n, m ≤ n → c m ≡ c n [SMOD (I ^ m • ⊤ : ideal R)],
{ intros m n hmn,
rw [← ideal.one_eq_top, algebra.id.smul_eq_mul, mul_one],
obtain ⟨k, rfl⟩ := nat.exists_eq_add_of_le hmn, clear hmn,
induction k with k ih, { rw add_zero, },
rw [nat.succ_eq_add_one, ← add_assoc, hc, ← add_zero (c m), sub_eq_add_neg],
refine ih.add _, symmetry,
rw [smodeq.zero, ideal.neg_mem_iff],
refine ideal.mul_mem_right _ _ (ideal.pow_le_pow _ (hfcI _)),
rw [add_assoc], exact le_self_add },
-- hence the sequence converges to some limit point `a`, which is the `a` we are looking for
obtain ⟨a, ha⟩ := is_precomplete.prec' c aux,
refine ⟨a, _, _⟩,
{ show f.is_root a,
suffices : ∀ n, f.eval a ≡ 0 [SMOD (I ^ n • ⊤ : ideal R)], { from is_Hausdorff.haus' _ this },
intro n, specialize ha n,
rw [← ideal.one_eq_top, algebra.id.smul_eq_mul, mul_one] at ha ⊢,
refine (ha.symm.eval f).trans _,
rw [smodeq.zero],
exact ideal.pow_le_pow le_self_add (hfcI _), },
{ show a - a₀ ∈ I,
specialize ha 1,
rw [hc, pow_one, ← ideal.one_eq_top, algebra.id.smul_eq_mul, mul_one, sub_eq_add_neg] at ha,
rw [← smodeq.sub_mem, ← add_zero a₀],
refine ha.symm.trans (smodeq.refl.add _),
rw [smodeq.zero, ideal.neg_mem_iff],
exact ideal.mul_mem_right _ _ h₁, }
end }
|
59c164b4932da45f7e3fea850827efdc9cc65a0b | 38bf3fd2bb651ab70511408fcf70e2029e2ba310 | /src/ring_theory/multiplicity.lean | b12ef016f3971dd0a219bbf50ff3f4e5450b53c0 | [
"Apache-2.0"
] | permissive | JaredCorduan/mathlib | 130392594844f15dad65a9308c242551bae6cd2e | d5de80376088954d592a59326c14404f538050a1 | refs/heads/master | 1,595,862,206,333 | 1,570,816,457,000 | 1,570,816,457,000 | 209,134,499 | 0 | 0 | Apache-2.0 | 1,568,746,811,000 | 1,568,746,811,000 | null | UTF-8 | Lean | false | false | 17,301 | lean | /-
Copyright (c) 2018 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis, Chris Hughes
-/
import algebra.associated data.int.gcd algebra.big_operators
import tactic.converter.interactive
variables {α : Type*}
open nat roption
theorem nat.find_le {p q : ℕ → Prop} [decidable_pred p] [decidable_pred q]
(h : ∀ n, q n → p n) (hp : ∃ n, p n) (hq : ∃ n, q n) :
nat.find hp ≤ nat.find hq :=
nat.find_min' _ ((h _) (nat.find_spec hq))
/-- `multiplicity a b` returns the largest natural number `n` such that
`a ^ n ∣ b`, as an `enat` or natural with infinity. If `∀ n, a ^ n ∣ b`,
then it returns `⊤`-/
def multiplicity [comm_semiring α] [decidable_rel ((∣) : α → α → Prop)] (a b : α) : enat :=
⟨∃ n : ℕ, ¬a ^ (n + 1) ∣ b, λ h, nat.find h⟩
namespace multiplicity
section comm_semiring
variables [comm_semiring α]
@[reducible] def finite (a b : α) : Prop := ∃ n : ℕ, ¬a ^ (n + 1) ∣ b
lemma finite_iff_dom [decidable_rel ((∣) : α → α → Prop)] {a b : α} :
finite a b ↔ (multiplicity a b).dom := iff.rfl
lemma finite_def {a b : α} : finite a b ↔ ∃ n : ℕ, ¬a ^ (n + 1) ∣ b := iff.rfl
@[move_cast]
theorem int.coe_nat_multiplicity (a b : ℕ) :
multiplicity a b = multiplicity (a : ℤ) (b : ℤ) :=
begin
apply roption.ext',
{ repeat {rw [← finite_iff_dom, finite_def]},
norm_cast, simp },
{ intros h1 h2,
apply _root_.le_antisymm; { apply nat.find_le, norm_cast, simp }}
end
lemma not_finite_iff_forall {a b : α} : (¬ finite a b) ↔ ∀ n : ℕ, a ^ n ∣ b :=
⟨λ h n, nat.cases_on n (one_dvd _) (by simpa [finite, classical.not_not] using h),
by simp [finite, multiplicity, classical.not_not]; tauto⟩
lemma not_unit_of_finite {a b : α} (h : finite a b) : ¬is_unit a :=
let ⟨n, hn⟩ := h in mt (is_unit_iff_forall_dvd.1 ∘ is_unit_pow (n + 1)) $
λ h, hn (h b)
lemma ne_zero_of_finite {a b : α} (h : finite a b) : b ≠ 0 :=
let ⟨n, hn⟩ := h in λ hb, by simpa [hb] using hn
lemma finite_of_finite_mul_left {a b c : α} : finite a (b * c) → finite a c :=
λ ⟨n, hn⟩, ⟨n, λ h, hn (dvd.trans h (by simp [_root_.mul_pow]))⟩
lemma finite_of_finite_mul_right {a b c : α} : finite a (b * c) → finite a b :=
by rw mul_comm; exact finite_of_finite_mul_left
variable [decidable_rel ((∣) : α → α → Prop)]
lemma pow_dvd_of_le_multiplicity {a b : α} {k : ℕ} : (k : enat) ≤ multiplicity a b → a ^ k ∣ b :=
nat.cases_on k (λ _, one_dvd _)
(λ k ⟨h₁, h₂⟩, by_contradiction (λ hk, (nat.find_min _ (lt_of_succ_le (h₂ ⟨k, hk⟩)) hk)))
lemma pow_multiplicity_dvd {a b : α} (h : finite a b) : a ^ get (multiplicity a b) h ∣ b :=
pow_dvd_of_le_multiplicity (by rw enat.coe_get)
lemma is_greatest {a b : α} {m : ℕ} (hm : multiplicity a b < m) : ¬a ^ m ∣ b :=
λ h, have finite a b, from enat.dom_of_le_some (le_of_lt hm),
by rw [← enat.coe_get (finite_iff_dom.1 this), enat.coe_lt_coe] at hm;
exact nat.find_spec this (dvd.trans (pow_dvd_pow _ hm) h)
lemma is_greatest' {a b : α} {m : ℕ} (h : finite a b) (hm : get (multiplicity a b) h < m) :
¬a ^ m ∣ b :=
is_greatest (by rwa [← enat.coe_lt_coe, enat.coe_get] at hm)
lemma unique {a b : α} {k : ℕ} (hk : a ^ k ∣ b) (hsucc : ¬a ^ (k + 1) ∣ b) :
(k : enat) = multiplicity a b :=
le_antisymm (le_of_not_gt (λ hk', is_greatest hk' hk)) $
have finite a b, from ⟨k, hsucc⟩,
by rw [← enat.coe_get (finite_iff_dom.1 this), enat.coe_le_coe];
exact nat.find_min' _ hsucc
lemma unique' {a b : α} {k : ℕ} (hk : a ^ k ∣ b) (hsucc : ¬ a ^ (k + 1) ∣ b) :
k = get (multiplicity a b) ⟨k, hsucc⟩ :=
by rw [← enat.coe_inj, enat.coe_get, unique hk hsucc]
lemma le_multiplicity_of_pow_dvd {a b : α}
{k : ℕ} (hk : a ^ k ∣ b) : (k : enat) ≤ multiplicity a b :=
le_of_not_gt $ λ hk', is_greatest hk' hk
lemma pow_dvd_iff_le_multiplicity {a b : α}
{k : ℕ} : a ^ k ∣ b ↔ (k : enat) ≤ multiplicity a b :=
⟨le_multiplicity_of_pow_dvd, pow_dvd_of_le_multiplicity⟩
lemma multiplicity_lt_iff_neg_dvd {a b : α} {k : ℕ} :
multiplicity a b < (k : enat) ↔ ¬ a ^ k ∣ b :=
by { rw [pow_dvd_iff_le_multiplicity, not_le] }
lemma eq_some_iff {a b : α} {n : ℕ} :
multiplicity a b = (n : enat) ↔ a ^ n ∣ b ∧ ¬a ^ (n + 1) ∣ b :=
⟨λ h, let ⟨h₁, h₂⟩ := eq_some_iff.1 h in
h₂ ▸ ⟨pow_multiplicity_dvd _, is_greatest
(by conv_lhs {rw ← enat.coe_get h₁ }; rw [enat.coe_lt_coe]; exact lt_succ_self _)⟩,
λ h, eq_some_iff.2 ⟨⟨n, h.2⟩, eq.symm $ unique' h.1 h.2⟩⟩
lemma eq_top_iff {a b : α} :
multiplicity a b = ⊤ ↔ ∀ n : ℕ, a ^ n ∣ b :=
⟨λ h n, nat.cases_on n (one_dvd _)
(λ n, by_contradiction (not_exists.1 (eq_none_iff'.1 h) n : _)),
λ h, eq_none_iff.2 (λ n ⟨⟨_, h₁⟩, _⟩, h₁ (h _))⟩
@[simp] protected lemma zero (a : α) : multiplicity a 0 = ⊤ :=
roption.eq_none_iff.2 (λ n ⟨⟨k, hk⟩, _⟩, hk (dvd_zero _))
lemma one_right {a : α} (ha : ¬is_unit a) : multiplicity a 1 = 0 :=
eq_some_iff.2 ⟨dvd_refl _, mt is_unit_iff_dvd_one.2 $ by simpa⟩
@[simp] lemma get_one_right {a : α} (ha : finite a 1) : get (multiplicity a 1) ha = 0 :=
get_eq_iff_eq_some.2 (eq_some_iff.2 ⟨dvd_refl _,
by simpa [is_unit_iff_dvd_one.symm] using not_unit_of_finite ha⟩)
@[simp] lemma one_left (b : α) : multiplicity 1 b = ⊤ := by simp [eq_top_iff]
@[simp] lemma multiplicity_unit {a : α} (b : α) (ha : is_unit a) : multiplicity a b = ⊤ :=
eq_top_iff.2 (λ _, is_unit_iff_forall_dvd.1 (is_unit_pow _ ha) _)
lemma multiplicity_eq_zero_of_not_dvd {a b : α} (ha : ¬a ∣ b) : multiplicity a b = 0 :=
eq_some_iff.2 (by simpa)
lemma eq_top_iff_not_finite {a b : α} : multiplicity a b = ⊤ ↔ ¬ finite a b :=
roption.eq_none_iff'
open_locale classical
lemma multiplicity_le_multiplicity_iff {a b c d : α} : multiplicity a b ≤ multiplicity c d ↔
(∀ n : ℕ, a ^ n ∣ b → c ^ n ∣ d) :=
⟨λ h n hab, (pow_dvd_of_le_multiplicity (le_trans (le_multiplicity_of_pow_dvd hab) h)),
λ h, if hab : finite a b
then by rw [← enat.coe_get (finite_iff_dom.1 hab)]; exact le_multiplicity_of_pow_dvd (h _ (pow_multiplicity_dvd _))
else
have ∀ n : ℕ, c ^ n ∣ d, from λ n, h n (not_finite_iff_forall.1 hab _),
by rw [eq_top_iff_not_finite.2 hab, eq_top_iff_not_finite.2
(not_finite_iff_forall.2 this)]⟩
lemma min_le_multiplicity_add {p a b : α} :
min (multiplicity p a) (multiplicity p b) ≤ multiplicity p (a + b) :=
(le_total (multiplicity p a) (multiplicity p b)).elim
(λ h, by rw [min_eq_left h, multiplicity_le_multiplicity_iff];
exact λ n hn, dvd_add hn (multiplicity_le_multiplicity_iff.1 h n hn))
(λ h, by rw [min_eq_right h, multiplicity_le_multiplicity_iff];
exact λ n hn, dvd_add (multiplicity_le_multiplicity_iff.1 h n hn) hn)
lemma dvd_of_multiplicity_pos {a b : α} (h : (0 : enat) < multiplicity a b) : a ∣ b :=
by rw [← _root_.pow_one a]; exact pow_dvd_of_le_multiplicity (enat.pos_iff_one_le.1 h)
lemma finite_nat_iff {a b : ℕ} : finite a b ↔ (a ≠ 1 ∧ 0 < b) :=
begin
rw [← not_iff_not, not_finite_iff_forall, not_and_distrib, ne.def,
not_not, not_lt, nat.le_zero_iff],
exact ⟨λ h, or_iff_not_imp_right.2 (λ hb,
have ha : a ≠ 0, from λ ha, by simpa [ha] using h 1,
by_contradiction (λ ha1 : a ≠ 1,
have ha_gt_one : 1 < a, from
have ∀ a : ℕ, a ≤ 1 → a ≠ 0 → a ≠ 1 → false, from dec_trivial,
lt_of_not_ge (λ ha', this a ha' ha ha1),
not_lt_of_ge (le_of_dvd (nat.pos_of_ne_zero hb) (h b))
(by simp only [nat.pow_eq_pow]; exact lt_pow_self ha_gt_one b))),
λ h, by cases h; simp *⟩
end
lemma finite_int_iff_nat_abs_finite {a b : ℤ} : finite a b ↔ finite a.nat_abs b.nat_abs :=
begin
rw [finite_def, finite_def],
conv in (a ^ _ ∣ b)
{ rw [← int.nat_abs_dvd_abs_iff, int.nat_abs_pow, ← pow_eq_pow] }
end
lemma finite_int_iff {a b : ℤ} : finite a b ↔ (a.nat_abs ≠ 1 ∧ b ≠ 0) :=
begin
have := int.nat_abs_eq a,
have := @int.nat_abs_ne_zero_of_ne_zero b,
rw [finite_int_iff_nat_abs_finite, finite_nat_iff, nat.pos_iff_ne_zero'],
split; finish
end
instance decidable_nat : decidable_rel (λ a b : ℕ, (multiplicity a b).dom) :=
λ a b, decidable_of_iff _ finite_nat_iff.symm
instance decidable_int : decidable_rel (λ a b : ℤ, (multiplicity a b).dom) :=
λ a b, decidable_of_iff _ finite_int_iff.symm
end comm_semiring
section comm_ring
variables [comm_ring α] [decidable_rel ((∣) : α → α → Prop)]
open_locale classical
@[simp] protected lemma neg (a b : α) : multiplicity a (-b) = multiplicity a b :=
roption.ext' (by simp only [multiplicity]; conv in (_ ∣ - _) {rw dvd_neg})
(λ h₁ h₂, enat.coe_inj.1 (by rw [enat.coe_get]; exact
eq.symm (unique ((dvd_neg _ _).2 (pow_multiplicity_dvd _))
(mt (dvd_neg _ _).1 (is_greatest' _ (lt_succ_self _))))))
lemma multiplicity_add_of_gt {p a b : α} (h : multiplicity p b < multiplicity p a) :
multiplicity p (a + b) = multiplicity p b :=
begin
apply le_antisymm,
{ apply enat.le_of_lt_add_one,
cases enat.ne_top_iff.mp (enat.ne_top_of_lt h) with k hk,
rw [hk], rw_mod_cast [multiplicity_lt_iff_neg_dvd], intro h_dvd,
rw [← dvd_add_iff_right] at h_dvd,
apply multiplicity.is_greatest _ h_dvd, rw [hk], apply_mod_cast nat.lt_succ_self,
rw [pow_dvd_iff_le_multiplicity, enat.coe_add, ← hk], exact enat.add_one_le_of_lt h },
{ convert min_le_multiplicity_add, rw [min_eq_right (le_of_lt h)] }
end
lemma multiplicity_sub_of_gt {p a b : α} (h : multiplicity p b < multiplicity p a) :
multiplicity p (a - b) = multiplicity p b :=
by { rw [sub_eq_add_neg, multiplicity_add_of_gt]; rwa [multiplicity.neg] }
lemma multiplicity_add_eq_min {p a b : α} (h : multiplicity p a ≠ multiplicity p b) :
multiplicity p (a + b) = min (multiplicity p a) (multiplicity p b) :=
begin
rcases lt_trichotomy (multiplicity p a) (multiplicity p b) with hab|hab|hab,
{ rw [add_comm, multiplicity_add_of_gt hab, min_eq_left], exact le_of_lt hab },
{ contradiction },
{ rw [multiplicity_add_of_gt hab, min_eq_right], exact le_of_lt hab},
end
end comm_ring
section integral_domain
variables [integral_domain α] [decidable_rel ((∣) : α → α → Prop)]
@[simp] lemma multiplicity_self {a : α} (ha : ¬is_unit a) (ha0 : a ≠ 0) :
multiplicity a a = 1 :=
eq_some_iff.2 ⟨by simp, λ ⟨b, hb⟩, ha (is_unit_iff_dvd_one.2
⟨b, (domain.mul_left_inj ha0).1 $ by clear _fun_match;
simpa [_root_.pow_succ, mul_assoc] using hb⟩)⟩
@[simp] lemma get_multiplicity_self {a : α} (ha : finite a a) :
get (multiplicity a a) ha = 1 :=
roption.get_eq_iff_eq_some.2 (eq_some_iff.2
⟨by simp, λ ⟨b, hb⟩,
by rw [← mul_one a, _root_.pow_add, _root_.pow_one, mul_assoc, mul_assoc,
domain.mul_left_inj (ne_zero_of_finite ha)] at hb;
exact mt is_unit_iff_dvd_one.2 (not_unit_of_finite ha)
⟨b, by clear _fun_match; simp * at *⟩⟩)
lemma finite_mul_aux {p : α} (hp : prime p) : ∀ {n m : ℕ} {a b : α},
¬p ^ (n + 1) ∣ a → ¬p ^ (m + 1) ∣ b → ¬p ^ (n + m + 1) ∣ a * b
| n m := λ a b ha hb ⟨s, hs⟩,
have p ∣ a * b, from ⟨p ^ (n + m) * s,
by simp [hs, _root_.pow_add, mul_comm, mul_assoc, mul_left_comm]⟩,
(hp.2.2 a b this).elim
(λ ⟨x, hx⟩, have hn0 : 0 < n,
from nat.pos_of_ne_zero (λ hn0, by clear _fun_match _fun_match; simpa [hx, hn0] using ha),
have wf : (n - 1) < n, from nat.sub_lt_self hn0 dec_trivial,
have hpx : ¬ p ^ (n - 1 + 1) ∣ x,
from λ ⟨y, hy⟩, ha (hx.symm ▸ ⟨y, (domain.mul_left_inj hp.1).1
$ by rw [nat.sub_add_cancel hn0] at hy;
simp [hy, _root_.pow_add, mul_comm, mul_assoc, mul_left_comm]⟩),
have 1 ≤ n + m, from le_trans hn0 (le_add_right n m),
finite_mul_aux hpx hb ⟨s, (domain.mul_left_inj hp.1).1 begin
rw [← nat.sub_add_comm hn0, nat.sub_add_cancel this],
clear _fun_match _fun_match finite_mul_aux,
simp [*, mul_comm, mul_assoc, mul_left_comm, _root_.pow_add] at *
end⟩)
(λ ⟨x, hx⟩, have hm0 : 0 < m,
from nat.pos_of_ne_zero (λ hm0, by clear _fun_match _fun_match; simpa [hx, hm0] using hb),
have wf : (m - 1) < m, from nat.sub_lt_self hm0 dec_trivial,
have hpx : ¬ p ^ (m - 1 + 1) ∣ x,
from λ ⟨y, hy⟩, hb (hx.symm ▸ ⟨y, (domain.mul_left_inj hp.1).1
$ by rw [nat.sub_add_cancel hm0] at hy;
simp [hy, _root_.pow_add, mul_comm, mul_assoc, mul_left_comm]⟩),
finite_mul_aux ha hpx ⟨s, (domain.mul_left_inj hp.1).1 begin
rw [add_assoc, nat.sub_add_cancel hm0],
clear _fun_match _fun_match finite_mul_aux,
simp [*, mul_comm, mul_assoc, mul_left_comm, _root_.pow_add] at *
end⟩)
lemma finite_mul {p a b : α} (hp : prime p) : finite p a → finite p b → finite p (a * b) :=
λ ⟨n, hn⟩ ⟨m, hm⟩, ⟨n + m, finite_mul_aux hp hn hm⟩
lemma finite_mul_iff {p a b : α} (hp : prime p) : finite p (a * b) ↔ finite p a ∧ finite p b :=
⟨λ h, ⟨finite_of_finite_mul_right h, finite_of_finite_mul_left h⟩,
λ h, finite_mul hp h.1 h.2⟩
lemma finite_pow {p a : α} (hp : prime p) : Π {k : ℕ} (ha : finite p a), finite p (a ^ k)
| 0 ha := ⟨0, by simp [mt is_unit_iff_dvd_one.2 hp.2.1]⟩
| (k+1) ha := by rw [_root_.pow_succ]; exact finite_mul hp ha (finite_pow ha)
protected lemma mul' {p a b : α} (hp : prime p)
(h : (multiplicity p (a * b)).dom) :
get (multiplicity p (a * b)) h =
get (multiplicity p a) ((finite_mul_iff hp).1 h).1 +
get (multiplicity p b) ((finite_mul_iff hp).1 h).2 :=
have hdiva : p ^ get (multiplicity p a) ((finite_mul_iff hp).1 h).1 ∣ a, from pow_multiplicity_dvd _,
have hdivb : p ^ get (multiplicity p b) ((finite_mul_iff hp).1 h).2 ∣ b, from pow_multiplicity_dvd _,
have hpoweq : p ^ (get (multiplicity p a) ((finite_mul_iff hp).1 h).1 +
get (multiplicity p b) ((finite_mul_iff hp).1 h).2) =
p ^ get (multiplicity p a) ((finite_mul_iff hp).1 h).1 *
p ^ get (multiplicity p b) ((finite_mul_iff hp).1 h).2,
by simp [_root_.pow_add],
have hdiv : p ^ (get (multiplicity p a) ((finite_mul_iff hp).1 h).1 +
get (multiplicity p b) ((finite_mul_iff hp).1 h).2) ∣ a * b,
by rw [hpoweq]; apply mul_dvd_mul; assumption,
have hsucc : ¬p ^ ((get (multiplicity p a) ((finite_mul_iff hp).1 h).1 +
get (multiplicity p b) ((finite_mul_iff hp).1 h).2) + 1) ∣ a * b,
from λ h, not_or (is_greatest' _ (lt_succ_self _)) (is_greatest' _ (lt_succ_self _))
(succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul hp (by convert hdiva)
(by convert hdivb) h),
by rw [← enat.coe_inj, enat.coe_get, eq_some_iff];
exact ⟨hdiv, hsucc⟩
open_locale classical
protected lemma mul {p a b : α} (hp : prime p) :
multiplicity p (a * b) = multiplicity p a + multiplicity p b :=
if h : finite p a ∧ finite p b then
by rw [← enat.coe_get (finite_iff_dom.1 h.1), ← enat.coe_get (finite_iff_dom.1 h.2),
← enat.coe_get (finite_iff_dom.1 (finite_mul hp h.1 h.2)),
← enat.coe_add, enat.coe_inj, multiplicity.mul' hp]; refl
else begin
rw [eq_top_iff_not_finite.2 (mt (finite_mul_iff hp).1 h)],
cases not_and_distrib.1 h with h h;
simp [eq_top_iff_not_finite.2 h]
end
lemma finset.prod {β : Type*} [decidable_eq β] {p : α} (hp : prime p) (s : finset β) (f : β → α) :
multiplicity p (s.prod f) = s.sum (λ x, multiplicity p (f x)) :=
begin
induction s using finset.induction with a s has ih h,
{ simp [one_right hp.not_unit] },
{ simp [has, multiplicity.mul hp, ih] }
end
protected lemma pow' {p a : α} (hp : prime p) (ha : finite p a) : ∀ {k : ℕ},
get (multiplicity p (a ^ k)) (finite_pow hp ha) = k * get (multiplicity p a) ha
| 0 := by dsimp [_root_.pow_zero]; simp [one_right hp.not_unit]; refl
| (k+1) := by dsimp only [_root_.pow_succ];
erw [multiplicity.mul' hp, pow', add_mul, one_mul, add_comm]
lemma pow {p a : α} (hp : prime p) : ∀ {k : ℕ},
multiplicity p (a ^ k) = add_monoid.smul k (multiplicity p a)
| 0 := by simp [one_right hp.not_unit]
| (succ k) := by simp [_root_.pow_succ, succ_smul, pow, multiplicity.mul hp]
lemma multiplicity_pow_self {p : α} (h0 : p ≠ 0) (hu : ¬ is_unit p) (n : ℕ) :
multiplicity p (p ^ n) = n :=
by { rw [eq_some_iff], use dvd_refl _, rw [pow_dvd_pow_iff h0 hu], apply nat.not_succ_le_self }
lemma multiplicity_pow_self_of_prime {p : α} (hp : prime p) (n : ℕ) :
multiplicity p (p ^ n) = n :=
multiplicity_pow_self hp.ne_zero hp.not_unit n
end integral_domain
end multiplicity
section nat
open multiplicity
lemma multiplicity_eq_zero_of_coprime {p a b : ℕ} (hp : p ≠ 1)
(hle : multiplicity p a ≤ multiplicity p b)
(hab : nat.coprime a b) : multiplicity p a = 0 :=
begin
rw [multiplicity_le_multiplicity_iff] at hle,
rw [← le_zero_iff_eq, ← not_lt, enat.pos_iff_one_le, ← enat.coe_one,
← pow_dvd_iff_le_multiplicity],
assume h,
have := nat.dvd_gcd h (hle _ h),
rw [coprime.gcd_eq_one hab, nat.dvd_one, _root_.pow_one] at this,
exact hp this
end
end nat
|
5c7d803551cf346b298883b5c6dbcae6cbfc8cc7 | 94e33a31faa76775069b071adea97e86e218a8ee | /src/analysis/mean_inequalities_pow.lean | ea49f3cc8f0c267ede09070b85c21ad77fc97d18 | [
"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 | 12,868 | lean | /-
Copyright (c) 2019 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov, Sébastien Gouëzel, Rémy Degenne
-/
import analysis.convex.specific_functions
/-!
# Mean value inequalities
In this file we prove several mean inequalities for finite sums. Versions for integrals of some of
these inequalities are available in `measure_theory.mean_inequalities`.
## Main theorems: generalized mean inequality
The inequality says that for two non-negative vectors $w$ and $z$ with $\sum_{i\in s} w_i=1$
and $p ≤ q$ we have
$$
\sqrt[p]{\sum_{i\in s} w_i z_i^p} ≤ \sqrt[q]{\sum_{i\in s} w_i z_i^q}.
$$
Currently we only prove this inequality for $p=1$. As in the rest of `mathlib`, we provide
different theorems for natural exponents (`pow_arith_mean_le_arith_mean_pow`), integer exponents
(`zpow_arith_mean_le_arith_mean_zpow`), and real exponents (`rpow_arith_mean_le_arith_mean_rpow` and
`arith_mean_le_rpow_mean`). In the first two cases we prove
$$
\left(\sum_{i\in s} w_i z_i\right)^n ≤ \sum_{i\in s} w_i z_i^n
$$
in order to avoid using real exponents. For real exponents we prove both this and standard versions.
## TODO
- each inequality `A ≤ B` should come with a theorem `A = B ↔ _`; one of the ways to prove them
is to define `strict_convex_on` functions.
- generalized mean inequality with any `p ≤ q`, including negative numbers;
- prove that the power mean tends to the geometric mean as the exponent tends to zero.
-/
universes u v
open finset
open_locale classical big_operators nnreal ennreal
noncomputable theory
variables {ι : Type u} (s : finset ι)
namespace real
theorem pow_arith_mean_le_arith_mean_pow (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i)
(hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) (n : ℕ) :
(∑ i in s, w i * z i) ^ n ≤ ∑ i in s, (w i * z i ^ n) :=
(convex_on_pow n).map_sum_le hw hw' hz
theorem pow_arith_mean_le_arith_mean_pow_of_even (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i)
(hw' : ∑ i in s, w i = 1) {n : ℕ} (hn : even n) :
(∑ i in s, w i * z i) ^ n ≤ ∑ i in s, (w i * z i ^ n) :=
hn.convex_on_pow.map_sum_le hw hw' (λ _ _, trivial)
theorem zpow_arith_mean_le_arith_mean_zpow (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i)
(hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 < z i) (m : ℤ) :
(∑ i in s, w i * z i) ^ m ≤ ∑ i in s, (w i * z i ^ m) :=
(convex_on_zpow m).map_sum_le hw hw' hz
theorem rpow_arith_mean_le_arith_mean_rpow (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i)
(hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) {p : ℝ} (hp : 1 ≤ p) :
(∑ i in s, w i * z i) ^ p ≤ ∑ i in s, (w i * z i ^ p) :=
(convex_on_rpow hp).map_sum_le hw hw' hz
theorem arith_mean_le_rpow_mean (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i)
(hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) {p : ℝ} (hp : 1 ≤ p) :
∑ i in s, w i * z i ≤ (∑ i in s, (w i * z i ^ p)) ^ (1 / p) :=
begin
have : 0 < p := lt_of_lt_of_le zero_lt_one hp,
rw [← rpow_le_rpow_iff _ _ this, ← rpow_mul, one_div_mul_cancel (ne_of_gt this), rpow_one],
exact rpow_arith_mean_le_arith_mean_rpow s w z hw hw' hz hp,
all_goals { apply_rules [sum_nonneg, rpow_nonneg_of_nonneg],
intros i hi,
apply_rules [mul_nonneg, rpow_nonneg_of_nonneg, hw i hi, hz i hi] },
end
end real
namespace nnreal
/-- Weighted generalized mean inequality, version sums over finite sets, with `ℝ≥0`-valued
functions and natural exponent. -/
theorem pow_arith_mean_le_arith_mean_pow (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) (n : ℕ) :
(∑ i in s, w i * z i) ^ n ≤ ∑ i in s, (w i * z i ^ n) :=
by exact_mod_cast real.pow_arith_mean_le_arith_mean_pow s _ _ (λ i _, (w i).coe_nonneg)
(by exact_mod_cast hw') (λ i _, (z i).coe_nonneg) n
/-- Weighted generalized mean inequality, version for sums over finite sets, with `ℝ≥0`-valued
functions and real exponents. -/
theorem rpow_arith_mean_le_arith_mean_rpow (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) {p : ℝ}
(hp : 1 ≤ p) :
(∑ i in s, w i * z i) ^ p ≤ ∑ i in s, (w i * z i ^ p) :=
by exact_mod_cast real.rpow_arith_mean_le_arith_mean_rpow s _ _ (λ i _, (w i).coe_nonneg)
(by exact_mod_cast hw') (λ i _, (z i).coe_nonneg) hp
/-- Weighted generalized mean inequality, version for two elements of `ℝ≥0` and real exponents. -/
theorem rpow_arith_mean_le_arith_mean2_rpow (w₁ w₂ z₁ z₂ : ℝ≥0) (hw' : w₁ + w₂ = 1) {p : ℝ}
(hp : 1 ≤ p) :
(w₁ * z₁ + w₂ * z₂) ^ p ≤ w₁ * z₁ ^ p + w₂ * z₂ ^ p :=
begin
have h := rpow_arith_mean_le_arith_mean_rpow univ ![w₁, w₂] ![z₁, z₂] _ hp,
{ simpa [fin.sum_univ_succ] using h, },
{ simp [hw', fin.sum_univ_succ], },
end
/-- Weighted generalized mean inequality, version for sums over finite sets, with `ℝ≥0`-valued
functions and real exponents. -/
theorem arith_mean_le_rpow_mean (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) {p : ℝ}
(hp : 1 ≤ p) :
∑ i in s, w i * z i ≤ (∑ i in s, (w i * z i ^ p)) ^ (1 / p) :=
by exact_mod_cast real.arith_mean_le_rpow_mean s _ _ (λ i _, (w i).coe_nonneg)
(by exact_mod_cast hw') (λ i _, (z i).coe_nonneg) hp
end nnreal
namespace nnreal
private lemma add_rpow_le_one_of_add_le_one {p : ℝ} (a b : ℝ≥0) (hab : a + b ≤ 1)
(hp1 : 1 ≤ p) :
a ^ p + b ^ p ≤ 1 :=
begin
have h_le_one : ∀ x : ℝ≥0, x ≤ 1 → x ^ p ≤ x, from λ x hx, rpow_le_self_of_le_one hx hp1,
have ha : a ≤ 1, from (self_le_add_right a b).trans hab,
have hb : b ≤ 1, from (self_le_add_left b a).trans hab,
exact (add_le_add (h_le_one a ha) (h_le_one b hb)).trans hab,
end
lemma add_rpow_le_rpow_add {p : ℝ} (a b : ℝ≥0) (hp1 : 1 ≤ p) :
a ^ p + b ^ p ≤ (a + b) ^ p :=
begin
have hp_pos : 0 < p := lt_of_lt_of_le zero_lt_one hp1,
by_cases h_zero : a + b = 0,
{ simp [add_eq_zero_iff.mp h_zero, hp_pos.ne'] },
have h_nonzero : ¬(a = 0 ∧ b = 0), by rwa add_eq_zero_iff at h_zero,
have h_add : a/(a+b) + b/(a+b) = 1, by rw [div_add_div_same, div_self h_zero],
have h := add_rpow_le_one_of_add_le_one (a/(a+b)) (b/(a+b)) h_add.le hp1,
rw [div_rpow a (a+b), div_rpow b (a+b)] at h,
have hab_0 : (a + b)^p ≠ 0, by simp [hp_pos, h_nonzero],
have hab_0' : 0 < (a + b) ^ p := zero_lt_iff.mpr hab_0,
have h_mul : (a + b)^p * (a ^ p / (a + b) ^ p + b ^ p / (a + b) ^ p) ≤ (a + b)^p,
{ nth_rewrite 3 ←mul_one ((a + b)^p),
exact (mul_le_mul_left hab_0').mpr h, },
rwa [div_eq_mul_inv, div_eq_mul_inv, mul_add, mul_comm (a^p), mul_comm (b^p), ←mul_assoc,
←mul_assoc, mul_inv_cancel hab_0, one_mul, one_mul] at h_mul,
end
lemma rpow_add_rpow_le_add {p : ℝ} (a b : ℝ≥0) (hp1 : 1 ≤ p) :
(a ^ p + b ^ p) ^ (1/p) ≤ a + b :=
begin
rw ←@nnreal.le_rpow_one_div_iff _ _ (1/p) (by simp [lt_of_lt_of_le zero_lt_one hp1]),
rw one_div_one_div,
exact add_rpow_le_rpow_add _ _ hp1,
end
theorem rpow_add_rpow_le {p q : ℝ} (a b : ℝ≥0) (hp_pos : 0 < p) (hpq : p ≤ q) :
(a ^ q + b ^ q) ^ (1/q) ≤ (a ^ p + b ^ p) ^ (1/p) :=
begin
have h_rpow : ∀ a : ℝ≥0, a^q = (a^p)^(q/p),
from λ a, by rw [←nnreal.rpow_mul, div_eq_inv_mul, ←mul_assoc,
_root_.mul_inv_cancel hp_pos.ne.symm, one_mul],
have h_rpow_add_rpow_le_add : ((a^p)^(q/p) + (b^p)^(q/p)) ^ (1/(q/p)) ≤ a^p + b^p,
{ refine rpow_add_rpow_le_add (a^p) (b^p) _,
rwa one_le_div hp_pos, },
rw [h_rpow a, h_rpow b, nnreal.le_rpow_one_div_iff hp_pos, ←nnreal.rpow_mul, mul_comm,
mul_one_div],
rwa one_div_div at h_rpow_add_rpow_le_add,
end
lemma rpow_add_le_add_rpow {p : ℝ} (a b : ℝ≥0) (hp_pos : 0 < p) (hp1 : p ≤ 1) :
(a + b) ^ p ≤ a ^ p + b ^ p :=
begin
have h := rpow_add_rpow_le a b hp_pos hp1,
rw one_div_one at h,
repeat { rw nnreal.rpow_one at h },
exact (nnreal.le_rpow_one_div_iff hp_pos).mp h
end
end nnreal
namespace ennreal
/-- Weighted generalized mean inequality, version for sums over finite sets, with `ℝ≥0∞`-valued
functions and real exponents. -/
theorem rpow_arith_mean_le_arith_mean_rpow (w z : ι → ℝ≥0∞) (hw' : ∑ i in s, w i = 1) {p : ℝ}
(hp : 1 ≤ p) :
(∑ i in s, w i * z i) ^ p ≤ ∑ i in s, (w i * z i ^ p) :=
begin
have hp_pos : 0 < p, from lt_of_lt_of_le zero_lt_one hp,
have hp_nonneg : 0 ≤ p, from le_of_lt hp_pos,
have hp_not_nonpos : ¬ p ≤ 0, by simp [hp_pos],
have hp_not_neg : ¬ p < 0, by simp [hp_nonneg],
have h_top_iff_rpow_top : ∀ (i : ι) (hi : i ∈ s), w i * z i = ⊤ ↔ w i * (z i) ^ p = ⊤,
by simp [hp_pos, hp_nonneg, hp_not_nonpos, hp_not_neg],
refine le_of_top_imp_top_of_to_nnreal_le _ _,
{ -- first, prove `(∑ i in s, w i * z i) ^ p = ⊤ → ∑ i in s, (w i * z i ^ p) = ⊤`
rw [rpow_eq_top_iff, sum_eq_top_iff, sum_eq_top_iff],
intro h,
simp only [and_false, hp_not_neg, false_or] at h,
rcases h.left with ⟨a, H, ha⟩,
use [a, H],
rwa ←h_top_iff_rpow_top a H, },
{ -- second, suppose both `(∑ i in s, w i * z i) ^ p ≠ ⊤` and `∑ i in s, (w i * z i ^ p) ≠ ⊤`,
-- and prove `((∑ i in s, w i * z i) ^ p).to_nnreal ≤ (∑ i in s, (w i * z i ^ p)).to_nnreal`,
-- by using `nnreal.rpow_arith_mean_le_arith_mean_rpow`.
intros h_top_rpow_sum _,
-- show hypotheses needed to put the `.to_nnreal` inside the sums.
have h_top : ∀ (a : ι), a ∈ s → w a * z a ≠ ⊤,
{ have h_top_sum : ∑ (i : ι) in s, w i * z i ≠ ⊤,
{ intro h,
rw [h, top_rpow_of_pos hp_pos] at h_top_rpow_sum,
exact h_top_rpow_sum rfl, },
exact λ a ha, (lt_top_of_sum_ne_top h_top_sum ha).ne },
have h_top_rpow : ∀ (a : ι), a ∈ s → w a * z a ^ p ≠ ⊤,
{ intros i hi,
specialize h_top i hi,
rwa [ne.def, ←h_top_iff_rpow_top i hi], },
-- put the `.to_nnreal` inside the sums.
simp_rw [to_nnreal_sum h_top_rpow, ←to_nnreal_rpow, to_nnreal_sum h_top, to_nnreal_mul,
←to_nnreal_rpow],
-- use corresponding nnreal result
refine nnreal.rpow_arith_mean_le_arith_mean_rpow s (λ i, (w i).to_nnreal) (λ i, (z i).to_nnreal)
_ hp,
-- verify the hypothesis `∑ i in s, (w i).to_nnreal = 1`, using `∑ i in s, w i = 1` .
have h_sum_nnreal : (∑ i in s, w i) = ↑(∑ i in s, (w i).to_nnreal),
{ rw coe_finset_sum,
refine sum_congr rfl (λ i hi, (coe_to_nnreal _).symm),
refine (lt_top_of_sum_ne_top _ hi).ne,
exact hw'.symm ▸ ennreal.one_ne_top },
rwa [←coe_eq_coe, ←h_sum_nnreal], },
end
/-- Weighted generalized mean inequality, version for two elements of `ℝ≥0∞` and real
exponents. -/
theorem rpow_arith_mean_le_arith_mean2_rpow (w₁ w₂ z₁ z₂ : ℝ≥0∞) (hw' : w₁ + w₂ = 1) {p : ℝ}
(hp : 1 ≤ p) :
(w₁ * z₁ + w₂ * z₂) ^ p ≤ w₁ * z₁ ^ p + w₂ * z₂ ^ p :=
begin
have h := rpow_arith_mean_le_arith_mean_rpow univ ![w₁, w₂] ![z₁, z₂] _ hp,
{ simpa [fin.sum_univ_succ] using h, },
{ simp [hw', fin.sum_univ_succ], },
end
end ennreal
namespace ennreal
lemma add_rpow_le_rpow_add {p : ℝ} (a b : ℝ≥0∞) (hp1 : 1 ≤ p) :
a ^ p + b ^ p ≤ (a + b) ^ p :=
begin
have hp_pos : 0 < p := lt_of_lt_of_le zero_lt_one hp1,
by_cases h_top : a + b = ⊤,
{ rw ←@ennreal.rpow_eq_top_iff_of_pos (a + b) p hp_pos at h_top,
rw h_top,
exact le_top, },
obtain ⟨ha_top, hb_top⟩ := add_ne_top.mp h_top,
lift a to ℝ≥0 using ha_top,
lift b to ℝ≥0 using hb_top,
simpa [← ennreal.coe_rpow_of_nonneg _ hp_pos.le] using
ennreal.coe_le_coe.2 (nnreal.add_rpow_le_rpow_add a b hp1),
end
lemma rpow_add_rpow_le_add {p : ℝ} (a b : ℝ≥0∞) (hp1 : 1 ≤ p) :
(a ^ p + b ^ p) ^ (1/p) ≤ a + b :=
begin
rw ←@ennreal.le_rpow_one_div_iff _ _ (1/p) (by simp [lt_of_lt_of_le zero_lt_one hp1]),
rw one_div_one_div,
exact add_rpow_le_rpow_add _ _ hp1,
end
theorem rpow_add_rpow_le {p q : ℝ} (a b : ℝ≥0∞) (hp_pos : 0 < p) (hpq : p ≤ q) :
(a ^ q + b ^ q) ^ (1/q) ≤ (a ^ p + b ^ p) ^ (1/p) :=
begin
have h_rpow : ∀ a : ℝ≥0∞, a^q = (a^p)^(q/p),
from λ a, by rw [← ennreal.rpow_mul, _root_.mul_div_cancel' _ hp_pos.ne'],
have h_rpow_add_rpow_le_add : ((a^p)^(q/p) + (b^p)^(q/p)) ^ (1/(q/p)) ≤ a^p + b^p,
{ refine rpow_add_rpow_le_add (a^p) (b^p) _,
rwa one_le_div hp_pos, },
rw [h_rpow a, h_rpow b, ennreal.le_rpow_one_div_iff hp_pos, ←ennreal.rpow_mul, mul_comm,
mul_one_div],
rwa one_div_div at h_rpow_add_rpow_le_add,
end
lemma rpow_add_le_add_rpow {p : ℝ} (a b : ℝ≥0∞) (hp_pos : 0 < p) (hp1 : p ≤ 1) :
(a + b) ^ p ≤ a ^ p + b ^ p :=
begin
have h := rpow_add_rpow_le a b hp_pos hp1,
rw one_div_one at h,
repeat { rw ennreal.rpow_one at h },
exact (ennreal.le_rpow_one_div_iff hp_pos).mp h,
end
end ennreal
|
9f5b6d2e11e5272892f19ba27e6bf5aeb18374de | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/category_theory/limits/shapes/binary_products.lean | 31fa5a5992a07bc149e36bcb2d91a4cf28cb7c43 | [
"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 | 25,998 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Bhavik Mehta
-/
import category_theory.limits.limits
import category_theory.discrete_category
import category_theory.epi_mono
/-!
# Binary (co)products
We define a category `walking_pair`, which is the index category
for a binary (co)product diagram. A convenience method `pair X Y`
constructs the functor from the walking pair, hitting the given objects.
We define `prod X Y` and `coprod X Y` as limits and colimits of such functors.
Typeclasses `has_binary_products` and `has_binary_coproducts` assert the existence
of (co)limits shaped as walking pairs.
We include lemmas for simplifying equations involving projections and coprojections, and define
braiding and associating isomorphisms, and the product comparison morphism.
## References
* [Stacks: Products of pairs](https://stacks.math.columbia.edu/tag/001R)
* [Stacks: coproducts of pairs](https://stacks.math.columbia.edu/tag/04AN)
-/
noncomputable theory
universes v u u₂
open category_theory
namespace category_theory.limits
/-- The type of objects for the diagram indexing a binary (co)product. -/
@[derive decidable_eq, derive inhabited]
inductive walking_pair : Type v
| left | right
open walking_pair
/--
The equivalence swapping left and right.
-/
def walking_pair.swap : walking_pair ≃ walking_pair :=
{ to_fun := λ j, walking_pair.rec_on j right left,
inv_fun := λ j, walking_pair.rec_on j right left,
left_inv := λ j, by { cases j; refl, },
right_inv := λ j, by { cases j; refl, }, }
@[simp] lemma walking_pair.swap_apply_left : walking_pair.swap left = right := rfl
@[simp] lemma walking_pair.swap_apply_right : walking_pair.swap right = left := rfl
@[simp] lemma walking_pair.swap_symm_apply_tt : walking_pair.swap.symm left = right := rfl
@[simp] lemma walking_pair.swap_symm_apply_ff : walking_pair.swap.symm right = left := rfl
/--
An equivalence from `walking_pair` to `bool`, sometimes useful when reindexing limits.
-/
def walking_pair.equiv_bool : walking_pair ≃ bool :=
{ to_fun := λ j, walking_pair.rec_on j tt ff, -- to match equiv.sum_equiv_sigma_bool
inv_fun := λ b, bool.rec_on b right left,
left_inv := λ j, by { cases j; refl, },
right_inv := λ b, by { cases b; refl, }, }
@[simp] lemma walking_pair.equiv_bool_apply_left : walking_pair.equiv_bool left = tt := rfl
@[simp] lemma walking_pair.equiv_bool_apply_right : walking_pair.equiv_bool right = ff := rfl
@[simp] lemma walking_pair.equiv_bool_symm_apply_tt : walking_pair.equiv_bool.symm tt = left := rfl
@[simp] lemma walking_pair.equiv_bool_symm_apply_ff : walking_pair.equiv_bool.symm ff = right := rfl
variables {C : Type u} [category.{v} C]
/-- The diagram on the walking pair, sending the two points to `X` and `Y`. -/
def pair (X Y : C) : discrete walking_pair ⥤ C :=
discrete.functor (λ j, walking_pair.cases_on j X Y)
@[simp] lemma pair_obj_left (X Y : C) : (pair X Y).obj left = X := rfl
@[simp] lemma pair_obj_right (X Y : C) : (pair X Y).obj right = Y := rfl
section
variables {F G : discrete walking_pair.{v} ⥤ C} (f : F.obj left ⟶ G.obj left) (g : F.obj right ⟶ G.obj right)
/-- The natural transformation between two functors out of the walking pair, specified by its components. -/
def map_pair : F ⟶ G :=
{ app := λ j, match j with
| left := f
| right := g
end }
@[simp] lemma map_pair_left : (map_pair f g).app left = f := rfl
@[simp] lemma map_pair_right : (map_pair f g).app right = g := rfl
/-- The natural isomorphism between two functors out of the walking pair, specified by its components. -/
@[simps]
def map_pair_iso (f : F.obj left ≅ G.obj left) (g : F.obj right ≅ G.obj right) : F ≅ G :=
{ hom := map_pair f.hom g.hom,
inv := map_pair f.inv g.inv,
hom_inv_id' := by { ext ⟨⟩; simp, },
inv_hom_id' := by { ext ⟨⟩; simp, } }
end
section
variables {D : Type u} [category.{v} D]
/-- The natural isomorphism between `pair X Y ⋙ F` and `pair (F.obj X) (F.obj Y)`. -/
def pair_comp (X Y : C) (F : C ⥤ D) : pair X Y ⋙ F ≅ pair (F.obj X) (F.obj Y) :=
map_pair_iso (eq_to_iso rfl) (eq_to_iso rfl)
end
/-- Every functor out of the walking pair is naturally isomorphic (actually, equal) to a `pair` -/
def diagram_iso_pair (F : discrete walking_pair ⥤ C) :
F ≅ pair (F.obj walking_pair.left) (F.obj walking_pair.right) :=
map_pair_iso (eq_to_iso rfl) (eq_to_iso rfl)
/-- A binary fan is just a cone on a diagram indexing a product. -/
abbreviation binary_fan (X Y : C) := cone (pair X Y)
/-- The first projection of a binary fan. -/
abbreviation binary_fan.fst {X Y : C} (s : binary_fan X Y) := s.π.app walking_pair.left
/-- The second projection of a binary fan. -/
abbreviation binary_fan.snd {X Y : C} (s : binary_fan X Y) := s.π.app walking_pair.right
@[simp] lemma binary_fan.π_app_left {X Y : C} (s : binary_fan X Y) :
s.π.app walking_pair.left = s.fst := rfl
@[simp] lemma binary_fan.π_app_right {X Y : C} (s : binary_fan X Y) :
s.π.app walking_pair.right = s.snd := rfl
lemma binary_fan.is_limit.hom_ext {W X Y : C} {s : binary_fan X Y} (h : is_limit s)
{f g : W ⟶ s.X} (h₁ : f ≫ s.fst = g ≫ s.fst) (h₂ : f ≫ s.snd = g ≫ s.snd) : f = g :=
h.hom_ext $ λ j, walking_pair.cases_on j h₁ h₂
/-- A binary cofan is just a cocone on a diagram indexing a coproduct. -/
abbreviation binary_cofan (X Y : C) := cocone (pair X Y)
/-- The first inclusion of a binary cofan. -/
abbreviation binary_cofan.inl {X Y : C} (s : binary_cofan X Y) := s.ι.app walking_pair.left
/-- The second inclusion of a binary cofan. -/
abbreviation binary_cofan.inr {X Y : C} (s : binary_cofan X Y) := s.ι.app walking_pair.right
@[simp] lemma binary_cofan.ι_app_left {X Y : C} (s : binary_cofan X Y) :
s.ι.app walking_pair.left = s.inl := rfl
@[simp] lemma binary_cofan.ι_app_right {X Y : C} (s : binary_cofan X Y) :
s.ι.app walking_pair.right = s.inr := rfl
lemma binary_cofan.is_colimit.hom_ext {W X Y : C} {s : binary_cofan X Y} (h : is_colimit s)
{f g : s.X ⟶ W} (h₁ : s.inl ≫ f = s.inl ≫ g) (h₂ : s.inr ≫ f = s.inr ≫ g) : f = g :=
h.hom_ext $ λ j, walking_pair.cases_on j h₁ h₂
variables {X Y : C}
/-- A binary fan with vertex `P` consists of the two projections `π₁ : P ⟶ X` and `π₂ : P ⟶ Y`. -/
@[simps X]
def binary_fan.mk {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) : binary_fan X Y :=
{ X := P,
π := { app := λ j, walking_pair.cases_on j π₁ π₂ }}
/-- A binary cofan with vertex `P` consists of the two inclusions `ι₁ : X ⟶ P` and `ι₂ : Y ⟶ P`. -/
@[simps X]
def binary_cofan.mk {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) : binary_cofan X Y :=
{ X := P,
ι := { app := λ j, walking_pair.cases_on j ι₁ ι₂ }}
@[simp] lemma binary_fan.mk_π_app_left {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) :
(binary_fan.mk π₁ π₂).π.app walking_pair.left = π₁ := rfl
@[simp] lemma binary_fan.mk_π_app_right {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) :
(binary_fan.mk π₁ π₂).π.app walking_pair.right = π₂ := rfl
@[simp] lemma binary_cofan.mk_ι_app_left {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) :
(binary_cofan.mk ι₁ ι₂).ι.app walking_pair.left = ι₁ := rfl
@[simp] lemma binary_cofan.mk_ι_app_right {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) :
(binary_cofan.mk ι₁ ι₂).ι.app walking_pair.right = ι₂ := rfl
/-- If `s` is a limit binary fan over `X` and `Y`, then every pair of morphisms `f : W ⟶ X` and
`g : W ⟶ Y` induces a morphism `l : W ⟶ s.X` satisfying `l ≫ s.fst = f` and `l ≫ s.snd = g`.
-/
@[simps]
def binary_fan.is_limit.lift' {W X Y : C} {s : binary_fan X Y} (h : is_limit s) (f : W ⟶ X)
(g : W ⟶ Y) : {l : W ⟶ s.X // l ≫ s.fst = f ∧ l ≫ s.snd = g} :=
⟨h.lift $ binary_fan.mk f g, h.fac _ _, h.fac _ _⟩
/-- If `s` is a colimit binary cofan over `X` and `Y`,, then every pair of morphisms `f : X ⟶ W` and
`g : Y ⟶ W` induces a morphism `l : s.X ⟶ W` satisfying `s.inl ≫ l = f` and `s.inr ≫ l = g`.
-/
@[simps]
def binary_cofan.is_colimit.desc' {W X Y : C} {s : binary_cofan X Y} (h : is_colimit s) (f : X ⟶ W)
(g : Y ⟶ W) : {l : s.X ⟶ W // s.inl ≫ l = f ∧ s.inr ≫ l = g} :=
⟨h.desc $ binary_cofan.mk f g, h.fac _ _, h.fac _ _⟩
/-- An abbreviation for `has_limit (pair X Y)`. -/
abbreviation has_binary_product (X Y : C) := has_limit (pair X Y)
/-- An abbreviation for `has_colimit (pair X Y)`. -/
abbreviation has_binary_coproduct (X Y : C) := has_colimit (pair X Y)
/-- If we have a product of `X` and `Y`, we can access it using `prod X Y` or
`X ⨯ Y`. -/
abbreviation prod (X Y : C) [has_binary_product X Y] := limit (pair X Y)
/-- If we have a coproduct of `X` and `Y`, we can access it using `coprod X Y ` or
`X ⨿ Y`. -/
abbreviation coprod (X Y : C) [has_binary_coproduct X Y] := colimit (pair X Y)
notation X ` ⨯ `:20 Y:20 := prod X Y
notation X ` ⨿ `:20 Y:20 := coprod X Y
/-- The projection map to the first component of the product. -/
abbreviation prod.fst {X Y : C} [has_binary_product X Y] : X ⨯ Y ⟶ X :=
limit.π (pair X Y) walking_pair.left
/-- The projecton map to the second component of the product. -/
abbreviation prod.snd {X Y : C} [has_binary_product X Y] : X ⨯ Y ⟶ Y :=
limit.π (pair X Y) walking_pair.right
/-- The inclusion map from the first component of the coproduct. -/
abbreviation coprod.inl {X Y : C} [has_binary_coproduct X Y] : X ⟶ X ⨿ Y :=
colimit.ι (pair X Y) walking_pair.left
/-- The inclusion map from the second component of the coproduct. -/
abbreviation coprod.inr {X Y : C} [has_binary_coproduct X Y] : Y ⟶ X ⨿ Y :=
colimit.ι (pair X Y) walking_pair.right
@[ext] lemma prod.hom_ext {W X Y : C} [has_binary_product X Y] {f g : W ⟶ X ⨯ Y}
(h₁ : f ≫ prod.fst = g ≫ prod.fst) (h₂ : f ≫ prod.snd = g ≫ prod.snd) : f = g :=
binary_fan.is_limit.hom_ext (limit.is_limit _) h₁ h₂
@[ext] lemma coprod.hom_ext {W X Y : C} [has_binary_coproduct X Y] {f g : X ⨿ Y ⟶ W}
(h₁ : coprod.inl ≫ f = coprod.inl ≫ g) (h₂ : coprod.inr ≫ f = coprod.inr ≫ g) : f = g :=
binary_cofan.is_colimit.hom_ext (colimit.is_colimit _) h₁ h₂
/-- If the product of `X` and `Y` exists, then every pair of morphisms `f : W ⟶ X` and `g : W ⟶ Y`
induces a morphism `prod.lift f g : W ⟶ X ⨯ Y`. -/
abbreviation prod.lift {W X Y : C} [has_binary_product X Y] (f : W ⟶ X) (g : W ⟶ Y) : W ⟶ X ⨯ Y :=
limit.lift _ (binary_fan.mk f g)
/-- diagonal arrow of the binary product in the category `fam I` -/
abbreviation diag (X : C) [has_binary_product X X] : X ⟶ X ⨯ X :=
prod.lift (𝟙 _) (𝟙 _)
/-- If the coproduct of `X` and `Y` exists, then every pair of morphisms `f : X ⟶ W` and
`g : Y ⟶ W` induces a morphism `coprod.desc f g : X ⨿ Y ⟶ W`. -/
abbreviation coprod.desc {W X Y : C} [has_binary_coproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) : X ⨿ Y ⟶ W :=
colimit.desc _ (binary_cofan.mk f g)
/-- codiagonal arrow of the binary coproduct -/
abbreviation codiag (X : C) [has_binary_coproduct X X] : X ⨿ X ⟶ X :=
coprod.desc (𝟙 _) (𝟙 _)
@[simp, reassoc]
lemma prod.lift_fst {W X Y : C} [has_binary_product X Y] (f : W ⟶ X) (g : W ⟶ Y) :
prod.lift f g ≫ prod.fst = f :=
limit.lift_π _ _
@[simp, reassoc]
lemma prod.lift_snd {W X Y : C} [has_binary_product X Y] (f : W ⟶ X) (g : W ⟶ Y) :
prod.lift f g ≫ prod.snd = g :=
limit.lift_π _ _
-- The simp linter says simp can prove the reassoc version of this lemma.
@[reassoc, simp]
lemma coprod.inl_desc {W X Y : C} [has_binary_coproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) :
coprod.inl ≫ coprod.desc f g = f :=
colimit.ι_desc _ _
-- The simp linter says simp can prove the reassoc version of this lemma.
@[reassoc, simp]
lemma coprod.inr_desc {W X Y : C} [has_binary_coproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) :
coprod.inr ≫ coprod.desc f g = g :=
colimit.ι_desc _ _
instance prod.mono_lift_of_mono_left {W X Y : C} [has_binary_product X Y] (f : W ⟶ X) (g : W ⟶ Y)
[mono f] : mono (prod.lift f g) :=
mono_of_mono_fac $ prod.lift_fst _ _
instance prod.mono_lift_of_mono_right {W X Y : C} [has_binary_product X Y] (f : W ⟶ X) (g : W ⟶ Y)
[mono g] : mono (prod.lift f g) :=
mono_of_mono_fac $ prod.lift_snd _ _
instance coprod.epi_desc_of_epi_left {W X Y : C} [has_binary_coproduct X Y] (f : X ⟶ W) (g : Y ⟶ W)
[epi f] : epi (coprod.desc f g) :=
epi_of_epi_fac $ coprod.inl_desc _ _
instance coprod.epi_desc_of_epi_right {W X Y : C} [has_binary_coproduct X Y] (f : X ⟶ W) (g : Y ⟶ W)
[epi g] : epi (coprod.desc f g) :=
epi_of_epi_fac $ coprod.inr_desc _ _
/-- If the product of `X` and `Y` exists, then every pair of morphisms `f : W ⟶ X` and `g : W ⟶ Y`
induces a morphism `l : W ⟶ X ⨯ Y` satisfying `l ≫ prod.fst = f` and `l ≫ prod.snd = g`. -/
def prod.lift' {W X Y : C} [has_binary_product X Y] (f : W ⟶ X) (g : W ⟶ Y) :
{l : W ⟶ X ⨯ Y // l ≫ prod.fst = f ∧ l ≫ prod.snd = g} :=
⟨prod.lift f g, prod.lift_fst _ _, prod.lift_snd _ _⟩
/-- If the coproduct of `X` and `Y` exists, then every pair of morphisms `f : X ⟶ W` and
`g : Y ⟶ W` induces a morphism `l : X ⨿ Y ⟶ W` satisfying `coprod.inl ≫ l = f` and
`coprod.inr ≫ l = g`. -/
def coprod.desc' {W X Y : C} [has_binary_coproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) :
{l : X ⨿ Y ⟶ W // coprod.inl ≫ l = f ∧ coprod.inr ≫ l = g} :=
⟨coprod.desc f g, coprod.inl_desc _ _, coprod.inr_desc _ _⟩
/-- If the products `W ⨯ X` and `Y ⨯ Z` exist, then every pair of morphisms `f : W ⟶ Y` and
`g : X ⟶ Z` induces a morphism `prod.map f g : W ⨯ X ⟶ Y ⨯ Z`. -/
def prod.map {W X Y Z : C} [has_binary_product W X] [has_binary_product Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) : W ⨯ X ⟶ Y ⨯ Z :=
lim_map (map_pair f g)
/-- If the coproducts `W ⨿ X` and `Y ⨿ Z` exist, then every pair of morphisms `f : W ⟶ Y` and
`g : W ⟶ Z` induces a morphism `coprod.map f g : W ⨿ X ⟶ Y ⨿ Z`. -/
def coprod.map {W X Y Z : C} [has_binary_coproduct W X] [has_binary_coproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) : W ⨿ X ⟶ Y ⨿ Z :=
colim_map (map_pair f g)
section prod_lemmas
-- Making the reassoc version of this a simp lemma seems to be more harmful than helpful.
@[reassoc, simp]
lemma prod.comp_lift {V W X Y : C} [has_binary_product X Y] (f : V ⟶ W) (g : W ⟶ X) (h : W ⟶ Y) :
f ≫ prod.lift g h = prod.lift (f ≫ g) (f ≫ h) :=
by { ext; simp }
lemma prod.comp_diag {X Y : C} [has_binary_product Y Y] (f : X ⟶ Y) :
f ≫ diag Y = prod.lift f f :=
by simp
@[simp, reassoc]
lemma prod.map_fst {W X Y Z : C} [has_binary_product W X] [has_binary_product Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) : prod.map f g ≫ prod.fst = prod.fst ≫ f :=
lim_map_π _ _
@[simp, reassoc]
lemma prod.map_snd {W X Y Z : C} [has_binary_product W X] [has_binary_product Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) : prod.map f g ≫ prod.snd = prod.snd ≫ g :=
lim_map_π _ _
@[simp] lemma prod.map_id_id {X Y : C} [has_binary_product X Y] :
prod.map (𝟙 X) (𝟙 Y) = 𝟙 _ :=
by { ext; simp }
@[simp] lemma prod.lift_fst_snd {X Y : C} [has_binary_product X Y] :
prod.lift prod.fst prod.snd = 𝟙 (X ⨯ Y) :=
by { ext; simp }
@[simp, reassoc] lemma prod.lift_map {V W X Y Z : C} [has_binary_product W X] [has_binary_product Y Z]
(f : V ⟶ W) (g : V ⟶ X) (h : W ⟶ Y) (k : X ⟶ Z) :
prod.lift f g ≫ prod.map h k = prod.lift (f ≫ h) (g ≫ k) :=
by { ext; simp }
@[simp] lemma prod.lift_fst_comp_snd_comp {W X Y Z : C} [has_binary_product W Y] [has_binary_product X Z]
(g : W ⟶ X) (g' : Y ⟶ Z) :
prod.lift (prod.fst ≫ g) (prod.snd ≫ g') = prod.map g g' :=
by { rw ← prod.lift_map, simp }
-- We take the right hand side here to be simp normal form, as this way composition lemmas for
-- `f ≫ h` and `g ≫ k` can fire (eg `id_comp`) , while `map_fst` and `map_snd` can still work just
-- as well.
@[simp, reassoc]
lemma prod.map_map {A₁ A₂ A₃ B₁ B₂ B₃ : C}
[has_binary_product A₁ B₁] [has_binary_product A₂ B₂] [has_binary_product A₃ B₃]
(f : A₁ ⟶ A₂) (g : B₁ ⟶ B₂) (h : A₂ ⟶ A₃) (k : B₂ ⟶ B₃) :
prod.map f g ≫ prod.map h k = prod.map (f ≫ h) (g ≫ k) :=
by { ext; simp }
-- TODO: is it necessary to weaken the assumption here?
@[reassoc]
lemma prod.map_swap {A B X Y : C} (f : A ⟶ B) (g : X ⟶ Y) [has_limits_of_shape (discrete walking_pair) C] :
prod.map (𝟙 X) f ≫ prod.map g (𝟙 B) = prod.map g (𝟙 A) ≫ prod.map (𝟙 Y) f :=
by simp
@[reassoc] lemma prod.map_comp_id {X Y Z W : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_binary_product X W] [has_binary_product Z W] [has_binary_product Y W] :
prod.map (f ≫ g) (𝟙 W) = prod.map f (𝟙 W) ≫ prod.map g (𝟙 W) :=
by simp
@[reassoc] lemma prod.map_id_comp {X Y Z W : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_binary_product W X] [has_binary_product W Y] [has_binary_product W Z] :
prod.map (𝟙 W) (f ≫ g) = prod.map (𝟙 W) f ≫ prod.map (𝟙 W) g :=
by simp
/-- If the products `W ⨯ X` and `Y ⨯ Z` exist, then every pair of isomorphisms `f : W ≅ Y` and
`g : X ≅ Z` induces an isomorphism `prod.map_iso f g : W ⨯ X ≅ Y ⨯ Z`. -/
@[simps]
def prod.map_iso {W X Y Z : C} [has_binary_product W X] [has_binary_product Y Z]
(f : W ≅ Y) (g : X ≅ Z) : W ⨯ X ≅ Y ⨯ Z :=
{ hom := prod.map f.hom g.hom,
inv := prod.map f.inv g.inv }
@[simp, reassoc]
lemma prod.diag_map {X Y : C} (f : X ⟶ Y) [has_binary_product X X] [has_binary_product Y Y] :
diag X ≫ prod.map f f = f ≫ diag Y :=
by simp
@[simp, reassoc]
lemma prod.diag_map_fst_snd {X Y : C} [has_binary_product X Y] [has_binary_product (X ⨯ Y) (X ⨯ Y)] :
diag (X ⨯ Y) ≫ prod.map prod.fst prod.snd = 𝟙 (X ⨯ Y) :=
by simp
@[simp, reassoc]
lemma prod.diag_map_fst_snd_comp [has_limits_of_shape (discrete walking_pair) C]
{X X' Y Y' : C} (g : X ⟶ Y) (g' : X' ⟶ Y') :
diag (X ⨯ X') ≫ prod.map (prod.fst ≫ g) (prod.snd ≫ g') = prod.map g g' :=
by simp
instance {X : C} [has_binary_product X X] : split_mono (diag X) :=
{ retraction := prod.fst }
end prod_lemmas
section coprod_lemmas
@[simp, reassoc]
lemma coprod.desc_comp {V W X Y : C} [has_binary_coproduct X Y] (f : V ⟶ W) (g : X ⟶ V) (h : Y ⟶ V) :
coprod.desc g h ≫ f = coprod.desc (g ≫ f) (h ≫ f) :=
by { ext; simp }
lemma coprod.diag_comp {X Y : C} [has_binary_coproduct X X] (f : X ⟶ Y) :
codiag X ≫ f = coprod.desc f f :=
by simp
@[simp, reassoc]
lemma coprod.inl_map {W X Y Z : C} [has_binary_coproduct W X] [has_binary_coproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) : coprod.inl ≫ coprod.map f g = f ≫ coprod.inl :=
ι_colim_map _ _
@[simp, reassoc]
lemma coprod.inr_map {W X Y Z : C} [has_binary_coproduct W X] [has_binary_coproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) : coprod.inr ≫ coprod.map f g = g ≫ coprod.inr :=
ι_colim_map _ _
@[simp]
lemma coprod.map_id_id {X Y : C} [has_binary_coproduct X Y] :
coprod.map (𝟙 X) (𝟙 Y) = 𝟙 _ :=
by { ext; simp }
@[simp]
lemma coprod.desc_inl_inr {X Y : C} [has_binary_coproduct X Y] :
coprod.desc coprod.inl coprod.inr = 𝟙 (X ⨿ Y) :=
by { ext; simp }
-- The simp linter says simp can prove the reassoc version of this lemma.
@[reassoc, simp]
lemma coprod.map_desc {S T U V W : C} [has_binary_coproduct U W] [has_binary_coproduct T V]
(f : U ⟶ S) (g : W ⟶ S) (h : T ⟶ U) (k : V ⟶ W) :
coprod.map h k ≫ coprod.desc f g = coprod.desc (h ≫ f) (k ≫ g) :=
by { ext; simp }
@[simp]
lemma coprod.desc_comp_inl_comp_inr {W X Y Z : C}
[has_binary_coproduct W Y] [has_binary_coproduct X Z]
(g : W ⟶ X) (g' : Y ⟶ Z) :
coprod.desc (g ≫ coprod.inl) (g' ≫ coprod.inr) = coprod.map g g' :=
by { rw ← coprod.map_desc, simp }
-- We take the right hand side here to be simp normal form, as this way composition lemmas for
-- `f ≫ h` and `g ≫ k` can fire (eg `id_comp`) , while `inl_map` and `inr_map` can still work just
-- as well.
@[simp, reassoc]
lemma coprod.map_map {A₁ A₂ A₃ B₁ B₂ B₃ : C}
[has_binary_coproduct A₁ B₁] [has_binary_coproduct A₂ B₂] [has_binary_coproduct A₃ B₃]
(f : A₁ ⟶ A₂) (g : B₁ ⟶ B₂) (h : A₂ ⟶ A₃) (k : B₂ ⟶ B₃) :
coprod.map f g ≫ coprod.map h k = coprod.map (f ≫ h) (g ≫ k) :=
by { ext; simp }
-- I don't think it's a good idea to make any of the following three simp lemmas.
@[reassoc]
lemma coprod.map_swap {A B X Y : C} (f : A ⟶ B) (g : X ⟶ Y) [has_colimits_of_shape (discrete walking_pair) C] :
coprod.map (𝟙 X) f ≫ coprod.map g (𝟙 B) = coprod.map g (𝟙 A) ≫ coprod.map (𝟙 Y) f :=
by simp
@[reassoc] lemma coprod.map_comp_id {X Y Z W : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_binary_coproduct Z W] [has_binary_coproduct Y W] [has_binary_coproduct X W] :
coprod.map (f ≫ g) (𝟙 W) = coprod.map f (𝟙 W) ≫ coprod.map g (𝟙 W) :=
by simp
@[reassoc] lemma coprod.map_id_comp {X Y Z W : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_binary_coproduct W X] [has_binary_coproduct W Y] [has_binary_coproduct W Z] :
coprod.map (𝟙 W) (f ≫ g) = coprod.map (𝟙 W) f ≫ coprod.map (𝟙 W) g :=
by simp
/-- If the coproducts `W ⨿ X` and `Y ⨿ Z` exist, then every pair of isomorphisms `f : W ≅ Y` and
`g : W ≅ Z` induces a isomorphism `coprod.map_iso f g : W ⨿ X ≅ Y ⨿ Z`. -/
@[simps]
def coprod.map_iso {W X Y Z : C} [has_binary_coproduct W X] [has_binary_coproduct Y Z]
(f : W ≅ Y) (g : X ≅ Z) : W ⨿ X ≅ Y ⨿ Z :=
{ hom := coprod.map f.hom g.hom,
inv := coprod.map f.inv g.inv }
-- The simp linter says simp can prove the reassoc version of this lemma.
@[reassoc, simp]
lemma coprod.map_codiag {X Y : C} (f : X ⟶ Y) [has_binary_coproduct X X] [has_binary_coproduct Y Y] :
coprod.map f f ≫ codiag Y = codiag X ≫ f :=
by simp
-- The simp linter says simp can prove the reassoc version of this lemma.
@[reassoc, simp]
lemma coprod.map_inl_inr_codiag {X Y : C} [has_binary_coproduct X Y] [has_binary_coproduct (X ⨿ Y) (X ⨿ Y)] :
coprod.map coprod.inl coprod.inr ≫ codiag (X ⨿ Y) = 𝟙 (X ⨿ Y) :=
by simp
-- The simp linter says simp can prove the reassoc version of this lemma.
@[reassoc, simp]
lemma coprod.map_comp_inl_inr_codiag [has_colimits_of_shape (discrete walking_pair) C]
{X X' Y Y' : C} (g : X ⟶ Y) (g' : X' ⟶ Y') :
coprod.map (g ≫ coprod.inl) (g' ≫ coprod.inr) ≫ codiag (Y ⨿ Y') = coprod.map g g' :=
by simp
end coprod_lemmas
variables (C)
/--
`has_binary_products` represents a choice of product for every pair of objects.
See https://stacks.math.columbia.edu/tag/001T.
-/
abbreviation has_binary_products := has_limits_of_shape (discrete walking_pair) C
/--
`has_binary_coproducts` represents a choice of coproduct for every pair of objects.
See https://stacks.math.columbia.edu/tag/04AP.
-/
abbreviation has_binary_coproducts := has_colimits_of_shape (discrete walking_pair) C
/-- If `C` has all limits of diagrams `pair X Y`, then it has all binary products -/
lemma has_binary_products_of_has_limit_pair [Π {X Y : C}, has_limit (pair X Y)] :
has_binary_products C :=
{ has_limit := λ F, has_limit_of_iso (diagram_iso_pair F).symm }
/-- If `C` has all colimits of diagrams `pair X Y`, then it has all binary coproducts -/
lemma has_binary_coproducts_of_has_colimit_pair [Π {X Y : C}, has_colimit (pair X Y)] :
has_binary_coproducts C :=
{ has_colimit := λ F, has_colimit_of_iso (diagram_iso_pair F) }
section prod_functor
variables {C} [has_binary_products C]
-- FIXME deterministic timeout with `-T50000`
/-- The binary product functor. -/
@[simps]
def prod.functor : C ⥤ C ⥤ C :=
{ obj := λ X, { obj := λ Y, X ⨯ Y, map := λ Y Z, prod.map (𝟙 X) },
map := λ Y Z f, { app := λ T, prod.map f (𝟙 T) }}
end prod_functor
section prod_comparison
variables {C} [has_binary_products C]
variables {D : Type u₂} [category.{v} D]
variables (F : C ⥤ D) {A A' B B' : C}
variables [has_binary_product (F.obj A) (F.obj B)] [has_binary_product (F.obj A') (F.obj B')]
/--
The product comparison morphism.
In `category_theory/limits/preserves` we show this is always an iso iff F preserves binary products.
-/
def prod_comparison (F : C ⥤ D) (A B : C) [has_binary_product (F.obj A) (F.obj B)] :
F.obj (A ⨯ B) ⟶ F.obj A ⨯ F.obj B :=
prod.lift (F.map prod.fst) (F.map prod.snd)
/-- Naturality of the prod_comparison morphism in both arguments. -/
@[reassoc] lemma prod_comparison_natural (f : A ⟶ A') (g : B ⟶ B') :
F.map (prod.map f g) ≫ prod_comparison F A' B' = prod_comparison F A B ≫ prod.map (F.map f) (F.map g) :=
begin
rw [prod_comparison, prod_comparison, prod.lift_map, ← F.map_comp, ← F.map_comp,
prod.comp_lift, ← F.map_comp, prod.map_fst, ← F.map_comp, prod.map_snd]
end
@[reassoc]
lemma inv_prod_comparison_map_fst [is_iso (prod_comparison F A B)] :
inv (prod_comparison F A B) ≫ F.map prod.fst = prod.fst :=
begin
erw (as_iso (prod_comparison F A B)).inv_comp_eq,
dsimp [as_iso_hom, prod_comparison],
rw prod.lift_fst,
end
@[reassoc]
lemma inv_prod_comparison_map_snd [is_iso (prod_comparison F A B)] :
inv (prod_comparison F A B) ≫ F.map prod.snd = prod.snd :=
begin
erw (as_iso (prod_comparison F A B)).inv_comp_eq,
dsimp [as_iso_hom, prod_comparison],
rw prod.lift_snd,
end
/-- If the product comparison morphism is an iso, its inverse is natural. -/
@[reassoc]
lemma prod_comparison_inv_natural (f : A ⟶ A') (g : B ⟶ B')
[is_iso (prod_comparison F A B)] [is_iso (prod_comparison F A' B')] :
inv (prod_comparison F A B) ≫ F.map (prod.map f g) = prod.map (F.map f) (F.map g) ≫ inv (prod_comparison F A' B') :=
by { erw [(as_iso (prod_comparison F A' B')).eq_comp_inv, category.assoc,
(as_iso (prod_comparison F A B)).inv_comp_eq, prod_comparison_natural], refl }
end prod_comparison
end category_theory.limits
|
6a9d238b9af5bfc9be20be0f927a2797c3d64cc2 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/topology/dense_embedding.lean | cb8bba18d0a98639e07e582609d6f4dfc69b8037 | [
"Apache-2.0"
] | permissive | jcommelin/mathlib | d8456447c36c176e14d96d9e76f39841f69d2d9b | ee8279351a2e434c2852345c51b728d22af5a156 | refs/heads/master | 1,664,782,136,488 | 1,663,638,983,000 | 1,663,638,983,000 | 132,563,656 | 0 | 0 | Apache-2.0 | 1,663,599,929,000 | 1,525,760,539,000 | Lean | UTF-8 | Lean | false | false | 15,300 | lean | /-
Copyright (c) 2019 Reid Barton. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot
-/
import topology.separation
import topology.bases
/-!
# Dense embeddings
This file defines three properties of functions:
* `dense_range f` means `f` has dense image;
* `dense_inducing i` means `i` is also `inducing`;
* `dense_embedding e` means `e` is also an `embedding`.
The main theorem `continuous_extend` gives a criterion for a function
`f : X → Z` to a T₃ space Z to extend along a dense embedding
`i : X → Y` to a continuous function `g : Y → Z`. Actually `i` only
has to be `dense_inducing` (not necessarily injective).
-/
noncomputable theory
open set filter
open_locale classical topological_space filter
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
/-- `i : α → β` is "dense inducing" if it has dense range and the topology on `α`
is the one induced by `i` from the topology on `β`. -/
@[protect_proj] structure dense_inducing [topological_space α] [topological_space β] (i : α → β)
extends inducing i : Prop :=
(dense : dense_range i)
namespace dense_inducing
variables [topological_space α] [topological_space β]
variables {i : α → β} (di : dense_inducing i)
lemma nhds_eq_comap (di : dense_inducing i) :
∀ a : α, 𝓝 a = comap i (𝓝 $ i a) :=
di.to_inducing.nhds_eq_comap
protected lemma continuous (di : dense_inducing i) : continuous i :=
di.to_inducing.continuous
lemma closure_range : closure (range i) = univ :=
di.dense.closure_range
protected lemma preconnected_space [preconnected_space α] (di : dense_inducing i) :
preconnected_space β :=
di.dense.preconnected_space di.continuous
lemma closure_image_mem_nhds {s : set α} {a : α} (di : dense_inducing i) (hs : s ∈ 𝓝 a) :
closure (i '' s) ∈ 𝓝 (i a) :=
begin
rw [di.nhds_eq_comap a, ((nhds_basis_opens _).comap _).mem_iff] at hs,
rcases hs with ⟨U, ⟨haU, hUo⟩, sub : i ⁻¹' U ⊆ s⟩,
refine mem_of_superset (hUo.mem_nhds haU) _,
calc U ⊆ closure (i '' (i ⁻¹' U)) : di.dense.subset_closure_image_preimage_of_is_open hUo
... ⊆ closure (i '' s) : closure_mono (image_subset i sub)
end
lemma dense_image (di : dense_inducing i) {s : set α} : dense (i '' s) ↔ dense s :=
begin
refine ⟨λ H x, _, di.dense.dense_image di.continuous⟩,
rw [di.to_inducing.closure_eq_preimage_closure_image, H.closure_eq, preimage_univ],
trivial
end
/-- If `i : α → β` is a dense embedding with dense complement of the range, then any compact set in
`α` has empty interior. -/
lemma interior_compact_eq_empty [t2_space β] (di : dense_inducing i) (hd : dense (range i)ᶜ)
{s : set α} (hs : is_compact s) : interior s = ∅ :=
begin
refine eq_empty_iff_forall_not_mem.2 (λ x hx, _),
rw [mem_interior_iff_mem_nhds] at hx,
have := di.closure_image_mem_nhds hx,
rw (hs.image di.continuous).is_closed.closure_eq at this,
rcases hd.inter_nhds_nonempty this with ⟨y, hyi, hys⟩,
exact hyi (image_subset_range _ _ hys)
end
/-- The product of two dense inducings is a dense inducing -/
protected lemma prod [topological_space γ] [topological_space δ]
{e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_inducing e₁) (de₂ : dense_inducing e₂) :
dense_inducing (λ(p : α × γ), (e₁ p.1, e₂ p.2)) :=
{ induced := (de₁.to_inducing.prod_mk de₂.to_inducing).induced,
dense := de₁.dense.prod_map de₂.dense }
open topological_space
/-- If the domain of a `dense_inducing` map is a separable space, then so is the codomain. -/
protected lemma separable_space [separable_space α] : separable_space β :=
di.dense.separable_space di.continuous
variables [topological_space δ] {f : γ → α} {g : γ → δ} {h : δ → β}
/--
```
γ -f→ α
g↓ ↓e
δ -h→ β
```
-/
lemma tendsto_comap_nhds_nhds {d : δ} {a : α} (di : dense_inducing i)
(H : tendsto h (𝓝 d) (𝓝 (i a))) (comm : h ∘ g = i ∘ f) : tendsto f (comap g (𝓝 d)) (𝓝 a) :=
begin
have lim1 : map g (comap g (𝓝 d)) ≤ 𝓝 d := map_comap_le,
replace lim1 : map h (map g (comap g (𝓝 d))) ≤ map h (𝓝 d) := map_mono lim1,
rw [filter.map_map, comm, ← filter.map_map, map_le_iff_le_comap] at lim1,
have lim2 : comap i (map h (𝓝 d)) ≤ comap i (𝓝 (i a)) := comap_mono H,
rw ← di.nhds_eq_comap at lim2,
exact le_trans lim1 lim2,
end
protected lemma nhds_within_ne_bot (di : dense_inducing i) (b : β) :
ne_bot (𝓝[range i] b) :=
di.dense.nhds_within_ne_bot b
lemma comap_nhds_ne_bot (di : dense_inducing i) (b : β) : ne_bot (comap i (𝓝 b)) :=
comap_ne_bot $ λ s hs,
let ⟨_, ⟨ha, a, rfl⟩⟩ := mem_closure_iff_nhds.1 (di.dense b) s hs in ⟨a, ha⟩
variables [topological_space γ]
/-- If `i : α → β` is a dense inducing, then any function `f : α → γ` "extends"
to a function `g = extend di f : β → γ`. If `γ` is Hausdorff and `f` has a
continuous extension, then `g` is the unique such extension. In general,
`g` might not be continuous or even extend `f`. -/
def extend (di : dense_inducing i) (f : α → γ) (b : β) : γ :=
@@lim _ ⟨f (di.dense.some b)⟩ (comap i (𝓝 b)) f
lemma extend_eq_of_tendsto [t2_space γ] {b : β} {c : γ} {f : α → γ}
(hf : tendsto f (comap i (𝓝 b)) (𝓝 c)) :
di.extend f b = c :=
by haveI := di.comap_nhds_ne_bot; exact hf.lim_eq
lemma extend_eq_at [t2_space γ] {f : α → γ} {a : α} (hf : continuous_at f a) :
di.extend f (i a) = f a :=
extend_eq_of_tendsto _ $ di.nhds_eq_comap a ▸ hf
lemma extend_eq_at' [t2_space γ] {f : α → γ} {a : α} (c : γ) (hf : tendsto f (𝓝 a) (𝓝 c)) :
di.extend f (i a) = f a :=
di.extend_eq_at (continuous_at_of_tendsto_nhds hf)
lemma extend_eq [t2_space γ] {f : α → γ} (hf : continuous f) (a : α) :
di.extend f (i a) = f a :=
di.extend_eq_at hf.continuous_at
/-- Variation of `extend_eq` where we ask that `f` has a limit along `comap i (𝓝 b)` for each
`b : β`. This is a strictly stronger assumption than continuity of `f`, but in a lot of cases
you'd have to prove it anyway to use `continuous_extend`, so this avoids doing the work twice. -/
lemma extend_eq' [t2_space γ] {f : α → γ}
(di : dense_inducing i) (hf : ∀ b, ∃ c, tendsto f (comap i (𝓝 b)) (𝓝 c)) (a : α) :
di.extend f (i a) = f a :=
begin
rcases hf (i a) with ⟨b, hb⟩,
refine di.extend_eq_at' b _,
rwa ← di.to_inducing.nhds_eq_comap at hb,
end
lemma extend_unique_at [t2_space γ] {b : β} {f : α → γ} {g : β → γ} (di : dense_inducing i)
(hf : ∀ᶠ x in comap i (𝓝 b), g (i x) = f x) (hg : continuous_at g b) :
di.extend f b = g b :=
begin
refine di.extend_eq_of_tendsto (λ s hs, mem_map.2 _),
suffices : ∀ᶠ (x : α) in comap i (𝓝 b), g (i x) ∈ s,
from hf.mp (this.mono $ λ x hgx hfx, hfx ▸ hgx),
clear hf f,
refine eventually_comap.2 ((hg.eventually hs).mono _),
rintros _ hxs x rfl,
exact hxs
end
lemma extend_unique [t2_space γ] {f : α → γ} {g : β → γ} (di : dense_inducing i)
(hf : ∀ x, g (i x) = f x) (hg : continuous g) :
di.extend f = g :=
funext $ λ b, extend_unique_at di (eventually_of_forall hf) hg.continuous_at
lemma continuous_at_extend [t3_space γ] {b : β} {f : α → γ} (di : dense_inducing i)
(hf : ∀ᶠ x in 𝓝 b, ∃c, tendsto f (comap i $ 𝓝 x) (𝓝 c)) :
continuous_at (di.extend f) b :=
begin
set φ := di.extend f,
haveI := di.comap_nhds_ne_bot,
suffices : ∀ V' ∈ 𝓝 (φ b), is_closed V' → φ ⁻¹' V' ∈ 𝓝 b,
by simpa [continuous_at, (closed_nhds_basis _).tendsto_right_iff],
intros V' V'_in V'_closed,
set V₁ := {x | tendsto f (comap i $ 𝓝 x) (𝓝 $ φ x)},
have V₁_in : V₁ ∈ 𝓝 b,
{ filter_upwards [hf],
rintros x ⟨c, hc⟩,
dsimp [V₁, φ],
rwa di.extend_eq_of_tendsto hc },
obtain ⟨V₂, V₂_in, V₂_op, hV₂⟩ : ∃ V₂ ∈ 𝓝 b, is_open V₂ ∧ ∀ x ∈ i ⁻¹' V₂, f x ∈ V',
{ simpa [and_assoc] using ((nhds_basis_opens' b).comap i).tendsto_left_iff.mp
(mem_of_mem_nhds V₁_in : b ∈ V₁) V' V'_in },
suffices : ∀ x ∈ V₁ ∩ V₂, φ x ∈ V',
{ filter_upwards [inter_mem V₁_in V₂_in] using this, },
rintros x ⟨x_in₁, x_in₂⟩,
have hV₂x : V₂ ∈ 𝓝 x := is_open.mem_nhds V₂_op x_in₂,
apply V'_closed.mem_of_tendsto x_in₁,
use V₂,
tauto,
end
lemma continuous_extend [t3_space γ] {f : α → γ} (di : dense_inducing i)
(hf : ∀b, ∃c, tendsto f (comap i (𝓝 b)) (𝓝 c)) : continuous (di.extend f) :=
continuous_iff_continuous_at.mpr $ assume b, di.continuous_at_extend $ univ_mem' hf
lemma mk'
(i : α → β)
(c : continuous i)
(dense : ∀x, x ∈ closure (range i))
(H : ∀ (a:α) s ∈ 𝓝 a,
∃t ∈ 𝓝 (i a), ∀ b, i b ∈ t → b ∈ s) :
dense_inducing i :=
{ induced := (induced_iff_nhds_eq i).2 $
λ a, le_antisymm (tendsto_iff_comap.1 $ c.tendsto _) (by simpa [filter.le_def] using H a),
dense := dense }
end dense_inducing
/-- A dense embedding is an embedding with dense image. -/
structure dense_embedding [topological_space α] [topological_space β] (e : α → β)
extends dense_inducing e : Prop :=
(inj : function.injective e)
theorem dense_embedding.mk'
[topological_space α] [topological_space β] (e : α → β)
(c : continuous e)
(dense : dense_range e)
(inj : function.injective e)
(H : ∀ (a:α) s ∈ 𝓝 a,
∃t ∈ 𝓝 (e a), ∀ b, e b ∈ t → b ∈ s) :
dense_embedding e :=
{ inj := inj,
..dense_inducing.mk' e c dense H}
namespace dense_embedding
open topological_space
variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ]
variables {e : α → β} (de : dense_embedding e)
lemma inj_iff {x y} : e x = e y ↔ x = y := de.inj.eq_iff
lemma to_embedding : embedding e :=
{ induced := de.induced,
inj := de.inj }
/-- If the domain of a `dense_embedding` is a separable space, then so is its codomain. -/
protected lemma separable_space [separable_space α] : separable_space β :=
de.to_dense_inducing.separable_space
/-- The product of two dense embeddings is a dense embedding. -/
protected lemma prod {e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_embedding e₁)
(de₂ : dense_embedding e₂) :
dense_embedding (λ(p : α × γ), (e₁ p.1, e₂ p.2)) :=
{ inj := assume ⟨x₁, x₂⟩ ⟨y₁, y₂⟩,
by simp; exact assume h₁ h₂, ⟨de₁.inj h₁, de₂.inj h₂⟩,
..dense_inducing.prod de₁.to_dense_inducing de₂.to_dense_inducing }
/-- The dense embedding of a subtype inside its closure. -/
@[simps] def subtype_emb {α : Type*} (p : α → Prop) (e : α → β) (x : {x // p x}) :
{x // x ∈ closure (e '' {x | p x})} :=
⟨e x, subset_closure $ mem_image_of_mem e x.prop⟩
protected lemma subtype (p : α → Prop) : dense_embedding (subtype_emb p e) :=
{ dense := dense_iff_closure_eq.2 $
begin
ext ⟨x, hx⟩,
rw image_eq_range at hx,
simpa [closure_subtype, ← range_comp, (∘)],
end,
inj := (de.inj.comp subtype.coe_injective).cod_restrict _,
induced := (induced_iff_nhds_eq _).2 (assume ⟨x, hx⟩,
by simp [subtype_emb, nhds_subtype_eq_comap, de.to_inducing.nhds_eq_comap, comap_comap, (∘)]) }
lemma dense_image {s : set α} : dense (e '' s) ↔ dense s :=
de.to_dense_inducing.dense_image
end dense_embedding
lemma dense.dense_embedding_coe [topological_space α] {s : set α} (hs : dense s) :
dense_embedding (coe : s → α) :=
{ dense := hs.dense_range_coe,
.. embedding_subtype_coe }
lemma is_closed_property [topological_space β] {e : α → β} {p : β → Prop}
(he : dense_range e) (hp : is_closed {x | p x}) (h : ∀a, p (e a)) :
∀b, p b :=
have univ ⊆ {b | p b},
from calc univ = closure (range e) : he.closure_range.symm
... ⊆ closure {b | p b} : closure_mono $ range_subset_iff.mpr h
... = _ : hp.closure_eq,
assume b, this trivial
lemma is_closed_property2 [topological_space β] {e : α → β} {p : β → β → Prop}
(he : dense_range e) (hp : is_closed {q:β×β | p q.1 q.2}) (h : ∀a₁ a₂, p (e a₁) (e a₂)) :
∀b₁ b₂, p b₁ b₂ :=
have ∀q:β×β, p q.1 q.2,
from is_closed_property (he.prod_map he) hp $ λ _, h _ _,
assume b₁ b₂, this ⟨b₁, b₂⟩
lemma is_closed_property3 [topological_space β] {e : α → β} {p : β → β → β → Prop}
(he : dense_range e) (hp : is_closed {q:β×β×β | p q.1 q.2.1 q.2.2})
(h : ∀a₁ a₂ a₃, p (e a₁) (e a₂) (e a₃)) :
∀b₁ b₂ b₃, p b₁ b₂ b₃ :=
have ∀q:β×β×β, p q.1 q.2.1 q.2.2,
from is_closed_property (he.prod_map $ he.prod_map he) hp $ λ _, h _ _ _,
assume b₁ b₂ b₃, this ⟨b₁, b₂, b₃⟩
@[elab_as_eliminator]
lemma dense_range.induction_on [topological_space β] {e : α → β} (he : dense_range e) {p : β → Prop}
(b₀ : β) (hp : is_closed {b | p b}) (ih : ∀a:α, p $ e a) : p b₀ :=
is_closed_property he hp ih b₀
@[elab_as_eliminator]
lemma dense_range.induction_on₂ [topological_space β] {e : α → β} {p : β → β → Prop}
(he : dense_range e) (hp : is_closed {q:β×β | p q.1 q.2}) (h : ∀a₁ a₂, p (e a₁) (e a₂))
(b₁ b₂ : β) : p b₁ b₂ := is_closed_property2 he hp h _ _
@[elab_as_eliminator]
lemma dense_range.induction_on₃ [topological_space β] {e : α → β} {p : β → β → β → Prop}
(he : dense_range e) (hp : is_closed {q:β×β×β | p q.1 q.2.1 q.2.2})
(h : ∀a₁ a₂ a₃, p (e a₁) (e a₂) (e a₃))
(b₁ b₂ b₃ : β) : p b₁ b₂ b₃ := is_closed_property3 he hp h _ _ _
section
variables [topological_space β] [topological_space γ] [t2_space γ]
variables {f : α → β}
/-- Two continuous functions to a t2-space that agree on the dense range of a function are equal. -/
lemma dense_range.equalizer (hfd : dense_range f)
{g h : β → γ} (hg : continuous g) (hh : continuous h) (H : g ∘ f = h ∘ f) :
g = h :=
funext $ λ y, hfd.induction_on y (is_closed_eq hg hh) $ congr_fun H
end
-- Bourbaki GT III §3 no.4 Proposition 7 (generalised to any dense-inducing map to a T₃ space)
lemma filter.has_basis.has_basis_of_dense_inducing
[topological_space α] [topological_space β] [t3_space β]
{ι : Type*} {s : ι → set α} {p : ι → Prop} {x : α} (h : (𝓝 x).has_basis p s)
{f : α → β} (hf : dense_inducing f) :
(𝓝 (f x)).has_basis p $ λ i, closure $ f '' (s i) :=
begin
rw filter.has_basis_iff at h ⊢,
intros T,
refine ⟨λ hT, _, λ hT, _⟩,
{ obtain ⟨T', hT₁, hT₂, hT₃⟩ := nhds_is_closed hT,
have hT₄ : f⁻¹' T' ∈ 𝓝 x,
{ rw hf.to_inducing.nhds_eq_comap x,
exact ⟨T', hT₁, subset.rfl⟩, },
obtain ⟨i, hi, hi'⟩ := (h _).mp hT₄,
exact ⟨i, hi, (closure_mono (image_subset f hi')).trans (subset.trans (closure_minimal
(image_subset_iff.mpr subset.rfl) hT₃) hT₂)⟩, },
{ obtain ⟨i, hi, hi'⟩ := hT,
suffices : closure (f '' s i) ∈ 𝓝 (f x), { filter_upwards [this] using hi', },
replace h := (h (s i)).mpr ⟨i, hi, subset.rfl⟩,
exact hf.closure_image_mem_nhds h, },
end
|
ac82182efd64348de1f3be75228deef26ea0fd59 | 75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2 | /library/theories/group_theory/perm.lean | 4ff686b6e6a941b563f86a83972234e7137d1871 | [
"Apache-2.0"
] | permissive | jroesch/lean | 30ef0860fa905d35b9ad6f76de1a4f65c9af6871 | 3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2 | refs/heads/master | 1,586,090,835,348 | 1,455,142,203,000 | 1,455,142,277,000 | 51,536,958 | 1 | 0 | null | 1,455,215,811,000 | 1,455,215,811,000 | null | UTF-8 | Lean | false | false | 4,039 | lean | /-
Copyright (c) 2015 Haitao Zhang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author : Haitao Zhang
-/
import algebra.group data data.fintype.function
open nat list function
namespace group_theory
open fintype
section perm
variables {A : Type} [fintype A] [decidable_eq A]
variable {f : A → A}
lemma perm_surj : injective f → surjective f :=
surj_of_inj_eq_card (eq.refl (card A))
variable (perm : injective f)
definition perm_inv : A → A :=
right_inv (perm_surj perm)
lemma perm_inv_right : f ∘ (perm_inv perm) = id :=
right_inv_of_surj (perm_surj perm)
lemma perm_inv_left : (perm_inv perm) ∘ f = id :=
have H : left_inverse f (perm_inv perm), from congr_fun (perm_inv_right perm),
funext (take x, right_inverse_of_injective_of_left_inverse perm H x)
lemma perm_inv_inj : injective (perm_inv perm) :=
injective_of_has_left_inverse (exists.intro f (congr_fun (perm_inv_right perm)))
end perm
structure perm (A : Type) [h : fintype A] : Type :=
(f : A → A) (inj : injective f)
local attribute perm.f [coercion]
section perm
variables {A : Type} [fintype A]
lemma eq_of_feq : ∀ {p₁ p₂ : perm A}, (perm.f p₁) = p₂ → p₁ = p₂
| (perm.mk f₁ P₁) (perm.mk f₂ P₂) := assume (feq : f₁ = f₂), by congruence; assumption
lemma feq_of_eq : ∀ {p₁ p₂ : perm A}, p₁ = p₂ → (perm.f p₁) = p₂
| (perm.mk f₁ P₁) (perm.mk f₂ P₂) := assume Peq,
have feq : f₁ = f₂, from perm.no_confusion Peq (λ Pe Pqe, Pe), feq
lemma eq_iff_feq {p₁ p₂ : perm A} : (perm.f p₁) = p₂ ↔ p₁ = p₂ :=
iff.intro eq_of_feq feq_of_eq
lemma perm.f_mk {f : A → A} {Pinj : injective f} : perm.f (perm.mk f Pinj) = f := rfl
definition move_by [reducible] (a : A) (f : perm A) : A := f a
variable [decidable_eq A]
lemma perm.has_decidable_eq [instance] : decidable_eq (perm A) :=
take f g,
perm.destruct f (λ ff finj, perm.destruct g (λ gf ginj,
decidable.rec_on (decidable_eq_fun ff gf)
(λ Peq, decidable.inl (by substvars))
(λ Pne, decidable.inr begin intro P, injection P, contradiction end)))
lemma dinj_perm_mk : dinj (@injective A A) perm.mk :=
take a₁ a₂ Pa₁ Pa₂ Pmkeq, perm.no_confusion Pmkeq (λ Pe Pqe, Pe)
definition all_perms : list (perm A) :=
dmap injective perm.mk (all_injs A)
lemma nodup_all_perms : nodup (@all_perms A _ _) :=
dmap_nodup_of_dinj dinj_perm_mk nodup_all_injs
lemma all_perms_complete : ∀ p : perm A, p ∈ all_perms :=
take p, perm.destruct p (take f Pinj,
assert Pin : f ∈ all_injs A, from all_injs_complete Pinj,
mem_dmap Pinj Pin)
definition perm_is_fintype [instance] : fintype (perm A) :=
fintype.mk all_perms nodup_all_perms all_perms_complete
definition perm.mul (f g : perm A) :=
perm.mk (f∘g) (injective_compose (perm.inj f) (perm.inj g))
definition perm.one [reducible] : perm A := perm.mk id injective_id
definition perm.inv (f : perm A) := let inj := perm.inj f in
perm.mk (perm_inv inj) (perm_inv_inj inj)
local infix `^` := perm.mul
lemma perm.assoc (f g h : perm A) : f ^ g ^ h = f ^ (g ^ h) := rfl
lemma perm.one_mul (p : perm A) : perm.one ^ p = p :=
perm.cases_on p (λ f inj, rfl)
lemma perm.mul_one (p : perm A) : p ^ perm.one = p :=
perm.cases_on p (λ f inj, rfl)
lemma perm.left_inv (p : perm A) : (perm.inv p) ^ p = perm.one :=
begin rewrite [↑perm.one], generalize @injective_id A,
rewrite [-perm_inv_left (perm.inj p)], intros, exact rfl
end
lemma perm.right_inv (p : perm A) : p ^ (perm.inv p) = perm.one :=
begin rewrite [↑perm.one], generalize @injective_id A,
rewrite [-perm_inv_right (perm.inj p)], intros, exact rfl
end
definition perm_group [instance] : group (perm A) :=
group.mk perm.mul perm.assoc perm.one perm.one_mul perm.mul_one perm.inv perm.left_inv
lemma perm_one : (1 : perm A) = perm.one := rfl
end perm
end group_theory
|
e34b911765540b2e10ea58c5ed49f1ece16f9b36 | e0f9ba56b7fedc16ef8697f6caeef5898b435143 | /src/topology/algebra/module.lean | 8de3c4f6193f523a215b3b5aff0818fc01cccf21 | [
"Apache-2.0"
] | permissive | anrddh/mathlib | 6a374da53c7e3a35cb0298b0cd67824efef362b4 | a4266a01d2dcb10de19369307c986d038c7bb6a6 | refs/heads/master | 1,656,710,827,909 | 1,589,560,456,000 | 1,589,560,456,000 | 264,271,800 | 0 | 0 | Apache-2.0 | 1,589,568,062,000 | 1,589,568,061,000 | null | UTF-8 | Lean | false | false | 33,907 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jan-David Salchow, Sébastien Gouëzel, Jean Lo, Yury Kudryashov
-/
import topology.algebra.ring
import ring_theory.algebra
/-!
# Theory of topological modules and continuous linear maps.
We define classes `topological_semimodule`, `topological_module` and `topological_vector_spaces`,
as extensions of the corresponding algebraic classes where the algebraic operations are continuous.
We also define continuous linear maps, as linear maps between topological modules which are
continuous. The set of continuous linear maps between the topological `R`-modules `M` and `M₂` is
denoted by `M →L[R] M₂`.
Continuous linear equivalences are denoted by `M ≃L[R] M₂`.
## Implementation notes
Topological vector spaces are defined as an `abbreviation` for topological modules,
if the base ring is a field. This has as advantage that topological vector spaces are completely
transparent for type class inference, which means that all instances for topological modules
are immediately picked up for vector spaces as well.
A cosmetic disadvantage is that one can not extend topological vector spaces.
The solution is to extend `topological_module` instead.
-/
open filter
open_locale topological_space
universes u v w u'
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A topological semimodule, over a semiring which is also a topological space, is a
semimodule in which scalar multiplication is continuous. In applications, R will be a topological
semiring and M a topological additive semigroup, but this is not needed for the definition -/
class topological_semimodule (R : Type u) (M : Type v)
[semiring R] [topological_space R]
[topological_space M] [add_comm_monoid M]
[semimodule R M] : Prop :=
(continuous_smul : continuous (λp : R × M, p.1 • p.2))
end prio
section
variables {R : Type u} {M : Type v}
[semiring R] [topological_space R]
[topological_space M] [add_comm_monoid M]
[semimodule R M] [topological_semimodule R M]
lemma continuous_smul : continuous (λp:R×M, p.1 • p.2) :=
topological_semimodule.continuous_smul
lemma continuous.smul {α : Type*} [topological_space α] {f : α → R} {g : α → M}
(hf : continuous f) (hg : continuous g) : continuous (λp, f p • g p) :=
continuous_smul.comp (hf.prod_mk hg)
lemma tendsto_smul {c : R} {x : M} : tendsto (λp:R×M, p.fst • p.snd) (𝓝 (c, x)) (𝓝 (c • x)) :=
continuous_smul.tendsto _
lemma filter.tendsto.smul {α : Type*} {l : filter α} {f : α → R} {g : α → M} {c : R} {x : M}
(hf : tendsto f l (𝓝 c)) (hg : tendsto g l (𝓝 x)) : tendsto (λ a, f a • g a) l (𝓝 (c • x)) :=
tendsto_smul.comp (hf.prod_mk_nhds hg)
end
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A topological module, over a ring which is also a topological space, is a module in which
scalar multiplication is continuous. In applications, `R` will be a topological ring and `M` a
topological additive group, but this is not needed for the definition -/
class topological_module (R : Type u) (M : Type v)
[ring R] [topological_space R]
[topological_space M] [add_comm_group M]
[module R M]
extends topological_semimodule R M : Prop
/-- A topological vector space is a topological module over a field. -/
abbreviation topological_vector_space (R : Type u) (M : Type v)
[field R] [topological_space R]
[topological_space M] [add_comm_group M] [module R M] :=
topological_module R M
end prio
section
variables {R : Type*} {M : Type*}
[ring R] [topological_space R]
[topological_space M] [add_comm_group M]
[module R M] [topological_module R M]
/-- Scalar multiplication by a unit is a homeomorphism from a
topological module onto itself. -/
protected def homeomorph.smul_of_unit (a : units R) : M ≃ₜ M :=
{ to_fun := λ x, (a : R) • x,
inv_fun := λ x, ((a⁻¹ : units R) : R) • x,
right_inv := λ x, calc (a : R) • ((a⁻¹ : units R) : R) • x = x :
by rw [smul_smul, units.mul_inv, one_smul],
left_inv := λ x, calc ((a⁻¹ : units R) : R) • (a : R) • x = x :
by rw [smul_smul, units.inv_mul, one_smul],
continuous_to_fun := continuous_const.smul continuous_id,
continuous_inv_fun := continuous_const.smul continuous_id }
lemma is_open_map_smul_of_unit (a : units R) : is_open_map (λ (x : M), (a : R) • x) :=
(homeomorph.smul_of_unit a).is_open_map
lemma is_closed_map_smul_of_unit (a : units R) : is_closed_map (λ (x : M), (a : R) • x) :=
(homeomorph.smul_of_unit a).is_closed_map
/-- If `M` is a topological module over `R` and `0` is a limit of invertible elements of `R`, then
`⊤` is the only submodule of `M` with a nonempty interior. See also
`submodule.eq_top_of_nonempty_interior` for a `normed_space` version. -/
lemma submodule.eq_top_of_nonempty_interior' [topological_add_monoid M]
(h : nhds_within (0:R) {x | is_unit x} ≠ ⊥)
(s : submodule R M) (hs : (interior (s:set M)).nonempty) :
s = ⊤ :=
begin
rcases hs with ⟨y, hy⟩,
refine (submodule.eq_top_iff'.2 $ λ x, _),
rw [mem_interior_iff_mem_nhds] at hy,
have : tendsto (λ c:R, y + c • x) (nhds_within 0 {x | is_unit x}) (𝓝 (y + (0:R) • x)),
from tendsto_const_nhds.add ((tendsto_nhds_within_of_tendsto_nhds tendsto_id).smul
tendsto_const_nhds),
rw [zero_smul, add_zero] at this,
rcases nonempty_of_mem_sets h (inter_mem_sets (mem_map.1 (this hy)) self_mem_nhds_within)
with ⟨_, hu, u, rfl⟩,
have hy' : y ∈ ↑s := mem_of_nhds hy,
exact (s.smul_mem_iff' _).1 ((s.add_mem_iff_right hy').1 hu)
end
end
section
variables {R : Type*} {M : Type*} {a : R}
[field R] [topological_space R]
[topological_space M] [add_comm_group M]
[vector_space R M] [topological_vector_space R M]
/-- Scalar multiplication by a non-zero field element is a
homeomorphism from a topological vector space onto itself. -/
protected def homeomorph.smul_of_ne_zero (ha : a ≠ 0) : M ≃ₜ M :=
{.. homeomorph.smul_of_unit (units.mk0 a ha)}
lemma is_open_map_smul_of_ne_zero (ha : a ≠ 0) : is_open_map (λ (x : M), a • x) :=
(homeomorph.smul_of_ne_zero ha).is_open_map
lemma is_closed_map_smul_of_ne_zero (ha : a ≠ 0) : is_closed_map (λ (x : M), a • x) :=
(homeomorph.smul_of_ne_zero ha).is_closed_map
end
/-- Continuous linear maps between modules. We only put the type classes that are necessary for the
definition, although in applications `M` and `M₂` will be topological modules over the topological
ring `R`. -/
structure continuous_linear_map
(R : Type*) [ring R]
(M : Type*) [topological_space M] [add_comm_group M]
(M₂ : Type*) [topological_space M₂] [add_comm_group M₂]
[module R M] [module R M₂]
extends linear_map R M M₂ :=
(cont : continuous to_fun)
notation M ` →L[`:25 R `] ` M₂ := continuous_linear_map R M M₂
/-- Continuous linear equivalences between modules. We only put the type classes that are necessary
for the definition, although in applications `M` and `M₂` will be topological modules over the
topological ring `R`. -/
@[nolint has_inhabited_instance]
structure continuous_linear_equiv
(R : Type*) [ring R]
(M : Type*) [topological_space M] [add_comm_group M]
(M₂ : Type*) [topological_space M₂] [add_comm_group M₂]
[module R M] [module R M₂]
extends linear_equiv R M M₂ :=
(continuous_to_fun : continuous to_fun)
(continuous_inv_fun : continuous inv_fun)
notation M ` ≃L[`:50 R `] ` M₂ := continuous_linear_equiv R M M₂
namespace continuous_linear_map
section general_ring
/- Properties that hold for non-necessarily commutative rings. -/
variables
{R : Type*} [ring R]
{M : Type*} [topological_space M] [add_comm_group M]
{M₂ : Type*} [topological_space M₂] [add_comm_group M₂]
{M₃ : Type*} [topological_space M₃] [add_comm_group M₃]
{M₄ : Type*} [topological_space M₄] [add_comm_group M₄]
[module R M] [module R M₂] [module R M₃] [module R M₄]
/-- Coerce continuous linear maps to linear maps. -/
instance : has_coe (M →L[R] M₂) (M →ₗ[R] M₂) := ⟨to_linear_map⟩
/-- Coerce continuous linear maps to functions. -/
-- see Note [function coercion]
instance to_fun : has_coe_to_fun $ M →L[R] M₂ := ⟨λ _, M → M₂, λ f, f⟩
protected lemma continuous (f : M →L[R] M₂) : continuous f := f.2
@[ext] theorem ext {f g : M →L[R] M₂} (h : ∀ x, f x = g x) : f = g :=
by cases f; cases g; congr' 1; ext x; apply h
theorem ext_iff {f g : M →L[R] M₂} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, by rw h, by ext⟩
variables (c : R) (f g : M →L[R] M₂) (h : M₂ →L[R] M₃) (x y z : M)
-- make some straightforward lemmas available to `simp`.
@[simp] lemma map_zero : f (0 : M) = 0 := (to_linear_map _).map_zero
@[simp] lemma map_add : f (x + y) = f x + f y := (to_linear_map _).map_add _ _
@[simp] lemma map_sub : f (x - y) = f x - f y := (to_linear_map _).map_sub _ _
@[simp] lemma map_smul : f (c • x) = c • f x := (to_linear_map _).map_smul _ _
@[simp] lemma map_neg : f (-x) = - (f x) := (to_linear_map _).map_neg _
@[simp, norm_cast] lemma coe_coe : ((f : M →ₗ[R] M₂) : (M → M₂)) = (f : M → M₂) := rfl
/-- The continuous map that is constantly zero. -/
instance: has_zero (M →L[R] M₂) := ⟨⟨0, continuous_const⟩⟩
instance : inhabited (M →L[R] M₂) := ⟨0⟩
@[simp] lemma zero_apply : (0 : M →L[R] M₂) x = 0 := rfl
@[simp, norm_cast] lemma coe_zero : ((0 : M →L[R] M₂) : M →ₗ[R] M₂) = 0 := rfl
/- no simp attribute on the next line as simp does not always simplify `0 x` to `0`
when `0` is the zero function, while it does for the zero continuous linear map,
and this is the most important property we care about. -/
@[norm_cast] lemma coe_zero' : ((0 : M →L[R] M₂) : M → M₂) = 0 := rfl
section
variables (R M)
/-- the identity map as a continuous linear map. -/
def id : M →L[R] M :=
⟨linear_map.id, continuous_id⟩
end
instance : has_one (M →L[R] M) := ⟨id R M⟩
lemma id_apply : id R M x = x := rfl
@[simp, norm_cast] lemma coe_id : (id R M : M →ₗ[R] M) = linear_map.id := rfl
@[simp, norm_cast] lemma coe_id' : (id R M : M → M) = _root_.id := rfl
@[simp] lemma one_apply : (1 : M →L[R] M) x = x := rfl
section add
variables [topological_add_group M₂]
instance : has_add (M →L[R] M₂) :=
⟨λ f g, ⟨f + g, f.2.add g.2⟩⟩
@[simp] lemma add_apply : (f + g) x = f x + g x := rfl
@[simp, norm_cast] lemma coe_add : (((f + g) : M →L[R] M₂) : M →ₗ[R] M₂) = (f : M →ₗ[R] M₂) + g := rfl
@[norm_cast] lemma coe_add' : (((f + g) : M →L[R] M₂) : M → M₂) = (f : M → M₂) + g := rfl
instance : has_neg (M →L[R] M₂) := ⟨λ f, ⟨-f, f.2.neg⟩⟩
@[simp] lemma neg_apply : (-f) x = - (f x) := rfl
@[simp, norm_cast] lemma coe_neg : (((-f) : M →L[R] M₂) : M →ₗ[R] M₂) = -(f : M →ₗ[R] M₂) := rfl
@[norm_cast] lemma coe_neg' : (((-f) : M →L[R] M₂) : M → M₂) = -(f : M → M₂) := rfl
instance : add_comm_group (M →L[R] M₂) :=
by { refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext;
apply_rules [zero_add, add_assoc, add_zero, add_left_neg, add_comm] }
lemma sub_apply (x : M) : (f - g) x = f x - g x := rfl
@[simp, norm_cast] lemma coe_sub : (((f - g) : M →L[R] M₂) : M →ₗ[R] M₂) = (f : M →ₗ[R] M₂) - g := rfl
@[simp, norm_cast] lemma coe_sub' : (((f - g) : M →L[R] M₂) : M → M₂) = (f : M → M₂) - g := rfl
lemma sum_apply {ι : Type*} (t : finset ι) (f : ι → M →L[R] M₂) (b : M) :
t.sum f b = t.sum (λd, f d b) :=
begin
haveI : is_add_group_hom (λ (g : M →L[R] M₂), g b) :=
{ map_add := λ f g, continuous_linear_map.add_apply f g b },
exact (finset.sum_hom t (λ g : M →L[R] M₂, g b)).symm
end
end add
@[simp] lemma sub_apply' (x : M) : ((f : M →ₗ[R] M₂) - g) x = f x - g x := rfl
/-- Composition of bounded linear maps. -/
def comp (g : M₂ →L[R] M₃) (f : M →L[R] M₂) : M →L[R] M₃ :=
⟨linear_map.comp g.to_linear_map f.to_linear_map, g.2.comp f.2⟩
@[simp, norm_cast] lemma coe_comp : ((h.comp f) : (M →ₗ[R] M₃)) = (h : M₂ →ₗ[R] M₃).comp f := rfl
@[simp, norm_cast] lemma coe_comp' : ((h.comp f) : (M → M₃)) = (h : M₂ → M₃) ∘ f := rfl
@[simp] theorem comp_id : f.comp (id R M) = f :=
ext $ λ x, rfl
@[simp] theorem id_comp : (id R M₂).comp f = f :=
ext $ λ x, rfl
@[simp] theorem comp_zero : f.comp (0 : M₃ →L[R] M) = 0 :=
by { ext, simp }
@[simp] theorem zero_comp : (0 : M₂ →L[R] M₃).comp f = 0 :=
by { ext, simp }
@[simp] lemma comp_add [topological_add_group M₂] [topological_add_group M₃]
(g : M₂ →L[R] M₃) (f₁ f₂ : M →L[R] M₂) :
g.comp (f₁ + f₂) = g.comp f₁ + g.comp f₂ :=
by { ext, simp }
@[simp] lemma add_comp [topological_add_group M₃]
(g₁ g₂ : M₂ →L[R] M₃) (f : M →L[R] M₂) :
(g₁ + g₂).comp f = g₁.comp f + g₂.comp f :=
by { ext, simp }
theorem comp_assoc (h : M₃ →L[R] M₄) (g : M₂ →L[R] M₃) (f : M →L[R] M₂) :
(h.comp g).comp f = h.comp (g.comp f) :=
rfl
instance : has_mul (M →L[R] M) := ⟨comp⟩
lemma mul_def (f g : M →L[R] M) : f * g = f.comp g := rfl
@[simp] lemma coe_mul (f g : M →L[R] M) : ⇑(f * g) = f ∘ g := rfl
lemma mul_apply (f g : M →L[R] M) (x : M) : (f * g) x = f (g x) := rfl
instance [topological_add_group M] : ring (M →L[R] M) :=
{ mul := (*),
one := 1,
mul_one := λ _, ext $ λ _, rfl,
one_mul := λ _, ext $ λ _, rfl,
mul_assoc := λ _ _ _, ext $ λ _, rfl,
left_distrib := λ _ _ _, ext $ λ _, map_add _ _ _,
right_distrib := λ _ _ _, ext $ λ _, linear_map.add_apply _ _ _,
..continuous_linear_map.add_comm_group }
/-- The cartesian product of two bounded linear maps, as a bounded linear map. -/
protected def prod (f₁ : M →L[R] M₂) (f₂ : M →L[R] M₃) : M →L[R] (M₂ × M₃) :=
{ cont := f₁.2.prod_mk f₂.2,
..f₁.to_linear_map.prod f₂.to_linear_map }
@[simp, norm_cast] lemma coe_prod (f₁ : M →L[R] M₂) (f₂ : M →L[R] M₃) :
(f₁.prod f₂ : M →ₗ[R] M₂ × M₃) = linear_map.prod f₁ f₂ :=
rfl
@[simp, norm_cast] lemma prod_apply (f₁ : M →L[R] M₂) (f₂ : M →L[R] M₃) (x : M) :
f₁.prod f₂ x = (f₁ x, f₂ x) :=
rfl
/-- Kernel of a continuous linear map. -/
def ker (f : M →L[R] M₂) : submodule R M := (f : M →ₗ[R] M₂).ker
@[norm_cast] lemma ker_coe : (f : M →ₗ[R] M₂).ker = f.ker := rfl
@[simp] lemma mem_ker {f : M →L[R] M₂} {x} : x ∈ f.ker ↔ f x = 0 := linear_map.mem_ker
lemma is_closed_ker [t1_space M₂] : is_closed (f.ker : set M) :=
continuous_iff_is_closed.1 f.cont _ is_closed_singleton
@[simp] lemma apply_ker (x : f.ker) : f x = 0 := mem_ker.1 x.2
/-- Range of a continuous linear map. -/
def range (f : M →L[R] M₂) : submodule R M₂ := (f : M →ₗ[R] M₂).range
lemma range_coe : (f.range : set M₂) = set.range f := linear_map.range_coe _
lemma mem_range {f : M →L[R] M₂} {y} : y ∈ f.range ↔ ∃ x, f x = y := linear_map.mem_range
/-- Restrict codomain of a continuous linear map. -/
def cod_restrict (f : M →L[R] M₂) (p : submodule R M₂) (h : ∀ x, f x ∈ p) :
M →L[R] p :=
{ cont := continuous_subtype_mk h f.continuous,
to_linear_map := (f : M →ₗ[R] M₂).cod_restrict p h}
@[norm_cast] lemma coe_cod_restrict (f : M →L[R] M₂) (p : submodule R M₂) (h : ∀ x, f x ∈ p) :
(f.cod_restrict p h : M →ₗ[R] p) = (f : M →ₗ[R] M₂).cod_restrict p h :=
rfl
@[simp] lemma coe_cod_restrict_apply (f : M →L[R] M₂) (p : submodule R M₂) (h : ∀ x, f x ∈ p) (x) :
(f.cod_restrict p h x : M₂) = f x :=
rfl
/-- Embedding of a submodule into the ambient space as a continuous linear map. -/
def subtype_val (p : submodule R M) : p →L[R] M :=
{ cont := continuous_subtype_val,
to_linear_map := p.subtype }
@[simp, norm_cast] lemma coe_subtype_val (p : submodule R M) :
(subtype_val p : p →ₗ[R] M) = p.subtype :=
rfl
@[simp, norm_cast] lemma subtype_val_apply (p : submodule R M) (x : p) :
(subtype_val p : p → M) x = x :=
rfl
variables (R M M₂)
/-- `prod.fst` as a `continuous_linear_map`. -/
def fst : M × M₂ →L[R] M :=
{ cont := continuous_fst, to_linear_map := linear_map.fst R M M₂ }
/-- `prod.snd` as a `continuous_linear_map`. -/
def snd : M × M₂ →L[R] M₂ :=
{ cont := continuous_snd, to_linear_map := linear_map.snd R M M₂ }
variables {R M M₂}
@[simp, norm_cast] lemma coe_fst : (fst R M M₂ : M × M₂ →ₗ[R] M) = linear_map.fst R M M₂ := rfl
@[simp, norm_cast] lemma coe_fst' : (fst R M M₂ : M × M₂ → M) = prod.fst := rfl
@[simp, norm_cast] lemma coe_snd : (snd R M M₂ : M × M₂ →ₗ[R] M₂) = linear_map.snd R M M₂ := rfl
@[simp, norm_cast] lemma coe_snd' : (snd R M M₂ : M × M₂ → M₂) = prod.snd := rfl
/-- `prod.map` of two continuous linear maps. -/
def prod_map (f₁ : M →L[R] M₂) (f₂ : M₃ →L[R] M₄) : (M × M₃) →L[R] (M₂ × M₄) :=
(f₁.comp (fst R M M₃)).prod (f₂.comp (snd R M M₃))
@[simp, norm_cast] lemma coe_prod_map (f₁ : M →L[R] M₂) (f₂ : M₃ →L[R] M₄) :
(f₁.prod_map f₂ : (M × M₃) →ₗ[R] (M₂ × M₄)) = ((f₁ : M →ₗ[R] M₂).prod_map (f₂ : M₃ →ₗ[R] M₄)) :=
rfl
@[simp, norm_cast] lemma coe_prod_map' (f₁ : M →L[R] M₂) (f₂ : M₃ →L[R] M₄) :
⇑(f₁.prod_map f₂) = prod.map f₁ f₂ :=
rfl
/-- The continuous linear map given by `(x, y) ↦ f₁ x + f₂ y`. -/
def coprod [topological_add_monoid M₃] (f₁ : M →L[R] M₃) (f₂ : M₂ →L[R] M₃) :
(M × M₂) →L[R] M₃ :=
⟨linear_map.coprod f₁ f₂, (f₁.cont.comp continuous_fst).add (f₂.cont.comp continuous_snd)⟩
@[norm_cast, simp] lemma coe_coprod [topological_add_monoid M₃]
(f₁ : M →L[R] M₃) (f₂ : M₂ →L[R] M₃) :
(f₁.coprod f₂ : (M × M₂) →ₗ[R] M₃) = linear_map.coprod f₁ f₂ :=
rfl
@[simp] lemma coprod_apply [topological_add_monoid M₃] (f₁ : M →L[R] M₃) (f₂ : M₂ →L[R] M₃) (x) :
f₁.coprod f₂ x = f₁ x.1 + f₂ x.2 := rfl
/-- Given a right inverse `f₂ : M₂ →L[R] M` to `f₁ : M →L[R] M₂`,
`proj_ker_of_right_inverse f₁ f₂ h` is the projection `M →L[R] f₁.ker` along `f₂.range`. -/
def proj_ker_of_right_inverse [topological_add_group M] (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M)
(h : function.right_inverse f₂ f₁) :
M →L[R] f₁.ker :=
(id R M - f₂.comp f₁).cod_restrict f₁.ker $ λ x, by simp [h (f₁ x)]
@[simp] lemma coe_proj_ker_of_right_inverse_apply [topological_add_group M]
(f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) (x : M) :
(f₁.proj_ker_of_right_inverse f₂ h x : M) = x - f₂ (f₁ x) :=
rfl
@[simp] lemma proj_ker_of_right_inverse_apply_idem [topological_add_group M]
(f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) (x : f₁.ker) :
f₁.proj_ker_of_right_inverse f₂ h x = x :=
subtype.coe_ext.2 $ by simp
@[simp] lemma proj_ker_of_right_inverse_comp_inv [topological_add_group M]
(f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) (y : M₂) :
f₁.proj_ker_of_right_inverse f₂ h (f₂ y) = 0 :=
subtype.coe_ext.2 $ by simp [h y]
variables [topological_space R] [topological_module R M₂]
/-- The linear map `λ x, c x • f`. Associates to a scalar-valued linear map and an element of
`M₂` the `M₂`-valued linear map obtained by multiplying the two (a.k.a. tensoring by `M₂`) -/
def smul_right (c : M →L[R] R) (f : M₂) : M →L[R] M₂ :=
{ cont := c.2.smul continuous_const,
..c.to_linear_map.smul_right f }
@[simp]
lemma smul_right_apply {c : M →L[R] R} {f : M₂} {x : M} :
(smul_right c f : M → M₂) x = (c : M → R) x • f :=
rfl
@[simp]
lemma smul_right_one_one (c : R →L[R] M₂) : smul_right 1 ((c : R → M₂) 1) = c :=
by ext; simp [-continuous_linear_map.map_smul, (continuous_linear_map.map_smul _ _ _).symm]
@[simp]
lemma smul_right_one_eq_iff {f f' : M₂} :
smul_right (1 : R →L[R] R) f = smul_right 1 f' ↔ f = f' :=
⟨λ h, have (smul_right (1 : R →L[R] R) f : R → M₂) 1 = (smul_right (1 : R →L[R] R) f' : R → M₂) 1,
by rw h,
by simp at this; assumption,
by cc⟩
lemma smul_right_comp [topological_module R R] {x : M₂} {c : R} :
(smul_right 1 x : R →L[R] M₂).comp (smul_right 1 c : R →L[R] R) = smul_right 1 (c • x) :=
by { ext, simp [mul_smul] }
lemma smul_right_one_pow [topological_add_group R] [topological_module R R] (c : R) (n : ℕ) :
(smul_right 1 c : R →L[R] R)^n = smul_right 1 (c^n) :=
begin
induction n with n ihn,
{ ext, simp },
{ rw [pow_succ, ihn, mul_def, smul_right_comp, smul_eq_mul, pow_succ'] }
end
end general_ring
section comm_ring
variables
{R : Type*} [comm_ring R] [topological_space R]
{M : Type*} [topological_space M] [add_comm_group M]
{M₂ : Type*} [topological_space M₂] [add_comm_group M₂]
{M₃ : Type*} [topological_space M₃] [add_comm_group M₃]
[module R M] [module R M₂] [module R M₃] [topological_module R M₃]
instance : has_scalar R (M →L[R] M₃) :=
⟨λ c f, ⟨c • f, continuous_const.smul f.2⟩⟩
variables (c : R) (h : M₂ →L[R] M₃) (f g : M →L[R] M₂) (x y z : M)
@[simp] lemma smul_comp : (c • h).comp f = c • (h.comp f) := rfl
variable [topological_module R M₂]
@[simp] lemma smul_apply : (c • f) x = c • (f x) := rfl
@[simp, norm_cast] lemma coe_apply : (((c • f) : M →L[R] M₂) : M →ₗ[R] M₂) = c • (f : M →ₗ[R] M₂) := rfl
@[norm_cast] lemma coe_apply' : (((c • f) : M →L[R] M₂) : M → M₂) = c • (f : M → M₂) := rfl
@[simp] lemma comp_smul : h.comp (c • f) = c • (h.comp f) := by { ext, simp }
variable [topological_add_group M₂]
instance : module R (M →L[R] M₂) :=
{ smul_zero := λ _, ext $ λ _, smul_zero _,
zero_smul := λ _, ext $ λ _, zero_smul _ _,
one_smul := λ _, ext $ λ _, one_smul _ _,
mul_smul := λ _ _ _, ext $ λ _, mul_smul _ _ _,
add_smul := λ _ _ _, ext $ λ _, add_smul _ _ _,
smul_add := λ _ _ _, ext $ λ _, smul_add _ _ _ }
instance : algebra R (M₂ →L[R] M₂) :=
algebra.of_semimodule' (λ c f, ext $ λ x, rfl) (λ c f, ext $ λ x, f.map_smul c x)
end comm_ring
end continuous_linear_map
namespace continuous_linear_equiv
variables {R : Type*} [ring R]
{M : Type*} [topological_space M] [add_comm_group M]
{M₂ : Type*} [topological_space M₂] [add_comm_group M₂]
{M₃ : Type*} [topological_space M₃] [add_comm_group M₃]
{M₄ : Type*} [topological_space M₄] [add_comm_group M₄]
[module R M] [module R M₂] [module R M₃] [module R M₄]
/-- A continuous linear equivalence induces a continuous linear map. -/
def to_continuous_linear_map (e : M ≃L[R] M₂) : M →L[R] M₂ :=
{ cont := e.continuous_to_fun,
..e.to_linear_equiv.to_linear_map }
/-- Coerce continuous linear equivs to continuous linear maps. -/
instance : has_coe (M ≃L[R] M₂) (M →L[R] M₂) := ⟨to_continuous_linear_map⟩
/-- Coerce continuous linear equivs to maps. -/
-- see Note [function coercion]
instance : has_coe_to_fun (M ≃L[R] M₂) := ⟨λ _, M → M₂, λ f, f⟩
@[simp] theorem coe_def_rev (e : M ≃L[R] M₂) : e.to_continuous_linear_map = e := rfl
@[simp] theorem coe_apply (e : M ≃L[R] M₂) (b : M) : (e : M →L[R] M₂) b = e b := rfl
@[norm_cast] lemma coe_coe (e : M ≃L[R] M₂) : ((e : M →L[R] M₂) : M → M₂) = e := rfl
@[ext] lemma ext {f g : M ≃L[R] M₂} (h : (f : M → M₂) = g) : f = g :=
begin
cases f; cases g,
simp only [],
ext x,
induction h,
refl
end
/-- A continuous linear equivalence induces a homeomorphism. -/
def to_homeomorph (e : M ≃L[R] M₂) : M ≃ₜ M₂ := { ..e }
-- Make some straightforward lemmas available to `simp`.
@[simp] lemma map_zero (e : M ≃L[R] M₂) : e (0 : M) = 0 := (e : M →L[R] M₂).map_zero
@[simp] lemma map_add (e : M ≃L[R] M₂) (x y : M) : e (x + y) = e x + e y :=
(e : M →L[R] M₂).map_add x y
@[simp] lemma map_sub (e : M ≃L[R] M₂) (x y : M) : e (x - y) = e x - e y :=
(e : M →L[R] M₂).map_sub x y
@[simp] lemma map_smul (e : M ≃L[R] M₂) (c : R) (x : M) : e (c • x) = c • (e x) :=
(e : M →L[R] M₂).map_smul c x
@[simp] lemma map_neg (e : M ≃L[R] M₂) (x : M) : e (-x) = -e x := (e : M →L[R] M₂).map_neg x
@[simp] lemma map_eq_zero_iff (e : M ≃L[R] M₂) {x : M} : e x = 0 ↔ x = 0 :=
e.to_linear_equiv.map_eq_zero_iff
protected lemma continuous (e : M ≃L[R] M₂) : continuous (e : M → M₂) :=
e.continuous_to_fun
protected lemma continuous_on (e : M ≃L[R] M₂) {s : set M} : continuous_on (e : M → M₂) s :=
e.continuous.continuous_on
protected lemma continuous_at (e : M ≃L[R] M₂) {x : M} : continuous_at (e : M → M₂) x :=
e.continuous.continuous_at
protected lemma continuous_within_at (e : M ≃L[R] M₂) {s : set M} {x : M} :
continuous_within_at (e : M → M₂) s x :=
e.continuous.continuous_within_at
lemma comp_continuous_on_iff
{α : Type*} [topological_space α] (e : M ≃L[R] M₂) (f : α → M) (s : set α) :
continuous_on (e ∘ f) s ↔ continuous_on f s :=
e.to_homeomorph.comp_continuous_on_iff _ _
lemma comp_continuous_iff
{α : Type*} [topological_space α] (e : M ≃L[R] M₂) (f : α → M) :
continuous (e ∘ f) ↔ continuous f :=
e.to_homeomorph.comp_continuous_iff _
/-- An extensionality lemma for `R ≃L[R] M`. -/
lemma ext₁ [topological_space R] {f g : R ≃L[R] M} (h : f 1 = g 1) : f = g :=
ext $ funext $ λ x, mul_one x ▸ by rw [← smul_eq_mul, map_smul, h, map_smul]
section
variables (R M)
/-- The identity map as a continuous linear equivalence. -/
@[refl] protected def refl : M ≃L[R] M :=
{ continuous_to_fun := continuous_id,
continuous_inv_fun := continuous_id,
.. linear_equiv.refl R M }
end
@[simp, norm_cast] lemma coe_refl :
(continuous_linear_equiv.refl R M : M →L[R] M) = continuous_linear_map.id R M := rfl
@[simp, norm_cast] lemma coe_refl' :
(continuous_linear_equiv.refl R M : M → M) = id := rfl
/-- The inverse of a continuous linear equivalence as a continuous linear equivalence-/
@[symm] protected def symm (e : M ≃L[R] M₂) : M₂ ≃L[R] M :=
{ continuous_to_fun := e.continuous_inv_fun,
continuous_inv_fun := e.continuous_to_fun,
.. e.to_linear_equiv.symm }
@[simp] lemma symm_to_linear_equiv (e : M ≃L[R] M₂) :
e.symm.to_linear_equiv = e.to_linear_equiv.symm :=
by { ext, refl }
/-- The composition of two continuous linear equivalences as a continuous linear equivalence. -/
@[trans] protected def trans (e₁ : M ≃L[R] M₂) (e₂ : M₂ ≃L[R] M₃) : M ≃L[R] M₃ :=
{ continuous_to_fun := e₂.continuous_to_fun.comp e₁.continuous_to_fun,
continuous_inv_fun := e₁.continuous_inv_fun.comp e₂.continuous_inv_fun,
.. e₁.to_linear_equiv.trans e₂.to_linear_equiv }
@[simp] lemma trans_to_linear_equiv (e₁ : M ≃L[R] M₂) (e₂ : M₂ ≃L[R] M₃) :
(e₁.trans e₂).to_linear_equiv = e₁.to_linear_equiv.trans e₂.to_linear_equiv :=
by { ext, refl }
/-- Product of two continuous linear equivalences. The map comes from `equiv.prod_congr`. -/
def prod (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) : (M × M₃) ≃L[R] (M₂ × M₄) :=
{ continuous_to_fun := e.continuous_to_fun.prod_map e'.continuous_to_fun,
continuous_inv_fun := e.continuous_inv_fun.prod_map e'.continuous_inv_fun,
.. e.to_linear_equiv.prod e'.to_linear_equiv }
@[simp, norm_cast] lemma prod_apply (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (x) :
e.prod e' x = (e x.1, e' x.2) := rfl
@[simp, norm_cast] lemma coe_prod (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) :
(e.prod e' : (M × M₃) →L[R] (M₂ × M₄)) = (e : M →L[R] M₂).prod_map (e' : M₃ →L[R] M₄) :=
rfl
variables [topological_add_group M₄]
/-- Equivalence given by a block lower diagonal matrix. `e` and `e'` are diagonal square blocks,
and `f` is a rectangular block below the diagonal. -/
def skew_prod (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (f : M →L[R] M₄) :
(M × M₃) ≃L[R] M₂ × M₄ :=
{ continuous_to_fun := (e.continuous_to_fun.comp continuous_fst).prod_mk
((e'.continuous_to_fun.comp continuous_snd).add $ f.continuous.comp continuous_fst),
continuous_inv_fun := (e.continuous_inv_fun.comp continuous_fst).prod_mk
(e'.continuous_inv_fun.comp $ continuous_snd.sub $ f.continuous.comp $
e.continuous_inv_fun.comp continuous_fst),
.. e.to_linear_equiv.skew_prod e'.to_linear_equiv ↑f }
@[simp] lemma skew_prod_apply (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (f : M →L[R] M₄) (x) :
e.skew_prod e' f x = (e x.1, e' x.2 + f x.1) := rfl
@[simp] lemma skew_prod_symm_apply (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (f : M →L[R] M₄) (x) :
(e.skew_prod e' f).symm x = (e.symm x.1, e'.symm (x.2 - f (e.symm x.1))) := rfl
theorem bijective (e : M ≃L[R] M₂) : function.bijective e := e.to_linear_equiv.to_equiv.bijective
theorem injective (e : M ≃L[R] M₂) : function.injective e := e.to_linear_equiv.to_equiv.injective
theorem surjective (e : M ≃L[R] M₂) : function.surjective e := e.to_linear_equiv.to_equiv.surjective
@[simp] theorem apply_symm_apply (e : M ≃L[R] M₂) (c : M₂) : e (e.symm c) = c := e.1.6 c
@[simp] theorem symm_apply_apply (e : M ≃L[R] M₂) (b : M) : e.symm (e b) = b := e.1.5 b
@[simp] theorem coe_comp_coe_symm (e : M ≃L[R] M₂) :
(e : M →L[R] M₂).comp (e.symm : M₂ →L[R] M) = continuous_linear_map.id R M₂ :=
continuous_linear_map.ext e.apply_symm_apply
@[simp] theorem coe_symm_comp_coe (e : M ≃L[R] M₂) :
(e.symm : M₂ →L[R] M).comp (e : M →L[R] M₂) = continuous_linear_map.id R M :=
continuous_linear_map.ext e.symm_apply_apply
lemma symm_comp_self (e : M ≃L[R] M₂) :
(e.symm : M₂ → M) ∘ (e : M → M₂) = id :=
by{ ext x, exact symm_apply_apply e x }
lemma self_comp_symm (e : M ≃L[R] M₂) :
(e : M → M₂) ∘ (e.symm : M₂ → M) = id :=
by{ ext x, exact apply_symm_apply e x }
@[simp] lemma symm_comp_self' (e : M ≃L[R] M₂) :
((e.symm : M₂ →L[R] M) : M₂ → M) ∘ ((e : M →L[R] M₂) : M → M₂) = id :=
symm_comp_self e
@[simp] lemma self_comp_symm' (e : M ≃L[R] M₂) :
((e : M →L[R] M₂) : M → M₂) ∘ ((e.symm : M₂ →L[R] M) : M₂ → M) = id :=
self_comp_symm e
@[simp] theorem symm_symm (e : M ≃L[R] M₂) : e.symm.symm = e :=
by { ext x, refl }
theorem symm_symm_apply (e : M ≃L[R] M₂) (x : M) : e.symm.symm x = e x :=
rfl
/-- Create a `continuous_linear_equiv` from two `continuous_linear_map`s that are
inverse of each other. -/
def equiv_of_inverse (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h₁ : function.left_inverse f₂ f₁)
(h₂ : function.right_inverse f₂ f₁) :
M ≃L[R] M₂ :=
{ to_fun := f₁,
continuous_to_fun := f₁.continuous,
inv_fun := f₂,
continuous_inv_fun := f₂.continuous,
left_inv := h₁,
right_inv := h₂,
.. f₁ }
@[simp] lemma equiv_of_inverse_apply (f₁ : M →L[R] M₂) (f₂ h₁ h₂ x) :
equiv_of_inverse f₁ f₂ h₁ h₂ x = f₁ x :=
rfl
@[simp] lemma symm_equiv_of_inverse (f₁ : M →L[R] M₂) (f₂ h₁ h₂) :
(equiv_of_inverse f₁ f₂ h₁ h₂).symm = equiv_of_inverse f₂ f₁ h₂ h₁ :=
rfl
section
variables (R) [topological_space R] [topological_module R R]
/-- Continuous linear equivalences `R ≃L[R] R` are enumerated by `units R`. -/
def units_equiv_aut : units R ≃ (R ≃L[R] R) :=
{ to_fun := λ u, equiv_of_inverse
(continuous_linear_map.smul_right 1 ↑u)
(continuous_linear_map.smul_right 1 ↑u⁻¹)
(λ x, by simp) (λ x, by simp),
inv_fun := λ e, ⟨e 1, e.symm 1,
by rw [← smul_eq_mul, ← map_smul, smul_eq_mul, mul_one, symm_apply_apply],
by rw [← smul_eq_mul, ← map_smul, smul_eq_mul, mul_one, apply_symm_apply]⟩,
left_inv := λ u, units.ext $ by simp,
right_inv := λ e, ext₁ $ by simp }
variable {R}
@[simp] lemma units_equiv_aut_apply (u : units R) (x : R) : units_equiv_aut R u x = x * u := rfl
@[simp] lemma units_equiv_aut_apply_symm (u : units R) (x : R) :
(units_equiv_aut R u).symm x = x * ↑u⁻¹ := rfl
@[simp] lemma units_equiv_aut_symm_apply (e : R ≃L[R] R) :
↑((units_equiv_aut R).symm e) = e 1 :=
rfl
end
variables [topological_add_group M]
open continuous_linear_map (id fst snd subtype_val mem_ker)
/-- A pair of continuous linear maps such that `f₁ ∘ f₂ = id` generates a continuous
linear equivalence `e` between `M` and `M₂ × f₁.ker` such that `(e x).2 = x` for `x ∈ f₁.ker`,
`(e x).1 = f₁ x`, and `(e (f₂ y)).2 = 0`. The map is given by `e x = (f₁ x, x - f₂ (f₁ x))`. -/
def equiv_of_right_inverse (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) :
M ≃L[R] M₂ × f₁.ker :=
equiv_of_inverse (f₁.prod (f₁.proj_ker_of_right_inverse f₂ h)) (f₂.coprod (subtype_val f₁.ker))
(λ x, by simp)
(λ ⟨x, y⟩, by simp [h x])
@[simp] lemma fst_equiv_of_right_inverse (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M)
(h : function.right_inverse f₂ f₁) (x : M) :
(equiv_of_right_inverse f₁ f₂ h x).1 = f₁ x := rfl
@[simp] lemma snd_equiv_of_right_inverse (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M)
(h : function.right_inverse f₂ f₁) (x : M) :
((equiv_of_right_inverse f₁ f₂ h x).2 : M) = x - f₂ (f₁ x) := rfl
@[simp] lemma equiv_of_right_inverse_symm_apply (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M)
(h : function.right_inverse f₂ f₁) (y : M₂ × f₁.ker) :
(equiv_of_right_inverse f₁ f₂ h).symm y = f₂ y.1 + y.2 := rfl
end continuous_linear_equiv
|
2e8613cec2577be47aa97f5a43057e2eb9995826 | 82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7 | /stage0/src/Lean/Meta/Match/CaseArraySizes.lean | 1307da144bc3d30fdc0707befe685417686640e0 | [
"Apache-2.0"
] | permissive | banksonian/lean4 | 3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc | 78da6b3aa2840693eea354a41e89fc5b212a5011 | refs/heads/master | 1,673,703,624,165 | 1,605,123,551,000 | 1,605,123,551,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,754 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Tactic.Assert
import Lean.Meta.Match.CaseValues
namespace Lean.Meta
structure CaseArraySizesSubgoal :=
(mvarId : MVarId)
(elems : Array FVarId := #[])
(diseqs : Array FVarId := #[])
(subst : FVarSubst := {})
instance : Inhabited CaseArraySizesSubgoal :=
⟨{ mvarId := arbitrary _ }⟩
def getArrayArgType (a : Expr) : MetaM Expr := do
let aType ← inferType a
let aType ← whnfD aType
unless aType.isAppOfArity `Array 1 do
throwError! "array expected{indentExpr a}"
pure aType.appArg!
private def mkArrayGetLit (a : Expr) (i : Nat) (n : Nat) (h : Expr) : MetaM Expr := do
let lt ← mkLt (mkNatLit i) (mkNatLit n)
let ltPrf ← mkDecideProof lt
mkAppM `Array.getLit #[a, mkNatLit i, h, ltPrf]
private partial def introArrayLit (mvarId : MVarId) (a : Expr) (n : Nat) (xNamePrefix : Name) (aSizeEqN : Expr) : MetaM MVarId := do
let α ← getArrayArgType a
let rec loop (i : Nat) (xs : Array Expr) (args : Array Expr) := do
if i < n then
withLocalDeclD (xNamePrefix.appendIndexAfter (i+1)) α fun xi => do
let xs := xs.push xi
let ai ← mkArrayGetLit a i n aSizeEqN
let args := args.push ai
loop (i+1) xs args
else
let xsLit ← mkArrayLit α xs.toList
let aEqXsLit ← mkEq a xsLit
let aEqLitPrf ← mkAppM `Array.toArrayLitEq #[a, mkNatLit n, aSizeEqN]
withLocalDeclD `hEqALit aEqXsLit fun heq => do
let target ← getMVarType mvarId
let newTarget ← mkForallFVars (xs.push heq) target
pure (newTarget, args.push aEqLitPrf)
let (newTarget, args) ← loop 0 #[] #[]
let tag ← getMVarTag mvarId
let newMVar ← mkFreshExprSyntheticOpaqueMVar newTarget tag
assignExprMVar mvarId (mkAppN newMVar args)
pure newMVar.mvarId!
/--
Split goal `... |- C a` into sizes.size + 1 subgoals
1) `..., x_1 ... x_{sizes[0]} |- C #[x_1, ... x_{sizes[0]}]`
...
n) `..., x_1 ... x_{sizes[n-1]} |- C #[x_1, ..., x_{sizes[n-1]}]`
n+1) `..., (h_1 : a.size != sizes[0]), ..., (h_n : a.size != sizes[n-1]) |- C a`
where `n = sizes.size` -/
def caseArraySizes (mvarId : MVarId) (fvarId : FVarId) (sizes : Array Nat) (xNamePrefix := `x) (hNamePrefix := `h) : MetaM (Array CaseArraySizesSubgoal) := do
let a := mkFVar fvarId
let α ← getArrayArgType a
let aSize ← mkAppM `Array.size #[a]
let mvarId ← assertExt mvarId `aSize (mkConst `Nat) aSize
let (aSizeFVarId, mvarId) ← intro1 mvarId
let (hEq, mvarId) ← intro1 mvarId
let subgoals ← caseValues mvarId aSizeFVarId (sizes.map mkNatLit) hNamePrefix
subgoals.mapIdxM fun i subgoal => do
let subst := subgoal.subst
let mvarId := subgoal.mvarId
let hEqSz := (subst.get hEq).fvarId!
if h : i.val < sizes.size then
let n := sizes.get ⟨i, h⟩
let mvarId ← clear mvarId subgoal.newHs[0]
let mvarId ← clear mvarId (subst.get aSizeFVarId).fvarId!
withMVarContext mvarId do
let hEqSzSymm ← mkEqSymm (mkFVar hEqSz)
let mvarId ← introArrayLit mvarId a n xNamePrefix hEqSzSymm
let (xs, mvarId) ← introN mvarId n
let (hEqLit, mvarId) ← intro1 mvarId
let mvarId ← clear mvarId hEqSz
let (subst, mvarId) ← substCore mvarId hEqLit false subst
pure { mvarId := mvarId, elems := xs, subst := subst }
else
let (subst, mvarId) ← substCore mvarId hEq false subst
let diseqs := subgoal.newHs.map fun fvarId => (subst.get fvarId).fvarId!
pure { mvarId := mvarId, diseqs := diseqs, subst := subst }
end Lean.Meta
|
392594af15812a6a30d7ef8b8d3d42d2f81a886c | 93b17e1ec33b7fd9fb0d8f958cdc9f2214b131a2 | /src/sep/rel_extra.lean | 01753929536c13285bbd3eedeb92a330bf708551 | [] | no_license | intoverflow/timesink | 93f8535cd504bc128ba1b57ce1eda4efc74e5136 | c25be4a2edb866ad0a9a87ee79e209afad6ab303 | refs/heads/master | 1,620,033,920,087 | 1,524,995,105,000 | 1,524,995,105,000 | 120,576,102 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,411 | lean | /- Extra stuff about relations
-
-/
import .rel
namespace Sep
universes ℓ₁ ℓ₂
-- More ways of writing up/down closed
def Rel.DownClosed' {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} (r : Rel A₁ A₂) : Prop
:= ∀ m₃ n₁ n₂
, (∃ m₁ m₂, r n₁ m₁ ∧ r n₂ m₂ ∧ A₂.join m₁ m₂ m₃)
→ (∃ n₃, r n₃ m₃ ∧ A₁.join n₁ n₂ n₃)
def Rel.UpClosed' {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} (r : Rel A₁ A₂) : Prop
:= ∀ n₃ m₁ m₂
, (∃ m₃, r m₃ n₃ ∧ A₁.join m₁ m₂ m₃)
→ (∃ n₁ n₂, r m₁ n₁ ∧ r m₂ n₂ ∧ A₂.join n₁ n₂ n₃)
def Rel.DownClosed_iff' {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} {r : Rel A₁ A₂}
: r.DownClosed ↔ r.DownClosed'
:= begin
apply iff.intro,
{ intro rDC,
intros m₃ n₁ n₂ H,
cases H with m₁ H,
cases H with m₂ H,
cases H with Rn₁m₁ H,
cases H with Rn₂m₂ Jm,
exact rDC Rn₁m₁ Rn₂m₂ Jm
},
{ intro rDC,
intros n₁ n₂ m₁ m₂ m₃ Rn₁m₁ Rn₂m₂ Jm,
apply rDC,
existsi m₁, existsi m₂,
apply and.intro Rn₁m₁,
exact and.intro Rn₂m₂ Jm
}
end
def Rel.UpClosed_iff'' {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} {r : Rel A₁ A₂}
: r.UpClosed ↔ r.UpClosed'
:= begin
apply iff.intro,
{ intro rUC,
intros n₃ m₁ m₂ H,
cases H with m₃ H,
cases H with Rm₃n₃ Jm,
have Q := rUC Jm Rm₃n₃,
cases Q with n₁ Q,
cases Q with n₂ Q,
cases Q with Jn Q,
cases Q with Rm₁n₁ Rm₂n₂,
existsi n₁, existsi n₂,
apply and.intro Rm₁n₁,
exact and.intro Rm₂n₂ Jn
},
{ intro rUC,
intros m₁ m₂ m₃ n₃ Jm Rm₃n₃,
have Q := rUC n₃ m₁ m₂ begin existsi m₃, exact and.intro Rm₃n₃ Jm end,
cases Q with n₁ Q,
cases Q with n₂ Q,
cases Q with Rm₁n₁ Q,
cases Q with Rm₂n₂ Jn,
existsi n₁, existsi n₂,
apply and.intro Jn,
exact and.intro Rm₁n₁ Rm₂n₂
}
end
structure Rel.Square {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} (r : Rel A₁ A₂)
:= (x₁ x₂ x₃ : A₁.τ)
(y₁ y₂ y₃ : A₂.τ)
(R₁ : r x₁ y₁)
(R₂ : r x₂ y₂)
(R₃ : r x₃ y₃)
(Jx : A₁.join x₁ x₂ x₃)
(Jy : A₂.join y₁ y₂ y₃)
structure Rel.DownSquare {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} (r : Rel A₁ A₂)
:= (x₁ x₂ : A₁.τ)
(y₁ y₂ y₃ : A₂.τ)
(R₁ : r x₁ y₁)
(R₂ : r x₂ y₂)
(Jy : A₂.join y₁ y₂ y₃)
structure Rel.UpSquare {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} (r : Rel A₁ A₂)
:= (x₁ x₂ x₃ : A₁.τ)
(y₃ : A₂.τ)
(R₃ : r x₃ y₃)
(Jx : A₁.join x₁ x₂ x₃)
def Rel.DownClosedPreImage {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} (r : Rel A₁ A₂)
: Set A₂
:= λ y, ∃ (s : r.Square), s.y₁ = y
def Rel.UpDom {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} (r : Rel A₁ A₂)
: Set A₁
:= λ x, ∃ (s : r.Square), s.x₃ = x
def UpDom_UpJoin {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} {r : Rel A₁ A₂}
: r.Fn r.UpDom ⊆ r.UpJoin
:= begin
intros y H,
cases H with x H,
cases H with H Rxy,
cases H with s E,
cases s, subst E,
simp at Rxy,
existsi x₁, existsi x₂, existsi x₃,
exact and.intro Rxy Jx
end
def UpClosed.UpJoin {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} {r : Rel A₁ A₂}
(rUC : r.UpClosed)
: r.UpJoin = r.Fn r.UpDom
:= begin
apply funext, intro y,
apply iff.to_eq, apply iff.intro,
{ intro H,
cases H with x₁ H,
cases H with x₂ H,
cases H with x₃ H,
cases H with R₃ Jx,
have Q := rUC Jx R₃,
cases Q with y₁ Q,
cases Q with y₂ Q,
cases Q with Jy Q,
cases Q with R₁ R₂,
existsi x₃,
refine and.intro _ R₃,
let ret : r.Square
:= { x₁ := x₁, x₂ := x₂, x₃ := x₃
, y₁ := y₁, y₂ := y₂, y₃ := y
, R₁ := R₁, R₂ := R₂, R₃ := R₃
, Jx := Jx, Jy := Jy
},
existsi ret,
trivial
},
{ apply UpDom_UpJoin }
end
def DownIm_DownPrime {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} {r : Rel A₁ A₂}
: r.FnInv r.DownClosedPreImage ⊆ r.DownPrime
:= begin
intros x H,
cases H with y H,
cases H with H Rxy,
cases H with s E,
cases s, subst E,
simp at Rxy,
existsi x₂, existsi y₁, existsi y₂, existsi y₃,
apply and.intro Rxy,
exact and.intro R₂ Jy
end
def DownClosed.DownPrime {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} {r : Rel A₁ A₂}
(rDC : r.DownClosed)
: r.DownPrime = r.FnInv r.DownClosedPreImage
:= begin
apply funext, intro x,
apply iff.to_eq, apply iff.intro,
{ intro H,
cases H with x₂ H,
cases H with y₁ H,
cases H with y₂ H,
cases H with y₃ H,
cases H with Rxy₁ H,
cases H with Rx₂y₂ Jy,
have Q := rDC Rxy₁ Rx₂y₂ Jy,
cases Q with x₃ Q,
cases Q with Rx₃y₃ Jx,
existsi y₁,
refine and.intro _ Rxy₁,
let ret : r.Square
:= { x₁ := x, x₂ := x₂, x₃ := x₃
, y₁ := y₁, y₂ := y₂, y₃ := y₃
, R₁ := Rxy₁, R₂ := Rx₂y₂, R₃ := Rx₃y₃
, Jx := Jx, Jy := Jy
},
existsi ret,
trivial
},
{ apply DownIm_DownPrime }
end
noncomputable def DownPrime.DownSquare {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} (r : Rel A₁ A₂)
: {x // r.DownPrime x} → r.DownSquare
:= begin
intro x,
cases x with x H₁,
cases classical.indefinite_description _ H₁ with x₂ H₂,
cases classical.indefinite_description _ H₂ with y₁ H₃,
cases classical.indefinite_description _ H₃ with y₂ H₄,
cases classical.indefinite_description _ H₄ with y₃ H,
clear H₁ H₂ H₃ H₄,
cases H with R₁ H,
cases H with R₂ Jy,
exact { x₁ := x, x₂ := x₂
, y₁ := y₁, y₂ := y₂, y₃ := y₃
, R₁ := R₁, R₂ := R₂
, Jy := Jy
}
end
noncomputable def UpJoin.UpSquare {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} (r : Rel A₁ A₂)
: {y // r.UpJoin y} → r.UpSquare
:= begin
intro y,
cases y with y H₁,
cases classical.indefinite_description _ H₁ with x₁ H₂,
cases classical.indefinite_description _ H₂ with x₂ H₃,
cases classical.indefinite_description _ H₃ with x₃ H,
clear H₁ H₂ H₃,
cases H with R₃ Jx,
exact { x₁ := x₁, x₂ := x₂, x₃ := x₃
, y₃ := y
, R₃ := R₃
, Jx := Jx
}
end
noncomputable def UpClosed.Square {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} {r : Rel A₁ A₂}
(rUC : r.UpClosed)
: r.UpSquare → r.Square
:= begin
intro y,
have Q₁ := rUC y.Jx y.R₃,
cases classical.indefinite_description _ Q₁ with y₁ Q₂,
cases classical.indefinite_description _ Q₂ with y₂ H,
clear Q₁ Q₂,
cases H with Jy H,
cases H with R₁ R₂,
exact { x₁ := y.x₁, x₂ := y.x₂, x₃ := y.x₃
, y₁ := y₁, y₂ := y₂, y₃ := y.y₃
, R₁ := R₁, R₂ := R₂, R₃ := y.R₃
, Jx := y.Jx
, Jy := Jy
}
end
noncomputable def DownClosed.Square {A₁ : Alg.{ℓ₁}} {A₂ : Alg.{ℓ₂}} {r : Rel A₁ A₂}
(rDC : r.DownClosed)
: r.DownSquare → r.Square
:= begin
intro x,
have Q := rDC x.R₁ x.R₂ x.Jy,
cases classical.indefinite_description _ Q with x₃ H,
clear Q,
cases H with R₃ Jx,
exact { x₁ := x.x₁, x₂ := x.x₂, x₃ := x₃
, y₁ := x.y₁, y₂ := x.y₂, y₃ := x.y₃
, R₁ := x.R₁, R₂ := x.R₂, R₃ := R₃
, Jx := Jx
, Jy := x.Jy
}
end
end Sep
|
afb9357c0ccdc592d5ff2d0126da9953694e2ecf | 4b846d8dabdc64e7ea03552bad8f7fa74763fc67 | /tests/lean/run/soundness.lean | 3b82199b94aeb71e0f01d83d67af9ce48054f75f | [
"Apache-2.0"
] | permissive | pacchiano/lean | 9324b33f3ac3b5c5647285160f9f6ea8d0d767dc | fdadada3a970377a6df8afcd629a6f2eab6e84e8 | refs/heads/master | 1,611,357,380,399 | 1,489,870,101,000 | 1,489,870,101,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,713 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
Define propositional calculus, valuation, provability, validity, prove soundness.
This file is based on Floris van Doorn Coq files.
Similar to soundness.lean, but defines Nc in Type.
The idea is to be able to prove soundness using recursive equations.
-/
open nat bool list decidable
attribute [reducible]
definition PropVar := nat
inductive PropF
| Var : PropVar → PropF
| Bot : PropF
| Conj : PropF → PropF → PropF
| Disj : PropF → PropF → PropF
| Impl : PropF → PropF → PropF
namespace PropF
notation `#`:max P:max := Var P
local notation A ∨ B := Disj A B
local notation A ∧ B := Conj A B
local infixr `⇒`:27 := Impl
notation `⊥` := Bot
def Neg A := A ⇒ ⊥
notation ~ A := Neg A
def Top := ~⊥
notation `⊤` := Top
def BiImpl A B := A ⇒ B ∧ B ⇒ A
infixr `⇔`:27 := BiImpl
def valuation := PropVar → bool
def TrueQ (v : valuation) : PropF → bool
| (# P) := v P
| ⊥ := ff
| (A ∨ B) := TrueQ A || TrueQ B
| (A ∧ B) := TrueQ A && TrueQ B
| (A ⇒ B) := bnot (TrueQ A) || TrueQ B
attribute [reducible]
def is_true (b : bool) := b = tt
-- the valuation v satisfies a list of PropF, if forall (A : PropF) in Γ,
-- (TrueQ v A) is tt (the Boolean true)
def Satisfies v (Γ : list PropF) := ∀ A, A ∈ Γ → is_true (TrueQ v A)
def Models Γ A := ∀ v, Satisfies v Γ → is_true (TrueQ v A)
infix `⊨`:80 := Models
def Valid p := [] ⊨ p
reserve infix ` ⊢ `:26
/- Provability -/
inductive Nc : list PropF → PropF → Type
infix ⊢ := Nc
| Nax : ∀ Γ A, A ∈ Γ → Γ ⊢ A
| ImpI : ∀ Γ A B, A::Γ ⊢ B → Γ ⊢ A ⇒ B
| ImpE : ∀ Γ A B, Γ ⊢ A ⇒ B → Γ ⊢ A → Γ ⊢ B
| BotC : ∀ Γ A, (~A)::Γ ⊢ ⊥ → Γ ⊢ A
| AndI : ∀ Γ A B, Γ ⊢ A → Γ ⊢ B → Γ ⊢ A ∧ B
| AndE₁ : ∀ Γ A B, Γ ⊢ A ∧ B → Γ ⊢ A
| AndE₂ : ∀ Γ A B, Γ ⊢ A ∧ B → Γ ⊢ B
| OrI₁ : ∀ Γ A B, Γ ⊢ A → Γ ⊢ A ∨ B
| OrI₂ : ∀ Γ A B, Γ ⊢ B → Γ ⊢ A ∨ B
| OrE : ∀ Γ A B C, Γ ⊢ A ∨ B → A::Γ ⊢ C → B::Γ ⊢ C → Γ ⊢ C
infix ⊢ := Nc
def Provable A := [] ⊢ A
def Prop_Soundness := ∀ A, Provable A → Valid A
def Prop_Completeness := ∀ A, Valid A → Provable A
open Nc
lemma weakening2 : ∀ {Γ A Δ}, Γ ⊢ A → Γ ⊆ Δ → Δ ⊢ A
| .Γ .A Δ (Nax Γ A Hin) Hs := Nax _ _ (Hs Hin)
| .Γ .(A ⇒ B) Δ (ImpI Γ A B H) Hs := ImpI _ _ _ (weakening2 H (cons_subset_cons A Hs))
| .Γ .B Δ (ImpE Γ A B H₁ H₂) Hs := ImpE _ _ _ (weakening2 H₁ Hs) (weakening2 H₂ Hs)
| .Γ .A Δ (BotC Γ A H) Hs := BotC _ _ (weakening2 H (cons_subset_cons (~A) Hs))
| .Γ .(A ∧ B) Δ (AndI Γ A B H₁ H₂) Hs := AndI _ _ _ (weakening2 H₁ Hs) (weakening2 H₂ Hs)
| .Γ .A Δ (AndE₁ Γ A B H) Hs := AndE₁ _ _ _ (weakening2 H Hs)
| .Γ .B Δ (AndE₂ Γ A B H) Hs := AndE₂ _ _ _ (weakening2 H Hs)
| .Γ .(A ∨ B) Δ (OrI₁ Γ A B H) Hs := OrI₁ _ _ _ (weakening2 H Hs)
| .Γ .(A ∨ B) Δ (OrI₂ Γ A B H) Hs := OrI₂ _ _ _ (weakening2 H Hs)
| .Γ .C Δ (OrE Γ A B C H₁ H₂ H₃) Hs :=
OrE _ _ _ _ (weakening2 H₁ Hs) (weakening2 H₂ (cons_subset_cons A Hs)) (weakening2 H₃ (cons_subset_cons B Hs))
lemma weakening : ∀ Γ Δ A, Γ ⊢ A → Γ++Δ ⊢ A :=
λ Γ Δ A H, weakening2 H (subset_append_left Γ Δ)
lemma deduction : ∀ Γ A B, Γ ⊢ A ⇒ B → A::Γ ⊢ B :=
λ Γ A B H, ImpE _ _ _ (weakening2 H (subset_cons A Γ)) (Nax _ _ (mem_cons_self A Γ))
lemma prov_impl : ∀ A B, Provable (A ⇒ B) → ∀ Γ, Γ ⊢ A → Γ ⊢ B :=
λ A B Hp Γ Ha,
have wHp : Γ ⊢ (A ⇒ B), from weakening _ _ _ Hp,
ImpE _ _ _ wHp Ha
lemma Satisfies_cons : ∀ {A Γ v}, Satisfies v Γ → is_true (TrueQ v A) → Satisfies v (A::Γ) :=
λ A Γ v s t B BinAG,
or.elim BinAG
(λ e : B = A, by rewrite e; exact t)
(λ i : B ∈ Γ, s _ i)
attribute [simp] is_true TrueQ
theorem Soundness_general {v : valuation} : ∀ {A Γ}, Γ ⊢ A → Satisfies v Γ → is_true (TrueQ v A)
| .A .Γ (Nax Γ A Hin) s := s _ Hin
| .(A ⇒ B) .Γ (ImpI Γ A B H) s :=
by_cases
(λ t : is_true (TrueQ v A),
have Satisfies v (A::Γ), from Satisfies_cons s t,
have TrueQ v B = tt, from Soundness_general H this,
by simph)
(λ f : ¬ is_true (TrueQ v A),
have TrueQ v A = ff, by simp at f; simph,
have bnot (TrueQ v A) = tt, by simph,
by simph)
| .B .Γ (ImpE Γ A B H₁ H₂) s :=
have aux : TrueQ v A = tt, from Soundness_general H₂ s,
have bnot (TrueQ v A) || TrueQ v B = tt, from Soundness_general H₁ s,
by simp [aux] at this; simph
| .A .Γ (BotC Γ A H) s := by_contradiction
(λ n : TrueQ v A ≠ tt,
have TrueQ v A = ff, by {simp at n; simph},
have TrueQ v (~A) = tt, begin change (bnot (TrueQ v A) || ff = tt), simph end,
have Satisfies v ((~A)::Γ), from Satisfies_cons s this,
have TrueQ v ⊥ = tt, from Soundness_general H this,
absurd this ff_ne_tt)
| .(A ∧ B) .Γ (AndI Γ A B H₁ H₂) s :=
have TrueQ v A = tt, from Soundness_general H₁ s,
have TrueQ v B = tt, from Soundness_general H₂ s,
by simph
| .A .Γ (AndE₁ Γ A B H) s :=
have TrueQ v (A ∧ B) = tt, from Soundness_general H s,
by simp [TrueQ] at this; simph [is_true]
| .B .Γ (AndE₂ Γ A B H) s :=
have TrueQ v (A ∧ B) = tt, from Soundness_general H s,
by simp at this; simph
| .(A ∨ B) .Γ (OrI₁ Γ A B H) s :=
have TrueQ v A = tt, from Soundness_general H s,
by simph
| .(A ∨ B) .Γ (OrI₂ Γ A B H) s :=
have TrueQ v B = tt, from Soundness_general H s,
by simph
| .C .Γ (OrE Γ A B C H₁ H₂ H₃) s :=
have TrueQ v A || TrueQ v B = tt, from Soundness_general H₁ s,
have or (TrueQ v A = tt) (TrueQ v B = tt), by simp at this; simph,
or.elim this
(λ At,
have Satisfies v (A::Γ), from Satisfies_cons s At,
Soundness_general H₂ this)
(λ Bt,
have Satisfies v (B::Γ), from Satisfies_cons s Bt,
Soundness_general H₃ this)
theorem Soundness : Prop_Soundness :=
λ A H v s, Soundness_general H s
end PropF
|
926f0cd074060a854c3a73b9ed7e4098171830d4 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/int/char_zero.lean | d23a772c2dc7d56c7075f3c5ff7152077973c418 | [] | 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,026 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.int.cast
import Mathlib.algebra.char_zero
import Mathlib.PostPort
universes u_1
namespace Mathlib
/-!
# Injectivity of `int.cast` into characteristic zero rings.
-/
namespace int
@[simp] theorem cast_eq_zero {α : Type u_1} [add_group α] [HasOne α] [char_zero α] {n : ℤ} : ↑n = 0 ↔ n = 0 := sorry
@[simp] theorem cast_inj {α : Type u_1} [add_group α] [HasOne α] [char_zero α] {m : ℤ} {n : ℤ} : ↑m = ↑n ↔ m = n := sorry
theorem cast_injective {α : Type u_1} [add_group α] [HasOne α] [char_zero α] : function.injective coe :=
fun {a₁ a₂ : ℤ} => idRhs (↑a₁ = ↑a₂ → a₁ = a₂) (iff.mp cast_inj)
theorem cast_ne_zero {α : Type u_1} [add_group α] [HasOne α] [char_zero α] {n : ℤ} : ↑n ≠ 0 ↔ n ≠ 0 :=
not_congr cast_eq_zero
|
7041705eec00b7d110e4ce5be834094c86ac063c | d450724ba99f5b50b57d244eb41fef9f6789db81 | /src/homework/hw5.lean | 933e05fa68a08410802b2628661ef1f312377166 | [] | no_license | jakekauff/CS2120F21 | 4f009adeb4ce4a148442b562196d66cc6c04530c | e69529ec6f5d47a554291c4241a3d8ec4fe8f5ad | refs/heads/main | 1,693,841,880,030 | 1,637,604,848,000 | 1,637,604,848,000 | 399,946,698 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,138 | lean | import data.set
/-
CS2120 F21 HW5
The goals of this assignment are to give you
practice with proofs of propositions that use
existential quantificaton, and to develop your
understanding of formal and informal proofs in
set theory.
-/
/-
PART I: EXISTENTIAL QUANTIFICATION.
-/
/-
To start, suppose that α and β are arbitrary
types, and p and q are predicates on values
of these types: α and β, respectively.
-/
axioms
(α β : Type) -- data types
(p : α → Prop) -- predicates
(q : β → Prop)
/-
In this context complete the following tasks:
(1) Write a fluent English-language statement
of the proposition to be proved.
(2) Provide a formal proof of the proposition.
(3) Write an informal proof of the proposition.
-/
-- here's the proposition
example :
(∃ (f : α → β), ∀ (a : α), p a → q (f a)) →
(∃ (a : α), p a) →
(∃ (b : β), q b) :=
/-
What does this propositon say? Explain it in
plain English. Here's a start: "If there's a
function that maps/takes every α value that ...
-- your completed English rendition here:
-/
-- Give your formal proof here
begin
_
end
|
0f402c65a211fccae066a442bb6bb1d7064124ac | d450724ba99f5b50b57d244eb41fef9f6789db81 | /src/Submissions/practice_2.lean | 2d2973fd7b3547ca9c23de0c09339a53cd7cb2aa | [] | no_license | jakekauff/CS2120F21 | 4f009adeb4ce4a148442b562196d66cc6c04530c | e69529ec6f5d47a554291c4241a3d8ec4fe8f5ad | refs/heads/main | 1,693,841,880,030 | 1,637,604,848,000 | 1,637,604,848,000 | 399,946,698 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 15,103 | lean | /-
Group: Earth, Wind, and Fire
Jakob Kauffmann, jgk2qq@virginia.edu, https://github.com/jakekauff/CS2120F21.git
Jumi Hall, jah5py@virginia.edu, https://github.com/hubdaha/cs2120f21.git
Connor McCaffrey, cam7qp@virginia.edu, https://github.com/camccaffrey/cs2120.git
-/
/-
Prove the following simple logical conjectures.
Give a formal and an English proof of each one.
Your English language proofs should be complete
in the sense that they identify all the axioms
and/or theorems that you use.
-/
example : true := true.intro
/-
We can apply the introduction rule for true. QED.
-/
example : false := _ -- trick question? why?
-- This is a trick question
-- because there is no proof of false.
-- Otherwise, everything false would be true.
example : ∀ (P : Prop), P ∨ P ↔ P :=
begin
assume P,
apply iff.intro _ _,
-- forward
assume porp,
apply or.elim porp,
-- left disjunct is true
assume p1,
exact p1,
-- right disjunct is true
assume p2,
exact p2,
-- backward
assume p,
exact or.intro_left P p,
end
/-
Theorem: P ∨ P ↔ P
Proof:
Assume P is true. Separating the theorem into two routes,
we get P ∨ P → P and P → P ∨ P. For the first route, we apply
the elimination rule for or. We can derive P in both cases of or.
Then we continue onto our second route, where we assume P and
use the introduction rule for or to show that the proposition P implies P.
Next, we are left to prove P which has already been assumed to be true. QED.
-/
example : ∀ (P : Prop), P ∧ P ↔ P :=
begin
assume P,
apply iff.intro _ _,
--forward
assume pandp,
apply and.elim pandp,
assume p,
assume p,
exact p,
--backward
assume P,
apply and.intro P P,
end
/-We can assume P is true. We then seperate the theorum into 2 routes, where P ∧ P
implies P, and P implies P ∧ P. For the first route, we can assume P ∧ P and apply the
elimination rule for and, which allows us now state that P → P → P. Assuming P, we can
then say that P does imply P, which in turn implies P because these are all logical equivalents.
Then we move onto the second route, where we start with assuming an arbitrary P. To this proposition, we apply
the introduction rule for and and fill in the sides with P. QED.-/
example : ∀ (P Q : Prop), P ∨ Q ↔ Q ∨ P :=
begin
assume P Q,
apply iff.intro _ _,
assume porq,
apply or.elim porq,
assume P,
apply or.intro_right,
exact P,
apply or.intro_left,
assume qorp,
apply or.elim qorp,
assume Q,
apply or.intro_right,
exact Q,
apply or.intro_left,
end
/-
We can assume that P and Q are true. We then seperate the theorum into 2 routes using the
introduction rule for if and only if, where P ∨ Q implies Q ∨ P, and Q ∨ P implies P ∨ Q.
For the first route, we can assume P ∨ Q.
To this, we apply the elimiation rule for or. We now have 3 paths to take. We can then assume
P, and apply the introduction rule for or to the right side of P ∨ Q, leaving us with P. Then to
complete the second goal where Q implies Q ∧ P, we can apply the introduction rule for or to the
left of this proposition.
Next, we move to the 2nd route, where we must prove that Q ∨ P implies P ∨ Q. We this we can assume Q ∨ P.
Then we can apply the elimination rule for or to Q ∨ P, giving us 2 routes to go down. We then assume Q and
apply the introduction rule for or to the right side, giving us Q, which we have. Then we apply
the introduction rule for or to the left side. QED.
-/
example : ∀ (P Q : Prop), P ∧ Q ↔ Q ∧ P :=
begin
assume P Q,
apply iff.intro _ _,
assume pandq,
have p : P := and.elim_left pandq,
have q : Q := and.elim_right pandq,
have qp : Q ∧ P := and.intro q p,
exact qp,
assume qandp,
have q : Q := and.elim_left qandp,
have p : P := and.elim_right qandp,
have pq : P ∧ Q := and.intro p q,
exact pq,
end
/-
For this proposition, we must assume P and Q. Then we apply the introduction rule for if
and only if. This gives us 2 propositions to prove, where P ∧ Q implies Q ∧ P, and Q ∧
P implies P ∧ Q. For the first, we assume P ∧ Q. Then, using the elimination rule
for and, we isolate P and Q using the left and right elimination rules, respectively. We can
also create the proposition Q ∧ P with the introduction rule for and, giving us Q ∧ P. For the
second route, where Q ∧ P implies P ∧ Q, we assume Q ∧ P. Then we isolate Q and P respectively through
the elimation rules for and on the left and the right. Then we create P ∧ Q with the introduction rule
for and. QED.
-/
example : ∀ (P Q R : Prop), P ∧ (Q ∨ R) ↔ (P ∧ Q) ∨ (P ∧ R) :=
begin
assume P Q R,
apply iff.intro _ _,
assume pqr,
have p : P := and.elim_left pqr,
have qr : Q ∨ R := and.elim_right pqr,
apply or.elim qr,
assume Q,
apply or.intro_left,
apply and.intro p Q,
assume R,
apply or.intro_right,
apply and.intro p R,
assume pqpr,
apply or.elim pqpr,
assume pq,
have p : P := and.elim_left pq,
apply and.intro p _,
apply or.intro_left,
exact and.elim_right pq,
assume pr,
have p : P := and.elim_left pr,
apply and.intro p _,
apply or.intro_right,
exact and.elim_right pr,
end
/-
First we assume that we have P Q and R. Then we must apply the introduciton rule for if and
only if, giving us two paths that consist of P ∧ (Q ∨ R) implying (P ∧ Q) ∨ (P ∧ R), and
(P ∧ Q) ∨ (P ∧ R) implying P ∧ (Q ∨ R). For the first proposition, we assume that we have a proof of
P ∧ (Q ∨ R). Given that, we can isolate P and Q ∧ R using the left and right elimination rules for and,
respectively. Then we can apply the elimination rule for or to Q ∧ R, separating the cases for
this or statement. When we assume Q for Q → ∧ Q ∨ P ∧ R, we can then apply the
introduction rule for or to the left side of the if statement, leaving us with P ∧ Q for our goal.
This goal can be completed by applying the introduction rule for and to P and Q. Now we must assume
R and this time apply the introduction rule for or to the right side of the if statement, leaving us
with P ∧ R for our goal. We can complete this goal using the introduction rule for and and applying
it to P and R. Now we must go down the opposite route, where (P ∧ Q) ∨ (P ∧ R) implies P ∧ (Q ∨ R).
To start, we assume (P ∧ Q) ∨ (P ∧ R). Now we apply the elimination rule for or, giving us
the possible routes to go down with this or statement. On the left side of the or statement is P ∧ Q,
which we assume to be true. Then we can apply the introduction rule for and to P, which we can isolate
through the elimination rule for and to the left side of P ∧ Q. The other side of this introduction rule for
and will be applied to Q ∨ R, which can be derived from applying the introduction rule for or
to the left side of Q ∨ R, leaving us with Q. We can derive Q through the elimination rule for and
to the right side of P ∧ Q. Now we have one goal left. First we can isolate p through the elimination rule
for and to the left side of P ∧ R. Then we can apply the introduction rule for or to the right side
of Q ∨ R, leaving us with R, which we can derive from using the elimination rule for and and applying it
to the right side of P ∧ R. This allows us to apply the introduction rule for and to P and R. QED.
-/
example : ∀ (P Q R : Prop), P ∨ (Q ∧ R) ↔ (P ∨ Q) ∧ (P ∨ R) :=
begin
assume P Q R,
apply iff.intro _ _,
assume pqr,
apply or.elim pqr,
assume P,
apply and.intro _ _,
apply or.intro_left,
exact P,
apply or.intro_left,
exact P,
assume qr,
apply and.intro _ _,
apply or.intro_right,
apply and.elim_left qr,
apply or.intro_right,
exact and.elim_right qr,
assume pqpr,
have porq : P ∨ Q := and.elim_left pqpr,
have porr : P ∨ R := and.elim_right pqpr,
apply or.elim porq,
assume P,
apply or.intro_left,
exact P,
assume q,
apply or.elim porr,
assume P,
apply or.intro_left,
exact P,
assume r,
have qr : Q ∧ R := and.intro q r,
apply or.intro_right,
exact qr,
end
/-
First we must assume P Q and R. Then we can apply the introduction rule for if and only if, creating two routes to prove,
where P ∨ (Q ∧ R) → (P ∨ Q) ∧ (P ∨ R) and (P ∨ Q) ∧ (P ∨ R) → P ∨ (Q ∧ R). For the first route, we msut assume
P ∨ (Q ∧ R). Then we can apply the or elimination rule to P ∨ (Q ∧ R), dividing this or statement into two
routes. For this first new "or" route, we can assume P and then apply the and introduction rule for the
two statements we will get later. To do so we will apply the or introduction rule to the left of
P ∨ R, leaving us to prove P, which we have already assumed. Then we can apply the or introduction rule to the left
and apply P again, which we already have to get the other or statement. Now we must assume Q ∧ R. We will do the same thing
of applying the and introduction rule before completing the two or statements. To get these or statements
we will apply the or introduction rule to the right, and then apply the and elimination rule to the left of Q ∧ R to
isolate Q. Now for the other pathway we must assume (P ∨ Q) ∧ (P ∨ R). Then we can isolate the or statements using the
elimination rule for and on both sides. Then we can apply the or elimination rule to P ∨ Q. We can assume P, and then apply the
or introduction rule to the left of P ∨ Q ∧ R, This allows us to find P. Then, we can assume Q and apply
the or elimination rule to P ∨ R. We can assume P, apply the or introduction rule to the left side,
and find P. Fianlly, we will assume R, and isolate Q ∧ R by combing q and r through the introduction rule for add.
QED.
-/
example : ∀ (P Q : Prop), P ∧ (P ∨ Q) ↔ P :=
begin
assume P Q,
apply iff.intro _ _,
--forward
assume ppq,
exact and.elim_left ppq,
-- backward
assume p,
apply and.intro p _,
apply or.intro_left,
exact p,
end
/-
First we must assume P and Q. Then, we will use the introduction rule for if and only if
to create two pathways: P ∧ (P ∨ Q) implies P, and P implies P ∧ (P ∨ Q).
For the first, we will assume P ∧ (P ∨ Q). Then to prove this we begin by using the
elimination rule for and apply it to the left where we can isolate P. Now for
the other direction we first assume P. Then we can apply the introduction rule for and, where we add P
to the left side of the ∧. To create the right side of P ∧ (P ∨ Q), we use the introduction rule
for or and apply it to the left, leaving us with P, which we have. QED.
-/
example : ∀ (P Q : Prop), P ∨ (P ∧ Q) ↔ P :=
begin
assume P Q,
apply iff.intro _ _,
assume ppq,
apply or.elim ppq,
assume p,
exact p,
assume pq,
apply and.elim_left pq,
assume p,
apply or.intro_left,
exact p,
end
/-
First we must assume P and Q. Then we can apply the introduction rule for if and only if, leaving
us with 2 pathways: P ∨ (P ∧ Q) → P and P → P ∨ (P ∧ Q). For the fowards pathway, we will assume
P ∨ (P ∧ Q). Then we can apply the or elimination rule to this, leaving us with 3 goals now. For the first
of the goals, we need to show that P → P, which we have. Then we assume P ∧ Q for the next goal, leaving us
with a goal of P. To isolate P, we use the and elimination rule and apply it to the left of P ∧ Q.
Lastlyk for our backwards route we assume P. Then we apply the or introduction rule to the left
of P ∨ (P ∧ Q), leaving us with P. We have already assumed P. QED.
-/
example : ∀ (P : Prop), P ∨ true ↔ true :=
begin
assume P,
apply iff.intro _ _,
assume port,
apply true.intro,
assume t,
apply or.intro_right,
apply true.intro,
end
/-
First we assume P. Then we apply the introduction rule for if and only if, giving us two pathways
that consist of P ∨ true → true and true → P ∨ true. For the first pathway we assume P ∨ true. Then
we apply the introduction rule for true for true. Then for the other pathway we must assume true, and
then apply the or introduction rule to the right of P ∨ true, leaving us with a goal of proving true. We
can do this by again using the true introduction rule. QED.
-/
example : ∀ (P : Prop), P ∨ false ↔ P :=
begin
assume P,
apply iff.intro _ _,
assume h,
apply or.elim h,
assume p,
exact p,
assume f,
cases f,
assume p,
apply or.intro_left,
exact p,
end
/-
First we must assume P. Then we can apply the introduction rule for if and only if, giving us two
routes to prove, where P ∨ false → P and P → P ∨ false. With this first route we assume P ∨ false. Then we
can apply the or elimination rule on P ∨ false, leaving us to prove that P → P. We can do this by assuming P.
Now we must prove that we an derive P from false, which we do by first assuming false, and then performing
case analyses on false. Then for the second pathway we assume P, and then apply the introduction rule for
or to the left of P ∨ false, leaving us to prove P, which we have already assumed. QED.
-/
example : ∀ (P : Prop), P ∧ true ↔ P :=
begin
assume P,
apply iff.intro _ _,
assume h,
apply and.elim_left h,
assume p,
apply and.intro,
exact p,
apply true.intro,
end
/-
We first assume an arbitrary proposition P. Then we use the if and only if introduction rule to give us two
routes: P ∧ true → P and P → P ∧ true. Beginning with the first, we must assume P ∧ true.
Then we can prove P by using the and elimination rule on the left side of P ∧ true. Now for
the other pathway we must assume P. Then we can apply the introduction rule for and to P and true. We may derive
true by using the introduction rule for true. QED.
-/
example : ∀ (P : Prop), P ∧ false ↔ false :=
begin
assume P,
apply iff.intro _ _,
assume h,
exact and.elim_right h,
assume f,
apply and.intro _ f,
cases f,
end
/-
We first assume an arbitrary proposition P. Then we can apply the introduction rule for if and only if to
get two routes: P ∧ false → false, and false → P ∧ false. For the first route we must assume P ∧ false.
Then, to prove "false" we apply the and elimination rule to the right side of P ∧ false. For the other route,
we begin by assuming false. Then we can use case analysis for false to get P, and apply the and introduction rule
to P and false. QED.
-/ |
05a5a139d4aeb436490357ef838dc5bbef4fa028 | 7a097aab717143b13a4a4f8824fe9cfe670a7883 | /src/smt2/lol.lean | 1bcc945aef14d0f3bde472412885944ef3a1cb9c | [
"Apache-2.0"
] | permissive | digama0/smt2_interface | 6b3bbc12cb90bd4bc70b07ca86918c1e9ff4ed97 | cf516ae4142f33adcf4b272758d63fee37ec9f7d | refs/heads/master | 1,611,266,770,453 | 1,498,177,713,000 | 1,498,177,713,000 | 95,167,865 | 0 | 0 | null | 1,498,177,813,000 | 1,498,177,813,000 | null | UTF-8 | Lean | false | false | 6,782 | lean | import smt2.syntax
import smt2.builder
import .except
-- instance {α : Type} [decidable_eq α] : decidable_eq (list α)
-- | [] [] := is_true rfl
-- | (a::l) [] := is_false (λh, list.no_confusion h)
-- | [] (b::l') := is_false (λh, list.no_confusion h)
-- | (a::l) (b::l') :=
-- begin
-- refine (if ab : a = b then _ else _),
-- constructor,
-- rewrite ab,
-- end
def ordering.or_else : ordering → thunk ordering → ordering
| ordering.lt _ := ordering.lt
| ordering.eq f := f ()
| ordering.gt _ := ordering.gt
-- temp from mario's pr
instance fin.has_ordering (n : nat) : has_ordering (fin n) :=
⟨λ a b, nat.cmp a.1 b.1⟩
instance : has_ordering char :=
fin.has_ordering _
instance : has_ordering unsigned :=
fin.has_ordering _
def list.cmp {α : Type} [has_ordering α] : list α → list α → ordering
| [] [] := ordering.eq
| [] (b::l') := ordering.lt
| (a::l) [] := ordering.gt
| (a::l) (b::l') := (has_ordering.cmp a b).or_else (list.cmp l l')
instance {α : Type} [has_ordering α] : has_ordering (list α) :=
⟨list.cmp⟩
-- end temp
def string.cmp : string → string → ordering :=
fun s1 s2, list.cmp s1.to_list s2.to_list
instance : has_ordering string :=
⟨string.cmp⟩
namespace lol
structure refinement (T : Type) :=
(pred : string → T)
mutual inductive type, term
with type : Type
| bool : type
| int : type
| var : string → type
| fn : list type → type → type
| refinement : type → refinement type → type
with term : Type
-- TODO: eventually allow for term in head position, and generalize in trans
-- TODO: stratify this so that Prop > Ordering > Arith
| apply : string → list term → term
| true : term
| false : term
| var : string → term
| not : term → term
| equals : term → term → term
| and : term → term → term
| or : term → term → term
| iff : term → term → term
| implies : term → term → term
| add : term → term → term
| sub : term → term → term
| mul : term → term → term
| div : term → term → term
| lt : term → term → term
| lte : term → term → term
| gt : term → term → term
| gte : term → term → term
| int : int → term
| forallq : string → type → term → term
mutual def type.to_string, list_map
with type.to_string : type → string
| (type.int) := "int"
| (type.bool) := "bool"
| (type.var s) := s
| (type.refinement t ref) := "t { ... }" -- fixme
| (type.fn args rt) := string.join (list_map args) ++ (type.to_string rt)
with list_map : list type → list string
| [] := []
| (t :: ts) := type.to_string t :: (list_map ts)
instance type.has_to_string : has_to_string type :=
⟨ type.to_string ⟩
inductive decl
| fn : string → type → (option term) → decl
def decl.name : decl → string
| (decl.fn n _ _) := n
meta structure context :=
(type_decl : rb_map string type)
(decls : rb_map string decl)
(assertions : list term)
meta def context.empty : context :=
⟨ rb_map.mk _ _ , rb_map.mk _ _ , [] ⟩
meta def context.declare_type : context → string → type → context
| ⟨ type_decl, decls, assertions ⟩ n ty :=
match ty with
| type.fn _ _ := ⟨ type_decl.insert n ty, decls, assertions ⟩
| _ := ⟨ type_decl, decls, assertions ⟩
end
meta def context.assert : context → term → context
| ⟨ type_decl, decls, assertions ⟩ t := ⟨ type_decl, decls, t :: assertions ⟩
meta def context.declare : context → decl → context
| ctxt decl :=
{ ctxt with decls := ctxt.decls.insert decl.name decl }
open smt2.builder
private meta def compile_type_simple : type → smt2.builder smt2.sort
| (type.bool) := return "Bool"
| (type.int) := return "Int"
| (type.var s) := return s
| (type.fn [] rt) := compile_type_simple rt
| ty := smt2.builder.fail $ "simple type error: " ++ to_string ty
private meta def compile_type : type → smt2.builder ((list smt2.sort) × smt2.sort)
| (type.bool) := return ([], "Bool")
| (type.int) := return ([], "Int")
| (type.fn args ret) :=
do args' ← monad.mapm compile_type_simple args,
ret' ← compile_type_simple ret,
return (args', ret')
| (type.var s) := return ([], s)
| (type.refinement t ref) := smt2.builder.fail "compile type error"
private meta def compile_types : list (string × type) → smt2.builder unit
| [] := return ()
| ((n, ty) :: decls) :=
do match ty with
| type.fn args ret :=
-- TODO: fix me
declare_sort n 0
| type.int := return ()
| type.bool := return ()
| type.var _ := return ()
| type.refinement t ref := return ()
end,
compile_types decls.
private meta def compile_decl : decl → smt2.builder unit
| (decl.fn n ty none) :=
do (args, rt) ← compile_type ty,
declare_fun n args rt
| _ := return () -- TODO: fix me
private meta def compile_decls : list (string × decl) → smt2.builder unit
| [] := return ()
| ((n, d) :: rs) := do compile_decl d, compile_decls rs
private meta def compile_term : lol.term → smt2.builder smt2.term
| (term.apply t us) := smt2.term.apply t <$> monad.mapm compile_term us
| (term.true) := pure $ smt2.term.qual_id "true"
| (term.false) := pure $ smt2.term.qual_id "false"
| (term.var str) := pure $ smt2.term.qual_id str
| (term.not t) := smt2.builder.not <$> compile_term t
| (term.equals t u) := smt2.builder.equals <$> compile_term t <*> compile_term u
| (term.and t u) := smt2.builder.and2 <$> compile_term t <*> compile_term u
| (term.or t u) := smt2.builder.or2 <$> compile_term t <*> compile_term u
| (term.implies t u) := smt2.builder.implies <$> compile_term t <*> compile_term u
| (term.iff t u) := smt2.builder.iff <$> compile_term t <*> compile_term u
| (term.add a b) := smt2.builder.add <$> compile_term a <*> compile_term b
| (term.sub a b) := smt2.builder.sub <$> compile_term a <*> compile_term b
| (term.mul a b) := smt2.builder.mul <$> compile_term a <*> compile_term b
| (term.div a b) := smt2.builder.div <$> compile_term a <*> compile_term b
| (term.lt a b) := smt2.builder.lt <$> compile_term a <*> compile_term b
| (term.lte a b) := smt2.builder.lte <$> compile_term a <*> compile_term b
| (term.gt a b) := smt2.builder.gt <$> compile_term a <*> compile_term b
| (term.gte a b) := smt2.builder.gte <$> compile_term a <*> compile_term b
| (term.int i) := return $ smt2.builder.int_const i
| (term.forallq n ty body) := smt2.builder.forallq n <$> (compile_type_simple ty) <*> compile_term body
private meta def compile_assertions : list term → smt2.builder unit
| [] := return ()
| (t :: ts) :=
do t' ← compile_term t,
smt2.builder.assert t',
compile_assertions ts
meta def compile (ctxt : context) : smt2.builder unit :=
do compile_types ctxt.type_decl.to_list,
compile_decls ctxt.decls.to_list,
compile_assertions ctxt.assertions
end lol
|
b3b029ed060736e342bc5b0c0432ac6f7cd8c6a0 | 4b846d8dabdc64e7ea03552bad8f7fa74763fc67 | /library/init/meta/vm.lean | cb1b02f0859912d1f84ac8c6b7cac673ff761616 | [
"Apache-2.0"
] | permissive | pacchiano/lean | 9324b33f3ac3b5c5647285160f9f6ea8d0d767dc | fdadada3a970377a6df8afcd629a6f2eab6e84e8 | refs/heads/master | 1,611,357,380,399 | 1,489,870,101,000 | 1,489,870,101,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,515 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.meta.tactic init.data.option.basic
import init.meta.mk_dec_eq_instance
meta constant vm_obj : Type
inductive vm_obj_kind
| simple | constructor | closure | native_closure | mpz
| name | level | expr | declaration
| environment | tactic_state | format
| options | other
instance vm_obj_kind_dec_eq : decidable_eq vm_obj_kind :=
by tactic.mk_dec_eq_instance
namespace vm_obj
meta constant kind : vm_obj → vm_obj_kind
/- For simple and constructor vm_obj's, it returns the constructor tag/index.
Return 0 otherwise. -/
meta constant cidx : vm_obj → nat
/- For closure vm_obj's, it returns the internal function index. -/
meta constant fn_idx : vm_obj → nat
/- For constructor vm_obj's, it returns the data stored in the object.
For closure vm_obj's, it returns the local arguments captured by the closure. -/
meta constant fields : vm_obj → list vm_obj
/- For simple and mpz vm_obj's -/
meta constant to_nat : vm_obj → nat
/- For name vm_obj's, it returns the name wrapped by the vm_obj. -/
meta constant to_name : vm_obj → name
/- For level vm_obj's, it returns the universe level wrapped by the vm_obj. -/
meta constant to_level : vm_obj → level
/- For expr vm_obj's, it returns the expression wrapped by the vm_obj. -/
meta constant to_expr : vm_obj → expr
/- For declaration vm_obj's, it returns the declaration wrapped by the vm_obj. -/
meta constant to_declaration : vm_obj → declaration
/- For environment vm_obj's, it returns the environment wrapped by the vm_obj. -/
meta constant to_environment : vm_obj → environment
/- For tactic_state vm_obj's, it returns the tactic_state object wrapped by the vm_obj. -/
meta constant to_tactic_state : vm_obj → tactic_state
/- For format vm_obj's, it returns the format object wrapped by the vm_obj. -/
meta constant to_format : vm_obj → format
end vm_obj
meta constant vm_decl : Type
inductive vm_decl_kind
| bytecode | builtin | cfun
/- Information for local variables and arguments on the VM stack.
Remark: type is only available if it is a closed term at compilation time. -/
meta structure vm_local_info :=
(id : name) (type : option expr)
namespace vm_decl
meta constant kind : vm_decl → vm_decl_kind
meta constant to_name : vm_decl → name
/- Internal function index associated with the given VM declaration. -/
meta constant idx : vm_decl → nat
/- Number of arguments needed to execute the given VM declaration. -/
meta constant arity : vm_decl → nat
/- Return source position if available -/
meta constant pos : vm_decl → option pos
/- Return .olean file where the given VM declaration was imported from. -/
meta constant olean : vm_decl → option string
/- Return names .olean file where the given VM declaration was imported from. -/
meta constant args_info : vm_decl → list vm_local_info
end vm_decl
meta constant vm_core : Type → Type
meta constant vm_core.map {α β : Type} : (α → β) → vm_core α → vm_core β
meta constant vm_core.ret {α : Type} : α → vm_core α
meta constant vm_core.bind {α β : Type} : vm_core α → (α → vm_core β) → vm_core β
meta instance : monad vm_core :=
{map := @vm_core.map, pure := @vm_core.ret, bind := @vm_core.bind}
@[reducible] meta def vm (α : Type) : Type := option_t vm_core α
namespace vm
meta constant get_env : vm environment
meta constant get_decl : name → vm vm_decl
meta constant get_options : vm options
meta constant stack_size : vm nat
/- Return the vm_obj stored at the given position on the execution stack.
It fails if position >= vm.stack_size -/
meta constant stack_obj : nat → vm vm_obj
/- Return (name, type) for the object at the given position on the execution stack.
It fails if position >= vm.stack_size.
The name is anonymous if vm_obj is a transient value created by the compiler.
Type information is only recorded if the type is a closed term at compilation time. -/
meta constant stack_obj_info : nat → vm (name × option expr)
/- Pretty print the vm_obj at the given position on the execution stack. -/
meta constant pp_stack_obj : nat → vm format
/- Pretty print the given expression. -/
meta constant pp_expr : expr → vm format
/- Number of frames on the call stack. -/
meta constant call_stack_size : vm nat
/- Return the function name at the given stack frame.
Action fails if position >= vm.call_stack_size. -/
meta constant call_stack_fn : nat → vm name
/- Return the range [start, end) for the given stack frame.
Action fails if position >= vm.call_stack_size.
The values start and end correspond to positions at the execution stack.
We have that 0 <= start < end <= vm.stack_size -/
meta constant call_stack_var_range : nat → vm (nat × nat)
/- Return the name of the function on top of the call stack. -/
meta constant curr_fn : vm name
/- Return the base stack pointer for the frame on top of the call stack. -/
meta constant bp : vm nat
/- Return the program counter. -/
meta constant pc : vm nat
/- Convert the given vm_obj into a string -/
meta constant obj_to_string : vm_obj → vm string
meta constant put_str : string → vm unit
meta constant get_line : vm string
/- Return tt if end of the input stream has been reached.
For example, this can happen if the user presses Ctrl-D -/
meta constant eof : vm bool
/- Return the list of declarations tagged with the given attribute. -/
meta constant get_attribute : name → vm (list name)
meta def trace {α : Type} [has_to_format α] (a : α) : vm unit :=
do fmt ← return $ to_fmt a,
return $ _root_.trace_fmt fmt (λ u, ())
end vm
meta structure vm_monitor (α : Type) :=
(init : α) (step : α → vm α)
/- Registers a new virtual machine monitor. The argument must be the name of a definition of type
`vm_monitor S`. The command will override the last monitor.
If option 'debugger' is true, then the VM will initialize the vm_monitor state using the
'init' field, and will invoke the function 'step' before each instruction is invoked. -/
meta constant vm_monitor.register : name → command
|
1e527b4b6f8d8058860f8ad9777654bd2ced1ec0 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/topology/sheaves/sheaf_condition/unique_gluing.lean | f12c4adc4668dceb835d3fffe14f139bf15a780f | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 10,765 | lean | /-
Copyright (c) 2021 Justus Springer. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Justus Springer
-/
import topology.sheaves.forget
import category_theory.limits.shapes.types
import topology.sheaves.sheaf
import category_theory.types
/-!
# The sheaf condition in terms of unique gluings
We provide an alternative formulation of the sheaf condition in terms of unique gluings.
We work with sheaves valued in a concrete category `C` admitting all limits, whose forgetful
functor `C ⥤ Type` preserves limits and reflects isomorphisms. The usual categories of algebraic
structures, such as `Mon`, `AddCommGroup`, `Ring`, `CommRing` etc. are all examples of this kind of
category.
A presheaf `F : presheaf C X` satisfies the sheaf condition if and only if, for every
compatible family of sections `sf : Π i : ι, F.obj (op (U i))`, there exists a unique gluing
`s : F.obj (op (supr U))`.
Here, the family `sf` is called compatible, if for all `i j : ι`, the restrictions of `sf i`
and `sf j` to `U i ⊓ U j` agree. A section `s : F.obj (op (supr U))` is a gluing for the
family `sf`, if `s` restricts to `sf i` on `U i` for all `i : ι`
We show that the sheaf condition in terms of unique gluings is equivalent to the definition
in terms of equalizers. Our approach is as follows: First, we show them to be equivalent for
`Type`-valued presheaves. Then we use that composing a presheaf with a limit-preserving and
isomorphism-reflecting functor leaves the sheaf condition invariant, as shown in
`topology/sheaves/forget.lean`.
-/
noncomputable theory
open Top
open Top.presheaf
open Top.presheaf.sheaf_condition_equalizer_products
open category_theory
open category_theory.limits
open topological_space
open topological_space.opens
open opposite
universes u v
variables {C : Type u} [category.{v} C] [concrete_category.{v} C]
namespace Top
namespace presheaf
section
local attribute [instance] concrete_category.has_coe_to_sort concrete_category.has_coe_to_fun
variables {X : Top.{v}} (F : presheaf C X) {ι : Type v} (U : ι → opens X)
/--
A family of sections `sf` is compatible, if the restrictions of `sf i` and `sf j` to `U i ⊓ U j`
agree, for all `i` and `j`
-/
def is_compatible (sf : Π i : ι, F.obj (op (U i))) : Prop :=
∀ i j : ι, F.map (inf_le_left (U i) (U j)).op (sf i) = F.map (inf_le_right (U i) (U j)).op (sf j)
/--
A section `s` is a gluing for a family of sections `sf` if it restricts to `sf i` on `U i`,
for all `i`
-/
def is_gluing (sf : Π i : ι, F.obj (op (U i))) (s : F.obj (op (supr U))) : Prop :=
∀ i : ι, F.map (opens.le_supr U i).op s = sf i
/--
The sheaf condition in terms of unique gluings. A presheaf `F : presheaf C X` satisfies this sheaf
condition if and only if, for every compatible family of sections `sf : Π i : ι, F.obj (op (U i))`,
there exists a unique gluing `s : F.obj (op (supr U))`.
We prove this to be equivalent to the usual one below in
`is_sheaf_iff_is_sheaf_unique_gluing`
-/
def is_sheaf_unique_gluing : Prop :=
∀ ⦃ι : Type v⦄ (U : ι → opens X) (sf : Π i : ι, F.obj (op (U i))),
is_compatible F U sf → ∃! s : F.obj (op (supr U)), is_gluing F U sf s
end
section type_valued
variables {X : Top.{v}} (F : presheaf (Type v) X) {ι : Type v} (U : ι → opens X)
/--
For presheaves of types, terms of `pi_opens F U` are just families of sections.
-/
def pi_opens_iso_sections_family : pi_opens F U ≅ Π i : ι, F.obj (op (U i)) :=
limits.is_limit.cone_point_unique_up_to_iso
(limit.is_limit (discrete.functor (λ i : ι, F.obj (op (U i)))))
((types.product_limit_cone (λ i : ι, F.obj (op (U i)))).is_limit)
/--
Under the isomorphism `pi_opens_iso_sections_family`, compatibility of sections is the same
as being equalized by the arrows `left_res` and `right_res` of the equalizer diagram.
-/
lemma compatible_iff_left_res_eq_right_res (sf : pi_opens F U) :
is_compatible F U ((pi_opens_iso_sections_family F U).hom sf)
↔ left_res F U sf = right_res F U sf :=
begin
split ; intros h,
{ ext ⟨i, j⟩,
rw [left_res, types.limit.lift_π_apply', fan.mk_π_app,
right_res, types.limit.lift_π_apply', fan.mk_π_app],
exact h i j, },
{ intros i j,
convert congr_arg (limits.pi.π (λ p : ι × ι, F.obj (op (U p.1 ⊓ U p.2))) (i,j)) h,
{ rw [left_res, types.pi_lift_π_apply], refl },
{ rw [right_res, types.pi_lift_π_apply], refl } }
end
/--
Under the isomorphism `pi_opens_iso_sections_family`, being a gluing of a family of
sections `sf` is the same as lying in the preimage of `res` (the leftmost arrow of the
equalizer diagram).
-/
@[simp]
lemma is_gluing_iff_eq_res (sf : pi_opens F U) (s : F.obj (op (supr U))):
is_gluing F U ((pi_opens_iso_sections_family F U).hom sf) s ↔ res F U s = sf :=
begin
split ; intros h,
{ ext i,
rw [res, types.limit.lift_π_apply', fan.mk_π_app],
exact h i, },
{ intro i,
convert congr_arg (limits.pi.π (λ i : ι, F.obj (op (U i))) i) h,
rw [res, types.pi_lift_π_apply],
refl },
end
/--
The "equalizer" sheaf condition can be obtained from the sheaf condition
in terms of unique gluings.
-/
lemma is_sheaf_of_is_sheaf_unique_gluing_types (Fsh : F.is_sheaf_unique_gluing) :
F.is_sheaf :=
begin
intros ι U,
refine ⟨fork.is_limit.mk' _ _⟩,
intro s,
have h_compatible : ∀ x : s.X,
F.is_compatible U ((F.pi_opens_iso_sections_family U).hom (s.ι x)),
{ intro x,
rw compatible_iff_left_res_eq_right_res,
convert congr_fun s.condition x, },
choose m m_spec m_uniq using
λ x : s.X, Fsh U ((pi_opens_iso_sections_family F U).hom (s.ι x)) (h_compatible x),
refine ⟨m, _, _⟩,
{ ext i x,
simp [res],
exact m_spec x i, },
{ intros l hl,
ext x,
apply m_uniq,
rw is_gluing_iff_eq_res,
exact congr_fun hl x },
end
/--
The sheaf condition in terms of unique gluings can be obtained from the usual
"equalizer" sheaf condition.
-/
lemma is_sheaf_unique_gluing_of_is_sheaf_types (Fsh : F.is_sheaf) :
F.is_sheaf_unique_gluing :=
begin
intros ι U sf hsf,
let sf' := (pi_opens_iso_sections_family F U).inv sf,
have hsf' : left_res F U sf' = right_res F U sf',
{ rwa [← compatible_iff_left_res_eq_right_res F U sf', inv_hom_id_apply] },
choose s s_spec s_uniq using types.unique_of_type_equalizer _ _ (Fsh U).some sf' hsf',
use s,
dsimp,
split,
{ convert (is_gluing_iff_eq_res F U sf' _).mpr s_spec,
rw inv_hom_id_apply },
{ intros y hy,
apply s_uniq,
rw ← is_gluing_iff_eq_res F U,
convert hy,
rw inv_hom_id_apply, },
end
/--
For type-valued presheaves, the sheaf condition in terms of unique gluings is equivalent to the
usual sheaf condition in terms of equalizer diagrams.
-/
lemma is_sheaf_iff_is_sheaf_unique_gluing_types :
F.is_sheaf ↔ F.is_sheaf_unique_gluing :=
iff.intro (is_sheaf_unique_gluing_of_is_sheaf_types F)
(is_sheaf_of_is_sheaf_unique_gluing_types F)
end type_valued
section
local attribute [instance] concrete_category.has_coe_to_sort concrete_category.has_coe_to_fun
variables [has_limits C] [reflects_isomorphisms (forget C)] [preserves_limits (forget C)]
variables {X : Top.{v}} (F : presheaf C X) {ι : Type v} (U : ι → opens X)
/--
For presheaves valued in a concrete category, whose forgetful functor reflects isomorphisms and
preserves limits, the sheaf condition in terms of unique gluings is equivalent to the usual one
in terms of equalizer diagrams.
-/
lemma is_sheaf_iff_is_sheaf_unique_gluing :
F.is_sheaf ↔ F.is_sheaf_unique_gluing :=
iff.trans (is_sheaf_iff_is_sheaf_comp (forget C) F)
(is_sheaf_iff_is_sheaf_unique_gluing_types (F ⋙ forget C))
end
end presheaf
namespace sheaf
open presheaf
open category_theory
section
local attribute [instance] concrete_category.has_coe_to_sort concrete_category.has_coe_to_fun
variables [has_limits C] [reflects_isomorphisms (concrete_category.forget C)]
variables [preserves_limits (concrete_category.forget C)]
variables {X : Top.{v}} (F : sheaf C X) {ι : Type v} (U : ι → opens X)
/--
A more convenient way of obtaining a unique gluing of sections for a sheaf.
-/
lemma exists_unique_gluing (sf : Π i : ι, F.1.obj (op (U i)))
(h : is_compatible F.1 U sf ) :
∃! s : F.1.obj (op (supr U)), is_gluing F.1 U sf s :=
(is_sheaf_iff_is_sheaf_unique_gluing F.1).mp F.property U sf h
/--
In this version of the lemma, the inclusion homs `iUV` can be specified directly by the user,
which can be more convenient in practice.
-/
lemma exists_unique_gluing' (V : opens X) (iUV : Π i : ι, U i ⟶ V) (hcover : V ≤ supr U)
(sf : Π i : ι, F.1.obj (op (U i))) (h : is_compatible F.1 U sf) :
∃! s : F.1.obj (op V), ∀ i : ι, F.1.map (iUV i).op s = sf i :=
begin
have V_eq_supr_U : V = supr U := le_antisymm hcover (supr_le (λ i, (iUV i).le)),
obtain ⟨gl, gl_spec, gl_uniq⟩ := F.exists_unique_gluing U sf h,
refine ⟨F.1.map (eq_to_hom V_eq_supr_U).op gl, _, _⟩,
{ intro i,
rw [← comp_apply, ← F.1.map_comp],
exact gl_spec i },
{ intros gl' gl'_spec,
convert congr_arg _ (gl_uniq (F.1.map (eq_to_hom V_eq_supr_U.symm).op gl') (λ i,_)) ;
rw [← comp_apply, ← F.1.map_comp],
{ rw [eq_to_hom_op, eq_to_hom_op, eq_to_hom_trans, eq_to_hom_refl,
F.1.map_id, id_apply] },
{ convert gl'_spec i } }
end
@[ext]
lemma eq_of_locally_eq (s t : F.1.obj (op (supr U)))
(h : ∀ i, F.1.map (opens.le_supr U i).op s = F.1.map (opens.le_supr U i).op t) :
s = t :=
begin
let sf : Π i : ι, F.1.obj (op (U i)) := λ i, F.1.map (opens.le_supr U i).op s,
have sf_compatible : is_compatible _ U sf,
{ intros i j, simp_rw [← comp_apply, ← F.1.map_comp], refl },
obtain ⟨gl, -, gl_uniq⟩ := F.exists_unique_gluing U sf sf_compatible,
transitivity gl,
{ apply gl_uniq, intro i, refl },
{ symmetry, apply gl_uniq, intro i, rw ← h },
end
/--
In this version of the lemma, the inclusion homs `iUV` can be specified directly by the user,
which can be more convenient in practice.
-/
lemma eq_of_locally_eq' (V : opens X) (iUV : Π i : ι, U i ⟶ V) (hcover : V ≤ supr U)
(s t : F.1.obj (op V))
(h : ∀ i, F.1.map (iUV i).op s = F.1.map (iUV i).op t) : s = t :=
begin
have V_eq_supr_U : V = supr U := le_antisymm hcover (supr_le (λ i, (iUV i).le)),
suffices : F.1.map (eq_to_hom V_eq_supr_U.symm).op s =
F.1.map (eq_to_hom V_eq_supr_U.symm).op t,
{ convert congr_arg (F.1.map (eq_to_hom V_eq_supr_U).op) this ;
rw [← comp_apply, ← F.1.map_comp, eq_to_hom_op, eq_to_hom_op, eq_to_hom_trans,
eq_to_hom_refl, F.1.map_id, id_apply] },
apply eq_of_locally_eq,
intro i,
rw [← comp_apply, ← comp_apply, ← F.1.map_comp],
convert h i,
end
end
end sheaf
end Top
|
00a7e443b315140c609d5823dc3ec9ae5953f831 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/linear_algebra/linear_independent.lean | 7242efa4e225652e70ba877053e2b99573933114 | [
"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 | 57,634 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Alexander Bentkamp, Anne Baanen
-/
import algebra.big_operators.fin
import linear_algebra.finsupp
import linear_algebra.prod
import set_theory.cardinal.basic
/-!
# Linear independence
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines linear independence in a module or vector space.
It is inspired by Isabelle/HOL's linear algebra, and hence indirectly by HOL Light.
We define `linear_independent R v` as `ker (finsupp.total ι M R v) = ⊥`. Here `finsupp.total` is the
linear map sending a function `f : ι →₀ R` with finite support to the linear combination of vectors
from `v` with these coefficients. Then we prove that several other statements are equivalent to this
one, including injectivity of `finsupp.total ι M R v` and some versions with explicitly written
linear combinations.
## Main definitions
All definitions are given for families of vectors, i.e. `v : ι → M` where `M` is the module or
vector space and `ι : Type*` is an arbitrary indexing type.
* `linear_independent R v` states that the elements of the family `v` are linearly independent.
* `linear_independent.repr hv x` returns the linear combination representing `x : span R (range v)`
on the linearly independent vectors `v`, given `hv : linear_independent R v`
(using classical choice). `linear_independent.repr hv` is provided as a linear map.
## Main statements
We prove several specialized tests for linear independence of families of vectors and of sets of
vectors.
* `fintype.linear_independent_iff`: if `ι` is a finite type, then any function `f : ι → R` has
finite support, so we can reformulate the statement using `∑ i : ι, f i • v i` instead of a sum
over an auxiliary `s : finset ι`;
* `linear_independent_empty_type`: a family indexed by an empty type is linearly independent;
* `linear_independent_unique_iff`: if `ι` is a singleton, then `linear_independent K v` is
equivalent to `v default ≠ 0`;
* linear_independent_option`, `linear_independent_sum`, `linear_independent_fin_cons`,
`linear_independent_fin_succ`: type-specific tests for linear independence of families of vector
fields;
* `linear_independent_insert`, `linear_independent_union`, `linear_independent_pair`,
`linear_independent_singleton`: linear independence tests for set operations.
In many cases we additionally provide dot-style operations (e.g., `linear_independent.union`) to
make the linear independence tests usable as `hv.insert ha` etc.
We also prove that, when working over a division ring,
any family of vectors includes a linear independent subfamily spanning the same subspace.
## Implementation notes
We use families instead of sets because it allows us to say that two identical vectors are linearly
dependent.
If you want to use sets, use the family `(λ x, x : s → M)` given a set `s : set M`. The lemmas
`linear_independent.to_subtype_range` and `linear_independent.of_subtype_range` connect those two
worlds.
## Tags
linearly dependent, linear dependence, linearly independent, linear independence
-/
noncomputable theory
open function set submodule
open_locale classical big_operators cardinal
universes u
variables {ι : Type*} {ι' : Type*} {R : Type*} {K : Type*}
variables {M : Type*} {M' M'' : Type*} {V : Type u} {V' : Type*}
section module
variables {v : ι → M}
variables [semiring R] [add_comm_monoid M] [add_comm_monoid M'] [add_comm_monoid M'']
variables [module R M] [module R M'] [module R M'']
variables {a b : R} {x y : M}
variables (R) (v)
/-- `linear_independent R v` states the family of vectors `v` is linearly independent over `R`. -/
def linear_independent : Prop := (finsupp.total ι M R v).ker = ⊥
variables {R} {v}
theorem linear_independent_iff : linear_independent R v ↔
∀l, finsupp.total ι M R v l = 0 → l = 0 :=
by simp [linear_independent, linear_map.ker_eq_bot']
theorem linear_independent_iff' : linear_independent R v ↔
∀ s : finset ι, ∀ g : ι → R, ∑ i in s, g i • v i = 0 → ∀ i ∈ s, g i = 0 :=
linear_independent_iff.trans
⟨λ hf s g hg i his, have h : _ := hf (∑ i in s, finsupp.single i (g i)) $
by simpa only [linear_map.map_sum, finsupp.total_single] using hg, calc
g i = (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single i (g i)) :
by rw [finsupp.lapply_apply, finsupp.single_eq_same]
... = ∑ j in s, (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single j (g j)) :
eq.symm $ finset.sum_eq_single i
(λ j hjs hji, by rw [finsupp.lapply_apply, finsupp.single_eq_of_ne hji])
(λ hnis, hnis.elim his)
... = (∑ j in s, finsupp.single j (g j)) i : (finsupp.lapply i : (ι →₀ R) →ₗ[R] R).map_sum.symm
... = 0 : finsupp.ext_iff.1 h i,
λ hf l hl, finsupp.ext $ λ i, classical.by_contradiction $ λ hni, hni $ hf _ _ hl _ $
finsupp.mem_support_iff.2 hni⟩
theorem linear_independent_iff'' :
linear_independent R v ↔ ∀ (s : finset ι) (g : ι → R) (hg : ∀ i ∉ s, g i = 0),
∑ i in s, g i • v i = 0 → ∀ i, g i = 0 :=
linear_independent_iff'.trans ⟨λ H s g hg hv i, if his : i ∈ s then H s g hv i his else hg i his,
λ H s g hg i hi, by { convert H s (λ j, if j ∈ s then g j else 0) (λ j hj, if_neg hj)
(by simp_rw [ite_smul, zero_smul, finset.sum_extend_by_zero, hg]) i,
exact (if_pos hi).symm }⟩
theorem not_linear_independent_iff : ¬ linear_independent R v ↔
∃ s : finset ι, ∃ g : ι → R, (∑ i in s, g i • v i) = 0 ∧ (∃ i ∈ s, g i ≠ 0) :=
begin
rw linear_independent_iff',
simp only [exists_prop, not_forall],
end
theorem fintype.linear_independent_iff [fintype ι] :
linear_independent R v ↔ ∀ g : ι → R, ∑ i, g i • v i = 0 → ∀ i, g i = 0 :=
begin
refine ⟨λ H g, by simpa using linear_independent_iff'.1 H finset.univ g,
λ H, linear_independent_iff''.2 $ λ s g hg hs i, H _ _ _⟩,
rw ← hs,
refine (finset.sum_subset (finset.subset_univ _) (λ i _ hi, _)).symm,
rw [hg i hi, zero_smul]
end
/-- A finite family of vectors `v i` is linear independent iff the linear map that sends
`c : ι → R` to `∑ i, c i • v i` has the trivial kernel. -/
theorem fintype.linear_independent_iff' [fintype ι] :
linear_independent R v ↔
(linear_map.lsum R (λ i : ι, R) ℕ (λ i, linear_map.id.smul_right (v i))).ker = ⊥ :=
by simp [fintype.linear_independent_iff, linear_map.ker_eq_bot', funext_iff]; skip
lemma fintype.not_linear_independent_iff [fintype ι] :
¬linear_independent R v ↔ ∃ g : ι → R, (∑ i, g i • v i) = 0 ∧ (∃ i, g i ≠ 0) :=
by simpa using (not_iff_not.2 fintype.linear_independent_iff)
lemma linear_independent_empty_type [is_empty ι] : linear_independent R v :=
linear_independent_iff.mpr $ λ v hv, subsingleton.elim v 0
lemma linear_independent.ne_zero [nontrivial R]
(i : ι) (hv : linear_independent R v) : v i ≠ 0 :=
λ h, zero_ne_one' R $ eq.symm begin
suffices : (finsupp.single i 1 : ι →₀ R) i = 0, {simpa},
rw linear_independent_iff.1 hv (finsupp.single i 1),
{ simp },
{ simp [h] }
end
/-- A subfamily of a linearly independent family (i.e., a composition with an injective map) is a
linearly independent family. -/
lemma linear_independent.comp
(h : linear_independent R v) (f : ι' → ι) (hf : injective f) : linear_independent R (v ∘ f) :=
begin
rw [linear_independent_iff, finsupp.total_comp],
intros l hl,
have h_map_domain : ∀ x, (finsupp.map_domain f l) (f x) = 0,
by rw linear_independent_iff.1 h (finsupp.map_domain f l) hl; simp,
ext x,
convert h_map_domain x,
rw [finsupp.map_domain_apply hf]
end
lemma linear_independent.coe_range (i : linear_independent R v) :
linear_independent R (coe : range v → M) :=
by simpa using i.comp _ (range_splitting_injective v)
/-- If `v` is a linearly independent family of vectors and the kernel of a linear map `f` is
disjoint with the submodule spanned by the vectors of `v`, then `f ∘ v` is a linearly independent
family of vectors. See also `linear_independent.map'` for a special case assuming `ker f = ⊥`. -/
lemma linear_independent.map (hv : linear_independent R v) {f : M →ₗ[R] M'}
(hf_inj : disjoint (span R (range v)) f.ker) : linear_independent R (f ∘ v) :=
begin
rw [disjoint_iff_inf_le, ← set.image_univ, finsupp.span_image_eq_map_total,
map_inf_eq_map_inf_comap,
map_le_iff_le_comap, comap_bot, finsupp.supported_univ, top_inf_eq] at hf_inj,
unfold linear_independent at hv ⊢,
rw [hv, le_bot_iff] at hf_inj,
haveI : inhabited M := ⟨0⟩,
rw [finsupp.total_comp, @finsupp.lmap_domain_total _ _ R _ _ _ _ _ _ _ _ _ _ f,
linear_map.ker_comp, hf_inj],
exact λ _, rfl,
end
/-- An injective linear map sends linearly independent families of vectors to linearly independent
families of vectors. See also `linear_independent.map` for a more general statement. -/
lemma linear_independent.map' (hv : linear_independent R v) (f : M →ₗ[R] M')
(hf_inj : f.ker = ⊥) : linear_independent R (f ∘ v) :=
hv.map $ by simp [hf_inj]
/-- If the image of a family of vectors under a linear map is linearly independent, then so is
the original family. -/
lemma linear_independent.of_comp (f : M →ₗ[R] M') (hfv : linear_independent R (f ∘ v)) :
linear_independent R v :=
linear_independent_iff'.2 $ λ s g hg i his,
have ∑ (i : ι) in s, g i • f (v i) = 0,
by simp_rw [← f.map_smul, ← f.map_sum, hg, f.map_zero],
linear_independent_iff'.1 hfv s g this i his
/-- If `f` is an injective linear map, then the family `f ∘ v` is linearly independent
if and only if the family `v` is linearly independent. -/
protected lemma linear_map.linear_independent_iff (f : M →ₗ[R] M') (hf_inj : f.ker = ⊥) :
linear_independent R (f ∘ v) ↔ linear_independent R v :=
⟨λ h, h.of_comp f, λ h, h.map $ by simp only [hf_inj, disjoint_bot_right]⟩
@[nontriviality]
lemma linear_independent_of_subsingleton [subsingleton R] : linear_independent R v :=
linear_independent_iff.2 (λ l hl, subsingleton.elim _ _)
theorem linear_independent_equiv (e : ι ≃ ι') {f : ι' → M} :
linear_independent R (f ∘ e) ↔ linear_independent R f :=
⟨λ h, function.comp.right_id f ▸ e.self_comp_symm ▸ h.comp _ e.symm.injective,
λ h, h.comp _ e.injective⟩
theorem linear_independent_equiv' (e : ι ≃ ι') {f : ι' → M} {g : ι → M} (h : f ∘ e = g) :
linear_independent R g ↔ linear_independent R f :=
h ▸ linear_independent_equiv e
theorem linear_independent_subtype_range {ι} {f : ι → M} (hf : injective f) :
linear_independent R (coe : range f → M) ↔ linear_independent R f :=
iff.symm $ linear_independent_equiv' (equiv.of_injective f hf) rfl
alias linear_independent_subtype_range ↔ linear_independent.of_subtype_range _
theorem linear_independent_image {ι} {s : set ι} {f : ι → M} (hf : set.inj_on f s) :
linear_independent R (λ x : s, f x) ↔ linear_independent R (λ x : f '' s, (x : M)) :=
linear_independent_equiv' (equiv.set.image_of_inj_on _ _ hf) rfl
lemma linear_independent_span (hs : linear_independent R v) :
@linear_independent ι R (span R (range v))
(λ i : ι, ⟨v i, subset_span (mem_range_self i)⟩) _ _ _ :=
linear_independent.of_comp (span R (range v)).subtype hs
/-- See `linear_independent.fin_cons` for a family of elements in a vector space. -/
lemma linear_independent.fin_cons' {m : ℕ} (x : M) (v : fin m → M)
(hli : linear_independent R v)
(x_ortho : (∀ (c : R) (y : submodule.span R (set.range v)), c • x + y = (0 : M) → c = 0)) :
linear_independent R (fin.cons x v : fin m.succ → M) :=
begin
rw fintype.linear_independent_iff at hli ⊢,
rintros g total_eq j,
simp_rw [fin.sum_univ_succ, fin.cons_zero, fin.cons_succ] at total_eq,
have : g 0 = 0,
{ refine x_ortho (g 0) ⟨∑ (i : fin m), g i.succ • v i, _⟩ total_eq,
exact sum_mem (λ i _, smul_mem _ _ (subset_span ⟨i, rfl⟩)) },
rw [this, zero_smul, zero_add] at total_eq,
exact fin.cases this (hli _ total_eq) j,
end
/-- A set of linearly independent vectors in a module `M` over a semiring `K` is also linearly
independent over a subring `R` of `K`.
The implementation uses minimal assumptions about the relationship between `R`, `K` and `M`.
The version where `K` is an `R`-algebra is `linear_independent.restrict_scalars_algebras`.
-/
lemma linear_independent.restrict_scalars [semiring K] [smul_with_zero R K] [module K M]
[is_scalar_tower R K M]
(hinj : function.injective (λ r : R, r • (1 : K))) (li : linear_independent K v) :
linear_independent R v :=
begin
refine linear_independent_iff'.mpr (λ s g hg i hi, hinj (eq.trans _ (zero_smul _ _).symm)),
refine (linear_independent_iff'.mp li : _) _ _ _ i hi,
simp_rw [smul_assoc, one_smul],
exact hg,
end
/-- Every finite subset of a linearly independent set is linearly independent. -/
lemma linear_independent_finset_map_embedding_subtype
(s : set M) (li : linear_independent R (coe : s → M)) (t : finset s) :
linear_independent R (coe : (finset.map (embedding.subtype s) t) → M) :=
begin
let f : t.map (embedding.subtype s) → s := λ x, ⟨x.1, begin
obtain ⟨x, h⟩ := x,
rw [finset.mem_map] at h,
obtain ⟨a, ha, rfl⟩ := h,
simp only [subtype.coe_prop, embedding.coe_subtype],
end⟩,
convert linear_independent.comp li f _,
rintros ⟨x, hx⟩ ⟨y, hy⟩,
rw [finset.mem_map] at hx hy,
obtain ⟨a, ha, rfl⟩ := hx,
obtain ⟨b, hb, rfl⟩ := hy,
simp only [imp_self, subtype.mk_eq_mk],
end
/--
If every finite set of linearly independent vectors has cardinality at most `n`,
then the same is true for arbitrary sets of linearly independent vectors.
-/
lemma linear_independent_bounded_of_finset_linear_independent_bounded {n : ℕ}
(H : ∀ s : finset M, linear_independent R (λ i : s, (i : M)) → s.card ≤ n) :
∀ s : set M, linear_independent R (coe : s → M) → #s ≤ n :=
begin
intros s li,
apply cardinal.card_le_of,
intro t,
rw ← finset.card_map (embedding.subtype s),
apply H,
apply linear_independent_finset_map_embedding_subtype _ li,
end
section subtype
/-! The following lemmas use the subtype defined by a set in `M` as the index set `ι`. -/
theorem linear_independent_comp_subtype {s : set ι} :
linear_independent R (v ∘ coe : s → M) ↔
∀ l ∈ (finsupp.supported R R s), (finsupp.total ι M R v) l = 0 → l = 0 :=
begin
simp only [linear_independent_iff, (∘), finsupp.mem_supported, finsupp.total_apply,
set.subset_def, finset.mem_coe],
split,
{ intros h l hl₁ hl₂,
have := h (l.subtype_domain s) ((finsupp.sum_subtype_domain_index hl₁).trans hl₂),
exact (finsupp.subtype_domain_eq_zero_iff hl₁).1 this },
{ intros h l hl,
refine finsupp.emb_domain_eq_zero.1 (h (l.emb_domain $ function.embedding.subtype s) _ _),
{ suffices : ∀ i hi, ¬l ⟨i, hi⟩ = 0 → i ∈ s, by simpa,
intros, assumption },
{ rwa [finsupp.emb_domain_eq_map_domain, finsupp.sum_map_domain_index],
exacts [λ _, zero_smul _ _, λ _ _ _, add_smul _ _ _] } }
end
lemma linear_dependent_comp_subtype' {s : set ι} :
¬ linear_independent R (v ∘ coe : s → M) ↔
∃ f : ι →₀ R, f ∈ finsupp.supported R R s ∧ finsupp.total ι M R v f = 0 ∧ f ≠ 0 :=
by simp [linear_independent_comp_subtype]
/-- A version of `linear_dependent_comp_subtype'` with `finsupp.total` unfolded. -/
lemma linear_dependent_comp_subtype {s : set ι} :
¬ linear_independent R (v ∘ coe : s → M) ↔
∃ f : ι →₀ R, f ∈ finsupp.supported R R s ∧ ∑ i in f.support, f i • v i = 0 ∧ f ≠ 0 :=
linear_dependent_comp_subtype'
theorem linear_independent_subtype {s : set M} :
linear_independent R (λ x, x : s → M) ↔
∀ l ∈ (finsupp.supported R R s), (finsupp.total M M R id) l = 0 → l = 0 :=
by apply @linear_independent_comp_subtype _ _ _ id
theorem linear_independent_comp_subtype_disjoint {s : set ι} :
linear_independent R (v ∘ coe : s → M) ↔
disjoint (finsupp.supported R R s) (finsupp.total ι M R v).ker :=
by rw [linear_independent_comp_subtype, linear_map.disjoint_ker]
theorem linear_independent_subtype_disjoint {s : set M} :
linear_independent R (λ x, x : s → M) ↔
disjoint (finsupp.supported R R s) (finsupp.total M M R id).ker :=
by apply @linear_independent_comp_subtype_disjoint _ _ _ id
theorem linear_independent_iff_total_on {s : set M} :
linear_independent R (λ x, x : s → M) ↔ (finsupp.total_on M M R id s).ker = ⊥ :=
by rw [finsupp.total_on, linear_map.ker, linear_map.comap_cod_restrict, submodule.map_bot,
comap_bot, linear_map.ker_comp, linear_independent_subtype_disjoint, disjoint_iff_inf_le,
← map_comap_subtype, map_le_iff_le_comap, comap_bot, ker_subtype, le_bot_iff]
lemma linear_independent.restrict_of_comp_subtype {s : set ι}
(hs : linear_independent R (v ∘ coe : s → M)) :
linear_independent R (s.restrict v) :=
hs
variables (R M)
lemma linear_independent_empty : linear_independent R (λ x, x : (∅ : set M) → M) :=
by simp [linear_independent_subtype_disjoint]
variables {R M}
lemma linear_independent.mono {t s : set M} (h : t ⊆ s) :
linear_independent R (λ x, x : s → M) → linear_independent R (λ x, x : t → M) :=
begin
simp only [linear_independent_subtype_disjoint],
exact (disjoint.mono_left (finsupp.supported_mono h))
end
lemma linear_independent_of_finite (s : set M)
(H : ∀ t ⊆ s, set.finite t → linear_independent R (λ x, x : t → M)) :
linear_independent R (λ x, x : s → M) :=
linear_independent_subtype.2 $
λ l hl, linear_independent_subtype.1 (H _ hl (finset.finite_to_set _)) l (subset.refl _)
lemma linear_independent_Union_of_directed {η : Type*}
{s : η → set M} (hs : directed (⊆) s)
(h : ∀ i, linear_independent R (λ x, x : s i → M)) :
linear_independent R (λ x, x : (⋃ i, s i) → M) :=
begin
by_cases hη : nonempty η,
{ resetI,
refine linear_independent_of_finite (⋃ i, s i) (λ t ht ft, _),
rcases finite_subset_Union ft ht with ⟨I, fi, hI⟩,
rcases hs.finset_le fi.to_finset with ⟨i, hi⟩,
exact (h i).mono (subset.trans hI $ Union₂_subset $
λ j hj, hi j (fi.mem_to_finset.2 hj)) },
{ refine (linear_independent_empty _ _).mono _,
rintro _ ⟨_, ⟨i, _⟩, _⟩, exact hη ⟨i⟩ }
end
lemma linear_independent_sUnion_of_directed {s : set (set M)}
(hs : directed_on (⊆) s)
(h : ∀ a ∈ s, linear_independent R (λ x, x : (a : set M) → M)) :
linear_independent R (λ x, x : (⋃₀ s) → M) :=
by rw sUnion_eq_Union; exact
linear_independent_Union_of_directed hs.directed_coe (by simpa using h)
lemma linear_independent_bUnion_of_directed {η} {s : set η} {t : η → set M}
(hs : directed_on (t ⁻¹'o (⊆)) s) (h : ∀a∈s, linear_independent R (λ x, x : t a → M)) :
linear_independent R (λ x, x : (⋃a∈s, t a) → M) :=
by rw bUnion_eq_Union; exact
linear_independent_Union_of_directed (directed_comp.2 $ hs.directed_coe) (by simpa using h)
end subtype
end module
/-! ### Properties which require `ring R` -/
section module
variables {v : ι → M}
variables [ring R] [add_comm_group M] [add_comm_group M'] [add_comm_group M'']
variables [module R M] [module R M'] [module R M'']
variables {a b : R} {x y : M}
theorem linear_independent_iff_injective_total : linear_independent R v ↔
function.injective (finsupp.total ι M R v) :=
linear_independent_iff.trans
(injective_iff_map_eq_zero (finsupp.total ι M R v).to_add_monoid_hom).symm
alias linear_independent_iff_injective_total ↔ linear_independent.injective_total _
lemma linear_independent.injective [nontrivial R] (hv : linear_independent R v) :
injective v :=
begin
intros i j hij,
let l : ι →₀ R := finsupp.single i (1 : R) - finsupp.single j 1,
have h_total : finsupp.total ι M R v l = 0,
{ simp_rw [linear_map.map_sub, finsupp.total_apply],
simp [hij] },
have h_single_eq : finsupp.single i (1 : R) = finsupp.single j 1,
{ rw linear_independent_iff at hv,
simp [eq_add_of_sub_eq' (hv l h_total)] },
simpa [finsupp.single_eq_single_iff] using h_single_eq
end
theorem linear_independent.to_subtype_range {ι} {f : ι → M} (hf : linear_independent R f) :
linear_independent R (coe : range f → M) :=
begin
nontriviality R,
exact (linear_independent_subtype_range hf.injective).2 hf
end
theorem linear_independent.to_subtype_range' {ι} {f : ι → M} (hf : linear_independent R f)
{t} (ht : range f = t) :
linear_independent R (coe : t → M) :=
ht ▸ hf.to_subtype_range
theorem linear_independent.image_of_comp {ι ι'} (s : set ι) (f : ι → ι') (g : ι' → M)
(hs : linear_independent R (λ x : s, g (f x))) :
linear_independent R (λ x : f '' s, g x) :=
begin
nontriviality R,
have : inj_on f s, from inj_on_iff_injective.2 hs.injective.of_comp,
exact (linear_independent_equiv' (equiv.set.image_of_inj_on f s this) rfl).1 hs
end
theorem linear_independent.image {ι} {s : set ι} {f : ι → M}
(hs : linear_independent R (λ x : s, f x)) : linear_independent R (λ x : f '' s, (x : M)) :=
by convert linear_independent.image_of_comp s f id hs
lemma linear_independent.group_smul
{G : Type*} [hG : group G] [distrib_mul_action G R] [distrib_mul_action G M]
[is_scalar_tower G R M] [smul_comm_class G R M] {v : ι → M} (hv : linear_independent R v)
(w : ι → G) : linear_independent R (w • v) :=
begin
rw linear_independent_iff'' at hv ⊢,
intros s g hgs hsum i,
refine (smul_eq_zero_iff_eq (w i)).1 _,
refine hv s (λ i, w i • g i) (λ i hi, _) _ i,
{ dsimp only,
exact (hgs i hi).symm ▸ smul_zero _ },
{ rw [← hsum, finset.sum_congr rfl _],
intros, erw [pi.smul_apply, smul_assoc, smul_comm] },
end
-- This lemma cannot be proved with `linear_independent.group_smul` since the action of
-- `Rˣ` on `R` is not commutative.
lemma linear_independent.units_smul {v : ι → M} (hv : linear_independent R v)
(w : ι → Rˣ) : linear_independent R (w • v) :=
begin
rw linear_independent_iff'' at hv ⊢,
intros s g hgs hsum i,
rw ← (w i).mul_left_eq_zero,
refine hv s (λ i, g i • w i) (λ i hi, _) _ i,
{ dsimp only,
exact (hgs i hi).symm ▸ zero_smul _ _ },
{ rw [← hsum, finset.sum_congr rfl _],
intros,
erw [pi.smul_apply, smul_assoc],
refl }
end
section maximal
universes v w
/--
A linearly independent family is maximal if there is no strictly larger linearly independent family.
-/
@[nolint unused_arguments]
def linear_independent.maximal {ι : Type w} {R : Type u} [semiring R]
{M : Type v} [add_comm_monoid M] [module R M] {v : ι → M} (i : linear_independent R v) : Prop :=
∀ (s : set M) (i' : linear_independent R (coe : s → M)) (h : range v ≤ s), range v = s
/--
An alternative characterization of a maximal linearly independent family,
quantifying over types (in the same universe as `M`) into which the indexing family injects.
-/
lemma linear_independent.maximal_iff {ι : Type w} {R : Type u} [ring R] [nontrivial R]
{M : Type v} [add_comm_group M] [module R M] {v : ι → M} (i : linear_independent R v) :
i.maximal ↔ ∀ (κ : Type v) (w : κ → M) (i' : linear_independent R w)
(j : ι → κ) (h : w ∘ j = v), surjective j :=
begin
fsplit,
{ rintros p κ w i' j rfl,
specialize p (range w) i'.coe_range (range_comp_subset_range _ _),
rw [range_comp, ←@image_univ _ _ w] at p,
exact range_iff_surjective.mp (image_injective.mpr i'.injective p), },
{ intros p w i' h,
specialize p w (coe : w → M) i'
(λ i, ⟨v i, range_subset_iff.mp h i⟩)
(by { ext, simp, }),
have q := congr_arg (λ s, (coe : w → M) '' s) p.range_eq,
dsimp at q,
rw [←image_univ, image_image] at q,
simpa using q, },
end
end maximal
/-- Linear independent families are injective, even if you multiply either side. -/
lemma linear_independent.eq_of_smul_apply_eq_smul_apply {M : Type*} [add_comm_group M] [module R M]
{v : ι → M} (li : linear_independent R v) (c d : R) (i j : ι)
(hc : c ≠ 0) (h : c • v i = d • v j) : i = j :=
begin
let l : ι →₀ R := finsupp.single i c - finsupp.single j d,
have h_total : finsupp.total ι M R v l = 0,
{ simp_rw [linear_map.map_sub, finsupp.total_apply],
simp [h] },
have h_single_eq : finsupp.single i c = finsupp.single j d,
{ rw linear_independent_iff at li,
simp [eq_add_of_sub_eq' (li l h_total)] },
rcases (finsupp.single_eq_single_iff _ _ _ _).mp h_single_eq with ⟨this, _⟩ | ⟨hc, _⟩,
{ exact this },
{ contradiction },
end
section subtype
/-! The following lemmas use the subtype defined by a set in `M` as the index set `ι`. -/
lemma linear_independent.disjoint_span_image (hv : linear_independent R v) {s t : set ι}
(hs : disjoint s t) :
disjoint (submodule.span R $ v '' s) (submodule.span R $ v '' t) :=
begin
simp only [disjoint_def, finsupp.mem_span_image_iff_total],
rintros _ ⟨l₁, hl₁, rfl⟩ ⟨l₂, hl₂, H⟩,
rw [hv.injective_total.eq_iff] at H, subst l₂,
have : l₁ = 0 :=
submodule.disjoint_def.mp (finsupp.disjoint_supported_supported hs) _ hl₁ hl₂,
simp [this]
end
lemma linear_independent.not_mem_span_image [nontrivial R] (hv : linear_independent R v) {s : set ι}
{x : ι} (h : x ∉ s) :
v x ∉ submodule.span R (v '' s) :=
begin
have h' : v x ∈ submodule.span R (v '' {x}),
{ rw set.image_singleton,
exact mem_span_singleton_self (v x), },
intro w,
apply linear_independent.ne_zero x hv,
refine disjoint_def.1 (hv.disjoint_span_image _) (v x) h' w,
simpa using h,
end
lemma linear_independent.total_ne_of_not_mem_support [nontrivial R] (hv : linear_independent R v)
{x : ι} (f : ι →₀ R) (h : x ∉ f.support) :
finsupp.total ι M R v f ≠ v x :=
begin
replace h : x ∉ (f.support : set ι) := h,
have p := hv.not_mem_span_image h,
intro w,
rw ←w at p,
rw finsupp.span_image_eq_map_total at p,
simp only [not_exists, not_and, mem_map] at p,
exact p f (f.mem_supported_support R) rfl,
end
lemma linear_independent_sum {v : ι ⊕ ι' → M} :
linear_independent R v ↔ linear_independent R (v ∘ sum.inl) ∧
linear_independent R (v ∘ sum.inr) ∧
disjoint (submodule.span R (range (v ∘ sum.inl))) (submodule.span R (range (v ∘ sum.inr))) :=
begin
rw [range_comp v, range_comp v],
refine ⟨λ h, ⟨h.comp _ sum.inl_injective, h.comp _ sum.inr_injective,
h.disjoint_span_image is_compl_range_inl_range_inr.1⟩, _⟩,
rintro ⟨hl, hr, hlr⟩,
rw [linear_independent_iff'] at *,
intros s g hg i hi,
have : ∑ i in s.preimage sum.inl (sum.inl_injective.inj_on _), (λ x, g x • v x) (sum.inl i) +
∑ i in s.preimage sum.inr (sum.inr_injective.inj_on _), (λ x, g x • v x) (sum.inr i) = 0,
{ rw [finset.sum_preimage', finset.sum_preimage', ← finset.sum_union, ← finset.filter_or],
{ simpa only [← mem_union, range_inl_union_range_inr, mem_univ, finset.filter_true] },
{ exact finset.disjoint_filter.2
(λ x _ hx, disjoint_left.1 is_compl_range_inl_range_inr.1 hx) } },
{ rw ← eq_neg_iff_add_eq_zero at this,
rw [disjoint_def'] at hlr,
have A := hlr _ (sum_mem $ λ i hi, _) _ (neg_mem $ sum_mem $ λ i hi, _) this,
{ cases i with i i,
{ exact hl _ _ A i (finset.mem_preimage.2 hi) },
{ rw [this, neg_eq_zero] at A,
exact hr _ _ A i (finset.mem_preimage.2 hi) } },
{ exact smul_mem _ _ (subset_span ⟨sum.inl i, mem_range_self _, rfl⟩) },
{ exact smul_mem _ _ (subset_span ⟨sum.inr i, mem_range_self _, rfl⟩) } }
end
lemma linear_independent.sum_type {v' : ι' → M} (hv : linear_independent R v)
(hv' : linear_independent R v')
(h : disjoint (submodule.span R (range v)) (submodule.span R (range v'))) :
linear_independent R (sum.elim v v') :=
linear_independent_sum.2 ⟨hv, hv', h⟩
lemma linear_independent.union {s t : set M}
(hs : linear_independent R (λ x, x : s → M)) (ht : linear_independent R (λ x, x : t → M))
(hst : disjoint (span R s) (span R t)) :
linear_independent R (λ x, x : (s ∪ t) → M) :=
(hs.sum_type ht $ by simpa).to_subtype_range' $ by simp
lemma linear_independent_Union_finite_subtype {ι : Type*} {f : ι → set M}
(hl : ∀i, linear_independent R (λ x, x : f i → M))
(hd : ∀i, ∀t:set ι, t.finite → i ∉ t → disjoint (span R (f i)) (⨆i∈t, span R (f i))) :
linear_independent R (λ x, x : (⋃i, f i) → M) :=
begin
rw [Union_eq_Union_finset f],
apply linear_independent_Union_of_directed,
{ apply directed_of_sup,
exact (λ t₁ t₂ ht, Union_mono $ λ i, Union_subset_Union_const $ λ h, ht h) },
assume t,
induction t using finset.induction_on with i s his ih,
{ refine (linear_independent_empty _ _).mono _,
simp },
{ rw [finset.set_bUnion_insert],
refine (hl _).union ih _,
rw span_Union₂,
exact hd i s s.finite_to_set his }
end
lemma linear_independent_Union_finite {η : Type*} {ιs : η → Type*}
{f : Π j : η, ιs j → M}
(hindep : ∀j, linear_independent R (f j))
(hd : ∀i, ∀t:set η, t.finite → i ∉ t →
disjoint (span R (range (f i))) (⨆i∈t, span R (range (f i)))) :
linear_independent R (λ ji : Σ j, ιs j, f ji.1 ji.2) :=
begin
nontriviality R,
apply linear_independent.of_subtype_range,
{ rintros ⟨x₁, x₂⟩ ⟨y₁, y₂⟩ hxy,
by_cases h_cases : x₁ = y₁,
subst h_cases,
{ apply sigma.eq,
rw linear_independent.injective (hindep _) hxy,
refl },
{ have h0 : f x₁ x₂ = 0,
{ apply disjoint_def.1 (hd x₁ {y₁} (finite_singleton y₁)
(λ h, h_cases (eq_of_mem_singleton h))) (f x₁ x₂) (subset_span (mem_range_self _)),
rw supr_singleton,
simp only at hxy,
rw hxy,
exact (subset_span (mem_range_self y₂)) },
exact false.elim ((hindep x₁).ne_zero _ h0) } },
rw range_sigma_eq_Union_range,
apply linear_independent_Union_finite_subtype (λ j, (hindep j).to_subtype_range) hd,
end
end subtype
section repr
variables (hv : linear_independent R v)
/-- Canonical isomorphism between linear combinations and the span of linearly independent vectors.
-/
@[simps {rhs_md := semireducible}]
def linear_independent.total_equiv (hv : linear_independent R v) :
(ι →₀ R) ≃ₗ[R] span R (range v) :=
begin
apply linear_equiv.of_bijective
(linear_map.cod_restrict (span R (range v)) (finsupp.total ι M R v) _),
split,
{ rw [← linear_map.ker_eq_bot, linear_map.ker_cod_restrict],
apply hv },
{ rw [← linear_map.range_eq_top, linear_map.range_eq_map, linear_map.map_cod_restrict,
← linear_map.range_le_iff_comap, range_subtype, submodule.map_top],
rw finsupp.range_total,
exact le_rfl },
{ intro l,
rw ← finsupp.range_total,
rw linear_map.mem_range,
apply mem_range_self l }
end
/-- Linear combination representing a vector in the span of linearly independent vectors.
Given a family of linearly independent vectors, we can represent any vector in their span as
a linear combination of these vectors. These are provided by this linear map.
It is simply one direction of `linear_independent.total_equiv`. -/
def linear_independent.repr (hv : linear_independent R v) :
span R (range v) →ₗ[R] ι →₀ R := hv.total_equiv.symm
@[simp] lemma linear_independent.total_repr (x) : finsupp.total ι M R v (hv.repr x) = x :=
subtype.ext_iff.1 (linear_equiv.apply_symm_apply hv.total_equiv x)
lemma linear_independent.total_comp_repr :
(finsupp.total ι M R v).comp hv.repr = submodule.subtype _ :=
linear_map.ext $ hv.total_repr
lemma linear_independent.repr_ker : hv.repr.ker = ⊥ :=
by rw [linear_independent.repr, linear_equiv.ker]
lemma linear_independent.repr_range : hv.repr.range = ⊤ :=
by rw [linear_independent.repr, linear_equiv.range]
lemma linear_independent.repr_eq
{l : ι →₀ R} {x} (eq : finsupp.total ι M R v l = ↑x) :
hv.repr x = l :=
begin
have : ↑((linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l)
= finsupp.total ι M R v l := rfl,
have : (linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l = x,
{ rw eq at this,
exact subtype.ext_iff.2 this },
rw ←linear_equiv.symm_apply_apply hv.total_equiv l,
rw ←this,
refl,
end
lemma linear_independent.repr_eq_single (i) (x) (hx : ↑x = v i) :
hv.repr x = finsupp.single i 1 :=
begin
apply hv.repr_eq,
simp [finsupp.total_single, hx]
end
lemma linear_independent.span_repr_eq [nontrivial R] (x) :
span.repr R (set.range v) x = (hv.repr x).equiv_map_domain (equiv.of_injective _ hv.injective) :=
begin
have p : (span.repr R (set.range v) x).equiv_map_domain (equiv.of_injective _ hv.injective).symm =
hv.repr x,
{ apply (linear_independent.total_equiv hv).injective,
ext,
simp only [linear_independent.total_equiv_apply_coe, equiv.self_comp_of_injective_symm,
linear_independent.total_repr, finsupp.total_equiv_map_domain, span.finsupp_total_repr], },
ext ⟨_, ⟨i, rfl⟩⟩,
simp [←p],
end
-- TODO: why is this so slow?
lemma linear_independent_iff_not_smul_mem_span :
linear_independent R v ↔ (∀ (i : ι) (a : R), a • (v i) ∈ span R (v '' (univ \ {i})) → a = 0) :=
⟨ λ hv i a ha, begin
rw [finsupp.span_image_eq_map_total, mem_map] at ha,
rcases ha with ⟨l, hl, e⟩,
rw sub_eq_zero.1 (linear_independent_iff.1 hv (l - finsupp.single i a) (by simp [e])) at hl,
by_contra hn,
exact (not_mem_of_mem_diff (hl $ by simp [hn])) (mem_singleton _),
end, λ H, linear_independent_iff.2 $ λ l hl, begin
ext i, simp only [finsupp.zero_apply],
by_contra hn,
refine hn (H i _ _),
refine (finsupp.mem_span_image_iff_total _).2 ⟨finsupp.single i (l i) - l, _, _⟩,
{ rw finsupp.mem_supported',
intros j hj,
have hij : j = i :=
not_not.1
(λ hij : j ≠ i, hj ((mem_diff _).2 ⟨mem_univ _, λ h, hij (eq_of_mem_singleton h)⟩)),
simp [hij] },
{ simp [hl] }
end⟩
/-- See also `complete_lattice.independent_iff_linear_independent_of_ne_zero`. -/
lemma linear_independent.independent_span_singleton (hv : linear_independent R v) :
complete_lattice.independent $ λ i, R ∙ v i :=
begin
refine complete_lattice.independent_def.mp (λ i, _),
rw disjoint_iff_inf_le,
intros m hm,
simp only [mem_inf, mem_span_singleton, supr_subtype', ← span_range_eq_supr] at hm,
obtain ⟨⟨r, rfl⟩, hm⟩ := hm,
suffices : r = 0, { simp [this], },
apply linear_independent_iff_not_smul_mem_span.mp hv i,
convert hm,
ext,
simp,
end
variable (R)
lemma exists_maximal_independent' (s : ι → M) :
∃ I : set ι, linear_independent R (λ x : I, s x) ∧
∀ J : set ι, I ⊆ J → linear_independent R (λ x : J, s x) → I = J :=
begin
let indep : set ι → Prop := λ I, linear_independent R (s ∘ coe : I → M),
let X := { I : set ι // indep I },
let r : X → X → Prop := λ I J, I.1 ⊆ J.1,
have key : ∀ c : set X, is_chain r c → indep (⋃ (I : X) (H : I ∈ c), I),
{ intros c hc,
dsimp [indep],
rw [linear_independent_comp_subtype],
intros f hsupport hsum,
rcases eq_empty_or_nonempty c with rfl | hn,
{ simpa using hsupport },
haveI : is_refl X r := ⟨λ _, set.subset.refl _⟩,
obtain ⟨I, I_mem, hI⟩ : ∃ I ∈ c, (f.support : set ι) ⊆ I :=
hc.directed_on.exists_mem_subset_of_finset_subset_bUnion hn hsupport,
exact linear_independent_comp_subtype.mp I.2 f hI hsum },
have trans : transitive r := λ I J K, set.subset.trans,
obtain ⟨⟨I, hli : indep I⟩, hmax : ∀ a, r ⟨I, hli⟩ a → r a ⟨I, hli⟩⟩ :=
@exists_maximal_of_chains_bounded _ r
(λ c hc, ⟨⟨⋃ I ∈ c, (I : set ι), key c hc⟩, λ I, set.subset_bUnion_of_mem⟩) trans,
exact ⟨I, hli, λ J hsub hli, set.subset.antisymm hsub (hmax ⟨J, hli⟩ hsub)⟩,
end
lemma exists_maximal_independent (s : ι → M) : ∃ I : set ι, linear_independent R (λ x : I, s x) ∧
∀ i ∉ I, ∃ a : R, a ≠ 0 ∧ a • s i ∈ span R (s '' I) :=
begin
classical,
rcases exists_maximal_independent' R s with ⟨I, hIlinind, hImaximal⟩,
use [I, hIlinind],
intros i hi,
specialize hImaximal (I ∪ {i}) (by simp),
set J := I ∪ {i} with hJ,
have memJ : ∀ {x}, x ∈ J ↔ x = i ∨ x ∈ I, by simp [hJ],
have hiJ : i ∈ J := by simp,
have h := mt hImaximal _, swap,
{ intro h2,
rw h2 at hi,
exact absurd hiJ hi },
obtain ⟨f, supp_f, sum_f, f_ne⟩ := linear_dependent_comp_subtype.mp h,
have hfi : f i ≠ 0,
{ contrapose hIlinind,
refine linear_dependent_comp_subtype.mpr ⟨f, _, sum_f, f_ne⟩,
simp only [finsupp.mem_supported, hJ] at ⊢ supp_f,
rintro x hx,
refine (memJ.mp (supp_f hx)).resolve_left _,
rintro rfl,
exact hIlinind (finsupp.mem_support_iff.mp hx) },
use [f i, hfi],
have hfi' : i ∈ f.support := finsupp.mem_support_iff.mpr hfi,
rw [← finset.insert_erase hfi', finset.sum_insert (finset.not_mem_erase _ _),
add_eq_zero_iff_eq_neg] at sum_f,
rw sum_f,
refine neg_mem (sum_mem (λ c hc, smul_mem _ _ (subset_span ⟨c, _, rfl⟩))),
exact (memJ.mp (supp_f (finset.erase_subset _ _ hc))).resolve_left (finset.ne_of_mem_erase hc),
end
end repr
lemma surjective_of_linear_independent_of_span [nontrivial R]
(hv : linear_independent R v) (f : ι' ↪ ι)
(hss : range v ⊆ span R (range (v ∘ f))) :
surjective f :=
begin
intros i,
let repr : (span R (range (v ∘ f)) : Type*) → ι' →₀ R := (hv.comp f f.injective).repr,
let l := (repr ⟨v i, hss (mem_range_self i)⟩).map_domain f,
have h_total_l : finsupp.total ι M R v l = v i,
{ dsimp only [l],
rw finsupp.total_map_domain,
rw (hv.comp f f.injective).total_repr,
{ refl } },
have h_total_eq : (finsupp.total ι M R v) l = (finsupp.total ι M R v) (finsupp.single i 1),
by rw [h_total_l, finsupp.total_single, one_smul],
have l_eq : l = _ := linear_map.ker_eq_bot.1 hv h_total_eq,
dsimp only [l] at l_eq,
rw ←finsupp.emb_domain_eq_map_domain at l_eq,
rcases finsupp.single_of_emb_domain_single (repr ⟨v i, _⟩) f i (1 : R) zero_ne_one.symm l_eq
with ⟨i', hi'⟩,
use i',
exact hi'.2
end
lemma eq_of_linear_independent_of_span_subtype [nontrivial R] {s t : set M}
(hs : linear_independent R (λ x, x : s → M)) (h : t ⊆ s) (hst : s ⊆ span R t) : s = t :=
begin
let f : t ↪ s := ⟨λ x, ⟨x.1, h x.2⟩, λ a b hab, subtype.coe_injective (subtype.mk.inj hab)⟩,
have h_surj : surjective f,
{ apply surjective_of_linear_independent_of_span hs f _,
convert hst; simp [f, comp], },
show s = t,
{ apply subset.antisymm _ h,
intros x hx,
rcases h_surj ⟨x, hx⟩ with ⟨y, hy⟩,
convert y.mem,
rw ← subtype.mk.inj hy,
refl }
end
open linear_map
lemma linear_independent.image_subtype {s : set M} {f : M →ₗ[R] M'}
(hs : linear_independent R (λ x, x : s → M))
(hf_inj : disjoint (span R s) f.ker) : linear_independent R (λ x, x : f '' s → M') :=
begin
rw [← @subtype.range_coe _ s] at hf_inj,
refine (hs.map hf_inj).to_subtype_range' _,
simp [set.range_comp f]
end
lemma linear_independent.inl_union_inr {s : set M} {t : set M'}
(hs : linear_independent R (λ x, x : s → M))
(ht : linear_independent R (λ x, x : t → M')) :
linear_independent R (λ x, x : inl R M M' '' s ∪ inr R M M' '' t → M × M') :=
begin
refine (hs.image_subtype _).union (ht.image_subtype _) _; [simp, simp, skip],
simp only [span_image],
simp [disjoint_iff, prod_inf_prod]
end
lemma linear_independent_inl_union_inr' {v : ι → M} {v' : ι' → M'}
(hv : linear_independent R v) (hv' : linear_independent R v') :
linear_independent R (sum.elim (inl R M M' ∘ v) (inr R M M' ∘ v')) :=
(hv.map' (inl R M M') ker_inl).sum_type (hv'.map' (inr R M M') ker_inr) $
begin
refine is_compl_range_inl_inr.disjoint.mono _ _;
simp only [span_le, range_coe, range_comp_subset_range],
end
/-- Dedekind's linear independence of characters -/
-- See, for example, Keith Conrad's note
-- <https://kconrad.math.uconn.edu/blurbs/galoistheory/linearchar.pdf>
theorem linear_independent_monoid_hom (G : Type*) [monoid G] (L : Type*) [comm_ring L]
[no_zero_divisors L] :
@linear_independent _ L (G → L) (λ f, f : (G →* L) → (G → L)) _ _ _ :=
by letI := classical.dec_eq (G →* L);
letI : mul_action L L := distrib_mul_action.to_mul_action;
-- We prove linear independence by showing that only the trivial linear combination vanishes.
exact linear_independent_iff'.2
-- To do this, we use `finset` induction,
(λ s, finset.induction_on s (λ g hg i, false.elim) $ λ a s has ih g hg,
-- Here
-- * `a` is a new character we will insert into the `finset` of characters `s`,
-- * `ih` is the fact that only the trivial linear combination of characters in `s` is zero
-- * `hg` is the fact that `g` are the coefficients of a linear combination summing to zero
-- and it remains to prove that `g` vanishes on `insert a s`.
-- We now make the key calculation:
-- For any character `i` in the original `finset`, we have `g i • i = g i • a` as functions on the
-- monoid `G`.
have h1 : ∀ i ∈ s, (g i • i : G → L) = g i • a, from λ i his, funext $ λ x : G,
-- We prove these expressions are equal by showing
-- the differences of their values on each monoid element `x` is zero
eq_of_sub_eq_zero $ ih (λ j, g j * j x - g j * a x)
(funext $ λ y : G, calc
-- After that, it's just a chase scene.
(∑ i in s, ((g i * i x - g i * a x) • i : G → L)) y
= ∑ i in s, (g i * i x - g i * a x) * i y : finset.sum_apply _ _ _
... = ∑ i in s, (g i * i x * i y - g i * a x * i y) : finset.sum_congr rfl
(λ _ _, sub_mul _ _ _)
... = ∑ i in s, g i * i x * i y - ∑ i in s, g i * a x * i y : finset.sum_sub_distrib
... = (g a * a x * a y + ∑ i in s, g i * i x * i y)
- (g a * a x * a y + ∑ i in s, g i * a x * i y) : by rw add_sub_add_left_eq_sub
... = ∑ i in insert a s, g i * i x * i y - ∑ i in insert a s, g i * a x * i y :
by rw [finset.sum_insert has, finset.sum_insert has]
... = ∑ i in insert a s, g i * i (x * y) - ∑ i in insert a s, a x * (g i * i y) :
congr (congr_arg has_sub.sub (finset.sum_congr rfl $ λ i _, by rw [i.map_mul, mul_assoc]))
(finset.sum_congr rfl $ λ _ _, by rw [mul_assoc, mul_left_comm])
... = (∑ i in insert a s, (g i • i : G → L)) (x * y)
- a x * (∑ i in insert a s, (g i • i : G → L)) y :
by rw [finset.sum_apply, finset.sum_apply, finset.mul_sum]; refl
... = 0 - a x * 0 : by rw hg; refl
... = 0 : by rw [mul_zero, sub_zero])
i
his,
-- On the other hand, since `a` is not already in `s`, for any character `i ∈ s`
-- there is some element of the monoid on which it differs from `a`.
have h2 : ∀ i : G →* L, i ∈ s → ∃ y, i y ≠ a y, from λ i his,
classical.by_contradiction $ λ h,
have hia : i = a, from monoid_hom.ext $ λ y, classical.by_contradiction $ λ hy, h ⟨y, hy⟩,
has $ hia ▸ his,
-- From these two facts we deduce that `g` actually vanishes on `s`,
have h3 : ∀ i ∈ s, g i = 0, from λ i his, let ⟨y, hy⟩ := h2 i his in
have h : g i • i y = g i • a y, from congr_fun (h1 i his) y,
or.resolve_right (mul_eq_zero.1 $ by rw [mul_sub, sub_eq_zero]; exact h) (sub_ne_zero_of_ne hy),
-- And so, using the fact that the linear combination over `s` and over `insert a s` both vanish,
-- we deduce that `g a = 0`.
have h4 : g a = 0, from calc
g a = g a * 1 : (mul_one _).symm
... = (g a • a : G → L) 1 : by rw ← a.map_one; refl
... = (∑ i in insert a s, (g i • i : G → L)) 1 : begin
rw finset.sum_eq_single a,
{ intros i his hia, rw finset.mem_insert at his,
rw [h3 i (his.resolve_left hia), zero_smul] },
{ intros haas, exfalso, apply haas, exact finset.mem_insert_self a s }
end
... = 0 : by rw hg; refl,
-- Now we're done; the last two facts together imply that `g` vanishes on every element
-- of `insert a s`.
(finset.forall_mem_insert _ _ _).2 ⟨h4, h3⟩)
lemma le_of_span_le_span [nontrivial R] {s t u: set M}
(hl : linear_independent R (coe : u → M )) (hsu : s ⊆ u) (htu : t ⊆ u)
(hst : span R s ≤ span R t) : s ⊆ t :=
begin
have := eq_of_linear_independent_of_span_subtype
(hl.mono (set.union_subset hsu htu))
(set.subset_union_right _ _)
(set.union_subset (set.subset.trans subset_span hst) subset_span),
rw ← this, apply set.subset_union_left
end
lemma span_le_span_iff [nontrivial R] {s t u: set M}
(hl : linear_independent R (coe : u → M)) (hsu : s ⊆ u) (htu : t ⊆ u) :
span R s ≤ span R t ↔ s ⊆ t :=
⟨le_of_span_le_span hl hsu htu, span_mono⟩
end module
section nontrivial
variables [ring R] [nontrivial R] [add_comm_group M] [add_comm_group M']
variables [module R M] [no_zero_smul_divisors R M] [module R M']
variables {v : ι → M} {s t : set M} {x y z : M}
lemma linear_independent_unique_iff
(v : ι → M) [unique ι] :
linear_independent R v ↔ v default ≠ 0 :=
begin
simp only [linear_independent_iff, finsupp.total_unique, smul_eq_zero],
refine ⟨λ h hv, _, λ hv l hl, finsupp.unique_ext $ hl.resolve_right hv⟩,
have := h (finsupp.single default 1) (or.inr hv),
exact one_ne_zero (finsupp.single_eq_zero.1 this)
end
alias linear_independent_unique_iff ↔ _ linear_independent_unique
lemma linear_independent_singleton {x : M} (hx : x ≠ 0) :
linear_independent R (λ x, x : ({x} : set M) → M) :=
linear_independent_unique coe hx
end nontrivial
/-!
### Properties which require `division_ring K`
These can be considered generalizations of properties of linear independence in vector spaces.
-/
section module
variables [division_ring K] [add_comm_group V] [add_comm_group V']
variables [module K V] [module K V']
variables {v : ι → V} {s t : set V} {x y z : V}
open submodule
/- TODO: some of the following proofs can generalized with a zero_ne_one predicate type class
(instead of a data containing type class) -/
lemma mem_span_insert_exchange : x ∈ span K (insert y s) → x ∉ span K s → y ∈ span K (insert x s) :=
begin
simp [mem_span_insert],
rintro a z hz rfl h,
refine ⟨a⁻¹, -a⁻¹ • z, smul_mem _ _ hz, _⟩,
have a0 : a ≠ 0, {rintro rfl, simp * at *},
simp [a0, smul_add, smul_smul]
end
lemma linear_independent_iff_not_mem_span :
linear_independent K v ↔ (∀i, v i ∉ span K (v '' (univ \ {i}))) :=
begin
apply linear_independent_iff_not_smul_mem_span.trans,
split,
{ intros h i h_in_span,
apply one_ne_zero (h i 1 (by simp [h_in_span])) },
{ intros h i a ha,
by_contradiction ha',
exact false.elim (h _ ((smul_mem_iff _ ha').1 ha)) }
end
lemma linear_independent.insert (hs : linear_independent K (λ b, b : s → V)) (hx : x ∉ span K s) :
linear_independent K (λ b, b : insert x s → V) :=
begin
rw ← union_singleton,
have x0 : x ≠ 0 := mt (by rintro rfl; apply zero_mem (span K s)) hx,
apply hs.union (linear_independent_singleton x0),
rwa [disjoint_span_singleton' x0]
end
lemma linear_independent_option' :
linear_independent K (λ o, option.cases_on' o x v : option ι → V) ↔
linear_independent K v ∧ (x ∉ submodule.span K (range v)) :=
begin
rw [← linear_independent_equiv (equiv.option_equiv_sum_punit ι).symm, linear_independent_sum,
@range_unique _ punit, @linear_independent_unique_iff punit, disjoint_span_singleton],
dsimp [(∘)],
refine ⟨λ h, ⟨h.1, λ hx, h.2.1 $ h.2.2 hx⟩, λ h, ⟨h.1, _, λ hx, (h.2 hx).elim⟩⟩,
rintro rfl,
exact h.2 (zero_mem _)
end
lemma linear_independent.option (hv : linear_independent K v)
(hx : x ∉ submodule.span K (range v)) :
linear_independent K (λ o, option.cases_on' o x v : option ι → V) :=
linear_independent_option'.2 ⟨hv, hx⟩
lemma linear_independent_option {v : option ι → V} :
linear_independent K v ↔
linear_independent K (v ∘ coe : ι → V) ∧ v none ∉ submodule.span K (range (v ∘ coe : ι → V)) :=
by simp only [← linear_independent_option', option.cases_on'_none_coe]
theorem linear_independent_insert' {ι} {s : set ι} {a : ι} {f : ι → V} (has : a ∉ s) :
linear_independent K (λ x : insert a s, f x) ↔
linear_independent K (λ x : s, f x) ∧ f a ∉ submodule.span K (f '' s) :=
by { rw [← linear_independent_equiv ((equiv.option_equiv_sum_punit _).trans
(equiv.set.insert has).symm), linear_independent_option], simp [(∘), range_comp f] }
theorem linear_independent_insert (hxs : x ∉ s) :
linear_independent K (λ b : insert x s, (b : V)) ↔
linear_independent K (λ b : s, (b : V)) ∧ x ∉ submodule.span K s :=
(@linear_independent_insert' _ _ _ _ _ _ _ _ id hxs).trans $ by simp
lemma linear_independent_pair {x y : V} (hx : x ≠ 0) (hy : ∀ a : K, a • x ≠ y) :
linear_independent K (coe : ({x, y} : set V) → V) :=
pair_comm y x ▸ (linear_independent_singleton hx).insert $ mt mem_span_singleton.1
(not_exists.2 hy)
lemma linear_independent_fin_cons {n} {v : fin n → V} :
linear_independent K (fin.cons x v : fin (n + 1) → V) ↔
linear_independent K v ∧ x ∉ submodule.span K (range v) :=
begin
rw [← linear_independent_equiv (fin_succ_equiv n).symm, linear_independent_option],
convert iff.rfl,
{ ext,
-- TODO: why doesn't simp use `fin_succ_equiv_symm_coe` here?
rw [comp_app, comp_app, fin_succ_equiv_symm_coe, fin.cons_succ] },
{ ext,
rw [comp_app, comp_app, fin_succ_equiv_symm_coe, fin.cons_succ] }
end
lemma linear_independent_fin_snoc {n} {v : fin n → V} :
linear_independent K (fin.snoc v x : fin (n + 1) → V) ↔
linear_independent K v ∧ x ∉ submodule.span K (range v) :=
by rw [fin.snoc_eq_cons_rotate, linear_independent_equiv, linear_independent_fin_cons]
/-- See `linear_independent.fin_cons'` for an uglier version that works if you
only have a module over a semiring. -/
lemma linear_independent.fin_cons {n} {v : fin n → V} (hv : linear_independent K v)
(hx : x ∉ submodule.span K (range v)) :
linear_independent K (fin.cons x v : fin (n + 1) → V) :=
linear_independent_fin_cons.2 ⟨hv, hx⟩
lemma linear_independent_fin_succ {n} {v : fin (n + 1) → V} :
linear_independent K v ↔
linear_independent K (fin.tail v) ∧ v 0 ∉ submodule.span K (range $ fin.tail v) :=
by rw [← linear_independent_fin_cons, fin.cons_self_tail]
lemma linear_independent_fin_succ' {n} {v : fin (n + 1) → V} :
linear_independent K v ↔
linear_independent K (fin.init v) ∧ v (fin.last _) ∉ submodule.span K (range $ fin.init v) :=
by rw [← linear_independent_fin_snoc, fin.snoc_init_self]
lemma linear_independent_fin2 {f : fin 2 → V} :
linear_independent K f ↔ f 1 ≠ 0 ∧ ∀ a : K, a • f 1 ≠ f 0 :=
by rw [linear_independent_fin_succ, linear_independent_unique_iff, range_unique,
mem_span_singleton, not_exists,
show fin.tail f default = f 1, by rw ← fin.succ_zero_eq_one; refl]
lemma exists_linear_independent_extension (hs : linear_independent K (coe : s → V)) (hst : s ⊆ t) :
∃b⊆t, s ⊆ b ∧ t ⊆ span K b ∧ linear_independent K (coe : b → V) :=
begin
rcases zorn_subset_nonempty {b | b ⊆ t ∧ linear_independent K (coe : b → V)} _ _
⟨hst, hs⟩ with ⟨b, ⟨bt, bi⟩, sb, h⟩,
{ refine ⟨b, bt, sb, λ x xt, _, bi⟩,
by_contra hn,
apply hn,
rw ← h _ ⟨insert_subset.2 ⟨xt, bt⟩, bi.insert hn⟩ (subset_insert _ _),
exact subset_span (mem_insert _ _) },
{ refine λ c hc cc c0, ⟨⋃₀ c, ⟨_, _⟩, λ x, _⟩,
{ exact sUnion_subset (λ x xc, (hc xc).1) },
{ exact linear_independent_sUnion_of_directed cc.directed_on (λ x xc, (hc xc).2) },
{ exact subset_sUnion_of_mem } }
end
variables (K t)
lemma exists_linear_independent :
∃ b ⊆ t, span K b = span K t ∧ linear_independent K (coe : b → V) :=
begin
obtain ⟨b, hb₁, -, hb₂, hb₃⟩ :=
exists_linear_independent_extension (linear_independent_empty K V) (set.empty_subset t),
exact ⟨b, hb₁, (span_eq_of_le _ hb₂ (submodule.span_mono hb₁)).symm, hb₃⟩,
end
variables {K t}
/-- `linear_independent.extend` adds vectors to a linear independent set `s ⊆ t` until it spans
all elements of `t`. -/
noncomputable def linear_independent.extend (hs : linear_independent K (λ x, x : s → V))
(hst : s ⊆ t) : set V :=
classical.some (exists_linear_independent_extension hs hst)
lemma linear_independent.extend_subset (hs : linear_independent K (λ x, x : s → V))
(hst : s ⊆ t) : hs.extend hst ⊆ t :=
let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in hbt
lemma linear_independent.subset_extend (hs : linear_independent K (λ x, x : s → V))
(hst : s ⊆ t) : s ⊆ hs.extend hst :=
let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in hsb
lemma linear_independent.subset_span_extend (hs : linear_independent K (λ x, x : s → V))
(hst : s ⊆ t) : t ⊆ span K (hs.extend hst) :=
let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in htb
lemma linear_independent.linear_independent_extend (hs : linear_independent K (λ x, x : s → V))
(hst : s ⊆ t) : linear_independent K (coe : hs.extend hst → V) :=
let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in hli
variables {K V}
-- TODO(Mario): rewrite?
lemma exists_of_linear_independent_of_finite_span {t : finset V}
(hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ (span K ↑t : submodule K V)) :
∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = t.card :=
have ∀t, ∀(s' : finset V), ↑s' ⊆ s → s ∩ ↑t = ∅ → s ⊆ (span K ↑(s' ∪ t) : submodule K V) →
∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = (s' ∪ t).card :=
assume t, finset.induction_on t
(assume s' hs' _ hss',
have s = ↑s',
from eq_of_linear_independent_of_span_subtype hs hs' $
by simpa using hss',
⟨s', by simp [this]⟩)
(assume b₁ t hb₁t ih s' hs' hst hss',
have hb₁s : b₁ ∉ s,
from assume h,
have b₁ ∈ s ∩ ↑(insert b₁ t), from ⟨h, finset.mem_insert_self _ _⟩,
by rwa [hst] at this,
have hb₁s' : b₁ ∉ s', from assume h, hb₁s $ hs' h,
have hst : s ∩ ↑t = ∅,
from eq_empty_of_subset_empty $ subset.trans
(by simp [inter_subset_inter, subset.refl]) (le_of_eq hst),
classical.by_cases
(assume : s ⊆ (span K ↑(s' ∪ t) : submodule K V),
let ⟨u, hust, hsu, eq⟩ := ih _ hs' hst this in
have hb₁u : b₁ ∉ u, from assume h, (hust h).elim hb₁s hb₁t,
⟨insert b₁ u, by simp [insert_subset_insert hust],
subset.trans hsu (by simp), by simp [eq, hb₁t, hb₁s', hb₁u]⟩)
(assume : ¬ s ⊆ (span K ↑(s' ∪ t) : submodule K V),
let ⟨b₂, hb₂s, hb₂t⟩ := not_subset.mp this in
have hb₂t' : b₂ ∉ s' ∪ t, from assume h, hb₂t $ subset_span h,
have s ⊆ (span K ↑(insert b₂ s' ∪ t) : submodule K V), from
assume b₃ hb₃,
have ↑(s' ∪ insert b₁ t) ⊆ insert b₁ (insert b₂ ↑(s' ∪ t) : set V),
by simp [insert_eq, -singleton_union, -union_singleton, union_subset_union, subset.refl,
subset_union_right],
have hb₃ : b₃ ∈ span K (insert b₁ (insert b₂ ↑(s' ∪ t) : set V)),
from span_mono this (hss' hb₃),
have s ⊆ (span K (insert b₁ ↑(s' ∪ t)) : submodule K V),
by simpa [insert_eq, -singleton_union, -union_singleton] using hss',
have hb₁ : b₁ ∈ span K (insert b₂ ↑(s' ∪ t)),
from mem_span_insert_exchange (this hb₂s) hb₂t,
by rw [span_insert_eq_span hb₁] at hb₃; simpa using hb₃,
let ⟨u, hust, hsu, eq⟩ := ih _ (by simp [insert_subset, hb₂s, hs']) hst this in
⟨u, subset.trans hust $ union_subset_union (subset.refl _) (by simp [subset_insert]),
hsu, by simp [eq, hb₂t', hb₁t, hb₁s']⟩)),
begin
have eq : t.filter (λx, x ∈ s) ∪ t.filter (λx, x ∉ s) = t,
{ ext1 x,
by_cases x ∈ s; simp * },
apply exists.elim (this (t.filter (λx, x ∉ s)) (t.filter (λx, x ∈ s))
(by simp [set.subset_def]) (by simp [set.ext_iff] {contextual := tt}) (by rwa [eq])),
intros u h,
exact ⟨u, subset.trans h.1 (by simp [subset_def, and_imp, or_imp_distrib] {contextual:=tt}),
h.2.1, by simp only [h.2.2, eq]⟩
end
lemma exists_finite_card_le_of_finite_of_linear_independent_of_span
(ht : t.finite) (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ span K t) :
∃h : s.finite, h.to_finset.card ≤ ht.to_finset.card :=
have s ⊆ (span K ↑(ht.to_finset) : submodule K V), by simp; assumption,
let ⟨u, hust, hsu, eq⟩ := exists_of_linear_independent_of_finite_span hs this in
have s.finite, from u.finite_to_set.subset hsu,
⟨this, by rw [←eq]; exact (finset.card_le_of_subset $ finset.coe_subset.mp $ by simp [hsu])⟩
end module
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.