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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
f94b93998a87ce70cdf07b6e246363538c90cbab | c777c32c8e484e195053731103c5e52af26a25d1 | /src/algebra/lie/nilpotent.lean | 3f7c43ddd61d17697028803a5d79dec8b2b909fd | [
"Apache-2.0"
] | permissive | kbuzzard/mathlib | 2ff9e85dfe2a46f4b291927f983afec17e946eb8 | 58537299e922f9c77df76cb613910914a479c1f7 | refs/heads/master | 1,685,313,702,744 | 1,683,974,212,000 | 1,683,974,212,000 | 128,185,277 | 1 | 0 | null | 1,522,920,600,000 | 1,522,920,600,000 | null | UTF-8 | Lean | false | false | 27,727 | lean | /-
Copyright (c) 2021 Oliver Nash. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Oliver Nash
-/
import algebra.lie.solvable
import algebra.lie.quotient
import algebra.lie.normalizer
import linear_algebra.eigenspace
import ring_theory.nilpotent
/-!
# Nilpotent Lie algebras
Like groups, Lie algebras admit a natural concept of nilpotency. More generally, any Lie module
carries a natural concept of nilpotency. We define these here via the lower central series.
## Main definitions
* `lie_module.lower_central_series`
* `lie_module.is_nilpotent`
## Tags
lie algebra, lower central series, nilpotent
-/
universes u v w w₁ w₂
section nilpotent_modules
variables {R : Type u} {L : Type v} {M : Type w}
variables [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M]
variables [lie_ring_module L M] [lie_module R L M]
variables (k : ℕ) (N : lie_submodule R L M)
namespace lie_submodule
/-- A generalisation of the lower central series. The zeroth term is a specified Lie submodule of
a Lie module. In the case when we specify the top ideal `⊤` of the Lie algebra, regarded as a Lie
module over itself, we get the usual lower central series of a Lie algebra.
It can be more convenient to work with this generalisation when considering the lower central series
of a Lie submodule, regarded as a Lie module in its own right, since it provides a type-theoretic
expression of the fact that the terms of the Lie submodule's lower central series are also Lie
submodules of the enclosing Lie module.
See also `lie_module.lower_central_series_eq_lcs_comap` and
`lie_module.lower_central_series_map_eq_lcs` below, as well as `lie_submodule.ucs`. -/
def lcs : lie_submodule R L M → lie_submodule R L M := (λ N, ⁅(⊤ : lie_ideal R L), N⁆)^[k]
@[simp] lemma lcs_zero (N : lie_submodule R L M) : N.lcs 0 = N := rfl
@[simp] lemma lcs_succ : N.lcs (k + 1) = ⁅(⊤ : lie_ideal R L), N.lcs k⁆ :=
function.iterate_succ_apply' (λ N', ⁅⊤, N'⁆) k N
end lie_submodule
namespace lie_module
variables (R L M)
/-- The lower central series of Lie submodules of a Lie module. -/
def lower_central_series : lie_submodule R L M := (⊤ : lie_submodule R L M).lcs k
@[simp] lemma lower_central_series_zero : lower_central_series R L M 0 = ⊤ := rfl
@[simp] lemma lower_central_series_succ :
lower_central_series R L M (k + 1) = ⁅(⊤ : lie_ideal R L), lower_central_series R L M k⁆ :=
(⊤ : lie_submodule R L M).lcs_succ k
end lie_module
namespace lie_submodule
open lie_module
variables {R L M}
lemma lcs_le_self : N.lcs k ≤ N :=
begin
induction k with k ih,
{ simp, },
{ simp only [lcs_succ],
exact (lie_submodule.mono_lie_right _ _ ⊤ ih).trans (N.lie_le_right ⊤), },
end
lemma lower_central_series_eq_lcs_comap :
lower_central_series R L N k = (N.lcs k).comap N.incl :=
begin
induction k with k ih,
{ simp, },
{ simp only [lcs_succ, lower_central_series_succ] at ⊢ ih,
have : N.lcs k ≤ N.incl.range,
{ rw N.range_incl,
apply lcs_le_self, },
rw [ih, lie_submodule.comap_bracket_eq _ _ N.incl N.ker_incl this], },
end
lemma lower_central_series_map_eq_lcs :
(lower_central_series R L N k).map N.incl = N.lcs k :=
begin
rw [lower_central_series_eq_lcs_comap, lie_submodule.map_comap_incl, inf_eq_right],
apply lcs_le_self,
end
end lie_submodule
namespace lie_module
variables (R L M)
lemma antitone_lower_central_series : antitone $ lower_central_series R L M :=
begin
intros l k,
induction k with k ih generalizing l;
intros h,
{ exact (le_zero_iff.mp h).symm ▸ le_rfl, },
{ rcases nat.of_le_succ h with hk | hk,
{ rw lower_central_series_succ,
exact (lie_submodule.mono_lie_right _ _ ⊤ (ih hk)).trans (lie_submodule.lie_le_right _ _), },
{ exact hk.symm ▸ le_rfl, }, },
end
lemma trivial_iff_lower_central_eq_bot : is_trivial L M ↔ lower_central_series R L M 1 = ⊥ :=
begin
split; intros h,
{ erw [eq_bot_iff, lie_submodule.lie_span_le], rintros m ⟨x, n, hn⟩, rw [← hn, h.trivial], simp,},
{ rw lie_submodule.eq_bot_iff at h, apply is_trivial.mk, intros x m, apply h,
apply lie_submodule.subset_lie_span, use [x, m], refl, },
end
lemma iterate_to_endomorphism_mem_lower_central_series (x : L) (m : M) (k : ℕ) :
(to_endomorphism R L M x)^[k] m ∈ lower_central_series R L M k :=
begin
induction k with k ih,
{ simp only [function.iterate_zero], },
{ simp only [lower_central_series_succ, function.comp_app, function.iterate_succ',
to_endomorphism_apply_apply],
exact lie_submodule.lie_mem_lie _ _ (lie_submodule.mem_top x) ih, },
end
variables {R L M}
lemma map_lower_central_series_le
{M₂ : Type w₁} [add_comm_group M₂] [module R M₂] [lie_ring_module L M₂] [lie_module R L M₂]
(k : ℕ) (f : M →ₗ⁅R,L⁆ M₂) :
lie_submodule.map f (lower_central_series R L M k) ≤ lower_central_series R L M₂ k :=
begin
induction k with k ih,
{ simp only [lie_module.lower_central_series_zero, le_top], },
{ simp only [lie_module.lower_central_series_succ, lie_submodule.map_bracket_eq],
exact lie_submodule.mono_lie_right _ _ ⊤ ih, },
end
variables (R L M)
open lie_algebra
lemma derived_series_le_lower_central_series (k : ℕ) :
derived_series R L k ≤ lower_central_series R L L k :=
begin
induction k with k h,
{ rw [derived_series_def, derived_series_of_ideal_zero, lower_central_series_zero],
exact le_rfl, },
{ have h' : derived_series R L k ≤ ⊤, { simp only [le_top], },
rw [derived_series_def, derived_series_of_ideal_succ, lower_central_series_succ],
exact lie_submodule.mono_lie _ _ _ _ h' h, },
end
/-- A Lie module is nilpotent if its lower central series reaches 0 (in a finite number of
steps). -/
class is_nilpotent : Prop :=
(nilpotent : ∃ k, lower_central_series R L M k = ⊥)
/-- See also `lie_module.is_nilpotent_iff_exists_ucs_eq_top`. -/
lemma is_nilpotent_iff :
is_nilpotent R L M ↔ ∃ k, lower_central_series R L M k = ⊥ :=
⟨λ h, h.nilpotent, λ h, ⟨h⟩⟩
variables {R L M}
lemma _root_.lie_submodule.is_nilpotent_iff_exists_lcs_eq_bot (N : lie_submodule R L M) :
lie_module.is_nilpotent R L N ↔ ∃ k, N.lcs k = ⊥ :=
begin
rw is_nilpotent_iff,
refine exists_congr (λ k, _),
rw [N.lower_central_series_eq_lcs_comap k, lie_submodule.comap_incl_eq_bot,
inf_eq_right.mpr (N.lcs_le_self k)],
end
variables (R L M)
@[priority 100]
instance trivial_is_nilpotent [is_trivial L M] : is_nilpotent R L M :=
⟨by { use 1, change ⁅⊤, ⊤⁆ = ⊥, simp, }⟩
lemma nilpotent_endo_of_nilpotent_module [hM : is_nilpotent R L M] :
∃ (k : ℕ), ∀ (x : L), (to_endomorphism R L M x)^k = 0 :=
begin
unfreezingI { obtain ⟨k, hM⟩ := hM, },
use k,
intros x, ext m,
rw [linear_map.pow_apply, linear_map.zero_apply, ← @lie_submodule.mem_bot R L M, ← hM],
exact iterate_to_endomorphism_mem_lower_central_series R L M x m k,
end
/-- For a nilpotent Lie module, the weight space of the 0 weight is the whole module.
This result will be used downstream to show that weight spaces are Lie submodules, at which time
it will be possible to state it in the language of weight spaces. -/
lemma infi_max_gen_zero_eigenspace_eq_top_of_nilpotent [is_nilpotent R L M] :
(⨅ (x : L), (to_endomorphism R L M x).maximal_generalized_eigenspace 0) = ⊤ :=
begin
ext m,
simp only [module.End.mem_maximal_generalized_eigenspace, submodule.mem_top, sub_zero, iff_true,
zero_smul, submodule.mem_infi],
intros x,
obtain ⟨k, hk⟩ := nilpotent_endo_of_nilpotent_module R L M,
use k, rw hk,
exact linear_map.zero_apply m,
end
/-- If the quotient of a Lie module `M` by a Lie submodule on which the Lie algebra acts trivially
is nilpotent then `M` is nilpotent.
This is essentially the Lie module equivalent of the fact that a central
extension of nilpotent Lie algebras is nilpotent. See `lie_algebra.nilpotent_of_nilpotent_quotient`
below for the corresponding result for Lie algebras. -/
lemma nilpotent_of_nilpotent_quotient {N : lie_submodule R L M}
(h₁ : N ≤ max_triv_submodule R L M) (h₂ : is_nilpotent R L (M ⧸ N)) : is_nilpotent R L M :=
begin
unfreezingI { obtain ⟨k, hk⟩ := h₂, },
use k+1,
simp only [lower_central_series_succ],
suffices : lower_central_series R L M k ≤ N,
{ replace this := lie_submodule.mono_lie_right _ _ ⊤ (le_trans this h₁),
rwa [ideal_oper_max_triv_submodule_eq_bot, le_bot_iff] at this, },
rw [← lie_submodule.quotient.map_mk'_eq_bot_le, ← le_bot_iff, ← hk],
exact map_lower_central_series_le k (lie_submodule.quotient.mk' N),
end
/-- Given a nilpotent Lie module `M` with lower central series `M = C₀ ≥ C₁ ≥ ⋯ ≥ Cₖ = ⊥`, this is
the natural number `k` (the number of inclusions).
For a non-nilpotent module, we use the junk value 0. -/
noncomputable def nilpotency_length : ℕ :=
Inf { k | lower_central_series R L M k = ⊥ }
lemma nilpotency_length_eq_zero_iff [is_nilpotent R L M] :
nilpotency_length R L M = 0 ↔ subsingleton M :=
begin
let s := { k | lower_central_series R L M k = ⊥ },
have hs : s.nonempty,
{ unfreezingI { obtain ⟨k, hk⟩ := (by apply_instance : is_nilpotent R L M), },
exact ⟨k, hk⟩, },
change Inf s = 0 ↔ _,
rw [← lie_submodule.subsingleton_iff R L M, ← subsingleton_iff_bot_eq_top,
← lower_central_series_zero, @eq_comm (lie_submodule R L M)],
refine ⟨λ h, h ▸ nat.Inf_mem hs, λ h, _⟩,
rw nat.Inf_eq_zero,
exact or.inl h,
end
lemma nilpotency_length_eq_succ_iff (k : ℕ) :
nilpotency_length R L M = k + 1 ↔
lower_central_series R L M (k + 1) = ⊥ ∧ lower_central_series R L M k ≠ ⊥ :=
begin
let s := { k | lower_central_series R L M k = ⊥ },
change Inf s = k + 1 ↔ k + 1 ∈ s ∧ k ∉ s,
have hs : ∀ k₁ k₂, k₁ ≤ k₂ → k₁ ∈ s → k₂ ∈ s,
{ rintros k₁ k₂ h₁₂ (h₁ : lower_central_series R L M k₁ = ⊥),
exact eq_bot_iff.mpr (h₁ ▸ antitone_lower_central_series R L M h₁₂), },
exact nat.Inf_upward_closed_eq_succ_iff hs k,
end
/-- Given a non-trivial nilpotent Lie module `M` with lower central series
`M = C₀ ≥ C₁ ≥ ⋯ ≥ Cₖ = ⊥`, this is the `k-1`th term in the lower central series (the last
non-trivial term).
For a trivial or non-nilpotent module, this is the bottom submodule, `⊥`. -/
noncomputable def lower_central_series_last : lie_submodule R L M :=
match nilpotency_length R L M with
| 0 := ⊥
| k + 1 := lower_central_series R L M k
end
lemma lower_central_series_last_le_max_triv :
lower_central_series_last R L M ≤ max_triv_submodule R L M :=
begin
rw lower_central_series_last,
cases h : nilpotency_length R L M with k,
{ exact bot_le, },
{ rw le_max_triv_iff_bracket_eq_bot,
rw [nilpotency_length_eq_succ_iff, lower_central_series_succ] at h,
exact h.1, },
end
lemma nontrivial_lower_central_series_last [nontrivial M] [is_nilpotent R L M] :
nontrivial (lower_central_series_last R L M) :=
begin
rw [lie_submodule.nontrivial_iff_ne_bot, lower_central_series_last],
cases h : nilpotency_length R L M,
{ rw [nilpotency_length_eq_zero_iff, ← not_nontrivial_iff_subsingleton] at h,
contradiction, },
{ rw nilpotency_length_eq_succ_iff at h,
exact h.2, },
end
lemma nontrivial_max_triv_of_is_nilpotent [nontrivial M] [is_nilpotent R L M] :
nontrivial (max_triv_submodule R L M) :=
set.nontrivial_mono
(lower_central_series_last_le_max_triv R L M)
(nontrivial_lower_central_series_last R L M)
@[simp] lemma coe_lcs_range_to_endomorphism_eq (k : ℕ) :
(lower_central_series R (to_endomorphism R L M).range M k : submodule R M) =
lower_central_series R L M k :=
begin
induction k with k ih,
{ simp, },
{ simp only [lower_central_series_succ, lie_submodule.lie_ideal_oper_eq_linear_span',
← (lower_central_series R (to_endomorphism R L M).range M k).mem_coe_submodule, ih],
congr,
ext m,
split,
{ rintros ⟨⟨-, ⟨y, rfl⟩⟩, -, n, hn, rfl⟩,
exact ⟨y, lie_submodule.mem_top _, n, hn, rfl⟩, },
{ rintros ⟨x, hx, n, hn, rfl⟩,
exact ⟨⟨to_endomorphism R L M x, lie_hom.mem_range_self _ x⟩, lie_submodule.mem_top _,
n, hn, rfl⟩, }, },
end
@[simp] lemma is_nilpotent_range_to_endomorphism_iff :
is_nilpotent R (to_endomorphism R L M).range M ↔ is_nilpotent R L M :=
begin
split;
rintros ⟨k, hk⟩;
use k;
rw ← lie_submodule.coe_to_submodule_eq_iff at ⊢ hk;
simpa using hk,
end
end lie_module
namespace lie_submodule
variables {N₁ N₂ : lie_submodule R L M}
/-- The upper (aka ascending) central series.
See also `lie_submodule.lcs`. -/
def ucs (k : ℕ) : lie_submodule R L M → lie_submodule R L M :=
normalizer^[k]
@[simp] lemma ucs_zero : N.ucs 0 = N := rfl
@[simp] lemma ucs_succ (k : ℕ) :
N.ucs (k + 1) = (N.ucs k).normalizer :=
function.iterate_succ_apply' normalizer k N
lemma ucs_add (k l : ℕ) :
N.ucs (k + l) = (N.ucs l).ucs k :=
function.iterate_add_apply normalizer k l N
@[mono] lemma ucs_mono (k : ℕ) (h : N₁ ≤ N₂) :
N₁.ucs k ≤ N₂.ucs k :=
begin
induction k with k ih, { simpa, },
simp only [ucs_succ],
mono,
end
lemma ucs_eq_self_of_normalizer_eq_self (h : N₁.normalizer = N₁) (k : ℕ) :
N₁.ucs k = N₁ :=
by { induction k with k ih, { simp, }, { rwa [ucs_succ, ih], }, }
/-- If a Lie module `M` contains a self-normalizing Lie submodule `N`, then all terms of the upper
central series of `M` are contained in `N`.
An important instance of this situation arises from a Cartan subalgebra `H ⊆ L` with the roles of
`L`, `M`, `N` played by `H`, `L`, `H`, respectively. -/
lemma ucs_le_of_normalizer_eq_self (h : N₁.normalizer = N₁) (k : ℕ) :
(⊥ : lie_submodule R L M).ucs k ≤ N₁ :=
by { rw ← ucs_eq_self_of_normalizer_eq_self h k, mono, simp, }
lemma lcs_add_le_iff (l k : ℕ) :
N₁.lcs (l + k) ≤ N₂ ↔ N₁.lcs l ≤ N₂.ucs k :=
begin
revert l,
induction k with k ih, { simp, },
intros l,
rw [(by abel : l + (k + 1) = l + 1 + k), ih, ucs_succ, lcs_succ, top_lie_le_iff_le_normalizer],
end
lemma lcs_le_iff (k : ℕ) :
N₁.lcs k ≤ N₂ ↔ N₁ ≤ N₂.ucs k :=
by { convert lcs_add_le_iff 0 k, rw zero_add, }
lemma gc_lcs_ucs (k : ℕ):
galois_connection (λ (N : lie_submodule R L M), N.lcs k) (λ (N : lie_submodule R L M), N.ucs k) :=
λ N₁ N₂, lcs_le_iff k
lemma ucs_eq_top_iff (k : ℕ) : N.ucs k = ⊤ ↔ lie_module.lower_central_series R L M k ≤ N :=
by { rw [eq_top_iff, ← lcs_le_iff], refl, }
lemma _root_.lie_module.is_nilpotent_iff_exists_ucs_eq_top :
lie_module.is_nilpotent R L M ↔ ∃ k, (⊥ : lie_submodule R L M).ucs k = ⊤ :=
by { rw lie_module.is_nilpotent_iff, exact exists_congr (λ k, by simp [ucs_eq_top_iff]), }
lemma ucs_comap_incl (k : ℕ) :
((⊥ : lie_submodule R L M).ucs k).comap N.incl = (⊥ : lie_submodule R L N).ucs k :=
by { induction k with k ih, { exact N.ker_incl, }, { simp [← ih], }, }
lemma is_nilpotent_iff_exists_self_le_ucs :
lie_module.is_nilpotent R L N ↔ ∃ k, N ≤ (⊥ : lie_submodule R L M).ucs k :=
by simp_rw [lie_module.is_nilpotent_iff_exists_ucs_eq_top, ← ucs_comap_incl, comap_incl_eq_top]
end lie_submodule
section morphisms
open lie_module function
variables {L₂ M₂ : Type*} [lie_ring L₂] [lie_algebra R L₂]
variables [add_comm_group M₂] [module R M₂] [lie_ring_module L₂ M₂] [lie_module R L₂ M₂]
variables {f : L →ₗ⁅R⁆ L₂} {g : M →ₗ[R] M₂}
variables (hf : surjective f) (hg : surjective g) (hfg : ∀ x m, ⁅f x, g m⁆ = g ⁅x, m⁆)
include hf hg hfg
lemma function.surjective.lie_module_lcs_map_eq (k : ℕ) :
(lower_central_series R L M k : submodule R M).map g = lower_central_series R L₂ M₂ k :=
begin
induction k with k ih,
{ simp [linear_map.range_eq_top, hg], },
{ suffices : g '' {m | ∃ (x : L) n, n ∈ lower_central_series R L M k ∧ ⁅x, n⁆ = m} =
{m | ∃ (x : L₂) n, n ∈ lower_central_series R L M k ∧ ⁅x, g n⁆ = m},
{ simp only [← lie_submodule.mem_coe_submodule] at this,
simp [← lie_submodule.mem_coe_submodule, ← ih, lie_submodule.lie_ideal_oper_eq_linear_span',
submodule.map_span, -submodule.span_image, this], },
ext m₂,
split,
{ rintros ⟨m, ⟨x, n, hn, rfl⟩, rfl⟩,
exact ⟨f x, n, hn, hfg x n⟩, },
{ rintros ⟨x, n, hn, rfl⟩,
obtain ⟨y, rfl⟩ := hf x,
exact ⟨⁅y, n⁆, ⟨y, n, hn, rfl⟩, (hfg y n).symm⟩, }, },
end
lemma function.surjective.lie_module_is_nilpotent [is_nilpotent R L M] : is_nilpotent R L₂ M₂ :=
begin
obtain ⟨k, hk⟩ := id (by apply_instance : is_nilpotent R L M),
use k,
rw ← lie_submodule.coe_to_submodule_eq_iff at ⊢ hk,
simp [← hf.lie_module_lcs_map_eq hg hfg k, hk],
end
omit hf hg hfg
lemma equiv.lie_module_is_nilpotent_iff (f : L ≃ₗ⁅R⁆ L₂) (g : M ≃ₗ[R] M₂)
(hfg : ∀ x m, ⁅f x, g m⁆ = g ⁅x, m⁆) :
is_nilpotent R L M ↔ is_nilpotent R L₂ M₂ :=
begin
split;
introsI h,
{ have hg : surjective (g : M →ₗ[R] M₂) := g.surjective,
exact f.surjective.lie_module_is_nilpotent hg hfg, },
{ have hg : surjective (g.symm : M₂ →ₗ[R] M) := g.symm.surjective,
refine f.symm.surjective.lie_module_is_nilpotent hg (λ x m, _),
rw [linear_equiv.coe_coe, lie_equiv.coe_to_lie_hom, ← g.symm_apply_apply ⁅f.symm x, g.symm m⁆,
← hfg, f.apply_symm_apply, g.apply_symm_apply], },
end
@[simp] lemma lie_module.is_nilpotent_of_top_iff :
is_nilpotent R (⊤ : lie_subalgebra R L) M ↔ is_nilpotent R L M :=
equiv.lie_module_is_nilpotent_iff lie_subalgebra.top_equiv (1 : M ≃ₗ[R] M) (λ x m, rfl)
end morphisms
end nilpotent_modules
@[priority 100]
instance lie_algebra.is_solvable_of_is_nilpotent (R : Type u) (L : Type v)
[comm_ring R] [lie_ring L] [lie_algebra R L] [hL : lie_module.is_nilpotent R L L] :
lie_algebra.is_solvable R L :=
begin
obtain ⟨k, h⟩ : ∃ k, lie_module.lower_central_series R L L k = ⊥ := hL.nilpotent,
use k, rw ← le_bot_iff at h ⊢,
exact le_trans (lie_module.derived_series_le_lower_central_series R L k) h,
end
section nilpotent_algebras
variables (R : Type u) (L : Type v) (L' : Type w)
variables [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L']
/-- We say a Lie algebra is nilpotent when it is nilpotent as a Lie module over itself via the
adjoint representation. -/
abbreviation lie_algebra.is_nilpotent (R : Type u) (L : Type v)
[comm_ring R] [lie_ring L] [lie_algebra R L] : Prop :=
lie_module.is_nilpotent R L L
open lie_algebra
lemma lie_algebra.nilpotent_ad_of_nilpotent_algebra [is_nilpotent R L] :
∃ (k : ℕ), ∀ (x : L), (ad R L x)^k = 0 :=
lie_module.nilpotent_endo_of_nilpotent_module R L L
/-- See also `lie_algebra.zero_root_space_eq_top_of_nilpotent`. -/
lemma lie_algebra.infi_max_gen_zero_eigenspace_eq_top_of_nilpotent [is_nilpotent R L] :
(⨅ (x : L), (ad R L x).maximal_generalized_eigenspace 0) = ⊤ :=
lie_module.infi_max_gen_zero_eigenspace_eq_top_of_nilpotent R L L
-- TODO Generalise the below to Lie modules if / when we define morphisms, equivs of Lie modules
-- covering a Lie algebra morphism of (possibly different) Lie algebras.
variables {R L L'}
open lie_module (lower_central_series)
/-- Given an ideal `I` of a Lie algebra `L`, the lower central series of `L ⧸ I` is the same
whether we regard `L ⧸ I` as an `L` module or an `L ⧸ I` module.
TODO: This result obviously generalises but the generalisation requires the missing definition of
morphisms between Lie modules over different Lie algebras. -/
lemma coe_lower_central_series_ideal_quot_eq {I : lie_ideal R L} (k : ℕ) :
(lower_central_series R L (L ⧸ I) k : submodule R (L ⧸ I)) =
lower_central_series R (L ⧸ I) (L ⧸ I) k :=
begin
induction k with k ih,
{ simp only [lie_submodule.top_coe_submodule, lie_module.lower_central_series_zero], },
{ simp only [lie_module.lower_central_series_succ, lie_submodule.lie_ideal_oper_eq_linear_span],
congr,
ext x,
split,
{ rintros ⟨⟨y, -⟩, ⟨z, hz⟩, rfl : ⁅y, z⁆ = x⟩,
erw [← lie_submodule.mem_coe_submodule, ih, lie_submodule.mem_coe_submodule] at hz,
exact ⟨⟨lie_submodule.quotient.mk y, lie_submodule.mem_top _⟩, ⟨z, hz⟩, rfl⟩, },
{ rintros ⟨⟨⟨y⟩, -⟩, ⟨z, hz⟩, rfl : ⁅y, z⁆ = x⟩,
erw [← lie_submodule.mem_coe_submodule, ← ih, lie_submodule.mem_coe_submodule] at hz,
exact ⟨⟨y, lie_submodule.mem_top _⟩, ⟨z, hz⟩, rfl⟩, }, },
end
/-- Note that the below inequality can be strict. For example the ideal of strictly-upper-triangular
2x2 matrices inside the Lie algebra of upper-triangular 2x2 matrices with `k = 1`. -/
lemma lie_module.coe_lower_central_series_ideal_le {I : lie_ideal R L} (k : ℕ) :
(lower_central_series R I I k : submodule R I) ≤ lower_central_series R L I k :=
begin
induction k with k ih,
{ simp, },
{ simp only [lie_module.lower_central_series_succ, lie_submodule.lie_ideal_oper_eq_linear_span],
apply submodule.span_mono,
rintros x ⟨⟨y, -⟩, ⟨z, hz⟩, rfl : ⁅y, z⁆ = x⟩,
exact ⟨⟨y.val, lie_submodule.mem_top _⟩, ⟨z, ih hz⟩, rfl⟩, },
end
/-- A central extension of nilpotent Lie algebras is nilpotent. -/
lemma lie_algebra.nilpotent_of_nilpotent_quotient {I : lie_ideal R L}
(h₁ : I ≤ center R L) (h₂ : is_nilpotent R (L ⧸ I)) : is_nilpotent R L :=
begin
suffices : lie_module.is_nilpotent R L (L ⧸ I),
{ exact lie_module.nilpotent_of_nilpotent_quotient R L L h₁ this, },
unfreezingI { obtain ⟨k, hk⟩ := h₂, },
use k,
simp [← lie_submodule.coe_to_submodule_eq_iff, coe_lower_central_series_ideal_quot_eq, hk],
end
lemma lie_algebra.non_trivial_center_of_is_nilpotent [nontrivial L] [is_nilpotent R L] :
nontrivial $ center R L :=
lie_module.nontrivial_max_triv_of_is_nilpotent R L L
lemma lie_ideal.map_lower_central_series_le (k : ℕ) {f : L →ₗ⁅R⁆ L'} :
lie_ideal.map f (lower_central_series R L L k) ≤ lower_central_series R L' L' k :=
begin
induction k with k ih,
{ simp only [lie_module.lower_central_series_zero, le_top], },
{ simp only [lie_module.lower_central_series_succ],
exact le_trans (lie_ideal.map_bracket_le f) (lie_submodule.mono_lie _ _ _ _ le_top ih), },
end
lemma lie_ideal.lower_central_series_map_eq (k : ℕ) {f : L →ₗ⁅R⁆ L'}
(h : function.surjective f) :
lie_ideal.map f (lower_central_series R L L k) = lower_central_series R L' L' k :=
begin
have h' : (⊤ : lie_ideal R L).map f = ⊤,
{ rw ←f.ideal_range_eq_map,
exact f.ideal_range_eq_top_of_surjective h, },
induction k with k ih,
{ simp only [lie_module.lower_central_series_zero], exact h', },
{ simp only [lie_module.lower_central_series_succ, lie_ideal.map_bracket_eq f h, ih, h'], },
end
lemma function.injective.lie_algebra_is_nilpotent [h₁ : is_nilpotent R L'] {f : L →ₗ⁅R⁆ L'}
(h₂ : function.injective f) : is_nilpotent R L :=
{ nilpotent :=
begin
obtain ⟨k, hk⟩ := id h₁,
use k,
apply lie_ideal.bot_of_map_eq_bot h₂, rw [eq_bot_iff, ← hk],
apply lie_ideal.map_lower_central_series_le,
end, }
lemma function.surjective.lie_algebra_is_nilpotent [h₁ : is_nilpotent R L] {f : L →ₗ⁅R⁆ L'}
(h₂ : function.surjective f) : is_nilpotent R L' :=
{ nilpotent :=
begin
obtain ⟨k, hk⟩ := id h₁,
use k,
rw [← lie_ideal.lower_central_series_map_eq k h₂, hk],
simp only [lie_ideal.map_eq_bot_iff, bot_le],
end, }
lemma lie_equiv.nilpotent_iff_equiv_nilpotent (e : L ≃ₗ⁅R⁆ L') :
is_nilpotent R L ↔ is_nilpotent R L' :=
begin
split; introsI h,
{ exact e.symm.injective.lie_algebra_is_nilpotent, },
{ exact e.injective.lie_algebra_is_nilpotent, },
end
lemma lie_hom.is_nilpotent_range [is_nilpotent R L] (f : L →ₗ⁅R⁆ L') :
is_nilpotent R f.range :=
f.surjective_range_restrict.lie_algebra_is_nilpotent
/-- Note that this result is not quite a special case of
`lie_module.is_nilpotent_range_to_endomorphism_iff` which concerns nilpotency of the
`(ad R L).range`-module `L`, whereas this result concerns nilpotency of the `(ad R L).range`-module
`(ad R L).range`. -/
@[simp] lemma lie_algebra.is_nilpotent_range_ad_iff :
is_nilpotent R (ad R L).range ↔ is_nilpotent R L :=
begin
refine ⟨λ h, _, _⟩,
{ have : (ad R L).ker = center R L, { simp, },
exact lie_algebra.nilpotent_of_nilpotent_quotient (le_of_eq this)
((ad R L).quot_ker_equiv_range.nilpotent_iff_equiv_nilpotent.mpr h), },
{ introsI h,
exact (ad R L).is_nilpotent_range, },
end
instance [h : lie_algebra.is_nilpotent R L] : lie_algebra.is_nilpotent R (⊤ : lie_subalgebra R L) :=
lie_subalgebra.top_equiv.nilpotent_iff_equiv_nilpotent.mpr h
end nilpotent_algebras
namespace lie_ideal
open lie_module
variables {R L : Type*} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L)
variables (M : Type*) [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M]
variables (k : ℕ)
/-- Given a Lie module `M` over a Lie algebra `L` together with an ideal `I` of `L`, this is the
lower central series of `M` as an `I`-module. The advantage of using this definition instead of
`lie_module.lower_central_series R I M` is that its terms are Lie submodules of `M` as an
`L`-module, rather than just as an `I`-module.
See also `lie_ideal.coe_lcs_eq`. -/
def lcs : lie_submodule R L M := (λ N, ⁅I, N⁆)^[k] ⊤
@[simp] lemma lcs_zero : I.lcs M 0 = ⊤ := rfl
@[simp] lemma lcs_succ : I.lcs M (k + 1) = ⁅I, I.lcs M k⁆ :=
function.iterate_succ_apply' (λ N, ⁅I, N⁆) k ⊤
lemma lcs_top : (⊤ : lie_ideal R L).lcs M k = lower_central_series R L M k := rfl
lemma coe_lcs_eq : (I.lcs M k : submodule R M) = lower_central_series R I M k :=
begin
induction k with k ih,
{ simp, },
{ simp_rw [lower_central_series_succ, lcs_succ, lie_submodule.lie_ideal_oper_eq_linear_span',
← (I.lcs M k).mem_coe_submodule, ih, lie_submodule.mem_coe_submodule,
lie_submodule.mem_top, exists_true_left, (I : lie_subalgebra R L).coe_bracket_of_module],
congr,
ext m,
split,
{ rintros ⟨x, hx, m, hm, rfl⟩,
exact ⟨⟨x, hx⟩, m, hm, rfl⟩, },
{ rintros ⟨⟨x, hx⟩, m, hm, rfl⟩,
exact ⟨x, hx, m, hm, rfl⟩, }, },
end
end lie_ideal
section of_associative
variables (R : Type u) {A : Type v} [comm_ring R] [ring A] [algebra R A]
lemma lie_algebra.ad_nilpotent_of_nilpotent {a : A} (h : is_nilpotent a) :
is_nilpotent (lie_algebra.ad R A a) :=
begin
rw lie_algebra.ad_eq_lmul_left_sub_lmul_right,
have hl : is_nilpotent (linear_map.mul_left R a),
{ rwa linear_map.is_nilpotent_mul_left_iff, },
have hr : is_nilpotent (linear_map.mul_right R a),
{ rwa linear_map.is_nilpotent_mul_right_iff, },
have := @linear_map.commute_mul_left_right R A _ _ _ _ _ a a,
exact this.is_nilpotent_sub hl hr,
end
variables {R}
lemma lie_subalgebra.is_nilpotent_ad_of_is_nilpotent_ad {L : Type v} [lie_ring L] [lie_algebra R L]
(K : lie_subalgebra R L) {x : K} (h : is_nilpotent (lie_algebra.ad R L ↑x)) :
is_nilpotent (lie_algebra.ad R K x) :=
begin
obtain ⟨n, hn⟩ := h,
use n,
exact linear_map.submodule_pow_eq_zero_of_pow_eq_zero (K.ad_comp_incl_eq x) hn,
end
lemma lie_algebra.is_nilpotent_ad_of_is_nilpotent {L : lie_subalgebra R A} {x : L}
(h : is_nilpotent (x : A)) : is_nilpotent (lie_algebra.ad R L x) :=
L.is_nilpotent_ad_of_is_nilpotent_ad $ lie_algebra.ad_nilpotent_of_nilpotent R h
end of_associative
|
333443b04a629054be402227115f7eff7ee0cf7d | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/set_theory/cardinal.lean | bf404e2c1a542091a7850aa28bbe9aad112726c5 | [
"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 | 58,566 | 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, Floris van Doorn
-/
import data.set.countable
import set_theory.schroeder_bernstein
import data.fintype.card
import data.nat.enat
/-!
# Cardinal Numbers
We define cardinal numbers as a quotient of types under the equivalence relation of equinumerity.
## Main definitions
* `cardinal` the type of cardinal numbers (in a given universe).
* `cardinal.mk α` or `#α` is the cardinality of `α`. The notation `#` lives in the locale
`cardinal`.
* There is an instance that `cardinal` forms a `canonically_ordered_comm_semiring`.
* Addition `c₁ + c₂` is defined by `cardinal.add_def α β : #α + #β = #(α ⊕ β)`.
* Multiplication `c₁ * c₂` is defined by `cardinal.mul_def : #α * #β = #(α * β)`.
* The order `c₁ ≤ c₂` is defined by `cardinal.le_def α β : #α ≤ #β ↔ nonempty (α ↪ β)`.
* Exponentiation `c₁ ^ c₂` is defined by `cardinal.power_def α β : #α ^ #β = #(β → α)`.
* `cardinal.omega` or `ω` the cardinality of `ℕ`. This definition is universe polymorphic:
`cardinal.omega.{u} : cardinal.{u}`
(contrast with `ℕ : Type`, which lives in a specific universe).
In some cases the universe level has to be given explicitly.
* `cardinal.min (I : nonempty ι) (c : ι → cardinal)` is the minimal cardinal in the range of `c`.
* `cardinal.succ c` is the successor cardinal, the smallest cardinal larger than `c`.
* `cardinal.sum` is the sum of a collection of cardinals.
* `cardinal.sup` is the supremum of a collection of cardinals.
* `cardinal.powerlt c₁ c₂` or `c₁ ^< c₂` is defined as `sup_{γ < β} α^γ`.
## Main Statements
* Cantor's theorem: `cardinal.cantor c : c < 2 ^ c`.
* König's theorem: `cardinal.sum_lt_prod`
## Implementation notes
* There is a type of cardinal numbers in every universe level:
`cardinal.{u} : Type (u + 1)` is the quotient of types in `Type u`.
The operation `cardinal.lift` lifts cardinal numbers to a higher level.
* Cardinal arithmetic specifically for infinite cardinals (like `κ * κ = κ`) is in the file
`set_theory/cardinal_ordinal.lean`.
* There is an instance `has_pow cardinal`, but this will only fire if Lean already knows that both
the base and the exponent live in the same universe. As a workaround, you can add
```
local infixr ^ := @has_pow.pow cardinal cardinal cardinal.has_pow
```
to a file. This notation will work even if Lean doesn't know yet that the base and the exponent
live in the same universe (but no exponents in other types can be used).
## References
* <https://en.wikipedia.org/wiki/Cardinal_number>
## Tags
cardinal number, cardinal arithmetic, cardinal exponentiation, omega,
Cantor's theorem, König's theorem, Konig's theorem
-/
open function set
open_locale classical
universes u v w x
variables {α β : Type u}
/-- The equivalence relation on types given by equivalence (bijective correspondence) of types.
Quotienting by this equivalence relation gives the cardinal numbers.
-/
instance cardinal.is_equivalent : setoid (Type u) :=
{ r := λα β, nonempty (α ≃ β),
iseqv := ⟨λα,
⟨equiv.refl α⟩,
λα β ⟨e⟩, ⟨e.symm⟩,
λα β γ ⟨e₁⟩ ⟨e₂⟩, ⟨e₁.trans e₂⟩⟩ }
/-- `cardinal.{u}` is the type of cardinal numbers in `Type u`,
defined as the quotient of `Type u` by existence of an equivalence
(a bijection with explicit inverse). -/
def cardinal : Type (u + 1) := quotient cardinal.is_equivalent
namespace cardinal
/-- The cardinal number of a type -/
def mk : Type u → cardinal := quotient.mk
localized "notation `#` := cardinal.mk" in cardinal
instance can_lift_cardinal_Type : can_lift cardinal.{u} (Type u) :=
⟨mk, λ c, true, λ c _, quot.induction_on c $ λ α, ⟨α, rfl⟩⟩
protected lemma induction_on {p : cardinal → Prop} (c : cardinal) (h : ∀ α, p (#α)) : p c :=
quotient.induction_on c h
protected lemma eq : #α = #β ↔ nonempty (α ≃ β) := quotient.eq
@[simp] theorem mk_def (α : Type u) : @eq cardinal ⟦α⟧ (#α) := rfl
@[simp] theorem mk_out (c : cardinal) : #(c.out) = c := quotient.out_eq _
/-- The representative of the cardinal of a type is equivalent ot the original type. -/
noncomputable def out_mk_equiv {α : Type v} : (#α).out ≃ α :=
nonempty.some $ cardinal.eq.mp (by simp)
protected lemma mk_congr (e : α ≃ β) : # α = # β :=
quot.sound ⟨e⟩
alias cardinal.mk_congr ← equiv.cardinal_eq
/-- The universe lift operation on cardinals. You can specify the universes explicitly with
`lift.{u v} : cardinal.{v} → cardinal.{max v u}` -/
def lift (c : cardinal.{v}) : cardinal.{max v u} :=
quotient.lift_on c (λ α, ⟦ulift α⟧) $ λ α β ⟨e⟩,
quotient.sound ⟨equiv.ulift.trans $ e.trans equiv.ulift.symm⟩
@[simp] theorem mk_ulift (α) : #(ulift.{v u} α) = lift.{v} (#α) := rfl
@[simp] theorem lift_umax : lift.{(max u v) u} = lift.{v u} :=
funext $ λ a, quot.induction_on a $ λ α,
quotient.sound ⟨equiv.ulift.trans equiv.ulift.symm⟩
theorem lift_id' (a : cardinal) : lift a = a :=
quot.induction_on a $ λ α, quot.sound ⟨equiv.ulift⟩
@[simp] theorem lift_id : ∀ a, lift.{u u} a = a := lift_id'.{u u}
@[simp] theorem lift_lift (a : cardinal) :
lift.{w} (lift.{v} a) = lift.{(max v w)} a :=
quot.induction_on a $ λ α,
quotient.sound ⟨equiv.ulift.trans $ equiv.ulift.trans equiv.ulift.symm⟩
/-- We define the order on cardinal numbers by `#α ≤ #β` if and only if
there exists an embedding (injective function) from α to β. -/
instance : has_le cardinal.{u} :=
⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, nonempty $ α ↪ β) $
assume α β γ δ ⟨e₁⟩ ⟨e₂⟩,
propext ⟨assume ⟨e⟩, ⟨e.congr e₁ e₂⟩, assume ⟨e⟩, ⟨e.congr e₁.symm e₂.symm⟩⟩⟩
theorem le_def (α β : Type u) : #α ≤ #β ↔ nonempty (α ↪ β) :=
iff.rfl
theorem mk_le_of_injective {α β : Type u} {f : α → β} (hf : injective f) : #α ≤ #β :=
⟨⟨f, hf⟩⟩
theorem mk_le_of_surjective {α β : Type u} {f : α → β} (hf : surjective f) : #β ≤ #α :=
⟨embedding.of_surjective f hf⟩
theorem le_mk_iff_exists_set {c : cardinal} {α : Type u} :
c ≤ #α ↔ ∃ p : set α, #p = c :=
⟨quotient.induction_on c $ λ β ⟨⟨f, hf⟩⟩,
⟨set.range f, (equiv.of_injective f hf).cardinal_eq.symm⟩,
λ ⟨p, e⟩, e ▸ ⟨⟨subtype.val, λ a b, subtype.eq⟩⟩⟩
theorem out_embedding {c c' : cardinal} : c ≤ c' ↔ nonempty (c.out ↪ c'.out) :=
by { transitivity _, rw [←quotient.out_eq c, ←quotient.out_eq c'], refl }
noncomputable instance : linear_order cardinal.{u} :=
{ le := (≤),
le_refl := by rintros ⟨α⟩; exact ⟨embedding.refl _⟩,
le_trans := by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨e₁⟩ ⟨e₂⟩; exact ⟨e₁.trans e₂⟩,
le_antisymm := by { rintros ⟨α⟩ ⟨β⟩ ⟨e₁⟩ ⟨e₂⟩, exact quotient.sound (e₁.antisymm e₂) },
le_total := by rintros ⟨α⟩ ⟨β⟩; exact embedding.total,
decidable_le := classical.dec_rel _ }
-- short-circuit type class inference
noncomputable instance : distrib_lattice cardinal.{u} := by apply_instance
theorem lift_mk_le {α : Type u} {β : Type v} :
lift.{(max v w)} (#α) ≤ lift.{(max u w)} (#β) ↔ nonempty (α ↪ β) :=
⟨λ ⟨f⟩, ⟨embedding.congr equiv.ulift equiv.ulift f⟩,
λ ⟨f⟩, ⟨embedding.congr equiv.ulift.symm equiv.ulift.symm f⟩⟩
/-- A variant of `lift_mk_le` with specialized universes.
Because Lean often can not realize it should use this specialization itself,
we provide this statement separately so you don't have to solve the specialization problem either.
-/
theorem lift_mk_le' {α : Type u} {β : Type v} :
lift.{v} (#α) ≤ lift.{u} (#β) ↔ nonempty (α ↪ β) :=
lift_mk_le.{u v 0}
theorem lift_mk_eq {α : Type u} {β : Type v} :
lift.{(max v w)} (#α) = lift.{(max u w)} (#β) ↔ nonempty (α ≃ β) :=
quotient.eq.trans
⟨λ ⟨f⟩, ⟨equiv.ulift.symm.trans $ f.trans equiv.ulift⟩,
λ ⟨f⟩, ⟨equiv.ulift.trans $ f.trans equiv.ulift.symm⟩⟩
/-- A variant of `lift_mk_eq` with specialized universes.
Because Lean often can not realize it should use this specialization itself,
we provide this statement separately so you don't have to solve the specialization problem either.
-/
theorem lift_mk_eq' {α : Type u} {β : Type v} :
lift.{v} (#α) = lift.{u} (#β) ↔ nonempty (α ≃ β) :=
lift_mk_eq.{u v 0}
@[simp] theorem lift_le {a b : cardinal} : lift a ≤ lift b ↔ a ≤ b :=
quotient.induction_on₂ a b $ λ α β,
by rw ← lift_umax; exact lift_mk_le
@[simp] theorem lift_inj {a b : cardinal} : lift a = lift b ↔ a = b :=
by simp [le_antisymm_iff]
@[simp] theorem lift_lt {a b : cardinal} : lift a < lift b ↔ a < b :=
by simp [lt_iff_le_not_le, -not_le]
instance : has_zero cardinal.{u} := ⟨#pempty⟩
instance : inhabited cardinal.{u} := ⟨0⟩
@[simp] lemma mk_eq_zero (α : Type u) [is_empty α] : #α = 0 :=
(equiv.equiv_pempty α).cardinal_eq
@[simp] theorem lift_zero : lift 0 = 0 :=
quotient.sound ⟨equiv.equiv_pempty _⟩
lemma mk_eq_zero_iff {α : Type u} : #α = 0 ↔ is_empty α :=
⟨λ e, let ⟨h⟩ := quotient.exact e in h.is_empty, @mk_eq_zero α⟩
theorem mk_ne_zero_iff {α : Type u} : #α ≠ 0 ↔ nonempty α :=
(not_iff_not.2 mk_eq_zero_iff).trans not_is_empty_iff
@[simp] lemma mk_ne_zero (α : Type u) [nonempty α] : #α ≠ 0 := mk_ne_zero_iff.2 ‹_›
instance : has_one cardinal.{u} := ⟨⟦punit⟧⟩
instance : nontrivial cardinal.{u} := ⟨⟨1, 0, mk_ne_zero _⟩⟩
theorem le_one_iff_subsingleton {α : Type u} : #α ≤ 1 ↔ subsingleton α :=
⟨λ ⟨f⟩, ⟨λ a b, f.injective (subsingleton.elim _ _)⟩,
λ ⟨h⟩, ⟨⟨λ a, punit.star, λ a b _, h _ _⟩⟩⟩
theorem one_lt_iff_nontrivial {α : Type u} : 1 < #α ↔ nontrivial α :=
by { rw [← not_iff_not, not_nontrivial_iff_subsingleton, ← le_one_iff_subsingleton], simp }
instance : has_add cardinal.{u} :=
⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, #(α ⊕ β)) $ assume α β γ δ ⟨e₁⟩ ⟨e₂⟩,
quotient.sound ⟨equiv.sum_congr e₁ e₂⟩⟩
@[simp] theorem add_def (α β : Type u) : #α + #β = #(α ⊕ β) := rfl
lemma add (α : Type u) (β : Type v) :
#(α ⊕ β) = lift.{v u} (#α) + lift.{u v} (#β) :=
begin
rw [←cardinal.mk_ulift, ←cardinal.mk_ulift, add_def],
exact cardinal.eq.2 ⟨equiv.sum_congr (equiv.ulift).symm (equiv.ulift).symm⟩,
end
instance : has_mul cardinal.{u} :=
⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, #(α × β)) $ assume α β γ δ ⟨e₁⟩ ⟨e₂⟩,
quotient.sound ⟨equiv.prod_congr e₁ e₂⟩⟩
@[simp] theorem mul_def (α β : Type u) : #α * #β = #(α × β) := rfl
lemma mul (α : Type u) (β : Type v) :
#(α × β) = lift.{v u} (#α) * lift.{u v} (#β) :=
begin
rw [←cardinal.mk_ulift, ←cardinal.mk_ulift, mul_def],
exact cardinal.eq.2 ⟨equiv.prod_congr (equiv.ulift).symm (equiv.ulift).symm⟩,
end
private theorem add_comm (a b : cardinal.{u}) : a + b = b + a :=
quotient.induction_on₂ a b $ assume α β, quotient.sound ⟨equiv.sum_comm α β⟩
private theorem mul_comm (a b : cardinal.{u}) : a * b = b * a :=
quotient.induction_on₂ a b $ assume α β, quotient.sound ⟨equiv.prod_comm α β⟩
private theorem zero_add (a : cardinal.{u}) : 0 + a = a :=
quotient.induction_on a $ assume α, quotient.sound ⟨equiv.empty_sum pempty α⟩
private theorem zero_mul (a : cardinal.{u}) : 0 * a = 0 :=
quotient.induction_on a $ assume α, quotient.sound ⟨equiv.pempty_prod α⟩
private theorem one_mul (a : cardinal.{u}) : 1 * a = a :=
quotient.induction_on a $ assume α, quotient.sound ⟨equiv.punit_prod α⟩
private theorem left_distrib (a b c : cardinal.{u}) : a * (b + c) = a * b + a * c :=
quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.prod_sum_distrib α β γ⟩
protected theorem eq_zero_or_eq_zero_of_mul_eq_zero {a b : cardinal.{u}} :
a * b = 0 → a = 0 ∨ b = 0 :=
begin
induction a using cardinal.induction_on with α,
induction b using cardinal.induction_on with β,
simp only [mul_def, mk_eq_zero_iff, is_empty_prod],
exact id
end
instance : comm_semiring cardinal.{u} :=
{ zero := 0,
one := 1,
add := (+),
mul := (*),
zero_add := zero_add,
add_zero := assume a, by rw [add_comm a 0, zero_add a],
add_assoc := λa b c, quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.sum_assoc α β γ⟩,
add_comm := add_comm,
zero_mul := zero_mul,
mul_zero := assume a, by rw [mul_comm a 0, zero_mul a],
one_mul := one_mul,
mul_one := assume a, by rw [mul_comm a 1, one_mul a],
mul_assoc := λa b c, quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.prod_assoc α β γ⟩,
mul_comm := mul_comm,
left_distrib := left_distrib,
right_distrib := assume a b c,
by rw [mul_comm (a + b) c, left_distrib c a b, mul_comm c a, mul_comm c b] }
/-- The cardinal exponential. `#α ^ #β` is the cardinal of `β → α`. -/
protected def power (a b : cardinal.{u}) : cardinal.{u} :=
quotient.lift_on₂ a b (λα β, #(β → α)) $ assume α₁ α₂ β₁ β₂ ⟨e₁⟩ ⟨e₂⟩,
quotient.sound ⟨equiv.arrow_congr e₂ e₁⟩
instance : has_pow cardinal cardinal := ⟨cardinal.power⟩
local infixr ^ := @has_pow.pow cardinal cardinal cardinal.has_pow
@[simp] theorem power_def (α β) : #α ^ #β = #(β → α) := rfl
@[simp] theorem lift_power (a b) : lift (a ^ b) = lift a ^ lift b :=
quotient.induction_on₂ a b $ λ α β,
quotient.sound ⟨equiv.ulift.trans (equiv.arrow_congr equiv.ulift equiv.ulift).symm⟩
@[simp] theorem power_zero {a : cardinal} : a ^ 0 = 1 :=
quotient.induction_on a $ assume α, quotient.sound
⟨equiv.pempty_arrow_equiv_punit α⟩
@[simp] theorem power_one {a : cardinal} : a ^ 1 = a :=
quotient.induction_on a $ assume α, quotient.sound
⟨equiv.punit_arrow_equiv α⟩
@[simp] theorem one_power {a : cardinal} : 1 ^ a = 1 :=
quotient.induction_on a $ assume α, quotient.sound
⟨equiv.arrow_punit_equiv_punit α⟩
@[simp] theorem prop_eq_two : #(ulift Prop) = 2 :=
quot.sound ⟨equiv.ulift.trans $ equiv.Prop_equiv_bool.trans equiv.bool_equiv_punit_sum_punit⟩
@[simp] theorem zero_power {a : cardinal} : a ≠ 0 → 0 ^ a = 0 :=
quotient.induction_on a $ assume α heq, mk_eq_zero_iff.2 $ is_empty_pi.2 $
let ⟨a⟩ := mk_ne_zero_iff.1 heq in ⟨a, pempty.is_empty⟩
theorem power_ne_zero {a : cardinal} (b) : a ≠ 0 → a ^ b ≠ 0 :=
quotient.induction_on₂ a b $ λ α β h,
let ⟨a⟩ := mk_ne_zero_iff.1 h in mk_ne_zero_iff.2 ⟨λ _, a⟩
theorem mul_power {a b c : cardinal} : (a * b) ^ c = a ^ c * b ^ c :=
quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.arrow_prod_equiv_prod_arrow α β γ⟩
theorem power_add {a b c : cardinal} : a ^ (b + c) = a ^ b * a ^ c :=
quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.sum_arrow_equiv_prod_arrow β γ α⟩
theorem power_mul {a b c : cardinal} : a ^ (b * c) = (a ^ b) ^ c :=
by rw [_root_.mul_comm b c];
from (quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.curry γ β α⟩)
@[simp] lemma pow_cast_right (κ : cardinal.{u}) :
∀ n : ℕ, (κ ^ (↑n : cardinal.{u})) = @has_pow.pow _ _ monoid.has_pow κ n
| 0 := by simp
| (_+1) := by rw [nat.cast_succ, power_add, power_one, _root_.mul_comm, pow_succ, pow_cast_right]
section order_properties
open sum
protected theorem zero_le : ∀(a : cardinal), 0 ≤ a :=
by rintro ⟨α⟩; exact ⟨embedding.of_is_empty⟩
protected theorem add_le_add : ∀{a b c d : cardinal}, a ≤ b → c ≤ d → a + c ≤ b + d :=
by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨δ⟩ ⟨e₁⟩ ⟨e₂⟩; exact ⟨e₁.sum_map e₂⟩
protected theorem add_le_add_left (a) {b c : cardinal} : b ≤ c → a + b ≤ a + c :=
cardinal.add_le_add (le_refl _)
protected theorem le_iff_exists_add {a b : cardinal} : a ≤ b ↔ ∃ c, b = a + c :=
⟨quotient.induction_on₂ a b $ λ α β ⟨⟨f, hf⟩⟩,
have (α ⊕ ((range f)ᶜ : set β)) ≃ β, from
(equiv.sum_congr (equiv.of_injective f hf) (equiv.refl _)).trans $
(equiv.set.sum_compl (range f)),
⟨⟦↥(range f)ᶜ⟧, quotient.sound ⟨this.symm⟩⟩,
λ ⟨c, e⟩, add_zero a ▸ e.symm ▸ cardinal.add_le_add_left _ (cardinal.zero_le _)⟩
instance : order_bot cardinal.{u} :=
{ bot := 0, bot_le := cardinal.zero_le, ..cardinal.linear_order }
instance : canonically_ordered_comm_semiring cardinal.{u} :=
{ add_le_add_left := λ a b h c, cardinal.add_le_add_left _ h,
le_iff_exists_add := @cardinal.le_iff_exists_add,
eq_zero_or_eq_zero_of_mul_eq_zero := @cardinal.eq_zero_or_eq_zero_of_mul_eq_zero,
..cardinal.order_bot,
..cardinal.comm_semiring, ..cardinal.linear_order }
noncomputable instance : canonically_linear_ordered_add_monoid cardinal.{u} :=
{ .. (infer_instance : canonically_ordered_add_monoid cardinal.{u}),
.. cardinal.linear_order }
@[simp] theorem zero_lt_one : (0 : cardinal) < 1 :=
lt_of_le_of_ne (zero_le _) zero_ne_one
@[simp] theorem lift_one : lift 1 = 1 :=
quotient.sound ⟨equiv.ulift.trans equiv.punit_equiv_punit⟩
@[simp] theorem lift_add (a b) : lift (a + b) = lift a + lift b :=
quotient.induction_on₂ a b $ λ α β,
quotient.sound ⟨equiv.ulift.trans (equiv.sum_congr equiv.ulift equiv.ulift).symm⟩
@[simp] theorem lift_mul (a b) : lift (a * b) = lift a * lift b :=
quotient.induction_on₂ a b $ λ α β,
quotient.sound ⟨equiv.ulift.trans (equiv.prod_congr equiv.ulift equiv.ulift).symm⟩
@[simp] theorem lift_bit0 (a : cardinal) : lift (bit0 a) = bit0 (lift a) :=
lift_add a a
@[simp] theorem lift_bit1 (a : cardinal) : lift (bit1 a) = bit1 (lift a) :=
by simp [bit1]
theorem lift_two : lift.{u v} 2 = 2 := by simp
theorem lift_two_power (a) : lift (2 ^ a) = 2 ^ lift a := by simp
lemma zero_power_le (c : cardinal.{u}) : (0 : cardinal.{u}) ^ c ≤ 1 :=
by { by_cases h : c = 0, rw [h, power_zero], rw [zero_power h], apply zero_le }
theorem power_le_power_left : ∀{a b c : cardinal}, a ≠ 0 → b ≤ c → a ^ b ≤ a ^ c :=
by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ hα ⟨e⟩; exact
let ⟨a⟩ := mk_ne_zero_iff.1 hα in
⟨@embedding.arrow_congr_right _ _ _ ⟨a⟩ e⟩
theorem power_le_max_power_one {a b c : cardinal} (h : b ≤ c) : a ^ b ≤ max (a ^ c) 1 :=
begin
by_cases ha : a = 0,
simp [ha, zero_power_le],
exact le_trans (power_le_power_left ha h) (le_max_left _ _)
end
theorem power_le_power_right {a b c : cardinal} : a ≤ b → a ^ c ≤ b ^ c :=
quotient.induction_on₃ a b c $ assume α β γ ⟨e⟩, ⟨embedding.arrow_congr_left e⟩
end order_properties
/-- **Cantor's theorem** -/
theorem cantor : ∀(a : cardinal.{u}), a < 2 ^ a :=
by rw ← prop_eq_two; rintros ⟨a⟩; exact ⟨
⟨⟨λ a b, ⟨a = b⟩, λ a b h, cast (ulift.up.inj (@congr_fun _ _ _ _ h b)).symm rfl⟩⟩,
λ ⟨⟨f, hf⟩⟩, cantor_injective (λ s, f (λ a, ⟨s a⟩)) $
λ s t h, by funext a; injection congr_fun (hf h) a⟩
instance : no_top_order cardinal.{u} :=
{ no_top := λ a, ⟨_, cantor a⟩, ..cardinal.linear_order }
/-- The minimum cardinal in a family of cardinals (the existence
of which is provided by `injective_min`). -/
noncomputable def min {ι} (I : nonempty ι) (f : ι → cardinal) : cardinal :=
f $ classical.some $
@embedding.min_injective _ (λ i, (f i).out) I
theorem min_eq {ι} (I) (f : ι → cardinal) : ∃ i, min I f = f i :=
⟨_, rfl⟩
theorem min_le {ι I} (f : ι → cardinal) (i) : min I f ≤ f i :=
by rw [← mk_out (min I f), ← mk_out (f i)]; exact
let ⟨g⟩ := classical.some_spec
(@embedding.min_injective _ (λ i, (f i).out) I) in
⟨g i⟩
theorem le_min {ι I} {f : ι → cardinal} {a} : a ≤ min I f ↔ ∀ i, a ≤ f i :=
⟨λ h i, le_trans h (min_le _ _),
λ h, let ⟨i, e⟩ := min_eq I f in e.symm ▸ h i⟩
protected theorem wf : @well_founded cardinal.{u} (<) :=
⟨λ a, classical.by_contradiction $ λ h,
let ι := {c :cardinal // ¬ acc (<) c},
f : ι → cardinal := subtype.val,
⟨⟨c, hc⟩, hi⟩ := @min_eq ι ⟨⟨_, h⟩⟩ f in
hc (acc.intro _ (λ j ⟨_, h'⟩,
classical.by_contradiction $ λ hj, h' $
by have := min_le f ⟨j, hj⟩; rwa hi at this))⟩
instance has_wf : @has_well_founded cardinal.{u} := ⟨(<), cardinal.wf⟩
instance wo : @is_well_order cardinal.{u} (<) := ⟨cardinal.wf⟩
/-- The successor cardinal - the smallest cardinal greater than
`c`. This is not the same as `c + 1` except in the case of finite `c`. -/
noncomputable def succ (c : cardinal) : cardinal :=
@min {c' // c < c'} ⟨⟨_, cantor _⟩⟩ subtype.val
theorem lt_succ_self (c : cardinal) : c < succ c :=
by cases min_eq _ _ with s e; rw [succ, e]; exact s.2
theorem succ_le {a b : cardinal} : succ a ≤ b ↔ a < b :=
⟨lt_of_lt_of_le (lt_succ_self _), λ h,
by exact min_le _ (subtype.mk b h)⟩
theorem lt_succ {a b : cardinal} : a < succ b ↔ a ≤ b :=
by rw [← not_le, succ_le, not_lt]
theorem add_one_le_succ (c : cardinal) : c + 1 ≤ succ c :=
begin
refine quot.induction_on c (λ α, _) (lt_succ_self c),
refine quot.induction_on (succ (quot.mk setoid.r α)) (λ β h, _),
cases h.left with f,
have : ¬ surjective f := λ hn,
ne_of_lt h (quotient.sound ⟨equiv.of_bijective f ⟨f.injective, hn⟩⟩),
cases not_forall.1 this with b nex,
refine ⟨⟨sum.rec (by exact f) _, _⟩⟩,
{ exact λ _, b },
{ intros a b h, rcases a with a|⟨⟨⟨⟩⟩⟩; rcases b with b|⟨⟨⟨⟩⟩⟩,
{ rw f.injective h },
{ exact nex.elim ⟨_, h⟩ },
{ exact nex.elim ⟨_, h.symm⟩ },
{ refl } }
end
lemma succ_ne_zero (c : cardinal) : succ c ≠ 0 :=
by { rw [←pos_iff_ne_zero, lt_succ], apply zero_le }
/-- The indexed sum of cardinals is the cardinality of the
indexed disjoint union, i.e. sigma type. -/
def sum {ι} (f : ι → cardinal) : cardinal := mk Σ i, (f i).out
theorem le_sum {ι} (f : ι → cardinal) (i) : f i ≤ sum f :=
by rw ← quotient.out_eq (f i); exact
⟨⟨λ a, ⟨i, a⟩, λ a b h, eq_of_heq $ by injection h⟩⟩
@[simp] theorem sum_mk {ι} (f : ι → Type*) : sum (λ i, #(f i)) = #(Σ i, f i) :=
quot.sound ⟨equiv.sigma_congr_right $ λ i,
classical.choice $ quotient.exact $ quot.out_eq $ #(f i)⟩
theorem sum_const (ι : Type u) (a : cardinal.{u}) : sum (λ _:ι, a) = #ι * a :=
quotient.induction_on a $ λ α, by { simp only [mul_def, sum_mk, mk_def], exact
quotient.sound ⟨equiv.sigma_equiv_prod _ _⟩ }
theorem sum_le_sum {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : sum f ≤ sum g :=
⟨(embedding.refl _).sigma_map $ λ i, classical.choice $
by have := H i; rwa [← quot.out_eq (f i), ← quot.out_eq (g i)] at this⟩
/-- The indexed supremum of cardinals is the smallest cardinal above
everything in the family. -/
noncomputable def sup {ι} (f : ι → cardinal) : cardinal :=
@min {c // ∀ i, f i ≤ c} ⟨⟨sum f, le_sum f⟩⟩ (λ a, a.1)
theorem le_sup {ι} (f : ι → cardinal) (i) : f i ≤ sup f :=
by dsimp [sup]; cases min_eq _ _ with c hc; rw hc; exact c.2 i
theorem sup_le {ι} {f : ι → cardinal} {a} : sup f ≤ a ↔ ∀ i, f i ≤ a :=
⟨λ h i, le_trans (le_sup _ _) h,
λ h, by dsimp [sup]; change a with (⟨a, h⟩:subtype _).1; apply min_le⟩
theorem sup_le_sup {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : sup f ≤ sup g :=
sup_le.2 $ λ i, le_trans (H i) (le_sup _ _)
theorem sup_le_sum {ι} (f : ι → cardinal) : sup f ≤ sum f :=
sup_le.2 $ le_sum _
theorem sum_le_sup {ι : Type u} (f : ι → cardinal.{u}) : sum f ≤ #ι * sup.{u u} f :=
by rw ← sum_const; exact sum_le_sum _ _ (le_sup _)
theorem sup_eq_zero {ι} {f : ι → cardinal} [is_empty ι] : sup f = 0 :=
by { rw [← nonpos_iff_eq_zero, sup_le], exact is_empty_elim }
/-- The indexed product of cardinals is the cardinality of the Pi type
(dependent product). -/
def prod {ι : Type u} (f : ι → cardinal) : cardinal := #(Π i, (f i).out)
@[simp] theorem prod_mk {ι} (f : ι → Type*) : prod (λ i, #(f i)) = #(Π i, f i) :=
quot.sound ⟨equiv.Pi_congr_right $ λ i,
classical.choice $ quotient.exact $ mk_out $ #(f i)⟩
theorem prod_const (ι : Type u) (a : cardinal.{u}) : prod (λ _:ι, a) = a ^ #ι :=
quotient.induction_on a $ by simp
theorem prod_le_prod {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : prod f ≤ prod g :=
⟨embedding.Pi_congr_right $ λ i, classical.choice $
by have := H i; rwa [← mk_out (f i), ← mk_out (g i)] at this⟩
theorem prod_eq_zero {ι} (f : ι → cardinal.{u}) : prod f = 0 ↔ ∃ i, f i = 0 :=
by { lift f to ι → Type u using λ _, trivial, simp [mk_eq_zero_iff] }
theorem prod_ne_zero {ι} (f : ι → cardinal) : prod f ≠ 0 ↔ ∀ i, f i ≠ 0 :=
by simp [prod_eq_zero]
@[simp] theorem lift_prod {ι : Type u} (c : ι → cardinal.{v}) :
lift.{w} (prod c) = prod (λ i, lift.{w} (c i)) :=
begin
lift c to ι → Type v using λ _, trivial,
simp only [prod_mk, ←mk_ulift],
exact cardinal.mk_congr (equiv.ulift.trans $ equiv.Pi_congr_right $ λ i, equiv.ulift.symm)
end
@[simp] theorem lift_min {ι I} (f : ι → cardinal) : lift (min I f) = min I (lift ∘ f) :=
le_antisymm (le_min.2 $ λ a, lift_le.2 $ min_le _ a) $
let ⟨i, e⟩ := min_eq I (lift ∘ f) in
by rw e; exact lift_le.2 (le_min.2 $ λ j, lift_le.1 $
by have := min_le (lift ∘ f) j; rwa e at this)
theorem lift_down {a : cardinal.{u}} {b : cardinal.{max u v}} :
b ≤ lift a → ∃ a', lift a' = b :=
quotient.induction_on₂ a b $ λ α β,
by dsimp; rw [← lift_id (#β), ← lift_umax, ← lift_umax.{u v}, lift_mk_le]; exact
λ ⟨f⟩, ⟨#(set.range f), eq.symm $ lift_mk_eq.2
⟨embedding.equiv_of_surjective
(embedding.cod_restrict _ f set.mem_range_self)
$ λ ⟨a, ⟨b, e⟩⟩, ⟨b, subtype.eq e⟩⟩⟩
theorem le_lift_iff {a : cardinal.{u}} {b : cardinal.{max u v}} :
b ≤ lift a ↔ ∃ a', lift a' = b ∧ a' ≤ a :=
⟨λ h, let ⟨a', e⟩ := lift_down h in ⟨a', e, lift_le.1 $ e.symm ▸ h⟩,
λ ⟨a', e, h⟩, e ▸ lift_le.2 h⟩
theorem lt_lift_iff {a : cardinal.{u}} {b : cardinal.{max u v}} :
b < lift a ↔ ∃ a', lift a' = b ∧ a' < a :=
⟨λ h, let ⟨a', e⟩ := lift_down (le_of_lt h) in
⟨a', e, lift_lt.1 $ e.symm ▸ h⟩,
λ ⟨a', e, h⟩, e ▸ lift_lt.2 h⟩
@[simp] theorem lift_succ (a) : lift (succ a) = succ (lift a) :=
le_antisymm
(le_of_not_gt $ λ h, begin
rcases lt_lift_iff.1 h with ⟨b, e, h⟩,
rw [lt_succ, ← lift_le, e] at h,
exact not_lt_of_le h (lt_succ_self _)
end)
(succ_le.2 $ lift_lt.2 $ lt_succ_self _)
@[simp] theorem lift_max {a : cardinal.{u}} {b : cardinal.{v}} :
lift.{(max v w)} a = lift.{(max u w)} b ↔ lift.{v} a = lift.{u} b :=
calc lift.{(max v w)} a = lift.{(max u w)} b
↔ lift.{w} (lift.{v} a) = lift.{w} (lift.{u} b) : by simp
... ↔ lift.{v} a = lift.{u} b : lift_inj
theorem mk_prod {α : Type u} {β : Type v} :
#(α × β) = lift.{v} (#α) * lift.{u} (#β) :=
quotient.sound ⟨equiv.prod_congr (equiv.ulift).symm (equiv.ulift).symm⟩
theorem sum_const_eq_lift_mul (ι : Type u) (a : cardinal.{v}) :
sum (λ _:ι, a) = lift.{v} (#ι) * lift.{u} a :=
begin
apply quotient.induction_on a,
intro α,
simp only [cardinal.mk_def, cardinal.sum_mk, cardinal.lift_id],
convert mk_prod using 1,
exact quotient.sound ⟨equiv.sigma_equiv_prod ι α⟩,
end
protected lemma le_sup_iff {ι : Type v} {f : ι → cardinal.{max v w}} {c : cardinal} :
(c ≤ sup f) ↔ (∀ b, (∀ i, f i ≤ b) → c ≤ b) :=
⟨λ h b hb, le_trans h (sup_le.mpr hb), λ h, h _ $ λ i, le_sup f i⟩
/-- The lift of a supremum is the supremum of the lifts. -/
lemma lift_sup {ι : Type v} (f : ι → cardinal.{max v w}) :
lift.{u} (sup.{v w} f) = sup.{v (max u w)} (λ i : ι, lift.{u} (f i)) :=
begin
apply le_antisymm,
{ rw [cardinal.le_sup_iff], intros c hc, by_contra h,
obtain ⟨d, rfl⟩ := cardinal.lift_down (not_le.mp h).le,
simp only [lift_le, sup_le] at h hc,
exact h hc },
{ simp only [cardinal.sup_le, lift_le, le_sup, implies_true_iff] }
end
/-- To prove that the lift of a supremum is bounded by some cardinal `t`,
it suffices to show that the lift of each cardinal is bounded by `t`. -/
lemma lift_sup_le {ι : Type v} (f : ι → cardinal.{max v w})
(t : cardinal.{max u v w}) (w : ∀ i, lift.{u} (f i) ≤ t) :
lift.{u} (sup f) ≤ t :=
by { rw lift_sup, exact sup_le.mpr w, }
@[simp] lemma lift_sup_le_iff {ι : Type v} (f : ι → cardinal.{max v w}) (t : cardinal.{max u v w}) :
lift.{u} (sup f) ≤ t ↔ ∀ i, lift.{u} (f i) ≤ t :=
⟨λ h i, (lift_le.mpr (le_sup f i)).trans h,
λ h, lift_sup_le f t h⟩
universes v' w'
/--
To prove an inequality between the lifts to a common universe of two different supremums,
it suffices to show that the lift of each cardinal from the smaller supremum
if bounded by the lift of some cardinal from the larger supremum.
-/
lemma lift_sup_le_lift_sup
{ι : Type v} {ι' : Type v'} (f : ι → cardinal.{max v w}) (f' : ι' → cardinal.{max v' w'})
(g : ι → ι') (h : ∀ i, lift.{(max v' w')} (f i) ≤ lift.{(max v w)} (f' (g i))) :
lift.{(max v' w')} (sup f) ≤ lift.{(max v w)} (sup f') :=
begin
apply lift_sup_le.{(max v' w')} f,
intro i,
apply le_trans (h i),
simp only [lift_le],
apply le_sup,
end
/-- A variant of `lift_sup_le_lift_sup` with universes specialized via `w = v` and `w' = v'`.
This is sometimes necessary to avoid universe unification issues. -/
lemma lift_sup_le_lift_sup'
{ι : Type v} {ι' : Type v'} (f : ι → cardinal.{v}) (f' : ι' → cardinal.{v'})
(g : ι → ι') (h : ∀ i, lift.{v'} (f i) ≤ lift.{v} (f' (g i))) :
lift.{v'} (sup.{v v} f) ≤ lift.{v} (sup.{v' v'} f') :=
lift_sup_le_lift_sup f f' g h
/-- `ω` is the smallest infinite cardinal, also known as ℵ₀. -/
def omega : cardinal.{u} := lift (#ℕ)
localized "notation `ω` := cardinal.omega" in cardinal
lemma mk_nat : #ℕ = ω := (lift_id _).symm
theorem omega_ne_zero : ω ≠ 0 := mk_ne_zero _
theorem omega_pos : 0 < ω :=
pos_iff_ne_zero.2 omega_ne_zero
@[simp] theorem lift_omega : lift ω = ω := lift_lift _
@[simp] theorem omega_le_lift {c : cardinal.{u}} : ω ≤ lift.{v} c ↔ ω ≤ c :=
by rw [← lift_omega, lift_le]
@[simp] theorem lift_le_omega {c : cardinal.{u}} : lift.{v} c ≤ ω ↔ c ≤ ω :=
by rw [← lift_omega, lift_le]
/- properties about the cast from nat -/
@[simp] theorem mk_fin : ∀ (n : ℕ), #(fin n) = n
| 0 := quotient.sound ⟨equiv.equiv_pempty _⟩
| (n+1) := by rw [nat.cast_succ, ← mk_fin]; exact
quotient.sound (fintype.card_eq.1 $ by simp)
@[simp] theorem lift_nat_cast (n : ℕ) : lift n = n :=
by induction n; simp *
lemma lift_eq_nat_iff {a : cardinal.{u}} {n : ℕ} : lift.{v} a = n ↔ a = n :=
by rw [← lift_nat_cast.{u v} n, lift_inj]
lemma nat_eq_lift_eq_iff {n : ℕ} {a : cardinal.{u}} :
(n : cardinal) = lift.{v} a ↔ (n : cardinal) = a :=
by rw [← lift_nat_cast.{u v} n, lift_inj]
theorem lift_mk_fin (n : ℕ) : lift (#(fin n)) = n := by simp
theorem fintype_card (α : Type u) [fintype α] : #α = fintype.card α :=
by rw [← lift_mk_fin.{u}, ← lift_id (#α), lift_mk_eq.{u 0 u}];
exact fintype.card_eq.1 (by simp)
theorem card_le_of_finset {α} (s : finset α) :
(s.card : cardinal) ≤ #α :=
begin
rw (_ : (s.card : cardinal) = #s),
{ exact ⟨function.embedding.subtype _⟩ },
rw [cardinal.fintype_card, fintype.card_coe]
end
@[simp, norm_cast] theorem nat_cast_pow {m n : ℕ} : (↑(pow m n) : cardinal) = m ^ n :=
by induction n; simp [pow_succ', -_root_.add_comm, power_add, *]
@[simp, norm_cast] theorem nat_cast_le {m n : ℕ} : (m : cardinal) ≤ n ↔ m ≤ n :=
by rw [← lift_mk_fin, ← lift_mk_fin, lift_le]; exact
⟨λ ⟨⟨f, hf⟩⟩, by simpa only [fintype.card_fin] using fintype.card_le_of_injective f hf,
λ h, ⟨(fin.cast_le h).to_embedding⟩⟩
@[simp, norm_cast] theorem nat_cast_lt {m n : ℕ} : (m : cardinal) < n ↔ m < n :=
by simp [lt_iff_le_not_le, -not_le]
instance : char_zero cardinal := ⟨strict_mono.injective $ λ m n, nat_cast_lt.2⟩
theorem nat_cast_inj {m n : ℕ} : (m : cardinal) = n ↔ m = n := nat.cast_inj
lemma nat_cast_injective : injective (coe : ℕ → cardinal) :=
nat.cast_injective
@[simp, norm_cast, priority 900] theorem nat_succ (n : ℕ) : (n.succ : cardinal) = succ n :=
le_antisymm (add_one_le_succ _) (succ_le.2 $ nat_cast_lt.2 $ nat.lt_succ_self _)
@[simp] theorem succ_zero : succ 0 = 1 :=
by norm_cast
theorem card_le_of {α : Type u} {n : ℕ} (H : ∀ s : finset α, s.card ≤ n) :
# α ≤ n :=
begin
refine lt_succ.1 (lt_of_not_ge $ λ hn, _),
rw [← cardinal.nat_succ, ← cardinal.lift_mk_fin n.succ] at hn,
cases hn with f,
refine not_lt_of_le (H $ finset.univ.map f) _,
rw [finset.card_map, ← fintype.card, fintype.card_ulift, fintype.card_fin],
exact n.lt_succ_self
end
theorem cantor' (a) {b : cardinal} (hb : 1 < b) : a < b ^ a :=
by rw [← succ_le, (by norm_cast : succ 1 = 2)] at hb;
exact lt_of_lt_of_le (cantor _) (power_le_power_right hb)
theorem one_le_iff_pos {c : cardinal} : 1 ≤ c ↔ 0 < c :=
by rw [← succ_zero, succ_le]
theorem one_le_iff_ne_zero {c : cardinal} : 1 ≤ c ↔ c ≠ 0 :=
by rw [one_le_iff_pos, pos_iff_ne_zero]
theorem nat_lt_omega (n : ℕ) : (n : cardinal.{u}) < ω :=
succ_le.1 $ by rw [← nat_succ, ← lift_mk_fin, omega, lift_mk_le.{0 0 u}]; exact
⟨⟨coe, λ a b, fin.ext⟩⟩
@[simp] theorem one_lt_omega : 1 < ω :=
by simpa using nat_lt_omega 1
theorem lt_omega {c : cardinal.{u}} : c < ω ↔ ∃ n : ℕ, c = n :=
⟨λ h, begin
rcases lt_lift_iff.1 h with ⟨c, rfl, h'⟩,
rcases le_mk_iff_exists_set.1 h'.1 with ⟨S, rfl⟩,
suffices : finite S,
{ cases this, resetI,
existsi fintype.card S,
rw [← lift_nat_cast.{0 u}, lift_inj, fintype_card S] },
by_contra nf,
have P : ∀ (n : ℕ) (IH : ∀ i<n, S), ∃ a : S, ¬ ∃ y h, IH y h = a :=
λ n IH,
let g : {i | i < n} → S := λ ⟨i, h⟩, IH i h in
not_forall.1 (λ h, nf
⟨fintype.of_surjective g (λ a, subtype.exists.2 (h a))⟩),
let F : ℕ → S := nat.lt_wf.fix (λ n IH, classical.some (P n IH)),
refine not_le_of_lt h' ⟨⟨F, _⟩⟩,
suffices : ∀ (n : ℕ) (m < n), F m ≠ F n,
{ refine λ m n, not_imp_not.1 (λ ne, _),
rcases lt_trichotomy m n with h|h|h,
{ exact this n m h },
{ contradiction },
{ exact (this m n h).symm } },
intros n m h,
have := classical.some_spec (P n (λ y _, F y)),
rw [← show F n = classical.some (P n (λ y _, F y)),
from nat.lt_wf.fix_eq (λ n IH, classical.some (P n IH)) n] at this,
exact λ e, this ⟨m, h, e⟩,
end, λ ⟨n, e⟩, e.symm ▸ nat_lt_omega _⟩
theorem omega_le {c : cardinal.{u}} : ω ≤ c ↔ ∀ n : ℕ, (n:cardinal) ≤ c :=
⟨λ h n, le_trans (le_of_lt (nat_lt_omega _)) h,
λ h, le_of_not_lt $ λ hn, begin
rcases lt_omega.1 hn with ⟨n, rfl⟩,
exact not_le_of_lt (nat.lt_succ_self _) (nat_cast_le.1 (h (n+1)))
end⟩
theorem lt_omega_iff_fintype {α : Type u} : #α < ω ↔ nonempty (fintype α) :=
lt_omega.trans ⟨λ ⟨n, e⟩, begin
rw [← lift_mk_fin n] at e,
cases quotient.exact e with f,
exact ⟨fintype.of_equiv _ f.symm⟩
end, λ ⟨_⟩, by exactI ⟨_, fintype_card _⟩⟩
theorem lt_omega_iff_finite {α} {S : set α} : #S < ω ↔ finite S :=
lt_omega_iff_fintype.trans finite_def.symm
instance can_lift_cardinal_nat : can_lift cardinal ℕ :=
⟨ coe, λ x, x < ω, λ x hx, let ⟨n, hn⟩ := lt_omega.mp hx in ⟨n, hn.symm⟩⟩
theorem add_lt_omega {a b : cardinal} (ha : a < ω) (hb : b < ω) : a + b < ω :=
match a, b, lt_omega.1 ha, lt_omega.1 hb with
| _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat.cast_add]; apply nat_lt_omega
end
lemma add_lt_omega_iff {a b : cardinal} : a + b < ω ↔ a < ω ∧ b < ω :=
⟨λ h, ⟨lt_of_le_of_lt (self_le_add_right _ _) h, lt_of_le_of_lt (self_le_add_left _ _) h⟩,
λ⟨h1, h2⟩, add_lt_omega h1 h2⟩
lemma omega_le_add_iff {a b : cardinal} : ω ≤ a + b ↔ ω ≤ a ∨ ω ≤ b :=
by simp only [← not_lt, add_lt_omega_iff, not_and_distrib]
theorem mul_lt_omega {a b : cardinal} (ha : a < ω) (hb : b < ω) : a * b < ω :=
match a, b, lt_omega.1 ha, lt_omega.1 hb with
| _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat.cast_mul]; apply nat_lt_omega
end
lemma mul_lt_omega_iff {a b : cardinal} : a * b < ω ↔ a = 0 ∨ b = 0 ∨ a < ω ∧ b < ω :=
begin
split,
{ intro h, by_cases ha : a = 0, { left, exact ha },
right, by_cases hb : b = 0, { left, exact hb },
right, rw [← ne, ← one_le_iff_ne_zero] at ha hb, split,
{ rw [← mul_one a],
refine lt_of_le_of_lt (mul_le_mul' (le_refl a) hb) h },
{ rw [← _root_.one_mul b],
refine lt_of_le_of_lt (mul_le_mul' ha (le_refl b)) h }},
rintro (rfl|rfl|⟨ha,hb⟩); simp only [*, mul_lt_omega, omega_pos, _root_.zero_mul, mul_zero]
end
lemma mul_lt_omega_iff_of_ne_zero {a b : cardinal} (ha : a ≠ 0) (hb : b ≠ 0) :
a * b < ω ↔ a < ω ∧ b < ω :=
by simp [mul_lt_omega_iff, ha, hb]
theorem power_lt_omega {a b : cardinal} (ha : a < ω) (hb : b < ω) : a ^ b < ω :=
match a, b, lt_omega.1 ha, lt_omega.1 hb with
| _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat_cast_pow]; apply nat_lt_omega
end
lemma eq_one_iff_unique {α : Type*} :
#α = 1 ↔ subsingleton α ∧ nonempty α :=
calc #α = 1 ↔ #α ≤ 1 ∧ ¬#α < 1 : eq_iff_le_not_lt
... ↔ subsingleton α ∧ nonempty α :
begin
apply and_congr le_one_iff_subsingleton,
push_neg,
rw [one_le_iff_ne_zero, mk_ne_zero_iff]
end
theorem infinite_iff {α : Type u} : infinite α ↔ ω ≤ #α :=
by rw [←not_lt, lt_omega_iff_fintype, not_nonempty_iff, is_empty_fintype]
lemma omega_le_mk (α : Type u) [infinite α] : ω ≤ #α := infinite_iff.1 ‹_›
lemma encodable_iff {α : Type u} : nonempty (encodable α) ↔ #α ≤ ω :=
⟨λ ⟨h⟩, ⟨(@encodable.encode' α h).trans equiv.ulift.symm.to_embedding⟩,
λ ⟨h⟩, ⟨encodable.of_inj _ (h.trans equiv.ulift.to_embedding).injective⟩⟩
@[simp] lemma mk_le_omega [encodable α] : #α ≤ ω := encodable_iff.1 ⟨‹_›⟩
lemma denumerable_iff {α : Type u} : nonempty (denumerable α) ↔ #α = ω :=
⟨λ⟨h⟩, quotient.sound $ by exactI ⟨ (denumerable.eqv α).trans equiv.ulift.symm ⟩,
λ h, by { cases quotient.exact h with f, exact ⟨denumerable.mk' $ f.trans equiv.ulift⟩ }⟩
@[simp] lemma mk_denumerable (α : Type u) [denumerable α] : #α = ω :=
denumerable_iff.1 ⟨‹_›⟩
lemma countable_iff (s : set α) : countable s ↔ #s ≤ ω :=
begin
rw [countable_iff_exists_injective], split,
rintro ⟨f, hf⟩, exact ⟨embedding.trans ⟨f, hf⟩ equiv.ulift.symm.to_embedding⟩,
rintro ⟨f'⟩, cases embedding.trans f' equiv.ulift.to_embedding with f hf, exact ⟨f, hf⟩
end
/-- This function sends finite cardinals to the corresponding natural, and infinite cardinals
to 0. -/
noncomputable def to_nat : zero_hom cardinal ℕ :=
⟨λ c, if h : c < omega.{v} then classical.some (lt_omega.1 h) else 0,
begin
have h : 0 < ω := nat_lt_omega 0,
rw [dif_pos h, ← cardinal.nat_cast_inj, ← classical.some_spec (lt_omega.1 h), nat.cast_zero],
end⟩
lemma to_nat_apply_of_lt_omega {c : cardinal} (h : c < ω) :
c.to_nat = classical.some (lt_omega.1 h) :=
dif_pos h
@[simp]
lemma to_nat_apply_of_omega_le {c : cardinal} (h : ω ≤ c) :
c.to_nat = 0 :=
dif_neg (not_lt_of_le h)
@[simp]
lemma cast_to_nat_of_lt_omega {c : cardinal} (h : c < ω) :
↑c.to_nat = c :=
by rw [to_nat_apply_of_lt_omega h, ← classical.some_spec (lt_omega.1 h)]
@[simp]
lemma cast_to_nat_of_omega_le {c : cardinal} (h : ω ≤ c) :
↑c.to_nat = (0 : cardinal) :=
by rw [to_nat_apply_of_omega_le h, nat.cast_zero]
@[simp]
lemma to_nat_cast (n : ℕ) : cardinal.to_nat n = n :=
begin
rw [to_nat_apply_of_lt_omega (nat_lt_omega n), ← nat_cast_inj],
exact (classical.some_spec (lt_omega.1 (nat_lt_omega n))).symm,
end
/-- `to_nat` has a right-inverse: coercion. -/
lemma to_nat_right_inverse : function.right_inverse (coe : ℕ → cardinal) to_nat := to_nat_cast
lemma to_nat_surjective : surjective to_nat := to_nat_right_inverse.surjective
@[simp]
lemma mk_to_nat_of_infinite [h : infinite α] : (#α).to_nat = 0 :=
dif_neg (not_lt_of_le (infinite_iff.1 h))
@[simp]
lemma mk_to_nat_eq_card [fintype α] : (#α).to_nat = fintype.card α :=
by simp [fintype_card]
@[simp]
lemma zero_to_nat : cardinal.to_nat 0 = 0 :=
by rw [← to_nat_cast 0, nat.cast_zero]
@[simp]
lemma one_to_nat : cardinal.to_nat 1 = 1 :=
by rw [← to_nat_cast 1, nat.cast_one]
@[simp] lemma to_nat_lift (c : cardinal.{v}) : (lift.{u v} c).to_nat = c.to_nat :=
begin
apply nat_cast_injective,
cases lt_or_ge c ω with hc hc,
{ rw [cast_to_nat_of_lt_omega, ←lift_nat_cast, cast_to_nat_of_lt_omega hc],
rwa [←lift_omega, lift_lt] },
{ rw [cast_to_nat_of_omega_le, ←lift_nat_cast, cast_to_nat_of_omega_le hc, lift_zero],
rwa [←lift_omega, lift_le] },
end
lemma to_nat_congr {β : Type v} (e : α ≃ β) : (#α).to_nat = (#β).to_nat :=
by rw [←to_nat_lift, lift_mk_eq.mpr ⟨e⟩, to_nat_lift]
lemma to_nat_mul (x y : cardinal) : (x * y).to_nat = x.to_nat * y.to_nat :=
begin
by_cases hx1 : x = 0,
{ rw [comm_semiring.mul_comm, hx1, mul_zero, zero_to_nat, nat.zero_mul] },
by_cases hy1 : y = 0,
{ rw [hy1, zero_to_nat, mul_zero, mul_zero, zero_to_nat] },
refine nat_cast_injective (eq.trans _ (nat.cast_mul _ _).symm),
cases lt_or_ge x ω with hx2 hx2,
{ cases lt_or_ge y ω with hy2 hy2,
{ rw [cast_to_nat_of_lt_omega, cast_to_nat_of_lt_omega hx2, cast_to_nat_of_lt_omega hy2],
exact mul_lt_omega hx2 hy2 },
{ rw [cast_to_nat_of_omega_le hy2, mul_zero, cast_to_nat_of_omega_le],
exact not_lt.mp (mt (mul_lt_omega_iff_of_ne_zero hx1 hy1).mp (λ h, not_lt.mpr hy2 h.2)) } },
{ rw [cast_to_nat_of_omega_le hx2, _root_.zero_mul, cast_to_nat_of_omega_le],
exact not_lt.mp (mt (mul_lt_omega_iff_of_ne_zero hx1 hy1).mp (λ h, not_lt.mpr hx2 h.1)) },
end
/-- This function sends finite cardinals to the corresponding natural, and infinite cardinals
to `⊤`. -/
noncomputable def to_enat : cardinal →+ enat :=
{ to_fun := λ c, if c < omega.{v} then c.to_nat else ⊤,
map_zero' := by simp [if_pos (lt_trans zero_lt_one one_lt_omega)],
map_add' := λ x y, begin
by_cases hx : x < ω,
{ obtain ⟨x0, rfl⟩ := lt_omega.1 hx,
by_cases hy : y < ω,
{ obtain ⟨y0, rfl⟩ := lt_omega.1 hy,
simp only [add_lt_omega hx hy, hx, hy, to_nat_cast, if_true],
rw [← nat.cast_add, to_nat_cast, nat.cast_add] },
{ rw [if_neg hy, if_neg, enat.add_top],
contrapose! hy,
apply lt_of_le_of_lt le_add_self hy } },
{ rw [if_neg hx, if_neg, enat.top_add],
contrapose! hx,
apply lt_of_le_of_lt le_self_add hx },
end }
@[simp]
lemma to_enat_apply_of_lt_omega {c : cardinal} (h : c < ω) :
c.to_enat = c.to_nat :=
if_pos h
@[simp]
lemma to_enat_apply_of_omega_le {c : cardinal} (h : ω ≤ c) :
c.to_enat = ⊤ :=
if_neg (not_lt_of_le h)
@[simp]
lemma to_enat_cast (n : ℕ) : cardinal.to_enat n = n :=
by rw [to_enat_apply_of_lt_omega (nat_lt_omega n), to_nat_cast]
@[simp]
lemma mk_to_enat_of_infinite [h : infinite α] : (#α).to_enat = ⊤ :=
to_enat_apply_of_omega_le (infinite_iff.1 h)
lemma to_enat_surjective : surjective to_enat :=
begin
intro x,
exact enat.cases_on x ⟨ω, to_enat_apply_of_omega_le (le_refl ω)⟩
(λ n, ⟨n, to_enat_cast n⟩),
end
@[simp]
lemma mk_to_enat_eq_coe_card [fintype α] : (#α).to_enat = fintype.card α :=
by simp [fintype_card]
lemma mk_int : #ℤ = ω := mk_denumerable ℤ
lemma mk_pnat : #ℕ+ = ω := mk_denumerable ℕ+
lemma two_le_iff : (2 : cardinal) ≤ #α ↔ ∃x y : α, x ≠ y :=
begin
split,
{ rintro ⟨f⟩, refine ⟨f $ sum.inl ⟨⟩, f $ sum.inr ⟨⟩, _⟩, intro h, cases f.2 h },
{ rintro ⟨x, y, h⟩, by_contra h',
rw [not_le, ←nat.cast_two, nat_succ, lt_succ, nat.cast_one, le_one_iff_subsingleton] at h',
apply h, exactI subsingleton.elim _ _ }
end
lemma two_le_iff' (x : α) : (2 : cardinal) ≤ #α ↔ ∃y : α, x ≠ y :=
begin
rw [two_le_iff],
split,
{ rintro ⟨y, z, h⟩, refine classical.by_cases (λ(h' : x = y), _) (λ h', ⟨y, h'⟩),
rw [←h'] at h, exact ⟨z, h⟩ },
{ rintro ⟨y, h⟩, exact ⟨x, y, h⟩ }
end
/-- **König's theorem** -/
theorem sum_lt_prod {ι} (f g : ι → cardinal) (H : ∀ i, f i < g i) : sum f < prod g :=
lt_of_not_ge $ λ ⟨F⟩, begin
haveI : inhabited (Π (i : ι), (g i).out),
{ refine ⟨λ i, classical.choice $ mk_ne_zero_iff.1 _⟩,
rw mk_out,
exact (H i).ne_bot },
let G := inv_fun F,
have sG : surjective G := inv_fun_surjective F.2,
choose C hc using show ∀ i, ∃ b, ∀ a, G ⟨i, a⟩ i ≠ b,
{ assume i,
simp only [- not_exists, not_exists.symm, not_forall.symm],
refine λ h, not_le_of_lt (H i) _,
rw [← mk_out (f i), ← mk_out (g i)],
exact ⟨embedding.of_surjective _ h⟩ },
exact (let ⟨⟨i, a⟩, h⟩ := sG C in hc i a (congr_fun h _))
end
@[simp] theorem mk_empty : #empty = 0 :=
fintype_card empty
@[simp] theorem mk_pempty : #pempty = 0 :=
fintype_card pempty
theorem mk_unit : #unit = 1 :=
(fintype_card unit).trans nat.cast_one
@[simp] theorem mk_punit : #punit = 1 :=
(fintype_card punit).trans nat.cast_one
@[simp] theorem mk_singleton {α : Type u} (x : α) : #({x} : set α) = 1 :=
quotient.sound ⟨equiv.set.singleton x⟩
@[simp] theorem mk_plift_of_true {p : Prop} (h : p) : #(plift p) = 1 :=
quotient.sound ⟨equiv.plift.trans $ equiv.prop_equiv_punit h⟩
@[simp] theorem mk_plift_of_false {p : Prop} (h : ¬ p) : #(plift p) = 0 :=
quotient.sound ⟨equiv.plift.trans $ equiv.prop_equiv_pempty h⟩
@[simp] theorem mk_bool : #bool = 2 :=
quotient.sound ⟨equiv.bool_equiv_punit_sum_punit⟩
@[simp] theorem mk_Prop : #Prop = 2 :=
(quotient.sound ⟨equiv.Prop_equiv_bool⟩ : #Prop = #bool).trans mk_bool
@[simp] theorem mk_set {α : Type u} : #(set α) = 2 ^ #α :=
begin
rw [← prop_eq_two, cardinal.power_def (ulift Prop) α, cardinal.eq],
exact ⟨equiv.arrow_congr (equiv.refl _) equiv.ulift.symm⟩,
end
@[simp] theorem mk_option {α : Type u} : #(option α) = #α + 1 :=
quotient.sound ⟨equiv.option_equiv_sum_punit α⟩
theorem mk_list_eq_sum_pow (α : Type u) : #(list α) = sum (λ n : ℕ, (#α)^(n:cardinal.{u})) :=
calc #(list α)
= #(Σ n, vector α n) : quotient.sound ⟨(equiv.sigma_preimage_equiv list.length).symm⟩
... = #(Σ n, fin n → α) : quotient.sound ⟨equiv.sigma_congr_right $ λ n,
⟨vector.nth, vector.of_fn, vector.of_fn_nth, λ f, funext $ vector.nth_of_fn f⟩⟩
... = #(Σ n : ℕ, ulift.{u} (fin n) → α) : quotient.sound ⟨equiv.sigma_congr_right $ λ n,
equiv.arrow_congr equiv.ulift.symm (equiv.refl α)⟩
... = sum (λ n : ℕ, (#α)^(n:cardinal.{u})) :
by simp only [(lift_mk_fin _).symm, ←mk_ulift, power_def, sum_mk]
theorem mk_quot_le {α : Type u} {r : α → α → Prop} : #(quot r) ≤ #α :=
mk_le_of_surjective quot.exists_rep
theorem mk_quotient_le {α : Type u} {s : setoid α} : #(quotient s) ≤ #α :=
mk_quot_le
theorem mk_subtype_le {α : Type u} (p : α → Prop) : #(subtype p) ≤ #α :=
⟨embedding.subtype p⟩
theorem mk_subtype_le_of_subset {α : Type u} {p q : α → Prop} (h : ∀ ⦃x⦄, p x → q x) :
#(subtype p) ≤ #(subtype q) :=
⟨embedding.subtype_map (embedding.refl α) h⟩
@[simp] theorem mk_emptyc (α : Type u) : #(∅ : set α) = 0 :=
quotient.sound ⟨equiv.set.pempty α⟩
lemma mk_emptyc_iff {α : Type u} {s : set α} : #s = 0 ↔ s = ∅ :=
begin
split,
{ intro h,
have h2 : #s = #pempty, by simp [h],
refine set.eq_empty_iff_forall_not_mem.mpr (λ _ hx, _),
rcases cardinal.eq.mp h2 with ⟨f, _⟩,
cases f ⟨_, hx⟩ },
{ intro, convert mk_emptyc _ }
end
@[simp] theorem mk_univ {α : Type u} : #(@univ α) = #α :=
quotient.sound ⟨equiv.set.univ α⟩
theorem mk_image_le {α β : Type u} {f : α → β} {s : set α} : #(f '' s) ≤ #s :=
mk_le_of_surjective surjective_onto_image
theorem mk_image_le_lift {α : Type u} {β : Type v} {f : α → β} {s : set α} :
lift.{u} (#(f '' s)) ≤ lift.{v} (#s) :=
lift_mk_le.{v u 0}.mpr ⟨embedding.of_surjective _ surjective_onto_image⟩
theorem mk_range_le {α β : Type u} {f : α → β} : #(range f) ≤ #α :=
mk_le_of_surjective surjective_onto_range
theorem mk_range_le_lift {α : Type u} {β : Type v} {f : α → β} :
lift.{u} (#(range f)) ≤ lift.{v} (#α) :=
lift_mk_le.{v u 0}.mpr ⟨embedding.of_surjective _ surjective_onto_range⟩
lemma mk_range_eq (f : α → β) (h : injective f) : #(range f) = #α :=
quotient.sound ⟨(equiv.of_injective f h).symm⟩
lemma mk_range_eq_of_injective {α : Type u} {β : Type v} {f : α → β} (hf : injective f) :
lift.{u} (#(range f)) = lift.{v} (#α) :=
begin
have := (@lift_mk_eq.{v u max u v} (range f) α).2 ⟨(equiv.of_injective f hf).symm⟩,
simp only [lift_umax.{u v}, lift_umax.{v u}] at this,
exact this
end
lemma mk_range_eq_lift {α : Type u} {β : Type v} {f : α → β} (hf : injective f) :
lift.{(max u w)} (# (range f)) = lift.{(max v w)} (# α) :=
lift_mk_eq.mpr ⟨(equiv.of_injective f hf).symm⟩
theorem mk_image_eq {α β : Type u} {f : α → β} {s : set α} (hf : injective f) :
#(f '' s) = #s :=
quotient.sound ⟨(equiv.set.image f s hf).symm⟩
theorem mk_Union_le_sum_mk {α ι : Type u} {f : ι → set α} : #(⋃ i, f i) ≤ sum (λ i, #(f i)) :=
calc #(⋃ i, f i) ≤ #(Σ i, f i) : mk_le_of_surjective (set.sigma_to_Union_surjective f)
... = sum (λ i, #(f i)) : (sum_mk _).symm
theorem mk_Union_eq_sum_mk {α ι : Type u} {f : ι → set α} (h : ∀i j, i ≠ j → disjoint (f i) (f j)) :
#(⋃ i, f i) = sum (λ i, #(f i)) :=
calc #(⋃ i, f i) = #(Σ i, f i) : quot.sound ⟨set.Union_eq_sigma_of_disjoint h⟩
... = sum (λi, #(f i)) : (sum_mk _).symm
lemma mk_Union_le {α ι : Type u} (f : ι → set α) :
#(⋃ i, f i) ≤ #ι * cardinal.sup.{u u} (λ i, #(f i)) :=
le_trans mk_Union_le_sum_mk (sum_le_sup _)
lemma mk_sUnion_le {α : Type u} (A : set (set α)) :
#(⋃₀ A) ≤ #A * cardinal.sup.{u u} (λ s : A, #s) :=
by { rw [sUnion_eq_Union], apply mk_Union_le }
lemma mk_bUnion_le {ι α : Type u} (A : ι → set α) (s : set ι) :
#(⋃(x ∈ s), A x) ≤ #s * cardinal.sup.{u u} (λ x : s, #(A x.1)) :=
by { rw [bUnion_eq_Union], apply mk_Union_le }
@[simp] lemma finset_card {α : Type u} {s : finset α} : ↑(finset.card s) = #s :=
by rw [fintype_card, nat_cast_inj, fintype.card_coe]
lemma finset_card_lt_omega (s : finset α) : #(↑s : set α) < ω :=
by { rw [lt_omega_iff_fintype], exact ⟨finset.subtype.fintype s⟩ }
theorem mk_eq_nat_iff_finset {α} {s : set α} {n : ℕ} :
#s = n ↔ ∃ t : finset α, (t : set α) = s ∧ t.card = n :=
begin
split,
{ intro h,
have : # s < omega, by { rw h, exact nat_lt_omega n },
refine ⟨(lt_omega_iff_finite.1 this).to_finset, finite.coe_to_finset _, nat_cast_inj.1 _⟩,
rwa [finset_card, finite.coe_sort_to_finset] },
{ rintro ⟨t, rfl, rfl⟩,
exact finset_card.symm }
end
theorem mk_union_add_mk_inter {α : Type u} {S T : set α} :
#(S ∪ T : set α) + #(S ∩ T : set α) = #S + #T :=
quot.sound ⟨equiv.set.union_sum_inter S T⟩
/-- The cardinality of a union is at most the sum of the cardinalities
of the two sets. -/
lemma mk_union_le {α : Type u} (S T : set α) : #(S ∪ T : set α) ≤ #S + #T :=
@mk_union_add_mk_inter α S T ▸ self_le_add_right (#(S ∪ T : set α)) (#(S ∩ T : set α))
theorem mk_union_of_disjoint {α : Type u} {S T : set α} (H : disjoint S T) :
#(S ∪ T : set α) = #S + #T :=
quot.sound ⟨equiv.set.union H⟩
theorem mk_insert {α : Type u} {s : set α} {a : α} (h : a ∉ s) :
#(insert a s : set α) = #s + 1 :=
by { rw [← union_singleton, mk_union_of_disjoint, mk_singleton], simpa }
lemma mk_sum_compl {α} (s : set α) : #s + #(sᶜ : set α) = #α :=
quotient.sound ⟨equiv.set.sum_compl s⟩
lemma mk_le_mk_of_subset {α} {s t : set α} (h : s ⊆ t) : #s ≤ #t :=
⟨set.embedding_of_subset s t h⟩
lemma mk_subtype_mono {p q : α → Prop} (h : ∀x, p x → q x) : #{x // p x} ≤ #{x // q x} :=
⟨embedding_of_subset _ _ h⟩
lemma mk_set_le (s : set α) : #s ≤ #α :=
mk_subtype_le s
lemma mk_image_eq_lift {α : Type u} {β : Type v} (f : α → β) (s : set α) (h : injective f) :
lift.{u} (#(f '' s)) = lift.{v} (#s) :=
lift_mk_eq.{v u 0}.mpr ⟨(equiv.set.image f s h).symm⟩
lemma mk_image_eq_of_inj_on_lift {α : Type u} {β : Type v} (f : α → β) (s : set α)
(h : inj_on f s) : lift.{u} (#(f '' s)) = lift.{v} (#s) :=
lift_mk_eq.{v u 0}.mpr ⟨(equiv.set.image_of_inj_on f s h).symm⟩
lemma mk_image_eq_of_inj_on {α β : Type u} (f : α → β) (s : set α) (h : inj_on f s) :
#(f '' s) = #s :=
quotient.sound ⟨(equiv.set.image_of_inj_on f s h).symm⟩
lemma mk_subtype_of_equiv {α β : Type u} (p : β → Prop) (e : α ≃ β) :
#{a : α // p (e a)} = #{b : β // p b} :=
quotient.sound ⟨equiv.subtype_equiv_of_subtype e⟩
lemma mk_sep (s : set α) (t : α → Prop) : #({ x ∈ s | t x } : set α) = #{ x : s | t x.1 } :=
quotient.sound ⟨equiv.set.sep s t⟩
lemma mk_preimage_of_injective_lift {α : Type u} {β : Type v} (f : α → β) (s : set β)
(h : injective f) : lift.{v} (#(f ⁻¹' s)) ≤ lift.{u} (#s) :=
begin
rw lift_mk_le.{u v 0}, use subtype.coind (λ x, f x.1) (λ x, x.2),
apply subtype.coind_injective, exact h.comp subtype.val_injective
end
lemma mk_preimage_of_subset_range_lift {α : Type u} {β : Type v} (f : α → β) (s : set β)
(h : s ⊆ range f) : lift.{u} (#s) ≤ lift.{v} (#(f ⁻¹' s)) :=
begin
rw lift_mk_le.{v u 0},
refine ⟨⟨_, _⟩⟩,
{ rintro ⟨y, hy⟩, rcases classical.subtype_of_exists (h hy) with ⟨x, rfl⟩, exact ⟨x, hy⟩ },
rintro ⟨y, hy⟩ ⟨y', hy'⟩, dsimp,
rcases classical.subtype_of_exists (h hy) with ⟨x, rfl⟩,
rcases classical.subtype_of_exists (h hy') with ⟨x', rfl⟩,
simp, intro hxx', rw hxx'
end
lemma mk_preimage_of_injective_of_subset_range_lift {β : Type v} (f : α → β) (s : set β)
(h : injective f) (h2 : s ⊆ range f) : lift.{v} (#(f ⁻¹' s)) = lift.{u} (#s) :=
le_antisymm (mk_preimage_of_injective_lift f s h) (mk_preimage_of_subset_range_lift f s h2)
lemma mk_preimage_of_injective (f : α → β) (s : set β) (h : injective f) :
#(f ⁻¹' s) ≤ #s :=
by { convert mk_preimage_of_injective_lift.{u u} f s h using 1; rw [lift_id] }
lemma mk_preimage_of_subset_range (f : α → β) (s : set β)
(h : s ⊆ range f) : #s ≤ #(f ⁻¹' s) :=
by { convert mk_preimage_of_subset_range_lift.{u u} f s h using 1; rw [lift_id] }
lemma mk_preimage_of_injective_of_subset_range (f : α → β) (s : set β)
(h : injective f) (h2 : s ⊆ range f) : #(f ⁻¹' s) = #s :=
by { convert mk_preimage_of_injective_of_subset_range_lift.{u u} f s h h2 using 1; rw [lift_id] }
lemma mk_subset_ge_of_subset_image_lift {α : Type u} {β : Type v} (f : α → β) {s : set α}
{t : set β} (h : t ⊆ f '' s) :
lift.{u} (#t) ≤ lift.{v} (#({ x ∈ s | f x ∈ t } : set α)) :=
by { rw [image_eq_range] at h, convert mk_preimage_of_subset_range_lift _ _ h using 1,
rw [mk_sep], refl }
lemma mk_subset_ge_of_subset_image (f : α → β) {s : set α} {t : set β} (h : t ⊆ f '' s) :
#t ≤ #({ x ∈ s | f x ∈ t } : set α) :=
by { rw [image_eq_range] at h, convert mk_preimage_of_subset_range _ _ h using 1,
rw [mk_sep], refl }
theorem le_mk_iff_exists_subset {c : cardinal} {α : Type u} {s : set α} :
c ≤ #s ↔ ∃ p : set α, p ⊆ s ∧ #p = c :=
begin
rw [le_mk_iff_exists_set, ←subtype.exists_set_subtype],
apply exists_congr, intro t, rw [mk_image_eq], apply subtype.val_injective
end
/-- The function α^{<β}, defined to be sup_{γ < β} α^γ.
We index over {s : set β.out // #s < β } instead of {γ // γ < β}, because the latter lives in a
higher universe -/
noncomputable def powerlt (α β : cardinal.{u}) : cardinal.{u} :=
sup.{u u} (λ(s : {s : set β.out // #s < β}), α ^ mk.{u} s)
infix ` ^< `:80 := powerlt
theorem powerlt_aux {c c' : cardinal} (h : c < c') :
∃(s : {s : set c'.out // #s < c'}), #s = c :=
begin
cases out_embedding.mp (le_of_lt h) with f,
have : #↥(range ⇑f) = c, { rwa [mk_range_eq, mk, quotient.out_eq c], exact f.2 },
exact ⟨⟨range f, by convert h⟩, this⟩
end
lemma le_powerlt {c₁ c₂ c₃ : cardinal} (h : c₂ < c₃) : c₁ ^ c₂ ≤ c₁ ^< c₃ :=
by { rcases powerlt_aux h with ⟨s, rfl⟩, apply le_sup _ s }
lemma powerlt_le {c₁ c₂ c₃ : cardinal} : c₁ ^< c₂ ≤ c₃ ↔ ∀(c₄ < c₂), c₁ ^ c₄ ≤ c₃ :=
begin
rw [powerlt, sup_le],
split,
{ intros h c₄ hc₄, rcases powerlt_aux hc₄ with ⟨s, rfl⟩, exact h s },
intros h s, exact h _ s.2
end
lemma powerlt_le_powerlt_left {a b c : cardinal} (h : b ≤ c) : a ^< b ≤ a ^< c :=
by { rw [powerlt, sup_le], rintro ⟨s, hs⟩, apply le_powerlt, exact lt_of_lt_of_le hs h }
lemma powerlt_succ {c₁ c₂ : cardinal} (h : c₁ ≠ 0) : c₁ ^< c₂.succ = c₁ ^ c₂ :=
begin
apply le_antisymm,
{ rw powerlt_le, intros c₃ h2, apply power_le_power_left h, rwa [←lt_succ] },
{ apply le_powerlt, apply lt_succ_self }
end
lemma powerlt_max {c₁ c₂ c₃ : cardinal} : c₁ ^< max c₂ c₃ = max (c₁ ^< c₂) (c₁ ^< c₃) :=
by { cases le_total c₂ c₃; simp only [max_eq_left, max_eq_right, h, powerlt_le_powerlt_left] }
lemma zero_powerlt {a : cardinal} (h : a ≠ 0) : 0 ^< a = 1 :=
begin
apply le_antisymm,
{ rw [powerlt_le], intros c hc, apply zero_power_le },
convert le_powerlt (pos_iff_ne_zero.2 h), rw [power_zero]
end
lemma powerlt_zero {a : cardinal} : a ^< 0 = 0 :=
begin
convert sup_eq_zero,
exact subtype.is_empty_of_false (λ x, (zero_le _).not_lt),
end
end cardinal
|
37fdd5c11a09ab8fd7be5d7dd2a632cc4c1338de | 38bf3fd2bb651ab70511408fcf70e2029e2ba310 | /src/topology/uniform_space/uniform_embedding.lean | c2e9dc738ff7140ebb76cdde0ebb7b0567ae9bd5 | [
"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 | 19,007 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Sébastien Gouëzel, Patrick Massot
Uniform embeddings of uniform spaces. Extension of uniform continuous functions.
-/
import topology.uniform_space.cauchy topology.uniform_space.separation
open filter topological_space lattice set classical
open_locale classical
open_locale uniformity
section
variables {α : Type*} {β : Type*} {γ : Type*}
[uniform_space α] [uniform_space β] [uniform_space γ]
universe u
structure uniform_inducing (f : α → β) : Prop :=
(comap_uniformity : comap (λx:α×α, (f x.1, f x.2)) (𝓤 β) = 𝓤 α)
lemma uniform_inducing.mk' {f : α → β} (h : ∀ s, s ∈ 𝓤 α ↔
∃ t ∈ 𝓤 β, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s) : uniform_inducing f :=
⟨by simp [eq_comm, filter.ext_iff, subset_def, h]⟩
lemma uniform_inducing.comp {g : β → γ} (hg : uniform_inducing g)
{f : α → β} (hf : uniform_inducing f) : uniform_inducing (g ∘ f) :=
⟨ by rw [show (λ (x : α × α), ((g ∘ f) x.1, (g ∘ f) x.2)) =
(λ y : β × β, (g y.1, g y.2)) ∘ (λ x : α × α, (f x.1, f x.2)), by ext ; simp,
← filter.comap_comap_comp, hg.1, hf.1]⟩
structure uniform_embedding (f : α → β) extends uniform_inducing f : Prop :=
(inj : function.injective f)
lemma uniform_embedding.comp {g : β → γ} (hg : uniform_embedding g)
{f : α → β} (hf : uniform_embedding f) : uniform_embedding (g ∘ f) :=
{ inj := function.injective_comp hg.inj hf.inj,
..hg.to_uniform_inducing.comp hf.to_uniform_inducing }
theorem uniform_embedding_def {f : α → β} :
uniform_embedding f ↔ function.injective f ∧ ∀ s, s ∈ 𝓤 α ↔
∃ t ∈ 𝓤 β, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s :=
begin
split,
{ rintro ⟨⟨h⟩, h'⟩,
rw [eq_comm, filter.ext_iff] at h,
simp [*, subset_def] },
{ rintro ⟨h, h'⟩,
refine uniform_embedding.mk ⟨_⟩ h,
rw [eq_comm, filter.ext_iff],
simp [*, subset_def] }
end
theorem uniform_embedding_def' {f : α → β} :
uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧
∀ s, s ∈ 𝓤 α →
∃ t ∈ 𝓤 β, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s :=
by simp [uniform_embedding_def, uniform_continuous_def]; exact
⟨λ ⟨I, H⟩, ⟨I, λ s su, (H _).2 ⟨s, su, λ x y, id⟩, λ s, (H s).1⟩,
λ ⟨I, H₁, H₂⟩, ⟨I, λ s, ⟨H₂ s,
λ ⟨t, tu, h⟩, sets_of_superset _ (H₁ t tu) (λ ⟨a, b⟩, h a b)⟩⟩⟩
lemma uniform_inducing.uniform_continuous {f : α → β}
(hf : uniform_inducing f) : uniform_continuous f :=
by simp [uniform_continuous, hf.comap_uniformity.symm, tendsto_comap]
lemma uniform_inducing.uniform_continuous_iff {f : α → β} {g : β → γ} (hg : uniform_inducing g) :
uniform_continuous f ↔ uniform_continuous (g ∘ f) :=
by simp [uniform_continuous, tendsto]; rw [← hg.comap_uniformity, ← map_le_iff_le_comap, filter.map_map]
lemma uniform_inducing.inducing {f : α → β} (h : uniform_inducing f) : inducing f :=
begin
refine ⟨eq_of_nhds_eq_nhds $ assume a, _ ⟩,
rw [nhds_induced, nhds_eq_uniformity, nhds_eq_uniformity, ← h.comap_uniformity,
comap_lift'_eq, comap_lift'_eq2];
{ refl <|> exact monotone_preimage }
end
lemma uniform_inducing.prod {α' : Type*} {β' : Type*} [uniform_space α'] [uniform_space β']
{e₁ : α → α'} {e₂ : β → β'} (h₁ : uniform_inducing e₁) (h₂ : uniform_inducing e₂) :
uniform_inducing (λp:α×β, (e₁ p.1, e₂ p.2)) :=
⟨by simp [(∘), uniformity_prod, h₁.comap_uniformity.symm, h₂.comap_uniformity.symm,
comap_inf, comap_comap_comp]⟩
lemma uniform_inducing.dense_inducing {f : α → β} (h : uniform_inducing f) (hd : dense_range f) :
dense_inducing f :=
{ dense := hd,
induced := h.inducing.induced }
lemma uniform_embedding.embedding {f : α → β} (h : uniform_embedding f) : embedding f :=
{ induced := h.to_uniform_inducing.inducing.induced,
inj := h.inj }
lemma uniform_embedding.dense_embedding {f : α → β} (h : uniform_embedding f) (hd : dense_range f) :
dense_embedding f :=
{ dense := hd,
inj := h.inj,
induced := h.embedding.induced }
lemma closure_image_mem_nhds_of_uniform_inducing
{s : set (α×α)} {e : α → β} (b : β)
(he₁ : uniform_inducing e) (he₂ : dense_inducing e) (hs : s ∈ 𝓤 α) :
∃a, closure (e '' {a' | (a, a') ∈ s}) ∈ nhds b :=
have s ∈ comap (λp:α×α, (e p.1, e p.2)) (𝓤 β),
from he₁.comap_uniformity.symm ▸ hs,
let ⟨t₁, ht₁u, ht₁⟩ := this in
have ht₁ : ∀p:α×α, (e p.1, e p.2) ∈ t₁ → p ∈ s, from ht₁,
let ⟨t₂, ht₂u, ht₂s, ht₂c⟩ := comp_symm_of_uniformity ht₁u in
let ⟨t, htu, hts, htc⟩ := comp_symm_of_uniformity ht₂u in
have preimage e {b' | (b, b') ∈ t₂} ∈ comap e (nhds b),
from preimage_mem_comap $ mem_nhds_left b ht₂u,
let ⟨a, (ha : (b, e a) ∈ t₂)⟩ := inhabited_of_mem_sets (he₂.comap_nhds_neq_bot) this in
have ∀b' (s' : set (β × β)), (b, b') ∈ t → s' ∈ 𝓤 β →
{y : β | (b', y) ∈ s'} ∩ e '' {a' : α | (a, a') ∈ s} ≠ ∅,
from assume b' s' hb' hs',
have preimage e {b'' | (b', b'') ∈ s' ∩ t} ∈ comap e (nhds b'),
from preimage_mem_comap $ mem_nhds_left b' $ inter_mem_sets hs' htu,
let ⟨a₂, ha₂s', ha₂t⟩ := inhabited_of_mem_sets (he₂.comap_nhds_neq_bot) this in
have (e a, e a₂) ∈ t₁,
from ht₂c $ prod_mk_mem_comp_rel (ht₂s ha) $ htc $ prod_mk_mem_comp_rel hb' ha₂t,
have e a₂ ∈ {b'':β | (b', b'') ∈ s'} ∩ e '' {a' | (a, a') ∈ s},
from ⟨ha₂s', mem_image_of_mem _ $ ht₁ (a, a₂) this⟩,
ne_empty_of_mem this,
have ∀b', (b, b') ∈ t → nhds b' ⊓ principal (e '' {a' | (a, a') ∈ s}) ≠ ⊥,
begin
intros b' hb',
rw [nhds_eq_uniformity, lift'_inf_principal_eq, lift'_neq_bot_iff],
exact assume s, this b' s hb',
exact monotone_inter monotone_preimage monotone_const
end,
have ∀b', (b, b') ∈ t → b' ∈ closure (e '' {a' | (a, a') ∈ s}),
from assume b' hb', by rw [closure_eq_nhds]; exact this b' hb',
⟨a, (nhds b).sets_of_superset (mem_nhds_left b htu) this⟩
lemma uniform_embedding_subtype_emb (p : α → Prop) {e : α → β} (ue : uniform_embedding e)
(de : dense_embedding e) : uniform_embedding (dense_embedding.subtype_emb p e) :=
{ comap_uniformity := by simp [comap_comap_comp, (∘), dense_embedding.subtype_emb,
uniformity_subtype, ue.comap_uniformity.symm],
inj := (de.subtype p).inj }
lemma uniform_embedding.prod {α' : Type*} {β' : Type*} [uniform_space α'] [uniform_space β']
{e₁ : α → α'} {e₂ : β → β'} (h₁ : uniform_embedding e₁) (h₂ : uniform_embedding e₂) :
uniform_embedding (λp:α×β, (e₁ p.1, e₂ p.2)) :=
{ inj := function.injective_prod h₁.inj h₂.inj,
..h₁.to_uniform_inducing.prod h₂.to_uniform_inducing }
/-- A set is complete iff its image under a uniform embedding is complete. -/
lemma is_complete_image_iff {m : α → β} {s : set α} (hm : uniform_embedding m) :
is_complete (m '' s) ↔ is_complete s :=
begin
refine ⟨λ c f hf fs, _, λ c f hf fs, _⟩,
{ let f' := map m f,
have cf' : cauchy f' := cauchy_map hm.to_uniform_inducing.uniform_continuous hf,
have f's : f' ≤ principal (m '' s),
{ simp only [filter.le_principal_iff, set.mem_image, filter.mem_map],
exact mem_sets_of_superset (filter.le_principal_iff.1 fs) (λx hx, ⟨x, hx, rfl⟩) },
rcases c f' cf' f's with ⟨y, yms, hy⟩,
rcases mem_image_iff_bex.1 yms with ⟨x, xs, rfl⟩,
rw [map_le_iff_le_comap, ← nhds_induced, ← (uniform_embedding.embedding hm).induced] at hy,
exact ⟨x, xs, hy⟩ },
{ rw filter.le_principal_iff at fs,
let f' := comap m f,
have cf' : cauchy f',
{ have : comap m f ≠ ⊥,
{ refine comap_neq_bot (λt ht, _),
have A : t ∩ m '' s ∈ f := filter.inter_mem_sets ht fs,
have : t ∩ m '' s ≠ ∅,
{ by_contradiction h,
simp only [not_not, ne.def] at h,
simpa [h, empty_in_sets_eq_bot, hf.1] using A },
rcases ne_empty_iff_exists_mem.1 this with ⟨x, ⟨xt, xms⟩⟩,
rcases mem_image_iff_bex.1 xms with ⟨y, ys, yx⟩,
rw ← yx at xt,
exact ⟨y, xt⟩ },
apply cauchy_comap _ hf this,
simp only [hm.comap_uniformity, le_refl] },
have : f' ≤ principal s := by simp [f']; exact
⟨m '' s, by simpa using fs, by simp [preimage_image_eq s hm.inj]⟩,
rcases c f' cf' this with ⟨x, xs, hx⟩,
existsi [m x, mem_image_of_mem m xs],
rw [(uniform_embedding.embedding hm).induced, nhds_induced] at hx,
calc f = map m f' : (map_comap $ filter.mem_sets_of_superset fs $ image_subset_range _ _).symm
... ≤ map m (comap m (nhds (m x))) : map_mono hx
... ≤ nhds (m x) : map_comap_le }
end
lemma complete_space_extension {m : β → α} (hm : uniform_inducing m) (dense : dense_range m)
(h : ∀f:filter β, cauchy f → ∃x:α, map m f ≤ nhds x) : complete_space α :=
⟨assume (f : filter α), assume hf : cauchy f,
let
p : set (α × α) → set α → set α := λs t, {y : α| ∃x:α, x ∈ t ∧ (x, y) ∈ s},
g := (𝓤 α).lift (λs, f.lift' (p s))
in
have mp₀ : monotone p,
from assume a b h t s ⟨x, xs, xa⟩, ⟨x, xs, h xa⟩,
have mp₁ : ∀{s}, monotone (p s),
from assume s a b h x ⟨y, ya, yxs⟩, ⟨y, h ya, yxs⟩,
have f ≤ g, from
le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht,
le_principal_iff.mpr $
mem_sets_of_superset ht $ assume x hx, ⟨x, hx, refl_mem_uniformity hs⟩,
have g ≠ ⊥, from neq_bot_of_le_neq_bot hf.left this,
have comap m g ≠ ⊥, from comap_neq_bot $ assume t ht,
let ⟨t', ht', ht_mem⟩ := (mem_lift_sets $ monotone_lift' monotone_const mp₀).mp ht in
let ⟨t'', ht'', ht'_sub⟩ := (mem_lift'_sets mp₁).mp ht_mem in
let ⟨x, (hx : x ∈ t'')⟩ := inhabited_of_mem_sets hf.left ht'' in
have h₀ : nhds x ⊓ principal (range m) ≠ ⊥,
by simpa [dense_range, closure_eq_nhds] using dense x,
have h₁ : {y | (x, y) ∈ t'} ∈ nhds x ⊓ principal (range m),
from @mem_inf_sets_of_left α (nhds x) (principal (range m)) _ $ mem_nhds_left x ht',
have h₂ : range m ∈ nhds x ⊓ principal (range m),
from @mem_inf_sets_of_right α (nhds x) (principal (range m)) _ $ subset.refl _,
have {y | (x, y) ∈ t'} ∩ range m ∈ nhds x ⊓ principal (range m),
from @inter_mem_sets α (nhds x ⊓ principal (range m)) _ _ h₁ h₂,
let ⟨y, xyt', b, b_eq⟩ := inhabited_of_mem_sets h₀ this in
⟨b, b_eq.symm ▸ ht'_sub ⟨x, hx, xyt'⟩⟩,
have cauchy g, from
⟨‹g ≠ ⊥›, assume s hs,
let
⟨s₁, hs₁, (comp_s₁ : comp_rel s₁ s₁ ⊆ s)⟩ := comp_mem_uniformity_sets hs,
⟨s₂, hs₂, (comp_s₂ : comp_rel s₂ s₂ ⊆ s₁)⟩ := comp_mem_uniformity_sets hs₁,
⟨t, ht, (prod_t : set.prod t t ⊆ s₂)⟩ := mem_prod_same_iff.mp (hf.right hs₂)
in
have hg₁ : p (preimage prod.swap s₁) t ∈ g,
from mem_lift (symm_le_uniformity hs₁) $ @mem_lift' α α f _ t ht,
have hg₂ : p s₂ t ∈ g,
from mem_lift hs₂ $ @mem_lift' α α f _ t ht,
have hg : set.prod (p (preimage prod.swap s₁) t) (p s₂ t) ∈ filter.prod g g,
from @prod_mem_prod α α _ _ g g hg₁ hg₂,
(filter.prod g g).sets_of_superset hg
(assume ⟨a, b⟩ ⟨⟨c₁, c₁t, hc₁⟩, ⟨c₂, c₂t, hc₂⟩⟩,
have (c₁, c₂) ∈ set.prod t t, from ⟨c₁t, c₂t⟩,
comp_s₁ $ prod_mk_mem_comp_rel hc₁ $
comp_s₂ $ prod_mk_mem_comp_rel (prod_t this) hc₂)⟩,
have cauchy (filter.comap m g),
from cauchy_comap (le_of_eq hm.comap_uniformity) ‹cauchy g› (by assumption),
let ⟨x, (hx : map m (filter.comap m g) ≤ nhds x)⟩ := h _ this in
have map m (filter.comap m g) ⊓ nhds x ≠ ⊥,
from (le_nhds_iff_adhp_of_cauchy (cauchy_map hm.uniform_continuous this)).mp hx,
have g ⊓ nhds x ≠ ⊥,
from neq_bot_of_le_neq_bot this (inf_le_inf (assume s hs, ⟨s, hs, subset.refl _⟩) (le_refl _)),
⟨x, calc f ≤ g : by assumption
... ≤ nhds x : le_nhds_of_cauchy_adhp ‹cauchy g› this⟩⟩
lemma totally_bounded_preimage {f : α → β} {s : set β} (hf : uniform_embedding f)
(hs : totally_bounded s) : totally_bounded (f ⁻¹' s) :=
λ t ht, begin
rw ← hf.comap_uniformity at ht,
rcases mem_comap_sets.2 ht with ⟨t', ht', ts⟩,
rcases totally_bounded_iff_subset.1
(totally_bounded_subset (image_preimage_subset f s) hs) _ ht' with ⟨c, cs, hfc, hct⟩,
refine ⟨f ⁻¹' c, finite_preimage (inj_on_of_injective _ hf.inj) hfc, λ x h, _⟩,
have := hct (mem_image_of_mem f h), simp at this ⊢,
rcases this with ⟨z, zc, zt⟩,
rcases cs zc with ⟨y, yc, rfl⟩,
exact ⟨y, zc, ts (by exact zt)⟩
end
end
lemma uniform_embedding_comap {α : Type*} {β : Type*} {f : α → β} [u : uniform_space β]
(hf : function.injective f) : @uniform_embedding α β (uniform_space.comap f u) u f :=
@uniform_embedding.mk _ _ (uniform_space.comap f u) _ _
(@uniform_inducing.mk _ _ (uniform_space.comap f u) _ _ rfl) hf
section uniform_extension
variables {α : Type*} {β : Type*} {γ : Type*}
[uniform_space α] [uniform_space β] [uniform_space γ]
{e : β → α}
(h_e : uniform_inducing e)
(h_dense : dense_range e)
{f : β → γ}
(h_f : uniform_continuous f)
local notation `ψ` := (h_e.dense_inducing h_dense).extend f
lemma uniformly_extend_exists [complete_space γ] (a : α) :
∃c, tendsto f (comap e (nhds a)) (nhds c) :=
let de := (h_e.dense_inducing h_dense) in
have cauchy (nhds a), from cauchy_nhds,
have cauchy (comap e (nhds a)), from
cauchy_comap (le_of_eq h_e.comap_uniformity) this de.comap_nhds_neq_bot,
have cauchy (map f (comap e (nhds a))), from
cauchy_map h_f this,
complete_space.complete this
lemma uniform_extend_subtype [complete_space γ]
{p : α → Prop} {e : α → β} {f : α → γ} {b : β} {s : set α}
(hf : uniform_continuous (λx:subtype p, f x.val))
(he : uniform_embedding e) (hd : ∀x:β, x ∈ closure (range e))
(hb : closure (e '' s) ∈ nhds b) (hs : is_closed s) (hp : ∀x∈s, p x) :
∃c, tendsto f (comap e (nhds b)) (nhds c) :=
have de : dense_embedding e,
from he.dense_embedding hd,
have de' : dense_embedding (dense_embedding.subtype_emb p e),
by exact de.subtype p,
have ue' : uniform_embedding (dense_embedding.subtype_emb p e),
from uniform_embedding_subtype_emb _ he de,
have b ∈ closure (e '' {x | p x}),
from (closure_mono $ mono_image $ hp) (mem_of_nhds hb),
let ⟨c, (hc : tendsto (f ∘ subtype.val) (comap (dense_embedding.subtype_emb p e) (nhds ⟨b, this⟩)) (nhds c))⟩ :=
uniformly_extend_exists ue'.to_uniform_inducing de'.dense hf _ in
begin
rw [nhds_subtype_eq_comap] at hc,
simp [comap_comap_comp] at hc,
change (tendsto (f ∘ @subtype.val α p) (comap (e ∘ @subtype.val α p) (nhds b)) (nhds c)) at hc,
rw [←comap_comap_comp, tendsto_comap'_iff] at hc,
exact ⟨c, hc⟩,
exact ⟨_, hb, assume x,
begin
change e x ∈ (closure (e '' s)) → x ∈ range subtype.val,
rw [←closure_induced, closure_eq_nhds, mem_set_of_eq, (≠), nhds_induced, ← de.to_dense_inducing.nhds_eq_comap],
change x ∈ {x | nhds x ⊓ principal s ≠ ⊥} → x ∈ range subtype.val,
rw [←closure_eq_nhds, closure_eq_of_is_closed hs],
exact assume hxs, ⟨⟨x, hp x hxs⟩, rfl⟩,
exact de.inj
end⟩
end
variables [separated γ]
lemma uniformly_extend_of_ind (b : β) : ψ (e b) = f b :=
dense_inducing.extend_e_eq _ b (continuous_iff_continuous_at.1 h_f.continuous b)
include h_f
lemma uniformly_extend_spec [complete_space γ] (a : α) :
tendsto f (comap e (nhds a)) (nhds (ψ a)) :=
let de := (h_e.dense_inducing h_dense) in
begin
by_cases ha : a ∈ range e,
{ rcases ha with ⟨b, rfl⟩,
rw [uniformly_extend_of_ind _ _ h_f, ← de.nhds_eq_comap],
exact h_f.continuous.tendsto _ },
{ simp only [dense_inducing.extend, dif_neg ha],
exact (@lim_spec _ _ (id _) _ $ uniformly_extend_exists h_e h_dense h_f _) }
end
lemma uniform_continuous_uniformly_extend [cγ : complete_space γ] : uniform_continuous ψ :=
assume d hd,
let ⟨s, hs, hs_comp⟩ := (mem_lift'_sets $
monotone_comp_rel monotone_id $ monotone_comp_rel monotone_id monotone_id).mp (comp_le_uniformity3 hd) in
have h_pnt : ∀{a m}, m ∈ nhds a → ∃c, c ∈ f '' preimage e m ∧ (c, ψ a) ∈ s ∧ (ψ a, c) ∈ s,
from assume a m hm,
have nb : map f (comap e (nhds a)) ≠ ⊥,
from map_ne_bot (h_e.dense_inducing h_dense).comap_nhds_neq_bot,
have (f '' preimage e m) ∩ ({c | (c, ψ a) ∈ s } ∩ {c | (ψ a, c) ∈ s }) ∈ map f (comap e (nhds a)),
from inter_mem_sets (image_mem_map $ preimage_mem_comap $ hm)
(uniformly_extend_spec h_e h_dense h_f _ (inter_mem_sets (mem_nhds_right _ hs) (mem_nhds_left _ hs))),
inhabited_of_mem_sets nb this,
have preimage (λp:β×β, (f p.1, f p.2)) s ∈ 𝓤 β,
from h_f hs,
have preimage (λp:β×β, (f p.1, f p.2)) s ∈ comap (λx:β×β, (e x.1, e x.2)) (𝓤 α),
by rwa [h_e.comap_uniformity.symm] at this,
let ⟨t, ht, ts⟩ := this in
show preimage (λp:(α×α), (ψ p.1, ψ p.2)) d ∈ 𝓤 α,
from (𝓤 α).sets_of_superset (interior_mem_uniformity ht) $
assume ⟨x₁, x₂⟩ hx_t,
have nhds (x₁, x₂) ≤ principal (interior t),
from is_open_iff_nhds.mp is_open_interior (x₁, x₂) hx_t,
have interior t ∈ filter.prod (nhds x₁) (nhds x₂),
by rwa [nhds_prod_eq, le_principal_iff] at this,
let ⟨m₁, hm₁, m₂, hm₂, (hm : set.prod m₁ m₂ ⊆ interior t)⟩ := mem_prod_iff.mp this in
let ⟨a, ha₁, _, ha₂⟩ := h_pnt hm₁ in
let ⟨b, hb₁, hb₂, _⟩ := h_pnt hm₂ in
have set.prod (preimage e m₁) (preimage e m₂) ⊆ preimage (λp:(β×β), (f p.1, f p.2)) s,
from calc _ ⊆ preimage (λp:(β×β), (e p.1, e p.2)) (interior t) : preimage_mono hm
... ⊆ preimage (λp:(β×β), (e p.1, e p.2)) t : preimage_mono interior_subset
... ⊆ preimage (λp:(β×β), (f p.1, f p.2)) s : ts,
have set.prod (f '' preimage e m₁) (f '' preimage e m₂) ⊆ s,
from calc set.prod (f '' preimage e m₁) (f '' preimage e m₂) =
(λp:(β×β), (f p.1, f p.2)) '' (set.prod (preimage e m₁) (preimage e m₂)) : prod_image_image_eq
... ⊆ (λp:(β×β), (f p.1, f p.2)) '' preimage (λp:(β×β), (f p.1, f p.2)) s : mono_image this
... ⊆ s : image_subset_iff.mpr $ subset.refl _,
have (a, b) ∈ s, from @this (a, b) ⟨ha₁, hb₁⟩,
hs_comp $ show (ψ x₁, ψ x₂) ∈ comp_rel s (comp_rel s s),
from ⟨a, ha₂, ⟨b, this, hb₂⟩⟩
end uniform_extension
|
5e19096e6d6ae7a4dd1bf054e8fa430092c1ec9d | cc060cf567f81c404a13ee79bf21f2e720fa6db0 | /lean/20170309-inherited-fields-lose-attributes.lean | 16f8a5d0e77020093f62545c5be3ceff4da88330 | [
"Apache-2.0"
] | permissive | semorrison/proof | cf0a8c6957153bdb206fd5d5a762a75958a82bca | 5ee398aa239a379a431190edbb6022b1a0aa2c70 | refs/heads/master | 1,610,414,502,842 | 1,518,696,851,000 | 1,518,696,851,000 | 78,375,937 | 2 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 82 | lean | structure X :=
(a : unit)
attribute [ematch] X.a
structure Y extends X
print Y.a |
be1e70fe34d828c20f607522debf2abd76e2f500 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/analysis/normed_space/linear_isometry.lean | 862806468477f59fb18fa1f1fe79014b30f7a349 | [
"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 | 36,659 | lean | /-
Copyright (c) 2021 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov, Frédéric Dupuis, Heather Macbeth
-/
import analysis.normed.group.basic
import topology.algebra.module.basic
import linear_algebra.basis
/-!
# (Semi-)linear isometries
In this file we define `linear_isometry σ₁₂ E E₂` (notation: `E →ₛₗᵢ[σ₁₂] E₂`) to be a semilinear
isometric embedding of `E` into `E₂` and `linear_isometry_equiv` (notation: `E ≃ₛₗᵢ[σ₁₂] E₂`) to be
a semilinear isometric equivalence between `E` and `E₂`. The notation for the associated purely
linear concepts is `E →ₗᵢ[R] E₂`, `E ≃ₗᵢ[R] E₂`, and `E →ₗᵢ⋆[R] E₂`, `E ≃ₗᵢ⋆[R] E₂` for
the star-linear versions.
We also prove some trivial lemmas and provide convenience constructors.
Since a lot of elementary properties don't require `∥x∥ = 0 → x = 0` we start setting up the
theory for `seminormed_add_comm_group` and we specialize to `normed_add_comm_group` when needed.
-/
open function set
variables {R R₂ R₃ R₄ E E₂ E₃ E₄ F 𝓕 : Type*} [semiring R] [semiring R₂] [semiring R₃] [semiring R₄]
{σ₁₂ : R →+* R₂} {σ₂₁ : R₂ →+* R} {σ₁₃ : R →+* R₃} {σ₃₁ : R₃ →+* R} {σ₁₄ : R →+* R₄}
{σ₄₁ : R₄ →+* R} {σ₂₃ : R₂ →+* R₃} {σ₃₂ : R₃ →+* R₂} {σ₂₄ : R₂ →+* R₄} {σ₄₂ : R₄ →+* R₂}
{σ₃₄ : R₃ →+* R₄} {σ₄₃ : R₄ →+* R₃}
[ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂]
[ring_hom_inv_pair σ₁₃ σ₃₁] [ring_hom_inv_pair σ₃₁ σ₁₃]
[ring_hom_inv_pair σ₂₃ σ₃₂] [ring_hom_inv_pair σ₃₂ σ₂₃]
[ring_hom_inv_pair σ₁₄ σ₄₁] [ring_hom_inv_pair σ₄₁ σ₁₄]
[ring_hom_inv_pair σ₂₄ σ₄₂] [ring_hom_inv_pair σ₄₂ σ₂₄]
[ring_hom_inv_pair σ₃₄ σ₄₃] [ring_hom_inv_pair σ₄₃ σ₃₄]
[ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] [ring_hom_comp_triple σ₁₂ σ₂₄ σ₁₄]
[ring_hom_comp_triple σ₂₃ σ₃₄ σ₂₄] [ring_hom_comp_triple σ₁₃ σ₃₄ σ₁₄]
[ring_hom_comp_triple σ₃₂ σ₂₁ σ₃₁] [ring_hom_comp_triple σ₄₂ σ₂₁ σ₄₁]
[ring_hom_comp_triple σ₄₃ σ₃₂ σ₄₂] [ring_hom_comp_triple σ₄₃ σ₃₁ σ₄₁]
[seminormed_add_comm_group E] [seminormed_add_comm_group E₂] [seminormed_add_comm_group E₃]
[seminormed_add_comm_group E₄] [module R E] [module R₂ E₂] [module R₃ E₃] [module R₄ E₄]
[normed_add_comm_group F] [module R F]
/-- A `σ₁₂`-semilinear isometric embedding of a normed `R`-module into an `R₂`-module. -/
structure linear_isometry (σ₁₂ : R →+* R₂) (E E₂ : Type*) [seminormed_add_comm_group E]
[seminormed_add_comm_group E₂] [module R E] [module R₂ E₂] extends E →ₛₗ[σ₁₂] E₂ :=
(norm_map' : ∀ x, ∥to_linear_map x∥ = ∥x∥)
notation E ` →ₛₗᵢ[`:25 σ₁₂:25 `] `:0 E₂:0 := linear_isometry σ₁₂ E E₂
notation E ` →ₗᵢ[`:25 R:25 `] `:0 E₂:0 := linear_isometry (ring_hom.id R) E E₂
notation E ` →ₗᵢ⋆[`:25 R:25 `] `:0 E₂:0 := linear_isometry (star_ring_end R) E E₂
set_option old_structure_cmd true
/-- `semilinear_isometry_class F σ E E₂` asserts `F` is a type of bundled `σ`-semilinear isometries
`E → E₂`.
See also `linear_isometry_class F R E E₂` for the case where `σ` is the identity map on `R`.
A map `f` between an `R`-module and an `S`-module over a ring homomorphism `σ : R →+* S`
is semilinear if it satisfies the two properties `f (x + y) = f x + f y` and
`f (c • x) = (σ c) • f x`. -/
class semilinear_isometry_class (𝓕 : Type*) {R R₂ : out_param Type*} [semiring R] [semiring R₂]
(σ₁₂ : out_param $ R →+* R₂) (E E₂ : out_param Type*) [seminormed_add_comm_group E]
[seminormed_add_comm_group E₂] [module R E] [module R₂ E₂]
extends semilinear_map_class 𝓕 σ₁₂ E E₂ :=
(norm_map : ∀ (f : 𝓕) (x : E), ∥f x∥ = ∥x∥)
/-- `linear_isometry_class F R E E₂` asserts `F` is a type of bundled `R`-linear isometries
`M → M₂`.
This is an abbreviation for `semilinear_isometry_class F (ring_hom.id R) E E₂`.
-/
abbreviation linear_isometry_class (𝓕 : Type*) (R E E₂ : out_param Type*) [semiring R]
[seminormed_add_comm_group E] [seminormed_add_comm_group E₂] [module R E] [module R E₂] :=
semilinear_isometry_class 𝓕 (ring_hom.id R) E E₂
set_option old_structure_cmd false
namespace semilinear_isometry_class
protected lemma isometry [semilinear_isometry_class 𝓕 σ₁₂ E E₂] (f : 𝓕) : isometry f :=
add_monoid_hom_class.isometry_of_norm _ (norm_map _)
@[continuity] protected lemma continuous [semilinear_isometry_class 𝓕 σ₁₂ E E₂] (f : 𝓕) :
continuous f :=
(semilinear_isometry_class.isometry f).continuous
@[simp] lemma nnnorm_map [semilinear_isometry_class 𝓕 σ₁₂ E E₂] (f : 𝓕) (x : E) :
∥f x∥₊ = ∥x∥₊ :=
nnreal.eq $ norm_map f x
protected lemma lipschitz [semilinear_isometry_class 𝓕 σ₁₂ E E₂] (f : 𝓕) :
lipschitz_with 1 f :=
(semilinear_isometry_class.isometry f).lipschitz
protected lemma antilipschitz [semilinear_isometry_class 𝓕 σ₁₂ E E₂] (f : 𝓕) :
antilipschitz_with 1 f :=
(semilinear_isometry_class.isometry f).antilipschitz
lemma ediam_image [semilinear_isometry_class 𝓕 σ₁₂ E E₂] (f : 𝓕) (s : set E) :
emetric.diam (f '' s) = emetric.diam s :=
(semilinear_isometry_class.isometry f).ediam_image s
lemma ediam_range [semilinear_isometry_class 𝓕 σ₁₂ E E₂] (f : 𝓕) :
emetric.diam (range f) = emetric.diam (univ : set E) :=
(semilinear_isometry_class.isometry f).ediam_range
lemma diam_image [semilinear_isometry_class 𝓕 σ₁₂ E E₂] (f : 𝓕) (s : set E) :
metric.diam (f '' s) = metric.diam s :=
(semilinear_isometry_class.isometry f).diam_image s
lemma diam_range [semilinear_isometry_class 𝓕 σ₁₂ E E₂] (f : 𝓕) :
metric.diam (range f) = metric.diam (univ : set E) :=
(semilinear_isometry_class.isometry f).diam_range
@[priority 100]
instance [s : semilinear_isometry_class 𝓕 σ₁₂ E E₂] : continuous_semilinear_map_class 𝓕 σ₁₂ E E₂ :=
{ map_continuous := semilinear_isometry_class.continuous,
..s }
end semilinear_isometry_class
namespace linear_isometry
/-- We use `f₁` when we need the domain to be a `normed_space`. -/
variables (f : E →ₛₗᵢ[σ₁₂] E₂) (f₁ : F →ₛₗᵢ[σ₁₂] E₂)
lemma to_linear_map_injective : injective (to_linear_map : (E →ₛₗᵢ[σ₁₂] E₂) → (E →ₛₗ[σ₁₂] E₂))
| ⟨f, _⟩ ⟨g, _⟩ rfl := rfl
@[simp] lemma to_linear_map_inj {f g : E →ₛₗᵢ[σ₁₂] E₂} :
f.to_linear_map = g.to_linear_map ↔ f = g := to_linear_map_injective.eq_iff
instance : semilinear_isometry_class (E →ₛₗᵢ[σ₁₂] E₂) σ₁₂ E E₂ :=
{ coe := λ f, f.to_fun,
coe_injective' := λ f g h, to_linear_map_injective (fun_like.coe_injective h),
map_add := λ f, map_add f.to_linear_map,
map_smulₛₗ := λ f, map_smulₛₗ f.to_linear_map,
norm_map := λ f, f.norm_map' }
/-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun`
directly.
-/
instance : has_coe_to_fun (E →ₛₗᵢ[σ₁₂] E₂) (λ _, E → E₂) := ⟨λ f, f.to_fun⟩
@[simp] lemma coe_to_linear_map : ⇑f.to_linear_map = f := rfl
@[simp] lemma coe_mk (f : E →ₛₗ[σ₁₂] E₂) (hf) : ⇑(mk f hf) = f := rfl
lemma coe_injective : @injective (E →ₛₗᵢ[σ₁₂] E₂) (E → E₂) coe_fn :=
fun_like.coe_injective
/-- See Note [custom simps projection]. We need to specify this projection explicitly in this case,
because it is a composition of multiple projections. -/
def simps.apply (σ₁₂ : R →+* R₂) (E E₂ : Type*) [seminormed_add_comm_group E]
[seminormed_add_comm_group E₂] [module R E] [module R₂ E₂] (h : E →ₛₗᵢ[σ₁₂] E₂) : E → E₂ := h
initialize_simps_projections linear_isometry (to_linear_map_to_fun → apply)
@[ext] lemma ext {f g : E →ₛₗᵢ[σ₁₂] E₂} (h : ∀ x, f x = g x) : f = g :=
coe_injective $ funext h
protected lemma congr_arg [semilinear_isometry_class 𝓕 σ₁₂ E E₂] {f : 𝓕} :
Π {x x' : E}, x = x' → f x = f x'
| _ _ rfl := rfl
protected lemma congr_fun [semilinear_isometry_class 𝓕 σ₁₂ E E₂] {f g : 𝓕} (h : f = g) (x : E) :
f x = g x := h ▸ rfl
@[simp] protected lemma map_zero : f 0 = 0 := f.to_linear_map.map_zero
@[simp] protected lemma map_add (x y : E) : f (x + y) = f x + f y := f.to_linear_map.map_add x y
@[simp] protected lemma map_neg (x : E) : f (- x) = - f x := f.to_linear_map.map_neg x
@[simp] protected lemma map_sub (x y : E) : f (x - y) = f x - f y := f.to_linear_map.map_sub x y
@[simp] protected lemma map_smulₛₗ (c : R) (x : E) : f (c • x) = σ₁₂ c • f x :=
f.to_linear_map.map_smulₛₗ c x
@[simp] protected lemma map_smul [module R E₂] (f : E →ₗᵢ[R] E₂) (c : R) (x : E) :
f (c • x) = c • f x :=
f.to_linear_map.map_smul c x
@[simp] lemma norm_map (x : E) : ∥f x∥ = ∥x∥ := semilinear_isometry_class.norm_map f x
@[simp] lemma nnnorm_map (x : E) : ∥f x∥₊ = ∥x∥₊ := nnreal.eq $ norm_map f x
protected lemma isometry : isometry f := add_monoid_hom_class.isometry_of_norm _ (norm_map _)
@[simp] lemma is_complete_image_iff [semilinear_isometry_class 𝓕 σ₁₂ E E₂] (f : 𝓕) {s : set E} :
is_complete (f '' s) ↔ is_complete s :=
is_complete_image_iff (semilinear_isometry_class.isometry f).uniform_inducing
lemma is_complete_map_iff [ring_hom_surjective σ₁₂] {p : submodule R E} :
is_complete (p.map f.to_linear_map : set E₂) ↔ is_complete (p : set E) :=
f.is_complete_image_iff
lemma is_complete_map_iff' [semilinear_isometry_class 𝓕 σ₁₂ E E₂] (f : 𝓕) [ring_hom_surjective σ₁₂]
{p : submodule R E} : is_complete (p.map f : set E₂) ↔ is_complete (p : set E) :=
is_complete_image_iff f
instance complete_space_map [semilinear_isometry_class 𝓕 σ₁₂ E E₂] (f : 𝓕) [ring_hom_surjective σ₁₂]
(p : submodule R E) [complete_space p] : complete_space (p.map f) :=
((is_complete_map_iff' f).2 $ complete_space_coe_iff_is_complete.1 ‹_›).complete_space_coe
instance complete_space_map' [ring_hom_surjective σ₁₂] (p : submodule R E) [complete_space p] :
complete_space (p.map f.to_linear_map) :=
(f.is_complete_map_iff.2 $ complete_space_coe_iff_is_complete.1 ‹_›).complete_space_coe
@[simp] lemma dist_map (x y : E) : dist (f x) (f y) = dist x y := f.isometry.dist_eq x y
@[simp] lemma edist_map (x y : E) : edist (f x) (f y) = edist x y := f.isometry.edist_eq x y
protected lemma injective : injective f₁ := isometry.injective (linear_isometry.isometry f₁)
@[simp] lemma map_eq_iff {x y : F} : f₁ x = f₁ y ↔ x = y := f₁.injective.eq_iff
lemma map_ne {x y : F} (h : x ≠ y) : f₁ x ≠ f₁ y := f₁.injective.ne h
protected lemma lipschitz : lipschitz_with 1 f := f.isometry.lipschitz
protected lemma antilipschitz : antilipschitz_with 1 f := f.isometry.antilipschitz
@[continuity] protected lemma continuous : continuous f := f.isometry.continuous
@[simp] lemma preimage_ball (x : E) (r : ℝ) :
f ⁻¹' (metric.ball (f x) r) = metric.ball x r :=
f.isometry.preimage_ball x r
@[simp] lemma preimage_sphere (x : E) (r : ℝ) :
f ⁻¹' (metric.sphere (f x) r) = metric.sphere x r :=
f.isometry.preimage_sphere x r
@[simp] lemma preimage_closed_ball (x : E) (r : ℝ) :
f ⁻¹' (metric.closed_ball (f x) r) = metric.closed_ball x r :=
f.isometry.preimage_closed_ball x r
lemma ediam_image (s : set E) : emetric.diam (f '' s) = emetric.diam s :=
f.isometry.ediam_image s
lemma ediam_range : emetric.diam (range f) = emetric.diam (univ : set E) :=
f.isometry.ediam_range
lemma diam_image (s : set E) : metric.diam (f '' s) = metric.diam s :=
isometry.diam_image (linear_isometry.isometry f) s
lemma diam_range : metric.diam (range f) = metric.diam (univ : set E) :=
isometry.diam_range (linear_isometry.isometry f)
/-- Interpret a linear isometry as a continuous linear map. -/
def to_continuous_linear_map : E →SL[σ₁₂] E₂ := ⟨f.to_linear_map, f.continuous⟩
lemma to_continuous_linear_map_injective :
function.injective (to_continuous_linear_map : _ → E →SL[σ₁₂] E₂) :=
λ x y h, coe_injective (congr_arg _ h : ⇑x.to_continuous_linear_map = _)
@[simp] lemma to_continuous_linear_map_inj {f g : E →ₛₗᵢ[σ₁₂] E₂} :
f.to_continuous_linear_map = g.to_continuous_linear_map ↔ f = g :=
to_continuous_linear_map_injective.eq_iff
@[simp] lemma coe_to_continuous_linear_map : ⇑f.to_continuous_linear_map = f := rfl
@[simp] lemma comp_continuous_iff {α : Type*} [topological_space α] {g : α → E} :
continuous (f ∘ g) ↔ continuous g :=
f.isometry.comp_continuous_iff
/-- The identity linear isometry. -/
def id : E →ₗᵢ[R] E := ⟨linear_map.id, λ x, rfl⟩
@[simp] lemma coe_id : ((id : E →ₗᵢ[R] E) : E → E) = _root_.id := rfl
@[simp] lemma id_apply (x : E) : (id : E →ₗᵢ[R] E) x = x := rfl
@[simp] lemma id_to_linear_map : (id.to_linear_map : E →ₗ[R] E) = linear_map.id := rfl
@[simp] lemma id_to_continuous_linear_map :
id.to_continuous_linear_map = continuous_linear_map.id R E := rfl
instance : inhabited (E →ₗᵢ[R] E) := ⟨id⟩
/-- Composition of linear isometries. -/
def comp (g : E₂ →ₛₗᵢ[σ₂₃] E₃) (f : E →ₛₗᵢ[σ₁₂] E₂) : E →ₛₗᵢ[σ₁₃] E₃ :=
⟨g.to_linear_map.comp f.to_linear_map, λ x, (norm_map g _).trans (norm_map f _)⟩
include σ₁₃
@[simp] lemma coe_comp (g : E₂ →ₛₗᵢ[σ₂₃] E₃) (f : E →ₛₗᵢ[σ₁₂] E₂) :
⇑(g.comp f) = g ∘ f :=
rfl
omit σ₁₃
@[simp] lemma id_comp : (id : E₂ →ₗᵢ[R₂] E₂).comp f = f := ext $ λ x, rfl
@[simp] lemma comp_id : f.comp id = f := ext $ λ x, rfl
include σ₁₃ σ₂₄ σ₁₄
lemma comp_assoc (f : E₃ →ₛₗᵢ[σ₃₄] E₄) (g : E₂ →ₛₗᵢ[σ₂₃] E₃) (h : E →ₛₗᵢ[σ₁₂] E₂) :
(f.comp g).comp h = f.comp (g.comp h) :=
rfl
omit σ₁₃ σ₂₄ σ₁₄
instance : monoid (E →ₗᵢ[R] E) :=
{ one := id,
mul := comp,
mul_assoc := comp_assoc,
one_mul := id_comp,
mul_one := comp_id }
@[simp] lemma coe_one : ((1 : E →ₗᵢ[R] E) : E → E) = _root_.id := rfl
@[simp] lemma coe_mul (f g : E →ₗᵢ[R] E) : ⇑(f * g) = f ∘ g := rfl
lemma one_def : (1 : E →ₗᵢ[R] E) = id := rfl
lemma mul_def (f g : E →ₗᵢ[R] E) : (f * g : E →ₗᵢ[R] E) = f.comp g := rfl
end linear_isometry
/-- Construct a `linear_isometry` from a `linear_map` satisfying `isometry`. -/
def linear_map.to_linear_isometry (f : E →ₛₗ[σ₁₂] E₂) (hf : isometry f) : E →ₛₗᵢ[σ₁₂] E₂ :=
{ norm_map' := by { simp_rw [←dist_zero_right, ←f.map_zero], exact λ x, hf.dist_eq x _ },
.. f }
namespace submodule
variables {R' : Type*} [ring R'] [module R' E] (p : submodule R' E)
/-- `submodule.subtype` as a `linear_isometry`. -/
def subtypeₗᵢ : p →ₗᵢ[R'] E := ⟨p.subtype, λ x, rfl⟩
@[simp] lemma coe_subtypeₗᵢ : ⇑p.subtypeₗᵢ = p.subtype := rfl
@[simp] lemma subtypeₗᵢ_to_linear_map : p.subtypeₗᵢ.to_linear_map = p.subtype := rfl
@[simp] lemma subtypeₗᵢ_to_continuous_linear_map :
p.subtypeₗᵢ.to_continuous_linear_map = p.subtypeL := rfl
end submodule
/-- A semilinear isometric equivalence between two normed vector spaces. -/
structure linear_isometry_equiv (σ₁₂ : R →+* R₂) {σ₂₁ : R₂ →+* R} [ring_hom_inv_pair σ₁₂ σ₂₁]
[ring_hom_inv_pair σ₂₁ σ₁₂] (E E₂ : Type*) [seminormed_add_comm_group E]
[seminormed_add_comm_group E₂] [module R E] [module R₂ E₂] extends E ≃ₛₗ[σ₁₂] E₂ :=
(norm_map' : ∀ x, ∥to_linear_equiv x∥ = ∥x∥)
notation E ` ≃ₛₗᵢ[`:25 σ₁₂:25 `] `:0 E₂:0 := linear_isometry_equiv σ₁₂ E E₂
notation E ` ≃ₗᵢ[`:25 R:25 `] `:0 E₂:0 := linear_isometry_equiv (ring_hom.id R) E E₂
notation E ` ≃ₗᵢ⋆[`:25 R:25 `] `:0 E₂:0 :=
linear_isometry_equiv (star_ring_end R) E E₂
set_option old_structure_cmd true
/-- `semilinear_isometry_equiv_class F σ E E₂` asserts `F` is a type of bundled `σ`-semilinear
isometric equivs `E → E₂`.
See also `linear_isometry_equiv_class F R E E₂` for the case where `σ` is the identity map on `R`.
A map `f` between an `R`-module and an `S`-module over a ring homomorphism `σ : R →+* S`
is semilinear if it satisfies the two properties `f (x + y) = f x + f y` and
`f (c • x) = (σ c) • f x`. -/
class semilinear_isometry_equiv_class (𝓕 : Type*) {R R₂ : out_param Type*}
[semiring R] [semiring R₂] (σ₁₂ : out_param $ R →+* R₂) {σ₂₁ : out_param $ R₂ →+* R}
[ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] (E E₂ : out_param Type*)
[seminormed_add_comm_group E] [seminormed_add_comm_group E₂] [module R E] [module R₂ E₂]
extends semilinear_equiv_class 𝓕 σ₁₂ E E₂ :=
(norm_map : ∀ (f : 𝓕) (x : E), ∥f x∥ = ∥x∥)
/-- `linear_isometry_equiv_class F R E E₂` asserts `F` is a type of bundled `R`-linear isometries
`M → M₂`.
This is an abbreviation for `semilinear_isometry_equiv_class F (ring_hom.id R) E E₂`.
-/
abbreviation linear_isometry_equiv_class (𝓕 : Type*) (R E E₂ : out_param Type*) [semiring R]
[seminormed_add_comm_group E] [seminormed_add_comm_group E₂] [module R E] [module R E₂] :=
semilinear_isometry_equiv_class 𝓕 (ring_hom.id R) E E₂
set_option old_structure_cmd false
namespace semilinear_isometry_equiv_class
variables (𝓕)
include σ₂₁
-- `σ₂₁` becomes a metavariable, but it's OK since it's an outparam
@[priority 100, nolint dangerous_instance]
instance [s : semilinear_isometry_equiv_class 𝓕 σ₁₂ E E₂] : semilinear_isometry_class 𝓕 σ₁₂ E E₂ :=
{ coe := (coe : 𝓕 → E → E₂),
coe_injective' := @fun_like.coe_injective 𝓕 _ _ _,
..s }
omit σ₂₁
end semilinear_isometry_equiv_class
namespace linear_isometry_equiv
variables (e : E ≃ₛₗᵢ[σ₁₂] E₂)
include σ₂₁
lemma to_linear_equiv_injective : injective (to_linear_equiv : (E ≃ₛₗᵢ[σ₁₂] E₂) → (E ≃ₛₗ[σ₁₂] E₂))
| ⟨e, _⟩ ⟨_, _⟩ rfl := rfl
@[simp] lemma to_linear_equiv_inj {f g : E ≃ₛₗᵢ[σ₁₂] E₂} :
f.to_linear_equiv = g.to_linear_equiv ↔ f = g :=
to_linear_equiv_injective.eq_iff
instance : semilinear_isometry_equiv_class (E ≃ₛₗᵢ[σ₁₂] E₂) σ₁₂ E E₂ :=
{ coe := λ e, e.to_fun,
inv := λ e, e.inv_fun,
coe_injective' := λ f g h₁ h₂,
by { cases f with f' _, cases g with g' _, cases f', cases g', congr', },
left_inv := λ e, e.left_inv,
right_inv := λ e, e.right_inv,
map_add := λ f, map_add f.to_linear_equiv,
map_smulₛₗ := λ e, map_smulₛₗ e.to_linear_equiv,
norm_map := λ e, e.norm_map' }
/-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun`
directly.
-/
instance : has_coe_to_fun (E ≃ₛₗᵢ[σ₁₂] E₂) (λ _, E → E₂) := ⟨λ f, f.to_fun⟩
lemma coe_injective : @function.injective (E ≃ₛₗᵢ[σ₁₂] E₂) (E → E₂) coe_fn :=
fun_like.coe_injective
@[simp] lemma coe_mk (e : E ≃ₛₗ[σ₁₂] E₂) (he : ∀ x, ∥e x∥ = ∥x∥) :
⇑(mk e he) = e :=
rfl
@[simp] lemma coe_to_linear_equiv (e : E ≃ₛₗᵢ[σ₁₂] E₂) : ⇑e.to_linear_equiv = e := rfl
@[ext] lemma ext {e e' : E ≃ₛₗᵢ[σ₁₂] E₂} (h : ∀ x, e x = e' x) : e = e' :=
to_linear_equiv_injective $ linear_equiv.ext h
protected lemma congr_arg {f : E ≃ₛₗᵢ[σ₁₂] E₂} : Π {x x' : E}, x = x' → f x = f x'
| _ _ rfl := rfl
protected lemma congr_fun {f g : E ≃ₛₗᵢ[σ₁₂] E₂} (h : f = g) (x : E) : f x = g x := h ▸ rfl
/-- Construct a `linear_isometry_equiv` from a `linear_equiv` and two inequalities:
`∀ x, ∥e x∥ ≤ ∥x∥` and `∀ y, ∥e.symm y∥ ≤ ∥y∥`. -/
def of_bounds (e : E ≃ₛₗ[σ₁₂] E₂) (h₁ : ∀ x, ∥e x∥ ≤ ∥x∥) (h₂ : ∀ y, ∥e.symm y∥ ≤ ∥y∥) :
E ≃ₛₗᵢ[σ₁₂] E₂ :=
⟨e, λ x, le_antisymm (h₁ x) $ by simpa only [e.symm_apply_apply] using h₂ (e x)⟩
@[simp] lemma norm_map (x : E) : ∥e x∥ = ∥x∥ := e.norm_map' x
/-- Reinterpret a `linear_isometry_equiv` as a `linear_isometry`. -/
def to_linear_isometry : E →ₛₗᵢ[σ₁₂] E₂ := ⟨e.1, e.2⟩
lemma to_linear_isometry_injective :
function.injective (to_linear_isometry : _ → E →ₛₗᵢ[σ₁₂] E₂) :=
λ x y h, coe_injective (congr_arg _ h : ⇑x.to_linear_isometry = _)
@[simp] lemma to_linear_isometry_inj {f g : E ≃ₛₗᵢ[σ₁₂] E₂} :
f.to_linear_isometry = g.to_linear_isometry ↔ f = g :=
to_linear_isometry_injective.eq_iff
@[simp] lemma coe_to_linear_isometry : ⇑e.to_linear_isometry = e := rfl
protected lemma isometry : isometry e := e.to_linear_isometry.isometry
/-- Reinterpret a `linear_isometry_equiv` as an `isometric`. -/
def to_isometric : E ≃ᵢ E₂ := ⟨e.to_linear_equiv.to_equiv, e.isometry⟩
lemma to_isometric_injective :
function.injective (to_isometric : (E ≃ₛₗᵢ[σ₁₂] E₂) → E ≃ᵢ E₂) :=
λ x y h, coe_injective (congr_arg _ h : ⇑x.to_isometric = _)
@[simp] lemma to_isometric_inj {f g : E ≃ₛₗᵢ[σ₁₂] E₂} :
f.to_isometric = g.to_isometric ↔ f = g :=
to_isometric_injective.eq_iff
@[simp] lemma coe_to_isometric : ⇑e.to_isometric = e := rfl
lemma range_eq_univ (e : E ≃ₛₗᵢ[σ₁₂] E₂) : set.range e = set.univ :=
by { rw ← coe_to_isometric, exact isometric.range_eq_univ _, }
/-- Reinterpret a `linear_isometry_equiv` as an `homeomorph`. -/
def to_homeomorph : E ≃ₜ E₂ := e.to_isometric.to_homeomorph
lemma to_homeomorph_injective :
function.injective (to_homeomorph : (E ≃ₛₗᵢ[σ₁₂] E₂) → E ≃ₜ E₂) :=
λ x y h, coe_injective (congr_arg _ h : ⇑x.to_homeomorph = _)
@[simp] lemma to_homeomorph_inj {f g : E ≃ₛₗᵢ[σ₁₂] E₂} :
f.to_homeomorph = g.to_homeomorph ↔ f = g :=
to_homeomorph_injective.eq_iff
@[simp] lemma coe_to_homeomorph : ⇑e.to_homeomorph = e := rfl
protected lemma continuous : continuous e := e.isometry.continuous
protected lemma continuous_at {x} : continuous_at e x := e.continuous.continuous_at
protected lemma continuous_on {s} : continuous_on e s := e.continuous.continuous_on
protected lemma continuous_within_at {s x} : continuous_within_at e s x :=
e.continuous.continuous_within_at
/-- Interpret a `linear_isometry_equiv` as a continuous linear equiv. -/
def to_continuous_linear_equiv : E ≃SL[σ₁₂] E₂ :=
{ .. e.to_linear_isometry.to_continuous_linear_map,
.. e.to_homeomorph }
lemma to_continuous_linear_equiv_injective :
function.injective (to_continuous_linear_equiv : _ → E ≃SL[σ₁₂] E₂) :=
λ x y h, coe_injective (congr_arg _ h : ⇑x.to_continuous_linear_equiv = _)
@[simp] lemma to_continuous_linear_equiv_inj {f g : E ≃ₛₗᵢ[σ₁₂] E₂} :
f.to_continuous_linear_equiv = g.to_continuous_linear_equiv ↔ f = g :=
to_continuous_linear_equiv_injective.eq_iff
@[simp] lemma coe_to_continuous_linear_equiv : ⇑e.to_continuous_linear_equiv = e := rfl
omit σ₂₁
variables (R E)
/-- Identity map as a `linear_isometry_equiv`. -/
def refl : E ≃ₗᵢ[R] E := ⟨linear_equiv.refl R E, λ x, rfl⟩
variables {R E}
instance : inhabited (E ≃ₗᵢ[R] E) := ⟨refl R E⟩
@[simp] lemma coe_refl : ⇑(refl R E) = id := rfl
/-- The inverse `linear_isometry_equiv`. -/
def symm : E₂ ≃ₛₗᵢ[σ₂₁] E :=
⟨e.to_linear_equiv.symm,
λ x, (e.norm_map _).symm.trans $ congr_arg norm $ e.to_linear_equiv.apply_symm_apply x⟩
@[simp] lemma apply_symm_apply (x : E₂) : e (e.symm x) = x := e.to_linear_equiv.apply_symm_apply x
@[simp] lemma symm_apply_apply (x : E) : e.symm (e x) = x := e.to_linear_equiv.symm_apply_apply x
@[simp] lemma map_eq_zero_iff {x : E} : e x = 0 ↔ x = 0 := e.to_linear_equiv.map_eq_zero_iff
@[simp] lemma symm_symm : e.symm.symm = e := ext $ λ x, rfl
@[simp] lemma to_linear_equiv_symm : e.to_linear_equiv.symm = e.symm.to_linear_equiv := rfl
@[simp] lemma to_isometric_symm : e.to_isometric.symm = e.symm.to_isometric := rfl
@[simp] lemma to_homeomorph_symm : e.to_homeomorph.symm = e.symm.to_homeomorph := rfl
/-- See Note [custom simps projection]. We need to specify this projection explicitly in this case,
because it is a composition of multiple projections. -/
def simps.apply (σ₁₂ : R →+* R₂) {σ₂₁ : R₂ →+* R} [ring_hom_inv_pair σ₁₂ σ₂₁]
[ring_hom_inv_pair σ₂₁ σ₁₂] (E E₂ : Type*) [seminormed_add_comm_group E]
[seminormed_add_comm_group E₂] [module R E] [module R₂ E₂] (h : E ≃ₛₗᵢ[σ₁₂] E₂) : E → E₂ := h
/-- See Note [custom simps projection] -/
def simps.symm_apply (σ₁₂ : R →+* R₂) {σ₂₁ : R₂ →+* R} [ring_hom_inv_pair σ₁₂ σ₂₁]
[ring_hom_inv_pair σ₂₁ σ₁₂] (E E₂ : Type*) [seminormed_add_comm_group E]
[seminormed_add_comm_group E₂]
[module R E] [module R₂ E₂] (h : E ≃ₛₗᵢ[σ₁₂] E₂) : E₂ → E := h.symm
initialize_simps_projections linear_isometry_equiv
(to_linear_equiv_to_fun → apply, to_linear_equiv_inv_fun → symm_apply)
include σ₃₁ σ₃₂
/-- Composition of `linear_isometry_equiv`s as a `linear_isometry_equiv`. -/
def trans (e' : E₂ ≃ₛₗᵢ[σ₂₃] E₃) : E ≃ₛₗᵢ[σ₁₃] E₃ :=
⟨e.to_linear_equiv.trans e'.to_linear_equiv, λ x, (e'.norm_map _).trans (e.norm_map _)⟩
include σ₁₃ σ₂₁
@[simp] lemma coe_trans (e₁ : E ≃ₛₗᵢ[σ₁₂] E₂) (e₂ : E₂ ≃ₛₗᵢ[σ₂₃] E₃) : ⇑(e₁.trans e₂) = e₂ ∘ e₁ :=
rfl
@[simp] lemma trans_apply (e₁ : E ≃ₛₗᵢ[σ₁₂] E₂) (e₂ : E₂ ≃ₛₗᵢ[σ₂₃] E₃) (c : E) :
(e₁.trans e₂ : E ≃ₛₗᵢ[σ₁₃] E₃) c = e₂ (e₁ c) :=
rfl
@[simp] lemma to_linear_equiv_trans (e' : E₂ ≃ₛₗᵢ[σ₂₃] E₃) :
(e.trans e').to_linear_equiv = e.to_linear_equiv.trans e'.to_linear_equiv :=
rfl
omit σ₁₃ σ₂₁ σ₃₁ σ₃₂
@[simp] lemma trans_refl : e.trans (refl R₂ E₂) = e := ext $ λ x, rfl
@[simp] lemma refl_trans : (refl R E).trans e = e := ext $ λ x, rfl
@[simp] lemma self_trans_symm : e.trans e.symm = refl R E := ext e.symm_apply_apply
@[simp] lemma symm_trans_self : e.symm.trans e = refl R₂ E₂ := ext e.apply_symm_apply
@[simp] lemma symm_comp_self : e.symm ∘ e = id := funext e.symm_apply_apply
@[simp] lemma self_comp_symm : e ∘ e.symm = id := e.symm.symm_comp_self
include σ₁₃ σ₂₁ σ₃₂ σ₃₁
@[simp] lemma symm_trans (e₁ : E ≃ₛₗᵢ[σ₁₂] E₂) (e₂ : E₂ ≃ₛₗᵢ[σ₂₃] E₃) :
(e₁.trans e₂).symm = e₂.symm.trans e₁.symm :=
rfl
lemma coe_symm_trans (e₁ : E ≃ₛₗᵢ[σ₁₂] E₂) (e₂ : E₂ ≃ₛₗᵢ[σ₂₃] E₃) :
⇑(e₁.trans e₂).symm = e₁.symm ∘ e₂.symm :=
rfl
include σ₁₄ σ₄₁ σ₄₂ σ₄₃ σ₂₄
lemma trans_assoc (eEE₂ : E ≃ₛₗᵢ[σ₁₂] E₂) (eE₂E₃ : E₂ ≃ₛₗᵢ[σ₂₃] E₃) (eE₃E₄ : E₃ ≃ₛₗᵢ[σ₃₄] E₄) :
eEE₂.trans (eE₂E₃.trans eE₃E₄) = (eEE₂.trans eE₂E₃).trans eE₃E₄ :=
rfl
omit σ₂₁ σ₃₁ σ₄₁ σ₃₂ σ₄₂ σ₄₃ σ₁₃ σ₂₄ σ₁₄
instance : group (E ≃ₗᵢ[R] E) :=
{ mul := λ e₁ e₂, e₂.trans e₁,
one := refl _ _,
inv := symm,
one_mul := trans_refl,
mul_one := refl_trans,
mul_assoc := λ _ _ _, trans_assoc _ _ _,
mul_left_inv := self_trans_symm }
@[simp] lemma coe_one : ⇑(1 : E ≃ₗᵢ[R] E) = id := rfl
@[simp] lemma coe_mul (e e' : E ≃ₗᵢ[R] E) : ⇑(e * e') = e ∘ e' := rfl
@[simp] lemma coe_inv (e : E ≃ₗᵢ[R] E) : ⇑(e⁻¹) = e.symm := rfl
lemma one_def : (1 : E ≃ₗᵢ[R] E) = refl _ _ := rfl
lemma mul_def (e e' : E ≃ₗᵢ[R] E) : (e * e' : E ≃ₗᵢ[R] E) = e'.trans e := rfl
lemma inv_def (e : E ≃ₗᵢ[R] E) : (e⁻¹ : E ≃ₗᵢ[R] E) = e.symm := rfl
/-! Lemmas about mixing the group structure with definitions. Because we have multiple ways to
express `linear_isometry_equiv.refl`, `linear_isometry_equiv.symm`, and
`linear_isometry_equiv.trans`, we want simp lemmas for every combination.
The assumption made here is that if you're using the group structure, you want to preserve it
after simp.
This copies the approach used by the lemmas near `equiv.perm.trans_one`. -/
@[simp] lemma trans_one : e.trans (1 : E₂ ≃ₗᵢ[R₂] E₂) = e := trans_refl _
@[simp] lemma one_trans : (1 : E ≃ₗᵢ[R] E).trans e = e := refl_trans _
@[simp] lemma refl_mul (e : E ≃ₗᵢ[R] E) : (refl _ _) * e = e := trans_refl _
@[simp] lemma mul_refl (e : E ≃ₗᵢ[R] E) : e * (refl _ _) = e := refl_trans _
include σ₂₁
/-- Reinterpret a `linear_isometry_equiv` as a `continuous_linear_equiv`. -/
instance : has_coe_t (E ≃ₛₗᵢ[σ₁₂] E₂) (E ≃SL[σ₁₂] E₂) :=
⟨λ e, ⟨e.to_linear_equiv, e.continuous, e.to_isometric.symm.continuous⟩⟩
instance : has_coe_t (E ≃ₛₗᵢ[σ₁₂] E₂) (E →SL[σ₁₂] E₂) := ⟨λ e, ↑(e : E ≃SL[σ₁₂] E₂)⟩
@[simp] lemma coe_coe : ⇑(e : E ≃SL[σ₁₂] E₂) = e := rfl
@[simp] lemma coe_coe' : ((e : E ≃SL[σ₁₂] E₂) : E →SL[σ₁₂] E₂) = e := rfl
@[simp] lemma coe_coe'' : ⇑(e : E →SL[σ₁₂] E₂) = e := rfl
omit σ₂₁
@[simp] lemma map_zero : e 0 = 0 := e.1.map_zero
@[simp] lemma map_add (x y : E) : e (x + y) = e x + e y := e.1.map_add x y
@[simp] lemma map_sub (x y : E) : e (x - y) = e x - e y := e.1.map_sub x y
@[simp] lemma map_smulₛₗ (c : R) (x : E) : e (c • x) = σ₁₂ c • e x := e.1.map_smulₛₗ c x
@[simp] lemma map_smul [module R E₂] {e : E ≃ₗᵢ[R] E₂} (c : R) (x : E) : e (c • x) = c • e x :=
e.1.map_smul c x
@[simp] lemma nnnorm_map (x : E) : ∥e x∥₊ = ∥x∥₊ := semilinear_isometry_class.nnnorm_map e x
@[simp] lemma dist_map (x y : E) : dist (e x) (e y) = dist x y :=
e.to_linear_isometry.dist_map x y
@[simp] lemma edist_map (x y : E) : edist (e x) (e y) = edist x y :=
e.to_linear_isometry.edist_map x y
protected lemma bijective : bijective e := e.1.bijective
protected lemma injective : injective e := e.1.injective
protected lemma surjective : surjective e := e.1.surjective
@[simp] lemma map_eq_iff {x y : E} : e x = e y ↔ x = y := e.injective.eq_iff
lemma map_ne {x y : E} (h : x ≠ y) : e x ≠ e y := e.injective.ne h
protected lemma lipschitz : lipschitz_with 1 e := e.isometry.lipschitz
protected lemma antilipschitz : antilipschitz_with 1 e := e.isometry.antilipschitz
lemma image_eq_preimage (s : set E) : e '' s = e.symm ⁻¹' s :=
e.to_linear_equiv.image_eq_preimage s
@[simp] lemma ediam_image (s : set E) : emetric.diam (e '' s) = emetric.diam s :=
e.isometry.ediam_image s
@[simp] lemma diam_image (s : set E) : metric.diam (e '' s) = metric.diam s :=
e.isometry.diam_image s
@[simp] lemma preimage_ball (x : E₂) (r : ℝ) :
e ⁻¹' (metric.ball x r) = metric.ball (e.symm x) r :=
e.to_isometric.preimage_ball x r
@[simp] lemma preimage_sphere (x : E₂) (r : ℝ) :
e ⁻¹' (metric.sphere x r) = metric.sphere (e.symm x) r :=
e.to_isometric.preimage_sphere x r
@[simp] lemma preimage_closed_ball (x : E₂) (r : ℝ) :
e ⁻¹' (metric.closed_ball x r) = metric.closed_ball (e.symm x) r :=
e.to_isometric.preimage_closed_ball x r
@[simp] lemma image_ball (x : E) (r : ℝ) :
e '' (metric.ball x r) = metric.ball (e x) r :=
e.to_isometric.image_ball x r
@[simp] lemma image_sphere (x : E) (r : ℝ) :
e '' (metric.sphere x r) = metric.sphere (e x) r :=
e.to_isometric.image_sphere x r
@[simp] lemma image_closed_ball (x : E) (r : ℝ) :
e '' (metric.closed_ball x r) = metric.closed_ball (e x) r :=
e.to_isometric.image_closed_ball x r
variables {α : Type*} [topological_space α]
@[simp] lemma comp_continuous_on_iff {f : α → E} {s : set α} :
continuous_on (e ∘ f) s ↔ continuous_on f s :=
e.isometry.comp_continuous_on_iff
@[simp] lemma comp_continuous_iff {f : α → E} :
continuous (e ∘ f) ↔ continuous f :=
e.isometry.comp_continuous_iff
instance complete_space_map (p : submodule R E) [complete_space p] :
complete_space (p.map (e.to_linear_equiv : E →ₛₗ[σ₁₂] E₂)) :=
e.to_linear_isometry.complete_space_map' p
include σ₂₁
/-- Construct a linear isometry equiv from a surjective linear isometry. -/
noncomputable def of_surjective (f : F →ₛₗᵢ[σ₁₂] E₂)
(hfr : function.surjective f) :
F ≃ₛₗᵢ[σ₁₂] E₂ :=
{ norm_map' := f.norm_map,
.. linear_equiv.of_bijective f.to_linear_map f.injective hfr }
@[simp] lemma coe_of_surjective (f : F →ₛₗᵢ[σ₁₂] E₂) (hfr : function.surjective f) :
⇑(linear_isometry_equiv.of_surjective f hfr) = f :=
by { ext, refl }
omit σ₂₁
variables (R)
/-- The negation operation on a normed space `E`, considered as a linear isometry equivalence. -/
def neg : E ≃ₗᵢ[R] E :=
{ norm_map' := norm_neg,
.. linear_equiv.neg R }
variables {R}
@[simp] lemma coe_neg : (neg R : E → E) = λ x, -x := rfl
@[simp] lemma symm_neg : (neg R : E ≃ₗᵢ[R] E).symm = neg R := rfl
variables (R E E₂ E₃)
/-- The natural equivalence `(E × E₂) × E₃ ≃ E × (E₂ × E₃)` is a linear isometry. -/
def prod_assoc [module R E₂] [module R E₃] : (E × E₂) × E₃ ≃ₗᵢ[R] E × E₂ × E₃ :=
{ to_fun := equiv.prod_assoc E E₂ E₃,
inv_fun := (equiv.prod_assoc E E₂ E₃).symm,
map_add' := by simp,
map_smul' := by simp,
norm_map' :=
begin
rintros ⟨⟨e, f⟩, g⟩,
simp only [linear_equiv.coe_mk, equiv.prod_assoc_apply, prod.norm_def, max_assoc],
end,
.. equiv.prod_assoc E E₂ E₃, }
@[simp] lemma coe_prod_assoc [module R E₂] [module R E₃] :
(prod_assoc R E E₂ E₃ : (E × E₂) × E₃ → E × E₂ × E₃) = equiv.prod_assoc E E₂ E₃ :=
rfl
@[simp] lemma coe_prod_assoc_symm [module R E₂] [module R E₃] :
((prod_assoc R E E₂ E₃).symm : E × E₂ × E₃ → (E × E₂) × E₃) = (equiv.prod_assoc E E₂ E₃).symm :=
rfl
/-- If `p` is a submodule that is equal to `⊤`, then `linear_isometry_equiv.of_top p hp` is the
"identity" equivalence between `p` and `E`. -/
@[simps to_linear_equiv apply symm_apply_coe]
def of_top {R : Type*} [ring R] [module R E] (p : submodule R E) (hp : p = ⊤) :
p ≃ₗᵢ[R] E :=
{ to_linear_equiv := linear_equiv.of_top p hp, .. p.subtypeₗᵢ }
variables {R E E₂ E₃} {R' : Type*} [ring R'] [module R' E] (p q : submodule R' E)
/-- `linear_equiv.of_eq` as a `linear_isometry_equiv`. -/
def of_eq (hpq : p = q) :
p ≃ₗᵢ[R'] q :=
{ norm_map' := λ x, rfl,
..linear_equiv.of_eq p q hpq }
variables {p q}
@[simp] lemma coe_of_eq_apply (h : p = q) (x : p) : (of_eq p q h x : E) = x := rfl
@[simp] lemma of_eq_symm (h : p = q) : (of_eq p q h).symm = of_eq q p h.symm := rfl
@[simp] lemma of_eq_rfl : of_eq p p rfl = linear_isometry_equiv.refl R' p := by ext; refl
end linear_isometry_equiv
/-- Two linear isometries are equal if they are equal on basis vectors. -/
lemma basis.ext_linear_isometry {ι : Type*} (b : basis ι R E) {f₁ f₂ : E →ₛₗᵢ[σ₁₂] E₂}
(h : ∀ i, f₁ (b i) = f₂ (b i)) : f₁ = f₂ :=
linear_isometry.to_linear_map_injective $ b.ext h
include σ₂₁
/-- Two linear isometric equivalences are equal if they are equal on basis vectors. -/
lemma basis.ext_linear_isometry_equiv {ι : Type*} (b : basis ι R E) {f₁ f₂ : E ≃ₛₗᵢ[σ₁₂] E₂}
(h : ∀ i, f₁ (b i) = f₂ (b i)) : f₁ = f₂ :=
linear_isometry_equiv.to_linear_equiv_injective $ b.ext' h
omit σ₂₁
/-- Reinterpret a `linear_isometry` as a `linear_isometry_equiv` to the range. -/
@[simps to_linear_equiv apply_coe]
noncomputable def linear_isometry.equiv_range {R S : Type*} [semiring R] [ring S] [module S E]
[module R F] {σ₁₂ : R →+* S} {σ₂₁ : S →+* R} [ring_hom_inv_pair σ₁₂ σ₂₁]
[ring_hom_inv_pair σ₂₁ σ₁₂] (f : F →ₛₗᵢ[σ₁₂] E) :
F ≃ₛₗᵢ[σ₁₂] f.to_linear_map.range :=
{ to_linear_equiv := linear_equiv.of_injective f.to_linear_map f.injective, .. f }
|
02e18f9e4fb8393a5a80e4e47c0cfc24e8053df3 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/topology/algebra/floor_ring.lean | 683cf5f27df8a9a020972b517fa574e1c21c1ffe | [
"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 | 9,287 | lean | /-
Copyright (c) 2020 Anatole Dedecker. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anatole Dedecker
Basic topological facts (limits and continuity) about `floor`,
`ceil` and `fract` in a `floor_ring`.
-/
import topology.algebra.ordered.basic
import algebra.floor
open set function filter
open_locale topological_space
variables {α : Type*} [linear_ordered_ring α] [floor_ring α]
lemma tendsto_floor_at_top : tendsto (floor : α → ℤ) at_top at_top :=
begin
refine monotone.tendsto_at_top_at_top (λ a b hab, floor_mono hab) (λ b, _),
use (b : α) + ((1 : ℤ) : α),
rw [floor_add_int, floor_coe],
exact (lt_add_one _).le
end
lemma tendsto_floor_at_bot : tendsto (floor : α → ℤ) at_bot at_bot :=
begin
refine monotone.tendsto_at_bot_at_bot (λ a b hab, floor_mono hab) (λ b, ⟨b, _⟩),
rw floor_coe
end
lemma tendsto_ceil_at_top : tendsto (ceil : α → ℤ) at_top at_top :=
tendsto_neg_at_bot_at_top.comp (tendsto_floor_at_bot.comp tendsto_neg_at_top_at_bot)
lemma tendsto_ceil_at_bot : tendsto (ceil : α → ℤ) at_bot at_bot :=
tendsto_neg_at_top_at_bot.comp (tendsto_floor_at_top.comp tendsto_neg_at_bot_at_top)
variables [topological_space α]
lemma continuous_on_floor (n : ℤ) : continuous_on (λ x, floor x : α → α) (Ico n (n+1) : set α) :=
(continuous_on_congr $ floor_eq_on_Ico' n).mpr continuous_on_const
lemma continuous_on_ceil (n : ℤ) : continuous_on (λ x, ceil x : α → α) (Ioc (n-1) n : set α) :=
(continuous_on_congr $ ceil_eq_on_Ioc' n).mpr continuous_on_const
lemma tendsto_floor_right' [order_closed_topology α] (n : ℤ) :
tendsto (λ x, floor x : α → α) (𝓝[Ici n] n) (𝓝 n) :=
begin
rw ← nhds_within_Ico_eq_nhds_within_Ici (lt_add_one (n : α)),
simpa only [floor_coe] using
(continuous_on_floor n _ (left_mem_Ico.mpr $ lt_add_one (_ : α))).tendsto
end
lemma tendsto_ceil_left' [order_closed_topology α] (n : ℤ) :
tendsto (λ x, ceil x : α → α) (𝓝[Iic n] n) (𝓝 n) :=
begin
rw ← nhds_within_Ioc_eq_nhds_within_Iic (sub_one_lt (n : α)),
simpa only [ceil_coe] using
(continuous_on_ceil _ _ (right_mem_Ioc.mpr $ sub_one_lt (_ : α))).tendsto
end
lemma tendsto_floor_right [order_closed_topology α] (n : ℤ) :
tendsto (λ x, floor x : α → α) (𝓝[Ici n] n) (𝓝[Ici n] n) :=
tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (tendsto_floor_right' _)
begin
refine (eventually_nhds_with_of_forall $ λ x (hx : (n : α) ≤ x), _),
change _ ≤ _,
norm_cast,
convert ← floor_mono hx,
rw floor_eq_iff,
exact ⟨le_refl _, lt_add_one _⟩
end
lemma tendsto_ceil_left [order_closed_topology α] (n : ℤ) :
tendsto (λ x, ceil x : α → α) (𝓝[Iic n] n) (𝓝[Iic n] n) :=
tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (tendsto_ceil_left' _)
begin
refine (eventually_nhds_with_of_forall $ λ x (hx : x ≤ (n : α)), _),
change _ ≤ _,
norm_cast,
convert ← ceil_mono hx,
rw ceil_eq_iff,
exact ⟨sub_one_lt _, le_refl _⟩
end
lemma tendsto_floor_left [order_closed_topology α] (n : ℤ) :
tendsto (λ x, floor x : α → α) (𝓝[Iio n] n) (𝓝[Iic (n-1)] (n-1)) :=
begin
rw ← nhds_within_Ico_eq_nhds_within_Iio (sub_one_lt (n : α)),
convert (tendsto_nhds_within_congr $ (λ x hx, (floor_eq_on_Ico' (n-1) x hx).symm))
(tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ tendsto_const_nhds
(eventually_of_forall (λ _, mem_Iic.mpr $ le_refl _)));
norm_cast <|> apply_instance,
ring
end
lemma tendsto_ceil_right [order_closed_topology α] (n : ℤ) :
tendsto (λ x, ceil x : α → α) (𝓝[Ioi n] n) (𝓝[Ici (n+1)] (n+1)) :=
begin
rw ← nhds_within_Ioc_eq_nhds_within_Ioi (lt_add_one (n : α)),
convert (tendsto_nhds_within_congr $ (λ x hx, (ceil_eq_on_Ioc' (n+1) x hx).symm))
(tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ tendsto_const_nhds
(eventually_of_forall (λ _, mem_Ici.mpr $ le_refl _)));
norm_cast <|> apply_instance,
ring
end
lemma tendsto_floor_left' [order_closed_topology α] (n : ℤ) :
tendsto (λ x, floor x : α → α) (𝓝[Iio n] n) (𝓝 (n-1)) :=
begin
rw ← nhds_within_univ,
exact tendsto_nhds_within_mono_right (subset_univ _) (tendsto_floor_left n),
end
lemma tendsto_ceil_right' [order_closed_topology α] (n : ℤ) :
tendsto (λ x, ceil x : α → α) (𝓝[Ioi n] n) (𝓝 (n+1)) :=
begin
rw ← nhds_within_univ,
exact tendsto_nhds_within_mono_right (subset_univ _) (tendsto_ceil_right n),
end
lemma continuous_on_fract [topological_add_group α] (n : ℤ) :
continuous_on (fract : α → α) (Ico n (n+1) : set α) :=
continuous_on_id.sub (continuous_on_floor n)
lemma tendsto_fract_left' [order_closed_topology α] [topological_add_group α]
(n : ℤ) : tendsto (fract : α → α) (𝓝[Iio n] n) (𝓝 1) :=
begin
convert (tendsto_nhds_within_of_tendsto_nhds tendsto_id).sub (tendsto_floor_left' n);
[{norm_cast, ring}, apply_instance, apply_instance]
end
lemma tendsto_fract_left [order_closed_topology α] [topological_add_group α]
(n : ℤ) : tendsto (fract : α → α) (𝓝[Iio n] n) (𝓝[Iio 1] 1) :=
tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _
(tendsto_fract_left' _) (eventually_of_forall fract_lt_one)
lemma tendsto_fract_right' [order_closed_topology α] [topological_add_group α]
(n : ℤ) : tendsto (fract : α → α) (𝓝[Ici n] n) (𝓝 0) :=
begin
convert (tendsto_nhds_within_of_tendsto_nhds tendsto_id).sub (tendsto_floor_right' n);
[exact (sub_self _).symm, apply_instance, apply_instance]
end
lemma tendsto_fract_right [order_closed_topology α] [topological_add_group α]
(n : ℤ) : tendsto (fract : α → α) (𝓝[Ici n] n) (𝓝[Ici 0] 0) :=
tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _
(tendsto_fract_right' _) (eventually_of_forall fract_nonneg)
local notation `I` := (Icc 0 1 : set α)
lemma continuous_on.comp_fract' {β γ : Type*} [order_topology α]
[topological_add_group α] [topological_space β] [topological_space γ] {f : β → α → γ}
(h : continuous_on (uncurry f) $ (univ : set β).prod I) (hf : ∀ s, f s 0 = f s 1) :
continuous (λ st : β × α, f st.1 $ fract st.2) :=
begin
change continuous ((uncurry f) ∘ (prod.map id (fract))),
rw continuous_iff_continuous_at,
rintro ⟨s, t⟩,
by_cases ht : t = floor t,
{ rw ht,
rw ← continuous_within_at_univ,
have : (univ : set (β × α)) ⊆ (set.prod univ (Iio $ floor t)) ∪ (set.prod univ (Ici $ floor t)),
{ rintros p -,
rw ← prod_union,
exact ⟨true.intro, lt_or_le _ _⟩ },
refine continuous_within_at.mono _ this,
refine continuous_within_at.union _ _,
{ simp only [continuous_within_at, fract_coe, nhds_within_prod_eq,
nhds_within_univ, id.def, comp_app, prod.map_mk],
have : (uncurry f) (s, 0) = (uncurry f) (s, (1 : α)),
by simp [uncurry, hf],
rw this,
refine (h _ ⟨true.intro, by exact_mod_cast right_mem_Icc.mpr zero_le_one⟩).tendsto.comp _,
rw [nhds_within_prod_eq, nhds_within_univ],
rw nhds_within_Icc_eq_nhds_within_Iic (@zero_lt_one α _ _),
exact tendsto_id.prod_map
(tendsto_nhds_within_mono_right Iio_subset_Iic_self $ tendsto_fract_left _) },
{ simp only [continuous_within_at, fract_coe, nhds_within_prod_eq,
nhds_within_univ, id.def, comp_app, prod.map_mk],
refine (h _ ⟨true.intro, by exact_mod_cast left_mem_Icc.mpr zero_le_one⟩).tendsto.comp _,
rw [nhds_within_prod_eq, nhds_within_univ,
nhds_within_Icc_eq_nhds_within_Ici (@zero_lt_one α _ _)],
exact tendsto_id.prod_map (tendsto_fract_right _) } },
{ have : t ∈ Ioo (floor t : α) ((floor t : α) + 1),
from ⟨lt_of_le_of_ne (floor_le t) (ne.symm ht), lt_floor_add_one _⟩,
apply (h ((prod.map _ fract) _) ⟨trivial, ⟨fract_nonneg _, (fract_lt_one _).le⟩⟩).tendsto.comp,
simp only [nhds_prod_eq, nhds_within_prod_eq, nhds_within_univ, id.def, prod.map_mk],
exact continuous_at_id.tendsto.prod_map
(tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _
(((continuous_on_fract _ _ (Ioo_subset_Ico_self this)).mono
Ioo_subset_Ico_self).continuous_at (Ioo_mem_nhds this.1 this.2))
(eventually_of_forall (λ x, ⟨fract_nonneg _, (fract_lt_one _).le⟩)) ) }
end
lemma continuous_on.comp_fract {β : Type*} [order_topology α]
[topological_add_group α] [topological_space β] {f : α → β}
(h : continuous_on f I) (hf : f 0 = f 1) : continuous (f ∘ fract) :=
begin
let f' : unit → α → β := λ x y, f y,
have : continuous_on (uncurry f') ((univ : set unit).prod I),
{ rintros ⟨s, t⟩ ⟨-, ht : t ∈ I⟩,
simp only [continuous_within_at, uncurry, nhds_within_prod_eq, nhds_within_univ, f'],
rw tendsto_prod_iff,
intros W hW,
specialize h t ht hW,
rw mem_map_sets_iff at h,
rcases h with ⟨V, hV, hVW⟩,
rw image_subset_iff at hVW,
use [univ, univ_mem_sets, V, hV],
intros x y hx hy,
exact hVW hy },
have key : continuous (λ s, ⟨unit.star, s⟩ : α → unit × α) := by continuity,
exact (this.comp_fract' (λ s, hf)).comp key
end
|
8e2e52040e9949691fdad3641ec5e9d54da3074f | 79cc757e5e5b09c7a522f717a6c490d321d36469 | /src/mywork/hw4.lean | 5d509a14befc7a0bf9a70b1213fdef1349b4d3bf | [] | no_license | LukeMathe/cs2120f21 | 534c3b8868dcfdea98a9d22513180c8a062794c6 | d51940b174569a8782e62ae027b108b5f099a9aa | refs/heads/main | 1,693,418,664,935 | 1,634,585,653,000 | 1,634,585,653,000 | 403,762,076 | 0 | 0 | null | 1,630,963,485,000 | 1,630,963,485,000 | null | UTF-8 | Lean | false | false | 6,292 | lean | /-Luke Mathe, lkm6eka-/
-- 1
example : 0 ≠ 1 :=
begin
-- ¬ (0 = 1)
-- (0 = 1) → false
assume h,
cases h,
end
-- 2
example : 0 ≠ 0 → 2 = 3 :=
begin
assume h,
have f : false := h (eq.refl 0),
exact false.elim (f),
end
-- 3
example : ∀ (P : Prop), P → ¬¬P :=
begin
assume P,
assume (p : P),
-- ¬¬P
-- ¬P → false
-- (P → false) → false
assume h,
have f := h p,
exact f,
end
-- We might need classical (vs constructive) reasoning
#check classical.em
open classical
#check em
/-
axiom em : ∀ (p : Prop), p ∨ ¬p
This is the famous and historically controversial
"law" (now axiom) of the excluded middle. It's is
a key to proving many intuitive theorems in logic
and mathematics. But it also leads to giving up on
having evidence *why* something is either true or
not true, in that you no longer need a proof of
either P or of ¬P to have a proof of P ∨ ¬P.
-/
-- 4
theorem neg_elim : ∀ (P : Prop), ¬¬P → P :=
begin
assume P,
assume h,
have pornp := classical.em P,
cases pornp with p pn,
assumption,
contradiction,
end
-- 5
theorem demorgan_1 : ∀ (P Q : Prop), ¬ (P ∧ Q) ↔ ¬ P ∨ ¬ Q :=
begin
assume P Q,
apply iff.intro _ _,
--forwards
assume h,
have pornp := classical.em P,
have qornq := classical.em Q,
cases pornp,
cases qornq,
apply or.intro_left,
assume p,
apply h,
apply and.intro _ _,
apply p,
apply qornq,
apply or.intro_right,
apply qornq,
apply or.intro_left,
apply pornp,
--backwards
assume npornq,
apply not.intro,
assume pandq,
apply or.elim npornq,
assume np,
apply np,
cases pandq with p q,
apply p,
assume nq,
apply nq,
cases pandq with p q,
apply q,
end
-- 6
theorem demorgan_2 : ∀ (P Q : Prop), ¬ (P ∨ Q) → ¬P ∧ ¬Q :=
begin
assume P Q,
assume notporq,
apply and.intro _ _,
assume p,
apply notporq,
apply or.intro_left,
apply p,
assume q,
apply notporq,
apply or.intro_right,
apply q,
end
-- 7
theorem disappearing_opposite :
∀ (P Q : Prop), P ∨ ¬P ∧ Q ↔ P ∨ Q :=
begin
assume P Q,
apply iff.intro _ _,
--forwards
assume pornpandq,
have pornp := classical.em P,
cases pornp with p np,
apply or.intro_left,
apply p,
apply or.intro_right,
apply or.elim pornpandq,
assume p,
contradiction,
assume notpandq,
apply and.elim_right notpandq,
--backwards
assume porq,
have pornp := classical.em P,
cases pornp,
cases porq,
apply or.intro_left,
apply pornp,
apply or.intro_left,
apply pornp,
apply or.elim porq,
assume p,
contradiction,
assume q,
apply or.intro_right,
apply and.intro pornp q,
end
-- 8
theorem distrib_and_or :
∀ (P Q R: Prop), (P ∨ Q) ∧ (P ∨ R) ↔
P ∨ (Q ∧ R) :=
begin
assume P Q R,
apply iff.intro _ _,
--forwards
assume porqandporr,
apply and.elim porqandporr,
assume porq,
assume porr,
apply or.elim porq,
assume p,
apply or.intro_left _ _,
apply p,
assume q,
apply or.elim porr,
assume p,
apply or.intro_left _ _,
apply p,
assume r,
apply or.intro_right _ _,
apply and.intro q r,
-- backwards
assume porqandr,
apply and.intro _ _,
apply or.elim porqandr,
assume p,
apply or.intro_left,
apply p,
assume qandr,
apply or.intro_right,
apply and.elim_left qandr,
apply or.elim porqandr,
assume p,
apply or.intro_left,
apply p,
assume qandr,
apply or.intro_right,
apply and.elim_right qandr,
end
-- remember or is right associative
-- you need this to know what the lefts and rights are
-- 9
theorem distrib_and_or_foil :
∀ (P Q R S : Prop),
(P ∨ Q) ∧ (R ∨ S) ↔
(P ∧ R) ∨ (P ∧ S) ∨ (Q ∧ R) ∨ (Q ∧ S) :=
begin
assume P Q R S,
apply iff.intro _ _,
--forwards
assume porqandrors,
have porq := and.left porqandrors,
have rors := and.right porqandrors,
cases porq,
cases rors,
apply or.intro_left,
apply and.intro,
apply porq,
apply rors,
apply or.intro_right,
apply or.intro_left,
apply and.intro _ _,
apply porq,
apply rors,
apply or.elim rors,
assume r,
apply or.intro_right,
apply or.intro_right,
apply or.intro_left,
apply and.intro _ _,
apply porq,
apply r,
assume s,
apply or.intro_right,
apply or.intro_right,
apply or.intro_right,
apply and.intro _ _,
apply porq,
apply s,
--backwards
assume h,
cases h,
apply and.intro _ _,
apply or.intro_left,
apply and.elim_left h,
apply or.intro_left,
apply and.elim_right h,
apply and.intro _ _,
cases h,
apply or.intro_left,
apply and.elim_left h,
apply or.intro_right,
apply or.elim h,
assume qandr,
apply and.elim_left qandr,
assume qands,
apply and.elim_left qands,
cases h,
apply or.intro_right,
apply and.elim_right h,
apply or.elim h,
assume qandr,
apply or.intro_left,
apply and.elim_right qandr,
assume qands,
apply or.intro_right,
apply and.elim_right qands,
end
/- 10
Formally state and prove the proposition that
not every natural number is equal to zero.
-/
lemma not_all_nats_are_zero : ¬ ∀( n : ℕ ), n = 0 :=
begin
assume h,
have f := h 1,
contradiction,
end
-- 11. equivalence of P→Q and (¬P∨Q)
example : ∀ (P Q : Prop), (P → Q) ↔ (¬P ∨ Q) :=
begin
assume P Q,
apply iff.intro _ _,
--forwards
have pornp := classical.em P,
apply or.elim pornp,
assume p,
assume pimpq,
apply or.intro_right,
apply pimpq,
apply p,
assume np,
assume pimpq,
apply or.intro_left,
apply np,
--backwards
assume nporq,
assume p,
cases nporq,
contradiction,
apply nporq,
end
-- 12
example : ∀ (P Q : Prop), (P → Q) → (¬ Q → ¬ P) :=
begin
assume P Q,
assume pimpq,
have pimpqornpimpq := classical.em (P → Q),
cases pimpqornpimpq,
assume nq,
apply not.intro,
assume p,
apply nq,
apply pimpq,
apply p,
assume nq,
apply not.intro,
assume p,
apply nq,
apply pimpq,
apply p,
end
-- 13
example : ∀ (P Q : Prop), ( ¬P → ¬Q) → (Q → P) :=
begin
assume P Q,
assume npimpnq,
have pornp := classical.em P,
have qornq := classical.em Q,
cases pornp,
cases qornq,
assume q,
apply pornp,
assume q,
apply pornp,
assume q,
have nq := npimpnq pornp,
contradiction,
end
|
10f32505f5445d5289169886d7a1caafb35a5be1 | 60bf3fa4185ec5075eaea4384181bfbc7e1dc319 | /src/game/series/tempLevel01.lean | b3c69a3f7d7e0ffa2ef66b6f13d8c3975b0e6a86 | [
"Apache-2.0"
] | permissive | anrddh/real-number-game | 660f1127d03a78fd35986c771d65c3132c5f4025 | c708c4e02ec306c657e1ea67862177490db041b0 | refs/heads/master | 1,668,214,277,092 | 1,593,105,075,000 | 1,593,105,075,000 | 264,269,218 | 0 | 0 | null | 1,589,567,264,000 | 1,589,567,264,000 | null | UTF-8 | Lean | false | false | 294 | lean | import game.series.L01defs
/-
Temporary level 01: If $\sum a_n$ converges, then $a_n \to 0$.
-/
/- Lemma
If partial sum sequence of $a_n$ convergent, $a_n → 0$.
-/
lemma sum_converges (a : ℕ → ℝ) :
is_convergent (partial_sum_sequence a) → is_limit a 0 :=
begin
sorry,
end
|
1c10c80670abe9cb2d54dfd4ff6423b655657a45 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/blast_ematch1.lean | 901de7d4e70db7fc9c3ab545567d619ab8d9ec28 | [
"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 | 255 | lean | import data.nat
open nat
constant f : nat → nat
constant g : nat → nat
definition lemma1 [forward] : ∀ x, (:g (f x):) = x :=
sorry
set_option blast.strategy "ematch"
example (a b c : nat) : a = f b → a = f c → g a ≠ b → false :=
by blast
|
f928401bf8e96c8b8681c79b4cb817d116f9495a | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /test/simp_rw.lean | a4194e91e106bf8f628d50652b58b4525c60dd2f | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 1,611 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen
-/
import data.nat.basic
import data.set.image
import tactic.simp_rw
/-!
# Tests for `simp_rw` extensions
-/
-- `simp_rw` can perform rewrites under binders:
example : (λ (x y : ℕ), x + y) = (λ x y, y + x) := by simp_rw [add_comm]
-- `simp_rw` can apply reverse rules:
example (f : ℕ → ℕ) {a b c : ℕ} (ha : f b = a) (hc : f b = c) : a = c := by simp_rw [← ha, hc]
-- `simp_rw` performs rewrites in the given order (`simp` fails on this example):
example {α β : Type} {f : α → β} {t : set β} :
(∀ s, f '' s ⊆ t) = ∀ s : set α, ∀ x ∈ s, x ∈ f ⁻¹' t :=
by simp_rw [set.image_subset_iff, set.subset_def]
-- `simp_rw` applies rewrite rules multiple times:
example (a b c d : ℕ) : a + (b + (c + d)) = ((d + c) + b) + a := by simp_rw [add_comm]
-- `simp_rw` can also rewrite in assumptions:
example (p : ℕ → Prop) (a b : ℕ) (h : p (a + b)) : p (b + a) :=
by {simp_rw [add_comm a b] at h, exact h}
-- or explicitly rewrite at the goal:
example (p : ℕ → Prop) (a b : ℕ) (h : p (a + b)) : p (b + a) :=
by {simp_rw [add_comm b a] at ⊢, exact h}
-- or at multiple assumptions:
example (p : ℕ → Prop) (a b : ℕ) (h₁ : p (b + a) → p (a + b)) (h₂ : p (a + b)) : p (b + a) :=
by {simp_rw [add_comm a b] at h₁ h₂, exact h₁ h₂}
-- or everywhere:
example (p : ℕ → Prop) (a b : ℕ) (h₁ : p (b + a) → p (a + b)) (h₂ : p (a + b)) : p (a + b) :=
by {simp_rw [add_comm a b] at *, exact h₁ h₂}
|
8005629300f5477c36631c39b43e5a99e5345652 | 4bddde0d06fbd53be6f23d7f5899998e8f63410b | /src/tactic/iconfig/tactic.lean | 1b10e9714f7db65934625a2cd5c4d86aeb5ed926 | [] | no_license | khoek/libiconfig | 4816290a5862af14b07683b3d2663e8e62832ef4 | 6f55c50bc5d852d26ee5ee4c5b52b2cda2a852e5 | refs/heads/master | 1,586,109,683,212 | 1,559,567,916,000 | 1,559,567,916,000 | 157,085,466 | 0 | 1 | null | 1,559,567,917,000 | 1,541,945,134,000 | Lean | UTF-8 | Lean | false | false | 2,782 | lean | import .types
import .monad
import .env
namespace iparam
namespace handler
open lean.parser interactive interactive.types
open iconfig cfgopt
meta def bool (n : name) (v : parse (letval pbool)) : tactic unit :=
publish n $ value.bool v
meta def nat (n : name) (v : parse (letval small_nat)) : tactic unit :=
publish n $ value.nat v
meta def enat (n : name) (v : parse (letval enat)) : tactic unit :=
publish n $ value.enat v
meta def string (n : name) (v : parse (letval pstring)) : tactic unit :=
publish n $ value.string v
meta def name (n : name) (v : parse ident) : tactic unit :=
publish n $ value.name v
meta def lpexpr (n : _root_.name) (v : parse (letval texpr)) : tactic unit :=
publish n $ value.pexpr v
meta def pexpr (n : _root_.name) (v : parse texpr) : tactic unit :=
publish n $ value.pexpr v
meta def list (t : type) (n : _root_.name) (v : parse (letval lean.parser.pexpr)) : tactic unit := do
e ← tactic.to_expr v,
publish n $ value.list t e
end handler
open iconfig tactic
open lean.parser interactive interactive.types
private meta def handle_member (cfgn : name) (n : name) (val : pexpr) (default : option pexpr) : tactic unit := do
iconfig.env_add cfgn n val,
match default with
| none := tactic.skip
| some default :=
to_expr default >>= eval_expr schema >>= env_add_schema cfgn n
end
meta def generic (val : pexpr) (cfgn : name) (n : name) : lean.parser unit := do
default_present ← optional $ lean.parser.tk ":=",
default ← if default_present.is_some then some <$> texpr
else pure none,
handle_member cfgn n val default
meta def custom (cfgn : name) (n : name) : lean.parser unit := do
tac ← ident,
of_tactic' $ resolve_name tac >>= iconfig.env_add cfgn n
meta def bool := generic ``(iparam.handler.bool)
meta def nat := generic ``(iparam.handler.nat)
meta def enat := generic ``(iparam.handler.enat)
meta def string := generic ``(iparam.handler.string)
meta def name := generic ``(iparam.handler.name)
meta def lpexpr := generic ``(iparam.handler.lpexpr)
meta def pexpr := generic ``(iparam.handler.pexpr)
meta def list := generic ``(iparam.handler.list)
end iparam
namespace cfgopt.type
open cfgopt.type
meta def to_reader_pexpr : cfgopt.type → _root_.pexpr
| bool := ``(iparam.handler.bool)
| nat := ``(iparam.handler.nat)
| enat := ``(iparam.handler.enat)
| string := ``(iparam.handler.string)
| name := ``(iparam.handler.name)
| pexpr := ``(iparam.handler.pexpr)
| (list t) := ``(iparam.handler.list) $ pexpr.of_expr `(t)
end cfgopt.type
namespace iconfig
meta def env_add_struct (cfgn : name) (struct : name) : tactic unit := do
e ← tactic.get_env,
l ← iconfig.get_struct_types e struct,
l.mmap' (λ s, iconfig.env_add cfgn s.1 s.2.to_reader_pexpr)
end iconfig
|
5f120879b0c47f6629b0173c342a84ea7c0c3cb7 | 3dd1b66af77106badae6edb1c4dea91a146ead30 | /tests/lean/run/tactic28.lean | d3d3b77428607b0677d6483cbd7512b005f53243 | [
"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 | 787 | lean | import standard
using tactic
inductive sum (A : Type) (B : Type) : Type :=
| inl : A → sum A B
| inr : B → sum A B
theorem inl_inhabited {A : Type} (B : Type) (H : inhabited A) : inhabited (sum A B)
:= inhabited_elim H (λ a, inhabited_intro (inl B a))
theorem inr_inhabited (A : Type) {B : Type} (H : inhabited B) : inhabited (sum A B)
:= inhabited_elim H (λ b, inhabited_intro (inr A b))
infixl `..`:100 := append
definition my_tac := repeat (trace "iteration"; state;
( apply @inl_inhabited; trace "used inl"
.. apply @inr_inhabited; trace "used inr"
.. apply @num.inhabited_num; trace "used num")) ; now
tactic_hint [inhabited] my_tac
theorem T : inhabited (sum false num.num)
|
dbcaa4802af667b712f423b21ca640737ab7fc19 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/data/multiset/lattice.lean | 12cf487ef99d97e384c3fbe12cbcdab65f94ec26 | [
"Apache-2.0"
] | permissive | robertylewis/mathlib | 3d16e3e6daf5ddde182473e03a1b601d2810952c | 1d13f5b932f5e40a8308e3840f96fc882fae01f0 | refs/heads/master | 1,651,379,945,369 | 1,644,276,960,000 | 1,644,276,960,000 | 98,875,504 | 0 | 0 | Apache-2.0 | 1,644,253,514,000 | 1,501,495,700,000 | Lean | UTF-8 | Lean | false | false | 4,073 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import data.multiset.finset_ops
import data.multiset.fold
/-!
# Lattice operations on multisets
-/
namespace multiset
variables {α : Type*}
/-! ### sup -/
section sup
-- can be defined with just `[has_bot α]` where some lemmas hold without requiring `[order_bot α]`
variables [semilattice_sup α] [order_bot α]
/-- Supremum of a multiset: `sup {a, b, c} = a ⊔ b ⊔ c` -/
def sup (s : multiset α) : α := s.fold (⊔) ⊥
@[simp] lemma sup_zero : (0 : multiset α).sup = ⊥ :=
fold_zero _ _
@[simp] lemma sup_cons (a : α) (s : multiset α) :
(a ::ₘ s).sup = a ⊔ s.sup :=
fold_cons_left _ _ _ _
@[simp] lemma sup_singleton {a : α} : ({a} : multiset α).sup = a :=
sup_bot_eq
@[simp] lemma sup_add (s₁ s₂ : multiset α) : (s₁ + s₂).sup = s₁.sup ⊔ s₂.sup :=
eq.trans (by simp [sup]) (fold_add _ _ _ _ _)
lemma sup_le {s : multiset α} {a : α} : s.sup ≤ a ↔ (∀b ∈ s, b ≤ a) :=
multiset.induction_on s (by simp)
(by simp [or_imp_distrib, forall_and_distrib] {contextual := tt})
lemma le_sup {s : multiset α} {a : α} (h : a ∈ s) : a ≤ s.sup :=
sup_le.1 le_rfl _ h
lemma sup_mono {s₁ s₂ : multiset α} (h : s₁ ⊆ s₂) : s₁.sup ≤ s₂.sup :=
sup_le.2 $ assume b hb, le_sup (h hb)
variables [decidable_eq α]
@[simp] lemma sup_erase_dup (s : multiset α) : (erase_dup s).sup = s.sup :=
fold_erase_dup_idem _ _ _
@[simp] lemma sup_ndunion (s₁ s₂ : multiset α) :
(ndunion s₁ s₂).sup = s₁.sup ⊔ s₂.sup :=
by rw [← sup_erase_dup, erase_dup_ext.2, sup_erase_dup, sup_add]; simp
@[simp] lemma sup_union (s₁ s₂ : multiset α) :
(s₁ ∪ s₂).sup = s₁.sup ⊔ s₂.sup :=
by rw [← sup_erase_dup, erase_dup_ext.2, sup_erase_dup, sup_add]; simp
@[simp] lemma sup_ndinsert (a : α) (s : multiset α) :
(ndinsert a s).sup = a ⊔ s.sup :=
by rw [← sup_erase_dup, erase_dup_ext.2, sup_erase_dup, sup_cons]; simp
lemma nodup_sup_iff {α : Type*} [decidable_eq α] {m : multiset (multiset α) } :
m.sup.nodup ↔ ∀ (a : multiset α), a ∈ m → a.nodup :=
begin
apply m.induction_on,
{ simp },
{ intros a s h,
simp [h] }
end
end sup
/-! ### inf -/
section inf
-- can be defined with just `[has_top α]` where some lemmas hold without requiring `[order_top α]`
variables [semilattice_inf α] [order_top α]
/-- Infimum of a multiset: `inf {a, b, c} = a ⊓ b ⊓ c` -/
def inf (s : multiset α) : α := s.fold (⊓) ⊤
@[simp] lemma inf_zero : (0 : multiset α).inf = ⊤ :=
fold_zero _ _
@[simp] lemma inf_cons (a : α) (s : multiset α) :
(a ::ₘ s).inf = a ⊓ s.inf :=
fold_cons_left _ _ _ _
@[simp] lemma inf_singleton {a : α} : ({a} : multiset α).inf = a :=
inf_top_eq
@[simp] lemma inf_add (s₁ s₂ : multiset α) : (s₁ + s₂).inf = s₁.inf ⊓ s₂.inf :=
eq.trans (by simp [inf]) (fold_add _ _ _ _ _)
lemma le_inf {s : multiset α} {a : α} : a ≤ s.inf ↔ (∀b ∈ s, a ≤ b) :=
multiset.induction_on s (by simp)
(by simp [or_imp_distrib, forall_and_distrib] {contextual := tt})
lemma inf_le {s : multiset α} {a : α} (h : a ∈ s) : s.inf ≤ a :=
le_inf.1 le_rfl _ h
lemma inf_mono {s₁ s₂ : multiset α} (h : s₁ ⊆ s₂) : s₂.inf ≤ s₁.inf :=
le_inf.2 $ assume b hb, inf_le (h hb)
variables [decidable_eq α]
@[simp] lemma inf_erase_dup (s : multiset α) : (erase_dup s).inf = s.inf :=
fold_erase_dup_idem _ _ _
@[simp] lemma inf_ndunion (s₁ s₂ : multiset α) :
(ndunion s₁ s₂).inf = s₁.inf ⊓ s₂.inf :=
by rw [← inf_erase_dup, erase_dup_ext.2, inf_erase_dup, inf_add]; simp
@[simp] lemma inf_union (s₁ s₂ : multiset α) :
(s₁ ∪ s₂).inf = s₁.inf ⊓ s₂.inf :=
by rw [← inf_erase_dup, erase_dup_ext.2, inf_erase_dup, inf_add]; simp
@[simp] lemma inf_ndinsert (a : α) (s : multiset α) :
(ndinsert a s).inf = a ⊓ s.inf :=
by rw [← inf_erase_dup, erase_dup_ext.2, inf_erase_dup, inf_cons]; simp
end inf
end multiset
|
84db5c451c7dc3073bbfe396a5d9836e99f1149e | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/funExpected.lean | 1b49d75a3b0640e74038aa42f52cff1892e0e1c3 | [
"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 | 70 | lean | --
def f (xs : List Nat) : List Nat :=
xs.map (fun x => x + 1) [1]
|
118e1520d1a9c530eed491bec7dc98cfc3653bd8 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/algebra/char_p/default_auto.lean | 14cc3353c35ebcbef22fa72f7173023cd66b7e86 | [] | 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 | 261 | lean | import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.algebra.char_p.basic
import Mathlib.algebra.char_p.pi
import Mathlib.algebra.char_p.quotient
import Mathlib.algebra.char_p.subring
import Mathlib.PostPort
namespace Mathlib
end Mathlib |
0a6135b73d0bbdeb26fc5af1b6d2e6eba4286bdb | 3863d2564418bccb1859e057bf5a4ef240e75fd7 | /hott/types/trunc.hlean | 048defcca61b9d485207b47daa3e60feaffcf3a3 | [
"Apache-2.0"
] | permissive | JacobGross/lean | 118bbb067ff4d4af48a266face2c7eb9868fa91c | eb26087df940c54337cb807b4bc6d345d1fc1085 | refs/heads/master | 1,582,735,011,532 | 1,462,557,826,000 | 1,462,557,826,000 | 46,451,196 | 0 | 0 | null | 1,462,557,826,000 | 1,447,885,161,000 | C++ | UTF-8 | Lean | false | false | 24,728 | hlean | /-
Copyright (c) 2015 Jakob von Raumer. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
Properties of trunc_index, is_trunc, trunctype, trunc, and the pointed versions of these
-/
-- NOTE: the fact that (is_trunc n A) is a mere proposition is proved in .prop_trunc
import .pointed ..function algebra.order types.nat.order
open eq sigma sigma.ops pi function equiv trunctype
is_equiv prod pointed nat is_trunc algebra sum
/- basic computation with ℕ₋₂, its operations and its order -/
namespace trunc_index
definition minus_one_le_succ (n : ℕ₋₂) : -1 ≤ n.+1 :=
succ_le_succ (minus_two_le n)
definition zero_le_of_nat (n : ℕ) : 0 ≤ of_nat n :=
succ_le_succ !minus_one_le_succ
open decidable
protected definition has_decidable_eq [instance] : Π(n m : ℕ₋₂), decidable (n = m)
| has_decidable_eq -2 -2 := inl rfl
| has_decidable_eq (n.+1) -2 := inr (by contradiction)
| has_decidable_eq -2 (m.+1) := inr (by contradiction)
| has_decidable_eq (n.+1) (m.+1) :=
match has_decidable_eq n m with
| inl xeqy := inl (by rewrite xeqy)
| inr xney := inr (λ h : succ n = succ m, by injection h with xeqy; exact absurd xeqy xney)
end
definition not_succ_le_minus_two {n : ℕ₋₂} (H : n .+1 ≤ -2) : empty :=
by cases H
protected definition le_trans {n m k : ℕ₋₂} (H1 : n ≤ m) (H2 : m ≤ k) : n ≤ k :=
begin
induction H2 with k H2 IH,
{ exact H1},
{ exact le.step IH}
end
definition le_of_succ_le_succ {n m : ℕ₋₂} (H : n.+1 ≤ m.+1) : n ≤ m :=
begin
cases H with m H',
{ apply le.tr_refl},
{ exact trunc_index.le_trans (le.step !le.tr_refl) H'}
end
theorem not_succ_le_self {n : ℕ₋₂} : ¬n.+1 ≤ n :=
begin
induction n with n IH: intro H,
{ exact not_succ_le_minus_two H},
{ exact IH (le_of_succ_le_succ H)}
end
protected definition le_antisymm {n m : ℕ₋₂} (H1 : n ≤ m) (H2 : m ≤ n) : n = m :=
begin
induction H2 with n H2 IH,
{ reflexivity},
{ exfalso, apply @not_succ_le_self n, exact trunc_index.le_trans H1 H2}
end
protected definition le_succ {n m : ℕ₋₂} (H1 : n ≤ m) : n ≤ m.+1 :=
le.step H1
protected definition self_le_succ (n : ℕ₋₂) : n ≤ n.+1 :=
le.step (trunc_index.le.tr_refl n)
-- the order is total
protected theorem le_sum_le (n m : ℕ₋₂) : n ≤ m ⊎ m ≤ n :=
begin
induction m with m IH,
{ exact inr !minus_two_le},
{ cases IH with H H,
{ exact inl (trunc_index.le_succ H)},
{ cases H with n' H,
{ exact inl !trunc_index.self_le_succ},
{ exact inr (succ_le_succ H)}}}
end
end trunc_index open trunc_index
definition linear_weak_order_trunc_index [trans_instance] [reducible] :
linear_weak_order trunc_index :=
linear_weak_order.mk le trunc_index.le.tr_refl @trunc_index.le_trans @trunc_index.le_antisymm
trunc_index.le_sum_le
namespace trunc_index
/- more theorems about truncation indices -/
definition zero_add (n : ℕ₋₂) : (0 : ℕ₋₂) + n = n :=
begin
cases n with n, reflexivity,
cases n with n, reflexivity,
induction n with n IH, reflexivity, exact ap succ IH
end
definition add_zero (n : ℕ₋₂) : n + (0 : ℕ₋₂) = n :=
by reflexivity
definition succ_add_nat (n : ℕ₋₂) (m : ℕ) : n.+1 + m = (n + m).+1 :=
by induction m with m IH; reflexivity; exact ap succ IH
definition nat_add_succ (n : ℕ) (m : ℕ₋₂) : n + m.+1 = (n + m).+1 :=
begin
cases m with m, reflexivity,
cases m with m, reflexivity,
induction m with m IH, reflexivity, exact ap succ IH
end
definition add_nat_succ (n : ℕ₋₂) (m : ℕ) : n + (nat.succ m) = (n + m).+1 :=
by reflexivity
definition nat_succ_add (n : ℕ) (m : ℕ₋₂) : (nat.succ n) + m = (n + m).+1 :=
begin
cases m with m, reflexivity,
cases m with m, reflexivity,
induction m with m IH, reflexivity, exact ap succ IH
end
definition sub_two_add_two (n : ℕ₋₂) : sub_two (add_two n) = n :=
begin
induction n with n IH,
{ reflexivity},
{ exact ap succ IH}
end
definition add_two_sub_two (n : ℕ) : add_two (sub_two n) = n :=
begin
induction n with n IH,
{ reflexivity},
{ exact ap nat.succ IH}
end
definition of_nat_add_plus_two_of_nat (n m : ℕ) : n +2+ m = of_nat (n + m + 2) :=
begin
induction m with m IH,
{ reflexivity},
{ exact ap succ IH}
end
definition of_nat_add_of_nat (n m : ℕ) : of_nat n + of_nat m = of_nat (n + m) :=
begin
induction m with m IH,
{ reflexivity},
{ exact ap succ IH}
end
definition succ_add_plus_two (n m : ℕ₋₂) : n.+1 +2+ m = (n +2+ m).+1 :=
begin
induction m with m IH,
{ reflexivity},
{ exact ap succ IH}
end
definition add_plus_two_succ (n m : ℕ₋₂) : n +2+ m.+1 = (n +2+ m).+1 :=
idp
definition add_succ_succ (n m : ℕ₋₂) : n + m.+2 = n +2+ m :=
idp
definition succ_add_succ (n m : ℕ₋₂) : n.+1 + m.+1 = n +2+ m :=
begin
cases m with m IH,
{ reflexivity},
{ apply succ_add_plus_two}
end
definition succ_succ_add (n m : ℕ₋₂) : n.+2 + m = n +2+ m :=
begin
cases m with m IH,
{ reflexivity},
{ exact !succ_add_succ ⬝ !succ_add_plus_two}
end
definition succ_sub_two (n : ℕ) : (nat.succ n).-2 = n.-2 .+1 := rfl
definition sub_two_succ_succ (n : ℕ) : n.-2.+1.+1 = n := rfl
definition succ_sub_two_succ (n : ℕ) : (nat.succ n).-2.+1 = n := rfl
definition of_nat_le_of_nat {n m : ℕ} (H : n ≤ m) : (of_nat n ≤ of_nat m) :=
begin
induction H with m H IH,
{ apply le.refl},
{ exact trunc_index.le_succ IH}
end
definition sub_two_le_sub_two {n m : ℕ} (H : n ≤ m) : n.-2 ≤ m.-2 :=
begin
induction H with m H IH,
{ apply le.refl},
{ exact trunc_index.le_succ IH}
end
definition add_two_le_add_two {n m : ℕ₋₂} (H : n ≤ m) : add_two n ≤ add_two m :=
begin
induction H with m H IH,
{ reflexivity},
{ constructor, exact IH},
end
definition le_of_sub_two_le_sub_two {n m : ℕ} (H : n.-2 ≤ m.-2) : n ≤ m :=
begin
rewrite [-add_two_sub_two n, -add_two_sub_two m],
exact add_two_le_add_two H,
end
definition le_of_of_nat_le_of_nat {n m : ℕ} (H : of_nat n ≤ of_nat m) : n ≤ m :=
begin
apply le_of_sub_two_le_sub_two,
exact le_of_succ_le_succ (le_of_succ_le_succ H)
end
protected theorem succ_le_of_not_le {n m : ℕ₋₂} (H : ¬ n ≤ m) : m.+1 ≤ n :=
begin
cases (le.total n m) with H2 H2,
{ exfalso, exact H H2},
{ cases H2 with n' H2',
{ exfalso, exact H !le.refl},
{ exact succ_le_succ H2'}}
end
end trunc_index open trunc_index
namespace is_trunc
variables {A B : Type} {n : ℕ₋₂}
/- closure properties of truncatedness -/
theorem is_trunc_is_embedding_closed (f : A → B) [Hf : is_embedding f] [HB : is_trunc n B]
(Hn : -1 ≤ n) : is_trunc n A :=
begin
induction n with n,
{exfalso, exact not_succ_le_minus_two Hn},
{apply is_trunc_succ_intro, intro a a',
fapply @is_trunc_is_equiv_closed_rev _ _ n (ap f)}
end
theorem is_trunc_is_retraction_closed (f : A → B) [Hf : is_retraction f]
(n : ℕ₋₂) [HA : is_trunc n A] : is_trunc n B :=
begin
revert A B f Hf HA,
induction n with n IH,
{ intro A B f Hf HA, induction Hf with g ε, fapply is_contr.mk,
{ exact f (center A)},
{ intro b, apply concat,
{ apply (ap f), exact (center_eq (g b))},
{ apply ε}}},
{ intro A B f Hf HA, induction Hf with g ε,
apply is_trunc_succ_intro, intro b b',
fapply (IH (g b = g b')),
{ intro q, exact ((ε b)⁻¹ ⬝ ap f q ⬝ ε b')},
{ apply (is_retraction.mk (ap g)),
{ intro p, induction p, {rewrite [↑ap, con.left_inv]}}},
{ apply is_trunc_eq}}
end
definition is_embedding_to_fun (A B : Type) : is_embedding (@to_fun A B) :=
λf f', !is_equiv_ap_to_fun
/- theorems about trunctype -/
protected definition trunctype.sigma_char.{l} [constructor] (n : ℕ₋₂) :
(trunctype.{l} n) ≃ (Σ (A : Type.{l}), is_trunc n A) :=
begin
fapply equiv.MK,
{ intro A, exact (⟨carrier A, struct A⟩)},
{ intro S, exact (trunctype.mk S.1 S.2)},
{ intro S, induction S with S1 S2, reflexivity},
{ intro A, induction A with A1 A2, reflexivity},
end
definition trunctype_eq_equiv [constructor] (n : ℕ₋₂) (A B : n-Type) :
(A = B) ≃ (carrier A = carrier B) :=
calc
(A = B) ≃ (to_fun (trunctype.sigma_char n) A = to_fun (trunctype.sigma_char n) B)
: eq_equiv_fn_eq_of_equiv
... ≃ ((to_fun (trunctype.sigma_char n) A).1 = (to_fun (trunctype.sigma_char n) B).1)
: equiv.symm (!equiv_subtype)
... ≃ (carrier A = carrier B) : equiv.refl
theorem is_trunc_trunctype [instance] (n : ℕ₋₂) : is_trunc n.+1 (n-Type) :=
begin
apply is_trunc_succ_intro, intro X Y,
fapply is_trunc_equiv_closed_rev, { apply trunctype_eq_equiv},
fapply is_trunc_equiv_closed_rev, { apply eq_equiv_equiv},
induction n,
{ apply @is_contr_of_inhabited_prop,
{ apply is_trunc_is_embedding_closed,
{ apply is_embedding_to_fun} ,
{ reflexivity}},
{ apply equiv_of_is_contr_of_is_contr}},
{ apply is_trunc_is_embedding_closed,
{ apply is_embedding_to_fun},
{ apply minus_one_le_succ}}
end
/- theorems about decidable equality and axiom K -/
theorem is_set_of_axiom_K {A : Type} (K : Π{a : A} (p : a = a), p = idp) : is_set A :=
is_set.mk _ (λa b p q, eq.rec K q p)
theorem is_set_of_relation.{u} {A : Type.{u}} (R : A → A → Type.{u})
(mere : Π(a b : A), is_prop (R a b)) (refl : Π(a : A), R a a)
(imp : Π{a b : A}, R a b → a = b) : is_set A :=
is_set_of_axiom_K
(λa p,
have H2 : transport (λx, R a x → a = x) p (@imp a a) = @imp a a, from !apdt,
have H3 : Π(r : R a a), transport (λx, a = x) p (imp r)
= imp (transport (λx, R a x) p r), from
to_fun (equiv.symm !heq_pi) H2,
have H4 : imp (refl a) ⬝ p = imp (refl a), from
calc
imp (refl a) ⬝ p = transport (λx, a = x) p (imp (refl a)) : transport_eq_r
... = imp (transport (λx, R a x) p (refl a)) : H3
... = imp (refl a) : is_prop.elim,
cancel_left (imp (refl a)) H4)
definition relation_equiv_eq {A : Type} (R : A → A → Type)
(mere : Π(a b : A), is_prop (R a b)) (refl : Π(a : A), R a a)
(imp : Π{a b : A}, R a b → a = b) (a b : A) : R a b ≃ a = b :=
have is_set A, from is_set_of_relation R mere refl @imp,
equiv_of_is_prop imp (λp, p ▸ refl a)
local attribute not [reducible]
theorem is_set_of_double_neg_elim {A : Type} (H : Π(a b : A), ¬¬a = b → a = b)
: is_set A :=
is_set_of_relation (λa b, ¬¬a = b) _ (λa n, n idp) H
section
open decidable
--this is proven differently in init.hedberg
theorem is_set_of_decidable_eq (A : Type) [H : decidable_eq A] : is_set A :=
is_set_of_double_neg_elim (λa b, by_contradiction)
end
theorem is_trunc_of_axiom_K_of_le {A : Type} {n : ℕ₋₂} (H : -1 ≤ n)
(K : Π(a : A), is_trunc n (a = a)) : is_trunc (n.+1) A :=
@is_trunc_succ_intro _ _ (λa b, is_trunc_of_imp_is_trunc_of_le H (λp, eq.rec_on p !K))
theorem is_trunc_succ_of_is_trunc_loop (Hn : -1 ≤ n) (Hp : Π(a : A), is_trunc n (a = a))
: is_trunc (n.+1) A :=
begin
apply is_trunc_succ_intro, intros a a',
apply is_trunc_of_imp_is_trunc_of_le Hn, intro p,
induction p, apply Hp
end
theorem is_prop_iff_is_contr {A : Type} (a : A) :
is_prop A ↔ is_contr A :=
iff.intro (λH, is_contr.mk a (is_prop.elim a)) _
theorem is_trunc_succ_iff_is_trunc_loop (A : Type) (Hn : -1 ≤ n) :
is_trunc (n.+1) A ↔ Π(a : A), is_trunc n (a = a) :=
iff.intro _ (is_trunc_succ_of_is_trunc_loop Hn)
theorem is_trunc_iff_is_contr_loop_succ (n : ℕ) (A : Type)
: is_trunc n A ↔ Π(a : A), is_contr (Ω[succ n](pointed.Mk a)) :=
begin
revert A, induction n with n IH,
{ intro A, esimp [iterated_ploop_space], transitivity _,
{ apply is_trunc_succ_iff_is_trunc_loop, apply le.refl},
{ apply pi_iff_pi, intro a, esimp, apply is_prop_iff_is_contr, reflexivity}},
{ intro A, esimp [iterated_ploop_space],
transitivity _,
{ apply @is_trunc_succ_iff_is_trunc_loop @n, esimp, apply minus_one_le_succ},
apply pi_iff_pi, intro a, transitivity _, apply IH,
transitivity _, apply pi_iff_pi, intro p,
rewrite [iterated_loop_space_loop_irrel n p], apply iff.refl, esimp,
apply imp_iff, reflexivity}
end
theorem is_trunc_iff_is_contr_loop (n : ℕ) (A : Type)
: is_trunc (n.-2.+1) A ↔ (Π(a : A), is_contr (Ω[n](pointed.Mk a))) :=
begin
induction n with n,
{ esimp [sub_two,iterated_ploop_space], apply iff.intro,
intro H a, exact is_contr_of_inhabited_prop a,
intro H, apply is_prop_of_imp_is_contr, exact H},
{ apply is_trunc_iff_is_contr_loop_succ},
end
theorem is_contr_loop_of_is_trunc (n : ℕ) (A : Type*) [H : is_trunc (n.-2.+1) A] :
is_contr (Ω[n] A) :=
begin
induction A,
apply iff.mp !is_trunc_iff_is_contr_loop H
end
theorem is_trunc_loop_of_is_trunc (n : ℕ₋₂) (k : ℕ) (A : Type*) [H : is_trunc n A] :
is_trunc n (Ω[k] A) :=
begin
induction k with k IH,
{ exact H},
{ apply is_trunc_eq}
end
end is_trunc open is_trunc
namespace trunc
universe variable u
variable {A : Type.{u}}
/- characterization of equality in truncated types -/
protected definition code [unfold 3 4] (n : ℕ₋₂) (aa aa' : trunc n.+1 A) : trunctype.{u} n :=
by induction aa with a; induction aa' with a'; exact trunctype.mk' n (trunc n (a = a'))
protected definition encode [unfold 3 5] {n : ℕ₋₂} {aa aa' : trunc n.+1 A}
: aa = aa' → trunc.code n aa aa' :=
begin
intro p, induction p, induction aa with a, esimp, exact (tr idp)
end
protected definition decode {n : ℕ₋₂} (aa aa' : trunc n.+1 A) : trunc.code n aa aa' → aa = aa' :=
begin
induction aa' with a', induction aa with a,
esimp [trunc.code, trunc.rec_on], intro x,
induction x with p, exact ap tr p,
end
definition trunc_eq_equiv [constructor] (n : ℕ₋₂) (aa aa' : trunc n.+1 A)
: aa = aa' ≃ trunc.code n aa aa' :=
begin
fapply equiv.MK,
{ apply trunc.encode},
{ apply trunc.decode},
{ eapply (trunc.rec_on aa'), eapply (trunc.rec_on aa),
intro a a' x, esimp [trunc.code, trunc.rec_on] at x,
refine (@trunc.rec_on n _ _ x _ _),
intro x, apply is_trunc_eq,
intro p, induction p, reflexivity},
{ intro p, induction p, apply (trunc.rec_on aa), intro a, exact idp},
end
definition tr_eq_tr_equiv [constructor] (n : ℕ₋₂) (a a' : A)
: (tr a = tr a' :> trunc n.+1 A) ≃ trunc n (a = a') :=
!trunc_eq_equiv
/- encode preserves concatenation -/
definition trunc_functor2 [unfold 6 7] {n : ℕ₋₂} {A B C : Type} (f : A → B → C)
(x : trunc n A) (y : trunc n B) : trunc n C :=
by induction x with a; induction y with b; exact tr (f a b)
definition trunc_concat [unfold 6 7] {n : ℕ₋₂} {A : Type} {a₁ a₂ a₃ : A}
(p : trunc n (a₁ = a₂)) (q : trunc n (a₂ = a₃)) : trunc n (a₁ = a₃) :=
trunc_functor2 concat p q
definition code_mul {n : ℕ₋₂} {aa₁ aa₂ aa₃ : trunc n.+1 A}
(g : trunc.code n aa₁ aa₂) (h : trunc.code n aa₂ aa₃) : trunc.code n aa₁ aa₃ :=
begin
induction aa₁ with a₁, induction aa₂ with a₂, induction aa₃ with a₃,
esimp at *, apply trunc_concat g h,
end
definition encode_con' {n : ℕ₋₂} {aa₁ aa₂ aa₃ : trunc n.+1 A} (p : aa₁ = aa₂) (q : aa₂ = aa₃)
: trunc.encode (p ⬝ q) = code_mul (trunc.encode p) (trunc.encode q) :=
begin
induction p, induction q, induction aa₁ with a₁, reflexivity
end
definition encode_con {n : ℕ₋₂} {a₁ a₂ a₃ : A} (p : tr a₁ = tr a₂ :> trunc (n.+1) A)
(q : tr a₂ = tr a₃ :> trunc (n.+1) A)
: trunc.encode (p ⬝ q) = trunc_concat (trunc.encode p) (trunc.encode q) :=
encode_con' p q
/- the principle of unique choice -/
definition unique_choice {P : A → Type} [H : Πa, is_prop (P a)] (f : Πa, ∥ P a ∥) (a : A)
: P a :=
!trunc_equiv (f a)
/- transport over a truncated family -/
definition trunc_transport {a a' : A} {P : A → Type} (p : a = a') (n : ℕ₋₂) (x : P a)
: transport (λa, trunc n (P a)) p (tr x) = tr (p ▸ x) :=
by induction p; reflexivity
/- pathover over a truncated family -/
definition trunc_pathover {A : Type} {B : A → Type} {n : ℕ₋₂} {a a' : A} {p : a = a'}
{b : B a} {b' : B a'} (q : b =[p] b') : @tr n _ b =[p] @tr n _ b' :=
by induction q; constructor
/- truncations preserve truncatedness -/
definition is_trunc_trunc_of_is_trunc [instance] [priority 500] (A : Type)
(n m : ℕ₋₂) [H : is_trunc n A] : is_trunc n (trunc m A) :=
begin
revert A m H, eapply (trunc_index.rec_on n),
{ clear n, intro A m H, apply is_contr_equiv_closed,
{ apply equiv.symm, apply trunc_equiv, apply (@is_trunc_of_le _ -2), apply minus_two_le} },
{ clear n, intro n IH A m H, induction m with m,
{ apply (@is_trunc_of_le _ -2), apply minus_two_le},
{ apply is_trunc_succ_intro, intro aa aa',
apply (@trunc.rec_on _ _ _ aa (λy, !is_trunc_succ_of_is_prop)),
eapply (@trunc.rec_on _ _ _ aa' (λy, !is_trunc_succ_of_is_prop)),
intro a a', apply (is_trunc_equiv_closed_rev),
{ apply tr_eq_tr_equiv},
{ exact (IH _ _ _)}}}
end
/- equivalences between truncated types (see also hit.trunc) -/
definition trunc_trunc_equiv_left [constructor] (A : Type) {n m : ℕ₋₂} (H : n ≤ m)
: trunc n (trunc m A) ≃ trunc n A :=
begin
note H2 := is_trunc_of_le (trunc n A) H,
fapply equiv.MK,
{ intro x, induction x with x, induction x with x, exact tr x},
{ intro x, induction x with x, exact tr (tr x)},
{ intro x, induction x with x, reflexivity},
{ intro x, induction x with x, induction x with x, reflexivity}
end
definition trunc_trunc_equiv_right [constructor] (A : Type) {n m : ℕ₋₂} (H : n ≤ m)
: trunc m (trunc n A) ≃ trunc n A :=
begin
apply trunc_equiv,
exact is_trunc_of_le _ H,
end
definition trunc_equiv_trunc_of_le {n m : ℕ₋₂} {A B : Type} (H : n ≤ m)
(f : trunc m A ≃ trunc m B) : trunc n A ≃ trunc n B :=
(trunc_trunc_equiv_left A H)⁻¹ᵉ ⬝e trunc_equiv_trunc n f ⬝e trunc_trunc_equiv_left B H
definition trunc_trunc_equiv_trunc_trunc [constructor] (n m : ℕ₋₂) (A : Type)
: trunc n (trunc m A) ≃ trunc m (trunc n A) :=
begin
fapply equiv.MK: intro x; induction x with x; induction x with x,
{ exact tr (tr x)},
{ exact tr (tr x)},
{ reflexivity},
{ reflexivity}
end
theorem is_trunc_trunc_of_le (A : Type)
(n : ℕ₋₂) {m k : ℕ₋₂} (H : m ≤ k) [is_trunc n (trunc k A)] : is_trunc n (trunc m A) :=
begin
apply is_trunc_equiv_closed,
{ apply trunc_trunc_equiv_left, exact H},
end
definition trunc_functor_homotopy_of_le {n k : ℕ₋₂} {A B : Type} (f : A → B) (H : n ≤ k) :
to_fun (trunc_trunc_equiv_left B H) ∘
trunc_functor n (trunc_functor k f) ∘
to_fun (trunc_trunc_equiv_left A H)⁻¹ᵉ ~
trunc_functor n f :=
begin
intro x, induction x with x, reflexivity
end
definition is_equiv_trunc_functor_of_le {n k : ℕ₋₂} {A B : Type} (f : A → B) (H : n ≤ k)
[is_equiv (trunc_functor k f)] : is_equiv (trunc_functor n f) :=
is_equiv_of_equiv_of_homotopy (trunc_equiv_trunc_of_le H (equiv.mk (trunc_functor k f) _))
(trunc_functor_homotopy_of_le f H)
/- trunc_functor preserves surjectivity -/
definition is_surjective_trunc_functor {A B : Type} (n : ℕ₋₂) (f : A → B) [H : is_surjective f]
: is_surjective (trunc_functor n f) :=
begin
cases n with n: intro b,
{ exact tr (fiber.mk !center !is_prop.elim)},
{ refine @trunc.rec _ _ _ _ _ b, {intro x, exact is_trunc_of_le _ !minus_one_le_succ},
clear b, intro b, induction H b with v, induction v with a p,
exact tr (fiber.mk (tr a) (ap tr p))}
end
/- the image of a map is the (-1)-truncated fiber -/
definition image [constructor] {A B : Type} (f : A → B) (b : B) : Prop := ∥ fiber f b ∥
definition image.mk [constructor] {A B : Type} {f : A → B} {b : B} (a : A) (p : f a = b)
: image f b :=
tr (fiber.mk a p)
/- truncation of pointed types and its functorial action -/
definition ptrunc [constructor] (n : ℕ₋₂) (X : Type*) : n-Type* :=
ptrunctype.mk (trunc n X) _ (tr pt)
definition ptrunc_functor [constructor] {X Y : Type*} (n : ℕ₋₂) (f : X →* Y)
: ptrunc n X →* ptrunc n Y :=
pmap.mk (trunc_functor n f) (ap tr (respect_pt f))
definition ptrunc_pequiv_ptrunc [constructor] (n : ℕ₋₂) {X Y : Type*} (H : X ≃* Y)
: ptrunc n X ≃* ptrunc n Y :=
pequiv_of_equiv (trunc_equiv_trunc n H) (ap tr (respect_pt H))
definition ptrunc_pequiv [constructor] (n : ℕ₋₂) (X : Type*) (H : is_trunc n X)
: ptrunc n X ≃* X :=
pequiv_of_equiv (trunc_equiv n X) idp
definition ptrunc_ptrunc_pequiv_left [constructor] (A : Type*) {n m : ℕ₋₂} (H : n ≤ m)
: ptrunc n (ptrunc m A) ≃* ptrunc n A :=
pequiv_of_equiv (trunc_trunc_equiv_left A H) idp
definition ptrunc_ptrunc_pequiv_right [constructor] (A : Type*) {n m : ℕ₋₂} (H : n ≤ m)
: ptrunc m (ptrunc n A) ≃* ptrunc n A :=
pequiv_of_equiv (trunc_trunc_equiv_right A H) idp
definition ptrunc_pequiv_ptrunc_of_le {n m : ℕ₋₂} {A B : Type*} (H : n ≤ m)
(f : ptrunc m A ≃* ptrunc m B) : ptrunc n A ≃* ptrunc n B :=
(ptrunc_ptrunc_pequiv_left A H)⁻¹ᵉ* ⬝e*
ptrunc_pequiv_ptrunc n f ⬝e*
ptrunc_ptrunc_pequiv_left B H
definition ptrunc_ptrunc_pequiv_ptrunc_ptrunc [constructor] (n m : ℕ₋₂) (A : Type*)
: ptrunc n (ptrunc m A) ≃ ptrunc m (ptrunc n A) :=
pequiv_of_equiv (trunc_trunc_equiv_trunc_trunc n m A) idp
definition loop_ptrunc_pequiv [constructor] (n : ℕ₋₂) (A : Type*) :
Ω (ptrunc (n+1) A) ≃* ptrunc n (Ω A) :=
pequiv_of_equiv !tr_eq_tr_equiv idp
definition loop_ptrunc_pequiv_con {n : ℕ₋₂} {A : Type*} (p q : Ω (ptrunc (n+1) A)) :
loop_ptrunc_pequiv n A (p ⬝ q) =
trunc_concat (loop_ptrunc_pequiv n A p) (loop_ptrunc_pequiv n A q) :=
encode_con p q
definition iterated_loop_ptrunc_pequiv (n : ℕ₋₂) (k : ℕ) (A : Type*) :
Ω[k] (ptrunc (n+k) A) ≃* ptrunc n (Ω[k] A) :=
begin
revert n, induction k with k IH: intro n,
{ reflexivity},
{ refine _ ⬝e* loop_ptrunc_pequiv n (Ω[k] A),
rewrite [iterated_ploop_space_succ], apply loop_pequiv_loop,
refine _ ⬝e* IH (n.+1),
rewrite succ_add_nat}
end
definition ptrunc_functor_pcompose [constructor] {X Y Z : Type*} (n : ℕ₋₂) (g : Y →* Z)
(f : X →* Y) : ptrunc_functor n (g ∘* f) ~* ptrunc_functor n g ∘* ptrunc_functor n f :=
begin
fapply phomotopy.mk,
{ apply trunc_functor_compose},
{ esimp, refine !idp_con ⬝ _, refine whisker_right !ap_compose'⁻¹ᵖ _ ⬝ _,
esimp, refine whisker_right (ap_compose' tr g _) _ ⬝ _, exact !ap_con⁻¹},
end
definition ptrunc_functor_pid [constructor] (X : Type*) (n : ℕ₋₂) :
ptrunc_functor n (pid X) ~* pid (ptrunc n X) :=
begin
fapply phomotopy.mk,
{ apply trunc_functor_id},
{ reflexivity},
end
definition ptrunc_functor_pcast [constructor] {X Y : Type*} (n : ℕ₋₂) (p : X = Y) :
ptrunc_functor n (pcast p) ~* pcast (ap (ptrunc n) p) :=
begin
fapply phomotopy.mk,
{ intro x, esimp, refine !trunc_functor_cast ⬝ _, refine ap010 cast _ x,
refine !ap_compose'⁻¹ ⬝ !ap_compose'},
{ induction p, reflexivity},
end
end trunc open trunc
namespace function
variables {A B : Type}
definition is_surjective_of_is_equiv [instance] (f : A → B) [H : is_equiv f] : is_surjective f :=
λb, begin esimp, apply center end
definition is_equiv_equiv_is_embedding_times_is_surjective [constructor] (f : A → B)
: is_equiv f ≃ (is_embedding f × is_surjective f) :=
equiv_of_is_prop (λH, (_, _))
(λP, prod.rec_on P (λH₁ H₂, !is_equiv_of_is_surjective_of_is_embedding))
end function
|
7bf25f4e7dc00509a6bdf2d5ad4860b81c71db92 | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/measure_theory/integration.lean | c5f079bf4ca64a8ee7cdde954a5cf65558a261cd | [
"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 | 62,683 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Johannes Hölzl
-/
import measure_theory.measure_space
import measure_theory.borel_space
import data.indicator_function
import data.support
/-!
# Lebesgue integral for `ennreal`-valued functions
We define simple functions and show that each Borel measurable function on `ennreal` can be
approximated by a sequence of simple functions.
## Notation
We introduce the following notation for the lower Lebesgue integral of a function `f : α → ennreal`.
* `∫⁻ x, f x ∂μ`: integral of a function `f : α → ennreal` with respect to a measure `μ`;
* `∫⁻ x, f x`: integral of a function `f : α → ennreal` with respect to the canonical measure
`volume` on `α`;
* `∫⁻ x in s, f x ∂μ`: integral of a function `f : α → ennreal` over a set `s` with respect
to a measure `μ`, defined as `∫⁻ x, f x ∂(μ.restrict s)`;
* `∫⁻ x in s, f x`: integral of a function `f : α → ennreal` over a set `s` with respect
to the canonical measure `volume`, defined as `∫⁻ x, f x ∂(volume.restrict s)`.
-/
noncomputable theory
open set (hiding restrict restrict_apply) filter ennreal
open_locale classical topological_space big_operators nnreal
namespace measure_theory
variables {α β γ δ : Type*}
/-- A function `f` from a measurable space to any type is called *simple*,
if every preimage `f ⁻¹' {x}` is measurable, and the range is finite. This structure bundles
a function with these properties. -/
structure {u v} simple_func (α : Type u) [measurable_space α] (β : Type v) :=
(to_fun : α → β)
(is_measurable_fiber' : ∀ x, is_measurable (to_fun ⁻¹' {x}))
(finite_range' : (set.range to_fun).finite)
local infixr ` →ₛ `:25 := simple_func
namespace simple_func
section measurable
variables [measurable_space α]
instance has_coe_to_fun : has_coe_to_fun (α →ₛ β) := ⟨_, to_fun⟩
lemma coe_injective ⦃f g : α →ₛ β⦄ (H : ⇑f = g) : f = g :=
by cases f; cases g; congr; exact H
@[ext] theorem ext {f g : α →ₛ β} (H : ∀ a, f a = g a) : f = g :=
coe_injective $ funext H
lemma finite_range (f : α →ₛ β) : (set.range f).finite := f.finite_range'
lemma is_measurable_fiber (f : α →ₛ β) (x : β) : is_measurable (f ⁻¹' {x}) :=
f.is_measurable_fiber' x
/-- Range of a simple function `α →ₛ β` as a `finset β`. -/
protected def range (f : α →ₛ β) : finset β := f.finite_range.to_finset
@[simp] theorem mem_range {f : α →ₛ β} {b} : b ∈ f.range ↔ b ∈ range f :=
finite.mem_to_finset
theorem mem_range_self (f : α →ₛ β) (x : α) : f x ∈ f.range := mem_range.2 ⟨x, rfl⟩
@[simp] lemma coe_range (f : α →ₛ β) : (↑f.range : set β) = set.range f :=
f.finite_range.coe_to_finset
theorem mem_range_of_measure_ne_zero {f : α →ₛ β} {x : β} {μ : measure α} (H : μ (f ⁻¹' {x}) ≠ 0) :
x ∈ f.range :=
let ⟨a, ha⟩ := nonempty_of_measure_ne_zero H in
mem_range.2 ⟨a, ha⟩
lemma forall_range_iff {f : α →ₛ β} {p : β → Prop} :
(∀ y ∈ f.range, p y) ↔ ∀ x, p (f x) :=
by simp only [mem_range, set.forall_range_iff]
lemma exists_range_iff {f : α →ₛ β} {p : β → Prop} :
(∃ y ∈ f.range, p y) ↔ ∃ x, p (f x) :=
by simpa only [mem_range, exists_prop] using set.exists_range_iff
lemma preimage_eq_empty_iff (f : α →ₛ β) (b : β) : f ⁻¹' {b} = ∅ ↔ b ∉ f.range :=
preimage_singleton_eq_empty.trans $ not_congr mem_range.symm
/-- Constant function as a `simple_func`. -/
def const (α) {β} [measurable_space α] (b : β) : α →ₛ β :=
⟨λ a, b, λ x, is_measurable.const _, finite_range_const⟩
instance [inhabited β] : inhabited (α →ₛ β) := ⟨const _ (default _)⟩
theorem const_apply (a : α) (b : β) : (const α b) a = b := rfl
@[simp] theorem coe_const (b : β) : ⇑(const α b) = function.const α b := rfl
@[simp] lemma range_const (α) [measurable_space α] [nonempty α] (b : β) :
(const α b).range = {b} :=
finset.coe_injective $ by simp
lemma is_measurable_cut (r : α → β → Prop) (f : α →ₛ β)
(h : ∀b, is_measurable {a | r a b}) : is_measurable {a | r a (f a)} :=
begin
have : {a | r a (f a)} = ⋃ b ∈ range f, {a | r a b} ∩ f ⁻¹' {b},
{ ext a,
suffices : r a (f a) ↔ ∃ i, r a (f i) ∧ f a = f i, by simpa,
exact ⟨λ h, ⟨a, ⟨h, rfl⟩⟩, λ ⟨a', ⟨h', e⟩⟩, e.symm ▸ h'⟩ },
rw this,
exact is_measurable.bUnion f.finite_range.countable
(λ b _, is_measurable.inter (h b) (f.is_measurable_fiber _))
end
theorem is_measurable_preimage (f : α →ₛ β) (s) : is_measurable (f ⁻¹' s) :=
is_measurable_cut (λ _ b, b ∈ s) f (λ b, is_measurable.const (b ∈ s))
/-- A simple function is measurable -/
protected theorem measurable [measurable_space β] (f : α →ₛ β) : measurable f :=
λ s _, is_measurable_preimage f s
/-- If-then-else as a `simple_func`. -/
def piecewise (s : set α) (hs : is_measurable s) (f g : α →ₛ β) : α →ₛ β :=
⟨s.piecewise f g,
λ x, by letI : measurable_space β := ⊤; exact
f.measurable.piecewise hs g.measurable trivial,
(f.finite_range.union g.finite_range).subset range_ite_subset⟩
@[simp] theorem coe_piecewise {s : set α} (hs : is_measurable s) (f g : α →ₛ β) :
⇑(piecewise s hs f g) = s.piecewise f g :=
rfl
theorem piecewise_apply {s : set α} (hs : is_measurable s) (f g : α →ₛ β) (a) :
piecewise s hs f g a = if a ∈ s then f a else g a :=
rfl
@[simp] lemma piecewise_compl {s : set α} (hs : is_measurable sᶜ) (f g : α →ₛ β) :
piecewise sᶜ hs f g = piecewise s hs.of_compl g f :=
coe_injective $ by simp [hs]
@[simp] lemma piecewise_univ (f g : α →ₛ β) : piecewise univ is_measurable.univ f g = f :=
coe_injective $ by simp
@[simp] lemma piecewise_empty (f g : α →ₛ β) : piecewise ∅ is_measurable.empty f g = g :=
coe_injective $ by simp
/-- If `f : α →ₛ β` is a simple function and `g : β → α →ₛ γ` is a family of simple functions,
then `f.bind g` binds the first argument of `g` to `f`. In other words, `f.bind g a = g (f a) a`. -/
def bind (f : α →ₛ β) (g : β → α →ₛ γ) : α →ₛ γ :=
⟨λa, g (f a) a,
λ c, is_measurable_cut (λa b, g b a ∈ ({c} : set γ)) f (λ b, (g b).is_measurable_fiber c),
(f.finite_range.bUnion (λ b _, (g b).finite_range)).subset $
by rintro _ ⟨a, rfl⟩; simp; exact ⟨a, a, rfl⟩⟩
@[simp] theorem bind_apply (f : α →ₛ β) (g : β → α →ₛ γ) (a) :
f.bind g a = g (f a) a := rfl
/-- Given a function `g : β → γ` and a simple function `f : α →ₛ β`, `f.map g` return the simple
function `g ∘ f : α →ₛ γ` -/
def map (g : β → γ) (f : α →ₛ β) : α →ₛ γ := bind f (const α ∘ g)
theorem map_apply (g : β → γ) (f : α →ₛ β) (a) : f.map g a = g (f a) := rfl
theorem map_map (g : β → γ) (h: γ → δ) (f : α →ₛ β) : (f.map g).map h = f.map (h ∘ g) := rfl
@[simp] theorem coe_map (g : β → γ) (f : α →ₛ β) : (f.map g : α → γ) = g ∘ f := rfl
@[simp] theorem range_map [decidable_eq γ] (g : β → γ) (f : α →ₛ β) :
(f.map g).range = f.range.image g :=
finset.coe_injective $ by simp [range_comp]
@[simp] theorem map_const (g : β → γ) (b : β) : (const α b).map g = const α (g b) := rfl
lemma map_preimage (f : α →ₛ β) (g : β → γ) (s : set γ) :
(f.map g) ⁻¹' s = f ⁻¹' ↑(f.range.filter (λb, g b ∈ s)) :=
by { simp only [coe_range, sep_mem_eq, set.mem_range, function.comp_app, coe_map, finset.coe_filter,
← mem_preimage, inter_comm, preimage_inter_range], apply preimage_comp }
lemma map_preimage_singleton (f : α →ₛ β) (g : β → γ) (c : γ) :
(f.map g) ⁻¹' {c} = f ⁻¹' ↑(f.range.filter (λ b, g b = c)) :=
map_preimage _ _ _
/-- If `f` is a simple function taking values in `β → γ` and `g` is another simple function
with the same domain and codomain `β`, then `f.seq g = f a (g a)`. -/
def seq (f : α →ₛ (β → γ)) (g : α →ₛ β) : α →ₛ γ := f.bind (λf, g.map f)
@[simp] lemma seq_apply (f : α →ₛ (β → γ)) (g : α →ₛ β) (a : α) : f.seq g a = f a (g a) := rfl
/-- Combine two simple functions `f : α →ₛ β` and `g : α →ₛ β`
into `λ a, (f a, g a)`. -/
def pair (f : α →ₛ β) (g : α →ₛ γ) : α →ₛ (β × γ) := (f.map prod.mk).seq g
@[simp] lemma pair_apply (f : α →ₛ β) (g : α →ₛ γ) (a) : pair f g a = (f a, g a) := rfl
lemma pair_preimage (f : α →ₛ β) (g : α →ₛ γ) (s : set β) (t : set γ) :
(pair f g) ⁻¹' (set.prod s t) = (f ⁻¹' s) ∩ (g ⁻¹' t) := rfl
/- A special form of `pair_preimage` -/
lemma pair_preimage_singleton (f : α →ₛ β) (g : α →ₛ γ) (b : β) (c : γ) :
(pair f g) ⁻¹' {(b, c)} = (f ⁻¹' {b}) ∩ (g ⁻¹' {c}) :=
by { rw ← prod_singleton_singleton, exact pair_preimage _ _ _ _ }
theorem bind_const (f : α →ₛ β) : f.bind (const α) = f := by ext; simp
instance [has_zero β] : has_zero (α →ₛ β) := ⟨const α 0⟩
instance [has_add β] : has_add (α →ₛ β) := ⟨λf g, (f.map (+)).seq g⟩
instance [has_mul β] : has_mul (α →ₛ β) := ⟨λf g, (f.map (*)).seq g⟩
instance [has_sup β] : has_sup (α →ₛ β) := ⟨λf g, (f.map (⊔)).seq g⟩
instance [has_inf β] : has_inf (α →ₛ β) := ⟨λf g, (f.map (⊓)).seq g⟩
instance [has_le β] : has_le (α →ₛ β) := ⟨λf g, ∀a, f a ≤ g a⟩
@[simp, norm_cast] lemma coe_zero [has_zero β] : ⇑(0 : α →ₛ β) = 0 := rfl
@[simp] lemma const_zero [has_zero β] : const α (0:β) = 0 := rfl
@[simp, norm_cast] lemma coe_add [has_add β] (f g : α →ₛ β) : ⇑(f + g) = f + g := rfl
@[simp, norm_cast] lemma coe_mul [has_mul β] (f g : α →ₛ β) : ⇑(f * g) = f * g := rfl
@[simp, norm_cast] lemma coe_le [preorder β] {f g : α →ₛ β} : (f : α → β) ≤ g ↔ f ≤ g := iff.rfl
@[simp] lemma range_zero [nonempty α] [has_zero β] : (0 : α →ₛ β).range = {0} :=
finset.ext $ λ x, by simp [eq_comm]
lemma eq_zero_of_mem_range_zero [has_zero β] : ∀ {y : β}, y ∈ (0 : α →ₛ β).range → y = 0 :=
forall_range_iff.2 $ λ x, rfl
lemma sup_apply [has_sup β] (f g : α →ₛ β) (a : α) : (f ⊔ g) a = f a ⊔ g a := rfl
lemma mul_apply [has_mul β] (f g : α →ₛ β) (a : α) : (f * g) a = f a * g a := rfl
lemma add_apply [has_add β] (f g : α →ₛ β) (a : α) : (f + g) a = f a + g a := rfl
lemma add_eq_map₂ [has_add β] (f g : α →ₛ β) : f + g = (pair f g).map (λp:β×β, p.1 + p.2) :=
rfl
lemma mul_eq_map₂ [has_mul β] (f g : α →ₛ β) : f * g = (pair f g).map (λp:β×β, p.1 * p.2) :=
rfl
lemma sup_eq_map₂ [has_sup β] (f g : α →ₛ β) : f ⊔ g = (pair f g).map (λp:β×β, p.1 ⊔ p.2) :=
rfl
lemma const_mul_eq_map [has_mul β] (f : α →ₛ β) (b : β) : const α b * f = f.map (λa, b * a) := rfl
instance [add_monoid β] : add_monoid (α →ₛ β) :=
function.injective.add_monoid (λ f, show α → β, from f) coe_injective coe_zero coe_add
instance add_comm_monoid [add_comm_monoid β] : add_comm_monoid (α →ₛ β) :=
function.injective.add_comm_monoid (λ f, show α → β, from f) coe_injective coe_zero coe_add
instance [has_neg β] : has_neg (α →ₛ β) := ⟨λf, f.map (has_neg.neg)⟩
@[simp, norm_cast] lemma coe_neg [has_neg β] (f : α →ₛ β) : ⇑(-f) = -f := rfl
instance [add_group β] : add_group (α →ₛ β) :=
function.injective.add_group (λ f, show α → β, from f) coe_injective coe_zero coe_add coe_neg
@[simp, norm_cast] lemma coe_sub [add_group β] (f g : α →ₛ β) : ⇑(f - g) = f - g := rfl
instance [add_comm_group β] : add_comm_group (α →ₛ β) :=
function.injective.add_comm_group (λ f, show α → β, from f) coe_injective
coe_zero coe_add coe_neg
variables {K : Type*}
instance [has_scalar K β] : has_scalar K (α →ₛ β) := ⟨λk f, f.map ((•) k)⟩
@[simp] lemma coe_smul [has_scalar K β] (c : K) (f : α →ₛ β) : ⇑(c • f) = c • f := rfl
lemma smul_apply [has_scalar K β] (k : K) (f : α →ₛ β) (a : α) : (k • f) a = k • f a := rfl
instance [semiring K] [add_comm_monoid β] [semimodule K β] : semimodule K (α →ₛ β) :=
function.injective.semimodule K ⟨λ f, show α → β, from f, coe_zero, coe_add⟩
coe_injective coe_smul
lemma smul_eq_map [has_scalar K β] (k : K) (f : α →ₛ β) : k • f = f.map ((•) k) := rfl
instance [preorder β] : preorder (α →ₛ β) :=
{ le_refl := λf a, le_refl _,
le_trans := λf g h hfg hgh a, le_trans (hfg _) (hgh a),
.. simple_func.has_le }
instance [partial_order β] : partial_order (α →ₛ β) :=
{ le_antisymm := assume f g hfg hgf, ext $ assume a, le_antisymm (hfg a) (hgf a),
.. simple_func.preorder }
instance [order_bot β] : order_bot (α →ₛ β) :=
{ bot := const α ⊥, bot_le := λf a, bot_le, .. simple_func.partial_order }
instance [order_top β] : order_top (α →ₛ β) :=
{ top := const α ⊤, le_top := λf a, le_top, .. simple_func.partial_order }
instance [semilattice_inf β] : semilattice_inf (α →ₛ β) :=
{ inf := (⊓),
inf_le_left := assume f g a, inf_le_left,
inf_le_right := assume f g a, inf_le_right,
le_inf := assume f g h hfh hgh a, le_inf (hfh a) (hgh a),
.. simple_func.partial_order }
instance [semilattice_sup β] : semilattice_sup (α →ₛ β) :=
{ sup := (⊔),
le_sup_left := assume f g a, le_sup_left,
le_sup_right := assume f g a, le_sup_right,
sup_le := assume f g h hfh hgh a, sup_le (hfh a) (hgh a),
.. simple_func.partial_order }
instance [semilattice_sup_bot β] : semilattice_sup_bot (α →ₛ β) :=
{ .. simple_func.semilattice_sup,.. simple_func.order_bot }
instance [lattice β] : lattice (α →ₛ β) :=
{ .. simple_func.semilattice_sup,.. simple_func.semilattice_inf }
instance [bounded_lattice β] : bounded_lattice (α →ₛ β) :=
{ .. simple_func.lattice, .. simple_func.order_bot, .. simple_func.order_top }
lemma finset_sup_apply [semilattice_sup_bot β] {f : γ → α →ₛ β} (s : finset γ) (a : α) :
s.sup f a = s.sup (λc, f c a) :=
begin
refine finset.induction_on s rfl _,
assume a s hs ih,
rw [finset.sup_insert, finset.sup_insert, sup_apply, ih]
end
section restrict
variables [has_zero β]
/-- Restrict a simple function `f : α →ₛ β` to a set `s`. If `s` is measurable,
then `f.restrict s a = if a ∈ s then f a else 0`, otherwise `f.restrict s = const α 0`. -/
def restrict (f : α →ₛ β) (s : set α) : α →ₛ β :=
if hs : is_measurable s then piecewise s hs f 0 else 0
theorem restrict_of_not_measurable {f : α →ₛ β} {s : set α}
(hs : ¬is_measurable s) :
restrict f s = 0 :=
dif_neg hs
@[simp] theorem coe_restrict (f : α →ₛ β) {s : set α} (hs : is_measurable s) :
⇑(restrict f s) = indicator s f :=
by { rw [restrict, dif_pos hs], refl }
@[simp] theorem restrict_univ (f : α →ₛ β) : restrict f univ = f :=
by simp [restrict]
@[simp] theorem restrict_empty (f : α →ₛ β) : restrict f ∅ = 0 :=
by simp [restrict]
theorem map_restrict_of_zero [has_zero γ] {g : β → γ} (hg : g 0 = 0) (f : α →ₛ β) (s : set α) :
(f.restrict s).map g = (f.map g).restrict s :=
ext $ λ x,
if hs : is_measurable s then by simp [hs, set.indicator_comp_of_zero hg]
else by simp [restrict_of_not_measurable hs, hg]
theorem map_coe_ennreal_restrict (f : α →ₛ nnreal) (s : set α) :
(f.restrict s).map (coe : nnreal → ennreal) = (f.map coe).restrict s :=
map_restrict_of_zero ennreal.coe_zero _ _
theorem map_coe_nnreal_restrict (f : α →ₛ nnreal) (s : set α) :
(f.restrict s).map (coe : nnreal → ℝ) = (f.map coe).restrict s :=
map_restrict_of_zero nnreal.coe_zero _ _
theorem restrict_apply (f : α →ₛ β) {s : set α} (hs : is_measurable s) (a) :
restrict f s a = if a ∈ s then f a else 0 :=
by simp only [hs, coe_restrict]
theorem restrict_preimage (f : α →ₛ β) {s : set α} (hs : is_measurable s)
{t : set β} (ht : (0:β) ∉ t) : restrict f s ⁻¹' t = s ∩ f ⁻¹' t :=
by simp [hs, indicator_preimage_of_not_mem _ _ ht]
theorem restrict_preimage_singleton (f : α →ₛ β) {s : set α} (hs : is_measurable s)
{r : β} (hr : r ≠ 0) : restrict f s ⁻¹' {r} = s ∩ f ⁻¹' {r} :=
f.restrict_preimage hs hr.symm
lemma mem_restrict_range {r : β} {s : set α} {f : α →ₛ β} (hs : is_measurable s) :
r ∈ (restrict f s).range ↔ (r = 0 ∧ s ≠ univ) ∨ (r ∈ f '' s) :=
by rw [← finset.mem_coe, coe_range, coe_restrict _ hs, mem_range_indicator]
lemma mem_image_of_mem_range_restrict {r : β} {s : set α} {f : α →ₛ β}
(hr : r ∈ (restrict f s).range) (h0 : r ≠ 0) :
r ∈ f '' s :=
if hs : is_measurable s then by simpa [mem_restrict_range hs, h0] using hr
else by { rw [restrict_of_not_measurable hs] at hr,
exact (h0 $ eq_zero_of_mem_range_zero hr).elim }
@[mono] lemma restrict_mono [preorder β] (s : set α) {f g : α →ₛ β} (H : f ≤ g) :
f.restrict s ≤ g.restrict s :=
if hs : is_measurable s then λ x, by simp only [coe_restrict _ hs, indicator_le_indicator (H x)]
else by simp only [restrict_of_not_measurable hs, le_refl]
end restrict
section approx
section
variables [semilattice_sup_bot β] [has_zero β]
/-- Fix a sequence `i : ℕ → β`. Given a function `α → β`, its `n`-th approximation
by simple functions is defined so that in case `β = ennreal` it sends each `a` to the supremum
of the set `{i k | k ≤ n ∧ i k ≤ f a}`, see `approx_apply` and `supr_approx_apply` for details. -/
def approx (i : ℕ → β) (f : α → β) (n : ℕ) : α →ₛ β :=
(finset.range n).sup (λk, restrict (const α (i k)) {a:α | i k ≤ f a})
lemma approx_apply [topological_space β] [order_closed_topology β] [measurable_space β]
[opens_measurable_space β] {i : ℕ → β} {f : α → β} {n : ℕ} (a : α) (hf : measurable f) :
(approx i f n : α →ₛ β) a = (finset.range n).sup (λk, if i k ≤ f a then i k else 0) :=
begin
dsimp only [approx],
rw [finset_sup_apply],
congr,
funext k,
rw [restrict_apply],
refl,
exact (hf is_measurable_Ici)
end
lemma monotone_approx (i : ℕ → β) (f : α → β) : monotone (approx i f) :=
assume n m h, finset.sup_mono $ finset.range_subset.2 h
lemma approx_comp [topological_space β] [order_closed_topology β] [measurable_space β]
[opens_measurable_space β] [measurable_space γ]
{i : ℕ → β} {f : γ → β} {g : α → γ} {n : ℕ} (a : α)
(hf : measurable f) (hg : measurable g) :
(approx i (f ∘ g) n : α →ₛ β) a = (approx i f n : γ →ₛ β) (g a) :=
by rw [approx_apply _ hf, approx_apply _ (hf.comp hg)]
end
lemma supr_approx_apply [topological_space β] [complete_lattice β] [order_closed_topology β] [has_zero β]
[measurable_space β] [opens_measurable_space β]
(i : ℕ → β) (f : α → β) (a : α) (hf : measurable f) (h_zero : (0 : β) = ⊥) :
(⨆n, (approx i f n : α →ₛ β) a) = (⨆k (h : i k ≤ f a), i k) :=
begin
refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume k, supr_le $ assume hk, _),
{ rw [approx_apply a hf, h_zero],
refine finset.sup_le (assume k hk, _),
split_ifs,
exact le_supr_of_le k (le_supr _ h),
exact bot_le },
{ refine le_supr_of_le (k+1) _,
rw [approx_apply a hf],
have : k ∈ finset.range (k+1) := finset.mem_range.2 (nat.lt_succ_self _),
refine le_trans (le_of_eq _) (finset.le_sup this),
rw [if_pos hk] }
end
end approx
section eapprox
/-- A sequence of `ennreal`s such that its range is the set of non-negative rational numbers. -/
def ennreal_rat_embed (n : ℕ) : ennreal :=
ennreal.of_real ((encodable.decode ℚ n).get_or_else (0 : ℚ))
lemma ennreal_rat_embed_encode (q : ℚ) :
ennreal_rat_embed (encodable.encode q) = nnreal.of_real q :=
by rw [ennreal_rat_embed, encodable.encodek]; refl
/-- Approximate a function `α → ennreal` by a sequence of simple functions. -/
def eapprox : (α → ennreal) → ℕ → α →ₛ ennreal :=
approx ennreal_rat_embed
lemma monotone_eapprox (f : α → ennreal) : monotone (eapprox f) :=
monotone_approx _ f
lemma supr_eapprox_apply (f : α → ennreal) (hf : measurable f) (a : α) :
(⨆n, (eapprox f n : α →ₛ ennreal) a) = f a :=
begin
rw [eapprox, supr_approx_apply ennreal_rat_embed f a hf rfl],
refine le_antisymm (supr_le $ assume i, supr_le $ assume hi, hi) (le_of_not_gt _),
assume h,
rcases ennreal.lt_iff_exists_rat_btwn.1 h with ⟨q, hq, lt_q, q_lt⟩,
have : (nnreal.of_real q : ennreal) ≤
(⨆ (k : ℕ) (h : ennreal_rat_embed k ≤ f a), ennreal_rat_embed k),
{ refine le_supr_of_le (encodable.encode q) _,
rw [ennreal_rat_embed_encode q],
refine le_supr_of_le (le_of_lt q_lt) _,
exact le_refl _ },
exact lt_irrefl _ (lt_of_le_of_lt this lt_q)
end
lemma eapprox_comp [measurable_space γ] {f : γ → ennreal} {g : α → γ} {n : ℕ}
(hf : measurable f) (hg : measurable g) :
(eapprox (f ∘ g) n : α → ennreal) = (eapprox f n : γ →ₛ ennreal) ∘ g :=
funext $ assume a, approx_comp a hf hg
end eapprox
end measurable
section measure
variables [measurable_space α] {μ : measure α}
/-- Integral of a simple function whose codomain is `ennreal`. -/
def lintegral (f : α →ₛ ennreal) (μ : measure α) : ennreal :=
∑ x in f.range, x * μ (f ⁻¹' {x})
lemma lintegral_eq_of_subset (f : α →ₛ ennreal) {s : finset ennreal}
(hs : ∀ x, f x ≠ 0 → μ (f ⁻¹' {f x}) ≠ 0 → f x ∈ s) :
f.lintegral μ = ∑ x in s, x * μ (f ⁻¹' {x}) :=
begin
refine finset.sum_bij_ne_zero (λr _ _, r) _ _ _ _,
{ simpa only [forall_range_iff, mul_ne_zero_iff, and_imp] },
{ intros, assumption },
{ intros b _ hb,
refine ⟨b, _, hb, rfl⟩,
rw [mem_range, ← preimage_singleton_nonempty],
exact nonempty_of_measure_ne_zero (mul_ne_zero_iff.1 hb).2 },
{ intros, refl }
end
/-- Calculate the integral of `(g ∘ f)`, where `g : β → ennreal` and `f : α →ₛ β`. -/
lemma map_lintegral (g : β → ennreal) (f : α →ₛ β) :
(f.map g).lintegral μ = ∑ x in f.range, g x * μ (f ⁻¹' {x}) :=
begin
simp only [lintegral, range_map],
refine finset.sum_image' _ (assume b hb, _),
rcases mem_range.1 hb with ⟨a, rfl⟩,
rw [map_preimage_singleton, ← sum_measure_preimage_singleton _
(λ _ _, f.is_measurable_preimage _), finset.mul_sum],
refine finset.sum_congr _ _,
{ congr },
{ assume x, simp only [finset.mem_filter], rintro ⟨_, h⟩, rw h },
end
lemma add_lintegral (f g : α →ₛ ennreal) : (f + g).lintegral μ = f.lintegral μ + g.lintegral μ :=
calc (f + g).lintegral μ =
∑ x in (pair f g).range, (x.1 * μ (pair f g ⁻¹' {x}) + x.2 * μ (pair f g ⁻¹' {x})) :
by rw [add_eq_map₂, map_lintegral]; exact finset.sum_congr rfl (assume a ha, add_mul _ _ _)
... = ∑ x in (pair f g).range, x.1 * μ (pair f g ⁻¹' {x}) +
∑ x in (pair f g).range, x.2 * μ (pair f g ⁻¹' {x}) : by rw [finset.sum_add_distrib]
... = ((pair f g).map prod.fst).lintegral μ + ((pair f g).map prod.snd).lintegral μ :
by rw [map_lintegral, map_lintegral]
... = lintegral f μ + lintegral g μ : rfl
lemma const_mul_lintegral (f : α →ₛ ennreal) (x : ennreal) :
(const α x * f).lintegral μ = x * f.lintegral μ :=
calc (f.map (λa, x * a)).lintegral μ = ∑ r in f.range, x * r * μ (f ⁻¹' {r}) :
map_lintegral _ _
... = ∑ r in f.range, x * (r * μ (f ⁻¹' {r})) :
finset.sum_congr rfl (assume a ha, mul_assoc _ _ _)
... = x * f.lintegral μ :
finset.mul_sum.symm
/-- Integral of a simple function `α →ₛ ennreal` as a bilinear map. -/
def lintegralₗ : (α →ₛ ennreal) →ₗ[ennreal] measure α →ₗ[ennreal] ennreal :=
{ to_fun := λ f,
{ to_fun := lintegral f,
map_add' := by simp [lintegral, mul_add, finset.sum_add_distrib],
map_smul' := λ c μ, by simp [lintegral, mul_left_comm _ c, finset.mul_sum] },
map_add' := λ f g, linear_map.ext (λ μ, add_lintegral f g),
map_smul' := λ c f, linear_map.ext (λ μ, const_mul_lintegral f c) }
@[simp] lemma zero_lintegral : (0 : α →ₛ ennreal).lintegral μ = 0 :=
linear_map.ext_iff.1 lintegralₗ.map_zero μ
lemma lintegral_add {ν} (f : α →ₛ ennreal) : f.lintegral (μ + ν) = f.lintegral μ + f.lintegral ν :=
(lintegralₗ f).map_add μ ν
lemma lintegral_smul (f : α →ₛ ennreal) (c : ennreal) :
f.lintegral (c • μ) = c • f.lintegral μ :=
(lintegralₗ f).map_smul c μ
@[simp] lemma lintegral_zero (f : α →ₛ ennreal) :
f.lintegral 0 = 0 :=
(lintegralₗ f).map_zero
lemma lintegral_sum {ι} (f : α →ₛ ennreal) (μ : ι → measure α) :
f.lintegral (measure.sum μ) = ∑' i, f.lintegral (μ i) :=
begin
simp only [lintegral, measure.sum_apply, f.is_measurable_preimage, ← finset.tsum_subtype,
← ennreal.tsum_mul_left],
apply ennreal.tsum_comm
end
lemma restrict_lintegral (f : α →ₛ ennreal) {s : set α} (hs : is_measurable s) :
(restrict f s).lintegral μ = ∑ r in f.range, r * μ (f ⁻¹' {r} ∩ s) :=
calc (restrict f s).lintegral μ = ∑ r in f.range, r * μ (restrict f s ⁻¹' {r}) :
lintegral_eq_of_subset _ $ λ x hx, if hxs : x ∈ s
then by simp [f.restrict_apply hs, if_pos hxs, mem_range_self]
else false.elim $ hx $ by simp [*]
... = ∑ r in f.range, r * μ (f ⁻¹' {r} ∩ s) :
finset.sum_congr rfl $ forall_range_iff.2 $ λ b, if hb : f b = 0 then by simp only [hb, zero_mul]
else by rw [restrict_preimage_singleton _ hs hb, inter_comm]
lemma lintegral_restrict (f : α →ₛ ennreal) (s : set α) (μ : measure α) :
f.lintegral (μ.restrict s) = ∑ y in f.range, y * μ (f ⁻¹' {y} ∩ s) :=
by simp only [lintegral, measure.restrict_apply, f.is_measurable_preimage]
lemma restrict_lintegral_eq_lintegral_restrict (f : α →ₛ ennreal) {s : set α}
(hs : is_measurable s) :
(restrict f s).lintegral μ = f.lintegral (μ.restrict s) :=
by rw [f.restrict_lintegral hs, lintegral_restrict]
lemma const_lintegral (c : ennreal) : (const α c).lintegral μ = c * μ univ :=
begin
rw [lintegral],
by_cases ha : nonempty α,
{ resetI, simp [preimage_const_of_mem] },
{ simp [μ.eq_zero_of_not_nonempty ha] }
end
lemma const_lintegral_restrict (c : ennreal) (s : set α) :
(const α c).lintegral (μ.restrict s) = c * μ s :=
by rw [const_lintegral, measure.restrict_apply is_measurable.univ, univ_inter]
lemma restrict_const_lintegral (c : ennreal) {s : set α} (hs : is_measurable s) :
((const α c).restrict s).lintegral μ = c * μ s :=
by rw [restrict_lintegral_eq_lintegral_restrict _ hs, const_lintegral_restrict]
lemma le_sup_lintegral (f g : α →ₛ ennreal) : f.lintegral μ ⊔ g.lintegral μ ≤ (f ⊔ g).lintegral μ :=
calc f.lintegral μ ⊔ g.lintegral μ =
((pair f g).map prod.fst).lintegral μ ⊔ ((pair f g).map prod.snd).lintegral μ : rfl
... ≤ ∑ x in (pair f g).range, (x.1 ⊔ x.2) * μ (pair f g ⁻¹' {x}) :
begin
rw [map_lintegral, map_lintegral],
refine sup_le _ _;
refine finset.sum_le_sum (λ a _, canonically_ordered_semiring.mul_le_mul _ (le_refl _)),
exact le_sup_left,
exact le_sup_right
end
... = (f ⊔ g).lintegral μ : by rw [sup_eq_map₂, map_lintegral]
/-- `simple_func.lintegral` is monotone both in function and in measure. -/
@[mono] lemma lintegral_mono {f g : α →ₛ ennreal} (hfg : f ≤ g) {μ ν : measure α} (hμν : μ ≤ ν) :
f.lintegral μ ≤ g.lintegral ν :=
calc f.lintegral μ ≤ f.lintegral μ ⊔ g.lintegral μ : le_sup_left
... ≤ (f ⊔ g).lintegral μ : le_sup_lintegral _ _
... = g.lintegral μ : by rw [sup_of_le_right hfg]
... ≤ g.lintegral ν : finset.sum_le_sum $ λ y hy, ennreal.mul_left_mono $
hμν _ (g.is_measurable_preimage _)
/-- `simple_func.lintegral` depends only on the measures of `f ⁻¹' {y}`. -/
lemma lintegral_eq_of_measure_preimage [measurable_space β] {f : α →ₛ ennreal} {g : β →ₛ ennreal}
{ν : measure β} (H : ∀ y, μ (f ⁻¹' {y}) = ν (g ⁻¹' {y})) :
f.lintegral μ = g.lintegral ν :=
begin
simp only [lintegral, ← H],
apply lintegral_eq_of_subset,
simp only [H],
intros,
exact mem_range_of_measure_ne_zero ‹_›
end
/-- If two simple functions are equal a.e., then their `lintegral`s are equal. -/
lemma lintegral_congr {f g : α →ₛ ennreal} (h : ∀ᵐ a ∂ μ, f a = g a) :
f.lintegral μ = g.lintegral μ :=
lintegral_eq_of_measure_preimage $ λ y, measure_congr $ h.mono $ λ x hx, by simp [hx]
lemma lintegral_map {β} [measurable_space β] {μ' : measure β} (f : α →ₛ ennreal) (g : β →ₛ ennreal)
(m : α → β) (eq : ∀a:α, f a = g (m a)) (h : ∀s:set β, is_measurable s → μ' s = μ (m ⁻¹' s)) :
f.lintegral μ = g.lintegral μ' :=
lintegral_eq_of_measure_preimage $ λ y,
by { simp only [preimage, eq], exact (h (g ⁻¹' {y}) (g.is_measurable_preimage _)).symm }
end measure
section fin_meas_supp
variables [measurable_space α] [has_zero β] [has_zero γ] {μ : measure α}
open finset ennreal function
lemma support_eq (f : α →ₛ β) : support f = ⋃ y ∈ f.range.filter (λ y, y ≠ 0), f ⁻¹' {y} :=
set.ext $ λ x, by simp only [finset.bUnion_preimage_singleton, mem_support, set.mem_preimage,
finset.mem_coe, mem_filter, mem_range_self, true_and]
/-- A `simple_func` has finite measure support if it is equal to `0` outside of a set of finite
measure. -/
protected def fin_meas_supp (f : α →ₛ β) (μ : measure α) : Prop :=
f =ᶠ[μ.cofinite] 0
lemma fin_meas_supp_iff_support {f : α →ₛ β} {μ : measure α} :
f.fin_meas_supp μ ↔ μ (support f) < ⊤ :=
iff.rfl
lemma fin_meas_supp_iff {f : α →ₛ β} {μ : measure α} :
f.fin_meas_supp μ ↔ ∀ y ≠ 0, μ (f ⁻¹' {y}) < ⊤ :=
begin
split,
{ refine λ h y hy, lt_of_le_of_lt (measure_mono _) h,
exact λ x hx (H : f x = 0), hy $ H ▸ eq.symm hx },
{ intro H,
rw [fin_meas_supp_iff_support, support_eq],
refine lt_of_le_of_lt (measure_bUnion_finset_le _ _) (sum_lt_top _),
exact λ y hy, H y (finset.mem_filter.1 hy).2 }
end
namespace fin_meas_supp
lemma meas_preimage_singleton_ne_zero {f : α →ₛ β} (h : f.fin_meas_supp μ) {y : β} (hy : y ≠ 0) :
μ (f ⁻¹' {y}) < ⊤ :=
fin_meas_supp_iff.1 h y hy
protected lemma map {f : α →ₛ β} {g : β → γ} (hf : f.fin_meas_supp μ) (hg : g 0 = 0) :
(f.map g).fin_meas_supp μ :=
flip lt_of_le_of_lt hf (measure_mono $ support_comp_subset hg f)
lemma of_map {f : α →ₛ β} {g : β → γ} (h : (f.map g).fin_meas_supp μ) (hg : ∀b, g b = 0 → b = 0) :
f.fin_meas_supp μ :=
flip lt_of_le_of_lt h $ measure_mono $ support_subset_comp hg _
lemma map_iff {f : α →ₛ β} {g : β → γ} (hg : ∀ {b}, g b = 0 ↔ b = 0) :
(f.map g).fin_meas_supp μ ↔ f.fin_meas_supp μ :=
⟨λ h, h.of_map $ λ b, hg.1, λ h, h.map $ hg.2 rfl⟩
protected lemma pair {f : α →ₛ β} {g : α →ₛ γ} (hf : f.fin_meas_supp μ) (hg : g.fin_meas_supp μ) :
(pair f g).fin_meas_supp μ :=
calc μ (support $ pair f g) = μ (support f ∪ support g) : congr_arg μ $ support_prod_mk f g
... ≤ μ (support f) + μ (support g) : measure_union_le _ _
... < _ : add_lt_top.2 ⟨hf, hg⟩
protected lemma map₂ [has_zero δ] {μ : measure α} {f : α →ₛ β} (hf : f.fin_meas_supp μ)
{g : α →ₛ γ} (hg : g.fin_meas_supp μ) {op : β → γ → δ} (H : op 0 0 = 0) :
((pair f g).map (function.uncurry op)).fin_meas_supp μ :=
(hf.pair hg).map H
protected lemma add {β} [add_monoid β] {f g : α →ₛ β} (hf : f.fin_meas_supp μ)
(hg : g.fin_meas_supp μ) :
(f + g).fin_meas_supp μ :=
by { rw [add_eq_map₂], exact hf.map₂ hg (zero_add 0) }
protected lemma mul {β} [monoid_with_zero β] {f g : α →ₛ β} (hf : f.fin_meas_supp μ)
(hg : g.fin_meas_supp μ) :
(f * g).fin_meas_supp μ :=
by { rw [mul_eq_map₂], exact hf.map₂ hg (zero_mul 0) }
lemma lintegral_lt_top {f : α →ₛ ennreal} (hm : f.fin_meas_supp μ) (hf : ∀ᵐ a ∂μ, f a < ⊤) :
f.lintegral μ < ⊤ :=
begin
refine sum_lt_top (λ a ha, _),
rcases eq_or_lt_of_le (le_top : a ≤ ⊤) with rfl|ha,
{ simp only [ae_iff, lt_top_iff_ne_top, ne.def, not_not] at hf,
simp [set.preimage, hf] },
{ by_cases ha0 : a = 0,
{ subst a, rwa [zero_mul] },
{ exact mul_lt_top ha (fin_meas_supp_iff.1 hm _ ha0) } }
end
lemma of_lintegral_lt_top {f : α →ₛ ennreal} (h : f.lintegral μ < ⊤) : f.fin_meas_supp μ :=
begin
refine fin_meas_supp_iff.2 (λ b hb, _),
rw [lintegral, sum_lt_top_iff] at h,
by_cases b_mem : b ∈ f.range,
{ rw ennreal.lt_top_iff_ne_top,
have h : ¬ _ = ⊤ := ennreal.lt_top_iff_ne_top.1 (h b b_mem),
simp only [mul_eq_top, not_or_distrib, not_and_distrib] at h,
rcases h with ⟨h, h'⟩,
refine or.elim h (λh, by contradiction) (λh, h) },
{ rw ← preimage_eq_empty_iff at b_mem,
rw [b_mem, measure_empty],
exact with_top.zero_lt_top }
end
lemma iff_lintegral_lt_top {f : α →ₛ ennreal} (hf : ∀ᵐ a ∂μ, f a < ⊤) :
f.fin_meas_supp μ ↔ f.lintegral μ < ⊤ :=
⟨λ h, h.lintegral_lt_top hf, λ h, of_lintegral_lt_top h⟩
end fin_meas_supp
end fin_meas_supp
end simple_func
section lintegral
open simple_func
variables [measurable_space α] {μ : measure α}
/-- The lower Lebesgue integral of a function `f` with respect to a measure `μ`. -/
def lintegral (μ : measure α) (f : α → ennreal) : ennreal :=
⨆ (g : α →ₛ ennreal) (hf : ⇑g ≤ f), g.lintegral μ
notation `∫⁻` binders `, ` r:(scoped:60 f, f) ` ∂` μ:70 := lintegral μ r
notation `∫⁻` binders `, ` r:(scoped:60 f, lintegral volume f) := r
notation `∫⁻` binders ` in ` s `, ` r:(scoped:60 f, f) ` ∂` μ:70 :=
lintegral (measure.restrict μ s) r
notation `∫⁻` binders ` in ` s `, ` r:(scoped:60 f, lintegral (measure.restrict volume s) f) := r
theorem simple_func.lintegral_eq_lintegral (f : α →ₛ ennreal) (μ : measure α) :
∫⁻ a, f a ∂ μ = f.lintegral μ :=
le_antisymm
(bsupr_le $ λ g hg, lintegral_mono hg $ le_refl _)
(le_supr_of_le f $ le_supr_of_le (le_refl _) (le_refl _))
@[mono] lemma lintegral_mono' ⦃μ ν : measure α⦄ (hμν : μ ≤ ν) ⦃f g : α → ennreal⦄ (hfg : f ≤ g) :
∫⁻ a, f a ∂μ ≤ ∫⁻ a, g a ∂ν :=
supr_le_supr $ λ φ, supr_le_supr2 $ λ hφ, ⟨le_trans hφ hfg, lintegral_mono (le_refl φ) hμν⟩
lemma lintegral_mono ⦃f g : α → ennreal⦄ (hfg : f ≤ g) :
∫⁻ a, f a ∂μ ≤ ∫⁻ a, g a ∂μ :=
lintegral_mono' (le_refl μ) hfg
lemma monotone_lintegral (μ : measure α) : monotone (lintegral μ) :=
lintegral_mono
@[simp] lemma lintegral_const (c : ennreal) : ∫⁻ a, c ∂μ = c * μ univ :=
by rw [← simple_func.const_lintegral, ← simple_func.lintegral_eq_lintegral, simple_func.coe_const]
/-- `∫⁻ a in s, f a ∂μ` is defined as the supremum of integrals of simple functions
`φ : α →ₛ ennreal` such that `φ ≤ f`. This lemma says that it suffices to take
functions `φ : α →ₛ ℝ≥0`. -/
lemma lintegral_eq_nnreal (f : α → ennreal) (μ : measure α) :
(∫⁻ a, f a ∂μ) = (⨆ (φ : α →ₛ ℝ≥0) (hf : ∀ x, ↑(φ x) ≤ f x),
(φ.map (coe : nnreal → ennreal)).lintegral μ) :=
begin
refine le_antisymm
(bsupr_le $ assume φ hφ, _)
(supr_le_supr2 $ λ φ, ⟨φ.map (coe : ℝ≥0 → ennreal), le_refl _⟩),
by_cases h : ∀ᵐ a ∂μ, φ a ≠ ⊤,
{ let ψ := φ.map ennreal.to_nnreal,
replace h : ψ.map (coe : ℝ≥0 → ennreal) =ᵐ[μ] φ :=
h.mono (λ a, ennreal.coe_to_nnreal),
have : ∀ x, ↑(ψ x) ≤ f x := λ x, le_trans ennreal.coe_to_nnreal_le_self (hφ x),
exact le_supr_of_le (φ.map ennreal.to_nnreal)
(le_supr_of_le this (ge_of_eq $ lintegral_congr h)) },
{ have h_meas : μ (φ ⁻¹' {⊤}) ≠ 0, from mt measure_zero_iff_ae_nmem.1 h,
refine le_trans le_top (ge_of_eq $ (supr_eq_top _).2 $ λ b hb, _),
obtain ⟨n, hn⟩ : ∃ n : ℕ, b < n * μ (φ ⁻¹' {⊤}), from exists_nat_mul_gt h_meas (ne_of_lt hb),
use (const α (n : ℝ≥0)).restrict (φ ⁻¹' {⊤}),
simp only [lt_supr_iff, exists_prop, coe_restrict, φ.is_measurable_preimage, coe_const,
ennreal.coe_indicator, map_coe_ennreal_restrict, map_const, ennreal.coe_nat,
restrict_const_lintegral],
refine ⟨indicator_le (λ x hx, le_trans _ (hφ _)), hn⟩,
simp only [mem_preimage, mem_singleton_iff] at hx,
simp only [hx, le_top] }
end
theorem supr_lintegral_le {ι : Sort*} (f : ι → α → ennreal) :
(⨆i, ∫⁻ a, f i a ∂μ) ≤ (∫⁻ a, ⨆i, f i a ∂μ) :=
begin
simp only [← supr_apply],
exact (monotone_lintegral μ).le_map_supr
end
theorem supr2_lintegral_le {ι : Sort*} {ι' : ι → Sort*} (f : Π i, ι' i → α → ennreal) :
(⨆i (h : ι' i), ∫⁻ a, f i h a ∂μ) ≤ (∫⁻ a, ⨆i (h : ι' i), f i h a ∂μ) :=
by { convert (monotone_lintegral μ).le_map_supr2 f, ext1 a, simp only [supr_apply] }
theorem le_infi_lintegral {ι : Sort*} (f : ι → α → ennreal) :
(∫⁻ a, ⨅i, f i a ∂μ) ≤ (⨅i, ∫⁻ a, f i a ∂μ) :=
by { simp only [← infi_apply], exact (monotone_lintegral μ).map_infi_le }
theorem le_infi2_lintegral {ι : Sort*} {ι' : ι → Sort*} (f : Π i, ι' i → α → ennreal) :
(∫⁻ a, ⨅ i (h : ι' i), f i h a ∂μ) ≤ (⨅ i (h : ι' i), ∫⁻ a, f i h a ∂μ) :=
by { convert (monotone_lintegral μ).map_infi2_le f, ext1 a, simp only [infi_apply] }
/-- Monotone convergence theorem -- sometimes called Beppo-Levi convergence.
See `lintegral_supr_directed` for a more general form. -/
theorem lintegral_supr
{f : ℕ → α → ennreal} (hf : ∀n, measurable (f n)) (h_mono : monotone f) :
(∫⁻ a, ⨆n, f n a ∂μ) = (⨆n, ∫⁻ a, f n a ∂μ) :=
begin
set c : nnreal → ennreal := coe,
set F := λ a:α, ⨆n, f n a,
have hF : measurable F := measurable_supr hf,
refine le_antisymm _ (supr_lintegral_le _),
rw [lintegral_eq_nnreal],
refine supr_le (assume s, supr_le (assume hsf, _)),
refine ennreal.le_of_forall_lt_one_mul_lt (assume a ha, _),
rcases ennreal.lt_iff_exists_coe.1 ha with ⟨r, rfl, ha⟩,
have ha : r < 1 := ennreal.coe_lt_coe.1 ha,
let rs := s.map (λa, r * a),
have eq_rs : (const α r : α →ₛ ennreal) * map c s = rs.map c,
{ ext1 a, exact ennreal.coe_mul.symm },
have eq : ∀p, (rs.map c) ⁻¹' {p} = (⋃n, (rs.map c) ⁻¹' {p} ∩ {a | p ≤ f n a}),
{ assume p,
rw [← inter_Union, ← inter_univ ((map c rs) ⁻¹' {p})] {occs := occurrences.pos [1]},
refine set.ext (assume x, and_congr_right $ assume hx, (true_iff _).2 _),
by_cases p_eq : p = 0, { simp [p_eq] },
simp at hx, subst hx,
have : r * s x ≠ 0, { rwa [(≠), ← ennreal.coe_eq_zero] },
have : s x ≠ 0, { refine mt _ this, assume h, rw [h, mul_zero] },
have : (rs.map c) x < ⨆ (n : ℕ), f n x,
{ refine lt_of_lt_of_le (ennreal.coe_lt_coe.2 (_)) (hsf x),
suffices : r * s x < 1 * s x, simpa [rs],
exact mul_lt_mul_of_pos_right ha (zero_lt_iff_ne_zero.2 this) },
rcases lt_supr_iff.1 this with ⟨i, hi⟩,
exact mem_Union.2 ⟨i, le_of_lt hi⟩ },
have mono : ∀r:ennreal, monotone (λn, (rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}),
{ assume r i j h,
refine inter_subset_inter (subset.refl _) _,
assume x hx, exact le_trans hx (h_mono h x) },
have h_meas : ∀n, is_measurable {a : α | ⇑(map c rs) a ≤ f n a} :=
assume n, is_measurable_le (simple_func.measurable _) (hf n),
calc (r:ennreal) * (s.map c).lintegral μ = ∑ r in (rs.map c).range, r * μ ((rs.map c) ⁻¹' {r}) :
by rw [← const_mul_lintegral, eq_rs, simple_func.lintegral]
... ≤ ∑ r in (rs.map c).range, r * μ (⋃n, (rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}) :
le_of_eq (finset.sum_congr rfl $ assume x hx, by rw ← eq)
... ≤ ∑ r in (rs.map c).range, (⨆n, r * μ ((rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a})) :
le_of_eq (finset.sum_congr rfl $ assume x hx,
begin
rw [measure_Union_eq_supr_nat _ (mono x), ennreal.mul_supr],
{ assume i,
refine ((rs.map c).is_measurable_preimage _).inter _,
exact hf i is_measurable_Ici }
end)
... ≤ ⨆n, ∑ r in (rs.map c).range, r * μ ((rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}) :
begin
refine le_of_eq _,
rw [ennreal.finset_sum_supr_nat],
assume p i j h,
exact canonically_ordered_semiring.mul_le_mul (le_refl _) (measure_mono $ mono p h)
end
... ≤ (⨆n:ℕ, ((rs.map c).restrict {a | (rs.map c) a ≤ f n a}).lintegral μ) :
begin
refine supr_le_supr (assume n, _),
rw [restrict_lintegral _ (h_meas n)],
{ refine le_of_eq (finset.sum_congr rfl $ assume r hr, _),
congr' 2,
ext a,
refine and_congr_right _,
simp {contextual := tt} }
end
... ≤ (⨆n, ∫⁻ a, f n a ∂μ) :
begin
refine supr_le_supr (assume n, _),
rw [← simple_func.lintegral_eq_lintegral],
refine lintegral_mono (assume a, _),
dsimp,
rw [restrict_apply],
split_ifs; simp, simpa using h,
exact h_meas n
end
end
lemma lintegral_eq_supr_eapprox_lintegral {f : α → ennreal} (hf : measurable f) :
(∫⁻ a, f a ∂μ) = (⨆n, (eapprox f n).lintegral μ) :=
calc (∫⁻ a, f a ∂μ) = (∫⁻ a, ⨆n, (eapprox f n : α → ennreal) a ∂μ) :
by congr; ext a; rw [supr_eapprox_apply f hf]
... = (⨆n, ∫⁻ a, (eapprox f n : α → ennreal) a ∂μ) :
begin
rw [lintegral_supr],
{ assume n, exact (eapprox f n).measurable },
{ assume i j h, exact (monotone_eapprox f h) }
end
... = (⨆n, (eapprox f n).lintegral μ) : by congr; ext n; rw [(eapprox f n).lintegral_eq_lintegral]
lemma lintegral_add {f g : α → ennreal} (hf : measurable f) (hg : measurable g) :
(∫⁻ a, f a + g a ∂μ) = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) :=
calc (∫⁻ a, f a + g a ∂μ) =
(∫⁻ a, (⨆n, (eapprox f n : α → ennreal) a) + (⨆n, (eapprox g n : α → ennreal) a) ∂μ) :
by congr; funext a; rw [supr_eapprox_apply f hf, supr_eapprox_apply g hg]
... = (∫⁻ a, (⨆n, (eapprox f n + eapprox g n : α → ennreal) a) ∂μ) :
begin
congr, funext a,
rw [ennreal.supr_add_supr_of_monotone], { refl },
{ assume i j h, exact monotone_eapprox _ h a },
{ assume i j h, exact monotone_eapprox _ h a },
end
... = (⨆n, (eapprox f n).lintegral μ + (eapprox g n).lintegral μ) :
begin
rw [lintegral_supr],
{ congr,
funext n, rw [← simple_func.add_lintegral, ← simple_func.lintegral_eq_lintegral],
refl },
{ assume n, exact measurable.add (eapprox f n).measurable (eapprox g n).measurable },
{ assume i j h a, exact add_le_add (monotone_eapprox _ h _) (monotone_eapprox _ h _) }
end
... = (⨆n, (eapprox f n).lintegral μ) + (⨆n, (eapprox g n).lintegral μ) :
by refine (ennreal.supr_add_supr_of_monotone _ _).symm;
{ assume i j h, exact simple_func.lintegral_mono (monotone_eapprox _ h) (le_refl μ) }
... = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) :
by rw [lintegral_eq_supr_eapprox_lintegral hf, lintegral_eq_supr_eapprox_lintegral hg]
lemma lintegral_zero : (∫⁻ a:α, 0 ∂μ) = 0 := by simp
lemma lintegral_smul_meas (c : ennreal) (f : α → ennreal) :
∫⁻ a, f a ∂ (c • μ) = c * ∫⁻ a, f a ∂μ :=
by simp only [lintegral, supr_subtype', simple_func.lintegral_smul, ennreal.mul_supr, smul_eq_mul]
lemma lintegral_sum_meas {ι} (f : α → ennreal) (μ : ι → measure α) :
∫⁻ a, f a ∂(measure.sum μ) = ∑' i, ∫⁻ a, f a ∂(μ i) :=
begin
simp only [lintegral, supr_subtype', simple_func.lintegral_sum, ennreal.tsum_eq_supr_sum],
rw [supr_comm],
congr, funext s,
induction s using finset.induction_on with i s hi hs, { apply bot_unique, simp },
simp only [finset.sum_insert hi, ← hs],
refine (ennreal.supr_add_supr _).symm,
intros φ ψ,
exact ⟨⟨φ ⊔ ψ, λ x, sup_le (φ.2 x) (ψ.2 x)⟩,
add_le_add (simple_func.lintegral_mono le_sup_left (le_refl _))
(finset.sum_le_sum $ λ j hj, simple_func.lintegral_mono le_sup_right (le_refl _))⟩
end
lemma lintegral_add_meas (f : α → ennreal) (μ ν : measure α) :
∫⁻ a, f a ∂ (μ + ν) = ∫⁻ a, f a ∂μ + ∫⁻ a, f a ∂ν :=
by simpa [tsum_fintype] using lintegral_sum_meas f (λ b, cond b μ ν)
@[simp] lemma lintegral_zero_meas (f : α → ennreal) : ∫⁻ a, f a ∂0 = 0 :=
bot_unique $ by simp [lintegral]
lemma lintegral_finset_sum (s : finset β) {f : β → α → ennreal} (hf : ∀b, measurable (f b)) :
(∫⁻ a, ∑ b in s, f b a ∂μ) = ∑ b in s, ∫⁻ a, f b a ∂μ :=
begin
refine finset.induction_on s _ _,
{ simp },
{ assume a s has ih,
simp only [finset.sum_insert has],
rw [lintegral_add (hf _) (s.measurable_sum hf), ih] }
end
lemma lintegral_const_mul (r : ennreal) {f : α → ennreal} (hf : measurable f) :
(∫⁻ a, r * f a ∂μ) = r * (∫⁻ a, f a ∂μ) :=
calc (∫⁻ a, r * f a ∂μ) = (∫⁻ a, (⨆n, (const α r * eapprox f n) a) ∂μ) :
by { congr, funext a, rw [← supr_eapprox_apply f hf, ennreal.mul_supr], refl }
... = (⨆n, r * (eapprox f n).lintegral μ) :
begin
rw [lintegral_supr],
{ congr, funext n,
rw [← simple_func.const_mul_lintegral, ← simple_func.lintegral_eq_lintegral] },
{ assume n, exact simple_func.measurable _ },
{ assume i j h a, exact canonically_ordered_semiring.mul_le_mul (le_refl _)
(monotone_eapprox _ h _) }
end
... = r * (∫⁻ a, f a ∂μ) : by rw [← ennreal.mul_supr, lintegral_eq_supr_eapprox_lintegral hf]
lemma lintegral_const_mul_le (r : ennreal) (f : α → ennreal) :
r * (∫⁻ a, f a ∂μ) ≤ (∫⁻ a, r * f a ∂μ) :=
begin
rw [lintegral, ennreal.mul_supr],
refine supr_le (λs, _),
rw [ennreal.mul_supr],
simp only [supr_le_iff, ge_iff_le],
assume hs,
rw ← simple_func.const_mul_lintegral,
refine le_supr_of_le (const α r * s) (le_supr_of_le (λx, _) (le_refl _)),
exact canonically_ordered_semiring.mul_le_mul (le_refl _) (hs x)
end
lemma lintegral_const_mul' (r : ennreal) (f : α → ennreal) (hr : r ≠ ⊤) :
(∫⁻ a, r * f a ∂μ) = r * (∫⁻ a, f a ∂μ) :=
begin
by_cases h : r = 0,
{ simp [h] },
apply le_antisymm _ (lintegral_const_mul_le r f),
have rinv : r * r⁻¹ = 1 := ennreal.mul_inv_cancel h hr,
have rinv' : r ⁻¹ * r = 1, by { rw mul_comm, exact rinv },
have := lintegral_const_mul_le (r⁻¹) (λx, r * f x),
simp [(mul_assoc _ _ _).symm, rinv'] at this,
simpa [(mul_assoc _ _ _).symm, rinv]
using canonically_ordered_semiring.mul_le_mul (le_refl r) this
end
lemma lintegral_mono_ae {f g : α → ennreal} (h : ∀ᵐ a ∂μ, f a ≤ g a) :
(∫⁻ a, f a ∂μ) ≤ (∫⁻ a, g a ∂μ) :=
begin
rcases exists_is_measurable_superset_of_measure_eq_zero h with ⟨t, hts, ht, ht0⟩,
have : ∀ᵐ x ∂μ, x ∉ t := measure_zero_iff_ae_nmem.1 ht0,
refine (supr_le $ assume s, supr_le $ assume hfs,
le_supr_of_le (s.restrict tᶜ) $ le_supr_of_le _ _),
{ assume a,
by_cases a ∈ t;
simp [h, restrict_apply, ht.compl],
exact le_trans (hfs a) (by_contradiction $ assume hnfg, h (hts hnfg)) },
{ refine le_of_eq (simple_func.lintegral_congr $ this.mono $ λ a hnt, _),
by_cases hat : a ∈ t; simp [hat, ht.compl],
exact (hnt hat).elim }
end
lemma lintegral_congr_ae {f g : α → ennreal} (h : f =ᵐ[μ] g) :
(∫⁻ a, f a ∂μ) = (∫⁻ a, g a ∂μ) :=
le_antisymm (lintegral_mono_ae $ h.le) (lintegral_mono_ae $ h.symm.le)
lemma lintegral_congr {f g : α → ennreal} (h : ∀ a, f a = g a) :
(∫⁻ a, f a ∂μ) = (∫⁻ a, g a ∂μ) :=
by simp only [h]
-- TODO: Need a better way of rewriting inside of a integral
lemma lintegral_rw₁ {f f' : α → β} (h : f =ᵐ[μ] f') (g : β → ennreal) :
(∫⁻ a, g (f a) ∂μ) = (∫⁻ a, g (f' a) ∂μ) :=
lintegral_congr_ae $ h.mono $ λ a h, by rw h
-- TODO: Need a better way of rewriting inside of a integral
lemma lintegral_rw₂ {f₁ f₁' : α → β} {f₂ f₂' : α → γ} (h₁ : f₁ =ᵐ[μ] f₁')
(h₂ : f₂ =ᵐ[μ] f₂') (g : β → γ → ennreal) :
(∫⁻ a, g (f₁ a) (f₂ a) ∂μ) = (∫⁻ a, g (f₁' a) (f₂' a) ∂μ) :=
lintegral_congr_ae $ h₁.mp $ h₂.mono $ λ _ h₂ h₁, by rw [h₁, h₂]
@[simp] lemma lintegral_indicator (f : α → ennreal) {s : set α} (hs : is_measurable s) :
∫⁻ a, s.indicator f a ∂μ = ∫⁻ a in s, f a ∂μ :=
begin
simp only [lintegral, ← restrict_lintegral_eq_lintegral_restrict _ hs, supr_subtype'],
apply le_antisymm; refine supr_le_supr2 (subtype.forall.2 $ λ φ hφ, _),
{ refine ⟨⟨φ, le_trans hφ (indicator_le_self _ _)⟩, _⟩,
refine simple_func.lintegral_mono (λ x, _) (le_refl _),
by_cases hx : x ∈ s,
{ simp [hx, hs, le_refl] },
{ apply le_trans (hφ x),
simp [hx, hs, le_refl] } },
{ refine ⟨⟨φ.restrict s, λ x, _⟩, le_refl _⟩,
simp [hφ x, hs, indicator_le_indicator] }
end
/-- Chebyshev's inequality -/
lemma mul_meas_ge_le_lintegral {f : α → ennreal} (hf : measurable f) (ε : ennreal) :
ε * μ {x | ε ≤ f x} ≤ ∫⁻ a, f a ∂μ :=
begin
have : is_measurable {a : α | ε ≤ f a }, from hf is_measurable_Ici,
rw [← simple_func.restrict_const_lintegral _ this, ← simple_func.lintegral_eq_lintegral],
refine lintegral_mono (λ a, _),
simp only [restrict_apply _ this],
split_ifs; [assumption, exact zero_le _]
end
lemma meas_ge_le_lintegral_div {f : α → ennreal} (hf : measurable f) {ε : ennreal}
(hε : ε ≠ 0) (hε' : ε ≠ ⊤) :
μ {x | ε ≤ f x} ≤ (∫⁻ a, f a ∂μ) / ε :=
(ennreal.le_div_iff_mul_le (or.inl hε) (or.inl hε')).2 $
by { rw [mul_comm], exact mul_meas_ge_le_lintegral hf ε }
lemma lintegral_eq_zero_iff {f : α → ennreal} (hf : measurable f) :
∫⁻ a, f a ∂μ = 0 ↔ (f =ᵐ[μ] 0) :=
begin
refine iff.intro (assume h, _) (assume h, _),
{ have : ∀n:ℕ, ∀ᵐ a ∂μ, f a < n⁻¹,
{ assume n,
rw [ae_iff, ← le_zero_iff_eq, ← @ennreal.zero_div n⁻¹,
ennreal.le_div_iff_mul_le, mul_comm],
simp only [not_lt],
-- TODO: why `rw ← h` fails with "not an equality or an iff"?
exacts [h ▸ mul_meas_ge_le_lintegral hf n⁻¹,
or.inl (ennreal.inv_ne_zero.2 ennreal.coe_nat_ne_top),
or.inr ennreal.zero_ne_top] },
refine (ae_all_iff.2 this).mono (λ a ha, _),
by_contradiction h,
rcases ennreal.exists_inv_nat_lt h with ⟨n, hn⟩,
exact (lt_irrefl _ $ lt_trans hn $ ha n).elim },
{ calc ∫⁻ a, f a ∂μ = ∫⁻ a, 0 ∂μ : lintegral_congr_ae h
... = 0 : lintegral_zero }
end
/-- Weaker version of the monotone convergence theorem-/
lemma lintegral_supr_ae {f : ℕ → α → ennreal} (hf : ∀n, measurable (f n))
(h_mono : ∀n, ∀ᵐ a ∂μ, f n a ≤ f n.succ a) :
(∫⁻ a, ⨆n, f n a ∂μ) = (⨆n, ∫⁻ a, f n a ∂μ) :=
let ⟨s, hs⟩ := exists_is_measurable_superset_of_measure_eq_zero
(ae_iff.1 (ae_all_iff.2 h_mono)) in
let g := λ n a, if a ∈ s then 0 else f n a in
have g_eq_f : ∀ᵐ a ∂μ, ∀n, g n a = f n a,
from (measure_zero_iff_ae_nmem.1 hs.2.2).mono (assume a ha n, if_neg ha),
calc
∫⁻ a, ⨆n, f n a ∂μ = ∫⁻ a, ⨆n, g n a ∂μ :
lintegral_congr_ae $ g_eq_f.mono $ λ a ha, by simp only [ha]
... = ⨆n, (∫⁻ a, g n a ∂μ) :
lintegral_supr
(assume n, measurable_const.piecewise hs.2.1 (hf n))
(monotone_of_monotone_nat $ assume n a, classical.by_cases
(assume h : a ∈ s, by simp [g, if_pos h])
(assume h : a ∉ s,
begin
simp only [g, if_neg h], have := hs.1, rw subset_def at this, have := mt (this a) h,
simp only [not_not, mem_set_of_eq] at this, exact this n
end))
... = ⨆n, (∫⁻ a, f n a ∂μ) :
by simp only [lintegral_congr_ae (g_eq_f.mono $ λ a ha, ha _)]
lemma lintegral_sub {f g : α → ennreal} (hf : measurable f) (hg : measurable g)
(hg_fin : ∫⁻ a, g a ∂μ < ⊤) (h_le : g ≤ᵐ[μ] f) :
∫⁻ a, f a - g a ∂μ = ∫⁻ a, f a ∂μ - ∫⁻ a, g a ∂μ :=
begin
rw [← ennreal.add_left_inj hg_fin,
ennreal.sub_add_cancel_of_le (lintegral_mono_ae h_le),
← lintegral_add (hf.ennreal_sub hg) hg],
refine lintegral_congr_ae (h_le.mono $ λ x hx, _),
exact ennreal.sub_add_cancel_of_le hx
end
/-- Monotone convergence theorem for nonincreasing sequences of functions -/
lemma lintegral_infi_ae
{f : ℕ → α → ennreal} (h_meas : ∀n, measurable (f n))
(h_mono : ∀n:ℕ, f n.succ ≤ᵐ[μ] f n) (h_fin : ∫⁻ a, f 0 a ∂μ < ⊤) :
∫⁻ a, ⨅n, f n a ∂μ = ⨅n, ∫⁻ a, f n a ∂μ :=
have fn_le_f0 : ∫⁻ a, ⨅n, f n a ∂μ ≤ ∫⁻ a, f 0 a ∂μ, from
lintegral_mono (assume a, infi_le_of_le 0 (le_refl _)),
have fn_le_f0' : (⨅n, ∫⁻ a, f n a ∂μ) ≤ ∫⁻ a, f 0 a ∂μ, from infi_le_of_le 0 (le_refl _),
(ennreal.sub_right_inj h_fin fn_le_f0 fn_le_f0').1 $
show ∫⁻ a, f 0 a ∂μ - ∫⁻ a, ⨅n, f n a ∂μ = ∫⁻ a, f 0 a ∂μ - (⨅n, ∫⁻ a, f n a ∂μ), from
calc
∫⁻ a, f 0 a ∂μ - (∫⁻ a, ⨅n, f n a ∂μ) = ∫⁻ a, f 0 a - ⨅n, f n a ∂μ:
(lintegral_sub (h_meas 0) (measurable_infi h_meas)
(calc
(∫⁻ a, ⨅n, f n a ∂μ) ≤ ∫⁻ a, f 0 a ∂μ : lintegral_mono (assume a, infi_le _ _)
... < ⊤ : h_fin )
(ae_of_all _ $ assume a, infi_le _ _)).symm
... = ∫⁻ a, ⨆n, f 0 a - f n a ∂μ : congr rfl (funext (assume a, ennreal.sub_infi))
... = ⨆n, ∫⁻ a, f 0 a - f n a ∂μ :
lintegral_supr_ae
(assume n, (h_meas 0).ennreal_sub (h_meas n))
(assume n, (h_mono n).mono $ assume a ha, ennreal.sub_le_sub (le_refl _) ha)
... = ⨆n, ∫⁻ a, f 0 a ∂μ - ∫⁻ a, f n a ∂μ :
have h_mono : ∀ᵐ a ∂μ, ∀n:ℕ, f n.succ a ≤ f n a := ae_all_iff.2 h_mono,
have h_mono : ∀n, ∀ᵐ a ∂μ, f n a ≤ f 0 a := assume n, h_mono.mono $ assume a h,
begin
induction n with n ih,
{exact le_refl _}, {exact le_trans (h n) ih}
end,
congr rfl (funext $ assume n, lintegral_sub (h_meas _) (h_meas _)
(calc
∫⁻ a, f n a ∂μ ≤ ∫⁻ a, f 0 a ∂μ : lintegral_mono_ae $ h_mono n
... < ⊤ : h_fin)
(h_mono n))
... = ∫⁻ a, f 0 a ∂μ - ⨅n, ∫⁻ a, f n a ∂μ : ennreal.sub_infi.symm
/-- Monotone convergence theorem for nonincreasing sequences of functions -/
lemma lintegral_infi
{f : ℕ → α → ennreal} (h_meas : ∀n, measurable (f n))
(h_mono : ∀ ⦃m n⦄, m ≤ n → f n ≤ f m) (h_fin : ∫⁻ a, f 0 a ∂μ < ⊤) :
∫⁻ a, ⨅n, f n a ∂μ = ⨅n, ∫⁻ a, f n a ∂μ :=
lintegral_infi_ae h_meas (λ n, ae_of_all _ $ h_mono $ le_of_lt n.lt_succ_self) h_fin
/-- Known as Fatou's lemma -/
lemma lintegral_liminf_le {f : ℕ → α → ennreal} (h_meas : ∀n, measurable (f n)) :
∫⁻ a, liminf at_top (λ n, f n a) ∂μ ≤ liminf at_top (λ n, ∫⁻ a, f n a ∂μ) :=
calc
∫⁻ a, liminf at_top (λ n, f n a) ∂μ = ∫⁻ a, ⨆n:ℕ, ⨅i≥n, f i a ∂μ :
by simp only [liminf_eq_supr_infi_of_nat]
... = ⨆n:ℕ, ∫⁻ a, ⨅i≥n, f i a ∂μ :
lintegral_supr
(assume n, measurable_binfi _ h_meas)
(assume n m hnm a, infi_le_infi_of_subset $ λ i hi, le_trans hnm hi)
... ≤ ⨆n:ℕ, ⨅i≥n, ∫⁻ a, f i a ∂μ :
supr_le_supr $ λ n, le_infi2_lintegral _
... = liminf at_top (λ n, ∫⁻ a, f n a ∂μ) : liminf_eq_supr_infi_of_nat.symm
lemma limsup_lintegral_le {f : ℕ → α → ennreal} {g : α → ennreal}
(hf_meas : ∀ n, measurable (f n)) (h_bound : ∀n, f n ≤ᵐ[μ] g) (h_fin : ∫⁻ a, g a ∂μ < ⊤) :
limsup at_top (λn, ∫⁻ a, f n a ∂μ) ≤ ∫⁻ a, limsup at_top (λn, f n a) ∂μ :=
calc
limsup at_top (λn, ∫⁻ a, f n a ∂μ) = ⨅n:ℕ, ⨆i≥n, ∫⁻ a, f i a ∂μ :
limsup_eq_infi_supr_of_nat
... ≤ ⨅n:ℕ, ∫⁻ a, ⨆i≥n, f i a ∂μ :
infi_le_infi $ assume n, supr2_lintegral_le _
... = ∫⁻ a, ⨅n:ℕ, ⨆i≥n, f i a ∂μ :
begin
refine (lintegral_infi _ _ _).symm,
{ assume n, exact measurable_bsupr _ hf_meas },
{ assume n m hnm a, exact (supr_le_supr_of_subset $ λ i hi, le_trans hnm hi) },
{ refine lt_of_le_of_lt (lintegral_mono_ae _) h_fin,
refine (ae_all_iff.2 h_bound).mono (λ n hn, _),
exact supr_le (λ i, supr_le $ λ hi, hn i) }
end
... = ∫⁻ a, limsup at_top (λn, f n a) ∂μ :
by simp only [limsup_eq_infi_supr_of_nat]
/-- Dominated convergence theorem for nonnegative functions -/
lemma tendsto_lintegral_of_dominated_convergence
{F : ℕ → α → ennreal} {f : α → ennreal} (bound : α → ennreal)
(hF_meas : ∀n, measurable (F n)) (h_bound : ∀n, F n ≤ᵐ[μ] bound)
(h_fin : ∫⁻ a, bound a ∂μ < ⊤)
(h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) :
tendsto (λn, ∫⁻ a, F n a ∂μ) at_top (𝓝 (∫⁻ a, f a ∂μ)) :=
tendsto_of_le_liminf_of_limsup_le
(calc ∫⁻ a, f a ∂μ = ∫⁻ a, liminf at_top (λ (n : ℕ), F n a) ∂μ :
lintegral_congr_ae $ h_lim.mono $ assume a h, h.liminf_eq.symm
... ≤ liminf at_top (λ n, ∫⁻ a, F n a ∂μ) : lintegral_liminf_le hF_meas)
(calc limsup at_top (λ (n : ℕ), ∫⁻ a, F n a ∂μ) ≤ ∫⁻ a, limsup at_top (λn, F n a) ∂μ :
limsup_lintegral_le hF_meas h_bound h_fin
... = ∫⁻ a, f a ∂μ : lintegral_congr_ae $ h_lim.mono $ λ a h, h.limsup_eq)
/-- Dominated convergence theorem for filters with a countable basis -/
lemma tendsto_lintegral_filter_of_dominated_convergence {ι} {l : filter ι}
{F : ι → α → ennreal} {f : α → ennreal} (bound : α → ennreal)
(hl_cb : l.is_countably_generated)
(hF_meas : ∀ᶠ n in l, measurable (F n))
(h_bound : ∀ᶠ n in l, ∀ᵐ a ∂μ, F n a ≤ bound a)
(h_fin : ∫⁻ a, bound a ∂μ < ⊤)
(h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) l (𝓝 (f a))) :
tendsto (λn, ∫⁻ a, F n a ∂μ) l (𝓝 $ ∫⁻ a, f a ∂μ) :=
begin
rw hl_cb.tendsto_iff_seq_tendsto,
{ intros x xl,
have hxl, { rw tendsto_at_top' at xl, exact xl },
have h := inter_mem_sets hF_meas h_bound,
replace h := hxl _ h,
rcases h with ⟨k, h⟩,
rw ← tendsto_add_at_top_iff_nat k,
refine tendsto_lintegral_of_dominated_convergence _ _ _ _ _,
{ exact bound },
{ intro, refine (h _ _).1, exact nat.le_add_left _ _ },
{ intro, refine (h _ _).2, exact nat.le_add_left _ _ },
{ assumption },
{ refine h_lim.mono (λ a h_lim, _),
apply @tendsto.comp _ _ _ (λn, x (n + k)) (λn, F n a),
{ assumption },
rw tendsto_add_at_top_iff_nat,
assumption } },
end
section
open encodable
/-- Monotone convergence for a suprema over a directed family and indexed by an encodable type -/
theorem lintegral_supr_directed [encodable β] {f : β → α → ennreal}
(hf : ∀b, measurable (f b)) (h_directed : directed (≤) f) :
∫⁻ a, ⨆b, f b a ∂μ = ⨆b, ∫⁻ a, f b a ∂μ :=
begin
by_cases hβ : ¬ nonempty β,
{ have : ∀f : β → ennreal, (⨆(b : β), f b) = 0 :=
assume f, supr_eq_bot.2 (assume b, (hβ ⟨b⟩).elim),
simp [this] },
cases of_not_not hβ with b,
haveI iβ : inhabited β := ⟨b⟩, clear hβ b,
have : ∀a, (⨆ b, f b a) = (⨆ n, f (h_directed.sequence f n) a),
{ assume a,
refine le_antisymm (supr_le $ assume b, _) (supr_le $ assume n, le_supr (λn, f n a) _),
exact le_supr_of_le (encode b + 1) (h_directed.le_sequence b a) },
calc ∫⁻ a, ⨆ b, f b a ∂μ = ∫⁻ a, ⨆ n, f (h_directed.sequence f n) a ∂μ :
by simp only [this]
... = ⨆ n, ∫⁻ a, f (h_directed.sequence f n) a ∂μ :
lintegral_supr (assume n, hf _) h_directed.sequence_mono
... = ⨆ b, ∫⁻ a, f b a ∂μ :
begin
refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume b, _),
{ exact le_supr (λb, ∫⁻ a, f b a ∂μ) _ },
{ exact le_supr_of_le (encode b + 1)
(lintegral_mono $ h_directed.le_sequence b) }
end
end
end
lemma lintegral_tsum [encodable β] {f : β → α → ennreal} (hf : ∀i, measurable (f i)) :
∫⁻ a, ∑' i, f i a ∂μ = ∑' i, ∫⁻ a, f i a ∂μ :=
begin
simp only [ennreal.tsum_eq_supr_sum],
rw [lintegral_supr_directed],
{ simp [lintegral_finset_sum _ hf] },
{ assume b, exact finset.measurable_sum _ hf },
{ assume s t,
use [s ∪ t],
split,
exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_left _ _),
exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_right _ _) }
end
open measure
lemma lintegral_Union [encodable β] {s : β → set α} (hm : ∀ i, is_measurable (s i))
(hd : pairwise (disjoint on s)) (f : α → ennreal) :
∫⁻ a in ⋃ i, s i, f a ∂μ = ∑' i, ∫⁻ a in s i, f a ∂μ :=
by simp only [measure.restrict_Union hd hm, lintegral_sum_meas]
lemma lintegral_Union_le [encodable β] (s : β → set α) (f : α → ennreal) :
∫⁻ a in ⋃ i, s i, f a ∂μ ≤ ∑' i, ∫⁻ a in s i, f a ∂μ :=
begin
rw [← lintegral_sum_meas],
exact lintegral_mono' restrict_Union_le (le_refl _)
end
lemma lintegral_map [measurable_space β] {f : β → ennreal} {g : α → β}
(hf : measurable f) (hg : measurable g) :
∫⁻ a, f a ∂(map g μ) = ∫⁻ a, f (g a) ∂μ :=
begin
simp only [lintegral_eq_supr_eapprox_lintegral, hf, hf.comp hg],
{ congr, funext n, symmetry,
apply simple_func.lintegral_map,
{ assume a, exact congr_fun (simple_func.eapprox_comp hf hg) a },
{ assume s hs, exact map_apply hg hs } },
end
lemma lintegral_dirac (a : α) {f : α → ennreal} (hf : measurable f) :
∫⁻ a, f a ∂(dirac a) = f a :=
by simp [lintegral_congr_ae (eventually_eq_dirac hf)]
def measure.with_density (μ : measure α) (f : α → ennreal) : measure α :=
measure.of_measurable (λs hs, ∫⁻ a in s, f a ∂μ) (by simp) (λ s hs hd, lintegral_Union hs hd _)
lemma with_density_apply (f : α → ennreal) {s : set α} (hs : is_measurable s) :
μ.with_density f s = ∫⁻ a in s, f a ∂μ :=
measure.of_measurable_apply s hs
end lintegral
end measure_theory
|
25fdd42d05453d8e6ca0320d13006d6e08c7b43b | 82e44445c70db0f03e30d7be725775f122d72f3e | /src/data/lazy_list.lean | 3ea6ec0b50f07ba19157546d0d66ee31e81a9a21 | [
"Apache-2.0"
] | permissive | stjordanis/mathlib | 51e286d19140e3788ef2c470bc7b953e4991f0c9 | 2568d41bca08f5d6bf39d915434c8447e21f42ee | refs/heads/master | 1,631,748,053,501 | 1,627,938,886,000 | 1,627,938,886,000 | 228,728,358 | 0 | 0 | Apache-2.0 | 1,576,630,588,000 | 1,576,630,587,000 | null | UTF-8 | Lean | false | false | 3,817 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import tactic.lint
/-!
# Lazy lists
The type `lazy_list α` is a lazy list with elements of type `α`.
In the VM, these are potentially infinite lists
where all elements after the first are computed on-demand.
(This is only useful for execution in the VM,
logically we can prove that `lazy_list α` is isomorphic to `list α`.)
-/
universes u v w
/--
Lazy list.
All elements (except the first) are computed lazily.
-/
inductive lazy_list (α : Type u) : Type u
| nil : lazy_list
| cons (hd : α) (tl : thunk lazy_list) : lazy_list
namespace lazy_list
variables {α : Type u} {β : Type v} {δ : Type w}
instance : inhabited (lazy_list α) := ⟨nil⟩
/-- The singleton lazy list. -/
def singleton : α → lazy_list α
| a := cons a nil
/-- Constructs a lazy list from a list. -/
def of_list : list α → lazy_list α
| [] := nil
| (h::t) := cons h (of_list t)
/--
Converts a lazy list to a list.
If the lazy list is infinite,
then this function does not terminate.
-/
def to_list : lazy_list α → list α
| nil := []
| (cons h t) := h :: to_list (t ())
/--
Returns the first element of the lazy list,
or `default α` if the lazy list is empty.
-/
def head [inhabited α] : lazy_list α → α
| nil := default α
| (cons h t) := h
/--
Removes the first element of the lazy list.
-/
def tail : lazy_list α → lazy_list α
| nil := nil
| (cons h t) := t ()
/-- Appends two lazy lists. -/
def append : lazy_list α → thunk (lazy_list α) → lazy_list α
| nil l := l ()
| (cons h t) l := cons h (@append (t ()) l)
/-- Maps a function over a lazy list. -/
def map (f : α → β) : lazy_list α → lazy_list β
| nil := nil
| (cons h t) := cons (f h) (map (t ()))
/--
Maps a binary function over two lazy list.
Like `lazy_list.zip`, the result is only as long as the smaller input.
-/
def map₂ (f : α → β → δ) : lazy_list α → lazy_list β → lazy_list δ
| nil _ := nil
| _ nil := nil
| (cons h₁ t₁) (cons h₂ t₂) := cons (f h₁ h₂) (map₂ (t₁ ()) (t₂ ()))
/-- Zips two lazy lists. -/
def zip : lazy_list α → lazy_list β → lazy_list (α × β) :=
map₂ prod.mk
/-- The monadic join operation for lazy lists. -/
def join : lazy_list (lazy_list α) → lazy_list α
| nil := nil
| (cons h t) := append h (join (t ()))
/--
Maps a function over a lazy list.
Same as `lazy_list.map`, but with swapped arguments.
-/
def for (l : lazy_list α) (f : α → β) : lazy_list β :=
map f l
/-- The list containing the first `n` elements of a lazy list. -/
def approx : nat → lazy_list α → list α
| 0 l := []
| _ nil := []
| (a+1) (cons h t) := h :: approx a (t ())
/--
The lazy list of all elements satisfying the predicate.
If the lazy list is infinite and none of the elements satisfy the predicate,
then this function will not terminate.
-/
def filter (p : α → Prop) [decidable_pred p] : lazy_list α → lazy_list α
| nil := nil
| (cons h t) := if p h then cons h (filter (t ())) else filter (t ())
/-- The nth element of a lazy list as an option (like `list.nth`). -/
def nth : lazy_list α → nat → option α
| nil n := none
| (cons a l) 0 := some a
| (cons a l) (n+1) := nth (l ()) n
/--
The infinite lazy list `[x, f x, f (f x), ...]` of iterates of a function.
This definition is meta because it creates an infinite list.
-/
meta def iterates (f : α → α) : α → lazy_list α
| x := cons x (iterates (f x))
/-- The infinite lazy list `[i, i+1, i+2, ...]` -/
meta def iota (i : nat) : lazy_list nat :=
iterates nat.succ i
end lazy_list
|
e96d61421cb1754e198ce1f677aab03f5f6303a9 | 3446e92e64a5de7ed1f2109cfb024f83cd904c34 | /src/game/world5/level4.lean | e96181dfd3b52737f8d46118113772fa9f493cc9 | [] | no_license | kckennylau/natural_number_game | 019f4a5f419c9681e65234ecd124c564f9a0a246 | ad8c0adaa725975be8a9f978c8494a39311029be | refs/heads/master | 1,598,784,137,722 | 1,571,905,156,000 | 1,571,905,156,000 | 218,354,686 | 0 | 0 | null | 1,572,373,319,000 | 1,572,373,318,000 | null | UTF-8 | Lean | false | false | 1,036 | lean | import game.world5.level3 -- hide
import game.world2.level14 -- add_right_eq_zero
namespace mynat -- hide
/-
# World 5 : Inequality world
## Level 4 : `le_zero`
To do a level like this, you'll need to know that
if `a + c = 0` then `a = 0`. You could launch into
a proof of this e.g. via a case split on `a` (`cases a`
will split into the cases `a = 0` and `a = succ(b)`) but
actually we already proved this lemma -- it's `add_right_eq_zero`
from the extra levels in world 2 (it's level 14).
If you're planning on working through world 5, you should probably
check that you can do the extra levels in world 2 first.
oh -- start with `intro h`. You should have picked up stuff about
the `intro` tactic in the world 2 extra levels.
-/
/-
Pro tip: if `h` is a proof of `A = B`, then `h.symm` is a proof of `B = A`.
-/
/- Lemma
For all naturals `a`, if `a ≤ 0` then `a = 0`.
-/
lemma le_zero {a : mynat} (h : a ≤ 0) : a = 0 :=
begin [less_leaky]
cases h with c hc,
apply add_right_eq_zero hc.symm,
end
end mynat -- hide
|
c840ff554a2fdc55a6b4871ffdfb886b48a24ea8 | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /11_Tactic-Style_Proofs.org.42.lean | 37402070f157b30ff78ff9e01345fac9d19bad62 | [] | no_license | cjmazey/lean-tutorial | ba559a49f82aa6c5848b9bf17b7389bf7f4ba645 | 381f61c9fcac56d01d959ae0fa6e376f2c4e3b34 | refs/heads/master | 1,610,286,098,832 | 1,447,124,923,000 | 1,447,124,923,000 | 43,082,433 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 199 | lean | import standard
import data.nat
open nat
-- BEGIN
variables (a b : nat)
example (H₁ : a + 0 = 0) (H₂ : b + 0 = 0) : a + b + 0 = 0 :=
begin
rewrite add_zero at *,
rewrite [H₁, H₂]
end
-- END
|
e7612c621b3922d410289849a2bcfd99f7cf42ba | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/data/int/lemmas.lean | e5effced31ce86064dea9e7e160a3fba67514c75 | [
"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 | 3,265 | lean | /-
Copyright (c) 2016 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
-/
import data.set.function
import data.int.order.lemmas
import data.int.bitwise
import data.nat.cast.basic
import data.nat.order.lemmas
/-!
# Miscellaneous lemmas about the integers
This file contains lemmas about integers, which require further imports than
`data.int.basic` or `data.int.order`.
-/
open nat
namespace int
lemma le_coe_nat_sub (m n : ℕ) :
(m - n : ℤ) ≤ ↑(m - n : ℕ) :=
begin
by_cases h: m ≥ n,
{ exact le_of_eq (int.coe_nat_sub h).symm },
{ simp [le_of_not_ge h, coe_nat_le] }
end
/-! ### succ and pred -/
@[simp] lemma succ_coe_nat_pos (n : ℕ) : 0 < (n : ℤ) + 1 :=
lt_add_one_iff.mpr (by simp)
/-! ### nat abs -/
variables {a b : ℤ} {n : ℕ}
lemma nat_abs_eq_iff_sq_eq {a b : ℤ} : a.nat_abs = b.nat_abs ↔ a ^ 2 = b ^ 2 :=
by { rw [sq, sq], exact nat_abs_eq_iff_mul_self_eq }
lemma nat_abs_lt_iff_sq_lt {a b : ℤ} : a.nat_abs < b.nat_abs ↔ a ^ 2 < b ^ 2 :=
by { rw [sq, sq], exact nat_abs_lt_iff_mul_self_lt }
lemma nat_abs_le_iff_sq_le {a b : ℤ} : a.nat_abs ≤ b.nat_abs ↔ a ^ 2 ≤ b ^ 2 :=
by { rw [sq, sq], exact nat_abs_le_iff_mul_self_le }
lemma nat_abs_inj_of_nonneg_of_nonneg {a b : ℤ} (ha : 0 ≤ a) (hb : 0 ≤ b) :
nat_abs a = nat_abs b ↔ a = b :=
by rw [←sq_eq_sq ha hb, ←nat_abs_eq_iff_sq_eq]
lemma nat_abs_inj_of_nonpos_of_nonpos {a b : ℤ} (ha : a ≤ 0) (hb : b ≤ 0) :
nat_abs a = nat_abs b ↔ a = b :=
by simpa only [int.nat_abs_neg, neg_inj]
using nat_abs_inj_of_nonneg_of_nonneg
(neg_nonneg_of_nonpos ha) (neg_nonneg_of_nonpos hb)
lemma nat_abs_inj_of_nonneg_of_nonpos {a b : ℤ} (ha : 0 ≤ a) (hb : b ≤ 0) :
nat_abs a = nat_abs b ↔ a = -b :=
by simpa only [int.nat_abs_neg]
using nat_abs_inj_of_nonneg_of_nonneg ha (neg_nonneg_of_nonpos hb)
lemma nat_abs_inj_of_nonpos_of_nonneg {a b : ℤ} (ha : a ≤ 0) (hb : 0 ≤ b) :
nat_abs a = nat_abs b ↔ -a = b :=
by simpa only [int.nat_abs_neg]
using nat_abs_inj_of_nonneg_of_nonneg (neg_nonneg_of_nonpos ha) hb
section intervals
open set
lemma strict_mono_on_nat_abs : strict_mono_on nat_abs (Ici 0) :=
λ a ha b hb hab, nat_abs_lt_nat_abs_of_nonneg_of_lt ha hab
lemma strict_anti_on_nat_abs : strict_anti_on nat_abs (Iic 0) :=
λ a ha b hb hab, by simpa [int.nat_abs_neg]
using nat_abs_lt_nat_abs_of_nonneg_of_lt (right.nonneg_neg_iff.mpr hb) (neg_lt_neg_iff.mpr hab)
lemma inj_on_nat_abs_Ici : inj_on nat_abs (Ici 0) := strict_mono_on_nat_abs.inj_on
lemma inj_on_nat_abs_Iic : inj_on nat_abs (Iic 0) := strict_anti_on_nat_abs.inj_on
end intervals
/-! ### to_nat -/
lemma to_nat_of_nonpos : ∀ {z : ℤ}, z ≤ 0 → z.to_nat = 0
| 0 _ := rfl
| (n + 1 : ℕ) h := (h.not_lt (by simp)).elim
| -[1+ n] _ := rfl
/-! ### bitwise ops
This lemma is orphaned from `data.int.bitwise` as it also requires material from `data.int.order`.
-/
local attribute [simp] int.zero_div
@[simp] lemma div2_bit (b n) : div2 (bit b n) = n :=
begin
rw [bit_val, div2_val, add_comm, int.add_mul_div_left, (_ : (_/2:ℤ) = 0), zero_add],
cases b,
{ simp },
{ show of_nat _ = _, rw nat.div_eq_zero; simp },
{ cc }
end
end int
|
d20372f1a01993ba3286d64c9e5b021660624572 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/field_theory/normal.lean | 83528a1b1ac33d3255da643a43554d181f9d0e2d | [
"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 | 1,997 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import field_theory.minimal_polynomial
import field_theory.splitting_field
import linear_algebra.finite_dimensional
/-!
# Normal field extensions
In this file we define normal field extensions and prove that for a finite extension, being normal
is the same as being a splitting field (TODO).
## Main Definitions
- `normal F K` where `K` is a field extension of `F`.
-/
noncomputable theory
open_locale classical
open polynomial
universes u v
variables (F : Type u) (K : Type v) [field F] [field K] [algebra F K]
/-- Typeclass for normal field extension: `K` is a normal extension of `F` iff the minimal
polynomial of every element `x` in `K` splits in `K`, i.e. every conjugate of `x` is in `K`. -/
@[class] def normal : Prop :=
∀ x : K, ∃ H : is_integral F x, splits (algebra_map F K) (minimal_polynomial H)
theorem normal.is_integral [h : normal F K] (x : K) : is_integral F x := (h x).fst
theorem normal.splits [h : normal F K] (x : K) :
splits (algebra_map F K) (minimal_polynomial $ normal.is_integral F K x) := (h x).snd
theorem normal.exists_is_splitting_field [normal F K] [finite_dimensional F K] :
∃ p : polynomial F, is_splitting_field F K p :=
begin
obtain ⟨s, hs⟩ := finite_dimensional.exists_is_basis_finset F K,
refine ⟨s.prod $ λ x, minimal_polynomial $ normal.is_integral F K x,
splits_prod _ $ λ x hx, normal.splits F K x,
subalgebra.to_submodule_injective _⟩,
rw [algebra.coe_top, eq_top_iff, ← hs.2, submodule.span_le, set.range_subset_iff],
refine λ x, algebra.subset_adjoin (multiset.mem_to_finset.mpr $
(mem_roots $ mt (map_eq_zero $ algebra_map F K).1 $
finset.prod_ne_zero_iff.2 $ λ x hx, _).2 _),
{ exact minimal_polynomial.ne_zero _ },
rw [is_root.def, eval_map, ← aeval_def, alg_hom.map_prod],
exact finset.prod_eq_zero x.2 (minimal_polynomial.aeval _)
end
|
bbb4f7e4514794716a2a3f1208d46e065c4a3dea | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/testing/slim_check/gen.lean | 0a516e4cfba5344b7f9daaaf8f2fe7f7a56d4438 | [
"Apache-2.0"
] | permissive | lacker/mathlib | f2439c743c4f8eb413ec589430c82d0f73b2d539 | ddf7563ac69d42cfa4a1bfe41db1fed521bd795f | refs/heads/master | 1,671,948,326,773 | 1,601,479,268,000 | 1,601,479,268,000 | 298,686,743 | 0 | 0 | Apache-2.0 | 1,601,070,794,000 | 1,601,070,794,000 | null | UTF-8 | Lean | false | false | 3,634 | lean | /-
Copyright (c) 2020 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author(s): Simon Hudon
-/
import control.uliftable
import system.random
import system.random.basic
/-!
# `gen` Monad
This monad is used to formulate randomized computations with a parameter
to specify the desired size of the result.
This is a port of the Haskell QuickCheck library.
## Main definitions
* `gen` monad
## Local notation
* `i .. j` : `Icc i j`, the set of values between `i` and `j` inclusively;
## Tags
random testing
## References
* https://hackage.haskell.org/package/QuickCheck
-/
universes u v
namespace slim_check
/-- Monad to generate random examples to test properties with.
It has a `nat` parameter so that the caller can decide on the
size of the examples. -/
@[reducible, derive [monad, is_lawful_monad]]
def gen (α : Type u) := reader_t (ulift ℕ) rand α
variable (α : Type u)
local infix ` .. `:41 := set.Icc
/-- Execute a `gen` inside the `io` monad using `i` as the example
size and with a fresh random number generator. -/
def io.run_gen {α} (x : gen α) (i : ℕ) : io α :=
io.run_rand (x.run ⟨i⟩)
namespace gen
section rand
variables [preorder α]
/-- Lift `random.random` to the `gen` monad. -/
def choose_any [random α] : gen α :=
⟨ λ _, rand.random α ⟩
variables {α}
/-- Lift `random.random_r` to the `gen` monad. -/
def choose [bounded_random α] (x y : α) (p : x ≤ y) : gen (x .. y) :=
⟨ λ _, rand.random_r x y p ⟩
end rand
open nat (hiding choose)
/-- Generate a `nat` example between `x` and `y`. -/
def choose_nat (x y : ℕ) (p : x ≤ y) : gen (x .. y) :=
choose x y p
open nat
instance : uliftable gen.{u} gen.{v} :=
reader_t.uliftable' (equiv.ulift.trans equiv.ulift.symm)
instance : has_orelse gen.{u} :=
⟨ λ α x y, do
b ← uliftable.up $ choose_any bool,
if b.down then x else y ⟩
variable {α}
/-- Get access to the size parameter of the `gen` monad. For
reasons of universe polymorphism, it is specified in
continuation passing style. -/
def sized (cmd : ℕ → gen α) : gen α :=
⟨ λ ⟨sz⟩, reader_t.run (cmd sz) ⟨sz⟩ ⟩
/-- Create `n` examples using `cmd`. -/
def vector_of : ∀ (n : ℕ) (cmd : gen α), gen (vector α n)
| 0 _ := return vector.nil
| (succ n) cmd := vector.cons <$> cmd <*> vector_of n cmd
/-- Create a list of examples using `cmd`. The size is controlled
by the size parameter of `gen`. -/
def list_of (cmd : gen α) : gen (list α) :=
sized $ λ sz, do
do ⟨ n ⟩ ← uliftable.up $ choose_nat 0 (sz + 1) dec_trivial,
v ← vector_of n.val cmd,
return v.to_list
open ulift
/-- Given a list of example generators, choose one to create an example.
This definition is needed for to provide a local `fact $ 0 < xs.length`
instance to the type resolution system. -/
def one_of_aux (xs : list (gen α)) (pos : fact $ 0 < xs.length) : gen α := do
n ← uliftable.up $ choose_any (fin xs.length),
list.nth_le xs (down n).val (down n).is_lt
/-- Given a list of example generators, choose one to create an example. -/
def one_of (xs : list (gen α)) (pos : 0 < xs.length) : gen α := do
one_of_aux xs pos
/-- Generate a random permutation of a given list. -/
def permutation_of {α : Type u} : Π xs : list α, gen (subtype $ list.perm xs)
| [] := pure ⟨[], list.perm.nil ⟩
| (x :: xs) := do
⟨xs',h⟩ ← permutation_of xs,
⟨⟨n,_,h'⟩⟩ ← uliftable.up $ choose_nat 0 xs'.length dec_trivial,
pure ⟨list.insert_nth n x xs',
list.perm.trans (list.perm.cons _ h)
(list.perm_insert_nth _ _ h').symm ⟩
end gen
end slim_check
|
065b893664e64f9fb7b45ba6b3aea3d34e5f3e16 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/category/Group/injective.lean | 730283712ca5586c0c9cbf342b60e77d8218780f | [
"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,213 | 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.Group.epi_mono
import algebra.category.Module.epi_mono
import algebra.module.injective
import category_theory.preadditive.injective
import group_theory.divisible
import ring_theory.principal_ideal_domain
/-!
# Injective objects in the category of abelian groups
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we prove that divisible groups are injective object in category of (additive) abelian
groups.
-/
open category_theory
open_locale pointwise
universe u
variables (A : Type u) [add_comm_group A]
namespace AddCommGroup
lemma injective_of_injective_as_module [injective (⟨A⟩ : Module ℤ)] :
category_theory.injective (⟨A⟩ : AddCommGroup) :=
{ factors := λ X Y g f m,
begin
resetI,
let G : (⟨X⟩ : Module ℤ) ⟶ ⟨A⟩ :=
{ map_smul' := by { intros, rw [ring_hom.id_apply, g.to_fun_eq_coe, map_zsmul], }, ..g },
let F : (⟨X⟩ : Module ℤ) ⟶ ⟨Y⟩ :=
{ map_smul' := by { intros, rw [ring_hom.id_apply, f.to_fun_eq_coe, map_zsmul], }, ..f },
haveI : mono F,
{ refine ⟨λ Z α β eq1, _⟩,
let α' : AddCommGroup.of Z ⟶ X := α.to_add_monoid_hom,
let β' : AddCommGroup.of Z ⟶ X := β.to_add_monoid_hom,
have eq2 : α' ≫ f = β' ≫ f,
{ ext,
simp only [category_theory.comp_apply, linear_map.to_add_monoid_hom_coe],
simpa only [Module.coe_comp, linear_map.coe_mk,
function.comp_app] using fun_like.congr_fun eq1 x },
rw cancel_mono at eq2,
ext, simpa only using fun_like.congr_fun eq2 x, },
refine ⟨(injective.factor_thru G F).to_add_monoid_hom, _⟩,
ext, convert fun_like.congr_fun (injective.comp_factor_thru G F) x,
end }
lemma injective_as_module_of_injective_as_Ab [injective (⟨A⟩ : AddCommGroup)] :
injective (⟨A⟩ : Module ℤ) :=
{ factors := λ X Y g f m,
begin
resetI,
let G : (⟨X⟩ : AddCommGroup) ⟶ ⟨A⟩ := g.to_add_monoid_hom,
let F : (⟨X⟩ : AddCommGroup) ⟶ ⟨Y⟩ := f.to_add_monoid_hom,
haveI : mono F,
{ rw mono_iff_injective, intros _ _ h, exact ((Module.mono_iff_injective f).mp m) h, },
refine ⟨{map_smul' := _, ..injective.factor_thru G F}, _⟩,
{ intros m x, rw [add_monoid_hom.to_fun_eq_coe, ring_hom.id_apply],
induction m using int.induction_on with n hn n hn,
{ rw [zero_smul],
convert map_zero _,
convert zero_smul _ x, },
{ simp only [add_smul, map_add, hn, one_smul], },
{ simp only [sub_smul, map_sub, hn, one_smul] }, },
ext, convert fun_like.congr_fun (injective.comp_factor_thru G F) x,
end }
instance injective_of_divisible [divisible_by A ℤ] :
category_theory.injective (⟨A⟩ : AddCommGroup) :=
@@injective_of_injective_as_module A _ $
@@module.injective_object_of_injective_module ℤ _ A _ _ $
module.Baer.injective $
λ I g, begin
rcases is_principal_ideal_ring.principal I with ⟨m, rfl⟩,
by_cases m_eq_zero : m = 0,
{ subst m_eq_zero,
refine ⟨{ to_fun := _, map_add' := _, map_smul' := _ }, λ n hn, _⟩,
{ intros n, exact g 0, },
{ intros n1 n2,
simp only [map_zero, add_zero] },
{ intros n1 n2,
simp only [map_zero, smul_zero], },
{ rw [submodule.span_singleton_eq_bot.mpr rfl, submodule.mem_bot] at hn,
simp only [hn, map_zero],
symmetry,
convert map_zero _, }, },
{ set gₘ := g ⟨m, submodule.subset_span (set.mem_singleton _)⟩ with gm_eq,
refine ⟨{ to_fun := _, map_add' := _, map_smul' := _ }, λ n hn, _⟩,
{ intros n,
exact n • divisible_by.div gₘ m, },
{ intros n1 n2, simp only [add_smul], },
{ intros n1 n2,
rw [ring_hom.id_apply, smul_eq_mul, mul_smul], },
{ rw submodule.mem_span_singleton at hn,
rcases hn with ⟨n, rfl⟩,
simp only [gm_eq, algebra.id.smul_eq_mul, linear_map.coe_mk],
rw [mul_smul, divisible_by.div_cancel (g ⟨m, _⟩) m_eq_zero, ←linear_map.map_smul],
congr, }, },
end
end AddCommGroup
|
ca7178d966f6dbc2ad56c66d9e89938e0c7fed17 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/data/int/order/basic.lean | 2bbabc6993ae030f543c0c772939a9da14dbd1f7 | [
"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 | 25,098 | lean | /-
Copyright (c) 2016 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
-/
import data.int.basic
import algebra.ring.divisibility
import algebra.order.group.abs
import algebra.order.ring.char_zero
import tactic.assert_exists
/-!
# Order instances on the integers
This file contains:
* instances on `ℤ`. The stronger one is `int.linear_ordered_comm_ring`.
* basic lemmas about integers that involve order properties.
## Recursors
* `int.rec`: Sign disjunction. Something is true/defined on `ℤ` if it's true/defined for nonnegative
and for negative values. (Defined in core Lean 3)
* `int.induction_on`: Simple growing induction on positive numbers, plus simple decreasing induction
on negative numbers. Note that this recursor is currently only `Prop`-valued.
* `int.induction_on'`: Simple growing induction for numbers greater than `b`, plus simple decreasing
induction on numbers less than `b`.
-/
open nat
namespace int
instance : linear_ordered_comm_ring ℤ :=
{ add_le_add_left := @int.add_le_add_left,
mul_pos := @int.mul_pos,
zero_le_one := le_of_lt int.zero_lt_one,
.. int.comm_ring, .. int.linear_order, .. int.nontrivial }
/-! ### Extra instances to short-circuit type class resolution
-/
instance : ordered_comm_ring ℤ := strict_ordered_comm_ring.to_ordered_comm_ring'
instance : ordered_ring ℤ := strict_ordered_ring.to_ordered_ring'
instance : linear_ordered_add_comm_group ℤ := by apply_instance
end int
namespace int
theorem abs_eq_nat_abs : ∀ a : ℤ, |a| = nat_abs a
| (n : ℕ) := abs_of_nonneg $ coe_zero_le _
| -[1+ n] := abs_of_nonpos $ le_of_lt $ neg_succ_lt_zero _
theorem nat_abs_abs (a : ℤ) : nat_abs (|a|) = nat_abs a :=
by rw [abs_eq_nat_abs]; refl
theorem sign_mul_abs (a : ℤ) : sign a * |a| = a :=
by rw [abs_eq_nat_abs, sign_mul_nat_abs]
theorem coe_nat_eq_zero {n : ℕ} : (n : ℤ) = 0 ↔ n = 0 := nat.cast_eq_zero
theorem coe_nat_ne_zero {n : ℕ} : (n : ℤ) ≠ 0 ↔ n ≠ 0 := by simp
lemma coe_nat_ne_zero_iff_pos {n : ℕ} : (n : ℤ) ≠ 0 ↔ 0 < n :=
⟨λ h, nat.pos_of_ne_zero (coe_nat_ne_zero.1 h),
λ h, (ne_of_lt (coe_nat_lt.2 h)).symm⟩
theorem coe_nat_abs (n : ℕ) : |(n : ℤ)| = n :=
abs_of_nonneg (coe_nat_nonneg n)
/-! ### succ and pred -/
theorem lt_succ_self (a : ℤ) : a < succ a :=
lt_add_of_pos_right _ zero_lt_one
theorem pred_self_lt (a : ℤ) : pred a < a :=
sub_lt_self _ zero_lt_one
theorem lt_add_one_iff {a b : ℤ} : a < b + 1 ↔ a ≤ b :=
add_le_add_iff_right _
lemma le_add_one {a b : ℤ} (h : a ≤ b) : a ≤ b + 1 :=
le_of_lt (int.lt_add_one_iff.mpr h)
theorem sub_one_lt_iff {a b : ℤ} : a - 1 < b ↔ a ≤ b :=
sub_lt_iff_lt_add.trans lt_add_one_iff
theorem le_sub_one_iff {a b : ℤ} : a ≤ b - 1 ↔ a < b :=
le_sub_iff_add_le
@[simp] lemma abs_lt_one_iff {a : ℤ} : |a| < 1 ↔ a = 0 :=
⟨λ a0, let ⟨hn, hp⟩ := abs_lt.mp a0 in (le_of_lt_add_one (by exact hp)).antisymm hn,
λ a0, (abs_eq_zero.mpr a0).le.trans_lt zero_lt_one⟩
lemma abs_le_one_iff {a : ℤ} : |a| ≤ 1 ↔ a = 0 ∨ a = 1 ∨ a = -1 :=
by rw [le_iff_lt_or_eq, abs_lt_one_iff, abs_eq (zero_le_one' ℤ)]
lemma one_le_abs {z : ℤ} (h₀: z ≠ 0) : 1 ≤ |z| :=
add_one_le_iff.mpr (abs_pos.mpr h₀)
/-- Inductively define a function on `ℤ` by defining it at `b`, for the `succ` of a number greater
than `b`, and the `pred` of a number less than `b`. -/
@[elab_as_eliminator]
protected def induction_on' {C : ℤ → Sort*} (z : ℤ) (b : ℤ)
(H0 : C b) (Hs : ∀ k, b ≤ k → C k → C (k + 1)) (Hp : ∀ k ≤ b, C k → C (k - 1)) : C z :=
begin
-- Note that we use `convert` here where possible as we are constructing data, and this reduces
-- the number of times `eq.mpr` appears in the term.
rw ←sub_add_cancel z b,
induction (z - b) with n n,
{ induction n with n ih,
{ convert H0 using 1,
rw [of_nat_zero, zero_add] },
convert Hs _ (le_add_of_nonneg_left (of_nat_nonneg _)) ih using 1,
rw [of_nat_succ, add_assoc, add_comm 1 b, ←add_assoc] },
{ induction n with n ih,
{ convert Hp _ le_rfl H0 using 1,
rw [neg_succ_of_nat_eq, ←of_nat_eq_coe, of_nat_zero, zero_add, neg_add_eq_sub] },
{ convert Hp _ (le_of_lt (add_lt_of_neg_of_le (neg_succ_lt_zero _) le_rfl)) ih using 1,
rw [neg_succ_of_nat_coe', nat.succ_eq_add_one, ←neg_succ_of_nat_coe, sub_add_eq_add_sub] } }
end
/-- See `int.induction_on'` for an induction in both directions. -/
protected lemma le_induction {P : ℤ → Prop} {m : ℤ} (h0 : P m)
(h1 : ∀ (n : ℤ), m ≤ n → P n → P (n + 1)) (n : ℤ) :
m ≤ n → P n :=
begin
apply int.induction_on' n m,
{ intro _, exact h0, },
{ intros k hle hi _, exact h1 k hle (hi hle), },
{ intros _ hle _ hle',
exfalso,
exact lt_irrefl k (le_sub_one_iff.mp (hle.trans hle')), },
end
/-- See `int.induction_on'` for an induction in both directions. -/
protected lemma le_induction_down {P : ℤ → Prop} {m : ℤ} (h0 : P m)
(h1 : ∀ (n : ℤ), n ≤ m → P n → P (n - 1)) (n : ℤ) :
n ≤ m → P n :=
begin
apply int.induction_on' n m,
{ intro _, exact h0, },
{ intros _ hle _ hle',
exfalso,
exact lt_irrefl k (add_one_le_iff.mp (hle'.trans hle)), },
{ intros k hle hi _,
exact h1 k hle (hi hle), },
end
/-! ### nat abs -/
variables {a b : ℤ} {n : ℕ}
attribute [simp] nat_abs nat_abs_of_nat nat_abs_zero nat_abs_one
@[simp] lemma nat_abs_dvd_iff_dvd {a b : ℤ} : a.nat_abs ∣ b.nat_abs ↔ a ∣ b :=
begin
refine ⟨_, λ ⟨k, hk⟩, ⟨k.nat_abs, hk.symm ▸ nat_abs_mul a k⟩⟩,
rintro ⟨k, hk⟩,
rw [←nat_abs_of_nat k, ←nat_abs_mul, nat_abs_eq_nat_abs_iff, neg_mul_eq_mul_neg] at hk,
cases hk; exact ⟨_, hk⟩
end
/-! ### `/` -/
protected theorem div_nonpos {a b : ℤ} (Ha : 0 ≤ a) (Hb : b ≤ 0) : a / b ≤ 0 :=
nonpos_of_neg_nonneg $ by rw [← int.div_neg]; exact int.div_nonneg Ha (neg_nonneg_of_nonpos Hb)
theorem div_eq_zero_of_lt_abs {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < |b|) : a / b = 0 :=
match b, |b|, abs_eq_nat_abs b, H2 with
| (n : ℕ), ._, rfl, H2 := div_eq_zero_of_lt H1 H2
| -[1+ n], ._, rfl, H2 := neg_injective $ by rw [← int.div_neg]; exact div_eq_zero_of_lt H1 H2
end
protected theorem add_mul_div_right (a b : ℤ) {c : ℤ} (H : c ≠ 0) :
(a + b * c) / c = a / c + b :=
have ∀ {k n : ℕ} {a : ℤ}, (a + n * k.succ) / k.succ = a / k.succ + n, from
λ k n a, match a with
| (m : ℕ) := congr_arg of_nat $ nat.add_mul_div_right _ _ k.succ_pos
| -[1+ m] := show ((n * k.succ:ℕ) - m.succ : ℤ) / k.succ =
n - (m / k.succ + 1 : ℕ), begin
cases lt_or_ge m (n*k.succ) with h h,
{ rw [← int.coe_nat_sub h,
← int.coe_nat_sub ((nat.div_lt_iff_lt_mul k.succ_pos).2 h)],
apply congr_arg of_nat,
rw [mul_comm, nat.mul_sub_div], rwa mul_comm },
{ change (↑(n * nat.succ k) - (m + 1) : ℤ) / ↑(nat.succ k) =
↑n - ((m / nat.succ k : ℕ) + 1),
rw [← sub_sub, ← sub_sub, ← neg_sub (m:ℤ), ← neg_sub _ (n:ℤ),
← int.coe_nat_sub h,
← int.coe_nat_sub ((nat.le_div_iff_mul_le k.succ_pos).2 h),
← neg_succ_of_nat_coe', ← neg_succ_of_nat_coe'],
{ apply congr_arg neg_succ_of_nat,
rw [mul_comm, nat.sub_mul_div], rwa mul_comm } }
end
end,
have ∀ {a b c : ℤ}, 0 < c → (a + b * c) / c = a / c + b, from
λ a b c H, match c, eq_succ_of_zero_lt H, b with
| ._, ⟨k, rfl⟩, (n : ℕ) := this
| ._, ⟨k, rfl⟩, -[1+ n] :=
show (a - n.succ * k.succ) / k.succ = (a / k.succ) - n.succ, from
eq_sub_of_add_eq $ by rw [← this, sub_add_cancel]
end,
match lt_trichotomy c 0 with
| or.inl hlt := neg_inj.1 $ by rw [← int.div_neg, neg_add, ← int.div_neg, ← neg_mul_neg];
apply this (neg_pos_of_neg hlt)
| or.inr (or.inl heq) := absurd heq H
| or.inr (or.inr hgt) := this hgt
end
protected theorem add_mul_div_left (a : ℤ) {b : ℤ} (c : ℤ) (H : b ≠ 0) :
(a + b * c) / b = a / b + c :=
by rw [mul_comm, int.add_mul_div_right _ _ H]
@[simp] protected theorem mul_div_cancel (a : ℤ) {b : ℤ} (H : b ≠ 0) : a * b / b = a :=
by have := int.add_mul_div_right 0 a H;
rwa [zero_add, int.zero_div, zero_add] at this
@[simp] protected theorem mul_div_cancel_left {a : ℤ} (b : ℤ) (H : a ≠ 0) : a * b / a = b :=
by rw [mul_comm, int.mul_div_cancel _ H]
@[simp] protected theorem div_self {a : ℤ} (H : a ≠ 0) : a / a = 1 :=
by have := int.mul_div_cancel 1 H; rwa one_mul at this
local attribute [simp] int.zero_div int.div_zero
protected theorem add_div_of_dvd_right {a b c : ℤ} (H : c ∣ b) :
(a + b) / c = a / c + b / c :=
begin
by_cases h1 : c = 0,
{ simp [h1] },
cases H with k hk,
rw hk,
change c ≠ 0 at h1,
rw [mul_comm c k, int.add_mul_div_right _ _ h1, ←zero_add (k * c), int.add_mul_div_right _ _ h1,
int.zero_div, zero_add]
end
protected theorem add_div_of_dvd_left {a b c : ℤ} (H : c ∣ a) :
(a + b) / c = a / c + b / c :=
by rw [add_comm, int.add_div_of_dvd_right H, add_comm]
/-! ### mod -/
@[simp] theorem mod_abs (a b : ℤ) : a % (|b|) = a % b :=
abs_by_cases (λ i, a % i = a % b) rfl (mod_neg _ _)
theorem mod_nonneg : ∀ (a : ℤ) {b : ℤ}, b ≠ 0 → 0 ≤ a % b
| (m : ℕ) n H := coe_zero_le _
| -[1+ m] n H :=
sub_nonneg_of_le $ coe_nat_le_coe_nat_of_le $ nat.mod_lt _ (nat_abs_pos_of_ne_zero H)
theorem mod_lt_of_pos (a : ℤ) {b : ℤ} (H : 0 < b) : a % b < b :=
match a, b, eq_succ_of_zero_lt H with
| (m : ℕ), ._, ⟨n, rfl⟩ := coe_nat_lt_coe_nat_of_lt (nat.mod_lt _ (nat.succ_pos _))
| -[1+ m], ._, ⟨n, rfl⟩ := sub_lt_self _ (coe_nat_lt_coe_nat_of_lt $ nat.succ_pos _)
end
theorem mod_lt (a : ℤ) {b : ℤ} (H : b ≠ 0) : a % b < |b| :=
by rw [← mod_abs]; exact mod_lt_of_pos _ (abs_pos.2 H)
@[simp] theorem add_mul_mod_self {a b c : ℤ} : (a + b * c) % c = a % c :=
if cz : c = 0 then by rw [cz, mul_zero, add_zero] else
by rw [mod_def, mod_def, int.add_mul_div_right _ _ cz,
mul_add, mul_comm, add_sub_add_right_eq_sub]
@[simp] theorem add_mul_mod_self_left (a b c : ℤ) : (a + b * c) % b = a % b :=
by rw [mul_comm, add_mul_mod_self]
@[simp] theorem add_mod_self {a b : ℤ} : (a + b) % b = a % b :=
by have := add_mul_mod_self_left a b 1; rwa mul_one at this
@[simp] theorem add_mod_self_left {a b : ℤ} : (a + b) % a = b % a :=
by rw [add_comm, add_mod_self]
@[simp] theorem mod_add_mod (m n k : ℤ) : (m % n + k) % n = (m + k) % n :=
by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm;
rwa [add_right_comm, mod_add_div] at this
@[simp] theorem add_mod_mod (m n k : ℤ) : (m + n % k) % k = (m + n) % k :=
by rw [add_comm, mod_add_mod, add_comm]
lemma add_mod (a b n : ℤ) : (a + b) % n = ((a % n) + (b % n)) % n :=
by rw [add_mod_mod, mod_add_mod]
theorem add_mod_eq_add_mod_right {m n k : ℤ} (i : ℤ) (H : m % n = k % n) :
(m + i) % n = (k + i) % n :=
by rw [← mod_add_mod, ← mod_add_mod k, H]
theorem add_mod_eq_add_mod_left {m n k : ℤ} (i : ℤ) (H : m % n = k % n) :
(i + m) % n = (i + k) % n :=
by rw [add_comm, add_mod_eq_add_mod_right _ H, add_comm]
theorem mod_add_cancel_right {m n k : ℤ} (i) : (m + i) % n = (k + i) % n ↔
m % n = k % n :=
⟨λ H, by have := add_mod_eq_add_mod_right (-i) H;
rwa [add_neg_cancel_right, add_neg_cancel_right] at this,
add_mod_eq_add_mod_right _⟩
theorem mod_add_cancel_left {m n k i : ℤ} :
(i + m) % n = (i + k) % n ↔ m % n = k % n :=
by rw [add_comm, add_comm i, mod_add_cancel_right]
theorem mod_sub_cancel_right {m n k : ℤ} (i) : (m - i) % n = (k - i) % n ↔
m % n = k % n :=
mod_add_cancel_right _
@[simp] theorem mul_mod_left (a b : ℤ) : (a * b) % b = 0 :=
by rw [← zero_add (a * b), add_mul_mod_self, zero_mod]
@[simp] theorem mul_mod_right (a b : ℤ) : (a * b) % a = 0 :=
by rw [mul_comm, mul_mod_left]
lemma mul_mod (a b n : ℤ) : (a * b) % n = ((a % n) * (b % n)) % n :=
begin
conv_lhs
{ rw [←mod_add_div a n, ←mod_add_div' b n, right_distrib, left_distrib, left_distrib,
mul_assoc, mul_assoc, ←left_distrib n _ _, add_mul_mod_self_left, ← mul_assoc,
add_mul_mod_self] }
end
local attribute [simp] -- Will be generalized to Euclidean domains.
theorem mod_self {a : ℤ} : a % a = 0 :=
by have := mul_mod_left 1 a; rwa one_mul at this
@[simp] theorem mod_mod_of_dvd (n : ℤ) {m k : ℤ} (h : m ∣ k) : n % k % m = n % m :=
begin
conv { to_rhs, rw ←mod_add_div n k },
rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left]
end
@[simp] theorem mod_mod (a b : ℤ) : a % b % b = a % b :=
by conv {to_rhs, rw [← mod_add_div a b, add_mul_mod_self_left]}
lemma sub_mod (a b n : ℤ) : (a - b) % n = ((a % n) - (b % n)) % n :=
begin
apply (mod_add_cancel_right b).mp,
rw [sub_add_cancel, ← add_mod_mod, sub_add_cancel, mod_mod]
end
protected theorem div_mod_unique {a b r q : ℤ} (h : 0 < b) :
a / b = q ∧ a % b = r ↔ r + b * q = a ∧ 0 ≤ r ∧ r < b :=
begin
split,
{ rintro ⟨rfl, rfl⟩,
exact ⟨mod_add_div a b, mod_nonneg _ h.ne.symm, mod_lt_of_pos _ h⟩, },
{ rintro ⟨rfl, hz, hb⟩,
split,
{ rw [int.add_mul_div_left r q (ne_of_gt h), div_eq_zero_of_lt hz hb],
simp, },
{ rw [add_mul_mod_self_left, mod_eq_of_lt hz hb] } },
end
local attribute [simp] int.zero_mod
theorem mod_eq_mod_iff_mod_sub_eq_zero {m n k : ℤ} : m % n = k % n ↔ (m - k) % n = 0 :=
(mod_sub_cancel_right k).symm.trans $ by simp
@[simp] lemma neg_mod_two (i : ℤ) : (-i) % 2 = i % 2 :=
begin
apply int.mod_eq_mod_iff_mod_sub_eq_zero.mpr,
convert int.mul_mod_right 2 (-i),
simp only [two_mul, sub_eq_add_neg]
end
/-! ### properties of `/` and `%` -/
theorem lt_div_add_one_mul_self (a : ℤ) {b : ℤ} (H : 0 < b) : a < (a / b + 1) * b :=
by { rw [add_mul, one_mul, mul_comm, ← sub_lt_iff_lt_add', ← mod_def],
exact mod_lt_of_pos _ H }
theorem abs_div_le_abs : ∀ (a b : ℤ), |a / b| ≤ |a| :=
suffices ∀ (a : ℤ) (n : ℕ), |a / n| ≤ |a|, from
λ a b, match b, eq_coe_or_neg b with
| ._, ⟨n, or.inl rfl⟩ := this _ _
| ._, ⟨n, or.inr rfl⟩ := by rw [int.div_neg, abs_neg]; apply this
end,
λ a n, by rw [abs_eq_nat_abs, abs_eq_nat_abs]; exact
coe_nat_le_coe_nat_of_le (match a, n with
| (m : ℕ), n := nat.div_le_self _ _
| -[1+ m], 0 := nat.zero_le _
| -[1+ m], n+1 := nat.succ_le_succ (nat.div_le_self _ _)
end)
theorem div_le_self {a : ℤ} (b : ℤ) (Ha : 0 ≤ a) : a / b ≤ a :=
by have := le_trans (le_abs_self _) (abs_div_le_abs a b);
rwa [abs_of_nonneg Ha] at this
lemma mod_two_eq_zero_or_one (n : ℤ) : n % 2 = 0 ∨ n % 2 = 1 :=
have h : n % 2 < 2 := abs_of_nonneg (show 0 ≤ (2 : ℤ), from dec_trivial) ▸ int.mod_lt _ dec_trivial,
have h₁ : 0 ≤ n % 2 := int.mod_nonneg _ dec_trivial,
match (n % 2), h, h₁ with
| (0 : ℕ) := λ _ _, or.inl rfl
| (1 : ℕ) := λ _ _, or.inr rfl
| (k + 2 : ℕ) := λ h _, absurd h dec_trivial
| -[1+ a] := λ _ h₁, absurd h₁ dec_trivial
end
/-! ### dvd -/
theorem dvd_of_mod_eq_zero {a b : ℤ} (H : b % a = 0) : a ∣ b :=
⟨b / a, (mul_div_cancel_of_mod_eq_zero H).symm⟩
theorem mod_eq_zero_of_dvd : ∀ {a b : ℤ}, a ∣ b → b % a = 0
| a ._ ⟨c, rfl⟩ := mul_mod_right _ _
theorem dvd_iff_mod_eq_zero (a b : ℤ) : a ∣ b ↔ b % a = 0 :=
⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩
/-- If `a % b = c` then `b` divides `a - c`. -/
lemma dvd_sub_of_mod_eq {a b c : ℤ} (h : a % b = c) : b ∣ a - c :=
begin
have hx : a % b % b = c % b, { rw h },
rw [mod_mod, ←mod_sub_cancel_right c, sub_self, zero_mod] at hx,
exact dvd_of_mod_eq_zero hx
end
theorem nat_abs_dvd {a b : ℤ} : (a.nat_abs : ℤ) ∣ b ↔ a ∣ b :=
(nat_abs_eq a).elim (λ e, by rw ← e) (λ e, by rw [← neg_dvd, ← e])
theorem dvd_nat_abs {a b : ℤ} : a ∣ b.nat_abs ↔ a ∣ b :=
(nat_abs_eq b).elim (λ e, by rw ← e) (λ e, by rw [← dvd_neg, ← e])
instance decidable_dvd : @decidable_rel ℤ (∣) :=
assume a n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm
protected theorem div_mul_cancel {a b : ℤ} (H : b ∣ a) : a / b * b = a :=
div_mul_cancel_of_mod_eq_zero (mod_eq_zero_of_dvd H)
protected theorem mul_div_cancel' {a b : ℤ} (H : a ∣ b) : a * (b / a) = b :=
by rw [mul_comm, int.div_mul_cancel H]
theorem div_dvd_div : ∀ {a b c : ℤ} (H1 : a ∣ b) (H2 : b ∣ c), b / a ∣ c / a
| a ._ ._ ⟨b, rfl⟩ ⟨c, rfl⟩ := if az : a = 0 then by simp [az] else
by rw [int.mul_div_cancel_left _ az, mul_assoc, int.mul_div_cancel_left _ az];
apply dvd_mul_right
protected theorem eq_mul_of_div_eq_right {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) :
a = b * c :=
by rw [← H2, int.mul_div_cancel' H1]
protected theorem div_eq_of_eq_mul_right {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = b * c) :
a / b = c :=
by rw [H2, int.mul_div_cancel_left _ H1]
protected theorem eq_div_of_mul_eq_right {a b c : ℤ} (H1 : a ≠ 0) (H2 : a * b = c) :
b = c / a :=
eq.symm $ int.div_eq_of_eq_mul_right H1 H2.symm
protected theorem div_eq_iff_eq_mul_right {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) :
a / b = c ↔ a = b * c :=
⟨int.eq_mul_of_div_eq_right H', int.div_eq_of_eq_mul_right H⟩
protected theorem div_eq_iff_eq_mul_left {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) :
a / b = c ↔ a = c * b :=
by rw mul_comm; exact int.div_eq_iff_eq_mul_right H H'
protected theorem eq_mul_of_div_eq_left {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) :
a = c * b :=
by rw [mul_comm, int.eq_mul_of_div_eq_right H1 H2]
protected theorem div_eq_of_eq_mul_left {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = c * b) :
a / b = c :=
int.div_eq_of_eq_mul_right H1 (by rw [mul_comm, H2])
protected lemma eq_zero_of_div_eq_zero {d n : ℤ} (h : d ∣ n) (H : n / d = 0) : n = 0 :=
by rw [← int.mul_div_cancel' h, H, mul_zero]
@[simp]
protected lemma div_left_inj {a b d : ℤ} (hda : d ∣ a) (hdb : d ∣ b) : a / d = b / d ↔ a = b :=
begin
refine ⟨λ h, _, congr_arg _⟩,
rw [←int.mul_div_cancel' hda, ←int.mul_div_cancel' hdb, h],
end
lemma abs_sign_of_nonzero {z : ℤ} (hz : z ≠ 0) : |z.sign| = 1 :=
by rw [abs_eq_nat_abs, nat_abs_sign_of_nonzero hz, int.coe_nat_one]
/-- If `n > 0` then `m` is not divisible by `n` iff it is between `n * k` and `n * (k + 1)`
for some `k`. -/
lemma exists_lt_and_lt_iff_not_dvd (m : ℤ) {n : ℤ} (hn : 0 < n) :
(∃ k, n * k < m ∧ m < n * (k + 1)) ↔ ¬ n ∣ m :=
begin
split,
{ rintro ⟨k, h1k, h2k⟩ ⟨l, rfl⟩, rw [mul_lt_mul_left hn] at h1k h2k,
rw [lt_add_one_iff, ← not_lt] at h2k, exact h2k h1k },
{ intro h, rw [dvd_iff_mod_eq_zero, ← ne.def] at h,
have := (mod_nonneg m hn.ne.symm).lt_of_ne h.symm,
simp only [← mod_add_div m n] {single_pass := tt},
refine ⟨m / n, lt_add_of_pos_left _ this, _⟩,
rw [add_comm _ (1 : ℤ), left_distrib, mul_one], exact add_lt_add_right (mod_lt_of_pos _ hn) _ }
end
local attribute [simp] int.div_zero
protected theorem mul_div_assoc (a : ℤ) : ∀ {b c : ℤ}, c ∣ b → (a * b) / c = a * (b / c)
| ._ c ⟨d, rfl⟩ := if cz : c = 0 then by simp [cz] else
by rw [mul_left_comm, int.mul_div_cancel_left _ cz, int.mul_div_cancel_left _ cz]
protected theorem mul_div_assoc' (b : ℤ) {a c : ℤ} (h : c ∣ a) : a * b / c = a / c * b :=
by rw [mul_comm, int.mul_div_assoc _ h, mul_comm]
theorem neg_div_of_dvd : ∀ {a b : ℤ} (H : b ∣ a), -a / b = -(a / b)
| ._ b ⟨c, rfl⟩ := if bz : b = 0 then by simp [bz] else
by rw [neg_mul_eq_mul_neg, int.mul_div_cancel_left _ bz, int.mul_div_cancel_left _ bz]
lemma sub_div_of_dvd (a : ℤ) {b c : ℤ} (hcb : c ∣ b) : (a - b) / c = a / c - b / c :=
begin
rw [sub_eq_add_neg, sub_eq_add_neg, int.add_div_of_dvd_right ((dvd_neg c b).mpr hcb)],
congr,
exact neg_div_of_dvd hcb,
end
lemma sub_div_of_dvd_sub {a b c : ℤ} (hcab : c ∣ (a - b)) : (a - b) / c = a / c - b / c :=
by rw [eq_sub_iff_add_eq, ← int.add_div_of_dvd_left hcab, sub_add_cancel]
protected theorem sign_eq_div_abs (a : ℤ) : sign a = a / |a| :=
if az : a = 0 then by simp [az] else
(int.div_eq_of_eq_mul_left (mt abs_eq_zero.1 az)
(sign_mul_abs _).symm).symm
/-! ### `/` and ordering -/
protected theorem div_mul_le (a : ℤ) {b : ℤ} (H : b ≠ 0) : a / b * b ≤ a :=
le_of_sub_nonneg $ by rw [mul_comm, ← mod_def]; apply mod_nonneg _ H
protected theorem div_le_of_le_mul {a b c : ℤ} (H : 0 < c) (H' : a ≤ b * c) : a / c ≤ b :=
le_of_mul_le_mul_right (le_trans (int.div_mul_le _ (ne_of_gt H)) H') H
protected theorem mul_lt_of_lt_div {a b c : ℤ} (H : 0 < c) (H3 : a < b / c) : a * c < b :=
lt_of_not_ge $ mt (int.div_le_of_le_mul H) (not_le_of_gt H3)
protected theorem mul_le_of_le_div {a b c : ℤ} (H1 : 0 < c) (H2 : a ≤ b / c) : a * c ≤ b :=
le_trans (mul_le_mul_of_nonneg_right H2 (le_of_lt H1)) (int.div_mul_le _ (ne_of_gt H1))
protected theorem le_div_of_mul_le {a b c : ℤ} (H1 : 0 < c) (H2 : a * c ≤ b) : a ≤ b / c :=
le_of_lt_add_one $ lt_of_mul_lt_mul_right
(lt_of_le_of_lt H2 (lt_div_add_one_mul_self _ H1)) (le_of_lt H1)
protected theorem le_div_iff_mul_le {a b c : ℤ} (H : 0 < c) : a ≤ b / c ↔ a * c ≤ b :=
⟨int.mul_le_of_le_div H, int.le_div_of_mul_le H⟩
protected theorem div_le_div {a b c : ℤ} (H : 0 < c) (H' : a ≤ b) : a / c ≤ b / c :=
int.le_div_of_mul_le H (le_trans (int.div_mul_le _ (ne_of_gt H)) H')
protected theorem div_lt_of_lt_mul {a b c : ℤ} (H : 0 < c) (H' : a < b * c) : a / c < b :=
lt_of_not_ge $ mt (int.mul_le_of_le_div H) (not_le_of_gt H')
protected theorem lt_mul_of_div_lt {a b c : ℤ} (H1 : 0 < c) (H2 : a / c < b) : a < b * c :=
lt_of_not_ge $ mt (int.le_div_of_mul_le H1) (not_le_of_gt H2)
protected theorem div_lt_iff_lt_mul {a b c : ℤ} (H : 0 < c) : a / c < b ↔ a < b * c :=
⟨int.lt_mul_of_div_lt H, int.div_lt_of_lt_mul H⟩
protected theorem le_mul_of_div_le {a b c : ℤ} (H1 : 0 ≤ b) (H2 : b ∣ a) (H3 : a / b ≤ c) :
a ≤ c * b :=
by rw [← int.div_mul_cancel H2]; exact mul_le_mul_of_nonneg_right H3 H1
protected theorem lt_div_of_mul_lt {a b c : ℤ} (H1 : 0 ≤ b) (H2 : b ∣ c) (H3 : a * b < c) :
a < c / b :=
lt_of_not_ge $ mt (int.le_mul_of_div_le H1 H2) (not_le_of_gt H3)
protected theorem lt_div_iff_mul_lt {a b : ℤ} (c : ℤ) (H : 0 < c) (H' : c ∣ b) :
a < b / c ↔ a * c < b :=
⟨int.mul_lt_of_lt_div H, int.lt_div_of_mul_lt (le_of_lt H) H'⟩
theorem div_pos_of_pos_of_dvd {a b : ℤ} (H1 : 0 < a) (H2 : 0 ≤ b) (H3 : b ∣ a) : 0 < a / b :=
int.lt_div_of_mul_lt H2 H3 (by rwa zero_mul)
lemma nat_abs_eq_of_dvd_dvd {s t : ℤ} (hst : s ∣ t) (hts : t ∣ s) : nat_abs s = nat_abs t :=
nat.dvd_antisymm (nat_abs_dvd_iff_dvd.mpr hst) (nat_abs_dvd_iff_dvd.mpr hts)
theorem div_eq_div_of_mul_eq_mul {a b c d : ℤ} (H2 : d ∣ c) (H3 : b ≠ 0)
(H4 : d ≠ 0) (H5 : a * d = b * c) :
a / b = c / d :=
int.div_eq_of_eq_mul_right H3 $
by rw [← int.mul_div_assoc _ H2]; exact
(int.div_eq_of_eq_mul_left H4 H5.symm).symm
lemma div_dvd_of_dvd {s t : ℤ} (hst : s ∣ t) : (t / s) ∣ t :=
begin
rcases eq_or_ne s 0 with rfl | hs,
{ simpa using hst },
rcases hst with ⟨c, hc⟩,
simp [hc, int.mul_div_cancel_left _ hs],
end
/-! ### to_nat -/
@[simp] theorem to_nat_le {a : ℤ} {n : ℕ} : to_nat a ≤ n ↔ a ≤ n :=
by rw [(coe_nat_le_coe_nat_iff _ _).symm, to_nat_eq_max, max_le_iff];
exact and_iff_left (coe_zero_le _)
@[simp] theorem lt_to_nat {n : ℕ} {a : ℤ} : n < to_nat a ↔ (n : ℤ) < a :=
le_iff_le_iff_lt_iff_lt.1 to_nat_le
@[simp]
lemma coe_nat_nonpos_iff {n : ℕ} : (n : ℤ) ≤ 0 ↔ n = 0 :=
⟨ λ h, le_antisymm (int.coe_nat_le.mp (h.trans int.coe_nat_zero.le)) n.zero_le,
λ h, (coe_nat_eq_zero.mpr h).le⟩
theorem to_nat_le_to_nat {a b : ℤ} (h : a ≤ b) : to_nat a ≤ to_nat b :=
by rw to_nat_le; exact le_trans h (le_to_nat b)
theorem to_nat_lt_to_nat {a b : ℤ} (hb : 0 < b) : to_nat a < to_nat b ↔ a < b :=
⟨λ h, begin cases a, exact lt_to_nat.1 h, exact lt_trans (neg_succ_of_nat_lt_zero a) hb, end,
λ h, begin rw lt_to_nat, cases a, exact h, exact hb end⟩
theorem lt_of_to_nat_lt {a b : ℤ} (h : to_nat a < to_nat b) : a < b :=
(to_nat_lt_to_nat $ lt_to_nat.1 $ lt_of_le_of_lt (nat.zero_le _) h).1 h
@[simp]
lemma to_nat_pred_coe_of_pos {i : ℤ} (h : 0 < i) : ((i.to_nat - 1 : ℕ) : ℤ) = i - 1 :=
by simp [h, le_of_lt h] with push_cast
@[simp]
lemma to_nat_eq_zero : ∀ {n : ℤ}, n.to_nat = 0 ↔ n ≤ 0
| (n : ℕ) := calc _ ↔ (n = 0) : ⟨(to_nat_coe_nat n).symm.trans, (to_nat_coe_nat n).trans⟩
... ↔ _ : coe_nat_nonpos_iff.symm
| -[1+ n] := show ((-((n : ℤ) + 1)).to_nat = 0) ↔ (-(n + 1) : ℤ) ≤ 0, from
calc _ ↔ true : ⟨λ _, trivial, λ h, to_nat_neg_nat _⟩
... ↔ _ : ⟨λ h, neg_nonpos_of_nonneg (coe_zero_le _), λ _, trivial⟩
@[simp] lemma to_nat_sub_of_le {a b : ℤ} (h : b ≤ a) : (to_nat (a - b) : ℤ) = a - b :=
int.to_nat_of_nonneg (sub_nonneg_of_le h)
end int
-- We should need only a minimal development of sets in order to get here.
assert_not_exists set.range
|
2456c5e636b3d929aa0ccad3df20d944861bdc3a | e0b0b1648286e442507eb62344760d5cd8d13f2d | /tests/lean/interactive/haveInfo.lean | 8b55c3af90602dcec1f655e83a73c98df5430132 | [
"Apache-2.0"
] | permissive | MULXCODE/lean4 | 743ed389e05e26e09c6a11d24607ad5a697db39b | 4675817a9e89824eca37192364cd47a4027c6437 | refs/heads/master | 1,682,231,879,857 | 1,620,423,501,000 | 1,620,423,501,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 362 | lean | example : False := by
have True by
skip
--^ $/lean/plainGoal
skip
admit
example : False := by
have True by
--^ $/lean/plainGoal
skip
skip
admit
example : False := by
have True by
--^ $/lean/plainGoal
skip
skip
admit
example : False := by
have True by
skip
--^ $/lean/plainGoal
skip
admit
|
a9b4f6d9ec70c45a4bcb8c7db0979d20f1335cbe | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/limits/creates.lean | 95f3488e9cddc4e204bff309ed7c62496f39e184 | [] | 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 | 27,502 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.limits.preserves.basic
import Mathlib.PostPort
universes v u₁ u₂ l u₃
namespace Mathlib
namespace category_theory
/--
Define the lift of a cone: For a cone `c` for `K ⋙ F`, give a cone for `K`
which is a lift of `c`, i.e. the image of it under `F` is (iso) to `c`.
We will then use this as part of the definition of creation of limits:
every limit cone has a lift.
Note this definition is really only useful when `c` is a limit already.
-/
structure liftable_cone {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) (c : limits.cone (K ⋙ F))
where
lifted_cone : limits.cone K
valid_lift : functor.map_cone F lifted_cone ≅ c
/--
Define the lift of a cocone: For a cocone `c` for `K ⋙ F`, give a cocone for
`K` which is a lift of `c`, i.e. the image of it under `F` is (iso) to `c`.
We will then use this as part of the definition of creation of colimits:
every limit cocone has a lift.
Note this definition is really only useful when `c` is a colimit already.
-/
structure liftable_cocone {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) (c : limits.cocone (K ⋙ F))
where
lifted_cocone : limits.cocone K
valid_lift : functor.map_cocone F lifted_cocone ≅ c
/--
Definition 3.3.1 of [Riehl].
We say that `F` creates limits of `K` if, given any limit cone `c` for `K ⋙ F`
(i.e. below) we can lift it to a cone "above", and further that `F` reflects
limits for `K`.
If `F` reflects isomorphisms, it suffices to show only that the lifted cone is
a limit - see `creates_limit_of_reflects_iso`.
-/
class creates_limit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D)
extends limits.reflects_limit K F
where
lifts : (c : limits.cone (K ⋙ F)) → limits.is_limit c → liftable_cone K F c
/--
`F` creates limits of shape `J` if `F` creates the limit of any diagram
`K : J ⥤ C`.
-/
class creates_limits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] (J : Type v) [small_category J] (F : C ⥤ D)
where
creates_limit : {K : J ⥤ C} → creates_limit K F
/-- `F` creates limits if it creates limits of shape `J` for any small `J`. -/
class creates_limits {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D)
where
creates_limits_of_shape : {J : Type v} → {𝒥 : small_category J} → creates_limits_of_shape J F
/--
Dual of definition 3.3.1 of [Riehl].
We say that `F` creates colimits of `K` if, given any limit cocone `c` for
`K ⋙ F` (i.e. below) we can lift it to a cocone "above", and further that `F`
reflects limits for `K`.
If `F` reflects isomorphisms, it suffices to show only that the lifted cocone is
a limit - see `creates_limit_of_reflects_iso`.
-/
class creates_colimit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D)
extends limits.reflects_colimit K F
where
lifts : (c : limits.cocone (K ⋙ F)) → limits.is_colimit c → liftable_cocone K F c
/--
`F` creates colimits of shape `J` if `F` creates the colimit of any diagram
`K : J ⥤ C`.
-/
class creates_colimits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] (J : Type v) [small_category J] (F : C ⥤ D)
where
creates_colimit : {K : J ⥤ C} → creates_colimit K F
/-- `F` creates colimits if it creates colimits of shape `J` for any small `J`. -/
class creates_colimits {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D)
where
creates_colimits_of_shape : {J : Type v} → {𝒥 : small_category J} → creates_colimits_of_shape J F
/- Interface to the `creates_limit` class. -/
/-- `lift_limit t` is the cone for `K` given by lifting the limit `t` for `K ⋙ F`. -/
def lift_limit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {F : C ⥤ D} [creates_limit K F] {c : limits.cone (K ⋙ F)} (t : limits.is_limit c) : limits.cone K :=
liftable_cone.lifted_cone (creates_limit.lifts c t)
/-- The lifted cone has an image isomorphic to the original cone. -/
def lifted_limit_maps_to_original {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {F : C ⥤ D} [creates_limit K F] {c : limits.cone (K ⋙ F)} (t : limits.is_limit c) : functor.map_cone F (lift_limit t) ≅ c :=
liftable_cone.valid_lift (creates_limit.lifts c t)
/-- The lifted cone is a limit. -/
def lifted_limit_is_limit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {F : C ⥤ D} [creates_limit K F] {c : limits.cone (K ⋙ F)} (t : limits.is_limit c) : limits.is_limit (lift_limit t) :=
limits.reflects_limit.reflects (limits.is_limit.of_iso_limit t (iso.symm (lifted_limit_maps_to_original t)))
/-- If `F` creates the limit of `K` and `K ⋙ F` has a limit, then `K` has a limit. -/
theorem has_limit_of_created {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) [limits.has_limit (K ⋙ F)] [creates_limit K F] : limits.has_limit K :=
limits.has_limit.mk
(limits.limit_cone.mk (lift_limit (limits.limit.is_limit (K ⋙ F)))
(lifted_limit_is_limit (limits.limit.is_limit (K ⋙ F))))
/--
If `F` creates limits of shape `J`, and `D` has limits of shape `J`, then
`C` has limits of shape `J`.
-/
theorem has_limits_of_shape_of_has_limits_of_shape_creates_limits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (F : C ⥤ D) [limits.has_limits_of_shape J D] [creates_limits_of_shape J F] : limits.has_limits_of_shape J C :=
limits.has_limits_of_shape.mk fun (G : J ⥤ C) => has_limit_of_created G F
/-- If `F` creates limits, and `D` has all limits, then `C` has all limits. -/
theorem has_limits_of_has_limits_creates_limits {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) [limits.has_limits D] [creates_limits F] : limits.has_limits C :=
limits.has_limits.mk
fun (J : Type v) (I : small_category J) => has_limits_of_shape_of_has_limits_of_shape_creates_limits_of_shape F
/- Interface to the `creates_colimit` class. -/
/-- `lift_colimit t` is the cocone for `K` given by lifting the colimit `t` for `K ⋙ F`. -/
def lift_colimit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {F : C ⥤ D} [creates_colimit K F] {c : limits.cocone (K ⋙ F)} (t : limits.is_colimit c) : limits.cocone K :=
liftable_cocone.lifted_cocone (creates_colimit.lifts c t)
/-- The lifted cocone has an image isomorphic to the original cocone. -/
def lifted_colimit_maps_to_original {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {F : C ⥤ D} [creates_colimit K F] {c : limits.cocone (K ⋙ F)} (t : limits.is_colimit c) : functor.map_cocone F (lift_colimit t) ≅ c :=
liftable_cocone.valid_lift (creates_colimit.lifts c t)
/-- The lifted cocone is a colimit. -/
def lifted_colimit_is_colimit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {F : C ⥤ D} [creates_colimit K F] {c : limits.cocone (K ⋙ F)} (t : limits.is_colimit c) : limits.is_colimit (lift_colimit t) :=
limits.reflects_colimit.reflects (limits.is_colimit.of_iso_colimit t (iso.symm (lifted_colimit_maps_to_original t)))
/-- If `F` creates the limit of `K` and `K ⋙ F` has a limit, then `K` has a limit. -/
theorem has_colimit_of_created {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) [limits.has_colimit (K ⋙ F)] [creates_colimit K F] : limits.has_colimit K :=
limits.has_colimit.mk
(limits.colimit_cocone.mk (lift_colimit (limits.colimit.is_colimit (K ⋙ F)))
(lifted_colimit_is_colimit (limits.colimit.is_colimit (K ⋙ F))))
/--
If `F` creates colimits of shape `J`, and `D` has colimits of shape `J`, then
`C` has colimits of shape `J`.
-/
theorem has_colimits_of_shape_of_has_colimits_of_shape_creates_colimits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (F : C ⥤ D) [limits.has_colimits_of_shape J D] [creates_colimits_of_shape J F] : limits.has_colimits_of_shape J C :=
limits.has_colimits_of_shape.mk fun (G : J ⥤ C) => has_colimit_of_created G F
/-- If `F` creates colimits, and `D` has all colimits, then `C` has all colimits. -/
theorem has_colimits_of_has_colimits_creates_colimits {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) [limits.has_colimits D] [creates_colimits F] : limits.has_colimits C :=
limits.has_colimits.mk
fun (J : Type v) (I : small_category J) => has_colimits_of_shape_of_has_colimits_of_shape_creates_colimits_of_shape F
/--
A helper to show a functor creates limits. In particular, if we can show
that for any limit cone `c` for `K ⋙ F`, there is a lift of it which is
a limit and `F` reflects isomorphisms, then `F` creates limits.
Usually, `F` creating limits says that _any_ lift of `c` is a limit, but
here we only need to show that our particular lift of `c` is a limit.
-/
structure lifts_to_limit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) (c : limits.cone (K ⋙ F)) (t : limits.is_limit c)
extends liftable_cone K F c
where
makes_limit : limits.is_limit (liftable_cone.lifted_cone _to_liftable_cone)
/--
A helper to show a functor creates colimits. In particular, if we can show
that for any limit cocone `c` for `K ⋙ F`, there is a lift of it which is
a limit and `F` reflects isomorphisms, then `F` creates colimits.
Usually, `F` creating colimits says that _any_ lift of `c` is a colimit, but
here we only need to show that our particular lift of `c` is a colimit.
-/
structure lifts_to_colimit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) (c : limits.cocone (K ⋙ F)) (t : limits.is_colimit c)
extends liftable_cocone K F c
where
makes_colimit : limits.is_colimit (liftable_cocone.lifted_cocone _to_liftable_cocone)
/--
If `F` reflects isomorphisms and we can lift any limit cone to a limit cone,
then `F` creates limits.
In particular here we don't need to assume that F reflects limits.
-/
def creates_limit_of_reflects_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {F : C ⥤ D} [reflects_isomorphisms F] (h : (c : limits.cone (K ⋙ F)) → (t : limits.is_limit c) → lifts_to_limit K F c t) : creates_limit K F :=
creates_limit.mk fun (c : limits.cone (K ⋙ F)) (t : limits.is_limit c) => lifts_to_limit.to_liftable_cone (h c t)
/--
When `F` is fully faithful, and `has_limit (K ⋙ F)`, to show that `F` creates the limit for `K`
it suffices to exhibit a lift of the chosen limit cone for `K ⋙ F`.
-/
-- Notice however that even if the isomorphism is `iso.refl _`,
-- this construction will insert additional identity morphisms in the cone maps,
-- so the constructed limits may not be ideal, definitionally.
def creates_limit_of_fully_faithful_of_lift {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {F : C ⥤ D} [full F] [faithful F] [limits.has_limit (K ⋙ F)] (c : limits.cone K) (i : functor.map_cone F c ≅ limits.limit.cone (K ⋙ F)) : creates_limit K F :=
creates_limit_of_reflects_iso
fun (c' : limits.cone (K ⋙ F)) (t : limits.is_limit c') =>
lifts_to_limit.mk (liftable_cone.mk c (i ≪≫ limits.is_limit.unique_up_to_iso (limits.limit.is_limit (K ⋙ F)) t))
(limits.is_limit.of_faithful F (limits.is_limit.of_iso_limit (limits.limit.is_limit (K ⋙ F)) (iso.symm i))
(fun (s : limits.cone K) =>
functor.preimage F
(limits.is_limit.lift (limits.is_limit.of_iso_limit (limits.limit.is_limit (K ⋙ F)) (iso.symm i))
(functor.map_cone F s)))
sorry)
/--
When `F` is fully faithful, and `has_limit (K ⋙ F)`, to show that `F` creates the limit for `K`
it suffices to show that the chosen limit point is in the essential image of `F`.
-/
-- Notice however that even if the isomorphism is `iso.refl _`,
-- this construction will insert additional identity morphisms in the cone maps,
-- so the constructed limits may not be ideal, definitionally.
def creates_limit_of_fully_faithful_of_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {F : C ⥤ D} [full F] [faithful F] [limits.has_limit (K ⋙ F)] (X : C) (i : functor.obj F X ≅ limits.limit (K ⋙ F)) : creates_limit K F :=
creates_limit_of_fully_faithful_of_lift
(limits.cone.mk X (nat_trans.mk fun (j : J) => functor.preimage F (iso.hom i ≫ limits.limit.π (K ⋙ F) j)))
(limits.cones.ext i sorry)
/-- `F` preserves the limit of `K` if it creates the limit and `K ⋙ F` has the limit. -/
protected instance preserves_limit_of_creates_limit_and_has_limit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) [creates_limit K F] [limits.has_limit (K ⋙ F)] : limits.preserves_limit K F :=
limits.preserves_limit.mk
fun (c : limits.cone K) (t : limits.is_limit c) =>
limits.is_limit.of_iso_limit (limits.limit.is_limit (K ⋙ F))
(iso.symm (lifted_limit_maps_to_original (limits.limit.is_limit (K ⋙ F))) ≪≫
functor.map_iso (limits.cones.functoriality K F)
(limits.is_limit.unique_up_to_iso (lifted_limit_is_limit (limits.limit.is_limit (K ⋙ F))) t))
/-- `F` preserves the limit of shape `J` if it creates these limits and `D` has them. -/
protected instance preserves_limit_of_shape_of_creates_limits_of_shape_and_has_limits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (F : C ⥤ D) [creates_limits_of_shape J F] [limits.has_limits_of_shape J D] : limits.preserves_limits_of_shape J F :=
limits.preserves_limits_of_shape.mk
fun (K : J ⥤ C) => category_theory.preserves_limit_of_creates_limit_and_has_limit K F
/-- `F` preserves limits if it creates limits and `D` has limits. -/
protected instance preserves_limits_of_creates_limits_and_has_limits {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) [creates_limits F] [limits.has_limits D] : limits.preserves_limits F :=
limits.preserves_limits.mk
fun (J : Type v) (𝒥 : small_category J) =>
category_theory.preserves_limit_of_shape_of_creates_limits_of_shape_and_has_limits_of_shape F
/--
If `F` reflects isomorphisms and we can lift any limit cocone to a limit cocone,
then `F` creates colimits.
In particular here we don't need to assume that F reflects colimits.
-/
def creates_colimit_of_reflects_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {F : C ⥤ D} [reflects_isomorphisms F] (h : (c : limits.cocone (K ⋙ F)) → (t : limits.is_colimit c) → lifts_to_colimit K F c t) : creates_colimit K F :=
creates_colimit.mk
fun (c : limits.cocone (K ⋙ F)) (t : limits.is_colimit c) => lifts_to_colimit.to_liftable_cocone (h c t)
/-- `F` preserves the colimit of `K` if it creates the colimit and `K ⋙ F` has the colimit. -/
protected instance preserves_colimit_of_creates_colimit_and_has_colimit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) [creates_colimit K F] [limits.has_colimit (K ⋙ F)] : limits.preserves_colimit K F :=
limits.preserves_colimit.mk
fun (c : limits.cocone K) (t : limits.is_colimit c) =>
limits.is_colimit.of_iso_colimit (limits.colimit.is_colimit (K ⋙ F))
(iso.symm (lifted_colimit_maps_to_original (limits.colimit.is_colimit (K ⋙ F))) ≪≫
functor.map_iso (limits.cocones.functoriality K F)
(limits.is_colimit.unique_up_to_iso (lifted_colimit_is_colimit (limits.colimit.is_colimit (K ⋙ F))) t))
/-- `F` preserves the colimit of shape `J` if it creates these colimits and `D` has them. -/
protected instance preserves_colimit_of_shape_of_creates_colimits_of_shape_and_has_colimits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (F : C ⥤ D) [creates_colimits_of_shape J F] [limits.has_colimits_of_shape J D] : limits.preserves_colimits_of_shape J F :=
limits.preserves_colimits_of_shape.mk
fun (K : J ⥤ C) => category_theory.preserves_colimit_of_creates_colimit_and_has_colimit K F
/-- `F` preserves limits if it creates limits and `D` has limits. -/
protected instance preserves_colimits_of_creates_colimits_and_has_colimits {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) [creates_colimits F] [limits.has_colimits D] : limits.preserves_colimits F :=
limits.preserves_colimits.mk
fun (J : Type v) (𝒥 : small_category J) =>
category_theory.preserves_colimit_of_shape_of_creates_colimits_of_shape_and_has_colimits_of_shape F
/-- If `F` creates the limit of `K` and `F ≅ G`, then `G` creates the limit of `K`. -/
def creates_limit_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [creates_limit K F] : creates_limit K G :=
creates_limit.mk
fun (c : limits.cone (K ⋙ G)) (t : limits.is_limit c) =>
liftable_cone.mk
(lift_limit (coe_fn (equiv.symm (limits.is_limit.postcompose_inv_equiv (iso_whisker_left K h) c)) t))
(limits.is_limit.unique_up_to_iso
(limits.is_limit.map_cone_equiv h
(limits.is_limit.of_iso_limit
(coe_fn (equiv.symm (limits.is_limit.postcompose_inv_equiv (iso_whisker_left K h) c)) t)
(iso.symm
(lifted_limit_maps_to_original
(coe_fn (equiv.symm (limits.is_limit.postcompose_inv_equiv (iso_whisker_left K h) c)) t)))))
t)
/-- If `F` creates limits of shape `J` and `F ≅ G`, then `G` creates limits of shape `J`. -/
def creates_limits_of_shape_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [creates_limits_of_shape J F] : creates_limits_of_shape J G :=
creates_limits_of_shape.mk fun (K : J ⥤ C) => creates_limit_of_nat_iso h
/-- If `F` creates limits and `F ≅ G`, then `G` creates limits. -/
def creates_limits_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [creates_limits F] : creates_limits G :=
creates_limits.mk fun (J : Type v) (𝒥₁ : small_category J) => creates_limits_of_shape_of_nat_iso h
/-- If `F` creates the colimit of `K` and `F ≅ G`, then `G` creates the colimit of `K`. -/
def creates_colimit_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [creates_colimit K F] : creates_colimit K G :=
creates_colimit.mk
fun (c : limits.cocone (K ⋙ G)) (t : limits.is_colimit c) =>
liftable_cocone.mk
(lift_colimit (coe_fn (equiv.symm (limits.is_colimit.precompose_hom_equiv (iso_whisker_left K h) c)) t))
(limits.is_colimit.unique_up_to_iso
(limits.is_colimit.map_cocone_equiv h
(limits.is_colimit.of_iso_colimit
(coe_fn (equiv.symm (limits.is_colimit.precompose_hom_equiv (iso_whisker_left K h) c)) t)
(iso.symm
(lifted_colimit_maps_to_original
(coe_fn (equiv.symm (limits.is_colimit.precompose_hom_equiv (iso_whisker_left K h) c)) t)))))
t)
/-- If `F` creates colimits of shape `J` and `F ≅ G`, then `G` creates colimits of shape `J`. -/
def creates_colimits_of_shape_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [creates_colimits_of_shape J F] : creates_colimits_of_shape J G :=
creates_colimits_of_shape.mk fun (K : J ⥤ C) => creates_colimit_of_nat_iso h
/-- If `F` creates colimits and `F ≅ G`, then `G` creates colimits. -/
def creates_colimits_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [creates_colimits F] : creates_colimits G :=
creates_colimits.mk fun (J : Type v) (𝒥₁ : small_category J) => creates_colimits_of_shape_of_nat_iso h
-- For the inhabited linter later.
/-- If F creates the limit of K, any cone lifts to a limit. -/
def lifts_to_limit_of_creates {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) [creates_limit K F] (c : limits.cone (K ⋙ F)) (t : limits.is_limit c) : lifts_to_limit K F c t :=
lifts_to_limit.mk (liftable_cone.mk (lift_limit t) (lifted_limit_maps_to_original t)) (lifted_limit_is_limit t)
-- For the inhabited linter later.
/-- If F creates the colimit of K, any cocone lifts to a colimit. -/
def lifts_to_colimit_of_creates {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) [creates_colimit K F] (c : limits.cocone (K ⋙ F)) (t : limits.is_colimit c) : lifts_to_colimit K F c t :=
lifts_to_colimit.mk (liftable_cocone.mk (lift_colimit t) (lifted_colimit_maps_to_original t))
(lifted_colimit_is_colimit t)
/-- Any cone lifts through the identity functor. -/
def id_lifts_cone {C : Type u₁} [category C] {J : Type v} [small_category J] {K : J ⥤ C} (c : limits.cone (K ⋙ 𝟭)) : liftable_cone K 𝟭 c :=
liftable_cone.mk (limits.cone.mk (limits.cone.X c) (limits.cone.π c ≫ iso.hom (functor.right_unitor K)))
(limits.cones.ext
(iso.refl
(limits.cone.X
(functor.map_cone 𝟭 (limits.cone.mk (limits.cone.X c) (limits.cone.π c ≫ iso.hom (functor.right_unitor K))))))
sorry)
/-- The identity functor creates all limits. -/
protected instance id_creates_limits {C : Type u₁} [category C] : creates_limits 𝟭 :=
creates_limits.mk
fun (J : Type v) (𝒥 : small_category J) =>
creates_limits_of_shape.mk
fun (F : J ⥤ C) => creates_limit.mk fun (c : limits.cone (F ⋙ 𝟭)) (t : limits.is_limit c) => id_lifts_cone c
/-- Any cocone lifts through the identity functor. -/
def id_lifts_cocone {C : Type u₁} [category C] {J : Type v} [small_category J] {K : J ⥤ C} (c : limits.cocone (K ⋙ 𝟭)) : liftable_cocone K 𝟭 c :=
liftable_cocone.mk (limits.cocone.mk (limits.cocone.X c) (iso.inv (functor.right_unitor K) ≫ limits.cocone.ι c))
(limits.cocones.ext
(iso.refl
(limits.cocone.X
(functor.map_cocone 𝟭
(limits.cocone.mk (limits.cocone.X c) (iso.inv (functor.right_unitor K) ≫ limits.cocone.ι c)))))
sorry)
/-- The identity functor creates all colimits. -/
protected instance id_creates_colimits {C : Type u₁} [category C] : creates_colimits 𝟭 :=
creates_colimits.mk
fun (J : Type v) (𝒥 : small_category J) =>
creates_colimits_of_shape.mk
fun (F : J ⥤ C) =>
creates_colimit.mk fun (c : limits.cocone (F ⋙ 𝟭)) (t : limits.is_colimit c) => id_lifts_cocone c
/-- Satisfy the inhabited linter -/
protected instance inhabited_liftable_cone {C : Type u₁} [category C] {J : Type v} [small_category J] {K : J ⥤ C} (c : limits.cone (K ⋙ 𝟭)) : Inhabited (liftable_cone K 𝟭 c) :=
{ default := id_lifts_cone c }
protected instance inhabited_liftable_cocone {C : Type u₁} [category C] {J : Type v} [small_category J] {K : J ⥤ C} (c : limits.cocone (K ⋙ 𝟭)) : Inhabited (liftable_cocone K 𝟭 c) :=
{ default := id_lifts_cocone c }
/-- Satisfy the inhabited linter -/
protected instance inhabited_lifts_to_limit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) [creates_limit K F] (c : limits.cone (K ⋙ F)) (t : limits.is_limit c) : Inhabited (lifts_to_limit K F c t) :=
{ default := lifts_to_limit_of_creates K F c t }
protected instance inhabited_lifts_to_colimit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) [creates_colimit K F] (c : limits.cocone (K ⋙ F)) (t : limits.is_colimit c) : Inhabited (lifts_to_colimit K F c t) :=
{ default := lifts_to_colimit_of_creates K F c t }
protected instance comp_creates_limit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [creates_limit K F] [creates_limit (K ⋙ F) G] : creates_limit K (F ⋙ G) :=
creates_limit.mk
fun (c : limits.cone (K ⋙ F ⋙ G)) (t : limits.is_limit c) =>
liftable_cone.mk (lift_limit (lifted_limit_is_limit t))
(functor.map_iso (limits.cones.functoriality (K ⋙ F) G)
(lifted_limit_maps_to_original (lifted_limit_is_limit t)) ≪≫
lifted_limit_maps_to_original t)
protected instance comp_creates_limits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [creates_limits_of_shape J F] [creates_limits_of_shape J G] : creates_limits_of_shape J (F ⋙ G) :=
creates_limits_of_shape.mk infer_instance
protected instance comp_creates_limits {C : Type u₁} [category C] {D : Type u₂} [category D] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [creates_limits F] [creates_limits G] : creates_limits (F ⋙ G) :=
creates_limits.mk infer_instance
protected instance comp_creates_colimit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [creates_colimit K F] [creates_colimit (K ⋙ F) G] : creates_colimit K (F ⋙ G) :=
creates_colimit.mk
fun (c : limits.cocone (K ⋙ F ⋙ G)) (t : limits.is_colimit c) =>
liftable_cocone.mk (lift_colimit (lifted_colimit_is_colimit t))
(functor.map_iso (limits.cocones.functoriality (K ⋙ F) G)
(lifted_colimit_maps_to_original (lifted_colimit_is_colimit t)) ≪≫
lifted_colimit_maps_to_original t)
protected instance comp_creates_colimits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [creates_colimits_of_shape J F] [creates_colimits_of_shape J G] : creates_colimits_of_shape J (F ⋙ G) :=
creates_colimits_of_shape.mk infer_instance
protected instance comp_creates_colimits {C : Type u₁} [category C] {D : Type u₂} [category D] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [creates_colimits F] [creates_colimits G] : creates_colimits (F ⋙ G) :=
creates_colimits.mk infer_instance
|
530114ea719dbe21678edd5628908a895db73874 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/topology/metric_space/kuratowski.lean | b0b5f91c9889c83f6b7a5a058ecd84f8def93478 | [
"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 | 5,217 | lean | /-
Copyright (c) 2018 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import analysis.normed_space.lp_space
import topology.sets.compacts
/-!
# The Kuratowski embedding
Any separable metric space can be embedded isometrically in `ℓ^∞(ℝ)`.
-/
noncomputable theory
open set metric topological_space
open_locale ennreal
local notation `ℓ_infty_ℝ`:= lp (λ n : ℕ, ℝ) ∞
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
namespace Kuratowski_embedding
/-! ### Any separable metric space can be embedded isometrically in ℓ^∞(ℝ) -/
variables {f g : ℓ_infty_ℝ} {n : ℕ} {C : ℝ} [metric_space α] (x : ℕ → α) (a b : α)
/-- A metric space can be embedded in `l^∞(ℝ)` via the distances to points in
a fixed countable set, if this set is dense. This map is given in `Kuratowski_embedding`,
without density assumptions. -/
def embedding_of_subset : ℓ_infty_ℝ :=
⟨ λ n, dist a (x n) - dist (x 0) (x n),
begin
apply mem_ℓp_infty,
use dist a (x 0),
rintros - ⟨n, rfl⟩,
exact abs_dist_sub_le _ _ _
end ⟩
lemma embedding_of_subset_coe : embedding_of_subset x a n = dist a (x n) - dist (x 0) (x n) := rfl
/-- The embedding map is always a semi-contraction. -/
lemma embedding_of_subset_dist_le (a b : α) :
dist (embedding_of_subset x a) (embedding_of_subset x b) ≤ dist a b :=
begin
refine lp.norm_le_of_forall_le dist_nonneg (λn, _),
simp only [lp.coe_fn_sub, pi.sub_apply, embedding_of_subset_coe, real.dist_eq],
convert abs_dist_sub_le a b (x n) using 2,
ring
end
/-- When the reference set is dense, the embedding map is an isometry on its image. -/
lemma embedding_of_subset_isometry (H : dense_range x) : isometry (embedding_of_subset x) :=
begin
refine isometry_emetric_iff_metric.2 (λa b, _),
refine (embedding_of_subset_dist_le x a b).antisymm (le_of_forall_pos_le_add (λe epos, _)),
/- First step: find n with dist a (x n) < e -/
rcases metric.mem_closure_range_iff.1 (H a) (e/2) (half_pos epos) with ⟨n, hn⟩,
/- Second step: use the norm control at index n to conclude -/
have C : dist b (x n) - dist a (x n) = embedding_of_subset x b n - embedding_of_subset x a n :=
by { simp only [embedding_of_subset_coe, sub_sub_sub_cancel_right] },
have := calc
dist a b ≤ dist a (x n) + dist (x n) b : dist_triangle _ _ _
... = 2 * dist a (x n) + (dist b (x n) - dist a (x n)) : by { simp [dist_comm], ring }
... ≤ 2 * dist a (x n) + |dist b (x n) - dist a (x n)| :
by apply_rules [add_le_add_left, le_abs_self]
... ≤ 2 * (e/2) + |embedding_of_subset x b n - embedding_of_subset x a n| :
begin rw C, apply_rules [add_le_add, mul_le_mul_of_nonneg_left, hn.le, le_refl], norm_num end
... ≤ 2 * (e/2) + dist (embedding_of_subset x b) (embedding_of_subset x a) :
begin
have : |embedding_of_subset x b n - embedding_of_subset x a n|
≤ dist (embedding_of_subset x b) (embedding_of_subset x a),
{ simpa [dist_eq_norm] using lp.norm_apply_le_norm ennreal.top_ne_zero
(embedding_of_subset x b - embedding_of_subset x a) n },
nlinarith,
end
... = dist (embedding_of_subset x b) (embedding_of_subset x a) + e : by ring,
simpa [dist_comm] using this
end
/-- Every separable metric space embeds isometrically in `ℓ_infty_ℝ`. -/
theorem exists_isometric_embedding (α : Type u) [metric_space α] [separable_space α] :
∃(f : α → ℓ_infty_ℝ), isometry f :=
begin
cases (univ : set α).eq_empty_or_nonempty with h h,
{ use (λ_, 0), assume x, exact absurd h (nonempty.ne_empty ⟨x, mem_univ x⟩) },
{ /- We construct a map x : ℕ → α with dense image -/
rcases h with ⟨basepoint⟩,
haveI : inhabited α := ⟨basepoint⟩,
have : ∃s:set α, countable s ∧ dense s := exists_countable_dense α,
rcases this with ⟨S, ⟨S_countable, S_dense⟩⟩,
rcases countable_iff_exists_surjective.1 S_countable with ⟨x, x_range⟩,
/- Use embedding_of_subset to construct the desired isometry -/
exact ⟨embedding_of_subset x, embedding_of_subset_isometry x (S_dense.mono x_range)⟩ }
end
end Kuratowski_embedding
open topological_space Kuratowski_embedding
/-- The Kuratowski embedding is an isometric embedding of a separable metric space in `ℓ^∞(ℝ)`. -/
def Kuratowski_embedding (α : Type u) [metric_space α] [separable_space α] : α → ℓ_infty_ℝ :=
classical.some (Kuratowski_embedding.exists_isometric_embedding α)
/-- The Kuratowski embedding is an isometry. -/
protected lemma Kuratowski_embedding.isometry (α : Type u) [metric_space α] [separable_space α] :
isometry (Kuratowski_embedding α) :=
classical.some_spec (exists_isometric_embedding α)
/-- Version of the Kuratowski embedding for nonempty compacts -/
def nonempty_compacts.Kuratowski_embedding (α : Type u) [metric_space α] [compact_space α]
[nonempty α] :
nonempty_compacts ℓ_infty_ℝ :=
{ carrier := range (Kuratowski_embedding α),
compact' := is_compact_range (Kuratowski_embedding.isometry α).continuous,
nonempty' := range_nonempty _ }
|
f0b481b0f57d3cf5eb24451826440d34bcb274b1 | 0a6b214fd60b822ae708d58655b7d2f36091d8f8 | /src/tactics.lean | 1624830107fd396992253a4a17e63dcfcaba9101 | [
"MIT"
] | permissive | Naomij1/lean-homotopie | de0bf067bc77254deac02aab0184511ce7aaafe7 | 21bd6be53449c61088234ceab7ec63c18628521a | refs/heads/master | 1,613,216,666,817 | 1,591,786,776,000 | 1,591,786,776,000 | 244,693,557 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,114 | lean | import tactic.interactive
import data.set.intervals.basic
import data.real.basic
import topology.instances.real
import tactic.suggest
import topology.basic
import topology.algebra.continuous_functions
import topology.continuous_on
import topology.algebra.ordered
import topology.constructions
import topology.algebra.ordered
import data.set.function
import topology.constructions
import tactic.split_ifs
open set topological_space
meta def applyc' (c : name) (cfg : tactic.apply_cfg := {}) : tactic unit :=
do c' ← tactic.mk_const c, tactic.apply' c' cfg, tactic.skip --tactic.trace $ "" ++ (to_string c) ++ "",tactic.skip
meta def exactc (c : name) : tactic unit :=
do c ← tactic.mk_const c, tactic.exact c, tactic.skip
meta def finish : list expr -> tactic unit
| [] := tactic.skip
| (h :: hs) :=
do {
applyc' ``continuous_const <|>
applyc' ``continuous_id<|>
applyc' ``continuous_fst <|>
applyc' ``continuous_snd <|>
applyc' ``continuous_subtype_val <|>
tactic.skip,
finish hs
}
meta def elaborate : tactic unit := do
{ applyc' ``continuous.prod_mk <|> applyc' ``continuous_subtype_mk <|> applyc' ``continuous.sub
<|> applyc' ``continuous.add
<|> applyc' ``continuous.comp
<|> applyc' ``continuous.mul}
-- matcher le plut tôt possible avec hyp
meta def elaborate_cont : nat -> tactic unit
| 0 := tactic.skip
| (n+1) := do{
--trié par priorité
tactic.any_goals elaborate<|>
tactic.skip,
elaborate_cont n}
-- élabore le plus possible toutes les continuités nécessaires, puis les résout avec finish
meta def cont : nat -> tactic unit
| n := do
let depth := n,
do { `(continuous %%s) ← tactic.target,
--tactic.trace $ "We are trying to prove that [" ++ (to_string s) ++ "] is continuous ",
elaborate_cont depth,
hs ← tactic.get_goals,
finish hs}
<|> tactic.fail "Goal is not continuous x"
--def f: ℝ × ℝ -> ℝ× ℝ:= λ x, (x.1+x.2,x.2)
--example : continuous f :=
--begin
--cont 3,
--end |
8ded66bc9319b8af31954b8b5faa81ef58478e2c | 6e41ee3ac9b96e8980a16295cc21f131e731884f | /hott/init/nat.hlean | 7fd2a6d89324c847b557e7ca122d0099cc38b6b8 | [
"Apache-2.0"
] | permissive | EgbertRijke/lean | 3426cfa0e5b3d35d12fc3fd7318b35574cb67dc3 | 4f2e0c6d7fc9274d953cfa1c37ab2f3e799ab183 | refs/heads/master | 1,610,834,871,476 | 1,422,159,801,000 | 1,422,159,801,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 11,506 | hlean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Leonardo de Moura
-/
prelude
import init.wf init.tactic init.hedberg init.util init.types.sum
open eq.ops decidable sum
namespace nat
open lift
notation `ℕ` := nat
inductive lt (a : nat) : nat → Type :=
base : lt a (succ a),
step : Π {b}, lt a b → lt a (succ b)
notation a < b := lt a b
definition le (a b : nat) : Type₀ := a < succ b
notation a ≤ b := le a b
definition pred (a : nat) : nat :=
cases_on a zero (λ a₁, a₁)
protected definition is_inhabited [instance] : inhabited nat :=
inhabited.mk zero
protected definition has_decidable_eq [instance] : decidable_eq nat :=
λn m : nat,
have general : ∀n, decidable (n = m), from
rec_on m
(λ n, cases_on n
(inl rfl)
(λ m, inr (λ (e : succ m = zero), down (no_confusion e))))
(λ (m' : nat) (ih : ∀n, decidable (n = m')) (n : nat), cases_on n
(inr (λ h, down (no_confusion h)))
(λ (n' : nat),
decidable.rec_on (ih n')
(assume Heq : n' = m', inl (eq.rec_on Heq rfl))
(assume Hne : n' ≠ m',
have H1 : succ n' ≠ succ m', from
assume Heq, down (no_confusion Heq (λ e : n' = m', Hne e)),
inr H1))),
general n
-- less-than is well-founded
definition lt.wf [instance] : well_founded lt :=
well_founded.intro (λn, rec_on n
(acc.intro zero (λ (y : nat) (hlt : y < zero),
have aux : ∀ {n₁}, y < n₁ → zero = n₁ → acc lt y, from
λ n₁ hlt, lt.cases_on hlt
(λ heq, down (no_confusion heq))
(λ b hlt heq, down (no_confusion heq)),
aux hlt rfl))
(λ (n : nat) (ih : acc lt n),
acc.intro (succ n) (λ (m : nat) (hlt : m < succ n),
have aux : ∀ {n₁} (hlt : m < n₁), succ n = n₁ → acc lt m, from
λ n₁ hlt, lt.cases_on hlt
(λ (heq : succ n = succ m),
down (no_confusion heq (λ (e : n = m),
eq.rec_on e ih)))
(λ b (hlt : m < b) (heq : succ n = succ b),
down (no_confusion heq (λ (e : n = b),
acc.inv (eq.rec_on e ih) hlt))),
aux hlt rfl)))
definition measure {A : Type} (f : A → nat) : A → A → Type₀ :=
inv_image lt f
definition measure.wf {A : Type} (f : A → nat) : well_founded (measure f) :=
inv_image.wf f lt.wf
definition not_lt_zero (a : nat) : ¬ a < zero :=
have aux : ∀ {b}, a < b → b = zero → empty, from
λ b H, lt.cases_on H
(λ heq, down (no_confusion heq))
(λ b h₁ heq, down (no_confusion heq)),
λ H, aux H rfl
definition zero_lt_succ (a : nat) : zero < succ a :=
rec_on a
(lt.base zero)
(λ a (hlt : zero < succ a), lt.step hlt)
definition lt.trans {a b c : nat} (H₁ : a < b) (H₂ : b < c) : a < c :=
have aux : ∀ {d}, d < c → b = d → a < b → a < c, from
(λ d H, lt.rec_on H
(λ h₁ h₂, lt.step (eq.rec_on h₁ h₂))
(λ b hl ih h₁ h₂, lt.step (ih h₁ h₂))),
aux H₂ rfl H₁
definition lt.succ_of_lt {a b : nat} (H : a < b) : succ a < succ b :=
lt.rec_on H
(lt.base (succ a))
(λ b hlt ih, lt.trans ih (lt.base (succ b)))
definition lt.of_succ_lt {a b : nat} (H : succ a < b) : a < b :=
have aux : ∀ {a₁}, a₁ < b → succ a = a₁ → a < b, from
λ a₁ H, lt.rec_on H
(λ e₁, eq.rec_on e₁ (lt.step (lt.base a)))
(λ d hlt ih e₁, lt.step (ih e₁)),
aux H rfl
definition lt.of_succ_lt_succ {a b : nat} (H : succ a < succ b) : a < b :=
have aux : pred (succ a) < pred (succ b), from
lt.rec_on H
(lt.base a)
(λ (b : nat) (hlt : succ a < b) ih,
show pred (succ a) < pred (succ b), from
lt.of_succ_lt hlt),
aux
definition lt.is_decidable_rel [instance] : decidable_rel lt :=
λ a b, rec_on b
(λ (a : nat), inr (not_lt_zero a))
(λ (b₁ : nat) (ih : ∀ a, decidable (a < b₁)) (a : nat), cases_on a
(inl !zero_lt_succ)
(λ a, decidable.rec_on (ih a)
(λ h_pos : a < b₁, inl (lt.succ_of_lt h_pos))
(λ h_neg : ¬ a < b₁,
have aux : ¬ succ a < succ b₁, from
λ h : succ a < succ b₁, h_neg (lt.of_succ_lt_succ h),
inr aux)))
a
definition le.refl (a : nat) : a ≤ a :=
lt.base a
definition le.of_lt {a b : nat} (H : a < b) : a ≤ b :=
lt.step H
definition eq_or_lt_of_le {a b : nat} (H : a ≤ b) : sum (a = b) (a < b) :=
have aux : Π (a₁ b₁ : nat) (hlt : a₁ < b₁), a₁ = a → b₁ = (succ b) → sum (a = b) (a < b), from
λ a₁ b₁ hlt, lt.rec_on hlt
(λ h₁, eq.rec_on h₁ (λ h₂, down (no_confusion h₂ (λ h₃, eq.rec_on h₃ (sum.inl rfl)))))
(λ b₁ hlt ih h₁, eq.rec_on h₁ (λ h₂, down (no_confusion h₂ (λ h₃, eq.rec_on h₃ (sum.inr hlt))))),
aux a (succ b) H rfl rfl
definition le.of_eq_or_lt {a b : nat} (H : sum (a = b) (a < b)) : a ≤ b :=
sum.rec_on H
(λ hl, eq.rec_on hl !le.refl)
(λ hr, le.of_lt hr)
definition le.is_decidable_rel [instance] : decidable_rel le :=
λ a b, decidable_iff_equiv _ (iff.intro le.of_eq_or_lt eq_or_lt_of_le)
definition le.rec_on {a : nat} {P : nat → Type} {b : nat} (H : a ≤ b) (H₁ : P a) (H₂ : ∀ b, a < b → P b) : P b :=
begin
cases H with (b', hlt),
apply H₁,
apply (H₂ b hlt)
end
definition lt.irrefl (a : nat) : ¬ a < a :=
rec_on a
!not_lt_zero
(λ (a : nat) (ih : ¬ a < a) (h : succ a < succ a),
ih (lt.of_succ_lt_succ h))
definition lt.asymm {a b : nat} (H : a < b) : ¬ b < a :=
lt.rec_on H
(λ h : succ a < a, !lt.irrefl (lt.of_succ_lt h))
(λ b hlt (ih : ¬ b < a) (h : succ b < a), ih (lt.of_succ_lt h))
definition lt.trichotomy (a b : nat) : a < b ⊎ a = b ⊎ b < a :=
rec_on b
(λa, cases_on a
(sum.inr (sum.inl rfl))
(λ a₁, sum.inr (sum.inr !zero_lt_succ)))
(λ b₁ (ih : ∀a, a < b₁ ⊎ a = b₁ ⊎ b₁ < a) (a : nat), cases_on a
(sum.inl !zero_lt_succ)
(λ a, sum.rec_on (ih a)
(λ h : a < b₁, sum.inl (lt.succ_of_lt h))
(λ h, sum.rec_on h
(λ h : a = b₁, sum.inr (sum.inl (eq.rec_on h rfl)))
(λ h : b₁ < a, sum.inr (sum.inr (lt.succ_of_lt h))))))
a
definition eq_or_lt_of_not_lt {a b : nat} (hnlt : ¬ a < b) : a = b ⊎ b < a :=
sum.rec_on (lt.trichotomy a b)
(λ hlt, absurd hlt hnlt)
(λ h, h)
definition lt_succ_of_le {a b : nat} (h : a ≤ b) : a < succ b :=
h
definition lt_of_succ_le {a b : nat} (h : succ a ≤ b) : a < b :=
lt.of_succ_lt_succ h
definition le.step {a b : nat} (h : a ≤ b) : a ≤ succ b :=
lt.step h
definition succ_le_of_lt {a b : nat} (h : a < b) : succ a ≤ b :=
lt.succ_of_lt h
definition le.trans {a b c : nat} (h₁ : a ≤ b) (h₂ : b ≤ c) : a ≤ c :=
begin
cases h₁ with (b', hlt),
apply h₂,
apply (lt.trans hlt h₂)
end
definition lt.of_le_of_lt {a b c : nat} (h₁ : a ≤ b) (h₂ : b < c) : a < c :=
begin
cases h₁ with (b', hlt),
apply h₂,
apply (lt.trans hlt h₂)
end
definition lt.of_lt_of_le {a b c : nat} (h₁ : a < b) (h₂ : b ≤ c) : a < c :=
begin
cases h₁ with (b', hlt),
apply (lt.of_succ_lt_succ h₂),
apply (lt.trans hlt (lt.of_succ_lt_succ h₂))
end
definition lt.of_lt_of_eq {a b c : nat} (h₁ : a < b) (h₂ : b = c) : a < c :=
eq.rec_on h₂ h₁
definition le.of_le_of_eq {a b c : nat} (h₁ : a ≤ b) (h₂ : b = c) : a ≤ c :=
eq.rec_on h₂ h₁
definition lt.of_eq_of_lt {a b c : nat} (h₁ : a = b) (h₂ : b < c) : a < c :=
eq.rec_on (eq.rec_on h₁ rfl) h₂
definition le.of_eq_of_le {a b c : nat} (h₁ : a = b) (h₂ : b ≤ c) : a ≤ c :=
eq.rec_on (eq.rec_on h₁ rfl) h₂
calc_trans lt.trans
calc_trans lt.of_le_of_lt
calc_trans lt.of_lt_of_le
calc_trans lt.of_lt_of_eq
calc_trans lt.of_eq_of_lt
calc_trans le.trans
calc_trans le.of_le_of_eq
calc_trans le.of_eq_of_le
definition max (a b : nat) : nat :=
if a < b then b else a
definition min (a b : nat) : nat :=
if a < b then a else b
definition max_a_a (a : nat) : a = max a a :=
eq.rec_on !if_t_t rfl
definition max.eq_right {a b : nat} (H : a < b) : max a b = b :=
if_pos H
definition max.eq_left {a b : nat} (H : ¬ a < b) : max a b = a :=
if_neg H
definition max.right_eq {a b : nat} (H : a < b) : b = max a b :=
eq.rec_on (max.eq_right H) rfl
definition max.left_eq {a b : nat} (H : ¬ a < b) : a = max a b :=
eq.rec_on (max.eq_left H) rfl
definition max.left (a b : nat) : a ≤ max a b :=
by_cases
(λ h : a < b, le.of_lt (eq.rec_on (max.right_eq h) h))
(λ h : ¬ a < b, eq.rec_on (max.eq_left h) !le.refl)
definition max.right (a b : nat) : b ≤ max a b :=
by_cases
(λ h : a < b, eq.rec_on (max.eq_right h) !le.refl)
(λ h : ¬ a < b, sum.rec_on (eq_or_lt_of_not_lt h)
(λ heq, eq.rec_on heq (eq.rec_on (max_a_a a) !le.refl))
(λ h : b < a,
have aux : a = max a b, from max.left_eq (lt.asymm h),
eq.rec_on aux (le.of_lt h)))
definition gt a b := lt b a
notation a > b := gt a b
definition ge a b := le b a
notation a ≥ b := ge a b
definition add (a b : nat) : nat :=
rec_on b a (λ b₁ r, succ r)
notation a + b := add a b
definition sub (a b : nat) : nat :=
rec_on b a (λ b₁ r, pred r)
notation a - b := sub a b
definition mul (a b : nat) : nat :=
rec_on b zero (λ b₁ r, r + a)
notation a * b := mul a b
attribute sub [reducible]
definition succ_sub_succ_eq_sub (a b : nat) : succ a - succ b = a - b :=
rec_on b
rfl
(λ b₁ (ih : succ a - succ b₁ = a - b₁),
eq.rec_on ih (eq.refl (pred (succ a - succ b₁))))
definition sub_eq_succ_sub_succ (a b : nat) : a - b = succ a - succ b :=
eq.rec_on (succ_sub_succ_eq_sub a b) rfl
definition zero_sub_eq_zero (a : nat) : zero - a = zero :=
rec_on a
rfl
(λ a₁ (ih : zero - a₁ = zero), calc
zero - succ a₁ = pred (zero - a₁) : rfl
... = pred zero : ih
... = zero : rfl)
definition zero_eq_zero_sub (a : nat) : zero = zero - a :=
eq.rec_on (zero_sub_eq_zero a) rfl
definition sub.lt {a b : nat} : zero < a → zero < b → a - b < a :=
have aux : Π {a}, zero < a → Π {b}, zero < b → a - b < a, from
λa h₁, lt.rec_on h₁
(λb h₂, lt.cases_on h₂
(lt.base zero)
(λ b₁ bpos,
eq.rec_on (sub_eq_succ_sub_succ zero b₁)
(eq.rec_on (zero_eq_zero_sub b₁) (lt.base zero))))
(λa₁ apos ih b h₂, lt.cases_on h₂
(lt.base a₁)
(λ b₁ bpos,
eq.rec_on (sub_eq_succ_sub_succ a₁ b₁)
(lt.trans (@ih b₁ bpos) (lt.base a₁)))),
λ h₁ h₂, aux h₁ h₂
definition pred_le (a : nat) : pred a ≤ a :=
cases_on a
(le.refl zero)
(λ a₁, le.of_lt (lt.base a₁))
definition sub_le (a b : nat) : a - b ≤ a :=
rec_on b
(le.refl a)
(λ b₁ ih, le.trans !pred_le ih)
definition of_num [coercion] [reducible] (n : num) : ℕ :=
num.rec zero
(λ n, pos_num.rec (succ zero) (λ n r, r + r + (succ zero)) (λ n r, r + r) n) n
end nat
|
83b24c12e17a7332f00e16f543b5a9b0d7f2f84e | c777c32c8e484e195053731103c5e52af26a25d1 | /src/measure_theory/covering/differentiation.lean | 8dd8cd38b8c53e4e46ee658c6cdcce43ad4527cc | [
"Apache-2.0"
] | permissive | kbuzzard/mathlib | 2ff9e85dfe2a46f4b291927f983afec17e946eb8 | 58537299e922f9c77df76cb613910914a479c1f7 | refs/heads/master | 1,685,313,702,744 | 1,683,974,212,000 | 1,683,974,212,000 | 128,185,277 | 1 | 0 | null | 1,522,920,600,000 | 1,522,920,600,000 | null | UTF-8 | Lean | false | false | 51,314 | lean | /-
Copyright (c) 2021 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 measure_theory.covering.vitali_family
import measure_theory.measure.regular
import measure_theory.function.ae_measurable_order
import measure_theory.integral.lebesgue
import measure_theory.integral.average
import measure_theory.decomposition.lebesgue
/-!
# Differentiation of measures
On a second countable metric space with a measure `μ`, consider a Vitali family (i.e., for each `x`
one has a family of sets shrinking to `x`, with a good behavior with respect to covering theorems).
Consider also another measure `ρ`. Then, for almost every `x`, the ratio `ρ a / μ a` converges when
`a` shrinks to `x` along the Vitali family, towards the Radon-Nikodym derivative of `ρ` with
respect to `μ`. This is the main theorem on differentiation of measures.
This theorem is proved in this file, under the name `vitali_family.ae_tendsto_rn_deriv`. Note that,
almost surely, `μ a` is eventually positive and finite (see
`vitali_family.ae_eventually_measure_pos` and `vitali_family.eventually_measure_lt_top`), so the
ratio really makes sense.
For concrete applications, one needs concrete instances of Vitali families, as provided for instance
by `besicovitch.vitali_family` (for balls) or by `vitali.vitali_family` (for doubling measures).
Specific applications to Lebesgue density points and the Lebesgue differentiation theorem are also
derived:
* `vitali_family.ae_tendsto_measure_inter_div` states that, for almost every point `x ∈ s`,
then `μ (s ∩ a) / μ a` tends to `1` as `a` shrinks to `x` along a Vitali family.
* `vitali_family.ae_tendsto_average_norm_sub` states that, for almost every point `x`, then the
average of `y ↦ ‖f y - f x‖` on `a` tends to `0` as `a` shrinks to `x` along a Vitali family.
## Sketch of proof
Let `v` be a Vitali family for `μ`. Assume for simplicity that `ρ` is absolutely continuous with
respect to `μ`, as the case of a singular measure is easier.
It is easy to see that a set `s` on which `liminf ρ a / μ a < q` satisfies `ρ s ≤ q * μ s`, by using
a disjoint subcovering provided by the definition of Vitali families. Similarly for the limsup.
It follows that a set on which `ρ a / μ a` oscillates has measure `0`, and therefore that
`ρ a / μ a` converges almost surely (`vitali_family.ae_tendsto_div`). Moreover, on a set where the
limit is close to a constant `c`, one gets `ρ s ∼ c μ s`, using again a covering lemma as above.
It follows that `ρ` is equal to `μ.with_density (v.lim_ratio ρ x)`, where `v.lim_ratio ρ x` is the
limit of `ρ a / μ a` at `x` (which is well defined almost everywhere). By uniqueness of the
Radon-Nikodym derivative, one gets `v.lim_ratio ρ x = ρ.rn_deriv μ x` almost everywhere, completing
the proof.
There is a difficulty in this sketch: this argument works well when `v.lim_ratio ρ` is measurable,
but there is no guarantee that this is the case, especially if one doesn't make further assumptions
on the Vitali family. We use an indirect argument to show that `v.lim_ratio ρ` is always
almost everywhere measurable, again based on the disjoint subcovering argument
(see `vitali_family.exists_measurable_supersets_lim_ratio`), and then proceed as sketched above
but replacing `v.lim_ratio ρ` by a measurable version called `v.lim_ratio_meas ρ`.
## Counterexample
The standing assumption in this file is that spaces are second countable. Without this assumption,
measures may be zero locally but nonzero globally, which is not compatible with differentiation
theory (which deduces global information from local one). Here is an example displaying this
behavior.
Define a measure `μ` by `μ s = 0` if `s` is covered by countably many balls of radius `1`,
and `μ s = ∞` otherwise. This is indeed a countably additive measure, which is moreover
locally finite and doubling at small scales. It vanishes on every ball of radius `1`, so all the
quantities in differentiation theory (defined as ratios of measures as the radius tends to zero)
make no sense. However, the measure is not globally zero if the space is big enough.
## References
* [Herbert Federer, Geometric Measure Theory, Chapter 2.9][Federer1996]
-/
open measure_theory metric set filter topological_space measure_theory.measure
open_locale filter ennreal measure_theory nnreal topology
variables {α : Type*} [metric_space α] {m0 : measurable_space α}
{μ : measure α} (v : vitali_family μ)
{E : Type*} [normed_add_comm_group E]
include v
namespace vitali_family
/-- The limit along a Vitali family of `ρ a / μ a` where it makes sense, and garbage otherwise.
Do *not* use this definition: it is only a temporary device to show that this ratio tends almost
everywhere to the Radon-Nikodym derivative. -/
noncomputable def lim_ratio (ρ : measure α) (x : α) : ℝ≥0∞ :=
lim (v.filter_at x) (λ a, ρ a / μ a)
/-- For almost every point `x`, sufficiently small sets in a Vitali family around `x` have positive
measure. (This is a nontrivial result, following from the covering property of Vitali families). -/
theorem ae_eventually_measure_pos [second_countable_topology α] :
∀ᵐ x ∂μ, ∀ᶠ a in v.filter_at x, 0 < μ a :=
begin
set s := {x | ¬ (∀ᶠ a in v.filter_at x, 0 < μ a)} with hs,
simp only [not_lt, not_eventually, nonpos_iff_eq_zero] at hs,
change μ s = 0,
let f : α → set (set α) := λ x, {a | μ a = 0},
have h : v.fine_subfamily_on f s,
{ assume x hx ε εpos,
rw hs at hx,
simp only [frequently_filter_at_iff, exists_prop, gt_iff_lt, mem_set_of_eq] at hx,
rcases hx ε εpos with ⟨a, a_sets, ax, μa⟩,
exact ⟨a, ⟨a_sets, μa⟩, ax⟩ },
refine le_antisymm _ bot_le,
calc μ s ≤ ∑' (x : h.index), μ (h.covering x) : h.measure_le_tsum
... = ∑' (x : h.index), 0 : by { congr, ext1 x, exact h.covering_mem x.2 }
... = 0 : by simp only [tsum_zero, add_zero]
end
/-- For every point `x`, sufficiently small sets in a Vitali family around `x` have finite measure.
(This is a trivial result, following from the fact that the measure is locally finite). -/
theorem eventually_measure_lt_top [is_locally_finite_measure μ] (x : α) :
∀ᶠ a in v.filter_at x, μ a < ∞ :=
begin
obtain ⟨ε, εpos, με⟩ : ∃ (ε : ℝ) (hi : 0 < ε), μ (closed_ball x ε) < ∞ :=
(μ.finite_at_nhds x).exists_mem_basis nhds_basis_closed_ball,
exact v.eventually_filter_at_iff.2 ⟨ε, εpos, λ a ha haε, (measure_mono haε).trans_lt με⟩,
end
/-- If two measures `ρ` and `ν` have, at every point of a set `s`, arbitrarily small sets in a
Vitali family satisfying `ρ a ≤ ν a`, then `ρ s ≤ ν s` if `ρ ≪ μ`.-/
theorem measure_le_of_frequently_le [second_countable_topology α] [borel_space α]
{ρ : measure α} (ν : measure α) [is_locally_finite_measure ν]
(hρ : ρ ≪ μ) (s : set α) (hs : ∀ x ∈ s, ∃ᶠ a in v.filter_at x, ρ a ≤ ν a) :
ρ s ≤ ν s :=
begin
-- this follows from a covering argument using the sets satisfying `ρ a ≤ ν a`.
apply ennreal.le_of_forall_pos_le_add (λ ε εpos hc, _),
obtain ⟨U, sU, U_open, νU⟩ : ∃ (U : set α) (H : s ⊆ U), is_open U ∧ ν U ≤ ν s + ε :=
exists_is_open_le_add s ν (ennreal.coe_pos.2 εpos).ne',
let f : α → set (set α) := λ x, {a | ρ a ≤ ν a ∧ a ⊆ U},
have h : v.fine_subfamily_on f s,
{ apply v.fine_subfamily_on_of_frequently f s (λ x hx, _),
have := (hs x hx).and_eventually ((v.eventually_filter_at_mem_sets x).and
(v.eventually_filter_at_subset_of_nhds (U_open.mem_nhds (sU hx)))),
apply frequently.mono this,
rintros a ⟨ρa, av, aU⟩,
exact ⟨ρa, aU⟩ },
haveI : encodable h.index := h.index_countable.to_encodable,
calc ρ s ≤ ∑' (x : h.index), ρ (h.covering x) : h.measure_le_tsum_of_absolutely_continuous hρ
... ≤ ∑' (x : h.index), ν (h.covering x) : ennreal.tsum_le_tsum (λ x, (h.covering_mem x.2).1)
... = ν (⋃ (x : h.index), h.covering x) :
by rw [measure_Union h.covering_disjoint_subtype (λ i, h.measurable_set_u i.2)]
... ≤ ν U : measure_mono (Union_subset (λ i, (h.covering_mem i.2).2))
... ≤ ν s + ε : νU
end
section
variables [second_countable_topology α] [borel_space α] [is_locally_finite_measure μ]
{ρ : measure α} [is_locally_finite_measure ρ]
/-- If a measure `ρ` is singular with respect to `μ`, then for `μ` almost every `x`, the ratio
`ρ a / μ a` tends to zero when `a` shrinks to `x` along the Vitali family. This makes sense
as `μ a` is eventually positive by `ae_eventually_measure_pos`. -/
lemma ae_eventually_measure_zero_of_singular (hρ : ρ ⟂ₘ μ) :
∀ᵐ x ∂μ, tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 0) :=
begin
have A : ∀ ε > (0 : ℝ≥0), ∀ᵐ x ∂μ, ∀ᶠ a in v.filter_at x, ρ a < ε * μ a,
{ assume ε εpos,
set s := {x | ¬(∀ᶠ a in v.filter_at x, ρ a < ε * μ a) } with hs,
change μ s = 0,
obtain ⟨o, o_meas, ρo, μo⟩ : ∃ (o : set α), measurable_set o ∧ ρ o = 0 ∧ μ oᶜ = 0 := hρ,
apply le_antisymm _ bot_le,
calc μ s ≤ μ ((s ∩ o) ∪ oᶜ) : begin
conv_lhs { rw ← inter_union_compl s o },
exact measure_mono (union_subset_union_right _ (inter_subset_right _ _))
end
... ≤ μ (s ∩ o) + μ (oᶜ) : measure_union_le _ _
... = μ (s ∩ o) : by rw [μo, add_zero]
... = ε⁻¹ * (ε • μ) (s ∩ o) : begin
simp only [coe_nnreal_smul_apply, ← mul_assoc, mul_comm _ (ε : ℝ≥0∞)],
rw [ennreal.mul_inv_cancel (ennreal.coe_pos.2 εpos).ne' ennreal.coe_ne_top, one_mul],
end
... ≤ ε⁻¹ * ρ (s ∩ o) : begin
refine mul_le_mul_left' _ _,
refine v.measure_le_of_frequently_le ρ ((measure.absolutely_continuous.refl μ).smul ε) _ _,
assume x hx,
rw hs at hx,
simp only [mem_inter_iff, not_lt, not_eventually, mem_set_of_eq] at hx,
exact hx.1
end
... ≤ ε⁻¹ * ρ o : mul_le_mul_left' (measure_mono (inter_subset_right _ _)) _
... = 0 : by rw [ρo, mul_zero] },
obtain ⟨u, u_anti, u_pos, u_lim⟩ :
∃ (u : ℕ → ℝ≥0), strict_anti u ∧ (∀ (n : ℕ), 0 < u n) ∧ tendsto u at_top (𝓝 0) :=
exists_seq_strict_anti_tendsto (0 : ℝ≥0),
have B : ∀ᵐ x ∂μ, ∀ n, ∀ᶠ a in v.filter_at x, ρ a < u n * μ a :=
ae_all_iff.2 (λ n, A (u n) (u_pos n)),
filter_upwards [B, v.ae_eventually_measure_pos],
assume x hx h'x,
refine tendsto_order.2 ⟨λ z hz, (ennreal.not_lt_zero hz).elim, λ z hz, _⟩,
obtain ⟨w, w_pos, w_lt⟩ : ∃ (w : ℝ≥0), (0 : ℝ≥0∞) < w ∧ (w : ℝ≥0∞) < z :=
ennreal.lt_iff_exists_nnreal_btwn.1 hz,
obtain ⟨n, hn⟩ : ∃ n, u n < w :=
((tendsto_order.1 u_lim).2 w (ennreal.coe_pos.1 w_pos)).exists,
filter_upwards [hx n, h'x, v.eventually_measure_lt_top x],
assume a ha μa_pos μa_lt_top,
rw ennreal.div_lt_iff (or.inl μa_pos.ne') (or.inl μa_lt_top.ne),
exact ha.trans_le (mul_le_mul_right' ((ennreal.coe_le_coe.2 hn.le).trans w_lt.le) _)
end
section absolutely_continuous
variable (hρ : ρ ≪ μ)
include hρ
/-- A set of points `s` satisfying both `ρ a ≤ c * μ a` and `ρ a ≥ d * μ a` at arbitrarily small
sets in a Vitali family has measure `0` if `c < d`. Indeed, the first inequality should imply
that `ρ s ≤ c * μ s`, and the second one that `ρ s ≥ d * μ s`, a contradiction if `0 < μ s`. -/
theorem null_of_frequently_le_of_frequently_ge {c d : ℝ≥0} (hcd : c < d) (s : set α)
(hc : ∀ x ∈ s, ∃ᶠ a in v.filter_at x, ρ a ≤ c * μ a)
(hd : ∀ x ∈ s, ∃ᶠ a in v.filter_at x, (d : ℝ≥0∞) * μ a ≤ ρ a) :
μ s = 0 :=
begin
apply null_of_locally_null s (λ x hx, _),
obtain ⟨o, xo, o_open, μo⟩ : ∃ o : set α, x ∈ o ∧ is_open o ∧ μ o < ∞ :=
measure.exists_is_open_measure_lt_top μ x,
refine ⟨s ∩ o, inter_mem_nhds_within _ (o_open.mem_nhds xo), _⟩,
let s' := s ∩ o,
by_contra,
apply lt_irrefl (ρ s'),
calc ρ s' ≤ c * μ s' : v.measure_le_of_frequently_le (c • μ) hρ s' (λ x hx, hc x hx.1)
... < d * μ s' : begin
apply (ennreal.mul_lt_mul_right h _).2 (ennreal.coe_lt_coe.2 hcd),
exact (lt_of_le_of_lt (measure_mono (inter_subset_right _ _)) μo).ne,
end
... ≤ ρ s' : v.measure_le_of_frequently_le ρ
((measure.absolutely_continuous.refl μ).smul d) s' (λ x hx, hd x hx.1)
end
/-- If `ρ` is absolutely continuous with respect to `μ`, then for almost every `x`,
the ratio `ρ a / μ a` converges as `a` shrinks to `x` along a Vitali family for `μ`. -/
theorem ae_tendsto_div :
∀ᵐ x ∂μ, ∃ c, tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 c) :=
begin
obtain ⟨w, w_count, w_dense, w_zero, w_top⟩ : ∃ w : set ℝ≥0∞, w.countable ∧ dense w ∧
0 ∉ w ∧ ∞ ∉ w := ennreal.exists_countable_dense_no_zero_top,
have I : ∀ x ∈ w, x ≠ ∞ := λ x xs hx, w_top (hx ▸ xs),
have A : ∀ (c ∈ w) (d ∈ w), (c < d) → ∀ᵐ x ∂μ,
¬((∃ᶠ a in v.filter_at x, ρ a / μ a < c) ∧ (∃ᶠ a in v.filter_at x, d < ρ a / μ a)),
{ assume c hc d hd hcd,
lift c to ℝ≥0 using I c hc,
lift d to ℝ≥0 using I d hd,
apply v.null_of_frequently_le_of_frequently_ge hρ (ennreal.coe_lt_coe.1 hcd),
{ simp only [and_imp, exists_prop, not_frequently, not_and, not_lt, not_le, not_eventually,
mem_set_of_eq, mem_compl_iff, not_forall],
assume x h1x h2x,
apply h1x.mono (λ a ha, _),
refine (ennreal.div_le_iff_le_mul _ (or.inr (bot_le.trans_lt ha).ne')).1 ha.le,
simp only [ennreal.coe_ne_top, ne.def, or_true, not_false_iff] },
{ simp only [and_imp, exists_prop, not_frequently, not_and, not_lt, not_le, not_eventually,
mem_set_of_eq, mem_compl_iff, not_forall],
assume x h1x h2x,
apply h2x.mono (λ a ha, _),
exact ennreal.mul_le_of_le_div ha.le } },
have B : ∀ᵐ x ∂μ, ∀ (c ∈ w) (d ∈ w), (c < d) →
¬((∃ᶠ a in v.filter_at x, ρ a / μ a < c) ∧ (∃ᶠ a in v.filter_at x, d < ρ a / μ a)),
by simpa only [ae_ball_iff w_count, ae_all_iff],
filter_upwards [B],
assume x hx,
exact tendsto_of_no_upcrossings w_dense hx,
end
lemma ae_tendsto_lim_ratio :
∀ᵐ x ∂μ, tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 (v.lim_ratio ρ x)) :=
begin
filter_upwards [v.ae_tendsto_div hρ],
assume x hx,
exact tendsto_nhds_lim hx,
end
/-- Given two thresholds `p < q`, the sets `{x | v.lim_ratio ρ x < p}`
and `{x | q < v.lim_ratio ρ x}` are obviously disjoint. The key to proving that `v.lim_ratio ρ` is
almost everywhere measurable is to show that these sets have measurable supersets which are also
disjoint, up to zero measure. This is the content of this lemma. -/
lemma exists_measurable_supersets_lim_ratio {p q : ℝ≥0} (hpq : p < q) :
∃ a b, measurable_set a ∧ measurable_set b ∧ {x | v.lim_ratio ρ x < p} ⊆ a
∧ {x | (q : ℝ≥0∞) < v.lim_ratio ρ x} ⊆ b ∧ μ (a ∩ b) = 0 :=
begin
/- Here is a rough sketch, assuming that the measure is finite and the limit is well defined
everywhere. Let `u := {x | v.lim_ratio ρ x < p}` and `w := {x | q < v.lim_ratio ρ x}`. They
have measurable supersets `u'` and `w'` of the same measure. We will show that these satisfy
the conclusion of the theorem, i.e., `μ (u' ∩ w') = 0`. For this, note that
`ρ (u' ∩ w') = ρ (u ∩ w')` (as `w'` is measurable, see `measure_to_measurable_add_inter_left`).
The latter set is included in the set where the limit of the ratios is `< p`, and therefore
its measure is `≤ p * μ (u ∩ w')`. Using the same trick in the other direction gives that this is
`p * μ (u' ∩ w')`. We have shown that `ρ (u' ∩ w') ≤ p * μ (u' ∩ w')`. Arguing in the same way but
using the `w` part gives `q * μ (u' ∩ w') ≤ ρ (u' ∩ w')`. If `μ (u' ∩ w')` were nonzero, this
would be a contradiction as `p < q`.
For the rigorous proof, we need to work on a part of the space where the measure is finite
(provided by `spanning_sets (ρ + μ)`) and to restrict to the set where the limit is well defined
(called `s` below, of full measure). Otherwise, the argument goes through.
-/
let s := {x | ∃ c, tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 c)},
let o : ℕ → set α := spanning_sets (ρ + μ),
let u := λ n, s ∩ {x | v.lim_ratio ρ x < p} ∩ o n,
let w := λ n, s ∩ {x | (q : ℝ≥0∞) < v.lim_ratio ρ x} ∩ o n,
-- the supersets are obtained by restricting to the set `s` where the limit is well defined, to
-- a finite measure part `o n`, taking a measurable superset here, and then taking the union over
-- `n`.
refine ⟨to_measurable μ sᶜ ∪ (⋃ n, to_measurable (ρ + μ) (u n)),
to_measurable μ sᶜ ∪ (⋃ n, to_measurable (ρ + μ) (w n)), _, _, _, _, _⟩,
-- check that these sets are measurable supersets as required
{ exact (measurable_set_to_measurable _ _).union
(measurable_set.Union (λ n, (measurable_set_to_measurable _ _))) },
{ exact (measurable_set_to_measurable _ _).union
(measurable_set.Union (λ n, (measurable_set_to_measurable _ _))) },
{ assume x hx,
by_cases h : x ∈ s,
{ refine or.inr (mem_Union.2 ⟨spanning_sets_index (ρ + μ) x, _⟩),
exact subset_to_measurable _ _ ⟨⟨h, hx⟩, mem_spanning_sets_index _ _⟩ },
{ exact or.inl (subset_to_measurable μ sᶜ h) } },
{ assume x hx,
by_cases h : x ∈ s,
{ refine or.inr (mem_Union.2 ⟨spanning_sets_index (ρ + μ) x, _⟩),
exact subset_to_measurable _ _ ⟨⟨h, hx⟩, mem_spanning_sets_index _ _⟩ },
{ exact or.inl (subset_to_measurable μ sᶜ h) } },
-- it remains to check the nontrivial part that these sets have zero measure intersection.
-- it suffices to do it for fixed `m` and `n`, as one is taking countable unions.
suffices H : ∀ (m n : ℕ), μ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) = 0,
{ have A : (to_measurable μ sᶜ ∪ (⋃ n, to_measurable (ρ + μ) (u n))) ∩
(to_measurable μ sᶜ ∪ (⋃ n, to_measurable (ρ + μ) (w n))) ⊆
to_measurable μ sᶜ ∪ (⋃ m n, (to_measurable (ρ + μ) (u m)) ∩ (to_measurable (ρ + μ) (w n))),
{ simp only [inter_distrib_left, inter_distrib_right, true_and, subset_union_left,
union_subset_iff, inter_self],
refine ⟨_, _, _⟩,
{ exact (inter_subset_left _ _).trans (subset_union_left _ _) },
{ exact (inter_subset_right _ _).trans (subset_union_left _ _) },
{ simp_rw [Union_inter, inter_Union], exact subset_union_right _ _ } },
refine le_antisymm ((measure_mono A).trans _) bot_le,
calc
μ (to_measurable μ sᶜ ∪ (⋃ m n, (to_measurable (ρ + μ) (u m)) ∩ (to_measurable (ρ + μ) (w n))))
≤ μ (to_measurable μ sᶜ)
+ μ (⋃ m n, (to_measurable (ρ + μ) (u m)) ∩ (to_measurable (ρ + μ) (w n))) :
measure_union_le _ _
... = μ (⋃ m n, (to_measurable (ρ + μ) (u m)) ∩ (to_measurable (ρ + μ) (w n))) :
by { have : μ sᶜ = 0 := v.ae_tendsto_div hρ, rw [measure_to_measurable, this, zero_add] }
... ≤ ∑' m n, μ ((to_measurable (ρ + μ) (u m)) ∩ (to_measurable (ρ + μ) (w n))) :
(measure_Union_le _).trans (ennreal.tsum_le_tsum (λ m, measure_Union_le _))
... = 0 : by simp only [H, tsum_zero] },
-- now starts the nontrivial part of the argument. We fix `m` and `n`, and show that the
-- measurable supersets of `u m` and `w n` have zero measure intersection by using the lemmas
-- `measure_to_measurable_add_inter_left` (to reduce to `u m` or `w n` instead of the measurable
-- superset) and `measure_le_of_frequently_le` to compare their measures for `ρ` and `μ`.
assume m n,
have I : (ρ + μ) (u m) ≠ ∞,
{ apply (lt_of_le_of_lt (measure_mono _) (measure_spanning_sets_lt_top (ρ + μ) m)).ne,
exact inter_subset_right _ _ },
have J : (ρ + μ) (w n) ≠ ∞,
{ apply (lt_of_le_of_lt (measure_mono _) (measure_spanning_sets_lt_top (ρ + μ) n)).ne,
exact inter_subset_right _ _ },
have A : ρ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n))
≤ p * μ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) := calc
ρ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n))
= ρ (u m ∩ to_measurable (ρ + μ) (w n)) :
measure_to_measurable_add_inter_left (measurable_set_to_measurable _ _) I
... ≤ (p • μ) (u m ∩ to_measurable (ρ + μ) (w n)) : begin
refine v.measure_le_of_frequently_le _ hρ _ (λ x hx, _),
have L : tendsto (λ (a : set α), ρ a / μ a) (v.filter_at x) (𝓝 (v.lim_ratio ρ x)) :=
tendsto_nhds_lim hx.1.1.1,
have I : ∀ᶠ (b : set α) in v.filter_at x, ρ b / μ b < p :=
(tendsto_order.1 L).2 _ hx.1.1.2,
apply I.frequently.mono (λ a ha, _),
rw [coe_nnreal_smul_apply],
refine (ennreal.div_le_iff_le_mul _ (or.inr (bot_le.trans_lt ha).ne')).1 ha.le,
simp only [ennreal.coe_ne_top, ne.def, or_true, not_false_iff]
end
... = p * μ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) :
by simp only [coe_nnreal_smul_apply,
(measure_to_measurable_add_inter_right (measurable_set_to_measurable _ _) I)],
have B : (q : ℝ≥0∞) * μ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n))
≤ ρ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) := calc
(q : ℝ≥0∞) * μ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n))
= (q : ℝ≥0∞) * μ (to_measurable (ρ + μ) (u m) ∩ w n) : begin
conv_rhs { rw inter_comm },
rw [inter_comm, measure_to_measurable_add_inter_right (measurable_set_to_measurable _ _) J]
end
... ≤ ρ (to_measurable (ρ + μ) (u m) ∩ w n) : begin
rw [← coe_nnreal_smul_apply],
refine v.measure_le_of_frequently_le _ (absolutely_continuous.rfl.smul _) _ _,
assume x hx,
have L : tendsto (λ (a : set α), ρ a / μ a) (v.filter_at x) (𝓝 (v.lim_ratio ρ x)) :=
tendsto_nhds_lim hx.2.1.1,
have I : ∀ᶠ (b : set α) in v.filter_at x, (q : ℝ≥0∞) < ρ b / μ b :=
(tendsto_order.1 L).1 _ hx.2.1.2,
apply I.frequently.mono (λ a ha, _),
rw [coe_nnreal_smul_apply],
exact ennreal.mul_le_of_le_div ha.le
end
... = ρ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) : begin
conv_rhs { rw inter_comm },
rw inter_comm,
exact (measure_to_measurable_add_inter_left (measurable_set_to_measurable _ _) J).symm,
end,
by_contra,
apply lt_irrefl (ρ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n))),
calc ρ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n))
≤ p * μ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) : A
... < q * μ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) : begin
apply (ennreal.mul_lt_mul_right h _).2 (ennreal.coe_lt_coe.2 hpq),
suffices H : (ρ + μ) (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) ≠ ∞,
{ simp only [not_or_distrib, ennreal.add_eq_top, pi.add_apply, ne.def, coe_add] at H,
exact H.2 },
apply (lt_of_le_of_lt (measure_mono (inter_subset_left _ _)) _).ne,
rw measure_to_measurable,
apply lt_of_le_of_lt (measure_mono _) (measure_spanning_sets_lt_top (ρ + μ) m),
exact inter_subset_right _ _
end
... ≤ ρ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) : B
end
theorem ae_measurable_lim_ratio : ae_measurable (v.lim_ratio ρ) μ :=
begin
apply ennreal.ae_measurable_of_exist_almost_disjoint_supersets _ _ (λ p q hpq, _),
exact v.exists_measurable_supersets_lim_ratio hρ hpq,
end
/-- A measurable version of `v.lim_ratio ρ`. Do *not* use this definition: it is only a temporary
device to show that `v.lim_ratio` is almost everywhere equal to the Radon-Nikodym derivative. -/
noncomputable def lim_ratio_meas : α → ℝ≥0∞ :=
(v.ae_measurable_lim_ratio hρ).mk _
lemma lim_ratio_meas_measurable : measurable (v.lim_ratio_meas hρ) :=
ae_measurable.measurable_mk _
lemma ae_tendsto_lim_ratio_meas :
∀ᵐ x ∂μ, tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 (v.lim_ratio_meas hρ x)) :=
begin
filter_upwards [v.ae_tendsto_lim_ratio hρ, ae_measurable.ae_eq_mk (v.ae_measurable_lim_ratio hρ)],
assume x hx h'x,
rwa h'x at hx,
end
/-- If, for all `x` in a set `s`, one has frequently `ρ a / μ a < p`, then `ρ s ≤ p * μ s`, as
proved in `measure_le_of_frequently_le`. Since `ρ a / μ a` tends almost everywhere to
`v.lim_ratio_meas hρ x`, the same property holds for sets `s` on which `v.lim_ratio_meas hρ < p`. -/
lemma measure_le_mul_of_subset_lim_ratio_meas_lt
{p : ℝ≥0} {s : set α} (h : s ⊆ {x | v.lim_ratio_meas hρ x < p}) :
ρ s ≤ p * μ s :=
begin
let t := {x : α | tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 (v.lim_ratio_meas hρ x))},
have A : μ tᶜ = 0 := v.ae_tendsto_lim_ratio_meas hρ,
suffices H : ρ (s ∩ t) ≤ (p • μ) (s ∩ t), from calc
ρ s = ρ ((s ∩ t) ∪ (s ∩ tᶜ)) : by rw inter_union_compl
... ≤ ρ (s ∩ t) + ρ (s ∩ tᶜ) : measure_union_le _ _
... ≤ p * μ (s ∩ t) + 0 :
add_le_add H ((measure_mono (inter_subset_right _ _)).trans (hρ A).le)
... ≤ p * μ s :
by { rw add_zero, exact mul_le_mul_left' (measure_mono (inter_subset_left _ _)) _ },
refine v.measure_le_of_frequently_le _ hρ _ (λ x hx, _),
have I : ∀ᶠ (b : set α) in v.filter_at x, ρ b / μ b < p := (tendsto_order.1 hx.2).2 _ (h hx.1),
apply I.frequently.mono (λ a ha, _),
rw [coe_nnreal_smul_apply],
refine (ennreal.div_le_iff_le_mul _ (or.inr (bot_le.trans_lt ha).ne')).1 ha.le,
simp only [ennreal.coe_ne_top, ne.def, or_true, not_false_iff]
end
/-- If, for all `x` in a set `s`, one has frequently `q < ρ a / μ a`, then `q * μ s ≤ ρ s`, as
proved in `measure_le_of_frequently_le`. Since `ρ a / μ a` tends almost everywhere to
`v.lim_ratio_meas hρ x`, the same property holds for sets `s` on which `q < v.lim_ratio_meas hρ`. -/
lemma mul_measure_le_of_subset_lt_lim_ratio_meas
{q : ℝ≥0} {s : set α} (h : s ⊆ {x | (q : ℝ≥0∞) < v.lim_ratio_meas hρ x}) :
(q : ℝ≥0∞) * μ s ≤ ρ s :=
begin
let t := {x : α | tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 (v.lim_ratio_meas hρ x))},
have A : μ tᶜ = 0 := v.ae_tendsto_lim_ratio_meas hρ,
suffices H : (q • μ) (s ∩ t) ≤ ρ (s ∩ t), from calc
(q • μ) s = (q • μ) ((s ∩ t) ∪ (s ∩ tᶜ)) : by rw inter_union_compl
... ≤ (q • μ) (s ∩ t) + (q • μ) (s ∩ tᶜ) : measure_union_le _ _
... ≤ ρ (s ∩ t) + q * μ tᶜ : begin
apply add_le_add H,
rw [coe_nnreal_smul_apply],
exact mul_le_mul_left' (measure_mono (inter_subset_right _ _)) _,
end
... ≤ ρ s :
by { rw [A, mul_zero, add_zero], exact measure_mono (inter_subset_left _ _) },
refine v.measure_le_of_frequently_le _ (absolutely_continuous.rfl.smul _) _ _,
assume x hx,
have I : ∀ᶠ a in v.filter_at x, (q : ℝ≥0∞) < ρ a / μ a := (tendsto_order.1 hx.2).1 _ (h hx.1),
apply I.frequently.mono (λ a ha, _),
rw [coe_nnreal_smul_apply],
exact ennreal.mul_le_of_le_div ha.le
end
/-- The points with `v.lim_ratio_meas hρ x = ∞` have measure `0` for `μ`. -/
lemma measure_lim_ratio_meas_top : μ {x | v.lim_ratio_meas hρ x = ∞} = 0 :=
begin
refine null_of_locally_null _ (λ x hx, _),
obtain ⟨o, xo, o_open, μo⟩ : ∃ o : set α, x ∈ o ∧ is_open o ∧ ρ o < ∞ :=
measure.exists_is_open_measure_lt_top ρ x,
let s := {x : α | v.lim_ratio_meas hρ x = ∞} ∩ o,
refine ⟨s, inter_mem_nhds_within _ (o_open.mem_nhds xo), le_antisymm _ bot_le⟩,
have ρs : ρ s ≠ ∞ := ((measure_mono (inter_subset_right _ _)).trans_lt μo).ne,
have A : ∀ (q : ℝ≥0), 1 ≤ q → μ s ≤ q⁻¹ * ρ s,
{ assume q hq,
rw [mul_comm, ← div_eq_mul_inv, ennreal.le_div_iff_mul_le _ (or.inr ρs), mul_comm],
{ apply v.mul_measure_le_of_subset_lt_lim_ratio_meas hρ,
assume y hy,
have : v.lim_ratio_meas hρ y = ∞ := hy.1,
simp only [this, ennreal.coe_lt_top, mem_set_of_eq], },
{ simp only [(zero_lt_one.trans_le hq).ne', true_or, ennreal.coe_eq_zero, ne.def,
not_false_iff] } },
have B : tendsto (λ (q : ℝ≥0), (q : ℝ≥0∞)⁻¹ * ρ s) at_top (𝓝 (∞⁻¹ * ρ s)),
{ apply ennreal.tendsto.mul_const _ (or.inr ρs),
exact ennreal.tendsto_inv_iff.2 (ennreal.tendsto_coe_nhds_top.2 tendsto_id) },
simp only [zero_mul, ennreal.inv_top] at B,
apply ge_of_tendsto B,
exact eventually_at_top.2 ⟨1, A⟩,
end
/-- The points with `v.lim_ratio_meas hρ x = 0` have measure `0` for `ρ`. -/
lemma measure_lim_ratio_meas_zero : ρ {x | v.lim_ratio_meas hρ x = 0} = 0 :=
begin
refine null_of_locally_null _ (λ x hx, _),
obtain ⟨o, xo, o_open, μo⟩ : ∃ o : set α, x ∈ o ∧ is_open o ∧ μ o < ∞ :=
measure.exists_is_open_measure_lt_top μ x,
let s := {x : α | v.lim_ratio_meas hρ x = 0} ∩ o,
refine ⟨s, inter_mem_nhds_within _ (o_open.mem_nhds xo), le_antisymm _ bot_le⟩,
have μs : μ s ≠ ∞ := ((measure_mono (inter_subset_right _ _)).trans_lt μo).ne,
have A : ∀ (q : ℝ≥0), 0 < q → ρ s ≤ q * μ s,
{ assume q hq,
apply v.measure_le_mul_of_subset_lim_ratio_meas_lt hρ,
assume y hy,
have : v.lim_ratio_meas hρ y = 0 := hy.1,
simp only [this, mem_set_of_eq, hq, ennreal.coe_pos], },
have B : tendsto (λ (q : ℝ≥0), (q : ℝ≥0∞) * μ s) (𝓝[>] (0 : ℝ≥0)) (𝓝 ((0 : ℝ≥0) * μ s)),
{ apply ennreal.tendsto.mul_const _ (or.inr μs),
rw ennreal.tendsto_coe,
exact nhds_within_le_nhds },
simp only [zero_mul, ennreal.coe_zero] at B,
apply ge_of_tendsto B,
filter_upwards [self_mem_nhds_within] using A,
end
/-- As an intermediate step to show that `μ.with_density (v.lim_ratio_meas hρ) = ρ`, we show here
that `μ.with_density (v.lim_ratio_meas hρ) ≤ t^2 ρ` for any `t > 1`. -/
lemma with_density_le_mul {s : set α} (hs : measurable_set s) {t : ℝ≥0} (ht : 1 < t) :
μ.with_density (v.lim_ratio_meas hρ) s ≤ t^2 * ρ s :=
begin
/- We cut `s` into the sets where `v.lim_ratio_meas hρ = 0`, where `v.lim_ratio_meas hρ = ∞`, and
where `v.lim_ratio_meas hρ ∈ [t^n, t^(n+1))` for `n : ℤ`. The first and second have measure `0`.
For the latter, since `v.lim_ratio_meas hρ` fluctuates by at most `t` on this slice, we can use
`measure_le_mul_of_subset_lim_ratio_meas_lt` and `mul_measure_le_of_subset_lt_lim_ratio_meas` to
show that the two measures are comparable up to `t` (in fact `t^2` for technical reasons of
strict inequalities). -/
have t_ne_zero' : t ≠ 0 := (zero_lt_one.trans ht).ne',
have t_ne_zero : (t : ℝ≥0∞) ≠ 0, by simpa only [ennreal.coe_eq_zero, ne.def] using t_ne_zero',
let ν := μ.with_density (v.lim_ratio_meas hρ),
let f := v.lim_ratio_meas hρ,
have f_meas : measurable f := v.lim_ratio_meas_measurable hρ,
have A : ν (s ∩ f ⁻¹' ({0})) ≤ ((t : ℝ≥0∞)^2 • ρ) (s ∩ f⁻¹' {0}),
{ apply le_trans _ (zero_le _),
have M : measurable_set (s ∩ f ⁻¹' ({0})) := hs.inter (f_meas (measurable_set_singleton _)),
simp only [ν, f, nonpos_iff_eq_zero, M, with_density_apply, lintegral_eq_zero_iff f_meas],
apply (ae_restrict_iff' M).2,
exact eventually_of_forall (λ x hx, hx.2) },
have B : ν (s ∩ f ⁻¹' ({∞})) ≤ ((t : ℝ≥0∞)^2 • ρ) (s ∩ f⁻¹' {∞}),
{ apply le_trans (le_of_eq _) (zero_le _),
apply with_density_absolutely_continuous μ _,
rw ← nonpos_iff_eq_zero,
exact (measure_mono (inter_subset_right _ _)).trans (v.measure_lim_ratio_meas_top hρ).le },
have C : ∀ (n : ℤ), ν (s ∩ f⁻¹' (Ico (t^n) (t^(n+1))))
≤ ((t : ℝ≥0∞)^2 • ρ) (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))),
{ assume n,
let I := Ico ((t : ℝ≥0∞)^n) (t^(n+1)),
have M : measurable_set (s ∩ f ⁻¹' I) := hs.inter (f_meas measurable_set_Ico),
simp only [f, M, with_density_apply, coe_nnreal_smul_apply],
calc
∫⁻ x in s ∩ f⁻¹' I, f x ∂μ
≤ ∫⁻ x in s ∩ f⁻¹' I, t^(n+1) ∂μ :
lintegral_mono_ae ((ae_restrict_iff' M).2 (eventually_of_forall (λ x hx, hx.2.2.le)))
... = t^(n+1) * μ (s ∩ f⁻¹' I) :
by simp only [lintegral_const, measurable_set.univ, measure.restrict_apply, univ_inter]
... = t^(2 : ℤ) * (t^(n-1) * μ (s ∩ f⁻¹' I)) : begin
rw [← mul_assoc, ← ennreal.zpow_add t_ne_zero ennreal.coe_ne_top],
congr' 2,
abel,
end
... ≤ t^2 * ρ (s ∩ f ⁻¹' I) : begin
refine mul_le_mul_left' _ _,
rw ← ennreal.coe_zpow (zero_lt_one.trans ht).ne',
apply v.mul_measure_le_of_subset_lt_lim_ratio_meas hρ,
assume x hx,
apply lt_of_lt_of_le _ hx.2.1,
rw [← ennreal.coe_zpow (zero_lt_one.trans ht).ne', ennreal.coe_lt_coe, sub_eq_add_neg,
zpow_add₀ t_ne_zero'],
conv_rhs { rw ← mul_one (t^ n) },
refine mul_lt_mul' le_rfl _ (zero_le _) (nnreal.zpow_pos t_ne_zero' _),
rw zpow_neg_one,
exact inv_lt_one ht,
end },
calc ν s = ν (s ∩ f⁻¹' {0}) + ν (s ∩ f⁻¹' {∞}) + ∑' (n : ℤ), ν (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))) :
measure_eq_measure_preimage_add_measure_tsum_Ico_zpow ν f_meas hs ht
... ≤ ((t : ℝ≥0∞)^2 • ρ) (s ∩ f⁻¹' {0}) + ((t : ℝ≥0∞)^2 • ρ) (s ∩ f⁻¹' {∞})
+ ∑' (n : ℤ), ((t : ℝ≥0∞)^2 • ρ) (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))) :
add_le_add (add_le_add A B) (ennreal.tsum_le_tsum C)
... = ((t : ℝ≥0∞)^2 • ρ) s :
(measure_eq_measure_preimage_add_measure_tsum_Ico_zpow ((t : ℝ≥0∞)^2 • ρ) f_meas hs ht).symm
end
/-- As an intermediate step to show that `μ.with_density (v.lim_ratio_meas hρ) = ρ`, we show here
that `ρ ≤ t μ.with_density (v.lim_ratio_meas hρ)` for any `t > 1`. -/
lemma le_mul_with_density {s : set α} (hs : measurable_set s) {t : ℝ≥0} (ht : 1 < t) :
ρ s ≤ t * μ.with_density (v.lim_ratio_meas hρ) s :=
begin
/- We cut `s` into the sets where `v.lim_ratio_meas hρ = 0`, where `v.lim_ratio_meas hρ = ∞`, and
where `v.lim_ratio_meas hρ ∈ [t^n, t^(n+1))` for `n : ℤ`. The first and second have measure `0`.
For the latter, since `v.lim_ratio_meas hρ` fluctuates by at most `t` on this slice, we can use
`measure_le_mul_of_subset_lim_ratio_meas_lt` and `mul_measure_le_of_subset_lt_lim_ratio_meas` to
show that the two measures are comparable up to `t`. -/
have t_ne_zero' : t ≠ 0 := (zero_lt_one.trans ht).ne',
have t_ne_zero : (t : ℝ≥0∞) ≠ 0, by simpa only [ennreal.coe_eq_zero, ne.def] using t_ne_zero',
let ν := μ.with_density (v.lim_ratio_meas hρ),
let f := v.lim_ratio_meas hρ,
have f_meas : measurable f := v.lim_ratio_meas_measurable hρ,
have A : ρ (s ∩ f ⁻¹' ({0})) ≤ (t • ν) (s ∩ f⁻¹' {0}),
{ refine le_trans (measure_mono (inter_subset_right _ _)) (le_trans (le_of_eq _) (zero_le _)),
exact v.measure_lim_ratio_meas_zero hρ },
have B : ρ (s ∩ f ⁻¹' ({∞})) ≤ (t • ν) (s ∩ f⁻¹' {∞}),
{ apply le_trans (le_of_eq _) (zero_le _),
apply hρ,
rw ← nonpos_iff_eq_zero,
exact (measure_mono (inter_subset_right _ _)).trans (v.measure_lim_ratio_meas_top hρ).le },
have C : ∀ (n : ℤ), ρ (s ∩ f⁻¹' (Ico (t^n) (t^(n+1))))
≤ (t • ν) (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))),
{ assume n,
let I := Ico ((t : ℝ≥0∞)^n) (t^(n+1)),
have M : measurable_set (s ∩ f ⁻¹' I) := hs.inter (f_meas measurable_set_Ico),
simp only [f, M, with_density_apply, coe_nnreal_smul_apply],
calc ρ (s ∩ f ⁻¹' I) ≤ t^ (n+1) * μ (s ∩ f ⁻¹' I) : begin
rw ← ennreal.coe_zpow t_ne_zero',
apply v.measure_le_mul_of_subset_lim_ratio_meas_lt hρ,
assume x hx,
apply hx.2.2.trans_le (le_of_eq _),
rw ennreal.coe_zpow t_ne_zero',
end
... = ∫⁻ x in s ∩ f⁻¹' I, t^(n+1) ∂μ :
by simp only [lintegral_const, measurable_set.univ, measure.restrict_apply, univ_inter]
... ≤ ∫⁻ x in s ∩ f⁻¹' I, t * f x ∂μ : begin
apply lintegral_mono_ae ((ae_restrict_iff' M).2 (eventually_of_forall (λ x hx, _))),
rw [add_comm, ennreal.zpow_add t_ne_zero ennreal.coe_ne_top, zpow_one],
exact mul_le_mul_left' hx.2.1 _,
end
... = t * ∫⁻ x in s ∩ f⁻¹' I, f x ∂μ : lintegral_const_mul _ f_meas },
calc ρ s = ρ (s ∩ f⁻¹' {0}) + ρ (s ∩ f⁻¹' {∞}) + ∑' (n : ℤ), ρ (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))) :
measure_eq_measure_preimage_add_measure_tsum_Ico_zpow ρ f_meas hs ht
... ≤ (t • ν) (s ∩ f⁻¹' {0}) + (t • ν) (s ∩ f⁻¹' {∞})
+ ∑' (n : ℤ), (t • ν) (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))) :
add_le_add (add_le_add A B) (ennreal.tsum_le_tsum C)
... = (t • ν) s :
(measure_eq_measure_preimage_add_measure_tsum_Ico_zpow (t • ν) f_meas hs ht).symm
end
theorem with_density_lim_ratio_meas_eq : μ.with_density (v.lim_ratio_meas hρ) = ρ :=
begin
ext1 s hs,
refine le_antisymm _ _,
{ have : tendsto (λ (t : ℝ≥0), (t^2 * ρ s : ℝ≥0∞)) (𝓝[>] 1) (𝓝 ((1 : ℝ≥0)^2 * ρ s)),
{ refine ennreal.tendsto.mul _ _ tendsto_const_nhds _,
{ exact ennreal.tendsto.pow (ennreal.tendsto_coe.2 nhds_within_le_nhds) },
{ simp only [one_pow, ennreal.coe_one, true_or, ne.def, not_false_iff, one_ne_zero] },
{ simp only [one_pow, ennreal.coe_one, ne.def, or_true, ennreal.one_ne_top,
not_false_iff] } },
simp only [one_pow, one_mul, ennreal.coe_one] at this,
refine ge_of_tendsto this _,
filter_upwards [self_mem_nhds_within] with _ ht,
exact v.with_density_le_mul hρ hs ht, },
{ have : tendsto (λ (t : ℝ≥0), (t : ℝ≥0∞) * μ.with_density (v.lim_ratio_meas hρ) s) (𝓝[>] 1)
(𝓝 ((1 : ℝ≥0) * μ.with_density (v.lim_ratio_meas hρ) s)),
{ refine ennreal.tendsto.mul_const (ennreal.tendsto_coe.2 nhds_within_le_nhds) _,
simp only [ennreal.coe_one, true_or, ne.def, not_false_iff, one_ne_zero], },
simp only [one_mul, ennreal.coe_one] at this,
refine ge_of_tendsto this _,
filter_upwards [self_mem_nhds_within] with _ ht,
exact v.le_mul_with_density hρ hs ht }
end
/-- Weak version of the main theorem on differentiation of measures: given a Vitali family `v`
for a locally finite measure `μ`, and another locally finite measure `ρ`, then for `μ`-almost
every `x` the ratio `ρ a / μ a` converges, when `a` shrinks to `x` along the Vitali family,
towards the Radon-Nikodym derivative of `ρ` with respect to `μ`.
This version assumes that `ρ` is absolutely continuous with respect to `μ`. The general version
without this superfluous assumption is `vitali_family.ae_tendsto_rn_deriv`.
-/
theorem ae_tendsto_rn_deriv_of_absolutely_continuous :
∀ᵐ x ∂μ, tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 (ρ.rn_deriv μ x)) :=
begin
have A : (μ.with_density (v.lim_ratio_meas hρ)).rn_deriv μ =ᵐ[μ] v.lim_ratio_meas hρ :=
rn_deriv_with_density μ (v.lim_ratio_meas_measurable hρ),
rw v.with_density_lim_ratio_meas_eq hρ at A,
filter_upwards [v.ae_tendsto_lim_ratio_meas hρ, A] with _ _ h'x,
rwa h'x,
end
end absolutely_continuous
variable (ρ)
/-- Main theorem on differentiation of measures: given a Vitali family `v` for a locally finite
measure `μ`, and another locally finite measure `ρ`, then for `μ`-almost every `x` the
ratio `ρ a / μ a` converges, when `a` shrinks to `x` along the Vitali family, towards the
Radon-Nikodym derivative of `ρ` with respect to `μ`. -/
theorem ae_tendsto_rn_deriv :
∀ᵐ x ∂μ, tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 (ρ.rn_deriv μ x)) :=
begin
let t := μ.with_density (ρ.rn_deriv μ),
have eq_add : ρ = ρ.singular_part μ + t := have_lebesgue_decomposition_add _ _,
have A : ∀ᵐ x ∂μ, tendsto (λ a, ρ.singular_part μ a / μ a) (v.filter_at x) (𝓝 0) :=
v.ae_eventually_measure_zero_of_singular (mutually_singular_singular_part ρ μ),
have B : ∀ᵐ x ∂μ, t.rn_deriv μ x = ρ.rn_deriv μ x :=
rn_deriv_with_density μ (measurable_rn_deriv ρ μ),
have C : ∀ᵐ x ∂μ, tendsto (λ a, t a / μ a) (v.filter_at x) (𝓝 (t.rn_deriv μ x)) :=
v.ae_tendsto_rn_deriv_of_absolutely_continuous (with_density_absolutely_continuous _ _),
filter_upwards [A, B, C] with _ Ax Bx Cx,
convert Ax.add Cx,
{ ext1 a,
conv_lhs { rw [eq_add] },
simp only [pi.add_apply, coe_add, ennreal.add_div] },
{ simp only [Bx, zero_add] }
end
/-! ### Lebesgue density points -/
/-- Given a measurable set `s`, then `μ (s ∩ a) / μ a` converges when `a` shrinks to a typical
point `x` along a Vitali family. The limit is `1` for `x ∈ s` and `0` for `x ∉ s`. This shows that
almost every point of `s` is a Lebesgue density point for `s`. A version for non-measurable sets
holds, but it only gives the first conclusion, see `ae_tendsto_measure_inter_div`. -/
lemma ae_tendsto_measure_inter_div_of_measurable_set {s : set α} (hs : measurable_set s) :
∀ᵐ x ∂μ, tendsto (λ a, μ (s ∩ a) / μ a) (v.filter_at x) (𝓝 (s.indicator 1 x)) :=
begin
haveI : is_locally_finite_measure (μ.restrict s) :=
is_locally_finite_measure_of_le restrict_le_self,
filter_upwards [ae_tendsto_rn_deriv v (μ.restrict s), rn_deriv_restrict μ hs],
assume x hx h'x,
simpa only [h'x, restrict_apply' hs, inter_comm] using hx,
end
/-- Given an arbitrary set `s`, then `μ (s ∩ a) / μ a` converges to `1` when `a` shrinks to a
typical point of `s` along a Vitali family. This shows that almost every point of `s` is a
Lebesgue density point for `s`. A stronger version for measurable sets is given
in `ae_tendsto_measure_inter_div_of_measurable_set`. -/
lemma ae_tendsto_measure_inter_div (s : set α) :
∀ᵐ x ∂(μ.restrict s), tendsto (λ a, μ (s ∩ a) / μ a) (v.filter_at x) (𝓝 1) :=
begin
let t := to_measurable μ s,
have A : ∀ᵐ x ∂(μ.restrict s),
tendsto (λ a, μ (t ∩ a) / μ a) (v.filter_at x) (𝓝 (t.indicator 1 x)),
{ apply ae_mono restrict_le_self,
apply ae_tendsto_measure_inter_div_of_measurable_set,
exact measurable_set_to_measurable _ _ },
have B : ∀ᵐ x ∂(μ.restrict s), t.indicator 1 x = (1 : ℝ≥0∞),
{ refine ae_restrict_of_ae_restrict_of_subset (subset_to_measurable μ s) _,
filter_upwards [ae_restrict_mem (measurable_set_to_measurable μ s)] with _ hx,
simp only [hx, pi.one_apply, indicator_of_mem] },
filter_upwards [A, B] with x hx h'x,
rw [h'x] at hx,
apply hx.congr' _,
filter_upwards [v.eventually_filter_at_measurable_set x] with _ ha,
congr' 1,
exact measure_to_measurable_inter_of_sigma_finite ha _,
end
/-! ### Lebesgue differentiation theorem -/
lemma ae_tendsto_lintegral_div' {f : α → ℝ≥0∞} (hf : measurable f) (h'f : ∫⁻ y, f y ∂μ ≠ ∞) :
∀ᵐ x ∂μ, tendsto (λ a, (∫⁻ y in a, f y ∂μ) / μ a) (v.filter_at x) (𝓝 (f x)) :=
begin
let ρ := μ.with_density f,
haveI : is_finite_measure ρ, from is_finite_measure_with_density h'f,
filter_upwards [ae_tendsto_rn_deriv v ρ, rn_deriv_with_density μ hf] with x hx h'x,
rw ← h'x,
apply hx.congr' _,
filter_upwards [v.eventually_filter_at_measurable_set] with a ha,
rw ← with_density_apply f ha,
end
lemma ae_tendsto_lintegral_div {f : α → ℝ≥0∞} (hf : ae_measurable f μ) (h'f : ∫⁻ y, f y ∂μ ≠ ∞) :
∀ᵐ x ∂μ, tendsto (λ a, (∫⁻ y in a, f y ∂μ) / μ a) (v.filter_at x) (𝓝 (f x)) :=
begin
have A : ∫⁻ y, hf.mk f y ∂μ ≠ ∞,
{ convert h'f using 1,
apply lintegral_congr_ae,
exact hf.ae_eq_mk.symm },
filter_upwards [v.ae_tendsto_lintegral_div' hf.measurable_mk A, hf.ae_eq_mk] with x hx h'x,
rw h'x,
convert hx,
ext1 a,
congr' 1,
apply lintegral_congr_ae,
exact ae_restrict_of_ae (hf.ae_eq_mk)
end
lemma ae_tendsto_lintegral_nnnorm_sub_div'
{f : α → E} (hf : integrable f μ) (h'f : strongly_measurable f) :
∀ᵐ x ∂μ, tendsto (λ a, (∫⁻ y in a, ‖f y - f x‖₊ ∂μ) / μ a) (v.filter_at x) (𝓝 0) :=
begin
/- For every `c`, then `(∫⁻ y in a, ‖f y - c‖₊ ∂μ) / μ a` tends almost everywhere to `‖f x - c‖`.
We apply this to a countable set of `c` which is dense in the range of `f`, to deduce the desired
convergence.
A minor technical inconvenience is that constants are not integrable, so to apply previous lemmas
we need to replace `c` with the restriction of `c` to a finite measure set `A n` in the
above sketch. -/
let A := measure_theory.measure.finite_spanning_sets_in_open' μ,
rcases h'f.is_separable_range with ⟨t, t_count, ht⟩,
have main : ∀ᵐ x ∂μ, ∀ (n : ℕ) (c : E) (hc : c ∈ t),
tendsto (λ a, (∫⁻ y in a, ‖f y - (A.set n).indicator (λ y, c) y‖₊ ∂μ) / μ a)
(v.filter_at x) (𝓝 (‖f x - (A.set n).indicator (λ y, c) x‖₊)),
{ simp_rw [ae_all_iff, ae_ball_iff t_count],
assume n c hc,
apply ae_tendsto_lintegral_div',
{ refine (h'f.sub _).ennnorm,
exact strongly_measurable_const.indicator (is_open.measurable_set (A.set_mem n)) },
{ apply ne_of_lt,
calc ∫⁻ y, ↑‖f y - (A.set n).indicator (λ (y : α), c) y‖₊ ∂μ
≤ ∫⁻ y, (‖f y‖₊ + ‖(A.set n).indicator (λ (y : α), c) y‖₊) ∂μ :
begin
apply lintegral_mono,
assume x,
dsimp,
rw ← ennreal.coe_add,
exact ennreal.coe_le_coe.2 (nnnorm_sub_le _ _),
end
... = ∫⁻ y, ‖f y‖₊ ∂μ + ∫⁻ y, ‖(A.set n).indicator (λ (y : α), c) y‖₊ ∂μ :
lintegral_add_left h'f.ennnorm _
... < ∞ + ∞ :
begin
have I : integrable ((A.set n).indicator (λ (y : α), c)) μ,
by simp only [integrable_indicator_iff (is_open.measurable_set (A.set_mem n)),
integrable_on_const, A.finite n, or_true],
exact ennreal.add_lt_add hf.2 I.2,
end } },
filter_upwards [main, v.ae_eventually_measure_pos] with x hx h'x,
have M : ∀ c ∈ t, tendsto (λ a, (∫⁻ y in a, ‖f y - c‖₊ ∂μ) / μ a)
(v.filter_at x) (𝓝 (‖f x - c‖₊)),
{ assume c hc,
obtain ⟨n, xn⟩ : ∃ n, x ∈ A.set n, by simpa [← A.spanning] using mem_univ x,
specialize hx n c hc,
simp only [xn, indicator_of_mem] at hx,
apply hx.congr' _,
filter_upwards [v.eventually_filter_at_subset_of_nhds (is_open.mem_nhds (A.set_mem n) xn),
v.eventually_filter_at_measurable_set]
with a ha h'a,
congr' 1,
apply set_lintegral_congr_fun h'a,
apply eventually_of_forall (λ y, _),
assume hy,
simp only [ha hy, indicator_of_mem] },
apply ennreal.tendsto_nhds_zero.2 (λ ε εpos, _),
obtain ⟨c, ct, xc⟩ : ∃ c ∈ t, (‖f x - c‖₊ : ℝ≥0∞) < ε / 2,
{ simp_rw ← edist_eq_coe_nnnorm_sub,
have : f x ∈ closure t, from ht (mem_range_self _),
exact emetric.mem_closure_iff.1 this (ε / 2) (ennreal.half_pos (ne_of_gt εpos)) },
filter_upwards [(tendsto_order.1 (M c ct)).2 (ε / 2) xc, h'x, v.eventually_measure_lt_top x]
with a ha h'a h''a,
apply ennreal.div_le_of_le_mul,
calc ∫⁻ y in a, ‖f y - f x‖₊ ∂μ
≤ ∫⁻ y in a, ‖f y - c‖₊ + ‖f x - c‖₊ ∂μ :
begin
apply lintegral_mono (λ x, _),
simpa only [← edist_eq_coe_nnnorm_sub] using edist_triangle_right _ _ _,
end
... = ∫⁻ y in a, ‖f y - c‖₊ ∂μ + ∫⁻ y in a, ‖f x - c‖₊ ∂μ :
lintegral_add_right _ measurable_const
... ≤ ε / 2 * μ a + ε / 2 * μ a :
begin
refine add_le_add _ _,
{ rw ennreal.div_lt_iff (or.inl (h'a.ne')) (or.inl (h''a.ne)) at ha,
exact ha.le },
{ simp only [lintegral_const, measure.restrict_apply, measurable_set.univ, univ_inter],
exact mul_le_mul_right' xc.le _ }
end
... = ε * μ a : by rw [← add_mul, ennreal.add_halves]
end
lemma ae_tendsto_lintegral_nnnorm_sub_div {f : α → E} (hf : integrable f μ) :
∀ᵐ x ∂μ, tendsto (λ a, (∫⁻ y in a, ‖f y - f x‖₊ ∂μ) / μ a) (v.filter_at x) (𝓝 0) :=
begin
have I : integrable (hf.1.mk f) μ, from hf.congr hf.1.ae_eq_mk,
filter_upwards [v.ae_tendsto_lintegral_nnnorm_sub_div' I hf.1.strongly_measurable_mk,
hf.1.ae_eq_mk] with x hx h'x,
apply hx.congr _,
assume a,
congr' 1,
apply lintegral_congr_ae,
apply ae_restrict_of_ae,
filter_upwards [hf.1.ae_eq_mk] with y hy,
rw [hy, h'x]
end
/-- *Lebesgue differentiation theorem*: for almost every point `x`, the
average of `‖f y - f x‖` on `a` tends to `0` as `a` shrinks to `x` along a Vitali family.-/
lemma ae_tendsto_average_norm_sub {f : α → E} (hf : integrable f μ) :
∀ᵐ x ∂μ, tendsto (λ a, ⨍ y in a, ‖f y - f x‖ ∂μ) (v.filter_at x) (𝓝 0) :=
begin
filter_upwards [v.ae_tendsto_lintegral_nnnorm_sub_div hf, v.ae_eventually_measure_pos]
with x hx h'x,
have := (ennreal.tendsto_to_real ennreal.zero_ne_top).comp hx,
simp only [ennreal.zero_to_real] at this,
apply tendsto.congr' _ this,
filter_upwards [h'x, v.eventually_measure_lt_top x] with a ha h'a,
simp only [function.comp_app, ennreal.to_real_div, set_average_eq, div_eq_inv_mul],
have A : integrable_on (λ y, (‖f y - f x‖₊ : ℝ)) a μ,
{ simp_rw [coe_nnnorm],
exact (hf.integrable_on.sub (integrable_on_const.2 (or.inr h'a))).norm },
rw [lintegral_coe_eq_integral _ A, ennreal.to_real_of_real],
{ simp_rw [coe_nnnorm],
refl },
{ apply integral_nonneg,
assume x,
exact nnreal.coe_nonneg _ }
end
/-- *Lebesgue differentiation theorem*: for almost every point `x`, the
average of `f` on `a` tends to `f x` as `a` shrinks to `x` along a Vitali family.-/
lemma ae_tendsto_average [normed_space ℝ E] [complete_space E] {f : α → E} (hf : integrable f μ) :
∀ᵐ x ∂μ, tendsto (λ a, ⨍ y in a, f y ∂μ) (v.filter_at x) (𝓝 (f x)) :=
begin
filter_upwards [v.ae_tendsto_average_norm_sub hf, v.ae_eventually_measure_pos] with x hx h'x,
rw tendsto_iff_norm_tendsto_zero,
refine squeeze_zero' (eventually_of_forall (λ a, norm_nonneg _)) _ hx,
filter_upwards [h'x, v.eventually_measure_lt_top x] with a ha h'a,
nth_rewrite 0 [← set_average_const ha.ne' h'a.ne (f x)],
simp_rw [set_average_eq'],
rw ← integral_sub,
{ exact norm_integral_le_integral_norm _ },
{ exact (integrable_inv_smul_measure ha.ne' h'a.ne).2 hf.integrable_on },
{ exact (integrable_inv_smul_measure ha.ne' h'a.ne).2 (integrable_on_const.2 (or.inr h'a)) }
end
end
end vitali_family
|
475864f28eec3a4cb7750eecf1c0f0f65aa316b7 | 5ae26df177f810c5006841e9c73dc56e01b978d7 | /src/category_theory/monad/algebra.lean | f29bf1660d10dbb346cefdc206305c7adf01fea4 | [
"Apache-2.0"
] | permissive | ChrisHughes24/mathlib | 98322577c460bc6b1fe5c21f42ce33ad1c3e5558 | a2a867e827c2a6702beb9efc2b9282bd801d5f9a | refs/heads/master | 1,583,848,251,477 | 1,565,164,247,000 | 1,565,164,247,000 | 129,409,993 | 0 | 1 | Apache-2.0 | 1,565,164,817,000 | 1,523,628,059,000 | Lean | UTF-8 | Lean | false | false | 3,837 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.monad.basic
import category_theory.adjunction.basic
/-!
# Eilenberg-Moore algebras for a monad
This file defines Eilenberg-Moore algebras for a monad, and provides the category instance for them.
Further it defines the adjoint pair of free and forgetful functors, respectively
from and to the original category.
## References
* [Riehl, *Category theory in context*, Section 5.2.4][riehl2017]
-/
namespace category_theory
open category
universes v₁ u₁ -- declare the `v`'s first; see `category_theory.category` for an explanation
variables {C : Type u₁} [𝒞 : category.{v₁} C]
include 𝒞
namespace monad
/-- An Eilenberg-Moore algebra for a monad `T`.
cf Definition 5.2.3 in [Riehl][riehl2017]. -/
structure algebra (T : C ⥤ C) [monad.{v₁} T] : Type (max u₁ v₁) :=
(A : C)
(a : T.obj A ⟶ A)
(unit' : (η_ T).app A ≫ a = 𝟙 A . obviously)
(assoc' : ((μ_ T).app A ≫ a) = (T.map a ≫ a) . obviously)
restate_axiom algebra.unit'
restate_axiom algebra.assoc'
namespace algebra
variables {T : C ⥤ C} [monad.{v₁} T]
structure hom (A B : algebra T) :=
(f : A.A ⟶ B.A)
(h' : T.map f ≫ B.a = A.a ≫ f . obviously)
restate_axiom hom.h'
attribute [simp] hom.h
namespace hom
@[extensionality] lemma ext {A B : algebra T} (f g : hom A B) (w : f.f = g.f) : f = g :=
by { cases f, cases g, congr, assumption }
def id (A : algebra T) : hom A A :=
{ f := 𝟙 A.A }
@[simp] lemma id_f (A : algebra T) : (id A).f = 𝟙 A.A := rfl
def comp {P Q R : algebra T} (f : hom P Q) (g : hom Q R) : hom P R :=
{ f := f.f ≫ g.f,
h' := by rw [functor.map_comp, category.assoc, g.h, ←category.assoc, f.h, category.assoc] }
@[simp] lemma comp_f {P Q R : algebra T} (f : hom P Q) (g : hom Q R) : (comp f g).f = f.f ≫ g.f := rfl
end hom
/-- The category of Eilenberg-Moore algebras for a monad.
cf Definition 5.2.4 in [Riehl][riehl2017]. -/
instance EilenbergMoore : category (algebra T) :=
{ hom := hom,
id := hom.id,
comp := @hom.comp _ _ _ _ }
@[simp] lemma id_f (P : algebra T) : hom.f (𝟙 P) = 𝟙 P.A := rfl
@[simp] lemma comp_f {P Q R : algebra T} (f : P ⟶ Q) (g : Q ⟶ R) : (f ≫ g).f = f.f ≫ g.f := rfl
end algebra
variables (T : C ⥤ C) [monad.{v₁} T]
def forget : algebra T ⥤ C :=
{ obj := λ A, A.A,
map := λ A B f, f.f }
@[simp] lemma forget_map {X Y : algebra T} (f : X ⟶ Y) : (forget T).map f = f.f := rfl
def free : C ⥤ algebra T :=
{ obj := λ X,
{ A := T.obj X,
a := (μ_ T).app X,
assoc' := (monad.assoc T _).symm },
map := λ X Y f,
{ f := T.map f,
h' := by erw (μ_ T).naturality } }
@[simp] lemma free_obj_a (X) : ((free T).obj X).a = (μ_ T).app X := rfl
@[simp] lemma free_map_f {X Y : C} (f : X ⟶ Y) : ((free T).map f).f = T.map f := rfl
/-- The adjunction between the free and forgetful constructions for Eilenberg-Moore algebras for a monad.
cf Lemma 5.2.8 of [Riehl][riehl2017]. -/
def adj : free T ⊣ forget T :=
adjunction.mk_of_hom_equiv
{ hom_equiv := λ X Y,
{ to_fun := λ f, (η_ T).app X ≫ f.f,
inv_fun := λ f,
{ f := T.map f ≫ Y.a,
h' :=
begin
dsimp, simp,
conv { to_rhs, rw [←category.assoc, ←(μ_ T).naturality, category.assoc], erw algebra.assoc },
refl,
end },
left_inv := λ f,
begin
ext1, dsimp,
simp only [free_obj_a, functor.map_comp, algebra.hom.h, category.assoc],
erw [←category.assoc, monad.right_unit, id_comp],
end,
right_inv := λ f,
begin
dsimp,
erw [←category.assoc, ←(η_ T).naturality, functor.id_map,
category.assoc, Y.unit, comp_id],
end }}
end monad
end category_theory
|
3b7dada17d02d712336628d2e92256a8bcb7adf9 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/analysis/special_functions/trigonometric/arctan_deriv.lean | 48990ebb7bde6468971a2e90b4a1dc28c777b107 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 7,626 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Benjamin Davidson
-/
import analysis.special_functions.trigonometric.arctan
import analysis.special_functions.trigonometric.complex_deriv
/-!
# Derivatives of the `tan` and `arctan` functions.
Continuity and derivatives of the tangent and arctangent functions.
-/
noncomputable theory
namespace real
open set filter
open_locale topological_space real
lemma has_strict_deriv_at_tan {x : ℝ} (h : cos x ≠ 0) :
has_strict_deriv_at tan (1 / (cos x)^2) x :=
by exact_mod_cast (complex.has_strict_deriv_at_tan (by exact_mod_cast h)).real_of_complex
lemma has_deriv_at_tan {x : ℝ} (h : cos x ≠ 0) :
has_deriv_at tan (1 / (cos x)^2) x :=
by exact_mod_cast (complex.has_deriv_at_tan (by exact_mod_cast h)).real_of_complex
lemma tendsto_abs_tan_of_cos_eq_zero {x : ℝ} (hx : cos x = 0) :
tendsto (λ x, abs (tan x)) (𝓝[≠] x) at_top :=
begin
have hx : complex.cos x = 0, by exact_mod_cast hx,
simp only [← complex.abs_of_real, complex.of_real_tan],
refine (complex.tendsto_abs_tan_of_cos_eq_zero hx).comp _,
refine tendsto.inf complex.continuous_of_real.continuous_at _,
exact tendsto_principal_principal.2 (λ y, mt complex.of_real_inj.1)
end
lemma tendsto_abs_tan_at_top (k : ℤ) :
tendsto (λ x, abs (tan x)) (𝓝[≠] ((2 * k + 1) * π / 2)) at_top :=
tendsto_abs_tan_of_cos_eq_zero $ cos_eq_zero_iff.2 ⟨k, rfl⟩
lemma continuous_at_tan {x : ℝ} : continuous_at tan x ↔ cos x ≠ 0 :=
begin
refine ⟨λ hc h₀, _, λ h, (has_deriv_at_tan h).continuous_at⟩,
exact not_tendsto_nhds_of_tendsto_at_top (tendsto_abs_tan_of_cos_eq_zero h₀) _
(hc.norm.tendsto.mono_left inf_le_left)
end
lemma differentiable_at_tan {x : ℝ} : differentiable_at ℝ tan x ↔ cos x ≠ 0 :=
⟨λ h, continuous_at_tan.1 h.continuous_at, λ h, (has_deriv_at_tan h).differentiable_at⟩
@[simp] lemma deriv_tan (x : ℝ) : deriv tan x = 1 / (cos x)^2 :=
if h : cos x = 0 then
have ¬differentiable_at ℝ tan x := mt differentiable_at_tan.1 (not_not.2 h),
by simp [deriv_zero_of_not_differentiable_at this, h, sq]
else (has_deriv_at_tan h).deriv
@[simp] lemma cont_diff_at_tan {n x} : cont_diff_at ℝ n tan x ↔ cos x ≠ 0 :=
⟨λ h, continuous_at_tan.1 h.continuous_at,
λ h, (complex.cont_diff_at_tan.2 $ by exact_mod_cast h).real_of_complex⟩
lemma has_deriv_at_tan_of_mem_Ioo {x : ℝ} (h : x ∈ Ioo (-(π/2):ℝ) (π/2)) :
has_deriv_at tan (1 / (cos x)^2) x :=
has_deriv_at_tan (cos_pos_of_mem_Ioo h).ne'
lemma differentiable_at_tan_of_mem_Ioo {x : ℝ} (h : x ∈ Ioo (-(π/2):ℝ) (π/2)) :
differentiable_at ℝ tan x :=
(has_deriv_at_tan_of_mem_Ioo h).differentiable_at
lemma has_strict_deriv_at_arctan (x : ℝ) : has_strict_deriv_at arctan (1 / (1 + x^2)) x :=
have A : cos (arctan x) ≠ 0 := (cos_arctan_pos x).ne',
by simpa [cos_sq_arctan]
using tan_local_homeomorph.has_strict_deriv_at_symm trivial (by simpa) (has_strict_deriv_at_tan A)
lemma has_deriv_at_arctan (x : ℝ) : has_deriv_at arctan (1 / (1 + x^2)) x :=
(has_strict_deriv_at_arctan x).has_deriv_at
lemma differentiable_at_arctan (x : ℝ) : differentiable_at ℝ arctan x :=
(has_deriv_at_arctan x).differentiable_at
lemma differentiable_arctan : differentiable ℝ arctan := differentiable_at_arctan
@[simp] lemma deriv_arctan : deriv arctan = (λ x, 1 / (1 + x^2)) :=
funext $ λ x, (has_deriv_at_arctan x).deriv
lemma cont_diff_arctan {n : with_top ℕ} : cont_diff ℝ n arctan :=
cont_diff_iff_cont_diff_at.2 $ λ x,
have cos (arctan x) ≠ 0 := (cos_arctan_pos x).ne',
tan_local_homeomorph.cont_diff_at_symm_deriv (by simpa) trivial (has_deriv_at_tan this)
(cont_diff_at_tan.2 this)
end real
section
/-!
### Lemmas for derivatives of the composition of `real.arctan` with a differentiable function
In this section we register lemmas for the derivatives of the composition of `real.arctan` with a
differentiable function, for standalone use and use with `simp`. -/
open real
section deriv
variables {f : ℝ → ℝ} {f' x : ℝ} {s : set ℝ}
lemma has_strict_deriv_at.arctan (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) * f') x :=
(real.has_strict_deriv_at_arctan (f x)).comp x hf
lemma has_deriv_at.arctan (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) * f') x :=
(real.has_deriv_at_arctan (f x)).comp x hf
lemma has_deriv_within_at.arctan (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) * f') s x :=
(real.has_deriv_at_arctan (f x)).comp_has_deriv_within_at x hf
lemma deriv_within_arctan (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
deriv_within (λ x, arctan (f x)) s x = (1 / (1 + (f x)^2)) * (deriv_within f s x) :=
hf.has_deriv_within_at.arctan.deriv_within hxs
@[simp] lemma deriv_arctan (hc : differentiable_at ℝ f x) :
deriv (λ x, arctan (f x)) x = (1 / (1 + (f x)^2)) * (deriv f x) :=
hc.has_deriv_at.arctan.deriv
end deriv
section fderiv
variables {E : Type*} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {f' : E →L[ℝ] ℝ} {x : E}
{s : set E} {n : with_top ℕ}
lemma has_strict_fderiv_at.arctan (hf : has_strict_fderiv_at f f' x) :
has_strict_fderiv_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) • f') x :=
(has_strict_deriv_at_arctan (f x)).comp_has_strict_fderiv_at x hf
lemma has_fderiv_at.arctan (hf : has_fderiv_at f f' x) :
has_fderiv_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) • f') x :=
(has_deriv_at_arctan (f x)).comp_has_fderiv_at x hf
lemma has_fderiv_within_at.arctan (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) • f') s x :=
(has_deriv_at_arctan (f x)).comp_has_fderiv_within_at x hf
lemma fderiv_within_arctan (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
fderiv_within ℝ (λ x, arctan (f x)) s x = (1 / (1 + (f x)^2)) • (fderiv_within ℝ f s x) :=
hf.has_fderiv_within_at.arctan.fderiv_within hxs
@[simp] lemma fderiv_arctan (hc : differentiable_at ℝ f x) :
fderiv ℝ (λ x, arctan (f x)) x = (1 / (1 + (f x)^2)) • (fderiv ℝ f x) :=
hc.has_fderiv_at.arctan.fderiv
lemma differentiable_within_at.arctan (hf : differentiable_within_at ℝ f s x) :
differentiable_within_at ℝ (λ x, real.arctan (f x)) s x :=
hf.has_fderiv_within_at.arctan.differentiable_within_at
@[simp] lemma differentiable_at.arctan (hc : differentiable_at ℝ f x) :
differentiable_at ℝ (λ x, arctan (f x)) x :=
hc.has_fderiv_at.arctan.differentiable_at
lemma differentiable_on.arctan (hc : differentiable_on ℝ f s) :
differentiable_on ℝ (λ x, arctan (f x)) s :=
λ x h, (hc x h).arctan
@[simp] lemma differentiable.arctan (hc : differentiable ℝ f) :
differentiable ℝ (λ x, arctan (f x)) :=
λ x, (hc x).arctan
lemma cont_diff_at.arctan (h : cont_diff_at ℝ n f x) :
cont_diff_at ℝ n (λ x, arctan (f x)) x :=
cont_diff_arctan.cont_diff_at.comp x h
lemma cont_diff.arctan (h : cont_diff ℝ n f) :
cont_diff ℝ n (λ x, arctan (f x)) :=
cont_diff_arctan.comp h
lemma cont_diff_within_at.arctan (h : cont_diff_within_at ℝ n f s x) :
cont_diff_within_at ℝ n (λ x, arctan (f x)) s x :=
cont_diff_arctan.comp_cont_diff_within_at h
lemma cont_diff_on.arctan (h : cont_diff_on ℝ n f s) :
cont_diff_on ℝ n (λ x, arctan (f x)) s :=
cont_diff_arctan.comp_cont_diff_on h
end fderiv
end
|
004f15286c0b0b0525df16ac8b10e96904e1856b | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/topology/continuous_on.lean | f595de0b5d37530c81a7657381d970b16f03cd80 | [
"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 | 24,099 | lean | /-
Copyright (c) 2019 Reid Barton. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import topology.constructions
/-!
# Neighborhoods and continuity relative to a subset
This file defines relative versions
`nhds_within` of `nhds`
`continuous_on` of `continuous`
`continuous_within_at` of `continuous_at`
and proves their basic properties, including the relationships between
these restricted notions and the corresponding notions for the subtype
equipped with the subspace topology.
-/
open set filter
open_locale topological_space
variables {α : Type*} {β : Type*} {γ : Type*}
variables [topological_space α]
/-- The "neighborhood within" filter. Elements of `nhds_within a s` are sets containing the
intersection of `s` and a neighborhood of `a`. -/
def nhds_within (a : α) (s : set α) : filter α := 𝓝 a ⊓ principal s
theorem nhds_within_eq (a : α) (s : set α) :
nhds_within a s = ⨅ t ∈ {t : set α | a ∈ t ∧ is_open t}, principal (t ∩ s) :=
have set.univ ∈ {s : set α | a ∈ s ∧ is_open s}, from ⟨set.mem_univ _, is_open_univ⟩,
begin
rw [nhds_within, nhds, binfi_inf]; try { exact this },
simp only [inf_principal]
end
theorem nhds_within_univ (a : α) : nhds_within a set.univ = 𝓝 a :=
by rw [nhds_within, principal_univ, inf_top_eq]
lemma nhds_within_has_basis {p : β → Prop} {s : β → set α} {a : α} (h : (𝓝 a).has_basis p s)
(t : set α) :
(nhds_within a t).has_basis p (λ i, s i ∩ t) :=
h.inf_principal t
lemma nhds_within_basis_open (a : α) (t : set α) :
(nhds_within a t).has_basis (λ u, a ∈ u ∧ is_open u) (λ u, u ∩ t) :=
nhds_within_has_basis (nhds_basis_opens a) t
theorem mem_nhds_within {t : set α} {a : α} {s : set α} :
t ∈ nhds_within a s ↔ ∃ u, is_open u ∧ a ∈ u ∧ u ∩ s ⊆ t :=
by simpa only [exists_prop, and_assoc, and_comm] using nhds_within_basis_open a s t
lemma mem_nhds_within_iff_exists_mem_nhds_inter {t : set α} {a : α} {s : set α} :
t ∈ nhds_within a s ↔ ∃ u ∈ 𝓝 a, u ∩ s ⊆ t :=
nhds_within_has_basis (𝓝 a).basis_sets s t
lemma mem_nhds_within_of_mem_nhds {s t : set α} {a : α} (h : s ∈ 𝓝 a) :
s ∈ nhds_within a t :=
mem_inf_sets_of_left h
theorem self_mem_nhds_within {a : α} {s : set α} : s ∈ nhds_within a s :=
mem_inf_sets_of_right (mem_principal_self s)
theorem inter_mem_nhds_within (s : set α) {t : set α} {a : α} (h : t ∈ 𝓝 a) :
s ∩ t ∈ nhds_within a s :=
inter_mem_sets (mem_inf_sets_of_right (mem_principal_self s)) (mem_inf_sets_of_left h)
theorem nhds_within_mono (a : α) {s t : set α} (h : s ⊆ t) : nhds_within a s ≤ nhds_within a t :=
inf_le_inf (le_refl _) (principal_mono.mpr h)
lemma mem_of_mem_nhds_within {a : α} {s t : set α} (ha : a ∈ s) (ht : t ∈ nhds_within a s) :
a ∈ t :=
let ⟨u, hu, H⟩ := mem_nhds_within.1 ht in H.2 ⟨H.1, ha⟩
theorem nhds_within_restrict'' {a : α} (s : set α) {t : set α} (h : t ∈ nhds_within a s) :
nhds_within a s = nhds_within a (s ∩ t) :=
le_antisymm
(le_inf inf_le_left (le_principal_iff.mpr (inter_mem_sets self_mem_nhds_within h)))
(inf_le_inf (le_refl _) (principal_mono.mpr (set.inter_subset_left _ _)))
theorem nhds_within_restrict' {a : α} (s : set α) {t : set α} (h : t ∈ 𝓝 a) :
nhds_within a s = nhds_within a (s ∩ t) :=
nhds_within_restrict'' s $ mem_inf_sets_of_left h
theorem nhds_within_restrict {a : α} (s : set α) {t : set α} (h₀ : a ∈ t) (h₁ : is_open t) :
nhds_within a s = nhds_within a (s ∩ t) :=
nhds_within_restrict' s (mem_nhds_sets h₁ h₀)
theorem nhds_within_le_of_mem {a : α} {s t : set α} (h : s ∈ nhds_within a t) :
nhds_within a t ≤ nhds_within a s :=
begin
rcases mem_nhds_within.1 h with ⟨u, u_open, au, uts⟩,
have : nhds_within a t = nhds_within a (t ∩ u) := nhds_within_restrict _ au u_open,
rw [this, inter_comm],
exact nhds_within_mono _ uts
end
theorem nhds_within_eq_nhds_within {a : α} {s t u : set α}
(h₀ : a ∈ s) (h₁ : is_open s) (h₂ : t ∩ s = u ∩ s) :
nhds_within a t = nhds_within a u :=
by rw [nhds_within_restrict t h₀ h₁, nhds_within_restrict u h₀ h₁, h₂]
theorem nhds_within_eq_of_open {a : α} {s : set α} (h₀ : a ∈ s) (h₁ : is_open s) :
nhds_within a s = 𝓝 a :=
by rw [←nhds_within_univ]; apply nhds_within_eq_nhds_within h₀ h₁;
rw [set.univ_inter, set.inter_self]
@[simp] theorem nhds_within_empty (a : α) : nhds_within a {} = ⊥ :=
by rw [nhds_within, principal_empty, inf_bot_eq]
theorem nhds_within_union (a : α) (s t : set α) :
nhds_within a (s ∪ t) = nhds_within a s ⊔ nhds_within a t :=
by unfold nhds_within; rw [←inf_sup_left, sup_principal]
theorem nhds_within_inter (a : α) (s t : set α) :
nhds_within a (s ∩ t) = nhds_within a s ⊓ nhds_within a t :=
by unfold nhds_within; rw [inf_left_comm, inf_assoc, inf_principal,
←inf_assoc, inf_idem]
theorem nhds_within_inter' (a : α) (s t : set α) :
nhds_within a (s ∩ t) = (nhds_within a s) ⊓ principal t :=
by { unfold nhds_within, rw [←inf_principal, inf_assoc] }
lemma nhds_within_prod_eq {α : Type*} [topological_space α] {β : Type*} [topological_space β]
(a : α) (b : β) (s : set α) (t : set β) :
nhds_within (a, b) (s.prod t) = (nhds_within a s).prod (nhds_within b t) :=
by { unfold nhds_within, rw [nhds_prod_eq, ←filter.prod_inf_prod, filter.prod_principal_principal] }
theorem tendsto_if_nhds_within {f g : α → β} {p : α → Prop} [decidable_pred p]
{a : α} {s : set α} {l : filter β}
(h₀ : tendsto f (nhds_within a (s ∩ p)) l)
(h₁ : tendsto g (nhds_within a (s ∩ {x | ¬ p x})) l) :
tendsto (λ x, if p x then f x else g x) (nhds_within a s) l :=
by apply tendsto_if; rw [←nhds_within_inter']; assumption
lemma map_nhds_within (f : α → β) (a : α) (s : set α) :
map f (nhds_within a s) =
⨅ t ∈ {t : set α | a ∈ t ∧ is_open t}, principal (set.image f (t ∩ s)) :=
((nhds_within_basis_open a s).map f).eq_binfi
theorem tendsto_nhds_within_mono_left {f : α → β} {a : α}
{s t : set α} {l : filter β} (hst : s ⊆ t) (h : tendsto f (nhds_within a t) l) :
tendsto f (nhds_within a s) l :=
tendsto_le_left (nhds_within_mono a hst) h
theorem tendsto_nhds_within_mono_right {f : β → α} {l : filter β}
{a : α} {s t : set α} (hst : s ⊆ t) (h : tendsto f l (nhds_within a s)) :
tendsto f l (nhds_within a t) :=
tendsto_le_right (nhds_within_mono a hst) h
theorem tendsto_nhds_within_of_tendsto_nhds {f : α → β} {a : α}
{s : set α} {l : filter β} (h : tendsto f (𝓝 a) l) :
tendsto f (nhds_within a s) l :=
by rw [←nhds_within_univ] at h; exact tendsto_nhds_within_mono_left (set.subset_univ _) h
theorem principal_subtype {α : Type*} (s : set α) (t : set {x // x ∈ s}) :
principal t = comap subtype.val (principal (subtype.val '' t)) :=
by rw comap_principal; rw set.preimage_image_eq; apply subtype.val_injective
lemma mem_closure_iff_nhds_within_ne_bot {s : set α} {x : α} :
x ∈ closure s ↔ nhds_within x s ≠ ⊥ :=
mem_closure_iff_nhds.trans (nhds_within_has_basis (𝓝 x).basis_sets s).forall_nonempty_iff_ne_bot
lemma nhds_within_ne_bot_of_mem {s : set α} {x : α} (hx : x ∈ s) :
nhds_within x s ≠ ⊥ :=
mem_closure_iff_nhds_within_ne_bot.1 $ subset_closure hx
lemma is_closed.mem_of_nhds_within_ne_bot {s : set α} (hs : is_closed s)
{x : α} (hx : nhds_within x s ≠ ⊥) : x ∈ s :=
by simpa only [closure_eq_of_is_closed hs] using mem_closure_iff_nhds_within_ne_bot.2 hx
/-
nhds_within and subtypes
-/
theorem mem_nhds_within_subtype (s : set α) (a : {x // x ∈ s}) (t u : set {x // x ∈ s}) :
t ∈ nhds_within a u ↔
t ∈ comap (@subtype.val _ s) (nhds_within a.val (subtype.val '' u)) :=
by rw [nhds_within, nhds_subtype, principal_subtype, ←comap_inf, ←nhds_within]
theorem nhds_within_subtype (s : set α) (a : {x // x ∈ s}) (t : set {x // x ∈ s}) :
nhds_within a t = comap (@subtype.val _ s) (nhds_within a.val (subtype.val '' t)) :=
filter_eq $ by ext u; rw mem_nhds_within_subtype
theorem nhds_within_eq_map_subtype_val {s : set α} {a : α} (h : a ∈ s) :
nhds_within a s = map subtype.val (𝓝 ⟨a, h⟩) :=
have h₀ : s ∈ nhds_within a s,
by { rw [mem_nhds_within], existsi set.univ, simp [set.diff_eq] },
have h₁ : ∀ y ∈ s, ∃ x, @subtype.val _ s x = y,
from λ y h, ⟨⟨y, h⟩, rfl⟩,
begin
rw [←nhds_within_univ, nhds_within_subtype, subtype.val_image_univ],
exact (map_comap_of_surjective' h₀ h₁).symm,
end
theorem tendsto_nhds_within_iff_subtype {s : set α} {a : α} (h : a ∈ s) (f : α → β) (l : filter β) :
tendsto f (nhds_within a s) l ↔ tendsto (s.restrict f) (𝓝 ⟨a, h⟩) l :=
by { simp only [tendsto, nhds_within_eq_map_subtype_val h, filter.map_map], refl }
variables [topological_space β] [topological_space γ]
/-- A function between topological spaces is continuous at a point `x₀` within a subset `s`
if `f x` tends to `f x₀` when `x` tends to `x₀` while staying within `s`. -/
def continuous_within_at (f : α → β) (s : set α) (x : α) : Prop :=
tendsto f (nhds_within x s) (𝓝 (f x))
/-- If a function is continuous within `s` at `x`, then it tends to `f x` within `s` by definition.
We register this fact for use with the dot notation, especially to use `tendsto.comp` as
`continuous_within_at.comp` will have a different meaning. -/
lemma continuous_within_at.tendsto {f : α → β} {s : set α} {x : α} (h : continuous_within_at f s x) :
tendsto f (nhds_within x s) (𝓝 (f x)) := h
/-- A function between topological spaces is continuous on a subset `s`
when it's continuous at every point of `s` within `s`. -/
def continuous_on (f : α → β) (s : set α) : Prop := ∀ x ∈ s, continuous_within_at f s x
lemma continuous_on.continuous_within_at {f : α → β} {s : set α} {x : α} (hf : continuous_on f s)
(hx : x ∈ s) : continuous_within_at f s x :=
hf x hx
theorem continuous_within_at_univ (f : α → β) (x : α) :
continuous_within_at f set.univ x ↔ continuous_at f x :=
by rw [continuous_at, continuous_within_at, nhds_within_univ]
theorem continuous_within_at_iff_continuous_at_restrict (f : α → β) {x : α} {s : set α} (h : x ∈ s) :
continuous_within_at f s x ↔ continuous_at (s.restrict f) ⟨x, h⟩ :=
tendsto_nhds_within_iff_subtype h f _
theorem continuous_within_at.tendsto_nhds_within_image {f : α → β} {x : α} {s : set α}
(h : continuous_within_at f s x) :
tendsto f (nhds_within x s) (nhds_within (f x) (f '' s)) :=
tendsto_inf.2 ⟨h, tendsto_principal.2 $
mem_inf_sets_of_right $ mem_principal_sets.2 $
λ x, mem_image_of_mem _⟩
theorem continuous_on_iff {f : α → β} {s : set α} :
continuous_on f s ↔ ∀ x ∈ s, ∀ t : set β, is_open t → f x ∈ t → ∃ u, is_open u ∧ x ∈ u ∧
u ∩ s ⊆ f ⁻¹' t :=
by simp only [continuous_on, continuous_within_at, tendsto_nhds, mem_nhds_within]
theorem continuous_on_iff_continuous_restrict {f : α → β} {s : set α} :
continuous_on f s ↔ continuous (s.restrict f) :=
begin
rw [continuous_on, continuous_iff_continuous_at], split,
{ rintros h ⟨x, xs⟩,
exact (continuous_within_at_iff_continuous_at_restrict f xs).mp (h x xs) },
intros h x xs,
exact (continuous_within_at_iff_continuous_at_restrict f xs).mpr (h ⟨x, xs⟩)
end
theorem continuous_on_iff' {f : α → β} {s : set α} :
continuous_on f s ↔ ∀ t : set β, is_open t → ∃ u, is_open u ∧ f ⁻¹' t ∩ s = u ∩ s :=
have ∀ t, is_open (s.restrict f ⁻¹' t) ↔ ∃ (u : set α), is_open u ∧ f ⁻¹' t ∩ s = u ∩ s,
begin
intro t,
rw [is_open_induced_iff, set.restrict_eq, set.preimage_comp],
simp only [preimage_coe_eq_preimage_coe_iff],
split; { rintros ⟨u, ou, useq⟩, exact ⟨u, ou, useq.symm⟩ }
end,
by rw [continuous_on_iff_continuous_restrict, continuous]; simp only [this]
theorem continuous_on_iff_is_closed {f : α → β} {s : set α} :
continuous_on f s ↔ ∀ t : set β, is_closed t → ∃ u, is_closed u ∧ f ⁻¹' t ∩ s = u ∩ s :=
have ∀ t, is_closed (s.restrict f ⁻¹' t) ↔ ∃ (u : set α), is_closed u ∧ f ⁻¹' t ∩ s = u ∩ s,
begin
intro t,
rw [is_closed_induced_iff, set.restrict_eq, set.preimage_comp],
simp only [preimage_coe_eq_preimage_coe_iff]
end,
by rw [continuous_on_iff_continuous_restrict, continuous_iff_is_closed]; simp only [this]
lemma continuous_on_empty (f : α → β) : continuous_on f ∅ :=
λ x, false.elim
theorem nhds_within_le_comap {x : α} {s : set α} {f : α → β} (ctsf : continuous_within_at f s x) :
nhds_within x s ≤ comap f (nhds_within (f x) (f '' s)) :=
map_le_iff_le_comap.1 ctsf.tendsto_nhds_within_image
theorem continuous_within_at_iff_ptendsto_res (f : α → β) {x : α} {s : set α} :
continuous_within_at f s x ↔ ptendsto (pfun.res f s) (𝓝 x) (𝓝 (f x)) :=
tendsto_iff_ptendsto _ _ _ _
lemma continuous_iff_continuous_on_univ {f : α → β} : continuous f ↔ continuous_on f univ :=
by simp [continuous_iff_continuous_at, continuous_on, continuous_at, continuous_within_at,
nhds_within_univ]
lemma continuous_within_at.mono {f : α → β} {s t : set α} {x : α} (h : continuous_within_at f t x)
(hs : s ⊆ t) : continuous_within_at f s x :=
tendsto_le_left (nhds_within_mono x hs) h
lemma continuous_within_at_inter' {f : α → β} {s t : set α} {x : α} (h : t ∈ nhds_within x s) :
continuous_within_at f (s ∩ t) x ↔ continuous_within_at f s x :=
by simp [continuous_within_at, nhds_within_restrict'' s h]
lemma continuous_within_at_inter {f : α → β} {s t : set α} {x : α} (h : t ∈ 𝓝 x) :
continuous_within_at f (s ∩ t) x ↔ continuous_within_at f s x :=
by simp [continuous_within_at, nhds_within_restrict' s h]
lemma continuous_within_at.union {f : α → β} {s t : set α} {x : α}
(hs : continuous_within_at f s x) (ht : continuous_within_at f t x) :
continuous_within_at f (s ∪ t) x :=
by simp only [continuous_within_at, nhds_within_union, tendsto, map_sup, sup_le_iff.2 ⟨hs, ht⟩]
lemma continuous_within_at.mem_closure_image {f : α → β} {s : set α} {x : α}
(h : continuous_within_at f s x) (hx : x ∈ closure s) : f x ∈ closure (f '' s) :=
mem_closure_of_tendsto (mem_closure_iff_nhds_within_ne_bot.1 hx) h $
mem_sets_of_superset self_mem_nhds_within (subset_preimage_image f s)
lemma continuous_within_at.mem_closure {f : α → β} {s : set α} {x : α} {A : set β}
(h : continuous_within_at f s x) (hx : x ∈ closure s) (hA : s ⊆ f⁻¹' A) : f x ∈ closure A :=
closure_mono (image_subset_iff.2 hA) (h.mem_closure_image hx)
lemma continuous_within_at.image_closure {f : α → β} {s : set α}
(hf : ∀ x ∈ closure s, continuous_within_at f s x) :
f '' (closure s) ⊆ closure (f '' s) :=
begin
rintros _ ⟨x, hx, rfl⟩,
exact (hf x hx).mem_closure_image hx
end
theorem is_open_map.continuous_on_image_of_left_inv_on {f : α → β} {s : set α}
(h : is_open_map (s.restrict f)) {finv : β → α} (hleft : left_inv_on finv f s) :
continuous_on finv (f '' s) :=
begin
rintros _ ⟨x, xs, rfl⟩ t ht,
rw [hleft xs] at ht,
replace h := h.nhds_le ⟨x, xs⟩,
apply mem_nhds_within_of_mem_nhds,
apply h,
erw [map_compose.symm, function.comp, mem_map, ← nhds_within_eq_map_subtype_val],
apply mem_sets_of_superset (inter_mem_nhds_within _ ht),
assume y hy,
rw [mem_set_of_eq, mem_preimage, hleft hy.1],
exact hy.2
end
theorem is_open_map.continuous_on_range_of_left_inverse {f : α → β} (hf : is_open_map f)
{finv : β → α} (hleft : function.left_inverse finv f) :
continuous_on finv (range f) :=
begin
rw [← image_univ],
exact (hf.restrict is_open_univ).continuous_on_image_of_left_inv_on (λ x _, hleft x)
end
lemma continuous_on.congr_mono {f g : α → β} {s s₁ : set α} (h : continuous_on f s)
(h' : ∀x ∈ s₁, g x = f x) (h₁ : s₁ ⊆ s) : continuous_on g s₁ :=
begin
assume x hx,
unfold continuous_within_at,
have A := (h x (h₁ hx)).mono h₁,
unfold continuous_within_at at A,
rw ← h' x hx at A,
have : {x : α | g x = f x} ∈ nhds_within x s₁ := mem_inf_sets_of_right h',
apply tendsto.congr' _ A,
convert this,
ext,
finish
end
lemma continuous_on.congr {f g : α → β} {s : set α} (h : continuous_on f s)
(h' : ∀x ∈ s, g x = f x) : continuous_on g s :=
h.congr_mono h' (subset.refl _)
lemma continuous_on_congr {f g : α → β} {s : set α} (h' : ∀x ∈ s, g x = f x) :
continuous_on g s ↔ continuous_on f s :=
⟨λ h, continuous_on.congr h (λx hx, (h' x hx).symm), λ h, continuous_on.congr h h'⟩
lemma continuous_at.continuous_within_at {f : α → β} {s : set α} {x : α} (h : continuous_at f x) :
continuous_within_at f s x :=
continuous_within_at.mono ((continuous_within_at_univ f x).2 h) (subset_univ _)
lemma continuous_within_at.continuous_at {f : α → β} {s : set α} {x : α}
(h : continuous_within_at f s x) (hs : s ∈ 𝓝 x) : continuous_at f x :=
begin
have : s = univ ∩ s, by rw univ_inter,
rwa [this, continuous_within_at_inter hs, continuous_within_at_univ] at h
end
lemma continuous_on.continuous_at {f : α → β} {s : set α} {x : α}
(h : continuous_on f s) (hx : s ∈ 𝓝 x) : continuous_at f x :=
(h x (mem_of_nhds hx)).continuous_at hx
lemma continuous_within_at.comp {g : β → γ} {f : α → β} {s : set α} {t : set β} {x : α}
(hg : continuous_within_at g t (f x)) (hf : continuous_within_at f s x) (h : s ⊆ f ⁻¹' t) :
continuous_within_at (g ∘ f) s x :=
begin
have : tendsto f (principal s) (principal t),
by { rw tendsto_principal_principal, exact λx hx, h hx },
have : tendsto f (nhds_within x s) (principal t) :=
tendsto_le_left inf_le_right this,
have : tendsto f (nhds_within x s) (nhds_within (f x) t) :=
tendsto_inf.2 ⟨hf, this⟩,
exact tendsto.comp hg this
end
lemma continuous_on.comp {g : β → γ} {f : α → β} {s : set α} {t : set β}
(hg : continuous_on g t) (hf : continuous_on f s) (h : s ⊆ f ⁻¹' t) :
continuous_on (g ∘ f) s :=
λx hx, continuous_within_at.comp (hg _ (h hx)) (hf x hx) h
lemma continuous_on.mono {f : α → β} {s t : set α} (hf : continuous_on f s) (h : t ⊆ s) :
continuous_on f t :=
λx hx, tendsto_le_left (nhds_within_mono _ h) (hf x (h hx))
lemma continuous.continuous_on {f : α → β} {s : set α} (h : continuous f) :
continuous_on f s :=
begin
rw continuous_iff_continuous_on_univ at h,
exact h.mono (subset_univ _)
end
lemma continuous.continuous_within_at {f : α → β} {s : set α} {x : α} (h : continuous f) :
continuous_within_at f s x :=
tendsto_le_left inf_le_left (h.tendsto x)
lemma continuous.comp_continuous_on {g : β → γ} {f : α → β} {s : set α}
(hg : continuous g) (hf : continuous_on f s) :
continuous_on (g ∘ f) s :=
hg.continuous_on.comp hf subset_preimage_univ
lemma continuous_within_at.preimage_mem_nhds_within {f : α → β} {x : α} {s : set α} {t : set β}
(h : continuous_within_at f s x) (ht : t ∈ 𝓝 (f x)) : f ⁻¹' t ∈ nhds_within x s :=
h ht
lemma continuous_within_at.preimage_mem_nhds_within' {f : α → β} {x : α} {s : set α} {t : set β}
(h : continuous_within_at f s x) (ht : t ∈ nhds_within (f x) (f '' s)) :
f ⁻¹' t ∈ nhds_within x s :=
begin
rw mem_nhds_within at ht,
rcases ht with ⟨u, u_open, fxu, hu⟩,
have : f ⁻¹' u ∩ s ∈ nhds_within x s :=
filter.inter_mem_sets (h (mem_nhds_sets u_open fxu)) self_mem_nhds_within,
apply mem_sets_of_superset this,
calc f ⁻¹' u ∩ s
⊆ f ⁻¹' u ∩ f ⁻¹' (f '' s) : inter_subset_inter_right _ (subset_preimage_image f s)
... = f ⁻¹' (u ∩ f '' s) : rfl
... ⊆ f ⁻¹' t : preimage_mono hu
end
lemma continuous_within_at.congr_of_mem_nhds_within {f f₁ : α → β} {s : set α} {x : α}
(h : continuous_within_at f s x) (h₁ : {y | f₁ y = f y} ∈ nhds_within x s) (hx : f₁ x = f x) :
continuous_within_at f₁ s x :=
by rwa [continuous_within_at, filter.tendsto, hx, filter.map_cong h₁]
lemma continuous_within_at.congr {f f₁ : α → β} {s : set α} {x : α}
(h : continuous_within_at f s x) (h₁ : ∀y∈s, f₁ y = f y) (hx : f₁ x = f x) :
continuous_within_at f₁ s x :=
h.congr_of_mem_nhds_within (mem_sets_of_superset self_mem_nhds_within h₁) hx
lemma continuous_on_const {s : set α} {c : β} : continuous_on (λx, c) s :=
continuous_const.continuous_on
lemma continuous_within_at_const {b : β} {s : set α} {x : α} :
continuous_within_at (λ _:α, b) s x :=
continuous_const.continuous_within_at
lemma continuous_on_id {s : set α} : continuous_on id s :=
continuous_id.continuous_on
lemma continuous_within_at_id {s : set α} {x : α} : continuous_within_at id s x :=
continuous_id.continuous_within_at
lemma continuous_on_open_iff {f : α → β} {s : set α} (hs : is_open s) :
continuous_on f s ↔ (∀t, is_open t → is_open (s ∩ f⁻¹' t)) :=
begin
rw continuous_on_iff',
split,
{ assume h t ht,
rcases h t ht with ⟨u, u_open, hu⟩,
rw [inter_comm, hu],
apply is_open_inter u_open hs },
{ assume h t ht,
refine ⟨s ∩ f ⁻¹' t, h t ht, _⟩,
rw [@inter_comm _ s (f ⁻¹' t), inter_assoc, inter_self] }
end
lemma continuous_on.preimage_open_of_open {f : α → β} {s : set α} {t : set β}
(hf : continuous_on f s) (hs : is_open s) (ht : is_open t) : is_open (s ∩ f⁻¹' t) :=
(continuous_on_open_iff hs).1 hf t ht
lemma continuous_on.preimage_closed_of_closed {f : α → β} {s : set α} {t : set β}
(hf : continuous_on f s) (hs : is_closed s) (ht : is_closed t) : is_closed (s ∩ f⁻¹' t) :=
begin
rcases continuous_on_iff_is_closed.1 hf t ht with ⟨u, hu⟩,
rw [inter_comm, hu.2],
apply is_closed_inter hu.1 hs
end
lemma continuous_on.preimage_interior_subset_interior_preimage {f : α → β} {s : set α} {t : set β}
(hf : continuous_on f s) (hs : is_open s) : s ∩ f⁻¹' (interior t) ⊆ s ∩ interior (f⁻¹' t) :=
calc s ∩ f ⁻¹' (interior t)
= interior (s ∩ f ⁻¹' (interior t)) :
(interior_eq_of_open (hf.preimage_open_of_open hs is_open_interior)).symm
... ⊆ interior (s ∩ f ⁻¹' t) :
interior_mono (inter_subset_inter (subset.refl _) (preimage_mono interior_subset))
... = s ∩ interior (f ⁻¹' t) :
by rw [interior_inter, interior_eq_of_open hs]
lemma continuous_on_of_locally_continuous_on {f : α → β} {s : set α}
(h : ∀x∈s, ∃t, is_open t ∧ x ∈ t ∧ continuous_on f (s ∩ t)) : continuous_on f s :=
begin
assume x xs,
rcases h x xs with ⟨t, open_t, xt, ct⟩,
have := ct x ⟨xs, xt⟩,
rwa [continuous_within_at, ← nhds_within_restrict _ xt open_t] at this
end
lemma continuous_on_open_of_generate_from {β : Type*} {s : set α} {T : set (set β)} {f : α → β}
(hs : is_open s) (h : ∀t ∈ T, is_open (s ∩ f⁻¹' t)) :
@continuous_on α β _ (topological_space.generate_from T) f s :=
begin
rw continuous_on_open_iff,
assume t ht,
induction ht with u hu u v Tu Tv hu hv U hU hU',
{ exact h u hu },
{ simp only [preimage_univ, inter_univ], exact hs },
{ have : s ∩ f ⁻¹' (u ∩ v) = (s ∩ f ⁻¹' u) ∩ (s ∩ f ⁻¹' v),
by { ext x, simp, split, finish, finish },
rw this,
exact is_open_inter hu hv },
{ rw [preimage_sUnion, inter_bUnion],
exact is_open_bUnion hU' },
{ exact hs }
end
lemma continuous_within_at.prod {f : α → β} {g : α → γ} {s : set α} {x : α}
(hf : continuous_within_at f s x) (hg : continuous_within_at g s x) :
continuous_within_at (λx, (f x, g x)) s x :=
hf.prod_mk_nhds hg
lemma continuous_on.prod {f : α → β} {g : α → γ} {s : set α}
(hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (λx, (f x, g x)) s :=
λx hx, continuous_within_at.prod (hf x hx) (hg x hx)
|
620c0f03942c595777d09a7cd2119dc1397e10a3 | 5ae26df177f810c5006841e9c73dc56e01b978d7 | /src/category_theory/monoidal/category.lean | fa306eeabf765ef310166794066b05749d82a2d4 | [
"Apache-2.0"
] | permissive | ChrisHughes24/mathlib | 98322577c460bc6b1fe5c21f42ce33ad1c3e5558 | a2a867e827c2a6702beb9efc2b9282bd801d5f9a | refs/heads/master | 1,583,848,251,477 | 1,565,164,247,000 | 1,565,164,247,000 | 129,409,993 | 0 | 1 | Apache-2.0 | 1,565,164,817,000 | 1,523,628,059,000 | Lean | UTF-8 | Lean | false | false | 14,797 | lean | /-
Copyright (c) 2018 Michael Jendrusch. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Michael Jendrusch, Scott Morrison
-/
import category_theory.monoidal.category_aux
import category_theory.natural_isomorphism
import tactic.basic
import tactic.slice
open category_theory
universes v u
open category_theory
open category_theory.category
open category_theory.iso
namespace category_theory
class monoidal_category (C : Type u) extends category.{v} C :=
-- curried tensor product of objects:
(tensor_obj : C → C → C)
(infixr ` ⊗ `:70 := tensor_obj) -- This notation is only temporary
-- curried tensor product of morphisms:
(tensor_hom :
Π {X₁ Y₁ X₂ Y₂ : C}, hom X₁ Y₁ → hom X₂ Y₂ → hom (X₁ ⊗ X₂) (Y₁ ⊗ Y₂))
(infixr ` ⊗' `:69 := tensor_hom) -- This notation is only temporary
-- tensor product laws:
(tensor_id' :
∀ (X₁ X₂ : C), (𝟙 X₁) ⊗' (𝟙 X₂) = 𝟙 (X₁ ⊗ X₂) . obviously)
(tensor_comp' :
∀ {X₁ Y₁ Z₁ X₂ Y₂ Z₂ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (g₁ : Y₁ ⟶ Z₁) (g₂ : Y₂ ⟶ Z₂),
(f₁ ≫ g₁) ⊗' (f₂ ≫ g₂) = (f₁ ⊗' f₂) ≫ (g₁ ⊗' g₂) . obviously)
-- tensor unit:
(tensor_unit : C)
-- associator:
(associator :
Π X Y Z : C, (X ⊗ Y) ⊗ Z ≅ X ⊗ (Y ⊗ Z))
(associator_naturality' :
assoc_natural tensor_obj @tensor_hom associator . obviously)
-- left unitor:
(left_unitor : Π X : C, tensor_unit ⊗ X ≅ X)
(left_unitor_naturality' :
left_unitor_natural tensor_obj @tensor_hom tensor_unit left_unitor . obviously)
-- right unitor:
(right_unitor : Π X : C, X ⊗ tensor_unit ≅ X)
(right_unitor_naturality' :
right_unitor_natural tensor_obj @tensor_hom tensor_unit right_unitor . obviously)
-- pentagon identity:
(pentagon' : pentagon @tensor_hom associator . obviously)
-- triangle identity:
(triangle' :
triangle @tensor_hom left_unitor right_unitor associator . obviously)
restate_axiom monoidal_category.tensor_id'
attribute [simp] monoidal_category.tensor_id
restate_axiom monoidal_category.tensor_comp'
attribute [simp] monoidal_category.tensor_comp
restate_axiom monoidal_category.associator_naturality'
restate_axiom monoidal_category.left_unitor_naturality'
restate_axiom monoidal_category.right_unitor_naturality'
restate_axiom monoidal_category.pentagon'
restate_axiom monoidal_category.triangle'
attribute [simp] monoidal_category.triangle
open monoidal_category
infixr ` ⊗ `:70 := tensor_obj
infixr ` ⊗ `:70 := tensor_hom
local notation `𝟙_` := tensor_unit
local notation `α_` := associator
local notation `λ_` := left_unitor
local notation `ρ_` := right_unitor
def tensor_iso {C : Type u} {X Y X' Y' : C} [monoidal_category.{v} C] (f : X ≅ Y) (g : X' ≅ Y') :
X ⊗ X' ≅ Y ⊗ Y' :=
{ hom := f.hom ⊗ g.hom,
inv := f.inv ⊗ g.inv,
hom_inv_id' := by rw [←tensor_comp, iso.hom_inv_id, iso.hom_inv_id, ←tensor_id],
inv_hom_id' := by rw [←tensor_comp, iso.inv_hom_id, iso.inv_hom_id, ←tensor_id] }
infixr ` ⊗ `:70 := tensor_iso
namespace monoidal_category
section
variables {C : Type u} [𝒞 : monoidal_category.{v} C]
include 𝒞
instance tensor_is_iso {W X Y Z : C} (f : W ⟶ X) [is_iso f] (g : Y ⟶ Z) [is_iso g] : is_iso (f ⊗ g) :=
{ ..(as_iso f ⊗ as_iso g) }
@[simp] lemma inv_tensor {W X Y Z : C} (f : W ⟶ X) [is_iso f] (g : Y ⟶ Z) [is_iso g] :
inv (f ⊗ g) = inv f ⊗ inv g := rfl
variables {U V W X Y Z : C}
-- When `rewrite_search` lands, add @[search] attributes to
-- monoidal_category.tensor_id monoidal_category.tensor_comp monoidal_category.associator_naturality
-- monoidal_category.left_unitor_naturality monoidal_category.right_unitor_naturality
-- monoidal_category.pentagon monoidal_category.triangle
-- tensor_comp_id tensor_id_comp comp_id_tensor_tensor_id
-- triangle_assoc_comp_left triangle_assoc_comp_right triangle_assoc_comp_left_inv triangle_assoc_comp_right_inv
-- left_unitor_tensor left_unitor_tensor_inv
-- right_unitor_tensor right_unitor_tensor_inv
-- pentagon_inv
-- associator_inv_naturality
-- left_unitor_inv_naturality
-- right_unitor_inv_naturality
@[simp] lemma comp_tensor_id (f : W ⟶ X) (g : X ⟶ Y) :
(f ≫ g) ⊗ (𝟙 Z) = (f ⊗ (𝟙 Z)) ≫ (g ⊗ (𝟙 Z)) :=
by { rw ←tensor_comp, simp }
@[simp] lemma id_tensor_comp (f : W ⟶ X) (g : X ⟶ Y) :
(𝟙 Z) ⊗ (f ≫ g) = (𝟙 Z ⊗ f) ≫ (𝟙 Z ⊗ g) :=
by { rw ←tensor_comp, simp }
@[simp] lemma id_tensor_comp_tensor_id (f : W ⟶ X) (g : Y ⟶ Z) :
((𝟙 Y) ⊗ f) ≫ (g ⊗ (𝟙 X)) = g ⊗ f :=
by { rw [←tensor_comp], simp }
@[simp] lemma tensor_id_comp_id_tensor (f : W ⟶ X) (g : Y ⟶ Z) :
(g ⊗ (𝟙 W)) ≫ ((𝟙 Z) ⊗ f) = g ⊗ f :=
by { rw [←tensor_comp], simp }
lemma left_unitor_inv_naturality {X X' : C} (f : X ⟶ X') :
f ≫ (λ_ X').inv = (λ_ X).inv ≫ (𝟙 _ ⊗ f) :=
begin
apply (cancel_mono (λ_ X').hom).1,
simp only [assoc, comp_id, iso.inv_hom_id],
rw [left_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp]
end
lemma right_unitor_inv_naturality {X X' : C} (f : X ⟶ X') :
f ≫ (ρ_ X').inv = (ρ_ X).inv ≫ (f ⊗ 𝟙 _) :=
begin
apply (cancel_mono (ρ_ X').hom).1,
simp only [assoc, comp_id, iso.inv_hom_id],
rw [right_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp]
end
@[simp] lemma tensor_left_iff
{X Y : C} (f g : X ⟶ Y) :
((𝟙 (𝟙_ C)) ⊗ f = (𝟙 (𝟙_ C)) ⊗ g) ↔ (f = g) :=
begin
split,
{ intro h,
have h' := congr_arg (λ k, (λ_ _).inv ≫ k) h,
dsimp at h',
rw [←left_unitor_inv_naturality, ←left_unitor_inv_naturality] at h',
exact (cancel_mono _).1 h', },
{ intro h, subst h, }
end
@[simp] lemma tensor_right_iff
{X Y : C} (f g : X ⟶ Y) :
(f ⊗ (𝟙 (𝟙_ C)) = g ⊗ (𝟙 (𝟙_ C))) ↔ (f = g) :=
begin
split,
{ intro h,
have h' := congr_arg (λ k, (ρ_ _).inv ≫ k) h,
dsimp at h',
rw [←right_unitor_inv_naturality, ←right_unitor_inv_naturality] at h',
exact (cancel_mono _).1 h' },
{ intro h, subst h, }
end
-- We now prove:
-- ((α_ (𝟙_ C) X Y).hom) ≫
-- ((λ_ (X ⊗ Y)).hom)
-- = ((λ_ X).hom ⊗ (𝟙 Y))
-- (and the corresponding fact for right unitors)
-- following the proof on nLab:
-- Lemma 2.2 at https://ncatlab.org/nlab/revision/monoidal+category/115
lemma left_unitor_product_aux_perimeter (X Y : C) :
((α_ (𝟙_ C) (𝟙_ C) X).hom ⊗ (𝟙 Y)) ≫
(α_ (𝟙_ C) ((𝟙_ C) ⊗ X) Y).hom ≫
((𝟙 (𝟙_ C)) ⊗ (α_ (𝟙_ C) X Y).hom) ≫
((𝟙 (𝟙_ C)) ⊗ (λ_ (X ⊗ Y)).hom)
= (((ρ_ (𝟙_ C)).hom ⊗ (𝟙 X)) ⊗ (𝟙 Y)) ≫
(α_ (𝟙_ C) X Y).hom :=
begin
conv_lhs { congr, skip, rw [←category.assoc] },
rw [←category.assoc, monoidal_category.pentagon, associator_naturality, tensor_id,
←monoidal_category.triangle, ←category.assoc]
end
lemma left_unitor_product_aux_triangle (X Y : C) :
((α_ (𝟙_ C) (𝟙_ C) X).hom ⊗ (𝟙 Y)) ≫
(((𝟙 (𝟙_ C)) ⊗ (λ_ X).hom) ⊗ (𝟙 Y))
= ((ρ_ (𝟙_ C)).hom ⊗ (𝟙 X)) ⊗ (𝟙 Y) :=
by rw [←comp_tensor_id, ←monoidal_category.triangle]
lemma left_unitor_product_aux_square (X Y : C) :
(α_ (𝟙_ C) ((𝟙_ C) ⊗ X) Y).hom ≫
((𝟙 (𝟙_ C)) ⊗ (λ_ X).hom ⊗ (𝟙 Y))
= (((𝟙 (𝟙_ C)) ⊗ (λ_ X).hom) ⊗ (𝟙 Y)) ≫
(α_ (𝟙_ C) X Y).hom :=
by rw associator_naturality
lemma left_unitor_product_aux (X Y : C) :
((𝟙 (𝟙_ C)) ⊗ (α_ (𝟙_ C) X Y).hom) ≫
((𝟙 (𝟙_ C)) ⊗ (λ_ (X ⊗ Y)).hom)
= (𝟙 (𝟙_ C)) ⊗ ((λ_ X).hom ⊗ (𝟙 Y)) :=
begin
rw ←(cancel_epi (α_ (𝟙_ C) ((𝟙_ C) ⊗ X) Y).hom),
rw left_unitor_product_aux_square,
rw ←(cancel_epi ((α_ (𝟙_ C) (𝟙_ C) X).hom ⊗ (𝟙 Y))),
slice_rhs 1 2 { rw left_unitor_product_aux_triangle },
conv_lhs { rw [left_unitor_product_aux_perimeter] }
end
lemma right_unitor_product_aux_perimeter (X Y : C) :
((α_ X Y (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫
(α_ X (Y ⊗ (𝟙_ C)) (𝟙_ C)).hom ≫
((𝟙 X) ⊗ (α_ Y (𝟙_ C) (𝟙_ C)).hom) ≫
((𝟙 X) ⊗ (𝟙 Y) ⊗ (λ_ (𝟙_ C)).hom)
= ((ρ_ (X ⊗ Y)).hom ⊗ (𝟙 (𝟙_ C))) ≫
(α_ X Y (𝟙_ C)).hom :=
begin
transitivity (((α_ X Y _).hom ⊗ 𝟙 _) ≫ (α_ X _ _).hom ≫
(𝟙 X ⊗ (α_ Y _ _).hom)) ≫
(𝟙 X ⊗ 𝟙 Y ⊗ (λ_ _).hom),
{ conv_lhs { congr, skip, rw [←category.assoc] },
conv_rhs { rw [category.assoc] } },
{ conv_lhs { congr, rw [monoidal_category.pentagon] },
conv_rhs { congr, rw [←monoidal_category.triangle] },
conv_rhs { rw [category.assoc] },
conv_rhs { congr, skip, congr, congr, rw [←tensor_id] },
conv_rhs { congr, skip, rw [associator_naturality] },
conv_rhs { rw [←category.assoc] } }
end
lemma right_unitor_product_aux_triangle (X Y : C) :
((𝟙 X) ⊗ (α_ Y (𝟙_ C) (𝟙_ C)).hom) ≫
((𝟙 X) ⊗ (𝟙 Y) ⊗ (λ_ (𝟙_ C)).hom)
= (𝟙 X) ⊗ (ρ_ Y).hom ⊗ (𝟙 (𝟙_ C)) :=
by rw [←id_tensor_comp, ←monoidal_category.triangle]
lemma right_unitor_product_aux_square (X Y : C) :
(α_ X (Y ⊗ (𝟙_ C)) (𝟙_ C)).hom ≫
((𝟙 X) ⊗ (ρ_ Y).hom ⊗ (𝟙 (𝟙_ C)))
= (((𝟙 X) ⊗ (ρ_ Y).hom) ⊗ (𝟙 (𝟙_ C))) ≫
(α_ X Y (𝟙_ C)).hom :=
by rw [associator_naturality]
lemma right_unitor_product_aux (X Y : C) :
((α_ X Y (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫
(((𝟙 X) ⊗ (ρ_ Y).hom) ⊗ (𝟙 (𝟙_ C)))
= ((ρ_ (X ⊗ Y)).hom ⊗ (𝟙 (𝟙_ C))) :=
begin
rw ←(cancel_mono (α_ X Y (𝟙_ C)).hom),
slice_lhs 2 3 { rw ←right_unitor_product_aux_square },
rw [←right_unitor_product_aux_triangle, ←right_unitor_product_aux_perimeter],
end
-- See Proposition 2.2.4 of http://www-math.mit.edu/~etingof/egnobookfinal.pdf
@[simp] lemma left_unitor_tensor (X Y : C) :
((α_ (𝟙_ C) X Y).hom) ≫ ((λ_ (X ⊗ Y)).hom) =
((λ_ X).hom ⊗ (𝟙 Y)) :=
by rw [←tensor_left_iff, id_tensor_comp, left_unitor_product_aux]
@[simp] lemma left_unitor_tensor_inv (X Y : C) :
((λ_ (X ⊗ Y)).inv) ≫ ((α_ (𝟙_ C) X Y).inv) =
((λ_ X).inv ⊗ (𝟙 Y)) :=
eq_of_inv_eq_inv (by simp)
@[simp] lemma right_unitor_tensor (X Y : C) :
((α_ X Y (𝟙_ C)).hom) ≫ ((𝟙 X) ⊗ (ρ_ Y).hom) =
((ρ_ (X ⊗ Y)).hom) :=
by rw [←tensor_right_iff, comp_tensor_id, right_unitor_product_aux]
@[simp] lemma right_unitor_tensor_inv (X Y : C) :
((𝟙 X) ⊗ (ρ_ Y).inv) ≫ ((α_ X Y (𝟙_ C)).inv) =
((ρ_ (X ⊗ Y)).inv) :=
eq_of_inv_eq_inv (by simp)
lemma associator_inv_naturality {X Y Z X' Y' Z' : C} (f : X ⟶ X') (g : Y ⟶ Y') (h : Z ⟶ Z') :
(f ⊗ (g ⊗ h)) ≫ (α_ X' Y' Z').inv = (α_ X Y Z).inv ≫ ((f ⊗ g) ⊗ h) :=
begin
apply (cancel_mono (α_ X' Y' Z').hom).1,
simp only [assoc, comp_id, iso.inv_hom_id],
rw [associator_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp]
end
lemma pentagon_inv (W X Y Z : C) :
((𝟙 W) ⊗ (α_ X Y Z).inv) ≫ (α_ W (X ⊗ Y) Z).inv ≫ ((α_ W X Y).inv ⊗ (𝟙 Z))
= (α_ W X (Y ⊗ Z)).inv ≫ (α_ (W ⊗ X) Y Z).inv :=
begin
apply category_theory.eq_of_inv_eq_inv,
dsimp,
rw [category.assoc, monoidal_category.pentagon]
end
@[simp] lemma triangle_assoc_comp_left (X Y : C) :
(α_ X (𝟙_ C) Y).hom ≫ ((𝟙 X) ⊗ (λ_ Y).hom) = (ρ_ X).hom ⊗ 𝟙 Y :=
monoidal_category.triangle C X Y
@[simp] lemma triangle_assoc_comp_right (X Y : C) :
(α_ X (𝟙_ C) Y).inv ≫ ((ρ_ X).hom ⊗ 𝟙 Y) = ((𝟙 X) ⊗ (λ_ Y).hom) :=
by rw [←triangle_assoc_comp_left, ←category.assoc, iso.inv_hom_id, category.id_comp]
@[simp] lemma triangle_assoc_comp_right_inv (X Y : C) :
((ρ_ X).inv ⊗ 𝟙 Y) ≫ (α_ X (𝟙_ C) Y).hom = ((𝟙 X) ⊗ (λ_ Y).inv) :=
begin
apply (cancel_mono (𝟙 X ⊗ (λ_ Y).hom)).1,
simp only [assoc, triangle_assoc_comp_left],
rw [←comp_tensor_id, iso.inv_hom_id, ←id_tensor_comp, iso.inv_hom_id]
end
@[simp] lemma triangle_assoc_comp_left_inv (X Y : C) :
((𝟙 X) ⊗ (λ_ Y).inv) ≫ (α_ X (𝟙_ C) Y).inv = ((ρ_ X).inv ⊗ 𝟙 Y) :=
begin
apply (cancel_mono ((ρ_ X).hom ⊗ 𝟙 Y)).1,
simp only [triangle_assoc_comp_right, assoc],
rw [←id_tensor_comp, iso.inv_hom_id, ←comp_tensor_id, iso.inv_hom_id]
end
end
section
-- In order to be able to describe the tensor product as a functor, we
-- need to be up in at least `Type 0` for both objects and morphisms,
-- so that we can construct products.
variables (C : Type u) [𝒞 : monoidal_category.{v+1} C]
include 𝒞
def tensor : (C × C) ⥤ C :=
{ obj := λ X, tensor_obj X.1 X.2,
map := λ {X Y : C × C} (f : X ⟶ Y), tensor_hom f.1 f.2 }
def left_assoc_tensor : (C × C × C) ⥤ C :=
{ obj := λ X, (X.1 ⊗ X.2.1) ⊗ X.2.2,
map := λ {X Y : C × C × C} (f : X ⟶ Y), (f.1 ⊗ f.2.1) ⊗ f.2.2 }
@[simp] lemma left_assoc_tensor_obj (X) :
(left_assoc_tensor C).obj X = (X.1 ⊗ X.2.1) ⊗ X.2.2 := rfl
@[simp] lemma left_assoc_tensor_map {X Y} (f : X ⟶ Y) :
(left_assoc_tensor C).map f = (f.1 ⊗ f.2.1) ⊗ f.2.2 := rfl
def right_assoc_tensor : (C × C × C) ⥤ C :=
{ obj := λ X, X.1 ⊗ (X.2.1 ⊗ X.2.2),
map := λ {X Y : C × C × C} (f : X ⟶ Y), f.1 ⊗ (f.2.1 ⊗ f.2.2) }
@[simp] lemma right_assoc_tensor_obj (X) :
(right_assoc_tensor C).obj X = X.1 ⊗ (X.2.1 ⊗ X.2.2) := rfl
@[simp] lemma right_assoc_tensor_map {X Y} (f : X ⟶ Y) :
(right_assoc_tensor C).map f = f.1 ⊗ (f.2.1 ⊗ f.2.2) := rfl
def tensor_unit_left : C ⥤ C :=
{ obj := λ X, 𝟙_ C ⊗ X,
map := λ {X Y : C} (f : X ⟶ Y), (𝟙 (𝟙_ C)) ⊗ f }
def tensor_unit_right : C ⥤ C :=
{ obj := λ X, X ⊗ 𝟙_ C,
map := λ {X Y : C} (f : X ⟶ Y), f ⊗ (𝟙 (𝟙_ C)) }
-- We can express the associator and the unitors, given componentwise above,
-- as natural isomorphisms.
def associator_nat_iso :
left_assoc_tensor C ≅ right_assoc_tensor C :=
nat_iso.of_components
(by { intros, apply monoidal_category.associator })
(by { intros, apply monoidal_category.associator_naturality })
def left_unitor_nat_iso :
tensor_unit_left C ≅ functor.id C :=
nat_iso.of_components
(by { intros, apply monoidal_category.left_unitor })
(by { intros, apply monoidal_category.left_unitor_naturality })
def right_unitor_nat_iso :
tensor_unit_right C ≅ functor.id C :=
nat_iso.of_components
(by { intros, apply monoidal_category.right_unitor })
(by { intros, apply monoidal_category.right_unitor_naturality })
end
end monoidal_category
end category_theory
|
5030fc6801490f254783a9fc8c0198c8bd99598c | 94e33a31faa76775069b071adea97e86e218a8ee | /src/category_theory/subterminal.lean | ddf8015a321e3661a46a53a263767d72e3605c31 | [
"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 | 6,042 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import category_theory.limits.shapes.binary_products
import category_theory.limits.shapes.terminal
import category_theory.subobject.mono_over
/-!
# Subterminal objects
Subterminal objects are the objects which can be thought of as subobjects of the terminal object.
In fact, the definition can be constructed to not require a terminal object, by defining `A` to be
subterminal iff for any `Z`, there is at most one morphism `Z ⟶ A`.
An alternate definition is that the diagonal morphism `A ⟶ A ⨯ A` is an isomorphism.
In this file we define subterminal objects and show the equivalence of these three definitions.
We also construct the subcategory of subterminal objects.
## TODO
* Define exponential ideals, and show this subcategory is an exponential ideal.
* Use the above to show that in a locally cartesian closed category, every subobject lattice
is cartesian closed (equivalently, a Heyting algebra).
-/
universes v₁ v₂ u₁ u₂
noncomputable theory
namespace category_theory
open limits category
variables {C : Type u₁} [category.{v₁} C] {A : C}
/-- An object `A` is subterminal iff for any `Z`, there is at most one morphism `Z ⟶ A`. -/
def is_subterminal (A : C) : Prop := ∀ ⦃Z : C⦄ (f g : Z ⟶ A), f = g
lemma is_subterminal.def : is_subterminal A ↔ ∀ ⦃Z : C⦄ (f g : Z ⟶ A), f = g := iff.rfl
/--
If `A` is subterminal, the unique morphism from it to a terminal object is a monomorphism.
The converse of `is_subterminal_of_mono_is_terminal_from`.
-/
lemma is_subterminal.mono_is_terminal_from (hA : is_subterminal A) {T : C} (hT : is_terminal T) :
mono (hT.from A) :=
{ right_cancellation := λ Z g h _, hA _ _ }
/--
If `A` is subterminal, the unique morphism from it to the terminal object is a monomorphism.
The converse of `is_subterminal_of_mono_terminal_from`.
-/
lemma is_subterminal.mono_terminal_from [has_terminal C] (hA : is_subterminal A) :
mono (terminal.from A) :=
hA.mono_is_terminal_from terminal_is_terminal
/--
If the unique morphism from `A` to a terminal object is a monomorphism, `A` is subterminal.
The converse of `is_subterminal.mono_is_terminal_from`.
-/
lemma is_subterminal_of_mono_is_terminal_from {T : C} (hT : is_terminal T) [mono (hT.from A)] :
is_subterminal A :=
λ Z f g, by { rw ← cancel_mono (hT.from A), apply hT.hom_ext }
/--
If the unique morphism from `A` to the terminal object is a monomorphism, `A` is subterminal.
The converse of `is_subterminal.mono_terminal_from`.
-/
lemma is_subterminal_of_mono_terminal_from [has_terminal C] [mono (terminal.from A)] :
is_subterminal A :=
λ Z f g, by { rw ← cancel_mono (terminal.from A), apply subsingleton.elim }
lemma is_subterminal_of_is_terminal {T : C} (hT : is_terminal T) : is_subterminal T :=
λ Z f g, hT.hom_ext _ _
lemma is_subterminal_of_terminal [has_terminal C] : is_subterminal (⊤_ C) :=
λ Z f g, subsingleton.elim _ _
/--
If `A` is subterminal, its diagonal morphism is an isomorphism.
The converse of `is_subterminal_of_is_iso_diag`.
-/
lemma is_subterminal.is_iso_diag (hA : is_subterminal A) [has_binary_product A A] :
is_iso (diag A) :=
⟨⟨limits.prod.fst, ⟨by simp, by { rw is_subterminal.def at hA, tidy }⟩⟩⟩
/--
If the diagonal morphism of `A` is an isomorphism, then it is subterminal.
The converse of `is_subterminal.is_iso_diag`.
-/
lemma is_subterminal_of_is_iso_diag [has_binary_product A A] [is_iso (diag A)] :
is_subterminal A :=
λ Z f g,
begin
have : (limits.prod.fst : A ⨯ A ⟶ _) = limits.prod.snd,
{ simp [←cancel_epi (diag A)] },
rw [←prod.lift_fst f g, this, prod.lift_snd],
end
/-- If `A` is subterminal, it is isomorphic to `A ⨯ A`. -/
@[simps]
def is_subterminal.iso_diag (hA : is_subterminal A) [has_binary_product A A] :
A ⨯ A ≅ A :=
begin
letI := is_subterminal.is_iso_diag hA,
apply (as_iso (diag A)).symm,
end
variables (C)
/--
The (full sub)category of subterminal objects.
TODO: If `C` is the category of sheaves on a topological space `X`, this category is equivalent
to the lattice of open subsets of `X`. More generally, if `C` is a topos, this is the lattice of
"external truth values".
-/
@[derive category]
def subterminals (C : Type u₁) [category.{v₁} C] :=
{A : C // is_subterminal A}
instance [has_terminal C] : inhabited (subterminals C) :=
⟨⟨⊤_ C, is_subterminal_of_terminal⟩⟩
/-- The inclusion of the subterminal objects into the original category. -/
@[derive [full, faithful], simps]
def subterminal_inclusion : subterminals C ⥤ C := full_subcategory_inclusion _
instance subterminals_thin (X Y : subterminals C) : subsingleton (X ⟶ Y) :=
⟨λ f g, Y.2 f g⟩
/--
The category of subterminal objects is equivalent to the category of monomorphisms to the terminal
object (which is in turn equivalent to the subobjects of the terminal object).
-/
@[simps]
def subterminals_equiv_mono_over_terminal [has_terminal C] :
subterminals C ≌ mono_over (⊤_ C) :=
{ functor :=
{ obj := λ X, ⟨over.mk (terminal.from X.1), X.2.mono_terminal_from⟩,
map := λ X Y f, mono_over.hom_mk f (by ext1 ⟨⟨⟩⟩) },
inverse :=
{ obj := λ X, ⟨X.val.left, λ Z f g, by { rw ← cancel_mono X.arrow, apply subsingleton.elim }⟩,
map := λ X Y f, f.1 },
unit_iso :=
{ hom := { app := λ X, 𝟙 _ },
inv := { app := λ X, 𝟙 _ } },
counit_iso :=
{ hom := { app := λ X, over.hom_mk (𝟙 _) },
inv := { app := λ X, over.hom_mk (𝟙 _) } } }
@[simp]
lemma subterminals_to_mono_over_terminal_comp_forget [has_terminal C] :
(subterminals_equiv_mono_over_terminal C).functor ⋙ mono_over.forget _ ⋙ over.forget _ =
subterminal_inclusion C :=
rfl
@[simp]
lemma mono_over_terminal_to_subterminals_comp [has_terminal C] :
(subterminals_equiv_mono_over_terminal C).inverse ⋙ subterminal_inclusion C =
mono_over.forget _ ⋙ over.forget _ :=
rfl
end category_theory
|
a4807568fd78bc84a933a666b86877d0f79c84b5 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/data/list/count.lean | 176ea1fe16385823671c92b5a633b8483273fc6b | [
"Apache-2.0"
] | permissive | robertylewis/mathlib | 3d16e3e6daf5ddde182473e03a1b601d2810952c | 1d13f5b932f5e40a8308e3840f96fc882fae01f0 | refs/heads/master | 1,651,379,945,369 | 1,644,276,960,000 | 1,644,276,960,000 | 98,875,504 | 0 | 0 | Apache-2.0 | 1,644,253,514,000 | 1,501,495,700,000 | Lean | UTF-8 | Lean | false | false | 7,423 | lean | /-
Copyright (c) 2014 Parikshit Khanna. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro
-/
import data.list.big_operators
/-!
# Counting in lists
This file proves basic properties of `list.countp` and `list.count`, which count the number of
elements of a list satisfying a predicate and equal to a given element respectively. Their
definitions can be found in [`data.list.defs`](./data/list/defs).
-/
open nat
variables {α β : Type*} {l l₁ l₂ : list α}
namespace list
section countp
variables (p : α → Prop) [decidable_pred p]
@[simp] lemma countp_nil : countp p [] = 0 := rfl
@[simp] lemma countp_cons_of_pos {a : α} (l) (pa : p a) : countp p (a::l) = countp p l + 1 :=
if_pos pa
@[simp] lemma countp_cons_of_neg {a : α} (l) (pa : ¬ p a) : countp p (a::l) = countp p l :=
if_neg pa
lemma length_eq_countp_add_countp (l) : length l = countp p l + countp (λ a, ¬p a) l :=
by induction l with x h ih; [refl, by_cases p x];
[simp only [countp_cons_of_pos _ _ h, countp_cons_of_neg (λ a, ¬p a) _ (decidable.not_not.2 h),
ih, length],
simp only [countp_cons_of_pos (λ a, ¬p a) _ h, countp_cons_of_neg _ _ h, ih, length]]; ac_refl
lemma countp_eq_length_filter (l) : countp p l = length (filter p l) :=
by induction l with x l ih; [refl, by_cases (p x)];
[simp only [filter_cons_of_pos _ h, countp, ih, if_pos h],
simp only [countp_cons_of_neg _ _ h, ih, filter_cons_of_neg _ h]]; refl
lemma countp_le_length : countp p l ≤ l.length :=
by simpa only [countp_eq_length_filter] using length_le_of_sublist (filter_sublist _)
@[simp] lemma countp_append (l₁ l₂) : countp p (l₁ ++ l₂) = countp p l₁ + countp p l₂ :=
by simp only [countp_eq_length_filter, filter_append, length_append]
lemma countp_pos {l} : 0 < countp p l ↔ ∃ a ∈ l, p a :=
by simp only [countp_eq_length_filter, length_pos_iff_exists_mem, mem_filter, exists_prop]
lemma length_filter_lt_length_iff_exists (l) : length (filter p l) < length l ↔ ∃ x ∈ l, ¬p x :=
by rw [length_eq_countp_add_countp p l, ← countp_pos, countp_eq_length_filter, lt_add_iff_pos_right]
lemma sublist.countp_le (s : l₁ <+ l₂) : countp p l₁ ≤ countp p l₂ :=
by simpa only [countp_eq_length_filter] using length_le_of_sublist (s.filter p)
@[simp] lemma countp_filter {q} [decidable_pred q] (l : list α) :
countp p (filter q l) = countp (λ a, p a ∧ q a) l :=
by simp only [countp_eq_length_filter, filter_filter]
@[simp] lemma countp_true : l.countp (λ _, true) = l.length :=
by simp [countp_eq_length_filter]
@[simp] lemma countp_false : l.countp (λ _, false) = 0 :=
by simp [countp_eq_length_filter]
end countp
/-! ### count -/
section count
variables [decidable_eq α]
@[simp] lemma count_nil (a : α) : count a [] = 0 := rfl
lemma count_cons (a b : α) (l : list α) :
count a (b :: l) = if a = b then succ (count a l) else count a l := rfl
lemma count_cons' (a b : α) (l : list α) :
count a (b :: l) = count a l + (if a = b then 1 else 0) :=
begin rw count_cons, split_ifs; refl end
@[simp] lemma count_cons_self (a : α) (l : list α) : count a (a::l) = succ (count a l) := if_pos rfl
@[simp, priority 990]
lemma count_cons_of_ne {a b : α} (h : a ≠ b) (l : list α) : count a (b::l) = count a l := if_neg h
lemma count_tail : Π (l : list α) (a : α) (h : 0 < l.length),
l.tail.count a = l.count a - ite (a = list.nth_le l 0 h) 1 0
| (_ :: _) a h := by { rw [count_cons], split_ifs; simp }
lemma count_le_length (a : α) (l : list α) : count a l ≤ l.length :=
countp_le_length _
lemma sublist.count_le (h : l₁ <+ l₂) (a : α) : count a l₁ ≤ count a l₂ := h.countp_le _
lemma count_le_count_cons (a b : α) (l : list α) : count a l ≤ count a (b :: l) :=
(sublist_cons _ _).count_le _
lemma count_singleton (a : α) : count a [a] = 1 := if_pos rfl
lemma count_singleton' (a b : α) : count a [b] = ite (a = b) 1 0 := rfl
@[simp] lemma count_append (a : α) : ∀ l₁ l₂, count a (l₁ ++ l₂) = count a l₁ + count a l₂ :=
countp_append _
lemma count_concat (a : α) (l : list α) : count a (concat l a) = succ (count a l) :=
by simp [-add_comm]
@[simp] lemma count_pos {a : α} {l : list α} : 0 < count a l ↔ a ∈ l :=
by simp only [count, countp_pos, exists_prop, exists_eq_right']
@[simp] lemma one_le_count_iff_mem {a : α} {l : list α} : 1 ≤ count a l ↔ a ∈ l :=
count_pos
@[simp, priority 980]
lemma count_eq_zero_of_not_mem {a : α} {l : list α} (h : a ∉ l) : count a l = 0 :=
decidable.by_contradiction $ λ h', h $ count_pos.1 (nat.pos_of_ne_zero h')
lemma not_mem_of_count_eq_zero {a : α} {l : list α} (h : count a l = 0) : a ∉ l :=
λ h', (count_pos.2 h').ne' h
@[simp] lemma count_repeat (a : α) (n : ℕ) : count a (repeat a n) = n :=
by rw [count, countp_eq_length_filter, filter_eq_self.2, length_repeat];
exact λ b m, (eq_of_mem_repeat m).symm
lemma le_count_iff_repeat_sublist {a : α} {l : list α} {n : ℕ} :
n ≤ count a l ↔ repeat a n <+ l :=
⟨λ h, ((repeat_sublist_repeat a).2 h).trans $
have filter (eq a) l = repeat a (count a l), from eq_repeat.2
⟨by simp only [count, countp_eq_length_filter], λ b m, (of_mem_filter m).symm⟩,
by rw ← this; apply filter_sublist,
λ h, by simpa only [count_repeat] using h.count_le a⟩
lemma repeat_count_eq_of_count_eq_length {a : α} {l : list α} (h : count a l = length l) :
repeat a (count a l) = l :=
eq_of_sublist_of_length_eq (le_count_iff_repeat_sublist.mp (le_refl (count a l)))
(eq.trans (length_repeat a (count a l)) h)
@[simp] lemma count_filter {p} [decidable_pred p]
{a} {l : list α} (h : p a) : count a (filter p l) = count a l :=
by simp only [count, countp_filter, show (λ b, a = b ∧ p b) = eq a, by { ext b, constructor; cc }]
lemma count_bind {α β} [decidable_eq β] (l : list α) (f : α → list β) (x : β) :
count x (l.bind f) = sum (map (count x ∘ f) l) :=
begin
induction l with hd tl IH,
{ simp },
{ simpa }
end
@[simp] lemma count_map_of_injective {α β} [decidable_eq α] [decidable_eq β]
(l : list α) (f : α → β) (hf : function.injective f) (x : α) :
count (f x) (map f l) = count x l :=
begin
induction l with y l IH generalizing x,
{ simp },
{ rw map_cons,
by_cases h : x = y,
{ simpa [h] using IH _ },
{ simpa [h, hf.ne h] using IH _ } }
end
lemma count_le_count_map [decidable_eq β] (l : list α) (f : α → β) (x : α) :
count x l ≤ count (f x) (map f l) :=
begin
induction l with a as IH, { simp },
rcases eq_or_ne x a with rfl | hxa,
{ simp [succ_le_succ IH] },
{ simp [hxa, le_add_right IH, count_cons'] }
end
@[simp] lemma count_erase_self (a : α) :
∀ (s : list α), count a (list.erase s a) = pred (count a s)
| [] := by simp
| (h :: t) :=
begin
rw erase_cons,
by_cases p : h = a,
{ rw [if_pos p, count_cons', if_pos p.symm], simp },
{ rw [if_neg p, count_cons', count_cons', if_neg (λ x : a = h, p x.symm), count_erase_self],
simp }
end
@[simp] lemma count_erase_of_ne {a b : α} (ab : a ≠ b) :
∀ (s : list α), count a (list.erase s b) = count a s
| [] := by simp
| (x :: xs) :=
begin
rw erase_cons,
split_ifs with h,
{ rw [count_cons', h, if_neg ab], simp },
{ rw [count_cons', count_cons', count_erase_of_ne] }
end
end count
end list
|
e3b30a80869f26c14497eb332cb77ef987c93e56 | fe25de614feb5587799621c41487aaee0d083b08 | /stage0/src/Lean/PrettyPrinter/Delaborator/TopDownAnalyze.lean | 85422acf4b6a6920374c4a0f5e9052fba7f38ad4 | [
"Apache-2.0"
] | permissive | pollend/lean4 | e8469c2f5fb8779b773618c3267883cf21fb9fac | c913886938c4b3b83238a3f99673c6c5a9cec270 | refs/heads/master | 1,687,973,251,481 | 1,628,039,739,000 | 1,628,039,739,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 24,564 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Daniel Selsam
-/
/-!
The top-down analyzer is an optional preprocessor to the delaborator that aims
to determine the minimal annotations necessary to ensure that the delaborated
expression can be re-elaborated correctly. Currently, the top-down analyzer
is neither sound nor complete: there may be edge-cases in which the expression
can still not be re-elaborated correctly, and it may also add many annotations
that are not strictly necessary.
-/
import Lean.Meta
import Lean.Util.FindMVar
import Lean.Util.FindLevelMVar
import Lean.Util.CollectLevelParams
import Lean.Util.ReplaceLevel
import Lean.PrettyPrinter.Delaborator.Options
import Lean.PrettyPrinter.Delaborator.SubExpr
import Std.Data.RBMap
namespace Lean
open Lean.Meta
open Std (RBMap)
register_builtin_option pp.analyze : Bool := {
defValue := true
group := "pp.analyze"
descr := "(pretty printer analyzer) determine annotations sufficient to ensure round-tripping"
}
register_builtin_option pp.analyze.checkInstances : Bool := {
-- TODO: It would be great to make this default to `true`, but currently, `MessageData` does not
-- include the `LocalInstances`, so this will be very over-aggressive in inserting instances
-- that would otherwise be easy to synthesize. We may consider threading the instances in the future,
-- or at least tracking a bool for whether the instances have been lost.
defValue := false
group := "pp.analyze"
descr := "(pretty printer analyzer) confirm that instances can be re-synthesized"
}
register_builtin_option pp.analyze.typeAscriptions : Bool := {
defValue := true
group := "pp.analyze"
descr := "(pretty printer analyzer) add type ascriptions when deemed necessary"
}
register_builtin_option pp.analyze.trustSubst : Bool := {
defValue := false
group := "pp.analyze"
descr := "(pretty printer analyzer) always 'pretend' applications that can delab to ▸ are 'regular'"
}
register_builtin_option pp.analyze.trustOfNat : Bool := {
defValue := true
group := "pp.analyze"
descr := "(pretty printer analyzer) always 'pretend' `OfNat.ofNat` applications can elab bottom-up"
}
register_builtin_option pp.analyze.trustOfScientific : Bool := {
defValue := true
group := "pp.analyze"
descr := "(pretty printer analyzer) always 'pretend' `OfScientific.ofScientific` applications can elab bottom-up"
}
register_builtin_option pp.analyze.trustCoe : Bool := {
defValue := false
group := "pp.analyze"
descr := "(pretty printer analyzer) always assume a coercion can be correctly inserted"
}
register_builtin_option pp.analyze.trustId : Bool := {
defValue := true
group := "pp.analyze"
descr := "(pretty printer analyzer) always assume an implicit `fun x => x` can be inferred"
}
register_builtin_option pp.analyze.trustKnownFOType2TypeHOFuns : Bool := {
defValue := true
group := "pp.analyze"
descr := "(pretty printer analyzer) omit higher-order functions whose values seem to be knownType2Type"
}
register_builtin_option pp.analyze.omitMax : Bool := {
defValue := true
group := "pp.analyze"
descr := "(pretty printer analyzer) omit universe `max` annotations (these constraints can actually hurt)"
}
register_builtin_option pp.analyze.knowsType : Bool := {
defValue := true
group := "pp.analyze"
descr := "(pretty printer analyzer) assume the type of the original expression is known"
}
def getPPAnalyze (o : Options) : Bool := o.get pp.analyze.name pp.analyze.defValue
def getPPAnalyzeCheckInstances (o : Options) : Bool := o.get pp.analyze.checkInstances.name pp.analyze.checkInstances.defValue
def getPPAnalyzeTypeAscriptions (o : Options) : Bool := o.get pp.analyze.typeAscriptions.name pp.analyze.typeAscriptions.defValue
def getPPAnalyzeTrustSubst (o : Options) : Bool := o.get pp.analyze.trustSubst.name pp.analyze.trustSubst.defValue
def getPPAnalyzeTrustOfNat (o : Options) : Bool := o.get pp.analyze.trustOfNat.name pp.analyze.trustOfNat.defValue
def getPPAnalyzeTrustOfScientific (o : Options) : Bool := o.get pp.analyze.trustOfScientific.name pp.analyze.trustOfScientific.defValue
def getPPAnalyzeTrustId (o : Options) : Bool := o.get pp.analyze.trustId.name pp.analyze.trustId.defValue
def getPPAnalyzeTrustCoe (o : Options) : Bool := o.get pp.analyze.trustCoe.name pp.analyze.trustCoe.defValue
def getPPAnalyzeTrustKnownFOType2TypeHOFuns (o : Options) : Bool := o.get pp.analyze.trustKnownFOType2TypeHOFuns.name pp.analyze.trustKnownFOType2TypeHOFuns.defValue
def getPPAnalyzeOmitMax (o : Options) : Bool := o.get pp.analyze.omitMax.name pp.analyze.omitMax.defValue
def getPPAnalyzeKnowsType (o : Options) : Bool := o.get pp.analyze.knowsType.name pp.analyze.knowsType.defValue
def getPPAnalysisSkip (o : Options) : Bool := o.get `pp.analysis.skip false
def getPPAnalysisHole (o : Options) : Bool := o.get `pp.analysis.hole false
def getPPAnalysisNamedArg (o : Options) : Bool := o.get `pp.analysis.namedArg false
def getPPAnalysisLetVarType (o : Options) : Bool := o.get `pp.analysis.letVarType false
def getPPAnalysisNeedsType (o : Options) : Bool := o.get `pp.analysis.needsType false
def getPPAnalysisBlockImplicit (o : Options) : Bool := o.get `pp.analysis.blockImplicit false
namespace PrettyPrinter.Delaborator
def returnsPi (motive : Expr) : MetaM Bool := do
lambdaTelescope motive fun xs b => b.isForall
def isNonConstFun (motive : Expr) : MetaM Bool := do
match motive with
| Expr.lam name d b _ => isNonConstFun b
| _ => motive.hasLooseBVars
def isSimpleHOFun (motive : Expr) : MetaM Bool := do
not (← returnsPi motive) && not (← isNonConstFun motive)
def isType2Type (motive : Expr) : MetaM Bool := do
match ← inferType motive with
| Expr.forallE _ (Expr.sort ..) (Expr.sort ..) .. => true
| _ => false
def isFOLike (motive : Expr) : MetaM Bool := do
let f := motive.getAppFn
f.isFVar || f.isConst
def isIdLike (arg : Expr) : Bool := do
-- TODO: allow `id` constant as well?
match arg with
| Expr.lam _ _ (Expr.bvar ..) .. => true
| _ => false
def isCoe (e : Expr) : Bool :=
-- TODO: `coeSort? Builtins doesn't seem to render them anyway
e.isAppOfArity `coe 4
|| (e.isAppOf `coeFun && e.getAppNumArgs >= 4)
|| e.isAppOfArity `coeSort 4
def isStructureInstance (e : Expr) : MetaM Bool := do
match e.isConstructorApp? (← getEnv) with
| some s => isStructure (← getEnv) s.induct
| none => false
namespace TopDownAnalyze
partial def hasMVarAtCurrDepth (e : Expr) : MetaM Bool := do
let mctx ← getMCtx
Option.isSome $ e.findMVar? fun mvarId =>
match mctx.findDecl? mvarId with
| some mdecl => mdecl.depth == mctx.depth
| _ => false
partial def hasLevelMVarAtCurrDepth (e : Expr) : MetaM Bool := do
let mctx ← getMCtx
Option.isSome $ e.findLevelMVar? fun mvarId =>
mctx.findLevelDepth? mvarId == some mctx.depth
private def valUnknown (e : Expr) : MetaM Bool := do
hasMVarAtCurrDepth (← instantiateMVars e)
private def typeUnknown (e : Expr) : MetaM Bool := do
valUnknown (← inferType e)
def isHBinOp (e : Expr) : Bool := do
-- TODO: instead of tracking these explicitly,
-- consider a more general solution that checks for defaultInstances
if e.getAppNumArgs != 6 then return false
let f := e.getAppFn
if !f.isConst then return false
-- Note: we leave out `HPow.hPow because we expect its homogeneous
-- version will change soon
let ops := #[
`HOr.hOr, `HXor.hXor, `HAnd.hAnd,
`HAppend.hAppend, `HOrElse.hOrElse, `HAndThen.hAndThen,
`HAdd.hAdd, `HSub.hSub, `HMul.hMul, `HDiv.hDiv, `HMod.hMod,
`HShiftLeft.hShiftLeft, `HShiftRight]
ops.any fun op => op == f.constName!
def replaceLPsWithVars (e : Expr) : MetaM Expr := do
let lps := collectLevelParams {} e |>.params
let mut replaceMap : Std.HashMap Name Level := {}
for lp in lps do replaceMap := replaceMap.insert lp (← mkFreshLevelMVar)
return e.replaceLevel fun
| Level.param n .. => replaceMap.find! n
| l => if !l.hasParam then some l else none
def isDefEqAssigning (t s : Expr) : MetaM Bool := do
withReader (fun ctx => { ctx with config := { ctx.config with assignSyntheticOpaque := true }}) $
Meta.isDefEq t s
def checkpointDefEq (t s : Expr) : MetaM Bool := do
Meta.checkpointDefEq (mayPostpone := false) do
isDefEqAssigning t s
def isHigherOrder (type : Expr) : MetaM Bool := do
withTransparency TransparencyMode.all do forallTelescopeReducing type fun xs b => xs.size > 0 && b.isSort
def isFunLike (e : Expr) : MetaM Bool := do
withTransparency TransparencyMode.all do forallTelescopeReducing (← inferType e) fun xs b => xs.size > 0
def isSubstLike (e : Expr) : Bool :=
e.isAppOfArity `Eq.ndrec 6 || e.isAppOfArity `Eq.rec 6
def nameNotRoundtrippable (n : Name) : Bool :=
n.hasMacroScopes || isPrivateName n || containsNum n
where
containsNum
| Name.str p .. => containsNum p
| Name.num .. => true
| Name.anonymous => false
def mvarName (mvar : Expr) : MetaM Name := do
(← getMVarDecl mvar.mvarId!).userName
def containsBadMax : Level → Bool
| Level.succ u .. => containsBadMax u
| Level.max u v .. => (u.hasParam && v.hasParam) || containsBadMax u || containsBadMax v
| Level.imax u v .. => containsBadMax u || containsBadMax v
| _ => false
open SubExpr
structure Context where
knowsType : Bool
knowsLevel : Bool -- only constants look at this
inBottomUp : Bool := false
parentIsApp : Bool := false
deriving Inhabited, Repr
structure State where
annotations : RBMap Pos Options compare := {}
postponed : Array (Expr × Expr) := #[] -- not currently used
abbrev AnalyzeM := ReaderT Context (ReaderT SubExpr (StateRefT State MetaM))
def tryUnify (e₁ e₂ : Expr) : AnalyzeM Unit := do
try
let r ← isDefEqAssigning e₁ e₂
if !r then modify fun s => { s with postponed := s.postponed.push (e₁, e₂) }
pure ()
catch ex =>
modify fun s => { s with postponed := s.postponed.push (e₁, e₂) }
partial def inspectOutParams (arg mvar : Expr) : AnalyzeM Unit := do
let argType ← inferType arg -- HAdd α α α
let mvarType ← inferType mvar
let fType ← inferType argType.getAppFn -- Type → Type → outParam Type
let mType ← inferType mvarType.getAppFn
inspectAux fType mType 0 argType.getAppArgs mvarType.getAppArgs
where
inspectAux (fType mType : Expr) (i : Nat) (args mvars : Array Expr) := do
let fType ← whnf fType
let mType ← whnf mType
if not (i < args.size) then return ()
match fType, mType with
| Expr.forallE _ fd fb _, Expr.forallE _ md mb _ => do
-- TODO: do I need to check (← okBottomUp? args[i] mvars[i] fuel).isSafe here?
-- if so, I'll need to take a callback
if isOutParam fd then
tryUnify (args[i]) (mvars[i])
inspectAux (fb.instantiate1 args[i]) (mb.instantiate1 mvars[i]) (i+1) args mvars
| _, _ => return ()
partial def canBottomUp (e : Expr) (mvar? : Option Expr := none) (fuel : Nat := 10) : AnalyzeM Bool := do
-- Here we check if `e` can be safely elaborated without its expected type.
-- These are incomplete (and possibly unsound) heuristics.
-- TODO: do I need to snapshot the state before calling this?
match fuel with
| 0 => false
| fuel + 1 =>
if e.isFVar || e.isMVar || e.isNatLit || e.isStringLit || e.isSort then return true
if getPPAnalyzeTrustOfNat (← getOptions) && e.isAppOfArity `OfNat.ofNat 3 then return true
if getPPAnalyzeTrustOfScientific (← getOptions) && e.isAppOfArity `OfScientific.ofScientific 5 then return true
let f := e.getAppFn
if !f.isConst && !f.isFVar then return false
let args := e.getAppArgs
let fType ← replaceLPsWithVars (← inferType e.getAppFn)
let ⟨mvars, bInfos, resultType⟩ ← forallMetaBoundedTelescope fType e.getAppArgs.size
for i in [:mvars.size] do
if bInfos[i] == BinderInfo.instImplicit then
inspectOutParams args[i] mvars[i]
else if bInfos[i] == BinderInfo.default then
if ← canBottomUp args[i] mvars[i] fuel then tryUnify args[i] mvars[i]
if ← (isHBinOp e <&&> (valUnknown mvars[0] <||> valUnknown mvars[1])) then tryUnify mvars[0] mvars[1]
if mvar?.isSome then tryUnify resultType (← inferType mvar?.get!)
return !(← valUnknown resultType)
partial def trivialBottomUp (e : Expr) : AnalyzeM Bool := do
e.isFVar || e.isMVar || e.isConst
def withKnowing (knowsType knowsLevel : Bool) (x : AnalyzeM α) : AnalyzeM α := do
withReader (fun ctx => { ctx with knowsType := knowsType, knowsLevel := knowsLevel }) x
builtin_initialize analyzeFailureId : InternalExceptionId ← registerInternalExceptionId `analyzeFailure
def checkKnowsType : AnalyzeM Unit := do
if not (← read).knowsType then
throw $ Exception.internal analyzeFailureId
def annotateBoolAt (n : Name) (pos : Pos) : AnalyzeM Unit := do
let opts := (← get).annotations.findD pos {} |>.setBool n true
trace[pp.analyze.annotate] "{pos} {n}"
modify fun s => { s with annotations := s.annotations.insert pos opts }
def annotateBool (n : Name) : AnalyzeM Unit := do
annotateBoolAt n (← getPos)
structure App.Context where
f : Expr
fType : Expr
args : Array Expr
mvars : Array Expr
bInfos : Array BinderInfo
forceRegularApp : Bool
structure App.State where
bottomUps : Array Bool
higherOrders : Array Bool
provideds : Array Bool
namedArgs : Array Name := #[]
abbrev AnalyzeAppM := ReaderT App.Context (StateT App.State AnalyzeM)
mutual
partial def analyze (parentIsApp : Bool := false) : AnalyzeM Unit := do
checkMaxHeartbeats "Delaborator.topDownAnalyze"
trace[pp.analyze] "{(← read).knowsType}.{(← read).knowsLevel}"
let e ← getExpr
let opts ← getOptions
if ← !e.isAtomic <&&> !(getPPProofs opts) <&&> (try Meta.isProof e catch ex => false) then
if getPPProofsWithType opts then
withType $ withKnowing true true $ analyze
else pure ()
else
withReader (fun ctx => { ctx with parentIsApp := parentIsApp }) do
match (← getExpr) with
| Expr.app .. => analyzeApp
| Expr.forallE .. => analyzePi
| Expr.lam .. => analyzeLam
| Expr.const .. => analyzeConst
| Expr.sort .. => analyzeSort
| Expr.proj .. => analyzeProj
| Expr.fvar .. => analyzeFVar
| Expr.mdata .. => analyzeMData
| Expr.letE .. => analyzeLet
| Expr.lit .. => pure ()
| Expr.mvar .. => pure ()
| Expr.bvar .. => pure ()
where
analyzeApp := do
let mut willKnowType := (← read).knowsType
if !(← read).knowsType && !(← canBottomUp (← getExpr)) then
annotateBool `pp.analysis.needsType
withType $ withKnowing true false $ analyze
willKnowType := true
else if ← (!(← read).knowsType <||> (← read).inBottomUp) <&&> isStructureInstance (← getExpr) then
withType do
annotateBool `pp.structureInstanceTypes
withKnowing true false $ analyze
willKnowType := true
withKnowing willKnowType true $ analyzeAppStaged (← getExpr).getAppFn (← getExpr).getAppArgs
analyzeAppStaged (f : Expr) (args : Array Expr) : AnalyzeM Unit := do
let fType ← replaceLPsWithVars (← inferType f)
let ⟨mvars, bInfos, resultType⟩ ← withTransparency TransparencyMode.all $ forallMetaBoundedTelescope fType args.size
let rest := args.extract mvars.size args.size
let args := args.shrink mvars.size
-- Unify with the expected type
if (← read).knowsType then tryUnify (← inferType (mkAppN f args)) resultType
let forceRegularApp : Bool :=
(getPPAnalyzeTrustSubst (← getOptions) && isSubstLike (← getExpr))
|| (getPPAnalyzeTrustCoe (← getOptions) && isCoe (← getExpr))
analyzeAppStagedCore ⟨f, fType, args, mvars, bInfos, forceRegularApp⟩ |>.run' {
bottomUps := mkArray args.size false,
higherOrders := mkArray args.size false,
provideds := mkArray args.size false
}
if not rest.isEmpty then
-- Note: this shouldn't happen for type-correct terms
if !args.isEmpty then
analyzeAppStaged (mkAppN f args) rest
maybeAddBlockImplicit : AnalyzeM Unit := do
-- See `MonadLift.noConfusion for an example where this is necessary.
if !(← read).parentIsApp then
let type ← inferType (← getExpr)
if type.isForall && type.bindingInfo! == BinderInfo.implicit then
annotateBool `pp.analysis.blockImplicit
analyzeConst : AnalyzeM Unit := do
let Expr.const n ls .. ← getExpr | unreachable!
if !(← read).knowsLevel && !ls.isEmpty then
-- TODO: this is a very crude heuristic, motivated by https://github.com/leanprover/lean4/issues/590
unless getPPAnalyzeOmitMax (← getOptions) && ls.any containsBadMax do
annotateBool `pp.universes
maybeAddBlockImplicit
analyzePi : AnalyzeM Unit := do
annotateBool `pp.binderTypes
withBindingDomain $ withKnowing true false analyze
withBindingBody Name.anonymous analyze
analyzeLam : AnalyzeM Unit := do
if !(← read).knowsType then annotateBool `pp.binderTypes
withBindingDomain $ withKnowing true false analyze
withBindingBody Name.anonymous analyze
analyzeLet : AnalyzeM Unit := do
let Expr.letE n t v body .. ← getExpr | unreachable!
if !(← canBottomUp v) then
annotateBool `pp.analysis.letVarType
withLetVarType $ withKnowing true false analyze
withLetValue $ withKnowing true true analyze
else
withReader (fun ctx => { ctx with inBottomUp := true }) do
withLetValue $ withKnowing true true analyze
withLetBody analyze
analyzeSort : AnalyzeM Unit := pure ()
analyzeProj : AnalyzeM Unit := withProj analyze
analyzeFVar : AnalyzeM Unit := maybeAddBlockImplicit
analyzeMData : AnalyzeM Unit := withMDataExpr analyze
partial def analyzeAppStagedCore : AnalyzeAppM Unit := do
collectBottomUps
checkOutParams
collectHigherOrders
hBinOpHeuristic
collectTrivialBottomUps
discard <| processPostponed (mayPostpone := true)
analyzeFn
for i in [:(← read).args.size] do analyzeArg i
maybeSetExplicit
where
collectBottomUps := do
let ⟨_, _, args, mvars, bInfos, _⟩ ← read
for target in [fun _ => none, fun i => some mvars[i]] do
for i in [:args.size] do
if bInfos[i] == BinderInfo.default then
if ← typeUnknown mvars[i] <&&> canBottomUp args[i] (target i) then
tryUnify args[i] mvars[i]
modify fun s => { s with bottomUps := s.bottomUps.set! i true }
checkOutParams := do
let ⟨_, _, args, mvars, bInfos, _⟩ ← read
for i in [:args.size] do
if bInfos[i] == BinderInfo.instImplicit then inspectOutParams args[i] mvars[i]
collectHigherOrders := do
let ⟨_, _, args, mvars, bInfos, _⟩ ← read
for i in [:args.size] do
if not (← bInfos[i] == BinderInfo.implicit) then continue
if not (← isHigherOrder (← inferType args[i])) then continue
if getPPAnalyzeTrustId (← getOptions) && isIdLike args[i] then continue
if getPPAnalyzeTrustKnownFOType2TypeHOFuns (← getOptions) && not (← valUnknown mvars[i])
&& (← isType2Type (args[i])) && (← isFOLike (args[i])) then continue
tryUnify args[i] mvars[i]
modify fun s => { s with higherOrders := s.higherOrders.set! i true }
hBinOpHeuristic := do
let ⟨_, _, args, mvars, bInfos, _⟩ ← read
if ← (isHBinOp (← getExpr) <&&> (valUnknown mvars[0] <||> valUnknown mvars[1])) then
tryUnify mvars[0] mvars[1]
collectTrivialBottomUps := do
-- motivation: prevent levels from printing in
-- Boo.mk : {α : Type u_1} → {β : Type u_2} → α → β → Boo.{u_1, u_2} α β
let ⟨_, _, args, mvars, bInfos, _⟩ ← read
for i in [:args.size] do
if bInfos[i] == BinderInfo.default then
if ← valUnknown mvars[i] <&&> trivialBottomUp args[i] then
tryUnify args[i] mvars[i]
modify fun s => { s with bottomUps := s.bottomUps.set! i true }
analyzeFn := do
-- Now, if this is the first staging, analyze the n-ary function without expected type
let ⟨f, fType, _, _, _, forceRegularApp⟩ ← read
if !f.isApp then withKnowing false (forceRegularApp || !(← hasLevelMVarAtCurrDepth (← instantiateMVars fType))) $ withNaryFn (analyze (parentIsApp := true))
annotateNamedArg (n : Name) : AnalyzeAppM Unit := do
annotateBool `pp.analysis.namedArg
modify fun s => { s with namedArgs := s.namedArgs.push n }
analyzeArg (i : Nat) := do
let ⟨f, _, args, mvars, bInfos, forceRegularApp⟩ ← read
let ⟨bottomUps, higherOrders,_, _⟩ ← get
let arg := args[i]
let argType ← inferType arg
withNaryArg (f.getAppNumArgs + i) do
withTheReader Context (fun ctx => { ctx with inBottomUp := ctx.inBottomUp || bottomUps[i] }) do
match bInfos[i] with
| BinderInfo.default =>
if ← !(← valUnknown mvars[i]) <&&> !(← readThe Context).inBottomUp <&&> !(← isFunLike arg) <&&> checkpointDefEq mvars[i] arg then
annotateBool `pp.analysis.hole
else
modify fun s => { s with provideds := s.provideds.set! i true }
| BinderInfo.implicit =>
if (← valUnknown mvars[i] <||> higherOrders[i]) && !forceRegularApp then
annotateNamedArg (← mvarName mvars[i])
modify fun s => { s with provideds := s.provideds.set! i true }
else
annotateBool `pp.analysis.skip
| BinderInfo.instImplicit =>
-- Note: apparently checking valUnknown here is not sound, because the elaborator
-- will not happily assign instImplicits that it cannot synthesize
let mut provided := true
if getPPAnalyzeCheckInstances (← getOptions) then
let instResult ← try trySynthInstance argType catch _ => LOption.undef
match instResult with
| LOption.some inst =>
if ← checkpointDefEq inst arg then annotateBool `pp.analysis.skip; provided := false
else annotateNamedArg (← mvarName mvars[i])
| _ => annotateNamedArg (← mvarName mvars[i])
else provided := false
modify fun s => { s with provideds := s.provideds.set! i provided }
| BinderInfo.auxDecl => pure ()
| BinderInfo.strictImplicit => unreachable!
if (← get).provideds[i] then withKnowing (not (← typeUnknown mvars[i])) true analyze
tryUnify mvars[i] args[i]
maybeSetExplicit := do
let ⟨f, _, args, mvars, bInfos, forceRegularApp⟩ ← read
if (← get).namedArgs.any nameNotRoundtrippable then
annotateBool `pp.explicit
for i in [:args.size] do
if !(← get).provideds[i] then
withNaryArg (f.getAppNumArgs + i) do annotateBool `pp.analysis.hole
end
end TopDownAnalyze
open TopDownAnalyze SubExpr
def topDownAnalyze (e : Expr) : MetaM OptionsPerPos := do
let s₀ ← get
traceCtx `pp.analyze do
withReader (fun ctx => { ctx with config := Lean.Elab.Term.setElabConfig ctx.config }) do
let ϕ : AnalyzeM OptionsPerPos := do withNewMCtxDepth analyze; (← get).annotations
try
let knowsType := getPPAnalyzeKnowsType (← getOptions)
ϕ { knowsType := knowsType, knowsLevel := knowsType } (mkRoot e) |>.run' {}
catch ex =>
trace[pp.analyze.error] "failed"
pure {}
finally set s₀
builtin_initialize
registerTraceClass `pp.analyze
registerTraceClass `pp.analyze.annotate
registerTraceClass `pp.analyze.tryUnify
registerTraceClass `pp.analyze.error
end Lean.PrettyPrinter.Delaborator
|
c2ff5f52ab3db0fa46d391fd2d2fd87cdcb0c552 | 1dd0001f48991684032999e18f88be3ece4261d7 | /src/submissions/practice_1.lean | 9080614a3a22b2531d7b87c3c5419287cc699d20 | [] | no_license | rayouness/cs2120f21 | e0a3fa89a6ac50c8a83e85bd7795a2710aceb7e2 | 5dfa57f111ec7f322474be1c87992fe1fedd6610 | refs/heads/main | 1,690,957,987,242 | 1,632,333,173,000 | 1,632,333,173,000 | 399,947,440 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,142 | lean | /-
clarksters
cru8jn; https://github.com/rayouness/cs2120f21.git (should end with .git).
-/
/-
EQUALITY
-/
/- #1
Suppose that x, y, z, and w are arbitrary objects of some type,
T; and suppose further that we know (have proofs of the facts)
that x = y, y = z, and w = z. Give a very, very short English
proof of the conjecture that z = w. You can use not only the
axioms of equality, but either of the theorems about properties
of equality that we have proven. Hint: There's something about
this question that makes it much easier to answer than it might
at first appear.
-/
/-
By the symmetry and the assumption that w=z, z=w.
-/
/- #2
Give a formal statement of the conjecture (proposition) from
#1 by filling in the "hole" in the following definition. The
def is a keyword. The name you're binding to your proposition
is prop_1. The type of the value is Prop (which is the type of
all propositions in Lean).
-/
def prop_1 : Prop := ∀ (T : Type) (x y z w : T), x=y → y=z → w=z → z=w
/- #3 (extra credit)
Give a formal proof of the proposition from #2 by filling in
the hole in this next definition. Hint: Use Lean's versions of
the axioms and basic theorems concerning equality. They are,
again, called eq.refl, eq.subst, eq.symm, eq.trans.
-/
theorem prop_1_proof : prop_1 :=
begin
assume T,
assume x y z w,
assume h1,
assume h2,
assume h3,
rw h3,
end
/-
FOR ALL: ∀.
-/
/- #4
Give a very brief explanation in English of the introduction
rule for ∀. For example, suppose you need to prove (∀ x, P x);
what do you do? (I'm being a little informal in leaving out the
type of X.)
-/
/-
Assume x is arbitrary, then show P x is true.
-/
/- #5
Suppose you have a proof, let's call it pf, of the proposition,
(∀ x, P x), and you need a proof of P t, for some particular t.
Write an expression then uses the elimination rule for ∀ to get
such a proof. Complete the answer by replacing the underscores
in the following expression: ( apply pf P t ).
-/
/-
IMPLIES: →
In the "code" that follows, we define two predicates, each
taking one natural number as an argument. We call them ev and
odd. When applied to any value, n, ev yields the proposition
that n is even (n % 2 = 0), while odd yields the proposition
that n is odd (n % 2 = 1).
-/
def ev (n : ℕ) := n % 2 = 0
def odd (n : ℕ) := n % 2 = 1
/- #6
Write a formal version of the proposition that, for *any*
natural number n, *if* n is even, *then* n + 1 is odd. Give
your answer by filling the hole in the following definition.
Hint: put parenthesis around "n + 1" in your answer.
-/
def successor_of_even_is_odd : Prop :=
∀ (n : ℕ), ev n → odd (n+1)
/- #7
Suppose that "its_raining" and "the_streets_are_wet" are
propositions. (We formalize these assumptions as axioms in
what follows. Then give a formal definition of the (larger)
proposition, "if it's raining out then the streets are wet")
by filling in the hole
-/
axioms (raining streets_wet : Prop)
axiom if_raining_then_streets_wet : raining → streets_wet
/- #9
Now suppose that in addition, its_raining is true, and
we have a proof of it, pf_its_raining. Again, we again give
you this assumption formally as an axiom below. Finish
the formal proof that the streets must be wet. Hint: here
you are asked to use the elimination rule for →.
-/
axiom pf_raining : raining
example : streets_wet :=
begin
apply if_raining_then_streets_wet pf_raining,
end
/-
AND: ∧
-/
/- #10
In our last class, we proved that "∧ is *commutative*."
That is, for any given *propositions*, P and Q, (P ∧ Q) →
(Q ∧ P). The way we proved it was to *assume* that we're
given such a P, Q, and proof, pq, of (P ∧ Q) -- applying
the introduction rules for ∀ and →). In this context, we
*use* the proof, pq, to derive separate proofs, let's call
them p, a proof of P, and q, a proof of Q. With these in
hand, we then apply the introduction rule for ∧ to put
them back together into a proof of (Q ∧ P). We give you
a formal version of this proof as a reminder, next.
-/
theorem and_commutative : ∀ (P Q : Prop), P ∧ Q → Q ∧ P :=
begin
assume P Q pq,
apply and.intro _ _,
exact (and.elim_right pq),
exact (and.elim_left pq),
end
/-
Your task now is to prove the theorem, "∧ is *associative*."
What this means is that for arbitrary propositions, P, Q, and
R, if (P ∧ (Q ∧ R)) is true, then ((P ∧ Q) ∧ R) is true, *and
vice versa*. You just need to prove it in the first direction.
Hint, if you have a proof, p_qr, of (P ∧ (Q ∧ R)), then the
application of and.elim_left will give you a proof of P, and
and.elim_right will give you a proof of (Q ∧ R).
To help you along, we give you the first part of the proof,
including an example of a new Lean tactic called have, which
allows you to give a name to a new value in the middle of a
proof script.
-/
theorem and_associative :
∀ (P Q R : Prop),
(P ∧ (Q ∧ R)) → ((P ∧ Q) ∧ R) :=
begin
intros P Q R h,
have p : P := and.elim_left h,
have qr : Q ∧ R := and.elim_right h,
have q : Q := and.elim_left qr,
have r : R := and.elim_right qr,
apply and.intro _ _,
apply and.intro p q,
apply r,
end
/- #11
Give an English language proof of the preceding
theorem. Do it by finishing off the following
partial "proof explanation."
Proof. We assume that P, Q, and R are arbitrary
but specific propositions, and that we have a
proof, let's call it p_qr, of (P ∧ (Q ∧ R)) [by
application of ∧ and → introduction.] What now
remains to be proved is ((P ∧ Q) ∧ R). We can
construct a proof of this proposition by applying
a proof of P, Q, and R to a proof of (P ∧ Q) and a proof of R.
What remains, then, is to obtain these proofs.
But this is easily done by the application of
the elimination rule for ∧ to (P ∧ (Q ∧ R)). QED.
-/
/-
Note that Lean includes versions of these
theorems (and many, many, many others) in
its extensive library of formalized maths,
as the following check commands reveal.
Note the difference in naming relative to
the definitions we give in this file.
-/
#check @and.comm
#check @and.assoc
|
cce8e85acf4a061cf42370b3c79f95ac0eb3ab2b | a4673261e60b025e2c8c825dfa4ab9108246c32e | /src/Lean/Parser/Term.lean | 1141564ade849b191324fcb5cd52fae9a5ca8b0b | [
"Apache-2.0"
] | permissive | jcommelin/lean4 | c02dec0cc32c4bccab009285475f265f17d73228 | 2909313475588cc20ac0436e55548a4502050d0a | refs/heads/master | 1,674,129,550,893 | 1,606,415,348,000 | 1,606,415,348,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 13,888 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Sebastian Ullrich
-/
import Lean.Parser.Basic
import Lean.Parser.Level
namespace Lean
namespace Parser
builtin_initialize
registerBuiltinParserAttribute `builtinTacticParser `tactic (leadingIdentAsSymbol := true)
registerBuiltinDynamicParserAttribute `tacticParser `tactic
@[inline] def tacticParser (rbp : Nat := 0) : Parser :=
categoryParser `tactic rbp
namespace Tactic
def tacticSeq1Indented : Parser :=
parser! many1Indent (group (ppLine >> tacticParser >> optional ";"))
def tacticSeqBracketed : Parser :=
parser! "{" >> many (group (ppLine >> tacticParser >> optional ";")) >> ppDedent (ppLine >> "}")
def tacticSeq :=
nodeWithAntiquot "tacticSeq" `Lean.Parser.Tactic.tacticSeq $ tacticSeqBracketed <|> tacticSeq1Indented
/- Raw sequence for quotation and grouping -/
def seq1 :=
node `Lean.Parser.Tactic.seq1 $ sepBy1 tacticParser ";\n" true
end Tactic
def darrow : Parser := " => "
namespace Term
/- Built-in parsers -/
@[builtinTermParser] def byTactic := parser!:leadPrec "by " >> Tactic.tacticSeq
def optSemicolon (p : Parser) : Parser := ppDedent $ optional ";" >> ppLine >> p
-- `checkPrec` necessary for the pretty printer
@[builtinTermParser] def ident := checkPrec maxPrec >> Parser.ident
@[builtinTermParser] def num : Parser := checkPrec maxPrec >> numLit
@[builtinTermParser] def str : Parser := checkPrec maxPrec >> strLit
@[builtinTermParser] def char : Parser := checkPrec maxPrec >> charLit
@[builtinTermParser] def type := parser! "Type" >> optional (checkWsBefore "" >> checkPrec leadPrec >> checkColGt >> levelParser maxPrec)
@[builtinTermParser] def sort := parser! "Sort" >> optional (checkWsBefore "" >> checkPrec leadPrec >> checkColGt >> levelParser maxPrec)
@[builtinTermParser] def prop := parser! "Prop"
@[builtinTermParser] def hole := parser! "_"
@[builtinTermParser] def syntheticHole := parser! "?" >> (ident <|> hole)
@[builtinTermParser] def «sorry» := parser! "sorry"
@[builtinTermParser] def cdot := parser! "·" <|> "."
@[builtinTermParser] def emptyC := parser! "∅" <|> ("{" >> "}")
def typeAscription := parser! " : " >> termParser
def tupleTail := parser! ", " >> sepBy1 termParser ", "
def parenSpecial : Parser := optional (tupleTail <|> typeAscription)
@[builtinTermParser] def paren := parser! "(" >> ppDedent (withoutPosition (withoutForbidden (optional (termParser >> parenSpecial)))) >> ")"
@[builtinTermParser] def anonymousCtor := parser! "⟨" >> sepBy termParser ", " >> "⟩"
def optIdent : Parser := optional (atomic (ident >> " : "))
def fromTerm := parser! " from " >> termParser
def haveAssign := parser! " := " >> termParser
def haveDecl := optIdent >> termParser >> (haveAssign <|> fromTerm <|> byTactic)
@[builtinTermParser] def «have» := parser!:leadPrec withPosition ("have " >> haveDecl) >> optSemicolon termParser
def sufficesDecl := optIdent >> termParser >> (fromTerm <|> byTactic)
@[builtinTermParser] def «suffices» := parser!:leadPrec withPosition ("suffices " >> sufficesDecl) >> optSemicolon termParser
@[builtinTermParser] def «show» := parser!:leadPrec "show " >> termParser >> (fromTerm <|> byTactic)
def structInstArrayRef := parser! "[" >> termParser >>"]"
def structInstLVal := (ident <|> fieldIdx <|> structInstArrayRef) >> many (group ("." >> (ident <|> fieldIdx)) <|> structInstArrayRef)
def structInstField := ppGroup $ parser! structInstLVal >> " := " >> termParser
@[builtinTermParser] def structInst := parser! "{" >> ppHardSpace >> optional (atomic (termParser >> " with "))
>> manyIndent (group (structInstField >> optional ", "))
>> optional ".."
>> optional (" : " >> termParser) >> " }"
def typeSpec := parser! " : " >> termParser
def optType : Parser := optional typeSpec
@[builtinTermParser] def explicit := parser! "@" >> termParser maxPrec
@[builtinTermParser] def inaccessible := parser! ".(" >> termParser >> ")"
def binderIdent : Parser := ident <|> hole
def binderType (requireType := false) : Parser := if requireType then group (" : " >> termParser) else optional (" : " >> termParser)
def binderTactic := parser! atomic (" := " >> " by ") >> Tactic.tacticSeq
def binderDefault := parser! " := " >> termParser
def explicitBinder (requireType := false) := ppGroup $ parser! "(" >> many1 binderIdent >> binderType requireType >> optional (binderTactic <|> binderDefault) >> ")"
def implicitBinder (requireType := false) := ppGroup $ parser! "{" >> many1 binderIdent >> binderType requireType >> "}"
def instBinder := ppGroup $ parser! "[" >> optIdent >> termParser >> "]"
def bracketedBinder (requireType := false) := explicitBinder requireType <|> implicitBinder requireType <|> instBinder
/-
It is feasible to support dependent arrows such as `{α} → α → α` without sacrificing the quality of the error messages for the longer case.
`{α} → α → α` would be short for `{α : Type} → α → α`
Here is the encoding:
```
def implicitShortBinder := node `Lean.Parser.Term.implicitBinder $ "{" >> many1 binderIdent >> pushNone >> "}"
def depArrowShortPrefix := try (implicitShortBinder >> unicodeSymbol " → " " -> ")
def depArrowLongPrefix := bracketedBinder true >> unicodeSymbol " → " " -> "
def depArrowPrefix := depArrowShortPrefix <|> depArrowLongPrefix
@[builtinTermParser] def depArrow := parser! depArrowPrefix >> termParser
```
Note that no changes in the elaborator are needed.
We decided to not use it because terms such as `{α} → α → α` may look too cryptic.
Note that we did not add a `explicitShortBinder` parser since `(α) → α → α` is really cryptic as a short for `(α : Type) → α → α`.
-/
@[builtinTermParser] def depArrow := parser!:25 bracketedBinder true >> unicodeSymbol " → " " -> " >> termParser
def simpleBinder := parser! many1 binderIdent >> optType
@[builtinTermParser]
def «forall» := parser!:leadPrec unicodeSymbol "∀ " "forall" >> many1 (ppSpace >> (simpleBinder <|> bracketedBinder)) >> ", " >> termParser
def matchAlt : Parser :=
nodeWithAntiquot "matchAlt" `Lean.Parser.Term.matchAlt $
sepBy1 termParser ", " >> darrow >> termParser
def matchAlts (optionalFirstBar := true) : Parser :=
parser! ppDedent $ withPosition $
ppLine >> (if optionalFirstBar then optional "| " else "| ") >>
sepBy1 (ppIndent matchAlt) (ppLine >> checkColGe "alternatives must be indented" >> "| ")
def matchDiscr := parser! optional (atomic (ident >> checkNoWsBefore "no space before ':'" >> ":")) >> termParser
@[builtinTermParser] def «match» := parser!:leadPrec "match " >> sepBy1 matchDiscr ", " >> optType >> " with " >> matchAlts
@[builtinTermParser] def «nomatch» := parser!:leadPrec "nomatch " >> termParser
def funImplicitBinder := atomic (lookahead ("{" >> many1 binderIdent >> (" : " <|> "}"))) >> implicitBinder
def funSimpleBinder := atomic (lookahead (many1 binderIdent >> " : ")) >> simpleBinder
def funBinder : Parser := funImplicitBinder <|> instBinder <|> funSimpleBinder <|> termParser maxPrec
-- NOTE: we use `nodeWithAntiquot` to ensure that `fun $b => ...` remains a `term` antiquotation
def basicFun : Parser := nodeWithAntiquot "basicFun" `Lean.Parser.Term.basicFun (many1 (ppSpace >> funBinder) >> darrow >> termParser)
@[builtinTermParser] def «fun» := parser!:maxPrec unicodeSymbol "λ" "fun" >> (basicFun <|> matchAlts false)
def optExprPrecedence := optional (atomic ":" >> termParser maxPrec)
@[builtinTermParser] def «parser!» := parser!:leadPrec "parser! " >> optExprPrecedence >> termParser
@[builtinTermParser] def «tparser!» := parser!:leadPrec "tparser! " >> optExprPrecedence >> termParser
@[builtinTermParser] def borrowed := parser! "@&" >> termParser leadPrec
@[builtinTermParser] def quotedName := parser! nameLit
-- NOTE: syntax quotations are defined in Init.Lean.Parser.Command
@[builtinTermParser] def «match_syntax» := parser!:leadPrec "match_syntax" >> termParser >> " with " >> matchAlts
def simpleBinderWithoutType := node `Lean.Parser.Term.simpleBinder (many1 binderIdent >> pushNone)
/- Remark: we use `checkWsBefore` to ensure `let x[i] := e; b` is not parsed as `let x [i] := e; b` where `[i]` is an `instBinder`. -/
def letIdLhs : Parser := ident >> checkWsBefore "expected space before binders" >> many (ppSpace >> (simpleBinderWithoutType <|> bracketedBinder)) >> optType
def letIdDecl := node `Lean.Parser.Term.letIdDecl $ atomic (letIdLhs >> " := ") >> termParser
def letPatDecl := node `Lean.Parser.Term.letPatDecl $ atomic (termParser >> pushNone >> optType >> " := ") >> termParser
def letEqnsDecl := node `Lean.Parser.Term.letEqnsDecl $ letIdLhs >> matchAlts false
-- Remark: we use `nodeWithAntiquot` here to make sure anonymous antiquotations (e.g., `$x`) are not `letDecl`
def letDecl := nodeWithAntiquot "letDecl" `Lean.Parser.Term.letDecl (notFollowedBy (nonReservedSymbol "rec") "rec" >> (letIdDecl <|> letPatDecl <|> letEqnsDecl))
@[builtinTermParser] def «let» := parser!:leadPrec withPosition ("let " >> letDecl) >> optSemicolon termParser
@[builtinTermParser] def «let!» := parser!:leadPrec withPosition ("let! " >> letDecl) >> optSemicolon termParser
@[builtinTermParser] def «let*» := parser!:leadPrec withPosition ("let* " >> letDecl) >> optSemicolon termParser
def attrArg : Parser := ident <|> strLit <|> numLit
-- use `rawIdent` because of attribute names such as `instance`
def attrInstance := ppGroup $ parser! rawIdent >> many (ppSpace >> attrArg)
def attributes := parser! "@[" >> sepBy1 attrInstance ", " >> "]"
def letRecDecls := sepBy1 (group (optional «attributes» >> letDecl)) ", "
@[builtinTermParser]
def «letrec» := parser!:leadPrec withPosition (group ("let " >> nonReservedSymbol "rec ") >> letRecDecls) >> optSemicolon termParser
@[runBuiltinParserAttributeHooks]
def whereDecls := parser! "where " >> many1Indent (group (optional «attributes» >> letDecl >> optional ";"))
@[runBuiltinParserAttributeHooks]
def matchAltsWhereDecls := parser! matchAlts false >> optional whereDecls
@[builtinTermParser] def nativeRefl := parser! "nativeRefl! " >> termParser maxPrec
@[builtinTermParser] def nativeDecide := parser! "nativeDecide!"
@[builtinTermParser] def decide := parser! "decide!"
@[builtinTermParser] def typeOf := parser! "typeOf! " >> termParser maxPrec
@[builtinTermParser] def ensureTypeOf := parser! "ensureTypeOf! " >> termParser maxPrec >> strLit >> termParser
@[builtinTermParser] def ensureExpectedType := parser! "ensureExpectedType! " >> strLit >> termParser maxPrec
@[builtinTermParser] def not := parser! "¬" >> termParser 40
@[builtinTermParser] def bnot := parser! "!" >> termParser 40
-- symbol precedence should be higher, but must match the one of `sub` below
@[builtinTermParser] def uminus := parser!:65 "-" >> termParser 100
def namedArgument := parser! atomic ("(" >> ident >> " := ") >> termParser >> ")"
def ellipsis := parser! ".."
@[builtinTermParser] def app := tparser!:(maxPrec-1) many1 $
checkWsBefore "expected space" >>
checkColGt "expected to be indented" >>
(namedArgument <|> ellipsis <|> termParser maxPrec)
@[builtinTermParser] def proj := tparser! checkNoWsBefore >> "." >> (fieldIdx <|> ident)
@[builtinTermParser] def arrayRef := tparser! checkNoWsBefore >> "[" >> termParser >>"]"
@[builtinTermParser] def arrow := tparser! checkPrec 25 >> unicodeSymbol " → " " -> " >> termParser 25
def isIdent (stx : Syntax) : Bool :=
-- antiquotations should also be allowed where an identifier is expected
stx.isAntiquot || stx.isIdent
@[builtinTermParser] def explicitUniv : TrailingParser := tparser! checkStackTop isIdent "expected preceding identifier" >> checkNoWsBefore "no space before '.{'" >> ".{" >> sepBy1 levelParser ", " >> "}"
@[builtinTermParser] def namedPattern : TrailingParser := tparser! checkStackTop isIdent "expected preceding identifier" >> checkNoWsBefore "no space before '@'" >> "@" >> termParser maxPrec
@[builtinTermParser] def pipeProj := tparser!:0 " |>. " >> (fieldIdx <|> ident)
@[builtinTermParser] def subst := tparser!:75 " ▸ " >> sepBy1 (termParser 75) " ▸ "
@[builtinTermParser] def funBinder.quot : Parser := parser! "`(funBinder|" >> toggleInsideQuot funBinder >> ")"
@[builtinTermParser] def panic := parser!:leadPrec "panic! " >> termParser
@[builtinTermParser] def unreachable := parser!:leadPrec "unreachable!"
@[builtinTermParser] def dbgTrace := parser!:leadPrec withPosition ("dbgTrace! " >> ((interpolatedStr termParser) <|> termParser)) >> optSemicolon termParser
@[builtinTermParser] def assert := parser!:leadPrec withPosition ("assert! " >> termParser) >> optSemicolon termParser
def macroArg := termParser maxPrec
def macroDollarArg := parser! "$" >> termParser 0
def macroLastArg := macroDollarArg <|> macroArg
-- Macro for avoiding exponentially big terms when using `STWorld`
@[builtinTermParser] def stateRefT := parser! "StateRefT" >> macroArg >> macroLastArg
end Term
@[builtinTermParser 1] def Tactic.quot : Parser := parser! "`(tactic|" >> toggleInsideQuot tacticParser >> ")"
@[builtinTermParser] def Tactic.quotSeq : Parser := parser! "`(tactic|" >> toggleInsideQuot Tactic.seq1 >> ")"
@[builtinTermParser] def Level.quot : Parser := parser! "`(level|" >> toggleInsideQuot levelParser >> ")"
builtin_initialize
registerParserAlias! "letDecl" Term.letDecl
registerParserAlias! "haveDecl" Term.haveDecl
registerParserAlias! "sufficesDecl" Term.sufficesDecl
registerParserAlias! "letRecDecls" Term.letRecDecls
registerParserAlias! "hole" Term.hole
registerParserAlias! "syntheticHole" Term.syntheticHole
registerParserAlias! "matchDiscr" Term.matchDiscr
end Parser
end Lean
|
8a433b98439a7200a138c35db4129c7e13ee9ee8 | a4673261e60b025e2c8c825dfa4ab9108246c32e | /tests/compiler/t4.lean | 7b9885a928d1ffba3e2f672f6d3143280fead1cf | [
"Apache-2.0"
] | permissive | jcommelin/lean4 | c02dec0cc32c4bccab009285475f265f17d73228 | 2909313475588cc20ac0436e55548a4502050d0a | refs/heads/master | 1,674,129,550,893 | 1,606,415,348,000 | 1,606,415,348,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,475 | lean | #lang lean4
/- Benchmark for new code generator -/
inductive Expr
| Val : Int → Expr
| Var : String → Expr
| Add : Expr → Expr → Expr
| Mul : Expr → Expr → Expr
| Pow : Expr → Expr → Expr
| Ln : Expr → Expr
namespace Expr
protected def Expr.toString : Expr → String
| Val n => toString n
| Var x => x
| Add f g => "(" ++ Expr.toString f ++ " + " ++ Expr.toString g ++ ")"
| Mul f g => "(" ++ Expr.toString f ++ " * " ++ Expr.toString g ++ ")"
| Pow f g => "(" ++ Expr.toString f ++ " ^ " ++ Expr.toString g ++ ")"
| Ln f => "ln(" ++ Expr.toString f ++ ")"
instance : ToString Expr :=
⟨Expr.toString⟩
partial def pown : Int → Int → Int
| a, 0 => 1
| a, 1 => a
| a, n =>
let b := pown a (n / 2);
b * b * (if n % 2 = 0 then 1 else a)
partial def addAux : Expr → Expr → Expr
| Val n, Val m => Val (n + m)
| Val 0, f => f
| f, Val 0 => f
| f, Val n => addAux (Val n) f
| Val n, Add (Val m) f => addAux (Val (n+m)) f
| f, Add (Val n) g => addAux (Val n) (addAux f g)
| Add f g, h => addAux f (addAux g h)
| f, g => Add f g
def add (a b : Expr) : Expr :=
-- dbgTrace (">> add (" ++ toString a ++ ", " ++ toString b ++ ")") $ fun _ =>
addAux a b
-- set_option trace.compiler.borrowed_inference true
partial def mulAux : Expr → Expr → Expr
| Val n, Val m => Val (n*m)
| Val 0, _ => Val 0
| _, Val 0 => Val 0
| Val 1, f => f
| f, Val 1 => f
| f, Val n => mulAux (Val n) f
| Val n, Mul (Val m) f => mulAux (Val (n*m)) f
| f, Mul (Val n) g => mulAux (Val n) (mulAux f g)
| Mul f g, h => mulAux f (mulAux g h)
| f, g => Mul f g
def mul (a b : Expr) : Expr :=
-- dbgTrace (">> mul (" ++ toString a ++ ", " ++ toString b ++ ")") $ fun _ =>
mulAux a b
def pow : Expr → Expr → Expr
| Val m, Val n => Val (pown m n)
| _, Val 0 => Val 1
| f, Val 1 => f
| Val 0, _ => Val 0
| f, g => Pow f g
def ln : Expr → Expr
| Val 1 => Val 0
| f => Ln f
def d (x : String) : Expr → Expr
| Val _ => Val 0
| Var y => if x = y then Val 1 else Val 0
| Add f g => add (d x f) (d x g)
| Mul f g =>
-- dbgTrace (">> d (" ++ toString f ++ ", " ++ toString g ++ ")") $ fun _ =>
add (mul f (d x g)) (mul g (d x f))
| Pow f g => mul (pow f g) (add (mul (mul g (d x f)) (pow f (Val (-1)))) (mul (ln f) (d x g)))
| Ln f => mul (d x f) (pow f (Val (-1)))
def count : Expr → Nat
| Val _ => 1
| Var _ => 1
| Add f g => count f + count g
| Mul f g => count f + count g
| Pow f g => count f + count g
| Ln f => count f
def nestAux (s : Nat) (f : Nat → Expr → IO Expr) : Nat → Expr → IO Expr
| 0, x => pure x
| m@(n+1), x => f (s - m) x >>= nestAux s f n
def nest (f : Nat → Expr → IO Expr) (n : Nat) (e : Expr) : IO Expr :=
nestAux n f n e
def deriv (i : Nat) (f : Expr) : IO Expr :=
do
let d := d "x" f;
IO.println (toString (i+1) ++ " count: " ++ (toString $ count d));
IO.println (toString d);
pure d
end Expr
def main (xs : List String) : IO UInt32 :=
do let x := Expr.Var "x";
let f := Expr.add x (Expr.mul x (Expr.mul x (Expr.add x x)));
IO.println f;
Expr.nest Expr.deriv 3 f;
pure 0
-- setOption profiler True
-- #eval main []
|
50cc73612e78c6e152d814435eb6d83efc662ec2 | 43390109ab88557e6090f3245c47479c123ee500 | /src/M3P14/Arithmetic_functions/sum_over_divisors.lean | 297be548cdfbcd81247f9c44882e323261533663 | [
"Apache-2.0"
] | permissive | Ja1941/xena-UROP-2018 | 41f0956519f94d56b8bf6834a8d39473f4923200 | b111fb87f343cf79eca3b886f99ee15c1dd9884b | refs/heads/master | 1,662,355,955,139 | 1,590,577,325,000 | 1,590,577,325,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,992 | lean | import algebra.big_operators algebra.group_power data.pnat M3P14.Arithmetic_functions.phi
-- positive naturals
-- todo: add all the properties of dirichlet conv
namespace nat
open list
/-- returns the finset of divisors of a positive natural -/
definition factors_new (d : ℕ+) : list ℕ :=
filter (λ e, e ∣ d) (range (d+1))
#eval factors_new 6 -- [1, 2, 3, 6]
lemma mem_factors_iff_divides (d : ℕ+) (e : ℕ) : e ∈ factors_new d ↔ e ∣ d :=
by simp [factors_new, -add_comm, nat.lt_succ_iff];
exact and_iff_right_of_imp (le_of_dvd d.2)
lemma nodup_factors (d : ℕ+) : nodup (factors_new d) :=
nodup_filter _ (nodup_range (d+1) : nodup (range (d+1)))
/-- returns the sum of f(e) as e ranges over the divisors of d (positive nat) -/
definition divisor_sum
{β : Type*} [add_comm_monoid β] (f : ℕ → β) (d : ℕ+) : β :=
finset.sum (⟨quotient.mk (factors_new d),nodup_factors d⟩) f
def perfect_num (n : ℕ+) : Prop := divisor_sum id n = 2*n
--theorem perfect_number_iff_mersenne (n p: ℕ+) (hp: prime p) : perfect_num n ↔ n = 2^(p-1)*((2^p)-1)
instance : decidable_pred perfect_num := λ _, by unfold perfect_num; apply_instance
theorem divisor_sum_phi (n : ℕ+) : divisor_sum (phi) n = n := sorry
#eval divisor_sum (id) (6) -- it's a perfect number!
#reduce (perfect_num 6 : bool)
#eval (perfect_num 5 : bool)
--dirichlet convolution
def conv (f g : ℕ → ℕ) := λ (n : pnat), divisor_sum (λ d, f d * g (n / d)) n
--#eval conv phi id
-- lemmas about conv
lemma conv_is_comm (f g : ℕ → ℕ) : conv f g = conv g f := sorry
--lemma conv_is_assoc (f g h : ℕ → ℕ) : conv (conv f g) h = conv f (conv g h) := sorry
--lemma conv_is_add_dist (f g h : ℕ → ℕ) : conv f (g + h) = conv f g + conv f h
--lemma conv_id_is_conv (f : ℕ → ℕ) : conv f id = f := sorry
--lemma conv_mul_is_mul (f g : ℕ → ℕ) : is_mult f
--lemma conv_has_inv (f : ℕ → ℕ) (hp : f 1 ≠ 0) : ∃ g : conv f g = id := sorry
end nat
|
e068ea58587eb74fd6b15cf9a3fa5f05746e27ee | 9bf90df35bb15a2f76571e35c48192142a328c40 | /src/ch8.lean | 8483a3a5379c84e12554bf400e44b83b27f374e7 | [] | no_license | ehaskell1/set_theory | ed0726520e84990d5f3180bafa0a3674ed31fb5e | e6c829c4dd953d98c9cba08f9f79784cd91794fb | refs/heads/master | 1,693,282,405,362 | 1,636,928,916,000 | 1,636,928,916,000 | 428,055,746 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 195,675 | lean | import ch7_cont
universe u
namespace Set
local attribute [irreducible] mem
noncomputable def con_fun (G : Set → Set) (α : Set) : Set := trans_rec α α.eps_order G
lemma con_fun_fun {G : Set → Set} {α : Set} (αord : α.is_ordinal) : (con_fun G α).is_function := trans_rec_fun (ordinal_well_ordered αord)
lemma con_fun_dom {G : Set → Set} {α : Set} (αord : α.is_ordinal) : (con_fun G α).dom = α := trans_rec_dom (ordinal_well_ordered αord)
noncomputable def ord_rec (G : Set → Set) : Set → Set :=
fun α, (con_fun G α.succ).fun_value α
lemma con_unique {G : Set → Set}
{f : Set} (ffun : f.is_function) (fdom : f.dom.is_ordinal) (fspec : ∀ {β : Set}, β ∈ f.dom → f.fun_value β = G (f.restrict β))
{g : Set} (gfun : g.is_function) (gdom : g.dom.is_ordinal) (gspec : ∀ {β : Set}, β ∈ g.dom → g.fun_value β = G (g.restrict β))
{α : Set} (αfg : α ∈ f.dom ∩ g.dom) : f.fun_value α = g.fun_value α :=
begin
have αord : α.is_ordinal, apply ord_of_mem_ord fdom, rw mem_inter at αfg, exact αfg.left,
revert α αord, refine trans_ind_schema _, intros α αord ind αfg,
rw mem_inter at αfg, rw [fspec αfg.left, gspec αfg.right], congr' 1,
have αf : α ⊆ f.dom, rw ←ord_le_iff_sub αord fdom, left, exact αfg.left,
have αg : α ⊆ g.dom, rw ←ord_le_iff_sub αord gdom, left, exact αfg.right,
apply fun_ext (restrict_is_function ffun) (restrict_is_function gfun),
rw [restrict_dom αf, restrict_dom αg],
intros β βα, rw restrict_dom αf at βα,
rw [restrict_fun_value ffun αf βα, restrict_fun_value gfun αg βα],
apply ind βα, rw mem_inter, cases αfg, split; refine ord_mem_trans _ βα _; assumption,
end
lemma con_fun_spec {G : Set → Set} {α : Set} (αord : α.is_ordinal) ⦃β : Set⦄ (βα : β ∈ α) : (con_fun G α).fun_value β = ord_rec G β :=
begin
have βord' := succ_ord_of_ord (ord_of_mem_ord αord βα),
refine @con_unique G _ (con_fun_fun αord) _ _ _ (con_fun_fun βord') _ _ _ _,
{ rw con_fun_dom αord, exact αord, },
{ intros β βα, rw con_fun_dom αord at βα,
rw [con_fun, trans_rec_spec (ordinal_well_ordered αord) βα, seg_ord_eq_self αord βα], },
{ rw con_fun_dom βord', exact βord', },
{ intros β ββ, rw con_fun_dom βord' at ββ,
rw [con_fun, trans_rec_spec (ordinal_well_ordered βord') ββ, seg_ord_eq_self βord' ββ], },
{ rw [con_fun_dom αord, con_fun_dom βord', mem_inter],
exact ⟨βα, self_mem_succ⟩, },
end
lemma ord_rec_spec {G : Set → Set} {α : Set} (αord : α.is_ordinal) : ord_rec G α = G (con_fun G α) :=
begin
have αord' := succ_ord_of_ord αord,
rw [ord_rec], dsimp, rw [con_fun, trans_rec_spec (ordinal_well_ordered αord') self_mem_succ],
congr, apply fun_ext (restrict_is_function (trans_rec_fun (ordinal_well_ordered αord'))) (con_fun_fun αord),
rw [con_fun_dom αord, seg_ord_eq_self αord' self_mem_succ],
apply restrict_dom, rw trans_rec_dom (ordinal_well_ordered αord'), exact self_sub_succ,
intros β βα, rw seg_ord_eq_self αord' self_mem_succ at *,
rw [restrict_dom_inter, trans_rec_dom (ordinal_well_ordered αord'), inter_comm, inter_eq_of_sub self_sub_succ] at βα,
rw restrict_fun_value (trans_rec_fun (ordinal_well_ordered αord')) _ βα, swap,
rw trans_rec_dom (ordinal_well_ordered αord'), exact self_sub_succ,
rw [←con_fun, con_fun_spec αord' (self_sub_succ βα), con_fun_spec αord βα],
end
lemma con_fun_ran_eq {f : Set → Set} {α : Set} (αord : α.is_ordinal) : (con_fun f α).ran = repl_img (ord_rec f) α :=
begin
rw eq_iff_subset_and_subset, split,
rw subset_def, apply @of_ran _ (@con_fun_fun f _ αord) (λ y, y ∈ repl_img (ord_rec f) α),
intros β βα, rw con_fun_dom αord at βα, rw [con_fun_spec αord βα, mem_repl_img], finish,
apply of_repl_img, intros β βα, rw mem_ran_iff (con_fun_fun αord),
rw [con_fun_dom αord, ←con_fun_spec αord βα], finish,
end
theorem Veb_eq' {α : Set} (αord : α.is_ordinal) : α.Veb = ord_rec (fun f, f.ran.powersets.Union) α :=
begin
revert α, apply trans_ind_schema, intros α αord ind,
rw [ord_rec_spec αord, Veb_eq αord], congr, apply ext,
intro X, rw [mem_Veb_img, mem_ran_iff (con_fun_fun αord), con_fun_dom αord],
apply exists_congr, intro β, rw and.congr_right_iff, intro βα,
rw [ind βα, ord_rec], dsimp, apply eq_iff_eq_cancel_left.mpr,
rw [con_fun_spec αord βα, con_fun_spec (succ_ord_of_ord (ord_of_mem_ord αord βα)) self_mem_succ],
end
lemma exists_not_mem {p : Set → Prop} (pclass : ¬ ∃ X : Set, ∀ {z : Set}, z ∈ X ↔ p z)
{X : Set} : ∃ x : Set, p x ∧ x ∉ X :=
begin
apply classical.by_contradiction, intro h, push_neg at h,
apply pclass, use {z ∈ X | p z}, intro z,
rw [mem_sep, and_iff_right_iff_imp], exact h _,
end
lemma ord_set_exists_iff_bounded {p : Set → Prop} (pord : ∀ {α : Set}, p α → α.is_ordinal) :
(∃ X : Set, ∀ {α : Set}, α ∈ X ↔ p α) ↔ ∃ β : Set, β.is_ordinal ∧ ∀ {α : Set}, p α → α ≤ β :=
begin
split,
refine classical.by_contradiction _, intro h,
push_neg at h, rcases h with ⟨⟨X, hX⟩, h⟩,
refine not_exists_ord_set ⟨X.Union, _⟩, intro β, simp only [mem_Union, exists_prop], split,
rintro ⟨γ, γX, βγ⟩, refine ord_of_mem_ord _ βγ,
apply pord, rw ←hX, exact γX,
intro βord, rcases h _ βord with ⟨α, pα, βα⟩,
rw ord_not_le_iff_lt (pord pα) βord at βα,
use α, rw hX, exact ⟨pα, βα⟩,
rintro ⟨β, -, h⟩, use {α ∈ β.succ | p α},
intro α, rw [mem_sep, and_iff_right_iff_imp],
rw mem_succ_iff_le, exact h,
end
local attribute [instance] classical.prop_decidable
lemma exists_least_inf_card (X : Set) : ∃ κ : Set, κ.is_cardinal ∧ ¬ κ.finite_cardinal ∧ κ ∉ X
∧ ∀ {μ : Set}, μ.is_cardinal → ¬ μ.finite_cardinal → μ ∉ X → κ.card_le μ :=
begin
have h₁ : ∃ κ : Set, (κ.is_cardinal ∧ ¬ κ.finite_cardinal) ∧ κ ∉ X,
apply exists_not_mem, rw ord_set_exists_iff_bounded, swap,
rintros α ⟨αcard, -⟩, exact card_is_ord αcard,
push_neg, intros α αord,
by_cases αfin : α.is_finite,
refine ⟨card ω, ⟨⟨_, rfl⟩, aleph_null_infinite_cardinal⟩, _⟩,
rw [ord_not_le_iff_lt (card_is_ord ⟨_, rfl⟩) αord, card_of_cardinal_eq_self omega_is_card],
apply ord_lt_of_card_lt αord omega_is_ord,
exact finite_card_lt_aleph_null' αfin,
have exp_card := exp_cardinal (nat_is_cardinal two_nat) ⟨α, rfl⟩,
have lt : α.card.card_lt (card_exp two α.card) := card_lt_exp ⟨_, rfl⟩,
refine ⟨card_exp two α.card, ⟨exp_card, _⟩, _⟩,
refine card_inf_of_ge_inf ⟨α, rfl⟩ _ exp_card _,
rw card_finite_iff_finite, exact αfin,
rw card_le_iff, left, exact lt,
rw [ord_not_le_iff_lt (card_is_ord exp_card) αord],
apply ord_lt_of_card_lt αord (card_is_ord exp_card),
rw card_of_cardinal_eq_self exp_card, exact lt,
have h₂ : ∃ κ : Set, κ.is_ordinal ∧ κ.is_cardinal ∧ ¬ κ.finite_cardinal ∧ κ ∉ X,
rcases h₁ with ⟨κ, ⟨κcard, κfin⟩, κX⟩, exact ⟨_, card_is_ord κcard, κcard, κfin, κX⟩,
obtain ⟨κ, κord, ⟨κcard, κfin, κX⟩, least⟩ := exists_least_ord_of_exists h₂,
refine ⟨_, κcard, κfin, κX, λ μ μcard μfin μX, _⟩,
specialize least (card_is_ord μcard) ⟨μcard, μfin, μX⟩,
rw card_le_iff_le κcard μcard, exact least,
end
noncomputable def Aleph : Set → Set := ord_rec (fun f, classical.some (exists_least_inf_card f.ran))
lemma Aleph_spec {α : Set} (αord : α.is_ordinal) : α.Aleph.is_cardinal ∧ ¬ α.Aleph.finite_cardinal ∧ α.Aleph ∉ (con_fun (fun f, classical.some (exists_least_inf_card f.ran)) α).ran
∧ ∀ {μ : Set}, μ.is_cardinal → ¬ μ.finite_cardinal → μ ∉ (con_fun (fun f, classical.some (exists_least_inf_card f.ran)) α).ran → α.Aleph.card_le μ :=
begin
rw [Aleph, ord_rec_spec αord], exact classical.some_spec (exists_least_inf_card _),
end
lemma Aleph_is_card {α : Set} (αord : α.is_ordinal) : α.Aleph.is_cardinal :=
begin
obtain ⟨αcard, -, -, -⟩ := Aleph_spec αord,
exact αcard,
end
lemma Aleph_inf {α : Set} (αord : α.is_ordinal) : ¬ α.Aleph.finite_cardinal :=
begin
obtain ⟨-, αfin, -, -⟩ := Aleph_spec αord,
exact αfin,
end
lemma Aleph_not_eq_of_mem {α : Set} (αord : α.is_ordinal) {β : Set} (βα : β ∈ α) : α.Aleph ≠ β.Aleph :=
begin
obtain ⟨-, -, αne, -⟩ := Aleph_spec αord,
intro αβ, apply αne, rw [mem_ran_iff (con_fun_fun αord), con_fun_dom αord],
refine ⟨_, βα, _⟩, rw con_fun_spec αord βα, exact αβ,
end
lemma Aleph_least {α : Set} (αord : α.is_ordinal)
{κ : Set} (κcard : κ.is_cardinal) (κfin : ¬ κ.finite_cardinal) (κne : ∀ {β : Set}, β ∈ α → κ ≠ β.Aleph) :
α.Aleph.card_le κ :=
begin
obtain ⟨-, -, -, least⟩ := Aleph_spec αord,
apply least κcard κfin, intro κα,
rw [mem_ran_iff (con_fun_fun αord), con_fun_dom αord] at κα,
rcases κα with ⟨β, βα, κβ⟩, subst κβ, apply κne βα,
rw [con_fun_spec αord βα, Aleph],
end
-- Theorem 8A(a)
theorem Aleph_lt_of_mem {β : Set} (βord : β.is_ordinal) {α : Set} (αβ : α ∈ β) : α.Aleph.card_lt β.Aleph :=
begin
have αord := ord_of_mem_ord βord αβ,
split,
apply Aleph_least αord (Aleph_is_card βord) (Aleph_inf βord),
intros γ γα, exact Aleph_not_eq_of_mem βord (ord_mem_trans βord γα αβ),
symmetry, exact Aleph_not_eq_of_mem βord αβ,
end
lemma ord_le_Aleph_self {α : Set.{u}} (αord : α.is_ordinal) : α ≤ α.Aleph :=
begin
rw ←ord_not_lt_iff_le (card_is_ord (Aleph_is_card αord)) αord,
let f : Set := rec_fun' (λ κ, κ.Aleph) α,
have ford : ∀ {n : Set.{u}}, n ∈ nat.{u} → (f.fun_value n).is_ordinal, apply induction,
rw rec_fun_base', exact αord,
intros n nω ind, rw rec_fun_ind' nω, exact card_is_ord (Aleph_is_card ind),
intro αα, refine no_ω_cycle ⟨f, rec_fun_fun', rec_fun_dom', _⟩,
apply induction,
rw [rec_fun_ind' zero_nat, rec_fun_base'], exact αα,
intros n nω ind,
have nω' := nat_induct.succ_closed nω,
rw [rec_fun_ind' nω, rec_fun_ind' nω', ←card_lt_iff_mem (Aleph_is_card (ford nω')) (Aleph_is_card (ford nω))],
exact Aleph_lt_of_mem (ford nω) ind,
end
-- Theorem 8A(b)
theorem inf_card_eq_Aleph {κ : Set.{u}} (κcard : κ.is_cardinal) (κinf : ¬ κ.finite_cardinal) : ∃ α : Set, α.is_ordinal ∧ κ = α.Aleph :=
begin
have κord : κ.is_ordinal := card_is_ord κcard,
revert κ κord, refine trans_ind_schema _, intros κ κord κind κcard κinf,
let X : Set := {β ∈ κ | β.is_ordinal ∧ β.Aleph.card_lt κ},
have memX : ∀ {β : Set}, β ∈ X ↔ β.is_ordinal ∧ β.Aleph.card_lt κ,
simp only [mem_sep, and_iff_right_iff_imp],
rintros β ⟨βord, βκ⟩, apply ord_lt_of_le_of_lt κord (ord_le_Aleph_self βord),
rw ←card_lt_iff_mem (Aleph_is_card βord) κcard, exact βκ,
have Xord : X.is_ordinal, apply trans_ords_is_ord,
intros α αX, rw memX at αX, exact αX.left,
rw transitive_set_iff, intros α αX β βα, rw memX at αX ⊢,
have βord := ord_of_mem_ord αX.left βα,
refine ⟨βord, _⟩,
refine card_lt_trans (Aleph_is_card βord) (Aleph_is_card αX.left) _ αX.right,
exact Aleph_lt_of_mem αX.left βα,
have XAord : X.Aleph.is_ordinal := card_is_ord (Aleph_is_card Xord),
refine ⟨_, Xord, _⟩, rw ord_eq_iff_le_and_le κord XAord, split,
rw ←ord_not_lt_iff_le XAord κord, intro Xκ,
rw ←card_lt_iff_mem (Aleph_is_card Xord) κcard at Xκ,
apply ord_mem_irrefl Xord, rw memX, exact ⟨Xord, Xκ⟩,
rw ←card_le_iff_le (Aleph_is_card Xord) κcard,
apply Aleph_least Xord κcard κinf,
intros β βX, rw memX at βX, symmetry, exact βX.right.right,
end
lemma Aleph_oto' {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal)
(αβ : α.Aleph.card_lt β.Aleph) : α ∈ β :=
begin
rw ←ord_not_le_iff_lt βord αord, rintro (βα|βα),
exact not_card_lt_cycle (Aleph_is_card αord) (Aleph_is_card βord) ⟨αβ, Aleph_lt_of_mem αord βα⟩,
subst βα, exact αβ.right rfl,
end
lemma Aleph_imm {α : Set} (αord : α.is_ordinal) : ¬ ∃ κ : Set, κ.is_cardinal ∧ α.Aleph.card_lt κ ∧ κ.card_lt α.succ.Aleph :=
begin
rintro ⟨κ, κcard, ακ, κα⟩,
have κinf : ¬ κ.finite_cardinal, apply card_inf_of_ge_inf (Aleph_is_card αord) (Aleph_inf αord) κcard,
rw card_le_iff, left, exact ακ,
obtain ⟨β, βord, κβ⟩ := inf_card_eq_Aleph κcard κinf, subst κβ,
apply succ_imm α ⟨_, Aleph_oto' αord βord ακ, Aleph_oto' βord (succ_ord_of_ord αord) κα⟩,
end
lemma Aleph_le_of_lt_succ {α : Set} (αord : α.is_ordinal) {κ : Set} (κcard : κ.is_cardinal)
(κα : κ.card_lt α.succ.Aleph) : κ.card_le α.Aleph :=
begin
rw ←card_not_lt_iff_le (Aleph_is_card αord) κcard, intro ακ,
exact Aleph_imm αord ⟨_, κcard, ακ, κα⟩,
end
lemma Aleph_zero_eq : Aleph ∅ = card ω :=
begin
have zord := nat_is_ord zero_nat,
have zcard := (Aleph_is_card zord),
apply card_eq_of_le_of_le zcard ⟨_, rfl⟩,
apply Aleph_least zord ⟨_, rfl⟩ aleph_null_infinite_cardinal,
simp only [mem_empty, forall_false_left, forall_const],
apply aleph_null_least_infinite_cardinal zcard,
exact Aleph_inf zord,
end
-- Lemma 8B
lemma Union_card_is_card {X : Set} (Xcard : ∀ {κ : Set}, κ ∈ X → κ.is_cardinal) : X.Union.is_cardinal :=
begin
rw ←init_iff_card, refine ⟨Union_ords_is_ord _, _⟩,
intros κ κX, exact card_is_ord (Xcard κX),
simp only [mem_Union, exists_prop], rintro ⟨α, ⟨κ, κX, ακ⟩, Xα⟩,
have κinit := Xcard κX, rw ←init_iff_card at κinit, apply κinit.right,
refine ⟨_, ακ, _⟩,
have αord := ord_of_mem_ord κinit.left ακ,
replace ακ : α ⊆ κ, rw ←ord_le_iff_sub αord κinit.left, left, exact ακ,
apply equin_of_dom_of_dom,
apply dominated_of_equin_of_dominated equin_refl Xα,
apply dominated_sub (subset_Union_of_mem κX),
exact dominated_sub ακ,
end
lemma Aleph_oto {α : Set} (αord : α.is_ordinal)
{β : Set} (βα : β ∈ α) {γ : Set} (γα : γ ∈ α) (fβγ : β.Aleph = γ.Aleph) : β = γ :=
begin
have βord := (ord_of_mem_ord αord βα),
have γord := (ord_of_mem_ord αord γα),
apply ord_eq_of_not_lt βord γord,
intro βγ, exact (Aleph_lt_of_mem γord βγ).right fβγ,
intro γβ, exact (Aleph_lt_of_mem βord γβ).right fβγ.symm,
end
lemma Aleph_limit_ord_eq {γ : Set} (γord : γ.limit_ord) : γ.Aleph = (repl_img Aleph γ).Union :=
begin
have γcard := Aleph_is_card γord.ord,
have Ucard : (repl_img Aleph γ).Union.is_cardinal, apply Union_card_is_card,
intro κ, rw mem_repl_img, rintro ⟨β, βγ, κβ⟩, subst κβ,
exact Aleph_is_card (ord_of_mem_ord γord.ord βγ),
apply card_eq_of_le_of_le γcard Ucard,
apply Aleph_least γord.ord Ucard,
rw [←card_of_cardinal_eq_self Ucard, card_finite_iff_finite],
refine inf_of_sup_inf (repl_img_inf_of_inf (limit_ord_inf γord) (@Aleph_oto _ γord.ord)) _,
intro κ, dsimp, simp only [mem_repl_img, mem_Union, exists_prop],
rintro ⟨α, αγ, κα⟩, subst κα,
have αord := ord_of_mem_ord γord.ord αγ,
refine ⟨_, ⟨_, succ_mem_limit γord αγ, rfl⟩, _⟩,
rw ←card_lt_iff_mem (Aleph_is_card αord) (Aleph_is_card (succ_ord_of_ord αord)),
exact Aleph_lt_of_mem (succ_ord_of_ord (ord_of_mem_ord γord.ord αγ)) self_mem_succ,
intros α αγ he,
have αord := ord_of_mem_ord γord.ord αγ,
apply ord_mem_irrefl (card_is_ord (Aleph_is_card αord)),
nth_rewrite 1 ←he, simp only [mem_Union, mem_repl_img, exists_prop],
refine ⟨_, ⟨_, succ_mem_limit γord αγ, rfl⟩, _⟩,
rw ←card_lt_iff_mem (Aleph_is_card αord) (Aleph_is_card (succ_ord_of_ord αord)),
exact Aleph_lt_of_mem (succ_ord_of_ord (ord_of_mem_ord γord.ord αγ)) self_mem_succ,
rw [card_le_iff_le Ucard γcard, ord_le_iff_sub (card_is_ord Ucard) (card_is_ord γcard)],
intro α, simp only [mem_Union, exists_prop, mem_repl_img],
rintro ⟨κ, ⟨β, βγ, κβ⟩, ακ⟩, subst κβ, apply ord_mem_trans (card_is_ord γcard) ακ,
have βord := ord_of_mem_ord γord.ord βγ,
rw ←card_lt_iff_mem (Aleph_is_card βord) γcard,
exact Aleph_lt_of_mem γord.ord βγ,
end
noncomputable def beth_fun : Set → Set := (fun f,
if f.dom = ∅
then card ω
else if ex₁ : ∃ α : Set, α.is_ordinal ∧ f.dom = α.succ
then card_exp two (f.fun_value (classical.some ex₁))
else if ex₂ : ∃ γ : Set, γ.limit_ord ∧ f.dom = γ
then f.ran.Union
else ∅)
lemma beth_fun_empty {f : Set} (fdom : f.dom = ∅) : beth_fun f = card ω :=
begin
rw beth_fun, dsimp, rw if_pos fdom,
end
lemma beth_fun_succ {f α : Set} (αord : α.is_ordinal) (dom : f.dom = α.succ) :
beth_fun f = two.card_exp (f.fun_value α) :=
begin
have h : ¬ f.dom = ∅, rw [eq_empty, dom], push_neg, exact ⟨_, self_mem_succ⟩,
rw beth_fun, dsimp, rw if_neg h,
have ex₁ : ∃ α : Set, α.is_ordinal ∧ f.dom = α.succ := ⟨_, αord, dom⟩,
rw dif_pos ex₁,
obtain ⟨ord, dom'⟩ := classical.some_spec ex₁, rw dom' at dom,
have h₂ := succ_inj' dom, rw h₂,
end
lemma beth_fun_limit {f γ : Set} (γord : γ.limit_ord) (dom : f.dom = γ) :
beth_fun f = f.ran.Union :=
begin
have ex₀ : ¬ f.dom = ∅, rw dom, exact γord.ne,
have ex₁ : ¬ ∃ α : Set, α.is_ordinal ∧ f.dom = α.succ,
rw dom, rintro ⟨α, -, γα⟩, exact γord.ns ⟨_, γα⟩,
have ex₂ : ∃ γ : Set, γ.limit_ord ∧ f.dom = γ := ⟨_, γord, dom⟩,
rw beth_fun, dsimp, rw [if_neg ex₀, dif_neg ex₁, if_pos ex₂],
end
noncomputable def beth : Set → Set := ord_rec beth_fun
lemma beth_zero : beth ∅ = card ω :=
begin
have dom : (con_fun beth_fun ∅).dom = ∅ := con_fun_dom zero_is_ord,
rw [beth, ord_rec_spec zero_is_ord, beth_fun_empty dom],
end
lemma beth_succ {α : Set} (αord : α.is_ordinal) : beth α.succ = card_exp two α.beth :=
begin
have αord' := succ_ord_of_ord αord,
rw [beth, ord_rec_spec αord', beth_fun_succ αord (con_fun_dom αord'), con_fun_spec αord' self_mem_succ],
end
lemma beth_limit {γ : Set} (γord : γ.limit_ord) : beth γ = (repl_img beth γ).Union :=
begin
rw [beth, ord_rec_spec γord.ord, beth_fun_limit γord (con_fun_dom γord.ord)], apply ext,
have h : ∀ {x z : Set}, x ∈ γ ∧ z = (con_fun beth_fun γ).fun_value x ↔ x ∈ γ ∧ z = ord_rec beth_fun x :=
λ x z, and_congr_right (λ xγ, eq.congr_right (con_fun_spec γord.ord xγ)),
simp only [mem_Union, exists_prop, mem_repl_img, mem_ran_iff (con_fun_fun γord.ord), con_fun_dom γord.ord, h, forall_const, iff_self],
end
lemma beth_is_card {α : Set} (αord : α.is_ordinal) : α.beth.is_cardinal :=
begin
revert α, apply trans_ind_schema, intros α αord ind,
by_cases αe : α = ∅,
subst αe, rw beth_zero, exact ⟨_, rfl⟩,
by_cases αs : ∃ β : Set, α = β.succ,
rcases αs with ⟨β, αβ⟩, subst αβ,
rw beth_succ (ord_of_succ_ord αord),
apply exp_cardinal (nat_is_cardinal two_nat) (ind self_mem_succ),
replace αord : α.limit_ord := ⟨αord, αe, αs⟩,
rw beth_limit αord, apply Union_card_is_card, intro κ,
rw mem_repl_img, rintro ⟨β, βα, κβ⟩, subst κβ, exact ind βα,
end
lemma beth_is_ord {α : Set} (αord : α.is_ordinal) : α.beth.is_ordinal := card_is_ord (beth_is_card αord)
def continuum_hypothesis : Prop := one.Aleph = one.beth
def generalized_continuum_hypothesis : Prop := ∀ α : Set, α.is_ordinal → α.Aleph = α.beth
def ord_op (f : Set → Set) : Prop := ∀ ⦃α : Set⦄, α.is_ordinal → (f α).is_ordinal
def monotone (f : Set → Set) : Prop := ∀ ⦃β : Set⦄, β.is_ordinal → ∀ {α : Set}, α ∈ β → f α ∈ f β
def continuous (f : Set → Set) : Prop := ∀ ⦃γ : Set⦄, γ.limit_ord → f γ = (repl_img f γ).Union
structure normal (f : Set → Set) : Prop :=
(mon : monotone f)
(con : continuous f)
theorem omega_limit_ord : limit_ord nat.{u} :=
begin
refine ⟨omega_is_ord, _, _⟩,
apply ne_empty_of_inhabited, refine ⟨_, zero_nat⟩,
rintro ⟨α, ωα⟩,
have αω : α ∈ nat.{u}, rw ωα, exact self_mem_succ,
have αω' := nat_induct.succ_closed αω,
rw ωα at αω', exact nat_not_mem_self (nat_induct.succ_closed αω) αω',
end
theorem Union_succ_limit {γ : Set} (γord : γ.limit_ord) : (repl_img succ γ).Union = γ :=
begin
apply ext, simp only [mem_Union, exists_prop, mem_repl_img], intro α, split,
rintro ⟨β', ⟨β, βγ, ββ'⟩, αβ⟩, subst ββ', rw [succ, mem_union, mem_singleton] at αβ, cases αβ,
subst αβ, exact βγ,
exact ord_mem_trans γord.ord αβ βγ,
intro αγ, exact ⟨_, ⟨_, αγ, rfl⟩, self_mem_succ⟩,
end
lemma succ_monotone : monotone succ := λ β βord α αβ, mem_succ_iff_le.mpr (succ_least_upper_bound βord αβ)
lemma not_succ_continuous : ¬ continuous succ :=
begin
rw continuous, push_neg, refine ⟨_, omega_limit_ord, _⟩,
rw Union_succ_limit omega_limit_ord, exact self_ne_succ,
end
lemma Aleph_ord_op : ord_op Aleph :=
λ α αord, card_is_ord (Aleph_is_card αord)
lemma Aleph_normal : normal Aleph :=
begin
refine ⟨λ β βord α αβ, _, @Aleph_limit_ord_eq⟩,
have αord := ord_of_mem_ord βord αβ,
rw ←card_lt_iff_mem (Aleph_is_card αord) (Aleph_is_card βord),
exact Aleph_lt_of_mem βord αβ,
end
def sup (S : Set) : Set := S.Union
lemma cont_iff {f : Set → Set} : continuous f ↔ ∀ {γ : Set}, γ.limit_ord → f γ = (repl_img f γ).sup := iff.rfl
lemma fun_le {f : Set → Set} {a b : Set} (h₁ : a ∈ b → f a ∈ f b) (h₂ : a ≤ b) : f a ≤ f b :=
begin
cases h₂,
exact or.inl (h₁ h₂),
subst h₂, exact or.inr rfl,
end
-- Theorem Schema 8C
theorem mon_of_con {f : Set → Set} (ford : ord_op f) (con : continuous f)
(h : ∀ {γ : Set}, γ.is_ordinal → f γ ∈ f γ.succ) : monotone f :=
begin
intros β βord α αβ,
have αord := ord_of_mem_ord βord αβ,
revert β βord, refine trans_ind_schema _, intros β βord ind αβ, by_cases βe : β = ∅,
subst βe, exfalso, exact mem_empty _ αβ,
by_cases βγ : ∃ γ : Set, β = γ.succ,
rcases βγ with ⟨γ, βγ⟩, subst βγ,
refine ord_lt_of_le_of_lt (ford βord) _ (h (ord_of_succ_ord βord)),
apply fun_le (ind self_mem_succ), rw ←mem_succ_iff_le, exact αβ,
replace βord : β.limit_ord := ⟨βord, βe, βγ⟩,
have αβ' : α.succ ∈ β := succ_mem_limit βord αβ,
simp only [con βord, mem_Union, exists_prop, mem_repl_img],
exact ⟨_, ⟨_, αβ', rfl⟩, ind αβ' self_mem_succ⟩,
end
theorem norm_of_con {f : Set → Set} (ford : ord_op f) (con : continuous f)
(h : ∀ {γ : Set}, γ.is_ordinal → f γ ∈ f γ.succ) : normal f :=
⟨mon_of_con @ford con @h, con⟩
theorem beth_norm : normal beth :=
begin
apply norm_of_con @beth_is_ord @beth_limit,
intros γ γord,
rw [←card_lt_iff_mem (beth_is_card γord) (beth_is_card (succ_ord_of_ord γord)), beth_succ γord],
exact card_lt_exp (beth_is_card γord),
end
lemma le_norm_fun {f : Set → Set} (ford : ord_op f) (norm : normal f) {α : Set} (αord : α.is_ordinal) : α ≤ f α :=
begin
revert α, apply trans_ind_schema, intros α αord ind,
by_cases αe : α = ∅,
subst αe, rw ord_le_iff_sub zero_is_ord (ford zero_is_ord), exact empty_subset,
by_cases αs : ∃ β : Set, α = β.succ,
rcases αs with ⟨β, αβ⟩, subst αβ,
apply succ_least_upper_bound (ford αord),
apply ord_lt_of_le_of_lt (ford αord) (ind self_mem_succ) (norm.mon αord self_mem_succ),
replace αord : α.limit_ord := ⟨αord, αe, αs⟩,
rw [ord_le_iff_sub αord.ord (ford αord.ord), norm.con αord], intros β βα,
simp only [mem_Union, exists_prop, mem_repl_img], refine ⟨_, ⟨_, succ_mem_limit αord βα, rfl⟩, _⟩,
have βord : β.succ.is_ordinal := succ_ord_of_ord (ord_of_mem_ord αord.ord βα),
apply ord_lt_of_le_of_lt (ford βord) (ind βα) (norm.mon βord self_mem_succ),
end
lemma is_succ_of_not_zero_of_not_limit {α : Set} (αord : α.is_ordinal) (αe : α ≠ ∅) (αnl : ¬ α.limit_ord) : ∃ β : Set, α = β.succ :=
classical.by_contradiction (λ h, αnl ⟨αord, αe, h⟩)
-- Theorem Schema 8D
theorem exists_large_ord_of_norm {f : Set → Set} (ford : ord_op f) (norm : normal f)
{β : Set} (βord : β.is_ordinal) (βl : f ∅ ≤ β) : ∃ γ : Set, γ.is_ordinal ∧ f γ ≤ β ∧ ∀ {α : Set}, α.is_ordinal → f α ≤ β → α ≤ γ :=
begin
let X : Set := {α ∈ β.succ | α.is_ordinal ∧ f α ≤ β},
have memX : ∀ {α : Set}, α ∈ X ↔ α.is_ordinal ∧ f α ≤ β, simp only [mem_sep, and_iff_right_iff_imp, and_imp],
intros α αord αβ, rw mem_succ_iff_le, exact ord_le_trans βord (le_norm_fun ford norm αord) αβ,
have Xord : X.is_ordinal, apply trans_ords_is_ord,
intros α αX, rw memX at αX, exact αX.left,
rw transitive_set_iff, intros α αX γ γα, rw memX at αX ⊢, refine ⟨ord_of_mem_ord αX.left γα, _⟩,
left, apply ord_lt_of_lt_of_le βord (norm.mon αX.left γα), exact αX.right,
suffices h : ∃ γ : Set, X = γ.succ,
rcases h with ⟨γ, Xγ⟩,
have γord : γ.is_ordinal, apply ord_of_succ_ord, rw ←Xγ, exact Xord,
have γX : γ ∈ X, rw Xγ, exact self_mem_succ,
rw memX at γX,
refine ⟨_, γord, γX.right, λ α αord αβ, _⟩, rw [←mem_succ_iff_le, ←Xγ, memX],
exact ⟨αord, αβ⟩,
apply is_succ_of_not_zero_of_not_limit Xord,
apply ne_empty_of_inhabited, use ∅, rw memX,
exact ⟨zero_is_ord, βl⟩,
intro Xl, apply ord_mem_irrefl Xord, rw memX, refine ⟨Xord, _⟩,
rw [ord_le_iff_sub (ford Xord) βord, norm.con Xl], intro γ,
simp only [mem_Union, exists_prop, mem_repl_img],
rintro ⟨fα, ⟨α, αX, fαα⟩, γfα⟩, subst fαα, rw memX at αX,
exact ord_lt_of_lt_of_le βord γfα αX.right,
end
lemma exists_larger_of_not_bounded {S : Set} (Sord : ∀ {α : Set}, α ∈ S → α.is_ordinal)
(h : ¬ ∃ β : Set, β ∈ S ∧ ∀ {α : Set}, α ∈ S → α ≤ β) {α : Set} (αS : α ∈ S) : ∃ β : Set, β ∈ S ∧ α ∈ β :=
begin
apply classical.by_contradiction, intro h₁, push_neg at h₁, apply h, refine ⟨_, αS, _⟩,
intros β βS, rw ←ord_not_lt_iff_le (Sord αS) (Sord βS), exact h₁ _ βS,
end
lemma repl_img_ords {f : Set → Set} (ford : ord_op f) {S : Set} (Sord : ∀ {α : Set}, α ∈ S → α.is_ordinal) :
∀ ⦃α : Set⦄, α ∈ (repl_img f S) → α.is_ordinal :=
begin
intros α, rw mem_repl_img, rintro ⟨β, βS, αβ⟩, subst αβ, exact ford (Sord βS),
end
-- Theorem Schema 8E
theorem sup_norm_fun {f : Set → Set} (ford : ord_op f) (norm : normal f)
{S : Set} (Sne : S ≠ ∅) (Sord : ∀ {α : Set}, α ∈ S → α.is_ordinal) : f S.sup = (repl_img f S).sup :=
begin
have SU := Union_ords_is_ord @Sord,
have h₁ := ford SU,
have h₂ : (repl_img f S).Union.is_ordinal, apply Union_ords_is_ord, intro α,
rw mem_repl_img, rintro ⟨β, βS, αβ⟩, subst αβ, exact ford (Sord βS),
simp only [sup], rw ord_eq_iff_le_and_le h₁ h₂, split,
by_cases h₃ : ∃ β : Set, β ∈ S ∧ ∀ {α : Set}, α ∈ S → α ≤ β,
obtain ⟨SUS, Ub⟩ := case_exists_bound @Sord h₃,
rw ord_le_iff_sub h₁ h₂, apply subset_Union_of_mem, rw mem_repl_img,
exact ⟨_, SUS, rfl⟩,
have USne : S.Union ≠ ∅, apply ne_empty_of_inhabited,
obtain ⟨α, αS⟩ := inhabited_of_ne_empty Sne,
obtain ⟨β, βS, αβ⟩ := exists_larger_of_not_bounded @Sord h₃ αS,
use α, simp only [mem_Union, exists_prop], exact ⟨_, βS, αβ⟩,
have Slim : S.Union.limit_ord, refine ⟨SU, USne, (case_not_exists_bound @Sord h₃).right⟩,
have h : (repl_img f S.Union).Union.is_ordinal := Union_ords_is_ord (repl_img_ords ford (ord_of_mem_ord SU)),
rw norm.con Slim, rw ord_le_iff_sub h h₂, intro β, simp only [mem_Union, exists_prop, mem_repl_img],
rintro ⟨fα, ⟨α, ⟨γ, γS, αγ⟩, fαα⟩, βfα⟩, subst fαα, refine ⟨_, ⟨_, γS, rfl⟩, _⟩,
apply ord_mem_trans (ford (Sord γS)) βfα, exact norm.mon (Sord γS) αγ,
rw ord_le_iff_sub h₂ h₁, intro β, rw mem_Union, simp only [exists_prop, mem_repl_img],
rintro ⟨fα, ⟨α, αS, fαα⟩, βfα⟩, subst fαα,
apply ord_lt_of_lt_of_le h₁ βfα, apply fun_le (norm.mon SU),
rw ord_le_iff_sub (Sord αS) SU, exact subset_Union_of_mem αS,
end
-- Veblen Fixed-Point Theorem Schema
theorem exists_fixed_point {f : Set → Set} (ford : ord_op f) (norm : normal f)
{β : Set.{u}} (βord : β.is_ordinal) : ∃ γ : Set, γ.is_ordinal ∧ f γ = γ ∧ β ≤ γ :=
begin
cases le_norm_fun ford norm βord with ββ ββ,
let F := rec_fun' f β,
let μ := F.ran.sup,
use μ,
have rne : F.ran ≠ ∅, rw [ne.def, ←dom_ran_eq_empty_iff, rec_fun_dom'],
apply ne_empty_of_inhabited, exact ⟨_, zero_nat⟩,
have rord : ∀ {n : Set}, n ∈ nat.{u} → (F.fun_value n).is_ordinal,
apply induction,
rw rec_fun_base', exact βord,
intros n nω ind, rw rec_fun_ind' nω, exact ford ind,
have rord' : ∀ {α : Set}, α ∈ F.ran → α.is_ordinal, intro α,
rw [mem_ran_iff (rec_fun_fun'), rec_fun_dom'], rintro ⟨n, nω, αn⟩, subst αn,
exact rord nω,
have μord : μ.is_ordinal := Union_ords_is_ord @rord',
have μne : μ ≠ ∅, apply ne_empty_of_inhabited, use β,
change β ∈ F.ran.Union, simp only [mem_Union, exists_prop, mem_ran_iff rec_fun_fun', rec_fun_dom'],
refine ⟨_, ⟨succ ∅, one_nat, rfl⟩, _⟩, rw [rec_fun_ind' zero_nat, rec_fun_base'],
exact ββ,
have Fmem : ∀ {n : Set}, n ∈ nat.{u} → F.fun_value n ∈ F.fun_value n.succ,
apply induction,
rw [rec_fun_ind' zero_nat, rec_fun_base'], exact ββ,
intros n nω ind,
have nω' : n.succ ∈ nat.{u} := nat_induct.succ_closed nω,
rw [rec_fun_ind' nω, rec_fun_ind' nω'],
exact norm.mon (rord nω') ind,
have μlim : μ.limit_ord, apply limit_ord_of_not_bound μord μne, push_neg,
intros α αμ, change α ∈ F.ran.Union at αμ,
simp only [mem_Union, exists_prop, mem_ran_iff rec_fun_fun', rec_fun_dom'] at αμ,
rcases αμ with ⟨Fn, ⟨n, nω, Fnn⟩, αFn⟩, subst Fnn, use F.fun_value n,
rw ord_not_le_iff_lt (rord nω) (ord_of_mem_ord (rord nω) αFn),
change F.fun_value n ∈ F.ran.Union ∧ α ∈ F.fun_value n,
simp only [mem_Union, exists_prop, mem_ran_iff rec_fun_fun', rec_fun_dom'],
exact ⟨⟨_, ⟨_, nat_induct.succ_closed nω, rfl⟩, Fmem nω⟩, αFn⟩,
refine ⟨μlim.ord, _, _⟩,
change f μ = F.ran.sup,
rw [sup_norm_fun ford norm rne @rord', sup, sup], rw eq_iff_subset_and_subset, split,
apply Union_sub_of_sub, apply repl_img_sub_of_closed, intro α,
simp only [mem_ran_iff rec_fun_fun', rec_fun_dom'], rintro ⟨n, nω, αFn⟩, subst αFn,
exact ⟨_, nat_induct.succ_closed nω, (rec_fun_ind' nω).symm⟩,
intro α, simp only [mem_Union, exists_prop, mem_repl_img, mem_ran_iff rec_fun_fun', rec_fun_dom'],
rintro ⟨Fn, ⟨n, nω, Fnn⟩, αFn⟩, subst Fnn,
refine ⟨_, ⟨_, ⟨_, nω, rfl⟩, rfl⟩, _⟩, rw ←@rec_fun_ind' f β _ nω,
exact ord_mem_trans (rord (nat_induct.succ_closed nω)) αFn (Fmem nω),
rw ord_le_iff_sub βord μord, apply subset_Union_of_mem,
rw [mem_ran_iff rec_fun_fun', rec_fun_dom'], refine ⟨_, zero_nat, _⟩,
rw rec_fun_base',
exact ⟨_, βord, ββ.symm, or.inr rfl⟩,
end
-- the exercises for ordinal operations section are pretty good
-- exercise 3
theorem mon_op_mon_iff {f : Set → Set} (ford : ord_op f) (mon : monotone f)
{α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) : α ∈ β ↔ f α ∈ f β :=
⟨λ αβ, mon βord αβ, λ fαβ, begin
apply classical.by_contradiction, intro βα, rw ord_not_lt_iff_le αord βord at βα,
cases βα,
exact ord_mem_irrefl (ford βord) (ord_mem_trans (ford βord) (mon αord βα) fαβ),
subst βα, exact ord_mem_irrefl (ford βord) fαβ,
end⟩
theorem mon_op_inj {f : Set → Set} (ford : ord_op f) (mon : monotone f)
{α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) (h : f α = f β) : α = β :=
begin
apply classical.by_contradiction, intro eq,
cases ord_conn αord βord eq with αβ βα,
have h₂ := mon βord αβ, rw h at h₂, apply ord_mem_irrefl (ford βord) h₂,
have h₂ := mon αord βα, rw h at h₂, apply ord_mem_irrefl (ford βord) h₂,
end
theorem mon_op_le_iff {f : Set → Set} (ford : ord_op f) (mon : monotone f)
{α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) : α ≤ β ↔ f α ≤ f β :=
⟨λ h, or.elim h (λ h₁, by left; rwa ←mon_op_mon_iff ford mon αord βord) (λ h₁, by right; rw h₁),
λ h, or.elim h (λ h₁, by left; rwa mon_op_mon_iff ford mon αord βord) (λ h₁, or.inr (mon_op_inj ford mon αord βord h₁))⟩
def struct_to_set (R : struct) : Set := R.fld.pair R.rel
lemma struct_to_set_is_pair {R : struct} : (struct_to_set R).is_pair := ⟨_, _, rfl⟩
def empty_struct : struct := ⟨∅, ∅, λ p hp, begin rw prod_empty_eq_empty, exact hp, end⟩
noncomputable def set_to_struct (R : Set) : struct := if is_rel : R.snd ⊆ R.fst.prod R.fst then ⟨R.fst, R.snd, is_rel⟩ else empty_struct
lemma set_fst_eq_fld {R : struct} : (struct_to_set R).fst = R.fld := by rw [struct_to_set, fst_congr]
lemma set_snd_eq_rel {R : struct} : (struct_to_set R).snd = R.rel := by rw [struct_to_set, snd_congr]
lemma set_rel {R : struct} : (struct_to_set R).snd ⊆ (struct_to_set R).fst.prod (struct_to_set R).fst :=
begin
rw [struct_to_set, fst_congr, snd_congr], exact R.is_rel,
end
lemma fld_eq_set_fst {R : Set} (is_rel : R.snd ⊆ R.fst.prod R.fst) : (set_to_struct R).fld = R.fst := by rw [set_to_struct, dif_pos is_rel]
lemma rel_eq_set_snd {R : Set} (is_rel : R.snd ⊆ R.fst.prod R.fst) : (set_to_struct R).rel = R.snd := by rw [set_to_struct, dif_pos is_rel]
lemma struct_set_struct_eq_self (R : struct) : set_to_struct (struct_to_set R) = R :=
begin
ext,
rw [fld_eq_set_fst set_rel], exact set_fst_eq_fld,
rw [rel_eq_set_snd set_rel], exact set_snd_eq_rel,
end
lemma struct_to_set_oto {R S : struct} (RS : struct_to_set R = struct_to_set S) : R = S :=
begin
ext,
rw [←set_fst_eq_fld, ←set_fst_eq_fld, RS],
rw [←set_snd_eq_rel, ←set_snd_eq_rel, RS],
end
lemma set_struct_set_eq_self {R : Set} (Rpair : R.is_pair) (is_rel : R.snd ⊆ R.fst.prod R.fst) :
struct_to_set (set_to_struct R) = R :=
begin
apply pair_eq struct_to_set_is_pair Rpair,
rw [set_fst_eq_fld, fld_eq_set_fst is_rel],
rw [set_snd_eq_rel, rel_eq_set_snd is_rel],
end
noncomputable def str_rank (R : struct) : Set := rank (struct_to_set R)
lemma str_rank_ord {R : struct} : (str_rank R).is_ordinal := rank_ord_of_grounded all_grounded
def pioneering (R : struct) : Prop := ¬ ∃ S : struct, isomorphic R S ∧ str_rank S ∈ str_rank R
lemma exists_rank_iso_struct (R : struct) :
∃ α : Set, α.is_ordinal ∧ ∃ S : struct, isomorphic R S ∧ α = str_rank S :=
⟨_, rank_ord_of_grounded all_grounded, _, iso_refl, rfl⟩
noncomputable def pioneering_ord (R : struct) : Set := classical.some (exists_least_ord_of_exists (exists_rank_iso_struct R))
lemma pioneering_ord_spec (R : struct) : (pioneering_ord R).is_ordinal ∧ (∃ S : struct, isomorphic R S ∧ pioneering_ord R = str_rank S)
∧ ∀ {β : Set}, β.is_ordinal → (∃ S : struct, isomorphic R S ∧ β = str_rank S) → pioneering_ord R ≤ β :=
classical.some_spec (exists_least_ord_of_exists (exists_rank_iso_struct R))
lemma pioneering_ord_ord {R : struct} : (pioneering_ord R).is_ordinal :=
begin
obtain ⟨ord, -, -⟩ := @pioneering_ord_spec R, exact ord,
end
lemma exists_iso_of_pioneering_rank {R : struct} : ∃ S : struct, isomorphic R S ∧ pioneering_ord R = str_rank S :=
begin
obtain ⟨-, h, -⟩ := @pioneering_ord_spec R, exact h,
end
lemma pioneering_ord_least {R S : struct} (RS : isomorphic R S) : pioneering_ord R ≤ str_rank S :=
begin
obtain ⟨-, -, least⟩ := @pioneering_ord_spec R,
exact least (rank_ord_of_grounded all_grounded) ⟨_, RS, rfl⟩,
end
lemma rank_iso_pioneering_iff {R S : struct} (RS : isomorphic R S) : pioneering S ↔ pioneering_ord R = str_rank S :=
begin
split,
{ intro Sp,
rw [str_rank, ord_eq_iff_le_and_le pioneering_ord_ord (rank_ord_of_grounded all_grounded)], split,
exact pioneering_ord_least RS,
rw ←ord_not_lt_iff_le pioneering_ord_ord (rank_ord_of_grounded all_grounded), intro h, apply Sp,
obtain ⟨T, RT, RT'⟩ := @exists_iso_of_pioneering_rank R,
rw RT' at h, exact ⟨_, iso_trans (iso_symm RS) RT, h⟩, },
{ rintros RS' ⟨T, ST, TS⟩, revert TS, change str_rank T ∉ str_rank S,
rw [ord_not_lt_iff_le str_rank_ord str_rank_ord, ←RS'],
exact pioneering_ord_least (iso_trans RS ST), },
end
lemma rank_eq_of_iso_pioneering {R S : struct} (RS : isomorphic R S) (Rp : pioneering R) (Sp : pioneering S) :
str_rank R = str_rank S :=
begin
rw [←(rank_iso_pioneering_iff (iso_symm RS)).mp Rp, ←rank_iso_pioneering_iff iso_refl], exact Sp,
end
lemma rank_eq_of_le_iso_pioneering {R S : struct} (RS : isomorphic S R)
(Sp : pioneering S) (RleS : str_rank R ≤ str_rank S) : str_rank S = str_rank R :=
begin
symmetry,
rw ord_eq_iff_le_and_le str_rank_ord str_rank_ord, refine ⟨RleS, _⟩,
rw rank_iso_pioneering_iff (iso_symm RS) at Sp, rw ←Sp,
exact pioneering_ord_least iso_refl,
end
noncomputable def it (R : struct.{u}) : Set.{u} :=
{S ∈ (pioneering_ord R).succ.Veb | ∃ T : struct, S = struct_to_set T ∧ pioneering T ∧ isomorphic R T}
lemma mem_it {R : struct} {S : Set} : S ∈ it R ↔ ∃ T : struct, S = struct_to_set T ∧ pioneering T ∧ isomorphic R T :=
begin
simp only [it, mem_sep, and_imp, and_iff_right_iff_imp, exists_imp_distrib],
intros T ST Tp RT, subst ST,
rw [(rank_iso_pioneering_iff RT).mp Tp, str_rank, Veb_succ_eq_powerset (rank_ord_of_grounded all_grounded), mem_powerset],
exact rank_sub_of_grounded all_grounded,
end
lemma struct_mem_it {R S : struct} : struct_to_set S ∈ it R ↔ pioneering S ∧ isomorphic R S :=
by simp only [mem_it, struct_set_struct_eq_self, function.injective.eq_iff @struct_to_set_oto, exists_eq_left']
lemma it_inhab (R : struct) : (it R).inhab :=
begin
obtain ⟨S, RS, RS'⟩ := @exists_iso_of_pioneering_rank R,
use struct_to_set S, rw ←rank_iso_pioneering_iff RS at RS', rw mem_it,
exact ⟨_, rfl, RS', RS⟩,
end
-- Theorem 8F
theorem iso_type_eq_iff_iso {R S : struct} : it R = it S ↔ isomorphic R S :=
begin
split,
{ intro RS,
obtain ⟨C, CR⟩ := it_inhab R,
have CS : C ∈ it S, rw ←RS, exact CR,
rw mem_it at CR, rcases CR with ⟨T, CT, -, RT⟩,
rw mem_it at CS, rcases CS with ⟨U, CU, -, SU⟩,
have TU : T = U, rw [←struct_set_struct_eq_self T, ←struct_set_struct_eq_self U, ←CT, ←CU],
subst TU, exact iso_trans RT (iso_symm SU), },
{ revert R S,
have sub : ∀ {R S : struct}, isomorphic R S → it R ⊆ it S,
intros R S RS C, simp only [mem_it],
rintro ⟨T, CT, Tp, RT⟩, exact ⟨_, CT, Tp, iso_trans (iso_symm RS) RT⟩,
intros R S, rw eq_iff_subset_and_subset,
exact λ RS, ⟨sub RS, sub (iso_symm RS)⟩, },
end
def order_type (ρ : Set) : Prop := ∃ R : struct, R.fld.lin_order R.rel ∧ ρ = it R
lemma order_type_inhab {ρ : Set} (ρot : ρ.order_type) : ρ.inhab :=
begin
rcases ρot with ⟨R, Rlin, ρR⟩, subst ρR, exact it_inhab R,
end
lemma mem_order_type {ρ : Set} (ρot : ρ.order_type) {R : Set} (Rρ : R ∈ ρ)
{S : Set} : S ∈ ρ ↔ ∃ T : struct, S = struct_to_set T ∧ str_rank (set_to_struct R) = str_rank T ∧ isomorphic (set_to_struct R) T ∧ T.fld.lin_order T.rel :=
begin
rcases ρot with ⟨U, Ulin, ρU⟩, subst ρU, rw mem_it at Rρ ⊢,
rcases Rρ with ⟨V, RV, Vp, UV⟩, subst RV, split,
{ rintro ⟨T, ST, Tp, UT⟩, subst ST, refine ⟨_, rfl, _, _, lin_order_iso UT Ulin⟩,
rw struct_set_struct_eq_self V,
have VT := iso_trans (iso_symm UV) UT,
rw rank_iso_pioneering_iff VT at Tp, rw ←Tp,
symmetry, rw ←rank_iso_pioneering_iff iso_refl, exact Vp,
rw struct_set_struct_eq_self V,
exact iso_trans (iso_symm UV) UT, },
{ rintro ⟨T, ST, VT, VT', Tlin⟩, subst ST,
rw struct_set_struct_eq_self V at VT',
refine ⟨_, rfl, _, iso_trans UV VT'⟩,
rw struct_set_struct_eq_self V at VT,
rw [rank_iso_pioneering_iff VT', ←VT, ←rank_iso_pioneering_iff iso_refl], exact Vp, },
end
lemma struct_mem_order_type {ρ : Set} (ρot : ρ.order_type) {R : struct} (Rρ : struct_to_set R ∈ ρ)
{S : struct} : struct_to_set S ∈ ρ ↔ str_rank R = str_rank S ∧ isomorphic R S ∧ S.fld.lin_order S.rel :=
by simp only [mem_order_type ρot Rρ, struct_set_struct_eq_self, function.injective.eq_iff @struct_to_set_oto, exists_eq_left']
lemma of_mem_order_type {ρ : Set} (ρot : ρ.order_type) {R : Set} (Rρ : R ∈ ρ) :
∃ T : struct, R = struct_to_set T ∧ pioneering T ∧ T.fld.lin_order T.rel :=
begin
rcases ρot with ⟨S, Slin, ρS⟩, subst ρS,
rw mem_it at Rρ, rcases Rρ with ⟨T, RT, Tp, ST⟩, subst RT,
exact ⟨_, rfl, Tp, lin_order_iso ST Slin⟩,
end
lemma order_type_ext {ρ : Set} (ρot : ρ.order_type) {R : struct} (Rρ : struct_to_set R ∈ ρ)
{σ : Set} (σot : σ.order_type) {S : struct} (Sσ : struct_to_set S ∈ σ)
(h : ∀ {T : struct}, struct_to_set T ∈ ρ ↔ struct_to_set T ∈ σ) : ρ = σ :=
begin
apply ext, intro T, rw [mem_order_type ρot Rρ, mem_order_type σot Sσ], split,
rintro ⟨U, TU, rank, iso, lin⟩, subst TU, rw struct_set_struct_eq_self at rank iso ⊢,
simp only [struct_set_struct_eq_self, function.injective.eq_iff @struct_to_set_oto, exists_eq_left'],
rw [←struct_mem_order_type σot Sσ, ←h, struct_mem_order_type ρot Rρ],
exact ⟨rank, iso, lin⟩,
rintro ⟨U, TU, rank, iso, lin⟩, subst TU, rw struct_set_struct_eq_self at rank iso ⊢,
simp only [struct_set_struct_eq_self, function.injective.eq_iff @struct_to_set_oto, exists_eq_left'],
rw [←struct_mem_order_type ρot Rρ, h, struct_mem_order_type σot Sσ],
exact ⟨rank, iso, lin⟩,
end
lemma fst_fun_onto {A B : Set} (Bne : B ≠ ∅) : (pair_sep_eq (A.prod B) A fst).onto_fun (A.prod B) A :=
begin
refine ⟨pair_sep_eq_is_fun, pair_sep_eq_dom_eq _, pair_sep_eq_ran_eq _⟩,
intro p, rw mem_prod, rintro ⟨a, aA, b, bB, pab⟩, subst pab, rw fst_congr, exact aA,
intros a aA,
obtain ⟨b, bB⟩ := inhabited_of_ne_empty Bne,
use a.pair b, rw [pair_mem_prod, fst_congr], exact ⟨⟨aA, bB⟩, rfl⟩,
end
lemma fst_fun_oto {A x : Set} : (pair_sep_eq (A.prod {x}) A fst).one_to_one :=
begin
apply pair_sep_eq_oto, intros p hp q hq, rw mem_prod at hp hq,
rcases hp with ⟨a, aA, b, bB, pab⟩, rcases hq with ⟨a', aA', b', bB', qab⟩, subst pab, subst qab,
simp only [fst_congr], intro aa, subst aa, apply pair_eq ⟨_, _, rfl⟩ ⟨_, _, rfl⟩,
simp only [fst_congr],
simp only [snd_congr], rw mem_singleton at bB bB', rw [bB', bB],
end
lemma fst_fun_value {A x a : Set} (aA : a ∈ A) : (pair_sep_eq (A.prod {x}) A fst).fun_value (a.pair x) = a :=
begin
have h : a.pair x ∈ (pair_sep_eq (A.prod {x}) A fst).dom,
rw [(fst_fun_onto singleton_ne_empty).right.left, pair_mem_prod, mem_singleton],
exact ⟨aA, rfl⟩,
rw [pair_sep_eq_fun_value h, fst_congr],
end
lemma fst_fun_value' {A x p : Set} (hp : p ∈ A.prod {x}) : (pair_sep_eq (A.prod {x}) A fst).fun_value p = p.fst :=
begin
rw ←(fst_fun_onto singleton_ne_empty).right.left at hp,
exact pair_sep_eq_fun_value hp,
end
lemma fst_fun_corr {A x : Set} : correspondence (A.prod {x}) A (pair_sep_eq (A.prod {x}) A fst) :=
⟨fst_fun_onto singleton_ne_empty, fst_fun_oto⟩
lemma fst_fun_iso {R : struct} {x : Set} : isomorphic ⟨R.fld.prod {x}, fun_order (R.fld.prod {x}) R.rel (pair_sep_eq (R.fld.prod {x}) R.fld fst), pair_sep_sub_prod⟩ R :=
begin
refine ⟨_, fst_fun_corr, _⟩, dsimp, intros y z hy hz,
simp only [fun_order, pair_mem_pair_sep' hy hz],
end
lemma ex_disj_of_ord_type' {A : struct} (Alin : A.fld.lin_order A.rel) {B : struct} (Blin : B.fld.lin_order B.rel) :
∃ P : struct × struct, isomorphic P.fst A ∧ isomorphic P.snd B ∧ P.fst.fld ∩ P.snd.fld = ∅ :=
⟨⟨_, _⟩, fst_fun_iso, fst_fun_iso, disj zero_ne_one⟩
lemma ex_3_disj {A : struct} (Alin : A.fld.lin_order A.rel)
{B : struct} (Blin : B.fld.lin_order B.rel)
{C : struct} (Clin : C.fld.lin_order C.rel) :
∃ R : struct, isomorphic R A ∧ ∃ S : struct, isomorphic S B ∧ R.fld ∩ S.fld = ∅
∧ ∃ T : struct, isomorphic T C ∧ R.fld ∩ T.fld = ∅ ∧ S.fld ∩ T.fld = ∅ :=
⟨_, fst_fun_iso, _, fst_fun_iso, disj zero_ne_one, _, fst_fun_iso, disj zero_ne_two, disj one_ne_two⟩
lemma exists_lin_mem_order_type {ρ : Set} (ρot : ρ.order_type) : ∃ R : struct, R.fld.lin_order R.rel ∧ struct_to_set R ∈ ρ :=
begin
obtain ⟨S, Sρ⟩ := order_type_inhab ρot,
obtain ⟨R, SR, -, Rlin⟩ := of_mem_order_type ρot Sρ, subst SR,
exact ⟨_, Rlin, Sρ⟩,
end
noncomputable def ot_repr (ρ : Set) : struct :=
if ρot : ρ.order_type then classical.some (exists_lin_mem_order_type ρot) else ⟨∅, ∅, empty_subset⟩
lemma ot_repr_lin {ρ : Set} (ρot : ρ.order_type) : ρ.ot_repr.fld.lin_order ρ.ot_repr.rel :=
begin
obtain ⟨lin, -⟩ := classical.some_spec (exists_lin_mem_order_type ρot),
rw [ot_repr, dif_pos ρot], exact lin,
end
lemma ot_repr_mem {ρ : Set} (ρot : ρ.order_type) : struct_to_set ρ.ot_repr ∈ ρ :=
begin
obtain ⟨-, mem⟩ := classical.some_spec (exists_lin_mem_order_type ρot),
rw [ot_repr, dif_pos ρot], exact mem,
end
lemma ot_repr_pioneering {ρ : Set} (ρot : ρ.order_type) : pioneering ρ.ot_repr :=
begin
have hρ := ot_repr_mem ρot,
rcases ρot with ⟨R, Rlin, ρR⟩, subst ρR,
rw struct_mem_it at hρ, exact hρ.left,
end
lemma iso_repr_it {R : struct} (Rlin : R.fld.lin_order R.rel) : isomorphic R (it R).ot_repr :=
begin
have mem := ot_repr_mem ⟨_, Rlin, rfl⟩,
rw struct_mem_it at mem, exact mem.right,
end
lemma order_type_ext' {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type)
(h : ∀ {T : struct}, struct_to_set T ∈ ρ ↔ struct_to_set T ∈ σ) : ρ = σ :=
order_type_ext ρot (ot_repr_mem ρot) σot (ot_repr_mem σot) @h
lemma order_type_eq_iff_repr_iso {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type) :
ρ = σ ↔ isomorphic ρ.ot_repr σ.ot_repr :=
begin
split,
intro ρot, subst ρot, exact iso_refl,
intro ρσ, apply order_type_ext ρot (ot_repr_mem ρot) σot (ot_repr_mem σot), intro T,
rw [struct_mem_order_type ρot (ot_repr_mem ρot), struct_mem_order_type σot (ot_repr_mem σot)], split,
rintro ⟨rank, iso, lin⟩, rw ←rank,
exact ⟨rank_eq_of_iso_pioneering (iso_symm ρσ) (ot_repr_pioneering σot) (ot_repr_pioneering ρot),
iso_trans (iso_symm ρσ) iso, lin_order_iso iso (ot_repr_lin ρot)⟩,
rintro ⟨rank, iso, lin⟩, rw ←rank,
exact ⟨rank_eq_of_iso_pioneering ρσ (ot_repr_pioneering ρot) (ot_repr_pioneering σot),
iso_trans ρσ iso, lin_order_iso iso (ot_repr_lin σot)⟩,
end
lemma ot_eq_iff_exists_iso {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type) :
ρ = σ ↔ ∃ R : struct, isomorphic R ρ.ot_repr ∧ ∃ S : struct, isomorphic S σ.ot_repr ∧ isomorphic R S :=
begin
rw order_type_eq_iff_repr_iso ρot σot, split,
intro iso, exact ⟨_, iso_refl, _, iso_refl, iso⟩,
rintro ⟨R, Riso, S, Siso, iso⟩, exact iso_trans (iso_symm Riso) (iso_trans iso Siso),
end
noncomputable def ot_disj_repr (ρ σ : Set.{u}) : struct.{u} × struct.{u} :=
if ρot : ρ.order_type then
(if σot : σ.order_type then
classical.some (ex_disj_of_ord_type' (ot_repr_lin ρot) (ot_repr_lin σot))
else ⟨⟨∅, ∅, empty_subset⟩, ⟨∅, ∅, empty_subset⟩⟩)
else ⟨⟨∅, ∅, empty_subset⟩, ⟨∅, ∅, empty_subset⟩⟩
lemma ot_disj_repr_left_iso {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type) :
isomorphic (ρ.ot_disj_repr σ).fst ρ.ot_repr :=
begin
obtain ⟨liso, riso, disj⟩ := classical.some_spec (ex_disj_of_ord_type' (ot_repr_lin ρot) (ot_repr_lin σot)),
rw [ot_disj_repr, dif_pos ρot, dif_pos σot], exact liso,
end
lemma ot_disj_repr_right_iso {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type) :
isomorphic (ρ.ot_disj_repr σ).snd σ.ot_repr :=
begin
obtain ⟨liso, riso, disj⟩ := classical.some_spec (ex_disj_of_ord_type' (ot_repr_lin ρot) (ot_repr_lin σot)),
rw [ot_disj_repr, dif_pos ρot, dif_pos σot], exact riso,
end
lemma ot_disj_repr_disj {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type) :
(ρ.ot_disj_repr σ).fst.fld ∩ (ρ.ot_disj_repr σ).snd.fld = ∅ :=
begin
obtain ⟨liso, riso, disj⟩ := classical.some_spec (ex_disj_of_ord_type' (ot_repr_lin ρot) (ot_repr_lin σot)),
rw [ot_disj_repr, dif_pos ρot, dif_pos σot], exact disj,
end
lemma ot_disj_repr_left_lin {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type) :
(ρ.ot_disj_repr σ).fst.fld.lin_order (ρ.ot_disj_repr σ).fst.rel :=
lin_order_iso (iso_symm (ot_disj_repr_left_iso ρot σot)) (ot_repr_lin ρot)
lemma ot_disj_repr_right_lin {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type) :
(ρ.ot_disj_repr σ).snd.fld.lin_order (ρ.ot_disj_repr σ).snd.rel :=
lin_order_iso (iso_symm (ot_disj_repr_right_iso ρot σot)) (ot_repr_lin σot)
def add_rel (R S : struct) : Set := R.rel ∪ S.rel ∪ (R.fld.prod S.fld)
def add_struct (R S : struct.{u}) : struct.{u} :=
let h := union_subset_of_subset_of_subset (union_subset_of_subset_of_subset (subset_trans R.is_rel (prod_subset_of_subset_of_subset subset_union_left subset_union_left))
(subset_trans S.is_rel (prod_subset_of_subset_of_subset subset_union_right subset_union_right)))
(prod_subset_of_subset_of_subset subset_union_left subset_union_right) in
⟨R.fld ∪ S.fld, add_rel R S, h⟩
-- Lemma 8G (a)
lemma add_struct_lin {R : struct} (Rlin : R.fld.lin_order R.rel) {S : struct} (Slin : S.fld.lin_order S.rel)
(disj : R.fld ∩ S.fld = ∅) : (add_struct R S).fld.lin_order (add_struct R S).rel :=
begin
split,
{ exact (add_struct R S).is_rel, },
{ intros x y z, rw add_struct, dsimp, rw add_rel,
simp only [mem_union, pair_mem_prod],
rintros ((xyR|xyS)|⟨xR, yS⟩) ((yzR|yzS)|⟨yR, zS⟩),
{ left, left, exact Rlin.trans xyR yzR, },
{ replace xyR := Rlin.rel xyR, replace yzS := Slin.rel yzS,
rw pair_mem_prod at xyR yzS, exfalso, apply mem_empty y,
rw [←disj, mem_inter], exact ⟨xyR.right, yzS.left⟩, },
{ replace xyR := Rlin.rel xyR,
rw pair_mem_prod at xyR, right, exact ⟨xyR.left, zS⟩, },
{ replace xyS := Slin.rel xyS, replace yzR := Rlin.rel yzR,
rw pair_mem_prod at xyS yzR, exfalso, apply mem_empty y,
rw [←disj, mem_inter], exact ⟨yzR.left, xyS.right⟩, },
{ left, right, exact Slin.trans xyS yzS, },
{ replace xyS := Slin.rel xyS,
rw pair_mem_prod at xyS, exfalso, apply mem_empty y,
rw [←disj, mem_inter], exact ⟨yR, xyS.right⟩, },
{ replace yzR := Rlin.rel yzR,
rw pair_mem_prod at yzR, exfalso, apply mem_empty y,
rw [←disj, mem_inter], exact ⟨yzR.left, yS⟩, },
{ replace yzS := Slin.rel yzS,
rw pair_mem_prod at yzS, right, exact ⟨xR, yzS.right⟩, },
{ right, exact ⟨xR, zS⟩, }, },
{ rw add_struct, dsimp, rw add_rel, intro x, simp only [mem_union, pair_mem_prod],
rintros ((xxR|xxS)|⟨xR, xS⟩),
{ exact Rlin.irrefl xxR, },
{ exact Slin.irrefl xxS, },
{ apply mem_empty x, rw [←disj, mem_inter], exact ⟨xR, xS⟩, }, },
{ rw add_struct, dsimp, intros x y, simp only [add_rel, mem_union, pair_mem_prod],
rintros (xR|xS) (yR|yS) xny,
{ cases Rlin.conn xR yR xny,
left, left, left, exact h,
right, left, left, exact h, },
{ left, right, exact ⟨xR, yS⟩, },
{ right, right, exact ⟨yR, xS⟩, },
{ cases Slin.conn xS yS xny,
left, left, right, exact h,
right, left, right, exact h, }, },
end
noncomputable def ot_add (ρ σ : Set.{u}) : Set.{u} := it (add_struct (ρ.ot_disj_repr σ).fst (ρ.ot_disj_repr σ).snd)
lemma ot_add_ot {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type) : (ρ.ot_add σ).order_type :=
begin
use add_struct (ρ.ot_disj_repr σ).fst (ρ.ot_disj_repr σ).snd,
exact ⟨add_struct_lin (ot_disj_repr_left_lin ρot σot) (ot_disj_repr_right_lin ρot σot) (ot_disj_repr_disj ρot σot), rfl⟩,
end
lemma iso_add_of_iso {R S : struct} (RS : isomorphic R S) {T U : struct} (TU : isomorphic T U)
(RT : R.fld ∩ T.fld = ∅) (SU : S.fld ∩ U.fld = ∅) : isomorphic (add_struct R T) (add_struct S U) :=
begin
rcases RS with ⟨f, ⟨fonto, foto⟩, fiso⟩, rcases TU with ⟨g, ⟨gonto, goto⟩, giso⟩,
refine ⟨f ∪ g, ⟨_, union_one_to_one foto goto _⟩, _⟩,
{ dsimp [add_struct],
simp only [←fonto.right.left, ←fonto.right.right, ←gonto.right.left, ←gonto.right.right] at RT ⊢,
exact union_fun fonto.left gonto.left RT, },
{ rw [fonto.right.right, gonto.right.right], exact SU },
dsimp [add_struct, add_rel], simp only [mem_union, pair_mem_prod],
rintros x y (xR|xT) (yR|yT),
{ rw [union_fun_value_left fonto gonto RT xR, union_fun_value_left fonto gonto RT yR],
specialize fiso xR yR, rw [fiso, or_assoc, or_assoc], apply or_congr_right, split,
rintro (xyT|⟨xR,yT⟩); exfalso,
{ apply mem_empty x, rw [←RT, mem_inter], refine ⟨xR, _⟩,
suffices xyT' : x.pair y ∈ T.fld.prod T.fld,
rw pair_mem_prod at xyT', exact xyT'.left,
exact T.is_rel xyT, },
{ apply mem_empty y, rw [←RT, mem_inter], exact ⟨yR, yT⟩, },
rintro (xyU|⟨xS,yU⟩); exfalso,
{ apply mem_empty (f.fun_value x), rw [←SU, mem_inter, ←fonto.right.right], rw ←fonto.right.left at xR,
refine ⟨fun_value_def'' fonto.left xR, _⟩,
suffices xyU' : (f.fun_value x).pair (f.fun_value y) ∈ U.fld.prod U.fld,
rw pair_mem_prod at xyU', exact xyU'.left,
exact U.is_rel xyU, },
{ apply mem_empty (f.fun_value y), rw [←SU, mem_inter, ←fonto.right.right], rw ←fonto.right.left at yR,
refine ⟨fun_value_def'' fonto.left yR, yU⟩, }, },
{ rw [union_fun_value_left fonto gonto RT xR, union_fun_value_right fonto gonto RT yT],
simp only [←fonto.right.right, ←gonto.right.right, ←fonto.right.left, ←gonto.right.left] at xR yT ⊢, split,
intro h, right, exact ⟨fun_value_def'' fonto.left xR, fun_value_def'' gonto.left yT⟩,
intro h, right, exact ⟨xR, yT⟩, },
{ rw [union_fun_value_right fonto gonto RT xT, union_fun_value_left fonto gonto RT yR], split,
rintro ((xyR|xyT)|⟨xR,yT⟩); exfalso,
{ apply mem_empty x, rw [←RT, mem_inter], refine ⟨_, xT⟩,
suffices xyR' : x.pair y ∈ R.fld.prod R.fld,
rw pair_mem_prod at xyR', exact xyR'.left,
exact R.is_rel xyR, },
{ apply mem_empty y, rw [←RT, mem_inter], refine ⟨yR, _⟩,
suffices xyT' : x.pair y ∈ T.fld.prod T.fld,
rw pair_mem_prod at xyT', exact xyT'.right,
exact T.is_rel xyT, },
{ apply mem_empty x, rw [←RT, mem_inter], exact ⟨xR, xT⟩, },
rintro ((xyS|xyU)|⟨xS,yU⟩); exfalso,
{ apply mem_empty (g.fun_value x), rw [←SU, mem_inter, ←gonto.right.right], rw ←gonto.right.left at xT,
refine ⟨_, fun_value_def'' gonto.left xT⟩,
suffices xyS : (g.fun_value x).pair (f.fun_value y) ∈ S.fld.prod S.fld,
rw pair_mem_prod at xyS, exact xyS.left,
exact S.is_rel xyS, },
{ apply mem_empty (f.fun_value y), rw [←SU, mem_inter, ←fonto.right.right], rw ←fonto.right.left at yR,
refine ⟨fun_value_def'' fonto.left yR, _⟩,
suffices xyS : (g.fun_value x).pair (f.fun_value y) ∈ U.fld.prod U.fld,
rw pair_mem_prod at xyS, exact xyS.right,
exact U.is_rel xyU, },
{ apply mem_empty (g.fun_value x), rw [←SU, mem_inter, ←gonto.right.right], rw ←gonto.right.left at xT,
exact ⟨xS, fun_value_def'' gonto.left xT⟩, }, },
{ rw [union_fun_value_right fonto gonto RT xT, union_fun_value_right fonto gonto RT yT],
specialize giso xT yT, rw giso, nth_rewrite 0 or_comm, nth_rewrite 2 or_comm,
rw [←or_assoc, ←or_assoc], apply or_congr_left, split,
rintros (⟨xR,yT⟩|xyR); exfalso; apply mem_empty x; rw [←RT, mem_inter],
{ exact ⟨xR, xT⟩, },
{ refine ⟨_, xT⟩,
suffices xyR' : x.pair y ∈ R.fld.prod R.fld,
rw pair_mem_prod at xyR', exact xyR'.left,
exact R.is_rel xyR, },
rintros (⟨xS,yU⟩|xyS); exfalso; apply mem_empty (g.fun_value x); rw [←SU, mem_inter, ←gonto.right.right];
rw ←gonto.right.left at xT,
{ refine ⟨xS, fun_value_def'' gonto.left xT⟩, },
{ refine ⟨_, fun_value_def'' gonto.left xT⟩,
suffices xyS' : (g.fun_value x).pair (g.fun_value y) ∈ S.fld.prod S.fld,
rw pair_mem_prod at xyS', exact xyS'.left,
exact S.is_rel xyS, }, },
end
lemma mem_ot_add {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type)
{T : struct} : struct_to_set T ∈ ρ.ot_add σ ↔ pioneering T ∧ ∃ R : struct, isomorphic R ρ.ot_repr ∧ ∃ S : struct, isomorphic S σ.ot_repr ∧
R.fld ∩ S.fld = ∅ ∧ isomorphic (add_struct R S) T :=
begin
rw [ot_add, struct_mem_it], apply and_congr_right', split,
intro iso,
exact ⟨_, ot_disj_repr_left_iso ρot σot, _, ot_disj_repr_right_iso ρot σot, ot_disj_repr_disj ρot σot, iso⟩,
rintro ⟨R, Riso, S, Siso, disj, iso⟩, refine iso_trans _ iso,
apply iso_add_of_iso,
exact (iso_trans (ot_disj_repr_left_iso ρot σot) (iso_symm Riso)),
exact (iso_trans (ot_disj_repr_right_iso ρot σot) (iso_symm Siso)),
exact ot_disj_repr_disj ρot σot,
exact disj,
end
lemma iso_ot_add_repr {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type)
{T : struct} : isomorphic T (ρ.ot_add σ).ot_repr ↔
∃ R : struct, isomorphic R ρ.ot_repr ∧ ∃ S : struct, isomorphic S σ.ot_repr ∧ R.fld ∩ S.fld = ∅ ∧ isomorphic (add_struct R S) T :=
begin
have h := ot_repr_mem (ot_add_ot ρot σot),
rw mem_ot_add ρot σot at h, rcases h with ⟨-, R, Riso, S, Siso, disj, iso⟩, split,
intro iso', exact ⟨_, Riso, _, Siso, disj, iso_trans iso (iso_symm iso')⟩,
rintro ⟨R', Riso', S', Siso', disj', iso'⟩,
refine iso_trans (iso_symm iso') (iso_trans (iso_add_of_iso (iso_trans Riso' (iso_symm Riso)) (iso_trans Siso' (iso_symm Siso)) disj' disj) iso),
end
noncomputable def ord_ot (α : Set) : Set := it (eps_order_struct α)
lemma ord_ot_is_ot {α : Set} (αord : α.is_ordinal) : α.ord_ot.order_type :=
⟨_, (ordinal_well_ordered' αord).lin, rfl⟩
lemma ord_eq_of_ord_ot_eq {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal)
(h : α.ord_ot = β.ord_ot) : α = β :=
begin
rw [←eps_img_ord_eq_self αord, ←eps_img_ord_eq_self βord,
←iso_iff_eps_img_eq (ordinal_well_ordered' αord) (ordinal_well_ordered' βord), ←iso_type_eq_iff_iso],
exact h,
end
lemma repr_ord_ot_iso {α : Set} (αord : α.is_ordinal) : isomorphic α.ord_ot.ot_repr α.eps_order_struct :=
iso_symm (struct_mem_it.mp (ot_repr_mem (ord_ot_is_ot αord))).right
def rev_struct (R : struct) : struct := ⟨R.fld, R.rel.inv, inv_sub_prod R.is_rel⟩
noncomputable def ot_rev (ρ : Set) : Set := it (rev_struct ρ.ot_repr)
lemma ot_rev_ot {ρ : Set} (ρot : ρ.order_type) : ρ.ot_rev.order_type :=
begin
refine ⟨_, _, rfl⟩, rw rev_struct, dsimp, split,
{ exact (rev_struct _).is_rel, },
{ intros x y z xy yz, rw pair_mem_inv at xy yz ⊢, exact (ot_repr_lin ρot).trans yz xy, },
{ intros x xx, rw pair_mem_inv at xx, exact (ot_repr_lin ρot).irrefl xx, },
{ intros x y hx hy xy, simp only [pair_mem_inv, or.comm], exact (ot_repr_lin ρot).conn hx hy xy, },
end
lemma it_rev_eq_of_iso {R S : struct} (RS : isomorphic R S) : it (rev_struct R) = it (rev_struct S) :=
begin
rw iso_type_eq_iff_iso, rcases RS with ⟨f, ⟨fonto, foto⟩, corr⟩, refine ⟨f, ⟨fonto, foto⟩, _⟩,
dsimp [rev_struct], simp only [pair_mem_inv], intros x y xR yR, exact corr yR xR,
end
lemma fin_ord_rev_ot_eq_self {n : Set} (nω : n ∈ ω) : n.ord_ot.ot_rev = n.ord_ot :=
begin
have not := ord_ot_is_ot (nat_is_ord nω),
have rot := ot_rev_ot not,
rw [ot_rev, it_rev_eq_of_iso (repr_ord_ot_iso (nat_is_ord nω)), ord_ot, iso_type_eq_iff_iso],
refine finite_lin_order_iso _ _ (ordinal_well_ordered (nat_is_ord nω)).lin,
rw eps_order_struct_fld, exact nat_finite nω,
rw [eps_order_struct_fld, eps_order_struct_rel],
exact inv_lin_order (ordinal_well_ordered (nat_is_ord nω)).lin,
end
def mul_rel (R S : struct) : Set :=
pair_sep (λ x y, x.snd.pair y.snd ∈ S.rel ∨ x.snd = y.snd ∧ x.fst.pair y.fst ∈ R.rel) (R.fld.prod S.fld) (R.fld.prod S.fld)
def mul_struct (R S : struct) : struct := ⟨R.fld.prod S.fld, mul_rel R S, pair_sep_sub_prod⟩
noncomputable def ot_mul (ρ σ : Set) : Set := it (mul_struct ρ.ot_repr σ.ot_repr)
-- Lemma 8H (a)
lemma mul_struct_lin {R : struct} (Rlin : R.fld.lin_order R.rel) {S : struct} (Slin : S.fld.lin_order S.rel) :
(mul_struct R S).fld.lin_order (mul_struct R S).rel :=
begin
split,
{ exact (mul_struct R S).is_rel, },
{ intros a b c, rw mul_struct, dsimp, rw mul_rel, intros ab bc, rw pair_mem_pair_sep at ab bc,
rw pair_mem_pair_sep' ab.left bc.right.left, rw [mem_prod, mem_prod] at ab bc,
rcases ab with ⟨⟨x₁, hx₁, y₁, hy₁, ha⟩, ⟨x₂, hx₂, y₂, hy₂, hb⟩, c₁⟩,
rcases bc with ⟨-, ⟨x₃, hx₃, y₃, hy₃, hc⟩, c₂⟩, subst ha, subst hb, subst hc,
simp only [fst_congr, snd_congr] at c₁ c₂ ⊢,
rcases c₁ with (yy₁|⟨yy₁, xx₁⟩); rcases c₂ with (yy₂|⟨yy₂, xx₂⟩),
left, exact Slin.trans yy₁ yy₂,
subst yy₂, left, exact yy₁,
subst yy₁, left, exact yy₂,
subst yy₁, subst yy₂, right, exact ⟨rfl, Rlin.trans xx₁ xx₂⟩, },
{ intro x, rw mul_struct, dsimp, rw [mul_rel, pair_mem_pair_sep, mem_prod],
rintro ⟨-, -, (aaR|⟨-, bbS⟩)⟩,
exact Slin.irrefl aaR,
exact Rlin.irrefl bbS, },
{ rw mul_struct, dsimp, intros x y hx hy xny,
rw [mul_rel, pair_mem_pair_sep' hx hy, pair_mem_pair_sep' hy hx], rw mem_prod at hx hy,
rcases hx with ⟨a₁, ha₁, b₁, hb₁, xab⟩, rcases hy with ⟨a₂, ha₂, b₂, hb₂, yab⟩, subst xab, subst yab,
simp only [fst_congr, snd_congr],
by_cases beb : b₁ = b₂,
by_cases aea : a₁ = a₂,
subst aea, subst beb, exfalso, exact xny rfl,
subst beb, cases Rlin.conn ha₁ ha₂ aea with ala ala,
left, right, exact ⟨rfl, ala⟩,
right, right, exact ⟨rfl, ala⟩,
cases Slin.conn hb₁ hb₂ beb with blb blb,
left, left, exact blb,
right, left, exact blb, },
end
-- Lemma 8H (b)
lemma iso_mul_of_iso {R S : struct} (RS : isomorphic R S) {T U : struct} (TU : isomorphic T U)
: isomorphic (mul_struct R T) (mul_struct S U) :=
begin
rcases RS with ⟨f, fc, fiso⟩, rcases TU with ⟨g, gc, giso⟩,
obtain ⟨honto, hoto⟩ := T6H_b_lemma fc gc,
refine ⟨_, ⟨honto, hoto⟩, _⟩, dsimp [mul_struct], intros x y xRT yRT,
rw ←honto.right.left at xRT yRT, rw [pair_sep_eq_fun_value xRT, pair_sep_eq_fun_value yRT], dsimp [mul_rel],
rw honto.right.left at xRT yRT, rw [pair_mem_pair_sep' xRT yRT],
rw mem_prod at xRT yRT, rcases xRT with ⟨r, rR, t, tT, xrt⟩, rcases yRT with ⟨r', rR', t', tT', yrt⟩,
subst xrt, subst yrt, simp only [fst_congr, snd_congr],
have fgrt : (f.fun_value r).pair (g.fun_value t) ∈ S.fld.prod U.fld,
rw pair_mem_prod, rw ←fc.onto.right.left at rR, rw ←gc.onto.right.left at tT,
rw [←fc.onto.right.right, ←gc.onto.right.right],
exact ⟨fun_value_def'' fc.onto.left rR, fun_value_def'' gc.onto.left tT⟩,
have fgrt' : (f.fun_value r').pair (g.fun_value t') ∈ S.fld.prod U.fld,
rw pair_mem_prod, rw ←fc.onto.right.left at rR', rw ←gc.onto.right.left at tT',
rw [←fc.onto.right.right, ←gc.onto.right.right],
exact ⟨fun_value_def'' fc.onto.left rR', fun_value_def'' gc.onto.left tT'⟩,
rw pair_mem_pair_sep' fgrt fgrt', simp only [fst_congr, snd_congr, giso tT tT', fiso rR rR'],
rw eq_iff_fun_value_eq_of_oto gc.onto.left gc.oto _ _,
rw gc.onto.right.left, exact tT,
rw gc.onto.right.left, exact tT',
end
lemma ot_mul_ot {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type) : (ρ.ot_mul σ).order_type :=
begin
use mul_struct ρ.ot_repr σ.ot_repr, exact ⟨mul_struct_lin (ot_repr_lin ρot) (ot_repr_lin σot), rfl⟩,
end
lemma mem_ot_mul {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type)
{T : struct} : struct_to_set T ∈ ρ.ot_mul σ ↔ pioneering T ∧
∃ R S : struct, isomorphic R ρ.ot_repr ∧ isomorphic S σ.ot_repr ∧ isomorphic (mul_struct R S) T :=
begin
rw [ot_mul, struct_mem_it], apply and_congr_right', split,
intro iso,
exact ⟨_, _, iso_refl, iso_refl, iso⟩,
rintro ⟨R, S, Riso, Siso, iso⟩,
exact iso_trans (iso_symm (iso_mul_of_iso Riso Siso)) iso,
end
lemma iso_ot_mul_repr {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type)
{T : struct} : isomorphic T (ρ.ot_mul σ).ot_repr ↔
∃ R S : struct, isomorphic R ρ.ot_repr ∧ isomorphic S σ.ot_repr ∧ isomorphic (mul_struct R S) T :=
begin
have h := ot_repr_mem (ot_mul_ot ρot σot),
rw mem_ot_mul ρot σot at h, rcases h with ⟨-, R, S, Riso, Siso, iso⟩, split,
intro iso', exact ⟨_, _, Riso, Siso, iso_trans iso (iso_symm iso')⟩,
rintro ⟨R', S', Riso', Siso', iso'⟩,
exact iso_trans (iso_symm iso') (iso_trans (iso_mul_of_iso (iso_trans Riso' (iso_symm Riso)) (iso_trans Siso' (iso_symm Siso))) iso),
end
lemma it_mul {R : struct} (Rlin : R.fld.lin_order R.rel) {S : struct} (Slin : S.fld.lin_order S.rel) :
(it R).ot_mul (it S) = it (mul_struct R S) :=
begin
rw [ot_mul, iso_type_eq_iff_iso],
exact iso_mul_of_iso (iso_symm (iso_repr_it Rlin)) (iso_symm (iso_repr_it Slin)),
end
-- Theorem 8I (a)
theorem ot_add_assoc {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type) {τ : Set} (τot : τ.order_type) :
(ρ.ot_add σ).ot_add τ = ρ.ot_add (σ.ot_add τ) :=
begin
have ρσot := ot_add_ot ρot σot, have στot := ot_add_ot σot τot,
obtain ⟨R, Riso, S, Siso, RSdisj, T, Tiso, RTdisj, STdisj⟩ := ex_3_disj (ot_repr_lin ρot) (ot_repr_lin σot) (ot_repr_lin τot),
rw ot_eq_iff_exists_iso (ot_add_ot ρσot τot) (ot_add_ot ρot στot),
refine ⟨add_struct (add_struct R S) T, _, add_struct R (add_struct S T), _, iso_of_eq _⟩,
simp only [iso_ot_add_repr ρσot τot, iso_ot_add_repr ρot σot],
refine ⟨_, ⟨_, Riso, _, Siso, RSdisj, iso_refl⟩, _, Tiso, _, iso_refl⟩,
dsimp [add_struct], rw [union_inter, RTdisj, STdisj, union_empty],
simp only [iso_ot_add_repr ρot στot, iso_ot_add_repr σot τot],
refine ⟨_, Riso, _, ⟨_, Siso, _, Tiso, STdisj, iso_refl⟩, _, iso_refl⟩,
dsimp [add_struct], rw [inter_union, RSdisj, RTdisj, union_empty],
ext; dsimp [add_struct],
rw union_assoc,
dsimp [add_rel], rw [union_prod, prod_union], apply ext, finish,
end
lemma mul_assoc_lemma {R S T : struct} :
isomorphic (mul_struct R (mul_struct S T)) (mul_struct (mul_struct R S) T) :=
begin
use pair_sep_eq (R.fld.prod (S.fld.prod T.fld)) ((R.fld.prod S.fld).prod T.fld) (λ x, (x.fst.pair x.snd.fst).pair x.snd.snd),
have deq : ((R.fld.prod (S.fld.prod T.fld)).pair_sep_eq ((R.fld.prod S.fld).prod T.fld)
(λ x, (x.fst.pair x.snd.fst).pair x.snd.snd)).dom = R.fld.prod (S.fld.prod T.fld),
apply pair_sep_eq_dom_eq,
intros rst rstRST, simp only [mem_prod, exists_prop] at rstRST, dsimp,
rcases rstRST with ⟨r, rR, st, ⟨s, sS, t, tT, rsrs⟩, rstrst⟩, subst rsrs, subst rstrst,
simp only [fst_congr, snd_congr, pair_mem_prod], exact ⟨⟨rR, sS⟩, tT⟩,
refine ⟨⟨⟨pair_sep_eq_is_fun, deq, pair_sep_eq_ran_eq _⟩, pair_sep_eq_oto _⟩, _⟩,
{ intros rst rstRST, simp only [mem_prod, exists_prop] at rstRST,
rcases rstRST with ⟨rs, ⟨r, rR, s, sS, rsrs⟩, t, tT, rstrst⟩, subst rsrs, subst rstrst,
use r.pair (s.pair t), dsimp, simp only [fst_congr, snd_congr, pair_mem_prod],
exact ⟨⟨rR, sS, tT⟩, rfl⟩, },
{ intros rst rstRST rst' rstRST' eq, dsimp at eq,
simp only [mem_prod, exists_prop] at rstRST rstRST',
rcases rstRST with ⟨r, rR, st, ⟨s, sS, t, tT, stst⟩, rstrst⟩, subst stst, subst rstrst,
rcases rstRST' with ⟨r', rR', st', ⟨s', sS', t', tT', stst'⟩, rstrst'⟩, subst stst', subst rstrst',
simp only [fst_congr, snd_congr] at eq,
obtain ⟨rsrs, tt⟩ := pair_inj eq, obtain ⟨rr, ss⟩ := pair_inj rsrs,
subst rr, subst ss, subst tt, },
{ dsimp [mul_struct, mul_rel], intros rst rst' rstRST rstRST',
have rstD : rst ∈ ((R.fld.prod (S.fld.prod T.fld)).pair_sep_eq ((R.fld.prod S.fld).prod T.fld)
(λ x, (x.fst.pair x.snd.fst).pair x.snd.snd)).dom,
rw deq, exact rstRST,
have rstD' : rst' ∈ ((R.fld.prod (S.fld.prod T.fld)).pair_sep_eq ((R.fld.prod S.fld).prod T.fld)
(λ x, (x.fst.pair x.snd.fst).pair x.snd.snd)).dom,
rw deq, exact rstRST',
rw [pair_sep_eq_fun_value rstD, pair_sep_eq_fun_value rstD'], dsimp,
rw pair_mem_pair_sep' rstRST rstRST', rw mem_prod at rstRST rstRST',
rcases rstRST with ⟨r, rR, st, stST, rstrst⟩, rcases rstRST' with ⟨r', rR', st', stST', rstrst'⟩,
subst rstrst, subst rstrst', simp only [fst_congr, snd_congr, pair_mem_pair_sep' stST stST'],
rw mem_prod at stST stST',
rcases stST with ⟨s, sS, t, tT, stst⟩, rcases stST' with ⟨s', sS', t', tT', stst'⟩, subst stst, subst stst',
simp only [fst_congr, snd_congr],
have rsmem : r.pair s ∈ R.fld.prod S.fld, rw pair_mem_prod, exact ⟨rR, sS⟩,
have rsmem' : r'.pair s' ∈ R.fld.prod S.fld, rw pair_mem_prod, exact ⟨rR', sS'⟩,
have rstmem : (r.pair s).pair t ∈ (R.fld.prod S.fld).prod T.fld,
rw pair_mem_prod, exact ⟨rsmem, tT⟩,
have rstmem' : (r'.pair s').pair t' ∈ (R.fld.prod S.fld).prod T.fld,
rw pair_mem_prod, exact ⟨rsmem', tT'⟩,
rw deq at rstD rstD',
simp only [fst_congr, snd_congr, pair_mem_pair_sep' rstmem rstmem', pair_mem_pair_sep' rsmem rsmem'],
clear deq rsmem rsmem' rstmem rstmem' rstD' rstD, split,
rintro ((tt|⟨tt,ss⟩)|⟨stst,rr⟩),
exact or.inl tt,
subst tt, exact or.inr ⟨rfl, or.inl ss⟩,
obtain ⟨ss, tt⟩ := pair_inj stst, subst ss, subst tt,
exact or.inr ⟨rfl, or.inr ⟨rfl, rr⟩⟩,
rintros (tt|⟨tt,(ss|⟨ss,rr⟩)⟩),
exact or.inl (or.inl tt),
subst tt, exact or.inl (or.inr ⟨rfl, ss⟩),
subst tt, subst ss, exact or.inr ⟨rfl, rr⟩, },
end
-- Theorem 8I (a)
theorem ot_mul_assoc {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type) {τ : Set} (τot : τ.order_type) :
(ρ.ot_mul σ).ot_mul τ = ρ.ot_mul (σ.ot_mul τ) :=
begin
have ρσot := ot_mul_ot ρot σot, have στot := ot_mul_ot σot τot,
apply order_type_ext' (ot_mul_ot ρσot τot) (ot_mul_ot ρot στot),
simp only [mem_ot_mul ρσot τot, mem_ot_mul ρot στot], intro U, apply and_congr_right',
simp only [iso_ot_mul_repr ρot σot, iso_ot_mul_repr σot τot], split,
rintros ⟨RS, T, ⟨R, S, Riso, Siso, RSiso⟩, Tiso, RSTiso⟩,
refine ⟨_, _, Riso, ⟨_, _, Siso, Tiso, iso_refl⟩, _⟩,
refine iso_trans _ RSTiso, refine iso_trans mul_assoc_lemma (iso_mul_of_iso RSiso iso_refl),
rintros ⟨R, ST, Riso, ⟨S, T, Siso, Tiso, STiso⟩, RSTiso⟩,
refine ⟨_, _, ⟨_, _, Riso, Siso, iso_refl⟩, Tiso, _⟩,
refine iso_trans _ RSTiso, apply iso_trans _ (iso_mul_of_iso iso_refl STiso),
exact iso_symm mul_assoc_lemma,
end
lemma pair_sep_union_prod_eq {p : Set → Set → Prop} {A B C : Set}
(BA : B ⊆ A) (CA : C ⊆ A) : (pair_sep p A A) ∪ (B.prod C) = pair_sep (λ x y, p x y ∨ x.pair y ∈ B.prod C) A A :=
begin
apply @rel_ext' A A _ _ _ pair_sep_sub_prod _,
exact union_subset_of_subset_of_subset pair_sep_sub_prod (prod_subset_of_subset_of_subset BA CA),
intros x xA y yA, rw [mem_union, pair_mem_pair_sep' xA yA, pair_mem_pair_sep' xA yA],
end
lemma pair_sep_union_eq {p q : Set → Set → Prop} {A B : Set} :
(pair_sep p A A) ∪ (pair_sep q B B) = pair_sep (λ x y, x ∈ A ∧ y ∈ A ∧ p x y ∨ x ∈ B ∧ y ∈ B ∧ q x y) (A ∪ B) (A ∪ B) :=
begin
refine @rel_ext' (A ∪ B) (A ∪ B) _ _ _ pair_sep_sub_prod _,
apply union_subset_of_subset_of_subset,
exact subset_trans pair_sep_sub_prod (prod_subset_of_subset_of_subset subset_union_left subset_union_left),
exact subset_trans pair_sep_sub_prod (prod_subset_of_subset_of_subset subset_union_right subset_union_right),
intros x xAB y yAB, rw pair_mem_pair_sep' xAB yAB,
simp only [mem_union] at *, simp only [pair_mem_pair_sep],
end
-- Theorem 8I (b)
theorem ot_add_mul {ρ : Set} (ρot : ρ.order_type) {σ : Set} (σot : σ.order_type) {τ : Set} (τot : τ.order_type) :
ρ.ot_mul (σ.ot_add τ) = (ρ.ot_mul σ).ot_add (ρ.ot_mul τ) :=
begin
have στot := ot_add_ot σot τot, have ρσot := ot_mul_ot ρot σot, have ρτot := ot_mul_ot ρot τot,
let R := ρ.ot_repr,
have Riso : isomorphic R ρ.ot_repr := iso_refl,
obtain ⟨⟨S, T⟩, Siso, Tiso, STdisj⟩ := ex_disj_of_ord_type' (ot_repr_lin σot) (ot_repr_lin τot),
dsimp at STdisj Siso Tiso,
have disj' : R.fld.prod S.fld ∩ R.fld.prod T.fld = ∅, rw [←prod_inter, STdisj, prod_empty_eq_empty],
rw ot_eq_iff_exists_iso (ot_mul_ot ρot στot) (ot_add_ot ρσot ρτot),
refine ⟨mul_struct R (add_struct S T), _, add_struct (mul_struct R S) (mul_struct R T), _, iso_of_eq _⟩,
simp only [iso_ot_mul_repr ρot στot, iso_ot_add_repr σot τot],
refine ⟨_, _, Riso, ⟨_, Siso, _, Tiso, STdisj, iso_refl⟩, iso_refl⟩,
simp only [iso_ot_add_repr ρσot ρτot, iso_ot_mul_repr ρot σot, iso_ot_mul_repr ρot τot],
refine ⟨_, ⟨_, _, Riso, Siso, iso_refl⟩, _, ⟨_, _, Riso, Tiso, iso_refl⟩, disj', iso_refl⟩,
have e : R.fld.prod (S.fld ∪ T.fld) = R.fld.prod S.fld ∪ R.fld.prod T.fld,
rw prod_union,
ext,
dsimp [mul_struct, add_struct], exact e,
let U := R.fld.prod (S.fld ∪ T.fld),
apply @rel_ext' U U,
dsimp [mul_struct, add_struct, mul_rel, add_rel], exact pair_sep_sub_prod,
dsimp [U, add_struct, add_rel, mul_struct], rw e, apply union_subset_of_subset_of_subset,
dsimp [mul_rel], apply union_subset_of_subset_of_subset,
exact subset_trans pair_sep_sub_prod (prod_subset_of_subset_of_subset subset_union_left subset_union_left),
exact subset_trans pair_sep_sub_prod (prod_subset_of_subset_of_subset subset_union_right subset_union_right),
exact prod_subset_of_subset_of_subset subset_union_left subset_union_right,
simp only [U, mem_prod], rintros x ⟨r, rR, st, stST, xrst⟩ y ⟨r', rR', st', stST', yrst⟩; subst xrst; subst yrst,
simp only [mul_struct, add_struct, mul_rel, add_rel],
rw [pair_sep_union_eq, pair_sep_union_prod_eq subset_union_left subset_union_right],
have rstRST : r.pair st ∈ R.fld.prod (S.fld ∪ T.fld),
rw pair_mem_prod, exact ⟨rR, stST⟩,
have rstRST' : r'.pair st' ∈ R.fld.prod (S.fld ∪ T.fld),
rw pair_mem_prod, exact ⟨rR', stST'⟩,
simp only [←e, pair_mem_pair_sep' rstRST rstRST', fst_congr, snd_congr, mem_union, pair_mem_prod], split,
rintro (((stst|stst)|⟨stS,stT⟩)|⟨stst,rr⟩),
{ have stst' := S.is_rel stst, rw pair_mem_prod at stst',
left, left, exact ⟨⟨rR, stst'.left⟩, ⟨rR', stst'.right⟩, or.inl stst⟩, },
{ have stst' := T.is_rel stst, rw pair_mem_prod at stst',
left, right, exact ⟨⟨rR, stst'.left⟩, ⟨rR', stst'.right⟩, or.inl stst⟩, },
{ right, exact ⟨⟨rR, stS⟩, ⟨rR', stT⟩⟩, },
{ subst stst, rw mem_union at stST, rcases stST with (stS|stT),
left, left, exact ⟨⟨rR, stS⟩, ⟨rR', stS⟩, or.inr ⟨rfl, rr⟩⟩,
left, right, exact ⟨⟨rR, stT⟩, ⟨rR', stT⟩, or.inr ⟨rfl, rr⟩⟩, },
finish,
end
-- Theorem 8I (c)
theorem ot_add_zero {ρ : Set} (ρot : ρ.order_type) : ρ.ot_add (ord_ot ∅) = ρ :=
begin
obtain ⟨⟨R, E⟩, Riso, Eiso, REdisj⟩ :=
ex_disj_of_ord_type' (ot_repr_lin ρot) (ot_repr_lin (ord_ot_is_ot zero_is_ord)),
dsimp at Riso Eiso REdisj,
rw ot_eq_iff_exists_iso (ot_add_ot ρot (ord_ot_is_ot zero_is_ord)) ρot,
refine ⟨add_struct R E, _, R, Riso, _⟩,
rw iso_ot_add_repr ρot (ord_ot_is_ot zero_is_ord),
refine ⟨_, Riso, _, Eiso, REdisj, iso_refl⟩,
refine iso_trans (iso_add_of_iso iso_refl (iso_trans Eiso (repr_ord_ot_iso zero_is_ord)) REdisj _) _,
rw [eps_order_struct_fld, inter_empty],
apply iso_of_eq, ext; dsimp [add_struct],
rw union_empty,
rw [add_rel, eps_order_struct_fld, eps_order_struct_rel, prod_empty_eq_empty, union_empty],
suffices h : eps_order ∅ = ∅,
rw [h, union_empty],
rw [eps_order, rel_eq_empty pair_sep_is_rel],
intros x y xy, rw pair_mem_pair_sep at xy, exact mem_empty _ xy.left,
end
-- Theorem 8I (c)
theorem ot_zero_add {ρ : Set} (ρot : ρ.order_type) : (ord_ot ∅).ot_add ρ = ρ :=
begin
obtain ⟨⟨E, R⟩, Eiso, Riso, REdisj⟩ :=
ex_disj_of_ord_type' (ot_repr_lin (ord_ot_is_ot zero_is_ord)) (ot_repr_lin ρot),
dsimp at Riso Eiso REdisj,
rw ot_eq_iff_exists_iso (ot_add_ot (ord_ot_is_ot zero_is_ord) ρot) ρot,
refine ⟨add_struct E R, _, R, Riso, _⟩,
rw iso_ot_add_repr (ord_ot_is_ot zero_is_ord) ρot,
refine ⟨_, Eiso, _, Riso, REdisj, iso_refl⟩,
refine iso_trans (iso_add_of_iso (iso_trans Eiso (repr_ord_ot_iso zero_is_ord)) iso_refl REdisj _) _,
rw [eps_order_struct_fld, inter_comm, inter_empty],
apply iso_of_eq, ext; dsimp [add_struct],
rw [union_comm, union_empty],
rw [add_rel, eps_order_struct_fld, eps_order_struct_rel, empty_prod_eq_empty, union_empty],
suffices h : eps_order ∅ = ∅,
rw [h, union_comm, union_empty],
rw [eps_order, rel_eq_empty pair_sep_is_rel],
intros x y xy, rw pair_mem_pair_sep at xy, exact mem_empty _ xy.left,
end
-- Theorem 8I (c)
theorem ot_mul_one {ρ : Set} (ρot : ρ.order_type) : ρ.ot_mul (ord_ot one) = ρ :=
begin
have one_ord := nat_is_ord one_nat,
let R := ρ.ot_repr,
have Riso : isomorphic R ρ.ot_repr := iso_refl,
let S := one.eps_order_struct,
have Siso : isomorphic S one.ord_ot.ot_repr := iso_symm (repr_ord_ot_iso one_ord),
rw ot_eq_iff_exists_iso (ot_mul_ot ρot (ord_ot_is_ot one_ord)) ρot,
refine ⟨mul_struct R S, _, R, Riso, _⟩,
rw iso_ot_mul_repr ρot (ord_ot_is_ot one_ord),
refine ⟨_, _, Riso, Siso, iso_refl⟩,
let f := pair_sep_eq (R.fld.prod S.fld) R.fld fst,
have fd : f.dom = R.fld.prod S.fld, apply pair_sep_eq_dom_eq,
simp only [S, eps_order_struct_fld, one, succ, union_empty, mem_prod, exists_prop, mem_singleton],
rintros x ⟨r, rR, s, sS, xrs⟩, subst xrs, rw fst_congr, exact rR,
refine ⟨f, ⟨⟨pair_sep_eq_is_fun, fd, pair_sep_eq_ran_eq _⟩, pair_sep_eq_oto _⟩, _⟩;
simp only [S, eps_order_struct_fld, one, succ, union_empty, mem_prod, exists_prop, mem_singleton],
{ intros r rR, refine ⟨_, ⟨_, rR, _, rfl, rfl⟩, _⟩, rw fst_congr, },
{ rintros x ⟨r, rR, s, sS, xrs⟩ y ⟨r', rR', s', sS', yrs⟩ rr, subst xrs, subst yrs,
simp only [fst_congr] at rr, subst rr, subst sS, subst sS', },
{ simp only [mul_struct, eps_order_struct_fld, mul_rel, eps_order_struct_rel],
intros x y xRS yRS, rw pair_mem_pair_sep' xRS yRS,
have xf : x ∈ f.dom, simp only [fd, S, eps_order_struct_fld, one, succ, union_empty], exact xRS,
have yf : y ∈ f.dom, simp only [fd, S, eps_order_struct_fld, one, succ, union_empty], exact yRS,
rw [pair_sep_eq_fun_value xf, pair_sep_eq_fun_value yf],
simp only [mem_prod, exists_prop] at xRS yRS,
rcases xRS with ⟨r, rR, s, sS, xrs⟩, rcases yRS with ⟨r', rR', s', sS', yrs⟩, subst xrs, subst yrs,
simp only [fst_congr, snd_congr],
rw pair_mem_eps_order' sS sS', rw mem_singleton at sS sS', subst sS, subst sS',
finish, },
end
-- Theorem 8I (c)
theorem ot_one_mul {ρ : Set} (ρot : ρ.order_type) : (ord_ot one).ot_mul ρ = ρ :=
begin
have one_ord := nat_is_ord one_nat,
let R := ρ.ot_repr,
have Riso : isomorphic R ρ.ot_repr := iso_refl,
let S := one.eps_order_struct,
have Siso : isomorphic S one.ord_ot.ot_repr := iso_symm (repr_ord_ot_iso one_ord),
rw ot_eq_iff_exists_iso (ot_mul_ot (ord_ot_is_ot one_ord) ρot) ρot,
refine ⟨mul_struct S R, _, R, Riso, _⟩,
rw iso_ot_mul_repr (ord_ot_is_ot one_ord) ρot,
refine ⟨_, _, Siso, Riso, iso_refl⟩,
let f := pair_sep_eq (S.fld.prod R.fld) R.fld snd,
have fd : f.dom = S.fld.prod R.fld, apply pair_sep_eq_dom_eq,
simp only [S, eps_order_struct_fld, one, succ, union_empty, mem_prod, exists_prop, mem_singleton],
rintros x ⟨r, rR, s, sS, xrs⟩, subst xrs, rw snd_congr, exact sS,
refine ⟨f, ⟨⟨pair_sep_eq_is_fun, fd, pair_sep_eq_ran_eq _⟩, pair_sep_eq_oto _⟩, _⟩;
simp only [S, eps_order_struct_fld, one, succ, union_empty, mem_prod, exists_prop, mem_singleton],
{ intros r rR, refine ⟨_, ⟨_, rfl, _, rR, rfl⟩, _⟩, rw snd_congr, },
{ rintros x ⟨r, rR, s, sS, xrs⟩ y ⟨r', rR', s', sS', yrs⟩ rr, subst xrs, subst yrs,
simp only [snd_congr] at rr, subst rr, subst rR, subst rR', },
{ simp only [mul_struct, eps_order_struct_fld, mul_rel, eps_order_struct_rel],
intros x y xRS yRS, rw pair_mem_pair_sep' xRS yRS,
have xf : x ∈ f.dom, simp only [fd, S, eps_order_struct_fld, one, succ, union_empty], exact xRS,
have yf : y ∈ f.dom, simp only [fd, S, eps_order_struct_fld, one, succ, union_empty], exact yRS,
rw [pair_sep_eq_fun_value xf, pair_sep_eq_fun_value yf],
simp only [mem_prod, exists_prop] at xRS yRS,
rcases xRS with ⟨r, rR, s, sS, xrs⟩, rcases yRS with ⟨r', rR', s', sS', yrs⟩, subst xrs, subst yrs,
simp only [fst_congr, snd_congr],
rw pair_mem_eps_order' rR rR', rw mem_singleton at rR rR', subst rR, subst rR',
finish, },
end
-- Theorem 8I (c)
theorem ot_mul_zero {ρ : Set} (ρot : ρ.order_type) : ρ.ot_mul (ord_ot ∅) = ord_ot ∅ :=
begin
let R := ρ.ot_repr,
have Riso : isomorphic R ρ.ot_repr := iso_refl,
let S := eps_order_struct ∅,
have Siso : isomorphic S (ord_ot ∅).ot_repr := iso_symm (repr_ord_ot_iso zero_is_ord),
rw ot_eq_iff_exists_iso (ot_mul_ot ρot (ord_ot_is_ot zero_is_ord)) (ord_ot_is_ot zero_is_ord),
refine ⟨mul_struct R S, _, _, Siso, iso_of_eq _⟩,
rw iso_ot_mul_repr ρot (ord_ot_is_ot zero_is_ord),
exact ⟨_, _, Riso, Siso, iso_refl⟩,
ext; simp only [mul_struct, mul_rel, eps_order, S, eps_order_struct_fld, prod_empty_eq_empty, eps_order_struct_rel],
apply rel_ext pair_sep_is_rel pair_sep_is_rel,
intros x y, simp only [pair_mem_pair_sep], split,
rintros ⟨xe, _, _⟩, exfalso, exact mem_empty _ xe,
rintros ⟨xe, _, _⟩, exfalso, exact mem_empty _ xe,
end
-- Theorem 8I (c)
theorem ot_zero_mul {ρ : Set} (ρot : ρ.order_type) : (ord_ot ∅).ot_mul ρ = ord_ot ∅ :=
begin
let R := ρ.ot_repr,
have Riso : isomorphic R ρ.ot_repr := iso_refl,
let S := eps_order_struct ∅,
have Siso : isomorphic S (ord_ot ∅).ot_repr := iso_symm (repr_ord_ot_iso zero_is_ord),
rw ot_eq_iff_exists_iso (ot_mul_ot (ord_ot_is_ot zero_is_ord) ρot) (ord_ot_is_ot zero_is_ord),
refine ⟨mul_struct S R, _, _, Siso, iso_of_eq _⟩,
rw iso_ot_mul_repr (ord_ot_is_ot zero_is_ord) ρot,
exact ⟨_, _, Siso, Riso, iso_refl⟩,
ext; simp only [mul_struct, mul_rel, eps_order, S, eps_order_struct_fld, empty_prod_eq_empty, eps_order_struct_rel],
apply rel_ext pair_sep_is_rel pair_sep_is_rel,
intros x y, simp only [pair_mem_pair_sep], split,
rintros ⟨xe, _, _⟩, exfalso, exact mem_empty _ xe,
rintros ⟨xe, _, _⟩, exfalso, exact mem_empty _ xe,
end
lemma prod_corr_of_corr {A B f : Set} (AB : A.correspondence B f)
{C D g : Set} (CD : C.correspondence D g) :
(A.prod C).correspondence (B.prod D) (pair_sep_eq (A.prod C) (B.prod D) (λ z, (f.fun_value z.fst).pair (g.fun_value z.snd))) :=
begin
refine ⟨⟨pair_sep_eq_is_fun, pair_sep_eq_dom_eq _, pair_sep_eq_ran_eq _⟩, pair_sep_eq_oto _⟩,
{ dsimp, simp only [pair_mem_prod, ←AB.onto.right.right, ←CD.onto.right.right],
intro x, rw mem_prod, rintro ⟨a, aA, c, cC, xac⟩, subst xac, rw [fst_congr, snd_congr],
refine ⟨fun_value_def'' AB.onto.left _, fun_value_def'' CD.onto.left _⟩,
rwa AB.onto.right.left,
rwa CD.onto.right.left, },
{ dsimp, simp only [mem_prod, exists_prop,
←AB.onto.right.right, mem_ran_iff AB.onto.left, AB.onto.right.left,
←CD.onto.right.right, mem_ran_iff CD.onto.left, CD.onto.right.left],
rintros y ⟨b, ⟨a, aA, ab⟩, d, ⟨c, cC, dc⟩, ybd⟩, subst ybd, subst ab, subst dc,
use a.pair c, rw [fst_congr, snd_congr], exact ⟨⟨_, aA, _, cC, rfl⟩, rfl⟩, },
{ dsimp, simp only [mem_prod, exists_prop],
rintros x₁ ⟨a₁, aA₁, c₁, cC₁, xac₁⟩ x₂ ⟨a₂, aA₂, c₂, cC₂, xac₂⟩ acac, subst xac₁, subst xac₂,
simp only [fst_congr, snd_congr] at acac,
obtain ⟨faa, fcc⟩ := pair_inj acac,
rw ←AB.onto.right.left at aA₁ aA₂, rw ←CD.onto.right.left at cC₁ cC₂,
rw [from_one_to_one AB.onto.left AB.oto aA₁ aA₂ faa,
from_one_to_one CD.onto.left CD.oto cC₁ cC₂ fcc], },
end
-- exercise 12
lemma it_add_eq_lex {R : struct} (Rlin : R.fld.lin_order R.rel)
{S : struct} (Slin : S.fld.lin_order S.rel) :
let C := R.fld.prod {∅} ∪ S.fld.prod {one} in
(it R).ot_add (it S) = it ⟨C, pair_sep (λ x y, x.snd ∈ y.snd ∨ x.snd = y.snd ∧ (x.snd = ∅ ∧ x.fst.pair y.fst ∈ R.rel ∨ x.snd = one ∧ x.fst.pair y.fst ∈ S.rel)) C C, pair_sep_sub_prod⟩ :=
begin
dsimp,
set C := R.fld.prod {∅} ∪ S.fld.prod {one},
set T : struct := ⟨C, pair_sep (λ x y, x.snd ∈ y.snd ∨ x.snd = y.snd ∧ (x.snd = ∅ ∧ x.fst.pair y.fst ∈ R.rel ∨ x.snd = one ∧ x.fst.pair y.fst ∈ S.rel)) C C, pair_sep_sub_prod⟩,
have Tlin : T.fld.lin_order T.rel, refine ⟨T.is_rel, _, _, _⟩,
{ intros x y z xy yz, dsimp [T] at xy yz ⊢,
rw pair_mem_pair_sep at xy yz, rcases xy with ⟨xC, yC, xy⟩, rcases yz with ⟨-, zC, yz⟩,
rw pair_mem_pair_sep' xC zC,
simp only [C, mem_union, mem_prod, exists_prop, mem_singleton] at xC yC zC,
rcases xC with (⟨x₁, hx₁, x₂, hx₂, xxx⟩|⟨x₁, hx₁, x₂, hx₂, xxx⟩); subst xxx; subst hx₂;
rcases yC with (⟨y₁, hy₁, y₂, hy₂, yyy⟩|⟨y₁, hy₁, y₂, hy₂, yyy⟩); subst yyy; subst hy₂;
rcases xy with (xy₂|⟨xy₂, (⟨x₂, xy₁⟩|⟨x₂, xy₁⟩)⟩); simp only [fst_congr, snd_congr] at *,
{ exfalso, exact mem_empty _ xy₂, },
rotate,
{ exfalso, exact zero_ne_one x₂, },
rotate,
{ exfalso, exact zero_ne_one xy₂, },
{ exfalso, exact zero_ne_one xy₂, },
{ exfalso, exact mem_empty _ xy₂, },
{ exfalso, exact zero_ne_one xy₂.symm, },
{ exfalso, exact zero_ne_one xy₂.symm, },
{ exfalso, exact nat_not_mem_self one_nat xy₂, },
{ exfalso, exact zero_ne_one x₂.symm, },
rotate,
all_goals { rcases zC with (⟨z₁, hz₁, z₂, hz₂, zzz⟩|⟨z₁, hz₁, z₂, hz₂, zzz⟩); subst zzz; subst hz₂;
rcases yz with (yz₂|⟨yz₂, (⟨y₂, yz₁⟩|⟨y₂, yz₁⟩)⟩); simp only [fst_congr, snd_congr] at *, },
{ exfalso, exact mem_empty _ yz₂, },
{ right, exact ⟨rfl, or.inl ⟨rfl, Rlin.trans xy₁ yz₁⟩⟩, },
{ exfalso, exact zero_ne_one y₂, },
{ left, rw [one, succ, union_empty, mem_singleton], },
{ exfalso, exact zero_ne_one yz₂, },
{ exfalso, exact zero_ne_one y₂, },
{ exfalso, exact mem_empty _ yz₂, },
{ exfalso, exact zero_ne_one y₂.symm, },
{ exfalso, exact zero_ne_one yz₂.symm, },
{ exfalso, exact nat_not_mem_self one_nat yz₂, },
{ exfalso, exact zero_ne_one y₂.symm, },
{ left, rw [one, succ, union_empty, mem_singleton], },
{ exfalso, exact mem_empty _ yz₂, },
{ exfalso, exact zero_ne_one y₂.symm, },
{ exfalso, exact zero_ne_one yz₂.symm, },
{ exfalso, exact nat_not_mem_self one_nat yz₂, },
{ exfalso, exact zero_ne_one y₂.symm, },
{ right, exact ⟨rfl, or.inr ⟨rfl, Slin.trans xy₁ yz₁⟩⟩, }, },
{ dsimp [T, C], intros x xx,
simp only [pair_mem_pair_sep, mem_union, mem_prod, exists_prop, mem_singleton] at xx,
rcases xx with ⟨(⟨x₁, hx₁, x₂, hx₂, xxx⟩|⟨x₁, hx₁, x₂, hx₂, xxx⟩), -, (xx₂|⟨xx₂, (⟨x₂, xx₁⟩|⟨x₂, xx₁⟩)⟩)⟩;
subst xxx; subst hx₂; simp only [fst_congr, snd_congr] at *,
{ exfalso, exact mem_empty _ xx₂, },
{ exact Rlin.irrefl xx₁, },
{ exfalso, exact zero_ne_one x₂, },
{ exfalso, exact nat_not_mem_self one_nat xx₂, },
{ exfalso, exact zero_ne_one x₂.symm, },
{ exact Slin.irrefl xx₁, }, },
{ dsimp [T, C], intros x y xC yC xy,
rw [pair_mem_pair_sep' xC yC, pair_mem_pair_sep' yC xC],
simp only [mem_union, mem_prod, exists_prop, mem_singleton] at xC yC,
rcases xC with (⟨x₁, hx₁, x₂, hx₂, xxx⟩|⟨x₁, hx₁, x₂, hx₂, xxx⟩); subst xxx; subst hx₂;
rcases yC with (⟨y₁, hy₁, y₂, hy₂, yyy⟩|⟨y₁, hy₁, y₂, hy₂, yyy⟩); subst yyy; subst hy₂;
simp only [fst_congr, snd_congr],
{ cases Rlin.conn hx₁ hy₁ (fst_ne_of_pair_ne xy) with xy yx,
left, right, exact ⟨rfl, or.inl ⟨rfl, xy⟩⟩,
right, right, exact ⟨rfl, or.inl ⟨rfl, yx⟩⟩, },
{ left, left, rw [one, succ, union_empty, mem_singleton], },
{ right, left, rw [one, succ, union_empty, mem_singleton], },
{ cases Slin.conn hx₁ hy₁ (fst_ne_of_pair_ne xy) with xy yx,
left, right, exact ⟨rfl, or.inr ⟨rfl, xy⟩⟩,
right, right, exact ⟨rfl, or.inr ⟨rfl, yx⟩⟩, }, },
simp only [ot_eq_iff_exists_iso (ot_add_ot ⟨_, Rlin, rfl⟩ ⟨_, Slin, rfl⟩) ⟨_, Tlin, rfl⟩,
iso_ot_add_repr ⟨_, Rlin, rfl⟩ ⟨_, Slin, rfl⟩],
let R' : struct := ⟨R.fld.prod {∅}, fun_order (R.fld.prod {∅}) R.rel (pair_sep_eq (R.fld.prod {∅}) R.fld fst), pair_sep_sub_prod⟩,
let S' : struct := ⟨S.fld.prod {one}, fun_order (S.fld.prod {one}) S.rel (pair_sep_eq (S.fld.prod {one}) S.fld fst), pair_sep_sub_prod⟩,
refine ⟨_, ⟨_, fst_fun_iso, _, fst_fun_iso, disj zero_ne_one, @iso_add_of_iso _ R' _ _ S' _ _ (disj zero_ne_one)⟩, _, iso_repr_it Tlin, _⟩,
{ obtain ⟨f, fcorr, fiso⟩ := iso_symm (iso_repr_it Rlin),
let F := pair_sep_eq (f.dom.prod {∅}) (f.ran.prod {∅}) (λ z, (f.fun_value z.fst).pair ((id {∅}).fun_value z.snd)),
have Fcorr := prod_corr_of_corr fcorr id_corr,
refine ⟨F, _, _⟩; dsimp [F, R']; simp only [fcorr.onto.right.left, fcorr.onto.right.right],
exact Fcorr,
intros x y xD yD, rw ←Fcorr.onto.right.left at xD yD,
rw [pair_sep_eq_fun_value xD, pair_sep_eq_fun_value yD], dsimp,
rw Fcorr.onto.right.left at xD yD, rw [fun_order, pair_mem_pair_sep' xD yD],
simp only [mem_prod, exists_prop, mem_singleton] at xD yD,
rcases xD with ⟨a, ha, b, hb, xab⟩, rcases yD with ⟨a', ha', b', hb', yab⟩,
subst xab, subst yab, subst hb, subst hb', simp only [fst_congr, snd_congr, id_singleton_value],
have fa : f.fun_value a ∈ R.fld, rw ←fcorr.onto.right.right, apply fun_value_def'' fcorr.onto.left,
rw fcorr.onto.right.left, exact ha,
have fa' : f.fun_value a' ∈ R.fld, rw ←fcorr.onto.right.right, apply fun_value_def'' fcorr.onto.left,
rw fcorr.onto.right.left, exact ha',
have h₁ : (f.fun_value a).pair ∅ ∈ R.fld.prod {∅},
rw [pair_mem_prod, mem_singleton], exact ⟨fa, rfl⟩,
have h₂ : (f.fun_value a').pair ∅ ∈ R.fld.prod {∅},
rw [pair_mem_prod, mem_singleton], exact ⟨fa', rfl⟩,
rw [fun_order, pair_mem_pair_sep' h₁ h₂, fst_fun_value fa, fst_fun_value fa', fst_fun_value ha, fst_fun_value ha'],
exact fiso ha ha', },
{ obtain ⟨f, fcorr, fiso⟩ := iso_symm (iso_repr_it Slin),
let F := pair_sep_eq (f.dom.prod {one}) (f.ran.prod {one}) (λ z, (f.fun_value z.fst).pair ((id {one}).fun_value z.snd)),
have Fcorr := prod_corr_of_corr fcorr id_corr,
refine ⟨F, _, _⟩; dsimp [F, S']; simp only [fcorr.onto.right.left, fcorr.onto.right.right],
exact Fcorr,
intros x y xD yD, rw ←Fcorr.onto.right.left at xD yD,
rw [pair_sep_eq_fun_value xD, pair_sep_eq_fun_value yD], dsimp,
rw Fcorr.onto.right.left at xD yD, rw [fun_order, pair_mem_pair_sep' xD yD],
simp only [mem_prod, exists_prop, mem_singleton] at xD yD,
rcases xD with ⟨a, ha, b, hb, xab⟩, rcases yD with ⟨a', ha', b', hb', yab⟩,
subst xab, subst yab, subst hb, subst hb', simp only [fst_congr, snd_congr, id_singleton_value],
have fa : f.fun_value a ∈ S.fld, rw ←fcorr.onto.right.right, apply fun_value_def'' fcorr.onto.left,
rw fcorr.onto.right.left, exact ha,
have fa' : f.fun_value a' ∈ S.fld, rw ←fcorr.onto.right.right, apply fun_value_def'' fcorr.onto.left,
rw fcorr.onto.right.left, exact ha',
have h₁ : (f.fun_value a).pair one ∈ S.fld.prod {one},
rw [pair_mem_prod, mem_singleton], exact ⟨fa, rfl⟩,
have h₂ : (f.fun_value a').pair one ∈ S.fld.prod {one},
rw [pair_mem_prod, mem_singleton], exact ⟨fa', rfl⟩,
rw [fun_order, pair_mem_pair_sep' h₁ h₂, fst_fun_value fa, fst_fun_value fa', fst_fun_value ha, fst_fun_value ha'],
exact fiso ha ha', },
{ dsimp, exact disj zero_ne_one, },
dsimp [R', S', T, C], apply iso_of_eq, ext,
dsimp [add_struct], refl,
dsimp [add_struct, add_rel], refine rel_ext' _ pair_sep_sub_prod _,
refine union_subset_of_subset_of_subset (union_subset_of_subset_of_subset _ _) (prod_subset_of_subset_of_subset subset_union_left subset_union_right),
{ exact subset_trans pair_sep_sub_prod (prod_subset_of_subset_of_subset subset_union_left subset_union_left), },
{ exact subset_trans pair_sep_sub_prod (prod_subset_of_subset_of_subset subset_union_right subset_union_right), },
simp only [fun_order], intros x xC y yC,
simp only [pair_mem_pair_sep' xC yC, pair_sep_union_eq, pair_sep_union_prod_eq subset_union_left subset_union_right],
have dR : ((R.fld.prod {∅}).pair_sep_eq R.fld fst).dom = R.fld.prod {∅},
apply pair_sep_eq_dom_eq, simp only [mem_prod, exists_prop, mem_singleton],
rintros z ⟨w, hw, b, -, zwb⟩, subst zwb, rw fst_congr, exact hw,
have dS : ((S.fld.prod {one}).pair_sep_eq S.fld fst).dom = S.fld.prod {one},
apply pair_sep_eq_dom_eq, simp only [mem_prod, exists_prop, mem_singleton],
rintros z ⟨w, hw, b, -, zwb⟩, subst zwb, rw fst_congr, exact hw,
split,
rintros ((⟨xR', yR', xy⟩|⟨xS', yS', xy⟩)|xy),
{ have xf : x ∈ ((R.fld.prod {∅}).pair_sep_eq R.fld fst).dom,
rw dR, exact xR',
have yf : y ∈ ((R.fld.prod {∅}).pair_sep_eq R.fld fst).dom,
rw dR, exact yR',
simp only [mem_prod, exists_prop, mem_singleton] at xR' yR',
rcases xR' with ⟨z, zR, t, ht, xzt⟩, rcases yR' with ⟨z', zR', t', ht', yzt⟩,
subst xzt, subst yzt, subst ht, subst ht',
simp only [pair_sep_eq_fun_value xf, pair_sep_eq_fun_value yf, fst_congr, snd_congr] at *,
right, exact ⟨rfl, or.inl ⟨rfl, xy⟩⟩, },
{ have xf : x ∈ ((S.fld.prod {one}).pair_sep_eq S.fld fst).dom,
rw dS, exact xS',
have yf : y ∈ ((S.fld.prod {one}).pair_sep_eq S.fld fst).dom,
rw dS, exact yS',
simp only [mem_prod, exists_prop, mem_singleton] at xS' yS',
rcases xS' with ⟨z, zS, t, ht, xzt⟩, rcases yS' with ⟨z', zS', t', ht', yzt⟩,
subst xzt, subst yzt, subst ht, subst ht',
simp only [pair_sep_eq_fun_value xf, pair_sep_eq_fun_value yf, fst_congr, snd_congr] at *,
right, exact ⟨rfl, or.inr ⟨rfl, xy⟩⟩, },
{ simp only [pair_mem_prod, mem_prod, exists_prop, mem_singleton] at xy,
rcases xy with ⟨x', ⟨z, -, t, ht, xzt'⟩, y', ⟨z', _, t', ht', yzt'⟩, xyxy⟩,
obtain ⟨xx, yy⟩ := pair_inj xyxy,
subst xx, subst yy, subst xzt', subst yzt', subst ht, subst ht',
left, simp only [snd_congr], rw [one, succ, union_empty, mem_singleton], },
rw mem_union at xC yC,
rcases xC with (xR'|xS'); rcases yC with (yR'|yS'),
{ have xf : x ∈ ((R.fld.prod {∅}).pair_sep_eq R.fld fst).dom,
rw dR, exact xR',
have yf : y ∈ ((R.fld.prod {∅}).pair_sep_eq R.fld fst).dom,
rw dR, exact yR',
simp only [pair_sep_eq_fun_value xf, pair_sep_eq_fun_value yf, pair_mem_prod, mem_union],
rintro (tt|⟨tt, (⟨tt', zz⟩|⟨tt', zz⟩)⟩),
all_goals {
simp only [mem_prod, exists_prop, mem_singleton] at xR' yR',
rcases xR' with ⟨z, zR, t, ht, xzt⟩, rcases yR' with ⟨z', zR', t', ht', yzt⟩,
subst xzt, subst yzt, subst ht, subst ht',
simp only [fst_congr, snd_congr] at *, },
exfalso, exact mem_empty _ tt,
left, left, simp only [pair_mem_prod, mem_singleton],
have zz' := R.is_rel zz, rw pair_mem_prod at zz',
exact ⟨⟨zz'.left, rfl⟩, ⟨zz'.right, rfl⟩, zz⟩,
exfalso, exact zero_ne_one tt', },
{ have xf : x ∈ ((R.fld.prod {∅}).pair_sep_eq R.fld fst).dom,
rw dR, exact xR',
have yf : y ∈ ((S.fld.prod {one}).pair_sep_eq S.fld fst).dom,
rw dS, exact yS',
simp only [pair_sep_eq_fun_value xf, pair_sep_eq_fun_value yf, pair_mem_prod, mem_union],
rintro (tt|⟨tt, (⟨tt', zz⟩|⟨tt', zz⟩)⟩),
all_goals {
simp only [mem_prod, exists_prop, mem_singleton] at xR' yS',
rcases xR' with ⟨z, zR, t, ht, xzt⟩, rcases yS' with ⟨z', zS', t', ht', yzt⟩,
subst xzt, subst yzt, subst ht, subst ht',
simp only [fst_congr, snd_congr] at *, },
right, simp only [pair_mem_prod, mem_singleton], exact ⟨⟨zR, rfl⟩, zS', rfl⟩,
exfalso, exact zero_ne_one tt,
exfalso, exact zero_ne_one tt, },
{ have xf : x ∈ ((S.fld.prod {one}).pair_sep_eq S.fld fst).dom,
rw dS, exact xS',
have yf : y ∈ ((R.fld.prod {∅}).pair_sep_eq R.fld fst).dom,
rw dR, exact yR',
simp only [pair_sep_eq_fun_value xf, pair_sep_eq_fun_value yf, pair_mem_prod, mem_union],
rintro (tt|⟨tt, (⟨tt', zz⟩|⟨tt', zz⟩)⟩),
all_goals {
simp only [mem_prod, exists_prop, mem_singleton] at xS' yR',
rcases xS' with ⟨z, zS, t, ht, xzt⟩, rcases yR' with ⟨z', zR', t', ht', yzt⟩,
subst xzt, subst yzt, subst ht, subst ht',
simp only [fst_congr, snd_congr] at *, },
exfalso, exact mem_empty _ tt,
exfalso, exact zero_ne_one tt.symm,
exfalso, exact zero_ne_one tt.symm, },
{ have xf : x ∈ ((S.fld.prod {one}).pair_sep_eq S.fld fst).dom,
rw dS, exact xS',
have yf : y ∈ ((S.fld.prod {one}).pair_sep_eq S.fld fst).dom,
rw dS, exact yS',
simp only [pair_sep_eq_fun_value xf, pair_sep_eq_fun_value yf, pair_mem_prod, mem_union],
rintro (tt|⟨tt, (⟨tt', zz⟩|⟨tt', zz⟩)⟩),
all_goals {
simp only [mem_prod, exists_prop, mem_singleton] at xS' yS',
rcases xS' with ⟨z, zS, t, ht, xzt⟩, rcases yS' with ⟨z', zS', t', ht', yzt⟩,
subst xzt, subst yzt, subst ht, subst ht',
simp only [fst_congr, snd_congr] at *, },
exfalso, exact ord_mem_irrefl (nat_is_ord one_nat) tt,
exfalso, exact zero_ne_one tt'.symm,
left, right, simp only [pair_mem_prod, mem_singleton],
have zz' := S.is_rel zz, rw pair_mem_prod at zz',
exact ⟨⟨zz'.left, rfl⟩, ⟨zz'.right, rfl⟩, zz⟩, },
end
lemma add_struct_well_order {A : struct} (Awell : A.fld.well_order A.rel)
{B : struct} (Bwell : B.fld.well_order B.rel) (AB : A.fld ∩ B.fld = ∅) :
let R : struct := add_struct A B in R.fld.well_order R.rel :=
begin
refine ⟨add_struct_lin Awell.lin Bwell.lin AB, _⟩,
intros C CE CAB,
by_cases CAE : C ∩ A.fld ≠ ∅,
have CA : C ∩ A.fld ⊆ A.fld := inter_subset_right,
obtain ⟨m, mCA, mle⟩ := Awell.well CAE CA,
have mC : m ∈ C, rw mem_inter at mCA, exact mCA.left,
refine ⟨m, mC, _⟩, rintro ⟨d, dC, dm⟩,
have dCA : d ∈ C ∩ A.fld, rw mem_inter, refine ⟨dC, _⟩,
have dAB : d ∈ A.fld ∨ d ∈ B.fld, rw ←mem_union, exact CAB dC,
rcases dAB with (dA|dB),
exact dA,
simp only [add_struct, add_rel, mem_union, pair_mem_prod] at dm, rcases dm with ((dmA|dmB)|⟨dA, -⟩),
replace dmA := A.is_rel dmA, rw pair_mem_prod at dmA,
exact dmA.left,
exfalso, apply mem_empty m, rw [←AB, mem_inter], rw mem_inter at mCA,
replace dmB := B.is_rel dmB, rw pair_mem_prod at dmB,
exact ⟨mCA.right, dmB.right⟩,
exact dA,
refine mle ⟨_, dCA, _⟩,
simp only [add_struct, add_rel, mem_union, pair_mem_prod] at dm, rcases dm with ((dmA|dmB)|⟨-, mB⟩),
exact dmA,
exfalso, apply mem_empty m, rw [←AB, mem_inter], rw mem_inter at mCA,
replace dmB := B.is_rel dmB, rw pair_mem_prod at dmB,
exact ⟨mCA.right, dmB.right⟩,
exfalso, apply mem_empty m, rw [←AB, mem_inter], rw mem_inter at mCA, exact ⟨mCA.right, mB⟩,
push_neg at CAE,
have CB : C ⊆ B.fld, intros x xC,
have xAB : x ∈ A.fld ∨ x ∈ B.fld, rw ←mem_union, exact CAB xC,
rcases xAB with (xA|xB),
exfalso, apply mem_empty x, rw [←CAE, mem_inter], exact ⟨xC, xA⟩,
exact xB,
obtain ⟨m, mC, mle⟩ := Bwell.well CE CB,
refine ⟨m, mC, _⟩, rintro ⟨d, dC, dm⟩, apply mle, refine ⟨d, dC, _⟩,
simp only [add_struct, add_rel, mem_union, pair_mem_prod] at dm, rcases dm with ((dmA|dmB)|dmAB),
exfalso, apply mem_empty d, rw [←CAE, mem_inter],
replace dmA := A.is_rel dmA, rw pair_mem_prod at dmA,
exact ⟨dC, dmA.left⟩,
exact dmB,
exfalso, apply mem_empty d, rw [←CAE, mem_inter], exact ⟨dC, dmAB.left⟩,
end
lemma lex_well_order {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) :
let C := α.prod {∅} ∪ β.prod {one} in
let R : struct := ⟨C, pair_sep (λ x y, x.snd ∈ y.snd ∨ x.snd = y.snd ∧ (x.snd = ∅ ∧ x.fst.pair y.fst ∈ α.eps_order ∨ x.snd = one ∧ x.fst.pair y.fst ∈ β.eps_order)) C C, pair_sep_sub_prod⟩ in
R.fld.well_order R.rel :=
begin
let disj := (it α.eps_order_struct).ot_disj_repr (it β.eps_order_struct),
apply @well_order_iso (add_struct disj.fst disj.snd),
rw ←iso_type_eq_iff_iso, exact it_add_eq_lex (ordinal_well_ordered' αord).lin (ordinal_well_ordered' βord).lin,
have αot : (it α.eps_order_struct).order_type := ⟨_, (ordinal_well_ordered' αord).lin, rfl⟩,
have βot : (it β.eps_order_struct).order_type := ⟨_, (ordinal_well_ordered' βord).lin, rfl⟩,
refine add_struct_well_order (well_order_iso _ (ordinal_well_ordered' αord)) (well_order_iso _ (ordinal_well_ordered' βord)) (ot_disj_repr_disj αot βot),
refine iso_symm (iso_trans (ot_disj_repr_left_iso αot βot) (iso_symm (iso_repr_it (ordinal_well_ordered' αord).lin))),
refine iso_symm (iso_trans (ot_disj_repr_right_iso αot βot) (iso_symm (iso_repr_it (ordinal_well_ordered' βord).lin))),
end
lemma pair_mem_lex {R S : struct} {x y : Set} (hxy : x.pair y ∈
pair_sep
(λ x y,
x.snd ∈ y.snd ∨
x.snd = y.snd ∧
(x.snd = ∅ ∧ x.fst.pair y.fst ∈ R.rel ∨ x.snd = one ∧ x.fst.pair y.fst ∈ S.rel))
(R.fld.prod {∅} ∪ S.fld.prod {one})
(R.fld.prod {∅} ∪ S.fld.prod {one})) :
(∃ a, a ∈ R.fld ∧ x = pair a ∅ ∧ ∃ b, b ∈ S.fld ∧ y = pair b one) ∨
(∃ a, a ∈ R.fld ∧ x = pair a ∅ ∧ ∃ b, b ∈ R.fld ∧ y = pair b ∅ ∧ a.pair b ∈ R.rel) ∨
(∃ a, a ∈ S.fld ∧ x = pair a one ∧ ∃ b, b ∈ S.fld ∧ y = pair b one ∧ a.pair b ∈ S.rel) :=
begin
rw pair_mem_pair_sep at hxy, rcases hxy with ⟨xC, yC, xy⟩,
simp only [C, mem_union, mem_prod, exists_prop, mem_singleton] at xC yC,
rcases xC with (⟨x₁, hx₁, x₂, hx₂, xxx⟩|⟨x₁, hx₁, x₂, hx₂, xxx⟩); subst xxx; subst hx₂;
rcases yC with (⟨y₁, hy₁, y₂, hy₂, yyy⟩|⟨y₁, hy₁, y₂, hy₂, yyy⟩); subst yyy; subst hy₂;
rcases xy with (xy₂|⟨xy₂, (⟨x₂, xy₁⟩|⟨x₂, xy₁⟩)⟩); simp only [fst_congr, snd_congr] at *,
{ exfalso, exact mem_empty _ xy₂, },
{ right, left, exact ⟨_, hx₁, rfl, _, hy₁, rfl, xy₁⟩, },
{ exfalso, exact zero_ne_one x₂, },
{ left, exact ⟨_, hx₁, rfl, _, hy₁, rfl⟩, },
{ exfalso, exact zero_ne_one xy₂, },
{ exfalso, exact zero_ne_one xy₂, },
{ exfalso, exact mem_empty _ xy₂, },
{ exfalso, exact zero_ne_one xy₂.symm, },
{ exfalso, exact zero_ne_one xy₂.symm, },
{ exfalso, exact nat_not_mem_self one_nat xy₂, },
{ exfalso, exact zero_ne_one x₂.symm, },
{ right, right, exact ⟨_, hx₁, rfl, _, hy₁, rfl, xy₁⟩, },
end
def wo_type (ρ : Set) : Prop := ∃ R : struct, R.fld.well_order R.rel ∧ ρ = it R
lemma ot_of_wo_type {ρ : Set} (ρwot : ρ.wo_type) : ρ.order_type :=
exists.elim ρwot (λ R hR, ⟨_, hR.left.lin, hR.right⟩)
lemma wo_type_of_mem {ρ : Set} (ρot : ρ.order_type)
{R : struct} (Rwell : R.fld.well_order R.rel) (Rρ : struct_to_set R ∈ ρ) :
ρ.wo_type :=
begin
rcases ρot with ⟨S, Slin, Sρ⟩, subst Sρ,
refine ⟨R, Rwell, _⟩, rw struct_mem_it at Rρ,
rw iso_type_eq_iff_iso, exact Rρ.right,
end
lemma wo_type_of_iso {ρ : Set} (ρot : ρ.order_type)
{R : struct} (Rwell : R.fld.well_order R.rel) (Riso : isomorphic R ρ.ot_repr) :
ρ.wo_type :=
wo_type_of_mem ρot (well_order_iso Riso Rwell) (ot_repr_mem ρot)
lemma well_order_of_mem_wo_type {ρ : Set} (ρwo : ρ.wo_type)
{R : struct} (Rρ : struct_to_set R ∈ ρ) : R.fld.well_order R.rel :=
begin
rcases ρwo with ⟨S, Swell, ρS⟩, subst ρS,
rw struct_mem_it at Rρ, exact well_order_iso Rρ.right Swell,
end
lemma well_order_of_iso_wo_repr {ρ : Set} (ρwo : ρ.wo_type)
{R : struct} (Riso : isomorphic R ρ.ot_repr) : R.fld.well_order R.rel :=
well_order_iso (iso_symm Riso) (well_order_of_mem_wo_type ρwo (ot_repr_mem (ot_of_wo_type ρwo)))
lemma repr_wo_type_well_order {ρ : Set} (ρwo : ρ.wo_type) :
ρ.ot_repr.fld.well_order ρ.ot_repr.rel :=
well_order_of_mem_wo_type ρwo (ot_repr_mem (ot_of_wo_type ρwo))
theorem exists_unique_ord_eq_wo_type {ρ : Set} (ρwot : ρ.wo_type) : ∃! α : Set, α.is_ordinal ∧ α.ord_ot = ρ :=
begin
obtain ⟨α, αord, αiso⟩ := exists_iso_ord (repr_wo_type_well_order ρwot),
have he : α.ord_ot = ρ,
rw ot_eq_iff_exists_iso (ord_ot_is_ot αord) (ot_of_wo_type ρwot),
exact ⟨_, iso_symm (repr_ord_ot_iso αord), _, iso_refl, αiso⟩,
refine ⟨_, ⟨αord, he⟩, _⟩,
dsimp, rintros β ⟨βord, βρ⟩, rw ←he at βρ, exact ord_eq_of_ord_ot_eq βord αord βρ,
end
theorem ord_ot_wot {α : Set} (αord : α.is_ordinal) : α.ord_ot.wo_type :=
⟨_, ordinal_well_ordered' αord, rfl⟩
theorem ord_ot_ot {α : Set} (αord : α.is_ordinal) : α.ord_ot.order_type :=
ot_of_wo_type (ord_ot_wot αord)
lemma well_order_of_it_wot {R : struct} (wot : (it R).wo_type) : R.fld.well_order R.rel :=
begin
rcases wot with ⟨S, Swell, RS⟩, refine well_order_iso _ Swell,
rw ←iso_type_eq_iff_iso, exact RS.symm,
end
-- Theorem 8J
theorem wot_add_wot {ρ : Set} (ρwot : ρ.wo_type) {σ : Set} (σwot : σ.wo_type) : (ρ.ot_add σ).wo_type :=
begin
have ρot := ot_of_wo_type ρwot, have σot := ot_of_wo_type σwot,
let A := (ρ.ot_disj_repr σ).fst, let B := (ρ.ot_disj_repr σ).snd,
have AB : A.fld ∩ B.fld = ∅ := ot_disj_repr_disj ρot σot,
have Awell : A.fld.well_order A.rel := well_order_of_iso_wo_repr ρwot (ot_disj_repr_left_iso ρot σot),
have Bwell : B.fld.well_order B.rel := well_order_of_iso_wo_repr σwot (ot_disj_repr_right_iso ρot σot),
let R := add_struct A B,
have Rwell : R.fld.well_order R.rel := add_struct_well_order Awell Bwell AB,
apply wo_type_of_iso (ot_add_ot ρot σot) Rwell,
rw iso_ot_add_repr ρot σot,
exact ⟨_, ot_disj_repr_left_iso ρot σot, _, ot_disj_repr_right_iso ρot σot, AB, iso_refl⟩,
end
-- Theorem 8J
theorem wot_mul_wot {ρ : Set} (ρwot : ρ.wo_type) {σ : Set} (σwot : σ.wo_type) : (ρ.ot_mul σ).wo_type :=
begin
have ρot := ot_of_wo_type ρwot, have σot := ot_of_wo_type σwot,
let A := ρ.ot_repr, let B := σ.ot_repr,
have Awell : A.fld.well_order A.rel := well_order_of_iso_wo_repr ρwot iso_refl,
have Bwell : B.fld.well_order B.rel := well_order_of_iso_wo_repr σwot iso_refl,
let R := mul_struct A B,
have Rwell : R.fld.well_order R.rel,
refine ⟨mul_struct_lin Awell.lin Bwell.lin, _⟩,
intros C CE CAB,
obtain ⟨b, bC, ble⟩ := Bwell.well (ran_ne_of_ne CAB CE) (ran_sub_of_sub_prod CAB),
let D : Set := {a ∈ A.fld | a.pair b ∈ C},
have DE : D ≠ ∅,
apply ne_empty_of_inhabited,
rw mem_ran at bC, rcases bC with ⟨a, abC⟩,
obtain abAB := CAB abC, simp only [R, mul_struct, mem_prod] at abAB,
rcases abAB with ⟨a', aA', b', bB', abab⟩,
obtain ⟨aa, bb⟩ := pair_inj abab, subst aa, subst bb, use a,
rw mem_sep, exact ⟨aA', abC⟩,
obtain ⟨a, aD, ale⟩ := Awell.well DE sep_subset,
have abC : a.pair b ∈ C,
rw mem_sep at aD, exact aD.right,
refine ⟨_, abC, _⟩, rintro ⟨d, dC, dab⟩,
have dR : d ∈ R.fld := CAB dC,
have abR : a.pair b ∈ R.fld := CAB abC,
dsimp [R, mul_struct] at dR abR,
simp only [R, mul_struct, mul_rel, pair_mem_pair_sep' dR abR] at dab,
rw mem_prod at dR, rcases dR with ⟨x, xA, y, yB, dxy⟩, subst dxy,
simp only [fst_congr, snd_congr] at dab, rcases dab with (yb|⟨ybx, xa⟩),
apply ble, refine ⟨_, _, yb⟩, rw mem_ran, exact ⟨_, dC⟩,
subst ybx, apply ale, use x, rw mem_sep, exact ⟨⟨xA, dC⟩, xa⟩,
apply wo_type_of_iso (ot_mul_ot ρot σot) Rwell,
rw iso_ot_mul_repr ρot σot,
exact ⟨_, _, iso_refl, iso_refl, iso_refl⟩,
end
lemma mul_struct_well_order {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) :
let R : struct := mul_struct α.eps_order_struct β.eps_order_struct in
R.fld.well_order R.rel :=
begin
dsimp, refine @well_order_iso (mul_struct α.ord_ot.ot_repr β.ord_ot.ot_repr) _ _ _,
apply iso_mul_of_iso (iso_symm (iso_repr_it (ordinal_well_ordered' αord).lin)) (iso_symm (iso_repr_it (ordinal_well_ordered' βord).lin)),
apply well_order_of_it_wot,
have h := wot_mul_wot (ord_ot_wot αord) (ord_ot_wot βord),
rw ot_mul at h, exact h,
end
lemma exists_ord_eq_ot_add {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) :
∃! γ : Set, γ.is_ordinal ∧ γ.ord_ot = α.ord_ot.ot_add β.ord_ot :=
exists_unique_ord_eq_wo_type (wot_add_wot (ord_ot_wot αord) (ord_ot_wot βord))
lemma exists_ord_eq_ot_mul {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) :
∃! γ : Set, γ.is_ordinal ∧ γ.ord_ot = α.ord_ot.ot_mul β.ord_ot :=
exists_unique_ord_eq_wo_type (wot_mul_wot (ord_ot_wot αord) (ord_ot_wot βord))
noncomputable def ord_add (α β : Set) : Set :=
if αord : α.is_ordinal then
if βord : β.is_ordinal then
classical.some (exists_ord_eq_ot_add αord βord)
else ∅ else ∅
noncomputable def ord_mul (α β : Set) : Set :=
if αord : α.is_ordinal then
if βord : β.is_ordinal then
classical.some (exists_ord_eq_ot_mul αord βord)
else ∅ else ∅
lemma ord_add_ord {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) :
(α.ord_add β).is_ordinal :=
begin
simp only [ord_add, dif_pos αord, dif_pos βord],
exact (classical.some_spec (exists_ord_eq_ot_add αord βord)).left.left,
end
lemma ord_mul_ord {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) :
(α.ord_mul β).is_ordinal :=
begin
simp only [ord_mul, dif_pos αord, dif_pos βord],
exact (classical.some_spec (exists_ord_eq_ot_mul αord βord)).left.left,
end
lemma ot_add_eq_add_ot {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) :
(α.ord_add β).ord_ot = α.ord_ot.ot_add β.ord_ot :=
begin
simp only [ord_add, dif_pos αord, dif_pos βord],
exact (classical.some_spec (exists_ord_eq_ot_add αord βord)).left.right,
end
lemma ot_mul_eq_mul_ot {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) :
(α.ord_mul β).ord_ot = α.ord_ot.ot_mul β.ord_ot :=
begin
simp only [ord_mul, dif_pos αord, dif_pos βord],
exact (classical.some_spec (exists_ord_eq_ot_mul αord βord)).left.right,
end
def singleton_struct (x : Set) : struct := ⟨{x}, ∅, empty_subset⟩
lemma singleton_struct_lin {x : Set} :
let R := x.singleton_struct in R.fld.lin_order R.rel :=
begin
dsimp, refine ⟨empty_subset, _, _, _⟩,
intros a b c ab bc, exfalso, exact mem_empty _ ab,
intros a aa, exfalso, exact mem_empty _ aa,
intros a b ax bx ab, exfalso, simp only [singleton_struct, mem_singleton] at ax bx,
subst ax, subst bx, exact ab rfl,
end
lemma singleton_struct_iso {x y : Set} : isomorphic x.singleton_struct y.singleton_struct :=
begin
refine ⟨{x.pair y}, ⟨single_pair_onto, single_pair_oto⟩, _⟩,
dsimp [singleton_struct], intros a b ha hb, exact iff_of_not_of_not (mem_empty _) (mem_empty _),
end
theorem ord_add_one_eq_succ {α : Set} (αord : α.is_ordinal) : α.ord_add one = α.succ :=
begin
have αone := ord_add_ord αord one_is_ord, have αord' := succ_ord_of_ord αord,
apply ord_eq_of_ord_ot_eq αone αord',
rw [ot_eq_iff_exists_iso (ord_ot_ot αone) (ord_ot_ot αord'), ot_add_eq_add_ot αord one_is_ord],
refine ⟨add_struct α.eps_order_struct α.singleton_struct, _, α.succ.eps_order_struct, iso_symm (repr_ord_ot_iso αord'), iso_of_eq _⟩,
rw iso_ot_add_repr (ord_ot_ot αord) (ord_ot_ot one_is_ord),
refine ⟨_, iso_symm (repr_ord_ot_iso αord), _, iso_trans (@singleton_struct_iso α ∅) (iso_trans (iso_of_eq _) (iso_symm (repr_ord_ot_iso one_is_ord))), _, iso_refl⟩,
rw [one, succ, union_empty], ext; dsimp [singleton_struct, eps_order_struct],
refl,
dsimp [eps_order], symmetry, rw eq_empty, intros z hz,
simp only [mem_pair_sep, exists_prop, mem_singleton] at hz,
rcases hz with ⟨a, az, b, bz, zab, ab⟩, subst az, subst bz,
exact mem_empty _ ab,
dsimp [eps_order_struct, singleton_struct], rw eq_empty,
intros β hβ, rw [mem_inter, mem_singleton] at hβ, rcases hβ with ⟨βα, βα'⟩, subst βα',
exact ord_mem_irrefl αord βα,
ext,
dsimp [add_struct, singleton_struct, succ], rw union_comm,
dsimp [add_struct, add_rel, eps_order, singleton_struct],
have αsub : {α} ⊆ α.succ,
intros β βα, rw mem_singleton at βα, subst βα, rw [succ, mem_union, mem_singleton], left, refl,
rw union_empty, apply rel_ext' (union_subset_of_subset_of_subset (subset_trans pair_sep_sub_prod (prod_subset_of_subset_of_subset self_sub_succ self_sub_succ)) (prod_subset_of_subset_of_subset self_sub_succ αsub)) pair_sep_sub_prod,
intros x xα y yα, rw [succ, mem_union, mem_singleton] at xα yα, rw mem_union,
cases xα,
subst xα, cases yα,
subst yα, split,
rw [pair_mem_pair_sep, pair_mem_prod], rintros (⟨yy, -, -⟩|⟨yy, -⟩); exfalso; exact ord_mem_irrefl αord yy,
rw [pair_mem_pair_sep' self_mem_succ self_mem_succ, pair_mem_pair_sep, pair_mem_prod, mem_singleton],
finish,
split,
rw [pair_mem_pair_sep, pair_mem_prod], rintros (⟨yy, -, -⟩|⟨yy, -⟩); exfalso; exact ord_mem_irrefl αord yy,
rw [pair_mem_pair_sep' self_mem_succ (ord_mem_trans αord' yα self_mem_succ), pair_mem_pair_sep, pair_mem_prod, mem_singleton],
intro xy, exfalso, exact ord_mem_irrefl αord (ord_mem_trans αord xy yα),
cases yα,
subst yα, split,
rw [pair_mem_pair_sep, pair_mem_prod, mem_singleton], rintros (⟨-, yy, -⟩|⟨-, yy⟩),
exfalso, exact ord_mem_irrefl αord yy,
rw pair_mem_pair_sep' (ord_mem_trans αord' xα self_mem_succ) self_mem_succ, exact xα,
rw [pair_mem_pair_sep' (ord_mem_trans αord' xα self_mem_succ) self_mem_succ, pair_mem_pair_sep, pair_mem_prod, mem_singleton],
finish,
rw [pair_mem_pair_sep' xα yα, pair_mem_prod, mem_singleton,
pair_mem_pair_sep' (ord_mem_trans αord' xα self_mem_succ) (ord_mem_trans αord' yα self_mem_succ)],
split; finish,
end
-- Theorem 8K
theorem ord_add_assoc {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) {γ : Set} (γord : γ.is_ordinal) :
(α.ord_add β).ord_add γ = α.ord_add (β.ord_add γ) :=
begin
have αβord := ord_add_ord αord βord, have βγord := ord_add_ord βord γord,
apply ord_eq_of_ord_ot_eq (ord_add_ord αβord γord) (ord_add_ord αord βγord),
rw [ot_add_eq_add_ot αβord γord, ot_add_eq_add_ot αord βγord, ot_add_eq_add_ot αord βord, ot_add_eq_add_ot βord γord],
exact ot_add_assoc (ord_ot_is_ot αord) (ord_ot_is_ot βord) (ord_ot_is_ot γord),
end
theorem ord_mul_assoc {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) {γ : Set} (γord : γ.is_ordinal) :
(α.ord_mul β).ord_mul γ = α.ord_mul (β.ord_mul γ) :=
begin
have αβord := ord_mul_ord αord βord, have βγord := ord_mul_ord βord γord,
apply ord_eq_of_ord_ot_eq (ord_mul_ord αβord γord) (ord_mul_ord αord βγord),
rw [ot_mul_eq_mul_ot αβord γord, ot_mul_eq_mul_ot αord βγord, ot_mul_eq_mul_ot αord βord, ot_mul_eq_mul_ot βord γord],
exact ot_mul_assoc (ord_ot_is_ot αord) (ord_ot_is_ot βord) (ord_ot_is_ot γord),
end
theorem ord_add_mul {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) {γ : Set} (γord : γ.is_ordinal) :
α.ord_mul (β.ord_add γ) = (α.ord_mul β).ord_add (α.ord_mul γ) :=
begin
have βγord := ord_add_ord βord γord, have αβord := ord_mul_ord αord βord, have αγord := ord_mul_ord αord γord,
apply ord_eq_of_ord_ot_eq (ord_mul_ord αord βγord) (ord_add_ord αβord αγord),
rw [ot_mul_eq_mul_ot αord βγord, ot_add_eq_add_ot βord γord, ot_add_eq_add_ot αβord αγord,
ot_mul_eq_mul_ot αord βord, ot_mul_eq_mul_ot αord γord],
exact ot_add_mul (ord_ot_is_ot αord) (ord_ot_is_ot βord) (ord_ot_is_ot γord),
end
-- Theorem 8L (A1)
theorem ord_add_zero {α : Set} (αord : α.is_ordinal) : α.ord_add ∅ = α :=
begin
apply ord_eq_of_ord_ot_eq (ord_add_ord αord zero_is_ord) αord,
rw [ot_add_eq_add_ot αord zero_is_ord],
exact ot_add_zero (ord_ot_is_ot αord),
end
theorem ord_zero_add {α : Set} (αord : α.is_ordinal) : ord_add ∅ α = α :=
begin
apply ord_eq_of_ord_ot_eq (ord_add_ord zero_is_ord αord) αord,
rw [ot_add_eq_add_ot zero_is_ord αord],
exact ot_zero_add (ord_ot_is_ot αord),
end
theorem ord_mul_one {α : Set} (αord : α.is_ordinal) : α.ord_mul one = α :=
begin
apply ord_eq_of_ord_ot_eq (ord_mul_ord αord one_is_ord) αord,
rw [ot_mul_eq_mul_ot αord one_is_ord],
exact ot_mul_one (ord_ot_is_ot αord),
end
theorem ord_one_mul {α : Set} (αord : α.is_ordinal) : ord_mul one α = α :=
begin
apply ord_eq_of_ord_ot_eq (ord_mul_ord one_is_ord αord) αord,
rw [ot_mul_eq_mul_ot one_is_ord αord],
exact ot_one_mul (ord_ot_is_ot αord),
end
-- Theorem 8L (M1)
theorem ord_mul_zero {α : Set} (αord : α.is_ordinal) : α.ord_mul ∅ = ∅ :=
begin
apply ord_eq_of_ord_ot_eq (ord_mul_ord αord zero_is_ord) zero_is_ord,
rw [ot_mul_eq_mul_ot αord zero_is_ord],
exact ot_mul_zero (ord_ot_is_ot αord),
end
theorem ord_zero_mul {α : Set} (αord : α.is_ordinal) : ord_mul ∅ α = ∅ :=
begin
apply ord_eq_of_ord_ot_eq (ord_mul_ord zero_is_ord αord) zero_is_ord,
rw [ot_mul_eq_mul_ot zero_is_ord αord],
exact ot_zero_mul (ord_ot_is_ot αord),
end
-- Theorem 8L (A2)
theorem ord_add_succ {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) :
α.ord_add β.succ = (α.ord_add β).succ :=
by rw [←ord_add_one_eq_succ βord, ←ord_add_assoc αord βord one_is_ord, ord_add_one_eq_succ (ord_add_ord αord βord)]
-- Theorem 8L (M2)
theorem ord_mul_succ {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) :
α.ord_mul β.succ = (α.ord_mul β).ord_add α :=
by rw [←ord_add_one_eq_succ βord, ord_add_mul αord βord one_is_ord, ord_mul_one αord]
structure end_extension (R S : struct) : Prop :=
(fld : R.fld ⊆ S.fld)
(rel : R.rel ⊆ S.rel)
(ext : ∀ {r : Set}, r ∈ R.fld → ∀ {s : Set}, s ∈ S.fld \ R.fld → r.pair s ∈ S.rel)
lemma L8M_lemma {C : Set} (hC : ∀ ⦃R : Set⦄, R ∈ C → ∃ S : struct, S.fld.well_order S.rel ∧ R = struct_to_set S) :
let A := (repl_img fst C).Union in
(repl_img snd C).Union ⊆ A.prod A :=
begin
dsimp, intro z, simp only [mem_Union, exists_prop, mem_repl_img],
rintro ⟨R', ⟨S, RC, RS⟩, zR⟩, subst RS, rcases hC RC with ⟨R, Rwell, RR⟩, subst RR,
rw set_snd_eq_rel at zR,
have zAA := R.is_rel zR, rw mem_prod at zAA, rcases zAA with ⟨a, aA, b, bA, zab⟩, subst zab,
simp only [pair_mem_prod, mem_Union, exists_prop, mem_repl_img],
refine ⟨⟨_, ⟨_, RC, _⟩, aA⟩, _, ⟨_, RC, _⟩, bA⟩; rw set_fst_eq_fld,
end
lemma seg_eq_of_ext {R : struct} (Rlin : R.fld.lin_order R.rel) {S : struct} (Slin : S.fld.lin_order S.rel)
(RS : end_extension R S) {a : Set} (aA : a ∈ R.fld) : S.rel.seg a = R.rel.seg a :=
begin
apply ext, simp only [mem_seg], intro x, split,
intro xa,
have xA : x ∈ R.fld, apply classical.by_contradiction, intro xA,
refine not_lt_and_gt_part (part_order_of_lin_order Slin) ⟨xa, RS.ext aA _⟩,
rw mem_diff, refine ⟨_, xA⟩, replace xa := Slin.rel xa, rw pair_mem_prod at xa, exact xa.left,
have xna : x ≠ a, intro xea, subst xea, exact Slin.irrefl xa,
cases Rlin.conn xA aA xna with xRa aRx,
exact xRa,
exfalso, exact not_lt_and_gt_part (part_order_of_lin_order Slin) ⟨xa, RS.rel aRx⟩,
apply RS.rel,
end
lemma L8M {C : Set.{u}} (hC : ∀ ⦃R : Set⦄, R ∈ C → ∃ S : struct, S.fld.well_order S.rel ∧ R = struct_to_set S)
(hext : ∀ {R : struct}, struct_to_set R ∈ C → ∀ {S : struct}, struct_to_set S ∈ C → end_extension R S ∨ end_extension S R) :
let W : struct := ⟨(repl_img fst C).Union, (repl_img snd C).Union, L8M_lemma hC⟩
in W.fld.well_order W.rel ∧ eps_img W = (repl_img (eps_img ∘ set_to_struct) C).Union :=
begin
have memC : ∀ {R : struct}, struct_to_set R ∈ C → R.fld.well_order R.rel,
intros R RC, rcases hC RC with ⟨S, Swell, RS⟩, rw struct_to_set_oto RS, exact Swell,
dsimp, set W : struct := ⟨(repl_img fst C).Union, (repl_img snd C).Union, L8M_lemma hC⟩,
let E := (repl_img (eps_img_fun ∘ set_to_struct) C).Union,
have ord : (repl_img (eps_img ∘ set_to_struct) C).Union.is_ordinal,
apply Union_ords_is_ord, apply of_repl_img, intros R RC, rw function.comp_app,
rcases hC RC with ⟨S, Swell, RS⟩, subst RS, rw struct_set_struct_eq_self,
exact eps_img_ord Swell,
have Eiso : E.isomorphism W (repl_img (eps_img ∘ set_to_struct) C).Union.eps_order_struct,
have Xch : (repl_img (eps_img_fun ∘ set_to_struct) C).is_chain,
{ intros F hF G hG, simp only [mem_repl_img, function.comp_app] at hF hG,
rcases hF with ⟨R', RC, FR⟩, subst FR, rcases hG with ⟨S', SC, GS⟩, subst GS,
obtain ⟨R, Rwell, RR⟩ := hC RC, subst RR, obtain ⟨S, Swell, SS⟩ := hC SC, subst SS,
simp only [struct_set_struct_eq_self], cases hext RC SC with RS RS,
left, rotate, rename [R → S, Rwell → Swell, RC → SC, S → R, Swell → Rwell, SC → RC], right, rotate,
all_goals {
have h : eps_img_fun R = (eps_img_fun S).restrict R.fld,
apply fun_ext (eps_img_fun_onto Rwell).left (restrict_is_function (eps_img_fun_onto Swell).left),
rw (eps_img_fun_onto Rwell).right.left, symmetry, apply restrict_dom,
rw (eps_img_fun_onto Swell).right.left, exact RS.fld,
let X := {x ∈ R.fld | (eps_img_fun R).fun_value x = ((eps_img_fun S).restrict R.fld).fun_value x},
suffices XA : X = R.fld,
intros x xA, rw [(eps_img_fun_onto Rwell).right.left, ←XA, mem_sep] at xA,
exact xA.right,
apply transfinite_ind Rwell sep_subset, intros a aA ind,
rw mem_sep, refine ⟨aA, _⟩, rw eps_img_fun_value_img Rwell aA,
have sub : R.fld ⊆ (eps_img_fun S).dom, rw (eps_img_fun_onto Swell).right.left, exact RS.fld,
rw [restrict_fun_value (eps_img_fun_onto Swell).left sub aA,
eps_img_fun_value_img Swell (RS.fld aA)],
rw seg_eq_of_ext Rwell.lin Swell.lin RS aA,
refine img_fun_eq (eps_img_fun_onto Rwell).left _ (eps_img_fun_onto Swell).left _ _,
rw (eps_img_fun_onto Rwell).right.left, exact seg_sub R.is_rel aA,
rw (eps_img_fun_onto Swell).right.left, exact subset_trans (seg_sub R.is_rel aA) RS.fld,
intros x xa, specialize ind xa, rw mem_sep at ind, rcases ind with ⟨xA, fgx⟩,
rw restrict_fun_value (eps_img_fun_onto Swell).left sub xA at fgx, exact fgx,
rw h, exact restrict_subset, }, },
have Xfun : ∀ ⦃f : Set⦄, f ∈ (repl_img (eps_img_fun ∘ set_to_struct) C) → f.is_function,
simp only [mem_repl_img, function.comp_app], rintros f ⟨R', RC, fR⟩, subst fR,
obtain ⟨R, Rwell, RR⟩ := hC RC, subst RR,
rw struct_set_struct_eq_self, exact (eps_img_fun_onto Rwell).left,
have Xoto : ∀ ⦃f : Set⦄, f ∈ (repl_img (eps_img_fun ∘ set_to_struct) C) → f.one_to_one,
simp only [mem_repl_img, function.comp_app], rintros f ⟨R', RC, fR⟩, subst fR,
obtain ⟨R, Rwell, RR⟩ := hC RC, subst RR,
rw struct_set_struct_eq_self, exact eps_img_fun_oto Rwell,
have Eonto := Union_chain_onto Xch Xfun, have Eoto := Union_chain_oto Xch Xoto,
have dom : (repl_img dom (repl_img (eps_img_fun ∘ set_to_struct) C)).Union = W.fld,
dsimp [W], rw repl_img_comp, congr' 1, apply repl_img_ext,
intros R' RC, simp only [function.comp_app],
obtain ⟨R, Rwell, RR⟩ := hC RC, subst RR,
rw [set_fst_eq_fld, struct_set_struct_eq_self, (eps_img_fun_onto (memC RC)).right.left],
have ran : (repl_img ran (repl_img (eps_img_fun ∘ set_to_struct) C)).Union = (repl_img (eps_img ∘ set_to_struct) C).Union.eps_order_struct.fld,
rw [eps_order_struct_fld, repl_img_comp], refl,
have hE : ∀ {R : struct}, struct_to_set R ∈ C → ∀ {x : Set}, x ∈ R.fld → E.fun_value x = (eps_img_fun R).fun_value x,
intros R RC x xR, apply Union_chain_fun_value Xch Xfun,
rw mem_repl_img, refine ⟨_, RC, _⟩, rw [function.comp_app, struct_set_struct_eq_self],
rw (eps_img_fun_onto (memC RC)).right.left, exact xR,
rw [dom, ran] at Eonto, apply iso_of_corr' ⟨Eonto, Eoto⟩,
dsimp [W], intros x y,
calc
x.pair y ∈ (repl_img snd C).Union ↔ ∃ S : struct.{u}, struct_to_set S ∈ C ∧ x.pair y ∈ S.rel : begin
simp only [mem_Union, exists_prop, mem_repl_img, ←exists_and_distrib_right, and_assoc],
rw exists_comm, simp only [exists_and_distrib_left, exists_eq_left, and_comm], split,
rintro ⟨R, RC, xyR⟩, obtain ⟨S, Swell, RS⟩ := hC RC, subst RS, rw set_snd_eq_rel at xyR,
exact ⟨_, RC, xyR⟩,
rintro ⟨S, SC, xyS⟩, refine ⟨_, SC, _⟩, rw set_snd_eq_rel, exact xyS,
end
... ↔ ∃ S : struct, struct_to_set S ∈ C ∧ x ∈ S.fld ∧ y ∈ S.fld ∧ ((eps_img_fun S).fun_value x).pair ((eps_img_fun S).fun_value y) ∈ (eps_img S).eps_order :
begin
apply exists_congr, intro R, apply and_congr_right, intro RC,
apply iso_iso (eps_img_iso (memC RC)),
end
... ↔ x ∈ (repl_img fst C).Union ∧ y ∈ (repl_img fst C).Union ∧ (E.fun_value x).pair (E.fun_value y) ∈ (repl_img (eps_img ∘ set_to_struct) C).Union.eps_order :
begin
split,
rintro ⟨R, RC, xR, yR, fxy⟩,
have xD : x ∈ (repl_img fst C).Union, simp only [mem_Union, exists_prop, mem_repl_img],
refine ⟨_, ⟨_, RC, _⟩, xR⟩, rw set_fst_eq_fld,
have yD : y ∈ (repl_img fst C).Union, simp only [mem_Union, exists_prop, mem_repl_img],
refine ⟨_, ⟨_, RC, _⟩, yR⟩, rw set_fst_eq_fld,
have Ex : E.fun_value x ∈ E.ran, apply fun_value_def'' Eonto.left, rw Eonto.right.left, exact xD,
have Ey : E.fun_value y ∈ E.ran, apply fun_value_def'' Eonto.left, rw Eonto.right.left, exact yD,
have ex : (eps_img_fun R).fun_value x ∈ (eps_img_fun R).ran, apply fun_value_def'' (eps_img_fun_onto (memC RC)).left,
rw (eps_img_fun_onto (memC RC)).right.left, exact xR,
have ey : (eps_img_fun R).fun_value y ∈ (eps_img_fun R).ran, apply fun_value_def'' (eps_img_fun_onto (memC RC)).left,
rw (eps_img_fun_onto (memC RC)).right.left, exact yR,
rw Eonto.right.right at Ex Ey, dsimp at Ex Ey, rw (eps_img_fun_onto (memC RC)).right.right at ex ey,
rw pair_mem_eps_order' ex ey at fxy, rw [pair_mem_eps_order' Ex Ey, hE RC xR, hE RC yR],
exact ⟨xD, yD, fxy⟩,
rintro ⟨xD, yD, Exy⟩,
have Ex : E.fun_value x ∈ E.ran, apply fun_value_def'' Eonto.left, rw Eonto.right.left, exact xD,
have Ey : E.fun_value y ∈ E.ran, apply fun_value_def'' Eonto.left, rw Eonto.right.left, exact yD,
rw Eonto.right.right at Ex Ey, dsimp at Ex Ey, rw pair_mem_eps_order' Ex Ey at Exy,
simp only [mem_Union, exists_prop, mem_repl_img] at xD yD,
rcases xD with ⟨A, ⟨R', RC, AR⟩, xA⟩, subst AR, rcases yD with ⟨B, ⟨S', SC, BS⟩, yB⟩, subst BS,
obtain ⟨R, Rwell, RR⟩ := hC RC, obtain ⟨S, Swell, SS⟩ := hC SC, subst RR, subst SS,
rw set_fst_eq_fld at xA yB,
cases hext RC SC with RS SR,
have xB := RS.fld xA,
refine ⟨S, SC, xB, yB, _⟩,
rw [pair_mem_eps_order' (fun_value_mem_eps_img (memC SC) xB) (fun_value_mem_eps_img (memC SC) yB),
←hE SC xB, ←hE SC yB],
exact Exy,
have yA := SR.fld yB,
refine ⟨R, RC, xA, yA, _⟩,
rw [pair_mem_eps_order' (fun_value_mem_eps_img (memC RC) xA) (fun_value_mem_eps_img (memC RC) yA),
←hE RC xA, ←hE RC yA],
exact Exy,
end,
refine ⟨_, eps_img_eq_of_iso_ord ord ⟨_, Eiso⟩⟩,
change W.fld.well_order W.rel, exact well_order_iso (iso_symm ⟨_, Eiso⟩) (ordinal_well_ordered' ord),
end
lemma prod_union_eq_Union_prod {X Y : Set} : Y.Union.prod X = (repl_img (λ z, z.prod X) Y).Union :=
begin
have h : ∀ ⦃z : Set⦄, z ∈ repl_img (λ z, z.prod X) Y → z.is_rel,
intros z hz, rw mem_repl_img at hz, rcases hz with ⟨x, xy, zXx⟩, subst zXx, exact prod_is_rel,
apply rel_ext prod_is_rel (Union_is_rel h),
simp only [pair_mem_prod, mem_Union, exists_prop, mem_repl_img], intros x y, split,
rintro ⟨⟨z, zY, xz⟩, yX⟩, refine ⟨_, ⟨_, zY, rfl⟩, _⟩, rw pair_mem_prod,
exact ⟨xz, yX⟩,
rintro ⟨P, ⟨z, zY, PzX⟩, xyP⟩, subst PzX, rw pair_mem_prod at xyP,
exact ⟨⟨_, zY, xyP.left⟩, xyP.right⟩,
end
lemma union_Union_repl_img {f : Set → Set} {X Y : Set} (Yin : Y.inhab) : X ∪ (repl_img f Y).Union = (repl_img (λ z, X ∪ f z) Y).Union :=
begin
apply ext, simp only [mem_union, mem_Union, exists_prop, mem_repl_img], intro z, split,
rintro (zX|⟨fx, ⟨y, yY, fxfx⟩, zfx⟩),
rcases Yin with ⟨c, cY⟩, refine ⟨_, ⟨_, cY, rfl⟩, _⟩, rw mem_union, left, exact zX,
subst fxfx, refine ⟨_, ⟨_, yY, rfl⟩, _⟩, rw mem_union, right, exact zfx,
rintro ⟨Xfx, ⟨y, yY, XfxXfx⟩, zXfx⟩, subst XfxXfx, rw mem_union at zXfx, rcases zXfx with (zX|zfy),
left, exact zX,
right, exact ⟨_, ⟨_, yY, rfl⟩, zfy⟩,
end
lemma pair_mem_pair_sep_of {q : Set → Set → Prop} {C D x y : Set} (xy : x.pair y ∈ pair_sep q C D)
{A : Set} (xA : x ∈ A) {B : Set} (yB : y ∈ B) : x.pair y ∈ pair_sep q A B :=
begin
rw pair_mem_pair_sep at xy ⊢, rcases xy with ⟨-, -, qxy⟩, exact ⟨xA, yB, qxy⟩,
end
noncomputable def ord_max (α β : Set) : Set :=
if α ∈ β then β else α
lemma ord_max_of_lt {α β : Set} (αβ : α ∈ β) : α.ord_max β = β :=
by rw [ord_max, if_pos αβ]
lemma ord_max_of_not_lt {α β : Set} (αβ : α ∉ β) : α.ord_max β = α :=
by rw [ord_max, if_neg αβ]
lemma ord_max_prop {p : Set → Prop} {α : Set} (pα : p α) {β : Set} (pβ : p β) : p (α.ord_max β) := ite_prop pβ pα
lemma ord_max_ord {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) : (α.ord_max β).is_ordinal :=
ord_max_prop αord βord
lemma ord_max_le_left {α β : Set} : α ≤ α.ord_max β :=
begin
by_cases αβ : α ∈ β,
rw ord_max_of_lt αβ, left, exact αβ,
rw ord_max_of_not_lt αβ, right, refl,
end
lemma ord_max_le_right {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) : β ≤ α.ord_max β :=
begin
by_cases αβ : α ∈ β,
rw ord_max_of_lt αβ, right, refl,
rw ord_max_of_not_lt αβ, rw ←ord_not_lt_iff_le αord βord, exact αβ,
end
lemma ord_max_sub_left {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) : α ⊆ α.ord_max β :=
begin
rw ←ord_le_iff_sub αord (ord_max_ord αord βord), exact ord_max_le_left,
end
lemma ord_max_sub_right {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) : β ⊆ α.ord_max β :=
begin
rw ←ord_le_iff_sub βord (ord_max_ord αord βord), exact ord_max_le_right αord βord,
end
-- Theorem 8L (A3)
theorem ord_add_limit {α : Set} (αord : α.is_ordinal) {γ : Set} (γord : γ.limit_ord) :
α.ord_add γ = (repl_img α.ord_add γ).Union :=
begin
have h : ∀ ⦃β : Set⦄, β ∈ repl_img α.ord_add γ → β.is_ordinal,
intros β hβ, rw mem_repl_img at hβ, rcases hβ with ⟨δ, δγ, βαδ⟩, subst βαδ,
exact ord_add_ord αord (ord_of_mem_ord γord.ord δγ),
apply ord_eq_of_ord_ot_eq (ord_add_ord αord γord.ord) (Union_ords_is_ord h),
rw ot_add_eq_add_ot αord γord.ord, dsimp [ord_ot],
have h₂ := it_add_eq_lex (ordinal_well_ordered' αord).lin (ordinal_well_ordered' γord.ord).lin,
dsimp at h₂, rw h₂, clear h₂,
rw [limit_ord_eq_Union γord, prod_union_eq_Union_prod, union_Union_repl_img (inhabited_of_ne_empty γord.ne)],
set C := (repl_img (λ β, let D := α.prod {∅} ∪ β.prod {one} in struct_to_set
⟨D, pair_sep (λ x y, x.snd ∈ y.snd ∨ x.snd = y.snd ∧ (x.snd = ∅ ∧ x.fst.pair y.fst ∈ α.eps_order ∨ x.snd = one ∧ x.fst.pair y.fst ∈ β.eps_order)) D D, pair_sep_sub_prod⟩) γ),
have hC: ∀ ⦃R : Set⦄, R ∈ C → ∃ S : struct, S.fld.well_order S.rel ∧ R = struct_to_set S,
intros R RC, simp only [C, mem_repl_img] at RC, rcases RC with ⟨β, βγ, Re⟩, subst Re, refine ⟨_, _, rfl⟩,
dsimp, exact lex_well_order αord (ord_of_mem_ord γord.ord βγ),
let W : struct := ⟨(repl_img fst C).Union, (repl_img snd C).Union, L8M_lemma hC⟩,
have h₃ : (⟨(repl_img (λ β, α.prod {∅} ∪ β.prod {one}) γ).Union,
pair_sep
(λ x y,
x.snd ∈ y.snd ∨
x.snd = y.snd ∧
(x.snd = ∅ ∧ x.fst.pair y.fst ∈ α.eps_order ∨
x.snd = one ∧ x.fst.pair y.fst ∈ γ.Union.eps_order))
(repl_img (λ β, α.prod {∅} ∪ β.prod {one}) γ).Union
(repl_img (λ β, α.prod {∅} ∪ β.prod {one}) γ).Union,
pair_sep_sub_prod⟩ : struct) = W,
have de : (repl_img (λ (β : Set), α.prod {∅} ∪ β.prod {one}) γ).Union = (repl_img fst C).Union,
congr' 1, apply ext, simp only [mem_repl_img], intro A, split,
rintro ⟨β, βγ, Ae⟩, subst Ae, refine ⟨_, ⟨_, βγ, rfl⟩, _⟩, rw set_fst_eq_fld,
rintro ⟨S, ⟨β, βγ, Se⟩, AS⟩, subst Se, rw set_fst_eq_fld at AS, exact ⟨_, βγ, AS⟩,
ext,
dsimp, exact de,
dsimp, apply @rel_ext' (repl_img fst C).Union (repl_img fst C).Union,
rw de, exact pair_sep_sub_prod,
refine rel_sub (Union_is_rel _) _,
simp only [mem_repl_img, C], rintros R ⟨S, ⟨β, βγ, Se⟩, RS⟩, subst Se, subst RS,
rw set_snd_eq_rel, dsimp, exact pair_sep_is_rel,
simp only [mem_Union, exists_prop, pair_mem_prod, mem_repl_img],
rintros x y ⟨R, ⟨S, ⟨β, βγ, Se⟩, RS⟩, xyR⟩, subst Se, subst RS,
rw set_snd_eq_rel at xyR, dsimp at xyR,
have xy := pair_sep_sub_prod xyR,
rw pair_mem_prod at xy,
refine ⟨⟨_, ⟨_, ⟨_, βγ, rfl⟩, rfl⟩, _⟩, _, ⟨_, ⟨_, βγ, rfl⟩, rfl⟩, _⟩; rw set_fst_eq_fld; dsimp,
exact xy.left,
exact xy.right,
intros x hx y hy, split,
simp only [mem_Union, exists_prop, mem_repl_img] at hx hy,
rcases hx with ⟨A, ⟨S, ⟨β, βγ, Se⟩, AS⟩, xA⟩, subst Se, subst AS, rw set_fst_eq_fld at xA, dsimp at xA,
rcases hy with ⟨A', ⟨S', ⟨β', βγ', Se'⟩, AS'⟩, yA⟩, subst Se', subst AS', rw set_fst_eq_fld at yA, dsimp at yA,
intro xy,
simp only [mem_Union, exists_prop, mem_repl_img],
refine ⟨_, ⟨_, ⟨_, @ord_max_prop (λ δ, δ ∈ γ) _ βγ _ βγ', rfl⟩, rfl⟩, _⟩,
rw set_snd_eq_rel, dsimp,
have βord := ord_of_mem_ord γord.ord βγ, have βord' := ord_of_mem_ord γord.ord βγ',
have sub : α.prod {∅} ∪ β.prod {one} ⊆ α.prod {∅} ∪ (β.ord_max β').prod {one} :=
union_subset_of_subset_of_subset subset_union_left (subset_union_of_subset_right (prod_subset_of_subset_of_subset (ord_max_sub_left βord βord') subset_self)),
have sub' : α.prod {∅} ∪ β'.prod {one} ⊆ α.prod {∅} ∪ (β.ord_max β').prod {one} :=
union_subset_of_subset_of_subset subset_union_left (subset_union_of_subset_right (prod_subset_of_subset_of_subset (ord_max_sub_right βord βord') subset_self)),
have xA'' : x ∈ α.prod {∅} ∪ (β.ord_max β').prod {one} := sub xA,
have yA'' : y ∈ α.prod {∅} ∪ (β.ord_max β').prod {one} := sub' yA,
have ord_max_sub : β.ord_max β' ⊆ γ, rw ←ord_le_iff_sub (ord_max_ord βord βord') γord.ord, left,
exact @ord_max_prop _ β βγ β' βγ',
have xA' : x ∈ α.prod {∅} ∪ γ.prod {one} :=
union_subset_of_subset_of_subset subset_union_left (subset_union_of_subset_right (prod_subset_of_subset_of_subset ord_max_sub subset_self)) xA'',
have yA' : y ∈ α.prod {∅} ∪ γ.prod {one} :=
union_subset_of_subset_of_subset subset_union_left (subset_union_of_subset_right (prod_subset_of_subset_of_subset ord_max_sub subset_self)) yA'',
rw ←limit_ord_eq_Union γord at xy,
rw pair_mem_pair_sep' xA'' yA'',
obtain (⟨a, aα, xaz, b, bγ, ybz⟩|(⟨a, aα, xaz, b, bα, ybz, ab⟩|⟨a, aγ, xaz, b, bγ, ybz, ab⟩)) :=
@pair_mem_lex α.eps_order_struct γ.eps_order_struct _ _ (pair_mem_pair_sep_of xy xA' yA');
subst xaz; subst ybz; simp only [fst_congr, snd_congr],
left, rw [one, succ, union_empty, mem_singleton],
right, exact ⟨rfl, or.inl ⟨rfl, ab⟩⟩,
right, refine ⟨rfl, or.inr ⟨rfl, _⟩⟩,
rw eps_order_struct_fld at aγ bγ,
rw [eps_order_struct_rel, pair_mem_eps_order' aγ bγ] at ab,
simp only [mem_union, pair_mem_prod] at xA yA,
rcases xA with (⟨-, h⟩|⟨aβ, -⟩),
exfalso, rw mem_singleton at h, exact zero_ne_one h.symm,
rcases yA with (⟨-, h⟩|⟨bβ, -⟩),
exfalso, rw mem_singleton at h, exact zero_ne_one h.symm,
rw pair_mem_eps_order' (ord_lt_of_lt_of_le (ord_max_ord βord βord') aβ ord_max_le_left) (ord_lt_of_lt_of_le (ord_max_ord βord βord') bβ (ord_max_le_right βord βord')),
exact ab,
intro xy, rw [de, pair_mem_pair_sep' hx hy],
simp only [mem_Union, exists_prop, mem_repl_img] at xy,
rcases xy with ⟨R, ⟨S, ⟨β, βγ, Se⟩, RS⟩, xyR⟩, subst Se, subst RS,
simp only [set_snd_eq_rel, pair_mem_pair_sep] at xyR, rw ←limit_ord_eq_Union γord,
rcases xyR with ⟨-, -, (xy|⟨xy, (⟨x₂, xy₁⟩|⟨x₂, xy₁⟩)⟩)⟩,
left, exact xy,
right, exact ⟨xy, or.inl ⟨x₂, xy₁⟩⟩,
right, refine ⟨xy, or.inr ⟨x₂, eps_order_sub (ord_of_mem_ord γord.ord βγ) γord.ord (or.inl βγ) xy₁⟩⟩,
rw h₃, clear h₃,
have ext' : ∀ {R : struct}, struct_to_set R ∈ C → ∀ {S : struct}, struct_to_set S ∈ C → end_extension R S ∨ end_extension S R,
intros R hR S hS, simp only [mem_repl_img] at hR hS,
rcases hR with ⟨β, βγ, hR⟩, replace hR := struct_to_set_oto hR, subst hR,
rcases hS with ⟨δ, δγ, hS⟩, replace hS := struct_to_set_oto hS, subst hS,
have βord := ord_of_mem_ord γord.ord βγ, have δord := ord_of_mem_ord γord.ord δγ,
have h₄ : ∀ {X Y Z : Set}, (X ∪ Y) \ (X ∪ Z) = (Y \ Z) \ X, intros X Y Z, apply ext,
simp only [mem_diff, mem_union], tauto,
cases ord_conn' βord δord with βδ δβ,
have sub : α.prod {∅} ∪ β.prod {one} ⊆ α.prod {∅} ∪ δ.prod {one},
refine union_subset_of_subset_of_subset subset_union_left (subset_union_of_subset_right (prod_subset_of_subset_of_subset _ subset_self)),
rw ←ord_le_iff_sub βord δord, exact βδ,
left, split,
dsimp, refine union_subset_of_subset_of_subset subset_union_left (subset_union_of_subset_right (prod_subset_of_subset_of_subset _ subset_self)),
rw ←ord_le_iff_sub βord δord, exact βδ,
dsimp, apply rel_sub pair_sep_is_rel, intros x y xy,
have xy' := pair_sep_sub_prod xy, rw pair_mem_prod at xy',
rw pair_mem_pair_sep' (sub xy'.left) (sub xy'.right),
obtain (⟨a, aα, xaz, b, bβ, ybz⟩|(⟨a, aα, xaz, b, bβ, ybz, ab⟩|⟨a, aα, xaz, b, bβ, ybz, ab⟩)) := @pair_mem_lex α.eps_order_struct β.eps_order_struct _ _ xy;
subst xaz; subst ybz; simp only [fst_congr, snd_congr]; rw eps_order_struct_fld at aα bβ,
left, rw [one, succ, union_empty, mem_singleton],
right, rw [eps_order_struct_rel, pair_mem_eps_order' aα bβ] at ab,
rw pair_mem_eps_order' aα bβ, exact ⟨rfl, or.inl ⟨rfl, ab⟩⟩,
right, rw [eps_order_struct_rel, pair_mem_eps_order' aα bβ] at ab,
rw pair_mem_eps_order' (ord_lt_of_lt_of_le δord aα βδ) (ord_lt_of_lt_of_le δord bβ βδ), exact ⟨rfl, or.inr ⟨rfl, ab⟩⟩,
dsimp, intros x hx y hy, have hy' := hy, rw h₄ at hy', rw mem_diff at hy,
rw pair_mem_pair_sep' (sub hx) hy.left,
simp only [mem_union, mem_prod, exists_prop, mem_diff, mem_singleton] at hx hy',
rcases hx with (⟨a, ha, z, hz, xaz⟩|⟨a, ha, z, hz, xaz⟩); subst xaz; subst hz;
rcases hy' with ⟨⟨⟨b, hb, w, hw, ybw⟩, h₅⟩, h₆⟩; subst ybw; subst hw;
simp only [fst_congr, snd_congr],
{ left, rw [one, succ, union_empty, mem_singleton], },
{ right, rw pair_mem_eps_order' (ord_lt_of_lt_of_le δord ha βδ) hb,
refine ⟨rfl, or.inr ⟨rfl, _⟩⟩, rw ←ord_not_le_iff_lt (ord_of_mem_ord δord hb) (ord_of_mem_ord βord ha),
intro ba, exact h₅ ⟨_, ord_lt_of_le_of_lt βord ba ha, _, rfl, rfl⟩, },
have sub : α.prod {∅} ∪ δ.prod {one} ⊆ α.prod {∅} ∪ β.prod {one},
refine union_subset_of_subset_of_subset subset_union_left (subset_union_of_subset_right (prod_subset_of_subset_of_subset _ subset_self)),
rw ←ord_le_iff_sub δord βord, exact δβ,
right, split,
dsimp, refine union_subset_of_subset_of_subset subset_union_left (subset_union_of_subset_right (prod_subset_of_subset_of_subset _ subset_self)),
rw ←ord_le_iff_sub δord βord, exact δβ,
dsimp, apply rel_sub pair_sep_is_rel, intros x y xy,
have xy' := pair_sep_sub_prod xy, rw pair_mem_prod at xy',
rw pair_mem_pair_sep' (sub xy'.left) (sub xy'.right),
obtain (⟨a, aα, xaz, b, bβ, ybz⟩|(⟨a, aα, xaz, b, bβ, ybz, ab⟩|⟨a, aα, xaz, b, bβ, ybz, ab⟩)) := @pair_mem_lex α.eps_order_struct δ.eps_order_struct _ _ xy;
subst xaz; subst ybz; simp only [fst_congr, snd_congr]; rw eps_order_struct_fld at aα bβ,
left, rw [one, succ, union_empty, mem_singleton],
right, rw [eps_order_struct_rel, pair_mem_eps_order' aα bβ] at ab,
rw pair_mem_eps_order' aα bβ, exact ⟨rfl, or.inl ⟨rfl, ab⟩⟩,
right, rw [eps_order_struct_rel, pair_mem_eps_order' aα bβ] at ab,
rw pair_mem_eps_order' (ord_lt_of_lt_of_le βord aα δβ) (ord_lt_of_lt_of_le βord bβ δβ), exact ⟨rfl, or.inr ⟨rfl, ab⟩⟩,
dsimp, intros x hx y hy, have hy' := hy, rw h₄ at hy', rw mem_diff at hy,
rw pair_mem_pair_sep' (sub hx) hy.left,
simp only [mem_union, mem_prod, exists_prop, mem_diff, mem_singleton] at hx hy',
rcases hx with (⟨a, ha, z, hz, xaz⟩|⟨a, ha, z, hz, xaz⟩); subst xaz; subst hz;
rcases hy' with ⟨⟨⟨b, hb, w, hw, ybw⟩, h₅⟩, h₆⟩; subst ybw; subst hw;
simp only [fst_congr, snd_congr],
{ left, rw [one, succ, union_empty, mem_singleton], },
{ right, rw pair_mem_eps_order' (ord_lt_of_lt_of_le βord ha δβ) hb,
refine ⟨rfl, or.inr ⟨rfl, _⟩⟩, rw ←ord_not_le_iff_lt (ord_of_mem_ord βord hb) (ord_of_mem_ord δord ha),
intro ba, exact h₅ ⟨_, ord_lt_of_le_of_lt δord ba ha, _, rfl, rfl⟩, },
obtain ⟨Wwell, We⟩ := L8M hC @ext',
let D := (repl_img (eps_img ∘ set_to_struct) C).Union,
rw iso_type_eq_iff_iso, apply iso_trans (eps_img_isomorphic Wwell), rw We, apply iso_of_eq, congr' 2,
have h₂ : ∀ {β : Set}, β ∈ γ → (eps_img ∘ set_to_struct)
(struct_to_set
{fld := α.prod {∅} ∪ β.prod {one},
rel := pair_sep
(λ (x y : Set),
x.snd ∈ y.snd ∨
x.snd = y.snd ∧
(x.snd = ∅ ∧ x.fst.pair y.fst ∈ α.eps_order ∨
x.snd = one ∧ x.fst.pair y.fst ∈ β.eps_order))
(α.prod {∅} ∪ β.prod {one})
(α.prod {∅} ∪ β.prod {one}),
is_rel := _}) =
α.ord_add β, intros β βγ,
have βord := ord_of_mem_ord γord.ord βγ,
rw [function.comp_app _ _, struct_set_struct_eq_self _],
apply ord_eq_of_ord_ot_eq (eps_img_ord (lex_well_order αord βord)) (ord_add_ord αord βord),
rw ot_add_eq_add_ot αord βord, dsimp [ord_ot],
have h₁ := it_add_eq_lex (ordinal_well_ordered' αord).lin (ordinal_well_ordered' βord).lin,
dsimp at h₁, rw h₁, clear h₁, rw iso_type_eq_iff_iso, apply iso_symm,
apply eps_img_isomorphic (lex_well_order αord βord),
apply ext, rw ←limit_ord_eq_Union γord, simp only [mem_repl_img], intro δ, split,
rintro ⟨R, ⟨β, βγ, Re⟩, δR⟩, refine ⟨_, βγ, _⟩, subst Re, subst δR,
exact h₂ βγ,
rintro ⟨β, βγ, δαβ⟩, subst δαβ, refine ⟨_, ⟨_, βγ, rfl⟩, (h₂ βγ).symm⟩,
end
-- Theorem 8L (M3)
theorem ord_mul_limit {α : Set} (αord : α.is_ordinal) {γ : Set} (γord : γ.limit_ord) :
α.ord_mul γ = (repl_img α.ord_mul γ).Union :=
begin
refine ord_eq_of_ord_ot_eq (ord_mul_ord αord γord.ord) (Union_ords_is_ord _) _,
intros αβ, rw mem_repl_img, rintro ⟨β, βγ, αβαβ⟩, subst αβαβ,
exact ord_mul_ord αord (ord_of_mem_ord γord.ord βγ),
rw [ot_mul_eq_mul_ot αord γord.ord, ord_ot, ord_ot,
it_mul (ordinal_well_ordered' αord).lin (ordinal_well_ordered' γord.ord).lin],
nth_rewrite 0 limit_ord_eq_Union γord,
have h₁ : ∀ ⦃β : Set⦄, β ∈ γ → α.ord_mul β = (eps_img ∘ (mul_struct α.eps_order_struct) ∘ eps_order_struct) β,
simp only [function.comp_app], intros β βγ,
have βord := ord_of_mem_ord γord.ord βγ,
apply ord_eq_of_ord_ot_eq (ord_mul_ord αord βord) (eps_img_ord (mul_struct_well_order αord βord)),
rw [ot_mul_eq_mul_ot αord βord, ord_ot, ord_ot, ord_ot,
it_mul (ordinal_well_ordered' αord).lin (ordinal_well_ordered' βord).lin,
iso_type_eq_iff_iso],
exact eps_img_isomorphic (mul_struct_well_order αord βord),
rw repl_img_ext h₁, dsimp,
let C := repl_img (struct_to_set ∘ (mul_struct α.eps_order_struct) ∘ eps_order_struct) γ,
have hC : ∀ ⦃R : Set⦄, R ∈ C → ∃ S : struct, S.fld.well_order S.rel ∧ R = struct_to_set S,
intros R RC, simp only [mem_repl_img, function.comp_app] at RC,
rcases RC with ⟨β, βγ, Re⟩, subst Re,
exact ⟨_, mul_struct_well_order αord (ord_of_mem_ord γord.ord βγ), rfl⟩,
have hext : ∀ {R : struct}, struct_to_set R ∈ C → ∀ {S : struct}, struct_to_set S ∈ C → end_extension R S ∨ end_extension S R,
{ simp only [mem_repl_img, function.comp_app], rintros R ⟨β, βγ, Re⟩ S ⟨δ, δγ, Se⟩,
replace Re := struct_to_set_oto Re, replace Se := struct_to_set_oto Se,
subst Re, subst Se,
cases ord_conn' (ord_of_mem_ord γord.ord βγ) (ord_of_mem_ord γord.ord δγ) with βδ βδ,
left, rotate, right, rename [δ → β, β → δ, δγ → βγ, βγ → δγ], rotate,
all_goals {
have βord := ord_of_mem_ord γord.ord βγ, have δord := ord_of_mem_ord γord.ord δγ,
have sub' : β ⊆ δ, rw ←ord_le_iff_sub βord δord, exact βδ,
have sub : α.prod β ⊆ α.prod δ := prod_subset_of_subset_of_subset subset_self sub',
split,
dsimp [mul_struct], exact sub,
dsimp [mul_struct, mul_rel], apply rel_sub pair_sep_is_rel,
intros x y, rw pair_mem_pair_sep, rintros ⟨hx, hy, h⟩,
rw pair_mem_pair_sep' (sub hx) (sub hy), rw mem_prod at hx hy,
rcases hx with ⟨a, aα, b, bβ, xab⟩, subst xab,
rcases hy with ⟨c, cα, d, dβ, ycd⟩, subst ycd,
simp only [fst_congr, snd_congr] at *,
rw pair_mem_eps_order' bβ dβ at h,
rw pair_mem_eps_order' (ord_lt_of_lt_of_le δord bβ βδ) (ord_lt_of_lt_of_le δord dβ βδ),
exact h,
dsimp [mul_struct, mul_rel], intros r hr s hs, rw pair_mem_pair_sep' (sub hr) (subset_diff hs),
simp only [prod_diff, mem_prod, exists_prop, mem_diff] at hr hs,
rcases hr with ⟨a, aα, b, bβ, rab⟩, subst rab, rcases hs with ⟨c, cα, d, ⟨dδ, dβ⟩, scd⟩, subst scd,
simp only [fst_congr, snd_congr],
rw [pair_mem_eps_order' (sub' bβ) dδ, pair_mem_eps_order' aα cα], left,
rw ←ord_not_le_iff_lt (ord_of_mem_ord δord dδ) (ord_of_mem_ord βord bβ),
intro db, apply dβ, exact ord_lt_of_le_of_lt βord db bβ, }, },
have L8M := L8M hC @hext,
dsimp at L8M, rcases L8M with ⟨well, h₂⟩,
have h₃ : repl_img (eps_img ∘ set_to_struct) C = repl_img (eps_img ∘ mul_struct α.eps_order_struct ∘ eps_order_struct) γ,
dsimp [C], rw [repl_img_comp, function.comp.assoc eps_img set_to_struct _],
have h₃ : ∀ {f : Set → struct}, set_to_struct ∘ struct_to_set ∘ f = f,
intro f, funext, simp only [function.comp_app, struct_set_struct_eq_self],
rw h₃,
rw h₃ at h₂, rw ←h₂,
have h₄ : mul_struct α.eps_order_struct γ.Union.eps_order_struct = ⟨(repl_img fst C).Union, (repl_img snd C).Union, L8M_lemma hC⟩,
ext; dsimp [mul_struct],
have h₄ : ∀ ⦃x : Set⦄, x ∈ repl_img fst C → x.is_rel,
simp only [mem_repl_img, function.comp_app], rintros P ⟨R, ⟨β, βγ, Re⟩, PR⟩, subst Re, subst PR,
rw set_fst_eq_fld, dsimp [mul_struct], exact prod_is_rel,
apply rel_ext prod_is_rel (Union_is_rel h₄),
simp only [mem_Union, exists_prop, mem_repl_img, function.comp_app, pair_mem_prod], intros a b, split,
rintro ⟨aα, β, βγ, bβ⟩, refine ⟨_, ⟨_, ⟨_, βγ, rfl⟩, rfl⟩, _⟩,
rw set_fst_eq_fld, dsimp [mul_struct], rw pair_mem_prod, exact ⟨aα, bβ⟩,
rintro ⟨P, ⟨R, ⟨β, βγ, Re⟩, PR⟩, abP⟩, subst Re, subst PR,
rw set_fst_eq_fld at abP, dsimp [mul_struct] at abP, rw pair_mem_prod at abP,
exact ⟨abP.left, _, βγ, abP.right⟩,
have h₄ : ∀ ⦃R : Set⦄, R ∈ repl_img snd C → R ⊆ (α.prod γ).prod (α.prod γ),
intro R, simp only [mem_repl_img, function.comp_app],
rintro ⟨S, ⟨β, βγ, Se⟩, RS⟩, subst Se, subst RS, rw set_snd_eq_rel,
dsimp [mul_struct, mul_rel],
have sub : α.prod β ⊆ α.prod γ, apply prod_subset_of_subset_of_subset subset_self,
rw ←ord_le_iff_sub (ord_of_mem_ord γord.ord βγ) γord.ord, left, exact βγ,
exact subset_trans pair_sep_sub_prod (prod_subset_of_subset_of_subset sub sub),
rw ←limit_ord_eq_Union γord, apply rel_ext' pair_sep_sub_prod (Union_sub h₄),
intros x hx y hy, simp only [pair_mem_pair_sep' hx hy, mem_Union, exists_prop, mem_repl_img, function.comp_app],
simp only [mem_prod] at hx hy,
rcases hx with ⟨a, aα, b, bγ, xab⟩, subst xab,
rcases hy with ⟨c, cα, d, dγ, ycd⟩, subst ycd,
have bord := ord_of_mem_ord γord.ord bγ,
have dord := ord_of_mem_ord γord.ord dγ,
have hab : a.pair b ∈ α.prod (b.ord_max d).succ,
rw pair_mem_prod, exact ⟨aα, mem_succ_iff_le.mpr ord_max_le_left⟩,
have hcd : c.pair d ∈ α.prod (b.ord_max d).succ,
rw pair_mem_prod, exact ⟨cα, mem_succ_iff_le.mpr (ord_max_le_right bord dord)⟩,
simp only [fst_congr, snd_congr],
rw [pair_mem_eps_order bγ dγ, pair_mem_eps_order aα cα], split,
rintro (bd|⟨bd, ac⟩);
refine ⟨_, ⟨_, ⟨(b.ord_max d).succ, succ_mem_limit γord (@ord_max_prop (λ x, x ∈ γ) _ bγ _ dγ), rfl⟩, rfl⟩, _⟩;
rw set_snd_eq_rel; dsimp [mul_struct, mul_rel];
rw pair_mem_pair_sep' hab hcd; simp only [fst_congr, snd_congr];
rw [pair_mem_eps_order' (mem_succ_iff_le.mpr ord_max_le_left) (mem_succ_iff_le.mpr (ord_max_le_right bord dord)),
pair_mem_eps_order' aα cα],
left, exact bd,
subst bd, right, exact ⟨rfl, ac⟩,
rintro ⟨R, ⟨S, ⟨β, βγ, Se⟩, RS⟩, abcd⟩, subst Se, subst RS,
rw set_snd_eq_rel at abcd, dsimp [mul_struct, mul_rel] at abcd,
simp only [pair_mem_pair_sep, fst_congr, snd_congr, pair_mem_eps_order' aα cα, eps_order, pair_mem_pair_sep] at abcd,
rcases abcd with ⟨-, -, (⟨-, -, bd⟩|⟨bd, -, -, ac⟩)⟩,
left, exact bd,
right, subst bd, exact ⟨rfl, ac⟩,
rw [h₄, ord_ot, iso_type_eq_iff_iso], apply eps_img_isomorphic,
rw [←h₄, ←limit_ord_eq_Union γord], exact mul_struct_well_order αord γord.ord,
end
noncomputable def ord_exp_fun (α : Set) : Set → Set := fun f,
if f.dom = ∅
then one
else if ex₁ : ∃ β : Set, β.is_ordinal ∧ f.dom = β.succ
then (f.fun_value (classical.some ex₁)).ord_mul α
else if ex₂ : f.dom.limit_ord
then f.ran.Union
else ∅
lemma ord_exp_fun_zero {α f : Set} (fdom : f.dom = ∅) : α.ord_exp_fun f = one :=
begin
rw ord_exp_fun, dsimp, rw if_pos fdom,
end
lemma ord_exp_fun_succ {α f β : Set} (βord : β.is_ordinal) (dom : f.dom = β.succ) :
α.ord_exp_fun f = (f.fun_value β).ord_mul α :=
begin
have h : ¬ f.dom = ∅, rw [eq_empty, dom], push_neg, exact ⟨_, self_mem_succ⟩,
rw ord_exp_fun, dsimp, rw if_neg h,
have ex₁ : ∃ β : Set, β.is_ordinal ∧ f.dom = β.succ := ⟨_, βord, dom⟩,
rw dif_pos ex₁,
obtain ⟨ord, dom'⟩ := classical.some_spec ex₁, rw dom' at dom,
have h₂ := succ_inj' dom, rw h₂,
end
lemma ord_exp_fun_limit {α f : Set} (fdom : f.dom.limit_ord) :
α.ord_exp_fun f = f.ran.Union :=
begin
have ex₀ : ¬ f.dom = ∅, rw dom, exact fdom.ne,
have ex₁ : ¬ ∃ α : Set, α.is_ordinal ∧ f.dom = α.succ,
rintro ⟨α, -, γα⟩, exact fdom.ns ⟨_, γα⟩,
rw ord_exp_fun, dsimp, rw [if_neg ex₀, dif_neg ex₁, if_pos fdom],
end
noncomputable def ord_exp (α β : Set) : Set :=
if α = ∅ then
if β = ∅ then one else ∅
else (ord_rec α.ord_exp_fun) β
lemma ord_exp_base_zero {β : Set} (βne : β ≠ ∅) : ord_exp ∅ β = ∅ :=
by rw [ord_exp, if_pos rfl, if_neg βne]
lemma ord_exp_zero {α : Set} : α.ord_exp ∅ = one :=
begin
by_cases hα : α = ∅,
rw [ord_exp, if_pos hα, if_pos rfl],
rw [ord_exp, if_neg hα, ord_rec_spec zero_is_ord, ord_exp_fun_zero (con_fun_dom zero_is_ord)],
end
lemma ord_exp_succ {α β : Set} (βord : β.is_ordinal) : α.ord_exp β.succ = (α.ord_exp β).ord_mul α :=
begin
by_cases hα : α = ∅,
rw [ord_exp, if_pos hα, if_neg succ_neq_empty, hα, ord_exp, if_pos rfl],
by_cases hβ : β = ∅,
rw [if_pos hβ, ord_mul_zero one_is_ord],
rw [if_neg hβ, ord_mul_zero zero_is_ord],
have βord' := succ_ord_of_ord βord,
rw [ord_exp, if_neg hα, ord_rec_spec βord', ord_exp_fun_succ βord (con_fun_dom βord'), con_fun_spec βord' self_mem_succ,
ord_exp, if_neg hα],
end
lemma ord_exp_limit {α : Set} (αne : α ≠ ∅) {γ : Set} (γord : γ.limit_ord) : α.ord_exp γ = (repl_img α.ord_exp γ).Union :=
begin
have dom : (con_fun α.ord_exp_fun γ).dom.limit_ord, rwa con_fun_dom γord.ord,
rw [ord_exp, if_neg αne, ord_rec_spec γord.ord, ord_exp_fun_limit dom, con_fun_ran_eq γord.ord], congr' 1,
apply repl_img_ext, intros β βγ, rw [ord_exp, if_neg αne],
end
lemma ord_exp_ord {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) :
(α.ord_exp β).is_ordinal :=
begin
revert β, apply trans_ind_schema, intros β βord ind,
by_cases hβ : β = ∅,
subst hβ, rw ord_exp_zero, exact one_is_ord,
by_cases βs : ∃ δ : Set, β = δ.succ,
rcases βs with ⟨δ, βδ⟩, subst βδ,
rw ord_exp_succ (ord_of_succ_ord βord),
exact ord_mul_ord (ind self_mem_succ) αord,
by_cases hα : α = ∅,
subst hα, rw ord_exp_base_zero hβ, exact zero_is_ord,
rw ord_exp_limit hα ⟨βord, hβ, βs⟩, exact Union_ords_is_ord (of_repl_img @ind),
end
theorem ord_one_exp : ∀ {α : Set}, α.is_ordinal → one.ord_exp α = one :=
begin
apply trans_ind_schema, intros α αord ind,
by_cases αz : α = ∅,
subst αz, rw ord_exp_zero,
by_cases ex : ∃ β : Set, α = β.succ,
rcases ex with ⟨β, αβ⟩, subst αβ,
have βord := ord_of_succ_ord αord,
rw [ord_exp_succ βord, ord_mul_one (ord_exp_ord one_is_ord βord), ind self_mem_succ],
have αord' : α.limit_ord := ⟨αord, αz, ex⟩,
rw [ord_exp_limit zero_ne_one.symm αord', eq_iff_subset_and_subset], split,
apply Union_sub, apply @of_repl_img _ _ (λ x, x ⊆ one), intros β βα,
have βord := ord_of_mem_ord αord βα,
rw ←ord_le_iff_sub (ord_exp_ord one_is_ord βord) one_is_ord, right,
exact ind βα,
intros z hz, rw [one, succ, union_empty, mem_singleton] at hz, subst hz,
simp only [mem_Union, exists_prop, mem_repl_img], refine ⟨_, ⟨∅, limit_ord_pos αord', rfl⟩, _⟩,
rw ord_exp_zero, exact zero_lt_one,
end
theorem nat_ord_add {n : Set} (nω : n ∈ ω) : ∀ {m : Set}, m ∈ ω → n.ord_add m = n + m :=
begin
have nord := nat_is_ord nω,
apply induction,
rw [ord_add_zero nord, add_base nω],
intros m mω ind, rw [ord_add_succ nord (nat_is_ord mω), add_ind nω mω, ind],
end
theorem nat_ord_mul {n : Set} (nω : n ∈ ω) : ∀ {m : Set}, m ∈ ω → n.ord_mul m = n * m :=
begin
have nord := nat_is_ord nω,
apply induction,
rw [ord_mul_zero nord, mul_base nω],
intros m mω ind, rw [ord_mul_succ nord (nat_is_ord mω), ind, nat_ord_add (mul_into_nat nω mω) nω, mul_ind nω mω],
end
theorem nat_ord_exp {n : Set} (nω : n ∈ ω) : ∀ {m : Set}, m ∈ ω → n.ord_exp m = n ^ m :=
begin
have nord := nat_is_ord nω,
apply induction,
rw [ord_exp_zero, exp_base nω],
intros m mω ind, rw [ord_exp_succ (nat_is_ord mω), ind, nat_ord_mul (exp_into_nat nω mω) nω, exp_ind nω mω],
end
theorem two_exp_omega_eq_omega : two.ord_exp ω = ω :=
begin
rw ord_exp_limit zero_ne_two.symm omega_limit_ord, apply unbounded_Union_eq_nat,
apply of_repl_img, intros n nω, rw nat_ord_exp two_nat nω, exact exp_into_nat two_nat nω,
rw mem_repl_img, refine ⟨_, zero_nat, _⟩, rw ord_exp_zero,
apply of_repl_img, intros n nω, use ord_exp two n.succ,
have nω' := succ_nat_is_nat nω,
rw mem_repl_img, refine ⟨⟨_, nω', rfl⟩, _⟩,
rw [nat_ord_exp two_nat nω, nat_ord_exp two_nat nω'],
exact exp_lt_of_lt two_nat one_lt_two self_mem_succ nω',
end
-- Theorem 8N (a)
theorem ord_add_normal {α : Set} (αord : α.is_ordinal) : normal α.ord_add :=
begin
refine norm_of_con (@ord_add_ord _ αord) (@ord_add_limit _ αord) (λ β βord, _),
rw ord_add_succ αord βord, exact self_mem_succ,
end
-- Theorem 8N (b)
theorem ord_mul_normal {α : Set} (αord : α.is_ordinal) (hα : one ≤ α) : normal α.ord_mul :=
begin
have αord' := succ_ord_of_ord αord,
refine norm_of_con (@ord_mul_ord _ αord) (@ord_mul_limit _ αord) (λ β βord, _),
rw ord_mul_succ αord βord,
have αβord := ord_mul_ord αord βord,
nth_rewrite 0 ←ord_add_zero αβord, apply (ord_add_normal αβord).mon αord (ord_lt_of_succ_le zero_is_ord αord hα),
end
lemma exp_positive {α : Set} (αord : α.is_ordinal) (αnz : α ≠ ∅) : ∀ {β : Set}, β.is_ordinal → ∅ ∈ α.ord_exp β :=
begin
apply trans_ind_schema, intros β βord ind,
by_cases hβ : β = ∅,
subst hβ, rw ord_exp_zero, exact zero_lt_one,
by_cases βs : ∃ δ : Set, β = δ.succ,
rcases βs with ⟨δ, βδ⟩, subst βδ,
have δord := ord_of_succ_ord βord,
have αδord := ord_exp_ord αord δord,
rw [ord_exp_succ (ord_of_succ_ord βord), ←ord_mul_zero αδord],
apply (ord_mul_normal αδord (succ_least_upper_bound αδord (ind self_mem_succ))).mon αord,
exact ord_pos_of_inhab αord (inhabited_of_ne_empty αnz),
rw ord_exp_limit αnz ⟨βord, hβ, βs⟩,
simp only [mem_Union, exists_prop, mem_repl_img],
obtain ⟨δ, δβ⟩ := inhabited_of_ne_empty hβ,
refine ⟨_, ⟨_, δβ, rfl⟩, ind δβ⟩,
end
-- Theorem 8N (c)
theorem ord_exp_normal {α : Set} (αord : α.is_ordinal) (hα : two ≤ α) : normal α.ord_exp :=
begin
have αnz : α ≠ ∅, intro αz, subst αz, cases hα,
exact mem_empty _ hα,
exact zero_ne_two.symm hα,
refine norm_of_con (@ord_exp_ord _ αord) (@ord_exp_limit _ αnz) (λ β βord, _),
have αβord := ord_exp_ord αord βord,
rw ord_exp_succ βord, nth_rewrite 0 ←ord_mul_one αβord,
apply (ord_mul_normal αβord (succ_least_upper_bound αβord (exp_positive αord αnz βord))).mon αord,
apply ord_lt_of_succ_le one_is_ord αord hα,
end
-- Corollary 8P (a)
theorem ord_add_lt_iff {α : Set} (αord : α.is_ordinal) :
∀ {β : Set}, β.is_ordinal → ∀ {γ : Set}, γ.is_ordinal → (β ∈ γ ↔ α.ord_add β ∈ α.ord_add γ) :=
@mon_op_mon_iff _ (@ord_add_ord _ αord) (@ord_add_normal _ αord).mon
theorem ord_mul_lt_iff {α : Set} (αord : α.is_ordinal) (hα : one ≤ α) :
∀ {β : Set}, β.is_ordinal → ∀ {γ : Set}, γ.is_ordinal → (β ∈ γ ↔ α.ord_mul β ∈ α.ord_mul γ) :=
@mon_op_mon_iff _ (@ord_mul_ord _ αord) (@ord_mul_normal _ αord hα).mon
theorem ord_exp_lt_iff {α : Set} (αord : α.is_ordinal) (hα : two ≤ α) :
∀ {β : Set}, β.is_ordinal → ∀ {γ : Set}, γ.is_ordinal → (β ∈ γ ↔ α.ord_exp β ∈ α.ord_exp γ) :=
@mon_op_mon_iff _ (@ord_exp_ord _ αord) (@ord_exp_normal _ αord hα).mon
theorem ord_add_le_iff {α : Set} (αord : α.is_ordinal) :
∀ {β : Set}, β.is_ordinal → ∀ {γ : Set}, γ.is_ordinal → (β ≤ γ ↔ α.ord_add β ≤ α.ord_add γ) :=
@mon_op_le_iff _ (@ord_add_ord _ αord) (@ord_add_normal _ αord).mon
theorem ord_mul_le_iff {α : Set} (αord : α.is_ordinal) (hα : one ≤ α) :
∀ {β : Set}, β.is_ordinal → ∀ {γ : Set}, γ.is_ordinal → (β ≤ γ ↔ α.ord_mul β ≤ α.ord_mul γ) :=
@mon_op_le_iff _ (@ord_mul_ord _ αord) (@ord_mul_normal _ αord hα).mon
theorem ord_exp_le_iff {α : Set} (αord : α.is_ordinal) (hα : two ≤ α) :
∀ {β : Set}, β.is_ordinal → ∀ {γ : Set}, γ.is_ordinal → (β ≤ γ ↔ α.ord_exp β ≤ α.ord_exp γ) :=
@mon_op_le_iff _ (@ord_exp_ord _ αord) (@ord_exp_normal _ αord hα).mon
-- Corollary 8P (b)
theorem ord_add_cancel {α : Set} (αord : α.is_ordinal) :
∀ {β : Set}, β.is_ordinal → ∀ {γ : Set}, γ.is_ordinal → α.ord_add β = α.ord_add γ → β = γ :=
@mon_op_inj _ (@ord_add_ord _ αord) (@ord_add_normal _ αord).mon
theorem ord_mul_cancel {α : Set} (αord : α.is_ordinal) (hα : one ≤ α) :
∀ {β : Set}, β.is_ordinal → ∀ {γ : Set}, γ.is_ordinal → α.ord_mul β = α.ord_mul γ → β = γ :=
@mon_op_inj _ (@ord_mul_ord _ αord) (@ord_mul_normal _ αord hα).mon
theorem ord_exp_cancel {α : Set} (αord : α.is_ordinal) (hα : two ≤ α) :
∀ {β : Set}, β.is_ordinal → ∀ {γ : Set}, γ.is_ordinal → α.ord_exp β = α.ord_exp γ → β = γ :=
@mon_op_inj _ (@ord_exp_ord _ αord) (@ord_exp_normal _ αord hα).mon
-- Theorem 8Q
theorem ord_add_le_right {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) (αβ : α ≤ β) :
∀ {γ : Set}, γ.is_ordinal → α.ord_add γ ≤ β.ord_add γ :=
begin
apply trans_ind_schema, intros γ γord ind,
by_cases γe : γ = ∅,
subst γe, rwa [ord_add_zero αord, ord_add_zero βord],
by_cases ex : ∃ δ : Set, γ = δ.succ,
rcases ex with ⟨δ, γδ⟩, subst γδ,
have δord := ord_of_succ_ord γord,
rw [ord_add_succ αord δord, ord_add_succ βord δord, ←ord_succ_le_iff (ord_add_ord αord δord) (ord_add_ord βord δord)],
exact ind self_mem_succ,
have γord' : γ.limit_ord := ⟨γord, γe, ex⟩,
rw [ord_le_iff_sub (ord_add_ord αord γord) (ord_add_ord βord γord), ord_add_limit αord γord', ord_add_limit βord γord'],
simp only [subset_def, mem_Union, exists_prop, mem_repl_img],
rintro μ ⟨αδ, ⟨δ, δγ, αδαδ⟩, μαδ⟩, subst αδαδ, refine ⟨_, ⟨_, δγ, rfl⟩, _⟩,
exact ord_lt_of_lt_of_le (ord_add_ord βord (ord_of_mem_ord γord δγ)) μαδ (ind δγ),
end
theorem ord_mul_le_right {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) (αβ : α ≤ β) :
∀ {γ : Set}, γ.is_ordinal → α.ord_mul γ ≤ β.ord_mul γ :=
begin
apply trans_ind_schema, intros γ γord ind,
by_cases γe : γ = ∅,
subst γe, rw [ord_mul_zero αord, ord_mul_zero βord], exact le_self,
by_cases ex : ∃ δ : Set, γ = δ.succ,
rcases ex with ⟨δ, γδ⟩, subst γδ,
have δord := ord_of_succ_ord γord,
rw [ord_mul_succ αord δord, ord_mul_succ βord δord],
have βδord := ord_mul_ord βord δord,
apply ord_le_trans (ord_add_ord βδord βord) (ord_add_le_right (ord_mul_ord αord δord) βδord (ind self_mem_succ) αord),
rwa ←ord_add_le_iff βδord αord βord,
have γord' : γ.limit_ord := ⟨γord, γe, ex⟩,
rw [ord_le_iff_sub (ord_mul_ord αord γord) (ord_mul_ord βord γord), ord_mul_limit αord γord', ord_mul_limit βord γord'],
simp only [subset_def, mem_Union, exists_prop, mem_repl_img],
rintro μ ⟨αδ, ⟨δ, δγ, αδαδ⟩, μαδ⟩, subst αδαδ, refine ⟨_, ⟨_, δγ, rfl⟩, _⟩,
exact ord_lt_of_lt_of_le (ord_mul_ord βord (ord_of_mem_ord γord δγ)) μαδ (ind δγ),
end
theorem ord_exp_le_right {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) (αβ : α ≤ β) :
∀ {γ : Set}, γ.is_ordinal → α.ord_exp γ ≤ β.ord_exp γ :=
begin
apply trans_ind_schema, intros γ γord ind,
by_cases γe : γ = ∅,
subst γe, rw [ord_exp_zero, ord_exp_zero], exact le_self,
by_cases ex : ∃ δ : Set, γ = δ.succ,
rcases ex with ⟨δ, γδ⟩, subst γδ,
have δord := ord_of_succ_ord γord,
rw [ord_exp_succ δord, ord_exp_succ δord],
have βδord := ord_exp_ord βord δord,
apply ord_le_trans (ord_mul_ord βδord βord) (ord_mul_le_right (ord_exp_ord αord δord) βδord (ind self_mem_succ) αord),
by_cases δz : δ = ∅,
subst δz, rwa [ord_exp_zero, ord_one_mul αord, ord_one_mul βord],
by_cases βz : β = ∅,
subst βz, rw [ord_exp_base_zero δz, ord_zero_mul αord, ord_zero_mul βord], exact le_self,
rwa ←ord_mul_le_iff βδord (succ_least_upper_bound βδord (exp_positive βord βz δord)) αord βord,
have γord' : γ.limit_ord := ⟨γord, γe, ex⟩,
by_cases αz : α = ∅,
subst αz, rw [ord_exp_base_zero γe], exact empty_le_ord (ord_exp_ord βord γord),
have βγord := ord_exp_ord βord γord,
rw [ord_le_iff_sub (ord_exp_ord αord γord) βγord, ord_exp_limit αz γord'],
apply Union_sub, apply @of_repl_img _ _ (λ x, x ⊆ β.ord_exp γ), intros δ δγ,
have δord := ord_of_mem_ord γord δγ,
rw ←ord_le_iff_sub (ord_exp_ord αord δord) βγord, apply ord_le_trans βγord (ind δγ),
cases empty_le_ord βord with hβ hβ,
replace hβ := succ_least_upper_bound βord hβ, change one ≤ β at hβ,
cases hβ with hβ hβ,
replace hβ := succ_least_upper_bound βord hβ, change two ≤ β at hβ,
rw ←ord_exp_le_iff βord hβ δord γord, exact or.inl δγ,
subst hβ, rw [ord_one_exp δord, ord_one_exp γord], exact le_self,
subst hβ, exfalso, exact αz (ord_eq_zero_of_le_zero αord αβ),
end
theorem ord_sub_theorem {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) (αβ : α ≤ β) :
∃! δ : Set, δ.is_ordinal ∧ α.ord_add δ = β :=
begin
refine exists_unique_of_exists_of_unique _ (λ δ γ hδ hγ, ord_add_cancel αord hδ.left hγ.left (by rw [hδ.right, hγ.right])),
have αβ' : α.ord_add ∅ ≤ β, rwa ord_add_zero αord,
obtain ⟨δ, δord, δβ, un⟩ := exists_large_ord_of_norm (@ord_add_ord _ αord) (@ord_add_normal _ αord) βord αβ',
dsimp at δβ un, cases δβ,
exfalso, specialize un (succ_ord_of_ord δord), rw ord_add_succ αord δord at un,
specialize un (succ_least_upper_bound βord δβ),
exact succ_ord_not_le_self δord un,
exact ⟨_, δord, δβ⟩,
end
theorem ord_div_theorem {α : Set} (αord : α.is_ordinal) {δ : Set} (δord : δ.is_ordinal) (δnz : δ ≠ ∅) :
∃! β : Set, β.is_ordinal ∧ ∃! γ : Set, γ.is_ordinal ∧ α = (δ.ord_mul β).ord_add γ ∧ γ ∈ δ :=
begin
refine exists_unique_of_exists_of_unique _ _,
have hδ : one ≤ δ := succ_least_upper_bound δord (ord_pos_of_inhab δord (inhabited_of_ne_empty δnz)),
have δα : δ.ord_mul ∅ ≤ α, rw ord_mul_zero δord, exact empty_le_ord αord,
obtain ⟨β, βord, δβα, h⟩ := exists_large_ord_of_norm (@ord_mul_ord _ δord) (@ord_mul_normal _ δord hδ) αord δα,
have δβord := ord_mul_ord δord βord,
dsimp at δβα h, obtain ⟨γ, ⟨γord, h'⟩, h''⟩ := ord_sub_theorem δβord αord δβα,
refine ⟨_, βord, _, ⟨γord, h'.symm, _⟩, _⟩,
rw ←ord_not_le_iff_lt δord γord, intro δγ,
refine succ_ord_not_le_self βord (h (succ_ord_of_ord βord) _),
rwa [ord_mul_succ δord βord, ←h', ←ord_add_le_iff δβord δord γord],
dsimp, rintros γ' ⟨γord', h''', γδ'⟩, apply ord_add_cancel δβord γord' γord, rw [h', ←h'''],
rintros β₁ β₂ ⟨βord₁, γ₁, ⟨γord₁, h₁, γδ₁⟩, -⟩ ⟨βord₂, γ₂, ⟨γord₂, h₂, γδ₂⟩, -⟩,
apply ord_eq_of_not_lt βord₁ βord₂,
rotate, rename [β₁ → β₂, β₂ → β₁, βord₁ → βord₂, βord₂ → βord₁, γ₁ → γ₂, γ₂ → γ₁, h₁ → h₂, h₂ → h₁, γord₁ → γord₂,
γord₂ → γord₁, γδ₁ → γδ₂, γδ₂ → γδ₁],
all_goals {
intro ββ, apply ord_mem_irrefl αord, nth_rewrite 0 h₁, rw h₂,
have h' := ord_mul_ord δord βord₂,
have h := ord_add_ord h' γord₂,
apply @ord_lt_of_lt_of_le _ ((δ.ord_mul β₁).ord_add δ) _ h,
rwa ←ord_add_lt_iff (ord_mul_ord δord βord₁) γord₁ δord,
rw ←ord_mul_succ δord βord₁, apply @ord_le_trans _ (δ.ord_mul β₂) _ h,
rw ←ord_mul_le_iff δord (succ_least_upper_bound δord (ord_pos_of_inhab δord (inhabited_of_ne_empty δnz)))
(succ_ord_of_ord βord₁) βord₂,
exact succ_least_upper_bound βord₂ ββ,
nth_rewrite 0 ←ord_add_zero h',
rw ←ord_add_le_iff h' zero_is_ord γord₂, exact empty_le_ord γord₂, },
end
theorem ord_log_theorem {α : Set} (αord : α.is_ordinal) (αnz : α ≠ ∅)
{β : Set} (βord : β.is_ordinal) (hβ : two ≤ β) :
∃! γ : Set, γ.is_ordinal ∧ ∃! δ : Set, δ.is_ordinal ∧ ∃! ρ : Set, ρ.is_ordinal ∧
α = ((β.ord_exp γ).ord_mul δ).ord_add ρ ∧ δ ≠ ∅ ∧ δ ∈ β ∧ ρ ∈ β.ord_exp γ :=
begin
have βnz : β ≠ ∅ := not_zero_of_pos (ord_lt_of_lt_of_le βord zero_lt_two hβ),
refine exists_unique_of_exists_of_unique _ _,
have βα : β.ord_exp ∅ ≤ α, rw ord_exp_zero,
exact succ_least_upper_bound αord (ord_pos_of_inhab αord (inhabited_of_ne_empty αnz)),
obtain ⟨γ, γord, βγα, un⟩ := exists_large_ord_of_norm (@ord_exp_ord _ βord) (@ord_exp_normal _ βord hβ) αord βα,
have h'' : β.ord_exp γ ≠ ∅ := not_zero_of_pos (exp_positive βord βnz γord),
obtain ⟨δ, ⟨δord, ρ, ⟨ρord, αe, hρ⟩, h⟩, h'⟩ := ord_div_theorem αord (ord_exp_ord βord γord) h'',
dsimp at *,
have δnz : δ ≠ ∅, intro δz, subst δz,
rw [ord_mul_zero (ord_exp_ord βord γord), ord_zero_add ρord] at αe, subst αe,
exact ord_mem_irrefl αord (ord_lt_of_lt_of_le αord hρ βγα),
have δβ : δ ∈ β, rw ←ord_not_le_iff_lt βord δord, intro βδ,
apply succ_ord_not_le_self γord, apply un (succ_ord_of_ord γord),
apply @ord_le_trans _ ((β.ord_exp γ).ord_mul δ) _ αord,
have h''' : one ≤ β.ord_exp γ := succ_least_upper_bound (ord_exp_ord βord γord)
(ord_pos_of_inhab (ord_exp_ord βord γord) (inhabited_of_ne_empty h'')),
rwa [ord_exp_succ γord, ←ord_mul_le_iff (ord_exp_ord βord γord) h''' βord δord],
rw [←ord_add_zero (ord_mul_ord (ord_exp_ord βord γord) δord), αe,
←ord_add_le_iff (ord_mul_ord (ord_exp_ord βord γord) δord) zero_is_ord ρord],
exact empty_le_ord ρord,
refine ⟨_, γord, _, ⟨δord, _, ⟨ρord, αe, δnz, δβ, hρ⟩, _⟩, _⟩; dsimp,
rintros ρ' ⟨ρord', αe', -, -, hρ'⟩, exact h _ ⟨ρord', αe', hρ'⟩,
rintros δ' ⟨δord', ρ', ⟨ρord', αe', δnz', δβ', hρ'⟩, h'''⟩, dsimp at h''',
apply h' _ ⟨δord', _, ⟨ρord', αe', hρ'⟩, _⟩, dsimp,
rintros ρ'' ⟨ρord'', αe'', hρ''⟩, exact h''' _ ⟨ρord'', αe'', δnz', δβ', hρ''⟩,
rintros γ₁ γ₂ ⟨γord₁, δ₁, ⟨δord₁, ρ₁, ⟨ρord₁, αe₁, δnz₁, δβ₁, hρ₁⟩, -⟩, -⟩
⟨γord₂, δ₂, ⟨δord₂, ρ₂, ⟨ρord₂, αe₂, δnz₂, δβ₂, hρ₂⟩, -⟩, -⟩,
apply ord_eq_of_not_lt γord₁ γord₂, rotate,
rename [γ₁ → γ₂, γord₁ → γord₂, δ₁ → δ₂, δord₁ → δord₂, ρ₁ → ρ₂, ρord₁ → ρord₂, αe₁ → αe₂, δnz₁ → δnz₂, δβ₁ → δβ₂, hρ₁ → hρ₂,
γ₂ → γ₁, γord₂ → γord₁, δ₂ → δ₁, δord₂ → δord₁, ρ₂ → ρ₁, ρord₂ → ρord₁, αe₂ → αe₁, δnz₂ → δnz₁, δβ₂ → δβ₁, hρ₂ → hρ₁],
all_goals {
intro γγ, apply ord_mem_irrefl αord, nth_rewrite 0 αe₁, subst αe₂,
apply @ord_lt_of_lt_of_le _ (((β.ord_exp γ₁).ord_mul δ₁).ord_add (β.ord_exp γ₁)) _ αord,
rwa ←ord_add_lt_iff (ord_mul_ord (ord_exp_ord βord γord₁) δord₁) ρord₁ (ord_exp_ord βord γord₁),
apply @ord_le_trans _ ((β.ord_exp γ₁).ord_mul β) _ αord,
have δβ' := succ_least_upper_bound βord δβ₁,
have h : one ≤ β.ord_exp γ₁ := succ_least_upper_bound (ord_exp_ord βord γord₁) (exp_positive βord βnz γord₁),
rwa [←ord_mul_succ (ord_exp_ord βord γord₁) δord₁, ←ord_mul_le_iff (ord_exp_ord βord γord₁) h (succ_ord_of_ord δord₁) βord],
apply @ord_le_trans _ (β.ord_exp γ₂) _ αord,
rw [←ord_exp_succ γord₁, ←ord_exp_le_iff βord hβ (succ_ord_of_ord γord₁) γord₂],
exact succ_least_upper_bound γord₂ γγ,
apply @ord_le_trans _ ((β.ord_exp γ₂).ord_mul δ₂) _ αord,
have h' : one ≤ β.ord_exp γ₂ := succ_least_upper_bound (ord_exp_ord βord γord₂) (exp_positive βord βnz γord₂),
nth_rewrite 0 ←ord_mul_one (ord_exp_ord βord γord₂),
rw ←ord_mul_le_iff (ord_exp_ord βord γord₂) h' one_is_ord δord₂,
exact succ_least_upper_bound δord₂ (ord_pos_of_inhab δord₂ (inhabited_of_ne_empty δnz₂)),
nth_rewrite 0 ←ord_add_zero (ord_mul_ord (ord_exp_ord βord γord₂) δord₂),
rw ←ord_add_le_iff (ord_mul_ord (ord_exp_ord βord γord₂) δord₂) zero_is_ord ρord₂,
exact empty_le_ord ρord₂, },
end
lemma right_addend_zero_of_add_zero {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal)
(h : α.ord_add β = ∅) : β = ∅ :=
begin
apply classical.by_contradiction, intro hβ,
have h' : one ≤ β := succ_least_upper_bound βord (ord_pos_of_inhab βord (inhabited_of_ne_empty hβ)),
refine mem_empty ∅ (ord_lt_of_lt_of_le zero_is_ord (zero_mem_succ_ord αord) _),
rwa [←ord_add_one_eq_succ αord, ←h, ←ord_add_le_iff αord one_is_ord βord],
end
lemma add_not_zero_of_right_not_zero {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal)
(h : β ≠ ∅) : α.ord_add β ≠ ∅ :=
λ h', h (right_addend_zero_of_add_zero αord βord h')
-- Theorem 8R
theorem ord_exp_add {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal)
{γ : Set} (γord : γ.is_ordinal) : α.ord_exp (β.ord_add γ) = (α.ord_exp β).ord_mul (α.ord_exp γ) :=
begin
cases empty_le_ord αord with hα hα,
replace hα := succ_least_upper_bound αord hα, cases hα,
replace hα := succ_least_upper_bound αord hα, change two ≤ α at hα,
have αnz : α ≠ ∅ := not_zero_of_pos (ord_lt_of_lt_of_le αord zero_lt_two hα),
revert γ, apply trans_ind_schema, intros γ γord ind,
rcases ord_cases γord with (γz|(⟨δ, γδ⟩|γord')),
{ subst γz, rw [ord_add_zero βord, ord_exp_zero, ord_mul_one (ord_exp_ord αord βord)], },
{ subst γδ, have δord := ord_of_succ_ord γord,
rw [ord_add_succ βord δord, ord_exp_succ (ord_add_ord βord δord), ind self_mem_succ,
ord_mul_assoc (ord_exp_ord αord βord) (ord_exp_ord αord δord) αord, ←ord_exp_succ δord], },
{ rw [ord_add_limit βord γord', ord_exp_limit αnz γord'],
have Sne : repl_img β.ord_add γ ≠ ∅, apply ne_empty_of_inhabited, use β,
rw mem_repl_img, refine ⟨_, limit_ord_pos γord', _⟩, rw ord_add_zero βord,
have Sord : ∀ ⦃δ : Set⦄, δ ∈ repl_img β.ord_add γ → δ.is_ordinal, intro δ, rw mem_repl_img,
rintro ⟨ρ, ργ, δe⟩, subst δe, exact ord_add_ord βord (ord_of_mem_ord γord ργ),
have h := sup_norm_fun (@ord_exp_ord _ αord) (@ord_exp_normal _ αord hα) Sne Sord,
have Tne : repl_img α.ord_exp γ ≠ ∅, apply ne_empty_of_inhabited, use one,
rw mem_repl_img, refine ⟨_, limit_ord_pos γord', _⟩, rw ord_exp_zero,
have Tord : ∀ ⦃δ : Set⦄, δ ∈ repl_img α.ord_exp γ → δ.is_ordinal, intro δ, rw mem_repl_img,
rintro ⟨ρ, ργ, δe⟩, subst δe, exact ord_exp_ord αord (ord_of_mem_ord γord ργ),
have αβord := ord_exp_ord αord βord,
have hαβ : one ≤ α.ord_exp β := succ_least_upper_bound αβord (exp_positive αord αnz βord),
have h' := sup_norm_fun (@ord_mul_ord _ αβord) (@ord_mul_normal _ αβord hαβ) Tne Tord,
dsimp at h h',
rw [←sup, ←sup, h, h', repl_img_comp, repl_img_comp], congr' 1, apply repl_img_ext,
intros δ δγ, simp only [function.comp_app], rw ind δγ, },
change one = α at hα, subst hα,
rw [ord_one_exp (ord_add_ord βord γord), ord_one_exp βord, ord_one_exp γord, ord_mul_one one_is_ord],
subst hα, by_cases hγ : γ = ∅,
subst hγ, by_cases hβ : β = ∅,
subst hβ, simp only [ord_add_zero zero_is_ord, ord_exp_zero, ord_mul_one one_is_ord],
rw [ord_add_zero βord, ord_exp_zero, ord_exp_base_zero hβ, ord_mul_one zero_is_ord],
have h := add_not_zero_of_right_not_zero βord γord hγ,
rw [ord_exp_base_zero h, ord_exp_base_zero hγ, ord_mul_zero (ord_exp_ord zero_is_ord βord)],
end
theorem ord_mul_eq_zero {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal) :
α.ord_mul β = ∅ ↔ α = ∅ ∨ β = ∅ :=
⟨λ h, classical.by_contradiction (λ h', begin
push_neg at h', rcases h' with ⟨hα, hβ⟩,
replace hα := succ_least_upper_bound αord (ord_pos_of_inhab αord (inhabited_of_ne_empty hα)),
replace hβ := succ_least_upper_bound βord (ord_pos_of_inhab βord (inhabited_of_ne_empty hβ)),
refine mem_empty ∅ (ord_lt_of_succ_le zero_is_ord zero_is_ord (@ord_le_trans _ (α.ord_mul one) _ zero_is_ord _ _)),
change one ≤ α.ord_mul one, nth_rewrite 0 ←ord_mul_one one_is_ord, apply ord_mul_le_right one_is_ord αord hα one_is_ord,
rwa [←h, ←ord_mul_le_iff αord hα one_is_ord βord],
end), λ h, or.elim h (λ h', by rw [h', ord_zero_mul βord]) (λ h', by rw [h', ord_mul_zero αord])⟩
-- Theorem 8S
theorem ord_exp_exp {α : Set} (αord : α.is_ordinal) {β : Set} (βord : β.is_ordinal)
{γ : Set} (γord : γ.is_ordinal) : (α.ord_exp β).ord_exp γ = α.ord_exp (β.ord_mul γ) :=
begin
cases empty_le_ord αord with hα hα,
replace hα := succ_least_upper_bound αord hα, cases hα,
replace hα := succ_least_upper_bound αord hα, change two ≤ α at hα,
have αnz : α ≠ ∅ := not_zero_of_pos (ord_lt_of_lt_of_le αord zero_lt_two hα),
have αβnz : α.ord_exp β ≠ ∅ := not_zero_of_pos (exp_positive αord αnz βord),
revert γ, apply trans_ind_schema, intros γ γord ind,
rcases ord_cases γord with (γz|(⟨δ, γδ⟩|γord')),
{ subst γz, simp only [ord_exp_zero, ord_mul_zero βord], },
{ subst γδ, have δord := ord_of_succ_ord γord,
rw [ord_exp_succ δord, ind self_mem_succ, ←ord_exp_add αord (ord_mul_ord βord δord) βord,
←ord_mul_succ βord δord], },
{ rw [ord_mul_limit βord γord', ←sup],
have Sne : repl_img β.ord_mul γ ≠ ∅, apply ne_empty_of_inhabited, use ∅,
rw mem_repl_img, refine ⟨_, limit_ord_pos γord', _⟩, rw ord_mul_zero βord,
have Sord : ∀ ⦃δ : Set⦄, δ ∈ repl_img β.ord_mul γ → δ.is_ordinal,
apply of_repl_img, intros δ δγ, exact ord_mul_ord βord (ord_of_mem_ord γord δγ),
have h := sup_norm_fun (@ord_exp_ord _ αord) (@ord_exp_normal _ αord hα) Sne Sord, dsimp at h,
rw [h, ord_exp_limit αβnz γord', sup, repl_img_comp], congr' 1, apply repl_img_ext,
intros δ δγ, rw [function.comp_app, ind δγ], },
change one = α at hα, subst hα,
rw [ord_one_exp βord, ord_one_exp (ord_mul_ord βord γord), ord_one_exp γord],
subst hα, by_cases βγ : β.ord_mul γ = ∅,
rw [βγ, ord_exp_zero], rw ord_mul_eq_zero βord γord at βγ, cases βγ with hβ hγ,
rw [hβ, ord_exp_zero, ord_one_exp γord],
rw [hγ, ord_exp_zero],
rw ord_exp_base_zero βγ, rw ord_mul_eq_zero βord γord at βγ, push_neg at βγ,
rw [ord_exp_base_zero βγ.left, ord_exp_base_zero βγ.right],
end
lemma ord_finite {α : Set} (αord : α.is_ordinal) : α.is_finite ↔ α ∈ ω :=
begin
split,
intro αfin, rw [←ord_not_le_iff_lt omega_is_ord αord, ord_le_iff_sub omega_is_ord αord], intro ωα,
exact nat_infinite (subset_finite_of_finite αfin ωα),
exact nat_finite,
end
-- revisiting countable sets
lemma nat_countable {n : Set} (nω : n ∈ ω) : n.countable :=
countable_iff.mpr (or.inl ((ord_finite (nat_is_ord nω)).mpr nω))
lemma omega_countable : countable ω :=
dominates_self
lemma sub_dom_of_dom {A B : Set} (AB : A ⊆ B) {C : Set} (BC : B ≼ C) : A ≼ C :=
begin
rcases BC with ⟨f, fBC, foto⟩,
refine ⟨f.restrict A, restrict_into_fun fBC AB, restrict_one_to_one fBC.left foto _⟩,
rw fBC.right.left, exact AB,
end
lemma sub_countable {A B : Set} (AB : A ⊆ B) (hB : B.countable) : A.countable :=
sub_dom_of_dom AB hB
theorem prod_countable {A : Set} (hA : A.countable) {B : Set} (hB : B.countable) : (A.prod B).countable :=
begin
rw ←countable_card at *,
apply @card_le_trans _ (B.card.card_mul (card ω)) (mul_cardinal ⟨_, rfl⟩ ⟨_, rfl⟩),
rw [card_mul_comm ⟨_, rfl⟩ ⟨_, rfl⟩, card_mul_spec rfl rfl],
exact card_mul_le_of_le_left ⟨_, rfl⟩ ⟨_, rfl⟩ hA ⟨_, rfl⟩,
nth_rewrite 1 ←aleph_mul_aleph_eq_aleph,
exact card_mul_le_of_le_left ⟨_, rfl⟩ ⟨_, rfl⟩ hB ⟨_, rfl⟩,
end
lemma countable_iff_onto {A : Set} (Ain : A.inhab) : A.countable ↔ ∃ f : Set, f.onto_fun ω A :=
by rw [countable, dominated_iff_exists_onto_fun Ain]
-- Theorem 6Q
theorem Union_countable : ∀ {A : Set}, A.countable → (∀ ⦃B : Set⦄, B ∈ A → B.countable) → A.Union.countable :=
begin
have h₁ : ∀ {A : Set}, ∅ ∉ A → A.countable → (∀ {B : Set}, B ∈ A → B.countable) → A.Union.countable,
intros A h₁ Ac hA,
by_cases Ain : A.inhab,
rw countable_iff_onto Ain at Ac, rcases Ac with ⟨G, Gonto⟩,
let H := pair_sep_eq ω (into_funs ω A.Union).powerset (λ m, {g ∈ into_funs ω (G.fun_value m) | g.onto_fun ω (G.fun_value m)}),
have Hfun : H.is_function := pair_sep_eq_is_fun,
have Hdom : H.dom = ω, apply pair_sep_eq_dom_eq, dsimp, intros n nω, rw mem_powerset,
intros g hg, rw mem_sep at hg, rw mem_into_funs, refine into_of_into_ran_sub _ (into_of_onto hg.right),
apply subset_Union_of_mem, rw ←Gonto.right.right, apply fun_value_def'' Gonto.left,
rw Gonto.right.left, exact nω,
have hH : ∀ m : Set, m ∈ ω → H.fun_value m ≠ ∅, intros m mω,
rw ←Hdom at mω, rw pair_sep_eq_fun_value mω, dsimp, apply ne_empty_of_inhabited,
have GmA : G.fun_value m ∈ A, rw ←Gonto.right.right, apply fun_value_def'' Gonto.left,
rw [Gonto.right.left, ←Hdom], exact mω,
have Gmne : G.fun_value m ≠ ∅, intro Gme, rw Gme at GmA, exact h₁ GmA,
specialize hA GmA, rw countable_iff_onto (inhabited_of_ne_empty Gmne) at hA,
rcases hA with ⟨g, gonto⟩, use g, rw [mem_sep, mem_into_funs],
exact ⟨into_of_onto gonto, gonto⟩,
have memHm : ∀ {m : Set}, m ∈ ω → ∀ {g : Set}, g ∈ H.fun_value m ↔ g.onto_fun ω (G.fun_value m),
intros m mω g, rw ←Hdom at mω, rw pair_sep_eq_fun_value mω, dsimp, rw [mem_sep, mem_into_funs], split,
rintro ⟨-, h⟩, exact h,
intro h, exact ⟨into_of_onto h, h⟩,
obtain ⟨F, Ffun, Fdom, hF⟩ := ax_ch_2 ⟨Hfun, Hdom, hH⟩,
let f := pair_sep_eq (prod ω ω) A.Union (λ z, (F.fun_value z.fst).fun_value z.snd),
have fonto : f.onto_fun (prod ω ω) A.Union, refine ⟨pair_sep_eq_is_fun, pair_sep_eq_dom_eq _, pair_sep_eq_ran_eq _⟩; dsimp,
simp only [mem_prod, mem_Union, exists_prop], rintros z ⟨m, mω, n, nω, zmn⟩, subst zmn, rw [fst_congr, snd_congr],
have h := (memHm mω).mp (hF _ mω),
use G.fun_value m, split,
rw ←Gonto.right.right, apply fun_value_def'' Gonto.left, rw Gonto.right.left, exact mω,
rw ←h.right.right, apply fun_value_def'' h.left, rw h.right.left, exact nω,
simp only [mem_Union, exists_prop], rintros y ⟨B, BA, yB⟩,
rw [←Gonto.right.right, mem_ran_iff Gonto.left] at BA, rcases BA with ⟨m, mω, BGm⟩, subst BGm,
rw Gonto.right.left at mω, specialize hF _ mω, rw memHm mω at hF,
rw [←hF.right.right, mem_ran_iff hF.left] at yB, rcases yB with ⟨n, nω, xe⟩, subst xe,
rw hF.right.left at nω, use m.pair n, simp only [pair_mem_prod, fst_congr, snd_congr],
exact ⟨⟨mω, nω⟩, rfl⟩,
have Ain' : A.Union.inhab, rcases Ain with ⟨B, BA⟩,
have Bin : B.inhab := classical.by_contradiction (λ Bnin,
have Bne : B = ∅ := classical.by_contradiction (λ Bne, Bnin (inhabited_of_ne_empty Bne)),
h₁ (Bne ▸ BA)),
rcases Bin with ⟨x, xB⟩, use x, rw mem_Union, exact ⟨_, BA, xB⟩,
rw countable_iff_onto Ain', obtain ⟨f', fonto', foto'⟩ := nat_prod_nat_equin_nat,
use f.comp f'.inv, refine ⟨T3H_a fonto.left (T3F_a.mpr foto'), _, _⟩,
have h : f'.inv.ran ⊆ f.dom, rw [T3E_b, fonto'.right.left, fonto.right.left], exact subset_self,
rw [dom_comp h, T3E_a, fonto'.right.right],
have h : f.dom ⊆ f'.inv.ran, rw [T3E_b, fonto'.right.left, fonto.right.left], exact subset_self,
rw [ran_comp h, fonto.right.right],
have h : A = ∅ := classical.by_contradiction (λ Ane, Ain (inhabited_of_ne_empty Ane)),
subst h, rw union_empty_eq_empty, exact nat_countable zero_nat,
intros A Ac hA, rw Union_diff_empty_eq,
refine h₁ _ (sub_countable subset_diff Ac) _,
intro h, rw [mem_diff, mem_singleton] at h, exact h.right rfl,
intros B hB, rw [mem_diff, mem_singleton] at hB, exact hA hB.left,
end
theorem union_countable {A : Set} (Ac : A.countable) {B : Set} (Bc : B.countable) : (A ∪ B).countable :=
begin
rw union_eq_Union, apply Union_countable, rw ←countable_card,
by_cases h : A ∈ {B},
have h : ({A, B} : Set) = {A}, rw mem_singleton at h, apply ext, simp only [mem_insert, mem_singleton], finish,
rw [h, card_singleton, ←card_nat one_nat, countable_card], exact nat_countable one_nat,
rw [card_insert h, card_singleton, card_add_one_eq_succ (finite_cardinal_iff_nat.mpr one_nat)],
change two.card_le (card ω), rw [←card_nat two_nat, countable_card], exact nat_countable two_nat,
intros z hz, rw [mem_insert, mem_singleton] at hz, finish,
end
theorem singleton_countable {A : Set} : countable {A} :=
begin
rw countable_iff, exact or.inl singleton_finite,
end
theorem succ_countable {A : Set} (Ac : A.countable) : A.succ.countable :=
by rw succ; exact union_countable singleton_countable Ac
theorem countable_of_succ {A : Set} (Ac : A.succ.countable) : A.countable :=
sub_countable self_sub_succ Ac
lemma repl_img_dom {A : Set} {f : Set → Set} : repl_img f A ≼ A :=
begin
apply dominates_of_onto_fun, use pair_sep_eq A (repl_img f A) f,
refine ⟨pair_sep_eq_is_fun, pair_sep_eq_dom_eq _, pair_sep_eq_ran_eq _⟩,
intros x xA, rw mem_repl_img, finish,
apply of_repl_img, finish,
end
lemma repl_img_card_le {A : Set} {f : Set → Set} : (repl_img f A).card.card_le A.card :=
card_le_iff_equin'.mpr repl_img_dom
lemma dom_trans {A B : Set} (AB : A ≼ B) {C : Set} (BC : B ≼ C) : A ≼ C :=
by rw ←card_le_iff_equin' at AB BC ⊢; exact card_le_trans ⟨_, rfl⟩ AB BC
lemma repl_img_countable {A : Set} (Ac : A.countable) {f : Set → Set} : (repl_img f A).countable :=
dom_trans repl_img_dom Ac
theorem countable_of_mem_countable_ord {α : Set} (αord : α.is_ordinal) (αc : α.countable)
{β : Set} (βα : β ∈ α) : β.countable :=
sub_countable ((ord_le_iff_sub (ord_of_mem_ord αord βα) αord).mp (or.inl βα)) αc
theorem ord_add_countable {α : Set} (αord : α.is_ordinal) (αc : α.countable)
{β : Set} (βord : β.is_ordinal) (βc : β.countable) : (α.ord_add β).countable :=
begin
revert β, apply @trans_ind_schema (λ β, β.countable → (α.ord_add β).countable),
intros β βord ind βc, dsimp at ind, rcases ord_cases βord with (βz|(⟨δ, βδ⟩|βord')),
{ subst βz, rw ord_add_zero αord, exact αc, },
{ subst βδ, have δord := ord_of_succ_ord βord, rw ord_add_succ αord δord,
exact succ_countable (ind self_mem_succ (countable_of_succ βc)), },
{ rw ord_add_limit αord βord', exact Union_countable (repl_img_countable βc)
(of_repl_img (λ δ δβ, ind δβ (countable_of_mem_countable_ord βord βc δβ))), },
end
theorem ord_mul_countable {α : Set} (αord : α.is_ordinal) (αc : α.countable)
{β : Set} (βord : β.is_ordinal) (βc : β.countable) : (α.ord_mul β).countable :=
begin
revert β, apply @trans_ind_schema (λ β, β.countable → (α.ord_mul β).countable),
intros β βord ind βc, dsimp at ind, rcases ord_cases βord with (βz|(⟨δ, βδ⟩|βord')),
{ subst βz, rw ord_mul_zero αord, exact nat_countable zero_nat, },
{ subst βδ, have δord := ord_of_succ_ord βord, rw ord_mul_succ αord δord,
exact ord_add_countable (ord_mul_ord αord δord) (ind self_mem_succ (countable_of_succ βc)) αord αc, },
{ rw ord_mul_limit αord βord', exact Union_countable (repl_img_countable βc)
(of_repl_img (λ δ δβ, ind δβ (countable_of_mem_countable_ord βord βc δβ))), },
end
theorem ord_exp_countable {α : Set} (αord : α.is_ordinal) (αc : α.countable)
{β : Set} (βord : β.is_ordinal) (βc : β.countable) : (α.ord_exp β).countable :=
begin
revert β, apply @trans_ind_schema (λ β, β.countable → (α.ord_exp β).countable),
intros β βord ind βc, dsimp at ind, rcases ord_cases βord with (βz|(⟨δ, βδ⟩|βord')),
{ subst βz, rw ord_exp_zero, exact nat_countable one_nat, },
{ subst βδ, have δord := ord_of_succ_ord βord, rw ord_exp_succ δord,
exact ord_mul_countable (ord_exp_ord αord δord) (ind self_mem_succ (countable_of_succ βc)) αord αc, },
{ by_cases αz : α = ∅,
subst αz, rw ord_exp_base_zero βord'.ne, exact nat_countable zero_nat,
rw ord_exp_limit αz βord', exact Union_countable (repl_img_countable βc)
(of_repl_img (λ δ δβ, ind δβ (countable_of_mem_countable_ord βord βc δβ))), },
end
end Set |
bc0a7a1e8e90dae54aca3f4c5214ca8caff5e03a | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /library/data/list/set.lean | f0755c661275c28ca08136da635ed6ff671b414d | [
"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 | 37,983 | lean | /-
Copyright (c) 2015 Leonardo de Moura. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
Set-like operations on lists
-/
import data.list.basic data.list.comb
open nat function decidable
namespace list
section erase
variable {A : Type}
variable [H : decidable_eq A]
include H
definition erase (a : A) : list A → list A
| [] := []
| (b::l) :=
match (H a b) with
| (tt e) := l
| (ff n) := b :: erase l
end
lemma erase_nil (a : A) : erase a [] = [] :=
rfl
lemma erase_cons_head (a : A) (l : list A) : erase a (a :: l) = l :=
sorry
/-
show match H a a with | inl e := l | inr n := a :: erase a l end = l,
by rewrite decidable_eq_inl_refl
-/
lemma erase_cons_tail {a b : A} (l : list A) : a ≠ b → erase a (b::l) = b :: erase a l :=
sorry
/-
assume h : a ≠ b,
show match H a b with | inl e := l | inr n₁ := b :: erase a l end = b :: erase a l,
by rewrite (decidable_eq_inr_neg h)
-/
lemma length_erase_of_mem {a : A} : ∀ {l}, a ∈ l → length (erase a l) = pred (length l)
:= sorry
/-
| [] h := rfl
| [x] h := by rewrite [mem_singleton h, erase_cons_head]
| (x::y::xs) h :=
by_cases
(suppose a = x, by rewrite [this, erase_cons_head])
(suppose a ≠ x,
have ainyxs : a ∈ y::xs, from or_resolve_right h this,
by rewrite [erase_cons_tail _ this, *length_cons, length_erase_of_mem ainyxs])
-/
lemma length_erase_of_not_mem {a : A} : ∀ {l}, a ∉ l → length (erase a l) = length l
:= sorry
/-
| [] h := rfl
| (x::xs) h :=
have anex : a ≠ x, from λ aeqx : a = x, absurd (or.inl aeqx) h,
have aninxs : a ∉ xs, from λ ainxs : a ∈ xs, absurd (or.inr ainxs) h,
by rewrite [erase_cons_tail _ anex, length_cons, length_erase_of_not_mem aninxs]
-/
lemma erase_append_left {a : A} : ∀ {l₁} (l₂), a ∈ l₁ → erase a (l₁++l₂) = erase a l₁ ++ l₂
:= sorry
/-
| [] l₂ h := absurd h !not_mem_nil
| (x::xs) l₂ h :=
by_cases
(λ aeqx : a = x, by rewrite [aeqx, append_cons, *erase_cons_head])
(λ anex : a ≠ x,
have ainxs : a ∈ xs, from mem_of_ne_of_mem anex h,
by rewrite [append_cons, *erase_cons_tail _ anex, erase_append_left l₂ ainxs])
-/
lemma erase_append_right {a : A} : ∀ {l₁} (l₂), a ∉ l₁ → erase a (l₁++l₂) = l₁ ++ erase a l₂
:= sorry
/-
| [] l₂ h := rfl
| (x::xs) l₂ h :=
by_cases
(λ aeqx : a = x, by rewrite aeqx at h; exact (absurd !mem_cons h))
(λ anex : a ≠ x,
have nainxs : a ∉ xs, from not_mem_of_not_mem_cons h,
by rewrite [append_cons, *erase_cons_tail _ anex, erase_append_right l₂ nainxs])
-/
lemma erase_sub (a : A) : ∀ l, erase a l ⊆ l
:= sorry
/-
| [] := λ x xine, xine
| (x::xs) := λ y xine,
by_cases
(λ aeqx : a = x, by rewrite [aeqx at xine, erase_cons_head at xine]; exact (or.inr xine))
(λ anex : a ≠ x,
have yinxe : y ∈ x :: erase a xs, by rewrite [erase_cons_tail _ anex at xine]; exact xine,
have subxs : erase a xs ⊆ xs, from erase_sub xs,
by_cases
(λ yeqx : y = x, by rewrite yeqx; apply mem_cons)
(λ ynex : y ≠ x,
have yine : y ∈ erase a xs, from mem_of_ne_of_mem ynex yinxe,
have yinxs : y ∈ xs, from subxs yine,
or.inr yinxs))
-/
theorem mem_erase_of_ne_of_mem {a b : A} : ∀ {l : list A}, a ≠ b → a ∈ l → a ∈ erase b l
:= sorry
/-
| [] n i := absurd i !not_mem_nil
| (c::l) n i := by_cases
(λ beqc : b = c,
have ainl : a ∈ l, from or.elim (eq_or_mem_of_mem_cons i)
(λ aeqc : a = c, absurd aeqc (beqc ▸ n))
(λ ainl : a ∈ l, ainl),
by rewrite [beqc, erase_cons_head]; exact ainl)
(λ bnec : b ≠ c, by_cases
(λ aeqc : a = c,
have aux : a ∈ c :: erase b l, by rewrite [aeqc]; exact !mem_cons,
by rewrite [erase_cons_tail _ bnec]; exact aux)
(λ anec : a ≠ c,
have ainl : a ∈ l, from mem_of_ne_of_mem anec i,
have ainel : a ∈ erase b l, from mem_erase_of_ne_of_mem n ainl,
have aux : a ∈ c :: erase b l, from mem_cons_of_mem _ ainel,
by rewrite [erase_cons_tail _ bnec]; exact aux)) --
-/
theorem mem_of_mem_erase {a b : A} : ∀ {l}, a ∈ erase b l → a ∈ l
:= sorry
/-
| [] i := absurd i !not_mem_nil
| (c::l) i := by_cases
(λ beqc : b = c, by rewrite [beqc at i, erase_cons_head at i]; exact (mem_cons_of_mem _ i))
(λ bnec : b ≠ c,
have i₁ : a ∈ c :: erase b l, by rewrite [erase_cons_tail _ bnec at i]; exact i,
or.elim (eq_or_mem_of_mem_cons i₁)
(λ aeqc : a = c, by rewrite [aeqc]; exact !mem_cons)
(λ ainel : a ∈ erase b l,
have ainl : a ∈ l, from mem_of_mem_erase ainel,
mem_cons_of_mem _ ainl))
-/
theorem all_erase_of_all {p : A → Prop} (a : A) : ∀ {l}, all l p → all (erase a l) p
:= sorry
/-
| [] h := by rewrite [erase_nil]; exact h
| (b::l) h :=
have h₁ : all l p, from all_of_all_cons h,
have h₂ : all (erase a l) p, from all_erase_of_all h₁,
have pb : p b, from of_all_cons h,
have h₃ : all (b :: erase a l) p, from all_cons_of_all pb h₂,
by_cases
(λ aeqb : a = b, by rewrite [aeqb, erase_cons_head]; exact h₁)
(λ aneb : a ≠ b, by rewrite [erase_cons_tail _ aneb]; exact h₃)
-/
end erase
/- disjoint -/
section disjoint
variable {A : Type}
definition disjoint (l₁ l₂ : list A) : Prop := ∀ ⦃a⦄, (a ∈ l₁ → a ∈ l₂ → false)
lemma disjoint_left {l₁ l₂ : list A} : disjoint l₁ l₂ → ∀ {a}, a ∈ l₁ → a ∉ l₂ :=
λ d a, d a
lemma disjoint_right {l₁ l₂ : list A} : disjoint l₁ l₂ → ∀ {a}, a ∈ l₂ → a ∉ l₁ :=
λ d a i₂ i₁, d a i₁ i₂
lemma disjoint.comm {l₁ l₂ : list A} : disjoint l₁ l₂ → disjoint l₂ l₁ :=
λ d a i₂ i₁, d a i₁ i₂
lemma disjoint_of_disjoint_cons_left {a : A} {l₁ l₂} : disjoint (a::l₁) l₂ → disjoint l₁ l₂ :=
λ d x xinl₁, disjoint_left d (or.inr xinl₁)
lemma disjoint_of_disjoint_cons_right {a : A} {l₁ l₂} : disjoint l₁ (a::l₂) → disjoint l₁ l₂ :=
λ d, disjoint.comm (disjoint_of_disjoint_cons_left (disjoint.comm d))
lemma disjoint_nil_left (l : list A) : disjoint [] l :=
λ a ab, absurd ab (not_mem_nil a)
lemma disjoint_nil_right (l : list A) : disjoint l [] :=
disjoint.comm (disjoint_nil_left l)
lemma disjoint_cons_of_not_mem_of_disjoint {a : A} {l₁ l₂} : a ∉ l₂ → disjoint l₁ l₂ → disjoint (a::l₁) l₂ :=
λ nainl₂ d x (xinal₁ : x ∈ a::l₁),
or.elim (eq_or_mem_of_mem_cons xinal₁)
(λ xeqa : x = a, eq.symm xeqa ▸ nainl₂)
(λ xinl₁ : x ∈ l₁, disjoint_left d xinl₁)
lemma disjoint_of_disjoint_append_left_left : ∀ {l₁ l₂ l : list A}, disjoint (l₁++l₂) l → disjoint l₁ l
| [] l₂ l d := disjoint_nil_left l
| (x::xs) l₂ l d :=
have nxinl : x ∉ l, from disjoint_left d (mem_cons x _),
have d₁ : disjoint (xs++l₂) l, from disjoint_of_disjoint_cons_left d,
have d₂ : disjoint xs l, from disjoint_of_disjoint_append_left_left d₁,
disjoint_cons_of_not_mem_of_disjoint nxinl d₂
lemma disjoint_of_disjoint_append_left_right : ∀ {l₁ l₂ l : list A}, disjoint (l₁++l₂) l → disjoint l₂ l
| [] l₂ l d := d
| (x::xs) l₂ l d :=
have d₁ : disjoint (xs++l₂) l, from disjoint_of_disjoint_cons_left d,
disjoint_of_disjoint_append_left_right d₁
lemma disjoint_of_disjoint_append_right_left : ∀ {l₁ l₂ l : list A}, disjoint l (l₁++l₂) → disjoint l l₁ :=
λ l₁ l₂ l d, disjoint.comm (disjoint_of_disjoint_append_left_left (disjoint.comm d))
lemma disjoint_of_disjoint_append_right_right : ∀ {l₁ l₂ l : list A}, disjoint l (l₁++l₂) → disjoint l l₂ :=
λ l₁ l₂ l d, disjoint.comm (disjoint_of_disjoint_append_left_right (disjoint.comm d))
end disjoint
/- no duplicates predicate -/
inductive nodup {A : Type} : list A → Prop :=
| ndnil : nodup []
| ndcons : ∀ {a l}, a ∉ l → nodup l → nodup (a::l)
section nodup
open nodup
variables {A B : Type}
theorem nodup_nil : @nodup A [] :=
ndnil
theorem nodup_cons {a : A} {l : list A} : a ∉ l → nodup l → nodup (a::l) :=
λ i n, ndcons i n
theorem nodup_singleton (a : A) : nodup [a] :=
nodup_cons (not_mem_nil a) nodup_nil
theorem nodup_of_nodup_cons : ∀ {a : A} {l : list A}, nodup (a::l) → nodup l
| a xs (ndcons i n) := n
theorem not_mem_of_nodup_cons : ∀ {a : A} {l : list A}, nodup (a::l) → a ∉ l
| a xs (ndcons i n) := i
theorem not_nodup_cons_of_mem {a : A} {l : list A} : a ∈ l → ¬ nodup (a :: l) :=
λ ainl d, absurd ainl (not_mem_of_nodup_cons d)
theorem not_nodup_cons_of_not_nodup {a : A} {l : list A} : ¬ nodup l → ¬ nodup (a :: l) :=
λ nd d, absurd (nodup_of_nodup_cons d) nd
theorem nodup_of_nodup_append_left : ∀ {l₁ l₂ : list A}, nodup (l₁++l₂) → nodup l₁
| [] l₂ n := nodup_nil
| (x::xs) l₂ n :=
have ndxs : nodup xs, from nodup_of_nodup_append_left (nodup_of_nodup_cons n),
have nxinxsl₂ : x ∉ xs++l₂, from not_mem_of_nodup_cons n,
have nxinxs : x ∉ xs, from not_mem_of_not_mem_append_left nxinxsl₂,
nodup_cons nxinxs ndxs
theorem nodup_of_nodup_append_right : ∀ {l₁ l₂ : list A}, nodup (l₁++l₂) → nodup l₂
| [] l₂ n := n
| (x::xs) l₂ n := nodup_of_nodup_append_right (nodup_of_nodup_cons n)
theorem disjoint_of_nodup_append : ∀ {l₁ l₂ : list A}, nodup (l₁++l₂) → disjoint l₁ l₂
| [] l₂ d := disjoint_nil_left l₂
| (x::xs) l₂ d :=
have nodup (x::(xs++l₂)), from d,
have x ∉ xs++l₂, from not_mem_of_nodup_cons this,
have nxinl₂ : x ∉ l₂, from not_mem_of_not_mem_append_right this,
take a, suppose a ∈ x::xs,
or.elim (eq_or_mem_of_mem_cons this)
(suppose a = x, eq.symm this ▸ nxinl₂)
(suppose ainxs : a ∈ xs,
have nodup (x::(xs++l₂)), from d,
have nodup (xs++l₂), from nodup_of_nodup_cons this,
have disjoint xs l₂, from disjoint_of_nodup_append this,
disjoint_left this ainxs)
theorem nodup_append_of_nodup_of_nodup_of_disjoint : ∀ {l₁ l₂ : list A}, nodup l₁ → nodup l₂ → disjoint l₁ l₂ → nodup (l₁++l₂)
| [] l₂ d₁ d₂ dsj := sorry -- by rewrite [append_nil_left]; exact d₂
| (x::xs) l₂ d₁ d₂ dsj :=
have ndxs : nodup xs, from nodup_of_nodup_cons d₁,
have disjoint xs l₂, from disjoint_of_disjoint_cons_left dsj,
have ndxsl₂ : nodup (xs++l₂), from nodup_append_of_nodup_of_nodup_of_disjoint ndxs d₂ this,
have nxinxs : x ∉ xs, from not_mem_of_nodup_cons d₁,
have x ∉ l₂, from disjoint_left dsj (mem_cons x xs),
have x ∉ xs++l₂, from not_mem_append nxinxs this,
nodup_cons this ndxsl₂
theorem nodup_app_comm {l₁ l₂ : list A} (d : nodup (l₁++l₂)) : nodup (l₂++l₁) :=
have d₁ : nodup l₁, from nodup_of_nodup_append_left d,
have d₂ : nodup l₂, from nodup_of_nodup_append_right d,
have dsj : disjoint l₁ l₂, from disjoint_of_nodup_append d,
nodup_append_of_nodup_of_nodup_of_disjoint d₂ d₁ (disjoint.comm dsj)
theorem nodup_head {a : A} {l₁ l₂ : list A} (d : nodup (l₁++(a::l₂))) : nodup (a::(l₁++l₂)) :=
have d₁ : nodup (a::(l₂++l₁)), from nodup_app_comm d,
have d₂ : nodup (l₂++l₁), from nodup_of_nodup_cons d₁,
have d₃ : nodup (l₁++l₂), from nodup_app_comm d₂,
have nain : a ∉ l₂++l₁, from not_mem_of_nodup_cons d₁,
have nain₂ : a ∉ l₂, from not_mem_of_not_mem_append_left nain,
have nain₁ : a ∉ l₁, from not_mem_of_not_mem_append_right nain,
nodup_cons (not_mem_append nain₁ nain₂) d₃
theorem nodup_middle {a : A} {l₁ l₂ : list A} (d : nodup (a::(l₁++l₂))) : nodup (l₁++(a::l₂)) :=
have d₁ : nodup (l₁++l₂), from nodup_of_nodup_cons d,
have nain : a ∉ l₁++l₂, from not_mem_of_nodup_cons d,
have disj : disjoint l₁ l₂, from disjoint_of_nodup_append d₁,
have d₂ : nodup l₁, from nodup_of_nodup_append_left d₁,
have d₃ : nodup l₂, from nodup_of_nodup_append_right d₁,
have nain₂ : a ∉ l₂, from not_mem_of_not_mem_append_right nain,
have nain₁ : a ∉ l₁, from not_mem_of_not_mem_append_left nain,
have d₄ : nodup (a::l₂), from nodup_cons nain₂ d₃,
have disj₂ : disjoint l₁ (a::l₂), from disjoint.comm (disjoint_cons_of_not_mem_of_disjoint nain₁ (disjoint.comm disj)),
nodup_append_of_nodup_of_nodup_of_disjoint d₂ d₄ disj₂
theorem nodup_map {f : A → B} (inj : injective f) : ∀ {l : list A}, nodup l → nodup (map f l)
:= sorry
/-
| [] n := begin rewrite [map_nil], apply nodup_nil end
| (x::xs) n :=
have nxinxs : x ∉ xs, from not_mem_of_nodup_cons n,
have ndxs : nodup xs, from nodup_of_nodup_cons n,
have ndmfxs : nodup (map f xs), from nodup_map ndxs,
have nfxinm : f x ∉ map f xs, from
λ ab : f x ∈ map f xs,
obtain (y : A) (yinxs : y ∈ xs) (fyfx : f y = f x), from exists_of_mem_map ab,
have yeqx : y = x, from inj fyfx,
by subst y; contradiction,
nodup_cons nfxinm ndmfxs
-/
theorem nodup_erase_of_nodup [decidable_eq A] (a : A) : ∀ {l}, nodup l → nodup (erase a l)
:= sorry
/-
| [] n := nodup_nil
| (b::l) n := by_cases
(λ aeqb : a = b, by rewrite [aeqb, erase_cons_head]; exact (nodup_of_nodup_cons n))
(λ aneb : a ≠ b,
have nbinl : b ∉ l, from not_mem_of_nodup_cons n,
have ndl : nodup l, from nodup_of_nodup_cons n,
have ndeal : nodup (erase a l), from nodup_erase_of_nodup ndl,
have nbineal : b ∉ erase a l, from λ i, absurd (erase_sub _ _ i) nbinl,
have aux : nodup (b :: erase a l), from nodup_cons nbineal ndeal,
by rewrite [erase_cons_tail _ aneb]; exact aux)
-/
theorem mem_erase_of_nodup [decidable_eq A] (a : A) : ∀ {l}, nodup l → a ∉ erase a l
:= sorry
/-
| [] n := !not_mem_nil
| (b::l) n :=
have ndl : nodup l, from nodup_of_nodup_cons n,
have naineal : a ∉ erase a l, from mem_erase_of_nodup ndl,
have nbinl : b ∉ l, from not_mem_of_nodup_cons n,
by_cases
(λ aeqb : a = b, by rewrite [aeqb, erase_cons_head]; exact nbinl)
(λ aneb : a ≠ b,
have aux : a ∉ b :: erase a l, from
assume ainbeal : a ∈ b :: erase a l, or.elim (eq_or_mem_of_mem_cons ainbeal)
(λ aeqb : a = b, absurd aeqb aneb)
(λ aineal : a ∈ erase a l, absurd aineal naineal),
by rewrite [erase_cons_tail _ aneb]; exact aux)
-/
definition erase_dup [decidable_eq A] : list A → list A
| [] := []
| (x :: xs) := if x ∈ xs then erase_dup xs else x :: erase_dup xs
theorem erase_dup_nil [decidable_eq A] : erase_dup [] = ([] : list A) := rfl
theorem erase_dup_cons_of_mem [decidable_eq A] {a : A} {l : list A} : a ∈ l → erase_dup (a::l) = erase_dup l :=
assume ainl, calc
erase_dup (a::l) = if a ∈ l then erase_dup l else a :: erase_dup l : rfl
... = erase_dup l : if_pos ainl
theorem erase_dup_cons_of_not_mem [decidable_eq A] {a : A} {l : list A} : a ∉ l → erase_dup (a::l) = a :: erase_dup l :=
assume nainl, calc
erase_dup (a::l) = if a ∈ l then erase_dup l else a :: erase_dup l : rfl
... = a :: erase_dup l : if_neg nainl
theorem mem_erase_dup [decidable_eq A] {a : A} : ∀ {l}, a ∈ l → a ∈ erase_dup l
:= sorry
/-
| [] h := absurd h !not_mem_nil
| (b::l) h := by_cases
(λ binl : b ∈ l, or.elim (eq_or_mem_of_mem_cons h)
(λ aeqb : a = b, by rewrite [erase_dup_cons_of_mem binl, -aeqb at binl]; exact (mem_erase_dup binl))
(λ ainl : a ∈ l, by rewrite [erase_dup_cons_of_mem binl]; exact (mem_erase_dup ainl)))
(λ nbinl : b ∉ l, or.elim (eq_or_mem_of_mem_cons h)
(λ aeqb : a = b, by rewrite [erase_dup_cons_of_not_mem nbinl, aeqb]; exact !mem_cons)
(λ ainl : a ∈ l, by rewrite [erase_dup_cons_of_not_mem nbinl]; exact (or.inr (mem_erase_dup ainl))))
-/
theorem mem_of_mem_erase_dup [decidable_eq A] {a : A} : ∀ {l}, a ∈ erase_dup l → a ∈ l
:= sorry
/-
| [] h := by rewrite [erase_dup_nil at h]; exact h
| (b::l) h := by_cases
(λ binl : b ∈ l,
have h₁ : a ∈ erase_dup l, by rewrite [erase_dup_cons_of_mem binl at h]; exact h,
or.inr (mem_of_mem_erase_dup h₁))
(λ nbinl : b ∉ l,
have h₁ : a ∈ b :: erase_dup l, by rewrite [erase_dup_cons_of_not_mem nbinl at h]; exact h,
or.elim (eq_or_mem_of_mem_cons h₁)
(λ aeqb : a = b, by rewrite aeqb; exact !mem_cons)
(λ ainel : a ∈ erase_dup l, or.inr (mem_of_mem_erase_dup ainel)))
-/
theorem erase_dup_sub [decidable_eq A] (l : list A) : erase_dup l ⊆ l :=
λ a i, mem_of_mem_erase_dup i
theorem sub_erase_dup [decidable_eq A] (l : list A) : l ⊆ erase_dup l :=
λ a i, mem_erase_dup i
theorem nodup_erase_dup [decidable_eq A] : ∀ l : list A, nodup (erase_dup l)
:= sorry
/-
| [] := by rewrite erase_dup_nil; exact nodup_nil
| (a::l) := by_cases
(λ ainl : a ∈ l, by rewrite [erase_dup_cons_of_mem ainl]; exact (nodup_erase_dup l))
(λ nainl : a ∉ l,
have r : nodup (erase_dup l), from nodup_erase_dup l,
have nin : a ∉ erase_dup l, from
assume ab : a ∈ erase_dup l, absurd (mem_of_mem_erase_dup ab) nainl,
by rewrite [erase_dup_cons_of_not_mem nainl]; exact (nodup_cons nin r))
-/
theorem erase_dup_eq_of_nodup [decidable_eq A] : ∀ {l : list A}, nodup l → erase_dup l = l
| [] d := rfl
| (a::l) d :=
have nainl : a ∉ l, from not_mem_of_nodup_cons d,
have dl : nodup l, from nodup_of_nodup_cons d,
sorry -- by rewrite [erase_dup_cons_of_not_mem nainl, erase_dup_eq_of_nodup dl]
attribute [instance]
definition decidable_nodup [decidable_eq A] : ∀ (l : list A), decidable (nodup l)
| [] := tt nodup_nil
| (a::l) :=
match (decidable_mem a l) with
| (tt p) := ff (not_nodup_cons_of_mem p)
| (ff n) :=
match (decidable_nodup l) with
| (tt nd) := tt (nodup_cons n nd)
| (ff d) := ff (not_nodup_cons_of_not_nodup d)
end
end
theorem nodup_product : ∀ {l₁ : list A} {l₂ : list B}, nodup l₁ → nodup l₂ → nodup (product l₁ l₂)
| [] l₂ n₁ n₂ := nodup_nil
| (a::l₁) l₂ n₁ n₂ :=
have nainl₁ : a ∉ l₁, from not_mem_of_nodup_cons n₁,
have n₃ : nodup l₁, from nodup_of_nodup_cons n₁,
have n₄ : nodup (product l₁ l₂), from nodup_product n₃ n₂,
have dgen : ∀ l, nodup l → nodup (map (λ b, (a, b)) l)
| [] h := nodup_nil
| (x::l) h :=
have dl : nodup l, from nodup_of_nodup_cons h,
have dm : nodup (map (λ b, (a, b)) l), from dgen l dl,
have nxin : x ∉ l, from not_mem_of_nodup_cons h,
have npin : (a, x) ∉ map (λ b, (a, b)) l, from
assume pin, absurd (mem_of_mem_map_pair₁ pin) nxin,
nodup_cons npin dm,
have dm : nodup (map (λ b, (a, b)) l₂), from dgen l₂ n₂,
have dsj : disjoint (map (λ b, (a, b)) l₂) (product l₁ l₂), from
λ p, match p with
| (a₁, b₁) :=
λ (i₁ : (a₁, b₁) ∈ map (λ b, (a, b)) l₂) (i₂ : (a₁, b₁) ∈ product l₁ l₂),
have a₁inl₁ : a₁ ∈ l₁, from mem_of_mem_product_left i₂,
have a₁eqa : a₁ = a, from eq_of_mem_map_pair₁ i₁,
absurd (a₁eqa ▸ a₁inl₁) nainl₁
end,
nodup_append_of_nodup_of_nodup_of_disjoint dm n₄ dsj
theorem nodup_filter (p : A → Prop) [decidable_pred p] : ∀ {l : list A}, nodup l → nodup (filter p l)
:= sorry
/-
| [] nd := nodup_nil
| (a::l) nd :=
have nainl : a ∉ l, from not_mem_of_nodup_cons nd,
have ndl : nodup l, from nodup_of_nodup_cons nd,
have ndf : nodup (filter p l), from nodup_filter ndl,
have nainf : a ∉ filter p l, from
assume ainf, absurd (mem_of_mem_filter ainf) nainl,
by_cases
(λ pa : p a, by rewrite [filter_cons_of_pos _ pa]; exact (nodup_cons nainf ndf))
(λ npa : ¬ p a, by rewrite [filter_cons_of_neg _ npa]; exact ndf)
-/
lemma dmap_nodup_of_dinj {p : A → Prop} [h : decidable_pred p] {f : Π a, p a → B} (Pdi : dinj p f):
∀ {l : list A}, nodup l → nodup (dmap p f l)
:= sorry
/-
| [] := take P, nodup.ndnil
| (a::l) := take Pnodup,
decidable.rec_on (h a)
(λ Pa,
begin
rewrite [dmap_cons_of_pos Pa],
apply nodup_cons,
apply (not_mem_dmap_of_dinj_of_not_mem Pdi Pa),
exact not_mem_of_nodup_cons Pnodup,
exact dmap_nodup_of_dinj (nodup_of_nodup_cons Pnodup)
end)
(λ nPa,
begin
rewrite [dmap_cons_of_neg nPa],
exact dmap_nodup_of_dinj (nodup_of_nodup_cons Pnodup)
end)
-/
end nodup
/- upto -/
definition upto : nat → list nat
| 0 := []
| (n+1) := n :: upto n
theorem upto_nil : upto 0 = nil := rfl
theorem upto_succ (n : nat) : upto (succ n) = n :: upto n := rfl
theorem length_upto : ∀ n, length (upto n) = n
:= sorry
/-
| 0 := rfl
| (succ n) := by rewrite [upto_succ, length_cons, length_upto]
-/
theorem upto_ne_nil_of_ne_zero {n : ℕ} (H : n ≠ 0) : upto n ≠ nil :=
sorry
/-
suppose upto n = nil,
have upto n = upto 0, from upto_nil ▸ this,
have n = 0, from calc
n = length (upto n) : by rewrite length_upto
... = length (upto 0) : by rewrite this
... = 0 : by rewrite length_upto,
H this
-/
theorem upto_less : ∀ n, all (upto n) (λ i, i < n)
| 0 := trivial
| (succ n) :=
have alln : all (upto n) (λ i, i < n), from upto_less n,
all_cons_of_all (lt.base n) (all_implies alln (λ x h, lt.step h))
theorem nodup_upto : ∀ n, nodup (upto n)
| 0 := nodup_nil
| (n+1) :=
have d : nodup (upto n), from nodup_upto n,
have n : n ∉ upto n, from
assume i : n ∈ upto n, absurd (of_mem_of_all i (upto_less n)) (nat.lt_irrefl n),
nodup_cons n d
theorem lt_of_mem_upto {n i : nat} : i ∈ upto n → i < n :=
assume i, of_mem_of_all i (upto_less n)
theorem mem_upto_succ_of_mem_upto {n i : nat} : i ∈ upto n → i ∈ upto (succ n) :=
assume i, mem_cons_of_mem _ i
theorem mem_upto_of_lt : ∀ {n i : nat}, i < n → i ∈ upto n
| 0 i h := absurd h (not_lt_zero i)
| (succ n) i h :=
sorry
/-
begin
cases h with m h',
{ rewrite upto_succ, apply mem_cons},
{ exact mem_upto_succ_of_mem_upto (mem_upto_of_lt h')}
end
-/
lemma upto_step : ∀ {n : nat}, upto (succ n) = (map succ (upto n))++[0]
| 0 := rfl
| (succ n) := sorry -- begin rewrite [upto_succ n, map_cons, append_cons, -upto_step] end
/- union -/
section union
variable {A : Type}
variable [H : decidable_eq A]
include H
definition union : list A → list A → list A
| [] l₂ := l₂
| (a::l₁) l₂ := if a ∈ l₂ then union l₁ l₂ else a :: union l₁ l₂
theorem nil_union (l : list A) : union [] l = l := rfl
theorem union_cons_of_mem {a : A} {l₂} : ∀ (l₁), a ∈ l₂ → union (a::l₁) l₂ = union l₁ l₂ :=
take l₁, assume ainl₂, calc
union (a::l₁) l₂ = if a ∈ l₂ then union l₁ l₂ else a :: union l₁ l₂ : rfl
... = union l₁ l₂ : if_pos ainl₂
theorem union_cons_of_not_mem {a : A} {l₂} : ∀ (l₁), a ∉ l₂ → union (a::l₁) l₂ = a :: union l₁ l₂ :=
take l₁, assume nainl₂, calc
union (a::l₁) l₂ = if a ∈ l₂ then union l₁ l₂ else a :: union l₁ l₂ : rfl
... = a :: union l₁ l₂ : if_neg nainl₂
theorem union_nil : ∀ (l : list A), union l [] = l
| [] := nil_union nil
| (a::l) := sorry -- by rewrite [union_cons_of_not_mem _ !not_mem_nil, union_nil]
theorem mem_or_mem_of_mem_union : ∀ {l₁ l₂} {a : A}, a ∈ union l₁ l₂ → a ∈ l₁ ∨ a ∈ l₂
:= sorry
/-
| [] l₂ a ainl₂ := by rewrite nil_union at ainl₂; exact (or.inr (ainl₂))
| (b::l₁) l₂ a ainbl₁l₂ := by_cases
(λ binl₂ : b ∈ l₂,
have ainl₁l₂ : a ∈ union l₁ l₂, by rewrite [union_cons_of_mem l₁ binl₂ at ainbl₁l₂]; exact ainbl₁l₂,
or.elim (mem_or_mem_of_mem_union ainl₁l₂)
(λ ainl₁, or.inl (mem_cons_of_mem _ ainl₁))
(λ ainl₂, or.inr ainl₂))
(λ nbinl₂ : b ∉ l₂,
have ainb_l₁l₂ : a ∈ b :: union l₁ l₂, by rewrite [union_cons_of_not_mem l₁ nbinl₂ at ainbl₁l₂]; exact ainbl₁l₂,
or.elim (eq_or_mem_of_mem_cons ainb_l₁l₂)
(λ aeqb, by rewrite aeqb; exact (or.inl !mem_cons))
(λ ainl₁l₂,
or.elim (mem_or_mem_of_mem_union ainl₁l₂)
(λ ainl₁, or.inl (mem_cons_of_mem _ ainl₁))
(λ ainl₂, or.inr ainl₂)))
-/
theorem mem_union_right {a : A} : ∀ (l₁) {l₂}, a ∈ l₂ → a ∈ union l₁ l₂
:= sorry
/-
| [] l₂ h := by rewrite nil_union; exact h
| (b::l₁) l₂ h := by_cases
(λ binl₂ : b ∈ l₂, by rewrite [union_cons_of_mem _ binl₂]; exact (mem_union_right _ h))
(λ nbinl₂ : b ∉ l₂, by rewrite [union_cons_of_not_mem _ nbinl₂]; exact (mem_cons_of_mem _ (mem_union_right _ h)))
-/
theorem mem_union_left {a : A} : ∀ {l₁} (l₂), a ∈ l₁ → a ∈ union l₁ l₂
:= sorry
/-
| [] l₂ h := absurd h !not_mem_nil
| (b::l₁) l₂ h := by_cases
(λ binl₂ : b ∈ l₂, or.elim (eq_or_mem_of_mem_cons h)
(λ aeqb : a = b,
by rewrite [union_cons_of_mem l₁ binl₂, -aeqb at binl₂]; exact (mem_union_right _ binl₂))
(λ ainl₁ : a ∈ l₁,
by rewrite [union_cons_of_mem l₁ binl₂]; exact (mem_union_left _ ainl₁)))
(λ nbinl₂ : b ∉ l₂, or.elim (eq_or_mem_of_mem_cons h)
(λ aeqb : a = b,
by rewrite [union_cons_of_not_mem l₁ nbinl₂, aeqb]; exact !mem_cons)
(λ ainl₁ : a ∈ l₁,
by rewrite [union_cons_of_not_mem l₁ nbinl₂]; exact (mem_cons_of_mem _ (mem_union_left _ ainl₁))))
-/
theorem mem_union_cons (a : A) (l₁ : list A) (l₂ : list A) : a ∈ union (a::l₁) l₂ :=
sorry
/-
by_cases
(λ ainl₂ : a ∈ l₂, mem_union_right _ ainl₂)
(λ nainl₂ : a ∉ l₂, by rewrite [union_cons_of_not_mem _ nainl₂]; exact !mem_cons)
-/
theorem nodup_union_of_nodup_of_nodup : ∀ {l₁ l₂ : list A}, nodup l₁ → nodup l₂ → nodup (union l₁ l₂)
:= sorry
/-
| [] l₂ n₁ nl₂ := by rewrite nil_union; exact nl₂
| (a::l₁) l₂ nal₁ nl₂ :=
have nl₁ : nodup l₁, from nodup_of_nodup_cons nal₁,
have nl₁l₂ : nodup (union l₁ l₂), from nodup_union_of_nodup_of_nodup nl₁ nl₂,
by_cases
(λ ainl₂ : a ∈ l₂,
by rewrite [union_cons_of_mem l₁ ainl₂]; exact nl₁l₂)
(λ nainl₂ : a ∉ l₂,
have nainl₁ : a ∉ l₁, from not_mem_of_nodup_cons nal₁,
have nainl₁l₂ : a ∉ union l₁ l₂, from
assume ainl₁l₂ : a ∈ union l₁ l₂, or.elim (mem_or_mem_of_mem_union ainl₁l₂)
(λ ainl₁, absurd ainl₁ nainl₁)
(λ ainl₂, absurd ainl₂ nainl₂),
by rewrite [union_cons_of_not_mem l₁ nainl₂]; exact (nodup_cons nainl₁l₂ nl₁l₂))
-/
theorem union_eq_append : ∀ {l₁ l₂ : list A}, disjoint l₁ l₂ → union l₁ l₂ = append l₁ l₂
:= sorry
/-
| [] l₂ d := rfl
| (a::l₁) l₂ d :=
have nainl₂ : a ∉ l₂, from disjoint_left d !mem_cons,
have d₁ : disjoint l₁ l₂, from disjoint_of_disjoint_cons_left d,
by rewrite [union_cons_of_not_mem _ nainl₂, append_cons, union_eq_append d₁]
-/
theorem all_union {p : A → Prop} : ∀ {l₁ l₂ : list A}, all l₁ p → all l₂ p → all (union l₁ l₂) p
:= sorry
/-
| [] l₂ h₁ h₂ := h₂
| (a::l₁) l₂ h₁ h₂ :=
have h₁' : all l₁ p, from all_of_all_cons h₁,
have pa : p a, from of_all_cons h₁,
have au : all (union l₁ l₂) p, from all_union h₁' h₂,
have au' : all (a :: union l₁ l₂) p, from all_cons_of_all pa au,
by_cases
(λ ainl₂ : a ∈ l₂, by rewrite [union_cons_of_mem _ ainl₂]; exact au)
(λ nainl₂ : a ∉ l₂, by rewrite [union_cons_of_not_mem _ nainl₂]; exact au')
-/
theorem all_of_all_union_left {p : A → Prop} : ∀ {l₁ l₂ : list A}, all (union l₁ l₂) p → all l₁ p
:= sorry
/-
| [] l₂ h := trivial
| (a::l₁) l₂ h :=
have ain : a ∈ union (a::l₁) l₂, from !mem_union_cons,
have pa : p a, from of_mem_of_all ain h,
by_cases
(λ ainl₂ : a ∈ l₂,
have al₁l₂ : all (union l₁ l₂) p, by rewrite [union_cons_of_mem _ ainl₂ at h]; exact h,
have al₁ : all l₁ p, from all_of_all_union_left al₁l₂,
all_cons_of_all pa al₁)
(λ nainl₂ : a ∉ l₂,
have aal₁l₂ : all (a::union l₁ l₂) p, by rewrite [union_cons_of_not_mem _ nainl₂ at h]; exact h,
have al₁l₂ : all (union l₁ l₂) p, from all_of_all_cons aal₁l₂,
have al₁ : all l₁ p, from all_of_all_union_left al₁l₂,
all_cons_of_all pa al₁)
-/
theorem all_of_all_union_right {p : A → Prop} : ∀ {l₁ l₂ : list A}, all (union l₁ l₂) p → all l₂ p
:= sorry
/-
| [] l₂ h := by rewrite [nil_union at h]; exact h
| (a::l₁) l₂ h := by_cases
(λ ainl₂ : a ∈ l₂, by rewrite [union_cons_of_mem _ ainl₂ at h]; exact (all_of_all_union_right h))
(λ nainl₂ : a ∉ l₂,
have h₁ : all (a :: union l₁ l₂) p, by rewrite [union_cons_of_not_mem _ nainl₂ at h]; exact h,
all_of_all_union_right (all_of_all_cons h₁))
-/
variable {B : Type}
theorem foldl_union_of_disjoint (f : B → A → B) (b : B) {l₁ l₂ : list A} (d : disjoint l₁ l₂)
: foldl f b (union l₁ l₂) = foldl f (foldl f b l₁) l₂ :=
sorry -- by rewrite [union_eq_append d, foldl_append]
theorem foldr_union_of_dijoint (f : A → B → B) (b : B) {l₁ l₂ : list A} (d : disjoint l₁ l₂)
: foldr f b (union l₁ l₂) = foldr f (foldr f b l₂) l₁ :=
sorry -- by rewrite [union_eq_append d, foldr_append]
end union
/- insert -/
section insert
variable {A : Type}
variable [H : decidable_eq A]
include H
definition insert (a : A) (l : list A) : list A :=
if a ∈ l then l else a::l
theorem insert_eq_of_mem {a : A} {l : list A} : a ∈ l → insert a l = l :=
assume ainl, if_pos ainl
theorem insert_eq_of_not_mem {a : A} {l : list A} : a ∉ l → insert a l = a::l :=
assume nainl, if_neg nainl
theorem mem_insert (a : A) (l : list A) : a ∈ insert a l :=
sorry
/-
by_cases
(λ ainl : a ∈ l, by rewrite [insert_eq_of_mem ainl]; exact ainl)
(λ nainl : a ∉ l, by rewrite [insert_eq_of_not_mem nainl]; exact !mem_cons)
-/
theorem mem_insert_of_mem {a : A} (b : A) {l : list A} : a ∈ l → a ∈ insert b l :=
sorry
/-
assume ainl, by_cases
(λ binl : b ∈ l, by rewrite [insert_eq_of_mem binl]; exact ainl)
(λ nbinl : b ∉ l, by rewrite [insert_eq_of_not_mem nbinl]; exact (mem_cons_of_mem _ ainl))
-/
theorem eq_or_mem_of_mem_insert {x a : A} {l : list A} (H : x ∈ insert a l) : x = a ∨ x ∈ l :=
decidable.by_cases
(assume H3: a ∈ l, or.inr (insert_eq_of_mem H3 ▸ H))
(assume H3: a ∉ l,
have H4: x ∈ a :: l, from insert_eq_of_not_mem H3 ▸ H,
iff.mp (mem_cons_iff x a l) H4)
theorem mem_insert_iff (x a : A) (l : list A) : x ∈ insert a l ↔ x = a ∨ x ∈ l :=
iff.intro
eq_or_mem_of_mem_insert
(assume H, or.elim H
(assume H' : x = a, eq.symm H' ▸ mem_insert a l)
(assume H' : x ∈ l, mem_insert_of_mem a H'))
theorem nodup_insert (a : A) {l : list A} : nodup l → nodup (insert a l) :=
sorry
/-
assume n, by_cases
(λ ainl : a ∈ l, by rewrite [insert_eq_of_mem ainl]; exact n)
(λ nainl : a ∉ l, by rewrite [insert_eq_of_not_mem nainl]; exact (nodup_cons nainl n))
-/
theorem length_insert_of_mem {a : A} {l : list A} : a ∈ l → length (insert a l) = length l :=
sorry -- assume ainl, by rewrite [insert_eq_of_mem ainl]
theorem length_insert_of_not_mem {a : A} {l : list A} : a ∉ l → length (insert a l) = length l + 1 :=
sorry -- assume nainl, by rewrite [insert_eq_of_not_mem nainl]
theorem all_insert_of_all {p : A → Prop} {a : A} {l} : p a → all l p → all (insert a l) p :=
sorry
/-
assume h₁ h₂, by_cases
(λ ainl : a ∈ l, by rewrite [insert_eq_of_mem ainl]; exact h₂)
(λ nainl : a ∉ l, by rewrite [insert_eq_of_not_mem nainl]; exact (all_cons_of_all h₁ h₂))
-/
end insert
/- inter -/
section inter
variable {A : Type}
variable [H : decidable_eq A]
include H
definition inter : list A → list A → list A
| [] l₂ := []
| (a::l₁) l₂ := if a ∈ l₂ then a :: inter l₁ l₂ else inter l₁ l₂
theorem inter_nil (l : list A) : inter [] l = [] := rfl
theorem inter_cons_of_mem {a : A} (l₁ : list A) {l₂} : a ∈ l₂ → inter (a::l₁) l₂ = a :: inter l₁ l₂ :=
assume i, if_pos i
theorem inter_cons_of_not_mem {a : A} (l₁ : list A) {l₂} : a ∉ l₂ → inter (a::l₁) l₂ = inter l₁ l₂ :=
assume i, if_neg i
theorem mem_of_mem_inter_left : ∀ {l₁ l₂} {a : A}, a ∈ inter l₁ l₂ → a ∈ l₁
:= sorry
/-
| [] l₂ a i := absurd i !not_mem_nil
| (b::l₁) l₂ a i := by_cases
(λ binl₂ : b ∈ l₂,
have aux : a ∈ b :: inter l₁ l₂, by rewrite [inter_cons_of_mem _ binl₂ at i]; exact i,
or.elim (eq_or_mem_of_mem_cons aux)
(λ aeqb : a = b, by rewrite [aeqb]; exact !mem_cons)
(λ aini, mem_cons_of_mem _ (mem_of_mem_inter_left aini)))
(λ nbinl₂ : b ∉ l₂,
have ainl₁ : a ∈ l₁, by rewrite [inter_cons_of_not_mem _ nbinl₂ at i]; exact (mem_of_mem_inter_left i),
mem_cons_of_mem _ ainl₁)
-/
theorem mem_of_mem_inter_right : ∀ {l₁ l₂} {a : A}, a ∈ inter l₁ l₂ → a ∈ l₂
:= sorry
/-
| [] l₂ a i := absurd i !not_mem_nil
| (b::l₁) l₂ a i := by_cases
(λ binl₂ : b ∈ l₂,
have aux : a ∈ b :: inter l₁ l₂, by rewrite [inter_cons_of_mem _ binl₂ at i]; exact i,
or.elim (eq_or_mem_of_mem_cons aux)
(λ aeqb : a = b, by rewrite [aeqb]; exact binl₂)
(λ aini : a ∈ inter l₁ l₂, mem_of_mem_inter_right aini))
(λ nbinl₂ : b ∉ l₂,
by rewrite [inter_cons_of_not_mem _ nbinl₂ at i]; exact (mem_of_mem_inter_right i))
-/
theorem mem_inter_of_mem_of_mem : ∀ {l₁ l₂} {a : A}, a ∈ l₁ → a ∈ l₂ → a ∈ inter l₁ l₂
:= sorry
/-
| [] l₂ a i₁ i₂ := absurd i₁ !not_mem_nil
| (b::l₁) l₂ a i₁ i₂ := by_cases
(λ binl₂ : b ∈ l₂,
or.elim (eq_or_mem_of_mem_cons i₁)
(λ aeqb : a = b,
by rewrite [inter_cons_of_mem _ binl₂, aeqb]; exact !mem_cons)
(λ ainl₁ : a ∈ l₁,
by rewrite [inter_cons_of_mem _ binl₂];
apply mem_cons_of_mem;
exact (mem_inter_of_mem_of_mem ainl₁ i₂)))
(λ nbinl₂ : b ∉ l₂,
or.elim (eq_or_mem_of_mem_cons i₁)
(λ aeqb : a = b, absurd (aeqb ▸ i₂) nbinl₂)
(λ ainl₁ : a ∈ l₁,
by rewrite [inter_cons_of_not_mem _ nbinl₂]; exact (mem_inter_of_mem_of_mem ainl₁ i₂)))
-/
theorem nodup_inter_of_nodup : ∀ {l₁ : list A} (l₂), nodup l₁ → nodup (inter l₁ l₂)
:= sorry
/-
| [] l₂ d := nodup_nil
| (a::l₁) l₂ d :=
have d₁ : nodup l₁, from nodup_of_nodup_cons d,
have d₂ : nodup (inter l₁ l₂), from nodup_inter_of_nodup _ d₁,
have nainl₁ : a ∉ l₁, from not_mem_of_nodup_cons d,
have naini : a ∉ inter l₁ l₂, from λ i, absurd (mem_of_mem_inter_left i) nainl₁,
by_cases
(λ ainl₂ : a ∈ l₂, by rewrite [inter_cons_of_mem _ ainl₂]; exact (nodup_cons naini d₂))
(λ nainl₂ : a ∉ l₂, by rewrite [inter_cons_of_not_mem _ nainl₂]; exact d₂)
-/
theorem inter_eq_nil_of_disjoint : ∀ {l₁ l₂ : list A}, disjoint l₁ l₂ → inter l₁ l₂ = []
:= sorry
/-
| [] l₂ d := rfl
| (a::l₁) l₂ d :=
have aux_eq : inter l₁ l₂ = [], from inter_eq_nil_of_disjoint (disjoint_of_disjoint_cons_left d),
have nainl₂ : a ∉ l₂, from disjoint_left d !mem_cons,
by rewrite [inter_cons_of_not_mem _ nainl₂, aux_eq]
-/
theorem all_inter_of_all_left {p : A → Prop} : ∀ {l₁} (l₂), all l₁ p → all (inter l₁ l₂) p
:= sorry
/-
| [] l₂ h := trivial
| (a::l₁) l₂ h :=
have h₁ : all l₁ p, from all_of_all_cons h,
have h₂ : all (inter l₁ l₂) p, from all_inter_of_all_left _ h₁,
have pa : p a, from of_all_cons h,
have h₃ : all (a :: inter l₁ l₂) p, from all_cons_of_all pa h₂,
by_cases
(λ ainl₂ : a ∈ l₂, by rewrite [inter_cons_of_mem _ ainl₂]; exact h₃)
(λ nainl₂ : a ∉ l₂, by rewrite [inter_cons_of_not_mem _ nainl₂]; exact h₂)
-/
theorem all_inter_of_all_right {p : A → Prop} : ∀ (l₁) {l₂}, all l₂ p → all (inter l₁ l₂) p
:= sorry
/-
| [] l₂ h := trivial
| (a::l₁) l₂ h :=
have h₁ : all (inter l₁ l₂) p, from all_inter_of_all_right _ h,
by_cases
(λ ainl₂ : a ∈ l₂,
have pa : p a, from of_mem_of_all ainl₂ h,
have h₂ : all (a :: inter l₁ l₂) p, from all_cons_of_all pa h₁,
by rewrite [inter_cons_of_mem _ ainl₂]; exact h₂)
(λ nainl₂ : a ∉ l₂, by rewrite [inter_cons_of_not_mem _ nainl₂]; exact h₁)
-/
end inter
end list
|
7f50de22b08d31e3b23e22231e118f0a22d7f2ac | 618003631150032a5676f229d13a079ac875ff77 | /src/tactic/clear.lean | b2a3eded9d7abb3198a813dfb482e5ed45c34af4 | [
"Apache-2.0"
] | permissive | awainverse/mathlib | 939b68c8486df66cfda64d327ad3d9165248c777 | ea76bd8f3ca0a8bf0a166a06a475b10663dec44a | refs/heads/master | 1,659,592,962,036 | 1,590,987,592,000 | 1,590,987,592,000 | 268,436,019 | 1 | 0 | Apache-2.0 | 1,590,990,500,000 | 1,590,990,500,000 | null | UTF-8 | Lean | false | false | 3,482 | lean | /-
Copyright (c) 2020 Jannis Limperg. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Jannis Limperg
-/
import tactic.core
/-!
# Better `clear` tactics
We define two variants of the standard `clear` tactic:
* `clear'` works like `clear` but the hypotheses that should be cleared can be
given in any order. In contrast, `clear` can fail if hypotheses that depend on
each other are given in the wrong order, even if all of them could be cleared.
* `clear_dependent` works like `clear'` but also clears any hypotheses that
depend on the given hypotheses.
## Implementation notes
The implementation (ab)uses the native `revert_lst`, which can figure out
dependencies between hypotheses. This implementation strategy was suggested by
Simon Hudon.
-/
open native tactic interactive lean.parser
/-- Clears all the hypotheses in `hyps`. The tactic fails if any of the `hyps`
is not a local or if the target depends on any of the `hyps`. It also fails if
`hyps` contains duplicates.
If there are local hypotheses or definitions, say `H`, which are not in `hyps`
but depend on one of the `hyps`, what we do depends on `clear_dependent`. If it
is true, `H` is implicitly also cleared. If it is false, `clear'` fails. -/
meta def tactic.clear' (clear_dependent : bool) (hyps : list expr) : tactic unit := do
tgt ← target,
-- Check if the target depends on any of the hyps. Doing this (instead of
-- letting one of the later tactics fail) lets us give a much more informative
-- error message.
hyps.mmap' (λ h, do
dep ← kdepends_on tgt h,
when dep $ fail $
format!"Cannot clear hypothesis {h} since the target depends on it."),
n ← revert_lst hyps,
-- If revert_lst reverted more hypotheses than we wanted to clear, there must
-- have been other hypotheses dependent on some of the hyps.
when (! clear_dependent && (n ≠ hyps.length)) $ fail $ format.join
[ "Some of the following hypotheses cannot be cleared because other "
, "hypotheses depend on (some of) them:\n"
, format.intercalate ", " (hyps.map to_fmt)
],
v ← mk_meta_var tgt,
intron n,
exact v,
gs ← get_goals,
set_goals $ v :: gs
namespace tactic.interactive
/--
An improved version of the standard `clear` tactic. `clear` is sensitive to the
order of its arguments: `clear x y` may fail even though both `x` and `y` could
be cleared (if the type of `y` depends on `x`). `clear'` lifts this limitation.
```lean
example {α} {β : α → Type} (a : α) (b : β a) : unit :=
begin
try { clear a b }, -- fails since `b` depends on `a`
clear' a b, -- succeeds
exact ()
end
```
-/
meta def clear' (p : parse (many ident)) : tactic unit := do
hyps ← p.mmap get_local,
tactic.clear' false hyps
/--
A variant of `clear'` which clears not only the given hypotheses, but also any
other hypotheses depending on them.
```lean
example {α} {β : α → Type} (a : α) (b : β a) : unit :=
begin
try { clear' a }, -- fails since `b` depends on `a`
clear_dependent a, -- succeeds, clearing `a` and `b`
exact ()
end
```
-/
meta def clear_dependent (p : parse (many ident)) : tactic unit := do
hyps ← p.mmap get_local,
tactic.clear' true hyps
add_tactic_doc
{ name := "clear'",
category := doc_category.tactic,
decl_names := [`tactic.interactive.clear', `tactic.interactive.clear_dependent],
tags := ["context management"],
inherit_description_from := `tactic.interactive.clear' }
end tactic.interactive
|
5b7189c76a90b4a3f8b61982c6d7ae4ea1d260f1 | 9dd3f3912f7321eb58ee9aa8f21778ad6221f87c | /tests/lean/pp_char_bug.lean | c3cddd6935f6996304ab7f8c4b38f44cf16ea09a | [
"Apache-2.0"
] | permissive | bre7k30/lean | de893411bcfa7b3c5572e61b9e1c52951b310aa4 | 5a924699d076dab1bd5af23a8f910b433e598d7a | refs/heads/master | 1,610,900,145,817 | 1,488,006,845,000 | 1,488,006,845,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 424 | lean | check (fin.mk 2 dec_trivial : fin 5)
check (fin.mk 1 dec_trivial : fin 3)
check #"a"
check to_string #"a"
vm_eval to_string #"a"
vm_eval #"a"
vm_eval char.of_nat 1
vm_eval char.of_nat 1
vm_eval char.of_nat 20
check char.of_nat 1
check char.of_nat 20
check char.of_nat 15
check char.of_nat 16
vm_eval char.of_nat 15
vm_eval char.of_nat 16
example : char.of_nat 1 = #"\x01" :=
rfl
example : char.of_nat 20 = #"\x14" :=
rfl
|
a799660d32f23f8d2b8152feece7dad6ce461c8c | c8af905dcd8475f414868d303b2eb0e9d3eb32f9 | /src/data/cpi/transition/equivalence.lean | ab8e71bf6cf0817c5f499fd781586d3eb262b397 | [
"BSD-3-Clause"
] | permissive | continuouspi/lean-cpi | 81480a13842d67ff5f3698643210d8ed5dd08de4 | 443bf2cb236feadc45a01387099c236ab2b78237 | refs/heads/master | 1,650,307,316,582 | 1,587,033,364,000 | 1,587,033,364,000 | 207,499,661 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 27,350 | lean | import data.cpi.transition.basic
namespace cpi
namespace transition
variables {ℍ : Type} {ω : context}
open_locale congruence
open species.equiv
private def no_rename_zero {Γ} {n} {p : upair (fin n)} {q : upair (name Γ)}
: upair.map name.extend q ≠ upair.map name.zero p
:= upair.rec_on q
(λ a b eql, begin
rcases upair.exists_rep p with ⟨ a', b', ⟨ _ ⟩ ⟩,
rcases quotient.exact eql with ⟨ l, _ ⟩ | ⟨ l, _ ⟩; cases l
end)
(λ a b, function.hfunext (by rw upair.mk.comm) (λ a₂ b₂ _, heq.rfl))
private def on_parallel_assoc₁_left {Γ ℓ} {A B C : species ℍ ω Γ} :
∀ {α : label ℍ Γ kind.species} {E : species ℍ ω Γ}
, (A |ₛ B) [ℓ, α]⟶ (production.species E)
→ Σ' E' (eq : production.species (E |ₛ C) ≈ E')
, (A |ₛ B |ₛ C) [ℓ, α]⟶ E'
| α E (@com₁ _ _ _ _ x y _ _ a b F G _ _ rfl rfl tf tg) :=
⟨ _, production.equiv.species (symm (concretion.pseudo_apply.on_parallel₁ _ _ C)),
com₁ rfl rfl tf (parL_concretion C tg) ⟩
| α E (parL_species C t) :=
⟨ _, production.equiv.species parallel_assoc₁, parL_species _ t ⟩
| α E (parR_species _ t) :=
⟨ _, production.equiv.species equiv.parallel_assoc₁, parR_species _ (parL_species _ t) ⟩
private def on_parallel_assoc₁ {Γ ℓ} {A B C : species ℍ ω Γ} :
∀ {k} {α : label ℍ Γ k} {E : production ℍ ω Γ k}
, ((A |ₛ B) |ₛ C) [ℓ, α]⟶ E
→ Σ' E' (eq : E ≈ E'), (A |ₛ B |ₛ C) [ℓ, α]⟶ E'
| k α E (@com₁ _ _ _ _ x y _ _ a b _ G _ _ rfl rfl (parL_concretion D tf) tg) :=
⟨ _, production.equiv.species (concretion.pseudo_apply.parallel_shift _ B G),
com₁ rfl rfl tf (parR_concretion _ tg) ⟩
| k α E (@com₁ _ _ _ _ x y _ _ a b _ G _ _ rfl rfl (parR_concretion D tf) tg) :=
⟨ _, production.equiv.species (concretion.pseudo_apply.on_parallel₂' A _ G),
parR_species A (com₁ rfl rfl tf tg) ⟩
| k α E (parL_species _ t) := on_parallel_assoc₁_left t
| k α E (parL_concretion B (parL_concretion C t)) :=
⟨ _, production.equiv.concretion concretion.equiv.parallel_assoc₁, parL_concretion _ t ⟩
| k α E (parL_concretion C (parR_concretion D t)) :=
⟨ _, production.equiv.concretion concretion.equiv.parallel_assoc₂, parR_concretion _ (parL_concretion _ t) ⟩
| k α E (parR_species D t) :=
⟨ _, production.equiv.species equiv.parallel_assoc₁, parR_species A (parR_species B t) ⟩
| k α E (parR_concretion D t) :=
⟨ _, production.equiv.concretion concretion.equiv.parallel_assoc₃, parR_concretion A (parR_concretion B t) ⟩
private def on_parallel_symm {Γ ℓ} {A B : species ℍ ω Γ} :
∀ {k} {α : label ℍ Γ k} {E : production ℍ ω Γ k}
, (A |ₛ B) [ℓ, α]⟶ E
→ Σ' E' (eq : E ≈ E'), (B |ₛ A) [ℓ, α]⟶ E'
| ._ ._ ._ (@com₁ _ _ _ _ x y _ _ a b F G _ _ rfl rfl tf tg) := begin
rw upair.eq a b,
from ⟨ _, production.equiv.species (concretion.pseudo_apply.symm F G), com₁ rfl rfl tg tf ⟩
end
| k α ._ (parL_species _ t) := ⟨ _, production.equiv.species parallel_symm, parR_species B t ⟩
| k α ._ (parL_concretion _ t) := ⟨ _, production.equiv.concretion concretion.equiv.parallel_symm, parR_concretion B t ⟩
| k α ._ (parR_species _ t) := ⟨ _, production.equiv.species parallel_symm, parL_species A t ⟩
| k α ._ (parR_concretion _ t) := ⟨ _, production.equiv.concretion (symm concretion.equiv.parallel_symm), parL_concretion A t ⟩
private def on_parallel_assoc₂_species {Γ ℓ} {A B C : species ℍ ω Γ} :
∀ {α : label ℍ Γ kind.species} {E : species ℍ ω Γ}
, (B |ₛ C) [ℓ, α]⟶ (production.species E)
→ Σ' (E' : production ℍ ω Γ kind.species) (eq : production.species (A |ₛ E) ≈ E')
, ((A |ₛ B) |ₛ C) [ℓ, α]⟶ E'
| α E (parL_species _ t) :=
⟨ _, production.equiv.species equiv.parallel_assoc₂, parL_species _ (parR_species _ t) ⟩
| α E (parR_species _ t) :=
⟨ _, production.equiv.species equiv.parallel_assoc₂, parR_species _ t ⟩
| α E (com₁ rfl rfl tf tg) :=
⟨ _, production.equiv.species (concretion.pseudo_apply.on_parallel₂' A _ _).symm,
com₁ rfl rfl (parR_concretion _ tf) tg ⟩
private def on_parallel_assoc₂_concretion {Γ ℓ} {A B C : species ℍ ω Γ} {b y} :
∀ {α : label ℍ Γ kind.concretion} {E : concretion ℍ ω Γ b y}
, (B |ₛ C) [ℓ, α]⟶ (production.concretion E)
→ Σ' (E' : production ℍ ω Γ kind.concretion) (eq : production.concretion (A |₂ E) ≈ E')
, ((A |ₛ B) |ₛ C) [ℓ, α]⟶ E'
| α E (parL_concretion _ t) :=
⟨ _, production.equiv.concretion concretion.equiv.parallel_assoc₂.symm
, parL_concretion _ (parR_concretion _ t) ⟩
| α E (parR_concretion _ t) :=
⟨ _, production.equiv.concretion concretion.equiv.parallel_assoc₃.symm, parR_concretion _ t ⟩
private def on_parallel_assoc₂ {Γ ℓ} {A B C : species ℍ ω Γ} :
∀ {k} {α : label ℍ Γ k} {E : production ℍ ω Γ k}
, (A |ₛ B |ₛ C) [ℓ, α]⟶ E
→ Σ' E' (eq : E ≈ E'), ((A |ₛ B) |ₛ C) [ℓ, α]⟶ E'
| ._ α E (@com₁ _ _ _ _ x y _ _ a b F G _ _ rfl rfl tf (parL_concretion _ tg)) :=
⟨ _, production.equiv.species (concretion.pseudo_apply.on_parallel₁ F _ C),
parL_species _ (com₁ rfl rfl tf tg) ⟩
| ._ α E (@com₁ _ _ _ _ x y _ _ a b F G _ _ rfl rfl tf (parR_concretion _ tg)) :=
⟨ _, production.equiv.species (concretion.pseudo_apply.parallel_shift F B _).symm,
com₁ rfl rfl (parL_concretion _ tf) tg ⟩
| k α ._ (parL_species _ t) :=
⟨ _, production.equiv.species equiv.parallel_assoc₂, parL_species _ (parL_species _ t) ⟩
| k α ._ (parL_concretion _ t) :=
⟨ _, production.equiv.concretion concretion.equiv.parallel_assoc₁.symm, parL_concretion _ (parL_concretion _ t) ⟩
| k α ._ (parR_species _ t) := on_parallel_assoc₂_species t
| k α ._ (parR_concretion _ t) := on_parallel_assoc₂_concretion t
private def on_choice_swap {Γ ℓ} {As : choices ℍ ω Γ} :
∀ {k f g} {α : label ℍ Γ k}
{π₁ : prefix_expr ℍ Γ f} {π₂ : prefix_expr ℍ Γ g}
{A : species ℍ ω (f.apply Γ)} {B : species ℍ ω (g.apply Γ)}
{E : production ℍ ω Γ k}
, (Σ# whole.cons π₁ A (whole.cons π₂ B As)) [ℓ, α]⟶ E
→ Σ' E' (eq : E ≈ E')
, (Σ# whole.cons π₂ B (whole.cons π₁ A As)) [ℓ, α]⟶ E'
| ._ α f g π₁ π₂ A B E (choice₁ a b b_len y _ _) := ⟨ _, refl _, ξ_choice (choice₁ a b b_len y _ _) ⟩
| ._ α f g π₁ π₂ A B E (choice₂ k _ _) := ⟨ _, refl _, ξ_choice (choice₂ k _ _) ⟩
| ._ α f g π₁ π₂ A B E (ξ_choice (choice₁ a b b_len y _ _)) := ⟨ _, refl _, choice₁ a b b_len y _ _ ⟩
| ._ α f g π₁ π₂ A B E (ξ_choice (choice₂ k _ _)) := ⟨ _, refl _, choice₂ k _ _ ⟩
| k α f g π₁ π₂ A B E (ξ_choice (ξ_choice t)) := ⟨ _, refl _, ξ_choice (ξ_choice t) ⟩
private def on_ν_parallel₂ {Γ ℓ} {M : affinity ℍ} {A : species ℍ ω Γ} {B : species ℍ ω (context.extend (M.arity) Γ)} :
∀ {k} {α : label ℍ Γ k} {E : production ℍ ω Γ k}
, (A |ₛ ν(M) B) [ℓ, α]⟶ E
→ Σ' E' (eq : E ≈ E'), (ν(M) rename name.extend A |ₛ B) [ℓ, α]⟶ E'
| ._ ._ _ (com₁ rfl rfl tf (ν₁_concretion M rfl tg)) :=
-- Slighty bizzare, but the separate binding helps this to TC
let tf' := transition.rename (@name.extend _ M.arity) tf in
⟨ _, production.equiv.species (concretion.pseudo_apply.on_restriction _ _ _),
ν₁_species M rfl (com₁ rfl rfl tf' tg) ⟩
| k α _ (parL_species E t) :=
let t' := transition.rename (@name.extend _ M.arity) t in
⟨ _, production.equiv.species (ν_parallel₂ M), ν₁_species M rfl (parL_species _ t' ) ⟩
| k α _ (parL_concretion E t) :=
let t' := transition.rename (@name.extend _ M.arity) t in
⟨ _, production.equiv.concretion (concretion.equiv.ν_parallel₂ M).symm,
ν₁_concretion M rfl (parL_concretion _ t' ) ⟩
| k α _ (parR_species A (com₂ M k' ek rfl t)) :=
⟨ _, production.equiv.species (ν_parallel₂ M), com₂ M k' ek rfl (parR_species _ t) ⟩
| k α _ (parR_species A (ν₁_species _ rfl t)) :=
⟨ _, production.equiv.species (ν_parallel₂ M), ν₁_species M rfl (parR_species _ t) ⟩
| k α _ (parR_concretion A (ν₁_concretion _ rfl t)) :=
⟨ _, production.equiv.concretion (concretion.equiv.ν_parallel₁ M).symm,
ν₁_concretion M rfl (parR_concretion _ t) ⟩
private def upair_extend {n} {Γ} :
∀ {p : upair (name Γ)} {a b : name (context.extend n Γ)}
, upair.map name.extend p = upair.mk a b
→ Σ' (a' b' : name Γ), p = upair.mk a' b' ∧ a = name.extend a' ∧ b = name.extend b'
| p (name.zero _) (name.zero _) e := false.elim (begin
rcases quot.exists_rep p with ⟨ ⟨ a, b ⟩, ep ⟩, have : upair.mk a b = p := ep, subst this,
rcases quotient.exact e with ⟨ l, r ⟩ | ⟨ l, r ⟩; contradiction,
end)
| p (name.zero _) (name.extend _) e := false.elim (begin
rcases quot.exists_rep p with ⟨ ⟨ a, b ⟩, ep ⟩, have : upair.mk a b = p := ep, subst this,
rcases quotient.exact e with ⟨ l, r ⟩ | ⟨ l, r ⟩; contradiction,
end)
| p (name.extend _) (name.zero _) e := false.elim (begin
rcases quot.exists_rep p with ⟨ ⟨ a, b ⟩, ep ⟩, have : upair.mk a b = p := ep, subst this,
rcases quotient.exact e with ⟨ l, r ⟩ | ⟨ l, r ⟩; contradiction,
end)
| p (name.extend a) (name.extend b) e := ⟨ a, b, begin
have : upair.map name.extend p = upair.map name.extend (upair.mk a b) := e,
from ⟨ upair.map.inj (@name.extend.inj _ _) this, rfl, rfl ⟩,
end ⟩
private noncomputable def on_ν_parallel₁_species {Γ ℓ} {M : affinity ℍ} {A : species ℍ ω Γ}
{B : species ℍ ω (context.extend M.arity Γ)} :
∀ {A' : species ℍ ω (context.extend (M.arity) Γ)}
{α' : label ℍ (context.extend M.arity Γ) kind.species}
{α : label ℍ Γ kind.species}
{E : species ℍ ω (context.extend M.arity Γ)}
, (A' |ₛ B) [lookup.rename name.extend ℓ, α']⟶ (production.species E)
→ A' = rename name.extend A → α' = label.rename name.extend α
→ Σ' E' (eq : production.species (ν(M)E) ≈ E')
, (A |ₛ ν(M) B) [ℓ, α]⟶ E'
| A' α' α E (parL_species _ t) eqA eqα := begin
subst eqA, subst eqα,
rcases transition.rename_from name.extend t with ⟨ α₂, ⟨ B ⟩, t₂, eα, eB ⟩,
cases label.rename.inj (@name.extend.inj _ _) eα,
rw ← production.species.inj eB,
from ⟨ _, production.equiv.species (ν_parallel₁ M), parL_species _ t₂ ⟩,
end
| A' α' α E (parR_species _ t) eqA eqα := begin
subst eqA, subst eqα,
from ⟨ _, production.equiv.species (ν_parallel₁ M), parR_species _ (ν₁_species M rfl t) ⟩,
end
| A' α' α E (@com₁ _ _ _ _ x y _ _ a b F G _ _ rfl eα tf tg) eqA eqα := begin
subst eqA, subst eqα,
cases α with _ _ _ _ _ p,
case label.spontaneous { cases eα },
rcases upair_extend (label.of_affinity.inj eα) with ⟨ a, b, ⟨ _ ⟩, ⟨ _ ⟩, ⟨ _ ⟩ ⟩,
rcases transition.rename_from name.extend tf with ⟨ ⟨ a₂ ⟩, ⟨ F ⟩, tf₂, ⟨ _ ⟩, ⟨ _ ⟩ ⟩,
from ⟨ _, production.equiv.species (concretion.pseudo_apply.on_restriction _ M _).symm,
com₁ rfl rfl tf₂ (ν₁_concretion M rfl tg) ⟩,
end
private noncomputable def on_ν_parallel₁_concretion {Γ ℓ} {M : affinity ℍ} {A : species ℍ ω Γ}
{B : species ℍ ω (context.extend M.arity Γ)} {b y} :
∀ {A' : species ℍ ω (context.extend (M.arity) Γ)}
{α' : label ℍ (context.extend M.arity Γ) kind.concretion}
{α : label ℍ Γ kind.concretion}
{E : concretion ℍ ω (context.extend M.arity Γ) b y}
, (A' |ₛ B) [lookup.rename name.extend ℓ, α']⟶ (production.concretion E)
→ A' = rename name.extend A → α' = label.rename name.extend α
→ Σ' E' (eq : production.concretion (ν'(M)E) ≈ E')
, (A |ₛ ν(M) B) [ℓ, α]⟶ E'
| A' α' α E (parL_concretion _ t) eqA eqα := begin
subst eqA, subst eqα,
rcases transition.rename_from name.extend t with ⟨ α₂, ⟨ B ⟩, t₂, eα, ⟨ _ ⟩ ⟩,
cases label.rename.inj (@name.extend.inj _ _) eα,
from ⟨ _, production.equiv.concretion (concretion.equiv.ν_parallel₂ M), parL_concretion _ t₂ ⟩,
end
| A' α' α E (parR_concretion _ t) eqA eqα := begin
subst eqA, subst eqα,
from ⟨ _, production.equiv.concretion (concretion.equiv.ν_parallel₁ M), parR_concretion _ (ν₁_concretion M rfl t) ⟩,
end
private noncomputable def on_ν_parallel₁ {Γ ℓ} {M : affinity ℍ} {A : species ℍ ω Γ}
{B : species ℍ ω (context.extend (M.arity) Γ)} :
∀ {k} {α : label ℍ Γ k} {E : production ℍ ω Γ k}
, (ν(M) rename name.extend A |ₛ B) [ℓ, α]⟶ E
→ Σ' E' (eq : E ≈ E'), (A |ₛ ν(M) B) [ℓ, α]⟶ E'
| k α ._ (ν₁_species M rfl t) := on_ν_parallel₁_species t rfl rfl
| k α ._ (ν₁_concretion M rfl t) := on_ν_parallel₁_concretion t rfl rfl
| ._ α ._ (@com₂ _ _ _ _ M p q _ _ k ek rfl t) := begin
generalize eqA : species.rename (@name.extend _ M.arity) A = A', rw eqA at t,
cases t,
case parL_species : E t {
subst eqA, exfalso,
rcases transition.rename_from name.extend t with ⟨ α₂, ⟨ E ⟩, tf₂, eα, eE ⟩, -- Annoying!
cases α₂; simp only [label.rename] at eα,
case label.spontaneous { cases eα },
from absurd eα no_rename_zero,
},
case parR_species : E t {
subst eqA,
from ⟨ _, production.equiv.species (ν_parallel₁ M), parR_species _ (com₂ M k ek rfl t ) ⟩,
},
case com₁ : x y a b F G eF eα tf tg {
subst eqA, exfalso,
rcases transition.rename_from name.extend tf with ⟨ α₂, ⟨ E ⟩, tf₂, eα', eE ⟩, -- Annoying!
cases α₂, simp only [label.rename] at eα', subst eα',
rcases upair.exists_rep p with ⟨ p₁, p₂, h ⟩, subst h,
rcases quotient.exact (label.of_affinity.inj eα) with ⟨ l, r ⟩ | ⟨ l, r ⟩; contradiction,
}
end
private noncomputable def on_ν_drop₁ {Γ ℓ} {M : affinity ℍ} {A : species ℍ ω Γ} :
∀ {k} {α : label ℍ Γ k} {E : production ℍ ω Γ k}
, (ν(M) species.rename name.extend A) [ℓ, α]⟶ E
→ Σ' (E' : production ℍ ω Γ k) (eq : E ≈ E'), A [ℓ, α]⟶ E'
| k α E t := begin
generalize eqA : species.rename (@name.extend _ M.arity) A = A', rw eqA at t,
cases t,
case com₂ : a b B k eqk eqα t' {
subst eqA, subst eqα, exfalso,
rcases transition.rename_from name.extend t' with ⟨ α₂, ⟨ E₂ ⟩ , t₂, eqα, eqE ⟩,
cases α₂; simp only [label.rename] at eqα,
case label.spontaneous { cases eqα },
from absurd eqα no_rename_zero,
},
case ν₁_species : α₁ α₂ E eα t' {
subst eqA, subst eα,
rcases transition.rename_from name.extend t' with ⟨ α₂, ⟨ E₂ ⟩ , t₂, eqα, eqE ⟩,
cases label.rename.inj (@name.extend.inj _ _) eqα,
rw ← production.species.inj eqE,
from ⟨ _, production.equiv.species (ν_drop₁ M), t₂ ⟩,
},
case ν₁_concretion : α₁ α₂ b y E eα t' {
subst eqA, subst eα,
rcases transition.rename_from name.extend t' with ⟨ α₂, ⟨ E₂ ⟩ , t₂, eqα, ⟨ _ ⟩ ⟩,
cases label.rename.inj (@name.extend.inj _ _) eqα,
from ⟨ _, production.equiv.concretion (concretion.equiv.ν_drop M), t₂ ⟩,
}
end
private def on_ν_drop₂ {Γ ℓ} {M : affinity ℍ} {A : species ℍ ω Γ} :
∀ {k} {α : label ℍ Γ k} {E : production ℍ ω Γ k}
, A [ℓ, α]⟶ E
→ Σ' (E' : production ℍ ω Γ k) (eq : E ≈ E'), (ν(M) species.rename name.extend A) [ℓ, α]⟶ E'
| k α (production.species E) t :=
let t' := transition.rename (@name.extend _ M.arity) t in
⟨ _, production.equiv.species (ν_drop₂ M), ν₁_species M rfl t' ⟩
| k α (production.concretion E) t :=
let t' := transition.rename (@name.extend _ M.arity) t in
⟨ _, production.equiv.concretion (concretion.equiv.ν_drop M).symm, ν₁_concretion M rfl t' ⟩
private def on_ν_swap₁ {Γ ℓ} {M N : affinity ℍ} {A : species ℍ ω (context.extend (N.arity) (context.extend (M.arity) Γ))} :
∀ {k} {α : label ℍ Γ k} {E : production ℍ ω Γ k}
, (ν(M) ν(N) A) [ℓ, α]⟶ E
→ Σ' (E' : production ℍ ω Γ k) (eq : E ≈ E')
, (ν(N) ν(M) species.rename name.swap A) [ℓ, α]⟶ E'
| ._ α E₁ (ν₁_species _ eα₁ (ν₁_species _ eα₂ t)) := begin
subst eα₁, subst eα₂,
have t' := transition.rename name.swap t,
rw [label.rename_compose, label.rename_compose, name.swap_comp_extend, name.ext_extend, ← label.rename_compose] at t',
rw [lookup.rename_compose, lookup.rename_compose, name.swap_comp_extend, name.ext_extend, ← lookup.rename_compose] at t',
from ⟨ _, production.equiv.species (ν_swap₁ M N), ν₁_species N rfl (ν₁_species M rfl t' ) ⟩,
end
| ._ α E₁ (ν₁_species _ eα (com₂ _ k ek rfl t)) := begin
cases α; simp only [label.rename] at eα,
case label.of_affinity { contradiction },
subst eα,
have t' := transition.rename name.swap t,
rw [lookup.rename_compose, lookup.rename_compose, name.swap_comp_extend, name.ext_extend, ← lookup.rename_compose] at t',
refine ⟨ _, production.equiv.species (ν_swap₁ M N), com₂ N k ek rfl (ν₁_species M _ t') ⟩,
simp only [label.rename, upair.map_compose, function.comp, name.swap],
end
| ._ α E₁ (ν₁_concretion _ rfl (ν₁_concretion _ rfl t)) := begin
have t' := transition.rename name.swap t,
rw [label.rename_compose, label.rename_compose, name.swap_comp_extend, name.ext_extend, ← label.rename_compose] at t',
rw [lookup.rename_compose, lookup.rename_compose, name.swap_comp_extend, name.ext_extend, ← lookup.rename_compose] at t',
from ⟨ _, production.equiv.concretion (concretion.equiv.ν_swap M N), ν₁_concretion N rfl (ν₁_concretion M rfl t' ) ⟩,
end
| ._ α E₁ (com₂ _ k ek rfl (ν₁_species _ rfl t)) := begin
have t' := transition.rename name.swap t,
rw [lookup.rename_compose, lookup.rename_compose, name.swap_comp_extend, name.ext_extend, ← lookup.rename_compose] at t',
refine ⟨ _, production.equiv.species (ν_swap₁ M N), ν₁_species N rfl (com₂ M k ek _ t') ⟩,
rw [upair.map_compose, upair.map_compose, name.swap_comp_extend, name.ext_zero],
end
private def on_ν_swap₂ {Γ ℓ} {M N : affinity ℍ} {A : species ℍ ω (context.extend (N.arity) (context.extend (M.arity) Γ))}:
∀ {k} {α : label ℍ Γ k} {E : production ℍ ω Γ k}
, (ν(N) ν(M) species.rename name.swap A) [ℓ, α]⟶ E
→ Σ' (E' : production ℍ ω Γ k) (eq : E ≈ E'), (ν(M) ν(N) A) [ℓ, α]⟶ E'
| k α E₁ t := begin
rcases on_ν_swap₁ t with ⟨ E', eqE, t' ⟩,
rw [species.rename_compose, name.swap_swap, species.rename_id] at t',
from ⟨ E', eqE, t' ⟩,
end
/-- Convert a transition from one species to a transition from another
equivalent species, with the same label and equivalent production. -/
noncomputable def equivalent_of :
∀ {Γ ℓ k} {A : species ℍ ω Γ} {B : species ℍ ω Γ} {α : label ℍ Γ k} {E : production ℍ ω Γ k}
, species.equivalent A B → A [ℓ, α]⟶ E
→ Σ' (E' : production ℍ ω Γ k) (eq : E ≈ E'), B [ℓ, α]⟶ E'
| Γ₁ ℓ₁ k₁ A₁ B₁ α₁ E₁ equ t₁ := begin
induction equ generalizing k₁,
case species.equivalent.refl { from ⟨ E₁, refl _, t₁ ⟩ },
case species.equivalent.trans : Γ A B C ab bc ih_ab ih_bc {
rcases ih_ab _ _ α₁ E₁ t₁ with ⟨ E₂, eq₂, t₂ ⟩,
rcases ih_bc _ _ α₁ E₂ t₂ with ⟨ E₃, eq₃, t₃ ⟩,
from ⟨ E₃, trans eq₂ eq₃, t₃ ⟩
},
case species.equivalent.ξ_parallel₁ : Γ A A' B eq ih {
cases t₁,
case com₁ : x y a b F G E' α' eE eα tf tg {
subst eE, subst eα,
rcases ih _ _ (#a) _ tf with ⟨ F', equ, tf' ⟩, cases F' with _ b y' F',
rcases production.equiv.arity equ with ⟨ ⟨ _ ⟩, ⟨ _ ⟩ ⟩,
from ⟨ _, production.equiv.species (concretion.pseudo_apply.equiv (production.equiv.unwrap_c equ) (refl G)),
com₁ rfl rfl tf' tg ⟩,
},
case parL_species : α' E t {
rcases ih _ _ _ _ t with ⟨ E', equ, t' ⟩, cases E',
from ⟨ _, production.equiv.species (ξ_parallel₁ (production.equiv.unwrap_s equ)), parL_species B t' ⟩
},
case parL_concretion : α' b y E t {
rcases ih _ _ _ _ t with ⟨ E', equ, t' ⟩, cases E',
rcases production.equiv.arity equ with ⟨ ⟨ _ ⟩, ⟨ _ ⟩ ⟩,
from ⟨ _, production.equiv.concretion (concretion.equiv.ξ_parallel₁ (production.equiv.unwrap_c equ)),
parL_concretion _ t' ⟩,
},
case parR_species : α' E t {
from ⟨ _, production.equiv.species (ξ_parallel₁ ⟨ eq ⟩), parR_species _ t ⟩,
},
case parR_concretion : α' b y E t {
from ⟨ _, production.equiv.concretion (concretion.equiv.ξ_parallel' ⟨ eq ⟩), parR_concretion _ t ⟩,
},
},
case species.equivalent.ξ_parallel₂ : Γ A B B' eq ih {
cases t₁,
case com₁ : x y a b F G E' α' eE eα tf tg {
subst eE, subst eα,
rcases ih _ _ (#b) _ tg with ⟨ G', equ, tg' ⟩, cases G' with _ b y' G',
rcases production.equiv.arity equ with ⟨ ⟨ _ ⟩, ⟨ _ ⟩ ⟩,
from ⟨ _, production.equiv.species (concretion.pseudo_apply.equiv (refl F) (production.equiv.unwrap_c equ)),
com₁ rfl rfl tf tg' ⟩,
},
case parL_species : α' E t {
from ⟨ _, production.equiv.species (ξ_parallel₂ ⟨ eq ⟩), parL_species _ t ⟩,
},
case parL_concretion : α' b y E t {
from ⟨ _, production.equiv.concretion (concretion.equiv.ξ_parallel ⟨ eq ⟩), parL_concretion _ t ⟩,
},
case parR_species : α' E t {
rcases ih _ _ _ _ t with ⟨ E', equ, t' ⟩, cases E',
from ⟨ _, production.equiv.species (ξ_parallel₂ (production.equiv.unwrap_s equ)), parR_species _ t' ⟩
},
case parR_concretion : α' b y E t {
rcases ih _ _ _ _ t with ⟨ E', equ, t' ⟩, cases E',
rcases production.equiv.arity equ with ⟨ ⟨ _ ⟩, ⟨ _ ⟩ ⟩,
from ⟨ _, production.equiv.concretion (concretion.equiv.ξ_parallel₂ (production.equiv.unwrap_c equ)),
parR_concretion _ t' ⟩,
},
},
case species.equivalent.ξ_restriction : Γ M A A' eq ih {
cases t₁,
case com₂ : p p' B k ek ep t {
rcases ih _ _ _ _ t with ⟨ E', equ, t' ⟩,
cases E' with B',
from ⟨ _, production.equiv.species (ξ_restriction M (production.equiv.unwrap_s equ)), com₂ M k ek ep t' ⟩,
},
case ν₁_species : α α' E eα t {
rcases ih _ _ _ _ t with ⟨ E', equ, t' ⟩, cases E',
from ⟨ _, production.equiv.species (ξ_restriction M (production.equiv.unwrap_s equ)),
ν₁_species M eα t' ⟩,
},
case ν₁_concretion : α α' b y E eα t {
rcases ih _ _ _ _ t with ⟨ E', equ, t' ⟩, cases E',
rcases production.equiv.arity equ with ⟨ ⟨ _ ⟩, ⟨ _ ⟩ ⟩,
from ⟨ _, production.equiv.concretion (concretion.equiv.ξ_restriction M (production.equiv.unwrap_c equ)),
ν₁_concretion M eα t' ⟩,
},
},
case species.equivalent.ξ_choice_here : Γ f π A A' As eq ih {
cases t₁,
case ξ_choice : t { refine ⟨ _, refl _, ξ_choice t ⟩ },
case choice₁ : a n b b_len y {
from ⟨ _, production.equiv.concretion (concretion.equiv.ξ_apply ⟨ eq ⟩), choice₁ a b b_len y A' As ⟩,
},
case choice₂ : k { from ⟨ _, production.equiv.species ⟨ eq ⟩, choice₂ k A' As ⟩ },
},
case species.equivalent.ξ_choice_there : Γ f π A As As' eq ih {
cases t₁,
case ξ_choice : t {
rcases ih _ _ α₁ E₁ t with ⟨ E', equ, t' ⟩,
refine ⟨ _, equ, ξ_choice t' ⟩,
},
case choice₁ : a n b b_len y A As { from ⟨ _, refl _, choice₁ a b b_len y A As' ⟩ },
case choice₂ : k A As { from ⟨ _, refl _, choice₂ k A As' ⟩ },
},
case species.equivalent.parallel_nil₁ : Γ A {
cases t₁,
-- No such transition for (nil ⟶ _)
case com₁ : x y a b F G FG α eFG eα tf tg { cases tg },
case parR_species : α E t { cases t },
case parR_concretion : α b y E t { cases t },
case parL_species : α E t { from ⟨ _, production.equiv.species parallel_nil₁, t ⟩ },
case parL_concretion : α b y E t { from ⟨ _, production.equiv.concretion concretion.equiv.parallel_nil, t ⟩ },
},
case species.equivalent.parallel_nil₂ : Γ B {
cases E₁,
case production.species { from ⟨ _, production.equiv.species parallel_nil₂, parL_species _ t₁ ⟩ },
case production.concretion { from ⟨ _, production.equiv.concretion concretion.equiv.parallel_nil.symm, parL_concretion _ t₁ ⟩ },
},
case species.equivalent.choice_swap { from on_choice_swap t₁ },
case species.equivalent.parallel_assoc₁ { from on_parallel_assoc₁ t₁ },
case species.equivalent.parallel_assoc₂ { from on_parallel_assoc₂ t₁ },
case species.equivalent.parallel_symm { from on_parallel_symm t₁ },
case species.equivalent.ν_parallel₁ { from on_ν_parallel₁ t₁ },
case species.equivalent.ν_parallel₂ { from on_ν_parallel₂ t₁ },
case species.equivalent.ν_drop₁ { from on_ν_drop₁ t₁ },
case species.equivalent.ν_drop₂ { from on_ν_drop₂ t₁ },
case species.equivalent.ν_swap₁ { from on_ν_swap₁ t₁ },
case species.equivalent.ν_swap₂ { from on_ν_swap₂ t₁ },
end
/-- Drops the congruence restriction from `equivalent_of`. -/
noncomputable def equivalent_of.map {Γ ℓ} {A B : species ℍ ω Γ} (h : species.equivalent A B)
{k} {α : label ℍ Γ k}
: (Σ (E : production ℍ ω Γ k), A [ℓ, α]⟶ E)
→ (Σ (E : production ℍ ω Γ k), B [ℓ, α]⟶ E)
| ⟨ E, t ⟩ :=
let ⟨ E', _, t' ⟩ := equivalent_of h t in
⟨ E', t' ⟩
/-- Show that 'equivalent_of' twice yields the same thing. This is not going to
be fun. -/
axiom equivalent_of.map_map {Γ ℓ} {A B : species ℍ ω Γ} (h : species.equivalent A B)
{k} {α : label ℍ Γ k} (t : Σ (E : production ℍ ω Γ k), A [ℓ, α]⟶ E)
: equivalent_of.map h.symm (equivalent_of.map h t) = t
/-- Show that two equivalent species's transition sets are equivalent. -/
noncomputable def equivalent_of.is_equiv {Γ ℓ} {A B : species ℍ ω Γ} (h : species.equivalent A B)
{k} {α : label ℍ Γ k}
: (Σ (E : production ℍ ω Γ k), A [ℓ, α]⟶ E) ≃ (Σ (E : production ℍ ω Γ k), B [ℓ, α]⟶ E)
:= { to_fun := equivalent_of.map h,
inv_fun := equivalent_of.map h.symm,
left_inv := equivalent_of.map_map h,
right_inv := λ x, begin
have this := equivalent_of.map_map h.symm x,
rw ← species.equivalent.symm_symm h at this,
from this,
end }
lemma equivalent_of.map_equiv {Γ ℓ} {A B : species ℍ ω Γ} (h : species.equivalent A B)
{k} {α : label ℍ Γ k}
(E : production ℍ ω Γ k) (t : A [ℓ, α]⟶ E)
: E ≈ (equivalent_of.map h ⟨ E, t ⟩).1 := begin
unfold equivalent_of.map,
rcases (equivalent_of h t) with ⟨ E', eqE, t' ⟩,
from eqE,
end
end transition
end cpi
#lint-
|
66cfa1cf78cb8d3890a5be4a5f8dd83e23bdd76c | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /test/library_search/basic.lean | d29e225e2da46352f8ef8e707c5fcbb86f8cff06 | [
"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,634 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import tactic.suggest -- No other imports, for fast testing
/- Turn off trace messages so they don't pollute the test build: -/
set_option trace.silence_library_search true
/- For debugging purposes, we can display the list of lemmas: -/
-- set_option trace.suggest true
namespace test.library_search
-- Check that `library_search` fails if there are no goals.
example : true :=
begin
trivial,
success_if_fail { library_search },
end
-- Verify that `library_search` solves goals via `solve_by_elim` when the library isn't
-- even needed.
example (P : Prop) (p : P) : P :=
by library_search
example (P : Prop) (p : P) (np : ¬P) : false :=
by library_search
example (X : Type) (P : Prop) (x : X) (h : Π x : X, x = x → P) : P :=
by library_search
example (α : Prop) : α → α :=
by library_search -- says: `exact id`
example (p : Prop) [decidable p] : (¬¬p) → p :=
by library_search -- says: `exact not_not.mp`
example (a b : Prop) (h : a ∧ b) : a :=
by library_search -- says: `exact h.left`
example (P Q : Prop) [decidable P] [decidable Q]: (¬ Q → ¬ P) → (P → Q) :=
by library_search -- says: `exact not_imp_not.mp`
example (a b : ℕ) : a + b = b + a :=
by library_search -- says: `exact add_comm a b`
example (n m k : ℕ) : n * (m - k) = n * m - n * k :=
by library_search -- says: `exact mul_tsub n m k`
example (n m k : ℕ) : n * m - n * k = n * (m - k) :=
by library_search -- says: `exact eq.symm (mul_tsub n m k)`
example {α : Type} (x y : α) : x = y ↔ y = x :=
by library_search -- says: `exact eq_comm`
example (a b : ℕ) (ha : 0 < a) (hb : 0 < b) : 0 < a + b :=
by library_search -- says: `exact add_pos ha hb`
section synonym
-- Synonym `>` for `<` in another part of the goal
example (a b : ℕ) (ha : a > 0) (hb : 0 < b) : 0 < a + b :=
by library_search -- says: `exact add_pos ha hb`
example (a b : ℕ) (h : a ∣ b) (w : b > 0) : a ≤ b :=
by library_search -- says: `exact nat.le_of_dvd w h`
example (a b : ℕ) (h : a ∣ b) (w : b > 0) : b ≥ a :=
by library_search -- says: `exact nat.le_of_dvd w h`
-- A lemma with head symbol `¬` can be used to prove `¬ p` or `⊥`
example (a : ℕ) : ¬ (a < 0) := by library_search -- says `exact not_lt_bot`
example (a : ℕ) (h : a < 0) : false := by library_search -- says `exact not_lt_bot h`
-- An inductive type hides the constructor's arguments enough
-- so that `library_search` doesn't accidentally close the goal.
inductive P : ℕ → Prop
| gt_in_head {n : ℕ} : n < 0 → P n
-- This lemma with `>` as its head symbol should also be found for goals with head symbol `<`.
lemma lemma_with_gt_in_head (a : ℕ) (h : P a) : 0 > a := by { cases h, assumption }
-- This lemma with `false` as its head symbols should also be found for goals with head symbol `¬`.
lemma lemma_with_false_in_head (a b : ℕ) (h1 : a < b) (h2 : P a) : false :=
by { apply nat.not_lt_zero, cases h2, assumption }
example (a : ℕ) (h : P a) : 0 > a := by library_search -- says `exact lemma_with_gt_in_head a h`
example (a : ℕ) (h : P a) : a < 0 := by library_search -- says `exact lemma_with_gt_in_head a h`
example (a b : ℕ) (h1 : a < b) (h2 : P a) : false := by library_search
-- says `exact lemma_with_false_in_head a b h1 h2`
example (a b : ℕ) (h1 : a < b) : ¬ (P a) := by library_search!
-- says `exact lemma_with_false_in_head a b h1`
end synonym
-- We even find `iff` results:
example : ∀ P : Prop, ¬(P ↔ ¬P) :=
by library_search! -- says: `λ (a : Prop), (iff_not_self a).mp`
example {a b c : ℕ} (h₁ : a ∣ c) (h₂ : a ∣ b + c) : a ∣ b :=
by library_search -- says `exact (nat.dvd_add_left h₁).mp h₂`
-- Checking examples from issue #2220
example {α : Sort*} (h : empty) : α :=
by library_search
example {α : Type*} (h : empty) : α :=
by library_search
def map_from_sum {A B C : Type} (f : A → C) (g : B → C) : (A ⊕ B) → C := by library_search
-- Test that we can set the transparency level in `apply`
-- to change how aggressively we unfold definitions while trying to apply lemmas.
lemma bind_singleton {α β} (x : α) (f : α → list β) : list.bind [x] f = f x :=
begin
success_if_fail {
library_search { md := tactic.transparency.reducible }, },
library_search!,
end
constant f : ℕ → ℕ
axiom F (a b : ℕ) : f a ≤ f b ↔ a ≤ b
example (a b : ℕ) (h : a ≤ b) : f a ≤ f b := by library_search
-- Test #3432
theorem nonzero_gt_one (n : ℕ) : ¬ n = 0 → n ≥ 1 :=
by library_search! -- `exact nat.pos_of_ne_zero`
example (L : list (list ℕ)) : list ℕ :=
by library_search using L
example (n m : ℕ) : ℕ :=
by library_search using n m
example (P Q : list ℕ) (h : ℕ) : list ℕ :=
by library_search using h Q
example (P Q : list ℕ) (h : ℕ) : list ℕ :=
by library_search using P Q
-- Make sure `library_search` finds nothing when we list too many hypotheses after `using`.
example (P Q R S T : list ℕ) : list ℕ :=
begin
success_if_fail { library_search using P Q R S T, },
exact []
end
-- Tests for #3428
constants (x y w z : ℕ)
axiom not_axiom : ¬ x = y
axiom ne_axiom : w ≠ z
example : x ≠ y := by library_search
example : ¬ w = z := by library_search
structure foo := (a : nat) (b : nat)
constants (k l : foo)
axiom ne_axiom' (h : k.a ≠ 0) : k.b ≠ 0
axiom not_axiom' (h : l.a ≠ 0) : ¬ l.b = 0
example (hq : k.a ≠ 0) : k.b ≠ 0 := by library_search
example (hq : l.a ≠ 0) : ¬ l.b = 0 := by library_search
end test.library_search
|
248821ff84cc17374492b2134555bc2208a4f80d | c31182a012eec69da0a1f6c05f42b0f0717d212d | /src/pseudo_normed_group/Tinv.lean | 5000d6296235dde0fb3530e8dcd640dbfb8993f5 | [] | no_license | Ja1941/lean-liquid | fbec3ffc7fc67df1b5ca95b7ee225685ab9ffbdc | 8e80ed0cbdf5145d6814e833a674eaf05a1495c1 | refs/heads/master | 1,689,437,983,362 | 1,628,362,719,000 | 1,628,362,719,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 29,743 | lean | import pseudo_normed_group.CLC
/-!
# V-hat((M_c)^n)^{T⁻¹}
This file defines a fundamental construction defined just above Definition 9.3
in `analytic.pdf`: the subspac of V-hat(M_c^n) where the two actions of T⁻¹ coincide.
## Main definition
Here `M` is a profinitely filtered pseudo-normed group with `T⁻¹` scaling things by `r'`,
`V` is a seminormed group with `T⁻¹` scaling norms by `r`, `c` is a real (a filtration coefficient)
and `n` is a natural.
- `CLCFPTinv r V r' c n M`: the seminormed group defined as the subgroup of `V-hat(M_c^n)` where
the two actions of `T⁻¹` (one coming from the action on M, the other coming from the
action on V) coincide.
-/
open_locale classical nnreal
noncomputable theory
local attribute [instance] type_pow
namespace category_theory
theorem comm_sq₂ {C} [category C] {A₁ A₂ A₃ B₁ B₂ B₃ : C}
{f₁ : A₁ ⟶ B₁} {f₂ : A₂ ⟶ B₂} {f₃ : A₃ ⟶ B₃}
{a : A₁ ⟶ A₂} {a' : A₂ ⟶ A₃} {b : B₁ ⟶ B₂} {b' : B₂ ⟶ B₃}
(h₁ : a ≫ f₂ = f₁ ≫ b) (h₂ : a' ≫ f₃ = f₂ ≫ b') : (a ≫ a') ≫ f₃ = f₁ ≫ b ≫ b' :=
by rw [category.assoc, h₂, ← category.assoc, h₁, ← category.assoc]
end category_theory
open SemiNormedGroup opposite Profinite pseudo_normed_group category_theory breen_deligne
open profinitely_filtered_pseudo_normed_group category_theory.limits
open normed_group_hom
namespace SemiNormedGroup
def equalizer {V W : SemiNormedGroup} (f g : V ⟶ W) := of (f.equalizer g)
namespace equalizer
def ι {V W : SemiNormedGroup} (f g : V ⟶ W) :
equalizer f g ⟶ V :=
normed_group_hom.equalizer.ι _ _
@[reassoc] lemma condition {V W : SemiNormedGroup} (f g : V ⟶ W) :
ι f g ≫ f = ι f g ≫ g :=
normed_group_hom.equalizer.comp_ι_eq _ _
lemma ι_range {V W : SemiNormedGroup} (f g : V ⟶ W) :
(ι f g).range = (f - g).ker :=
begin
ext, rw [normed_group_hom.mem_range, normed_group_hom.mem_ker],
split,
{ rintro ⟨x, rfl⟩, rw [normed_group_hom.sub_apply], exact x.2 },
{ intro h, refine ⟨⟨x, h⟩, rfl⟩, }
end
lemma ι_range' {V W : SemiNormedGroup} (f g : V ⟶ W) :
(ι f g).range = (g - f).ker :=
begin
rw ι_range, ext x,
simp only [normed_group_hom.mem_ker, normed_group_hom.sub_apply, sub_eq_zero],
rw eq_comm
end
def map {V₁ V₂ W₁ W₂ : SemiNormedGroup} {f₁ f₂ g₁ g₂} (φ : V₁ ⟶ V₂) (ψ : W₁ ⟶ W₂)
(hf : φ ≫ f₂ = f₁ ≫ ψ) (hg : φ ≫ g₂ = g₁ ≫ ψ) :
equalizer f₁ g₁ ⟶ equalizer f₂ g₂ :=
normed_group_hom.equalizer.map _ _ hf.symm hg.symm
theorem map_congr
{V₁ V₂ W₁ W₂ : SemiNormedGroup} {f₁ f₂ g₁ g₂} {φ : V₁ ⟶ V₂} {ψ : W₁ ⟶ W₂}
{V₁' V₂' W₁' W₂' : SemiNormedGroup} {f₁' f₂' g₁' g₂'} {φ' : V₁' ⟶ V₂'} {ψ' : W₁' ⟶ W₂'}
{hf : φ ≫ f₂ = f₁ ≫ ψ} {hg : φ ≫ g₂ = g₁ ≫ ψ}
{hf' : φ' ≫ f₂' = f₁' ≫ ψ'} {hg' : φ' ≫ g₂' = g₁' ≫ ψ'}
(Hφ : arrow.mk φ = arrow.mk φ') (Hψ : arrow.mk ψ = arrow.mk ψ')
(Hf₁ : arrow.mk f₁ = arrow.mk f₁') (Hf₂ : arrow.mk f₂ = arrow.mk f₂')
(Hg₁ : arrow.mk g₁ = arrow.mk g₁') (Hg₂ : arrow.mk g₂ = arrow.mk g₂') :
arrow.mk (map φ ψ hf hg) = arrow.mk (map φ' ψ' hf' hg') :=
by { cases Hφ, cases Hψ, cases Hf₁, cases Hf₂, cases Hg₁, cases Hg₂, refl }
lemma map_comp_map {V₁ V₂ V₃ W₁ W₂ W₃ : SemiNormedGroup} {f₁ f₂ f₃ g₁ g₂ g₃}
{φ : V₁ ⟶ V₂} {ψ : W₁ ⟶ W₂} {φ' : V₂ ⟶ V₃} {ψ' : W₂ ⟶ W₃}
(hf : φ ≫ f₂ = f₁ ≫ ψ) (hg : φ ≫ g₂ = g₁ ≫ ψ)
(hf' : φ' ≫ f₃ = f₂ ≫ ψ') (hg' : φ' ≫ g₃ = g₂ ≫ ψ') :
map φ ψ hf hg ≫ map φ' ψ' hf' hg' =
map (φ ≫ φ') (ψ ≫ ψ') (comm_sq₂ hf hf') (comm_sq₂ hg hg') :=
by { ext, refl }
lemma map_id {J} [category J] {V W : SemiNormedGroup} (f g : V ⟶ W) :
map (𝟙 V) (𝟙 W) (show 𝟙 V ≫ f = f ≫ 𝟙 W, by simp) (show 𝟙 V ≫ g = g ≫ 𝟙 W, by simp) = 𝟙 _ :=
by { ext, refl }
lemma norm_map_le {V₁ V₂ W₁ W₂ : SemiNormedGroup} {f₁ f₂ g₁ g₂} {φ : V₁ ⟶ V₂} {ψ : W₁ ⟶ W₂}
(hf : φ ≫ f₂ = f₁ ≫ ψ) (hg : φ ≫ g₂ = g₁ ≫ ψ) (C : ℝ) (hφ : ∥ι f₁ g₁ ≫ φ∥ ≤ C) :
∥map φ ψ hf hg∥ ≤ C :=
normed_group_hom.equalizer.norm_map_le _ _ C hφ
@[simps obj map]
protected def F {J} [category J] {V W : J ⥤ SemiNormedGroup} (f g : V ⟶ W) : J ⥤ SemiNormedGroup :=
{ obj := λ X, of ((f.app X).equalizer (g.app X)),
map := λ X Y φ, equalizer.map (V.map φ) (W.map φ) (f.naturality _) (g.naturality _),
map_id' := λ X, by simp only [category_theory.functor.map_id]; exact normed_group_hom.equalizer.map_id,
map_comp' := λ X Y Z φ ψ, begin
simp only [functor.map_comp],
exact (map_comp_map _ _ _ _).symm
end }
@[simps]
def map_nat {J} [category J] {V₁ V₂ W₁ W₂ : J ⥤ SemiNormedGroup}
{f₁ f₂ g₁ g₂} (φ : V₁ ⟶ V₂) (ψ : W₁ ⟶ W₂)
(hf : φ ≫ f₂ = f₁ ≫ ψ) (hg : φ ≫ g₂ = g₁ ≫ ψ) :
equalizer.F f₁ g₁ ⟶ equalizer.F f₂ g₂ :=
{ app := λ X, equalizer.map (φ.app X) (ψ.app X)
(by rw [← nat_trans.comp_app, ← nat_trans.comp_app, hf])
(by rw [← nat_trans.comp_app, ← nat_trans.comp_app, hg]),
naturality' := λ X Y α, by simp only [equalizer.F_map, map_comp_map, nat_trans.naturality] }
lemma map_nat_comp_map_nat {J} [category J] {V₁ V₂ V₃ W₁ W₂ W₃ : J ⥤ SemiNormedGroup}
{f₁ f₂ f₃ g₁ g₂ g₃} {φ : V₁ ⟶ V₂} {ψ : W₁ ⟶ W₂} {φ' : V₂ ⟶ V₃} {ψ' : W₂ ⟶ W₃}
(hf : φ ≫ f₂ = f₁ ≫ ψ) (hg : φ ≫ g₂ = g₁ ≫ ψ)
(hf' : φ' ≫ f₃ = f₂ ≫ ψ') (hg' : φ' ≫ g₃ = g₂ ≫ ψ') :
map_nat φ ψ hf hg ≫ map_nat φ' ψ' hf' hg' =
map_nat (φ ≫ φ') (ψ ≫ ψ') (comm_sq₂ hf hf') (comm_sq₂ hg hg') :=
by { ext, refl }
lemma map_nat_id {J} [category J] {V W : J ⥤ SemiNormedGroup} (f g : V ⟶ W) :
map_nat (𝟙 V) (𝟙 W) (show 𝟙 V ≫ f = f ≫ 𝟙 W, by simp) (show 𝟙 V ≫ g = g ≫ 𝟙 W, by simp) = 𝟙 _ :=
by { ext, refl }
end equalizer
end SemiNormedGroup
universe variable u
variables (r : ℝ≥0) (V : SemiNormedGroup) [normed_with_aut r V] [fact (0 < r)]
variables (r' : ℝ≥0) [fact (0 < r')] [fact (r' ≤ 1)]
variables (M M₁ M₂ M₃ : ProFiltPseuNormGrpWithTinv.{u} r')
variables (c c₁ c₂ c₃ c₄ c₅ c₆ c₇ c₈ : ℝ≥0) (l m n : ℕ)
variables (f : M₁ ⟶ M₂) (g : M₂ ⟶ M₃)
def CLCTinv (r : ℝ≥0) (V : SemiNormedGroup)
[normed_with_aut r V] [fact (0 < r)] {A B : Profiniteᵒᵖ} (f g : A ⟶ B) :
SemiNormedGroup :=
SemiNormedGroup.of $ normed_group_hom.equalizer
((CLC V).map f)
((CLC V).map g ≫ (CLC.T_inv r V).app B)
namespace CLCTinv
def ι (r : ℝ≥0) (V : SemiNormedGroup)
[normed_with_aut r V] [fact (0 < r)] {A B : Profiniteᵒᵖ} (f g : A ⟶ B) :
CLCTinv r V f g ⟶ (CLC V).obj A :=
SemiNormedGroup.equalizer.ι _ _
lemma ι_range (r : ℝ≥0) (V : SemiNormedGroup)
[normed_with_aut r V] [fact (0 < r)] {A B : Profiniteᵒᵖ} (f g : A ⟶ B) :
(ι r V f g).range =
normed_group_hom.ker ((CLC V).map f - ((CLC V).map g ≫ (CLC.T_inv r V).app B)) :=
SemiNormedGroup.equalizer.ι_range _ _
lemma ι_range' (r : ℝ≥0) (V : SemiNormedGroup)
[normed_with_aut r V] [fact (0 < r)] {A B : Profiniteᵒᵖ} (f g : A ⟶ B) :
(ι r V f g).range =
normed_group_hom.ker (((CLC V).map g ≫ (CLC.T_inv r V).app B) - (CLC V).map f) :=
SemiNormedGroup.equalizer.ι_range' _ _
def map {A₁ B₁ A₂ B₂ : Profiniteᵒᵖ} (f₁ g₁ : A₁ ⟶ B₁) (f₂ g₂ : A₂ ⟶ B₂)
(ϕ : A₁ ⟶ A₂) (ψ : B₁ ⟶ B₂) (h₁ : ϕ ≫ f₂ = f₁ ≫ ψ) (h₂ : ϕ ≫ g₂ = g₁ ≫ ψ) :
CLCTinv r V f₁ g₁ ⟶ CLCTinv r V f₂ g₂ :=
SemiNormedGroup.equalizer.map ((CLC V).map ϕ) ((CLC V).map ψ)
(by rw [← functor.map_comp, ← functor.map_comp, h₁]) $
by rw [← category.assoc, ← functor.map_comp, h₂, functor.map_comp,
category.assoc, (CLC.T_inv _ _).naturality, category.assoc]
def map_comp_ι {A₁ B₁ A₂ B₂ : Profiniteᵒᵖ} (f₁ g₁ : A₁ ⟶ B₁) (f₂ g₂ : A₂ ⟶ B₂)
(ϕ : A₁ ⟶ A₂) (ψ : B₁ ⟶ B₂) (h₁ : ϕ ≫ f₂ = f₁ ≫ ψ) (h₂ : ϕ ≫ g₂ = g₁ ≫ ψ) :
map r V f₁ g₁ f₂ g₂ ϕ ψ h₁ h₂ ≫ ι r V _ _ = ι _ _ _ _ ≫ (CLC V).map ϕ :=
normed_group_hom.equalizer.ι_comp_map _ _
lemma map_norm_noninc {A₁ B₁ A₂ B₂ : Profiniteᵒᵖ} (f₁ g₁ : A₁ ⟶ B₁) (f₂ g₂ : A₂ ⟶ B₂)
(ϕ : A₁ ⟶ A₂) (ψ : B₁ ⟶ B₂) (h₁ h₂) :
(CLCTinv.map r V f₁ g₁ f₂ g₂ ϕ ψ h₁ h₂).norm_noninc :=
equalizer.map_norm_noninc _ _ $ CLC.map_norm_noninc _ _
lemma norm_map_le {A₁ B₁ A₂ B₂ : Profiniteᵒᵖ} (f₁ g₁ : A₁ ⟶ B₁) (f₂ g₂ : A₂ ⟶ B₂)
(ϕ : A₁ ⟶ A₂) (ψ : B₁ ⟶ B₂) (h₁ h₂) (C : ℝ≥0)
(H : ∥SemiNormedGroup.equalizer.ι
((CLC V).map f₁)
((CLC V).map g₁ ≫ (CLC.T_inv r V).app B₁) ≫
(CLC V).map ϕ∥ ≤ C) :
∥CLCTinv.map r V f₁ g₁ f₂ g₂ ϕ ψ h₁ h₂∥ ≤ C :=
SemiNormedGroup.equalizer.norm_map_le _ _ C H
@[simp] lemma map_id {A B : Profiniteᵒᵖ} (f g : A ⟶ B) :
map r V f g f g (𝟙 A) (𝟙 B) rfl rfl = 𝟙 _ :=
begin
simp only [map, SemiNormedGroup.equalizer.map, category_theory.functor.map_id],
exact equalizer.map_id,
end
lemma map_comp {A₁ A₂ A₃ B₁ B₂ B₃ : Profiniteᵒᵖ}
{f₁ g₁ : A₁ ⟶ B₁} {f₂ g₂ : A₂ ⟶ B₂} {f₃ g₃ : A₃ ⟶ B₃}
(ϕ₁ : A₁ ⟶ A₂) (ϕ₂ : A₂ ⟶ A₃) (ψ₁ : B₁ ⟶ B₂) (ψ₂ : B₂ ⟶ B₃)
(h1 h2 h3 h4 h5 h6) :
CLCTinv.map r V f₁ g₁ f₃ g₃ (ϕ₁ ≫ ϕ₂) (ψ₁ ≫ ψ₂) h1 h2 =
CLCTinv.map r V f₁ g₁ f₂ g₂ ϕ₁ ψ₁ h3 h4 ≫
CLCTinv.map r V f₂ g₂ f₃ g₃ ϕ₂ ψ₂ h5 h6 :=
begin
simp only [map, SemiNormedGroup.equalizer.map, category_theory.functor.map_comp],
exact (equalizer.map_comp_map _ _ _ _).symm,
end
lemma map_comp_map {A₁ A₂ A₃ B₁ B₂ B₃ : Profiniteᵒᵖ}
{f₁ g₁ : A₁ ⟶ B₁} {f₂ g₂ : A₂ ⟶ B₂} {f₃ g₃ : A₃ ⟶ B₃}
(ϕ₁ : A₁ ⟶ A₂) (ϕ₂ : A₂ ⟶ A₃) (ψ₁ : B₁ ⟶ B₂) (ψ₂ : B₂ ⟶ B₃)
(h₁ h₂ h₃ h₄) :
CLCTinv.map r V f₁ g₁ f₂ g₂ ϕ₁ ψ₁ h₁ h₂ ≫
CLCTinv.map r V f₂ g₂ f₃ g₃ ϕ₂ ψ₂ h₃ h₄ =
CLCTinv.map r V f₁ g₁ f₃ g₃ (ϕ₁ ≫ ϕ₂) (ψ₁ ≫ ψ₂) (comm_sq₂ h₁ h₃) (comm_sq₂ h₂ h₄) :=
(map_comp _ _ _ _ _ _ _ _ _ _ _ _).symm
@[simps]
def map_iso {A₁ B₁ A₂ B₂ : Profiniteᵒᵖ} (f₁ g₁ : A₁ ⟶ B₁) (f₂ g₂ : A₂ ⟶ B₂)
(ϕ : A₁ ≅ A₂) (ψ : B₁ ≅ B₂) (h₁ : ϕ.hom ≫ f₂ = f₁ ≫ ψ.hom) (h₂ : ϕ.hom ≫ g₂ = g₁ ≫ ψ.hom) :
CLCTinv r V f₁ g₁ ≅ CLCTinv r V f₂ g₂ :=
{ hom := map r V f₁ g₁ f₂ g₂ ϕ.hom ψ.hom h₁ h₂,
inv := map r V f₂ g₂ f₁ g₁ ϕ.inv ψ.inv
(by rw [iso.inv_comp_eq, ← category.assoc, iso.eq_comp_inv, h₁])
(by rw [iso.inv_comp_eq, ← category.assoc, iso.eq_comp_inv, h₂]),
hom_inv_id' := by { simp only [map_comp_map, iso.hom_inv_id], apply map_id },
inv_hom_id' := by { simp only [map_comp_map, iso.inv_hom_id], apply map_id } }
lemma map_iso_isometry {A₁ B₁ A₂ B₂ : Profiniteᵒᵖ} (f₁ g₁ : A₁ ⟶ B₁) (f₂ g₂ : A₂ ⟶ B₂)
(ϕ : A₁ ≅ A₂) (ψ : B₁ ≅ B₂) (h₁ : ϕ.hom ≫ f₂ = f₁ ≫ ψ.hom) (h₂ : ϕ.hom ≫ g₂ = g₁ ≫ ψ.hom) :
isometry (map_iso r V f₁ g₁ f₂ g₂ ϕ ψ h₁ h₂).hom :=
begin
apply SemiNormedGroup.iso_isometry_of_norm_noninc;
apply map_norm_noninc
end
@[simps]
protected def F {J} [category J] (r : ℝ≥0) (V : SemiNormedGroup)
[normed_with_aut r V] [fact (0 < r)] {A B : J ⥤ Profiniteᵒᵖ} (f g : A ⟶ B) :
J ⥤ SemiNormedGroup :=
{ obj := λ X, CLCTinv r V (f.app X) (g.app X),
map := λ X Y φ, map _ _ _ _ _ _ (A.map φ) (B.map φ) (f.naturality _) (g.naturality _),
map_id' := λ X, by simp only [category_theory.functor.map_id]; apply map_id,
map_comp' := λ X Y Z φ ψ, by simp only [functor.map_comp]; apply map_comp }
theorem F_def {J} [category J] (r : ℝ≥0) (V : SemiNormedGroup)
[normed_with_aut r V] [fact (0 < r)] {A B : J ⥤ Profiniteᵒᵖ} (f g : A ⟶ B) :
CLCTinv.F r V f g = SemiNormedGroup.equalizer.F
(whisker_right f (CLC V))
(whisker_right g (CLC V) ≫ whisker_left B (CLC.T_inv r V)) := rfl
@[simps]
def map_nat {J} [category J] {A₁ B₁ A₂ B₂ : J ⥤ Profiniteᵒᵖ} (f₁ g₁ : A₁ ⟶ B₁) (f₂ g₂ : A₂ ⟶ B₂)
(ϕ : A₁ ⟶ A₂) (ψ : B₁ ⟶ B₂) (h₁ : ϕ ≫ f₂ = f₁ ≫ ψ) (h₂ : ϕ ≫ g₂ = g₁ ≫ ψ) :
CLCTinv.F r V f₁ g₁ ⟶ CLCTinv.F r V f₂ g₂ :=
{ app := λ X, map _ _ _ _ _ _ (ϕ.app X) (ψ.app X)
(by rw [← nat_trans.comp_app, h₁, nat_trans.comp_app])
(by rw [← nat_trans.comp_app, h₂, nat_trans.comp_app]),
naturality' := λ X Y α, by simp only [CLCTinv.F_map, map_comp_map, ϕ.naturality, ψ.naturality] }
theorem map_nat_def {J} [category J] {A₁ B₁ A₂ B₂ : J ⥤ Profiniteᵒᵖ} (f₁ g₁ : A₁ ⟶ B₁) (f₂ g₂ : A₂ ⟶ B₂)
(ϕ : A₁ ⟶ A₂) (ψ : B₁ ⟶ B₂) (h₁ : ϕ ≫ f₂ = f₁ ≫ ψ) (h₂ : ϕ ≫ g₂ = g₁ ≫ ψ) :
map_nat r V f₁ g₁ f₂ g₂ ϕ ψ h₁ h₂ = begin
dsimp only [F_def],
refine SemiNormedGroup.equalizer.map_nat
(whisker_right ϕ (CLC V))
(whisker_right ψ (CLC V))
(by rw [← whisker_right_comp, ← whisker_right_comp, h₁])
(comm_sq₂ _ _).symm,
{ exact whisker_right ψ _ },
{ rw [← whisker_right_comp, ← whisker_right_comp, h₂] },
ext x : 2,
simp only [nat_trans.comp_app, whisker_left_app, whisker_right_app,
(CLC.T_inv _ _).naturality],
end := rfl
.
-- @[simps]
def map_nat_iso {J} [category J] {A₁ B₁ A₂ B₂ : J ⥤ Profiniteᵒᵖ} (f₁ g₁ : A₁ ⟶ B₁) (f₂ g₂ : A₂ ⟶ B₂)
(ϕ : A₁ ≅ A₂) (ψ : B₁ ≅ B₂) (h₁ : ϕ.hom ≫ f₂ = f₁ ≫ ψ.hom) (h₂ : ϕ.hom ≫ g₂ = g₁ ≫ ψ.hom) :
CLCTinv.F r V f₁ g₁ ≅ CLCTinv.F r V f₂ g₂ :=
{ hom := map_nat r V f₁ g₁ f₂ g₂ ϕ.hom ψ.hom h₁ h₂,
inv := map_nat r V f₂ g₂ f₁ g₁ ϕ.inv ψ.inv
(by rw [iso.inv_comp_eq, ← category.assoc, iso.eq_comp_inv, h₁])
(by rw [iso.inv_comp_eq, ← category.assoc, iso.eq_comp_inv, h₂]),
hom_inv_id' :=
begin
simp only [map_nat_def, _root_.id, SemiNormedGroup.equalizer.map_nat_comp_map_nat,
← whisker_right_comp, iso.hom_inv_id, whisker_right_id', SemiNormedGroup.equalizer.map_nat_id],
refl
end,
inv_hom_id' :=
begin
simp only [map_nat_def, _root_.id, SemiNormedGroup.equalizer.map_nat_comp_map_nat,
← whisker_right_comp, iso.inv_hom_id, whisker_right_id', SemiNormedGroup.equalizer.map_nat_id],
refl
end, }
end CLCTinv
lemma aux (r' c c₂ : ℝ≥0) [r1 : fact (r' ≤ 1)] [h : fact (c₂ ≤ r' * c)] : fact (c₂ ≤ c) :=
⟨h.1.trans $ (mul_le_mul' r1.1 le_rfl).trans (by simp)⟩
@[simps obj]
def CLCFPTinv₂ (r : ℝ≥0) (V : SemiNormedGroup)
(r' : ℝ≥0) [fact (0 < r)] [fact (0 < r')] [r1 : fact (r' ≤ 1)] [normed_with_aut r V]
(c c₂ : ℝ≥0) [fact (c₂ ≤ r' * c)] (n : ℕ) : (ProFiltPseuNormGrpWithTinv r')ᵒᵖ ⥤ SemiNormedGroup :=
by haveI : fact (c₂ ≤ c) := aux r' c c₂; exact
CLCTinv.F r V
(nat_trans.op (FiltrationPow.Tinv r' c₂ c n))
(nat_trans.op (FiltrationPow.cast_le r' c₂ c n))
theorem CLCFPTinv₂_def (r : ℝ≥0) (V : SemiNormedGroup)
(r' : ℝ≥0) [fact (0 < r)] [fact (0 < r')] [r1 : fact (r' ≤ 1)] [normed_with_aut r V]
(c c₂ : ℝ≥0) [fact (c₂ ≤ r' * c)] (n : ℕ) :
CLCFPTinv₂ r V r' c c₂ n = SemiNormedGroup.equalizer.F
(CLCFP.Tinv V r' c c₂ n)
(@CLCFP.res V r' c c₂ n (aux r' c c₂) ≫ CLCFP.T_inv r V r' c₂ n) := rfl
instance CLCFPTinv₂.separated_space [fact (c₂ ≤ r' * c₁)] (M) :
separated_space ((CLCFPTinv₂ r V r' c₁ c₂ n).obj M) :=
begin
rw separated_iff_t2,
refine @subtype.t2_space _ _ (id _) (id _),
rw ← separated_iff_t2,
apply uniform_space.completion.separated_space
end
instance CLCFPTinv₂.complete_space [fact (c₂ ≤ r' * c₁)] (M) :
complete_space ((CLCFPTinv₂ r V r' c₁ c₂ n).obj M) :=
begin
refine @is_closed.complete_space_coe _ (id _) (id _) _ _,
{ apply uniform_space.completion.complete_space },
{ refine is_closed_eq _ continuous_const,
apply normed_group_hom.continuous }
end
/-- The functor that sends `M` and `c` to `V-hat((filtration M c)^n)^{T⁻¹}`,
defined by taking `T⁻¹`-invariants for two different actions by `T⁻¹`:
* The first comes from the action of `T⁻¹` on `M`.
* The second comes from the action of `T⁻¹` on `V`.
We take the equalizer of those two actions.
See the lines just above Definition 9.3 of [Analytic]. -/
def CLCFPTinv (r : ℝ≥0) (V : SemiNormedGroup) (r' : ℝ≥0)
(c : ℝ≥0) (n : ℕ) [normed_with_aut r V] [fact (0 < r)] [fact (0 < r')] [fact (r' ≤ 1)] :
(ProFiltPseuNormGrpWithTinv r')ᵒᵖ ⥤ SemiNormedGroup :=
CLCFPTinv₂ r V r' c (r' * c) n
namespace CLCFPTinv₂
lemma map_norm_noninc [fact (c₂ ≤ r' * c)] [fact (c₂ ≤ c)]
{M₁ M₂} (f : M₁ ⟶ M₂) : ((CLCFPTinv₂ r V r' c c₂ n).map f).norm_noninc :=
CLCTinv.map_norm_noninc _ _ _ _ _ _ _ _ _ _
def res [fact (c₂ ≤ r' * c₁)] [fact (c₂ ≤ c₁)] [fact (c₄ ≤ r' * c₃)] [fact (c₄ ≤ c₃)]
[fact (c₃ ≤ c₁)] [fact (c₄ ≤ c₂)] : CLCFPTinv₂ r V r' c₁ c₂ n ⟶ CLCFPTinv₂ r V r' c₃ c₄ n :=
CLCTinv.map_nat r V _ _ _ _
(nat_trans.op (FiltrationPow.cast_le _ c₃ c₁ n))
(nat_trans.op (FiltrationPow.cast_le _ c₄ c₂ n)) rfl rfl
@[simp] lemma res_refl [fact (c₂ ≤ r' * c₁)] [fact (c₂ ≤ c₁)] : res r V r' c₁ c₂ c₁ c₂ n = 𝟙 _ :=
by { simp only [res, FiltrationPow.cast_le_refl, nat_trans.op_id], ext x : 2, apply CLCTinv.map_id }
lemma res_comp_res
[fact (c₂ ≤ r' * c₁)] [fact (c₂ ≤ c₁)]
[fact (c₄ ≤ r' * c₃)] [fact (c₄ ≤ c₃)]
[fact (c₆ ≤ r' * c₅)] [fact (c₆ ≤ c₅)]
[fact (c₃ ≤ c₁)] [fact (c₄ ≤ c₂)]
[fact (c₅ ≤ c₃)] [fact (c₆ ≤ c₄)]
[fact (c₅ ≤ c₁)] [fact (c₆ ≤ c₂)] :
res r V r' c₁ c₂ c₃ c₄ n ≫ res r V r' c₃ c₄ c₅ c₆ n = res r V r' c₁ c₂ c₅ c₆ n :=
begin
ext x : 2, simp only [res, nat_trans.comp_app],
exact (CLCTinv.map_comp _ _ _ _ _ _ _ _ _ _ _ _).symm
end
lemma res_norm_noninc {_ : fact (c₂ ≤ r' * c₁)} {_ : fact (c₂ ≤ c₁)}
{_ : fact (c₄ ≤ r' * c₃)} {_ : fact (c₄ ≤ c₃)} {_ : fact (c₃ ≤ c₁)} {_ : fact (c₄ ≤ c₂)} (M) :
((res r V r' c₁ c₂ c₃ c₄ n).app M).norm_noninc :=
CLCTinv.map_norm_noninc _ _ _ _ _ _ _ _ _ _
lemma norm_res_le [fact (c₂ ≤ r' * c₁)] [fact (c₂ ≤ c₁)] [fact (c₄ ≤ r' * c₃)] [fact (c₄ ≤ c₃)]
[fact (c₃ ≤ c₁)] [fact (c₄ ≤ c₂)] (h₂₃ : c₂ = c₃) (M) :
∥(res r V r' c₁ c₂ c₃ c₄ n).app M∥ ≤ r :=
begin
apply CLCTinv.norm_map_le,
rw [← category.comp_id ((CLC V).map ((nat_trans.op (FiltrationPow.cast_le r' c₃ c₁ n)).app M))],
have := nat_trans.congr_app (CLC.T r V).inv_hom_id ((FiltrationPow r' c₃ n).op.obj M),
dsimp only [nat_trans.id_app] at this,
rw [← this, CLC.T_inv_eq, nat_trans.comp_app, ← category.assoc ((CLC V).map _)],
unfreezingI { subst c₃ },
rw [← SemiNormedGroup.equalizer.condition_assoc, ← category.assoc],
refine normed_group_hom.norm_comp_le_of_le' 1 r r (mul_one ↑r).symm _ _,
{ apply CLC.norm_T_le },
{ apply norm_noninc.norm_noninc_iff_norm_le_one.1,
exact (CLC.map_norm_noninc V _).comp equalizer.ι_norm_noninc }
end
end CLCFPTinv₂
namespace CLCFPTinv
lemma map_norm_noninc {M₁ M₂} (f : M₁ ⟶ M₂) : ((CLCFPTinv r V r' c n).map f).norm_noninc :=
CLCFPTinv₂.map_norm_noninc _ _ _ _ _ _ _
def res [fact (c₂ ≤ c₁)] : CLCFPTinv r V r' c₁ n ⟶ CLCFPTinv r V r' c₂ n :=
CLCFPTinv₂.res r V r' c₁ _ c₂ _ n
@[simp] lemma res_refl : res r V r' c₁ c₁ n = 𝟙 _ :=
CLCFPTinv₂.res_refl _ _ _ _ _ _
lemma res_comp_res [fact (c₃ ≤ c₁)] [fact (c₅ ≤ c₃)] [fact (c₅ ≤ c₁)] :
res r V r' c₁ c₃ n ≫ res r V r' c₃ c₅ n = res r V r' c₁ c₅ n :=
CLCFPTinv₂.res_comp_res _ _ _ _ _ _ _ _ _ _
lemma res_norm_noninc {_ : fact (c₂ ≤ c₁)} (M) :
((res r V r' c₁ c₂ n).app M).norm_noninc :=
CLCFPTinv₂.res_norm_noninc r V r' _ _ _ _ _ _
lemma norm_res_le [fact (c₂ ≤ c₁)] [fact (c₂ ≤ r' * c₁)] (M) :
∥(res r V r' c₁ c₂ n).app M∥ ≤ r :=
begin
rw ← res_comp_res r V r' c₁ (r' * c₁) c₂,
refine norm_comp_le_of_le' _ _ _ (one_mul ↑r).symm _ (CLCFPTinv₂.norm_res_le r V r' _ _ _ _ n rfl M),
apply norm_noninc.norm_noninc_iff_norm_le_one.1,
exact CLCTinv.map_norm_noninc r V _ _ _ _ _ _ _ _
end
lemma norm_res_le_pow (N : ℕ) [fact (c₂ ≤ c₁)] [h : fact (c₂ ≤ r' ^ N * c₁)] (M) :
∥(res r V r' c₁ c₂ n).app M∥ ≤ (r ^ N) :=
begin
unfreezingI { induction N with N ih generalizing c₁ c₂ },
{ rw pow_zero,
apply norm_noninc.norm_noninc_iff_norm_le_one.1,
exact CLCTinv.map_norm_noninc r V _ _ _ _ _ _ _ _ },
haveI : fact (c₂ ≤ r' ^ N * c₁) := nnreal.fact_le_pow_mul_of_le_pow_succ_mul _ _ _,
rw [pow_succ, mul_assoc] at h, resetI,
rw [← res_comp_res r V r' c₁ (r' ^ N * c₁) c₂],
exact norm_comp_le_of_le' _ _ _ (pow_succ _ _) (norm_res_le r V r' _ _ n M) (ih _ _)
end
end CLCFPTinv
namespace breen_deligne
open CLCFPTinv
variables (M) {l m n}
namespace universal_map
variables (ϕ ψ : universal_map m n)
def eval_CLCFPTinv₂
[fact (c₂ ≤ r' * c₁)] [fact (c₄ ≤ r' * c₃)]
[ϕ.suitable c₃ c₁] [ϕ.suitable c₄ c₂] :
CLCFPTinv₂ r V r' c₁ c₂ n ⟶ CLCFPTinv₂ r V r' c₃ c₄ m :=
begin
dsimp only [CLCFPTinv₂_def],
refine SemiNormedGroup.equalizer.map_nat (ϕ.eval_CLCFP _ _ _ _) (ϕ.eval_CLCFP _ _ _ _)
(Tinv_comp_eval_CLCFP V r' c₁ c₂ c₃ c₄ ϕ).symm _,
haveI : fact (c₂ ≤ c₁) := aux r' _ _, haveI : fact (c₄ ≤ c₃) := aux r' _ _,
have h₁ := res_comp_eval_CLCFP V r' c₁ c₂ c₃ c₄ ϕ,
have h₂ := T_inv_comp_eval_CLCFP r V r' c₂ c₄ ϕ,
have := comm_sq₂ h₁ h₂,
exact this.symm
end
@[simp] lemma eval_CLCFPTinv₂_zero
[fact (c₂ ≤ r' * c₁)] [fact (c₄ ≤ r' * c₃)] :
(0 : universal_map m n).eval_CLCFPTinv₂ r V r' c₁ c₂ c₃ c₄ = 0 :=
by { simp only [eval_CLCFPTinv₂, eval_CLCFP_zero], ext, refl }
@[simp] lemma eval_CLCFPTinv₂_add
[fact (c₂ ≤ r' * c₁)] [fact (c₄ ≤ r' * c₃)]
[ϕ.suitable c₃ c₁] [ϕ.suitable c₄ c₂]
[ψ.suitable c₃ c₁] [ψ.suitable c₄ c₂] :
(ϕ + ψ : universal_map m n).eval_CLCFPTinv₂ r V r' c₁ c₂ c₃ c₄ =
ϕ.eval_CLCFPTinv₂ r V r' c₁ c₂ c₃ c₄ + ψ.eval_CLCFPTinv₂ r V r' c₁ c₂ c₃ c₄ :=
by { simp only [eval_CLCFPTinv₂, eval_CLCFP_add], ext, refl }
@[simp] lemma eval_CLCFPTinv₂_sub
[fact (c₂ ≤ r' * c₁)] [fact (c₄ ≤ r' * c₃)]
[ϕ.suitable c₃ c₁] [ϕ.suitable c₄ c₂]
[ψ.suitable c₃ c₁] [ψ.suitable c₄ c₂] :
(ϕ - ψ : universal_map m n).eval_CLCFPTinv₂ r V r' c₁ c₂ c₃ c₄ =
ϕ.eval_CLCFPTinv₂ r V r' c₁ c₂ c₃ c₄ - ψ.eval_CLCFPTinv₂ r V r' c₁ c₂ c₃ c₄ :=
by { simp only [eval_CLCFPTinv₂, eval_CLCFP_sub], ext, refl }
lemma eval_CLCFPTinv₂_comp {l m n : FreeMat} (f : l ⟶ m) (g : m ⟶ n)
[fact (c₂ ≤ r' * c₁)] [fact (c₄ ≤ r' * c₃)] [fact (c₆ ≤ r' * c₅)]
[f.suitable c₅ c₃] [f.suitable c₆ c₄] [g.suitable c₃ c₁] [g.suitable c₄ c₂] :
@eval_CLCFPTinv₂ r V _ _ r' _ _ c₁ c₂ c₅ c₆ _ _ (f ≫ g)
_ _ (suitable.comp c₃) (suitable.comp c₄) =
g.eval_CLCFPTinv₂ r V r' c₁ c₂ c₃ c₄ ≫ f.eval_CLCFPTinv₂ r V r' c₃ c₄ c₅ c₆ :=
begin
dsimp only [eval_CLCFPTinv₂, CLCFPTinv₂_def], delta id,
simp only [SemiNormedGroup.equalizer.map_nat_comp_map_nat],
generalize_proofs h1 h2 h3 h4 h5 h6 h7 h8,
revert h5 h6 h7 h8, resetI,
have H1 : eval_CLCFP V r' c₁ c₅ (f ≫ g) = eval_CLCFP V r' c₁ c₃ g ≫ eval_CLCFP V r' c₃ c₅ f :=
eval_CLCFP_comp V r' c₁ c₃ c₅ g f,
have H2 : eval_CLCFP V r' c₂ c₆ (f ≫ g) = eval_CLCFP V r' c₂ c₄ g ≫ eval_CLCFP V r' c₄ c₆ f :=
eval_CLCFP_comp V r' c₂ c₄ c₆ g f,
rw [H1, H2],
intros, refl,
end
lemma res_comp_eval_CLCFPTinv₂
[fact (c₂ ≤ r' * c₁)] [fact (c₄ ≤ r' * c₃)]
[fact (c₆ ≤ r' * c₅)] [fact (c₈ ≤ r' * c₇)]
[fact (c₂ ≤ c₁)] [fact (c₃ ≤ c₁)] [fact (c₄ ≤ c₂)] [fact (c₄ ≤ c₃)]
[fact (c₆ ≤ c₅)] [fact (c₇ ≤ c₅)] [fact (c₈ ≤ c₆)] [fact (c₈ ≤ c₇)]
[ϕ.suitable c₅ c₁] [ϕ.suitable c₆ c₂]
[ϕ.suitable c₇ c₃] [ϕ.suitable c₈ c₄] :
CLCFPTinv₂.res r V r' c₁ c₂ c₃ c₄ n ≫ ϕ.eval_CLCFPTinv₂ r V r' c₃ c₄ c₇ c₈ =
ϕ.eval_CLCFPTinv₂ r V r' c₁ c₂ c₅ c₆ ≫ CLCFPTinv₂.res r V r' c₅ c₆ c₇ c₈ m :=
begin
dsimp only [CLCFPTinv₂.res, eval_CLCFPTinv₂, CLCFPTinv₂_def, CLCTinv.map_nat_def], delta id,
simp only [SemiNormedGroup.equalizer.map_nat_comp_map_nat],
congr' 1; { simp only [← CLCFP.res_def], apply res_comp_eval_CLCFP },
end
lemma norm_eval_CLCFPTinv₂_le [fact (c₂ ≤ r' * c₁)] [fact (c₄ ≤ r' * c₃)]
[ϕ.suitable c₃ c₁] [ϕ.suitable c₄ c₂] (N : ℕ) (h : ϕ.bound_by N) (M) :
∥(ϕ.eval_CLCFPTinv₂ r V r' c₁ c₂ c₃ c₄).app M∥ ≤ N :=
begin
apply SemiNormedGroup.equalizer.norm_map_le,
refine normed_group_hom.norm_comp_le_of_le' _ _ _ (mul_one _).symm _ _,
{ apply norm_eval_CLCFP_le, exact h },
{ apply norm_noninc.norm_noninc_iff_norm_le_one.1,
exact equalizer.ι_norm_noninc }
end
def eval_CLCFPTinv [ϕ.suitable c₂ c₁] :
CLCFPTinv r V r' c₁ n ⟶ CLCFPTinv r V r' c₂ m :=
ϕ.eval_CLCFPTinv₂ r V r' c₁ _ c₂ _
lemma eval_CLCFPTinv_def [ϕ.suitable c₂ c₁] :
ϕ.eval_CLCFPTinv r V r' c₁ c₂ = ϕ.eval_CLCFPTinv₂ r V r' c₁ _ c₂ _ := rfl
@[simp] lemma eval_CLCFPTinv_zero :
(0 : universal_map m n).eval_CLCFPTinv r V r' c₁ c₂ = 0 :=
by apply eval_CLCFPTinv₂_zero
@[simp] lemma eval_CLCFPTinv_add [ϕ.suitable c₂ c₁] [ψ.suitable c₂ c₁] :
(ϕ + ψ : universal_map m n).eval_CLCFPTinv r V r' c₁ c₂ =
ϕ.eval_CLCFPTinv r V r' c₁ c₂ + ψ.eval_CLCFPTinv r V r' c₁ c₂ :=
eval_CLCFPTinv₂_add _ _ _ _ _ _ _ _ _
@[simp] lemma eval_CLCFPTinv_sub [ϕ.suitable c₂ c₁] [ψ.suitable c₂ c₁] :
(ϕ - ψ : universal_map m n).eval_CLCFPTinv r V r' c₁ c₂ =
ϕ.eval_CLCFPTinv r V r' c₁ c₂ - ψ.eval_CLCFPTinv r V r' c₁ c₂ :=
eval_CLCFPTinv₂_sub _ _ _ _ _ _ _ _ _
lemma eval_CLCFPTinv_comp {l m n : FreeMat} (f : l ⟶ m) (g : m ⟶ n)
[hg : g.suitable c₂ c₁] [hf : f.suitable c₃ c₂] :
@eval_CLCFPTinv r V _ _ r' _ _ c₁ c₃ _ _ (f ≫ g) (suitable.comp c₂) =
g.eval_CLCFPTinv r V r' c₁ c₂ ≫ f.eval_CLCFPTinv r V r' c₂ c₃ :=
by apply eval_CLCFPTinv₂_comp
lemma res_comp_eval_CLCFPTinv
[fact (c₂ ≤ c₁)] [ϕ.suitable c₄ c₂] [ϕ.suitable c₃ c₁] [fact (c₄ ≤ c₃)] :
res r V r' c₁ c₂ n ≫ ϕ.eval_CLCFPTinv r V r' c₂ c₄ =
ϕ.eval_CLCFPTinv r V r' c₁ c₃ ≫ res r V r' c₃ c₄ m :=
by apply res_comp_eval_CLCFPTinv₂
lemma res_comp_eval_CLCFPTinv_absorb
[fact (c₂ ≤ c₁)] [hϕ : ϕ.suitable c₃ c₂] :
res r V r' c₁ c₂ n ≫ ϕ.eval_CLCFPTinv r V r' c₂ c₃ =
@eval_CLCFPTinv r V _ _ r' _ _ c₁ c₃ _ _ ϕ (hϕ.le _ _ _ _ le_rfl (fact.out _)) :=
by rw [@res_comp_eval_CLCFPTinv r V _ _ r' _ _ c₁ c₂ c₃ c₃ _ _ ϕ
(_root_.id _) (_root_.id _) (_root_.id _) (_root_.id _),
res_refl, category.comp_id]
lemma eval_CLCFPTinv_comp_res_absorb
{_: fact (c₃ ≤ c₂)} [hϕ : ϕ.suitable c₂ c₁] :
ϕ.eval_CLCFPTinv r V r' c₁ c₂ ≫ res r V r' c₂ c₃ m =
@eval_CLCFPTinv r V _ _ r' _ _ c₁ c₃ _ _ ϕ (hϕ.le _ _ _ _ (fact.out _) le_rfl) :=
by rw [← @res_comp_eval_CLCFPTinv r V _ _ r' _ _ c₁ c₁ c₂ c₃ _ _ ϕ
(_root_.id _) (_root_.id _) (_root_.id _) (_root_.id _),
res_refl, category.id_comp]
lemma norm_eval_CLCFPTinv_le [normed_with_aut r V] [fact (0 < r)] [ϕ.suitable c₂ c₁]
(N : ℕ) (h : ϕ.bound_by N) (M) :
∥(ϕ.eval_CLCFPTinv r V r' c₁ c₂).app M∥ ≤ N :=
norm_eval_CLCFPTinv₂_le r V r' _ _ _ _ _ N h M
lemma eval_CLCFPTinv_norm_noninc [normed_with_aut r V] [fact (0 < r)]
[h : ϕ.very_suitable r r' c₂ c₁] (M) :
((ϕ.eval_CLCFPTinv r V r' c₁ c₂).app M).norm_noninc :=
begin
apply norm_noninc.norm_noninc_iff_norm_le_one.2,
have h' := h,
unfreezingI { rcases h with ⟨N, k, c', hN, hϕ, hr, H⟩ },
haveI : fact (c' ≤ c₁) := ⟨H.trans $ fact.out _⟩,
have aux := res_comp_eval_CLCFPTinv r V r' c₁ c' c₂ c₂ ϕ,
rw [res_refl, category.comp_id] at aux,
rw ← aux,
refine le_trans _ hr,
rw mul_comm,
apply normed_group_hom.norm_comp_le_of_le,
{ apply_mod_cast norm_eval_CLCFPTinv_le, exact hN },
{ haveI : fact (c' ≤ r' ^ k * c₁) := ⟨H⟩,
rw nnreal.coe_pow,
apply norm_res_le_pow },
end
end universal_map
end breen_deligne
attribute [irreducible] CLCFPTinv₂ CLCFPTinv₂.res
breen_deligne.universal_map.eval_CLCFPTinv₂
|
03da6bb5fb89e7a0627bc54ff0993a75d5ad1c45 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/analysis/convex/caratheodory.lean | e1065174b651e10ed3681f509c93e2690b13f3b9 | [] | 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 | 5,252 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Scott Morrison
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.analysis.convex.basic
import Mathlib.linear_algebra.finite_dimensional
import Mathlib.PostPort
universes u
namespace Mathlib
/-!
# Carathéodory's convexity theorem
This file is devoted to proving Carathéodory's convexity theorem:
The convex hull of a set `s` in ℝᵈ is the union of the convex hulls of the (d+1)-tuples in `s`.
## Main results:
* `convex_hull_eq_union`: Carathéodory's convexity theorem
## Implementation details
This theorem was formalized as part of the Sphere Eversion project.
## Tags
convex hull, caratheodory
-/
namespace caratheodory
/--
If `x` is in the convex hull of some finset `t` with strictly more than `findim + 1` elements,
then it is in the union of the convex hulls of the finsets `t.erase y` for `y ∈ t`.
-/
theorem mem_convex_hull_erase {E : Type u} [add_comm_group E] [vector_space ℝ E] [finite_dimensional ℝ E] [DecidableEq E] {t : finset E} (h : finite_dimensional.findim ℝ E + 1 < finset.card t) {x : E} (m : x ∈ convex_hull ↑t) : ∃ (y : ↥↑t), x ∈ convex_hull ↑(finset.erase t ↑y) := sorry
/--
The convex hull of a finset `t` with `findim ℝ E + 1 < t.card` is equal to
the union of the convex hulls of the finsets `t.erase x` for `x ∈ t`.
-/
theorem step {E : Type u} [add_comm_group E] [vector_space ℝ E] [finite_dimensional ℝ E] [DecidableEq E] (t : finset E) (h : finite_dimensional.findim ℝ E + 1 < finset.card t) : convex_hull ↑t = set.Union fun (x : ↥↑t) => convex_hull ↑(finset.erase t ↑x) := sorry
/--
The convex hull of a finset `t` with `findim ℝ E + 1 < t.card` is contained in
the union of the convex hulls of the finsets `t' ⊆ t` with `t'.card ≤ findim ℝ E + 1`.
-/
theorem shrink' {E : Type u} [add_comm_group E] [vector_space ℝ E] [finite_dimensional ℝ E] (t : finset E) (k : ℕ) (h : finset.card t = finite_dimensional.findim ℝ E + 1 + k) : convex_hull ↑t ⊆
set.Union
fun (t' : finset E) =>
set.Union
fun (w : t' ⊆ t) => set.Union fun (b : finset.card t' ≤ finite_dimensional.findim ℝ E + 1) => convex_hull ↑t' := sorry
/--
The convex hull of any finset `t` is contained in
the union of the convex hulls of the finsets `t' ⊆ t` with `t'.card ≤ findim ℝ E + 1`.
-/
theorem shrink {E : Type u} [add_comm_group E] [vector_space ℝ E] [finite_dimensional ℝ E] (t : finset E) : convex_hull ↑t ⊆
set.Union
fun (t' : finset E) =>
set.Union
fun (w : t' ⊆ t) => set.Union fun (b : finset.card t' ≤ finite_dimensional.findim ℝ E + 1) => convex_hull ↑t' := sorry
end caratheodory
/--
One inclusion of Carathéodory's convexity theorem.
The convex hull of a set `s` in ℝᵈ is contained in
the union of the convex hulls of the (d+1)-tuples in `s`.
-/
theorem convex_hull_subset_union {E : Type u} [add_comm_group E] [vector_space ℝ E] [finite_dimensional ℝ E] (s : set E) : convex_hull s ⊆
set.Union
fun (t : finset E) =>
set.Union
fun (w : ↑t ⊆ s) => set.Union fun (b : finset.card t ≤ finite_dimensional.findim ℝ E + 1) => convex_hull ↑t := sorry
/--
Carathéodory's convexity theorem.
The convex hull of a set `s` in ℝᵈ is the union of the convex hulls of the (d+1)-tuples in `s`.
-/
theorem convex_hull_eq_union {E : Type u} [add_comm_group E] [vector_space ℝ E] [finite_dimensional ℝ E] (s : set E) : convex_hull s =
set.Union
fun (t : finset E) =>
set.Union
fun (w : ↑t ⊆ s) => set.Union fun (b : finset.card t ≤ finite_dimensional.findim ℝ E + 1) => convex_hull ↑t := sorry
/--
A more explicit formulation of Carathéodory's convexity theorem,
writing an element of a convex hull as the center of mass
of an explicit `finset` with cardinality at most `dim + 1`.
-/
theorem eq_center_mass_card_le_dim_succ_of_mem_convex_hull {E : Type u} [add_comm_group E] [vector_space ℝ E] [finite_dimensional ℝ E] {s : set E} {x : E} (h : x ∈ convex_hull s) : ∃ (t : finset E),
∃ (w : ↑t ⊆ s),
∃ (b : finset.card t ≤ finite_dimensional.findim ℝ E + 1),
∃ (f : E → ℝ), (∀ (y : E), y ∈ t → 0 ≤ f y) ∧ finset.sum t f = 1 ∧ finset.center_mass t f id = x := sorry
/--
A variation on Carathéodory's convexity theorem,
writing an element of a convex hull as a center of mass
of an explicit `finset` with cardinality at most `dim + 1`,
where all coefficients in the center of mass formula
are strictly positive.
(This is proved using `eq_center_mass_card_le_dim_succ_of_mem_convex_hull`,
and discarding any elements of the set with coefficient zero.)
-/
theorem eq_pos_center_mass_card_le_dim_succ_of_mem_convex_hull {E : Type u} [add_comm_group E] [vector_space ℝ E] [finite_dimensional ℝ E] {s : set E} {x : E} (h : x ∈ convex_hull s) : ∃ (t : finset E),
∃ (w : ↑t ⊆ s),
∃ (b : finset.card t ≤ finite_dimensional.findim ℝ E + 1),
∃ (f : E → ℝ), (∀ (y : E), y ∈ t → 0 < f y) ∧ finset.sum t f = 1 ∧ finset.center_mass t f id = x := sorry
|
dea8f041dba3ae9263795e2b2ae95e5d9bed3e57 | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/tactic/omega/coeffs.lean | dd94f2f7fef5093fb1115d775262f6542db08e18 | [
"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 | 10,601 | lean | /- Copyright (c) 2019 Seul Baek. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Seul Baek
Non-constant terms of linear constraints are represented
by storing their coefficients in integer lists. -/
import data.list.defs
import tactic.ring
import tactic.omega.misc
namespace omega
namespace coeffs
open list.func
variable {v : nat → int}
/-- `val_between v as l o` is the value (under valuation `v`) of the term
obtained taking the term represented by `(0, as)` and dropping all
subterms that include variables outside the range `[l,l+o)` -/
@[simp] def val_between (v : nat → int) (as : list int) (l : nat) : nat → int
| 0 := 0
| (o+1) := (val_between o) + (get (l+o) as * v (l+o))
@[simp] lemma val_between_nil {l : nat} :
∀ m, val_between v [] l m = 0
| 0 := by simp only [val_between]
| (m+1) :=
by simp only [val_between_nil m, omega.coeffs.val_between,
get_nil, zero_add, zero_mul, int.default_eq_zero]
/-- Evaluation of the nonconstant component of a normalized linear arithmetic term. -/
def val (v : nat → int) (as : list int) : int :=
val_between v as 0 as.length
@[simp] lemma val_nil : val v [] = 0 := rfl
lemma val_between_eq_of_le {as : list int} {l : nat} :
∀ m, as.length ≤ l + m →
val_between v as l m = val_between v as l (as.length - l)
| 0 h1 :=
begin rw (nat.sub_eq_zero_iff_le.elim_right _), apply h1 end
| (m+1) h1 :=
begin
rw le_iff_eq_or_lt at h1, cases h1,
{ rw [h1, add_comm l, nat.add_sub_cancel] },
have h2 : list.length as ≤ l + m,
{ rw ← nat.lt_succ_iff, apply h1 },
simpa [ get_eq_default_of_le _ h2, zero_mul, add_zero,
val_between ] using val_between_eq_of_le _ h2
end
lemma val_eq_of_le {as : list int} {k : nat} :
as.length ≤ k → val v as = val_between v as 0 k :=
begin
intro h1, unfold val,
rw [val_between_eq_of_le k _], refl,
rw zero_add, exact h1
end
lemma val_between_eq_val_between
{v w : nat → int} {as bs : list int} {l : nat} :
∀ {m}, (∀ x, l ≤ x → x < l + m → v x = w x) →
(∀ x, l ≤ x → x < l + m → get x as = get x bs) →
val_between v as l m = val_between w bs l m
| 0 h1 h2 := rfl
| (m+1) h1 h2 :=
begin
unfold val_between,
have h3 : l + m < l + (m + 1),
{ rw ← add_assoc, apply lt_add_one },
apply fun_mono_2,
apply val_between_eq_val_between; intros x h4 h5,
{ apply h1 _ h4 (lt_trans h5 h3) },
{ apply h2 _ h4 (lt_trans h5 h3) },
rw [h1 _ _ h3, h2 _ _ h3];
apply nat.le_add_right
end
open_locale list.func
lemma val_between_set {a : int} {l n : nat} :
∀ {m}, l ≤ n → n < l + m → val_between v ([] {n ↦ a}) l m = a * v n
| 0 h1 h2 :=
begin exfalso, apply lt_irrefl l (lt_of_le_of_lt h1 h2) end
| (m+1) h1 h2 :=
begin
rw [← add_assoc, nat.lt_succ_iff, le_iff_eq_or_lt] at h2,
cases h2; unfold val_between,
{ have h3 : val_between v ([] {l + m ↦ a}) l m = 0,
{ apply @eq.trans _ _ (val_between v [] l m),
{ apply val_between_eq_val_between,
{ intros, refl },
{ intros x h4 h5, rw [get_nil,
get_set_eq_of_ne, get_nil],
apply ne_of_lt h5 } },
apply val_between_nil },
rw h2,
simp only [h3, zero_add, list.func.get_set] },
{ have h3 : l + m ≠ n,
{ apply ne_of_gt h2 },
rw [@val_between_set m h1 h2, get_set_eq_of_ne _ _ h3],
simp only [h3, get_nil, add_zero, zero_mul, int.default_eq_zero] }
end
@[simp] lemma val_set {m : nat} {a : int} :
val v ([] {m ↦ a}) = a * v m :=
begin
apply val_between_set, apply zero_le,
apply lt_of_lt_of_le (lt_add_one _),
simp only [length_set, zero_add, le_max_right],
end
lemma val_between_neg {as : list int} {l : nat} :
∀ {o}, val_between v (neg as) l o = -(val_between v as l o)
| 0 := rfl
| (o+1) :=
begin
unfold val_between,
rw [neg_add, neg_mul_eq_neg_mul],
apply fun_mono_2,
apply val_between_neg,
apply fun_mono_2 _ rfl,
apply get_neg
end
@[simp] lemma val_neg {as : list int} :
val v (neg as) = -(val v as) :=
by simpa only [val, length_neg] using val_between_neg
lemma val_between_add {is js : list int} {l : nat} :
∀ m, val_between v (add is js) l m =
(val_between v is l m) + (val_between v js l m)
| 0 := rfl
| (m+1) :=
by { simp only [val_between, val_between_add m,
list.func.get, get_add], ring }
@[simp] lemma val_add {is js : list int} :
val v (add is js) = (val v is) + (val v js) :=
begin
unfold val,
rw val_between_add, apply fun_mono_2;
apply val_between_eq_of_le;
rw [zero_add, length_add],
apply le_max_left, apply le_max_right
end
lemma val_between_sub {is js : list int} {l : nat} :
∀ m, val_between v (sub is js) l m =
(val_between v is l m) - (val_between v js l m)
| 0 := rfl
| (m+1) :=
by { simp only [val_between, val_between_sub m,
list.func.get, get_sub], ring }
@[simp] lemma val_sub {is js : list int} :
val v (sub is js) = (val v is) - (val v js) :=
begin
unfold val,
rw val_between_sub,
apply fun_mono_2;
apply val_between_eq_of_le;
rw [zero_add, length_sub],
apply le_max_left,
apply le_max_right
end
/-- `val_except k v as` is the value (under valuation `v`) of the term
obtained taking the term represented by `(0, as)` and dropping the
subterm that includes the `k`th variable. -/
def val_except (k : nat) (v : nat → int) (as) :=
val_between v as 0 k + val_between v as (k+1) (as.length - (k+1))
lemma val_except_eq_val_except
{k : nat} {is js : list int} {v w : nat → int} :
(∀ x ≠ k, v x = w x) → (∀ x ≠ k, get x is = get x js) →
val_except k v is = val_except k w js :=
begin
intros h1 h2, unfold val_except,
apply fun_mono_2,
{ apply val_between_eq_val_between;
intros x h3 h4;
[ {apply h1}, {apply h2} ];
apply ne_of_lt;
rw zero_add at h4;
apply h4 },
{ repeat { rw ← val_between_eq_of_le
((max is.length js.length) - (k+1)) },
{ apply val_between_eq_val_between;
intros x h3 h4;
[ {apply h1}, {apply h2} ];
apply ne.symm;
apply ne_of_lt;
rw nat.lt_iff_add_one_le;
exact h3 },
repeat { rw add_comm,
apply le_trans _ (nat.le_sub_add _ _),
{ apply le_max_right <|> apply le_max_left } } }
end
open_locale omega
lemma val_except_update_set
{n : nat} {as : list int} {i j : int} :
val_except n (v⟨n ↦ i⟩) (as {n ↦ j}) = val_except n v as :=
by apply val_except_eq_val_except update_eq_of_ne (get_set_eq_of_ne _)
lemma val_between_add_val_between {as : list int} {l m : nat} :
∀ {n}, val_between v as l m + val_between v as (l+m) n =
val_between v as l (m+n)
| 0 := by simp only [val_between, add_zero]
| (n+1) :=
begin
rw ← add_assoc,
unfold val_between,
rw add_assoc,
rw ← @val_between_add_val_between n,
ring,
end
lemma val_except_add_eq (n : nat) {as : list int} :
(val_except n v as) + ((get n as) * (v n)) = val v as :=
begin
unfold val_except, unfold val,
by_cases h1 : n + 1 ≤ as.length,
{ have h4 := @val_between_add_val_between v as 0 (n+1) (as.length - (n+1)),
have h5 : n + 1 + (as.length - (n + 1)) = as.length,
{ rw [add_comm, nat.sub_add_cancel h1] },
rw h5 at h4, apply eq.trans _ h4,
simp only [val_between, zero_add], ring },
rw not_lt at h1,
have h2 : (list.length as - (n + 1)) = 0,
{ apply nat.sub_eq_zero_of_le
(le_trans h1 (nat.le_add_right _ _)) },
have h3 : val_between v as 0 (list.length as) =
val_between v as 0 (n + 1),
{ simpa only [val] using @val_eq_of_le v as (n+1)
(le_trans h1 (nat.le_add_right _ _)) },
simp only [add_zero, val_between, zero_add, h2, h3]
end
@[simp] lemma val_between_map_mul {i : int} {as: list int} {l : nat} :
∀ {m}, val_between v (list.map ((*) i) as) l m = i * val_between v as l m
| 0 := by simp only [val_between, mul_zero, list.map]
| (m+1) :=
begin
unfold val_between,
rw [@val_between_map_mul m, mul_add],
apply fun_mono_2 rfl,
by_cases h1 : l + m < as.length,
{ rw [get_map h1, mul_assoc] },
rw not_lt at h1,
rw [get_eq_default_of_le, get_eq_default_of_le];
try {simp}; apply h1
end
lemma forall_val_dvd_of_forall_mem_dvd {i : int} {as : list int} :
(∀ x ∈ as, i ∣ x) → (∀ n, i ∣ get n as) | h1 n :=
by { apply forall_val_of_forall_mem _ h1,
apply dvd_zero }
lemma dvd_val_between {i} {as: list int} {l : nat} :
∀ {m}, (∀ x ∈ as, i ∣ x) → (i ∣ val_between v as l m)
| 0 h1 := dvd_zero _
| (m+1) h1 :=
begin
unfold val_between,
apply dvd_add,
apply dvd_val_between h1,
apply dvd_mul_of_dvd_left,
by_cases h2 : get (l+m) as = 0,
{ rw h2, apply dvd_zero },
apply h1, apply mem_get_of_ne_zero h2
end
lemma dvd_val {as : list int} {i : int} :
(∀ x ∈ as, i ∣ x) → (i ∣ val v as) := by apply dvd_val_between
@[simp] lemma val_between_map_div
{as: list int} {i : int} {l : nat} (h1 : ∀ x ∈ as, i ∣ x) :
∀ {m}, val_between v (list.map (λ x, x / i) as) l m = (val_between v as l m) / i
| 0 := by simp only [int.zero_div, val_between, list.map]
| (m+1) :=
begin
unfold val_between,
rw [@val_between_map_div m, int.add_div_of_dvd (dvd_val_between h1)],
apply fun_mono_2 rfl,
{ apply calc get (l + m) (list.map (λ (x : ℤ), x / i) as) * v (l + m)
= ((get (l + m) as) / i) * v (l + m) :
begin
apply fun_mono_2 _ rfl,
rw get_map',
apply int.zero_div
end
... = get (l + m) as * v (l + m) / i :
begin
repeat {rw mul_comm _ (v (l+m))},
rw int.mul_div_assoc,
apply forall_val_dvd_of_forall_mem_dvd h1
end },
apply dvd_mul_of_dvd_left,
apply forall_val_dvd_of_forall_mem_dvd h1,
end
@[simp] lemma val_map_div {as : list int} {i : int} :
(∀ x ∈ as, i ∣ x) → val v (list.map (λ x, x / i) as) = (val v as) / i :=
by {intro h1, simpa only [val, list.length_map] using val_between_map_div h1}
lemma val_between_eq_zero {is: list int} {l : nat} :
∀ {m}, (∀ x : int, x ∈ is → x = 0) → val_between v is l m = 0
| 0 h1 := rfl
| (m+1) h1 :=
begin
have h2 := @forall_val_of_forall_mem _ _ is (λ x, x = 0) rfl h1,
simpa only [val_between, h2 (l+m), zero_mul, add_zero]
using @val_between_eq_zero m h1,
end
lemma val_eq_zero {is : list int} :
(∀ x : int, x ∈ is → x = 0) → val v is = 0 :=
by apply val_between_eq_zero
end coeffs
end omega
|
86291614b815f8d4fa188a0e5ad6a5e1e05e5bcd | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/analysis/special_functions/arsinh.lean | 9611925d171fdb6546a5b1e4ed21119e6c09a061 | [
"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,843 | lean | /-
Copyright (c) 2020 James Arthur. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: James Arthur, Chris Hughes, Shing Tak Lam
-/
import analysis.special_functions.trigonometric.deriv
import analysis.special_functions.log.basic
/-!
# Inverse of the sinh function
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we prove that sinh is bijective and hence has an
inverse, arsinh.
## Main definitions
- `real.arsinh`: The inverse function of `real.sinh`.
- `real.sinh_equiv`, `real.sinh_order_iso`, `real.sinh_homeomorph`: `real.sinh` as an `equiv`,
`order_iso`, and `homeomorph`, respectively.
## Main Results
- `real.sinh_surjective`, `real.sinh_bijective`: `real.sinh` is surjective and bijective;
- `real.arsinh_injective`, `real.arsinh_surjective`, `real.arsinh_bijective`: `real.arsinh` is
injective, surjective, and bijective;
- `real.continuous_arsinh`, `real.differentiable_arsinh`, `real.cont_diff_arsinh`: `real.arsinh` is
continuous, differentiable, and continuously differentiable; we also provide dot notation
convenience lemmas like `filter.tendsto.arsinh` and `cont_diff_at.arsinh`.
## Tags
arsinh, arcsinh, argsinh, asinh, sinh injective, sinh bijective, sinh surjective
-/
noncomputable theory
open function filter set
open_locale topology
namespace real
variables {x y : ℝ}
/-- `arsinh` is defined using a logarithm, `arsinh x = log (x + sqrt(1 + x^2))`. -/
@[pp_nodot] def arsinh (x : ℝ) := log (x + sqrt (1 + x^2))
lemma exp_arsinh (x : ℝ) : exp (arsinh x) = x + sqrt (1 + x^2) :=
begin
apply exp_log,
rw [← neg_lt_iff_pos_add'],
calc -x ≤ sqrt (x ^ 2) : le_sqrt_of_sq_le (neg_pow_bit0 _ _).le
... < sqrt (1 + x ^ 2) : sqrt_lt_sqrt (sq_nonneg _) (lt_one_add _)
end
@[simp] lemma arsinh_zero : arsinh 0 = 0 := by simp [arsinh]
@[simp] lemma arsinh_neg (x : ℝ) : arsinh (-x) = -arsinh x :=
begin
rw [← exp_eq_exp, exp_arsinh, exp_neg, exp_arsinh],
apply eq_inv_of_mul_eq_one_left,
rw [neg_sq, neg_add_eq_sub, add_comm x, mul_comm, ← sq_sub_sq, sq_sqrt, add_sub_cancel],
exact add_nonneg zero_le_one (sq_nonneg _)
end
/-- `arsinh` is the right inverse of `sinh`. -/
@[simp] lemma sinh_arsinh (x : ℝ) : sinh (arsinh x) = x :=
by { rw [sinh_eq, ← arsinh_neg, exp_arsinh, exp_arsinh, neg_sq], field_simp }
@[simp] lemma cosh_arsinh (x : ℝ) : cosh (arsinh x) = sqrt (1 + x^2) :=
by rw [← sqrt_sq (cosh_pos _).le, cosh_sq', sinh_arsinh]
/-- `sinh` is surjective, `∀ b, ∃ a, sinh a = b`. In this case, we use `a = arsinh b`. -/
lemma sinh_surjective : surjective sinh := left_inverse.surjective sinh_arsinh
/-- `sinh` is bijective, both injective and surjective. -/
lemma sinh_bijective : bijective sinh := ⟨sinh_injective, sinh_surjective⟩
/-- `arsinh` is the left inverse of `sinh`. -/
@[simp] lemma arsinh_sinh (x : ℝ) : arsinh (sinh x) = x :=
right_inverse_of_injective_of_left_inverse sinh_injective sinh_arsinh x
/-- `real.sinh` as an `equiv`. -/
@[simps] def sinh_equiv : ℝ ≃ ℝ :=
{ to_fun := sinh,
inv_fun := arsinh,
left_inv := arsinh_sinh,
right_inv := sinh_arsinh }
/-- `real.sinh` as an `order_iso`. -/
@[simps { fully_applied := ff }] def sinh_order_iso : ℝ ≃o ℝ :=
{ to_equiv := sinh_equiv,
map_rel_iff' := @sinh_le_sinh }
/-- `real.sinh` as a `homeomorph`. -/
@[simps { fully_applied := ff }] def sinh_homeomorph : ℝ ≃ₜ ℝ := sinh_order_iso.to_homeomorph
lemma arsinh_bijective : bijective arsinh := sinh_equiv.symm.bijective
lemma arsinh_injective : injective arsinh := sinh_equiv.symm.injective
lemma arsinh_surjective : surjective arsinh := sinh_equiv.symm.surjective
lemma arsinh_strict_mono : strict_mono arsinh := sinh_order_iso.symm.strict_mono
@[simp] lemma arsinh_inj : arsinh x = arsinh y ↔ x = y := arsinh_injective.eq_iff
@[simp] lemma arsinh_le_arsinh : arsinh x ≤ arsinh y ↔ x ≤ y := sinh_order_iso.symm.le_iff_le
@[simp] lemma arsinh_lt_arsinh : arsinh x < arsinh y ↔ x < y := sinh_order_iso.symm.lt_iff_lt
@[simp] lemma arsinh_eq_zero_iff : arsinh x = 0 ↔ x = 0 :=
arsinh_injective.eq_iff' arsinh_zero
@[simp] lemma arsinh_nonneg_iff : 0 ≤ arsinh x ↔ 0 ≤ x :=
by rw [← sinh_le_sinh, sinh_zero, sinh_arsinh]
@[simp] lemma arsinh_nonpos_iff : arsinh x ≤ 0 ↔ x ≤ 0 :=
by rw [← sinh_le_sinh, sinh_zero, sinh_arsinh]
@[simp] lemma arsinh_pos_iff : 0 < arsinh x ↔ 0 < x :=
lt_iff_lt_of_le_iff_le arsinh_nonpos_iff
@[simp] lemma arsinh_neg_iff : arsinh x < 0 ↔ x < 0 :=
lt_iff_lt_of_le_iff_le arsinh_nonneg_iff
lemma has_strict_deriv_at_arsinh (x : ℝ) : has_strict_deriv_at arsinh (sqrt (1 + x ^ 2))⁻¹ x :=
begin
convert sinh_homeomorph.to_local_homeomorph.has_strict_deriv_at_symm (mem_univ x)
(cosh_pos _).ne' (has_strict_deriv_at_sinh _),
exact (cosh_arsinh _).symm
end
lemma has_deriv_at_arsinh (x : ℝ) : has_deriv_at arsinh (sqrt (1 + x ^ 2))⁻¹ x :=
(has_strict_deriv_at_arsinh x).has_deriv_at
lemma differentiable_arsinh : differentiable ℝ arsinh :=
λ x, (has_deriv_at_arsinh x).differentiable_at
lemma cont_diff_arsinh {n : ℕ∞} : cont_diff ℝ n arsinh :=
sinh_homeomorph.cont_diff_symm_deriv (λ x, (cosh_pos x).ne') has_deriv_at_sinh cont_diff_sinh
@[continuity] lemma continuous_arsinh : continuous arsinh := sinh_homeomorph.symm.continuous
end real
open real
lemma filter.tendsto.arsinh {α : Type*} {l : filter α} {f : α → ℝ} {a : ℝ}
(h : tendsto f l (𝓝 a)) : tendsto (λ x, arsinh (f x)) l (𝓝 (arsinh a)) :=
(continuous_arsinh.tendsto _).comp h
section continuous
variables {X : Type*} [topological_space X] {f : X → ℝ} {s : set X} {a : X}
lemma continuous_at.arsinh (h : continuous_at f a) : continuous_at (λ x, arsinh (f x)) a := h.arsinh
lemma continuous_within_at.arsinh (h : continuous_within_at f s a) :
continuous_within_at (λ x, arsinh (f x)) s a :=
h.arsinh
lemma continuous_on.arsinh (h : continuous_on f s) : continuous_on (λ x, arsinh (f x)) s :=
λ x hx, (h x hx).arsinh
lemma continuous.arsinh (h : continuous f) : continuous (λ x, arsinh (f x)) :=
continuous_arsinh.comp h
end continuous
section fderiv
variables {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] {f : E → ℝ} {s : set E} {a : E}
{f' : E →L[ℝ] ℝ} {n : ℕ∞}
lemma has_strict_fderiv_at.arsinh (hf : has_strict_fderiv_at f f' a) :
has_strict_fderiv_at (λ x, arsinh (f x)) ((sqrt (1 + (f a) ^ 2))⁻¹ • f') a :=
(has_strict_deriv_at_arsinh _).comp_has_strict_fderiv_at a hf
lemma has_fderiv_at.arsinh (hf : has_fderiv_at f f' a) :
has_fderiv_at (λ x, arsinh (f x)) ((sqrt (1 + (f a) ^ 2))⁻¹ • f') a :=
(has_deriv_at_arsinh _).comp_has_fderiv_at a hf
lemma has_fderiv_within_at.arsinh (hf : has_fderiv_within_at f f' s a) :
has_fderiv_within_at (λ x, arsinh (f x)) ((sqrt (1 + (f a) ^ 2))⁻¹ • f') s a :=
(has_deriv_at_arsinh _).comp_has_fderiv_within_at a hf
lemma differentiable_at.arsinh (h : differentiable_at ℝ f a) :
differentiable_at ℝ (λ x, arsinh (f x)) a :=
(differentiable_arsinh _).comp a h
lemma differentiable_within_at.arsinh (h : differentiable_within_at ℝ f s a) :
differentiable_within_at ℝ (λ x, arsinh (f x)) s a :=
(differentiable_arsinh _).comp_differentiable_within_at a h
lemma differentiable_on.arsinh (h : differentiable_on ℝ f s) :
differentiable_on ℝ (λ x, arsinh (f x)) s :=
λ x hx, (h x hx).arsinh
lemma differentiable.arsinh (h : differentiable ℝ f) :
differentiable ℝ (λ x, arsinh (f x)) :=
differentiable_arsinh.comp h
lemma cont_diff_at.arsinh (h : cont_diff_at ℝ n f a) :
cont_diff_at ℝ n (λ x, arsinh (f x)) a :=
cont_diff_arsinh.cont_diff_at.comp a h
lemma cont_diff_within_at.arsinh (h : cont_diff_within_at ℝ n f s a) :
cont_diff_within_at ℝ n (λ x, arsinh (f x)) s a :=
cont_diff_arsinh.cont_diff_at.comp_cont_diff_within_at a h
lemma cont_diff.arsinh (h : cont_diff ℝ n f) : cont_diff ℝ n (λ x, arsinh (f x)) :=
cont_diff_arsinh.comp h
lemma cont_diff_on.arsinh (h : cont_diff_on ℝ n f s) : cont_diff_on ℝ n (λ x, arsinh (f x)) s :=
λ x hx, (h x hx).arsinh
end fderiv
section deriv
variables {f : ℝ → ℝ} {s : set ℝ} {a f' : ℝ}
lemma has_strict_deriv_at.arsinh (hf : has_strict_deriv_at f f' a) :
has_strict_deriv_at (λ x, arsinh (f x)) ((sqrt (1 + (f a) ^ 2))⁻¹ • f') a :=
(has_strict_deriv_at_arsinh _).comp a hf
lemma has_deriv_at.arsinh (hf : has_deriv_at f f' a) :
has_deriv_at (λ x, arsinh (f x)) ((sqrt (1 + (f a) ^ 2))⁻¹ • f') a :=
(has_deriv_at_arsinh _).comp a hf
lemma has_deriv_within_at.arsinh (hf : has_deriv_within_at f f' s a) :
has_deriv_within_at (λ x, arsinh (f x)) ((sqrt (1 + (f a) ^ 2))⁻¹ • f') s a :=
(has_deriv_at_arsinh _).comp_has_deriv_within_at a hf
end deriv
|
be525ca09f63b3c062d9f2952f6a69b55392e25d | d1a52c3f208fa42c41df8278c3d280f075eb020c | /stage0/src/Lean/Compiler/IR/Checker.lean | 6652e7d1fb05c3deb088b796d688a64a18cb8674 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | cipher1024/lean4 | 6e1f98bb58e7a92b28f5364eb38a14c8d0aae393 | 69114d3b50806264ef35b57394391c3e738a9822 | refs/heads/master | 1,642,227,983,603 | 1,642,011,696,000 | 1,642,011,696,000 | 228,607,691 | 0 | 0 | Apache-2.0 | 1,576,584,269,000 | 1,576,584,268,000 | null | UTF-8 | Lean | false | false | 7,085 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Compiler.IR.CompilerM
import Lean.Compiler.IR.Format
namespace Lean.IR.Checker
@[extern c inline "lean_box(LEAN_MAX_CTOR_FIELDS)"]
constant getMaxCtorFields : Unit → Nat
def maxCtorFields := getMaxCtorFields ()
@[extern c inline "lean_box(LEAN_MAX_CTOR_SCALARS_SIZE)"]
constant getMaxCtorScalarsSize : Unit → Nat
def maxCtorScalarsSize := getMaxCtorScalarsSize ()
@[extern c inline "lean_box(sizeof(size_t))"]
constant getUSizeSize : Unit → Nat
def usizeSize := getUSizeSize ()
structure CheckerContext where
env : Environment
localCtx : LocalContext := {}
decls : Array Decl
structure CheckerState where
foundVars : IndexSet := {}
abbrev M := ReaderT CheckerContext (ExceptT String (StateT CheckerState Id))
def markIndex (i : Index) : M Unit := do
let s ← get
if s.foundVars.contains i then
throw s!"variable / joinpoint index {i} has already been used"
modify fun s => { s with foundVars := s.foundVars.insert i }
def markVar (x : VarId) : M Unit :=
markIndex x.idx
def markJP (j : JoinPointId) : M Unit :=
markIndex j.idx
def getDecl (c : Name) : M Decl := do
let ctx ← read
match findEnvDecl' ctx.env c ctx.decls with
| none => throw s!"unknown declaration '{c}'"
| some d => pure d
def checkVar (x : VarId) : M Unit := do
let ctx ← read
unless ctx.localCtx.isLocalVar x.idx || ctx.localCtx.isParam x.idx do
throw s!"unknown variable '{x}'"
def checkJP (j : JoinPointId) : M Unit := do
let ctx ← read
unless ctx.localCtx.isJP j.idx do
throw s!"unknown join point '{j}'"
def checkArg (a : Arg) : M Unit :=
match a with
| Arg.var x => checkVar x
| other => pure ()
def checkArgs (as : Array Arg) : M Unit :=
as.forM checkArg
@[inline] def checkEqTypes (ty₁ ty₂ : IRType) : M Unit := do
unless ty₁ == ty₂ do
throw "unexpected type"
@[inline] def checkType (ty : IRType) (p : IRType → Bool) : M Unit := do
unless p ty do
throw s!"unexpected type '{ty}'"
def checkObjType (ty : IRType) : M Unit := checkType ty IRType.isObj
def checkScalarType (ty : IRType) : M Unit := checkType ty IRType.isScalar
def getType (x : VarId) : M IRType := do
let ctx ← read
match ctx.localCtx.getType x with
| some ty => pure ty
| none => throw s!"unknown variable '{x}'"
@[inline] def checkVarType (x : VarId) (p : IRType → Bool) : M Unit := do
let ty ← getType x; checkType ty p
def checkObjVar (x : VarId) : M Unit :=
checkVarType x IRType.isObj
def checkScalarVar (x : VarId) : M Unit :=
checkVarType x IRType.isScalar
def checkFullApp (c : FunId) (ys : Array Arg) : M Unit := do
let decl ← getDecl c
unless ys.size == decl.params.size do
throw s!"incorrect number of arguments to '{c}', {ys.size} provided, {decl.params.size} expected"
checkArgs ys
def checkPartialApp (c : FunId) (ys : Array Arg) : M Unit := do
let decl ← getDecl c
unless ys.size < decl.params.size do
throw s!"too many arguments too partial application '{c}', num. args: {ys.size}, arity: {decl.params.size}"
checkArgs ys
def checkExpr (ty : IRType) : Expr → M Unit
| Expr.pap f ys => checkPartialApp f ys *> checkObjType ty -- partial applications should always produce a closure object
| Expr.ap x ys => checkObjVar x *> checkArgs ys
| Expr.fap f ys => checkFullApp f ys
| Expr.ctor c ys => do
if c.size > maxCtorFields then
throw s!"constructor '{c.name}' has too many fields"
if c.ssize + c.usize * usizeSize > maxCtorScalarsSize then
throw s!"constructor '{c.name}' has too many scalar fields"
if !ty.isStruct && !ty.isUnion && c.isRef then
(checkObjType ty) *> checkArgs ys
| Expr.reset _ x => checkObjVar x *> checkObjType ty
| Expr.reuse x i u ys => checkObjVar x *> checkArgs ys *> checkObjType ty
| Expr.box xty x => checkObjType ty *> checkScalarVar x *> checkVarType x (fun t => t == xty)
| Expr.unbox x => checkScalarType ty *> checkObjVar x
| Expr.proj i x => do
let xType ← getType x;
match xType with
| IRType.object => checkObjType ty
| IRType.tobject => checkObjType ty
| IRType.struct _ tys => if h : i < tys.size then checkEqTypes (tys.get ⟨i,h⟩) ty else throw "invalid proj index"
| IRType.union _ tys => if h : i < tys.size then checkEqTypes (tys.get ⟨i,h⟩) ty else throw "invalid proj index"
| other => throw s!"unexpected IR type '{xType}'"
| Expr.uproj _ x => checkObjVar x *> checkType ty (fun t => t == IRType.usize)
| Expr.sproj _ _ x => checkObjVar x *> checkScalarType ty
| Expr.isShared x => checkObjVar x *> checkType ty (fun t => t == IRType.uint8)
| Expr.isTaggedPtr x => checkObjVar x *> checkType ty (fun t => t == IRType.uint8)
| Expr.lit (LitVal.str _) => checkObjType ty
| Expr.lit _ => pure ()
@[inline] def withParams (ps : Array Param) (k : M Unit) : M Unit := do
let ctx ← read
let localCtx ← ps.foldlM (init := ctx.localCtx) fun (ctx : LocalContext) p => do
markVar p.x
pure $ ctx.addParam p
withReader (fun _ => { ctx with localCtx := localCtx }) k
partial def checkFnBody : FnBody → M Unit
| FnBody.vdecl x t v b => do
checkExpr t v;
markVar x;
let ctx ← read
withReader (fun ctx => { ctx with localCtx := ctx.localCtx.addLocal x t v }) (checkFnBody b)
| FnBody.jdecl j ys v b => do
markJP j;
withParams ys (checkFnBody v);
let ctx ← read
withReader (fun ctx => { ctx with localCtx := ctx.localCtx.addJP j ys v }) (checkFnBody b)
| FnBody.set x _ y b => checkVar x *> checkArg y *> checkFnBody b
| FnBody.uset x _ y b => checkVar x *> checkVar y *> checkFnBody b
| FnBody.sset x _ _ y _ b => checkVar x *> checkVar y *> checkFnBody b
| FnBody.setTag x _ b => checkVar x *> checkFnBody b
| FnBody.inc x _ _ _ b => checkVar x *> checkFnBody b
| FnBody.dec x _ _ _ b => checkVar x *> checkFnBody b
| FnBody.del x b => checkVar x *> checkFnBody b
| FnBody.mdata _ b => checkFnBody b
| FnBody.jmp j ys => checkJP j *> checkArgs ys
| FnBody.ret x => checkArg x
| FnBody.case _ x _ alts => checkVar x *> alts.forM (fun alt => checkFnBody alt.body)
| FnBody.unreachable => pure ()
def checkDecl : Decl → M Unit
| Decl.fdecl (xs := xs) (body := b) .. => withParams xs (checkFnBody b)
| Decl.extern (xs := xs) .. => withParams xs (pure ())
end Checker
def checkDecl (decls : Array Decl) (decl : Decl) : CompilerM Unit := do
let env ← getEnv
match (Checker.checkDecl decl { env := env, decls := decls }).run' {} with
| Except.error msg => throw s!"IR check failed at '{decl.name}', error: {msg}"
| other => pure ()
def checkDecls (decls : Array Decl) : CompilerM Unit :=
decls.forM (checkDecl decls)
end IR
end Lean
|
05564248e14ac1179186a14dd2f05de8f93b919b | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /src/Lean/Util/ReplaceExpr.lean | bf503aa2cbe91e541c1cdf389c8d88fbfc12cce7 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | EdAyers/lean4 | 57ac632d6b0789cb91fab2170e8c9e40441221bd | 37ba0df5841bde51dbc2329da81ac23d4f6a4de4 | refs/heads/master | 1,676,463,245,298 | 1,660,619,433,000 | 1,660,619,433,000 | 183,433,437 | 1 | 0 | Apache-2.0 | 1,657,612,672,000 | 1,556,196,574,000 | Lean | UTF-8 | Lean | false | false | 2,972 | 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 Expr
namespace ReplaceImpl
abbrev cacheSize : USize := 8192
structure State where
keys : Array Expr -- Remark: our "unsafe" implementation relies on the fact that `()` is not a valid Expr
results : Array Expr
abbrev ReplaceM := StateM State
unsafe def cache (i : USize) (key : Expr) (result : Expr) : ReplaceM Expr := do
modify fun ⟨keys, results⟩ => { keys := keys.uset i key lcProof, results := results.uset i result lcProof };
pure result
unsafe def replaceUnsafeM (f? : Expr → Option Expr) (size : USize) (e : Expr) : ReplaceM Expr := do
let rec visit (e : Expr) := do
let c ← get
let h := ptrAddrUnsafe e
let i := h % size
if ptrAddrUnsafe (c.keys.uget i lcProof) == h then
pure <| c.results.uget i lcProof
else match f? e with
| some eNew => cache i e eNew
| none => match e with
| Expr.forallE _ d b _ => cache i e <| e.updateForallE! (← visit d) (← visit b)
| Expr.lam _ d b _ => cache i e <| e.updateLambdaE! (← visit d) (← visit b)
| Expr.mdata _ b => cache i e <| e.updateMData! (← visit b)
| Expr.letE _ t v b _ => cache i e <| e.updateLet! (← visit t) (← visit v) (← visit b)
| Expr.app f a => cache i e <| e.updateApp! (← visit f) (← visit a)
| Expr.proj _ _ b => cache i e <| e.updateProj! (← visit b)
| e => pure e
visit e
unsafe def initCache : State :=
{ keys := mkArray cacheSize.toNat (cast lcProof ()), -- `()` is not a valid `Expr`
results := mkArray cacheSize.toNat default }
unsafe def replaceUnsafe (f? : Expr → Option Expr) (e : Expr) : Expr :=
(replaceUnsafeM f? cacheSize e).run' initCache
end ReplaceImpl
/- TODO: use withPtrAddr, withPtrEq to avoid unsafe tricks above.
We also need an invariant at `State` and proofs for the `uget` operations. -/
@[implementedBy ReplaceImpl.replaceUnsafe]
partial def replace (f? : Expr → Option Expr) (e : Expr) : Expr :=
/- This is a reference implementation for the unsafe one above -/
match f? e with
| some eNew => eNew
| none => match e with
| Expr.forallE _ d b _ => let d := replace f? d; let b := replace f? b; e.updateForallE! d b
| Expr.lam _ d b _ => let d := replace f? d; let b := replace f? b; e.updateLambdaE! d b
| Expr.mdata _ b => let b := replace f? b; e.updateMData! b
| Expr.letE _ t v b _ => let t := replace f? t; let v := replace f? v; let b := replace f? b; e.updateLet! t v b
| Expr.app f a => let f := replace f? f; let a := replace f? a; e.updateApp! f a
| Expr.proj _ _ b => let b := replace f? b; e.updateProj! b
| e => e
end Expr
end Lean
|
866b40e2d0cdc070289435ec9b08af0a0b557e5e | 4727251e0cd73359b15b664c3170e5d754078599 | /src/control/bitraversable/instances.lean | 851bc71c60b8a152a76f7e1ed286432412e3c9e9 | [
"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,813 | lean | /-
Copyright (c) 2019 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon
-/
import control.bitraversable.lemmas
import control.traversable.lemmas
/-!
# Bitraversable instances
This file provides `bitraversable` instances for concrete bifunctors:
* `prod`
* `sum`
* `functor.const`
* `flip`
* `function.bicompl`
* `function.bicompr`
## References
* Hackage: <https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Bitraversable.html>
## Tags
traversable bitraversable functor bifunctor applicative
-/
universes u v w
variables {t : Type u → Type u → Type u} [bitraversable t]
section
variables {F : Type u → Type u} [applicative F]
/-- The bitraverse function for `α × β`. -/
def prod.bitraverse {α α' β β'} (f : α → F α') (f' : β → F β') : α × β → F (α' × β')
| (x, y) := prod.mk <$> f x <*> f' y
instance : bitraversable prod :=
{ bitraverse := @prod.bitraverse }
instance : is_lawful_bitraversable prod :=
by constructor; introsI; cases x;
simp [bitraverse,prod.bitraverse] with functor_norm; refl
open functor
/-- The bitraverse function for `α ⊕ β`. -/
def sum.bitraverse {α α' β β'} (f : α → F α') (f' : β → F β') : α ⊕ β → F (α' ⊕ β')
| (sum.inl x) := sum.inl <$> f x
| (sum.inr x) := sum.inr <$> f' x
instance : bitraversable sum :=
{ bitraverse := @sum.bitraverse }
instance : is_lawful_bitraversable sum :=
by constructor; introsI; cases x;
simp [bitraverse, sum.bitraverse] with functor_norm; refl
/-- The bitraverse function for `const`. It throws away the second map. -/
@[nolint unused_arguments] def const.bitraverse {α α' β β'} (f : α → F α') (f' : β → F β') :
const α β → F (const α' β') := f
instance bitraversable.const : bitraversable const :=
{ bitraverse := @const.bitraverse }
instance is_lawful_bitraversable.const : is_lawful_bitraversable const :=
by constructor; introsI;
simp [bitraverse,const.bitraverse] with functor_norm; refl
/-- The bitraverse function for `flip`. -/
def flip.bitraverse {α α' β β'} (f : α → F α') (f' : β → F β') : flip t α β → F (flip t α' β') :=
(bitraverse f' f : t β α → F (t β' α'))
instance bitraversable.flip : bitraversable (flip t) :=
{ bitraverse := @flip.bitraverse t _ }
open is_lawful_bitraversable
instance is_lawful_bitraversable.flip [is_lawful_bitraversable t] :
is_lawful_bitraversable (flip t) :=
by constructor; intros; unfreezingI { casesm is_lawful_bitraversable t }; tactic.apply_assumption
open bitraversable functor
@[priority 10]
instance bitraversable.traversable {α} : traversable (t α) :=
{ traverse := @tsnd t _ _ }
@[priority 10]
instance bitraversable.is_lawful_traversable [is_lawful_bitraversable t] {α} :
is_lawful_traversable (t α) :=
by { constructor; introsI; simp [traverse,comp_tsnd] with functor_norm,
{ refl },
{ simp [tsnd_eq_snd_id], refl },
{ simp [tsnd,binaturality,function.comp] with functor_norm } }
end
open bifunctor traversable is_lawful_traversable is_lawful_bitraversable
open function (bicompl bicompr)
section bicompl
variables (F G : Type u → Type u) [traversable F] [traversable G]
/-- The bitraverse function for `bicompl`. -/
def bicompl.bitraverse {m} [applicative m] {α β α' β'} (f : α → m β) (f' : α' → m β') :
bicompl t F G α α' → m (bicompl t F G β β') :=
(bitraverse (traverse f) (traverse f') : t (F α) (G α') → m _)
instance : bitraversable (bicompl t F G) :=
{ bitraverse := @bicompl.bitraverse t _ F G _ _ }
instance [is_lawful_traversable F] [is_lawful_traversable G] [is_lawful_bitraversable t] :
is_lawful_bitraversable (bicompl t F G) :=
begin
constructor; introsI;
simp [bitraverse, bicompl.bitraverse, bimap, traverse_id, bitraverse_id_id, comp_bitraverse]
with functor_norm,
{ simp [traverse_eq_map_id',bitraverse_eq_bimap_id], },
{ revert x, dunfold bicompl,
simp [binaturality,naturality_pf] }
end
end bicompl
section bicompr
variables (F : Type u → Type u) [traversable F]
/-- The bitraverse function for `bicompr`. -/
def bicompr.bitraverse {m} [applicative m] {α β α' β'} (f : α → m β) (f' : α' → m β') :
bicompr F t α α' → m (bicompr F t β β') :=
(traverse (bitraverse f f') : F (t α α') → m _)
instance : bitraversable (bicompr F t) :=
{ bitraverse := @bicompr.bitraverse t _ F _ }
instance [is_lawful_traversable F] [is_lawful_bitraversable t] :
is_lawful_bitraversable (bicompr F t) :=
begin
constructor; introsI;
simp [bitraverse,bicompr.bitraverse,bitraverse_id_id] with functor_norm,
{ simp [bitraverse_eq_bimap_id',traverse_eq_map_id'], refl },
{ revert x, dunfold bicompr, intro,
simp [naturality,binaturality'] }
end
end bicompr
|
3cdfc5e3bc6b5810105baa0ae08d7e1fb797ee2a | 90edd5cdcf93124fe15627f7304069fdce3442dd | /stage0/src/Init/Data/String/Basic.lean | 303823c3c905b0bd1e1d43926729ef8242b1b650 | [
"Apache-2.0"
] | permissive | JLimperg/lean4-aesop | 8a9d9cd3ee484a8e67fda2dd9822d76708098712 | 5c4b9a3e05c32f69a4357c3047c274f4b94f9c71 | refs/heads/master | 1,689,415,944,104 | 1,627,383,284,000 | 1,627,383,284,000 | 377,536,770 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 17,147 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
-/
prelude
import Init.Data.List.Basic
import Init.Data.Char.Basic
import Init.Data.Option.Basic
universe u
def List.asString (s : List Char) : String :=
⟨s⟩
namespace String
instance : LT String :=
⟨fun s₁ s₂ => s₁.data < s₂.data⟩
@[extern "lean_string_dec_lt"]
instance decLt (s₁ s₂ : @& String) : Decidable (s₁ < s₂) :=
List.hasDecidableLt s₁.data s₂.data
@[extern "lean_string_length"]
def length : (@& String) → Nat
| ⟨s⟩ => s.length
/-- The internal implementation uses dynamic arrays and will perform destructive updates
if the String is not shared. -/
@[extern "lean_string_push"]
def push : String → Char → String
| ⟨s⟩, c => ⟨s ++ [c]⟩
/-- The internal implementation uses dynamic arrays and will perform destructive updates
if the String is not shared. -/
@[extern "lean_string_append"]
def append : String → (@& String) → String
| ⟨a⟩, ⟨b⟩ => ⟨a ++ b⟩
/-- O(n) in the runtime, where n is the length of the String -/
def toList (s : String) : List Char :=
s.data
private def utf8GetAux : List Char → Pos → Pos → Char
| [], i, p => arbitrary
| c::cs, i, p => if i = p then c else utf8GetAux cs (i + csize c) p
@[extern "lean_string_utf8_get"]
def get : (@& String) → (@& Pos) → Char
| ⟨s⟩, p => utf8GetAux s 0 p
def getOp (self : String) (idx : Pos) : Char :=
self.get idx
private def utf8SetAux (c' : Char) : List Char → Pos → Pos → List Char
| [], i, p => []
| c::cs, i, p =>
if i = p then (c'::cs) else c::(utf8SetAux c' cs (i + csize c) p)
@[extern "lean_string_utf8_set"]
def set : String → (@& Pos) → Char → String
| ⟨s⟩, i, c => ⟨utf8SetAux c s 0 i⟩
def modify (s : String) (i : Pos) (f : Char → Char) : String :=
s.set i <| f <| s.get i
@[extern "lean_string_utf8_next"]
def next (s : @& String) (p : @& Pos) : Pos :=
let c := get s p
p + csize c
private def utf8PrevAux : List Char → Pos → Pos → Pos
| [], i, p => 0
| c::cs, i, p =>
let cz := csize c
let i' := i + cz
if i' = p then i else utf8PrevAux cs i' p
@[extern "lean_string_utf8_prev"]
def prev : (@& String) → (@& Pos) → Pos
| ⟨s⟩, p => if p = 0 then 0 else utf8PrevAux s 0 p
def front (s : String) : Char :=
get s 0
def back (s : String) : Char :=
get s (prev s (bsize s))
@[extern "lean_string_utf8_at_end"]
def atEnd : (@& String) → (@& Pos) → Bool
| s, p => p ≥ utf8ByteSize s
/- TODO: remove `partial` keywords after we restore the tactic
framework and wellfounded recursion support -/
partial def posOfAux (s : String) (c : Char) (stopPos : Pos) (pos : Pos) : Pos :=
if pos == stopPos then pos
else if s.get pos == c then pos
else posOfAux s c stopPos (s.next pos)
@[inline] def posOf (s : String) (c : Char) : Pos :=
posOfAux s c s.bsize 0
partial def revPosOfAux (s : String) (c : Char) (pos : Pos) : Option Pos :=
if s.get pos == c then some pos
else if pos == 0 then none
else revPosOfAux s c (s.prev pos)
def revPosOf (s : String) (c : Char) : Option Pos :=
if s.bsize == 0 then none
else revPosOfAux s c (s.prev s.bsize)
partial def findAux (s : String) (p : Char → Bool) (stopPos : Pos) (pos : Pos) : Pos :=
if pos == stopPos then pos
else if p (s.get pos) then pos
else findAux s p stopPos (s.next pos)
@[inline] def find (s : String) (p : Char → Bool) : Pos :=
findAux s p s.bsize 0
partial def revFindAux (s : String) (p : Char → Bool) (pos : Pos) : Option Pos :=
if p (s.get pos) then some pos
else if pos == 0 then none
else revFindAux s p (s.prev pos)
def revFind (s : String) (p : Char → Bool) : Option Pos :=
if s.bsize == 0 then none
else revFindAux s p (s.prev s.bsize)
private def utf8ExtractAux₂ : List Char → Pos → Pos → List Char
| [], _, _ => []
| c::cs, i, e => if i = e then [] else c :: utf8ExtractAux₂ cs (i + csize c) e
private def utf8ExtractAux₁ : List Char → Pos → Pos → Pos → List Char
| [], _, _, _ => []
| s@(c::cs), i, b, e => if i = b then utf8ExtractAux₂ s i e else utf8ExtractAux₁ cs (i + csize c) b e
@[extern "lean_string_utf8_extract"]
def extract : (@& String) → (@& Pos) → (@& Pos) → String
| ⟨s⟩, b, e => if b ≥ e then ⟨[]⟩ else ⟨utf8ExtractAux₁ s 0 b e⟩
@[specialize] partial def splitAux (s : String) (p : Char → Bool) (b : Pos) (i : Pos) (r : List String) : List String :=
if s.atEnd i then
let r := (s.extract b i)::r
r.reverse
else if p (s.get i) then
let i := s.next i
splitAux s p i i (s.extract b (i-1)::r)
else
splitAux s p b (s.next i) r
@[specialize] def split (s : String) (p : Char → Bool) : List String :=
splitAux s p 0 0 []
partial def splitOnAux (s sep : String) (b : Pos) (i : Pos) (j : Pos) (r : List String) : List String :=
if s.atEnd i then
let r := if sep.atEnd j then ""::(s.extract b (i-j))::r else (s.extract b i)::r
r.reverse
else if s.get i == sep.get j then
let i := s.next i
let j := sep.next j
if sep.atEnd j then
splitOnAux s sep i i 0 (s.extract b (i-j)::r)
else
splitOnAux s sep b i j r
else
splitOnAux s sep b (s.next i) 0 r
def splitOn (s : String) (sep : String := " ") : List String :=
if sep == "" then [s] else splitOnAux s sep 0 0 0 []
instance : Inhabited String := ⟨""⟩
instance : Append String := ⟨String.append⟩
def str : String → Char → String := push
def pushn (s : String) (c : Char) (n : Nat) : String :=
n.repeat (fun s => s.push c) s
def isEmpty (s : String) : Bool :=
s.bsize == 0
def join (l : List String) : String :=
l.foldl (fun r s => r ++ s) ""
def singleton (c : Char) : String :=
"".push c
def intercalate (s : String) (ss : List String) : String :=
(List.intercalate s.toList (ss.map toList)).asString
structure Iterator where
s : String
i : Pos
deriving DecidableEq
def mkIterator (s : String) : Iterator :=
⟨s, 0⟩
namespace Iterator
def toString : Iterator → String
| ⟨s, _⟩ => s
def remainingBytes : Iterator → Nat
| ⟨s, i⟩ => s.bsize - i
def pos : Iterator → Pos
| ⟨s, i⟩ => i
def curr : Iterator → Char
| ⟨s, i⟩ => get s i
def next : Iterator → Iterator
| ⟨s, i⟩ => ⟨s, s.next i⟩
def prev : Iterator → Iterator
| ⟨s, i⟩ => ⟨s, s.prev i⟩
def hasNext : Iterator → Bool
| ⟨s, i⟩ => i < utf8ByteSize s
def hasPrev : Iterator → Bool
| ⟨s, i⟩ => i > 0
def setCurr : Iterator → Char → Iterator
| ⟨s, i⟩, c => ⟨s.set i c, i⟩
def toEnd : Iterator → Iterator
| ⟨s, _⟩ => ⟨s, s.bsize⟩
def extract : Iterator → Iterator → String
| ⟨s₁, b⟩, ⟨s₂, e⟩ =>
if s₁ ≠ s₂ || b > e then ""
else s₁.extract b e
def forward : Iterator → Nat → Iterator
| it, 0 => it
| it, n+1 => forward it.next n
def remainingToString : Iterator → String
| ⟨s, i⟩ => s.extract i s.bsize
/-- `(isPrefixOfRemaining it₁ it₂)` is `true` iff `it₁.remainingToString` is a prefix
of `it₂.remainingToString`. -/
def isPrefixOfRemaining : Iterator → Iterator → Bool
| ⟨s₁, i₁⟩, ⟨s₂, i₂⟩ => s₁.extract i₁ s₁.bsize = s₂.extract i₂ (i₂ + (s₁.bsize - i₁))
def nextn : Iterator → Nat → Iterator
| it, 0 => it
| it, i+1 => nextn it.next i
def prevn : Iterator → Nat → Iterator
| it, 0 => it
| it, i+1 => prevn it.prev i
end Iterator
partial def offsetOfPosAux (s : String) (pos : Pos) (i : Pos) (offset : Nat) : Nat :=
if i == pos || s.atEnd i then
offset
else
offsetOfPosAux s pos (s.next i) (offset+1)
def offsetOfPos (s : String) (pos : Pos) : Nat :=
offsetOfPosAux s pos 0 0
@[specialize] partial def foldlAux {α : Type u} (f : α → Char → α) (s : String) (stopPos : Pos) (i : Pos) (a : α) : α :=
let rec loop (i : Pos) (a : α) :=
if i == stopPos then a
else loop (s.next i) (f a (s.get i))
loop i a
@[inline] def foldl {α : Type u} (f : α → Char → α) (init : α) (s : String) : α :=
foldlAux f s s.bsize 0 init
@[specialize] partial def foldrAux {α : Type u} (f : Char → α → α) (a : α) (s : String) (stopPos : Pos) (i : Pos) : α :=
let rec loop (i : Pos) :=
if i == stopPos then a
else f (s.get i) (loop (s.next i))
loop i
@[inline] def foldr {α : Type u} (f : Char → α → α) (init : α) (s : String) : α :=
foldrAux f init s s.bsize 0
@[specialize] partial def anyAux (s : String) (stopPos : Pos) (p : Char → Bool) (i : Pos) : Bool :=
let rec loop (i : Pos) :=
if i == stopPos then false
else if p (s.get i) then true
else loop (s.next i)
loop i
@[inline] def any (s : String) (p : Char → Bool) : Bool :=
anyAux s s.bsize p 0
@[inline] def all (s : String) (p : Char → Bool) : Bool :=
!s.any (fun c => !p c)
def contains (s : String) (c : Char) : Bool :=
s.any (fun a => a == c)
@[specialize] partial def mapAux (f : Char → Char) (i : Pos) (s : String) : String :=
if s.atEnd i then s
else
let c := f (s.get i)
let s := s.set i c
mapAux f (s.next i) s
@[inline] def map (f : Char → Char) (s : String) : String :=
mapAux f 0 s
def isNat (s : String) : Bool :=
s.all fun c => c.isDigit
def toNat? (s : String) : Option Nat :=
if s.isNat then
some <| s.foldl (fun n c => n*10 + (c.toNat - '0'.toNat)) 0
else
none
/-- Return true iff `p` is a prefix of `s` -/
partial def isPrefixOf (p : String) (s : String) : Bool :=
let rec loop (i : Pos) :=
if p.atEnd i then true
else
let c₁ := p.get i
let c₂ := s.get i
c₁ == c₂ && loop (s.next i)
p.length ≤ s.length && loop 0
end String
namespace Substring
@[inline] def isEmpty (ss : Substring) : Bool :=
ss.bsize == 0
@[inline] def toString : Substring → String
| ⟨s, b, e⟩ => s.extract b e
@[inline] def toIterator : Substring → String.Iterator
| ⟨s, b, _⟩ => ⟨s, b⟩
/-- Return the codepoint at the given offset into the substring. -/
@[inline] def get : Substring → String.Pos → Char
| ⟨s, b, _⟩, p => s.get (b+p)
/-- Given an offset of a codepoint into the substring,
return the offset there of the next codepoint. -/
@[inline] private def next : Substring → String.Pos → String.Pos
| ⟨s, b, e⟩, p =>
let absP := b+p
if absP = e then p else s.next absP - b
/-- Given an offset of a codepoint into the substring,
return the offset there of the previous codepoint. -/
@[inline] private def prev : Substring → String.Pos → String.Pos
| ⟨s, b, _⟩, p =>
let absP := b+p
if absP = b then p else s.prev absP - b
private def nextn : Substring → Nat → String.Pos → String.Pos
| ss, 0, p => p
| ss, i+1, p => ss.nextn i (ss.next p)
private def prevn : Substring → String.Pos → Nat → String.Pos
| ss, 0, p => p
| ss, i+1, p => ss.prevn i (ss.prev p)
@[inline] def front (s : Substring) : Char :=
s.get 0
/-- Return the offset into `s` of the first occurence of `c` in `s`,
or `s.bsize` if `c` doesn't occur. -/
@[inline] def posOf (s : Substring) (c : Char) : String.Pos :=
match s with
| ⟨s, b, e⟩ => (String.posOfAux s c e b) - b
@[inline] def drop : Substring → Nat → Substring
| ss@⟨s, b, e⟩, n => ⟨s, b + ss.nextn n 0, e⟩
@[inline] def dropRight : Substring → Nat → Substring
| ss@⟨s, b, e⟩, n => ⟨s, b, b + ss.prevn n ss.bsize⟩
@[inline] def take : Substring → Nat → Substring
| ss@⟨s, b, e⟩, n => ⟨s, b, b + ss.nextn n 0⟩
@[inline] def takeRight : Substring → Nat → Substring
| ss@⟨s, b, e⟩, n => ⟨s, b + ss.prevn n ss.bsize, e⟩
@[inline] def atEnd : Substring → String.Pos → Bool
| ⟨s, b, e⟩, p => b + p == e
@[inline] def extract : Substring → String.Pos → String.Pos → Substring
| ⟨s, b, _⟩, b', e' => if b' ≥ e' then ⟨"", 0, 1⟩ else ⟨s, b+b', b+e'⟩
partial def splitOn (s : Substring) (sep : String := " ") : List Substring :=
if sep == "" then
[s]
else
let stopPos := s.stopPos
let str := s.str
let rec loop (b i j : String.Pos) (r : List Substring) : List Substring :=
if i == stopPos then
let r := if sep.atEnd j then
"".toSubstring::{ str := str, startPos := b, stopPos := i-j } :: r
else
{ str := str, startPos := b, stopPos := i } :: r
r.reverse
else if s.get i == sep.get j then
let i := s.next i
let j := sep.next j
if sep.atEnd j then
loop i i 0 ({ str := str, startPos := b, stopPos := i-j } :: r)
else
loop b i j r
else
loop b (s.next i) 0 r
loop s.startPos s.startPos 0 []
@[inline] def foldl {α : Type u} (f : α → Char → α) (init : α) (s : Substring) : α :=
match s with
| ⟨s, b, e⟩ => String.foldlAux f s e b init
@[inline] def foldr {α : Type u} (f : Char → α → α) (init : α) (s : Substring) : α :=
match s with
| ⟨s, b, e⟩ => String.foldrAux f init s e b
@[inline] def any (s : Substring) (p : Char → Bool) : Bool :=
match s with
| ⟨s, b, e⟩ => String.anyAux s e p b
@[inline] def all (s : Substring) (p : Char → Bool) : Bool :=
!s.any (fun c => !p c)
def contains (s : Substring) (c : Char) : Bool :=
s.any (fun a => a == c)
@[specialize] private partial def takeWhileAux (s : String) (stopPos : String.Pos) (p : Char → Bool) (i : String.Pos) : String.Pos :=
if i == stopPos then i
else if p (s.get i) then takeWhileAux s stopPos p (s.next i)
else i
@[inline] def takeWhile : Substring → (Char → Bool) → Substring
| ⟨s, b, e⟩, p =>
let e := takeWhileAux s e p b;
⟨s, b, e⟩
@[inline] def dropWhile : Substring → (Char → Bool) → Substring
| ⟨s, b, e⟩, p =>
let b := takeWhileAux s e p b;
⟨s, b, e⟩
@[specialize] private partial def takeRightWhileAux (s : String) (begPos : String.Pos) (p : Char → Bool) (i : String.Pos) : String.Pos :=
if i == begPos then i
else
let i' := s.prev i
let c := s.get i'
if !p c then i
else takeRightWhileAux s begPos p i'
@[inline] def takeRightWhile : Substring → (Char → Bool) → Substring
| ⟨s, b, e⟩, p =>
let b := takeRightWhileAux s b p e
⟨s, b, e⟩
@[inline] def dropRightWhile : Substring → (Char → Bool) → Substring
| ⟨s, b, e⟩, p =>
let e := takeRightWhileAux s b p e
⟨s, b, e⟩
@[inline] def trimLeft (s : Substring) : Substring :=
s.dropWhile Char.isWhitespace
@[inline] def trimRight (s : Substring) : Substring :=
s.dropRightWhile Char.isWhitespace
@[inline] def trim : Substring → Substring
| ⟨s, b, e⟩ =>
let b := takeWhileAux s e Char.isWhitespace b
let e := takeRightWhileAux s b Char.isWhitespace e
⟨s, b, e⟩
def isNat (s : Substring) : Bool :=
s.all fun c => c.isDigit
def toNat? (s : Substring) : Option Nat :=
if s.isNat then
some <| s.foldl (fun n c => n*10 + (c.toNat - '0'.toNat)) 0
else
none
def beq (ss1 ss2 : Substring) : Bool :=
-- TODO: should not allocate
ss1.bsize == ss2.bsize && ss1.toString == ss2.toString
instance hasBeq : BEq Substring := ⟨beq⟩
end Substring
namespace String
def drop (s : String) (n : Nat) : String :=
(s.toSubstring.drop n).toString
def dropRight (s : String) (n : Nat) : String :=
(s.toSubstring.dropRight n).toString
def take (s : String) (n : Nat) : String :=
(s.toSubstring.take n).toString
def takeRight (s : String) (n : Nat) : String :=
(s.toSubstring.takeRight n).toString
def takeWhile (s : String) (p : Char → Bool) : String :=
(s.toSubstring.takeWhile p).toString
def dropWhile (s : String) (p : Char → Bool) : String :=
(s.toSubstring.dropWhile p).toString
def takeRightWhile (s : String) (p : Char → Bool) : String :=
(s.toSubstring.takeRightWhile p).toString
def dropRightWhile (s : String) (p : Char → Bool) : String :=
(s.toSubstring.dropRightWhile p).toString
def startsWith (s pre : String) : Bool :=
s.toSubstring.take pre.length == pre.toSubstring
def endsWith (s post : String) : Bool :=
s.toSubstring.takeRight post.length == post.toSubstring
def trimRight (s : String) : String :=
s.toSubstring.trimRight.toString
def trimLeft (s : String) : String :=
s.toSubstring.trimLeft.toString
def trim (s : String) : String :=
s.toSubstring.trim.toString
@[inline] def nextWhile (s : String) (p : Char → Bool) (i : String.Pos) : String.Pos :=
Substring.takeWhileAux s s.bsize p i
@[inline] def nextUntil (s : String) (p : Char → Bool) (i : String.Pos) : String.Pos :=
nextWhile s (fun c => !p c) i
def toUpper (s : String) : String :=
s.map Char.toUpper
def toLower (s : String) : String :=
s.map Char.toLower
def capitalize (s : String) :=
s.set 0 <| s.get 0 |>.toUpper
def decapitalize (s : String) :=
s.set 0 <| s.get 0 |>.toLower
end String
protected def Char.toString (c : Char) : String :=
String.singleton c
|
ba98b87a75419bc3ee5b37460971ec42785ba099 | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /stage0/src/Leanpkg/Manifest.lean | 50caaafe4ed798b1cc0e497d1bd04d755deaed48 | [
"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 | 2,847 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Gabriel Ebner, Sebastian Ullrich
-/
import Leanpkg.Toml
import Leanpkg.LeanVersion
open System
namespace Leanpkg
inductive Source where
| path (dir : System.FilePath) : Source
| git (url rev : String) (branch : Option String) : Source
namespace Source
def fromToml (v : Toml.Value) : Option Source :=
(do let Toml.Value.str dir ← v.lookup "path" | none
path ⟨dir⟩) <|>
(do let Toml.Value.str url ← v.lookup "git" | none
let Toml.Value.str rev ← v.lookup "rev" | none
match v.lookup "branch" with
| none => git url rev none
| some (Toml.Value.str branch) => git url rev (some branch)
| _ => none)
def toToml : Source → Toml.Value
| path dir => Toml.Value.table [("path", Toml.Value.str dir.toString)]
| git url rev none =>
Toml.Value.table [("git", Toml.Value.str url), ("rev", Toml.Value.str rev)]
| git url rev (some branch) =>
Toml.Value.table [("git", Toml.Value.str url), ("branch", Toml.Value.str branch), ("rev", Toml.Value.str rev)]
end Source
structure Dependency where
name : String
src : Source
structure Manifest where
name : String
version : String
leanVersion : String := leanVersionString
timeout : Option Nat := none
path : Option FilePath := none
dependencies : List Dependency := []
namespace Manifest
def effectivePath (m : Manifest) : FilePath :=
m.path.getD ⟨"."⟩
def fromToml (t : Toml.Value) : Option Manifest := OptionM.run do
let pkg ← t.lookup "package"
let Toml.Value.str n ← pkg.lookup "name" | none
let Toml.Value.str ver ← pkg.lookup "version" | none
let leanVer ← match pkg.lookup "lean_version" with
| some (Toml.Value.str leanVer) => some leanVer
| none => some leanVersionString
| _ => none
let tm ← match pkg.lookup "timeout" with
| some (Toml.Value.nat timeout) => some (some timeout)
| none => some none
| _ => none
let path ← match pkg.lookup "path" with
| some (Toml.Value.str path) => some (some ⟨path⟩)
| none => some none
| _ => none
let Toml.Value.table deps ← t.lookup "dependencies" <|> some (Toml.Value.table []) | none
let deps ← deps.mapM fun ⟨n, src⟩ => do Dependency.mk n (← Source.fromToml src)
return { name := n, version := ver, leanVersion := leanVer,
path := path, dependencies := deps, timeout := tm }
def fromFile (fn : System.FilePath) : IO Manifest := do
let cnts ← IO.FS.readFile fn
let toml ← Toml.parse cnts
let some manifest ← pure (fromToml toml)
| throw <| IO.userError s!"cannot read manifest from {fn}"
manifest
end Manifest
def leanpkgTomlFn : System.FilePath := ⟨"leanpkg.toml"⟩
end Leanpkg
|
0e6cae3b6fa17f47467a20e0eaea25bb9f7b6bb5 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/tactic/norm_fin.lean | b2a2013719555e779ab0bbec19727ab76a702cca | [
"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 | 25,241 | lean | /-
Copyright (c) 2021 Yakov Pechersky All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yakov Pechersky, Mario Carneiro
-/
import tactic.norm_num
/-!
# `norm_fin`
This file defines functions for dealing with `fin n` numbers as expressions.
## Main definitions
* `tactic.norm_fin.eval_ineq` is a `norm_num` plugin for normalizing equalities and inequalities of
type `fin n`.
* `tactic.interactive.norm_fin` is a standalone tactic like `norm_num` for normalizing `fin n`
expressions anywhere in the goal.
-/
namespace tactic
namespace norm_fin
open norm_num
/-- `normalize_fin n a b` means that `a : fin n` is equivalent to `b : ℕ` in the modular sense -
that is, `↑a ≡ b (mod n)`. This is used for translating the algebraic operations: addition,
multiplication, zero and one, which use modulo for reduction. -/
def normalize_fin (n : ℕ) (a : fin n) (b : ℕ) := a.1 = b % n
/-- `normalize_fin_lt n a b` means that `a : fin n` is equivalent to `b : ℕ` in the embedding
sense - that is, `↑a = b`. This is used for operations that treat `fin n` as the subset
`{0, ..., n-1}` of `ℕ`. For example, `fin.succ : fin n → fin (n+1)` is thought of as the successor
function, but it does not lift to a map `zmod n → zmod (n+1)`; this addition only makes sense if
the input is strictly less than `n`.
`normalize_fin_lt n a b` is equivalent to `normalize_fin n a b ∧ b < n`. -/
def normalize_fin_lt (n : ℕ) (a : fin n) (b : ℕ) := a.1 = b
theorem normalize_fin_lt.coe {n} {a : fin n} {b : ℕ} (h : normalize_fin_lt n a b) : ↑a = b := h
theorem normalize_fin_iff {n} [fact (0 < n)] {a b} :
normalize_fin n a b ↔ a = fin.of_nat' b :=
iff.symm (fin.eq_iff_veq _ _)
theorem normalize_fin_lt.mk {n a b n'} (hn : n = n')
(h : normalize_fin n a b) (h2 : b < n') : normalize_fin_lt n a b :=
h.trans $ nat.mod_eq_of_lt $ by rw hn; exact h2
theorem normalize_fin_lt.lt {n a b} (h : normalize_fin_lt n a b) : b < n :=
by rw ← h.coe; exact a.2
theorem normalize_fin_lt.of {n a b} (h : normalize_fin_lt n a b) : normalize_fin n a b :=
h.trans $ eq.symm $ nat.mod_eq_of_lt h.lt
theorem normalize_fin.zero (n) : normalize_fin (n+1) 0 0 := by { rw normalize_fin, norm_num }
theorem normalize_fin_lt.zero (n) : normalize_fin_lt (n+1) 0 0 := refl _
theorem normalize_fin.one (n) : normalize_fin (n+1) 1 1 := refl _
theorem normalize_fin.add {n} {a b : fin n} {a' b' c' : ℕ}
(ha : normalize_fin n a a') (hb : normalize_fin n b b')
(h : a' + b' = c') : normalize_fin n (a + b) c' :=
by simp only [normalize_fin, ← h] at *; rw [nat.add_mod, ← ha, ← hb, fin.add_def]
theorem normalize_fin.mul {n} {a b : fin n} {a' b' c' : ℕ}
(ha : normalize_fin n a a') (hb : normalize_fin n b b')
(h : a' * b' = c') : normalize_fin n (a * b) c' :=
by simp only [normalize_fin, ← h] at *; rw [nat.mul_mod, ← ha, ← hb, fin.mul_def]
theorem normalize_fin.bit0 {n} {a : fin n} {a' : ℕ}
(h : normalize_fin n a a') : normalize_fin n (bit0 a) (bit0 a') := h.add h rfl
theorem normalize_fin.bit1 {n} {a : fin (n+1)} {a' : ℕ}
(h : normalize_fin (n+1) a a') : normalize_fin (n+1) (bit1 a) (bit1 a') :=
h.bit0.add (normalize_fin.one _) rfl
theorem normalize_fin_lt.succ {n} {a : fin n} {a' b : ℕ}
(h : normalize_fin_lt n a a') (e : a' + 1 = b) : normalize_fin_lt n.succ (fin.succ a) b :=
by simpa [normalize_fin_lt, ← e] using h
theorem normalize_fin_lt.cast_lt {n m} {a : fin m} {ha} {a' : ℕ}
(h : normalize_fin_lt m a a') : normalize_fin_lt n (fin.cast_lt a ha) a' :=
by simpa [normalize_fin_lt] using h
theorem normalize_fin_lt.cast_le {n m} {nm} {a : fin m} {a' : ℕ}
(h : normalize_fin_lt m a a') : normalize_fin_lt n (fin.cast_le nm a) a' :=
by simpa [normalize_fin_lt] using h
theorem normalize_fin_lt.cast {n m} {nm} {a : fin m} {a' : ℕ}
(h : normalize_fin_lt m a a') : normalize_fin_lt n (fin.cast nm a) a' :=
by simpa [normalize_fin_lt] using h
theorem normalize_fin.cast {n m} {nm} {a : fin m} {a' : ℕ}
(h : normalize_fin m a a') : normalize_fin n (fin.cast nm a) a' :=
by convert ← normalize_fin_lt.cast h
theorem normalize_fin_lt.cast_add {n m} {a : fin n} {a' : ℕ}
(h : normalize_fin_lt n a a') : normalize_fin_lt (n + m) (fin.cast_add m a) a' :=
by simpa [normalize_fin_lt] using h
theorem normalize_fin_lt.cast_succ {n} {a : fin n} {a' : ℕ}
(h : normalize_fin_lt n a a') : normalize_fin_lt (n+1) (fin.cast_succ a) a' :=
normalize_fin_lt.cast_add h
theorem normalize_fin_lt.add_nat {n m m'} (hm : m = m') {a : fin n} {a' b : ℕ}
(h : normalize_fin_lt n a a') (e : a' + m' = b) :
normalize_fin_lt (n+m) (@fin.add_nat n m a) b :=
by simpa [normalize_fin_lt, ← e, ← hm] using h
theorem normalize_fin_lt.nat_add {n m n'} (hn : n = n') {a : fin m} {a' b : ℕ}
(h : normalize_fin_lt m a a') (e : n' + a' = b) :
normalize_fin_lt (n+m) (@fin.nat_add n m a) b :=
by simpa [normalize_fin_lt, ← e, ← hn] using h
theorem normalize_fin.reduce {n} {a : fin n} {n' a' b k nk : ℕ}
(hn : n = n') (h : normalize_fin n a a') (e1 : n' * k = nk) (e2 : nk + b = a') :
normalize_fin n a b :=
by rwa [← e2, ← e1, ← hn, normalize_fin, add_comm, nat.add_mul_mod_self_left] at h
theorem normalize_fin_lt.reduce {n} {a : fin n} {n' a' b k nk : ℕ}
(hn : n = n') (h : normalize_fin n a a') (e1 : n' * k = nk) (e2 : nk + b = a') (hl : b < n') :
normalize_fin_lt n a b :=
normalize_fin_lt.mk hn (h.reduce hn e1 e2) hl
theorem normalize_fin.eq {n} {a b : fin n} {c : ℕ}
(ha : normalize_fin n a c) (hb : normalize_fin n b c) : a = b := fin.eq_of_veq $ ha.trans hb.symm
theorem normalize_fin.lt {n} {a b : fin n} {a' b' : ℕ}
(ha : normalize_fin n a a') (hb : normalize_fin_lt n b b') (h : a' < b') : a < b :=
by have ha' := normalize_fin_lt.mk rfl ha (h.trans hb.lt); rwa [← hb.coe, ← ha'.coe] at h
theorem normalize_fin.le {n} {a b : fin n} {a' b' : ℕ}
(ha : normalize_fin n a a') (hb : normalize_fin_lt n b b') (h : a' ≤ b') : a ≤ b :=
by have ha' := normalize_fin_lt.mk rfl ha (h.trans_lt hb.lt); rwa [← hb.coe, ← ha'.coe] at h
/-- The monad for the `norm_fin` internal tactics. The state consists of an instance cache for `ℕ`,
and a tuple `(nn, n', p)` where `p` is a proof of `n = n'` and `nn` is `n` evaluated to a natural
number. (`n` itself is implicit.) It is in an `option` because it is lazily initialized - for many
`n` we will never need this information, and indeed eagerly computing it would make some reductions
fail spuriously if `n` is not a numeral. -/
@[derive [monad, alternative]]
meta def eval_fin_m (α : Type) : Type :=
state_t (instance_cache × option (ℕ × expr × expr)) tactic α
/-- Lifts a tactic into the `eval_fin_m` monad. -/
@[inline] meta def eval_fin_m.lift {α} (m : tactic α) : eval_fin_m α :=
⟨λ ⟨ic, r⟩, do a ← m, pure (a, ic, r)⟩
meta instance {α} : has_coe (tactic α) (eval_fin_m α) := ⟨eval_fin_m.lift⟩
/-- Lifts an `instance_cache` tactic into the `eval_fin_m` monad. -/
@[inline] meta def eval_fin_m.lift_ic {α}
(m : instance_cache → tactic (instance_cache × α)) : eval_fin_m α :=
⟨λ ⟨ic, r⟩, do (ic, a) ← m ic, pure (a, ic, r)⟩
/-- Evaluates a monadic action with a fresh `n` cache, and restore the old cache on completion of
the action. This is used when evaluating a tactic in the context of a different `n` than the parent
context. For example if we are evaluating `fin.succ a`, then `a : fin n` and
`fin.succ a : fin (n+1)`, so the parent cache will be about `n+1` and we need a separate cache for
`n`. -/
@[inline] meta def eval_fin_m.reset {α} (m : eval_fin_m α) : eval_fin_m α :=
⟨λ ⟨ic, r⟩, do (a, ic, _) ← m.run ⟨ic, none⟩, pure (a, ic, r)⟩
/-- Given `n`, returns a tuple `(nn, n', p)` where `p` is a proof of `n = n'` and `nn` is `n`
evaluated to a natural number. The result of the evaluation is cached for future references.
Future calls to this function must use the same value of `n`, unless it is in a sub-context
created by `eval_fin_m.reset`. -/
meta def eval_fin_m.eval_n (n : expr) : eval_fin_m (ℕ × expr × expr) :=
⟨λ ⟨ic, r⟩, match r with
| none := do
(n', p) ← or_refl_conv norm_num.derive n,
nn ← n'.to_nat,
let np := (nn, n', p),
pure (np, ic, some np)
| some np := pure (np, ic, some np)
end⟩
/-- Run an `eval_fin_m` action with a new cache and discard the cache after evaluation. -/
@[inline] meta def eval_fin_m.run {α} (m : eval_fin_m α) : tactic α :=
do ic ← mk_instance_cache `(ℕ), (a, _) ← state_t.run m (ic, none), pure a
/-- The expression constructors recognized by the `eval_fin` evaluator. This is used instead of a
direct expr pattern match because expr pattern matches generate very large terms under the
hood so going via an intermediate inductive type like this is more efficient. -/
meta inductive match_fin_result
| zero (n : expr) -- `(0 : fin (n+1))`
| one (n : expr) -- `(1 : fin (n+1))`
| add (n a b : expr) -- `(a + b : fin n)`
| mul (n a b : expr) -- `(a * b : fin n)`
| bit0 (n a : expr) -- `(bit0 a : fin n)`
| bit1 (n a : expr) -- `(bit1 a : fin (n+1))`
| succ (n a : expr) -- `(fin.succ a : fin n.succ)`
| cast_lt (n m i h : expr) -- `(fin.cast_lt (i : fin m) (h : i.val < n) : fin n)`
| cast_le (n m h a : expr) -- `(fin.cast_le (h : n ≤ m) (a : fin n) : fin m)`
| cast (n m h a : expr) -- `(fin.cast_le (h : n = m) (a : fin n) : fin m)`
| cast_add (n m a : expr) -- `(fin.cast_add m (a : fin n) : fin (n + m))`
| cast_succ (n a : expr) -- `(fin.cast_succ (a : fin n) : fin (n + 1))`
| add_nat (n m a : expr) -- `(fin.add_nat m (a : fin n) : fin (n + m))`
| nat_add (n m a : expr) -- `(fin.nat_add n (a : fin m) : fin (n + m))`
section
open match_fin_result
/-- Match a fin expression of the form `(coe_fn f a)` where `f` is some fin function. Several fin
functions are written this way: for example `cast_le : n ≤ m → fin n ↪o fin m` is not actually a
function but rather an order embedding with a coercion to a function. -/
meta def match_fin_coe_fn (a : expr) : expr → option match_fin_result
| `(@fin.cast_le %%n %%m %%h) := some (cast_le n m h a)
| `(@fin.cast %%m %%n %%h) := some (cast n m h a)
| `(@fin.cast_add %%n %%m) := some (cast_add n m a)
| `(@fin.cast_succ %%n) := some (cast_succ n a)
| `(@fin.add_nat %%n %%m) := some (add_nat n m a)
| `(@fin.nat_add %%n %%m) := some (nat_add n m a)
| _ := none
/-- Match a fin expression to a `match_fin_result`, for easier pattern matching in the
evaluator. -/
meta def match_fin : expr → option match_fin_result
| `(@has_zero.zero ._ (@fin.has_zero %%n)) := some (zero n)
| `(@has_one.one ._ (@fin.has_one %%n)) := some (one n)
| `(@has_add.add (fin %%n) ._ %%a %%b) := some (add n a b)
| `(@has_mul.mul (fin %%n) ._ %%a %%b) := some (mul n a b)
| `(@_root_.bit0 (fin %%n) ._ %%a) := some (bit0 n a)
| `(@_root_.bit1 ._ (@fin.has_one %%n) ._ %%a) := some (bit1 n a)
| `(@fin.succ %%n %%a) := some (succ n a)
| `(@fin.cast_lt %%n %%m %%a %%h) := some (cast_lt n m a h)
| (expr.app `(@coe_fn ._ ._ ._ %%f) a) := match_fin_coe_fn a f
| _ := none
end
/-- `reduce_fin lt n a (a', pa)` expects that `pa : normalize_fin n a a'` where `a'`
is a natural numeral, and produces `(b, pb)` where `pb : normalize_fin n a b` if `lt` is false, or
`pb : normalize_fin_lt n a b` if `lt` is true. In either case, `b` will be chosen to be less than
`n`, but if `lt` is true then we also prove it. This requires that `n` can be evaluated to a
numeral. -/
meta def reduce_fin' : bool → expr → expr → expr × expr → eval_fin_m (expr × expr)
| lt n a (a', pa) := do
(nn, n', pn) ← eval_fin_m.eval_n n,
na ← expr.to_nat a',
if na < nn then
if lt then do
p ← eval_fin_m.lift_ic (λ ic, prove_lt_nat ic a' n'),
pure (a', `(@normalize_fin_lt.mk).mk_app [n, a, a', n', pn, pa, p])
else pure (a', pa)
else
let nb := na % nn, nk := (na - nb) / nn in
eval_fin_m.lift_ic $ λ ic, do
(ic, k) ← ic.of_nat nk,
(ic, b) ← ic.of_nat nb,
(ic, nk, pe1) ← prove_mul_nat ic n' k,
(ic, pe2) ← prove_add_nat ic nk b a',
if lt then do
(ic, p) ← prove_lt_nat ic b n',
pure (ic, b,
`(@normalize_fin_lt.reduce).mk_app [n, a, n', a', b, k, nk, pn, pa, pe1, pe2, p])
else pure (ic, b,
`(@normalize_fin.reduce).mk_app [n, a, n', a', b, k, nk, pn, pa, pe1, pe2])
/-- `eval_fin_lt' eval_fin n a` expects that `a : fin n`, and produces `(b, p)` where
`p : normalize_fin_lt n a b`. (It is mutually recursive with `eval_fin` which is why it takes the
function as an argument.) -/
meta def eval_fin_lt' (eval_fin : expr → eval_fin_m (expr × expr)) :
expr → expr → eval_fin_m (expr × expr)
| n a := do
e ← match_fin a,
match e with
| match_fin_result.succ n a := do
(a', pa) ← (eval_fin_lt' n a).reset,
(b, pb) ← eval_fin_m.lift_ic (λ ic, prove_succ' ic a'),
pure (b, `(@normalize_fin_lt.succ).mk_app [n, a, a', b, pa, pb])
| match_fin_result.cast_lt _ m a h := do
(a', pa) ← (eval_fin_lt' m a).reset,
pure (a', `(@normalize_fin_lt.cast_lt).mk_app [n, m, a, h, a', pa])
| match_fin_result.cast_le _ m nm a := do
(a', pa) ← (eval_fin_lt' m a).reset,
pure (a', `(@normalize_fin_lt.cast_le).mk_app [n, m, nm, a, a', pa])
| match_fin_result.cast m _ nm a := do
(a', pa) ← (eval_fin_lt' m a).reset,
pure (a', `(@normalize_fin_lt.cast).mk_app [n, m, nm, a, a', pa])
| match_fin_result.cast_add n m a := do
(a', pa) ← (eval_fin_lt' m a).reset,
pure (a', `(@normalize_fin_lt.cast_add).mk_app [n, m, a, a', pa])
| match_fin_result.cast_succ n a := do
(a', pa) ← (eval_fin_lt' n a).reset,
pure (a', `(@normalize_fin_lt.cast_succ).mk_app [n, a, a', pa])
| match_fin_result.add_nat n m a := do
(a', pa) ← (eval_fin_lt' n a).reset,
(m', pm) ← or_refl_conv norm_num.derive m,
(b, pb) ← eval_fin_m.lift_ic (λ ic, prove_add_nat' ic a' m'),
pure (b, `(@normalize_fin_lt.add_nat).mk_app [n, m, m', pm, a, a', b, pa, pb])
| match_fin_result.nat_add n m a := do
(a', pa) ← (eval_fin_lt' m a).reset,
(n', pn) ← or_refl_conv norm_num.derive n,
(b, pb) ← eval_fin_m.lift_ic (λ ic, prove_add_nat' ic n' a'),
pure (b, `(@normalize_fin_lt.nat_add).mk_app [n, m, n', pn, a, a', b, pa, pb])
| _ := do
(_, n', pn) ← eval_fin_m.eval_n n,
(a', pa) ← eval_fin a >>= reduce_fin' tt n a,
p ← eval_fin_m.lift_ic (λ ic, prove_lt_nat ic a' n'),
pure (a', `(@normalize_fin_lt.mk).mk_app [n, a, a', n', pn, pa, p])
end
/-- Get `n` such that `a : fin n`. -/
meta def get_fin_type (a : expr) : tactic expr := do `(fin %%n) ← infer_type a, pure n
/-- Given `a : fin n`, `eval_fin a` returns `(b, p)` where `p : normalize_fin n a b`. This function
does no reduction of the numeral `b`; for example `eval_fin (5 + 5 : fin 6)` returns `10`. It works
even if `n` is a variable, for example `eval_fin (5 + 5 : fin (n+1))` also returns `10`. -/
meta def eval_fin : expr → eval_fin_m (expr × expr)
| a := do
m ← match_fin a,
match m with
| match_fin_result.zero n := pure (`(0 : ℕ), `(normalize_fin.zero).mk_app [n])
| match_fin_result.one n := pure (`(1 : ℕ), `(normalize_fin.one).mk_app [n])
| match_fin_result.add n a b := do
(a', pa) ← eval_fin a,
(b', pb) ← eval_fin b,
(c, pc) ← eval_fin_m.lift_ic (λ ic, prove_add_nat' ic a' b'),
pure (c, `(@normalize_fin.add).mk_app [n, a, b, a', b', c, pa, pb, pc])
| match_fin_result.mul n a b := do
(a', pa) ← eval_fin a,
(b', pb) ← eval_fin b,
(c, pc) ← eval_fin_m.lift_ic (λ ic, prove_mul_nat ic a' b'),
pure (c, `(@normalize_fin.mul).mk_app [n, a, b, a', b', c, pa, pb, pc])
| match_fin_result.bit0 n a := do
(a', pa) ← eval_fin a,
pure (`(@bit0 ℕ _).mk_app [a'], `(@normalize_fin.bit0).mk_app [n, a, a', pa])
| match_fin_result.bit1 n a := do
(a', pa) ← eval_fin a,
pure (`(@bit1 ℕ _ _).mk_app [a'], `(@normalize_fin.bit1).mk_app [n, a, a', pa])
| match_fin_result.cast m n nm a := do
(a', pa) ← (eval_fin a).reset,
pure (a', `(@normalize_fin.cast).mk_app [n, m, nm, a, a', pa])
| _ := do
n ← get_fin_type a,
(a', pa) ← eval_fin_lt' eval_fin n a,
pure (a', `(@normalize_fin_lt.of).mk_app [n, a, a', pa])
end
/-- `eval_fin_lt n a` expects that `a : fin n`, and produces `(b, p)` where
`p : normalize_fin_lt n a b`. -/
meta def eval_fin_lt : expr → expr → eval_fin_m (expr × expr) := eval_fin_lt' eval_fin
/-- Given `a : fin n`, `eval_fin ff n a` returns `(b, p)` where `p : normalize_fin n a b`, and
`eval_fin tt n a` returns `p : normalize_fin_lt n a b`. Unlike `eval_fin`, this also does reduction
of the numeral `b`; for example `reduce_fin ff 6 (5 + 5 : fin 6)` returns `4`. As a result, it
fails if `n` is a variable, for example `reduce_fin ff (n+1) (5 + 5 : fin (n+1))` fails. -/
meta def reduce_fin (lt : bool) (n a : expr) : eval_fin_m (expr × expr) :=
eval_fin a >>= reduce_fin' lt n a
/-- If `a b : fin n` and `a'` and `b'` are as returned by `eval_fin`,
then `prove_lt_fin' n a b a' b'` proves `a < b`. -/
meta def prove_lt_fin' : expr → expr → expr → expr × expr → expr × expr → eval_fin_m expr
| n a b a' b' := do
(a', pa) ← reduce_fin' ff n a a',
(b', pb) ← reduce_fin' tt n b b',
p ← eval_fin_m.lift_ic (λ ic, prove_lt_nat ic a' b'),
pure (`(@normalize_fin.lt).mk_app [n, a, b, a', b', pa, pb, p])
/-- If `a b : fin n` and `a'` and `b'` are as returned by `eval_fin`,
then `prove_le_fin' n a b a' b'` proves `a ≤ b`. -/
meta def prove_le_fin' : expr → expr → expr → expr × expr → expr × expr → eval_fin_m expr
| n a b a' b' := do
(a', pa) ← reduce_fin' ff n a a',
(b', pb) ← reduce_fin' tt n b b',
p ← eval_fin_m.lift_ic (λ ic, prove_le_nat ic a' b'),
pure (`(@normalize_fin.le).mk_app [n, a, b, a', b', pa, pb, p])
/-- If `a b : fin n` and `a'` and `b'` are as returned by `eval_fin`,
then `prove_eq_fin' n a b a' b'` proves `a = b`. -/
meta def prove_eq_fin' : expr → expr → expr → expr × expr → expr × expr → eval_fin_m expr
| n a b (a', pa) (b', pb) :=
if a' =ₐ b' then do
pure (`(@normalize_fin.eq).mk_app [n, a, b, a', pa, pb])
else do
(a', pa) ← reduce_fin' ff n a (a', pa),
(b', pb) ← reduce_fin' ff n b (b', pb),
guard (a' =ₐ b'),
pure (`(@normalize_fin.eq).mk_app [n, a, b, a', pa, pb])
/-- Given a function with the type of `prove_eq_fin'`, evaluates it with the given `a` and `b`. -/
meta def eval_prove_fin
(f : expr → expr → expr → expr × expr → expr × expr → eval_fin_m expr)
(a b : expr) : tactic expr :=
do n ← get_fin_type a, eval_fin_m.run $ eval_fin a >>= λ a', eval_fin b >>= f n a b a'
/-- If `a b : fin n`, then `prove_eq_fin a b` proves `a = b`. -/
meta def prove_eq_fin : expr → expr → tactic expr := eval_prove_fin prove_eq_fin'
/-- If `a b : fin n`, then `prove_lt_fin a b` proves `a < b`. -/
meta def prove_lt_fin : expr → expr → tactic expr := eval_prove_fin prove_lt_fin'
/-- If `a b : fin n`, then `prove_le_fin a b` proves `a ≤ b`. -/
meta def prove_le_fin : expr → expr → tactic expr := eval_prove_fin prove_le_fin'
section
open norm_num.match_numeral_result
/-- Given expressions `n` and `m` such that `n` is definitionally equal to `m.succ`, and
a natural numeral `a`, proves `(b, ⊢ normalize_fin n b a)`, where `n` and `m` are both used
in the construction of the numeral `b : fin n`. -/
meta def mk_fin_numeral (n m : expr) : expr → option (expr × expr)
| a := match match_numeral a with
| zero := some (
expr.app `(@has_zero.zero (fin %%n)) `(@fin.has_zero %%m),
expr.app `(normalize_fin.zero) m)
| one := some (
expr.app `(@has_one.one (fin %%n)) `(@fin.has_one %%m),
expr.app `(normalize_fin.one) m)
| bit0 a := do
(a', p) ← mk_fin_numeral a,
some (`(bit0 %%a' : fin %%n), `(@normalize_fin.bit0).mk_app [n, a', a, p])
| bit1 a := do
(a', p) ← mk_fin_numeral a,
some (
`(@_root_.bit1 (fin %%n)).mk_app [`(@fin.has_one %%m), `(@fin.has_add %%n), a'],
`(@normalize_fin.bit1).mk_app [m, a', a, p])
| _ := none
end
end
/-- The common prep work for the cases in `eval_ineq`. Given inputs `a b : fin n`, it calls
`f n a' b' na nb` where `a'` and `b'` are the result of `eval_fin` and `na` and `nb` are
`a' % n` and `b' % n` as natural numbers. -/
meta def eval_rel {α} (a b : expr)
(f : expr → expr × expr → expr × expr → ℕ → ℕ → eval_fin_m α) : tactic α :=
do n ← get_fin_type a,
eval_fin_m.run $ do
(nn, n', pn) ← eval_fin_m.eval_n n,
(a', pa) ← eval_fin a,
(b', pb) ← eval_fin b,
na ← eval_fin_m.lift a'.to_nat,
nb ← eval_fin_m.lift b'.to_nat,
f n (a', pa) (b', pb) (na % nn) (nb % nn)
/-- Given `a b : fin n`, proves either `(n, tt, p)` where `p : a < b` or
`(n, ff, p)` where `p : b ≤ a`. -/
meta def prove_lt_ge_fin : expr → expr → tactic (expr × bool × expr)
| a b := eval_rel a b $ λ n a' b' na nb,
if na < nb then prod.mk n <$> prod.mk tt <$> prove_lt_fin' n a b a' b'
else prod.mk n <$> prod.mk ff <$> prove_le_fin' n b a b' a'
/-- Given `a b : fin n`, proves either `(n, tt, p)` where `p : a = b` or
`(n, ff, p)` where `p : a ≠ b`. -/
meta def prove_eq_ne_fin : expr → expr → tactic (expr × bool × expr)
| a b := eval_rel a b $ λ n a' b' na nb,
if na = nb then prod.mk n <$> prod.mk tt <$> prove_eq_fin' n a b a' b'
else if na < nb then do
p ← prove_lt_fin' n a b a' b',
pure (n, ff, `(@ne_of_lt (fin %%n) _).mk_app [a, b, p])
else do
p ← prove_lt_fin' n b a b' a',
pure (n, ff, `(@ne_of_gt (fin %%n) _).mk_app [a, b, p])
/-- A `norm_num` extension that evaluates equalities and inequalities on the type `fin n`.
```
example : (5 : fin 7) = fin.succ (fin.succ 3) := by norm_num
```
-/
@[norm_num] meta def eval_ineq : expr → tactic (expr × expr)
| `(%%a < %%b) := do
(n, lt, p) ← prove_lt_ge_fin a b,
if lt then true_intro p else false_intro (`(@not_lt_of_ge (fin %%n) _).mk_app [a, b, p])
| `(%%a ≤ %%b) := do
(n, lt, p) ← prove_lt_ge_fin b a,
if lt then false_intro (`(@not_le_of_gt (fin %%n) _).mk_app [a, b, p]) else true_intro p
| `(%%a = %%b) := do
(n, eq, p) ← prove_eq_ne_fin a b,
if eq then true_intro p else false_intro p
| `(%%a > %%b) := mk_app ``has_lt.lt [b, a] >>= eval_ineq
| `(%%a ≥ %%b) := mk_app ``has_le.le [b, a] >>= eval_ineq
| `(%%a ≠ %%b) := do
(n, eq, p) ← prove_eq_ne_fin a b,
if eq then false_intro `(not_not_intro (%%p : (%%a : fin %%n) = %%b)) else true_intro p
| _ := failed
/-- Evaluates `e : fin n` to a natural number less than `n`. Returns `none` if it is not a natural
number or greater than `n`. -/
meta def as_numeral (n e : expr) : eval_fin_m (option ℕ) :=
match e.to_nat with
| none := pure none
| some ne := do
(nn, _) ← eval_fin_m.eval_n n,
pure $ if ne < nn then some ne else none
end
/-- Given `a : fin n`, returns `(b, ⊢ a = b)` where `b` is a normalized fin numeral. Fails if `a`
is already normalized. -/
meta def eval_fin_num (a : expr) : tactic (expr × expr) :=
do n ← get_fin_type a,
eval_fin_m.run $ do
as_numeral n a >>= (λ o, guardb o.is_none),
(a', pa) ← eval_fin a,
(a', pa) ← reduce_fin' ff n a (a', pa) <|> pure (a', pa),
(nm + 1, _) ← eval_fin_m.eval_n n | failure,
m' ← eval_fin_m.lift_ic (λ ic, ic.of_nat nm),
n' ← eval_fin_m.lift_ic (λ ic, ic.of_nat (nm+1)),
(b, pb) ← mk_fin_numeral n' m' a',
pure (b, `(@normalize_fin.eq).mk_app [n, a, b, a', pa, pb])
end norm_fin
namespace interactive
setup_tactic_parser
/-- Rewrites occurrences of fin expressions to normal form anywhere in the goal.
The `norm_num` extension will only rewrite fin expressions if they appear in equalities and
inequalities. For example if the goal is `P (2 + 2 : fin 3)` then `norm_num` will not do anything
but `norm_fin` will reduce the goal to `P 1`.
(The reason this is not part of `norm_num` is because evaluation of fin numerals uses a top down
evaluation strategy while `norm_num` works bottom up; also determining whether a normalization
will work is expensive, meaning that unrelated uses of `norm_num` would be slowed down with this
as a plugin.) -/
meta def norm_fin (hs : parse simp_arg_list) : tactic unit :=
try (simp_top_down tactic.norm_fin.eval_fin_num) >> try (norm_num hs (loc.ns [none]))
/--
Rewrites occurrences of fin expressions to normal form anywhere in the goal.
The `norm_num` extension will only rewrite fin expressions if they appear in equalities and
inequalities. For example if the goal is `P (2 + 2 : fin 3)` then `norm_num` will not do anything
but `norm_fin` will reduce the goal to `P 1`.
```lean
example : (5 : fin 7) = fin.succ (fin.succ 3) := by norm_num
example (P : fin 7 → Prop) (h : P 5) : P (fin.succ (fin.succ 3)) := by norm_fin; exact h
```
-/
add_tactic_doc
{ name := "norm_fin",
category := doc_category.tactic,
decl_names := [`tactic.interactive.norm_fin],
tags := ["arithmetic", "decision procedure"] }
end interactive
end tactic
|
84a082e8fb71202379148b1c2f96d37844cfc38a | 38bf3fd2bb651ab70511408fcf70e2029e2ba310 | /src/data/mv_polynomial.lean | 701c2f98ca12a11e03e525a9930badcdee05fc17 | [
"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 | 42,379 | 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, Johan Commelin, Mario Carneiro
Multivariate Polynomial
-/
import algebra.ring
import data.finsupp data.polynomial data.equiv.algebra
noncomputable theory
local attribute [instance, priority 100] classical.prop_decidable
open set function finsupp lattice
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x}
/-- Multivariate polynomial, where `σ` is the index set of the variables and
`α` is the coefficient ring -/
def mv_polynomial (σ : Type*) (α : Type*) [comm_semiring α] := (σ →₀ ℕ) →₀ α
namespace mv_polynomial
variables {σ : Type*} {a a' a₁ a₂ : α} {e : ℕ} {n m : σ} {s : σ →₀ ℕ}
section comm_semiring
variables [comm_semiring α] {p q : mv_polynomial σ α}
instance decidable_eq_mv_polynomial [decidable_eq σ] [decidable_eq α] : decidable_eq (mv_polynomial σ α) := finsupp.decidable_eq
instance : has_zero (mv_polynomial σ α) := finsupp.has_zero
instance : has_one (mv_polynomial σ α) := finsupp.has_one
instance : has_add (mv_polynomial σ α) := finsupp.has_add
instance : has_mul (mv_polynomial σ α) := finsupp.has_mul
instance : comm_semiring (mv_polynomial σ α) := finsupp.comm_semiring
/-- `monomial s a` is the monomial `a * X^s` -/
def monomial (s : σ →₀ ℕ) (a : α) : mv_polynomial σ α := single s a
/-- `C a` is the constant polynomial with value `a` -/
def C (a : α) : mv_polynomial σ α := monomial 0 a
/-- `X n` is the polynomial with value X_n -/
def X (n : σ) : mv_polynomial σ α := monomial (single n 1) 1
@[simp] lemma C_0 : C 0 = (0 : mv_polynomial σ α) := by simp [C, monomial]; refl
@[simp] lemma C_1 : C 1 = (1 : mv_polynomial σ α) := rfl
lemma C_mul_monomial : C a * monomial s a' = monomial s (a * a') :=
by simp [C, monomial, single_mul_single]
@[simp] lemma C_add : (C (a + a') : mv_polynomial σ α) = C a + C a' := single_add
@[simp] lemma C_mul : (C (a * a') : mv_polynomial σ α) = C a * C a' := C_mul_monomial.symm
@[simp] lemma C_pow (a : α) (n : ℕ) : (C (a^n) : mv_polynomial σ α) = (C a)^n :=
by induction n; simp [pow_succ, *]
instance : is_semiring_hom (C : α → mv_polynomial σ α) :=
{ map_zero := C_0,
map_one := C_1,
map_add := λ a a', C_add,
map_mul := λ a a', C_mul }
lemma C_eq_coe_nat (n : ℕ) : (C ↑n : mv_polynomial σ α) = n :=
by induction n; simp [nat.succ_eq_add_one, *]
lemma X_pow_eq_single : X n ^ e = monomial (single n e) (1 : α) :=
begin
induction e,
{ simp [X], refl },
{ simp [pow_succ, e_ih],
simp [X, monomial, single_mul_single, nat.succ_eq_add_one] }
end
lemma monomial_add_single : monomial (s + single n e) a = (monomial s a * X n ^ e) :=
by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp
lemma monomial_single_add : monomial (single n e + s) a = (X n ^ e * monomial s a) :=
by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp
lemma monomial_eq : monomial s a = C a * (s.prod $ λn e, X n ^ e : mv_polynomial σ α) :=
begin
apply @finsupp.induction σ ℕ _ _ s,
{ simp [C, prod_zero_index]; exact (mul_one _).symm },
{ assume n e s hns he ih,
simp [prod_add_index, prod_single_index, pow_zero, pow_add, (mul_assoc _ _ _).symm, ih.symm,
monomial_add_single] }
end
@[recursor 5]
lemma induction_on {M : mv_polynomial σ α → Prop} (p : mv_polynomial σ α)
(h_C : ∀a, M (C a)) (h_add : ∀p q, M p → M q → M (p + q)) (h_X : ∀p n, M p → M (p * X n)) :
M p :=
have ∀s a, M (monomial s a),
begin
assume s a,
apply @finsupp.induction σ ℕ _ _ s,
{ show M (monomial 0 a), from h_C a, },
{ assume n e p hpn he ih,
have : ∀e:ℕ, M (monomial p a * X n ^ e),
{ intro e,
induction e,
{ simp [ih] },
{ simp [ih, pow_succ', (mul_assoc _ _ _).symm, h_X, e_ih] } },
simp [monomial_add_single, this] }
end,
finsupp.induction p
(by have : M (C 0) := h_C 0; rwa [C_0] at this)
(assume s a p hsp ha hp, h_add _ _ (this s a) hp)
lemma hom_eq_hom [semiring γ]
(f g : mv_polynomial σ α → γ) (hf : is_semiring_hom f) (hg : is_semiring_hom g)
(hC : ∀a:α, f (C a) = g (C a)) (hX : ∀n:σ, f (X n) = g (X n)) (p : mv_polynomial σ α) :
f p = g p :=
mv_polynomial.induction_on p hC
begin assume p q hp hq, rw [is_semiring_hom.map_add f, is_semiring_hom.map_add g, hp, hq] end
begin assume p n hp, rw [is_semiring_hom.map_mul f, is_semiring_hom.map_mul g, hp, hX] end
lemma is_id (f : mv_polynomial σ α → mv_polynomial σ α) (hf : is_semiring_hom f)
(hC : ∀a:α, f (C a) = (C a)) (hX : ∀n:σ, f (X n) = (X n)) (p : mv_polynomial σ α) :
f p = p :=
hom_eq_hom f id hf is_semiring_hom.id hC hX p
section coeff
def tmp.coe : has_coe_to_fun (mv_polynomial σ α) := by delta mv_polynomial; apply_instance
local attribute [instance] tmp.coe
def coeff (m : σ →₀ ℕ) (p : mv_polynomial σ α) : α := p m
lemma ext (p q : mv_polynomial σ α) :
(∀ m, coeff m p = coeff m q) → p = q := ext
lemma ext_iff (p q : mv_polynomial σ α) :
(∀ m, coeff m p = coeff m q) ↔ p = q :=
⟨ext p q, λ h m, by rw h⟩
@[simp] lemma coeff_add (m : σ →₀ ℕ) (p q : mv_polynomial σ α) :
coeff m (p + q) = coeff m p + coeff m q := add_apply
@[simp] lemma coeff_zero (m : σ →₀ ℕ) :
coeff m (0 : mv_polynomial σ α) = 0 := rfl
@[simp] lemma coeff_zero_X (i : σ) : coeff 0 (X i : mv_polynomial σ α) = 0 :=
single_eq_of_ne (λ h, by cases single_eq_zero.1 h)
instance coeff.is_add_monoid_hom (m : σ →₀ ℕ) :
is_add_monoid_hom (coeff m : mv_polynomial σ α → α) :=
{ map_add := coeff_add m,
map_zero := coeff_zero m }
lemma coeff_sum {X : Type*} (s : finset X) (f : X → mv_polynomial σ α) (m : σ →₀ ℕ) :
coeff m (s.sum f) = s.sum (λ x, coeff m (f x)) :=
(finset.sum_hom _).symm
lemma monic_monomial_eq (m) : monomial m (1:α) = (m.prod $ λn e, X n ^ e : mv_polynomial σ α) :=
by simp [monomial_eq]
@[simp] lemma coeff_monomial (m n) (a) :
coeff m (monomial n a : mv_polynomial σ α) = if n = m then a else 0 :=
by convert single_apply
@[simp] lemma coeff_C (m) (a) :
coeff m (C a : mv_polynomial σ α) = if 0 = m then a else 0 :=
by convert single_apply
lemma coeff_X_pow (i : σ) (m) (k : ℕ) :
coeff m (X i ^ k : mv_polynomial σ α) = if single i k = m then 1 else 0 :=
begin
have := coeff_monomial m (finsupp.single i k) (1:α),
rwa [@monomial_eq _ _ (1:α) (finsupp.single i k) _,
C_1, one_mul, finsupp.prod_single_index] at this,
exact pow_zero _
end
lemma coeff_X' (i : σ) (m) :
coeff m (X i : mv_polynomial σ α) = if single i 1 = m then 1 else 0 :=
by rw [← coeff_X_pow, pow_one]
@[simp] lemma coeff_X (i : σ) :
coeff (single i 1) (X i : mv_polynomial σ α) = 1 :=
by rw [coeff_X', if_pos rfl]
@[simp] lemma coeff_C_mul (m) (a : α) (p : mv_polynomial σ α) : coeff m (C a * p) = a * coeff m p :=
begin
rw [mul_def, C, monomial],
simp only [sum_single_index, zero_mul, single_zero, zero_add, sum_zero],
convert sum_apply,
simp only [single_apply, finsupp.sum],
rw finset.sum_eq_single m,
{ rw if_pos rfl, refl },
{ intros m' hm' H, apply if_neg, exact H },
{ intros hm, rw if_pos rfl, rw not_mem_support_iff at hm, simp [hm] }
end
lemma coeff_mul (p q : mv_polynomial σ α) (n : σ →₀ ℕ) :
coeff n (p * q) = finset.sum (antidiagonal n).support (λ x, coeff x.1 p * coeff x.2 q) :=
begin
rw mul_def,
have := @finset.sum_sigma (σ →₀ ℕ) α _ _ p.support (λ _, q.support)
(λ x, if (x.1 + x.2 = n) then coeff x.1 p * coeff x.2 q else 0),
convert this.symm using 1; clear this,
{ rw [coeff],
repeat {rw sum_apply, apply finset.sum_congr rfl, intros, dsimp only},
convert single_apply },
{ have : (antidiagonal n).support.filter (λ x, x.1 ∈ p.support ∧ x.2 ∈ q.support) ⊆
(antidiagonal n).support := finset.filter_subset _,
rw [← finset.sum_sdiff this, finset.sum_eq_zero, zero_add], swap,
{ intros x hx,
rw [finset.mem_sdiff, not_iff_not_of_iff (finset.mem_filter),
not_and, not_and, not_mem_support_iff] at hx,
by_cases H : x.1 ∈ p.support,
{ rw [coeff, coeff, hx.2 hx.1 H, mul_zero] },
{ rw not_mem_support_iff at H, rw [coeff, H, zero_mul] } },
symmetry,
rw [← finset.sum_sdiff (finset.filter_subset _), finset.sum_eq_zero, zero_add], swap,
{ intros x hx,
rw [finset.mem_sdiff, not_iff_not_of_iff (finset.mem_filter), not_and] at hx,
rw if_neg,
exact hx.2 hx.1 },
{ apply finset.sum_bij, swap 5,
{ intros x hx, exact (x.1, x.2) },
{ intros x hx, rw [finset.mem_filter, finset.mem_sigma] at hx,
simpa [finset.mem_filter, mem_antidiagonal_support] using hx.symm },
{ intros x hx, rw finset.mem_filter at hx, rw if_pos hx.2 },
{ rintros ⟨i,j⟩ ⟨k,l⟩ hij hkl, simpa using and.intro },
{ rintros ⟨i,j⟩ hij, refine ⟨⟨i,j⟩, _, _⟩, { apply_instance },
{ rw [finset.mem_filter, mem_antidiagonal_support] at hij,
simpa [finset.mem_filter, finset.mem_sigma] using hij.symm },
{ refl } } },
all_goals { apply_instance } }
end
@[simp] lemma coeff_mul_X (m) (s : σ) (p : mv_polynomial σ α) :
coeff (m + single s 1) (p * X s) = coeff m p :=
begin
have : (m, single s 1) ∈ (m + single s 1).antidiagonal.support := mem_antidiagonal_support.2 rfl,
rw [coeff_mul, ← finset.insert_erase this, finset.sum_insert (finset.not_mem_erase _ _),
finset.sum_eq_zero, add_zero, coeff_X, mul_one],
rintros ⟨i,j⟩ hij,
rw [finset.mem_erase, mem_antidiagonal_support] at hij,
by_cases H : single s 1 = j,
{ subst j, simpa using hij },
{ rw [coeff_X', if_neg H, mul_zero] },
end
lemma coeff_mul_X' (m) (s : σ) (p : mv_polynomial σ α) :
coeff m (p * X s) = if s ∈ m.support then coeff (m - single s 1) p else 0 :=
begin
split_ifs with h h,
{ conv_rhs {rw ← coeff_mul_X _ s},
congr' 1, ext t,
by_cases hj : s = t,
{ subst t, simp only [nat_sub_apply, add_apply, single_eq_same],
refine (nat.sub_add_cancel $ nat.pos_of_ne_zero _).symm, rwa mem_support_iff at h },
{ simp [single_eq_of_ne hj] } },
{ delta coeff, rw ← not_mem_support_iff, intro hm, apply h,
have H := support_mul _ _ hm, simp only [finset.mem_bind] at H,
rcases H with ⟨j, hj, i', hi', H⟩,
delta X monomial at hi', rw mem_support_single at hi', cases hi', subst i',
erw finset.mem_singleton at H, subst m,
rw [mem_support_iff, add_apply, single_apply, if_pos rfl],
intro H, rw [add_eq_zero_iff] at H, exact one_ne_zero H.2 }
end
end coeff
section eval₂
variables [comm_semiring β]
variables (f : α → β) (g : σ → β)
/-- Evaluate a polynomial `p` given a valuation `g` of all the variables
and a ring hom `f` from the scalar ring to the target -/
def eval₂ (p : mv_polynomial σ α) : β :=
p.sum (λs a, f a * s.prod (λn e, g n ^ e))
@[simp] lemma eval₂_zero : (0 : mv_polynomial σ α).eval₂ f g = 0 :=
finsupp.sum_zero_index
variables [is_semiring_hom f]
@[simp] lemma eval₂_add : (p + q).eval₂ f g = p.eval₂ f g + q.eval₂ f g :=
finsupp.sum_add_index
(by simp [is_semiring_hom.map_zero f])
(by simp [add_mul, is_semiring_hom.map_add f])
@[simp] lemma eval₂_monomial : (monomial s a).eval₂ f g = f a * s.prod (λn e, g n ^ e) :=
finsupp.sum_single_index (by simp [is_semiring_hom.map_zero f])
@[simp] lemma eval₂_C (a) : (C a).eval₂ f g = f a :=
by simp [eval₂_monomial, C, prod_zero_index]
@[simp] lemma eval₂_one : (1 : mv_polynomial σ α).eval₂ f g = 1 :=
(eval₂_C _ _ _).trans (is_semiring_hom.map_one f)
@[simp] lemma eval₂_X (n) : (X n).eval₂ f g = g n :=
by simp [eval₂_monomial,
is_semiring_hom.map_one f, X, prod_single_index, pow_one]
lemma eval₂_mul_monomial :
∀{s a}, (p * monomial s a).eval₂ f g = p.eval₂ f g * f a * s.prod (λn e, g n ^ e) :=
begin
apply mv_polynomial.induction_on p,
{ assume a' s a,
simp [C_mul_monomial, eval₂_monomial, is_semiring_hom.map_mul f] },
{ assume p q ih_p ih_q, simp [add_mul, eval₂_add, ih_p, ih_q] },
{ assume p n ih s a,
from calc (p * X n * monomial s a).eval₂ f g = (p * monomial (single n 1 + s) a).eval₂ f g :
by simp [monomial_single_add, -add_comm, pow_one, mul_assoc]
... = (p * monomial (single n 1) 1).eval₂ f g * f a * s.prod (λn e, g n ^ e) :
by simp [ih, prod_single_index, prod_add_index, pow_one, pow_add, mul_assoc, mul_left_comm,
is_semiring_hom.map_one f, -add_comm] }
end
@[simp] lemma eval₂_mul : ∀{p}, (p * q).eval₂ f g = p.eval₂ f g * q.eval₂ f g :=
begin
apply mv_polynomial.induction_on q,
{ simp [C, eval₂_monomial, eval₂_mul_monomial, prod_zero_index] },
{ simp [mul_add, eval₂_add] {contextual := tt} },
{ simp [X, eval₂_monomial, eval₂_mul_monomial, (mul_assoc _ _ _).symm] { contextual := tt} }
end
@[simp] lemma eval₂_pow {p:mv_polynomial σ α} : ∀{n:ℕ}, (p ^ n).eval₂ f g = (p.eval₂ f g)^n
| 0 := eval₂_one _ _
| (n + 1) := by rw [pow_add, pow_one, pow_add, pow_one, eval₂_mul, eval₂_pow]
instance eval₂.is_semiring_hom : is_semiring_hom (eval₂ f g) :=
{ map_zero := eval₂_zero _ _,
map_one := eval₂_one _ _,
map_add := λ p q, eval₂_add _ _,
map_mul := λ p q, eval₂_mul _ _ }
lemma eval₂_comp_left {γ} [comm_semiring γ]
(k : β → γ) [is_semiring_hom k]
(f : α → β) [is_semiring_hom f] (g : σ → β)
(p) : k (eval₂ f g p) = eval₂ (k ∘ f) (k ∘ g) p :=
by apply mv_polynomial.induction_on p; simp [
eval₂_add, is_semiring_hom.map_add k,
eval₂_mul, is_semiring_hom.map_mul k] {contextual := tt}
@[simp] lemma eval₂_eta (p : mv_polynomial σ α) : eval₂ C X p = p :=
by apply mv_polynomial.induction_on p;
simp [eval₂_add, eval₂_mul] {contextual := tt}
lemma eval₂_congr (g₁ g₂ : σ → β)
(h : ∀ {i : σ} {c : σ →₀ ℕ}, i ∈ c.support → coeff c p ≠ 0 → g₁ i = g₂ i) :
p.eval₂ f g₁ = p.eval₂ f g₂ :=
begin
apply finset.sum_congr rfl,
intros c hc, dsimp, congr' 1,
apply finset.prod_congr rfl,
intros i hi, dsimp, congr' 1,
apply h hi,
rwa finsupp.mem_support_iff at hc
end
@[simp] lemma eval₂_prod (s : finset γ) (p : γ → mv_polynomial σ α) :
eval₂ f g (s.prod p) = s.prod (λ x, eval₂ f g $ p x) :=
(finset.prod_hom _).symm
@[simp] lemma eval₂_sum (s : finset γ) (p : γ → mv_polynomial σ α) :
eval₂ f g (s.sum p) = s.sum (λ x, eval₂ f g $ p x) :=
(finset.sum_hom _).symm
attribute [to_additive] eval₂_prod
lemma eval₂_assoc (q : γ → mv_polynomial σ α) (p : mv_polynomial γ α) :
eval₂ f (λ t, eval₂ f g (q t)) p = eval₂ f g (eval₂ C q p) :=
by { rw eval₂_comp_left (eval₂ f g), congr, funext, simp }
end eval₂
section eval
variables {f : σ → α}
/-- Evaluate a polynomial `p` given a valuation `f` of all the variables -/
def eval (f : σ → α) : mv_polynomial σ α → α := eval₂ id f
@[simp] lemma eval_zero : (0 : mv_polynomial σ α).eval f = 0 := eval₂_zero _ _
@[simp] lemma eval_add : (p + q).eval f = p.eval f + q.eval f := eval₂_add _ _
lemma eval_monomial : (monomial s a).eval f = a * s.prod (λn e, f n ^ e) :=
eval₂_monomial _ _
@[simp] lemma eval_C : ∀ a, (C a).eval f = a := eval₂_C _ _
@[simp] lemma eval_X : ∀ n, (X n).eval f = f n := eval₂_X _ _
@[simp] lemma eval_mul : (p * q).eval f = p.eval f * q.eval f := eval₂_mul _ _
instance eval.is_semiring_hom : is_semiring_hom (eval f) :=
eval₂.is_semiring_hom _ _
theorem eval_assoc {τ}
(f : σ → mv_polynomial τ α) (g : τ → α)
(p : mv_polynomial σ α) :
p.eval (eval g ∘ f) = (eval₂ C f p).eval g :=
begin
rw eval₂_comp_left (eval g),
unfold eval, congr; funext a; simp
end
end eval
section map
variables [comm_semiring β]
variables (f : α → β) [is_semiring_hom f]
/-- `map f p` maps a polynomial `p` across a ring hom `f` -/
def map : mv_polynomial σ α → mv_polynomial σ β := eval₂ (C ∘ f) X
@[simp] theorem map_monomial (s : σ →₀ ℕ) (a : α) : map f (monomial s a) = monomial s (f a) :=
(eval₂_monomial _ _).trans monomial_eq.symm
@[simp] theorem map_C : ∀ (a : α), map f (C a : mv_polynomial σ α) = C (f a) := map_monomial _ _
@[simp] theorem map_X : ∀ (n : σ), map f (X n : mv_polynomial σ α) = X n := eval₂_X _ _
@[simp] theorem map_one : map f (1 : mv_polynomial σ α) = 1 := eval₂_one _ _
@[simp] theorem map_add (p q : mv_polynomial σ α) :
map f (p + q) = map f p + map f q := eval₂_add _ _
@[simp] theorem map_mul (p q : mv_polynomial σ α) :
map f (p * q) = map f p * map f q := eval₂_mul _ _
@[simp] lemma map_pow (p : mv_polynomial σ α) (n : ℕ) :
map f (p^n) = (map f p)^n := eval₂_pow _ _
instance map.is_semiring_hom :
is_semiring_hom (map f : mv_polynomial σ α → mv_polynomial σ β) :=
eval₂.is_semiring_hom _ _
theorem map_id : ∀ (p : mv_polynomial σ α), map id p = p := eval₂_eta
theorem map_map [comm_semiring γ]
(g : β → γ) [is_semiring_hom g]
(p : mv_polynomial σ α) :
map g (map f p) = map (g ∘ f) p :=
(eval₂_comp_left (map g) (C ∘ f) X p).trans $
by congr; funext a; simp
theorem eval₂_eq_eval_map (g : σ → β) (p : mv_polynomial σ α) :
p.eval₂ f g = (map f p).eval g :=
begin
unfold map eval,
rw eval₂_comp_left (eval₂ id g),
congr; funext a; simp
end
lemma eval₂_comp_right {γ} [comm_semiring γ]
(k : β → γ) [is_semiring_hom k]
(f : α → β) [is_semiring_hom f] (g : σ → β)
(p) : k (eval₂ f g p) = eval₂ k (k ∘ g) (map f p) :=
begin
apply mv_polynomial.induction_on p,
{ intro r, rw [eval₂_C, map_C, eval₂_C] },
{ intros p q hp hq, rw [eval₂_add, is_semiring_hom.map_add k, map_add, eval₂_add, hp, hq] },
{ intros p s hp,
rw [eval₂_mul, is_semiring_hom.map_mul k, map_mul, eval₂_mul, map_X, hp, eval₂_X, eval₂_X] }
end
lemma map_eval₂ (f : α → β) [is_semiring_hom f] (g : γ → mv_polynomial δ α) (p : mv_polynomial γ α) :
map f (eval₂ C g p) = eval₂ C (map f ∘ g) (map f p) :=
begin
apply mv_polynomial.induction_on p,
{ intro r, rw [eval₂_C, map_C, map_C, eval₂_C] },
{ intros p q hp hq, rw [eval₂_add, map_add, hp, hq, map_add, eval₂_add] },
{ intros p s hp,
rw [eval₂_mul, map_mul, hp, map_mul, map_X, eval₂_mul, eval₂_X, eval₂_X] }
end
lemma coeff_map (p : mv_polynomial σ α) : ∀ (m : σ →₀ ℕ), coeff m (p.map f) = f (coeff m p) :=
begin
apply mv_polynomial.induction_on p; clear p,
{ intros r m, rw [map_C], simp only [coeff_C], split_ifs, {refl}, rw is_semiring_hom.map_zero f },
{ intros p q hp hq m, simp only [hp, hq, map_add, coeff_add], rw is_semiring_hom.map_add f },
{ intros p i hp m, simp only [hp, map_mul, map_X],
simp only [hp, mem_support_iff, coeff_mul_X'],
split_ifs, {refl},
rw is_semiring_hom.map_zero f }
end
lemma map_injective (hf : function.injective f) :
function.injective (map f : mv_polynomial σ α → mv_polynomial σ β) :=
λ p q h, ext _ _ $ λ m, hf $
begin
rw ← ext_iff at h,
specialize h m,
rw [coeff_map, coeff_map] at h,
exact h
end
end map
section degrees
section comm_semiring
def degrees (p : mv_polynomial σ α) : multiset σ :=
p.support.sup (λs:σ →₀ ℕ, s.to_multiset)
lemma degrees_monomial (s : σ →₀ ℕ) (a : α) : degrees (monomial s a) ≤ s.to_multiset :=
finset.sup_le $ assume t h,
begin
have := finsupp.support_single_subset h,
rw [finset.singleton_eq_singleton, finset.mem_singleton] at this,
rw this
end
lemma degrees_monomial_eq (s : σ →₀ ℕ) (a : α) (ha : a ≠ 0) :
degrees (monomial s a) = s.to_multiset :=
le_antisymm (degrees_monomial s a) $ finset.le_sup $
by rw [monomial, finsupp.support_single_ne_zero ha,
finset.singleton_eq_singleton, finset.mem_singleton]
lemma degrees_C (a : α) : degrees (C a : mv_polynomial σ α) = 0 :=
multiset.le_zero.1 $ degrees_monomial _ _
lemma degrees_X (n : σ) : degrees (X n : mv_polynomial σ α) ≤ {n} :=
le_trans (degrees_monomial _ _) $ le_of_eq $ to_multiset_single _ _
lemma degrees_zero : degrees (0 : mv_polynomial σ α) = 0 :=
by { rw ← C_0, exact degrees_C 0 }
lemma degrees_one : degrees (1 : mv_polynomial σ α) = 0 := degrees_C 1
lemma degrees_add (p q : mv_polynomial σ α) : (p + q).degrees ≤ p.degrees ⊔ q.degrees :=
begin
refine finset.sup_le (assume b hb, _),
have := finsupp.support_add hb, rw finset.mem_union at this,
cases this,
{ exact le_sup_left_of_le (finset.le_sup this) },
{ exact le_sup_right_of_le (finset.le_sup this) },
end
lemma degrees_sum {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ α) :
(s.sum f).degrees ≤ s.sup (λi, (f i).degrees) :=
begin
refine s.induction _ _,
{ simp only [finset.sum_empty, finset.sup_empty, degrees_zero], exact le_refl _ },
{ assume i s his ih,
rw [finset.sup_insert, finset.sum_insert his],
exact le_trans (degrees_add _ _) (sup_le_sup_left ih _) }
end
lemma degrees_mul (p q : mv_polynomial σ α) : (p * q).degrees ≤ p.degrees + q.degrees :=
begin
refine finset.sup_le (assume b hb, _),
have := support_mul p q hb,
simp only [finset.mem_bind, finset.singleton_eq_singleton, finset.mem_singleton] at this,
rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩,
rw [finsupp.to_multiset_add],
exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂)
end
lemma degrees_prod {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ α) :
(s.prod f).degrees ≤ s.sum (λi, (f i).degrees) :=
begin
refine s.induction _ _,
{ simp only [finset.prod_empty, finset.sum_empty, degrees_one] },
{ assume i s his ih,
rw [finset.prod_insert his, finset.sum_insert his],
exact le_trans (degrees_mul _ _) (add_le_add_left ih _) }
end
lemma degrees_pow (p : mv_polynomial σ α) :
∀(n : ℕ), (p^n).degrees ≤ add_monoid.smul n p.degrees
| 0 := begin rw [pow_zero, degrees_one], exact multiset.zero_le _ end
| (n + 1) := le_trans (degrees_mul _ _) (add_le_add_left (degrees_pow n) _)
end comm_semiring
end degrees
section vars
/-- `vars p` is the set of variables appearing in the polynomial `p` -/
def vars (p : mv_polynomial σ α) : finset σ := p.degrees.to_finset
@[simp] lemma vars_0 : (0 : mv_polynomial σ α).vars = ∅ :=
by rw [vars, degrees_zero, multiset.to_finset_zero]
@[simp] lemma vars_monomial (h : a ≠ 0) : (monomial s a).vars = s.support :=
by rw [vars, degrees_monomial_eq _ _ h, finsupp.to_finset_to_multiset]
@[simp] lemma vars_C : (C a : mv_polynomial σ α).vars = ∅ :=
by rw [vars, degrees_C, multiset.to_finset_zero]
@[simp] lemma vars_X (h : 0 ≠ (1 : α)) : (X n : mv_polynomial σ α).vars = {n} :=
by rw [X, vars_monomial h.symm, finsupp.support_single_ne_zero zero_ne_one.symm]
end vars
section degree_of
/-- `degree_of n p` gives the highest power of X_n that appears in `p` -/
def degree_of (n : σ) (p : mv_polynomial σ α) : ℕ := p.degrees.count n
end degree_of
section total_degree
/-- `total_degree p` gives the maximum |s| over the monomials X^s in `p` -/
def total_degree (p : mv_polynomial σ α) : ℕ := p.support.sup (λs, s.sum $ λn e, e)
lemma total_degree_eq (p : mv_polynomial σ α) :
p.total_degree = p.support.sup (λm, m.to_multiset.card) :=
begin
rw [total_degree],
congr, funext m,
exact (finsupp.card_to_multiset _).symm
end
lemma total_degree_le_degrees_card (p : mv_polynomial σ α) :
p.total_degree ≤ p.degrees.card :=
begin
rw [total_degree_eq],
exact finset.sup_le (assume s hs, multiset.card_le_of_le $ finset.le_sup hs)
end
lemma total_degree_C (a : α) : (C a : mv_polynomial σ α).total_degree = 0 :=
nat.eq_zero_of_le_zero $ finset.sup_le $ assume n hn,
have _ := finsupp.support_single_subset hn,
begin
rw [finset.singleton_eq_singleton, finset.mem_singleton] at this,
subst this,
exact le_refl _
end
lemma total_degree_zero : (0 : mv_polynomial σ α).total_degree = 0 :=
by rw [← C_0]; exact total_degree_C (0 : α)
lemma total_degree_one : (1 : mv_polynomial σ α).total_degree = 0 :=
total_degree_C (1 : α)
lemma total_degree_add (a b : mv_polynomial σ α) :
(a + b).total_degree ≤ max a.total_degree b.total_degree :=
finset.sup_le $ assume n hn,
have _ := finsupp.support_add hn,
begin
rw finset.mem_union at this,
cases this,
{ exact le_max_left_of_le (finset.le_sup this) },
{ exact le_max_right_of_le (finset.le_sup this) }
end
lemma total_degree_mul (a b : mv_polynomial σ α) :
(a * b).total_degree ≤ a.total_degree + b.total_degree :=
finset.sup_le $ assume n hn,
have _ := finsupp.support_mul a b hn,
begin
simp only [finset.mem_bind, finset.mem_singleton, finset.singleton_eq_singleton] at this,
rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩,
rw [finsupp.sum_add_index],
{ exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂) },
{ assume a, refl },
{ assume a b₁ b₂, refl }
end
lemma total_degree_list_prod :
∀(s : list (mv_polynomial σ α)), s.prod.total_degree ≤ (s.map mv_polynomial.total_degree).sum
| [] := by rw [@list.prod_nil (mv_polynomial σ α) _, total_degree_one]; refl
| (p :: ps) :=
begin
rw [@list.prod_cons (mv_polynomial σ α) _, list.map, list.sum_cons],
exact le_trans (total_degree_mul _ _) (add_le_add_left (total_degree_list_prod ps) _)
end
lemma total_degree_multiset_prod (s : multiset (mv_polynomial σ α)) :
s.prod.total_degree ≤ (s.map mv_polynomial.total_degree).sum :=
begin
refine quotient.induction_on s (assume l, _),
rw [multiset.quot_mk_to_coe, multiset.coe_prod, multiset.coe_map, multiset.coe_sum],
exact total_degree_list_prod l
end
lemma total_degree_finset_prod {ι : Type*}
(s : finset ι) (f : ι → mv_polynomial σ α) :
(s.prod f).total_degree ≤ s.sum (λi, (f i).total_degree) :=
begin
refine le_trans (total_degree_multiset_prod _) _,
rw [multiset.map_map],
refl
end
end total_degree
end comm_semiring
section comm_ring
variable [comm_ring α]
variables {p q : mv_polynomial σ α}
instance : ring (mv_polynomial σ α) := finsupp.ring
instance : comm_ring (mv_polynomial σ α) := finsupp.comm_ring
instance : has_scalar α (mv_polynomial σ α) := finsupp.has_scalar
instance : module α (mv_polynomial σ α) := finsupp.module _ α
instance C.is_ring_hom : is_ring_hom (C : α → mv_polynomial σ α) :=
by apply is_ring_hom.of_semiring
variables (σ a a')
lemma C_sub : (C (a - a') : mv_polynomial σ α) = C a - C a' := is_ring_hom.map_sub _
@[simp] lemma C_neg : (C (-a) : mv_polynomial σ α) = -C a := is_ring_hom.map_neg _
@[simp] lemma coeff_sub (m : σ →₀ ℕ) (p q : mv_polynomial σ α) :
coeff m (p - q) = coeff m p - coeff m q := finsupp.sub_apply
instance coeff.is_add_group_hom (m : σ →₀ ℕ) :
is_add_group_hom (coeff m : mv_polynomial σ α → α) :=
{ map_add := coeff_add m }
variables {σ} (p)
theorem C_mul' : mv_polynomial.C a * p = a • p :=
begin
apply finsupp.induction p,
{ exact (mul_zero $ mv_polynomial.C a).trans (@smul_zero α (mv_polynomial σ α) _ _ _ a).symm },
intros p b f haf hb0 ih,
rw [mul_add, ih, @smul_add α (mv_polynomial σ α) _ _ _ a], congr' 1,
rw [finsupp.mul_def, finsupp.smul_single, mv_polynomial.C, mv_polynomial.monomial],
rw [finsupp.sum_single_index, finsupp.sum_single_index, zero_add, smul_eq_mul],
{ rw [mul_zero, finsupp.single_zero] },
{ rw finsupp.sum_single_index,
all_goals { rw [zero_mul, finsupp.single_zero] } }
end
lemma smul_eq_C_mul (p : mv_polynomial σ α) (a : α) : a • p = C a * p :=
begin
rw [← finsupp.sum_single p, @finsupp.smul_sum (σ →₀ ℕ) α α, finsupp.mul_sum],
refine finset.sum_congr rfl (assume n _, _),
simp only [finsupp.smul_single],
exact C_mul_monomial.symm
end
@[simp] lemma smul_eval (x) (p : mv_polynomial σ α) (s) : (s • p).eval x = s * p.eval x :=
by rw [smul_eq_C_mul, eval_mul, eval_C]
section degrees
lemma degrees_neg (p : mv_polynomial σ α) : (- p).degrees = p.degrees :=
by rw [degrees, finsupp.support_neg]; refl
lemma degrees_sub (p q : mv_polynomial σ α) :
(p - q).degrees ≤ p.degrees ⊔ q.degrees :=
le_trans (degrees_add p (-q)) $ by rw [degrees_neg]
end degrees
section eval₂
variables [comm_ring β]
variables (f : α → β) [is_ring_hom f] (g : σ → β)
instance eval₂.is_ring_hom : is_ring_hom (eval₂ f g) :=
by apply is_ring_hom.of_semiring
lemma eval₂_sub : (p - q).eval₂ f g = p.eval₂ f g - q.eval₂ f g := is_ring_hom.map_sub _
@[simp] lemma eval₂_neg : (-p).eval₂ f g = -(p.eval₂ f g) := is_ring_hom.map_neg _
lemma hom_C (f : mv_polynomial σ ℤ → β) [is_ring_hom f] (n : ℤ) : f (C n) = (n : β) :=
congr_fun (int.eq_cast' (f ∘ C)) n
/-- A ring homomorphism f : Z[X_1, X_2, ...] → R
is determined by the evaluations f(X_1), f(X_2), ... -/
@[simp] lemma eval₂_hom_X {α : Type u} (c : ℤ → β) [is_ring_hom c]
(f : mv_polynomial α ℤ → β) [is_ring_hom f] (x : mv_polynomial α ℤ) :
eval₂ c (f ∘ X) x = f x :=
mv_polynomial.induction_on x
(λ n, by { rw [hom_C f, eval₂_C, int.eq_cast' c], refl })
(λ p q hp hq, by { rw [eval₂_add, hp, hq], exact (is_ring_hom.map_add f).symm })
(λ p n hp, by { rw [eval₂_mul, eval₂_X, hp], exact (is_ring_hom.map_mul f).symm })
/-- Ring homomorphisms out of integer polynomials on a type `σ` are the same as
functiosn out of the the type `σ`, -/
def hom_equiv : (mv_polynomial σ ℤ →+* β) ≃ (σ → β) :=
{ to_fun := λ f, ⇑f ∘ X,
inv_fun := λ f, ring_hom.of (eval₂ (λ n : ℤ, (n : β)) f),
left_inv := λ f, ring_hom.ext $ eval₂_hom_X _ _,
right_inv := λ f, funext $ λ x, by simp only [ring_hom.coe_of, function.comp_app, eval₂_X] }
end eval₂
section eval
variables (f : σ → α)
instance eval.is_ring_hom : is_ring_hom (eval f) := eval₂.is_ring_hom _ _
lemma eval_sub : (p - q).eval f = p.eval f - q.eval f := is_ring_hom.map_sub _
@[simp] lemma eval_neg : (-p).eval f = -(p.eval f) := is_ring_hom.map_neg _
end eval
section map
variables [comm_ring β]
variables (f : α → β) [is_ring_hom f]
instance map.is_ring_hom : is_ring_hom (map f : mv_polynomial σ α → mv_polynomial σ β) :=
eval₂.is_ring_hom _ _
lemma map_sub : (p - q).map f = p.map f - q.map f := is_ring_hom.map_sub _
@[simp] lemma map_neg : (-p).map f = -(p.map f) := is_ring_hom.map_neg _
end map
end comm_ring
section rename
variables {α} [comm_semiring α]
def rename (f : β → γ) : mv_polynomial β α → mv_polynomial γ α :=
eval₂ C (X ∘ f)
instance rename.is_semiring_hom (f : β → γ) :
is_semiring_hom (rename f : mv_polynomial β α → mv_polynomial γ α) :=
by unfold rename; apply_instance
@[simp] lemma rename_C (f : β → γ) (a : α) : rename f (C a) = C a :=
eval₂_C _ _ _
@[simp] lemma rename_X (f : β → γ) (b : β) : rename f (X b : mv_polynomial β α) = X (f b) :=
eval₂_X _ _ _
@[simp] lemma rename_zero (f : β → γ) :
rename f (0 : mv_polynomial β α) = 0 :=
eval₂_zero _ _
@[simp] lemma rename_one (f : β → γ) :
rename f (1 : mv_polynomial β α) = 1 :=
eval₂_one _ _
@[simp] lemma rename_add (f : β → γ) (p q : mv_polynomial β α) :
rename f (p + q) = rename f p + rename f q :=
eval₂_add _ _
@[simp] lemma rename_sub {α} [comm_ring α]
(f : β → γ) (p q : mv_polynomial β α) :
rename f (p - q) = rename f p - rename f q :=
eval₂_sub _ _ _
@[simp] lemma rename_mul (f : β → γ) (p q : mv_polynomial β α) :
rename f (p * q) = rename f p * rename f q :=
eval₂_mul _ _
@[simp] lemma rename_pow (f : β → γ) (p : mv_polynomial β α) (n : ℕ) :
rename f (p^n) = (rename f p)^n :=
eval₂_pow _ _
lemma map_rename [comm_semiring β] (f : α → β) [is_semiring_hom f]
(g : γ → δ) (p : mv_polynomial γ α) :
map f (rename g p) = rename g (map f p) :=
mv_polynomial.induction_on p
(λ a, by simp)
(λ p q hp hq, by simp [hp, hq])
(λ p n hp, by simp [hp])
@[simp] lemma rename_rename (f : β → γ) (g : γ → δ) (p : mv_polynomial β α) :
rename g (rename f p) = rename (g ∘ f) p :=
show rename g (eval₂ C (X ∘ f) p) = _,
by simp only [eval₂_comp_left (rename g) C (X ∘ f) p, (∘), rename_C, rename_X]; refl
@[simp] lemma rename_id (p : mv_polynomial β α) : rename id p = p :=
eval₂_eta p
lemma rename_monomial (f : β → γ) (p : β →₀ ℕ) (a : α) :
rename f (monomial p a) = monomial (p.map_domain f) a :=
begin
rw [rename, eval₂_monomial, monomial_eq, finsupp.prod_map_domain_index],
{ exact assume n, pow_zero _ },
{ exact assume n i₁ i₂, pow_add _ _ _ }
end
lemma rename_eq (f : β → γ) (p : mv_polynomial β α) :
rename f p = finsupp.map_domain (finsupp.map_domain f) p :=
begin
simp only [rename, eval₂, finsupp.map_domain],
congr, ext s a : 2,
rw [← monomial, monomial_eq, finsupp.prod_sum_index],
congr, ext n i : 2,
rw [finsupp.prod_single_index],
exact pow_zero _,
exact assume a, pow_zero _,
exact assume a b c, pow_add _ _ _
end
lemma injective_rename (f : β → γ) (hf : function.injective f) :
function.injective (rename f : mv_polynomial β α → mv_polynomial γ α) :=
have (rename f : mv_polynomial β α → mv_polynomial γ α) =
finsupp.map_domain (finsupp.map_domain f) := funext (rename_eq f),
begin
rw this,
exact finsupp.injective_map_domain (finsupp.injective_map_domain hf)
end
lemma total_degree_rename_le (f : β → γ) (p : mv_polynomial β α) :
(p.rename f).total_degree ≤ p.total_degree :=
finset.sup_le $ assume b,
begin
assume h,
rw rename_eq at h,
have h' := finsupp.map_domain_support h,
rw finset.mem_image at h',
rcases h' with ⟨s, hs, rfl⟩,
rw finsupp.sum_map_domain_index,
exact le_trans (le_refl _) (finset.le_sup hs),
exact assume _, rfl,
exact assume _ _ _, rfl
end
section
variables [comm_semiring β] (f : α → β) [is_semiring_hom f]
variables (k : γ → δ) (g : δ → β) (p : mv_polynomial γ α)
lemma eval₂_rename : (p.rename k).eval₂ f g = p.eval₂ f (g ∘ k) :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma rename_eval₂ (g : δ → mv_polynomial γ α) :
(p.eval₂ C (g ∘ k)).rename k = (p.rename k).eval₂ C (rename k ∘ g) :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma rename_prodmk_eval₂ (d : δ) (g : γ → mv_polynomial γ α) :
(p.eval₂ C g).rename (prod.mk d) = p.eval₂ C (λ x, (g x).rename (prod.mk d)) :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma eval₂_rename_prodmk (g : δ × γ → β) (d : δ) :
(rename (prod.mk d) p).eval₂ f g = eval₂ f (λ i, g (d, i)) p :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma eval_rename_prodmk (g : δ × γ → α) (d : δ) :
(rename (prod.mk d) p).eval g = eval (λ i, g (d, i)) p :=
eval₂_rename_prodmk id _ _ _
end
end rename
lemma eval₂_cast_comp {β : Type u} {γ : Type v} (f : γ → β)
{α : Type w} [comm_ring α] (c : ℤ → α) [is_ring_hom c] (g : β → α) (x : mv_polynomial γ ℤ) :
eval₂ c (g ∘ f) x = eval₂ c g (rename f x) :=
mv_polynomial.induction_on x
(λ n, by simp only [eval₂_C, rename_C])
(λ p q hp hq, by simp only [hp, hq, rename, eval₂_add])
(λ p n hp, by simp only [hp, rename, eval₂_X, eval₂_mul])
instance rename.is_ring_hom
{α} [comm_ring α] (f : β → γ) :
is_ring_hom (rename f : mv_polynomial β α → mv_polynomial γ α) :=
@is_ring_hom.of_semiring (mv_polynomial β α) (mv_polynomial γ α) _ _ (rename f)
(rename.is_semiring_hom f)
section equiv
variables (α) [comm_ring α]
set_option class.instance_max_depth 40
def pempty_ring_equiv : mv_polynomial pempty α ≃+* α :=
{ to_fun := mv_polynomial.eval₂ id $ pempty.elim,
inv_fun := C,
left_inv := is_id _ (by apply_instance) (assume a, by rw [eval₂_C]; refl) (assume a, a.elim),
right_inv := λ r, eval₂_C _ _ _,
map_mul' := λ _ _, eval₂_mul _ _,
map_add' := λ _ _, eval₂_add _ _ }
def punit_ring_equiv : mv_polynomial punit α ≃+* polynomial α :=
{ to_fun := eval₂ polynomial.C (λu:punit, polynomial.X),
inv_fun := polynomial.eval₂ mv_polynomial.C (X punit.star),
left_inv :=
begin
refine is_id _ _ _ _,
apply is_semiring_hom.comp (eval₂ polynomial.C (λu:punit, polynomial.X)) _; apply_instance,
{ assume a, rw [eval₂_C, polynomial.eval₂_C] },
{ rintros ⟨⟩, rw [eval₂_X, polynomial.eval₂_X] }
end,
right_inv := assume p, polynomial.induction_on p
(assume a, by rw [polynomial.eval₂_C, mv_polynomial.eval₂_C])
(assume p q hp hq, by rw [polynomial.eval₂_add, mv_polynomial.eval₂_add, hp, hq])
(assume p n hp,
by rw [polynomial.eval₂_mul, polynomial.eval₂_pow, polynomial.eval₂_X, polynomial.eval₂_C,
eval₂_mul, eval₂_C, eval₂_pow, eval₂_X]),
map_mul' := λ _ _, eval₂_mul _ _,
map_add' := λ _ _, eval₂_add _ _ }
def ring_equiv_of_equiv (e : β ≃ γ) : mv_polynomial β α ≃+* mv_polynomial γ α :=
{ to_fun := rename e,
inv_fun := rename e.symm,
left_inv := λ p, by simp only [rename_rename, (∘), e.symm_apply_apply]; exact rename_id p,
right_inv := λ p, by simp only [rename_rename, (∘), e.apply_symm_apply]; exact rename_id p,
map_mul' := rename_mul e,
map_add' := rename_add e }
def ring_equiv_congr [comm_ring γ] (e : α ≃+* γ) : mv_polynomial β α ≃+* mv_polynomial β γ :=
{ to_fun := map e,
inv_fun := map e.symm,
left_inv := assume p,
have (e.symm ∘ e) = id,
{ ext a, exact e.symm_apply_apply a },
by simp only [map_map, this, map_id],
right_inv := assume p,
have (e ∘ e.symm) = id,
{ ext a, exact e.apply_symm_apply a },
by simp only [map_map, this, map_id],
map_mul' := map_mul _,
map_add' := map_add _ }
section
variables (β γ δ)
instance ring_on_sum : ring (mv_polynomial (β ⊕ γ) α) := by apply_instance
instance ring_on_iter : ring (mv_polynomial β (mv_polynomial γ α)) := by apply_instance
def sum_to_iter : mv_polynomial (β ⊕ γ) α → mv_polynomial β (mv_polynomial γ α) :=
eval₂ (C ∘ C) (λbc, sum.rec_on bc X (C ∘ X))
instance is_semiring_hom_C_C :
is_semiring_hom (C ∘ C : α → mv_polynomial β (mv_polynomial γ α)) :=
@is_semiring_hom.comp _ _ _ _ C mv_polynomial.is_semiring_hom _ _ C mv_polynomial.is_semiring_hom
instance is_semiring_hom_sum_to_iter : is_semiring_hom (sum_to_iter α β γ) :=
eval₂.is_semiring_hom _ _
lemma sum_to_iter_C (a : α) : sum_to_iter α β γ (C a) = C (C a) :=
eval₂_C _ _ a
lemma sum_to_iter_Xl (b : β) : sum_to_iter α β γ (X (sum.inl b)) = X b :=
eval₂_X _ _ (sum.inl b)
lemma sum_to_iter_Xr (c : γ) : sum_to_iter α β γ (X (sum.inr c)) = C (X c) :=
eval₂_X _ _ (sum.inr c)
def iter_to_sum : mv_polynomial β (mv_polynomial γ α) → mv_polynomial (β ⊕ γ) α :=
eval₂ (eval₂ C (X ∘ sum.inr)) (X ∘ sum.inl)
section
instance is_semiring_hom_iter_to_sum : is_semiring_hom (iter_to_sum α β γ) :=
eval₂.is_semiring_hom _ _
end
lemma iter_to_sum_C_C (a : α) : iter_to_sum α β γ (C (C a)) = C a :=
eq.trans (eval₂_C _ _ (C a)) (eval₂_C _ _ _)
lemma iter_to_sum_X (b : β) : iter_to_sum α β γ (X b) = X (sum.inl b) :=
eval₂_X _ _ _
lemma iter_to_sum_C_X (c : γ) : iter_to_sum α β γ (C (X c)) = X (sum.inr c) :=
eq.trans (eval₂_C _ _ (X c)) (eval₂_X _ _ _)
def mv_polynomial_equiv_mv_polynomial [comm_ring δ]
(f : mv_polynomial β α → mv_polynomial γ δ) (hf : is_semiring_hom f)
(g : mv_polynomial γ δ → mv_polynomial β α) (hg : is_semiring_hom g)
(hfgC : ∀a, f (g (C a)) = C a)
(hfgX : ∀n, f (g (X n)) = X n)
(hgfC : ∀a, g (f (C a)) = C a)
(hgfX : ∀n, g (f (X n)) = X n) :
mv_polynomial β α ≃+* mv_polynomial γ δ :=
{ to_fun := f, inv_fun := g,
left_inv := is_id _ (is_semiring_hom.comp _ _) hgfC hgfX,
right_inv := is_id _ (is_semiring_hom.comp _ _) hfgC hfgX,
map_mul' := hf.map_mul,
map_add' := hf.map_add }
def sum_ring_equiv : mv_polynomial (β ⊕ γ) α ≃+* mv_polynomial β (mv_polynomial γ α) :=
begin
apply @mv_polynomial_equiv_mv_polynomial α (β ⊕ γ) _ _ _ _
(sum_to_iter α β γ) _ (iter_to_sum α β γ) _,
{ assume p,
apply hom_eq_hom _ _ _ _ _ _ p,
apply_instance,
{ apply @is_semiring_hom.comp _ _ _ _ _ _ _ _ _ _,
apply_instance,
apply @is_semiring_hom.comp _ _ _ _ _ _ _ _ _ _,
apply_instance,
{ apply @mv_polynomial.is_semiring_hom },
{ apply mv_polynomial.is_semiring_hom_iter_to_sum α β γ },
{ apply mv_polynomial.is_semiring_hom_sum_to_iter α β γ } },
{ apply mv_polynomial.is_semiring_hom },
{ assume a, rw [iter_to_sum_C_C α β γ, sum_to_iter_C α β γ] },
{ assume c, rw [iter_to_sum_C_X α β γ, sum_to_iter_Xr α β γ] } },
{ assume b, rw [iter_to_sum_X α β γ, sum_to_iter_Xl α β γ] },
{ assume a, rw [sum_to_iter_C α β γ, iter_to_sum_C_C α β γ] },
{ assume n, cases n with b c,
{ rw [sum_to_iter_Xl, iter_to_sum_X] },
{ rw [sum_to_iter_Xr, iter_to_sum_C_X] } },
{ apply mv_polynomial.is_semiring_hom_sum_to_iter α β γ },
{ apply mv_polynomial.is_semiring_hom_iter_to_sum α β γ }
end
instance option_ring : ring (mv_polynomial (option β) α) :=
mv_polynomial.ring
instance polynomial_ring : ring (polynomial (mv_polynomial β α)) :=
@comm_ring.to_ring _ polynomial.comm_ring
instance polynomial_ring2 : ring (mv_polynomial β (polynomial α)) :=
by apply_instance
def option_equiv_left : mv_polynomial (option β) α ≃+* polynomial (mv_polynomial β α) :=
(ring_equiv_of_equiv α $ (equiv.option_equiv_sum_punit β).trans (equiv.sum_comm _ _)).trans $
(sum_ring_equiv α _ _).trans $
punit_ring_equiv _
def option_equiv_right : mv_polynomial (option β) α ≃+* mv_polynomial β (polynomial α) :=
(ring_equiv_of_equiv α $ equiv.option_equiv_sum_punit.{0} β).trans $
(sum_ring_equiv α β unit).trans $
ring_equiv_congr (mv_polynomial unit α) (punit_ring_equiv α)
end
end equiv
end mv_polynomial
|
fb969629803a9ad789af0231ba448373c1b4c8b4 | 82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7 | /stage0/src/Init.lean | f60bed246fb2006d6062b4ca311e692500db9bd4 | [
"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 | 357 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Init.Core
import Init.Control
import Init.Data.Basic
import Init.WF
import Init.Data
import Init.System
import Init.Util
import Init.Fix
import Init.LeanInit
import Init.Tactics
|
56fa8ef755233c2a3b089531870c344b32804d8d | 97f752b44fd85ec3f635078a2dd125ddae7a82b6 | /hott/hit/trunc.hlean | ea7b3d345c20074fbbdd7436d0612ddb95b7636f | [
"Apache-2.0"
] | permissive | tectronics/lean | ab977ba6be0fcd46047ddbb3c8e16e7c26710701 | f38af35e0616f89c6e9d7e3eb1d48e47ee666efe | refs/heads/master | 1,532,358,526,384 | 1,456,276,623,000 | 1,456,276,623,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,358 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
n-truncation of types.
Ported from Coq HoTT
-/
/- The hit n-truncation is primitive, declared in init.hit. -/
import types.sigma types.pointed
open is_trunc eq equiv is_equiv function prod sum sigma
namespace trunc
protected definition elim {n : trunc_index} {A : Type} {P : Type}
[Pt : is_trunc n P] (H : A → P) : trunc n A → P :=
trunc.rec H
protected definition elim_on {n : trunc_index} {A : Type} {P : Type} (aa : trunc n A)
[Pt : is_trunc n P] (H : A → P) : P :=
trunc.elim H aa
end trunc
attribute trunc.elim_on [unfold 4]
attribute trunc.rec [recursor 6]
attribute trunc.elim [recursor 6] [unfold 6]
namespace trunc
variables {X Y Z : Type} {P : X → Type} (A B : Type) (n : trunc_index)
local attribute is_trunc_eq [instance]
variables {A n}
definition untrunc_of_is_trunc [reducible] [H : is_trunc n A] : trunc n A → A :=
trunc.rec id
variables (A n)
definition is_equiv_tr [instance] [constructor] [H : is_trunc n A] : is_equiv (@tr n A) :=
adjointify _
(untrunc_of_is_trunc)
(λaa, trunc.rec_on aa (λa, idp))
(λa, idp)
definition trunc_equiv [constructor] [H : is_trunc n A] : trunc n A ≃ A :=
(equiv.mk tr _)⁻¹ᵉ
definition is_trunc_of_is_equiv_tr [H : is_equiv (@tr n A)] : is_trunc n A :=
is_trunc_is_equiv_closed n (@tr n _)⁻¹
/- Functoriality -/
definition trunc_functor [unfold 5] (f : X → Y) : trunc n X → trunc n Y :=
λxx, trunc.rec_on xx (λx, tr (f x))
definition trunc_functor_compose (f : X → Y) (g : Y → Z)
: trunc_functor n (g ∘ f) ~ trunc_functor n g ∘ trunc_functor n f :=
λxx, trunc.rec_on xx (λx, idp)
definition trunc_functor_id : trunc_functor n (@id A) ~ id :=
λxx, trunc.rec_on xx (λx, idp)
definition is_equiv_trunc_functor [constructor] (f : X → Y) [H : is_equiv f]
: is_equiv (trunc_functor n f) :=
adjointify _
(trunc_functor n f⁻¹)
(λyy, trunc.rec_on yy (λy, ap tr !right_inv))
(λxx, trunc.rec_on xx (λx, ap tr !left_inv))
definition trunc_homotopy {f g : X → Y} (p : f ~ g) : trunc_functor n f ~ trunc_functor n g :=
λxx, trunc.rec_on xx (λx, ap tr (p x))
section
open equiv.ops
definition trunc_equiv_trunc [constructor] (f : X ≃ Y) : trunc n X ≃ trunc n Y :=
equiv.mk _ (is_equiv_trunc_functor n f)
end
section
open prod.ops
definition trunc_prod_equiv [constructor] : trunc n (X × Y) ≃ trunc n X × trunc n Y :=
begin
fapply equiv.MK,
{exact (λpp, trunc.rec_on pp (λp, (tr p.1, tr p.2)))},
{intro p, cases p with xx yy,
apply (trunc.rec_on xx), intro x,
apply (trunc.rec_on yy), intro y, exact (tr (x,y))},
{intro p, cases p with xx yy,
apply (trunc.rec_on xx), intro x,
apply (trunc.rec_on yy), intro y, apply idp},
{intro pp, apply (trunc.rec_on pp), intro p, cases p, apply idp}
end
end
/- Propositional truncation -/
-- should this live in Prop?
definition merely [reducible] [constructor] (A : Type) : Prop := trunctype.mk (trunc -1 A) _
notation `||`:max A `||`:0 := merely A
notation `∥`:max A `∥`:0 := merely A
definition Exists [reducible] [constructor] (P : X → Type) : Prop := ∥ sigma P ∥
definition or [reducible] [constructor] (A B : Type) : Prop := ∥ A ⊎ B ∥
notation `exists` binders `,` r:(scoped P, Exists P) := r
notation `∃` binders `,` r:(scoped P, Exists P) := r
notation A ` \/ ` B := or A B
notation A ∨ B := or A B
definition merely.intro [reducible] [constructor] (a : A) : ∥ A ∥ := tr a
definition exists.intro [reducible] [constructor] (x : X) (p : P x) : ∃x, P x := tr ⟨x, p⟩
definition or.intro_left [reducible] [constructor] (x : X) : X ∨ Y := tr (inl x)
definition or.intro_right [reducible] [constructor] (y : Y) : X ∨ Y := tr (inr y)
definition is_contr_of_merely_prop [H : is_prop A] (aa : merely A) : is_contr A :=
is_contr_of_inhabited_prop (trunc.rec_on aa id)
section
open sigma.ops
definition trunc_sigma_equiv [constructor] : trunc n (Σ x, P x) ≃ trunc n (Σ x, trunc n (P x)) :=
equiv.MK (λpp, trunc.rec_on pp (λp, tr ⟨p.1, tr p.2⟩))
(λpp, trunc.rec_on pp (λp, trunc.rec_on p.2 (λb, tr ⟨p.1, b⟩)))
(λpp, trunc.rec_on pp (λp, sigma.rec_on p (λa bb, trunc.rec_on bb (λb, by esimp))))
(λpp, trunc.rec_on pp (λp, sigma.rec_on p (λa b, by esimp)))
definition trunc_sigma_equiv_of_is_trunc [H : is_trunc n X]
: trunc n (Σ x, P x) ≃ Σ x, trunc n (P x) :=
calc
trunc n (Σ x, P x) ≃ trunc n (Σ x, trunc n (P x)) : trunc_sigma_equiv
... ≃ Σ x, trunc n (P x) : !trunc_equiv
end
/- the (non-dependent) universal property -/
definition trunc_arrow_equiv [constructor] [H : is_trunc n B] :
(trunc n A → B) ≃ (A → B) :=
begin
fapply equiv.MK,
{ intro g a, exact g (tr a)},
{ intro f x, exact trunc.rec_on x f},
{ intro f, apply eq_of_homotopy, intro a, reflexivity},
{ intro g, apply eq_of_homotopy, intro x, exact trunc.rec_on x (λa, idp)},
end
end trunc
|
dd9129a4c2830c187cd0fd529ef7eed99eb2e7c4 | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/topology/partition_of_unity.lean | 111cbe44a922b72a3598f5857ef4ae427620a6ed | [
"Apache-2.0"
] | permissive | waynemunro/mathlib | e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552 | 065a70810b5480d584033f7bbf8e0409480c2118 | refs/heads/master | 1,693,417,182,397 | 1,634,644,781,000 | 1,634,644,781,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 20,366 | lean | /-
Copyright (c) 2021 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import algebra.big_operators.finprod
import topology.urysohns_lemma
import topology.paracompact
import topology.shrinking_lemma
import topology.continuous_function.algebra
import set_theory.ordinal
/-!
# Continuous partition of unity
In this file we define `partition_of_unity (ι X : Type*) [topological_space X] (s : set X := univ)`
to be a continuous partition of unity on `s` indexed by `ι`. More precisely, `f : partition_of_unity
ι X s` is a collection of continuous functions `f i : C(X, ℝ)`, `i : ι`, such that
* the supports of `f i` form a locally finite family of sets;
* each `f i` is nonnegative;
* `∑ᶠ i, f i x = 1` for all `x ∈ s`;
* `∑ᶠ i, f i x ≤ 1` for all `x : X`.
In the case `s = univ` the last assumption follows from the previous one but it is convenient to
have this assumption in the case `s ≠ univ`.
We also define a bump function covering,
`bump_covering (ι X : Type*) [topological_space X] (s : set X := univ)`, to be a collection of
functions `f i : C(X, ℝ)`, `i : ι`, such that
* the supports of `f i` form a locally finite family of sets;
* each `f i` is nonnegative;
* for each `x ∈ s` there exists `i : ι` such that `f i y = 1` in a neighborhood of `x`.
The term is motivated by the smooth case.
If `f` is a bump function covering indexed by a linearly ordered type, then
`g i x = f i x * ∏ᶠ j < i, (1 - f j x)` is a partition of unity, see
`bump_covering.to_partition_of_unity`. Note that only finitely many terms `1 - f j x` are not equal
to one, so this product is well-defined.
Note that `g i x = ∏ᶠ j ≤ i, (1 - f j x) - ∏ᶠ j < i, (1 - f j x)`, so most terms in the sum
`∑ᶠ i, g i x` cancel, and we get `∑ᶠ i, g i x = 1 - ∏ᶠ i, (1 - f i x)`, and the latter product
equals zero because one of `f i x` is equal to one.
We say that a partition of unity or a bump function covering `f` is *subordinate* to a family of
sets `U i`, `i : ι`, if the closure of the support of each `f i` is included in `U i`. We use
Urysohn's Lemma to prove that a locally finite open covering of a normal topological space admits a
subordinate bump function covering (hence, a subordinate partition of unity), see
`bump_covering.exists_is_subordinate_of_locally_finite`. If `X` is a paracompact space, then any
open covering admits a locally finite refinement, hence it admits a subordinate bump function
covering and a subordinate partition of unity, see `bump_covering.exists_is_subordinate`.
We also provide two slightly more general versions of these lemmas,
`bump_covering.exists_is_subordinate_of_locally_finite_of_prop` and
`bump_covering.exists_is_subordinate_of_prop`, to be used later in the construction of a smooth
partition of unity.
## Implementation notes
Most (if not all) books only define a partition of unity of the whole space. However, quite a few
proofs only deal with `f i` such that `closure (support (f i))` meets a specific closed subset, and
it is easier to formalize these proofs if we don't have other functions right away.
We use `well_ordering_rel j i` instead of `j < i` in the definition of
`bump_covering.to_partition_of_unity` to avoid a `[linear_order ι]` assumption. While
`well_ordering_rel j i` is a well order, not only a strict linear order, we never use this property.
## Tags
partition of unity, bump function, Urysohn's lemma, normal space, paracompact space
-/
universes u v
open function set filter
open_locale big_operators topological_space classical
noncomputable theory
/-- A continuous partition of unity on a set `s : set X` is a collection of continuous functions
`f i` such that
* the supports of `f i` form a locally finite family of sets, i.e., for every point `x : X` there
exists a neighborhood `U ∋ x` such that all but finitely many functions `f i` are zero on `U`;
* the functions `f i` are nonnegative;
* the sum `∑ᶠ i, f i x` is equal to one for every `x ∈ s` and is less than or equal to one
otherwise.
If `X` is a normal paracompact space, then `partition_of_unity.exists_is_subordinate` guarantees
that for every open covering `U : set (set X)` of `s` there exists a partition of unity that is
subordinate to `U`.
-/
structure partition_of_unity (ι X : Type*) [topological_space X] (s : set X := univ) :=
(to_fun : ι → C(X, ℝ))
(locally_finite' : locally_finite (λ i, support (to_fun i)))
(nonneg' : 0 ≤ to_fun)
(sum_eq_one' : ∀ x ∈ s, ∑ᶠ i, to_fun i x = 1)
(sum_le_one' : ∀ x, ∑ᶠ i, to_fun i x ≤ 1)
/-- A `bump_covering ι X s` is an indexed family of functions `f i`, `i : ι`, such that
* the supports of `f i` form a locally finite family of sets, i.e., for every point `x : X` there
exists a neighborhood `U ∋ x` such that all but finitely many functions `f i` are zero on `U`;
* for all `i`, `x` we have `0 ≤ f i x ≤ 1`;
* each point `x ∈ s` belongs to the interior of `{x | f i x = 1}` for some `i`.
One of the main use cases for a `bump_covering` is to define a `partition_of_unity`, see
`bump_covering.to_partition_of_unity`, but some proofs can directly use a `bump_covering` instead of
a `partition_of_unity`.
If `X` is a normal paracompact space, then `bump_covering.exists_is_subordinate` guarantees that for
every open covering `U : set (set X)` of `s` there exists a `bump_covering` of `s` that is
subordinate to `U`.
-/
structure bump_covering (ι X : Type*) [topological_space X] (s : set X := univ) :=
(to_fun : ι → C(X, ℝ))
(locally_finite' : locally_finite (λ i, support (to_fun i)))
(nonneg' : 0 ≤ to_fun)
(le_one' : to_fun ≤ 1)
(eventually_eq_one' : ∀ x ∈ s, ∃ i, to_fun i =ᶠ[𝓝 x] 1)
variables {ι : Type u} {X : Type v} [topological_space X]
namespace partition_of_unity
variables {s : set X} (f : partition_of_unity ι X s)
instance : has_coe_to_fun (partition_of_unity ι X s) := ⟨_, to_fun⟩
protected lemma locally_finite : locally_finite (λ i, support (f i)) :=
f.locally_finite'
lemma nonneg (i : ι) (x : X) : 0 ≤ f i x := f.nonneg' i x
lemma sum_eq_one {x : X} (hx : x ∈ s) : ∑ᶠ i, f i x = 1 := f.sum_eq_one' x hx
lemma sum_le_one (x : X) : ∑ᶠ i, f i x ≤ 1 := f.sum_le_one' x
lemma sum_nonneg (x : X) : 0 ≤ ∑ᶠ i, f i x := finsum_nonneg $ λ i, f.nonneg i x
lemma le_one (i : ι) (x : X) : f i x ≤ 1 :=
(single_le_finsum i (f.locally_finite.point_finite x) (λ j, f.nonneg j x)).trans (f.sum_le_one x)
/-- A partition of unity `f i` is subordinate to a family of sets `U i` indexed by the same type if
for each `i` the closure of the support of `f i` is a subset of `U i`. -/
def is_subordinate (f : partition_of_unity ι X s) (U : ι → set X) : Prop :=
∀ i, closure (support (f i)) ⊆ U i
end partition_of_unity
namespace bump_covering
variables {s : set X} (f : bump_covering ι X s)
instance : has_coe_to_fun (bump_covering ι X s) := ⟨_, to_fun⟩
protected lemma locally_finite : locally_finite (λ i, support (f i)) :=
f.locally_finite'
protected lemma point_finite (x : X) : finite {i | f i x ≠ 0} :=
f.locally_finite.point_finite x
lemma nonneg (i : ι) (x : X) : 0 ≤ f i x := f.nonneg' i x
lemma le_one (i : ι) (x : X) : f i x ≤ 1 := f.le_one' i x
/-- A `bump_covering` that consists of a single function, uniformly equal to one, defined as an
example for `inhabited` instance. -/
protected def single (i : ι) (s : set X) : bump_covering ι X s :=
{ to_fun := pi.single i 1,
locally_finite' := λ x,
begin
refine ⟨univ, univ_mem, (finite_singleton i).subset _⟩,
rintro j ⟨x, hx, -⟩,
contrapose! hx,
rw [mem_singleton_iff] at hx,
simp [hx]
end,
nonneg' := le_update_iff.2 ⟨λ x, zero_le_one, λ _ _, le_rfl⟩,
le_one' := update_le_iff.2 ⟨le_rfl, λ _ _ _, zero_le_one⟩,
eventually_eq_one' := λ x _, ⟨i, by simp⟩ }
@[simp] lemma coe_single (i : ι) (s : set X) : ⇑(bump_covering.single i s) = pi.single i 1 := rfl
instance [inhabited ι] : inhabited (bump_covering ι X s) :=
⟨bump_covering.single (default ι) s⟩
/-- A collection of bump functions `f i` is subordinate to a family of sets `U i` indexed by the
same type if for each `i` the closure of the support of `f i` is a subset of `U i`. -/
def is_subordinate (f : bump_covering ι X s) (U : ι → set X) : Prop :=
∀ i, closure (support (f i)) ⊆ U i
lemma is_subordinate.mono {f : bump_covering ι X s} {U V : ι → set X} (hU : f.is_subordinate U)
(hV : ∀ i, U i ⊆ V i) :
f.is_subordinate V :=
λ i, subset.trans (hU i) (hV i)
/-- If `X` is a normal topological space and `U i`, `i : ι`, is a locally finite open covering of a
closed set `s`, then there exists a `bump_covering ι X s` that is subordinate to `U`. If `X` is a
paracompact space, then the assumption `hf : locally_finite U` can be omitted, see
`bump_covering.exists_is_subordinate`. This version assumes that `p : (X → ℝ) → Prop` is a predicate
that satisfies Urysohn's lemma, and provides a `bump_covering` such that each function of the
covering satisfies `p`. -/
lemma exists_is_subordinate_of_locally_finite_of_prop [normal_space X] (p : (X → ℝ) → Prop)
(h01 : ∀ s t, is_closed s → is_closed t → disjoint s t →
∃ f : C(X, ℝ), p f ∧ eq_on f 0 s ∧ eq_on f 1 t ∧ ∀ x, f x ∈ Icc (0 : ℝ) 1)
(hs : is_closed s) (U : ι → set X) (ho : ∀ i, is_open (U i)) (hf : locally_finite U)
(hU : s ⊆ ⋃ i, U i) :
∃ f : bump_covering ι X s, (∀ i, p (f i)) ∧ f.is_subordinate U :=
begin
rcases exists_subset_Union_closure_subset hs ho (λ x _, hf.point_finite x) hU
with ⟨V, hsV, hVo, hVU⟩,
have hVU' : ∀ i, V i ⊆ U i, from λ i, subset.trans subset_closure (hVU i),
rcases exists_subset_Union_closure_subset hs hVo
(λ x _, (hf.subset hVU').point_finite x) hsV with ⟨W, hsW, hWo, hWV⟩,
choose f hfp hf0 hf1 hf01
using λ i, h01 _ _ (is_closed_compl_iff.2 $ hVo i)
is_closed_closure (disjoint_right.2 $ λ x hx, not_not.2 (hWV i hx)),
have hsupp : ∀ i, support (f i) ⊆ V i,
from λ i, support_subset_iff'.2 (hf0 i),
refine ⟨⟨f, hf.subset (λ i, subset.trans (hsupp i) (hVU' i)),
λ i x, (hf01 i x).1, λ i x, (hf01 i x).2, λ x hx, _⟩, hfp,
λ i, subset.trans (closure_mono (hsupp i)) (hVU i)⟩,
rcases mem_Union.1 (hsW hx) with ⟨i, hi⟩,
exact ⟨i, ((hf1 i).mono subset_closure).eventually_eq_of_mem ((hWo i).mem_nhds hi)⟩
end
/-- If `X` is a normal topological space and `U i`, `i : ι`, is a locally finite open covering of a
closed set `s`, then there exists a `bump_covering ι X s` that is subordinate to `U`. If `X` is a
paracompact space, then the assumption `hf : locally_finite U` can be omitted, see
`bump_covering.exists_is_subordinate`. -/
lemma exists_is_subordinate_of_locally_finite [normal_space X] (hs : is_closed s)
(U : ι → set X) (ho : ∀ i, is_open (U i)) (hf : locally_finite U)
(hU : s ⊆ ⋃ i, U i) :
∃ f : bump_covering ι X s, f.is_subordinate U :=
let ⟨f, _, hfU⟩ :=
exists_is_subordinate_of_locally_finite_of_prop (λ _, true)
(λ s t hs ht hd, (exists_continuous_zero_one_of_closed hs ht hd).imp $ λ f hf, ⟨trivial, hf⟩)
hs U ho hf hU
in ⟨f, hfU⟩
/-- If `X` is a paracompact normal topological space and `U` is an open covering of a closed set
`s`, then there exists a `bump_covering ι X s` that is subordinate to `U`. This version assumes that
`p : (X → ℝ) → Prop` is a predicate that satisfies Urysohn's lemma, and provides a
`bump_covering` such that each function of the covering satisfies `p`. -/
lemma exists_is_subordinate_of_prop [normal_space X] [paracompact_space X] (p : (X → ℝ) → Prop)
(h01 : ∀ s t, is_closed s → is_closed t → disjoint s t →
∃ f : C(X, ℝ), p f ∧ eq_on f 0 s ∧ eq_on f 1 t ∧ ∀ x, f x ∈ Icc (0 : ℝ) 1)
(hs : is_closed s) (U : ι → set X) (ho : ∀ i, is_open (U i)) (hU : s ⊆ ⋃ i, U i) :
∃ f : bump_covering ι X s, (∀ i, p (f i)) ∧ f.is_subordinate U :=
begin
rcases precise_refinement_set hs _ ho hU with ⟨V, hVo, hsV, hVf, hVU⟩,
rcases exists_is_subordinate_of_locally_finite_of_prop p h01 hs V hVo hVf hsV with ⟨f, hfp, hf⟩,
exact ⟨f, hfp, hf.mono hVU⟩
end
/-- If `X` is a paracompact normal topological space and `U` is an open covering of a closed set
`s`, then there exists a `bump_covering ι X s` that is subordinate to `U`. -/
lemma exists_is_subordinate [normal_space X] [paracompact_space X]
(hs : is_closed s) (U : ι → set X) (ho : ∀ i, is_open (U i)) (hU : s ⊆ ⋃ i, U i) :
∃ f : bump_covering ι X s, f.is_subordinate U :=
begin
rcases precise_refinement_set hs _ ho hU with ⟨V, hVo, hsV, hVf, hVU⟩,
rcases exists_is_subordinate_of_locally_finite hs V hVo hVf hsV with ⟨f, hf⟩,
exact ⟨f, hf.mono hVU⟩
end
/-- Index of a bump function such that `fs i =ᶠ[𝓝 x] 1`. -/
def ind (x : X) (hx : x ∈ s) : ι := (f.eventually_eq_one' x hx).some
lemma eventually_eq_one (x : X) (hx : x ∈ s) : f (f.ind x hx) =ᶠ[𝓝 x] 1 :=
(f.eventually_eq_one' x hx).some_spec
lemma ind_apply (x : X) (hx : x ∈ s) : f (f.ind x hx) x = 1 :=
(f.eventually_eq_one x hx).eq_of_nhds
/-- Partition of unity defined by a `bump_covering`. We use this auxiliary definition to prove some
properties of the new family of functions before bundling it into a `partition_of_unity`. Do not use
this definition, use `bump_function.to_partition_of_unity` instead.
The partition of unity is given by the formula `g i x = f i x * ∏ᶠ j < i, (1 - f j x)`. In other
words, `g i x = ∏ᶠ j < i, (1 - f j x) - ∏ᶠ j ≤ i, (1 - f j x)`, so
`∑ᶠ i, g i x = 1 - ∏ᶠ j, (1 - f j x)`. If `x ∈ s`, then one of `f j x` equals one, hence the product
of `1 - f j x` vanishes, and `∑ᶠ i, g i x = 1`.
In order to avoid an assumption `linear_order ι`, we use `well_ordering_rel` instead of `(<)`. -/
def to_pou_fun (i : ι) (x : X) : ℝ :=
f i x * ∏ᶠ j (hj : well_ordering_rel j i), (1 - f j x)
lemma to_pou_fun_zero_of_zero {i : ι} {x : X} (h : f i x = 0) :
f.to_pou_fun i x = 0 :=
by rw [to_pou_fun, h, zero_mul]
lemma support_to_pou_fun_subset (i : ι) :
support (f.to_pou_fun i) ⊆ support (f i) :=
λ x, mt $ f.to_pou_fun_zero_of_zero
lemma to_pou_fun_eq_mul_prod (i : ι) (x : X) (t : finset ι)
(ht : ∀ j, well_ordering_rel j i → f j x ≠ 0 → j ∈ t) :
f.to_pou_fun i x = f i x * ∏ j in t.filter (λ j, well_ordering_rel j i), (1 - f j x) :=
begin
refine congr_arg _ (finprod_cond_eq_prod_of_cond_iff _ (λ j hj, _)),
rw [ne.def, sub_eq_self] at hj,
rw [finset.mem_filter, iff.comm, and_iff_right_iff_imp],
exact flip (ht j) hj
end
lemma sum_to_pou_fun_eq (x : X) :
∑ᶠ i, f.to_pou_fun i x = 1 - ∏ᶠ i, (1 - f i x) :=
begin
set s := (f.point_finite x).to_finset,
have hs : (s : set ι) = {i | f i x ≠ 0} := finite.coe_to_finset _,
have A : support (λ i, to_pou_fun f i x) ⊆ s,
{ rw hs,
exact λ i hi, f.support_to_pou_fun_subset i hi },
have B : mul_support (λ i, 1 - f i x) ⊆ s,
{ rw [hs, mul_support_one_sub], exact λ i, id },
letI : linear_order ι := linear_order_of_STO' well_ordering_rel,
rw [finsum_eq_sum_of_support_subset _ A, finprod_eq_prod_of_mul_support_subset _ B,
finset.prod_one_sub_ordered, sub_sub_cancel],
refine finset.sum_congr rfl (λ i hi, _),
convert f.to_pou_fun_eq_mul_prod _ _ _ (λ j hji hj, _),
rwa finite.mem_to_finset
end
lemma exists_finset_to_pou_fun_eventually_eq (i : ι) (x : X) :
∃ t : finset ι, f.to_pou_fun i =ᶠ[𝓝 x]
f i * ∏ j in t.filter (λ j, well_ordering_rel j i), (1 - f j) :=
begin
rcases f.locally_finite x with ⟨U, hU, hf⟩,
use hf.to_finset,
filter_upwards [hU],
intros y hyU,
simp only [pi.mul_apply, finset.prod_apply],
apply to_pou_fun_eq_mul_prod,
intros j hji hj,
exact hf.mem_to_finset.2 ⟨y, ⟨hj, hyU⟩⟩
end
lemma continuous_to_pou_fun (i : ι) : continuous (f.to_pou_fun i) :=
begin
refine ((f i).continuous.mul $
continuous_finprod_cond (λ j _, continuous_const.sub (f j).continuous) _),
simp only [mul_support_one_sub],
exact f.locally_finite
end
/-- The partition of unity defined by a `bump_covering`.
The partition of unity is given by the formula `g i x = f i x * ∏ᶠ j < i, (1 - f j x)`. In other
words, `g i x = ∏ᶠ j < i, (1 - f j x) - ∏ᶠ j ≤ i, (1 - f j x)`, so
`∑ᶠ i, g i x = 1 - ∏ᶠ j, (1 - f j x)`. If `x ∈ s`, then one of `f j x` equals one, hence the product
of `1 - f j x` vanishes, and `∑ᶠ i, g i x = 1`.
In order to avoid an assumption `linear_order ι`, we use `well_ordering_rel` instead of `(<)`. -/
def to_partition_of_unity : partition_of_unity ι X s :=
{ to_fun := λ i, ⟨f.to_pou_fun i, f.continuous_to_pou_fun i⟩,
locally_finite' := f.locally_finite.subset f.support_to_pou_fun_subset,
nonneg' := λ i x, mul_nonneg (f.nonneg i x)
(finprod_cond_nonneg $ λ j hj, sub_nonneg.2 $ f.le_one j x),
sum_eq_one' := λ x hx,
begin
simp only [continuous_map.coe_mk, sum_to_pou_fun_eq, sub_eq_self],
apply finprod_eq_zero (λ i, 1 - f i x) (f.ind x hx),
{ simp only [f.ind_apply x hx, sub_self] },
{ rw mul_support_one_sub, exact f.point_finite x }
end,
sum_le_one' := λ x,
begin
simp only [continuous_map.coe_mk, sum_to_pou_fun_eq, sub_le_self_iff],
exact finprod_nonneg (λ i, sub_nonneg.2 $ f.le_one i x)
end }
lemma to_partition_of_unity_apply (i : ι) (x : X) :
f.to_partition_of_unity i x = f i x * ∏ᶠ j (hj : well_ordering_rel j i), (1 - f j x) :=
rfl
lemma to_partition_of_unity_eq_mul_prod (i : ι) (x : X) (t : finset ι)
(ht : ∀ j, well_ordering_rel j i → f j x ≠ 0 → j ∈ t) :
f.to_partition_of_unity i x = f i x * ∏ j in t.filter (λ j, well_ordering_rel j i), (1 - f j x) :=
f.to_pou_fun_eq_mul_prod i x t ht
lemma exists_finset_to_partition_of_unity_eventually_eq (i : ι) (x : X) :
∃ t : finset ι, f.to_partition_of_unity i =ᶠ[𝓝 x]
f i * ∏ j in t.filter (λ j, well_ordering_rel j i), (1 - f j) :=
f.exists_finset_to_pou_fun_eventually_eq i x
lemma to_partition_of_unity_zero_of_zero {i : ι} {x : X} (h : f i x = 0) :
f.to_partition_of_unity i x = 0 :=
f.to_pou_fun_zero_of_zero h
lemma support_to_partition_of_unity_subset (i : ι) :
support (f.to_partition_of_unity i) ⊆ support (f i) :=
f.support_to_pou_fun_subset i
lemma sum_to_partition_of_unity_eq (x : X) :
∑ᶠ i, f.to_partition_of_unity i x = 1 - ∏ᶠ i, (1 - f i x) :=
f.sum_to_pou_fun_eq x
lemma is_subordinate.to_partition_of_unity {f : bump_covering ι X s} {U : ι → set X}
(h : f.is_subordinate U) :
f.to_partition_of_unity.is_subordinate U :=
λ i, subset.trans (closure_mono $ f.support_to_partition_of_unity_subset i) (h i)
end bump_covering
namespace partition_of_unity
variables {s : set X}
instance [inhabited ι] : inhabited (partition_of_unity ι X s) :=
⟨(default (bump_covering ι X s)).to_partition_of_unity⟩
/-- If `X` is a normal topological space and `U` is a locally finite open covering of a closed set
`s`, then there exists a `partition_of_unity ι X s` that is subordinate to `U`. If `X` is a
paracompact space, then the assumption `hf : locally_finite U` can be omitted, see
`bump_covering.exists_is_subordinate`. -/
lemma exists_is_subordinate_of_locally_finite [normal_space X] (hs : is_closed s)
(U : ι → set X) (ho : ∀ i, is_open (U i)) (hf : locally_finite U)
(hU : s ⊆ ⋃ i, U i) :
∃ f : partition_of_unity ι X s, f.is_subordinate U :=
let ⟨f, hf⟩ := bump_covering.exists_is_subordinate_of_locally_finite hs U ho hf hU
in ⟨f.to_partition_of_unity, hf.to_partition_of_unity⟩
/-- If `X` is a paracompact normal topological space and `U` is an open covering of a closed set
`s`, then there exists a `partition_of_unity ι X s` that is subordinate to `U`. -/
lemma exists_is_subordinate [normal_space X] [paracompact_space X] (hs : is_closed s)
(U : ι → set X) (ho : ∀ i, is_open (U i)) (hU : s ⊆ ⋃ i, U i) :
∃ f : partition_of_unity ι X s, f.is_subordinate U :=
let ⟨f, hf⟩ := bump_covering.exists_is_subordinate hs U ho hU
in ⟨f.to_partition_of_unity, hf.to_partition_of_unity⟩
end partition_of_unity
|
6065c06957257b385996699bf90f69470571a3ae | e2fc96178628c7451e998a0db2b73877d0648be5 | /src/classes/context_free/closure_properties/bijection.lean | d6e0cee8dfe9c11d9129133c7705bf87c67ecbbf | [
"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 | 5,358 | lean | import classes.context_free.basics.toolbox
import utilities.language_operations
variables {T₁ T₂ N : Type}
private def sT₂_of_sT₁ (π : equiv T₁ T₂) : (symbol T₁ N) → (symbol T₂ N)
| (symbol.terminal t) := symbol.terminal (π.to_fun t)
| (symbol.nonterminal n) := symbol.nonterminal n
private def sT₁_of_sT₂ (π : equiv T₁ T₂) : (symbol T₂ N) → (symbol T₁ N)
| (symbol.terminal t) := symbol.terminal (π.inv_fun t)
| (symbol.nonterminal n) := symbol.nonterminal n
private def lsT₂_of_lsT₁ (π : equiv T₁ T₂) : list (symbol T₁ N) → list (symbol T₂ N) :=
list.map (sT₂_of_sT₁ π)
private def lsT₁_of_lsT₂ (π : equiv T₁ T₂) : list (symbol T₂ N) → list (symbol T₁ N) :=
list.map (sT₁_of_sT₂ π)
/-- The class of context-free languages is closed under bijection between terminal alphabets. -/
theorem CF_of_bijemap_CF (π : equiv T₁ T₂) (L : language T₁) :
is_CF L → is_CF (bijemap_lang L π) :=
begin
rintro ⟨g, hg⟩,
let g' : CF_grammar T₂ := CF_grammar.mk g.nt g.initial (list.map (
λ r : g.nt × (list (symbol T₁ g.nt)), (r.fst, lsT₂_of_lsT₁ π r.snd)
) g.rules),
use g',
apply set.eq_of_subset_of_subset,
{
intros w hw,
unfold bijemap_lang,
change list.map π.inv_fun w ∈ L,
rw ←hg,
unfold CF_language at hw ⊢,
rw set.mem_set_of_eq at hw ⊢,
unfold CF_generates at hw ⊢,
unfold CF_generates_str at hw ⊢,
have deri_of_deri :
∀ v : list (symbol T₂ g'.nt),
CF_derives g' [symbol.nonterminal g'.initial] v →
CF_derives g [symbol.nonterminal g.initial] (lsT₁_of_lsT₂ π v),
{
intros v hv,
induction hv with u v trash orig ih,
{
apply CF_deri_self,
},
apply CF_deri_of_deri_tran,
{
exact ih,
},
rcases orig with ⟨r, r_in, x, y, bef, aft⟩,
let r₁ := (r.fst, lsT₁_of_lsT₂ π r.snd),
let x₁ := lsT₁_of_lsT₂ π x,
let y₁ := lsT₁_of_lsT₂ π y,
use r₁,
split,
{
change (r.fst, lsT₁_of_lsT₂ π r.snd) ∈ g.rules,
rw [list.mem_map, prod.exists] at r_in,
rcases r_in with ⟨a, b, ab_in, ab_eq⟩,
have a_eq : a = r.fst :=
(congr_arg prod.fst ab_eq).congr_right.mp rfl,
have b_eq : lsT₂_of_lsT₁ π b = r.snd :=
(congr_arg prod.snd ab_eq).congr_right.mp rfl,
rw a_eq at ab_in,
convert ab_in,
rw ←b_eq,
unfold lsT₁_of_lsT₂,
unfold lsT₂_of_lsT₁,
rw list.map_map,
ext1,
rw list.nth_map,
cases (b.nth n),
{
-- none = none
refl,
},
cases val, swap,
{
-- nonterminal = nonterminal
refl,
},
{
-- (sT₁_of_sT₂ π ∘ sT₂_of_sT₁ π) terminal = terminal
simp [sT₂_of_sT₁, sT₁_of_sT₂, equiv.left_inv],
}
},
use x₁,
use y₁,
split,
{
rw bef,
unfold lsT₁_of_lsT₂,
rw list.map_append,
rw list.map_append,
refl,
},
{
rw aft,
unfold lsT₁_of_lsT₂,
rw list.map_append,
rw list.map_append,
refl,
},
},
specialize deri_of_deri (list.map symbol.terminal w) hw,
unfold lsT₁_of_lsT₂ at deri_of_deri,
rw list.map_map at *,
convert deri_of_deri,
},
{
intros w hw,
unfold bijemap_lang at hw,
change list.map π.inv_fun w ∈ L at hw,
rw ←hg at hw,
unfold CF_language at hw,
rw set.mem_set_of_eq at hw,
unfold CF_generates at hw,
rw list.map_map at hw,
unfold CF_generates_str at hw,
unfold CF_language,
change CF_generates_str g' (list.map symbol.terminal w),
unfold CF_generates_str,
have deri_of_deri :
∀ v : list (symbol T₁ g.nt),
CF_derives g [symbol.nonterminal g.initial] v →
CF_derives g' [symbol.nonterminal g'.initial] (lsT₂_of_lsT₁ π v),
{
intros v hv,
induction hv with u v trash orig ih,
{
apply CF_deri_self,
},
apply CF_deri_of_deri_tran,
{
exact ih,
},
rcases orig with ⟨r, r_in, x, y, bef, aft⟩,
let r₂ := (r.fst, lsT₂_of_lsT₁ π r.snd),
let x₂ := lsT₂_of_lsT₁ π x,
let y₂ := lsT₂_of_lsT₁ π y,
use r₂,
split,
{
rw [list.mem_map, prod.exists],
use r.fst,
use r.snd,
split,
{
convert r_in,
exact prod.ext rfl rfl,
},
split;
refl,
},
use x₂,
use y₂,
split,
{
rw bef,
unfold lsT₂_of_lsT₁,
rw list.map_append,
rw list.map_append,
refl,
},
{
rw aft,
unfold lsT₂_of_lsT₁,
rw list.map_append,
rw list.map_append,
refl,
},
},
specialize deri_of_deri (list.map (symbol.terminal ∘ π.inv_fun) w) hw,
rw lsT₂_of_lsT₁ at deri_of_deri,
rw list.map_map at deri_of_deri,
convert deri_of_deri,
ext1,
change symbol.terminal x = sT₂_of_sT₁ π (symbol.terminal (π.inv_fun x)),
unfold sT₂_of_sT₁,
rw equiv.right_inv,
},
end
|
f0677397fc9562e08f42e4741cac32c31837df84 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/combinatorics/set_family/kleitman.lean | 4d1edd2943526ae9b4f2719aa795dd86d2ba9fc3 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 3,882 | 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 combinatorics.set_family.harris_kleitman
import combinatorics.set_family.intersecting
/-!
# Kleitman's bound on the size of intersecting families
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
An intersecting family on `n` elements has size at most `2ⁿ⁻¹`, so we could naïvely think that two
intersecting families could cover all `2ⁿ` sets. But actually that's not case because for example
none of them can contain the empty set. Intersecting families are in some sense correlated.
Kleitman's bound stipulates that `k` intersecting families cover at most `2ⁿ - 2ⁿ⁻ᵏ` sets.
## Main declarations
* `finset.card_bUnion_le_of_intersecting`: Kleitman's theorem.
## References
* [D. J. Kleitman, *Families of non-disjoint subsets*][kleitman1966]
-/
open finset fintype (card)
variables {ι α : Type*} [fintype α] [decidable_eq α] [nonempty α]
/-- **Kleitman's theorem**. An intersecting family on `n` elements contains at most `2ⁿ⁻¹` sets, and
each further intersecting family takes at most half of the sets that are in no previous family. -/
lemma finset.card_bUnion_le_of_intersecting (s : finset ι) (f : ι → finset (finset α))
(hf : ∀ i ∈ s, (f i : set (finset α)).intersecting) :
(s.bUnion f).card ≤ 2 ^ card α - 2 ^ (card α - s.card) :=
begin
obtain hs | hs := le_total (card α) s.card,
{ rw [tsub_eq_zero_of_le hs, pow_zero],
refine (card_le_of_subset $ bUnion_subset.2 $ λ i hi a ha, mem_compl.2 $ not_mem_singleton.2 $
(hf _ hi).ne_bot ha).trans_eq _,
rw [card_compl, fintype.card_finset, card_singleton] },
induction s using finset.cons_induction with i s hi ih generalizing f,
{ simp },
classical,
set f' : ι → finset (finset α) := λ j,
if hj : j ∈ cons i s hi then (hf j hj).exists_card_eq.some else ∅ with hf',
have hf₁ : ∀ j, j ∈ cons i s hi →
f j ⊆ f' j ∧ 2 * (f' j).card = 2 ^ card α ∧ (f' j : set (finset α)).intersecting,
{ rintro j hj,
simp_rw [hf', dif_pos hj, ←fintype.card_finset],
exact classical.some_spec (hf j hj).exists_card_eq },
have hf₂ : ∀ j, j ∈ cons i s hi → is_upper_set (f' j : set (finset α)),
{ refine λ j hj, (hf₁ _ hj).2.2.is_upper_set' ((hf₁ _ hj).2.2.is_max_iff_card_eq.2 _),
rw fintype.card_finset,
exact (hf₁ _ hj).2.1 },
refine (card_le_of_subset $ bUnion_mono $ λ j hj, (hf₁ _ hj).1).trans _,
nth_rewrite 0 cons_eq_insert i,
rw bUnion_insert,
refine (card_mono $ @le_sup_sdiff _ _ _ $ f' i).trans ((card_union_le _ _).trans _),
rw [union_sdiff_left, sdiff_eq_inter_compl],
refine le_of_mul_le_mul_left _ (pow_pos zero_lt_two $ card α + 1),
rw [pow_succ', mul_add, mul_assoc, mul_comm _ 2, mul_assoc],
refine (add_le_add ((mul_le_mul_left $ pow_pos (zero_lt_two' ℕ) _).2
(hf₁ _ $ mem_cons_self _ _).2.2.card_le) $ (mul_le_mul_left $ zero_lt_two' ℕ).2 $
is_upper_set.card_inter_le_finset _ _).trans _,
{ rw coe_bUnion,
exact is_upper_set_Union₂ (λ i hi, hf₂ _ $ subset_cons _ hi) },
{ rw coe_compl,
exact (hf₂ _ $ mem_cons_self _ _).compl },
rw [mul_tsub, card_compl, fintype.card_finset, mul_left_comm, mul_tsub,
(hf₁ _ $ mem_cons_self _ _).2.1, two_mul, add_tsub_cancel_left, ←mul_tsub, ←mul_two, mul_assoc,
←add_mul, mul_comm],
refine mul_le_mul_left' _ _,
refine (add_le_add_left (ih ((card_le_of_subset $ subset_cons _).trans hs) _ $ λ i hi,
(hf₁ _ $ subset_cons _ hi).2.2) _).trans _,
rw [mul_tsub, two_mul, ←pow_succ, ←add_tsub_assoc_of_le (pow_le_pow' (one_le_two : (1 : ℕ) ≤ 2)
tsub_le_self), tsub_add_eq_add_tsub hs, card_cons, add_tsub_add_eq_tsub_right],
end
|
55963d05d873cef9576cf92fac2ad9bae4e2775b | 9b9a16fa2cb737daee6b2785474678b6fa91d6d4 | /src/category_theory/adjunction.lean | 028217dae91b4a61d3e7fbefb63c675d24960fc2 | [
"Apache-2.0"
] | permissive | johoelzl/mathlib | 253f46daa30b644d011e8e119025b01ad69735c4 | 592e3c7a2dfbd5826919b4605559d35d4d75938f | refs/heads/master | 1,625,657,216,488 | 1,551,374,946,000 | 1,551,374,946,000 | 98,915,829 | 0 | 0 | Apache-2.0 | 1,522,917,267,000 | 1,501,524,499,000 | Lean | UTF-8 | Lean | false | false | 14,354 | lean | /-
Copyright (c) 2019 Reid Barton. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Reid Barton, Johan Commelin
-/
import category_theory.limits.preserves
import category_theory.whiskering
import category_theory.equivalence
namespace category_theory
open category
open category_theory.limits
universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation
local attribute [elab_simple] whisker_left whisker_right
variables {C : Type u₁} [𝒞 : category.{v₁} C] {D : Type u₂} [𝒟 : category.{v₂} D]
include 𝒞 𝒟
/--
`adjunction F G` represents the data of an adjunction between two functors
`F : C ⥤ D` and `G : D ⥤ C`. `F` is the left adjoint and `G` is the right adjoint.
-/
structure adjunction (F : C ⥤ D) (G : D ⥤ C) :=
(hom_equiv : Π (X Y), (F.obj X ⟶ Y) ≃ (X ⟶ G.obj Y))
(unit : functor.id C ⟶ F.comp G)
(counit : G.comp F ⟶ functor.id D)
(hom_equiv_unit' : Π {X Y f}, (hom_equiv X Y) f = (unit : _ ⟹ _).app X ≫ G.map f . obviously)
(hom_equiv_counit' : Π {X Y g}, (hom_equiv X Y).symm g = F.map g ≫ counit.app Y . obviously)
namespace adjunction
restate_axiom hom_equiv_unit'
restate_axiom hom_equiv_counit'
attribute [simp, priority 1] hom_equiv_unit hom_equiv_counit
section
variables {F : C ⥤ D} {G : D ⥤ C} (adj : adjunction F G) {X' X : C} {Y Y' : D}
@[simp, priority 1] lemma hom_equiv_naturality_left_symm (f : X' ⟶ X) (g : X ⟶ G.obj Y) :
(adj.hom_equiv X' Y).symm (f ≫ g) = F.map f ≫ (adj.hom_equiv X Y).symm g :=
by rw [hom_equiv_counit, F.map_comp, assoc, adj.hom_equiv_counit.symm]
@[simp] lemma hom_equiv_naturality_left (f : X' ⟶ X) (g : F.obj X ⟶ Y) :
(adj.hom_equiv X' Y) (F.map f ≫ g) = f ≫ (adj.hom_equiv X Y) g :=
by rw [← equiv.eq_symm_apply]; simp [-hom_equiv_unit]
@[simp, priority 1] lemma hom_equiv_naturality_right (f : F.obj X ⟶ Y) (g : Y ⟶ Y') :
(adj.hom_equiv X Y') (f ≫ g) = (adj.hom_equiv X Y) f ≫ G.map g :=
by rw [hom_equiv_unit, G.map_comp, ← assoc, ←hom_equiv_unit]
@[simp] lemma hom_equiv_naturality_right_symm (f : X ⟶ G.obj Y) (g : Y ⟶ Y') :
(adj.hom_equiv X Y').symm (f ≫ G.map g) = (adj.hom_equiv X Y).symm f ≫ g :=
by rw [equiv.symm_apply_eq]; simp [-hom_equiv_counit]
@[simp] lemma left_triangle :
(whisker_right adj.unit F).vcomp (whisker_left F adj.counit) = nat_trans.id _ :=
begin
ext1 X, dsimp,
erw [← adj.hom_equiv_counit, equiv.symm_apply_eq, adj.hom_equiv_unit],
simp
end
@[simp] lemma right_triangle :
(whisker_left G adj.unit).vcomp (whisker_right adj.counit G) = nat_trans.id _ :=
begin
ext1 Y, dsimp,
erw [← adj.hom_equiv_unit, ← equiv.eq_symm_apply, adj.hom_equiv_counit],
simp
end
@[simp] lemma left_triangle_components :
F.map (adj.unit.app X) ≫ adj.counit.app (F.obj X) = 𝟙 _ :=
congr_arg (λ (t : _ ⟹ functor.id C ⋙ F), t.app X) adj.left_triangle
@[simp] lemma right_triangle_components {Y : D} :
adj.unit.app (G.obj Y) ≫ G.map (adj.counit.app Y) = 𝟙 _ :=
congr_arg (λ (t : _ ⟹ G ⋙ functor.id C), t.app Y) adj.right_triangle
end
structure core_hom_equiv (F : C ⥤ D) (G : D ⥤ C) :=
(hom_equiv : Π (X Y), (F.obj X ⟶ Y) ≃ (X ⟶ G.obj Y))
(hom_equiv_naturality_left_symm' : Π {X' X Y} (f : X' ⟶ X) (g : X ⟶ G.obj Y),
(hom_equiv X' Y).symm (f ≫ g) = F.map f ≫ (hom_equiv X Y).symm g . obviously)
(hom_equiv_naturality_right' : Π {X Y Y'} (f : F.obj X ⟶ Y) (g : Y ⟶ Y'),
(hom_equiv X Y') (f ≫ g) = (hom_equiv X Y) f ≫ G.map g . obviously)
namespace core_hom_equiv
restate_axiom hom_equiv_naturality_left_symm'
restate_axiom hom_equiv_naturality_right'
attribute [simp, priority 1] hom_equiv_naturality_left_symm hom_equiv_naturality_right
variables {F : C ⥤ D} {G : D ⥤ C} (adj : core_hom_equiv F G) {X' X : C} {Y Y' : D}
@[simp] lemma hom_equiv_naturality_left (f : X' ⟶ X) (g : F.obj X ⟶ Y) :
(adj.hom_equiv X' Y) (F.map f ≫ g) = f ≫ (adj.hom_equiv X Y) g :=
by rw [← equiv.eq_symm_apply]; simp
@[simp] lemma hom_equiv_naturality_right_symm (f : X ⟶ G.obj Y) (g : Y ⟶ Y') :
(adj.hom_equiv X Y').symm (f ≫ G.map g) = (adj.hom_equiv X Y).symm f ≫ g :=
by rw [equiv.symm_apply_eq]; simp
end core_hom_equiv
structure core_unit_counit (F : C ⥤ D) (G : D ⥤ C) :=
(unit : functor.id C ⟶ F.comp G)
(counit : G.comp F ⟶ functor.id D)
(left_triangle' : (whisker_right unit F).vcomp (whisker_left F counit) = nat_trans.id _ . obviously)
(right_triangle' : (whisker_left G unit).vcomp (whisker_right counit G) = nat_trans.id _ . obviously)
namespace core_unit_counit
restate_axiom left_triangle'
restate_axiom right_triangle'
attribute [simp] left_triangle right_triangle
end core_unit_counit
variables (F : C ⥤ D) (G : D ⥤ C)
def mk_of_hom_equiv (adj : core_hom_equiv F G) : adjunction F G :=
{ unit :=
{ app := λ X, (adj.hom_equiv X (F.obj X)) (𝟙 (F.obj X)),
naturality' :=
begin
intros,
erw [← adj.hom_equiv_naturality_left, ← adj.hom_equiv_naturality_right],
dsimp, simp
end },
counit :=
{ app := λ Y, (adj.hom_equiv _ _).inv_fun (𝟙 (G.obj Y)),
naturality' :=
begin
intros,
erw [← adj.hom_equiv_naturality_left_symm, ← adj.hom_equiv_naturality_right_symm],
dsimp, simp
end },
hom_equiv_unit' := λ X Y f, by erw [← adj.hom_equiv_naturality_right]; simp,
hom_equiv_counit' := λ X Y f, by erw [← adj.hom_equiv_naturality_left_symm]; simp,
.. adj }
def mk_of_unit_counit (adj : core_unit_counit F G) : adjunction F G :=
{ hom_equiv := λ X Y,
{ to_fun := λ f, adj.unit.app X ≫ G.map f,
inv_fun := λ g, F.map g ≫ adj.counit.app Y,
left_inv := λ f, begin
change F.map (_ ≫ _) ≫ _ = _,
rw [F.map_comp, assoc, ←functor.comp_map, adj.counit.naturality, ←assoc],
convert id_comp _ f,
exact congr_arg (λ t : _ ⟹ _, t.app _) adj.left_triangle
end,
right_inv := λ g, begin
change _ ≫ G.map (_ ≫ _) = _,
rw [G.map_comp, ←assoc, ←functor.comp_map, ←adj.unit.naturality, assoc],
convert comp_id _ g,
exact congr_arg (λ t : _ ⟹ _, t.app _) adj.right_triangle
end },
.. adj }
section
omit 𝒟
def id : adjunction (functor.id C) (functor.id C) :=
{ hom_equiv := λ X Y, equiv.refl _,
unit := 𝟙 _,
counit := 𝟙 _ }
end
/-
TODO
* define adjoint equivalences
* show that every equivalence can be improved into an adjoint equivalence
-/
section
variables {E : Type u₃} [ℰ : category.{v₃} E] (H : D ⥤ E) (I : E ⥤ D)
def comp (adj₁ : adjunction F G) (adj₂ : adjunction H I) : adjunction (F ⋙ H) (I ⋙ G) :=
{ hom_equiv := λ X Z, equiv.trans (adj₂.hom_equiv _ _) (adj₁.hom_equiv _ _),
unit := adj₁.unit ≫
(whisker_left F $ whisker_right adj₂.unit G) ≫ (functor.associator _ _ _).inv,
counit := (functor.associator _ _ _).hom ≫
(whisker_left I $ whisker_right adj₁.counit H) ≫ adj₂.counit }
end
structure is_left_adjoint (left : C ⥤ D) :=
(right : D ⥤ C)
(adj : adjunction left right)
structure is_right_adjoint (right : D ⥤ C) :=
(left : C ⥤ D)
(adj : adjunction left right)
section construct_left
-- Construction of a left adjoint. In order to construct a left
-- adjoint to a functor G : D → C, it suffices to give the object part
-- of a functor F : C → D together with isomorphisms Hom(FX, Y) ≃
-- Hom(X, GY) natural in Y. The action of F on morphisms can be
-- constructed from this data.
variables {F_obj : C → D} {G}
variables (e : Π X Y, (F_obj X ⟶ Y) ≃ (X ⟶ G.obj Y))
variables (he : Π X Y Y' g h, e X Y' (h ≫ g) = e X Y h ≫ G.map g)
include he
private lemma he' {X Y Y'} (f g) : (e X Y').symm (f ≫ G.map g) = (e X Y).symm f ≫ g :=
by intros; rw [equiv.symm_apply_eq, he]; simp
def left_adjoint_of_equiv : C ⥤ D :=
{ obj := F_obj,
map := λ X X' f, (e X (F_obj X')).symm (f ≫ e X' (F_obj X') (𝟙 _)),
map_comp' := λ X X' X'' f f', begin
rw [equiv.symm_apply_eq, he, equiv.apply_inverse_apply],
conv { to_rhs, rw [assoc, ←he, id_comp, equiv.apply_inverse_apply] },
simp
end }
def adjunction_of_equiv_left : adjunction (left_adjoint_of_equiv e he) G :=
mk_of_hom_equiv (left_adjoint_of_equiv e he) G
{ hom_equiv := e,
hom_equiv_naturality_left_symm' :=
begin
intros,
erw [← he' e he, ← equiv.apply_eq_iff_eq],
simp [(he _ _ _ _ _).symm]
end }
end construct_left
section construct_right
-- Construction of a right adjoint, analogous to the above.
variables {F} {G_obj : D → C}
variables (e : Π X Y, (F.obj X ⟶ Y) ≃ (X ⟶ G_obj Y))
variables (he : Π X' X Y f g, e X' Y (F.map f ≫ g) = f ≫ e X Y g)
include he
private lemma he' {X' X Y} (f g) : F.map f ≫ (e X Y).symm g = (e X' Y).symm (f ≫ g) :=
by intros; rw [equiv.eq_symm_apply, he]; simp
def right_adjoint_of_equiv : D ⥤ C :=
{ obj := G_obj,
map := λ Y Y' g, (e (G_obj Y) Y') ((e (G_obj Y) Y).symm (𝟙 _) ≫ g),
map_comp' := λ Y Y' Y'' g g', begin
rw [← equiv.eq_symm_apply, ← he' e he, equiv.inverse_apply_apply],
conv { to_rhs, rw [← assoc, he' e he, comp_id, equiv.inverse_apply_apply] },
simp
end }
def adjunction_of_equiv_right : adjunction F (right_adjoint_of_equiv e he) :=
mk_of_hom_equiv F (right_adjoint_of_equiv e he)
{ hom_equiv := e,
hom_equiv_naturality_left_symm' := by intros; rw [equiv.symm_apply_eq, he]; simp,
hom_equiv_naturality_right' :=
begin
intros X Y Y' g h,
erw [←he, equiv.apply_eq_iff_eq, ←assoc, he' e he, comp_id, equiv.inverse_apply_apply]
end }
end construct_right
end adjunction
end category_theory
namespace category_theory.adjunction
open category_theory
open category_theory.functor
open category_theory.limits
universes u₁ u₂ v
variables {C : Type u₁} [𝒞 : category.{v} C] {D : Type u₂} [𝒟 : category.{v} D]
include 𝒞 𝒟
variables {F : C ⥤ D} {G : D ⥤ C} (adj : adjunction F G)
include adj
section preservation_colimits
variables {J : Type v} [small_category J] (K : J ⥤ C)
def functoriality_is_left_adjoint :
is_left_adjoint (@cocones.functoriality _ _ _ _ K _ _ F) :=
{ right := (cocones.functoriality G) ⋙ (cocones.precompose
(K.right_unitor.inv ≫ (whisker_left K adj.unit) ≫ (associator _ _ _).inv)),
adj := mk_of_unit_counit _ _
{ unit :=
{ app := λ c,
{ hom := adj.unit.app c.X,
w' := λ j, by have := adj.unit.naturality (c.ι.app j); tidy },
naturality' := λ _ _ f, by have := adj.unit.naturality (f.hom); tidy },
counit :=
{ app := λ c,
{ hom := adj.counit.app c.X,
w' :=
begin
intro j,
dsimp,
erw [category.comp_id, category.id_comp, F.map_comp, category.assoc,
adj.counit.naturality (c.ι.app j), ← category.assoc,
adj.left_triangle_components, category.id_comp],
refl,
end },
naturality' := λ _ _ f, by have := adj.counit.naturality (f.hom); tidy } } }
/-- A left adjoint preserves colimits. -/
def left_adjoint_preserves_colimits : preserves_colimits F :=
λ J 𝒥 K, by resetI; exact
{ preserves := λ c hc, is_colimit_iso_unique_cocone_morphism.inv
(λ s, (((adj.functoriality_is_left_adjoint _).adj).hom_equiv _ _).unique_of_equiv $
is_colimit_iso_unique_cocone_morphism.hom hc _ ) }
end preservation_colimits
section preservation_limits
variables {J : Type v} [small_category J] (K : J ⥤ D)
def functoriality_is_right_adjoint :
is_right_adjoint (@cones.functoriality _ _ _ _ K _ _ G) :=
{ left := (cones.functoriality F) ⋙ (cones.postcompose
((associator _ _ _).hom ≫ (whisker_left K adj.counit) ≫ K.right_unitor.hom)),
adj := mk_of_unit_counit _ _
{ unit :=
{ app := λ c,
{ hom := adj.unit.app c.X,
w' :=
begin
intro j,
dsimp,
erw [category.comp_id, category.id_comp, G.map_comp, ← category.assoc,
← adj.unit.naturality (c.π.app j), category.assoc,
adj.right_triangle_components, category.comp_id],
refl,
end },
naturality' := λ _ _ f, by have := adj.unit.naturality (f.hom); tidy },
counit :=
{ app := λ c,
{ hom := adj.counit.app c.X,
w' := λ j, by have := adj.counit.naturality (c.π.app j); tidy },
naturality' := λ _ _ f, by have := adj.counit.naturality (f.hom); tidy } } }
/-- A right adjoint preserves limits. -/
def right_adjoint_preserves_limits : preserves_limits G :=
λ J 𝒥 K, by resetI; exact
{ preserves := λ c hc, is_limit_iso_unique_cone_morphism.inv
(λ s, (((adj.functoriality_is_right_adjoint _).adj).hom_equiv _ _).symm.unique_of_equiv $
is_limit_iso_unique_cone_morphism.hom hc _) }
end preservation_limits
-- Note: this is natural in K, but we do not yet have the tools to formulate that.
def cocones_iso {J : Type v} [small_category J] {K : J ⥤ C} :
(cocones J D).obj (op (K ⋙ F)) ≅ G ⋙ ((cocones J C).obj (op K)) :=
nat_iso.of_components (λ Y,
{ hom := λ t,
{ app := λ j, (adj.hom_equiv (K.obj j) Y) (t.app j),
naturality' := λ j j' f, by erw [← adj.hom_equiv_naturality_left, t.naturality]; dsimp; simp },
inv := λ t,
{ app := λ j, (adj.hom_equiv (K.obj j) Y).symm (t.app j),
naturality' := λ j j' f, begin
erw [← adj.hom_equiv_naturality_left_symm, ← adj.hom_equiv_naturality_right_symm, t.naturality],
dsimp, simp
end } } )
begin
intros Y₁ Y₂ f,
ext1 t,
ext1 j,
apply adj.hom_equiv_naturality_right
end
-- Note: this is natural in K, but we do not yet have the tools to formulate that.
def cones_iso {J : Type v} [small_category J] {K : J ⥤ D} :
F.op ⋙ ((cones J D).obj K) ≅ (cones J C).obj (K ⋙ G) :=
nat_iso.of_components (λ X,
{ hom := λ t,
{ app := λ j, (adj.hom_equiv (unop X) (K.obj j)) (t.app j),
naturality' := λ j j' f, begin
erw [← adj.hom_equiv_naturality_right, ← t.naturality, category.id_comp, category.id_comp],
refl
end },
inv := λ t,
{ app := λ j, (adj.hom_equiv (unop X) (K.obj j)).symm (t.app j),
naturality' := λ j j' f, begin
erw [← adj.hom_equiv_naturality_right_symm, ← t.naturality, category.id_comp, category.id_comp]
end } } )
(by tidy)
end category_theory.adjunction
|
7b0d1db47ecc9f447585d4ecc0df26800b704a9d | 69d4931b605e11ca61881fc4f66db50a0a875e39 | /src/algebra/lie/abelian.lean | c46fe3f98503a2bdb9f11a193e227d61f43a6114 | [
"Apache-2.0"
] | permissive | abentkamp/mathlib | d9a75d291ec09f4637b0f30cc3880ffb07549ee5 | 5360e476391508e092b5a1e5210bd0ed22dc0755 | refs/heads/master | 1,682,382,954,948 | 1,622,106,077,000 | 1,622,106,077,000 | 149,285,665 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,946 | lean | /-
Copyright (c) 2021 Oliver Nash. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Oliver Nash
-/
import algebra.lie.of_associative
import algebra.lie.ideal_operations
/-!
# Trivial Lie modules and Abelian Lie algebras
The action of a Lie algebra `L` on a module `M` is trivial if `⁅x, m⁆ = 0` for all `x ∈ L` and
`m ∈ M`. In the special case that `M = L` with the adjoint action, triviality corresponds to the
concept of an Abelian Lie algebra.
In this file we define these concepts and provide some related definitions and results.
## Main definitions
* `lie_module.is_trivial`
* `is_lie_abelian`
* `commutative_ring_iff_abelian_lie_ring`
* `lie_module.ker`
* `lie_module.max_triv_submodule`
* `lie_algebra.center`
## Tags
lie algebra, abelian, commutative, center
-/
universes u v w w₁ w₂
/-- A Lie (ring) module is trivial iff all brackets vanish. -/
class lie_module.is_trivial (L : Type v) (M : Type w) [has_bracket L M] [has_zero M] : Prop :=
(trivial : ∀ (x : L) (m : M), ⁅x, m⁆ = 0)
@[simp] lemma trivial_lie_zero (L : Type v) (M : Type w)
[has_bracket L M] [has_zero M] [lie_module.is_trivial L M] (x : L) (m : M) : ⁅x, m⁆ = 0 :=
lie_module.is_trivial.trivial x m
/-- A Lie algebra is Abelian iff it is trivial as a Lie module over itself. -/
abbreviation is_lie_abelian (L : Type v) [has_bracket L L] [has_zero L] : Prop :=
lie_module.is_trivial L L
instance lie_ideal.is_lie_abelian_of_trivial (R : Type u) (L : Type v)
[comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) [h : lie_module.is_trivial L I] :
is_lie_abelian I :=
{ trivial := λ x y, by apply h.trivial, }
lemma function.injective.is_lie_abelian {R : Type u} {L₁ : Type v} {L₂ : Type w}
[comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂]
{f : L₁ →ₗ⁅R⁆ L₂} (h₁ : function.injective f) (h₂ : is_lie_abelian L₂) :
is_lie_abelian L₁ :=
{ trivial := λ x y,
by { apply h₁, rw [lie_hom.map_lie, trivial_lie_zero, lie_hom.map_zero], } }
lemma function.surjective.is_lie_abelian {R : Type u} {L₁ : Type v} {L₂ : Type w}
[comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂]
{f : L₁ →ₗ⁅R⁆ L₂} (h₁ : function.surjective f) (h₂ : is_lie_abelian L₁) :
is_lie_abelian L₂ :=
{ trivial := λ x y,
begin
obtain ⟨u, hu⟩ := h₁ x, rw ← hu,
obtain ⟨v, hv⟩ := h₁ y, rw ← hv,
rw [← lie_hom.map_lie, trivial_lie_zero, lie_hom.map_zero],
end }
lemma lie_abelian_iff_equiv_lie_abelian {R : Type u} {L₁ : Type v} {L₂ : Type w}
[comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂]
(e : L₁ ≃ₗ⁅R⁆ L₂) : is_lie_abelian L₁ ↔ is_lie_abelian L₂ :=
⟨e.symm.injective.is_lie_abelian, e.injective.is_lie_abelian⟩
lemma commutative_ring_iff_abelian_lie_ring {A : Type v} [ring A] :
is_commutative A (*) ↔ is_lie_abelian A :=
begin
have h₁ : is_commutative A (*) ↔ ∀ (a b : A), a * b = b * a := ⟨λ h, h.1, λ h, ⟨h⟩⟩,
have h₂ : is_lie_abelian A ↔ ∀ (a b : A), ⁅a, b⁆ = 0 := ⟨λ h, h.1, λ h, ⟨h⟩⟩,
simp only [h₁, h₂, lie_ring.of_associative_ring_bracket, sub_eq_zero],
end
lemma lie_algebra.is_lie_abelian_bot (R : Type u) (L : Type v)
[comm_ring R] [lie_ring L] [lie_algebra R L] : is_lie_abelian (⊥ : lie_ideal R L) :=
⟨begin
rintros ⟨x, hx⟩ ⟨y, hy⟩,
suffices : ⁅x, y⁆ = 0,
{ ext, simp only [this, lie_subalgebra.coe_bracket, submodule.coe_mk, submodule.coe_zero], },
change x ∈ (⊥ : lie_ideal R L) at hx, rw lie_submodule.mem_bot at hx, rw [hx, zero_lie],
end⟩
section center
variables (R : Type u) (L : Type v) (M : Type w) (N : Type w₁)
variables [comm_ring R] [lie_ring L] [lie_algebra R L]
variables [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M]
variables [add_comm_group N] [module R N] [lie_ring_module L N] [lie_module R L N]
namespace lie_module
/-- The kernel of the action of a Lie algebra `L` on a Lie module `M` as a Lie ideal in `L`. -/
protected def ker : lie_ideal R L := (to_endomorphism R L M).ker
@[simp] protected lemma mem_ker (x : L) : x ∈ lie_module.ker R L M ↔ ∀ (m : M), ⁅x, m⁆ = 0 :=
begin
dunfold lie_module.ker,
simp only [lie_hom.mem_ker, linear_map.ext_iff, linear_map.zero_apply,
to_endomorphism_apply_apply],
end
/-- The largest submodule of a Lie module `M` on which the Lie algebra `L` acts trivially. -/
def max_triv_submodule : lie_submodule R L M :=
{ carrier := { m | ∀ (x : L), ⁅x, m⁆ = 0 },
zero_mem' := λ x, lie_zero x,
add_mem' := λ x y hx hy z, by rw [lie_add, hx, hy, add_zero],
smul_mem' := λ c x hx y, by rw [lie_smul, hx, smul_zero],
lie_mem := λ x m hm y, by rw [hm, lie_zero], }
@[simp] lemma mem_max_triv_submodule (m : M) :
m ∈ max_triv_submodule R L M ↔ ∀ (x : L), ⁅x, m⁆ = 0 :=
iff.rfl
instance : is_trivial L (max_triv_submodule R L M) :=
{ trivial := λ x m, subtype.ext (m.property x), }
lemma trivial_iff_le_maximal_trivial (N : lie_submodule R L M) :
is_trivial L N ↔ N ≤ max_triv_submodule R L M :=
begin
split,
{ rintros ⟨h⟩, intros m hm x, specialize h x ⟨m, hm⟩, rw subtype.ext_iff at h, exact h, },
{ intros h, constructor, rintros x ⟨m, hm⟩, apply subtype.ext, apply h, exact hm, },
end
lemma is_trivial_iff_max_triv_eq_top :
is_trivial L M ↔ max_triv_submodule R L M = ⊤ :=
begin
split,
{ rintros ⟨h⟩, ext,
simp only [mem_max_triv_submodule, h, forall_const, true_iff, eq_self_iff_true], },
{ intros h, constructor, intros x m, revert x,
rw [← mem_max_triv_submodule R L M, h], exact lie_submodule.mem_top m, },
end
variables {R L M N}
/-- `max_triv_submodule` is functorial. -/
def max_triv_hom (f : M →ₗ⁅R,L⁆ N) :
max_triv_submodule R L M →ₗ⁅R,L⁆ max_triv_submodule R L N :=
{ to_fun := λ m, ⟨f m, λ x, by
{ have h := congr_arg f (m.property x),
rw [lie_module_hom.map_zero, lie_module_hom.map_lie] at h, exact h, }⟩,
map_add' := λ m n, by simpa,
map_smul' := λ t m, by simpa,
map_lie' := λ x m, by simp, }
@[norm_cast, simp] lemma coe_max_triv_hom_apply
(f : M →ₗ⁅R,L⁆ N) (m : max_triv_submodule R L M) :
(max_triv_hom f m : N) = f m :=
rfl
/-- The maximal trivial submodules of Lie-equivalent Lie modules are Lie-equivalent. -/
def max_triv_equiv (e : M ≃ₗ⁅R,L⁆ N) :
max_triv_submodule R L M ≃ₗ⁅R,L⁆ max_triv_submodule R L N :=
{ to_fun := max_triv_hom (e : M →ₗ⁅R,L⁆ N),
inv_fun := max_triv_hom (e.symm : N →ₗ⁅R,L⁆ M),
left_inv := λ m, by { ext, simp, },
right_inv := λ n, by { ext, simp, },
.. max_triv_hom (e : M →ₗ⁅R,L⁆ N), }
@[norm_cast, simp] lemma coe_max_triv_equiv_apply
(e : M ≃ₗ⁅R,L⁆ N) (m : max_triv_submodule R L M) :
(max_triv_equiv e m : N) = e ↑m :=
rfl
@[simp] lemma max_triv_equiv_of_refl_eq_refl :
max_triv_equiv (lie_module_equiv.refl : M ≃ₗ⁅R,L⁆ M) = lie_module_equiv.refl :=
by { ext, simp only [coe_max_triv_equiv_apply, lie_module_equiv.refl_apply], }
@[simp] lemma max_triv_equiv_of_equiv_symm_eq_symm (e : M ≃ₗ⁅R,L⁆ N) :
(max_triv_equiv e).symm = max_triv_equiv e.symm :=
rfl
/-- A linear map between two Lie modules is a morphism of Lie modules iff the Lie algebra action
on it is trivial. -/
def max_triv_linear_map_equiv_lie_module_hom :
(max_triv_submodule R L (M →ₗ[R] N)) ≃ₗ[R] (M →ₗ⁅R,L⁆ N) :=
{ to_fun := λ f,
{ map_lie' := λ x m, by
{ have hf : ⁅x, f.val⁆ m = 0, { rw [f.property x, linear_map.zero_apply], },
rw [lie_hom.lie_apply, sub_eq_zero, ← linear_map.to_fun_eq_coe] at hf, exact hf.symm, },
..f.val, },
map_add' := λ f g, by { ext, simp, },
map_smul' := λ F G, by { ext, simp, },
inv_fun := λ F, ⟨F, λ x, by { ext, simp, }⟩,
left_inv := λ f, by simp,
right_inv := λ F, by simp, }
@[simp] lemma coe_max_triv_linear_map_equiv_lie_module_hom
(f : max_triv_submodule R L (M →ₗ[R] N)) :
((max_triv_linear_map_equiv_lie_module_hom f) : M → N) = f :=
by { ext, refl, }
@[simp] lemma coe_max_triv_linear_map_equiv_lie_module_hom_symm
(f : M →ₗ⁅R,L⁆ N) :
((max_triv_linear_map_equiv_lie_module_hom.symm f) : M → N) = f :=
rfl
@[simp] lemma coe_linear_map_max_triv_linear_map_equiv_lie_module_hom
(f : max_triv_submodule R L (M →ₗ[R] N)) :
((max_triv_linear_map_equiv_lie_module_hom f) : M →ₗ[R] N) = (f : M →ₗ[R] N) :=
by { ext, refl, }
@[simp] lemma coe_linear_map_max_triv_linear_map_equiv_lie_module_hom_symm
(f : M →ₗ⁅R,L⁆ N) :
((max_triv_linear_map_equiv_lie_module_hom.symm f) : M →ₗ[R] N) = (f : M →ₗ[R] N) :=
rfl
end lie_module
namespace lie_algebra
/-- The center of a Lie algebra is the set of elements that commute with everything. It can
be viewed as the maximal trivial submodule of the Lie algebra as a Lie module over itself via the
adjoint representation. -/
abbreviation center : lie_ideal R L := lie_module.max_triv_submodule R L L
instance : is_lie_abelian (center R L) := infer_instance
lemma center_eq_adjoint_kernel : center R L = lie_module.ker R L L :=
begin
ext y,
simp only [lie_module.mem_max_triv_submodule, lie_module.mem_ker,
← lie_skew _ y, neg_eq_zero],
end
lemma abelian_of_le_center (I : lie_ideal R L) (h : I ≤ center R L) : is_lie_abelian I :=
begin
rw ← lie_module.trivial_iff_le_maximal_trivial R L L I at h,
haveI := h, exact lie_ideal.is_lie_abelian_of_trivial R L I,
end
lemma is_lie_abelian_iff_center_eq_top : is_lie_abelian L ↔ center R L = ⊤ :=
lie_module.is_trivial_iff_max_triv_eq_top R L L
end lie_algebra
end center
section ideal_operations
open lie_submodule lie_subalgebra
variables {R : Type u} {L : Type v} {M : Type w}
variables [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M]
variables [lie_ring_module L M] [lie_module R L M]
variables (N N' : lie_submodule R L M) (I J : lie_ideal R L)
@[simp] lemma lie_submodule.trivial_lie_oper_zero [lie_module.is_trivial L M] : ⁅I, N⁆ = ⊥ :=
begin
suffices : ⁅I, N⁆ ≤ ⊥, { exact le_bot_iff.mp this, },
rw [lie_ideal_oper_eq_span, lie_submodule.lie_span_le],
rintros m ⟨x, n, h⟩, rw trivial_lie_zero at h, simp [← h],
end
lemma lie_submodule.lie_abelian_iff_lie_self_eq_bot : is_lie_abelian I ↔ ⁅I, I⁆ = ⊥ :=
begin
simp only [_root_.eq_bot_iff, lie_ideal_oper_eq_span, lie_submodule.lie_span_le,
lie_submodule.bot_coe, set.subset_singleton_iff, set.mem_set_of_eq, exists_imp_distrib],
split; intros h,
{ intros z x y hz, rw [← hz, ← lie_subalgebra.coe_bracket, coe_zero_iff_zero], apply h.trivial, },
{ exact ⟨λ x y, by { rw ← coe_zero_iff_zero, apply h _ x y, refl, }⟩, },
end
end ideal_operations
|
b7a5933920b855a3c64fa5de540ef1f35a1a48d0 | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/order/filter/basic.lean | 313e134593b15d3f051626cd2d6bfb66fc82baa3 | [
"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 | 96,930 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Jeremy Avigad
-/
import order.zorn
import order.copy
import data.set.finite
import tactic.monotonicity
/-!
# Theory of filters on sets
## Main definitions
* `filter` : filters on a set;
* `at_top`, `at_bot`, `cofinite`, `principal` : specific filters;
* `map`, `comap`, `prod` : operations on filters;
* `tendsto` : limit with respect to filters;
* `eventually` : `f.eventually p` means `{x | p x} ∈ f`;
* `frequently` : `f.frequently p` means `{x | ¬p x} ∉ f`;
* `filter_upwards [h₁, ..., hₙ]` : takes a list of proofs `hᵢ : sᵢ ∈ f`, and replaces a goal `s ∈ f`
with `∀ x, x ∈ s₁ → ... → x ∈ sₙ → x ∈ s`;
* `ne_bot f` : an utility class stating that `f` is a non-trivial filter.
Filters on a type `X` are sets of sets of `X` satisfying three conditions. They are mostly used to
abstract two related kinds of ideas:
* *limits*, including finite or infinite limits of sequences, finite or infinite limits of functions
at a point or at infinity, etc...
* *things happening eventually*, including things happening for large enough `n : ℕ`, or near enough
a point `x`, or for close enough pairs of points, or things happening almost everywhere in the
sense of measure theory. Dually, filters can also express the idea of *things happening often*:
for arbitrarily large `n`, or at a point in any neighborhood of given a point etc...
In this file, we define the type `filter X` of filters on `X`, and endow it with a complete lattice
structure. This structure is lifted from the lattice structure on `set (set X)` using the Galois
insertion which maps a filter to its elements in one direction, and an arbitrary set of sets to
the smallest filter containing it in the other direction.
We also prove `filter` is a monadic functor, with a push-forward operation
`filter.map` and a pull-back operation `filter.comap` that form a Galois connections for the
order on filters.
Finally we describe a product operation `filter X → filter Y → filter (X × Y)`.
The examples of filters appearing in the description of the two motivating ideas are:
* `(at_top : filter ℕ)` : made of sets of `ℕ` containing `{n | n ≥ N}` for some `N`
* `𝓝 x` : made of neighborhoods of `x` in a topological space (defined in topology.basic)
* `𝓤 X` : made of entourages of a uniform space (those space are generalizations of metric spaces
defined in topology.uniform_space.basic)
* `μ.ae` : made of sets whose complement has zero measure with respect to `μ` (defined in
`measure_theory.measure_space`)
The general notion of limit of a map with respect to filters on the source and target types
is `filter.tendsto`. It is defined in terms of the order and the push-forward operation.
The predicate "happening eventually" is `filter.eventually`, and "happening often" is
`filter.frequently`, whose definitions are immediate after `filter` is defined (but they come
rather late in this file in order to immediately relate them to the lattice structure).
For instance, anticipating on topology.basic, the statement: "if a sequence `u` converges to
some `x` and `u n` belongs to a set `M` for `n` large enough then `x` is in the closure of
`M`" is formalized as: `tendsto u at_top (𝓝 x) → (∀ᶠ n in at_top, u n ∈ M) → x ∈ closure M`,
which is a special case of `mem_closure_of_tendsto` from topology.basic.
## Notations
* `∀ᶠ x in f, p x` : `f.eventually p`;
* `∃ᶠ x in f, p x` : `f.frequently p`;
* `f =ᶠ[l] g` : `∀ᶠ x in l, f x = g x`;
* `f ≤ᶠ[l] g` : `∀ᶠ x in l, f x ≤ g x`;
* `f ×ᶠ g` : `filter.prod f g`, localized in `filter`;
* `𝓟 s` : `principal s`, localized in `filter`.
## References
* [N. Bourbaki, *General Topology*][bourbaki1966]
Important note: Bourbaki requires that a filter on `X` cannot contain all sets of `X`, which
we do *not* require. This gives `filter X` better formal properties, in particular a bottom element
`⊥` for its lattice structure, at the cost of including the assumption
`[ne_bot f]` in a number of lemmas and definitions.
-/
open set
universes u v w x y
open_locale classical
/-- A filter `F` on a type `α` is a collection of sets of `α` which contains the whole `α`,
is upwards-closed, and is stable under intersection. We do not forbid this collection to be
all sets of `α`. -/
structure filter (α : Type*) :=
(sets : set (set α))
(univ_sets : set.univ ∈ sets)
(sets_of_superset {x y} : x ∈ sets → x ⊆ y → y ∈ sets)
(inter_sets {x y} : x ∈ sets → y ∈ sets → x ∩ y ∈ sets)
/-- If `F` is a filter on `α`, and `U` a subset of `α` then we can write `U ∈ F` as on paper. -/
@[reducible]
instance {α : Type*}: has_mem (set α) (filter α) := ⟨λ U F, U ∈ F.sets⟩
namespace filter
variables {α : Type u} {f g : filter α} {s t : set α}
instance inhabited_mem : inhabited {s : set α // s ∈ f} := ⟨⟨univ, f.univ_sets⟩⟩
lemma filter_eq : ∀{f g : filter α}, f.sets = g.sets → f = g
| ⟨a, _, _, _⟩ ⟨._, _, _, _⟩ rfl := rfl
lemma filter_eq_iff : f = g ↔ f.sets = g.sets :=
⟨congr_arg _, filter_eq⟩
protected lemma ext_iff : f = g ↔ ∀ s, s ∈ f ↔ s ∈ g :=
by rw [filter_eq_iff, ext_iff]
@[ext]
protected lemma ext : (∀ s, s ∈ f ↔ s ∈ g) → f = g :=
filter.ext_iff.2
lemma univ_mem_sets : univ ∈ f :=
f.univ_sets
lemma mem_sets_of_superset : ∀{x y : set α}, x ∈ f → x ⊆ y → y ∈ f :=
f.sets_of_superset
lemma inter_mem_sets : ∀{s t}, s ∈ f → t ∈ f → s ∩ t ∈ f :=
f.inter_sets
lemma univ_mem_sets' (h : ∀ a, a ∈ s) : s ∈ f :=
mem_sets_of_superset univ_mem_sets (assume x _, h x)
lemma mp_sets (hs : s ∈ f) (h : {x | x ∈ s → x ∈ t} ∈ f) : t ∈ f :=
mem_sets_of_superset (inter_mem_sets hs h) $ assume x ⟨h₁, h₂⟩, h₂ h₁
lemma congr_sets (h : {x | x ∈ s ↔ x ∈ t} ∈ f) : s ∈ f ↔ t ∈ f :=
⟨λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mp)),
λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mpr))⟩
lemma Inter_mem_sets {β : Type v} {s : β → set α} {is : set β} (hf : finite is) :
(∀i∈is, s i ∈ f) → (⋂i∈is, s i) ∈ f :=
finite.induction_on hf
(assume hs, by simp only [univ_mem_sets, mem_empty_eq, Inter_neg, Inter_univ, not_false_iff])
(assume i is _ hf hi hs,
have h₁ : s i ∈ f, from hs i (by simp),
have h₂ : (⋂x∈is, s x) ∈ f, from hi $ assume a ha, hs _ $ by simp only [ha, mem_insert_iff, or_true],
by simp [inter_mem_sets h₁ h₂])
lemma sInter_mem_sets_of_finite {s : set (set α)} (hfin : finite s) (h_in : ∀ U ∈ s, U ∈ f) :
⋂₀ s ∈ f :=
by { rw sInter_eq_bInter, exact Inter_mem_sets hfin h_in }
lemma Inter_mem_sets_of_fintype {β : Type v} {s : β → set α} [fintype β] (h : ∀i, s i ∈ f) :
(⋂i, s i) ∈ f :=
by simpa using Inter_mem_sets finite_univ (λi hi, h i)
lemma exists_sets_subset_iff : (∃t ∈ f, t ⊆ s) ↔ s ∈ f :=
⟨assume ⟨t, ht, ts⟩, mem_sets_of_superset ht ts, assume hs, ⟨s, hs, subset.refl _⟩⟩
lemma monotone_mem_sets {f : filter α} : monotone (λs, s ∈ f) :=
assume s t hst h, mem_sets_of_superset h hst
end filter
namespace tactic.interactive
open tactic interactive
/-- `filter_upwards [h1, ⋯, hn]` replaces a goal of the form `s ∈ f`
and terms `h1 : t1 ∈ f, ⋯, hn : tn ∈ f` with `∀x, x ∈ t1 → ⋯ → x ∈ tn → x ∈ s`.
`filter_upwards [h1, ⋯, hn] e` is a short form for `{ filter_upwards [h1, ⋯, hn], exact e }`.
-/
meta def filter_upwards
(s : parse types.pexpr_list)
(e' : parse $ optional types.texpr) : tactic unit :=
do
s.reverse.mmap (λ e, eapplyc `filter.mp_sets >> eapply e),
eapplyc `filter.univ_mem_sets',
match e' with
| some e := interactive.exact e
| none := skip
end
end tactic.interactive
namespace filter
variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x}
section principal
/-- The principal filter of `s` is the collection of all supersets of `s`. -/
def principal (s : set α) : filter α :=
{ sets := {t | s ⊆ t},
univ_sets := subset_univ s,
sets_of_superset := assume x y hx hy, subset.trans hx hy,
inter_sets := assume x y, subset_inter }
localized "notation `𝓟` := filter.principal" in filter
instance : inhabited (filter α) :=
⟨𝓟 ∅⟩
@[simp] lemma mem_principal_sets {s t : set α} : s ∈ 𝓟 t ↔ t ⊆ s := iff.rfl
lemma mem_principal_self (s : set α) : s ∈ 𝓟 s := subset.refl _
end principal
open_locale filter
section join
/-- The join of a filter of filters is defined by the relation `s ∈ join f ↔ {t | s ∈ t} ∈ f`. -/
def join (f : filter (filter α)) : filter α :=
{ sets := {s | {t : filter α | s ∈ t} ∈ f},
univ_sets := by simp only [univ_mem_sets, mem_set_of_eq]; exact univ_mem_sets,
sets_of_superset := assume x y hx xy,
mem_sets_of_superset hx $ assume f h, mem_sets_of_superset h xy,
inter_sets := assume x y hx hy,
mem_sets_of_superset (inter_mem_sets hx hy) $ assume f ⟨h₁, h₂⟩, inter_mem_sets h₁ h₂ }
@[simp] lemma mem_join_sets {s : set α} {f : filter (filter α)} :
s ∈ join f ↔ {t | s ∈ t} ∈ f := iff.rfl
end join
section lattice
instance : partial_order (filter α) :=
{ le := λf g, ∀ ⦃U : set α⦄, U ∈ g → U ∈ f,
le_antisymm := assume a b h₁ h₂, filter_eq $ subset.antisymm h₂ h₁,
le_refl := assume a, subset.refl _,
le_trans := assume a b c h₁ h₂, subset.trans h₂ h₁ }
theorem le_def {f g : filter α} : f ≤ g ↔ ∀ x ∈ g, x ∈ f := iff.rfl
/-- `generate_sets g s`: `s` is in the filter closure of `g`. -/
inductive generate_sets (g : set (set α)) : set α → Prop
| basic {s : set α} : s ∈ g → generate_sets s
| univ : generate_sets univ
| superset {s t : set α} : generate_sets s → s ⊆ t → generate_sets t
| inter {s t : set α} : generate_sets s → generate_sets t → generate_sets (s ∩ t)
/-- `generate g` is the smallest filter containing the sets `g`. -/
def generate (g : set (set α)) : filter α :=
{ sets := generate_sets g,
univ_sets := generate_sets.univ,
sets_of_superset := assume x y, generate_sets.superset,
inter_sets := assume s t, generate_sets.inter }
lemma sets_iff_generate {s : set (set α)} {f : filter α} : f ≤ filter.generate s ↔ s ⊆ f.sets :=
iff.intro
(assume h u hu, h $ generate_sets.basic $ hu)
(assume h u hu, hu.rec_on h univ_mem_sets
(assume x y _ hxy hx, mem_sets_of_superset hx hxy)
(assume x y _ _ hx hy, inter_mem_sets hx hy))
lemma mem_generate_iff (s : set $ set α) {U : set α} : U ∈ generate s ↔ ∃ t ⊆ s, finite t ∧ ⋂₀ t ⊆ U :=
begin
split ; intro h,
{ induction h with V V_in V W V_in hVW hV V W V_in W_in hV hW,
{ use {V},
simp [V_in] },
{ use ∅,
simp [subset.refl, univ] },
{ rcases hV with ⟨t, hts, htfin, hinter⟩,
exact ⟨t, hts, htfin, subset.trans hinter hVW⟩ },
{ rcases hV with ⟨t, hts, htfin, htinter⟩,
rcases hW with ⟨z, hzs, hzfin, hzinter⟩,
refine ⟨t ∪ z, union_subset hts hzs, htfin.union hzfin, _⟩,
rw sInter_union,
exact inter_subset_inter htinter hzinter } },
{ rcases h with ⟨t, ts, tfin, h⟩,
apply generate_sets.superset _ h,
revert ts,
apply finite.induction_on tfin,
{ intro h,
rw sInter_empty,
exact generate_sets.univ },
{ intros V r hV rfin hinter h,
cases insert_subset.mp h with V_in r_sub,
rw [insert_eq V r, sInter_union],
apply generate_sets.inter _ (hinter r_sub),
rw sInter_singleton,
exact generate_sets.basic V_in } },
end
/-- `mk_of_closure s hs` constructs a filter on `α` whose elements set is exactly
`s : set (set α)`, provided one gives the assumption `hs : (generate s).sets = s`. -/
protected def mk_of_closure (s : set (set α)) (hs : (generate s).sets = s) : filter α :=
{ sets := s,
univ_sets := hs ▸ (univ_mem_sets : univ ∈ generate s),
sets_of_superset := assume x y, hs ▸ (mem_sets_of_superset : x ∈ generate s → x ⊆ y → y ∈ generate s),
inter_sets := assume x y, hs ▸ (inter_mem_sets : x ∈ generate s → y ∈ generate s → x ∩ y ∈ generate s) }
lemma mk_of_closure_sets {s : set (set α)} {hs : (generate s).sets = s} :
filter.mk_of_closure s hs = generate s :=
filter.ext $ assume u,
show u ∈ (filter.mk_of_closure s hs).sets ↔ u ∈ (generate s).sets, from hs.symm ▸ iff.rfl
/-- Galois insertion from sets of sets into filters. -/
def gi_generate (α : Type*) :
@galois_insertion (set (set α)) (order_dual (filter α)) _ _ filter.generate filter.sets :=
{ gc := assume s f, sets_iff_generate,
le_l_u := assume f u h, generate_sets.basic h,
choice := λs hs, filter.mk_of_closure s (le_antisymm hs $ sets_iff_generate.1 $ le_refl _),
choice_eq := assume s hs, mk_of_closure_sets }
/-- The infimum of filters is the filter generated by intersections
of elements of the two filters. -/
instance : has_inf (filter α) := ⟨λf g : filter α,
{ sets := {s | ∃ (a ∈ f) (b ∈ g), a ∩ b ⊆ s },
univ_sets := ⟨_, univ_mem_sets, _, univ_mem_sets, inter_subset_left _ _⟩,
sets_of_superset := assume x y ⟨a, ha, b, hb, h⟩ xy, ⟨a, ha, b, hb, subset.trans h xy⟩,
inter_sets := assume x y ⟨a, ha, b, hb, hx⟩ ⟨c, hc, d, hd, hy⟩,
⟨_, inter_mem_sets ha hc, _, inter_mem_sets hb hd,
calc a ∩ c ∩ (b ∩ d) = (a ∩ b) ∩ (c ∩ d) : by ac_refl
... ⊆ x ∩ y : inter_subset_inter hx hy⟩ }⟩
@[simp] lemma mem_inf_sets {f g : filter α} {s : set α} :
s ∈ f ⊓ g ↔ ∃t₁∈f, ∃t₂∈g, t₁ ∩ t₂ ⊆ s := iff.rfl
lemma mem_inf_sets_of_left {f g : filter α} {s : set α} (h : s ∈ f) : s ∈ f ⊓ g :=
⟨s, h, univ, univ_mem_sets, inter_subset_left _ _⟩
lemma mem_inf_sets_of_right {f g : filter α} {s : set α} (h : s ∈ g) : s ∈ f ⊓ g :=
⟨univ, univ_mem_sets, s, h, inter_subset_right _ _⟩
lemma inter_mem_inf_sets {α : Type u} {f g : filter α} {s t : set α}
(hs : s ∈ f) (ht : t ∈ g) : s ∩ t ∈ f ⊓ g :=
inter_mem_sets (mem_inf_sets_of_left hs) (mem_inf_sets_of_right ht)
instance : has_top (filter α) :=
⟨{ sets := {s | ∀x, x ∈ s},
univ_sets := assume x, mem_univ x,
sets_of_superset := assume x y hx hxy a, hxy (hx a),
inter_sets := assume x y hx hy a, mem_inter (hx _) (hy _) }⟩
lemma mem_top_sets_iff_forall {s : set α} : s ∈ (⊤ : filter α) ↔ (∀x, x ∈ s) :=
iff.rfl
@[simp] lemma mem_top_sets {s : set α} : s ∈ (⊤ : filter α) ↔ s = univ :=
by rw [mem_top_sets_iff_forall, eq_univ_iff_forall]
section complete_lattice
/- We lift the complete lattice along the Galois connection `generate` / `sets`. Unfortunately,
we want to have different definitional equalities for the lattice operations. So we define them
upfront and change the lattice operations for the complete lattice instance. -/
private def original_complete_lattice : complete_lattice (filter α) :=
@order_dual.complete_lattice _ (gi_generate α).lift_complete_lattice
local attribute [instance] original_complete_lattice
instance : complete_lattice (filter α) := original_complete_lattice.copy
/- le -/ filter.partial_order.le rfl
/- top -/ (filter.has_top).1
(top_unique $ assume s hs, by have := univ_mem_sets ; finish)
/- bot -/ _ rfl
/- sup -/ _ rfl
/- inf -/ (filter.has_inf).1
begin
ext f g : 2,
exact le_antisymm
(le_inf (assume s, mem_inf_sets_of_left) (assume s, mem_inf_sets_of_right))
(assume s ⟨a, ha, b, hb, hs⟩, show s ∈ complete_lattice.inf f g, from
mem_sets_of_superset (inter_mem_sets
(@inf_le_left (filter α) _ _ _ _ ha)
(@inf_le_right (filter α) _ _ _ _ hb)) hs)
end
/- Sup -/ (join ∘ 𝓟) (by ext s x; exact (@mem_bInter_iff _ _ s filter.sets x).symm)
/- Inf -/ _ rfl
end complete_lattice
/-- A filter is `ne_bot` if it is not equal to `⊥`, or equivalently the empty set
does not belong to the filter. Bourbaki include this assumption in the definition
of a filter but we prefer to have a `complete_lattice` structure on filter, so
we use a typeclass argument in lemmas instead. -/
@[class] def ne_bot (f : filter α) := f ≠ ⊥
lemma ne_bot.ne {f : filter α} (hf : ne_bot f) : f ≠ ⊥ := hf
lemma ne_bot.mono {f g : filter α} (hf : ne_bot f) (hg : f ≤ g) : ne_bot g :=
ne_bot_of_le_ne_bot hf hg
lemma ne_bot_of_le {f g : filter α} [hf : ne_bot f] (hg : f ≤ g) : ne_bot g :=
hf.mono hg
lemma bot_sets_eq : (⊥ : filter α).sets = univ := rfl
lemma sup_sets_eq {f g : filter α} : (f ⊔ g).sets = f.sets ∩ g.sets :=
(gi_generate α).gc.u_inf
lemma Sup_sets_eq {s : set (filter α)} : (Sup s).sets = (⋂f∈s, (f:filter α).sets) :=
(gi_generate α).gc.u_Inf
lemma supr_sets_eq {f : ι → filter α} : (supr f).sets = (⋂i, (f i).sets) :=
(gi_generate α).gc.u_infi
lemma generate_empty : filter.generate ∅ = (⊤ : filter α) :=
(gi_generate α).gc.l_bot
lemma generate_univ : filter.generate univ = (⊥ : filter α) :=
mk_of_closure_sets.symm
lemma generate_union {s t : set (set α)} :
filter.generate (s ∪ t) = filter.generate s ⊓ filter.generate t :=
(gi_generate α).gc.l_sup
lemma generate_Union {s : ι → set (set α)} :
filter.generate (⋃ i, s i) = (⨅ i, filter.generate (s i)) :=
(gi_generate α).gc.l_supr
@[simp] lemma mem_bot_sets {s : set α} : s ∈ (⊥ : filter α) :=
trivial
@[simp] lemma mem_sup_sets {f g : filter α} {s : set α} :
s ∈ f ⊔ g ↔ s ∈ f ∧ s ∈ g :=
iff.rfl
@[simp] lemma mem_Sup_sets {x : set α} {s : set (filter α)} :
x ∈ Sup s ↔ (∀f∈s, x ∈ (f:filter α)) :=
iff.rfl
@[simp] lemma mem_supr_sets {x : set α} {f : ι → filter α} :
x ∈ supr f ↔ (∀i, x ∈ f i) :=
by simp only [supr_sets_eq, iff_self, mem_Inter]
lemma infi_eq_generate (s : ι → filter α) : infi s = generate (⋃ i, (s i).sets) :=
show generate _ = generate _, from congr_arg _ supr_range
lemma mem_infi_iff {ι} {s : ι → filter α} {U : set α} : (U ∈ ⨅ i, s i) ↔
∃ I : set ι, finite I ∧ ∃ V : {i | i ∈ I} → set α, (∀ i, V i ∈ s i) ∧ (⋂ i, V i) ⊆ U :=
begin
rw [infi_eq_generate, mem_generate_iff],
split,
{ rintro ⟨t, tsub, tfin, tinter⟩,
rcases eq_finite_Union_of_finite_subset_Union tfin tsub with ⟨I, Ifin, σ, σfin, σsub, rfl⟩,
rw sInter_Union at tinter,
let V := λ i, ⋂₀ σ i,
have V_in : ∀ i, V i ∈ s i,
{ rintro ⟨i, i_in⟩,
apply sInter_mem_sets_of_finite (σfin _),
apply σsub },
exact ⟨I, Ifin, V, V_in, tinter⟩ },
{ rintro ⟨I, Ifin, V, V_in, h⟩,
refine ⟨range V, _, _, h⟩,
{ rintro _ ⟨i, rfl⟩,
rw mem_Union,
use [i, V_in i] },
{ haveI : fintype {i : ι | i ∈ I} := finite.fintype Ifin,
exact finite_range _ } },
end
@[simp] lemma le_principal_iff {s : set α} {f : filter α} : f ≤ 𝓟 s ↔ s ∈ f :=
show (∀{t}, s ⊆ t → t ∈ f) ↔ s ∈ f,
from ⟨assume h, h (subset.refl s), assume hs t ht, mem_sets_of_superset hs ht⟩
lemma principal_mono {s t : set α} : 𝓟 s ≤ 𝓟 t ↔ s ⊆ t :=
by simp only [le_principal_iff, iff_self, mem_principal_sets]
@[mono] lemma monotone_principal : monotone (𝓟 : set α → filter α) :=
λ _ _, principal_mono.2
@[simp] lemma principal_eq_iff_eq {s t : set α} : 𝓟 s = 𝓟 t ↔ s = t :=
by simp only [le_antisymm_iff, le_principal_iff, mem_principal_sets]; refl
@[simp] lemma join_principal_eq_Sup {s : set (filter α)} : join (𝓟 s) = Sup s := rfl
lemma principal_univ : 𝓟 (univ : set α) = ⊤ :=
top_unique $ by simp only [le_principal_iff, mem_top_sets, eq_self_iff_true]
lemma principal_empty : 𝓟 (∅ : set α) = ⊥ :=
bot_unique $ assume s _, empty_subset _
/-! ### Lattice equations -/
lemma empty_in_sets_eq_bot {f : filter α} : ∅ ∈ f ↔ f = ⊥ :=
⟨assume h, bot_unique $ assume s _, mem_sets_of_superset h (empty_subset s),
assume : f = ⊥, this.symm ▸ mem_bot_sets⟩
lemma nonempty_of_mem_sets {f : filter α} [hf : ne_bot f] {s : set α} (hs : s ∈ f) :
s.nonempty :=
s.eq_empty_or_nonempty.elim (λ h, absurd hs (h.symm ▸ mt empty_in_sets_eq_bot.mp hf)) id
lemma ne_bot.nonempty_of_mem {f : filter α} (hf : ne_bot f) {s : set α} (hs : s ∈ f) :
s.nonempty :=
@nonempty_of_mem_sets α f hf s hs
lemma nonempty_of_ne_bot (f : filter α) [ne_bot f] : nonempty α :=
nonempty_of_exists $ nonempty_of_mem_sets (univ_mem_sets : univ ∈ f)
lemma filter_eq_bot_of_not_nonempty (f : filter α) (ne : ¬ nonempty α) : f = ⊥ :=
empty_in_sets_eq_bot.mp $ univ_mem_sets' $ assume x, false.elim (ne ⟨x⟩)
lemma forall_sets_nonempty_iff_ne_bot {f : filter α} :
(∀ (s : set α), s ∈ f → s.nonempty) ↔ ne_bot f :=
⟨λ h hf, empty_not_nonempty (h ∅ $ hf.symm ▸ mem_bot_sets), @nonempty_of_mem_sets _ _⟩
lemma mem_sets_of_eq_bot {f : filter α} {s : set α} (h : f ⊓ 𝓟 sᶜ = ⊥) : s ∈ f :=
have ∅ ∈ f ⊓ 𝓟 sᶜ, from h.symm ▸ mem_bot_sets,
let ⟨s₁, hs₁, s₂, (hs₂ : sᶜ ⊆ s₂), (hs : s₁ ∩ s₂ ⊆ ∅)⟩ := this in
by filter_upwards [hs₁] assume a ha, classical.by_contradiction $ assume ha', hs ⟨ha, hs₂ ha'⟩
lemma inf_ne_bot_iff {f g : filter α} :
ne_bot (f ⊓ g) ↔ ∀ {U V}, U ∈ f → V ∈ g → set.nonempty (U ∩ V) :=
begin
rw ← forall_sets_nonempty_iff_ne_bot,
simp_rw mem_inf_sets,
split ; intro h,
{ intros U V U_in V_in,
exact h (U ∩ V) ⟨U, U_in, V, V_in, subset.refl _⟩ },
{ rintros S ⟨U, U_in, V, V_in, hUV⟩,
cases h U_in V_in with a ha,
use [a, hUV ha] }
end
lemma inf_principal_ne_bot_iff {f : filter α} {s : set α} :
ne_bot (f ⊓ 𝓟 s) ↔ ∀ U ∈ f, (U ∩ s).nonempty :=
begin
rw inf_ne_bot_iff,
apply forall_congr,
intros U,
split,
{ intros h U_in,
exact h U_in (mem_principal_self s) },
{ intros h V U_in V_in,
rw mem_principal_sets at V_in,
cases h U_in with x hx,
exact ⟨x, hx.1, V_in hx.2⟩ },
end
lemma inf_eq_bot_iff {f g : filter α} :
f ⊓ g = ⊥ ↔ ∃ U V, (U ∈ f) ∧ (V ∈ g) ∧ U ∩ V = ∅ :=
begin
rw ← not_iff_not,
apply inf_ne_bot_iff.trans,
simp only [not_exists, not_and, ← ne.def, ne_empty_iff_nonempty]
end
protected lemma disjoint_iff {f g : filter α} :
disjoint f g ↔ ∃ U V, (U ∈ f) ∧ (V ∈ g) ∧ U ∩ V = ∅ :=
disjoint_iff.trans inf_eq_bot_iff
lemma eq_Inf_of_mem_sets_iff_exists_mem {S : set (filter α)} {l : filter α}
(h : ∀ {s}, s ∈ l ↔ ∃ f ∈ S, s ∈ f) : l = Inf S :=
le_antisymm (le_Inf $ λ f hf s hs, h.2 ⟨f, hf, hs⟩)
(λ s hs, let ⟨f, hf, hs⟩ := h.1 hs in (Inf_le hf : Inf S ≤ f) hs)
lemma eq_infi_of_mem_sets_iff_exists_mem {f : ι → filter α} {l : filter α}
(h : ∀ {s}, s ∈ l ↔ ∃ i, s ∈ f i) :
l = infi f :=
eq_Inf_of_mem_sets_iff_exists_mem $ λ s, h.trans exists_range_iff.symm
lemma eq_binfi_of_mem_sets_iff_exists_mem {f : ι → filter α} {p : ι → Prop} {l : filter α}
(h : ∀ {s}, s ∈ l ↔ ∃ i (_ : p i), s ∈ f i) :
l = ⨅ i (_ : p i), f i :=
begin
rw [infi_subtype'],
apply eq_infi_of_mem_sets_iff_exists_mem,
intro s,
exact h.trans ⟨λ ⟨i, pi, si⟩, ⟨⟨i, pi⟩, si⟩, λ ⟨⟨i, pi⟩, si⟩, ⟨i, pi, si⟩⟩
end
lemma infi_sets_eq {f : ι → filter α} (h : directed (≥) f) [ne : nonempty ι] :
(infi f).sets = (⋃ i, (f i).sets) :=
let ⟨i⟩ := ne, u := { filter .
sets := (⋃ i, (f i).sets),
univ_sets := by simp only [mem_Union]; exact ⟨i, univ_mem_sets⟩,
sets_of_superset := by simp only [mem_Union, exists_imp_distrib];
intros x y i hx hxy; exact ⟨i, mem_sets_of_superset hx hxy⟩,
inter_sets :=
begin
simp only [mem_Union, exists_imp_distrib],
assume x y a hx b hy,
rcases h a b with ⟨c, ha, hb⟩,
exact ⟨c, inter_mem_sets (ha hx) (hb hy)⟩
end } in
have u = infi f, from eq_infi_of_mem_sets_iff_exists_mem (λ s, by simp only [mem_Union]),
congr_arg filter.sets this.symm
lemma mem_infi {f : ι → filter α} (h : directed (≥) f) [nonempty ι] (s) :
s ∈ infi f ↔ ∃ i, s ∈ f i :=
by simp only [infi_sets_eq h, mem_Union]
@[nolint ge_or_gt] -- Intentional use of `≥`
lemma binfi_sets_eq {f : β → filter α} {s : set β}
(h : directed_on (f ⁻¹'o (≥)) s) (ne : s.nonempty) :
(⨅ i∈s, f i).sets = (⋃ i ∈ s, (f i).sets) :=
by haveI := ne.to_subtype;
calc (⨅ i ∈ s, f i).sets = (⨅ t : {t // t ∈ s}, (f t.val)).sets : by rw [infi_subtype]; refl
... = (⨆ t : {t // t ∈ s}, (f t.val).sets) : infi_sets_eq h.directed_coe
... = (⨆ t ∈ s, (f t).sets) : by rw [supr_subtype]; refl
@[nolint ge_or_gt] -- Intentional use of `≥`
lemma mem_binfi {f : β → filter α} {s : set β}
(h : directed_on (f ⁻¹'o (≥)) s) (ne : s.nonempty) {t : set α} :
t ∈ (⨅ i∈s, f i) ↔ ∃ i ∈ s, t ∈ f i :=
by simp only [binfi_sets_eq h ne, mem_bUnion_iff]
lemma infi_sets_eq_finite {ι : Type*} (f : ι → filter α) :
(⨅i, f i).sets = (⋃t:finset ι, (⨅i∈t, f i).sets) :=
begin
rw [infi_eq_infi_finset, infi_sets_eq],
exact (directed_of_sup $ λs₁ s₂ hs, infi_le_infi $ λi, infi_le_infi_const $ λh, hs h),
end
lemma infi_sets_eq_finite' (f : ι → filter α) :
(⨅i, f i).sets = (⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets) :=
by rw [← infi_sets_eq_finite, ← equiv.plift.surjective.infi_comp]; refl
lemma mem_infi_finite {ι : Type*} {f : ι → filter α} (s) :
s ∈ infi f ↔ s ∈ ⋃t:finset ι, (⨅i∈t, f i).sets :=
set.ext_iff.1 (infi_sets_eq_finite f) s
lemma mem_infi_finite' {f : ι → filter α} (s) :
s ∈ infi f ↔ s ∈ ⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets :=
set.ext_iff.1 (infi_sets_eq_finite' f) s
@[simp] lemma sup_join {f₁ f₂ : filter (filter α)} : (join f₁ ⊔ join f₂) = join (f₁ ⊔ f₂) :=
filter_eq $ set.ext $ assume x,
by simp only [supr_sets_eq, join, mem_sup_sets, iff_self, mem_set_of_eq]
@[simp] lemma supr_join {ι : Sort w} {f : ι → filter (filter α)} :
(⨆x, join (f x)) = join (⨆x, f x) :=
filter_eq $ set.ext $ assume x,
by simp only [supr_sets_eq, join, iff_self, mem_Inter, mem_set_of_eq]
instance : bounded_distrib_lattice (filter α) :=
{ le_sup_inf :=
begin
assume x y z s,
simp only [and_assoc, mem_inf_sets, mem_sup_sets, exists_prop, exists_imp_distrib, and_imp],
intros hs t₁ ht₁ t₂ ht₂ hts,
exact ⟨s ∪ t₁,
x.sets_of_superset hs $ subset_union_left _ _,
y.sets_of_superset ht₁ $ subset_union_right _ _,
s ∪ t₂,
x.sets_of_superset hs $ subset_union_left _ _,
z.sets_of_superset ht₂ $ subset_union_right _ _,
subset.trans (@le_sup_inf (set α) _ _ _ _) (union_subset (subset.refl _) hts)⟩
end,
..filter.complete_lattice }
/- the complementary version with ⨆i, f ⊓ g i does not hold! -/
lemma infi_sup_eq {f : filter α} {g : ι → filter α} : (⨅ x, f ⊔ g x) = f ⊔ infi g :=
begin
refine le_antisymm _ (le_infi $ assume i, sup_le_sup_left (infi_le _ _) _),
rintros t ⟨h₁, h₂⟩,
rw [infi_sets_eq_finite'] at h₂,
simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at h₂,
rcases h₂ with ⟨s, hs⟩,
suffices : (⨅i, f ⊔ g i) ≤ f ⊔ s.inf (λi, g i.down), { exact this ⟨h₁, hs⟩ },
refine finset.induction_on s _ _,
{ exact le_sup_right_of_le le_top },
{ rintros ⟨i⟩ s his ih,
rw [finset.inf_insert, sup_inf_left],
exact le_inf (infi_le _ _) ih }
end
lemma mem_infi_sets_finset {s : finset α} {f : α → filter β} :
∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⋂a∈s, p a) ⊆ t) :=
show ∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⨅a∈s, p a) ≤ t),
begin
simp only [(finset.inf_eq_infi _ _).symm],
refine finset.induction_on s _ _,
{ simp only [finset.not_mem_empty, false_implies_iff, finset.inf_empty, top_le_iff,
imp_true_iff, mem_top_sets, true_and, exists_const],
intros; refl },
{ intros a s has ih t,
simp only [ih, finset.forall_mem_insert, finset.inf_insert, mem_inf_sets,
exists_prop, iff_iff_implies_and_implies, exists_imp_distrib, and_imp, and_assoc] {contextual := tt},
split,
{ intros t₁ ht₁ t₂ p hp ht₂ ht,
existsi function.update p a t₁,
have : ∀a'∈s, function.update p a t₁ a' = p a',
from assume a' ha',
have a' ≠ a, from assume h, has $ h ▸ ha',
function.update_noteq this _ _,
have eq : s.inf (λj, function.update p a t₁ j) = s.inf (λj, p j) :=
finset.inf_congr rfl this,
simp only [this, ht₁, hp, function.update_same, true_and, imp_true_iff, eq] {contextual := tt},
exact subset.trans (inter_subset_inter (subset.refl _) ht₂) ht },
assume p hpa hp ht,
exact ⟨p a, hpa, (s.inf p), ⟨⟨p, hp, le_refl _⟩, ht⟩⟩ }
end
/-- If `f : ι → filter α` is directed, `ι` is not empty, and `∀ i, f i ≠ ⊥`, then `infi f ≠ ⊥`.
See also `infi_ne_bot_of_directed` for a version assuming `nonempty α` instead of `nonempty ι`. -/
lemma infi_ne_bot_of_directed' {f : ι → filter α} [nonempty ι]
(hd : directed (≥) f) (hb : ∀i, ne_bot (f i)) : ne_bot (infi f) :=
begin
intro h,
have he: ∅ ∈ (infi f), from h.symm ▸ (mem_bot_sets : ∅ ∈ (⊥ : filter α)),
obtain ⟨i, hi⟩ : ∃i, ∅ ∈ f i,
from (mem_infi hd ∅).1 he,
exact hb i (empty_in_sets_eq_bot.1 hi)
end
/-- If `f : ι → filter α` is directed, `α` is not empty, and `∀ i, f i ≠ ⊥`, then `infi f ≠ ⊥`.
See also `infi_ne_bot_of_directed'` for a version assuming `nonempty ι` instead of `nonempty α`. -/
lemma infi_ne_bot_of_directed {f : ι → filter α}
[hn : nonempty α] (hd : directed (≥) f) (hb : ∀i, ne_bot (f i)) : ne_bot (infi f) :=
if hι : nonempty ι then @infi_ne_bot_of_directed' _ _ _ hι hd hb else
assume h : infi f = ⊥,
have univ ⊆ (∅ : set α),
begin
rw [←principal_mono, principal_univ, principal_empty, ←h],
exact (le_infi $ assume i, false.elim $ hι ⟨i⟩)
end,
let ⟨x⟩ := hn in this (mem_univ x)
lemma infi_ne_bot_iff_of_directed' {f : ι → filter α} [nonempty ι] (hd : directed (≥) f) :
ne_bot (infi f) ↔ ∀i, ne_bot (f i) :=
⟨assume H i, H.mono (infi_le _ i), infi_ne_bot_of_directed' hd⟩
lemma infi_ne_bot_iff_of_directed {f : ι → filter α} [nonempty α] (hd : directed (≥) f) :
ne_bot (infi f) ↔ (∀i, ne_bot (f i)) :=
⟨assume H i, H.mono (infi_le _ i), infi_ne_bot_of_directed hd⟩
lemma mem_infi_sets {f : ι → filter α} (i : ι) : ∀{s}, s ∈ f i → s ∈ ⨅i, f i :=
show (⨅i, f i) ≤ f i, from infi_le _ _
@[elab_as_eliminator]
lemma infi_sets_induct {f : ι → filter α} {s : set α} (hs : s ∈ infi f) {p : set α → Prop}
(uni : p univ)
(ins : ∀{i s₁ s₂}, s₁ ∈ f i → p s₂ → p (s₁ ∩ s₂))
(upw : ∀{s₁ s₂}, s₁ ⊆ s₂ → p s₁ → p s₂) : p s :=
begin
rw [mem_infi_finite'] at hs,
simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at hs,
rcases hs with ⟨is, his⟩,
revert s,
refine finset.induction_on is _ _,
{ assume s hs, rwa [mem_top_sets.1 hs] },
{ rintros ⟨i⟩ js his ih s hs,
rw [finset.inf_insert, mem_inf_sets] at hs,
rcases hs with ⟨s₁, hs₁, s₂, hs₂, hs⟩,
exact upw hs (ins hs₁ (ih hs₂)) }
end
/- principal equations -/
@[simp] lemma inf_principal {s t : set α} : 𝓟 s ⊓ 𝓟 t = 𝓟 (s ∩ t) :=
le_antisymm
(by simp; exact ⟨s, subset.refl s, t, subset.refl t, by simp⟩)
(by simp [le_inf_iff, inter_subset_left, inter_subset_right])
@[simp] lemma sup_principal {s t : set α} : 𝓟 s ⊔ 𝓟 t = 𝓟 (s ∪ t) :=
filter_eq $ set.ext $
by simp only [union_subset_iff, union_subset_iff, mem_sup_sets, forall_const, iff_self, mem_principal_sets]
@[simp] lemma supr_principal {ι : Sort w} {s : ι → set α} : (⨆x, 𝓟 (s x)) = 𝓟 (⋃i, s i) :=
filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, mem_principal_sets, mem_Inter];
exact (@supr_le_iff (set α) _ _ _ _).symm
@[simp] lemma principal_eq_bot_iff {s : set α} : 𝓟 s = ⊥ ↔ s = ∅ :=
empty_in_sets_eq_bot.symm.trans $ mem_principal_sets.trans subset_empty_iff
lemma principal_ne_bot_iff {s : set α} : ne_bot (𝓟 s) ↔ s.nonempty :=
(not_congr principal_eq_bot_iff).trans ne_empty_iff_nonempty
lemma is_compl_principal (s : set α) : is_compl (𝓟 s) (𝓟 sᶜ) :=
⟨by simp only [inf_principal, inter_compl_self, principal_empty, le_refl],
by simp only [sup_principal, union_compl_self, principal_univ, le_refl]⟩
lemma inf_principal_eq_bot {f : filter α} {s : set α} (hs : sᶜ ∈ f) : f ⊓ 𝓟 s = ⊥ :=
empty_in_sets_eq_bot.mp ⟨_, hs, s, mem_principal_self s, assume x ⟨h₁, h₂⟩, h₁ h₂⟩
theorem mem_inf_principal {f : filter α} {s t : set α} :
s ∈ f ⊓ 𝓟 t ↔ {x | x ∈ t → x ∈ s} ∈ f :=
begin
simp only [← le_principal_iff, (is_compl_principal s).le_left_iff, disjoint, inf_assoc,
inf_principal, imp_iff_not_or],
rw [← disjoint, ← (is_compl_principal (t ∩ sᶜ)).le_right_iff, compl_inter, compl_compl],
refl
end
lemma mem_iff_inf_principal_compl {f : filter α} {V : set α} :
V ∈ f ↔ f ⊓ 𝓟 Vᶜ = ⊥ :=
begin
rw inf_eq_bot_iff,
split,
{ intro h,
use [V, Vᶜ],
simp [h, subset.refl] },
{ rintros ⟨U, W, U_in, W_in, UW⟩,
rw [mem_principal_sets, compl_subset_comm] at W_in,
apply mem_sets_of_superset U_in,
intros x x_in,
apply W_in,
intro H,
have : x ∈ U ∩ W := ⟨x_in, H⟩,
rwa UW at this },
end
lemma le_iff_forall_inf_principal_compl {f g : filter α} :
f ≤ g ↔ ∀ V ∈ g, f ⊓ 𝓟 Vᶜ = ⊥ :=
begin
change (∀ V ∈ g, V ∈ f) ↔ _,
simp_rw [mem_iff_inf_principal_compl],
end
lemma principal_le_iff {s : set α} {f : filter α} :
𝓟 s ≤ f ↔ ∀ V ∈ f, s ⊆ V :=
begin
change (∀ V, V ∈ f → V ∈ _) ↔ _,
simp_rw mem_principal_sets,
end
@[simp] lemma infi_principal_finset {ι : Type w} (s : finset ι) (f : ι → set α) :
(⨅i∈s, 𝓟 (f i)) = 𝓟 (⋂i∈s, f i) :=
begin
ext t,
simp [mem_infi_sets_finset],
split,
{ rintros ⟨p, hp, ht⟩,
calc (⋂ (i : ι) (H : i ∈ s), f i) ≤ (⋂ (i : ι) (H : i ∈ s), p i) :
infi_le_infi (λi, infi_le_infi (λhi, mem_principal_sets.1 (hp i hi)))
... ≤ t : ht },
{ assume h,
exact ⟨f, λi hi, subset.refl _, h⟩ }
end
@[simp] lemma infi_principal_fintype {ι : Type w} [fintype ι] (f : ι → set α) :
(⨅i, 𝓟 (f i)) = 𝓟 (⋂i, f i) :=
by simpa using infi_principal_finset finset.univ f
end lattice
@[mono] lemma join_mono {f₁ f₂ : filter (filter α)} (h : f₁ ≤ f₂) :
join f₁ ≤ join f₂ :=
λ s hs, h hs
/-! ### Eventually -/
/-- `f.eventually p` or `∀ᶠ x in f, p x` mean that `{x | p x} ∈ f`. E.g., `∀ᶠ x in at_top, p x`
means that `p` holds true for sufficiently large `x`. -/
protected def eventually (p : α → Prop) (f : filter α) : Prop := {x | p x} ∈ f
notation `∀ᶠ` binders ` in ` f `, ` r:(scoped p, filter.eventually p f) := r
lemma eventually_iff {f : filter α} {P : α → Prop} : (∀ᶠ x in f, P x) ↔ {x | P x} ∈ f :=
iff.rfl
protected lemma ext' {f₁ f₂ : filter α}
(h : ∀ p : α → Prop, (∀ᶠ x in f₁, p x) ↔ (∀ᶠ x in f₂, p x)) :
f₁ = f₂ :=
filter.ext h
lemma eventually.filter_mono {f₁ f₂ : filter α} (h : f₁ ≤ f₂) {p : α → Prop}
(hp : ∀ᶠ x in f₂, p x) :
∀ᶠ x in f₁, p x :=
h hp
lemma eventually_of_mem {f : filter α} {P : α → Prop} {U : set α} (hU : U ∈ f) (h : ∀ x ∈ U, P x) :
∀ᶠ x in f, P x :=
mem_sets_of_superset hU h
protected lemma eventually.and {p q : α → Prop} {f : filter α} :
f.eventually p → f.eventually q → ∀ᶠ x in f, p x ∧ q x :=
inter_mem_sets
@[simp]
lemma eventually_true (f : filter α) : ∀ᶠ x in f, true := univ_mem_sets
lemma eventually_of_forall {p : α → Prop} {f : filter α} (hp : ∀ x, p x) :
∀ᶠ x in f, p x :=
univ_mem_sets' hp
@[simp] lemma eventually_false_iff_eq_bot {f : filter α} :
(∀ᶠ x in f, false) ↔ f = ⊥ :=
empty_in_sets_eq_bot
@[simp] lemma eventually_const {f : filter α} [ne_bot f] {p : Prop} :
(∀ᶠ x in f, p) ↔ p :=
classical.by_cases (λ h : p, by simp [h]) (λ h, by simpa [h])
lemma eventually_iff_exists_mem {p : α → Prop} {f : filter α} :
(∀ᶠ x in f, p x) ↔ ∃ v ∈ f, ∀ y ∈ v, p y :=
exists_sets_subset_iff.symm
lemma eventually.exists_mem {p : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) :
∃ v ∈ f, ∀ y ∈ v, p y :=
eventually_iff_exists_mem.1 hp
lemma eventually.mp {p q : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x)
(hq : ∀ᶠ x in f, p x → q x) :
∀ᶠ x in f, q x :=
mp_sets hp hq
lemma eventually.mono {p q : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x)
(hq : ∀ x, p x → q x) :
∀ᶠ x in f, q x :=
hp.mp (eventually_of_forall hq)
@[simp] lemma eventually_and {p q : α → Prop} {f : filter α} :
(∀ᶠ x in f, p x ∧ q x) ↔ (∀ᶠ x in f, p x) ∧ (∀ᶠ x in f, q x) :=
⟨λ h, ⟨h.mono $ λ _, and.left, h.mono $ λ _, and.right⟩, λ h, h.1.and h.2⟩
lemma eventually.congr {f : filter α} {p q : α → Prop} (h' : ∀ᶠ x in f, p x)
(h : ∀ᶠ x in f, p x ↔ q x) : ∀ᶠ x in f, q x :=
h'.mp (h.mono $ λ x hx, hx.mp)
lemma eventually_congr {f : filter α} {p q : α → Prop} (h : ∀ᶠ x in f, p x ↔ q x) :
(∀ᶠ x in f, p x) ↔ (∀ᶠ x in f, q x) :=
⟨λ hp, hp.congr h, λ hq, hq.congr $ by simpa only [iff.comm] using h⟩
@[simp] lemma eventually_or_distrib_left {f : filter α} {p : Prop} {q : α → Prop} :
(∀ᶠ x in f, p ∨ q x) ↔ (p ∨ ∀ᶠ x in f, q x) :=
classical.by_cases (λ h : p, by simp [h]) (λ h, by simp [h])
@[simp] lemma eventually_or_distrib_right {f : filter α} {p : α → Prop} {q : Prop} :
(∀ᶠ x in f, p x ∨ q) ↔ ((∀ᶠ x in f, p x) ∨ q) :=
by simp only [or_comm _ q, eventually_or_distrib_left]
@[simp] lemma eventually_imp_distrib_left {f : filter α} {p : Prop} {q : α → Prop} :
(∀ᶠ x in f, p → q x) ↔ (p → ∀ᶠ x in f, q x) :=
by simp only [imp_iff_not_or, eventually_or_distrib_left]
@[simp]
lemma eventually_bot {p : α → Prop} : ∀ᶠ x in ⊥, p x := ⟨⟩
@[simp]
lemma eventually_top {p : α → Prop} : (∀ᶠ x in ⊤, p x) ↔ (∀ x, p x) :=
iff.rfl
lemma eventually_sup {p : α → Prop} {f g : filter α} :
(∀ᶠ x in f ⊔ g, p x) ↔ (∀ᶠ x in f, p x) ∧ (∀ᶠ x in g, p x) :=
iff.rfl
@[simp]
lemma eventually_Sup {p : α → Prop} {fs : set (filter α)} :
(∀ᶠ x in Sup fs, p x) ↔ (∀ f ∈ fs, ∀ᶠ x in f, p x) :=
iff.rfl
@[simp]
lemma eventually_supr {p : α → Prop} {fs : β → filter α} :
(∀ᶠ x in (⨆ b, fs b), p x) ↔ (∀ b, ∀ᶠ x in fs b, p x) :=
mem_supr_sets
@[simp]
lemma eventually_principal {a : set α} {p : α → Prop} :
(∀ᶠ x in 𝓟 a, p x) ↔ (∀ x ∈ a, p x) :=
iff.rfl
theorem eventually_inf_principal {f : filter α} {p : α → Prop} {s : set α} :
(∀ᶠ x in f ⊓ 𝓟 s, p x) ↔ ∀ᶠ x in f, x ∈ s → p x :=
mem_inf_principal
/-! ### Frequently -/
/-- `f.frequently p` or `∃ᶠ x in f, p x` mean that `{x | ¬p x} ∉ f`. E.g., `∃ᶠ x in at_top, p x`
means that there exist arbitrarily large `x` for which `p` holds true. -/
protected def frequently (p : α → Prop) (f : filter α) : Prop := ¬∀ᶠ x in f, ¬p x
notation `∃ᶠ` binders ` in ` f `, ` r:(scoped p, filter.frequently p f) := r
lemma eventually.frequently {f : filter α} [ne_bot f] {p : α → Prop} (h : ∀ᶠ x in f, p x) :
∃ᶠ x in f, p x :=
begin
assume h',
have := h.and h',
simp only [and_not_self, eventually_false_iff_eq_bot] at this,
contradiction
end
lemma frequently_of_forall {f : filter α} [ne_bot f] {p : α → Prop} (h : ∀ x, p x) :
∃ᶠ x in f, p x :=
eventually.frequently (eventually_of_forall h)
lemma frequently.mp {p q : α → Prop} {f : filter α} (h : ∃ᶠ x in f, p x)
(hpq : ∀ᶠ x in f, p x → q x) :
∃ᶠ x in f, q x :=
mt (λ hq, hq.mp $ hpq.mono $ λ x, mt) h
lemma frequently.mono {p q : α → Prop} {f : filter α} (h : ∃ᶠ x in f, p x)
(hpq : ∀ x, p x → q x) :
∃ᶠ x in f, q x :=
h.mp (eventually_of_forall hpq)
lemma frequently.and_eventually {p q : α → Prop} {f : filter α}
(hp : ∃ᶠ x in f, p x) (hq : ∀ᶠ x in f, q x) :
∃ᶠ x in f, p x ∧ q x :=
begin
refine mt (λ h, hq.mp $ h.mono _) hp,
assume x hpq hq hp,
exact hpq ⟨hp, hq⟩
end
lemma frequently.exists {p : α → Prop} {f : filter α} (hp : ∃ᶠ x in f, p x) : ∃ x, p x :=
begin
by_contradiction H,
replace H : ∀ᶠ x in f, ¬ p x, from eventually_of_forall (not_exists.1 H),
exact hp H
end
lemma eventually.exists {p : α → Prop} {f : filter α} [ne_bot f] (hp : ∀ᶠ x in f, p x) :
∃ x, p x :=
hp.frequently.exists
lemma frequently_iff_forall_eventually_exists_and {p : α → Prop} {f : filter α} :
(∃ᶠ x in f, p x) ↔ ∀ {q : α → Prop}, (∀ᶠ x in f, q x) → ∃ x, p x ∧ q x :=
⟨assume hp q hq, (hp.and_eventually hq).exists,
assume H hp, by simpa only [and_not_self, exists_false] using H hp⟩
lemma frequently_iff {f : filter α} {P : α → Prop} :
(∃ᶠ x in f, P x) ↔ ∀ {U}, U ∈ f → ∃ x ∈ U, P x :=
begin
rw frequently_iff_forall_eventually_exists_and,
split ; intro h,
{ intros U U_in,
simpa [exists_prop, and_comm] using h U_in },
{ intros H H',
simpa [and_comm] using h H' },
end
@[simp] lemma not_eventually {p : α → Prop} {f : filter α} :
(¬ ∀ᶠ x in f, p x) ↔ (∃ᶠ x in f, ¬ p x) :=
by simp [filter.frequently]
@[simp] lemma not_frequently {p : α → Prop} {f : filter α} :
(¬ ∃ᶠ x in f, p x) ↔ (∀ᶠ x in f, ¬ p x) :=
by simp only [filter.frequently, not_not]
@[simp] lemma frequently_true_iff_ne_bot (f : filter α) : (∃ᶠ x in f, true) ↔ ne_bot f :=
by simp [filter.frequently, -not_eventually, eventually_false_iff_eq_bot, ne_bot]
@[simp] lemma frequently_false (f : filter α) : ¬ ∃ᶠ x in f, false := by simp
@[simp] lemma frequently_const {f : filter α} [ne_bot f] {p : Prop} :
(∃ᶠ x in f, p) ↔ p :=
classical.by_cases (λ h : p, by simpa [h]) (λ h, by simp [h])
@[simp] lemma frequently_or_distrib {f : filter α} {p q : α → Prop} :
(∃ᶠ x in f, p x ∨ q x) ↔ (∃ᶠ x in f, p x) ∨ (∃ᶠ x in f, q x) :=
by simp only [filter.frequently, ← not_and_distrib, not_or_distrib, eventually_and]
lemma frequently_or_distrib_left {f : filter α} [ne_bot f] {p : Prop} {q : α → Prop} :
(∃ᶠ x in f, p ∨ q x) ↔ (p ∨ ∃ᶠ x in f, q x) :=
by simp
lemma frequently_or_distrib_right {f : filter α} [ne_bot f] {p : α → Prop} {q : Prop} :
(∃ᶠ x in f, p x ∨ q) ↔ (∃ᶠ x in f, p x) ∨ q :=
by simp
@[simp] lemma frequently_imp_distrib {f : filter α} {p q : α → Prop} :
(∃ᶠ x in f, p x → q x) ↔ ((∀ᶠ x in f, p x) → ∃ᶠ x in f, q x) :=
by simp [imp_iff_not_or, not_eventually, frequently_or_distrib]
lemma frequently_imp_distrib_left {f : filter α} [ne_bot f] {p : Prop} {q : α → Prop} :
(∃ᶠ x in f, p → q x) ↔ (p → ∃ᶠ x in f, q x) :=
by simp
lemma frequently_imp_distrib_right {f : filter α} [ne_bot f] {p : α → Prop} {q : Prop} :
(∃ᶠ x in f, p x → q) ↔ ((∀ᶠ x in f, p x) → q) :=
by simp
@[simp] lemma eventually_imp_distrib_right {f : filter α} {p : α → Prop} {q : Prop} :
(∀ᶠ x in f, p x → q) ↔ ((∃ᶠ x in f, p x) → q) :=
by simp only [imp_iff_not_or, eventually_or_distrib_right, not_frequently]
@[simp] lemma frequently_bot {p : α → Prop} : ¬ ∃ᶠ x in ⊥, p x := by simp
@[simp]
lemma frequently_top {p : α → Prop} : (∃ᶠ x in ⊤, p x) ↔ (∃ x, p x) :=
by simp [filter.frequently]
lemma inf_ne_bot_iff_frequently_left {f g : filter α} :
ne_bot (f ⊓ g) ↔ ∀ {p : α → Prop}, (∀ᶠ x in f, p x) → ∃ᶠ x in g, p x :=
begin
rw filter.inf_ne_bot_iff,
split ; intro h,
{ intros U U_in H,
rcases h U_in H with ⟨x, hx, hx'⟩,
exact hx' hx},
{ intros U V U_in V_in,
classical,
by_contra H,
exact h U_in (mem_sets_of_superset V_in $ λ v v_in v_in', H ⟨v, v_in', v_in⟩) }
end
lemma inf_ne_bot_iff_frequently_right {f g : filter α} :
ne_bot (f ⊓ g) ↔ ∀ {p : α → Prop}, (∀ᶠ x in g, p x) → ∃ᶠ x in f, p x :=
by { rw inf_comm, exact filter.inf_ne_bot_iff_frequently_left }
@[simp]
lemma frequently_principal {a : set α} {p : α → Prop} :
(∃ᶠ x in 𝓟 a, p x) ↔ (∃ x ∈ a, p x) :=
by simp [filter.frequently, not_forall]
lemma frequently_sup {p : α → Prop} {f g : filter α} :
(∃ᶠ x in f ⊔ g, p x) ↔ (∃ᶠ x in f, p x) ∨ (∃ᶠ x in g, p x) :=
by simp only [filter.frequently, eventually_sup, not_and_distrib]
@[simp]
lemma frequently_Sup {p : α → Prop} {fs : set (filter α)} :
(∃ᶠ x in Sup fs, p x) ↔ (∃ f ∈ fs, ∃ᶠ x in f, p x) :=
by simp [filter.frequently, -not_eventually, not_forall]
@[simp]
lemma frequently_supr {p : α → Prop} {fs : β → filter α} :
(∃ᶠ x in (⨆ b, fs b), p x) ↔ (∃ b, ∃ᶠ x in fs b, p x) :=
by simp [filter.frequently, -not_eventually, not_forall]
/-!
### Relation “eventually equal”
-/
/-- Two functions `f` and `g` are *eventually equal* along a filter `l` if the set of `x` such that
`f x = g x` belongs to `l`. -/
def eventually_eq (l : filter α) (f g : α → β) : Prop := ∀ᶠ x in l, f x = g x
notation f ` =ᶠ[`:50 l:50 `] `:0 g:50 := eventually_eq l f g
lemma eventually_eq.rw {l : filter α} {f g : α → β} (h : f =ᶠ[l] g) (p : α → β → Prop)
(hf : ∀ᶠ x in l, p x (f x)) :
∀ᶠ x in l, p x (g x) :=
hf.congr $ h.mono $ λ x hx, hx ▸ iff.rfl
lemma eventually_set_ext {s t : set α} {l : filter α} :
s =ᶠ[l] t ↔ ∀ᶠ x in l, x ∈ s ↔ x ∈ t :=
eventually_congr $ eventually_of_forall $ λ x, ⟨eq.to_iff, iff.to_eq⟩
lemma eventually_eq.mem_iff {s t : set α} {l : filter α} (h : s =ᶠ[l] t) :
∀ᶠ x in l, x ∈ s ↔ x ∈ t :=
eventually_set_ext.1 h
lemma eventually_eq.exists_mem {l : filter α} {f g : α → β} (h : f =ᶠ[l] g) :
∃ s ∈ l, ∀ x ∈ s, f x = g x :=
filter.eventually.exists_mem h
lemma eventually_eq_of_mem {l : filter α} {f g : α → β} {s : set α}
(hs : s ∈ l) (h : eq_on f g s) : f =ᶠ[l] g :=
eventually_of_mem hs h
lemma eventually_eq_iff_exists_mem {l : filter α} {f g : α → β} :
(f =ᶠ[l] g) ↔ ∃ s ∈ l, ∀ x ∈ s, f x = g x :=
eventually_iff_exists_mem
@[refl] lemma eventually_eq.refl (l : filter α) (f : α → β) :
f =ᶠ[l] f :=
eventually_of_forall $ λ x, rfl
@[symm] lemma eventually_eq.symm {f g : α → β} {l : filter α} (H : f =ᶠ[l] g) :
g =ᶠ[l] f :=
H.mono $ λ _, eq.symm
@[trans] lemma eventually_eq.trans {f g h : α → β} {l : filter α}
(H₁ : f =ᶠ[l] g) (H₂ : g =ᶠ[l] h) :
f =ᶠ[l] h :=
H₂.rw (λ x y, f x = y) H₁
lemma eventually_eq.prod_mk {l} {f f' : α → β} (hf : f =ᶠ[l] f') {g g' : α → γ} (hg : g =ᶠ[l] g') :
(λ x, (f x, g x)) =ᶠ[l] (λ x, (f' x, g' x)) :=
hf.mp $ hg.mono $ by { intros, simp only * }
lemma eventually_eq.fun_comp {f g : α → β} {l : filter α} (H : f =ᶠ[l] g) (h : β → γ) :
(h ∘ f) =ᶠ[l] (h ∘ g) :=
H.mono $ λ x hx, congr_arg h hx
lemma eventually_eq.comp₂ {δ} {f f' : α → β} {g g' : α → γ} {l} (Hf : f =ᶠ[l] f') (h : β → γ → δ)
(Hg : g =ᶠ[l] g') :
(λ x, h (f x) (g x)) =ᶠ[l] (λ x, h (f' x) (g' x)) :=
(Hf.prod_mk Hg).fun_comp (function.uncurry h)
@[to_additive]
lemma eventually_eq.mul [has_mul β] {f f' g g' : α → β} {l : filter α} (h : f =ᶠ[l] g)
(h' : f' =ᶠ[l] g') :
((λ x, f x * f' x) =ᶠ[l] (λ x, g x * g' x)) :=
h.comp₂ (*) h'
@[to_additive]
lemma eventually_eq.inv [has_inv β] {f g : α → β} {l : filter α} (h : f =ᶠ[l] g) :
((λ x, (f x)⁻¹) =ᶠ[l] (λ x, (g x)⁻¹)) :=
h.fun_comp has_inv.inv
lemma eventually_eq.div [group_with_zero β] {f f' g g' : α → β} {l : filter α} (h : f =ᶠ[l] g)
(h' : f' =ᶠ[l] g') :
((λ x, f x / f' x) =ᶠ[l] (λ x, g x / g' x)) :=
h.mul h'.inv
lemma eventually_eq.sub [add_group β] {f f' g g' : α → β} {l : filter α} (h : f =ᶠ[l] g)
(h' : f' =ᶠ[l] g') :
((λ x, f x - f' x) =ᶠ[l] (λ x, g x - g' x)) :=
h.add h'.neg
section has_le
variables [has_le β] {l : filter α}
/-- A function `f` is eventually less than or equal to a function `g` at a filter `l`. -/
def eventually_le (l : filter α) (f g : α → β) : Prop := ∀ᶠ x in l, f x ≤ g x
notation f ` ≤ᶠ[`:50 l:50 `] `:0 g:50 := eventually_le l f g
lemma eventually_le.congr {f f' g g' : α → β} (H : f ≤ᶠ[l] g) (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') :
f' ≤ᶠ[l] g' :=
H.mp $ hg.mp $ hf.mono $ λ x hf hg H, by rwa [hf, hg] at H
lemma eventually_le_congr {f f' g g' : α → β} (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') :
f ≤ᶠ[l] g ↔ f' ≤ᶠ[l] g' :=
⟨λ H, H.congr hf hg, λ H, H.congr hf.symm hg.symm⟩
end has_le
section preorder
variables [preorder β] {l : filter α} {f g h : α → β}
lemma eventually_eq.le (h : f =ᶠ[l] g) : f ≤ᶠ[l] g := h.mono $ λ x, le_of_eq
@[refl] lemma eventually_le.refl (l : filter α) (f : α → β) :
f ≤ᶠ[l] f :=
(eventually_eq.refl l f).le
@[trans] lemma eventually_le.trans (H₁ : f ≤ᶠ[l] g) (H₂ : g ≤ᶠ[l] h) : f ≤ᶠ[l] h :=
H₂.mp $ H₁.mono $ λ x, le_trans
@[trans] lemma eventually_eq.trans_le (H₁ : f =ᶠ[l] g) (H₂ : g ≤ᶠ[l] h) : f ≤ᶠ[l] h :=
H₁.le.trans H₂
@[trans] lemma eventually_le.trans_eq (H₁ : f ≤ᶠ[l] g) (H₂ : g =ᶠ[l] h) : f ≤ᶠ[l] h :=
H₁.trans H₂.le
end preorder
lemma eventually_le.antisymm [partial_order β] {l : filter α} {f g : α → β}
(h₁ : f ≤ᶠ[l] g) (h₂ : g ≤ᶠ[l] f) :
f =ᶠ[l] g :=
h₂.mp $ h₁.mono $ λ x, le_antisymm
lemma join_le {f : filter (filter α)} {l : filter α} (h : ∀ᶠ m in f, m ≤ l) : join f ≤ l :=
λ s hs, h.mono $ λ m hm, hm hs
/-! ### Push-forwards, pull-backs, and the monad structure -/
section map
/-- The forward map of a filter -/
def map (m : α → β) (f : filter α) : filter β :=
{ sets := preimage m ⁻¹' f.sets,
univ_sets := univ_mem_sets,
sets_of_superset := assume s t hs st, mem_sets_of_superset hs $ preimage_mono st,
inter_sets := assume s t hs ht, inter_mem_sets hs ht }
@[simp] lemma map_principal {s : set α} {f : α → β} :
map f (𝓟 s) = 𝓟 (set.image f s) :=
filter_eq $ set.ext $ assume a, image_subset_iff.symm
variables {f : filter α} {m : α → β} {m' : β → γ} {s : set α} {t : set β}
@[simp] lemma eventually_map {P : β → Prop} :
(∀ᶠ b in map m f, P b) ↔ ∀ᶠ a in f, P (m a) :=
iff.rfl
@[simp] lemma frequently_map {P : β → Prop} :
(∃ᶠ b in map m f, P b) ↔ ∃ᶠ a in f, P (m a) :=
iff.rfl
@[simp] lemma mem_map : t ∈ map m f ↔ {x | m x ∈ t} ∈ f := iff.rfl
lemma image_mem_map (hs : s ∈ f) : m '' s ∈ map m f :=
f.sets_of_superset hs $ subset_preimage_image m s
lemma range_mem_map : range m ∈ map m f :=
by rw ←image_univ; exact image_mem_map univ_mem_sets
lemma mem_map_sets_iff : t ∈ map m f ↔ (∃s∈f, m '' s ⊆ t) :=
iff.intro
(assume ht, ⟨set.preimage m t, ht, image_preimage_subset _ _⟩)
(assume ⟨s, hs, ht⟩, mem_sets_of_superset (image_mem_map hs) ht)
@[simp] lemma map_id : filter.map id f = f :=
filter_eq $ rfl
@[simp] lemma map_compose : filter.map m' ∘ filter.map m = filter.map (m' ∘ m) :=
funext $ assume _, filter_eq $ rfl
@[simp] lemma map_map : filter.map m' (filter.map m f) = filter.map (m' ∘ m) f :=
congr_fun (@@filter.map_compose m m') f
/-- If functions `m₁` and `m₂` are eventually equal at a filter `f`, then
they map this filter to the same filter. -/
lemma map_congr {m₁ m₂ : α → β} {f : filter α} (h : m₁ =ᶠ[f] m₂) :
map m₁ f = map m₂ f :=
filter.ext' $ λ p,
by { simp only [eventually_map], exact eventually_congr (h.mono $ λ x hx, hx ▸ iff.rfl) }
end map
section comap
/-- The inverse map of a filter -/
def comap (m : α → β) (f : filter β) : filter α :=
{ sets := { s | ∃t∈ f, m ⁻¹' t ⊆ s },
univ_sets := ⟨univ, univ_mem_sets, by simp only [subset_univ, preimage_univ]⟩,
sets_of_superset := assume a b ⟨a', ha', ma'a⟩ ab,
⟨a', ha', subset.trans ma'a ab⟩,
inter_sets := assume a b ⟨a', ha₁, ha₂⟩ ⟨b', hb₁, hb₂⟩,
⟨a' ∩ b', inter_mem_sets ha₁ hb₁, inter_subset_inter ha₂ hb₂⟩ }
@[simp] lemma eventually_comap {f : filter β} {φ : α → β} {P : α → Prop} :
(∀ᶠ a in comap φ f, P a) ↔ ∀ᶠ b in f, ∀ a, φ a = b → P a :=
begin
split ; intro h,
{ rcases h with ⟨t, t_in, ht⟩,
apply mem_sets_of_superset t_in,
rintros y y_in _ rfl,
apply ht y_in },
{ exact ⟨_, h, λ _ x_in, x_in _ rfl⟩ }
end
@[simp] lemma frequently_comap {f : filter β} {φ : α → β} {P : α → Prop} :
(∃ᶠ a in comap φ f, P a) ↔ ∃ᶠ b in f, ∃ a, φ a = b ∧ P a :=
begin
classical,
erw [← not_iff_not, not_not, not_not, filter.eventually_comap],
simp only [not_exists, not_and],
end
end comap
/-- The monadic bind operation on filter is defined the usual way in terms of `map` and `join`.
Unfortunately, this `bind` does not result in the expected applicative. See `filter.seq` for the
applicative instance. -/
def bind (f : filter α) (m : α → filter β) : filter β := join (map m f)
/-- The applicative sequentiation operation. This is not induced by the bind operation. -/
def seq (f : filter (α → β)) (g : filter α) : filter β :=
⟨{ s | ∃u∈ f, ∃t∈ g, (∀m∈u, ∀x∈t, (m : α → β) x ∈ s) },
⟨univ, univ_mem_sets, univ, univ_mem_sets, by simp only [forall_prop_of_true, mem_univ, forall_true_iff]⟩,
assume s₀ s₁ ⟨t₀, t₁, h₀, h₁, h⟩ hst, ⟨t₀, t₁, h₀, h₁, assume x hx y hy, hst $ h _ hx _ hy⟩,
assume s₀ s₁ ⟨t₀, ht₀, t₁, ht₁, ht⟩ ⟨u₀, hu₀, u₁, hu₁, hu⟩,
⟨t₀ ∩ u₀, inter_mem_sets ht₀ hu₀, t₁ ∩ u₁, inter_mem_sets ht₁ hu₁,
assume x ⟨hx₀, hx₁⟩ x ⟨hy₀, hy₁⟩, ⟨ht _ hx₀ _ hy₀, hu _ hx₁ _ hy₁⟩⟩⟩
/-- `pure x` is the set of sets that contain `x`. It is equal to `𝓟 {x}` but
with this definition we have `s ∈ pure a` defeq `a ∈ s`. -/
instance : has_pure filter :=
⟨λ (α : Type u) x,
{ sets := {s | x ∈ s},
inter_sets := λ s t, and.intro,
sets_of_superset := λ s t hs hst, hst hs,
univ_sets := trivial }⟩
instance : has_bind filter := ⟨@filter.bind⟩
instance : has_seq filter := ⟨@filter.seq⟩
instance : functor filter := { map := @filter.map }
lemma pure_sets (a : α) : (pure a : filter α).sets = {s | a ∈ s} := rfl
@[simp] lemma mem_pure_sets {a : α} {s : set α} : s ∈ (pure a : filter α) ↔ a ∈ s := iff.rfl
lemma pure_eq_principal (a : α) : (pure a : filter α) = 𝓟 {a} :=
filter.ext $ λ s, by simp only [mem_pure_sets, mem_principal_sets, singleton_subset_iff]
@[simp] lemma map_pure (f : α → β) (a : α) : map f (pure a) = pure (f a) :=
filter.ext $ λ s, iff.rfl
@[simp] lemma join_pure (f : filter α) : join (pure f) = f := filter.ext $ λ s, iff.rfl
@[simp] lemma pure_bind (a : α) (m : α → filter β) :
bind (pure a) m = m a :=
by simp only [has_bind.bind, bind, map_pure, join_pure]
section
-- this section needs to be before applicative, otherwise the wrong instance will be chosen
/-- The monad structure on filters. -/
protected def monad : monad filter := { map := @filter.map }
local attribute [instance] filter.monad
protected lemma is_lawful_monad : is_lawful_monad filter :=
{ id_map := assume α f, filter_eq rfl,
pure_bind := assume α β, pure_bind,
bind_assoc := assume α β γ f m₁ m₂, filter_eq rfl,
bind_pure_comp_eq_map := assume α β f x, filter.ext $ λ s,
by simp only [has_bind.bind, bind, functor.map, mem_map, mem_join_sets, mem_set_of_eq,
function.comp, mem_pure_sets] }
end
instance : applicative filter := { map := @filter.map, seq := @filter.seq }
instance : alternative filter :=
{ failure := λα, ⊥,
orelse := λα x y, x ⊔ y }
@[simp] lemma map_def {α β} (m : α → β) (f : filter α) : m <$> f = map m f := rfl
@[simp] lemma bind_def {α β} (f : filter α) (m : α → filter β) : f >>= m = bind f m := rfl
/- map and comap equations -/
section map
variables {f f₁ f₂ : filter α} {g g₁ g₂ : filter β} {m : α → β} {m' : β → γ} {s : set α} {t : set β}
@[simp] theorem mem_comap_sets : s ∈ comap m g ↔ ∃t∈ g, m ⁻¹' t ⊆ s := iff.rfl
theorem preimage_mem_comap (ht : t ∈ g) : m ⁻¹' t ∈ comap m g :=
⟨t, ht, subset.refl _⟩
lemma comap_id : comap id f = f :=
le_antisymm (assume s, preimage_mem_comap) (assume s ⟨t, ht, hst⟩, mem_sets_of_superset ht hst)
lemma comap_const_of_not_mem {x : α} {f : filter α} {V : set α} (hV : V ∈ f) (hx : x ∉ V) :
comap (λ y : α, x) f = ⊥ :=
begin
ext W,
suffices : ∃ t ∈ f, (λ (y : α), x) ⁻¹' t ⊆ W, by simpa,
use [V, hV],
simp [preimage_const_of_not_mem hx],
end
lemma comap_const_of_mem {x : α} {f : filter α} (h : ∀ V ∈ f, x ∈ V) : comap (λ y : α, x) f = ⊤ :=
begin
ext W,
suffices : (∃ (t : set α), t ∈ f.sets ∧ (λ (y : α), x) ⁻¹' t ⊆ W) ↔ W = univ,
by simpa,
split,
{ rintros ⟨V, V_in, hW⟩,
simpa [preimage_const_of_mem (h V V_in), univ_subset_iff] using hW },
{ rintro rfl,
use univ,
simp [univ_mem_sets] },
end
lemma comap_comap {m : γ → β} {n : β → α} : comap m (comap n f) = comap (n ∘ m) f :=
le_antisymm
(assume c ⟨b, hb, (h : preimage (n ∘ m) b ⊆ c)⟩, ⟨preimage n b, preimage_mem_comap hb, h⟩)
(assume c ⟨b, ⟨a, ha, (h₁ : preimage n a ⊆ b)⟩, (h₂ : preimage m b ⊆ c)⟩,
⟨a, ha, show preimage m (preimage n a) ⊆ c, from subset.trans (preimage_mono h₁) h₂⟩)
@[simp] theorem comap_principal {t : set β} : comap m (𝓟 t) = 𝓟 (m ⁻¹' t) :=
filter_eq $ set.ext $ assume s,
⟨assume ⟨u, (hu : t ⊆ u), (b : preimage m u ⊆ s)⟩, subset.trans (preimage_mono hu) b,
assume : preimage m t ⊆ s, ⟨t, subset.refl t, this⟩⟩
lemma map_le_iff_le_comap : map m f ≤ g ↔ f ≤ comap m g :=
⟨assume h s ⟨t, ht, hts⟩, mem_sets_of_superset (h ht) hts, assume h s ht, h ⟨_, ht, subset.refl _⟩⟩
lemma gc_map_comap (m : α → β) : galois_connection (map m) (comap m) :=
assume f g, map_le_iff_le_comap
@[mono] lemma map_mono : monotone (map m) := (gc_map_comap m).monotone_l
@[mono] lemma comap_mono : monotone (comap m) := (gc_map_comap m).monotone_u
@[simp] lemma map_bot : map m ⊥ = ⊥ := (gc_map_comap m).l_bot
@[simp] lemma map_sup : map m (f₁ ⊔ f₂) = map m f₁ ⊔ map m f₂ := (gc_map_comap m).l_sup
@[simp] lemma map_supr {f : ι → filter α} : map m (⨆i, f i) = (⨆i, map m (f i)) :=
(gc_map_comap m).l_supr
@[simp] lemma comap_top : comap m ⊤ = ⊤ := (gc_map_comap m).u_top
@[simp] lemma comap_inf : comap m (g₁ ⊓ g₂) = comap m g₁ ⊓ comap m g₂ := (gc_map_comap m).u_inf
@[simp] lemma comap_infi {f : ι → filter β} : comap m (⨅i, f i) = (⨅i, comap m (f i)) :=
(gc_map_comap m).u_infi
lemma le_comap_top (f : α → β) (l : filter α) : l ≤ comap f ⊤ :=
by rw [comap_top]; exact le_top
lemma map_comap_le : map m (comap m g) ≤ g := (gc_map_comap m).l_u_le _
lemma le_comap_map : f ≤ comap m (map m f) := (gc_map_comap m).le_u_l _
@[simp] lemma comap_bot : comap m ⊥ = ⊥ :=
bot_unique $ assume s _, ⟨∅, by simp only [mem_bot_sets], by simp only [empty_subset, preimage_empty]⟩
lemma comap_supr {ι} {f : ι → filter β} {m : α → β} :
comap m (supr f) = (⨆i, comap m (f i)) :=
le_antisymm
(assume s hs,
have ∀i, ∃t, t ∈ f i ∧ m ⁻¹' t ⊆ s, by simpa only [mem_comap_sets, exists_prop, mem_supr_sets] using mem_supr_sets.1 hs,
let ⟨t, ht⟩ := classical.axiom_of_choice this in
⟨⋃i, t i, mem_supr_sets.2 $ assume i, (f i).sets_of_superset (ht i).1 (subset_Union _ _),
begin
rw [preimage_Union, Union_subset_iff],
assume i,
exact (ht i).2
end⟩)
(supr_le $ assume i, comap_mono $ le_supr _ _)
lemma comap_Sup {s : set (filter β)} {m : α → β} : comap m (Sup s) = (⨆f∈s, comap m f) :=
by simp only [Sup_eq_supr, comap_supr, eq_self_iff_true]
lemma comap_sup : comap m (g₁ ⊔ g₂) = comap m g₁ ⊔ comap m g₂ :=
le_antisymm
(assume s ⟨⟨t₁, ht₁, hs₁⟩, ⟨t₂, ht₂, hs₂⟩⟩,
⟨t₁ ∪ t₂,
⟨g₁.sets_of_superset ht₁ (subset_union_left _ _), g₂.sets_of_superset ht₂ (subset_union_right _ _)⟩,
union_subset hs₁ hs₂⟩)
((@comap_mono _ _ m).le_map_sup _ _)
lemma map_comap {f : filter β} {m : α → β} (hf : range m ∈ f) : (f.comap m).map m = f :=
le_antisymm
map_comap_le
(assume t' ⟨t, ht, sub⟩, by filter_upwards [ht, hf]; rintros x hxt ⟨y, rfl⟩; exact sub hxt)
lemma comap_map {f : filter α} {m : α → β} (h : ∀ x y, m x = m y → x = y) :
comap m (map m f) = f :=
have ∀s, preimage m (image m s) = s,
from assume s, preimage_image_eq s h,
le_antisymm
(assume s hs, ⟨
image m s,
f.sets_of_superset hs $ by simp only [this, subset.refl],
by simp only [this, subset.refl]⟩)
le_comap_map
lemma le_of_map_le_map_inj' {f g : filter α} {m : α → β} {s : set α}
(hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y)
(h : map m f ≤ map m g) : f ≤ g :=
assume t ht, by filter_upwards [hsf, h $ image_mem_map (inter_mem_sets hsg ht)]
assume a has ⟨b, ⟨hbs, hb⟩, h⟩,
have b = a, from hm _ hbs _ has h,
this ▸ hb
lemma le_of_map_le_map_inj_iff {f g : filter α} {m : α → β} {s : set α}
(hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) :
map m f ≤ map m g ↔ f ≤ g :=
iff.intro (le_of_map_le_map_inj' hsf hsg hm) (λ h, map_mono h)
lemma eq_of_map_eq_map_inj' {f g : filter α} {m : α → β} {s : set α}
(hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y)
(h : map m f = map m g) : f = g :=
le_antisymm
(le_of_map_le_map_inj' hsf hsg hm $ le_of_eq h)
(le_of_map_le_map_inj' hsg hsf hm $ le_of_eq h.symm)
lemma map_inj {f g : filter α} {m : α → β} (hm : ∀ x y, m x = m y → x = y) (h : map m f = map m g) :
f = g :=
have comap m (map m f) = comap m (map m g), by rw h,
by rwa [comap_map hm, comap_map hm] at this
theorem le_map_comap_of_surjective' {f : α → β} {l : filter β} {u : set β} (ul : u ∈ l)
(hf : ∀ y ∈ u, ∃ x, f x = y) :
l ≤ map f (comap f l) :=
assume s ⟨t, tl, ht⟩,
have t ∩ u ⊆ s, from
assume x ⟨xt, xu⟩,
exists.elim (hf x xu) $ λ a faeq,
by { rw ←faeq, apply ht, change f a ∈ t, rw faeq, exact xt },
mem_sets_of_superset (inter_mem_sets tl ul) this
theorem map_comap_of_surjective' {f : α → β} {l : filter β} {u : set β} (ul : u ∈ l)
(hf : ∀ y ∈ u, ∃ x, f x = y) :
map f (comap f l) = l :=
le_antisymm map_comap_le (le_map_comap_of_surjective' ul hf)
theorem le_map_comap_of_surjective {f : α → β} (hf : function.surjective f) (l : filter β) :
l ≤ map f (comap f l) :=
le_map_comap_of_surjective' univ_mem_sets (λ y _, hf y)
theorem map_comap_of_surjective {f : α → β} (hf : function.surjective f) (l : filter β) :
map f (comap f l) = l :=
le_antisymm map_comap_le (le_map_comap_of_surjective hf l)
lemma subtype_coe_map_comap (s : set α) (f : filter α) :
map (coe : s → α) (comap (coe : s → α) f) = f ⊓ 𝓟 s :=
begin
apply le_antisymm,
{ rw [map_le_iff_le_comap, comap_inf, comap_principal],
have : (coe : s → α) ⁻¹' s = univ, by { ext x, simp },
rw [this, principal_univ],
simp [le_refl _] },
{ intros V V_in,
rcases V_in with ⟨W, W_in, H⟩,
rw mem_inf_sets,
use [W, W_in, s, mem_principal_self s],
erw [← image_subset_iff, subtype.image_preimage_coe] at H,
exact H }
end
lemma subtype_coe_map_comap_prod (s : set α) (f : filter (α × α)) :
map (coe : s × s → α × α) (comap (coe : s × s → α × α) f) = f ⊓ 𝓟 (s.prod s) :=
let φ (x : s × s) : s.prod s := ⟨⟨x.1.1, x.2.1⟩, ⟨x.1.2, x.2.2⟩⟩ in
begin
rw show (coe : s × s → α × α) = coe ∘ φ, by ext x; cases x; refl,
rw [← filter.map_map, ← filter.comap_comap],
rw map_comap_of_surjective,
exact subtype_coe_map_comap _ _,
exact λ ⟨⟨a, b⟩, ⟨ha, hb⟩⟩, ⟨⟨⟨a, ha⟩, ⟨b, hb⟩⟩, rfl⟩
end
lemma comap_ne_bot_iff {f : filter β} {m : α → β} : ne_bot (comap m f) ↔ ∀ t ∈ f, ∃ a, m a ∈ t :=
begin
rw ← forall_sets_nonempty_iff_ne_bot,
exact ⟨λ h t t_in, h (m ⁻¹' t) ⟨t, t_in, subset.refl _⟩,
λ h s ⟨u, u_in, hu⟩, let ⟨x, hx⟩ := h u u_in in ⟨x, hu hx⟩⟩,
end
lemma comap_ne_bot {f : filter β} {m : α → β} (hm : ∀t∈ f, ∃a, m a ∈ t) : ne_bot (comap m f) :=
comap_ne_bot_iff.mpr hm
lemma ne_bot.comap_of_range_mem {f : filter β} {m : α → β}
(hf : ne_bot f) (hm : range m ∈ f) : ne_bot (comap m f) :=
comap_ne_bot $ assume t ht,
let ⟨_, ha, a, rfl⟩ := hf.nonempty_of_mem (inter_mem_sets ht hm)
in ⟨a, ha⟩
lemma comap_inf_principal_ne_bot_of_image_mem {f : filter β} {m : α → β}
(hf : ne_bot f) {s : set α} (hs : m '' s ∈ f) :
ne_bot (comap m f ⊓ 𝓟 s) :=
begin
refine compl_compl s ▸ mt mem_sets_of_eq_bot _,
rintros ⟨t, ht, hts⟩,
rcases hf.nonempty_of_mem (inter_mem_sets hs ht) with ⟨_, ⟨x, hxs, rfl⟩, hxt⟩,
exact absurd hxs (hts hxt)
end
lemma ne_bot.comap_of_surj {f : filter β} {m : α → β}
(hf : ne_bot f) (hm : function.surjective m) :
ne_bot (comap m f) :=
hf.comap_of_range_mem $ univ_mem_sets' hm
lemma ne_bot.comap_of_image_mem {f : filter β} {m : α → β} (hf : ne_bot f)
{s : set α} (hs : m '' s ∈ f) :
ne_bot (comap m f) :=
hf.comap_of_range_mem $ mem_sets_of_superset hs (image_subset_range _ _)
@[simp] lemma map_eq_bot_iff : map m f = ⊥ ↔ f = ⊥ :=
⟨by rw [←empty_in_sets_eq_bot, ←empty_in_sets_eq_bot]; exact id,
assume h, by simp only [h, eq_self_iff_true, map_bot]⟩
lemma map_ne_bot_iff (f : α → β) {F : filter α} : ne_bot (map f F) ↔ ne_bot F :=
not_congr map_eq_bot_iff
lemma ne_bot.map (hf : ne_bot f) (m : α → β) : ne_bot (map m f) :=
(map_ne_bot_iff m).2 hf
instance map_ne_bot [hf : ne_bot f] : ne_bot (f.map m) := hf.map m
lemma sInter_comap_sets (f : α → β) (F : filter β) :
⋂₀(comap f F).sets = ⋂ U ∈ F, f ⁻¹' U :=
begin
ext x,
suffices : (∀ (A : set α) (B : set β), B ∈ F → f ⁻¹' B ⊆ A → x ∈ A) ↔
∀ (B : set β), B ∈ F → f x ∈ B,
by simp only [mem_sInter, mem_Inter, mem_comap_sets, this, and_imp, mem_comap_sets, exists_prop, mem_sInter,
iff_self, mem_Inter, mem_preimage, exists_imp_distrib],
split,
{ intros h U U_in,
simpa only [set.subset.refl, forall_prop_of_true, mem_preimage] using h (f ⁻¹' U) U U_in },
{ intros h V U U_in f_U_V,
exact f_U_V (h U U_in) },
end
end map
-- this is a generic rule for monotone functions:
lemma map_infi_le {f : ι → filter α} {m : α → β} :
map m (infi f) ≤ (⨅ i, map m (f i)) :=
le_infi $ assume i, map_mono $ infi_le _ _
lemma map_infi_eq {f : ι → filter α} {m : α → β} (hf : directed (≥) f) [nonempty ι] :
map m (infi f) = (⨅ i, map m (f i)) :=
le_antisymm
map_infi_le
(assume s (hs : preimage m s ∈ infi f),
have ∃i, preimage m s ∈ f i,
by simp only [infi_sets_eq hf, mem_Union] at hs; assumption,
let ⟨i, hi⟩ := this in
have (⨅ i, map m (f i)) ≤ 𝓟 s, from
infi_le_of_le i $ by simp only [le_principal_iff, mem_map]; assumption,
by simp only [filter.le_principal_iff] at this; assumption)
lemma map_binfi_eq {ι : Type w} {f : ι → filter α} {m : α → β} {p : ι → Prop}
(h : directed_on (f ⁻¹'o (≥)) {x | p x}) (ne : ∃i, p i) :
map m (⨅i (h : p i), f i) = (⨅i (h: p i), map m (f i)) :=
begin
haveI := nonempty_subtype.2 ne,
simp only [infi_subtype'],
exact map_infi_eq h.directed_coe
end
lemma map_inf_le {f g : filter α} {m : α → β} : map m (f ⊓ g) ≤ map m f ⊓ map m g :=
(@map_mono _ _ m).map_inf_le f g
lemma map_inf' {f g : filter α} {m : α → β} {t : set α} (htf : t ∈ f) (htg : t ∈ g)
(h : ∀x∈t, ∀y∈t, m x = m y → x = y) : map m (f ⊓ g) = map m f ⊓ map m g :=
begin
refine le_antisymm map_inf_le (assume s hs, _),
simp only [map, mem_inf_sets, exists_prop, mem_map, mem_preimage, mem_inf_sets] at hs ⊢,
rcases hs with ⟨t₁, h₁, t₂, h₂, hs⟩,
refine ⟨m '' (t₁ ∩ t), _, m '' (t₂ ∩ t), _, _⟩,
{ filter_upwards [h₁, htf] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ },
{ filter_upwards [h₂, htg] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ },
{ rw [image_inter_on],
{ refine image_subset_iff.2 _,
exact λ x ⟨⟨h₁, _⟩, h₂, _⟩, hs ⟨h₁, h₂⟩ },
{ exact λ x ⟨_, hx⟩ y ⟨_, hy⟩, h x hx y hy } }
end
lemma map_inf {f g : filter α} {m : α → β} (h : function.injective m) :
map m (f ⊓ g) = map m f ⊓ map m g :=
map_inf' univ_mem_sets univ_mem_sets (assume x _ y _ hxy, h hxy)
lemma map_eq_comap_of_inverse {f : filter α} {m : α → β} {n : β → α}
(h₁ : m ∘ n = id) (h₂ : n ∘ m = id) : map m f = comap n f :=
le_antisymm
(assume b ⟨a, ha, (h : preimage n a ⊆ b)⟩, f.sets_of_superset ha $
calc a = preimage (n ∘ m) a : by simp only [h₂, preimage_id, eq_self_iff_true]
... ⊆ preimage m b : preimage_mono h)
(assume b (hb : preimage m b ∈ f),
⟨preimage m b, hb, show preimage (m ∘ n) b ⊆ b, by simp only [h₁]; apply subset.refl⟩)
lemma map_swap_eq_comap_swap {f : filter (α × β)} : prod.swap <$> f = comap prod.swap f :=
map_eq_comap_of_inverse prod.swap_swap_eq prod.swap_swap_eq
lemma le_map {f : filter α} {m : α → β} {g : filter β} (h : ∀s∈ f, m '' s ∈ g) :
g ≤ f.map m :=
assume s hs, mem_sets_of_superset (h _ hs) $ image_preimage_subset _ _
protected lemma push_pull (f : α → β) (F : filter α) (G : filter β) :
map f (F ⊓ comap f G) = map f F ⊓ G :=
begin
apply le_antisymm,
{ calc map f (F ⊓ comap f G) ≤ map f F ⊓ (map f $ comap f G) : map_inf_le
... ≤ map f F ⊓ G : inf_le_inf_left (map f F) map_comap_le },
{ rintros U ⟨V, V_in, W, ⟨Z, Z_in, hZ⟩, h⟩,
rw ← image_subset_iff at h,
use [f '' V, image_mem_map V_in, Z, Z_in],
refine subset.trans _ h,
have : f '' (V ∩ f ⁻¹' Z) ⊆ f '' (V ∩ W),
from image_subset _ (inter_subset_inter_right _ ‹_›),
rwa set.push_pull at this }
end
protected lemma push_pull' (f : α → β) (F : filter α) (G : filter β) :
map f (comap f G ⊓ F) = G ⊓ map f F :=
by simp only [filter.push_pull, inf_comm]
section applicative
lemma singleton_mem_pure_sets {a : α} : {a} ∈ (pure a : filter α) :=
mem_singleton a
lemma pure_injective : function.injective (pure : α → filter α) :=
assume a b hab, (filter.ext_iff.1 hab {x | a = x}).1 rfl
instance pure_ne_bot {α : Type u} {a : α} : ne_bot (pure a) :=
mt empty_in_sets_eq_bot.2 $ not_mem_empty a
@[simp] lemma le_pure_iff {f : filter α} {a : α} : f ≤ pure a ↔ {a} ∈ f :=
⟨λ h, h singleton_mem_pure_sets,
λ h s hs, mem_sets_of_superset h $ singleton_subset_iff.2 hs⟩
lemma mem_seq_sets_def {f : filter (α → β)} {g : filter α} {s : set β} :
s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, ∀x∈u, ∀y∈t, (x : α → β) y ∈ s) :=
iff.rfl
lemma mem_seq_sets_iff {f : filter (α → β)} {g : filter α} {s : set β} :
s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, set.seq u t ⊆ s) :=
by simp only [mem_seq_sets_def, seq_subset, exists_prop, iff_self]
lemma mem_map_seq_iff {f : filter α} {g : filter β} {m : α → β → γ} {s : set γ} :
s ∈ (f.map m).seq g ↔ (∃t u, t ∈ g ∧ u ∈ f ∧ ∀x∈u, ∀y∈t, m x y ∈ s) :=
iff.intro
(assume ⟨t, ht, s, hs, hts⟩, ⟨s, m ⁻¹' t, hs, ht, assume a, hts _⟩)
(assume ⟨t, s, ht, hs, hts⟩, ⟨m '' s, image_mem_map hs, t, ht, assume f ⟨a, has, eq⟩, eq ▸ hts _ has⟩)
lemma seq_mem_seq_sets {f : filter (α → β)} {g : filter α} {s : set (α → β)} {t : set α}
(hs : s ∈ f) (ht : t ∈ g) : s.seq t ∈ f.seq g :=
⟨s, hs, t, ht, assume f hf a ha, ⟨f, hf, a, ha, rfl⟩⟩
lemma le_seq {f : filter (α → β)} {g : filter α} {h : filter β}
(hh : ∀t ∈ f, ∀u ∈ g, set.seq t u ∈ h) : h ≤ seq f g :=
assume s ⟨t, ht, u, hu, hs⟩, mem_sets_of_superset (hh _ ht _ hu) $
assume b ⟨m, hm, a, ha, eq⟩, eq ▸ hs _ hm _ ha
@[mono] lemma seq_mono {f₁ f₂ : filter (α → β)} {g₁ g₂ : filter α}
(hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.seq g₁ ≤ f₂.seq g₂ :=
le_seq $ assume s hs t ht, seq_mem_seq_sets (hf hs) (hg ht)
@[simp] lemma pure_seq_eq_map (g : α → β) (f : filter α) : seq (pure g) f = f.map g :=
begin
refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _),
{ rw ← singleton_seq, apply seq_mem_seq_sets _ hs,
exact singleton_mem_pure_sets },
{ refine sets_of_superset (map g f) (image_mem_map ht) _,
rintros b ⟨a, ha, rfl⟩, exact ⟨g, hs, a, ha, rfl⟩ }
end
@[simp] lemma seq_pure (f : filter (α → β)) (a : α) : seq f (pure a) = map (λg:α → β, g a) f :=
begin
refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _),
{ rw ← seq_singleton,
exact seq_mem_seq_sets hs singleton_mem_pure_sets },
{ refine sets_of_superset (map (λg:α→β, g a) f) (image_mem_map hs) _,
rintros b ⟨g, hg, rfl⟩, exact ⟨g, hg, a, ht, rfl⟩ }
end
@[simp] lemma seq_assoc (x : filter α) (g : filter (α → β)) (h : filter (β → γ)) :
seq h (seq g x) = seq (seq (map (∘) h) g) x :=
begin
refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _),
{ rcases mem_seq_sets_iff.1 hs with ⟨u, hu, v, hv, hs⟩,
rcases mem_map_sets_iff.1 hu with ⟨w, hw, hu⟩,
refine mem_sets_of_superset _
(set.seq_mono (subset.trans (set.seq_mono hu (subset.refl _)) hs) (subset.refl _)),
rw ← set.seq_seq,
exact seq_mem_seq_sets hw (seq_mem_seq_sets hv ht) },
{ rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩,
refine mem_sets_of_superset _ (set.seq_mono (subset.refl _) ht),
rw set.seq_seq,
exact seq_mem_seq_sets (seq_mem_seq_sets (image_mem_map hs) hu) hv }
end
lemma prod_map_seq_comm (f : filter α) (g : filter β) :
(map prod.mk f).seq g = seq (map (λb a, (a, b)) g) f :=
begin
refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _),
{ rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩,
refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)),
rw ← set.prod_image_seq_comm,
exact seq_mem_seq_sets (image_mem_map ht) hu },
{ rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩,
refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)),
rw set.prod_image_seq_comm,
exact seq_mem_seq_sets (image_mem_map ht) hu }
end
instance : is_lawful_functor (filter : Type u → Type u) :=
{ id_map := assume α f, map_id,
comp_map := assume α β γ f g a, map_map.symm }
instance : is_lawful_applicative (filter : Type u → Type u) :=
{ pure_seq_eq_map := assume α β, pure_seq_eq_map,
map_pure := assume α β, map_pure,
seq_pure := assume α β, seq_pure,
seq_assoc := assume α β γ, seq_assoc }
instance : is_comm_applicative (filter : Type u → Type u) :=
⟨assume α β f g, prod_map_seq_comm f g⟩
lemma {l} seq_eq_filter_seq {α β : Type l} (f : filter (α → β)) (g : filter α) :
f <*> g = seq f g := rfl
end applicative
/- bind equations -/
section bind
@[simp] lemma eventually_bind {f : filter α} {m : α → filter β} {p : β → Prop} :
(∀ᶠ y in bind f m, p y) ↔ ∀ᶠ x in f, ∀ᶠ y in m x, p y :=
iff.rfl
@[simp] lemma eventually_eq_bind {f : filter α} {m : α → filter β} {g₁ g₂ : β → γ} :
(g₁ =ᶠ[bind f m] g₂) ↔ ∀ᶠ x in f, g₁ =ᶠ[m x] g₂ :=
iff.rfl
@[simp] lemma eventually_le_bind [has_le γ] {f : filter α} {m : α → filter β} {g₁ g₂ : β → γ} :
(g₁ ≤ᶠ[bind f m] g₂) ↔ ∀ᶠ x in f, g₁ ≤ᶠ[m x] g₂ :=
iff.rfl
lemma mem_bind_sets' {s : set β} {f : filter α} {m : α → filter β} :
s ∈ bind f m ↔ {a | s ∈ m a} ∈ f :=
iff.rfl
@[simp] lemma mem_bind_sets {s : set β} {f : filter α} {m : α → filter β} :
s ∈ bind f m ↔ ∃t ∈ f, ∀x ∈ t, s ∈ m x :=
calc s ∈ bind f m ↔ {a | s ∈ m a} ∈ f : iff.rfl
... ↔ (∃t ∈ f, t ⊆ {a | s ∈ m a}) : exists_sets_subset_iff.symm
... ↔ (∃t ∈ f, ∀x ∈ t, s ∈ m x) : iff.rfl
lemma bind_le {f : filter α} {g : α → filter β} {l : filter β} (h : ∀ᶠ x in f, g x ≤ l) :
f.bind g ≤ l :=
join_le $ eventually_map.2 h
@[mono] lemma bind_mono {f₁ f₂ : filter α} {g₁ g₂ : α → filter β} (hf : f₁ ≤ f₂)
(hg : g₁ ≤ᶠ[f₁] g₂) :
bind f₁ g₁ ≤ bind f₂ g₂ :=
begin
refine le_trans (λ s hs, _) (join_mono $ map_mono hf),
simp only [mem_join_sets, mem_bind_sets', mem_map] at hs ⊢,
filter_upwards [hg, hs],
exact λ x hx hs, hx hs
end
lemma bind_inf_principal {f : filter α} {g : α → filter β} {s : set β} :
f.bind (λ x, g x ⊓ 𝓟 s) = (f.bind g) ⊓ 𝓟 s :=
filter.ext $ λ s, by simp only [mem_bind_sets, mem_inf_principal]
lemma sup_bind {f g : filter α} {h : α → filter β} :
bind (f ⊔ g) h = bind f h ⊔ bind g h :=
by simp only [bind, sup_join, map_sup, eq_self_iff_true]
lemma principal_bind {s : set α} {f : α → filter β} :
(bind (𝓟 s) f) = (⨆x ∈ s, f x) :=
show join (map f (𝓟 s)) = (⨆x ∈ s, f x),
by simp only [Sup_image, join_principal_eq_Sup, map_principal, eq_self_iff_true]
end bind
section list_traverse
/- This is a separate section in order to open `list`, but mostly because of universe
equality requirements in `traverse` -/
open list
lemma sequence_mono :
∀(as bs : list (filter α)), forall₂ (≤) as bs → sequence as ≤ sequence bs
| [] [] forall₂.nil := le_refl _
| (a::as) (b::bs) (forall₂.cons h hs) := seq_mono (map_mono h) (sequence_mono as bs hs)
variables {α' β' γ' : Type u} {f : β' → filter α'} {s : γ' → set α'}
lemma mem_traverse_sets :
∀(fs : list β') (us : list γ'),
forall₂ (λb c, s c ∈ f b) fs us → traverse s us ∈ traverse f fs
| [] [] forall₂.nil := mem_pure_sets.2 $ mem_singleton _
| (f::fs) (u::us) (forall₂.cons h hs) := seq_mem_seq_sets (image_mem_map h) (mem_traverse_sets fs us hs)
lemma mem_traverse_sets_iff (fs : list β') (t : set (list α')) :
t ∈ traverse f fs ↔
(∃us:list (set α'), forall₂ (λb (s : set α'), s ∈ f b) fs us ∧ sequence us ⊆ t) :=
begin
split,
{ induction fs generalizing t,
case nil { simp only [sequence, mem_pure_sets, imp_self, forall₂_nil_left_iff,
exists_eq_left, set.pure_def, singleton_subset_iff, traverse_nil] },
case cons : b fs ih t {
assume ht,
rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩,
rcases mem_map_sets_iff.1 hu with ⟨w, hw, hwu⟩,
rcases ih v hv with ⟨us, hus, hu⟩,
exact ⟨w :: us, forall₂.cons hw hus, subset.trans (set.seq_mono hwu hu) ht⟩ } },
{ rintros ⟨us, hus, hs⟩,
exact mem_sets_of_superset (mem_traverse_sets _ _ hus) hs }
end
end list_traverse
/-! ### Limits -/
/-- `tendsto` is the generic "limit of a function" predicate.
`tendsto f l₁ l₂` asserts that for every `l₂` neighborhood `a`,
the `f`-preimage of `a` is an `l₁` neighborhood. -/
def tendsto (f : α → β) (l₁ : filter α) (l₂ : filter β) := l₁.map f ≤ l₂
lemma tendsto_def {f : α → β} {l₁ : filter α} {l₂ : filter β} :
tendsto f l₁ l₂ ↔ ∀ s ∈ l₂, f ⁻¹' s ∈ l₁ := iff.rfl
lemma tendsto.eventually {f : α → β} {l₁ : filter α} {l₂ : filter β} {p : β → Prop}
(hf : tendsto f l₁ l₂) (h : ∀ᶠ y in l₂, p y) :
∀ᶠ x in l₁, p (f x) :=
hf h
@[simp] lemma tendsto_bot {f : α → β} {l : filter β} : tendsto f ⊥ l := by simp [tendsto]
lemma tendsto_of_not_nonempty {f : α → β} {la : filter α} {lb : filter β} (h : ¬nonempty α) :
tendsto f la lb :=
by simp only [filter_eq_bot_of_not_nonempty la h, tendsto_bot]
lemma eventually_eq_of_left_inv_of_right_inv {f : α → β} {g₁ g₂ : β → α} {fa : filter α}
{fb : filter β} (hleft : ∀ᶠ x in fa, g₁ (f x) = x) (hright : ∀ᶠ y in fb, f (g₂ y) = y)
(htendsto : tendsto g₂ fb fa) :
g₁ =ᶠ[fb] g₂ :=
(htendsto.eventually hleft).mp $ hright.mono $ λ y hr hl, (congr_arg g₁ hr.symm).trans hl
lemma tendsto_iff_comap {f : α → β} {l₁ : filter α} {l₂ : filter β} :
tendsto f l₁ l₂ ↔ l₁ ≤ l₂.comap f :=
map_le_iff_le_comap
lemma tendsto_congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (hl : f₁ =ᶠ[l₁] f₂) :
tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ :=
by rw [tendsto, tendsto, map_congr hl]
lemma tendsto.congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β}
(hl : f₁ =ᶠ[l₁] f₂) (h : tendsto f₁ l₁ l₂) : tendsto f₂ l₁ l₂ :=
(tendsto_congr' hl).1 h
theorem tendsto_congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β}
(h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ :=
tendsto_congr' (univ_mem_sets' h)
theorem tendsto.congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β}
(h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ → tendsto f₂ l₁ l₂ :=
(tendsto_congr h).1
lemma tendsto_id' {x y : filter α} : x ≤ y → tendsto id x y :=
by simp only [tendsto, map_id, forall_true_iff] {contextual := tt}
lemma tendsto_id {x : filter α} : tendsto id x x := tendsto_id' $ le_refl x
lemma tendsto.comp {f : α → β} {g : β → γ} {x : filter α} {y : filter β} {z : filter γ}
(hg : tendsto g y z) (hf : tendsto f x y) : tendsto (g ∘ f) x z :=
calc map (g ∘ f) x = map g (map f x) : by rw [map_map]
... ≤ map g y : map_mono hf
... ≤ z : hg
lemma tendsto_le_left {f : α → β} {x y : filter α} {z : filter β}
(h : y ≤ x) : tendsto f x z → tendsto f y z :=
le_trans (map_mono h)
lemma tendsto_le_right {f : α → β} {x : filter α} {y z : filter β}
(h₁ : y ≤ z) (h₂ : tendsto f x y) : tendsto f x z :=
le_trans h₂ h₁
lemma tendsto.ne_bot {f : α → β} {x : filter α} {y : filter β} (h : tendsto f x y) [hx : ne_bot x] :
ne_bot y :=
(hx.map _).mono h
lemma tendsto_map {f : α → β} {x : filter α} : tendsto f x (map f x) := le_refl (map f x)
lemma tendsto_map' {f : β → γ} {g : α → β} {x : filter α} {y : filter γ}
(h : tendsto (f ∘ g) x y) : tendsto f (map g x) y :=
by rwa [tendsto, map_map]
lemma tendsto_map'_iff {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} :
tendsto f (map g x) y ↔ tendsto (f ∘ g) x y :=
by rw [tendsto, map_map]; refl
lemma tendsto_comap {f : α → β} {x : filter β} : tendsto f (comap f x) x :=
map_comap_le
lemma tendsto_comap_iff {f : α → β} {g : β → γ} {a : filter α} {c : filter γ} :
tendsto f a (c.comap g) ↔ tendsto (g ∘ f) a c :=
⟨assume h, tendsto_comap.comp h, assume h, map_le_iff_le_comap.mp $ by rwa [map_map]⟩
lemma tendsto_comap'_iff {m : α → β} {f : filter α} {g : filter β} {i : γ → α}
(h : range i ∈ f) : tendsto (m ∘ i) (comap i f) g ↔ tendsto m f g :=
by rw [tendsto, ← map_compose]; simp only [(∘), map_comap h, tendsto]
lemma comap_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α)
(eq : ψ ∘ φ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : comap φ g = f :=
begin
refine le_antisymm (le_trans (comap_mono $ map_le_iff_le_comap.1 hψ) _) (map_le_iff_le_comap.1 hφ),
rw [comap_comap, eq, comap_id],
exact le_refl _
end
lemma map_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α)
(eq : φ ∘ ψ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : map φ f = g :=
begin
refine le_antisymm hφ (le_trans _ (map_mono hψ)),
rw [map_map, eq, map_id],
exact le_refl _
end
lemma tendsto_inf {f : α → β} {x : filter α} {y₁ y₂ : filter β} :
tendsto f x (y₁ ⊓ y₂) ↔ tendsto f x y₁ ∧ tendsto f x y₂ :=
by simp only [tendsto, le_inf_iff, iff_self]
lemma tendsto_inf_left {f : α → β} {x₁ x₂ : filter α} {y : filter β}
(h : tendsto f x₁ y) : tendsto f (x₁ ⊓ x₂) y :=
le_trans (map_mono inf_le_left) h
lemma tendsto_inf_right {f : α → β} {x₁ x₂ : filter α} {y : filter β}
(h : tendsto f x₂ y) : tendsto f (x₁ ⊓ x₂) y :=
le_trans (map_mono inf_le_right) h
lemma tendsto.inf {f : α → β} {x₁ x₂ : filter α} {y₁ y₂ : filter β}
(h₁ : tendsto f x₁ y₁) (h₂ : tendsto f x₂ y₂) : tendsto f (x₁ ⊓ x₂) (y₁ ⊓ y₂) :=
tendsto_inf.2 ⟨tendsto_inf_left h₁, tendsto_inf_right h₂⟩
lemma tendsto_infi {f : α → β} {x : filter α} {y : ι → filter β} :
tendsto f x (⨅i, y i) ↔ ∀i, tendsto f x (y i) :=
by simp only [tendsto, iff_self, le_infi_iff]
lemma tendsto_infi' {f : α → β} {x : ι → filter α} {y : filter β} (i : ι) :
tendsto f (x i) y → tendsto f (⨅i, x i) y :=
tendsto_le_left (infi_le _ _)
lemma tendsto_principal {f : α → β} {l : filter α} {s : set β} :
tendsto f l (𝓟 s) ↔ ∀ᶠ a in l, f a ∈ s :=
by simp only [tendsto, le_principal_iff, mem_map, iff_self, filter.eventually]
lemma tendsto_principal_principal {f : α → β} {s : set α} {t : set β} :
tendsto f (𝓟 s) (𝓟 t) ↔ ∀a∈s, f a ∈ t :=
by simp only [tendsto, image_subset_iff, le_principal_iff, map_principal, mem_principal_sets]; refl
lemma tendsto_pure {f : α → β} {a : filter α} {b : β} :
tendsto f a (pure b) ↔ ∀ᶠ x in a, f x = b :=
by simp only [tendsto, le_pure_iff, mem_map, mem_singleton_iff, filter.eventually]
lemma tendsto_pure_pure (f : α → β) (a : α) :
tendsto f (pure a) (pure (f a)) :=
tendsto_pure.2 rfl
lemma tendsto_const_pure {a : filter α} {b : β} : tendsto (λx, b) a (pure b) :=
tendsto_pure.2 $ univ_mem_sets' $ λ _, rfl
/-- If two filters are disjoint, then a function cannot tend to both of them along a non-trivial
filter. -/
lemma tendsto.not_tendsto {f : α → β} {a : filter α} {b₁ b₂ : filter β} (hf : tendsto f a b₁)
[ne_bot a] (hb : disjoint b₁ b₂) :
¬ tendsto f a b₂ :=
λ hf', (tendsto_inf.2 ⟨hf, hf'⟩).ne_bot hb.eq_bot
lemma tendsto_if {l₁ : filter α} {l₂ : filter β}
{f g : α → β} {p : α → Prop} [decidable_pred p]
(h₀ : tendsto f (l₁ ⊓ 𝓟 p) l₂)
(h₁ : tendsto g (l₁ ⊓ 𝓟 { x | ¬ p x }) l₂) :
tendsto (λ x, if p x then f x else g x) l₁ l₂ :=
begin
revert h₀ h₁, simp only [tendsto_def, mem_inf_principal],
intros h₀ h₁ s hs,
apply mem_sets_of_superset (inter_mem_sets (h₀ s hs) (h₁ s hs)),
rintros x ⟨hp₀, hp₁⟩, simp only [mem_preimage],
by_cases h : p x,
{ rw if_pos h, exact hp₀ h },
rw if_neg h, exact hp₁ h
end
/-! ### Products of filters -/
section prod
variables {s : set α} {t : set β} {f : filter α} {g : filter β}
/- The product filter cannot be defined using the monad structure on filters. For example:
F := do {x ← seq, y ← top, return (x, y)}
hence:
s ∈ F ↔ ∃n, [n..∞] × univ ⊆ s
G := do {y ← top, x ← seq, return (x, y)}
hence:
s ∈ G ↔ ∀i:ℕ, ∃n, [n..∞] × {i} ⊆ s
Now ⋃i, [i..∞] × {i} is in G but not in F.
As product filter we want to have F as result.
-/
/-- Product of filters. This is the filter generated by cartesian products
of elements of the component filters. -/
protected def prod (f : filter α) (g : filter β) : filter (α × β) :=
f.comap prod.fst ⊓ g.comap prod.snd
localized "infix ` ×ᶠ `:60 := filter.prod" in filter
lemma prod_mem_prod {s : set α} {t : set β} {f : filter α} {g : filter β}
(hs : s ∈ f) (ht : t ∈ g) : set.prod s t ∈ f ×ᶠ g :=
inter_mem_inf_sets (preimage_mem_comap hs) (preimage_mem_comap ht)
lemma mem_prod_iff {s : set (α×β)} {f : filter α} {g : filter β} :
s ∈ f ×ᶠ g ↔ (∃ t₁ ∈ f, ∃ t₂ ∈ g, set.prod t₁ t₂ ⊆ s) :=
begin
simp only [filter.prod],
split,
exact assume ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, h⟩,
⟨s₁, hs₁, s₂, hs₂, subset.trans (inter_subset_inter hts₁ hts₂) h⟩,
exact assume ⟨t₁, ht₁, t₂, ht₂, h⟩,
⟨prod.fst ⁻¹' t₁, ⟨t₁, ht₁, subset.refl _⟩, prod.snd ⁻¹' t₂, ⟨t₂, ht₂, subset.refl _⟩, h⟩
end
lemma comap_prod (f : α → β × γ) (b : filter β) (c : filter γ) :
comap f (b ×ᶠ c) = (comap (prod.fst ∘ f) b) ⊓ (comap (prod.snd ∘ f) c) :=
by erw [comap_inf, filter.comap_comap, filter.comap_comap]
lemma eventually_prod_iff {p : α × β → Prop} {f : filter α} {g : filter β} :
(∀ᶠ x in f ×ᶠ g, p x) ↔ ∃ (pa : α → Prop) (ha : ∀ᶠ x in f, pa x)
(pb : β → Prop) (hb : ∀ᶠ y in g, pb y), ∀ {x}, pa x → ∀ {y}, pb y → p (x, y) :=
by simpa only [set.prod_subset_iff] using @mem_prod_iff α β p f g
lemma tendsto_fst {f : filter α} {g : filter β} : tendsto prod.fst (f ×ᶠ g) f :=
tendsto_inf_left tendsto_comap
lemma tendsto_snd {f : filter α} {g : filter β} : tendsto prod.snd (f ×ᶠ g) g :=
tendsto_inf_right tendsto_comap
lemma tendsto.prod_mk {f : filter α} {g : filter β} {h : filter γ} {m₁ : α → β} {m₂ : α → γ}
(h₁ : tendsto m₁ f g) (h₂ : tendsto m₂ f h) : tendsto (λx, (m₁ x, m₂ x)) f (g ×ᶠ h) :=
tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩
lemma eventually.prod_inl {la : filter α} {p : α → Prop} (h : ∀ᶠ x in la, p x) (lb : filter β) :
∀ᶠ x in la ×ᶠ lb, p (x : α × β).1 :=
tendsto_fst.eventually h
lemma eventually.prod_inr {lb : filter β} {p : β → Prop} (h : ∀ᶠ x in lb, p x) (la : filter α) :
∀ᶠ x in la ×ᶠ lb, p (x : α × β).2 :=
tendsto_snd.eventually h
lemma eventually.prod_mk {la : filter α} {pa : α → Prop} (ha : ∀ᶠ x in la, pa x)
{lb : filter β} {pb : β → Prop} (hb : ∀ᶠ y in lb, pb y) :
∀ᶠ p in la ×ᶠ lb, pa (p : α × β).1 ∧ pb p.2 :=
(ha.prod_inl lb).and (hb.prod_inr la)
lemma eventually.curry {la : filter α} {lb : filter β} {p : α × β → Prop}
(h : ∀ᶠ x in la.prod lb, p x) :
∀ᶠ x in la, ∀ᶠ y in lb, p (x, y) :=
begin
rcases eventually_prod_iff.1 h with ⟨pa, ha, pb, hb, h⟩,
exact ha.mono (λ a ha, hb.mono $ λ b hb, h ha hb)
end
lemma prod_infi_left {f : ι → filter α} {g : filter β} (i : ι) :
(⨅i, f i) ×ᶠ g = (⨅i, (f i) ×ᶠ g) :=
by rw [filter.prod, comap_infi, infi_inf i]; simp only [filter.prod, eq_self_iff_true]
lemma prod_infi_right {f : filter α} {g : ι → filter β} (i : ι) :
f ×ᶠ (⨅i, g i) = (⨅i, f ×ᶠ (g i)) :=
by rw [filter.prod, comap_infi, inf_infi i]; simp only [filter.prod, eq_self_iff_true]
@[mono] lemma prod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) :
f₁ ×ᶠ g₁ ≤ f₂ ×ᶠ g₂ :=
inf_le_inf (comap_mono hf) (comap_mono hg)
lemma prod_comap_comap_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x}
{f₁ : filter α₁} {f₂ : filter α₂} {m₁ : β₁ → α₁} {m₂ : β₂ → α₂} :
(comap m₁ f₁) ×ᶠ (comap m₂ f₂) = comap (λp:β₁×β₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) :=
by simp only [filter.prod, comap_comap, eq_self_iff_true, comap_inf]
lemma prod_comm' : f ×ᶠ g = comap (prod.swap) (g ×ᶠ f) :=
by simp only [filter.prod, comap_comap, (∘), inf_comm, prod.fst_swap,
eq_self_iff_true, prod.snd_swap, comap_inf]
lemma prod_comm : f ×ᶠ g = map (λp:β×α, (p.2, p.1)) (g ×ᶠ f) :=
by rw [prod_comm', ← map_swap_eq_comap_swap]; refl
lemma prod_map_map_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x}
{f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} :
(map m₁ f₁) ×ᶠ (map m₂ f₂) = map (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) :=
le_antisymm
(assume s hs,
let ⟨s₁, hs₁, s₂, hs₂, h⟩ := mem_prod_iff.mp hs in
filter.sets_of_superset _ (prod_mem_prod (image_mem_map hs₁) (image_mem_map hs₂)) $
calc set.prod (m₁ '' s₁) (m₂ '' s₂) = (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) '' set.prod s₁ s₂ :
set.prod_image_image_eq
... ⊆ _ : by rwa [image_subset_iff])
((tendsto.comp (le_refl _) tendsto_fst).prod_mk (tendsto.comp (le_refl _) tendsto_snd))
lemma tendsto.prod_map {δ : Type*} {f : α → γ} {g : β → δ} {a : filter α} {b : filter β}
{c : filter γ} {d : filter δ} (hf : tendsto f a c) (hg : tendsto g b d) :
tendsto (prod.map f g) (a ×ᶠ b) (c ×ᶠ d) :=
begin
erw [tendsto, ← prod_map_map_eq],
exact filter.prod_mono hf hg,
end
lemma map_prod (m : α × β → γ) (f : filter α) (g : filter β) :
map m (f.prod g) = (f.map (λa b, m (a, b))).seq g :=
begin
simp [filter.ext_iff, mem_prod_iff, mem_map_seq_iff],
assume s,
split,
exact assume ⟨t, ht, s, hs, h⟩, ⟨s, hs, t, ht, assume x hx y hy, @h ⟨x, y⟩ ⟨hx, hy⟩⟩,
exact assume ⟨s, hs, t, ht, h⟩, ⟨t, ht, s, hs, assume ⟨x, y⟩ ⟨hx, hy⟩, h x hx y hy⟩
end
lemma prod_eq {f : filter α} {g : filter β} : f.prod g = (f.map prod.mk).seq g :=
have h : _ := map_prod id f g, by rwa [map_id] at h
lemma prod_inf_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} :
(f₁ ×ᶠ g₁) ⊓ (f₂ ×ᶠ g₂) = (f₁ ⊓ f₂) ×ᶠ (g₁ ⊓ g₂) :=
by simp only [filter.prod, comap_inf, inf_comm, inf_assoc, inf_left_comm]
@[simp] lemma prod_bot {f : filter α} : f ×ᶠ (⊥ : filter β) = ⊥ := by simp [filter.prod]
@[simp] lemma bot_prod {g : filter β} : (⊥ : filter α) ×ᶠ g = ⊥ := by simp [filter.prod]
@[simp] lemma prod_principal_principal {s : set α} {t : set β} :
(𝓟 s) ×ᶠ (𝓟 t) = 𝓟 (set.prod s t) :=
by simp only [filter.prod, comap_principal, principal_eq_iff_eq, comap_principal, inf_principal]; refl
@[simp] lemma prod_pure_pure {a : α} {b : β} : (pure a) ×ᶠ (pure b) = pure (a, b) :=
by simp [pure_eq_principal]
lemma prod_eq_bot {f : filter α} {g : filter β} : f ×ᶠ g = ⊥ ↔ (f = ⊥ ∨ g = ⊥) :=
begin
split,
{ assume h,
rcases mem_prod_iff.1 (empty_in_sets_eq_bot.2 h) with ⟨s, hs, t, ht, hst⟩,
rw [subset_empty_iff, set.prod_eq_empty_iff] at hst,
cases hst with s_eq t_eq,
{ left, exact empty_in_sets_eq_bot.1 (s_eq ▸ hs) },
{ right, exact empty_in_sets_eq_bot.1 (t_eq ▸ ht) } },
{ rintros (rfl | rfl),
exact bot_prod,
exact prod_bot }
end
lemma prod_ne_bot {f : filter α} {g : filter β} : ne_bot (f ×ᶠ g) ↔ (ne_bot f ∧ ne_bot g) :=
(not_congr prod_eq_bot).trans not_or_distrib
lemma ne_bot.prod {f : filter α} {g : filter β} (hf : ne_bot f) (hg : ne_bot g) :
ne_bot (f ×ᶠ g) :=
prod_ne_bot.2 ⟨hf, hg⟩
instance prod_ne_bot' {f : filter α} {g : filter β} [hf : ne_bot f] [hg : ne_bot g] :
ne_bot (f ×ᶠ g) :=
hf.prod hg
lemma tendsto_prod_iff {f : α × β → γ} {x : filter α} {y : filter β} {z : filter γ} :
filter.tendsto f (x ×ᶠ y) z ↔
∀ W ∈ z, ∃ U ∈ x, ∃ V ∈ y, ∀ x y, x ∈ U → y ∈ V → f (x, y) ∈ W :=
by simp only [tendsto_def, mem_prod_iff, prod_sub_preimage_iff, exists_prop, iff_self]
end prod
end filter
|
2c017c8a90d3d5ec8df0b24ed5f80457d9bb1943 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/geometry/manifold/diffeomorph.lean | f3f29504c097343be21bc06b90e7eb4b9a3ed525 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 19,003 | lean | /-
Copyright © 2020 Nicolò Cavalleri. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Nicolò Cavalleri, Yury Kudryashov
-/
import geometry.manifold.cont_mdiff_map
/-!
# Diffeomorphisms
This file implements diffeomorphisms.
## Definitions
* `diffeomorph I I' M M' n`: `n`-times continuously differentiable diffeomorphism between
`M` and `M'` with respect to I and I'; we do not introduce a separate definition for the case
`n = ∞`; we use notation instead.
* `diffeomorph.to_homeomorph`: reinterpret a diffeomorphism as a homeomorphism.
* `continuous_linear_equiv.to_diffeomorph`: reinterpret a continuous equivalence as
a diffeomorphism.
* `model_with_corners.trans_diffeomorph`: compose a given `model_with_corners` with a diffeomorphism
between the old and the new target spaces. Useful, e.g, to turn any finite dimensional manifold
into a manifold modelled on a Euclidean space.
* `diffeomorph.to_trans_diffeomorph`: the identity diffeomorphism between `M` with model `I` and `M`
with model `I.trans_diffeomorph e`.
## Notations
* `M ≃ₘ^n⟮I, I'⟯ M'` := `diffeomorph I J M N n`
* `M ≃ₘ⟮I, I'⟯ M'` := `diffeomorph I J M N ⊤`
* `E ≃ₘ^n[𝕜] E'` := `E ≃ₘ^n⟮𝓘(𝕜, E), 𝓘(𝕜, E')⟯ E'`
* `E ≃ₘ[𝕜] E'` := `E ≃ₘ⟮𝓘(𝕜, E), 𝓘(𝕜, E')⟯ E'`
## Implementation notes
This notion of diffeomorphism is needed although there is already a notion of structomorphism
because structomorphisms do not allow the model spaces `H` and `H'` of the two manifolds to be
different, i.e. for a structomorphism one has to impose `H = H'` which is often not the case in
practice.
## Keywords
diffeomorphism, manifold
-/
open_locale manifold topological_space
open function set
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{F : Type*} [normed_group F] [normed_space 𝕜 F]
{H : Type*} [topological_space H]
{H' : Type*} [topological_space H']
{G : Type*} [topological_space G]
{I : model_with_corners 𝕜 E H} {I' : model_with_corners 𝕜 E' H'}
{J : model_with_corners 𝕜 F G}
variables {M : Type*} [topological_space M] [charted_space H M]
{M' : Type*} [topological_space M'] [charted_space H' M']
{N : Type*} [topological_space N] [charted_space G N]
{n : with_top ℕ}
section defs
variables (I I' M M' n)
/--
`n`-times continuously differentiable diffeomorphism between `M` and `M'` with respect to I and I'
-/
@[protect_proj, nolint has_inhabited_instance]
structure diffeomorph extends M ≃ M' :=
(cont_mdiff_to_fun : cont_mdiff I I' n to_equiv)
(cont_mdiff_inv_fun : cont_mdiff I' I n to_equiv.symm)
end defs
localized "notation M ` ≃ₘ^` n:1000 `⟮`:50 I `,` J `⟯ ` N := diffeomorph I J M N n" in manifold
localized "notation M ` ≃ₘ⟮` I `,` J `⟯ ` N := diffeomorph I J M N ⊤" in manifold
localized "notation E ` ≃ₘ^` n:1000 `[`:50 𝕜 `] ` E' :=
diffeomorph (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') E E' n" in manifold
localized "notation E ` ≃ₘ[` 𝕜 `] ` E' :=
diffeomorph (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') E E' ⊤" in manifold
namespace diffeomorph
instance : has_coe_to_fun (M ≃ₘ^n⟮I, I'⟯ M') (λ _, M → M') := ⟨λe, e.to_equiv⟩
instance : has_coe (M ≃ₘ^n⟮I, I'⟯ M') C^n⟮I, M; I', M'⟯ := ⟨λ Φ, ⟨Φ, Φ.cont_mdiff_to_fun⟩⟩
@[continuity] protected lemma continuous (h : M ≃ₘ^n⟮I, I'⟯ M') : continuous h :=
h.cont_mdiff_to_fun.continuous
protected lemma cont_mdiff (h : M ≃ₘ^n⟮I, I'⟯ M') : cont_mdiff I I' n h :=
h.cont_mdiff_to_fun
protected lemma cont_mdiff_at (h : M ≃ₘ^n⟮I, I'⟯ M') {x} : cont_mdiff_at I I' n h x :=
h.cont_mdiff.cont_mdiff_at
protected lemma cont_mdiff_within_at (h : M ≃ₘ^n⟮I, I'⟯ M') {s x} :
cont_mdiff_within_at I I' n h s x :=
h.cont_mdiff_at.cont_mdiff_within_at
protected lemma cont_diff (h : E ≃ₘ^n[𝕜] E') : cont_diff 𝕜 n h :=
h.cont_mdiff.cont_diff
protected lemma smooth (h : M ≃ₘ⟮I, I'⟯ M') : smooth I I' h := h.cont_mdiff_to_fun
protected lemma mdifferentiable (h : M ≃ₘ^n⟮I, I'⟯ M') (hn : 1 ≤ n) : mdifferentiable I I' h :=
h.cont_mdiff.mdifferentiable hn
protected lemma mdifferentiable_on (h : M ≃ₘ^n⟮I, I'⟯ M') (s : set M) (hn : 1 ≤ n) :
mdifferentiable_on I I' h s :=
(h.mdifferentiable hn).mdifferentiable_on
@[simp] lemma coe_to_equiv (h : M ≃ₘ^n⟮I, I'⟯ M') : ⇑h.to_equiv = h := rfl
@[simp, norm_cast] lemma coe_coe (h : M ≃ₘ^n⟮I, I'⟯ M') : ⇑(h : C^n⟮I, M; I', M'⟯) = h := rfl
lemma to_equiv_injective : injective (diffeomorph.to_equiv : (M ≃ₘ^n⟮I, I'⟯ M') → (M ≃ M'))
| ⟨e, _, _⟩ ⟨e', _, _⟩ rfl := rfl
@[simp] lemma to_equiv_inj {h h' : M ≃ₘ^n⟮I, I'⟯ M'} : h.to_equiv = h'.to_equiv ↔ h = h' :=
to_equiv_injective.eq_iff
/-- Coercion to function `λ h : M ≃ₘ^n⟮I, I'⟯ M', (h : M → M')` is injective. -/
lemma coe_fn_injective : injective (λ (h : M ≃ₘ^n⟮I, I'⟯ M') (x : M), h x) :=
equiv.coe_fn_injective.comp to_equiv_injective
@[ext] lemma ext {h h' : M ≃ₘ^n⟮I, I'⟯ M'} (Heq : ∀ x, h x = h' x) : h = h' :=
coe_fn_injective $ funext Heq
section
variables (M I n)
/-- Identity map as a diffeomorphism. -/
protected def refl : M ≃ₘ^n⟮I, I⟯ M :=
{ cont_mdiff_to_fun := cont_mdiff_id,
cont_mdiff_inv_fun := cont_mdiff_id,
to_equiv := equiv.refl M }
@[simp] lemma refl_to_equiv : (diffeomorph.refl I M n).to_equiv = equiv.refl _ := rfl
@[simp] lemma coe_refl : ⇑(diffeomorph.refl I M n) = id := rfl
end
/-- Composition of two diffeomorphisms. -/
protected def trans (h₁ : M ≃ₘ^n⟮I, I'⟯ M') (h₂ : M' ≃ₘ^n⟮I', J⟯ N) :
M ≃ₘ^n⟮I, J⟯ N :=
{ cont_mdiff_to_fun := h₂.cont_mdiff_to_fun.comp h₁.cont_mdiff_to_fun,
cont_mdiff_inv_fun := h₁.cont_mdiff_inv_fun.comp h₂.cont_mdiff_inv_fun,
to_equiv := h₁.to_equiv.trans h₂.to_equiv }
@[simp] lemma trans_refl (h : M ≃ₘ^n⟮I, I'⟯ M') : h.trans (diffeomorph.refl I' M' n) = h :=
ext $ λ _, rfl
@[simp] lemma refl_trans (h : M ≃ₘ^n⟮I, I'⟯ M') : (diffeomorph.refl I M n).trans h = h :=
ext $ λ _, rfl
@[simp] lemma coe_trans (h₁ : M ≃ₘ^n⟮I, I'⟯ M') (h₂ : M' ≃ₘ^n⟮I', J⟯ N) :
⇑(h₁.trans h₂) = h₂ ∘ h₁ := rfl
/-- Inverse of a diffeomorphism. -/
protected def symm (h : M ≃ₘ^n⟮I, J⟯ N) : N ≃ₘ^n⟮J, I⟯ M :=
{ cont_mdiff_to_fun := h.cont_mdiff_inv_fun,
cont_mdiff_inv_fun := h.cont_mdiff_to_fun,
to_equiv := h.to_equiv.symm }
@[simp] lemma apply_symm_apply (h : M ≃ₘ^n⟮I, J⟯ N) (x : N) : h (h.symm x) = x :=
h.to_equiv.apply_symm_apply x
@[simp] lemma symm_apply_apply (h : M ≃ₘ^n⟮I, J⟯ N) (x : M) : h.symm (h x) = x :=
h.to_equiv.symm_apply_apply x
@[simp] lemma symm_refl : (diffeomorph.refl I M n).symm = diffeomorph.refl I M n :=
ext $ λ _, rfl
@[simp] lemma self_trans_symm (h : M ≃ₘ^n⟮I, J⟯ N) : h.trans h.symm = diffeomorph.refl I M n :=
ext h.symm_apply_apply
@[simp] lemma symm_trans_self (h : M ≃ₘ^n⟮I, J⟯ N) : h.symm.trans h = diffeomorph.refl J N n :=
ext h.apply_symm_apply
@[simp] lemma symm_trans' (h₁ : M ≃ₘ^n⟮I, I'⟯ M') (h₂ : M' ≃ₘ^n⟮I', J⟯ N) :
(h₁.trans h₂).symm = h₂.symm.trans h₁.symm := rfl
@[simp] lemma symm_to_equiv (h : M ≃ₘ^n⟮I, J⟯ N) : h.symm.to_equiv = h.to_equiv.symm := rfl
@[simp, mfld_simps] lemma to_equiv_coe_symm (h : M ≃ₘ^n⟮I, J⟯ N) : ⇑h.to_equiv.symm = h.symm := rfl
lemma image_eq_preimage (h : M ≃ₘ^n⟮I, J⟯ N) (s : set M) : h '' s = h.symm ⁻¹' s :=
h.to_equiv.image_eq_preimage s
lemma symm_image_eq_preimage (h : M ≃ₘ^n⟮I, J⟯ N) (s : set N) : h.symm '' s = h ⁻¹' s :=
h.symm.image_eq_preimage s
@[simp, mfld_simps] lemma range_comp {α} (h : M ≃ₘ^n⟮I, J⟯ N) (f : α → M) :
range (h ∘ f) = h.symm ⁻¹' (range f) :=
by rw [range_comp, image_eq_preimage]
@[simp] lemma image_symm_image (h : M ≃ₘ^n⟮I, J⟯ N) (s : set N) : h '' (h.symm '' s) = s :=
h.to_equiv.image_symm_image s
@[simp] lemma symm_image_image (h : M ≃ₘ^n⟮I, J⟯ N) (s : set M) : h.symm '' (h '' s) = s :=
h.to_equiv.symm_image_image s
/-- A diffeomorphism is a homeomorphism. -/
def to_homeomorph (h : M ≃ₘ^n⟮I, J⟯ N) : M ≃ₜ N :=
⟨h.to_equiv, h.continuous, h.symm.continuous⟩
@[simp] lemma to_homeomorph_to_equiv (h : M ≃ₘ^n⟮I, J⟯ N) :
h.to_homeomorph.to_equiv = h.to_equiv :=
rfl
@[simp] lemma symm_to_homeomorph (h : M ≃ₘ^n⟮I, J⟯ N) :
h.symm.to_homeomorph = h.to_homeomorph.symm :=
rfl
@[simp] lemma coe_to_homeomorph (h : M ≃ₘ^n⟮I, J⟯ N) : ⇑h.to_homeomorph = h := rfl
@[simp] lemma coe_to_homeomorph_symm (h : M ≃ₘ^n⟮I, J⟯ N) :
⇑h.to_homeomorph.symm = h.symm := rfl
@[simp] lemma cont_mdiff_within_at_comp_diffeomorph_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : N → M'}
{s x} (hm : m ≤ n) :
cont_mdiff_within_at I I' m (f ∘ h) s x ↔
cont_mdiff_within_at J I' m f (h.symm ⁻¹' s) (h x) :=
begin
split,
{ intro Hfh,
rw [← h.symm_apply_apply x] at Hfh,
simpa only [(∘), h.apply_symm_apply]
using Hfh.comp (h x) (h.symm.cont_mdiff_within_at.of_le hm) (maps_to_preimage _ _) },
{ rw ← h.image_eq_preimage,
exact λ hf, hf.comp x (h.cont_mdiff_within_at.of_le hm) (maps_to_image _ _) }
end
@[simp] lemma cont_mdiff_on_comp_diffeomorph_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : N → M'}
{s} (hm : m ≤ n) :
cont_mdiff_on I I' m (f ∘ h) s ↔ cont_mdiff_on J I' m f (h.symm ⁻¹' s) :=
h.to_equiv.forall_congr $ λ x, by simp only [hm, coe_to_equiv, symm_apply_apply,
cont_mdiff_within_at_comp_diffeomorph_iff, mem_preimage]
@[simp] lemma cont_mdiff_at_comp_diffeomorph_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : N → M'} {x}
(hm : m ≤ n) :
cont_mdiff_at I I' m (f ∘ h) x ↔ cont_mdiff_at J I' m f (h x) :=
h.cont_mdiff_within_at_comp_diffeomorph_iff hm
@[simp] lemma cont_mdiff_comp_diffeomorph_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : N → M'}
(hm : m ≤ n) :
cont_mdiff I I' m (f ∘ h) ↔ cont_mdiff J I' m f :=
h.to_equiv.forall_congr $ λ x, (h.cont_mdiff_at_comp_diffeomorph_iff hm)
@[simp] lemma cont_mdiff_within_at_diffeomorph_comp_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : M' → M}
(hm : m ≤ n) {s x} :
cont_mdiff_within_at I' J m (h ∘ f) s x ↔ cont_mdiff_within_at I' I m f s x :=
⟨λ Hhf, by simpa only [(∘), h.symm_apply_apply]
using (h.symm.cont_mdiff_at.of_le hm).comp_cont_mdiff_within_at _ Hhf,
λ Hf, (h.cont_mdiff_at.of_le hm).comp_cont_mdiff_within_at _ Hf⟩
@[simp] lemma cont_mdiff_at_diffeomorph_comp_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : M' → M}
(hm : m ≤ n) {x} :
cont_mdiff_at I' J m (h ∘ f) x ↔ cont_mdiff_at I' I m f x :=
h.cont_mdiff_within_at_diffeomorph_comp_iff hm
@[simp] lemma cont_mdiff_on_diffeomorph_comp_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : M' → M}
(hm : m ≤ n) {s} :
cont_mdiff_on I' J m (h ∘ f) s ↔ cont_mdiff_on I' I m f s :=
forall₂_congr $ λ x hx, h.cont_mdiff_within_at_diffeomorph_comp_iff hm
@[simp] lemma cont_mdiff_diffeomorph_comp_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : M' → M}
(hm : m ≤ n) :
cont_mdiff I' J m (h ∘ f) ↔ cont_mdiff I' I m f :=
forall_congr $ λ x, h.cont_mdiff_within_at_diffeomorph_comp_iff hm
lemma to_local_homeomorph_mdifferentiable (h : M ≃ₘ^n⟮I, J⟯ N) (hn : 1 ≤ n) :
h.to_homeomorph.to_local_homeomorph.mdifferentiable I J :=
⟨h.mdifferentiable_on _ hn, h.symm.mdifferentiable_on _ hn⟩
variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners J N]
lemma unique_mdiff_on_image_aux (h : M ≃ₘ^n⟮I, J⟯ N) (hn : 1 ≤ n)
{s : set M} (hs : unique_mdiff_on I s) :
unique_mdiff_on J (h '' s) :=
begin
convert hs.unique_mdiff_on_preimage (h.to_local_homeomorph_mdifferentiable hn),
simp [h.image_eq_preimage]
end
@[simp] lemma unique_mdiff_on_image (h : M ≃ₘ^n⟮I, J⟯ N) (hn : 1 ≤ n) {s : set M} :
unique_mdiff_on J (h '' s) ↔ unique_mdiff_on I s :=
⟨λ hs, h.symm_image_image s ▸ h.symm.unique_mdiff_on_image_aux hn hs,
h.unique_mdiff_on_image_aux hn⟩
@[simp] lemma unique_mdiff_on_preimage (h : M ≃ₘ^n⟮I, J⟯ N) (hn : 1 ≤ n) {s : set N} :
unique_mdiff_on I (h ⁻¹' s) ↔ unique_mdiff_on J s :=
h.symm_image_eq_preimage s ▸ h.symm.unique_mdiff_on_image hn
@[simp] lemma unique_diff_on_image (h : E ≃ₘ^n[𝕜] F) (hn : 1 ≤ n) {s : set E} :
unique_diff_on 𝕜 (h '' s) ↔ unique_diff_on 𝕜 s :=
by simp only [← unique_mdiff_on_iff_unique_diff_on, unique_mdiff_on_image, hn]
@[simp] lemma unique_diff_on_preimage (h : E ≃ₘ^n[𝕜] F) (hn : 1 ≤ n) {s : set F} :
unique_diff_on 𝕜 (h ⁻¹' s) ↔ unique_diff_on 𝕜 s :=
h.symm_image_eq_preimage s ▸ h.symm.unique_diff_on_image hn
end diffeomorph
namespace continuous_linear_equiv
variable (e : E ≃L[𝕜] E')
/-- A continuous linear equivalence between normed spaces is a diffeomorphism. -/
def to_diffeomorph : E ≃ₘ[𝕜] E' :=
{ cont_mdiff_to_fun := e.cont_diff.cont_mdiff,
cont_mdiff_inv_fun := e.symm.cont_diff.cont_mdiff,
to_equiv := e.to_linear_equiv.to_equiv }
@[simp] lemma coe_to_diffeomorph : ⇑e.to_diffeomorph = e := rfl
@[simp] lemma symm_to_diffeomorph : e.symm.to_diffeomorph = e.to_diffeomorph.symm := rfl
@[simp] lemma coe_to_diffeomorph_symm : ⇑e.to_diffeomorph.symm = e.symm := rfl
end continuous_linear_equiv
namespace model_with_corners
variables (I) (e : E ≃ₘ[𝕜] E')
/-- Apply a diffeomorphism (e.g., a continuous linear equivalence) to the model vector space. -/
def trans_diffeomorph (I : model_with_corners 𝕜 E H) (e : E ≃ₘ[𝕜] E') :
model_with_corners 𝕜 E' H :=
{ to_local_equiv := I.to_local_equiv.trans e.to_equiv.to_local_equiv,
source_eq := by simp,
unique_diff' := by simp [range_comp e, I.unique_diff],
continuous_to_fun := e.continuous.comp I.continuous,
continuous_inv_fun := I.continuous_symm.comp e.symm.continuous }
@[simp, mfld_simps] lemma coe_trans_diffeomorph : ⇑(I.trans_diffeomorph e) = e ∘ I := rfl
@[simp, mfld_simps] lemma coe_trans_diffeomorph_symm :
⇑(I.trans_diffeomorph e).symm = I.symm ∘ e.symm := rfl
lemma trans_diffeomorph_range : range (I.trans_diffeomorph e) = e '' (range I) :=
range_comp e I
lemma coe_ext_chart_at_trans_diffeomorph (x : M) :
⇑(ext_chart_at (I.trans_diffeomorph e) x) = e ∘ ext_chart_at I x := rfl
lemma coe_ext_chart_at_trans_diffeomorph_symm (x : M) :
⇑(ext_chart_at (I.trans_diffeomorph e) x).symm = (ext_chart_at I x).symm ∘ e.symm := rfl
lemma ext_chart_at_trans_diffeomorph_target (x : M) :
(ext_chart_at (I.trans_diffeomorph e) x).target = e.symm ⁻¹' (ext_chart_at I x).target :=
by simp only [range_comp e, e.image_eq_preimage, preimage_preimage] with mfld_simps
end model_with_corners
namespace diffeomorph
variables (e : E ≃ₘ[𝕜] F)
instance smooth_manifold_with_corners_trans_diffeomorph [smooth_manifold_with_corners I M] :
smooth_manifold_with_corners (I.trans_diffeomorph e) M :=
begin
refine smooth_manifold_with_corners_of_cont_diff_on _ _ (λ e₁ e₂ h₁ h₂, _),
refine e.cont_diff.comp_cont_diff_on
(((cont_diff_groupoid ⊤ I).compatible h₁ h₂).1.comp
e.symm.cont_diff.cont_diff_on _),
mfld_set_tac
end
variables (I M)
/-- The identity diffeomorphism between a manifold with model `I` and the same manifold
with model `I.trans_diffeomorph e`. -/
def to_trans_diffeomorph (e : E ≃ₘ[𝕜] F) : M ≃ₘ⟮I, I.trans_diffeomorph e⟯ M :=
{ to_equiv := equiv.refl M,
cont_mdiff_to_fun := λ x,
begin
refine cont_mdiff_within_at_iff.2 ⟨continuous_within_at_id, _⟩,
refine e.cont_diff.cont_diff_within_at.congr' (λ y hy, _) _,
{ simp only [equiv.coe_refl, id, (∘), I.coe_ext_chart_at_trans_diffeomorph,
(ext_chart_at I x).right_inv hy.1] },
exact ⟨(ext_chart_at I x).map_source (mem_ext_chart_source I x), trivial,
by simp only with mfld_simps⟩
end,
cont_mdiff_inv_fun := λ x,
begin
refine cont_mdiff_within_at_iff.2 ⟨continuous_within_at_id, _⟩,
refine e.symm.cont_diff.cont_diff_within_at.congr' (λ y hy, _) _,
{ simp only [mem_inter_eq, I.ext_chart_at_trans_diffeomorph_target] at hy,
simp only [equiv.coe_refl, equiv.refl_symm, id, (∘),
I.coe_ext_chart_at_trans_diffeomorph_symm, (ext_chart_at I x).right_inv hy.1] },
exact ⟨(ext_chart_at _ x).map_source (mem_ext_chart_source _ x), trivial,
by simp only [e.symm_apply_apply, equiv.refl_symm, equiv.coe_refl] with mfld_simps⟩
end }
variables {I M}
@[simp] lemma cont_mdiff_within_at_trans_diffeomorph_right {f : M' → M} {x s} :
cont_mdiff_within_at I' (I.trans_diffeomorph e) n f s x ↔
cont_mdiff_within_at I' I n f s x :=
(to_trans_diffeomorph I M e).cont_mdiff_within_at_diffeomorph_comp_iff le_top
@[simp] lemma cont_mdiff_at_trans_diffeomorph_right {f : M' → M} {x} :
cont_mdiff_at I' (I.trans_diffeomorph e) n f x ↔ cont_mdiff_at I' I n f x :=
(to_trans_diffeomorph I M e).cont_mdiff_at_diffeomorph_comp_iff le_top
@[simp] lemma cont_mdiff_on_trans_diffeomorph_right {f : M' → M} {s} :
cont_mdiff_on I' (I.trans_diffeomorph e) n f s ↔ cont_mdiff_on I' I n f s :=
(to_trans_diffeomorph I M e).cont_mdiff_on_diffeomorph_comp_iff le_top
@[simp] lemma cont_mdiff_trans_diffeomorph_right {f : M' → M} :
cont_mdiff I' (I.trans_diffeomorph e) n f ↔ cont_mdiff I' I n f :=
(to_trans_diffeomorph I M e).cont_mdiff_diffeomorph_comp_iff le_top
@[simp] lemma smooth_trans_diffeomorph_right {f : M' → M} :
smooth I' (I.trans_diffeomorph e) f ↔ smooth I' I f :=
cont_mdiff_trans_diffeomorph_right e
@[simp] lemma cont_mdiff_within_at_trans_diffeomorph_left {f : M → M'} {x s} :
cont_mdiff_within_at (I.trans_diffeomorph e) I' n f s x ↔
cont_mdiff_within_at I I' n f s x :=
((to_trans_diffeomorph I M e).cont_mdiff_within_at_comp_diffeomorph_iff le_top).symm
@[simp] lemma cont_mdiff_at_trans_diffeomorph_left {f : M → M'} {x} :
cont_mdiff_at (I.trans_diffeomorph e) I' n f x ↔ cont_mdiff_at I I' n f x :=
((to_trans_diffeomorph I M e).cont_mdiff_at_comp_diffeomorph_iff le_top).symm
@[simp] lemma cont_mdiff_on_trans_diffeomorph_left {f : M → M'} {s} :
cont_mdiff_on (I.trans_diffeomorph e) I' n f s ↔ cont_mdiff_on I I' n f s :=
((to_trans_diffeomorph I M e).cont_mdiff_on_comp_diffeomorph_iff le_top).symm
@[simp] lemma cont_mdiff_trans_diffeomorph_left {f : M → M'} :
cont_mdiff (I.trans_diffeomorph e) I' n f ↔ cont_mdiff I I' n f :=
((to_trans_diffeomorph I M e).cont_mdiff_comp_diffeomorph_iff le_top).symm
@[simp] lemma smooth_trans_diffeomorph_left {f : M → M'} :
smooth (I.trans_diffeomorph e) I' f ↔ smooth I I' f :=
e.cont_mdiff_trans_diffeomorph_left
end diffeomorph
|
ca7f6d2a8bfb32a90eea306cc74c05271abe3e38 | e38d5e91d30731bef617cc9b6de7f79c34cdce9a | /src/examples/diagonal.lean | 8b0d67935752d98bd9f982a48c52e07f8f6a3873 | [
"Apache-2.0"
] | permissive | bbentzen/cubicalean | 55e979c303fbf55a81ac46b1000c944b2498be7a | 3b94cd2aefdfc2163c263bd3fc6f2086fef814b5 | refs/heads/master | 1,588,314,875,258 | 1,554,412,699,000 | 1,554,412,699,000 | 177,333,390 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 383 | lean | /-
Copyright (c) 2019 Bruno Bentzen. All rights reserved.
Released under the Apache License 2.0 (see "License");
Author: Bruno Bentzen
-/
import ..core.interval
open interval
-- diagonal maps (contraction) for types and terms
example {A : I → I → Type} : I → Type := λ i, A i i
example {A : I → I → Type} (a : Π j i, A j i) : Π i, A i i := λ i, a i i |
be06cffe87f1e05716e821301abffd539c10fc9f | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/algebra/direct_sum/algebra.lean | c9c7d6813e910751e654f1c43329cd4b096de27f | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,012 | lean | /-
Copyright (c) 2021 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
import algebra.direct_sum.ring
import algebra.direct_sum.module
/-! # Additively-graded algebra structures on `⨁ i, A i`
This file provides `R`-algebra structures on external direct sums of `R`-modules.
Recall that if `A i` are a family of `add_comm_monoid`s indexed by an `add_monoid`, then an instance
of `direct_sum.gmonoid A` is a multiplication `A i → A j → A (i + j)` giving `⨁ i, A i` the
structure of a semiring. In this file, we introduce the `direct_sum.galgebra R A` class for the case
where all `A i` are `R`-modules. This is the extra structure needed to promote `⨁ i, A i` to an
`R`-algebra.
## Main definitions
* `direct_sum.galgebra R A`, the typeclass.
* `direct_sum.galgebra.of_submodules`, for creating the above instance from a collection of
submodules.
* `direct_sum.to_algebra` extends `direct_sum.to_semiring` to produce an `alg_hom`.
## Direct sums of subobjects
Additionally, this module provides the instance `direct_sum.galgebra.of_submodules` which promotes
any instance constructed with `direct_sum.gmonoid.of_submodules` to an `R`-algebra.
-/
universes uι uR uA uB
variables {ι : Type uι}
namespace direct_sum
open_locale direct_sum
variables (R : Type uR) (A : ι → Type uA) {B : Type uB} [decidable_eq ι]
variables [comm_semiring R] [Π i, add_comm_monoid (A i)] [Π i, module R (A i)]
variables [add_monoid ι] [gsemiring A]
section
/-- A graded version of `algebra`. An instance of `direct_sum.galgebra R A` endows `(⨁ i, A i)`
with an `R`-algebra structure. -/
class galgebra :=
(to_fun : R →+ A 0)
(map_one : to_fun 1 = graded_monoid.ghas_one.one)
(map_mul : ∀ r s,
graded_monoid.mk _ (to_fun (r * s)) = ⟨_, graded_monoid.ghas_mul.mul (to_fun r) (to_fun s)⟩)
(commutes : ∀ r x, graded_monoid.mk _ (to_fun r) * x = x * ⟨_, to_fun r⟩)
(smul_def : ∀ r (x : graded_monoid A), graded_monoid.mk x.1 (r • x.2) = ⟨_, to_fun (r)⟩ * x)
end
variables [semiring B] [galgebra R A] [algebra R B]
instance : algebra R (⨁ i, A i) :=
{ to_fun := (direct_sum.of A 0).comp galgebra.to_fun,
map_zero' := add_monoid_hom.map_zero _,
map_add' := add_monoid_hom.map_add _,
map_one' := (direct_sum.of A 0).congr_arg galgebra.map_one,
map_mul' := λ a b, begin
simp only [add_monoid_hom.comp_apply],
rw of_mul_of,
apply dfinsupp.single_eq_of_sigma_eq (galgebra.map_mul a b),
end,
commutes' := λ r x, begin
change add_monoid_hom.mul (direct_sum.of _ _ _) x =
add_monoid_hom.mul.flip (direct_sum.of _ _ _) x,
apply add_monoid_hom.congr_fun _ x,
ext i xi : 2,
dsimp only [add_monoid_hom.comp_apply, add_monoid_hom.mul_apply, add_monoid_hom.flip_apply],
rw [of_mul_of, of_mul_of],
apply dfinsupp.single_eq_of_sigma_eq (galgebra.commutes r ⟨i, xi⟩),
end,
smul_def' := λ r x, begin
change distrib_mul_action.to_add_monoid_hom _ r x = add_monoid_hom.mul (direct_sum.of _ _ _) x,
apply add_monoid_hom.congr_fun _ x,
ext i xi : 2,
dsimp only [add_monoid_hom.comp_apply, distrib_mul_action.to_add_monoid_hom_apply,
add_monoid_hom.mul_apply],
rw [direct_sum.of_mul_of, ←of_smul],
apply dfinsupp.single_eq_of_sigma_eq (galgebra.smul_def r ⟨i, xi⟩),
end }
lemma algebra_map_apply (r : R) :
algebra_map R (⨁ i, A i) r = direct_sum.of A 0 (galgebra.to_fun r) := rfl
lemma algebra_map_to_add_monoid_hom :
↑(algebra_map R (⨁ i, A i)) = (direct_sum.of A 0).comp (galgebra.to_fun : R →+ A 0) := rfl
section
-- for `simps`
local attribute [simp] linear_map.cod_restrict
/-- A `direct_sum.gmonoid` instance produced by `direct_sum.gmonoid.of_submodules` is automatically
a `direct_sum.galgebra`. -/
@[simps to_fun_apply {simp_rhs := tt}]
instance galgebra.of_submodules
(carriers : ι → submodule R B)
(one_mem : (1 : B) ∈ carriers 0)
(mul_mem : ∀ ⦃i j⦄ (gi : carriers i) (gj : carriers j), (gi * gj : B) ∈ carriers (i + j)) :
by haveI : gsemiring (λ i, carriers i) := gsemiring.of_submodules carriers one_mem mul_mem; exact
galgebra R (λ i, carriers i) :=
by exact {
to_fun := begin
refine ((algebra.linear_map R B).cod_restrict (carriers 0) $ λ r, _).to_add_monoid_hom,
exact submodule.one_le.mpr one_mem (submodule.algebra_map_mem _),
end,
map_one := subtype.ext $ by exact (algebra_map R B).map_one,
map_mul := λ x y, sigma.subtype_ext (add_zero 0).symm $ (algebra_map R B).map_mul _ _,
commutes := λ r ⟨i, xi⟩,
sigma.subtype_ext ((zero_add i).trans (add_zero i).symm) $ algebra.commutes _ _,
smul_def := λ r ⟨i, xi⟩, sigma.subtype_ext (zero_add i).symm $ algebra.smul_def _ _ }
end
/-- A family of `linear_map`s preserving `direct_sum.ghas_one.one` and `direct_sum.ghas_mul.mul`
describes an `alg_hom` on `⨁ i, A i`. This is a stronger version of `direct_sum.to_semiring`.
Of particular interest is the case when `A i` are bundled subojects, `f` is the family of
coercions such as `submodule.subtype (A i)`, and the `[gmonoid A]` structure originates from
`direct_sum.gmonoid.of_add_submodules`, in which case the proofs about `ghas_one` and `ghas_mul`
can be discharged by `rfl`. -/
@[simps]
def to_algebra
(f : Π i, A i →ₗ[R] B) (hone : f _ (graded_monoid.ghas_one.one) = 1)
(hmul : ∀ {i j} (ai : A i) (aj : A j), f _ (graded_monoid.ghas_mul.mul ai aj) = f _ ai * f _ aj)
(hcommutes : ∀ r, (f 0) (galgebra.to_fun r) = (algebra_map R B) r) :
(⨁ i, A i) →ₐ[R] B :=
{ to_fun := to_semiring (λ i, (f i).to_add_monoid_hom) hone @hmul,
commutes' := λ r, (direct_sum.to_semiring_of _ _ _ _ _).trans (hcommutes r),
.. to_semiring (λ i, (f i).to_add_monoid_hom) hone @hmul}
/-- Two `alg_hom`s out of a direct sum are equal if they agree on the generators.
See note [partially-applied ext lemmas]. -/
@[ext]
lemma alg_hom_ext ⦃f g : (⨁ i, A i) →ₐ[R] B⦄
(h : ∀ i, f.to_linear_map.comp (lof _ _ A i) = g.to_linear_map.comp (lof _ _ A i)) : f = g :=
alg_hom.coe_ring_hom_injective $
direct_sum.ring_hom_ext $ λ i, add_monoid_hom.ext $ linear_map.congr_fun (h i)
end direct_sum
/-! ### Concrete instances -/
/-- A direct sum of copies of a `algebra` inherits the algebra structure.
-/
@[simps]
instance algebra.direct_sum_galgebra {R A : Type*} [decidable_eq ι]
[add_monoid ι] [comm_semiring R] [semiring A] [algebra R A] :
direct_sum.galgebra R (λ i : ι, A) :=
{ to_fun := (algebra_map R A).to_add_monoid_hom,
map_one := (algebra_map R A).map_one,
map_mul := λ a b, sigma.ext (zero_add _).symm (heq_of_eq $ (algebra_map R A).map_mul a b),
commutes := λ r ⟨ai, a⟩, sigma.ext ((zero_add _).trans (add_zero _).symm)
(heq_of_eq $ algebra.commutes _ _),
smul_def := λ r ⟨ai, a⟩, sigma.ext (zero_add _).symm (heq_of_eq $ algebra.smul_def _ _) }
namespace submodule
variables {R A : Type*} [comm_semiring R]
end submodule
|
31c3409142e6dca2f048ee25ca71d255a6ab5c36 | 92e157ec9825b5e4597a6d715a8928703bc8e3b2 | /src/mywork/practice_1 solution.lean | c7e00595bf922e9d3b7125ead1a124fc8e5dd320 | [] | no_license | exb3dg/cs2120f21 | 9e566bc508762573c023d3e70f83cb839c199ec8 | 319b8bf0d63bf96437bf17970ce0198d0b3525cd | refs/heads/main | 1,692,970,909,568 | 1,634,584,540,000 | 1,634,584,540,000 | 399,947,025 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,648 | lean | /-
chameleon
exb3dg; https://github.com/exb3dg/cs2120f21.git
-/
/-
EQUALITY
-/
/- #1
Suppose that x, y, z, and w are arbitrary objects of some type,
T; and suppose further that we know (have proofs of the facts)
that x = y, y = z, and w = z. Give a very, very short English
proof of the conjecture that z = w. You can use not only the
axioms of equality, but either of the theorems about properties
of equality that we have proven. Hint: There's something about
this question that makes it much easier to answer than it might
at first appear.
-- 1) Using the theorem that the equality relation is symmetric we
know that if w *equals* z, then z *equals* w.
-/
/- #2
Give a formal statement of the conjecture (proposition) from
#1 by filling in the "hole" in the following definition. The
def is a keyword. The name you're binding to your proposition
is prop_1. The type of the value is Prop (which is the type of
all propositions in Lean).
-/
-- 2) what I answered
def prop_1 : Prop :=
∀ (T : Type)
(w z : T),
w = z →
z = w
-- 2) ***solution***
def prop_1_sol : Prop :=
∀ (T :Type) (x y z w :T),
x = y →
y = z →
w = z →
z = w
/- #3 (extra credit)
Give a formal proof of the proposition from #2 by filling in
the hole in this next definition. Hint: Use Lean's versions of
the axioms and basic theorems concerning equality. They are,
again, called eq.refl, eq.subst, eq.symm, eq.trans.
-/
-- 3) what I answered
theorem prop_1_proof : prop_1 :=
begin
assume T,
assume w z,
assume h,
rw h,
end
-- 3) ***solution***
theorem prop_1_proof_sol : prop_1s :=
begin
assume T x y z w,
assume xy yz zw,
exact eq.symm zw,
end
/-
FOR ALL: ∀.
-/
/- #4
Give a very brief explanation in English of the introduction
rule for ∀. For example, suppose you need to prove (∀ x, P x);
what do you do? (I'm being a little informal in leaving out the
type of X.)
-- 4) what I answered
∀ - Means for all objects x of some arbitrary type,
every x has property P.
-- 4) ***solution***
Assume you have an arbitrary x and test it. Because x can be any
value by testing the x and getting whatever result you get, you
can apply it to all x because you could have picked any x.
-/
/- #5
Suppose you have a proof, let's call it pf, of the proposition,
(∀ x, P x), and you need a proof of P t, for some particular t.
Write an expression then uses the elimination rule for ∀ to get
such a proof. Complete the answer by replacing the underscores
in the following expression:
-- 5) what I answered
( (a : ∀ (x : T), P x), example : P t := a t ).
-/
-- 5) ***solution***
axioms (Ball : Type) (blue : Ball → Prop)
(allBallsBlue : ∀ (b : Ball), blue b)
(tomsBall : Ball)
theorem tomsBallIsBlue : blue tomsBall :=
allBallsBlue tomsBall
/-
IMPLIES: →
In the "code" that follows, we define two predicates, each
taking one natural number as an argument. We call them ev and
odd. When applied to any value, n, ev yields the proposition
that n is even (n % 2 = 0), while odd yields the proposition
that n is odd (n % 2 = 1).
-/
def ev (n : ℕ) := n % 2 = 0
def odd (n : ℕ) := n % 2 = 1
/- #6
Write a formal version of the proposition that, for *any*
natural number n, *if* n is even, *then* n + 1 is odd. Give
your answer by filling the hole in the following definition.
Hint: put parenthesis around "n + 1" in your answer.
-/
-- 6) what I answered
def successor_of_even_is_odd : Prop :=
∀ ( n: ℕ), n % 2 = 0 → (n + 1) % 2 = 1
-- 6) ***solution***
def successor_of_even_is_odd_sol : Prop :=
∀ ( n: ℕ), ev n → odd (n + 1)
/- #7
Suppose that "its_raining" and "the_streets_are_wet" are
propositions. (We formalize these assumptions as axioms in
what follows. Then give a formal definition of the (larger)
proposition, "if it's raining out then the streets are wet")
by filling in the hole
-/
axioms (raining streets_wet : Prop)
-- 7) what I answered
axiom if_raining_then_streets_wet : raining → streets_wet
-- 7) ***solution***
axiom if_raining_then_streets_wet_sol : raining → streets_wet
/- #9
Now suppose that in addition, its_raining is true, and
we have a proof of it, pf_its_raining. Again, we again give
you this assumption formally as an axiom below. Finish
the formal proof that the streets must be wet. Hint: here
you are asked to use the elimination rule for →.
-/
axiom pf_raining : raining
-- 9) what I answered
example : streets_wet :=
if_raining_then_streets_wet pf_raining
-- 9) ***solution***
example : streets_wet :=
if_raining_then_streets_wet pf_raining
/-
AND: ∧
-/
/- #10
In our last class, we proved that "∧ is *commutative*."
That is, for any given *propositions*, P and Q, (P ∧ Q) →
(Q ∧ P). The way we proved it was to *assume* that we're
given such a P, Q, and proof, pq, of (P ∧ Q) -- applying
the introduction rules for ∀ and →). In this context, we
*use* the proof, pq, to derive separate proofs, let's call
them p, a proof of P, and q, a proof of Q. With these in
hand, we then apply the introduction rule for ∧ to put
them back together into a proof of (Q ∧ P). We give you
a formal version of this proof as a reminder, next.
-/
theorem and_commutative : ∀ (P Q : Prop), P ∧ Q → Q ∧ P :=
begin
assume P Q pq,
apply and.intro _ _,
exact (and.elim_right pq),
exact (and.elim_left pq),
end
/-
Your task now is to prove the theorem, "∧ is *associative*."
What this means is that for arbitrary propositions, P, Q, and
R, if (P ∧ (Q ∧ R)) is true, then ((P ∧ Q) ∧ R) is true, *and
vice versa*. You just need to prove it in the first direction.
Hint, if you have a proof, p_qr, of (P ∧ (Q ∧ R)), then the
application of and.elim_left will give you a proof of P, and
and.elim_right will give you a proof of (Q ∧ R).
To help you along, we give you the first part of the proof,
including an example of a new Lean tactic called have, which
allows you to give a name to a new value in the middle of a
proof script.
-/
-- 10) what I answered
theorem and_associative :
∀ (P Q R : Prop),
(P ∧ (Q ∧ R)) → ((P ∧ Q) ∧ R) :=
begin
intros P Q R h,
have p : P := and.elim_left h,
have qr : Q ∧ R := and.elim_right h,
have q : Q := and.elim_left qr,
have r : R := and.elim_right qr,
have pq : P ∧ Q := and.intro p q,
exact and.intro pq r,
end
-- 10) ***solution***
theorem and_associative_sol :
∀ (P Q R : Prop),
(P ∧ (Q ∧ R)) → ((P ∧ Q) ∧ R) :=
begin
intros P Q R h,
have p : P := and.elim_left h,
have q : Q := (and.elim_right h).left,
have r : R := and.elim_right h,
have pq : P ∧ Q := and.intro p q,
exact and.intro pq r,
end
/- #11
Give an English language proof of the preceding
theorem. Do it by finishing off the following
partial "proof explanation."
-- 11)
Proof. We assume that P, Q, and R are arbitrary
but specific propositions, and that we have a
proof, let's call it p_qr, of (P ∧ (Q ∧ R)) [by
application of ∧ and → introduction.] What now
remains to be proved is ((P ∧ Q) ∧ R). We can
construct a proof of this proposition by applying
and.intro to a proof of (P ∧ Q) and a proof of R.
What remains, then, is to obtain these proofs.
But this is easily done by the application of
and.elim_(left/right) to p_qr to break it into
proofs of the individual propositions. QED.
-/
/-
Note that Lean includes versions of these
theorems (and many, many, many others) in
its extensive library of formalized maths,
as the following check commands reveal.
Note the difference in naming relative to
the definitions we give in this file.
-/
#check @and.comm
#check @and.assoc |
87ebb730aa777af6b3cc578e358e3ef98c957649 | 6e41ee3ac9b96e8980a16295cc21f131e731884f | /hott/algebra/precategory/basic.hlean | 25276915358cf68379952979cc3500939040cbf5 | [
"Apache-2.0"
] | permissive | EgbertRijke/lean | 3426cfa0e5b3d35d12fc3fd7318b35574cb67dc3 | 4f2e0c6d7fc9274d953cfa1c37ab2f3e799ab183 | refs/heads/master | 1,610,834,871,476 | 1,422,159,801,000 | 1,422,159,801,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,223 | hlean | -- Copyright (c) 2014 Floris van Doorn. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Author: Floris van Doorn
open eq truncation
structure precategory [class] (ob : Type) : Type :=
(hom : ob → ob → Type)
(homH : Π {a b : ob}, is_hset (hom a b))
(comp : Π⦃a b c : ob⦄, hom b c → hom a b → hom a c)
(ID : Π (a : ob), hom a a)
(assoc : Π ⦃a b c d : ob⦄ (h : hom c d) (g : hom b c) (f : hom a b),
comp h (comp g f) = comp (comp h g) f)
(id_left : Π ⦃a b : ob⦄ (f : hom a b), comp !ID f = f)
(id_right : Π ⦃a b : ob⦄ (f : hom a b), comp f !ID = f)
persistent attribute precategory [multiple-instances]
namespace precategory
variables {ob : Type} [C : precategory ob]
variables {a b c d : ob}
include C
persistent attribute homH [instance]
definition compose := comp
definition id [reducible] {a : ob} : hom a a := ID a
infixr `∘` := compose
infixl `⟶`:25 := hom -- input ⟶ using \--> (this is a different arrow than \-> (→))
variables {h : hom c d} {g : hom b c} {f : hom a b} {i : hom a a}
--the following is the only theorem for which "include C" is necessary if C is a variable (why?)
theorem id_compose (a : ob) : (ID a) ∘ id = id := !id_left
theorem left_id_unique (H : Π{b} {f : hom b a}, i ∘ f = f) : i = id :=
calc i = i ∘ id : id_right
... = id : H
theorem right_id_unique (H : Π{b} {f : hom a b}, f ∘ i = f) : i = id :=
calc i = id ∘ i : id_left
... = id : H
end precategory
inductive Precategory : Type := mk : Π (ob : Type), precategory ob → Precategory
namespace precategory
definition Mk {ob} (C) : Precategory := Precategory.mk ob C
definition MK (a b c d e f g h) : Precategory := Precategory.mk a (precategory.mk b c d e f g h)
definition objects [coercion] [reducible] (C : Precategory) : Type
:= Precategory.rec (fun c s, c) C
definition category_instance [instance] [coercion] [reducible] (C : Precategory) : precategory (objects C)
:= Precategory.rec (fun c s, s) C
end precategory
open precategory
theorem Precategory.equal (C : Precategory) : Precategory.mk C C = C :=
Precategory.rec (λ ob c, idp) C
|
dcdde13934e446cc7c663f276196b35b22bc7df9 | eb586f8ae80b7045ba4ff08f1c7ba69403d61081 | /src/algebraic_countable_over_Q.lean | 9cbb806878770d1478f9918607a69b4f9745e55f | [] | no_license | bryangingechen/transcendental | 926584f58c47d766b304d39d1aa6814284535394 | 3692ac5ebe85e49c5f65bc7f2ea4f873a973bac2 | refs/heads/master | 1,668,733,660,442 | 1,594,269,161,000 | 1,594,269,161,000 | 278,266,099 | 0 | 0 | null | 1,594,270,314,000 | 1,594,270,313,000 | null | UTF-8 | Lean | false | false | 32,841 | lean | import data.set.basic set_theory.schroeder_bernstein
import data.set.countable
import ring_theory.algebraic
import data.polynomial
import data.rat.default
import data.real.basic data.real.cardinality
import tactic
noncomputable theory
open_locale classical
namespace project
def algebraic_set : set real := {x | is_algebraic rat x }
def roots_real (p : polynomial rat) : set real := {x | polynomial.aeval rat real x p = 0}
def poly_rat_to_poly_real (p : polynomial rat) : polynomial real :=
begin
constructor,
swap,
{
exact p.1,
},
swap,
{
intro n,
exact real.of_rat (p.2 n),
},
{
intro n,
split,
intro hn,
rw real.of_rat_eq_cast,
norm_cast,
exact (p.3 n).mp hn,
rw real.of_rat_eq_cast,
norm_cast,
exact (p.3 n).mpr,
},
done
end
def poly_rat_to_poly_real_wd (p : polynomial rat) : Prop := ∀ x : real, polynomial.aeval rat real x p = (poly_rat_to_poly_real p).eval x
theorem poly_rat_to_poly_real_preserve_deg (p : polynomial rat) : p.degree = (poly_rat_to_poly_real p).degree :=
begin
rw [poly_rat_to_poly_real, polynomial.degree, polynomial.degree], done
end
theorem poly_rat_to_poly_real_C_wd' (a : rat) : polynomial.C (real.of_rat a) = poly_rat_to_poly_real (polynomial.C a) :=
begin
ext,
unfold poly_rat_to_poly_real,
rw polynomial.coeff_C,
split_ifs,
simp only [rat.cast_inj, polynomial.coeff_mk, real.of_rat_eq_cast], rw h, rw <-polynomial.coeff, rw polynomial.coeff_C_zero,
simp only [polynomial.coeff_mk, real.of_rat_eq_cast], rw <-polynomial.coeff, rw polynomial.coeff_C, split_ifs, norm_cast,
done
end
theorem poly_rat_to_poly_real_C_wd : ∀ a : rat, poly_rat_to_poly_real_wd (polynomial.C a) :=
begin
intros r x,
rw <-poly_rat_to_poly_real_C_wd',
simp only [polynomial.eval_C, polynomial.aeval_C, real.of_rat_eq_cast],
exact rfl,
done
end
theorem poly_rat_to_poly_real_add (p1 p2 : polynomial rat) : poly_rat_to_poly_real (p1 + p2) = poly_rat_to_poly_real p1 + poly_rat_to_poly_real p2 :=
begin
ext,
rw [poly_rat_to_poly_real, poly_rat_to_poly_real, poly_rat_to_poly_real],
simp only [polynomial.coeff_add, polynomial.coeff_mk, real.of_rat_eq_cast],
norm_cast,
done
end
theorem poly_rat_to_poly_real_add_wd (p1 p2 : polynomial rat)
(h1 : poly_rat_to_poly_real_wd p1)
(h2 : poly_rat_to_poly_real_wd p2) : poly_rat_to_poly_real_wd (p1 + p2) :=
begin
intro x,
simp only [alg_hom.map_add],
rw h1, rw h2,
rw <-polynomial.eval_add,
rw poly_rat_to_poly_real_add,
done
end
theorem poly_rat_to_poly_real_pow1 (n : nat) : poly_rat_to_poly_real (polynomial.X ^ n) = polynomial.X ^ n :=
begin
ext, rename n_1 m,
rw poly_rat_to_poly_real, simp only [polynomial.coeff_X_pow, polynomial.coeff_mk, real.of_rat_eq_cast],
split_ifs;
norm_cast; rw [<-polynomial.coeff, polynomial.coeff_X_pow]; split_ifs; refl,
done
end
theorem poly_rat_to_poly_real_pow2 (n : nat) (a : rat) : poly_rat_to_poly_real ((polynomial.C a) * polynomial.X ^ n) = (polynomial.C (real.of_rat a)) * polynomial.X ^ n :=
begin
ext, rename n_1 m,
rw poly_rat_to_poly_real, simp only [mul_boole, polynomial.coeff_X_pow, polynomial.coeff_C_mul, polynomial.coeff_mk, real.of_rat_eq_cast],
norm_cast, rw [<-polynomial.coeff, polynomial.coeff_C_mul_X],
done
end
theorem poly_rat_to_poly_real_pow_wd (n : nat) (a : rat) (h : poly_rat_to_poly_real_wd ((polynomial.C a) * polynomial.X ^ n)) : poly_rat_to_poly_real_wd ((polynomial.C a) * polynomial.X ^ n.succ) :=
begin
intro x,
rw poly_rat_to_poly_real_pow2,
simp only [polynomial.aeval_def, polynomial.eval_X, polynomial.eval₂_C, polynomial.eval_C, polynomial.eval_pow,
polynomial.eval₂_mul, polynomial.eval_mul, real.of_rat_eq_cast],
rw polynomial.eval₂_pow,
rw <-polynomial.aeval_def,
rw polynomial.aeval_X,
exact rfl,
done
end
theorem poly_rat_to_poly_real_ne_zero (p : polynomial rat) : p ≠ 0 ↔ (poly_rat_to_poly_real p) ≠ 0 :=
begin
split,
intros h hp,
rw poly_rat_to_poly_real at hp,
rw polynomial.ext_iff at hp,
simp only [polynomial.coeff_zero, rat.cast_eq_zero, polynomial.coeff_mk, real.of_rat_eq_cast] at hp,
rw <-polynomial.coeff at hp,
have h' : p = 0,
exact finsupp.ext hp,
exact h h',
intros h hp,
have h' : poly_rat_to_poly_real p = 0,
unfold poly_rat_to_poly_real,
simp only [real.of_rat_eq_cast], ext, simp only [polynomial.coeff_zero, rat.cast_eq_zero, polynomial.coeff_mk], rw <-polynomial.coeff, rw hp, simp only [polynomial.coeff_zero],
exact h h',
done,
end
theorem poly_rat_to_poly_real_well_defined (x : real) (p : polynomial rat) :
poly_rat_to_poly_real_wd p :=
polynomial.induction_on
p
poly_rat_to_poly_real_C_wd
poly_rat_to_poly_real_add_wd
poly_rat_to_poly_real_pow_wd
def roots_real' (p : polynomial rat) : set real := {x | (poly_rat_to_poly_real p).eval x = 0}
theorem roots_real_eq_roots_real' (p : polynomial rat) : roots_real p = roots_real' p :=
begin
ext, split; intro hx,
unfold roots_real at hx,
rw set.mem_set_of_eq at hx,
unfold roots_real',
rw set.mem_set_of_eq,
have g := poly_rat_to_poly_real_well_defined x p,
unfold poly_rat_to_poly_real_wd at g,
have g' := g x,
rw hx at g',
exact eq.symm g',
unfold roots_real' at hx,
rw set.mem_set_of_eq at hx,
rw roots_real,
rw set.mem_set_of_eq,
have g := poly_rat_to_poly_real_well_defined x p,
unfold poly_rat_to_poly_real_wd at g,
have g' := g x,
rw hx at g',
exact g',
done
end
theorem roots_real'_eq_roots (p : polynomial rat) (hp : p ≠ 0) : roots_real' p = ↑((poly_rat_to_poly_real p).roots) :=
begin
ext,
have g := @polynomial.mem_roots real x _ (poly_rat_to_poly_real p) ((poly_rat_to_poly_real_ne_zero p).mp hp),
split,
intro hx, rw roots_real' at hx, rw set.mem_set_of_eq at hx,
simp only [finset.mem_coe], apply g.mpr, exact hx,
intro hx, simp only [finset.mem_coe] at hx, unfold roots_real', rw set.mem_set_of_eq,
exact g.mp hx,
done
end
theorem roots_real_eq_roots (p : polynomial rat) (hp : p ≠ 0) : roots_real p = ↑((poly_rat_to_poly_real p).roots) :=
begin
rw [roots_real_eq_roots_real', roots_real'_eq_roots], assumption, done
end
theorem roots_finite (p : polynomial rat) (hp : p ≠ 0) : set.finite (roots_real p) :=
begin
rw roots_real_eq_roots,
split, exact additive.fintype, assumption, done
end
-- Part I of project
def nat_set : set nat := @set.univ nat
def rat_n (n : nat) := fin n -> rat
def nat_n (n : nat) := fin n -> nat
def poly_n (n : nat) := {x : polynomial rat // x.nat_degree < n}
def poly_n' (n : nat) := {p : polynomial rat // p ≠ 0 ∧ p.nat_degree < n}
def rat_n' (n : nat) := {f : fin n -> rat // f ≠ 0}
def rat' := {r : rat // r ≠ 0}
theorem strange_fun_aux (q : rat) (hq : ¬(q.2 = 1 ∨ q.1 = 0)) : q ≠ 0 :=
begin
intro absurd, rw rat.zero_iff_num_zero at absurd,
exact (not_or_distrib.mp hq).2 absurd
end
def strange_fun (q : rat) : rat' :=
begin
by_cases (q.2 = 1 ∨ q.1 = 0); rename h is_q_int,
{
by_cases (q < 0), exact ⟨q, ne_of_lt h⟩,
exact ⟨q + 1, by linarith⟩,
},
{
exact ⟨q, strange_fun_aux q is_q_int⟩,
}
end
theorem strange_fun_inj : function.injective strange_fun :=
begin
{
intros q1 q2 hq,
by_cases (q1.2 = 1 ∨ q1.1 = 0); rename h q1_int;
by_cases (q2.2 = 1 ∨ q2.1 = 0); rename h q2_int,
{
simp only [strange_fun, q1_int, q2_int, dif_pos] at hq,
by_cases (q1 < 0); rename h q1_neg; by_cases (q2 < 0); rename h q2_neg,
simp only [q1_neg, q2_neg, dif_pos, subtype.mk_eq_mk] at hq, exact hq,
simp only [q1_neg, q2_neg, dif_pos, subtype.mk_eq_mk, dif_neg, not_false_iff] at hq, linarith,
simp only [q1_neg, q2_neg, dif_pos, subtype.mk_eq_mk, dif_neg, not_false_iff] at hq, linarith,
simp only [q1_neg, q2_neg, add_left_inj, subtype.mk_eq_mk, dif_neg, not_false_iff] at hq, exact hq,
},
{
simp only [strange_fun, strange_fun, q1_int, q2_int, dif_pos, dif_neg, not_false_iff] at hq,
by_cases (q1 < 0); rename h q1_neg; by_cases (q2 < 0); rename h q2_neg,
simp only [q1_neg, dif_pos, subtype.mk_eq_mk] at hq, assumption,
simp only [q1_neg, dif_pos, subtype.mk_eq_mk] at hq, assumption,
simp only [q1_neg, subtype.mk_eq_mk, dif_neg, not_false_iff] at hq, linarith,
simp only [q1_neg, subtype.mk_eq_mk, dif_neg, not_false_iff] at hq, cases q1_int,
{
simp only [rat.add_num_denom, q1_int, mul_one, rat.num_one, int.coe_nat_zero, int.coe_nat_succ, zero_add, rat.denom_one] at hq,
simp only [not_lt] at q1_neg,
have g := @rat.num_denom' (q1.num + 1) 1 (by linarith) (nat.coprime_one_right (int.nat_abs (q1.num + 1))),
norm_cast at g,
rw <-g at hq,
have h : ({ num := q1.num + 1, denom := 1, pos := (by linarith), cop := (nat.coprime_one_right (int.nat_abs (q1.num + 1))) } : rat).denom = q2.denom := by rw hq,
have h' : ({ num := q1.num + 1, denom := 1, pos := (by linarith), cop := (nat.coprime_one_right (int.nat_abs (q1.num + 1))) } : rat).denom = 1 := rfl,
rw h at h', rw not_or_distrib at q2_int, cases q2_int with q21 q22, exfalso, exact q21 h',
},
{
rw <-rat.zero_iff_num_zero at q1_int,
rw q1_int at hq, simp only [zero_add] at hq,
have h : q2.denom = 1,
exact calc q2.denom = (1 : rat).denom : by rw hq
... = 1 : rfl,
rw not_or_distrib at q2_int, cases q2_int with q21 q22, exfalso, exact q21 h,
},
},
{
simp only [strange_fun, strange_fun, q1_int, q2_int, dif_pos, dif_neg, not_false_iff] at hq,
by_cases (q1 < 0); rename h q1_neg; by_cases (q2 < 0); rename h q2_neg,
simp only [q2_neg, dif_pos, subtype.mk_eq_mk] at hq, exact hq, simp only [q2_neg, subtype.mk_eq_mk, dif_neg, not_false_iff] at hq,
linarith, split_ifs at hq, simp only [subtype.mk_eq_mk] at hq, exact hq, split_ifs at hq, simp only [subtype.mk_eq_mk] at hq,
cases q2_int with q22 q21,
have h : q1.denom = (q2 + 1).denom := by rw hq,
simp only [rat.add_num_denom, q22, mul_one, rat.num_one, int.coe_nat_zero, int.coe_nat_succ, zero_add, rat.denom_one] at h,
have g := @rat.num_denom' (q2.num + 1) 1 (by linarith) (nat.coprime_one_right (int.nat_abs (q2.num + 1))),
norm_cast at g,
rw <-g at h,
have h' : ({ num := q2.num + 1, denom := 1, pos := (by linarith), cop := (nat.coprime_one_right (int.nat_abs (q2.num + 1))) } : rat).denom = 1 := rfl,
rw h' at h, exfalso, exact (not_or_distrib.mp q1_int).1 h,
rw <-rat.zero_iff_num_zero at q21, simp only [q21, zero_add] at hq,
have h : q1.denom = 1,
exact calc q1.denom = (1 : rat).denom : by rw hq
... = 1 : rfl,
exfalso, exact (not_or_distrib.mp q1_int).1 h,
},
{
simp only [strange_fun, q1_int, q2_int, subtype.mk_eq_mk, dif_neg, not_false_iff] at hq,
by_cases (q1 < 0); rename h q1_neg; by_cases (q2 < 0); rename h q2_neg; assumption,
},
},
end
theorem strange_fun_sur : function.surjective strange_fun :=
begin
{
intro q,
by_cases (q.1.2 = 1 ∨ q.1.1 = 0); rename h q_int,
{
by_cases (q.1 < 0); rename h q_pos,
use q.1, simp only [strange_fun, q_int, q_pos, dif_pos, subtype.eta],
generalize h' : q.1-1 = q',
have q'_int : q'.2 = 1 ∨ q'.1 = 0,
{
cases q_int, left,
have h'' : q.1 + (-1 : rat) = q', linarith, rw rat.add_num_denom at h'', simp only [q_int, rat.num_neg_eq_neg_num, mul_one, rat.num_one, int.coe_nat_zero, int.coe_nat_succ, zero_add,
rat.denom_neg_eq_denom, rat.denom_one, int.mul_neg_eq_neg_mul_symm] at h'',
have h''' : ({ num := -1 + q.1.1, denom := 1, pos := (by linarith), cop := (nat.coprime_one_right (int.nat_abs (-1 + q.1.1))) } : rat) = rat.mk (-1 + q.1.1) 1,
rw rat.num_denom', norm_cast,
-- rw <-h''' at h'',
have H : q' = ({ num := -1 + q.1.1, denom := 1, pos := (by linarith), cop := (nat.coprime_one_right (int.nat_abs (-1 + q.1.1))) } : rat),
{
rw <-h'', rw add_comm, rw <-h''',
},
rw H,
left, rw <-rat.zero_iff_num_zero at q_int, simp only [q_int, zero_sub] at h', rw <-h', exact rfl,
},
have q_ne_0 := q.2, rw [not_lt] at q_pos,
have q_ge_1 : q.1 ≥ 1,
{
cases q_int,
have H : q.1 = q.1.1,
{
rw rat.coe_int_eq_mk,
have H : q.1 = { num := q.1.1, denom := q.1.2, pos := q.1.3, cop := q.1.4 }, {induction q.1, refl},
conv_lhs { rw H }, rw rat.num_denom', rw q_int, norm_cast,
},
have H2 : q.1 > 0 := lt_of_le_of_ne q_pos (ne.symm q_ne_0), rw H at H2, norm_cast at H2,
suffices H3 : q.1.1 ≥ 1, have H4 : (q.1.1 : rat) ≥ 1, norm_cast, assumption, rw <-H at H4, assumption, linarith,
exfalso, rw <-rat.zero_iff_num_zero at q_int, exact q_ne_0 q_int,
},
have H2 : q' ≥ 0, linarith,
have H2' : ¬ q' < 0, linarith, use q', simp only [strange_fun, q'_int, H2', dif_pos, dif_neg, not_false_iff],
apply subtype.eq, simp only [], linarith,
},
{
use q.1, simp only [strange_fun, q_int, subtype.eta, dif_neg, not_false_iff],
},
},
done
end
theorem rat_equiv_rat' : rat ≃ rat' :=
begin
suffices H : ∃ f : rat -> rat', function.bijective f,
choose f Hf using H, exact equiv.of_bijective f Hf,
use strange_fun,
split,
exact strange_fun_inj,
exact strange_fun_sur,
end
def zero_rat_n {n : nat} : rat_n n.succ := (fun m, 0)
def zero_poly_n {n : nat} : poly_n n.succ := ⟨0, nat.succ_pos n⟩
def identify (n : nat) : (poly_n n) -> (rat_n n) :=
begin
intro p,
intro m,
exact p.val.coeff m.val,
done
end
@[simp] theorem identify_0_eq_0 (n : nat) : (identify n.succ zero_poly_n) = zero_rat_n :=
begin
rw [identify, zero_rat_n, zero_poly_n], ext, simp only [id.def, polynomial.coeff_zero], done
end
lemma m_mod_n_lt_n : ∀ n : nat, n ≠ 0 -> ∀ m : nat, m % n < n :=
begin
intros n hn m,
have hn' : 0 < n := zero_lt_iff_ne_zero.mpr hn,
exact @nat.mod_lt m n hn',
end
theorem inj_identify_n : ∀ n : nat, (n ≠ 0) -> function.injective (identify n) :=
begin
intros n hn,
unfold function.injective,
intros p1 p2 h,
unfold identify at h,
simp only [id.def] at h,
rw subtype.ext_iff_val,
ext, rename n_1 m,
rw function.funext_iff at h,
have p1_deg := p1.property,
have p2_deg := p2.property,
-- consider m = n, m < n, m > n seprately,
cases (nat.lt_trichotomy m n) with ineq,
-- m < n
{
let m' := (⟨m, ineq⟩ : fin n),
have h_m := h m',
have g : m'.val = m,
{
exact rfl,
},
rw [<-g, h_m],
},
rename h_1 ineq,
cases ineq,
-- m = n
{
rw ineq,
rw [(@polynomial.coeff_eq_zero_of_nat_degree_lt rat _ p1.val n p1_deg),
(@polynomial.coeff_eq_zero_of_nat_degree_lt rat _ p2.val n p2_deg)],
},
-- n < m
{
rw @polynomial.coeff_eq_zero_of_nat_degree_lt rat _ p1.val m (lt.trans p1_deg ineq),
rw @polynomial.coeff_eq_zero_of_nat_degree_lt rat _ p2.val m (lt.trans p2_deg ineq),
}
end
theorem identify_nzero_to_nzero (n : nat) (p : poly_n n.succ) (hp : p ≠ zero_poly_n) : (identify n.succ p) ≠ zero_rat_n :=
begin
have g := inj_identify_n n.succ (nat.succ_ne_zero n),
have g' := @g p zero_poly_n, simp only [identify_0_eq_0] at g',
intro absurd, exact hp (g' absurd),
end
theorem sur_identify_n : ∀ n : nat, (n ≠ 0) -> function.surjective (identify n) :=
begin
intros n hn,
unfold function.surjective,
intro q,
let support : finset nat := finset.filter (λ m : nat, (q (⟨m % n, m_mod_n_lt_n n hn m⟩ : fin n)) ≠ 0) (finset.range n),
have hsupport : support = finset.filter (λ m : nat, (q (⟨m % n, m_mod_n_lt_n n hn m⟩ : fin n)) ≠ 0) (finset.range n) := rfl,
let to_fun : nat -> rat := (λ m : nat, ite (m ∈ support) (q (⟨m % n, m_mod_n_lt_n n hn m⟩ : fin n)) 0),
have hto_fun : to_fun = (λ m : nat, ite (m ∈ support) (q (⟨m % n, m_mod_n_lt_n n hn m⟩ : fin n)) 0) := rfl,
let mem_support_to_fun : ∀a, a ∈ support ↔ to_fun a ≠ 0,
{
intro m,
split,
intro hm,
have hm'' := hm,
rw hsupport at hm,
have hm' := hm,
rw finset.mem_filter at hm',
cases hm',
rename hm'_right qm_ne_0,
rw hto_fun,
have g : (λ (m : ℕ), ite (m ∈ support) (q ⟨m % n, m_mod_n_lt_n n hn m⟩) 0) m = ite (m ∈ support) (q ⟨m % n, m_mod_n_lt_n n hn m⟩) 0 := rfl,
rw g,
split_ifs,
exact qm_ne_0,
intro hm,
have hm' := hm,
rw hto_fun at hm',
have g : (λ (m : ℕ), ite (m ∈ support) (q ⟨m % n, m_mod_n_lt_n n hn m⟩) 0) m = ite (m ∈ support) (q ⟨m % n, m_mod_n_lt_n n hn m⟩) 0 := rfl,
rw g at hm',
have g : ite (m ∈ support) (q ⟨m % n, m_mod_n_lt_n n hn m⟩) 0 ≠ 0 -> (m ∈ support),
{
intro h,
by_contra,
split_ifs at h,
have h' : (0 : rat) = 0 := rfl,
exact h h',
},
exact g hm',
},
let p : polynomial rat := ⟨support, to_fun, mem_support_to_fun⟩,
have hp_support : p.support = finset.filter (λ (m : ℕ), q ⟨m % n, m_mod_n_lt_n n hn m⟩ ≠ 0) (finset.range n) := rfl,
have hp_support2 : ∀ m ∈ p.support, m < n,
{
rw hp_support,
intro m,
rw finset.mem_filter,
intro hm,
cases hm,
exact list.mem_range.mp hm_left,
},
have hp_deg : (p.degree ≠ ⊥) -> p.degree < n,
{
intro hp_deg_not_bot,
rw polynomial.degree,
rw finset.sup_lt_iff,
intros m hm,
have hmn := hp_support2 m hm,
swap,
exact @with_bot.bot_lt_coe nat _ n,
have g := @with_bot.some_eq_coe nat n,
rw <-g,
rw with_bot.some_lt_some,
exact hmn,
},
have hp_nat_deg : p.nat_degree < n,
{
by_cases (p = 0),
rename h hp_eq_0,
have g := polynomial.nat_degree_zero,
rw <-hp_eq_0 at g,
rw g,
rw zero_lt_iff_ne_zero,
exact hn,
rename h hp_ne_0,
have p_deg_ne_bot : p.degree ≠ ⊥,
{
intro gg,
rw polynomial.degree_eq_bot at gg,
exact hp_ne_0 gg,
},
have hp_deg' := hp_deg p_deg_ne_bot,
have g := polynomial.degree_eq_nat_degree hp_ne_0,
rw g at hp_deg',
rw <-with_bot.coe_lt_coe,
exact hp_deg',
},
use ⟨p, hp_nat_deg⟩,
{
ext,
rename x m,
unfold identify,
simp only [id.def, polynomial.coeff_mk],
rw hto_fun,
have g : (λ (m : ℕ), ite (m ∈ support) (q ⟨m % n, m_mod_n_lt_n n hn m⟩) 0) (m.val) = ite (m.val ∈ support) (q ⟨m.val % n, m_mod_n_lt_n n hn m.val⟩) 0 := rfl,
rw g,
split_ifs,
-- m.val ∈ support
{
simp only [ne.def, finset.mem_filter, finset.mem_range] at h,
cases h,
have important : m = ⟨m.1 % n, _⟩,
{
rw fin.ext_iff,
simp only [],
rw nat.mod_eq_of_lt,
exact h_left,
},
rw <-important,
},
-- m.val ∉ support
{
simp only [not_and, finset.mem_filter, finset.mem_range, classical.not_not] at h,
have h' := h m.2,
have important : m = ⟨m.1 % n, _⟩,
{
rw fin.ext_iff,
simp only [],
rw nat.mod_eq_of_lt,
exact m.2,
},
rw <-important at h',
exact eq.symm h',
},
},
end
theorem poly_n'_equiv_rat_n' (n : nat) : poly_n' n.succ ≃ rat_n' n.succ :=
begin
let f : (poly_n' n.succ) -> (rat_n' n.succ),
{
intro p,
generalize hx : (identify n.succ) ⟨p.1, p.2.2⟩ = x,
split, swap, exact x,
have g := identify_nzero_to_nzero n ⟨p.1, p.2.2⟩ _,
rw hx at g, exact g, rw [zero_poly_n], simp only [subtype.mk_eq_mk, ne.def], exact p.2.1,
},
suffices f_bij : function.bijective f,
exact equiv.of_bijective f f_bij,
split,
{
intros x1 x2 hx, simp only [subtype.mk_eq_mk] at hx, rw [identify, function.funext_iff] at hx, simp only [id.def] at hx,
suffices h : x1.val = x2.val, exact subtype.eq h,
ext,
have h1 := x1.2.2, have h2 := x2.2.2, rename n_1 m,
by_cases (m ≥ n.succ),
rw [polynomial.coeff_eq_zero_of_nat_degree_lt, polynomial.coeff_eq_zero_of_nat_degree_lt],
exact lt_of_lt_of_le h2 h, exact lt_of_lt_of_le h1 h,
simp only [not_lt] at h, rw hx ⟨m, _⟩, exact nat.lt_succ_iff.mpr h,
},
{
intro q,
have h := sur_identify_n n.succ (nat.succ_ne_zero n) q.1,
choose p hp using h,
have p_ne_0' : p ≠ zero_poly_n,
{
by_contra absurd, rw [ne.def, not_not] at absurd,
have absurd' := identify_0_eq_0 n,
rw <-absurd at absurd', rw hp at absurd', exact q.2 absurd',
},
have p_ne_0 : p.1 ≠ 0,
{
intro hp,
have h : 0 = zero_poly_n.1 := rfl, rw h at hp,
exact p_ne_0' (@subtype.eq _ _ p zero_poly_n hp),
},
use ⟨p.1, ⟨p_ne_0, p.2⟩⟩,
rw [subtype.ext_iff_val], simp only [subtype.eta], assumption,
},
done
end
def f : rat_n 1 -> rat_n' 1 :=
begin
intro r, split, swap, exact (fun m, (strange_fun (r m)).1),
intro a, rw function.funext_iff at a, replace a := a 0, simp only [pi.zero_apply] at a,
generalize hx : (strange_fun (r 0)) = x, rw hx at a, exact x.2 a,
end
def g : rat_n' 1 -> rat_n 1 :=
begin
intros g n, exact g.1 n,
end
theorem rat_1_equiv_rat_1' : rat_n 1 ≃ rat_n' 1 :=
begin
suffices H1 : ∃ f : rat_n 1 -> rat_n' 1, function.bijective f,
choose h hh using H1,
exact equiv.of_bijective h hh,
suffices H2 : ∃ f : rat_n 1 -> rat_n' 1, ∃ g : rat_n' 1 -> rat_n 1, function.injective f ∧ function.injective g,
choose f g h using H2, exact function.embedding.schroeder_bernstein h.1 h.2,
use f, use g, split,
{
intros x1 x2 hx, simp only [f, subtype.mk_eq_mk] at hx, rw function.funext_iff at hx, replace hx := hx 0,
replace hx := subtype.eq hx, replace hx := strange_fun_inj hx, ext, fin_cases x, exact hx,
},
{
intros x1 x2 hx, simp only [g, id.def] at hx, replace hx := subtype.eq hx, exact hx,
}
end
def fn {n : nat} : rat_n (nat.succ (nat.succ n)) -> rat_n (nat.succ n) × rat :=
begin
intro r, split, intro m, exact r (⟨m.1, nat.lt_trans m.2 (nat.lt_succ_self n.succ)⟩ : fin n.succ.succ),
exact r (⟨n.succ, nat.lt_succ_self n.succ⟩ : fin n.succ.succ),
end
def gn {n : nat} : rat_n (nat.succ n) × rat -> rat_n (nat.succ (nat.succ n)) :=
begin
intros r m, cases r with p r,
by_cases (m.1 = n.succ), exact r,
have hm' := m.2,
have hm : m.1 < n.succ,
replace hm : m.1 ≤ n.succ, exact fin.le_last m, exact lt_of_le_of_ne hm h,
exact p (⟨m.1, hm⟩ : fin n.succ),
end
theorem aux_rat_n (n : nat) : rat_n (nat.succ (nat.succ n)) ≃ rat_n (nat.succ n) × rat :=
begin
suffices H1 : ∃ f : rat_n n.succ.succ -> rat_n n.succ × rat, function.bijective f,
choose h hh using H1,
exact equiv.of_bijective h hh,
suffices H2 : ∃ f : rat_n n.succ.succ -> rat_n n.succ × rat, ∃ g : rat_n n.succ × rat -> rat_n n.succ.succ, function.injective f ∧ function.injective g,
choose f g h using H2, exact function.embedding.schroeder_bernstein h.1 h.2,
use fn, use gn, split,
{
intros x1 x2 hx, simp only [fn, id.def, prod.mk.inj_iff] at hx, cases hx with h1 h2, rw function.funext_iff at h1, ext,
by_cases (x = ⟨n.succ, nat.lt_succ_self n.succ⟩), rw <-h at h2, assumption,
simp only [fin.eq_iff_veq] at h, have h2 := x.2, replace h2 : x.1 ≤ n.succ := fin.le_last x,
have h3 : x.1 < n.succ := lt_of_le_of_ne h2 h,
have H := h1 ⟨x.1, h3⟩, simp only [fin.eta] at H, exact H,
},
{
intros x1 x2 hx, cases x1 with p1 x1, cases x2 with p2 x2,
ext, swap, simp only [], simp only [gn, id.def] at hx, rw function.funext_iff at hx,
generalize hm : (⟨n.succ, nat.lt_succ_self n.succ⟩ : fin n.succ.succ) = m,
have hm' : m.val = n.succ, rw <-hm, replace hx := hx m, simp only [hm', dif_pos] at hx, assumption,
simp only [], simp only [gn, id.def] at hx, rw function.funext_iff at hx,
generalize hm : (⟨x.1, nat.lt_trans x.2 (nat.lt_succ_self n.succ)⟩ : fin n.succ.succ) = m,
replace hx := hx m, have hm' : m.1 ≠ n.succ,
intro a, rw <-hm at a, simp only [] at a, have hx' := x.2, linarith,
simp only [hm', dif_neg, not_false_iff] at hx, have hm2 := m.2, replace hm2 : m.1 ≤ n.succ, exact fin.le_last m,
have hm3 : m.1 < n.succ, exact lt_of_le_of_ne hm2 hm',
have H : x = ⟨m.1, hm3⟩, rw fin.ext_iff at hm ⊢, simp only [] at hm ⊢, exact hm,
rw H, exact hx,
},
end
def fn' {n : nat} : rat_n' (nat.succ (nat.succ n)) -> rat_n (nat.succ n) × rat :=
begin
intro p, split, intro m,
exact p.1 (⟨m.1, nat.lt_trans m.2 (nat.lt_succ_self n.succ)⟩ : fin n.succ.succ),
exact p.1 (⟨n.succ, nat.lt_succ_self n.succ⟩ : fin n.succ.succ),
end
def gn' {n : nat} : rat_n (nat.succ n) × rat -> rat_n' (nat.succ (nat.succ n)) :=
begin
intro x,
split, swap 2, intro m,
by_cases (m.1 = n.succ), exact (strange_fun x.2).1,
have H2 : m.1 < n.succ, have H2' := fin.le_last m, exact lt_of_le_of_ne H2' h,
exact (strange_fun (x.1 ⟨m.1, H2⟩)).1,
intro a, rw function.funext_iff at a, simp only [pi.zero_apply, subtype.val_eq_coe] at a,
generalize hm : (⟨n.succ, nat.lt_succ_self n.succ⟩ : fin n.succ.succ) = m,
have a' := a m, have h : m.1 = n.succ, rw <-hm, simp only [h, dif_pos] at a', exact (strange_fun x.2).2 a',
end
theorem aux_rat_n' (n : nat) : rat_n' (nat.succ (nat.succ n)) ≃ rat_n (nat.succ n) × rat :=
begin
suffices H1 : ∃ f : rat_n' n.succ.succ -> rat_n n.succ × rat, function.bijective f,
choose h hh using H1,
exact equiv.of_bijective h hh,
suffices H2 : ∃ f : rat_n' n.succ.succ -> rat_n n.succ × rat, ∃ g : rat_n n.succ × rat -> rat_n' n.succ.succ, function.injective f ∧ function.injective g,
choose f g h using H2, exact function.embedding.schroeder_bernstein h.1 h.2,
use fn', use gn', split,
{
intros x1 x2 hx, simp only [fn', id.def, prod.mk.inj_iff, subtype.val_eq_coe] at hx, cases hx with h1 h2, rw function.funext_iff at h1,
apply subtype.eq, ext m,
by_cases (m.val = n.succ),
have H1 : m = (⟨n.succ, (nat.lt_succ_self n.succ)⟩ : fin n.succ.succ),
rw fin.ext_iff, simp only [], exact h,
rw H1, assumption,
have H2 : m.1 < n.succ, have H2' := fin.le_last m, exact lt_of_le_of_ne H2' h,
have h1m := h1 ⟨m.1, H2⟩, simp only [fin.eta] at h1m, exact h1m,
},
{
intros x1 x2 hx, simp only [gn', subtype.mk_eq_mk, subtype.val_eq_coe] at hx, ext, rename x m,
generalize hm' : (⟨m.1, nat.lt_trans m.2 (nat.lt_succ_self n.succ)⟩ : fin n.succ.succ) = m',
have hm'' : m'.val = m.1, rw <-hm',
have Hm' : m'.val ≠ n.succ, have Hm'' := ne_of_lt m.2, rw <-hm'' at Hm'', assumption,
rw function.funext_iff at hx, have H := hx m', simp only [hm'', Hm', dif_neg, not_false_iff, fin.eta] at H,
split_ifs at H,
replace H := subtype.eq H,
exact congr_fun (false.rec (x1.fst = λ (m : fin n.succ), x2.fst m) (Hm' h)) m, replace H := subtype.eq H, exact strange_fun_inj H,
generalize hm : (⟨n.succ, nat.lt_succ_self n.succ⟩ : fin n.succ.succ) = m,
have Hm : m.val = n.succ, rw <-hm, rw function.funext_iff at hx,
have H := hx m, simp only [Hm, dif_pos] at H, replace H := subtype.eq H,
exact strange_fun_inj H,
}
end
theorem rat_n_equiv_rat_n' (n : nat) : rat_n n.succ ≃ rat_n' n.succ :=
begin
induction n with n hn,
exact rat_1_equiv_rat_1',
suffices H1 : rat_n n.succ.succ ≃ rat_n n.succ × rat,
suffices H2 : rat_n' n.succ.succ ≃ rat_n' n.succ × rat',
have e1 : rat_n n.succ × rat ≃ rat_n' n.succ × rat', apply equiv.prod_congr,
exact hn, exact rat_equiv_rat',
exact equiv.trans (equiv.trans H1 e1) H2.symm, swap, exact aux_rat_n n,
have e2 := aux_rat_n' n, suffices H3 : rat_n (nat.succ n) × rat ≃ rat_n' (nat.succ n) × rat', exact equiv.trans e2 H3,
apply equiv.prod_congr, exact hn, exact rat_equiv_rat',
end
def algebraic_set'_n (n : nat) : set real := ⋃ p : (poly_n' n.succ), roots_real p.1
def algebraic_set' : set real := ⋃ n : nat, algebraic_set'_n n.succ
theorem algebraic_set'_eq_algebraic_set : algebraic_set' = algebraic_set :=
begin
ext, split; intro hx,
{
rw [algebraic_set', set.mem_Union] at hx,
choose n hx using hx,
rw [algebraic_set'_n, set.mem_Union] at hx,
choose p hp using hx, rw [roots_real, set.mem_set_of_eq] at hp,
rw [algebraic_set, set.mem_set_of_eq, is_algebraic],
use p.val, split, exact p.2.1,assumption
},
{
rw [algebraic_set, set.mem_set_of_eq] at hx,
choose p hp using hx,
cases hp with p_ne_0 p_x_0,
rw [algebraic_set', set.mem_Union],
use p.nat_degree.succ, rw [algebraic_set'_n, set.mem_Union],
use p, split, exact p_ne_0,
{
suffices h1 : p.nat_degree < p.nat_degree.succ,
suffices h2 : p.nat_degree.succ < p.nat_degree.succ.succ,
suffices h3 : p.nat_degree.succ.succ < p.nat_degree.succ.succ.succ,
exact lt.trans (lt.trans h1 h2) h3,
exact lt_add_one p.nat_degree.succ.succ, exact lt_add_one (nat.succ (polynomial.nat_degree p)),
exact lt_add_one (polynomial.nat_degree p),
},
simpa only [],
},
done
end
theorem poly_n'_1_coeff_ne_0 (q : poly_n' 1) : q.1.coeff 0 ≠ 0 :=
begin
have h := q.2,
cases h with h1 h2,
have h : q.1 = polynomial.C (q.1.coeff 0), ext,
by_cases (n = 0), rw h, simp only [polynomial.coeff_C_zero],
rw polynomial.coeff_C, simp only [h, if_false], rw polynomial.coeff_eq_zero_of_nat_degree_lt,
suffices H : 1 ≤ n, linarith, replace h : n ≠ 0 := h, rw <-nat.lt_one_add_iff, norm_num, exact zero_lt_iff_ne_zero.mpr h,
rw h, simp only [polynomial.coeff_C_zero, ne.def], intro absurd, conv_rhs at h {rw absurd}, suffices H2 : polynomial.C (0 : rat) = 0, conv_rhs at h {rw H2}, exact h1 h,
ext, simp only [ring_hom.map_zero],
end
def identify'_1 : (poly_n' 1) -> rat' :=
begin
intro q, split, swap, exact q.1.coeff 0, exact poly_n'_1_coeff_ne_0 q,
end
theorem rat_1_equiv_rat : rat_n 1 ≃ rat :=
begin
constructor, swap 3, {
intro f, exact f ⟨0, by linarith⟩,
}, swap 3, {
intros r m, exact r,
}, {
intros x, simp only [id.def], ext, rename x_1 m,
have hm' : m.1 = 0, have hm1 := m.2, linarith,
have hm : m = ⟨0, by linarith⟩, rw fin.ext_iff, simp only [], exact hm', rw hm,
}, {
intros x, simp only [id.def],
}
end
theorem rat_n_denumerable {n : nat} : denumerable (rat_n n.succ) :=
begin
induction n with n hn,
apply denumerable.mk', suffices H : rat_n 1 ≃ rat, apply equiv.trans H, exact denumerable.eqv rat,
exact rat_1_equiv_rat,
apply denumerable.mk',
have Hn := @denumerable.eqv (rat_n (nat.succ n)) hn,
have e1 := aux_rat_n n, suffices H : rat_n (nat.succ n) × ℚ ≃ nat, exact equiv.trans e1 H,
have e2 : rat_n (nat.succ n) × ℚ ≃ nat × rat, apply equiv.prod_congr, exact Hn, refl,
suffices H : ℕ × ℚ ≃ nat, exact equiv.trans e2 H, exact denumerable.eqv (ℕ × ℚ),
end
theorem poly_n'_denumerable (n : nat) : denumerable (poly_n' n.succ) :=
begin
apply denumerable.mk',
suffices e1 : rat_n' n.succ ≃ nat, exact equiv.trans (poly_n'_equiv_rat_n' n) e1,
suffices e2 : rat_n n.succ ≃ nat, exact equiv.trans (rat_n_equiv_rat_n' n).symm e2,
exact @denumerable.eqv _ (rat_n_denumerable),
done
end
theorem algebraic_set'_n_countable (n : nat) : set.countable $ algebraic_set'_n n :=
begin
rw algebraic_set'_n,
have h := @set.countable_Union (poly_n' n.succ) real (fun p, roots_real p.1) (poly_n'_denumerable n).1,
simp only [subtype.val_eq_coe] at h, apply h,
intro q, apply set.finite.countable, exact roots_finite q.1 q.2.1,
done
end
theorem algebraic_set'_countable : set.countable algebraic_set' :=
begin
apply set.countable_Union,
intro n, exact algebraic_set'_n_countable n.succ,
end
theorem countable_algebraic_set : set.countable algebraic_set :=
begin
rw <-algebraic_set'_eq_algebraic_set, exact algebraic_set'_countable, done
end
def real_set : set real := @set.univ real
theorem transcendental_number_exists : ∃ x : real, ¬ (is_algebraic rat x) :=
begin
have H : algebraic_set ≠ real_set,
{
intro h1,
have h2 : set.countable real_set,
{
rw <-h1, exact countable_algebraic_set,
},
have h3 : ¬ set.countable real_set := cardinal.not_countable_real,
exact h3 h2,
},
rw [ne.def, set.ext_iff, not_forall] at H,
choose x Hx using H,
rw not_iff at Hx,
replace Hx := Hx.mpr,
use x,
exact Hx trivial,
done
end
end project |
e2693a72d25e005ac35ac6231e05581097f6dcbe | 31f556cdeb9239ffc2fad8f905e33987ff4feab9 | /src/Lean/Elab/MutualDef.lean | fac8407b6c4577ca6dbfef50acf710aaeaeaf9b6 | [
"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 | 39,539 | 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.Parser.Term
import Lean.Meta.Closure
import Lean.Meta.Check
import Lean.Meta.Transform
import Lean.PrettyPrinter.Delaborator.Options
import Lean.Elab.Command
import Lean.Elab.Match
import Lean.Elab.DefView
import Lean.Elab.Deriving.Basic
import Lean.Elab.PreDefinition.Main
import Lean.Elab.DeclarationRange
namespace Lean.Elab
open Lean.Parser.Term
/-- `DefView` after elaborating the header. -/
structure DefViewElabHeader where
ref : Syntax
modifiers : Modifiers
/-- Stores whether this is the header of a definition, theorem, ... -/
kind : DefKind
/--
Short name. Recall that all declarations in Lean 4 are potentially recursive. We use `shortDeclName` to refer
to them at `valueStx`, and other declarations in the same mutual block. -/
shortDeclName : Name
/-- Full name for this declaration. This is the name that will be added to the `Environment`. -/
declName : Name
/-- Universe level parameter names explicitly provided by the user. -/
levelNames : List Name
/-- Syntax objects for the binders occurring befor `:`, we use them to populate the `InfoTree` when elaborating `valueStx`. -/
binderIds : Array Syntax
/-- Number of parameters before `:`, it also includes auto-implicit parameters automatically added by Lean. -/
numParams : Nat
/-- Type including parameters. -/
type : Expr
/-- `Syntax` object the body/value of the definition. -/
valueStx : Syntax
deriving Inhabited
namespace Term
open Meta
private def checkModifiers (m₁ m₂ : Modifiers) : TermElabM Unit := do
unless m₁.isUnsafe == m₂.isUnsafe do
throwError "cannot mix unsafe and safe definitions"
unless m₁.isNoncomputable == m₂.isNoncomputable do
throwError "cannot mix computable and non-computable definitions"
unless m₁.isPartial == m₂.isPartial do
throwError "cannot mix partial and non-partial definitions"
private def checkKinds (k₁ k₂ : DefKind) : TermElabM Unit := do
unless k₁.isExample == k₂.isExample do
throwError "cannot mix examples and definitions" -- Reason: we should discard examples
unless k₁.isTheorem == k₂.isTheorem do
throwError "cannot mix theorems and definitions" -- Reason: we will eventually elaborate theorems in `Task`s.
private def check (prevHeaders : Array DefViewElabHeader) (newHeader : DefViewElabHeader) : TermElabM Unit := do
if newHeader.kind.isTheorem && newHeader.modifiers.isUnsafe then
throwError "'unsafe' theorems are not allowed"
if newHeader.kind.isTheorem && newHeader.modifiers.isPartial then
throwError "'partial' theorems are not allowed, 'partial' is a code generation directive"
if newHeader.kind.isTheorem && newHeader.modifiers.isNoncomputable then
throwError "'theorem' subsumes 'noncomputable', code is not generated for theorems"
if newHeader.modifiers.isNoncomputable && newHeader.modifiers.isUnsafe then
throwError "'noncomputable unsafe' is not allowed"
if newHeader.modifiers.isNoncomputable && newHeader.modifiers.isPartial then
throwError "'noncomputable partial' is not allowed"
if newHeader.modifiers.isPartial && newHeader.modifiers.isUnsafe then
throwError "'unsafe' subsumes 'partial'"
if h : 0 < prevHeaders.size then
let firstHeader := prevHeaders.get ⟨0, h⟩
try
unless newHeader.levelNames == firstHeader.levelNames do
throwError "universe parameters mismatch"
checkModifiers newHeader.modifiers firstHeader.modifiers
checkKinds newHeader.kind firstHeader.kind
catch
| .error ref msg => throw (.error ref m!"invalid mutually recursive definitions, {msg}")
| ex => throw ex
else
pure ()
private def registerFailedToInferDefTypeInfo (type : Expr) (ref : Syntax) : TermElabM Unit :=
registerCustomErrorIfMVar type ref "failed to infer definition type"
/--
Return `some [b, c]` if the given `views` are representing a declaration of the form
```
opaque a b c : Nat
``` -/
private def isMultiConstant? (views : Array DefView) : Option (List Name) :=
if views.size == 1 &&
views[0]!.kind == .opaque &&
views[0]!.binders.getArgs.size > 0 &&
views[0]!.binders.getArgs.all (·.isIdent) then
some (views[0]!.binders.getArgs.toList.map (·.getId))
else
none
private def getPendindMVarErrorMessage (views : Array DefView) : String :=
match isMultiConstant? views with
| some ids =>
let idsStr := ", ".intercalate <| ids.map fun id => s!"`{id}`"
let paramsStr := ", ".intercalate <| ids.map fun id => s!"`({id} : _)`"
s!"\nrecall that you cannot declare multiple constants in a single declaration. The identifier(s) {idsStr} are being interpreted as parameters {paramsStr}"
| none =>
"\nwhen the resulting type of a declaration is explicitly provided, all holes (e.g., `_`) in the header are resolved before the declaration body is processed"
/--
Convert terms of the form `OfNat <type> (OfNat.ofNat Nat <num> ..)` into `OfNat <type> <num>`.
We use this method on instance declaration types.
The motivation is to address a recurrent mistake when users forget to use `nat_lit` when declaring `OfNat` instances.
See issues #1389 and #875
-/
private def cleanupOfNat (type : Expr) : MetaM Expr := do
Meta.transform type fun e => do
if !e.isAppOfArity ``OfNat 2 then return .continue
let arg ← instantiateMVars e.appArg!
if !arg.isAppOfArity ``OfNat.ofNat 3 then return .continue
let argArgs := arg.getAppArgs
if !argArgs[0]!.isConstOf ``Nat then return .continue
let eNew := mkApp e.appFn! argArgs[1]!
return .done eNew
/-- Elaborate only the declaration headers. We have to elaborate the headers first because we support mutually recursive declarations in Lean 4. -/
private def elabHeaders (views : Array DefView) : TermElabM (Array DefViewElabHeader) := do
let expandedDeclIds ← views.mapM fun view => withRef view.ref do
Term.expandDeclId (← getCurrNamespace) (← getLevelNames) view.declId view.modifiers
withAutoBoundImplicitForbiddenPred (fun n => expandedDeclIds.any (·.shortName == n)) do
let mut headers := #[]
for view in views, ⟨shortDeclName, declName, levelNames⟩ in expandedDeclIds do
let newHeader ← withRef view.ref do
addDeclarationRanges declName view.ref
applyAttributesAt declName view.modifiers.attrs .beforeElaboration
withDeclName declName <| withAutoBoundImplicit <| withLevelNames levelNames <|
elabBindersEx view.binders.getArgs fun xs => do
let refForElabFunType := view.value
let mut type ← match view.type? with
| some typeStx =>
let type ← elabType typeStx
registerFailedToInferDefTypeInfo type typeStx
pure type
| none =>
let hole := mkHole refForElabFunType
let type ← elabType hole
trace[Elab.definition] ">> type: {type}\n{type.mvarId!}"
registerFailedToInferDefTypeInfo type refForElabFunType
pure type
Term.synthesizeSyntheticMVarsNoPostponing
if view.isInstance then
type ← cleanupOfNat type
let (binderIds, xs) := xs.unzip
-- TODO: add forbidden predicate using `shortDeclName` from `views`
let xs ← addAutoBoundImplicits xs
type ← mkForallFVars' xs type
type ← instantiateMVars type
let levelNames ← getLevelNames
if view.type?.isSome then
let pendingMVarIds ← getMVars type
discard <| logUnassignedUsingErrorInfos pendingMVarIds <|
getPendindMVarErrorMessage views
let newHeader := {
ref := view.ref
modifiers := view.modifiers
kind := view.kind
shortDeclName := shortDeclName
declName, type, levelNames, binderIds
numParams := xs.size
valueStx := view.value : DefViewElabHeader }
check headers newHeader
return newHeader
headers := headers.push newHeader
return headers
/--
Create auxiliary local declarations `fs` for the given hearders using their `shortDeclName` and `type`, given hearders, and execute `k fs`.
The new free variables are tagged as `auxDecl`.
Remark: `fs.size = headers.size`.
-/
private partial def withFunLocalDecls {α} (headers : Array DefViewElabHeader) (k : Array Expr → TermElabM α) : TermElabM α :=
let rec loop (i : Nat) (fvars : Array Expr) := do
if h : i < headers.size then
let header := headers.get ⟨i, h⟩
if header.modifiers.isNonrec then
loop (i+1) fvars
else
withAuxDecl header.shortDeclName header.type header.declName fun fvar => loop (i+1) (fvars.push fvar)
else
k fvars
loop 0 #[]
private def expandWhereStructInst : Macro
| `(Parser.Command.whereStructInst|where $[$decls:letDecl];* $[$whereDecls?:whereDecls]?) => do
let letIdDecls ← decls.mapM fun stx => match stx with
| `(letDecl|$_decl:letPatDecl) => Macro.throwErrorAt stx "patterns are not allowed here"
| `(letDecl|$decl:letEqnsDecl) => expandLetEqnsDecl decl (useExplicit := false)
| `(letDecl|$decl:letIdDecl) => pure decl
| _ => Macro.throwUnsupported
let structInstFields ← letIdDecls.mapM fun
| stx@`(letIdDecl|$id:ident $binders* $[: $ty?]? := $val) => withRef stx do
let mut val := val
if let some ty := ty? then
val ← `(($val : $ty))
-- HACK: this produces invalid syntax, but the fun elaborator supports letIdBinders as well
have : Coe (TSyntax ``letIdBinder) (TSyntax ``funBinder) := ⟨(⟨·⟩)⟩
val ← if binders.size > 0 then `(fun $binders* => $val) else pure val
`(structInstField|$id:ident := $val)
| _ => Macro.throwUnsupported
let body ← `({ $structInstFields,* })
match whereDecls? with
| some whereDecls => expandWhereDecls whereDecls body
| none => return body
| _ => Macro.throwUnsupported
/-
Recall that
```
def declValSimple := leading_parser " :=\n" >> termParser >> optional Term.whereDecls
def declValEqns := leading_parser Term.matchAltsWhereDecls
def declVal := declValSimple <|> declValEqns <|> Term.whereDecls
```
-/
private def declValToTerm (declVal : Syntax) : MacroM Syntax := withRef declVal do
if declVal.isOfKind ``Parser.Command.declValSimple then
expandWhereDeclsOpt declVal[2] declVal[1]
else if declVal.isOfKind ``Parser.Command.declValEqns then
expandMatchAltsWhereDecls declVal[0]
else if declVal.isOfKind ``Parser.Command.whereStructInst then
expandWhereStructInst declVal
else if declVal.isMissing then
Macro.throwErrorAt declVal "declaration body is missing"
else
Macro.throwErrorAt declVal "unexpected declaration body"
private def elabFunValues (headers : Array DefViewElabHeader) : TermElabM (Array Expr) :=
headers.mapM fun header => withDeclName header.declName <| withLevelNames header.levelNames do
let valStx ← liftMacroM <| declValToTerm header.valueStx
forallBoundedTelescope header.type header.numParams fun xs type => do
-- Add new info nodes for new fvars. The server will detect all fvars of a binder by the binder's source location.
for i in [0:header.binderIds.size] do
-- skip auto-bound prefix in `xs`
addLocalVarInfo header.binderIds[i]! xs[header.numParams - header.binderIds.size + i]!
let val ← elabTermEnsuringType valStx type
mkLambdaFVars xs val
private def collectUsed (headers : Array DefViewElabHeader) (values : Array Expr) (toLift : List LetRecToLift)
: StateRefT CollectFVars.State MetaM Unit := do
headers.forM fun header => header.type.collectFVars
values.forM fun val => val.collectFVars
toLift.forM fun letRecToLift => do
letRecToLift.type.collectFVars
letRecToLift.val.collectFVars
private def removeUnusedVars (vars : Array Expr) (headers : Array DefViewElabHeader) (values : Array Expr) (toLift : List LetRecToLift)
: TermElabM (LocalContext × LocalInstances × Array Expr) := do
let (_, used) ← (collectUsed headers values toLift).run {}
removeUnused vars used
private def withUsed {α} (vars : Array Expr) (headers : Array DefViewElabHeader) (values : Array Expr) (toLift : List LetRecToLift)
(k : Array Expr → TermElabM α) : TermElabM α := do
let (lctx, localInsts, vars) ← removeUnusedVars vars headers values toLift
withLCtx lctx localInsts <| k vars
private def isExample (views : Array DefView) : Bool :=
views.any (·.kind.isExample)
private def isTheorem (views : Array DefView) : Bool :=
views.any (·.kind.isTheorem)
private def instantiateMVarsAtHeader (header : DefViewElabHeader) : TermElabM DefViewElabHeader := do
let type ← instantiateMVars header.type
pure { header with type := type }
private def instantiateMVarsAtLetRecToLift (toLift : LetRecToLift) : TermElabM LetRecToLift := do
let type ← instantiateMVars toLift.type
let val ← instantiateMVars toLift.val
pure { toLift with type, val }
private def typeHasRecFun (type : Expr) (funFVars : Array Expr) (letRecsToLift : List LetRecToLift) : Option FVarId :=
let occ? := type.find? fun e => match e with
| Expr.fvar fvarId => funFVars.contains e || letRecsToLift.any fun toLift => toLift.fvarId == fvarId
| _ => false
match occ? with
| some (Expr.fvar fvarId) => some fvarId
| _ => none
private def getFunName (fvarId : FVarId) (letRecsToLift : List LetRecToLift) : TermElabM Name := do
match (← fvarId.findDecl?) with
| some decl => return decl.userName
| none =>
/- Recall that the FVarId of nested let-recs are not in the current local context. -/
match letRecsToLift.findSome? fun toLift => if toLift.fvarId == fvarId then some toLift.shortDeclName else none with
| none => throwError "unknown function"
| some n => return n
/--
Ensures that the of let-rec definition types do not contain functions being defined.
In principle, this test can be improved. We could perform it after we separate the set of functions is strongly connected components.
However, this extra complication doesn't seem worth it.
-/
private def checkLetRecsToLiftTypes (funVars : Array Expr) (letRecsToLift : List LetRecToLift) : TermElabM Unit :=
letRecsToLift.forM fun toLift =>
match typeHasRecFun toLift.type funVars letRecsToLift with
| none => pure ()
| some fvarId => do
let fnName ← getFunName fvarId letRecsToLift
throwErrorAt toLift.ref "invalid type in 'let rec', it uses '{fnName}' which is being defined simultaneously"
namespace MutualClosure
/-- A mapping from FVarId to Set of FVarIds. -/
abbrev UsedFVarsMap := FVarIdMap FVarIdSet
/--
Create the `UsedFVarsMap` mapping that takes the variable id for the mutually recursive functions being defined to the set of
free variables in its definition.
For `mainFVars`, this is just the set of section variables `sectionVars` used.
For nested let-rec functions, we collect their free variables.
Recall that a `let rec` expressions are encoded as follows in the elaborator.
```lean
let rec
f : A := t,
g : B := s;
body
```
is encoded as
```lean
let f : A := ?m₁;
let g : B := ?m₂;
body
```
where `?m₁` and `?m₂` are synthetic opaque metavariables. That are assigned by this module.
We may have nested `let rec`s.
```lean
let rec f : A :=
let rec g : B := t;
s;
body
```
is encoded as
```lean
let f : A := ?m₁;
body
```
and the body of `f` is stored the field `val` of a `LetRecToLift`. For the example above,
we would have a `LetRecToLift` containing:
```
{
mvarId := m₁,
val := `(let g : B := ?m₂; body)
...
}
```
Note that `g` is not a free variable at `(let g : B := ?m₂; body)`. We recover the fact that
`f` depends on `g` because it contains `m₂`
-/
private def mkInitialUsedFVarsMap [Monad m] [MonadMCtx m] (sectionVars : Array Expr) (mainFVarIds : Array FVarId) (letRecsToLift : Array LetRecToLift)
: m UsedFVarsMap := do
let mut sectionVarSet := {}
for var in sectionVars do
sectionVarSet := sectionVarSet.insert var.fvarId!
let mut usedFVarMap := {}
for mainFVarId in mainFVarIds do
usedFVarMap := usedFVarMap.insert mainFVarId sectionVarSet
for toLift in letRecsToLift do
let state := Lean.collectFVars {} toLift.val
let state := Lean.collectFVars state toLift.type
let mut set := state.fvarSet
/- toLift.val may contain metavariables that are placeholders for nested let-recs. We should collect the fvarId
for the associated let-rec because we need this information to compute the fixpoint later. -/
let mvarIds := (toLift.val.collectMVars {}).result
for mvarId in mvarIds do
match (← letRecsToLift.findSomeM? fun (toLift : LetRecToLift) => return if toLift.mvarId == (← getDelayedMVarRoot mvarId) then some toLift.fvarId else none) with
| some fvarId => set := set.insert fvarId
| none => pure ()
usedFVarMap := usedFVarMap.insert toLift.fvarId set
return usedFVarMap
/-!
The let-recs may invoke each other. Example:
```
let rec
f (x : Nat) := g x + y
g : Nat → Nat
| 0 => 1
| x+1 => f x + z
```
`y` is free variable in `f`, and `z` is a free variable in `g`.
To close `f` and `g`, `y` and `z` must be in the closure of both.
That is, we need to generate the top-level definitions.
```
def f (y z x : Nat) := g y z x + y
def g (y z : Nat) : Nat → Nat
| 0 => 1
| x+1 => f y z x + z
```
-/
namespace FixPoint
structure State where
usedFVarsMap : UsedFVarsMap := {}
modified : Bool := false
abbrev M := ReaderT (Array FVarId) $ StateM State
private def isModified : M Bool := do pure (← get).modified
private def resetModified : M Unit := modify fun s => { s with modified := false }
private def markModified : M Unit := modify fun s => { s with modified := true }
private def getUsedFVarsMap : M UsedFVarsMap := do pure (← get).usedFVarsMap
private def modifyUsedFVars (f : UsedFVarsMap → UsedFVarsMap) : M Unit := modify fun s => { s with usedFVarsMap := f s.usedFVarsMap }
-- merge s₂ into s₁
private def merge (s₁ s₂ : FVarIdSet) : M FVarIdSet :=
s₂.foldM (init := s₁) fun s₁ k => do
if s₁.contains k then
return s₁
else
markModified
return s₁.insert k
private def updateUsedVarsOf (fvarId : FVarId) : M Unit := do
let usedFVarsMap ← getUsedFVarsMap
match usedFVarsMap.find? fvarId with
| none => return ()
| some fvarIds =>
let fvarIdsNew ← fvarIds.foldM (init := fvarIds) fun fvarIdsNew fvarId' => do
if fvarId == fvarId' then
return fvarIdsNew
else
match usedFVarsMap.find? fvarId' with
| none => return fvarIdsNew
/- We are being sloppy here `otherFVarIds` may contain free variables that are
not in the context of the let-rec associated with fvarId.
We filter these out-of-context free variables later. -/
| some otherFVarIds => merge fvarIdsNew otherFVarIds
modifyUsedFVars fun usedFVars => usedFVars.insert fvarId fvarIdsNew
private partial def fixpoint : Unit → M Unit
| _ => do
resetModified
let letRecFVarIds ← read
letRecFVarIds.forM updateUsedVarsOf
if (← isModified) then
fixpoint ()
def run (letRecFVarIds : Array FVarId) (usedFVarsMap : UsedFVarsMap) : UsedFVarsMap :=
let (_, s) := fixpoint () |>.run letRecFVarIds |>.run { usedFVarsMap := usedFVarsMap }
s.usedFVarsMap
end FixPoint
abbrev FreeVarMap := FVarIdMap (Array FVarId)
private def mkFreeVarMap [Monad m] [MonadMCtx m]
(sectionVars : Array Expr) (mainFVarIds : Array FVarId)
(recFVarIds : Array FVarId) (letRecsToLift : Array LetRecToLift) : m FreeVarMap := do
let usedFVarsMap ← mkInitialUsedFVarsMap sectionVars mainFVarIds letRecsToLift
let letRecFVarIds := letRecsToLift.map fun toLift => toLift.fvarId
let usedFVarsMap := FixPoint.run letRecFVarIds usedFVarsMap
let mut freeVarMap := {}
for toLift in letRecsToLift do
let lctx := toLift.lctx
let fvarIdsSet := usedFVarsMap.find? toLift.fvarId |>.get!
let fvarIds := fvarIdsSet.fold (init := #[]) fun fvarIds fvarId =>
if lctx.contains fvarId && !recFVarIds.contains fvarId then
fvarIds.push fvarId
else
fvarIds
freeVarMap := freeVarMap.insert toLift.fvarId fvarIds
return freeVarMap
structure ClosureState where
newLocalDecls : Array LocalDecl := #[]
localDecls : Array LocalDecl := #[]
newLetDecls : Array LocalDecl := #[]
exprArgs : Array Expr := #[]
private def pickMaxFVar? (lctx : LocalContext) (fvarIds : Array FVarId) : Option FVarId :=
fvarIds.getMax? fun fvarId₁ fvarId₂ => (lctx.get! fvarId₁).index < (lctx.get! fvarId₂).index
private def preprocess (e : Expr) : TermElabM Expr := do
let e ← instantiateMVars e
-- which let-decls are dependent. We say a let-decl is dependent if its lambda abstraction is type incorrect.
Meta.check e
pure e
/-- Push free variables in `s` to `toProcess` if they are not already there. -/
private def pushNewVars (toProcess : Array FVarId) (s : CollectFVars.State) : Array FVarId :=
s.fvarSet.fold (init := toProcess) fun toProcess fvarId =>
if toProcess.contains fvarId then toProcess else toProcess.push fvarId
private def pushLocalDecl (toProcess : Array FVarId) (fvarId : FVarId) (userName : Name) (type : Expr) (bi := BinderInfo.default)
: StateRefT ClosureState TermElabM (Array FVarId) := do
let type ← preprocess type
modify fun s => { s with
newLocalDecls := s.newLocalDecls.push <| LocalDecl.cdecl default fvarId userName type bi
exprArgs := s.exprArgs.push (mkFVar fvarId)
}
return pushNewVars toProcess (collectFVars {} type)
private partial def mkClosureForAux (toProcess : Array FVarId) : StateRefT ClosureState TermElabM Unit := do
let lctx ← getLCtx
match pickMaxFVar? lctx toProcess with
| none => return ()
| some fvarId =>
trace[Elab.definition.mkClosure] "toProcess: {toProcess.map mkFVar}, maxVar: {mkFVar fvarId}"
let toProcess := toProcess.erase fvarId
let localDecl ← fvarId.getDecl
match localDecl with
| .cdecl _ _ userName type bi =>
let toProcess ← pushLocalDecl toProcess fvarId userName type bi
mkClosureForAux toProcess
| .ldecl _ _ userName type val _ =>
let zetaFVarIds ← getZetaFVarIds
if !zetaFVarIds.contains fvarId then
/- Non-dependent let-decl. See comment at src/Lean/Meta/Closure.lean -/
let toProcess ← pushLocalDecl toProcess fvarId userName type
mkClosureForAux toProcess
else
/- Dependent let-decl. -/
let type ← preprocess type
let val ← preprocess val
modify fun s => { s with
newLetDecls := s.newLetDecls.push <| LocalDecl.ldecl default fvarId userName type val false,
/- We don't want to interleave let and lambda declarations in our closure. So, we expand any occurrences of fvarId
at `newLocalDecls` and `localDecls` -/
newLocalDecls := s.newLocalDecls.map (·.replaceFVarId fvarId val)
localDecls := s.localDecls.map (·.replaceFVarId fvarId val)
}
mkClosureForAux (pushNewVars toProcess (collectFVars (collectFVars {} type) val))
private partial def mkClosureFor (freeVars : Array FVarId) (localDecls : Array LocalDecl) : TermElabM ClosureState := do
let (_, s) ← mkClosureForAux freeVars |>.run { localDecls := localDecls }
return { s with
newLocalDecls := s.newLocalDecls.reverse
newLetDecls := s.newLetDecls.reverse
exprArgs := s.exprArgs.reverse
}
structure LetRecClosure where
ref : Syntax
localDecls : Array LocalDecl
/-- Expression used to replace occurrences of the let-rec `FVarId`. -/
closed : Expr
toLift : LetRecToLift
private def mkLetRecClosureFor (toLift : LetRecToLift) (freeVars : Array FVarId) : TermElabM LetRecClosure := do
let lctx := toLift.lctx
withLCtx lctx toLift.localInstances do
lambdaTelescope toLift.val fun xs val => do
/-
Recall that `toLift.type` and `toLift.value` may have different binder annotations.
See issue #1377 for an example.
-/
let userNameAndBinderInfos ← forallBoundedTelescope toLift.type xs.size fun xs _ =>
xs.mapM fun x => do
let localDecl ← x.fvarId!.getDecl
return (localDecl.userName, localDecl.binderInfo)
/- Auxiliary map for preserving binder user-facind names and `BinderInfo` for types. -/
let mut userNameBinderInfoMap : FVarIdMap (Name × BinderInfo) := {}
for x in xs, (userName, bi) in userNameAndBinderInfos do
userNameBinderInfoMap := userNameBinderInfoMap.insert x.fvarId! (userName, bi)
let type ← instantiateForall toLift.type xs
let lctx ← getLCtx
let s ← mkClosureFor freeVars <| xs.map fun x => lctx.get! x.fvarId!
/- Apply original type binder info and user-facing names to local declarations. -/
let typeLocalDecls := s.localDecls.map fun localDecl =>
if let some (userName, bi) := userNameBinderInfoMap.find? localDecl.fvarId then
localDecl.setBinderInfo bi |>.setUserName userName
else
localDecl
let type := Closure.mkForall typeLocalDecls <| Closure.mkForall s.newLetDecls type
let val := Closure.mkLambda s.localDecls <| Closure.mkLambda s.newLetDecls val
let c := mkAppN (Lean.mkConst toLift.declName) s.exprArgs
toLift.mvarId.assign c
return {
ref := toLift.ref
localDecls := s.newLocalDecls
closed := c
toLift := { toLift with val, type }
}
private def mkLetRecClosures (sectionVars : Array Expr) (mainFVarIds : Array FVarId) (recFVarIds : Array FVarId) (letRecsToLift : Array LetRecToLift) : TermElabM (List LetRecClosure) := do
-- Compute the set of free variables (excluding `recFVarIds`) for each let-rec.
let mut letRecsToLift := letRecsToLift
let mut freeVarMap ← mkFreeVarMap sectionVars mainFVarIds recFVarIds letRecsToLift
let mut result := #[]
for i in [:letRecsToLift.size] do
if letRecsToLift[i]!.val.hasExprMVar then
-- This can happen when this particular let-rec has nested let-rec that have been resolved in previous iterations.
-- This code relies on the fact that nested let-recs occur before the outer most let-recs at `letRecsToLift`.
-- Unresolved nested let-recs appear as metavariables before they are resolved. See `assignExprMVar` at `mkLetRecClosureFor`
let valNew ← instantiateMVars letRecsToLift[i]!.val
letRecsToLift := letRecsToLift.modify i fun t => { t with val := valNew }
-- We have to recompute the `freeVarMap` in this case. This overhead should not be an issue in practice.
freeVarMap ← mkFreeVarMap sectionVars mainFVarIds recFVarIds letRecsToLift
let toLift := letRecsToLift[i]!
result := result.push (← mkLetRecClosureFor toLift (freeVarMap.find? toLift.fvarId).get!)
return result.toList
/-- Mapping from FVarId of mutually recursive functions being defined to "closure" expression. -/
abbrev Replacement := FVarIdMap Expr
def insertReplacementForMainFns (r : Replacement) (sectionVars : Array Expr) (mainHeaders : Array DefViewElabHeader) (mainFVars : Array Expr) : Replacement :=
mainFVars.size.fold (init := r) fun i r =>
r.insert mainFVars[i]!.fvarId! (mkAppN (Lean.mkConst mainHeaders[i]!.declName) sectionVars)
def insertReplacementForLetRecs (r : Replacement) (letRecClosures : List LetRecClosure) : Replacement :=
letRecClosures.foldl (init := r) fun r c =>
r.insert c.toLift.fvarId c.closed
def Replacement.apply (r : Replacement) (e : Expr) : Expr :=
e.replace fun e => match e with
| .fvar fvarId => match r.find? fvarId with
| some c => some c
| _ => none
| _ => none
def pushMain (preDefs : Array PreDefinition) (sectionVars : Array Expr) (mainHeaders : Array DefViewElabHeader) (mainVals : Array Expr)
: TermElabM (Array PreDefinition) :=
mainHeaders.size.foldM (init := preDefs) fun i preDefs => do
let header := mainHeaders[i]!
let value ← mkLambdaFVars sectionVars mainVals[i]!
let type ← mkForallFVars sectionVars header.type
return preDefs.push {
ref := getDeclarationSelectionRef header.ref
kind := header.kind
declName := header.declName
levelParams := [], -- we set it later
modifiers := header.modifiers
type, value
}
def pushLetRecs (preDefs : Array PreDefinition) (letRecClosures : List LetRecClosure) (kind : DefKind) (modifiers : Modifiers) : Array PreDefinition :=
letRecClosures.foldl (init := preDefs) fun preDefs c =>
let type := Closure.mkForall c.localDecls c.toLift.type
let value := Closure.mkLambda c.localDecls c.toLift.val
preDefs.push {
ref := c.ref
declName := c.toLift.declName
levelParams := [] -- we set it later
modifiers := { modifiers with attrs := c.toLift.attrs }
kind, type, value
}
def getKindForLetRecs (mainHeaders : Array DefViewElabHeader) : DefKind :=
if mainHeaders.any fun h => h.kind.isTheorem then DefKind.«theorem»
else DefKind.«def»
def getModifiersForLetRecs (mainHeaders : Array DefViewElabHeader) : Modifiers := {
isNoncomputable := mainHeaders.any fun h => h.modifiers.isNoncomputable
recKind := if mainHeaders.any fun h => h.modifiers.isPartial then RecKind.partial else RecKind.default
isUnsafe := mainHeaders.any fun h => h.modifiers.isUnsafe
}
/--
- `sectionVars`: The section variables used in the `mutual` block.
- `mainHeaders`: The elaborated header of the top-level definitions being defined by the mutual block.
- `mainFVars`: The auxiliary variables used to represent the top-level definitions being defined by the mutual block.
- `mainVals`: The elaborated value for the top-level definitions
- `letRecsToLift`: The let-rec's definitions that need to be lifted
-/
def main (sectionVars : Array Expr) (mainHeaders : Array DefViewElabHeader) (mainFVars : Array Expr) (mainVals : Array Expr) (letRecsToLift : List LetRecToLift)
: TermElabM (Array PreDefinition) := do
-- Store in recFVarIds the fvarId of every function being defined by the mutual block.
let letRecsToLift := letRecsToLift.toArray
let mainFVarIds := mainFVars.map Expr.fvarId!
let recFVarIds := (letRecsToLift.map fun toLift => toLift.fvarId) ++ mainFVarIds
resetZetaFVarIds
withTrackingZeta do
-- By checking `toLift.type` and `toLift.val` we populate `zetaFVarIds`. See comments at `src/Lean/Meta/Closure.lean`.
let letRecsToLift ← letRecsToLift.mapM fun toLift => withLCtx toLift.lctx toLift.localInstances do
Meta.check toLift.type
Meta.check toLift.val
return { toLift with val := (← instantiateMVars toLift.val), type := (← instantiateMVars toLift.type) }
let letRecClosures ← mkLetRecClosures sectionVars mainFVarIds recFVarIds letRecsToLift
-- mkLetRecClosures assign metavariables that were placeholders for the lifted declarations.
let mainVals ← mainVals.mapM (instantiateMVars ·)
let mainHeaders ← mainHeaders.mapM instantiateMVarsAtHeader
let letRecClosures ← letRecClosures.mapM fun closure => do pure { closure with toLift := (← instantiateMVarsAtLetRecToLift closure.toLift) }
-- Replace fvarIds for functions being defined with closed terms
let r := insertReplacementForMainFns {} sectionVars mainHeaders mainFVars
let r := insertReplacementForLetRecs r letRecClosures
let mainVals := mainVals.map r.apply
let mainHeaders := mainHeaders.map fun h => { h with type := r.apply h.type }
let letRecClosures := letRecClosures.map fun c => { c with toLift := { c.toLift with type := r.apply c.toLift.type, val := r.apply c.toLift.val } }
let letRecKind := getKindForLetRecs mainHeaders
let letRecMods := getModifiersForLetRecs mainHeaders
pushMain (pushLetRecs #[] letRecClosures letRecKind letRecMods) sectionVars mainHeaders mainVals
end MutualClosure
private def getAllUserLevelNames (headers : Array DefViewElabHeader) : List Name :=
if h : 0 < headers.size then
-- Recall that all top-level functions must have the same levels. See `check` method above
(headers.get ⟨0, h⟩).levelNames
else
[]
/-- Eagerly convert universe metavariables occurring in theorem headers to universe parameters. -/
private def levelMVarToParamHeaders (views : Array DefView) (headers : Array DefViewElabHeader) : TermElabM (Array DefViewElabHeader) := do
let rec process : StateRefT Nat TermElabM (Array DefViewElabHeader) := do
let mut newHeaders := #[]
for view in views, header in headers do
if view.kind.isTheorem then
newHeaders ←
withLevelNames header.levelNames do
return newHeaders.push { header with type := (← levelMVarToParam header.type), levelNames := (← getLevelNames) }
else
newHeaders := newHeaders.push header
return newHeaders
let newHeaders ← (process).run' 1
newHeaders.mapM fun header => return { header with type := (← instantiateMVars header.type) }
/-- Remove auxiliary match discriminant let-declarations. -/
def eraseAuxDiscr (e : Expr) : CoreM Expr := do
Core.transform e fun e => do
if let .letE n _ v b .. := e then
if isAuxDiscrName n then
return .visit (b.instantiate1 v)
return .continue
partial def checkForHiddenUnivLevels (allUserLevelNames : List Name) (preDefs : Array PreDefinition) : TermElabM Unit :=
unless (← MonadLog.hasErrors) do
-- We do not report this kind of error if the declaration already contains errors
let mut sTypes : CollectLevelParams.State := {}
let mut sValues : CollectLevelParams.State := {}
for preDef in preDefs do
sTypes := collectLevelParams sTypes preDef.type
sValues := collectLevelParams sValues preDef.value
if sValues.params.all fun u => sTypes.params.contains u || allUserLevelNames.contains u then
-- If all universe level occurring in values also occur in types or explicitly provided universes, then everything is fine
-- and we just return
return ()
let checkPreDef (preDef : PreDefinition) : TermElabM Unit :=
-- Otherwise, we try to produce an error message containing the expression with the offending universe
let rec visitLevel (u : Level) : ReaderT Expr TermElabM Unit := do
match u with
| .succ u => visitLevel u
| .imax u v | .max u v => visitLevel u; visitLevel v
| .param n =>
unless sTypes.visitedLevel.contains u || allUserLevelNames.contains n do
let parent ← withOptions (fun o => pp.universes.set o true) do addMessageContext m!"{indentExpr (← read)}"
let body ← withOptions (fun o => pp.letVarTypes.setIfNotSet (pp.funBinderTypes.setIfNotSet o true) true) do addMessageContext m!"{indentExpr preDef.value}"
throwError "invalid occurrence of universe level '{u}' at '{preDef.declName}', it does not occur at the declaration type, nor it is explicit universe level provided by the user, occurring at expression{parent}\nat declaration body{body}"
| _ => pure ()
let rec visit (e : Expr) : ReaderT Expr (MonadCacheT ExprStructEq Unit TermElabM) Unit := do
checkCache { val := e : ExprStructEq } fun _ => do
match e with
| .forallE n d b c | .lam n d b c => visit d e; withLocalDecl n c d fun x => visit (b.instantiate1 x) e
| .letE n t v b _ => visit t e; visit v e; withLetDecl n t v fun x => visit (b.instantiate1 x) e
| .app .. => e.withApp fun f args => do visit f e; args.forM fun arg => visit arg e
| .mdata _ b => visit b e
| .proj _ _ b => visit b e
| .sort u => visitLevel u (← read)
| .const _ us => us.forM (visitLevel · (← read))
| _ => pure ()
visit preDef.value preDef.value |>.run {}
for preDef in preDefs do
checkPreDef preDef
def elabMutualDef (vars : Array Expr) (views : Array DefView) (hints : TerminationHints) : TermElabM Unit :=
if isExample views then
withoutModifyingEnv go
else
go
where
go := do
let scopeLevelNames ← getLevelNames
let headers ← elabHeaders views
let headers ← levelMVarToParamHeaders views headers
let allUserLevelNames := getAllUserLevelNames headers
withFunLocalDecls headers fun funFVars => do
for view in views, funFVar in funFVars do
addLocalVarInfo view.declId funFVar
let values ←
try
let values ← elabFunValues headers
Term.synthesizeSyntheticMVarsNoPostponing
values.mapM (instantiateMVars ·)
catch ex =>
logException ex
headers.mapM fun header => mkSorry header.type (synthetic := true)
let headers ← headers.mapM instantiateMVarsAtHeader
let letRecsToLift ← getLetRecsToLift
let letRecsToLift ← letRecsToLift.mapM instantiateMVarsAtLetRecToLift
checkLetRecsToLiftTypes funFVars letRecsToLift
withUsed vars headers values letRecsToLift fun vars => do
let preDefs ← MutualClosure.main vars headers funFVars values letRecsToLift
for preDef in preDefs do
trace[Elab.definition] "{preDef.declName} : {preDef.type} :=\n{preDef.value}"
let preDefs ← withLevelNames allUserLevelNames <| levelMVarToParamPreDecls preDefs
let preDefs ← instantiateMVarsAtPreDecls preDefs
let preDefs ← fixLevelParams preDefs scopeLevelNames allUserLevelNames
let preDefs ← preDefs.mapM fun preDef =>
if preDef.kind.isTheorem || preDef.kind.isExample then
return preDef
else
return { preDef with value := (← eraseAuxDiscr preDef.value) }
for preDef in preDefs do
trace[Elab.definition] "after eraseAuxDiscr, {preDef.declName} : {preDef.type} :=\n{preDef.value}"
checkForHiddenUnivLevels allUserLevelNames preDefs
addPreDefinitions preDefs hints
processDeriving headers
processDeriving (headers : Array DefViewElabHeader) := do
for header in headers, view in views do
if let some classNamesStx := view.deriving? then
for classNameStx in classNamesStx do
let className ← resolveGlobalConstNoOverload classNameStx
withRef classNameStx do
unless (← processDefDeriving className header.declName) do
throwError "failed to synthesize instance '{className}' for '{header.declName}'"
end Term
namespace Command
def elabMutualDef (ds : Array Syntax) (hints : TerminationHints) : CommandElabM Unit := do
let views ← ds.mapM fun d => do
let modifiers ← elabModifiers d[0]
if ds.size > 1 && modifiers.isNonrec then
throwErrorAt d "invalid use of 'nonrec' modifier in 'mutual' block"
mkDefView modifiers d[1]
runTermElabM fun vars => Term.elabMutualDef vars views hints
end Command
end Lean.Elab
|
b9f286dc4a12265b6644c543304a154faf2de216 | da2388aff16df48b4a928b575bcec678b8e7f2cf | /src/asymptotics2.lean | 73baa6793b66926efa26b4e5bb46d80c4543582e | [] | no_license | ADedecker/series | 1d0d282d4333c7599f07cc705a35cf5edfb87a3b | 12fbcdd16b3cdeda4476165a4645fcccc0fb3234 | refs/heads/master | 1,674,828,739,868 | 1,607,618,619,000 | 1,607,618,619,000 | 304,379,894 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,060 | lean | import analysis.calculus.deriv
import algebra.group.pi
import analysis.normed_space.ordered
namespace asymptotics
open filter
open_locale topological_space
lemma is_o.tendsto_of_tendsto_const {α E 𝕜 : Type*} [normed_group E] [normed_field 𝕜] {u : α → E}
{v : α → 𝕜} {l : filter α} {y : 𝕜} (huv : is_o u v l) (hv : tendsto v l (𝓝 y)) :
tendsto u l (𝓝 0) :=
begin
suffices h : is_o u (λ x, (1 : 𝕜)) l,
{ rwa is_o_one_iff at h },
exact huv.trans_is_O (is_O_one_of_tendsto 𝕜 hv),
end
section normed_linear_ordered_group
variables {α β : Type*} [normed_linear_ordered_group β] {u v w : α → β} {l : filter α} {c : ℝ}
lemma is_O.trans_tendsto_norm_at_top (huv : is_O u v l)
(hu : tendsto (λ x, ∥u x∥) l at_top) :
tendsto (λ x, ∥v x∥) l at_top :=
begin
rcases huv.exists_pos with ⟨c, hc, hcuv⟩,
rw is_O_with at hcuv,
convert tendsto_at_top_div hc (tendsto_at_top_mono' l hcuv hu),
ext x,
rw mul_div_cancel_left _ hc.ne.symm,
end
end normed_linear_ordered_group
end asymptotics |
6df13e300320aa3ff45c312ec80881f5ccb06ef1 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/topology/algebra/nonarchimedean/basic.lean | 8ba60f2a088a79c99414276d34848f8c293466fa | [
"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 | 5,829 | lean | /-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import group_theory.subgroup.basic
import topology.algebra.open_subgroup
import topology.algebra.ring
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `nonarchimedean_add_group`: nonarchimedean additive group.
- `nonarchimedean_group`: nonarchimedean multiplicative group.
- `nonarchimedean_ring`: nonarchimedean ring.
-/
open_locale pointwise
/-- An topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class nonarchimedean_add_group (G : Type*)
[add_group G] [topological_space G] extends topological_add_group G : Prop :=
(is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : open_add_subgroup G, (V : set G) ⊆ U)
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class nonarchimedean_group (G : Type*)
[group G] [topological_space G] extends topological_group G : Prop :=
(is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : open_subgroup G, (V : set G) ⊆ U)
/-- An topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class nonarchimedean_ring (R : Type*)
[ring R] [topological_space R] extends topological_ring R : Prop :=
(is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : open_add_subgroup R, (V : set R) ⊆ U)
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
@[priority 100] -- see Note [lower instance priority]
instance nonarchimedean_ring.to_nonarchimedean_add_group
(R : Type*) [ring R] [topological_space R] [t: nonarchimedean_ring R] :
nonarchimedean_add_group R := {..t}
namespace nonarchimedean_group
variables {G : Type*} [group G] [topological_space G] [nonarchimedean_group G]
variables {H : Type*} [group H] [topological_space H] [topological_group H]
variables {K : Type*} [group K] [topological_space K] [nonarchimedean_group K]
/-- If a topological group embeds into a nonarchimedean group, then it
is nonarchimedean. -/
@[to_additive nonarchimedean_add_group.nonarchimedean_of_emb]
lemma nonarchimedean_of_emb (f : G →* H) (emb : open_embedding f) : nonarchimedean_group H :=
{ is_nonarchimedean := λ U hU, have h₁ : (f ⁻¹' U) ∈ nhds (1 : G), from
by {apply emb.continuous.tendsto, rwa f.map_one},
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁ in
⟨{is_open' := emb.is_open_map _ V.is_open, ..subgroup.map f V},
set.image_subset_iff.2 hV⟩ }
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive nonarchimedean_add_group.prod_subset]
lemma prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : open_subgroup G) (W : open_subgroup K), (V : set G) ×ˢ (W : set K) ⊆ U :=
begin
erw [nhds_prod_eq, filter.mem_prod_iff] at hU,
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩,
cases is_nonarchimedean _ hU₁ with V hV,
cases is_nonarchimedean _ hU₂ with W hW,
use V, use W,
rw set.prod_subset_iff,
intros x hX y hY,
exact set.subset.trans (set.prod_mono hV hW) h (set.mem_sep hX hY),
end
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive nonarchimedean_add_group.prod_self_subset]
lemma prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ (V : open_subgroup G), (V : set G) ×ˢ (V : set G) ⊆ U :=
let ⟨V, W, h⟩ := prod_subset hU in
⟨V ⊓ W, by {refine set.subset.trans (set.prod_mono _ _) ‹_›; simp}⟩
/-- The cartesian product of two nonarchimedean groups is nonarchimedean. -/
@[to_additive]
instance : nonarchimedean_group (G × K) :=
{ is_nonarchimedean := λ U hU, let ⟨V, W, h⟩ := prod_subset hU in ⟨V.prod W, ‹_›⟩ }
end nonarchimedean_group
namespace nonarchimedean_ring
open nonarchimedean_ring
open nonarchimedean_add_group
variables {R S : Type*}
variables [ring R] [topological_space R] [nonarchimedean_ring R]
variables [ring S] [topological_space S] [nonarchimedean_ring S]
/-- The cartesian product of two nonarchimedean rings is nonarchimedean. -/
instance : nonarchimedean_ring (R × S) :=
{ is_nonarchimedean := nonarchimedean_add_group.is_nonarchimedean }
/-- Given an open subgroup `U` and an element `r` of a nonarchimedean ring, there is an open
subgroup `V` such that `r • V` is contained in `U`. -/
lemma left_mul_subset (U : open_add_subgroup R) (r : R) :
∃ V : open_add_subgroup R, r • (V : set R) ⊆ U :=
⟨U.comap (add_monoid_hom.mul_left r) (continuous_mul_left r),
(U : set R).image_preimage_subset _⟩
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
lemma mul_subset (U : open_add_subgroup R) :
∃ V : open_add_subgroup R, (V : set R) * V ⊆ U :=
let ⟨V, H⟩ := prod_self_subset (is_open.mem_nhds (is_open.preimage continuous_mul U.is_open)
begin
simpa only [set.mem_preimage, open_add_subgroup.mem_coe, prod.snd_zero, mul_zero]
using U.zero_mem,
end) in
begin
use V,
rintros v ⟨a, b, ha, hb, hv⟩,
have hy := H (set.mk_mem_prod ha hb),
simp only [set.mem_preimage, open_add_subgroup.mem_coe] at hy,
rwa hv at hy
end
end nonarchimedean_ring
|
64e8d4c472324b2dd0126c1bc8193629d7f1a4a2 | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/linear_algebra/matrix.lean | 22668e65a8328e750a5f2d40ae0f8eeb670ee094 | [
"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 | 34,393 | lean | /-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Johannes Hölzl, Patrick Massot, Casper Putz
-/
import linear_algebra.finite_dimensional
import linear_algebra.nonsingular_inverse
import linear_algebra.multilinear
/-!
# Linear maps and matrices
This file defines the maps to send matrices to a linear map,
and to send linear maps between modules with a finite bases
to matrices. This defines a linear equivalence between linear maps
between finite-dimensional vector spaces and matrices indexed by
the respective bases.
It also defines the trace of an endomorphism, and the determinant of a family of vectors with
respect to some basis.
Some results are proved about the linear map corresponding to a
diagonal matrix (`range`, `ker` and `rank`).
## Main definitions
In the list below, and in all this file, `R` is a commutative ring (semiring
is sometimes enough), `M` and its variations are `R`-modules, `ι`, `κ`, `n` and `m` are finite
types used for indexing.
* `to_lin`: the `R`-linear map from `matrix m n R` to `R`-linear maps from `n → R` to `m → R`
* `to_matrix`: the map in the other direction
* `linear_equiv_matrix`: given bases `v₁ : ι → M₁` and `v₂ : κ → M₂`, the `R`-linear equivalence
from `M₁ →ₗ[R] M₂` to `matrix κ ι R`
* `linear_equiv_matrix'`: the same thing but with `M₁ = n → R` and `M₂ = m → R`, using their
standard bases
* `alg_equiv_matrix`: given a basis indexed by `n`, the `R`-algebra equivalence between
`R`-endomorphisms of `M` and `matrix n n R`
* `matrix.trace`: the trace of a square matrix
* `linear_map.trace`: the trace of an endomorphism
* `is_basis.to_matrix`: the matrix whose columns are a given family of vectors in a given basis
* `is_basis.to_matrix_equiv`: given a basis, the linear equivalence between families of vectors
and matrices arising from `is_basis.to_matrix`
* `is_basis.det`: the determinant of a family of vectors with respect to a basis, as a multilinear
map
## Tags
linear_map, matrix, linear_equiv, diagonal, det, trace
-/
noncomputable theory
open set submodule
open_locale big_operators
universes u v w
variables {l m n : Type*} [fintype l] [fintype m] [fintype n]
namespace matrix
variables {R : Type v} [comm_ring R]
instance [decidable_eq m] [decidable_eq n] (R) [fintype R] : fintype (matrix m n R) :=
by unfold matrix; apply_instance
/-- Evaluation of matrices gives a linear map from `matrix m n R` to
linear maps `(n → R) →ₗ[R] (m → R)`. -/
def eval : (matrix m n R) →ₗ[R] ((n → R) →ₗ[R] (m → R)) :=
begin
refine linear_map.mk₂ R mul_vec _ _ _ _,
{ assume M N v, funext x,
change ∑ y : n, (M x y + N x y) * v y = _,
simp only [_root_.add_mul, finset.sum_add_distrib],
refl },
{ assume c M v, funext x,
change ∑ y : n, (c * M x y) * v y = _,
simp only [_root_.mul_assoc, finset.mul_sum.symm],
refl },
{ assume M v w, funext x,
change ∑ y : n, M x y * (v y + w y) = _,
simp [_root_.mul_add, finset.sum_add_distrib],
refl },
{ assume c M v, funext x,
change ∑ y : n, M x y * (c * v y) = _,
rw [show (λy:n, M x y * (c * v y)) = (λy:n, c * (M x y * v y)), { funext n, ac_refl },
← finset.mul_sum],
refl }
end
/-- Evaluation of matrices gives a map from `matrix m n R` to
linear maps `(n → R) →ₗ[R] (m → R)`. -/
def to_lin : matrix m n R → (n → R) →ₗ[R] (m → R) := eval.to_fun
theorem to_lin_of_equiv {p q : Type*} [fintype p] [fintype q] (e₁ : m ≃ p) (e₂ : n ≃ q)
(f : matrix p q R) : to_lin (λ i j, f (e₁ i) (e₂ j) : matrix m n R) =
linear_equiv.arrow_congr
(linear_map.fun_congr_left R R e₂)
(linear_map.fun_congr_left R R e₁)
(to_lin f) :=
linear_map.ext $ λ v, funext $ λ i,
calc ∑ j : n, f (e₁ i) (e₂ j) * v j
= ∑ j : n, f (e₁ i) (e₂ j) * v (e₂.symm (e₂ j)) : by simp_rw e₂.symm_apply_apply
... = ∑ k : q, f (e₁ i) k * v (e₂.symm k) : finset.sum_equiv e₂ (λ k, f (e₁ i) k * v (e₂.symm k))
lemma to_lin_add (M N : matrix m n R) : (M + N).to_lin = M.to_lin + N.to_lin :=
matrix.eval.map_add M N
@[simp] lemma to_lin_zero : (0 : matrix m n R).to_lin = 0 :=
matrix.eval.map_zero
@[simp] lemma to_lin_neg (M : matrix m n R) : (-M).to_lin = -M.to_lin :=
@linear_map.map_neg _ _ ((n → R) →ₗ[R] m → R) _ _ _ _ _ matrix.eval M
instance to_lin.is_add_monoid_hom :
@is_add_monoid_hom (matrix m n R) ((n → R) →ₗ[R] (m → R)) _ _ to_lin :=
{ map_zero := to_lin_zero, map_add := to_lin_add }
@[simp] lemma to_lin_apply (M : matrix m n R) (v : n → R) :
(M.to_lin : (n → R) → (m → R)) v = mul_vec M v := rfl
lemma mul_to_lin (M : matrix m n R) (N : matrix n l R) :
(M.mul N).to_lin = M.to_lin.comp N.to_lin :=
by { ext, simp }
@[simp] lemma to_lin_one [decidable_eq n] : (1 : matrix n n R).to_lin = linear_map.id :=
by { ext, simp }
end matrix
namespace linear_map
variables {R : Type v} [comm_ring R]
/-- The linear map from linear maps `(n → R) →ₗ[R] (m → R)` to `matrix m n R`. -/
def to_matrixₗ [decidable_eq n] : ((n → R) →ₗ[R] (m → R)) →ₗ[R] matrix m n R :=
begin
refine linear_map.mk (λ f i j, f (λ n, ite (j = n) 1 0) i) _ _,
{ assume f g, simp only [add_apply], refl },
{ assume f g, simp only [smul_apply], refl }
end
/-- The map from linear maps `(n → R) →ₗ[R] (m → R)` to `matrix m n R`. -/
def to_matrix [decidable_eq n] : ((n → R) →ₗ[R] (m → R)) → matrix m n R := to_matrixₗ.to_fun
@[simp] lemma to_matrix_id [decidable_eq n] :
(@linear_map.id _ (n → R) _ _ _).to_matrix = 1 :=
by { ext, simp [to_matrix, to_matrixₗ, matrix.one_apply, eq_comm] }
theorem to_matrix_of_equiv {p q : Type*} [decidable_eq n] [decidable_eq q] [fintype p] [fintype q]
(e₁ : m ≃ p) (e₂ : n ≃ q) (f : (q → R) →ₗ[R] (p → R)) (i j) :
to_matrix f (e₁ i) (e₂ j) = to_matrix (linear_equiv.arrow_congr
(linear_map.fun_congr_left R R e₂)
(linear_map.fun_congr_left R R e₁) f) i j :=
show f (λ k : q, ite (e₂ j = k) 1 0) (e₁ i) = f (λ k : q, ite (j = e₂.symm k) 1 0) (e₁ i),
by { rcongr, rw equiv.eq_symm_apply }
end linear_map
section linear_equiv_matrix
variables {R : Type v} [comm_ring R] [decidable_eq n]
open finsupp matrix linear_map
/-- `to_lin` is the left inverse of `to_matrix`. -/
lemma to_matrix_to_lin {f : (n → R) →ₗ[R] (m → R)} :
to_lin (to_matrix f) = f :=
begin
ext : 1,
-- Show that the two sides are equal by showing that they are equal on a basis
convert linear_eq_on (set.range _) _ (is_basis.mem_span (@pi.is_basis_fun R n _ _) _),
assume e he,
rw [@std_basis_eq_single R _ _ _ 1] at he,
cases (set.mem_range.mp he) with i h,
ext j,
change ∑ k, (f (λ l, ite (k = l) 1 0)) j * (e k) = _,
rw [←h],
conv_lhs { congr, skip, funext,
rw [mul_comm, ←smul_eq_mul, ←pi.smul_apply, ←linear_map.map_smul],
rw [show _ = ite (i = k) (1:R) 0, by convert single_apply],
rw [show f (ite (i = k) (1:R) 0 • (λ l, ite (k = l) 1 0)) = ite (i = k) (f _) 0,
{ split_ifs, { rw [one_smul] }, { rw [zero_smul], exact linear_map.map_zero f } }] },
convert finset.sum_eq_single i _ _,
{ rw [if_pos rfl], convert rfl, ext, congr },
{ assume _ _ hbi, rw [if_neg $ ne.symm hbi], refl },
{ assume hi, exact false.elim (hi $ finset.mem_univ i) }
end
/-- `to_lin` is the right inverse of `to_matrix`. -/
lemma to_lin_to_matrix {M : matrix m n R} : to_matrix (to_lin M) = M :=
begin
ext,
change ∑ y, M i y * ite (j = y) 1 0 = M i j,
have h1 : (λ y, M i y * ite (j = y) 1 0) = (λ y, ite (j = y) (M i y) 0),
{ ext, split_ifs, exact mul_one _, exact mul_zero _ },
have h2 : ∑ y, ite (j = y) (M i y) 0 = ∑ y in {j}, ite (j = y) (M i y) 0,
{ refine (finset.sum_subset _ _).symm,
{ intros _ H, rwa finset.mem_singleton.1 H, exact finset.mem_univ _ },
{ exact λ _ _ H, if_neg (mt (finset.mem_singleton.2 ∘ eq.symm) H) } },
rw [h1, h2, finset.sum_singleton],
exact if_pos rfl
end
/-- Linear maps `(n → R) →ₗ[R] (m → R)` are linearly equivalent to `matrix m n R`. -/
def linear_equiv_matrix' : ((n → R) →ₗ[R] (m → R)) ≃ₗ[R] matrix m n R :=
{ to_fun := to_matrix,
inv_fun := to_lin,
right_inv := λ _, to_lin_to_matrix,
left_inv := λ _, to_matrix_to_lin,
map_add' := to_matrixₗ.map_add,
map_smul' := to_matrixₗ.map_smul }
@[simp] lemma linear_equiv_matrix'_apply (f : (n → R) →ₗ[R] (m → R)) :
linear_equiv_matrix' f = to_matrix f := rfl
variables {ι κ M₁ M₂ : Type*}
[add_comm_group M₁] [module R M₁]
[add_comm_group M₂] [module R M₂]
[fintype ι] [decidable_eq ι] [fintype κ]
{v₁ : ι → M₁} {v₂ : κ → M₂}
/-- Given bases of two modules `M₁` and `M₂` over a commutative ring `R`, we get a linear
equivalence between linear maps `M₁ →ₗ M₂` and matrices over `R` indexed by the bases. -/
def linear_equiv_matrix (hv₁ : is_basis R v₁) (hv₂ : is_basis R v₂) :
(M₁ →ₗ[R] M₂) ≃ₗ[R] matrix κ ι R :=
linear_equiv.trans (linear_equiv.arrow_congr hv₁.equiv_fun hv₂.equiv_fun) linear_equiv_matrix'
variables (hv₁ : is_basis R v₁) (hv₂ : is_basis R v₂)
lemma linear_equiv_matrix_apply (f : M₁ →ₗ[R] M₂) (i : κ) (j : ι) :
linear_equiv_matrix hv₁ hv₂ f i j = hv₂.equiv_fun (f (v₁ j)) i :=
by simp only [linear_equiv_matrix, to_matrix, to_matrixₗ, ite_smul,
linear_equiv.trans_apply, linear_equiv.arrow_congr_apply,
linear_equiv.coe_coe, linear_equiv_matrix'_apply, finset.mem_univ, if_true,
one_smul, zero_smul, finset.sum_ite_eq, hv₁.equiv_fun_symm_apply]
lemma linear_equiv_matrix_apply' (f : M₁ →ₗ[R] M₂) (i : κ) (j : ι) :
linear_equiv_matrix hv₁ hv₂ f i j = hv₂.repr (f (v₁ j)) i :=
linear_equiv_matrix_apply hv₁ hv₂ f i j
@[simp]
lemma linear_equiv_matrix_id : linear_equiv_matrix hv₁ hv₁ id = 1 :=
begin
ext i j,
simp [linear_equiv_matrix_apply, is_basis.equiv_fun, matrix.one_apply, finsupp.single, eq_comm]
end
@[simp] lemma linear_equiv_matrix_symm_one : (linear_equiv_matrix hv₁ hv₁).symm 1 = id :=
begin
rw [← linear_equiv_matrix_id hv₁, ← linear_equiv.trans_apply],
simp
end
open_locale classical
theorem linear_equiv_matrix_range (f : M₁ →ₗ[R] M₂) (k : κ) (i : ι) :
linear_equiv_matrix hv₁.range hv₂.range f ⟨v₂ k, mem_range_self k⟩ ⟨v₁ i, mem_range_self i⟩ =
linear_equiv_matrix hv₁ hv₂ f k i :=
if H : (0 : R) = 1 then eq_of_zero_eq_one H _ _ else
begin
haveI : nontrivial R := ⟨⟨0, 1, H⟩⟩,
simp_rw [linear_equiv_matrix, linear_equiv.trans_apply, linear_equiv_matrix'_apply,
← equiv.of_injective_apply _ hv₁.injective, ← equiv.of_injective_apply _ hv₂.injective,
to_matrix_of_equiv, ← linear_equiv.trans_apply, linear_equiv.arrow_congr_trans], congr' 3;
refine function.left_inverse.injective linear_equiv.symm_symm _; ext x;
simp_rw [linear_equiv.symm_trans_apply, is_basis.equiv_fun_symm_apply, fun_congr_left_symm,
fun_congr_left_apply, fun_left_apply],
convert (finset.sum_equiv (equiv.of_injective _ hv₁.injective) _).symm,
simp_rw [equiv.symm_apply_apply, equiv.of_injective_apply, subtype.coe_mk],
convert (finset.sum_equiv (equiv.of_injective _ hv₂.injective) _).symm,
simp_rw [equiv.symm_apply_apply, equiv.of_injective_apply, subtype.coe_mk]
end
end linear_equiv_matrix
namespace matrix
open_locale matrix
lemma comp_to_matrix_mul {R : Type v} [comm_ring R] [decidable_eq l] [decidable_eq m]
(f : (m → R) →ₗ[R] (n → R)) (g : (l → R) →ₗ[R] (m → R)) :
(f.comp g).to_matrix = f.to_matrix ⬝ g.to_matrix :=
suffices (f.comp g) = (f.to_matrix ⬝ g.to_matrix).to_lin, by rw [this, to_lin_to_matrix],
by rw [mul_to_lin, to_matrix_to_lin, to_matrix_to_lin]
section comp
variables {R ι κ μ M₁ M₂ M₃ : Type*} [comm_ring R]
[add_comm_group M₁] [module R M₁]
[add_comm_group M₂] [module R M₂]
[add_comm_group M₃] [module R M₃]
[fintype ι] [decidable_eq κ] [fintype κ] [fintype μ]
{v₁ : ι → M₁} {v₂ : κ → M₂} {v₃ : μ → M₃}
(hv₁ : is_basis R v₁) (hv₂ : is_basis R v₂) (hv₃ : is_basis R v₃)
lemma linear_equiv_matrix_comp [decidable_eq ι] (f : M₂ →ₗ[R] M₃) (g : M₁ →ₗ[R] M₂) :
linear_equiv_matrix hv₁ hv₃ (f.comp g) =
linear_equiv_matrix hv₂ hv₃ f ⬝ linear_equiv_matrix hv₁ hv₂ g :=
by simp_rw [linear_equiv_matrix, linear_equiv.trans_apply, linear_equiv_matrix'_apply,
linear_equiv.arrow_congr_comp _ hv₂.equiv_fun, comp_to_matrix_mul]
lemma linear_equiv_matrix_mul [decidable_eq ι] (f g : M₁ →ₗ[R] M₁) :
linear_equiv_matrix hv₁ hv₁ (f * g) = linear_equiv_matrix hv₁ hv₁ f * linear_equiv_matrix hv₁ hv₁ g :=
linear_equiv_matrix_comp hv₁ hv₁ hv₁ f g
lemma linear_equiv_matrix_symm_mul [decidable_eq μ] (A : matrix ι κ R) (B : matrix κ μ R) :
(linear_equiv_matrix hv₃ hv₁).symm (A ⬝ B) =
((linear_equiv_matrix hv₂ hv₁).symm A).comp ((linear_equiv_matrix hv₃ hv₂).symm B) :=
begin
suffices : A ⬝ B = (linear_equiv_matrix hv₃ hv₁)
(((linear_equiv_matrix hv₂ hv₁).symm A).comp $ (linear_equiv_matrix hv₃ hv₂).symm B),
by rw [this, ← linear_equiv.trans_apply, linear_equiv.trans_symm, linear_equiv.refl_apply],
rw [linear_equiv_matrix_comp hv₃ hv₂ hv₁,
linear_equiv.apply_symm_apply, linear_equiv.apply_symm_apply]
end
end comp
end matrix
section is_basis_to_matrix
variables {ι ι' R M : Type*} [fintype ι] [decidable_eq ι]
[comm_ring R] [add_comm_group M] [module R M]
open function matrix
/-- From a basis `e : ι → M` and a family of vectors `v : ι → M`, make the matrix whose columns
are the vectors `v i` written in the basis `e`. -/
def is_basis.to_matrix {e : ι → M} (he : is_basis R e) (v : ι → M) : matrix ι ι R :=
linear_equiv_matrix he he (he.constr v)
variables {e : ι → M} (he : is_basis R e) (v : ι → M) (i j : ι)
namespace is_basis
lemma to_matrix_apply : he.to_matrix v i j = he.equiv_fun (v j) i :=
by simp [is_basis.to_matrix, linear_equiv_matrix_apply]
@[simp] lemma to_matrix_self : he.to_matrix e = 1 :=
begin
rw is_basis.to_matrix,
ext i j,
simp [linear_equiv_matrix_apply, is_basis.equiv_fun, matrix.one_apply, finsupp.single, eq_comm]
end
lemma to_matrix_update (x : M) :
he.to_matrix (function.update v i x) = matrix.update_column (he.to_matrix v) i (he.repr x) :=
begin
ext j k,
rw [is_basis.to_matrix, linear_equiv_matrix_apply' he he (he.constr (update v i x)),
matrix.update_column_apply, constr_basis, he.to_matrix_apply],
split_ifs,
{ rw [h, update_same i x v] },
{ rw [update_noteq h, he.equiv_fun_apply] },
end
/-- From a basis `e : ι → M`, build a linear equivalence between families of vectors `v : ι → M`,
and matrices, making the matrix whose columns are the vectors `v i` written in the basis `e`. -/
def to_matrix_equiv {e : ι → M} (he : is_basis R e) : (ι → M) ≃ₗ[R] matrix ι ι R :=
{ to_fun := he.to_matrix,
map_add' := λ v w, begin
ext i j,
change _ = _ + _,
simp [he.to_matrix_apply]
end,
map_smul' := begin
intros c v,
ext i j,
simp [he.to_matrix_apply]
end,
inv_fun := λ m j, ∑ i, (m i j) • e i,
left_inv := begin
intro v,
ext j,
simp [he.to_matrix_apply, he.equiv_fun_total (v j)]
end,
right_inv := begin
intros x,
ext k l,
simp [he.to_matrix_apply, he.equiv_fun.map_sum, he.equiv_fun.map_smul,
fintype.sum_apply k (λ i, x i l • he.equiv_fun (e i)),
he.equiv_fun_self]
end }
end is_basis
end is_basis_to_matrix
open_locale matrix
section det
open matrix
variables {R ι M M' : Type*} [comm_ring R]
[add_comm_group M] [module R M]
[add_comm_group M'] [module R M']
[decidable_eq ι] [fintype ι]
{v : ι → M} {v' : ι → M'}
lemma linear_equiv.is_unit_det (f : M ≃ₗ[R] M') (hv : is_basis R v) (hv' : is_basis R v') :
is_unit (linear_equiv_matrix hv hv' f).det :=
begin
apply is_unit_det_of_left_inverse,
simpa using (linear_equiv_matrix_comp hv hv' hv f.symm f).symm
end
/-- Builds a linear equivalence from a linear map whose determinant in some bases is a unit. -/
def linear_equiv.of_is_unit_det {f : M →ₗ[R] M'} {hv : is_basis R v} {hv' : is_basis R v'}
(h : is_unit (linear_equiv_matrix hv hv' f).det) : M ≃ₗ[R] M' :=
{ to_fun := f,
map_add' := f.map_add,
map_smul' := f.map_smul,
inv_fun := (linear_equiv_matrix hv' hv).symm (linear_equiv_matrix hv hv' f)⁻¹,
left_inv := begin
rw function.left_inverse_iff_comp,
have : f = (linear_equiv_matrix hv hv').symm (linear_equiv_matrix hv hv' f),
{ rw ← linear_equiv.trans_apply,
simp },
conv_lhs { congr, skip, rw this },
rw [linear_map.comp_coe, ← linear_equiv_matrix_symm_mul],
simp [h]
end,
right_inv := begin
rw function.right_inverse_iff_comp,
have : f = (linear_equiv_matrix hv hv').symm (linear_equiv_matrix hv hv' f),
{ change f = (linear_equiv_matrix hv hv').trans (linear_equiv_matrix hv hv').symm f,
simp },
conv_lhs { congr, rw this },
rw [linear_map.comp_coe, ← linear_equiv_matrix_symm_mul],
simp [h]
end }
variables {e : ι → M} (he : is_basis R e)
/-- The determinant of a family of vectors with respect to some basis, as a multilinear map. -/
def is_basis.det : multilinear_map R (λ i : ι, M) R :=
{ to_fun := λ v, det (he.to_matrix v),
map_add' := begin
intros v i x y,
simp only [he.to_matrix_update, linear_map.map_add],
apply det_update_column_add
end,
map_smul' := begin
intros u i c x,
simp only [he.to_matrix_update, algebra.id.smul_eq_mul, map_smul_eq_smul_map],
apply det_update_column_smul
end }
lemma is_basis.det_apply (v : ι → M) : he.det v = det (he.to_matrix v) := rfl
lemma is_basis.det_self : he.det e = 1 :=
by simp [he.det_apply]
lemma is_basis.iff_det {v : ι → M} : is_basis R v ↔ is_unit (he.det v) :=
begin
split,
{ intro hv,
change is_unit (linear_equiv_matrix he he (equiv_of_is_basis he hv $ equiv.refl ι)).det,
apply linear_equiv.is_unit_det },
{ intro h,
convert linear_equiv.is_basis he (linear_equiv.of_is_unit_det h),
ext i,
exact (constr_basis he).symm },
end
end det
namespace matrix
section trace
variables (n) (R : Type v) (M : Type w) [semiring R] [add_comm_monoid M] [semimodule R M]
/--
The diagonal of a square matrix.
-/
def diag : (matrix n n M) →ₗ[R] n → M :=
{ to_fun := λ A i, A i i,
map_add' := by { intros, ext, refl, },
map_smul' := by { intros, ext, refl, } }
variables {n} {R} {M}
@[simp] lemma diag_apply (A : matrix n n M) (i : n) : diag n R M A i = A i i := rfl
@[simp] lemma diag_one [decidable_eq n] :
diag n R R 1 = λ i, 1 := by { dunfold diag, ext, simp [one_apply_eq] }
@[simp] lemma diag_transpose (A : matrix n n M) : diag n R M Aᵀ = diag n R M A := rfl
variables (n) (R) (M)
/--
The trace of a square matrix.
-/
def trace : (matrix n n M) →ₗ[R] M :=
{ to_fun := λ A, ∑ i, diag n R M A i,
map_add' := by { intros, apply finset.sum_add_distrib, },
map_smul' := by { intros, simp [finset.smul_sum], } }
variables {n} {R} {M}
@[simp] lemma trace_diag (A : matrix n n M) : trace n R M A = ∑ i, diag n R M A i := rfl
@[simp] lemma trace_one [decidable_eq n] :
trace n R R 1 = fintype.card n :=
have h : trace n R R 1 = ∑ i, diag n R R 1 i := rfl,
by simp_rw [h, diag_one, finset.sum_const, nsmul_one]; refl
@[simp] lemma trace_transpose (A : matrix n n M) : trace n R M Aᵀ = trace n R M A := rfl
@[simp] lemma trace_transpose_mul (A : matrix m n R) (B : matrix n m R) :
trace n R R (Aᵀ ⬝ Bᵀ) = trace m R R (A ⬝ B) := finset.sum_comm
lemma trace_mul_comm {S : Type v} [comm_ring S] (A : matrix m n S) (B : matrix n m S) :
trace n S S (B ⬝ A) = trace m S S (A ⬝ B) :=
by rw [←trace_transpose, ←trace_transpose_mul, transpose_mul]
end trace
section ring
variables {R : Type v} [comm_ring R] [decidable_eq n]
open linear_map matrix
lemma proj_diagonal (i : n) (w : n → R) :
(proj i).comp (to_lin (diagonal w)) = (w i) • proj i :=
by ext j; simp [mul_vec_diagonal]
lemma diagonal_comp_std_basis (w : n → R) (i : n) :
(diagonal w).to_lin.comp (std_basis R (λ_:n, R) i) = (w i) • std_basis R (λ_:n, R) i :=
begin
ext a j,
simp_rw [linear_map.comp_apply, to_lin_apply, mul_vec_diagonal, linear_map.smul_apply,
pi.smul_apply, algebra.id.smul_eq_mul],
by_cases i = j,
{ subst h },
{ rw [std_basis_ne R (λ_:n, R) _ _ (ne.symm h), _root_.mul_zero, _root_.mul_zero] }
end
lemma diagonal_to_lin (w : n → R) :
(diagonal w).to_lin = linear_map.pi (λi, w i • linear_map.proj i) :=
by ext v j; simp [mul_vec_diagonal]
/-- An invertible matrix yields a linear equivalence from the free module to itself. -/
def to_linear_equiv (P : matrix n n R) (h : is_unit P) : (n → R) ≃ₗ[R] (n → R) :=
have h' : is_unit P.det := P.is_unit_iff_is_unit_det.mp h,
{ inv_fun := P⁻¹.to_lin,
left_inv := λ v,
show (P⁻¹.to_lin.comp P.to_lin) v = v,
by rw [←matrix.mul_to_lin, P.nonsing_inv_mul h', matrix.to_lin_one, linear_map.id_apply],
right_inv := λ v,
show (P.to_lin.comp P⁻¹.to_lin) v = v,
by rw [←matrix.mul_to_lin, P.mul_nonsing_inv h', matrix.to_lin_one, linear_map.id_apply],
..P.to_lin }
@[simp] lemma to_linear_equiv_apply (P : matrix n n R) (h : is_unit P) :
(↑(P.to_linear_equiv h) : module.End R (n → R)) = P.to_lin := rfl
@[simp] lemma to_linear_equiv_symm_apply (P : matrix n n R) (h : is_unit P) :
(↑(P.to_linear_equiv h).symm : module.End R (n → R)) = P⁻¹.to_lin := rfl
end ring
section vector_space
variables {K : Type u} [field K] -- maybe try to relax the universe constraint
open linear_map matrix
set_option pp.all true
lemma rank_vec_mul_vec {m n : Type u} [fintype m] [fintype n]
(w : m → K) (v : n → K) :
rank (vec_mul_vec w v).to_lin ≤ 1 :=
begin
rw [vec_mul_vec_eq, mul_to_lin],
refine le_trans (rank_comp_le1 _ _) _,
refine le_trans (rank_le_domain _) _,
rw [dim_fun', ← cardinal.lift_eq_nat_iff.mpr (cardinal.fintype_card unit), cardinal.mk_unit],
exact le_of_eq (cardinal.lift_one)
end
lemma ker_diagonal_to_lin [decidable_eq m] (w : m → K) :
ker (diagonal w).to_lin = (⨆i∈{i | w i = 0 }, range (std_basis K (λi, K) i)) :=
begin
rw [← comap_bot, ← infi_ker_proj],
simp only [comap_infi, (ker_comp _ _).symm, proj_diagonal, ker_smul'],
have : univ ⊆ {i : m | w i = 0} ∪ {i : m | w i = 0}ᶜ, { rw set.union_compl_self },
exact (supr_range_std_basis_eq_infi_ker_proj K (λi:m, K)
(disjoint_compl_right {i | w i = 0}) this (finite.of_fintype _)).symm
end
lemma range_diagonal [decidable_eq m] (w : m → K) :
(diagonal w).to_lin.range = (⨆ i ∈ {i | w i ≠ 0}, (std_basis K (λi, K) i).range) :=
begin
dsimp only [mem_set_of_eq],
rw [← map_top, ← supr_range_std_basis, map_supr],
congr, funext i,
rw [← linear_map.range_comp, diagonal_comp_std_basis, ← range_smul']
end
lemma rank_diagonal [decidable_eq m] [decidable_eq K] (w : m → K) :
rank (diagonal w).to_lin = fintype.card { i // w i ≠ 0 } :=
begin
have hu : univ ⊆ {i : m | w i = 0}ᶜ ∪ {i : m | w i = 0}, { rw set.compl_union_self },
have hd : disjoint {i : m | w i ≠ 0} {i : m | w i = 0} := (disjoint_compl_right {i | w i = 0}).symm,
have h₁ := supr_range_std_basis_eq_infi_ker_proj K (λi:m, K) hd hu (finite.of_fintype _),
have h₂ := @infi_ker_proj_equiv K _ _ (λi:m, K) _ _ _ _ (by simp; apply_instance) hd hu,
rw [rank, range_diagonal, h₁, ←@dim_fun' K],
apply linear_equiv.dim_eq,
apply h₂,
end
end vector_space
section finite_dimensional
variables {R : Type v} [field R]
instance : finite_dimensional R (matrix m n R) :=
linear_equiv.finite_dimensional (linear_equiv.uncurry R m n).symm
/--
The dimension of the space of finite dimensional matrices
is the product of the number of rows and columns.
-/
@[simp] lemma findim_matrix :
finite_dimensional.findim R (matrix m n R) = fintype.card m * fintype.card n :=
by rw [@linear_equiv.findim_eq R (matrix m n R) _ _ _ _ _ _ (linear_equiv.uncurry R m n),
finite_dimensional.findim_fintype_fun_eq_card, fintype.card_prod]
end finite_dimensional
section reindexing
variables {l' m' n' : Type*} [fintype l'] [fintype m'] [fintype n']
variables {R : Type v}
/-- The natural map that reindexes a matrix's rows and columns with equivalent types is an
equivalence. -/
def reindex (eₘ : m ≃ m') (eₙ : n ≃ n') : matrix m n R ≃ matrix m' n' R :=
{ to_fun := λ M i j, M (eₘ.symm i) (eₙ.symm j),
inv_fun := λ M i j, M (eₘ i) (eₙ j),
left_inv := λ M, by simp,
right_inv := λ M, by simp, }
@[simp] lemma reindex_apply (eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m n R) :
reindex eₘ eₙ M = λ i j, M (eₘ.symm i) (eₙ.symm j) :=
rfl
@[simp] lemma reindex_symm_apply (eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m' n' R) :
(reindex eₘ eₙ).symm M = λ i j, M (eₘ i) (eₙ j) :=
rfl
/-- The natural map that reindexes a matrix's rows and columns with equivalent types is a linear
equivalence. -/
def reindex_linear_equiv [semiring R] (eₘ : m ≃ m') (eₙ : n ≃ n') :
matrix m n R ≃ₗ[R] matrix m' n' R :=
{ map_add' := λ M N, rfl,
map_smul' := λ M N, rfl,
..(reindex eₘ eₙ)}
@[simp] lemma reindex_linear_equiv_apply [semiring R]
(eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m n R) :
reindex_linear_equiv eₘ eₙ M = λ i j, M (eₘ.symm i) (eₙ.symm j) :=
rfl
@[simp] lemma reindex_linear_equiv_symm_apply [semiring R]
(eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m' n' R) :
(reindex_linear_equiv eₘ eₙ).symm M = λ i j, M (eₘ i) (eₙ j) :=
rfl
lemma reindex_mul [semiring R]
(eₘ : m ≃ m') (eₙ : n ≃ n') (eₗ : l ≃ l') (M : matrix m n R) (N : matrix n l R) :
(reindex_linear_equiv eₘ eₙ M) ⬝ (reindex_linear_equiv eₙ eₗ N) = reindex_linear_equiv eₘ eₗ (M ⬝ N) :=
begin
ext i j,
dsimp only [matrix.mul, matrix.dot_product],
rw [←finset.univ_map_equiv_to_embedding eₙ, finset.sum_map finset.univ eₙ.to_embedding],
simp,
end
/-- For square matrices, the natural map that reindexes a matrix's rows and columns with equivalent
types is an equivalence of algebras. -/
def reindex_alg_equiv [comm_semiring R] [decidable_eq m] [decidable_eq n]
(e : m ≃ n) : matrix m m R ≃ₐ[R] matrix n n R :=
{ map_mul' := λ M N, by simp only [reindex_mul, linear_equiv.to_fun_apply, mul_eq_mul],
commutes' := λ r, by { ext, simp [algebra_map, algebra.to_ring_hom], by_cases h : i = j; simp [h], },
..(reindex_linear_equiv e e) }
@[simp] lemma reindex_alg_equiv_apply [comm_semiring R] [decidable_eq m] [decidable_eq n]
(e : m ≃ n) (M : matrix m m R) :
reindex_alg_equiv e M = λ i j, M (e.symm i) (e.symm j) :=
rfl
@[simp] lemma reindex_alg_equiv_symm_apply [comm_semiring R] [decidable_eq m] [decidable_eq n]
(e : m ≃ n) (M : matrix n n R) :
(reindex_alg_equiv e).symm M = λ i j, M (e i) (e j) :=
rfl
lemma reindex_transpose (eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m n R) :
(reindex eₘ eₙ M)ᵀ = (reindex eₙ eₘ Mᵀ) :=
rfl
end reindexing
end matrix
namespace linear_map
open_locale matrix
/-- The trace of an endomorphism given a basis. -/
def trace_aux (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type w} [decidable_eq ι] [fintype ι] {b : ι → M} (hb : is_basis R b) :
(M →ₗ[R] M) →ₗ[R] R :=
(matrix.trace ι R R).comp $ linear_equiv_matrix hb hb
@[simp] lemma trace_aux_def (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type w} [decidable_eq ι] [fintype ι] {b : ι → M} (hb : is_basis R b) (f : M →ₗ[R] M) :
trace_aux R hb f = matrix.trace ι R R (linear_equiv_matrix hb hb f) :=
rfl
theorem trace_aux_eq' (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type w} [decidable_eq ι] [fintype ι] {b : ι → M} (hb : is_basis R b)
{κ : Type w} [decidable_eq κ] [fintype κ] {c : κ → M} (hc : is_basis R c) :
trace_aux R hb = trace_aux R hc :=
linear_map.ext $ λ f,
calc matrix.trace ι R R (linear_equiv_matrix hb hb f)
= matrix.trace ι R R (linear_equiv_matrix hb hb ((linear_map.id.comp f).comp linear_map.id)) :
by rw [linear_map.id_comp, linear_map.comp_id]
... = matrix.trace ι R R (linear_equiv_matrix hc hb linear_map.id ⬝
linear_equiv_matrix hc hc f ⬝
linear_equiv_matrix hb hc linear_map.id) :
by rw [matrix.linear_equiv_matrix_comp _ hc, matrix.linear_equiv_matrix_comp _ hc]
... = matrix.trace κ R R (linear_equiv_matrix hc hc f ⬝
linear_equiv_matrix hb hc linear_map.id ⬝
linear_equiv_matrix hc hb linear_map.id) :
by rw [matrix.mul_assoc, matrix.trace_mul_comm]
... = matrix.trace κ R R (linear_equiv_matrix hc hc ((f.comp linear_map.id).comp linear_map.id)) :
by rw [matrix.linear_equiv_matrix_comp _ hb, matrix.linear_equiv_matrix_comp _ hc]
... = matrix.trace κ R R (linear_equiv_matrix hc hc f) :
by rw [linear_map.comp_id, linear_map.comp_id]
open_locale classical
theorem trace_aux_range (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type w} [fintype ι] {b : ι → M} (hb : is_basis R b) :
trace_aux R hb.range = trace_aux R hb :=
linear_map.ext $ λ f, if H : 0 = 1 then eq_of_zero_eq_one H _ _ else
begin
haveI : nontrivial R := ⟨⟨0, 1, H⟩⟩,
change ∑ i : set.range b, _ = ∑ i : ι, _, simp_rw [matrix.diag_apply], symmetry,
convert finset.sum_equiv (equiv.of_injective _ hb.injective) _, ext i,
exact (linear_equiv_matrix_range hb hb f i i).symm
end
/-- where `ι` and `κ` can reside in different universes -/
theorem trace_aux_eq (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type*} [decidable_eq ι] [fintype ι] {b : ι → M} (hb : is_basis R b)
{κ : Type*} [decidable_eq κ] [fintype κ] {c : κ → M} (hc : is_basis R c) :
trace_aux R hb = trace_aux R hc :=
calc trace_aux R hb
= trace_aux R hb.range : by { rw trace_aux_range R hb, congr }
... = trace_aux R hc.range : trace_aux_eq' _ _ _
... = trace_aux R hc : by { rw trace_aux_range R hc, congr }
/-- Trace of an endomorphism independent of basis. -/
def trace (R : Type u) [comm_ring R] (M : Type v) [add_comm_group M] [module R M] :
(M →ₗ[R] M) →ₗ[R] R :=
if H : ∃ s : finset M, is_basis R (λ x, x : (↑s : set M) → M)
then trace_aux R (classical.some_spec H)
else 0
theorem trace_eq_matrix_trace (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type w} [fintype ι] {b : ι → M} (hb : is_basis R b) (f : M →ₗ[R] M) :
trace R M f = matrix.trace ι R R (linear_equiv_matrix hb hb f) :=
have ∃ s : finset M, is_basis R (λ x, x : (↑s : set M) → M),
from ⟨finset.univ.image b,
by { rw [finset.coe_image, finset.coe_univ, set.image_univ], exact hb.range }⟩,
by { rw [trace, dif_pos this, ← trace_aux_def], congr' 1, apply trace_aux_eq }
theorem trace_mul_comm (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
(f g : M →ₗ[R] M) : trace R M (f * g) = trace R M (g * f) :=
if H : ∃ s : finset M, is_basis R (λ x, x : (↑s : set M) → M) then let ⟨s, hb⟩ := H in
by { simp_rw [trace_eq_matrix_trace R hb, matrix.linear_equiv_matrix_mul], apply matrix.trace_mul_comm }
else by rw [trace, dif_neg H, linear_map.zero_apply, linear_map.zero_apply]
section finite_dimensional
variables {K : Type*} [field K]
variables {V : Type*} [add_comm_group V] [vector_space K V] [finite_dimensional K V]
variables {W : Type*} [add_comm_group W] [vector_space K W] [finite_dimensional K W]
instance : finite_dimensional K (V →ₗ[K] W) :=
begin
classical,
cases finite_dimensional.exists_is_basis_finset K V with bV hbV,
cases finite_dimensional.exists_is_basis_finset K W with bW hbW,
apply linear_equiv.finite_dimensional (linear_equiv_matrix hbV hbW).symm,
end
/--
The dimension of the space of linear transformations is the product of the dimensions of the
domain and codomain.
-/
@[simp] lemma findim_linear_map :
finite_dimensional.findim K (V →ₗ[K] W) =
(finite_dimensional.findim K V) * (finite_dimensional.findim K W) :=
begin
classical,
cases finite_dimensional.exists_is_basis_finset K V with bV hbV,
cases finite_dimensional.exists_is_basis_finset K W with bW hbW,
rw [linear_equiv.findim_eq (linear_equiv_matrix hbV hbW), matrix.findim_matrix,
finite_dimensional.findim_eq_card_basis hbV, finite_dimensional.findim_eq_card_basis hbW,
mul_comm],
end
end finite_dimensional
end linear_map
/-- The natural equivalence between linear endomorphisms of finite free modules and square matrices
is compatible with the algebra structures. -/
def alg_equiv_matrix' {R : Type v} [comm_ring R] [decidable_eq n] :
module.End R (n → R) ≃ₐ[R] matrix n n R :=
{ map_mul' := matrix.comp_to_matrix_mul,
map_add' := linear_equiv_matrix'.map_add,
commutes' := λ r, by { change (r • (linear_map.id : module.End R _)).to_matrix = r • 1,
rw ←linear_map.to_matrix_id, refl, },
..linear_equiv_matrix' }
/-- A linear equivalence of two modules induces an equivalence of algebras of their
endomorphisms. -/
def linear_equiv.alg_conj {R : Type v} [comm_ring R] {M₁ M₂ : Type*}
[add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂] (e : M₁ ≃ₗ[R] M₂) :
module.End R M₁ ≃ₐ[R] module.End R M₂ :=
{ map_mul' := λ f g, by apply e.arrow_congr_comp,
map_add' := e.conj.map_add,
commutes' := λ r, by { change e.conj (r • linear_map.id) = r • linear_map.id,
rw [linear_equiv.map_smul, linear_equiv.conj_id], },
..e.conj }
/-- A basis of a module induces an equivalence of algebras from the endomorphisms of the module to
square matrices. -/
def alg_equiv_matrix {R : Type v} {M : Type w}
[comm_ring R] [add_comm_group M] [module R M] [decidable_eq n] {b : n → M} (h : is_basis R b) :
module.End R M ≃ₐ[R] matrix n n R :=
h.equiv_fun.alg_conj.trans alg_equiv_matrix'
|
06fb216b5dd7754afb42a35c13827d692638b215 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/number_theory/ramification_inertia.lean | 5a54db5a86d19c74e17d56f048d88844546f39b3 | [
"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 | 39,248 | lean | /-
Copyright (c) 2022 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen
-/
import algebra.is_prime_pow
import field_theory.separable
import linear_algebra.free_module.finite.rank
import linear_algebra.free_module.pid
import linear_algebra.matrix.nonsingular_inverse
import ring_theory.dedekind_domain.ideal
import ring_theory.localization.module
/-!
# Ramification index and inertia degree
Given `P : ideal S` lying over `p : ideal R` for the ring extension `f : R →+* S`
(assuming `P` and `p` are prime or maximal where needed),
the **ramification index** `ideal.ramification_idx f p P` is the multiplicity of `P` in `map f p`,
and the **inertia degree** `ideal.inertia_deg f p P` is the degree of the field extension
`(S / P) : (R / p)`.
## Main results
The main theorem `ideal.sum_ramification_inertia` states that for all coprime `P` lying over `p`,
`Σ P, ramification_idx f p P * inertia_deg f p P` equals the degree of the field extension
`Frac(S) : Frac(R)`.
## Implementation notes
Often the above theory is set up in the case where:
* `R` is the ring of integers of a number field `K`,
* `L` is a finite separable extension of `K`,
* `S` is the integral closure of `R` in `L`,
* `p` and `P` are maximal ideals,
* `P` is an ideal lying over `p`
We will try to relax the above hypotheses as much as possible.
## Notation
In this file, `e` stands for the ramification index and `f` for the inertia degree of `P` over `p`,
leaving `p` and `P` implicit.
-/
namespace ideal
universes u v
variables {R : Type u} [comm_ring R]
variables {S : Type v} [comm_ring S] (f : R →+* S)
variables (p : ideal R) (P : ideal S)
open finite_dimensional
open unique_factorization_monoid
section dec_eq
open_locale classical
/-- The ramification index of `P` over `p` is the largest exponent `n` such that
`p` is contained in `P^n`.
In particular, if `p` is not contained in `P^n`, then the ramification index is 0.
If there is no largest such `n` (e.g. because `p = ⊥`), then `ramification_idx` is
defined to be 0.
-/
noncomputable def ramification_idx : ℕ :=
Sup {n | map f p ≤ P ^ n}
variables {f p P}
lemma ramification_idx_eq_find (h : ∃ n, ∀ k, map f p ≤ P ^ k → k ≤ n) :
ramification_idx f p P = nat.find h :=
nat.Sup_def h
lemma ramification_idx_eq_zero (h : ∀ n : ℕ, ∃ k, map f p ≤ P ^ k ∧ n < k) :
ramification_idx f p P = 0 :=
dif_neg (by push_neg; exact h)
lemma ramification_idx_spec {n : ℕ} (hle : map f p ≤ P ^ n) (hgt : ¬ map f p ≤ P ^ (n + 1)) :
ramification_idx f p P = n :=
begin
have : ∀ (k : ℕ), map f p ≤ P ^ k → k ≤ n,
{ intros k hk,
refine le_of_not_lt (λ hnk, _),
exact hgt (hk.trans (ideal.pow_le_pow hnk)) },
rw ramification_idx_eq_find ⟨n, this⟩,
{ refine le_antisymm (nat.find_min' _ this) (le_of_not_gt (λ (h : nat.find _ < n), _)),
obtain this' := nat.find_spec ⟨n, this⟩,
exact h.not_le (this' _ hle) },
end
lemma ramification_idx_lt {n : ℕ} (hgt : ¬ (map f p ≤ P ^ n)) :
ramification_idx f p P < n :=
begin
cases n,
{ simpa using hgt },
rw nat.lt_succ_iff,
have : ∀ k, map f p ≤ P ^ k → k ≤ n,
{ refine λ k hk, le_of_not_lt (λ hnk, _),
exact hgt (hk.trans (ideal.pow_le_pow hnk)) },
rw ramification_idx_eq_find ⟨n, this⟩,
exact nat.find_min' ⟨n, this⟩ this
end
@[simp] lemma ramification_idx_bot : ramification_idx f ⊥ P = 0 :=
dif_neg $ not_exists.mpr $ λ n hn, n.lt_succ_self.not_le (hn _ (by simp))
@[simp] lemma ramification_idx_of_not_le (h : ¬ map f p ≤ P) : ramification_idx f p P = 0 :=
ramification_idx_spec (by simp) (by simpa using h)
lemma ramification_idx_ne_zero {e : ℕ} (he : e ≠ 0)
(hle : map f p ≤ P ^ e) (hnle : ¬ map f p ≤ P ^ (e + 1)):
ramification_idx f p P ≠ 0 :=
by rwa ramification_idx_spec hle hnle
lemma le_pow_of_le_ramification_idx {n : ℕ} (hn : n ≤ ramification_idx f p P) :
map f p ≤ P ^ n :=
begin
contrapose! hn,
exact ramification_idx_lt hn
end
lemma le_pow_ramification_idx :
map f p ≤ P ^ ramification_idx f p P :=
le_pow_of_le_ramification_idx (le_refl _)
lemma le_comap_pow_ramification_idx :
p ≤ comap f (P ^ ramification_idx f p P) :=
map_le_iff_le_comap.mp le_pow_ramification_idx
lemma le_comap_of_ramification_idx_ne_zero (h : ramification_idx f p P ≠ 0) : p ≤ comap f P :=
ideal.map_le_iff_le_comap.mp $ le_pow_ramification_idx.trans $ ideal.pow_le_self $ h
namespace is_dedekind_domain
variables [is_domain S] [is_dedekind_domain S]
lemma ramification_idx_eq_normalized_factors_count
(hp0 : map f p ≠ ⊥) (hP : P.is_prime) (hP0 : P ≠ ⊥) :
ramification_idx f p P = (normalized_factors (map f p)).count P :=
begin
have hPirr := (ideal.prime_of_is_prime hP0 hP).irreducible,
refine ramification_idx_spec (ideal.le_of_dvd _) (mt ideal.dvd_iff_le.mpr _);
rw [dvd_iff_normalized_factors_le_normalized_factors (pow_ne_zero _ hP0) hp0,
normalized_factors_pow, normalized_factors_irreducible hPirr, normalize_eq,
multiset.nsmul_singleton, ← multiset.le_count_iff_repeat_le],
{ exact (nat.lt_succ_self _).not_le },
end
lemma ramification_idx_eq_factors_count (hp0 : map f p ≠ ⊥) (hP : P.is_prime) (hP0 : P ≠ ⊥) :
ramification_idx f p P = (factors (map f p)).count P :=
by rw [is_dedekind_domain.ramification_idx_eq_normalized_factors_count hp0 hP hP0,
factors_eq_normalized_factors]
lemma ramification_idx_ne_zero (hp0 : map f p ≠ ⊥) (hP : P.is_prime) (le : map f p ≤ P) :
ramification_idx f p P ≠ 0 :=
begin
have hP0 : P ≠ ⊥,
{ unfreezingI { rintro rfl },
have := le_bot_iff.mp le,
contradiction },
have hPirr := (ideal.prime_of_is_prime hP0 hP).irreducible,
rw is_dedekind_domain.ramification_idx_eq_normalized_factors_count hp0 hP hP0,
obtain ⟨P', hP', P'_eq⟩ :=
exists_mem_normalized_factors_of_dvd hp0 hPirr (ideal.dvd_iff_le.mpr le),
rwa [multiset.count_ne_zero, associated_iff_eq.mp P'_eq],
end
end is_dedekind_domain
variables (f p P)
local attribute [instance] ideal.quotient.field
/-- The inertia degree of `P : ideal S` lying over `p : ideal R` is the degree of the
extension `(S / P) : (R / p)`.
We do not assume `P` lies over `p` in the definition; we return `0` instead.
See `inertia_deg_algebra_map` for the common case where `f = algebra_map R S`
and there is an algebra structure `R / p → S / P`.
-/
noncomputable def inertia_deg [hp : p.is_maximal] : ℕ :=
if hPp : comap f P = p
then @finrank (R ⧸ p) (S ⧸ P) _ _ $ @algebra.to_module _ _ _ _ $ ring_hom.to_algebra $
ideal.quotient.lift p (P^.quotient.mk^.comp f) $
λ a ha, quotient.eq_zero_iff_mem.mpr $ mem_comap.mp $ hPp.symm ▸ ha
else 0
-- Useful for the `nontriviality` tactic using `comap_eq_of_scalar_tower_quotient`.
@[simp] lemma inertia_deg_of_subsingleton [hp : p.is_maximal] [hQ : subsingleton (S ⧸ P)] :
inertia_deg f p P = 0 :=
begin
have := ideal.quotient.subsingleton_iff.mp hQ,
unfreezingI { subst this },
exact dif_neg (λ h, hp.ne_top $ h.symm.trans comap_top)
end
@[simp] lemma inertia_deg_algebra_map [algebra R S] [algebra (R ⧸ p) (S ⧸ P)]
[is_scalar_tower R (R ⧸ p) (S ⧸ P)]
[hp : p.is_maximal] :
inertia_deg (algebra_map R S) p P = finrank (R ⧸ p) (S ⧸ P) :=
begin
nontriviality (S ⧸ P) using [inertia_deg_of_subsingleton, finrank_zero_of_subsingleton],
have := comap_eq_of_scalar_tower_quotient (algebra_map (R ⧸ p) (S ⧸ P)).injective,
rw [inertia_deg, dif_pos this],
congr,
refine algebra.algebra_ext _ _ (λ x', quotient.induction_on' x' $ λ x, _),
change ideal.quotient.lift p _ _ (ideal.quotient.mk p x) =
algebra_map _ _ (ideal.quotient.mk p x),
rw [ideal.quotient.lift_mk, ← ideal.quotient.algebra_map_eq, ← is_scalar_tower.algebra_map_eq,
← ideal.quotient.algebra_map_eq, ← is_scalar_tower.algebra_map_apply]
end
end dec_eq
section finrank_quotient_map
open_locale big_operators
open_locale non_zero_divisors
variables [algebra R S]
variables {K : Type*} [field K] [algebra R K] [hRK : is_fraction_ring R K]
variables {L : Type*} [field L] [algebra S L] [is_fraction_ring S L]
variables {V V' V'' : Type*}
variables [add_comm_group V] [module R V] [module K V] [is_scalar_tower R K V]
variables [add_comm_group V'] [module R V'] [module S V'] [is_scalar_tower R S V']
variables [add_comm_group V''] [module R V'']
variables (K)
include hRK
/-- Let `V` be a vector space over `K = Frac(R)`, `S / R` a ring extension
and `V'` a module over `S`. If `b`, in the intersection `V''` of `V` and `V'`,
is linear independent over `S` in `V'`, then it is linear independent over `R` in `V`.
The statement we prove is actually slightly more general:
* it suffices that the inclusion `algebra_map R S : R → S` is nontrivial
* the function `f' : V'' → V'` doesn't need to be injective
-/
lemma finrank_quotient_map.linear_independent_of_nontrivial
[is_domain R] [is_dedekind_domain R] (hRS : (algebra_map R S).ker ≠ ⊤)
(f : V'' →ₗ[R] V) (hf : function.injective f) (f' : V'' →ₗ[R] V')
{ι : Type*} {b : ι → V''} (hb' : linear_independent S (f' ∘ b)) :
linear_independent K (f ∘ b) :=
begin
contrapose! hb' with hb,
-- Informally, if we have a nontrivial linear dependence with coefficients `g` in `K`,
-- then we can find a linear dependence with coefficients `I.quotient.mk g'` in `R/I`,
-- where `I = ker (algebra_map R S)`.
-- We make use of the same principle but stay in `R` everywhere.
simp only [linear_independent_iff', not_forall] at hb ⊢,
obtain ⟨s, g, eq, j', hj's, hj'g⟩ := hb,
use s,
obtain ⟨a, hag, j, hjs, hgI⟩ :=
ideal.exist_integer_multiples_not_mem hRS s g hj's hj'g,
choose g'' hg'' using hag,
letI := classical.prop_decidable,
let g' := λ i, if h : i ∈ s then g'' i h else 0,
have hg' : ∀ i ∈ s, algebra_map _ _ (g' i) = a * g i,
{ intros i hi, exact (congr_arg _ (dif_pos hi)).trans (hg'' i hi) },
-- Because `R/I` is nontrivial, we can lift `g` to a nontrivial linear dependence in `S`.
have hgI : algebra_map R S (g' j) ≠ 0,
{ simp only [fractional_ideal.mem_coe_ideal, not_exists, not_and'] at hgI,
exact hgI _ (hg' j hjs) },
refine ⟨λ i, algebra_map R S (g' i), _, j, hjs, hgI⟩,
have eq : f (∑ i in s, g' i • (b i)) = 0,
{ rw [linear_map.map_sum, ← smul_zero a, ← eq, finset.smul_sum, finset.sum_congr rfl],
intros i hi,
rw [linear_map.map_smul, ← is_scalar_tower.algebra_map_smul K, hg' i hi, ← smul_assoc,
smul_eq_mul],
apply_instance },
simp only [is_scalar_tower.algebra_map_smul, ← linear_map.map_smul, ← linear_map.map_sum,
(f.map_eq_zero_iff hf).mp eq, linear_map.map_zero],
end
open_locale matrix
variables {K}
omit hRK
/-- If `b` mod `p` spans `S/p` as `R/p`-space, then `b` itself spans `Frac(S)` as `K`-space.
Here,
* `p` is an ideal of `R` such that `R / p` is nontrivial
* `K` is a field that has an embedding of `R` (in particular we can take `K = Frac(R)`)
* `L` is a field extension of `K`
* `S` is the integral closure of `R` in `L`
More precisely, we avoid quotients in this statement and instead require that `b ∪ pS` spans `S`.
-/
lemma finrank_quotient_map.span_eq_top [is_domain R] [is_domain S] [algebra K L] [is_noetherian R S]
[algebra R L] [is_scalar_tower R S L] [is_scalar_tower R K L] [is_integral_closure S R L]
[no_zero_smul_divisors R K]
(hp : p ≠ ⊤)
(b : set S) (hb' : submodule.span R b ⊔ (p.map (algebra_map R S)).restrict_scalars R = ⊤) :
submodule.span K (algebra_map S L '' b) = ⊤ :=
begin
have hRL : function.injective (algebra_map R L),
{ rw is_scalar_tower.algebra_map_eq R K L,
exact (algebra_map K L).injective.comp (no_zero_smul_divisors.algebra_map_injective R K) },
-- Let `M` be the `R`-module spanned by the proposed basis elements.
set M : submodule R S := submodule.span R b with hM,
-- Then `S / M` is generated by some finite set of `n` vectors `a`.
letI h : module.finite R (S ⧸ M) :=
module.finite.of_surjective (submodule.mkq _) (submodule.quotient.mk_surjective _),
obtain ⟨n, a, ha⟩ := @@module.finite.exists_fin _ _ _ h,
-- Because the image of `p` in `S / M` is `⊤`,
have smul_top_eq : p • (⊤ : submodule R (S ⧸ M)) = ⊤,
{ calc p • ⊤ = submodule.map M.mkq (p • ⊤) :
by rw [submodule.map_smul'', submodule.map_top, M.range_mkq]
... = ⊤ : by rw [ideal.smul_top_eq_map, (submodule.map_mkq_eq_top M _).mpr hb'] },
-- we can write the elements of `a` as `p`-linear combinations of other elements of `a`.
have exists_sum : ∀ x : (S ⧸ M), ∃ a' : fin n → R, (∀ i, a' i ∈ p) ∧ ∑ i, a' i • a i = x,
{ intro x,
obtain ⟨a'', ha'', hx⟩ := (submodule.mem_ideal_smul_span_iff_exists_sum p a x).1 _,
{ refine ⟨λ i, a'' i, λ i, ha'' _, _⟩,
rw [← hx, finsupp.sum_fintype],
exact λ _, zero_smul _ _ },
{ rw [ha, smul_top_eq],
exact submodule.mem_top } },
choose A' hA'p hA' using λ i, exists_sum (a i),
-- This gives us a(n invertible) matrix `A` such that `det A ∈ (M = span R b)`,
let A : matrix (fin n) (fin n) R := A' - 1,
let B := A.adjugate,
have A_smul : ∀ i, ∑ j, A i j • a j = 0,
{ intros,
simp only [A, pi.sub_apply, sub_smul, finset.sum_sub_distrib, hA', matrix.one_apply, ite_smul,
one_smul, zero_smul, finset.sum_ite_eq, finset.mem_univ, if_true, sub_self] },
-- since `span S {det A} / M = 0`.
have d_smul : ∀ i, A.det • a i = 0,
{ intro i,
calc A.det • a i = ∑ j, (B ⬝ A) i j • a j : _
... = ∑ k, B i k • ∑ j, (A k j • a j) : _
... = 0 : finset.sum_eq_zero (λ k _, _),
{ simp only [matrix.adjugate_mul, pi.smul_apply, matrix.one_apply, mul_ite, ite_smul,
smul_eq_mul, mul_one, mul_zero, one_smul, zero_smul, finset.sum_ite_eq, finset.mem_univ,
if_true] },
{ simp only [matrix.mul_apply, finset.smul_sum, finset.sum_smul, smul_smul],
rw finset.sum_comm },
{ rw [A_smul, smul_zero] } },
-- In the rings of integers we have the desired inclusion.
have span_d : (submodule.span S ({algebra_map R S A.det} : set S)).restrict_scalars R ≤ M,
{ intros x hx,
rw submodule.restrict_scalars_mem at hx,
obtain ⟨x', rfl⟩ := submodule.mem_span_singleton.mp hx,
rw [smul_eq_mul, mul_comm, ← algebra.smul_def] at ⊢ hx,
rw [← submodule.quotient.mk_eq_zero, submodule.quotient.mk_smul],
obtain ⟨a', _, quot_x_eq⟩ := exists_sum (submodule.quotient.mk x'),
simp_rw [← quot_x_eq, finset.smul_sum, smul_comm A.det, d_smul, smul_zero,
finset.sum_const_zero] },
-- So now we lift everything to the fraction field.
refine top_le_iff.mp (calc ⊤ = (ideal.span {algebra_map R L A.det}).restrict_scalars K : _
... ≤ submodule.span K (algebra_map S L '' b) : _),
-- Because `det A ≠ 0`, we have `span L {det A} = ⊤`.
{ rw [eq_comm, submodule.restrict_scalars_eq_top_iff, ideal.span_singleton_eq_top],
refine is_unit.mk0 _ ((map_ne_zero_iff ((algebra_map R L)) hRL).mpr (
@ne_zero_of_map _ _ _ _ _ _ (ideal.quotient.mk p) _ _)),
haveI := ideal.quotient.nontrivial hp,
calc ideal.quotient.mk p (A.det)
= matrix.det ((ideal.quotient.mk p).map_matrix A) :
by rw [ring_hom.map_det, ring_hom.map_matrix_apply]
... = matrix.det ((ideal.quotient.mk p).map_matrix (A' - 1)) : rfl
... = matrix.det (λ i j, (ideal.quotient.mk p) (A' i j) -
(1 : matrix (fin n) (fin n) (R ⧸ p)) i j) : _
... = matrix.det (-1 : matrix (fin n) (fin n) (R ⧸ p)) : _
... = (-1 : R ⧸ p) ^ n : by rw [matrix.det_neg, fintype.card_fin, matrix.det_one, mul_one]
... ≠ 0 : is_unit.ne_zero (is_unit_one.neg.pow _),
{ refine congr_arg matrix.det (matrix.ext (λ i j, _)),
rw [map_sub, ring_hom.map_matrix_apply, map_one],
refl },
{ refine congr_arg matrix.det (matrix.ext (λ i j, _)),
rw [ideal.quotient.eq_zero_iff_mem.mpr (hA'p i j), zero_sub],
refl } },
-- And we conclude `L = span L {det A} ≤ span K b`, so `span K b` spans everything.
{ intros x hx,
rw [submodule.restrict_scalars_mem, is_scalar_tower.algebra_map_apply R S L] at hx,
refine is_fraction_ring.ideal_span_singleton_map_subset R _ hRL span_d hx,
haveI : no_zero_smul_divisors R L := no_zero_smul_divisors.of_algebra_map_injective hRL,
rw ← is_fraction_ring.is_algebraic_iff' R S,
intros x,
exact is_integral.is_algebraic _ (is_integral_of_noetherian infer_instance _) },
end
include hRK
variables (K L)
/-- If `p` is a maximal ideal of `R`, and `S` is the integral closure of `R` in `L`,
then the dimension `[S/pS : R/p]` is equal to `[Frac(S) : Frac(R)]`. -/
lemma finrank_quotient_map [is_domain R] [is_domain S] [is_dedekind_domain R]
[algebra K L] [algebra R L] [is_scalar_tower R K L] [is_scalar_tower R S L]
[is_integral_closure S R L]
[hp : p.is_maximal] [is_noetherian R S] :
finrank (R ⧸ p) (S ⧸ map (algebra_map R S) p) = finrank K L :=
begin
-- Choose an arbitrary basis `b` for `[S/pS : R/p]`.
-- We'll use the previous results to turn it into a basis on `[Frac(S) : Frac(R)]`.
letI : field (R ⧸ p) := ideal.quotient.field _,
let ι := module.free.choose_basis_index (R ⧸ p) (S ⧸ map (algebra_map R S) p),
let b : basis ι (R ⧸ p) (S ⧸ map (algebra_map R S) p) := module.free.choose_basis _ _,
-- Namely, choose a representative `b' i : S` for each `b i : S / pS`.
let b' : ι → S := λ i, (ideal.quotient.mk_surjective (b i)).some,
have b_eq_b' : ⇑ b = (submodule.mkq _).restrict_scalars R ∘ b' :=
funext (λ i, (ideal.quotient.mk_surjective (b i)).some_spec.symm),
-- We claim `b'` is a basis for `Frac(S)` over `Frac(R)` because it is linear independent
-- and spans the whole of `Frac(S)`.
let b'' : ι → L := algebra_map S L ∘ b',
have b''_li : linear_independent _ b'' := _,
have b''_sp : submodule.span _ (set.range b'') = ⊤ := _,
-- Since the two bases have the same index set, the spaces have the same dimension.
let c : basis ι K L := basis.mk b''_li b''_sp.ge,
rw [finrank_eq_card_basis b, finrank_eq_card_basis c],
-- It remains to show that the basis is indeed linear independent and spans the whole space.
{ rw set.range_comp,
refine finrank_quotient_map.span_eq_top p hp.ne_top _ (top_le_iff.mp _),
-- The nicest way to show `S ≤ span b' ⊔ pS` is by reducing both sides modulo pS.
-- However, this would imply distinguishing between `pS` as `S`-ideal,
-- and `pS` as `R`-submodule, since they have different (non-defeq) quotients.
-- Instead we'll lift `x mod pS ∈ span b` to `y ∈ span b'` for some `y - x ∈ pS`.
intros x hx,
have mem_span_b :
((submodule.mkq (map (algebra_map R S) p)) x :
S ⧸ map (algebra_map R S) p) ∈ submodule.span (R ⧸ p) (set.range b) := b.mem_span _,
rw [← @submodule.restrict_scalars_mem R, algebra.span_restrict_scalars_eq_span_of_surjective
(show function.surjective (algebra_map R (R ⧸ p)), from ideal.quotient.mk_surjective) _,
b_eq_b', set.range_comp, ← submodule.map_span]
at mem_span_b,
obtain ⟨y, y_mem, y_eq⟩ := submodule.mem_map.mp mem_span_b,
suffices : y + -(y - x) ∈ _, { simpa },
rw [linear_map.restrict_scalars_apply, submodule.mkq_apply, submodule.mkq_apply,
submodule.quotient.eq] at y_eq,
exact add_mem (submodule.mem_sup_left y_mem) (neg_mem $ submodule.mem_sup_right y_eq) },
{ have := b.linear_independent, rw b_eq_b' at this,
convert finrank_quotient_map.linear_independent_of_nontrivial K _
((algebra.linear_map S L).restrict_scalars R) _
((submodule.mkq _).restrict_scalars R)
this,
{ rw [quotient.algebra_map_eq, ideal.mk_ker],
exact hp.ne_top },
{ exact is_fraction_ring.injective S L } },
end
end finrank_quotient_map
section fact_le_comap
local notation `e` := ramification_idx f p P
/-- `R / p` has a canonical map to `S / (P ^ e)`, where `e` is the ramification index
of `P` over `p`. -/
noncomputable instance quotient.algebra_quotient_pow_ramification_idx :
algebra (R ⧸ p) (S ⧸ (P ^ e)) :=
quotient.algebra_quotient_of_le_comap (ideal.map_le_iff_le_comap.mp le_pow_ramification_idx)
@[simp] lemma quotient.algebra_map_quotient_pow_ramification_idx (x : R) :
algebra_map (R ⧸ p) (S ⧸ P ^ e) (ideal.quotient.mk p x) = ideal.quotient.mk _ (f x) :=
rfl
variables [hfp : ne_zero (ramification_idx f p P)]
include hfp
/-- If `P` lies over `p`, then `R / p` has a canonical map to `S / P`.
This can't be an instance since the map `f : R → S` is generally not inferrable.
-/
def quotient.algebra_quotient_of_ramification_idx_ne_zero :
algebra (R ⧸ p) (S ⧸ P) :=
quotient.algebra_quotient_of_le_comap (le_comap_of_ramification_idx_ne_zero hfp.out)
-- In this file, the value for `f` can be inferred.
local attribute [instance] ideal.quotient.algebra_quotient_of_ramification_idx_ne_zero
@[simp] lemma quotient.algebra_map_quotient_of_ramification_idx_ne_zero (x : R) :
algebra_map (R ⧸ p) (S ⧸ P) (ideal.quotient.mk p x) = ideal.quotient.mk _ (f x) :=
rfl
omit hfp
/-- The inclusion `(P^(i + 1) / P^e) ⊂ (P^i / P^e)`. -/
@[simps]
def pow_quot_succ_inclusion (i : ℕ) :
ideal.map (P^e)^.quotient.mk (P ^ (i + 1)) →ₗ[R ⧸ p] ideal.map (P^e)^.quotient.mk (P ^ i) :=
{ to_fun := λ x, ⟨x, ideal.map_mono (ideal.pow_le_pow i.le_succ) x.2⟩,
map_add' := λ x y, rfl,
map_smul' := λ c x, rfl }
lemma pow_quot_succ_inclusion_injective (i : ℕ) :
function.injective (pow_quot_succ_inclusion f p P i) :=
begin
rw [← linear_map.ker_eq_bot, linear_map.ker_eq_bot'],
rintro ⟨x, hx⟩ hx0,
rw subtype.ext_iff at hx0 ⊢,
rwa pow_quot_succ_inclusion_apply_coe at hx0
end
/-- `S ⧸ P` embeds into the quotient by `P^(i+1) ⧸ P^e` as a subspace of `P^i ⧸ P^e`.
See `quotient_to_quotient_range_pow_quot_succ` for this as a linear map,
and `quotient_range_pow_quot_succ_inclusion_equiv` for this as a linear equivalence.
-/
noncomputable def quotient_to_quotient_range_pow_quot_succ_aux {i : ℕ} {a : S} (a_mem : a ∈ P^i) :
S ⧸ P → ((P ^ i).map (P ^ e)^.quotient.mk ⧸ (pow_quot_succ_inclusion f p P i).range) :=
quotient.map' (λ (x : S), ⟨_, ideal.mem_map_of_mem _ (ideal.mul_mem_left _ x a_mem)⟩)
(λ x y h, begin
rw submodule.quotient_rel_r_def at ⊢ h,
simp only [_root_.map_mul, linear_map.mem_range],
refine ⟨⟨_, ideal.mem_map_of_mem _ (ideal.mul_mem_mul h a_mem)⟩, _⟩,
ext,
rw [pow_quot_succ_inclusion_apply_coe, subtype.coe_mk, submodule.coe_sub, subtype.coe_mk,
subtype.coe_mk, _root_.map_mul, map_sub, sub_mul]
end)
lemma quotient_to_quotient_range_pow_quot_succ_aux_mk {i : ℕ} {a : S} (a_mem : a ∈ P^i) (x : S) :
quotient_to_quotient_range_pow_quot_succ_aux f p P a_mem (submodule.quotient.mk x) =
submodule.quotient.mk ⟨_, ideal.mem_map_of_mem _ (ideal.mul_mem_left _ x a_mem)⟩ :=
by apply quotient.map'_mk'
include hfp
/-- `S ⧸ P` embeds into the quotient by `P^(i+1) ⧸ P^e` as a subspace of `P^i ⧸ P^e`. -/
noncomputable def quotient_to_quotient_range_pow_quot_succ {i : ℕ} {a : S} (a_mem : a ∈ P^i) :
S ⧸ P →ₗ[R ⧸ p] ((P ^ i).map (P ^ e)^.quotient.mk ⧸ (pow_quot_succ_inclusion f p P i).range) :=
{ to_fun := quotient_to_quotient_range_pow_quot_succ_aux f p P a_mem,
map_add' := begin
intros x y, refine quotient.induction_on' x (λ x, quotient.induction_on' y (λ y, _)),
simp only [submodule.quotient.mk'_eq_mk, ← submodule.quotient.mk_add,
quotient_to_quotient_range_pow_quot_succ_aux_mk, add_mul],
refine congr_arg submodule.quotient.mk _,
ext,
refl
end,
map_smul' := begin
intros x y, refine quotient.induction_on' x (λ x, quotient.induction_on' y (λ y, _)),
simp only [submodule.quotient.mk'_eq_mk, ← submodule.quotient.mk_add,
quotient_to_quotient_range_pow_quot_succ_aux_mk, ring_hom.id_apply],
refine congr_arg submodule.quotient.mk _,
ext,
simp only [subtype.coe_mk, _root_.map_mul, algebra.smul_def, submodule.coe_mk, mul_assoc,
ideal.quotient.mk_eq_mk, submodule.coe_smul_of_tower,
ideal.quotient.algebra_map_quotient_pow_ramification_idx]
end }
lemma quotient_to_quotient_range_pow_quot_succ_mk {i : ℕ} {a : S} (a_mem : a ∈ P^i) (x : S) :
quotient_to_quotient_range_pow_quot_succ f p P a_mem (submodule.quotient.mk x) =
submodule.quotient.mk ⟨_, ideal.mem_map_of_mem _ (ideal.mul_mem_left _ x a_mem)⟩ :=
quotient_to_quotient_range_pow_quot_succ_aux_mk f p P a_mem x
lemma quotient_to_quotient_range_pow_quot_succ_injective [is_domain S] [is_dedekind_domain S]
[P.is_prime] {i : ℕ} (hi : i < e) {a : S} (a_mem : a ∈ P^i) (a_not_mem : a ∉ P^(i + 1)) :
function.injective (quotient_to_quotient_range_pow_quot_succ f p P a_mem) :=
λ x, quotient.induction_on' x $ λ x y, quotient.induction_on' y $ λ y h,
begin
have Pe_le_Pi1 : P^e ≤ P^(i + 1) := ideal.pow_le_pow hi,
simp only [submodule.quotient.mk'_eq_mk, quotient_to_quotient_range_pow_quot_succ_mk,
submodule.quotient.eq, linear_map.mem_range, subtype.ext_iff, subtype.coe_mk,
submodule.coe_sub] at ⊢ h,
rcases h with ⟨⟨⟨z⟩, hz⟩, h⟩,
rw [submodule.quotient.quot_mk_eq_mk, ideal.quotient.mk_eq_mk, ideal.mem_quotient_iff_mem_sup,
sup_eq_left.mpr Pe_le_Pi1] at hz,
rw [pow_quot_succ_inclusion_apply_coe, subtype.coe_mk, submodule.quotient.quot_mk_eq_mk,
ideal.quotient.mk_eq_mk, ← map_sub, ideal.quotient.eq, ← sub_mul] at h,
exact (ideal.is_prime.mul_mem_pow _
((submodule.sub_mem_iff_right _ hz).mp (Pe_le_Pi1 h))).resolve_right a_not_mem,
end
lemma quotient_to_quotient_range_pow_quot_succ_surjective [is_domain S] [is_dedekind_domain S]
(hP0 : P ≠ ⊥) [hP : P.is_prime] {i : ℕ} (hi : i < e)
{a : S} (a_mem : a ∈ P^i) (a_not_mem : a ∉ P^(i + 1)) :
function.surjective (quotient_to_quotient_range_pow_quot_succ f p P a_mem) :=
begin
rintro ⟨⟨⟨x⟩, hx⟩⟩,
have Pe_le_Pi : P^e ≤ P^i := ideal.pow_le_pow hi.le,
have Pe_le_Pi1 : P^e ≤ P^(i + 1) := ideal.pow_le_pow hi,
rw [submodule.quotient.quot_mk_eq_mk, ideal.quotient.mk_eq_mk, ideal.mem_quotient_iff_mem_sup,
sup_eq_left.mpr Pe_le_Pi] at hx,
suffices hx' : x ∈ ideal.span {a} ⊔ P^(i+1),
{ obtain ⟨y', hy', z, hz, rfl⟩ := submodule.mem_sup.mp hx',
obtain ⟨y, rfl⟩ := ideal.mem_span_singleton.mp hy',
refine ⟨submodule.quotient.mk y, _⟩,
simp only [submodule.quotient.quot_mk_eq_mk, quotient_to_quotient_range_pow_quot_succ_mk,
submodule.quotient.eq, linear_map.mem_range, subtype.ext_iff, subtype.coe_mk,
submodule.coe_sub],
refine ⟨⟨_, ideal.mem_map_of_mem _ (submodule.neg_mem _ hz)⟩, _⟩,
rw [pow_quot_succ_inclusion_apply_coe, subtype.coe_mk, ideal.quotient.mk_eq_mk, map_add,
mul_comm y a, sub_add_cancel', map_neg] },
letI := classical.dec_eq (ideal S),
rw [sup_eq_prod_inf_factors _ (pow_ne_zero _ hP0), normalized_factors_pow,
normalized_factors_irreducible ((ideal.prime_iff_is_prime hP0).mpr hP).irreducible,
normalize_eq, multiset.nsmul_singleton, multiset.inter_repeat, multiset.prod_repeat],
rw [← submodule.span_singleton_le_iff_mem, ideal.submodule_span_eq] at a_mem a_not_mem,
rwa [ideal.count_normalized_factors_eq a_mem a_not_mem, min_eq_left i.le_succ],
{ intro ha,
rw ideal.span_singleton_eq_bot.mp ha at a_not_mem,
have := (P^(i+1)).zero_mem,
contradiction },
end
/-- Quotienting `P^i / P^e` by its subspace `P^(i+1) ⧸ P^e` is
`R ⧸ p`-linearly isomorphic to `S ⧸ P`. -/
noncomputable def quotient_range_pow_quot_succ_inclusion_equiv [is_domain S] [is_dedekind_domain S]
[P.is_prime] (hP : P ≠ ⊥) {i : ℕ} (hi : i < e) :
((P ^ i).map (P ^ e)^.quotient.mk ⧸ (pow_quot_succ_inclusion f p P i).range) ≃ₗ[R ⧸ p] S ⧸ P :=
begin
choose a a_mem a_not_mem using set_like.exists_of_lt
(ideal.strict_anti_pow P hP (ideal.is_prime.ne_top infer_instance) (le_refl i.succ)),
refine (linear_equiv.of_bijective _ _ _).symm,
{ exact quotient_to_quotient_range_pow_quot_succ f p P a_mem },
{ exact quotient_to_quotient_range_pow_quot_succ_injective f p P hi a_mem a_not_mem },
{ exact quotient_to_quotient_range_pow_quot_succ_surjective f p P hP hi a_mem a_not_mem }
end
/-- Since the inclusion `(P^(i + 1) / P^e) ⊂ (P^i / P^e)` has a kernel isomorphic to `P / S`,
`[P^i / P^e : R / p] = [P^(i+1) / P^e : R / p] + [P / S : R / p]` -/
lemma dim_pow_quot_aux [is_domain S] [is_dedekind_domain S] [p.is_maximal] [P.is_prime]
(hP0 : P ≠ ⊥) {i : ℕ} (hi : i < e) :
module.rank (R ⧸ p) (ideal.map (P^e)^.quotient.mk (P ^ i)) =
module.rank (R ⧸ p) (S ⧸ P) + module.rank (R ⧸ p) (ideal.map (P^e)^.quotient.mk (P ^ (i + 1))) :=
begin
letI : field (R ⧸ p) := ideal.quotient.field _,
rw [dim_eq_of_injective _ (pow_quot_succ_inclusion_injective f p P i),
(quotient_range_pow_quot_succ_inclusion_equiv f p P hP0 hi).symm.dim_eq],
exact (dim_quotient_add_dim (linear_map.range (pow_quot_succ_inclusion f p P i))).symm,
end
lemma dim_pow_quot [is_domain S] [is_dedekind_domain S] [p.is_maximal] [P.is_prime]
(hP0 : P ≠ ⊥) (i : ℕ) (hi : i ≤ e) :
module.rank (R ⧸ p) (ideal.map (P^e)^.quotient.mk (P ^ i)) =
(e - i) • module.rank (R ⧸ p) (S ⧸ P) :=
begin
refine @nat.decreasing_induction' _ i e (λ j lt_e le_j ih, _) hi _,
{ rw [dim_pow_quot_aux f p P _ lt_e, ih, ← succ_nsmul, nat.sub_succ, ← nat.succ_eq_add_one,
nat.succ_pred_eq_of_pos (nat.sub_pos_of_lt lt_e)],
assumption },
{ rw [nat.sub_self, zero_nsmul, map_quotient_self],
exact dim_bot (R ⧸ p) (S ⧸ (P^e)) }
end
omit hfp
/-- If `p` is a maximal ideal of `R`, `S` extends `R` and `P^e` lies over `p`,
then the dimension `[S/(P^e) : R/p]` is equal to `e * [S/P : R/p]`. -/
lemma dim_prime_pow_ramification_idx [is_domain S] [is_dedekind_domain S] [p.is_maximal]
[P.is_prime] (hP0 : P ≠ ⊥) (he : e ≠ 0) :
module.rank (R ⧸ p) (S ⧸ P^e) =
e • @module.rank (R ⧸ p) (S ⧸ P) _ _ (@algebra.to_module _ _ _ _ $
@@quotient.algebra_quotient_of_ramification_idx_ne_zero _ _ _ _ _ ⟨he⟩) :=
begin
letI : ne_zero e := ⟨he⟩,
have := dim_pow_quot f p P hP0 0 (nat.zero_le e),
rw [pow_zero, nat.sub_zero, ideal.one_eq_top, ideal.map_top] at this,
exact (dim_top (R ⧸ p) _).symm.trans this
end
/-- If `p` is a maximal ideal of `R`, `S` extends `R` and `P^e` lies over `p`,
then the dimension `[S/(P^e) : R/p]`, as a natural number, is equal to `e * [S/P : R/p]`. -/
lemma finrank_prime_pow_ramification_idx [is_domain S] [is_dedekind_domain S]
(hP0 : P ≠ ⊥) [p.is_maximal] [P.is_prime] (he : e ≠ 0) :
finrank (R ⧸ p) (S ⧸ P^e) =
e * @finrank (R ⧸ p) (S ⧸ P) _ _ (@algebra.to_module _ _ _ _ $
@@quotient.algebra_quotient_of_ramification_idx_ne_zero _ _ _ _ _ ⟨he⟩) :=
begin
letI : ne_zero e := ⟨he⟩,
letI : algebra (R ⧸ p) (S ⧸ P) := quotient.algebra_quotient_of_ramification_idx_ne_zero f p P,
letI := ideal.quotient.field p,
have hdim := dim_prime_pow_ramification_idx _ _ _ hP0 he,
by_cases hP : finite_dimensional (R ⧸ p) (S ⧸ P),
{ haveI := hP,
haveI := (finite_dimensional_iff_of_rank_eq_nsmul he hdim).mpr hP,
refine cardinal.nat_cast_injective _,
rw [finrank_eq_dim, nat.cast_mul, finrank_eq_dim, hdim, nsmul_eq_mul] },
have hPe := mt (finite_dimensional_iff_of_rank_eq_nsmul he hdim).mp hP,
simp only [finrank_of_infinite_dimensional hP, finrank_of_infinite_dimensional hPe, mul_zero],
end
end fact_le_comap
section factors_map
open_locale classical
/-! ## Properties of the factors of `p.map (algebra_map R S)` -/
variables [is_domain S] [is_dedekind_domain S] [algebra R S]
lemma factors.ne_bot (P : (factors (map (algebra_map R S) p)).to_finset) :
(P : ideal S) ≠ ⊥ :=
(prime_of_factor _ (multiset.mem_to_finset.mp P.2)).ne_zero
instance factors.is_prime (P : (factors (map (algebra_map R S) p)).to_finset) :
is_prime (P : ideal S) :=
ideal.is_prime_of_prime (prime_of_factor _ (multiset.mem_to_finset.mp P.2))
lemma factors.ramification_idx_ne_zero (P : (factors (map (algebra_map R S) p)).to_finset) :
ramification_idx (algebra_map R S) p P ≠ 0 :=
is_dedekind_domain.ramification_idx_ne_zero
(ne_zero_of_mem_factors (multiset.mem_to_finset.mp P.2))
(factors.is_prime p P)
(ideal.le_of_dvd (dvd_of_mem_factors (multiset.mem_to_finset.mp P.2)))
instance factors.fact_ramification_idx_ne_zero (P : (factors (map (algebra_map R S) p)).to_finset) :
ne_zero (ramification_idx (algebra_map R S) p P) :=
⟨factors.ramification_idx_ne_zero p P⟩
local attribute [instance] quotient.algebra_quotient_of_ramification_idx_ne_zero
instance factors.is_scalar_tower
(P : (factors (map (algebra_map R S) p)).to_finset) :
is_scalar_tower R (R ⧸ p) (S ⧸ (P : ideal S)) :=
is_scalar_tower.of_algebra_map_eq (λ x, by simp)
local attribute [instance] ideal.quotient.field
lemma factors.finrank_pow_ramification_idx [p.is_maximal]
(P : (factors (map (algebra_map R S) p)).to_finset) :
finrank (R ⧸ p) (S ⧸ (P : ideal S) ^ ramification_idx (algebra_map R S) p P) =
ramification_idx (algebra_map R S) p P * inertia_deg (algebra_map R S) p P :=
begin
rw [finrank_prime_pow_ramification_idx, inertia_deg_algebra_map],
exact factors.ne_bot p P,
end
instance factors.finite_dimensional_quotient [is_noetherian R S] [p.is_maximal]
(P : (factors (map (algebra_map R S) p)).to_finset) :
finite_dimensional (R ⧸ p) (S ⧸ (P : ideal S)) :=
is_noetherian.iff_fg.mp $
is_noetherian_of_tower R $
is_noetherian_of_surjective S (ideal.quotient.mkₐ _ _).to_linear_map $
linear_map.range_eq_top.mpr ideal.quotient.mk_surjective
lemma factors.inertia_deg_ne_zero [is_noetherian R S] [p.is_maximal]
(P : (factors (map (algebra_map R S) p)).to_finset) :
inertia_deg (algebra_map R S) p P ≠ 0 :=
by { rw inertia_deg_algebra_map, exact (finite_dimensional.finrank_pos_iff.mpr infer_instance).ne' }
instance factors.finite_dimensional_quotient_pow [is_noetherian R S] [p.is_maximal]
(P : (factors (map (algebra_map R S) p)).to_finset) :
finite_dimensional (R ⧸ p) (S ⧸ (P : ideal S) ^ ramification_idx (algebra_map R S) p P) :=
begin
refine finite_dimensional.finite_dimensional_of_finrank _,
rw [pos_iff_ne_zero, factors.finrank_pow_ramification_idx],
exact mul_ne_zero (factors.ramification_idx_ne_zero p P) (factors.inertia_deg_ne_zero p P)
end
universes w
/-- **Chinese remainder theorem** for a ring of integers: if the prime ideal `p : ideal R`
factors in `S` as `∏ i, P i ^ e i`, then `S ⧸ I` factors as `Π i, R ⧸ (P i ^ e i)`. -/
noncomputable def factors.pi_quotient_equiv
(p : ideal R) (hp : map (algebra_map R S) p ≠ ⊥) :
(S ⧸ map (algebra_map R S) p) ≃+* Π (P : (factors (map (algebra_map R S) p)).to_finset),
S ⧸ ((P : ideal S) ^ ramification_idx (algebra_map R S) p P) :=
(is_dedekind_domain.quotient_equiv_pi_factors hp).trans $
(@ring_equiv.Pi_congr_right (factors (map (algebra_map R S) p)).to_finset
(λ P, S ⧸ (P : ideal S) ^ (factors (map (algebra_map R S) p)).count P)
(λ P, S ⧸ (P : ideal S) ^ ramification_idx (algebra_map R S) p P) _ _
(λ P : (factors (map (algebra_map R S) p)).to_finset, ideal.quot_equiv_of_eq $
by rw is_dedekind_domain.ramification_idx_eq_factors_count hp
(factors.is_prime p P) (factors.ne_bot p P)))
@[simp] lemma factors.pi_quotient_equiv_mk
(p : ideal R) (hp : map (algebra_map R S) p ≠ ⊥) (x : S) :
factors.pi_quotient_equiv p hp (ideal.quotient.mk _ x) = λ P, ideal.quotient.mk _ x :=
rfl
@[simp] lemma factors.pi_quotient_equiv_map
(p : ideal R) (hp : map (algebra_map R S) p ≠ ⊥) (x : R) :
factors.pi_quotient_equiv p hp (algebra_map _ _ x) =
λ P, ideal.quotient.mk _ (algebra_map _ _ x) :=
rfl
/-- **Chinese remainder theorem** for a ring of integers: if the prime ideal `p : ideal R`
factors in `S` as `∏ i, P i ^ e i`,
then `S ⧸ I` factors `R ⧸ I`-linearly as `Π i, R ⧸ (P i ^ e i)`. -/
noncomputable def factors.pi_quotient_linear_equiv
(p : ideal R) (hp : map (algebra_map R S) p ≠ ⊥) :
(S ⧸ map (algebra_map R S) p) ≃ₗ[R ⧸ p] Π (P : (factors (map (algebra_map R S) p)).to_finset),
S ⧸ ((P : ideal S) ^ ramification_idx (algebra_map R S) p P) :=
{ map_smul' := begin
rintro ⟨c⟩ ⟨x⟩, ext P,
simp only [ideal.quotient.mk_algebra_map,
factors.pi_quotient_equiv_mk, factors.pi_quotient_equiv_map, submodule.quotient.quot_mk_eq_mk,
pi.algebra_map_apply, ring_equiv.to_fun_eq_coe, pi.mul_apply,
ideal.quotient.algebra_map_quotient_map_quotient, ideal.quotient.mk_eq_mk, algebra.smul_def,
_root_.map_mul, ring_hom_comp_triple.comp_apply],
congr
end,
.. factors.pi_quotient_equiv p hp }
open_locale big_operators
/-- The **fundamental identity** of ramification index `e` and inertia degree `f`:
for `P` ranging over the primes lying over `p`, `∑ P, e P * f P = [Frac(S) : Frac(R)]`;
here `S` is a finite `R`-module (and thus `Frac(S) : Frac(R)` is a finite extension) and `p`
is maximal.
-/
theorem sum_ramification_inertia (K L : Type*) [field K] [field L]
[is_domain R] [is_dedekind_domain R]
[algebra R K] [is_fraction_ring R K] [algebra S L] [is_fraction_ring S L]
[algebra K L] [algebra R L] [is_scalar_tower R S L] [is_scalar_tower R K L]
[is_noetherian R S] [is_integral_closure S R L] [p.is_maximal] (hp0 : p ≠ ⊥) :
∑ P in (factors (map (algebra_map R S) p)).to_finset,
ramification_idx (algebra_map R S) p P * inertia_deg (algebra_map R S) p P =
finrank K L :=
begin
set e := ramification_idx (algebra_map R S) p,
set f := inertia_deg (algebra_map R S) p,
have inj_RL : function.injective (algebra_map R L),
{ rw [is_scalar_tower.algebra_map_eq R K L, ring_hom.coe_comp],
exact (ring_hom.injective _).comp (is_fraction_ring.injective R K) },
have inj_RS : function.injective (algebra_map R S),
{ refine function.injective.of_comp (show function.injective (algebra_map S L ∘ _), from _),
rw [← ring_hom.coe_comp, ← is_scalar_tower.algebra_map_eq],
exact inj_RL },
calc ∑ P in (factors (map (algebra_map R S) p)).to_finset, e P * f P
= ∑ P in (factors (map (algebra_map R S) p)).to_finset.attach,
finrank (R ⧸ p) (S ⧸ (P : ideal S)^(e P)) : _
... = finrank (R ⧸ p) (Π P : (factors (map (algebra_map R S) p)).to_finset,
(S ⧸ (P : ideal S)^(e P))) :
(module.free.finrank_pi_fintype (R ⧸ p)).symm
... = finrank (R ⧸ p) (S ⧸ map (algebra_map R S) p) : _
... = finrank K L : _,
{ rw ← finset.sum_attach,
refine finset.sum_congr rfl (λ P _, _),
rw factors.finrank_pow_ramification_idx },
{ refine linear_equiv.finrank_eq (factors.pi_quotient_linear_equiv p _).symm,
rwa [ne.def, ideal.map_eq_bot_iff_le_ker, (ring_hom.injective_iff_ker_eq_bot _).mp inj_RS,
le_bot_iff] },
{ exact finrank_quotient_map p K L },
end
end factors_map
end ideal
|
a064f7842aba4be5b3269ed117cb7e23d1b73da0 | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/data/nat/factorial/basic.lean | 972a734c9b4adfabe7de7d1aed5ecc4e7109e476 | [
"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 | 14,930 | 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, Floris van Doorn, Yaël Dillies
-/
import data.nat.basic
import data.nat.pow
/-!
# Factorial and variants
This file defines the factorial, along with the ascending and descending variants.
## Main declarations
* `factorial`: The factorial.
* `asc_factorial`: The ascending factorial. Note that it runs from `n + 1` to `n + k` and *not*
from`n`
to `n + k - 1`. We might want to change that in the future.
* `desc_factorial`: The descending factorial. It runs from `n - k` to `n`.
-/
namespace nat
/-- `nat.factorial n` is the factorial of `n`. -/
@[simp] def factorial : ℕ → ℕ
| 0 := 1
| (succ n) := succ n * factorial n
localized "notation n `!`:10000 := nat.factorial n" in nat
section factorial
variables {m n : ℕ}
@[simp] theorem factorial_zero : 0! = 1 := rfl
@[simp] theorem factorial_succ (n : ℕ) : n.succ! = (n + 1) * n! := rfl
@[simp] theorem factorial_one : 1! = 1 := rfl
@[simp] theorem factorial_two : 2! = 2 := rfl
theorem mul_factorial_pred (hn : 0 < n) : n * (n - 1)! = n! :=
nat.sub_add_cancel hn ▸ rfl
theorem factorial_pos : ∀ n, 0 < n!
| 0 := zero_lt_one
| (succ n) := mul_pos (succ_pos _) (factorial_pos n)
theorem factorial_ne_zero (n : ℕ) : n! ≠ 0 := ne_of_gt (factorial_pos _)
theorem factorial_dvd_factorial {m n} (h : m ≤ n) : m! ∣ n! :=
begin
induction n with n IH; simp,
{ have := nat.eq_zero_of_le_zero h, subst m, simp },
obtain he | hl := h.eq_or_lt,
{ subst m, simp },
exact (IH (le_of_lt_succ hl)).mul_left _,
end
theorem dvd_factorial : ∀ {m n}, 0 < m → m ≤ n → m ∣ n!
| (succ m) n _ h := dvd_of_mul_right_dvd (factorial_dvd_factorial h)
@[mono] theorem factorial_le {m n} (h : m ≤ n) : m! ≤ n! :=
le_of_dvd (factorial_pos _) (factorial_dvd_factorial h)
lemma factorial_mul_pow_le_factorial : ∀ {m n : ℕ}, m! * m.succ ^ n ≤ (m + n)!
| m 0 := by simp
| m (n+1) :=
by rw [← add_assoc, nat.factorial_succ, mul_comm (nat.succ _), pow_succ', ← mul_assoc];
exact mul_le_mul factorial_mul_pow_le_factorial
(nat.succ_le_succ (nat.le_add_right _ _)) (nat.zero_le _) (nat.zero_le _)
lemma monotone_factorial : monotone factorial := λ n m, factorial_le
lemma factorial_lt (hn : 0 < n) : n! < m! ↔ n < m :=
begin
split; intro h,
{ rw [← not_le], intro hmn, apply not_le_of_lt h (factorial_le hmn) },
have : ∀ n, 0 < n → n! < n.succ!,
{ intros k hk, rw [factorial_succ, succ_mul, lt_add_iff_pos_left],
apply mul_pos hk (factorial_pos k) },
induction h with k hnk generalizing hn,
{ exact this _ hn, },
refine lt_trans (h_ih hn) (this _ _),
exact lt_trans hn (lt_of_succ_le hnk),
end
lemma one_lt_factorial : 1 < n! ↔ 1 < n :=
by { convert factorial_lt _, refl, exact one_pos }
lemma factorial_eq_one : n! = 1 ↔ n ≤ 1 :=
begin
split; intro h,
{ rw [← not_lt, ← one_lt_factorial, h],
apply lt_irrefl },
cases h with h h, refl, cases h, refl,
end
lemma factorial_inj (hn : 1 < n!) : n! = m! ↔ n = m :=
begin
split; intro h,
{ obtain hnm | hnm | hnm := lt_trichotomy n m,
{ exfalso, rw [← factorial_lt, h] at hnm, exact lt_irrefl _ hnm,
rw [one_lt_factorial] at hn, exact lt_trans one_pos hn },
{ exact hnm },
exfalso,
rw [h, one_lt_factorial] at hn,
rw [←factorial_lt (lt_trans one_pos hn), h] at hnm, exact lt_irrefl _ hnm, },
{ rw h },
end
lemma self_le_factorial : ∀ n : ℕ, n ≤ n!
| 0 := zero_le_one
| (k + 1) := le_mul_of_one_le_right k.zero_lt_succ.le (nat.one_le_of_lt $ nat.factorial_pos _)
lemma lt_factorial_self {n : ℕ} (hi : 3 ≤ n) : n < n! :=
begin
rw [← succ_pred_eq_of_pos ((zero_lt_two.trans (lt.base 2)).trans_le hi), factorial_succ],
exact lt_mul_of_one_lt_right ((pred n).succ_pos) ((one_lt_two.trans_le
(le_pred_of_lt (succ_le_iff.mp hi))).trans_le (self_le_factorial _)),
end
lemma add_factorial_succ_lt_factorial_add_succ {i : ℕ} (n : ℕ) (hi : 2 ≤ i) :
i + (n + 1)! < (i + n + 1)! :=
begin
rw [factorial_succ (i + _), add_mul, one_mul],
have : i ≤ i + n := le.intro rfl,
exact add_lt_add_of_lt_of_le (this.trans_lt ((lt_mul_iff_one_lt_right (zero_lt_two.trans_le
(hi.trans this))).mpr (lt_iff_le_and_ne.mpr ⟨(i + n).factorial_pos, λ g,
nat.not_succ_le_self 1 ((hi.trans this).trans (factorial_eq_one.mp g.symm))⟩))) (factorial_le
((le_of_eq (add_comm n 1)).trans ((add_le_add_iff_right n).mpr (one_le_two.trans hi)))),
end
lemma add_factorial_lt_factorial_add {i n : ℕ} (hi : 2 ≤ i) (hn : 1 ≤ n) :
i + n! < (i + n)! :=
begin
cases hn,
{ rw factorial_one,
exact lt_factorial_self (succ_le_succ hi) },
exact add_factorial_succ_lt_factorial_add_succ _ hi,
end
lemma add_factorial_succ_le_factorial_add_succ (i : ℕ) (n : ℕ) :
i + (n + 1)! ≤ (i + (n + 1))! :=
begin
obtain i2 | (_ | ⟨_, i0⟩) := le_or_lt 2 i,
{ exact (n.add_factorial_succ_lt_factorial_add_succ i2).le },
{ change 1 + (n + 1)! ≤ (1 + n + 1) * (1 + n)!,
rw [add_mul, one_mul, add_comm 1 n],
exact (add_le_add_iff_right _).mpr (one_le_mul (nat.le_add_left 1 n) (n + 1).factorial_pos) },
rw [nat.le_zero_iff.mp (nat.succ_le_succ_iff.mp i0), zero_add, zero_add]
end
lemma add_factorial_le_factorial_add (i : ℕ) {n : ℕ} (n1 : 1 ≤ n) :
i + n! ≤ (i + n)! :=
begin
cases n1 with h,
{ exact self_le_factorial _ },
exact add_factorial_succ_le_factorial_add_succ i h,
end
lemma factorial_mul_pow_sub_le_factorial {n m : ℕ} (hnm : n ≤ m) : n! * n ^ (m - n) ≤ m! :=
begin
suffices : n! * (n + 1) ^ (m - n) ≤ m!,
{ apply trans _ this,
rw mul_le_mul_left,
apply pow_le_pow_of_le_left (zero_le n) (le_succ n),
exact factorial_pos n,},
convert nat.factorial_mul_pow_le_factorial,
exact (nat.add_sub_of_le hnm).symm,
end
end factorial
/-! ### Ascending and descending factorials -/
section asc_factorial
/-- `n.asc_factorial k = (n + k)! / n!` (as seen in `nat.asc_factorial_eq_div`), but implemented
recursively to allow for "quick" computation when using `norm_num`. This is closely related to
`pochhammer`, but much less general. -/
def asc_factorial (n : ℕ) : ℕ → ℕ
| 0 := 1
| (k + 1) := (n + k + 1) * asc_factorial k
@[simp] lemma asc_factorial_zero (n : ℕ) : n.asc_factorial 0 = 1 := rfl
@[simp] lemma zero_asc_factorial (k : ℕ) : (0 : ℕ).asc_factorial k = k! :=
begin
induction k with t ht, refl,
unfold asc_factorial, rw [ht, zero_add, nat.factorial_succ],
end
lemma asc_factorial_succ {n k : ℕ} : n.asc_factorial k.succ = (n + k + 1) * n.asc_factorial k := rfl
lemma succ_asc_factorial (n : ℕ) :
∀ k, (n + 1) * n.succ.asc_factorial k = (n + k + 1) * n.asc_factorial k
| 0 := by rw [add_zero, asc_factorial_zero, asc_factorial_zero]
| (k + 1) := by rw [asc_factorial, mul_left_comm, succ_asc_factorial, asc_factorial, succ_add,
←add_assoc]
/-- `n.asc_factorial k = (n + k)! / n!` but without ℕ-division. See `nat.asc_factorial_eq_div` for
the version with ℕ-division. -/
theorem factorial_mul_asc_factorial (n : ℕ) : ∀ k, n! * n.asc_factorial k = (n + k)!
| 0 := by rw [asc_factorial, add_zero, mul_one]
| (k + 1) := by rw [asc_factorial_succ, mul_left_comm, factorial_mul_asc_factorial, ← add_assoc,
factorial]
/-- Avoid in favor of `nat.factorial_mul_asc_factorial` if you can. ℕ-division isn't worth it. -/
lemma asc_factorial_eq_div (n k : ℕ) : n.asc_factorial k = (n + k)! / n! :=
begin
apply mul_left_cancel₀ (factorial_ne_zero n),
rw factorial_mul_asc_factorial,
exact (nat.mul_div_cancel' $ factorial_dvd_factorial $ le.intro rfl).symm
end
lemma asc_factorial_of_sub {n k : ℕ} (h : k < n) :
(n - k) * (n - k).asc_factorial k = (n - (k + 1)).asc_factorial (k + 1) :=
begin
set t := n - k.succ with ht,
suffices h' : n - k = t.succ, by rw [←ht, h', succ_asc_factorial, asc_factorial_succ],
rw [ht, succ_eq_add_one, ←sub_sub_assoc (succ_le_of_lt h) (succ_pos _), succ_sub_one],
end
lemma pow_succ_le_asc_factorial (n : ℕ) : ∀ (k : ℕ), (n + 1)^k ≤ n.asc_factorial k
| 0 := by rw [asc_factorial_zero, pow_zero]
| (k + 1) := begin
rw pow_succ,
exact nat.mul_le_mul (nat.add_le_add_right le_self_add _) (pow_succ_le_asc_factorial k),
end
lemma pow_lt_asc_factorial' (n k : ℕ) : (n + 1)^(k + 2) < n.asc_factorial (k + 2) :=
begin
rw pow_succ,
exact nat.mul_lt_mul (nat.add_lt_add_right (nat.lt_add_of_pos_right succ_pos') 1)
(pow_succ_le_asc_factorial n _) (pow_pos succ_pos' _),
end
lemma pow_lt_asc_factorial (n : ℕ) : ∀ {k : ℕ}, 2 ≤ k → (n + 1)^k < n.asc_factorial k
| 0 := by rintro ⟨⟩
| 1 := by rintro (_ | ⟨_, ⟨⟩⟩)
| (k + 2) := λ _, pow_lt_asc_factorial' n k
lemma asc_factorial_le_pow_add (n : ℕ) : ∀ (k : ℕ), n.asc_factorial k ≤ (n + k)^k
| 0 := by rw [asc_factorial_zero, pow_zero]
| (k + 1) := begin
rw [asc_factorial_succ, pow_succ],
exact nat.mul_le_mul_of_nonneg_left ((asc_factorial_le_pow_add k).trans (nat.pow_le_pow_of_le_left
(le_succ _) _)),
end
lemma asc_factorial_lt_pow_add (n : ℕ) : ∀ {k : ℕ}, 2 ≤ k → n.asc_factorial k < (n + k)^k
| 0 := by rintro ⟨⟩
| 1 := by rintro (_ | ⟨_, ⟨⟩⟩)
| (k + 2) := λ _, begin
rw [asc_factorial_succ, pow_succ],
refine nat.mul_lt_mul' (le_refl _) ((asc_factorial_le_pow_add n _).trans_lt
(pow_lt_pow_of_lt_left (lt_add_one _) (succ_pos _))) (succ_pos _),
end
lemma asc_factorial_pos (n k : ℕ) : 0 < n.asc_factorial k :=
(pow_pos (succ_pos n) k).trans_le (pow_succ_le_asc_factorial n k)
end asc_factorial
section desc_factorial
/-- `n.desc_factorial k = n! / (n - k)!` (as seen in `nat.desc_factorial_eq_div`), but
implemented recursively to allow for "quick" computation when using `norm_num`. This is closely
related to `pochhammer`, but much less general. -/
def desc_factorial (n : ℕ) : ℕ → ℕ
| 0 := 1
| (k + 1) := (n - k) * desc_factorial k
@[simp] lemma desc_factorial_zero (n : ℕ) : n.desc_factorial 0 = 1 := rfl
@[simp] lemma desc_factorial_succ (n k : ℕ) :
n.desc_factorial k.succ = (n - k) * n.desc_factorial k := rfl
lemma zero_desc_factorial_succ (k : ℕ) :
(0 : ℕ).desc_factorial k.succ = 0 :=
by rw [desc_factorial_succ, nat.zero_sub, zero_mul]
@[simp] lemma desc_factorial_one (n : ℕ) :
n.desc_factorial 1 = n :=
by rw [desc_factorial_succ, desc_factorial_zero, mul_one, nat.sub_zero]
@[simp] lemma succ_desc_factorial_succ (n : ℕ) :
∀ k : ℕ, (n + 1).desc_factorial (k + 1) = (n + 1) * n.desc_factorial k
| 0 := by rw [desc_factorial_zero, desc_factorial_one, mul_one]
| (succ k) := by rw [desc_factorial_succ, succ_desc_factorial_succ, desc_factorial_succ,
succ_sub_succ, mul_left_comm]
lemma succ_desc_factorial (n : ℕ) :
∀ k, (n + 1 - k) * (n + 1).desc_factorial k = (n + 1) * n.desc_factorial k
| 0 := by rw [nat.sub_zero, desc_factorial_zero, desc_factorial_zero]
| (k + 1) := by rw [desc_factorial, succ_desc_factorial, desc_factorial_succ, succ_sub_succ,
mul_left_comm]
lemma desc_factorial_self : ∀ n : ℕ, n.desc_factorial n = n!
| 0 := by rw [desc_factorial_zero, factorial_zero]
| (succ n) := by rw [succ_desc_factorial_succ, desc_factorial_self, factorial_succ]
@[simp] lemma desc_factorial_eq_zero_iff_lt {n : ℕ} : ∀ {k : ℕ}, n.desc_factorial k = 0 ↔ n < k
| 0 := by simp only [desc_factorial_zero, nat.one_ne_zero, nat.not_lt_zero]
| (succ k) := begin
rw [desc_factorial_succ, mul_eq_zero, desc_factorial_eq_zero_iff_lt, lt_succ_iff,
nat.sub_eq_zero_iff_le, lt_iff_le_and_ne, or_iff_left_iff_imp, and_imp],
exact λ h _, h,
end
alias nat.desc_factorial_eq_zero_iff_lt ↔ _ nat.desc_factorial_of_lt
lemma add_desc_factorial_eq_asc_factorial (n : ℕ) :
∀ k : ℕ, (n + k).desc_factorial k = n.asc_factorial k
| 0 := by rw [asc_factorial_zero, desc_factorial_zero]
| (succ k) := by rw [nat.add_succ, succ_desc_factorial_succ, asc_factorial_succ,
add_desc_factorial_eq_asc_factorial]
/-- `n.desc_factorial k = n! / (n - k)!` but without ℕ-division. See `nat.desc_factorial_eq_div`
for the version using ℕ-division. -/
theorem factorial_mul_desc_factorial : ∀ {n k : ℕ}, k ≤ n → (n - k)! * n.desc_factorial k = n!
| n 0 := λ _, by rw [desc_factorial_zero, mul_one, nat.sub_zero]
| 0 (succ k) := λ h, by { exfalso, exact not_succ_le_zero k h }
| (succ n) (succ k) := λ h, by rw [succ_desc_factorial_succ, succ_sub_succ, ←mul_assoc,
mul_comm (n - k)!, mul_assoc, factorial_mul_desc_factorial (nat.succ_le_succ_iff.1 h),
factorial_succ]
/-- Avoid in favor of `nat.factorial_mul_desc_factorial` if you can. ℕ-division isn't worth it. -/
lemma desc_factorial_eq_div {n k : ℕ} (h : k ≤ n) : n.desc_factorial k = n! / (n - k)! :=
begin
apply mul_left_cancel₀ (factorial_ne_zero (n - k)),
rw factorial_mul_desc_factorial h,
exact (nat.mul_div_cancel' $ factorial_dvd_factorial $ nat.sub_le n k).symm,
end
lemma pow_sub_le_desc_factorial (n : ℕ) : ∀ (k : ℕ), (n + 1 - k)^k ≤ n.desc_factorial k
| 0 := by rw [desc_factorial_zero, pow_zero]
| (k + 1) := begin
rw [desc_factorial_succ, pow_succ, succ_sub_succ],
exact nat.mul_le_mul_of_nonneg_left (le_trans (nat.pow_le_pow_of_le_left
(nat.sub_le_sub_right (le_succ _) _) k) (pow_sub_le_desc_factorial k)),
end
lemma pow_sub_lt_desc_factorial' {n : ℕ} :
∀ {k : ℕ}, k + 2 ≤ n → (n - (k + 1))^(k + 2) < n.desc_factorial (k + 2)
| 0 := λ h, begin
rw [desc_factorial_succ, pow_succ, pow_one, desc_factorial_one],
exact nat.mul_lt_mul_of_pos_left (sub_lt_self' (lt_of_lt_of_le zero_lt_two h) zero_lt_one)
(nat.sub_pos_of_lt h),
end
| (k + 1) := λ h, begin
rw [desc_factorial_succ, pow_succ],
refine nat.mul_lt_mul_of_pos_left ((nat.pow_le_pow_of_le_left (nat.sub_le_sub_right
(le_succ n) _) _).trans_lt _) (nat.sub_pos_of_lt h),
rw succ_sub_succ,
exact (pow_sub_lt_desc_factorial' ((le_succ _).trans h)),
end
lemma pow_sub_lt_desc_factorial {n : ℕ} :
∀ {k : ℕ}, 2 ≤ k → k ≤ n → (n + 1 - k)^k < n.desc_factorial k
| 0 := by rintro ⟨⟩
| 1 := by rintro (_ | ⟨_, ⟨⟩⟩)
| (k + 2) := λ _ h, by { rw succ_sub_succ, exact pow_sub_lt_desc_factorial' h }
lemma desc_factorial_le_pow (n : ℕ) : ∀ (k : ℕ), n.desc_factorial k ≤ n^k
| 0 := by rw [desc_factorial_zero, pow_zero]
| (k + 1) := begin
rw [desc_factorial_succ, pow_succ],
exact nat.mul_le_mul (nat.sub_le _ _) (desc_factorial_le_pow k),
end
lemma desc_factorial_lt_pow {n : ℕ} (hn : 1 ≤ n) : ∀ {k : ℕ}, 2 ≤ k → n.desc_factorial k < n^k
| 0 := by rintro ⟨⟩
| 1 := by rintro (_ | ⟨_, ⟨⟩⟩)
| (k + 2) := λ _, begin
rw [desc_factorial_succ, pow_succ', mul_comm],
exact nat.mul_lt_mul' (desc_factorial_le_pow _ _) (sub_lt_self' hn k.zero_lt_succ)
(pow_pos hn _),
end
end desc_factorial
end nat
|
d47bc747c2b7fd05a0e5fd26c650d74860002c6e | 9dd3f3912f7321eb58ee9aa8f21778ad6221f87c | /tests/lean/hinst_lemmas1.lean | e5c637ddb015c46093b257c94f037cfa0d5c6401 | [
"Apache-2.0"
] | permissive | bre7k30/lean | de893411bcfa7b3c5572e61b9e1c52951b310aa4 | 5a924699d076dab1bd5af23a8f910b433e598d7a | refs/heads/master | 1,610,900,145,817 | 1,488,006,845,000 | 1,488,006,845,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 479 | lean | axiom foo1 : ∀ (a b c : nat), b > a → b < c → a < c
axiom foo2 : ∀ (a b c : nat), b > a → b < c → a < c
axiom foo3 : ∀ (a b c : nat), b > a → b < c + c → a < c + c
run_command
do
hs ← return $ hinst_lemmas.mk,
h₁ ← hinst_lemma.mk_from_decl `foo1,
h₂ ← hinst_lemma.mk_from_decl_core tactic.transparency.none `foo2 ff,
h₃ ← hinst_lemma.mk_from_decl `foo3,
hs ← return $ ((hs^.add h₁)^.add h₂)^.add h₃,
hs^.pp >>= tactic.trace
|
f48b8d2a69e69d5bb445fb0fe0abcefd7b34c39c | 2a70b774d16dbdf5a533432ee0ebab6838df0948 | /_target/deps/mathlib/src/order/complete_lattice.lean | 33637a49c576be2dc70487ab2c69051ac1a77e6b | [
"Apache-2.0"
] | permissive | hjvromen/lewis | 40b035973df7c77ebf927afab7878c76d05ff758 | 105b675f73630f028ad5d890897a51b3c1146fb0 | refs/heads/master | 1,677,944,636,343 | 1,676,555,301,000 | 1,676,555,301,000 | 327,553,599 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 39,628 | 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. If other fields are known explicitly, they should be
provided; for example, if `inf` is known explicitly, construct the `complete_lattice`
instance as
```
instance : complete_lattice my_T :=
{ inf := better_inf,
le_inf := ...,
inf_le_right := ...,
inf_le_left := ...
-- don't care to fix sup, Sup, bot, top
..complete_lattice_of_Inf my_T _ }
```
-/
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. If other fields are known explicitly, they should be
provided; for example, if `inf` is known explicitly, construct the `complete_lattice`
instance as
```
instance : complete_lattice my_T :=
{ inf := better_inf,
le_inf := ...,
inf_le_right := ...,
inf_le_left := ...
-- don't care to fix sup, Inf, bot, top
..complete_lattice_of_Sup my_T _ }
```
-/
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 α, 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.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)
lemma eq_singleton_top_of_Inf_eq_top_of_nonempty {s : set α}
(h_inf : Inf s = ⊤) (hne : s.nonempty) : s = {⊤} :=
by { rw set.eq_singleton_iff_nonempty_unique_mem, rw Inf_eq_top at h_inf, exact ⟨hne, h_inf⟩, }
@[simp] theorem Sup_eq_bot : Sup s = ⊥ ↔ (∀a∈s, a = ⊥) :=
@Inf_eq_top (order_dual α) _ _
lemma eq_singleton_bot_of_Sup_eq_bot_of_nonempty {s : set α}
(h_sup : Sup s = ⊥) (hne : s.nonempty) : s = {⊥} :=
by { rw set.eq_singleton_iff_nonempty_unique_mem, rw Sup_eq_bot at h_sup, exact ⟨hne, h_sup⟩, }
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 le_bsupr_of_le {p : ι → Prop} {f : Π i (h : p i), α} (i : ι) (hi : p i) (h : a ≤ f i hi) :
a ≤ ⨆ i hi, f i hi :=
le_trans h (le_bsupr 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 binfi_le_of_le {p : ι → Prop} {f : Π i (hi : p i), α} (i : ι) (hi : p i) (h : f i hi ≤ a) :
(⨅ i hi, f i hi) ≤ a :=
le_trans (binfi_le i hi) h
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
lemma supr_const_le {x : α} : (⨆ (h : ι), x) ≤ x :=
supr_le (λ _, le_rfl)
lemma le_infi_const {x : α} : x ≤ (⨅ (h : ι), x) :=
le_infi (λ _, le_rfl)
-- 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]
lemma inf_binfi {p : ι → Prop} {f : Π i (hi : p i), α} {a : α} (h : ∃ i, p i) :
a ⊓ (⨅i (h : p i), f i h) = (⨅ i (h : p i), a ⊓ f i h) :=
by simpa only [inf_comm] using binfi_inf h
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
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
|
9d17ed47552a449d2a95412c10f6424e8a74cb28 | 437dc96105f48409c3981d46fb48e57c9ac3a3e4 | /src/data/set/countable.lean | 39f3733e5a2c64406ef22874077433fa272bfc0d | [
"Apache-2.0"
] | permissive | dan-c-k/mathlib | 08efec79bd7481ee6da9cc44c24a653bff4fbe0d | 96efc220f6225bc7a5ed8349900391a33a38cc56 | refs/heads/master | 1,658,082,847,093 | 1,589,013,201,000 | 1,589,013,201,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,358 | 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
-/
import data.equiv.list
import data.set.finite
/-!
# Countable sets
-/
noncomputable theory
open function set encodable
open classical (hiding some)
open_locale classical
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
namespace set
/-- A set is countable if there exists an encoding of the set into the natural numbers.
An encoding is an injection with a partial inverse, which can be viewed as a
constructive analogue of countability. (For the most part, theorems about
`countable` will be classical and `encodable` will be constructive.)
-/
def countable (s : set α) : Prop := nonempty (encodable s)
lemma countable_iff_exists_injective {s : set α} :
countable s ↔ ∃f:s → ℕ, injective f :=
⟨λ ⟨h⟩, by exactI ⟨encode, encode_injective⟩,
λ ⟨f, h⟩, ⟨⟨f, partial_inv f, partial_inv_left h⟩⟩⟩
lemma countable_iff_exists_inj_on {s : set α} :
countable s ↔ ∃ f : α → ℕ, inj_on f s :=
countable_iff_exists_injective.trans
⟨λ ⟨f, hf⟩, ⟨λ a, if h : a ∈ s then f ⟨a, h⟩ else 0,
λ a b as bs h, congr_arg subtype.val $
hf $ by simpa [as, bs] using h⟩,
λ ⟨f, hf⟩, ⟨_, inj_on_iff_injective.1 hf⟩⟩
lemma countable_iff_exists_surjective [ne : nonempty α] {s : set α} :
countable s ↔ ∃f:ℕ → α, s ⊆ range f :=
⟨λ ⟨h⟩, by inhabit α; exactI ⟨λ n, ((decode s n).map subtype.val).iget,
λ a as, ⟨encode (⟨a, as⟩ : s), by simp [encodek]⟩⟩,
λ ⟨f, hf⟩, ⟨⟨
λ x, inv_fun f x.1,
λ n, if h : f n ∈ s then some ⟨f n, h⟩ else none,
λ ⟨x, hx⟩, begin
have := inv_fun_eq (hf hx), dsimp at this ⊢,
simp [this, hx]
end⟩⟩⟩
/--
A non-empty set is countable iff there exists a surjection from the
natural numbers onto the subtype induced by the set.
-/
lemma countable_iff_exists_surjective_to_subtype {s : set α} (hs : s.nonempty) :
countable s ↔ ∃ f : ℕ → s, surjective f :=
have inhabited s, from ⟨classical.choice hs.to_subtype⟩,
have countable s → ∃ f : ℕ → s, surjective f, from assume ⟨h⟩,
by exactI ⟨λ n, (decode s n).iget, λ a, ⟨encode a, by simp [encodek]⟩⟩,
have (∃ f : ℕ → s, surjective f) → countable s, from assume ⟨f, fsurj⟩,
⟨⟨inv_fun f, option.some ∘ f,
by intro h; simp [(inv_fun_eq (fsurj h) : f (inv_fun f h) = h)]⟩⟩,
by split; assumption
/-- Convert `countable s` to `encodable s` (noncomputable). -/
def countable.to_encodable {s : set α} : countable s → encodable s :=
classical.choice
lemma countable_encodable' (s : set α) [H : encodable s] : countable s :=
⟨H⟩
lemma countable_encodable [encodable α] (s : set α) : countable s :=
⟨by apply_instance⟩
/-- If `s : set α` is a nonempty countable set, then there exists a map
`f : ℕ → α` such that `s = range f`. -/
lemma countable.exists_surjective {s : set α} (hc : countable s) (hs : s.nonempty) :
∃f:ℕ → α, s = range f :=
begin
rcases hs with ⟨x, hx⟩,
letI : encodable s := countable.to_encodable hc,
letI : inhabited s := ⟨⟨x, hx⟩⟩,
have : countable (univ : set s) := countable_encodable _,
rcases countable_iff_exists_surjective.1 this with ⟨g, hg⟩,
have : range g = univ := univ_subset_iff.1 hg,
use coe ∘ g,
rw [range_comp, this],
simp
end
@[simp] lemma countable_empty : countable (∅ : set α) :=
⟨⟨λ x, x.2.elim, λ n, none, λ x, x.2.elim⟩⟩
@[simp] lemma countable_singleton (a : α) : countable ({a} : set α) :=
⟨of_equiv _ (equiv.set.singleton a)⟩
lemma countable.mono {s₁ s₂ : set α} (h : s₁ ⊆ s₂) : countable s₂ → countable s₁
| ⟨H⟩ := ⟨@of_inj _ _ H _ (embedding_of_subset h).2⟩
lemma countable.image {s : set α} (hs : countable s) (f : α → β) : countable (f '' s) :=
let f' : s → f '' s := λ⟨a, ha⟩, ⟨f a, mem_image_of_mem f ha⟩ in
have hf' : surjective f', from assume ⟨b, a, ha, hab⟩, ⟨⟨a, ha⟩, subtype.eq hab⟩,
⟨@encodable.of_inj _ _ hs.to_encodable (surj_inv hf') (injective_surj_inv hf')⟩
lemma countable_range [encodable α] (f : α → β) : countable (range f) :=
by rw ← image_univ; exact (countable_encodable _).image _
lemma countable_of_injective_of_countable_image {s : set α} {f : α → β}
(hf : inj_on f s) (hs : countable (f '' s)) : countable s :=
let ⟨g, hg⟩ := countable_iff_exists_inj_on.1 hs in
countable_iff_exists_inj_on.2 ⟨g ∘ f, hg.comp hf (maps_to_image _ _)⟩
lemma countable_Union {t : α → set β} [encodable α] (ht : ∀a, countable (t a)) :
countable (⋃a, t a) :=
by haveI := (λ a, (ht a).to_encodable);
rw Union_eq_range_sigma; apply countable_range
lemma countable.bUnion {s : set α} {t : α → set β} (hs : countable s) (ht : ∀a∈s, countable (t a)) :
countable (⋃a∈s, t a) :=
begin
rw bUnion_eq_Union,
haveI := hs.to_encodable,
exact countable_Union (by simpa using ht)
end
lemma countable.sUnion {s : set (set α)} (hs : countable s) (h : ∀a∈s, countable a) :
countable (⋃₀ s) :=
by rw sUnion_eq_bUnion; exact hs.bUnion h
lemma countable_Union_Prop {p : Prop} {t : p → set β} (ht : ∀h:p, countable (t h)) :
countable (⋃h:p, t h) :=
by by_cases p; simp [h, ht]
lemma countable.union {s₁ s₂ : set α} (h₁ : countable s₁) (h₂ : countable s₂) : countable (s₁ ∪ s₂) :=
by rw union_eq_Union; exact
countable_Union (bool.forall_bool.2 ⟨h₂, h₁⟩)
lemma countable.insert {s : set α} (a : α) (h : countable s) : countable (insert a s) :=
by { rw [set.insert_eq], exact (countable_singleton _).union h }
lemma finite.countable {s : set α} : finite s → countable s
| ⟨h⟩ := nonempty_of_trunc (by exactI trunc_encodable_of_fintype s)
/-- The set of finite subsets of a countable set is countable. -/
lemma countable_set_of_finite_subset {s : set α} : countable s →
countable {t | finite t ∧ t ⊆ s} | ⟨h⟩ :=
begin
resetI,
refine countable.mono _ (countable_range
(λ t : finset s, {a | ∃ h:a ∈ s, subtype.mk a h ∈ t})),
rintro t ⟨⟨ht⟩, ts⟩,
refine ⟨finset.univ.map (embedding_of_subset ts),
set.ext $ λ a, _⟩,
simp, split,
{ rintro ⟨as, b, bt, e⟩,
cases congr_arg subtype.val e, exact bt },
{ exact λ h, ⟨ts h, _, h, rfl⟩ }
end
lemma countable_pi {π : α → Type*} [fintype α] {s : Πa, set (π a)} (hs : ∀a, countable (s a)) :
countable {f : Πa, π a | ∀a, f a ∈ s a} :=
countable.mono
(show {f : Πa, π a | ∀a, f a ∈ s a} ⊆ range (λf : Πa, s a, λa, (f a).1), from
assume f hf, ⟨λa, ⟨f a, hf a⟩, funext $ assume a, rfl⟩) $
have trunc (encodable (Π (a : α), s a)), from
@encodable.fintype_pi α _ _ _ (assume a, (hs a).to_encodable),
trunc.induction_on this $ assume h,
@countable_range _ _ h _
lemma countable_prod {s : set α} {t : set β} (hs : countable s) (ht : countable t) :
countable (set.prod s t) :=
begin
haveI : encodable s := hs.to_encodable,
haveI : encodable t := ht.to_encodable,
haveI : encodable (s × t) := by apply_instance,
have : range (λp, ⟨p.1, p.2⟩ : s × t → α × β) = set.prod s t,
{ ext z,
rcases z with ⟨x, y⟩,
simp only [exists_prop, set.mem_range, set_coe.exists, prod.mk.inj_iff,
set.prod_mk_mem_set_prod_eq, subtype.coe_mk, prod.exists],
split,
{ rintros ⟨x', x's, y', y't, x'x, y'y⟩,
simp [x'x.symm, y'y.symm, x's, y't] },
{ rintros ⟨xs, yt⟩,
exact ⟨x, xs, y, yt, rfl, rfl⟩ }},
rw ← this,
exact countable_range _
end
section enumerate
/-- Enumerate elements in a countable set.-/
def enumerate_countable {s : set α} (h : countable s) (default : α) : ℕ → α :=
assume n, match @encodable.decode s (h.to_encodable) n with
| (some y) := y
| (none) := default
end
lemma subset_range_enumerate {s : set α} (h : countable s) (default : α) :
s ⊆ range (enumerate_countable h default) :=
assume x hx,
⟨@encodable.encode s h.to_encodable ⟨x, hx⟩,
by simp [enumerate_countable, encodable.encodek]⟩
end enumerate
end set
lemma finset.countable_to_set (s : finset α) : set.countable (↑s : set α) :=
s.finite_to_set.countable
|
633d4b2f48190d6bb9a4c2d5e90665d68685e22f | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /hott/hit/colimit.hlean | 9b500367463fe885c489e3c006df8e8ba4222937 | [
"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 | 6,855 | 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
Definition of general colimits and sequential colimits.
-/
/- definition of a general colimit -/
open eq nat quotient sigma equiv equiv.ops
namespace colimit
section
parameters {I J : Type} (A : I → Type) (dom cod : J → I)
(f : Π(j : J), A (dom j) → A (cod j))
variables {i : I} (a : A i) (j : J) (b : A (dom j))
local abbreviation B := Σ(i : I), A i
inductive colim_rel : B → B → Type :=
| Rmk : Π{j : J} (a : A (dom j)), colim_rel ⟨cod j, f j a⟩ ⟨dom j, a⟩
open colim_rel
local abbreviation R := colim_rel
-- TODO: define this in root namespace
definition colimit : Type :=
quotient colim_rel
definition incl : colimit :=
class_of R ⟨i, a⟩
abbreviation ι := @incl
definition cglue : ι (f j b) = ι b :=
eq_of_rel colim_rel (Rmk f b)
protected definition rec {P : colimit → Type}
(Pincl : Π⦃i : I⦄ (x : A i), P (ι x))
(Pglue : Π(j : J) (x : A (dom j)), Pincl (f j x) =[cglue j x] Pincl x)
(y : colimit) : P y :=
begin
fapply (quotient.rec_on y),
{ intro a, cases a, apply Pincl},
{ intro a a' H, cases H, apply Pglue}
end
protected definition rec_on [reducible] {P : colimit → Type} (y : colimit)
(Pincl : Π⦃i : I⦄ (x : A i), P (ι x))
(Pglue : Π(j : J) (x : A (dom j)), Pincl (f j x) =[cglue j x] Pincl x) : P y :=
rec Pincl Pglue y
theorem rec_cglue {P : colimit → Type}
(Pincl : Π⦃i : I⦄ (x : A i), P (ι x))
(Pglue : Π(j : J) (x : A (dom j)), Pincl (f j x) =[cglue j x] Pincl x)
{j : J} (x : A (dom j)) : apdo (rec Pincl Pglue) (cglue j x) = Pglue j x :=
!rec_eq_of_rel
protected definition elim {P : Type} (Pincl : Π⦃i : I⦄ (x : A i), P)
(Pglue : Π(j : J) (x : A (dom j)), Pincl (f j x) = Pincl x) (y : colimit) : P :=
rec Pincl (λj a, pathover_of_eq (Pglue j a)) y
protected definition elim_on [reducible] {P : Type} (y : colimit)
(Pincl : Π⦃i : I⦄ (x : A i), P)
(Pglue : Π(j : J) (x : A (dom j)), Pincl (f j x) = Pincl x) : P :=
elim Pincl Pglue y
theorem elim_cglue {P : Type}
(Pincl : Π⦃i : I⦄ (x : A i), P)
(Pglue : Π(j : J) (x : A (dom j)), Pincl (f j x) = Pincl x)
{j : J} (x : A (dom j)) : ap (elim Pincl Pglue) (cglue j x) = Pglue j x :=
begin
apply eq_of_fn_eq_fn_inv !(pathover_constant (cglue j x)),
rewrite [▸*,-apdo_eq_pathover_of_eq_ap,↑elim,rec_cglue],
end
protected definition elim_type (Pincl : Π⦃i : I⦄ (x : A i), Type)
(Pglue : Π(j : J) (x : A (dom j)), Pincl (f j x) ≃ Pincl x) (y : colimit) : Type :=
elim Pincl (λj a, ua (Pglue j a)) y
protected definition elim_type_on [reducible] (y : colimit)
(Pincl : Π⦃i : I⦄ (x : A i), Type)
(Pglue : Π(j : J) (x : A (dom j)), Pincl (f j x) ≃ Pincl x) : Type :=
elim_type Pincl Pglue y
theorem elim_type_cglue (Pincl : Π⦃i : I⦄ (x : A i), Type)
(Pglue : Π(j : J) (x : A (dom j)), Pincl (f j x) ≃ Pincl x)
{j : J} (x : A (dom j)) : transport (elim_type Pincl Pglue) (cglue j x) = Pglue j x :=
by rewrite [tr_eq_cast_ap_fn,↑elim_type,elim_cglue];apply cast_ua_fn
end
end colimit
/- definition of a sequential colimit -/
namespace seq_colim
section
/-
we define it directly in terms of quotients. An alternative definition could be
definition seq_colim := colimit.colimit A id succ f
-/
parameters {A : ℕ → Type} (f : Π⦃n⦄, A n → A (succ n))
variables {n : ℕ} (a : A n)
local abbreviation B := Σ(n : ℕ), A n
inductive seq_rel : B → B → Type :=
| Rmk : Π{n : ℕ} (a : A n), seq_rel ⟨succ n, f a⟩ ⟨n, a⟩
open seq_rel
local abbreviation R := seq_rel
-- TODO: define this in root namespace
definition seq_colim : Type :=
quotient seq_rel
definition inclusion : seq_colim :=
class_of R ⟨n, a⟩
abbreviation sι := @inclusion
definition glue : sι (f a) = sι a :=
eq_of_rel seq_rel (Rmk f a)
protected definition rec {P : seq_colim → Type}
(Pincl : Π⦃n : ℕ⦄ (a : A n), P (sι a))
(Pglue : Π(n : ℕ) (a : A n), Pincl (f a) =[glue a] Pincl a) (aa : seq_colim) : P aa :=
begin
fapply (quotient.rec_on aa),
{ intro a, cases a, apply Pincl},
{ intro a a' H, cases H, apply Pglue}
end
protected definition rec_on [reducible] {P : seq_colim → Type} (aa : seq_colim)
(Pincl : Π⦃n : ℕ⦄ (a : A n), P (sι a))
(Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) =[glue a] Pincl a)
: P aa :=
rec Pincl Pglue aa
theorem rec_glue {P : seq_colim → Type} (Pincl : Π⦃n : ℕ⦄ (a : A n), P (sι a))
(Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) =[glue a] Pincl a) {n : ℕ} (a : A n)
: apdo (rec Pincl Pglue) (glue a) = Pglue a :=
!rec_eq_of_rel
protected definition elim {P : Type} (Pincl : Π⦃n : ℕ⦄ (a : A n), P)
(Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) = Pincl a) : seq_colim → P :=
rec Pincl (λn a, pathover_of_eq (Pglue a))
protected definition elim_on [reducible] {P : Type} (aa : seq_colim)
(Pincl : Π⦃n : ℕ⦄ (a : A n), P)
(Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) = Pincl a) : P :=
elim Pincl Pglue aa
theorem elim_glue {P : Type} (Pincl : Π⦃n : ℕ⦄ (a : A n), P)
(Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) = Pincl a) {n : ℕ} (a : A n)
: ap (elim Pincl Pglue) (glue a) = Pglue a :=
begin
apply eq_of_fn_eq_fn_inv !(pathover_constant (glue a)),
rewrite [▸*,-apdo_eq_pathover_of_eq_ap,↑elim,rec_glue],
end
protected definition elim_type (Pincl : Π⦃n : ℕ⦄ (a : A n), Type)
(Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) ≃ Pincl a) : seq_colim → Type :=
elim Pincl (λn a, ua (Pglue a))
protected definition elim_type_on [reducible] (aa : seq_colim)
(Pincl : Π⦃n : ℕ⦄ (a : A n), Type)
(Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) ≃ Pincl a) : Type :=
elim_type Pincl Pglue aa
theorem elim_type_glue (Pincl : Π⦃n : ℕ⦄ (a : A n), Type)
(Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) ≃ Pincl a) {n : ℕ} (a : A n)
: transport (elim_type Pincl Pglue) (glue a) = Pglue a :=
by rewrite [tr_eq_cast_ap_fn,↑elim_type,elim_glue];apply cast_ua_fn
end
end seq_colim
attribute colimit.incl seq_colim.inclusion [constructor]
attribute colimit.rec colimit.elim [unfold 10] [recursor 10]
attribute colimit.elim_type [unfold 9]
attribute colimit.rec_on colimit.elim_on [unfold 8]
attribute colimit.elim_type_on [unfold 7]
attribute seq_colim.rec seq_colim.elim [unfold 6] [recursor 6]
attribute seq_colim.elim_type [unfold 5]
attribute seq_colim.rec_on seq_colim.elim_on [unfold 4]
attribute seq_colim.elim_type_on [unfold 3]
|
48c126c1137001e42ff1255d08ebdb1aa5dbdfd5 | 8e6cad62ec62c6c348e5faaa3c3f2079012bdd69 | /src/topology/basic.lean | 10e5280b9426e1642a27dc98017568324e9814c7 | [
"Apache-2.0"
] | permissive | benjamindavidson/mathlib | 8cc81c865aa8e7cf4462245f58d35ae9a56b150d | fad44b9f670670d87c8e25ff9cdf63af87ad731e | refs/heads/master | 1,679,545,578,362 | 1,615,343,014,000 | 1,615,343,014,000 | 312,926,983 | 0 | 0 | Apache-2.0 | 1,615,360,301,000 | 1,605,399,418,000 | Lean | UTF-8 | Lean | false | false | 53,676 | 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, Jeremy Avigad
-/
import order.filter.ultrafilter
import order.filter.partial
noncomputable theory
/-!
# Basic theory of topological spaces.
The main definition is the type class `topological space α` which endows a type `α` with a topology.
Then `set α` gets predicates `is_open`, `is_closed` and functions `interior`, `closure` and
`frontier`. Each point `x` of `α` gets a neighborhood filter `𝓝 x`. A filter `F` on `α` has
`x` as a cluster point if `cluster_pt x F : 𝓝 x ⊓ F ≠ ⊥`. A map `f : ι → α` clusters at `x`
along `F : filter ι` if `map_cluster_pt x F f : cluster_pt x (map f F)`. In particular
the notion of cluster point of a sequence `u` is `map_cluster_pt x at_top u`.
This file also defines locally finite families of subsets of `α`.
For topological spaces `α` and `β`, a function `f : α → β` and a point `a : α`,
`continuous_at f a` means `f` is continuous at `a`, and global continuity is
`continuous f`. There is also a version of continuity `pcontinuous` for
partially defined functions.
## Notation
* `𝓝 x`: the filter of neighborhoods of a point `x`;
* `𝓟 s`: the principal filter of a set `s`;
* `𝓝[s] x`: the filter `nhds_within x s` of neighborhoods of a point `x` within a set `s`.
## Implementation notes
Topology in mathlib heavily uses filters (even more than in Bourbaki). See explanations in
<https://leanprover-community.github.io/theories/topology.html>.
## References
* [N. Bourbaki, *General Topology*][bourbaki1966]
* [I. M. James, *Topologies and Uniformities*][james1999]
## Tags
topological space, interior, closure, frontier, neighborhood, continuity, continuous function
-/
open set filter classical
open_locale classical filter
universes u v w
/-!
### Topological spaces
-/
/-- A topology on `α`. -/
@[protect_proj] structure topological_space (α : Type u) :=
(is_open : set α → Prop)
(is_open_univ : is_open univ)
(is_open_inter : ∀s t, is_open s → is_open t → is_open (s ∩ t))
(is_open_sUnion : ∀s, (∀t∈s, is_open t) → is_open (⋃₀ s))
attribute [class] topological_space
/-- A constructor for topologies by specifying the closed sets,
and showing that they satisfy the appropriate conditions. -/
def topological_space.of_closed {α : Type u} (T : set (set α))
(empty_mem : ∅ ∈ T) (sInter_mem : ∀ A ⊆ T, ⋂₀ A ∈ T) (union_mem : ∀ A B ∈ T, A ∪ B ∈ T) :
topological_space α :=
{ is_open := λ X, Xᶜ ∈ T,
is_open_univ := by simp [empty_mem],
is_open_inter := λ s t hs ht, by simpa [set.compl_inter] using union_mem sᶜ tᶜ hs ht,
is_open_sUnion := λ s hs,
by rw set.compl_sUnion; exact sInter_mem (set.compl '' s)
(λ z ⟨y, hy, hz⟩, by simpa [hz.symm] using hs y hy) }
section topological_space
variables {α : Type u} {β : Type v} {ι : Sort w} {a : α} {s s₁ s₂ : set α} {p p₁ p₂ : α → Prop}
@[ext]
lemma topological_space_eq : ∀ {f g : topological_space α}, f.is_open = g.is_open → f = g
| ⟨a, _, _, _⟩ ⟨b, _, _, _⟩ rfl := rfl
section
variables [t : topological_space α]
include t
/-- `is_open s` means that `s` is open in the ambient topological space on `α` -/
def is_open (s : set α) : Prop := topological_space.is_open t s
@[simp]
lemma is_open_univ : is_open (univ : set α) := topological_space.is_open_univ t
lemma is_open_inter (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∩ s₂) :=
topological_space.is_open_inter t s₁ s₂ h₁ h₂
lemma is_open_sUnion {s : set (set α)} (h : ∀t ∈ s, is_open t) : is_open (⋃₀ s) :=
topological_space.is_open_sUnion t s h
end
lemma topological_space_eq_iff {t t' : topological_space α} :
t = t' ↔ ∀ s, @is_open α t s ↔ @is_open α t' s :=
⟨λ h s, h ▸ iff.rfl, λ h, by { ext, exact h _ }⟩
lemma is_open_fold {s : set α} {t : topological_space α} : t.is_open s = @is_open α t s :=
rfl
variables [topological_space α]
lemma is_open_Union {f : ι → set α} (h : ∀i, is_open (f i)) : is_open (⋃i, f i) :=
is_open_sUnion $ by rintro _ ⟨i, rfl⟩; exact h i
lemma is_open_bUnion {s : set β} {f : β → set α} (h : ∀i∈s, is_open (f i)) :
is_open (⋃i∈s, f i) :=
is_open_Union $ assume i, is_open_Union $ assume hi, h i hi
lemma is_open_union (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∪ s₂) :=
by rw union_eq_Union; exact is_open_Union (bool.forall_bool.2 ⟨h₂, h₁⟩)
@[simp] lemma is_open_empty : is_open (∅ : set α) :=
by rw ← sUnion_empty; exact is_open_sUnion (assume a, false.elim)
lemma is_open_sInter {s : set (set α)} (hs : finite s) : (∀t ∈ s, is_open t) → is_open (⋂₀ s) :=
finite.induction_on hs (λ _, by rw sInter_empty; exact is_open_univ) $
λ a s has hs ih h, by rw sInter_insert; exact
is_open_inter (h _ $ mem_insert _ _) (ih $ λ t, h t ∘ mem_insert_of_mem _)
lemma is_open_bInter {s : set β} {f : β → set α} (hs : finite s) :
(∀i∈s, is_open (f i)) → is_open (⋂i∈s, f i) :=
finite.induction_on hs
(λ _, by rw bInter_empty; exact is_open_univ)
(λ a s has hs ih h, by rw bInter_insert; exact
is_open_inter (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi))))
lemma is_open_Inter [fintype β] {s : β → set α}
(h : ∀ i, is_open (s i)) : is_open (⋂ i, s i) :=
suffices is_open (⋂ (i : β) (hi : i ∈ @univ β), s i), by simpa,
is_open_bInter finite_univ (λ i _, h i)
lemma is_open_Inter_prop {p : Prop} {s : p → set α}
(h : ∀ h : p, is_open (s h)) : is_open (Inter s) :=
by by_cases p; simp *
lemma is_open_const {p : Prop} : is_open {a : α | p} :=
by_cases
(assume : p, begin simp only [this]; exact is_open_univ end)
(assume : ¬ p, begin simp only [this]; exact is_open_empty end)
lemma is_open_and : is_open {a | p₁ a} → is_open {a | p₂ a} → is_open {a | p₁ a ∧ p₂ a} :=
is_open_inter
/-- A set is closed if its complement is open -/
def is_closed (s : set α) : Prop := is_open sᶜ
@[simp] lemma is_closed_empty : is_closed (∅ : set α) :=
by unfold is_closed; rw compl_empty; exact is_open_univ
@[simp] lemma is_closed_univ : is_closed (univ : set α) :=
by unfold is_closed; rw compl_univ; exact is_open_empty
lemma is_closed_union : is_closed s₁ → is_closed s₂ → is_closed (s₁ ∪ s₂) :=
λ h₁ h₂, by unfold is_closed; rw compl_union; exact is_open_inter h₁ h₂
lemma is_closed_sInter {s : set (set α)} : (∀t ∈ s, is_closed t) → is_closed (⋂₀ s) :=
by simpa only [is_closed, compl_sInter, sUnion_image] using is_open_bUnion
lemma is_closed_Inter {f : ι → set α} (h : ∀i, is_closed (f i)) : is_closed (⋂i, f i ) :=
is_closed_sInter $ assume t ⟨i, (heq : f i = t)⟩, heq ▸ h i
lemma is_closed_bInter {s : set β} {f : β → set α} (h : ∀ i ∈ s, is_closed (f i)) :
is_closed (⋂ i ∈ s, f i) :=
is_closed_Inter $ λ i, is_closed_Inter $ h i
@[simp] lemma is_open_compl_iff {s : set α} : is_open sᶜ ↔ is_closed s := iff.rfl
@[simp] lemma is_closed_compl_iff {s : set α} : is_closed sᶜ ↔ is_open s :=
by rw [←is_open_compl_iff, compl_compl]
lemma is_open_diff {s t : set α} (h₁ : is_open s) (h₂ : is_closed t) : is_open (s \ t) :=
is_open_inter h₁ $ is_open_compl_iff.mpr h₂
lemma is_closed_inter (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (s₁ ∩ s₂) :=
by rw [is_closed, compl_inter]; exact is_open_union h₁ h₂
lemma is_closed_bUnion {s : set β} {f : β → set α} (hs : finite s) :
(∀i∈s, is_closed (f i)) → is_closed (⋃i∈s, f i) :=
finite.induction_on hs
(λ _, by rw bUnion_empty; exact is_closed_empty)
(λ a s has hs ih h, by rw bUnion_insert; exact
is_closed_union (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi))))
lemma is_closed_Union [fintype β] {s : β → set α}
(h : ∀ i, is_closed (s i)) : is_closed (Union s) :=
suffices is_closed (⋃ (i : β) (hi : i ∈ @univ β), s i),
by convert this; simp [set.ext_iff],
is_closed_bUnion finite_univ (λ i _, h i)
lemma is_closed_Union_prop {p : Prop} {s : p → set α}
(h : ∀ h : p, is_closed (s h)) : is_closed (Union s) :=
by by_cases p; simp *
lemma is_closed_imp {p q : α → Prop} (hp : is_open {x | p x})
(hq : is_closed {x | q x}) : is_closed {x | p x → q x} :=
have {x | p x → q x} = {x | p x}ᶜ ∪ {x | q x}, from set.ext $ λ x, imp_iff_not_or,
by rw [this]; exact is_closed_union (is_closed_compl_iff.mpr hp) hq
lemma is_open_neg : is_closed {a | p a} → is_open {a | ¬ p a} :=
is_open_compl_iff.mpr
/-!
### Interior of a set
-/
/-- The interior of a set `s` is the largest open subset of `s`. -/
def interior (s : set α) : set α := ⋃₀ {t | is_open t ∧ t ⊆ s}
lemma mem_interior {s : set α} {x : α} :
x ∈ interior s ↔ ∃ t ⊆ s, is_open t ∧ x ∈ t :=
by simp only [interior, mem_set_of_eq, exists_prop, and_assoc, and.left_comm]
@[simp] lemma is_open_interior {s : set α} : is_open (interior s) :=
is_open_sUnion $ assume t ⟨h₁, h₂⟩, h₁
lemma interior_subset {s : set α} : interior s ⊆ s :=
sUnion_subset $ assume t ⟨h₁, h₂⟩, h₂
lemma interior_maximal {s t : set α} (h₁ : t ⊆ s) (h₂ : is_open t) : t ⊆ interior s :=
subset_sUnion_of_mem ⟨h₂, h₁⟩
lemma is_open.interior_eq {s : set α} (h : is_open s) : interior s = s :=
subset.antisymm interior_subset (interior_maximal (subset.refl s) h)
lemma interior_eq_iff_open {s : set α} : interior s = s ↔ is_open s :=
⟨assume h, h ▸ is_open_interior, is_open.interior_eq⟩
lemma subset_interior_iff_open {s : set α} : s ⊆ interior s ↔ is_open s :=
by simp only [interior_eq_iff_open.symm, subset.antisymm_iff, interior_subset, true_and]
lemma subset_interior_iff_subset_of_open {s t : set α} (h₁ : is_open s) :
s ⊆ interior t ↔ s ⊆ t :=
⟨assume h, subset.trans h interior_subset, assume h₂, interior_maximal h₂ h₁⟩
lemma interior_mono {s t : set α} (h : s ⊆ t) : interior s ⊆ interior t :=
interior_maximal (subset.trans interior_subset h) is_open_interior
@[simp] lemma interior_empty : interior (∅ : set α) = ∅ :=
is_open_empty.interior_eq
@[simp] lemma interior_univ : interior (univ : set α) = univ :=
is_open_univ.interior_eq
@[simp] lemma interior_interior {s : set α} : interior (interior s) = interior s :=
is_open_interior.interior_eq
@[simp] lemma interior_inter {s t : set α} : interior (s ∩ t) = interior s ∩ interior t :=
subset.antisymm
(subset_inter (interior_mono $ inter_subset_left s t) (interior_mono $ inter_subset_right s t))
(interior_maximal (inter_subset_inter interior_subset interior_subset) $
is_open_inter is_open_interior is_open_interior)
lemma interior_union_is_closed_of_interior_empty {s t : set α} (h₁ : is_closed s)
(h₂ : interior t = ∅) :
interior (s ∪ t) = interior s :=
have interior (s ∪ t) ⊆ s, from
assume x ⟨u, ⟨(hu₁ : is_open u), (hu₂ : u ⊆ s ∪ t)⟩, (hx₁ : x ∈ u)⟩,
classical.by_contradiction $ assume hx₂ : x ∉ s,
have u \ s ⊆ t,
from assume x ⟨h₁, h₂⟩, or.resolve_left (hu₂ h₁) h₂,
have u \ s ⊆ interior t,
by rwa subset_interior_iff_subset_of_open (is_open_diff hu₁ h₁),
have u \ s ⊆ ∅,
by rwa h₂ at this,
this ⟨hx₁, hx₂⟩,
subset.antisymm
(interior_maximal this is_open_interior)
(interior_mono $ subset_union_left _ _)
lemma is_open_iff_forall_mem_open : is_open s ↔ ∀ x ∈ s, ∃ t ⊆ s, is_open t ∧ x ∈ t :=
by rw ← subset_interior_iff_open; simp only [subset_def, mem_interior]
/-!
### Closure of a set
-/
/-- The closure of `s` is the smallest closed set containing `s`. -/
def closure (s : set α) : set α := ⋂₀ {t | is_closed t ∧ s ⊆ t}
@[simp] lemma is_closed_closure {s : set α} : is_closed (closure s) :=
is_closed_sInter $ assume t ⟨h₁, h₂⟩, h₁
lemma subset_closure {s : set α} : s ⊆ closure s :=
subset_sInter $ assume t ⟨h₁, h₂⟩, h₂
lemma closure_minimal {s t : set α} (h₁ : s ⊆ t) (h₂ : is_closed t) : closure s ⊆ t :=
sInter_subset_of_mem ⟨h₂, h₁⟩
lemma is_closed.closure_eq {s : set α} (h : is_closed s) : closure s = s :=
subset.antisymm (closure_minimal (subset.refl s) h) subset_closure
lemma is_closed.closure_subset {s : set α} (hs : is_closed s) : closure s ⊆ s :=
closure_minimal (subset.refl _) hs
lemma is_closed.closure_subset_iff {s t : set α} (h₁ : is_closed t) :
closure s ⊆ t ↔ s ⊆ t :=
⟨subset.trans subset_closure, assume h, closure_minimal h h₁⟩
@[mono] lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t :=
closure_minimal (subset.trans h subset_closure) is_closed_closure
lemma monotone_closure (α : Type*) [topological_space α] : monotone (@closure α _) :=
λ _ _, closure_mono
lemma diff_subset_closure_iff {s t : set α} :
s \ t ⊆ closure t ↔ s ⊆ closure t :=
by rw [diff_subset_iff, union_eq_self_of_subset_left subset_closure]
lemma closure_inter_subset_inter_closure (s t : set α) :
closure (s ∩ t) ⊆ closure s ∩ closure t :=
(monotone_closure α).map_inf_le s t
lemma is_closed_of_closure_subset {s : set α} (h : closure s ⊆ s) : is_closed s :=
by rw subset.antisymm subset_closure h; exact is_closed_closure
lemma closure_eq_iff_is_closed {s : set α} : closure s = s ↔ is_closed s :=
⟨assume h, h ▸ is_closed_closure, is_closed.closure_eq⟩
lemma closure_subset_iff_is_closed {s : set α} : closure s ⊆ s ↔ is_closed s :=
⟨is_closed_of_closure_subset, is_closed.closure_subset⟩
@[simp] lemma closure_empty : closure (∅ : set α) = ∅ :=
is_closed_empty.closure_eq
@[simp] lemma closure_empty_iff (s : set α) : closure s = ∅ ↔ s = ∅ :=
⟨subset_eq_empty subset_closure, λ h, h.symm ▸ closure_empty⟩
@[simp] lemma closure_nonempty_iff {s : set α} : (closure s).nonempty ↔ s.nonempty :=
by simp only [← ne_empty_iff_nonempty, ne.def, closure_empty_iff]
alias closure_nonempty_iff ↔ set.nonempty.of_closure set.nonempty.closure
@[simp] lemma closure_univ : closure (univ : set α) = univ :=
is_closed_univ.closure_eq
@[simp] lemma closure_closure {s : set α} : closure (closure s) = closure s :=
is_closed_closure.closure_eq
@[simp] lemma closure_union {s t : set α} : closure (s ∪ t) = closure s ∪ closure t :=
subset.antisymm
(closure_minimal (union_subset_union subset_closure subset_closure) $
is_closed_union is_closed_closure is_closed_closure)
((monotone_closure α).le_map_sup s t)
lemma interior_subset_closure {s : set α} : interior s ⊆ closure s :=
subset.trans interior_subset subset_closure
lemma closure_eq_compl_interior_compl {s : set α} : closure s = (interior sᶜ)ᶜ :=
begin
unfold interior closure is_closed,
rw [compl_sUnion, compl_image_set_of],
simp only [compl_subset_compl]
end
@[simp] lemma interior_compl {s : set α} : interior sᶜ = (closure s)ᶜ :=
by simp [closure_eq_compl_interior_compl]
@[simp] lemma closure_compl {s : set α} : closure sᶜ = (interior s)ᶜ :=
by simp [closure_eq_compl_interior_compl]
theorem mem_closure_iff {s : set α} {a : α} :
a ∈ closure s ↔ ∀ o, is_open o → a ∈ o → (o ∩ s).nonempty :=
⟨λ h o oo ao, classical.by_contradiction $ λ os,
have s ⊆ oᶜ, from λ x xs xo, os ⟨x, xo, xs⟩,
closure_minimal this (is_closed_compl_iff.2 oo) h ao,
λ H c ⟨h₁, h₂⟩, classical.by_contradiction $ λ nc,
let ⟨x, hc, hs⟩ := (H _ h₁ nc) in hc (h₂ hs)⟩
/-- A set is dense in a topological space if every point belongs to its closure. -/
def dense (s : set α) : Prop := ∀ x, x ∈ closure s
lemma dense_iff_closure_eq {s : set α} : dense s ↔ closure s = univ :=
eq_univ_iff_forall.symm
lemma dense.closure_eq {s : set α} (h : dense s) : closure s = univ :=
dense_iff_closure_eq.mp h
/-- The closure of a set `s` is dense if and only if `s` is dense. -/
@[simp] lemma dense_closure {s : set α} : dense (closure s) ↔ dense s :=
by rw [dense, dense, closure_closure]
alias dense_closure ↔ dense.of_closure dense.closure
@[simp] lemma dense_univ : dense (univ : set α) := λ x, subset_closure trivial
/-- A set is dense if and only if it has a nonempty intersection with each nonempty open set. -/
lemma dense_iff_inter_open {s : set α} :
dense s ↔ ∀ U, is_open U → U.nonempty → (U ∩ s).nonempty :=
begin
split ; intro h,
{ rintros U U_op ⟨x, x_in⟩,
exact mem_closure_iff.1 (by simp only [h.closure_eq]) U U_op x_in },
{ intro x,
rw mem_closure_iff,
intros U U_op x_in,
exact h U U_op ⟨_, x_in⟩ },
end
alias dense_iff_inter_open ↔ dense.inter_open_nonempty _
lemma dense.nonempty_iff {s : set α} (hs : dense s) :
s.nonempty ↔ nonempty α :=
⟨λ ⟨x, hx⟩, ⟨x⟩, λ ⟨x⟩,
let ⟨y, hy⟩ := hs.inter_open_nonempty _ is_open_univ ⟨x, trivial⟩ in ⟨y, hy.2⟩⟩
lemma dense.nonempty [h : nonempty α] {s : set α} (hs : dense s) : s.nonempty :=
hs.nonempty_iff.2 h
@[mono]
lemma dense.mono {s₁ s₂ : set α} (h : s₁ ⊆ s₂) (hd : dense s₁) : dense s₂ :=
λ x, closure_mono h (hd x)
/-!
### Frontier of a set
-/
/-- The frontier of a set is the set of points between the closure and interior. -/
def frontier (s : set α) : set α := closure s \ interior s
lemma frontier_eq_closure_inter_closure {s : set α} :
frontier s = closure s ∩ closure sᶜ :=
by rw [closure_compl, frontier, diff_eq]
/-- The complement of a set has the same frontier as the original set. -/
@[simp] lemma frontier_compl (s : set α) : frontier sᶜ = frontier s :=
by simp only [frontier_eq_closure_inter_closure, compl_compl, inter_comm]
@[simp] lemma frontier_univ : frontier (univ : set α) = ∅ := by simp [frontier]
@[simp] lemma frontier_empty : frontier (∅ : set α) = ∅ := by simp [frontier]
lemma frontier_inter_subset (s t : set α) :
frontier (s ∩ t) ⊆ (frontier s ∩ closure t) ∪ (closure s ∩ frontier t) :=
begin
simp only [frontier_eq_closure_inter_closure, compl_inter, closure_union],
convert inter_subset_inter_left _ (closure_inter_subset_inter_closure s t),
simp only [inter_distrib_left, inter_distrib_right, inter_assoc],
congr' 2,
apply inter_comm
end
lemma frontier_union_subset (s t : set α) :
frontier (s ∪ t) ⊆ (frontier s ∩ closure tᶜ) ∪ (closure sᶜ ∩ frontier t) :=
by simpa only [frontier_compl, ← compl_union]
using frontier_inter_subset sᶜ tᶜ
lemma is_closed.frontier_eq {s : set α} (hs : is_closed s) : frontier s = s \ interior s :=
by rw [frontier, hs.closure_eq]
lemma is_open.frontier_eq {s : set α} (hs : is_open s) : frontier s = closure s \ s :=
by rw [frontier, hs.interior_eq]
/-- The frontier of a set is closed. -/
lemma is_closed_frontier {s : set α} : is_closed (frontier s) :=
by rw frontier_eq_closure_inter_closure; exact is_closed_inter is_closed_closure is_closed_closure
/-- The frontier of a closed set has no interior point. -/
lemma interior_frontier {s : set α} (h : is_closed s) : interior (frontier s) = ∅ :=
begin
have A : frontier s = s \ interior s, from h.frontier_eq,
have B : interior (frontier s) ⊆ interior s, by rw A; exact interior_mono (diff_subset _ _),
have C : interior (frontier s) ⊆ frontier s := interior_subset,
have : interior (frontier s) ⊆ (interior s) ∩ (s \ interior s) :=
subset_inter B (by simpa [A] using C),
rwa [inter_diff_self, subset_empty_iff] at this,
end
lemma closure_eq_interior_union_frontier (s : set α) : closure s = interior s ∪ frontier s :=
(union_diff_cancel interior_subset_closure).symm
lemma closure_eq_self_union_frontier (s : set α) : closure s = s ∪ frontier s :=
(union_diff_cancel' interior_subset subset_closure).symm
/-!
### Neighborhoods
-/
/-- A set is called a neighborhood of `a` if it contains an open set around `a`. The set of all
neighborhoods of `a` forms a filter, the neighborhood filter at `a`, is here defined as the
infimum over the principal filters of all open sets containing `a`. -/
@[irreducible] def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 s)
localized "notation `𝓝` := nhds" in topological_space
/-- The "neighborhood within" filter. Elements of `𝓝[s] a` are sets containing the
intersection of `s` and a neighborhood of `a`. -/
def nhds_within (a : α) (s : set α) : filter α := 𝓝 a ⊓ 𝓟 s
localized "notation `𝓝[` s `] ` x:100 := nhds_within x s" in topological_space
lemma nhds_def (a : α) : 𝓝 a = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 s) := by rw nhds
/-- The open sets containing `a` are a basis for the neighborhood filter. See `nhds_basis_opens'`
for a variant using open neighborhoods instead. -/
lemma nhds_basis_opens (a : α) : (𝓝 a).has_basis (λ s : set α, a ∈ s ∧ is_open s) (λ x, x) :=
begin
rw nhds_def,
exact has_basis_binfi_principal
(λ s ⟨has, hs⟩ t ⟨hat, ht⟩, ⟨s ∩ t, ⟨⟨has, hat⟩, is_open_inter hs ht⟩,
⟨inter_subset_left _ _, inter_subset_right _ _⟩⟩)
⟨univ, ⟨mem_univ a, is_open_univ⟩⟩
end
/-- A filter lies below the neighborhood filter at `a` iff it contains every open set around `a`. -/
lemma le_nhds_iff {f a} : f ≤ 𝓝 a ↔ ∀ s : set α, a ∈ s → is_open s → s ∈ f :=
by simp [nhds_def]
/-- To show a filter is above the neighborhood filter at `a`, it suffices to show that it is above
the principal filter of some open set `s` containing `a`. -/
lemma nhds_le_of_le {f a} {s : set α} (h : a ∈ s) (o : is_open s) (sf : 𝓟 s ≤ f) : 𝓝 a ≤ f :=
by rw nhds_def; exact infi_le_of_le s (infi_le_of_le ⟨h, o⟩ sf)
lemma mem_nhds_sets_iff {a : α} {s : set α} :
s ∈ 𝓝 a ↔ ∃t⊆s, is_open t ∧ a ∈ t :=
(nhds_basis_opens a).mem_iff.trans
⟨λ ⟨t, ⟨hat, ht⟩, hts⟩, ⟨t, hts, ht, hat⟩, λ ⟨t, hts, ht, hat⟩, ⟨t, ⟨hat, ht⟩, hts⟩⟩
/-- A predicate is true in a neighborhood of `a` iff it is true for all the points in an open set
containing `a`. -/
lemma eventually_nhds_iff {a : α} {p : α → Prop} :
(∀ᶠ x in 𝓝 a, p x) ↔ ∃ (t : set α), (∀ x ∈ t, p x) ∧ is_open t ∧ a ∈ t :=
mem_nhds_sets_iff.trans $ by simp only [subset_def, exists_prop, mem_set_of_eq]
lemma map_nhds {a : α} {f : α → β} :
map f (𝓝 a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 (image f s)) :=
((nhds_basis_opens a).map f).eq_binfi
lemma mem_of_nhds {a : α} {s : set α} : s ∈ 𝓝 a → a ∈ s :=
λ H, let ⟨t, ht, _, hs⟩ := mem_nhds_sets_iff.1 H in ht hs
/-- If a predicate is true in a neighborhood of `a`, then it is true for `a`. -/
lemma filter.eventually.self_of_nhds {p : α → Prop} {a : α}
(h : ∀ᶠ y in 𝓝 a, p y) : p a :=
mem_of_nhds h
lemma mem_nhds_sets {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) :
s ∈ 𝓝 a :=
mem_nhds_sets_iff.2 ⟨s, subset.refl _, hs, ha⟩
lemma is_open.eventually_mem {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) :
∀ᶠ x in 𝓝 a, x ∈ s :=
mem_nhds_sets hs ha
/-- The open neighborhoods of `a` are a basis for the neighborhood filter. See `nhds_basis_opens`
for a variant using open sets around `a` instead. -/
lemma nhds_basis_opens' (a : α) : (𝓝 a).has_basis (λ s : set α, s ∈ 𝓝 a ∧ is_open s) (λ x, x) :=
begin
convert nhds_basis_opens a,
ext s,
split,
{ rintros ⟨s_in, s_op⟩,
exact ⟨mem_of_nhds s_in, s_op⟩ },
{ rintros ⟨a_in, s_op⟩,
exact ⟨mem_nhds_sets s_op a_in, s_op⟩ },
end
/-- If a predicate is true in a neighbourhood of `a`, then for `y` sufficiently close
to `a` this predicate is true in a neighbourhood of `y`. -/
lemma filter.eventually.eventually_nhds {p : α → Prop} {a : α} (h : ∀ᶠ y in 𝓝 a, p y) :
∀ᶠ y in 𝓝 a, ∀ᶠ x in 𝓝 y, p x :=
let ⟨t, htp, hto, ha⟩ := eventually_nhds_iff.1 h in
eventually_nhds_iff.2 ⟨t, λ x hx, eventually_nhds_iff.2 ⟨t, htp, hto, hx⟩, hto, ha⟩
@[simp] lemma eventually_eventually_nhds {p : α → Prop} {a : α} :
(∀ᶠ y in 𝓝 a, ∀ᶠ x in 𝓝 y, p x) ↔ ∀ᶠ x in 𝓝 a, p x :=
⟨λ h, h.self_of_nhds, λ h, h.eventually_nhds⟩
@[simp] lemma nhds_bind_nhds : (𝓝 a).bind 𝓝 = 𝓝 a := filter.ext $ λ s, eventually_eventually_nhds
@[simp] lemma eventually_eventually_eq_nhds {f g : α → β} {a : α} :
(∀ᶠ y in 𝓝 a, f =ᶠ[𝓝 y] g) ↔ f =ᶠ[𝓝 a] g :=
eventually_eventually_nhds
lemma filter.eventually_eq.eq_of_nhds {f g : α → β} {a : α} (h : f =ᶠ[𝓝 a] g) : f a = g a :=
h.self_of_nhds
@[simp] lemma eventually_eventually_le_nhds [has_le β] {f g : α → β} {a : α} :
(∀ᶠ y in 𝓝 a, f ≤ᶠ[𝓝 y] g) ↔ f ≤ᶠ[𝓝 a] g :=
eventually_eventually_nhds
/-- If two functions are equal in a neighbourhood of `a`, then for `y` sufficiently close
to `a` these functions are equal in a neighbourhood of `y`. -/
lemma filter.eventually_eq.eventually_eq_nhds {f g : α → β} {a : α} (h : f =ᶠ[𝓝 a] g) :
∀ᶠ y in 𝓝 a, f =ᶠ[𝓝 y] g :=
h.eventually_nhds
/-- If `f x ≤ g x` in a neighbourhood of `a`, then for `y` sufficiently close to `a` we have
`f x ≤ g x` in a neighbourhood of `y`. -/
lemma filter.eventually_le.eventually_le_nhds [has_le β] {f g : α → β} {a : α} (h : f ≤ᶠ[𝓝 a] g) :
∀ᶠ y in 𝓝 a, f ≤ᶠ[𝓝 y] g :=
h.eventually_nhds
theorem all_mem_nhds (x : α) (P : set α → Prop) (hP : ∀ s t, s ⊆ t → P s → P t) :
(∀ s ∈ 𝓝 x, P s) ↔ (∀ s, is_open s → x ∈ s → P s) :=
((nhds_basis_opens x).forall_iff hP).trans $ by simp only [and_comm (x ∈ _), and_imp]
theorem all_mem_nhds_filter (x : α) (f : set α → set β) (hf : ∀ s t, s ⊆ t → f s ⊆ f t)
(l : filter β) :
(∀ s ∈ 𝓝 x, f s ∈ l) ↔ (∀ s, is_open s → x ∈ s → f s ∈ l) :=
all_mem_nhds _ _ (λ s t ssubt h, mem_sets_of_superset h (hf s t ssubt))
theorem rtendsto_nhds {r : rel β α} {l : filter β} {a : α} :
rtendsto r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.core s ∈ l) :=
all_mem_nhds_filter _ _ (λ s t, id) _
theorem rtendsto'_nhds {r : rel β α} {l : filter β} {a : α} :
rtendsto' r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.preimage s ∈ l) :=
by { rw [rtendsto'_def], apply all_mem_nhds_filter, apply rel.preimage_mono }
theorem ptendsto_nhds {f : β →. α} {l : filter β} {a : α} :
ptendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.core s ∈ l) :=
rtendsto_nhds
theorem ptendsto'_nhds {f : β →. α} {l : filter β} {a : α} :
ptendsto' f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.preimage s ∈ l) :=
rtendsto'_nhds
theorem tendsto_nhds {f : β → α} {l : filter β} {a : α} :
tendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f ⁻¹' s ∈ l) :=
all_mem_nhds_filter _ _ (λ s t h, preimage_mono h) _
lemma tendsto_const_nhds {a : α} {f : filter β} : tendsto (λb:β, a) f (𝓝 a) :=
tendsto_nhds.mpr $ assume s hs ha, univ_mem_sets' $ assume _, ha
lemma pure_le_nhds : pure ≤ (𝓝 : α → filter α) :=
assume a s hs, mem_pure_sets.2 $ mem_of_nhds hs
lemma tendsto_pure_nhds {α : Type*} [topological_space β] (f : α → β) (a : α) :
tendsto f (pure a) (𝓝 (f a)) :=
(tendsto_pure_pure f a).mono_right (pure_le_nhds _)
lemma order_top.tendsto_at_top_nhds {α : Type*} [order_top α] [topological_space β] (f : α → β) :
tendsto f at_top (𝓝 $ f ⊤) :=
(tendsto_at_top_pure f).mono_right (pure_le_nhds _)
@[simp] instance nhds_ne_bot {a : α} : ne_bot (𝓝 a) :=
ne_bot_of_le (pure_le_nhds a)
/-!
### Cluster points
In this section we define [cluster points](https://en.wikipedia.org/wiki/Limit_point)
(also known as limit points and accumulation points) of a filter and of a sequence.
-/
/-- A point `x` is a cluster point of a filter `F` if 𝓝 x ⊓ F ≠ ⊥. Also known as
an accumulation point or a limit point. -/
def cluster_pt (x : α) (F : filter α) : Prop := ne_bot (𝓝 x ⊓ F)
lemma cluster_pt.ne_bot {x : α} {F : filter α} (h : cluster_pt x F) : ne_bot (𝓝 x ⊓ F) := h
lemma filter.has_basis.cluster_pt_iff {ιa ιF} {pa : ιa → Prop} {sa : ιa → set α}
{pF : ιF → Prop} {sF : ιF → set α} {F : filter α}
(ha : (𝓝 a).has_basis pa sa) (hF : F.has_basis pF sF) :
cluster_pt a F ↔ ∀ ⦃i⦄ (hi : pa i) ⦃j⦄ (hj : pF j), (sa i ∩ sF j).nonempty :=
ha.inf_basis_ne_bot_iff hF
lemma cluster_pt_iff {x : α} {F : filter α} :
cluster_pt x F ↔ ∀ ⦃U : set α⦄ (hU : U ∈ 𝓝 x) ⦃V⦄ (hV : V ∈ F), (U ∩ V).nonempty :=
inf_ne_bot_iff
/-- `x` is a cluster point of a set `s` if every neighbourhood of `x` meets `s` on a nonempty
set. -/
lemma cluster_pt_principal_iff {x : α} {s : set α} :
cluster_pt x (𝓟 s) ↔ ∀ U ∈ 𝓝 x, (U ∩ s).nonempty :=
inf_principal_ne_bot_iff
lemma cluster_pt_principal_iff_frequently {x : α} {s : set α} :
cluster_pt x (𝓟 s) ↔ ∃ᶠ y in 𝓝 x, y ∈ s :=
by simp only [cluster_pt_principal_iff, frequently_iff, set.nonempty, exists_prop, mem_inter_iff]
lemma cluster_pt.of_le_nhds {x : α} {f : filter α} (H : f ≤ 𝓝 x) [ne_bot f] : cluster_pt x f :=
by rwa [cluster_pt, inf_eq_right.mpr H]
lemma cluster_pt.of_le_nhds' {x : α} {f : filter α} (H : f ≤ 𝓝 x) (hf : ne_bot f) :
cluster_pt x f :=
cluster_pt.of_le_nhds H
lemma cluster_pt.of_nhds_le {x : α} {f : filter α} (H : 𝓝 x ≤ f) : cluster_pt x f :=
by simp only [cluster_pt, inf_eq_left.mpr H, nhds_ne_bot]
lemma cluster_pt.mono {x : α} {f g : filter α} (H : cluster_pt x f) (h : f ≤ g) :
cluster_pt x g :=
⟨ne_bot_of_le_ne_bot H.ne $ inf_le_inf_left _ h⟩
lemma cluster_pt.of_inf_left {x : α} {f g : filter α} (H : cluster_pt x $ f ⊓ g) :
cluster_pt x f :=
H.mono inf_le_left
lemma cluster_pt.of_inf_right {x : α} {f g : filter α} (H : cluster_pt x $ f ⊓ g) :
cluster_pt x g :=
H.mono inf_le_right
lemma ultrafilter.cluster_pt_iff {x : α} {f : ultrafilter α} : cluster_pt x f ↔ ↑f ≤ 𝓝 x :=
⟨f.le_of_inf_ne_bot', λ h, cluster_pt.of_le_nhds h⟩
/-- A point `x` is a cluster point of a sequence `u` along a filter `F` if it is a cluster point
of `map u F`. -/
def map_cluster_pt {ι :Type*} (x : α) (F : filter ι) (u : ι → α) : Prop := cluster_pt x (map u F)
lemma map_cluster_pt_iff {ι :Type*} (x : α) (F : filter ι) (u : ι → α) :
map_cluster_pt x F u ↔ ∀ s ∈ 𝓝 x, ∃ᶠ a in F, u a ∈ s :=
by { simp_rw [map_cluster_pt, cluster_pt, inf_ne_bot_iff_frequently_left, frequently_map], refl }
lemma map_cluster_pt_of_comp {ι δ :Type*} {F : filter ι} {φ : δ → ι} {p : filter δ}
{x : α} {u : ι → α} [ne_bot p] (h : tendsto φ p F) (H : tendsto (u ∘ φ) p (𝓝 x)) :
map_cluster_pt x F u :=
begin
have := calc
map (u ∘ φ) p = map u (map φ p) : map_map
... ≤ map u F : map_mono h,
have : map (u ∘ φ) p ≤ 𝓝 x ⊓ map u F,
from le_inf H this,
exact ne_bot_of_le this
end
/-!
### Interior, closure and frontier in terms of neighborhoods
-/
lemma interior_eq_nhds' {s : set α} : interior s = {a | s ∈ 𝓝 a} :=
set.ext $ λ x, by simp only [mem_interior, mem_nhds_sets_iff, mem_set_of_eq]
lemma interior_eq_nhds {s : set α} : interior s = {a | 𝓝 a ≤ 𝓟 s} :=
interior_eq_nhds'.trans $ by simp only [le_principal_iff]
lemma mem_interior_iff_mem_nhds {s : set α} {a : α} :
a ∈ interior s ↔ s ∈ 𝓝 a :=
by rw [interior_eq_nhds', mem_set_of_eq]
@[simp] lemma interior_mem_nhds {s : set α} {a : α} :
interior s ∈ 𝓝 a ↔ s ∈ 𝓝 a :=
⟨λ h, mem_sets_of_superset h interior_subset,
λ h, mem_nhds_sets is_open_interior (mem_interior_iff_mem_nhds.2 h)⟩
lemma interior_set_of_eq {p : α → Prop} :
interior {x | p x} = {x | ∀ᶠ y in 𝓝 x, p y} :=
interior_eq_nhds'
lemma is_open_set_of_eventually_nhds {p : α → Prop} :
is_open {x | ∀ᶠ y in 𝓝 x, p y} :=
by simp only [← interior_set_of_eq, is_open_interior]
lemma subset_interior_iff_nhds {s V : set α} : s ⊆ interior V ↔ ∀ x ∈ s, V ∈ 𝓝 x :=
show (∀ x, x ∈ s → x ∈ _) ↔ _, by simp_rw mem_interior_iff_mem_nhds
lemma is_open_iff_nhds {s : set α} : is_open s ↔ ∀a∈s, 𝓝 a ≤ 𝓟 s :=
calc is_open s ↔ s ⊆ interior s : subset_interior_iff_open.symm
... ↔ (∀a∈s, 𝓝 a ≤ 𝓟 s) : by rw [interior_eq_nhds]; refl
lemma is_open_iff_mem_nhds {s : set α} : is_open s ↔ ∀a∈s, s ∈ 𝓝 a :=
is_open_iff_nhds.trans $ forall_congr $ λ _, imp_congr_right $ λ _, le_principal_iff
theorem is_open_iff_ultrafilter {s : set α} :
is_open s ↔ (∀ (x ∈ s) (l : ultrafilter α), ↑l ≤ 𝓝 x → s ∈ l) :=
by simp_rw [is_open_iff_mem_nhds, ← mem_iff_ultrafilter]
lemma mem_closure_iff_frequently {s : set α} {a : α} : a ∈ closure s ↔ ∃ᶠ x in 𝓝 a, x ∈ s :=
by rw [filter.frequently, filter.eventually, ← mem_interior_iff_mem_nhds,
closure_eq_compl_interior_compl]; refl
alias mem_closure_iff_frequently ↔ _ filter.frequently.mem_closure
/-- The set of cluster points of a filter is closed. In particular, the set of limit points
of a sequence is closed. -/
lemma is_closed_set_of_cluster_pt {f : filter α} : is_closed {x | cluster_pt x f} :=
begin
simp only [cluster_pt, inf_ne_bot_iff_frequently_left, set_of_forall, imp_iff_not_or],
refine is_closed_Inter (λ p, is_closed_union _ _); apply is_closed_compl_iff.2,
exacts [is_open_set_of_eventually_nhds, is_open_const]
end
theorem mem_closure_iff_cluster_pt {s : set α} {a : α} : a ∈ closure s ↔ cluster_pt a (𝓟 s) :=
mem_closure_iff_frequently.trans cluster_pt_principal_iff_frequently.symm
lemma mem_closure_iff_nhds_ne_bot {s : set α} : a ∈ closure s ↔ 𝓝 a ⊓ 𝓟 s ≠ ⊥ :=
mem_closure_iff_cluster_pt.trans ne_bot_iff
lemma closure_eq_cluster_pts {s : set α} : closure s = {a | cluster_pt a (𝓟 s)} :=
set.ext $ λ x, mem_closure_iff_cluster_pt
theorem mem_closure_iff_nhds {s : set α} {a : α} :
a ∈ closure s ↔ ∀ t ∈ 𝓝 a, (t ∩ s).nonempty :=
mem_closure_iff_cluster_pt.trans cluster_pt_principal_iff
theorem mem_closure_iff_nhds' {s : set α} {a : α} :
a ∈ closure s ↔ ∀ t ∈ 𝓝 a, ∃ y : s, ↑y ∈ t :=
by simp only [mem_closure_iff_nhds, set.nonempty_inter_iff_exists_right]
theorem mem_closure_iff_comap_ne_bot {A : set α} {x : α} :
x ∈ closure A ↔ ne_bot (comap (coe : A → α) (𝓝 x)) :=
by simp_rw [mem_closure_iff_nhds, comap_ne_bot_iff, set.nonempty_inter_iff_exists_right]
theorem mem_closure_iff_nhds_basis' {a : α} {p : β → Prop} {s : β → set α} (h : (𝓝 a).has_basis p s)
{t : set α} :
a ∈ closure t ↔ ∀ i, p i → (s i ∩ t).nonempty :=
mem_closure_iff_cluster_pt.trans $ (h.cluster_pt_iff (has_basis_principal _)).trans $
by simp only [exists_prop, forall_const]
theorem mem_closure_iff_nhds_basis {a : α} {p : β → Prop} {s : β → set α} (h : (𝓝 a).has_basis p s)
{t : set α} :
a ∈ closure t ↔ ∀ i, p i → ∃ y ∈ t, y ∈ s i :=
(mem_closure_iff_nhds_basis' h).trans $
by simp only [set.nonempty, mem_inter_eq, exists_prop, and_comm]
/-- `x` belongs to the closure of `s` if and only if some ultrafilter
supported on `s` converges to `x`. -/
lemma mem_closure_iff_ultrafilter {s : set α} {x : α} :
x ∈ closure s ↔ ∃ (u : ultrafilter α), s ∈ u ∧ ↑u ≤ 𝓝 x :=
by simp [closure_eq_cluster_pts, cluster_pt, ← exists_ultrafilter_iff, and.comm]
lemma is_closed_iff_cluster_pt {s : set α} : is_closed s ↔ ∀a, cluster_pt a (𝓟 s) → a ∈ s :=
calc is_closed s ↔ closure s ⊆ s : closure_subset_iff_is_closed.symm
... ↔ (∀a, cluster_pt a (𝓟 s) → a ∈ s) : by simp only [subset_def, mem_closure_iff_cluster_pt]
lemma is_closed_iff_nhds {s : set α} : is_closed s ↔ ∀ x, (∀ U ∈ 𝓝 x, (U ∩ s).nonempty) → x ∈ s :=
by simp_rw [is_closed_iff_cluster_pt, cluster_pt, inf_principal_ne_bot_iff]
lemma closure_inter_open {s t : set α} (h : is_open s) : s ∩ closure t ⊆ closure (s ∩ t) :=
begin
rintro a ⟨hs, ht⟩,
have : s ∈ 𝓝 a := mem_nhds_sets h hs,
rw mem_closure_iff_nhds_ne_bot at ht ⊢,
rwa [← inf_principal, ← inf_assoc, inf_eq_left.2 (le_principal_iff.2 this)],
end
lemma closure_inter_open' {s t : set α} (h : is_open t) : closure s ∩ t ⊆ closure (s ∩ t) :=
by simpa only [inter_comm] using closure_inter_open h
/-- The intersection of an open dense set with a dense set is a dense set. -/
lemma dense.inter_of_open_left {s t : set α} (hs : dense s) (ht : dense t) (hso : is_open s) :
dense (s ∩ t) :=
λ x, (closure_minimal (closure_inter_open hso) is_closed_closure) $
by simp [hs.closure_eq, ht.closure_eq]
/-- The intersection of a dense set with an open dense set is a dense set. -/
lemma dense.inter_of_open_right {s t : set α} (hs : dense s) (ht : dense t) (hto : is_open t) :
dense (s ∩ t) :=
inter_comm t s ▸ ht.inter_of_open_left hs hto
lemma dense.inter_nhds_nonempty {s t : set α} (hs : dense s) {x : α} (ht : t ∈ 𝓝 x) :
(s ∩ t).nonempty :=
let ⟨U, hsub, ho, hx⟩ := mem_nhds_sets_iff.1 ht in
(hs.inter_open_nonempty U ho ⟨x, hx⟩).mono $ λ y hy, ⟨hy.2, hsub hy.1⟩
lemma closure_diff {s t : set α} : closure s \ closure t ⊆ closure (s \ t) :=
calc closure s \ closure t = (closure t)ᶜ ∩ closure s : by simp only [diff_eq, inter_comm]
... ⊆ closure ((closure t)ᶜ ∩ s) : closure_inter_open $ is_open_compl_iff.mpr $ is_closed_closure
... = closure (s \ closure t) : by simp only [diff_eq, inter_comm]
... ⊆ closure (s \ t) : closure_mono $ diff_subset_diff (subset.refl s) subset_closure
lemma filter.frequently.mem_of_closed {a : α} {s : set α} (h : ∃ᶠ x in 𝓝 a, x ∈ s)
(hs : is_closed s) : a ∈ s :=
hs.closure_subset h.mem_closure
lemma is_closed.mem_of_frequently_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α}
(hs : is_closed s) (h : ∃ᶠ x in b, f x ∈ s) (hf : tendsto f b (𝓝 a)) : a ∈ s :=
(hf.frequently $ show ∃ᶠ x in b, (λ y, y ∈ s) (f x), from h).mem_of_closed hs
lemma is_closed.mem_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α}
[ne_bot b] (hs : is_closed s) (hf : tendsto f b (𝓝 a)) (h : ∀ᶠ x in b, f x ∈ s) : a ∈ s :=
hs.mem_of_frequently_of_tendsto h.frequently hf
lemma mem_closure_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α}
[ne_bot b] (hf : tendsto f b (𝓝 a)) (h : ∀ᶠ x in b, f x ∈ s) : a ∈ closure s :=
is_closed_closure.mem_of_tendsto hf $ h.mono (preimage_mono subset_closure)
/-- Suppose that `f` sends the complement to `s` to a single point `a`, and `l` is some filter.
Then `f` tends to `a` along `l` restricted to `s` if and only if it tends to `a` along `l`. -/
lemma tendsto_inf_principal_nhds_iff_of_forall_eq {f : β → α} {l : filter β} {s : set β}
{a : α} (h : ∀ x ∉ s, f x = a) :
tendsto f (l ⊓ 𝓟 s) (𝓝 a) ↔ tendsto f l (𝓝 a) :=
begin
rw [tendsto_iff_comap, tendsto_iff_comap],
replace h : 𝓟 sᶜ ≤ comap f (𝓝 a),
{ rintros U ⟨t, ht, htU⟩ x hx,
have : f x ∈ t, from (h x hx).symm ▸ mem_of_nhds ht,
exact htU this },
refine ⟨λ h', _, le_trans inf_le_left⟩,
have := sup_le h' h,
rw [sup_inf_right, sup_principal, union_compl_self, principal_univ,
inf_top_eq, sup_le_iff] at this,
exact this.1
end
/-!
### Limits of filters in topological spaces
-/
section lim
/-- If `f` is a filter, then `Lim f` is a limit of the filter, if it exists. -/
noncomputable def Lim [nonempty α] (f : filter α) : α := epsilon $ λa, f ≤ 𝓝 a
/--
If `f` is a filter satisfying `ne_bot f`, then `Lim' f` is a limit of the filter, if it exists.
-/
def Lim' (f : filter α) [ne_bot f] : α := @Lim _ _ (nonempty_of_ne_bot f) f
/--
If `F` is an ultrafilter, then `filter.ultrafilter.Lim F` is a limit of the filter, if it exists.
Note that dot notation `F.Lim` can be used for `F : ultrafilter α`.
-/
def ultrafilter.Lim : ultrafilter α → α := λ F, Lim' F
/-- If `f` is a filter in `β` and `g : β → α` is a function, then `lim f` is a limit of `g` at `f`,
if it exists. -/
noncomputable def lim [nonempty α] (f : filter β) (g : β → α) : α :=
Lim (f.map g)
/-- If a filter `f` is majorated by some `𝓝 a`, then it is majorated by `𝓝 (Lim f)`. We formulate
this lemma with a `[nonempty α]` argument of `Lim` derived from `h` to make it useful for types
without a `[nonempty α]` instance. Because of the built-in proof irrelevance, Lean will unify
this instance with any other instance. -/
lemma le_nhds_Lim {f : filter α} (h : ∃a, f ≤ 𝓝 a) : f ≤ 𝓝 (@Lim _ _ (nonempty_of_exists h) f) :=
epsilon_spec h
/-- If `g` tends to some `𝓝 a` along `f`, then it tends to `𝓝 (lim f g)`. We formulate
this lemma with a `[nonempty α]` argument of `lim` derived from `h` to make it useful for types
without a `[nonempty α]` instance. Because of the built-in proof irrelevance, Lean will unify
this instance with any other instance. -/
lemma tendsto_nhds_lim {f : filter β} {g : β → α} (h : ∃ a, tendsto g f (𝓝 a)) :
tendsto g f (𝓝 $ @lim _ _ _ (nonempty_of_exists h) f g) :=
le_nhds_Lim h
end lim
/-!
### Locally finite families
-/
/- locally finite family [General Topology (Bourbaki, 1995)] -/
section locally_finite
/-- A family of sets in `set α` is locally finite if at every point `x:α`,
there is a neighborhood of `x` which meets only finitely many sets in the family -/
def locally_finite (f : β → set α) :=
∀x:α, ∃t ∈ 𝓝 x, finite {i | (f i ∩ t).nonempty }
lemma locally_finite.point_finite {f : β → set α} (hf : locally_finite f) (x : α) :
finite {b | x ∈ f b} :=
let ⟨t, hxt, ht⟩ := hf x in ht.subset $ λ b hb, ⟨x, hb, mem_of_nhds hxt⟩
lemma locally_finite_of_fintype [fintype β] (f : β → set α) : locally_finite f :=
assume x, ⟨univ, univ_mem_sets, finite.of_fintype _⟩
lemma locally_finite.subset
{f₁ f₂ : β → set α} (hf₂ : locally_finite f₂) (hf : ∀b, f₁ b ⊆ f₂ b) : locally_finite f₁ :=
assume a,
let ⟨t, ht₁, ht₂⟩ := hf₂ a in
⟨t, ht₁, ht₂.subset $ assume i hi, hi.mono $ inter_subset_inter (hf i) $ subset.refl _⟩
lemma locally_finite.comp_injective {ι} {f : β → set α} {g : ι → β} (hf : locally_finite f)
(hg : function.injective g) : locally_finite (f ∘ g) :=
λ x, let ⟨t, htx, htf⟩ := hf x in ⟨t, htx, htf.preimage (hg.inj_on _)⟩
lemma locally_finite.closure {f : β → set α} (hf : locally_finite f) :
locally_finite (λ i, closure (f i)) :=
begin
intro x,
rcases hf x with ⟨s, hsx, hsf⟩,
refine ⟨interior s, interior_mem_nhds.2 hsx, hsf.subset $ λ i hi, _⟩,
exact (hi.mono (closure_inter_open' is_open_interior)).of_closure.mono
(inter_subset_inter_right _ interior_subset)
end
lemma locally_finite.is_closed_Union {f : β → set α}
(h₁ : locally_finite f) (h₂ : ∀i, is_closed (f i)) : is_closed (⋃i, f i) :=
is_open_iff_nhds.mpr $ assume a, assume h : a ∉ (⋃i, f i),
have ∀i, a ∈ (f i)ᶜ,
from assume i hi, h $ mem_Union.2 ⟨i, hi⟩,
have ∀i, (f i)ᶜ ∈ (𝓝 a),
by simp only [mem_nhds_sets_iff]; exact assume i, ⟨(f i)ᶜ, subset.refl _, h₂ i, this i⟩,
let ⟨t, h_sets, (h_fin : finite {i | (f i ∩ t).nonempty })⟩ := h₁ a in
calc 𝓝 a ≤ 𝓟 (t ∩ (⋂ i∈{i | (f i ∩ t).nonempty }, (f i)ᶜ)) : by simp *
... ≤ 𝓟 (⋃i, f i)ᶜ :
begin
simp only [principal_mono, subset_def, mem_compl_eq, mem_inter_eq,
mem_Inter, mem_set_of_eq, mem_Union, and_imp, not_exists,
exists_imp_distrib, ne_empty_iff_nonempty, set.nonempty],
exact assume x xt ht i xfi, ht i x xfi xt xfi
end
lemma locally_finite.closure_Union {f : β → set α} (h : locally_finite f) :
closure (⋃ i, f i) = ⋃ i, closure (f i) :=
subset.antisymm
(closure_minimal (Union_subset_Union $ λ _, subset_closure) $
h.closure.is_closed_Union $ λ _, is_closed_closure)
(Union_subset $ λ i, closure_mono $ subset_Union _ _)
end locally_finite
end topological_space
/-!
### Continuity
-/
section continuous
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
variables [topological_space α] [topological_space β] [topological_space γ]
open_locale topological_space
/-- A function between topological spaces is continuous if the preimage
of every open set is open. Registered as a structure to make sure it is not unfolded by Lean. -/
structure continuous (f : α → β) : Prop :=
(is_open_preimage : ∀s, is_open s → is_open (f ⁻¹' s))
lemma continuous_def {f : α → β} : continuous f ↔ (∀s, is_open s → is_open (f ⁻¹' s)) :=
⟨λ hf s hs, hf.is_open_preimage s hs, λ h, ⟨h⟩⟩
lemma is_open.preimage {f : α → β} (hf : continuous f) {s : set β} (h : is_open s) :
is_open (f ⁻¹' s) :=
hf.is_open_preimage s h
/-- A function between topological spaces is continuous at a point `x₀`
if `f x` tends to `f x₀` when `x` tends to `x₀`. -/
def continuous_at (f : α → β) (x : α) := tendsto f (𝓝 x) (𝓝 (f x))
lemma continuous_at.tendsto {f : α → β} {x : α} (h : continuous_at f x) :
tendsto f (𝓝 x) (𝓝 (f x)) :=
h
lemma continuous_at_congr {f g : α → β} {x : α} (h : f =ᶠ[𝓝 x] g) :
continuous_at f x ↔ continuous_at g x :=
by simp only [continuous_at, tendsto_congr' h, h.eq_of_nhds]
lemma continuous_at.congr {f g : α → β} {x : α} (hf : continuous_at f x) (h : f =ᶠ[𝓝 x] g) :
continuous_at g x :=
(continuous_at_congr h).1 hf
lemma continuous_at.preimage_mem_nhds {f : α → β} {x : α} {t : set β} (h : continuous_at f x)
(ht : t ∈ 𝓝 (f x)) : f ⁻¹' t ∈ 𝓝 x :=
h ht
lemma cluster_pt.map {x : α} {la : filter α} {lb : filter β} (H : cluster_pt x la)
{f : α → β} (hfc : continuous_at f x) (hf : tendsto f la lb) :
cluster_pt (f x) lb :=
⟨ne_bot_of_le_ne_bot ((map_ne_bot_iff f).2 H).ne $ hfc.tendsto.inf hf⟩
lemma preimage_interior_subset_interior_preimage {f : α → β} {s : set β}
(hf : continuous f) : f⁻¹' (interior s) ⊆ interior (f⁻¹' s) :=
interior_maximal (preimage_mono interior_subset) (is_open_interior.preimage hf)
lemma continuous_id : continuous (id : α → α) :=
continuous_def.2 $ assume s h, h
lemma continuous.comp {g : β → γ} {f : α → β} (hg : continuous g) (hf : continuous f) :
continuous (g ∘ f) :=
continuous_def.2 $ assume s h, (h.preimage hg).preimage hf
lemma continuous.iterate {f : α → α} (h : continuous f) (n : ℕ) : continuous (f^[n]) :=
nat.rec_on n continuous_id (λ n ihn, ihn.comp h)
lemma continuous_at.comp {g : β → γ} {f : α → β} {x : α}
(hg : continuous_at g (f x)) (hf : continuous_at f x) :
continuous_at (g ∘ f) x :=
hg.comp hf
lemma continuous.tendsto {f : α → β} (hf : continuous f) (x) :
tendsto f (𝓝 x) (𝓝 (f x)) :=
((nhds_basis_opens x).tendsto_iff $ nhds_basis_opens $ f x).2 $
λ t ⟨hxt, ht⟩, ⟨f ⁻¹' t, ⟨hxt, ht.preimage hf⟩, subset.refl _⟩
/-- A version of `continuous.tendsto` that allows one to specify a simpler form of the limit.
E.g., one can write `continuous_exp.tendsto' 0 1 exp_zero`. -/
lemma continuous.tendsto' {f : α → β} (hf : continuous f) (x : α) (y : β) (h : f x = y) :
tendsto f (𝓝 x) (𝓝 y) :=
h ▸ hf.tendsto x
lemma continuous.continuous_at {f : α → β} {x : α} (h : continuous f) :
continuous_at f x :=
h.tendsto x
lemma continuous_iff_continuous_at {f : α → β} : continuous f ↔ ∀ x, continuous_at f x :=
⟨continuous.tendsto,
assume hf : ∀x, tendsto f (𝓝 x) (𝓝 (f x)),
continuous_def.2 $
assume s, assume hs : is_open s,
have ∀a, f a ∈ s → s ∈ 𝓝 (f a),
from λ a ha, mem_nhds_sets hs ha,
show is_open (f ⁻¹' s),
from is_open_iff_nhds.2 $ λ a ha, le_principal_iff.2 $ hf _ (this a ha)⟩
lemma continuous_at_const {x : α} {b : β} : continuous_at (λ a:α, b) x :=
tendsto_const_nhds
lemma continuous_const {b : β} : continuous (λa:α, b) :=
continuous_iff_continuous_at.mpr $ assume a, continuous_at_const
lemma continuous_at_id {x : α} : continuous_at id x :=
continuous_id.continuous_at
lemma continuous_at.iterate {f : α → α} {x : α} (hf : continuous_at f x) (hx : f x = x) (n : ℕ) :
continuous_at (f^[n]) x :=
nat.rec_on n continuous_at_id $ λ n ihn,
show continuous_at (f^[n] ∘ f) x,
from continuous_at.comp (hx.symm ▸ ihn) hf
lemma continuous_iff_is_closed {f : α → β} :
continuous f ↔ (∀s, is_closed s → is_closed (f ⁻¹' s)) :=
⟨assume hf s hs, continuous_def.1 hf sᶜ hs,
assume hf, continuous_def.2 $ assume s,
by rw [←is_closed_compl_iff, ←is_closed_compl_iff]; exact hf _⟩
lemma is_closed.preimage {f : α → β} (hf : continuous f) {s : set β} (h : is_closed s) :
is_closed (f ⁻¹' s) :=
continuous_iff_is_closed.mp hf s h
lemma continuous_at_iff_ultrafilter {f : α → β} {x} : continuous_at f x ↔
∀ g : ultrafilter α, ↑g ≤ 𝓝 x → tendsto f g (𝓝 (f x)) :=
tendsto_iff_ultrafilter f (𝓝 x) (𝓝 (f x))
lemma continuous_iff_ultrafilter {f : α → β} :
continuous f ↔ ∀ x (g : ultrafilter α), ↑g ≤ 𝓝 x → tendsto f g (𝓝 (f x)) :=
by simp only [continuous_iff_continuous_at, continuous_at_iff_ultrafilter]
/-! ### Continuity and partial functions -/
/-- Continuity of a partial function -/
def pcontinuous (f : α →. β) := ∀ s, is_open s → is_open (f.preimage s)
lemma open_dom_of_pcontinuous {f : α →. β} (h : pcontinuous f) : is_open f.dom :=
by rw [←pfun.preimage_univ]; exact h _ is_open_univ
lemma pcontinuous_iff' {f : α →. β} :
pcontinuous f ↔ ∀ {x y} (h : y ∈ f x), ptendsto' f (𝓝 x) (𝓝 y) :=
begin
split,
{ intros h x y h',
simp only [ptendsto'_def, mem_nhds_sets_iff],
rintros s ⟨t, tsubs, opent, yt⟩,
exact ⟨f.preimage t, pfun.preimage_mono _ tsubs, h _ opent, ⟨y, yt, h'⟩⟩
},
intros hf s os,
rw is_open_iff_nhds,
rintros x ⟨y, ys, fxy⟩ t,
rw [mem_principal_sets],
assume h : f.preimage s ⊆ t,
change t ∈ 𝓝 x,
apply mem_sets_of_superset _ h,
have h' : ∀ s ∈ 𝓝 y, f.preimage s ∈ 𝓝 x,
{ intros s hs,
have : ptendsto' f (𝓝 x) (𝓝 y) := hf fxy,
rw ptendsto'_def at this,
exact this s hs },
show f.preimage s ∈ 𝓝 x,
apply h', rw mem_nhds_sets_iff, exact ⟨s, set.subset.refl _, os, ys⟩
end
/-- If a continuous map `f` maps `s` to `t`, then it maps `closure s` to `closure t`. -/
lemma set.maps_to.closure {s : set α} {t : set β} {f : α → β} (h : maps_to f s t)
(hc : continuous f) : maps_to f (closure s) (closure t) :=
begin
simp only [maps_to, mem_closure_iff_cluster_pt],
exact λ x hx, hx.map hc.continuous_at (tendsto_principal_principal.2 h)
end
lemma image_closure_subset_closure_image {f : α → β} {s : set α} (h : continuous f) :
f '' closure s ⊆ closure (f '' s) :=
((maps_to_image f s).closure h).image_subset
lemma map_mem_closure {s : set α} {t : set β} {f : α → β} {a : α}
(hf : continuous f) (ha : a ∈ closure s) (ht : ∀a∈s, f a ∈ t) : f a ∈ closure t :=
set.maps_to.closure ht hf ha
/-!
### Function with dense range
-/
section dense_range
variables {κ ι : Type*} (f : κ → β) (g : β → γ)
/-- `f : ι → β` has dense range if its range (image) is a dense subset of β. -/
def dense_range := dense (range f)
variables {f}
/-- A surjective map has dense range. -/
lemma function.surjective.dense_range (hf : function.surjective f) : dense_range f :=
λ x, by simp [hf.range_eq]
lemma dense_range_iff_closure_range : dense_range f ↔ closure (range f) = univ :=
dense_iff_closure_eq
lemma dense_range.closure_range (h : dense_range f) : closure (range f) = univ :=
h.closure_eq
lemma continuous.range_subset_closure_image_dense {f : α → β} (hf : continuous f)
{s : set α} (hs : dense s) :
range f ⊆ closure (f '' s) :=
by { rw [← image_univ, ← hs.closure_eq], exact image_closure_subset_closure_image hf }
/-- The image of a dense set under a continuous map with dense range is a dense set. -/
lemma dense_range.dense_image {f : α → β} (hf' : dense_range f) (hf : continuous f)
{s : set α} (hs : dense s) :
dense (f '' s) :=
(hf'.mono $ hf.range_subset_closure_image_dense hs).of_closure
/-- If a continuous map with dense range maps a dense set to a subset of `t`, then `t` is a dense
set. -/
lemma dense_range.dense_of_maps_to {f : α → β} (hf' : dense_range f) (hf : continuous f)
{s : set α} (hs : dense s) {t : set β} (ht : maps_to f s t) :
dense t :=
(hf'.dense_image hf hs).mono ht.image_subset
/-- Composition of a continuous map with dense range and a function with dense range has dense
range. -/
lemma dense_range.comp {g : β → γ} {f : κ → β} (hg : dense_range g) (hf : dense_range f)
(cg : continuous g) :
dense_range (g ∘ f) :=
by { rw [dense_range, range_comp], exact hg.dense_image cg hf }
lemma dense_range.nonempty_iff (hf : dense_range f) : nonempty κ ↔ nonempty β :=
range_nonempty_iff_nonempty.symm.trans hf.nonempty_iff
lemma dense_range.nonempty [h : nonempty β] (hf : dense_range f) : nonempty κ :=
hf.nonempty_iff.mpr h
/-- Given a function `f : α → β` with dense range and `b : β`, returns some `a : α`. -/
def dense_range.some (hf : dense_range f) (b : β) : κ :=
classical.choice $ hf.nonempty_iff.mpr ⟨b⟩
end dense_range
end continuous
|
3d573ffd22adde58ef04fc63cc0df7b67604e1c2 | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /tests/lean/run/pickle1.lean | b2916d846955db8a3ca495947f4fd6c670bf1e79 | [
"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 | 204 | lean | import data.encodable
open encodable decidable bool prod list nat option
variable l : list (nat × bool)
check encode l
eval encode [2, 1]
example : decode (list nat) (encode [1, 1]) = some [1, 1] :=
rfl
|
01f4dd3cfa56e1ee602b33d186a04c688089fa64 | d29d82a0af640c937e499f6be79fc552eae0aa13 | /src/ring_theory/roots_of_unity.lean | e0322a9cd66c8f67adf0a80b22b5c7bc108c4829 | [
"Apache-2.0"
] | permissive | AbdulMajeedkhurasani/mathlib | 835f8a5c5cf3075b250b3737172043ab4fa1edf6 | 79bc7323b164aebd000524ebafd198eb0e17f956 | refs/heads/master | 1,688,003,895,660 | 1,627,788,521,000 | 1,627,788,521,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 40,422 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import data.nat.parity
import data.polynomial.ring_division
import group_theory.specific_groups.cyclic
import ring_theory.integral_domain
import number_theory.divisors
import data.zmod.basic
import tactic.zify
import field_theory.separable
import field_theory.finite.basic
/-!
# Roots of unity and primitive roots of unity
We define roots of unity in the context of an arbitrary commutative monoid,
as a subgroup of the group of units. We also define a predicate `is_primitive_root` on commutative
monoids, expressing that an element is a primitive root of unity.
## Main definitions
* `roots_of_unity n M`, for `n : ℕ+` is the subgroup of the units of a commutative monoid `M`
consisting of elements `x` that satisfy `x ^ n = 1`.
* `is_primitive_root ζ k`: an element `ζ` is a primitive `k`-th root of unity if `ζ ^ k = 1`,
and if `l` satisfies `ζ ^ l = 1` then `k ∣ l`.
* `primitive_roots k R`: the finset of primitive `k`-th roots of unity in an integral domain `R`.
## Main results
* `roots_of_unity.is_cyclic`: the roots of unity in an integral domain form a cyclic group.
* `is_primitive_root.zmod_equiv_gpowers`: `zmod k` is equivalent to
the subgroup generated by a primitive `k`-th root of unity.
* `is_primitive_root.gpowers_eq`: in an integral domain, the subgroup generated by
a primitive `k`-th root of unity is equal to the `k`-th roots of unity.
* `is_primitive_root.card_primitive_roots`: if an integral domain
has a primitive `k`-th root of unity, then it has `φ k` of them.
## Implementation details
It is desirable that `roots_of_unity` is a subgroup,
and it will mainly be applied to rings (e.g. the ring of integers in a number field) and fields.
We therefore implement it as a subgroup of the units of a commutative monoid.
We have chosen to define `roots_of_unity n` for `n : ℕ+`, instead of `n : ℕ`,
because almost all lemmas need the positivity assumption,
and in particular the type class instances for `fintype` and `is_cyclic`.
On the other hand, for primitive roots of unity, it is desirable to have a predicate
not just on units, but directly on elements of the ring/field.
For example, we want to say that `exp (2 * pi * I / n)` is a primitive `n`-th root of unity
in the complex numbers, without having to turn that number into a unit first.
This creates a little bit of friction, but lemmas like `is_primitive_root.is_unit` and
`is_primitive_root.coe_units_iff` should provide the necessary glue.
-/
open_locale classical big_operators
noncomputable theory
open polynomial
open finset
variables {M N G G₀ R S : Type*}
variables [comm_monoid M] [comm_monoid N] [comm_group G] [comm_group_with_zero G₀]
variables [integral_domain R] [integral_domain S]
section roots_of_unity
variables {k l : ℕ+}
/-- `roots_of_unity k M` is the subgroup of elements `m : units M` that satisfy `m ^ k = 1` -/
def roots_of_unity (k : ℕ+) (M : Type*) [comm_monoid M] : subgroup (units M) :=
{ carrier := { ζ | ζ ^ (k : ℕ) = 1 },
one_mem' := one_pow _,
mul_mem' := λ ζ ξ hζ hξ, by simp only [*, set.mem_set_of_eq, mul_pow, one_mul] at *,
inv_mem' := λ ζ hζ, by simp only [*, set.mem_set_of_eq, inv_pow, one_inv] at * }
@[simp] lemma mem_roots_of_unity (k : ℕ+) (ζ : units M) :
ζ ∈ roots_of_unity k M ↔ ζ ^ (k : ℕ) = 1 := iff.rfl
lemma roots_of_unity_le_of_dvd (h : k ∣ l) : roots_of_unity k M ≤ roots_of_unity l M :=
begin
obtain ⟨d, rfl⟩ := h,
intros ζ h,
simp only [mem_roots_of_unity, pnat.mul_coe, pow_mul, one_pow, *] at *,
end
lemma map_roots_of_unity (f : units M →* units N) (k : ℕ+) :
(roots_of_unity k M).map f ≤ roots_of_unity k N :=
begin
rintros _ ⟨ζ, h, rfl⟩,
simp only [←monoid_hom.map_pow, *, mem_roots_of_unity, set_like.mem_coe, monoid_hom.map_one] at *
end
lemma mem_roots_of_unity_iff_mem_nth_roots {ζ : units R} :
ζ ∈ roots_of_unity k R ↔ (ζ : R) ∈ nth_roots k (1 : R) :=
by simp only [mem_roots_of_unity, mem_nth_roots k.pos, units.ext_iff, units.coe_one, units.coe_pow]
variables (k R)
/-- Equivalence between the `k`-th roots of unity in `R` and the `k`-th roots of `1`.
This is implemented as equivalence of subtypes,
because `roots_of_unity` is a subgroup of the group of units,
whereas `nth_roots` is a multiset. -/
def roots_of_unity_equiv_nth_roots :
roots_of_unity k R ≃ {x // x ∈ nth_roots k (1 : R)} :=
begin
refine
{ to_fun := λ x, ⟨x, mem_roots_of_unity_iff_mem_nth_roots.mp x.2⟩,
inv_fun := λ x, ⟨⟨x, x ^ (k - 1 : ℕ), _, _⟩, _⟩,
left_inv := _,
right_inv := _ },
swap 4, { rintro ⟨x, hx⟩, ext, refl },
swap 4, { rintro ⟨x, hx⟩, ext, refl },
all_goals
{ rcases x with ⟨x, hx⟩, rw [mem_nth_roots k.pos] at hx,
simp only [subtype.coe_mk, ← pow_succ, ← pow_succ', hx,
nat.sub_add_cancel (show 1 ≤ (k : ℕ), from k.one_le)] },
{ show (_ : units R) ^ (k : ℕ) = 1,
simp only [units.ext_iff, hx, units.coe_mk, units.coe_one, subtype.coe_mk, units.coe_pow] }
end
variables {k R}
@[simp] lemma roots_of_unity_equiv_nth_roots_apply (x : roots_of_unity k R) :
(roots_of_unity_equiv_nth_roots R k x : R) = x :=
rfl
@[simp] lemma roots_of_unity_equiv_nth_roots_symm_apply (x : {x // x ∈ nth_roots k (1 : R)}) :
((roots_of_unity_equiv_nth_roots R k).symm x : R) = x :=
rfl
variables (k R)
instance roots_of_unity.fintype : fintype (roots_of_unity k R) :=
fintype.of_equiv {x // x ∈ nth_roots k (1 : R)} $ (roots_of_unity_equiv_nth_roots R k).symm
instance roots_of_unity.is_cyclic : is_cyclic (roots_of_unity k R) :=
is_cyclic_of_subgroup_integral_domain ((units.coe_hom R).comp (roots_of_unity k R).subtype)
(units.ext.comp subtype.val_injective)
lemma card_roots_of_unity : fintype.card (roots_of_unity k R) ≤ k :=
calc fintype.card (roots_of_unity k R)
= fintype.card {x // x ∈ nth_roots k (1 : R)} :
fintype.card_congr (roots_of_unity_equiv_nth_roots R k)
... ≤ (nth_roots k (1 : R)).attach.card : multiset.card_le_of_le (multiset.erase_dup_le _)
... = (nth_roots k (1 : R)).card : multiset.card_attach
... ≤ k : card_nth_roots k 1
variables {k R}
@[norm_cast]
lemma roots_of_unity.coe_pow (ζ : roots_of_unity k R) (m : ℕ) : ↑(ζ ^ m) = (ζ ^ m : R) :=
begin
change ↑(↑(ζ ^ m) : units R) = ↑(ζ : units R) ^ m,
rw [subgroup.coe_pow, units.coe_pow],
end
/-- Restrict a ring homomorphism between integral domains to the nth roots of unity -/
def ring_hom.restrict_roots_of_unity (σ : R →+* S) (n : ℕ+) :
roots_of_unity n R →* roots_of_unity n S :=
let h : ∀ ξ : roots_of_unity n R, (σ ξ) ^ (n : ℕ) = 1 := λ ξ, by
{ change (σ (ξ : units R)) ^ (n : ℕ) = 1,
rw [←σ.map_pow, ←units.coe_pow, show ((ξ : units R) ^ (n : ℕ) = 1), from ξ.2,
units.coe_one, σ.map_one] } in
{ to_fun := λ ξ, ⟨@unit_of_invertible _ _ _ (invertible_of_pow_eq_one _ _ (h ξ) n.2),
by { ext, rw units.coe_pow, exact h ξ }⟩,
map_one' := by { ext, exact σ.map_one },
map_mul' := λ ξ₁ ξ₂, by { ext, rw [subgroup.coe_mul, units.coe_mul], exact σ.map_mul _ _ } }
@[simp] lemma ring_hom.restrict_roots_of_unity_coe_apply (σ : R →+* S) (ζ : roots_of_unity k R) :
↑(σ.restrict_roots_of_unity k ζ) = σ ↑ζ :=
rfl
/-- Restrict a ring isomorphism between integral domains to the nth roots of unity -/
def ring_equiv.restrict_roots_of_unity (σ : R ≃+* S) (n : ℕ+) :
roots_of_unity n R ≃* roots_of_unity n S :=
{ to_fun := σ.to_ring_hom.restrict_roots_of_unity n,
inv_fun := σ.symm.to_ring_hom.restrict_roots_of_unity n,
left_inv := λ ξ, by { ext, exact σ.symm_apply_apply ξ },
right_inv := λ ξ, by { ext, exact σ.apply_symm_apply ξ },
map_mul' := (σ.to_ring_hom.restrict_roots_of_unity n).map_mul }
@[simp] lemma ring_equiv.restrict_roots_of_unity_coe_apply (σ : R ≃+* S) (ζ : roots_of_unity k R) :
↑(σ.restrict_roots_of_unity k ζ) = σ ↑ζ :=
rfl
@[simp] lemma ring_equiv.restrict_roots_of_unity_symm (σ : R ≃+* S) :
(σ.restrict_roots_of_unity k).symm = σ.symm.restrict_roots_of_unity k :=
rfl
lemma ring_hom.map_root_of_unity_eq_pow_self (σ : R →+* R) (ζ : roots_of_unity k R) :
∃ m : ℕ, σ ζ = ζ ^ m :=
begin
obtain ⟨m, hm⟩ := (σ.restrict_roots_of_unity k).map_cyclic,
rw [←σ.restrict_roots_of_unity_coe_apply, hm, gpow_eq_mod_order_of, ←int.to_nat_of_nonneg
(m.mod_nonneg (int.coe_nat_ne_zero.mpr (pos_iff_ne_zero.mp (order_of_pos ζ)))),
gpow_coe_nat, roots_of_unity.coe_pow],
exact ⟨(m % (order_of ζ)).to_nat, rfl⟩,
end
end roots_of_unity
/-- An element `ζ` is a primitive `k`-th root of unity if `ζ ^ k = 1`,
and if `l` satisfies `ζ ^ l = 1` then `k ∣ l`. -/
structure is_primitive_root (ζ : M) (k : ℕ) : Prop :=
(pow_eq_one : ζ ^ (k : ℕ) = 1)
(dvd_of_pow_eq_one : ∀ l : ℕ, ζ ^ l = 1 → k ∣ l)
section primitive_roots
variables {k : ℕ}
/-- `primitive_roots k R` is the finset of primitive `k`-th roots of unity
in the integral domain `R`. -/
def primitive_roots (k : ℕ) (R : Type*) [integral_domain R] : finset R :=
(nth_roots k (1 : R)).to_finset.filter (λ ζ, is_primitive_root ζ k)
@[simp] lemma mem_primitive_roots {ζ : R} (h0 : 0 < k) :
ζ ∈ primitive_roots k R ↔ is_primitive_root ζ k :=
begin
rw [primitive_roots, mem_filter, multiset.mem_to_finset, mem_nth_roots h0, and_iff_right_iff_imp],
exact is_primitive_root.pow_eq_one
end
end primitive_roots
namespace is_primitive_root
variables {k l : ℕ}
lemma iff_def (ζ : M) (k : ℕ) :
is_primitive_root ζ k ↔ (ζ ^ k = 1) ∧ (∀ l : ℕ, ζ ^ l = 1 → k ∣ l) :=
⟨λ ⟨h1, h2⟩, ⟨h1, h2⟩, λ ⟨h1, h2⟩, ⟨h1, h2⟩⟩
lemma mk_of_lt (ζ : M) (hk : 0 < k) (h1 : ζ ^ k = 1) (h : ∀ l : ℕ, 0 < l → l < k → ζ ^ l ≠ 1) :
is_primitive_root ζ k :=
begin
refine ⟨h1, _⟩,
intros l hl,
apply dvd_trans _ (k.gcd_dvd_right l),
suffices : k.gcd l = k, { rw this },
rw eq_iff_le_not_lt,
refine ⟨nat.le_of_dvd hk (k.gcd_dvd_left l), _⟩,
intro h', apply h _ (nat.gcd_pos_of_pos_left _ hk) h',
exact pow_gcd_eq_one _ h1 hl
end
section comm_monoid
variables {ζ : M} (h : is_primitive_root ζ k)
lemma pow_eq_one_iff_dvd (l : ℕ) : ζ ^ l = 1 ↔ k ∣ l :=
⟨h.dvd_of_pow_eq_one l,
by { rintro ⟨i, rfl⟩, simp only [pow_mul, h.pow_eq_one, one_pow, pnat.mul_coe] }⟩
lemma is_unit (h : is_primitive_root ζ k) (h0 : 0 < k) : is_unit ζ :=
begin
apply is_unit_of_mul_eq_one ζ (ζ ^ (k - 1)),
rw [← pow_succ, nat.sub_add_cancel h0, h.pow_eq_one]
end
lemma pow_ne_one_of_pos_of_lt (h0 : 0 < l) (hl : l < k) : ζ ^ l ≠ 1 :=
mt (nat.le_of_dvd h0 ∘ h.dvd_of_pow_eq_one _) $ not_le_of_lt hl
lemma pow_inj (h : is_primitive_root ζ k) ⦃i j : ℕ⦄ (hi : i < k) (hj : j < k) (H : ζ ^ i = ζ ^ j) :
i = j :=
begin
wlog hij : i ≤ j,
apply le_antisymm hij,
rw ← nat.sub_eq_zero_iff_le,
apply nat.eq_zero_of_dvd_of_lt _ (lt_of_le_of_lt (nat.sub_le_self _ _) hj),
apply h.dvd_of_pow_eq_one,
rw [← ((h.is_unit (lt_of_le_of_lt (nat.zero_le _) hi)).pow i).mul_left_inj,
← pow_add, nat.sub_add_cancel hij, H, one_mul]
end
lemma one : is_primitive_root (1 : M) 1 :=
{ pow_eq_one := pow_one _,
dvd_of_pow_eq_one := λ l hl, one_dvd _ }
@[simp] lemma one_right_iff : is_primitive_root ζ 1 ↔ ζ = 1 :=
begin
split,
{ intro h, rw [← pow_one ζ, h.pow_eq_one] },
{ rintro rfl, exact one }
end
@[simp] lemma coe_units_iff {ζ : units M} :
is_primitive_root (ζ : M) k ↔ is_primitive_root ζ k :=
by simp only [iff_def, units.ext_iff, units.coe_pow, units.coe_one]
lemma pow_of_coprime (h : is_primitive_root ζ k) (i : ℕ) (hi : i.coprime k) :
is_primitive_root (ζ ^ i) k :=
begin
by_cases h0 : k = 0,
{ subst k, simp only [*, pow_one, nat.coprime_zero_right] at * },
rcases h.is_unit (nat.pos_of_ne_zero h0) with ⟨ζ, rfl⟩,
rw [← units.coe_pow],
rw coe_units_iff at h ⊢,
refine
{ pow_eq_one := by rw [← pow_mul', pow_mul, h.pow_eq_one, one_pow],
dvd_of_pow_eq_one := _ },
intros l hl,
apply h.dvd_of_pow_eq_one,
rw [← pow_one ζ, ← gpow_coe_nat ζ, ← hi.gcd_eq_one, nat.gcd_eq_gcd_ab, gpow_add,
mul_pow, ← gpow_coe_nat, ← gpow_mul, mul_right_comm],
simp only [gpow_mul, hl, h.pow_eq_one, one_gpow, one_pow, one_mul, gpow_coe_nat]
end
lemma pow_of_prime (h : is_primitive_root ζ k) {p : ℕ} (hprime : nat.prime p) (hdiv : ¬ p ∣ k) :
is_primitive_root (ζ ^ p) k :=
h.pow_of_coprime p (hprime.coprime_iff_not_dvd.2 hdiv)
lemma pow_iff_coprime (h : is_primitive_root ζ k) (h0 : 0 < k) (i : ℕ) :
is_primitive_root (ζ ^ i) k ↔ i.coprime k :=
begin
refine ⟨_, h.pow_of_coprime i⟩,
intro hi,
obtain ⟨a, ha⟩ := i.gcd_dvd_left k,
obtain ⟨b, hb⟩ := i.gcd_dvd_right k,
suffices : b = k,
{ rwa [this, ← one_mul k, nat.mul_left_inj h0, eq_comm] at hb { occs := occurrences.pos [1] } },
rw [ha] at hi,
rw [mul_comm] at hb,
apply nat.dvd_antisymm ⟨i.gcd k, hb⟩ (hi.dvd_of_pow_eq_one b _),
rw [← pow_mul', ← mul_assoc, ← hb, pow_mul, h.pow_eq_one, one_pow]
end
end comm_monoid
section comm_group
variables {ζ : G}
lemma gpow_eq_one (h : is_primitive_root ζ k) : ζ ^ (k : ℤ) = 1 :=
by { rw gpow_coe_nat, exact h.pow_eq_one }
lemma gpow_eq_one_iff_dvd (h : is_primitive_root ζ k) (l : ℤ) :
ζ ^ l = 1 ↔ (k : ℤ) ∣ l :=
begin
by_cases h0 : 0 ≤ l,
{ lift l to ℕ using h0, rw [gpow_coe_nat], norm_cast, exact h.pow_eq_one_iff_dvd l },
{ have : 0 ≤ -l, { simp only [not_le, neg_nonneg] at h0 ⊢, exact le_of_lt h0 },
lift -l to ℕ using this with l' hl',
rw [← dvd_neg, ← hl'],
norm_cast,
rw [← h.pow_eq_one_iff_dvd, ← inv_inj, ← gpow_neg, ← hl', gpow_coe_nat, one_inv] }
end
lemma inv (h : is_primitive_root ζ k) : is_primitive_root ζ⁻¹ k :=
{ pow_eq_one := by simp only [h.pow_eq_one, one_inv, eq_self_iff_true, inv_pow],
dvd_of_pow_eq_one :=
begin
intros l hl,
apply h.dvd_of_pow_eq_one l,
rw [← inv_inj, ← inv_pow, hl, one_inv]
end }
@[simp] lemma inv_iff : is_primitive_root ζ⁻¹ k ↔ is_primitive_root ζ k :=
by { refine ⟨_, λ h, inv h⟩, intro h, rw [← inv_inv ζ], exact inv h }
lemma gpow_of_gcd_eq_one (h : is_primitive_root ζ k) (i : ℤ) (hi : i.gcd k = 1) :
is_primitive_root (ζ ^ i) k :=
begin
by_cases h0 : 0 ≤ i,
{ lift i to ℕ using h0,
rw gpow_coe_nat,
exact h.pow_of_coprime i hi },
have : 0 ≤ -i, { simp only [not_le, neg_nonneg] at h0 ⊢, exact le_of_lt h0 },
lift -i to ℕ using this with i' hi',
rw [← inv_iff, ← gpow_neg, ← hi', gpow_coe_nat],
apply h.pow_of_coprime,
rw [int.gcd, ← int.nat_abs_neg, ← hi'] at hi,
exact hi
end
@[simp] lemma coe_subgroup_iff (H : subgroup G) {ζ : H} :
is_primitive_root (ζ : G) k ↔ is_primitive_root ζ k :=
by simp only [iff_def, ← subgroup.coe_pow, ← H.coe_one, ← subtype.ext_iff]
end comm_group
section comm_group_with_zero
variables {ζ : G₀}
lemma fpow_eq_one (h : is_primitive_root ζ k) : ζ ^ (k : ℤ) = 1 :=
by { rw gpow_coe_nat, exact h.pow_eq_one }
lemma fpow_eq_one_iff_dvd (h : is_primitive_root ζ k) (l : ℤ) :
ζ ^ l = 1 ↔ (k : ℤ) ∣ l :=
begin
by_cases h0 : 0 ≤ l,
{ lift l to ℕ using h0, rw [gpow_coe_nat], norm_cast, exact h.pow_eq_one_iff_dvd l },
{ have : 0 ≤ -l, { simp only [not_le, neg_nonneg] at h0 ⊢, exact le_of_lt h0 },
lift -l to ℕ using this with l' hl',
rw [← dvd_neg, ← hl'],
norm_cast,
rw [← h.pow_eq_one_iff_dvd, ← inv_inj', ← fpow_neg, ← hl', gpow_coe_nat, inv_one] }
end
lemma inv' (h : is_primitive_root ζ k) : is_primitive_root ζ⁻¹ k :=
{ pow_eq_one := by simp only [h.pow_eq_one, inv_one, eq_self_iff_true, inv_pow'],
dvd_of_pow_eq_one :=
begin
intros l hl,
apply h.dvd_of_pow_eq_one l,
rw [← inv_inj', ← inv_pow', hl, inv_one]
end }
@[simp] lemma inv_iff' : is_primitive_root ζ⁻¹ k ↔ is_primitive_root ζ k :=
by { refine ⟨_, λ h, inv' h⟩, intro h, rw [← inv_inv' ζ], exact inv' h }
lemma fpow_of_gcd_eq_one (h : is_primitive_root ζ k) (i : ℤ) (hi : i.gcd k = 1) :
is_primitive_root (ζ ^ i) k :=
begin
by_cases h0 : 0 ≤ i,
{ lift i to ℕ using h0,
rw gpow_coe_nat,
exact h.pow_of_coprime i hi },
have : 0 ≤ -i, { simp only [not_le, neg_nonneg] at h0 ⊢, exact le_of_lt h0 },
lift -i to ℕ using this with i' hi',
rw [← inv_iff', ← fpow_neg, ← hi', gpow_coe_nat],
apply h.pow_of_coprime,
rw [int.gcd, ← int.nat_abs_neg, ← hi'] at hi,
exact hi
end
end comm_group_with_zero
section integral_domain
variables {ζ : R}
@[simp] lemma primitive_roots_zero : primitive_roots 0 R = ∅ :=
begin
rw [← finset.val_eq_zero, ← multiset.subset_zero, ← nth_roots_zero (1 : R), primitive_roots],
simp only [finset.not_mem_empty, forall_const, forall_prop_of_false, multiset.to_finset_zero,
finset.filter_true_of_mem, finset.empty_val, not_false_iff,
multiset.zero_subset, nth_roots_zero]
end
@[simp] lemma primitive_roots_one : primitive_roots 1 R = {(1 : R)} :=
begin
apply finset.eq_singleton_iff_unique_mem.2,
split,
{ simp only [is_primitive_root.one_right_iff, mem_primitive_roots zero_lt_one] },
{ intros x hx,
rw [mem_primitive_roots zero_lt_one, is_primitive_root.one_right_iff] at hx,
exact hx }
end
lemma neg_one (p : ℕ) [char_p R p] (hp : p ≠ 2) : is_primitive_root (-1 : R) 2 :=
mk_of_lt (-1 : R) dec_trivial (by simp only [one_pow, neg_sq]) $
begin
intros l hl0 hl2,
obtain rfl : l = 1,
{ unfreezingI { clear_dependent R p }, dec_trivial! },
simp only [pow_one, ne.def],
intro h,
suffices h2 : p ∣ 2,
{ have := char_p.char_ne_one R p,
unfreezingI { clear_dependent R },
have aux := nat.le_of_dvd dec_trivial h2,
revert this hp h2, revert p, dec_trivial },
simp only [← char_p.cast_eq_zero_iff R p, nat.cast_bit0, nat.cast_one],
rw [bit0, ← h, neg_add_self] { occs := occurrences.pos [1] }
end
lemma eq_neg_one_of_two_right (h : is_primitive_root ζ 2) : ζ = -1 :=
begin
apply (eq_or_eq_neg_of_sq_eq_sq ζ 1 _).resolve_left,
{ rw [← pow_one ζ], apply h.pow_ne_one_of_pos_of_lt; dec_trivial },
{ simp only [h.pow_eq_one, one_pow] }
end
end integral_domain
section integral_domain
variables {ζ : units R} (h : is_primitive_root ζ k)
protected
lemma mem_roots_of_unity {n : ℕ+} (h : is_primitive_root ζ n) : ζ ∈ roots_of_unity n R :=
h.pow_eq_one
/-- The (additive) monoid equivalence between `zmod k`
and the powers of a primitive root of unity `ζ`. -/
def zmod_equiv_gpowers (h : is_primitive_root ζ k) : zmod k ≃+ additive (subgroup.gpowers ζ) :=
add_equiv.of_bijective
(add_monoid_hom.lift_of_right_inverse (int.cast_add_hom $ zmod k) _ zmod.int_cast_right_inverse
⟨{ to_fun := λ i, additive.of_mul (⟨_, i, rfl⟩ : subgroup.gpowers ζ),
map_zero' := by { simp only [gpow_zero], refl },
map_add' := by { intros i j, simp only [gpow_add], refl } },
(λ i hi,
begin
simp only [add_monoid_hom.mem_ker, char_p.int_cast_eq_zero_iff (zmod k) k,
add_monoid_hom.coe_mk, int.coe_cast_add_hom] at hi ⊢,
obtain ⟨i, rfl⟩ := hi,
simp only [gpow_mul, h.pow_eq_one, one_gpow, gpow_coe_nat],
refl
end)⟩)
begin
split,
{ rw add_monoid_hom.injective_iff,
intros i hi,
rw subtype.ext_iff at hi,
have := (h.gpow_eq_one_iff_dvd _).mp hi,
rw [← (char_p.int_cast_eq_zero_iff (zmod k) k _).mpr this, eq_comm],
exact zmod.int_cast_right_inverse i },
{ rintro ⟨ξ, i, rfl⟩,
refine ⟨int.cast_add_hom _ i, _⟩,
rw [add_monoid_hom.lift_of_right_inverse_comp_apply],
refl }
end
@[simp] lemma zmod_equiv_gpowers_apply_coe_int (i : ℤ) :
h.zmod_equiv_gpowers i = additive.of_mul (⟨ζ ^ i, i, rfl⟩ : subgroup.gpowers ζ) :=
add_monoid_hom.lift_of_right_inverse_comp_apply _ _ zmod.int_cast_right_inverse _ _
@[simp] lemma zmod_equiv_gpowers_apply_coe_nat (i : ℕ) :
h.zmod_equiv_gpowers i = additive.of_mul (⟨ζ ^ i, i, rfl⟩ : subgroup.gpowers ζ) :=
begin
have : (i : zmod k) = (i : ℤ), by norm_cast,
simp only [this, zmod_equiv_gpowers_apply_coe_int, gpow_coe_nat],
refl
end
@[simp] lemma zmod_equiv_gpowers_symm_apply_gpow (i : ℤ) :
h.zmod_equiv_gpowers.symm (additive.of_mul (⟨ζ ^ i, i, rfl⟩ : subgroup.gpowers ζ)) = i :=
by rw [← h.zmod_equiv_gpowers.symm_apply_apply i, zmod_equiv_gpowers_apply_coe_int]
@[simp] lemma zmod_equiv_gpowers_symm_apply_gpow' (i : ℤ) :
h.zmod_equiv_gpowers.symm ⟨ζ ^ i, i, rfl⟩ = i :=
h.zmod_equiv_gpowers_symm_apply_gpow i
@[simp] lemma zmod_equiv_gpowers_symm_apply_pow (i : ℕ) :
h.zmod_equiv_gpowers.symm (additive.of_mul (⟨ζ ^ i, i, rfl⟩ : subgroup.gpowers ζ)) = i :=
by rw [← h.zmod_equiv_gpowers.symm_apply_apply i, zmod_equiv_gpowers_apply_coe_nat]
@[simp] lemma zmod_equiv_gpowers_symm_apply_pow' (i : ℕ) :
h.zmod_equiv_gpowers.symm ⟨ζ ^ i, i, rfl⟩ = i :=
h.zmod_equiv_gpowers_symm_apply_pow i
lemma gpowers_eq {k : ℕ+} {ζ : units R} (h : is_primitive_root ζ k) :
subgroup.gpowers ζ = roots_of_unity k R :=
begin
apply set_like.coe_injective,
haveI : fact (0 < (k : ℕ)) := ⟨k.pos⟩,
haveI F : fintype (subgroup.gpowers ζ) := fintype.of_equiv _ (h.zmod_equiv_gpowers).to_equiv,
refine @set.eq_of_subset_of_card_le (units R) (subgroup.gpowers ζ) (roots_of_unity k R)
F (roots_of_unity.fintype R k)
(subgroup.gpowers_subset $ show ζ ∈ roots_of_unity k R, from h.pow_eq_one) _,
calc fintype.card (roots_of_unity k R)
≤ k : card_roots_of_unity R k
... = fintype.card (zmod k) : (zmod.card k).symm
... = fintype.card (subgroup.gpowers ζ) : fintype.card_congr (h.zmod_equiv_gpowers).to_equiv
end
lemma eq_pow_of_mem_roots_of_unity {k : ℕ+} {ζ ξ : units R}
(h : is_primitive_root ζ k) (hξ : ξ ∈ roots_of_unity k R) :
∃ (i : ℕ) (hi : i < k), ζ ^ i = ξ :=
begin
obtain ⟨n, rfl⟩ : ∃ n : ℤ, ζ ^ n = ξ, by rwa [← h.gpowers_eq] at hξ,
have hk0 : (0 : ℤ) < k := by exact_mod_cast k.pos,
let i := n % k,
have hi0 : 0 ≤ i := int.mod_nonneg _ (ne_of_gt hk0),
lift i to ℕ using hi0 with i₀ hi₀,
refine ⟨i₀, _, _⟩,
{ zify, rw [hi₀], exact int.mod_lt_of_pos _ hk0 },
{ have aux := h.gpow_eq_one, rw [← coe_coe] at aux,
rw [← gpow_coe_nat, hi₀, ← int.mod_add_div n k, gpow_add, gpow_mul,
aux, one_gpow, mul_one] }
end
lemma eq_pow_of_pow_eq_one {k : ℕ} {ζ ξ : R}
(h : is_primitive_root ζ k) (hξ : ξ ^ k = 1) (h0 : 0 < k) :
∃ i < k, ζ ^ i = ξ :=
begin
obtain ⟨ζ, rfl⟩ := h.is_unit h0,
obtain ⟨ξ, rfl⟩ := is_unit_of_pow_eq_one ξ k hξ h0,
obtain ⟨k, rfl⟩ : ∃ k' : ℕ+, k = k' := ⟨⟨k, h0⟩, rfl⟩,
simp only [← units.coe_pow, ← units.ext_iff],
rw coe_units_iff at h,
apply h.eq_pow_of_mem_roots_of_unity,
rw [mem_roots_of_unity, units.ext_iff, units.coe_pow, hξ, units.coe_one]
end
lemma is_primitive_root_iff' {k : ℕ+} {ζ ξ : units R} (h : is_primitive_root ζ k) :
is_primitive_root ξ k ↔ ∃ (i < (k : ℕ)) (hi : i.coprime k), ζ ^ i = ξ :=
begin
split,
{ intro hξ,
obtain ⟨i, hik, rfl⟩ := h.eq_pow_of_mem_roots_of_unity hξ.pow_eq_one,
rw h.pow_iff_coprime k.pos at hξ,
exact ⟨i, hik, hξ, rfl⟩ },
{ rintro ⟨i, -, hi, rfl⟩, exact h.pow_of_coprime i hi }
end
lemma is_primitive_root_iff {k : ℕ} {ζ ξ : R} (h : is_primitive_root ζ k) (h0 : 0 < k) :
is_primitive_root ξ k ↔ ∃ (i < k) (hi : i.coprime k), ζ ^ i = ξ :=
begin
split,
{ intro hξ,
obtain ⟨i, hik, rfl⟩ := h.eq_pow_of_pow_eq_one hξ.pow_eq_one h0,
rw h.pow_iff_coprime h0 at hξ,
exact ⟨i, hik, hξ, rfl⟩ },
{ rintro ⟨i, -, hi, rfl⟩, exact h.pow_of_coprime i hi }
end
lemma card_roots_of_unity' {n : ℕ+} (h : is_primitive_root ζ n) :
fintype.card (roots_of_unity n R) = n :=
begin
haveI : fact (0 < ↑n) := ⟨n.pos⟩,
let e := h.zmod_equiv_gpowers,
haveI F : fintype (subgroup.gpowers ζ) := fintype.of_equiv _ e.to_equiv,
calc fintype.card (roots_of_unity n R)
= fintype.card (subgroup.gpowers ζ) : fintype.card_congr $ by rw h.gpowers_eq
... = fintype.card (zmod n) : fintype.card_congr e.to_equiv.symm
... = n : zmod.card n
end
lemma card_roots_of_unity {ζ : R} {n : ℕ+} (h : is_primitive_root ζ n) :
fintype.card (roots_of_unity n R) = n :=
begin
obtain ⟨ζ, hζ⟩ := h.is_unit n.pos,
rw [← hζ, is_primitive_root.coe_units_iff] at h,
exact h.card_roots_of_unity'
end
/-- The cardinality of the multiset `nth_roots ↑n (1 : R)` is `n`
if there is a primitive root of unity in `R`. -/
lemma card_nth_roots {ζ : R} {n : ℕ} (h : is_primitive_root ζ n) :
(nth_roots n (1 : R)).card = n :=
begin
cases nat.eq_zero_or_pos n with hzero hpos,
{ simp only [hzero, multiset.card_zero, nth_roots_zero] },
rw eq_iff_le_not_lt,
use card_nth_roots n 1,
{ rw [not_lt],
have hcard : fintype.card {x // x ∈ nth_roots n (1 : R)}
≤ (nth_roots n (1 : R)).attach.card := multiset.card_le_of_le (multiset.erase_dup_le _),
rw multiset.card_attach at hcard,
rw ← pnat.to_pnat'_coe hpos at hcard h ⊢,
set m := nat.to_pnat' n,
rw [← fintype.card_congr (roots_of_unity_equiv_nth_roots R m), card_roots_of_unity h] at hcard,
exact hcard }
end
/-- The multiset `nth_roots ↑n (1 : R)` has no repeated elements
if there is a primitive root of unity in `R`. -/
lemma nth_roots_nodup {ζ : R} {n : ℕ} (h : is_primitive_root ζ n) : (nth_roots n (1 : R)).nodup :=
begin
cases nat.eq_zero_or_pos n with hzero hpos,
{ simp only [hzero, multiset.nodup_zero, nth_roots_zero] },
apply (@multiset.erase_dup_eq_self R _ _).1,
rw eq_iff_le_not_lt,
split,
{ exact multiset.erase_dup_le (nth_roots n (1 : R)) },
{ by_contra ha,
replace ha := multiset.card_lt_of_lt ha,
rw card_nth_roots h at ha,
have hrw : (nth_roots n (1 : R)).erase_dup.card =
fintype.card {x // x ∈ (nth_roots n (1 : R))},
{ set fs := (⟨(nth_roots n (1 : R)).erase_dup, multiset.nodup_erase_dup _⟩ : finset R),
rw [← finset.card_mk, ← fintype.card_of_subtype fs _],
intro x,
simp only [multiset.mem_erase_dup, finset.mem_mk] },
rw ← pnat.to_pnat'_coe hpos at h hrw ha,
set m := nat.to_pnat' n,
rw [hrw, ← fintype.card_congr (roots_of_unity_equiv_nth_roots R m),
card_roots_of_unity h] at ha,
exact nat.lt_asymm ha ha }
end
@[simp] lemma card_nth_roots_finset {ζ : R} {n : ℕ} (h : is_primitive_root ζ n) :
(nth_roots_finset n R).card = n :=
by rw [nth_roots_finset, ← multiset.to_finset_eq (nth_roots_nodup h), card_mk, h.card_nth_roots]
open_locale nat
/-- If an integral domain has a primitive `k`-th root of unity, then it has `φ k` of them. -/
lemma card_primitive_roots {ζ : R} {k : ℕ} (h : is_primitive_root ζ k) (h0 : 0 < k) :
(primitive_roots k R).card = φ k :=
begin
symmetry,
refine finset.card_congr (λ i _, ζ ^ i) _ _ _,
{ simp only [true_and, and_imp, mem_filter, mem_range, mem_univ],
rintro i - hi,
rw mem_primitive_roots h0,
exact h.pow_of_coprime i hi.symm },
{ simp only [true_and, and_imp, mem_filter, mem_range, mem_univ],
rintro i j hi - hj - H,
exact h.pow_inj hi hj H },
{ simp only [exists_prop, true_and, mem_filter, mem_range, mem_univ],
intros ξ hξ,
rw [mem_primitive_roots h0, h.is_primitive_root_iff h0] at hξ,
rcases hξ with ⟨i, hin, hi, H⟩,
exact ⟨i, ⟨hin, hi.symm⟩, H⟩ }
end
/-- The sets `primitive_roots k R` are pairwise disjoint. -/
lemma disjoint {k l : ℕ} (hk : 0 < k) (hl : 0 < l) (h : k ≠ l) :
disjoint (primitive_roots k R) (primitive_roots l R) :=
begin
intro z,
simp only [finset.inf_eq_inter, finset.mem_inter, mem_primitive_roots, hk, hl, iff_def],
rintro ⟨⟨hzk, Hzk⟩, ⟨hzl, Hzl⟩⟩,
apply_rules [h, nat.dvd_antisymm, Hzk, Hzl, hzk, hzl]
end
/-- If there is a `n`-th primitive root of unity in `R` and `b` divides `n`,
then there is a `b`-th primitive root of unity in `R`. -/
lemma pow {ζ : R} {n : ℕ} {a b : ℕ}
(hn : 0 < n) (h : is_primitive_root ζ n) (hprod : n = a * b) :
is_primitive_root (ζ ^ a) b :=
begin
subst n,
simp only [iff_def, ← pow_mul, h.pow_eq_one, eq_self_iff_true, true_and],
intros l hl,
have ha0 : a ≠ 0, { rintro rfl, simpa only [nat.not_lt_zero, zero_mul] using hn },
rwa ← mul_dvd_mul_iff_left ha0,
exact h.dvd_of_pow_eq_one _ hl
end
/-- `nth_roots n` as a `finset` is equal to the union of `primitive_roots i R` for `i ∣ n`
if there is a primitive root of unity in `R`. -/
lemma nth_roots_one_eq_bUnion_primitive_roots' {ζ : R} {n : ℕ+} (h : is_primitive_root ζ n) :
nth_roots_finset n R = (nat.divisors ↑n).bUnion (λ i, (primitive_roots i R)) :=
begin
symmetry,
apply finset.eq_of_subset_of_card_le,
{ intros x,
simp only [nth_roots_finset, ← multiset.to_finset_eq (nth_roots_nodup h),
exists_prop, finset.mem_bUnion, finset.mem_filter, finset.mem_range, mem_nth_roots,
finset.mem_mk, nat.mem_divisors, and_true, ne.def, pnat.ne_zero, pnat.pos, not_false_iff],
rintro ⟨a, ⟨d, hd⟩, ha⟩,
have hazero : 0 < a,
{ contrapose! hd with ha0,
simp only [nonpos_iff_eq_zero, zero_mul, *] at *,
exact n.ne_zero },
rw mem_primitive_roots hazero at ha,
rw [hd, pow_mul, ha.pow_eq_one, one_pow] },
{ apply le_of_eq,
rw [h.card_nth_roots_finset, finset.card_bUnion],
{ rw [← nat.sum_totient n, nat.filter_dvd_eq_divisors (pnat.ne_zero n), sum_congr rfl]
{ occs := occurrences.pos [1] },
simp only [finset.mem_filter, finset.mem_range, nat.mem_divisors],
rintro k ⟨H, hk⟩,
have hdvd := H,
rcases H with ⟨d, hd⟩,
rw mul_comm at hd,
rw (h.pow n.pos hd).card_primitive_roots (pnat.pos_of_div_pos hdvd) },
{ intros i hi j hj hdiff,
simp only [nat.mem_divisors, and_true, ne.def, pnat.ne_zero, not_false_iff] at hi hj,
exact disjoint (pnat.pos_of_div_pos hi) (pnat.pos_of_div_pos hj) hdiff } }
end
/-- `nth_roots n` as a `finset` is equal to the union of `primitive_roots i R` for `i ∣ n`
if there is a primitive root of unity in `R`. -/
lemma nth_roots_one_eq_bUnion_primitive_roots {ζ : R} {n : ℕ} (hpos : 0 < n)
(h : is_primitive_root ζ n) :
nth_roots_finset n R = (nat.divisors n).bUnion (λ i, (primitive_roots i R)) :=
@nth_roots_one_eq_bUnion_primitive_roots' _ _ _ ⟨n, hpos⟩ h
end integral_domain
section minpoly
open minpoly
variables {n : ℕ} {K : Type*} [field K] {μ : K} (h : is_primitive_root μ n) (hpos : 0 < n)
include n μ h hpos
/--`μ` is integral over `ℤ`. -/
lemma is_integral : is_integral ℤ μ :=
begin
use (X ^ n - 1),
split,
{ exact (monic_X_pow_sub_C 1 (ne_of_lt hpos).symm) },
{ simp only [((is_primitive_root.iff_def μ n).mp h).left, eval₂_one, eval₂_X_pow, eval₂_sub,
sub_self] }
end
variables [char_zero K]
/--The minimal polynomial of a root of unity `μ` divides `X ^ n - 1`. -/
lemma minpoly_dvd_X_pow_sub_one : minpoly ℤ μ ∣ X ^ n - 1 :=
begin
apply minpoly.gcd_domain_dvd ℚ (is_integral h hpos) (polynomial.monic.is_primitive
(monic_X_pow_sub_C 1 (ne_of_lt hpos).symm)),
simp only [((is_primitive_root.iff_def μ n).mp h).left, aeval_X_pow, ring_hom.eq_int_cast,
int.cast_one, aeval_one, alg_hom.map_sub, sub_self]
end
/-- The reduction modulo `p` of the minimal polynomial of a root of unity `μ` is separable. -/
lemma separable_minpoly_mod {p : ℕ} [fact p.prime] (hdiv : ¬p ∣ n) :
separable (map (int.cast_ring_hom (zmod p)) (minpoly ℤ μ)) :=
begin
have hdvd : (map (int.cast_ring_hom (zmod p))
(minpoly ℤ μ)) ∣ X ^ n - 1,
{ simpa [map_pow, map_X, map_one, ring_hom.coe_of, map_sub] using
ring_hom.map_dvd (ring_hom.of (map (int.cast_ring_hom (zmod p))))
(minpoly_dvd_X_pow_sub_one h hpos) },
refine separable.of_dvd (separable_X_pow_sub_C 1 _ one_ne_zero) hdvd,
by_contra hzero,
exact hdiv ((zmod.nat_coe_zmod_eq_zero_iff_dvd n p).1 (not_not.1 hzero))
end
/-- The reduction modulo `p` of the minimal polynomial of a root of unity `μ` is squarefree. -/
lemma squarefree_minpoly_mod {p : ℕ} [fact p.prime] (hdiv : ¬ p ∣ n) :
squarefree (map (int.cast_ring_hom (zmod p)) (minpoly ℤ μ)) :=
(separable_minpoly_mod h hpos hdiv).squarefree
/- Let `P` be the minimal polynomial of a root of unity `μ` and `Q` be the minimal polynomial of
`μ ^ p`, where `p` is a prime that does not divide `n`. Then `P` divides `expand ℤ p Q`. -/
lemma minpoly_dvd_expand {p : ℕ} (hprime : nat.prime p) (hdiv : ¬ p ∣ n) :
minpoly ℤ μ ∣
expand ℤ p (minpoly ℤ (μ ^ p)) :=
begin
apply minpoly.gcd_domain_dvd ℚ (h.is_integral hpos),
{ apply monic.is_primitive,
rw [polynomial.monic, leading_coeff, nat_degree_expand, mul_comm, coeff_expand_mul'
(nat.prime.pos hprime), ← leading_coeff, ← polynomial.monic],
exact minpoly.monic (is_integral (pow_of_prime h hprime hdiv) hpos) },
{ rw [aeval_def, coe_expand, ← comp, eval₂_eq_eval_map, map_comp, map_pow, map_X, eval_comp,
eval_pow, eval_X, ← eval₂_eq_eval_map, ← aeval_def],
exact minpoly.aeval _ _ }
end
/- Let `P` be the minimal polynomial of a root of unity `μ` and `Q` be the minimal polynomial of
`μ ^ p`, where `p` is a prime that does not divide `n`. Then `P` divides `Q ^ p` modulo `p`. -/
lemma minpoly_dvd_pow_mod {p : ℕ} [hprime : fact p.prime] (hdiv : ¬ p ∣ n) :
map (int.cast_ring_hom (zmod p)) (minpoly ℤ μ) ∣
map (int.cast_ring_hom (zmod p)) (minpoly ℤ (μ ^ p)) ^ p :=
begin
set Q := minpoly ℤ (μ ^ p),
have hfrob : map (int.cast_ring_hom (zmod p)) Q ^ p =
map (int.cast_ring_hom (zmod p)) (expand ℤ p Q),
by rw [← zmod.expand_card, map_expand hprime.1.pos],
rw [hfrob],
apply ring_hom.map_dvd (ring_hom.of (map (int.cast_ring_hom (zmod p)))),
exact minpoly_dvd_expand h hpos hprime.1 hdiv
end
/- Let `P` be the minimal polynomial of a root of unity `μ` and `Q` be the minimal polynomial of
`μ ^ p`, where `p` is a prime that does not divide `n`. Then `P` divides `Q` modulo `p`. -/
lemma minpoly_dvd_mod_p {p : ℕ} [hprime : fact p.prime] (hdiv : ¬ p ∣ n) :
map (int.cast_ring_hom (zmod p)) (minpoly ℤ μ) ∣
map (int.cast_ring_hom (zmod p)) (minpoly ℤ (μ ^ p)) :=
(unique_factorization_monoid.dvd_pow_iff_dvd_of_squarefree (squarefree_minpoly_mod h
hpos hdiv) hprime.1.ne_zero).1 (minpoly_dvd_pow_mod h hpos hdiv)
/-- If `p` is a prime that does not divide `n`,
then the minimal polynomials of a primitive `n`-th root of unity `μ`
and of `μ ^ p` are the same. -/
lemma minpoly_eq_pow {p : ℕ} [hprime : fact p.prime] (hdiv : ¬ p ∣ n) :
minpoly ℤ μ = minpoly ℤ (μ ^ p) :=
begin
by_contra hdiff,
set P := minpoly ℤ μ,
set Q := minpoly ℤ (μ ^ p),
have Pmonic : P.monic := minpoly.monic (h.is_integral hpos),
have Qmonic : Q.monic := minpoly.monic ((h.pow_of_prime hprime.1 hdiv).is_integral hpos),
have Pirr : irreducible P := minpoly.irreducible (h.is_integral hpos),
have Qirr : irreducible Q :=
minpoly.irreducible ((h.pow_of_prime hprime.1 hdiv).is_integral hpos),
have PQprim : is_primitive (P * Q) := Pmonic.is_primitive.mul Qmonic.is_primitive,
have prod : P * Q ∣ X ^ n - 1,
{ rw [(is_primitive.int.dvd_iff_map_cast_dvd_map_cast (P * Q) (X ^ n - 1) PQprim
(monic_X_pow_sub_C (1 : ℤ) (ne_of_gt hpos)).is_primitive), map_mul],
refine is_coprime.mul_dvd _ _ _,
{ have aux := is_primitive.int.irreducible_iff_irreducible_map_cast Pmonic.is_primitive,
refine (dvd_or_coprime _ _ (aux.1 Pirr)).resolve_left _,
rw map_dvd_map (int.cast_ring_hom ℚ) int.cast_injective Pmonic,
intro hdiv,
refine hdiff (eq_of_monic_of_associated Pmonic Qmonic _),
exact associated_of_dvd_dvd hdiv (dvd_symm_of_irreducible Pirr Qirr hdiv) },
{ apply (map_dvd_map (int.cast_ring_hom ℚ) int.cast_injective Pmonic).2,
exact minpoly_dvd_X_pow_sub_one h hpos },
{ apply (map_dvd_map (int.cast_ring_hom ℚ) int.cast_injective Qmonic).2,
exact minpoly_dvd_X_pow_sub_one (pow_of_prime h hprime.1 hdiv) hpos } },
replace prod := ring_hom.map_dvd (ring_hom.of (map (int.cast_ring_hom (zmod p)))) prod,
rw [ring_hom.coe_of, map_mul, map_sub, map_one, map_pow, map_X] at prod,
obtain ⟨R, hR⟩ := minpoly_dvd_mod_p h hpos hdiv,
rw [hR, ← mul_assoc, ← map_mul, ← sq, map_pow] at prod,
have habs : map (int.cast_ring_hom (zmod p)) P ^ 2 ∣ map (int.cast_ring_hom (zmod p)) P ^ 2 * R,
{ use R },
replace habs := lt_of_lt_of_le (enat.coe_lt_coe.2 one_lt_two)
(multiplicity.le_multiplicity_of_pow_dvd (dvd_trans habs prod)),
have hfree : squarefree (X ^ n - 1 : polynomial (zmod p)),
{ refine squarefree_X_pow_sub_C 1 _ one_ne_zero,
by_contra hzero,
exact hdiv ((zmod.nat_coe_zmod_eq_zero_iff_dvd n p).1 (not_not.1 hzero)) },
cases (multiplicity.squarefree_iff_multiplicity_le_one (X ^ n - 1)).1 hfree
(map (int.cast_ring_hom (zmod p)) P) with hle hunit,
{ exact not_lt_of_le hle habs },
{ replace hunit := degree_eq_zero_of_is_unit hunit,
rw degree_map_eq_of_leading_coeff_ne_zero _ _ at hunit,
{ exact (ne_of_lt (minpoly.degree_pos (is_integral h hpos))).symm hunit },
simp only [Pmonic, ring_hom.eq_int_cast, monic.leading_coeff, int.cast_one, ne.def,
not_false_iff, one_ne_zero] },
end
/-- If `m : ℕ` is coprime with `n`,
then the minimal polynomials of a primitive `n`-th root of unity `μ`
and of `μ ^ m` are the same. -/
lemma minpoly_eq_pow_coprime {m : ℕ} (hcop : nat.coprime m n) :
minpoly ℤ μ = minpoly ℤ (μ ^ m) :=
begin
revert n hcop,
refine unique_factorization_monoid.induction_on_prime m _ _ _,
{ intros n hn h hpos,
congr,
simpa [(nat.coprime_zero_left n).mp hn] using h },
{ intros u hunit n hcop h hpos,
congr,
simp [nat.is_unit_iff.mp hunit] },
{ intros a p ha hprime hind n hcop h hpos,
rw hind (nat.coprime.coprime_mul_left hcop) h hpos, clear hind,
replace hprime := nat.prime_iff.2 hprime,
have hdiv := (nat.prime.coprime_iff_not_dvd hprime).1 (nat.coprime.coprime_mul_right hcop),
haveI := fact.mk hprime,
rw [minpoly_eq_pow
(h.pow_of_coprime a (nat.coprime.coprime_mul_left hcop)) hpos hdiv],
congr' 1,
ring_exp }
end
/-- If `m : ℕ` is coprime with `n`,
then the minimal polynomial of a primitive `n`-th root of unity `μ`
has `μ ^ m` as root. -/
lemma pow_is_root_minpoly {m : ℕ} (hcop : nat.coprime m n) :
is_root (map (int.cast_ring_hom K) (minpoly ℤ μ)) (μ ^ m) :=
by simpa [minpoly_eq_pow_coprime h hpos hcop, eval_map, aeval_def (μ ^ m) _]
using minpoly.aeval ℤ (μ ^ m)
/-- `primitive_roots n K` is a subset of the roots of the minimal polynomial of a primitive
`n`-th root of unity `μ`. -/
lemma is_roots_of_minpoly : primitive_roots n K ⊆ (map (int.cast_ring_hom K)
(minpoly ℤ μ)).roots.to_finset :=
begin
intros x hx,
obtain ⟨m, hle, hcop, rfl⟩ := (is_primitive_root_iff h hpos).1 ((mem_primitive_roots hpos).1 hx),
simpa [multiset.mem_to_finset,
mem_roots (map_monic_ne_zero $ minpoly.monic $ is_integral h hpos)]
using pow_is_root_minpoly h hpos hcop
end
/-- The degree of the minimal polynomial of `μ` is at least `totient n`. -/
lemma totient_le_degree_minpoly : nat.totient n ≤ (minpoly ℤ μ).nat_degree :=
let P : polynomial ℤ := minpoly ℤ μ,-- minimal polynomial of `μ`
P_K : polynomial K := map (int.cast_ring_hom K) P -- minimal polynomial of `μ` sent to `K[X]`
in calc
n.totient = (primitive_roots n K).card : (h.card_primitive_roots hpos).symm
... ≤ P_K.roots.to_finset.card : finset.card_le_of_subset (is_roots_of_minpoly h hpos)
... ≤ P_K.roots.card : multiset.to_finset_card_le _
... ≤ P_K.nat_degree : (card_roots' $ map_monic_ne_zero
(minpoly.monic $ is_integral h hpos))
... ≤ P.nat_degree : nat_degree_map_le _ _
end minpoly
end is_primitive_root
|
3866ab3fddf93740276bec4ee0f952fa50e266af | 8b8ab1eba67c5c35b51ab27fcf8151aab660e44d | /src/invariant_norms.lean | 495e8ea1dece0148c5aa7d6af19034d8c3ed2380 | [] | no_license | PatrickMassot/lean-scratchpad | ab5e89b69034cc699bf234852822acf74b50f2b9 | 03eec3bfabfc218b79dcbe7c7712bfa024a02625 | refs/heads/master | 1,626,995,046,509 | 1,538,577,853,000 | 1,538,577,853,000 | 115,135,779 | 1 | 2 | null | 1,522,993,992,000 | 1,513,962,994,000 | Lean | UTF-8 | Lean | false | false | 6,098 | lean | import tactic.finish
import algebra.group algebra.big_operators
noncomputable theory
local attribute [instance] classical.prop_decidable
local attribute [simp] mul_assoc
open list
variables {α β : Type} [group α] [group β] {a b g h : α}
-- Conjuguation in a group
--------------------------
def conj (a b : α) := a*b*a⁻¹
@[simp] lemma conj_action : conj (g * h) a = conj g (conj h a) :=
by simp[conj]
@[simp] lemma conj_by_one : conj 1 a = a :=
by simp[conj]
instance conj.is_group_hom : is_group_hom (conj a) :=
⟨λ x y, by simp [conj, mul_assoc]⟩
lemma inv_conj : conj a (b⁻¹) = (conj a b)⁻¹ :=
is_group_hom.inv (conj a) b
lemma conj_mul : conj g (a * b) = conj g a * conj g b :=
is_group_hom.mul _ _ _
@[simp] lemma conj_one : conj a 1 = 1 :=
is_group_hom.one (conj a)
-- Products
-----------
/- "is_product S n a" means a can be written as a product of n elements of S or S⁻¹ -/
def is_product (S : set α) (n : ℕ) (g : α) : Prop :=
∃ l : list α, g = prod l ∧ (∀ x ∈ l, x ∈ S ∨ x⁻¹ ∈ S) ∧ l.length = n
lemma is_product_mul {S : set α} {m n a b}
(h₁ : is_product S m a) (h₂ : is_product S n b) : is_product S (m + n) (a * b) :=
begin
rcases h₁ with ⟨l₁, prod₁, inS₁, len₁⟩,
rcases h₂ with ⟨l₂, prod₂, inS₂, len₂⟩,
existsi l₁ ++ l₂, -- denoted by l in comments
repeat {split},
{ -- prove a*b = prod l
simp [prod₁,prod₂] },
{ -- prove elements of l are in S or S⁻¹
simpa,
intros x x_in_l₁_or_l₂,
cases x_in_l₁_or_l₂,
{ apply inS₁ x, assumption },
{ apply inS₂ x, assumption },
},
{ -- prove length l is m + n
simp [len₁, len₂] }
end
lemma is_product_inv (S : set α) {n a} (h : is_product S n a) : is_product S n (a⁻¹) :=
begin
rcases h with ⟨l, product, inS, len⟩,
existsi map (λ x, x⁻¹) (reverse l),
repeat {split},
{ rw product,
apply inv_prod },
{ simpa,
intros,
have H := (inS x_1) a_1,
have H' : x_1 = x⁻¹ := eq_inv_of_eq_inv (eq.symm a_2),
simp[H'] at H,
exact or.symm H },
{ simpa }
end
lemma is_product_conj {S T : set α} (g) (H : ∀ a, a ∈ S → conj g a ∈ T)
{n a} (h : is_product S n a) : is_product T n (conj g a) :=
begin
rcases h with ⟨l, prod, inS, len⟩,
existsi (map (conj g) l),
repeat {split},
{ rw prod,
apply is_group_hom.prod },
{ clear prod a len n,
intros x x_in_conj_l,
rw mem_map at x_in_conj_l,
rcases x_in_conj_l with ⟨b, b_in_l, conj_b_x⟩,
specialize inS b b_in_l, clear b_in_l l,
cases inS,
{ have conj_in_T := H b inS,
rw conj_b_x at conj_in_T,
exact or.inl conj_in_T},
{ have conj_in_T := H b⁻¹ inS,
rw [inv_conj, conj_b_x] at conj_in_T,
exact or.inr conj_in_T } },
{ simp[len] }
end
--- Generating sets
-------------------
def is_generating (S : set α) : Prop :=
∀ g : α, ∃ n : ℕ, is_product S n g
structure generating_set :=
(set : set α)
(gen : is_generating set)
-- Invariant norms on a group
-----------------------------
structure is_invariant_norm (ν : α → ℕ) : Prop :=
(nonneg : ∀ g : α, 0 ≤ ν g) -- this is silly but ultimately the target will be ℝ
(eq_zero : ∀ g : α, ν g = 0 → g = 1)
(mul : ∀ g h : α, ν (g*h) ≤ ν g + ν h)
(inv : ∀ g : α, ν g⁻¹ = ν g)
(conj : ∀ g h : α, ν (conj h g) = ν g)
def is_conj_invariant_set (S : set α) : Prop :=
∀ g s : α, s ∈ S → conj g s ∈ S
/- Given a generating set S and an alement a,
gen_norm S a is the minimal number of elements of S or S⁻¹
required to write a as a product.
The next two lemma prove the definition is what it should be -/
def gen_norm (S : generating_set) (a : α) := nat.find (S.gen a)
lemma is_product_norm (S : generating_set) (g : α) :
is_product S.set (gen_norm S g) g :=
nat.find_spec (S.gen g)
lemma norm_min (S : generating_set) {a : α} {n} :
is_product S.set n a → gen_norm S a ≤ n :=
by apply nat.find_min' (S.gen a)
lemma inv_norm_of_inv_set [str : group α] (S : @generating_set α str) :
is_conj_invariant_set S.set → is_invariant_norm (gen_norm S) :=
begin
intro inv_hyp,
constructor; intros,
{ apply nat.zero_le },
{ have H' := is_product_norm S g,
rw a at H',
rcases H' with ⟨l, prod, inS, len⟩,
rw [eq_nil_of_length_eq_zero len] at prod,
simp at prod,
assumption },
{ have g_prod := is_product_norm S g,
have h_prod := is_product_norm S h,
have estimate := is_product_mul g_prod h_prod,
exact norm_min S estimate },
{ apply le_antisymm,
{ apply norm_min,
exact is_product_inv S.set (is_product_norm S g) },
{ apply norm_min,
simpa using is_product_inv S.set (is_product_norm S g⁻¹) } },
{ apply le_antisymm ; apply norm_min,
{ exact is_product_conj h (inv_hyp h) (is_product_norm S g) },
{ have prod := is_product_conj h⁻¹ (inv_hyp h⁻¹) (is_product_norm S (conj h g)),
rw [←conj_action] at prod,
simp[conj_by_one] at prod,
exact prod } },
end
-- Commutators
--------------
def comm (a b : α) := a*b*a⁻¹*b⁻¹
local notation `[[`a, b`]]` := comm a b
lemma commuting : [[a, b]] = 1 ↔ a*b = b*a :=
by simp [comm, -mul_assoc, mul_inv_eq_iff_eq_mul]
lemma commutator_trading (comm_hyp : [[a, conj g b]] = 1) :
∃ c d e f : α, [[a, b]] = (conj c g⁻¹)*(conj d g)*(conj e g⁻¹)*(conj f g) :=
begin
unfold conj at comm_hyp,
let b':= g*b*g⁻¹,
exact ⟨_, _, _, _, calc
[[a, b]] = a * b * a⁻¹ * b⁻¹ : rfl
... = a * (g⁻¹ * b' * g) * a⁻¹ * (g⁻¹ * b'⁻¹ * g) : by simp
... = a * g⁻¹ * (a⁻¹ * a) * b' * g * a⁻¹ * (b'⁻¹ * b') * g⁻¹ * b'⁻¹ * g : by simp
... = a * g⁻¹ * (a⁻¹ * a) * b' * g * (b'*a)⁻¹ * b' * g⁻¹ * b'⁻¹ * g : by simp
... = a * g⁻¹ * (a⁻¹ * a) * b' * g * (a*b')⁻¹ * b' * g⁻¹ * b'⁻¹ * g : by simp [commuting.1 comm_hyp]
... = (conj a g⁻¹) * (conj (a*b') g) * (conj b' g⁻¹) * (conj 1 g) : by simp [conj]⟩
end |
2a03f537471c8337ace760626ac57d6433a1cf65 | 82e44445c70db0f03e30d7be725775f122d72f3e | /src/data/list/basic.lean | fd4bf289c81f9a2735469527587dba4cd41745f2 | [
"Apache-2.0"
] | permissive | stjordanis/mathlib | 51e286d19140e3788ef2c470bc7b953e4991f0c9 | 2568d41bca08f5d6bf39d915434c8447e21f42ee | refs/heads/master | 1,631,748,053,501 | 1,627,938,886,000 | 1,627,938,886,000 | 228,728,358 | 0 | 0 | Apache-2.0 | 1,576,630,588,000 | 1,576,630,587,000 | null | UTF-8 | Lean | false | false | 195,101 | lean | /-
Copyright (c) 2014 Parikshit Khanna. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro
-/
import control.monad.basic
import data.nat.basic
import order.rel_classes
import algebra.group_power.basic
/-!
# Basic properties of lists
-/
open function nat
namespace list
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x}
attribute [inline] list.head
instance : is_left_id (list α) has_append.append [] :=
⟨ nil_append ⟩
instance : is_right_id (list α) has_append.append [] :=
⟨ append_nil ⟩
instance : is_associative (list α) has_append.append :=
⟨ append_assoc ⟩
theorem cons_ne_nil (a : α) (l : list α) : a::l ≠ [].
theorem cons_ne_self (a : α) (l : list α) : a::l ≠ l :=
mt (congr_arg length) (nat.succ_ne_self _)
theorem head_eq_of_cons_eq {h₁ h₂ : α} {t₁ t₂ : list α} :
(h₁::t₁) = (h₂::t₂) → h₁ = h₂ :=
assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pheq)
theorem tail_eq_of_cons_eq {h₁ h₂ : α} {t₁ t₂ : list α} :
(h₁::t₁) = (h₂::t₂) → t₁ = t₂ :=
assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pteq)
@[simp] theorem cons_injective {a : α} : injective (cons a) :=
assume l₁ l₂, assume Pe, tail_eq_of_cons_eq Pe
theorem cons_inj (a : α) {l l' : list α} : a::l = a::l' ↔ l = l' :=
cons_injective.eq_iff
theorem exists_cons_of_ne_nil {l : list α} (h : l ≠ nil) : ∃ b L, l = b :: L :=
by { induction l with c l', contradiction, use [c,l'], }
/-! ### mem -/
theorem mem_singleton_self (a : α) : a ∈ [a] := mem_cons_self _ _
theorem eq_of_mem_singleton {a b : α} : a ∈ [b] → a = b :=
assume : a ∈ [b], or.elim (eq_or_mem_of_mem_cons this)
(assume : a = b, this)
(assume : a ∈ [], absurd this (not_mem_nil a))
@[simp] theorem mem_singleton {a b : α} : a ∈ [b] ↔ a = b :=
⟨eq_of_mem_singleton, or.inl⟩
theorem mem_of_mem_cons_of_mem {a b : α} {l : list α} : a ∈ b::l → b ∈ l → a ∈ l :=
assume ainbl binl, or.elim (eq_or_mem_of_mem_cons ainbl)
(assume : a = b, begin subst a, exact binl end)
(assume : a ∈ l, this)
theorem _root_.decidable.list.eq_or_ne_mem_of_mem [decidable_eq α]
{a b : α} {l : list α} (h : a ∈ b :: l) : a = b ∨ (a ≠ b ∧ a ∈ l) :=
decidable.by_cases or.inl $ assume : a ≠ b, h.elim or.inl $ assume h, or.inr ⟨this, h⟩
theorem eq_or_ne_mem_of_mem {a b : α} {l : list α} : a ∈ b :: l → a = b ∨ (a ≠ b ∧ a ∈ l) :=
by classical; exact decidable.list.eq_or_ne_mem_of_mem
theorem not_mem_append {a : α} {s t : list α} (h₁ : a ∉ s) (h₂ : a ∉ t) : a ∉ s ++ t :=
mt mem_append.1 $ not_or_distrib.2 ⟨h₁, h₂⟩
theorem ne_nil_of_mem {a : α} {l : list α} (h : a ∈ l) : l ≠ [] :=
by intro e; rw e at h; cases h
theorem mem_split {a : α} {l : list α} (h : a ∈ l) : ∃ s t : list α, l = s ++ a :: t :=
begin
induction l with b l ih, {cases h}, rcases h with rfl | h,
{ exact ⟨[], l, rfl⟩ },
{ rcases ih h with ⟨s, t, rfl⟩,
exact ⟨b::s, t, rfl⟩ }
end
theorem mem_of_ne_of_mem {a y : α} {l : list α} (h₁ : a ≠ y) (h₂ : a ∈ y :: l) : a ∈ l :=
or.elim (eq_or_mem_of_mem_cons h₂) (λe, absurd e h₁) (λr, r)
theorem ne_of_not_mem_cons {a b : α} {l : list α} : a ∉ b::l → a ≠ b :=
assume nin aeqb, absurd (or.inl aeqb) nin
theorem not_mem_of_not_mem_cons {a b : α} {l : list α} : a ∉ b::l → a ∉ l :=
assume nin nainl, absurd (or.inr nainl) nin
theorem not_mem_cons_of_ne_of_not_mem {a y : α} {l : list α} : a ≠ y → a ∉ l → a ∉ y::l :=
assume p1 p2, not.intro (assume Pain, absurd (eq_or_mem_of_mem_cons Pain) (not_or p1 p2))
theorem ne_and_not_mem_of_not_mem_cons {a y : α} {l : list α} : a ∉ y::l → a ≠ y ∧ a ∉ l :=
assume p, and.intro (ne_of_not_mem_cons p) (not_mem_of_not_mem_cons p)
theorem mem_map_of_mem (f : α → β) {a : α} {l : list α} (h : a ∈ l) : f a ∈ map f l :=
begin
induction l with b l' ih,
{cases h},
{rcases h with rfl | h,
{exact or.inl rfl},
{exact or.inr (ih h)}}
end
theorem exists_of_mem_map {f : α → β} {b : β} {l : list α} (h : b ∈ map f l) :
∃ a, a ∈ l ∧ f a = b :=
begin
induction l with c l' ih,
{cases h},
{cases (eq_or_mem_of_mem_cons h) with h h,
{exact ⟨c, mem_cons_self _ _, h.symm⟩},
{rcases ih h with ⟨a, ha₁, ha₂⟩,
exact ⟨a, mem_cons_of_mem _ ha₁, ha₂⟩ }}
end
@[simp] theorem mem_map {f : α → β} {b : β} {l : list α} : b ∈ map f l ↔ ∃ a, a ∈ l ∧ f a = b :=
⟨exists_of_mem_map, λ ⟨a, la, h⟩, by rw [← h]; exact mem_map_of_mem f la⟩
theorem mem_map_of_injective {f : α → β} (H : injective f) {a : α} {l : list α} :
f a ∈ map f l ↔ a ∈ l :=
⟨λ m, let ⟨a', m', e⟩ := exists_of_mem_map m in H e ▸ m', mem_map_of_mem _⟩
lemma forall_mem_map_iff {f : α → β} {l : list α} {P : β → Prop} :
(∀ i ∈ l.map f, P i) ↔ ∀ j ∈ l, P (f j) :=
begin
split,
{ assume H j hj,
exact H (f j) (mem_map_of_mem f hj) },
{ assume H i hi,
rcases mem_map.1 hi with ⟨j, hj, ji⟩,
rw ← ji,
exact H j hj }
end
@[simp] lemma map_eq_nil {f : α → β} {l : list α} : list.map f l = [] ↔ l = [] :=
⟨by cases l; simp only [forall_prop_of_true, map, forall_prop_of_false, not_false_iff],
λ h, h.symm ▸ rfl⟩
@[simp] theorem mem_join {a : α} : ∀ {L : list (list α)}, a ∈ join L ↔ ∃ l, l ∈ L ∧ a ∈ l
| [] := ⟨false.elim, λ⟨_, h, _⟩, false.elim h⟩
| (c :: L) := by simp only [join, mem_append, @mem_join L, mem_cons_iff, or_and_distrib_right,
exists_or_distrib, exists_eq_left]
theorem exists_of_mem_join {a : α} {L : list (list α)} : a ∈ join L → ∃ l, l ∈ L ∧ a ∈ l :=
mem_join.1
theorem mem_join_of_mem {a : α} {L : list (list α)} {l} (lL : l ∈ L) (al : a ∈ l) : a ∈ join L :=
mem_join.2 ⟨l, lL, al⟩
@[simp]
theorem mem_bind {b : β} {l : list α} {f : α → list β} : b ∈ list.bind l f ↔ ∃ a ∈ l, b ∈ f a :=
iff.trans mem_join
⟨λ ⟨l', h1, h2⟩, let ⟨a, al, fa⟩ := exists_of_mem_map h1 in ⟨a, al, fa.symm ▸ h2⟩,
λ ⟨a, al, bfa⟩, ⟨f a, mem_map_of_mem _ al, bfa⟩⟩
theorem exists_of_mem_bind {b : β} {l : list α} {f : α → list β} :
b ∈ list.bind l f → ∃ a ∈ l, b ∈ f a :=
mem_bind.1
theorem mem_bind_of_mem {b : β} {l : list α} {f : α → list β} {a} (al : a ∈ l) (h : b ∈ f a) :
b ∈ list.bind l f :=
mem_bind.2 ⟨a, al, h⟩
lemma bind_map {g : α → list β} {f : β → γ} :
∀(l : list α), list.map f (l.bind g) = l.bind (λa, (g a).map f)
| [] := rfl
| (a::l) := by simp only [cons_bind, map_append, bind_map l]
lemma map_bind (g : β → list γ) (f : α → β) :
∀ l : list α, (list.map f l).bind g = l.bind (λ a, g (f a))
| [] := rfl
| (a::l) := by simp only [cons_bind, map_cons, map_bind l]
/-! ### length -/
theorem length_eq_zero {l : list α} : length l = 0 ↔ l = [] :=
⟨eq_nil_of_length_eq_zero, λ h, h.symm ▸ rfl⟩
@[simp] lemma length_singleton (a : α) : length [a] = 1 := rfl
theorem length_pos_of_mem {a : α} : ∀ {l : list α}, a ∈ l → 0 < length l
| (b::l) _ := zero_lt_succ _
theorem exists_mem_of_length_pos : ∀ {l : list α}, 0 < length l → ∃ a, a ∈ l
| (b::l) _ := ⟨b, mem_cons_self _ _⟩
theorem length_pos_iff_exists_mem {l : list α} : 0 < length l ↔ ∃ a, a ∈ l :=
⟨exists_mem_of_length_pos, λ ⟨a, h⟩, length_pos_of_mem h⟩
theorem ne_nil_of_length_pos {l : list α} : 0 < length l → l ≠ [] :=
λ h1 h2, lt_irrefl 0 ((length_eq_zero.2 h2).subst h1)
theorem length_pos_of_ne_nil {l : list α} : l ≠ [] → 0 < length l :=
λ h, pos_iff_ne_zero.2 $ λ h0, h $ length_eq_zero.1 h0
theorem length_pos_iff_ne_nil {l : list α} : 0 < length l ↔ l ≠ [] :=
⟨ne_nil_of_length_pos, length_pos_of_ne_nil⟩
lemma exists_mem_of_ne_nil (l : list α) (h : l ≠ []) : ∃ x, x ∈ l :=
exists_mem_of_length_pos (length_pos_of_ne_nil h)
theorem length_eq_one {l : list α} : length l = 1 ↔ ∃ a, l = [a] :=
⟨match l with [a], _ := ⟨a, rfl⟩ end, λ ⟨a, e⟩, e.symm ▸ rfl⟩
lemma exists_of_length_succ {n} :
∀ l : list α, l.length = n + 1 → ∃ h t, l = h :: t
| [] H := absurd H.symm $ succ_ne_zero n
| (h :: t) H := ⟨h, t, rfl⟩
@[simp] lemma length_injective_iff : injective (list.length : list α → ℕ) ↔ subsingleton α :=
begin
split,
{ intro h, refine ⟨λ x y, _⟩, suffices : [x] = [y], { simpa using this }, apply h, refl },
{ intros hα l1 l2 hl, induction l1 generalizing l2; cases l2,
{ refl }, { cases hl }, { cases hl },
congr, exactI subsingleton.elim _ _, apply l1_ih, simpa using hl }
end
@[simp] lemma length_injective [subsingleton α] : injective (length : list α → ℕ) :=
length_injective_iff.mpr $ by apply_instance
/-! ### set-theoretic notation of lists -/
lemma empty_eq : (∅ : list α) = [] := by refl
lemma singleton_eq (x : α) : ({x} : list α) = [x] := rfl
lemma insert_neg [decidable_eq α] {x : α} {l : list α} (h : x ∉ l) :
has_insert.insert x l = x :: l :=
if_neg h
lemma insert_pos [decidable_eq α] {x : α} {l : list α} (h : x ∈ l) :
has_insert.insert x l = l :=
if_pos h
lemma doubleton_eq [decidable_eq α] {x y : α} (h : x ≠ y) : ({x, y} : list α) = [x, y] :=
by { rw [insert_neg, singleton_eq], rwa [singleton_eq, mem_singleton] }
/-! ### bounded quantifiers over lists -/
theorem forall_mem_nil (p : α → Prop) : ∀ x ∈ @nil α, p x.
theorem forall_mem_cons : ∀ {p : α → Prop} {a : α} {l : list α},
(∀ x ∈ a :: l, p x) ↔ p a ∧ ∀ x ∈ l, p x :=
ball_cons
theorem forall_mem_of_forall_mem_cons {p : α → Prop} {a : α} {l : list α}
(h : ∀ x ∈ a :: l, p x) :
∀ x ∈ l, p x :=
(forall_mem_cons.1 h).2
theorem forall_mem_singleton {p : α → Prop} {a : α} : (∀ x ∈ [a], p x) ↔ p a :=
by simp only [mem_singleton, forall_eq]
theorem forall_mem_append {p : α → Prop} {l₁ l₂ : list α} :
(∀ x ∈ l₁ ++ l₂, p x) ↔ (∀ x ∈ l₁, p x) ∧ (∀ x ∈ l₂, p x) :=
by simp only [mem_append, or_imp_distrib, forall_and_distrib]
theorem not_exists_mem_nil (p : α → Prop) : ¬ ∃ x ∈ @nil α, p x.
theorem exists_mem_cons_of {p : α → Prop} {a : α} (l : list α) (h : p a) :
∃ x ∈ a :: l, p x :=
bex.intro a (mem_cons_self _ _) h
theorem exists_mem_cons_of_exists {p : α → Prop} {a : α} {l : list α} (h : ∃ x ∈ l, p x) :
∃ x ∈ a :: l, p x :=
bex.elim h (λ x xl px, bex.intro x (mem_cons_of_mem _ xl) px)
theorem or_exists_of_exists_mem_cons {p : α → Prop} {a : α} {l : list α} (h : ∃ x ∈ a :: l, p x) :
p a ∨ ∃ x ∈ l, p x :=
bex.elim h (λ x xal px,
or.elim (eq_or_mem_of_mem_cons xal)
(assume : x = a, begin rw ←this, left, exact px end)
(assume : x ∈ l, or.inr (bex.intro x this px)))
theorem exists_mem_cons_iff (p : α → Prop) (a : α) (l : list α) :
(∃ x ∈ a :: l, p x) ↔ p a ∨ ∃ x ∈ l, p x :=
iff.intro or_exists_of_exists_mem_cons
(assume h, or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists)
/-! ### list subset -/
theorem subset_def {l₁ l₂ : list α} : l₁ ⊆ l₂ ↔ ∀ ⦃a : α⦄, a ∈ l₁ → a ∈ l₂ := iff.rfl
theorem subset_append_of_subset_left (l l₁ l₂ : list α) : l ⊆ l₁ → l ⊆ l₁++l₂ :=
λ s, subset.trans s $ subset_append_left _ _
theorem subset_append_of_subset_right (l l₁ l₂ : list α) : l ⊆ l₂ → l ⊆ l₁++l₂ :=
λ s, subset.trans s $ subset_append_right _ _
@[simp] theorem cons_subset {a : α} {l m : list α} :
a::l ⊆ m ↔ a ∈ m ∧ l ⊆ m :=
by simp only [subset_def, mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq]
theorem cons_subset_of_subset_of_mem {a : α} {l m : list α}
(ainm : a ∈ m) (lsubm : l ⊆ m) : a::l ⊆ m :=
cons_subset.2 ⟨ainm, lsubm⟩
theorem append_subset_of_subset_of_subset {l₁ l₂ l : list α} (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) :
l₁ ++ l₂ ⊆ l :=
λ a h, (mem_append.1 h).elim (@l₁subl _) (@l₂subl _)
@[simp] theorem append_subset_iff {l₁ l₂ l : list α} :
l₁ ++ l₂ ⊆ l ↔ l₁ ⊆ l ∧ l₂ ⊆ l :=
begin
split,
{ intro h, simp only [subset_def] at *, split; intros; simp* },
{ rintro ⟨h1, h2⟩, apply append_subset_of_subset_of_subset h1 h2 }
end
theorem eq_nil_of_subset_nil : ∀ {l : list α}, l ⊆ [] → l = []
| [] s := rfl
| (a::l) s := false.elim $ s $ mem_cons_self a l
theorem eq_nil_iff_forall_not_mem {l : list α} : l = [] ↔ ∀ a, a ∉ l :=
show l = [] ↔ l ⊆ [], from ⟨λ e, e ▸ subset.refl _, eq_nil_of_subset_nil⟩
theorem map_subset {l₁ l₂ : list α} (f : α → β) (H : l₁ ⊆ l₂) : map f l₁ ⊆ map f l₂ :=
λ x, by simp only [mem_map, not_and, exists_imp_distrib, and_imp]; exact λ a h e, ⟨a, H h, e⟩
theorem map_subset_iff {l₁ l₂ : list α} (f : α → β) (h : injective f) :
map f l₁ ⊆ map f l₂ ↔ l₁ ⊆ l₂ :=
begin
refine ⟨_, map_subset f⟩, intros h2 x hx,
rcases mem_map.1 (h2 (mem_map_of_mem f hx)) with ⟨x', hx', hxx'⟩,
cases h hxx', exact hx'
end
/-! ### append -/
lemma append_eq_has_append {L₁ L₂ : list α} : list.append L₁ L₂ = L₁ ++ L₂ := rfl
@[simp] lemma singleton_append {x : α} {l : list α} : [x] ++ l = x :: l := rfl
theorem append_ne_nil_of_ne_nil_left (s t : list α) : s ≠ [] → s ++ t ≠ [] :=
by induction s; intros; contradiction
theorem append_ne_nil_of_ne_nil_right (s t : list α) : t ≠ [] → s ++ t ≠ [] :=
by induction s; intros; contradiction
@[simp] lemma append_eq_nil {p q : list α} : (p ++ q) = [] ↔ p = [] ∧ q = [] :=
by cases p; simp only [nil_append, cons_append, eq_self_iff_true, true_and, false_and]
@[simp] lemma nil_eq_append_iff {a b : list α} : [] = a ++ b ↔ a = [] ∧ b = [] :=
by rw [eq_comm, append_eq_nil]
lemma append_eq_cons_iff {a b c : list α} {x : α} :
a ++ b = x :: c ↔ (a = [] ∧ b = x :: c) ∨ (∃a', a = x :: a' ∧ c = a' ++ b) :=
by cases a; simp only [and_assoc, @eq_comm _ c, nil_append, cons_append, eq_self_iff_true,
true_and, false_and, exists_false, false_or, or_false, exists_and_distrib_left, exists_eq_left']
lemma cons_eq_append_iff {a b c : list α} {x : α} :
(x :: c : list α) = a ++ b ↔ (a = [] ∧ b = x :: c) ∨ (∃a', a = x :: a' ∧ c = a' ++ b) :=
by rw [eq_comm, append_eq_cons_iff]
lemma append_eq_append_iff {a b c d : list α} :
a ++ b = c ++ d ↔ (∃a', c = a ++ a' ∧ b = a' ++ d) ∨ (∃c', a = c ++ c' ∧ d = c' ++ b) :=
begin
induction a generalizing c,
case nil { rw nil_append, split,
{ rintro rfl, left, exact ⟨_, rfl, rfl⟩ },
{ rintro (⟨a', rfl, rfl⟩ | ⟨a', H, rfl⟩), {refl}, {rw [← append_assoc, ← H], refl} } },
case cons : a as ih {
cases c,
{ simp only [cons_append, nil_append, false_and, exists_false, false_or, exists_eq_left'],
exact eq_comm },
{ simp only [cons_append, @eq_comm _ a, ih, and_assoc, and_or_distrib_left,
exists_and_distrib_left] } }
end
@[simp] theorem split_at_eq_take_drop : ∀ (n : ℕ) (l : list α), split_at n l = (take n l, drop n l)
| 0 a := rfl
| (succ n) [] := rfl
| (succ n) (x :: xs) := by simp only [split_at, split_at_eq_take_drop n xs, take, drop]
@[simp] theorem take_append_drop : ∀ (n : ℕ) (l : list α), take n l ++ drop n l = l
| 0 a := rfl
| (succ n) [] := rfl
| (succ n) (x :: xs) := congr_arg (cons x) $ take_append_drop n xs
-- TODO(Leo): cleanup proof after arith dec proc
theorem append_inj :
∀ {s₁ s₂ t₁ t₂ : list α}, s₁ ++ t₁ = s₂ ++ t₂ → length s₁ = length s₂ → s₁ = s₂ ∧ t₁ = t₂
| [] [] t₁ t₂ h hl := ⟨rfl, h⟩
| (a::s₁) [] t₁ t₂ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl
| [] (b::s₂) t₁ t₂ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl.symm
| (a::s₁) (b::s₂) t₁ t₂ h hl := list.no_confusion h $ λab hap,
let ⟨e1, e2⟩ := @append_inj s₁ s₂ t₁ t₂ hap (succ.inj hl) in
by rw [ab, e1, e2]; exact ⟨rfl, rfl⟩
theorem append_inj_right {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂)
(hl : length s₁ = length s₂) : t₁ = t₂ :=
(append_inj h hl).right
theorem append_inj_left {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂)
(hl : length s₁ = length s₂) : s₁ = s₂ :=
(append_inj h hl).left
theorem append_inj' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length t₁ = length t₂) :
s₁ = s₂ ∧ t₁ = t₂ :=
append_inj h $ @nat.add_right_cancel _ (length t₁) _ $
let hap := congr_arg length h in by simp only [length_append] at hap; rwa [← hl] at hap
theorem append_inj_right' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂)
(hl : length t₁ = length t₂) : t₁ = t₂ :=
(append_inj' h hl).right
theorem append_inj_left' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂)
(hl : length t₁ = length t₂) : s₁ = s₂ :=
(append_inj' h hl).left
theorem append_left_cancel {s t₁ t₂ : list α} (h : s ++ t₁ = s ++ t₂) : t₁ = t₂ :=
append_inj_right h rfl
theorem append_right_cancel {s₁ s₂ t : list α} (h : s₁ ++ t = s₂ ++ t) : s₁ = s₂ :=
append_inj_left' h rfl
theorem append_right_injective (s : list α) : function.injective (λ t, s ++ t) :=
λ t₁ t₂, append_left_cancel
theorem append_right_inj {t₁ t₂ : list α} (s) : s ++ t₁ = s ++ t₂ ↔ t₁ = t₂ :=
(append_right_injective s).eq_iff
theorem append_left_injective (t : list α) : function.injective (λ s, s ++ t) :=
λ s₁ s₂, append_right_cancel
theorem append_left_inj {s₁ s₂ : list α} (t) : s₁ ++ t = s₂ ++ t ↔ s₁ = s₂ :=
(append_left_injective t).eq_iff
theorem map_eq_append_split {f : α → β} {l : list α} {s₁ s₂ : list β}
(h : map f l = s₁ ++ s₂) : ∃ l₁ l₂, l = l₁ ++ l₂ ∧ map f l₁ = s₁ ∧ map f l₂ = s₂ :=
begin
have := h, rw [← take_append_drop (length s₁) l] at this ⊢,
rw map_append at this,
refine ⟨_, _, rfl, append_inj this _⟩,
rw [length_map, length_take, min_eq_left],
rw [← length_map f l, h, length_append],
apply nat.le_add_right
end
/-! ### repeat -/
@[simp] theorem repeat_succ (a : α) (n) : repeat a (n + 1) = a :: repeat a n := rfl
theorem mem_repeat {a b : α} : ∀ {n}, b ∈ repeat a n ↔ n ≠ 0 ∧ b = a
| 0 := by simp
| (n + 1) := by simp [mem_repeat]
theorem eq_of_mem_repeat {a b : α} {n} (h : b ∈ repeat a n) : b = a :=
(mem_repeat.1 h).2
theorem eq_repeat_of_mem {a : α} : ∀ {l : list α}, (∀ b ∈ l, b = a) → l = repeat a l.length
| [] H := rfl
| (b::l) H := by cases forall_mem_cons.1 H with H₁ H₂;
unfold length repeat; congr; [exact H₁, exact eq_repeat_of_mem H₂]
theorem eq_repeat' {a : α} {l : list α} : l = repeat a l.length ↔ ∀ b ∈ l, b = a :=
⟨λ h, h.symm ▸ λ b, eq_of_mem_repeat, eq_repeat_of_mem⟩
theorem eq_repeat {a : α} {n} {l : list α} : l = repeat a n ↔ length l = n ∧ ∀ b ∈ l, b = a :=
⟨λ h, h.symm ▸ ⟨length_repeat _ _, λ b, eq_of_mem_repeat⟩,
λ ⟨e, al⟩, e ▸ eq_repeat_of_mem al⟩
theorem repeat_add (a : α) (m n) : repeat a (m + n) = repeat a m ++ repeat a n :=
by induction m; simp only [*, zero_add, succ_add, repeat]; split; refl
theorem repeat_subset_singleton (a : α) (n) : repeat a n ⊆ [a] :=
λ b h, mem_singleton.2 (eq_of_mem_repeat h)
@[simp] theorem map_const (l : list α) (b : β) : map (function.const α b) l = repeat b l.length :=
by induction l; [refl, simp only [*, map]]; split; refl
theorem eq_of_mem_map_const {b₁ b₂ : β} {l : list α} (h : b₁ ∈ map (function.const α b₂) l) :
b₁ = b₂ :=
by rw map_const at h; exact eq_of_mem_repeat h
@[simp] theorem map_repeat (f : α → β) (a : α) (n) : map f (repeat a n) = repeat (f a) n :=
by induction n; [refl, simp only [*, repeat, map]]; split; refl
@[simp] theorem tail_repeat (a : α) (n) : tail (repeat a n) = repeat a n.pred :=
by cases n; refl
@[simp] theorem join_repeat_nil (n : ℕ) : join (repeat [] n) = @nil α :=
by induction n; [refl, simp only [*, repeat, join, append_nil]]
lemma repeat_left_injective {n : ℕ} (hn : n ≠ 0) :
function.injective (λ a : α, repeat a n) :=
λ a b h, (eq_repeat.1 h).2 _ $ mem_repeat.2 ⟨hn, rfl⟩
lemma repeat_left_inj {a b : α} {n : ℕ} (hn : n ≠ 0) :
repeat a n = repeat b n ↔ a = b :=
(repeat_left_injective hn).eq_iff
@[simp] lemma repeat_left_inj' {a b : α} :
∀ {n}, repeat a n = repeat b n ↔ n = 0 ∨ a = b
| 0 := by simp
| (n + 1) := (repeat_left_inj n.succ_ne_zero).trans $ by simp only [n.succ_ne_zero, false_or]
lemma repeat_right_injective (a : α) : function.injective (repeat a) :=
function.left_inverse.injective (length_repeat a)
@[simp] lemma repeat_right_inj {a : α} {n m : ℕ} :
repeat a n = repeat a m ↔ n = m :=
(repeat_right_injective a).eq_iff
/-! ### pure -/
@[simp] theorem mem_pure {α} (x y : α) :
x ∈ (pure y : list α) ↔ x = y := by simp! [pure,list.ret]
/-! ### bind -/
@[simp] theorem bind_eq_bind {α β} (f : α → list β) (l : list α) :
l >>= f = l.bind f := rfl
-- TODO: duplicate of a lemma in core
theorem bind_append (f : α → list β) (l₁ l₂ : list α) :
(l₁ ++ l₂).bind f = l₁.bind f ++ l₂.bind f :=
append_bind _ _ _
@[simp] theorem bind_singleton (f : α → list β) (x : α) : [x].bind f = f x :=
append_nil (f x)
/-! ### concat -/
theorem concat_nil (a : α) : concat [] a = [a] := rfl
theorem concat_cons (a b : α) (l : list α) : concat (a :: l) b = a :: concat l b := rfl
@[simp] theorem concat_eq_append (a : α) (l : list α) : concat l a = l ++ [a] :=
by induction l; simp only [*, concat]; split; refl
theorem init_eq_of_concat_eq {a : α} {l₁ l₂ : list α} : concat l₁ a = concat l₂ a → l₁ = l₂ :=
begin
intro h,
rw [concat_eq_append, concat_eq_append] at h,
exact append_right_cancel h
end
theorem last_eq_of_concat_eq {a b : α} {l : list α} : concat l a = concat l b → a = b :=
begin
intro h,
rw [concat_eq_append, concat_eq_append] at h,
exact head_eq_of_cons_eq (append_left_cancel h)
end
theorem concat_ne_nil (a : α) (l : list α) : concat l a ≠ [] :=
by simp
theorem concat_append (a : α) (l₁ l₂ : list α) : concat l₁ a ++ l₂ = l₁ ++ a :: l₂ :=
by simp
theorem length_concat (a : α) (l : list α) : length (concat l a) = succ (length l) :=
by simp only [concat_eq_append, length_append, length]
theorem append_concat (a : α) (l₁ l₂ : list α) : l₁ ++ concat l₂ a = concat (l₁ ++ l₂) a :=
by simp
/-! ### reverse -/
@[simp] theorem reverse_nil : reverse (@nil α) = [] := rfl
local attribute [simp] reverse_core
@[simp] theorem reverse_cons (a : α) (l : list α) : reverse (a::l) = reverse l ++ [a] :=
have aux : ∀ l₁ l₂, reverse_core l₁ l₂ ++ [a] = reverse_core l₁ (l₂ ++ [a]),
by intro l₁; induction l₁; intros; [refl, simp only [*, reverse_core, cons_append]],
(aux l nil).symm
theorem reverse_core_eq (l₁ l₂ : list α) : reverse_core l₁ l₂ = reverse l₁ ++ l₂ :=
by induction l₁ generalizing l₂; [refl, simp only [*, reverse_core, reverse_cons, append_assoc]];
refl
theorem reverse_cons' (a : α) (l : list α) : reverse (a::l) = concat (reverse l) a :=
by simp only [reverse_cons, concat_eq_append]
@[simp] theorem reverse_singleton (a : α) : reverse [a] = [a] := rfl
@[simp] theorem reverse_append (s t : list α) : reverse (s ++ t) = (reverse t) ++ (reverse s) :=
by induction s; [rw [nil_append, reverse_nil, append_nil],
simp only [*, cons_append, reverse_cons, append_assoc]]
theorem reverse_concat (l : list α) (a : α) : reverse (concat l a) = a :: reverse l :=
by rw [concat_eq_append, reverse_append, reverse_singleton, singleton_append]
@[simp] theorem reverse_reverse (l : list α) : reverse (reverse l) = l :=
by induction l; [refl, simp only [*, reverse_cons, reverse_append]]; refl
@[simp] theorem reverse_involutive : involutive (@reverse α) :=
λ l, reverse_reverse l
@[simp] theorem reverse_injective : injective (@reverse α) :=
reverse_involutive.injective
@[simp] theorem reverse_inj {l₁ l₂ : list α} : reverse l₁ = reverse l₂ ↔ l₁ = l₂ :=
reverse_injective.eq_iff
lemma reverse_eq_iff {l l' : list α} :
l.reverse = l' ↔ l = l'.reverse :=
reverse_involutive.eq_iff
@[simp] theorem reverse_eq_nil {l : list α} : reverse l = [] ↔ l = [] :=
@reverse_inj _ l []
theorem concat_eq_reverse_cons (a : α) (l : list α) : concat l a = reverse (a :: reverse l) :=
by simp only [concat_eq_append, reverse_cons, reverse_reverse]
@[simp] theorem length_reverse (l : list α) : length (reverse l) = length l :=
by induction l; [refl, simp only [*, reverse_cons, length_append, length]]
@[simp] theorem map_reverse (f : α → β) (l : list α) : map f (reverse l) = reverse (map f l) :=
by induction l; [refl, simp only [*, map, reverse_cons, map_append]]
theorem map_reverse_core (f : α → β) (l₁ l₂ : list α) :
map f (reverse_core l₁ l₂) = reverse_core (map f l₁) (map f l₂) :=
by simp only [reverse_core_eq, map_append, map_reverse]
@[simp] theorem mem_reverse {a : α} {l : list α} : a ∈ reverse l ↔ a ∈ l :=
by induction l; [refl, simp only [*, reverse_cons, mem_append, mem_singleton, mem_cons_iff,
not_mem_nil, false_or, or_false, or_comm]]
@[simp] theorem reverse_repeat (a : α) (n) : reverse (repeat a n) = repeat a n :=
eq_repeat.2 ⟨by simp only [length_reverse, length_repeat],
λ b h, eq_of_mem_repeat (mem_reverse.1 h)⟩
/-! ### empty -/
attribute [simp] list.empty
lemma empty_iff_eq_nil {l : list α} : l.empty ↔ l = [] :=
list.cases_on l (by simp) (by simp)
/-! ### init -/
@[simp] theorem length_init : ∀ (l : list α), length (init l) = length l - 1
| [] := rfl
| [a] := rfl
| (a :: b :: l) :=
begin
rw init,
simp only [add_left_inj, length, succ_add_sub_one],
exact length_init (b :: l)
end
/-! ### last -/
@[simp] theorem last_cons {a : α} {l : list α} :
∀ (h₁ : a :: l ≠ nil) (h₂ : l ≠ nil), last (a :: l) h₁ = last l h₂ :=
by {induction l; intros, contradiction, reflexivity}
@[simp] theorem last_append {a : α} (l : list α) (h : l ++ [a] ≠ []) : last (l ++ [a]) h = a :=
by induction l;
[refl, simp only [cons_append, last_cons _ (λ H, cons_ne_nil _ _ (append_eq_nil.1 H).2), *]]
theorem last_concat {a : α} (l : list α) (h : concat l a ≠ []) : last (concat l a) h = a :=
by simp only [concat_eq_append, last_append]
@[simp] theorem last_singleton (a : α) (h : [a] ≠ []) : last [a] h = a := rfl
@[simp] theorem last_cons_cons (a₁ a₂ : α) (l : list α) (h : a₁::a₂::l ≠ []) :
last (a₁::a₂::l) h = last (a₂::l) (cons_ne_nil a₂ l) := rfl
theorem init_append_last : ∀ {l : list α} (h : l ≠ []), init l ++ [last l h] = l
| [] h := absurd rfl h
| [a] h := rfl
| (a::b::l) h :=
begin
rw [init, cons_append, last_cons (cons_ne_nil _ _) (cons_ne_nil _ _)],
congr,
exact init_append_last (cons_ne_nil b l)
end
theorem last_congr {l₁ l₂ : list α} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) :
last l₁ h₁ = last l₂ h₂ :=
by subst l₁
theorem last_mem : ∀ {l : list α} (h : l ≠ []), last l h ∈ l
| [] h := absurd rfl h
| [a] h := or.inl rfl
| (a::b::l) h := or.inr $ by { rw [last_cons_cons], exact last_mem (cons_ne_nil b l) }
lemma last_repeat_succ (a m : ℕ) :
(repeat a m.succ).last (ne_nil_of_length_eq_succ
(show (repeat a m.succ).length = m.succ, by rw length_repeat)) = a :=
begin
induction m with k IH,
{ simp },
{ simpa only [repeat_succ, last] }
end
/-! ### last' -/
@[simp] theorem last'_is_none :
∀ {l : list α}, (last' l).is_none ↔ l = []
| [] := by simp
| [a] := by simp
| (a::b::l) := by simp [@last'_is_none (b::l)]
@[simp] theorem last'_is_some : ∀ {l : list α}, l.last'.is_some ↔ l ≠ []
| [] := by simp
| [a] := by simp
| (a::b::l) := by simp [@last'_is_some (b::l)]
theorem mem_last'_eq_last : ∀ {l : list α} {x : α}, x ∈ l.last' → ∃ h, x = last l h
| [] x hx := false.elim $ by simpa using hx
| [a] x hx := have a = x, by simpa using hx, this ▸ ⟨cons_ne_nil a [], rfl⟩
| (a::b::l) x hx :=
begin
rw last' at hx,
rcases mem_last'_eq_last hx with ⟨h₁, h₂⟩,
use cons_ne_nil _ _,
rwa [last_cons]
end
theorem mem_of_mem_last' {l : list α} {a : α} (ha : a ∈ l.last') : a ∈ l :=
let ⟨h₁, h₂⟩ := mem_last'_eq_last ha in h₂.symm ▸ last_mem _
theorem init_append_last' : ∀ {l : list α} (a ∈ l.last'), init l ++ [a] = l
| [] a ha := (option.not_mem_none a ha).elim
| [a] _ rfl := rfl
| (a :: b :: l) c hc := by { rw [last'] at hc, rw [init, cons_append, init_append_last' _ hc] }
theorem ilast_eq_last' [inhabited α] : ∀ l : list α, l.ilast = l.last'.iget
| [] := by simp [ilast, arbitrary]
| [a] := rfl
| [a, b] := rfl
| [a, b, c] := rfl
| (a :: b :: c :: l) := by simp [ilast, ilast_eq_last' (c :: l)]
@[simp] theorem last'_append_cons : ∀ (l₁ : list α) (a : α) (l₂ : list α),
last' (l₁ ++ a :: l₂) = last' (a :: l₂)
| [] a l₂ := rfl
| [b] a l₂ := rfl
| (b::c::l₁) a l₂ := by rw [cons_append, cons_append, last', ← cons_append, last'_append_cons]
theorem last'_append_of_ne_nil (l₁ : list α) : ∀ {l₂ : list α} (hl₂ : l₂ ≠ []),
last' (l₁ ++ l₂) = last' l₂
| [] hl₂ := by contradiction
| (b::l₂) _ := last'_append_cons l₁ b l₂
/-! ### head(') and tail -/
theorem head_eq_head' [inhabited α] (l : list α) : head l = (head' l).iget :=
by cases l; refl
theorem mem_of_mem_head' {x : α} : ∀ {l : list α}, x ∈ l.head' → x ∈ l
| [] h := (option.not_mem_none _ h).elim
| (a::l) h := by { simp only [head', option.mem_def] at h, exact h ▸ or.inl rfl }
@[simp] theorem head_cons [inhabited α] (a : α) (l : list α) : head (a::l) = a := rfl
@[simp] theorem tail_nil : tail (@nil α) = [] := rfl
@[simp] theorem tail_cons (a : α) (l : list α) : tail (a::l) = l := rfl
@[simp] theorem head_append [inhabited α] (t : list α) {s : list α} (h : s ≠ []) :
head (s ++ t) = head s :=
by {induction s, contradiction, refl}
theorem tail_append_singleton_of_ne_nil {a : α} {l : list α} (h : l ≠ nil) :
tail (l ++ [a]) = tail l ++ [a] :=
by { induction l, contradiction, rw [tail,cons_append,tail], }
theorem cons_head'_tail : ∀ {l : list α} {a : α} (h : a ∈ head' l), a :: tail l = l
| [] a h := by contradiction
| (b::l) a h := by { simp at h, simp [h] }
theorem head_mem_head' [inhabited α] : ∀ {l : list α} (h : l ≠ []), head l ∈ head' l
| [] h := by contradiction
| (a::l) h := rfl
theorem cons_head_tail [inhabited α] {l : list α} (h : l ≠ []) : (head l)::(tail l) = l :=
cons_head'_tail (head_mem_head' h)
lemma head_mem_self [inhabited α] {l : list α} (h : l ≠ nil) : l.head ∈ l :=
begin
have h' := mem_cons_self l.head l.tail,
rwa cons_head_tail h at h',
end
@[simp] theorem head'_map (f : α → β) (l) : head' (map f l) = (head' l).map f := by cases l; refl
lemma tail_append_of_ne_nil (l l' : list α) (h : l ≠ []) :
(l ++ l').tail = l.tail ++ l' :=
begin
cases l,
{ contradiction },
{ simp }
end
/-! ### Induction from the right -/
/-- Induction principle from the right for lists: if a property holds for the empty list, and
for `l ++ [a]` if it holds for `l`, then it holds for all lists. The principle is given for
a `Sort`-valued predicate, i.e., it can also be used to construct data. -/
@[elab_as_eliminator] def reverse_rec_on {C : list α → Sort*}
(l : list α) (H0 : C [])
(H1 : ∀ (l : list α) (a : α), C l → C (l ++ [a])) : C l :=
begin
rw ← reverse_reverse l,
induction reverse l,
{ exact H0 },
{ rw reverse_cons, exact H1 _ _ ih }
end
/-- Bidirectional induction principle for lists: if a property holds for the empty list, the
singleton list, and `a :: (l ++ [b])` from `l`, then it holds for all lists. This can be used to
prove statements about palindromes. The principle is given for a `Sort`-valued predicate, i.e., it
can also be used to construct data. -/
def bidirectional_rec {C : list α → Sort*}
(H0 : C []) (H1 : ∀ (a : α), C [a])
(Hn : ∀ (a : α) (l : list α) (b : α), C l → C (a :: (l ++ [b]))) : ∀ l, C l
| [] := H0
| [a] := H1 a
| (a :: b :: l) :=
let l' := init (b :: l), b' := last (b :: l) (cons_ne_nil _ _) in
have length l' < length (a :: b :: l), by { change _ < length l + 2, simp },
begin
rw ←init_append_last (cons_ne_nil b l),
have : C l', from bidirectional_rec l',
exact Hn a l' b' ‹C l'›
end
using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf list.length⟩] }
/-- Like `bidirectional_rec`, but with the list parameter placed first. -/
@[elab_as_eliminator] def bidirectional_rec_on {C : list α → Sort*}
(l : list α) (H0 : C []) (H1 : ∀ (a : α), C [a])
(Hn : ∀ (a : α) (l : list α) (b : α), C l → C (a :: (l ++ [b]))) : C l :=
bidirectional_rec H0 H1 Hn l
/-! ### sublists -/
@[simp] theorem nil_sublist : Π (l : list α), [] <+ l
| [] := sublist.slnil
| (a :: l) := sublist.cons _ _ a (nil_sublist l)
@[refl, simp] theorem sublist.refl : Π (l : list α), l <+ l
| [] := sublist.slnil
| (a :: l) := sublist.cons2 _ _ a (sublist.refl l)
@[trans] theorem sublist.trans {l₁ l₂ l₃ : list α} (h₁ : l₁ <+ l₂) (h₂ : l₂ <+ l₃) : l₁ <+ l₃ :=
sublist.rec_on h₂ (λ_ s, s)
(λl₂ l₃ a h₂ IH l₁ h₁, sublist.cons _ _ _ (IH l₁ h₁))
(λl₂ l₃ a h₂ IH l₁ h₁, @sublist.cases_on _ (λl₁ l₂', l₂' = a :: l₂ → l₁ <+ a :: l₃) _ _ h₁
(λ_, nil_sublist _)
(λl₁ l₂' a' h₁' e, match a', l₂', e, h₁' with ._, ._, rfl, h₁ :=
sublist.cons _ _ _ (IH _ h₁) end)
(λl₁ l₂' a' h₁' e, match a', l₂', e, h₁' with ._, ._, rfl, h₁ :=
sublist.cons2 _ _ _ (IH _ h₁) end) rfl)
l₁ h₁
@[simp] theorem sublist_cons (a : α) (l : list α) : l <+ a::l :=
sublist.cons _ _ _ (sublist.refl l)
theorem sublist_of_cons_sublist {a : α} {l₁ l₂ : list α} : a::l₁ <+ l₂ → l₁ <+ l₂ :=
sublist.trans (sublist_cons a l₁)
theorem cons_sublist_cons {l₁ l₂ : list α} (a : α) (s : l₁ <+ l₂) : a::l₁ <+ a::l₂ :=
sublist.cons2 _ _ _ s
@[simp] theorem sublist_append_left : Π (l₁ l₂ : list α), l₁ <+ l₁++l₂
| [] l₂ := nil_sublist _
| (a::l₁) l₂ := cons_sublist_cons _ (sublist_append_left l₁ l₂)
@[simp] theorem sublist_append_right : Π (l₁ l₂ : list α), l₂ <+ l₁++l₂
| [] l₂ := sublist.refl _
| (a::l₁) l₂ := sublist.cons _ _ _ (sublist_append_right l₁ l₂)
theorem sublist_cons_of_sublist (a : α) {l₁ l₂ : list α} : l₁ <+ l₂ → l₁ <+ a::l₂ :=
sublist.cons _ _ _
theorem sublist_append_of_sublist_left {l l₁ l₂ : list α} (s : l <+ l₁) : l <+ l₁++l₂ :=
s.trans $ sublist_append_left _ _
theorem sublist_append_of_sublist_right {l l₁ l₂ : list α} (s : l <+ l₂) : l <+ l₁++l₂ :=
s.trans $ sublist_append_right _ _
theorem sublist_of_cons_sublist_cons {l₁ l₂ : list α} : ∀ {a : α}, a::l₁ <+ a::l₂ → l₁ <+ l₂
| ._ (sublist.cons ._ ._ a s) := sublist_of_cons_sublist s
| ._ (sublist.cons2 ._ ._ a s) := s
theorem cons_sublist_cons_iff {l₁ l₂ : list α} {a : α} : a::l₁ <+ a::l₂ ↔ l₁ <+ l₂ :=
⟨sublist_of_cons_sublist_cons, cons_sublist_cons _⟩
@[simp] theorem append_sublist_append_left {l₁ l₂ : list α} : ∀ l, l++l₁ <+ l++l₂ ↔ l₁ <+ l₂
| [] := iff.rfl
| (a::l) := cons_sublist_cons_iff.trans (append_sublist_append_left l)
theorem sublist.append_right {l₁ l₂ : list α} (h : l₁ <+ l₂) (l) : l₁++l <+ l₂++l :=
begin
induction h with _ _ a _ ih _ _ a _ ih,
{ refl },
{ apply sublist_cons_of_sublist a ih },
{ apply cons_sublist_cons a ih }
end
theorem sublist_or_mem_of_sublist {l l₁ l₂ : list α} {a : α} (h : l <+ l₁ ++ a::l₂) :
l <+ l₁ ++ l₂ ∨ a ∈ l :=
begin
induction l₁ with b l₁ IH generalizing l,
{ cases h, { left, exact ‹l <+ l₂› }, { right, apply mem_cons_self } },
{ cases h with _ _ _ h _ _ _ h,
{ exact or.imp_left (sublist_cons_of_sublist _) (IH h) },
{ exact (IH h).imp (cons_sublist_cons _) (mem_cons_of_mem _) } }
end
theorem sublist.reverse {l₁ l₂ : list α} (h : l₁ <+ l₂) : l₁.reverse <+ l₂.reverse :=
begin
induction h with _ _ _ _ ih _ _ a _ ih, {refl},
{ rw reverse_cons, exact sublist_append_of_sublist_left ih },
{ rw [reverse_cons, reverse_cons], exact ih.append_right [a] }
end
@[simp] theorem reverse_sublist_iff {l₁ l₂ : list α} : l₁.reverse <+ l₂.reverse ↔ l₁ <+ l₂ :=
⟨λ h, l₁.reverse_reverse ▸ l₂.reverse_reverse ▸ h.reverse, sublist.reverse⟩
@[simp] theorem append_sublist_append_right {l₁ l₂ : list α} (l) : l₁++l <+ l₂++l ↔ l₁ <+ l₂ :=
⟨λ h, by simpa only [reverse_append, append_sublist_append_left, reverse_sublist_iff]
using h.reverse,
λ h, h.append_right l⟩
theorem sublist.append {l₁ l₂ r₁ r₂ : list α}
(hl : l₁ <+ l₂) (hr : r₁ <+ r₂) : l₁ ++ r₁ <+ l₂ ++ r₂ :=
(hl.append_right _).trans ((append_sublist_append_left _).2 hr)
theorem sublist.subset : Π {l₁ l₂ : list α}, l₁ <+ l₂ → l₁ ⊆ l₂
| ._ ._ sublist.slnil b h := h
| ._ ._ (sublist.cons l₁ l₂ a s) b h := mem_cons_of_mem _ (sublist.subset s h)
| ._ ._ (sublist.cons2 l₁ l₂ a s) b h :=
match eq_or_mem_of_mem_cons h with
| or.inl h := h ▸ mem_cons_self _ _
| or.inr h := mem_cons_of_mem _ (sublist.subset s h)
end
theorem singleton_sublist {a : α} {l} : [a] <+ l ↔ a ∈ l :=
⟨λ h, h.subset (mem_singleton_self _), λ h,
let ⟨s, t, e⟩ := mem_split h in e.symm ▸
(cons_sublist_cons _ (nil_sublist _)).trans (sublist_append_right _ _)⟩
theorem eq_nil_of_sublist_nil {l : list α} (s : l <+ []) : l = [] :=
eq_nil_of_subset_nil $ s.subset
@[simp] theorem sublist_nil_iff_eq_nil {l : list α} : l <+ [] ↔ l = [] :=
⟨eq_nil_of_sublist_nil, λ H, H ▸ sublist.refl _⟩
theorem repeat_sublist_repeat (a : α) {m n} : repeat a m <+ repeat a n ↔ m ≤ n :=
⟨λ h, by simpa only [length_repeat] using length_le_of_sublist h,
λ h, by induction h; [refl, simp only [*, repeat_succ, sublist.cons]] ⟩
theorem eq_of_sublist_of_length_eq : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → length l₁ = length l₂ → l₁ = l₂
| ._ ._ sublist.slnil h := rfl
| ._ ._ (sublist.cons l₁ l₂ a s) h :=
absurd (length_le_of_sublist s) $ not_le_of_gt $ by rw h; apply lt_succ_self
| ._ ._ (sublist.cons2 l₁ l₂ a s) h :=
by rw [length, length] at h; injection h with h; rw eq_of_sublist_of_length_eq s h
theorem eq_of_sublist_of_length_le {l₁ l₂ : list α} (s : l₁ <+ l₂) (h : length l₂ ≤ length l₁) :
l₁ = l₂ :=
eq_of_sublist_of_length_eq s (le_antisymm (length_le_of_sublist s) h)
theorem sublist.antisymm {l₁ l₂ : list α} (s₁ : l₁ <+ l₂) (s₂ : l₂ <+ l₁) : l₁ = l₂ :=
eq_of_sublist_of_length_le s₁ (length_le_of_sublist s₂)
instance decidable_sublist [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <+ l₂)
| [] l₂ := is_true $ nil_sublist _
| (a::l₁) [] := is_false $ λh, list.no_confusion $ eq_nil_of_sublist_nil h
| (a::l₁) (b::l₂) :=
if h : a = b then
decidable_of_decidable_of_iff (decidable_sublist l₁ l₂) $
by rw [← h]; exact ⟨cons_sublist_cons _, sublist_of_cons_sublist_cons⟩
else decidable_of_decidable_of_iff (decidable_sublist (a::l₁) l₂)
⟨sublist_cons_of_sublist _, λs, match a, l₁, s, h with
| a, l₁, sublist.cons ._ ._ ._ s', h := s'
| ._, ._, sublist.cons2 t ._ ._ s', h := absurd rfl h
end⟩
/-! ### index_of -/
section index_of
variable [decidable_eq α]
@[simp] theorem index_of_nil (a : α) : index_of a [] = 0 := rfl
theorem index_of_cons (a b : α) (l : list α) :
index_of a (b::l) = if a = b then 0 else succ (index_of a l) := rfl
theorem index_of_cons_eq {a b : α} (l : list α) : a = b → index_of a (b::l) = 0 :=
assume e, if_pos e
@[simp] theorem index_of_cons_self (a : α) (l : list α) : index_of a (a::l) = 0 :=
index_of_cons_eq _ rfl
@[simp, priority 990]
theorem index_of_cons_ne {a b : α} (l : list α) : a ≠ b → index_of a (b::l) = succ (index_of a l) :=
assume n, if_neg n
theorem index_of_eq_length {a : α} {l : list α} : index_of a l = length l ↔ a ∉ l :=
begin
induction l with b l ih,
{ exact iff_of_true rfl (not_mem_nil _) },
simp only [length, mem_cons_iff, index_of_cons], split_ifs,
{ exact iff_of_false (by rintro ⟨⟩) (λ H, H $ or.inl h) },
{ simp only [h, false_or], rw ← ih, exact succ_inj' }
end
@[simp, priority 980]
theorem index_of_of_not_mem {l : list α} {a : α} : a ∉ l → index_of a l = length l :=
index_of_eq_length.2
theorem index_of_le_length {a : α} {l : list α} : index_of a l ≤ length l :=
begin
induction l with b l ih, {refl},
simp only [length, index_of_cons],
by_cases h : a = b, {rw if_pos h, exact nat.zero_le _},
rw if_neg h, exact succ_le_succ ih
end
theorem index_of_lt_length {a} {l : list α} : index_of a l < length l ↔ a ∈ l :=
⟨λh, decidable.by_contradiction $ λ al, ne_of_lt h $ index_of_eq_length.2 al,
λal, lt_of_le_of_ne index_of_le_length $ λ h, index_of_eq_length.1 h al⟩
end index_of
/-! ### nth element -/
theorem nth_le_of_mem : ∀ {a} {l : list α}, a ∈ l → ∃ n h, nth_le l n h = a
| a (_ :: l) (or.inl rfl) := ⟨0, succ_pos _, rfl⟩
| a (b :: l) (or.inr m) :=
let ⟨n, h, e⟩ := nth_le_of_mem m in ⟨n+1, succ_lt_succ h, e⟩
theorem nth_le_nth : ∀ {l : list α} {n} h, nth l n = some (nth_le l n h)
| (a :: l) 0 h := rfl
| (a :: l) (n+1) h := @nth_le_nth l n _
theorem nth_len_le : ∀ {l : list α} {n}, length l ≤ n → nth l n = none
| [] n h := rfl
| (a :: l) (n+1) h := nth_len_le (le_of_succ_le_succ h)
theorem nth_eq_some {l : list α} {n a} : nth l n = some a ↔ ∃ h, nth_le l n h = a :=
⟨λ e,
have h : n < length l, from lt_of_not_ge $ λ hn,
by rw nth_len_le hn at e; contradiction,
⟨h, by rw nth_le_nth h at e;
injection e with e; apply nth_le_mem⟩,
λ ⟨h, e⟩, e ▸ nth_le_nth _⟩
@[simp]
theorem nth_eq_none_iff : ∀ {l : list α} {n}, nth l n = none ↔ length l ≤ n :=
begin
intros, split,
{ intro h, by_contradiction h',
have h₂ : ∃ h, l.nth_le n h = l.nth_le n (lt_of_not_ge h') := ⟨lt_of_not_ge h', rfl⟩,
rw [← nth_eq_some, h] at h₂, cases h₂ },
{ solve_by_elim [nth_len_le] },
end
theorem nth_of_mem {a} {l : list α} (h : a ∈ l) : ∃ n, nth l n = some a :=
let ⟨n, h, e⟩ := nth_le_of_mem h in ⟨n, by rw [nth_le_nth, e]⟩
theorem nth_le_mem : ∀ (l : list α) n h, nth_le l n h ∈ l
| (a :: l) 0 h := mem_cons_self _ _
| (a :: l) (n+1) h := mem_cons_of_mem _ (nth_le_mem l _ _)
theorem nth_mem {l : list α} {n a} (e : nth l n = some a) : a ∈ l :=
let ⟨h, e⟩ := nth_eq_some.1 e in e ▸ nth_le_mem _ _ _
theorem mem_iff_nth_le {a} {l : list α} : a ∈ l ↔ ∃ n h, nth_le l n h = a :=
⟨nth_le_of_mem, λ ⟨n, h, e⟩, e ▸ nth_le_mem _ _ _⟩
theorem mem_iff_nth {a} {l : list α} : a ∈ l ↔ ∃ n, nth l n = some a :=
mem_iff_nth_le.trans $ exists_congr $ λ n, nth_eq_some.symm
lemma nth_zero (l : list α) : l.nth 0 = l.head' := by cases l; refl
lemma nth_injective {α : Type u} {xs : list α} {i j : ℕ}
(h₀ : i < xs.length)
(h₁ : nodup xs)
(h₂ : xs.nth i = xs.nth j) : i = j :=
begin
induction xs with x xs generalizing i j,
{ cases h₀ },
{ cases i; cases j,
case nat.zero nat.zero
{ refl },
case nat.succ nat.succ
{ congr, cases h₁,
apply xs_ih;
solve_by_elim [lt_of_succ_lt_succ] },
iterate 2
{ dsimp at h₂,
cases h₁ with _ _ h h',
cases h x _ rfl,
rw mem_iff_nth,
exact ⟨_, h₂.symm⟩ <|>
exact ⟨_, h₂⟩ } },
end
@[simp] theorem nth_map (f : α → β) : ∀ l n, nth (map f l) n = (nth l n).map f
| [] n := rfl
| (a :: l) 0 := rfl
| (a :: l) (n+1) := nth_map l n
theorem nth_le_map (f : α → β) {l n} (H1 H2) : nth_le (map f l) n H1 = f (nth_le l n H2) :=
option.some.inj $ by rw [← nth_le_nth, nth_map, nth_le_nth]; refl
/-- A version of `nth_le_map` that can be used for rewriting. -/
theorem nth_le_map_rev (f : α → β) {l n} (H) :
f (nth_le l n H) = nth_le (map f l) n ((length_map f l).symm ▸ H) :=
(nth_le_map f _ _).symm
@[simp] theorem nth_le_map' (f : α → β) {l n} (H) :
nth_le (map f l) n H = f (nth_le l n (length_map f l ▸ H)) :=
nth_le_map f _ _
/-- If one has `nth_le L i hi` in a formula and `h : L = L'`, one can not `rw h` in the formula as
`hi` gives `i < L.length` and not `i < L'.length`. The lemma `nth_le_of_eq` can be used to make
such a rewrite, with `rw (nth_le_of_eq h)`. -/
lemma nth_le_of_eq {L L' : list α} (h : L = L') {i : ℕ} (hi : i < L.length) :
nth_le L i hi = nth_le L' i (h ▸ hi) :=
by { congr, exact h}
@[simp] lemma nth_le_singleton (a : α) {n : ℕ} (hn : n < 1) :
nth_le [a] n hn = a :=
have hn0 : n = 0 := le_zero_iff.1 (le_of_lt_succ hn),
by subst hn0; refl
lemma nth_le_zero [inhabited α] {L : list α} (h : 0 < L.length) :
L.nth_le 0 h = L.head :=
by { cases L, cases h, simp, }
lemma nth_le_append : ∀ {l₁ l₂ : list α} {n : ℕ} (hn₁) (hn₂),
(l₁ ++ l₂).nth_le n hn₁ = l₁.nth_le n hn₂
| [] _ n hn₁ hn₂ := (not_lt_zero _ hn₂).elim
| (a::l) _ 0 hn₁ hn₂ := rfl
| (a::l) _ (n+1) hn₁ hn₂ := by simp only [nth_le, cons_append];
exact nth_le_append _ _
lemma nth_le_append_right_aux {l₁ l₂ : list α} {n : ℕ}
(h₁ : l₁.length ≤ n) (h₂ : n < (l₁ ++ l₂).length) : n - l₁.length < l₂.length :=
begin
rw list.length_append at h₂,
convert (nat.sub_lt_sub_right_iff h₁).mpr h₂,
simp,
end
lemma nth_le_append_right : ∀ {l₁ l₂ : list α} {n : ℕ} (h₁ : l₁.length ≤ n) (h₂),
(l₁ ++ l₂).nth_le n h₂ = l₂.nth_le (n - l₁.length) (nth_le_append_right_aux h₁ h₂)
| [] _ n h₁ h₂ := rfl
| (a :: l) _ (n+1) h₁ h₂ :=
begin
dsimp,
conv { to_rhs, congr, skip, rw [←nat.sub_sub, nat.sub.right_comm, nat.add_sub_cancel], },
rw nth_le_append_right (nat.lt_succ_iff.mp h₁),
end
@[simp] lemma nth_le_repeat (a : α) {n m : ℕ} (h : m < (list.repeat a n).length) :
(list.repeat a n).nth_le m h = a :=
eq_of_mem_repeat (nth_le_mem _ _ _)
lemma nth_append {l₁ l₂ : list α} {n : ℕ} (hn : n < l₁.length) :
(l₁ ++ l₂).nth n = l₁.nth n :=
have hn' : n < (l₁ ++ l₂).length := lt_of_lt_of_le hn
(by rw length_append; exact le_add_right _ _),
by rw [nth_le_nth hn, nth_le_nth hn', nth_le_append]
lemma nth_append_right {l₁ l₂ : list α} {n : ℕ} (hn : l₁.length ≤ n) :
(l₁ ++ l₂).nth n = l₂.nth (n - l₁.length) :=
begin
by_cases hl : n < (l₁ ++ l₂).length,
{ rw [nth_le_nth hl, nth_le_nth, nth_le_append_right hn] },
{ rw [nth_len_le (le_of_not_lt hl), nth_len_le],
rw [not_lt, length_append] at hl,
exact nat.le_sub_left_of_add_le hl }
end
lemma last_eq_nth_le : ∀ (l : list α) (h : l ≠ []),
last l h = l.nth_le (l.length - 1) (sub_lt (length_pos_of_ne_nil h) one_pos)
| [] h := rfl
| [a] h := by rw [last_singleton, nth_le_singleton]
| (a :: b :: l) h := by { rw [last_cons, last_eq_nth_le (b :: l)],
refl, exact cons_ne_nil b l }
@[simp] lemma nth_concat_length : ∀ (l : list α) (a : α), (l ++ [a]).nth l.length = some a
| [] a := rfl
| (b::l) a := by rw [cons_append, length_cons, nth, nth_concat_length]
lemma nth_le_cons_length (x : α) (xs : list α) (n : ℕ) (h : n = xs.length) :
(x :: xs).nth_le n (by simp [h]) = (x :: xs).last (cons_ne_nil x xs) :=
begin
rw last_eq_nth_le,
congr,
simp [h]
end
@[ext]
theorem ext : ∀ {l₁ l₂ : list α}, (∀n, nth l₁ n = nth l₂ n) → l₁ = l₂
| [] [] h := rfl
| (a::l₁) [] h := by have h0 := h 0; contradiction
| [] (a'::l₂) h := by have h0 := h 0; contradiction
| (a::l₁) (a'::l₂) h := by have h0 : some a = some a' := h 0; injection h0 with aa;
simp only [aa, ext (λn, h (n+1))]; split; refl
theorem ext_le {l₁ l₂ : list α} (hl : length l₁ = length l₂)
(h : ∀n h₁ h₂, nth_le l₁ n h₁ = nth_le l₂ n h₂) : l₁ = l₂ :=
ext $ λn, if h₁ : n < length l₁
then by rw [nth_le_nth, nth_le_nth, h n h₁ (by rwa [← hl])]
else let h₁ := le_of_not_gt h₁ in by { rw [nth_len_le h₁, nth_len_le], rwa [←hl], }
@[simp] theorem index_of_nth_le [decidable_eq α] {a : α} :
∀ {l : list α} h, nth_le l (index_of a l) h = a
| (b::l) h := by by_cases h' : a = b;
simp only [h', if_pos, if_false, index_of_cons, nth_le, @index_of_nth_le l]
@[simp] theorem index_of_nth [decidable_eq α] {a : α} {l : list α} (h : a ∈ l) :
nth l (index_of a l) = some a :=
by rw [nth_le_nth, index_of_nth_le (index_of_lt_length.2 h)]
theorem nth_le_reverse_aux1 :
∀ (l r : list α) (i h1 h2), nth_le (reverse_core l r) (i + length l) h1 = nth_le r i h2
| [] r i := λh1 h2, rfl
| (a :: l) r i :=
by rw (show i + length (a :: l) = i + 1 + length l, from add_right_comm i (length l) 1);
exact λh1 h2, nth_le_reverse_aux1 l (a :: r) (i+1) h1 (succ_lt_succ h2)
lemma index_of_inj [decidable_eq α] {l : list α} {x y : α}
(hx : x ∈ l) (hy : y ∈ l) : index_of x l = index_of y l ↔ x = y :=
⟨λ h, have nth_le l (index_of x l) (index_of_lt_length.2 hx) =
nth_le l (index_of y l) (index_of_lt_length.2 hy),
by simp only [h],
by simpa only [index_of_nth_le],
λ h, by subst h⟩
theorem nth_le_reverse_aux2 : ∀ (l r : list α) (i : nat) (h1) (h2),
nth_le (reverse_core l r) (length l - 1 - i) h1 = nth_le l i h2
| [] r i h1 h2 := absurd h2 (not_lt_zero _)
| (a :: l) r 0 h1 h2 := begin
have aux := nth_le_reverse_aux1 l (a :: r) 0,
rw zero_add at aux,
exact aux _ (zero_lt_succ _)
end
| (a :: l) r (i+1) h1 h2 := begin
have aux := nth_le_reverse_aux2 l (a :: r) i,
have heq := calc length (a :: l) - 1 - (i + 1)
= length l - (1 + i) : by rw add_comm; refl
... = length l - 1 - i : by rw nat.sub_sub,
rw [← heq] at aux,
apply aux
end
@[simp] theorem nth_le_reverse (l : list α) (i : nat) (h1 h2) :
nth_le (reverse l) (length l - 1 - i) h1 = nth_le l i h2 :=
nth_le_reverse_aux2 _ _ _ _ _
lemma nth_le_reverse' (l : list α) (n : ℕ) (hn : n < l.reverse.length) (hn') :
l.reverse.nth_le n hn = l.nth_le (l.length - 1 - n) hn' :=
begin
rw eq_comm,
convert nth_le_reverse l.reverse _ _ _ using 1,
{ simp },
{ simpa }
end
lemma eq_cons_of_length_one {l : list α} (h : l.length = 1) :
l = [l.nth_le 0 (h.symm ▸ zero_lt_one)] :=
begin
refine ext_le (by convert h) (λ n h₁ h₂, _),
simp only [nth_le_singleton],
congr,
exact eq_bot_iff.mpr (nat.lt_succ_iff.mp h₂)
end
lemma modify_nth_tail_modify_nth_tail {f g : list α → list α} (m : ℕ) :
∀n (l:list α), (l.modify_nth_tail f n).modify_nth_tail g (m + n) =
l.modify_nth_tail (λl, (f l).modify_nth_tail g m) n
| 0 l := rfl
| (n+1) [] := rfl
| (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_modify_nth_tail n l)
lemma modify_nth_tail_modify_nth_tail_le
{f g : list α → list α} (m n : ℕ) (l : list α) (h : n ≤ m) :
(l.modify_nth_tail f n).modify_nth_tail g m =
l.modify_nth_tail (λl, (f l).modify_nth_tail g (m - n)) n :=
begin
rcases le_iff_exists_add.1 h with ⟨m, rfl⟩,
rw [nat.add_sub_cancel_left, add_comm, modify_nth_tail_modify_nth_tail]
end
lemma modify_nth_tail_modify_nth_tail_same {f g : list α → list α} (n : ℕ) (l:list α) :
(l.modify_nth_tail f n).modify_nth_tail g n = l.modify_nth_tail (g ∘ f) n :=
by rw [modify_nth_tail_modify_nth_tail_le n n l (le_refl n), nat.sub_self]; refl
lemma modify_nth_tail_id :
∀n (l:list α), l.modify_nth_tail id n = l
| 0 l := rfl
| (n+1) [] := rfl
| (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_id n l)
theorem remove_nth_eq_nth_tail : ∀ n (l : list α), remove_nth l n = modify_nth_tail tail n l
| 0 l := by cases l; refl
| (n+1) [] := rfl
| (n+1) (a::l) := congr_arg (cons _) (remove_nth_eq_nth_tail _ _)
theorem update_nth_eq_modify_nth (a : α) : ∀ n (l : list α),
update_nth l n a = modify_nth (λ _, a) n l
| 0 l := by cases l; refl
| (n+1) [] := rfl
| (n+1) (b::l) := congr_arg (cons _) (update_nth_eq_modify_nth _ _)
theorem modify_nth_eq_update_nth (f : α → α) : ∀ n (l : list α),
modify_nth f n l = ((λ a, update_nth l n (f a)) <$> nth l n).get_or_else l
| 0 l := by cases l; refl
| (n+1) [] := rfl
| (n+1) (b::l) := (congr_arg (cons b)
(modify_nth_eq_update_nth n l)).trans $ by cases nth l n; refl
theorem nth_modify_nth (f : α → α) : ∀ n (l : list α) m,
nth (modify_nth f n l) m = (λ a, if n = m then f a else a) <$> nth l m
| n l 0 := by cases l; cases n; refl
| n [] (m+1) := by cases n; refl
| 0 (a::l) (m+1) := by cases nth l m; refl
| (n+1) (a::l) (m+1) := (nth_modify_nth n l m).trans $
by cases nth l m with b; by_cases n = m;
simp only [h, if_pos, if_true, if_false, option.map_none, option.map_some, mt succ.inj,
not_false_iff]
theorem modify_nth_tail_length (f : list α → list α) (H : ∀ l, length (f l) = length l) :
∀ n l, length (modify_nth_tail f n l) = length l
| 0 l := H _
| (n+1) [] := rfl
| (n+1) (a::l) := @congr_arg _ _ _ _ (+1) (modify_nth_tail_length _ _)
@[simp] theorem modify_nth_length (f : α → α) :
∀ n l, length (modify_nth f n l) = length l :=
modify_nth_tail_length _ (λ l, by cases l; refl)
@[simp] theorem update_nth_length (l : list α) (n) (a : α) :
length (update_nth l n a) = length l :=
by simp only [update_nth_eq_modify_nth, modify_nth_length]
@[simp] theorem nth_modify_nth_eq (f : α → α) (n) (l : list α) :
nth (modify_nth f n l) n = f <$> nth l n :=
by simp only [nth_modify_nth, if_pos]
@[simp] theorem nth_modify_nth_ne (f : α → α) {m n} (l : list α) (h : m ≠ n) :
nth (modify_nth f m l) n = nth l n :=
by simp only [nth_modify_nth, if_neg h, id_map']
theorem nth_update_nth_eq (a : α) (n) (l : list α) :
nth (update_nth l n a) n = (λ _, a) <$> nth l n :=
by simp only [update_nth_eq_modify_nth, nth_modify_nth_eq]
theorem nth_update_nth_of_lt (a : α) {n} {l : list α} (h : n < length l) :
nth (update_nth l n a) n = some a :=
by rw [nth_update_nth_eq, nth_le_nth h]; refl
theorem nth_update_nth_ne (a : α) {m n} (l : list α) (h : m ≠ n) :
nth (update_nth l m a) n = nth l n :=
by simp only [update_nth_eq_modify_nth, nth_modify_nth_ne _ _ h]
@[simp] lemma update_nth_nil (n : ℕ) (a : α) : [].update_nth n a = [] := rfl
@[simp] lemma update_nth_succ (x : α) (xs : list α) (n : ℕ) (a : α) :
(x :: xs).update_nth n.succ a = x :: xs.update_nth n a := rfl
lemma update_nth_comm (a b : α) : Π {n m : ℕ} (l : list α) (h : n ≠ m),
(l.update_nth n a).update_nth m b = (l.update_nth m b).update_nth n a
| _ _ [] _ := by simp
| 0 0 (x :: t) h := absurd rfl h
| (n + 1) 0 (x :: t) h := by simp [list.update_nth]
| 0 (m + 1) (x :: t) h := by simp [list.update_nth]
| (n + 1) (m + 1) (x :: t) h := by { simp only [update_nth, true_and, eq_self_iff_true],
exact update_nth_comm t (λ h', h $ nat.succ_inj'.mpr h'), }
@[simp] lemma nth_le_update_nth_eq (l : list α) (i : ℕ) (a : α)
(h : i < (l.update_nth i a).length) : (l.update_nth i a).nth_le i h = a :=
by rw [← option.some_inj, ← nth_le_nth, nth_update_nth_eq, nth_le_nth]; simp * at *
@[simp] lemma nth_le_update_nth_of_ne {l : list α} {i j : ℕ} (h : i ≠ j) (a : α)
(hj : j < (l.update_nth i a).length) :
(l.update_nth i a).nth_le j hj = l.nth_le j (by simpa using hj) :=
by rw [← option.some_inj, ← list.nth_le_nth, list.nth_update_nth_ne _ _ h, list.nth_le_nth]
lemma mem_or_eq_of_mem_update_nth : ∀ {l : list α} {n : ℕ} {a b : α}
(h : a ∈ l.update_nth n b), a ∈ l ∨ a = b
| [] n a b h := false.elim h
| (c::l) 0 a b h := ((mem_cons_iff _ _ _).1 h).elim
or.inr (or.inl ∘ mem_cons_of_mem _)
| (c::l) (n+1) a b h := ((mem_cons_iff _ _ _).1 h).elim
(λ h, h ▸ or.inl (mem_cons_self _ _))
(λ h, (mem_or_eq_of_mem_update_nth h).elim
(or.inl ∘ mem_cons_of_mem _) or.inr)
section insert_nth
variable {a : α}
@[simp] lemma insert_nth_nil (a : α) : insert_nth 0 a [] = [a] := rfl
@[simp] lemma insert_nth_succ_nil (n : ℕ) (a : α) : insert_nth (n + 1) a [] = [] := rfl
lemma length_insert_nth : ∀n as, n ≤ length as → length (insert_nth n a as) = length as + 1
| 0 as h := rfl
| (n+1) [] h := (nat.not_succ_le_zero _ h).elim
| (n+1) (a'::as) h := congr_arg nat.succ $ length_insert_nth n as (nat.le_of_succ_le_succ h)
lemma remove_nth_insert_nth (n:ℕ) (l : list α) : (l.insert_nth n a).remove_nth n = l :=
by rw [remove_nth_eq_nth_tail, insert_nth, modify_nth_tail_modify_nth_tail_same];
from modify_nth_tail_id _ _
lemma insert_nth_remove_nth_of_ge : ∀n m as, n < length as → n ≤ m →
insert_nth m a (as.remove_nth n) = (as.insert_nth (m + 1) a).remove_nth n
| 0 0 [] has _ := (lt_irrefl _ has).elim
| 0 0 (a::as) has hmn := by simp [remove_nth, insert_nth]
| 0 (m+1) (a::as) has hmn := rfl
| (n+1) (m+1) (a::as) has hmn :=
congr_arg (cons a) $
insert_nth_remove_nth_of_ge n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn)
lemma insert_nth_remove_nth_of_le : ∀n m as, n < length as → m ≤ n →
insert_nth m a (as.remove_nth n) = (as.insert_nth m a).remove_nth (n + 1)
| n 0 (a :: as) has hmn := rfl
| (n + 1) (m + 1) (a :: as) has hmn :=
congr_arg (cons a) $
insert_nth_remove_nth_of_le n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn)
lemma insert_nth_comm (a b : α) :
∀(i j : ℕ) (l : list α) (h : i ≤ j) (hj : j ≤ length l),
(l.insert_nth i a).insert_nth (j + 1) b = (l.insert_nth j b).insert_nth i a
| 0 j l := by simp [insert_nth]
| (i + 1) 0 l := assume h, (nat.not_lt_zero _ h).elim
| (i + 1) (j+1) [] := by simp
| (i + 1) (j+1) (c::l) :=
assume h₀ h₁,
by simp [insert_nth];
exact insert_nth_comm i j l (nat.le_of_succ_le_succ h₀) (nat.le_of_succ_le_succ h₁)
lemma mem_insert_nth {a b : α} : ∀ {n : ℕ} {l : list α} (hi : n ≤ l.length),
a ∈ l.insert_nth n b ↔ a = b ∨ a ∈ l
| 0 as h := iff.rfl
| (n+1) [] h := (nat.not_succ_le_zero _ h).elim
| (n+1) (a'::as) h := begin
dsimp [list.insert_nth],
erw [list.mem_cons_iff, mem_insert_nth (nat.le_of_succ_le_succ h), list.mem_cons_iff,
← or.assoc, or_comm (a = a'), or.assoc]
end
end insert_nth
/-! ### map -/
@[simp] lemma map_nil (f : α → β) : map f [] = [] := rfl
theorem map_eq_foldr (f : α → β) (l : list α) :
map f l = foldr (λ a bs, f a :: bs) [] l :=
by induction l; simp *
lemma map_congr {f g : α → β} : ∀ {l : list α}, (∀ x ∈ l, f x = g x) → map f l = map g l
| [] _ := rfl
| (a::l) h := let ⟨h₁, h₂⟩ := forall_mem_cons.1 h in
by rw [map, map, h₁, map_congr h₂]
lemma map_eq_map_iff {f g : α → β} {l : list α} : map f l = map g l ↔ (∀ x ∈ l, f x = g x) :=
begin
refine ⟨_, map_congr⟩, intros h x hx,
rw [mem_iff_nth_le] at hx, rcases hx with ⟨n, hn, rfl⟩,
rw [nth_le_map_rev f, nth_le_map_rev g], congr, exact h
end
theorem map_concat (f : α → β) (a : α) (l : list α) : map f (concat l a) = concat (map f l) (f a) :=
by induction l; [refl, simp only [*, concat_eq_append, cons_append, map, map_append]]; split; refl
theorem map_id' {f : α → α} (h : ∀ x, f x = x) (l : list α) : map f l = l :=
by induction l; [refl, simp only [*, map]]; split; refl
theorem eq_nil_of_map_eq_nil {f : α → β} {l : list α} (h : map f l = nil) : l = nil :=
eq_nil_of_length_eq_zero $ by rw [← length_map f l, h]; refl
@[simp] theorem map_join (f : α → β) (L : list (list α)) :
map f (join L) = join (map (map f) L) :=
by induction L; [refl, simp only [*, join, map, map_append]]
theorem bind_ret_eq_map (f : α → β) (l : list α) :
l.bind (list.ret ∘ f) = map f l :=
by unfold list.bind; induction l; simp only [map, join, list.ret, cons_append, nil_append, *];
split; refl
@[simp] theorem map_eq_map {α β} (f : α → β) (l : list α) : f <$> l = map f l := rfl
@[simp] theorem map_tail (f : α → β) (l) : map f (tail l) = tail (map f l) :=
by cases l; refl
@[simp] theorem map_injective_iff {f : α → β} : injective (map f) ↔ injective f :=
begin
split; intros h x y hxy,
{ suffices : [x] = [y], { simpa using this }, apply h, simp [hxy] },
{ induction y generalizing x, simpa using hxy,
cases x, simpa using hxy, simp at hxy, simp [y_ih hxy.2, h hxy.1] }
end
/--
A single `list.map` of a composition of functions is equal to
composing a `list.map` with another `list.map`, fully applied.
This is the reverse direction of `list.map_map`.
-/
lemma comp_map (h : β → γ) (g : α → β) (l : list α) :
map (h ∘ g) l = map h (map g l) := (map_map _ _ _).symm
/--
Composing a `list.map` with another `list.map` is equal to
a single `list.map` of composed functions.
-/
@[simp] lemma map_comp_map (g : β → γ) (f : α → β) :
map g ∘ map f = map (g ∘ f) :=
by { ext l, rw comp_map }
theorem map_filter_eq_foldr (f : α → β) (p : α → Prop) [decidable_pred p] (as : list α) :
map f (filter p as) = foldr (λ a bs, if p a then f a :: bs else bs) [] as :=
by { induction as, { refl }, { simp! [*, apply_ite (map f)] } }
lemma last_map (f : α → β) {l : list α} (hl : l ≠ []) :
(l.map f).last (mt eq_nil_of_map_eq_nil hl) = f (l.last hl) :=
begin
induction l with l_ih l_tl l_ih,
{ apply (hl rfl).elim },
{ cases l_tl,
{ simp },
{ simpa using l_ih } }
end
/-! ### map₂ -/
theorem nil_map₂ (f : α → β → γ) (l : list β) : map₂ f [] l = [] :=
by cases l; refl
theorem map₂_nil (f : α → β → γ) (l : list α) : map₂ f l [] = [] :=
by cases l; refl
@[simp] theorem map₂_flip (f : α → β → γ) :
∀ as bs, map₂ (flip f) bs as = map₂ f as bs
| [] [] := rfl
| [] (b :: bs) := rfl
| (a :: as) [] := rfl
| (a :: as) (b :: bs) := by { simp! [map₂_flip], refl }
/-! ### take, drop -/
@[simp] theorem take_zero (l : list α) : take 0 l = [] := rfl
@[simp] theorem take_nil : ∀ n, take n [] = ([] : list α)
| 0 := rfl
| (n+1) := rfl
theorem take_cons (n) (a : α) (l : list α) : take (succ n) (a::l) = a :: take n l := rfl
@[simp] theorem take_length : ∀ (l : list α), take (length l) l = l
| [] := rfl
| (a::l) := begin change a :: (take (length l) l) = a :: l, rw take_length end
theorem take_all_of_le : ∀ {n} {l : list α}, length l ≤ n → take n l = l
| 0 [] h := rfl
| 0 (a::l) h := absurd h (not_le_of_gt (zero_lt_succ _))
| (n+1) [] h := rfl
| (n+1) (a::l) h :=
begin
change a :: take n l = a :: l,
rw [take_all_of_le (le_of_succ_le_succ h)]
end
@[simp] theorem take_left : ∀ l₁ l₂ : list α, take (length l₁) (l₁ ++ l₂) = l₁
| [] l₂ := rfl
| (a::l₁) l₂ := congr_arg (cons a) (take_left l₁ l₂)
theorem take_left' {l₁ l₂ : list α} {n} (h : length l₁ = n) :
take n (l₁ ++ l₂) = l₁ :=
by rw ← h; apply take_left
theorem take_take : ∀ (n m) (l : list α), take n (take m l) = take (min n m) l
| n 0 l := by rw [min_zero, take_zero, take_nil]
| 0 m l := by rw [zero_min, take_zero, take_zero]
| (succ n) (succ m) nil := by simp only [take_nil]
| (succ n) (succ m) (a::l) := by simp only [take, min_succ_succ, take_take n m l]; split; refl
theorem take_repeat (a : α) : ∀ (n m : ℕ), take n (repeat a m) = repeat a (min n m)
| n 0 := by simp
| 0 m := by simp
| (succ n) (succ m) := by simp [min_succ_succ, take_repeat]
lemma map_take {α β : Type*} (f : α → β) :
∀ (L : list α) (i : ℕ), (L.take i).map f = (L.map f).take i
| [] i := by simp
| L 0 := by simp
| (h :: t) (n+1) := by { dsimp, rw [map_take], }
lemma take_append_of_le_length : ∀ {l₁ l₂ : list α} {n : ℕ},
n ≤ l₁.length → (l₁ ++ l₂).take n = l₁.take n
| l₁ l₂ 0 hn := by simp
| [] l₂ (n+1) hn := absurd hn dec_trivial
| (a::l₁) l₂ (n+1) hn :=
by rw [list.take, list.cons_append, list.take, take_append_of_le_length (le_of_succ_le_succ hn)]
/-- Taking the first `l₁.length + i` elements in `l₁ ++ l₂` is the same as appending the first
`i` elements of `l₂` to `l₁`. -/
lemma take_append {l₁ l₂ : list α} (i : ℕ) :
take (l₁.length + i) (l₁ ++ l₂) = l₁ ++ (take i l₂) :=
begin
induction l₁, { simp },
have : length l₁_tl + 1 + i = (length l₁_tl + i).succ,
by { rw nat.succ_eq_add_one, exact succ_add _ _ },
simp only [cons_append, length, this, take_cons, l₁_ih, eq_self_iff_true, and_self]
end
/-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of
length `> i`. Version designed to rewrite from the big list to the small list. -/
lemma nth_le_take (L : list α) {i j : ℕ} (hi : i < L.length) (hj : i < j) :
nth_le L i hi = nth_le (L.take j) i (by { rw length_take, exact lt_min hj hi }) :=
by { rw nth_le_of_eq (take_append_drop j L).symm hi, exact nth_le_append _ _ }
/-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of
length `> i`. Version designed to rewrite from the small list to the big list. -/
lemma nth_le_take' (L : list α) {i j : ℕ} (hi : i < (L.take j).length) :
nth_le (L.take j) i hi = nth_le L i (lt_of_lt_of_le hi (by simp [le_refl])) :=
by { simp at hi, rw nth_le_take L _ hi.1 }
lemma nth_take {l : list α} {n m : ℕ} (h : m < n) :
(l.take n).nth m = l.nth m :=
begin
induction n with n hn generalizing l m,
{ simp only [nat.nat_zero_eq_zero] at h,
exact absurd h (not_lt_of_le m.zero_le) },
{ cases l with hd tl,
{ simp only [take_nil] },
{ cases m,
{ simp only [nth, take] },
{ simpa only using hn (nat.lt_of_succ_lt_succ h) } } },
end
@[simp] lemma nth_take_of_succ {l : list α} {n : ℕ} :
(l.take (n + 1)).nth n = l.nth n :=
nth_take (nat.lt_succ_self n)
lemma take_succ {l : list α} {n : ℕ} :
l.take (n + 1) = l.take n ++ (l.nth n).to_list :=
begin
induction l with hd tl hl generalizing n,
{ simp only [option.to_list, nth, take_nil, append_nil]},
{ cases n,
{ simp only [option.to_list, nth, eq_self_iff_true, and_self, take, nil_append] },
{ simp only [hl, cons_append, nth, eq_self_iff_true, and_self, take] } }
end
@[simp] lemma take_eq_nil_iff {l : list α} {k : ℕ} :
l.take k = [] ↔ l = [] ∨ k = 0 :=
by { cases l; cases k; simp [nat.succ_ne_zero] }
lemma init_eq_take (l : list α) : l.init = l.take l.length.pred :=
begin
cases l with x l,
{ simp [init] },
{ induction l with hd tl hl generalizing x,
{ simp [init], },
{ simp [init, hl] } }
end
lemma init_take {n : ℕ} {l : list α} (h : n < l.length) :
(l.take n).init = l.take n.pred :=
by simp [init_eq_take, min_eq_left_of_lt h, take_take, pred_le]
@[simp] lemma drop_eq_nil_of_le {l : list α} {k : ℕ} (h : l.length ≤ k) :
l.drop k = [] :=
by simpa [←length_eq_zero] using nat.sub_eq_zero_of_le h
lemma drop_eq_nil_iff_le {l : list α} {k : ℕ} :
l.drop k = [] ↔ l.length ≤ k :=
begin
refine ⟨λ h, _, drop_eq_nil_of_le⟩,
induction k with k hk generalizing l,
{ simp only [drop] at h,
simp [h] },
{ cases l,
{ simp },
{ simp only [drop] at h,
simpa [nat.succ_le_succ_iff] using hk h } }
end
lemma tail_drop (l : list α) (n : ℕ) : (l.drop n).tail = l.drop (n + 1) :=
begin
induction l with hd tl hl generalizing n,
{ simp },
{ cases n,
{ simp },
{ simp [hl] } }
end
lemma cons_nth_le_drop_succ {l : list α} {n : ℕ} (hn : n < l.length) :
l.nth_le n hn :: l.drop (n + 1) = l.drop n :=
begin
induction l with hd tl hl generalizing n,
{ exact absurd n.zero_le (not_le_of_lt (by simpa using hn)) },
{ cases n,
{ simp },
{ simp only [nat.succ_lt_succ_iff, list.length] at hn,
simpa [list.nth_le, list.drop] using hl hn } }
end
theorem drop_nil : ∀ n, drop n [] = ([] : list α) :=
λ _, drop_eq_nil_of_le (nat.zero_le _)
lemma mem_of_mem_drop {α} {n : ℕ} {l : list α} {x : α}
(h : x ∈ l.drop n) :
x ∈ l :=
begin
induction l generalizing n,
case list.nil : n h
{ simpa using h },
case list.cons : l_hd l_tl l_ih n h
{ cases n; simp only [mem_cons_iff, drop] at h ⊢,
{ exact h },
right, apply l_ih h },
end
@[simp] theorem drop_one : ∀ l : list α, drop 1 l = tail l
| [] := rfl
| (a :: l) := rfl
theorem drop_add : ∀ m n (l : list α), drop (m + n) l = drop m (drop n l)
| m 0 l := rfl
| m (n+1) [] := (drop_nil _).symm
| m (n+1) (a::l) := drop_add m n _
@[simp] theorem drop_left : ∀ l₁ l₂ : list α, drop (length l₁) (l₁ ++ l₂) = l₂
| [] l₂ := rfl
| (a::l₁) l₂ := drop_left l₁ l₂
theorem drop_left' {l₁ l₂ : list α} {n} (h : length l₁ = n) :
drop n (l₁ ++ l₂) = l₂ :=
by rw ← h; apply drop_left
theorem drop_eq_nth_le_cons : ∀ {n} {l : list α} h,
drop n l = nth_le l n h :: drop (n+1) l
| 0 (a::l) h := rfl
| (n+1) (a::l) h := @drop_eq_nth_le_cons n _ _
@[simp] lemma drop_length (l : list α) : l.drop l.length = [] :=
calc l.drop l.length = (l ++ []).drop l.length : by simp
... = [] : drop_left _ _
lemma drop_append_of_le_length : ∀ {l₁ l₂ : list α} {n : ℕ}, n ≤ l₁.length →
(l₁ ++ l₂).drop n = l₁.drop n ++ l₂
| l₁ l₂ 0 hn := by simp
| [] l₂ (n+1) hn := absurd hn dec_trivial
| (a::l₁) l₂ (n+1) hn :=
by rw [drop, cons_append, drop, drop_append_of_le_length (le_of_succ_le_succ hn)]
/-- Dropping the elements up to `l₁.length + i` in `l₁ + l₂` is the same as dropping the elements
up to `i` in `l₂`. -/
lemma drop_append {l₁ l₂ : list α} (i : ℕ) :
drop (l₁.length + i) (l₁ ++ l₂) = drop i l₂ :=
begin
induction l₁, { simp },
have : length l₁_tl + 1 + i = (length l₁_tl + i).succ,
by { rw nat.succ_eq_add_one, exact succ_add _ _ },
simp only [cons_append, length, this, drop, l₁_ih]
end
/-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by
dropping the first `i` elements. Version designed to rewrite from the big list to the small list. -/
lemma nth_le_drop (L : list α) {i j : ℕ} (h : i + j < L.length) :
nth_le L (i + j) h = nth_le (L.drop i) j
begin
have A : i < L.length := lt_of_le_of_lt (nat.le.intro rfl) h,
rw (take_append_drop i L).symm at h,
simpa only [le_of_lt A, min_eq_left, add_lt_add_iff_left, length_take, length_append] using h
end :=
begin
have A : length (take i L) = i, by simp [le_of_lt (lt_of_le_of_lt (nat.le.intro rfl) h)],
rw [nth_le_of_eq (take_append_drop i L).symm h, nth_le_append_right];
simp [A]
end
/-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by
dropping the first `i` elements. Version designed to rewrite from the small list to the big list. -/
lemma nth_le_drop' (L : list α) {i j : ℕ} (h : j < (L.drop i).length) :
nth_le (L.drop i) j h = nth_le L (i + j) (nat.add_lt_of_lt_sub_left ((length_drop i L) ▸ h)) :=
by rw nth_le_drop
lemma nth_drop (L : list α) (i j : ℕ) :
nth (L.drop i) j = nth L (i + j) :=
begin
ext,
simp only [nth_eq_some, nth_le_drop', option.mem_def],
split;
exact λ ⟨h, ha⟩, ⟨by simpa [nat.lt_sub_left_iff_add_lt] using h, ha⟩
end
@[simp] theorem drop_drop (n : ℕ) : ∀ (m) (l : list α), drop n (drop m l) = drop (n + m) l
| m [] := by simp
| 0 l := by simp
| (m+1) (a::l) :=
calc drop n (drop (m + 1) (a :: l)) = drop n (drop m l) : rfl
... = drop (n + m) l : drop_drop m l
... = drop (n + (m + 1)) (a :: l) : rfl
theorem drop_take : ∀ (m : ℕ) (n : ℕ) (l : list α),
drop m (take (m + n) l) = take n (drop m l)
| 0 n _ := by simp
| (m+1) n nil := by simp
| (m+1) n (_::l) :=
have h: m + 1 + n = (m+n) + 1, by ac_refl,
by simpa [take_cons, h] using drop_take m n l
lemma map_drop {α β : Type*} (f : α → β) :
∀ (L : list α) (i : ℕ), (L.drop i).map f = (L.map f).drop i
| [] i := by simp
| L 0 := by simp
| (h :: t) (n+1) := by { dsimp, rw [map_drop], }
theorem modify_nth_tail_eq_take_drop (f : list α → list α) (H : f [] = []) :
∀ n l, modify_nth_tail f n l = take n l ++ f (drop n l)
| 0 l := rfl
| (n+1) [] := H.symm
| (n+1) (b::l) := congr_arg (cons b) (modify_nth_tail_eq_take_drop n l)
theorem modify_nth_eq_take_drop (f : α → α) :
∀ n l, modify_nth f n l = take n l ++ modify_head f (drop n l) :=
modify_nth_tail_eq_take_drop _ rfl
theorem modify_nth_eq_take_cons_drop (f : α → α) {n l} (h) :
modify_nth f n l = take n l ++ f (nth_le l n h) :: drop (n+1) l :=
by rw [modify_nth_eq_take_drop, drop_eq_nth_le_cons h]; refl
theorem update_nth_eq_take_cons_drop (a : α) {n l} (h : n < length l) :
update_nth l n a = take n l ++ a :: drop (n+1) l :=
by rw [update_nth_eq_modify_nth, modify_nth_eq_take_cons_drop _ h]
lemma reverse_take {α} {xs : list α} (n : ℕ)
(h : n ≤ xs.length) :
xs.reverse.take n = (xs.drop (xs.length - n)).reverse :=
begin
induction xs generalizing n;
simp only [reverse_cons, drop, reverse_nil, nat.zero_sub, length, take_nil],
cases h.lt_or_eq_dec with h' h',
{ replace h' := le_of_succ_le_succ h',
rwa [take_append_of_le_length, xs_ih _ h'],
rw [show xs_tl.length + 1 - n = succ (xs_tl.length - n), from _, drop],
{ rwa [succ_eq_add_one, nat.sub_add_comm] },
{ rwa length_reverse } },
{ subst h', rw [length, nat.sub_self, drop],
suffices : xs_tl.length + 1 = (xs_tl.reverse ++ [xs_hd]).length,
by rw [this, take_length, reverse_cons],
rw [length_append, length_reverse], refl }
end
@[simp] lemma update_nth_eq_nil (l : list α) (n : ℕ) (a : α) : l.update_nth n a = [] ↔ l = [] :=
by cases l; cases n; simp only [update_nth]
section take'
variable [inhabited α]
@[simp] theorem take'_length : ∀ n l, length (@take' α _ n l) = n
| 0 l := rfl
| (n+1) l := congr_arg succ (take'_length _ _)
@[simp] theorem take'_nil : ∀ n, take' n (@nil α) = repeat (default _) n
| 0 := rfl
| (n+1) := congr_arg (cons _) (take'_nil _)
theorem take'_eq_take : ∀ {n} {l : list α},
n ≤ length l → take' n l = take n l
| 0 l h := rfl
| (n+1) (a::l) h := congr_arg (cons _) $
take'_eq_take $ le_of_succ_le_succ h
@[simp] theorem take'_left (l₁ l₂ : list α) : take' (length l₁) (l₁ ++ l₂) = l₁ :=
(take'_eq_take (by simp only [length_append, nat.le_add_right])).trans (take_left _ _)
theorem take'_left' {l₁ l₂ : list α} {n} (h : length l₁ = n) :
take' n (l₁ ++ l₂) = l₁ :=
by rw ← h; apply take'_left
end take'
/-! ### foldl, foldr -/
lemma foldl_ext (f g : α → β → α) (a : α)
{l : list β} (H : ∀ a : α, ∀ b ∈ l, f a b = g a b) :
foldl f a l = foldl g a l :=
begin
induction l with hd tl ih generalizing a, {refl},
unfold foldl,
rw [ih (λ a b bin, H a b $ mem_cons_of_mem _ bin), H a hd (mem_cons_self _ _)]
end
lemma foldr_ext (f g : α → β → β) (b : β)
{l : list α} (H : ∀ a ∈ l, ∀ b : β, f a b = g a b) :
foldr f b l = foldr g b l :=
begin
induction l with hd tl ih, {refl},
simp only [mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq] at H,
simp only [foldr, ih H.2, H.1]
end
@[simp] theorem foldl_nil (f : α → β → α) (a : α) : foldl f a [] = a := rfl
@[simp] theorem foldl_cons (f : α → β → α) (a : α) (b : β) (l : list β) :
foldl f a (b::l) = foldl f (f a b) l := rfl
@[simp] theorem foldr_nil (f : α → β → β) (b : β) : foldr f b [] = b := rfl
@[simp] theorem foldr_cons (f : α → β → β) (b : β) (a : α) (l : list α) :
foldr f b (a::l) = f a (foldr f b l) := rfl
@[simp] theorem foldl_append (f : α → β → α) :
∀ (a : α) (l₁ l₂ : list β), foldl f a (l₁++l₂) = foldl f (foldl f a l₁) l₂
| a [] l₂ := rfl
| a (b::l₁) l₂ := by simp only [cons_append, foldl_cons, foldl_append (f a b) l₁ l₂]
@[simp] theorem foldr_append (f : α → β → β) :
∀ (b : β) (l₁ l₂ : list α), foldr f b (l₁++l₂) = foldr f (foldr f b l₂) l₁
| b [] l₂ := rfl
| b (a::l₁) l₂ := by simp only [cons_append, foldr_cons, foldr_append b l₁ l₂]
@[simp] theorem foldl_join (f : α → β → α) :
∀ (a : α) (L : list (list β)), foldl f a (join L) = foldl (foldl f) a L
| a [] := rfl
| a (l::L) := by simp only [join, foldl_append, foldl_cons, foldl_join (foldl f a l) L]
@[simp] theorem foldr_join (f : α → β → β) :
∀ (b : β) (L : list (list α)), foldr f b (join L) = foldr (λ l b, foldr f b l) b L
| a [] := rfl
| a (l::L) := by simp only [join, foldr_append, foldr_join a L, foldr_cons]
theorem foldl_reverse (f : α → β → α) (a : α) (l : list β) :
foldl f a (reverse l) = foldr (λx y, f y x) a l :=
by induction l; [refl, simp only [*, reverse_cons, foldl_append, foldl_cons, foldl_nil, foldr]]
theorem foldr_reverse (f : α → β → β) (a : β) (l : list α) :
foldr f a (reverse l) = foldl (λx y, f y x) a l :=
let t := foldl_reverse (λx y, f y x) a (reverse l) in
by rw reverse_reverse l at t; rwa t
@[simp] theorem foldr_eta : ∀ (l : list α), foldr cons [] l = l
| [] := rfl
| (x::l) := by simp only [foldr_cons, foldr_eta l]; split; refl
@[simp] theorem reverse_foldl {l : list α} : reverse (foldl (λ t h, h :: t) [] l) = l :=
by rw ←foldr_reverse; simp
@[simp] theorem foldl_map (g : β → γ) (f : α → γ → α) (a : α) (l : list β) :
foldl f a (map g l) = foldl (λx y, f x (g y)) a l :=
by revert a; induction l; intros; [refl, simp only [*, map, foldl]]
@[simp] theorem foldr_map (g : β → γ) (f : γ → α → α) (a : α) (l : list β) :
foldr f a (map g l) = foldr (f ∘ g) a l :=
by revert a; induction l; intros; [refl, simp only [*, map, foldr]]
theorem foldl_map' {α β: Type u} (g : α → β) (f : α → α → α) (f' : β → β → β)
(a : α) (l : list α) (h : ∀ x y, f' (g x) (g y) = g (f x y)) :
list.foldl f' (g a) (l.map g) = g (list.foldl f a l) :=
begin
induction l generalizing a,
{ simp }, { simp [l_ih, h] }
end
theorem foldr_map' {α β: Type u} (g : α → β) (f : α → α → α) (f' : β → β → β)
(a : α) (l : list α) (h : ∀ x y, f' (g x) (g y) = g (f x y)) :
list.foldr f' (g a) (l.map g) = g (list.foldr f a l) :=
begin
induction l generalizing a,
{ simp }, { simp [l_ih, h] }
end
theorem foldl_hom (l : list γ) (f : α → β) (op : α → γ → α) (op' : β → γ → β) (a : α)
(h : ∀a x, f (op a x) = op' (f a) x) : foldl op' (f a) l = f (foldl op a l) :=
eq.symm $ by { revert a, induction l; intros; [refl, simp only [*, foldl]] }
theorem foldr_hom (l : list γ) (f : α → β) (op : γ → α → α) (op' : γ → β → β) (a : α)
(h : ∀x a, f (op x a) = op' x (f a)) : foldr op' (f a) l = f (foldr op a l) :=
by { revert a, induction l; intros; [refl, simp only [*, foldr]] }
lemma injective_foldl_comp {α : Type*} {l : list (α → α)} {f : α → α}
(hl : ∀ f ∈ l, function.injective f) (hf : function.injective f):
function.injective (@list.foldl (α → α) (α → α) function.comp f l) :=
begin
induction l generalizing f,
{ exact hf },
{ apply l_ih (λ _ h, hl _ (list.mem_cons_of_mem _ h)),
apply function.injective.comp hf,
apply hl _ (list.mem_cons_self _ _) }
end
/-- Induction principle for values produced by a `foldr`: if a property holds
for the seed element `b : β` and for all incremental `op : α → β → β`
performed on the elements `(a : α) ∈ l`. The principle is given for
a `Sort`-valued predicate, i.e., it can also be used to construct data. -/
def foldr_rec_on {C : β → Sort*} (l : list α) (op : α → β → β) (b : β) (hb : C b)
(hl : ∀ (b : β) (hb : C b) (a : α) (ha : a ∈ l), C (op a b)) :
C (foldr op b l) :=
begin
induction l with hd tl IH,
{ exact hb },
{ refine hl _ _ hd (mem_cons_self hd tl),
refine IH _,
intros y hy x hx,
exact hl y hy x (mem_cons_of_mem hd hx) }
end
/-- Induction principle for values produced by a `foldl`: if a property holds
for the seed element `b : β` and for all incremental `op : β → α → β`
performed on the elements `(a : α) ∈ l`. The principle is given for
a `Sort`-valued predicate, i.e., it can also be used to construct data. -/
def foldl_rec_on {C : β → Sort*} (l : list α) (op : β → α → β) (b : β) (hb : C b)
(hl : ∀ (b : β) (hb : C b) (a : α) (ha : a ∈ l), C (op b a)) :
C (foldl op b l) :=
begin
induction l with hd tl IH generalizing b,
{ exact hb },
{ refine IH _ _ _,
{ intros y hy x hx,
exact hl y hy x (mem_cons_of_mem hd hx) },
{ exact hl b hb hd (mem_cons_self hd tl) } }
end
@[simp] lemma foldr_rec_on_nil {C : β → Sort*} (op : α → β → β) (b) (hb : C b) (hl) :
foldr_rec_on [] op b hb hl = hb := rfl
@[simp] lemma foldr_rec_on_cons {C : β → Sort*} (x : α) (l : list α)
(op : α → β → β) (b) (hb : C b)
(hl : ∀ (b : β) (hb : C b) (a : α) (ha : a ∈ (x :: l)), C (op a b)) :
foldr_rec_on (x :: l) op b hb hl = hl _ (foldr_rec_on l op b hb
(λ b hb a ha, hl b hb a (mem_cons_of_mem _ ha))) x (mem_cons_self _ _) := rfl
@[simp] lemma foldl_rec_on_nil {C : β → Sort*} (op : β → α → β) (b) (hb : C b) (hl) :
foldl_rec_on [] op b hb hl = hb := rfl
/- scanl -/
section scanl
variables {f : β → α → β} {b : β} {a : α} {l : list α}
lemma length_scanl :
∀ a l, length (scanl f a l) = l.length + 1
| a [] := rfl
| a (x :: l) := by erw [length_cons, length_cons, length_scanl]
@[simp] lemma scanl_nil (b : β) : scanl f b nil = [b] := rfl
@[simp] lemma scanl_cons :
scanl f b (a :: l) = [b] ++ scanl f (f b a) l :=
by simp only [scanl, eq_self_iff_true, singleton_append, and_self]
@[simp] lemma nth_zero_scanl : (scanl f b l).nth 0 = some b :=
begin
cases l,
{ simp only [nth, scanl_nil] },
{ simp only [nth, scanl_cons, singleton_append] }
end
@[simp] lemma nth_le_zero_scanl {h : 0 < (scanl f b l).length} :
(scanl f b l).nth_le 0 h = b :=
begin
cases l,
{ simp only [nth_le, scanl_nil] },
{ simp only [nth_le, scanl_cons, singleton_append] }
end
lemma nth_succ_scanl {i : ℕ} :
(scanl f b l).nth (i + 1) = ((scanl f b l).nth i).bind (λ x, (l.nth i).map (λ y, f x y)) :=
begin
induction l with hd tl hl generalizing b i,
{ symmetry,
simp only [option.bind_eq_none', nth, forall_2_true_iff, not_false_iff, option.map_none',
scanl_nil, option.not_mem_none, forall_true_iff] },
{ simp only [nth, scanl_cons, singleton_append],
cases i,
{ simp only [option.map_some', nth_zero_scanl, nth, option.some_bind'] },
{ simp only [hl, nth] } }
end
lemma nth_le_succ_scanl {i : ℕ} {h : i + 1 < (scanl f b l).length} :
(scanl f b l).nth_le (i + 1) h =
f ((scanl f b l).nth_le i (nat.lt_of_succ_lt h))
(l.nth_le i (nat.lt_of_succ_lt_succ (lt_of_lt_of_le h (le_of_eq (length_scanl b l))))) :=
begin
induction i with i hi generalizing b l,
{ cases l,
{ simp only [length, zero_add, scanl_nil] at h,
exact absurd h (lt_irrefl 1) },
{ simp only [scanl_cons, singleton_append, nth_le_zero_scanl, nth_le] } },
{ cases l,
{ simp only [length, add_lt_iff_neg_right, scanl_nil] at h,
exact absurd h (not_lt_of_lt nat.succ_pos') },
{ simp_rw scanl_cons,
rw nth_le_append_right _,
{ simpa only [hi, length, succ_add_sub_one] },
{ simp only [length, nat.zero_le, le_add_iff_nonneg_left] } } }
end
end scanl
/- scanr -/
@[simp] theorem scanr_nil (f : α → β → β) (b : β) : scanr f b [] = [b] := rfl
@[simp] theorem scanr_aux_cons (f : α → β → β) (b : β) : ∀ (a : α) (l : list α),
scanr_aux f b (a::l) = (foldr f b (a::l), scanr f b l)
| a [] := rfl
| a (x::l) := let t := scanr_aux_cons x l in
by simp only [scanr, scanr_aux, t, foldr_cons]
@[simp] theorem scanr_cons (f : α → β → β) (b : β) (a : α) (l : list α) :
scanr f b (a::l) = foldr f b (a::l) :: scanr f b l :=
by simp only [scanr, scanr_aux_cons, foldr_cons]; split; refl
section foldl_eq_foldr
-- foldl and foldr coincide when f is commutative and associative
variables {f : α → α → α} (hcomm : commutative f) (hassoc : associative f)
include hassoc
theorem foldl1_eq_foldr1 : ∀ a b l, foldl f a (l++[b]) = foldr f b (a::l)
| a b nil := rfl
| a b (c :: l) :=
by simp only [cons_append, foldl_cons, foldr_cons, foldl1_eq_foldr1 _ _ l]; rw hassoc
include hcomm
theorem foldl_eq_of_comm_of_assoc : ∀ a b l, foldl f a (b::l) = f b (foldl f a l)
| a b nil := hcomm a b
| a b (c::l) := by simp only [foldl_cons];
rw [← foldl_eq_of_comm_of_assoc, right_comm _ hcomm hassoc]; refl
theorem foldl_eq_foldr : ∀ a l, foldl f a l = foldr f a l
| a nil := rfl
| a (b :: l) :=
by simp only [foldr_cons, foldl_eq_of_comm_of_assoc hcomm hassoc]; rw (foldl_eq_foldr a l)
end foldl_eq_foldr
section foldl_eq_foldlr'
variables {f : α → β → α}
variables hf : ∀ a b c, f (f a b) c = f (f a c) b
include hf
theorem foldl_eq_of_comm' : ∀ a b l, foldl f a (b::l) = f (foldl f a l) b
| a b [] := rfl
| a b (c :: l) := by rw [foldl,foldl,foldl,← foldl_eq_of_comm',foldl,hf]
theorem foldl_eq_foldr' : ∀ a l, foldl f a l = foldr (flip f) a l
| a [] := rfl
| a (b :: l) := by rw [foldl_eq_of_comm' hf,foldr,foldl_eq_foldr']; refl
end foldl_eq_foldlr'
section foldl_eq_foldlr'
variables {f : α → β → β}
variables hf : ∀ a b c, f a (f b c) = f b (f a c)
include hf
theorem foldr_eq_of_comm' : ∀ a b l, foldr f a (b::l) = foldr f (f b a) l
| a b [] := rfl
| a b (c :: l) := by rw [foldr,foldr,foldr,hf,← foldr_eq_of_comm']; refl
end foldl_eq_foldlr'
section
variables {op : α → α → α} [ha : is_associative α op] [hc : is_commutative α op]
local notation a * b := op a b
local notation l <*> a := foldl op a l
include ha
lemma foldl_assoc : ∀ {l : list α} {a₁ a₂}, l <*> (a₁ * a₂) = a₁ * (l <*> a₂)
| [] a₁ a₂ := rfl
| (a :: l) a₁ a₂ :=
calc a::l <*> (a₁ * a₂) = l <*> (a₁ * (a₂ * a)) : by simp only [foldl_cons, ha.assoc]
... = a₁ * (a::l <*> a₂) : by rw [foldl_assoc, foldl_cons]
lemma foldl_op_eq_op_foldr_assoc : ∀{l : list α} {a₁ a₂}, (l <*> a₁) * a₂ = a₁ * l.foldr (*) a₂
| [] a₁ a₂ := rfl
| (a :: l) a₁ a₂ := by simp only [foldl_cons, foldr_cons, foldl_assoc, ha.assoc];
rw [foldl_op_eq_op_foldr_assoc]
include hc
lemma foldl_assoc_comm_cons {l : list α} {a₁ a₂} : (a₁ :: l) <*> a₂ = a₁ * (l <*> a₂) :=
by rw [foldl_cons, hc.comm, foldl_assoc]
end
/-! ### mfoldl, mfoldr, mmap -/
section mfoldl_mfoldr
variables {m : Type v → Type w} [monad m]
@[simp] theorem mfoldl_nil (f : β → α → m β) {b} : mfoldl f b [] = pure b := rfl
@[simp] theorem mfoldr_nil (f : α → β → m β) {b} : mfoldr f b [] = pure b := rfl
@[simp] theorem mfoldl_cons {f : β → α → m β} {b a l} :
mfoldl f b (a :: l) = f b a >>= λ b', mfoldl f b' l := rfl
@[simp] theorem mfoldr_cons {f : α → β → m β} {b a l} :
mfoldr f b (a :: l) = mfoldr f b l >>= f a := rfl
theorem mfoldr_eq_foldr (f : α → β → m β) (b l) :
mfoldr f b l = foldr (λ a mb, mb >>= f a) (pure b) l :=
by induction l; simp *
attribute [simp] mmap mmap'
variables [is_lawful_monad m]
theorem mfoldl_eq_foldl (f : β → α → m β) (b l) :
mfoldl f b l = foldl (λ mb a, mb >>= λ b, f b a) (pure b) l :=
begin
suffices h : ∀ (mb : m β),
(mb >>= λ b, mfoldl f b l) = foldl (λ mb a, mb >>= λ b, f b a) mb l,
by simp [←h (pure b)],
induction l; intro,
{ simp },
{ simp only [mfoldl, foldl, ←l_ih] with monad_norm }
end
@[simp] theorem mfoldl_append {f : β → α → m β} : ∀ {b l₁ l₂},
mfoldl f b (l₁ ++ l₂) = mfoldl f b l₁ >>= λ x, mfoldl f x l₂
| _ [] _ := by simp only [nil_append, mfoldl_nil, pure_bind]
| _ (_::_) _ := by simp only [cons_append, mfoldl_cons, mfoldl_append, bind_assoc]
@[simp] theorem mfoldr_append {f : α → β → m β} : ∀ {b l₁ l₂},
mfoldr f b (l₁ ++ l₂) = mfoldr f b l₂ >>= λ x, mfoldr f x l₁
| _ [] _ := by simp only [nil_append, mfoldr_nil, bind_pure]
| _ (_::_) _ := by simp only [mfoldr_cons, cons_append, mfoldr_append, bind_assoc]
end mfoldl_mfoldr
/-! ### prod and sum -/
-- list.sum was already defined in defs.lean, but we couldn't tag it with `to_additive` yet.
attribute [to_additive] list.prod
section monoid
variables [monoid α] {l l₁ l₂ : list α} {a : α}
@[simp, to_additive]
theorem prod_nil : ([] : list α).prod = 1 := rfl
@[to_additive]
theorem prod_singleton : [a].prod = a := one_mul a
@[simp, to_additive]
theorem prod_cons : (a::l).prod = a * l.prod :=
calc (a::l).prod = foldl (*) (a * 1) l : by simp only [list.prod, foldl_cons, one_mul, mul_one]
... = _ : foldl_assoc
@[simp, priority 500, to_additive]
theorem prod_repeat (a : α) (n : ℕ) : (list.repeat a n).prod = a ^ n :=
begin
induction n with n ih,
{ rw pow_zero, refl },
{ rw [list.repeat_succ, list.prod_cons, ih, pow_succ] }
end
@[simp, to_additive]
theorem prod_append : (l₁ ++ l₂).prod = l₁.prod * l₂.prod :=
calc (l₁ ++ l₂).prod = foldl (*) (foldl (*) 1 l₁ * 1) l₂ : by simp [list.prod]
... = l₁.prod * l₂.prod : foldl_assoc
@[simp, to_additive]
theorem prod_join {l : list (list α)} : l.join.prod = (l.map list.prod).prod :=
by induction l; [refl, simp only [*, list.join, map, prod_append, prod_cons]]
/-- If zero is an element of a list `L`, then `list.prod L = 0`. If the domain is a nontrivial
monoid with zero with no divisors, then this implication becomes an `iff`, see
`list.prod_eq_zero_iff`. -/
theorem prod_eq_zero {M₀ : Type*} [monoid_with_zero M₀] {L : list M₀} (h : (0 : M₀) ∈ L) :
L.prod = 0 :=
begin
induction L with a L ihL,
{ exact absurd h (not_mem_nil _) },
{ rw prod_cons,
cases (mem_cons_iff _ _ _).1 h with ha hL,
exacts [mul_eq_zero_of_left ha.symm _, mul_eq_zero_of_right _ (ihL hL)] }
end
/-- Product of elements of a list `L` equals zero if and only if `0 ∈ L`. See also
`list.prod_eq_zero` for an implication that needs weaker typeclass assumptions. -/
@[simp] theorem prod_eq_zero_iff {M₀ : Type*} [monoid_with_zero M₀] [nontrivial M₀]
[no_zero_divisors M₀] {L : list M₀} :
L.prod = 0 ↔ (0 : M₀) ∈ L :=
begin
induction L with a L ihL,
{ simp },
{ rw [prod_cons, mul_eq_zero, ihL, mem_cons_iff, eq_comm] }
end
theorem prod_ne_zero {M₀ : Type*} [monoid_with_zero M₀] [nontrivial M₀] [no_zero_divisors M₀]
{L : list M₀} (hL : (0 : M₀) ∉ L) : L.prod ≠ 0 :=
mt prod_eq_zero_iff.1 hL
@[to_additive]
theorem prod_eq_foldr : l.prod = foldr (*) 1 l :=
list.rec_on l rfl $ λ a l ihl, by rw [prod_cons, foldr_cons, ihl]
@[to_additive]
theorem prod_hom_rel {α β γ : Type*} [monoid β] [monoid γ] (l : list α) {r : β → γ → Prop}
{f : α → β} {g : α → γ} (h₁ : r 1 1) (h₂ : ∀⦃a b c⦄, r b c → r (f a * b) (g a * c)) :
r (l.map f).prod (l.map g).prod :=
list.rec_on l h₁ (λ a l hl, by simp only [map_cons, prod_cons, h₂ hl])
@[to_additive]
theorem prod_hom [monoid β] (l : list α) (f : α →* β) :
(l.map f).prod = f l.prod :=
by { simp only [prod, foldl_map, f.map_one.symm],
exact l.foldl_hom _ _ _ 1 f.map_mul }
@[to_additive]
lemma prod_is_unit [monoid β] : Π {L : list β} (u : ∀ m ∈ L, is_unit m), is_unit L.prod
| [] _ := by simp
| (h :: t) u :=
begin
simp only [list.prod_cons],
exact is_unit.mul (u h (mem_cons_self h t)) (prod_is_unit (λ m mt, u m (mem_cons_of_mem h mt)))
end
-- `to_additive` chokes on the next few lemmas, so we do them by hand below
@[simp]
lemma prod_take_mul_prod_drop :
∀ (L : list α) (i : ℕ), (L.take i).prod * (L.drop i).prod = L.prod
| [] i := by simp
| L 0 := by simp
| (h :: t) (n+1) := by { dsimp, rw [prod_cons, prod_cons, mul_assoc, prod_take_mul_prod_drop], }
@[simp]
lemma prod_take_succ :
∀ (L : list α) (i : ℕ) (p), (L.take (i + 1)).prod = (L.take i).prod * L.nth_le i p
| [] i p := by cases p
| (h :: t) 0 _ := by simp
| (h :: t) (n+1) _ := by { dsimp, rw [prod_cons, prod_cons, prod_take_succ, mul_assoc], }
/-- A list with product not one must have positive length. -/
lemma length_pos_of_prod_ne_one (L : list α) (h : L.prod ≠ 1) : 0 < L.length :=
by { cases L, { simp at h, cases h, }, { simp, }, }
lemma prod_update_nth : ∀ (L : list α) (n : ℕ) (a : α),
(L.update_nth n a).prod =
(L.take n).prod * (if n < L.length then a else 1) * (L.drop (n + 1)).prod
| (x::xs) 0 a := by simp [update_nth]
| (x::xs) (i+1) a := by simp [update_nth, prod_update_nth xs i a, mul_assoc]
| [] _ _ := by simp [update_nth, (nat.zero_le _).not_lt]
end monoid
section group
variables [group α]
/-- This is the `list.prod` version of `mul_inv_rev` -/
@[to_additive "This is the `list.sum` version of `add_neg_rev`"]
lemma prod_inv_reverse : ∀ (L : list α), L.prod⁻¹ = (L.map (λ x, x⁻¹)).reverse.prod
| [] := by simp
| (x :: xs) := by simp [prod_inv_reverse xs]
/-- A non-commutative variant of `list.prod_reverse` -/
@[to_additive "A non-commutative variant of `list.sum_reverse`"]
lemma prod_reverse_noncomm : ∀ (L : list α), L.reverse.prod = (L.map (λ x, x⁻¹)).prod⁻¹ :=
by simp [prod_inv_reverse]
end group
section comm_group
variables [comm_group α]
/-- This is the `list.prod` version of `mul_inv` -/
@[to_additive "This is the `list.sum` version of `add_neg`"]
lemma prod_inv : ∀ (L : list α), L.prod⁻¹ = (L.map (λ x, x⁻¹)).prod
| [] := by simp
| (x :: xs) := by simp [mul_comm, prod_inv xs]
end comm_group
@[simp]
lemma sum_take_add_sum_drop [add_monoid α] :
∀ (L : list α) (i : ℕ), (L.take i).sum + (L.drop i).sum = L.sum
| [] i := by simp
| L 0 := by simp
| (h :: t) (n+1) := by { dsimp, rw [sum_cons, sum_cons, add_assoc, sum_take_add_sum_drop], }
@[simp]
lemma sum_take_succ [add_monoid α] :
∀ (L : list α) (i : ℕ) (p), (L.take (i + 1)).sum = (L.take i).sum + L.nth_le i p
| [] i p := by cases p
| (h :: t) 0 _ := by simp
| (h :: t) (n+1) _ := by { dsimp, rw [sum_cons, sum_cons, sum_take_succ, add_assoc], }
lemma eq_of_sum_take_eq [add_left_cancel_monoid α] {L L' : list α} (h : L.length = L'.length)
(h' : ∀ i ≤ L.length, (L.take i).sum = (L'.take i).sum) : L = L' :=
begin
apply ext_le h (λ i h₁ h₂, _),
have : (L.take (i + 1)).sum = (L'.take (i + 1)).sum := h' _ (nat.succ_le_of_lt h₁),
rw [sum_take_succ L i h₁, sum_take_succ L' i h₂, h' i (le_of_lt h₁)] at this,
exact add_left_cancel this
end
lemma monotone_sum_take [canonically_ordered_add_monoid α] (L : list α) :
monotone (λ i, (L.take i).sum) :=
begin
apply monotone_of_monotone_nat (λ n, _),
by_cases h : n < L.length,
{ rw sum_take_succ _ _ h,
exact le_self_add },
{ push_neg at h,
simp [take_all_of_le h, take_all_of_le (le_trans h (nat.le_succ _))] }
end
@[to_additive sum_nonneg]
lemma one_le_prod_of_one_le [ordered_comm_monoid α] {l : list α} (hl₁ : ∀ x ∈ l, (1 : α) ≤ x) :
1 ≤ l.prod :=
begin
induction l with hd tl ih,
{ simp },
rw prod_cons,
exact one_le_mul (hl₁ hd (mem_cons_self hd tl)) (ih (λ x h, hl₁ x (mem_cons_of_mem hd h))),
end
@[to_additive]
lemma single_le_prod [ordered_comm_monoid α] {l : list α} (hl₁ : ∀ x ∈ l, (1 : α) ≤ x) :
∀ x ∈ l, x ≤ l.prod :=
begin
induction l,
{ simp },
simp_rw [prod_cons, forall_mem_cons] at ⊢ hl₁,
split,
{ exact le_mul_of_one_le_right' (one_le_prod_of_one_le hl₁.2) },
{ exact λ x H, le_mul_of_one_le_of_le hl₁.1 (l_ih hl₁.right x H) },
end
@[to_additive all_zero_of_le_zero_le_of_sum_eq_zero]
lemma all_one_of_le_one_le_of_prod_eq_one [ordered_comm_monoid α]
{l : list α} (hl₁ : ∀ x ∈ l, (1 : α) ≤ x) (hl₂ : l.prod = 1) :
∀ x ∈ l, x = (1 : α) :=
λ x hx, le_antisymm (hl₂ ▸ single_le_prod hl₁ _ hx) (hl₁ x hx)
lemma sum_eq_zero_iff [canonically_ordered_add_monoid α] (l : list α) :
l.sum = 0 ↔ ∀ x ∈ l, x = (0 : α) :=
⟨all_zero_of_le_zero_le_of_sum_eq_zero (λ _ _, zero_le _),
begin
induction l,
{ simp },
{ intro h,
rw [sum_cons, add_eq_zero_iff],
rw forall_mem_cons at h,
exact ⟨h.1, l_ih h.2⟩ },
end⟩
/-- A list with sum not zero must have positive length. -/
lemma length_pos_of_sum_ne_zero [add_monoid α] (L : list α) (h : L.sum ≠ 0) : 0 < L.length :=
by { cases L, { simp at h, cases h, }, { simp, }, }
/-- If all elements in a list are bounded below by `1`, then the length of the list is bounded
by the sum of the elements. -/
lemma length_le_sum_of_one_le (L : list ℕ) (h : ∀ i ∈ L, 1 ≤ i) : L.length ≤ L.sum :=
begin
induction L with j L IH h, { simp },
rw [sum_cons, length, add_comm],
exact add_le_add (h _ (set.mem_insert _ _)) (IH (λ i hi, h i (set.mem_union_right _ hi)))
end
-- Now we tie those lemmas back to their multiplicative versions.
attribute [to_additive] prod_take_mul_prod_drop prod_take_succ length_pos_of_prod_ne_one
/-- A list with positive sum must have positive length. -/
-- This is an easy consequence of `length_pos_of_sum_ne_zero`, but often useful in applications.
lemma length_pos_of_sum_pos [ordered_cancel_add_comm_monoid α] (L : list α) (h : 0 < L.sum) :
0 < L.length :=
length_pos_of_sum_ne_zero L (ne_of_gt h)
@[simp, to_additive]
theorem prod_erase [decidable_eq α] [comm_monoid α] {a} :
Π {l : list α}, a ∈ l → a * (l.erase a).prod = l.prod
| (b::l) h :=
begin
rcases decidable.list.eq_or_ne_mem_of_mem h with rfl | ⟨ne, h⟩,
{ simp only [list.erase, if_pos, prod_cons] },
{ simp only [list.erase, if_neg (mt eq.symm ne), prod_cons, prod_erase h, mul_left_comm a b] }
end
lemma dvd_prod [comm_monoid α] {a} {l : list α} (ha : a ∈ l) : a ∣ l.prod :=
let ⟨s, t, h⟩ := mem_split ha in
by rw [h, prod_append, prod_cons, mul_left_comm]; exact dvd_mul_right _ _
@[simp] theorem sum_const_nat (m n : ℕ) : sum (list.repeat m n) = m * n :=
by induction n; [refl, simp only [*, repeat_succ, sum_cons, nat.mul_succ, add_comm]]
theorem dvd_sum [comm_semiring α] {a} {l : list α} (h : ∀ x ∈ l, a ∣ x) : a ∣ l.sum :=
begin
induction l with x l ih,
{ exact dvd_zero _ },
{ rw [list.sum_cons],
exact dvd_add (h _ (mem_cons_self _ _)) (ih (λ x hx, h x (mem_cons_of_mem _ hx))) }
end
@[simp] theorem length_join (L : list (list α)) : length (join L) = sum (map length L) :=
by induction L; [refl, simp only [*, join, map, sum_cons, length_append]]
@[simp] theorem length_bind (l : list α) (f : α → list β) :
length (list.bind l f) = sum (map (length ∘ f) l) :=
by rw [list.bind, length_join, map_map]
lemma exists_lt_of_sum_lt [linear_ordered_cancel_add_comm_monoid β] {l : list α}
(f g : α → β) (h : (l.map f).sum < (l.map g).sum) : ∃ x ∈ l, f x < g x :=
begin
induction l with x l,
{ exfalso, exact lt_irrefl _ h },
{ by_cases h' : f x < g x, exact ⟨x, mem_cons_self _ _, h'⟩,
rcases l_ih _ with ⟨y, h1y, h2y⟩, refine ⟨y, mem_cons_of_mem x h1y, h2y⟩, simp at h,
exact lt_of_add_lt_add_left (lt_of_lt_of_le h $ add_le_add_right (le_of_not_gt h') _) }
end
lemma exists_le_of_sum_le [linear_ordered_cancel_add_comm_monoid β] {l : list α}
(hl : l ≠ []) (f g : α → β) (h : (l.map f).sum ≤ (l.map g).sum) : ∃ x ∈ l, f x ≤ g x :=
begin
cases l with x l,
{ contradiction },
{ by_cases h' : f x ≤ g x, exact ⟨x, mem_cons_self _ _, h'⟩,
rcases exists_lt_of_sum_lt f g _ with ⟨y, h1y, h2y⟩,
exact ⟨y, mem_cons_of_mem x h1y, le_of_lt h2y⟩, simp at h,
exact lt_of_add_lt_add_left (lt_of_le_of_lt h $ add_lt_add_right (lt_of_not_ge h') _) }
end
-- Several lemmas about sum/head/tail for `list ℕ`.
-- These are hard to generalize well, as they rely on the fact that `default ℕ = 0`.
-- We'd like to state this as `L.head * L.tail.prod = L.prod`,
-- but because `L.head` relies on an inhabited instances and
-- returns a garbage value for the empty list, this is not possible.
-- Instead we write the statement in terms of `(L.nth 0).get_or_else 1`,
-- and below, restate the lemma just for `ℕ`.
@[to_additive]
lemma head_mul_tail_prod' [monoid α] (L : list α) :
(L.nth 0).get_or_else 1 * L.tail.prod = L.prod :=
by cases L; simp
lemma head_add_tail_sum (L : list ℕ) : L.head + L.tail.sum = L.sum :=
by { cases L, { simp, refl, }, { simp, }, }
lemma head_le_sum (L : list ℕ) : L.head ≤ L.sum :=
nat.le.intro (head_add_tail_sum L)
lemma tail_sum (L : list ℕ) : L.tail.sum = L.sum - L.head :=
by rw [← head_add_tail_sum L, add_comm, nat.add_sub_cancel]
section
variables {G : Type*} [comm_group G]
attribute [to_additive] alternating_prod
@[simp, to_additive] lemma alternating_prod_nil :
alternating_prod ([] : list G) = 1 := rfl
@[simp, to_additive] lemma alternating_prod_singleton (g : G) :
alternating_prod [g] = g := rfl
@[simp, to_additive alternating_sum_cons_cons']
lemma alternating_prod_cons_cons (g h : G) (l : list G) :
alternating_prod (g :: h :: l) = g * h⁻¹ * alternating_prod l := rfl
lemma alternating_sum_cons_cons {G : Type*} [add_comm_group G] (g h : G) (l : list G) :
alternating_sum (g :: h :: l) = g - h + alternating_sum l :=
by rw [sub_eq_add_neg, alternating_sum]
end
/-! ### join -/
attribute [simp] join
@[simp] theorem join_eq_nil : ∀ {L : list (list α)}, join L = [] ↔ ∀ l ∈ L, l = []
| [] := iff_of_true rfl (forall_mem_nil _)
| (l::L) := by simp only [join, append_eq_nil, join_eq_nil, forall_mem_cons]
@[simp] theorem join_append (L₁ L₂ : list (list α)) : join (L₁ ++ L₂) = join L₁ ++ join L₂ :=
by induction L₁; [refl, simp only [*, join, cons_append, append_assoc]]
@[simp] theorem join_filter_empty_eq_ff [decidable_pred (λ l : list α, l.empty = ff)] :
∀ {L : list (list α)}, join (L.filter (λ l, l.empty = ff)) = L.join
| [] := rfl
| ([]::L) := by simp [@join_filter_empty_eq_ff L]
| ((a::l)::L) := by simp [@join_filter_empty_eq_ff L]
@[simp] theorem join_filter_ne_nil [decidable_pred (λ l : list α, l ≠ [])] {L : list (list α)} :
join (L.filter (λ l, l ≠ [])) = L.join :=
by simp [join_filter_empty_eq_ff, ← empty_iff_eq_nil]
lemma join_join (l : list (list (list α))) : l.join.join = (l.map join).join :=
by { induction l, simp, simp [l_ih] }
/-- In a join, taking the first elements up to an index which is the sum of the lengths of the
first `i` sublists, is the same as taking the join of the first `i` sublists. -/
lemma take_sum_join (L : list (list α)) (i : ℕ) :
L.join.take ((L.map length).take i).sum = (L.take i).join :=
begin
induction L generalizing i, { simp },
cases i, { simp },
simp [take_append, L_ih]
end
/-- In a join, dropping all the elements up to an index which is the sum of the lengths of the
first `i` sublists, is the same as taking the join after dropping the first `i` sublists. -/
lemma drop_sum_join (L : list (list α)) (i : ℕ) :
L.join.drop ((L.map length).take i).sum = (L.drop i).join :=
begin
induction L generalizing i, { simp },
cases i, { simp },
simp [drop_append, L_ih],
end
/-- Taking only the first `i+1` elements in a list, and then dropping the first `i` ones, one is
left with a list of length `1` made of the `i`-th element of the original list. -/
lemma drop_take_succ_eq_cons_nth_le (L : list α) {i : ℕ} (hi : i < L.length) :
(L.take (i+1)).drop i = [nth_le L i hi] :=
begin
induction L generalizing i,
{ simp only [length] at hi, exact (nat.not_succ_le_zero i hi).elim },
cases i, { simp },
have : i < L_tl.length,
{ simp at hi,
exact nat.lt_of_succ_lt_succ hi },
simp [L_ih this],
refl
end
/-- In a join of sublists, taking the slice between the indices `A` and `B - 1` gives back the
original sublist of index `i` if `A` is the sum of the lenghts of sublists of index `< i`, and
`B` is the sum of the lengths of sublists of index `≤ i`. -/
lemma drop_take_succ_join_eq_nth_le (L : list (list α)) {i : ℕ} (hi : i < L.length) :
(L.join.take ((L.map length).take (i+1)).sum).drop ((L.map length).take i).sum = nth_le L i hi :=
begin
have : (L.map length).take i = ((L.take (i+1)).map length).take i, by simp [map_take, take_take],
simp [take_sum_join, this, drop_sum_join, drop_take_succ_eq_cons_nth_le _ hi]
end
/-- Auxiliary lemma to control elements in a join. -/
lemma sum_take_map_length_lt1 (L : list (list α)) {i j : ℕ}
(hi : i < L.length) (hj : j < (nth_le L i hi).length) :
((L.map length).take i).sum + j < ((L.map length).take (i+1)).sum :=
by simp [hi, sum_take_succ, hj]
/-- Auxiliary lemma to control elements in a join. -/
lemma sum_take_map_length_lt2 (L : list (list α)) {i j : ℕ}
(hi : i < L.length) (hj : j < (nth_le L i hi).length) :
((L.map length).take i).sum + j < L.join.length :=
begin
convert lt_of_lt_of_le (sum_take_map_length_lt1 L hi hj) (monotone_sum_take _ hi),
have : L.length = (L.map length).length, by simp,
simp [this, -length_map]
end
/-- The `n`-th element in a join of sublists is the `j`-th element of the `i`th sublist,
where `n` can be obtained in terms of `i` and `j` by adding the lengths of all the sublists
of index `< i`, and adding `j`. -/
lemma nth_le_join (L : list (list α)) {i j : ℕ}
(hi : i < L.length) (hj : j < (nth_le L i hi).length) :
nth_le L.join (((L.map length).take i).sum + j) (sum_take_map_length_lt2 L hi hj) =
nth_le (nth_le L i hi) j hj :=
by rw [nth_le_take L.join (sum_take_map_length_lt2 L hi hj) (sum_take_map_length_lt1 L hi hj),
nth_le_drop, nth_le_of_eq (drop_take_succ_join_eq_nth_le L hi)]
/-- Two lists of sublists are equal iff their joins coincide, as well as the lengths of the
sublists. -/
theorem eq_iff_join_eq (L L' : list (list α)) :
L = L' ↔ L.join = L'.join ∧ map length L = map length L' :=
begin
refine ⟨λ H, by simp [H], _⟩,
rintros ⟨join_eq, length_eq⟩,
apply ext_le,
{ have : length (map length L) = length (map length L'), by rw length_eq,
simpa using this },
{ assume n h₁ h₂,
rw [← drop_take_succ_join_eq_nth_le, ← drop_take_succ_join_eq_nth_le, join_eq, length_eq] }
end
/-! ### lexicographic ordering -/
/-- Given a strict order `<` on `α`, the lexicographic strict order on `list α`, for which
`[a0, ..., an] < [b0, ..., b_k]` if `a0 < b0` or `a0 = b0` and `[a1, ..., an] < [b1, ..., bk]`.
The definition is given for any relation `r`, not only strict orders. -/
inductive lex (r : α → α → Prop) : list α → list α → Prop
| nil {a l} : lex [] (a :: l)
| cons {a l₁ l₂} (h : lex l₁ l₂) : lex (a :: l₁) (a :: l₂)
| rel {a₁ l₁ a₂ l₂} (h : r a₁ a₂) : lex (a₁ :: l₁) (a₂ :: l₂)
namespace lex
theorem cons_iff {r : α → α → Prop} [is_irrefl α r] {a l₁ l₂} :
lex r (a :: l₁) (a :: l₂) ↔ lex r l₁ l₂ :=
⟨λ h, by cases h with _ _ _ _ _ h _ _ _ _ h;
[exact h, exact (irrefl_of r a h).elim], lex.cons⟩
@[simp] theorem not_nil_right (r : α → α → Prop) (l : list α) : ¬ lex r l [].
instance is_order_connected (r : α → α → Prop)
[is_order_connected α r] [is_trichotomous α r] :
is_order_connected (list α) (lex r) :=
⟨λ l₁, match l₁ with
| _, [], c::l₃, nil := or.inr nil
| _, [], c::l₃, rel _ := or.inr nil
| _, [], c::l₃, cons _ := or.inr nil
| _, b::l₂, c::l₃, nil := or.inl nil
| a::l₁, b::l₂, c::l₃, rel h :=
(is_order_connected.conn _ b _ h).imp rel rel
| a::l₁, b::l₂, _::l₃, cons h := begin
rcases trichotomous_of r a b with ab | rfl | ab,
{ exact or.inl (rel ab) },
{ exact (_match _ l₂ _ h).imp cons cons },
{ exact or.inr (rel ab) }
end
end⟩
instance is_trichotomous (r : α → α → Prop) [is_trichotomous α r] :
is_trichotomous (list α) (lex r) :=
⟨λ l₁, match l₁ with
| [], [] := or.inr (or.inl rfl)
| [], b::l₂ := or.inl nil
| a::l₁, [] := or.inr (or.inr nil)
| a::l₁, b::l₂ := begin
rcases trichotomous_of r a b with ab | rfl | ab,
{ exact or.inl (rel ab) },
{ exact (_match l₁ l₂).imp cons
(or.imp (congr_arg _) cons) },
{ exact or.inr (or.inr (rel ab)) }
end
end⟩
instance is_asymm (r : α → α → Prop)
[is_asymm α r] : is_asymm (list α) (lex r) :=
⟨λ l₁, match l₁ with
| a::l₁, b::l₂, lex.rel h₁, lex.rel h₂ := asymm h₁ h₂
| a::l₁, b::l₂, lex.rel h₁, lex.cons h₂ := asymm h₁ h₁
| a::l₁, b::l₂, lex.cons h₁, lex.rel h₂ := asymm h₂ h₂
| a::l₁, b::l₂, lex.cons h₁, lex.cons h₂ :=
by exact _match _ _ h₁ h₂
end⟩
instance is_strict_total_order (r : α → α → Prop)
[is_strict_total_order' α r] : is_strict_total_order' (list α) (lex r) :=
{..is_strict_weak_order_of_is_order_connected}
instance decidable_rel [decidable_eq α] (r : α → α → Prop)
[decidable_rel r] : decidable_rel (lex r)
| l₁ [] := is_false $ λ h, by cases h
| [] (b::l₂) := is_true lex.nil
| (a::l₁) (b::l₂) := begin
haveI := decidable_rel l₁ l₂,
refine decidable_of_iff (r a b ∨ a = b ∧ lex r l₁ l₂) ⟨λ h, _, λ h, _⟩,
{ rcases h with h | ⟨rfl, h⟩,
{ exact lex.rel h },
{ exact lex.cons h } },
{ rcases h with _|⟨_,_,_,h⟩|⟨_,_,_,_,h⟩,
{ exact or.inr ⟨rfl, h⟩ },
{ exact or.inl h } }
end
theorem append_right (r : α → α → Prop) :
∀ {s₁ s₂} t, lex r s₁ s₂ → lex r s₁ (s₂ ++ t)
| _ _ t nil := nil
| _ _ t (cons h) := cons (append_right _ h)
| _ _ t (rel r) := rel r
theorem append_left (R : α → α → Prop) {t₁ t₂} (h : lex R t₁ t₂) :
∀ s, lex R (s ++ t₁) (s ++ t₂)
| [] := h
| (a::l) := cons (append_left l)
theorem imp {r s : α → α → Prop} (H : ∀ a b, r a b → s a b) :
∀ l₁ l₂, lex r l₁ l₂ → lex s l₁ l₂
| _ _ nil := nil
| _ _ (cons h) := cons (imp _ _ h)
| _ _ (rel r) := rel (H _ _ r)
theorem to_ne : ∀ {l₁ l₂ : list α}, lex (≠) l₁ l₂ → l₁ ≠ l₂
| _ _ (cons h) e := to_ne h (list.cons.inj e).2
| _ _ (rel r) e := r (list.cons.inj e).1
theorem _root_.decidable.list.lex.ne_iff [decidable_eq α]
{l₁ l₂ : list α} (H : length l₁ ≤ length l₂) : lex (≠) l₁ l₂ ↔ l₁ ≠ l₂ :=
⟨to_ne, λ h, begin
induction l₁ with a l₁ IH generalizing l₂; cases l₂ with b l₂,
{ contradiction },
{ apply nil },
{ exact (not_lt_of_ge H).elim (succ_pos _) },
{ by_cases ab : a = b,
{ subst b, apply cons,
exact IH (le_of_succ_le_succ H) (mt (congr_arg _) h) },
{ exact rel ab } }
end⟩
theorem ne_iff {l₁ l₂ : list α} (H : length l₁ ≤ length l₂) : lex (≠) l₁ l₂ ↔ l₁ ≠ l₂ :=
by classical; exact decidable.list.lex.ne_iff H
end lex
--Note: this overrides an instance in core lean
instance has_lt' [has_lt α] : has_lt (list α) := ⟨lex (<)⟩
theorem nil_lt_cons [has_lt α] (a : α) (l : list α) : [] < a :: l :=
lex.nil
instance [linear_order α] : linear_order (list α) :=
linear_order_of_STO' (lex (<))
--Note: this overrides an instance in core lean
instance has_le' [linear_order α] : has_le (list α) :=
preorder.to_has_le _
/-! ### all & any -/
@[simp] theorem all_nil (p : α → bool) : all [] p = tt := rfl
@[simp] theorem all_cons (p : α → bool) (a : α) (l : list α) :
all (a::l) p = (p a && all l p) := rfl
theorem all_iff_forall {p : α → bool} {l : list α} : all l p ↔ ∀ a ∈ l, p a :=
begin
induction l with a l ih,
{ exact iff_of_true rfl (forall_mem_nil _) },
simp only [all_cons, band_coe_iff, ih, forall_mem_cons]
end
theorem all_iff_forall_prop {p : α → Prop} [decidable_pred p]
{l : list α} : all l (λ a, p a) ↔ ∀ a ∈ l, p a :=
by simp only [all_iff_forall, bool.of_to_bool_iff]
@[simp] theorem any_nil (p : α → bool) : any [] p = ff := rfl
@[simp] theorem any_cons (p : α → bool) (a : α) (l : list α) :
any (a::l) p = (p a || any l p) := rfl
theorem any_iff_exists {p : α → bool} {l : list α} : any l p ↔ ∃ a ∈ l, p a :=
begin
induction l with a l ih,
{ exact iff_of_false bool.not_ff (not_exists_mem_nil _) },
simp only [any_cons, bor_coe_iff, ih, exists_mem_cons_iff]
end
theorem any_iff_exists_prop {p : α → Prop} [decidable_pred p]
{l : list α} : any l (λ a, p a) ↔ ∃ a ∈ l, p a :=
by simp [any_iff_exists]
theorem any_of_mem {p : α → bool} {a : α} {l : list α} (h₁ : a ∈ l) (h₂ : p a) : any l p :=
any_iff_exists.2 ⟨_, h₁, h₂⟩
@[priority 500] instance decidable_forall_mem {p : α → Prop} [decidable_pred p] (l : list α) :
decidable (∀ x ∈ l, p x) :=
decidable_of_iff _ all_iff_forall_prop
instance decidable_exists_mem {p : α → Prop} [decidable_pred p] (l : list α) :
decidable (∃ x ∈ l, p x) :=
decidable_of_iff _ any_iff_exists_prop
/-! ### map for partial functions -/
/-- Partial map. If `f : Π a, p a → β` is a partial function defined on
`a : α` satisfying `p`, then `pmap f l h` is essentially the same as `map f l`
but is defined only when all members of `l` satisfy `p`, using the proof
to apply `f`. -/
@[simp] def pmap {p : α → Prop} (f : Π a, p a → β) : Π l : list α, (∀ a ∈ l, p a) → list β
| [] H := []
| (a::l) H := f a (forall_mem_cons.1 H).1 :: pmap l (forall_mem_cons.1 H).2
/-- "Attach" the proof that the elements of `l` are in `l` to produce a new list
with the same elements but in the type `{x // x ∈ l}`. -/
def attach (l : list α) : list {x // x ∈ l} := pmap subtype.mk l (λ a, id)
theorem sizeof_lt_sizeof_of_mem [has_sizeof α] {x : α} {l : list α} (hx : x ∈ l) :
sizeof x < sizeof l :=
begin
induction l with h t ih; cases hx,
{ rw hx, exact lt_add_of_lt_of_nonneg (lt_one_add _) (nat.zero_le _) },
{ exact lt_add_of_pos_of_le (zero_lt_one_add _) (le_of_lt (ih hx)) }
end
theorem pmap_eq_map (p : α → Prop) (f : α → β) (l : list α) (H) :
@pmap _ _ p (λ a _, f a) l H = map f l :=
by induction l; [refl, simp only [*, pmap, map]]; split; refl
theorem pmap_congr {p q : α → Prop} {f : Π a, p a → β} {g : Π a, q a → β}
(l : list α) {H₁ H₂} (h : ∀ a h₁ h₂, f a h₁ = g a h₂) :
pmap f l H₁ = pmap g l H₂ :=
by induction l with _ _ ih; [refl, rw [pmap, pmap, h, ih]]
theorem map_pmap {p : α → Prop} (g : β → γ) (f : Π a, p a → β)
(l H) : map g (pmap f l H) = pmap (λ a h, g (f a h)) l H :=
by induction l; [refl, simp only [*, pmap, map]]; split; refl
theorem pmap_map {p : β → Prop} (g : ∀ b, p b → γ) (f : α → β)
(l H) : pmap g (map f l) H = pmap (λ a h, g (f a) h) l (λ a h, H _ (mem_map_of_mem _ h)) :=
by induction l; [refl, simp only [*, pmap, map]]; split; refl
theorem pmap_eq_map_attach {p : α → Prop} (f : Π a, p a → β)
(l H) : pmap f l H = l.attach.map (λ x, f x.1 (H _ x.2)) :=
by rw [attach, map_pmap]; exact pmap_congr l (λ a h₁ h₂, rfl)
theorem attach_map_val (l : list α) : l.attach.map subtype.val = l :=
by rw [attach, map_pmap]; exact (pmap_eq_map _ _ _ _).trans (map_id l)
@[simp] theorem mem_attach (l : list α) : ∀ x, x ∈ l.attach | ⟨a, h⟩ :=
by have := mem_map.1 (by rw [attach_map_val]; exact h);
{ rcases this with ⟨⟨_, _⟩, m, rfl⟩, exact m }
@[simp] theorem mem_pmap {p : α → Prop} {f : Π a, p a → β}
{l H b} : b ∈ pmap f l H ↔ ∃ a (h : a ∈ l), f a (H a h) = b :=
by simp only [pmap_eq_map_attach, mem_map, mem_attach, true_and, subtype.exists]
@[simp] theorem length_pmap {p : α → Prop} {f : Π a, p a → β}
{l H} : length (pmap f l H) = length l :=
by induction l; [refl, simp only [*, pmap, length]]
@[simp] lemma length_attach (L : list α) : L.attach.length = L.length := length_pmap
@[simp] lemma pmap_eq_nil {p : α → Prop} {f : Π a, p a → β}
{l H} : pmap f l H = [] ↔ l = [] :=
by rw [← length_eq_zero, length_pmap, length_eq_zero]
@[simp] lemma attach_eq_nil (l : list α) : l.attach = [] ↔ l = [] := pmap_eq_nil
lemma last_pmap {α β : Type*} (p : α → Prop) (f : Π a, p a → β)
(l : list α) (hl₁ : ∀ a ∈ l, p a) (hl₂ : l ≠ []) :
(l.pmap f hl₁).last (mt list.pmap_eq_nil.1 hl₂) = f (l.last hl₂) (hl₁ _ (list.last_mem hl₂)) :=
begin
induction l with l_hd l_tl l_ih,
{ apply (hl₂ rfl).elim },
{ cases l_tl,
{ simp },
{ apply l_ih } }
end
lemma nth_pmap {p : α → Prop} (f : Π a, p a → β) {l : list α} (h : ∀ a ∈ l, p a) (n : ℕ) :
nth (pmap f l h) n = option.pmap f (nth l n) (λ x H, h x (nth_mem H)) :=
begin
induction l with hd tl hl generalizing n,
{ simp },
{ cases n; simp [hl] }
end
lemma nth_le_pmap {p : α → Prop} (f : Π a, p a → β) {l : list α} (h : ∀ a ∈ l, p a) {n : ℕ}
(hn : n < (pmap f l h).length) :
nth_le (pmap f l h) n hn = f (nth_le l n (@length_pmap _ _ p f l h ▸ hn))
(h _ (nth_le_mem l n (@length_pmap _ _ p f l h ▸ hn))) :=
begin
induction l with hd tl hl generalizing n,
{ simp only [length, pmap] at hn,
exact absurd hn (not_lt_of_le n.zero_le) },
{ cases n,
{ simp },
{ simpa [hl] } }
end
/-! ### find -/
section find
variables {p : α → Prop} [decidable_pred p] {l : list α} {a : α}
@[simp] theorem find_nil (p : α → Prop) [decidable_pred p] : find p [] = none :=
rfl
@[simp] theorem find_cons_of_pos (l) (h : p a) : find p (a::l) = some a :=
if_pos h
@[simp] theorem find_cons_of_neg (l) (h : ¬ p a) : find p (a::l) = find p l :=
if_neg h
@[simp] theorem find_eq_none : find p l = none ↔ ∀ x ∈ l, ¬ p x :=
begin
induction l with a l IH,
{ exact iff_of_true rfl (forall_mem_nil _) },
rw forall_mem_cons, by_cases h : p a,
{ simp only [find_cons_of_pos _ h, h, not_true, false_and] },
{ rwa [find_cons_of_neg _ h, iff_true_intro h, true_and] }
end
theorem find_some (H : find p l = some a) : p a :=
begin
induction l with b l IH, {contradiction},
by_cases h : p b,
{ rw find_cons_of_pos _ h at H, cases H, exact h },
{ rw find_cons_of_neg _ h at H, exact IH H }
end
@[simp] theorem find_mem (H : find p l = some a) : a ∈ l :=
begin
induction l with b l IH, {contradiction},
by_cases h : p b,
{ rw find_cons_of_pos _ h at H, cases H, apply mem_cons_self },
{ rw find_cons_of_neg _ h at H, exact mem_cons_of_mem _ (IH H) }
end
end find
/-! ### lookmap -/
section lookmap
variables (f : α → option α)
@[simp] theorem lookmap_nil : [].lookmap f = [] := rfl
@[simp] theorem lookmap_cons_none {a : α} (l : list α) (h : f a = none) :
(a :: l).lookmap f = a :: l.lookmap f :=
by simp [lookmap, h]
@[simp] theorem lookmap_cons_some {a b : α} (l : list α) (h : f a = some b) :
(a :: l).lookmap f = b :: l :=
by simp [lookmap, h]
theorem lookmap_some : ∀ l : list α, l.lookmap some = l
| [] := rfl
| (a::l) := rfl
theorem lookmap_none : ∀ l : list α, l.lookmap (λ _, none) = l
| [] := rfl
| (a::l) := congr_arg (cons a) (lookmap_none l)
theorem lookmap_congr {f g : α → option α} :
∀ {l : list α}, (∀ a ∈ l, f a = g a) → l.lookmap f = l.lookmap g
| [] H := rfl
| (a::l) H := begin
cases forall_mem_cons.1 H with H₁ H₂,
cases h : g a with b,
{ simp [h, H₁.trans h, lookmap_congr H₂] },
{ simp [lookmap_cons_some _ _ h, lookmap_cons_some _ _ (H₁.trans h)] }
end
theorem lookmap_of_forall_not {l : list α} (H : ∀ a ∈ l, f a = none) : l.lookmap f = l :=
(lookmap_congr H).trans (lookmap_none l)
theorem lookmap_map_eq (g : α → β) (h : ∀ a (b ∈ f a), g a = g b) :
∀ l : list α, map g (l.lookmap f) = map g l
| [] := rfl
| (a::l) := begin
cases h' : f a with b,
{ simp [h', lookmap_map_eq] },
{ simp [lookmap_cons_some _ _ h', h _ _ h'] }
end
theorem lookmap_id' (h : ∀ a (b ∈ f a), a = b) (l : list α) : l.lookmap f = l :=
by rw [← map_id (l.lookmap f), lookmap_map_eq, map_id]; exact h
theorem length_lookmap (l : list α) : length (l.lookmap f) = length l :=
by rw [← length_map, lookmap_map_eq _ (λ _, ()), length_map]; simp
end lookmap
/-! ### filter_map -/
@[simp] theorem filter_map_nil (f : α → option β) : filter_map f [] = [] := rfl
@[simp] theorem filter_map_cons_none {f : α → option β} (a : α) (l : list α) (h : f a = none) :
filter_map f (a :: l) = filter_map f l :=
by simp only [filter_map, h]
@[simp] theorem filter_map_cons_some (f : α → option β)
(a : α) (l : list α) {b : β} (h : f a = some b) :
filter_map f (a :: l) = b :: filter_map f l :=
by simp only [filter_map, h]; split; refl
lemma filter_map_append {α β : Type*} (l l' : list α) (f : α → option β) :
filter_map f (l ++ l') = filter_map f l ++ filter_map f l' :=
begin
induction l with hd tl hl generalizing l',
{ simp },
{ rw [cons_append, filter_map, filter_map],
cases f hd;
simp only [filter_map, hl, cons_append, eq_self_iff_true, and_self] }
end
theorem filter_map_eq_map (f : α → β) : filter_map (some ∘ f) = map f :=
begin
funext l,
induction l with a l IH, {refl},
simp only [filter_map_cons_some (some ∘ f) _ _ rfl, IH, map_cons], split; refl
end
theorem filter_map_eq_filter (p : α → Prop) [decidable_pred p] :
filter_map (option.guard p) = filter p :=
begin
funext l,
induction l with a l IH, {refl},
by_cases pa : p a,
{ simp only [filter_map, option.guard, IH, if_pos pa, filter_cons_of_pos _ pa], split; refl },
{ simp only [filter_map, option.guard, IH, if_neg pa, filter_cons_of_neg _ pa] }
end
theorem filter_map_filter_map (f : α → option β) (g : β → option γ) (l : list α) :
filter_map g (filter_map f l) = filter_map (λ x, (f x).bind g) l :=
begin
induction l with a l IH, {refl},
cases h : f a with b,
{ rw [filter_map_cons_none _ _ h, filter_map_cons_none, IH],
simp only [h, option.none_bind'] },
rw filter_map_cons_some _ _ _ h,
cases h' : g b with c;
[ rw [filter_map_cons_none _ _ h', filter_map_cons_none, IH],
rw [filter_map_cons_some _ _ _ h', filter_map_cons_some, IH] ];
simp only [h, h', option.some_bind']
end
theorem map_filter_map (f : α → option β) (g : β → γ) (l : list α) :
map g (filter_map f l) = filter_map (λ x, (f x).map g) l :=
by rw [← filter_map_eq_map, filter_map_filter_map]; refl
theorem filter_map_map (f : α → β) (g : β → option γ) (l : list α) :
filter_map g (map f l) = filter_map (g ∘ f) l :=
by rw [← filter_map_eq_map, filter_map_filter_map]; refl
theorem filter_filter_map (f : α → option β) (p : β → Prop) [decidable_pred p] (l : list α) :
filter p (filter_map f l) = filter_map (λ x, (f x).filter p) l :=
by rw [← filter_map_eq_filter, filter_map_filter_map]; refl
theorem filter_map_filter (p : α → Prop) [decidable_pred p] (f : α → option β) (l : list α) :
filter_map f (filter p l) = filter_map (λ x, if p x then f x else none) l :=
begin
rw [← filter_map_eq_filter, filter_map_filter_map], congr,
funext x,
show (option.guard p x).bind f = ite (p x) (f x) none,
by_cases h : p x,
{ simp only [option.guard, if_pos h, option.some_bind'] },
{ simp only [option.guard, if_neg h, option.none_bind'] }
end
@[simp] theorem filter_map_some (l : list α) : filter_map some l = l :=
by rw filter_map_eq_map; apply map_id
@[simp] theorem mem_filter_map (f : α → option β) (l : list α) {b : β} :
b ∈ filter_map f l ↔ ∃ a, a ∈ l ∧ f a = some b :=
begin
induction l with a l IH,
{ split, { intro H, cases H }, { rintro ⟨_, H, _⟩, cases H } },
cases h : f a with b',
{ have : f a ≠ some b, {rw h, intro, contradiction},
simp only [filter_map_cons_none _ _ h, IH, mem_cons_iff,
or_and_distrib_right, exists_or_distrib, exists_eq_left, this, false_or] },
{ have : f a = some b ↔ b = b',
{ split; intro t, {rw t at h; injection h}, {exact t.symm ▸ h} },
simp only [filter_map_cons_some _ _ _ h, IH, mem_cons_iff,
or_and_distrib_right, exists_or_distrib, this, exists_eq_left] }
end
theorem map_filter_map_of_inv (f : α → option β) (g : β → α)
(H : ∀ x : α, (f x).map g = some x) (l : list α) :
map g (filter_map f l) = l :=
by simp only [map_filter_map, H, filter_map_some]
theorem sublist.filter_map (f : α → option β) {l₁ l₂ : list α}
(s : l₁ <+ l₂) : filter_map f l₁ <+ filter_map f l₂ :=
by induction s with l₁ l₂ a s IH l₁ l₂ a s IH;
simp only [filter_map]; cases f a with b;
simp only [filter_map, IH, sublist.cons, sublist.cons2]
theorem sublist.map (f : α → β) {l₁ l₂ : list α}
(s : l₁ <+ l₂) : map f l₁ <+ map f l₂ :=
filter_map_eq_map f ▸ s.filter_map _
/-! ### reduce_option -/
@[simp] lemma reduce_option_cons_of_some (x : α) (l : list (option α)) :
reduce_option (some x :: l) = x :: l.reduce_option :=
by simp only [reduce_option, filter_map, id.def, eq_self_iff_true, and_self]
@[simp] lemma reduce_option_cons_of_none (l : list (option α)) :
reduce_option (none :: l) = l.reduce_option :=
by simp only [reduce_option, filter_map, id.def]
@[simp] lemma reduce_option_nil : @reduce_option α [] = [] := rfl
@[simp] lemma reduce_option_map {l : list (option α)} {f : α → β} :
reduce_option (map (option.map f) l) = map f (reduce_option l) :=
begin
induction l with hd tl hl,
{ simp only [reduce_option_nil, map_nil] },
{ cases hd;
simpa only [true_and, option.map_some', map, eq_self_iff_true,
reduce_option_cons_of_some] using hl },
end
lemma reduce_option_append (l l' : list (option α)) :
(l ++ l').reduce_option = l.reduce_option ++ l'.reduce_option :=
filter_map_append l l' id
lemma reduce_option_length_le (l : list (option α)) :
l.reduce_option.length ≤ l.length :=
begin
induction l with hd tl hl,
{ simp only [reduce_option_nil, length] },
{ cases hd,
{ exact nat.le_succ_of_le hl },
{ simpa only [length, add_le_add_iff_right, reduce_option_cons_of_some] using hl} }
end
lemma reduce_option_length_eq_iff {l : list (option α)} :
l.reduce_option.length = l.length ↔ ∀ x ∈ l, option.is_some x :=
begin
induction l with hd tl hl,
{ simp only [forall_const, reduce_option_nil, not_mem_nil,
forall_prop_of_false, eq_self_iff_true, length, not_false_iff] },
{ cases hd,
{ simp only [mem_cons_iff, forall_eq_or_imp, bool.coe_sort_ff, false_and,
reduce_option_cons_of_none, length, option.is_some_none, iff_false],
intro H,
have := reduce_option_length_le tl,
rw H at this,
exact absurd (nat.lt_succ_self _) (not_lt_of_le this) },
{ simp only [hl, true_and, mem_cons_iff, forall_eq_or_imp, add_left_inj,
bool.coe_sort_tt, length, option.is_some_some, reduce_option_cons_of_some] } }
end
lemma reduce_option_length_lt_iff {l : list (option α)} :
l.reduce_option.length < l.length ↔ none ∈ l :=
begin
rw [(reduce_option_length_le l).lt_iff_ne, ne, reduce_option_length_eq_iff],
induction l; simp *,
rw [eq_comm, ← option.not_is_some_iff_eq_none, decidable.imp_iff_not_or]
end
lemma reduce_option_singleton (x : option α) :
[x].reduce_option = x.to_list :=
by cases x; refl
lemma reduce_option_concat (l : list (option α)) (x : option α) :
(l.concat x).reduce_option = l.reduce_option ++ x.to_list :=
begin
induction l with hd tl hl generalizing x,
{ cases x;
simp [option.to_list] },
{ simp only [concat_eq_append, reduce_option_append] at hl,
cases hd;
simp [hl, reduce_option_append] }
end
lemma reduce_option_concat_of_some (l : list (option α)) (x : α) :
(l.concat (some x)).reduce_option = l.reduce_option.concat x :=
by simp only [reduce_option_nil, concat_eq_append, reduce_option_append, reduce_option_cons_of_some]
lemma reduce_option_mem_iff {l : list (option α)} {x : α} :
x ∈ l.reduce_option ↔ (some x) ∈ l :=
by simp only [reduce_option, id.def, mem_filter_map, exists_eq_right]
lemma reduce_option_nth_iff {l : list (option α)} {x : α} :
(∃ i, l.nth i = some (some x)) ↔ ∃ i, l.reduce_option.nth i = some x :=
by rw [←mem_iff_nth, ←mem_iff_nth, reduce_option_mem_iff]
/-! ### filter -/
section filter
variables {p : α → Prop} [decidable_pred p]
theorem filter_eq_foldr (p : α → Prop) [decidable_pred p] (l : list α) :
filter p l = foldr (λ a out, if p a then a :: out else out) [] l :=
by induction l; simp [*, filter]
lemma filter_congr {p q : α → Prop} [decidable_pred p] [decidable_pred q]
: ∀ {l : list α}, (∀ x ∈ l, p x ↔ q x) → filter p l = filter q l
| [] _ := rfl
| (a::l) h := by rw forall_mem_cons at h; by_cases pa : p a;
[simp only [filter_cons_of_pos _ pa, filter_cons_of_pos _ (h.1.1 pa), filter_congr h.2],
simp only [filter_cons_of_neg _ pa, filter_cons_of_neg _ (mt h.1.2 pa), filter_congr h.2]];
split; refl
@[simp] theorem filter_subset (l : list α) : filter p l ⊆ l :=
(filter_sublist l).subset
theorem of_mem_filter {a : α} : ∀ {l}, a ∈ filter p l → p a
| (b::l) ain :=
if pb : p b then
have a ∈ b :: filter p l, by simpa only [filter_cons_of_pos _ pb] using ain,
or.elim (eq_or_mem_of_mem_cons this)
(assume : a = b, begin rw [← this] at pb, exact pb end)
(assume : a ∈ filter p l, of_mem_filter this)
else
begin simp only [filter_cons_of_neg _ pb] at ain, exact (of_mem_filter ain) end
theorem mem_of_mem_filter {a : α} {l} (h : a ∈ filter p l) : a ∈ l :=
filter_subset l h
theorem mem_filter_of_mem {a : α} : ∀ {l}, a ∈ l → p a → a ∈ filter p l
| (_::l) (or.inl rfl) pa := by rw filter_cons_of_pos _ pa; apply mem_cons_self
| (b::l) (or.inr ain) pa := if pb : p b
then by rw [filter_cons_of_pos _ pb]; apply mem_cons_of_mem; apply mem_filter_of_mem ain pa
else by rw [filter_cons_of_neg _ pb]; apply mem_filter_of_mem ain pa
@[simp] theorem mem_filter {a : α} {l} : a ∈ filter p l ↔ a ∈ l ∧ p a :=
⟨λ h, ⟨mem_of_mem_filter h, of_mem_filter h⟩, λ ⟨h₁, h₂⟩, mem_filter_of_mem h₁ h₂⟩
theorem filter_eq_self {l} : filter p l = l ↔ ∀ a ∈ l, p a :=
begin
induction l with a l ih,
{ exact iff_of_true rfl (forall_mem_nil _) },
rw forall_mem_cons, by_cases p a,
{ rw [filter_cons_of_pos _ h, cons_inj, ih, and_iff_right h] },
{ rw [filter_cons_of_neg _ h],
refine iff_of_false _ (mt and.left h), intro e,
have := filter_sublist l, rw e at this,
exact not_lt_of_ge (length_le_of_sublist this) (lt_succ_self _) }
end
theorem filter_eq_nil {l} : filter p l = [] ↔ ∀ a ∈ l, ¬p a :=
by simp only [eq_nil_iff_forall_not_mem, mem_filter, not_and]
variable (p)
theorem filter_sublist_filter {l₁ l₂} (s : l₁ <+ l₂) : filter p l₁ <+ filter p l₂ :=
filter_map_eq_filter p ▸ s.filter_map _
theorem map_filter (f : β → α) (l : list β) :
filter p (map f l) = map f (filter (p ∘ f) l) :=
by rw [← filter_map_eq_map, filter_filter_map, filter_map_filter]; refl
@[simp] theorem filter_filter (q) [decidable_pred q] : ∀ l,
filter p (filter q l) = filter (λ a, p a ∧ q a) l
| [] := rfl
| (a :: l) := by by_cases hp : p a; by_cases hq : q a; simp only [hp, hq, filter, if_true, if_false,
true_and, false_and, filter_filter l, eq_self_iff_true]
@[simp] lemma filter_true {h : decidable_pred (λ a : α, true)} (l : list α) :
@filter α (λ _, true) h l = l :=
by convert filter_eq_self.2 (λ _ _, trivial)
@[simp] lemma filter_false {h : decidable_pred (λ a : α, false)} (l : list α) :
@filter α (λ _, false) h l = [] :=
by convert filter_eq_nil.2 (λ _ _, id)
@[simp] theorem span_eq_take_drop : ∀ (l : list α), span p l = (take_while p l, drop_while p l)
| [] := rfl
| (a::l) :=
if pa : p a then by simp only [span, if_pos pa, span_eq_take_drop l, take_while, drop_while]
else by simp only [span, take_while, drop_while, if_neg pa]
@[simp] theorem take_while_append_drop : ∀ (l : list α), take_while p l ++ drop_while p l = l
| [] := rfl
| (a::l) := if pa : p a then by rw [take_while, drop_while, if_pos pa, if_pos pa, cons_append,
take_while_append_drop l]
else by rw [take_while, drop_while, if_neg pa, if_neg pa, nil_append]
@[simp] theorem countp_nil : countp p [] = 0 := rfl
@[simp] theorem countp_cons_of_pos {a : α} (l) (pa : p a) : countp p (a::l) = countp p l + 1 :=
if_pos pa
@[simp] theorem countp_cons_of_neg {a : α} (l) (pa : ¬ p a) : countp p (a::l) = countp p l :=
if_neg pa
theorem countp_eq_length_filter (l) : countp p l = length (filter p l) :=
by induction l with x l ih; [refl, by_cases (p x)];
[simp only [filter_cons_of_pos _ h, countp, ih, if_pos h],
simp only [countp_cons_of_neg _ _ h, ih, filter_cons_of_neg _ h]]; refl
local attribute [simp] countp_eq_length_filter
@[simp] theorem countp_append (l₁ l₂) : countp p (l₁ ++ l₂) = countp p l₁ + countp p l₂ :=
by simp only [countp_eq_length_filter, filter_append, length_append]
theorem countp_pos {l} : 0 < countp p l ↔ ∃ a ∈ l, p a :=
by simp only [countp_eq_length_filter, length_pos_iff_exists_mem, mem_filter, exists_prop]
theorem countp_le_of_sublist {l₁ l₂} (s : l₁ <+ l₂) : countp p l₁ ≤ countp p l₂ :=
by simpa only [countp_eq_length_filter] using length_le_of_sublist (filter_sublist_filter p s)
@[simp] theorem countp_filter {q} [decidable_pred q] (l : list α) :
countp p (filter q l) = countp (λ a, p a ∧ q a) l :=
by simp only [countp_eq_length_filter, filter_filter]
end filter
/-! ### count -/
section count
variable [decidable_eq α]
@[simp] theorem count_nil (a : α) : count a [] = 0 := rfl
theorem count_cons (a b : α) (l : list α) :
count a (b :: l) = if a = b then succ (count a l) else count a l := rfl
theorem count_cons' (a b : α) (l : list α) :
count a (b :: l) = count a l + (if a = b then 1 else 0) :=
begin rw count_cons, split_ifs; refl end
@[simp] theorem count_cons_self (a : α) (l : list α) : count a (a::l) = succ (count a l) :=
if_pos rfl
@[simp, priority 990]
theorem count_cons_of_ne {a b : α} (h : a ≠ b) (l : list α) : count a (b::l) = count a l :=
if_neg h
theorem count_tail : Π (l : list α) (a : α) (h : 0 < l.length),
l.tail.count a = l.count a - ite (a = list.nth_le l 0 h) 1 0
| (_ :: _) a h := by { rw [count_cons], split_ifs; simp }
theorem count_le_of_sublist (a : α) {l₁ l₂} : l₁ <+ l₂ → count a l₁ ≤ count a l₂ :=
countp_le_of_sublist _
theorem count_le_count_cons (a b : α) (l : list α) : count a l ≤ count a (b :: l) :=
count_le_of_sublist _ (sublist_cons _ _)
theorem count_singleton (a : α) : count a [a] = 1 := if_pos rfl
@[simp] theorem count_append (a : α) : ∀ l₁ l₂, count a (l₁ ++ l₂) = count a l₁ + count a l₂ :=
countp_append _
theorem count_concat (a : α) (l : list α) : count a (concat l a) = succ (count a l) :=
by simp [-add_comm]
theorem count_pos {a : α} {l : list α} : 0 < count a l ↔ a ∈ l :=
by simp only [count, countp_pos, exists_prop, exists_eq_right']
@[simp, priority 980]
theorem count_eq_zero_of_not_mem {a : α} {l : list α} (h : a ∉ l) : count a l = 0 :=
decidable.by_contradiction $ λ h', h $ count_pos.1 (nat.pos_of_ne_zero h')
theorem not_mem_of_count_eq_zero {a : α} {l : list α} (h : count a l = 0) : a ∉ l :=
λ h', ne_of_gt (count_pos.2 h') h
@[simp] theorem count_repeat (a : α) (n : ℕ) : count a (repeat a n) = n :=
by rw [count, countp_eq_length_filter, filter_eq_self.2, length_repeat];
exact λ b m, (eq_of_mem_repeat m).symm
theorem le_count_iff_repeat_sublist {a : α} {l : list α} {n : ℕ} :
n ≤ count a l ↔ repeat a n <+ l :=
⟨λ h, ((repeat_sublist_repeat a).2 h).trans $
have filter (eq a) l = repeat a (count a l), from eq_repeat.2
⟨by simp only [count, countp_eq_length_filter], λ b m, (of_mem_filter m).symm⟩,
by rw ← this; apply filter_sublist,
λ h, by simpa only [count_repeat] using count_le_of_sublist a h⟩
theorem repeat_count_eq_of_count_eq_length {a : α} {l : list α} (h : count a l = length l) :
repeat a (count a l) = l :=
eq_of_sublist_of_length_eq (le_count_iff_repeat_sublist.mp (le_refl (count a l)))
(eq.trans (length_repeat a (count a l)) h)
@[simp] theorem count_filter {p} [decidable_pred p]
{a} {l : list α} (h : p a) : count a (filter p l) = count a l :=
by simp only [count, countp_filter]; congr; exact
set.ext (λ b, and_iff_left_of_imp (λ e, e ▸ h))
end count
/-! ### prefix, suffix, infix -/
@[simp] theorem prefix_append (l₁ l₂ : list α) : l₁ <+: l₁ ++ l₂ := ⟨l₂, rfl⟩
@[simp] theorem suffix_append (l₁ l₂ : list α) : l₂ <:+ l₁ ++ l₂ := ⟨l₁, rfl⟩
theorem infix_append (l₁ l₂ l₃ : list α) : l₂ <:+: l₁ ++ l₂ ++ l₃ := ⟨l₁, l₃, rfl⟩
@[simp] theorem infix_append' (l₁ l₂ l₃ : list α) : l₂ <:+: l₁ ++ (l₂ ++ l₃) :=
by rw ← list.append_assoc; apply infix_append
theorem nil_prefix (l : list α) : [] <+: l := ⟨l, rfl⟩
theorem nil_suffix (l : list α) : [] <:+ l := ⟨l, append_nil _⟩
@[refl] theorem prefix_refl (l : list α) : l <+: l := ⟨[], append_nil _⟩
@[refl] theorem suffix_refl (l : list α) : l <:+ l := ⟨[], rfl⟩
@[simp] theorem suffix_cons (a : α) : ∀ l, l <:+ a :: l := suffix_append [a]
theorem prefix_concat (a : α) (l) : l <+: concat l a := by simp
theorem infix_of_prefix {l₁ l₂ : list α} : l₁ <+: l₂ → l₁ <:+: l₂ :=
λ⟨t, h⟩, ⟨[], t, h⟩
theorem infix_of_suffix {l₁ l₂ : list α} : l₁ <:+ l₂ → l₁ <:+: l₂ :=
λ⟨t, h⟩, ⟨t, [], by simp only [h, append_nil]⟩
@[refl] theorem infix_refl (l : list α) : l <:+: l := infix_of_prefix $ prefix_refl l
theorem nil_infix (l : list α) : [] <:+: l := infix_of_prefix $ nil_prefix l
theorem infix_cons {L₁ L₂ : list α} {x : α} : L₁ <:+: L₂ → L₁ <:+: x :: L₂ :=
λ⟨LP, LS, H⟩, ⟨x :: LP, LS, H ▸ rfl⟩
@[trans] theorem is_prefix.trans : ∀ {l₁ l₂ l₃ : list α}, l₁ <+: l₂ → l₂ <+: l₃ → l₁ <+: l₃
| l ._ ._ ⟨r₁, rfl⟩ ⟨r₂, rfl⟩ := ⟨r₁ ++ r₂, (append_assoc _ _ _).symm⟩
@[trans] theorem is_suffix.trans : ∀ {l₁ l₂ l₃ : list α}, l₁ <:+ l₂ → l₂ <:+ l₃ → l₁ <:+ l₃
| l ._ ._ ⟨l₁, rfl⟩ ⟨l₂, rfl⟩ := ⟨l₂ ++ l₁, append_assoc _ _ _⟩
@[trans] theorem is_infix.trans : ∀ {l₁ l₂ l₃ : list α}, l₁ <:+: l₂ → l₂ <:+: l₃ → l₁ <:+: l₃
| l ._ ._ ⟨l₁, r₁, rfl⟩ ⟨l₂, r₂, rfl⟩ := ⟨l₂ ++ l₁, r₁ ++ r₂, by simp only [append_assoc]⟩
theorem sublist_of_infix {l₁ l₂ : list α} : l₁ <:+: l₂ → l₁ <+ l₂ :=
λ⟨s, t, h⟩, by rw [← h]; exact (sublist_append_right _ _).trans (sublist_append_left _ _)
theorem sublist_of_prefix {l₁ l₂ : list α} : l₁ <+: l₂ → l₁ <+ l₂ :=
sublist_of_infix ∘ infix_of_prefix
theorem sublist_of_suffix {l₁ l₂ : list α} : l₁ <:+ l₂ → l₁ <+ l₂ :=
sublist_of_infix ∘ infix_of_suffix
theorem reverse_suffix {l₁ l₂ : list α} : reverse l₁ <:+ reverse l₂ ↔ l₁ <+: l₂ :=
⟨λ ⟨r, e⟩, ⟨reverse r,
by rw [← reverse_reverse l₁, ← reverse_append, e, reverse_reverse]⟩,
λ ⟨r, e⟩, ⟨reverse r, by rw [← reverse_append, e]⟩⟩
theorem reverse_prefix {l₁ l₂ : list α} : reverse l₁ <+: reverse l₂ ↔ l₁ <:+ l₂ :=
by rw ← reverse_suffix; simp only [reverse_reverse]
theorem length_le_of_infix {l₁ l₂ : list α} (s : l₁ <:+: l₂) : length l₁ ≤ length l₂ :=
length_le_of_sublist $ sublist_of_infix s
theorem eq_nil_of_infix_nil {l : list α} (s : l <:+: []) : l = [] :=
eq_nil_of_sublist_nil $ sublist_of_infix s
@[simp] theorem eq_nil_iff_infix_nil {l : list α} : l <:+: [] ↔ l = [] :=
⟨eq_nil_of_infix_nil, λ h, h ▸ infix_refl _⟩
theorem eq_nil_of_prefix_nil {l : list α} (s : l <+: []) : l = [] :=
eq_nil_of_infix_nil $ infix_of_prefix s
@[simp] theorem eq_nil_iff_prefix_nil {l : list α} : l <+: [] ↔ l = [] :=
⟨eq_nil_of_prefix_nil, λ h, h ▸ prefix_refl _⟩
theorem eq_nil_of_suffix_nil {l : list α} (s : l <:+ []) : l = [] :=
eq_nil_of_infix_nil $ infix_of_suffix s
@[simp] theorem eq_nil_iff_suffix_nil {l : list α} : l <:+ [] ↔ l = [] :=
⟨eq_nil_of_suffix_nil, λ h, h ▸ suffix_refl _⟩
theorem infix_iff_prefix_suffix (l₁ l₂ : list α) : l₁ <:+: l₂ ↔ ∃ t, l₁ <+: t ∧ t <:+ l₂ :=
⟨λ⟨s, t, e⟩, ⟨l₁ ++ t, ⟨_, rfl⟩, by rw [← e, append_assoc]; exact ⟨_, rfl⟩⟩,
λ⟨._, ⟨t, rfl⟩, ⟨s, e⟩⟩, ⟨s, t, by rw append_assoc; exact e⟩⟩
theorem eq_of_infix_of_length_eq {l₁ l₂ : list α} (s : l₁ <:+: l₂) :
length l₁ = length l₂ → l₁ = l₂ :=
eq_of_sublist_of_length_eq $ sublist_of_infix s
theorem eq_of_prefix_of_length_eq {l₁ l₂ : list α} (s : l₁ <+: l₂) :
length l₁ = length l₂ → l₁ = l₂ :=
eq_of_sublist_of_length_eq $ sublist_of_prefix s
theorem eq_of_suffix_of_length_eq {l₁ l₂ : list α} (s : l₁ <:+ l₂) :
length l₁ = length l₂ → l₁ = l₂ :=
eq_of_sublist_of_length_eq $ sublist_of_suffix s
theorem prefix_of_prefix_length_le : ∀ {l₁ l₂ l₃ : list α},
l₁ <+: l₃ → l₂ <+: l₃ → length l₁ ≤ length l₂ → l₁ <+: l₂
| [] l₂ l₃ h₁ h₂ _ := nil_prefix _
| (a::l₁) (b::l₂) _ ⟨r₁, rfl⟩ ⟨r₂, e⟩ ll := begin
injection e with _ e', subst b,
rcases prefix_of_prefix_length_le ⟨_, rfl⟩ ⟨_, e'⟩
(le_of_succ_le_succ ll) with ⟨r₃, rfl⟩,
exact ⟨r₃, rfl⟩
end
theorem prefix_or_prefix_of_prefix {l₁ l₂ l₃ : list α}
(h₁ : l₁ <+: l₃) (h₂ : l₂ <+: l₃) : l₁ <+: l₂ ∨ l₂ <+: l₁ :=
(le_total (length l₁) (length l₂)).imp
(prefix_of_prefix_length_le h₁ h₂)
(prefix_of_prefix_length_le h₂ h₁)
theorem suffix_of_suffix_length_le {l₁ l₂ l₃ : list α}
(h₁ : l₁ <:+ l₃) (h₂ : l₂ <:+ l₃) (ll : length l₁ ≤ length l₂) : l₁ <:+ l₂ :=
reverse_prefix.1 $ prefix_of_prefix_length_le
(reverse_prefix.2 h₁) (reverse_prefix.2 h₂) (by simp [ll])
theorem suffix_or_suffix_of_suffix {l₁ l₂ l₃ : list α}
(h₁ : l₁ <:+ l₃) (h₂ : l₂ <:+ l₃) : l₁ <:+ l₂ ∨ l₂ <:+ l₁ :=
(prefix_or_prefix_of_prefix (reverse_prefix.2 h₁) (reverse_prefix.2 h₂)).imp
reverse_prefix.1 reverse_prefix.1
theorem suffix_cons_iff {x : α} {l₁ l₂ : list α} :
l₁ <:+ x :: l₂ ↔ l₁ = x :: l₂ ∨ l₁ <:+ l₂ :=
begin
split,
{ rintro ⟨⟨hd, tl⟩, hl₃⟩,
{ exact or.inl hl₃ },
{ simp only [cons_append] at hl₃,
exact or.inr ⟨_, hl₃.2⟩ } },
{ rintro (rfl | hl₁),
{ exact (x :: l₂).suffix_refl },
{ exact hl₁.trans (l₂.suffix_cons _) } }
end
theorem infix_of_mem_join : ∀ {L : list (list α)} {l}, l ∈ L → l <:+: join L
| (_ :: L) l (or.inl rfl) := infix_append [] _ _
| (l' :: L) l (or.inr h) :=
is_infix.trans (infix_of_mem_join h) $ infix_of_suffix $ suffix_append _ _
theorem prefix_append_right_inj {l₁ l₂ : list α} (l) : l ++ l₁ <+: l ++ l₂ ↔ l₁ <+: l₂ :=
exists_congr $ λ r, by rw [append_assoc, append_right_inj]
theorem prefix_cons_inj {l₁ l₂ : list α} (a) : a :: l₁ <+: a :: l₂ ↔ l₁ <+: l₂ :=
prefix_append_right_inj [a]
theorem take_prefix (n) (l : list α) : take n l <+: l := ⟨_, take_append_drop _ _⟩
theorem drop_suffix (n) (l : list α) : drop n l <:+ l := ⟨_, take_append_drop _ _⟩
theorem tail_suffix (l : list α) : tail l <:+ l := by rw ← drop_one; apply drop_suffix
lemma tail_sublist (l : list α) : l.tail <+ l := sublist_of_suffix (tail_suffix l)
theorem tail_subset (l : list α) : tail l ⊆ l := (tail_sublist l).subset
theorem prefix_iff_eq_append {l₁ l₂ : list α} : l₁ <+: l₂ ↔ l₁ ++ drop (length l₁) l₂ = l₂ :=
⟨by rintros ⟨r, rfl⟩; rw drop_left, λ e, ⟨_, e⟩⟩
theorem suffix_iff_eq_append {l₁ l₂ : list α} :
l₁ <:+ l₂ ↔ take (length l₂ - length l₁) l₂ ++ l₁ = l₂ :=
⟨by rintros ⟨r, rfl⟩; simp only [length_append, nat.add_sub_cancel, take_left], λ e, ⟨_, e⟩⟩
theorem prefix_iff_eq_take {l₁ l₂ : list α} : l₁ <+: l₂ ↔ l₁ = take (length l₁) l₂ :=
⟨λ h, append_right_cancel $
(prefix_iff_eq_append.1 h).trans (take_append_drop _ _).symm,
λ e, e.symm ▸ take_prefix _ _⟩
theorem suffix_iff_eq_drop {l₁ l₂ : list α} : l₁ <:+ l₂ ↔ l₁ = drop (length l₂ - length l₁) l₂ :=
⟨λ h, append_left_cancel $
(suffix_iff_eq_append.1 h).trans (take_append_drop _ _).symm,
λ e, e.symm ▸ drop_suffix _ _⟩
instance decidable_prefix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <+: l₂)
| [] l₂ := is_true ⟨l₂, rfl⟩
| (a::l₁) [] := is_false $ λ ⟨t, te⟩, list.no_confusion te
| (a::l₁) (b::l₂) :=
if h : a = b then
@decidable_of_iff _ _ (by rw [← h, prefix_cons_inj])
(decidable_prefix l₁ l₂)
else
is_false $ λ ⟨t, te⟩, h $ by injection te
-- Alternatively, use mem_tails
instance decidable_suffix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <:+ l₂)
| [] l₂ := is_true ⟨l₂, append_nil _⟩
| (a::l₁) [] := is_false $ mt (length_le_of_sublist ∘ sublist_of_suffix) dec_trivial
| l₁ l₂ := let len1 := length l₁, len2 := length l₂ in
if hl : len1 ≤ len2 then
decidable_of_iff' (l₁ = drop (len2-len1) l₂) suffix_iff_eq_drop
else is_false $ λ h, hl $ length_le_of_sublist $ sublist_of_suffix h
lemma prefix_take_le_iff {L : list (list (option α))} {m n : ℕ} (hm : m < L.length) :
(take m L) <+: (take n L) ↔ m ≤ n :=
begin
simp only [prefix_iff_eq_take, length_take],
induction m with m IH generalizing L n,
{ simp only [min_eq_left, eq_self_iff_true, nat.zero_le, take] },
{ cases n,
{ simp only [nat.nat_zero_eq_zero, nonpos_iff_eq_zero, take, take_nil],
split,
{ cases L,
{ exact absurd hm (not_lt_of_le m.succ.zero_le) },
{ simp only [forall_prop_of_false, not_false_iff, take] } },
{ intro h,
contradiction } },
{ cases L with l ls,
{ exact absurd hm (not_lt_of_le m.succ.zero_le) },
{ simp only [length] at hm,
specialize @IH ls n (nat.lt_of_succ_lt_succ hm),
simp only [le_of_lt (nat.lt_of_succ_lt_succ hm), min_eq_left] at IH,
simp only [le_of_lt hm, IH, true_and, min_eq_left, eq_self_iff_true, length, take],
exact ⟨nat.succ_le_succ, nat.le_of_succ_le_succ⟩ } } },
end
lemma cons_prefix_iff {l l' : list α} {x y : α} :
x :: l <+: y :: l' ↔ x = y ∧ l <+: l' :=
begin
split,
{ rintro ⟨L, hL⟩,
simp only [cons_append] at hL,
exact ⟨hL.left, ⟨L, hL.right⟩⟩ },
{ rintro ⟨rfl, h⟩,
rwa [prefix_cons_inj] },
end
lemma map_prefix {l l' : list α} (f : α → β) (h : l <+: l') :
l.map f <+: l'.map f :=
begin
induction l with hd tl hl generalizing l',
{ simp only [nil_prefix, map_nil] },
{ cases l' with hd' tl',
{ simpa only using eq_nil_of_prefix_nil h },
{ rw cons_prefix_iff at h,
simp only [h, prefix_cons_inj, hl, map] } },
end
lemma is_prefix.filter_map {l l' : list α} (h : l <+: l') (f : α → option β) :
l.filter_map f <+: l'.filter_map f :=
begin
induction l with hd tl hl generalizing l',
{ simp only [nil_prefix, filter_map_nil] },
{ cases l' with hd' tl',
{ simpa only using eq_nil_of_prefix_nil h },
{ rw cons_prefix_iff at h,
rw [←@singleton_append _ hd _, ←@singleton_append _ hd' _, filter_map_append,
filter_map_append, h.left, prefix_append_right_inj],
exact hl h.right } },
end
lemma is_prefix.reduce_option {l l' : list (option α)} (h : l <+: l') :
l.reduce_option <+: l'.reduce_option :=
h.filter_map id
@[simp] theorem mem_inits : ∀ (s t : list α), s ∈ inits t ↔ s <+: t
| s [] := suffices s = nil ↔ s <+: nil, by simpa only [inits, mem_singleton],
⟨λh, h.symm ▸ prefix_refl [], eq_nil_of_prefix_nil⟩
| s (a::t) :=
suffices (s = nil ∨ ∃ l ∈ inits t, a :: l = s) ↔ s <+: a :: t, by simpa,
⟨λo, match s, o with
| ._, or.inl rfl := ⟨_, rfl⟩
| s, or.inr ⟨r, hr, hs⟩ := let ⟨s, ht⟩ := (mem_inits _ _).1 hr in
by rw [← hs, ← ht]; exact ⟨s, rfl⟩
end, λmi, match s, mi with
| [], ⟨._, rfl⟩ := or.inl rfl
| (b::s), ⟨r, hr⟩ := list.no_confusion hr $ λba (st : s++r = t), or.inr $
by rw ba; exact ⟨_, (mem_inits _ _).2 ⟨_, st⟩, rfl⟩
end⟩
@[simp] theorem mem_tails : ∀ (s t : list α), s ∈ tails t ↔ s <:+ t
| s [] := by simp only [tails, mem_singleton];
exact ⟨λh, by rw h; exact suffix_refl [], eq_nil_of_suffix_nil⟩
| s (a::t) := by simp only [tails, mem_cons_iff, mem_tails s t];
exact show s = a :: t ∨ s <:+ t ↔ s <:+ a :: t, from
⟨λo, match s, t, o with
| ._, t, or.inl rfl := suffix_refl _
| s, ._, or.inr ⟨l, rfl⟩ := ⟨a::l, rfl⟩
end, λe, match s, t, e with
| ._, t, ⟨[], rfl⟩ := or.inl rfl
| s, t, ⟨b::l, he⟩ := list.no_confusion he (λab lt, or.inr ⟨l, lt⟩)
end⟩
lemma inits_cons (a : α) (l : list α) : inits (a :: l) = [] :: l.inits.map (λ t, a :: t) :=
by simp
lemma tails_cons (a : α) (l : list α) : tails (a :: l) = (a :: l) :: l.tails :=
by simp
@[simp]
lemma inits_append : ∀ (s t : list α), inits (s ++ t) = s.inits ++ t.inits.tail.map (λ l, s ++ l)
| [] [] := by simp
| [] (a::t) := by simp
| (a::s) t := by simp [inits_append s t]
@[simp]
lemma tails_append : ∀ (s t : list α), tails (s ++ t) = s.tails.map (λ l, l ++ t) ++ t.tails.tail
| [] [] := by simp
| [] (a::t) := by simp
| (a::s) t := by simp [tails_append s t]
-- the lemma names `inits_eq_tails` and `tails_eq_inits` are like `sublists_eq_sublists'`
lemma inits_eq_tails :
∀ (l : list α), l.inits = (reverse $ map reverse $ tails $ reverse l)
| [] := by simp
| (a :: l) := by simp [inits_eq_tails l, map_eq_map_iff]
lemma tails_eq_inits :
∀ (l : list α), l.tails = (reverse $ map reverse $ inits $ reverse l)
| [] := by simp
| (a :: l) := by simp [tails_eq_inits l, append_left_inj]
lemma inits_reverse (l : list α) : inits (reverse l) = reverse (map reverse l.tails) :=
by { rw tails_eq_inits l, simp [reverse_involutive.comp_self], }
lemma tails_reverse (l : list α) : tails (reverse l) = reverse (map reverse l.inits) :=
by { rw inits_eq_tails l, simp [reverse_involutive.comp_self], }
lemma map_reverse_inits (l : list α) : map reverse l.inits = (reverse $ tails $ reverse l) :=
by { rw inits_eq_tails l, simp [reverse_involutive.comp_self], }
lemma map_reverse_tails (l : list α) : map reverse l.tails = (reverse $ inits $ reverse l) :=
by { rw tails_eq_inits l, simp [reverse_involutive.comp_self], }
instance decidable_infix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <:+: l₂)
| [] l₂ := is_true ⟨[], l₂, rfl⟩
| (a::l₁) [] := is_false $ λ⟨s, t, te⟩, absurd te $ append_ne_nil_of_ne_nil_left _ _ $
append_ne_nil_of_ne_nil_right _ _ $ λh, list.no_confusion h
| l₁ l₂ := decidable_of_decidable_of_iff (list.decidable_bex (λt, l₁ <+: t) (tails l₂)) $
by refine (exists_congr (λt, _)).trans (infix_iff_prefix_suffix _ _).symm;
exact ⟨λ⟨h1, h2⟩, ⟨h2, (mem_tails _ _).1 h1⟩, λ⟨h2, h1⟩, ⟨(mem_tails _ _).2 h1, h2⟩⟩
/-! ### permutations -/
section permutations
theorem permutations_aux2_fst (t : α) (ts : list α) (r : list β) : ∀ (ys : list α) (f : list α → β),
(permutations_aux2 t ts r ys f).1 = ys ++ ts
| [] f := rfl
| (y::ys) f := match _, permutations_aux2_fst ys _ : ∀ o : list α × list β, o.1 = ys ++ ts →
(permutations_aux2._match_1 t y f o).1 = y :: ys ++ ts with
| ⟨_, zs⟩, rfl := rfl
end
@[simp] theorem permutations_aux2_snd_nil (t : α) (ts : list α) (r : list β) (f : list α → β) :
(permutations_aux2 t ts r [] f).2 = r := rfl
@[simp] theorem permutations_aux2_snd_cons (t : α) (ts : list α) (r : list β) (y : α) (ys : list α)
(f : list α → β) :
(permutations_aux2 t ts r (y::ys) f).2 = f (t :: y :: ys ++ ts) ::
(permutations_aux2 t ts r ys (λx : list α, f (y::x))).2 :=
match _, permutations_aux2_fst t ts r _ _ : ∀ o : list α × list β, o.1 = ys ++ ts →
(permutations_aux2._match_1 t y f o).2 = f (t :: y :: ys ++ ts) :: o.2 with
| ⟨_, zs⟩, rfl := rfl
end
/-- The `r` argument to `permutations_aux2` is the same as appending. -/
theorem permutations_aux2_append (t : α) (ts : list α) (r : list β) (ys : list α) (f : list α → β) :
(permutations_aux2 t ts nil ys f).2 ++ r = (permutations_aux2 t ts r ys f).2 :=
by induction ys generalizing f; simp *
/-- The `ts` argument to `permutations_aux2` can be folded into the `f` argument. -/
theorem permutations_aux2_comp_append {t : α} {ts ys : list α} {r : list β} (f : list α → β) :
(permutations_aux2 t [] r ys $ λ x, f (x ++ ts)).2 = (permutations_aux2 t ts r ys f).2 :=
begin
induction ys generalizing f,
{ simp },
{ simp [ys_ih (λ xs, f (ys_hd :: xs))] },
end
theorem map_permutations_aux2' {α β α' β'} (g : α → α') (g' : β → β')
(t : α) (ts ys : list α) (r : list β) (f : list α → β) (f' : list α' → β')
(H : ∀ a, g' (f a) = f' (map g a)) :
map g' (permutations_aux2 t ts r ys f).2 =
(permutations_aux2 (g t) (map g ts) (map g' r) (map g ys) f').2 :=
begin
induction ys generalizing f f'; simp *,
apply ys_ih, simp [H],
end
/-- The `f` argument to `permutations_aux2` when `r = []` can be eliminated. -/
theorem map_permutations_aux2 (t : α) (ts : list α) (ys : list α) (f : list α → β) :
(permutations_aux2 t ts [] ys id).2.map f = (permutations_aux2 t ts [] ys f).2 :=
begin
convert map_permutations_aux2' id _ _ _ _ _ _ _ _; simp only [map_id, id.def],
exact (λ _, rfl)
end
/-- An expository lemma to show how all of `ts`, `r`, and `f` can be eliminated from
`permutations_aux2`.
`(permutations_aux2 t [] [] ys id).2`, which appears on the RHS, is a list whose elements are
produced by inserting `t` into every non-terminal position of `ys` in order. As an example:
```lean
#eval permutations_aux2 1 [] [] [2, 3, 4] id
-- [[1, 2, 3, 4], [2, 1, 3, 4], [2, 3, 1, 4]]
```
-/
lemma permutations_aux2_snd_eq (t : α) (ts : list α) (r : list β) (ys : list α) (f : list α → β) :
(permutations_aux2 t ts r ys f).2 =
(permutations_aux2 t [] [] ys id).2.map (λ x, f (x ++ ts)) ++ r :=
by rw [←permutations_aux2_append, map_permutations_aux2, permutations_aux2_comp_append]
theorem map_map_permutations_aux2 {α α'} (g : α → α') (t : α) (ts ys : list α) :
map (map g) (permutations_aux2 t ts [] ys id).2 =
(permutations_aux2 (g t) (map g ts) [] (map g ys) id).2 :=
map_permutations_aux2' _ _ _ _ _ _ _ _ (λ _, rfl)
theorem mem_permutations_aux2 {t : α} {ts : list α} {ys : list α} {l l' : list α} :
l' ∈ (permutations_aux2 t ts [] ys (append l)).2 ↔
∃ l₁ l₂, l₂ ≠ [] ∧ ys = l₁ ++ l₂ ∧ l' = l ++ l₁ ++ t :: l₂ ++ ts :=
begin
induction ys with y ys ih generalizing l,
{ simp {contextual := tt} },
{ rw [permutations_aux2_snd_cons, show (λ (x : list α), l ++ y :: x) = append (l ++ [y]),
by funext; simp, mem_cons_iff, ih], split; intro h,
{ rcases h with e | ⟨l₁, l₂, l0, ye, _⟩,
{ subst l', exact ⟨[], y::ys, by simp⟩ },
{ substs l' ys, exact ⟨y::l₁, l₂, l0, by simp⟩ } },
{ rcases h with ⟨_ | ⟨y', l₁⟩, l₂, l0, ye, rfl⟩,
{ simp [ye] },
{ simp at ye, rcases ye with ⟨rfl, rfl⟩,
exact or.inr ⟨l₁, l₂, l0, by simp⟩ } } }
end
theorem mem_permutations_aux2' {t : α} {ts : list α} {ys : list α} {l : list α} :
l ∈ (permutations_aux2 t ts [] ys id).2 ↔
∃ l₁ l₂, l₂ ≠ [] ∧ ys = l₁ ++ l₂ ∧ l = l₁ ++ t :: l₂ ++ ts :=
by rw [show @id (list α) = append nil, by funext; refl]; apply mem_permutations_aux2
theorem length_permutations_aux2 (t : α) (ts : list α) (ys : list α) (f : list α → β) :
length (permutations_aux2 t ts [] ys f).2 = length ys :=
by induction ys generalizing f; simp *
theorem foldr_permutations_aux2 (t : α) (ts : list α) (r L : list (list α)) :
foldr (λy r, (permutations_aux2 t ts r y id).2) r L =
L.bind (λ y, (permutations_aux2 t ts [] y id).2) ++ r :=
by induction L with l L ih; [refl, {simp [ih], rw ← permutations_aux2_append}]
theorem mem_foldr_permutations_aux2 {t : α} {ts : list α} {r L : list (list α)} {l' : list α} :
l' ∈ foldr (λy r, (permutations_aux2 t ts r y id).2) r L ↔ l' ∈ r ∨
∃ l₁ l₂, l₁ ++ l₂ ∈ L ∧ l₂ ≠ [] ∧ l' = l₁ ++ t :: l₂ ++ ts :=
have (∃ (a : list α), a ∈ L ∧
∃ (l₁ l₂ : list α), ¬l₂ = nil ∧ a = l₁ ++ l₂ ∧ l' = l₁ ++ t :: (l₂ ++ ts)) ↔
∃ (l₁ l₂ : list α), ¬l₂ = nil ∧ l₁ ++ l₂ ∈ L ∧ l' = l₁ ++ t :: (l₂ ++ ts),
from ⟨λ ⟨a, aL, l₁, l₂, l0, e, h⟩, ⟨l₁, l₂, l0, e ▸ aL, h⟩,
λ ⟨l₁, l₂, l0, aL, h⟩, ⟨_, aL, l₁, l₂, l0, rfl, h⟩⟩,
by rw foldr_permutations_aux2; simp [mem_permutations_aux2', this,
or.comm, or.left_comm, or.assoc, and.comm, and.left_comm, and.assoc]
theorem length_foldr_permutations_aux2 (t : α) (ts : list α) (r L : list (list α)) :
length (foldr (λy r, (permutations_aux2 t ts r y id).2) r L) = sum (map length L) + length r :=
by simp [foldr_permutations_aux2, (∘), length_permutations_aux2]
theorem length_foldr_permutations_aux2' (t : α) (ts : list α) (r L : list (list α))
(n) (H : ∀ l ∈ L, length l = n) :
length (foldr (λy r, (permutations_aux2 t ts r y id).2) r L) = n * length L + length r :=
begin
rw [length_foldr_permutations_aux2, (_ : sum (map length L) = n * length L)],
induction L with l L ih, {simp},
have sum_map : sum (map length L) = n * length L :=
ih (λ l m, H l (mem_cons_of_mem _ m)),
have length_l : length l = n := H _ (mem_cons_self _ _),
simp [sum_map, length_l, mul_add, add_comm]
end
@[simp] theorem permutations_aux_nil (is : list α) : permutations_aux [] is = [] :=
by rw [permutations_aux, permutations_aux.rec]
@[simp] theorem permutations_aux_cons (t : α) (ts is : list α) :
permutations_aux (t :: ts) is = foldr (λy r, (permutations_aux2 t ts r y id).2)
(permutations_aux ts (t::is)) (permutations is) :=
by rw [permutations_aux, permutations_aux.rec]; refl
theorem map_permutations_aux (f : α → β) : ∀ (ts is : list α),
map (map f) (permutations_aux ts is) = permutations_aux (map f ts) (map f is) :=
begin
refine permutations_aux.rec (by simp) _,
introv IH1 IH2, rw map at IH2,
simp only [foldr_permutations_aux2, map_append, map, map_map_permutations_aux2, permutations,
bind_map, IH1, append_assoc, permutations_aux_cons, cons_bind, ← IH2, map_bind],
end
theorem map_permutations (f : α → β) (ts : list α) :
map (map f) (permutations ts) = permutations (map f ts) :=
by rw [permutations, permutations, map, map_permutations_aux, map]
end permutations
/-! ### insert -/
section insert
variable [decidable_eq α]
@[simp] theorem insert_nil (a : α) : insert a nil = [a] := rfl
theorem insert.def (a : α) (l : list α) : insert a l = if a ∈ l then l else a :: l := rfl
@[simp, priority 980]
theorem insert_of_mem {a : α} {l : list α} (h : a ∈ l) : insert a l = l :=
by simp only [insert.def, if_pos h]
@[simp, priority 970]
theorem insert_of_not_mem {a : α} {l : list α} (h : a ∉ l) : insert a l = a :: l :=
by simp only [insert.def, if_neg h]; split; refl
@[simp] theorem mem_insert_iff {a b : α} {l : list α} : a ∈ insert b l ↔ a = b ∨ a ∈ l :=
begin
by_cases h' : b ∈ l,
{ simp only [insert_of_mem h'],
apply (or_iff_right_of_imp _).symm,
exact λ e, e.symm ▸ h' },
simp only [insert_of_not_mem h', mem_cons_iff]
end
@[simp] theorem suffix_insert (a : α) (l : list α) : l <:+ insert a l :=
by by_cases a ∈ l; [simp only [insert_of_mem h], simp only [insert_of_not_mem h, suffix_cons]]
@[simp] theorem mem_insert_self (a : α) (l : list α) : a ∈ insert a l :=
mem_insert_iff.2 (or.inl rfl)
theorem mem_insert_of_mem {a b : α} {l : list α} (h : a ∈ l) : a ∈ insert b l :=
mem_insert_iff.2 (or.inr h)
theorem eq_or_mem_of_mem_insert {a b : α} {l : list α} (h : a ∈ insert b l) : a = b ∨ a ∈ l :=
mem_insert_iff.1 h
@[simp] theorem length_insert_of_mem {a : α} {l : list α} (h : a ∈ l) :
length (insert a l) = length l :=
by rw insert_of_mem h
@[simp] theorem length_insert_of_not_mem {a : α} {l : list α} (h : a ∉ l) :
length (insert a l) = length l + 1 :=
by rw insert_of_not_mem h; refl
end insert
/-! ### erasep -/
section erasep
variables {p : α → Prop} [decidable_pred p]
@[simp] theorem erasep_nil : [].erasep p = [] := rfl
theorem erasep_cons (a : α) (l : list α) :
(a :: l).erasep p = if p a then l else a :: l.erasep p := rfl
@[simp] theorem erasep_cons_of_pos {a : α} {l : list α} (h : p a) : (a :: l).erasep p = l :=
by simp [erasep_cons, h]
@[simp] theorem erasep_cons_of_neg {a : α} {l : list α} (h : ¬ p a) :
(a::l).erasep p = a :: l.erasep p :=
by simp [erasep_cons, h]
theorem erasep_of_forall_not {l : list α}
(h : ∀ a ∈ l, ¬ p a) : l.erasep p = l :=
by induction l with _ _ ih; [refl,
simp [h _ (or.inl rfl), ih (forall_mem_of_forall_mem_cons h)]]
theorem exists_of_erasep {l : list α} {a} (al : a ∈ l) (pa : p a) :
∃ a l₁ l₂, (∀ b ∈ l₁, ¬ p b) ∧ p a ∧ l = l₁ ++ a :: l₂ ∧ l.erasep p = l₁ ++ l₂ :=
begin
induction l with b l IH, {cases al},
by_cases pb : p b,
{ exact ⟨b, [], l, forall_mem_nil _, pb, by simp [pb]⟩ },
{ rcases al with rfl | al, {exact pb.elim pa},
rcases IH al with ⟨c, l₁, l₂, h₁, h₂, h₃, h₄⟩,
exact ⟨c, b::l₁, l₂, forall_mem_cons.2 ⟨pb, h₁⟩,
h₂, by rw h₃; refl, by simp [pb, h₄]⟩ }
end
theorem exists_or_eq_self_of_erasep (p : α → Prop) [decidable_pred p] (l : list α) :
l.erasep p = l ∨ ∃ a l₁ l₂, (∀ b ∈ l₁, ¬ p b) ∧ p a ∧ l = l₁ ++ a :: l₂ ∧ l.erasep p = l₁ ++ l₂ :=
begin
by_cases h : ∃ a ∈ l, p a,
{ rcases h with ⟨a, ha, pa⟩,
exact or.inr (exists_of_erasep ha pa) },
{ simp at h, exact or.inl (erasep_of_forall_not h) }
end
@[simp] theorem length_erasep_of_mem {l : list α} {a} (al : a ∈ l) (pa : p a) :
length (l.erasep p) = pred (length l) :=
by rcases exists_of_erasep al pa with ⟨_, l₁, l₂, _, _, e₁, e₂⟩;
rw e₂; simp [-add_comm, e₁]; refl
theorem erasep_append_left {a : α} (pa : p a) :
∀ {l₁ : list α} (l₂), a ∈ l₁ → (l₁++l₂).erasep p = l₁.erasep p ++ l₂
| (x::xs) l₂ h := begin
by_cases h' : p x; simp [h'],
rw erasep_append_left l₂ (mem_of_ne_of_mem (mt _ h') h),
rintro rfl, exact pa
end
theorem erasep_append_right :
∀ {l₁ : list α} (l₂), (∀ b ∈ l₁, ¬ p b) → (l₁++l₂).erasep p = l₁ ++ l₂.erasep p
| [] l₂ h := rfl
| (x::xs) l₂ h := by simp [(forall_mem_cons.1 h).1,
erasep_append_right _ (forall_mem_cons.1 h).2]
theorem erasep_sublist (l : list α) : l.erasep p <+ l :=
by rcases exists_or_eq_self_of_erasep p l with h | ⟨c, l₁, l₂, h₁, h₂, h₃, h₄⟩;
[rw h, {rw [h₄, h₃], simp}]
theorem erasep_subset (l : list α) : l.erasep p ⊆ l :=
(erasep_sublist l).subset
theorem sublist.erasep {l₁ l₂ : list α} (s : l₁ <+ l₂) : l₁.erasep p <+ l₂.erasep p :=
begin
induction s,
case list.sublist.slnil { refl },
case list.sublist.cons : l₁ l₂ a s IH {
by_cases h : p a; simp [h],
exacts [IH.trans (erasep_sublist _), IH.cons _ _ _] },
case list.sublist.cons2 : l₁ l₂ a s IH {
by_cases h : p a; simp [h],
exacts [s, IH.cons2 _ _ _] }
end
theorem mem_of_mem_erasep {a : α} {l : list α} : a ∈ l.erasep p → a ∈ l :=
@erasep_subset _ _ _ _ _
@[simp] theorem mem_erasep_of_neg {a : α} {l : list α} (pa : ¬ p a) : a ∈ l.erasep p ↔ a ∈ l :=
⟨mem_of_mem_erasep, λ al, begin
rcases exists_or_eq_self_of_erasep p l with h | ⟨c, l₁, l₂, h₁, h₂, h₃, h₄⟩,
{ rwa h },
{ rw h₄, rw h₃ at al,
have : a ≠ c, {rintro rfl, exact pa.elim h₂},
simpa [this] using al }
end⟩
theorem erasep_map (f : β → α) :
∀ (l : list β), (map f l).erasep p = map f (l.erasep (p ∘ f))
| [] := rfl
| (b::l) := by by_cases p (f b); simp [h, erasep_map l]
@[simp] theorem extractp_eq_find_erasep :
∀ l : list α, extractp p l = (find p l, erasep p l)
| [] := rfl
| (a::l) := by by_cases pa : p a; simp [extractp, pa, extractp_eq_find_erasep l]
end erasep
/-! ### erase -/
section erase
variable [decidable_eq α]
@[simp] theorem erase_nil (a : α) : [].erase a = [] := rfl
theorem erase_cons (a b : α) (l : list α) :
(b :: l).erase a = if b = a then l else b :: l.erase a := rfl
@[simp] theorem erase_cons_head (a : α) (l : list α) : (a :: l).erase a = l :=
by simp only [erase_cons, if_pos rfl]
@[simp] theorem erase_cons_tail {a b : α} (l : list α) (h : b ≠ a) :
(b::l).erase a = b :: l.erase a :=
by simp only [erase_cons, if_neg h]; split; refl
theorem erase_eq_erasep (a : α) (l : list α) : l.erase a = l.erasep (eq a) :=
by { induction l with b l, {refl},
by_cases a = b; [simp [h], simp [h, ne.symm h, *]] }
@[simp, priority 980]
theorem erase_of_not_mem {a : α} {l : list α} (h : a ∉ l) : l.erase a = l :=
by rw [erase_eq_erasep, erasep_of_forall_not]; rintro b h' rfl; exact h h'
theorem exists_erase_eq {a : α} {l : list α} (h : a ∈ l) :
∃ l₁ l₂, a ∉ l₁ ∧ l = l₁ ++ a :: l₂ ∧ l.erase a = l₁ ++ l₂ :=
by rcases exists_of_erasep h rfl with ⟨_, l₁, l₂, h₁, rfl, h₂, h₃⟩;
rw erase_eq_erasep; exact ⟨l₁, l₂, λ h, h₁ _ h rfl, h₂, h₃⟩
@[simp] theorem length_erase_of_mem {a : α} {l : list α} (h : a ∈ l) :
length (l.erase a) = pred (length l) :=
by rw erase_eq_erasep; exact length_erasep_of_mem h rfl
theorem erase_append_left {a : α} {l₁ : list α} (l₂) (h : a ∈ l₁) :
(l₁++l₂).erase a = l₁.erase a ++ l₂ :=
by simp [erase_eq_erasep]; exact erasep_append_left (by refl) l₂ h
theorem erase_append_right {a : α} {l₁ : list α} (l₂) (h : a ∉ l₁) :
(l₁++l₂).erase a = l₁ ++ l₂.erase a :=
by rw [erase_eq_erasep, erase_eq_erasep, erasep_append_right];
rintro b h' rfl; exact h h'
theorem erase_sublist (a : α) (l : list α) : l.erase a <+ l :=
by rw erase_eq_erasep; apply erasep_sublist
theorem erase_subset (a : α) (l : list α) : l.erase a ⊆ l :=
(erase_sublist a l).subset
theorem sublist.erase (a : α) {l₁ l₂ : list α} (h : l₁ <+ l₂) : l₁.erase a <+ l₂.erase a :=
by simp [erase_eq_erasep]; exact sublist.erasep h
theorem mem_of_mem_erase {a b : α} {l : list α} : a ∈ l.erase b → a ∈ l :=
@erase_subset _ _ _ _ _
@[simp] theorem mem_erase_of_ne {a b : α} {l : list α} (ab : a ≠ b) : a ∈ l.erase b ↔ a ∈ l :=
by rw erase_eq_erasep; exact mem_erasep_of_neg ab.symm
theorem erase_comm (a b : α) (l : list α) : (l.erase a).erase b = (l.erase b).erase a :=
if ab : a = b then by rw ab else
if ha : a ∈ l then
if hb : b ∈ l then match l, l.erase a, exists_erase_eq ha, hb with
| ._, ._, ⟨l₁, l₂, ha', rfl, rfl⟩, hb :=
if h₁ : b ∈ l₁ then
by rw [erase_append_left _ h₁, erase_append_left _ h₁,
erase_append_right _ (mt mem_of_mem_erase ha'), erase_cons_head]
else
by rw [erase_append_right _ h₁, erase_append_right _ h₁, erase_append_right _ ha',
erase_cons_tail _ ab, erase_cons_head]
end
else by simp only [erase_of_not_mem hb, erase_of_not_mem (mt mem_of_mem_erase hb)]
else by simp only [erase_of_not_mem ha, erase_of_not_mem (mt mem_of_mem_erase ha)]
theorem map_erase [decidable_eq β] {f : α → β} (finj : injective f) {a : α}
(l : list α) : map f (l.erase a) = (map f l).erase (f a) :=
by rw [erase_eq_erasep, erase_eq_erasep, erasep_map]; congr;
ext b; simp [finj.eq_iff]
theorem map_foldl_erase [decidable_eq β] {f : α → β} (finj : injective f) {l₁ l₂ : list α} :
map f (foldl list.erase l₁ l₂) = foldl (λ l a, l.erase (f a)) (map f l₁) l₂ :=
by induction l₂ generalizing l₁; [refl,
simp only [foldl_cons, map_erase finj, *]]
@[simp] theorem count_erase_self (a : α) :
∀ (s : list α), count a (list.erase s a) = pred (count a s)
| [] := by simp
| (h :: t) :=
begin
rw erase_cons,
by_cases p : h = a,
{ rw [if_pos p, count_cons', if_pos p.symm], simp },
{ rw [if_neg p, count_cons', count_cons', if_neg (λ x : a = h, p x.symm), count_erase_self],
simp, }
end
@[simp] theorem count_erase_of_ne {a b : α} (ab : a ≠ b) :
∀ (s : list α), count a (list.erase s b) = count a s
| [] := by simp
| (x :: xs) :=
begin
rw erase_cons,
split_ifs with h,
{ rw [count_cons', h, if_neg ab], simp },
{ rw [count_cons', count_cons', count_erase_of_ne] }
end
end erase
/-! ### diff -/
section diff
variable [decidable_eq α]
@[simp] theorem diff_nil (l : list α) : l.diff [] = l := rfl
@[simp] theorem diff_cons (l₁ l₂ : list α) (a : α) : l₁.diff (a::l₂) = (l₁.erase a).diff l₂ :=
if h : a ∈ l₁ then by simp only [list.diff, if_pos h]
else by simp only [list.diff, if_neg h, erase_of_not_mem h]
lemma diff_cons_right (l₁ l₂ : list α) (a : α) : l₁.diff (a::l₂) = (l₁.diff l₂).erase a :=
begin
induction l₂ with b l₂ ih generalizing l₁ a,
{ simp_rw [diff_cons, diff_nil] },
{ rw [diff_cons, diff_cons, erase_comm, ← diff_cons, ih, ← diff_cons] }
end
lemma diff_erase (l₁ l₂ : list α) (a : α) : (l₁.diff l₂).erase a = (l₁.erase a).diff l₂ :=
by rw [← diff_cons_right, diff_cons]
@[simp] theorem nil_diff (l : list α) : [].diff l = [] :=
by induction l; [refl, simp only [*, diff_cons, erase_of_not_mem (not_mem_nil _)]]
theorem diff_eq_foldl : ∀ (l₁ l₂ : list α), l₁.diff l₂ = foldl list.erase l₁ l₂
| l₁ [] := rfl
| l₁ (a::l₂) := (diff_cons l₁ l₂ a).trans (diff_eq_foldl _ _)
@[simp] theorem diff_append (l₁ l₂ l₃ : list α) : l₁.diff (l₂ ++ l₃) = (l₁.diff l₂).diff l₃ :=
by simp only [diff_eq_foldl, foldl_append]
@[simp] theorem map_diff [decidable_eq β] {f : α → β} (finj : injective f) {l₁ l₂ : list α} :
map f (l₁.diff l₂) = (map f l₁).diff (map f l₂) :=
by simp only [diff_eq_foldl, foldl_map, map_foldl_erase finj]
theorem diff_sublist : ∀ l₁ l₂ : list α, l₁.diff l₂ <+ l₁
| l₁ [] := sublist.refl _
| l₁ (a::l₂) := calc l₁.diff (a :: l₂) = (l₁.erase a).diff l₂ : diff_cons _ _ _
... <+ l₁.erase a : diff_sublist _ _
... <+ l₁ : list.erase_sublist _ _
theorem diff_subset (l₁ l₂ : list α) : l₁.diff l₂ ⊆ l₁ :=
(diff_sublist _ _).subset
theorem mem_diff_of_mem {a : α} : ∀ {l₁ l₂ : list α}, a ∈ l₁ → a ∉ l₂ → a ∈ l₁.diff l₂
| l₁ [] h₁ h₂ := h₁
| l₁ (b::l₂) h₁ h₂ := by rw diff_cons; exact
mem_diff_of_mem ((mem_erase_of_ne (ne_of_not_mem_cons h₂)).2 h₁) (not_mem_of_not_mem_cons h₂)
theorem sublist.diff_right : ∀ {l₁ l₂ l₃: list α}, l₁ <+ l₂ → l₁.diff l₃ <+ l₂.diff l₃
| l₁ l₂ [] h := h
| l₁ l₂ (a::l₃) h := by simp only
[diff_cons, (h.erase _).diff_right]
theorem erase_diff_erase_sublist_of_sublist {a : α} : ∀ {l₁ l₂ : list α},
l₁ <+ l₂ → (l₂.erase a).diff (l₁.erase a) <+ l₂.diff l₁
| [] l₂ h := erase_sublist _ _
| (b::l₁) l₂ h := if heq : b = a then by simp only [heq, erase_cons_head, diff_cons]
else by simpa only [erase_cons_head, erase_cons_tail _ heq, diff_cons,
erase_comm a b l₂]
using erase_diff_erase_sublist_of_sublist (h.erase b)
end diff
/-! ### enum -/
theorem length_enum_from : ∀ n (l : list α), length (enum_from n l) = length l
| n [] := rfl
| n (a::l) := congr_arg nat.succ (length_enum_from _ _)
theorem length_enum : ∀ (l : list α), length (enum l) = length l := length_enum_from _
@[simp] theorem enum_from_nth : ∀ n (l : list α) m,
nth (enum_from n l) m = (λ a, (n + m, a)) <$> nth l m
| n [] m := rfl
| n (a :: l) 0 := rfl
| n (a :: l) (m+1) := (enum_from_nth (n+1) l m).trans $
by rw [add_right_comm]; refl
@[simp] theorem enum_nth : ∀ (l : list α) n,
nth (enum l) n = (λ a, (n, a)) <$> nth l n :=
by simp only [enum, enum_from_nth, zero_add]; intros; refl
@[simp] theorem enum_from_map_snd : ∀ n (l : list α),
map prod.snd (enum_from n l) = l
| n [] := rfl
| n (a :: l) := congr_arg (cons _) (enum_from_map_snd _ _)
@[simp] theorem enum_map_snd : ∀ (l : list α),
map prod.snd (enum l) = l := enum_from_map_snd _
theorem mem_enum_from {x : α} {i : ℕ} :
∀ {j : ℕ} (xs : list α), (i, x) ∈ xs.enum_from j → j ≤ i ∧ i < j + xs.length ∧ x ∈ xs
| j [] := by simp [enum_from]
| j (y :: ys) :=
suffices i = j ∧ x = y ∨ (i, x) ∈ enum_from (j + 1) ys →
j ≤ i ∧ i < j + (length ys + 1) ∧ (x = y ∨ x ∈ ys),
by simpa [enum_from, mem_enum_from ys],
begin
rintro (h|h),
{ refine ⟨le_of_eq h.1.symm,h.1 ▸ _,or.inl h.2⟩,
apply nat.lt_add_of_pos_right; simp },
{ obtain ⟨hji, hijlen, hmem⟩ := mem_enum_from _ h,
refine ⟨_, _, _⟩,
{ exact le_trans (nat.le_succ _) hji },
{ convert hijlen using 1, ac_refl },
{ simp [hmem] } }
end
/-! ### product -/
@[simp] theorem nil_product (l : list β) : product (@nil α) l = [] := rfl
@[simp] theorem product_cons (a : α) (l₁ : list α) (l₂ : list β)
: product (a::l₁) l₂ = map (λ b, (a, b)) l₂ ++ product l₁ l₂ := rfl
@[simp] theorem product_nil : ∀ (l : list α), product l (@nil β) = []
| [] := rfl
| (a::l) := by rw [product_cons, product_nil]; refl
@[simp] theorem mem_product {l₁ : list α} {l₂ : list β} {a : α} {b : β} :
(a, b) ∈ product l₁ l₂ ↔ a ∈ l₁ ∧ b ∈ l₂ :=
by simp only [product, mem_bind, mem_map, prod.ext_iff, exists_prop,
and.left_comm, exists_and_distrib_left, exists_eq_left, exists_eq_right]
theorem length_product (l₁ : list α) (l₂ : list β) :
length (product l₁ l₂) = length l₁ * length l₂ :=
by induction l₁ with x l₁ IH; [exact (zero_mul _).symm,
simp only [length, product_cons, length_append, IH,
right_distrib, one_mul, length_map, add_comm]]
/-! ### sigma -/
section
variable {σ : α → Type*}
@[simp] theorem nil_sigma (l : Π a, list (σ a)) : (@nil α).sigma l = [] := rfl
@[simp] theorem sigma_cons (a : α) (l₁ : list α) (l₂ : Π a, list (σ a))
: (a::l₁).sigma l₂ = map (sigma.mk a) (l₂ a) ++ l₁.sigma l₂ := rfl
@[simp] theorem sigma_nil : ∀ (l : list α), l.sigma (λ a, @nil (σ a)) = []
| [] := rfl
| (a::l) := by rw [sigma_cons, sigma_nil]; refl
@[simp] theorem mem_sigma {l₁ : list α} {l₂ : Π a, list (σ a)} {a : α} {b : σ a} :
sigma.mk a b ∈ l₁.sigma l₂ ↔ a ∈ l₁ ∧ b ∈ l₂ a :=
by simp only [list.sigma, mem_bind, mem_map, exists_prop, exists_and_distrib_left,
and.left_comm, exists_eq_left, heq_iff_eq, exists_eq_right]
theorem length_sigma (l₁ : list α) (l₂ : Π a, list (σ a)) :
length (l₁.sigma l₂) = (l₁.map (λ a, length (l₂ a))).sum :=
by induction l₁ with x l₁ IH; [refl,
simp only [map, sigma_cons, length_append, length_map, IH, sum_cons]]
end
/-! ### disjoint -/
section disjoint
theorem disjoint.symm {l₁ l₂ : list α} (d : disjoint l₁ l₂) : disjoint l₂ l₁
| a i₂ i₁ := d i₁ i₂
theorem disjoint_comm {l₁ l₂ : list α} : disjoint l₁ l₂ ↔ disjoint l₂ l₁ :=
⟨disjoint.symm, disjoint.symm⟩
theorem disjoint_left {l₁ l₂ : list α} : disjoint l₁ l₂ ↔ ∀ {a}, a ∈ l₁ → a ∉ l₂ := iff.rfl
theorem disjoint_right {l₁ l₂ : list α} : disjoint l₁ l₂ ↔ ∀ {a}, a ∈ l₂ → a ∉ l₁ :=
disjoint_comm
theorem disjoint_iff_ne {l₁ l₂ : list α} : disjoint l₁ l₂ ↔ ∀ a ∈ l₁, ∀ b ∈ l₂, a ≠ b :=
by simp only [disjoint_left, imp_not_comm, forall_eq']
theorem disjoint_of_subset_left {l₁ l₂ l : list α} (ss : l₁ ⊆ l) (d : disjoint l l₂) :
disjoint l₁ l₂
| x m₁ := d (ss m₁)
theorem disjoint_of_subset_right {l₁ l₂ l : list α} (ss : l₂ ⊆ l) (d : disjoint l₁ l) :
disjoint l₁ l₂
| x m m₁ := d m (ss m₁)
theorem disjoint_of_disjoint_cons_left {a : α} {l₁ l₂} : disjoint (a::l₁) l₂ → disjoint l₁ l₂ :=
disjoint_of_subset_left (list.subset_cons _ _)
theorem disjoint_of_disjoint_cons_right {a : α} {l₁ l₂} : disjoint l₁ (a::l₂) → disjoint l₁ l₂ :=
disjoint_of_subset_right (list.subset_cons _ _)
@[simp] theorem disjoint_nil_left (l : list α) : disjoint [] l
| a := (not_mem_nil a).elim
@[simp] theorem disjoint_nil_right (l : list α) : disjoint l [] :=
by rw disjoint_comm; exact disjoint_nil_left _
@[simp, priority 1100] theorem singleton_disjoint {l : list α} {a : α} : disjoint [a] l ↔ a ∉ l :=
by simp only [disjoint, mem_singleton, forall_eq]; refl
@[simp, priority 1100] theorem disjoint_singleton {l : list α} {a : α} : disjoint l [a] ↔ a ∉ l :=
by rw disjoint_comm; simp only [singleton_disjoint]
@[simp] theorem disjoint_append_left {l₁ l₂ l : list α} :
disjoint (l₁++l₂) l ↔ disjoint l₁ l ∧ disjoint l₂ l :=
by simp only [disjoint, mem_append, or_imp_distrib, forall_and_distrib]
@[simp] theorem disjoint_append_right {l₁ l₂ l : list α} :
disjoint l (l₁++l₂) ↔ disjoint l l₁ ∧ disjoint l l₂ :=
disjoint_comm.trans $ by simp only [disjoint_comm, disjoint_append_left]
@[simp] theorem disjoint_cons_left {a : α} {l₁ l₂ : list α} :
disjoint (a::l₁) l₂ ↔ a ∉ l₂ ∧ disjoint l₁ l₂ :=
(@disjoint_append_left _ [a] l₁ l₂).trans $ by simp only [singleton_disjoint]
@[simp] theorem disjoint_cons_right {a : α} {l₁ l₂ : list α} :
disjoint l₁ (a::l₂) ↔ a ∉ l₁ ∧ disjoint l₁ l₂ :=
disjoint_comm.trans $ by simp only [disjoint_comm, disjoint_cons_left]
theorem disjoint_of_disjoint_append_left_left {l₁ l₂ l : list α} (d : disjoint (l₁++l₂) l) :
disjoint l₁ l :=
(disjoint_append_left.1 d).1
theorem disjoint_of_disjoint_append_left_right {l₁ l₂ l : list α} (d : disjoint (l₁++l₂) l) :
disjoint l₂ l :=
(disjoint_append_left.1 d).2
theorem disjoint_of_disjoint_append_right_left {l₁ l₂ l : list α} (d : disjoint l (l₁++l₂)) :
disjoint l l₁ :=
(disjoint_append_right.1 d).1
theorem disjoint_of_disjoint_append_right_right {l₁ l₂ l : list α} (d : disjoint l (l₁++l₂)) :
disjoint l l₂ :=
(disjoint_append_right.1 d).2
theorem disjoint_take_drop {l : list α} {m n : ℕ} (hl : l.nodup) (h : m ≤ n) :
disjoint (l.take m) (l.drop n) :=
begin
induction l generalizing m n,
case list.nil : m n
{ simp },
case list.cons : x xs xs_ih m n
{ cases m; cases n; simp only [disjoint_cons_left, mem_cons_iff, disjoint_cons_right, drop,
true_or, eq_self_iff_true, not_true, false_and,
disjoint_nil_left, take],
{ cases h },
cases hl with _ _ h₀ h₁, split,
{ intro h, exact h₀ _ (mem_of_mem_drop h) rfl, },
solve_by_elim [le_of_succ_le_succ] { max_depth := 4 } },
end
end disjoint
/-! ### union -/
section union
variable [decidable_eq α]
@[simp] theorem nil_union (l : list α) : [] ∪ l = l := rfl
@[simp] theorem cons_union (l₁ l₂ : list α) (a : α) : a :: l₁ ∪ l₂ = insert a (l₁ ∪ l₂) := rfl
@[simp] theorem mem_union {l₁ l₂ : list α} {a : α} : a ∈ l₁ ∪ l₂ ↔ a ∈ l₁ ∨ a ∈ l₂ :=
by induction l₁; simp only [nil_union, not_mem_nil, false_or, cons_union, mem_insert_iff,
mem_cons_iff, or_assoc, *]
theorem mem_union_left {a : α} {l₁ : list α} (h : a ∈ l₁) (l₂ : list α) : a ∈ l₁ ∪ l₂ :=
mem_union.2 (or.inl h)
theorem mem_union_right {a : α} (l₁ : list α) {l₂ : list α} (h : a ∈ l₂) : a ∈ l₁ ∪ l₂ :=
mem_union.2 (or.inr h)
theorem sublist_suffix_of_union : ∀ l₁ l₂ : list α, ∃ t, t <+ l₁ ∧ t ++ l₂ = l₁ ∪ l₂
| [] l₂ := ⟨[], by refl, rfl⟩
| (a::l₁) l₂ := let ⟨t, s, e⟩ := sublist_suffix_of_union l₁ l₂ in
if h : a ∈ l₁ ∪ l₂
then ⟨t, sublist_cons_of_sublist _ s, by simp only [e, cons_union, insert_of_mem h]⟩
else ⟨a::t, cons_sublist_cons _ s, by simp only [cons_append, cons_union, e, insert_of_not_mem h];
split; refl⟩
theorem suffix_union_right (l₁ l₂ : list α) : l₂ <:+ l₁ ∪ l₂ :=
(sublist_suffix_of_union l₁ l₂).imp (λ a, and.right)
theorem union_sublist_append (l₁ l₂ : list α) : l₁ ∪ l₂ <+ l₁ ++ l₂ :=
let ⟨t, s, e⟩ := sublist_suffix_of_union l₁ l₂ in
e ▸ (append_sublist_append_right _).2 s
theorem forall_mem_union {p : α → Prop} {l₁ l₂ : list α} :
(∀ x ∈ l₁ ∪ l₂, p x) ↔ (∀ x ∈ l₁, p x) ∧ (∀ x ∈ l₂, p x) :=
by simp only [mem_union, or_imp_distrib, forall_and_distrib]
theorem forall_mem_of_forall_mem_union_left {p : α → Prop} {l₁ l₂ : list α}
(h : ∀ x ∈ l₁ ∪ l₂, p x) : ∀ x ∈ l₁, p x :=
(forall_mem_union.1 h).1
theorem forall_mem_of_forall_mem_union_right {p : α → Prop} {l₁ l₂ : list α}
(h : ∀ x ∈ l₁ ∪ l₂, p x) : ∀ x ∈ l₂, p x :=
(forall_mem_union.1 h).2
end union
/-! ### inter -/
section inter
variable [decidable_eq α]
@[simp] theorem inter_nil (l : list α) : [] ∩ l = [] := rfl
@[simp] theorem inter_cons_of_mem {a : α} (l₁ : list α) {l₂ : list α} (h : a ∈ l₂) :
(a::l₁) ∩ l₂ = a :: (l₁ ∩ l₂) :=
if_pos h
@[simp] theorem inter_cons_of_not_mem {a : α} (l₁ : list α) {l₂ : list α} (h : a ∉ l₂) :
(a::l₁) ∩ l₂ = l₁ ∩ l₂ :=
if_neg h
theorem mem_of_mem_inter_left {l₁ l₂ : list α} {a : α} : a ∈ l₁ ∩ l₂ → a ∈ l₁ :=
mem_of_mem_filter
theorem mem_of_mem_inter_right {l₁ l₂ : list α} {a : α} : a ∈ l₁ ∩ l₂ → a ∈ l₂ :=
of_mem_filter
theorem mem_inter_of_mem_of_mem {l₁ l₂ : list α} {a : α} : a ∈ l₁ → a ∈ l₂ → a ∈ l₁ ∩ l₂ :=
mem_filter_of_mem
@[simp] theorem mem_inter {a : α} {l₁ l₂ : list α} : a ∈ l₁ ∩ l₂ ↔ a ∈ l₁ ∧ a ∈ l₂ :=
mem_filter
theorem inter_subset_left (l₁ l₂ : list α) : l₁ ∩ l₂ ⊆ l₁ :=
filter_subset _
theorem inter_subset_right (l₁ l₂ : list α) : l₁ ∩ l₂ ⊆ l₂ :=
λ a, mem_of_mem_inter_right
theorem subset_inter {l l₁ l₂ : list α} (h₁ : l ⊆ l₁) (h₂ : l ⊆ l₂) : l ⊆ l₁ ∩ l₂ :=
λ a h, mem_inter.2 ⟨h₁ h, h₂ h⟩
theorem inter_eq_nil_iff_disjoint {l₁ l₂ : list α} : l₁ ∩ l₂ = [] ↔ disjoint l₁ l₂ :=
by simp only [eq_nil_iff_forall_not_mem, mem_inter, not_and]; refl
theorem forall_mem_inter_of_forall_left {p : α → Prop} {l₁ : list α} (h : ∀ x ∈ l₁, p x)
(l₂ : list α) :
∀ x, x ∈ l₁ ∩ l₂ → p x :=
ball.imp_left (λ x, mem_of_mem_inter_left) h
theorem forall_mem_inter_of_forall_right {p : α → Prop} (l₁ : list α) {l₂ : list α}
(h : ∀ x ∈ l₂, p x) :
∀ x, x ∈ l₁ ∩ l₂ → p x :=
ball.imp_left (λ x, mem_of_mem_inter_right) h
@[simp] lemma inter_reverse {xs ys : list α} :
xs.inter ys.reverse = xs.inter ys :=
by simp only [list.inter, mem_reverse]; congr
end inter
section choose
variables (p : α → Prop) [decidable_pred p] (l : list α)
lemma choose_spec (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) :=
(choose_x p l hp).property
lemma choose_mem (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1
lemma choose_property (hp : ∃ a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2
end choose
/-! ### map₂_left' -/
section map₂_left'
-- The definitional equalities for `map₂_left'` can already be used by the
-- simplifie because `map₂_left'` is marked `@[simp]`.
@[simp] theorem map₂_left'_nil_right (f : α → option β → γ) (as) :
map₂_left' f as [] = (as.map (λ a, f a none), []) :=
by cases as; refl
end map₂_left'
/-! ### map₂_right' -/
section map₂_right'
variables (f : option α → β → γ) (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem map₂_right'_nil_left :
map₂_right' f [] bs = (bs.map (f none), []) :=
by cases bs; refl
@[simp] theorem map₂_right'_nil_right :
map₂_right' f as [] = ([], as) :=
rfl
@[simp] theorem map₂_right'_nil_cons :
map₂_right' f [] (b :: bs) = (f none b :: bs.map (f none), []) :=
rfl
@[simp] theorem map₂_right'_cons_cons :
map₂_right' f (a :: as) (b :: bs) =
let rec := map₂_right' f as bs in
(f (some a) b :: rec.fst, rec.snd) :=
rfl
end map₂_right'
/-! ### zip_left' -/
section zip_left'
variables (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem zip_left'_nil_right :
zip_left' as ([] : list β) = (as.map (λ a, (a, none)), []) :=
by cases as; refl
@[simp] theorem zip_left'_nil_left :
zip_left' ([] : list α) bs = ([], bs) :=
rfl
@[simp] theorem zip_left'_cons_nil :
zip_left' (a :: as) ([] : list β) = ((a, none) :: as.map (λ a, (a, none)), []) :=
rfl
@[simp] theorem zip_left'_cons_cons :
zip_left' (a :: as) (b :: bs) =
let rec := zip_left' as bs in
((a, some b) :: rec.fst, rec.snd) :=
rfl
end zip_left'
/-! ### zip_right' -/
section zip_right'
variables (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem zip_right'_nil_left :
zip_right' ([] : list α) bs = (bs.map (λ b, (none, b)), []) :=
by cases bs; refl
@[simp] theorem zip_right'_nil_right :
zip_right' as ([] : list β) = ([], as) :=
rfl
@[simp] theorem zip_right'_nil_cons :
zip_right' ([] : list α) (b :: bs) = ((none, b) :: bs.map (λ b, (none, b)), []) :=
rfl
@[simp] theorem zip_right'_cons_cons :
zip_right' (a :: as) (b :: bs) =
let rec := zip_right' as bs in
((some a, b) :: rec.fst, rec.snd) :=
rfl
end zip_right'
/-! ### map₂_left -/
section map₂_left
variables (f : α → option β → γ) (as : list α)
-- The definitional equalities for `map₂_left` can already be used by the
-- simplifier because `map₂_left` is marked `@[simp]`.
@[simp] theorem map₂_left_nil_right :
map₂_left f as [] = as.map (λ a, f a none) :=
by cases as; refl
theorem map₂_left_eq_map₂_left' : ∀ as bs,
map₂_left f as bs = (map₂_left' f as bs).fst
| [] bs := by simp!
| (a :: as) [] := by simp!
| (a :: as) (b :: bs) := by simp! [*]
theorem map₂_left_eq_map₂ : ∀ as bs,
length as ≤ length bs →
map₂_left f as bs = map₂ (λ a b, f a (some b)) as bs
| [] [] h := by simp!
| [] (b :: bs) h := by simp!
| (a :: as) [] h := by { simp at h, contradiction }
| (a :: as) (b :: bs) h := by { simp at h, simp! [*] }
end map₂_left
/-! ### map₂_right -/
section map₂_right
variables (f : option α → β → γ) (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem map₂_right_nil_left :
map₂_right f [] bs = bs.map (f none) :=
by cases bs; refl
@[simp] theorem map₂_right_nil_right :
map₂_right f as [] = [] :=
rfl
@[simp] theorem map₂_right_nil_cons :
map₂_right f [] (b :: bs) = f none b :: bs.map (f none) :=
rfl
@[simp] theorem map₂_right_cons_cons :
map₂_right f (a :: as) (b :: bs) = f (some a) b :: map₂_right f as bs :=
rfl
theorem map₂_right_eq_map₂_right' :
map₂_right f as bs = (map₂_right' f as bs).fst :=
by simp only [map₂_right, map₂_right', map₂_left_eq_map₂_left']
theorem map₂_right_eq_map₂ (h : length bs ≤ length as) :
map₂_right f as bs = map₂ (λ a b, f (some a) b) as bs :=
begin
have : (λ a b, flip f a (some b)) = (flip (λ a b, f (some a) b)) := rfl,
simp only [map₂_right, map₂_left_eq_map₂, map₂_flip, *]
end
end map₂_right
/-! ### zip_left -/
section zip_left
variables (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem zip_left_nil_right :
zip_left as ([] : list β) = as.map (λ a, (a, none)) :=
by cases as; refl
@[simp] theorem zip_left_nil_left :
zip_left ([] : list α) bs = [] :=
rfl
@[simp] theorem zip_left_cons_nil :
zip_left (a :: as) ([] : list β) = (a, none) :: as.map (λ a, (a, none)) :=
rfl
@[simp] theorem zip_left_cons_cons :
zip_left (a :: as) (b :: bs) = (a, some b) :: zip_left as bs :=
rfl
theorem zip_left_eq_zip_left' :
zip_left as bs = (zip_left' as bs).fst :=
by simp only [zip_left, zip_left', map₂_left_eq_map₂_left']
end zip_left
/-! ### zip_right -/
section zip_right
variables (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem zip_right_nil_left :
zip_right ([] : list α) bs = bs.map (λ b, (none, b)) :=
by cases bs; refl
@[simp] theorem zip_right_nil_right :
zip_right as ([] : list β) = [] :=
rfl
@[simp] theorem zip_right_nil_cons :
zip_right ([] : list α) (b :: bs) = (none, b) :: bs.map (λ b, (none, b)) :=
rfl
@[simp] theorem zip_right_cons_cons :
zip_right (a :: as) (b :: bs) = (some a, b) :: zip_right as bs :=
rfl
theorem zip_right_eq_zip_right' :
zip_right as bs = (zip_right' as bs).fst :=
by simp only [zip_right, zip_right', map₂_right_eq_map₂_right']
end zip_right
/-! ### to_chunks -/
section to_chunks
@[simp] theorem to_chunks_nil (n) : @to_chunks α n [] = [] := by cases n; refl
theorem to_chunks_aux_eq (n) : ∀ xs i,
@to_chunks_aux α n xs i = (xs.take i, (xs.drop i).to_chunks (n+1))
| [] i := by cases i; refl
| (x::xs) 0 := by rw [to_chunks_aux, drop, to_chunks]; cases to_chunks_aux n xs n; refl
| (x::xs) (i+1) := by rw [to_chunks_aux, to_chunks_aux_eq]; refl
theorem to_chunks_eq_cons' (n) : ∀ {xs : list α} (h : xs ≠ []),
xs.to_chunks (n+1) = xs.take (n+1) :: (xs.drop (n+1)).to_chunks (n+1)
| [] e := (e rfl).elim
| (x::xs) _ := by rw [to_chunks, to_chunks_aux_eq]; refl
theorem to_chunks_eq_cons : ∀ {n} {xs : list α} (n0 : n ≠ 0) (x0 : xs ≠ []),
xs.to_chunks n = xs.take n :: (xs.drop n).to_chunks n
| 0 _ e := (e rfl).elim
| (n+1) xs _ := to_chunks_eq_cons' _
theorem to_chunks_aux_join {n} : ∀ {xs i l L}, @to_chunks_aux α n xs i = (l, L) → l ++ L.join = xs
| [] _ _ _ rfl := rfl
| (x::xs) i l L e := begin
cases i; [
cases e' : to_chunks_aux n xs n with l L,
cases e' : to_chunks_aux n xs i with l L];
{ rw [to_chunks_aux, e', to_chunks_aux] at e, cases e,
exact (congr_arg (cons x) (to_chunks_aux_join e') : _) }
end
@[simp] theorem to_chunks_join : ∀ n xs, (@to_chunks α n xs).join = xs
| n [] := by cases n; refl
| 0 (x::xs) := by simp only [to_chunks, join]; rw append_nil
| (n+1) (x::xs) := begin
rw to_chunks,
cases e : to_chunks_aux n xs n with l L,
exact (congr_arg (cons x) (to_chunks_aux_join e) : _),
end
theorem to_chunks_length_le : ∀ n xs, n ≠ 0 → ∀ l : list α,
l ∈ @to_chunks α n xs → l.length ≤ n
| 0 _ e _ := (e rfl).elim
| (n+1) xs _ l := begin
refine (measure_wf length).induction xs _, intros xs IH h,
by_cases x0 : xs = [], {subst xs, cases h},
rw to_chunks_eq_cons' _ x0 at h, rcases h with rfl|h,
{ apply length_take_le },
{ refine IH _ _ h,
simp only [measure, inv_image, length_drop],
exact nat.sub_lt_self (length_pos_iff_ne_nil.2 x0) (succ_pos _) },
end
end to_chunks
/-! ### Miscellaneous lemmas -/
theorem ilast'_mem : ∀ a l, @ilast' α a l ∈ a :: l
| a [] := or.inl rfl
| a (b::l) := or.inr (ilast'_mem b l)
@[simp] lemma nth_le_attach (L : list α) (i) (H : i < L.attach.length) :
(L.attach.nth_le i H).1 = L.nth_le i (length_attach L ▸ H) :=
calc (L.attach.nth_le i H).1
= (L.attach.map subtype.val).nth_le i (by simpa using H) : by rw nth_le_map'
... = L.nth_le i _ : by congr; apply attach_map_val
end list
@[to_additive]
theorem monoid_hom.map_list_prod {α β : Type*} [monoid α] [monoid β] (f : α →* β) (l : list α) :
f l.prod = (l.map f).prod :=
(l.prod_hom f).symm
namespace list
@[to_additive]
theorem prod_map_hom {α β γ : Type*} [monoid β] [monoid γ] (L : list α) (f : α → β) (g : β →* γ) :
(L.map (g ∘ f)).prod = g ((L.map f).prod) :=
by {rw g.map_list_prod, exact congr_arg _ (map_map _ _ _).symm}
theorem sum_map_mul_left {α : Type*} [semiring α] {β : Type*} (L : list β)
(f : β → α) (r : α) :
(L.map (λ b, r * f b)).sum = r * (L.map f).sum :=
sum_map_hom L f $ add_monoid_hom.mul_left r
theorem sum_map_mul_right {α : Type*} [semiring α] {β : Type*} (L : list β)
(f : β → α) (r : α) :
(L.map (λ b, f b * r)).sum = (L.map f).sum * r :=
sum_map_hom L f $ add_monoid_hom.mul_right r
universes u v
@[simp]
theorem mem_map_swap {α : Type u} {β : Type v} (x : α) (y : β) (xs : list (α × β)) :
(y, x) ∈ map prod.swap xs ↔ (x, y) ∈ xs :=
begin
induction xs with x xs,
{ simp only [not_mem_nil, map_nil] },
{ cases x with a b,
simp only [mem_cons_iff, prod.mk.inj_iff, map, prod.swap_prod_mk,
prod.exists, xs_ih, and_comm] },
end
lemma slice_eq {α} (xs : list α) (n m : ℕ) :
slice n m xs = xs.take n ++ xs.drop (n+m) :=
begin
induction n generalizing xs,
{ simp [slice] },
{ cases xs; simp [slice, *, nat.succ_add], }
end
lemma sizeof_slice_lt {α} [has_sizeof α] (i j : ℕ) (hj : 0 < j) (xs : list α) (hi : i < xs.length) :
sizeof (list.slice i j xs) < sizeof xs :=
begin
induction xs generalizing i j,
case list.nil : i j h
{ cases hi },
case list.cons : x xs xs_ih i j h
{ cases i; simp only [-slice_eq, list.slice],
{ cases j, cases h,
dsimp only [drop], unfold_wf,
apply @lt_of_le_of_lt _ _ _ xs.sizeof,
{ clear_except,
induction xs generalizing j; unfold_wf,
case list.nil : j
{ refl },
case list.cons : xs_hd xs_tl xs_ih j
{ cases j; unfold_wf, refl,
transitivity, apply xs_ih,
simp }, },
unfold_wf, apply zero_lt_one_add, },
{ unfold_wf, apply xs_ih _ _ h,
apply lt_of_succ_lt_succ hi, } },
end
end list
|
3b3d0051ba8b6ab989c3783db4ba9eb4f95378c7 | 3f1a1d97c03bb24b55a1b9969bb4b3c619491d5a | /library/data/rbmap/default.lean | 5c9fdd76cd3596b5506a58ddcec8b262910a69ee | [
"Apache-2.0"
] | permissive | praveenmunagapati/lean | 00c3b4496cef8e758396005013b9776bb82c4f56 | fc760f57d20e0a486d14bc8a08d89147b60f530c | refs/heads/master | 1,630,692,342,183 | 1,515,626,222,000 | 1,515,626,222,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 15,015 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import data.rbtree
universes u v
namespace rbmap
variables {α : Type u} {β : Type v} {lt : α → α → Prop}
/- Auxiliary instances -/
private def rbmap_lt_is_swo {α : Type u} {β : Type v} {lt : α → α → Prop} [is_strict_weak_order α lt] : is_strict_weak_order (α × β) (rbmap_lt lt) :=
{ irrefl := λ _, irrefl_of lt _,
trans := λ _ _ _ h₁ h₂, trans_of lt h₁ h₂,
incomp_trans := λ _ _ _ h₁ h₂, incomp_trans_of lt h₁ h₂ }
private def rbmap_lt_dec {α : Type u} {β : Type v} {lt : α → α → Prop} [h : decidable_rel lt] : decidable_rel (@rbmap_lt α β lt) :=
λ a b, h a.1 b.1
local attribute [instance] rbmap_lt_is_swo rbmap_lt_dec
/- Helper lemmas for reusing rbtree results. -/
private lemma to_rbtree_mem {k : α} {m : rbmap α β lt} : k ∈ m → ∃ v : β, rbtree.mem (k, v) m :=
begin
cases m with n p; cases n; intros h,
{ exact false.elim h },
all_goals { existsi n_val.2, exact h }
end
private lemma eqv_entries_of_eqv_keys {k₁ k₂ : α} (v₁ v₂ : β) : k₁ ≈[lt] k₂ → (k₁, v₁) ≈[rbmap_lt lt] (k₂, v₂) :=
id
private lemma eqv_keys_of_eqv_entries {k₁ k₂ : α} {v₁ v₂ : β} : (k₁, v₁) ≈[rbmap_lt lt] (k₂, v₂) → k₁ ≈[lt] k₂ :=
id
private lemma eqv_entries [is_irrefl α lt] (k : α) (v₁ v₂ : β) : (k, v₁) ≈[rbmap_lt lt] (k, v₂) :=
and.intro (irrefl_of lt k) (irrefl_of lt k)
private lemma to_rbmap_mem [is_strict_weak_order α lt] {k : α} {v : β} {m : rbmap α β lt} : rbtree.mem (k, v) m → k ∈ m :=
begin
cases m with n p; cases n; intros h,
{ exact false.elim h },
{ simp [has_mem.mem, rbmap.mem],
exact @rbtree.mem_of_mem_of_eqv _ _ _ ⟨rbnode.red_node n_lchild n_val n_rchild, p⟩ _ _ h (eqv_entries _ _ _) },
{ simp [has_mem.mem, rbmap.mem],
exact @rbtree.mem_of_mem_of_eqv _ _ _ ⟨rbnode.black_node n_lchild n_val n_rchild, p⟩ _ _ h (eqv_entries _ _ _) }
end
private lemma to_rbtree_mem' [is_strict_weak_order α lt] {k : α} {m : rbmap α β lt} (v : β) : k ∈ m → rbtree.mem (k, v) m :=
begin
intro h,
cases to_rbtree_mem h with v' hm,
apply rbtree.mem_of_mem_of_eqv hm,
apply eqv_entries
end
lemma eq_some_of_to_value_eq_some {e : option (α × β)} {v : β} : to_value e = some v → ∃ k, e = some (k, v) :=
begin
cases e with val; simp [to_value],
{ contradiction },
{ cases val, simp, intro h, injection h, subst v, constructor, refl }
end
lemma eq_none_of_to_value_eq_none {e : option (α × β)} : to_value e = none → e = none :=
begin
cases e; simp [to_value],
{ contradiction }
end
/- Lemmas -/
lemma not_mem_mk_rbmap : ∀ (k : α), k ∉ mk_rbmap α β lt :=
by simp [has_mem.mem, mk_rbmap, mk_rbtree, rbmap.mem]
lemma not_mem_of_empty {m : rbmap α β lt} (k : α) : m.empty = tt → k ∉ m :=
by cases m with n p; cases n; simp [has_mem.mem, mk_rbmap, mk_rbtree, rbmap.mem, rbmap.empty, rbtree.empty]
variables [decidable_rel lt]
lemma not_mem_of_find_entry_none [is_strict_weak_order α lt] {k : α} {m : rbmap α β lt} : m.find_entry k = none → k ∉ m :=
begin
cases m with t p, cases t; simp [find_entry],
{ intros, simp [has_mem.mem, rbmap.mem] },
all_goals { intro h, exact rbtree.not_mem_of_find_none h, }
end
lemma not_mem_of_find_none [is_strict_weak_order α lt] {k : α} {m : rbmap α β lt} : m.find k = none → k ∉ m :=
begin
simp [find], intro h,
have := eq_none_of_to_value_eq_none h,
exact not_mem_of_find_entry_none this
end
lemma mem_of_find_entry_some [is_strict_weak_order α lt] {k₁ : α} {e : α × β} {m : rbmap α β lt} : m.find_entry k₁ = some e → k₁ ∈ m :=
begin
cases m with t p, cases t; simp [find_entry],
{ intros, contradiction },
all_goals { intro h, exact rbtree.mem_of_find_some h }
end
lemma mem_of_find_some [is_strict_weak_order α lt] {k : α} {v : β} {m : rbmap α β lt} : m.find k = some v → k ∈ m :=
begin
simp [find], intro h,
have := eq_some_of_to_value_eq_some h,
cases this with _ he,
exact mem_of_find_entry_some he
end
lemma find_entry_eq_find_entry_of_eqv [is_strict_weak_order α lt] {m : rbmap α β lt} {k₁ k₂ : α} : k₁ ≈[lt] k₂ → m.find_entry k₁ = m.find_entry k₂ :=
begin
intro h, cases m with t p, cases t; simp [find_entry],
all_goals { apply rbtree.find_eq_find_of_eqv, apply eqv_entries_of_eqv_keys, assumption }
end
lemma find_eq_find_of_eqv [is_strict_weak_order α lt] {k₁ k₂ : α} (m : rbmap α β lt) : k₁ ≈[lt] k₂ → m.find k₁ = m.find k₂ :=
begin intro h, simp [find], apply congr_arg, apply find_entry_eq_find_entry_of_eqv, assumption end
lemma find_entry_correct [is_strict_weak_order α lt] (k : α) (m : rbmap α β lt) : k ∈ m ↔ (∃ e, m.find_entry k = some e ∧ k ≈[lt] e.1) :=
begin
apply iff.intro; cases m with t p,
{ intro h,
have h := to_rbtree_mem h, cases h with v h₁,
have hex := iff.mp (rbtree.find_correct _ _) h₁, cases hex with e h₂,
existsi e, cases t; simp [find_entry] at ⊢ h₂,
{ simp [rbtree.find, rbnode.find] at h₂, cases h₂, contradiction },
{ cases h₂ with h₂₁ h₂₂, split,
{ have := rbtree.find_eq_find_of_eqv ⟨rbnode.red_node t_lchild t_val t_rchild, p⟩ (eqv_entries k v t_val.2),
rw [←this], exact h₂₁ },
{ cases e, apply eqv_keys_of_eqv_entries h₂₂ } },
{ cases h₂ with h₂₁ h₂₂, split,
{ have := rbtree.find_eq_find_of_eqv ⟨rbnode.black_node t_lchild t_val t_rchild, p⟩ (eqv_entries k v t_val.2),
rw [←this], exact h₂₁ },
{ cases e, apply eqv_keys_of_eqv_entries h₂₂ } } },
{ intro h, cases h with e h,
cases h with h₁ h₂, cases t; simp [find_entry] at h₁,
{ contradiction },
all_goals { exact to_rbmap_mem (rbtree.mem_of_find_some h₁) } }
end
lemma eqv_of_find_entry_some [is_strict_weak_order α lt] {k₁ k₂ : α} {v : β} {m : rbmap α β lt} : m.find_entry k₁ = some (k₂, v) → k₁ ≈[lt] k₂ :=
begin
cases m with t p, cases t; simp [find_entry],
{ contradiction },
all_goals { intro h, exact eqv_keys_of_eqv_entries (rbtree.eqv_of_find_some h) }
end
lemma eq_of_find_entry_some [is_strict_total_order α lt] {k₁ k₂ : α} {v : β} {m : rbmap α β lt} : m.find_entry k₁ = some (k₂, v) → k₁ = k₂ :=
λ h, suffices k₁ ≈[lt] k₂, from eq_of_eqv_lt this,
eqv_of_find_entry_some h
lemma find_correct [is_strict_weak_order α lt] (k : α) (m : rbmap α β lt) : k ∈ m ↔ ∃ v, m.find k = some v :=
begin
apply iff.intro,
{ intro h,
have := iff.mp (find_entry_correct k m) h,
cases this with e h, cases h with h₁ h₂,
existsi e.2, simp [find, h₁, to_value] },
{ intro h,
cases h with v h,
simp [find] at h,
have h := eq_some_of_to_value_eq_some h,
cases h with k' h,
have heqv := eqv_of_find_entry_some h,
exact iff.mpr (find_entry_correct k m) ⟨(k', v), ⟨h, heqv⟩⟩ }
end
lemma constains_correct [is_strict_weak_order α lt] (k : α) (m : rbmap α β lt) : k ∈ m ↔ m.contains k = tt :=
begin
apply iff.intro,
{ intro h,
have h := iff.mp (find_entry_correct k m) h,
cases h with e h, cases h with h₁ h₂,
simp [contains, h₁, option.is_some] },
{ simp [contains],
intro h,
generalize he : find_entry m k = e,
cases e,
{ simp [he, option.is_some] at h, contradiction },
{ exact mem_of_find_entry_some he } }
end
lemma mem_of_mem_of_eqv [is_strict_weak_order α lt] {m : rbmap α β lt} {k₁ k₂ : α} : k₁ ∈ m → k₁ ≈[lt] k₂ → k₂ ∈ m :=
begin
intros h₁ h₂,
have h₁ := to_rbtree_mem h₁, cases h₁ with v h₁,
exact to_rbmap_mem (rbtree.mem_of_mem_of_eqv h₁ (eqv_entries_of_eqv_keys v v h₂))
end
lemma mem_insert_of_incomp [is_strict_weak_order α lt] {k₁ k₂ : α} (m : rbmap α β lt) (v : β) : (¬ lt k₁ k₂ ∧ ¬ lt k₂ k₁) → k₁ ∈ m.insert k₂ v :=
λ h, to_rbmap_mem (rbtree.mem_insert_of_incomp m (eqv_entries_of_eqv_keys v v h))
lemma mem_insert [is_strict_weak_order α lt] (k : α) (m : rbmap α β lt) (v : β) : k ∈ m.insert k v :=
to_rbmap_mem (rbtree.mem_insert (k, v) m)
lemma mem_insert_of_equiv [is_strict_weak_order α lt] {k₁ k₂ : α} (m : rbmap α β lt) (v : β) : k₁ ≈[lt] k₂ → k₁ ∈ m.insert k₂ v :=
mem_insert_of_incomp m v
lemma mem_insert_of_mem [is_strict_weak_order α lt] {k₁ : α} {m : rbmap α β lt} (k₂ : α) (v : β) : k₁ ∈ m → k₁ ∈ m.insert k₂ v :=
λ h, to_rbmap_mem (rbtree.mem_insert_of_mem (k₂, v) (to_rbtree_mem' v h))
lemma equiv_or_mem_of_mem_insert [is_strict_weak_order α lt] {k₁ k₂ : α} {v : β} {m : rbmap α β lt} : k₁ ∈ m.insert k₂ v → k₁ ≈[lt] k₂ ∨ k₁ ∈ m :=
λ h, or.elim (rbtree.equiv_or_mem_of_mem_insert (to_rbtree_mem' v h))
(λ h, or.inl (eqv_keys_of_eqv_entries h))
(λ h, or.inr (to_rbmap_mem h))
lemma incomp_or_mem_of_mem_ins [is_strict_weak_order α lt] {k₁ k₂ : α} {v : β} {m : rbmap α β lt} : k₁ ∈ m.insert k₂ v → (¬ lt k₁ k₂ ∧ ¬ lt k₂ k₁) ∨ k₁ ∈ m :=
equiv_or_mem_of_mem_insert
lemma eq_or_mem_of_mem_ins [is_strict_total_order α lt] {k₁ k₂ : α} {v : β} {m : rbmap α β lt} : k₁ ∈ m.insert k₂ v → k₁ = k₂ ∨ k₁ ∈ m :=
λ h, suffices k₁ ≈[lt] k₂ ∨ k₁ ∈ m, by simp [eqv_lt_iff_eq] at this; assumption,
incomp_or_mem_of_mem_ins h
lemma find_entry_insert_of_eqv [is_strict_weak_order α lt] (m : rbmap α β lt) {k₁ k₂ : α} (v : β) : k₁ ≈[lt] k₂ → (m.insert k₁ v).find_entry k₂ = some (k₁, v) :=
begin
intro h,
generalize h₁ : m.insert k₁ v = m',
cases m' with t p, cases t,
{ have := mem_insert k₁ m v, rw [h₁] at this, apply absurd this, apply not_mem_mk_rbmap },
all_goals { simp [find_entry], rw [←h₁, insert], apply rbtree.find_insert_of_eqv, apply eqv_entries_of_eqv_keys _ _ h }
end
lemma find_entry_insert [is_strict_weak_order α lt] (m : rbmap α β lt) (k : α) (v : β) : (m.insert k v).find_entry k = some (k, v) :=
find_entry_insert_of_eqv m v (refl k)
lemma find_insert_of_eqv [is_strict_weak_order α lt] (m : rbmap α β lt) {k₁ k₂ : α} (v : β) : k₁ ≈[lt] k₂ → (m.insert k₁ v).find k₂ = some v :=
begin
intro h,
have := find_entry_insert_of_eqv m v h,
simp [find, this, to_value]
end
lemma find_insert [is_strict_weak_order α lt] (m : rbmap α β lt) (k : α) (v : β) : (m.insert k v).find k = some v :=
find_insert_of_eqv m v (refl k)
lemma find_entry_insert_of_disj [is_strict_weak_order α lt] {k₁ k₂ : α} (m : rbmap α β lt) (v : β) : lt k₁ k₂ ∨ lt k₂ k₁ → (m.insert k₁ v).find_entry k₂ = m.find_entry k₂ :=
begin
intro h,
have h' : ∀ {v₁ v₂ : β}, (rbmap_lt lt) (k₁, v₁) (k₂, v₂) ∨ (rbmap_lt lt) (k₂, v₂) (k₁, v₁) := λ _ _, h,
generalize h₁ : m = m₁,
generalize h₂ : insert m₁ k₁ v = m₂,
rw [←h₁] at h₂ ⊢, rw [←h₂],
cases m₁ with t₁ p₁; cases t₁; cases m₂ with t₂ p₂; cases t₂,
{ rw [h₂, h₁] },
iterate 2 {
rw [h₂],
conv { to_lhs, simp [find_entry] },
rw [←h₂, insert, rbtree.find_insert_of_disj _ h', h₁],
refl },
any_goals { simp [insert] at h₂,
exact absurd h₂ (rbtree.insert_ne_mk_rbtree m (k₁, v)) },
any_goals {
rw [h₂, h₁], simp [find_entry], rw [←h₂, ←h₁, insert, rbtree.find_insert_of_disj _ h'],
apply rbtree.find_eq_find_of_eqv, apply eqv_entries }
end
lemma find_entry_insert_of_not_eqv [is_strict_weak_order α lt] {k₁ k₂ : α} (m : rbmap α β lt) (v : β) : ¬ k₁ ≈[lt] k₂ → (m.insert k₁ v).find_entry k₂ = m.find_entry k₂ :=
begin
intro hn,
have he : lt k₁ k₂ ∨ lt k₂ k₁, {
simp [strict_weak_order.equiv, decidable.not_and_iff_or_not, decidable.not_not_iff] at hn,
assumption },
apply find_entry_insert_of_disj _ _ he
end
lemma find_entry_insert_of_ne [is_strict_total_order α lt] {k₁ k₂ : α} (m : rbmap α β lt) (v : β) : k₁ ≠ k₂ → (m.insert k₁ v).find_entry k₂ = m.find_entry k₂ :=
begin
intro h,
have : ¬ k₁ ≈[lt] k₂ := λ h', h (eq_of_eqv_lt h'),
apply find_entry_insert_of_not_eqv _ _ this
end
lemma find_insert_of_disj [is_strict_weak_order α lt] {k₁ k₂ : α} (m : rbmap α β lt) (v : β) : lt k₁ k₂ ∨ lt k₂ k₁ → (m.insert k₁ v).find k₂ = m.find k₂ :=
begin intro h, have := find_entry_insert_of_disj m v h, simp [find, this] end
lemma find_insert_of_not_eqv [is_strict_weak_order α lt] {k₁ k₂ : α} (m : rbmap α β lt) (v : β) : ¬ k₁ ≈[lt] k₂ → (m.insert k₁ v).find k₂ = m.find k₂ :=
begin intro h, have := find_entry_insert_of_not_eqv m v h, simp [find, this] end
lemma find_insert_of_ne [is_strict_total_order α lt] {k₁ k₂ : α} (m : rbmap α β lt) (v : β) : k₁ ≠ k₂ → (m.insert k₁ v).find k₂ = m.find k₂ :=
begin intro h, have := find_entry_insert_of_ne m v h, simp [find, this] end
lemma mem_of_min_eq [is_strict_total_order α lt] {k : α} {v : β} {m : rbmap α β lt} : m.min = some (k, v) → k ∈ m :=
λ h, to_rbmap_mem (rbtree.mem_of_min_eq h)
lemma mem_of_max_eq [is_strict_total_order α lt] {k : α} {v : β} {m : rbmap α β lt} : m.max = some (k, v) → k ∈ m :=
λ h, to_rbmap_mem (rbtree.mem_of_max_eq h)
lemma eq_leaf_of_min_eq_none [is_strict_weak_order α lt] {m : rbmap α β lt} : m.min = none → m = mk_rbmap α β lt :=
rbtree.eq_leaf_of_min_eq_none
lemma eq_leaf_of_max_eq_none [is_strict_weak_order α lt] {m : rbmap α β lt} : m.max = none → m = mk_rbmap α β lt :=
rbtree.eq_leaf_of_max_eq_none
lemma min_is_minimal [is_strict_weak_order α lt] {k : α} {v : β} {m : rbmap α β lt} : m.min = some (k, v) → ∀ {k'}, k' ∈ m → k ≈[lt] k' ∨ lt k k' :=
λ h k' hm, or.elim (rbtree.min_is_minimal h (to_rbtree_mem' v hm))
(λ h, or.inl (eqv_keys_of_eqv_entries h))
(λ h, or.inr h)
lemma max_is_maximal [is_strict_weak_order α lt] {k : α} {v : β} {m : rbmap α β lt} : m.max = some (k, v) → ∀ {k'}, k' ∈ m → k ≈[lt] k' ∨ lt k' k :=
λ h k' hm, or.elim (rbtree.max_is_maximal h (to_rbtree_mem' v hm))
(λ h, or.inl (eqv_keys_of_eqv_entries h))
(λ h, or.inr h)
lemma min_is_minimal_of_total [is_strict_total_order α lt] {k : α} {v : β} {m : rbmap α β lt} : m.min = some (k, v) → ∀ {k'}, k' ∈ m → k = k' ∨ lt k k' :=
λ h k' hm,
match min_is_minimal h hm with
| or.inl h := or.inl (eq_of_eqv_lt h)
| or.inr h := or.inr h
end
lemma max_is_maximal_of_total [is_strict_total_order α lt] {k : α} {v : β} {m : rbmap α β lt} : m.max = some (k, v) → ∀ {k'}, k' ∈ m → k = k' ∨ lt k' k :=
λ h k' hm,
match max_is_maximal h hm with
| or.inl h := or.inl (eq_of_eqv_lt h)
| or.inr h := or.inr h
end
end rbmap
|
eb8e37b87f46cf38c5974c35208e2360c50a1295 | ad0c7d243dc1bd563419e2767ed42fb323d7beea | /algebra/big_operators.lean | a963297c0641d202078863d48e229c5cc7042e01 | [
"Apache-2.0"
] | permissive | sebzim4500/mathlib | e0b5a63b1655f910dee30badf09bd7e191d3cf30 | 6997cafbd3a7325af5cb318561768c316ceb7757 | refs/heads/master | 1,585,549,958,618 | 1,538,221,723,000 | 1,538,221,723,000 | 150,869,076 | 0 | 0 | Apache-2.0 | 1,538,229,323,000 | 1,538,229,323,000 | null | UTF-8 | Lean | false | false | 19,579 | 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
Some big operators for lists and finite sets.
-/
import data.list.basic data.list.perm data.finset
algebra.group algebra.ordered_group algebra.group_power
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
theorem directed.finset_le {r : α → α → Prop} [is_trans α r]
{ι} (hι : nonempty ι) {f : ι → α} (D : directed r f) (s : finset ι) :
∃ z, ∀ i ∈ s, r (f i) (f z) :=
show ∃ z, ∀ i ∈ s.1, r (f i) (f z), from
multiset.induction_on s.1 (by simp [exists_true_iff_nonempty, hι]) $
begin
rintro i s ⟨j, H⟩,
rcases D i j with ⟨k, h₁, h₂⟩, existsi k,
simp, rintro a (rfl | h),
exacts [h₁, trans (H _ h) h₂]
end
namespace finset
variables {s s₁ s₂ : finset α} {a : α} {f g : α → β}
/-- `prod s f` is the product of `f x` as `x` ranges over the elements of the finite set `s`. -/
@[to_additive finset.sum]
protected def prod [comm_monoid β] (s : finset α) (f : α → β) : β := (s.1.map f).prod
attribute [to_additive finset.sum.equations._eqn_1] finset.prod.equations._eqn_1
@[to_additive finset.sum_eq_fold]
theorem prod_eq_fold [comm_monoid β] (s : finset α) (f : α → β) : s.prod f = s.fold (*) 1 f := rfl
section comm_monoid
variables [comm_monoid β]
@[simp, to_additive finset.sum_empty]
lemma prod_empty {α : Type u} {f : α → β} : (∅:finset α).prod f = 1 := rfl
@[simp, to_additive finset.sum_insert]
lemma prod_insert [decidable_eq α] : a ∉ s → (insert a s).prod f = f a * s.prod f := fold_insert
@[simp, to_additive finset.sum_singleton]
lemma prod_singleton : (singleton a).prod f = f a :=
eq.trans fold_singleton (by simp)
@[simp] lemma prod_const_one : s.prod (λx, (1 : β)) = 1 :=
by simp [finset.prod]
@[simp] lemma sum_const_zero {β} {s : finset α} [add_comm_monoid β] : s.sum (λx, (0 : β)) = 0 :=
@prod_const_one _ (multiplicative β) _ _
attribute [to_additive finset.sum_const_zero] prod_const_one
@[simp, to_additive finset.sum_image]
lemma prod_image [decidable_eq α] [decidable_eq γ] {s : finset γ} {g : γ → α} :
(∀x∈s, ∀y∈s, g x = g y → x = y) → (s.image g).prod f = s.prod (λx, f (g x)) :=
fold_image
@[congr, to_additive finset.sum_congr]
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 finset.sum_union_inter]
lemma prod_union_inter [decidable_eq α] : (s₁ ∪ s₂).prod f * (s₁ ∩ s₂).prod f = s₁.prod f * s₂.prod f :=
fold_union_inter
@[to_additive finset.sum_union]
lemma prod_union [decidable_eq α] (h : s₁ ∩ s₂ = ∅) : (s₁ ∪ s₂).prod f = s₁.prod f * s₂.prod f :=
by rw [←prod_union_inter, h]; simp
@[to_additive finset.sum_sdiff]
lemma prod_sdiff [decidable_eq α] (h : s₁ ⊆ s₂) : (s₂ \ s₁).prod f * s₁.prod f = s₂.prod f :=
by rw [←prod_union (sdiff_inter_self _ _), sdiff_union_of_subset h]
@[to_additive finset.sum_bind]
lemma prod_bind [decidable_eq α] {s : finset γ} {t : γ → finset α} :
(∀x∈s, ∀y∈s, x ≠ y → t x ∩ t y = ∅) → (s.bind t).prod f = s.prod (λx, (t x).prod f) :=
by haveI := classical.dec_eq γ; exact
finset.induction_on s (by simp)
(assume x s hxs ih hd,
have hd' : ∀x∈s, ∀y∈s, x ≠ y → t x ∩ t y = ∅,
from assume _ hx _ hy, hd _ (mem_insert_of_mem hx) _ (mem_insert_of_mem hy),
have t x ∩ finset.bind s t = ∅,
from ext.2 $ assume a,
by simp [mem_bind];
from assume h₁ y hys hy₂,
have ha : a ∈ t x ∩ t y, by simp [*],
have t x ∩ t y = ∅,
from hd _ (mem_insert_self _ _) _ (mem_insert_of_mem hys) $ assume h, hxs $ h.symm ▸ hys,
by rwa [this] at ha,
by simp [hxs, prod_union this, ih hd'] {contextual := tt})
@[to_additive finset.sum_product]
lemma prod_product {s : finset γ} {t : finset α} {f : γ×α → β} :
(s.product t).prod f = s.prod (λx, t.prod $ λy, f (x, y)) :=
begin
haveI := classical.dec_eq α, haveI := classical.dec_eq γ,
rw [product_eq_bind, prod_bind (λ x hx y hy h, ext.2 _)], {simp [prod_image]},
simp [mem_image], intros, intro, refine h _, cc
end
@[to_additive finset.sum_sigma]
lemma prod_sigma {σ : α → Type*}
{s : finset α} {t : Πa, finset (σ a)} {f : sigma σ → β} :
(s.sigma t).prod f = s.prod (λa, (t a).prod $ λs, f ⟨a, s⟩) :=
by haveI := classical.dec_eq α; haveI := (λ a, classical.dec_eq (σ a)); exact
have ∀a₁ a₂:α, ∀s₁ : finset (σ a₁), ∀s₂ : finset (σ a₂), a₁ ≠ a₂ →
s₁.image (sigma.mk a₁) ∩ s₂.image (sigma.mk a₂) = ∅,
from assume b₁ b₂ s₁ s₂ h, ext.2 $ assume ⟨b₃, c₃⟩,
by simp [mem_image, sigma.mk.inj_iff, heq_iff_eq] {contextual := tt}; cc,
calc (s.sigma t).prod f =
(s.bind (λa, (t a).image (λs, sigma.mk a s))).prod f : by rw sigma_eq_bind
... = s.prod (λa, ((t a).image (λs, sigma.mk a s)).prod f) :
prod_bind $ assume a₁ ha a₂ ha₂ h, this a₁ a₂ (t a₁) (t a₂) h
... = (s.prod $ λa, (t a).prod $ λs, f ⟨a, s⟩) :
by simp [prod_image, sigma.mk.inj_iff, heq_iff_eq]
@[to_additive finset.sum_add_distrib]
lemma prod_mul_distrib : s.prod (λx, f x * g x) = s.prod f * s.prod g :=
eq.trans (by simp; refl) fold_op_distrib
@[to_additive finset.sum_comm]
lemma prod_comm [decidable_eq γ] {s : finset γ} {t : finset α} {f : γ → α → β} :
s.prod (λx, t.prod $ f x) = t.prod (λy, s.prod $ λx, f x y) :=
finset.induction_on s (by simp) (by simp [prod_mul_distrib] {contextual := tt})
@[to_additive finset.sum_hom]
lemma prod_hom [comm_monoid γ] (g : β → γ)
(h₁ : g 1 = 1) (h₂ : ∀x y, g (x * y) = g x * g y) : s.prod (λx, g (f x)) = g (s.prod f) :=
eq.trans (by rw [h₁]; refl) (fold_hom h₂)
lemma sum_nat_cast [add_comm_monoid β] [has_one β] (s : finset α) (f : α → ℕ) :
↑(s.sum f) = s.sum (λa, f a : α → β) :=
(sum_hom _ nat.cast_zero nat.cast_add).symm
@[to_additive finset.sum_subset]
lemma prod_subset (h : s₁ ⊆ s₂) (hf : ∀x∈s₂, x ∉ s₁ → f x = 1) : s₁.prod f = s₂.prod f :=
by haveI := classical.dec_eq α; exact
have (s₂ \ s₁).prod f = (s₂ \ s₁).prod (λx, 1),
from prod_congr rfl begin simp [hf] {contextual := tt} end,
by rw [←prod_sdiff h]; simp [this]
@[to_additive finset.sum_eq_single]
lemma prod_eq_single {s : finset α} {f : α → β} (a : α)
(h₀ : ∀b∈s, b ≠ a → f b = 1) (h₁ : a ∉ s → f a = 1) : s.prod f = f a :=
by haveI := classical.dec_eq α;
from classical.by_cases
(assume : a ∈ s,
calc s.prod f = ({a} : finset α).prod f :
begin
refine (prod_subset _ _).symm,
{ simp [finset.subset_iff, this] },
{ simpa using h₀ }
end
... = f a : by simp)
(assume : a ∉ s,
have ∀b, b ∈ s → f b = 1,
from assume b hb, h₀ b hb $ assume eq, this $ eq ▸ hb,
calc s.prod f = (∅ : finset α).prod f : (prod_subset (empty_subset s) $ by simpa).symm
... = f a : (h₁ ‹a ∉ s›).symm)
@[to_additive finset.sum_attach]
lemma prod_attach {f : α → β} : s.attach.prod (λx, f x.val) = s.prod f :=
by haveI := classical.dec_eq α; exact
calc s.attach.prod (λx, f x.val) = ((s.attach).image subtype.val).prod f :
by rw [prod_image]; exact assume x _ y _, subtype.eq
... = _ : by rw [attach_image_val]
@[to_additive finset.sum_bij]
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) :
s.prod f = t.prod g :=
by haveI := classical.prop_decidable; exact
calc s.prod f = s.attach.prod (λx, f x.val) : prod_attach.symm
... = s.attach.prod (λx, g (i x.1 x.2)) : prod_congr rfl $ assume x hx, h _ _
... = (s.attach.image $ λx:{x // x ∈ s}, i x.1 x.2).prod g :
(prod_image $ assume (a₁:{x // x ∈ s}) _ a₂ _ eq, subtype.eq $ i_inj a₁.1 a₂.1 a₁.2 a₂.2 eq).symm
... = _ :
prod_subset
(by simp [subset_iff]; intros b a h eq; subst eq; exact hi _ _)
(assume b hb, not_imp_comm.mp $ assume hb₂,
let ⟨a, ha, eq⟩ := i_surj b hb in by simp [eq]; exact ⟨_, _, rfl⟩)
@[to_additive finset.sum_bij_ne_zero]
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)
(hi₂ : ∀a₁ a₂ h₁₁ h₁₂ h₂₁ h₂₂, i a₁ h₁₁ h₁₂ = i a₂ h₂₁ h₂₂ → a₁ = a₂)
(hi₃ : ∀b∈t, g b ≠ 1 → ∃a h₁ h₂, b = i a h₁ h₂)
(h : ∀a h₁ h₂, f a = g (i a h₁ h₂)) :
s.prod f = t.prod g :=
by haveI := classical.prop_decidable; exact
calc s.prod f = (s.filter $ λx, f x ≠ 1).prod f :
(prod_subset (filter_subset _) $ by simp {contextual:=tt}).symm
... = (t.filter $ λx, g x ≠ 1).prod g :
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₂₂, hi₂ a₁ a₂ _ _ _ _)
(assume b hb, (mem_filter.mp hb).elim $ λh₁ h₂,
let ⟨a, ha₁, ha₂, eq⟩ := hi₃ b h₁ h₂ in ⟨a, mem_filter.mpr ⟨ha₁, ha₂⟩, eq⟩)
... = t.prod g :
(prod_subset (filter_subset _) $ by simp {contextual:=tt})
@[to_additive finset.exists_ne_zero_of_sum_ne_zero]
lemma exists_ne_one_of_prod_ne_one : s.prod f ≠ 1 → ∃a∈s, f a ≠ 1 :=
by haveI := classical.dec_eq α; exact
finset.induction_on s (by simp) (assume a s has ih h,
classical.by_cases
(assume ha : f a = 1,
have s.prod f ≠ 1, by simpa [ha, has] using h,
let ⟨a, ha, hfa⟩ := ih this in
⟨a, mem_insert_of_mem ha, hfa⟩)
(assume hna : f a ≠ 1,
⟨a, mem_insert_self _ _, hna⟩))
@[to_additive finset.sum_range_succ]
lemma prod_range_succ (f : ℕ → β) (n : ℕ) :
(range (nat.succ n)).prod f = f n * (range n).prod f := by simp
lemma prod_range_succ' (f : ℕ → β) :
∀ n : ℕ, (range (nat.succ n)).prod f = (range n).prod (f ∘ nat.succ) * f 0
| 0 := by simp
| (n + 1) := by rw [prod_range_succ (λ m, f (nat.succ m)), mul_assoc, ← prod_range_succ'];
exact prod_range_succ _ _
@[simp] lemma prod_const [decidable_eq α] (b : β) : s.prod (λ a, b) = b ^ s.card :=
finset.induction_on s rfl (by simp [pow_add, mul_comm] {contextual := tt})
end comm_monoid
@[simp] lemma sum_const [add_comm_monoid β] [decidable_eq α] (b : β) :
s.sum (λ a, b) = add_monoid.smul s.card b :=
@prod_const _ (multiplicative β) _ _ _ _
attribute [to_additive finset.sum_const] prod_const
lemma sum_range_succ' [add_comm_monoid β] (f : ℕ → β) :
∀ n : ℕ, (range (nat.succ n)).sum f = (range n).sum (f ∘ nat.succ) + f 0 :=
@prod_range_succ' (multiplicative β) _ _
attribute [to_additive finset.sum_range_succ'] prod_range_succ'
section comm_group
variables [comm_group β]
@[simp, to_additive finset.sum_neg_distrib]
lemma prod_inv_distrib : s.prod (λx, (f x)⁻¹) = (s.prod f)⁻¹ :=
prod_hom has_inv.inv one_inv mul_inv
end comm_group
@[simp] theorem card_sigma {σ : α → Type*} (s : finset α) (t : Π a, finset (σ a)) :
card (s.sigma t) = s.sum (λ a, card (t a)) :=
multiset.card_sigma _ _
end finset
namespace finset
variables {s s₁ s₂ : finset α} {f g : α → β} {b : β} {a : α}
@[simp] lemma sum_sub_distrib [add_comm_group β] : s.sum (λx, f x - g x) = s.sum f - s.sum g :=
by simp [sum_add_distrib]
section ordered_cancel_comm_monoid
variables [decidable_eq α] [ordered_cancel_comm_monoid β]
lemma sum_le_sum : (∀x∈s, f x ≤ g x) → s.sum f ≤ s.sum g :=
finset.induction_on s (by simp) $ assume a s ha ih h,
have f a + s.sum f ≤ g a + s.sum g,
from add_le_add (h _ (mem_insert_self _ _)) (ih $ assume x hx, h _ $ mem_insert_of_mem hx),
by simp [*]
lemma zero_le_sum (h : ∀x∈s, 0 ≤ f x) : 0 ≤ s.sum f := le_trans (by simp) (sum_le_sum h)
lemma sum_le_zero (h : ∀x∈s, f x ≤ 0) : s.sum f ≤ 0 := le_trans (sum_le_sum h) (by simp)
end ordered_cancel_comm_monoid
section semiring
variables [semiring β]
lemma sum_mul : s.sum f * b = s.sum (λx, f x * b) :=
(sum_hom (λx, x * b) (zero_mul b) (assume a c, add_mul a c b)).symm
lemma mul_sum : b * s.sum f = s.sum (λx, b * f x) :=
(sum_hom (λx, b * x) (mul_zero b) (assume a c, mul_add b a c)).symm
end semiring
section comm_semiring
variables [decidable_eq α] [comm_semiring β]
lemma prod_eq_zero (ha : a ∈ s) (h : f a = 0) : s.prod f = 0 :=
calc s.prod f = (insert a (erase s a)).prod f : by simp [ha, insert_erase]
... = 0 : by simp [h]
lemma prod_sum {δ : α → Type*} [∀a, decidable_eq (δ a)]
{s : finset α} {t : Πa, finset (δ a)} {f : Πa, δ a → β} :
s.prod (λa, (t a).sum (λb, f a b)) =
(s.pi t).sum (λp, s.attach.prod (λx, f x.1 (p x.1 x.2))) :=
begin
induction s using finset.induction with a s ha ih,
{ simp },
{ have h₁ : ∀x ∈ t a, ∀y∈t a, ∀h : x ≠ y,
image (pi.cons s a x) (pi s t) ∩ image (pi.cons s a y) (pi s t) = ∅,
{ assume x hx y hy h,
apply eq_empty_of_forall_not_mem,
simp,
assume p₁ p₂ hp eq p₃ hp₃ eq', subst eq',
have : pi.cons s a x p₂ a (mem_insert_self _ _) = pi.cons s a y p₃ a (mem_insert_self _ _),
{ rw [eq] },
rw [pi.cons_same, pi.cons_same] at this,
exact h this },
have h₂ : ∀b:δ a, ∀p₁∈pi s t, ∀p₂∈pi s t, pi.cons s a b p₁ = pi.cons s a b p₂ → p₁ = p₂, from
assume b p₁ h₁ p₂ h₂ eq, injective_pi_cons ha eq,
have h₃ : ∀(v:{x // x ∈ s}) (b:δ a) (h : v.1 ∈ insert a s) (p : Πa, a ∈ s → δ a),
pi.cons s a b p v.1 h = p v.1 v.2, from
assume v b h p, pi.cons_ne $ assume eq, ha $ eq.symm ▸ v.2,
simp [ha, ih, sum_bind h₁, sum_image (h₂ _), sum_mul, h₃],
simp [mul_sum] }
end
end comm_semiring
section integral_domain /- add integral_semi_domain to support nat and ennreal -/
variables [decidable_eq α] [integral_domain β]
lemma prod_eq_zero_iff : s.prod f = 0 ↔ (∃a∈s, f a = 0) :=
finset.induction_on s (by simp)
begin
intros a s,
rw [bex_def, bex_def, exists_mem_insert],
simp [mul_eq_zero_iff_eq_zero_or_eq_zero] {contextual := tt}
end
end integral_domain
section ordered_comm_monoid
variables [decidable_eq α] [ordered_comm_monoid β]
lemma sum_le_sum' : (∀x∈s, f x ≤ g x) → s.sum f ≤ s.sum g :=
finset.induction_on s (by simp; refl) $ assume a s ha ih h,
have f a + s.sum f ≤ g a + s.sum g,
from add_le_add' (h _ (mem_insert_self _ _)) (ih $ assume x hx, h _ $ mem_insert_of_mem hx),
by simp [*]
lemma zero_le_sum' (h : ∀x∈s, 0 ≤ f x) : 0 ≤ s.sum f := le_trans (by simp) (sum_le_sum' h)
lemma sum_le_zero' (h : ∀x∈s, f x ≤ 0) : s.sum f ≤ 0 := le_trans (sum_le_sum' h) (by simp)
lemma sum_le_sum_of_subset_of_nonneg
(h : s₁ ⊆ s₂) (hf : ∀x∈s₂, x ∉ s₁ → 0 ≤ f x) : s₁.sum f ≤ s₂.sum f :=
calc s₁.sum f ≤ (s₂ \ s₁).sum f + s₁.sum f :
le_add_of_nonneg_left' $ zero_le_sum' $ by simp [hf] {contextual := tt}
... = (s₂ \ s₁ ∪ s₁).sum f : (sum_union (sdiff_inter_self _ _)).symm
... = s₂.sum f : by rw [sdiff_union_of_subset h]
lemma sum_eq_zero_iff_of_nonneg : (∀x∈s, 0 ≤ f x) → (s.sum f = 0 ↔ ∀x∈s, f x = 0) :=
finset.induction_on s (by simp) $
by simp [or_imp_distrib, forall_and_distrib, zero_le_sum' ,
add_eq_zero_iff_eq_zero_and_eq_zero_of_nonneg_of_nonneg'] {contextual := tt}
lemma single_le_sum (hf : ∀x∈s, 0 ≤ f x) {a} (h : a ∈ s) : f a ≤ s.sum f :=
by simpa using show (singleton a).sum f ≤ s.sum f,
from sum_le_sum_of_subset_of_nonneg
(λ x e, (mem_singleton.1 e).symm ▸ h) (λ x h _, hf x h)
end ordered_comm_monoid
section canonically_ordered_monoid
variables [decidable_eq α] [canonically_ordered_monoid β] [@decidable_rel β (≤)]
lemma sum_le_sum_of_subset (h : s₁ ⊆ s₂) : s₁.sum f ≤ s₂.sum f :=
sum_le_sum_of_subset_of_nonneg h $ assume x h₁ h₂, zero_le _
lemma sum_le_sum_of_ne_zero (h : ∀x∈s₁, f x ≠ 0 → x ∈ s₂) : s₁.sum f ≤ s₂.sum f :=
calc s₁.sum f = (s₁.filter (λx, f x = 0)).sum f + (s₁.filter (λx, f x ≠ 0)).sum f :
by rw [←sum_union, filter_union_filter_neg_eq]; apply filter_inter_filter_neg_eq
... ≤ s₂.sum f : add_le_of_nonpos_of_le'
(sum_le_zero' $ by simp {contextual:=tt})
(sum_le_sum_of_subset $ by simp [subset_iff, *] {contextual:=tt})
end canonically_ordered_monoid
section discrete_linear_ordered_field
variables [discrete_linear_ordered_field α] [decidable_eq β]
lemma abs_sum_le_sum_abs {f : β → α} {s : finset β} : abs (s.sum f) ≤ s.sum (λa, abs (f a)) :=
finset.induction_on s (by simp [abs_zero]) $
assume a s has ih,
calc abs (sum (insert a s) f) ≤ abs (f a) + abs (sum s f) :
by simp [has]; exact abs_add_le_abs_add_abs _ _
... ≤ abs (f a) + s.sum (λa, abs (f a)) : add_le_add (le_refl _) ih
... ≤ sum (insert a s) (λ (a : β), abs (f a)) : by simp [has]
end discrete_linear_ordered_field
@[simp] lemma card_pi [decidable_eq α] {δ : α → Type*}
(s : finset α) (t : Π a, finset (δ a)) :
(s.pi t).card = s.prod (λ a, card (t a)) :=
multiset.card_pi _ _
end finset
section group
open list
variables [group α] [group β]
theorem is_group_hom.prod (f : α → β) [is_group_hom f] (l : list α) :
f (prod l) = prod (map f l) :=
by induction l; simp [*, is_group_hom.mul f, is_group_hom.one f]
theorem is_group_anti_hom.prod (f : α → β) [is_group_anti_hom f] (l : list α) :
f (prod l) = prod (map f (reverse l)) :=
by induction l; simp [*, is_group_anti_hom.mul f, is_group_anti_hom.one f]
theorem inv_prod : ∀ l : list α, (prod l)⁻¹ = prod (map (λ x, x⁻¹) (reverse l)) :=
λ l, @is_group_anti_hom.prod _ _ _ _ _ inv_is_group_anti_hom l -- TODO there is probably a cleaner proof of this
end group
namespace multiset
variables [decidable_eq α]
@[simp] lemma to_finset_sum_count_eq (s : multiset α) :
s.to_finset.sum (λa, s.count a) = s.card :=
multiset.induction_on s (by simp)
(assume a s ih,
calc (to_finset (a :: s)).sum (λx, count x (a :: s)) =
(to_finset (a :: s)).sum (λx, (if x = a then 1 else 0) + count x s) :
by congr; funext x; split_ifs; simp [h, nat.one_add]
... = card (a :: s) :
begin
by_cases a ∈ s.to_finset,
{ have : (to_finset s).sum (λx, ite (x = a) 1 0) = ({a}:finset α).sum (λx, ite (x = a) 1 0),
{ apply (finset.sum_subset _ _).symm,
{ simp [finset.subset_iff, *] at * },
{ simp [if_neg] {contextual := tt} } },
simp [h, ih, this, finset.sum_add_distrib] },
{ have : a ∉ s, by simp * at *,
have : (to_finset s).sum (λx, ite (x = a) 1 0) = (to_finset s).sum (λx, 0), from
finset.sum_congr rfl begin assume a ha, split_ifs; simp [*] at * end,
simp [*, finset.sum_add_distrib], }
end)
end multiset
|
30dd76f0c3313cd5331ada56c6d976be3ccda18f | 8b8ab1eba67c5c35b51ab27fcf8151aab660e44d | /src/support.lean | a7cdf21c9263b28eb5c2cd2551d9b497729189d7 | [] | no_license | PatrickMassot/lean-scratchpad | ab5e89b69034cc699bf234852822acf74b50f2b9 | 03eec3bfabfc218b79dcbe7c7712bfa024a02625 | refs/heads/master | 1,626,995,046,509 | 1,538,577,853,000 | 1,538,577,853,000 | 115,135,779 | 1 | 2 | null | 1,522,993,992,000 | 1,513,962,994,000 | Lean | UTF-8 | Lean | false | false | 7,908 | lean | import analysis.topology.topological_space
import data.set
import data.fin
import tactic.find
import tactic.ring
import algebra.group
import homeos
import invariant_norms
import bigop
noncomputable theory
local attribute [instance] classical.prop_decidable
set_option pp.coercions false
open set function equiv list
def fix {X} (f : X → X) := { x : X | f x = x }
lemma fix_stable {X} (f : X → X) : f '' fix f = fix f :=
begin
apply subset.antisymm,
{ intros x H,
rcases H with ⟨y, y_in_fix, f_y_x⟩,
simp [fix] at y_in_fix,
rw y_in_fix at f_y_x,
finish },
{ intros x H,
existsi x,
finish }
end
variables {X : Type} [topological_space X] (f : X → X)
def supp := closure (-fix f)
lemma compl_supp_eq_interior_fix : -supp f = interior (fix f) :=
calc -supp f = -closure (-fix f) : rfl
... = -(-interior (fix f)) : by rw closure_compl
... = interior (fix f) : by rw compl_compl
lemma compl_supp_subset_fix : -supp f ⊆ fix f :=
calc -supp f = interior (fix f) : by rw compl_supp_eq_interior_fix
... ⊆ fix f : interior_subset
lemma fix_of_not_in_supp {x : X} {f : homeo X X} (h : x ∉ supp f) : f x = x :=
compl_supp_subset_fix f h
lemma mem_supp_or_fix (x) : x ∈ supp f ∨ f x = x :=
or_iff_not_imp_left.2 (λ h, compl_supp_subset_fix _ h)
-- Recall: or_iff_not_imp_left : a ∨ b ↔ (¬ a → b)
lemma stable_support (f : homeo X X) : f '' supp f = supp f :=
calc
f '' supp f = f '' (closure (-fix f)) : rfl
... = closure (f '' (-fix f)) : by rw f.image_closure
... = closure (-(f '' (fix f))) : by rw f.image_compl
... = closure (-(fix f)) : by rw fix_stable
lemma fundamental {f g : homeo X X} (H : supp f ∩ supp g = ∅) : f * g = g * f :=
begin
ext x,
by_cases H' : x ∈ supp f ∨ x ∈ supp g,
{ -- Here we assume H' : x ∈ supp f ∨ x ∈ supp g
wlog h : x ∈ supp f using f g,
have x_not_supp_g : x ∉ supp g := (subset_compl_iff_disjoint.2 H) h,
have f_x_supp_f : f x ∈ supp f,
{ have : f x ∈ f '' supp f := mem_image_of_mem f h,
finish [stable_support f] },
have : f x ∉ supp g := (subset_compl_iff_disjoint.2 H) f_x_supp_f,
finish [fix_of_not_in_supp] },
{ -- Now we assume H' : ¬(x ∈ supp f ∨ x ∈ supp g)
rw not_or_distrib at H',
finish [fix_of_not_in_supp] }
end
/- Calc version of above proof
lemma fundamental'' {f g : homeo X X} (H : supp f ∩ supp g = ∅) : f * g = g * f :=
begin
ext x,
by_cases H' : x ∈ supp f ∨ x ∈ supp g,
{ -- Here we assume H' : x ∈ supp f ∨ x ∈ supp g
wlog h : x ∈ supp f using f g,
exact calc
(f * g) x = f (g x) : by simp
... = f x : by { have x_not_supp_g : x ∉ supp g := (subset_compl_iff_disjoint.2 H) h,
finish [fix_of_not_in_supp] }
... = g (f x) : by { have f_x_supp_f : f x ∈ supp f,
{ have : f x ∈ f '' supp f := mem_image_of_mem f h,
finish [stable_support f] },
have : f x ∉ supp g := (subset_compl_iff_disjoint.2 H) f_x_supp_f,
finish [fix_of_not_in_supp] }
... = (g * f) x : by simp },
{ -- Now we assume H' : ¬(x ∈ supp f ∨ x ∈ supp g)
rw not_or_distrib at H',
finish [fix_of_not_in_supp] }
end
-/
lemma supp_conj (f g : homeo X X) : supp (conj g f : homeo X X) = g '' supp f :=
begin
unfold supp,
rw homeo.image_closure,
congr_n 1,
apply set.ext,
intro x,
rw mem_image_iff_of_inverse g.left_inverse g.right_inverse,
apply not_congr,
dsimp [conj],
exact calc
(g * f * g⁻¹) x = x
↔ g⁻¹ (g (f (g⁻¹ x))) = g⁻¹ x : by simp [(g⁻¹).bijective.1.eq_iff]
... ↔ (f (g⁻¹ x)) = g⁻¹ x : by rw [← aut_mul_val, mul_left_inv]; simp
end
local notation `〚`a, b`〛` := comm a b -- type with \llb / \rrb
lemma numbers' {N} (H : N > 0) : ∀ i, i ∈ range' 0 (N - 1 + 1 - 0) → N - (N - 1 - i) = i + 1 :=
begin
intros i hyp,
simp [nat.add_sub_cancel' H] at hyp,
rw [nat.sub_sub, add_comm, nat.sub_sub_self hyp.2]
end
/- replace hyp := hyp.right,
have := nat.succ_le_of_lt hyp,
rw [← int.coe_nat_inj', int.coe_nat_sub, int.coe_nat_sub, int.coe_nat_sub],
{ ring },
{ assumption },
{ sorry },
{ sorry }-/
lemma trading_of_displaced (g a b : homeo X X)
(supp_hyp : supp a ∩ g '' supp b = ∅) :
∃ c d e f : homeo X X, 〚a, b〛 = (conj c g⁻¹)*(conj d g)*(conj e g⁻¹)*(conj f g) :=
begin
apply commutator_trading,
rw ←supp_conj at supp_hyp,
rw [commuting, fundamental supp_hyp]
end
--set_option trace.class_instances true
--set_option pp.all true
lemma commutators_crunching (U : set X) (φ f : homeo X X)
(wandering_hyp : ∀ i j : ℕ, i ≠ j → ⇑(φ^i) '' U ∩ ⇑(φ^j) '' U = ∅)
(N : ℕ) (aa : ℕ → homeo X X) (b : ℕ → homeo X X)
(supp_hyp : ∀ k : ℕ, supp (aa k) ⊆ U ∧ supp (b k) ⊆ U)
(comm_hyp : f = Π_(i = 1..N) 〚aa i, b i〛) :
∃ A B C D : homeo X X, f = 〚A, B〛 * 〚C, D〛 :=
begin
by_cases H : N = 0,
{ rw big.empty_range at comm_hyp,
repeat { existsi (1 : homeo X X) },
simp [comm_hyp, comm],
simp [H, zero_lt_one] },
{ replace H : N ≥ 1 := nat.succ_le_of_lt (nat.pos_of_ne_zero H),
let G := homeo X X,
let g := λ i, (Π_(j=1..i) 〚aa i, b i〛: G),
let F := (Π_(i=0..(N-1)) conj (φ^i) $ g (N-i) : G),
have commute : ∀ (i j k l : ℕ), i ≠ j →
(conj (φ^i) (aa k))*(conj (φ^j) (b l)) = (conj (φ^j) (b l))*(conj (φ^i) (aa k)),
begin
intros i j k l i_neq_j,
apply fundamental,
repeat { rw supp_conj },
specialize wandering_hyp i j i_neq_j,
have supp_a := set.image_subset (φ^i : G) (supp_hyp k).left,
have supp_b := set.image_subset (φ^j : G) (supp_hyp l).right,
have := set.inter_subset_inter supp_a supp_b,
rw wandering_hyp at this,
exact set.eq_empty_of_subset_empty this
end,
have numbers :=
calc 1 + (1 + (N - 1)) - 1 = (1 + (N-1)) + 1 - 1 : by simp
... = 1 + (N-1) : by rw nat.add_sub_cancel
... = N : by rw nat.add_sub_cancel' H,
have cφf:= calc
conj φ F = conj φ (Π_(i=0..(N-1)) conj (φ^i) $ g (N-i)) : rfl
... = Π_(i=0..(N-1)) conj φ (conj (φ^i) $ g (N-i)) : by rw (big.mph _ _ _ _ _ (is_group_hom.mul (conj φ)) (is_group_hom.one _))
... = Π_(i=0..(N-1)) conj (φ^(i+1)) $ g (N-i) : by simp[pow_succ]
... = Π_(i=1..N) conj (φ^i) $ g (N-(i-1)) : by { rw big.shift _ _ _ _ 0 (N-1) 1, simp, rw numbers, apply big.ext, simp, intros i hyp, rw nat.add_sub_cancel' (mem_range'.1 hyp).left},
have := (@inv_is_group_anti_hom G _ _).mul,
let gg := λ i, (conj (φ^i) $ (g (N-i))⁻¹ : G),
have Finv := calc
F⁻¹ = (Π_(i=0..(N-1)) conj (φ^i) $ g (N-i) : G)⁻¹ : rfl
... = (λ (g : G), g⁻¹) (Π_(i=0..(N-1)) conj (φ^i) $ g (N-i) : G) : rfl
... = (Π_(i=0..(N-1)) (conj (φ^(N-1-i)) $ g (N - (N - 1 - i)))⁻¹ : G) : by { rw big.range_anti_mph _ (1:G) _ _ _ _ this ; simp }
... = (Π_(i=0..(N-1)) (conj (φ^(N-1-i)) $ g (i+1))⁻¹ : G) : by { apply big.ext, { simp }, intros i hyp, rw numbers' H i hyp }
... = (Π_(i=0..(N-1)) conj (φ^(N-1-i)) $ (g (i+1))⁻¹ : G) : by { apply big.ext, simp, intros, rw ←is_group_hom.inv (conj _), apply_instance }
... = (Π_(i=0..(N-1)) conj (φ^i) $ (g (N-i))⁻¹ : G) : by { rw big.reverse_range_of_commute, apply big.ext, simp, simp, sorry, sorry }
... = (Π_(i=0..(N-1)) gg i)*(gg N) : by { dsimp [g], simp[big.nil], sorry }
... = (Π_(i=0..N) gg i) : by{ have := (big.concat_true (*) (1:G) (range' 0 (N - 1 + 1 - 0)) gg N), cc, },
have : conj (φ^N) (g 0)⁻¹ = 1,
by { dsimp [g], simp[big.nil] } ,
sorry }
end
#check big.concat_true
#check int.cast_inj |
2d0208f886dccd80b532386c0a5382e66471882c | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /src/Lean/Meta/Tactic/Generalize.lean | 2483524dc884e648cac56b873b2af8c663307eea | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | EdAyers/lean4 | 57ac632d6b0789cb91fab2170e8c9e40441221bd | 37ba0df5841bde51dbc2329da81ac23d4f6a4de4 | refs/heads/master | 1,676,463,245,298 | 1,660,619,433,000 | 1,660,619,433,000 | 183,433,437 | 1 | 0 | Apache-2.0 | 1,657,612,672,000 | 1,556,196,574,000 | Lean | UTF-8 | Lean | false | false | 3,333 | 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.KAbstract
import Lean.Meta.Tactic.Util
import Lean.Meta.Tactic.Intro
namespace Lean.Meta
/-- The `generalize` tactic takes arguments of the form `h : e = x` -/
structure GeneralizeArg where
expr : Expr
xName? : Option Name := none
hName? : Option Name := none
deriving Inhabited
/--
Telescopic `generalize` tactic. It can simultaneously generalize many terms.
It uses `kabstract` to occurrences of the terms that need to be generalized.
-/
private partial def generalizeCore (mvarId : MVarId) (args : Array GeneralizeArg) : MetaM (Array FVarId × MVarId) :=
mvarId.withContext do
mvarId.checkNotAssigned `generalize
let tag ← mvarId.getTag
let target ← instantiateMVars (← mvarId.getType)
let rec go (i : Nat) : MetaM Expr := do
if i < args.size then
let arg := args[i]!
let e ← instantiateMVars arg.expr
let eType ← instantiateMVars (← inferType e)
let type ← go (i+1)
let xName ← if let some xName := arg.xName? then pure xName else mkFreshUserName `x
return Lean.mkForall xName BinderInfo.default eType (← kabstract type e)
else
return target
let targetNew ← go 0
unless (← isTypeCorrect targetNew) do
throwTacticEx `generalize mvarId m!"result is not type correct{indentExpr targetNew}"
let es := args.map (·.expr)
if !args.any fun arg => arg.hName?.isSome then
let mvarNew ← mkFreshExprSyntheticOpaqueMVar targetNew tag
mvarId.assign (mkAppN mvarNew es)
mvarNew.mvarId!.introNP args.size
else
let (rfls, targetNew) ← forallBoundedTelescope targetNew args.size fun xs type => do
let rec go' (i : Nat) : MetaM (List Expr × Expr) := do
if i < xs.size then
let arg := args[i]!
if let some hName := arg.hName? then
let xType ← inferType xs[i]!
let e ← instantiateMVars arg.expr
let eType ← instantiateMVars (← inferType e)
let (hType, r) ← if (← isDefEq xType eType) then
pure (← mkEq e xs[i]!, ← mkEqRefl e)
else
pure (← mkHEq e xs[i]!, ← mkHEqRefl e)
let (rs, type) ← go' (i+1)
return (r :: rs, mkForall hName BinderInfo.default hType type)
else
go' (i+1)
else
return ([], type)
let (rfls, type) ← go' 0
return (rfls, ← mkForallFVars xs type)
let mvarNew ← mkFreshExprSyntheticOpaqueMVar targetNew tag
mvarId.assign (mkAppN (mkAppN mvarNew es) rfls.toArray)
mvarNew.mvarId!.introNP (args.size + rfls.length)
/--
Telescopic `generalize` tactic. It can simultaneously generalize many terms.
It uses `kabstract` to occurrences of the terms that need to be generalized.
-/
def _root_.Lean.MVarId.generalize (mvarId : MVarId) (args : Array GeneralizeArg) : MetaM (Array FVarId × MVarId) :=
generalizeCore mvarId args
@[deprecated MVarId.generalize]
def generalize (mvarId : MVarId) (args : Array GeneralizeArg) : MetaM (Array FVarId × MVarId) :=
generalizeCore mvarId args
end Lean.Meta
|
f6543cead04cd47d520e32cbf693709b52d40101 | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/algebra/algebra/basic.lean | b04697f5de8a4aadb1c7a2eb13bc6a7257c0c460 | [
"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 | 64,275 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Yury Kudryashov
-/
import algebra.module.basic
import algebra.ring.aut
import algebra.ring.ulift
import algebra.module.ulift
import linear_algebra.span
import tactic.abel
/-!
# Algebras over commutative semirings
In this file we define associative unital `algebra`s over commutative (semi)rings, algebra
homomorphisms `alg_hom`, and algebra equivalences `alg_equiv`.
`subalgebra`s are defined in `algebra.algebra.subalgebra`.
For the category of `R`-algebras, denoted `Algebra R`, see the file
`algebra/category/Algebra/basic.lean`.
See the implementation notes for remarks about non-associative and non-unital algebras.
## Main definitions:
* `algebra R A`: the algebra typeclass.
* `alg_hom R A B`: the type of `R`-algebra morphisms from `A` to `B`.
* `alg_equiv R A B`: the type of `R`-algebra isomorphisms between `A` to `B`.
* `algebra_map R A : R →+* A`: the canonical map from `R` to `A`, as a `ring_hom`. This is the
preferred spelling of this map.
* `algebra.linear_map R A : R →ₗ[R] A`: the canonical map from `R` to `A`, as a `linear_map`.
* `algebra.of_id R A : R →ₐ[R] A`: the canonical map from `R` to `A`, as n `alg_hom`.
* Instances of `algebra` in this file:
* `algebra.id`
* `pi.algebra`
* `prod.algebra`
* `algebra_nat`
* `algebra_int`
* `algebra_rat`
* `mul_opposite.algebra`
* `module.End.algebra`
## Notations
* `A →ₐ[R] B` : `R`-algebra homomorphism from `A` to `B`.
* `A ≃ₐ[R] B` : `R`-algebra equivalence from `A` to `B`.
## Implementation notes
Given a commutative (semi)ring `R`, there are two ways to define an `R`-algebra structure on a
(possibly noncommutative) (semi)ring `A`:
* By endowing `A` with a morphism of rings `R →+* A` denoted `algebra_map R A` which lands in the
center of `A`.
* By requiring `A` be an `R`-module such that the action associates and commutes with multiplication
as `r • (a₁ * a₂) = (r • a₁) * a₂ = a₁ * (r • a₂)`.
We define `algebra R A` in a way that subsumes both definitions, by extending `has_smul R A` and
requiring that this scalar action `r • x` must agree with left multiplication by the image of the
structure morphism `algebra_map R A r * x`.
As a result, there are two ways to talk about an `R`-algebra `A` when `A` is a semiring:
1. ```lean
variables [comm_semiring R] [semiring A]
variables [algebra R A]
```
2. ```lean
variables [comm_semiring R] [semiring A]
variables [module R A] [smul_comm_class R A A] [is_scalar_tower R A A]
```
The first approach implies the second via typeclass search; so any lemma stated with the second set
of arguments will automatically apply to the first set. Typeclass search does not know that the
second approach implies the first, but this can be shown with:
```lean
example {R A : Type*} [comm_semiring R] [semiring A]
[module R A] [smul_comm_class R A A] [is_scalar_tower R A A] : algebra R A :=
algebra.of_module smul_mul_assoc mul_smul_comm
```
The advantage of the first approach is that `algebra_map R A` is available, and `alg_hom R A B` and
`subalgebra R A` can be used. For concrete `R` and `A`, `algebra_map R A` is often definitionally
convenient.
The advantage of the second approach is that `comm_semiring R`, `semiring A`, and `module R A` can
all be relaxed independently; for instance, this allows us to:
* Replace `semiring A` with `non_unital_non_assoc_semiring A` in order to describe non-unital and/or
non-associative algebras.
* Replace `comm_semiring R` and `module R A` with `comm_group R'` and `distrib_mul_action R' A`,
which when `R' = Rˣ` lets us talk about the "algebra-like" action of `Rˣ` on an
`R`-algebra `A`.
While `alg_hom R A B` cannot be used in the second approach, `non_unital_alg_hom R A B` still can.
You should always use the first approach when working with associative unital algebras, and mimic
the second approach only when you need to weaken a condition on either `R` or `A`.
-/
universes u v w u₁ v₁
open_locale big_operators
section prio
-- We set this priority to 0 later in this file
set_option extends_priority 200 /- control priority of
`instance [algebra R A] : has_smul R A` -/
/--
An associative unital `R`-algebra is a semiring `A` equipped with a map into its center `R → A`.
See the implementation notes in this file for discussion of the details of this definition.
-/
@[nolint has_nonempty_instance]
class algebra (R : Type u) (A : Type v) [comm_semiring R] [semiring A]
extends has_smul R A, R →+* A :=
(commutes' : ∀ r x, to_fun r * x = x * to_fun r)
(smul_def' : ∀ r x, r • x = to_fun r * x)
end prio
/-- Embedding `R →+* A` given by `algebra` structure. -/
def algebra_map (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] : R →+* A :=
algebra.to_ring_hom
/-- Creating an algebra from a morphism to the center of a semiring. -/
def ring_hom.to_algebra' {R S} [comm_semiring R] [semiring S] (i : R →+* S)
(h : ∀ c x, i c * x = x * i c) :
algebra R S :=
{ smul := λ c x, i c * x,
commutes' := h,
smul_def' := λ c x, rfl,
to_ring_hom := i}
/-- Creating an algebra from a morphism to a commutative semiring. -/
def ring_hom.to_algebra {R S} [comm_semiring R] [comm_semiring S] (i : R →+* S) :
algebra R S :=
i.to_algebra' $ λ _, mul_comm _
lemma ring_hom.algebra_map_to_algebra {R S} [comm_semiring R] [comm_semiring S]
(i : R →+* S) :
@algebra_map R S _ _ i.to_algebra = i :=
rfl
namespace algebra
variables {R : Type u} {S : Type v} {A : Type w} {B : Type*}
/-- Let `R` be a commutative semiring, let `A` be a semiring with a `module R` structure.
If `(r • 1) * x = x * (r • 1) = r • x` for all `r : R` and `x : A`, then `A` is an `algebra`
over `R`.
See note [reducible non-instances]. -/
@[reducible]
def of_module' [comm_semiring R] [semiring A] [module R A]
(h₁ : ∀ (r : R) (x : A), (r • 1) * x = r • x)
(h₂ : ∀ (r : R) (x : A), x * (r • 1) = r • x) : algebra R A :=
{ to_fun := λ r, r • 1,
map_one' := one_smul _ _,
map_mul' := λ r₁ r₂, by rw [h₁, mul_smul],
map_zero' := zero_smul _ _,
map_add' := λ r₁ r₂, add_smul r₁ r₂ 1,
commutes' := λ r x, by simp only [h₁, h₂],
smul_def' := λ r x, by simp only [h₁] }
/-- Let `R` be a commutative semiring, let `A` be a semiring with a `module R` structure.
If `(r • x) * y = x * (r • y) = r • (x * y)` for all `r : R` and `x y : A`, then `A`
is an `algebra` over `R`.
See note [reducible non-instances]. -/
@[reducible]
def of_module [comm_semiring R] [semiring A] [module R A]
(h₁ : ∀ (r : R) (x y : A), (r • x) * y = r • (x * y))
(h₂ : ∀ (r : R) (x y : A), x * (r • y) = r • (x * y)) : algebra R A :=
of_module' (λ r x, by rw [h₁, one_mul]) (λ r x, by rw [h₂, mul_one])
section semiring
variables [comm_semiring R] [comm_semiring S]
variables [semiring A] [algebra R A] [semiring B] [algebra R B]
/-- We keep this lemma private because it picks up the `algebra.to_has_smul` instance
which we set to priority 0 shortly. See `smul_def` below for the public version. -/
private lemma smul_def'' (r : R) (x : A) : r • x = algebra_map R A r * x :=
algebra.smul_def' r x
/--
To prove two algebra structures on a fixed `[comm_semiring R] [semiring A]` agree,
it suffices to check the `algebra_map`s agree.
-/
-- We'll later use this to show `algebra ℤ M` is a subsingleton.
@[ext]
lemma algebra_ext {R : Type*} [comm_semiring R] {A : Type*} [semiring A] (P Q : algebra R A)
(w : ∀ (r : R), by { haveI := P, exact algebra_map R A r } =
by { haveI := Q, exact algebra_map R A r }) :
P = Q :=
begin
unfreezingI { rcases P with ⟨⟨P⟩⟩, rcases Q with ⟨⟨Q⟩⟩ },
congr,
{ funext r a,
replace w := congr_arg (λ s, s * a) (w r),
simp only [←smul_def''] at w,
apply w, },
{ ext r,
exact w r, },
{ apply proof_irrel_heq, },
{ apply proof_irrel_heq, },
end
@[priority 200] -- see Note [lower instance priority]
instance to_module : module R A :=
{ one_smul := by simp [smul_def''],
mul_smul := by simp [smul_def'', mul_assoc],
smul_add := by simp [smul_def'', mul_add],
smul_zero := by simp [smul_def''],
add_smul := by simp [smul_def'', add_mul],
zero_smul := by simp [smul_def''] }
-- From now on, we don't want to use the following instance anymore.
-- Unfortunately, leaving it in place causes deterministic timeouts later in mathlib.
attribute [instance, priority 0] algebra.to_has_smul
lemma smul_def (r : R) (x : A) : r • x = algebra_map R A r * x :=
algebra.smul_def' r x
lemma algebra_map_eq_smul_one (r : R) : algebra_map R A r = r • 1 :=
calc algebra_map R A r = algebra_map R A r * 1 : (mul_one _).symm
... = r • 1 : (algebra.smul_def r 1).symm
lemma algebra_map_eq_smul_one' : ⇑(algebra_map R A) = λ r, r • (1 : A) :=
funext algebra_map_eq_smul_one
/-- `mul_comm` for `algebra`s when one element is from the base ring. -/
theorem commutes (r : R) (x : A) : algebra_map R A r * x = x * algebra_map R A r :=
algebra.commutes' r x
/-- `mul_left_comm` for `algebra`s when one element is from the base ring. -/
theorem left_comm (x : A) (r : R) (y : A) :
x * (algebra_map R A r * y) = algebra_map R A r * (x * y) :=
by rw [← mul_assoc, ← commutes, mul_assoc]
/-- `mul_right_comm` for `algebra`s when one element is from the base ring. -/
theorem right_comm (x : A) (r : R) (y : A) :
(x * algebra_map R A r) * y = (x * y) * algebra_map R A r :=
by rw [mul_assoc, commutes, ←mul_assoc]
instance _root_.is_scalar_tower.right : is_scalar_tower R A A :=
⟨λ x y z, by rw [smul_eq_mul, smul_eq_mul, smul_def, smul_def, mul_assoc]⟩
/-- This is just a special case of the global `mul_smul_comm` lemma that requires less typeclass
search (and was here first). -/
@[simp] protected lemma mul_smul_comm (s : R) (x y : A) :
x * (s • y) = s • (x * y) :=
-- TODO: set up `is_scalar_tower.smul_comm_class` earlier so that we can actually prove this using
-- `mul_smul_comm s x y`.
by rw [smul_def, smul_def, left_comm]
/-- This is just a special case of the global `smul_mul_assoc` lemma that requires less typeclass
search (and was here first). -/
@[simp] protected lemma smul_mul_assoc (r : R) (x y : A) :
(r • x) * y = r • (x * y) :=
smul_mul_assoc r x y
@[simp]
lemma _root_.smul_algebra_map {α : Type*} [monoid α] [mul_distrib_mul_action α A]
[smul_comm_class α R A] (a : α) (r : R) : a • algebra_map R A r = algebra_map R A r :=
by rw [algebra_map_eq_smul_one, smul_comm a r (1 : A), smul_one]
section
variables {r : R} {a : A}
@[simp] lemma bit0_smul_one : bit0 r • (1 : A) = bit0 (r • (1 : A)) :=
by simp [bit0, add_smul]
lemma bit0_smul_one' : bit0 r • (1 : A) = r • 2 :=
by simp [bit0, add_smul, smul_add]
@[simp] lemma bit0_smul_bit0 : bit0 r • bit0 a = r • (bit0 (bit0 a)) :=
by simp [bit0, add_smul, smul_add]
@[simp] lemma bit0_smul_bit1 : bit0 r • bit1 a = r • (bit0 (bit1 a)) :=
by simp [bit0, add_smul, smul_add]
@[simp] lemma bit1_smul_one : bit1 r • (1 : A) = bit1 (r • (1 : A)) :=
by simp [bit1, add_smul]
lemma bit1_smul_one' : bit1 r • (1 : A) = r • 2 + 1 :=
by simp [bit1, bit0, add_smul, smul_add]
@[simp] lemma bit1_smul_bit0 : bit1 r • bit0 a = r • (bit0 (bit0 a)) + bit0 a :=
by simp [bit1, add_smul, smul_add]
@[simp] lemma bit1_smul_bit1 : bit1 r • bit1 a = r • (bit0 (bit1 a)) + bit1 a :=
by { simp only [bit0, bit1, add_smul, smul_add, one_smul], abel }
end
variables (R A)
/--
The canonical ring homomorphism `algebra_map R A : R →* A` for any `R`-algebra `A`,
packaged as an `R`-linear map.
-/
protected def linear_map : R →ₗ[R] A :=
{ map_smul' := λ x y, by simp [algebra.smul_def],
..algebra_map R A }
@[simp]
lemma linear_map_apply (r : R) : algebra.linear_map R A r = algebra_map R A r := rfl
lemma coe_linear_map : ⇑(algebra.linear_map R A) = algebra_map R A := rfl
instance id : algebra R R := (ring_hom.id R).to_algebra
variables {R A}
namespace id
@[simp] lemma map_eq_id : algebra_map R R = ring_hom.id _ := rfl
lemma map_eq_self (x : R) : algebra_map R R x = x := rfl
@[simp] lemma smul_eq_mul (x y : R) : x • y = x * y := rfl
end id
section punit
instance _root_.punit.algebra : algebra R punit :=
{ to_fun := λ x, punit.star,
map_one' := rfl,
map_mul' := λ _ _, rfl,
map_zero' := rfl,
map_add' := λ _ _, rfl,
commutes' := λ _ _, rfl,
smul_def' := λ _ _, rfl }
@[simp] lemma algebra_map_punit (r : R) : algebra_map R punit r = punit.star := rfl
end punit
section ulift
instance _root_.ulift.algebra : algebra R (ulift A) :=
{ to_fun := λ r, ulift.up (algebra_map R A r),
commutes' := λ r x, ulift.down_injective $ algebra.commutes r x.down,
smul_def' := λ r x, ulift.down_injective $ algebra.smul_def' r x.down,
.. ulift.module',
.. (ulift.ring_equiv : ulift A ≃+* A).symm.to_ring_hom.comp (algebra_map R A) }
lemma _root_.ulift.algebra_map_eq (r : R) :
algebra_map R (ulift A) r = ulift.up (algebra_map R A r) := rfl
@[simp] lemma _root_.ulift.down_algebra_map (r : R) :
(algebra_map R (ulift A) r).down = algebra_map R A r := rfl
end ulift
section prod
variables (R A B)
instance _root_.prod.algebra : algebra R (A × B) :=
{ commutes' := by { rintro r ⟨a, b⟩, dsimp, rw [commutes r a, commutes r b] },
smul_def' := by { rintro r ⟨a, b⟩, dsimp, rw [smul_def r a, smul_def r b] },
.. prod.module,
.. ring_hom.prod (algebra_map R A) (algebra_map R B) }
variables {R A B}
@[simp] lemma algebra_map_prod_apply (r : R) :
algebra_map R (A × B) r = (algebra_map R A r, algebra_map R B r) := rfl
end prod
/-- Algebra over a subsemiring. This builds upon `subsemiring.module`. -/
instance of_subsemiring (S : subsemiring R) : algebra S A :=
{ smul := (•),
commutes' := λ r x, algebra.commutes r x,
smul_def' := λ r x, algebra.smul_def r x,
.. (algebra_map R A).comp S.subtype }
lemma algebra_map_of_subsemiring (S : subsemiring R) :
(algebra_map S R : S →+* R) = subsemiring.subtype S := rfl
lemma coe_algebra_map_of_subsemiring (S : subsemiring R) :
(algebra_map S R : S → R) = subtype.val := rfl
lemma algebra_map_of_subsemiring_apply (S : subsemiring R) (x : S) :
algebra_map S R x = x := rfl
/-- Algebra over a subring. This builds upon `subring.module`. -/
instance of_subring {R A : Type*} [comm_ring R] [ring A] [algebra R A]
(S : subring R) : algebra S A :=
{ smul := (•),
.. algebra.of_subsemiring S.to_subsemiring,
.. (algebra_map R A).comp S.subtype }
lemma algebra_map_of_subring {R : Type*} [comm_ring R] (S : subring R) :
(algebra_map S R : S →+* R) = subring.subtype S := rfl
lemma coe_algebra_map_of_subring {R : Type*} [comm_ring R] (S : subring R) :
(algebra_map S R : S → R) = subtype.val := rfl
lemma algebra_map_of_subring_apply {R : Type*} [comm_ring R] (S : subring R) (x : S) :
algebra_map S R x = x := rfl
/-- Explicit characterization of the submonoid map in the case of an algebra.
`S` is made explicit to help with type inference -/
def algebra_map_submonoid (S : Type*) [semiring S] [algebra R S]
(M : submonoid R) : (submonoid S) :=
submonoid.map (algebra_map R S : R →* S) M
lemma mem_algebra_map_submonoid_of_mem {S : Type*} [semiring S] [algebra R S] {M : submonoid R}
(x : M) : (algebra_map R S x) ∈ algebra_map_submonoid S M :=
set.mem_image_of_mem (algebra_map R S) x.2
end semiring
section comm_semiring
variables [comm_semiring R]
lemma mul_sub_algebra_map_commutes [ring A] [algebra R A] (x : A) (r : R) :
x * (x - algebra_map R A r) = (x - algebra_map R A r) * x :=
by rw [mul_sub, ←commutes, sub_mul]
lemma mul_sub_algebra_map_pow_commutes [ring A] [algebra R A] (x : A) (r : R) (n : ℕ) :
x * (x - algebra_map R A r) ^ n = (x - algebra_map R A r) ^ n * x :=
begin
induction n with n ih,
{ simp },
{ rw [pow_succ, ←mul_assoc, mul_sub_algebra_map_commutes, mul_assoc, ih, ←mul_assoc] }
end
end comm_semiring
section ring
variables [comm_ring R]
variables (R)
/-- A `semiring` that is an `algebra` over a commutative ring carries a natural `ring` structure.
See note [reducible non-instances]. -/
@[reducible]
def semiring_to_ring [semiring A] [algebra R A] : ring A :=
{ ..module.add_comm_monoid_to_add_comm_group R,
..(infer_instance : semiring A) }
end ring
end algebra
namespace mul_opposite
variables {R A : Type*} [comm_semiring R] [semiring A] [algebra R A]
instance : algebra R Aᵐᵒᵖ :=
{ to_ring_hom := (algebra_map R A).to_opposite $ λ x y, algebra.commutes _ _,
smul_def' := λ c x, unop_injective $
by { dsimp, simp only [op_mul, algebra.smul_def, algebra.commutes, op_unop] },
commutes' := λ r, mul_opposite.rec $ λ x, by dsimp; simp only [← op_mul, algebra.commutes],
.. mul_opposite.has_smul A R }
@[simp] lemma algebra_map_apply (c : R) : algebra_map R Aᵐᵒᵖ c = op (algebra_map R A c) := rfl
end mul_opposite
namespace module
variables (R : Type u) (M : Type v) [comm_semiring R] [add_comm_monoid M] [module R M]
instance : algebra R (module.End R M) :=
algebra.of_module smul_mul_assoc (λ r f g, (smul_comm r f g).symm)
lemma algebra_map_End_eq_smul_id (a : R) :
(algebra_map R (End R M)) a = a • linear_map.id := rfl
@[simp] lemma algebra_map_End_apply (a : R) (m : M) :
(algebra_map R (End R M)) a m = a • m := rfl
@[simp] lemma ker_algebra_map_End (K : Type u) (V : Type v)
[field K] [add_comm_group V] [module K V] (a : K) (ha : a ≠ 0) :
((algebra_map K (End K V)) a).ker = ⊥ :=
linear_map.ker_smul _ _ ha
end module
set_option old_structure_cmd true
/-- Defining the homomorphism in the category R-Alg. -/
@[nolint has_nonempty_instance]
structure alg_hom (R : Type u) (A : Type v) (B : Type w)
[comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] extends ring_hom A B :=
(commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r)
run_cmd tactic.add_doc_string `alg_hom.to_ring_hom "Reinterpret an `alg_hom` as a `ring_hom`"
infixr ` →ₐ `:25 := alg_hom _
notation A ` →ₐ[`:25 R `] ` B := alg_hom R A B
/-- `alg_hom_class F R A B` asserts `F` is a type of bundled algebra homomorphisms
from `A` to `B`. -/
class alg_hom_class (F : Type*) (R : out_param Type*) (A : out_param Type*) (B : out_param Type*)
[comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B]
extends ring_hom_class F A B :=
(commutes : ∀ (f : F) (r : R), f (algebra_map R A r) = algebra_map R B r)
-- `R` becomes a metavariable but that's fine because it's an `out_param`
attribute [nolint dangerous_instance] alg_hom_class.to_ring_hom_class
attribute [simp] alg_hom_class.commutes
namespace alg_hom_class
variables {R : Type*} {A : Type*} {B : Type*} [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] : linear_map_class F R A B :=
{ map_smulₛₗ := λ f r x, by simp only [algebra.smul_def, map_mul, commutes, ring_hom.id_apply],
..‹alg_hom_class F R A B› }
end alg_hom_class
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁}
section semiring
variables [comm_semiring R] [semiring A] [semiring B] [semiring C] [semiring D]
variables [algebra R A] [algebra R B] [algebra R C] [algebra R D]
instance : has_coe_to_fun (A →ₐ[R] B) (λ _, A → B) := ⟨alg_hom.to_fun⟩
initialize_simps_projections alg_hom (to_fun → apply)
@[simp] lemma to_fun_eq_coe (f : A →ₐ[R] B) : f.to_fun = f := rfl
instance : alg_hom_class (A →ₐ[R] B) R A B :=
{ coe := to_fun,
coe_injective' := λ f g h, by { cases f, cases g, congr' },
map_add := map_add',
map_zero := map_zero',
map_mul := map_mul',
map_one := map_one',
commutes := λ f, f.commutes' }
instance coe_ring_hom : has_coe (A →ₐ[R] B) (A →+* B) := ⟨alg_hom.to_ring_hom⟩
instance coe_monoid_hom : has_coe (A →ₐ[R] B) (A →* B) := ⟨λ f, ↑(f : A →+* B)⟩
instance coe_add_monoid_hom : has_coe (A →ₐ[R] B) (A →+ B) := ⟨λ f, ↑(f : A →+* B)⟩
@[simp, norm_cast] lemma coe_mk {f : A → B} (h₁ h₂ h₃ h₄ h₅) :
⇑(⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := rfl
-- make the coercion the simp-normal form
@[simp] lemma to_ring_hom_eq_coe (f : A →ₐ[R] B) : f.to_ring_hom = f := rfl
@[simp, norm_cast] lemma coe_to_ring_hom (f : A →ₐ[R] B) : ⇑(f : A →+* B) = f := rfl
@[simp, norm_cast] lemma coe_to_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →* B) = f := rfl
@[simp, norm_cast] lemma coe_to_add_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →+ B) = f := rfl
variables (φ : A →ₐ[R] B)
theorem coe_fn_injective : @function.injective (A →ₐ[R] B) (A → B) coe_fn := fun_like.coe_injective
theorem coe_fn_inj {φ₁ φ₂ : A →ₐ[R] B} : (φ₁ : A → B) = φ₂ ↔ φ₁ = φ₂ := fun_like.coe_fn_eq
theorem coe_ring_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+* B)) :=
λ φ₁ φ₂ H, coe_fn_injective $ show ((φ₁ : (A →+* B)) : A → B) = ((φ₂ : (A →+* B)) : A → B),
from congr_arg _ H
theorem coe_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →* B)) :=
ring_hom.coe_monoid_hom_injective.comp coe_ring_hom_injective
theorem coe_add_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+ B)) :=
ring_hom.coe_add_monoid_hom_injective.comp coe_ring_hom_injective
protected lemma congr_fun {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁ = φ₂) (x : A) : φ₁ x = φ₂ x :=
fun_like.congr_fun H x
protected lemma congr_arg (φ : A →ₐ[R] B) {x y : A} (h : x = y) : φ x = φ y :=
fun_like.congr_arg φ h
@[ext]
theorem ext {φ₁ φ₂ : A →ₐ[R] B} (H : ∀ x, φ₁ x = φ₂ x) : φ₁ = φ₂ := fun_like.ext _ _ H
theorem ext_iff {φ₁ φ₂ : A →ₐ[R] B} : φ₁ = φ₂ ↔ ∀ x, φ₁ x = φ₂ x := fun_like.ext_iff
@[simp] theorem mk_coe {f : A →ₐ[R] B} (h₁ h₂ h₃ h₄ h₅) :
(⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := ext $ λ _, rfl
@[simp]
theorem commutes (r : R) : φ (algebra_map R A r) = algebra_map R B r := φ.commutes' r
theorem comp_algebra_map : (φ : A →+* B).comp (algebra_map R A) = algebra_map R B :=
ring_hom.ext $ φ.commutes
protected lemma map_add (r s : A) : φ (r + s) = φ r + φ s := map_add _ _ _
protected lemma map_zero : φ 0 = 0 := map_zero _
protected lemma map_mul (x y) : φ (x * y) = φ x * φ y := map_mul _ _ _
protected lemma map_one : φ 1 = 1 := map_one _
protected lemma map_pow (x : A) (n : ℕ) : φ (x ^ n) = (φ x) ^ n := map_pow _ _ _
@[simp] protected lemma map_smul (r : R) (x : A) : φ (r • x) = r • φ x := map_smul _ _ _
protected lemma map_sum {ι : Type*} (f : ι → A) (s : finset ι) :
φ (∑ x in s, f x) = ∑ x in s, φ (f x) := map_sum _ _ _
protected lemma map_finsupp_sum {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) :
φ (f.sum g) = f.sum (λ i a, φ (g i a)) := map_finsupp_sum _ _ _
protected lemma map_bit0 (x) : φ (bit0 x) = bit0 (φ x) := map_bit0 _ _
protected lemma map_bit1 (x) : φ (bit1 x) = bit1 (φ x) := map_bit1 _ _
/-- If a `ring_hom` is `R`-linear, then it is an `alg_hom`. -/
def mk' (f : A →+* B) (h : ∀ (c : R) x, f (c • x) = c • f x) : A →ₐ[R] B :=
{ to_fun := f,
commutes' := λ c, by simp only [algebra.algebra_map_eq_smul_one, h, f.map_one],
.. f }
@[simp] lemma coe_mk' (f : A →+* B) (h : ∀ (c : R) x, f (c • x) = c • f x) : ⇑(mk' f h) = f := rfl
section
variables (R A)
/-- Identity map as an `alg_hom`. -/
protected def id : A →ₐ[R] A :=
{ commutes' := λ _, rfl,
..ring_hom.id A }
@[simp] lemma coe_id : ⇑(alg_hom.id R A) = id := rfl
@[simp] lemma id_to_ring_hom : (alg_hom.id R A : A →+* A) = ring_hom.id _ := rfl
end
lemma id_apply (p : A) : alg_hom.id R A p = p := rfl
/-- Composition of algebra homeomorphisms. -/
def comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : A →ₐ[R] C :=
{ commutes' := λ r : R, by rw [← φ₁.commutes, ← φ₂.commutes]; refl,
.. φ₁.to_ring_hom.comp ↑φ₂ }
@[simp] lemma coe_comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : ⇑(φ₁.comp φ₂) = φ₁ ∘ φ₂ := rfl
lemma comp_apply (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) (p : A) : φ₁.comp φ₂ p = φ₁ (φ₂ p) := rfl
lemma comp_to_ring_hom (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) :
⇑(φ₁.comp φ₂ : A →+* C) = (φ₁ : B →+* C).comp ↑φ₂ := rfl
@[simp] theorem comp_id : φ.comp (alg_hom.id R A) = φ :=
ext $ λ x, rfl
@[simp] theorem id_comp : (alg_hom.id R B).comp φ = φ :=
ext $ λ x, rfl
theorem comp_assoc (φ₁ : C →ₐ[R] D) (φ₂ : B →ₐ[R] C) (φ₃ : A →ₐ[R] B) :
(φ₁.comp φ₂).comp φ₃ = φ₁.comp (φ₂.comp φ₃) :=
ext $ λ x, rfl
/-- R-Alg ⥤ R-Mod -/
def to_linear_map : A →ₗ[R] B :=
{ to_fun := φ,
map_add' := map_add _,
map_smul' := map_smul _ }
@[simp] lemma to_linear_map_apply (p : A) : φ.to_linear_map p = φ p := rfl
theorem to_linear_map_injective : function.injective (to_linear_map : _ → (A →ₗ[R] B)) :=
λ φ₁ φ₂ h, ext $ linear_map.congr_fun h
@[simp] lemma comp_to_linear_map (f : A →ₐ[R] B) (g : B →ₐ[R] C) :
(g.comp f).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl
@[simp] lemma to_linear_map_id : to_linear_map (alg_hom.id R A) = linear_map.id :=
linear_map.ext $ λ _, rfl
/-- Promote a `linear_map` to an `alg_hom` by supplying proofs about the behavior on `1` and `*`. -/
@[simps]
def of_linear_map (f : A →ₗ[R] B) (map_one : f 1 = 1) (map_mul : ∀ x y, f (x * y) = f x * f y) :
A →ₐ[R] B :=
{ to_fun := f,
map_one' := map_one,
map_mul' := map_mul,
commutes' := λ c, by simp only [algebra.algebra_map_eq_smul_one, f.map_smul, map_one],
.. f.to_add_monoid_hom }
@[simp] lemma of_linear_map_to_linear_map (map_one) (map_mul) :
of_linear_map φ.to_linear_map map_one map_mul = φ :=
by { ext, refl }
@[simp] lemma to_linear_map_of_linear_map (f : A →ₗ[R] B) (map_one) (map_mul) :
to_linear_map (of_linear_map f map_one map_mul) = f :=
by { ext, refl }
@[simp] lemma of_linear_map_id (map_one) (map_mul) :
of_linear_map linear_map.id map_one map_mul = alg_hom.id R A :=
ext $ λ _, rfl
lemma map_smul_of_tower {R'} [has_smul R' A] [has_smul R' B]
[linear_map.compatible_smul A B R' R] (r : R') (x : A) : φ (r • x) = r • φ x :=
φ.to_linear_map.map_smul_of_tower r x
lemma map_list_prod (s : list A) :
φ s.prod = (s.map φ).prod :=
φ.to_ring_hom.map_list_prod s
@[simps mul one {attrs := []}] instance End : monoid (A →ₐ[R] A) :=
{ mul := comp,
mul_assoc := λ ϕ ψ χ, rfl,
one := alg_hom.id R A,
one_mul := λ ϕ, ext $ λ x, rfl,
mul_one := λ ϕ, ext $ λ x, rfl }
@[simp] lemma one_apply (x : A) : (1 : A →ₐ[R] A) x = x := rfl
@[simp] lemma mul_apply (φ ψ : A →ₐ[R] A) (x : A) : (φ * ψ) x = φ (ψ x) := rfl
section prod
/-- First projection as `alg_hom`. -/
def fst : A × B →ₐ[R] A :=
{ commutes' := λ r, rfl, .. ring_hom.fst A B}
/-- Second projection as `alg_hom`. -/
def snd : A × B →ₐ[R] B :=
{ commutes' := λ r, rfl, .. ring_hom.snd A B}
end prod
lemma algebra_map_eq_apply (f : A →ₐ[R] B) {y : R} {x : A} (h : algebra_map R A y = x) :
algebra_map R B y = f x :=
h ▸ (f.commutes _).symm
end semiring
section comm_semiring
variables [comm_semiring R] [comm_semiring A] [comm_semiring B]
variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B)
protected lemma map_multiset_prod (s : multiset A) :
φ s.prod = (s.map φ).prod := map_multiset_prod _ _
protected lemma map_prod {ι : Type*} (f : ι → A) (s : finset ι) :
φ (∏ x in s, f x) = ∏ x in s, φ (f x) := map_prod _ _ _
protected lemma map_finsupp_prod {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) :
φ (f.prod g) = f.prod (λ i a, φ (g i a)) := map_finsupp_prod _ _ _
end comm_semiring
section ring
variables [comm_semiring R] [ring A] [ring B]
variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B)
protected lemma map_neg (x) : φ (-x) = -φ x := map_neg _ _
protected lemma map_sub (x y) : φ (x - y) = φ x - φ y := map_sub _ _ _
@[simp] lemma map_int_cast (n : ℤ) : φ n = n :=
φ.to_ring_hom.map_int_cast n
end ring
end alg_hom
@[simp] lemma rat.smul_one_eq_coe {A : Type*} [division_ring A] [algebra ℚ A] (m : ℚ) :
@@has_smul.smul algebra.to_has_smul m (1 : A) = ↑m :=
by rw [algebra.smul_def, mul_one, ring_hom.eq_rat_cast]
set_option old_structure_cmd true
/-- An equivalence of algebras is an equivalence of rings commuting with the actions of scalars. -/
structure alg_equiv (R : Type u) (A : Type v) (B : Type w)
[comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B]
extends A ≃ B, A ≃* B, A ≃+ B, A ≃+* B :=
(commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r)
attribute [nolint doc_blame] alg_equiv.to_ring_equiv
attribute [nolint doc_blame] alg_equiv.to_equiv
attribute [nolint doc_blame] alg_equiv.to_add_equiv
attribute [nolint doc_blame] alg_equiv.to_mul_equiv
notation A ` ≃ₐ[`:50 R `] ` A' := alg_equiv R A A'
/-- `alg_equiv_class F R A B` states that `F` is a type of algebra structure preserving
equivalences. You should extend this class when you extend `alg_equiv`. -/
class alg_equiv_class (F : Type*) (R A B : out_param Type*)
[comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B]
extends ring_equiv_class F A B :=
(commutes : ∀ (f : F) (r : R), f (algebra_map R A r) = algebra_map R B r)
-- `R` becomes a metavariable but that's fine because it's an `out_param`
attribute [nolint dangerous_instance] alg_equiv_class.to_ring_equiv_class
namespace alg_equiv_class
@[priority 100] -- See note [lower instance priority]
instance to_alg_hom_class (F R A B : Type*)
[comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B]
[h : alg_equiv_class F R A B] : alg_hom_class F R A B :=
{ coe := coe_fn,
coe_injective' := fun_like.coe_injective,
map_zero := map_zero,
map_one := map_one,
.. h }
@[priority 100]
instance to_linear_equiv_class (F R A B : Type*)
[comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B]
[h : alg_equiv_class F R A B] : linear_equiv_class F R A B :=
{ map_smulₛₗ := λ f, map_smulₛₗ f,
..h }
end alg_equiv_class
namespace alg_equiv
variables {R : Type u} {A₁ : Type v} {A₂ : Type w} {A₃ : Type u₁}
section semiring
variables [comm_semiring R] [semiring A₁] [semiring A₂] [semiring A₃]
variables [algebra R A₁] [algebra R A₂] [algebra R A₃]
variables (e : A₁ ≃ₐ[R] A₂)
instance : alg_equiv_class (A₁ ≃ₐ[R] A₂) R A₁ A₂ :=
{ coe := to_fun,
inv := inv_fun,
coe_injective' := λ f g h₁ h₂, by { cases f, cases g, congr' },
map_add := map_add',
map_mul := map_mul',
commutes := commutes',
left_inv := left_inv,
right_inv := right_inv }
/-- Helper instance for when there's too many metavariables to apply
`fun_like.has_coe_to_fun` directly. -/
instance : has_coe_to_fun (A₁ ≃ₐ[R] A₂) (λ _, A₁ → A₂) := ⟨alg_equiv.to_fun⟩
@[ext]
lemma ext {f g : A₁ ≃ₐ[R] A₂} (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h
protected lemma congr_arg {f : A₁ ≃ₐ[R] A₂} {x x' : A₁} : x = x' → f x = f x' :=
fun_like.congr_arg f
protected lemma congr_fun {f g : A₁ ≃ₐ[R] A₂} (h : f = g) (x : A₁) : f x = g x :=
fun_like.congr_fun h x
protected lemma ext_iff {f g : A₁ ≃ₐ[R] A₂} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff
lemma coe_fun_injective : @function.injective (A₁ ≃ₐ[R] A₂) (A₁ → A₂) (λ e, (e : A₁ → A₂)) :=
fun_like.coe_injective
instance has_coe_to_ring_equiv : has_coe (A₁ ≃ₐ[R] A₂) (A₁ ≃+* A₂) := ⟨alg_equiv.to_ring_equiv⟩
@[simp] lemma coe_mk {to_fun inv_fun left_inv right_inv map_mul map_add commutes} :
⇑(⟨to_fun, inv_fun, left_inv, right_inv, map_mul, map_add, commutes⟩ : A₁ ≃ₐ[R] A₂) = to_fun :=
rfl
@[simp] theorem mk_coe (e : A₁ ≃ₐ[R] A₂) (e' h₁ h₂ h₃ h₄ h₅) :
(⟨e, e', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂) = e := ext $ λ _, rfl
@[simp] lemma to_fun_eq_coe (e : A₁ ≃ₐ[R] A₂) : e.to_fun = e := rfl
@[simp] lemma to_equiv_eq_coe : e.to_equiv = e := rfl
@[simp] lemma to_ring_equiv_eq_coe : e.to_ring_equiv = e := rfl
@[simp, norm_cast] lemma coe_ring_equiv : ((e : A₁ ≃+* A₂) : A₁ → A₂) = e := rfl
lemma coe_ring_equiv' : (e.to_ring_equiv : A₁ → A₂) = e := rfl
lemma coe_ring_equiv_injective : function.injective (coe : (A₁ ≃ₐ[R] A₂) → (A₁ ≃+* A₂)) :=
λ e₁ e₂ h, ext $ ring_equiv.congr_fun h
protected lemma map_add : ∀ x y, e (x + y) = e x + e y := map_add e
protected lemma map_zero : e 0 = 0 := map_zero e
protected lemma map_mul : ∀ x y, e (x * y) = (e x) * (e y) := map_mul e
protected lemma map_one : e 1 = 1 := map_one e
@[simp] lemma commutes : ∀ (r : R), e (algebra_map R A₁ r) = algebra_map R A₂ r :=
e.commutes'
@[simp] lemma map_smul (r : R) (x : A₁) : e (r • x) = r • e x :=
by simp only [algebra.smul_def, map_mul, commutes]
lemma map_sum {ι : Type*} (f : ι → A₁) (s : finset ι) :
e (∑ x in s, f x) = ∑ x in s, e (f x) :=
e.to_add_equiv.map_sum f s
lemma map_finsupp_sum {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) :
e (f.sum g) = f.sum (λ i b, e (g i b)) :=
e.map_sum _ _
/-- Interpret an algebra equivalence as an algebra homomorphism.
This definition is included for symmetry with the other `to_*_hom` projections.
The `simp` normal form is to use the coercion of the `has_coe_to_alg_hom` instance. -/
def to_alg_hom : A₁ →ₐ[R] A₂ :=
{ map_one' := e.map_one, map_zero' := e.map_zero, ..e }
instance has_coe_to_alg_hom : has_coe (A₁ ≃ₐ[R] A₂) (A₁ →ₐ[R] A₂) :=
⟨to_alg_hom⟩
@[simp] lemma to_alg_hom_eq_coe : e.to_alg_hom = e := rfl
@[simp, norm_cast] lemma coe_alg_hom : ((e : A₁ →ₐ[R] A₂) : A₁ → A₂) = e :=
rfl
lemma coe_alg_hom_injective : function.injective (coe : (A₁ ≃ₐ[R] A₂) → (A₁ →ₐ[R] A₂)) :=
λ e₁ e₂ h, ext $ alg_hom.congr_fun h
/-- The two paths coercion can take to a `ring_hom` are equivalent -/
lemma coe_ring_hom_commutes : ((e : A₁ →ₐ[R] A₂) : A₁ →+* A₂) = ((e : A₁ ≃+* A₂) : A₁ →+* A₂) :=
rfl
protected lemma map_pow : ∀ (x : A₁) (n : ℕ), e (x ^ n) = (e x) ^ n := map_pow _
protected lemma injective : function.injective e := equiv_like.injective e
protected lemma surjective : function.surjective e := equiv_like.surjective e
protected lemma bijective : function.bijective e := equiv_like.bijective e
/-- Algebra equivalences are reflexive. -/
@[refl] def refl : A₁ ≃ₐ[R] A₁ := {commutes' := λ r, rfl, ..(1 : A₁ ≃+* A₁)}
instance : inhabited (A₁ ≃ₐ[R] A₁) := ⟨refl⟩
@[simp] lemma refl_to_alg_hom : ↑(refl : A₁ ≃ₐ[R] A₁) = alg_hom.id R A₁ := rfl
@[simp] lemma coe_refl : ⇑(refl : A₁ ≃ₐ[R] A₁) = id := rfl
/-- Algebra equivalences are symmetric. -/
@[symm]
def symm (e : A₁ ≃ₐ[R] A₂) : A₂ ≃ₐ[R] A₁ :=
{ commutes' := λ r, by { rw ←e.to_ring_equiv.symm_apply_apply (algebra_map R A₁ r), congr,
change _ = e _, rw e.commutes, },
..e.to_ring_equiv.symm, }
/-- See Note [custom simps projection] -/
def simps.symm_apply (e : A₁ ≃ₐ[R] A₂) : A₂ → A₁ := e.symm
initialize_simps_projections alg_equiv (to_fun → apply, inv_fun → symm_apply)
@[simp] lemma inv_fun_eq_symm {e : A₁ ≃ₐ[R] A₂} : e.inv_fun = e.symm := rfl
@[simp] lemma symm_symm (e : A₁ ≃ₐ[R] A₂) : e.symm.symm = e :=
by { ext, refl, }
lemma symm_bijective : function.bijective (symm : (A₁ ≃ₐ[R] A₂) → (A₂ ≃ₐ[R] A₁)) :=
equiv.bijective ⟨symm, symm, symm_symm, symm_symm⟩
@[simp] lemma mk_coe' (e : A₁ ≃ₐ[R] A₂) (f h₁ h₂ h₃ h₄ h₅) :
(⟨f, e, h₁, h₂, h₃, h₄, h₅⟩ : A₂ ≃ₐ[R] A₁) = e.symm :=
symm_bijective.injective $ ext $ λ x, rfl
@[simp] theorem symm_mk (f f') (h₁ h₂ h₃ h₄ h₅) :
(⟨f, f', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂).symm =
{ to_fun := f', inv_fun := f,
..(⟨f, f', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂).symm } := rfl
@[simp]
theorem refl_symm : (alg_equiv.refl : A₁ ≃ₐ[R] A₁).symm = alg_equiv.refl := rfl
/-- Algebra equivalences are transitive. -/
@[trans]
def trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : A₁ ≃ₐ[R] A₃ :=
{ commutes' := λ r, show e₂.to_fun (e₁.to_fun _) = _, by rw [e₁.commutes', e₂.commutes'],
..(e₁.to_ring_equiv.trans e₂.to_ring_equiv), }
@[simp] lemma apply_symm_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e (e.symm x) = x :=
e.to_equiv.apply_symm_apply
@[simp] lemma symm_apply_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e.symm (e x) = x :=
e.to_equiv.symm_apply_apply
@[simp] lemma symm_trans_apply (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) (x : A₃) :
(e₁.trans e₂).symm x = e₁.symm (e₂.symm x) := rfl
@[simp] lemma coe_trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) :
⇑(e₁.trans e₂) = e₂ ∘ e₁ := rfl
@[simp] lemma trans_apply (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) (x : A₁) :
(e₁.trans e₂) x = e₂ (e₁ x) := rfl
@[simp] lemma comp_symm (e : A₁ ≃ₐ[R] A₂) :
alg_hom.comp (e : A₁ →ₐ[R] A₂) ↑e.symm = alg_hom.id R A₂ :=
by { ext, simp }
@[simp] lemma symm_comp (e : A₁ ≃ₐ[R] A₂) :
alg_hom.comp ↑e.symm (e : A₁ →ₐ[R] A₂) = alg_hom.id R A₁ :=
by { ext, simp }
theorem left_inverse_symm (e : A₁ ≃ₐ[R] A₂) : function.left_inverse e.symm e := e.left_inv
theorem right_inverse_symm (e : A₁ ≃ₐ[R] A₂) : function.right_inverse e.symm e := e.right_inv
/-- If `A₁` is equivalent to `A₁'` and `A₂` is equivalent to `A₂'`, then the type of maps
`A₁ →ₐ[R] A₂` is equivalent to the type of maps `A₁' →ₐ[R] A₂'`. -/
def arrow_congr {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂'] [algebra R A₁'] [algebra R A₂']
(e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') : (A₁ →ₐ[R] A₂) ≃ (A₁' →ₐ[R] A₂') :=
{ to_fun := λ f, (e₂.to_alg_hom.comp f).comp e₁.symm.to_alg_hom,
inv_fun := λ f, (e₂.symm.to_alg_hom.comp f).comp e₁.to_alg_hom,
left_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, symm_comp],
simp only [←alg_hom.comp_assoc, symm_comp, alg_hom.id_comp, alg_hom.comp_id] },
right_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, comp_symm],
simp only [←alg_hom.comp_assoc, comp_symm, alg_hom.id_comp, alg_hom.comp_id] } }
lemma arrow_congr_comp {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃']
[algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂')
(e₃ : A₃ ≃ₐ[R] A₃') (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₃) :
arrow_congr e₁ e₃ (g.comp f) = (arrow_congr e₂ e₃ g).comp (arrow_congr e₁ e₂ f) :=
by { ext, simp only [arrow_congr, equiv.coe_fn_mk, alg_hom.comp_apply],
congr, exact (e₂.symm_apply_apply _).symm }
@[simp] lemma arrow_congr_refl :
arrow_congr alg_equiv.refl alg_equiv.refl = equiv.refl (A₁ →ₐ[R] A₂) :=
by { ext, refl }
@[simp] lemma arrow_congr_trans {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃']
[algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₂) (e₁' : A₁' ≃ₐ[R] A₂')
(e₂ : A₂ ≃ₐ[R] A₃) (e₂' : A₂' ≃ₐ[R] A₃') :
arrow_congr (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr e₁ e₁').trans (arrow_congr e₂ e₂') :=
by { ext, refl }
@[simp] lemma arrow_congr_symm {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂']
[algebra R A₁'] [algebra R A₂'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') :
(arrow_congr e₁ e₂).symm = arrow_congr e₁.symm e₂.symm :=
by { ext, refl }
/-- If an algebra morphism has an inverse, it is a algebra isomorphism. -/
def of_alg_hom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ : f.comp g = alg_hom.id R A₂)
(h₂ : g.comp f = alg_hom.id R A₁) : A₁ ≃ₐ[R] A₂ :=
{ to_fun := f,
inv_fun := g,
left_inv := alg_hom.ext_iff.1 h₂,
right_inv := alg_hom.ext_iff.1 h₁,
..f }
lemma coe_alg_hom_of_alg_hom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) :
↑(of_alg_hom f g h₁ h₂) = f := alg_hom.ext $ λ _, rfl
@[simp]
lemma of_alg_hom_coe_alg_hom (f : A₁ ≃ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) :
of_alg_hom ↑f g h₁ h₂ = f := ext $ λ _, rfl
lemma of_alg_hom_symm (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) :
(of_alg_hom f g h₁ h₂).symm = of_alg_hom g f h₂ h₁ := rfl
/-- Promotes a bijective algebra homomorphism to an algebra equivalence. -/
noncomputable def of_bijective (f : A₁ →ₐ[R] A₂) (hf : function.bijective f) : A₁ ≃ₐ[R] A₂ :=
{ .. ring_equiv.of_bijective (f : A₁ →+* A₂) hf, .. f }
@[simp] lemma coe_of_bijective {f : A₁ →ₐ[R] A₂} {hf : function.bijective f} :
(alg_equiv.of_bijective f hf : A₁ → A₂) = f := rfl
lemma of_bijective_apply {f : A₁ →ₐ[R] A₂} {hf : function.bijective f} (a : A₁) :
(alg_equiv.of_bijective f hf) a = f a := rfl
/-- Forgetting the multiplicative structures, an equivalence of algebras is a linear equivalence. -/
@[simps apply] def to_linear_equiv (e : A₁ ≃ₐ[R] A₂) : A₁ ≃ₗ[R] A₂ :=
{ to_fun := e,
map_smul' := e.map_smul,
inv_fun := e.symm,
.. e }
@[simp] lemma to_linear_equiv_refl :
(alg_equiv.refl : A₁ ≃ₐ[R] A₁).to_linear_equiv = linear_equiv.refl R A₁ := rfl
@[simp] lemma to_linear_equiv_symm (e : A₁ ≃ₐ[R] A₂) :
e.to_linear_equiv.symm = e.symm.to_linear_equiv := rfl
@[simp] lemma to_linear_equiv_trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) :
(e₁.trans e₂).to_linear_equiv = e₁.to_linear_equiv.trans e₂.to_linear_equiv := rfl
theorem to_linear_equiv_injective : function.injective (to_linear_equiv : _ → (A₁ ≃ₗ[R] A₂)) :=
λ e₁ e₂ h, ext $ linear_equiv.congr_fun h
/-- Interpret an algebra equivalence as a linear map. -/
def to_linear_map : A₁ →ₗ[R] A₂ :=
e.to_alg_hom.to_linear_map
@[simp] lemma to_alg_hom_to_linear_map :
(e : A₁ →ₐ[R] A₂).to_linear_map = e.to_linear_map := rfl
@[simp] lemma to_linear_equiv_to_linear_map :
e.to_linear_equiv.to_linear_map = e.to_linear_map := rfl
@[simp] lemma to_linear_map_apply (x : A₁) : e.to_linear_map x = e x := rfl
theorem to_linear_map_injective : function.injective (to_linear_map : _ → (A₁ →ₗ[R] A₂)) :=
λ e₁ e₂ h, ext $ linear_map.congr_fun h
@[simp] lemma trans_to_linear_map (f : A₁ ≃ₐ[R] A₂) (g : A₂ ≃ₐ[R] A₃) :
(f.trans g).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl
section of_linear_equiv
variables (l : A₁ ≃ₗ[R] A₂)
(map_mul : ∀ x y : A₁, l (x * y) = l x * l y)
(commutes : ∀ r : R, l (algebra_map R A₁ r) = algebra_map R A₂ r)
/--
Upgrade a linear equivalence to an algebra equivalence,
given that it distributes over multiplication and action of scalars.
-/
@[simps apply]
def of_linear_equiv : A₁ ≃ₐ[R] A₂ :=
{ to_fun := l,
inv_fun := l.symm,
map_mul' := map_mul,
commutes' := commutes,
..l }
@[simp]
lemma of_linear_equiv_symm :
(of_linear_equiv l map_mul commutes).symm = of_linear_equiv l.symm
((of_linear_equiv l map_mul commutes).symm.map_mul)
((of_linear_equiv l map_mul commutes).symm.commutes) :=
rfl
@[simp] lemma of_linear_equiv_to_linear_equiv (map_mul) (commutes) :
of_linear_equiv e.to_linear_equiv map_mul commutes = e :=
by { ext, refl }
@[simp] lemma to_linear_equiv_of_linear_equiv :
to_linear_equiv (of_linear_equiv l map_mul commutes) = l :=
by { ext, refl }
end of_linear_equiv
@[simps mul one {attrs := []}] instance aut : group (A₁ ≃ₐ[R] A₁) :=
{ mul := λ ϕ ψ, ψ.trans ϕ,
mul_assoc := λ ϕ ψ χ, rfl,
one := refl,
one_mul := λ ϕ, ext $ λ x, rfl,
mul_one := λ ϕ, ext $ λ x, rfl,
inv := symm,
mul_left_inv := λ ϕ, ext $ symm_apply_apply ϕ }
@[simp] lemma one_apply (x : A₁) : (1 : A₁ ≃ₐ[R] A₁) x = x := rfl
@[simp] lemma mul_apply (e₁ e₂ : A₁ ≃ₐ[R] A₁) (x : A₁) : (e₁ * e₂) x = e₁ (e₂ x) := rfl
/-- An algebra isomorphism induces a group isomorphism between automorphism groups -/
@[simps apply]
def aut_congr (ϕ : A₁ ≃ₐ[R] A₂) : (A₁ ≃ₐ[R] A₁) ≃* (A₂ ≃ₐ[R] A₂) :=
{ to_fun := λ ψ, ϕ.symm.trans (ψ.trans ϕ),
inv_fun := λ ψ, ϕ.trans (ψ.trans ϕ.symm),
left_inv := λ ψ, by { ext, simp_rw [trans_apply, symm_apply_apply] },
right_inv := λ ψ, by { ext, simp_rw [trans_apply, apply_symm_apply] },
map_mul' := λ ψ χ, by { ext, simp only [mul_apply, trans_apply, symm_apply_apply] } }
@[simp] lemma aut_congr_refl : aut_congr (alg_equiv.refl) = mul_equiv.refl (A₁ ≃ₐ[R] A₁) :=
by { ext, refl }
@[simp] lemma aut_congr_symm (ϕ : A₁ ≃ₐ[R] A₂) : (aut_congr ϕ).symm = aut_congr ϕ.symm := rfl
@[simp] lemma aut_congr_trans (ϕ : A₁ ≃ₐ[R] A₂) (ψ : A₂ ≃ₐ[R] A₃) :
(aut_congr ϕ).trans (aut_congr ψ) = aut_congr (ϕ.trans ψ) := rfl
/-- The tautological action by `A₁ ≃ₐ[R] A₁` on `A₁`.
This generalizes `function.End.apply_mul_action`. -/
instance apply_mul_semiring_action : mul_semiring_action (A₁ ≃ₐ[R] A₁) A₁ :=
{ smul := ($),
smul_zero := alg_equiv.map_zero,
smul_add := alg_equiv.map_add,
smul_one := alg_equiv.map_one,
smul_mul := alg_equiv.map_mul,
one_smul := λ _, rfl,
mul_smul := λ _ _ _, rfl }
@[simp] protected lemma smul_def (f : A₁ ≃ₐ[R] A₁) (a : A₁) : f • a = f a := rfl
instance apply_has_faithful_smul : has_faithful_smul (A₁ ≃ₐ[R] A₁) A₁ :=
⟨λ _ _, alg_equiv.ext⟩
instance apply_smul_comm_class : smul_comm_class R (A₁ ≃ₐ[R] A₁) A₁ :=
{ smul_comm := λ r e a, (e.map_smul r a).symm }
instance apply_smul_comm_class' : smul_comm_class (A₁ ≃ₐ[R] A₁) R A₁ :=
{ smul_comm := λ e r a, (e.map_smul r a) }
@[simp] lemma algebra_map_eq_apply (e : A₁ ≃ₐ[R] A₂) {y : R} {x : A₁} :
(algebra_map R A₂ y = e x) ↔ (algebra_map R A₁ y = x) :=
⟨λ h, by simpa using e.symm.to_alg_hom.algebra_map_eq_apply h,
λ h, e.to_alg_hom.algebra_map_eq_apply h⟩
end semiring
section comm_semiring
variables [comm_semiring R] [comm_semiring A₁] [comm_semiring A₂]
variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂)
lemma map_prod {ι : Type*} (f : ι → A₁) (s : finset ι) :
e (∏ x in s, f x) = ∏ x in s, e (f x) :=
map_prod _ f s
lemma map_finsupp_prod {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) :
e (f.prod g) = f.prod (λ i a, e (g i a)) :=
map_finsupp_prod _ f g
end comm_semiring
section ring
variables [comm_semiring R] [ring A₁] [ring A₂]
variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂)
protected lemma map_neg (x) : e (-x) = -e x := map_neg e x
protected lemma map_sub (x y) : e (x - y) = e x - e y := map_sub e x y
end ring
end alg_equiv
namespace mul_semiring_action
variables {M G : Type*} (R A : Type*) [comm_semiring R] [semiring A] [algebra R A]
section
variables [monoid M] [mul_semiring_action M A] [smul_comm_class M R A]
/-- Each element of the monoid defines a algebra homomorphism.
This is a stronger version of `mul_semiring_action.to_ring_hom` and
`distrib_mul_action.to_linear_map`. -/
@[simps]
def to_alg_hom (m : M) : A →ₐ[R] A :=
{ to_fun := λ a, m • a,
commutes' := smul_algebra_map _,
..mul_semiring_action.to_ring_hom _ _ m }
theorem to_alg_hom_injective [has_faithful_smul M A] :
function.injective (mul_semiring_action.to_alg_hom R A : M → A →ₐ[R] A) :=
λ m₁ m₂ h, eq_of_smul_eq_smul $ λ r, alg_hom.ext_iff.1 h r
end
section
variables [group G] [mul_semiring_action G A] [smul_comm_class G R A]
/-- Each element of the group defines a algebra equivalence.
This is a stronger version of `mul_semiring_action.to_ring_equiv` and
`distrib_mul_action.to_linear_equiv`. -/
@[simps]
def to_alg_equiv (g : G) : A ≃ₐ[R] A :=
{ .. mul_semiring_action.to_ring_equiv _ _ g,
.. mul_semiring_action.to_alg_hom R A g }
theorem to_alg_equiv_injective [has_faithful_smul G A] :
function.injective (mul_semiring_action.to_alg_equiv R A : G → A ≃ₐ[R] A) :=
λ m₁ m₂ h, eq_of_smul_eq_smul $ λ r, alg_equiv.ext_iff.1 h r
end
end mul_semiring_action
section nat
variables {R : Type*} [semiring R]
-- Lower the priority so that `algebra.id` is picked most of the time when working with
-- `ℕ`-algebras. This is only an issue since `algebra.id` and `algebra_nat` are not yet defeq.
-- TODO: fix this by adding an `of_nat` field to semirings.
/-- Semiring ⥤ ℕ-Alg -/
@[priority 99] instance algebra_nat : algebra ℕ R :=
{ commutes' := nat.cast_commute,
smul_def' := λ _ _, nsmul_eq_mul _ _,
to_ring_hom := nat.cast_ring_hom R }
instance nat_algebra_subsingleton : subsingleton (algebra ℕ R) :=
⟨λ P Q, by { ext, simp, }⟩
end nat
namespace ring_hom
variables {R S : Type*}
/-- Reinterpret a `ring_hom` as an `ℕ`-algebra homomorphism. -/
def to_nat_alg_hom [semiring R] [semiring S] (f : R →+* S) :
R →ₐ[ℕ] S :=
{ to_fun := f, commutes' := λ n, by simp, .. f }
/-- Reinterpret a `ring_hom` as a `ℤ`-algebra homomorphism. -/
def to_int_alg_hom [ring R] [ring S] [algebra ℤ R] [algebra ℤ S] (f : R →+* S) :
R →ₐ[ℤ] S :=
{ commutes' := λ n, by simp, .. f }
-- note that `R`, `S` could be `semiring`s but this is useless mathematically speaking -
-- a ℚ-algebra is a ring. furthermore, this change probably slows down elaboration.
@[simp] lemma map_rat_algebra_map [ring R] [ring S] [algebra ℚ R] [algebra ℚ S]
(f : R →+* S) (r : ℚ) : f (algebra_map ℚ R r) = algebra_map ℚ S r :=
ring_hom.ext_iff.1 (subsingleton.elim (f.comp (algebra_map ℚ R)) (algebra_map ℚ S)) r
/-- Reinterpret a `ring_hom` as a `ℚ`-algebra homomorphism. This actually yields an equivalence,
see `ring_hom.equiv_rat_alg_hom`. -/
def to_rat_alg_hom [ring R] [ring S] [algebra ℚ R] [algebra ℚ S] (f : R →+* S) :
R →ₐ[ℚ] S :=
{ commutes' := f.map_rat_algebra_map, .. f }
@[simp]
lemma to_rat_alg_hom_to_ring_hom [ring R] [ring S] [algebra ℚ R] [algebra ℚ S]
(f : R →+* S) : ↑f.to_rat_alg_hom = f :=
ring_hom.ext $ λ x, rfl
end ring_hom
section
variables {R S : Type*}
@[simp]
lemma alg_hom.to_ring_hom_to_rat_alg_hom [ring R] [ring S] [algebra ℚ R] [algebra ℚ S]
(f : R →ₐ[ℚ] S) : (f : R →+* S).to_rat_alg_hom = f :=
alg_hom.ext $ λ x, rfl
/-- The equivalence between `ring_hom` and `ℚ`-algebra homomorphisms. -/
@[simps]
def ring_hom.equiv_rat_alg_hom [ring R] [ring S] [algebra ℚ R] [algebra ℚ S] :
(R →+* S) ≃ (R →ₐ[ℚ] S) :=
{ to_fun := ring_hom.to_rat_alg_hom,
inv_fun := alg_hom.to_ring_hom,
left_inv := ring_hom.to_rat_alg_hom_to_ring_hom,
right_inv := alg_hom.to_ring_hom_to_rat_alg_hom, }
end
section rat
instance algebra_rat {α} [division_ring α] [char_zero α] : algebra ℚ α :=
{ smul := (•),
smul_def' := division_ring.qsmul_eq_mul',
to_ring_hom := rat.cast_hom α,
commutes' := rat.cast_commute }
/-- The two `algebra ℚ ℚ` instances should coincide. -/
example : algebra_rat = algebra.id ℚ := rfl
@[simp] theorem algebra_map_rat_rat : algebra_map ℚ ℚ = ring_hom.id ℚ :=
subsingleton.elim _ _
instance algebra_rat_subsingleton {α} [semiring α] :
subsingleton (algebra ℚ α) :=
⟨λ x y, algebra.algebra_ext x y $ ring_hom.congr_fun $ subsingleton.elim _ _⟩
end rat
namespace algebra
open module
variables (R : Type u) (A : Type v)
variables [comm_semiring R] [semiring A] [algebra R A]
/-- `algebra_map` as an `alg_hom`. -/
def of_id : R →ₐ[R] A :=
{ commutes' := λ _, rfl, .. algebra_map R A }
variables {R}
theorem of_id_apply (r) : of_id R A r = algebra_map R A r := rfl
end algebra
section int
variables (R : Type*) [ring R]
-- Lower the priority so that `algebra.id` is picked most of the time when working with
-- `ℤ`-algebras. This is only an issue since `algebra.id ℤ` and `algebra_int ℤ` are not yet defeq.
-- TODO: fix this by adding an `of_int` field to rings.
/-- Ring ⥤ ℤ-Alg -/
@[priority 99] instance algebra_int : algebra ℤ R :=
{ commutes' := int.cast_commute,
smul_def' := λ _ _, zsmul_eq_mul _ _,
to_ring_hom := int.cast_ring_hom R }
/-- A special case of `ring_hom.eq_int_cast'` that happens to be true definitionally -/
@[simp] lemma algebra_map_int_eq : algebra_map ℤ R = int.cast_ring_hom R := rfl
variables {R}
instance int_algebra_subsingleton : subsingleton (algebra ℤ R) :=
⟨λ P Q, by { ext, simp, }⟩
end int
namespace no_zero_smul_divisors
variables {R A : Type*}
open algebra
/-- If `algebra_map R A` is injective and `A` has no zero divisors,
`R`-multiples in `A` are zero only if one of the factors is zero.
Cannot be an instance because there is no `injective (algebra_map R A)` typeclass.
-/
lemma of_algebra_map_injective
[comm_semiring R] [semiring A] [algebra R A] [no_zero_divisors A]
(h : function.injective (algebra_map R A)) : no_zero_smul_divisors R A :=
⟨λ c x hcx, (mul_eq_zero.mp ((smul_def c x).symm.trans hcx)).imp_left
(map_eq_zero_iff (algebra_map R A) h).mp⟩
variables (R A)
lemma algebra_map_injective [comm_ring R] [ring A] [nontrivial A]
[algebra R A] [no_zero_smul_divisors R A] :
function.injective (algebra_map R A) :=
suffices function.injective (λ (c : R), c • (1 : A)),
by { convert this, ext, rw [algebra.smul_def, mul_one] },
smul_left_injective R one_ne_zero
variables {R A}
lemma iff_algebra_map_injective [comm_ring R] [ring A] [is_domain A] [algebra R A] :
no_zero_smul_divisors R A ↔ function.injective (algebra_map R A) :=
⟨@@no_zero_smul_divisors.algebra_map_injective R A _ _ _ _,
no_zero_smul_divisors.of_algebra_map_injective⟩
@[priority 100] -- see note [lower instance priority]
instance char_zero.no_zero_smul_divisors_nat [semiring R] [no_zero_divisors R] [char_zero R] :
no_zero_smul_divisors ℕ R :=
no_zero_smul_divisors.of_algebra_map_injective $ (algebra_map ℕ R).injective_nat
@[priority 100] -- see note [lower instance priority]
instance char_zero.no_zero_smul_divisors_int [ring R] [no_zero_divisors R] [char_zero R] :
no_zero_smul_divisors ℤ R :=
no_zero_smul_divisors.of_algebra_map_injective $ (algebra_map ℤ R).injective_int
section field
variables [field R] [semiring A] [algebra R A]
@[priority 100] -- see note [lower instance priority]
instance algebra.no_zero_smul_divisors [nontrivial A] [no_zero_divisors A] :
no_zero_smul_divisors R A :=
no_zero_smul_divisors.of_algebra_map_injective (algebra_map R A).injective
end field
end no_zero_smul_divisors
/-!
The R-algebra structure on `Π i : I, A i` when each `A i` is an R-algebra.
We couldn't set this up back in `algebra.pi_instances` because this file imports it.
-/
namespace pi
variable {I : Type u} -- The indexing type
variable {R : Type*} -- The scalar type
variable {f : I → Type v} -- The family of types already equipped with instances
variables (x y : Π i, f i) (i : I)
variables (I f)
instance algebra {r : comm_semiring R}
[s : ∀ i, semiring (f i)] [∀ i, algebra R (f i)] :
algebra R (Π i : I, f i) :=
{ commutes' := λ a f, begin ext, simp [algebra.commutes], end,
smul_def' := λ a f, begin ext, simp [algebra.smul_def], end,
..(pi.ring_hom (λ i, algebra_map R (f i)) : R →+* Π i : I, f i) }
@[simp] lemma algebra_map_apply {r : comm_semiring R}
[s : ∀ i, semiring (f i)] [∀ i, algebra R (f i)] (a : R) (i : I) :
algebra_map R (Π i, f i) a i = algebra_map R (f i) a := rfl
-- One could also build a `Π i, R i`-algebra structure on `Π i, A i`,
-- when each `A i` is an `R i`-algebra, although I'm not sure that it's useful.
variables {I} (R) (f)
/-- `function.eval` as an `alg_hom`. The name matches `pi.eval_ring_hom`, `pi.eval_monoid_hom`,
etc. -/
@[simps]
def eval_alg_hom {r : comm_semiring R} [Π i, semiring (f i)] [Π i, algebra R (f i)] (i : I) :
(Π i, f i) →ₐ[R] f i :=
{ to_fun := λ f, f i, commutes' := λ r, rfl, .. pi.eval_ring_hom f i}
variables (A B : Type*) [comm_semiring R] [semiring B] [algebra R B]
/-- `function.const` as an `alg_hom`. The name matches `pi.const_ring_hom`, `pi.const_monoid_hom`,
etc. -/
@[simps]
def const_alg_hom : B →ₐ[R] (A → B) :=
{ to_fun := function.const _,
commutes' := λ r, rfl,
.. pi.const_ring_hom A B}
/-- When `R` is commutative and permits an `algebra_map`, `pi.const_ring_hom` is equal to that
map. -/
@[simp] lemma const_ring_hom_eq_algebra_map : const_ring_hom A R = algebra_map R (A → R) :=
rfl
@[simp] lemma const_alg_hom_eq_algebra_of_id : const_alg_hom R A R = algebra.of_id R (A → R) :=
rfl
end pi
/-- A special case of `pi.algebra` for non-dependent types. Lean struggles to elaborate
definitions elsewhere in the library without this, -/
instance function.algebra {R : Type*} (I : Type*) (A : Type*) [comm_semiring R]
[semiring A] [algebra R A] : algebra R (I → A) :=
pi.algebra _ _
namespace alg_equiv
/-- A family of algebra equivalences `Π j, (A₁ j ≃ₐ A₂ j)` generates a
multiplicative equivalence between `Π j, A₁ j` and `Π j, A₂ j`.
This is the `alg_equiv` version of `equiv.Pi_congr_right`, and the dependent version of
`alg_equiv.arrow_congr`.
-/
@[simps apply]
def Pi_congr_right {R ι : Type*} {A₁ A₂ : ι → Type*} [comm_semiring R]
[Π i, semiring (A₁ i)] [Π i, semiring (A₂ i)] [Π i, algebra R (A₁ i)] [Π i, algebra R (A₂ i)]
(e : Π i, A₁ i ≃ₐ[R] A₂ i) : (Π i, A₁ i) ≃ₐ[R] Π i, A₂ i :=
{ to_fun := λ x j, e j (x j),
inv_fun := λ x j, (e j).symm (x j),
commutes' := λ r, by { ext i, simp },
.. @ring_equiv.Pi_congr_right ι A₁ A₂ _ _ (λ i, (e i).to_ring_equiv) }
@[simp]
lemma Pi_congr_right_refl {R ι : Type*} {A : ι → Type*} [comm_semiring R]
[Π i, semiring (A i)] [Π i, algebra R (A i)] :
Pi_congr_right (λ i, (alg_equiv.refl : A i ≃ₐ[R] A i)) = alg_equiv.refl := rfl
@[simp]
lemma Pi_congr_right_symm {R ι : Type*} {A₁ A₂ : ι → Type*} [comm_semiring R]
[Π i, semiring (A₁ i)] [Π i, semiring (A₂ i)] [Π i, algebra R (A₁ i)] [Π i, algebra R (A₂ i)]
(e : Π i, A₁ i ≃ₐ[R] A₂ i) : (Pi_congr_right e).symm = (Pi_congr_right $ λ i, (e i).symm) := rfl
@[simp]
lemma Pi_congr_right_trans {R ι : Type*} {A₁ A₂ A₃ : ι → Type*} [comm_semiring R]
[Π i, semiring (A₁ i)] [Π i, semiring (A₂ i)] [Π i, semiring (A₃ i)]
[Π i, algebra R (A₁ i)] [Π i, algebra R (A₂ i)] [Π i, algebra R (A₃ i)]
(e₁ : Π i, A₁ i ≃ₐ[R] A₂ i) (e₂ : Π i, A₂ i ≃ₐ[R] A₃ i) :
(Pi_congr_right e₁).trans (Pi_congr_right e₂) = (Pi_congr_right $ λ i, (e₁ i).trans (e₂ i)) :=
rfl
end alg_equiv
section is_scalar_tower
variables {R : Type*} [comm_semiring R]
variables (A : Type*) [semiring A] [algebra R A]
variables {M : Type*} [add_comm_monoid M] [module A M] [module R M] [is_scalar_tower R A M]
variables {N : Type*} [add_comm_monoid N] [module A N] [module R N] [is_scalar_tower R A N]
lemma algebra_compatible_smul (r : R) (m : M) : r • m = ((algebra_map R A) r) • m :=
by rw [←(one_smul A m), ←smul_assoc, algebra.smul_def, mul_one, one_smul]
@[simp] lemma algebra_map_smul (r : R) (m : M) : ((algebra_map R A) r) • m = r • m :=
(algebra_compatible_smul A r m).symm
lemma no_zero_smul_divisors.trans (R A M : Type*) [comm_ring R] [ring A] [is_domain A] [algebra R A]
[add_comm_group M] [module R M] [module A M] [is_scalar_tower R A M] [no_zero_smul_divisors R A]
[no_zero_smul_divisors A M] : no_zero_smul_divisors R M :=
begin
refine ⟨λ r m h, _⟩,
rw [algebra_compatible_smul A r m] at h,
cases smul_eq_zero.1 h with H H,
{ have : function.injective (algebra_map R A) :=
no_zero_smul_divisors.iff_algebra_map_injective.1 infer_instance,
left,
exact (injective_iff_map_eq_zero _).1 this _ H },
{ right,
exact H }
end
variable {A}
@[priority 100] -- see Note [lower instance priority]
instance is_scalar_tower.to_smul_comm_class : smul_comm_class R A M :=
⟨λ r a m, by rw [algebra_compatible_smul A r (a • m), smul_smul, algebra.commutes, mul_smul,
←algebra_compatible_smul]⟩
@[priority 100] -- see Note [lower instance priority]
instance is_scalar_tower.to_smul_comm_class' : smul_comm_class A R M :=
smul_comm_class.symm _ _ _
lemma smul_algebra_smul_comm (r : R) (a : A) (m : M) : a • r • m = r • a • m :=
smul_comm _ _ _
namespace linear_map
instance coe_is_scalar_tower : has_coe (M →ₗ[A] N) (M →ₗ[R] N) :=
⟨restrict_scalars R⟩
variables (R) {A M N}
@[simp, norm_cast squash] lemma coe_restrict_scalars_eq_coe (f : M →ₗ[A] N) :
(f.restrict_scalars R : M → N) = f := rfl
@[simp, norm_cast squash] lemma coe_coe_is_scalar_tower (f : M →ₗ[A] N) :
((f : M →ₗ[R] N) : M → N) = f := rfl
/-- `A`-linearly coerce a `R`-linear map from `M` to `A` to a function, given an algebra `A` over
a commutative semiring `R` and `M` a module over `R`. -/
def lto_fun (R : Type u) (M : Type v) (A : Type w)
[comm_semiring R] [add_comm_monoid M] [module R M] [comm_ring A] [algebra R A] :
(M →ₗ[R] A) →ₗ[A] (M → A) :=
{ to_fun := linear_map.to_fun,
map_add' := λ f g, rfl,
map_smul' := λ c f, rfl }
end linear_map
end is_scalar_tower
/-! TODO: The following lemmas no longer involve `algebra` at all, and could be moved closer
to `algebra/module/submodule.lean`. Currently this is tricky because `ker`, `range`, `⊤`, and `⊥`
are all defined in `linear_algebra/basic.lean`. -/
section module
open module
variables (R S M N : Type*) [semiring R] [semiring S] [has_smul R S]
variables [add_comm_monoid M] [module R M] [module S M] [is_scalar_tower R S M]
variables [add_comm_monoid N] [module R N] [module S N] [is_scalar_tower R S N]
variables {S M N}
@[simp]
lemma linear_map.ker_restrict_scalars (f : M →ₗ[S] N) :
(f.restrict_scalars R).ker = f.ker.restrict_scalars R :=
rfl
end module
namespace submodule
variables (R A M : Type*)
variables [comm_semiring R] [semiring A] [algebra R A] [add_comm_monoid M]
variables [module R M] [module A M] [is_scalar_tower R A M]
/-- If `A` is an `R`-algebra such that the induced morhpsim `R →+* A` is surjective, then the
`R`-module generated by a set `X` equals the `A`-module generated by `X`. -/
lemma span_eq_restrict_scalars (X : set M) (hsur : function.surjective (algebra_map R A)) :
span R X = restrict_scalars R (span A X) :=
begin
apply (span_le_restrict_scalars R A X).antisymm (λ m hm, _),
refine span_induction hm subset_span (zero_mem _) (λ _ _, add_mem) (λ a m hm, _),
obtain ⟨r, rfl⟩ := hsur a,
simpa [algebra_map_smul] using smul_mem _ r hm
end
end submodule
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w} {I : Type*}
variables [comm_semiring R] [semiring A] [semiring B]
variables [algebra R A] [algebra R B]
/-- `R`-algebra homomorphism between the function spaces `I → A` and `I → B`, induced by an
`R`-algebra homomorphism `f` between `A` and `B`. -/
@[simps] protected def comp_left (f : A →ₐ[R] B) (I : Type*) : (I → A) →ₐ[R] (I → B) :=
{ to_fun := λ h, f ∘ h,
commutes' := λ c, by { ext, exact f.commutes' c },
.. f.to_ring_hom.comp_left I }
end alg_hom
example {R A} [comm_semiring R] [semiring A]
[module R A] [smul_comm_class R A A] [is_scalar_tower R A A] : algebra R A :=
algebra.of_module smul_mul_assoc mul_smul_comm
|
1c17880e3b10d418e72bc2bf3a6a1c3a702fdedc | 4727251e0cd73359b15b664c3170e5d754078599 | /src/order/filter/countable_Inter.lean | 48c7dc690b1761107478efc98431417b94bd6256 | [
"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 | 8,471 | 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 order.filter.basic
import data.set.countable
/-!
# Filters with countable intersection property
In this file we define `countable_Inter_filter` to be the class of filters with the following
property: for any countable collection of sets `s ∈ l` their intersection belongs to `l` as well.
Two main examples are the `residual` filter defined in `topology.metric_space.baire` and
the `measure.ae` filter defined in `measure_theory.measure_space`.
We reformulate the definition in terms of indexed intersection and in terms of `filter.eventually`
and provide instances for some basic constructions (`⊥`, `⊤`, `filter.principal`, `filter.map`,
`filter.comap`, `has_inf.inf`). We also provide a custom constructor `filter.of_countable_Inter`
that deduces two axioms of a `filter` from the countable intersection property.
## Tags
filter, countable
-/
open set filter
open_locale filter
variables {ι α β : Type*}
/-- A filter `l` has the countable intersection property if for any countable collection
of sets `s ∈ l` their intersection belongs to `l` as well. -/
class countable_Inter_filter (l : filter α) : Prop :=
(countable_sInter_mem' :
∀ {S : set (set α)} (hSc : countable S) (hS : ∀ s ∈ S, s ∈ l), ⋂₀ S ∈ l)
variables {l : filter α} [countable_Inter_filter l]
lemma countable_sInter_mem {S : set (set α)} (hSc : countable S) :
⋂₀ S ∈ l ↔ ∀ s ∈ S, s ∈ l :=
⟨λ hS s hs, mem_of_superset hS (sInter_subset_of_mem hs),
countable_Inter_filter.countable_sInter_mem' hSc⟩
lemma countable_Inter_mem [encodable ι] {s : ι → set α} :
(⋂ i, s i) ∈ l ↔ ∀ i, s i ∈ l :=
sInter_range s ▸ (countable_sInter_mem (countable_range _)).trans forall_range_iff
lemma countable_bInter_mem {S : set ι} (hS : countable S) {s : Π i ∈ S, set α} :
(⋂ i ∈ S, s i ‹_›) ∈ l ↔ ∀ i ∈ S, s i ‹_› ∈ l :=
begin
rw [bInter_eq_Inter],
haveI := hS.to_encodable,
exact countable_Inter_mem.trans subtype.forall
end
lemma eventually_countable_forall [encodable ι] {p : α → ι → Prop} :
(∀ᶠ x in l, ∀ i, p x i) ↔ ∀ i, ∀ᶠ x in l, p x i :=
by simpa only [filter.eventually, set_of_forall]
using @countable_Inter_mem _ _ l _ _ (λ i, {x | p x i})
lemma eventually_countable_ball {S : set ι} (hS : countable S) {p : Π (x : α) (i ∈ S), Prop} :
(∀ᶠ x in l, ∀ i ∈ S, p x i ‹_›) ↔ ∀ i ∈ S, ∀ᶠ x in l, p x i ‹_› :=
by simpa only [filter.eventually, set_of_forall]
using @countable_bInter_mem _ _ l _ _ hS (λ i hi, {x | p x i hi})
lemma eventually_le.countable_Union [encodable ι] {s t : ι → set α} (h : ∀ i, s i ≤ᶠ[l] t i) :
(⋃ i, s i) ≤ᶠ[l] ⋃ i, t i :=
(eventually_countable_forall.2 h).mono $ λ x hst hs, mem_Union.2 $
(mem_Union.1 hs).imp hst
lemma eventually_eq.countable_Union [encodable ι] {s t : ι → set α} (h : ∀ i, s i =ᶠ[l] t i) :
(⋃ i, s i) =ᶠ[l] ⋃ i, t i :=
(eventually_le.countable_Union (λ i, (h i).le)).antisymm
(eventually_le.countable_Union (λ i, (h i).symm.le))
lemma eventually_le.countable_bUnion {S : set ι} (hS : countable S) {s t : Π i ∈ S, set α}
(h : ∀ i ∈ S, s i ‹_› ≤ᶠ[l] t i ‹_›) : (⋃ i ∈ S, s i ‹_›) ≤ᶠ[l] ⋃ i ∈ S, t i ‹_› :=
begin
simp only [bUnion_eq_Union],
haveI := hS.to_encodable,
exact eventually_le.countable_Union (λ i, h i i.2)
end
lemma eventually_eq.countable_bUnion {S : set ι} (hS : countable S) {s t : Π i ∈ S, set α}
(h : ∀ i ∈ S, s i ‹_› =ᶠ[l] t i ‹_›) : (⋃ i ∈ S, s i ‹_›) =ᶠ[l] ⋃ i ∈ S, t i ‹_› :=
(eventually_le.countable_bUnion hS (λ i hi, (h i hi).le)).antisymm
(eventually_le.countable_bUnion hS (λ i hi, (h i hi).symm.le))
lemma eventually_le.countable_Inter [encodable ι] {s t : ι → set α} (h : ∀ i, s i ≤ᶠ[l] t i) :
(⋂ i, s i) ≤ᶠ[l] ⋂ i, t i :=
(eventually_countable_forall.2 h).mono $ λ x hst hs, mem_Inter.2 $ λ i, hst _ (mem_Inter.1 hs i)
lemma eventually_eq.countable_Inter [encodable ι] {s t : ι → set α} (h : ∀ i, s i =ᶠ[l] t i) :
(⋂ i, s i) =ᶠ[l] ⋂ i, t i :=
(eventually_le.countable_Inter (λ i, (h i).le)).antisymm
(eventually_le.countable_Inter (λ i, (h i).symm.le))
lemma eventually_le.countable_bInter {S : set ι} (hS : countable S) {s t : Π i ∈ S, set α}
(h : ∀ i ∈ S, s i ‹_› ≤ᶠ[l] t i ‹_›) : (⋂ i ∈ S, s i ‹_›) ≤ᶠ[l] ⋂ i ∈ S, t i ‹_› :=
begin
simp only [bInter_eq_Inter],
haveI := hS.to_encodable,
exact eventually_le.countable_Inter (λ i, h i i.2)
end
lemma eventually_eq.countable_bInter {S : set ι} (hS : countable S) {s t : Π i ∈ S, set α}
(h : ∀ i ∈ S, s i ‹_› =ᶠ[l] t i ‹_›) : (⋂ i ∈ S, s i ‹_›) =ᶠ[l] ⋂ i ∈ S, t i ‹_› :=
(eventually_le.countable_bInter hS (λ i hi, (h i hi).le)).antisymm
(eventually_le.countable_bInter hS (λ i hi, (h i hi).symm.le))
/-- Construct a filter with countable intersection property. This constructor deduces
`filter.univ_sets` and `filter.inter_sets` from the countable intersection property. -/
def filter.of_countable_Inter (l : set (set α))
(hp : ∀ S : set (set α), countable S → S ⊆ l → (⋂₀ S) ∈ l)
(h_mono : ∀ s t, s ∈ l → s ⊆ t → t ∈ l) :
filter α :=
{ sets := l,
univ_sets := @sInter_empty α ▸ hp _ countable_empty (empty_subset _),
sets_of_superset := h_mono,
inter_sets := λ s t hs ht, sInter_pair s t ▸
hp _ ((countable_singleton _).insert _) (insert_subset.2 ⟨hs, singleton_subset_iff.2 ht⟩) }
instance filter.countable_Inter_of_countable_Inter (l : set (set α))
(hp : ∀ S : set (set α), countable S → S ⊆ l → (⋂₀ S) ∈ l)
(h_mono : ∀ s t, s ∈ l → s ⊆ t → t ∈ l) :
countable_Inter_filter (filter.of_countable_Inter l hp h_mono) := ⟨hp⟩
@[simp] lemma filter.mem_of_countable_Inter {l : set (set α)}
(hp : ∀ S : set (set α), countable S → S ⊆ l → (⋂₀ S) ∈ l)
(h_mono : ∀ s t, s ∈ l → s ⊆ t → t ∈ l) {s : set α} :
s ∈ filter.of_countable_Inter l hp h_mono ↔ s ∈ l :=
iff.rfl
instance countable_Inter_filter_principal (s : set α) : countable_Inter_filter (𝓟 s) :=
⟨λ S hSc hS, subset_sInter hS⟩
instance countable_Inter_filter_bot : countable_Inter_filter (⊥ : filter α) :=
by { rw ← principal_empty, apply countable_Inter_filter_principal }
instance countable_Inter_filter_top : countable_Inter_filter (⊤ : filter α) :=
by { rw ← principal_univ, apply countable_Inter_filter_principal }
instance (l : filter β) [countable_Inter_filter l] (f : α → β) :
countable_Inter_filter (comap f l) :=
begin
refine ⟨λ S hSc hS, _⟩,
choose! t htl ht using hS,
have : (⋂ s ∈ S, t s) ∈ l, from (countable_bInter_mem hSc).2 htl,
refine ⟨_, this, _⟩,
simpa [preimage_Inter] using (Inter₂_mono ht)
end
instance (l : filter α) [countable_Inter_filter l] (f : α → β) :
countable_Inter_filter (map f l) :=
begin
constructor, intros S hSc hS,
simp only [mem_map, sInter_eq_bInter, preimage_Inter₂] at hS ⊢,
exact (countable_bInter_mem hSc).2 hS
end
/-- Infimum of two `countable_Inter_filter`s is a `countable_Inter_filter`. This is useful, e.g.,
to automatically get an instance for `residual α ⊓ 𝓟 s`. -/
instance countable_Inter_filter_inf (l₁ l₂ : filter α) [countable_Inter_filter l₁]
[countable_Inter_filter l₂] :
countable_Inter_filter (l₁ ⊓ l₂) :=
begin
refine ⟨λ S hSc hS, _⟩,
choose s hs t ht hst using hS,
replace hs : (⋂ i ∈ S, s i ‹_›) ∈ l₁ := (countable_bInter_mem hSc).2 hs,
replace ht : (⋂ i ∈ S, t i ‹_›) ∈ l₂ := (countable_bInter_mem hSc).2 ht,
refine mem_of_superset (inter_mem_inf hs ht) (subset_sInter $ λ i hi, _),
rw hst i hi,
apply inter_subset_inter ; exact Inter_subset_of_subset i (Inter_subset _ _)
end
/-- Supremum of two `countable_Inter_filter`s is a `countable_Inter_filter`. -/
instance countable_Inter_filter_sup (l₁ l₂ : filter α) [countable_Inter_filter l₁]
[countable_Inter_filter l₂] :
countable_Inter_filter (l₁ ⊔ l₂) :=
begin
refine ⟨λ S hSc hS, ⟨_, _⟩⟩; refine (countable_sInter_mem hSc).2 (λ s hs, _),
exacts [(hS s hs).1, (hS s hs).2]
end
|
192650fdde4837a99fe295ecaa9f79e8294e144d | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/data/nat/multiplicity.lean | d5b072a69172107cd23fce5f3a5d25e0454a0be7 | [
"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 | 9,426 | 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.choose.dvd
import ring_theory.multiplicity
import data.nat.modeq
import algebra.gcd_monoid
import data.finset.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
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 at least `n` -/
lemma multiplicity_eq_card_pow_dvd {m n b : ℕ} (hm1 : m ≠ 1) (hn0 : 0 < n) (hb : 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 ^ n ∣ 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_gt $ lt_pow_self
(lt_of_le_of_ne (nat.pos_of_ne_zero hm0) hm1.symm) _),
⟨λ 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 :=
by rw [multiplicity.one_right (mt nat.is_unit_iff.mp (ne_of_gt hp.one_lt))]
lemma multiplicity_mul {p m n : ℕ} (hp : p.prime) :
multiplicity p (m * n) = multiplicity p m + multiplicity p n :=
by rw [← int.coe_nat_multiplicity, ← int.coe_nat_multiplicity,
← int.coe_nat_multiplicity, int.coe_nat_mul, multiplicity.mul (nat.prime_iff_prime_int.1 hp)]
lemma multiplicity_pow {p m n : ℕ} (hp : p.prime) :
multiplicity p (m ^ n) = n •ℕ (multiplicity p m) :=
by induction n; simp [pow_succ', hp.multiplicity_mul, *, hp.multiplicity_one, succ_nsmul,
add_comm]
lemma multiplicity_self {p : ℕ} (hp : p.prime) : multiplicity p p = 1 :=
have h₁ : ¬ is_unit (p : ℤ), from mt is_unit_int.1 (ne_of_gt hp.one_lt),
have h₂ : (p : ℤ) ≠ 0, from int.coe_nat_ne_zero.2 hp.ne_zero,
by rw [← int.coe_nat_multiplicity, multiplicity_self h₁ h₂]
lemma multiplicity_pow_self {p n : ℕ} (hp : p.prime) : multiplicity p (p ^ n) = n :=
by induction n; simp [hp.multiplicity_one, pow_succ', hp.multiplicity_mul, *,
hp.multiplicity_self, succ_eq_add_one]
/-- The multiplicity of a prime in `fact 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 at least `n` -/
lemma multiplicity_fact {p : ℕ} (hp : p.prime) :
∀ {n b : ℕ}, n ≤ b → multiplicity p n.fact = (∑ 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).fact = multiplicity p n.fact + multiplicity p (n+1) :
by rw [fact_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_fact (le_of_succ_le 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)
/-- A prime power divides `fact 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 at least `n` -/
lemma pow_dvd_fact_iff {p : ℕ} {n r b : ℕ} (hp : p.prime) (hbn : n ≤ b) :
p ^ r ∣ fact n ↔ r ≤ ∑ i in Ico 1 b, n / p ^ i :=
by rw [← enat.coe_le_coe, ← hp.multiplicity_fact 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 _)]
... = _ : begin simp only [sum_add_distrib], simp [sum_boole], end -- we have to use `sum_add_distrib` before `add_ite` fires.
/-- 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 at least `n`. -/
lemma multiplicity_choose {p n k b : ℕ} (hp : p.prime) (hkn : k ≤ n) (hnb : 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.fact * (n - k).fact) =
((finset.Ico 1 b).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card +
multiplicity p (k.fact * (n - k).fact),
begin
rw [← hp.multiplicity_mul, ← mul_assoc, choose_mul_fact_mul_fact hkn,
hp.multiplicity_fact hnb, hp.multiplicity_mul, hp.multiplicity_fact (le_trans hkn hnb),
hp.multiplicity_fact (le_trans (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 (fact_pos k) (fact_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) (le_refl _),
multiplicity_eq_card_pow_dvd (ne_of_gt hp.one_lt) (nat.pos_of_ne_zero hk0) (le_of_not_gt hkn),
multiplicity_eq_card_pow_dvd (ne_of_gt hp.one_lt) (nat.pos_of_ne_zero hn0) (le_refl _),
← enat.coe_add, enat.coe_le_coe],
calc ((Ico 1 n).filter (λ i, p ^ i ∣ n)).card
≤ ((Ico 1 n).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i) ∪
(Ico 1 n).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 n).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card +
((Ico 1 n).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 (p ^ n)).filter (λ i, p ^ i ≤ k % p ^ i + (p ^ n - k) % p ^ i))
((Ico 1 (p ^ n)).filter (λ i, p ^ i ∣ k)),
by simp [disjoint_right, *, dvd_iff_mod_eq_zero, nat.mod_lt _ (pow_pos hp.pos _)]
{contextual := tt},
have filter_subset_Ico : filter (λ i, p ^ i ≤ k % p ^ i +
(p ^ n - k) % p ^ i ∨ p ^ i ∣ k) (Ico 1 (p ^ n)) ⊆ Ico 1 n.succ,
from begin
simp only [finset.subset_iff, Ico.mem, mem_filter, and_imp, true_and] {contextual := tt},
assume i h1i hip h,
refine lt_succ_of_le (le_of_not_gt (λ hin, _)),
have hpik : ¬ p ^ i ∣ k, from mt (le_of_dvd hk0)
(not_le_of_gt (lt_of_le_of_lt hkn (pow_right_strict_mono hp.two_le hin))),
have hpn : k % p ^ i + (p ^ n - k) % p ^ i < p ^ i,
from calc k % p ^ i + (p ^ n - k) % p ^ i
≤ k + (p ^ n - k) : add_le_add (mod_le _ _) (mod_le _ _)
... = p ^ n : nat.add_sub_cancel' hkn
... < p ^ i : pow_right_strict_mono hp.two_le hin,
simpa [hpik, not_le_of_gt hpn] using h
end,
begin
rw [multiplicity_choose hp hkn (le_refl _),
multiplicity_eq_card_pow_dvd (ne_of_gt hp.one_lt) hk0 hkn, ← enat.coe_add,
enat.coe_le_coe, ← card_disjoint_union hdisj, filter_union_right],
exact le_trans (card_le_of_subset filter_subset_Ico) (by simp)
end)
(by rw [← hp.multiplicity_pow_self];
exact multiplicity_le_multiplicity_choose_add hp _ _)
end prime
end nat
|
32b60efd912d4c5d6e80e6531c238ba8de49b845 | b3fced0f3ff82d577384fe81653e47df68bb2fa1 | /src/ring_theory/ideals.lean | ccede65329501978c4736f23ae2c643e4413032e | [
"Apache-2.0"
] | permissive | ratmice/mathlib | 93b251ef5df08b6fd55074650ff47fdcc41a4c75 | 3a948a6a4cd5968d60e15ed914b1ad2f4423af8d | refs/heads/master | 1,599,240,104,318 | 1,572,981,183,000 | 1,572,981,183,000 | 219,830,178 | 0 | 0 | Apache-2.0 | 1,572,980,897,000 | 1,572,980,896,000 | null | UTF-8 | Lean | false | false | 18,923 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Mario Carneiro
-/
import algebra.associated linear_algebra.basic order.zorn
universes u v
variables {α : Type u} {β : Type v} {a b : α}
open set function lattice
open_locale classical
namespace ideal
variables [comm_ring α] (I : ideal α)
@[extensionality] lemma ext {I J : ideal α} (h : ∀ x, x ∈ I ↔ x ∈ J) : I = J :=
submodule.ext h
theorem eq_top_of_unit_mem
(x y : α) (hx : x ∈ I) (h : y * x = 1) : I = ⊤ :=
eq_top_iff.2 $ λ z _, calc
z = z * (y * x) : by simp [h]
... = (z * y) * x : eq.symm $ mul_assoc z y x
... ∈ I : I.mul_mem_left hx
theorem eq_top_of_is_unit_mem {x} (hx : x ∈ I) (h : is_unit x) : I = ⊤ :=
let ⟨y, hy⟩ := is_unit_iff_exists_inv'.1 h in eq_top_of_unit_mem I x y hx hy
theorem eq_top_iff_one : I = ⊤ ↔ (1:α) ∈ I :=
⟨by rintro rfl; trivial,
λ h, eq_top_of_unit_mem _ _ 1 h (by simp)⟩
theorem ne_top_iff_one : I ≠ ⊤ ↔ (1:α) ∉ I :=
not_congr I.eq_top_iff_one
def span (s : set α) : ideal α := submodule.span α s
lemma subset_span {s : set α} : s ⊆ span s := submodule.subset_span
lemma span_le {s : set α} {I} : span s ≤ I ↔ s ⊆ I := submodule.span_le
lemma span_mono {s t : set α} : s ⊆ t → span s ≤ span t := submodule.span_mono
@[simp] lemma span_eq : span (I : set α) = I := submodule.span_eq _
@[simp] lemma span_singleton_one : span ({1} : set α) = ⊤ :=
(eq_top_iff_one _).2 $ subset_span $ mem_singleton _
lemma mem_span_insert {s : set α} {x y} :
x ∈ span (insert y s) ↔ ∃ a (z ∈ span s), x = a * y + z := submodule.mem_span_insert
lemma mem_span_insert' {s : set α} {x y} :
x ∈ span (insert y s) ↔ ∃a, x + a * y ∈ span s := submodule.mem_span_insert'
lemma mem_span_singleton' {x y : α} :
x ∈ span ({y} : set α) ↔ ∃ a, a * y = x := submodule.mem_span_singleton
lemma mem_span_singleton {x y : α} :
x ∈ span ({y} : set α) ↔ y ∣ x :=
mem_span_singleton'.trans $ exists_congr $ λ _, by rw [eq_comm, mul_comm]; refl
lemma span_singleton_le_span_singleton {x y : α} :
span ({x} : set α) ≤ span ({y} : set α) ↔ y ∣ x :=
span_le.trans $ singleton_subset_iff.trans mem_span_singleton
lemma span_eq_bot {s : set α} : span s = ⊥ ↔ ∀ x ∈ s, (x:α) = 0 := submodule.span_eq_bot
lemma span_singleton_eq_bot {x} : span ({x} : set α) = ⊥ ↔ x = 0 := submodule.span_singleton_eq_bot
lemma span_singleton_eq_top {x} : span ({x} : set α) = ⊤ ↔ is_unit x :=
by rw [is_unit_iff_dvd_one, ← span_singleton_le_span_singleton, span_singleton_one, eq_top_iff]
@[class] def is_prime (I : ideal α) : Prop :=
I ≠ ⊤ ∧ ∀ {x y : α}, x * y ∈ I → x ∈ I ∨ y ∈ I
theorem is_prime.mem_or_mem {I : ideal α} (hI : I.is_prime) :
∀ {x y : α}, x * y ∈ I → x ∈ I ∨ y ∈ I := hI.2
theorem is_prime.mem_or_mem_of_mul_eq_zero {I : ideal α} (hI : I.is_prime)
{x y : α} (h : x * y = 0) : x ∈ I ∨ y ∈ I :=
hI.2 (h.symm ▸ I.zero_mem)
theorem is_prime.mem_of_pow_mem {I : ideal α} (hI : I.is_prime)
{r : α} (n : ℕ) (H : r^n ∈ I) : r ∈ I :=
begin
induction n with n ih,
{ exact (mt (eq_top_iff_one _).2 hI.1).elim H },
exact or.cases_on (hI.mem_or_mem H) id ih
end
theorem zero_ne_one_of_proper {I : ideal α} (h : I ≠ ⊤) : (0:α) ≠ 1 :=
λ hz, I.ne_top_iff_one.1 h $ hz ▸ I.zero_mem
theorem span_singleton_prime {p : α} (hp : p ≠ 0) :
is_prime (span ({p} : set α)) ↔ prime p :=
by simp [is_prime, prime, span_singleton_eq_top, hp, mem_span_singleton]
@[class] def is_maximal (I : ideal α) : Prop :=
I ≠ ⊤ ∧ ∀ J, I < J → J = ⊤
theorem is_maximal_iff {I : ideal α} : I.is_maximal ↔
(1:α) ∉ I ∧ ∀ (J : ideal α) x, I ≤ J → x ∉ I → x ∈ J → (1:α) ∈ J :=
and_congr I.ne_top_iff_one $ forall_congr $ λ J,
by rw [lt_iff_le_not_le]; exact
⟨λ H x h hx₁ hx₂, J.eq_top_iff_one.1 $
H ⟨h, not_subset.2 ⟨_, hx₂, hx₁⟩⟩,
λ H ⟨h₁, h₂⟩, let ⟨x, xJ, xI⟩ := not_subset.1 h₂ in
J.eq_top_iff_one.2 $ H x h₁ xI xJ⟩
theorem is_maximal.eq_of_le {I J : ideal α}
(hI : I.is_maximal) (hJ : J ≠ ⊤) (IJ : I ≤ J) : I = J :=
eq_iff_le_not_lt.2 ⟨IJ, λ h, hJ (hI.2 _ h)⟩
theorem is_maximal.exists_inv {I : ideal α}
(hI : I.is_maximal) {x} (hx : x ∉ I) : ∃ y, y * x - 1 ∈ I :=
begin
cases is_maximal_iff.1 hI with H₁ H₂,
rcases mem_span_insert'.1 (H₂ (span (insert x I)) x
(set.subset.trans (subset_insert _ _) subset_span)
hx (subset_span (mem_insert _ _))) with ⟨y, hy⟩,
rw [span_eq, ← neg_mem_iff, add_comm, neg_add', neg_mul_eq_neg_mul] at hy,
exact ⟨-y, hy⟩
end
theorem is_maximal.is_prime {I : ideal α} (H : I.is_maximal) : I.is_prime :=
⟨H.1, λ x y hxy, or_iff_not_imp_left.2 $ λ hx, begin
cases H.exists_inv hx with z hz,
have := I.mul_mem_left hz,
rw [mul_sub, mul_one, mul_comm, mul_assoc] at this,
exact I.neg_mem_iff.1 ((I.add_mem_iff_right $ I.mul_mem_left hxy).1 this)
end⟩
instance is_maximal.is_prime' (I : ideal α) : ∀ [H : I.is_maximal], I.is_prime := is_maximal.is_prime
theorem exists_le_maximal (I : ideal α) (hI : I ≠ ⊤) :
∃ M : ideal α, M.is_maximal ∧ I ≤ M :=
begin
rcases zorn.zorn_partial_order₀ { J : ideal α | J ≠ ⊤ } _ I hI with ⟨M, M0, IM, h⟩,
{ refine ⟨M, ⟨M0, λ J hJ, by_contradiction $ λ J0, _⟩, IM⟩,
cases h J J0 (le_of_lt hJ), exact lt_irrefl _ hJ },
{ intros S SC cC I IS,
refine ⟨Sup S, λ H, _, λ _, le_Sup⟩,
rcases submodule.mem_Sup_of_directed ((eq_top_iff_one _).1 H) I IS cC.directed_on with ⟨J, JS, J0⟩,
exact SC JS ((eq_top_iff_one _).2 J0) }
end
def is_coprime (x y : α) : Prop :=
span ({x, y} : set α) = ⊤
theorem mem_span_pair {x y z : α} :
z ∈ span (insert y {x} : set α) ↔ ∃ a b, a * x + b * y = z :=
begin
simp only [mem_span_insert, mem_span_singleton', exists_prop],
split,
{ rintros ⟨a, b, ⟨c, hc⟩, h⟩,
exact ⟨c, a, by simp [h, hc]⟩ },
{ rintro ⟨b, c, e⟩, exact ⟨c, b * x, ⟨b, rfl⟩, by simp [e.symm]⟩ }
end
theorem is_coprime_def {x y : α} :
is_coprime x y ↔ ∀ z, ∃ a b, a * x + b * y = z :=
by simp [is_coprime, submodule.eq_top_iff', mem_span_pair]
theorem is_coprime_self {x : α} :
is_coprime x x ↔ is_unit x :=
by rw [← span_singleton_eq_top]; simp [is_coprime]
lemma span_singleton_lt_span_singleton [integral_domain β] {x y : β} :
span ({x} : set β) < span ({y} : set β) ↔ y ≠ 0 ∧ ∃ d : β, ¬ is_unit d ∧ x = y * d :=
by rw [lt_iff_le_not_le, span_singleton_le_span_singleton, span_singleton_le_span_singleton,
dvd_and_not_dvd_iff]
def quotient (I : ideal α) := I.quotient
namespace quotient
variables {I} {x y : α}
def mk (I : ideal α) (a : α) : I.quotient := submodule.quotient.mk a
protected theorem eq : mk I x = mk I y ↔ x - y ∈ I := submodule.quotient.eq I
instance (I : ideal α) : has_one I.quotient := ⟨mk I 1⟩
@[simp] lemma mk_one (I : ideal α) : mk I 1 = 1 := rfl
instance (I : ideal α) : has_mul I.quotient :=
⟨λ a b, quotient.lift_on₂' a b (λ a b, mk I (a * b)) $
λ a₁ a₂ b₁ b₂ h₁ h₂, quot.sound $ begin
refine calc a₁ * a₂ - b₁ * b₂ = a₂ * (a₁ - b₁) + (a₂ - b₂) * b₁ : _
... ∈ I : I.add_mem (I.mul_mem_left h₁) (I.mul_mem_right h₂),
rw [mul_sub, sub_mul, sub_add_sub_cancel, mul_comm, mul_comm b₁]
end⟩
@[simp] theorem mk_mul : mk I (x * y) = mk I x * mk I y := rfl
instance (I : ideal α) : comm_ring I.quotient :=
{ mul := (*),
one := 1,
mul_assoc := λ a b c, quotient.induction_on₃' a b c $
λ a b c, congr_arg (mk _) (mul_assoc a b c),
mul_comm := λ a b, quotient.induction_on₂' a b $
λ a b, congr_arg (mk _) (mul_comm a b),
one_mul := λ a, quotient.induction_on' a $
λ a, congr_arg (mk _) (one_mul a),
mul_one := λ a, quotient.induction_on' a $
λ a, congr_arg (mk _) (mul_one a),
left_distrib := λ a b c, quotient.induction_on₃' a b c $
λ a b c, congr_arg (mk _) (left_distrib a b c),
right_distrib := λ a b c, quotient.induction_on₃' a b c $
λ a b c, congr_arg (mk _) (right_distrib a b c),
..submodule.quotient.add_comm_group I }
instance is_ring_hom_mk (I : ideal α) : is_ring_hom (mk I) :=
⟨rfl, λ _ _, rfl, λ _ _, rfl⟩
def map_mk (I J : ideal α) : ideal I.quotient :=
{ carrier := mk I '' J,
zero := ⟨0, J.zero_mem, rfl⟩,
add := by rintro _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩;
exact ⟨x + y, J.add_mem hx hy, rfl⟩,
smul := by rintro ⟨c⟩ _ ⟨x, hx, rfl⟩;
exact ⟨c * x, J.mul_mem_left hx, rfl⟩ }
@[simp] lemma mk_zero (I : ideal α) : mk I 0 = 0 := rfl
@[simp] lemma mk_add (I : ideal α) (a b : α) : mk I (a + b) = mk I a + mk I b := rfl
@[simp] lemma mk_neg (I : ideal α) (a : α) : mk I (-a : α) = -mk I a := rfl
@[simp] lemma mk_sub (I : ideal α) (a b : α) : mk I (a - b : α) = mk I a - mk I b := rfl
@[simp] lemma mk_pow (I : ideal α) (a : α) (n : ℕ) : mk I (a ^ n : α) = mk I a ^ n :=
by induction n; simp [*, pow_succ]
lemma eq_zero_iff_mem {I : ideal α} : mk I a = 0 ↔ a ∈ I :=
by conv {to_rhs, rw ← sub_zero a }; exact quotient.eq'
theorem zero_eq_one_iff {I : ideal α} : (0 : I.quotient) = 1 ↔ I = ⊤ :=
eq_comm.trans $ eq_zero_iff_mem.trans (eq_top_iff_one _).symm
theorem zero_ne_one_iff {I : ideal α} : (0 : I.quotient) ≠ 1 ↔ I ≠ ⊤ :=
not_congr zero_eq_one_iff
protected def nonzero_comm_ring {I : ideal α} (hI : I ≠ ⊤) : nonzero_comm_ring I.quotient :=
{ zero_ne_one := zero_ne_one_iff.2 hI, ..quotient.comm_ring I }
instance (I : ideal α) [hI : I.is_prime] : integral_domain I.quotient :=
{ eq_zero_or_eq_zero_of_mul_eq_zero := λ a b,
quotient.induction_on₂' a b $ λ a b hab,
(hI.mem_or_mem (eq_zero_iff_mem.1 hab)).elim
(or.inl ∘ eq_zero_iff_mem.2)
(or.inr ∘ eq_zero_iff_mem.2),
..quotient.nonzero_comm_ring hI.1 }
lemma exists_inv {I : ideal α} [hI : I.is_maximal] :
∀ {a : I.quotient}, a ≠ 0 → ∃ b : I.quotient, a * b = 1 :=
begin
rintro ⟨a⟩ h,
cases hI.exists_inv (mt eq_zero_iff_mem.2 h) with b hb,
rw [mul_comm] at hb,
exact ⟨mk _ b, quot.sound hb⟩
end
/-- quotient by maximal ideal is a field. def rather than instance, since users will have
computable inverses in some applications -/
protected noncomputable def field (I : ideal α) [hI : I.is_maximal] : discrete_field I.quotient :=
{ inv := λ a, if ha : a = 0 then 0 else classical.some (exists_inv ha),
mul_inv_cancel := λ a (ha : a ≠ 0), show a * dite _ _ _ = _,
by rw dif_neg ha;
exact classical.some_spec (exists_inv ha),
inv_mul_cancel := λ a (ha : a ≠ 0), show dite _ _ _ * a = _,
by rw [mul_comm, dif_neg ha];
exact classical.some_spec (exists_inv ha),
inv_zero := dif_pos rfl,
has_decidable_eq := classical.dec_eq _,
..quotient.integral_domain I }
variable [comm_ring β]
def lift (S : ideal α) (f : α → β) [is_ring_hom f] (H : ∀ (a : α), a ∈ S → f a = 0) :
quotient S → β :=
λ x, quotient.lift_on' x f $ λ (a b) (h : _ ∈ _),
eq_of_sub_eq_zero (by simpa only [is_ring_hom.map_sub f] using H _ h)
variables {S : ideal α} {f : α → β} [is_ring_hom f] {H : ∀ (a : α), a ∈ S → f a = 0}
@[simp] lemma lift_mk : lift S f H (mk S a) = f a := rfl
instance : is_ring_hom (lift S f H) :=
{ map_one := by show lift S f H (mk S 1) = 1; simp [is_ring_hom.map_one f, - mk_one],
map_add := λ a₁ a₂, quotient.induction_on₂' a₁ a₂ $ λ a₁ a₂, begin
show lift S f H (mk S a₁ + mk S a₂) = lift S f H (mk S a₁) + lift S f H (mk S a₂),
have := ideal.quotient.is_ring_hom_mk S,
rw ← this.map_add,
show lift S f H (mk S (a₁ + a₂)) = lift S f H (mk S a₁) + lift S f H (mk S a₂),
simp only [lift_mk, is_ring_hom.map_add f],
end,
map_mul := λ a₁ a₂, quotient.induction_on₂' a₁ a₂ $ λ a₁ a₂, begin
show lift S f H (mk S a₁ * mk S a₂) = lift S f H (mk S a₁) * lift S f H (mk S a₂),
have := ideal.quotient.is_ring_hom_mk S,
rw ← this.map_mul,
show lift S f H (mk S (a₁ * a₂)) = lift S f H (mk S a₁) * lift S f H (mk S a₂),
simp only [lift_mk, is_ring_hom.map_mul f],
end }
end quotient
lemma eq_bot_or_top {K : Type u} [discrete_field K] (I : ideal K) :
I = ⊥ ∨ I = ⊤ :=
begin
rw classical.or_iff_not_imp_right,
change _ ≠ _ → _,
rw ideal.ne_top_iff_one,
intro h1,
rw eq_bot_iff,
intros r hr,
by_cases H : r = 0, {simpa},
simpa [H, h1] using submodule.smul_mem I r⁻¹ hr,
end
lemma eq_bot_of_prime {K : Type u} [discrete_field K] (I : ideal K) [h : I.is_prime] :
I = ⊥ :=
classical.or_iff_not_imp_right.mp I.eq_bot_or_top h.1
end ideal
def nonunits (α : Type u) [monoid α] : set α := { a | ¬is_unit a }
@[simp] theorem mem_nonunits_iff [comm_monoid α] : a ∈ nonunits α ↔ ¬ is_unit a := iff.rfl
theorem mul_mem_nonunits_right [comm_monoid α] :
b ∈ nonunits α → a * b ∈ nonunits α :=
mt is_unit_of_mul_is_unit_right
theorem mul_mem_nonunits_left [comm_monoid α] :
a ∈ nonunits α → a * b ∈ nonunits α :=
mt is_unit_of_mul_is_unit_left
theorem zero_mem_nonunits [semiring α] : 0 ∈ nonunits α ↔ (0:α) ≠ 1 :=
not_congr is_unit_zero_iff
@[simp] theorem one_not_mem_nonunits [monoid α] : (1:α) ∉ nonunits α :=
not_not_intro is_unit_one
theorem coe_subset_nonunits [comm_ring α] {I : ideal α} (h : I ≠ ⊤) :
(I : set α) ⊆ nonunits α :=
λ x hx hu, h $ I.eq_top_of_is_unit_mem hx hu
lemma exists_max_ideal_of_mem_nonunits [comm_ring α] (h : a ∈ nonunits α) :
∃ I : ideal α, I.is_maximal ∧ a ∈ I :=
begin
have : ideal.span ({a} : set α) ≠ ⊤,
{ intro H, rw ideal.span_singleton_eq_top at H, contradiction },
rcases ideal.exists_le_maximal _ this with ⟨I, Imax, H⟩,
use [I, Imax], apply H, apply ideal.subset_span, exact set.mem_singleton a
end
class local_ring (α : Type u) extends nonzero_comm_ring α :=
(is_local : ∀ (a : α), (is_unit a) ∨ (is_unit (1 - a)))
namespace local_ring
variable [local_ring α]
instance : comm_ring α := by apply_instance
lemma is_unit_or_is_unit_one_sub_self (a : α) :
(is_unit a) ∨ (is_unit (1 - a)) :=
is_local a
lemma is_unit_of_mem_nonunits_one_sub_self (a : α) (h : (1 - a) ∈ nonunits α) :
is_unit a :=
or_iff_not_imp_right.1 (is_local a) h
lemma is_unit_one_sub_self_of_mem_nonunits (a : α) (h : a ∈ nonunits α) :
is_unit (1 - a) :=
or_iff_not_imp_left.1 (is_local a) h
lemma nonunits_add {x y} (hx : x ∈ nonunits α) (hy : y ∈ nonunits α) :
x + y ∈ nonunits α :=
begin
rintros ⟨u, hu⟩,
apply hy,
suffices : is_unit ((↑u⁻¹ : α) * y),
{ rcases this with ⟨s, hs⟩,
use u * s,
convert congr_arg (λ z, (u : α) * z) hs,
rw ← mul_assoc, simp },
rw show (↑u⁻¹ * y) = (1 - ↑u⁻¹ * x),
{ rw eq_sub_iff_add_eq,
replace hu := congr_arg (λ z, (↑u⁻¹ : α) * z) hu,
simpa [mul_add] using hu },
apply is_unit_one_sub_self_of_mem_nonunits,
exact mul_mem_nonunits_right hx
end
variable (α)
def nonunits_ideal : ideal α :=
{ carrier := nonunits α,
zero := zero_mem_nonunits.2 $ zero_ne_one,
add := λ x y hx hy, nonunits_add hx hy,
smul := λ a x, mul_mem_nonunits_right }
instance nonunits_ideal.is_maximal : (nonunits_ideal α).is_maximal :=
begin
rw ideal.is_maximal_iff,
split,
{ intro h, apply h, exact is_unit_one },
{ intros I x hI hx H,
erw not_not at hx,
rcases hx with ⟨u,rfl⟩,
simpa using I.smul_mem ↑u⁻¹ H }
end
lemma max_ideal_unique :
∃! I : ideal α, I.is_maximal :=
⟨nonunits_ideal α, nonunits_ideal.is_maximal α,
λ I hI, hI.eq_of_le (nonunits_ideal.is_maximal α).1 $
λ x hx, hI.1 ∘ I.eq_top_of_is_unit_mem hx⟩
variable {α}
@[simp] lemma mem_nonunits_ideal (x) :
x ∈ nonunits_ideal α ↔ x ∈ nonunits α := iff.rfl
end local_ring
def is_local_ring (α : Type u) [comm_ring α] : Prop :=
((0:α) ≠ 1) ∧ ∀ (a : α), (is_unit a) ∨ (is_unit (1 - a))
def local_of_is_local_ring [comm_ring α] (h : is_local_ring α) : local_ring α :=
{ zero_ne_one := h.1,
is_local := h.2,
.. ‹comm_ring α› }
def local_of_unit_or_unit_one_sub [comm_ring α] (hnze : (0:α) ≠ 1)
(h : ∀ x : α, is_unit x ∨ is_unit (1 - x)) : local_ring α :=
local_of_is_local_ring ⟨hnze, h⟩
def local_of_nonunits_ideal [comm_ring α] (hnze : (0:α) ≠ 1)
(h : ∀ x y ∈ nonunits α, x + y ∈ nonunits α) : local_ring α :=
local_of_is_local_ring ⟨hnze,
λ x, or_iff_not_imp_left.mpr $ λ hx,
begin
by_contra H,
apply h _ _ hx H,
simp [-sub_eq_add_neg, add_sub_cancel'_right]
end⟩
def local_of_unique_max_ideal [comm_ring α] (h : ∃! I : ideal α, I.is_maximal) :
local_ring α :=
local_of_nonunits_ideal
(let ⟨I, Imax, _⟩ := h in (λ (H : 0 = 1), Imax.1 $ I.eq_top_iff_one.2 $ H ▸ I.zero_mem))
$ λ x y hx hy H,
let ⟨I, Imax, Iuniq⟩ := h in
let ⟨Ix, Ixmax, Hx⟩ := exists_max_ideal_of_mem_nonunits hx in
let ⟨Iy, Iymax, Hy⟩ := exists_max_ideal_of_mem_nonunits hy in
have xmemI : x ∈ I, from ((Iuniq Ix Ixmax) ▸ Hx),
have ymemI : y ∈ I, from ((Iuniq Iy Iymax) ▸ Hy),
Imax.1 $ I.eq_top_of_is_unit_mem (I.add_mem xmemI ymemI) H
class is_local_ring_hom [comm_ring α] [comm_ring β] (f : α → β) extends is_ring_hom f : Prop :=
(map_nonunit : ∀ a, is_unit (f a) → is_unit a)
@[simp] lemma is_unit_of_map_unit [comm_ring α] [comm_ring β] (f : α → β) [is_local_ring_hom f]
(a) (h : is_unit (f a)) : is_unit a :=
is_local_ring_hom.map_nonunit a h
section
open local_ring
variables [local_ring α] [local_ring β]
variables (f : α → β) [is_local_ring_hom f]
lemma map_nonunit (a) (h : a ∈ nonunits_ideal α) : f a ∈ nonunits_ideal β :=
λ H, h $ is_unit_of_map_unit f a H
end
namespace local_ring
variables [local_ring α] [local_ring β]
variable (α)
def residue_field := (nonunits_ideal α).quotient
namespace residue_field
noncomputable instance : discrete_field (residue_field α) :=
ideal.quotient.field (nonunits_ideal α)
variables {α β}
noncomputable def map (f : α → β) [is_local_ring_hom f] :
residue_field α → residue_field β :=
ideal.quotient.lift (nonunits_ideal α) (ideal.quotient.mk _ ∘ f) $
λ a ha,
begin
erw ideal.quotient.eq_zero_iff_mem,
exact map_nonunit f a ha
end
instance map.is_field_hom (f : α → β) [is_local_ring_hom f] :
is_field_hom (map f) :=
ideal.quotient.is_ring_hom
end residue_field
end local_ring
namespace discrete_field
variables [discrete_field α]
instance : local_ring α :=
{ is_local := λ a,
if h : a = 0
then or.inr (by rw [h, sub_zero]; exact is_unit_one)
else or.inl $ is_unit_of_mul_one a a⁻¹ $ div_self h }
end discrete_field
|
d21f4c4941ce6eacfb5571b77158ec9423944af1 | 01ae0d022f2e2fefdaaa898938c1ac1fbce3b3ab | /categories/projects.lean | 24300de44074633250e3a61c6c276deb1ca47063 | [] | no_license | PatrickMassot/lean-category-theory | 0f56a83464396a253c28a42dece16c93baf8ad74 | ef239978e91f2e1c3b8e88b6e9c64c155dc56c99 | refs/heads/master | 1,629,739,187,316 | 1,512,422,659,000 | 1,512,422,659,000 | 113,098,786 | 0 | 0 | null | 1,512,424,022,000 | 1,512,424,022,000 | null | UTF-8 | Lean | false | false | 718 | lean | -- PROJECTS
-- equivalences can be turned into adjoint equivalences
-- Idempotent completion as a left adjoint to the forgetful 2-functor from categories to semicategories??
-- IDEAS for Lean:
-- extract a goal as a lemma
-- VSCode bugs:
-- "extension-output-#4" not found
-- file /settings not found
-- settings.json command expected
-- projects.lean.git not found
-- is "checking open files really working?" (e.g. can't find a trace message, even with all files open)
-- on rename of Lean file, delete olean file
-- Questions about Lean:
-- how can we use options for new tactics?
-- how can we store state across tactic invocations? (e.g. for profiling)
-- how can trace messages show the file and line number?
|
a4d825d1e79fd3965e244a14aecb6de810df788b | 5a5e1bb8063d7934afac91f30aa17c715821040b | /lean3SOS/src/to_mathlib/matrix.lean | 68f41f701fd7d3d88b375abaaf8941abfdcda476 | [] | no_license | ramonfmir/leanSOS | 1883392d73710db5c6e291a2abd03a6c5b44a42b | 14b50713dc887f6d408b7b2bce1f8af5bb619958 | refs/heads/main | 1,683,348,826,105 | 1,622,056,982,000 | 1,622,056,982,000 | 341,232,766 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,709 | lean | import data.matrix.basic
import linear_algebra.matrix
import linear_algebra.eigenspace
variables {γ : Type*} [fintype γ] [decidable_eq γ]
variables {μ : Type*} [fintype μ] [decidable_eq μ]
variables {R : Type*}
open_locale big_operators
open_locale matrix
namespace matrix
-- Properties of symmetric matrices.
def symmetric (M : matrix γ γ R) : Prop :=
∀ i j, M i j = M j i
lemma symmetric_diagonal [ring R] (D : γ → R)
: symmetric (diagonal D) :=
begin
intros i j, unfold diagonal, by_cases (i = j),
{ split_ifs, { rw h, }, { exfalso, exact h_1 h.symm, }, },
{ split_ifs, { exfalso, exact h h_1.symm, }, { refl, }, },
end
lemma symmetric_sum
[ring R]
{A B : matrix γ γ R}
(hA : symmetric A) (hB : symmetric B)
: symmetric (A + B) :=
begin
intros i j, simp, rw [hA i j, hB i j],
end
lemma symmetric_LDLT
[comm_ring R] (L : matrix γ γ R) (h : symmetric L) (D : γ → R)
: symmetric (Lᵀ ⬝ diagonal D ⬝ L) :=
begin
intros i j, rw ←matrix.transpose_apply (Lᵀ ⬝ diagonal D ⬝ L) j i,
rw [matrix.transpose_mul _ L, matrix.transpose_mul Lᵀ _],
rw [matrix.diagonal_transpose, matrix.transpose_transpose],
rw [matrix.mul_assoc],
end
-- Properties of the dot product.
-- TODO: to_mathlib
variable [linear_ordered_comm_ring R]
lemma dot_product_of_nonneg {v w : γ → R} (hv : 0 ≤ v) (hw : 0 ≤ w) : 0 ≤ dot_product v w :=
begin
simp only [dot_product], apply finset.sum_nonneg, intros x hx,
exact mul_nonneg (hv x) (hw x),
end
lemma vec_mul_self_nonneg (v : γ → R) : 0 ≤ v * v :=
λ i, mul_self_nonneg (v i)
lemma dot_product_diagonal_mul_vec_eq (d v w : γ → R) :
dot_product ((diagonal d).mul_vec v) w = dot_product d (v * w) :=
begin
simp only [dot_product], congr, ext i,
rw [mul_vec_diagonal, pi.mul_apply, mul_assoc],
end
lemma dot_product_self_nonneg (v : γ → R) : 0 ≤ dot_product v v :=
begin
simp only [dot_product], apply finset.sum_nonneg, intros x hx,
exact mul_self_nonneg (v x),
end
lemma dot_product_self_eq_zero_iff (v : γ → R) : dot_product v v = 0 ↔ v = 0 :=
begin
have hsumz := finset.sum_eq_zero_iff_of_nonneg (λ x hx, mul_self_nonneg (v x)), split,
{ intros hdp, ext x,
have hvx := (hsumz.1 hdp) x (finset.mem_univ x),
exact (mul_self_eq_zero.1 hvx), },
{ intros hvz, erw hsumz, intros x hx, dsimp,
erw [congr_fun hvz x], simp, }
end
lemma dot_product_self_pos_of_nonzero (v : γ → R) (hvnz : v ≠ 0) : dot_product v v > 0 :=
begin
have h1 := dot_product_self_nonneg v,
have h2 := ne_comm.1 ((not_iff_not_of_iff (dot_product_self_eq_zero_iff v)).2 hvnz),
exact (lt_of_le_of_ne h1 h2),
end
lemma dot_product_transpose (v : γ → R) (K L : matrix μ γ R)
: dot_product v ((Kᵀ ⬝ L).mul_vec v) = dot_product (K.mul_vec v) (L.mul_vec v) :=
begin
rw ←mul_vec_mul_vec, dsimp [mul_vec, dot_product],
have : ∑ i, (v i) * (∑ j, K j i * ∑ k, L j k * v k) =
∑ i, (∑ j, (v i) * K j i * ∑ k, L j k * v k),
{ congr, ext i, rw [finset.mul_sum], congr, ext j, ring, },
rw this,
have : ∑ i j, v i * K j i * ∑ k, L j k * v k =
∑ i j k, v i * K j i * L j k * v k,
{ congr, ext i, congr, ext j, rw [finset.mul_sum], congr, ext k, ring, },
rw this,
have : ∑ i, (∑ j, K i j * v j) * ∑ k, L i k * v k =
∑ i, (∑ j, K i j * v j * ∑ k, L i k * v k),
{ congr, ext i, rw [finset.sum_mul], },
rw this,
have : ∑ i j, K i j * v j * ∑ k, L i k * v k =
∑ i j k, K i j * v j * L i k * v k,
{ congr, ext i, congr, ext j, rw [finset.mul_sum], congr, ext k, ring, },
rw this,
rw [finset.sum_comm],
congr, ext i, congr, ext j, congr, ext k, ring,
end
end matrix |
dcedfb5372be9a7e5a0ea4dad03bb7f51e073ce7 | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /src/Lean/PrettyPrinter/Parenthesizer.lean | b7de8dd704c0282b37e3f4d3f6746f132c39af6e | [
"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 | 29,857 | lean | /-
Copyright (c) 2020 Sebastian Ullrich. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich
-/
/-!
The parenthesizer inserts parentheses into a `Syntax` object where syntactically necessary, usually as an intermediary
step between the delaborator and the formatter. While the delaborator outputs structurally well-formed syntax trees that
can be re-elaborated without post-processing, this tree structure is lost in the formatter and thus needs to be
preserved by proper insertion of parentheses.
# The abstract problem & solution
The Lean 4 grammar is unstructured and extensible with arbitrary new parsers, so in general it is undecidable whether
parentheses are necessary or even allowed at any point in the syntax tree. Parentheses for different categories, e.g.
terms and levels, might not even have the same structure. In this module, we focus on the correct parenthesization of
parsers defined via `Lean.Parser.prattParser`, which includes both aforementioned built-in categories. Custom
parenthesizers can be added for new node kinds, but the data collected in the implementation below might not be
appropriate for other parenthesization strategies.
Usages of a parser defined via `prattParser` in general have the form `p prec`, where `prec` is the minimum precedence
or binding power. Recall that a Pratt parser greedily runs a leading parser with precedence at least `prec` (otherwise
it fails) followed by zero or more trailing parsers with precedence at least `prec`; the precedence of a parser is
encoded in the call to `leadingNode/trailingNode`, respectively. Thus we should parenthesize a syntax node `stx`
supposedly produced by `p prec` if
1. the leading/any trailing parser involved in `stx` has precedence < `prec` (because without parentheses, `p prec`
would not produce all of `stx`), or
2. the trailing parser parsing the input to *the right of* `stx`, if any, has precedence >= `prec` (because without
parentheses, `p prec` would have parsed it as well and made it a part of `stx`). We also check that the two parsers
are from the same syntax category.
Note that in case 2, it is also sufficient to parenthesize a *parent* node as long as the offending parser is still to
the right of that node. For example, imagine the tree structure of `(f $ fun x => x) y` without parentheses. We need to
insert *some* parentheses between `x` and `y` since the lambda body is parsed with precedence 0, while the identifier
parser for `y` has precedence `maxPrec`. But we need to parenthesize the `$` node anyway since the precedence of its
RHS (0) again is smaller than that of `y`. So it's better to only parenthesize the outer node than ending up with
`(f $ (fun x => x)) y`.
# Implementation
We transform the syntax tree and collect the necessary precedence information for that in a single traversal. The
traversal is right-to-left to cover case 2. More specifically, for every Pratt parser call, we store as monadic state
the precedence of the left-most trailing parser and the minimum precedence of any parser (`contPrec`/`minPrec`) in this
call, if any, and the precedence of the nested trailing Pratt parser call (`trailPrec`), if any. If `stP` is the state
resulting from the traversal of a Pratt parser call `p prec`, and `st` is the state of the surrounding call, we
parenthesize if `prec > stP.minPrec` (case 1) or if `stP.trailPrec <= st.contPrec` (case 2).
The traversal can be customized for each `[*Parser]` parser declaration `c` (more specifically, each `SyntaxNodeKind`
`c`) using the `[parenthesizer c]` attribute. Otherwise, a default parenthesizer will be synthesized from the used
parser combinators by recursively replacing them with declarations tagged as `[combinatorParenthesizer]` for the
respective combinator. If a called function does not have a registered combinator parenthesizer and is not reducible,
the synthesizer fails. This happens mostly at the `Parser.mk` decl, which is irreducible, when some parser primitive has
not been handled yet.
The traversal over the `Syntax` object is complicated by the fact that a parser does not produce exactly one syntax
node, but an arbitrary (but constant, for each parser) amount that it pushes on top of the parser stack. This amount can
even be zero for parsers such as `checkWsBefore`. Thus we cannot simply pass and return a `Syntax` object to and from
`visit`. Instead, we use a `Syntax.Traverser` that allows arbitrary movement and modification inside the syntax tree.
Our traversal invariant is that a parser interpreter should stop at the syntax object to the *left* of all syntax
objects its parser produced, except when it is already at the left-most child. This special case is not an issue in
practice since if there is another parser to the left that produced zero nodes in this case, it should always do so, so
there is no danger of the left-most child being processed multiple times.
Ultimately, most parenthesizers are implemented via three primitives that do all the actual syntax traversal:
`maybeParenthesize mkParen prec x` runs `x` and afterwards transforms it with `mkParen` if the above
condition for `p prec` is fulfilled. `visitToken` advances to the preceding sibling and is used on atoms. `visitArgs x`
executes `x` on the last child of the current node and then advances to the preceding sibling (of the original current
node).
-/
import Lean.CoreM
import Lean.KeyedDeclsAttribute
import Lean.Parser.Extension
import Lean.ParserCompiler.Attribute
import Lean.PrettyPrinter.Basic
namespace Lean
namespace PrettyPrinter
namespace Parenthesizer
structure Context where
-- We need to store this `categoryParser` argument to deal with the implicit Pratt parser call in `trailingNode.parenthesizer`.
cat : Name := Name.anonymous
structure State where
stxTrav : Syntax.Traverser
--- precedence and category of the current left-most trailing parser, if any; see module doc for details
contPrec : Option Nat := none
contCat : Name := Name.anonymous
-- current minimum precedence in this Pratt parser call, if any; see module doc for details
minPrec : Option Nat := none
-- precedence and category of the trailing Pratt parser call if any; see module doc for details
trailPrec : Option Nat := none
trailCat : Name := Name.anonymous
-- true iff we have already visited a token on this parser level; used for detecting trailing parsers
visitedToken : Bool := false
end Parenthesizer
abbrev ParenthesizerM := ReaderT Parenthesizer.Context $ StateRefT Parenthesizer.State CoreM
abbrev Parenthesizer := ParenthesizerM Unit
@[inline] def ParenthesizerM.orelse {α} (p₁ p₂ : ParenthesizerM α) : ParenthesizerM α := do
let s ← get
catchInternalId backtrackExceptionId
p₁
(fun _ => do set s; p₂)
instance {α} : OrElse (ParenthesizerM α) := ⟨ParenthesizerM.orelse⟩
unsafe def mkParenthesizerAttribute : IO (KeyedDeclsAttribute Parenthesizer) :=
KeyedDeclsAttribute.init {
builtinName := `builtinParenthesizer,
name := `parenthesizer,
descr := "Register a parenthesizer for a parser.
[parenthesizer k] registers a declaration of type `Lean.PrettyPrinter.Parenthesizer` for the `SyntaxNodeKind` `k`.",
valueTypeName := `Lean.PrettyPrinter.Parenthesizer,
evalKey := fun builtin stx => do
let env ← getEnv
let id ← Attribute.Builtin.getId stx
-- `isValidSyntaxNodeKind` is updated only in the next stage for new `[builtin*Parser]`s, but we try to
-- synthesize a parenthesizer for it immediately, so we just check for a declaration in this case
if (builtin && (env.find? id).isSome) || Parser.isValidSyntaxNodeKind env id then pure id
else throwError "invalid [parenthesizer] argument, unknown syntax kind '{id}'"
} `Lean.PrettyPrinter.parenthesizerAttribute
@[builtinInit mkParenthesizerAttribute] constant parenthesizerAttribute : KeyedDeclsAttribute Parenthesizer
abbrev CategoryParenthesizer := forall (prec : Nat), Parenthesizer
unsafe def mkCategoryParenthesizerAttribute : IO (KeyedDeclsAttribute CategoryParenthesizer) :=
KeyedDeclsAttribute.init {
builtinName := `builtinCategoryParenthesizer,
name := `categoryParenthesizer,
descr := "Register a parenthesizer for a syntax category.
[parenthesizer cat] registers a declaration of type `Lean.PrettyPrinter.CategoryParenthesizer` for the category `cat`,
which is used when parenthesizing calls of `categoryParser cat prec`. Implementations should call `maybeParenthesize`
with the precedence and `cat`. If no category parenthesizer is registered, the category will never be parenthesized,
but still be traversed for parenthesizing nested categories.",
valueTypeName := `Lean.PrettyPrinter.CategoryParenthesizer,
evalKey := fun _ stx => do
let env ← getEnv
let id ← Attribute.Builtin.getId stx
if Parser.isParserCategory env id then pure id
else throwError "invalid [parenthesizer] argument, unknown parser category '{toString id}'"
} `Lean.PrettyPrinter.categoryParenthesizerAttribute
@[builtinInit mkCategoryParenthesizerAttribute] constant categoryParenthesizerAttribute : KeyedDeclsAttribute CategoryParenthesizer
unsafe def mkCombinatorParenthesizerAttribute : IO ParserCompiler.CombinatorAttribute :=
ParserCompiler.registerCombinatorAttribute
`combinatorParenthesizer
"Register a parenthesizer for a parser combinator.
[combinatorParenthesizer c] registers a declaration of type `Lean.PrettyPrinter.Parenthesizer` for the `Parser` declaration `c`.
Note that, unlike with [parenthesizer], this is not a node kind since combinators usually do not introduce their own node kinds.
The tagged declaration may optionally accept parameters corresponding to (a prefix of) those of `c`, where `Parser` is replaced
with `Parenthesizer` in the parameter types."
@[builtinInit mkCombinatorParenthesizerAttribute] constant combinatorParenthesizerAttribute : ParserCompiler.CombinatorAttribute
namespace Parenthesizer
open Lean.Core
open Std.Format
def throwBacktrack {α} : ParenthesizerM α :=
throw $ Exception.internal backtrackExceptionId
instance : Syntax.MonadTraverser ParenthesizerM := ⟨{
get := State.stxTrav <$> get,
set := fun t => modify (fun st => { st with stxTrav := t }),
modifyGet := fun f => modifyGet (fun st => let (a, t) := f st.stxTrav; (a, { st with stxTrav := t }))
}⟩
open Syntax.MonadTraverser
def addPrecCheck (prec : Nat) : ParenthesizerM Unit :=
modify fun st => { st with contPrec := Nat.min (st.contPrec.getD prec) prec, minPrec := Nat.min (st.minPrec.getD prec) prec }
/-- Execute `x` at the right-most child of the current node, if any, then advance to the left. -/
def visitArgs (x : ParenthesizerM Unit) : ParenthesizerM Unit := do
let stx ← getCur
if stx.getArgs.size > 0 then
goDown (stx.getArgs.size - 1) *> x <* goUp
goLeft
-- Macro scopes in the parenthesizer output are ultimately ignored by the pretty printer,
-- so give a trivial implementation.
instance : MonadQuotation ParenthesizerM := {
getCurrMacroScope := pure arbitrary,
getMainModule := pure arbitrary,
withFreshMacroScope := fun x => x,
}
/--
Run `x` and parenthesize the result using `mkParen` if necessary.
If `canJuxtapose` is false, we assume the category does not have a token-less juxtaposition syntax a la function application and deactivate rule 2. -/
def maybeParenthesize (cat : Name) (canJuxtapose : Bool) (mkParen : Syntax → Syntax) (prec : Nat) (x : ParenthesizerM Unit) : ParenthesizerM Unit := do
let stx ← getCur
let idx ← getIdx
let st ← get
-- reset precs for the recursive call
set { stxTrav := st.stxTrav : State }
trace[PrettyPrinter.parenthesize] "parenthesizing (cont := {(st.contPrec, st.contCat)}){indentD (fmt stx)}"
x
let { minPrec := some minPrec, trailPrec := trailPrec, trailCat := trailCat, .. } ← get
| trace[PrettyPrinter.parenthesize] "visited a syntax tree without precedences?!{line ++ fmt stx}"
trace[PrettyPrinter.parenthesize] ("...precedences are {prec} >? {minPrec}" ++ if canJuxtapose then m!", {(trailPrec, trailCat)} <=? {(st.contPrec, st.contCat)}" else "")
-- Should we parenthesize?
if (prec > minPrec || canJuxtapose && match trailPrec, st.contPrec with | some trailPrec, some contPrec => trailCat == st.contCat && trailPrec <= contPrec | _, _ => false) then
-- The recursive `visit` call, by the invariant, has moved to the preceding node. In order to parenthesize
-- the original node, we must first move to the right, except if we already were at the left-most child in the first
-- place.
if idx > 0 then goRight
let mut stx ← getCur
-- Move leading/trailing whitespace of `stx` outside of parentheses
if let SourceInfo.original _ pos trail endPos := stx.getHeadInfo then
stx := stx.setHeadInfo (SourceInfo.original "".toSubstring pos trail endPos)
if let SourceInfo.original lead pos _ endPos := stx.getTailInfo then
stx := stx.setTailInfo (SourceInfo.original lead pos "".toSubstring endPos)
let mut stx' := mkParen stx
if let SourceInfo.original lead pos _ endPos := stx.getHeadInfo then
stx' := stx'.setHeadInfo (SourceInfo.original lead pos "".toSubstring endPos)
if let SourceInfo.original _ pos trail endPos := stx.getTailInfo then
stx' := stx'.setTailInfo (SourceInfo.original "".toSubstring pos trail endPos)
trace[PrettyPrinter.parenthesize] "parenthesized: {stx'.formatStx none}"
setCur stx'
goLeft
-- after parenthesization, there is no more trailing parser
modify (fun st => { st with contPrec := Parser.maxPrec, contCat := cat, trailPrec := none })
let { trailPrec := trailPrec, .. } ← get
-- If we already had a token at this level, keep the trailing parser. Otherwise, use the minimum of
-- `prec` and `trailPrec`.
if st.visitedToken then
modify fun stP => { stP with trailPrec := st.trailPrec, trailCat := st.trailCat }
else
let trailPrec := match trailPrec with
| some trailPrec => Nat.min trailPrec prec
| _ => prec
modify fun stP => { stP with trailPrec := trailPrec, trailCat := cat }
modify fun stP => { stP with minPrec := st.minPrec }
/-- Adjust state and advance. -/
def visitToken : Parenthesizer := do
modify fun st => { st with contPrec := none, contCat := Name.anonymous, visitedToken := true }
goLeft
@[combinatorParenthesizer Lean.Parser.orelse] def orelse.parenthesizer (p1 p2 : Parenthesizer) : Parenthesizer := do
let st ← get
-- HACK: We have no (immediate) information on which side of the orelse could have produced the current node, so try
-- them in turn. Uses the syntax traverser non-linearly!
p1 <|> p2
-- `mkAntiquot` is quite complex, so we'd rather have its parenthesizer synthesized below the actual parser definition.
-- Note that there is a mutual recursion
-- `categoryParser -> mkAntiquot -> termParser -> categoryParser`, so we need to introduce an indirection somewhere
-- anyway.
@[extern 8 "lean_mk_antiquot_parenthesizer"]
constant mkAntiquot.parenthesizer' (name : String) (kind : Option SyntaxNodeKind) (anonymous := true) : Parenthesizer
@[inline] def liftCoreM {α} (x : CoreM α) : ParenthesizerM α :=
liftM x
-- break up big mutual recursion
@[extern "lean_pretty_printer_parenthesizer_interpret_parser_descr"]
constant interpretParserDescr' : ParserDescr → CoreM Parenthesizer
unsafe def parenthesizerForKindUnsafe (k : SyntaxNodeKind) : Parenthesizer := do
if k == `missing then
pure ()
else
let p ← runForNodeKind parenthesizerAttribute k interpretParserDescr'
p
@[implementedBy parenthesizerForKindUnsafe]
constant parenthesizerForKind (k : SyntaxNodeKind) : Parenthesizer
@[combinatorParenthesizer Lean.Parser.withAntiquot]
def withAntiquot.parenthesizer (antiP p : Parenthesizer) : Parenthesizer :=
-- TODO: could be optimized using `isAntiquot` (which would have to be moved), but I'd rather
-- fix the backtracking hack outright.
orelse.parenthesizer antiP p
@[combinatorParenthesizer Lean.Parser.withAntiquotSuffixSplice]
def withAntiquotSuffixSplice.parenthesizer (k : SyntaxNodeKind) (p suffix : Parenthesizer) : Parenthesizer := do
if (← getCur).isAntiquotSuffixSplice then
visitArgs <| suffix *> p
else
p
@[combinatorParenthesizer Lean.Parser.tokenWithAntiquot]
def tokenWithAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do
if (← getCur).isTokenAntiquot then
visitArgs p
else
p
def parenthesizeCategoryCore (cat : Name) (prec : Nat) : Parenthesizer :=
withReader (fun ctx => { ctx with cat := cat }) do
let stx ← getCur
if stx.getKind == `choice then
visitArgs $ stx.getArgs.size.forM fun _ => do
let stx ← getCur
parenthesizerForKind stx.getKind
else
withAntiquot.parenthesizer (mkAntiquot.parenthesizer' cat.toString none) (parenthesizerForKind stx.getKind)
modify fun st => { st with contCat := cat }
@[combinatorParenthesizer Lean.Parser.categoryParser]
def categoryParser.parenthesizer (cat : Name) (prec : Nat) : Parenthesizer := do
let env ← getEnv
match categoryParenthesizerAttribute.getValues env cat with
| p::_ => p prec
-- Fall back to the generic parenthesizer.
-- In this case this node will never be parenthesized since we don't know which parentheses to use.
| _ => parenthesizeCategoryCore cat prec
@[combinatorParenthesizer Lean.Parser.categoryParserOfStack]
def categoryParserOfStack.parenthesizer (offset : Nat) (prec : Nat) : Parenthesizer := do
let st ← get
let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset)
categoryParser.parenthesizer stx.getId prec
@[combinatorParenthesizer Lean.Parser.parserOfStack]
def parserOfStack.parenthesizer (offset : Nat) (prec : Nat := 0) : Parenthesizer := do
let st ← get
let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset)
parenthesizerForKind stx.getKind
@[builtinCategoryParenthesizer term]
def term.parenthesizer : CategoryParenthesizer | prec => do
let stx ← getCur
-- this can happen at `termParser <|> many1 commandParser` in `Term.stxQuot`
if stx.getKind == nullKind then
throwBacktrack
else do
maybeParenthesize `term true (fun stx => Unhygienic.run `(($stx))) prec $
parenthesizeCategoryCore `term prec
@[builtinCategoryParenthesizer tactic]
def tactic.parenthesizer : CategoryParenthesizer | prec => do
maybeParenthesize `tactic false (fun stx => Unhygienic.run `(tactic|($stx))) prec $
parenthesizeCategoryCore `tactic prec
@[builtinCategoryParenthesizer level]
def level.parenthesizer : CategoryParenthesizer | prec => do
maybeParenthesize `level false (fun stx => Unhygienic.run `(level|($stx))) prec $
parenthesizeCategoryCore `level prec
@[combinatorParenthesizer Lean.Parser.error]
def error.parenthesizer (msg : String) : Parenthesizer :=
pure ()
@[combinatorParenthesizer Lean.Parser.errorAtSavedPos]
def errorAtSavedPos.parenthesizer (msg : String) (delta : Bool) : Parenthesizer :=
pure ()
@[combinatorParenthesizer Lean.Parser.atomic]
def atomic.parenthesizer (p : Parenthesizer) : Parenthesizer :=
p
@[combinatorParenthesizer Lean.Parser.lookahead]
def lookahead.parenthesizer (p : Parenthesizer) : Parenthesizer :=
pure ()
@[combinatorParenthesizer Lean.Parser.notFollowedBy]
def notFollowedBy.parenthesizer (p : Parenthesizer) : Parenthesizer :=
pure ()
@[combinatorParenthesizer Lean.Parser.andthen]
def andthen.parenthesizer (p1 p2 : Parenthesizer) : Parenthesizer :=
p2 *> p1
@[combinatorParenthesizer Lean.Parser.node]
def node.parenthesizer (k : SyntaxNodeKind) (p : Parenthesizer) : Parenthesizer := do
let stx ← getCur
if k != stx.getKind then
trace[PrettyPrinter.parenthesize.backtrack] "unexpected node kind '{stx.getKind}', expected '{k}'"
-- HACK; see `orelse.parenthesizer`
throwBacktrack
visitArgs p
@[combinatorParenthesizer Lean.Parser.checkPrec]
def checkPrec.parenthesizer (prec : Nat) : Parenthesizer :=
addPrecCheck prec
@[combinatorParenthesizer Lean.Parser.leadingNode]
def leadingNode.parenthesizer (k : SyntaxNodeKind) (prec : Nat) (p : Parenthesizer) : Parenthesizer := do
node.parenthesizer k p
addPrecCheck prec
-- Limit `cont` precedence to `maxPrec-1`.
-- This is because `maxPrec-1` is the precedence of function application, which is the only way to turn a leading parser
-- into a trailing one.
modify fun st => { st with contPrec := Nat.min (Parser.maxPrec-1) prec }
@[combinatorParenthesizer Lean.Parser.trailingNode]
def trailingNode.parenthesizer (k : SyntaxNodeKind) (prec _ : Nat) (p : Parenthesizer) : Parenthesizer := do
let stx ← getCur
if k != stx.getKind then
trace[PrettyPrinter.parenthesize.backtrack] "unexpected node kind '{stx.getKind}', expected '{k}'"
-- HACK; see `orelse.parenthesizer`
throwBacktrack
visitArgs do
p
addPrecCheck prec
let ctx ← read
modify fun st => { st with contCat := ctx.cat }
-- After visiting the nodes actually produced by the parser passed to `trailingNode`, we are positioned on the
-- left-most child, which is the term injected by `trailingNode` in place of the recursion. Left recursion is not an
-- issue for the parenthesizer, so we can think of this child being produced by `termParser 0`, or whichever Pratt
-- parser is calling us.
categoryParser.parenthesizer ctx.cat 0
@[combinatorParenthesizer Lean.Parser.symbolNoAntiquot] def symbolNoAntiquot.parenthesizer (sym : String) := visitToken
@[combinatorParenthesizer Lean.Parser.unicodeSymbolNoAntiquot] def unicodeSymbolNoAntiquot.parenthesizer (sym asciiSym : String) := visitToken
@[combinatorParenthesizer Lean.Parser.identNoAntiquot] def identNoAntiquot.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.rawIdentNoAntiquot] def rawIdentNoAntiquot.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.identEq] def identEq.parenthesizer (id : Name) := visitToken
@[combinatorParenthesizer Lean.Parser.nonReservedSymbolNoAntiquot] def nonReservedSymbolNoAntiquot.parenthesizer (sym : String) (includeIdent : Bool) := visitToken
@[combinatorParenthesizer Lean.Parser.charLitNoAntiquot] def charLitNoAntiquot.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.strLitNoAntiquot] def strLitNoAntiquot.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.nameLitNoAntiquot] def nameLitNoAntiquot.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.numLitNoAntiquot] def numLitNoAntiquot.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.scientificLitNoAntiquot] def scientificLitNoAntiquot.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.fieldIdx] def fieldIdx.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.manyNoAntiquot]
def manyNoAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do
let stx ← getCur
visitArgs $ stx.getArgs.size.forM fun _ => p
@[combinatorParenthesizer Lean.Parser.many1NoAntiquot]
def many1NoAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do
manyNoAntiquot.parenthesizer p
@[combinatorParenthesizer Lean.Parser.many1Unbox]
def many1Unbox.parenthesizer (p : Parenthesizer) : Parenthesizer := do
let stx ← getCur
if stx.getKind == nullKind then
manyNoAntiquot.parenthesizer p
else
p
@[combinatorParenthesizer Lean.Parser.optionalNoAntiquot]
def optionalNoAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do
visitArgs p
@[combinatorParenthesizer Lean.Parser.sepByNoAntiquot]
def sepByNoAntiquot.parenthesizer (p pSep : Parenthesizer) : Parenthesizer := do
let stx ← getCur
visitArgs $ (List.range stx.getArgs.size).reverse.forM $ fun i => if i % 2 == 0 then p else pSep
@[combinatorParenthesizer Lean.Parser.sepBy1NoAntiquot] def sepBy1NoAntiquot.parenthesizer := sepByNoAntiquot.parenthesizer
@[combinatorParenthesizer Lean.Parser.withPosition] def withPosition.parenthesizer (p : Parenthesizer) : Parenthesizer := do
-- We assume the formatter will indent syntax sufficiently such that parenthesizing a `withPosition` node is never necessary
modify fun st => { st with contPrec := none }
p
@[combinatorParenthesizer Lean.Parser.withoutPosition] def withoutPosition.parenthesizer (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.withForbidden] def withForbidden.parenthesizer (tk : Parser.Token) (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.withoutForbidden] def withoutForbidden.parenthesizer (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.withoutInfo] def withoutInfo.parenthesizer (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.setExpected]
def setExpected.parenthesizer (expected : List String) (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.incQuotDepth] def incQuotDepth.parenthesizer (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.decQuotDepth] def decQuotDepth.parenthesizer (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.suppressInsideQuot] def suppressInsideQuot.parenthesizer (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.evalInsideQuot] def evalInsideQuot.parenthesizer (declName : Name) (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.checkStackTop] def checkStackTop.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkWsBefore] def checkWsBefore.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkNoWsBefore] def checkNoWsBefore.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkLinebreakBefore] def checkLinebreakBefore.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkTailWs] def checkTailWs.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkColGe] def checkColGe.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkColGt] def checkColGt.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkLineEq] def checkLineEq.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.eoi] def eoi.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.notFollowedByCategoryToken] def notFollowedByCategoryToken.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkNoImmediateColon] def checkNoImmediateColon.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkInsideQuot] def checkInsideQuot.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkOutsideQuot] def checkOutsideQuot.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.skip] def skip.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.pushNone] def pushNone.parenthesizer : Parenthesizer := goLeft
@[combinatorParenthesizer Lean.Parser.interpolatedStr]
def interpolatedStr.parenthesizer (p : Parenthesizer) : Parenthesizer := do
visitArgs $ (← getCur).getArgs.reverse.forM fun chunk =>
if chunk.isOfKind interpolatedStrLitKind then
goLeft
else
p
@[combinatorParenthesizer Lean.Parser.dbgTraceState] def dbgTraceState.parenthesizer (label : String) (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer ite, macroInline] def ite {α : Type} (c : Prop) [h : Decidable c] (t e : Parenthesizer) : Parenthesizer :=
if c then t else e
open Parser
abbrev ParenthesizerAliasValue := AliasValue Parenthesizer
builtin_initialize parenthesizerAliasesRef : IO.Ref (NameMap ParenthesizerAliasValue) ← IO.mkRef {}
def registerAlias (aliasName : Name) (v : ParenthesizerAliasValue) : IO Unit := do
Parser.registerAliasCore parenthesizerAliasesRef aliasName v
instance : Coe Parenthesizer ParenthesizerAliasValue := { coe := AliasValue.const }
instance : Coe (Parenthesizer → Parenthesizer) ParenthesizerAliasValue := { coe := AliasValue.unary }
instance : Coe (Parenthesizer → Parenthesizer → Parenthesizer) ParenthesizerAliasValue := { coe := AliasValue.binary }
builtin_initialize
registerAlias "ws" checkWsBefore.parenthesizer
registerAlias "noWs" checkNoWsBefore.parenthesizer
registerAlias "linebreak" checkLinebreakBefore.parenthesizer
registerAlias "colGt" checkColGt.parenthesizer
registerAlias "colGe" checkColGe.parenthesizer
registerAlias "lookahead" lookahead.parenthesizer
registerAlias "atomic" atomic.parenthesizer
registerAlias "notFollowedBy" notFollowedBy.parenthesizer
registerAlias "withPosition" withPosition.parenthesizer
registerAlias "interpolatedStr" interpolatedStr.parenthesizer
registerAlias "orelse" orelse.parenthesizer
registerAlias "andthen" andthen.parenthesizer
end Parenthesizer
open Parenthesizer
/-- Add necessary parentheses in `stx` parsed by `parser`. -/
def parenthesize (parenthesizer : Parenthesizer) (stx : Syntax) : CoreM Syntax := do
trace[PrettyPrinter.parenthesize.input] "{fmt stx}"
catchInternalId backtrackExceptionId
(do
let (_, st) ← (parenthesizer {}).run { stxTrav := Syntax.Traverser.fromSyntax stx }
pure st.stxTrav.cur)
(fun _ => throwError "parenthesize: uncaught backtrack exception")
def parenthesizeTerm := parenthesize $ categoryParser.parenthesizer `term 0
def parenthesizeCommand := parenthesize $ categoryParser.parenthesizer `command 0
builtin_initialize registerTraceClass `PrettyPrinter.parenthesize
end PrettyPrinter
end Lean
|
d53687a765d57c863f1b5542640609344ce1cfb1 | 46125763b4dbf50619e8846a1371029346f4c3db | /src/order/galois_connection.lean | 9398c0977338a27ff5b4fec79a564080a42523b6 | [
"Apache-2.0"
] | permissive | thjread/mathlib | a9d97612cedc2c3101060737233df15abcdb9eb1 | 7cffe2520a5518bba19227a107078d83fa725ddc | refs/heads/master | 1,615,637,696,376 | 1,583,953,063,000 | 1,583,953,063,000 | 246,680,271 | 0 | 0 | Apache-2.0 | 1,583,960,875,000 | 1,583,960,875,000 | null | UTF-8 | Lean | false | false | 13,579 | 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
Galois connections - order theoretic adjoints.
-/
import order.complete_lattice order.bounds order.order_iso
open function set lattice
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {a a₁ a₂ : α} {b b₁ b₂ : β}
/-- A Galois connection is a pair of functions `l` and `u` satisfying
`l a ≤ b ↔ a ≤ u b`. They are closely connected to adjoint functors
in category theory. -/
def galois_connection [preorder α] [preorder β] (l : α → β) (u : β → α) := ∀a b, l a ≤ b ↔ a ≤ u b
/-- Makes a Galois connection from an order-preserving bijection. -/
theorem order_iso.to_galois_connection [preorder α] [preorder β] (oi : @order_iso α β (≤) (≤)) :
galois_connection oi oi.symm :=
λ b g, by rw [order_iso.ord' oi, order_iso.apply_symm_apply]
namespace galois_connection
section
variables [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u)
lemma monotone_intro (hu : monotone u) (hl : monotone l)
(hul : ∀ a, a ≤ u (l a)) (hlu : ∀ a, l (u a) ≤ a) : galois_connection l u :=
assume a b, ⟨assume h, le_trans (hul _) (hu h), assume h, le_trans (hl h) (hlu _)⟩
include gc
lemma l_le {a : α} {b : β} : a ≤ u b → l a ≤ b :=
(gc _ _).mpr
lemma le_u {a : α} {b : β} : l a ≤ b → a ≤ u b :=
(gc _ _).mp
lemma le_u_l (a) : a ≤ u (l a) :=
gc.le_u $ le_refl _
lemma l_u_le (a) : l (u a) ≤ a :=
gc.l_le $ le_refl _
lemma monotone_u : monotone u :=
assume a b H, gc.le_u (le_trans (gc.l_u_le a) H)
lemma monotone_l : monotone l :=
assume a b H, gc.l_le (le_trans H (gc.le_u_l b))
lemma upper_bounds_l_image_subset {s : set α} : upper_bounds (l '' s) ⊆ u ⁻¹' upper_bounds s :=
assume b hb c, assume : c ∈ s, gc.le_u (hb (mem_image_of_mem _ ‹c ∈ s›))
lemma lower_bounds_u_image_subset {s : set β} : lower_bounds (u '' s) ⊆ l ⁻¹' lower_bounds s :=
assume a ha c, assume : c ∈ s, gc.l_le (ha (mem_image_of_mem _ ‹c ∈ s›))
lemma is_lub_l_image {s : set α} {a : α} (h : is_lub s a) : is_lub (l '' s) (l a) :=
⟨gc.monotone_l.mem_upper_bounds_image $ and.elim_left ‹is_lub s a›,
assume b hb, gc.l_le $ and.elim_right ‹is_lub s a› $ gc.upper_bounds_l_image_subset hb⟩
lemma is_glb_u_image {s : set β} {b : β} (h : is_glb s b) : is_glb (u '' s) (u b) :=
⟨gc.monotone_u.mem_lower_bounds_image $ and.elim_left ‹is_glb s b›,
assume a ha, gc.le_u $ and.elim_right ‹is_glb s b› $ gc.lower_bounds_u_image_subset ha⟩
lemma is_glb_l {a : α} : is_glb { b | a ≤ u b } (l a) :=
⟨assume b, gc.l_le, assume b h, h $ gc.le_u_l _⟩
lemma is_lub_u {b : β} : is_lub { a | l a ≤ b } (u b) :=
⟨assume b, gc.le_u, assume b h, h $ gc.l_u_le _⟩
end
section partial_order
variables [partial_order α] [partial_order β] {l : α → β} {u : β → α} (gc : galois_connection l u)
include gc
lemma u_l_u_eq_u : u ∘ l ∘ u = u :=
funext (assume x, le_antisymm (gc.monotone_u (gc.l_u_le _)) (gc.le_u_l _))
lemma l_u_l_eq_l : l ∘ u ∘ l = l :=
funext (assume x, le_antisymm (gc.l_u_le _) (gc.monotone_l (gc.le_u_l _)))
end partial_order
section order_top
variables [order_top α] [order_top β] {l : α → β} {u : β → α} (gc : galois_connection l u)
include gc
lemma u_top : u ⊤ = ⊤ :=
(gc.is_glb_u_image is_glb_empty).unique $ by simp only [is_glb_empty, image_empty]
end order_top
section order_bot
variables [order_bot α] [order_bot β] {l : α → β} {u : β → α} (gc : galois_connection l u)
include gc
lemma l_bot : l ⊥ = ⊥ :=
(gc.is_lub_l_image is_lub_empty).unique $ by simp only [is_lub_empty, image_empty]
end order_bot
section semilattice_sup
variables [semilattice_sup α] [semilattice_sup β] {l : α → β} {u : β → α} (gc : galois_connection l u)
include gc
lemma l_sup : l (a₁ ⊔ a₂) = l a₁ ⊔ l a₂ :=
(gc.is_lub_l_image is_lub_pair).unique $ by simp only [image_pair, is_lub_pair]
end semilattice_sup
section semilattice_inf
variables [semilattice_inf α] [semilattice_inf β] {l : α → β} {u : β → α} (gc : galois_connection l u)
include gc
lemma u_inf : u (b₁ ⊓ b₂) = u b₁ ⊓ u b₂ :=
(gc.is_glb_u_image is_glb_pair).unique $ by simp only [image_pair, is_glb_pair]
end semilattice_inf
section complete_lattice
variables [complete_lattice α] [complete_lattice β] {l : α → β} {u : β → α} (gc : galois_connection l u)
include gc
lemma l_supr {f : ι → α} : l (supr f) = (⨆i, l (f i)) :=
eq.symm $ is_lub.supr_eq $ show is_lub (range (l ∘ f)) (l (supr f)),
by rw [range_comp, ← Sup_range]; exact gc.is_lub_l_image (is_lub_Sup _)
lemma u_infi {f : ι → β} : u (infi f) = (⨅i, u (f i)) :=
eq.symm $ is_glb.infi_eq $ show is_glb (range (u ∘ f)) (u (infi f)),
by rw [range_comp, ← Inf_range]; exact gc.is_glb_u_image (is_glb_Inf _)
end complete_lattice
section complete_lattice
variables [complete_lattice α] [complete_lattice β] {l : α → β} {u : β → α} (gc : galois_connection l u)
include gc
lemma l_Sup {s : set α} : l (Sup s) = (⨆a∈s, l a) :=
by simp [Sup_eq_supr, gc.l_supr]
lemma u_Inf {s : set β} : u (Inf s) = (⨅a∈s, u a) :=
by simp [Inf_eq_infi, gc.u_infi]
end complete_lattice
/- Constructing Galois connections -/
section constructions
protected lemma id [pα : preorder α] : @galois_connection α α pα pα id id :=
assume a b, iff.intro (λx, x) (λx, x)
protected lemma compose [preorder α] [preorder β] [preorder γ]
(l1 : α → β) (u1 : β → α) (l2 : β → γ) (u2 : γ → β)
(gc1 : galois_connection l1 u1) (gc2 : galois_connection l2 u2) :
galois_connection (l2 ∘ l1) (u1 ∘ u2) :=
by intros a b; rw [gc2, gc1]
protected lemma dual [pα : preorder α] [pβ : preorder β]
{l : α → β} {u : β → α} (gc : galois_connection l u) :
@galois_connection (order_dual β) (order_dual α) _ _ u l :=
assume a b, (gc _ _).symm
protected lemma dfun {ι : Type u} {α : ι → Type v} {β : ι → Type w}
[∀i, preorder (α i)] [∀i, preorder (β i)]
(l : Πi, α i → β i) (u : Πi, β i → α i) (gc : ∀i, galois_connection (l i) (u i)) :
@galois_connection (Π i, α i) (Π i, β i) _ _ (λa i, l i (a i)) (λb i, u i (b i)) :=
assume a b, forall_congr $ assume i, gc i (a i) (b i)
end constructions
end galois_connection
namespace nat
lemma galois_connection_mul_div {k : ℕ} (h : k > 0) : galois_connection (λn, n * k) (λn, n / k) :=
assume x y, (le_div_iff_mul_le x y h).symm
end nat
/-- A Galois insertion is a Galois connection where `l ∘ u = id`. It also contains a constructive
choice function, to give better definitional equalities when lifting order structures. -/
structure galois_insertion {α β : Type*} [preorder α] [preorder β] (l : α → β) (u : β → α) :=
(choice : Πx:α, u (l x) ≤ x → β)
(gc : galois_connection l u)
(le_l_u : ∀x, x ≤ l (u x))
(choice_eq : ∀a h, choice a h = l a)
/-- A constructor for a Galois insertion with the trivial `choice` function. -/
def galois_insertion.monotone_intro {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α}
(hu : monotone u) (hl : monotone l) (hul : ∀ a, a ≤ u (l a)) (hlu : ∀ b, l (u b) = b) :
galois_insertion l u :=
{ choice := λ x _, l x,
gc := galois_connection.monotone_intro hu hl hul (λ b, le_of_eq (hlu b)),
le_l_u := λ b, le_of_eq $ (hlu b).symm,
choice_eq := λ _ _, rfl }
/-- Makes a Galois insertion from an order-preserving bijection. -/
protected def order_iso.to_galois_insertion [preorder α] [preorder β] (oi : @order_iso α β (≤) (≤)) :
@galois_insertion α β _ _ (oi) (oi.symm) :=
{ choice := λ b h, oi b,
gc := oi.to_galois_connection,
le_l_u := λ g, le_of_eq (oi.right_inv g).symm,
choice_eq := λ b h, rfl }
/-- Lift the bottom along a Galois connection -/
def galois_connection.lift_order_bot {α β : Type*} [order_bot α] [partial_order β]
{l : α → β} {u : β → α} (gc : galois_connection l u) :
order_bot β :=
{ bot := l ⊥,
bot_le := assume b, gc.l_le $ bot_le,
.. ‹partial_order β› }
namespace galois_insertion
open lattice
variables {l : α → β} {u : β → α}
lemma l_u_eq [preorder α] [partial_order β] (gi : galois_insertion l u) (b : β) :
l (u b) = b :=
le_antisymm (gi.gc.l_u_le _) (gi.le_l_u _)
lemma l_surjective [preorder α] [partial_order β] (gi : galois_insertion l u) :
surjective l :=
assume b, ⟨u b, gi.l_u_eq b⟩
lemma u_injective [preorder α] [partial_order β] (gi : galois_insertion l u) :
injective u :=
assume a b h,
calc a = l (u a) : (gi.l_u_eq a).symm
... = l (u b) : congr_arg l h
... = b : gi.l_u_eq b
lemma l_sup_u [semilattice_sup α] [semilattice_sup β] (gi : galois_insertion l u) (a b : β) :
l (u a ⊔ u b) = a ⊔ b :=
calc l (u a ⊔ u b) = l (u a) ⊔ l (u b) : gi.gc.l_sup
... = a ⊔ b : by simp only [gi.l_u_eq]
lemma l_supr_u [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u)
{ι : Sort x} (f : ι → β) :
l (⨆ i, u (f i)) = ⨆ i, (f i) :=
calc l (⨆ (i : ι), u (f i)) = ⨆ (i : ι), l (u (f i)) : gi.gc.l_supr
... = ⨆ (i : ι), f i : congr_arg _ $ funext $ λ i, gi.l_u_eq (f i)
lemma l_supr_of_ul [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u)
{ι : Sort x} (f : ι → α) (hf : ∀ i, u (l (f i)) = f i) :
l (⨆ i, (f i)) = ⨆ i, l (f i) :=
calc l (⨆ (i : ι), (f i)) = l ⨆ (i : ι), (u (l (f i))) : by simp [hf]
... = ⨆ (i : ι), l (f i) : gi.l_supr_u _
lemma l_inf_u [semilattice_inf α] [semilattice_inf β] (gi : galois_insertion l u) (a b : β) :
l (u a ⊓ u b) = a ⊓ b :=
calc l (u a ⊓ u b) = l (u (a ⊓ b)) : congr_arg l gi.gc.u_inf.symm
... = a ⊓ b : by simp only [gi.l_u_eq]
lemma l_infi_u [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u)
{ι : Sort x} (f : ι → β) :
l (⨅ i, u (f i)) = ⨅ i, (f i) :=
calc l (⨅ (i : ι), u (f i)) = l (u (⨅ (i : ι), (f i))) : congr_arg l gi.gc.u_infi.symm
... = ⨅ (i : ι), f i : gi.l_u_eq _
lemma l_infi_of_ul [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u)
{ι : Sort x} (f : ι → α) (hf : ∀ i, u (l (f i)) = f i) :
l (⨅ i, (f i)) = ⨅ i, l (f i) :=
calc l (⨅ i, (f i)) = l ⨅ (i : ι), (u (l (f i))) : by simp [hf]
... = ⨅ i, l (f i) : gi.l_infi_u _
section lift
variables [partial_order β]
/-- Lift the suprema along a Galois insertion -/
def lift_semilattice_sup [semilattice_sup α] (gi : galois_insertion l u) : semilattice_sup β :=
{ sup := λa b, l (u a ⊔ u b),
le_sup_left := assume a b, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_sup_left,
le_sup_right := assume a b, le_trans (gi.le_l_u b) $ gi.gc.monotone_l $ le_sup_right,
sup_le := assume a b c hac hbc, gi.gc.l_le $ sup_le (gi.gc.monotone_u hac) (gi.gc.monotone_u hbc),
.. ‹partial_order β› }
/-- Lift the infima along a Galois insertion -/
def lift_semilattice_inf [semilattice_inf α] (gi : galois_insertion l u) : semilattice_inf β :=
{ inf := λa b, gi.choice (u a ⊓ u b) $
(le_inf (gi.gc.monotone_u $ gi.gc.l_le $ inf_le_left) (gi.gc.monotone_u $ gi.gc.l_le $ inf_le_right)),
inf_le_left := by simp only [gi.choice_eq]; exact assume a b, gi.gc.l_le inf_le_left,
inf_le_right := by simp only [gi.choice_eq]; exact assume a b, gi.gc.l_le inf_le_right,
le_inf := by simp only [gi.choice_eq]; exact assume a b c hac hbc,
le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_inf (gi.gc.monotone_u hac) (gi.gc.monotone_u hbc),
.. ‹partial_order β› }
/-- Lift the suprema and infima along a Galois insertion -/
def lift_lattice [lattice α] (gi : galois_insertion l u) : lattice β :=
{ .. gi.lift_semilattice_sup, .. gi.lift_semilattice_inf }
/-- Lift the top along a Galois insertion -/
def lift_order_top [order_top α] (gi : galois_insertion l u) : order_top β :=
{ top := gi.choice ⊤ $ le_top,
le_top := by simp only [gi.choice_eq]; exact assume b, le_trans (gi.le_l_u b) (gi.gc.monotone_l le_top),
.. ‹partial_order β› }
/-- Lift the top, bottom, suprema, and infima along a Galois insertion -/
def lift_bounded_lattice [bounded_lattice α] (gi : galois_insertion l u) : bounded_lattice β :=
{ .. gi.lift_lattice, .. gi.lift_order_top, .. gi.gc.lift_order_bot }
/-- Lift all suprema and infima along a Galois insertion -/
def lift_complete_lattice [complete_lattice α] (gi : galois_insertion l u) : complete_lattice β :=
{ Sup := λs, l (⨆ b∈s, u b),
Sup_le := assume s a hs, gi.gc.l_le $ supr_le $ assume b, supr_le $ assume hb, gi.gc.monotone_u $ hs _ hb,
le_Sup := assume s a ha, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_supr_of_le a $ le_supr_of_le ha $ le_refl _,
Inf := λs, gi.choice (⨅ b∈s, u b) $ le_infi $ assume b, le_infi $ assume hb,
gi.gc.monotone_u $ gi.gc.l_le $ infi_le_of_le b $ infi_le_of_le hb $ le_refl _,
Inf_le := by simp only [gi.choice_eq]; exact
assume s a ha, gi.gc.l_le $ infi_le_of_le a $ infi_le_of_le ha $ le_refl _,
le_Inf := by simp only [gi.choice_eq]; exact
assume s a hs, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_infi $ assume b,
show u a ≤ ⨅ (H : b ∈ s), u b, from le_infi $ assume hb, gi.gc.monotone_u $ hs _ hb,
.. gi.lift_bounded_lattice }
end lift
end galois_insertion
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.