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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
f728201b9d339971acce95301eb499b6fa0bdc3d | 9028d228ac200bbefe3a711342514dd4e4458bff | /src/topology/basic.lean | 7f8bb1528eb43aa1be7c195ab6c9acf726ac102a | [
"Apache-2.0"
] | permissive | mcncm/mathlib | 8d25099344d9d2bee62822cb9ed43aa3e09fa05e | fde3d78cadeec5ef827b16ae55664ef115e66f57 | refs/heads/master | 1,672,743,316,277 | 1,602,618,514,000 | 1,602,618,514,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 49,134 | 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 `is_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`;
## 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 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 simp only [is_closed, compl_sInter, sUnion_image]; exact assume h, is_open_Union $ assume t, is_open_Union $ assume ht, h t ht
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
@[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₁⟩
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 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⟩
lemma set.nonempty.closure {s : set α} (h : s.nonempty) :
set.nonempty (closure s) :=
let ⟨x, hx⟩ := h in ⟨x, subset_closure hx⟩
@[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 :=
by rw [dense, eq_univ_iff_forall]
lemma dense.closure_eq {s : set α} (h : dense s) : closure s = univ :=
dense_iff_closure_eq.mp h
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 _
@[mono]
lemma dense_of_subset_dense {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]
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`. -/
def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 s)
localized "notation `𝓝` := nhds" in topological_space
lemma nhds_def (a : α) : 𝓝 a = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 s) := rfl
/-- 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) :=
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⟩⟩
/-- 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
attribute [irreducible] nhds
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 cluster_pt_iff {x : α} {F : filter α} :
cluster_pt x F ↔ ∀ {U V : set α}, U ∈ 𝓝 x → 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 $ 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
/-- 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 | 𝓝 a ≤ 𝓟 s} :=
set.ext $ λ x, by simp only [mem_interior, le_principal_iff, mem_nhds_sets_iff]; refl
lemma mem_interior_iff_mem_nhds {s : set α} {a : α} :
a ∈ interior s ↔ s ∈ 𝓝 a :=
by simp only [interior_eq_nhds, le_principal_iff]; refl
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 : filter α), is_ultrafilter l → 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
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 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 → ∃ y ∈ t, y ∈ s i :=
mem_closure_iff_nhds.trans
⟨λ H i hi, let ⟨x, hx⟩ := (H _ $ h.mem_of_mem hi) in ⟨x, hx.2, hx.1⟩,
λ H t' ht', let ⟨i, hi, hit⟩ := h.mem_iff.1 ht', ⟨x, xt, hx⟩ := H i hi in
⟨x, hit hx, xt⟩⟩
/-- `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.val ∧ u.val ≤ 𝓝 x :=
begin
rw closure_eq_cluster_pts, change cluster_pt x (𝓟 s) ↔ _, symmetry,
convert exists_ultrafilter_iff _, ext u,
rw [←le_principal_iff, inf_comm, le_inf_iff]
end
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) :=
assume a ⟨hs, ht⟩,
have s ∈ 𝓝 a, from mem_nhds_sets h hs,
have 𝓝 a ⊓ 𝓟 s = 𝓝 a, by rwa [inf_eq_left, le_principal_iff],
have cluster_pt a (𝓟 (s ∩ t)),
from calc 𝓝 a ⊓ 𝓟 (s ∩ t) = 𝓝 a ⊓ (𝓟 s ⊓ 𝓟 t) : by rw inf_principal
... = 𝓝 a ⊓ 𝓟 t : by rw [←inf_assoc, this]
... ≠ ⊥ : by rw [closure_eq_cluster_pts] at ht; assumption,
by rwa [closure_eq_cluster_pts]
lemma dense_inter_of_open_left {s t : set α} (hs : closure s = univ) (ht : closure t = univ)
(hso : is_open s) :
closure (s ∩ t) = univ :=
eq_univ_of_subset (closure_minimal (closure_inter_open hso) is_closed_closure) $
by simp only [*, inter_univ]
lemma dense_inter_of_open_right {s t : set α} (hs : closure s = univ) (ht : closure t = univ)
(hto : is_open t) :
closure (s ∩ t) = univ :=
inter_comm t s ▸ dense_inter_of_open_left ht hs hto
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
-- Note: `ultrafilter` is inside the `filter` namespace.
/--
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 filter.ultrafilter.Lim : ultrafilter α → α := λ F, Lim' F.1
/-- 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_of_finite {f : β → set α} (h : finite (univ : set β)) : locally_finite f :=
assume x, ⟨univ, univ_mem_sets, h.subset $ subset_univ _⟩
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 is_closed_Union_of_locally_finite {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)ᶜ)) :
begin
rw [le_principal_iff],
apply @filter.inter_mem_sets _ (𝓝 a) _ _ h_sets,
apply @filter.Inter_mem_sets _ (𝓝 a) _ _ _ h_fin,
exact assume i h, this i
end
... ≤ 𝓟 (⋃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
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. -/
def continuous (f : α → β) := ∀s, is_open s → is_open (f ⁻¹' s)
lemma is_open.preimage {f : α → β} (hf : continuous f) {s : set β} (h : is_open s) :
is_open (f ⁻¹' s) :=
hf 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.preimage_mem_nhds {f : α → β} {x : α} {t : set β} (h : continuous_at f x)
(ht : t ∈ 𝓝 (f x)) : f ⁻¹' t ∈ 𝓝 x :=
h ht
lemma preimage_interior_subset_interior_preimage {f : α → β} {s : set β}
(hf : continuous f) : f⁻¹' (interior s) ⊆ interior (f⁻¹' s) :=
interior_maximal (preimage_mono interior_subset) (hf _ is_open_interior)
lemma continuous_id : continuous (id : α → α) :=
assume s h, h
lemma continuous.comp {g : β → γ} {f : α → β} (hg : continuous g) (hf : continuous f) :
continuous (g ∘ f) :=
assume s h, hf _ (hg s h)
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, hf _ ht⟩, subset.refl _⟩
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)),
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_const {b : β} : continuous (λa:α, b) :=
continuous_iff_continuous_at.mpr $ assume a, tendsto_const_nhds
lemma continuous_at_const {x : α} {b : β} : continuous_at (λ a:α, b) x :=
continuous_const.continuous_at
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, hf sᶜ hs,
assume hf 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, is_ultrafilter g → g ≤ 𝓝 x → g.map f ≤ 𝓝 (f x) :=
tendsto_iff_ultrafilter f (𝓝 x) (𝓝 (f x))
lemma continuous_iff_ultrafilter {f : α → β} :
continuous f ↔ ∀ x g, is_ultrafilter g → g ≤ 𝓝 x → g.map f ≤ 𝓝 (f x) :=
by simp only [continuous_iff_continuous_at, continuous_at_iff_ultrafilter]
/-- A piecewise defined function `if p then f else g` is continuous, if both `f` and `g`
are continuous, and they coincide on the frontier (boundary) of the set `{a | p a}`. -/
lemma continuous_if {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)}
(hp : ∀a∈frontier {a | p a}, f a = g a) (hf : continuous f) (hg : continuous g) :
continuous (λa, @ite (p a) (h a) β (f a) (g a)) :=
continuous_iff_is_closed.mpr $
assume s hs,
have (λa, ite (p a) (f a) (g a)) ⁻¹' s =
(closure {a | p a} ∩ f ⁻¹' s) ∪ (closure {a | ¬ p a} ∩ g ⁻¹' s),
from set.ext $ assume a,
classical.by_cases
(assume : a ∈ frontier {a | p a},
have hac : a ∈ closure {a | p a}, from this.left,
have hai : a ∈ closure {a | ¬ p a},
from have a ∈ (interior {a | p a})ᶜ, from this.right, by rwa [←closure_compl] at this,
by by_cases p a; simp [h, hp a this, hac, hai, iff_def] {contextual := tt})
(assume hf : a ∈ (frontier {a | p a})ᶜ,
classical.by_cases
(assume : p a,
have hc : a ∈ closure {a | p a}, from subset_closure this,
have hnc : a ∉ closure {a | ¬ p a},
by show a ∉ closure {a | p a}ᶜ; rw [closure_compl]; simpa [frontier, hc] using hf,
by simp [this, hc, hnc])
(assume : ¬ p a,
have hc : a ∈ closure {a | ¬ p a}, from subset_closure this,
have hnc : a ∉ closure {a | p a},
begin
have hc : a ∈ closure {a | p a}ᶜ, from hc,
simp [closure_compl] at hc,
simpa [frontier, hc] using hf
end,
by simp [this, hc, hnc])),
by rw [this]; exact is_closed_union
(is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hf s hs)
(is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hg s hs)
/- 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
lemma image_closure_subset_closure_image {f : α → β} {s : set α} (h : continuous f) :
f '' closure s ⊆ closure (f '' s) :=
have ∀ (a : α), cluster_pt a (𝓟 s) → cluster_pt (f a) (𝓟 (f '' s)),
from assume a ha,
have h₁ : ¬ map f (𝓝 a ⊓ 𝓟 s) = ⊥,
by rwa[map_eq_bot_iff],
have h₂ : map f (𝓝 a ⊓ 𝓟 s) ≤ 𝓝 (f a) ⊓ 𝓟 (f '' s),
from le_inf
(le_trans (map_mono inf_le_left) $ by rw [continuous_iff_continuous_at] at h; exact h a)
(le_trans (map_mono inf_le_right) $ by simp [subset_preimage_image] ),
ne_bot_of_le_ne_bot h₁ h₂,
by simp [image_subset_iff, closure_eq_cluster_pts]; assumption
lemma 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 :=
subset.trans (image_closure_subset_closure_image hf) (closure_mono $ image_subset_iff.2 ht) $
(mem_image_of_mem f 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}
lemma dense_range_iff_closure_range : dense_range f ↔ closure (range f) = univ :=
eq_univ_iff_forall.symm
lemma dense_range.closure_range (h : dense_range f) : closure (range f) = univ :=
eq_univ_iff_forall.mpr h
lemma dense_range.comp (hg : dense_range g) (hf : dense_range f) (cg : continuous g) :
dense_range (g ∘ f) :=
begin
have : g '' (closure $ range f) ⊆ closure (g '' range f),
from image_closure_subset_closure_image cg,
have : closure (g '' closure (range f)) ⊆ closure (g '' range f),
by simpa [closure_closure] using (closure_mono this),
intro c,
rw range_comp,
apply this,
rw [hf.closure_range, image_univ],
exact hg c
end
/-- If `f : ι → β` has dense range and `β` contains some element, then `ι` must too. -/
def dense_range.inhabited (df : dense_range f) (b : β) : inhabited κ :=
⟨classical.choice $
by simpa only [univ_inter, range_nonempty_iff_nonempty] using
mem_closure_iff.1 (df b) _ is_open_univ trivial⟩
lemma dense_range.nonempty (hf : dense_range f) : nonempty κ ↔ nonempty β :=
⟨nonempty.map f, λ ⟨b⟩, @nonempty_of_inhabited _ (hf.inhabited b)⟩
lemma continuous.dense_image_of_dense_range {f : α → β}
(hf : continuous f) (hf' : dense_range f) {s : set α} (hs : dense s) :
closure (f '' s) = univ :=
begin
have : f '' (closure s) ⊆ closure (f '' s) := image_closure_subset_closure_image hf,
have := closure_mono this,
rw [hs.closure_eq, image_univ, hf'.closure_eq, closure_closure] at this,
exact eq_univ_of_univ_subset this
end
end dense_range
end continuous
|
15469c5d276b1b864e32aed8e4711cd599ccd3fa | c777c32c8e484e195053731103c5e52af26a25d1 | /src/analysis/complex/conformal.lean | 895a1c2cc86c34e308e2cf39790fe15fa15f98b8 | [
"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 | 5,414 | lean | /-
Copyright (c) 2021 Yourong Zang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yourong Zang
-/
import analysis.complex.isometry
import analysis.normed_space.conformal_linear_map
import analysis.normed_space.finite_dimension
/-!
# Conformal maps between complex vector spaces
We prove the sufficient and necessary conditions for a real-linear map between complex vector spaces
to be conformal.
## Main results
* `is_conformal_map_complex_linear`: a nonzero complex linear map into an arbitrary complex
normed space is conformal.
* `is_conformal_map_complex_linear_conj`: the composition of a nonzero complex linear map with
`conj` is complex linear.
* `is_conformal_map_iff_is_complex_or_conj_linear`: a real linear map between the complex
plane is conformal iff it's complex
linear or the composition of
some complex linear map and `conj`.
## Warning
Antiholomorphic functions such as the complex conjugate are considered as conformal functions in
this file.
-/
noncomputable theory
open complex continuous_linear_map
open_locale complex_conjugate
lemma is_conformal_map_conj : is_conformal_map (conj_lie : ℂ →L[ℝ] ℂ) :=
conj_lie.to_linear_isometry.is_conformal_map
section conformal_into_complex_normed
variables {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] [normed_space ℂ E]
{z : ℂ} {g : ℂ →L[ℝ] E} {f : ℂ → E}
lemma is_conformal_map_complex_linear {map : ℂ →L[ℂ] E} (nonzero : map ≠ 0) :
is_conformal_map (map.restrict_scalars ℝ) :=
begin
have minor₁ : ‖map 1‖ ≠ 0,
{ simpa only [ext_ring_iff, ne.def, norm_eq_zero] using nonzero},
refine ⟨‖map 1‖, minor₁, ⟨‖map 1‖⁻¹ • map, _⟩, _⟩,
{ intros x,
simp only [linear_map.smul_apply],
have : x = x • 1 := by rw [smul_eq_mul, mul_one],
nth_rewrite 0 [this],
rw [_root_.coe_coe map, linear_map.coe_coe_is_scalar_tower],
simp only [map.coe_coe, map.map_smul, norm_smul, norm_inv, norm_norm],
field_simp only [one_mul] },
{ ext1,
simp only [minor₁, linear_map.smul_apply, _root_.coe_coe, linear_map.coe_coe_is_scalar_tower,
continuous_linear_map.coe_coe, coe_restrict_scalars', coe_smul',
linear_isometry.coe_to_continuous_linear_map, linear_isometry.coe_mk, pi.smul_apply,
smul_inv_smul₀, ne.def, not_false_iff] },
end
lemma is_conformal_map_complex_linear_conj
{map : ℂ →L[ℂ] E} (nonzero : map ≠ 0) :
is_conformal_map ((map.restrict_scalars ℝ).comp (conj_cle : ℂ →L[ℝ] ℂ)) :=
(is_conformal_map_complex_linear nonzero).comp is_conformal_map_conj
end conformal_into_complex_normed
section conformal_into_complex_plane
open continuous_linear_map
variables {f : ℂ → ℂ} {z : ℂ} {g : ℂ →L[ℝ] ℂ}
lemma is_conformal_map.is_complex_or_conj_linear (h : is_conformal_map g) :
(∃ (map : ℂ →L[ℂ] ℂ), map.restrict_scalars ℝ = g) ∨
(∃ (map : ℂ →L[ℂ] ℂ), map.restrict_scalars ℝ = g ∘L ↑conj_cle) :=
begin
rcases h with ⟨c, hc, li, rfl⟩,
obtain ⟨li, rfl⟩ : ∃ li' : ℂ ≃ₗᵢ[ℝ] ℂ, li'.to_linear_isometry = li,
from ⟨li.to_linear_isometry_equiv rfl, by { ext1, refl }⟩,
rcases linear_isometry_complex li with ⟨a, rfl|rfl⟩,
-- let rot := c • (a : ℂ) • continuous_linear_map.id ℂ ℂ,
{ refine or.inl ⟨c • (a : ℂ) • continuous_linear_map.id ℂ ℂ, _⟩,
ext1,
simp only [coe_restrict_scalars', smul_apply, linear_isometry.coe_to_continuous_linear_map,
linear_isometry_equiv.coe_to_linear_isometry, rotation_apply, id_apply, smul_eq_mul] },
{ refine or.inr ⟨c • (a : ℂ) • continuous_linear_map.id ℂ ℂ, _⟩,
ext1,
simp only [coe_restrict_scalars', smul_apply, linear_isometry.coe_to_continuous_linear_map,
linear_isometry_equiv.coe_to_linear_isometry, rotation_apply, id_apply, smul_eq_mul,
comp_apply, linear_isometry_equiv.trans_apply, continuous_linear_equiv.coe_coe,
conj_cle_apply, conj_lie_apply, conj_conj] },
end
/-- A real continuous linear map on the complex plane is conformal if and only if the map or its
conjugate is complex linear, and the map is nonvanishing. -/
lemma is_conformal_map_iff_is_complex_or_conj_linear:
is_conformal_map g ↔
((∃ (map : ℂ →L[ℂ] ℂ), map.restrict_scalars ℝ = g) ∨
(∃ (map : ℂ →L[ℂ] ℂ), map.restrict_scalars ℝ = g ∘L ↑conj_cle)) ∧ g ≠ 0 :=
begin
split,
{ exact λ h, ⟨h.is_complex_or_conj_linear, h.ne_zero⟩, },
{ rintros ⟨⟨map, rfl⟩ | ⟨map, hmap⟩, h₂⟩,
{ refine is_conformal_map_complex_linear _,
contrapose! h₂ with w,
simp only [w, restrict_scalars_zero]},
{ have minor₁ : g = (map.restrict_scalars ℝ) ∘L ↑conj_cle,
{ ext1,
simp only [hmap, coe_comp', continuous_linear_equiv.coe_coe, function.comp_app,
conj_cle_apply, star_ring_end_self_apply]},
rw minor₁ at ⊢ h₂,
refine is_conformal_map_complex_linear_conj _,
contrapose! h₂ with w,
simp only [w, restrict_scalars_zero, zero_comp]} }
end
end conformal_into_complex_plane
|
8ceaa0fd5f99ef7f2763a6d254b4a6d86450f201 | 9028d228ac200bbefe3a711342514dd4e4458bff | /src/data/int/parity.lean | 7594bf594b2b7e34df735adfb82afbedb2285719 | [
"Apache-2.0"
] | permissive | mcncm/mathlib | 8d25099344d9d2bee62822cb9ed43aa3e09fa05e | fde3d78cadeec5ef827b16ae55664ef115e66f57 | refs/heads/master | 1,672,743,316,277 | 1,602,618,514,000 | 1,602,618,514,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,609 | lean | /-
Copyright (c) 2019 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
The `even` and `odd` predicates on the integers.
-/
import data.int.modeq
import data.nat.parity
namespace int
@[simp] theorem mod_two_ne_one {n : ℤ} : ¬ n % 2 = 1 ↔ n % 2 = 0 :=
by cases mod_two_eq_zero_or_one n with h h; simp [h]
local attribute [simp] -- euclidean_domain.mod_eq_zero uses (2 ∣ n) as normal form
theorem mod_two_ne_zero {n : ℤ} : ¬ n % 2 = 0 ↔ n % 2 = 1 :=
by cases mod_two_eq_zero_or_one n with h h; simp [h]
@[simp] theorem even_coe_nat (n : nat) : even (n : ℤ) ↔ even n :=
have ∀ m, 2 * to_nat m = to_nat (2 * m),
from λ m, by cases m; refl,
⟨λ ⟨m, hm⟩, ⟨to_nat m, by rw [this, ←to_nat_coe_nat n, hm]⟩,
λ ⟨m, hm⟩, ⟨m, by simp [hm]⟩⟩
theorem even_iff {n : ℤ} : even n ↔ n % 2 = 0 :=
⟨λ ⟨m, hm⟩, by simp [hm], λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by simp [h])⟩⟩
theorem odd_iff {n : ℤ} : odd n ↔ n % 2 = 1 :=
⟨λ ⟨m, hm⟩, by { rw [hm, add_mod], norm_num },
λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by { rw h, abel })⟩⟩
lemma not_even_iff {n : ℤ} : ¬ even n ↔ n % 2 = 1 :=
by rw [even_iff, mod_two_ne_zero]
@[simp] lemma odd_iff_not_even {n : ℤ} : odd n ↔ ¬ even n :=
by rw [not_even_iff, odd_iff]
@[simp] theorem two_dvd_ne_zero {n : ℤ} : ¬ 2 ∣ n ↔ n % 2 = 1 :=
not_even_iff
instance : decidable_pred (even : ℤ → Prop) :=
λ n, decidable_of_decidable_of_iff (by apply_instance) even_iff.symm
instance decidable_pred_odd : decidable_pred (odd : ℤ → Prop) :=
λ n, decidable_of_decidable_of_iff (by apply_instance) odd_iff_not_even.symm
@[simp] theorem even_zero : even (0 : ℤ) := ⟨0, dec_trivial⟩
@[simp] theorem not_even_one : ¬ even (1 : ℤ) :=
by rw even_iff; apply one_ne_zero
@[simp] theorem even_bit0 (n : ℤ) : even (bit0 n) :=
⟨n, by rw [bit0, two_mul]⟩
@[parity_simps] theorem even_add {m n : ℤ} : even (m + n) ↔ (even m ↔ even n) :=
begin
cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂;
simp [even_iff, h₁, h₂, -euclidean_domain.mod_eq_zero],
{ exact @modeq.modeq_add _ _ 0 _ 0 h₁ h₂ },
{ exact @modeq.modeq_add _ _ 0 _ 1 h₁ h₂ },
{ exact @modeq.modeq_add _ _ 1 _ 0 h₁ h₂ },
exact @modeq.modeq_add _ _ 1 _ 1 h₁ h₂
end
@[parity_simps] theorem even_neg {n : ℤ} : even (-n) ↔ even n := by simp [even_iff]
@[simp] theorem not_even_bit1 (n : ℤ) : ¬ even (bit1 n) :=
by simp [bit1] with parity_simps
@[parity_simps] theorem even_sub {m n : ℤ} : even (m - n) ↔ (even m ↔ even n) :=
by simp [sub_eq_add_neg] with parity_simps
@[parity_simps] theorem even_mul {m n : ℤ} : even (m * n) ↔ even m ∨ even n :=
begin
cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂;
simp [even_iff, h₁, h₂, -euclidean_domain.mod_eq_zero],
{ exact @modeq.modeq_mul _ _ 0 _ 0 h₁ h₂ },
{ exact @modeq.modeq_mul _ _ 0 _ 1 h₁ h₂ },
{ exact @modeq.modeq_mul _ _ 1 _ 0 h₁ h₂ },
exact @modeq.modeq_mul _ _ 1 _ 1 h₁ h₂
end
@[parity_simps] theorem even_pow {m : ℤ} {n : ℕ} : even (m^n) ↔ even m ∧ n ≠ 0 :=
by { induction n with n ih; simp [*, even_mul, pow_succ], tauto }
-- Here are examples of how `parity_simps` can be used with `int`.
example (m n : ℤ) (h : even m) : ¬ even (n + 3) ↔ even (m^2 + m + n) :=
by simp [*, (dec_trivial : ¬ 2 = 0)] with parity_simps
example : ¬ even (25394535 : ℤ) :=
by simp
end int
|
9e68f8d4e2dc5b33425212f28dc06eb3e2c71870 | 618003631150032a5676f229d13a079ac875ff77 | /src/topology/sheaves/presheaf_of_functions.lean | 2fb6f2f12317c0486c2912c5e14704ddbc2f03d4 | [
"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,608 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import topology.sheaves.presheaf
import topology.category.TopCommRing
import topology.algebra.continuous_functions
universes v u
open category_theory
open topological_space
open opposite
namespace Top
variables (X : Top.{v})
/-- The presheaf of continuous functions on `X` with values in fixed target topological space `T`. -/
def presheaf_to_Top (T : Top.{v}) : X.presheaf (Type v) :=
(opens.to_Top X).op ⋙ (yoneda.obj T)
/-- The (bundled) commutative ring of continuous functions from a topological space
to a topological commutative ring, with pointwise multiplication. -/
-- TODO upgrade the result to TopCommRing?
def continuous_functions (X : Top.{v}ᵒᵖ) (R : TopCommRing.{v}) : CommRing.{v} :=
{ α := unop X ⟶ (forget₂ TopCommRing Top).obj R,
str := _root_.continuous_comm_ring }
namespace continuous_functions
@[simp] lemma one (X : Top.{v}ᵒᵖ) (R : TopCommRing.{v}) (x) :
(monoid.one : continuous_functions X R).val x = 1 := rfl
@[simp] lemma zero (X : Top.{v}ᵒᵖ) (R : TopCommRing.{v}) (x) :
(comm_ring.zero : continuous_functions X R).val x = 0 := rfl
@[simp] lemma add (X : Top.{v}ᵒᵖ) (R : TopCommRing.{v}) (f g : continuous_functions X R) (x) :
(comm_ring.add f g).val x = f.1 x + g.1 x := rfl
@[simp] lemma mul (X : Top.{v}ᵒᵖ) (R : TopCommRing.{v}) (f g : continuous_functions X R) (x) :
(ring.mul f g).val x = f.1 x * g.1 x := rfl
/-- Pulling back functions into a topological ring along a continuous map is a ring homomorphism. -/
def pullback {X Y : Topᵒᵖ} (f : X ⟶ Y) (R : TopCommRing) :
continuous_functions X R ⟶ continuous_functions Y R :=
{ to_fun := λ g, f.unop ≫ g,
map_one' := rfl,
map_zero' := rfl,
map_add' := by tidy,
map_mul' := by tidy }
local attribute [ext] subtype.eq
/-- A homomorphism of topological rings can be postcomposed with functions from a source space `X`;
this is a ring homomorphism (with respect to the pointwise ring operations on functions). -/
def map (X : Topᵒᵖ) {R S : TopCommRing} (φ : R ⟶ S) :
continuous_functions X R ⟶ continuous_functions X S :=
{ to_fun := λ g, g ≫ ((forget₂ TopCommRing Top).map φ),
map_one' := by ext; exact φ.1.map_one,
map_zero' := by ext; exact φ.1.map_zero,
map_add' := by intros; ext; apply φ.1.map_add,
map_mul' := by intros; ext; apply φ.1.map_mul }
end continuous_functions
/-- An upgraded version of the Yoneda embedding, observing that the continuous maps
from `X : Top` to `R : TopCommRing` form a commutative ring, functorial in both `X` and `R`. -/
def CommRing_yoneda : TopCommRing.{u} ⥤ (Top.{u}ᵒᵖ ⥤ CommRing.{u}) :=
{ obj := λ R,
{ obj := λ X, continuous_functions X R,
map := λ X Y f, continuous_functions.pullback f R },
map := λ R S φ,
{ app := λ X, continuous_functions.map X φ } }
/-- The presheaf (of commutative rings), consisting of functions on an open set `U ⊆ X` with
values in some topological commutative ring `T`. -/
def presheaf_to_TopCommRing (T : TopCommRing.{v}) :
X.presheaf CommRing.{v} :=
(opens.to_Top X).op ⋙ (CommRing_yoneda.obj T)
/-- The presheaf (of commutative rings) of real valued functions. -/
noncomputable def presheaf_ℝ (Y : Top) : Y.presheaf CommRing :=
presheaf_to_TopCommRing Y (TopCommRing.of ℝ)
/-- The presheaf (of commutative rings) of complex valued functions. -/
noncomputable def presheaf_ℂ (Y : Top) : Y.presheaf CommRing :=
presheaf_to_TopCommRing Y (TopCommRing.of ℂ)
end Top
|
8fcb7b4a5051fe5aec6a522392f33b5b5031adc3 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/ring_theory/non_zero_divisors.lean | 539d03416b96c56eb2bc4c3306fc565cafb874d6 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 6,785 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Devon Tuma
-/
import group_theory.submonoid.operations
import group_theory.submonoid.membership
/-!
# Non-zero divisors
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we define the submonoid `non_zero_divisors` of a `monoid_with_zero`.
## Notations
This file declares the notation `R⁰` for the submonoid of non-zero-divisors of `R`,
in the locale `non_zero_divisors`. Use the statement `open_locale non_zero_divisors`
to access this notation in your own code.
-/
section non_zero_divisors
/-- The submonoid of non-zero-divisors of a `monoid_with_zero` `R`. -/
def non_zero_divisors (R : Type*) [monoid_with_zero R] : submonoid R :=
{ carrier := {x | ∀ z, z * x = 0 → z = 0},
one_mem' := λ z hz, by rwa mul_one at hz,
mul_mem' := λ x₁ x₂ hx₁ hx₂ z hz,
have z * x₁ * x₂ = 0, by rwa mul_assoc,
hx₁ z $ hx₂ (z * x₁) this }
localized "notation (name := non_zero_divisors)
R`⁰`:9000 := non_zero_divisors R" in non_zero_divisors
variables {M M' M₁ R R' F : Type*} [monoid_with_zero M] [monoid_with_zero M']
[comm_monoid_with_zero M₁] [ring R] [comm_ring R']
lemma mem_non_zero_divisors_iff {r : M} : r ∈ M⁰ ↔ ∀ x, x * r = 0 → x = 0 := iff.rfl
lemma mul_right_mem_non_zero_divisors_eq_zero_iff {x r : M} (hr : r ∈ M⁰) :
x * r = 0 ↔ x = 0 :=
⟨hr _, by simp {contextual := tt}⟩
@[simp] lemma mul_right_coe_non_zero_divisors_eq_zero_iff {x : M} {c : M⁰} :
x * c = 0 ↔ x = 0 :=
mul_right_mem_non_zero_divisors_eq_zero_iff c.prop
lemma mul_left_mem_non_zero_divisors_eq_zero_iff {r x : M₁} (hr : r ∈ M₁⁰) :
r * x = 0 ↔ x = 0 :=
by rw [mul_comm, mul_right_mem_non_zero_divisors_eq_zero_iff hr]
@[simp] lemma mul_left_coe_non_zero_divisors_eq_zero_iff {c : M₁⁰} {x : M₁} :
(c : M₁) * x = 0 ↔ x = 0 :=
mul_left_mem_non_zero_divisors_eq_zero_iff c.prop
lemma mul_cancel_right_mem_non_zero_divisor {x y r : R} (hr : r ∈ R⁰) :
x * r = y * r ↔ x = y :=
begin
refine ⟨λ h, _, congr_arg _⟩,
rw [←sub_eq_zero, ←mul_right_mem_non_zero_divisors_eq_zero_iff hr, sub_mul, h, sub_self]
end
lemma mul_cancel_right_coe_non_zero_divisor {x y : R} {c : R⁰} :
x * c = y * c ↔ x = y :=
mul_cancel_right_mem_non_zero_divisor c.prop
@[simp] lemma mul_cancel_left_mem_non_zero_divisor {x y r : R'} (hr : r ∈ R'⁰) :
r * x = r * y ↔ x = y :=
by simp_rw [mul_comm r, mul_cancel_right_mem_non_zero_divisor hr]
lemma mul_cancel_left_coe_non_zero_divisor {x y : R'} {c : R'⁰} :
(c : R') * x = c * y ↔ x = y :=
mul_cancel_left_mem_non_zero_divisor c.prop
lemma non_zero_divisors.ne_zero [nontrivial M] {x} (hx : x ∈ M⁰) : x ≠ 0 :=
λ h, one_ne_zero (hx _ $ (one_mul _).trans h)
lemma non_zero_divisors.coe_ne_zero [nontrivial M] (x : M⁰) : (x : M) ≠ 0 :=
non_zero_divisors.ne_zero x.2
lemma mul_mem_non_zero_divisors {a b : M₁} :
a * b ∈ M₁⁰ ↔ a ∈ M₁⁰ ∧ b ∈ M₁⁰ :=
begin
split,
{ intro h,
split; intros x h'; apply h,
{ rw [←mul_assoc, h', zero_mul] },
{ rw [mul_comm a b, ←mul_assoc, h', zero_mul] } },
{ rintros ⟨ha, hb⟩ x hx,
apply ha,
apply hb,
rw [mul_assoc, hx] },
end
lemma is_unit_of_mem_non_zero_divisors {G₀ : Type*} [group_with_zero G₀]
{x : G₀} (hx : x ∈ non_zero_divisors G₀) : is_unit x :=
⟨⟨x, x⁻¹, mul_inv_cancel (non_zero_divisors.ne_zero hx),
inv_mul_cancel (non_zero_divisors.ne_zero hx)⟩, rfl⟩
lemma eq_zero_of_ne_zero_of_mul_right_eq_zero [no_zero_divisors M]
{x y : M} (hnx : x ≠ 0) (hxy : y * x = 0) : y = 0 :=
or.resolve_right (eq_zero_or_eq_zero_of_mul_eq_zero hxy) hnx
lemma eq_zero_of_ne_zero_of_mul_left_eq_zero [no_zero_divisors M]
{x y : M} (hnx : x ≠ 0) (hxy : x * y = 0) : y = 0 :=
or.resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero hxy) hnx
lemma mem_non_zero_divisors_of_ne_zero [no_zero_divisors M] {x : M} (hx : x ≠ 0) : x ∈ M⁰ :=
λ _, eq_zero_of_ne_zero_of_mul_right_eq_zero hx
lemma mem_non_zero_divisors_iff_ne_zero [no_zero_divisors M] [nontrivial M] {x : M} :
x ∈ M⁰ ↔ x ≠ 0 :=
⟨non_zero_divisors.ne_zero, mem_non_zero_divisors_of_ne_zero⟩
lemma map_ne_zero_of_mem_non_zero_divisors [nontrivial M] [zero_hom_class F M M']
(g : F) (hg : function.injective (g : M → M')) {x : M} (h : x ∈ M⁰) : g x ≠ 0 :=
λ h0, one_ne_zero (h 1 ((one_mul x).symm ▸ (hg (trans h0 (map_zero g).symm))))
lemma map_mem_non_zero_divisors [nontrivial M] [no_zero_divisors M'] [zero_hom_class F M M']
(g : F) (hg : function.injective g) {x : M} (h : x ∈ M⁰) : g x ∈ M'⁰ :=
λ z hz, eq_zero_of_ne_zero_of_mul_right_eq_zero
(map_ne_zero_of_mem_non_zero_divisors g hg h) hz
lemma le_non_zero_divisors_of_no_zero_divisors [no_zero_divisors M] {S : submonoid M}
(hS : (0 : M) ∉ S) : S ≤ M⁰ :=
λ x hx y hy, or.rec_on (eq_zero_or_eq_zero_of_mul_eq_zero hy)
(λ h, h) (λ h, absurd (h ▸ hx : (0 : M) ∈ S) hS)
lemma powers_le_non_zero_divisors_of_no_zero_divisors [no_zero_divisors M]
{a : M} (ha : a ≠ 0) : submonoid.powers a ≤ M⁰ :=
le_non_zero_divisors_of_no_zero_divisors (λ h, absurd (h.rec_on (λ _ hn, pow_eq_zero hn)) ha)
lemma map_le_non_zero_divisors_of_injective [no_zero_divisors M']
[monoid_with_zero_hom_class F M M'] (f : F) (hf : function.injective f) {S : submonoid M}
(hS : S ≤ M⁰) : S.map f ≤ M'⁰ :=
begin
casesI subsingleton_or_nontrivial M,
{ simp [subsingleton.elim S ⊥] },
{ exact le_non_zero_divisors_of_no_zero_divisors (λ h, let ⟨x, hx, hx0⟩ := h in
zero_ne_one (hS (hf (trans hx0 ((map_zero f).symm)) ▸ hx : 0 ∈ S) 1 (mul_zero 1)).symm) }
end
lemma non_zero_divisors_le_comap_non_zero_divisors_of_injective [no_zero_divisors M']
[monoid_with_zero_hom_class F M M'] (f : F) (hf : function.injective f) : M⁰ ≤ M'⁰.comap f :=
submonoid.le_comap_of_map_le _ (map_le_non_zero_divisors_of_injective _ hf le_rfl)
lemma prod_zero_iff_exists_zero [no_zero_divisors M₁] [nontrivial M₁]
{s : multiset M₁} : s.prod = 0 ↔ ∃ (r : M₁) (hr : r ∈ s), r = 0 :=
begin
split, swap,
{ rintros ⟨r, hrs, rfl⟩,
exact multiset.prod_eq_zero hrs, },
refine multiset.induction _ (λ a s ih, _) s,
{ intro habs,
simpa using habs, },
{ rw multiset.prod_cons,
intro hprod,
replace hprod := eq_zero_or_eq_zero_of_mul_eq_zero hprod,
cases hprod with ha,
{ exact ⟨a, multiset.mem_cons_self a s, ha⟩ },
{ apply (ih hprod).imp _,
rintros b ⟨hb₁, hb₂⟩,
exact ⟨multiset.mem_cons_of_mem hb₁, hb₂⟩, }, },
end
end non_zero_divisors
|
760ba4af76d2953cc8603dfdcc967af7ec478b3a | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/algebra/star/pi.lean | b0089803f188802d5e7d3e699862177fc6ee9ec9 | [
"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 | 1,406 | 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.star.algebra
/-!
# `star` on pi types
We put a `has_star` structure on pi types that operates elementwise, such that it describes the
complex conjugation of vectors.
-/
universes u v w
variable {I : Type u} -- The indexing type
variable {f : I → Type v} -- The family of types already equipped with instances
namespace pi
instance [Π i, has_star (f i)] : has_star (Π i, f i) :=
{ star := λ x i, star (x i) }
@[simp] lemma star_apply [Π i, has_star (f i)] (x : Π i, f i) (i : I) : star x i = star (x i) := rfl
instance [Π i, has_involutive_star (f i)] : has_involutive_star (Π i, f i) :=
{ star_involutive := λ _, funext $ λ _, star_star _ }
instance [Π i, monoid (f i)] [Π i, star_monoid (f i)] : star_monoid (Π i, f i) :=
{ star_mul := λ _ _, funext $ λ _, star_mul _ _ }
instance [Π i, semiring (f i)] [Π i, star_ring (f i)] : star_ring (Π i, f i) :=
{ star_add := λ _ _, funext $ λ _, star_add _ _,
..(by apply_instance : star_monoid (Π i, f i)) }
instance {R : Type w}
[comm_semiring R] [Π i, semiring (f i)] [Π i, algebra R (f i)]
[star_ring R] [Π i, star_ring (f i)] [Π i, star_algebra R (f i)] :
star_algebra R (Π i, f i) :=
{ star_smul := λ r x, funext $ λ _, star_smul _ _ }
end pi
|
3cd583bf4a42592dc649a69186228b9507479c2e | 957a80ea22c5abb4f4670b250d55534d9db99108 | /library/init/algebra/ring.lean | 687eb709a7aec782dcf3229b94fc3c764d3ad7b7 | [
"Apache-2.0"
] | permissive | GaloisInc/lean | aa1e64d604051e602fcf4610061314b9a37ab8cd | f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0 | refs/heads/master | 1,592,202,909,807 | 1,504,624,387,000 | 1,504,624,387,000 | 75,319,626 | 2 | 1 | Apache-2.0 | 1,539,290,164,000 | 1,480,616,104,000 | C++ | UTF-8 | Lean | false | false | 12,594 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura
-/
prelude
import init.algebra.group
/- Make sure instances defined in this file have lower priority than the ones
defined for concrete structures -/
set_option default_priority 100
set_option old_structure_cmd true
universe u
class distrib (α : Type u) extends has_mul α, has_add α :=
(left_distrib : ∀ a b c : α, a * (b + c) = (a * b) + (a * c))
(right_distrib : ∀ a b c : α, (a + b) * c = (a * c) + (b * c))
variable {α : Type u}
lemma left_distrib [distrib α] (a b c : α) : a * (b + c) = a * b + a * c :=
distrib.left_distrib a b c
def mul_add := @left_distrib
lemma right_distrib [distrib α] (a b c : α) : (a + b) * c = a * c + b * c :=
distrib.right_distrib a b c
def add_mul := @right_distrib
class mul_zero_class (α : Type u) extends has_mul α, has_zero α :=
(zero_mul : ∀ a : α, 0 * a = 0)
(mul_zero : ∀ a : α, a * 0 = 0)
@[simp] lemma zero_mul [mul_zero_class α] (a : α) : 0 * a = 0 :=
mul_zero_class.zero_mul a
@[simp] lemma mul_zero [mul_zero_class α] (a : α) : a * 0 = 0 :=
mul_zero_class.mul_zero a
class zero_ne_one_class (α : Type u) extends has_zero α, has_one α :=
(zero_ne_one : 0 ≠ (1:α))
@[simp]
lemma zero_ne_one [s: zero_ne_one_class α] : 0 ≠ (1:α) :=
@zero_ne_one_class.zero_ne_one α s
@[simp]
lemma one_ne_zero [s: zero_ne_one_class α] : (1:α) ≠ 0 :=
assume h, @zero_ne_one_class.zero_ne_one α s h.symm
/- semiring -/
class semiring (α : Type u) extends add_comm_monoid α, monoid α, distrib α, mul_zero_class α
section semiring
variables [semiring α]
lemma one_add_one_eq_two : 1 + 1 = (2 : α) :=
by unfold bit0
theorem two_mul (n : α) : 2 * n = n + n :=
eq.trans (right_distrib 1 1 n) (by simp)
lemma ne_zero_of_mul_ne_zero_right {a b : α} (h : a * b ≠ 0) : a ≠ 0 :=
assume : a = 0,
have a * b = 0, by rw [this, zero_mul],
h this
lemma ne_zero_of_mul_ne_zero_left {a b : α} (h : a * b ≠ 0) : b ≠ 0 :=
assume : b = 0,
have a * b = 0, by rw [this, mul_zero],
h this
lemma distrib_three_right (a b c d : α) : (a + b + c) * d = a * d + b * d + c * d :=
by simp [right_distrib]
end semiring
class comm_semiring (α : Type u) extends semiring α, comm_monoid α
section comm_semiring
variables [comm_semiring α] (a b c : α)
instance comm_semiring_has_dvd : has_dvd α :=
has_dvd.mk (λ a b, ∃ c, b = a * c)
-- TODO: this used to not have c explicit, but that seems to be important
-- for use with tactics, similar to exist.intro
theorem dvd.intro {a b : α} (c : α) (h : a * c = b) : a ∣ b :=
exists.intro c h^.symm
def dvd_of_mul_right_eq := @dvd.intro
theorem dvd.intro_left {a b : α} (c : α) (h : c * a = b) : a ∣ b :=
dvd.intro _ (begin rewrite mul_comm at h, apply h end)
def dvd_of_mul_left_eq := @dvd.intro_left
theorem exists_eq_mul_right_of_dvd {a b : α} (h : a ∣ b) : ∃ c, b = a * c := h
theorem dvd.elim {P : Prop} {a b : α} (H₁ : a ∣ b) (H₂ : ∀ c, b = a * c → P) : P :=
exists.elim H₁ H₂
theorem exists_eq_mul_left_of_dvd {a b : α} (h : a ∣ b) : ∃ c, b = c * a :=
dvd.elim h (assume c, assume H1 : b = a * c, exists.intro c (eq.trans H1 (mul_comm a c)))
theorem dvd.elim_left {P : Prop} {a b : α} (h₁ : a ∣ b) (h₂ : ∀ c, b = c * a → P) : P :=
exists.elim (exists_eq_mul_left_of_dvd h₁) (assume c, assume h₃ : b = c * a, h₂ c h₃)
@[simp] theorem dvd_refl : a ∣ a :=
dvd.intro 1 (by simp)
theorem dvd_trans {a b c : α} (h₁ : a ∣ b) (h₂ : b ∣ c) : a ∣ c :=
match h₁, h₂ with
| ⟨d, (h₃ : b = a * d)⟩, ⟨e, (h₄ : c = b * e)⟩ :=
⟨d * e, show c = a * (d * e), by simp [h₃, h₄]⟩
end
def dvd.trans := @dvd_trans
theorem eq_zero_of_zero_dvd {a : α} (h : 0 ∣ a) : a = 0 :=
dvd.elim h (assume c, assume H' : a = 0 * c, eq.trans H' (zero_mul c))
@[simp] theorem dvd_zero : a ∣ 0 := dvd.intro 0 (by simp)
@[simp] theorem one_dvd : 1 ∣ a := dvd.intro a (by simp)
@[simp] theorem dvd_mul_right : a ∣ a * b := dvd.intro b rfl
@[simp] theorem dvd_mul_left : a ∣ b * a := dvd.intro b (by simp)
theorem dvd_mul_of_dvd_left {a b : α} (h : a ∣ b) (c : α) : a ∣ b * c :=
dvd.elim h (λ d h', begin rw [h', mul_assoc], apply dvd_mul_right end)
theorem dvd_mul_of_dvd_right {a b : α} (h : a ∣ b) (c : α) : a ∣ c * b :=
begin rw mul_comm, exact dvd_mul_of_dvd_left h _ end
theorem mul_dvd_mul : ∀ {a b c d : α}, a ∣ b → c ∣ d → a * c ∣ b * d
| a ._ c ._ ⟨e, rfl⟩ ⟨f, rfl⟩ := ⟨e * f, by simp⟩
theorem mul_dvd_mul_left (a : α) {b c : α} (h : b ∣ c) : a * b ∣ a * c :=
mul_dvd_mul (dvd_refl a) h
theorem mul_dvd_mul_right {a b : α} (h : a ∣ b) (c : α) : a * c ∣ b * c :=
mul_dvd_mul h (dvd_refl c)
theorem dvd_add {a b c : α} (h₁ : a ∣ b) (h₂ : a ∣ c) : a ∣ b + c :=
dvd.elim h₁ (λ d hd, dvd.elim h₂ (λ e he, dvd.intro (d + e) (by simp [left_distrib, hd, he])))
theorem dvd_of_mul_right_dvd {a b c : α} (h : a * b ∣ c) : a ∣ c :=
dvd.elim h (begin intros d h₁, rw [h₁, mul_assoc], apply dvd_mul_right end)
theorem dvd_of_mul_left_dvd {a b c : α} (h : a * b ∣ c) : b ∣ c :=
dvd.elim h (λ d ceq, dvd.intro (a * d) (by simp [ceq]))
end comm_semiring
/- ring -/
class ring (α : Type u) extends add_comm_group α, monoid α, distrib α
lemma ring.mul_zero [ring α] (a : α) : a * 0 = 0 :=
have a * 0 + 0 = a * 0 + a * 0, from calc
a * 0 + 0 = a * (0 + 0) : by simp
... = a * 0 + a * 0 : by rw left_distrib,
show a * 0 = 0, from (add_left_cancel this).symm
lemma ring.zero_mul [ring α] (a : α) : 0 * a = 0 :=
have 0 * a + 0 = 0 * a + 0 * a, from calc
0 * a + 0 = (0 + 0) * a : by simp
... = 0 * a + 0 * a : by rewrite right_distrib,
show 0 * a = 0, from (add_left_cancel this).symm
instance ring.to_semiring [s : ring α] : semiring α :=
{ s with
mul_zero := ring.mul_zero,
zero_mul := ring.zero_mul }
lemma neg_mul_eq_neg_mul [s : ring α] (a b : α) : -(a * b) = -a * b :=
neg_eq_of_add_eq_zero
begin rw [← right_distrib, add_right_neg, zero_mul] end
lemma neg_mul_eq_mul_neg [s : ring α] (a b : α) : -(a * b) = a * -b :=
neg_eq_of_add_eq_zero
begin rw [← left_distrib, add_right_neg, mul_zero] end
@[simp] lemma neg_mul_eq_neg_mul_symm [s : ring α] (a b : α) : - a * b = - (a * b) :=
eq.symm (neg_mul_eq_neg_mul a b)
@[simp] lemma mul_neg_eq_neg_mul_symm [s : ring α] (a b : α) : a * - b = - (a * b) :=
eq.symm (neg_mul_eq_mul_neg a b)
lemma neg_mul_neg [s : ring α] (a b : α) : -a * -b = a * b :=
by simp
lemma neg_mul_comm [s : ring α] (a b : α) : -a * b = a * -b :=
by simp
theorem neg_eq_neg_one_mul [s : ring α] (a : α) : -a = -1 * a :=
by simp
lemma mul_sub_left_distrib [s : ring α] (a b c : α) : a * (b - c) = a * b - a * c :=
calc
a * (b - c) = a * b + a * -c : left_distrib a b (-c)
... = a * b - a * c : by simp
def mul_sub := @mul_sub_left_distrib
lemma mul_sub_right_distrib [s : ring α] (a b c : α) : (a - b) * c = a * c - b * c :=
calc
(a - b) * c = a * c + -b * c : right_distrib a (-b) c
... = a * c - b * c : by simp
def sub_mul := @mul_sub_right_distrib
class comm_ring (α : Type u) extends ring α, comm_semigroup α
instance comm_ring.to_comm_semiring [s : comm_ring α] : comm_semiring α :=
{ s with
mul_zero := mul_zero,
zero_mul := zero_mul }
section comm_ring
variable [comm_ring α]
lemma mul_self_sub_mul_self_eq (a b : α) : a * a - b * b = (a + b) * (a - b) :=
by simp [right_distrib, left_distrib]
lemma mul_self_sub_one_eq (a : α) : a * a - 1 = (a + 1) * (a - 1) :=
by simp [right_distrib, left_distrib]
lemma add_mul_self_eq (a b : α) : (a + b) * (a + b) = a*a + 2*a*b + b*b :=
calc (a + b)*(a + b) = a*a + (1+1)*a*b + b*b : by simp [right_distrib, left_distrib]
... = a*a + 2*a*b + b*b : by rw one_add_one_eq_two
theorem dvd_neg_of_dvd {a b : α} (h : a ∣ b) : (a ∣ -b) :=
dvd.elim h
(assume c, assume : b = a * c,
dvd.intro (-c) (by simp [this]))
theorem dvd_of_dvd_neg {a b : α} (h : a ∣ -b) : (a ∣ b) :=
let t := dvd_neg_of_dvd h in by rwa neg_neg at t
theorem dvd_neg_iff_dvd (a b : α) : (a ∣ -b) ↔ (a ∣ b) :=
⟨dvd_of_dvd_neg, dvd_neg_of_dvd⟩
theorem neg_dvd_of_dvd {a b : α} (h : a ∣ b) : -a ∣ b :=
dvd.elim h
(assume c, assume : b = a * c,
dvd.intro (-c) (by simp [this]))
theorem dvd_of_neg_dvd {a b : α} (h : -a ∣ b) : a ∣ b :=
let t := neg_dvd_of_dvd h in by rwa neg_neg at t
theorem neg_dvd_iff_dvd (a b : α) : (-a ∣ b) ↔ (a ∣ b) :=
⟨dvd_of_neg_dvd, neg_dvd_of_dvd⟩
theorem dvd_sub {a b c : α} (h₁ : a ∣ b) (h₂ : a ∣ c) : a ∣ b - c :=
dvd_add h₁ (dvd_neg_of_dvd h₂)
theorem dvd_add_iff_left {a b c : α} (h : a ∣ c) : a ∣ b ↔ a ∣ b + c :=
⟨λh₂, dvd_add h₂ h, λH, by have t := dvd_sub H h; rwa add_sub_cancel at t⟩
theorem dvd_add_iff_right {a b c : α} (h : a ∣ b) : a ∣ c ↔ a ∣ b + c :=
by rw add_comm; exact dvd_add_iff_left h
end comm_ring
class no_zero_divisors (α : Type u) extends has_mul α, has_zero α :=
(eq_zero_or_eq_zero_of_mul_eq_zero : ∀ a b : α, a * b = 0 → a = 0 ∨ b = 0)
lemma eq_zero_or_eq_zero_of_mul_eq_zero [no_zero_divisors α] {a b : α} (h : a * b = 0) : a = 0 ∨ b = 0 :=
no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero a b h
lemma eq_zero_of_mul_self_eq_zero [no_zero_divisors α] {a : α} (h : a * a = 0) : a = 0 :=
or.elim (eq_zero_or_eq_zero_of_mul_eq_zero h) (assume h', h') (assume h', h')
class integral_domain (α : Type u) extends comm_ring α, no_zero_divisors α, zero_ne_one_class α
section integral_domain
variable [integral_domain α]
lemma mul_eq_zero_iff_eq_zero_or_eq_zero {a b : α} : a * b = 0 ↔ a = 0 ∨ b = 0 :=
⟨eq_zero_or_eq_zero_of_mul_eq_zero, λo,
or.elim o (λh, by rw h; apply zero_mul) (λh, by rw h; apply mul_zero)⟩
lemma mul_ne_zero {a b : α} (h₁ : a ≠ 0) (h₂ : b ≠ 0) : a * b ≠ 0 :=
λ h, or.elim (eq_zero_or_eq_zero_of_mul_eq_zero h) (assume h₃, h₁ h₃) (assume h₄, h₂ h₄)
lemma eq_of_mul_eq_mul_right {a b c : α} (ha : a ≠ 0) (h : b * a = c * a) : b = c :=
have b * a - c * a = 0, from sub_eq_zero_of_eq h,
have (b - c) * a = 0, by rw [mul_sub_right_distrib, this],
have b - c = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right ha,
eq_of_sub_eq_zero this
lemma eq_of_mul_eq_mul_left {a b c : α} (ha : a ≠ 0) (h : a * b = a * c) : b = c :=
have a * b - a * c = 0, from sub_eq_zero_of_eq h,
have a * (b - c) = 0, by rw [mul_sub_left_distrib, this],
have b - c = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_left ha,
eq_of_sub_eq_zero this
lemma eq_zero_of_mul_eq_self_right {a b : α} (h₁ : b ≠ 1) (h₂ : a * b = a) : a = 0 :=
have hb : b - 1 ≠ 0, from
assume : b - 1 = 0,
have b = 0 + 1, from eq_add_of_sub_eq this,
have b = 1, by rwa zero_add at this,
h₁ this,
have a * b - a = 0, by simp [h₂],
have a * (b - 1) = 0, by rwa [mul_sub_left_distrib, mul_one],
show a = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right hb
lemma eq_zero_of_mul_eq_self_left {a b : α} (h₁ : b ≠ 1) (h₂ : b * a = a) : a = 0 :=
eq_zero_of_mul_eq_self_right h₁ (by rwa mul_comm at h₂)
lemma mul_self_eq_mul_self_iff (a b : α) : a * a = b * b ↔ a = b ∨ a = -b :=
iff.intro
(assume : a * a = b * b,
have (a - b) * (a + b) = 0,
by rewrite [mul_comm, ← mul_self_sub_mul_self_eq, this, sub_self],
have a - b = 0 ∨ a + b = 0, from eq_zero_or_eq_zero_of_mul_eq_zero this,
or.elim this
(assume : a - b = 0, or.inl (eq_of_sub_eq_zero this))
(assume : a + b = 0, or.inr (eq_neg_of_add_eq_zero this)))
(assume : a = b ∨ a = -b, or.elim this
(assume : a = b, by rewrite this)
(assume : a = -b, by rewrite [this, neg_mul_neg]))
lemma mul_self_eq_one_iff (a : α) : a * a = 1 ↔ a = 1 ∨ a = -1 :=
have a * a = 1 * 1 ↔ a = 1 ∨ a = -1, from mul_self_eq_mul_self_iff a 1,
by rwa mul_one at this
end integral_domain
/- TODO(Leo): remove the following annotations as soon as we have support for arithmetic
in the SMT tactic framework -/
attribute [ematch] add_zero zero_add mul_one one_mul mul_zero zero_mul
|
26af6a21cea963fdee7bbe5f59d98dd3e46d0f66 | a4673261e60b025e2c8c825dfa4ab9108246c32e | /src/Lean/Elab/Binders.lean | 9fa5351ace78e411c4e1d733570bcfaa58f47225 | [
"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 | 25,602 | 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.Elab.Term
import Lean.Elab.Quotation
namespace Lean.Elab.Term
open Meta
/--
Given syntax of the forms
a) (`:` term)?
b) `:` term
return `term` if it is present, or a hole if not. -/
private def expandBinderType (ref : Syntax) (stx : Syntax) : Syntax :=
if stx.getNumArgs == 0 then
mkHole ref
else
stx[1]
/-- Given syntax of the form `ident <|> hole`, return `ident`. If `hole`, then we create a new anonymous name. -/
private def expandBinderIdent (stx : Syntax) : TermElabM Syntax :=
match_syntax stx with
| `(_) => mkFreshIdent stx
| _ => pure stx
/-- Given syntax of the form `(ident >> " : ")?`, return `ident`, or a new instance name. -/
private def expandOptIdent (stx : Syntax) : TermElabM Syntax := do
if stx.isNone then
let id ← withFreshMacroScope <| MonadQuotation.addMacroScope `inst
return mkIdentFrom stx id
else
return stx[0]
structure BinderView :=
(id : Syntax)
(type : Syntax)
(bi : BinderInfo)
partial def quoteAutoTactic : Syntax → TermElabM Syntax
| stx@(Syntax.ident _ _ _ _) => throwErrorAt stx "invalic auto tactic, identifier is not allowed"
| stx@(Syntax.node k args) => do
if stx.isAntiquot then
throwErrorAt stx "invalic auto tactic, antiquotation is not allowed"
else
let mut quotedArgs ← `(Array.empty)
for arg in args do
if k == nullKind && Quotation.isAntiquotSplice arg then
throwErrorAt arg "invalic auto tactic, antiquotation is not allowed"
else
let quotedArg ← quoteAutoTactic arg
quotedArgs ← `(Array.push $quotedArgs $quotedArg)
`(Syntax.node $(quote k) $quotedArgs)
| Syntax.atom info val => `(Syntax.atom {} $(quote val))
| Syntax.missing => unreachable!
def declareTacticSyntax (tactic : Syntax) : TermElabM Name :=
withFreshMacroScope do
let name ← MonadQuotation.addMacroScope `_auto
let type := Lean.mkConst `Lean.Syntax
let tactic ← quoteAutoTactic tactic
let val ← elabTerm tactic type
let val ← instantiateMVars val
trace[Elab.autoParam]! val
let decl := Declaration.defnDecl { name := name, lparams := [], type := type, value := val, hints := ReducibilityHints.opaque, isUnsafe := false }
addDecl decl
compileDecl decl
pure name
/-
Expand `optional (binderTactic <|> binderDefault)`
def binderTactic := parser! " := " >> " by " >> tacticParser
def binderDefault := parser! " := " >> termParser
-/
private def expandBinderModifier (type : Syntax) (optBinderModifier : Syntax) : TermElabM Syntax := do
if optBinderModifier.isNone then
pure type
else
let modifier := optBinderModifier[0]
let kind := modifier.getKind
if kind == `Lean.Parser.Term.binderDefault then
let defaultVal := modifier[1]
`(optParam $type $defaultVal)
else if kind == `Lean.Parser.Term.binderTactic then
let tac := modifier[2]
let name ← declareTacticSyntax tac
`(autoParam $type $(mkIdentFrom tac name))
else
throwUnsupportedSyntax
private def getBinderIds (ids : Syntax) : TermElabM (Array Syntax) :=
ids.getArgs.mapM fun id =>
let k := id.getKind
if k == identKind || k == `Lean.Parser.Term.hole then
pure id
else
throwErrorAt id "identifier or `_` expected"
/-
Recall that
```
def typeSpec := parser! " : " >> termParser
def optType : Parser := optional typeSpec
``` -/
def expandOptType (ref : Syntax) (optType : Syntax) : Syntax :=
if optType.isNone then
mkHole ref
else
optType[0][1]
private def matchBinder (stx : Syntax) : TermElabM (Array BinderView) :=
match stx with
| Syntax.node k args => do
if k == `Lean.Parser.Term.simpleBinder then
-- binderIdent+ >> optType
let ids ← getBinderIds args[0]
let type := expandOptType stx args[1]
ids.mapM fun id => do pure { id := (← expandBinderIdent id), type := type, bi := BinderInfo.default }
else if k == `Lean.Parser.Term.explicitBinder then
-- `(` binderIdent+ binderType (binderDefault <|> binderTactic)? `)`
let ids ← getBinderIds args[1]
let type := expandBinderType stx args[2]
let optModifier := args[3]
let type ← expandBinderModifier type optModifier
ids.mapM fun id => do pure { id := (← expandBinderIdent id), type := type, bi := BinderInfo.default }
else if k == `Lean.Parser.Term.implicitBinder then
-- `{` binderIdent+ binderType `}`
let ids ← getBinderIds args[1]
let type := expandBinderType stx args[2]
ids.mapM fun id => do pure { id := (← expandBinderIdent id), type := type, bi := BinderInfo.implicit }
else if k == `Lean.Parser.Term.instBinder then
-- `[` optIdent type `]`
let id ← expandOptIdent args[1]
let type := args[2]
pure #[ { id := id, type := type, bi := BinderInfo.instImplicit } ]
else
throwUnsupportedSyntax
| _ => throwUnsupportedSyntax
private def registerFailedToInferBinderTypeInfo (type : Expr) (ref : Syntax) : TermElabM Unit :=
registerCustomErrorIfMVar type ref "failed to infer binder type"
private partial def elabBinderViews {α} (binderViews : Array BinderView) (catchUnboundImplicit : Bool) (fvars : Array Expr) (k : Array Expr → TermElabM α)
: TermElabM α :=
let rec loop (i : Nat) (fvars : Array Expr) : TermElabM α := do
if h : i < binderViews.size then
let binderView := binderViews.get ⟨i, h⟩
if catchUnboundImplicit then
elabTypeWithUnboundImplicit binderView.type fun unboundImplicitFVars type => do
registerFailedToInferBinderTypeInfo type binderView.type
let fvars := fvars ++ unboundImplicitFVars
withLocalDecl binderView.id.getId binderView.bi type fun fvar =>
loop (i+1) (fvars.push fvar)
else
let type ← elabType binderView.type
registerFailedToInferBinderTypeInfo type binderView.type
withLocalDecl binderView.id.getId binderView.bi type fun fvar =>
loop (i+1) (fvars.push fvar)
else
k fvars
loop 0 fvars
private partial def elabBindersAux {α} (binders : Array Syntax) (catchUnboundImplicit : Bool) (k : Array Expr → TermElabM α) : TermElabM α :=
let rec loop (i : Nat) (fvars : Array Expr) : TermElabM α := do
if h : i < binders.size then
let binderViews ← matchBinder (binders.get ⟨i, h⟩)
elabBinderViews binderViews catchUnboundImplicit fvars <| loop (i+1)
else
k fvars
loop 0 #[]
/--
Elaborate the given binders (i.e., `Syntax` objects for `simpleBinder <|> bracketedBinder`),
update the local context, set of local instances, reset instance chache (if needed), and then
execute `x` with the updated context. -/
def elabBinders {α} (binders : Array Syntax) (k : Array Expr → TermElabM α) (catchUnboundImplicit := false) : TermElabM α :=
withoutPostponingUniverseConstraints do
if binders.isEmpty then
k #[]
else
elabBindersAux binders catchUnboundImplicit k
@[inline] def elabBinder {α} (binder : Syntax) (x : Expr → TermElabM α) (catchUnboundImplicit := false) : TermElabM α :=
elabBinders #[binder] (catchUnboundImplicit := catchUnboundImplicit) (fun fvars => x (fvars.get! 0))
@[builtinTermElab «forall»] def elabForall : TermElab := fun stx _ =>
match_syntax stx with
| `(forall $binders*, $term) =>
elabBinders binders fun xs => do
let e ← elabType term
mkForallFVars xs e
| _ => throwUnsupportedSyntax
@[builtinTermElab arrow] def elabArrow : TermElab :=
adaptExpander fun stx => match_syntax stx with
| `($dom:term -> $rng) => `(forall (a : $dom), $rng)
| _ => throwUnsupportedSyntax
@[builtinTermElab depArrow] def elabDepArrow : TermElab := fun stx _ =>
-- bracketedBinder `->` term
let binder := stx[0]
let term := stx[2]
elabBinders #[binder] fun xs => do
mkForallFVars xs (← elabType term)
/--
Auxiliary functions for converting `Term.app ... (Term.app id_1 id_2) ... id_n` into `#[id_1, ..., id_m]`
It is used at `expandFunBinders`. -/
private partial def getFunBinderIds? (stx : Syntax) : TermElabM (Option (Array Syntax)) :=
let rec loop (idOnly : Bool) (stx : Syntax) (acc : Array Syntax) :=
match_syntax stx with
| `($f $a) => do
if idOnly then
pure none
else
let (some acc) ← loop false f acc | pure none
loop true a acc
| `(_) => do let ident ← mkFreshIdent stx; pure (some (acc.push ident))
| `($id:ident) => pure (some (acc.push id))
| _ => pure none
loop false stx #[]
/--
Auxiliary function for expanding `fun` notation binders. Recall that `fun` parser is defined as
```
def funBinder : Parser := implicitBinder <|> instBinder <|> termParser maxPrec
parser! unicodeSymbol "λ" "fun" >> many1 funBinder >> "=>" >> termParser
```
to allow notation such as `fun (a, b) => a + b`, where `(a, b)` should be treated as a pattern.
The result is a pair `(explicitBinders, newBody)`, where `explicitBinders` is syntax of the form
```
`(` ident `:` term `)`
```
which can be elaborated using `elabBinders`, and `newBody` is the updated `body` syntax.
We update the `body` syntax when expanding the pattern notation.
Example: `fun (a, b) => a + b` expands into `fun _a_1 => match _a_1 with | (a, b) => a + b`.
See local function `processAsPattern` at `expandFunBindersAux`.
The resulting `Bool` is true if a pattern was found. We use it "mark" a macro expansion. -/
partial def expandFunBinders (binders : Array Syntax) (body : Syntax) : TermElabM (Array Syntax × Syntax × Bool) :=
let rec loop (body : Syntax) (i : Nat) (newBinders : Array Syntax) := do
if h : i < binders.size then
let binder := binders.get ⟨i, h⟩
let processAsPattern : Unit → TermElabM (Array Syntax × Syntax × Bool) := fun _ => do
let pattern := binder
let major ← mkFreshIdent binder
let (binders, newBody, _) ← loop body (i+1) (newBinders.push $ mkExplicitBinder major (mkHole binder))
let newBody ← `(match $major:ident with | $pattern => $newBody)
pure (binders, newBody, true)
match binder with
| Syntax.node `Lean.Parser.Term.implicitBinder _ => loop body (i+1) (newBinders.push binder)
| Syntax.node `Lean.Parser.Term.instBinder _ => loop body (i+1) (newBinders.push binder)
| Syntax.node `Lean.Parser.Term.explicitBinder _ => loop body (i+1) (newBinders.push binder)
| Syntax.node `Lean.Parser.Term.simpleBinder _ => loop body (i+1) (newBinders.push binder)
| Syntax.node `Lean.Parser.Term.hole _ =>
let ident ← mkFreshIdent binder
let type := binder
loop body (i+1) (newBinders.push $ mkExplicitBinder ident type)
| Syntax.node `Lean.Parser.Term.paren args =>
-- `(` (termParser >> parenSpecial)? `)`
-- parenSpecial := (tupleTail <|> typeAscription)?
let binderBody := binder[1]
if binderBody.isNone then processAsPattern ()
else
let idents := binderBody[0]
let special := binderBody[1]
if special.isNone then processAsPattern ()
else if special[0].getKind != `Lean.Parser.Term.typeAscription then
processAsPattern ()
else
-- typeAscription := `:` term
let type := special[0][1]
match (← getFunBinderIds? idents) with
| some idents => loop body (i+1) (newBinders ++ idents.map (fun ident => mkExplicitBinder ident type))
| none => processAsPattern ()
| Syntax.ident _ _ _ _ =>
let type := mkHole binder
loop body (i+1) (newBinders.push $ mkExplicitBinder binder type)
| _ => processAsPattern ()
else
pure (newBinders, body, false)
loop body 0 #[]
namespace FunBinders
structure State :=
(fvars : Array Expr := #[])
(lctx : LocalContext)
(localInsts : LocalInstances)
(expectedType? : Option Expr := none)
private def propagateExpectedType (fvar : Expr) (fvarType : Expr) (s : State) : TermElabM State := do
match s.expectedType? with
| none => pure s
| some expectedType =>
let expectedType ← whnfForall expectedType
match expectedType with
| Expr.forallE _ d b _ =>
isDefEq fvarType d
let b := b.instantiate1 fvar
pure { s with expectedType? := some b }
| _ => pure { s with expectedType? := none }
private partial def elabFunBinderViews (binderViews : Array BinderView) (i : Nat) (s : State) : TermElabM State :=
if h : i < binderViews.size then
let binderView := binderViews.get ⟨i, h⟩
withRef binderView.type $ withLCtx s.lctx s.localInsts do
let type ← elabType binderView.type
registerFailedToInferBinderTypeInfo type binderView.type
let fvarId ← mkFreshFVarId
let fvar := mkFVar fvarId
let s := { s with fvars := s.fvars.push fvar }
-- dbgTrace (toString binderView.id.getId ++ " : " ++ toString type)
/-
We do **not** want to support default and auto arguments in lambda abstractions.
Example: `fun (x : Nat := 10) => x+1`.
We do not believe this is an useful feature, and it would complicate the logic here.
-/
let lctx := s.lctx.mkLocalDecl fvarId binderView.id.getId type binderView.bi
let s ← withRef binderView.id $ propagateExpectedType fvar type s
let s := { s with lctx := lctx }
match (← isClass? type) with
| none => elabFunBinderViews binderViews (i+1) s
| some className =>
resettingSynthInstanceCache do
let localInsts := s.localInsts.push { className := className, fvar := mkFVar fvarId }
elabFunBinderViews binderViews (i+1) { s with localInsts := localInsts }
else
pure s
partial def elabFunBindersAux (binders : Array Syntax) (i : Nat) (s : State) : TermElabM State := do
if h : i < binders.size then
let binderViews ← matchBinder (binders.get ⟨i, h⟩)
let s ← elabFunBinderViews binderViews 0 s
elabFunBindersAux binders (i+1) s
else
pure s
end FunBinders
def elabFunBinders {α} (binders : Array Syntax) (expectedType? : Option Expr) (x : Array Expr → Option Expr → TermElabM α) : TermElabM α :=
if binders.isEmpty then
x #[] expectedType?
else do
let lctx ← getLCtx
let localInsts ← getLocalInstances
let s ← FunBinders.elabFunBindersAux binders 0 { lctx := lctx, localInsts := localInsts, expectedType? := expectedType? }
resettingSynthInstanceCacheWhen (s.localInsts.size > localInsts.size) $ withLCtx s.lctx s.localInsts $
x s.fvars s.expectedType?
/- Helper function for `expandEqnsIntoMatch` -/
private def getMatchAltNumPatterns (matchAlts : Syntax) : Nat :=
let alt0 := matchAlts[1][0]
let pats := alt0[0].getSepArgs
pats.size
private def mkMatch (ref : Syntax) (discrs : Array Syntax) (matchAlts : Syntax) (matchTactic := false) : Syntax :=
Syntax.node (if matchTactic then `Lean.Parser.Tactic.match else `Lean.Parser.Term.match)
#[mkAtomFrom ref "match ", mkNullNode discrs, mkNullNode, mkAtomFrom ref " with ", matchAlts]
private def addDiscr (ref : Syntax) (discrs : Array Syntax) (discrTerm : Syntax) : Array Syntax :=
let discrs := if discrs.isEmpty then discrs else discrs.push $ mkAtomFrom ref ", "
discrs.push <| Syntax.node `Lean.Parser.Term.matchDiscr #[mkNullNode, discrTerm]
/-
Recall that
```
def letRecDecls := sepBy1 (group (optional «attributes» >> letDecl)) ", "
def «letrec» := parser!:leadPrec withPosition (group ("let " >> nonReservedSymbol "rec ") >> letRecDecls) >> optSemicolon termParser
```
The argument `letRecDecls` is an array of syntax terms of the form `(group (optional «attributes» >> letDecl))`
-/
def mkLetRec (ref : Syntax) (letRecDecls : Array Syntax) (body : Syntax) : Syntax :=
Syntax.node `Lean.Parser.Term.letrec #[
mkNullNode #[mkAtomFrom ref "let ", mkAtomFrom ref "rec "],
Syntax.mkSep letRecDecls (mkAtomFrom ref ","),
mkNullNode, -- optional ";"
body
]
/- Recall that
```
whereDecls := parser! "where " >> many1Indent (group (optional «attributes» >> letDecl >> optional ";"))
``` -/
def expandWhereDecls (ref : Syntax) (whereDecls : Syntax) (body : Syntax) : Syntax :=
let letRecDecls := whereDecls[1].getArgs.map fun stx => mkNullNode #[stx[0], stx[1]] -- drop (optional ";")
return mkLetRec ref letRecDecls body
def expandWhereDeclsOpt (ref : Syntax) (whereDeclsOpt : Syntax) (body : Syntax) : Syntax :=
if whereDeclsOpt.isNone then
body
else
expandWhereDecls ref whereDeclsOpt[0] body
/- Helper function for `expandMatchAltsIntoMatch` -/
private def expandMatchAltsIntoMatchAux (ref : Syntax) (matchAlts : Syntax) (matchTactic : Bool) : Nat → Array Syntax → MacroM Syntax
| 0, discrs =>
return mkMatch ref discrs matchAlts matchTactic
| n+1, discrs => withFreshMacroScope do
let x ← `(x)
let body ← expandMatchAltsIntoMatchAux ref matchAlts matchTactic n (addDiscr ref discrs x)
if matchTactic then
`(tactic| intro $x:term; $body:tactic)
else
`(@fun $x => $body)
/--
Expand `matchAlts` syntax into a full `match`-expression.
Example
```
| 0, true => alt_1
| i, _ => alt_2
```
expands into (for tactic == false)
```
fun x_1 x_2 =>
match x_1, x_2 with
| 0, true => alt_1
| i, _ => alt_2
```
and (for tactic == true)
```
intro x_1; intro x_2;
match x_1, x_2 with
| 0, true => alt_1
| i, _ => alt_2
```
-/
def expandMatchAltsIntoMatch (ref : Syntax) (matchAlts : Syntax) (tactic := false) : MacroM Syntax :=
expandMatchAltsIntoMatchAux ref matchAlts tactic (getMatchAltNumPatterns matchAlts) #[]
def expandMatchAltsIntoMatchTactic (ref : Syntax) (matchAlts : Syntax) : MacroM Syntax :=
expandMatchAltsIntoMatchAux ref matchAlts true (getMatchAltNumPatterns matchAlts) #[]
/--
Similar to `expandMatchAltsIntoMatch`, but supports an optional `where` clause.
Expand `matchAltsWhereDecls` into `let rec` + `match`-expression.
Example
```
| 0, true => ... f 0 ...
| i, _ => ... f i + g i ...
where
f x := g x + 1
g : Nat → Nat
| 0 => 1
| x+1 => f x
```
expands into
```
fux x_1 x_2 =>
let rec
f x := g x + 1,
g : Nat → Nat
| 0 => 1
| x+1 => f x
match x_1, x_2 with
| 0, true => ... f 0 ...
| i, _ => ... f i + g i ...
```
-/
def expandMatchAltsWhereDecls (ref : Syntax) (matchAltsWhereDecls : Syntax) : MacroM Syntax :=
let matchAlts := matchAltsWhereDecls[0]
let whereDeclsOpt := matchAltsWhereDecls[1]
let rec loop (i : Nat) (discrs : Array Syntax) : MacroM Syntax :=
match i with
| 0 =>
let matchStx := mkMatch ref discrs matchAlts
if whereDeclsOpt.isNone then
return matchStx
else
expandWhereDeclsOpt ref whereDeclsOpt matchStx
| n+1 => withFreshMacroScope do
let x ← `(x)
let body ← loop n (addDiscr ref discrs x)
`(@fun $x => $body)
loop (getMatchAltNumPatterns matchAlts) #[]
@[builtinTermElab «fun»] def elabFun : TermElab := fun stx expectedType? => match_syntax stx with
| `(fun $binders* => $body) => do
let (binders, body, expandedPattern) ← expandFunBinders binders body
if expandedPattern then
let newStx ← `(fun $binders* => $body)
withMacroExpansion stx newStx $ elabTerm newStx expectedType?
else
elabFunBinders binders expectedType? fun xs expectedType? => do
/- We ensure the expectedType here since it will force coercions to be applied if needed.
If we just use `elabTerm`, then we will need to a coercion `Coe (α → β) (α → δ)` whenever there is a coercion `Coe β δ`,
and another instance for the dependent version. -/
let e ← elabTermEnsuringType body expectedType?
mkLambdaFVars xs e
| `(fun $m:matchAlts) => do
let stxNew ← liftMacroM $ expandMatchAltsIntoMatch stx m
withMacroExpansion stx stxNew $ elabTerm stxNew expectedType?
| _ => throwUnsupportedSyntax
/- If `useLetExpr` is true, then a kernel let-expression `let x : type := val; body` is created.
Otherwise, we create a term of the form `(fun (x : type) => body) val`
The default elaboration order is `binders`, `typeStx`, `valStx`, and `body`.
If `elabBodyFirst == true`, then we use the order `binders`, `typeStx`, `body`, and `valStx`. -/
def elabLetDeclAux (n : Name) (binders : Array Syntax) (typeStx : Syntax) (valStx : Syntax) (body : Syntax)
(expectedType? : Option Expr) (useLetExpr : Bool) (elabBodyFirst : Bool) : TermElabM Expr := do
let (type, val, arity) ← elabBinders binders fun xs => do
let type ← elabType typeStx
registerCustomErrorIfMVar type typeStx "failed to infer 'let' declaration type"
if elabBodyFirst then
let type ← mkForallFVars xs type
let val ← mkFreshExprMVar type
pure (type, val, xs.size)
else
let val ← elabTermEnsuringType valStx type
let type ← mkForallFVars xs type
let val ← mkLambdaFVars xs val
pure (type, val, xs.size)
trace[Elab.let.decl]! "{n} : {type} := {val}"
let result ←
if useLetExpr then
withLetDecl n type val fun x => do
let body ← elabTerm body expectedType?
let body ← instantiateMVars body
mkLetFVars #[x] body
else
let f ← withLocalDecl n BinderInfo.default type fun x => do
let body ← elabTerm body expectedType?
let body ← instantiateMVars body
mkLambdaFVars #[x] body
pure $ mkApp f val
if elabBodyFirst then
forallBoundedTelescope type arity fun xs type => do
let valResult ← elabTermEnsuringType valStx type
let valResult ← mkLambdaFVars xs valResult
unless (← isDefEq val valResult) do
throwError "unexpected error when elaborating 'let'"
pure result
structure LetIdDeclView :=
(id : Name)
(binders : Array Syntax)
(type : Syntax)
(value : Syntax)
def mkLetIdDeclView (letIdDecl : Syntax) : LetIdDeclView :=
-- `letIdDecl` is of the form `ident >> many bracketedBinder >> optType >> " := " >> termParser
let id := letIdDecl[0].getId
let binders := letIdDecl[1].getArgs
let optType := letIdDecl[2]
let type := expandOptType letIdDecl optType
let value := letIdDecl[4]
{ id := id, binders := binders, type := type, value := value }
private def expandLetEqnsDeclVal (ref : Syntax) (alts : Syntax) : Nat → Array Syntax → MacroM Syntax
| 0, discrs =>
pure $ Syntax.node `Lean.Parser.Term.match
#[mkAtomFrom ref "match ", mkNullNode discrs, mkNullNode, mkAtomFrom ref " with ", alts]
| n+1, discrs => withFreshMacroScope do
let x ← `(x)
let discrs := if discrs.isEmpty then discrs else discrs.push $ mkAtomFrom ref ", "
let discrs := discrs.push $ Syntax.node `Lean.Parser.Term.matchDiscr #[mkNullNode, x]
let body ← expandLetEqnsDeclVal ref alts n discrs
`(fun $x => $body)
def expandLetEqnsDecl (letDecl : Syntax) : MacroM Syntax := do
let ref := letDecl
let matchAlts := letDecl[3]
let val ← expandMatchAltsIntoMatch ref matchAlts
pure $ Syntax.node `Lean.Parser.Term.letIdDecl #[letDecl[0], letDecl[1], letDecl[2], mkAtomFrom ref " := ", val]
def elabLetDeclCore (stx : Syntax) (expectedType? : Option Expr) (useLetExpr : Bool) (elabBodyFirst : Bool) : TermElabM Expr := do
let ref := stx
let letDecl := stx[1][0]
let body := stx[3]
if letDecl.getKind == `Lean.Parser.Term.letIdDecl then
let { id := id, binders := binders, type := type, value := val } := mkLetIdDeclView letDecl
elabLetDeclAux id binders type val body expectedType? useLetExpr elabBodyFirst
else if letDecl.getKind == `Lean.Parser.Term.letPatDecl then
-- node `Lean.Parser.Term.letPatDecl $ try (termParser >> pushNone >> optType >> " := ") >> termParser
let pat := letDecl[0]
let optType := letDecl[2]
let type := expandOptType stx optType
let val := letDecl[4]
let stxNew ← `(let x : $type := $val; match x with | $pat => $body)
let stxNew := match useLetExpr, elabBodyFirst with
| true, false => stxNew
| true, true => stxNew.setKind `Lean.Parser.Term.«let*»
| false, true => stxNew.setKind `Lean.Parser.Term.«let!»
| false, false => unreachable!
withMacroExpansion stx stxNew $ elabTerm stxNew expectedType?
else if letDecl.getKind == `Lean.Parser.Term.letEqnsDecl then
let letDeclIdNew ← liftMacroM $ expandLetEqnsDecl letDecl
let declNew := stx[1].setArg 0 letDeclIdNew
let stxNew := stx.setArg 1 declNew
withMacroExpansion stx stxNew $ elabTerm stxNew expectedType?
else
throwUnsupportedSyntax
@[builtinTermElab «let»] def elabLetDecl : TermElab :=
fun stx expectedType? => elabLetDeclCore stx expectedType? true false
@[builtinTermElab «let!»] def elabLetBangDecl : TermElab :=
fun stx expectedType? => elabLetDeclCore stx expectedType? false false
@[builtinTermElab «let*»] def elabLetStarDecl : TermElab :=
fun stx expectedType? => elabLetDeclCore stx expectedType? true true
builtin_initialize registerTraceClass `Elab.let
end Lean.Elab.Term
|
6a9db98052141e98564472d2e3ae1e5e5708c0a8 | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /stage0/src/Init/SizeOfLemmas.lean | 3c57afabef74b0e1e3eebbd0b61f42e8085f51c0 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | EdAyers/lean4 | 57ac632d6b0789cb91fab2170e8c9e40441221bd | 37ba0df5841bde51dbc2329da81ac23d4f6a4de4 | refs/heads/master | 1,676,463,245,298 | 1,660,619,433,000 | 1,660,619,433,000 | 183,433,437 | 1 | 0 | Apache-2.0 | 1,657,612,672,000 | 1,556,196,574,000 | Lean | UTF-8 | Lean | false | false | 1,127 | lean | /-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Init.Meta
import Init.SizeOf
import Init.Data.Nat.Linear
@[simp] theorem Fin.sizeOf (a : Fin n) : sizeOf a = a.val + 1 := by
cases a; simp_arith
@[simp] theorem UInt8.sizeOf (a : UInt8) : sizeOf a = a.toNat + 2 := by
cases a; simp_arith [UInt8.toNat]
@[simp] theorem UInt16.sizeOf (a : UInt16) : sizeOf a = a.toNat + 2 := by
cases a; simp_arith [UInt16.toNat]
@[simp] theorem UInt32.sizeOf (a : UInt32) : sizeOf a = a.toNat + 2 := by
cases a; simp_arith [UInt32.toNat]
@[simp] theorem UInt64.sizeOf (a : UInt64) : sizeOf a = a.toNat + 2 := by
cases a; simp_arith [UInt64.toNat]
@[simp] theorem USize.sizeOf (a : USize) : sizeOf a = a.toNat + 2 := by
cases a; simp_arith [USize.toNat]
@[simp] theorem Char.sizeOf (a : Char) : sizeOf a = a.toNat + 3 := by
cases a; simp_arith [Char.toNat]
@[simp] theorem Subtype.sizeOf {α : Sort u_1} {p : α → Prop} (s : Subtype p) : sizeOf s = sizeOf s.val + 1 := by
cases s; simp_arith
|
5c8708002acd93de375f9b45ae5e3400045f47ab | 88892181780ff536a81e794003fe058062f06758 | /src/100_theorems/t022.lean | e1a7f3beeef99f2f4154a4de57f7d8e5aae7b317 | [] | no_license | AtnNn/lean-sandbox | fe2c44280444e8bb8146ab8ac391c82b480c0a2e | 8c68afbdc09213173aef1be195da7a9a86060a97 | refs/heads/master | 1,623,004,395,876 | 1,579,969,507,000 | 1,579,969,507,000 | 146,666,368 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 162 | lean | import data.real.cardinality
-- The Non-Denumerability of the Continuum
open set
lemma t022 : ¬ countable (set.univ : set ℝ) := cardinal.not_countable_real
|
1cefc369cfaf4722e9ba29b5b6c0f899e671150b | a4673261e60b025e2c8c825dfa4ab9108246c32e | /src/Init/Notation.lean | b0be3e21c3f7909e6efcb1ab366706ad012f2f81 | [
"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 | 7,094 | 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
Notation for operators defined at Prelude.lean
-/
prelude
import Init.Prelude
infixr:90 " ∘ " => Function.comp
infixr:35 " × " => Prod
infixl:65 " + " => Add.add
infixl:65 " - " => Sub.sub
infixl:70 " * " => Mul.mul
infixl:70 " / " => Div.div
infixl:70 " % " => Mod.mod
infixl:70 " %ₙ " => ModN.modn
infixr:80 " ^ " => Pow.pow
infix:50 " ≤ " => HasLessEq.LessEq
infix:50 " <= " => HasLessEq.LessEq
infix:50 " < " => HasLess.Less
infix:50 " ≥ " => GreaterEq
infix:50 " >= " => GreaterEq
infix:50 " > " => Greater
infix:50 " = " => Eq
infix:50 " == " => BEq.beq
infix:50 " ~= " => HEq
infix:50 " ≅ " => HEq
infixr:35 " ∧ " => And
infixr:35 " /\\ " => And
infixr:30 " ∨ " => Or
infixr:30 " \\/ " => Or
infixl:35 " && " => and
infixl:30 " || " => or
infixl:65 " ++ " => Append.append
infixr:67 " :: " => List.cons
infixr:2 " <|> " => OrElse.orElse
infixr:60 " >> " => AndThen.andThen
infixl:55 " >>= " => Bind.bind
infixl:60 " <*> " => Seq.seq
infixl:60 " <* " => SeqLeft.seqLeft
infixr:60 " *> " => SeqRight.seqRight
infixr:100 " <$> " => Functor.map
macro "if" h:ident " : " c:term " then " t:term " else " e:term : term =>
`(dite $c (fun $h => $t) (fun $h => $e))
macro "if" c:term " then " t:term " else " e:term : term =>
`(ite $c $t $e)
syntax "[" sepBy(term, ", ") "]" : term
open Lean in
macro_rules
| `([ $elems* ]) => do
let rec expandListLit (i : Nat) (skip : Bool) (result : Syntax) : MacroM Syntax := do
match i, skip with
| 0, _ => pure result
| i+1, true => expandListLit i false result
| i+1, false => expandListLit i true (← `(List.cons $(elems[i]) $result))
expandListLit elems.size false (← `(List.nil))
syntax:0 term "<|" term:0 : term
macro_rules
| `($f $args* <| $a) => let args := args.push a; `($f $args*)
| `($f <| $a) => `($f $a)
syntax:0 term "|>" term:1 : term
macro_rules
| `($a |> $f $args*) => let args := args.push a; `($f $args*)
| `($a |> $f) => `($f $a)
-- Haskell-like pipe <|
-- Note that we have a whitespace after `$` to avoid an ambiguity with the antiquotations.
syntax:0 term "$" ws term:0 : term
macro_rules
| `($f $args* $ $a) => let args := args.push a; `($f $args*)
| `($f $ $a) => `($f $a)
-- Basic notation for defining parsers
syntax stx "+" : stx
syntax stx "*" : stx
syntax stx "?" : stx
syntax:2 stx " <|> " stx:1 : stx
macro_rules
| `(stx| $p +) => `(stx| many1($p))
| `(stx| $p *) => `(stx| many($p))
| `(stx| $p ?) => `(stx| optional($p))
| `(stx| $p₁ <|> $p₂) => `(stx| orelse($p₁, $p₂))
macro "!" x:stx : stx => `(stx| notFollowedBy($x))
syntax "{ " ident (" : " term)? " // " term " }" : term
macro_rules
| `({ $x : $type // $p }) => `(Subtype (fun ($x:ident : $type) => $p))
| `({ $x // $p }) => `(Subtype (fun ($x:ident : _) => $p))
namespace Lean.Parser.Tactic
syntax[intro] "intro " notFollowedBy("|") (colGt term:max)* : tactic
syntax[intros] "intros " (colGt (ident <|> "_"))* : tactic
syntax[revert] "revert " (colGt ident)+ : tactic
syntax[clear] "clear " (colGt ident)+ : tactic
syntax[subst] "subst " (colGt ident)+ : tactic
syntax[assumption] "assumption" : tactic
syntax[apply] "apply " term : tactic
syntax[exact] "exact " term : tactic
syntax[refine] "refine " term : tactic
syntax[refine!] "refine! " term : tactic
syntax[case] "case " ident " => " tacticSeq : tactic
syntax[allGoals] "allGoals " tacticSeq : tactic
syntax[focus] "focus " tacticSeq : tactic
syntax[skip] "skip" : tactic
syntax[done] "done" : tactic
syntax[traceState] "traceState" : tactic
syntax[failIfSuccess] "failIfSuccess " tacticSeq : tactic
syntax[generalize] "generalize " atomic(ident " : ")? term:51 " = " ident : tactic
syntax[paren] "(" tacticSeq ")" : tactic
syntax locationWildcard := "*"
syntax locationTarget := "⊢" <|> "|-"
syntax locationHyp := (colGt ident)+
syntax location := withPosition("at " locationWildcard <|> locationTarget <|> locationHyp)
syntax[change] "change " term (location)? : tactic
syntax[changeWith] "change " term " with " term (location)? : tactic
syntax rwRule := ("←" <|> "<-")? term
syntax rwRuleSeq := "[" sepBy1T(rwRule, ", ") "]"
syntax[rewrite] "rewrite " rwRule (location)? : tactic
syntax[rewriteSeq, 1] "rewrite " rwRuleSeq (location)? : tactic
syntax[erewrite] "erewrite " rwRule (location)? : tactic
syntax[erewriteSeq, 1] "erewrite " rwRuleSeq (location)? : tactic
syntax[rw] "rw " rwRule (location)? : tactic
syntax[rwSeq, 1] "rw " rwRuleSeq (location)? : tactic
syntax[erw] "erw " rwRule (location)? : tactic
syntax[erwSeq, 1] "erw " rwRuleSeq (location)? : tactic
@[macro rw]
def expandRw : Macro :=
fun stx => return stx.setKind `Lean.Parser.Tactic.rewrite |>.setArg 0 (mkAtomFrom stx "rewrite")
@[macro rwSeq]
def expandRwSeq : Macro :=
fun stx => return stx.setKind `Lean.Parser.Tactic.rewriteSeq |>.setArg 0 (mkAtomFrom stx "rewrite")
@[macro erw]
def expandERw : Macro :=
fun stx => return stx.setKind `Lean.Parser.Tactic.erewrite |>.setArg 0 (mkAtomFrom stx "erewrite")
@[macro erwSeq]
def expandERwSeq : Macro :=
fun stx => return stx.setKind `Lean.Parser.Tactic.erewriteSeq |>.setArg 0 (mkAtomFrom stx "erewrite")
syntax:2[orelse] tactic "<|>" tactic:1 : tactic
syntax[injection] "injection " term (" with " (colGt (ident <|> "_"))+)? : tactic
syntax[«have»] "have " haveDecl : tactic
syntax[«suffices»] "suffices " sufficesDecl : tactic
syntax[«show»] "show " term : tactic
syntax[«let»] "let " letDecl : tactic
syntax[«let!»] "let! " letDecl : tactic
syntax[letrec] withPosition(atomic(group("let " &"rec ")) letRecDecls) : tactic
syntax inductionAlt := (ident <|> "_") (ident <|> "_")* " => " (hole <|> syntheticHole <|> tacticSeq)
syntax inductionAlts := "with " withPosition("| " sepBy1(inductionAlt, colGe "| "))
syntax[induction] "induction " sepBy1(term, ", ") (" using " ident)? ("generalizing " ident+)? (inductionAlts)? : tactic
syntax casesTarget := atomic(ident " : ")? term
syntax[cases] "cases " sepBy1(casesTarget, ", ") (" using " ident)? (inductionAlts)? : tactic
syntax matchAlt := sepBy1(term, ", ") " => " (hole <|> syntheticHole <|> tacticSeq)
syntax matchAlts := withPosition("| " sepBy1(matchAlt, colGe "| "))
syntax[«match»] "match " sepBy1(matchDiscr, ", ") (" : " term)? " with " matchAlts : tactic
syntax[introMatch] "intro " matchAlts : tactic
syntax[existsIntro] "exists " term : tactic
macro "rfl" : tactic => `(exact rfl)
macro "decide!" : tactic => `(exact decide!)
macro "admit" : tactic => `(exact sorry)
/- We use a priority > 0, to avoid ambiguity with the builtin `have` notation -/
macro[1] "have" x:ident " := " p:term : tactic => `(have $x:ident : _ := $p)
syntax "repeat " tacticSeq : tactic
macro_rules
| `(tactic| repeat $seq) => `(tactic| (($seq); repeat $seq) <|> skip)
macro "try " t:tacticSeq : tactic => `(($t) <|> skip)
end Lean.Parser.Tactic
|
50e297e560ab287c1df72f854a4585ac03dcd41a | 037dba89703a79cd4a4aec5e959818147f97635d | /src/2020/logic/bool_not.lean | 45c5f496ed7435f0b93bea65ca7584e1c0cc1023 | [] | no_license | ImperialCollegeLondon/M40001_lean | 3a6a09298da395ab51bc220a535035d45bbe919b | 62a76fa92654c855af2b2fc2bef8e60acd16ccec | refs/heads/master | 1,666,750,403,259 | 1,665,771,117,000 | 1,665,771,117,000 | 209,141,835 | 115 | 12 | null | 1,640,270,596,000 | 1,568,749,174,000 | Lean | UTF-8 | Lean | false | false | 3,380 | lean | import tactic
@[derive fintype] inductive bool2
| ff2 : bool2
| tt2 : bool2
namespace bool2
definition and2 : bool2 → bool2 → bool2
| ff2 P := ff2
| tt2 P := P
definition or2 : bool2 → bool2 → bool2
| tt2 P := tt2
| ff2 P := P
definition not2 : bool2 → bool2
| tt2 := ff2
| ff2 := tt2
definition xor2 (x y : bool2) := and2 (or2 x y) (not2 (and2 x y))
definition going : bool2 → bool
| ff2 := tt
| tt2 := ff
end bool2
open bool2
definition bool.to2 : bool → bool2
| tt := ff2
| ff := tt2
namespace bool2
-- no good -- false and true name clash
-- `bool2.F`, standing for "From `bool` (to `bool2` in this case)".
-- `bool2.T`, standing for "To `bool` (from `bool2` in this case)"
-- So anything we can do in `bool` about `bool2`
-- These functions bijectively identify `bool` and `bool2`.
open bool
definition equiv : bool2 ≃ bool :=
{ to_fun := going,
inv_fun := to2,
left_inv := begin
intro x,
cases x;
refl,
end,
right_inv := begin
rintro (ht | hf);
refl
end
}
end bool2
-- every definition involving bool has a corresponding definition
-- in bool2
-- What construction in `bool2` corresponds to `and` in `bool`?
example (x y : bool) : (x && y).to2 = or2 x.to2 y.to2 :=
begin
cases x;
cases y;
refl,
end
example (x y : bool) : (x || y).to2 = and2 x.to2 y.to2 :=
begin
cases x;
cases y;
refl,
end
example (x y : bool) : (bxor x y).to2 = not2 (xor2 x.to2 y.to2) :=
begin
cases x;
cases y;
refl,
-- sorry,sorry,sorry,sorry,
end
#print prefix bool
example (x : bool) : (bnot x).to2 = not2 (x.to2) :=
begin
cases x;
refl
end
def bimp : bool → bool → bool
| ff tt := ff
| _ _ := tt
-- corresponds to something awful
-- Computer scientists don't want to reason about bool
-- or prove theorems about it -- they just need it
-- to make data structures, recording yes-no answers
-- to questions about the terms involved.
example (b : bool) : b = ff ∨ b = tt := bool.dichotomy b
-- I can'y do this -- ask Chris?
example : ∀ f : bool → bool → bool,
(∀ x y : bool, f x y = f y x) →
(f = bor ∨ f = band ∨ f = bxor ∨ f = λ x y, bnot (band x y)
∨ f = λ x y, bnot (bor x y) ∨ f = λ x y, bnot (bxor x y) ∨ f = λ x y, tt ∨ f = λ x y, ff) :=
begin
intros,
rw function.funext_iff,
rw function.funext_iff,
rw function.funext_iff,
rw function.funext_iff,
cases (f tt tt).dichotomy;
cases (f tt ff).dichotomy;
cases (f ff tt).dichotomy;
cases (f ff ff).dichotomy,
{
sorry },
repeat {sorry},
-- exact dec_trivial,
end
-- now let's see forall and exists
variables (Ω : Type) (X Y : set Ω)
example : ¬ (∃ a, X a) ↔ ∀ b, ¬ (X b) :=
begin
split,
{ intro h,
intros b hb,
apply h,
use b,
assumption },
{ intro h,
intro h2,
cases h2 with a ha,
apply h a,
assumption },
end
example : ¬ (∀ a, X a) ↔ ∃ b, ¬ (X b) :=
begin
split,
{ -- classical
intro h,
classical,
by_contra hnX,
apply h,
intro a,
by_contra hXa,
apply hnX,
use a },
{ intro h,
cases h with b hb,
intro h,
apply hb,
apply h }
end
example : ¬ (∃ a, X a) ↔ ∀ b, ¬ (X b) :=
begin
split,
{ intro h,
intros b hb,
apply h,
use b,
assumption },
{ intro h,
intro h2,
cases h2 with a ha,
apply h a,
assumption },
end
|
ef3e10d3d00151d1f99b61a970adc3626eae55a7 | a7dd8b83f933e72c40845fd168dde330f050b1c9 | /src/topology/basic.lean | 1111da7b418ab54b68c4b8dbcc233f03ae735eac | [
"Apache-2.0"
] | permissive | NeilStrickland/mathlib | 10420e92ee5cb7aba1163c9a01dea2f04652ed67 | 3efbd6f6dff0fb9b0946849b43b39948560a1ffe | refs/heads/master | 1,589,043,046,346 | 1,558,938,706,000 | 1,558,938,706,000 | 181,285,984 | 0 | 0 | Apache-2.0 | 1,568,941,848,000 | 1,555,233,833,000 | Lean | UTF-8 | Lean | false | false | 35,958 | 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
Theory of topological spaces.
Parts of the formalization is based on the books:
N. Bourbaki: General Topology
I. M. James: Topologies and Uniformities
A major difference is that this formalization is heavily based on the filter library.
-/
import order.filter
open set filter lattice classical
local attribute [instance] prop_decidable
universes u v w
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
section topological_space
variables {α : Type u} {β : Type v} {ι : Sort w} {a : α} {s s₁ s₂ : set α} {p p₁ p₂ : α → Prop}
@[extensionality]
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 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_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 simp only [is_closed, compl_sInter, sUnion_image]; exact assume h, is_open_Union $ assume t, is_open_Union $ assume ht, h t ht
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
@[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_Union {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_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
/-- 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 interior_eq_of_open {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, interior_eq_of_open⟩
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 α) = ∅ :=
interior_eq_of_open is_open_empty
@[simp] lemma interior_univ : interior (univ : set α) = univ :=
interior_eq_of_open is_open_univ
@[simp] lemma interior_interior {s : set α} : interior (interior s) = interior s :=
interior_eq_of_open is_open_interior
@[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]
/-- 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 closure_eq_of_is_closed {s : set α} (h : is_closed s) : closure s = s :=
subset.antisymm (closure_minimal (subset.refl s) h) subset_closure
lemma closure_eq_iff_is_closed {s : set α} : closure s = s ↔ is_closed s :=
⟨assume h, h ▸ is_closed_closure, closure_eq_of_is_closed⟩
lemma closure_subset_iff_subset_of_is_closed {s t : set α} (h₁ : is_closed t) :
closure s ⊆ t ↔ s ⊆ t :=
⟨subset.trans subset_closure, assume h, closure_minimal h h₁⟩
lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t :=
closure_minimal (subset.trans h subset_closure) is_closed_closure
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
@[simp] lemma closure_empty : closure (∅ : set α) = ∅ :=
closure_eq_of_is_closed is_closed_empty
lemma closure_empty_iff (s : set α) : closure s = ∅ ↔ s = ∅ :=
begin
split; intro h,
{ rw set.eq_empty_iff_forall_not_mem,
intros x H,
simpa only [h] using subset_closure H },
{ exact (eq.symm h) ▸ closure_empty },
end
@[simp] lemma closure_univ : closure (univ : set α) = univ :=
closure_eq_of_is_closed is_closed_univ
@[simp] lemma closure_closure {s : set α} : closure (closure s) = closure s :=
closure_eq_of_is_closed is_closed_closure
@[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)
(union_subset (closure_mono $ subset_union_left _ _) (closure_mono $ subset_union_right _ _))
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 ≠ ∅ :=
⟨λ h o oo ao os,
have s ⊆ -o, from λ x xs xo, @ne_empty_of_mem α (o∩s) x ⟨xo, xs⟩ os,
closure_minimal this (is_closed_compl_iff.2 oo) h ao,
λ H c ⟨h₁, h₂⟩, classical.by_contradiction $ λ nc,
let ⟨x, hc, hs⟩ := exists_mem_of_ne_empty (H _ h₁ nc) in hc (h₂ hs)⟩
lemma dense_iff_inter_open {s : set α} : closure s = univ ↔ ∀ U, is_open U → U ≠ ∅ → U ∩ s ≠ ∅ :=
begin
split ; intro h,
{ intros U U_op U_ne,
cases exists_mem_of_ne_empty U_ne with x x_in,
exact mem_closure_iff.1 (by simp only [h]) U U_op x_in },
{ apply eq_univ_of_forall, intro x,
rw mem_closure_iff,
intros U U_op x_in,
exact h U U_op (ne_empty_of_mem x_in) },
end
/-- 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]
@[simp] lemma frontier_compl (s : set α) : frontier (-s) = frontier s :=
by simp only [frontier_eq_closure_inter_closure, lattice.neg_neg, inter_comm]
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
lemma interior_frontier {s : set α} (h : is_closed s) : interior (frontier s) = ∅ :=
begin
have A : frontier s = s \ interior s, by rw [frontier, closure_eq_of_is_closed h],
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
/-- neighbourhood filter -/
def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s)
lemma nhds_def (a : α) : nhds a = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s) := rfl
lemma le_nhds_iff {f a} : f ≤ nhds a ↔ ∀ s : set α, a ∈ s → is_open s → s ∈ f :=
by simp [nhds_def]
lemma nhds_le_of_le {f a} {s : set α} (h : a ∈ s) (o : is_open s) (sf : principal s ≤ f) : nhds a ≤ f :=
by rw nhds_def; exact infi_le_of_le s (infi_le_of_le ⟨h, o⟩ sf)
lemma nhds_sets {a : α} : (nhds a).sets = {s | ∃t⊆s, is_open t ∧ a ∈ t} :=
calc (nhds a).sets = (⋃s∈{s : set α| a ∈ s ∧ is_open s}, (principal s).sets) : infi_sets_eq'
(assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩,
⟨x ∩ y, ⟨⟨hx₁, hy₁⟩, is_open_inter hx₂ hy₂⟩,
le_principal_iff.2 (inter_subset_left _ _),
le_principal_iff.2 (inter_subset_right _ _)⟩)
⟨univ, mem_univ _, is_open_univ⟩
... = {s | ∃t⊆s, is_open t ∧ a ∈ t} :
le_antisymm
(supr_le $ assume i, supr_le $ assume ⟨hi₁, hi₂⟩ t ht, ⟨i, ht, hi₂, hi₁⟩)
(assume t ⟨i, hi₁, hi₂, hi₃⟩, mem_Union.2 ⟨i, mem_Union.2 ⟨⟨hi₃, hi₂⟩, hi₁⟩⟩)
lemma map_nhds {a : α} {f : α → β} :
map f (nhds a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal (image f s)) :=
calc map f (nhds a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, map f (principal s)) :
map_binfi_eq
(assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩,
⟨x ∩ y, ⟨⟨hx₁, hy₁⟩, is_open_inter hx₂ hy₂⟩,
le_principal_iff.2 (inter_subset_left _ _),
le_principal_iff.2 (inter_subset_right _ _)⟩)
⟨univ, mem_univ _, is_open_univ⟩
... = _ : by simp only [map_principal]
attribute [irreducible] nhds
lemma mem_nhds_sets_iff {a : α} {s : set α} :
s ∈ nhds a ↔ ∃t⊆s, is_open t ∧ a ∈ t :=
by simp only [nhds_sets, mem_set_of_eq, exists_prop]
lemma mem_of_nhds {a : α} {s : set α} : s ∈ nhds a → a ∈ s :=
λ H, let ⟨t, ht, _, hs⟩ := mem_nhds_sets_iff.1 H in ht hs
lemma mem_nhds_sets {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) :
s ∈ nhds a :=
mem_nhds_sets_iff.2 ⟨s, subset.refl _, hs, ha⟩
theorem all_mem_nhds (x : α) (P : set α → Prop) (hP : ∀ s t, s ⊆ t → P s → P t) :
(∀ s ∈ nhds x, P s) ↔ (∀ s, is_open s → x ∈ s → P s) :=
iff.intro
(λ h s os xs, h s (mem_nhds_sets os xs))
(λ h t,
begin
change t ∈ (nhds x).sets → P t,
rw nhds_sets,
rintros ⟨s, hs, opens, xs⟩,
exact hP _ _ hs (h s opens xs),
end)
theorem all_mem_nhds_filter (x : α) (f : set α → set β) (hf : ∀ s t, s ⊆ t → f s ⊆ f t)
(l : filter β) :
(∀ s ∈ nhds 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 (nhds a) ↔ (∀ s, is_open s → a ∈ s → r.core s ∈ l) :=
all_mem_nhds_filter _ _ (λ s t h, λ x hx, λ y hy, h (hx y hy)) _
theorem rtendsto'_nhds {r : rel β α} {l : filter β} {a : α} :
rtendsto' r l (nhds 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 (nhds a) ↔ (∀ s, is_open s → a ∈ s → f.core s ∈ l) :=
rtendsto_nhds
theorem ptendsto'_nhds {f : β →. α} {l : filter β} {a : α} :
ptendsto' f l (nhds a) ↔ (∀ s, is_open s → a ∈ s → f.preimage s ∈ l) :=
rtendsto'_nhds
theorem tendsto_nhds {f : β → α} {l : filter β} {a : α} :
tendsto f l (nhds 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 (nhds a) :=
tendsto_nhds.mpr $ assume s hs ha, univ_mem_sets' $ assume _, ha
lemma pure_le_nhds : pure ≤ (nhds : α → filter α) :=
assume a, by rw nhds_def; exact le_infi
(assume s, le_infi $ assume ⟨h₁, _⟩, principal_mono.mpr $
singleton_subset_iff.2 h₁)
lemma tendsto_pure_nhds [topological_space β] (f : α → β) (a : α) :
tendsto f (pure a) (nhds (f a)) :=
begin
rw [tendsto, filter.map_pure],
exact pure_le_nhds (f a)
end
@[simp] lemma nhds_neq_bot {a : α} : nhds a ≠ ⊥ :=
assume : nhds a = ⊥,
have pure a = (⊥ : filter α),
from lattice.bot_unique $ this ▸ pure_le_nhds a,
pure_neq_bot this
lemma interior_eq_nhds {s : set α} : interior s = {a | nhds a ≤ principal s} :=
set.ext $ λ x, by simp only [mem_interior, le_principal_iff, mem_nhds_sets_iff]; refl
lemma mem_interior_iff_mem_nhds {s : set α} {a : α} :
a ∈ interior s ↔ s ∈ nhds a :=
by simp only [interior_eq_nhds, le_principal_iff]; refl
lemma is_open_iff_nhds {s : set α} : is_open s ↔ ∀a∈s, nhds a ≤ principal s :=
calc is_open s ↔ s ⊆ interior s : subset_interior_iff_open.symm
... ↔ (∀a∈s, nhds a ≤ principal s) : by rw [interior_eq_nhds]; refl
lemma is_open_iff_mem_nhds {s : set α} : is_open s ↔ ∀a∈s, s ∈ nhds a :=
is_open_iff_nhds.trans $ forall_congr $ λ _, imp_congr_right $ λ _, le_principal_iff
lemma closure_eq_nhds {s : set α} : closure s = {a | nhds a ⊓ principal s ≠ ⊥} :=
calc closure s = - interior (- s) : closure_eq_compl_interior_compl
... = {a | ¬ nhds a ≤ principal (-s)} : by rw [interior_eq_nhds]; refl
... = {a | nhds a ⊓ principal s ≠ ⊥} : set.ext $ assume a, not_congr
(inf_eq_bot_iff_le_compl
(show principal s ⊔ principal (-s) = ⊤, by simp only [sup_principal, union_compl_self, principal_univ])
(by simp only [inf_principal, inter_compl_self, principal_empty])).symm
theorem mem_closure_iff_nhds {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ nhds a, t ∩ s ≠ ∅ :=
mem_closure_iff.trans
⟨λ H t ht, subset_ne_empty
(inter_subset_inter_left _ interior_subset)
(H _ is_open_interior (mem_interior_iff_mem_nhds.2 ht)),
λ H o oo ao, H _ (mem_nhds_sets oo ao)⟩
/-- `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.val ∧ u.val ≤ nhds x :=
begin
rw closure_eq_nhds, change nhds x ⊓ principal s ≠ ⊥ ↔ _, symmetry,
convert exists_ultrafilter_iff _, ext u,
rw [←le_principal_iff, inf_comm, le_inf_iff]
end
lemma is_closed_iff_nhds {s : set α} : is_closed s ↔ ∀a, nhds a ⊓ principal s ≠ ⊥ → a ∈ s :=
calc is_closed s ↔ closure s = s : by rw [closure_eq_iff_is_closed]
... ↔ closure s ⊆ s : ⟨assume h, by rw h, assume h, subset.antisymm h subset_closure⟩
... ↔ (∀a, nhds a ⊓ principal s ≠ ⊥ → a ∈ s) : by rw [closure_eq_nhds]; refl
lemma closure_inter_open {s t : set α} (h : is_open s) : s ∩ closure t ⊆ closure (s ∩ t) :=
assume a ⟨hs, ht⟩,
have s ∈ nhds a, from mem_nhds_sets h hs,
have nhds a ⊓ principal s = nhds a, from inf_of_le_left $ by rwa le_principal_iff,
have nhds a ⊓ principal (s ∩ t) ≠ ⊥,
from calc nhds a ⊓ principal (s ∩ t) = nhds a ⊓ (principal s ⊓ principal t) : by rw inf_principal
... = nhds a ⊓ principal t : by rw [←inf_assoc, this]
... ≠ ⊥ : by rw [closure_eq_nhds] at ht; assumption,
by rw [closure_eq_nhds]; assumption
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 mem_of_closed_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α}
(hb : b ≠ ⊥) (hf : tendsto f b (nhds a)) (hs : is_closed s) (h : f ⁻¹' s ∈ b) : a ∈ s :=
have b.map f ≤ nhds a ⊓ principal s,
from le_trans (le_inf (le_refl _) (le_principal_iff.mpr h)) (inf_le_inf hf (le_refl _)),
is_closed_iff_nhds.mp hs a $ neq_bot_of_le_neq_bot (map_ne_bot hb) this
lemma mem_of_closed_of_tendsto' {f : β → α} {x : filter β} {a : α} {s : set α}
(hf : tendsto f x (nhds a)) (hs : is_closed s) (h : x ⊓ principal (f ⁻¹' s) ≠ ⊥) : a ∈ s :=
is_closed_iff_nhds.mp hs _ $ neq_bot_of_le_neq_bot (@map_ne_bot _ _ _ f h) $
le_inf (le_trans (map_mono $ inf_le_left) hf) $
le_trans (map_mono $ inf_le_right_of_le $ by simp only [comap_principal, le_principal_iff]; exact subset.refl _) (@map_comap_le _ _ _ f)
lemma mem_closure_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α}
(hb : b ≠ ⊥) (hf : tendsto f b (nhds a)) (h : f ⁻¹' s ∈ b) : a ∈ closure s :=
mem_of_closed_of_tendsto hb hf (is_closed_closure) $
filter.mem_sets_of_superset h (preimage_mono subset_closure)
section lim
variables [inhabited α]
/-- If `f` is a filter, then `lim f` is a limit of the filter, if it exists. -/
noncomputable def lim (f : filter α) : α := epsilon $ λa, f ≤ nhds a
lemma lim_spec {f : filter α} (h : ∃a, f ≤ nhds a) : f ≤ nhds (lim f) := epsilon_spec h
end lim
/-
The nhds_within filter.
-/
def nhds_within (a : α) (s : set α) : filter α := nhds 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, lattice.binfi_inf]; try { exact this },
simp only [inf_principal]
end
theorem nhds_within_univ (a : α) : nhds_within a set.univ = nhds a :=
by rw [nhds_within, principal_univ, lattice.inf_top_eq]
theorem mem_nhds_within (t : set α) (a : α) (s : set α) :
t ∈ nhds_within a s ↔ ∃ u, is_open u ∧ a ∈ u ∧ u ∩ s ⊆ t :=
begin
rw [nhds_within, mem_inf_principal, mem_nhds_sets_iff], split,
{ rintros ⟨u, hu, openu, au⟩,
exact ⟨u, openu, au, λ x ⟨xu, xs⟩, hu xu xs⟩ },
rintros ⟨u, openu, au, hu⟩,
exact ⟨u, λ x xu xs, hu ⟨xu, xs⟩, openu, au⟩
end
theorem inter_mem_nhds_within (s : set α) {t : set α} {a : α} (h : t ∈ nhds 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 :=
lattice.inf_le_inf (le_refl _) (principal_mono.mpr h)
theorem nhds_within_restrict' {a : α} (s : set α) {t : set α} (h : t ∈ nhds a) :
nhds_within a s = nhds_within a (s ∩ t) :=
le_antisymm
(lattice.le_inf lattice.inf_le_left (le_principal_iff.mpr (inter_mem_nhds_within s h)))
(lattice.inf_le_inf (le_refl _) (principal_mono.mpr (set.inter_subset_left _ _)))
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_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 = nhds 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, lattice.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 [←lattice.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 [lattice.inf_left_comm, lattice.inf_assoc, inf_principal,
←lattice.inf_assoc, lattice.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, lattice.inf_assoc] }
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)) :=
have h₀ : directed_on ((λ (i : set α), principal (i ∩ s)) ⁻¹'o ge)
{x : set α | x ∈ {t : set α | a ∈ t ∧ is_open t}}, from
assume x ⟨ax, openx⟩ y ⟨ay, openy⟩,
⟨x ∩ y, ⟨⟨ax, ay⟩, is_open_inter openx openy⟩,
le_principal_iff.mpr (set.inter_subset_inter_left _ (set.inter_subset_left _ _)),
le_principal_iff.mpr (set.inter_subset_inter_left _ (set.inter_subset_right _ _))⟩,
have h₁ : ∃ (i : set α), i ∈ {t : set α | a ∈ t ∧ is_open t},
from ⟨set.univ, set.mem_univ _, is_open_univ⟩,
by { rw [nhds_within_eq, map_binfi_eq h₀ h₁], simp only [map_principal] }
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 (nhds 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
/- 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 ∈ nhds x, finite {i | f i ∩ t ≠ ∅ }
lemma locally_finite_of_finite {f : β → set α} (h : finite (univ : set β)) : locally_finite f :=
assume x, ⟨univ, univ_mem_sets, finite_subset h $ subset_univ _⟩
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₁, finite_subset ht₂ $ assume i hi,
neq_bot_of_le_neq_bot hi $ inter_subset_inter (hf i) $ subset.refl _⟩
lemma is_closed_Union_of_locally_finite {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 ∈ (nhds a).sets,
by rw [nhds_sets]; exact assume i, ⟨- f i, subset.refl _, h₂ i, this i⟩,
let ⟨t, h_sets, (h_fin : finite {i | f i ∩ t ≠ ∅ })⟩ := h₁ a in
calc nhds a ≤ principal (t ∩ (⋂ i∈{i | f i ∩ t ≠ ∅ }, - f i)) :
begin
rw [le_principal_iff],
apply @filter.inter_mem_sets _ (nhds a) _ _ h_sets,
apply @filter.Inter_mem_sets _ (nhds a) _ _ _ h_fin,
exact assume i h, this i
end
... ≤ principal (- ⋃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,
not_eq_empty_iff_exists, exists_imp_distrib, (≠)],
exact assume x xt ht i xfi, ht i x xfi xt xfi
end
end locally_finite
end topological_space
section continuous
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
variables [topological_space α] [topological_space β] [topological_space γ]
/-- A function between topological spaces is continuous if the preimage
of every open set is open. -/
def continuous (f : α → β) := ∀s, is_open s → is_open (f ⁻¹' s)
def continuous_at (f : α → β) (x : α) := tendsto f (nhds x) (nhds (f x))
def continuous_within_at (f : α → β) (s : set α) (x : α) : Prop :=
tendsto f (nhds_within x s) (nhds (f x))
def continuous_on (f : α → β) (s : set α) : Prop := ∀ x ∈ s, continuous_within_at f s x
lemma continuous_id : continuous (id : α → α) :=
assume s h, h
lemma continuous.comp {g : β → γ} {f : α → β} (hg : continuous g) (hf : continuous f) :
continuous (g ∘ f) :=
assume s h, hf _ (hg s h)
lemma continuous.tendsto {f : α → β} (hf : continuous f) (x) :
tendsto f (nhds x) (nhds (f x)) | s :=
show s ∈ nhds (f x) → s ∈ map f (nhds x),
by simp [nhds_sets]; exact
assume t t_subset t_open fx_in_t,
⟨f ⁻¹' t, preimage_mono t_subset, hf t t_open, fx_in_t⟩
lemma continuous_iff_continuous_at {f : α → β} : continuous f ↔ ∀ x, continuous_at f x :=
⟨continuous.tendsto,
assume hf : ∀x, tendsto f (nhds x) (nhds (f x)),
assume s, assume hs : is_open s,
have ∀a, f a ∈ s → s ∈ nhds (f a),
by simp [nhds_sets]; exact assume a ha, ⟨s, subset.refl s, hs, ha⟩,
show is_open (f ⁻¹' s),
by simp [is_open_iff_nhds]; exact assume a ha, hf a (this a ha)⟩
lemma continuous_const {b : β} : continuous (λa:α, b) :=
continuous_iff_continuous_at.mpr $ assume a, tendsto_const_nhds
lemma continuous_iff_is_closed {f : α → β} :
continuous f ↔ (∀s, is_closed s → is_closed (f ⁻¹' s)) :=
⟨assume hf s hs, hf (-s) hs,
assume hf s, by rw [←is_closed_compl_iff, ←is_closed_compl_iff]; exact hf _⟩
lemma continuous_at_iff_ultrafilter {f : α → β} (x) : continuous_at f x ↔
∀ g, is_ultrafilter g → g ≤ nhds x → g.map f ≤ nhds (f x) :=
tendsto_iff_ultrafilter f (nhds x) (nhds (f x))
lemma continuous_iff_ultrafilter {f : α → β} :
continuous f ↔ ∀ x g, is_ultrafilter g → g ≤ nhds x → g.map f ≤ nhds (f x) :=
by simp only [continuous_iff_continuous_at, continuous_at_iff_ultrafilter]
lemma continuous_if {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)}
(hp : ∀a∈frontier {a | p a}, f a = g a) (hf : continuous f) (hg : continuous g) :
continuous (λa, @ite (p a) (h a) β (f a) (g a)) :=
continuous_iff_is_closed.mpr $
assume s hs,
have (λa, ite (p a) (f a) (g a)) ⁻¹' s =
(closure {a | p a} ∩ f ⁻¹' s) ∪ (closure {a | ¬ p a} ∩ g ⁻¹' s),
from set.ext $ assume a,
classical.by_cases
(assume : a ∈ frontier {a | p a},
have hac : a ∈ closure {a | p a}, from this.left,
have hai : a ∈ closure {a | ¬ p a},
from have a ∈ - interior {a | p a}, from this.right, by rwa [←closure_compl] at this,
by by_cases p a; simp [h, hp a this, hac, hai, iff_def] {contextual := tt})
(assume hf : a ∈ - frontier {a | p a},
classical.by_cases
(assume : p a,
have hc : a ∈ closure {a | p a}, from subset_closure this,
have hnc : a ∉ closure {a | ¬ p a},
by show a ∉ closure (- {a | p a}); rw [closure_compl]; simpa [frontier, hc] using hf,
by simp [this, hc, hnc])
(assume : ¬ p a,
have hc : a ∈ closure {a | ¬ p a}, from subset_closure this,
have hnc : a ∉ closure {a | p a},
begin
have hc : a ∈ closure (- {a | p a}), from hc,
simp [closure_compl] at hc,
simpa [frontier, hc] using hf
end,
by simp [this, hc, hnc])),
by rw [this]; exact is_closed_union
(is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hf s hs)
(is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hg s hs)
/- Continuity and partial functions -/
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 (nhds x) (nhds y) :=
begin
split,
{ intros h x y h',
rw [ptendsto'_def],
change ∀ (s : set β), s ∈ (nhds y).sets → pfun.preimage f s ∈ (nhds x).sets,
rw [nhds_sets, nhds_sets],
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 ∈ nhds x,
apply mem_sets_of_superset _ h,
have h' : ∀ s ∈ nhds y, f.preimage s ∈ nhds x,
{ intros s hs,
have : ptendsto' f (nhds x) (nhds y) := hf fxy,
rw ptendsto'_def at this,
exact this s hs },
show f.preimage s ∈ nhds x,
apply h', rw mem_nhds_sets_iff, exact ⟨s, set.subset.refl _, os, ys⟩
end
lemma image_closure_subset_closure_image {f : α → β} {s : set α} (h : continuous f) :
f '' closure s ⊆ closure (f '' s) :=
have ∀ (a : α), nhds a ⊓ principal s ≠ ⊥ → nhds (f a) ⊓ principal (f '' s) ≠ ⊥,
from assume a ha,
have h₁ : ¬ map f (nhds a ⊓ principal s) = ⊥,
by rwa[map_eq_bot_iff],
have h₂ : map f (nhds a ⊓ principal s) ≤ nhds (f a) ⊓ principal (f '' s),
from le_inf
(le_trans (map_mono inf_le_left) $ by rw [continuous_iff_continuous_at] at h; exact h a)
(le_trans (map_mono inf_le_right) $ by simp; exact subset.refl _),
neq_bot_of_le_neq_bot h₁ h₂,
by simp [image_subset_iff, closure_eq_nhds]; assumption
lemma mem_closure [topological_space α] [topological_space β]
{s : set α} {t : set β} {f : α → β} {a : α}
(hf : continuous f) (ha : a ∈ closure s) (ht : ∀a∈s, f a ∈ t) : f a ∈ closure t :=
subset.trans (image_closure_subset_closure_image hf) (closure_mono $ image_subset_iff.2 ht) $
(mem_image_of_mem f ha)
end continuous
|
b949d8d8cd2257bc2c12052b2c6265ec3c1f8d45 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /archive/imo/imo1994_q1.lean | 05f5f08dcd8e43869c183b1c6ad381fe8b0c643e | [
"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 | 3,937 | lean | /-
Copyright (c) 2021 Antoine Labelle. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Antoine Labelle
-/
import algebra.big_operators.basic
import algebra.big_operators.order
import data.fintype.card
import data.finset.sort
import data.fin.interval
import tactic.linarith
import tactic.by_contra
/-!
# IMO 1994 Q1
Let `m` and `n` be two positive integers.
Let `a₁, a₂, ..., aₘ` be `m` different numbers from the set `{1, 2, ..., n}`
such that for any two indices `i` and `j` with `1 ≤ i ≤ j ≤ m` and `aᵢ + aⱼ ≤ n`,
there exists an index `k` such that `aᵢ + aⱼ = aₖ`.
Show that `(a₁+a₂+...+aₘ)/m ≥ (n+1)/2`
# Sketch of solution
We can order the numbers so that `a₁ ≤ a₂ ≤ ... ≤ aₘ`.
The key idea is to pair the numbers in the sum and show that `aᵢ + aₘ₊₁₋ᵢ ≥ n+1`.
Indeed, if we had `aᵢ + aₘ₊₁₋ᵢ ≤ n`, then `a₁ + aₘ₊₁₋ᵢ, a₂ + aₘ₊₁₋ᵢ, ..., aᵢ + aₘ₊₁₋ᵢ`
would be `m` elements of the set of `aᵢ`'s all larger than `aₘ₊₁₋ᵢ`, which is impossible.
-/
open_locale big_operators
open finset
lemma tedious (m : ℕ) (k : fin (m+1)) : m - (m + (m + 1 - ↑k)) % (m + 1) = ↑k :=
begin
cases k with k hk,
rw [nat.lt_succ_iff,le_iff_exists_add] at hk,
rcases hk with ⟨c, rfl⟩,
have : k + c + (k + c + 1 - k) = c + (k + c + 1),
{ simp only [add_assoc, add_tsub_cancel_left], ring_nf, },
rw [fin.coe_mk, this, nat.add_mod_right, nat.mod_eq_of_lt, nat.add_sub_cancel],
linarith
end
theorem imo1994_q1 (n : ℕ) (m : ℕ) (A : finset ℕ) (hm : A.card = m + 1)
(hrange : ∀ a ∈ A, 0 < a ∧ a ≤ n) (hadd : ∀ (a b ∈ A), a + b ≤ n → a + b ∈ A) :
(m+1)*(n+1) ≤ 2*(∑ x in A, x) :=
begin
set a := order_emb_of_fin A hm, -- We sort the elements of `A`
have ha : ∀ i, a i ∈ A := λ i, order_emb_of_fin_mem A hm i,
set rev := equiv.sub_left (fin.last m), -- `i ↦ m-i`
-- We reindex the sum by fin (m+1)
have : ∑ x in A, x = ∑ i : fin (m+1), a i,
{ convert sum_image (λ x hx y hy, (order_embedding.eq_iff_eq a).1),
rw ←coe_inj, simp },
rw this, clear this,
-- The main proof is a simple calculation by rearranging one of the two sums
suffices hpair : ∀ k ∈ univ, a k + a (rev k) ≥ n+1,
calc 2 * ∑ i : fin (m+1), a i
= ∑ i : fin (m+1), a i + ∑ i : fin (m+1), a i : two_mul _
... = ∑ i : fin (m+1), a i + ∑ i : fin (m+1), a (rev i) : by rw equiv.sum_comp rev
... = ∑ i : fin (m+1), (a i + a (rev i)) : sum_add_distrib.symm
... ≥ ∑ i : fin (m+1), (n+1) : sum_le_sum hpair
... = (m+1) * (n+1) : by simp,
-- It remains to prove the key inequality, by contradiction
rintros k -,
by_contra' h : a k + a (rev k) < n + 1,
-- We exhibit `k+1` elements of `A` greater than `a (rev k)`
set f : fin (m+1) ↪ ℕ := ⟨λ i, a i + a (rev k),
begin
apply injective_of_le_imp_le,
intros i j hij,
rwa [add_le_add_iff_right, a.map_rel_iff] at hij,
end⟩,
-- Proof that the `f i` are greater than `a (rev k)` for `i ≤ k`
have hf : map f (Icc 0 k) ⊆ map a.to_embedding (Ioc (rev k) (fin.last m)),
{ intros x hx,
simp at h hx ⊢,
rcases hx with ⟨i, ⟨hi, rfl⟩⟩,
have h1 : a i + a (fin.last m - k) ≤ n,
{ linarith only [h, a.monotone hi.2] },
have h2 : a i + a (fin.last m - k) ∈ A := hadd _ (ha _) _ (ha _) h1,
rw [←mem_coe, ←range_order_emb_of_fin A hm, set.mem_range] at h2,
cases h2 with j hj,
use j,
refine ⟨⟨_, fin.le_last j⟩, hj⟩,
rw [← a.strict_mono.lt_iff_lt, hj],
simpa using (hrange (a i) (ha i)).1 },
-- A set of size `k+1` embed in one of size `k`, which yields a contradiction
have ineq := card_le_of_subset hf,
simp [fin.coe_sub, tedious] at ineq,
contradiction ,
end
|
1f5732ec3d30ce9380ee5e5f87ffc7c0e847d05d | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /tests/lean/run/coesec.lean | 30bea8630ce075af69a54724454409b8d24d85c1 | [
"Apache-2.0"
] | permissive | codyroux/lean | 7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3 | 0cca265db19f7296531e339192e9b9bae4a31f8b | refs/heads/master | 1,610,909,964,159 | 1,407,084,399,000 | 1,416,857,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 195 | lean | inductive func (A B : Type) :=
mk : (A → B) → func A B
section
variables {A B : Type}
definition to_function (F : func A B) : A → B :=
func.rec (λf, f) F
coercion to_function
end
|
9fb85f926c95a4e143ad230aab27730b3bc011f8 | a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7 | /src/category_theory/limits/shapes/biproducts.lean | 9b5b3ef5dce4fa819c0f76f4b882486dbfb130a1 | [
"Apache-2.0"
] | permissive | kmill/mathlib | ea5a007b67ae4e9e18dd50d31d8aa60f650425ee | 1a419a9fea7b959317eddd556e1bb9639f4dcc05 | refs/heads/master | 1,668,578,197,719 | 1,593,629,163,000 | 1,593,629,163,000 | 276,482,939 | 0 | 0 | null | 1,593,637,960,000 | 1,593,637,959,000 | null | UTF-8 | Lean | false | false | 30,925 | 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.epi_mono
import category_theory.limits.shapes.binary_products
import category_theory.preadditive
import algebra.big_operators
/-!
# Biproducts and binary biproducts
We introduce the notion of biproducts and binary biproducts.
These are slightly unusual relative to the other shapes in the library,
as they are simultaneously limits and colimits.
(Zero objects are similar; they are "biterminal".)
We treat first the case of a general category with zero morphisms,
and subsequently the case of a preadditive category.
In a category with zero morphisms, we model the (binary) biproduct of `P Q : C`
using a `binary_bicone`, which has a cone point `X`,
and morphisms `fst : X ⟶ P`, `snd : X ⟶ Q`, `inl : P ⟶ X` and `inr : X ⟶ Q`,
such that `inl ≫ fst = 𝟙 P`, `inl ≫ snd = 0`, `inr ≫ fst = 0`, and `inr ≫ snd = 𝟙 Q`.
Such a `binary_bicone` is a biproduct if the cone is a limit cone, and the cocone is a colimit cocone.
In a preadditive category,
* any `binary_biproduct` satisfies `total : fst ≫ inl + snd ≫ inr = 𝟙 X`
* any `binary_product` is a `binary_biproduct`
* any `binary_coproduct` is a `binary_biproduct`
For biproducts indexed by a `fintype J`, a `bicone` again consists of a cone point `X`
and morphisms `π j : X ⟶ F j` and `ι j : F j ⟶ X` for each `j`,
such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise.
In a preadditive category,
* any `biproduct` satisfies `total : ∑ j : J, biproduct.π f j ≫ biproduct.ι f j = 𝟙 (⨁ f)`
* any `product` is a `biproduct`
* any `coproduct` is a `biproduct`
## Notation
As `⊕` is already taken for the sum of types, we introduce the notation `X ⊞ Y` for
a binary biproduct. We introduce `⨁ f` for the indexed biproduct.
-/
universes v u
open category_theory
open category_theory.functor
namespace category_theory.limits
variables {J : Type v} [decidable_eq J]
variables {C : Type u} [category.{v} C] [has_zero_morphisms C]
/--
A `c : bicone F` is:
* an object `c.X` and
* morphisms `π j : X ⟶ F j` and `ι j : F j ⟶ X` for each `j`,
* such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise.
-/
@[nolint has_inhabited_instance]
structure bicone (F : J → C) :=
(X : C)
(π : Π j, X ⟶ F j)
(ι : Π j, F j ⟶ X)
(ι_π : ∀ j j', ι j ≫ π j' = if h : j = j' then eq_to_hom (congr_arg F h) else 0)
@[simp] lemma bicone_ι_π_self {F : J → C} (B : bicone F) (j : J) : B.ι j ≫ B.π j = 𝟙 (F j) :=
by simpa using B.ι_π j j
@[simp] lemma bicone_ι_π_ne {F : J → C} (B : bicone F) {j j' : J} (h : j ≠ j') :
B.ι j ≫ B.π j' = 0 :=
by simpa [h] using B.ι_π j j'
variables {F : J → C}
namespace bicone
/-- Extract the cone from a bicone. -/
@[simps]
def to_cone (B : bicone F) : cone (discrete.functor F) :=
{ X := B.X,
π := { app := λ j, B.π j }, }
/-- Extract the cocone from a bicone. -/
@[simps]
def to_cocone (B : bicone F) : cocone (discrete.functor F) :=
{ X := B.X,
ι := { app := λ j, B.ι j }, }
end bicone
/--
`has_biproduct F` represents a particular chosen bicone which is
simultaneously a limit and a colimit of the diagram `F`.
-/
class has_biproduct (F : J → C) :=
(bicone : bicone F)
(is_limit : is_limit bicone.to_cone)
(is_colimit : is_colimit bicone.to_cocone)
@[priority 100]
instance has_product_of_has_biproduct [has_biproduct F] : has_limit (discrete.functor F) :=
{ cone := has_biproduct.bicone.to_cone,
is_limit := has_biproduct.is_limit, }
@[priority 100]
instance has_coproduct_of_has_biproduct [has_biproduct F] : has_colimit (discrete.functor F) :=
{ cocone := has_biproduct.bicone.to_cocone,
is_colimit := has_biproduct.is_colimit, }
variables (J C)
/--
`C` has biproducts of shape `J` if we have chosen
a particular limit and a particular colimit, with the same cone points,
of every function `F : J → C`.
-/
class has_biproducts_of_shape :=
(has_biproduct : Π F : J → C, has_biproduct F)
attribute [instance, priority 100] has_biproducts_of_shape.has_biproduct
/-- `has_finite_biproducts C` represents a choice of biproduct for every family of objects in `C`
indexed by a finite type with decidable equality. -/
class has_finite_biproducts :=
(has_biproducts_of_shape : Π (J : Type v) [fintype J] [decidable_eq J],
has_biproducts_of_shape J C)
attribute [instance, priority 100] has_finite_biproducts.has_biproducts_of_shape
variables {J C}
/--
The isomorphism between the specified limit and the specified colimit for
a functor with a bilimit.
-/
def biproduct_iso (F : J → C) [has_biproduct F] :
limits.pi_obj F ≅ limits.sigma_obj F :=
eq_to_iso rfl
end category_theory.limits
namespace category_theory.limits
variables {J : Type v} [decidable_eq J]
variables {C : Type u} [category.{v} C] [has_zero_morphisms C]
/-- `biproduct f` computes the biproduct of a family of elements `f`. (It is defined as an
abbreviation for `limit (discrete.functor f)`, so for most facts about `biproduct f`, you will
just use general facts about limits and colimits.) -/
abbreviation biproduct (f : J → C) [has_biproduct f] : C :=
limit (discrete.functor f)
notation `⨁ ` f:20 := biproduct f
/-- The chosen bicone over a family of elements. -/
abbreviation biproduct.bicone (f : J → C) [has_biproduct f] : bicone f :=
has_biproduct.bicone
/-- The cone coming from the chosen bicone is a limit cone. -/
abbreviation biproduct.is_limit (f : J → C) [has_biproduct f] :
is_limit (biproduct.bicone f).to_cone :=
has_biproduct.is_limit
/-- The cocone coming from the chosen bicone is a colimit cocone. -/
abbreviation biproduct.is_colimit (f : J → C) [has_biproduct f] :
is_colimit (biproduct.bicone f).to_cocone :=
has_biproduct.is_colimit
/-- The projection onto a summand of a biproduct. -/
abbreviation biproduct.π (f : J → C) [has_biproduct f] (b : J) : ⨁ f ⟶ f b :=
limit.π (discrete.functor f) b
/-- The inclusion into a summand of a biproduct. -/
abbreviation biproduct.ι (f : J → C) [has_biproduct f] (b : J) : f b ⟶ ⨁ f :=
colimit.ι (discrete.functor f) b
@[reassoc]
lemma biproduct.ι_π (f : J → C) [has_biproduct f] (j j' : J) :
biproduct.ι f j ≫ biproduct.π f j' = if h : j = j' then eq_to_hom (congr_arg f h) else 0 :=
has_biproduct.bicone.ι_π j j'
@[simp,reassoc]
lemma biproduct.ι_π_self (f : J → C) [has_biproduct f] (j : J) :
biproduct.ι f j ≫ biproduct.π f j = 𝟙 _ :=
by simp [biproduct.ι_π]
@[simp,reassoc]
lemma biproduct.ι_π_ne (f : J → C) [has_biproduct f] {j j' : J} (h : j ≠ j') :
biproduct.ι f j ≫ biproduct.π f j' = 0 :=
by simp [biproduct.ι_π, h]
/-- Given a collection of maps into the summands, we obtain a map into the biproduct. -/
abbreviation biproduct.lift
{f : J → C} [has_biproduct f] {P : C} (p : Π b, P ⟶ f b) : P ⟶ ⨁ f :=
limit.lift _ (fan.mk p)
/-- Given a collection of maps out of the summands, we obtain a map out of the biproduct. -/
abbreviation biproduct.desc
{f : J → C} [has_biproduct f] {P : C} (p : Π b, f b ⟶ P) : ⨁ f ⟶ P :=
colimit.desc _ (cofan.mk p)
/-- Given a collection of maps between corresponding summands of a pair of biproducts
indexed by the same type, we obtain a map between the biproducts. -/
abbreviation biproduct.map [fintype J] {f g : J → C} [has_finite_biproducts C]
(p : Π b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g :=
lim_map (discrete.nat_trans p)
/-- An alternative to `biproduct.map` constructed via colimits.
This construction only exists in order to show it is equal to `biproduct.map`. -/
abbreviation biproduct.map' [fintype J] {f g : J → C} [has_finite_biproducts C]
(p : Π b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g :=
@colim_map _ _ _ _ (discrete.functor f) (discrete.functor g) _ _ (discrete.nat_trans p)
@[ext] lemma biproduct.hom_ext {f : J → C} [has_biproduct f]
{Z : C} (g h : Z ⟶ ⨁ f)
(w : ∀ j, g ≫ biproduct.π f j = h ≫ biproduct.π f j) : g = h :=
limit.hom_ext w
@[ext] lemma biproduct.hom_ext' {f : J → C} [has_biproduct f]
{Z : C} (g h : ⨁ f ⟶ Z)
(w : ∀ j, biproduct.ι f j ≫ g = biproduct.ι f j ≫ h) : g = h :=
colimit.hom_ext w
lemma biproduct.map_eq_map' [fintype J] {f g : J → C} [has_finite_biproducts C]
(p : Π b, f b ⟶ g b) : biproduct.map p = biproduct.map' p :=
begin
ext j j',
simp only [discrete.nat_trans_app, limits.ι_colim_map, limits.lim_map_π, category.assoc],
rw [biproduct.ι_π_assoc, biproduct.ι_π],
split_ifs,
{ subst h, rw [eq_to_hom_refl, category.id_comp], erw category.comp_id, },
{ simp, },
end
instance biproduct.ι_mono (f : J → C) [has_biproduct f]
(b : J) : split_mono (biproduct.ι f b) :=
{ retraction := biproduct.desc $
λ b', if h : b' = b then eq_to_hom (congr_arg f h) else biproduct.ι f b' ≫ biproduct.π f b }
instance biproduct.π_epi (f : J → C) [has_biproduct f]
(b : J) : split_epi (biproduct.π f b) :=
{ section_ := biproduct.lift $
λ b', if h : b = b' then eq_to_hom (congr_arg f h) else biproduct.ι f b ≫ biproduct.π f b' }
-- Because `biproduct.map` is defined in terms of `lim` rather than `colim`,
-- we need to provide additional `simp` lemmas.
@[simp]
lemma biproduct.inl_map [fintype J] {f g : J → C} [has_finite_biproducts C]
(p : Π j, f j ⟶ g j) (j : J) :
biproduct.ι f j ≫ biproduct.map p = p j ≫ biproduct.ι g j :=
begin
rw biproduct.map_eq_map',
simp,
end
variables {C}
/--
A binary bicone for a pair of objects `P Q : C` consists of the cone point `X`,
maps from `X` to both `P` and `Q`, and maps from both `P` and `Q` to `X`,
so that `inl ≫ fst = 𝟙 P`, `inl ≫ snd = 0`, `inr ≫ fst = 0`, and `inr ≫ snd = 𝟙 Q`
-/
@[nolint has_inhabited_instance]
structure binary_bicone (P Q : C) :=
(X : C)
(fst : X ⟶ P)
(snd : X ⟶ Q)
(inl : P ⟶ X)
(inr : Q ⟶ X)
(inl_fst' : inl ≫ fst = 𝟙 P . obviously)
(inl_snd' : inl ≫ snd = 0 . obviously)
(inr_fst' : inr ≫ fst = 0 . obviously)
(inr_snd' : inr ≫ snd = 𝟙 Q . obviously)
restate_axiom binary_bicone.inl_fst'
restate_axiom binary_bicone.inl_snd'
restate_axiom binary_bicone.inr_fst'
restate_axiom binary_bicone.inr_snd'
attribute [simp, reassoc] binary_bicone.inl_fst binary_bicone.inl_snd
binary_bicone.inr_fst binary_bicone.inr_snd
namespace binary_bicone
variables {P Q : C}
/-- Extract the cone from a binary bicone. -/
def to_cone (c : binary_bicone P Q) : cone (pair P Q) :=
binary_fan.mk c.fst c.snd
@[simp]
lemma to_cone_X (c : binary_bicone P Q) :
c.to_cone.X = c.X := rfl
@[simp]
lemma to_cone_π_app_left (c : binary_bicone P Q) :
c.to_cone.π.app (walking_pair.left) = c.fst := rfl
@[simp]
lemma to_cone_π_app_right (c : binary_bicone P Q) :
c.to_cone.π.app (walking_pair.right) = c.snd := rfl
/-- Extract the cocone from a binary bicone. -/
def to_cocone (c : binary_bicone P Q) : cocone (pair P Q) :=
binary_cofan.mk c.inl c.inr
@[simp]
lemma to_cocone_X (c : binary_bicone P Q) :
c.to_cocone.X = c.X := rfl
@[simp]
lemma to_cocone_ι_app_left (c : binary_bicone P Q) :
c.to_cocone.ι.app (walking_pair.left) = c.inl := rfl
@[simp]
lemma to_cocone_ι_app_right (c : binary_bicone P Q) :
c.to_cocone.ι.app (walking_pair.right) = c.inr := rfl
end binary_bicone
namespace bicone
/-- Convert a `bicone` over a function on `walking_pair` to a binary_bicone. -/
@[simps]
def to_binary_bicone {X Y : C} (b : bicone (pair X Y).obj) : binary_bicone X Y :=
{ X := b.X,
fst := b.π walking_pair.left,
snd := b.π walking_pair.right,
inl := b.ι walking_pair.left,
inr := b.ι walking_pair.right,
inl_fst' := by { simp [bicone.ι_π], refl, },
inr_fst' := by simp [bicone.ι_π],
inl_snd' := by simp [bicone.ι_π],
inr_snd' := by { simp [bicone.ι_π], refl, }, }
/--
If the cone obtained from a bicone over `pair X Y` is a limit cone,
so is the cone obtained by converting that bicone to a binary_bicone, then to a cone.
-/
def to_binary_bicone_is_limit {X Y : C} {b : bicone (pair X Y).obj}
(c : is_limit (b.to_cone)) :
is_limit (b.to_binary_bicone.to_cone) :=
{ lift := λ s, c.lift s,
fac' := λ s j, by { cases j; erw c.fac, },
uniq' := λ s m w,
begin
apply c.uniq s,
rintro (⟨⟩|⟨⟩),
exact w walking_pair.left,
exact w walking_pair.right,
end, }
/--
If the cocone obtained from a bicone over `pair X Y` is a colimit cocone,
so is the cocone obtained by converting that bicone to a binary_bicone, then to a cocone.
-/
def to_binary_bicone_is_colimit {X Y : C} {b : bicone (pair X Y).obj}
(c : is_colimit (b.to_cocone)) :
is_colimit (b.to_binary_bicone.to_cocone) :=
{ desc := λ s, c.desc s,
fac' := λ s j, by { cases j; erw c.fac, },
uniq' := λ s m w,
begin
apply c.uniq s,
rintro (⟨⟩|⟨⟩),
exact w walking_pair.left,
exact w walking_pair.right,
end, }
end bicone
/--
`has_binary_biproduct P Q` represents a particular chosen bicone which is
simultaneously a limit and a colimit of the diagram `pair P Q`.
-/
class has_binary_biproduct (P Q : C) :=
(bicone : binary_bicone P Q)
(is_limit : is_limit bicone.to_cone)
(is_colimit : is_colimit bicone.to_cocone)
section
variable (C)
/--
`has_binary_biproducts C` represents a particular chosen bicone which is
simultaneously a limit and a colimit of the diagram `pair P Q`, for every `P Q : C`.
-/
class has_binary_biproducts :=
(has_binary_biproduct : Π (P Q : C), has_binary_biproduct P Q)
attribute [instance, priority 100] has_binary_biproducts.has_binary_biproduct
/--
A category with finite biproducts has binary biproducts.
This is not an instance as typically in concrete categories there will be
an alternative construction with nicer definitional properties.
-/
def has_binary_biproducts_of_finite_biproducts [has_finite_biproducts C] :
has_binary_biproducts C :=
{ has_binary_biproduct := λ P Q,
{ bicone := (biproduct.bicone (pair P Q).obj).to_binary_bicone,
is_limit := bicone.to_binary_bicone_is_limit (biproduct.is_limit _),
is_colimit := bicone.to_binary_bicone_is_colimit (biproduct.is_colimit _) } }
end
variables {P Q : C}
instance has_binary_biproduct.has_limit_pair [has_binary_biproduct P Q] :
has_limit (pair P Q) :=
{ cone := has_binary_biproduct.bicone.to_cone,
is_limit := has_binary_biproduct.is_limit, }
instance has_binary_biproduct.has_colimit_pair [has_binary_biproduct P Q] :
has_colimit (pair P Q) :=
{ cocone := has_binary_biproduct.bicone.to_cocone,
is_colimit := has_binary_biproduct.is_colimit, }
@[priority 100]
instance has_limits_of_shape_walking_pair [has_binary_biproducts C] :
has_limits_of_shape (discrete walking_pair) C :=
{ has_limit := λ F, has_limit_of_iso (diagram_iso_pair F).symm }
@[priority 100]
instance has_colimits_of_shape_walking_pair [has_binary_biproducts C] :
has_colimits_of_shape (discrete walking_pair) C :=
{ has_colimit := λ F, has_colimit_of_iso (diagram_iso_pair F) }
@[priority 100]
instance has_binary_products_of_has_binary_biproducts [has_binary_biproducts C] :
has_binary_products C :=
⟨by apply_instance⟩
@[priority 100]
instance has_binary_coproducts_of_has_binary_biproducts [has_binary_biproducts C] :
has_binary_coproducts C :=
⟨by apply_instance⟩
/--
The isomorphism between the specified binary product and the specified binary coproduct for
a pair for a binary biproduct.
-/
def biprod_iso (X Y : C) [has_binary_biproduct X Y] :
limits.prod X Y ≅ limits.coprod X Y :=
eq_to_iso rfl
/-- The chosen biproduct of a pair of objects. -/
abbreviation biprod (X Y : C) [has_binary_biproduct X Y] := limit (pair X Y)
notation X ` ⊞ `:20 Y:20 := biprod X Y
/-- The projection onto the first summand of a binary biproduct. -/
abbreviation biprod.fst {X Y : C} [has_binary_biproduct X Y] : X ⊞ Y ⟶ X :=
limit.π (pair X Y) walking_pair.left
/-- The projection onto the second summand of a binary biproduct. -/
abbreviation biprod.snd {X Y : C} [has_binary_biproduct X Y] : X ⊞ Y ⟶ Y :=
limit.π (pair X Y) walking_pair.right
/-- The inclusion into the first summand of a binary biproduct. -/
abbreviation biprod.inl {X Y : C} [has_binary_biproduct X Y] : X ⟶ X ⊞ Y :=
colimit.ι (pair X Y) walking_pair.left
/-- The inclusion into the second summand of a binary biproduct. -/
abbreviation biprod.inr {X Y : C} [has_binary_biproduct X Y] : Y ⟶ X ⊞ Y :=
colimit.ι (pair X Y) walking_pair.right
@[simp,reassoc]
lemma biprod.inl_fst {X Y : C} [has_binary_biproduct X Y] :
(biprod.inl : X ⟶ X ⊞ Y) ≫ (biprod.fst : X ⊞ Y ⟶ X) = 𝟙 X :=
has_binary_biproduct.bicone.inl_fst
@[simp,reassoc]
lemma biprod.inl_snd {X Y : C} [has_binary_biproduct X Y] :
(biprod.inl : X ⟶ X ⊞ Y) ≫ (biprod.snd : X ⊞ Y ⟶ Y) = 0 :=
has_binary_biproduct.bicone.inl_snd
@[simp,reassoc]
lemma biprod.inr_fst {X Y : C} [has_binary_biproduct X Y] :
(biprod.inr : Y ⟶ X ⊞ Y) ≫ (biprod.fst : X ⊞ Y ⟶ X) = 0 :=
has_binary_biproduct.bicone.inr_fst
@[simp,reassoc]
lemma biprod.inr_snd {X Y : C} [has_binary_biproduct X Y] :
(biprod.inr : Y ⟶ X ⊞ Y) ≫ (biprod.snd : X ⊞ Y ⟶ Y) = 𝟙 Y :=
has_binary_biproduct.bicone.inr_snd
/-- Given a pair of maps into the summands of a binary biproduct,
we obtain a map into the binary biproduct. -/
abbreviation biprod.lift {W X Y : C} [has_binary_biproduct X Y] (f : W ⟶ X) (g : W ⟶ Y) :
W ⟶ X ⊞ Y :=
limit.lift _ (binary_fan.mk f g)
/-- Given a pair of maps out of the summands of a binary biproduct,
we obtain a map out of the binary biproduct. -/
abbreviation biprod.desc {W X Y : C} [has_binary_biproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) :
X ⊞ Y ⟶ W :=
colimit.desc _ (binary_cofan.mk f g)
/-- Given a pair of maps between the summands of a pair of binary biproducts,
we obtain a map between the binary biproducts. -/
abbreviation biprod.map {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) : W ⊞ X ⟶ Y ⊞ Z :=
lim_map (@map_pair _ _ (pair W X) (pair Y Z) f g)
/-- Given a pair of isomorphisms between the summands of a pair of binary biproducts,
we obtain an isomorphism between the binary biproducts. -/
@[simps]
def biprod.map_iso {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ≅ Y) (g : X ≅ Z) : W ⊞ X ≅ Y ⊞ Z :=
{ hom := biprod.map f.hom g.hom,
inv := biprod.map f.inv g.inv, }
/-- An alternative to `biprod.map` constructed via colimits.
This construction only exists in order to show it is equal to `biprod.map`. -/
abbreviation biprod.map' {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) : W ⊞ X ⟶ Y ⊞ Z :=
colim_map (@map_pair _ _ (pair W X) (pair Y Z) f g)
@[ext] lemma biprod.hom_ext {X Y Z : C} [has_binary_biproduct X Y] (f g : Z ⟶ X ⊞ Y)
(h₀ : f ≫ biprod.fst = g ≫ biprod.fst) (h₁ : f ≫ biprod.snd = g ≫ biprod.snd) : f = g :=
binary_fan.is_limit.hom_ext has_binary_biproduct.is_limit h₀ h₁
@[ext] lemma biprod.hom_ext' {X Y Z : C} [has_binary_biproduct X Y] (f g : X ⊞ Y ⟶ Z)
(h₀ : biprod.inl ≫ f = biprod.inl ≫ g) (h₁ : biprod.inr ≫ f = biprod.inr ≫ g) : f = g :=
binary_cofan.is_colimit.hom_ext has_binary_biproduct.is_colimit h₀ h₁
lemma biprod.map_eq_map' {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) : biprod.map f g = biprod.map' f g :=
begin
ext,
{ simp only [map_pair_left, ι_colim_map, lim_map_π, biprod.inl_fst_assoc, category.assoc],
erw [biprod.inl_fst, category.comp_id], },
{ simp only [map_pair_left, ι_colim_map, lim_map_π, has_zero_morphisms.zero_comp,
biprod.inl_snd_assoc, category.assoc],
erw [biprod.inl_snd], simp, },
{ simp only [map_pair_right, biprod.inr_fst_assoc, ι_colim_map, lim_map_π,
has_zero_morphisms.zero_comp, category.assoc],
erw [biprod.inr_fst], simp, },
{ simp only [map_pair_right, ι_colim_map, lim_map_π, biprod.inr_snd_assoc, category.assoc],
erw [biprod.inr_snd, category.comp_id], },
end
instance biprod.inl_mono {X Y : C} [has_binary_biproduct X Y] :
split_mono (biprod.inl : X ⟶ X ⊞ Y) :=
{ retraction := biprod.desc (𝟙 X) (biprod.inr ≫ biprod.fst) }
instance biprod.inr_mono {X Y : C} [has_binary_biproduct X Y] :
split_mono (biprod.inr : Y ⟶ X ⊞ Y) :=
{ retraction := biprod.desc (biprod.inl ≫ biprod.snd) (𝟙 Y)}
instance biprod.fst_epi {X Y : C} [has_binary_biproduct X Y] :
split_epi (biprod.fst : X ⊞ Y ⟶ X) :=
{ section_ := biprod.lift (𝟙 X) (biprod.inl ≫ biprod.snd) }
instance biprod.snd_epi {X Y : C} [has_binary_biproduct X Y] :
split_epi (biprod.snd : X ⊞ Y ⟶ Y) :=
{ section_ := biprod.lift (biprod.inr ≫ biprod.fst) (𝟙 Y) }
@[simp,reassoc]
lemma biprod.map_fst {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) :
biprod.map f g ≫ biprod.fst = biprod.fst ≫ f :=
by simp
@[simp,reassoc]
lemma biprod.map_snd {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) :
biprod.map f g ≫ biprod.snd = biprod.snd ≫ g :=
by simp
-- Because `biprod.map` is defined in terms of `lim` rather than `colim`,
-- we need to provide additional `simp` lemmas.
@[simp,reassoc]
lemma biprod.inl_map {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) :
biprod.inl ≫ biprod.map f g = f ≫ biprod.inl :=
begin
rw biprod.map_eq_map',
simp,
end
@[simp,reassoc]
lemma biprod.inr_map {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) :
biprod.inr ≫ biprod.map f g = g ≫ biprod.inr :=
begin
rw biprod.map_eq_map',
simp,
end
section
variables [has_binary_biproducts C]
/-- The braiding isomorphism which swaps a binary biproduct. -/
@[simps] def biprod.braiding (P Q : C) : P ⊞ Q ≅ Q ⊞ P :=
{ hom := biprod.lift biprod.snd biprod.fst,
inv := biprod.lift biprod.snd biprod.fst }
/--
An alternative formula for the braiding isomorphism which swaps a binary biproduct,
using the fact that the biproduct is a coproduct.
-/
@[simps]
def biprod.braiding' (P Q : C) : P ⊞ Q ≅ Q ⊞ P :=
{ hom := biprod.desc biprod.inr biprod.inl,
inv := biprod.desc biprod.inr biprod.inl }
lemma biprod.braiding'_eq_braiding {P Q : C} :
biprod.braiding' P Q = biprod.braiding P Q :=
by tidy
/-- The braiding isomorphism can be passed through a map by swapping the order. -/
@[reassoc] lemma biprod.braid_natural {W X Y Z : C} (f : X ⟶ Y) (g : Z ⟶ W) :
biprod.map f g ≫ (biprod.braiding _ _).hom = (biprod.braiding _ _).hom ≫ biprod.map g f :=
by tidy
@[reassoc] lemma biprod.braiding_map_braiding {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) :
(biprod.braiding X W).hom ≫ biprod.map f g ≫ (biprod.braiding Y Z).hom = biprod.map g f :=
by tidy
@[simp, reassoc] lemma biprod.symmetry' (P Q : C) :
biprod.lift biprod.snd biprod.fst ≫ biprod.lift biprod.snd biprod.fst = 𝟙 (P ⊞ Q) :=
by tidy
/-- The braiding isomorphism is symmetric. -/
@[reassoc] lemma biprod.symmetry (P Q : C) :
(biprod.braiding P Q).hom ≫ (biprod.braiding Q P).hom = 𝟙 _ :=
by simp
end
-- TODO:
-- If someone is interested, they could provide the constructions:
-- has_binary_biproducts ↔ has_finite_biproducts
end category_theory.limits
namespace category_theory.limits
section preadditive
variables {C : Type u} [category.{v} C] [preadditive C]
variables {J : Type v} [fintype J] [decidable_eq J]
open category_theory.preadditive
open_locale big_operators
/--
In a preadditive category, we can construct a biproduct for `f : J → C` from
any bicone `b` for `f` satisfying `total : ∑ j : J, b.π j ≫ b.ι j = 𝟙 b.X`.
(That is, such a bicone is a limit cone and a colimit cocone.)
-/
def has_biproduct_of_total {f : J → C} (b : bicone f) (total : ∑ j : J, b.π j ≫ b.ι j = 𝟙 b.X) :
has_biproduct f :=
{ bicone := b,
is_limit :=
{ lift := λ s, ∑ j, s.π.app j ≫ b.ι j,
uniq' := λ s m h,
begin
erw [←category.comp_id m, ←total, comp_sum],
apply finset.sum_congr rfl,
intros j m,
erw [reassoc_of (h j)],
end,
fac' := λ s j,
begin
simp only [sum_comp, category.assoc, bicone.to_cone_π_app, b.ι_π, comp_dite],
dsimp, simp,
end },
is_colimit :=
{ desc := λ s, ∑ j, b.π j ≫ s.ι.app j,
uniq' := λ s m h,
begin
erw [←category.id_comp m, ←total, sum_comp],
apply finset.sum_congr rfl,
intros j m,
erw [category.assoc, h],
end,
fac' := λ s j,
begin
simp only [comp_sum, ←category.assoc, bicone.to_cocone_ι_app, b.ι_π, dite_comp],
dsimp, simp,
end } }
/-- In a preadditive category, if the product over `f : J → C` exists,
then the biproduct over `f` exists. -/
def has_biproduct.of_has_product (f : J → C) [has_product f] :
has_biproduct f :=
has_biproduct_of_total
{ X := pi_obj f,
π := limits.pi.π f,
ι := λ j, pi.lift (λ j', if h : j = j' then eq_to_hom (congr_arg f h) else 0),
ι_π := λ j j', by simp, }
(by { ext, simp [sum_comp, comp_dite] })
/-- In a preadditive category, if the coproduct over `f : J → C` exists,
then the biproduct over `f` exists. -/
def has_biproduct.of_has_coproduct (f : J → C) [has_coproduct f] :
has_biproduct f :=
has_biproduct_of_total
{ X := sigma_obj f,
π := λ j, sigma.desc (λ j', if h : j' = j then eq_to_hom (congr_arg f h) else 0),
ι := limits.sigma.ι f,
ι_π := λ j j', by simp, }
begin
ext,
simp only [comp_sum, limits.cofan.mk_π_app, limits.colimit.ι_desc_assoc, eq_self_iff_true,
limits.colimit.ι_desc, category.comp_id],
dsimp,
simp only [dite_comp, finset.sum_dite_eq, finset.mem_univ, if_true, category.id_comp,
eq_to_hom_refl, limits.has_zero_morphisms.zero_comp],
end
section
variables {f : J → C} [has_biproduct f]
/--
In any preadditive category, any biproduct satsifies
`∑ j : J, biproduct.π f j ≫ biproduct.ι f j = 𝟙 (⨁ f)`
-/
@[simp] lemma biproduct.total : ∑ j : J, biproduct.π f j ≫ biproduct.ι f j = 𝟙 (⨁ f) :=
begin
ext j j',
simp [comp_sum, sum_comp, biproduct.ι_π, comp_dite, dite_comp],
end
lemma biproduct.lift_eq {T : C} {g : Π j, T ⟶ f j} :
biproduct.lift g = ∑ j, g j ≫ biproduct.ι f j :=
begin
ext j,
simp [sum_comp, biproduct.ι_π, comp_dite],
end
lemma biproduct.desc_eq {T : C} {g : Π j, f j ⟶ T} :
biproduct.desc g = ∑ j, biproduct.π f j ≫ g j :=
begin
ext j,
simp [comp_sum, biproduct.ι_π_assoc, dite_comp],
end
@[simp, reassoc] lemma biproduct.lift_desc {T U : C} {g : Π j, T ⟶ f j} {h : Π j, f j ⟶ U} :
biproduct.lift g ≫ biproduct.desc h = ∑ j : J, g j ≫ h j :=
by simp [biproduct.lift_eq, biproduct.desc_eq, comp_sum, sum_comp, biproduct.ι_π_assoc,
comp_dite, dite_comp]
lemma biproduct.map_eq [has_finite_biproducts C] {f g : J → C} {h : Π j, f j ⟶ g j} :
biproduct.map h = ∑ j : J, biproduct.π f j ≫ h j ≫ biproduct.ι g j :=
begin
ext,
simp [biproduct.ι_π, biproduct.ι_π_assoc, comp_sum, sum_comp, comp_dite, dite_comp],
end
end
/--
In a preadditive category, we can construct a binary biproduct for `X Y : C` from
any binary bicone `b` satisfying `total : b.fst ≫ b.inl + b.snd ≫ b.inr = 𝟙 b.X`.
(That is, such a bicone is a limit cone and a colimit cocone.)
-/
def has_binary_biproduct_of_total {X Y : C} (b : binary_bicone X Y)
(total : b.fst ≫ b.inl + b.snd ≫ b.inr = 𝟙 b.X) :
has_binary_biproduct X Y :=
{ bicone := b,
is_limit :=
{ lift := λ s, binary_fan.fst s ≫ b.inl +
binary_fan.snd s ≫ b.inr,
uniq' := λ s m h, by erw [←category.comp_id m, ←total,
comp_add, reassoc_of (h walking_pair.left), reassoc_of (h walking_pair.right)],
fac' := λ s j, by cases j; simp, },
is_colimit :=
{ desc := λ s, b.fst ≫ binary_cofan.inl s +
b.snd ≫ binary_cofan.inr s,
uniq' := λ s m h, by erw [←category.id_comp m, ←total,
add_comp, category.assoc, category.assoc, h walking_pair.left, h walking_pair.right],
fac' := λ s j, by cases j; simp, } }
/-- In a preadditive category, if the product of `X` and `Y` exists, then the
binary biproduct of `X` and `Y` exists. -/
def has_binary_biproduct.of_has_binary_product (X Y : C) [has_binary_product X Y] :
has_binary_biproduct X Y :=
has_binary_biproduct_of_total
{ X := X ⨯ Y,
fst := category_theory.limits.prod.fst,
snd := category_theory.limits.prod.snd,
inl := prod.lift (𝟙 X) 0,
inr := prod.lift 0 (𝟙 Y) }
begin
ext; simp [add_comp],
end
/-- In a preadditive category, if all binary products exist,
then the all binary biproducts exist. -/
def has_binary_biproducts.of_has_binary_products [has_binary_products C] :
has_binary_biproducts C :=
{ has_binary_biproduct := λ X Y, has_binary_biproduct.of_has_binary_product X Y, }
/-- In a preadditive category, if the product of `X` and `Y` exists, then the
binary biproduct of `X` and `Y` exists. -/
def has_binary_biproduct.of_has_binary_coproduct (X Y : C) [has_binary_coproduct X Y] :
has_binary_biproduct X Y :=
has_binary_biproduct_of_total
{ X := X ⨿ Y,
fst := coprod.desc (𝟙 X) 0,
snd := coprod.desc 0 (𝟙 Y),
inl := category_theory.limits.coprod.inl,
inr := category_theory.limits.coprod.inr }
begin
ext; simp [add_comp],
end
/-- In a preadditive category, if all binary coproducts exist,
then the all binary biproducts exist. -/
def has_binary_biproducts.of_has_binary_coproducts [has_binary_coproducts C] :
has_binary_biproducts C :=
{ has_binary_biproduct := λ X Y, has_binary_biproduct.of_has_binary_coproduct X Y, }
section
variables {X Y : C} [has_binary_biproduct X Y]
/--
In any preadditive category, any binary biproduct satsifies
`biprod.fst ≫ biprod.inl + biprod.snd ≫ biprod.inr = 𝟙 (X ⊞ Y)`.
-/
@[simp] lemma biprod.total : biprod.fst ≫ biprod.inl + biprod.snd ≫ biprod.inr = 𝟙 (X ⊞ Y) :=
begin
ext; simp [add_comp],
end
lemma biprod.lift_eq {T : C} {f : T ⟶ X} {g : T ⟶ Y} :
biprod.lift f g = f ≫ biprod.inl + g ≫ biprod.inr :=
begin
ext; simp [add_comp],
end
lemma biprod.desc_eq {T : C} {f : X ⟶ T} {g : Y ⟶ T} :
biprod.desc f g = biprod.fst ≫ f + biprod.snd ≫ g :=
begin
ext; simp [add_comp],
end
@[simp, reassoc] lemma biprod.lift_desc {T U : C} {f : T ⟶ X} {g : T ⟶ Y} {h : X ⟶ U} {i : Y ⟶ U} :
biprod.lift f g ≫ biprod.desc h i = f ≫ h + g ≫ i :=
by simp [biprod.lift_eq, biprod.desc_eq]
lemma biprod.map_eq [has_binary_biproducts C] {W X Y Z : C} {f : W ⟶ Y} {g : X ⟶ Z} :
biprod.map f g = biprod.fst ≫ f ≫ biprod.inl + biprod.snd ≫ g ≫ biprod.inr :=
by apply biprod.hom_ext; apply biprod.hom_ext'; simp
end
end preadditive
end category_theory.limits
|
f14c1b10a3b2b1a987077e652222f95aca468347 | 1437b3495ef9020d5413178aa33c0a625f15f15f | /meta/rb_map.lean | 986eaa3e65248f83e87be7224171f1ca14c00dae | [
"Apache-2.0"
] | permissive | jean002/mathlib | c66bbb2d9fdc9c03ae07f869acac7ddbfce67a30 | dc6c38a765799c99c4d9c8d5207d9e6c9e0e2cfd | refs/heads/master | 1,587,027,806,375 | 1,547,306,358,000 | 1,547,306,358,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,811 | lean | /-
Copyright (c) 2018 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Robert Y. Lewis
Additional operations on native rb_maps and rb_sets.
-/
import data.option.defs
namespace native
namespace rb_set
meta def filter {key} (s : rb_set key) (P : key → bool) : rb_set key :=
s.fold s (λ a m, if P a then m else m.erase a)
meta def mfilter {m} [monad m] {key} (s : rb_set key) (P : key → m bool) : m (rb_set key) :=
s.fold (pure s) (λ a m,
do x ← m,
mcond (P a) (pure x) (pure $ x.erase a))
meta def union {key} (s t : rb_set key) : rb_set key :=
s.fold t (λ a t, t.insert a)
end rb_set
namespace rb_map
meta def find_def {α β} [has_lt α] [decidable_rel ((<) : α → α → Prop)]
(x : β) (m : rb_map α β) (k : α) :=
(m.find k).get_or_else x
meta def insert_cons {α β} [has_lt α] [decidable_rel ((<) : α → α → Prop)]
(k : α) (x : β) (m : rb_map α (list β)) : rb_map α (list β) :=
m.insert k (x :: m.find_def [] k)
meta def ifind {α β} [inhabited β] (m : rb_map α β) (a : α) : β :=
(m.find a).iget
meta def zfind {α β} [has_zero β] (m : rb_map α β) (a : α) : β :=
(m.find a).get_or_else 0
meta def add {α β} [has_add β] [has_zero β] [decidable_eq β] (m1 m2 : rb_map α β) : rb_map α β :=
m1.fold m2
(λ n v m,
let nv := v + m2.zfind n in
if nv = 0 then m.erase n else m.insert n nv)
variables {m : Type → Type*} [monad m]
open function
meta def mfilter {key val} [has_lt key] [decidable_rel ((<) : key → key → Prop)]
(s : rb_map key val) (P : key → val → m bool) : m (rb_map.{0 0} key val) :=
rb_map.of_list <$> s.to_list.mfilter (uncurry P)
meta def mmap {key val val'} [has_lt key] [decidable_rel ((<) : key → key → Prop)]
(s : rb_map key val) (f : val → m val') : m (rb_map.{0 0} key val') :=
rb_map.of_list <$> s.to_list.mmap (λ ⟨a,b⟩, prod.mk a <$> f b)
meta def scale {α β} [has_lt α] [decidable_rel ((<) : α → α → Prop)] [has_mul β] (b : β) (m : rb_map α β) : rb_map α β :=
m.map ((*) b)
section
open format prod
variables {key : Type} {data : Type} [has_to_tactic_format key] [has_to_tactic_format data]
private meta def pp_key_data (k : key) (d : data) (first : bool) : tactic format :=
do fk ← tactic.pp k, fd ← tactic.pp d, return $
(if first then to_fmt "" else to_fmt "," ++ line) ++ fk ++ space ++ to_fmt "←" ++ space ++ fd
meta instance : has_to_tactic_format (rb_map key data) :=
⟨λ m, do
(fmt, _) ← fold m (return (to_fmt "", tt))
(λ k d p, do p ← p, pkd ← pp_key_data k d (snd p), return (fst p ++ pkd, ff)),
return $ group $ to_fmt "⟨" ++ nest 1 fmt ++ to_fmt "⟩"⟩
end
end rb_map
end native
|
4aef56bc83d9198848cb012b8d4c5a78f27e2883 | 6b2a480f27775cba4f3ae191b1c1387a29de586e | /group_rep1/programmation/contant_group_scheme.lean | e3ab68f745353aaf4f390d6c35065d3468b6596e | [] | no_license | Or7ando/group_representation | a681de2e19d1930a1e1be573d6735a2f0b8356cb | 9b576984f17764ebf26c8caa2a542d248f1b50d2 | refs/heads/master | 1,662,413,107,324 | 1,590,302,389,000 | 1,590,302,389,000 | 258,130,829 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 1,458 | lean | import ring_theory.algebra
import ring_theory.ideals
/-!
Galois algebra : B : Algebra A.
G →* Aut (B / A)
est-ce que le groupe des automorphismes d'algebre est ok !
`A →ₐ[R] B`
-/
namespace prio
variables (k : Type)[comm_ring k] (B : Type)[comm_ring B][algebra k B](G : Type)[group G][fintype G] [mul_action G B]
instance coe_base_to_algebra : has_coe(k)B := ⟨λ r, algebra_map k B r ⟩
end prio
class Galois_algebra (k : Type)[comm_ring k] (B : Type)[comm_ring B][algebra k B](G : Type)[group G][fintype G] [mul_action G B]
:=
(G_mul' : ∀ g : G, ∀ x y : B, g • (x * y) = g • x * g • y)
(G_add' : ∀ g : G, ∀ x y : B, g • (x+y) = g • x + g • y)
(G_smul' : ∀ g : G, ∀ x : B, ∀ r : k, g • (r • x) = r • (g • x) )
(invariant' : ∀ b : B, ( (∀ g : G, g • b = b) → (∃ a : k, b = a )))
/-!
* ⟨ b - σ b | σ ∈ G ⟩
*
-/
variables {k : Type}[comm_ring k] {B : Type}[comm_ring B][algebra k B]
{G : Type}[group G][fintype G] [mul_action G B]
[Galois_algebra k B G]
#check Galois_algebra
example (g : G ) (b1 b2 : B): g • (b1+b2) = g • (b1) + g • b2 := Galois_algebra.G_add'
def 𝒥 : G → ideal B := λ g : G, ideal.span $ set.image (λ b, b - (g ⊚ b)) set.univ
def separate_action (Δ : (Galois_algebra R B G)) : Prop := ∀ g : G, g ≠ 1 → 𝒥 Δ g = ⊤
-- other has_mul_action ? st g • (a * b) = g • a * g • b, |
1022763c3a8f47460ef10723af613501322220cb | 618003631150032a5676f229d13a079ac875ff77 | /src/algebraic_geometry/stalks.lean | 780f921034fc2e3900923cba603b7bfff35c0377 | [
"Apache-2.0"
] | permissive | awainverse/mathlib | 939b68c8486df66cfda64d327ad3d9165248c777 | ea76bd8f3ca0a8bf0a166a06a475b10663dec44a | refs/heads/master | 1,659,592,962,036 | 1,590,987,592,000 | 1,590,987,592,000 | 268,436,019 | 1 | 0 | Apache-2.0 | 1,590,990,500,000 | 1,590,990,500,000 | null | UTF-8 | Lean | false | false | 2,094 | 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 algebraic_geometry.presheafed_space
import topology.sheaves.stalks
/-!
# Stalks for presheaved spaces
This file lifts constructions of stalks and pushforwards of stalks to work with
the category of presheafed spaces.
-/
universes v u v' u'
open category_theory
open category_theory.limits category_theory.category category_theory.functor
open algebraic_geometry
open topological_space
variables {C : Type u} [𝒞 : category.{v} C] [has_colimits.{v} C]
include 𝒞
local attribute [tidy] tactic.op_induction'
open Top.presheaf
namespace algebraic_geometry.PresheafedSpace
def stalk (X : PresheafedSpace.{v} C) (x : X) : C := X.𝒪.stalk x
def stalk_map {X Y : PresheafedSpace.{v} C} (α : X ⟶ Y) (x : X) : Y.stalk (α x) ⟶ X.stalk x :=
(stalk_functor C (α x)).map (α.c) ≫ X.𝒪.stalk_pushforward C α x
namespace stalk_map
@[simp] lemma id (X : PresheafedSpace.{v} C) (x : X) : stalk_map (𝟙 X) x = 𝟙 (X.stalk x) :=
begin
dsimp [stalk_map],
simp only [stalk_pushforward.id],
rw [←map_comp],
convert (stalk_functor C x).map_id X.𝒪,
tidy,
end
-- TODO understand why this proof is still gross (i.e. requires using `erw`)
@[simp] lemma comp {X Y Z : PresheafedSpace.{v} C} (α : X ⟶ Y) (β : Y ⟶ Z) (x : X) :
stalk_map (α ≫ β) x =
(stalk_map β (α x) : Z.stalk (β (α x)) ⟶ Y.stalk (α x)) ≫
(stalk_map α x : Y.stalk (α x) ⟶ X.stalk x) :=
begin
dsimp [stalk_map, stalk_functor, stalk_pushforward],
ext U,
op_induction U,
cases U,
simp only [colimit.ι_map_assoc, colimit.ι_pre_assoc, colimit.ι_pre,
whisker_left_app, whisker_right_app,
assoc, id_comp, map_id, map_comp],
dsimp,
simp only [map_id, assoc, pushforward.comp_inv_app],
-- FIXME Why doesn't simp do this:
erw [category_theory.functor.map_id],
erw [category_theory.functor.map_id],
erw [id_comp, id_comp, id_comp],
end
end stalk_map
end algebraic_geometry.PresheafedSpace
|
6bd3029599de6eab7dc46089985ad0bdf4c9b9ff | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/category_theory/subobject/limits.lean | 3bc6056120a1c2a30673d447df6b3b9174412080 | [
"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 | 16,314 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta, Scott Morrison
-/
import category_theory.subobject.lattice
/-!
# Specific subobjects
We define `equalizer_subobject`, `kernel_subobject` and `image_subobject`, which are the subobjects
represented by the equalizer, kernel and image of (a pair of) morphism(s) and provide conditions
for `P.factors f`, where `P` is one of these special subobjects.
TODO: Add conditions for when `P` is a pullback subobject.
TODO: an iff characterisation of `(image_subobject f).factors h`
-/
universes v u
noncomputable theory
open category_theory category_theory.category category_theory.limits category_theory.subobject
opposite
variables {C : Type u} [category.{v} C] {X Y Z : C}
namespace category_theory
namespace limits
section equalizer
variables (f g : X ⟶ Y) [has_equalizer f g]
/-- The equalizer of morphisms `f g : X ⟶ Y` as a `subobject X`. -/
abbreviation equalizer_subobject : subobject X :=
subobject.mk (equalizer.ι f g)
/-- The underlying object of `equalizer_subobject f g` is (up to isomorphism!)
the same as the chosen object `equalizer f g`. -/
def equalizer_subobject_iso : (equalizer_subobject f g : C) ≅ equalizer f g :=
subobject.underlying_iso (equalizer.ι f g)
@[simp, reassoc]
lemma equalizer_subobject_arrow :
(equalizer_subobject_iso f g).hom ≫ equalizer.ι f g = (equalizer_subobject f g).arrow :=
by simp [equalizer_subobject_iso]
@[simp, reassoc]
lemma equalizer_subobject_arrow' :
(equalizer_subobject_iso f g).inv ≫ (equalizer_subobject f g).arrow = equalizer.ι f g :=
by simp [equalizer_subobject_iso]
@[reassoc]
lemma equalizer_subobject_arrow_comp :
(equalizer_subobject f g).arrow ≫ f = (equalizer_subobject f g).arrow ≫ g :=
by rw [←equalizer_subobject_arrow, category.assoc, category.assoc, equalizer.condition]
lemma equalizer_subobject_factors {W : C} (h : W ⟶ X) (w : h ≫ f = h ≫ g) :
(equalizer_subobject f g).factors h :=
⟨equalizer.lift h w, by simp⟩
lemma equalizer_subobject_factors_iff {W : C} (h : W ⟶ X) :
(equalizer_subobject f g).factors h ↔ h ≫ f = h ≫ g :=
⟨λ w, by rw [←subobject.factor_thru_arrow _ _ w, category.assoc,
equalizer_subobject_arrow_comp, category.assoc],
equalizer_subobject_factors f g h⟩
end equalizer
section kernel
variables [has_zero_morphisms C] (f : X ⟶ Y) [has_kernel f]
/-- The kernel of a morphism `f : X ⟶ Y` as a `subobject X`. -/
abbreviation kernel_subobject : subobject X :=
subobject.mk (kernel.ι f)
/-- The underlying object of `kernel_subobject f` is (up to isomorphism!)
the same as the chosen object `kernel f`. -/
def kernel_subobject_iso :
(kernel_subobject f : C) ≅ kernel f :=
subobject.underlying_iso (kernel.ι f)
@[simp, reassoc, elementwise]
lemma kernel_subobject_arrow :
(kernel_subobject_iso f).hom ≫ kernel.ι f = (kernel_subobject f).arrow :=
by simp [kernel_subobject_iso]
@[simp, reassoc, elementwise]
lemma kernel_subobject_arrow' :
(kernel_subobject_iso f).inv ≫ (kernel_subobject f).arrow = kernel.ι f :=
by simp [kernel_subobject_iso]
@[simp, reassoc, elementwise]
lemma kernel_subobject_arrow_comp :
(kernel_subobject f).arrow ≫ f = 0 :=
by { rw [←kernel_subobject_arrow], simp only [category.assoc, kernel.condition, comp_zero], }
lemma kernel_subobject_factors {W : C} (h : W ⟶ X) (w : h ≫ f = 0) :
(kernel_subobject f).factors h :=
⟨kernel.lift _ h w, by simp⟩
lemma kernel_subobject_factors_iff {W : C} (h : W ⟶ X) :
(kernel_subobject f).factors h ↔ h ≫ f = 0 :=
⟨λ w, by rw [←subobject.factor_thru_arrow _ _ w, category.assoc,
kernel_subobject_arrow_comp, comp_zero],
kernel_subobject_factors f h⟩
/-- A factorisation of `h : W ⟶ X` through `kernel_subobject f`, assuming `h ≫ f = 0`. -/
def factor_thru_kernel_subobject {W : C} (h : W ⟶ X) (w : h ≫ f = 0) :
W ⟶ kernel_subobject f :=
(kernel_subobject f).factor_thru h (kernel_subobject_factors f h w)
@[simp] lemma factor_thru_kernel_subobject_comp_arrow {W : C} (h : W ⟶ X) (w : h ≫ f = 0) :
factor_thru_kernel_subobject f h w ≫ (kernel_subobject f).arrow = h :=
by { dsimp [factor_thru_kernel_subobject], simp, }
@[simp] lemma factor_thru_kernel_subobject_comp_kernel_subobject_iso {W : C} (h : W ⟶ X)
(w : h ≫ f = 0) :
factor_thru_kernel_subobject f h w ≫ (kernel_subobject_iso f).hom = kernel.lift f h w :=
(cancel_mono (kernel.ι f)).1 $ by simp
section
variables {f} {X' Y' : C} {f' : X' ⟶ Y'} [has_kernel f']
/-- A commuting square induces a morphism between the kernel subobjects. -/
def kernel_subobject_map (sq : arrow.mk f ⟶ arrow.mk f') :
(kernel_subobject f : C) ⟶ (kernel_subobject f' : C) :=
subobject.factor_thru _
((kernel_subobject f).arrow ≫ sq.left)
(kernel_subobject_factors _ _ (by simp [sq.w]))
@[simp, reassoc, elementwise]
lemma kernel_subobject_map_arrow (sq : arrow.mk f ⟶ arrow.mk f') :
kernel_subobject_map sq ≫ (kernel_subobject f').arrow =
(kernel_subobject f).arrow ≫ sq.left :=
by simp [kernel_subobject_map]
@[simp] lemma kernel_subobject_map_id : kernel_subobject_map (𝟙 (arrow.mk f)) = 𝟙 _ :=
by { ext, simp, dsimp, simp, } -- See library note [dsimp, simp].
@[simp] lemma kernel_subobject_map_comp {X'' Y'' : C} {f'' : X'' ⟶ Y''} [has_kernel f'']
(sq : arrow.mk f ⟶ arrow.mk f') (sq' : arrow.mk f' ⟶ arrow.mk f'') :
kernel_subobject_map (sq ≫ sq') = kernel_subobject_map sq ≫ kernel_subobject_map sq' :=
by { ext, simp, }
end
@[simp]
lemma kernel_subobject_zero {A B : C} : kernel_subobject (0 : A ⟶ B) = ⊤ :=
(is_iso_iff_mk_eq_top _).mp (by apply_instance)
instance is_iso_kernel_subobject_zero_arrow : is_iso (kernel_subobject (0 : X ⟶ Y)).arrow :=
(is_iso_arrow_iff_eq_top _).mpr kernel_subobject_zero
lemma le_kernel_subobject (A : subobject X) (h : A.arrow ≫ f = 0) : A ≤ kernel_subobject f :=
subobject.le_mk_of_comm (kernel.lift f A.arrow h) (by simp)
/--
The isomorphism between the kernel of `f ≫ g` and the kernel of `g`,
when `f` is an isomorphism.
-/
def kernel_subobject_iso_comp
{X' : C} (f : X' ⟶ X) [is_iso f] (g : X ⟶ Y) [has_kernel g] :
(kernel_subobject (f ≫ g) : C) ≅ (kernel_subobject g : C) :=
(kernel_subobject_iso _) ≪≫ (kernel_is_iso_comp f g) ≪≫ (kernel_subobject_iso _).symm
@[simp]
lemma kernel_subobject_iso_comp_hom_arrow
{X' : C} (f : X' ⟶ X) [is_iso f] (g : X ⟶ Y) [has_kernel g] :
(kernel_subobject_iso_comp f g).hom ≫ (kernel_subobject g).arrow =
(kernel_subobject (f ≫ g)).arrow ≫ f :=
by { simp [kernel_subobject_iso_comp], }
@[simp]
lemma kernel_subobject_iso_comp_inv_arrow
{X' : C} (f : X' ⟶ X) [is_iso f] (g : X ⟶ Y) [has_kernel g] :
(kernel_subobject_iso_comp f g).inv ≫ (kernel_subobject (f ≫ g)).arrow =
(kernel_subobject g).arrow ≫ inv f :=
by { simp [kernel_subobject_iso_comp], }
/-- The kernel of `f` is always a smaller subobject than the kernel of `f ≫ h`. -/
lemma kernel_subobject_comp_le
(f : X ⟶ Y) [has_kernel f] {Z : C} (h : Y ⟶ Z) [has_kernel (f ≫ h)]:
kernel_subobject f ≤ kernel_subobject (f ≫ h) :=
le_kernel_subobject _ _ (by simp)
/-- Postcomposing by an monomorphism does not change the kernel subobject. -/
@[simp]
lemma kernel_subobject_comp_mono
(f : X ⟶ Y) [has_kernel f] {Z : C} (h : Y ⟶ Z) [mono h] :
kernel_subobject (f ≫ h) = kernel_subobject f :=
le_antisymm
(le_kernel_subobject _ _ ((cancel_mono h).mp (by simp)))
(kernel_subobject_comp_le f h)
instance kernel_subobject_comp_mono_is_iso
(f : X ⟶ Y) [has_kernel f] {Z : C} (h : Y ⟶ Z) [mono h] :
is_iso (subobject.of_le _ _ (kernel_subobject_comp_le f h)) :=
begin
rw of_le_mk_le_mk_of_comm (kernel_comp_mono f h).inv,
{ apply_instance, },
{ simp, },
end
/-- Taking cokernels is an order-reversing map from the subobjects of `X` to the quotient objects
of `X`. -/
@[simps]
def cokernel_order_hom [has_cokernels C] (X : C) : subobject X →o (subobject (op X))ᵒᵈ :=
{ to_fun := subobject.lift (λ A f hf, subobject.mk (cokernel.π f).op)
begin
rintros A B f g hf hg i rfl,
refine subobject.mk_eq_mk_of_comm _ _ (iso.op _) (quiver.hom.unop_inj _),
{ exact (is_colimit.cocone_point_unique_up_to_iso (colimit.is_colimit _)
(is_cokernel_epi_comp (colimit.is_colimit _) i.hom rfl)).symm },
{ simp only [iso.comp_inv_eq, iso.op_hom, iso.symm_hom, unop_comp, quiver.hom.unop_op,
colimit.comp_cocone_point_unique_up_to_iso_hom, cofork.of_π_ι_app, coequalizer.cofork_π] }
end,
monotone' := subobject.ind₂ _ $
begin
introsI A B f g hf hg h,
dsimp only [subobject.lift_mk],
refine subobject.mk_le_mk_of_comm (cokernel.desc f (cokernel.π g) _).op _,
{ rw [← subobject.of_mk_le_mk_comp h, category.assoc, cokernel.condition, comp_zero] },
{ exact quiver.hom.unop_inj (cokernel.π_desc _ _ _) }
end }
/-- Taking kernels is an order-reversing map from the quotient objects of `X` to the subobjects of
`X`. -/
@[simps]
def kernel_order_hom [has_kernels C] (X : C) : (subobject (op X))ᵒᵈ →o subobject X :=
{ to_fun := subobject.lift (λ A f hf, subobject.mk (kernel.ι f.unop))
begin
rintros A B f g hf hg i rfl,
refine subobject.mk_eq_mk_of_comm _ _ _ _,
{ exact is_limit.cone_point_unique_up_to_iso (limit.is_limit _)
(is_kernel_comp_mono (limit.is_limit (parallel_pair g.unop 0)) i.unop.hom rfl) },
{ dsimp,
simp only [←iso.eq_inv_comp, limit.cone_point_unique_up_to_iso_inv_comp, fork.of_ι_π_app] }
end,
monotone' := subobject.ind₂ _ $
begin
introsI A B f g hf hg h,
dsimp only [subobject.lift_mk],
refine subobject.mk_le_mk_of_comm (kernel.lift g.unop (kernel.ι f.unop) _) _,
{ rw [← subobject.of_mk_le_mk_comp h, unop_comp, kernel.condition_assoc, zero_comp] },
{ exact quiver.hom.op_inj (by simp) }
end }
end kernel
section image
variables (f : X ⟶ Y) [has_image f]
/-- The image of a morphism `f g : X ⟶ Y` as a `subobject Y`. -/
abbreviation image_subobject : subobject Y :=
subobject.mk (image.ι f)
/-- The underlying object of `image_subobject f` is (up to isomorphism!)
the same as the chosen object `image f`. -/
def image_subobject_iso :
(image_subobject f : C) ≅ image f :=
subobject.underlying_iso (image.ι f)
@[simp, reassoc]
lemma image_subobject_arrow :
(image_subobject_iso f).hom ≫ image.ι f = (image_subobject f).arrow :=
by simp [image_subobject_iso]
@[simp, reassoc]
lemma image_subobject_arrow' :
(image_subobject_iso f).inv ≫ (image_subobject f).arrow = image.ι f :=
by simp [image_subobject_iso]
/-- A factorisation of `f : X ⟶ Y` through `image_subobject f`. -/
def factor_thru_image_subobject : X ⟶ image_subobject f :=
factor_thru_image f ≫ (image_subobject_iso f).inv
instance [has_equalizers C] : epi (factor_thru_image_subobject f) :=
by { dsimp [factor_thru_image_subobject], apply epi_comp, }
@[simp, reassoc, elementwise]
lemma image_subobject_arrow_comp :
factor_thru_image_subobject f ≫ (image_subobject f).arrow = f :=
by simp [factor_thru_image_subobject, image_subobject_arrow]
lemma image_subobject_arrow_comp_eq_zero
[has_zero_morphisms C] {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} [has_image f]
[epi (factor_thru_image_subobject f)] (h : f ≫ g = 0) : (image_subobject f).arrow ≫ g = 0 :=
zero_of_epi_comp (factor_thru_image_subobject f) $ by simp [h]
lemma image_subobject_factors_comp_self {W : C} (k : W ⟶ X) :
(image_subobject f).factors (k ≫ f) :=
⟨k ≫ factor_thru_image f, by simp⟩
@[simp]
lemma factor_thru_image_subobject_comp_self {W : C} (k : W ⟶ X) (h) :
(image_subobject f).factor_thru (k ≫ f) h = k ≫ factor_thru_image_subobject f :=
by { ext, simp, }
@[simp]
lemma factor_thru_image_subobject_comp_self_assoc {W W' : C} (k : W ⟶ W') (k' : W' ⟶ X) (h) :
(image_subobject f).factor_thru (k ≫ k' ≫ f) h = k ≫ k' ≫ factor_thru_image_subobject f :=
by { ext, simp, }
/-- The image of `h ≫ f` is always a smaller subobject than the image of `f`. -/
lemma image_subobject_comp_le
{X' : C} (h : X' ⟶ X) (f : X ⟶ Y) [has_image f] [has_image (h ≫ f)] :
image_subobject (h ≫ f) ≤ image_subobject f :=
subobject.mk_le_mk_of_comm (image.pre_comp h f) (by simp)
section
open_locale zero_object
variables [has_zero_morphisms C] [has_zero_object C]
@[simp]
lemma image_subobject_zero_arrow :
(image_subobject (0 : X ⟶ Y)).arrow = 0 :=
by { rw ←image_subobject_arrow, simp, }
@[simp]
lemma image_subobject_zero {A B : C} :
image_subobject (0 : A ⟶ B) = ⊥ :=
subobject.eq_of_comm
(image_subobject_iso _ ≪≫ image_zero ≪≫ subobject.bot_coe_iso_zero.symm) (by simp)
end
section
variables [has_equalizers C]
local attribute [instance] epi_comp
/--
The morphism `image_subobject (h ≫ f) ⟶ image_subobject f`
is an epimorphism when `h` is an epimorphism.
In general this does not imply that `image_subobject (h ≫ f) = image_subobject f`,
although it will when the ambient category is abelian.
-/
instance image_subobject_comp_le_epi_of_epi
{X' : C} (h : X' ⟶ X) [epi h] (f : X ⟶ Y) [has_image f] [has_image (h ≫ f)] :
epi (subobject.of_le _ _ (image_subobject_comp_le h f)) :=
begin
rw of_le_mk_le_mk_of_comm (image.pre_comp h f),
{ apply_instance, },
{ simp, },
end
end
section
variables [has_equalizers C]
/-- Postcomposing by an isomorphism gives an isomorphism between image subobjects. -/
def image_subobject_comp_iso
(f : X ⟶ Y) [has_image f] {Y' : C} (h : Y ⟶ Y') [is_iso h] :
(image_subobject (f ≫ h) : C) ≅ (image_subobject f : C) :=
(image_subobject_iso _) ≪≫ (image.comp_iso _ _).symm ≪≫ (image_subobject_iso _).symm
@[simp, reassoc]
lemma image_subobject_comp_iso_hom_arrow
(f : X ⟶ Y) [has_image f] {Y' : C} (h : Y ⟶ Y') [is_iso h] :
(image_subobject_comp_iso f h).hom ≫ (image_subobject f).arrow =
(image_subobject (f ≫ h)).arrow ≫ inv h :=
by simp [image_subobject_comp_iso]
@[simp, reassoc]
lemma image_subobject_comp_iso_inv_arrow
(f : X ⟶ Y) [has_image f] {Y' : C} (h : Y ⟶ Y') [is_iso h] :
(image_subobject_comp_iso f h).inv ≫ (image_subobject (f ≫ h)).arrow =
(image_subobject f).arrow ≫ h :=
by simp [image_subobject_comp_iso]
end
lemma image_subobject_mono (f : X ⟶ Y) [mono f] : image_subobject f = mk f :=
eq_of_comm (image_subobject_iso f ≪≫ image_mono_iso_source f ≪≫ (underlying_iso f).symm) (by simp)
/-- Precomposing by an isomorphism does not change the image subobject. -/
lemma image_subobject_iso_comp [has_equalizers C]
{X' : C} (h : X' ⟶ X) [is_iso h] (f : X ⟶ Y) [has_image f] :
image_subobject (h ≫ f) = image_subobject f :=
le_antisymm
(image_subobject_comp_le h f)
(subobject.mk_le_mk_of_comm (inv (image.pre_comp h f)) (by simp))
lemma image_subobject_le {A B : C} {X : subobject B} (f : A ⟶ B) [has_image f]
(h : A ⟶ X) (w : h ≫ X.arrow = f) :
image_subobject f ≤ X :=
subobject.le_of_comm
((image_subobject_iso f).hom ≫ image.lift { I := (X : C), e := h, m := X.arrow, })
(by simp)
lemma image_subobject_le_mk {A B : C} {X : C} (g : X ⟶ B) [mono g] (f : A ⟶ B) [has_image f]
(h : A ⟶ X) (w : h ≫ g = f) :
image_subobject f ≤ subobject.mk g :=
image_subobject_le f (h ≫ (subobject.underlying_iso g).inv) (by simp [w])
/-- Given a commutative square between morphisms `f` and `g`,
we have a morphism in the category from `image_subobject f` to `image_subobject g`. -/
def image_subobject_map {W X Y Z : C} {f : W ⟶ X} [has_image f] {g : Y ⟶ Z} [has_image g]
(sq : arrow.mk f ⟶ arrow.mk g) [has_image_map sq] :
(image_subobject f : C) ⟶ (image_subobject g : C) :=
(image_subobject_iso f).hom ≫ image.map sq ≫ (image_subobject_iso g).inv
@[simp, reassoc]
lemma image_subobject_map_arrow {W X Y Z : C} {f : W ⟶ X} [has_image f] {g : Y ⟶ Z} [has_image g]
(sq : arrow.mk f ⟶ arrow.mk g) [has_image_map sq] :
image_subobject_map sq ≫ (image_subobject g).arrow = (image_subobject f).arrow ≫ sq.right :=
begin
simp only [image_subobject_map, category.assoc, image_subobject_arrow'],
erw [image.map_ι, ←category.assoc, image_subobject_arrow],
end
end image
end limits
end category_theory
|
cb2cd5500b3929c7b72334760483e40999a9d9ff | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/data/polynomial/derivative.lean | fbc1819285c936d61a50a738e54599e4bdbe9cc5 | [
"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 | 15,076 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker
-/
import algebra.hom.iterate
import data.polynomial.eval
/-!
# The derivative map on polynomials
## Main definitions
* `polynomial.derivative`: The formal derivative of polynomials, expressed as a linear map.
-/
noncomputable theory
open finset
open_locale big_operators classical polynomial
namespace polynomial
universes u v w y z
variables {R : Type u} {S : Type v} {T : Type w} {ι : Type y} {A : Type z} {a b : R} {n : ℕ}
section derivative
section semiring
variables [semiring R]
/-- `derivative p` is the formal derivative of the polynomial `p` -/
def derivative : R[X] →ₗ[R] R[X] :=
{ to_fun := λ p, p.sum (λ n a, C (a * n) * X^(n-1)),
map_add' := λ p q, by rw sum_add_index;
simp only [add_mul, forall_const, ring_hom.map_add,
eq_self_iff_true, zero_mul, ring_hom.map_zero],
map_smul' := λ a p, by dsimp; rw sum_smul_index;
simp only [mul_sum, ← C_mul', mul_assoc, coeff_C_mul, ring_hom.map_mul, forall_const,
zero_mul, ring_hom.map_zero, sum] }
lemma derivative_apply (p : R[X]) :
derivative p = p.sum (λn a, C (a * n) * X^(n - 1)) := rfl
lemma coeff_derivative (p : R[X]) (n : ℕ) :
coeff (derivative p) n = coeff p (n + 1) * (n + 1) :=
begin
rw [derivative_apply],
simp only [coeff_X_pow, coeff_sum, coeff_C_mul],
rw [sum, finset.sum_eq_single (n + 1)],
simp only [nat.add_succ_sub_one, add_zero, mul_one, if_true, eq_self_iff_true], norm_cast,
{ assume b, cases b,
{ intros, rw [nat.cast_zero, mul_zero, zero_mul], },
{ intros _ H, rw [nat.succ_sub_one b, if_neg (mt (congr_arg nat.succ) H.symm), mul_zero] } },
{ rw [if_pos (add_tsub_cancel_right n 1).symm, mul_one, nat.cast_add, nat.cast_one,
mem_support_iff],
intro h, push_neg at h, simp [h], },
end
@[simp]
lemma derivative_zero : derivative (0 : R[X]) = 0 :=
derivative.map_zero
@[simp]
lemma iterate_derivative_zero {k : ℕ} : derivative^[k] (0 : R[X]) = 0 :=
begin
induction k with k ih,
{ simp, },
{ simp [ih], },
end
@[simp]
lemma derivative_monomial (a : R) (n : ℕ) : derivative (monomial n a) = monomial (n - 1) (a * n) :=
by { rw [derivative_apply, sum_monomial_index, C_mul_X_pow_eq_monomial], simp }
lemma derivative_C_mul_X_pow (a : R) (n : ℕ) : derivative (C a * X ^ n) = C (a * n) * X^(n - 1) :=
by rw [C_mul_X_pow_eq_monomial, C_mul_X_pow_eq_monomial, derivative_monomial]
@[simp] lemma derivative_X_pow (n : ℕ) :
derivative (X ^ n : R[X]) = (n : R[X]) * X ^ (n - 1) :=
by convert derivative_C_mul_X_pow (1 : R) n; simp
@[simp] lemma derivative_C {a : R} : derivative (C a) = 0 :=
by simp [derivative_apply]
lemma derivative_of_nat_degree_zero {p : R[X]} (hp : p.nat_degree = 0) : p.derivative = 0 :=
by rw [eq_C_of_nat_degree_eq_zero hp, derivative_C]
@[simp] lemma derivative_X : derivative (X : R[X]) = 1 :=
(derivative_monomial _ _).trans $ by simp
@[simp] lemma derivative_one : derivative (1 : R[X]) = 0 :=
derivative_C
@[simp] lemma derivative_bit0 {a : R[X]} : derivative (bit0 a) = bit0 (derivative a) :=
by simp [bit0]
@[simp] lemma derivative_bit1 {a : R[X]} : derivative (bit1 a) = bit0 (derivative a) :=
by simp [bit1]
@[simp] lemma derivative_add {f g : R[X]} :
derivative (f + g) = derivative f + derivative g :=
derivative.map_add f g
@[simp] lemma iterate_derivative_add {f g : R[X]} {k : ℕ} :
derivative^[k] (f + g) = (derivative^[k] f) + (derivative^[k] g) :=
derivative.to_add_monoid_hom.iterate_map_add _ _ _
@[simp] lemma derivative_sum {s : finset ι} {f : ι → R[X]} :
derivative (∑ b in s, f b) = ∑ b in s, derivative (f b) :=
derivative.map_sum
@[simp] lemma derivative_smul {S : Type*} [monoid S]
[distrib_mul_action S R] [is_scalar_tower S R R]
(s : S) (p : R[X]) : derivative (s • p) = s • derivative p :=
derivative.map_smul_of_tower s p
@[simp] lemma iterate_derivative_smul {S : Type*} [monoid S]
[distrib_mul_action S R] [is_scalar_tower S R R]
(s : S) (p : R[X]) (k : ℕ) :
derivative^[k] (s • p) = s • (derivative^[k] p) :=
begin
induction k with k ih generalizing p,
{ simp, },
{ simp [ih], },
end
@[simp]
lemma iterate_derivative_C_mul (a : R) (p : R[X]) (k : ℕ) :
derivative^[k] (C a * p) = C a * (derivative^[k] p) :=
by simp_rw [← smul_eq_C_mul, iterate_derivative_smul]
theorem of_mem_support_derivative {p : R[X]} {n : ℕ} (h : n ∈ p.derivative.support) :
n + 1 ∈ p.support :=
mem_support_iff.2 $ λ (h1 : p.coeff (n+1) = 0), mem_support_iff.1 h $
show p.derivative.coeff n = 0, by rw [coeff_derivative, h1, zero_mul]
theorem degree_derivative_lt {p : R[X]} (hp : p ≠ 0) : p.derivative.degree < p.degree :=
(finset.sup_lt_iff $ bot_lt_iff_ne_bot.2 $ mt degree_eq_bot.1 hp).2 $ λ n hp, lt_of_lt_of_le
(with_bot.some_lt_some.2 n.lt_succ_self) $ finset.le_sup $ of_mem_support_derivative hp
theorem degree_derivative_le {p : R[X]} : p.derivative.degree ≤ p.degree :=
if H : p = 0 then le_of_eq $ by rw [H, derivative_zero] else (degree_derivative_lt H).le
theorem nat_degree_derivative_lt {p : R[X]} (hp : p.nat_degree ≠ 0) :
p.derivative.nat_degree < p.nat_degree :=
begin
cases eq_or_ne p.derivative 0 with hp' hp',
{ rw [hp', polynomial.nat_degree_zero],
exact hp.bot_lt },
{ rw nat_degree_lt_nat_degree_iff hp',
exact degree_derivative_lt (λ h, hp (h.symm ▸ nat_degree_zero)) }
end
lemma nat_degree_derivative_le (p : R[X]) : p.derivative.nat_degree ≤ p.nat_degree - 1 :=
begin
by_cases p0 : p.nat_degree = 0,
{ simp [p0, derivative_of_nat_degree_zero] },
{ exact nat.le_pred_of_lt (nat_degree_derivative_lt p0) }
end
@[simp] lemma derivative_nat_cast {n : ℕ} : derivative (n : R[X]) = 0 :=
begin
rw ← map_nat_cast C n,
exact derivative_C,
end
lemma iterate_derivative_eq_zero {p : R[X]} {x : ℕ} (hx : p.nat_degree < x) :
polynomial.derivative^[x] p = 0 :=
begin
induction h : p.nat_degree using nat.strong_induction_on with _ ih generalizing p x,
subst h,
obtain ⟨t, rfl⟩ := nat.exists_eq_succ_of_ne_zero (pos_of_gt hx).ne',
rw [function.iterate_succ_apply],
by_cases hp : p.nat_degree = 0,
{ rw [derivative_of_nat_degree_zero hp, iterate_derivative_zero] },
have := nat_degree_derivative_lt hp,
exact ih _ this (this.trans_le $ nat.le_of_lt_succ hx) rfl
end
theorem nat_degree_eq_zero_of_derivative_eq_zero [no_zero_divisors R] [char_zero R] {f : R[X]}
(h : f.derivative = 0) : f.nat_degree = 0 :=
begin
rcases eq_or_ne f 0 with rfl | hf,
{ exact nat_degree_zero },
rw nat_degree_eq_zero_iff_degree_le_zero,
by_contra' f_nat_degree_pos,
rw [←nat_degree_pos_iff_degree_pos] at f_nat_degree_pos,
let m := f.nat_degree - 1,
have hm : m + 1 = f.nat_degree := tsub_add_cancel_of_le f_nat_degree_pos,
have h2 := coeff_derivative f m,
rw polynomial.ext_iff at h,
rw [h m, coeff_zero, zero_eq_mul] at h2,
replace h2 := h2.resolve_right (λ h2, by norm_cast at h2),
rw [hm, ←leading_coeff, leading_coeff_eq_zero] at h2,
exact hf h2
end
@[simp] lemma derivative_mul {f g : R[X]} :
derivative (f * g) = derivative f * g + f * derivative g :=
calc derivative (f * g) = f.sum (λn a, g.sum (λm b, (n + m) • (C (a * b) * X^((n + m) - 1)))) :
begin
rw mul_eq_sum_sum,
transitivity, exact derivative_sum,
transitivity, { apply finset.sum_congr rfl, assume x hx, exact derivative_sum },
apply finset.sum_congr rfl, assume n hn, apply finset.sum_congr rfl, assume m hm,
transitivity,
{ apply congr_arg, exact monomial_eq_C_mul_X },
dsimp, rw [← smul_mul_assoc, smul_C, nsmul_eq_mul'], exact derivative_C_mul_X_pow _ _
end
... = f.sum (λn a, g.sum (λm b,
(n • (C a * X^(n - 1))) * (C b * X^m) + (C a * X^n) * (m • (C b * X^(m - 1))))) :
sum_congr rfl $ assume n hn, sum_congr rfl $ assume m hm,
by cases n; cases m; simp_rw [add_smul, mul_smul_comm, smul_mul_assoc,
X_pow_mul_assoc, ← mul_assoc, ← C_mul, mul_assoc, ← pow_add];
simp only [nat.add_succ, nat.succ_add, nat.succ_sub_one, zero_smul, add_comm]
... = derivative f * g + f * derivative g :
begin
conv { to_rhs, congr,
{ rw [← sum_C_mul_X_eq g] },
{ rw [← sum_C_mul_X_eq f] } },
simp only [sum, sum_add_distrib, finset.mul_sum, finset.sum_mul, derivative_apply],
simp_rw [← smul_mul_assoc, smul_C, nsmul_eq_mul'],
end
lemma derivative_eval (p : R[X]) (x : R) :
p.derivative.eval x = p.sum (λ n a, (a * n)*x^(n-1)) :=
by simp_rw [derivative_apply, eval_sum, eval_mul_X_pow, eval_C]
@[simp]
theorem derivative_map [semiring S] (p : R[X]) (f : R →+* S) :
(p.map f).derivative = p.derivative.map f :=
begin
let n := max p.nat_degree ((map f p).nat_degree),
rw [derivative_apply, derivative_apply],
rw [sum_over_range' _ _ (n + 1) ((le_max_left _ _).trans_lt (lt_add_one _))],
rw [sum_over_range' _ _ (n + 1) ((le_max_right _ _).trans_lt (lt_add_one _))],
simp only [polynomial.map_sum, polynomial.map_mul, polynomial.map_C, map_mul, coeff_map,
map_nat_cast, polynomial.map_nat_cast, polynomial.map_pow, map_X],
all_goals { intro n, rw [zero_mul, C_0, zero_mul], }
end
@[simp]
theorem iterate_derivative_map [semiring S] (p : R[X]) (f : R →+* S) (k : ℕ):
polynomial.derivative^[k] (p.map f) = (polynomial.derivative^[k] p).map f :=
begin
induction k with k ih generalizing p,
{ simp, },
{ simp only [ih, function.iterate_succ, polynomial.derivative_map, function.comp_app], },
end
@[simp] lemma iterate_derivative_nat_cast_mul {n k : ℕ} {f : R[X]} :
derivative^[k] (n * f) = n * (derivative^[k] f) :=
by induction k with k ih generalizing f; simp*
lemma mem_support_derivative [no_zero_smul_divisors ℕ R]
(p : R[X]) (n : ℕ) :
n ∈ (derivative p).support ↔ n + 1 ∈ p.support :=
suffices ¬p.coeff (n + 1) * (n + 1 : ℕ) = 0 ↔ coeff p (n + 1) ≠ 0,
by simpa only [mem_support_iff, coeff_derivative, ne.def, nat.cast_succ],
by { rw [← nsmul_eq_mul', smul_eq_zero], simp only [nat.succ_ne_zero, false_or] }
@[simp] lemma degree_derivative_eq [no_zero_smul_divisors ℕ R]
(p : R[X]) (hp : 0 < nat_degree p) :
degree (derivative p) = (nat_degree p - 1 : ℕ) :=
begin
have h0 : p ≠ 0,
{ contrapose! hp,
simp [hp] },
apply le_antisymm,
{ rw derivative_apply,
apply le_trans (degree_sum_le _ _) (sup_le (λ n hn, _)),
apply le_trans (degree_C_mul_X_pow_le _ _) (with_bot.coe_le_coe.2 (tsub_le_tsub_right _ _)),
apply le_nat_degree_of_mem_supp _ hn },
{ refine le_sup _,
rw [mem_support_derivative, tsub_add_cancel_of_le, mem_support_iff],
{ show ¬ leading_coeff p = 0,
rw [leading_coeff_eq_zero],
assume h, rw [h, nat_degree_zero] at hp,
exact lt_irrefl 0 (lt_of_le_of_lt (zero_le _) hp), },
exact hp }
end
end semiring
section comm_semiring
variables [comm_semiring R]
theorem derivative_pow_succ (p : R[X]) (n : ℕ) :
(p ^ (n + 1)).derivative = (n + 1) * (p ^ n) * p.derivative :=
nat.rec_on n (by rw [pow_one, nat.cast_zero, zero_add, one_mul, pow_zero, one_mul]) $ λ n ih,
by rw [pow_succ', derivative_mul, ih, mul_right_comm, ← add_mul,
add_mul (n.succ : R[X]), one_mul, pow_succ', mul_assoc, n.cast_succ]
theorem derivative_pow (p : R[X]) (n : ℕ) :
(p ^ n).derivative = n * (p ^ (n - 1)) * p.derivative :=
nat.cases_on n (by rw [pow_zero, derivative_one, nat.cast_zero, zero_mul, zero_mul]) $ λ n,
by rw [p.derivative_pow_succ n, n.succ_sub_one, n.cast_succ]
lemma derivative_comp (p q : R[X]) :
(p.comp q).derivative = q.derivative * p.derivative.comp q :=
begin
apply polynomial.induction_on' p,
{ intros p₁ p₂ h₁ h₂, simp [h₁, h₂, mul_add], },
{ intros n r,
simp only [derivative_pow, derivative_mul, monomial_comp, derivative_monomial, derivative_C,
zero_mul, C_eq_nat_cast, zero_add, ring_hom.map_mul],
-- is there a tactic for this? (a multiplicative `abel`):
rw [mul_comm (derivative q)],
simp only [mul_assoc], }
end
/-- Chain rule for formal derivative of polynomials. -/
theorem derivative_eval₂_C (p q : R[X]) :
(p.eval₂ C q).derivative = p.derivative.eval₂ C q * q.derivative :=
polynomial.induction_on p
(λ r, by rw [eval₂_C, derivative_C, eval₂_zero, zero_mul])
(λ p₁ p₂ ih₁ ih₂, by rw [eval₂_add, derivative_add, ih₁, ih₂, derivative_add, eval₂_add, add_mul])
(λ n r ih, by rw [pow_succ', ← mul_assoc, eval₂_mul, eval₂_X, derivative_mul, ih,
@derivative_mul _ _ _ X, derivative_X, mul_one, eval₂_add, @eval₂_mul _ _ _ _ X, eval₂_X,
add_mul, mul_right_comm])
theorem derivative_prod {s : multiset ι} {f : ι → R[X]} :
(multiset.map f s).prod.derivative =
(multiset.map (λ i, (multiset.map f (s.erase i)).prod * (f i).derivative) s).sum :=
begin
refine multiset.induction_on s (by simp) (λ i s h, _),
rw [multiset.map_cons, multiset.prod_cons, derivative_mul, multiset.map_cons _ i s,
multiset.sum_cons, multiset.erase_cons_head, mul_comm (f i).derivative],
congr,
rw [h, ← add_monoid_hom.coe_mul_left, (add_monoid_hom.mul_left (f i)).map_multiset_sum _,
add_monoid_hom.coe_mul_left],
simp only [function.comp_app, multiset.map_map],
refine congr_arg _ (multiset.map_congr rfl (λ j hj, _)),
rw [← mul_assoc, ← multiset.prod_cons, ← multiset.map_cons],
by_cases hij : i = j,
{ simp [hij, ← multiset.prod_cons, ← multiset.map_cons, multiset.cons_erase hj] },
{ simp [hij] }
end
end comm_semiring
section ring
variables [ring R]
@[simp] lemma derivative_neg (f : R[X]) : derivative (-f) = - derivative f :=
linear_map.map_neg derivative f
@[simp] lemma iterate_derivative_neg {f : R[X]} {k : ℕ} :
derivative^[k] (-f) = - (derivative^[k] f) :=
(@derivative R _).to_add_monoid_hom.iterate_map_neg _ _
@[simp] lemma derivative_sub {f g : R[X]} :
derivative (f - g) = derivative f - derivative g :=
linear_map.map_sub derivative f g
@[simp] lemma iterate_derivative_sub {k : ℕ} {f g : R[X]} :
derivative^[k] (f - g) = (derivative^[k] f) - (derivative^[k] g) :=
by induction k with k ih generalizing f g; simp*
end ring
section comm_ring
variables [comm_ring R]
lemma derivative_comp_one_sub_X (p : R[X]) :
(p.comp (1-X)).derivative = -p.derivative.comp (1-X) :=
by simp [derivative_comp]
@[simp]
lemma iterate_derivative_comp_one_sub_X (p : R[X]) (k : ℕ) :
derivative^[k] (p.comp (1-X)) = (-1)^k * (derivative^[k] p).comp (1-X) :=
begin
induction k with k ih generalizing p,
{ simp, },
{ simp [ih p.derivative, iterate_derivative_neg, derivative_comp, pow_succ], },
end
lemma eval_multiset_prod_X_sub_C_derivative {S : multiset R} {r : R} (hr : r ∈ S) :
eval r (multiset.map (λ a, X - C a) S).prod.derivative =
(multiset.map (λ a, r - a) (S.erase r)).prod :=
begin
nth_rewrite 0 [← multiset.cons_erase hr],
simpa using (eval_ring_hom r).map_multiset_prod (multiset.map (λ a, X - C a) (S.erase r)),
end
end comm_ring
end derivative
end polynomial
|
8668c979773790244c912884c8caae696cb6153b | 367134ba5a65885e863bdc4507601606690974c1 | /src/data/multiset/fold.lean | 39d9d0602a6307ae0e7f7b24d35822accda3f509 | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 3,795 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
-/
import data.multiset.erase_dup
/-!
# The fold operation for a commutative associative operation over a multiset.
-/
namespace multiset
variables {α β : Type*}
/-! ### fold -/
section fold
variables (op : α → α → α) [hc : is_commutative α op] [ha : is_associative α op]
local notation a * b := op a b
include hc ha
/-- `fold op b s` folds a commutative associative operation `op` over
the multiset `s`. -/
def fold : α → multiset α → α := foldr op (left_comm _ hc.comm ha.assoc)
theorem fold_eq_foldr (b : α) (s : multiset α) :
fold op b s = foldr op (left_comm _ hc.comm ha.assoc) b s := rfl
@[simp] theorem coe_fold_r (b : α) (l : list α) : fold op b l = l.foldr op b := rfl
theorem coe_fold_l (b : α) (l : list α) : fold op b l = l.foldl op b :=
(coe_foldr_swap op _ b l).trans $ by simp [hc.comm]
theorem fold_eq_foldl (b : α) (s : multiset α) :
fold op b s = foldl op (right_comm _ hc.comm ha.assoc) b s :=
quot.induction_on s $ λ l, coe_fold_l _ _ _
@[simp] theorem fold_zero (b : α) : (0 : multiset α).fold op b = b := rfl
@[simp] theorem fold_cons_left : ∀ (b a : α) (s : multiset α),
(a ::ₘ s).fold op b = a * s.fold op b := foldr_cons _ _
theorem fold_cons_right (b a : α) (s : multiset α) : (a ::ₘ s).fold op b = s.fold op b * a :=
by simp [hc.comm]
theorem fold_cons'_right (b a : α) (s : multiset α) : (a ::ₘ s).fold op b = s.fold op (b * a) :=
by rw [fold_eq_foldl, foldl_cons, ← fold_eq_foldl]
theorem fold_cons'_left (b a : α) (s : multiset α) : (a ::ₘ s).fold op b = s.fold op (a * b) :=
by rw [fold_cons'_right, hc.comm]
theorem fold_add (b₁ b₂ : α) (s₁ s₂ : multiset α) :
(s₁ + s₂).fold op (b₁ * b₂) = s₁.fold op b₁ * s₂.fold op b₂ :=
multiset.induction_on s₂
(by rw [add_zero, fold_zero, ← fold_cons'_right, ← fold_cons_right op])
(by simp {contextual := tt}; cc)
theorem fold_singleton (b a : α) : (a ::ₘ 0 : multiset α).fold op b = a * b := by simp
theorem fold_distrib {f g : β → α} (u₁ u₂ : α) (s : multiset β) :
(s.map (λx, f x * g x)).fold op (u₁ * u₂) = (s.map f).fold op u₁ * (s.map g).fold op u₂ :=
multiset.induction_on s (by simp) (by simp {contextual := tt}; cc)
theorem fold_hom {op' : β → β → β} [is_commutative β op'] [is_associative β op']
{m : α → β} (hm : ∀x y, m (op x y) = op' (m x) (m y)) (b : α) (s : multiset α) :
(s.map m).fold op' (m b) = m (s.fold op b) :=
multiset.induction_on s (by simp) (by simp [hm] {contextual := tt})
theorem fold_union_inter [decidable_eq α] (s₁ s₂ : multiset α) (b₁ b₂ : α) :
(s₁ ∪ s₂).fold op b₁ * (s₁ ∩ s₂).fold op b₂ = s₁.fold op b₁ * s₂.fold op b₂ :=
by rw [← fold_add op, union_add_inter, fold_add op]
@[simp] theorem fold_erase_dup_idem [decidable_eq α] [hi : is_idempotent α op] (s : multiset α)
(b : α) :
(erase_dup s).fold op b = s.fold op b :=
multiset.induction_on s (by simp) $ λ a s IH, begin
by_cases a ∈ s; simp [IH, h],
show fold op b s = op a (fold op b s),
rw [← cons_erase h, fold_cons_left, ← ha.assoc, hi.idempotent],
end
end fold
open nat
theorem le_smul_erase_dup [decidable_eq α] (s : multiset α) :
∃ n : ℕ, s ≤ n •ℕ erase_dup s :=
⟨(s.map (λ a, count a s)).fold max 0, le_iff_count.2 $ λ a, begin
rw count_smul, by_cases a ∈ s,
{ refine le_trans _ (mul_le_mul_left _ $ count_pos.2 $ mem_erase_dup.2 h),
have : count a s ≤ fold max 0 (map (λ a, count a s) (a ::ₘ erase s a));
[simp [le_max_left], simpa [cons_erase h]] },
{ simp [count_eq_zero.2 h, nat.zero_le] }
end⟩
end multiset
|
442e0916afe62dd4387f532a98de86bfe975ac61 | e07b1aca72e83a272dd59d24c6e0fa246034d774 | /src/tutorials/05_sequence_limits.lean | 0858cb9591fff9d23e83fb4649e9714789b70c09 | [
"Apache-2.0",
"LicenseRef-scancode-unknown-license-reference"
] | permissive | pedrominicz/learn | 637a343bd4f8669d76819ac660a2d2d3e0958710 | b79b802a9846c86c21d4b6f3e17af36e7382f0ef | refs/heads/master | 1,671,746,990,402 | 1,670,778,113,000 | 1,670,778,113,000 | 265,735,177 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,097 | lean | import data.real.basic
import algebra.pi_instances
import tuto_lib
notation `|`x`|` := abs x
/-
In this file we manipulate the elementary definition of limits of
sequences of real numbers.
mathlib has a much more general definition of limits, but here
we want to practice using the logical operators and relations
covered in the previous files.
A sequence u is a function from ℕ to ℝ, hence Lean says
u : ℕ → ℝ
The definition we'll be using is:
-- Definition of « u tends to l »
def seq_limit (u : ℕ → ℝ) (l : ℝ) : Prop :=
∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| ≤ ε
Note the use of `∀ ε > 0, ...` which is an abbreviation of
`∀ ε, ε > 0 → ... `
In particular, a statement like `h : ∀ ε > 0, ...`
can be specialized to a given ε₀ by
`specialize h ε₀ hε₀`
where hε₀ is a proof of ε₀ > 0.
Also recall that, wherever Lean expects some proof term, we can
start a tactic mode proof using the keyword `by` (followed by curly braces
if you need more than one tactic invocation).
For instance, if the local context contains:
δ : ℝ
δ_pos : δ > 0
h : ∀ ε > 0, ...
then we can specialize h to the real number δ/2 using:
`specialize h (δ/2) (by linarith)`
where `by linarith` will provide the proof of `δ/2 > 0` expected by Lean.
We'll take this opportunity to use two new tactics:
`norm_num` will perform numerical normalization on the goal and `norm_num at h`
will do the same in assumption `h`. This will get rid of trivial calculations on numbers,
like replacing |l - l| by zero in the next exercise.
`congr'` will try to prove equalities between applications of functions by recursively
proving the arguments are the same.
For instance, if the goal is `f x + g y = f z + g t` then congr will replace it by
two goals: `x = z` and `y = t`.
You can limit the recursion depth by specifying a natural number after `congr'`.
For instance, in the above example, `congr' 1` will give new goals
`f x = f z` and `g y = g t`, which only inspect arguments of the addition and not deeper.
-/
variables (u v w : ℕ → ℝ) (l l' l₁ l₂ : ℝ)
-- If u is constant with value l then u tends to l
-- 0033
example : (∀ n, u n = l) → seq_limit u l :=
begin
intros h x hx,
use 0,
intros n hn,
rw h,
norm_num,
exact le_of_lt hx
end
/- When dealing with absolute values, we'll use lemmas:
abs_le (x y : ℝ) : |x| ≤ y ↔ -y ≤ x ∧ x ≤ y
abs_add (x y : ℝ) : |x + y| ≤ |x| + |y|
abs_sub (x y : ℝ) : |x - y| = |y - x|
You should probably write them down on a sheet of paper that you keep at
hand since they are used in many exercises.
-/
-- Assume l > 0. Then u tends to l implies u n ≥ l/2 for large enough n
-- 0034
example (hl : l > 0) : seq_limit u l → ∃ N, ∀ n ≥ N, u n ≥ l/2 :=
begin
intro h,
cases h (l/2) (by linarith) with x hx,
use x,
intros y hy,
specialize hx y hy,
rw abs_le at hx,
linarith
end
/-
When dealing with max, you can use
ge_max_iff (p q r) : r ≥ max p q ↔ r ≥ p ∧ r ≥ q
le_max_left p q : p ≤ max p q
le_max_right p q : q ≤ max p q
You should probably add them to the sheet of paper where you wrote
the `abs` lemmas since they are used in many exercises.
Let's see an example.
-/
example (hu : seq_limit u l₁) (hv : seq_limit v l₂) :
seq_limit (u + v) (l₁ + l₂) :=
begin
intros ε ε_pos,
cases hu (ε/2) (by linarith) with n₁ hn₁,
cases hv (ε/2) (by linarith) with n₂ hn₂,
use max n₁ n₂,
intros n hn,
cases ge_max_iff.mp hn with hn₁' hn₂',
have : |u n - l₁| ≤ ε/2, from hn₁ n hn₁',
have : |v n - l₂| ≤ ε/2, from hn₂ n hn₂',
calc
|(u + v) n - (l₁ + l₂)| = |u n + v n - (l₁ + l₂)| : rfl
... = |(u n - l₁) + (v n - l₂)| : by congr' 1; ring
... ≤ |u n - l₁| + |v n - l₂| : by apply abs_add
... ≤ ε : by linarith
end
-- If u tends to l and v tends l' then u+v tends to l+l'
example (hu : seq_limit u l) (hv : seq_limit v l') :
seq_limit (u + v) (l + l') :=
begin
intros ε ε_pos,
cases hu (ε/2) (by linarith) with N₁ hN₁,
cases hv (ε/2) (by linarith) with N₂ hN₂,
use max N₁ N₂,
intros n hn,
cases ge_max_iff.mp hn with hn₁ hn₂,
have fact₁ : |u n - l| ≤ ε/2,
from hN₁ n (by linarith), -- note the use of `from`.
-- This is an alias for `exact`,
-- but reads nicer in this context
have fact₂ : |v n - l'| ≤ ε/2,
from hN₂ n (by linarith),
calc
|(u + v) n - (l + l')| = |u n + v n - (l + l')| : rfl
... = |(u n - l) + (v n - l')| : by congr' 1; ring
... ≤ |u n - l| + |v n - l'| : by apply abs_add
... ≤ ε : by linarith,
end
/-
In the above proof, we used `have` to prepare facts for `linarith` consumption in the last line.
Since we have direct proof terms for them, we can feed them directly to `linarith` as in the next proof
of the same statement.
Another variation we introduce is rewriting using `ge_max_iff` and letting `linarith` handle the
conjunction, instead of creating two new assumptions.
-/
example (hu : seq_limit u l) (hv : seq_limit v l') :
seq_limit (u + v) (l + l') :=
begin
intros ε ε_pos,
cases hu (ε/2) (by linarith) with N₁ hN₁,
cases hv (ε/2) (by linarith) with N₂ hN₂,
use max N₁ N₂,
intros n hn,
rw ge_max_iff at hn,
calc
|(u + v) n - (l + l')| = |u n + v n - (l + l')| : rfl
... = |(u n - l) + (v n - l')| : by congr' 1 ; ring
... ≤ |u n - l| + |v n - l'| : by apply abs_add
... ≤ ε : by linarith [hN₁ n (by linarith), hN₂ n (by linarith)],
end
/- Let's do something similar: the squeezing theorem. -/
-- 0035
example (hu : seq_limit u l) (hw : seq_limit w l)
(h₁ : ∀ n, u n ≤ v n)
(h₂ : ∀ n, v n ≤ w n) : seq_limit v l :=
begin
intros ε ε_pos,
cases hu ε (by linarith) with n₁ hn₁,
cases hw ε (by linarith) with n₂ hn₂,
use max n₁ n₂,
intros k hk,
rw ge_max_iff at hk,
specialize hn₁ k hk.1,
specialize hn₂ k hk.2,
specialize h₁ k,
specialize h₂ k,
rw abs_le at *,
split,
show -ε ≤ v k - l, by calc
-ε ≤ u k - l : by linarith
... ≤ v k - l : by linarith,
show v k - l ≤ ε, by calc
v k - l ≤ w k - l : by linarith
... ≤ ε : by linarith,
end
/- What about < ε? -/
-- 0036
example (u l) : seq_limit u l ↔
∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| < ε :=
begin
split,
{ intros h ε ε_pos,
cases h (ε/2) (by linarith) with n hn,
use n,
intros m hm,
specialize hn m hm,
linarith },
{ intros h ε ε_pos,
cases h ε ε_pos with n hn,
use n,
intros m hm,
specialize hn m hm,
linarith }
end
/- In the next exercise, we'll use
eq_of_abs_sub_le_all (x y : ℝ) : (∀ ε > 0, |x - y| ≤ ε) → x = y
-/
-- A sequence admits at most one limit
-- 0037
example : seq_limit u l → seq_limit u l' → l = l' :=
begin
intros h h',
apply eq_of_abs_sub_le_all,
intros ε ε_pos,
cases h (ε/2) (by linarith) with n h,
cases h' (ε/2) (by linarith) with n' h',
specialize h (max n n') (le_max_left _ _),
specialize h' (max n n') (le_max_right _ _),
rw abs_le at *,
split; linarith
end
/-
Let's now practice deciphering definitions before proving.
-/
def non_decreasing (u : ℕ → ℝ) := ∀ n m, n ≤ m → u n ≤ u m
def is_seq_sup (M : ℝ) (u : ℕ → ℝ) :=
(∀ n, u n ≤ M) ∧ ∀ ε > 0, ∃ n₀, u n₀ ≥ M - ε
-- 0038
example (M : ℝ) (h : is_seq_sup M u) (h' : non_decreasing u) :
seq_limit u M :=
begin
intros ε ε_pos,
cases h with hM hu,
cases hu ε ε_pos with N hN,
use N,
intros n hn,
apply abs_le_of_le_of_neg_le,
{ apply sub_le_of_sub_le,
rw sub_le_iff_le_add,
specialize hM n,
linarith },
{ specialize h' N n hn,
linarith }
end
|
bba40451be75d1b6cd667d30ef164484d38641b9 | 87a08a8e9b222ec02f3327dca4ae24590c1b3de9 | /src/topology/stone_cech.lean | 6c3f628c83df257fbb8677e4565d5e8bcfa9a651 | [
"Apache-2.0"
] | permissive | naussicaa/mathlib | 86d05223517a39e80920549a8052f9cf0e0b77b8 | 1ef2c2df20cf45c21675d855436228c7ae02d47a | refs/heads/master | 1,592,104,950,080 | 1,562,073,069,000 | 1,562,073,069,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,793 | lean | /-
Copyright (c) 2018 Reid Barton. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Reid Barton
Construction of the Stone-Čech compactification using ultrafilters.
Parts of the formalization are based on "Ultrafilters and Topology"
by Marius Stekelenburg, particularly section 5.
-/
import topology.constructions
noncomputable theory
open filter lattice set
universes u v
section ultrafilter
/- The set of ultrafilters on α carries a natural topology which makes
it the Stone-Čech compactification of α (viewed as a discrete space). -/
/-- Basis for the topology on `ultrafilter α`. -/
def ultrafilter_basis (α : Type u) : set (set (ultrafilter α)) :=
{t | ∃ (s : set α), t = {u | s ∈ u.val}}
variables {α : Type u}
instance : topological_space (ultrafilter α) :=
topological_space.generate_from (ultrafilter_basis α)
lemma ultrafilter_basis_is_basis :
topological_space.is_topological_basis (ultrafilter_basis α) :=
⟨begin
rintros _ ⟨a, rfl⟩ _ ⟨b, rfl⟩ u ⟨ua, ub⟩,
refine ⟨_, ⟨a ∩ b, rfl⟩, u.val.inter_sets ua ub, assume v hv, ⟨_, _⟩⟩;
apply v.val.sets_of_superset hv; simp
end,
eq_univ_of_univ_subset $ subset_sUnion_of_mem $
⟨univ, eq.symm (eq_univ_of_forall (λ u, u.val.univ_sets))⟩,
rfl⟩
/-- The basic open sets for the topology on ultrafilters are open. -/
lemma ultrafilter_is_open_basic (s : set α) :
is_open {u : ultrafilter α | s ∈ u.val} :=
topological_space.is_open_of_is_topological_basis ultrafilter_basis_is_basis ⟨s, rfl⟩
/-- The basic open sets for the topology on ultrafilters are also closed. -/
lemma ultrafilter_is_closed_basic (s : set α) :
is_closed {u : ultrafilter α | s ∈ u.val} :=
begin
change is_open (- _),
convert ultrafilter_is_open_basic (-s),
ext u,
exact (ultrafilter_iff_compl_mem_iff_not_mem.mp u.property s).symm
end
/-- Every ultrafilter `u` on `ultrafilter α` converges to a unique
point of `ultrafilter α`, namely `mjoin u`. -/
lemma ultrafilter_converges_iff {u : ultrafilter (ultrafilter α)} {x : ultrafilter α} :
u.val ≤ nhds x ↔ x = mjoin u :=
begin
rw [eq_comm, ultrafilter.eq_iff_val_le_val],
change u.val ≤ nhds x ↔ x.val.sets ⊆ {a | {v : ultrafilter α | a ∈ v.val} ∈ u.val},
simp only [topological_space.nhds_generate_from, lattice.le_infi_iff, ultrafilter_basis,
le_principal_iff],
split; intro h,
{ intros a ha, exact h _ ⟨ha, a, rfl⟩ },
{ rintros _ ⟨xi, a, rfl⟩, exact h xi }
end
instance ultrafilter_compact : compact_space (ultrafilter α) :=
⟨compact_iff_ultrafilter_le_nhds.mpr $ assume f uf _,
⟨mjoin ⟨f, uf⟩, trivial, ultrafilter_converges_iff.mpr rfl⟩⟩
instance ultrafilter.t2_space : t2_space (ultrafilter α) :=
t2_iff_ultrafilter.mpr $ assume f x y u fx fy,
have hx : x = mjoin ⟨f, u⟩, from ultrafilter_converges_iff.mp fx,
have hy : y = mjoin ⟨f, u⟩, from ultrafilter_converges_iff.mp fy,
hx.trans hy.symm
lemma ultrafilter_comap_pure_nhds (b : ultrafilter α) : comap pure (nhds b) ≤ b.val :=
begin
rw topological_space.nhds_generate_from,
simp only [comap_infi, comap_principal],
intros s hs,
rw ←le_principal_iff,
refine lattice.infi_le_of_le {u | s ∈ u.val} _,
refine lattice.infi_le_of_le ⟨hs, ⟨s, rfl⟩⟩ _,
rw principal_mono,
intros a ha,
exact mem_pure_iff.mp ha
end
section embedding
lemma ultrafilter_pure_injective : function.injective (pure : α → ultrafilter α) :=
begin
intros x y h,
have : {x} ∈ (pure x : ultrafilter α).val := singleton_mem_pure_sets,
rw h at this,
exact (mem_singleton_iff.mp (mem_pure_sets.mp this)).symm
end
open topological_space
/-- `pure : α → ultrafilter α` defines a dense embedding of `α` in `ultrafilter α`. -/
lemma dense_embedding_pure : @dense_embedding _ _ ⊥ _ (pure : α → ultrafilter α) :=
by letI : topological_space α := ⊥; exact
dense_embedding.mk' pure continuous_bot
(assume x, mem_closure_iff_ultrafilter.mpr
⟨x.map ultrafilter.pure, range_mem_map,
ultrafilter_converges_iff.mpr (bind_pure x).symm⟩)
ultrafilter_pure_injective
(assume a s as,
⟨{u | s ∈ u.val},
mem_nhds_sets (ultrafilter_is_open_basic s) (mem_pure_sets.mpr (mem_of_nhds as)),
assume b hb, mem_pure_sets.mp hb⟩)
end embedding
section extension
/- Goal: Any function `α → γ` to a compact Hausdorff space `γ` has a
unique extension to a continuous function `ultrafilter α → γ`. We
already know it must be unique because `α → ultrafilter α` is a
dense embedding and `γ` is Hausdorff. For existence, we will invoke
`dense_embedding.continuous_extend`. -/
variables {γ : Type*} [topological_space γ]
/-- The extension of a function `α → γ` to a function `ultrafilter α → γ`.
When `γ` is a compact Hausdorff space it will be continuous. -/
def ultrafilter.extend (f : α → γ) : ultrafilter α → γ :=
by letI : topological_space α := ⊥; exact dense_embedding_pure.extend f
lemma ultrafilter_extend_extends (f : α → γ) : ultrafilter.extend f ∘ pure = f :=
by letI : topological_space α := ⊥; exact funext dense_embedding_pure.extend_e_eq
variables [t2_space γ] [compact_space γ]
lemma continuous_ultrafilter_extend (f : α → γ) : continuous (ultrafilter.extend f) :=
have ∀ (b : ultrafilter α), ∃ c, tendsto f (comap ultrafilter.pure (nhds b)) (nhds c) := assume b,
-- b.map f is an ultrafilter on γ, which is compact, so it converges to some c in γ.
let ⟨c, _, h⟩ := compact_iff_ultrafilter_le_nhds.mp compact_univ (b.map f).val (b.map f).property
(by rw [le_principal_iff]; exact univ_mem_sets) in
⟨c, le_trans (map_mono (ultrafilter_comap_pure_nhds _)) h⟩,
begin
letI : topological_space α := ⊥,
letI : normal_space γ := normal_of_compact_t2,
exact dense_embedding_pure.continuous_extend this
end
/-- The value of `ultrafilter.extend f` on an ultrafilter `b` is the
unique limit of the ultrafilter `b.map f` in `γ`. -/
lemma ultrafilter_extend_eq_iff {f : α → γ} {b : ultrafilter α} {c : γ} :
ultrafilter.extend f b = c ↔ b.val.map f ≤ nhds c :=
⟨assume h, begin
-- Write b as an ultrafilter limit of pure ultrafilters, and use
-- the facts that ultrafilter.extend is a continuous extension of f.
let b' : ultrafilter (ultrafilter α) := b.map pure,
have t : b'.val ≤ nhds b,
from ultrafilter_converges_iff.mpr (by exact (bind_pure _).symm),
rw ←h,
have := (continuous_ultrafilter_extend f).tendsto b,
refine le_trans _ (le_trans (map_mono t) this),
change _ ≤ map (ultrafilter.extend f ∘ pure) b.val,
rw ultrafilter_extend_extends,
exact le_refl _
end,
assume h, by letI : topological_space α := ⊥; exact
dense_embedding_pure.extend_eq (le_trans (map_mono (ultrafilter_comap_pure_nhds _)) h)⟩
end extension
end ultrafilter
section stone_cech
/- Now, we start with a (not necessarily discrete) topological space α
and we want to construct its Stone-Čech compactification. We can
build it as a quotient of `ultrafilter α` by the relation which
identifies two points if the extension of every continuous function
α → γ to a compact Hausdorff space sends the two points to the same
point of γ. -/
variables (α : Type u) [topological_space α]
instance stone_cech_setoid : setoid (ultrafilter α) :=
{ r := λ x y, ∀ (γ : Type u) [topological_space γ], by exactI
∀ [t2_space γ] [compact_space γ] (f : α → γ) (hf : continuous f),
ultrafilter.extend f x = ultrafilter.extend f y,
iseqv :=
⟨assume x γ tγ h₁ h₂ f hf, rfl,
assume x y xy γ tγ h₁ h₂ f hf, by exactI (xy γ f hf).symm,
assume x y z xy yz γ tγ h₁ h₂ f hf, by exactI (xy γ f hf).trans (yz γ f hf)⟩ }
/-- The Stone-Čech compactification of a topological space. -/
def stone_cech : Type u := quotient (stone_cech_setoid α)
variables {α}
instance : topological_space (stone_cech α) := by unfold stone_cech; apply_instance
/-- The natural map from α to its Stone-Čech compactification. -/
def stone_cech_unit (x : α) : stone_cech α := ⟦pure x⟧
/-- The image of stone_cech_unit is dense. (But stone_cech_unit need
not be an embedding, for example if α is not Hausdorff.) -/
lemma stone_cech_unit_dense : closure (range (@stone_cech_unit α _)) = univ :=
begin
convert quotient_dense_of_dense (eq_univ_iff_forall.mp dense_embedding_pure.closure_range),
rw [←range_comp], refl
end
section extension
variables {γ : Type u} [topological_space γ] [t2_space γ] [compact_space γ]
variables {f : α → γ} (hf : continuous f)
local attribute [elab_with_expected_type] quotient.lift
/-- The extension of a continuous function from α to a compact
Hausdorff space γ to the Stone-Čech compactification of α. -/
def stone_cech_extend : stone_cech α → γ :=
quotient.lift (ultrafilter.extend f) (λ x y xy, xy γ f hf)
lemma stone_cech_extend_extends : stone_cech_extend hf ∘ stone_cech_unit = f :=
ultrafilter_extend_extends f
lemma continuous_stone_cech_extend : continuous (stone_cech_extend hf) :=
continuous_quot_lift _ (continuous_ultrafilter_extend f)
end extension
lemma convergent_eqv_pure {u : ultrafilter α} {x : α} (ux : u.val ≤ nhds x) : u ≈ pure x :=
assume γ tγ h₁ h₂ f hf, begin
resetI,
transitivity f x, swap, symmetry,
all_goals { refine ultrafilter_extend_eq_iff.mpr (le_trans (map_mono _) (hf.tendsto _)) },
{ apply pure_le_nhds }, { exact ux }
end
lemma continuous_stone_cech_unit : continuous (stone_cech_unit : α → stone_cech α) :=
continuous_iff_ultrafilter.mpr $ λ x g u gx,
let g' : ultrafilter α := ⟨g, u⟩ in
have (g'.map ultrafilter.pure).val ≤ nhds g',
by rw ultrafilter_converges_iff; exact (bind_pure _).symm,
have (g'.map stone_cech_unit).val ≤ nhds ⟦g'⟧, from
(continuous_at_iff_ultrafilter g').mp
(continuous_quotient_mk.tendsto g') _ (ultrafilter_map u) this,
by rwa (show ⟦g'⟧ = ⟦pure x⟧, from quotient.sound $ convergent_eqv_pure gx) at this
instance stone_cech.t2_space : t2_space (stone_cech α) :=
begin
rw t2_iff_ultrafilter,
rintros g ⟨x⟩ ⟨y⟩ u gx gy,
apply quotient.sound,
intros γ tγ h₁ h₂ f hf,
resetI,
let ff := stone_cech_extend hf,
change ff ⟦x⟧ = ff ⟦y⟧,
have lim : ∀ z : ultrafilter α, g ≤ nhds ⟦z⟧ → tendsto ff g (nhds (ff ⟦z⟧)) :=
assume z gz,
calc map ff g ≤ map ff (nhds ⟦z⟧) : map_mono gz
... ≤ nhds (ff ⟦z⟧) : (continuous_stone_cech_extend hf).tendsto _,
exact tendsto_nhds_unique u.1 (lim x gx) (lim y gy)
end
instance stone_cech.compact_space : compact_space (stone_cech α) :=
quotient.compact_space
end stone_cech
|
1a4ed02cbd8ea18b9131f4c5bf749159471c0518 | 137c667471a40116a7afd7261f030b30180468c2 | /src/analysis/normed_space/units.lean | 1f051533bec9c1a7aa833c754628e796f55fa1b8 | [
"Apache-2.0"
] | permissive | bragadeesh153/mathlib | 46bf814cfb1eecb34b5d1549b9117dc60f657792 | b577bb2cd1f96eb47031878256856020b76f73cd | refs/heads/master | 1,687,435,188,334 | 1,626,384,207,000 | 1,626,384,207,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 12,260 | lean | /-
Copyright (c) 2020 Heather Macbeth. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Heather Macbeth
-/
import analysis.specific_limits
import analysis.asymptotics.asymptotics
/-!
# The group of units of a complete normed ring
This file contains the basic theory for the group of units (invertible elements) of a complete
normed ring (Banach algebras being a notable special case).
## Main results
The constructions `one_sub`, `add` and `unit_of_nearby` state, in varying forms, that perturbations
of a unit are units. The latter two are not stated in their optimal form; more precise versions
would use the spectral radius.
The first main result is `is_open`: the group of units of a complete normed ring is an open subset
of the ring.
The function `inverse` (defined in `algebra.ring`), for a ring `R`, sends `a : R` to `a⁻¹` if `a` is
a unit and 0 if not. The other major results of this file (notably `inverse_add`,
`inverse_add_norm` and `inverse_add_norm_diff_nth_order`) cover the asymptotic properties of
`inverse (x + t)` as `t → 0`.
-/
noncomputable theory
open_locale topological_space
variables {R : Type*} [normed_ring R] [complete_space R]
namespace units
/-- In a complete normed ring, a perturbation of `1` by an element `t` of distance less than `1`
from `1` is a unit. Here we construct its `units` structure. -/
@[simps coe]
def one_sub (t : R) (h : ∥t∥ < 1) : units R :=
{ val := 1 - t,
inv := ∑' n : ℕ, t ^ n,
val_inv := mul_neg_geom_series t h,
inv_val := geom_series_mul_neg t h }
/-- In a complete normed ring, a perturbation of a unit `x` by an element `t` of distance less than
`∥x⁻¹∥⁻¹` from `x` is a unit. Here we construct its `units` structure. -/
@[simps coe]
def add (x : units R) (t : R) (h : ∥t∥ < ∥(↑x⁻¹ : R)∥⁻¹) : units R :=
units.copy -- to make `coe_add` true definitionally, for convenience
(x * (units.one_sub (-(↑x⁻¹ * t)) begin
nontriviality R using [zero_lt_one],
have hpos : 0 < ∥(↑x⁻¹ : R)∥ := units.norm_pos x⁻¹,
calc ∥-(↑x⁻¹ * t)∥
= ∥↑x⁻¹ * t∥ : by { rw norm_neg }
... ≤ ∥(↑x⁻¹ : R)∥ * ∥t∥ : norm_mul_le ↑x⁻¹ _
... < ∥(↑x⁻¹ : R)∥ * ∥(↑x⁻¹ : R)∥⁻¹ : by nlinarith only [h, hpos]
... = 1 : mul_inv_cancel (ne_of_gt hpos)
end))
(x + t) (by simp [mul_add]) _ rfl
/-- In a complete normed ring, an element `y` of distance less than `∥x⁻¹∥⁻¹` from `x` is a unit.
Here we construct its `units` structure. -/
@[simps coe]
def unit_of_nearby (x : units R) (y : R) (h : ∥y - x∥ < ∥(↑x⁻¹ : R)∥⁻¹) : units R :=
units.copy (x.add (y - x : R) h) y (by simp) _ rfl
/-- The group of units of a complete normed ring is an open subset of the ring. -/
protected lemma is_open : is_open {x : R | is_unit x} :=
begin
nontriviality R,
apply metric.is_open_iff.mpr,
rintros x' ⟨x, rfl⟩,
refine ⟨∥(↑x⁻¹ : R)∥⁻¹, inv_pos.mpr (units.norm_pos x⁻¹), _⟩,
intros y hy,
rw [metric.mem_ball, dist_eq_norm] at hy,
exact (x.unit_of_nearby y hy).is_unit
end
protected lemma nhds (x : units R) : {x : R | is_unit x} ∈ 𝓝 (x : R) :=
is_open.mem_nhds units.is_open x.is_unit
end units
namespace normed_ring
open_locale classical big_operators
open asymptotics filter metric finset ring
lemma inverse_one_sub (t : R) (h : ∥t∥ < 1) : inverse (1 - t) = ↑(units.one_sub t h)⁻¹ :=
by rw [← inverse_unit (units.one_sub t h), units.coe_one_sub]
/-- The formula `inverse (x + t) = inverse (1 + x⁻¹ * t) * x⁻¹` holds for `t` sufficiently small. -/
lemma inverse_add (x : units R) :
∀ᶠ t in (𝓝 0), inverse ((x : R) + t) = inverse (1 + ↑x⁻¹ * t) * ↑x⁻¹ :=
begin
nontriviality R,
rw [eventually_iff, metric.mem_nhds_iff],
have hinv : 0 < ∥(↑x⁻¹ : R)∥⁻¹, by cancel_denoms,
use [∥(↑x⁻¹ : R)∥⁻¹, hinv],
intros t ht,
simp only [mem_ball, dist_zero_right] at ht,
have ht' : ∥-↑x⁻¹ * t∥ < 1,
{ refine lt_of_le_of_lt (norm_mul_le _ _) _,
rw norm_neg,
refine lt_of_lt_of_le (mul_lt_mul_of_pos_left ht x⁻¹.norm_pos) _,
cancel_denoms },
have hright := inverse_one_sub (-↑x⁻¹ * t) ht',
have hleft := inverse_unit (x.add t ht),
simp only [← neg_mul_eq_neg_mul, sub_neg_eq_add] at hright,
simp only [units.coe_add] at hleft,
simp [hleft, hright, units.add]
end
lemma inverse_one_sub_nth_order (n : ℕ) :
∀ᶠ t in (𝓝 0), inverse ((1:R) - t) = (∑ i in range n, t ^ i) + (t ^ n) * inverse (1 - t) :=
begin
simp only [eventually_iff, metric.mem_nhds_iff],
use [1, by norm_num],
intros t ht,
simp only [mem_ball, dist_zero_right] at ht,
simp only [inverse_one_sub t ht, set.mem_set_of_eq],
have h : 1 = ((range n).sum (λ i, t ^ i)) * (units.one_sub t ht) + t ^ n,
{ simp only [units.coe_one_sub],
rw [← geom_sum, geom_sum_mul_neg],
simp },
rw [← one_mul ↑(units.one_sub t ht)⁻¹, h, add_mul],
congr,
{ rw [mul_assoc, (units.one_sub t ht).mul_inv],
simp },
{ simp only [units.coe_one_sub],
rw [← add_mul, ← geom_sum, geom_sum_mul_neg],
simp }
end
/-- The formula
`inverse (x + t) = (∑ i in range n, (- x⁻¹ * t) ^ i) * x⁻¹ + (- x⁻¹ * t) ^ n * inverse (x + t)`
holds for `t` sufficiently small. -/
lemma inverse_add_nth_order (x : units R) (n : ℕ) :
∀ᶠ t in (𝓝 0), inverse ((x : R) + t)
= (∑ i in range n, (- ↑x⁻¹ * t) ^ i) * ↑x⁻¹ + (- ↑x⁻¹ * t) ^ n * inverse (x + t) :=
begin
refine (inverse_add x).mp _,
have hzero : tendsto (λ (t : R), - ↑x⁻¹ * t) (𝓝 0) (𝓝 0),
{ convert ((mul_left_continuous (- (↑x⁻¹ : R))).tendsto 0).comp tendsto_id,
simp },
refine (hzero.eventually (inverse_one_sub_nth_order n)).mp (eventually_of_forall _),
simp only [neg_mul_eq_neg_mul_symm, sub_neg_eq_add],
intros t h1 h2,
have h := congr_arg (λ (a : R), a * ↑x⁻¹) h1,
dsimp at h,
convert h,
rw [add_mul, mul_assoc],
simp [h2.symm]
end
lemma inverse_one_sub_norm : is_O (λ t, inverse ((1:R) - t)) (λ t, (1:ℝ)) (𝓝 (0:R)) :=
begin
simp only [is_O, is_O_with, eventually_iff, metric.mem_nhds_iff],
refine ⟨∥(1:R)∥ + 1, (2:ℝ)⁻¹, by norm_num, _⟩,
intros t ht,
simp only [ball, dist_zero_right, set.mem_set_of_eq] at ht,
have ht' : ∥t∥ < 1,
{ have : (2:ℝ)⁻¹ < 1 := by cancel_denoms,
linarith },
simp only [inverse_one_sub t ht', norm_one, mul_one, set.mem_set_of_eq],
change ∥∑' n : ℕ, t ^ n∥ ≤ _,
have := normed_ring.tsum_geometric_of_norm_lt_1 t ht',
have : (1 - ∥t∥)⁻¹ ≤ 2,
{ rw ← inv_inv' (2:ℝ),
refine inv_le_inv_of_le (by norm_num) _,
have : (2:ℝ)⁻¹ + (2:ℝ)⁻¹ = 1 := by ring,
linarith },
linarith
end
/-- The function `λ t, inverse (x + t)` is O(1) as `t → 0`. -/
lemma inverse_add_norm (x : units R) : is_O (λ t, inverse (↑x + t)) (λ t, (1:ℝ)) (𝓝 (0:R)) :=
begin
nontriviality R,
simp only [is_O_iff, norm_one, mul_one],
cases is_O_iff.mp (@inverse_one_sub_norm R _ _) with C hC,
use C * ∥((x⁻¹:units R):R)∥,
have hzero : tendsto (λ t, - (↑x⁻¹ : R) * t) (𝓝 0) (𝓝 0),
{ convert ((mul_left_continuous (-↑x⁻¹ : R)).tendsto 0).comp tendsto_id,
simp },
refine (inverse_add x).mp ((hzero.eventually hC).mp (eventually_of_forall _)),
intros t bound iden,
rw iden,
simp at bound,
have hmul := norm_mul_le (inverse (1 + ↑x⁻¹ * t)) ↑x⁻¹,
nlinarith [norm_nonneg (↑x⁻¹ : R)]
end
/-- The function
`λ t, inverse (x + t) - (∑ i in range n, (- x⁻¹ * t) ^ i) * x⁻¹`
is `O(t ^ n)` as `t → 0`. -/
lemma inverse_add_norm_diff_nth_order (x : units R) (n : ℕ) :
is_O (λ (t : R), inverse (↑x + t) - (∑ i in range n, (- ↑x⁻¹ * t) ^ i) * ↑x⁻¹)
(λ t, ∥t∥ ^ n) (𝓝 (0:R)) :=
begin
by_cases h : n = 0,
{ simpa [h] using inverse_add_norm x },
have hn : 0 < n := nat.pos_of_ne_zero h,
simp [is_O_iff],
cases (is_O_iff.mp (inverse_add_norm x)) with C hC,
use C * ∥(1:ℝ)∥ * ∥(↑x⁻¹ : R)∥ ^ n,
have h : eventually_eq (𝓝 (0:R))
(λ t, inverse (↑x + t) - (∑ i in range n, (- ↑x⁻¹ * t) ^ i) * ↑x⁻¹)
(λ t, ((- ↑x⁻¹ * t) ^ n) * inverse (x + t)),
{ refine (inverse_add_nth_order x n).mp (eventually_of_forall _),
intros t ht,
convert congr_arg (λ a, a - (range n).sum (pow (-↑x⁻¹ * t)) * ↑x⁻¹) ht,
simp },
refine h.mp (hC.mp (eventually_of_forall _)),
intros t _ hLHS,
simp only [neg_mul_eq_neg_mul_symm] at hLHS,
rw hLHS,
refine le_trans (norm_mul_le _ _ ) _,
have h' : ∥(-(↑x⁻¹ * t)) ^ n∥ ≤ ∥(↑x⁻¹ : R)∥ ^ n * ∥t∥ ^ n,
{ calc ∥(-(↑x⁻¹ * t)) ^ n∥ ≤ ∥(-(↑x⁻¹ * t))∥ ^ n : norm_pow_le' _ hn
... = ∥↑x⁻¹ * t∥ ^ n : by rw norm_neg
... ≤ (∥(↑x⁻¹ : R)∥ * ∥t∥) ^ n : _
... = ∥(↑x⁻¹ : R)∥ ^ n * ∥t∥ ^ n : mul_pow _ _ n,
exact pow_le_pow_of_le_left (norm_nonneg _) (norm_mul_le ↑x⁻¹ t) n },
have h'' : 0 ≤ ∥(↑x⁻¹ : R)∥ ^ n * ∥t∥ ^ n,
{ refine mul_nonneg _ _;
exact pow_nonneg (norm_nonneg _) n },
nlinarith [norm_nonneg (inverse (↑x + t))],
end
/-- The function `λ t, inverse (x + t) - x⁻¹` is `O(t)` as `t → 0`. -/
lemma inverse_add_norm_diff_first_order (x : units R) :
is_O (λ t, inverse (↑x + t) - ↑x⁻¹) (λ t, ∥t∥) (𝓝 (0:R)) :=
by { convert inverse_add_norm_diff_nth_order x 1; simp }
/-- The function
`λ t, inverse (x + t) - x⁻¹ + x⁻¹ * t * x⁻¹`
is `O(t ^ 2)` as `t → 0`. -/
lemma inverse_add_norm_diff_second_order (x : units R) :
is_O (λ t, inverse (↑x + t) - ↑x⁻¹ + ↑x⁻¹ * t * ↑x⁻¹) (λ t, ∥t∥ ^ 2) (𝓝 (0:R)) :=
begin
convert inverse_add_norm_diff_nth_order x 2,
ext t,
simp only [range_succ, range_one, sum_insert, mem_singleton, sum_singleton, not_false_iff,
one_ne_zero, pow_zero, add_mul, pow_one, one_mul, neg_mul_eq_neg_mul_symm,
sub_add_eq_sub_sub_swap, sub_neg_eq_add],
end
/-- The function `inverse` is continuous at each unit of `R`. -/
lemma inverse_continuous_at (x : units R) : continuous_at inverse (x : R) :=
begin
have h_is_o : is_o (λ (t : R), ∥inverse (↑x + t) - ↑x⁻¹∥) (λ (t : R), (1:ℝ)) (𝓝 0),
{ refine is_o_norm_left.mpr ((inverse_add_norm_diff_first_order x).trans_is_o _),
exact is_o_norm_left.mpr (is_o_id_const one_ne_zero) },
have h_lim : tendsto (λ (y:R), y - x) (𝓝 x) (𝓝 0),
{ refine tendsto_zero_iff_norm_tendsto_zero.mpr _,
exact tendsto_iff_norm_tendsto_zero.mp tendsto_id },
simp only [continuous_at],
rw [tendsto_iff_norm_tendsto_zero, inverse_unit],
convert h_is_o.tendsto_0.comp h_lim,
ext, simp
end
end normed_ring
namespace units
open opposite filter normed_ring
/-- In a normed ring, the coercion from `units R` (equipped with the induced topology from the
embedding in `R × R`) to `R` is an open map. -/
lemma is_open_map_coe : is_open_map (coe : units R → R) :=
begin
rw is_open_map_iff_nhds_le,
intros x s,
rw [mem_map, mem_nhds_induced],
rintros ⟨t, ht, hts⟩,
obtain ⟨u, hu, v, hv, huvt⟩ :
∃ (u : set R), u ∈ 𝓝 ↑x ∧ ∃ (v : set Rᵒᵖ), v ∈ 𝓝 (opposite.op ↑x⁻¹) ∧ u.prod v ⊆ t,
{ simpa [embed_product, mem_nhds_prod_iff] using ht },
have : u ∩ (op ∘ ring.inverse) ⁻¹' v ∩ (set.range (coe : units R → R)) ∈ 𝓝 ↑x,
{ refine inter_mem_sets (inter_mem_sets hu _) (units.nhds x),
refine (continuous_op.continuous_at.comp (inverse_continuous_at x)).preimage_mem_nhds _,
simpa using hv },
refine mem_sets_of_superset this _,
rintros _ ⟨⟨huy, hvy⟩, ⟨y, rfl⟩⟩,
have : embed_product R y ∈ u.prod v := ⟨huy, by simpa using hvy⟩,
simpa using hts (huvt this)
end
/-- In a normed ring, the coercion from `units R` (equipped with the induced topology from the
embedding in `R × R`) to `R` is an open embedding. -/
lemma open_embedding_coe : open_embedding (coe : units R → R) :=
open_embedding_of_continuous_injective_open continuous_coe ext is_open_map_coe
end units
|
6565937b7ba9bc7aee03364b676beb2583c5b202 | 64874bd1010548c7f5a6e3e8902efa63baaff785 | /hott/init/function.hlean | eb2af4e3df7daca6b5c0e5324aee8852cee826f0 | [
"Apache-2.0"
] | permissive | tjiaqi/lean | 4634d729795c164664d10d093f3545287c76628f | d0ce4cf62f4246b0600c07e074d86e51f2195e30 | refs/heads/master | 1,622,323,796,480 | 1,422,643,069,000 | 1,422,643,069,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,485 | hlean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
General operations on functions.
-/
prelude
import init.reserved_notation
namespace function
variables {A : Type} {B : Type} {C : Type} {D : Type} {E : Type}
definition compose [reducible] (f : B → C) (g : A → B) : A → C :=
λx, f (g x)
definition id [reducible] (a : A) : A :=
a
definition on_fun (f : B → B → C) (g : A → B) : A → A → C :=
λx y, f (g x) (g y)
definition combine (f : A → B → C) (op : C → D → E) (g : A → B → D) : A → B → E :=
λx y, op (f x y) (g x y)
definition const {A : Type} (B : Type) (a : A) : B → A :=
λx, a
definition dcompose {A : Type} {B : A → Type} {C : Π {x : A}, B x → Type}
(f : Π {x : A} (y : B x), C y) (g : Πx, B x) : Πx, C (g x) :=
λx, f (g x)
definition flip {A : Type} {B : Type} {C : A → B → Type} (f : Πx y, C x y) : Πy x, C x y :=
λy x, f x y
definition app {A : Type} {B : A → Type} (f : Πx, B x) (x : A) : B x :=
f x
precedence `∘'`:60
precedence `on`:1
precedence `$`:1
variables {f g : A → B}
infixr ∘ := compose
infixr ∘' := dcompose
infixl on := on_fun
infixr $ := app
notation f `-[` op `]-` g := combine f op g
-- Trick for using any binary function as infix operator
notation a `⟨` f `⟩` b := f a b
end function
|
e27b597adde27d4bc361a86b564c6738d6e80685 | 64874bd1010548c7f5a6e3e8902efa63baaff785 | /tests/lean/unzip_error.lean | 183e26ada0de3897f42451a92e133d397aeda3f9 | [
"Apache-2.0"
] | permissive | tjiaqi/lean | 4634d729795c164664d10d093f3545287c76628f | d0ce4cf62f4246b0600c07e074d86e51f2195e30 | refs/heads/master | 1,622,323,796,480 | 1,422,643,069,000 | 1,422,643,069,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 353 | lean | import data.vector
open nat vector prod
variables {A B : Type}
definition unzip : Π {n : nat}, vector (A × B) n → vector A n × vector B n,
unzip nil := (nil, nil),
unzip ((a, b) :: v) :=
match unzip v with
(va, vb) := (a :: va, b :: vb)
end
example : unzip ((1, 20) :: (2, 30) :: nil) = (1 :: 2 :: nil, 20 :: 30 :: nil) :=
rfl
|
2db11fffb306b856110b6f59fd967004cc8d19a2 | f3849be5d845a1cb97680f0bbbe03b85518312f0 | /tests/lean/run/record1.lean | ebc730e5b3532333acc4180f0f04ea68cec64487 | [
"Apache-2.0"
] | permissive | bjoeris/lean | 0ed95125d762b17bfcb54dad1f9721f953f92eeb | 4e496b78d5e73545fa4f9a807155113d8e6b0561 | refs/heads/master | 1,611,251,218,281 | 1,495,337,658,000 | 1,495,337,658,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 786 | lean | structure point (A : Type) (B : Type) :=
mk :: (x : A) (y : B)
#check point
#check @ point.rec
#check point.mk
#check point.x
#check point.y
#check point.rec_on
inductive color
| red | green | blue
structure color_point (A : Type) (B : Type) extends point A B :=
mk :: (c : color)
#check @color_point.rec_on
#check color_point.rec_on
#check color_point.to_point
section
variables a b : num
example : point.x (point.mk a b) = a :=
rfl
example : point.y (point.mk a b) = b :=
rfl
variables cc : color
example : {color_point . x := a, y := b, c := cc}.x = a :=
rfl
example : {color_point . x := a, y := b, c := cc}.y = b :=
rfl
example : {color_point . x := a, y := b, c := cc}.c = cc :=
rfl
example : {color_point . x := a, y := b, c := cc}.to_point = point.mk a b :=
rfl
end
|
36f7fb5e79a5b4bbd9e59718059c013090c8b1db | 367134ba5a65885e863bdc4507601606690974c1 | /src/data/real/conjugate_exponents.lean | 9a13b6d17e943baf7f43d9200c5737f7d3a13982 | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 3,686 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel, Yury Kudryashov
-/
import data.real.ennreal
/-!
# Real conjugate exponents
`p.is_conjugate_exponent q` registers the fact that the real numbers `p` and `q` are `> 1` and
satisfy `1/p + 1/q = 1`. This property shows up often in analysis, especially when dealing with
`L^p` spaces.
We make several basic facts available through dot notation in this situation.
We also introduce `p.conjugate_exponent` for `p / (p-1)`. When `p > 1`, it is conjugate to `p`.
-/
noncomputable theory
namespace real
/-- Two real exponents `p, q` are conjugate if they are `> 1` and satisfy the equality
`1/p + 1/q = 1`. This condition shows up in many theorems in analysis, notably related to `L^p`
norms. -/
structure is_conjugate_exponent (p q : ℝ) : Prop :=
(one_lt : 1 < p)
(inv_add_inv_conj : 1/p + 1/q = 1)
/-- The conjugate exponent of `p` is `q = p/(p-1)`, so that `1/p + 1/q = 1`. -/
def conjugate_exponent (p : ℝ) : ℝ := p/(p-1)
namespace is_conjugate_exponent
variables {p q : ℝ} (h : p.is_conjugate_exponent q)
include h
/- Register several non-vanishing results following from the fact that `p` has a conjugate exponent
`q`: many computations using these exponents require clearing out denominators, which can be done
with `field_simp` given a proof that these denominators are non-zero, so we record the most usual
ones. -/
lemma pos : 0 < p :=
lt_trans zero_lt_one h.one_lt
lemma nonneg : 0 ≤ p := le_of_lt h.pos
lemma ne_zero : p ≠ 0 :=
ne_of_gt h.pos
lemma sub_one_pos : 0 < p - 1 :=
sub_pos.2 h.one_lt
lemma sub_one_ne_zero : p - 1 ≠ 0 :=
ne_of_gt h.sub_one_pos
lemma one_div_pos : 0 < 1/p :=
one_div_pos.2 h.pos
lemma one_div_nonneg : 0 ≤ 1/p :=
le_of_lt h.one_div_pos
lemma one_div_ne_zero : 1/p ≠ 0 :=
ne_of_gt (h.one_div_pos)
lemma conj_eq : q = p/(p-1) :=
begin
have := h.inv_add_inv_conj,
rw [← eq_sub_iff_add_eq', one_div, inv_eq_iff] at this,
field_simp [← this, h.ne_zero]
end
lemma conjugate_eq : conjugate_exponent p = q := h.conj_eq.symm
lemma sub_one_mul_conj : (p - 1) * q = p :=
mul_comm q (p - 1) ▸ (eq_div_iff h.sub_one_ne_zero).1 h.conj_eq
lemma mul_eq_add : p * q = p + q :=
by simpa only [sub_mul, sub_eq_iff_eq_add, one_mul] using h.sub_one_mul_conj
@[symm] protected lemma symm : q.is_conjugate_exponent p :=
{ one_lt := by { rw [h.conj_eq], exact (one_lt_div h.sub_one_pos).mpr (sub_one_lt p) },
inv_add_inv_conj := by simpa [add_comm] using h.inv_add_inv_conj }
lemma one_lt_nnreal : 1 < nnreal.of_real p :=
begin
rw [←nnreal.of_real_one, nnreal.of_real_lt_of_real_iff h.pos],
exact h.one_lt,
end
lemma inv_add_inv_conj_nnreal : 1 / nnreal.of_real p + 1 / nnreal.of_real q = 1 :=
by rw [←nnreal.of_real_one, ←nnreal.of_real_div' h.nonneg, ←nnreal.of_real_div' h.symm.nonneg,
←nnreal.of_real_add h.one_div_nonneg h.symm.one_div_nonneg, h.inv_add_inv_conj]
lemma inv_add_inv_conj_ennreal : 1 / ennreal.of_real p + 1 / ennreal.of_real q = 1 :=
by rw [←ennreal.of_real_one, ←ennreal.of_real_div_of_pos h.pos,
←ennreal.of_real_div_of_pos h.symm.pos,
←ennreal.of_real_add h.one_div_nonneg h.symm.one_div_nonneg, h.inv_add_inv_conj]
end is_conjugate_exponent
lemma is_conjugate_exponent_iff {p q : ℝ} (h : 1 < p) :
p.is_conjugate_exponent q ↔ q = p/(p-1) :=
⟨λ H, H.conj_eq, λ H, ⟨h, by field_simp [H, ne_of_gt (lt_trans zero_lt_one h)]⟩⟩
lemma is_conjugate_exponent_conjugate_exponent {p : ℝ} (h : 1 < p) :
p.is_conjugate_exponent (conjugate_exponent p) :=
(is_conjugate_exponent_iff h).2 rfl
end real
|
299ea2b6bdca781ee003e8c35040f070335a75c4 | f3a5af2927397cf346ec0e24312bfff077f00425 | /src/game/world3/level2.lean | 3e7685768568b74a8f714dd292bbf387ee381494 | [
"Apache-2.0"
] | permissive | ImperialCollegeLondon/natural_number_game | 05c39e1586408cfb563d1a12e1085a90726ab655 | f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd | refs/heads/master | 1,688,570,964,990 | 1,636,908,242,000 | 1,636,908,242,000 | 195,403,790 | 277 | 84 | Apache-2.0 | 1,694,547,955,000 | 1,562,328,792,000 | Lean | UTF-8 | Lean | false | false | 795 | lean | import game.world3.level1 -- hide
import mynat.mul -- hide
namespace mynat -- hide
/-
# Multiplication World
## Level 2: `mul_one`
Remember that you can see everything you have proved so far about multiplication in
the drop-down box on the left (and that this list will grow as we proceed).
In this level we'll need to use
* `one_eq_succ_zero : 1 = succ(0)`
which was mentioned back in Addition World and
which will be a useful thing to rewrite right now, as we
begin to prove a couple of lemmas about how `1` behaves
with respect to multiplication.
-/
/- Lemma
For any natural number $m$, we have
$$ m \times 1 = m. $$
-/
lemma mul_one (m : mynat) : m * 1 = m :=
begin [nat_num_game]
rw one_eq_succ_zero,
rw mul_succ,
rw mul_zero,
rw zero_add,
refl
end
end mynat -- hide
|
d0591b5395272de8bd9f91a66abe846fd652a4f3 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/playground/pldi/do2.lean | ebba3165db6b09d3c44e07ef04bdd78c8f918707 | [
"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 | 912 | lean | open Lean
/-
inductive Syntax
| missing : Syntax
| node (kind : SyntaxNodeKind) (args : Array Syntax) : Syntax
| atom (info : SourceInfo) (val : String) : Syntax
| ident (info : SourceInfo) (rawVal : Substring) (val : Name) (preresolved : List (Name × List String)) : Syntax
-/
partial def expandHash : Syntax → StateT Bool MacroM Syntax
| Syntax.node k args =>
if k == `doHash then do set true; `(←MonadState.get)
else do
args ← args.mapM expandHash;
pure $ Syntax.node k args
| stx => pure stx
@[macro Lean.Parser.Term.do] def expandDo : Macro :=
fun stx => do
(stx, expanded) ← expandHash stx false;
if expanded then pure stx
else Macro.throwUnsupported
syntax:max [doHash] "#" : term
def tst : StateT (Nat × Nat) IO Unit := do
if #.1 == 0 then
IO.println "first field is zero"
else
IO.println "first field is not zero"
#eval tst.run' (1, 1)
#eval tst.run' (0, 1)
|
320a69d6021f4722e77c4d107259c26e47ef9cda | 26ac254ecb57ffcb886ff709cf018390161a9225 | /src/algebra/lie_algebra.lean | 16beef4aae4c45f1ff9c72e46eca1fea205f9072 | [
"Apache-2.0"
] | permissive | eric-wieser/mathlib | 42842584f584359bbe1fc8b88b3ff937c8acd72d | d0df6b81cd0920ad569158c06a3fd5abb9e63301 | refs/heads/master | 1,669,546,404,255 | 1,595,254,668,000 | 1,595,254,668,000 | 281,173,504 | 0 | 0 | Apache-2.0 | 1,595,263,582,000 | 1,595,263,581,000 | null | UTF-8 | Lean | false | false | 40,809 | lean | /-
Copyright (c) 2019 Oliver Nash. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Oliver Nash
-/
import ring_theory.algebra
import linear_algebra.linear_action
import linear_algebra.bilinear_form
import tactic.noncomm_ring
/-!
# Lie algebras
This file defines Lie rings, and Lie algebras over a commutative ring. It shows how these arise from
associative rings and algebras via the ring commutator. In particular it defines the Lie algebra
of endomorphisms of a module as well as of the algebra of square matrices over a commutative ring.
It also includes definitions of morphisms of Lie algebras, Lie subalgebras, Lie modules, Lie
submodules, and the quotient of a Lie algebra by an ideal.
## Notations
We introduce the notation ⁅x, y⁆ for the Lie bracket. Note that these are the Unicode "square with
quill" brackets rather than the usual square brackets.
We also introduce the notations L →ₗ⁅R⁆ L' for a morphism of Lie algebras over a commutative ring R,
and L →ₗ⁅⁆ L' for the same, when the ring is implicit.
## Implementation notes
Lie algebras are defined as modules with a compatible Lie ring structure, and thus are partially
unbundled. Since they extend Lie rings, these are also partially unbundled.
## References
* [N. Bourbaki, *Lie Groups and Lie Algebras, Chapters 1--3*][bourbaki1975]
## Tags
lie bracket, ring commutator, jacobi identity, lie ring, lie algebra
-/
universes u v w w₁
/--
A binary operation, intended use in Lie algebras and similar structures.
-/
class has_bracket (L : Type v) := (bracket : L → L → L)
notation `⁅`x`,` y`⁆` := has_bracket.bracket x y
/-- An Abelian Lie algebra is one in which all brackets vanish. Arguably this class belongs in the
`has_bracket` namespace but it seems much more user-friendly to compromise slightly and put it in
the `lie_algebra` namespace. -/
class lie_algebra.is_abelian (L : Type v) [has_bracket L] [has_zero L] : Prop :=
(abelian : ∀ (x y : L), ⁅x, y⁆ = 0)
namespace ring_commutator
variables {A : Type v} [ring A]
/--
The ring commutator captures the extent to which a ring is commutative. It is identically zero
exactly when the ring is commutative.
-/
def commutator (x y : A) := x*y - y*x
local notation `⁅`x`,` y`⁆` := commutator x y
@[simp] lemma add_left (x y z : A) :
⁅x + y, z⁆ = ⁅x, z⁆ + ⁅y, z⁆ :=
by simp [commutator, right_distrib, left_distrib, sub_eq_add_neg, add_comm, add_left_comm]
@[simp] lemma add_right (x y z : A) :
⁅z, x + y⁆ = ⁅z, x⁆ + ⁅z, y⁆ :=
by simp [commutator, right_distrib, left_distrib, sub_eq_add_neg, add_comm, add_left_comm]
@[simp] lemma alternate (x : A) :
⁅x, x⁆ = 0 :=
by simp [commutator]
lemma jacobi (x y z : A) :
⁅x, ⁅y, z⁆⁆ + ⁅y, ⁅z, x⁆⁆ + ⁅z, ⁅x, y⁆⁆ = 0 :=
by { unfold commutator, noncomm_ring, }
end ring_commutator
section prio
set_option default_priority 100 -- see Note [default priority]
/--
A Lie ring is an additive group with compatible product, known as the bracket, satisfying the
Jacobi identity. The bracket is not associative unless it is identically zero.
-/
@[protect_proj] class lie_ring (L : Type v) extends add_comm_group L, has_bracket L :=
(add_lie : ∀ (x y z : L), ⁅x + y, z⁆ = ⁅x, z⁆ + ⁅y, z⁆)
(lie_add : ∀ (x y z : L), ⁅z, x + y⁆ = ⁅z, x⁆ + ⁅z, y⁆)
(lie_self : ∀ (x : L), ⁅x, x⁆ = 0)
(jacobi : ∀ (x y z : L), ⁅x, ⁅y, z⁆⁆ + ⁅y, ⁅z, x⁆⁆ + ⁅z, ⁅x, y⁆⁆ = 0)
end prio
section lie_ring
variables {L : Type v} [lie_ring L]
@[simp] lemma add_lie (x y z : L) : ⁅x + y, z⁆ = ⁅x, z⁆ + ⁅y, z⁆ := lie_ring.add_lie x y z
@[simp] lemma lie_add (x y z : L) : ⁅z, x + y⁆ = ⁅z, x⁆ + ⁅z, y⁆ := lie_ring.lie_add x y z
@[simp] lemma lie_self (x : L) : ⁅x, x⁆ = 0 := lie_ring.lie_self x
@[simp] lemma lie_skew (x y : L) :
-⁅y, x⁆ = ⁅x, y⁆ :=
begin
symmetry,
rw [←sub_eq_zero_iff_eq, sub_neg_eq_add],
have H : ⁅x + y, x + y⁆ = 0, from lie_self _,
rw add_lie at H,
simpa using H,
end
@[simp] lemma lie_zero (x : L) :
⁅x, 0⁆ = 0 :=
begin
have H : ⁅x, 0⁆ + ⁅x, 0⁆ = ⁅x, 0⁆ + 0 := by { rw ←lie_add, simp, },
exact add_left_cancel H,
end
@[simp] lemma zero_lie (x : L) :
⁅0, x⁆ = 0 := by { rw [←lie_skew, lie_zero], simp, }
@[simp] lemma neg_lie (x y : L) :
⁅-x, y⁆ = -⁅x, y⁆ := by { rw [←sub_eq_zero_iff_eq, sub_neg_eq_add, ←add_lie], simp, }
@[simp] lemma lie_neg (x y : L) :
⁅x, -y⁆ = -⁅x, y⁆ := by { rw [←lie_skew, ←lie_skew], simp, }
@[simp] lemma gsmul_lie (x y : L) (n : ℤ) :
⁅n • x, y⁆ = n • ⁅x, y⁆ :=
add_monoid_hom.map_gsmul ⟨λ x, ⁅x, y⁆, zero_lie y, λ _ _, add_lie _ _ _⟩ _ _
@[simp] lemma lie_gsmul (x y : L) (n : ℤ) :
⁅x, n • y⁆ = n • ⁅x, y⁆ :=
begin
rw [←lie_skew, ←lie_skew x, gsmul_lie],
unfold has_scalar.smul, rw gsmul_neg,
end
/--
An associative ring gives rise to a Lie ring by taking the bracket to be the ring commutator.
-/
def lie_ring.of_associative_ring (A : Type v) [ring A] : lie_ring A :=
{ bracket := ring_commutator.commutator,
add_lie := ring_commutator.add_left,
lie_add := ring_commutator.add_right,
lie_self := ring_commutator.alternate,
jacobi := ring_commutator.jacobi }
local attribute [instance] lie_ring.of_associative_ring
lemma lie_ring.of_associative_ring_bracket (A : Type v) [ring A] (x y : A) :
⁅x, y⁆ = x*y - y*x := rfl
lemma commutative_ring_iff_abelian_lie_ring (A : Type v) [ring A] :
is_commutative A (*) ↔ lie_algebra.is_abelian A :=
begin
have h₁ : is_commutative A (*) ↔ ∀ (a b : A), a * b = b * a := ⟨λ h, h.1, λ h, ⟨h⟩⟩,
have h₂ : lie_algebra.is_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
end lie_ring
section prio
set_option default_priority 100 -- see Note [default priority]
/--
A Lie algebra is a module with compatible product, known as the bracket, satisfying the Jacobi
identity. Forgetting the scalar multiplication, every Lie algebra is a Lie ring.
-/
class lie_algebra (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] extends semimodule R L :=
(lie_smul : ∀ (t : R) (x y : L), ⁅x, t • y⁆ = t • ⁅x, y⁆)
end prio
@[simp] lemma lie_smul (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L]
(t : R) (x y : L) : ⁅x, t • y⁆ = t • ⁅x, y⁆ :=
lie_algebra.lie_smul t x y
@[simp] lemma smul_lie (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L]
(t : R) (x y : L) : ⁅t • x, y⁆ = t • ⁅x, y⁆ :=
by { rw [←lie_skew, ←lie_skew x y], simp [-lie_skew], }
namespace lie_algebra
set_option old_structure_cmd true
/-- A morphism of Lie algebras is a linear map respecting the bracket operations. -/
structure morphism (R : Type u) (L : Type v) (L' : Type w)
[comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L']
extends linear_map R L L' :=
(map_lie : ∀ {x y : L}, to_fun ⁅x, y⁆ = ⁅to_fun x, to_fun y⁆)
attribute [nolint doc_blame] lie_algebra.morphism.to_linear_map
infixr ` →ₗ⁅⁆ `:25 := morphism _
notation L ` →ₗ⁅`:25 R:25 `⁆ `:0 L':0 := morphism R L L'
section morphism_properties
variables {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁}
variables [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃]
variables [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃]
instance : has_coe (L₁ →ₗ⁅R⁆ L₂) (L₁ →ₗ[R] L₂) := ⟨morphism.to_linear_map⟩
/-- see Note [function coercion] -/
instance : has_coe_to_fun (L₁ →ₗ⁅R⁆ L₂) := ⟨_, morphism.to_fun⟩
@[simp] lemma map_lie (f : L₁ →ₗ⁅R⁆ L₂) (x y : L₁) : f ⁅x, y⁆ = ⁅f x, f y⁆ := morphism.map_lie f
/-- The constant 0 map is a Lie algebra morphism. -/
instance : has_zero (L₁ →ₗ⁅R⁆ L₂) := ⟨{ map_lie := by simp, ..(0 : L₁ →ₗ[R] L₂)}⟩
/-- The identity map is a Lie algebra morphism. -/
instance : has_one (L₁ →ₗ⁅R⁆ L₁) := ⟨{ map_lie := by simp, ..(1 : L₁ →ₗ[R] L₁)}⟩
instance : inhabited (L₁ →ₗ⁅R⁆ L₂) := ⟨0⟩
/-- The composition of morphisms is a morphism. -/
def morphism.comp (f : L₂ →ₗ⁅R⁆ L₃) (g : L₁ →ₗ⁅R⁆ L₂) : L₁ →ₗ⁅R⁆ L₃ :=
{ map_lie := λ x y, by { change f (g ⁅x, y⁆) = ⁅f (g x), f (g y)⁆, rw [map_lie, map_lie], },
..linear_map.comp f.to_linear_map g.to_linear_map }
lemma morphism.comp_apply (f : L₂ →ₗ⁅R⁆ L₃) (g : L₁ →ₗ⁅R⁆ L₂) (x : L₁) :
f.comp g x = f (g x) := rfl
/-- The inverse of a bijective morphism is a morphism. -/
def morphism.inverse (f : L₁ →ₗ⁅R⁆ L₂) (g : L₂ → L₁)
(h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : L₂ →ₗ⁅R⁆ L₁ :=
{ map_lie := λ x y, by {
calc g ⁅x, y⁆ = g ⁅f (g x), f (g y)⁆ : by { conv_lhs { rw [←h₂ x, ←h₂ y], }, }
... = g (f ⁅g x, g y⁆) : by rw map_lie
... = ⁅g x, g y⁆ : (h₁ _), },
..linear_map.inverse f.to_linear_map g h₁ h₂ }
end morphism_properties
/-- An equivalence of Lie algebras is a morphism which is also a linear equivalence. We could
instead define an equivalence to be a morphism which is also a (plain) equivalence. However it is
more convenient to define via linear equivalence to get `.to_linear_equiv` for free. -/
structure equiv (R : Type u) (L : Type v) (L' : Type w)
[comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L']
extends L →ₗ⁅R⁆ L', L ≃ₗ[R] L'
attribute [nolint doc_blame] lie_algebra.equiv.to_morphism
attribute [nolint doc_blame] lie_algebra.equiv.to_linear_equiv
notation L ` ≃ₗ⁅`:50 R `⁆ ` L' := equiv R L L'
namespace equiv
variables {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁}
variables [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃]
variables [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃]
instance has_coe_to_lie_hom : has_coe (L₁ ≃ₗ⁅R⁆ L₂) (L₁ →ₗ⁅R⁆ L₂) := ⟨to_morphism⟩
instance has_coe_to_linear_equiv : has_coe (L₁ ≃ₗ⁅R⁆ L₂) (L₁ ≃ₗ[R] L₂) := ⟨to_linear_equiv⟩
/-- see Note [function coercion] -/
instance : has_coe_to_fun (L₁ ≃ₗ⁅R⁆ L₂) := ⟨_, to_fun⟩
@[simp, norm_cast] lemma coe_to_lie_equiv (e : L₁ ≃ₗ⁅R⁆ L₂) : ((e : L₁ →ₗ⁅R⁆ L₂) : L₁ → L₂) = e :=
rfl
@[simp, norm_cast] lemma coe_to_linear_equiv (e : L₁ ≃ₗ⁅R⁆ L₂) : ((e : L₁ ≃ₗ[R] L₂) : L₁ → L₂) = e :=
rfl
instance : has_one (L₁ ≃ₗ⁅R⁆ L₁) :=
⟨{ map_lie := λ x y, by { change ((1 : L₁→ₗ[R] L₁) ⁅x, y⁆) = ⁅(1 : L₁→ₗ[R] L₁) x, (1 : L₁→ₗ[R] L₁) y⁆, simp, },
..(1 : L₁ ≃ₗ[R] L₁)}⟩
@[simp] lemma one_apply (x : L₁) : (1 : (L₁ ≃ₗ⁅R⁆ L₁)) x = x := rfl
instance : inhabited (L₁ ≃ₗ⁅R⁆ L₁) := ⟨1⟩
/-- Lie algebra equivalences are reflexive. -/
@[refl]
def refl : L₁ ≃ₗ⁅R⁆ L₁ := 1
@[simp] lemma refl_apply (x : L₁) : (refl : L₁ ≃ₗ⁅R⁆ L₁) x = x := rfl
/-- Lie algebra equivalences are symmetric. -/
@[symm]
def symm (e : L₁ ≃ₗ⁅R⁆ L₂) : L₂ ≃ₗ⁅R⁆ L₁ :=
{ ..morphism.inverse e.to_morphism e.inv_fun e.left_inv e.right_inv,
..e.to_linear_equiv.symm }
@[simp] lemma symm_symm (e : L₁ ≃ₗ⁅R⁆ L₂) : e.symm.symm = e :=
by { cases e, refl, }
@[simp] lemma apply_symm_apply (e : L₁ ≃ₗ⁅R⁆ L₂) : ∀ x, e (e.symm x) = x :=
e.to_linear_equiv.apply_symm_apply
@[simp] lemma symm_apply_apply (e : L₁ ≃ₗ⁅R⁆ L₂) : ∀ x, e.symm (e x) = x :=
e.to_linear_equiv.symm_apply_apply
/-- Lie algebra equivalences are transitive. -/
@[trans]
def trans (e₁ : L₁ ≃ₗ⁅R⁆ L₂) (e₂ : L₂ ≃ₗ⁅R⁆ L₃) : L₁ ≃ₗ⁅R⁆ L₃ :=
{ ..morphism.comp e₂.to_morphism e₁.to_morphism,
..linear_equiv.trans e₁.to_linear_equiv e₂.to_linear_equiv }
@[simp] lemma trans_apply (e₁ : L₁ ≃ₗ⁅R⁆ L₂) (e₂ : L₂ ≃ₗ⁅R⁆ L₃) (x : L₁) :
(e₁.trans e₂) x = e₂ (e₁ x) := rfl
@[simp] lemma symm_trans_apply (e₁ : L₁ ≃ₗ⁅R⁆ L₂) (e₂ : L₂ ≃ₗ⁅R⁆ L₃) (x : L₃) :
(e₁.trans e₂).symm x = e₁.symm (e₂.symm x) := rfl
end equiv
namespace direct_sum
open dfinsupp
variables {R : Type u} [comm_ring R]
variables {ι : Type v} [decidable_eq ι] {L : ι → Type w}
variables [Π i, lie_ring (L i)] [Π i, lie_algebra R (L i)]
/-- The direct sum of Lie rings carries a natural Lie ring structure. -/
instance : lie_ring (direct_sum ι L) := {
bracket := zip_with (λ i, λ x y, ⁅x, y⁆) (λ i, lie_zero 0),
add_lie := λ x y z, by { ext, simp only [zip_with_apply, add_apply, add_lie], },
lie_add := λ x y z, by { ext, simp only [zip_with_apply, add_apply, lie_add], },
lie_self := λ x, by { ext, simp only [zip_with_apply, add_apply, lie_self, zero_apply], },
jacobi := λ x y z, by { ext, simp only [zip_with_apply, add_apply, lie_ring.jacobi, zero_apply], },
..(infer_instance : add_comm_group _) }
@[simp] lemma bracket_apply {x y : direct_sum ι L} {i : ι} :
⁅x, y⁆ i = ⁅x i, y i⁆ := zip_with_apply
/-- The direct sum of Lie algebras carries a natural Lie algebra structure. -/
instance : lie_algebra R (direct_sum ι L) :=
{ lie_smul := λ c x y, by { ext, simp only [zip_with_apply, smul_apply, bracket_apply, lie_smul], },
..(infer_instance : module R _) }
end direct_sum
variables {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L]
/--
An associative algebra gives rise to a Lie algebra by taking the bracket to be the ring commutator.
-/
def of_associative_algebra (A : Type v) [ring A] [algebra R A] :
@lie_algebra R A _ (lie_ring.of_associative_ring _) :=
{ lie_smul := λ t x y,
by rw [lie_ring.of_associative_ring_bracket, lie_ring.of_associative_ring_bracket,
algebra.mul_smul_comm, algebra.smul_mul_assoc, smul_sub], }
instance (M : Type v) [add_comm_group M] [module R M] : lie_ring (module.End R M) :=
lie_ring.of_associative_ring _
local attribute [instance] lie_ring.of_associative_ring
local attribute [instance] lie_algebra.of_associative_algebra
/-- The map `of_associative_algebra` associating a Lie algebra to an associative algebra is
functorial. -/
def of_associative_algebra_hom {R : Type u} {A : Type v} {B : Type w}
[comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B] (f : A →ₐ[R] B) : A →ₗ⁅R⁆ B :=
{ map_lie := λ x y, show f ⁅x,y⁆ = ⁅f x,f y⁆,
by simp only [lie_ring.of_associative_ring_bracket, alg_hom.map_sub, alg_hom.map_mul],
..f.to_linear_map, }
@[simp] lemma of_associative_algebra_hom_id {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] :
of_associative_algebra_hom (alg_hom.id R A) = 1 := rfl
@[simp] lemma of_associative_algebra_hom_comp {R : Type u} {A : Type v} {B : Type w} {C : Type w₁}
[comm_ring R] [ring A] [ring B] [ring C] [algebra R A] [algebra R B] [algebra R C]
(f : A →ₐ[R] B) (g : B →ₐ[R] C) :
of_associative_algebra_hom (g.comp f) = (of_associative_algebra_hom g).comp (of_associative_algebra_hom f) := rfl
/--
An important class of Lie algebras are those arising from the associative algebra structure on
module endomorphisms.
-/
instance of_endomorphism_algebra (M : Type v) [add_comm_group M] [module R M] :
lie_algebra R (module.End R M) :=
of_associative_algebra (module.End R M)
lemma endo_algebra_bracket (M : Type v) [add_comm_group M] [module R M] (f g : module.End R M) :
⁅f, g⁆ = f.comp g - g.comp f := rfl
/--
The adjoint action of a Lie algebra on itself.
-/
def Ad : L →ₗ⁅R⁆ module.End R L :=
{ to_fun := λ x,
{ to_fun := has_bracket.bracket x,
map_add' := by { intros, apply lie_add, },
map_smul' := by { intros, apply lie_smul, } },
map_add' := by { intros, ext, simp, },
map_smul' := by { intros, ext, simp, },
map_lie := by {
intros x y, ext z,
rw endo_algebra_bracket,
suffices : ⁅⁅x, y⁆, z⁆ = ⁅x, ⁅y, z⁆⁆ + ⁅⁅x, z⁆, y⁆, by simpa [sub_eq_add_neg],
rw [eq_comm, ←lie_skew ⁅x, y⁆ z, ←lie_skew ⁅x, z⁆ y, ←lie_skew x z, lie_neg, neg_neg,
←sub_eq_zero_iff_eq, sub_neg_eq_add, lie_ring.jacobi], } }
end lie_algebra
section lie_subalgebra
variables (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L]
set_option old_structure_cmd true
/--
A Lie subalgebra of a Lie algebra is submodule that is closed under the Lie bracket.
This is a sufficient condition for the subset itself to form a Lie algebra.
-/
structure lie_subalgebra extends submodule R L :=
(lie_mem : ∀ {x y}, x ∈ carrier → y ∈ carrier → ⁅x, y⁆ ∈ carrier)
attribute [nolint doc_blame] lie_subalgebra.to_submodule
/-- The zero algebra is a subalgebra of any Lie algebra. -/
instance : has_zero (lie_subalgebra R L) :=
⟨{ lie_mem := λ x y hx hy, by { rw [((submodule.mem_bot R).1 hx), zero_lie],
exact submodule.zero_mem (0 : submodule R L), },
..(0 : submodule R L) }⟩
instance : inhabited (lie_subalgebra R L) := ⟨0⟩
instance : has_coe (lie_subalgebra R L) (set L) := ⟨lie_subalgebra.carrier⟩
instance : has_mem L (lie_subalgebra R L) := ⟨λ x L', x ∈ (L' : set L)⟩
instance lie_subalgebra_coe_submodule : has_coe (lie_subalgebra R L) (submodule R L) :=
⟨lie_subalgebra.to_submodule⟩
/-- A Lie subalgebra forms a new Lie ring. -/
instance lie_subalgebra_lie_ring (L' : lie_subalgebra R L) : lie_ring L' := {
bracket := λ x y, ⟨⁅x.val, y.val⁆, L'.lie_mem x.property y.property⟩,
lie_add := by { intros, apply set_coe.ext, apply lie_add, },
add_lie := by { intros, apply set_coe.ext, apply add_lie, },
lie_self := by { intros, apply set_coe.ext, apply lie_self, },
jacobi := by { intros, apply set_coe.ext, apply lie_ring.jacobi, } }
/-- A Lie subalgebra forms a new Lie algebra. -/
instance lie_subalgebra_lie_algebra (L' : lie_subalgebra R L) :
@lie_algebra R L' _ (lie_subalgebra_lie_ring _ _ _) :=
{ lie_smul := by { intros, apply set_coe.ext, apply lie_smul } }
@[simp] lemma lie_subalgebra.mem_coe {L' : lie_subalgebra R L} {x : L} :
x ∈ (L' : set L) ↔ x ∈ L' := iff.rfl
@[simp] lemma lie_subalgebra.mem_coe' {L' : lie_subalgebra R L} {x : L} :
x ∈ (L' : submodule R L) ↔ x ∈ L' := iff.rfl
@[simp, norm_cast] lemma lie_subalgebra.coe_bracket (L' : lie_subalgebra R L) (x y : L') :
(↑⁅x, y⁆ : L) = ⁅↑x, ↑y⁆ := rfl
@[ext] lemma lie_subalgebra.ext (L₁' L₂' : lie_subalgebra R L) (h : ∀ x, x ∈ L₁' ↔ x ∈ L₂') :
L₁' = L₂' :=
by { cases L₁', cases L₂', simp only [], ext x, exact h x, }
lemma lie_subalgebra.ext_iff (L₁' L₂' : lie_subalgebra R L) : L₁' = L₂' ↔ ∀ x, x ∈ L₁' ↔ x ∈ L₂' :=
⟨λ h x, by rw h, lie_subalgebra.ext R L L₁' L₂'⟩
local attribute [instance] lie_ring.of_associative_ring
local attribute [instance] lie_algebra.of_associative_algebra
/-- A subalgebra of an associative algebra is a Lie subalgebra of the associated Lie algebra. -/
def lie_subalgebra_of_subalgebra (A : Type v) [ring A] [algebra R A]
(A' : subalgebra R A) : lie_subalgebra R A :=
{ lie_mem := λ x y hx hy, by {
change ⁅x, y⁆ ∈ A', change x ∈ A' at hx, change y ∈ A' at hy,
rw lie_ring.of_associative_ring_bracket,
have hxy := A'.mul_mem hx hy,
have hyx := A'.mul_mem hy hx,
exact submodule.sub_mem A'.to_submodule hxy hyx, },
..A'.to_submodule }
variables {R L} {L₂ : Type w} [lie_ring L₂] [lie_algebra R L₂]
/-- The embedding of a Lie subalgebra into the ambient space as a Lie morphism. -/
def lie_subalgebra.incl (L' : lie_subalgebra R L) : L' →ₗ⁅R⁆ L :=
{ map_lie := λ x y, by { rw [linear_map.to_fun_eq_coe, submodule.subtype_apply], refl, },
..L'.to_submodule.subtype }
/-- The range of a morphism of Lie algebras is a Lie subalgebra. -/
def lie_algebra.morphism.range (f : L →ₗ⁅R⁆ L₂) : lie_subalgebra R L₂ :=
{ lie_mem := λ x y,
show x ∈ f.to_linear_map.range → y ∈ f.to_linear_map.range → ⁅x, y⁆ ∈ f.to_linear_map.range,
by { repeat { rw linear_map.mem_range }, rintros ⟨x', hx⟩ ⟨y', hy⟩, refine ⟨⁅x', y'⁆, _⟩,
rw [←hx, ←hy], change f ⁅x', y'⁆ = ⁅f x', f y'⁆, rw lie_algebra.map_lie, },
..f.to_linear_map.range }
@[simp] lemma lie_algebra.morphism.range_bracket (f : L →ₗ⁅R⁆ L₂) (x y : f.range) :
(↑⁅x, y⁆ : L₂) = ⁅↑x, ↑y⁆ := rfl
/-- The image of a Lie subalgebra under a Lie algebra morphism is a Lie subalgebra of the
codomain. -/
def lie_subalgebra.map (f : L →ₗ⁅R⁆ L₂) (L' : lie_subalgebra R L) : lie_subalgebra R L₂ :=
{ lie_mem := λ x y hx hy, by {
erw submodule.mem_map at hx, rcases hx with ⟨x', hx', hx⟩, rw ←hx,
erw submodule.mem_map at hy, rcases hy with ⟨y', hy', hy⟩, rw ←hy,
erw submodule.mem_map,
exact ⟨⁅x', y'⁆, L'.lie_mem hx' hy', lie_algebra.map_lie f x' y'⟩, },
..((L' : submodule R L).map (f : L →ₗ[R] L₂))}
@[simp] lemma lie_subalgebra.mem_map_submodule (e : L ≃ₗ⁅R⁆ L₂) (L' : lie_subalgebra R L) (x : L₂) :
x ∈ L'.map (e : L →ₗ⁅R⁆ L₂) ↔ x ∈ (L' : submodule R L).map (e : L →ₗ[R] L₂) :=
by refl
end lie_subalgebra
namespace lie_algebra
variables {R : Type u} {L₁ : Type v} {L₂ : Type w}
variables [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂]
namespace equiv
/-- An injective Lie algebra morphism is an equivalence onto its range. -/
noncomputable def of_injective (f : L₁ →ₗ⁅R⁆ L₂) (h : function.injective f) :
L₁ ≃ₗ⁅R⁆ f.range :=
have h' : (f : L₁ →ₗ[R] L₂).ker = ⊥ := linear_map.ker_eq_bot_of_injective h,
{ map_lie := λ x y, by { apply set_coe.ext,
simp only [linear_equiv.of_injective_apply, lie_algebra.morphism.range_bracket],
apply f.map_lie, },
..(linear_equiv.of_injective ↑f h')}
@[simp] lemma of_injective_apply (f : L₁ →ₗ⁅R⁆ L₂) (h : function.injective f) (x : L₁) :
↑(of_injective f h x) = f x := rfl
variables (L₁' L₁'' : lie_subalgebra R L₁) (L₂' : lie_subalgebra R L₂)
/-- Lie subalgebras that are equal as sets are equivalent as Lie algebras. -/
def of_eq (h : (L₁' : set L₁) = L₁'') : L₁' ≃ₗ⁅R⁆ L₁'' :=
{ map_lie := λ x y, by { apply set_coe.ext, simp, },
..(linear_equiv.of_eq ↑L₁' ↑L₁''
(by {ext x, change x ∈ (L₁' : set L₁) ↔ x ∈ (L₁'' : set L₁), rw h, } )) }
@[simp] lemma of_eq_apply (L L' : lie_subalgebra R L₁) (h : (L : set L₁) = L') (x : L) :
(↑(of_eq L L' h x) : L₁) = x := rfl
variables (e : L₁ ≃ₗ⁅R⁆ L₂)
/-- An equivalence of Lie algebras restricts to an equivalence from any Lie subalgebra onto its
image. -/
def of_subalgebra : L₁'' ≃ₗ⁅R⁆ (L₁''.map e : lie_subalgebra R L₂) :=
{ map_lie := λ x y, by { apply set_coe.ext, exact lie_algebra.map_lie (↑e : L₁ →ₗ⁅R⁆ L₂) ↑x ↑y, }
..(linear_equiv.of_submodule (e : L₁ ≃ₗ[R] L₂) ↑L₁'') }
@[simp] lemma of_subalgebra_apply (x : L₁'') : ↑(e.of_subalgebra _ x) = e x := rfl
/-- An equivalence of Lie algebras restricts to an equivalence from any Lie subalgebra onto its
image. -/
def of_subalgebras (h : L₁'.map ↑e = L₂') : L₁' ≃ₗ⁅R⁆ L₂' :=
{ map_lie := λ x y, by { apply set_coe.ext, exact lie_algebra.map_lie (↑e : L₁ →ₗ⁅R⁆ L₂) ↑x ↑y, },
..(linear_equiv.of_submodules (e : L₁ ≃ₗ[R] L₂) ↑L₁' ↑L₂' (by { rw ←h, refl, })) }
@[simp] lemma of_subalgebras_apply (h : L₁'.map ↑e = L₂') (x : L₁') :
↑(e.of_subalgebras _ _ h x) = e x := rfl
@[simp] lemma of_subalgebras_symm_apply (h : L₁'.map ↑e = L₂') (x : L₂') :
↑((e.of_subalgebras _ _ h).symm x) = e.symm x := rfl
end equiv
end lie_algebra
section lie_module
variables (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L]
variables (M : Type v) [add_comm_group M] [module R M]
section prio
set_option default_priority 100 -- see Note [default priority]
/--
A Lie module is a module over a commutative ring, together with a linear action of a Lie algebra
on this module, such that the Lie bracket acts as the commutator of endomorphisms.
-/
class lie_module extends linear_action R L M :=
(lie_act : ∀ (l l' : L) (m : M), act ⁅l, l'⁆ m = act l (act l' m) - act l' (act l m))
end prio
@[simp] lemma lie_act [lie_module R L M]
(l l' : L) (m : M) : linear_action.act R ⁅l, l'⁆ m =
linear_action.act R l (linear_action.act R l' m) -
linear_action.act R l' (linear_action.act R l m) :=
lie_module.lie_act l l' m
protected lemma of_endo_map_action (α : L →ₗ⁅R⁆ module.End R M) (x : L) (m : M) :
@linear_action.act R _ _ _ _ _ _ _ (linear_action.of_endo_map R L M α) x m = α x m := rfl
/--
A Lie morphism from a Lie algebra to the endomorphism algebra of a module yields
a Lie module structure.
-/
def lie_module.of_endo_morphism (α : L →ₗ⁅R⁆ module.End R M) : lie_module R L M := {
lie_act := by { intros x y m, rw [of_endo_map_action, lie_algebra.map_lie,
lie_algebra.endo_algebra_bracket], refl, },
..(linear_action.of_endo_map R L M α) }
/--
Every Lie algebra is a module over itself.
-/
instance lie_algebra_self_module : lie_module R L L :=
lie_module.of_endo_morphism R L L lie_algebra.Ad
/--
A Lie submodule of a Lie module is a submodule that is closed under the Lie bracket.
This is a sufficient condition for the subset itself to form a Lie module.
-/
structure lie_submodule [lie_module R L M] extends submodule R M :=
(lie_mem : ∀ {x : L} {m : M}, m ∈ carrier → linear_action.act R x m ∈ carrier)
/-- The zero module is a Lie submodule of any Lie module. -/
instance [lie_module R L M] : has_zero (lie_submodule R L M) :=
⟨{ lie_mem := λ x m h, by { rw [((submodule.mem_bot R).1 h), linear_action_zero],
exact submodule.zero_mem (0 : submodule R M), },
..(0 : submodule R M)}⟩
instance [lie_module R L M] : inhabited (lie_submodule R L M) := ⟨0⟩
instance lie_submodule_coe_submodule [lie_module R L M] :
has_coe (lie_submodule R L M) (submodule R M) := ⟨lie_submodule.to_submodule⟩
instance lie_submodule_has_mem [lie_module R L M] :
has_mem M (lie_submodule R L M) := ⟨λ x N, x ∈ (N : set M)⟩
instance lie_submodule_lie_module [lie_module R L M] (N : lie_submodule R L M) :
lie_module R L N := {
act := λ x m, ⟨linear_action.act R x m.val, N.lie_mem m.property⟩,
add_act := by { intros x y m, apply set_coe.ext, apply linear_action.add_act, },
act_add := by { intros x m n, apply set_coe.ext, apply linear_action.act_add, },
act_smul := by { intros r x y, apply set_coe.ext, apply linear_action.act_smul, },
smul_act := by { intros r x y, apply set_coe.ext, apply linear_action.smul_act, },
lie_act := by { intros x y m, apply set_coe.ext, apply lie_module.lie_act, } }
/--
An ideal of a Lie algebra is a Lie submodule of the Lie algebra as a Lie module over itself.
-/
abbreviation lie_ideal := lie_submodule R L L
lemma lie_mem_right (I : lie_ideal R L) (x y : L) (h : y ∈ I) : ⁅x, y⁆ ∈ I := I.lie_mem h
lemma lie_mem_left (I : lie_ideal R L) (x y : L) (h : x ∈ I) : ⁅x, y⁆ ∈ I := by {
rw [←lie_skew, ←neg_lie], apply lie_mem_right, assumption, }
/--
An ideal of a Lie algebra is a Lie subalgebra.
-/
def lie_ideal_subalgebra (I : lie_ideal R L) : lie_subalgebra R L := {
lie_mem := by { intros x y hx hy, apply lie_mem_right, exact hy, },
..I.to_submodule, }
/-- A Lie module is irreducible if its only non-trivial Lie submodule is itself. -/
class lie_module.is_irreducible [lie_module R L M] : Prop :=
(irreducible : ∀ (M' : lie_submodule R L M), (∃ (m : M'), m ≠ 0) → (∀ (m : M), m ∈ M'))
/-- A Lie algebra is simple if it is irreducible as a Lie module over itself via the adjoint
action, and it is non-Abelian. -/
class lie_algebra.is_simple : Prop :=
(simple : lie_module.is_irreducible R L L ∧ ¬lie_algebra.is_abelian L)
end lie_module
namespace lie_submodule
variables {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L]
variables {M : Type v} [add_comm_group M] [module R M] [α : lie_module R L M]
variables (N : lie_submodule R L M) (I : lie_ideal R L)
/--
The quotient of a Lie module by a Lie submodule. It is a Lie module.
-/
abbreviation quotient := N.to_submodule.quotient
namespace quotient
variables {N I}
/--
Map sending an element of `M` to the corresponding element of `M/N`, when `N` is a lie_submodule of
the lie_module `N`.
-/
abbreviation mk : M → N.quotient := submodule.quotient.mk
lemma is_quotient_mk (m : M) :
quotient.mk' m = (mk m : N.quotient) := rfl
/-- Given a Lie module `M` over a Lie algebra `L`, together with a Lie submodule `N ⊆ M`, there
is a natural linear map from `L` to the endomorphisms of `M` leaving `N` invariant. -/
def lie_submodule_invariant : L →ₗ[R] submodule.compatible_maps N.to_submodule N.to_submodule :=
linear_map.cod_restrict _ (α.to_linear_action.to_endo_map _ _ _) N.lie_mem
instance lie_quotient_action : linear_action R L N.quotient :=
linear_action.of_endo_map _ _ _ (linear_map.comp (submodule.mapq_linear N N) lie_submodule_invariant)
lemma lie_quotient_action_apply (z : L) (m : M) :
linear_action.act R z (mk m : N.quotient) = mk (linear_action.act R z m) := rfl
/-- The quotient of a Lie module by a Lie submodule, is a Lie module. -/
instance lie_quotient_lie_module : lie_module R L N.quotient :=
{ lie_act := λ x y m', by { apply quotient.induction_on' m', intros m, rw is_quotient_mk,
repeat { rw lie_quotient_action_apply, }, rw lie_act, refl, },
..quotient.lie_quotient_action, }
instance lie_quotient_has_bracket : has_bracket (quotient I) := ⟨by {
intros x y,
apply quotient.lift_on₂' x y (λ x' y', mk ⁅x', y'⁆),
intros x₁ x₂ y₁ y₂ h₁ h₂,
apply (submodule.quotient.eq I.to_submodule).2,
have h : ⁅x₁, x₂⁆ - ⁅y₁, y₂⁆ = ⁅x₁, x₂ - y₂⁆ + ⁅x₁ - y₁, y₂⁆ := by simp [-lie_skew, sub_eq_add_neg, add_assoc],
rw h,
apply submodule.add_mem,
{ apply lie_mem_right R L I x₁ (x₂ - y₂) h₂, },
{ apply lie_mem_left R L I (x₁ - y₁) y₂ h₁, }, }⟩
@[simp] lemma mk_bracket (x y : L) :
(mk ⁅x, y⁆ : quotient I) = ⁅mk x, mk y⁆ := rfl
instance lie_quotient_lie_ring : lie_ring (quotient I) := {
add_lie := by { intros x' y' z', apply quotient.induction_on₃' x' y' z', intros x y z,
repeat { rw is_quotient_mk <|>
rw ←mk_bracket <|>
rw ←submodule.quotient.mk_add, },
apply congr_arg, apply add_lie, },
lie_add := by { intros x' y' z', apply quotient.induction_on₃' x' y' z', intros x y z,
repeat { rw is_quotient_mk <|>
rw ←mk_bracket <|>
rw ←submodule.quotient.mk_add, },
apply congr_arg, apply lie_add, },
lie_self := by { intros x', apply quotient.induction_on' x', intros x,
rw [is_quotient_mk, ←mk_bracket],
apply congr_arg, apply lie_self, },
jacobi := by { intros x' y' z', apply quotient.induction_on₃' x' y' z', intros x y z,
repeat { rw is_quotient_mk <|>
rw ←mk_bracket <|>
rw ←submodule.quotient.mk_add, },
apply congr_arg, apply lie_ring.jacobi, } }
instance lie_quotient_lie_algebra : lie_algebra R (quotient I) := {
lie_smul := by { intros t x' y', apply quotient.induction_on₂' x' y', intros x y,
repeat { rw is_quotient_mk <|>
rw ←mk_bracket <|>
rw ←submodule.quotient.mk_smul, },
apply congr_arg, apply lie_smul, } }
end quotient
end lie_submodule
namespace linear_equiv
variables {R : Type u} {M₁ : Type v} {M₂ : Type w}
variables [comm_ring R] [add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂]
variables (e : M₁ ≃ₗ[R] M₂)
/-- A linear equivalence of two modules induces a Lie algebra equivalence of their endomorphisms. -/
def lie_conj : module.End R M₁ ≃ₗ⁅R⁆ module.End R M₂ :=
{ map_lie := λ f g, show e.conj ⁅f, g⁆ = ⁅e.conj f, e.conj g⁆,
by simp only [lie_algebra.endo_algebra_bracket, e.conj_comp, linear_equiv.map_sub],
..e.conj }
@[simp] lemma lie_conj_apply (f : module.End R M₁) : e.lie_conj f = e.conj f := rfl
@[simp] lemma lie_conj_symm : e.lie_conj.symm = e.symm.lie_conj := rfl
end linear_equiv
section matrices
open_locale matrix
variables {R : Type u} [comm_ring R]
variables {n : Type w} [fintype n] [decidable_eq n]
/-- An important class of Lie rings are those arising from the associative algebra structure on
square matrices over a commutative ring. -/
def matrix.lie_ring : lie_ring (matrix n n R) :=
lie_ring.of_associative_ring (matrix n n R)
local attribute [instance] matrix.lie_ring
/-- An important class of Lie algebras are those arising from the associative algebra structure on
square matrices over a commutative ring. -/
def matrix.lie_algebra : lie_algebra R (matrix n n R) :=
lie_algebra.of_associative_algebra (matrix n n R)
local attribute [instance] matrix.lie_algebra
/-- The natural equivalence between linear endomorphisms of finite free modules and square matrices
is compatible with the Lie algebra structures. -/
def lie_equiv_matrix' : module.End R (n → R) ≃ₗ⁅R⁆ matrix n n R :=
{ map_lie := λ T S,
begin
let f := @linear_map.to_matrixₗ n n _ _ R _ _,
change f (T.comp S - S.comp T) = (f T) * (f S) - (f S) * (f T),
have h : ∀ (T S : module.End R _), f (T.comp S) = (f T) ⬝ (f S) := matrix.comp_to_matrix_mul,
rw [linear_map.map_sub, h, h, matrix.mul_eq_mul, matrix.mul_eq_mul],
end,
..linear_equiv_matrix' }
@[simp] lemma lie_equiv_matrix'_apply (f : module.End R (n → R)) :
lie_equiv_matrix' f = f.to_matrix := rfl
@[simp] lemma lie_equiv_matrix'_symm_apply (A : matrix n n R) :
(@lie_equiv_matrix' R _ n _ _).symm A = A.to_lin := rfl
/-- An invertible matrix induces a Lie algebra equivalence from the space of matrices to itself. -/
noncomputable def matrix.lie_conj (P : matrix n n R) (h : is_unit P) :
matrix n n R ≃ₗ⁅R⁆ matrix n n R :=
((@lie_equiv_matrix' R _ n _ _).symm.trans (P.to_linear_equiv h).lie_conj).trans lie_equiv_matrix'
@[simp] lemma matrix.lie_conj_apply (P A : matrix n n R) (h : is_unit P) :
P.lie_conj h A = P ⬝ A ⬝ P⁻¹ :=
by simp [linear_equiv.conj_apply, matrix.lie_conj, matrix.comp_to_matrix_mul, to_lin_to_matrix]
@[simp] lemma matrix.lie_conj_symm_apply (P A : matrix n n R) (h : is_unit P) :
(P.lie_conj h).symm A = P⁻¹ ⬝ A ⬝ P :=
by simp [linear_equiv.symm_conj_apply, matrix.lie_conj, matrix.comp_to_matrix_mul, to_lin_to_matrix]
/-- For square matrices, the natural map that reindexes a matrix's rows and columns with equivalent
types is an equivalence of Lie algebras. -/
def matrix.reindex_lie_equiv {m : Type w₁} [fintype m] [decidable_eq m]
(e : n ≃ m) : matrix n n R ≃ₗ⁅R⁆ matrix m m R :=
{ map_lie := λ M N, by simp only [lie_ring.of_associative_ring_bracket, matrix.reindex_mul,
matrix.mul_eq_mul, linear_equiv.map_sub, linear_equiv.to_fun_apply],
..(matrix.reindex_linear_equiv e e) }
@[simp] lemma matrix.reindex_lie_equiv_apply {m : Type w₁} [fintype m] [decidable_eq m]
(e : n ≃ m) (M : matrix n n R) :
matrix.reindex_lie_equiv e M = λ i j, M (e.symm i) (e.symm j) :=
rfl
@[simp] lemma matrix.reindex_lie_equiv_symm_apply {m : Type w₁} [fintype m] [decidable_eq m]
(e : n ≃ m) (M : matrix m m R) :
(matrix.reindex_lie_equiv e).symm M = λ i j, M (e i) (e j) :=
rfl
end matrices
section skew_adjoint_endomorphisms
open bilin_form
variables {R : Type u} {M : Type v} [comm_ring R] [add_comm_group M] [module R M]
variables (B : bilin_form R M)
lemma bilin_form.is_skew_adjoint_bracket (f g : module.End R M)
(hf : f ∈ B.skew_adjoint_submodule) (hg : g ∈ B.skew_adjoint_submodule) :
⁅f, g⁆ ∈ B.skew_adjoint_submodule :=
begin
rw mem_skew_adjoint_submodule at *,
have hfg : is_adjoint_pair B B (f * g) (g * f), { rw ←neg_mul_neg g f, exact hf.mul hg, },
have hgf : is_adjoint_pair B B (g * f) (f * g), { rw ←neg_mul_neg f g, exact hg.mul hf, },
change bilin_form.is_adjoint_pair B B (f * g - g * f) (-(f * g - g * f)), rw neg_sub,
exact hfg.sub hgf,
end
/-- Given an `R`-module `M`, equipped with a bilinear form, the skew-adjoint endomorphisms form a
Lie subalgebra of the Lie algebra of endomorphisms. -/
def skew_adjoint_lie_subalgebra : lie_subalgebra R (module.End R M) :=
{ lie_mem := B.is_skew_adjoint_bracket, ..B.skew_adjoint_submodule }
variables {N : Type w} [add_comm_group N] [module R N] (e : N ≃ₗ[R] M)
/-- An equivalence of modules with bilinear forms gives equivalence of Lie algebras of skew-adjoint
endomorphisms. -/
def skew_adjoint_lie_subalgebra_equiv :
skew_adjoint_lie_subalgebra (B.comp (↑e : N →ₗ[R] M) ↑e) ≃ₗ⁅R⁆ skew_adjoint_lie_subalgebra B :=
begin
apply lie_algebra.equiv.of_subalgebras _ _ e.lie_conj,
ext f,
simp only [lie_subalgebra.mem_coe, submodule.mem_map_equiv, lie_subalgebra.mem_map_submodule,
coe_coe],
exact (bilin_form.is_pair_self_adjoint_equiv (-B) B e f).symm,
end
@[simp] lemma skew_adjoint_lie_subalgebra_equiv_apply
(f : skew_adjoint_lie_subalgebra (B.comp ↑e ↑e)) :
↑(skew_adjoint_lie_subalgebra_equiv B e f) = e.lie_conj f :=
by simp [skew_adjoint_lie_subalgebra_equiv]
@[simp] lemma skew_adjoint_lie_subalgebra_equiv_symm_apply (f : skew_adjoint_lie_subalgebra B) :
↑((skew_adjoint_lie_subalgebra_equiv B e).symm f) = e.symm.lie_conj f :=
by simp [skew_adjoint_lie_subalgebra_equiv]
end skew_adjoint_endomorphisms
section skew_adjoint_matrices
open_locale matrix
variables {R : Type u} {n : Type w} [comm_ring R] [fintype n] [decidable_eq n]
variables (J : matrix n n R)
local attribute [instance] matrix.lie_ring
local attribute [instance] matrix.lie_algebra
lemma matrix.lie_transpose (A B : matrix n n R) : ⁅A, B⁆ᵀ = ⁅Bᵀ, Aᵀ⁆ :=
show (A * B - B * A)ᵀ = (Bᵀ * Aᵀ - Aᵀ * Bᵀ), by simp
lemma matrix.is_skew_adjoint_bracket (A B : matrix n n R)
(hA : A ∈ skew_adjoint_matrices_submodule J) (hB : B ∈ skew_adjoint_matrices_submodule J) :
⁅A, B⁆ ∈ skew_adjoint_matrices_submodule J :=
begin
simp only [mem_skew_adjoint_matrices_submodule] at *,
change ⁅A, B⁆ᵀ ⬝ J = J ⬝ -⁅A, B⁆, change Aᵀ ⬝ J = J ⬝ -A at hA, change Bᵀ ⬝ J = J ⬝ -B at hB,
simp only [←matrix.mul_eq_mul] at *,
rw [matrix.lie_transpose, lie_ring.of_associative_ring_bracket, lie_ring.of_associative_ring_bracket,
sub_mul, mul_assoc, mul_assoc, hA, hB, ←mul_assoc, ←mul_assoc, hA, hB],
noncomm_ring,
end
/-- The Lie subalgebra of skew-adjoint square matrices corresponding to a square matrix `J`. -/
def skew_adjoint_matrices_lie_subalgebra : lie_subalgebra R (matrix n n R) :=
{ lie_mem := J.is_skew_adjoint_bracket, ..(skew_adjoint_matrices_submodule J) }
/-- An invertible matrix `P` gives a Lie algebra equivalence between those endomorphisms that are
skew-adjoint with respect to a square matrix `J` and those with respect to `PᵀJP`. -/
noncomputable def skew_adjoint_matrices_lie_subalgebra_equiv (P : matrix n n R) (h : is_unit P) :
skew_adjoint_matrices_lie_subalgebra J ≃ₗ⁅R⁆ skew_adjoint_matrices_lie_subalgebra (Pᵀ ⬝ J ⬝ P) :=
lie_algebra.equiv.of_subalgebras _ _ (P.lie_conj h).symm
begin
ext A,
suffices : P.lie_conj h A ∈ skew_adjoint_matrices_submodule J ↔
A ∈ skew_adjoint_matrices_submodule (Pᵀ ⬝ J ⬝ P),
{ simp only [lie_subalgebra.mem_coe, submodule.mem_map_equiv, lie_subalgebra.mem_map_submodule,
coe_coe], exact this, },
simp [matrix.is_skew_adjoint, J.is_adjoint_pair_equiv _ _ P h],
end
lemma skew_adjoint_matrices_lie_subalgebra_equiv_apply
(P : matrix n n R) (h : is_unit P) (A : skew_adjoint_matrices_lie_subalgebra J) :
↑(skew_adjoint_matrices_lie_subalgebra_equiv J P h A) = P⁻¹ ⬝ ↑A ⬝ P :=
by simp [skew_adjoint_matrices_lie_subalgebra_equiv]
end skew_adjoint_matrices
|
2f4e448fcff3b2b63faf67d33b22640746842a5f | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/linear_algebra/special_linear_group.lean | 77c18d981b620fd58f04aaf6579e0bf150499c62 | [
"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 | 8,212 | 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 linear_algebra.matrix.adjugate
import linear_algebra.matrix.to_lin
/-!
# The Special Linear group $SL(n, R)$
This file defines the elements of the Special Linear group `special_linear_group n R`, consisting
of all square `R`-matrices with determinant `1` on the fintype `n` by `n`. In addition, we define
the group structure on `special_linear_group n R` and the embedding into the general linear group
`general_linear_group R (n → R)`.
## Main definitions
* `matrix.special_linear_group` is the type of matrices with determinant 1
* `matrix.special_linear_group.group` gives the group structure (under multiplication)
* `matrix.special_linear_group.to_GL` is the embedding `SLₙ(R) → GLₙ(R)`
## Notation
For `m : ℕ`, we introduce the notation `SL(m,R)` for the special linear group on the fintype
`n = fin m`, in the locale `matrix_groups`.
## Implementation notes
The inverse operation in the `special_linear_group` is defined to be the adjugate
matrix, so that `special_linear_group n R` has a group structure for all `comm_ring R`.
We define the elements of `special_linear_group` to be matrices, since we need to
compute their determinant. This is in contrast with `general_linear_group R M`,
which consists of invertible `R`-linear maps on `M`.
We provide `matrix.special_linear_group.has_coe_to_fun` for convenience, but do not state any
lemmas about it, and use `matrix.special_linear_group.coe_fn_eq_coe` to eliminate it `⇑` in favor
of a regular `↑` coercion.
## References
* https://en.wikipedia.org/wiki/Special_linear_group
## Tags
matrix group, group, matrix inverse
-/
namespace matrix
universes u v
open_locale matrix
open linear_map
section
variables (n : Type u) [decidable_eq n] [fintype n] (R : Type v) [comm_ring R]
/-- `special_linear_group n R` is the group of `n` by `n` `R`-matrices with determinant equal to 1.
-/
def special_linear_group := { A : matrix n n R // A.det = 1 }
end
localized "notation `SL(` n `,` R `)`:= matrix.special_linear_group (fin n) R" in matrix_groups
namespace special_linear_group
variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R]
instance has_coe_to_matrix : has_coe (special_linear_group n R) (matrix n n R) :=
⟨λ A, A.val⟩
/- In this file, Lean often has a hard time working out the values of `n` and `R` for an expression
like `det ↑A`. Rather than writing `(A : matrix n n R)` everywhere in this file which is annoyingly
verbose, or `A.val` which is not the simp-normal form for subtypes, we create a local notation
`↑ₘA`. This notation references the local `n` and `R` variables, so is not valid as a global
notation. -/
local prefix `↑ₘ`:1024 := @coe _ (matrix n n R) _
lemma ext_iff (A B : special_linear_group n R) : A = B ↔ (∀ i j, ↑ₘA i j = ↑ₘB i j) :=
subtype.ext_iff.trans matrix.ext_iff.symm
@[ext] lemma ext (A B : special_linear_group n R) : (∀ i j, ↑ₘA i j = ↑ₘB i j) → A = B :=
(special_linear_group.ext_iff A B).mpr
instance has_inv : has_inv (special_linear_group n R) :=
⟨λ A, ⟨adjugate A, by rw [det_adjugate, A.prop, one_pow]⟩⟩
instance has_mul : has_mul (special_linear_group n R) :=
⟨λ A B, ⟨A.1 ⬝ B.1, by erw [det_mul, A.2, B.2, one_mul]⟩⟩
instance has_one : has_one (special_linear_group n R) :=
⟨⟨1, det_one⟩⟩
instance : inhabited (special_linear_group n R) := ⟨1⟩
section coe_lemmas
variables (A B : special_linear_group n R)
@[simp] lemma coe_mk (A : matrix n n R) (h : det A = 1) :
↑(⟨A, h⟩ : special_linear_group n R) = A :=
rfl
@[simp] lemma coe_inv : ↑ₘ(A⁻¹) = adjugate A := rfl
@[simp] lemma coe_mul : ↑ₘ(A * B) = ↑ₘA ⬝ ↑ₘB := rfl
@[simp] lemma coe_one : ↑ₘ(1 : special_linear_group n R) = (1 : matrix n n R) := rfl
@[simp] lemma det_coe : det ↑ₘA = 1 := A.2
lemma det_ne_zero [nontrivial R] (g : special_linear_group n R) :
det ↑ₘg ≠ 0 :=
by { rw g.det_coe, norm_num }
lemma row_ne_zero [nontrivial R] (g : special_linear_group n R) (i : n):
↑ₘg i ≠ 0 :=
λ h, g.det_ne_zero $ det_eq_zero_of_row_eq_zero i $ by simp [h]
end coe_lemmas
instance : monoid (special_linear_group n R) :=
function.injective.monoid coe subtype.coe_injective coe_one coe_mul
instance : group (special_linear_group n R) :=
{ mul_left_inv := λ A, by { ext1, simp [adjugate_mul] },
..special_linear_group.monoid,
..special_linear_group.has_inv }
/-- A version of `matrix.to_lin' A` that produces linear equivalences. -/
def to_lin' : special_linear_group n R →* (n → R) ≃ₗ[R] (n → R) :=
{ to_fun := λ A, linear_equiv.of_linear (matrix.to_lin' ↑ₘA) (matrix.to_lin' ↑ₘ(A⁻¹))
(by rw [←to_lin'_mul, ←coe_mul, mul_right_inv, coe_one, to_lin'_one])
(by rw [←to_lin'_mul, ←coe_mul, mul_left_inv, coe_one, to_lin'_one]),
map_one' := linear_equiv.to_linear_map_injective matrix.to_lin'_one,
map_mul' := λ A B, linear_equiv.to_linear_map_injective $ matrix.to_lin'_mul A B }
lemma to_lin'_apply (A : special_linear_group n R) (v : n → R) :
special_linear_group.to_lin' A v = matrix.to_lin' ↑ₘA v := rfl
lemma to_lin'_to_linear_map (A : special_linear_group n R) :
↑(special_linear_group.to_lin' A) = matrix.to_lin' ↑ₘA := rfl
lemma to_lin'_symm_apply (A : special_linear_group n R) (v : n → R) :
A.to_lin'.symm v = matrix.to_lin' ↑ₘ(A⁻¹) v := rfl
lemma to_lin'_symm_to_linear_map (A : special_linear_group n R) :
↑(A.to_lin'.symm) = matrix.to_lin' ↑ₘ(A⁻¹) := rfl
lemma to_lin'_injective :
function.injective ⇑(to_lin' : special_linear_group n R →* (n → R) ≃ₗ[R] (n → R)) :=
λ A B h, subtype.coe_injective $ matrix.to_lin'.injective $
linear_equiv.to_linear_map_injective.eq_iff.mpr h
/-- `to_GL` is the map from the special linear group to the general linear group -/
def to_GL : special_linear_group n R →* general_linear_group R (n → R) :=
(general_linear_group.general_linear_equiv _ _).symm.to_monoid_hom.comp to_lin'
lemma coe_to_GL (A : special_linear_group n R) : ↑A.to_GL = A.to_lin'.to_linear_map := rfl
variables {S : Type*} [comm_ring S]
/-- A ring homomorphism from `R` to `S` induces a group homomorphism from
`special_linear_group n R` to `special_linear_group n S`. -/
@[simps] def map (f : R →+* S) : special_linear_group n R →* special_linear_group n S :=
{ to_fun := λ g, ⟨f.map_matrix ↑g, by { rw ← f.map_det, simp [g.2] }⟩,
map_one' := subtype.ext $ f.map_matrix.map_one,
map_mul' := λ x y, subtype.ext $ f.map_matrix.map_mul x y }
section cast
/-- Coercion of SL `n` `ℤ` to SL `n` `R` for a commutative ring `R`. -/
instance : has_coe (special_linear_group n ℤ) (special_linear_group n R) :=
⟨λ x, map (int.cast_ring_hom R) x⟩
@[simp] lemma coe_matrix_coe (g : special_linear_group n ℤ) :
↑(g : special_linear_group n R)
= (↑g : matrix n n ℤ).map (int.cast_ring_hom R) :=
map_apply_coe (int.cast_ring_hom R) g
end cast
section has_neg
variables [fact (even (fintype.card n))]
/-- Formal operation of negation on special linear group on even cardinality `n` given by negating
each element. -/
instance : has_neg (special_linear_group n R) :=
⟨λ g,
⟨- g, by simpa [nat.neg_one_pow_of_even (fact.out (even (fintype.card n))), g.det_coe] using
det_smul ↑ₘg (-1)⟩⟩
@[simp] lemma coe_neg (g : special_linear_group n R) :
↑(- g) = - (↑g : matrix n n R) :=
rfl
@[simp] lemma coe_int_neg (g : (special_linear_group n ℤ)) :
↑(-g) = (-↑g : special_linear_group n R) :=
subtype.ext $ (@ring_hom.map_matrix n _ _ _ _ _ _ (int.cast_ring_hom R)).map_neg ↑g
end has_neg
-- this section should be last to ensure we do not use it in lemmas
section coe_fn_instance
/-- This instance is here for convenience, but is not the simp-normal form. -/
instance : has_coe_to_fun (special_linear_group n R) (λ _, n → n → R) :=
{ coe := λ A, A.val }
@[simp]
lemma coe_fn_eq_coe (s : special_linear_group n R) : ⇑s = ↑ₘs := rfl
end coe_fn_instance
end special_linear_group
end matrix
|
36a78ae3d26eb2130292a8e93fc92547b99c055c | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/eoi.lean | 8050e8d1aa936ff829f5d16b196ed2d38030aae9 | [
"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 | 14 | lean | def foo : Nat
|
0797f8823856090e4554396dbe294980101e0788 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/finmap.lean | 0518cbb1a88c7f980b30e0da348828eca2563fcd | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 22,220 | lean | /-
Copyright (c) 2018 Sean Leather. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sean Leather, Mario Carneiro
-/
import data.list.alist
import data.finset.sigma
import data.part
/-!
# Finite maps over `multiset`
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
-/
universes u v w
open list
variables {α : Type u} {β : α → Type v}
/-! ### multisets of sigma types-/
namespace multiset
/-- Multiset of keys of an association multiset. -/
def keys (s : multiset (sigma β)) : multiset α :=
s.map sigma.fst
@[simp] theorem coe_keys {l : list (sigma β)} :
keys (l : multiset (sigma β)) = (l.keys : multiset α) :=
rfl
/-- `nodupkeys s` means that `s` has no duplicate keys. -/
def nodupkeys (s : multiset (sigma β)) : Prop :=
quot.lift_on s list.nodupkeys (λ s t p, propext $ perm_nodupkeys p)
@[simp] theorem coe_nodupkeys {l : list (sigma β)} : @nodupkeys α β l ↔ l.nodupkeys := iff.rfl
lemma nodup_keys {m : multiset (Σ a, β a)} : m.keys.nodup ↔ m.nodupkeys :=
by { rcases m with ⟨l⟩, refl }
alias nodup_keys ↔ _ nodupkeys.nodup_keys
lemma nodupkeys.nodup {m : multiset (Σ a, β a)} (h : m.nodupkeys) : m.nodup :=
h.nodup_keys.of_map _
end multiset
/-! ### finmap -/
/-- `finmap β` is the type of finite maps over a multiset. It is effectively
a quotient of `alist β` by permutation of the underlying list. -/
structure finmap (β : α → Type v) : Type (max u v) :=
(entries : multiset (sigma β))
(nodupkeys : entries.nodupkeys)
/-- The quotient map from `alist` to `finmap`. -/
def alist.to_finmap (s : alist β) : finmap β := ⟨s.entries, s.nodupkeys⟩
local notation (name := to_finmap) `⟦`:max a `⟧`:0 := alist.to_finmap a
theorem alist.to_finmap_eq {s₁ s₂ : alist β} :
⟦s₁⟧ = ⟦s₂⟧ ↔ s₁.entries ~ s₂.entries :=
by cases s₁; cases s₂; simp [alist.to_finmap]
@[simp] theorem alist.to_finmap_entries (s : alist β) : ⟦s⟧.entries = s.entries := rfl
/-- Given `l : list (sigma β)`, create a term of type `finmap β` by removing
entries with duplicate keys. -/
def list.to_finmap [decidable_eq α] (s : list (sigma β)) : finmap β := s.to_alist.to_finmap
namespace finmap
open alist
lemma nodup_entries (f : finmap β) : f.entries.nodup := f.nodupkeys.nodup
/-! ### lifting from alist -/
/-- Lift a permutation-respecting function on `alist` to `finmap`. -/
@[elab_as_eliminator] def lift_on
{γ} (s : finmap β) (f : alist β → γ)
(H : ∀ a b : alist β, a.entries ~ b.entries → f a = f b) : γ :=
begin
refine (quotient.lift_on s.1 (λ l, (⟨_, λ nd, f ⟨l, nd⟩⟩ : part γ))
(λ l₁ l₂ p, part.ext' (perm_nodupkeys p) _) : part γ).get _,
{ exact λ h₁ h₂, H _ _ (by exact p) },
{ have := s.nodupkeys, rcases s.entries with ⟨l⟩, exact id }
end
@[simp] theorem lift_on_to_finmap {γ} (s : alist β) (f : alist β → γ) (H) :
lift_on ⟦s⟧ f H = f s := by cases s; refl
/-- Lift a permutation-respecting function on 2 `alist`s to 2 `finmap`s. -/
@[elab_as_eliminator] def lift_on₂
{γ} (s₁ s₂ : finmap β) (f : alist β → alist β → γ)
(H : ∀ a₁ b₁ a₂ b₂ : alist β, a₁.entries ~ a₂.entries → b₁.entries ~ b₂.entries →
f a₁ b₁ = f a₂ b₂) : γ :=
lift_on s₁
(λ l₁, lift_on s₂ (f l₁) (λ b₁ b₂ p, H _ _ _ _ (perm.refl _) p))
(λ a₁ a₂ p, have H' : f a₁ = f a₂ := funext (λ _, H _ _ _ _ p (perm.refl _)), by simp only [H'])
@[simp] theorem lift_on₂_to_finmap {γ} (s₁ s₂ : alist β) (f : alist β → alist β → γ) (H) :
lift_on₂ ⟦s₁⟧ ⟦s₂⟧ f H = f s₁ s₂ :=
by cases s₁; cases s₂; refl
/-! ### induction -/
@[elab_as_eliminator] theorem induction_on
{C : finmap β → Prop} (s : finmap β) (H : ∀ (a : alist β), C ⟦a⟧) : C s :=
by rcases s with ⟨⟨a⟩, h⟩; exact H ⟨a, h⟩
@[elab_as_eliminator] theorem induction_on₂ {C : finmap β → finmap β → Prop}
(s₁ s₂ : finmap β) (H : ∀ (a₁ a₂ : alist β), C ⟦a₁⟧ ⟦a₂⟧) : C s₁ s₂ :=
induction_on s₁ $ λ l₁, induction_on s₂ $ λ l₂, H l₁ l₂
@[elab_as_eliminator] theorem induction_on₃ {C : finmap β → finmap β → finmap β → Prop}
(s₁ s₂ s₃ : finmap β) (H : ∀ (a₁ a₂ a₃ : alist β), C ⟦a₁⟧ ⟦a₂⟧ ⟦a₃⟧) : C s₁ s₂ s₃ :=
induction_on₂ s₁ s₂ $ λ l₁ l₂, induction_on s₃ $ λ l₃, H l₁ l₂ l₃
/-! ### extensionality -/
@[ext] theorem ext : ∀ {s t : finmap β}, s.entries = t.entries → s = t
| ⟨l₁, h₁⟩ ⟨l₂, h₂⟩ H := by congr'
@[simp] theorem ext_iff {s t : finmap β} : s.entries = t.entries ↔ s = t :=
⟨ext, congr_arg _⟩
/-! ### mem -/
/-- The predicate `a ∈ s` means that `s` has a value associated to the key `a`. -/
instance : has_mem α (finmap β) := ⟨λ a s, a ∈ s.entries.keys⟩
theorem mem_def {a : α} {s : finmap β} :
a ∈ s ↔ a ∈ s.entries.keys := iff.rfl
@[simp] theorem mem_to_finmap {a : α} {s : alist β} :
a ∈ ⟦s⟧ ↔ a ∈ s := iff.rfl
/-! ### keys -/
/-- The set of keys of a finite map. -/
def keys (s : finmap β) : finset α :=
⟨s.entries.keys, s.nodupkeys.nodup_keys⟩
@[simp] theorem keys_val (s : alist β) : (keys ⟦s⟧).val = s.keys := rfl
@[simp] theorem keys_ext {s₁ s₂ : alist β} :
keys ⟦s₁⟧ = keys ⟦s₂⟧ ↔ s₁.keys ~ s₂.keys :=
by simp [keys, alist.keys]
theorem mem_keys {a : α} {s : finmap β} : a ∈ s.keys ↔ a ∈ s :=
induction_on s $ λ s, alist.mem_keys
/-! ### empty -/
/-- The empty map. -/
instance : has_emptyc (finmap β) := ⟨⟨0, nodupkeys_nil⟩⟩
instance : inhabited (finmap β) := ⟨∅⟩
@[simp] theorem empty_to_finmap : (⟦∅⟧ : finmap β) = ∅ := rfl
@[simp] theorem to_finmap_nil [decidable_eq α] : ([].to_finmap : finmap β) = ∅ := rfl
theorem not_mem_empty {a : α} : a ∉ (∅ : finmap β) :=
multiset.not_mem_zero a
@[simp] theorem keys_empty : (∅ : finmap β).keys = ∅ := rfl
/-! ### singleton -/
/-- The singleton map. -/
def singleton (a : α) (b : β a) : finmap β := ⟦alist.singleton a b⟧
@[simp] theorem keys_singleton (a : α) (b : β a) :
(singleton a b).keys = {a} := rfl
@[simp] lemma mem_singleton (x y : α) (b : β y) : x ∈ singleton y b ↔ x = y :=
by simp only [singleton]; erw [mem_cons_eq, mem_nil_iff, or_false]
section
variables [decidable_eq α]
instance has_decidable_eq [∀ a, decidable_eq (β a)] : decidable_eq (finmap β)
| s₁ s₂ := decidable_of_iff _ ext_iff
/-! ### lookup -/
/-- Look up the value associated to a key in a map. -/
def lookup (a : α) (s : finmap β) : option (β a) :=
lift_on s (lookup a) (λ s t, perm_lookup)
@[simp] theorem lookup_to_finmap (a : α) (s : alist β) :
lookup a ⟦s⟧ = s.lookup a := rfl
@[simp] theorem lookup_list_to_finmap (a : α) (s : list (sigma β)) :
lookup a s.to_finmap = s.lookup a :=
by rw [list.to_finmap, lookup_to_finmap, lookup_to_alist]
@[simp] theorem lookup_empty (a) : lookup a (∅ : finmap β) = none :=
rfl
theorem lookup_is_some {a : α} {s : finmap β} :
(s.lookup a).is_some ↔ a ∈ s :=
induction_on s $ λ s, alist.lookup_is_some
theorem lookup_eq_none {a} {s : finmap β} : lookup a s = none ↔ a ∉ s :=
induction_on s $ λ s, alist.lookup_eq_none
lemma mem_lookup_iff {f : finmap β} {a : α} {b : β a} :
b ∈ f.lookup a ↔ sigma.mk a b ∈ f.entries :=
by { rcases f with ⟨⟨l⟩, hl⟩, exact list.mem_lookup_iff hl }
/-- A version of `finmap.mem_lookup_iff` with LHS in the simp-normal form. -/
lemma lookup_eq_some_iff {f : finmap β} {a : α} {b : β a} :
f.lookup a = some b ↔ sigma.mk a b ∈ f.entries :=
mem_lookup_iff
@[simp] lemma sigma_keys_lookup (f : finmap β) :
f.keys.sigma (λ i, (f.lookup i).to_finset) = ⟨f.entries, f.nodup_entries⟩ :=
begin
ext x,
have : x ∈ f.entries → x.fst ∈ f.keys, from multiset.mem_map_of_mem _,
simpa [lookup_eq_some_iff]
end
@[simp] lemma lookup_singleton_eq {a : α} {b : β a} : (singleton a b).lookup a = some b :=
by rw [singleton, lookup_to_finmap, alist.singleton, alist.lookup, lookup_cons_eq]
instance (a : α) (s : finmap β) : decidable (a ∈ s) :=
decidable_of_iff _ lookup_is_some
lemma mem_iff {a : α} {s : finmap β} : a ∈ s ↔ ∃ b, s.lookup a = some b :=
induction_on s $ λ s,
iff.trans list.mem_keys $ exists_congr $ λ b,
(list.mem_lookup_iff s.nodupkeys).symm
lemma mem_of_lookup_eq_some {a : α} {b : β a} {s : finmap β} (h : s.lookup a = some b) : a ∈ s :=
mem_iff.mpr ⟨_, h⟩
theorem ext_lookup {s₁ s₂ : finmap β} : (∀ x, s₁.lookup x = s₂.lookup x) → s₁ = s₂ :=
induction_on₂ s₁ s₂ $ λ s₁ s₂ h,
begin
simp only [alist.lookup, lookup_to_finmap] at h,
rw [alist.to_finmap_eq],
apply lookup_ext s₁.nodupkeys s₂.nodupkeys,
intros x y,
rw h,
end
/-- An equivalence between `finmap β` and pairs `(keys : finset α, lookup : Π a, option (β a))` such
that `(lookup a).is_some ↔ a ∈ keys`. -/
@[simps apply_coe_fst apply_coe_snd]
def keys_lookup_equiv :
finmap β ≃ {f : finset α × (Π a, option (β a)) // ∀ i, (f.2 i).is_some ↔ i ∈ f.1} :=
{ to_fun := λ f, ⟨(f.keys, λ i, f.lookup i), λ i, lookup_is_some⟩,
inv_fun := λ f, ⟨(f.1.1.sigma $ λ i, (f.1.2 i).to_finset).val,
begin
refine multiset.nodup_keys.1 ((finset.nodup _).map_on _),
simp only [finset.mem_val, finset.mem_sigma, option.mem_to_finset, option.mem_def],
rintro ⟨i, x⟩ ⟨hi, hx⟩ ⟨j, y⟩ ⟨hj, hy⟩ (rfl : i = j),
obtain rfl : x = y, from option.some.inj (hx.symm.trans hy),
refl
end⟩,
left_inv := λ f, ext $ by simp,
right_inv := λ ⟨⟨s, f⟩, hf⟩,
begin
ext : 2; dsimp [keys],
{ ext1 i,
have : i ∈ s → (∃ x, f i = some x),
from λ hi, ⟨option.get _, option.get_mem $ (hf i).2 hi⟩,
simpa [multiset.keys] },
{ ext i x : 2,
simp only [option.mem_def, lookup_eq_some_iff, finset.mem_val, finset.mem_sigma,
option.mem_to_finset, and_iff_right_iff_imp, ← hf],
exact λ h, option.is_some_iff_exists.2 ⟨_, h⟩ }
end }
@[simp] lemma keys_lookup_equiv_symm_apply_keys :
∀ f : {f : finset α × (Π a, option (β a)) // ∀ i, (f.2 i).is_some ↔ i ∈ f.1},
(keys_lookup_equiv.symm f).keys = (f : finset α × Π a, option (β a)).1 :=
keys_lookup_equiv.surjective.forall.2 $ λ f,
by simp only [equiv.symm_apply_apply, keys_lookup_equiv_apply_coe_fst]
@[simp] lemma keys_lookup_equiv_symm_apply_lookup :
∀ (f : {f : finset α × (Π a, option (β a)) // ∀ i, (f.2 i).is_some ↔ i ∈ f.1}) a,
(keys_lookup_equiv.symm f).lookup a = (f : finset α × Π a, option (β a)).2 a :=
keys_lookup_equiv.surjective.forall.2 $ λ f a,
by simp only [equiv.symm_apply_apply, keys_lookup_equiv_apply_coe_snd]
/-! ### replace -/
/-- Replace a key with a given value in a finite map.
If the key is not present it does nothing. -/
def replace (a : α) (b : β a) (s : finmap β) : finmap β :=
lift_on s (λ t, ⟦replace a b t⟧) $
λ s₁ s₂ p, to_finmap_eq.2 $ perm_replace p
@[simp] theorem replace_to_finmap (a : α) (b : β a) (s : alist β) :
replace a b ⟦s⟧ = ⟦s.replace a b⟧ := by simp [replace]
@[simp] theorem keys_replace (a : α) (b : β a) (s : finmap β) :
(replace a b s).keys = s.keys :=
induction_on s $ λ s, by simp
@[simp] theorem mem_replace {a a' : α} {b : β a} {s : finmap β} :
a' ∈ replace a b s ↔ a' ∈ s :=
induction_on s $ λ s, by simp
end
/-! ### foldl -/
/-- Fold a commutative function over the key-value pairs in the map -/
def foldl {δ : Type w} (f : δ → Π a, β a → δ)
(H : ∀ d a₁ b₁ a₂ b₂, f (f d a₁ b₁) a₂ b₂ = f (f d a₂ b₂) a₁ b₁)
(d : δ) (m : finmap β) : δ :=
m.entries.foldl (λ d s, f d s.1 s.2) (λ d s t, H _ _ _ _ _) d
/-- `any f s` returns `tt` iff there exists a value `v` in `s` such that `f v = tt`. -/
def any (f : Π x, β x → bool) (s : finmap β) : bool :=
s.foldl (λ x y z, x || f y z) (by { intros, simp_rw [bool.bor_assoc, bool.bor_comm] }) ff
/-- `all f s` returns `tt` iff `f v = tt` for all values `v` in `s`. -/
def all (f : Π x, β x → bool) (s : finmap β) : bool :=
s.foldl (λ x y z, x && f y z) (by { intros, simp_rw [bool.band_assoc, bool.band_comm] }) tt
/-! ### erase -/
section
variables [decidable_eq α]
/-- Erase a key from the map. If the key is not present it does nothing. -/
def erase (a : α) (s : finmap β) : finmap β :=
lift_on s (λ t, ⟦erase a t⟧) $
λ s₁ s₂ p, to_finmap_eq.2 $ perm_erase p
@[simp] theorem erase_to_finmap (a : α) (s : alist β) :
erase a ⟦s⟧ = ⟦s.erase a⟧ := by simp [erase]
@[simp] theorem keys_erase_to_finset (a : α) (s : alist β) :
keys ⟦s.erase a⟧ = (keys ⟦s⟧).erase a :=
by simp [finset.erase, keys, alist.erase, keys_kerase]
@[simp] theorem keys_erase (a : α) (s : finmap β) :
(erase a s).keys = s.keys.erase a :=
induction_on s $ λ s, by simp
@[simp] theorem mem_erase {a a' : α} {s : finmap β} : a' ∈ erase a s ↔ a' ≠ a ∧ a' ∈ s :=
induction_on s $ λ s, by simp
theorem not_mem_erase_self {a : α} {s : finmap β} : ¬ a ∈ erase a s :=
by rw [mem_erase, not_and_distrib, not_not]; left; refl
@[simp] theorem lookup_erase (a) (s : finmap β) : lookup a (erase a s) = none :=
induction_on s $ lookup_erase a
@[simp] theorem lookup_erase_ne {a a'} {s : finmap β} (h : a ≠ a') :
lookup a (erase a' s) = lookup a s :=
induction_on s $ λ s, lookup_erase_ne h
theorem erase_erase {a a' : α} {s : finmap β} : erase a (erase a' s) = erase a' (erase a s) :=
induction_on s $ λ s, ext (by simp only [erase_erase, erase_to_finmap])
/-! ### sdiff -/
/-- `sdiff s s'` consists of all key-value pairs from `s` and `s'` where the keys are in `s` or
`s'` but not both. -/
def sdiff (s s' : finmap β) : finmap β :=
s'.foldl (λ s x _, s.erase x) (λ a₀ a₁ _ a₂ _, erase_erase) s
instance : has_sdiff (finmap β) := ⟨sdiff⟩
/-! ### insert -/
/-- Insert a key-value pair into a finite map, replacing any existing pair with
the same key. -/
def insert (a : α) (b : β a) (s : finmap β) : finmap β :=
lift_on s (λ t, ⟦insert a b t⟧) $
λ s₁ s₂ p, to_finmap_eq.2 $ perm_insert p
@[simp] theorem insert_to_finmap (a : α) (b : β a) (s : alist β) :
insert a b ⟦s⟧ = ⟦s.insert a b⟧ := by simp [insert]
theorem insert_entries_of_neg {a : α} {b : β a} {s : finmap β} : a ∉ s →
(insert a b s).entries = ⟨a, b⟩ ::ₘ s.entries :=
induction_on s $ λ s h,
by simp [insert_entries_of_neg (mt mem_to_finmap.1 h)]
@[simp] theorem mem_insert {a a' : α} {b' : β a'} {s : finmap β} :
a ∈ insert a' b' s ↔ a = a' ∨ a ∈ s :=
induction_on s mem_insert
@[simp] theorem lookup_insert {a} {b : β a} (s : finmap β) :
lookup a (insert a b s) = some b :=
induction_on s $ λ s,
by simp only [insert_to_finmap, lookup_to_finmap, lookup_insert]
@[simp] theorem lookup_insert_of_ne {a a'} {b : β a} (s : finmap β) (h : a' ≠ a) :
lookup a' (insert a b s) = lookup a' s :=
induction_on s $ λ s,
by simp only [insert_to_finmap, lookup_to_finmap, lookup_insert_ne h]
@[simp] theorem insert_insert {a} {b b' : β a} (s : finmap β) :
(s.insert a b).insert a b' = s.insert a b' :=
induction_on s $ λ s,
by simp only [insert_to_finmap, insert_insert]
theorem insert_insert_of_ne {a a'} {b : β a} {b' : β a'} (s : finmap β) (h : a ≠ a') :
(s.insert a b).insert a' b' = (s.insert a' b').insert a b :=
induction_on s $ λ s,
by simp only [insert_to_finmap, alist.to_finmap_eq, insert_insert_of_ne _ h]
theorem to_finmap_cons (a : α) (b : β a) (xs : list (sigma β)) :
list.to_finmap (⟨a,b⟩ :: xs) = insert a b xs.to_finmap := rfl
theorem mem_list_to_finmap (a : α) (xs : list (sigma β)) :
a ∈ xs.to_finmap ↔ (∃ b : β a, sigma.mk a b ∈ xs) :=
by { induction xs with x xs; [skip, cases x];
simp only [to_finmap_cons, *, not_mem_empty, exists_or_distrib, not_mem_nil, to_finmap_nil,
exists_false, mem_cons_iff, mem_insert, exists_and_distrib_left];
apply or_congr _ iff.rfl,
conv { to_lhs, rw ← and_true (a = x_fst) },
apply and_congr_right, rintro ⟨⟩, simp only [exists_eq, heq_iff_eq] }
@[simp] theorem insert_singleton_eq {a : α} {b b' : β a} :
insert a b (singleton a b') = singleton a b :=
by simp only [singleton, finmap.insert_to_finmap, alist.insert_singleton_eq]
/-! ### extract -/
/-- Erase a key from the map, and return the corresponding value, if found. -/
def extract (a : α) (s : finmap β) : option (β a) × finmap β :=
lift_on s (λ t, prod.map id to_finmap (extract a t)) $
λ s₁ s₂ p, by simp [perm_lookup p, to_finmap_eq, perm_erase p]
@[simp] theorem extract_eq_lookup_erase (a : α) (s : finmap β) :
extract a s = (lookup a s, erase a s) :=
induction_on s $ λ s, by simp [extract]
/-! ### union -/
/-- `s₁ ∪ s₂` is the key-based union of two finite maps. It is left-biased: if
there exists an `a ∈ s₁`, `lookup a (s₁ ∪ s₂) = lookup a s₁`. -/
def union (s₁ s₂ : finmap β) : finmap β :=
lift_on₂ s₁ s₂ (λ s₁ s₂, ⟦s₁ ∪ s₂⟧) $
λ s₁ s₂ s₃ s₄ p₁₃ p₂₄, to_finmap_eq.mpr $ perm_union p₁₃ p₂₄
instance : has_union (finmap β) := ⟨union⟩
@[simp] theorem mem_union {a} {s₁ s₂ : finmap β} :
a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ :=
induction_on₂ s₁ s₂ $ λ _ _, mem_union
@[simp] theorem union_to_finmap (s₁ s₂ : alist β) : ⟦s₁⟧ ∪ ⟦s₂⟧ = ⟦s₁ ∪ s₂⟧ :=
by simp [(∪), union]
theorem keys_union {s₁ s₂ : finmap β} : (s₁ ∪ s₂).keys = s₁.keys ∪ s₂.keys :=
induction_on₂ s₁ s₂ $ λ s₁ s₂, finset.ext $ by simp [keys]
@[simp] theorem lookup_union_left {a} {s₁ s₂ : finmap β} :
a ∈ s₁ → lookup a (s₁ ∪ s₂) = lookup a s₁ :=
induction_on₂ s₁ s₂ $ λ s₁ s₂, lookup_union_left
@[simp] theorem lookup_union_right {a} {s₁ s₂ : finmap β} :
a ∉ s₁ → lookup a (s₁ ∪ s₂) = lookup a s₂ :=
induction_on₂ s₁ s₂ $ λ s₁ s₂, lookup_union_right
theorem lookup_union_left_of_not_in {a} {s₁ s₂ : finmap β} (h : a ∉ s₂) :
lookup a (s₁ ∪ s₂) = lookup a s₁ :=
begin
by_cases h' : a ∈ s₁,
{ rw lookup_union_left h' },
{ rw [lookup_union_right h', lookup_eq_none.mpr h, lookup_eq_none.mpr h'] }
end
@[simp] theorem mem_lookup_union {a} {b : β a} {s₁ s₂ : finmap β} :
b ∈ lookup a (s₁ ∪ s₂) ↔ b ∈ lookup a s₁ ∨ a ∉ s₁ ∧ b ∈ lookup a s₂ :=
induction_on₂ s₁ s₂ $ λ s₁ s₂, mem_lookup_union
theorem mem_lookup_union_middle {a} {b : β a} {s₁ s₂ s₃ : finmap β} :
b ∈ lookup a (s₁ ∪ s₃) → a ∉ s₂ → b ∈ lookup a (s₁ ∪ s₂ ∪ s₃) :=
induction_on₃ s₁ s₂ s₃ $ λ s₁ s₂ s₃, mem_lookup_union_middle
theorem insert_union {a} {b : β a} {s₁ s₂ : finmap β} :
insert a b (s₁ ∪ s₂) = insert a b s₁ ∪ s₂ :=
induction_on₂ s₁ s₂ $ λ a₁ a₂, by simp [insert_union]
theorem union_assoc {s₁ s₂ s₃ : finmap β} : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) :=
induction_on₃ s₁ s₂ s₃ $ λ s₁ s₂ s₃,
by simp only [alist.to_finmap_eq, union_to_finmap, alist.union_assoc]
@[simp] theorem empty_union {s₁ : finmap β} : ∅ ∪ s₁ = s₁ :=
induction_on s₁ $ λ s₁, by rw ← empty_to_finmap;
simp [- empty_to_finmap, alist.to_finmap_eq, union_to_finmap, alist.union_assoc]
@[simp] theorem union_empty {s₁ : finmap β} : s₁ ∪ ∅ = s₁ :=
induction_on s₁ $ λ s₁, by rw ← empty_to_finmap;
simp [- empty_to_finmap, alist.to_finmap_eq, union_to_finmap, alist.union_assoc]
theorem erase_union_singleton (a : α) (b : β a) (s : finmap β) (h : s.lookup a = some b) :
s.erase a ∪ singleton a b = s :=
ext_lookup
(λ x, by { by_cases h' : x = a,
{ subst a, rw [lookup_union_right not_mem_erase_self, lookup_singleton_eq, h], },
{ have : x ∉ singleton a b, { rwa mem_singleton },
rw [lookup_union_left_of_not_in this, lookup_erase_ne h'] } } )
end
/-! ### disjoint -/
/-- `disjoint s₁ s₂` holds if `s₁` and `s₂` have no keys in common. -/
def disjoint (s₁ s₂ : finmap β) : Prop :=
∀ x ∈ s₁, ¬ x ∈ s₂
lemma disjoint_empty (x : finmap β) : disjoint ∅ x .
@[symm]
lemma disjoint.symm (x y : finmap β) (h : disjoint x y) : disjoint y x :=
λ p hy hx, h p hx hy
lemma disjoint.symm_iff (x y : finmap β) : disjoint x y ↔ disjoint y x :=
⟨disjoint.symm x y, disjoint.symm y x⟩
section
variables [decidable_eq α]
instance : decidable_rel (@disjoint α β) :=
λ x y, by dsimp only [disjoint]; apply_instance
lemma disjoint_union_left (x y z : finmap β) : disjoint (x ∪ y) z ↔ disjoint x z ∧ disjoint y z :=
by simp [disjoint, finmap.mem_union, or_imp_distrib, forall_and_distrib]
lemma disjoint_union_right (x y z : finmap β) : disjoint x (y ∪ z) ↔ disjoint x y ∧ disjoint x z :=
by rw [disjoint.symm_iff, disjoint_union_left, disjoint.symm_iff _ x, disjoint.symm_iff _ x]
theorem union_comm_of_disjoint {s₁ s₂ : finmap β} : disjoint s₁ s₂ → s₁ ∪ s₂ = s₂ ∪ s₁ :=
induction_on₂ s₁ s₂ $ λ s₁ s₂,
by { intros h, simp only [alist.to_finmap_eq, union_to_finmap, alist.union_comm_of_disjoint h] }
theorem union_cancel {s₁ s₂ s₃ : finmap β} (h : disjoint s₁ s₃) (h' : disjoint s₂ s₃) :
s₁ ∪ s₃ = s₂ ∪ s₃ ↔ s₁ = s₂ :=
⟨λ h'', begin
apply ext_lookup, intro x,
have : (s₁ ∪ s₃).lookup x = (s₂ ∪ s₃).lookup x, from h'' ▸ rfl,
by_cases hs₁ : x ∈ s₁,
{ rwa [lookup_union_left hs₁, lookup_union_left_of_not_in (h _ hs₁)] at this, },
{ by_cases hs₂ : x ∈ s₂,
{ rwa [lookup_union_left_of_not_in (h' _ hs₂), lookup_union_left hs₂] at this, },
{ rw [lookup_eq_none.mpr hs₁, lookup_eq_none.mpr hs₂] } }
end,
λ h, h ▸ rfl⟩
end
end finmap
|
810ebb517d09c06a899ca77ed08a3740e760beda | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/toFieldNameIssue.lean | abd3f37eb4e149545b9c8aacf08c59331706d4fb | [
"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 | 646 | lean | structure Foo.A where
x : Nat
structure Boo.A extends Foo.A where
y : Nat
structure B extends Boo.A where
z : Nat
def f1 (x y z : Nat) : B :=
{ x, y, z }
theorem ex1 (x y z : Nat) : f1 x y z = ⟨⟨⟨x⟩, y⟩, z⟩ :=
rfl
theorem ex2 (x y z : Nat) : f1 x y z = B.mk (Boo.A.mk (Foo.A.mk x) y) z :=
rfl
#check { x := 0, y := 1, z := 2 : B }
structure Foo.C where
x : Nat
y : Nat
structure Boo.C where
x : Nat
z : Nat
structure D extends Foo.C, Boo.C
def f2 (x y z : Nat) : D :=
{ x, y, z }
theorem ex3 (x y z : Nat) : f2 x y z = D.mk ⟨x, y⟩ z :=
rfl
#check { x := 0, y := 1, z := 2 : D }
#print D.toC_1
|
5927ceeea3e59d401afadee18fa848ada736f094 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/ring_theory/unique_factorization_domain.lean | d4c443fe63a912bb1a1dbc810bab7834ea27e91c | [
"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 | 82,851 | lean | /-
Copyright (c) 2018 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Jens Wagemaker, Aaron Anderson
-/
import algebra.big_operators.associated
import algebra.gcd_monoid.basic
import data.finsupp.multiset
import ring_theory.noetherian
import ring_theory.multiplicity
/-!
# Unique factorization
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
## Main Definitions
* `wf_dvd_monoid` holds for `monoid`s for which a strict divisibility relation is
well-founded.
* `unique_factorization_monoid` holds for `wf_dvd_monoid`s where
`irreducible` is equivalent to `prime`
## To do
* set up the complete lattice structure on `factor_set`.
-/
variables {α : Type*}
local infix ` ~ᵤ ` : 50 := associated
/-- Well-foundedness of the strict version of |, which is equivalent to the descending chain
condition on divisibility and to the ascending chain condition on
principal ideals in an integral domain.
-/
class wf_dvd_monoid (α : Type*) [comm_monoid_with_zero α] : Prop :=
(well_founded_dvd_not_unit : well_founded (@dvd_not_unit α _))
export wf_dvd_monoid (well_founded_dvd_not_unit)
@[priority 100] -- see Note [lower instance priority]
instance is_noetherian_ring.wf_dvd_monoid [comm_ring α] [is_domain α] [is_noetherian_ring α] :
wf_dvd_monoid α :=
⟨by { convert inv_image.wf (λ a, ideal.span ({a} : set α)) (well_founded_submodule_gt _ _),
ext,
exact ideal.span_singleton_lt_span_singleton.symm }⟩
namespace wf_dvd_monoid
variables [comm_monoid_with_zero α]
open associates nat
theorem of_wf_dvd_monoid_associates (h : wf_dvd_monoid (associates α)): wf_dvd_monoid α :=
⟨begin
haveI := h,
refine (surjective.well_founded_iff mk_surjective _).2 well_founded_dvd_not_unit,
intros, rw mk_dvd_not_unit_mk_iff
end⟩
variables [wf_dvd_monoid α]
instance wf_dvd_monoid_associates : wf_dvd_monoid (associates α) :=
⟨begin
refine (surjective.well_founded_iff mk_surjective _).1 well_founded_dvd_not_unit,
intros, rw mk_dvd_not_unit_mk_iff
end⟩
theorem well_founded_associates : well_founded ((<) : associates α → associates α → Prop) :=
subrelation.wf (λ x y, dvd_not_unit_of_lt) well_founded_dvd_not_unit
local attribute [elab_as_eliminator] well_founded.fix
lemma exists_irreducible_factor {a : α} (ha : ¬ is_unit a) (ha0 : a ≠ 0) :
∃ i, irreducible i ∧ i ∣ a :=
let ⟨b, hs, hr⟩ := well_founded_dvd_not_unit.has_min {b | b ∣ a ∧ ¬ is_unit b} ⟨a, dvd_rfl, ha⟩ in
⟨b, ⟨hs.2, λ c d he, let h := dvd_trans ⟨d, he⟩ hs.1 in or_iff_not_imp_left.2 $
λ hc, of_not_not $ λ hd, hr c ⟨h, hc⟩ ⟨ne_zero_of_dvd_ne_zero ha0 h, d, hd, he⟩⟩, hs.1⟩
@[elab_as_eliminator] lemma induction_on_irreducible {P : α → Prop} (a : α)
(h0 : P 0) (hu : ∀ u : α, is_unit u → P u)
(hi : ∀ a i : α, a ≠ 0 → irreducible i → P a → P (i * a)) :
P a :=
by haveI := classical.dec; exact
well_founded_dvd_not_unit.fix
(λ a ih, if ha0 : a = 0 then ha0.substr h0
else if hau : is_unit a then hu a hau
else let ⟨i, hii, b, hb⟩ := exists_irreducible_factor hau ha0,
hb0 : b ≠ 0 := ne_zero_of_dvd_ne_zero ha0 ⟨i, mul_comm i b ▸ hb⟩ in
hb.symm ▸ hi b i hb0 hii $ ih b ⟨hb0, i, hii.1, mul_comm i b ▸ hb⟩)
a
lemma exists_factors (a : α) : a ≠ 0 →
∃ f : multiset α, (∀ b ∈ f, irreducible b) ∧ associated f.prod a :=
induction_on_irreducible a
(λ h, (h rfl).elim)
(λ u hu _, ⟨0, λ _ h, h.elim, hu.unit, one_mul _⟩)
(λ a i ha0 hi ih _,
let ⟨s, hs⟩ := ih ha0 in
⟨i ::ₘ s, λ b H, (multiset.mem_cons.1 H).elim (λ h, h.symm ▸ hi) (hs.1 b),
by { rw s.prod_cons i, exact hs.2.mul_left i }⟩)
lemma not_unit_iff_exists_factors_eq (a : α) (hn0 : a ≠ 0) :
¬ is_unit a ↔ ∃ f : multiset α, (∀ b ∈ f, irreducible b) ∧ f.prod = a ∧ f ≠ ∅ :=
⟨λ hnu, begin
obtain ⟨f, hi, u, rfl⟩ := exists_factors a hn0,
obtain ⟨b, h⟩ := multiset.exists_mem_of_ne_zero (λ h : f = 0, hnu $ by simp [h]),
classical, refine ⟨(f.erase b).cons (b * u), λ a ha, _, _, multiset.cons_ne_zero⟩,
{ obtain (rfl|ha) := multiset.mem_cons.1 ha,
exacts [associated.irreducible ⟨u,rfl⟩ (hi b h), hi a (multiset.mem_of_mem_erase ha)] },
{ rw [multiset.prod_cons, mul_comm b, mul_assoc, multiset.prod_erase h, mul_comm] },
end,
λ ⟨f, hi, he, hne⟩, let ⟨b, h⟩ := multiset.exists_mem_of_ne_zero hne in
not_is_unit_of_not_is_unit_dvd (hi b h).not_unit $ he ▸ multiset.dvd_prod h⟩
end wf_dvd_monoid
theorem wf_dvd_monoid.of_well_founded_associates [cancel_comm_monoid_with_zero α]
(h : well_founded ((<) : associates α → associates α → Prop)) : wf_dvd_monoid α :=
wf_dvd_monoid.of_wf_dvd_monoid_associates
⟨by { convert h, ext, exact associates.dvd_not_unit_iff_lt }⟩
theorem wf_dvd_monoid.iff_well_founded_associates [cancel_comm_monoid_with_zero α] :
wf_dvd_monoid α ↔ well_founded ((<) : associates α → associates α → Prop) :=
⟨by apply wf_dvd_monoid.well_founded_associates, wf_dvd_monoid.of_well_founded_associates⟩
section prio
set_option default_priority 100 -- see Note [default priority]
/-- unique factorization monoids.
These are defined as `cancel_comm_monoid_with_zero`s with well-founded strict divisibility
relations, but this is equivalent to more familiar definitions:
Each element (except zero) is uniquely represented as a multiset of irreducible factors.
Uniqueness is only up to associated elements.
Each element (except zero) is non-uniquely represented as a multiset
of prime factors.
To define a UFD using the definition in terms of multisets
of irreducible factors, use the definition `of_exists_unique_irreducible_factors`
To define a UFD using the definition in terms of multisets
of prime factors, use the definition `of_exists_prime_factors`
-/
class unique_factorization_monoid (α : Type*) [cancel_comm_monoid_with_zero α]
extends wf_dvd_monoid α : Prop :=
(irreducible_iff_prime : ∀ {a : α}, irreducible a ↔ prime a)
/-- Can't be an instance because it would cause a loop `ufm → wf_dvd_monoid → ufm → ...`. -/
@[reducible] lemma ufm_of_gcd_of_wf_dvd_monoid [cancel_comm_monoid_with_zero α]
[wf_dvd_monoid α] [gcd_monoid α] : unique_factorization_monoid α :=
{ irreducible_iff_prime := λ _, gcd_monoid.irreducible_iff_prime
.. ‹wf_dvd_monoid α› }
instance associates.ufm [cancel_comm_monoid_with_zero α]
[unique_factorization_monoid α] : unique_factorization_monoid (associates α) :=
{ irreducible_iff_prime := by { rw ← associates.irreducible_iff_prime_iff,
apply unique_factorization_monoid.irreducible_iff_prime, }
.. (wf_dvd_monoid.wf_dvd_monoid_associates : wf_dvd_monoid (associates α)) }
end prio
namespace unique_factorization_monoid
variables [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α]
theorem exists_prime_factors (a : α) : a ≠ 0 →
∃ f : multiset α, (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a :=
by { simp_rw ← unique_factorization_monoid.irreducible_iff_prime,
apply wf_dvd_monoid.exists_factors a }
@[elab_as_eliminator] lemma induction_on_prime {P : α → Prop}
(a : α) (h₁ : P 0) (h₂ : ∀ x : α, is_unit x → P x)
(h₃ : ∀ a p : α, a ≠ 0 → prime p → P a → P (p * a)) : P a :=
begin
simp_rw ← unique_factorization_monoid.irreducible_iff_prime at h₃,
exact wf_dvd_monoid.induction_on_irreducible a h₁ h₂ h₃,
end
end unique_factorization_monoid
lemma prime_factors_unique [cancel_comm_monoid_with_zero α] : ∀ {f g : multiset α},
(∀ x ∈ f, prime x) → (∀ x ∈ g, prime x) → f.prod ~ᵤ g.prod →
multiset.rel associated f g :=
by haveI := classical.dec_eq α; exact
λ f, multiset.induction_on f
(λ g _ hg h,
multiset.rel_zero_left.2 $
multiset.eq_zero_of_forall_not_mem $ λ x hx,
have is_unit g.prod, by simpa [associated_one_iff_is_unit] using h.symm,
(hg x hx).not_unit $ is_unit_iff_dvd_one.2 $
(multiset.dvd_prod hx).trans (is_unit_iff_dvd_one.1 this))
(λ p f ih g hf hg hfg,
let ⟨b, hbg, hb⟩ := exists_associated_mem_of_dvd_prod
(hf p (by simp)) (λ q hq, hg _ hq) $
hfg.dvd_iff_dvd_right.1
(show p ∣ (p ::ₘ f).prod, by simp) in
begin
rw ← multiset.cons_erase hbg,
exact multiset.rel.cons hb (ih (λ q hq, hf _ (by simp [hq]))
(λ q (hq : q ∈ g.erase b), hg q (multiset.mem_of_mem_erase hq))
(associated.of_mul_left
(by rwa [← multiset.prod_cons, ← multiset.prod_cons, multiset.cons_erase hbg]) hb
(hf p (by simp)).ne_zero)),
end)
namespace unique_factorization_monoid
variables [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α]
lemma factors_unique {f g : multiset α} (hf : ∀ x ∈ f, irreducible x) (hg : ∀ x ∈ g, irreducible x)
(h : f.prod ~ᵤ g.prod) : multiset.rel associated f g :=
prime_factors_unique
(λ x hx, irreducible_iff_prime.mp (hf x hx))
(λ x hx, irreducible_iff_prime.mp (hg x hx))
h
end unique_factorization_monoid
/-- If an irreducible has a prime factorization,
then it is an associate of one of its prime factors. -/
lemma prime_factors_irreducible [cancel_comm_monoid_with_zero α] {a : α} {f : multiset α}
(ha : irreducible a) (pfa : (∀ b ∈ f, prime b) ∧ f.prod ~ᵤ a) :
∃ p, a ~ᵤ p ∧ f = {p} :=
begin
haveI := classical.dec_eq α,
refine multiset.induction_on f (λ h, (ha.not_unit
(associated_one_iff_is_unit.1 (associated.symm h))).elim) _ pfa.2 pfa.1,
rintros p s _ ⟨u, hu⟩ hs,
use p,
have hs0 : s = 0,
{ by_contra hs0,
obtain ⟨q, hq⟩ := multiset.exists_mem_of_ne_zero hs0,
apply (hs q (by simp [hq])).2.1,
refine (ha.is_unit_or_is_unit (_ : _ = ((p * ↑u) * (s.erase q).prod) * _)).resolve_left _,
{ rw [mul_right_comm _ _ q, mul_assoc, ← multiset.prod_cons, multiset.cons_erase hq, ← hu,
mul_comm, mul_comm p _, mul_assoc],
simp, },
apply mt is_unit_of_mul_is_unit_left (mt is_unit_of_mul_is_unit_left _),
apply (hs p (multiset.mem_cons_self _ _)).2.1 },
simp only [mul_one, multiset.prod_cons, multiset.prod_zero, hs0] at *,
exact ⟨associated.symm ⟨u, hu⟩, rfl⟩,
end
section exists_prime_factors
variables [cancel_comm_monoid_with_zero α]
variables (pf : ∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a)
include pf
lemma wf_dvd_monoid.of_exists_prime_factors : wf_dvd_monoid α :=
⟨begin
classical,
refine rel_hom_class.well_founded
(rel_hom.mk _ _ : (dvd_not_unit : α → α → Prop) →r ((<) : ℕ∞ → ℕ∞ → Prop))
(with_top.well_founded_lt nat.lt_wf),
{ intro a,
by_cases h : a = 0, { exact ⊤ },
exact (classical.some (pf a h)).card },
rintros a b ⟨ane0, ⟨c, hc, b_eq⟩⟩,
rw dif_neg ane0,
by_cases h : b = 0, { simp [h, lt_top_iff_ne_top] },
rw [dif_neg h, with_top.coe_lt_coe],
have cne0 : c ≠ 0, { refine mt (λ con, _) h, rw [b_eq, con, mul_zero] },
calc multiset.card (classical.some (pf a ane0))
< _ + multiset.card (classical.some (pf c cne0)) :
lt_add_of_pos_right _ (multiset.card_pos.mpr (λ con, hc (associated_one_iff_is_unit.mp _)))
... = multiset.card (classical.some (pf a ane0) + classical.some (pf c cne0)) :
(multiset.card_add _ _).symm
... = multiset.card (classical.some (pf b h)) :
multiset.card_eq_card_of_rel (prime_factors_unique _ (classical.some_spec (pf _ h)).1 _),
{ convert (classical.some_spec (pf c cne0)).2.symm,
rw [con, multiset.prod_zero] },
{ intros x hadd,
rw multiset.mem_add at hadd,
cases hadd; apply (classical.some_spec (pf _ _)).1 _ hadd },
{ rw multiset.prod_add,
transitivity a * c,
{ apply associated.mul_mul; apply (classical.some_spec (pf _ _)).2 },
{ rw ← b_eq,
apply (classical.some_spec (pf _ _)).2.symm, } }
end⟩
lemma irreducible_iff_prime_of_exists_prime_factors {p : α} : irreducible p ↔ prime p :=
begin
by_cases hp0 : p = 0,
{ simp [hp0] },
refine ⟨λ h, _, prime.irreducible⟩,
obtain ⟨f, hf⟩ := pf p hp0,
obtain ⟨q, hq, rfl⟩ := prime_factors_irreducible h hf,
rw hq.prime_iff,
exact hf.1 q (multiset.mem_singleton_self _)
end
theorem unique_factorization_monoid.of_exists_prime_factors :
unique_factorization_monoid α :=
{ irreducible_iff_prime := λ _, irreducible_iff_prime_of_exists_prime_factors pf,
.. wf_dvd_monoid.of_exists_prime_factors pf }
end exists_prime_factors
theorem unique_factorization_monoid.iff_exists_prime_factors [cancel_comm_monoid_with_zero α] :
unique_factorization_monoid α ↔
(∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a) :=
⟨λ h, @unique_factorization_monoid.exists_prime_factors _ _ h,
unique_factorization_monoid.of_exists_prime_factors⟩
section
variables {β : Type*} [cancel_comm_monoid_with_zero α] [cancel_comm_monoid_with_zero β]
lemma mul_equiv.unique_factorization_monoid (e : α ≃* β)
(hα : unique_factorization_monoid α) : unique_factorization_monoid β :=
begin
rw unique_factorization_monoid.iff_exists_prime_factors at hα ⊢, intros a ha,
obtain ⟨w,hp,u,h⟩ := hα (e.symm a) (λ h, ha $ by { convert ← map_zero e, simp [← h] }),
exact ⟨ w.map e,
λ b hb, let ⟨c,hc,he⟩ := multiset.mem_map.1 hb in he ▸ e.prime_iff.1 (hp c hc),
units.map e.to_monoid_hom u,
by { erw [multiset.prod_hom, ← e.map_mul, h], simp } ⟩,
end
lemma mul_equiv.unique_factorization_monoid_iff (e : α ≃* β) :
unique_factorization_monoid α ↔ unique_factorization_monoid β :=
⟨ e.unique_factorization_monoid, e.symm.unique_factorization_monoid ⟩
end
theorem irreducible_iff_prime_of_exists_unique_irreducible_factors [cancel_comm_monoid_with_zero α]
(eif : ∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, irreducible b) ∧ f.prod ~ᵤ a)
(uif : ∀ (f g : multiset α),
(∀ x ∈ f, irreducible x) → (∀ x ∈ g, irreducible x) → f.prod ~ᵤ g.prod →
multiset.rel associated f g)
(p : α) : irreducible p ↔ prime p :=
⟨by letI := classical.dec_eq α; exact λ hpi,
⟨hpi.ne_zero, hpi.1,
λ a b ⟨x, hx⟩,
if hab0 : a * b = 0
then (eq_zero_or_eq_zero_of_mul_eq_zero hab0).elim
(λ ha0, by simp [ha0])
(λ hb0, by simp [hb0])
else
have hx0 : x ≠ 0, from λ hx0, by simp * at *,
have ha0 : a ≠ 0, from left_ne_zero_of_mul hab0,
have hb0 : b ≠ 0, from right_ne_zero_of_mul hab0,
begin
cases eif x hx0 with fx hfx,
cases eif a ha0 with fa hfa,
cases eif b hb0 with fb hfb,
have h : multiset.rel associated (p ::ₘ fx) (fa + fb),
{ apply uif,
{ exact λ i hi, (multiset.mem_cons.1 hi).elim (λ hip, hip.symm ▸ hpi) (hfx.1 _), },
{ exact λ i hi, (multiset.mem_add.1 hi).elim (hfa.1 _) (hfb.1 _), },
calc multiset.prod (p ::ₘ fx)
~ᵤ a * b : by rw [hx, multiset.prod_cons];
exact hfx.2.mul_left _
... ~ᵤ (fa).prod * (fb).prod :
hfa.2.symm.mul_mul hfb.2.symm
... = _ : by rw multiset.prod_add, },
exact let ⟨q, hqf, hq⟩ := multiset.exists_mem_of_rel_of_mem h
(multiset.mem_cons_self p _) in
(multiset.mem_add.1 hqf).elim
(λ hqa, or.inl $ hq.dvd_iff_dvd_left.2 $
hfa.2.dvd_iff_dvd_right.1
(multiset.dvd_prod hqa))
(λ hqb, or.inr $ hq.dvd_iff_dvd_left.2 $
hfb.2.dvd_iff_dvd_right.1
(multiset.dvd_prod hqb))
end⟩, prime.irreducible⟩
theorem unique_factorization_monoid.of_exists_unique_irreducible_factors
[cancel_comm_monoid_with_zero α]
(eif : ∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, irreducible b) ∧ f.prod ~ᵤ a)
(uif : ∀ (f g : multiset α),
(∀ x ∈ f, irreducible x) → (∀ x ∈ g, irreducible x) → f.prod ~ᵤ g.prod →
multiset.rel associated f g) :
unique_factorization_monoid α :=
unique_factorization_monoid.of_exists_prime_factors (by
{ convert eif,
simp_rw irreducible_iff_prime_of_exists_unique_irreducible_factors eif uif })
namespace unique_factorization_monoid
variables [cancel_comm_monoid_with_zero α] [decidable_eq α]
variables [unique_factorization_monoid α]
/-- Noncomputably determines the multiset of prime factors. -/
noncomputable def factors (a : α) : multiset α := if h : a = 0 then 0 else
classical.some (unique_factorization_monoid.exists_prime_factors a h)
theorem factors_prod {a : α} (ane0 : a ≠ 0) : associated (factors a).prod a :=
begin
rw [factors, dif_neg ane0],
exact (classical.some_spec (exists_prime_factors a ane0)).2
end
lemma ne_zero_of_mem_factors {p a : α} (h : p ∈ factors a) : a ≠ 0 :=
begin
intro ha,
rw [factors, dif_pos ha] at h,
exact multiset.not_mem_zero _ h
end
lemma dvd_of_mem_factors {p a : α} (h : p ∈ factors a) : p ∣ a :=
dvd_trans (multiset.dvd_prod h) (associated.dvd (factors_prod (ne_zero_of_mem_factors h)))
theorem prime_of_factor {a : α} (x : α) (hx : x ∈ factors a) : prime x :=
begin
have ane0 := ne_zero_of_mem_factors hx,
rw [factors, dif_neg ane0] at hx,
exact (classical.some_spec (unique_factorization_monoid.exists_prime_factors a ane0)).1 x hx,
end
theorem irreducible_of_factor {a : α} : ∀ (x : α), x ∈ factors a → irreducible x :=
λ x h, (prime_of_factor x h).irreducible
@[simp] lemma factors_zero : factors (0 : α) = 0 :=
by simp [factors]
@[simp] lemma factors_one : factors (1 : α) = 0 :=
begin
nontriviality α using [factors],
rw ← multiset.rel_zero_right,
refine factors_unique irreducible_of_factor (λ x hx, (multiset.not_mem_zero x hx).elim) _,
rw multiset.prod_zero,
exact factors_prod one_ne_zero,
end
lemma exists_mem_factors_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) : p ∣ a →
∃ q ∈ factors a, p ~ᵤ q :=
λ ⟨b, hb⟩,
have hb0 : b ≠ 0, from λ hb0, by simp * at *,
have multiset.rel associated (p ::ₘ factors b) (factors a),
from factors_unique
(λ x hx, (multiset.mem_cons.1 hx).elim (λ h, h.symm ▸ hp) (irreducible_of_factor _))
irreducible_of_factor
(associated.symm $ calc multiset.prod (factors a) ~ᵤ a : factors_prod ha0
... = p * b : hb
... ~ᵤ multiset.prod (p ::ₘ factors b) :
by rw multiset.prod_cons; exact (factors_prod hb0).symm.mul_left _),
multiset.exists_mem_of_rel_of_mem this (by simp)
lemma exists_mem_factors {x : α} (hx : x ≠ 0) (h : ¬ is_unit x) : ∃ p, p ∈ factors x :=
begin
obtain ⟨p', hp', hp'x⟩ := wf_dvd_monoid.exists_irreducible_factor h hx,
obtain ⟨p, hp, hpx⟩ := exists_mem_factors_of_dvd hx hp' hp'x,
exact ⟨p, hp⟩
end
lemma factors_mul {x y : α} (hx : x ≠ 0) (hy : y ≠ 0) :
multiset.rel associated (factors (x * y)) (factors x + factors y) :=
begin
refine factors_unique irreducible_of_factor
(λ a ha, (multiset.mem_add.mp ha).by_cases (irreducible_of_factor _) (irreducible_of_factor _))
((factors_prod (mul_ne_zero hx hy)).trans _),
rw multiset.prod_add,
exact (associated.mul_mul (factors_prod hx) (factors_prod hy)).symm,
end
lemma factors_pow {x : α} (n : ℕ) :
multiset.rel associated (factors (x ^ n)) (n • factors x) :=
begin
induction n with n ih,
{ simp },
by_cases h0 : x = 0,
{ simp [h0, zero_pow n.succ_pos, smul_zero] },
rw [pow_succ, succ_nsmul],
refine multiset.rel.trans _ (factors_mul h0 (pow_ne_zero n h0)) _,
refine multiset.rel.add _ ih,
exact multiset.rel_refl_of_refl_on (λ y hy, associated.refl _),
end
@[simp] lemma factors_pos (x : α) (hx : x ≠ 0) : 0 < factors x ↔ ¬ is_unit x :=
begin
split,
{ intros h hx,
obtain ⟨p, hp⟩ := multiset.exists_mem_of_ne_zero h.ne',
exact (prime_of_factor _ hp).not_unit (is_unit_of_dvd_unit (dvd_of_mem_factors hp) hx) },
{ intros h,
obtain ⟨p, hp⟩ := exists_mem_factors hx h,
exact bot_lt_iff_ne_bot.mpr (mt multiset.eq_zero_iff_forall_not_mem.mp
(not_forall.mpr ⟨p, not_not.mpr hp⟩)) },
end
end unique_factorization_monoid
namespace unique_factorization_monoid
variables [cancel_comm_monoid_with_zero α] [decidable_eq α] [normalization_monoid α]
variables [unique_factorization_monoid α]
/-- Noncomputably determines the multiset of prime factors. -/
noncomputable def normalized_factors (a : α) : multiset α :=
multiset.map normalize $ factors a
/-- An arbitrary choice of factors of `x : M` is exactly the (unique) normalized set of factors,
if `M` has a trivial group of units. -/
@[simp] lemma factors_eq_normalized_factors {M : Type*} [cancel_comm_monoid_with_zero M]
[decidable_eq M] [unique_factorization_monoid M] [unique (Mˣ)] (x : M) :
factors x = normalized_factors x :=
begin
unfold normalized_factors,
convert (multiset.map_id (factors x)).symm,
ext p,
exact normalize_eq p
end
theorem normalized_factors_prod {a : α} (ane0 : a ≠ 0) : associated (normalized_factors a).prod a :=
begin
rw [normalized_factors, factors, dif_neg ane0],
refine associated.trans _ (classical.some_spec (exists_prime_factors a ane0)).2,
rw [← associates.mk_eq_mk_iff_associated, ← associates.prod_mk, ← associates.prod_mk,
multiset.map_map],
congr' 2,
ext,
rw [function.comp_apply, associates.mk_normalize],
end
theorem prime_of_normalized_factor {a : α} : ∀ (x : α), x ∈ normalized_factors a → prime x :=
begin
rw [normalized_factors, factors],
split_ifs with ane0, { simp },
intros x hx, rcases multiset.mem_map.1 hx with ⟨y, ⟨hy, rfl⟩⟩,
rw (normalize_associated _).prime_iff,
exact (classical.some_spec (unique_factorization_monoid.exists_prime_factors a ane0)).1 y hy,
end
theorem irreducible_of_normalized_factor {a : α} :
∀ (x : α), x ∈ normalized_factors a → irreducible x :=
λ x h, (prime_of_normalized_factor x h).irreducible
theorem normalize_normalized_factor {a : α} :
∀ (x : α), x ∈ normalized_factors a → normalize x = x :=
begin
rw [normalized_factors, factors],
split_ifs with h, { simp },
intros x hx,
obtain ⟨y, hy, rfl⟩ := multiset.mem_map.1 hx,
apply normalize_idem
end
lemma normalized_factors_irreducible {a : α} (ha : irreducible a) :
normalized_factors a = {normalize a} :=
begin
obtain ⟨p, a_assoc, hp⟩ := prime_factors_irreducible ha
⟨prime_of_normalized_factor, normalized_factors_prod ha.ne_zero⟩,
have p_mem : p ∈ normalized_factors a,
{ rw hp, exact multiset.mem_singleton_self _ },
convert hp,
rwa [← normalize_normalized_factor p p_mem, normalize_eq_normalize_iff, dvd_dvd_iff_associated]
end
lemma normalized_factors_eq_of_dvd (a : α) : ∀ (p q ∈ normalized_factors a), p ∣ q → p = q :=
begin
intros p hp q hq hdvd,
convert normalize_eq_normalize hdvd
(((prime_of_normalized_factor _ hp).irreducible).dvd_symm
((prime_of_normalized_factor _ hq).irreducible) hdvd);
apply (normalize_normalized_factor _ _).symm; assumption
end
lemma exists_mem_normalized_factors_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) : p ∣ a →
∃ q ∈ normalized_factors a, p ~ᵤ q :=
λ ⟨b, hb⟩,
have hb0 : b ≠ 0, from λ hb0, by simp * at *,
have multiset.rel associated (p ::ₘ normalized_factors b) (normalized_factors a),
from factors_unique
(λ x hx, (multiset.mem_cons.1 hx).elim (λ h, h.symm ▸ hp)
(irreducible_of_normalized_factor _))
irreducible_of_normalized_factor
(associated.symm $ calc multiset.prod (normalized_factors a) ~ᵤ a : normalized_factors_prod ha0
... = p * b : hb
... ~ᵤ multiset.prod (p ::ₘ normalized_factors b) :
by rw multiset.prod_cons; exact (normalized_factors_prod hb0).symm.mul_left _),
multiset.exists_mem_of_rel_of_mem this (by simp)
lemma exists_mem_normalized_factors {x : α} (hx : x ≠ 0) (h : ¬ is_unit x) :
∃ p, p ∈ normalized_factors x :=
begin
obtain ⟨p', hp', hp'x⟩ := wf_dvd_monoid.exists_irreducible_factor h hx,
obtain ⟨p, hp, hpx⟩ := exists_mem_normalized_factors_of_dvd hx hp' hp'x,
exact ⟨p, hp⟩
end
@[simp] lemma normalized_factors_zero : normalized_factors (0 : α) = 0 :=
by simp [normalized_factors, factors]
@[simp] lemma normalized_factors_one : normalized_factors (1 : α) = 0 :=
begin
nontriviality α using [normalized_factors, factors],
rw ← multiset.rel_zero_right,
apply factors_unique irreducible_of_normalized_factor,
{ intros x hx,
exfalso,
apply multiset.not_mem_zero x hx },
{ simp [normalized_factors_prod one_ne_zero] },
apply_instance
end
@[simp] lemma normalized_factors_mul {x y : α} (hx : x ≠ 0) (hy : y ≠ 0) :
normalized_factors (x * y) = normalized_factors x + normalized_factors y :=
begin
have h : (normalize : α → α) = associates.out ∘ associates.mk,
{ ext, rw [function.comp_apply, associates.out_mk], },
rw [← multiset.map_id' (normalized_factors (x * y)), ← multiset.map_id' (normalized_factors x),
← multiset.map_id' (normalized_factors y), ← multiset.map_congr rfl normalize_normalized_factor,
← multiset.map_congr rfl normalize_normalized_factor,
← multiset.map_congr rfl normalize_normalized_factor,
← multiset.map_add, h, ← multiset.map_map associates.out, eq_comm,
← multiset.map_map associates.out],
refine congr rfl _,
apply multiset.map_mk_eq_map_mk_of_rel,
apply factors_unique,
{ intros x hx,
rcases multiset.mem_add.1 hx with hx | hx;
exact irreducible_of_normalized_factor x hx },
{ exact irreducible_of_normalized_factor },
{ rw multiset.prod_add,
exact ((normalized_factors_prod hx).mul_mul (normalized_factors_prod hy)).trans
(normalized_factors_prod (mul_ne_zero hx hy)).symm }
end
@[simp] lemma normalized_factors_pow {x : α} (n : ℕ) :
normalized_factors (x ^ n) = n • normalized_factors x :=
begin
induction n with n ih,
{ simp },
by_cases h0 : x = 0,
{ simp [h0, zero_pow n.succ_pos, smul_zero] },
rw [pow_succ, succ_nsmul, normalized_factors_mul h0 (pow_ne_zero _ h0), ih],
end
theorem _root_.irreducible.normalized_factors_pow {p : α} (hp : irreducible p) (k : ℕ) :
normalized_factors (p ^ k) = multiset.replicate k (normalize p) :=
by rw [normalized_factors_pow, normalized_factors_irreducible hp, multiset.nsmul_singleton]
theorem normalized_factors_prod_eq (s : multiset α) (hs : ∀ a ∈ s, irreducible a) :
normalized_factors s.prod = s.map normalize :=
begin
induction s using multiset.induction with a s ih,
{ rw [multiset.prod_zero, normalized_factors_one, multiset.map_zero] },
{ have ia := hs a (multiset.mem_cons_self a _),
have ib := λ b h, hs b (multiset.mem_cons_of_mem h),
obtain rfl | ⟨b, hb⟩ := s.empty_or_exists_mem,
{ rw [multiset.cons_zero, multiset.prod_singleton,
multiset.map_singleton, normalized_factors_irreducible ia] },
haveI := nontrivial_of_ne b 0 (ib b hb).ne_zero,
rw [multiset.prod_cons, multiset.map_cons, normalized_factors_mul ia.ne_zero,
normalized_factors_irreducible ia, ih],
exacts [rfl, ib, multiset.prod_ne_zero (λ h, (ib 0 h).ne_zero rfl)] },
end
lemma dvd_iff_normalized_factors_le_normalized_factors {x y : α} (hx : x ≠ 0) (hy : y ≠ 0) :
x ∣ y ↔ normalized_factors x ≤ normalized_factors y :=
begin
split,
{ rintro ⟨c, rfl⟩,
simp [hx, right_ne_zero_of_mul hy] },
{ rw [← (normalized_factors_prod hx).dvd_iff_dvd_left,
← (normalized_factors_prod hy).dvd_iff_dvd_right],
apply multiset.prod_dvd_prod_of_le }
end
lemma associated_iff_normalized_factors_eq_normalized_factors {x y : α} (hx : x ≠ 0) (hy : y ≠ 0) :
x ~ᵤ y ↔ normalized_factors x = normalized_factors y :=
begin
refine ⟨λ h, _,
λ h, (normalized_factors_prod hx).symm.trans (trans (by rw h) (normalized_factors_prod hy))⟩,
apply le_antisymm; rw [← dvd_iff_normalized_factors_le_normalized_factors],
all_goals { simp [*, h.dvd, h.symm.dvd], },
end
theorem normalized_factors_of_irreducible_pow {p : α} (hp : irreducible p) (k : ℕ) :
normalized_factors (p ^ k) = multiset.replicate k (normalize p) :=
by rw [normalized_factors_pow, normalized_factors_irreducible hp, multiset.nsmul_singleton]
lemma zero_not_mem_normalized_factors (x : α) : (0 : α) ∉ normalized_factors x :=
λ h, prime.ne_zero (prime_of_normalized_factor _ h) rfl
lemma dvd_of_mem_normalized_factors {a p : α} (H : p ∈ normalized_factors a) : p ∣ a :=
begin
by_cases hcases : a = 0,
{ rw hcases,
exact dvd_zero p },
{ exact dvd_trans (multiset.dvd_prod H) (associated.dvd (normalized_factors_prod hcases)) },
end
lemma exists_associated_prime_pow_of_unique_normalized_factor {p r : α}
(h : ∀ {m}, m ∈ normalized_factors r → m = p) (hr : r ≠ 0) : ∃ (i : ℕ), associated (p ^ i) r :=
begin
use (normalized_factors r).card,
have := unique_factorization_monoid.normalized_factors_prod hr,
rwa [multiset.eq_replicate_of_mem (λ b, h), multiset.prod_replicate] at this
end
lemma normalized_factors_prod_of_prime [nontrivial α] [unique αˣ] {m : multiset α}
(h : ∀ p ∈ m, prime p) : (normalized_factors m.prod) = m :=
by simpa only [←multiset.rel_eq, ←associated_eq_eq] using prime_factors_unique
(prime_of_normalized_factor) h (normalized_factors_prod (m.prod_ne_zero_of_prime h))
lemma mem_normalized_factors_eq_of_associated {a b c : α} (ha : a ∈ normalized_factors c)
(hb : b ∈ normalized_factors c) (h : associated a b) : a = b :=
begin
rw [← normalize_normalized_factor a ha, ← normalize_normalized_factor b hb,
normalize_eq_normalize_iff],
apply associated.dvd_dvd h,
end
@[simp] lemma normalized_factors_pos (x : α) (hx : x ≠ 0) :
0 < normalized_factors x ↔ ¬ is_unit x :=
begin
split,
{ intros h hx,
obtain ⟨p, hp⟩ := multiset.exists_mem_of_ne_zero h.ne',
exact (prime_of_normalized_factor _ hp).not_unit
(is_unit_of_dvd_unit (dvd_of_mem_normalized_factors hp) hx) },
{ intros h,
obtain ⟨p, hp⟩ := exists_mem_normalized_factors hx h,
exact bot_lt_iff_ne_bot.mpr (mt multiset.eq_zero_iff_forall_not_mem.mp
(not_forall.mpr ⟨p, not_not.mpr hp⟩)) },
end
lemma dvd_not_unit_iff_normalized_factors_lt_normalized_factors
{x y : α} (hx : x ≠ 0) (hy : y ≠ 0) :
dvd_not_unit x y ↔ normalized_factors x < normalized_factors y :=
begin
split,
{ rintro ⟨_, c, hc, rfl⟩,
simp only [hx, right_ne_zero_of_mul hy, normalized_factors_mul, ne.def, not_false_iff,
lt_add_iff_pos_right, normalized_factors_pos, hc] },
{ intro h,
exact dvd_not_unit_of_dvd_of_not_dvd
((dvd_iff_normalized_factors_le_normalized_factors hx hy).mpr h.le)
(mt (dvd_iff_normalized_factors_le_normalized_factors hy hx).mp h.not_le) }
end
end unique_factorization_monoid
namespace unique_factorization_monoid
open_locale classical
open multiset associates
noncomputable theory
variables [cancel_comm_monoid_with_zero α] [nontrivial α] [unique_factorization_monoid α]
/-- Noncomputably defines a `normalization_monoid` structure on a `unique_factorization_monoid`. -/
protected def normalization_monoid : normalization_monoid α :=
normalization_monoid_of_monoid_hom_right_inverse
{ to_fun := λ a : associates α, if a = 0 then 0 else ((normalized_factors a).map
(classical.some mk_surjective.has_right_inverse : associates α → α)).prod,
map_one' := by simp,
map_mul' := λ x y, by
{ by_cases hx : x = 0, { simp [hx] },
by_cases hy : y = 0, { simp [hy] },
simp [hx, hy] } } begin
intro x,
dsimp,
by_cases hx : x = 0, { simp [hx] },
have h : associates.mk_monoid_hom ∘ (classical.some mk_surjective.has_right_inverse) =
(id : associates α → associates α),
{ ext x,
rw [function.comp_apply, mk_monoid_hom_apply,
classical.some_spec mk_surjective.has_right_inverse x],
refl },
rw [if_neg hx, ← mk_monoid_hom_apply, monoid_hom.map_multiset_prod, map_map, h, map_id,
← associated_iff_eq],
apply normalized_factors_prod hx
end
instance : inhabited (normalization_monoid α) := ⟨unique_factorization_monoid.normalization_monoid⟩
end unique_factorization_monoid
namespace unique_factorization_monoid
variables {R : Type*} [cancel_comm_monoid_with_zero R] [unique_factorization_monoid R]
lemma no_factors_of_no_prime_factors {a b : R} (ha : a ≠ 0)
(h : (∀ {d}, d ∣ a → d ∣ b → ¬ prime d)) : ∀ {d}, d ∣ a → d ∣ b → is_unit d :=
λ d, induction_on_prime d
(by { simp only [zero_dvd_iff], intros, contradiction })
(λ x hx _ _, hx)
(λ d q hp hq ih dvd_a dvd_b,
absurd hq (h (dvd_of_mul_right_dvd dvd_a) (dvd_of_mul_right_dvd dvd_b)))
/-- Euclid's lemma: if `a ∣ b * c` and `a` and `c` have no common prime factors, `a ∣ b`.
Compare `is_coprime.dvd_of_dvd_mul_left`. -/
lemma dvd_of_dvd_mul_left_of_no_prime_factors {a b c : R} (ha : a ≠ 0) :
(∀ {d}, d ∣ a → d ∣ c → ¬ prime d) → a ∣ b * c → a ∣ b :=
begin
refine induction_on_prime c _ _ _,
{ intro no_factors,
simp only [dvd_zero, mul_zero, forall_prop_of_true],
haveI := classical.prop_decidable,
exact is_unit_iff_forall_dvd.mp
(no_factors_of_no_prime_factors ha @no_factors (dvd_refl a) (dvd_zero a)) _ },
{ rintros _ ⟨x, rfl⟩ _ a_dvd_bx,
apply units.dvd_mul_right.mp a_dvd_bx },
{ intros c p hc hp ih no_factors a_dvd_bpc,
apply ih (λ q dvd_a dvd_c hq, no_factors dvd_a (dvd_c.mul_left _) hq),
rw mul_left_comm at a_dvd_bpc,
refine or.resolve_left (hp.left_dvd_or_dvd_right_of_dvd_mul a_dvd_bpc) (λ h, _),
exact no_factors h (dvd_mul_right p c) hp }
end
/-- Euclid's lemma: if `a ∣ b * c` and `a` and `b` have no common prime factors, `a ∣ c`.
Compare `is_coprime.dvd_of_dvd_mul_right`. -/
lemma dvd_of_dvd_mul_right_of_no_prime_factors {a b c : R} (ha : a ≠ 0)
(no_factors : ∀ {d}, d ∣ a → d ∣ b → ¬ prime d) : a ∣ b * c → a ∣ c :=
by simpa [mul_comm b c] using dvd_of_dvd_mul_left_of_no_prime_factors ha @no_factors
/-- If `a ≠ 0, b` are elements of a unique factorization domain, then dividing
out their common factor `c'` gives `a'` and `b'` with no factors in common. -/
lemma exists_reduced_factors : ∀ (a ≠ (0 : R)) b,
∃ a' b' c', (∀ {d}, d ∣ a' → d ∣ b' → is_unit d) ∧ c' * a' = a ∧ c' * b' = b :=
begin
haveI := classical.prop_decidable,
intros a,
refine induction_on_prime a _ _ _,
{ intros, contradiction },
{ intros a a_unit a_ne_zero b,
use [a, b, 1],
split,
{ intros p p_dvd_a _,
exact is_unit_of_dvd_unit p_dvd_a a_unit },
{ simp } },
{ intros a p a_ne_zero p_prime ih_a pa_ne_zero b,
by_cases p ∣ b,
{ rcases h with ⟨b, rfl⟩,
obtain ⟨a', b', c', no_factor, ha', hb'⟩ := ih_a a_ne_zero b,
refine ⟨a', b', p * c', @no_factor, _, _⟩,
{ rw [mul_assoc, ha'] },
{ rw [mul_assoc, hb'] } },
{ obtain ⟨a', b', c', coprime, rfl, rfl⟩ := ih_a a_ne_zero b,
refine ⟨p * a', b', c', _, mul_left_comm _ _ _, rfl⟩,
intros q q_dvd_pa' q_dvd_b',
cases p_prime.left_dvd_or_dvd_right_of_dvd_mul q_dvd_pa' with p_dvd_q q_dvd_a',
{ have : p ∣ c' * b' := dvd_mul_of_dvd_right (p_dvd_q.trans q_dvd_b') _,
contradiction },
exact coprime q_dvd_a' q_dvd_b' } }
end
lemma exists_reduced_factors' (a b : R) (hb : b ≠ 0) :
∃ a' b' c', (∀ {d}, d ∣ a' → d ∣ b' → is_unit d) ∧ c' * a' = a ∧ c' * b' = b :=
let ⟨b', a', c', no_factor, hb, ha⟩ := exists_reduced_factors b hb a
in ⟨a', b', c', λ _ hpb hpa, no_factor hpa hpb, ha, hb⟩
lemma pow_right_injective {a : R} (ha0 : a ≠ 0) (ha1 : ¬ is_unit a) :
function.injective ((^) a : ℕ → R) :=
begin
letI := classical.dec_eq R,
intros i j hij,
letI : nontrivial R := ⟨⟨a, 0, ha0⟩⟩,
letI : normalization_monoid R := unique_factorization_monoid.normalization_monoid,
obtain ⟨p', hp', dvd'⟩ := wf_dvd_monoid.exists_irreducible_factor ha1 ha0,
obtain ⟨p, mem, _⟩ := exists_mem_normalized_factors_of_dvd ha0 hp' dvd',
have := congr_arg (λ x, multiset.count p (normalized_factors x)) hij,
simp only [normalized_factors_pow, multiset.count_nsmul] at this,
exact mul_right_cancel₀ (multiset.count_ne_zero.mpr mem) this
end
lemma pow_eq_pow_iff {a : R} (ha0 : a ≠ 0) (ha1 : ¬ is_unit a) {i j : ℕ} :
a ^ i = a ^ j ↔ i = j :=
(pow_right_injective ha0 ha1).eq_iff
section multiplicity
variables [nontrivial R] [normalization_monoid R]
variables [dec_dvd : decidable_rel (has_dvd.dvd : R → R → Prop)]
open multiplicity multiset
include dec_dvd
lemma le_multiplicity_iff_replicate_le_normalized_factors [decidable_eq R] {a b : R} {n : ℕ}
(ha : irreducible a) (hb : b ≠ 0) :
↑n ≤ multiplicity a b ↔ replicate n (normalize a) ≤ normalized_factors b :=
begin
rw ← pow_dvd_iff_le_multiplicity,
revert b,
induction n with n ih, { simp },
intros b hb,
split,
{ rintro ⟨c, rfl⟩,
rw [ne.def, pow_succ, mul_assoc, mul_eq_zero, decidable.not_or_iff_and_not] at hb,
rw [pow_succ, mul_assoc, normalized_factors_mul hb.1 hb.2, replicate_succ,
normalized_factors_irreducible ha, singleton_add, cons_le_cons_iff, ← ih hb.2],
apply dvd.intro _ rfl },
{ rw [multiset.le_iff_exists_add],
rintro ⟨u, hu⟩,
rw [← (normalized_factors_prod hb).dvd_iff_dvd_right, hu, prod_add, prod_replicate],
exact (associated.pow_pow $ associated_normalize a).dvd.trans (dvd.intro u.prod rfl) }
end
/-- The multiplicity of an irreducible factor of a nonzero element is exactly the number of times
the normalized factor occurs in the `normalized_factors`.
See also `count_normalized_factors_eq` which expands the definition of `multiplicity`
to produce a specification for `count (normalized_factors _) _`..
-/
lemma multiplicity_eq_count_normalized_factors [decidable_eq R] {a b : R} (ha : irreducible a)
(hb : b ≠ 0) : multiplicity a b = (normalized_factors b).count (normalize a) :=
begin
apply le_antisymm,
{ apply part_enat.le_of_lt_add_one,
rw [← nat.cast_one, ← nat.cast_add, lt_iff_not_ge, ge_iff_le,
le_multiplicity_iff_replicate_le_normalized_factors ha hb, ← le_count_iff_replicate_le],
simp },
rw [le_multiplicity_iff_replicate_le_normalized_factors ha hb, ← le_count_iff_replicate_le],
end
omit dec_dvd
/-- The number of times an irreducible factor `p` appears in `normalized_factors x` is defined by
the number of times it divides `x`.
See also `multiplicity_eq_count_normalized_factors` if `n` is given by `multiplicity p x`.
-/
lemma count_normalized_factors_eq [decidable_eq R] {p x : R} (hp : irreducible p)
(hnorm : normalize p = p) {n : ℕ} (hle : p^n ∣ x) (hlt : ¬ (p^(n+1) ∣ x)) :
(normalized_factors x).count p = n :=
begin
letI : decidable_rel ((∣) : R → R → Prop) := λ _ _, classical.prop_decidable _,
by_cases hx0 : x = 0,
{ simp [hx0] at hlt, contradiction },
rw [← part_enat.coe_inj],
convert (multiplicity_eq_count_normalized_factors hp hx0).symm,
{ exact hnorm.symm },
exact (multiplicity.eq_coe_iff.mpr ⟨hle, hlt⟩).symm
end
/-- The number of times an irreducible factor `p` appears in `normalized_factors x` is defined by
the number of times it divides `x`. This is a slightly more general version of
`unique_factorization_monoid.count_normalized_factors_eq` that allows `p = 0`.
See also `multiplicity_eq_count_normalized_factors` if `n` is given by `multiplicity p x`.
-/
lemma count_normalized_factors_eq' [decidable_eq R] {p x : R} (hp : p = 0 ∨ irreducible p)
(hnorm : normalize p = p) {n : ℕ} (hle : p^n ∣ x) (hlt : ¬ (p^(n+1) ∣ x)) :
(normalized_factors x).count p = n :=
begin
rcases hp with rfl|hp,
{ cases n,
{ exact count_eq_zero.2 (zero_not_mem_normalized_factors _) },
{ rw [zero_pow (nat.succ_pos _)] at hle hlt,
exact absurd hle hlt } },
{ exact count_normalized_factors_eq hp hnorm hle hlt }
end
lemma max_power_factor {a₀ : R} {x : R} (h : a₀ ≠ 0) (hx : irreducible x) :
∃ n : ℕ, ∃ a : R, ¬ x ∣ a ∧ a₀ = x ^ n * a :=
begin
classical,
let n := (normalized_factors a₀).count (normalize x),
obtain ⟨a, ha1, ha2⟩ := (@exists_eq_pow_mul_and_not_dvd R _ _ x a₀
(ne_top_iff_finite.mp (part_enat.ne_top_iff.mpr _))),
simp_rw [← (multiplicity_eq_count_normalized_factors hx h).symm] at ha1,
use [n, a, ha2, ha1],
use [n, (multiplicity_eq_count_normalized_factors hx h)],
end
end multiplicity
section multiplicative
variables [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α]
variables {β : Type*} [cancel_comm_monoid_with_zero β]
open_locale big_operators
lemma prime_pow_coprime_prod_of_coprime_insert [decidable_eq α] {s : finset α} (i : α → ℕ) (p : α)
(hps : p ∉ s) (is_prime : ∀ q ∈ insert p s, prime q)
(is_coprime : ∀ (q q' ∈ insert p s), q ∣ q' → q = q') :
∀ (q : α), q ∣ p ^ i p → q ∣ ∏ p' in s, p' ^ i p' → is_unit q :=
begin
have hp := is_prime _ (finset.mem_insert_self _ _),
refine λ _, no_factors_of_no_prime_factors (pow_ne_zero _ hp.ne_zero) _,
intros d hdp hdprod hd,
apply hps,
replace hdp := hd.dvd_of_dvd_pow hdp,
obtain ⟨q, q_mem', hdq⟩ := hd.exists_mem_multiset_dvd hdprod,
obtain ⟨q, q_mem, rfl⟩ := multiset.mem_map.mp q_mem',
replace hdq := hd.dvd_of_dvd_pow hdq,
have : p ∣ q := dvd_trans
(hd.irreducible.dvd_symm hp.irreducible hdp)
hdq,
convert q_mem,
exact is_coprime _ (finset.mem_insert_self p s) _ (finset.mem_insert_of_mem q_mem) this,
end
/-- If `P` holds for units and powers of primes,
and `P x ∧ P y` for coprime `x, y` implies `P (x * y)`,
then `P` holds on a product of powers of distinct primes. -/
@[elab_as_eliminator]
theorem induction_on_prime_power
{P : α → Prop} (s : finset α) (i : α → ℕ)
(is_prime : ∀ p ∈ s, prime p) (is_coprime : ∀ p q ∈ s, p ∣ q → p = q)
(h1 : ∀ {x}, is_unit x → P x) (hpr : ∀ {p} (i : ℕ), prime p → P (p ^ i))
(hcp : ∀ {x y}, (∀ p, p ∣ x → p ∣ y → is_unit p) → P x → P y → P (x * y)) :
P (∏ p in s, p ^ (i p)) :=
begin
letI := classical.dec_eq α,
induction s using finset.induction_on with p f' hpf' ih,
{ simpa using h1 is_unit_one },
rw finset.prod_insert hpf',
exact hcp
(prime_pow_coprime_prod_of_coprime_insert i p hpf' is_prime is_coprime)
(hpr (i p) (is_prime _ (finset.mem_insert_self _ _)))
(ih (λ q hq, is_prime _ (finset.mem_insert_of_mem hq))
(λ q hq q' hq', is_coprime _ (finset.mem_insert_of_mem hq) _ (finset.mem_insert_of_mem hq')))
end
/-- If `P` holds for `0`, units and powers of primes,
and `P x ∧ P y` for coprime `x, y` implies `P (x * y)`,
then `P` holds on all `a : α`. -/
@[elab_as_eliminator]
theorem induction_on_coprime
{P : α → Prop} (a : α) (h0 : P 0) (h1 : ∀ {x}, is_unit x → P x)
(hpr : ∀ {p} (i : ℕ), prime p → P (p ^ i))
(hcp : ∀ {x y}, (∀ p, p ∣ x → p ∣ y → is_unit p) → P x → P y → P (x * y)) :
P a :=
begin
letI := classical.dec_eq α,
have P_of_associated : ∀ {x y}, associated x y → P x → P y,
{ rintros x y ⟨u, rfl⟩ hx,
exact hcp (λ p _ hpx, is_unit_of_dvd_unit hpx u.is_unit) hx (h1 u.is_unit) },
by_cases ha0 : a = 0, { rwa ha0 },
haveI : nontrivial α := ⟨⟨_, _, ha0⟩⟩,
letI : normalization_monoid α := unique_factorization_monoid.normalization_monoid,
refine P_of_associated (normalized_factors_prod ha0) _,
rw [← (normalized_factors a).map_id, finset.prod_multiset_map_count],
refine induction_on_prime_power _ _ _ _ @h1 @hpr @hcp;
simp only [multiset.mem_to_finset],
{ apply prime_of_normalized_factor },
{ apply normalized_factors_eq_of_dvd },
end
/-- If `f` maps `p ^ i` to `(f p) ^ i` for primes `p`, and `f`
is multiplicative on coprime elements, then `f` is multiplicative on all products of primes. -/
@[elab_as_eliminator]
theorem multiplicative_prime_power
{f : α → β} (s : finset α) (i j : α → ℕ)
(is_prime : ∀ p ∈ s, prime p) (is_coprime : ∀ p q ∈ s, p ∣ q → p = q)
(h1 : ∀ {x y}, is_unit y → f (x * y) = f x * f y)
(hpr : ∀ {p} (i : ℕ), prime p → f (p ^ i) = (f p) ^ i)
(hcp : ∀ {x y}, (∀ p, p ∣ x → p ∣ y → is_unit p) → f (x * y) = f x * f y) :
f (∏ p in s, p ^ (i p + j p)) = f (∏ p in s, p ^ i p) * f (∏ p in s, p ^ j p) :=
begin
letI := classical.dec_eq α,
induction s using finset.induction_on with p s hps ih,
{ simpa using h1 is_unit_one },
have hpr_p := is_prime _ (finset.mem_insert_self _ _),
have hpr_s : ∀ p ∈ s, prime p := λ p hp, is_prime _ (finset.mem_insert_of_mem hp),
have hcp_p := λ i, (prime_pow_coprime_prod_of_coprime_insert i p hps is_prime is_coprime),
have hcp_s : ∀ (p q ∈ s), p ∣ q → p = q := λ p hp q hq, is_coprime p
(finset.mem_insert_of_mem hp) q
(finset.mem_insert_of_mem hq),
rw [finset.prod_insert hps, finset.prod_insert hps, finset.prod_insert hps,
hcp (hcp_p _), hpr _ hpr_p, hcp (hcp_p _), hpr _ hpr_p, hcp (hcp_p _), hpr _ hpr_p,
ih hpr_s hcp_s,
pow_add, mul_assoc, mul_left_comm (f p ^ j p), mul_assoc],
end
/-- If `f` maps `p ^ i` to `(f p) ^ i` for primes `p`, and `f`
is multiplicative on coprime elements, then `f` is multiplicative everywhere. -/
theorem multiplicative_of_coprime
(f : α → β) (a b : α) (h0 : f 0 = 0) (h1 : ∀ {x y}, is_unit y → f (x * y) = f x * f y)
(hpr : ∀ {p} (i : ℕ), prime p → f (p ^ i) = (f p) ^ i)
(hcp : ∀ {x y}, (∀ p, p ∣ x → p ∣ y → is_unit p) → f (x * y) = f x * f y) :
f (a * b) = f a * f b :=
begin
letI := classical.dec_eq α,
by_cases ha0 : a = 0, { rw [ha0, zero_mul, h0, zero_mul] },
by_cases hb0 : b = 0, { rw [hb0, mul_zero, h0, mul_zero] },
by_cases hf1 : f 1 = 0,
{ calc f (a * b) = f ((a * b) * 1) : by rw mul_one
... = 0 : by simp only [h1 is_unit_one, hf1, mul_zero]
... = f a * f (b * 1) : by simp only [h1 is_unit_one, hf1, mul_zero]
... = f a * f b : by rw mul_one },
have h1' : f 1 = 1 := (mul_left_inj' hf1).mp (by rw [← h1 is_unit_one, one_mul, one_mul]),
haveI : nontrivial α := ⟨⟨_, _, ha0⟩⟩,
letI : normalization_monoid α := unique_factorization_monoid.normalization_monoid,
suffices : f (∏ p in (normalized_factors a).to_finset ∪ (normalized_factors b).to_finset,
p ^ ((normalized_factors a).count p + (normalized_factors b).count p))
= f (∏ p in (normalized_factors a).to_finset ∪ (normalized_factors b).to_finset,
p ^ (normalized_factors a).count p) *
f (∏ (p : α) in (normalized_factors a).to_finset ∪ (normalized_factors b).to_finset,
p ^ (normalized_factors b).count p),
{ obtain ⟨ua, a_eq⟩ := normalized_factors_prod ha0,
obtain ⟨ub, b_eq⟩ := normalized_factors_prod hb0,
rw [← a_eq, ← b_eq, mul_right_comm _ ↑ua, h1 ua.is_unit, h1 ub.is_unit, h1 ua.is_unit,
← mul_assoc, h1 ub.is_unit, mul_right_comm _ (f ua), ← mul_assoc],
congr,
rw [← (normalized_factors a).map_id, ← (normalized_factors b).map_id,
finset.prod_multiset_map_count, finset.prod_multiset_map_count,
finset.prod_subset (finset.subset_union_left _ (normalized_factors b).to_finset),
finset.prod_subset (finset.subset_union_right _ (normalized_factors b).to_finset),
← finset.prod_mul_distrib],
simp_rw [id.def, ← pow_add, this],
all_goals { simp only [multiset.mem_to_finset] },
{ intros p hpab hpb, simp [hpb] },
{ intros p hpab hpa, simp [hpa] } },
refine multiplicative_prime_power _ _ _ _ _ @h1 @hpr @hcp,
all_goals { simp only [multiset.mem_to_finset, finset.mem_union] },
{ rintros p (hpa | hpb); apply prime_of_normalized_factor; assumption },
{ rintro p (hp | hp) q (hq | hq) hdvd;
rw [← normalize_normalized_factor _ hp, ← normalize_normalized_factor _ hq];
exact normalize_eq_normalize hdvd
((prime_of_normalized_factor _ hp).irreducible.dvd_symm
(prime_of_normalized_factor _ hq).irreducible
hdvd) },
end
end multiplicative
end unique_factorization_monoid
namespace associates
open unique_factorization_monoid associated multiset
variables [cancel_comm_monoid_with_zero α]
/-- `factor_set α` representation elements of unique factorization domain as multisets.
`multiset α` produced by `normalized_factors` are only unique up to associated elements, while the
multisets in `factor_set α` are unique by equality and restricted to irreducible elements. This
gives us a representation of each element as a unique multisets (or the added ⊤ for 0), which has a
complete lattice struture. Infimum is the greatest common divisor and supremum is the least common
multiple.
-/
@[reducible] def {u} factor_set (α : Type u) [cancel_comm_monoid_with_zero α] :
Type u :=
with_top (multiset { a : associates α // irreducible a })
local attribute [instance] associated.setoid
theorem factor_set.coe_add {a b : multiset { a : associates α // irreducible a }} :
(↑(a + b) : factor_set α) = a + b :=
by norm_cast
lemma factor_set.sup_add_inf_eq_add [decidable_eq (associates α)] :
∀(a b : factor_set α), a ⊔ b + a ⊓ b = a + b
| none b := show ⊤ ⊔ b + ⊤ ⊓ b = ⊤ + b, by simp
| a none := show a ⊔ ⊤ + a ⊓ ⊤ = a + ⊤, by simp
| (some a) (some b) := show (a : factor_set α) ⊔ b + a ⊓ b = a + b, from
begin
rw [← with_top.coe_sup, ← with_top.coe_inf, ← with_top.coe_add, ← with_top.coe_add,
with_top.coe_eq_coe],
exact multiset.union_add_inter _ _
end
/-- Evaluates the product of a `factor_set` to be the product of the corresponding multiset,
or `0` if there is none. -/
def factor_set.prod : factor_set α → associates α
| none := 0
| (some s) := (s.map coe).prod
@[simp] theorem prod_top : (⊤ : factor_set α).prod = 0 := rfl
@[simp] theorem prod_coe {s : multiset { a : associates α // irreducible a }} :
(s : factor_set α).prod = (s.map coe).prod :=
rfl
@[simp] theorem prod_add : ∀(a b : factor_set α), (a + b).prod = a.prod * b.prod
| none b := show (⊤ + b).prod = (⊤:factor_set α).prod * b.prod, by simp
| a none := show (a + ⊤).prod = a.prod * (⊤:factor_set α).prod, by simp
| (some a) (some b) :=
show (↑a + ↑b:factor_set α).prod = (↑a:factor_set α).prod * (↑b:factor_set α).prod,
by rw [← factor_set.coe_add, prod_coe, prod_coe, prod_coe, multiset.map_add, multiset.prod_add]
theorem prod_mono : ∀{a b : factor_set α}, a ≤ b → a.prod ≤ b.prod
| none b h := have b = ⊤, from top_unique h, by rw [this, prod_top]; exact le_rfl
| a none h := show a.prod ≤ (⊤ : factor_set α).prod, by simp; exact le_top
| (some a) (some b) h := prod_le_prod $ multiset.map_le_map $ with_top.coe_le_coe.1 $ h
theorem factor_set.prod_eq_zero_iff [nontrivial α] (p : factor_set α) :
p.prod = 0 ↔ p = ⊤ :=
begin
induction p using with_top.rec_top_coe,
{ simp only [iff_self, eq_self_iff_true, associates.prod_top] },
simp only [prod_coe, with_top.coe_ne_top, iff_false, prod_eq_zero_iff, multiset.mem_map],
rintro ⟨⟨a, ha⟩, -, eq⟩,
rw [subtype.coe_mk] at eq,
exact ha.ne_zero eq,
end
/-- `bcount p s` is the multiplicity of `p` in the factor_set `s` (with bundled `p`)-/
def bcount [decidable_eq (associates α)] (p : {a : associates α // irreducible a}) :
factor_set α → ℕ
| none := 0
| (some s) := s.count p
variables [dec_irr : Π (p : associates α), decidable (irreducible p)]
include dec_irr
/-- `count p s` is the multiplicity of the irreducible `p` in the factor_set `s`.
If `p` is not irreducible, `count p s` is defined to be `0`. -/
def count [decidable_eq (associates α)] (p : associates α) :
factor_set α → ℕ :=
if hp : irreducible p then bcount ⟨p, hp⟩ else 0
@[simp] lemma count_some [decidable_eq (associates α)] {p : associates α} (hp : irreducible p)
(s : multiset _) : count p (some s) = s.count ⟨p, hp⟩:=
by { dunfold count, split_ifs, refl }
@[simp] lemma count_zero [decidable_eq (associates α)] {p : associates α} (hp : irreducible p) :
count p (0 : factor_set α) = 0 :=
by { dunfold count, split_ifs, refl }
lemma count_reducible [decidable_eq (associates α)] {p : associates α} (hp : ¬ irreducible p) :
count p = 0 := dif_neg hp
omit dec_irr
/-- membership in a factor_set (bundled version) -/
def bfactor_set_mem : {a : associates α // irreducible a} → (factor_set α) → Prop
| _ ⊤ := true
| p (some l) := p ∈ l
include dec_irr
/-- `factor_set_mem p s` is the predicate that the irreducible `p` is a member of
`s : factor_set α`.
If `p` is not irreducible, `p` is not a member of any `factor_set`. -/
def factor_set_mem (p : associates α) (s : factor_set α) : Prop :=
if hp : irreducible p then bfactor_set_mem ⟨p, hp⟩ s else false
instance : has_mem (associates α) (factor_set α) := ⟨factor_set_mem⟩
@[simp] lemma factor_set_mem_eq_mem (p : associates α) (s : factor_set α) :
factor_set_mem p s = (p ∈ s) := rfl
lemma mem_factor_set_top {p : associates α} {hp : irreducible p} :
p ∈ (⊤ : factor_set α) :=
begin
dunfold has_mem.mem, dunfold factor_set_mem, split_ifs, exact trivial
end
lemma mem_factor_set_some {p : associates α} {hp : irreducible p}
{l : multiset {a : associates α // irreducible a }} :
p ∈ (l : factor_set α) ↔ subtype.mk p hp ∈ l :=
begin
dunfold has_mem.mem, dunfold factor_set_mem, split_ifs, refl
end
lemma reducible_not_mem_factor_set {p : associates α} (hp : ¬ irreducible p)
(s : factor_set α) : ¬ p ∈ s :=
λ (h : if hp : irreducible p then bfactor_set_mem ⟨p, hp⟩ s else false),
by rwa [dif_neg hp] at h
omit dec_irr
variable [unique_factorization_monoid α]
theorem unique' {p q : multiset (associates α)} :
(∀a∈p, irreducible a) → (∀a∈q, irreducible a) → p.prod = q.prod → p = q :=
begin
apply multiset.induction_on_multiset_quot p,
apply multiset.induction_on_multiset_quot q,
assume s t hs ht eq,
refine multiset.map_mk_eq_map_mk_of_rel (unique_factorization_monoid.factors_unique _ _ _),
{ exact assume a ha, ((irreducible_mk _).1 $ hs _ $ multiset.mem_map_of_mem _ ha) },
{ exact assume a ha, ((irreducible_mk _).1 $ ht _ $ multiset.mem_map_of_mem _ ha) },
simpa [quot_mk_eq_mk, prod_mk, mk_eq_mk_iff_associated] using eq
end
theorem factor_set.unique [nontrivial α] {p q : factor_set α} (h : p.prod = q.prod) : p = q :=
begin
induction p using with_top.rec_top_coe;
induction q using with_top.rec_top_coe,
{ refl },
{ rw [eq_comm, ←factor_set.prod_eq_zero_iff, ←h, associates.prod_top] },
{ rw [←factor_set.prod_eq_zero_iff, h, associates.prod_top] },
{ congr' 1,
rw ←multiset.map_eq_map subtype.coe_injective,
apply unique' _ _ h;
{ intros a ha,
obtain ⟨⟨a', irred⟩, -, rfl⟩ := multiset.mem_map.mp ha,
rwa [subtype.coe_mk] } },
end
theorem prod_le_prod_iff_le [nontrivial α] {p q : multiset (associates α)}
(hp : ∀a∈p, irreducible a) (hq : ∀a∈q, irreducible a) :
p.prod ≤ q.prod ↔ p ≤ q :=
iff.intro
begin
classical,
rintros ⟨c, eqc⟩,
refine multiset.le_iff_exists_add.2 ⟨factors c, unique' hq (λ x hx, _) _⟩,
{ obtain h|h := multiset.mem_add.1 hx,
{ exact hp x h },
{ exact irreducible_of_factor _ h } },
{ rw [eqc, multiset.prod_add],
congr,
refine associated_iff_eq.mp (factors_prod (λ hc, _)).symm,
refine not_irreducible_zero (hq _ _),
rw [←prod_eq_zero_iff, eqc, hc, mul_zero] }
end
prod_le_prod
variables [dec : decidable_eq α] [dec' : decidable_eq (associates α)]
include dec
/-- This returns the multiset of irreducible factors as a `factor_set`,
a multiset of irreducible associates `with_top`. -/
noncomputable def factors' (a : α) :
multiset { a : associates α // irreducible a } :=
(factors a).pmap (λa ha, ⟨associates.mk a, (irreducible_mk _).2 ha⟩)
(irreducible_of_factor)
@[simp] theorem map_subtype_coe_factors' {a : α} :
(factors' a).map coe = (factors a).map associates.mk :=
by simp [factors', multiset.map_pmap, multiset.pmap_eq_map]
theorem factors'_cong {a b : α} (h : a ~ᵤ b) :
factors' a = factors' b :=
begin
obtain rfl|hb := eq_or_ne b 0,
{ rw associated_zero_iff_eq_zero at h, rw h },
have ha : a ≠ 0,
{ contrapose! hb with ha,
rw [←associated_zero_iff_eq_zero, ←ha],
exact h.symm },
rw [←multiset.map_eq_map subtype.coe_injective, map_subtype_coe_factors',
map_subtype_coe_factors', ←rel_associated_iff_map_eq_map],
exact factors_unique irreducible_of_factor irreducible_of_factor
((factors_prod ha).trans $ h.trans $ (factors_prod hb).symm),
end
include dec'
/-- This returns the multiset of irreducible factors of an associate as a `factor_set`,
a multiset of irreducible associates `with_top`. -/
noncomputable def factors (a : associates α) :
factor_set α :=
begin
refine (if h : a = 0 then ⊤ else
quotient.hrec_on a (λx h, some $ factors' x) _ h),
assume a b hab,
apply function.hfunext,
{ have : a ~ᵤ 0 ↔ b ~ᵤ 0, from
iff.intro (assume ha0, hab.symm.trans ha0) (assume hb0, hab.trans hb0),
simp only [associated_zero_iff_eq_zero] at this,
simp only [quotient_mk_eq_mk, this, mk_eq_zero] },
exact (assume ha hb eq, heq_of_eq $ congr_arg some $ factors'_cong hab)
end
@[simp] theorem factors_0 : (0 : associates α).factors = ⊤ :=
dif_pos rfl
@[simp] theorem factors_mk (a : α) (h : a ≠ 0) :
(associates.mk a).factors = factors' a :=
by { classical, apply dif_neg, apply (mt mk_eq_zero.1 h) }
@[simp]
theorem factors_prod (a : associates α) : a.factors.prod = a :=
quotient.induction_on a $ assume a, decidable.by_cases
(assume : associates.mk a = 0, by simp [quotient_mk_eq_mk, this])
(assume : associates.mk a ≠ 0,
have a ≠ 0, by simp * at *,
by simp [this, quotient_mk_eq_mk, prod_mk,
mk_eq_mk_iff_associated.2 (factors_prod this)])
theorem prod_factors [nontrivial α] (s : factor_set α) : s.prod.factors = s :=
factor_set.unique $ factors_prod _
@[nontriviality] lemma factors_subsingleton [subsingleton α] {a : associates α} :
a.factors = option.none :=
by { convert factors_0; apply_instance }
lemma factors_eq_none_iff_zero {a : associates α} :
a.factors = option.none ↔ a = 0 :=
begin
nontriviality α,
exact ⟨λ h, by rwa [← factors_prod a, factor_set.prod_eq_zero_iff], λ h, h.symm ▸ factors_0⟩
end
lemma factors_eq_some_iff_ne_zero {a : associates α} :
(∃ (s : multiset {p : associates α // irreducible p}), a.factors = some s) ↔ a ≠ 0 :=
by rw [← option.is_some_iff_exists, ← option.ne_none_iff_is_some, ne.def, ne.def,
factors_eq_none_iff_zero]
theorem eq_of_factors_eq_factors {a b : associates α} (h : a.factors = b.factors) : a = b :=
have a.factors.prod = b.factors.prod, by rw h,
by rwa [factors_prod, factors_prod] at this
omit dec dec'
theorem eq_of_prod_eq_prod [nontrivial α] {a b : factor_set α} (h : a.prod = b.prod) : a = b :=
begin
classical,
have : a.prod.factors = b.prod.factors, by rw h,
rwa [prod_factors, prod_factors] at this
end
include dec dec' dec_irr
theorem eq_factors_of_eq_counts {a b : associates α} (ha : a ≠ 0) (hb : b ≠ 0)
(h : ∀ (p : associates α) (hp : irreducible p), p.count a.factors = p.count b.factors) :
a.factors = b.factors :=
begin
obtain ⟨sa, h_sa⟩ := factors_eq_some_iff_ne_zero.mpr ha,
obtain ⟨sb, h_sb⟩ := factors_eq_some_iff_ne_zero.mpr hb,
rw [h_sa, h_sb] at h ⊢,
rw option.some_inj,
have h_count : ∀ (p : associates α) (hp : irreducible p), sa.count ⟨p, hp⟩ = sb.count ⟨p, hp⟩,
{ intros p hp, rw [← count_some, ← count_some, h p hp] },
apply multiset.to_finsupp.injective,
ext ⟨p, hp⟩,
rw [multiset.to_finsupp_apply, multiset.to_finsupp_apply, h_count p hp]
end
theorem eq_of_eq_counts {a b : associates α} (ha : a ≠ 0) (hb : b ≠ 0)
(h : ∀ (p : associates α), irreducible p → p.count a.factors = p.count b.factors) : a = b :=
eq_of_factors_eq_factors (eq_factors_of_eq_counts ha hb h)
lemma count_le_count_of_factors_le {a b p : associates α} (hb : b ≠ 0)
(hp : irreducible p) (h : a.factors ≤ b.factors) : p.count a.factors ≤ p.count b.factors :=
begin
by_cases ha : a = 0,
{ simp [*] at *, },
obtain ⟨sa, h_sa⟩ := factors_eq_some_iff_ne_zero.mpr ha,
obtain ⟨sb, h_sb⟩ := factors_eq_some_iff_ne_zero.mpr hb,
rw [h_sa, h_sb] at h ⊢,
rw [count_some hp, count_some hp], rw with_top.some_le_some at h,
exact multiset.count_le_of_le _ h
end
omit dec_irr
@[simp] theorem factors_mul (a b : associates α) :
(a * b).factors = a.factors + b.factors :=
begin
casesI subsingleton_or_nontrivial α,
{ simp [subsingleton.elim a 0], },
refine (eq_of_prod_eq_prod (eq_of_factors_eq_factors _)),
rw [prod_add, factors_prod, factors_prod, factors_prod],
end
theorem factors_mono : ∀{a b : associates α}, a ≤ b → a.factors ≤ b.factors
| s t ⟨d, rfl⟩ := by rw [factors_mul] ; exact le_add_of_nonneg_right bot_le
theorem factors_le {a b : associates α} : a.factors ≤ b.factors ↔ a ≤ b :=
iff.intro
(assume h, have a.factors.prod ≤ b.factors.prod, from prod_mono h,
by rwa [factors_prod, factors_prod] at this)
factors_mono
include dec_irr
lemma count_le_count_of_le {a b p : associates α} (hb : b ≠ 0)
(hp : irreducible p) (h : a ≤ b) : p.count a.factors ≤ p.count b.factors :=
count_le_count_of_factors_le hb hp $ factors_mono h
omit dec dec' dec_irr
theorem prod_le [nontrivial α] {a b : factor_set α} : a.prod ≤ b.prod ↔ a ≤ b :=
begin
classical,
exact iff.intro
(assume h, have a.prod.factors ≤ b.prod.factors, from factors_mono h,
by rwa [prod_factors, prod_factors] at this)
prod_mono
end
include dec dec'
noncomputable instance : has_sup (associates α) := ⟨λa b, (a.factors ⊔ b.factors).prod⟩
noncomputable instance : has_inf (associates α) := ⟨λa b, (a.factors ⊓ b.factors).prod⟩
noncomputable instance : lattice (associates α) :=
{ sup := (⊔),
inf := (⊓),
sup_le :=
assume a b c hac hbc, factors_prod c ▸ prod_mono (sup_le (factors_mono hac) (factors_mono hbc)),
le_sup_left := assume a b,
le_trans (le_of_eq (factors_prod a).symm) $ prod_mono $ le_sup_left,
le_sup_right := assume a b,
le_trans (le_of_eq (factors_prod b).symm) $ prod_mono $ le_sup_right,
le_inf :=
assume a b c hac hbc, factors_prod a ▸ prod_mono (le_inf (factors_mono hac) (factors_mono hbc)),
inf_le_left := assume a b,
le_trans (prod_mono inf_le_left) (le_of_eq (factors_prod a)),
inf_le_right := assume a b,
le_trans (prod_mono inf_le_right) (le_of_eq (factors_prod b)),
.. associates.partial_order }
lemma sup_mul_inf (a b : associates α) : (a ⊔ b) * (a ⊓ b) = a * b :=
show (a.factors ⊔ b.factors).prod * (a.factors ⊓ b.factors).prod = a * b,
begin
nontriviality α,
refine eq_of_factors_eq_factors _,
rw [← prod_add, prod_factors, factors_mul, factor_set.sup_add_inf_eq_add]
end
include dec_irr
lemma dvd_of_mem_factors {a p : associates α} {hp : irreducible p}
(hm : p ∈ factors a) : p ∣ a :=
begin
by_cases ha0 : a = 0, { rw ha0, exact dvd_zero p },
obtain ⟨a0, nza, ha'⟩ := exists_non_zero_rep ha0,
rw [← associates.factors_prod a],
rw [← ha', factors_mk a0 nza] at hm ⊢,
erw prod_coe,
apply multiset.dvd_prod, apply multiset.mem_map.mpr,
exact ⟨⟨p, hp⟩, mem_factor_set_some.mp hm, rfl⟩
end
omit dec'
lemma dvd_of_mem_factors' {a : α} {p : associates α} {hp : irreducible p} {hz : a ≠ 0}
(h_mem : subtype.mk p hp ∈ factors' a) : p ∣ associates.mk a :=
by { haveI := classical.dec_eq (associates α),
apply @dvd_of_mem_factors _ _ _ _ _ _ _ _ hp,
rw factors_mk _ hz,
apply mem_factor_set_some.2 h_mem }
omit dec_irr
lemma mem_factors'_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) (hd : p ∣ a) :
subtype.mk (associates.mk p) ((irreducible_mk _).2 hp) ∈ factors' a :=
begin
obtain ⟨q, hq, hpq⟩ := exists_mem_factors_of_dvd ha0 hp hd,
apply multiset.mem_pmap.mpr, use q, use hq,
exact subtype.eq (eq.symm (mk_eq_mk_iff_associated.mpr hpq))
end
include dec_irr
lemma mem_factors'_iff_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) :
subtype.mk (associates.mk p) ((irreducible_mk _).2 hp) ∈ factors' a ↔ p ∣ a :=
begin
split,
{ rw ← mk_dvd_mk, apply dvd_of_mem_factors', apply ha0 },
{ apply mem_factors'_of_dvd ha0 }
end
include dec'
lemma mem_factors_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) (hd : p ∣ a) :
(associates.mk p) ∈ factors (associates.mk a) :=
begin
rw factors_mk _ ha0, exact mem_factor_set_some.mpr (mem_factors'_of_dvd ha0 hp hd)
end
lemma mem_factors_iff_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) :
(associates.mk p) ∈ factors (associates.mk a) ↔ p ∣ a :=
begin
split,
{ rw ← mk_dvd_mk, apply dvd_of_mem_factors, exact (irreducible_mk p).mpr hp },
{ apply mem_factors_of_dvd ha0 hp }
end
lemma exists_prime_dvd_of_not_inf_one {a b : α}
(ha : a ≠ 0) (hb : b ≠ 0) (h : (associates.mk a) ⊓ (associates.mk b) ≠ 1) :
∃ (p : α), prime p ∧ p ∣ a ∧ p ∣ b :=
begin
have hz : (factors (associates.mk a)) ⊓ (factors (associates.mk b)) ≠ 0,
{ contrapose! h with hf,
change ((factors (associates.mk a)) ⊓ (factors (associates.mk b))).prod = 1,
rw hf,
exact multiset.prod_zero },
rw [factors_mk a ha, factors_mk b hb, ← with_top.coe_inf] at hz,
obtain ⟨⟨p0, p0_irr⟩, p0_mem⟩ := multiset.exists_mem_of_ne_zero ((mt with_top.coe_eq_coe.mpr) hz),
rw multiset.inf_eq_inter at p0_mem,
obtain ⟨p, rfl⟩ : ∃ p, associates.mk p = p0 := quot.exists_rep p0,
refine ⟨p, _, _, _⟩,
{ rw [← irreducible_iff_prime, ← irreducible_mk],
exact p0_irr },
{ apply dvd_of_mk_le_mk,
apply dvd_of_mem_factors' (multiset.mem_inter.mp p0_mem).left,
apply ha, },
{ apply dvd_of_mk_le_mk,
apply dvd_of_mem_factors' (multiset.mem_inter.mp p0_mem).right,
apply hb }
end
theorem coprime_iff_inf_one {a b : α} (ha0 : a ≠ 0) (hb0 : b ≠ 0) :
(associates.mk a) ⊓ (associates.mk b) = 1 ↔ ∀ {d : α}, d ∣ a → d ∣ b → ¬ prime d :=
begin
split,
{ intros hg p ha hb hp,
refine ((associates.prime_mk _).mpr hp).not_unit (is_unit_of_dvd_one _ _),
rw ← hg,
exact le_inf (mk_le_mk_of_dvd ha) (mk_le_mk_of_dvd hb) },
{ contrapose,
intros hg hc,
obtain ⟨p, hp, hpa, hpb⟩ := exists_prime_dvd_of_not_inf_one ha0 hb0 hg,
exact hc hpa hpb hp }
end
omit dec_irr
theorem factors_self [nontrivial α] {p : associates α} (hp : irreducible p) :
p.factors = some ({⟨p, hp⟩}) :=
eq_of_prod_eq_prod (by rw [factors_prod, factor_set.prod, map_singleton, prod_singleton,
subtype.coe_mk])
theorem factors_prime_pow [nontrivial α] {p : associates α} (hp : irreducible p)
(k : ℕ) : factors (p ^ k) = some (multiset.replicate k ⟨p, hp⟩) :=
eq_of_prod_eq_prod (by rw [associates.factors_prod, factor_set.prod, multiset.map_replicate,
multiset.prod_replicate, subtype.coe_mk])
include dec_irr
theorem prime_pow_dvd_iff_le [nontrivial α] {m p : associates α} (h₁ : m ≠ 0)
(h₂ : irreducible p) {k : ℕ} : p ^ k ≤ m ↔ k ≤ count p m.factors :=
begin
obtain ⟨a, nz, rfl⟩ := associates.exists_non_zero_rep h₁,
rw [factors_mk _ nz, ← with_top.some_eq_coe, count_some, multiset.le_count_iff_replicate_le,
← factors_le, factors_prime_pow h₂, factors_mk _ nz],
exact with_top.coe_le_coe
end
theorem le_of_count_ne_zero {m p : associates α} (h0 : m ≠ 0)
(hp : irreducible p) : count p m.factors ≠ 0 → p ≤ m :=
begin
nontriviality α,
rw [← pos_iff_ne_zero],
intro h,
rw [← pow_one p],
apply (prime_pow_dvd_iff_le h0 hp).2,
simpa only
end
theorem count_ne_zero_iff_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) :
(associates.mk p).count (associates.mk a).factors ≠ 0 ↔ p ∣ a :=
begin
nontriviality α,
rw ← associates.mk_le_mk_iff_dvd_iff,
refine ⟨λ h, associates.le_of_count_ne_zero (associates.mk_ne_zero.mpr ha0)
((associates.irreducible_mk p).mpr hp) h, λ h, _⟩,
{ rw [← pow_one (associates.mk p), associates.prime_pow_dvd_iff_le
(associates.mk_ne_zero.mpr ha0) ((associates.irreducible_mk p).mpr hp)] at h,
exact (zero_lt_one.trans_le h).ne' }
end
theorem count_self [nontrivial α] {p : associates α} (hp : irreducible p) :
p.count p.factors = 1 :=
by simp [factors_self hp, associates.count_some hp]
lemma count_eq_zero_of_ne {p q : associates α} (hp : irreducible p) (hq : irreducible q)
(h : p ≠ q) : p.count q.factors = 0 :=
not_ne_iff.mp $ λ h', h $ associated_iff_eq.mp $ hp.associated_of_dvd hq $
by { nontriviality α, exact le_of_count_ne_zero hq.ne_zero hp h' }
theorem count_mul {a : associates α} (ha : a ≠ 0) {b : associates α} (hb : b ≠ 0)
{p : associates α} (hp : irreducible p) :
count p (factors (a * b)) = count p a.factors + count p b.factors :=
begin
obtain ⟨a0, nza, ha'⟩ := exists_non_zero_rep ha,
obtain ⟨b0, nzb, hb'⟩ := exists_non_zero_rep hb,
rw [factors_mul, ← ha', ← hb', factors_mk a0 nza, factors_mk b0 nzb, ← factor_set.coe_add,
← with_top.some_eq_coe, ← with_top.some_eq_coe, ← with_top.some_eq_coe, count_some hp,
multiset.count_add, count_some hp, count_some hp]
end
theorem count_of_coprime {a : associates α} (ha : a ≠ 0) {b : associates α}
(hb : b ≠ 0)
(hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) {p : associates α} (hp : irreducible p) :
count p a.factors = 0 ∨ count p b.factors = 0 :=
begin
rw [or_iff_not_imp_left, ← ne.def],
intro hca,
contrapose! hab with hcb,
exact ⟨p, le_of_count_ne_zero ha hp hca, le_of_count_ne_zero hb hp hcb,
(irreducible_iff_prime.mp hp)⟩,
end
theorem count_mul_of_coprime {a : associates α} {b : associates α}
(hb : b ≠ 0)
{p : associates α} (hp : irreducible p) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) :
count p a.factors = 0 ∨ count p a.factors = count p (a * b).factors :=
begin
by_cases ha : a = 0,
{ simp [ha], },
cases count_of_coprime ha hb hab hp with hz hb0, { tauto },
apply or.intro_right,
rw [count_mul ha hb hp, hb0, add_zero]
end
theorem count_mul_of_coprime' {a b : associates α}
{p : associates α} (hp : irreducible p) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) :
count p (a * b).factors = count p a.factors
∨ count p (a * b).factors = count p b.factors :=
begin
by_cases ha : a = 0, { simp [ha], },
by_cases hb : b = 0, { simp [hb], },
rw [count_mul ha hb hp],
cases count_of_coprime ha hb hab hp with ha0 hb0,
{ apply or.intro_right, rw [ha0, zero_add] },
{ apply or.intro_left, rw [hb0, add_zero] }
end
theorem dvd_count_of_dvd_count_mul {a b : associates α} (hb : b ≠ 0)
{p : associates α} (hp : irreducible p) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d)
{k : ℕ} (habk : k ∣ count p (a * b).factors) : k ∣ count p a.factors :=
begin
by_cases ha : a = 0, { simpa [*] using habk, },
cases count_of_coprime ha hb hab hp with hz h,
{ rw hz, exact dvd_zero k },
{ rw [count_mul ha hb hp, h] at habk, exact habk }
end
omit dec_irr
@[simp] lemma factors_one [nontrivial α] : factors (1 : associates α) = 0 :=
begin
apply eq_of_prod_eq_prod,
rw associates.factors_prod,
exact multiset.prod_zero,
end
@[simp] theorem pow_factors [nontrivial α] {a : associates α} {k : ℕ} :
(a ^ k).factors = k • a.factors :=
begin
induction k with n h,
{ rw [zero_nsmul, pow_zero], exact factors_one },
{ rw [pow_succ, succ_nsmul, factors_mul, h] }
end
include dec_irr
lemma count_pow [nontrivial α] {a : associates α} (ha : a ≠ 0) {p : associates α}
(hp : irreducible p)
(k : ℕ) : count p (a ^ k).factors = k * count p a.factors :=
begin
induction k with n h,
{ rw [pow_zero, factors_one, zero_mul, count_zero hp] },
{ rw [pow_succ, count_mul ha (pow_ne_zero _ ha) hp, h, nat.succ_eq_add_one], ring }
end
theorem dvd_count_pow [nontrivial α] {a : associates α} (ha : a ≠ 0) {p : associates α}
(hp : irreducible p)
(k : ℕ) : k ∣ count p (a ^ k).factors := by { rw count_pow ha hp, apply dvd_mul_right }
theorem is_pow_of_dvd_count [nontrivial α] {a : associates α} (ha : a ≠ 0) {k : ℕ}
(hk : ∀ (p : associates α) (hp : irreducible p), k ∣ count p a.factors) :
∃ (b : associates α), a = b ^ k :=
begin
obtain ⟨a0, hz, rfl⟩ := exists_non_zero_rep ha,
rw [factors_mk a0 hz] at hk,
have hk' : ∀ p, p ∈ (factors' a0) → k ∣ (factors' a0).count p,
{ rintros p -,
have pp : p = ⟨p.val, p.2⟩, { simp only [subtype.coe_eta, subtype.val_eq_coe] },
rw [pp, ← count_some p.2], exact hk p.val p.2 },
obtain ⟨u, hu⟩ := multiset.exists_smul_of_dvd_count _ hk',
use (u : factor_set α).prod,
apply eq_of_factors_eq_factors,
rw [pow_factors, prod_factors, factors_mk a0 hz, ← with_top.some_eq_coe, hu],
exact with_bot.coe_nsmul u k
end
/-- The only divisors of prime powers are prime powers. See `eq_pow_find_of_dvd_irreducible_pow`
for an explicit expression as a p-power (without using `count`). -/
theorem eq_pow_count_factors_of_dvd_pow {p a : associates α} (hp : irreducible p)
{n : ℕ} (h : a ∣ p ^ n) : a = p ^ p.count a.factors :=
begin
nontriviality α,
have hph := pow_ne_zero n hp.ne_zero,
have ha := ne_zero_of_dvd_ne_zero hph h,
apply eq_of_eq_counts ha (pow_ne_zero _ hp.ne_zero),
have eq_zero_of_ne : ∀ (q : associates α), irreducible q → q ≠ p → _ = 0 :=
λ q hq h', nat.eq_zero_of_le_zero $ by
{ convert count_le_count_of_le hph hq h, symmetry,
rw [count_pow hp.ne_zero hq, count_eq_zero_of_ne hq hp h', mul_zero] },
intros q hq,
rw count_pow hp.ne_zero hq,
by_cases h : q = p,
{ rw [h, count_self hp, mul_one] },
{ rw [count_eq_zero_of_ne hq hp h, mul_zero, eq_zero_of_ne q hq h] }
end
lemma count_factors_eq_find_of_dvd_pow {a p : associates α} (hp : irreducible p)
[∀ n : ℕ, decidable (a ∣ p ^ n)] {n : ℕ} (h : a ∣ p ^ n) : nat.find ⟨n, h⟩ = p.count a.factors :=
begin
apply le_antisymm,
{ refine nat.find_le ⟨1, _⟩, rw mul_one, symmetry, exact eq_pow_count_factors_of_dvd_pow hp h },
{ have hph := pow_ne_zero (nat.find ⟨n, h⟩) hp.ne_zero,
casesI (subsingleton_or_nontrivial α) with hα hα,
{ simpa using hph, },
convert count_le_count_of_le hph hp (nat.find_spec ⟨n, h⟩),
rw [count_pow hp.ne_zero hp, count_self hp, mul_one] }
end
omit dec
omit dec_irr
omit dec'
theorem eq_pow_of_mul_eq_pow [nontrivial α] {a b c : associates α} (ha : a ≠ 0) (hb : b ≠ 0)
(hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) {k : ℕ} (h : a * b = c ^ k) :
∃ (d : associates α), a = d ^ k :=
begin
classical,
by_cases hk0 : k = 0,
{ use 1,
rw [hk0, pow_zero] at h ⊢,
apply (mul_eq_one_iff.1 h).1 },
{ refine is_pow_of_dvd_count ha _,
intros p hp,
apply dvd_count_of_dvd_count_mul hb hp hab,
rw h,
apply dvd_count_pow _ hp,
rintros rfl,
rw zero_pow' _ hk0 at h,
cases mul_eq_zero.mp h; contradiction }
end
/-- The only divisors of prime powers are prime powers. -/
theorem eq_pow_find_of_dvd_irreducible_pow {a p : associates α} (hp : irreducible p)
[∀ n : ℕ, decidable (a ∣ p ^ n)] {n : ℕ} (h : a ∣ p ^ n) : a = p ^ nat.find ⟨n, h⟩ :=
by { classical, rw [count_factors_eq_find_of_dvd_pow hp, ← eq_pow_count_factors_of_dvd_pow hp h] }
end associates
section
open associates unique_factorization_monoid
lemma associates.quot_out {α : Type*} [comm_monoid α] (a : associates α):
associates.mk (quot.out (a)) = a :=
by rw [←quot_mk_eq_mk, quot.out_eq]
/-- `to_gcd_monoid` constructs a GCD monoid out of a unique factorization domain. -/
noncomputable def unique_factorization_monoid.to_gcd_monoid
(α : Type*) [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α]
[decidable_eq (associates α)] [decidable_eq α] : gcd_monoid α :=
{ gcd := λa b, quot.out (associates.mk a ⊓ associates.mk b : associates α),
lcm := λa b, quot.out (associates.mk a ⊔ associates.mk b : associates α),
gcd_dvd_left := λ a b, by
{ rw [←mk_dvd_mk, (associates.mk a ⊓ associates.mk b).quot_out, dvd_eq_le],
exact inf_le_left },
gcd_dvd_right := λ a b, by
{ rw [←mk_dvd_mk, (associates.mk a ⊓ associates.mk b).quot_out, dvd_eq_le],
exact inf_le_right },
dvd_gcd := λ a b c hac hab, by
{ rw [←mk_dvd_mk, (associates.mk c ⊓ associates.mk b).quot_out, dvd_eq_le,
le_inf_iff, mk_le_mk_iff_dvd_iff, mk_le_mk_iff_dvd_iff],
exact ⟨hac, hab⟩ },
lcm_zero_left := λ a, by
{ have : associates.mk (0 : α) = ⊤ := rfl,
rw [this, top_sup_eq, ←this, ←associated_zero_iff_eq_zero, ←mk_eq_mk_iff_associated,
←associated_iff_eq, associates.quot_out] },
lcm_zero_right := λ a, by
{ have : associates.mk (0 : α) = ⊤ := rfl,
rw [this, sup_top_eq, ←this, ←associated_zero_iff_eq_zero, ←mk_eq_mk_iff_associated,
←associated_iff_eq, associates.quot_out] },
gcd_mul_lcm := λ a b, by
{ rw [←mk_eq_mk_iff_associated, ←associates.mk_mul_mk, ←associated_iff_eq, associates.quot_out,
associates.quot_out, mul_comm, sup_mul_inf, associates.mk_mul_mk] } }
/-- `to_normalized_gcd_monoid` constructs a GCD monoid out of a normalization on a
unique factorization domain. -/
noncomputable def unique_factorization_monoid.to_normalized_gcd_monoid
(α : Type*) [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α]
[normalization_monoid α] [decidable_eq (associates α)] [decidable_eq α] :
normalized_gcd_monoid α :=
{ gcd := λa b, (associates.mk a ⊓ associates.mk b).out,
lcm := λa b, (associates.mk a ⊔ associates.mk b).out,
gcd_dvd_left := assume a b, (out_dvd_iff a (associates.mk a ⊓ associates.mk b)).2 $ inf_le_left,
gcd_dvd_right := assume a b, (out_dvd_iff b (associates.mk a ⊓ associates.mk b)).2 $ inf_le_right,
dvd_gcd := assume a b c hac hab, show a ∣ (associates.mk c ⊓ associates.mk b).out,
by rw [dvd_out_iff, le_inf_iff, mk_le_mk_iff_dvd_iff, mk_le_mk_iff_dvd_iff]; exact ⟨hac, hab⟩,
lcm_zero_left := assume a, show (⊤ ⊔ associates.mk a).out = 0, by simp,
lcm_zero_right := assume a, show (associates.mk a ⊔ ⊤).out = 0, by simp,
gcd_mul_lcm := assume a b, by
{ rw [← out_mul, mul_comm, sup_mul_inf, mk_mul_mk, out_mk],
exact normalize_associated (a * b) },
normalize_gcd := assume a b, by convert normalize_out _,
normalize_lcm := assume a b, by convert normalize_out _,
.. ‹normalization_monoid α› }
end
namespace unique_factorization_monoid
/-- If `y` is a nonzero element of a unique factorization monoid with finitely
many units (e.g. `ℤ`, `ideal (ring_of_integers K)`), it has finitely many divisors. -/
noncomputable def fintype_subtype_dvd {M : Type*} [cancel_comm_monoid_with_zero M]
[unique_factorization_monoid M] [fintype Mˣ]
(y : M) (hy : y ≠ 0) :
fintype {x // x ∣ y} :=
begin
haveI : nontrivial M := ⟨⟨y, 0, hy⟩⟩,
haveI : normalization_monoid M := unique_factorization_monoid.normalization_monoid,
haveI := classical.dec_eq M,
haveI := classical.dec_eq (associates M),
-- We'll show `λ (u : Mˣ) (f ⊆ factors y) → u * Π f` is injective
-- and has image exactly the divisors of `y`.
refine fintype.of_finset
(((normalized_factors y).powerset.to_finset.product (finset.univ : finset Mˣ)).image
(λ s, (s.snd : M) * s.fst.prod))
(λ x, _),
simp only [exists_prop, finset.mem_image, finset.mem_product, finset.mem_univ, and_true,
multiset.mem_to_finset, multiset.mem_powerset, exists_eq_right, multiset.mem_map],
split,
{ rintros ⟨s, hs, rfl⟩,
have prod_s_ne : s.fst.prod ≠ 0,
{ intro hz,
apply hy (eq_zero_of_zero_dvd _),
have hz := (@multiset.prod_eq_zero_iff M _ _ _ s.fst).mp hz,
rw ← (normalized_factors_prod hy).dvd_iff_dvd_right,
exact multiset.dvd_prod (multiset.mem_of_le hs hz) },
show (s.snd : M) * s.fst.prod ∣ y,
rw [(unit_associated_one.mul_right s.fst.prod).dvd_iff_dvd_left, one_mul,
← (normalized_factors_prod hy).dvd_iff_dvd_right],
exact multiset.prod_dvd_prod_of_le hs },
{ rintro (h : x ∣ y),
have hx : x ≠ 0, { refine mt (λ hx, _) hy, rwa [hx, zero_dvd_iff] at h },
obtain ⟨u, hu⟩ := normalized_factors_prod hx,
refine ⟨⟨normalized_factors x, u⟩, _, (mul_comm _ _).trans hu⟩,
exact (dvd_iff_normalized_factors_le_normalized_factors hx hy).mp h }
end
end unique_factorization_monoid
section finsupp
variables [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α]
variables [normalization_monoid α] [decidable_eq α]
open unique_factorization_monoid
/-- This returns the multiset of irreducible factors as a `finsupp` -/
noncomputable def factorization (n : α) : α →₀ ℕ := (normalized_factors n).to_finsupp
lemma factorization_eq_count {n p : α} :
factorization n p = multiset.count p (normalized_factors n) :=
by simp [factorization]
@[simp] lemma factorization_zero : factorization (0 : α) = 0 := by simp [factorization]
@[simp] lemma factorization_one : factorization (1 : α) = 0 := by simp [factorization]
/-- The support of `factorization n` is exactly the finset of normalized factors -/
@[simp] lemma support_factorization {n : α} :
(factorization n).support = (normalized_factors n).to_finset :=
by simp [factorization, multiset.to_finsupp_support]
/-- For nonzero `a` and `b`, the power of `p` in `a * b` is the sum of the powers in `a` and `b` -/
@[simp] lemma factorization_mul {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) :
factorization (a * b) = factorization a + factorization b :=
by simp [factorization, normalized_factors_mul ha hb]
/-- For any `p`, the power of `p` in `x^n` is `n` times the power in `x` -/
lemma factorization_pow {x : α} {n : ℕ} :
factorization (x^n) = n • factorization x :=
by { ext, simp [factorization] }
lemma associated_of_factorization_eq (a b: α) (ha: a ≠ 0) (hb: b ≠ 0)
(h: factorization a = factorization b) : associated a b :=
begin
simp_rw [factorization, add_equiv.apply_eq_iff_eq] at h,
rwa [associated_iff_normalized_factors_eq_normalized_factors ha hb],
end
end finsupp
|
6d43b29568f230ea3f5872d7ef90e7cf7f2e06ba | e5c11e5a7d990ce404047c2bd848eeafac3c0a85 | /src/power_basis.lean | 9237221f0b96374bf8000c9479d0ad06c8a56133 | [
"LPPL-1.3c"
] | permissive | lean-forward/class-number | 9ec63c24845e46efc8fa8b15324d0815918292c7 | 4fccf36d5e0e16accae84c16df77a3839ad964e4 | refs/heads/main | 1,686,927,014,542 | 1,624,886,724,000 | 1,624,886,724,000 | 327,319,245 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 22,004 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen
-/
import field_theory.minpoly
import linear_algebra.free_module
import ring_theory.adjoin
import ring_theory.adjoin_root
import ring_theory.algebraic
/-!
# Power basis
This file defines a structure `power_basis R S`, giving a basis of the
`R`-algebra `S` as a finite list of powers `1, x, ..., x^n`.
There are also constructors for `power_basis` when adjoining an algebraic
element to a ring/field.
## Definitions
* `power_basis R A`: a structure containing an `x` and an `n` such that
`1, x, ..., x^n` is a basis for the `R`-algebra `A` (viewed as an `R`-module).
* `findim (hf : f ≠ 0) : finite_dimensional.findim K (adjoin_root f) = f.nat_degree`,
the dimension of `adjoin_root f` equals the degree of `f`
* `power_basis.lift (pb : power_basis R S)`: if `y : S'` satisfies the same
equations as `pb.gen`, this is the map `S →ₐ[R] S'` sending `pb.gen` to `y`
* `power_basis.equiv`: if two power bases satisfy the same equations, they are
equivalent as algebras
## Implementation notes
Throughout this file, `R`, `S`, ... are `comm_ring`s, `A`, `B`, ... are
`integral_domain`s and `K`, `L`, ... are `field`s.
`S` is an `R`-algebra, `B` is an `A`-algebra, `L` is a `K`-algebra.
## Tags
power basis, powerbasis
-/
open polynomial
variables {R S T : Type*} [comm_ring R] [comm_ring S] [comm_ring T]
variables [algebra R S] [algebra S T] [algebra R T] [is_scalar_tower R S T]
variables {A B : Type*} [integral_domain A] [integral_domain B] [algebra A B]
variables {K L : Type*} [field K] [field L] [algebra K L]
/-- `pb : power_basis R S` states that `1, pb.gen, ..., pb.gen ^ (pb.dim - 1)`
is a basis for the `R`-algebra `S` (viewed as `R`-module).
This is a structure, not a class, since the same algebra can have many power bases.
For the common case where `S` is defined by adjoining an integral element to `R`,
the canonical power basis is given by `{algebra,intermediate_field}.adjoin.power_basis`.
-/
@[nolint has_inhabited_instance]
structure power_basis (R S : Type*) [comm_ring R] [ring S] [algebra R S] :=
(gen : S)
(dim : ℕ)
(is_basis : is_basis R (λ (i : fin dim), gen ^ (i : ℕ)))
namespace power_basis
/-- Cannot be an instance because `power_basis` cannot be a class. -/
lemma finite_dimensional [algebra K S] (pb : power_basis K S) : finite_dimensional K S :=
finite_dimensional.of_fintype_basis pb.is_basis
lemma findim [algebra K S] (pb : power_basis K S) : finite_dimensional.findim K S = pb.dim :=
by rw [finite_dimensional.findim_eq_card_basis pb.is_basis, fintype.card_fin]
/-- TODO: this mixes `polynomial` and `finsupp`, we should hide this behind a
new function `polynomial.of_finsupp`. -/
lemma polynomial.mem_supported_range {f : polynomial R} {d : ℕ} :
(f : finsupp ℕ R) ∈ finsupp.supported R R (↑(finset.range d) : set ℕ) ↔ f.degree < d :=
by { simp_rw [finsupp.mem_supported', finset.mem_coe, finset.mem_range, not_lt,
degree_lt_iff_coeff_zero],
refl }
lemma mem_span_pow' {x y : S} {d : ℕ} :
y ∈ submodule.span R (set.range (λ (i : fin d), x ^ (i : ℕ))) ↔
∃ f : polynomial R, f.degree < d ∧ y = aeval x f :=
begin
have : set.range (λ (i : fin d), x ^ (i : ℕ)) = (λ (i : ℕ), x ^ i) '' ↑(finset.range d),
{ ext n,
simp_rw [set.mem_range, set.mem_image, finset.mem_coe, finset.mem_range],
exact ⟨λ ⟨⟨i, hi⟩, hy⟩, ⟨i, hi, hy⟩, λ ⟨i, hi, hy⟩, ⟨⟨i, hi⟩, hy⟩⟩ },
rw [this, finsupp.mem_span_iff_total],
-- In the next line we use that `polynomial R := finsupp ℕ R`.
-- It would be nice to have a function `polynomial.of_finsupp`.
apply exists_congr,
rintro (f : polynomial R),
simp only [exists_prop, polynomial.mem_supported_range, eq_comm],
apply and_congr iff.rfl,
split;
{ rintro rfl;
rw [finsupp.total_apply, aeval_def, eval₂_eq_sum, eq_comm],
apply finset.sum_congr rfl,
rintro i -,
simp only [algebra.smul_def] }
end
lemma mem_span_pow {x y : S} {d : ℕ} (hd : d ≠ 0) :
y ∈ submodule.span R (set.range (λ (i : fin d), x ^ (i : ℕ))) ↔
∃ f : polynomial R, f.nat_degree < d ∧ y = aeval x f :=
begin
rw mem_span_pow',
split;
{ rintros ⟨f, h, hy⟩,
refine ⟨f, _, hy⟩,
by_cases hf : f = 0,
{ simp only [hf, nat_degree_zero, degree_zero] at h ⊢,
exact lt_of_le_of_ne (nat.zero_le d) hd.symm <|> exact with_bot.bot_lt_some d },
simpa only [degree_eq_nat_degree hf, with_bot.coe_lt_coe] using h },
end
lemma dim_ne_zero [nontrivial S] (pb : power_basis R S) : pb.dim ≠ 0 :=
λ h, one_ne_zero $
show (1 : S) = 0,
by { rw [← pb.is_basis.total_repr 1, finsupp.total_apply, finsupp.sum_fintype],
{ refine finset.sum_eq_zero (λ x hx, _),
cases x with x x_lt,
rw h at x_lt,
cases x_lt },
{ simp } }
lemma dim_pos [nontrivial S] (pb : power_basis R S) : 0 < pb.dim :=
nat.pos_of_ne_zero pb.dim_ne_zero
lemma exists_eq_aeval [nontrivial S] (pb : power_basis R S) (y : S) :
∃ f : polynomial R, f.nat_degree < pb.dim ∧ y = aeval pb.gen f :=
(mem_span_pow pb.dim_ne_zero).mp (pb.is_basis.mem_span y)
section minpoly
open_locale big_operators
variable [algebra A S]
/-- `pb.minpoly_gen` is a minimal polynomial for `pb.gen`.
If `A` is not a field, it might not necessarily be *the* minimal polynomial,
however `nat_degree_minpoly` shows its degree is indeed minimal.
-/
noncomputable def minpoly_gen (pb : power_basis A S) : polynomial A :=
X ^ pb.dim -
∑ (i : fin pb.dim), C (pb.is_basis.repr (pb.gen ^ pb.dim) i) * X ^ (i : ℕ)
@[simp]
lemma nat_degree_minpoly_gen (pb : power_basis A S) :
nat_degree (minpoly_gen pb) = pb.dim :=
begin
unfold minpoly_gen,
apply nat_degree_eq_of_degree_eq_some,
rw degree_sub_eq_left_of_degree_lt; rw degree_X_pow,
apply degree_sum_fin_lt
end
lemma minpoly_gen_monic (pb : power_basis A S) : monic (minpoly_gen pb) :=
begin
apply monic_sub_of_left (monic_pow (monic_X) _),
rw degree_X_pow,
exact degree_sum_fin_lt _
end
lemma minpoly_gen_ne_zero (pb : power_basis A S) : minpoly_gen pb ≠ 0 :=
pb.minpoly_gen_monic.ne_zero
@[simp]
lemma degree_minpoly_gen (pb : power_basis A S) : degree (minpoly_gen pb) = pb.dim :=
by rw [degree_eq_nat_degree pb.minpoly_gen_ne_zero, nat_degree_minpoly_gen, with_bot.coe_eq_coe]
lemma degree_minpoly_gen_pos [nontrivial S] (pb : power_basis A S) :
0 < degree (minpoly_gen pb) :=
by { rw [degree_minpoly_gen, ← with_bot.coe_zero, with_bot.coe_lt_coe], exact pb.dim_pos }
lemma nat_degree_minpoly_gen_pos [nontrivial S] (pb : power_basis A S) :
0 < nat_degree (minpoly_gen pb) :=
by { rw nat_degree_minpoly_gen, exact pb.dim_pos }
@[simp]
lemma aeval_minpoly_gen (pb : power_basis A S) : aeval pb.gen (minpoly_gen pb) = 0 :=
begin
simp_rw [minpoly_gen, alg_hom.map_sub, alg_hom.map_sum, alg_hom.map_mul, alg_hom.map_pow,
aeval_C, ← algebra.smul_def, aeval_X],
refine sub_eq_zero.mpr ((pb.is_basis.total_repr (pb.gen ^ pb.dim)).symm.trans _),
rw [finsupp.total_apply, finsupp.sum_fintype],
intro i, rw zero_smul
end
lemma is_integral_gen (pb : power_basis A S) : is_integral A pb.gen :=
⟨minpoly_gen pb, minpoly_gen_monic pb, aeval_minpoly_gen pb⟩
lemma dim_le_nat_degree_of_root (h : power_basis A S) {p : polynomial A}
(ne_zero : p ≠ 0) (root : aeval h.gen p = 0) :
h.dim ≤ p.nat_degree :=
begin
refine le_of_not_lt (λ hlt, ne_zero _),
let p_coeff : fin (h.dim) → A := λ i, p.coeff i,
suffices : ∀ i, p_coeff i = 0,
{ ext i,
by_cases hi : i < h.dim,
{ exact this ⟨i, hi⟩ },
exact coeff_eq_zero_of_nat_degree_lt (lt_of_lt_of_le hlt (le_of_not_gt hi)) },
intro i,
refine linear_independent_iff'.mp h.is_basis.1 finset.univ _ _ i (finset.mem_univ _),
rw aeval_eq_sum_range' hlt at root,
rw finset.sum_fin_eq_sum_range,
convert root,
ext i,
split_ifs with hi,
{ refl },
{ rw [coeff_eq_zero_of_nat_degree_lt (lt_of_lt_of_le hlt (le_of_not_gt hi)),
zero_smul] }
end
lemma dim_le_degree_of_root (h : power_basis A S) {p : polynomial A}
(ne_zero : p ≠ 0) (root : aeval h.gen p = 0) :
↑h.dim ≤ p.degree :=
begin
have := dim_le_nat_degree_of_root h ne_zero root,
rwa [← with_bot.coe_le_coe, ← degree_eq_nat_degree ne_zero] at this,
end
@[simp]
lemma degree_minpoly (pb : power_basis A S) :
(minpoly A pb.gen).degree = pb.dim :=
begin
refine le_antisymm _
(dim_le_degree_of_root pb (minpoly.ne_zero pb.is_integral_gen) (minpoly.aeval _ _)),
rw [← nat_degree_minpoly_gen, ← degree_eq_nat_degree (minpoly_gen_monic pb).ne_zero],
exact minpoly.min _ _ (minpoly_gen_monic pb) (aeval_minpoly_gen pb)
end
@[simp]
lemma nat_degree_minpoly (pb : power_basis A S) :
(minpoly A pb.gen).nat_degree = pb.dim :=
by rw [← with_bot.coe_eq_coe, ← degree_minpoly pb,
degree_eq_nat_degree (minpoly.ne_zero pb.is_integral_gen)]
lemma minpoly_gen_eq [algebra K S] (pb : power_basis K S) :
pb.minpoly_gen = minpoly K pb.gen :=
begin
apply minpoly.unique _ _ pb.minpoly_gen_monic pb.aeval_minpoly_gen,
intros q q_monic aeval_q,
rw [degree_minpoly_gen pb, ← degree_minpoly pb],
exact minpoly.min _ _ q_monic aeval_q
end
end minpoly
section equiv
variables [algebra A S] {S' : Type*} [comm_ring S'] [algebra A S']
lemma nat_degree_lt_nat_degree {p q : polynomial R} (hp : p ≠ 0) (hpq : p.degree < q.degree) :
p.nat_degree < q.nat_degree :=
begin
by_cases hq : q = 0, { rw [hq, degree_zero] at hpq, have := not_lt_bot hpq, contradiction },
rwa [degree_eq_nat_degree hp, degree_eq_nat_degree hq, with_bot.coe_lt_coe] at hpq
end
lemma constr_pow_aeval (pb : power_basis A S) {y : S'}
(hy : aeval y pb.minpoly_gen = 0) (f : polynomial A) :
pb.is_basis.constr (λ i, y ^ (i : ℕ)) (aeval pb.gen f) = aeval y f :=
begin
rw [← aeval_mod_by_monic_eq_self_of_root
pb.minpoly_gen_monic
pb.aeval_minpoly_gen,
← @aeval_mod_by_monic_eq_self_of_root _ _ _ _ _ f _
pb.minpoly_gen_monic y hy],
by_cases hf : f %ₘ pb.minpoly_gen = 0,
{ simp only [hf, alg_hom.map_zero, linear_map.map_zero] },
have : (f %ₘ pb.minpoly_gen).nat_degree < pb.dim,
{ rw ← pb.nat_degree_minpoly_gen,
apply nat_degree_lt_nat_degree hf,
exact degree_mod_by_monic_lt _ pb.minpoly_gen_monic pb.minpoly_gen_ne_zero },
rw [aeval_eq_sum_range' this, aeval_eq_sum_range' this, linear_map.map_sum],
refine finset.sum_congr rfl (λ i (hi : i ∈ finset.range pb.dim), _),
rw finset.mem_range at hi,
rw linear_map.map_smul,
congr,
exact @constr_basis _ _ _ _ _ _ _ _ _ _ _ (⟨i, hi⟩ : fin pb.dim) pb.is_basis,
end
lemma constr_pow_gen (pb : power_basis A S) {y : S'}
(hy : aeval y pb.minpoly_gen = 0) :
pb.is_basis.constr (λ i, y ^ (i : ℕ)) pb.gen = y :=
by { convert pb.constr_pow_aeval hy X; rw aeval_X }
lemma constr_pow_algebra_map (pb : power_basis A S) {y : S'}
(hy : aeval y pb.minpoly_gen = 0) (x : A) :
pb.is_basis.constr (λ i, y ^ (i : ℕ)) (algebra_map A S x) = algebra_map A S' x :=
by { convert pb.constr_pow_aeval hy (C x); rw aeval_C }
lemma constr_pow_mul [nontrivial S] (pb : power_basis A S) {y : S'}
(hy : aeval y pb.minpoly_gen = 0) (x x' : S) :
pb.is_basis.constr (λ i, y ^ (i : ℕ)) (x * x') =
pb.is_basis.constr (λ i, y ^ (i : ℕ)) x * pb.is_basis.constr (λ i, y ^ (i : ℕ)) x' :=
begin
obtain ⟨f, hf, rfl⟩ := pb.exists_eq_aeval x,
obtain ⟨g, hg, rfl⟩ := pb.exists_eq_aeval x',
simp only [← aeval_mul, pb.constr_pow_aeval hy]
end
/-- `pb.lift y hy` is the algebra map sending `pb.gen` to `y`,
where `hy` states the higher powers of `y` are the same as the higher powers of `pb.gen`. -/
noncomputable def lift [nontrivial S] (pb : power_basis A S) (y : S')
(hy : aeval y pb.minpoly_gen = 0) :
S →ₐ[A] S' :=
{ map_one' := by { convert pb.constr_pow_algebra_map hy 1 using 2; rw ring_hom.map_one },
map_zero' := by { convert pb.constr_pow_algebra_map hy 0 using 2; rw ring_hom.map_zero },
map_mul' := pb.constr_pow_mul hy,
commutes' := pb.constr_pow_algebra_map hy,
.. pb.is_basis.constr (λ i, y ^ (i : ℕ)) }
@[simp] lemma lift_gen [nontrivial S] (pb : power_basis A S) (y : S')
(hy : aeval y pb.minpoly_gen = 0) :
pb.lift y hy pb.gen = y :=
pb.constr_pow_gen hy
@[simp] lemma lift_aeval [nontrivial S] (pb : power_basis A S) (y : S')
(hy : aeval y pb.minpoly_gen = 0) (f : polynomial A) :
pb.lift y hy (aeval pb.gen f) = aeval y f :=
pb.constr_pow_aeval hy f
/-- `pb.equiv pb' h` is an equivalence of algebras with the same power basis. -/
noncomputable def equiv [nontrivial S] [nontrivial S']
(pb : power_basis A S) (pb' : power_basis A S')
(h : pb.minpoly_gen = pb'.minpoly_gen) :
S ≃ₐ[A] S' :=
alg_equiv.of_alg_hom
(pb.lift pb'.gen (h.symm ▸ pb'.aeval_minpoly_gen))
(pb'.lift pb.gen (h ▸ pb.aeval_minpoly_gen))
(by { ext x, obtain ⟨f, hf, rfl⟩ := pb'.exists_eq_aeval x, simp })
(by { ext x, obtain ⟨f, hf, rfl⟩ := pb.exists_eq_aeval x, simp })
@[simp]
lemma equiv_aeval [nontrivial S] [nontrivial S']
(pb : power_basis A S) (pb' : power_basis A S')
(h : pb.minpoly_gen = pb'.minpoly_gen)
(f : polynomial A) :
pb.equiv pb' h (aeval pb.gen f) = aeval pb'.gen f :=
pb.lift_aeval _ (h.symm ▸ pb'.aeval_minpoly_gen) _
@[simp]
lemma equiv_gen [nontrivial S] [nontrivial S']
(pb : power_basis A S) (pb' : power_basis A S')
(h : pb.minpoly_gen = pb'.minpoly_gen) :
pb.equiv pb' h pb.gen = pb'.gen :=
pb.lift_gen _ (h.symm ▸ pb'.aeval_minpoly_gen)
local attribute [irreducible] power_basis.lift
@[simp]
lemma equiv_symm [nontrivial S] [nontrivial S']
(pb : power_basis A S) (pb' : power_basis A S')
(h : pb.minpoly_gen = pb'.minpoly_gen) :
(pb.equiv pb' h).symm = pb'.equiv pb h.symm :=
rfl
/-- An algebra equivalence induces an equivalence of power bases.
This definition is used for either direction of `power_basis.congr`.
-/
def congr_aux {R A A' : Type*} [comm_ring R] [ring A] [ring A']
[algebra R A] [algebra R A'] (e : A ≃ₐ[R] A')
(pb : power_basis R A) : power_basis R A' :=
{ gen := e pb.gen,
dim := pb.dim,
is_basis := begin
simp only [← e.map_pow],
convert linear_equiv.is_basis pb.is_basis e.to_linear_equiv
end }
@[ext]
lemma ext {R A : Type*} [integral_domain R] [ring A] [algebra R A]
{pb pb' : power_basis R A} (h : pb.gen = pb'.gen) : pb = pb' :=
begin
cases pb, cases pb', congr,
{ exact h },
convert le_antisymm
(pb'_is_basis.card_le_card_of_linear_independent pb_is_basis.1)
(pb_is_basis.card_le_card_of_linear_independent pb'_is_basis.1);
rw fintype.card_fin
end
lemma ext_iff {R A : Type*} [integral_domain R] [ring A] [algebra R A]
{pb pb' : power_basis R A} : pb = pb' ↔ pb.gen = pb'.gen :=
⟨λ h, by rw h, power_basis.ext⟩
/-- An algebra equivalence induces an equivalence of power bases. -/
def congr {R A A' : Type*} [integral_domain R] [ring A] [ring A']
[algebra R A] [algebra R A'] (e : A ≃ₐ[R] A') :
power_basis R A ≃ power_basis R A' :=
{ to_fun := power_basis.congr_aux e,
inv_fun := power_basis.congr_aux e.symm,
left_inv := λ pb, power_basis.ext (e.symm_apply_apply pb.gen),
right_inv := λ pb, power_basis.ext (e.apply_symm_apply pb.gen) }
end equiv
end power_basis
namespace algebra
open power_basis
lemma mem_span_power_basis [nontrivial R] {x y : S} (hx : _root_.is_integral R x)
(hy : ∃ f : polynomial R, y = aeval x f) :
y ∈ submodule.span R (set.range (λ (i : fin (minpoly R x).nat_degree),
x ^ (i : ℕ))) :=
begin
obtain ⟨f, rfl⟩ := hy,
rw mem_span_pow',
have := minpoly.monic hx,
refine ⟨f.mod_by_monic (minpoly R x),
lt_of_lt_of_le (degree_mod_by_monic_lt _ this (ne_zero_of_monic this)) degree_le_nat_degree,
_⟩,
conv_lhs { rw ← mod_by_monic_add_div f this },
simp only [add_zero, zero_mul, minpoly.aeval, aeval_add, alg_hom.map_mul]
end
lemma linear_independent_power_basis [algebra K S] {x : S} (hx : _root_.is_integral K x) :
linear_independent K (λ (i : fin (minpoly K x).nat_degree), x ^ (i : ℕ)) :=
begin
rw linear_independent_iff,
intros p hp,
let f : polynomial K := p.sum (λ i, monomial i),
have f_def : ∀ (i : fin _), f.coeff i = p i,
{ intro i,
-- TODO: how can we avoid unfolding here?
change (p.sum (λ i pi, finsupp.single i pi) : ℕ →₀ K) i = p i,
simp_rw [finsupp.sum_apply, finsupp.single_apply, finsupp.sum],
rw [finset.sum_eq_single, if_pos rfl],
{ intros b _ hb,
rw if_neg (mt (λ h, _) hb),
exact fin.coe_injective h },
{ intro hi,
split_ifs; { exact finsupp.not_mem_support_iff.mp hi } } },
have f_def' : ∀ i, f.coeff i = if hi : i < _ then p ⟨i, hi⟩ else 0,
{ intro i,
split_ifs with hi,
{ exact f_def ⟨i, hi⟩ },
-- TODO: how can we avoid unfolding here?
change (p.sum (λ i pi, finsupp.single i pi) : ℕ →₀ K) i = 0,
simp_rw [finsupp.sum_apply, finsupp.single_apply, finsupp.sum],
apply finset.sum_eq_zero,
rintro ⟨j, hj⟩ -,
apply if_neg (mt _ hi),
rintro rfl,
exact hj },
suffices : f = 0,
{ ext i, rw [← f_def, this, coeff_zero, finsupp.zero_apply] },
contrapose hp with hf,
intro h,
have : (minpoly K x).degree ≤ f.degree,
{ apply minpoly.degree_le_of_ne_zero K x hf,
convert h,
rw [finsupp.total_apply, aeval_def, eval₂_eq_sum, finsupp.sum_sum_index],
{ apply finset.sum_congr rfl,
rintro i -,
simp only [algebra.smul_def, monomial, finsupp.lsingle_apply, zero_mul, ring_hom.map_zero,
finsupp.sum_single_index] },
{ intro, simp only [ring_hom.map_zero, zero_mul] },
{ intros, simp only [ring_hom.map_add, add_mul] } },
have : ¬ (minpoly K x).degree ≤ f.degree,
{ apply not_le_of_lt,
rw [degree_eq_nat_degree (minpoly.ne_zero hx), degree_lt_iff_coeff_zero],
intros i hi,
rw [f_def' i, dif_neg],
exact not_lt_of_ge hi },
contradiction
end
lemma power_basis_is_basis [algebra K S] {x : S} (hx : _root_.is_integral K x) :
is_basis K (λ (i : fin (minpoly K x).nat_degree),
(⟨x, subset_adjoin (set.mem_singleton x)⟩ ^ (i : ℕ) : adjoin K ({x} : set S))) :=
begin
have hST : function.injective (algebra_map (adjoin K ({x} : set S)) S) := subtype.coe_injective,
have hx' : _root_.is_integral K
(show adjoin K ({x} : set S), from ⟨x, subset_adjoin (set.mem_singleton x)⟩),
{ apply (is_integral_algebra_map_iff hST).mp,
convert hx,
apply_instance },
have minpoly_eq := minpoly.eq_of_algebra_map_eq hST hx' rfl,
refine ⟨_, _root_.eq_top_iff.mpr _⟩,
{ have := linear_independent_power_basis hx',
rwa minpoly_eq at this },
{ rintros ⟨y, hy⟩ _,
have := mem_span_power_basis hx',
rw minpoly_eq at this,
apply this,
{ rw [adjoin_singleton_eq_range] at hy,
obtain ⟨f, rfl⟩ := (aeval x).mem_range.mp hy,
use f,
ext,
exact (is_scalar_tower.algebra_map_aeval K (adjoin K {x}) S ⟨x, _⟩ _).symm } }
end
/-- The power basis `1, x, ..., x ^ (d - 1)` for `K[x]`,
where `d` is the degree of the minimal polynomial of `x`. -/
noncomputable def adjoin.power_basis [algebra K S] {x : S} (hx : _root_.is_integral K x) :
power_basis K (adjoin K ({x} : set S)) :=
{ gen := ⟨x, subset_adjoin (set.mem_singleton x)⟩,
dim := (minpoly K x).nat_degree,
is_basis := power_basis_is_basis hx }
end algebra
namespace adjoin_root
variables {f : polynomial K}
lemma power_basis_is_basis (hf : f ≠ 0) : is_basis K (λ (i : fin f.nat_degree), (root f ^ i.val)) :=
begin
set f' := f * C (f.leading_coeff⁻¹) with f'_def,
have deg_f' : f'.nat_degree = f.nat_degree,
{ rw [nat_degree_mul hf, nat_degree_C, add_zero],
{ rwa [ne.def, C_eq_zero, inv_eq_zero, leading_coeff_eq_zero] } },
have f'_monic : monic f' := monic_mul_leading_coeff_inv hf,
have aeval_f' : aeval (root f) f' = 0,
{ rw [f'_def, alg_hom.map_mul, aeval_eq, mk_self, zero_mul] },
have hx : is_integral K (root f) := ⟨f', f'_monic, aeval_f'⟩,
have minpoly_eq : f' = minpoly K (root f),
{ apply minpoly.unique K _ f'_monic aeval_f',
intros q q_monic q_aeval,
have commutes : (lift (algebra_map K (adjoin_root f)) (root f) q_aeval).comp (mk q) = mk f,
{ ext,
{ simp only [ring_hom.comp_apply, mk_C, lift_of], refl },
{ simp only [ring_hom.comp_apply, mk_X, lift_root] } },
rw [degree_eq_nat_degree f'_monic.ne_zero, degree_eq_nat_degree q_monic.ne_zero,
with_bot.coe_le_coe, deg_f'],
apply nat_degree_le_of_dvd,
{ rw [←ideal.mem_span_singleton, ←ideal.quotient.eq_zero_iff_mem],
change mk f q = 0,
rw [←commutes, ring_hom.comp_apply, mk_self, ring_hom.map_zero] },
{ exact q_monic.ne_zero } },
refine ⟨_, eq_top_iff.mpr _⟩,
{ rw [←deg_f', minpoly_eq],
exact algebra.linear_independent_power_basis hx, },
{ rintros y -,
rw [←deg_f', minpoly_eq],
apply algebra.mem_span_power_basis hx,
obtain ⟨g⟩ := y,
use g,
rw aeval_eq,
refl }
end
/-- The power basis `1, root f, ..., root f ^ (d - 1)` for `adjoin_root f`,
where `f : polynomial K` of degree `d ≥ 0`. -/
noncomputable def power_basis (hf : f ≠ 0) :
power_basis K (adjoin_root f) :=
{ gen := root f,
dim := f.nat_degree,
is_basis := power_basis_is_basis hf }
@[simp] lemma gen_eq (hf : f ≠ 0) : (adjoin_root.power_basis hf).gen = root f := rfl
lemma minpoly_gen_dvd (hf : f ≠ 0) :
(adjoin_root.power_basis hf).minpoly_gen ∣ f :=
by { rw power_basis.minpoly_gen_eq, exact minpoly.dvd _ _ (adjoin_root.eval₂_root f) }
@[simp]
lemma minpoly_gen_eq (hf : irreducible f) (hfm : monic f) :
(adjoin_root.power_basis hf.ne_zero).minpoly_gen = f :=
begin
rw [(power_basis hf.ne_zero).minpoly_gen_eq, ← minpoly.unique' _ hf _ hfm],
{ exact field.to_nontrivial _ },
{ exact (power_basis hf.ne_zero).is_integral_gen },
{ exact adjoin_root.eval₂_root f }
end
end adjoin_root
|
ec64869f11b77e1732c85499e315eca076b2f066 | 037dba89703a79cd4a4aec5e959818147f97635d | /src/2021/sets/sheet7.lean | 1524c9948c549fdcd820adc17a4707147d4e1086 | [] | no_license | ImperialCollegeLondon/M40001_lean | 3a6a09298da395ab51bc220a535035d45bbe919b | 62a76fa92654c855af2b2fc2bef8e60acd16ccec | refs/heads/master | 1,666,750,403,259 | 1,665,771,117,000 | 1,665,771,117,000 | 209,141,835 | 115 | 12 | null | 1,640,270,596,000 | 1,568,749,174,000 | Lean | UTF-8 | Lean | false | false | 5,073 | lean | /-
Copyright (c) 2021 Kevin Buzzard. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author : Kevin Buzzard
-/
import tactic -- imports all the Lean tactics
/-!
# Sets in Lean, sheet 7 : the sets API
It might have occurred to you that if you had a prove
things like commutativity of `∩` every time you needed it,
then things would get boring quite quickly.
Fortunately, the writers of Lean's mathematics library
already proved these things and *gave the proofs names*.
On the previous sheet you showed that you could do these
things yourself, so on this sheet I'll tell you the names
of the Lean versions of the proofs, and show you how to use them.
Here are some examples of proof names in Lean.
`set.union_comm A B : A ∪ B = B ∪ A`
`set.inter_comm A B : A ∩ B = B ∩ A`
-- note `A ∪ B ∪ C` means `(A ∪ B) ∪ C`
`set.union_assoc A B C : A ∪ B ∪ C = A ∪ (B ∪ C)`
-- similar comment for `∩`
`set.inter_assoc A B C : A ∩ B ∩ C = A ∩ (B ∩ C)`
(A technical point: note that `∪` and `∩` are "left associative",
in contrast to `→` which is right associative: `P → Q → R` means `P → (Q → R)`).
These lemmas are all in the `set` namespace (i.e. their full names
all begin with `set.`). It's annoying having to type `set.` all the time
so we `open set` which means you can skip it and just write things
like `union_comm A B`.
Note that `union_comm A B` is the *proof* that `A ∪ B = B ∪ A`.
In the language of type theory, `union_comm A B` is a
"term of type `A ∪ B = B ∪ A`". Let's just compare this with what we
were seeing in the logic sheet:
```
P : Prop -- P is a proposition
hP : P -- hP is a proof of P
```
Similarly here we have
```
A ∪ B = B ∪ A : Prop -- this is the *statement*
union_comm A B : A ∪ B = B ∪ A -- this is the *proof*
```
## New tactics you will need
* `rw` ("rewrite")
### The `rw` tactic (again)
We've seen `rw h,` being used if `h : P ↔ Q`; it changed all `P`s to `Q`s
in the goal. But `rw h,` also works for `h : A = B` -- it changes all
`A`s to `B`s in the goal. So `rw` is a "substitute in" command.
After the substitution has occurred, Lean tries `refl` just to see if it works.
For example if `A`, `B`, `C` are sets, and our context is
```
h : A = B
⊢ A ∩ C = B ∩ C
```
then `rw h` changes the goal into `B ∩ C = B ∩ C` and then solves
the goal automatically, because `refl` works.
`rw` doesn't just work for hypotheses -- if there is a theorem
in Lean's maths library (like `inter_comm A B`, which is a proof
that `A ∩ B = B ∩ A`) then you can `rw inter_comm A B` and it
will change `A ∩ B` in the goal to `B ∩ A`.
`rw` is a smart tactic. If the goal is
```
⊢ (A ∪ B) ∩ C = D
```
and you want to change it to `⊢ (B ∪ A) ∩ C = D` then you don't
have to write `rw union_comm A B`, you can write `rw union_comm`
and Lean will figure out what you meant. You can also write
`rw union_comm A` or even `rw union_comm _ B` if you want to give
it hints about exactly which union to commute.
-/
open set
variables
(X : Type) -- Everything will be a subset of `X`
(A B C D E : set X) -- A,B,C,D,E are subsets of `X`
(x y z : X) -- x,y,z are elements of `X` or, more precisely, terms of type `X`
-- You can solve this one with `exact union_comm A B` or `rw union_comm`
example : A ∪ B = B ∪ A :=
begin
sorry
end
example : (A ∪ B) ∩ C = C ∩ (B ∪ A) :=
begin
sorry
end
/-
Note that `rw union_comm A` will change `A ∪ ?` to
`? ∪ A` for some set `?`. You can even do `rw union_comm _ B` if you
want to change `? ∪ B` to `B ∪ ?`
-/
example : A ∪ B ∪ C = C ∪ B ∪ A :=
begin
sorry
end
example : x ∈ A ∩ B ∩ C ↔ x ∈ B ∩ C ∩ A :=
begin
sorry
end
example : ((A ∪ B) ∪ C) ∪ D = A ∪ (B ∪ (C ∪ D)) :=
begin
sorry
end
example : A ∪ B = C ∩ D → B ∪ A ∪ E = E ∪ (C ∩ D) :=
begin
sorry
end
/-
Here are some distributivity results.
`set.union_distrib_left A B C : A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)`
`set.union_distrib_right A B C : (A ∩ B) ∪ C = (A ∪ C) ∩ (B ∪ C)`
`set.inter_distrib_left A B C : A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C)`
`set.inter_distrib_right A B C : (A ∪ B) ∩ C = (A ∩ C) ∪ (B ∩ C)`
Which ones will you use to do the following? Note that because of the `open set`
line much earlier, you don't have to write the `set.` part of the name.
-/
example : (A ∪ B) ∩ (C ∪ D) = (A ∩ C) ∪ (B ∩ C) ∪ (A ∩ D) ∪ (B ∩ D) :=
begin
sorry
end
/-
## Top tips
How the heck are we supposed to remember all the names of these lemmas
in Lean's maths library??
Method 1) There is a logic to the naming system! You will slowly
get used to the logic.
Method 2) Until then, just use the `library_search` tactic
to look up proofs.
If you run the below code
```
example : A ∩ B = B ∩ A :=
begin
library_search
end
```
you'll see some output on the right hand side of the screen containing
the name of the lemma which proves this example.
-/
|
17fd3f1792a4f9f984bb451a91ccf910f1638ea5 | e030b0259b777fedcdf73dd966f3f1556d392178 | /library/init/meta/smt/smt_tactic.lean | 373a9e3f53f95084e0150e015009fee9710dc9f5 | [
"Apache-2.0"
] | permissive | fgdorais/lean | 17b46a095b70b21fa0790ce74876658dc5faca06 | c3b7c54d7cca7aaa25328f0a5660b6b75fe26055 | refs/heads/master | 1,611,523,590,686 | 1,484,412,902,000 | 1,484,412,902,000 | 38,489,734 | 0 | 0 | null | 1,435,923,380,000 | 1,435,923,379,000 | null | UTF-8 | Lean | false | false | 13,788 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.meta.simp_tactic
import init.meta.smt.congruence_closure
import init.meta.smt.ematch
universe variables u
run_command mk_simp_attr `pre_smt
run_command mk_hinst_lemma_attr_set `ematch [] [`ematch_lhs]
/--
Configuration for the smt tactic preprocessor. The preprocessor
is applied whenever a new hypothesis is introduced.
- simp_attr: is the attribute name for the simplification lemmas
that are used during the preprocessing step.
- max_steps: it is the maximum number of steps performed by the simplifier.
- zeta: if tt, then zeta reduction (i.e., unfolding let-expressions)
is used during preprocessing.
-/
structure smt_pre_config :=
(simp_attr : name)
(max_steps : nat)
(zeta : bool)
def default_smt_pre_config : smt_pre_config :=
{ simp_attr := `pre_smt,
max_steps := 1000000,
zeta := ff }
/--
Configuration for the smt_state object.
- em_attr: is the attribute name for the hinst_lemmas
that are used for ematching -/
structure smt_config :=
(cc_cfg : cc_config)
(em_cfg : ematch_config)
(pre_cfg : smt_pre_config)
(em_attr : name)
def default_smt_config : smt_config :=
{cc_cfg := default_cc_config,
em_cfg := default_ematch_config,
pre_cfg := default_smt_pre_config,
em_attr := `ematch}
meta def smt_config.set_classical (c : smt_config) (b : bool) : smt_config :=
{c with cc_cfg := { (c^.cc_cfg) with em := b}}
meta constant smt_goal : Type
meta def smt_state :=
list smt_goal
meta constant smt_state.mk : smt_config → tactic smt_state
meta constant smt_state.to_format : smt_state → tactic_state → format
/-- Return tt iff classical excluded middle was enabled at smt_state.mk -/
meta constant smt_state.classical : smt_state → bool
meta def smt_tactic :=
state_t smt_state tactic
meta instance : has_append smt_state :=
list.has_append
meta instance : monad smt_tactic :=
state_t.monad _ _
/- We don't use the default state_t lift operation because only
tactics that do not change hypotheses can be automatically lifted to smt_tactic. -/
meta constant tactic_to_smt_tactic (α : Type) : tactic α → smt_tactic α
meta instance : monad.has_monad_lift tactic smt_tactic :=
⟨tactic_to_smt_tactic⟩
meta instance (α : Type) : has_coe (tactic α) (smt_tactic α) :=
⟨monad.monad_lift⟩
meta def smt_tactic_orelse {α : Type} (t₁ t₂ : smt_tactic α) : smt_tactic α :=
λ ss ts, tactic_result.cases_on (t₁ ss ts)
tactic_result.success
(λ e₁ ref₁ s', tactic_result.cases_on (t₂ ss ts)
tactic_result.success
(tactic_result.exception (α × smt_state)))
meta instance : alternative smt_tactic :=
{failure := λ α, @tactic.failed α,
orelse := @smt_tactic_orelse,
pure := @return _ _,
seq := @fapp _ _,
map := @fmap _ _}
namespace smt_tactic
open tactic (transparency)
meta constant intros : smt_tactic unit
meta constant intron : nat → smt_tactic unit
meta constant intro_lst : list name → smt_tactic unit
/--
Try to close main goal by using equalities implied by the congruence
closure module.
-/
meta constant close : smt_tactic unit
/--
Produce new facts using heuristic lemma instantiation based on E-matching.
This tactic tries to match patterns from lemmas in the main goal with terms
in the main goal. The set of lemmas is populated with theorems
tagged with the attribute specified at smt_config.em_attr, and lemmas
added using tactics such as `smt_tactic.add_lemmas`.
The current set of lemmas can be retrieved using the tactic `smt_tactic.get_lemmas`.
Remark: the given predicate is applied to every new instance. The instance
is only added to the state if the predicate returns tt.
-/
meta constant ematch_core : (expr → bool) → smt_tactic unit
/--
Produce new facts using heuristic lemma instantiation based on E-matching.
This tactic tries to match patterns from the given lemmas with terms in
the main goal.
-/
meta constant ematch_using : hinst_lemmas → smt_tactic unit
meta constant mk_ematch_eqn_lemmas_for_core : transparency → name → smt_tactic hinst_lemmas
meta constant to_cc_state : smt_tactic cc_state
meta constant to_em_state : smt_tactic ematch_state
meta constant get_config : smt_tactic cc_config
/--
Preprocess the given term using the same simplifications rules used when
we introduce a new hypothesis. The result is pair containing the resulting
term and a proof that it is equal to the given one.
-/
meta constant preprocess : expr → smt_tactic (expr × expr)
meta constant get_lemmas : smt_tactic hinst_lemmas
meta constant set_lemmas : hinst_lemmas → smt_tactic unit
meta constant add_lemmas : hinst_lemmas → smt_tactic unit
meta def add_ematch_lemma_core (md : transparency) (as_simp : bool) (e : expr) : smt_tactic unit :=
do h ← hinst_lemma.mk_core md e as_simp,
add_lemmas (mk_hinst_singleton h)
meta def add_ematch_lemma_from_decl_core (md : transparency) (as_simp : bool) (n : name) : smt_tactic unit :=
do h ← hinst_lemma.mk_from_decl_core md n as_simp,
add_lemmas (mk_hinst_singleton h)
meta def add_ematch_eqn_lemmas_for_core (md : transparency) (n : name) : smt_tactic unit :=
do hs ← mk_ematch_eqn_lemmas_for_core md n,
add_lemmas hs
meta def ematch : smt_tactic unit :=
ematch_core (λ _, tt)
meta def failed : smt_tactic unit :=
tactic.failed
meta def fail {α : Type} {β : Type u} [has_to_format β] (msg : β) : tactic α :=
tactic.fail msg
meta def try {α : Type} (t : smt_tactic α) : smt_tactic unit :=
λ ss ts, tactic_result.cases_on (t ss ts)
(λ ⟨a, new_ss⟩, tactic_result.success ((), new_ss))
(λ e ref s', tactic_result.success ((), ss) ts)
/- (repeat_at_most n t): repeat the given tactic at most n times or until t fails -/
meta def repeat_at_most : nat → smt_tactic unit → smt_tactic unit
| 0 t := return ()
| (n+1) t := (do t, repeat_at_most n t) <|> return ()
/-- (repeat_exactly n t) : execute t n times -/
meta def repeat_exactly : nat → smt_tactic unit → smt_tactic unit
| 0 t := return ()
| (n+1) t := do t, repeat_exactly n t
meta def repeat : smt_tactic unit → smt_tactic unit :=
repeat_at_most 100000
meta def eblast : smt_tactic unit :=
repeat (ematch >> try close)
open tactic
protected meta def read : smt_tactic (smt_state × tactic_state) :=
do s₁ ← state_t.read,
s₂ ← tactic.read,
return (s₁, s₂)
private meta def mk_smt_goals_for (cfg : smt_config) : list expr → list smt_goal → list expr
→ tactic (list smt_goal × list expr)
| [] sr tr := return (sr^.reverse, tr^.reverse)
| (tg::tgs) sr tr := do
tactic.set_goals [tg],
[new_sg] ← smt_state.mk cfg | tactic.failed,
[new_tg] ← get_goals | tactic.failed,
mk_smt_goals_for tgs (new_sg::sr) (new_tg::tr)
/--
This lift operation will restart the SMT state.
It is useful for using tactics that change the set of hypotheses.
-/
meta def slift {α : Type} (t : tactic α) : smt_tactic α :=
λ ss, do
_::sgs ← return ss | fail "slift tactic failed, there no smt goals to be solved",
cfg ← return default_smt_config, -- TODO(Leo): use get_config
tg::tgs ← tactic.get_goals | tactic.failed,
tactic.set_goals [tg], a ← t,
new_tgs ← tactic.get_goals,
(new_sgs, new_tgs) ← mk_smt_goals_for cfg new_tgs [] [],
tactic.set_goals (new_tgs ++ tgs),
return (a, new_sgs ++ sgs)
meta def trace_state : smt_tactic unit :=
do (s₁, s₂) ← smt_tactic.read,
trace (smt_state.to_format s₁ s₂)
meta def trace {α : Type} [has_to_tactic_format α] (a : α) : smt_tactic unit :=
tactic.trace a
meta def classical : smt_tactic bool :=
do s ← state_t.read,
return s^.classical
/- Low level primitives for managing set of goals -/
meta def get_goals : smt_tactic (list smt_goal × list expr) :=
do (g₁, _) ← smt_tactic.read,
g₂ ← tactic.get_goals,
return (g₁, g₂)
meta def set_goals : list smt_goal → list expr → smt_tactic unit :=
λ g₁ g₂ ss, tactic.set_goals g₂ >> return ((), g₁)
private meta def all_goals_core (tac : smt_tactic unit) : list smt_goal → list expr → list smt_goal → list expr → smt_tactic unit
| [] ts acs act := set_goals acs (ts ++ act)
| (s :: ss) [] acs act := fail "ill-formed smt_state"
| (s :: ss) (t :: ts) acs act :=
do set_goals [s] [t],
tac,
(new_ss, new_ts) ← get_goals,
all_goals_core ss ts (acs ++ new_ss) (act ++ new_ts)
/- Apply the given tactic to all goals. -/
meta def all_goals (tac : smt_tactic unit) : smt_tactic unit :=
do (ss, ts) ← get_goals,
all_goals_core tac ss ts [] []
/- LCF-style AND_THEN tactic. It applies tac1, and if succeed applies tac2 to each subgoal produced by tac1 -/
meta def seq (tac1 : smt_tactic unit) (tac2 : smt_tactic unit) : smt_tactic unit :=
do (s::ss, t::ts) ← get_goals | failed,
set_goals [s] [t],
tac1, all_goals tac2,
(new_ss, new_ts) ← get_goals,
set_goals (new_ss ++ ss) (new_ts ++ ts)
meta def swap : smt_tactic unit :=
do (ss, ts) ← get_goals,
match ss, ts with
| (s₁ :: s₂ :: ss), (t₁ :: t₂ :: ts) := set_goals (s₂ :: s₁ :: ss) (t₂ :: t₁ :: ts)
| _, _ := failed
end
/-- Add a new goal for t, and the hypothesis (h : t) in the current goal. -/
meta def assert (h : name) (t : expr) : smt_tactic unit :=
tactic.assert_core h t >> swap >> intros >> swap >> try close
/-- Add the hypothesis (h : t) in the current goal if v has type t. -/
meta def assertv (h : name) (t : expr) (v : expr) : smt_tactic unit :=
tactic.assertv_core h t v >> intros >> return ()
/-- Add a new goal for t, and the hypothesis (h : t := ?M) in the current goal. -/
meta def define (h : name) (t : expr) : smt_tactic unit :=
tactic.define_core h t >> swap >> intros >> swap >> try close
/-- Add the hypothesis (h : t := v) in the current goal if v has type t. -/
meta def definev (h : name) (t : expr) (v : expr) : smt_tactic unit :=
tactic.definev_core h t v >> intros >> return ()
/-- Add (h : t := pr) to the current goal -/
meta def pose (h : name) (pr : expr) : smt_tactic unit :=
do t ← tactic.infer_type pr,
definev h t pr
/- Add (h : t) to the current goal, given a proof (pr : t) -/
meta def note (n : name) (pr : expr) : smt_tactic unit :=
do t ← tactic.infer_type pr,
assertv n t pr
meta def destruct (e : expr) : smt_tactic unit :=
smt_tactic.seq (tactic.destruct e) smt_tactic.intros
meta def by_cases (e : expr) : smt_tactic unit :=
do c ← classical,
if c then
destruct (expr.app (expr.const `classical.em []) e)
else do
dec_e ← (mk_app `decidable [e] <|> fail "by_cases smt_tactic failed, type is not a proposition"),
inst ← (mk_instance dec_e <|> fail "by_cases smt_tactic failed, type of given expression is not decidable"),
em ← mk_app `decidable.em [e, inst],
destruct em
meta def by_contradiction : smt_tactic unit :=
do t ← target,
c ← classical,
if t^.is_false then skip
else if c then do
apply (expr.app (expr.const `classical.by_contradiction []) t),
intros
else do
dec_t ← (mk_app `decidable [t] <|> fail "by_contradiction smt_tactic failed, target is not a proposition"),
inst ← (mk_instance dec_t <|> fail "by_contradiction smt_tactic failed, target is not decidable"),
a ← mk_mapp `decidable.by_contradiction [some t, some inst],
apply a,
intros
/- Return a proof for e, if 'e' is a known fact in the main goal. -/
meta def proof_for (e : expr) : smt_tactic expr :=
do cc ← to_cc_state, cc^.proof_for e
/- Return a refutation for e (i.e., a proof for (not e)), if 'e' has been refuted in the main goal. -/
meta def refutation_for (e : expr) : smt_tactic expr :=
do cc ← to_cc_state, cc^.refutation_for e
meta def get_facts : smt_tactic (list expr) :=
do cc ← to_cc_state,
return $ cc^.eqc_of expr.mk_true
meta def get_refuted_facts : smt_tactic (list expr) :=
do cc ← to_cc_state,
return $ cc^.eqc_of expr.mk_false
meta def add_ematch_lemma : expr → smt_tactic unit :=
add_ematch_lemma_core reducible ff
meta def add_ematch_lhs_lemma : expr → smt_tactic unit :=
add_ematch_lemma_core reducible tt
meta def add_ematch_lemma_from_decl : name → smt_tactic unit :=
add_ematch_lemma_from_decl_core reducible ff
meta def add_ematch_lhs_lemma_from_decl : name → smt_tactic unit :=
add_ematch_lemma_from_decl_core reducible ff
meta def add_ematch_eqn_lemmas_for : name → smt_tactic unit :=
add_ematch_eqn_lemmas_for_core reducible
meta def add_lemmas_from_facts_core : list expr → smt_tactic unit
| [] := return ()
| (f::fs) := do
try (is_prop f >> guard (f^.is_pi && bnot (f^.is_arrow)) >> proof_for f >>= add_ematch_lemma_core reducible ff),
add_lemmas_from_facts_core fs
meta def add_lemmas_from_facts : smt_tactic unit :=
get_facts >>= add_lemmas_from_facts_core
meta def induction (e : expr) (rec : name) (ids : list name) : smt_tactic unit :=
slift (tactic.induction e rec ids)
end smt_tactic
open smt_tactic
meta def using_smt_core (cfg : smt_config) (t : smt_tactic unit) : tactic unit :=
do ss ← smt_state.mk cfg,
(t >> repeat close) ss,
return ()
meta def using_smt : smt_tactic unit → tactic unit :=
using_smt_core default_smt_config
|
b4f1f4fa9543d5dca2f4710a90a2824ce51c7996 | 5ffb8080b18aa71638631832811ab18c0aec5879 | /src/group/basic.lean | 371a27622e1d4525c21911598b6694baec2a8414 | [
"Apache-2.0"
] | permissive | isabella232/group-theory-game | 805ba8f473af58e3b0437121712dfc36f535b71a | 152ec4a92ad67b6174a3d240c63fa56a6df6017e | refs/heads/master | 1,679,206,314,315 | 1,611,521,951,000 | 1,611,521,951,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,501 | lean | import tactic
/-!
Basic definitions in group theory.
Source:
https://xenaproject.wordpress.com/2018/04/30/group-theory-revision/
-/
set_option old_structure_cmd true -- it's better for this kind of stuff
-- We're overwriting inbuilt group theory here so we always work in
-- a namespace
namespace mygroup
-- definitions of the group classes
section groupdefs
-- Set up notation typeclass using `extends`.
class has_group_notation (G : Type) extends has_mul G, has_one G, has_inv G
-- definition of the group structure
class group (G : Type) extends has_group_notation G :=
(mul_assoc : ∀ (a b c : G), a * b * c = a * (b * c))
(one_mul : ∀ (a : G), 1 * a = a)
(mul_left_inv : ∀ (a : G), a⁻¹ * a = 1)
class comm_group (G : Type) extends group G :=
(mul_comm : ∀ a b : G, a * b = b * a)
-- an example
instance perm_group (α : Type) : group (α ≃ α) :=
{ mul := function.swap equiv.trans,
one := equiv.refl _,
inv := equiv.symm,
mul_assoc := by intros; ext; refl,
one_mul := by intros; ext; refl,
mul_left_inv := by intros; ext; simp }
end groupdefs
/- Our first task is to prove `mul_one` and `mul_right_inv`.
We prove some other things along the way too -- we make
what a computer scientist would call "an interface for
the group class".
Examples of what we prove:
`mul_left_cancel : ∀ (a b c : G), a * b = a * c → b = c`
`mul_eq_of_eq_inv_mul {a x y : G} : x = a⁻¹ * y → a * x = y`
`mul_one (a : G) : a * 1 = a`
`mul_right_inv (a : G) : a * a⁻¹ = 1`
-/
namespace group
variables {G : Type} [group G]
-- We prove left_mul_cancel for group using `calc`.
lemma mul_left_cancel (a b c : G) (Habac : a * b = a * c) : b = c :=
calc b = 1 * b : by rw one_mul
... = (a⁻¹ * a) * b : by rw mul_left_inv
... = a⁻¹ * (a * b) : by rw mul_assoc
... = a⁻¹ * (a * c) : by rw Habac
... = (a⁻¹ * a) * c : by rw mul_assoc
... = 1 * c : by rw mul_left_inv
... = c : by rw one_mul
-- We can do all this one go:
lemma mul_left_cancel' (a b c : G) (Habac : a * b = a * c) : b = c :=
begin
rw [←one_mul b, ←mul_left_inv a, mul_assoc, Habac,
←mul_assoc, mul_left_inv, one_mul],
end
-- Because the above proof just uses one tactic, we could use `by`
-- instead of `begin ... end`:
lemma mul_left_cancel'' (a b c : G) (Habac : a * b = a * c) : b = c :=
by rw [←one_mul b, ←mul_left_inv a, mul_assoc, Habac,
←mul_assoc, mul_left_inv, one_mul]
-- The below is also a useful intermediate lemma
lemma mul_eq_of_eq_inv_mul {a x y : G} (h : x = a⁻¹ * y) : a * x = y :=
begin
apply mul_left_cancel a⁻¹,
rw ←mul_assoc,
rw mul_left_inv,
rwa one_mul, -- rewrite then assumption
end
-- could prove it in `calc` mode:
lemma mul_eq_of_eq_inv_mul' {a x y : G} (h : x = a⁻¹ * y) : a * x = y :=
begin
apply mul_left_cancel a⁻¹,
exact calc
a⁻¹ * (a * x) = a⁻¹ * a * x : by rw mul_assoc
... = 1 * x : by rw mul_left_inv
... = x : by rw one_mul
... = a⁻¹ * y : by rw h
end
attribute [simp] one_mul mul_left_inv
-- Alternatively, get the simplifier to do some of the work for us
lemma mul_eq_of_eq_inv_mul'' {a x y : G} : x = a⁻¹ * y → a * x = y :=
λ h, mul_left_cancel a⁻¹ _ _ $ by rw ←mul_assoc; simp [h]
-- We can now prove `mul_one`:
-- nice short proof
theorem mul_one (a : G) : a * 1 = a :=
begin
apply mul_eq_of_eq_inv_mul,
rw mul_left_inv,
-- note no refl
end
-- calc example (longer than previous one)
theorem mul_one' : ∀ (a : G), a * 1 = a :=
begin
intro a, -- goal is a * 1 = a
apply mul_left_cancel a⁻¹, -- goal now a⁻¹ * (a * 1) = a⁻¹ * a
exact calc a⁻¹ * (a * 1) = (a⁻¹ * a) * 1 : by rw mul_assoc
... = 1 * 1 : by rw mul_left_inv
... = 1 : by rw one_mul
... = a⁻¹ * a : by rw mul_left_inv
end
-- term mode proof
theorem mul_one'' (a : G) : a * 1 = a :=
mul_eq_of_eq_inv_mul $ by simp
-- it's also a good simp lemma
attribute [simp] mul_one
-- mul_left_inv is an axiom: here's mul_right_inv.
theorem mul_right_inv (a : G) : a * a⁻¹ = 1 :=
begin
apply mul_eq_of_eq_inv_mul,
rw mul_one,
end
-- another good simp lemma
attribute [simp] mul_right_inv
-- We already proved `mul_eq_of_eq_inv_mul` but there are several other
-- similar-looking, but slightly different, versions of this. Here
-- is one.
lemma eq_mul_inv_of_mul_eq {a b c : G} (h : a * c = b) : a = b * c⁻¹ :=
begin
rw ←h,
rw mul_assoc,
rw mul_right_inv,
rw mul_one
end
-- one-liner proof
lemma eq_mul_inv_of_mul_eq' {a b c : G} (h : a * c = b) : a = b * c⁻¹ :=
by rw [←h, mul_assoc, mul_right_inv, mul_one]
-- proof using automation
lemma eq_mul_inv_of_mul_eq'' {a b c : G} (h : a * c = b) : a = b * c⁻¹ :=
by simp [h.symm, mul_assoc]
lemma eq_inv_mul_of_mul_eq {a b c : G} (h : b * a = c) : a = b⁻¹ * c :=
begin
rw [←h, ←mul_assoc, mul_left_inv b, one_mul]
end
-- Another useful lemma for the interface:
lemma mul_left_eq_self {a b : G} : a * b = b ↔ a = 1 :=
begin
split,
{ intro h,
replace h := eq_mul_inv_of_mul_eq h,
rw mul_right_inv at h,
assumption },
{ intro h,
rw h,
rw one_mul }
end
lemma mul_right_eq_self {a b : G} : a * b = a ↔ b = 1 :=
begin
split,
intro h,
from calc b = a⁻¹ * a : by apply eq_inv_mul_of_mul_eq h
... = 1 : by rw mul_left_inv,
intro h,
rw [h, mul_one]
end
-- Another useful lemma for the interface.
-- Note use of the powerful `convert` tactic.
-- `eq_mul_inv_of_mul_eq h` says ` a = 1 * b⁻¹` which is
-- equal to our goal; convert creates the goals necessary
-- to prove this
lemma eq_inv_of_mul_eq_one {a b : G} (h : a * b = 1) : a = b⁻¹ :=
begin
convert eq_mul_inv_of_mul_eq h,
rw one_mul, -- `simp` would also work
end
-- Another useful lemma for the interface
lemma inv_inv (a : G) : a ⁻¹ ⁻¹ = a :=
begin
symmetry,
apply eq_inv_of_mul_eq_one,
simp,
end
-- Another useful lemma for the interface
lemma inv_eq_of_mul_eq_one {a b : G} (h : a * b = 1) : a⁻¹ = b :=
begin
-- we can change hypotheses with the `replace` tactic.
-- h implies a = 1 * b⁻¹
replace h := eq_mul_inv_of_mul_eq h,
-- and so a = b⁻¹
rw one_mul at h,
-- By substituting in, we have to prove (b⁻¹)⁻¹ = b
rw h,
-- and we just did this, it's `inv_inv`
rw inv_inv,
end
lemma unique_id {e : G} (h : ∀ x : G, e * x = x) : e = 1 :=
calc e = e * 1 : by rw mul_one
... = 1 : by rw h 1
-- Maybe add unique_id but with x * e = x
lemma unique_inv {a b : G} (h : a * b = 1) : b = a⁻¹ :=
begin
apply mul_left_cancel a,
rw [h, mul_right_inv]
end
lemma mul_right_cancel (a x y : G) (Habac : x * a = y * a) : x = y :=
calc x = x * 1 : by rw mul_one
... = x * (a * a⁻¹) : by rw mul_right_inv
... = x * a * a⁻¹ : by rw mul_assoc
... = y * a * a⁻¹ : by rw Habac
... = y * (a * a⁻¹) : by rw mul_assoc
... = y * 1 : by rw mul_right_inv
... = y : by rw mul_one
lemma mul_left_cancel_iff (a x y : G) : a * x = a * y ↔ x = y :=
begin
split,
from mul_left_cancel a x y,
intro hxy,
rwa hxy
end
lemma mul_right_cancel_iff (a x y : G) : x * a = y * a ↔ x = y :=
begin
split,
from mul_right_cancel a x y,
intro hxy,
rwa hxy
end
@[simp] lemma inv_mul_cancel_left (a b : G) : a⁻¹ * (a * b) = b :=
begin
rw ←mul_assoc, simp
end
@[simp] lemma mul_inv_cancel_left (a b : G) : a * (a⁻¹ * b) = b :=
begin
rw ←mul_assoc,
simp
end
lemma inv_mul (a b : G) : (a * b)⁻¹ = b⁻¹ * a⁻¹ :=
begin
apply mul_left_cancel (a * b),
rw mul_right_inv, simp [mul_assoc]
end
lemma one_inv : (1 : G)⁻¹ = 1 :=
by conv_rhs { rw [←(mul_left_inv (1 : G)), mul_one] }
attribute [simp] mul_left_cancel_iff mul_right_cancel_iff inv_mul inv_inv one_inv
@[simp] theorem inv_inj_iff {a b : G}: a⁻¹ = b⁻¹ ↔ a = b :=
begin
split,
{ intro h,
rw [← inv_inv a, h, inv_inv b] },
{ rintro rfl,
refl }
end
theorem inv_eq {a b : G}: a⁻¹ = b ↔ b⁻¹ = a :=
begin
split;
{ rintro rfl,
rw inv_inv }
end
theorem mul_comm {G : Type} [comm_group G] (g h : G) :
g * h = h * g := comm_group.mul_comm g h
-- **TODO** open an issue about abel only working with `*`. We
-- have `group` working but not `comm_group`. It
-- would be an interesting exercise to get `abel` working.
end group
end mygroup |
73170fcc025bfc8381d3c8598be8878de362b7bd | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/dfinsupp/lex.lean | 3f52398c4be954291b69440d7ee7ce945eac1815 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 6,910 | lean | /-
Copyright (c) 2022 Junyan Xu. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Damiano Testa, Junyan Xu
-/
import data.dfinsupp.order
import data.dfinsupp.ne_locus
import order.well_founded_set
/-!
# Lexicographic order on finitely supported dependent functions
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines the lexicographic order on `dfinsupp`.
-/
variables {ι : Type*} {α : ι → Type*}
namespace dfinsupp
section has_zero
variable [Π i, has_zero (α i)]
/-- `dfinsupp.lex r s` is the lexicographic relation on `Π₀ i, α i`, where `ι` is ordered by `r`,
and `α i` is ordered by `s i`.
The type synonym `lex (Π₀ i, α i)` has an order given by `dfinsupp.lex (<) (λ i, (<))`.
-/
protected def lex (r : ι → ι → Prop) (s : Π i, α i → α i → Prop) (x y : Π₀ i, α i) : Prop :=
pi.lex r s x y
lemma _root_.pi.lex_eq_dfinsupp_lex {r : ι → ι → Prop} {s : Π i, α i → α i → Prop}
(a b : Π₀ i, α i) : pi.lex r s (a : Π i, α i) b = dfinsupp.lex r s a b := rfl
lemma lex_def {r : ι → ι → Prop} {s : Π i, α i → α i → Prop} {a b : Π₀ i, α i} :
dfinsupp.lex r s a b ↔ ∃ j, (∀ d, r d j → a d = b d) ∧ s j (a j) (b j) := iff.rfl
instance [has_lt ι] [Π i, has_lt (α i)] : has_lt (lex (Π₀ i, α i)) :=
⟨λ f g, dfinsupp.lex (<) (λ i, (<)) (of_lex f) (of_lex g)⟩
lemma lex_lt_of_lt_of_preorder [Π i, preorder (α i)] (r) [is_strict_order ι r]
{x y : Π₀ i, α i} (hlt : x < y) : ∃ i, (∀ j, r j i → x j ≤ y j ∧ y j ≤ x j) ∧ x i < y i :=
begin
obtain ⟨hle, j, hlt⟩ := pi.lt_def.1 hlt, classical,
have : (x.ne_locus y : set ι).well_founded_on r := (x.ne_locus y).finite_to_set.well_founded_on,
obtain ⟨i, hi, hl⟩ := this.has_min {i | x i < y i} ⟨⟨j, mem_ne_locus.2 hlt.ne⟩, hlt⟩,
exact ⟨i, λ k hk, ⟨hle k, of_not_not $ λ h,
hl ⟨k, mem_ne_locus.2 (ne_of_not_le h).symm⟩ ((hle k).lt_of_not_le h) hk⟩, hi⟩,
end
lemma lex_lt_of_lt [Π i, partial_order (α i)] (r) [is_strict_order ι r]
{x y : Π₀ i, α i} (hlt : x < y) : pi.lex r (λ i, (<)) x y :=
by { simp_rw [pi.lex, le_antisymm_iff], exact lex_lt_of_lt_of_preorder r hlt }
instance lex.is_strict_order [linear_order ι] [Π i, partial_order (α i)] :
is_strict_order (lex (Π₀ i, α i)) (<) :=
let i : is_strict_order (lex (Π i, α i)) (<) := pi.lex.is_strict_order in
{ irrefl := to_lex.surjective.forall.2 $ λ a, @irrefl _ _ i.to_is_irrefl a,
trans := to_lex.surjective.forall₃.2 $ λ a b c, @trans _ _ i.to_is_trans a b c }
variables [linear_order ι]
/-- The partial order on `dfinsupp`s obtained by the lexicographic ordering.
See `dfinsupp.lex.linear_order` for a proof that this partial order is in fact linear. -/
instance lex.partial_order [Π i, partial_order (α i)] : partial_order (lex (Π₀ i, α i)) :=
partial_order.lift (λ x, to_lex ⇑(of_lex x)) dfinsupp.coe_fn_injective
section linear_order
variable [Π i, linear_order (α i)]
/-- Auxiliary helper to case split computably. There is no need for this to be public, as it
can be written with `or.by_cases` on `lt_trichotomy` once the instances below are constructed. -/
private def lt_trichotomy_rec {P : lex (Π₀ i, α i) → lex (Π₀ i, α i) → Sort*}
(h_lt : Π {f g}, to_lex f < to_lex g → P (to_lex f) (to_lex g))
(h_eq : Π {f g}, to_lex f = to_lex g → P (to_lex f) (to_lex g))
(h_gt : Π {f g}, to_lex g < to_lex f → P (to_lex f) (to_lex g)) :
Π f g, P f g :=
lex.rec $ λ f, lex.rec $ λ g,
match _, rfl : ∀ y, (f.ne_locus g).min = y → _ with
| ⊤, h := h_eq (ne_locus_eq_empty.mp $ finset.min_eq_top.mp h)
| (wit : ι), h := (mem_ne_locus.mp $ finset.mem_of_min h).lt_or_lt.by_cases
(λ hwit, h_lt ⟨wit, λ j hj, not_mem_ne_locus.mp (finset.not_mem_of_lt_min hj h), hwit⟩)
(λ hwit, h_gt ⟨wit, λ j hj, not_mem_ne_locus.mp
(finset.not_mem_of_lt_min hj $ by rwa ne_locus_comm), hwit⟩)
end
@[irreducible] instance lex.decidable_le : @decidable_rel (lex (Π₀ i, α i)) (≤) :=
lt_trichotomy_rec
(λ f g h, is_true $ or.inr h)
(λ f g h, is_true $ or.inl $ congr_arg _ h)
(λ f g h, is_false $ λ h', (lt_irrefl _ (h.trans_le h')).elim)
@[irreducible] instance lex.decidable_lt : @decidable_rel (lex (Π₀ i, α i)) (<) :=
lt_trichotomy_rec
(λ f g h, is_true h)
(λ f g h, is_false h.not_lt)
(λ f g h, is_false h.asymm)
/-- The linear order on `dfinsupp`s obtained by the lexicographic ordering. -/
instance lex.linear_order : linear_order (lex (Π₀ i, α i)) :=
{ le_total := lt_trichotomy_rec
(λ f g h, or.inl h.le)
(λ f g h, or.inl h.le)
(λ f g h, or.inr h.le),
decidable_lt := by apply_instance,
decidable_le := by apply_instance,
decidable_eq := by apply_instance,
..lex.partial_order }
end linear_order
variable [Π i, partial_order (α i)]
lemma to_lex_monotone : monotone (@to_lex (Π₀ i, α i)) :=
λ a b h, le_of_lt_or_eq $ or_iff_not_imp_right.2 $ λ hne, by classical; exact
⟨finset.min' _ (nonempty_ne_locus_iff.2 hne),
λ j hj, not_mem_ne_locus.1 (λ h, (finset.min'_le _ _ h).not_lt hj),
(h _).lt_of_ne (mem_ne_locus.1 $ finset.min'_mem _ _)⟩
lemma lt_of_forall_lt_of_lt (a b : lex (Π₀ i, α i)) (i : ι) :
(∀ j < i, of_lex a j = of_lex b j) → of_lex a i < of_lex b i → a < b :=
λ h1 h2, ⟨i, h1, h2⟩
end has_zero
section covariants
variables [linear_order ι] [Π i, add_monoid (α i)] [Π i, linear_order (α i)]
/-! We are about to sneak in a hypothesis that might appear to be too strong.
We assume `covariant_class` with *strict* inequality `<` also when proving the one with the
*weak* inequality `≤`. This is actually necessary: addition on `lex (Π₀ i, α i)` may fail to be
monotone, when it is "just" monotone on `α i`. -/
section left
variables [Π i, covariant_class (α i) (α i) (+) (<)]
instance lex.covariant_class_lt_left :
covariant_class (lex (Π₀ i, α i)) (lex (Π₀ i, α i)) (+) (<) :=
⟨λ f g h ⟨a, lta, ha⟩, ⟨a, λ j ja, congr_arg ((+) _) (lta j ja), add_lt_add_left ha _⟩⟩
instance lex.covariant_class_le_left :
covariant_class (lex (Π₀ i, α i)) (lex (Π₀ i, α i)) (+) (≤) := has_add.to_covariant_class_left _
end left
section right
variables [Π i, covariant_class (α i) (α i) (function.swap (+)) (<)]
instance lex.covariant_class_lt_right :
covariant_class (lex (Π₀ i, α i)) (lex (Π₀ i, α i)) (function.swap (+)) (<) :=
⟨λ f g h ⟨a, lta, ha⟩, ⟨a, λ j ja, congr_arg (+ (of_lex f j)) (lta j ja), add_lt_add_right ha _⟩⟩
instance lex.covariant_class_le_right :
covariant_class (lex (Π₀ i, α i)) (lex (Π₀ i, α i)) (function.swap (+)) (≤) :=
has_add.to_covariant_class_right _
end right
end covariants
end dfinsupp
|
932bc682d531b688d0ddc4ee69ed9a9d7564ed7a | e151e9053bfd6d71740066474fc500a087837323 | /src/hott/init/meta/instances.lean | b6f579ee0f0020549b7fcebd8d41b9d3ac48eca4 | [
"Apache-2.0"
] | permissive | daniel-carranza/hott3 | 15bac2d90589dbb952ef15e74b2837722491963d | 913811e8a1371d3a5751d7d32ff9dec8aa6815d9 | refs/heads/master | 1,610,091,349,670 | 1,596,222,336,000 | 1,596,222,336,000 | 241,957,822 | 0 | 0 | Apache-2.0 | 1,582,222,839,000 | 1,582,222,838,000 | null | UTF-8 | Lean | false | false | 3,028 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
open lean
open lean.parser
local postfix `?`:9001 := optional
local postfix *:9001 := many
namespace tactic
/-- Reset the instance cache for the main goal. -/
meta def reset_instance_cache : tactic unit :=
unfreeze_local_instances
namespace interactive
open interactive interactive.types expr
/-- Reset the instance cache. This allows any new instances
added to the context to be used in typeclass inference. -/
meta def resetI := reset_instance_cache
/-- Unfreeze local instances, which allows us to revert
instances in the context. -/
meta def unfreezeI := tactic.unfreeze_local_instances
/-- Like `intro`, but uses the introduced variable
in typeclass inference. -/
meta def introI (p : parse ident_?) : tactic unit :=
intro p >> reset_instance_cache
/-- Like `intros`, but uses the introduced variable(s)
in typeclass inference. -/
meta def introsI (p : parse ident_*) : tactic unit :=
intros p >> reset_instance_cache
/-- Used to add typeclasses to the context so that they can
be used in typeclass inference. The syntax is the same as `have`. -/
meta def haveI (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : tactic unit :=
do h ← match h with
| none := get_unused_name "_inst"
| some a := return a
end,
«have» (some h) q₁ q₂,
match q₁ with
| none := swap >> reset_instance_cache >> swap
| some p₂ := reset_instance_cache
end
/-- Used to add typeclasses to the context so that they can
be used in typeclass inference. The syntax is the same as `let`. -/
meta def letI (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : tactic unit :=
do h ← match h with
| none := get_unused_name "_inst"
| some a := return a
end,
«let» (some h) q₁ q₂,
match q₁ with
| none := swap >> reset_instance_cache >> swap
| some p₂ := reset_instance_cache
end
/-- Like `exact`, but uses all variables in the context
for typeclass inference. -/
meta def exactI (q : parse texpr) : tactic unit :=
reset_instance_cache >> exact q
/-- Like `apply`, but uses all variables in the context
for typeclass inference. -/
meta def applyI (q : parse texpr) : tactic unit :=
reset_instance_cache >> apply q
/-- Try to close the goal using type class inference without using the cache. -/
meta def infer : tactic unit :=
reset_instance_cache >> apply_instance
/-- Like `apply`, but doesn't use type class inference. -/
meta def napply (q : parse texpr) : tactic unit :=
concat_tags (do h ← i_to_expr_for_apply q, tactic.apply h {instances := ff})
/-- Like `apply`, but doesn't use type class inference and doesn't reorder goals. -/
meta def nfapply (q : parse texpr) : tactic unit :=
concat_tags (do h ← i_to_expr_for_apply q, tactic.apply h
{instances := ff, new_goals := new_goals.all})
end interactive
end tactic
|
c74158cfd550459f59920dd04869be1778a96523 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/bad_eqns.lean | 640a59f3a0b20c7f0fdfffd784d481907e1cb54b | [
"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 | 863 | lean | open nat
definition foo1 : nat → nat
| foo1 (0 + x) := x
definition foo2 : nat → nat → nat
| foo2 0 _ := 0
| foo2 x ⌞y⌟ := x
definition foo3 : nat → nat → nat
| foo3 0 _ := 0
| foo3 ⌞x⌟ x := x
inductive tree (A : Type) :=
| node : tree_list A → tree A
| leaf : A → tree A
with tree_list :=
| nil {} : tree_list A
| cons : tree A → tree_list A → tree_list A
definition is_leaf {A : Type} : tree A → bool
with is_leaf_aux : tree_list A → bool
| is_leaf (tree.node _) := bool.ff
| is_leaf (tree.leaf _) := bool.tt
| is_leaf_aux tree_list.nil := bool.ff
| is_leaf_aux (tree_list.cons _ _) := bool.ff
definition foo : nat → nat
| foo 0 := 0
| foo (x+1) := let y := x + 2 in y * y
example : foo 5 = 36 := rfl
definition boo : nat → nat
| boo (x + 1) := boo (x + 2)
| boo 0 := 0
|
404d695f6b555bf47c8c093de3e144d52f490aab | 86f6f4f8d827a196a32bfc646234b73328aeb306 | /examples/basics/unnamed_155.lean | 20e060616be7f5d66b376dffa43b0403c6ea4150 | [] | no_license | jamescheuk91/mathematics_in_lean | 09f1f87d2b0dce53464ff0cbe592c568ff59cf5e | 4452499264e2975bca2f42565c0925506ba5dda3 | refs/heads/master | 1,679,716,410,967 | 1,613,957,947,000 | 1,613,957,947,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 182 | lean | import data.real.basic
-- BEGIN
example (a b c : ℝ) : a * (b * c) = b * (c * a) :=
begin
sorry
end
example (a b c : ℝ) : a * (b * c) = b * (a * c) :=
begin
sorry
end
-- END |
9845f3b31e91270d55cd1b50aaa1f3028bc75de0 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/run/vars_anywhere.lean | 8b1206012e25f0ae05409dd4b89d6edcdede557b | [
"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 | 107 | lean | variable {A : Type}
check @id
inductive List
| nil : List
| cons : A → List → List
check @List.cons
|
433719a35de5c6f1fa89f3b4790b14634811eb38 | 9e90bb7eb4d1bde1805f9eb6187c333fdf09588a | /src/stump/algorithm_properties.lean | 8cabf960e497ef0170d7ce516343ba8344aa912a | [
"Apache-2.0"
] | permissive | alexjbest/stump-learnable | 6311d0c3a1a1a0e65ce83edcbb3b4b7cecabb851 | f8fd812fc646d2ece312ff6ffc2a19848ac76032 | refs/heads/master | 1,659,486,805,691 | 1,590,454,024,000 | 1,590,454,024,000 | 266,173,720 | 0 | 0 | Apache-2.0 | 1,590,169,884,000 | 1,590,169,883,000 | null | UTF-8 | Lean | false | false | 4,136 | lean | /-
Copyright © 2019, Oracle and/or its affiliates. All rights reserved.
-/
import .algorithm_definition
import .setup_properties
namespace stump
variables (μ: probability_measure ℍ) (target: ℍ) (n: ℕ)
lemma label_sample_correct_2:
∀ n, ∀ v: vec ℍ n, ∀ i: dfin (nat.succ n), (kth_projn (label_sample target n v) i).snd = tt ↔ (kth_projn (label_sample target n v) i).fst ≤ target :=
begin
intros,
dunfold label_sample,
rw ← kth_projn_map_comm,
apply label_correct,
end
lemma filter_prop_1:
∀ n, ∀ v: vec ℍ n, ∀ i: dfin (nat.succ n),
kth_projn (filter n (label_sample target n v)) i ≤ target :=
begin
intros,
unfold filter,
rw ← kth_projn_map_comm,
by_cases ((kth_projn (label_sample target n v) i).snd = tt),
{
rw h,
simp,
have LBLCO := label_sample_correct_2 target n v i,
cases LBLCO,
apply LBLCO_mp,
assumption,
},
{
simp at h,
rw h,
simp,
}
end
lemma max_prop_1:
∀ n, ∀ v: vec ℍ n, ∀ i: dfin (nat.succ n), kth_projn v i ≤ max n v :=
begin
intro,
induction n; intros,
{
unfold max,
cases i,
tidy,
},
{
cases v,
unfold max, simp,
cases i,
{
simp,
by_cases (rlt (max n_n v_snd) v_fst = tt),
{
rw h,
simp,
},
{
simp at h,
rw h,
simp,
unfold rlt at h,
cases v_fst, simp at *, assumption,
},
},
{
have IH := n_ih v_snd i_a,
simp,
by_cases (rlt (max n_n v_snd) v_fst = tt),
{
rw h,
simp, unfold rlt at h,
transitivity (max n_n v_snd),
assumption, cases v_fst, simp at *, apply le_of_lt, assumption,
},
{
simp at h,
rw h,
simp,
assumption,
}
}
}
end
lemma max_prop_2:
∀ n, ∀ v: vec ℍ n, ∃ i: dfin (nat.succ n), kth_projn v i = max n v :=
begin
intro,
induction n; intros,
{
unfold max,
cases v,
existsi dfin.fz,
simp,
},{
cases v,
unfold max, simp,
by_cases (rlt (max n_n v_snd) v_fst = tt),
{
rw h, simp,
existsi dfin.fz,
simp,
},
{
have IND := n_ih v_snd,
cases IND, clear n_ih,
simp at h,
rw h,
simp,
rw ← IND_h,
existsi dfin.fs IND_w,
simp,
}
}
end
lemma choose_property_1:
∀ n: ℕ, ∀ S: vec ℍ n, choose n (label_sample target n S) ≤ target :=
begin
intros,
unfold choose,
have MP := max_prop_2 n (filter n (label_sample target n S)),
cases MP,
have FP := filter_prop_1 target n S MP_w,
rw ← MP_h,
assumption,
end
lemma choose_property_2:
∀ n: ℕ, ∀ S: vec ℍ n,
∀ i: dfin (nat.succ n),
∀ p = kth_projn (label_sample target n S) i,
p.snd = tt →
p.fst ≤ choose n (label_sample target n S) :=
begin
intros,
unfold choose,
have MP := max_prop_1 n (filter n (label_sample target n S)) i,
unfold filter at MP,
rw ← kth_projn_map_comm at MP,
rw ← H at MP,
rw a at MP,
simp at MP,
assumption,
end
lemma choose_property_3:
∀ n: ℕ, ∀ S: vec ℍ n,
∀ i: dfin (nat.succ n),
∀ p = kth_projn (label_sample target n S) i,
p.snd = tt →
p.fst ≤ target :=
begin
intros,
transitivity (choose n (label_sample target n S)),
apply choose_property_2; try {assumption},
apply choose_property_1,
end
lemma choose_property_4:
∀ n: ℕ, ∀ S: vec ℍ n,
0 ≤ choose n (label_sample target n S) :=
begin
intro,
induction n,
{
intros, simp,
},
{
intros, finish,
},
end
end stump |
e5ced43418247355837432b8d743fec095aceb2c | 947b78d97130d56365ae2ec264df196ce769371a | /tests/lean/run/array1.lean | 246e3901c8d793b558ae0e544b344692f605c337 | [
"Apache-2.0"
] | permissive | shyamalschandra/lean4 | 27044812be8698f0c79147615b1d5090b9f4b037 | 6e7a883b21eaf62831e8111b251dc9b18f40e604 | refs/heads/master | 1,671,417,126,371 | 1,601,859,995,000 | 1,601,860,020,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,389 | lean | new_frontend
#check @Array.mk
def v : Array Nat := @Array.mk Nat 10 (fun ⟨i, _⟩ => i)
def w : Array Nat :=
(mkArray 9 1).push 3
#check @Array.casesOn
def f : Fin w.sz → Nat :=
@Array.casesOn _ (fun w => Fin w.sz → Nat) w (fun _ f => f)
def arraySum (a : Array Nat) : Nat :=
a.foldl Nat.add 0
#eval mkArray 4 1
#eval Array.map (fun x => x+10) v
#eval f ⟨1, sorry⟩
#eval f ⟨9, sorry⟩
#eval (((mkArray 1 1).push 2).push 3).foldl (fun x y => x + y) 0
#eval arraySum (mkArray 10 1)
axiom axLt {a b : Nat} : a < b
#eval (mkArray 10 1).data ⟨1, axLt⟩ + 20
#eval (mkArray 10 1).data ⟨2, axLt⟩
#eval (mkArray 10 3).data ⟨2, axLt⟩
#eval #[1, 2, 3].insertAt 0 10
#eval #[1, 2, 3].insertAt 1 10
#eval #[1, 2, 3].insertAt 2 10
#eval #[1, 2, 3].insertAt 3 10
#eval #[].insertAt 0 10
def tst1 : IO Unit :=
#[1, 2, 3, 4].forRevM IO.println
#eval tst1
#eval #[1, 2].extract 0 1
#eval #[1, 2].extract 0 0
#eval #[1, 2].extract 0 2
#eval #[1, 2, 3, 4].filterMap fun x => if x % 2 == 0 then some (x + 10) else none
def tst : IO (List Nat) :=
[1, 2, 3, 4].filterMapM fun x => do
IO.println x;
(if x % 2 == 0 then pure $ some (x + 10) else pure none)
#eval tst
#eval #[1, 3, 6, 2].getMax? (fun a b => a < b)
#eval #[].getMax? (fun (a b : Nat) => a < b)
#eval #[1, 8].getMax? (fun a b => a < b)
#eval #[8, 1].getMax? (fun a b => a < b)
#eval #[1, 6, 5, 3, 8, 2, 0].partition fun x => x % 2 == 0
def check (b : Bool) : IO Unit :=
«unless» b $ throw $ IO.userError "check failed"
#eval check $ #[].isPrefixOf #[2, 3]
#eval check $ (#[] : Array Nat).isPrefixOf #[]
#eval check $ #[2, 3].isPrefixOf #[2, 3]
#eval check $ #[2, 3].isPrefixOf #[2, 3, 4, 5]
#eval check $ ! #[2, 4].isPrefixOf #[2, 3]
#eval check $ ! #[2, 3, 4].isPrefixOf #[2, 3]
#eval check $ ! #[2].isPrefixOf #[]
#eval check $ ! #[4, 3].isPrefixOf #[2, 3, 4, 5]
#eval check $ #[1, 2, 3].allDiff
#eval check $ !#[1, 2, 1, 3].allDiff
#eval check $ #[1, 2, 4, 3].allDiff
#eval check $ (#[] : Array Nat).allDiff
#eval check $ !#[1, 1].allDiff
#eval check $ !#[1, 2, 3, 4, 5, 1].allDiff
#eval check $ #[1, 2, 3, 4, 5].allDiff
#eval check $ !#[1, 2, 3, 4, 5, 5].allDiff
#eval check $ !#[1, 3, 3, 4, 5].allDiff
#eval check $ !#[1, 2, 3, 4, 5, 3].allDiff
#eval check $ !#[1, 2, 3, 4, 5, 4].allDiff
#eval check $ #[1, 2, 3, 4, 5, 6].allDiff
#eval check $ Array.zip #[1, 2] #[3, 4, 6] == #[(1, 3), (2, 4)]
|
2b2091bc732a8fe1619a73591df3e7ee7691a0a3 | 9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e | /src/combinatorics/falling.lean | da3d093a75cf4e3ab5ed1dd7c832e6680d39c2b5 | [] | no_license | agusakov/lean_lib | c0e9cc29fc7d2518004e224376adeb5e69b5cc1a | f88d162da2f990b87c4d34f5f46bbca2bbc5948e | refs/heads/master | 1,642,141,461,087 | 1,557,395,798,000 | 1,557,395,798,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,929 | lean | /-
Copyright (c) 2019 Neil Strickland. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Neil Strickland
This is about the function falling n k = n (n - 1) ... (n - k + 1)
Note that the above formula involves subtraction but we give a
different recursive definition that only involves addition and
so works more smoothly as a map ℕ → ℕ → ℕ.
-/
import data.nat.choose
open nat
def falling : ℕ → ℕ → ℕ
| 0 0 := 1
| 0 (k + 1) := 0
| (n + 1) 0 := 1
| (n + 1) (k + 1) := (n + 1) * falling n k
@[simp]
lemma falling_zero (n : ℕ) : falling n 0 = 1 := begin
cases n; refl,
end
@[simp]
lemma falling_succ (n k : ℕ) : falling (n + 1) (k + 1) =
(n + 1) * falling n k := rfl
@[simp]
lemma falling_zero_succ (k : ℕ) : falling 0 (k + 1) = 0 := rfl
/- falling n n = n! -/
lemma falling_fact : ∀ n : ℕ, falling n n = fact n
| 0 := rfl
| (n + 1) := by {rw[falling,fact,falling_fact n]}
/- falling p n = p ! / (p - n)! for p ≥ n.
We formalise this with p = n + m rather than with an inequality.
-/
lemma falling_fact_quot : ∀ n m : ℕ, (falling (n + m) n) * fact m = fact (n + m)
| n 0 := by {rw[add_zero,fact,mul_one,falling_fact n]}
| 0 (m + 1) := by {rw[zero_add,falling,one_mul]}
| (n + 1) (m + 1) := calc
falling ((n + 1) + (m + 1)) (n + 1) * fact (m + 1) =
((n + 1) + (m + 1)) * (falling ((n + 1) + m) n) * fact (m + 1) : rfl
... = ((n + 1) + (m + 1)) * ((falling (n + (m + 1)) n) * fact (m + 1)) :
by rw[mul_assoc,add_assoc n 1 m,add_comm 1 m]
... = ((n + 1) + (m + 1)) * fact (n + (m + 1)) :
by {rw[falling_fact_quot n (m + 1)]}
... = (n + (m + 1) + 1) * fact (n + (m + 1)) :
by {rw[add_assoc n 1 (m + 1),add_comm 1 (m + 1),← add_assoc n (m + 1) 1]}
... = fact (n + (m + 1) + 1) : rfl
... = fact ((n + 1) + (m + 1)) :
by {rw[add_assoc n (m + 1) 1,add_assoc n 1 (m + 1),add_comm (m + 1) 1],}
|
bb36b5b3b32637d9e822c9d46cd10b0d4e90ffe9 | 31f556cdeb9239ffc2fad8f905e33987ff4feab9 | /tests/lean/run/KyleAlg.lean | 10fb741b21ee2da1be5c1114379d5110bcf69cba | [
"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 | 8,442 | lean | import Lean
/- from core:
class OfNat (α : Type u) (n : Nat) where
ofNat : α
class Mul (α : Type u) where
mul : α → α → α
class Add (α : Type u) where
add : α → α → α
-/
class Inv (α : Type u) where
inv : α → α
postfix:max "⁻¹" => Inv.inv
class One (α : Type u) where
one : α
export One (one)
instance [One α] : OfNat α (nat_lit 1) where
ofNat := one
class Zero (α : Type u) where
zero : α
export Zero (zero)
instance [Zero α] : OfNat α (nat_lit 0) where
ofNat := zero
class MulComm (α : Type u) [Mul α] : Prop where
mulComm : {a b : α} → a * b = b * a
export MulComm (mulComm)
class MulAssoc (α : Type u) [Mul α] : Prop where
mulAssoc : {a b c : α} → a * b * c = a * (b * c)
export MulAssoc (mulAssoc)
class OneUnit (α : Type u) [Mul α] [One α] : Prop where
oneMul : {a : α} → 1 * a = a
mulOne : {a : α} → a * 1 = a
export OneUnit (oneMul mulOne)
class AddComm (α : Type u) [Add α] : Prop where
addComm : {a b : α} → a + b = b + a
export AddComm (addComm)
class AddAssoc (α : Type u) [Add α] : Prop where
addAssoc : {a b c : α} → a + b + c = a + (b + c)
export AddAssoc (addAssoc)
class ZeroUnit (α : Type u) [Add α] [Zero α] : Prop where
zeroAdd : {a : α} → 0 + a = a
addZero : {a : α} → a + 0 = a
export ZeroUnit (zeroAdd addZero)
class InvMul (α : Type u) [Mul α] [One α] [Inv α] : Prop where
invMul : {a : α} → a⁻¹ * a = 1
export InvMul (invMul)
class NegAdd (α : Type u) [Add α] [Zero α] [Neg α] : Prop where
negAdd : {a : α} → -a + a = 0
export NegAdd (negAdd)
class ZeroMul (α : Type u) [Mul α] [Zero α] : Prop where
zeroMul : {a : α} → 0 * a = 0
export ZeroMul (zeroMul)
class Distrib (α : Type u) [Add α] [Mul α] : Prop where
leftDistrib : {a b c : α} → a * (b + c) = a * b + a * c
rightDistrib : {a b c : α} → (a + b) * c = a * c + b * c
export Distrib (leftDistrib rightDistrib)
class Domain (α : Type u) [Mul α] [Zero α] : Prop where
eqZeroOrEqZeroOfMulEqZero : {a b : α} → a * b = 0 → a = 0 ∨ b = 0
export Domain (eqZeroOrEqZeroOfMulEqZero)
class Semigroup (α : Type u) extends Mul α, MulAssoc α
attribute [instance] Semigroup.mk
class AddSemigroup (α : Type u) extends Add α, AddAssoc α
attribute [instance] AddSemigroup.mk
class Monoid (α : Type u) extends Semigroup α, One α, OneUnit α
attribute [instance] Monoid.mk
class AddMonoid (α : Type u) extends AddSemigroup α, Zero α, ZeroUnit α
attribute [instance] AddMonoid.mk
class CommSemigroup (α : Type u) extends Semigroup α, MulComm α
attribute [instance] CommSemigroup.mk
class CommMonoid (α : Type u) extends Monoid α, MulComm α
attribute [instance] CommMonoid.mk
class Group (α : Type u) extends Monoid α, Inv α, InvMul α
attribute [instance] Group.mk
class AddGroup (α : Type u) extends AddMonoid α, Neg α, NegAdd α
attribute [instance] AddGroup.mk
class Semiring (α : Type u) extends AddMonoid α, Monoid α, AddComm α, ZeroMul α, Distrib α
attribute [instance] Semiring.mk
class Ring (α : Type u) extends AddGroup α, Monoid α, AddComm α, Distrib α
attribute [instance] Ring.mk
class CommRing (α : Type u) extends Ring α, MulComm α
attribute [instance] CommRing.mk
class IntegralDomain (α : Type u) extends CommRing α, Domain α
attribute [instance] IntegralDomain.mk
section test1
variable (α : Type u) [h : CommMonoid α]
example : Semigroup α := inferInstance
example : Monoid α := inferInstance
example : CommSemigroup α := inferInstance
end test1
section test2
variable (β : Type u) [CommSemigroup β] [One β] [OneUnit β]
example : Monoid β := inferInstance
example : CommMonoid β := inferInstance
example : Semigroup β := inferInstance
end test2
section test3
variable (β : Type u) [Mul β] [One β] [MulAssoc β] [OneUnit β]
example : Monoid β := inferInstance
example : Semigroup β := inferInstance
end test3
theorem negZero [AddGroup α] : -(0 : α) = 0 := by
rw [←addZero (a := -(0 : α)), negAdd]
theorem subZero [AddGroup α] {a : α} : a + -(0 : α) = a := by
rw [←addZero (a := a), addAssoc, negZero, addZero]
theorem negNeg [AddGroup α] {a : α} : -(-a) = a := by {
rw [←addZero (a := - -a)];
rw [←negAdd (a := a)];
rw [←addAssoc];
rw [negAdd];
rw [zeroAdd];
}
theorem addNeg [AddGroup α] {a : α} : a + -a = 0 := by {
have h : - -a + -a = 0 := by { rw [negAdd] };
rw [negNeg] at h;
exact h
}
theorem addIdemIffZero [AddGroup α] {a : α} : a + a = a ↔ a = 0 := by
apply Iff.intro
focus
intro h
have h' := congrArg (λ x => x + -a) h
simp at h'
rw [addAssoc, addNeg, addZero] at h'
exact h'
focus
intro h
subst a
rw [addZero]
instance [Ring α] : ZeroMul α := by {
apply ZeroMul.mk;
intro a;
have h : 0 * a + 0 * a = 0 * a := by { rw [←rightDistrib, addZero] };
rw [addIdemIffZero (a := 0 * a)] at h;
rw [h];
}
example [Ring α] : Semiring α := inferInstance
section prod
instance [Mul α] [Mul β] : Mul (α × β) where
mul p p' := (p.1 * p'.1, p.2 * p'.2)
instance [Inv α] [Inv β] : Inv (α × β) where
inv p := (p.1⁻¹, p.2⁻¹)
instance [One α] [One β] : One (α × β) where
one := (1, 1)
theorem Product.ext : {p q : α × β} → p.1 = q.1 → p.2 = q.2 → p = q
| (a, b), (c, d) => by simp_all
instance [Semigroup α] [Semigroup β] : Semigroup (α × β) where
mulAssoc := by
intros
apply Product.ext
apply mulAssoc
apply mulAssoc
instance [Monoid α] [Monoid β] : Monoid (α × β) where
oneMul := by
intros
apply Product.ext
apply oneMul
apply oneMul
mulOne := by
intros
apply Product.ext
apply mulOne
apply mulOne
instance [Group α] [Group β] : Group (α × β) where
invMul := by
intros
apply Product.ext
apply invMul
apply invMul
end prod
def test1 {G : Type _} [Group G]: Group (G) := inferInstance
def test2 {G : Type _} [Group G]: Group (G × G) := inferInstance
def test3 {G : Type _} [Group G]: Group (G × G × G) := inferInstance
def test4 {G : Type _} [Group G]: Group (G × G × G × G) := inferInstance
def test5 {G : Type _} [Group G]: Group (G × G × G × G × G) := inferInstance
def test6 {G : Type _} [Group G]: Group (G × G × G × G × G × G) := inferInstance
def test7 {G : Type _} [Group G]: Group (G × G × G × G × G × G × G) := inferInstance
def test8 {G : Type _} [Group G]: Group (G × G × G × G × G × G × G × G) := inferInstance
namespace Lean
unsafe def Expr.dagSizeUnsafe (e : Expr) : IO Nat := do
let (_, s) ← visit e |>.run ({}, 0)
return s.2
where
visit (e : Expr) : StateRefT (HashSet USize × Nat) IO Unit := do
let addr := ptrAddrUnsafe e
unless (← get).1.contains addr do
modify fun (s, c) => (s.insert addr, c+1)
match e with
| Expr.proj _ _ s => visit s
| Expr.forallE _ d b _ => visit d; visit b
| Expr.lam _ d b _ => visit d; visit b
| Expr.letE _ t v b _ => visit t; visit v; visit b
| Expr.app f a => visit f; visit a
| Expr.mdata _ b => visit b
| _ => return ()
@[implementedBy Expr.dagSizeUnsafe]
opaque Expr.dagSize (e : Expr) : IO Nat
def getDeclTypeValueDagSize (declName : Name) : CoreM Nat := do
let info ← getConstInfo declName
let n ← info.type.dagSize
match info.value? with
| none => return n
| some v => return n + (← v.dagSize)
#eval getDeclTypeValueDagSize `test2
#eval getDeclTypeValueDagSize `test3
#eval getDeclTypeValueDagSize `test4
#eval getDeclTypeValueDagSize `test5
#eval getDeclTypeValueDagSize `test6
#eval getDeclTypeValueDagSize `test7
#eval getDeclTypeValueDagSize `test8
def reduceAndGetDagSize (declName : Name) : MetaM Nat := do
let c := mkConst declName [levelOne]
let e ← Meta.reduce c
trace[Meta.debug] "{e}"
e.dagSize
#eval reduceAndGetDagSize `test1
#eval reduceAndGetDagSize `test2
#eval reduceAndGetDagSize `test3
#eval reduceAndGetDagSize `test4
#eval reduceAndGetDagSize `test5
#eval reduceAndGetDagSize `test6
#eval reduceAndGetDagSize `test7
-- Use `set_option` to trace the reduced term
set_option pp.all true in
set_option trace.Meta.debug true in
#eval reduceAndGetDagSize `test8
|
1460faccb9fcef081ae5bd7d9db4e15358a744c5 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/1020.lean | 8fbea6469711da5fed7df0a80e548ad33f690726 | [
"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 | 684 | lean | def allPairs (xs : List α) (ys : List β) : List (α × β) :=
let rec aux
| [], r => r
| x::xs, r =>
let rec aux₂
| [], r => r
| y::ys, r => (x, y) :: r
aux₂ ys (aux xs r)
aux xs []
def allPairsFixed (xs : List α) (ys : List β) : List (α × β) :=
let rec aux
| [], r => r
| x::xs, r =>
let rec aux₂
| [], r => r
| y::ys, r => aux₂ ys ((x, y) :: r)
aux₂ ys (aux xs r)
aux xs []
#eval allPairsFixed [1, 2, 3] ['a', 'b']
example : (allPairsFixed [1, 2, 3] ['a', 'b']) = [(1, 'b'), (1, 'a'), (2, 'b'), (2, 'a'), (3, 'b'), (3, 'a')] :=
rfl
example : (allPairsFixed (List.iota 3) (List.iota 4) |>.length) = 12 :=
rfl
|
e6bc2ed8d8fae50ac5bfc19007396c7439c6c86e | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/analysis/calculus/inverse.lean | f84292e53e6335d8ff6a69b6a92c1d55f1ddb432 | [
"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 | 37,385 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov, Heather Macbeth, Sébastien Gouëzel
-/
import analysis.calculus.cont_diff
import analysis.normed_space.banach
/-!
# Inverse function theorem
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we prove the inverse function theorem. It says that if a map `f : E → F`
has an invertible strict derivative `f'` at `a`, then it is locally invertible,
and the inverse function has derivative `f' ⁻¹`.
We define `has_strict_deriv_at.to_local_homeomorph` that repacks a function `f`
with a `hf : has_strict_fderiv_at f f' a`, `f' : E ≃L[𝕜] F`, into a `local_homeomorph`.
The `to_fun` of this `local_homeomorph` is `defeq` to `f`, so one can apply theorems
about `local_homeomorph` to `hf.to_local_homeomorph f`, and get statements about `f`.
Then we define `has_strict_fderiv_at.local_inverse` to be the `inv_fun` of this `local_homeomorph`,
and prove two versions of the inverse function theorem:
* `has_strict_fderiv_at.to_local_inverse`: if `f` has an invertible derivative `f'` at `a` in the
strict sense (`hf`), then `hf.local_inverse f f' a` has derivative `f'.symm` at `f a` in the
strict sense;
* `has_strict_fderiv_at.to_local_left_inverse`: if `f` has an invertible derivative `f'` at `a` in
the strict sense and `g` is locally left inverse to `f` near `a`, then `g` has derivative
`f'.symm` at `f a` in the strict sense.
In the one-dimensional case we reformulate these theorems in terms of `has_strict_deriv_at` and
`f'⁻¹`.
We also reformulate the theorems in terms of `cont_diff`, to give that `C^k` (respectively,
smooth) inputs give `C^k` (smooth) inverses. These versions require that continuous
differentiability implies strict differentiability; this is false over a general field, true over
`ℝ` or `ℂ` and implemented here assuming `is_R_or_C 𝕂`.
Some related theorems, providing the derivative and higher regularity assuming that we already know
the inverse function, are formulated in `fderiv.lean`, `deriv.lean`, and `cont_diff.lean`.
## Notations
In the section about `approximates_linear_on` we introduce some `local notation` to make formulas
shorter:
* by `N` we denote `‖f'⁻¹‖`;
* by `g` we denote the auxiliary contracting map `x ↦ x + f'.symm (y - f x)` used to prove that
`{x | f x = y}` is nonempty.
## Tags
derivative, strictly differentiable, continuously differentiable, smooth, inverse function
-/
open function set filter metric
open_locale topology classical nnreal
noncomputable theory
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
variables {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E]
variables {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F]
variables {G : Type*} [normed_add_comm_group G] [normed_space 𝕜 G]
variables {G' : Type*} [normed_add_comm_group G'] [normed_space 𝕜 G']
variables {ε : ℝ}
open asymptotics filter metric set
open continuous_linear_map (id)
/-!
### Non-linear maps close to affine maps
In this section we study a map `f` such that `‖f x - f y - f' (x - y)‖ ≤ c * ‖x - y‖` on an open set
`s`, where `f' : E →L[𝕜] F` is a continuous linear map and `c` is suitably small. Maps of this type
behave like `f a + f' (x - a)` near each `a ∈ s`.
When `f'` is onto, we show that `f` is locally onto.
When `f'` is a continuous linear equiv, we show that `f` is a homeomorphism
between `s` and `f '' s`. More precisely, we define `approximates_linear_on.to_local_homeomorph` to
be a `local_homeomorph` with `to_fun = f`, `source = s`, and `target = f '' s`.
Maps of this type naturally appear in the proof of the inverse function theorem (see next section),
and `approximates_linear_on.to_local_homeomorph` will imply that the locally inverse function
exists.
We define this auxiliary notion to split the proof of the inverse function theorem into small
lemmas. This approach makes it possible
- to prove a lower estimate on the size of the domain of the inverse function;
- to reuse parts of the proofs in the case if a function is not strictly differentiable. E.g., for a
function `f : E × F → G` with estimates on `f x y₁ - f x y₂` but not on `f x₁ y - f x₂ y`.
-/
/-- We say that `f` approximates a continuous linear map `f'` on `s` with constant `c`,
if `‖f x - f y - f' (x - y)‖ ≤ c * ‖x - y‖` whenever `x, y ∈ s`.
This predicate is defined to facilitate the splitting of the inverse function theorem into small
lemmas. Some of these lemmas can be useful, e.g., to prove that the inverse function is defined
on a specific set. -/
def approximates_linear_on (f : E → F) (f' : E →L[𝕜] F) (s : set E) (c : ℝ≥0) : Prop :=
∀ (x ∈ s) (y ∈ s), ‖f x - f y - f' (x - y)‖ ≤ c * ‖x - y‖
@[simp] lemma approximates_linear_on_empty (f : E → F) (f' : E →L[𝕜] F) (c : ℝ≥0) :
approximates_linear_on f f' ∅ c :=
by simp [approximates_linear_on]
namespace approximates_linear_on
variables [cs : complete_space E] {f : E → F}
/-! First we prove some properties of a function that `approximates_linear_on` a (not necessarily
invertible) continuous linear map. -/
section
variables {f' : E →L[𝕜] F} {s t : set E} {c c' : ℝ≥0}
theorem mono_num (hc : c ≤ c') (hf : approximates_linear_on f f' s c) :
approximates_linear_on f f' s c' :=
λ x hx y hy, le_trans (hf x hx y hy) (mul_le_mul_of_nonneg_right hc $ norm_nonneg _)
theorem mono_set (hst : s ⊆ t) (hf : approximates_linear_on f f' t c) :
approximates_linear_on f f' s c :=
λ x hx y hy, hf x (hst hx) y (hst hy)
lemma approximates_linear_on_iff_lipschitz_on_with
{f : E → F} {f' : E →L[𝕜] F} {s : set E} {c : ℝ≥0} :
approximates_linear_on f f' s c ↔ lipschitz_on_with c (f - f') s :=
begin
have : ∀ x y, f x - f y - f' (x - y) = (f - f') x - (f - f') y,
{ assume x y, simp only [map_sub, pi.sub_apply], abel },
simp only [this, lipschitz_on_with_iff_norm_sub_le, approximates_linear_on],
end
alias approximates_linear_on_iff_lipschitz_on_with ↔
lipschitz_on_with _root_.lipschitz_on_with.approximates_linear_on
lemma lipschitz_sub (hf : approximates_linear_on f f' s c) :
lipschitz_with c (λ x : s, f x - f' x) :=
begin
refine lipschitz_with.of_dist_le_mul (λ x y, _),
rw [dist_eq_norm, subtype.dist_eq, dist_eq_norm],
convert hf x x.2 y y.2 using 2,
rw [f'.map_sub], abel
end
protected lemma lipschitz (hf : approximates_linear_on f f' s c) :
lipschitz_with (‖f'‖₊ + c) (s.restrict f) :=
by simpa only [restrict_apply, add_sub_cancel'_right]
using (f'.lipschitz.restrict s).add hf.lipschitz_sub
protected lemma continuous (hf : approximates_linear_on f f' s c) :
continuous (s.restrict f) :=
hf.lipschitz.continuous
protected lemma continuous_on (hf : approximates_linear_on f f' s c) :
continuous_on f s :=
continuous_on_iff_continuous_restrict.2 hf.continuous
end
section locally_onto
/-!
We prove that a function which is linearly approximated by a continuous linear map with a nonlinear
right inverse is locally onto. This will apply to the case where the approximating map is a linear
equivalence, for the local inverse theorem, but also whenever the approximating map is onto,
by Banach's open mapping theorem. -/
include cs
variables {s : set E} {c : ℝ≥0} {f' : E →L[𝕜] F}
/-- If a function is linearly approximated by a continuous linear map with a (possibly nonlinear)
right inverse, then it is locally onto: a ball of an explicit radius is included in the image
of the map. -/
theorem surj_on_closed_ball_of_nonlinear_right_inverse
(hf : approximates_linear_on f f' s c) (f'symm : f'.nonlinear_right_inverse)
{ε : ℝ} {b : E} (ε0 : 0 ≤ ε) (hε : closed_ball b ε ⊆ s) :
surj_on f (closed_ball b ε) (closed_ball (f b) (((f'symm.nnnorm : ℝ)⁻¹ - c) * ε)) :=
begin
assume y hy,
cases le_or_lt (f'symm.nnnorm : ℝ) ⁻¹ c with hc hc,
{ refine ⟨b, by simp [ε0], _⟩,
have : dist y (f b) ≤ 0 :=
(mem_closed_ball.1 hy).trans (mul_nonpos_of_nonpos_of_nonneg (by linarith) ε0),
simp only [dist_le_zero] at this,
rw this },
have If' : (0 : ℝ) < f'symm.nnnorm,
by { rw [← inv_pos], exact (nnreal.coe_nonneg _).trans_lt hc },
have Icf' : (c : ℝ) * f'symm.nnnorm < 1, by rwa [inv_eq_one_div, lt_div_iff If'] at hc,
have Jf' : (f'symm.nnnorm : ℝ) ≠ 0 := ne_of_gt If',
have Jcf' : (1 : ℝ) - c * f'symm.nnnorm ≠ 0, by { apply ne_of_gt, linarith },
/- We have to show that `y` can be written as `f x` for some `x ∈ closed_ball b ε`.
The idea of the proof is to apply the Banach contraction principle to the map
`g : x ↦ x + f'symm (y - f x)`, as a fixed point of this map satisfies `f x = y`.
When `f'symm` is a genuine linear inverse, `g` is a contracting map. In our case, since `f'symm`
is nonlinear, this map is not contracting (it is not even continuous), but still the proof of
the contraction theorem holds: `uₙ = gⁿ b` is a Cauchy sequence, converging exponentially fast
to the desired point `x`. Instead of appealing to general results, we check this by hand.
The main point is that `f (u n)` becomes exponentially close to `y`, and therefore
`dist (u (n+1)) (u n)` becomes exponentally small, making it possible to get an inductive
bound on `dist (u n) b`, from which one checks that `u n` stays in the ball on which one has a
control. Therefore, the bound can be checked at the next step, and so on inductively.
-/
set g := λ x, x + f'symm (y - f x) with hg,
set u := λ (n : ℕ), g ^[n] b with hu,
have usucc : ∀ n, u (n + 1) = g (u n), by simp [hu, ← iterate_succ_apply' g _ b],
-- First bound: if `f z` is close to `y`, then `g z` is close to `z` (i.e., almost a fixed point).
have A : ∀ z, dist (g z) z ≤ f'symm.nnnorm * dist (f z) y,
{ assume z,
rw [dist_eq_norm, hg, add_sub_cancel', dist_eq_norm'],
exact f'symm.bound _ },
-- Second bound: if `z` and `g z` are in the set with good control, then `f (g z)` becomes closer
-- to `y` than `f z` was (this uses the linear approximation property, and is the reason for the
-- choice of the formula for `g`).
have B : ∀ z ∈ closed_ball b ε, g z ∈ closed_ball b ε →
dist (f (g z)) y ≤ c * f'symm.nnnorm * dist (f z) y,
{ assume z hz hgz,
set v := f'symm (y - f z) with hv,
calc dist (f (g z)) y = ‖f (z + v) - y‖ : by rw [dist_eq_norm]
... = ‖f (z + v) - f z - f' v + f' v - (y - f z)‖ : by { congr' 1, abel }
... = ‖f (z + v) - f z - f' ((z + v) - z)‖ :
by simp only [continuous_linear_map.nonlinear_right_inverse.right_inv,
add_sub_cancel', sub_add_cancel]
... ≤ c * ‖(z + v) - z‖ : hf _ (hε hgz) _ (hε hz)
... ≤ c * (f'symm.nnnorm * dist (f z) y) : begin
apply mul_le_mul_of_nonneg_left _ (nnreal.coe_nonneg c),
simpa [hv, dist_eq_norm'] using f'symm.bound (y - f z),
end
... = c * f'symm.nnnorm * dist (f z) y : by ring },
-- Third bound: a complicated bound on `dist w b` (that will show up in the induction) is enough
-- to check that `w` is in the ball on which one has controls. Will be used to check that `u n`
-- belongs to this ball for all `n`.
have C : ∀ (n : ℕ) (w : E),
dist w b ≤ f'symm.nnnorm * (1 - (c * f'symm.nnnorm)^n) / (1 - c * f'symm.nnnorm) * dist (f b) y
→ w ∈ closed_ball b ε,
{ assume n w hw,
apply hw.trans,
rw [div_mul_eq_mul_div, div_le_iff], swap, { linarith },
calc (f'symm.nnnorm : ℝ) * (1 - (c * f'symm.nnnorm) ^ n) * dist (f b) y
= f'symm.nnnorm * dist (f b) y * (1 - (c * f'symm.nnnorm) ^ n) : by ring
... ≤ f'symm.nnnorm * dist (f b) y * 1 :
begin
apply mul_le_mul_of_nonneg_left _ (mul_nonneg (nnreal.coe_nonneg _) dist_nonneg),
rw [sub_le_self_iff],
exact pow_nonneg (mul_nonneg (nnreal.coe_nonneg _) (nnreal.coe_nonneg _)) _,
end
... ≤ f'symm.nnnorm * (((f'symm.nnnorm : ℝ)⁻¹ - c) * ε) :
by { rw [mul_one],
exact mul_le_mul_of_nonneg_left (mem_closed_ball'.1 hy) (nnreal.coe_nonneg _) }
... = ε * (1 - c * f'symm.nnnorm) : by { field_simp, ring } },
/- Main inductive control: `f (u n)` becomes exponentially close to `y`, and therefore
`dist (u (n+1)) (u n)` becomes exponentally small, making it possible to get an inductive
bound on `dist (u n) b`, from which one checks that `u n` remains in the ball on which we
have estimates. -/
have D : ∀ (n : ℕ), dist (f (u n)) y ≤ (c * f'symm.nnnorm)^n * dist (f b) y
∧ dist (u n) b ≤ f'symm.nnnorm * (1 - (c * f'symm.nnnorm)^n) / (1 - c * f'symm.nnnorm)
* dist (f b) y,
{ assume n,
induction n with n IH, { simp [hu, le_refl] },
rw usucc,
have Ign : dist (g (u n)) b ≤
f'symm.nnnorm * (1 - (c * f'symm.nnnorm)^n.succ) / (1 - c * f'symm.nnnorm) * dist (f b) y :=
calc
dist (g (u n)) b ≤ dist (g (u n)) (u n) + dist (u n) b : dist_triangle _ _ _
... ≤ f'symm.nnnorm * dist (f (u n)) y + dist (u n) b : add_le_add (A _) le_rfl
... ≤ f'symm.nnnorm * ((c * f'symm.nnnorm)^n * dist (f b) y) +
f'symm.nnnorm * (1 - (c * f'symm.nnnorm)^n) / (1 - c * f'symm.nnnorm) * dist (f b) y :
add_le_add (mul_le_mul_of_nonneg_left IH.1 (nnreal.coe_nonneg _)) IH.2
... = f'symm.nnnorm * (1 - (c * f'symm.nnnorm)^n.succ) / (1 - c * f'symm.nnnorm)
* dist (f b) y : by { field_simp [Jcf'], ring_exp },
refine ⟨_, Ign⟩,
calc dist (f (g (u n))) y ≤ c * f'symm.nnnorm * dist (f (u n)) y :
B _ (C n _ IH.2) (C n.succ _ Ign)
... ≤ (c * f'symm.nnnorm) * ((c * f'symm.nnnorm)^n * dist (f b) y) :
mul_le_mul_of_nonneg_left IH.1 (mul_nonneg (nnreal.coe_nonneg _) (nnreal.coe_nonneg _))
... = (c * f'symm.nnnorm) ^ n.succ * dist (f b) y : by ring_exp },
-- Deduce from the inductive bound that `uₙ` is a Cauchy sequence, therefore converging.
have : cauchy_seq u,
{ have : ∀ (n : ℕ), dist (u n) (u (n+1)) ≤ f'symm.nnnorm * dist (f b) y * (c * f'symm.nnnorm)^n,
{ assume n,
calc dist (u n) (u (n+1)) = dist (g (u n)) (u n) : by rw [usucc, dist_comm]
... ≤ f'symm.nnnorm * dist (f (u n)) y : A _
... ≤ f'symm.nnnorm * ((c * f'symm.nnnorm)^n * dist (f b) y) :
mul_le_mul_of_nonneg_left (D n).1 (nnreal.coe_nonneg _)
... = f'symm.nnnorm * dist (f b) y * (c * f'symm.nnnorm)^n : by ring },
exact cauchy_seq_of_le_geometric _ _ Icf' this },
obtain ⟨x, hx⟩ : ∃ x, tendsto u at_top (𝓝 x) := cauchy_seq_tendsto_of_complete this,
-- As all the `uₙ` belong to the ball `closed_ball b ε`, so does their limit `x`.
have xmem : x ∈ closed_ball b ε :=
is_closed_ball.mem_of_tendsto hx (eventually_of_forall (λ n, C n _ (D n).2)),
refine ⟨x, xmem, _⟩,
-- It remains to check that `f x = y`. This follows from continuity of `f` on `closed_ball b ε`
-- and from the fact that `f uₙ` is converging to `y` by construction.
have hx' : tendsto u at_top (𝓝[closed_ball b ε] x),
{ simp only [nhds_within, tendsto_inf, hx, true_and, ge_iff_le, tendsto_principal],
exact eventually_of_forall (λ n, C n _ (D n).2) },
have T1 : tendsto (λ n, f (u n)) at_top (𝓝 (f x)) :=
(hf.continuous_on.mono hε x xmem).tendsto.comp hx',
have T2 : tendsto (λ n, f (u n)) at_top (𝓝 y),
{ rw tendsto_iff_dist_tendsto_zero,
refine squeeze_zero (λ n, dist_nonneg) (λ n, (D n).1) _,
simpa using (tendsto_pow_at_top_nhds_0_of_lt_1
(mul_nonneg (nnreal.coe_nonneg _) (nnreal.coe_nonneg _)) Icf').mul tendsto_const_nhds },
exact tendsto_nhds_unique T1 T2,
end
lemma open_image (hf : approximates_linear_on f f' s c) (f'symm : f'.nonlinear_right_inverse)
(hs : is_open s) (hc : subsingleton F ∨ c < f'symm.nnnorm⁻¹) : is_open (f '' s) :=
begin
cases hc with hE hc, { resetI, apply is_open_discrete },
simp only [is_open_iff_mem_nhds, nhds_basis_closed_ball.mem_iff, ball_image_iff] at hs ⊢,
intros x hx,
rcases hs x hx with ⟨ε, ε0, hε⟩,
refine ⟨(f'symm.nnnorm⁻¹ - c) * ε, mul_pos (sub_pos.2 hc) ε0, _⟩,
exact (hf.surj_on_closed_ball_of_nonlinear_right_inverse f'symm (le_of_lt ε0) hε).mono
hε (subset.refl _)
end
lemma image_mem_nhds (hf : approximates_linear_on f f' s c) (f'symm : f'.nonlinear_right_inverse)
{x : E} (hs : s ∈ 𝓝 x) (hc : subsingleton F ∨ c < f'symm.nnnorm⁻¹) :
f '' s ∈ 𝓝 (f x) :=
begin
obtain ⟨t, hts, ht, xt⟩ : ∃ t ⊆ s, is_open t ∧ x ∈ t := _root_.mem_nhds_iff.1 hs,
have := is_open.mem_nhds ((hf.mono_set hts).open_image f'symm ht hc) (mem_image_of_mem _ xt),
exact mem_of_superset this (image_subset _ hts),
end
lemma map_nhds_eq (hf : approximates_linear_on f f' s c) (f'symm : f'.nonlinear_right_inverse)
{x : E} (hs : s ∈ 𝓝 x) (hc : subsingleton F ∨ c < f'symm.nnnorm⁻¹) :
map f (𝓝 x) = 𝓝 (f x) :=
begin
refine le_antisymm ((hf.continuous_on x (mem_of_mem_nhds hs)).continuous_at hs)
(le_map (λ t ht, _)),
have : f '' (s ∩ t) ∈ 𝓝 (f x) := (hf.mono_set (inter_subset_left s t)).image_mem_nhds
f'symm (inter_mem hs ht) hc,
exact mem_of_superset this (image_subset _ (inter_subset_right _ _)),
end
end locally_onto
/-!
From now on we assume that `f` approximates an invertible continuous linear map `f : E ≃L[𝕜] F`.
We also assume that either `E = {0}`, or `c < ‖f'⁻¹‖⁻¹`. We use `N` as an abbreviation for `‖f'⁻¹‖`.
-/
variables {f' : E ≃L[𝕜] F} {s : set E} {c : ℝ≥0}
local notation `N` := ‖(f'.symm : F →L[𝕜] E)‖₊
protected lemma antilipschitz (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) :
antilipschitz_with (N⁻¹ - c)⁻¹ (s.restrict f) :=
begin
cases hc with hE hc,
{ haveI : subsingleton s := ⟨λ x y, subtype.eq $ @subsingleton.elim _ hE _ _⟩,
exact antilipschitz_with.of_subsingleton },
convert (f'.antilipschitz.restrict s).add_lipschitz_with hf.lipschitz_sub hc,
simp [restrict]
end
protected lemma injective (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) :
injective (s.restrict f) :=
(hf.antilipschitz hc).injective
protected lemma inj_on (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) :
inj_on f s :=
inj_on_iff_injective.2 $ hf.injective hc
protected lemma surjective [complete_space E]
(hf : approximates_linear_on f (f' : E →L[𝕜] F) univ c) (hc : subsingleton E ∨ c < N⁻¹) :
surjective f :=
begin
cases hc with hE hc,
{ haveI : subsingleton F := (equiv.subsingleton_congr f'.to_linear_equiv.to_equiv).1 hE,
exact surjective_to_subsingleton _ },
{ apply forall_of_forall_mem_closed_ball (λ (y : F), ∃ a, f a = y) (f 0) _,
have hc' : (0 : ℝ) < N⁻¹ - c, by { rw sub_pos, exact hc },
let p : ℝ → Prop := λ R, closed_ball (f 0) R ⊆ set.range f,
have hp : ∀ᶠ (r:ℝ) in at_top, p ((N⁻¹ - c) * r),
{ have hr : ∀ᶠ (r:ℝ) in at_top, 0 ≤ r := eventually_ge_at_top 0,
refine hr.mono (λ r hr, subset.trans _ (image_subset_range f (closed_ball 0 r))),
refine hf.surj_on_closed_ball_of_nonlinear_right_inverse f'.to_nonlinear_right_inverse hr _,
exact subset_univ _ },
refine ((tendsto_id.const_mul_at_top hc').frequently hp.frequently).mono _,
exact λ R h y hy, h hy },
end
/-- A map approximating a linear equivalence on a set defines a local equivalence on this set.
Should not be used outside of this file, because it is superseded by `to_local_homeomorph` below.
This is a first step towards the inverse function. -/
def to_local_equiv (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) : local_equiv E F :=
(hf.inj_on hc).to_local_equiv _ _
/-- The inverse function is continuous on `f '' s`. Use properties of `local_homeomorph` instead. -/
lemma inverse_continuous_on (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) :
continuous_on (hf.to_local_equiv hc).symm (f '' s) :=
begin
apply continuous_on_iff_continuous_restrict.2,
refine ((hf.antilipschitz hc).to_right_inv_on' _ (hf.to_local_equiv hc).right_inv').continuous,
exact (λ x hx, (hf.to_local_equiv hc).map_target hx)
end
/-- The inverse function is approximated linearly on `f '' s` by `f'.symm`. -/
lemma to_inv (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) :
approximates_linear_on (hf.to_local_equiv hc).symm (f'.symm : F →L[𝕜] E) (f '' s)
(N * (N⁻¹ - c)⁻¹ * c) :=
begin
assume x hx y hy,
set A := hf.to_local_equiv hc with hA,
have Af : ∀ z, A z = f z := λ z, rfl,
rcases (mem_image _ _ _).1 hx with ⟨x', x's, rfl⟩,
rcases (mem_image _ _ _).1 hy with ⟨y', y's, rfl⟩,
rw [← Af x', ← Af y', A.left_inv x's, A.left_inv y's],
calc ‖x' - y' - (f'.symm) (A x' - A y')‖
≤ N * ‖f' (x' - y' - (f'.symm) (A x' - A y'))‖ :
(f' : E →L[𝕜] F).bound_of_antilipschitz f'.antilipschitz _
... = N * ‖A y' - A x' - f' (y' - x')‖ :
begin
congr' 2,
simp only [continuous_linear_equiv.apply_symm_apply, continuous_linear_equiv.map_sub],
abel,
end
... ≤ N * (c * ‖y' - x'‖) :
mul_le_mul_of_nonneg_left (hf _ y's _ x's) (nnreal.coe_nonneg _)
... ≤ N * (c * (((N⁻¹ - c)⁻¹ : ℝ≥0) * ‖A y' - A x'‖)) :
begin
apply_rules [mul_le_mul_of_nonneg_left, nnreal.coe_nonneg],
rw [← dist_eq_norm, ← dist_eq_norm],
exact (hf.antilipschitz hc).le_mul_dist ⟨y', y's⟩ ⟨x', x's⟩,
end
... = (N * (N⁻¹ - c)⁻¹ * c : ℝ≥0) * ‖A x' - A y'‖ :
by { simp only [norm_sub_rev, nonneg.coe_mul], ring }
end
include cs
section
variables (f s)
/-- Given a function `f` that approximates a linear equivalence on an open set `s`,
returns a local homeomorph with `to_fun = f` and `source = s`. -/
def to_local_homeomorph (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) (hs : is_open s) : local_homeomorph E F :=
{ to_local_equiv := hf.to_local_equiv hc,
open_source := hs,
open_target := hf.open_image f'.to_nonlinear_right_inverse hs
(by rwa f'.to_linear_equiv.to_equiv.subsingleton_congr at hc),
continuous_to_fun := hf.continuous_on,
continuous_inv_fun := hf.inverse_continuous_on hc }
/-- A function `f` that approximates a linear equivalence on the whole space is a homeomorphism. -/
def to_homeomorph (hf : approximates_linear_on f (f' : E →L[𝕜] F) univ c)
(hc : subsingleton E ∨ c < N⁻¹) :
E ≃ₜ F :=
begin
refine (hf.to_local_homeomorph _ _ hc is_open_univ).to_homeomorph_of_source_eq_univ_target_eq_univ
rfl _,
change f '' univ = univ,
rw [image_univ, range_iff_surjective],
exact hf.surjective hc,
end
omit cs
/-- In a real vector space, a function `f` that approximates a linear equivalence on a subset `s`
can be extended to a homeomorphism of the whole space. -/
lemma exists_homeomorph_extension {E : Type*} [normed_add_comm_group E] [normed_space ℝ E]
{F : Type*} [normed_add_comm_group F] [normed_space ℝ F] [finite_dimensional ℝ F]
{s : set E} {f : E → F} {f' : E ≃L[ℝ] F} {c : ℝ≥0}
(hf : approximates_linear_on f (f' : E →L[ℝ] F) s c)
(hc : subsingleton E ∨ lipschitz_extension_constant F * c < (‖(f'.symm : F →L[ℝ] E)‖₊)⁻¹) :
∃ g : E ≃ₜ F, eq_on f g s :=
begin
-- the difference `f - f'` is Lipschitz on `s`. It can be extended to a Lipschitz function `u`
-- on the whole space, with a slightly worse Lipschitz constant. Then `f' + u` will be the
-- desired homeomorphism.
obtain ⟨u, hu, uf⟩ : ∃ (u : E → F), lipschitz_with (lipschitz_extension_constant F * c) u
∧ eq_on (f - f') u s := hf.lipschitz_on_with.extend_finite_dimension,
let g : E → F := λ x, f' x + u x,
have fg : eq_on f g s := λ x hx, by simp_rw [g, ← uf hx, pi.sub_apply, add_sub_cancel'_right],
have hg : approximates_linear_on g (f' : E →L[ℝ] F) univ (lipschitz_extension_constant F * c),
{ apply lipschitz_on_with.approximates_linear_on,
rw lipschitz_on_univ,
convert hu,
ext x,
simp only [add_sub_cancel', continuous_linear_equiv.coe_coe, pi.sub_apply] },
haveI : finite_dimensional ℝ E := f'.symm.to_linear_equiv.finite_dimensional,
exact ⟨hg.to_homeomorph g hc, fg⟩,
end
end
@[simp] lemma to_local_homeomorph_coe (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) (hs : is_open s) :
(hf.to_local_homeomorph f s hc hs : E → F) = f := rfl
@[simp] lemma to_local_homeomorph_source (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) (hs : is_open s) :
(hf.to_local_homeomorph f s hc hs).source = s := rfl
@[simp] lemma to_local_homeomorph_target (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) (hs : is_open s) :
(hf.to_local_homeomorph f s hc hs).target = f '' s := rfl
lemma closed_ball_subset_target (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) (hs : is_open s) {b : E} (ε0 : 0 ≤ ε) (hε : closed_ball b ε ⊆ s) :
closed_ball (f b) ((N⁻¹ - c) * ε) ⊆ (hf.to_local_homeomorph f s hc hs).target :=
(hf.surj_on_closed_ball_of_nonlinear_right_inverse f'.to_nonlinear_right_inverse
ε0 hε).mono hε (subset.refl _)
end approximates_linear_on
/-!
### Inverse function theorem
Now we prove the inverse function theorem. Let `f : E → F` be a map defined on a complete vector
space `E`. Assume that `f` has an invertible derivative `f' : E ≃L[𝕜] F` at `a : E` in the strict
sense. Then `f` approximates `f'` in the sense of `approximates_linear_on` on an open neighborhood
of `a`, and we can apply `approximates_linear_on.to_local_homeomorph` to construct the inverse
function. -/
namespace has_strict_fderiv_at
/-- If `f` has derivative `f'` at `a` in the strict sense and `c > 0`, then `f` approximates `f'`
with constant `c` on some neighborhood of `a`. -/
lemma approximates_deriv_on_nhds {f : E → F} {f' : E →L[𝕜] F} {a : E}
(hf : has_strict_fderiv_at f f' a) {c : ℝ≥0} (hc : subsingleton E ∨ 0 < c) :
∃ s ∈ 𝓝 a, approximates_linear_on f f' s c :=
begin
cases hc with hE hc,
{ refine ⟨univ, is_open.mem_nhds is_open_univ trivial, λ x hx y hy, _⟩,
simp [@subsingleton.elim E hE x y] },
have := hf.def hc,
rw [nhds_prod_eq, filter.eventually, mem_prod_same_iff] at this,
rcases this with ⟨s, has, hs⟩,
exact ⟨s, has, λ x hx y hy, hs (mk_mem_prod hx hy)⟩
end
lemma map_nhds_eq_of_surj [complete_space E] [complete_space F]
{f : E → F} {f' : E →L[𝕜] F} {a : E}
(hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) (h : linear_map.range f' = ⊤) :
map f (𝓝 a) = 𝓝 (f a) :=
begin
let f'symm := f'.nonlinear_right_inverse_of_surjective h,
set c : ℝ≥0 := f'symm.nnnorm⁻¹ / 2 with hc,
have f'symm_pos : 0 < f'symm.nnnorm := f'.nonlinear_right_inverse_of_surjective_nnnorm_pos h,
have cpos : 0 < c, by simp [hc, half_pos, inv_pos, f'symm_pos],
obtain ⟨s, s_nhds, hs⟩ : ∃ s ∈ 𝓝 a, approximates_linear_on f f' s c :=
hf.approximates_deriv_on_nhds (or.inr cpos),
apply hs.map_nhds_eq f'symm s_nhds (or.inr (nnreal.half_lt_self _)),
simp [ne_of_gt f'symm_pos],
end
variables [cs : complete_space E] {f : E → F} {f' : E ≃L[𝕜] F} {a : E}
lemma approximates_deriv_on_open_nhds (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
∃ (s : set E) (hs : a ∈ s ∧ is_open s),
approximates_linear_on f (f' : E →L[𝕜] F) s (‖(f'.symm : F →L[𝕜] E)‖₊⁻¹ / 2) :=
begin
refine ((nhds_basis_opens a).exists_iff _).1 _,
exact (λ s t, approximates_linear_on.mono_set),
exact (hf.approximates_deriv_on_nhds $ f'.subsingleton_or_nnnorm_symm_pos.imp id $
λ hf', half_pos $ inv_pos.2 hf')
end
include cs
variable (f)
/-- Given a function with an invertible strict derivative at `a`, returns a `local_homeomorph`
with `to_fun = f` and `a ∈ source`. This is a part of the inverse function theorem.
The other part `has_strict_fderiv_at.to_local_inverse` states that the inverse function
of this `local_homeomorph` has derivative `f'.symm`. -/
def to_local_homeomorph (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) : local_homeomorph E F :=
approximates_linear_on.to_local_homeomorph f
(classical.some hf.approximates_deriv_on_open_nhds)
(classical.some_spec hf.approximates_deriv_on_open_nhds).snd
(f'.subsingleton_or_nnnorm_symm_pos.imp id $ λ hf', nnreal.half_lt_self $ ne_of_gt $
inv_pos.2 hf')
(classical.some_spec hf.approximates_deriv_on_open_nhds).fst.2
variable {f}
@[simp] lemma to_local_homeomorph_coe (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
(hf.to_local_homeomorph f : E → F) = f := rfl
lemma mem_to_local_homeomorph_source (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
a ∈ (hf.to_local_homeomorph f).source :=
(classical.some_spec hf.approximates_deriv_on_open_nhds).fst.1
lemma image_mem_to_local_homeomorph_target (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
f a ∈ (hf.to_local_homeomorph f).target :=
(hf.to_local_homeomorph f).map_source hf.mem_to_local_homeomorph_source
lemma map_nhds_eq_of_equiv (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
map f (𝓝 a) = 𝓝 (f a) :=
(hf.to_local_homeomorph f).map_nhds_eq hf.mem_to_local_homeomorph_source
variables (f f' a)
/-- Given a function `f` with an invertible derivative, returns a function that is locally inverse
to `f`. -/
def local_inverse (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) : F → E :=
(hf.to_local_homeomorph f).symm
variables {f f' a}
lemma local_inverse_def (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
hf.local_inverse f _ _ = (hf.to_local_homeomorph f).symm :=
rfl
lemma eventually_left_inverse (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
∀ᶠ x in 𝓝 a, hf.local_inverse f f' a (f x) = x :=
(hf.to_local_homeomorph f).eventually_left_inverse hf.mem_to_local_homeomorph_source
@[simp] lemma local_inverse_apply_image (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
hf.local_inverse f f' a (f a) = a :=
hf.eventually_left_inverse.self_of_nhds
lemma eventually_right_inverse (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
∀ᶠ y in 𝓝 (f a), f (hf.local_inverse f f' a y) = y :=
(hf.to_local_homeomorph f).eventually_right_inverse' hf.mem_to_local_homeomorph_source
lemma local_inverse_continuous_at (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
continuous_at (hf.local_inverse f f' a) (f a) :=
(hf.to_local_homeomorph f).continuous_at_symm hf.image_mem_to_local_homeomorph_target
lemma local_inverse_tendsto (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
tendsto (hf.local_inverse f f' a) (𝓝 $ f a) (𝓝 a) :=
(hf.to_local_homeomorph f).tendsto_symm hf.mem_to_local_homeomorph_source
lemma local_inverse_unique (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) {g : F → E}
(hg : ∀ᶠ x in 𝓝 a, g (f x) = x) :
∀ᶠ y in 𝓝 (f a), g y = local_inverse f f' a hf y :=
eventually_eq_of_left_inv_of_right_inv hg hf.eventually_right_inverse $
(hf.to_local_homeomorph f).tendsto_symm hf.mem_to_local_homeomorph_source
/-- If `f` has an invertible derivative `f'` at `a` in the sense of strict differentiability `(hf)`,
then the inverse function `hf.local_inverse f` has derivative `f'.symm` at `f a`. -/
theorem to_local_inverse (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
has_strict_fderiv_at (hf.local_inverse f f' a) (f'.symm : F →L[𝕜] E) (f a) :=
(hf.to_local_homeomorph f).has_strict_fderiv_at_symm hf.image_mem_to_local_homeomorph_target $
by simpa [← local_inverse_def] using hf
/-- If `f : E → F` has an invertible derivative `f'` at `a` in the sense of strict differentiability
and `g (f x) = x` in a neighborhood of `a`, then `g` has derivative `f'.symm` at `f a`.
For a version assuming `f (g y) = y` and continuity of `g` at `f a` but not `[complete_space E]`
see `of_local_left_inverse`. -/
theorem to_local_left_inverse (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) {g : F → E}
(hg : ∀ᶠ x in 𝓝 a, g (f x) = x) :
has_strict_fderiv_at g (f'.symm : F →L[𝕜] E) (f a) :=
hf.to_local_inverse.congr_of_eventually_eq $ (hf.local_inverse_unique hg).mono $ λ _, eq.symm
end has_strict_fderiv_at
/-- If a function has an invertible strict derivative at all points, then it is an open map. -/
lemma open_map_of_strict_fderiv_equiv [complete_space E] {f : E → F} {f' : E → E ≃L[𝕜] F}
(hf : ∀ x, has_strict_fderiv_at f (f' x : E →L[𝕜] F) x) :
is_open_map f :=
is_open_map_iff_nhds_le.2 $ λ x, (hf x).map_nhds_eq_of_equiv.ge
/-!
### Inverse function theorem, 1D case
In this case we prove a version of the inverse function theorem for maps `f : 𝕜 → 𝕜`.
We use `continuous_linear_equiv.units_equiv_aut` to translate `has_strict_deriv_at f f' a` and
`f' ≠ 0` into `has_strict_fderiv_at f (_ : 𝕜 ≃L[𝕜] 𝕜) a`.
-/
namespace has_strict_deriv_at
variables [cs : complete_space 𝕜] {f : 𝕜 → 𝕜} {f' a : 𝕜} (hf : has_strict_deriv_at f f' a)
(hf' : f' ≠ 0)
include cs
variables (f f' a)
/-- A function that is inverse to `f` near `a`. -/
@[reducible] def local_inverse : 𝕜 → 𝕜 :=
(hf.has_strict_fderiv_at_equiv hf').local_inverse _ _ _
variables {f f' a}
lemma map_nhds_eq : map f (𝓝 a) = 𝓝 (f a) :=
(hf.has_strict_fderiv_at_equiv hf').map_nhds_eq_of_equiv
theorem to_local_inverse : has_strict_deriv_at (hf.local_inverse f f' a hf') f'⁻¹ (f a) :=
(hf.has_strict_fderiv_at_equiv hf').to_local_inverse
theorem to_local_left_inverse {g : 𝕜 → 𝕜} (hg : ∀ᶠ x in 𝓝 a, g (f x) = x) :
has_strict_deriv_at g f'⁻¹ (f a) :=
(hf.has_strict_fderiv_at_equiv hf').to_local_left_inverse hg
end has_strict_deriv_at
/-- If a function has a non-zero strict derivative at all points, then it is an open map. -/
lemma open_map_of_strict_deriv [complete_space 𝕜] {f f' : 𝕜 → 𝕜}
(hf : ∀ x, has_strict_deriv_at f (f' x) x) (h0 : ∀ x, f' x ≠ 0) :
is_open_map f :=
is_open_map_iff_nhds_le.2 $ λ x, ((hf x).map_nhds_eq (h0 x)).ge
/-!
### Inverse function theorem, smooth case
-/
namespace cont_diff_at
variables {𝕂 : Type*} [is_R_or_C 𝕂]
variables {E' : Type*} [normed_add_comm_group E'] [normed_space 𝕂 E']
variables {F' : Type*} [normed_add_comm_group F'] [normed_space 𝕂 F']
variables [complete_space E'] (f : E' → F') {f' : E' ≃L[𝕂] F'} {a : E'}
/-- Given a `cont_diff` function over `𝕂` (which is `ℝ` or `ℂ`) with an invertible
derivative at `a`, returns a `local_homeomorph` with `to_fun = f` and `a ∈ source`. -/
def to_local_homeomorph
{n : ℕ∞} (hf : cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (f' : E' →L[𝕂] F') a)
(hn : 1 ≤ n) :
local_homeomorph E' F' :=
(hf.has_strict_fderiv_at' hf' hn).to_local_homeomorph f
variable {f}
@[simp] lemma to_local_homeomorph_coe
{n : ℕ∞} (hf : cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (f' : E' →L[𝕂] F') a)
(hn : 1 ≤ n) :
(hf.to_local_homeomorph f hf' hn : E' → F') = f := rfl
lemma mem_to_local_homeomorph_source
{n : ℕ∞} (hf : cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (f' : E' →L[𝕂] F') a)
(hn : 1 ≤ n) :
a ∈ (hf.to_local_homeomorph f hf' hn).source :=
(hf.has_strict_fderiv_at' hf' hn).mem_to_local_homeomorph_source
lemma image_mem_to_local_homeomorph_target
{n : ℕ∞} (hf : cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (f' : E' →L[𝕂] F') a)
(hn : 1 ≤ n) :
f a ∈ (hf.to_local_homeomorph f hf' hn).target :=
(hf.has_strict_fderiv_at' hf' hn).image_mem_to_local_homeomorph_target
/-- Given a `cont_diff` function over `𝕂` (which is `ℝ` or `ℂ`) with an invertible derivative
at `a`, returns a function that is locally inverse to `f`. -/
def local_inverse
{n : ℕ∞} (hf : cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (f' : E' →L[𝕂] F') a)
(hn : 1 ≤ n) :
F' → E' :=
(hf.has_strict_fderiv_at' hf' hn).local_inverse f f' a
lemma local_inverse_apply_image
{n : ℕ∞} (hf : cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (f' : E' →L[𝕂] F') a)
(hn : 1 ≤ n) :
hf.local_inverse hf' hn (f a) = a :=
(hf.has_strict_fderiv_at' hf' hn).local_inverse_apply_image
/-- Given a `cont_diff` function over `𝕂` (which is `ℝ` or `ℂ`) with an invertible derivative
at `a`, the inverse function (produced by `cont_diff.to_local_homeomorph`) is
also `cont_diff`. -/
lemma to_local_inverse
{n : ℕ∞} (hf : cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (f' : E' →L[𝕂] F') a)
(hn : 1 ≤ n) :
cont_diff_at 𝕂 n (hf.local_inverse hf' hn) (f a) :=
begin
have := hf.local_inverse_apply_image hf' hn,
apply (hf.to_local_homeomorph f hf' hn).cont_diff_at_symm
(image_mem_to_local_homeomorph_target hf hf' hn),
{ convert hf' },
{ convert hf }
end
end cont_diff_at
|
515d9c87ddcdb02183a6743ab4cc51b583e2e731 | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/category_theory/limits/shapes/equalizers.lean | b953dc4b3fe2d7feed3decc10140c2a0cfaf75e6 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 7,094 | 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 data.fintype
import category_theory.limits.limits
open category_theory
namespace category_theory.limits
local attribute [tidy] tactic.case_bash
universes v u
/-- The type of objects for the diagram indexing a (co)equalizer. -/
@[derive decidable_eq] inductive walking_parallel_pair : Type v
| zero | one
instance fintype_walking_parallel_pair : fintype walking_parallel_pair :=
{ elems := [walking_parallel_pair.zero, walking_parallel_pair.one].to_finset,
complete := λ x, by { cases x; simp } }
open walking_parallel_pair
/-- The type family of morphisms for the diagram indexing a (co)equalizer. -/
inductive walking_parallel_pair_hom : walking_parallel_pair → walking_parallel_pair → Type v
| left : walking_parallel_pair_hom zero one
| right : walking_parallel_pair_hom zero one
| id : Π X : walking_parallel_pair.{v}, walking_parallel_pair_hom X X
open walking_parallel_pair_hom
def walking_parallel_pair_hom.comp :
Π (X Y Z : walking_parallel_pair)
(f : walking_parallel_pair_hom X Y) (g : walking_parallel_pair_hom Y Z),
walking_parallel_pair_hom X Z
| _ _ _ (id _) h := h
| _ _ _ left (id one) := left
| _ _ _ right (id one) := right
.
instance walking_parallel_pair_hom_category : small_category.{v} walking_parallel_pair :=
{ hom := walking_parallel_pair_hom,
id := walking_parallel_pair_hom.id,
comp := walking_parallel_pair_hom.comp }
lemma walking_parallel_pair_hom_id (X : walking_parallel_pair.{v}) :
walking_parallel_pair_hom.id X = 𝟙 X :=
rfl
variables {C : Type u} [𝒞 : category.{v} C]
include 𝒞
variables {X Y : C}
def parallel_pair (f g : X ⟶ Y) : walking_parallel_pair.{v} ⥤ C :=
{ obj := λ x, match x with
| zero := X
| one := Y
end,
map := λ x y h, match x, y, h with
| _, _, (id _) := 𝟙 _
| _, _, left := f
| _, _, right := g
end }.
@[simp] lemma parallel_pair_map_left (f g : X ⟶ Y) : (parallel_pair f g).map left = f := rfl
@[simp] lemma parallel_pair_map_right (f g : X ⟶ Y) : (parallel_pair f g).map right = g := rfl
@[simp] lemma parallel_pair_functor_obj
{F : walking_parallel_pair.{v} ⥤ C} (j : walking_parallel_pair.{v}) :
(parallel_pair (F.map left) (F.map right)).obj j = F.obj j :=
begin
cases j; refl
end
abbreviation fork (f g : X ⟶ Y) := cone (parallel_pair f g)
abbreviation cofork (f g : X ⟶ Y) := cocone (parallel_pair f g)
variables {f g : X ⟶ Y}
attribute [simp] walking_parallel_pair_hom_id
def fork.of_ι {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) : fork f g :=
{ X := P,
π :=
{ app := λ X, begin cases X, exact ι, exact ι ≫ f, end,
naturality' := λ X Y f,
begin
cases X; cases Y; cases f; dsimp; simp,
exact w
end }}
def cofork.of_π {P : C} (π : Y ⟶ P) (w : f ≫ π = g ≫ π) : cofork f g :=
{ X := P,
ι :=
{ app := λ X, begin cases X, exact f ≫ π, exact π, end,
naturality' := λ X Y f,
begin
cases X; cases Y; cases f; dsimp; simp,
exact eq.symm w
end }}
@[simp] lemma fork.of_ι_app_zero {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) :
(fork.of_ι ι w).π.app zero = ι := rfl
@[simp] lemma fork.of_ι_app_one {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) :
(fork.of_ι ι w).π.app one = ι ≫ f := rfl
def fork.ι (t : fork f g) := t.π.app zero
def cofork.π (t : cofork f g) := t.ι.app one
lemma fork.condition (t : fork f g) : (fork.ι t) ≫ f = (fork.ι t) ≫ g :=
begin
erw [t.w left, ← t.w right], refl
end
lemma cofork.condition (t : cofork f g) : f ≫ (cofork.π t) = g ≫ (cofork.π t) :=
begin
erw [t.w left, ← t.w right], refl
end
def cone.of_fork
{F : walking_parallel_pair.{v} ⥤ C} (t : fork (F.map left) (F.map right)) : cone F :=
{ X := t.X,
π :=
{ app := λ X, t.π.app X ≫ eq_to_hom (by tidy),
naturality' := λ j j' g,
begin
cases j; cases j'; cases g; dsimp; simp,
erw ← t.w left, refl,
erw ← t.w right, refl,
end } }.
def cocone.of_cofork
{F : walking_parallel_pair.{v} ⥤ C} (t : cofork (F.map left) (F.map right)) : cocone F :=
{ X := t.X,
ι :=
{ app := λ X, eq_to_hom (by tidy) ≫ t.ι.app X,
naturality' := λ j j' g,
begin
cases j; cases j'; cases g; dsimp; simp,
erw ← t.w left, refl,
erw ← t.w right, refl,
end } }.
@[simp] lemma cone.of_fork_π
{F : walking_parallel_pair.{v} ⥤ C} (t : fork (F.map left) (F.map right)) (j) :
(cone.of_fork t).π.app j = t.π.app j ≫ eq_to_hom (by tidy) := rfl
@[simp] lemma cocone.of_cofork_ι
{F : walking_parallel_pair.{v} ⥤ C} (t : cofork (F.map left) (F.map right)) (j) :
(cocone.of_cofork t).ι.app j = eq_to_hom (by tidy) ≫ t.ι.app j := rfl
def fork.of_cone
{F : walking_parallel_pair.{v} ⥤ C} (t : cone F) : fork (F.map left) (F.map right) :=
{ X := t.X,
π := { app := λ X, t.π.app X ≫ eq_to_hom (by tidy) } }
def cofork.of_cocone
{F : walking_parallel_pair.{v} ⥤ C} (t : cocone F) : cofork (F.map left) (F.map right) :=
{ X := t.X,
ι := { app := λ X, eq_to_hom (by tidy) ≫ t.ι.app X } }
@[simp] lemma fork.of_cone_π {F : walking_parallel_pair.{v} ⥤ C} (t : cone F) (j) :
(fork.of_cone t).π.app j = t.π.app j ≫ eq_to_hom (by tidy) := rfl
@[simp] lemma cofork.of_cocone_ι {F : walking_parallel_pair.{v} ⥤ C} (t : cocone F) (j) :
(cofork.of_cocone t).ι.app j = eq_to_hom (by tidy) ≫ t.ι.app j := rfl
variables (f g)
section
variables [has_limit (parallel_pair f g)]
abbreviation equalizer := limit (parallel_pair f g)
abbreviation equalizer.ι : equalizer f g ⟶ X :=
limit.π (parallel_pair f g) zero
@[simp, reassoc] lemma equalizer.condition : equalizer.ι f g ≫ f = equalizer.ι f g ≫ g :=
begin
erw limit.w (parallel_pair f g) walking_parallel_pair_hom.left,
erw limit.w (parallel_pair f g) walking_parallel_pair_hom.right
end
abbreviation equalizer.lift {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : W ⟶ equalizer f g :=
limit.lift (parallel_pair f g) (fork.of_ι k h)
end
section
variables [has_colimit (parallel_pair f g)]
abbreviation coequalizer := colimit (parallel_pair f g)
abbreviation coequalizer.π : Y ⟶ coequalizer f g :=
colimit.ι (parallel_pair f g) one
@[simp, reassoc] lemma coequalizer.condition : f ≫ coequalizer.π f g = g ≫ coequalizer.π f g :=
begin
erw colimit.w (parallel_pair f g) walking_parallel_pair_hom.left,
erw colimit.w (parallel_pair f g) walking_parallel_pair_hom.right
end
abbreviation coequalizer.desc {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : coequalizer f g ⟶ W :=
colimit.desc (parallel_pair f g) (cofork.of_π k h)
end
variables (C)
class has_equalizers :=
(has_limits_of_shape : has_limits_of_shape.{v} walking_parallel_pair C)
class has_coequalizers :=
(has_colimits_of_shape : has_colimits_of_shape.{v} walking_parallel_pair C)
attribute [instance] has_equalizers.has_limits_of_shape has_coequalizers.has_colimits_of_shape
end category_theory.limits
|
65e44d5f65a361f1d55b2148851ae5949e97c98c | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/simpPrefixIssue.lean | 3cea8940324baab0f7f3e4f20326602cefa64748 | [
"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 | 168 | lean | universe u
axiom f {α : Sort u} (a : α) : α
axiom f_eq {α : Sort u} (a : α) : f a = a
example (a : Nat) : f id a = a := by
simp only [f_eq]
trace_state
rfl
|
0e69993c383dcf65d93d93b31a974a6b4a5842b5 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/data/quot.lean | 50c0a4138b3a5002d1cb70eab957db7c7d42dee8 | [
"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 | 20,070 | 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 logic.relator
/-!
# Quotient types
This module extensd the core library's treatment of quotient types (`init.data.quot`).
## Tags
quotient
-/
variables {α : Sort*} {β : Sort*}
namespace setoid
lemma ext {α : Sort*} :
∀{s t : setoid α}, (∀a b, @setoid.r α s a b ↔ @setoid.r α t a b) → s = t
| ⟨r, _⟩ ⟨p, _⟩ eq :=
have r = p, from funext $ assume a, funext $ assume b, propext $ eq a b,
by subst this
end setoid
namespace quot
variables {ra : α → α → Prop} {rb : β → β → Prop} {φ : quot ra → quot rb → Sort*}
local notation `⟦`:max a `⟧` := quot.mk _ a
instance [inhabited α] : inhabited (quot ra) := ⟨⟦default _⟧⟩
/-- Recursion on two `quotient` arguments `a` and `b`, result type depends on `⟦a⟧` and `⟦b⟧`. -/
protected def hrec_on₂ (qa : quot ra) (qb : quot rb) (f : Π a b, φ ⟦a⟧ ⟦b⟧)
(ca : ∀ {b a₁ a₂}, ra a₁ a₂ → f a₁ b == f a₂ b)
(cb : ∀ {a b₁ b₂}, rb b₁ b₂ → f a b₁ == f a b₂) : φ qa qb :=
quot.hrec_on qa (λ a, quot.hrec_on qb (f a) (λ b₁ b₂ pb, cb pb)) $ λ a₁ a₂ pa,
quot.induction_on qb $ λ b,
calc @quot.hrec_on _ _ (φ _) ⟦b⟧ (f a₁) (@cb _)
== f a₁ b : by simp
... == f a₂ b : ca pa
... == @quot.hrec_on _ _ (φ _) ⟦b⟧ (f a₂) (@cb _) : by simp
/-- Map a function `f : α → β` such that `ra x y` implies `rb (f x) (f y)`
to a map `quot ra → quot rb`. -/
protected def map (f : α → β) (h : (ra ⇒ rb) f f) : quot ra → quot rb :=
quot.lift (λ x, ⟦f x⟧) $ assume x y (h₁ : ra x y), quot.sound $ h h₁
/-- If `ra` is a subrelation of `ra'`, then we have a natural map `quot ra → quot ra'`. -/
protected def map_right {ra' : α → α → Prop} (h : ∀a₁ a₂, ra a₁ a₂ → ra' a₁ a₂) :
quot ra → quot ra' :=
quot.map id h
/-- weaken the relation of a quotient -/
def factor {α : Type*} (r s : α → α → Prop) (h : ∀ x y, r x y → s x y) :
quot r → quot s :=
quot.lift (quot.mk s) (λ x y rxy, quot.sound (h x y rxy))
lemma factor_mk_eq {α : Type*} (r s : α → α → Prop) (h : ∀ x y, r x y → s x y) :
factor r s h ∘ quot.mk _ = quot.mk _ := rfl
variables {γ : Sort*} {r : α → α → Prop} {s : β → β → Prop}
/-- Descends a function `f : α → β → γ` to quotients of `α` and `β`. -/
attribute [reducible, elab_as_eliminator]
protected def lift₂
(f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂)
(hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b)
(q₁ : quot r) (q₂ : quot s) : γ :=
quot.lift (λ a, quot.lift (f a) (hr a))
(λ a₁ a₂ ha, funext (λ q, quot.induction_on q (λ b, hs a₁ a₂ b ha)))
q₁ q₂
@[simp]
lemma lift₂_mk (a : α) (b : β) (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂)
(hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) :
quot.lift₂ f hr hs (quot.mk r a) (quot.mk s b) = f a b := rfl
/-- Descends a function `f : α → β → γ` to quotients of `α` and `β` and applies it. -/
attribute [reducible, elab_as_eliminator]
protected def lift_on₂ (p : quot r) (q : quot s) (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂)
(hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) : γ := quot.lift₂ f hr hs p q
@[simp]
lemma lift_on₂_mk (a : α) (b : β) (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂)
(hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) :
quot.lift_on₂ (quot.mk r a) (quot.mk s b) f hr hs = f a b := rfl
variables {t : γ → γ → Prop}
/-- Descends a function `f : α → β → γ` to quotients of `α` and `β` wih values in a quotient of
`γ`. -/
protected def map₂ (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → t (f a b₁) (f a b₂))
(hs : ∀ a₁ a₂ b, r a₁ a₂ → t (f a₁ b) (f a₂ b))
(q₁ : quot r) (q₂ : quot s) : quot t :=
quot.lift₂ (λ a b, quot.mk t $ f a b) (λ a b₁ b₂ hb, quot.sound (hr a b₁ b₂ hb))
(λ a₁ a₂ b ha, quot.sound (hs a₁ a₂ b ha)) q₁ q₂
@[simp]
lemma map₂_mk (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → t (f a b₁) (f a b₂))
(hs : ∀ a₁ a₂ b, r a₁ a₂ → t (f a₁ b) (f a₂ b))
(a : α) (b : β) : quot.map₂ f hr hs (quot.mk r a) (quot.mk s b) = quot.mk t (f a b) := rfl
attribute [elab_as_eliminator]
protected lemma induction_on₂
{δ : quot r → quot s → Prop} (q₁ : quot r) (q₂ : quot s)
(h : ∀ a b, δ (quot.mk r a) (quot.mk s b)) : δ q₁ q₂ :=
quot.ind (λ a₁, quot.ind (λ a₂, h a₁ a₂) q₂) q₁
attribute [elab_as_eliminator]
protected lemma induction_on₃
{δ : quot r → quot s → quot t → Prop} (q₁ : quot r) (q₂ : quot s) (q₃ : quot t)
(h : ∀ a b c, δ (quot.mk r a) (quot.mk s b) (quot.mk t c)) : δ q₁ q₂ q₃ :=
quot.ind (λ a₁, quot.ind (λ a₂, quot.ind (λ a₃, h a₁ a₂ a₃) q₃) q₂) q₁
end quot
namespace quotient
variables [sa : setoid α] [sb : setoid β]
variables {φ : quotient sa → quotient sb → Sort*}
instance [inhabited α] : inhabited (quotient sa) := ⟨⟦default _⟧⟩
/-- Induction on two `quotient` arguments `a` and `b`, result type depends on `⟦a⟧` and `⟦b⟧`. -/
protected def hrec_on₂ (qa : quotient sa) (qb : quotient sb) (f : Π a b, φ ⟦a⟧ ⟦b⟧)
(c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ == f a₂ b₂) : φ qa qb :=
quot.hrec_on₂ qa qb f
(λ _ _ _ p, c _ _ _ _ p (setoid.refl _))
(λ _ _ _ p, c _ _ _ _ (setoid.refl _) p)
/-- Map a function `f : α → β` that sends equivalent elements to equivalent elements
to a function `quotient sa → quotient sb`. Useful to define unary operations on quotients. -/
protected def map (f : α → β) (h : ((≈) ⇒ (≈)) f f) : quotient sa → quotient sb :=
quot.map f h
@[simp] lemma map_mk (f : α → β) (h : ((≈) ⇒ (≈)) f f) (x : α) :
quotient.map f h (⟦x⟧ : quotient sa) = (⟦f x⟧ : quotient sb) :=
rfl
variables {γ : Sort*} [sc : setoid γ]
/-- Map a function `f : α → β → γ` that sends equivalent elements to equivalent elements
to a function `f : quotient sa → quotient sb → quotient sc`.
Useful to define binary operations on quotients. -/
protected def map₂ (f : α → β → γ) (h : ((≈) ⇒ (≈) ⇒ (≈)) f f) :
quotient sa → quotient sb → quotient sc :=
quotient.lift₂ (λ x y, ⟦f x y⟧) (λ x₁ y₁ x₂ y₂ h₁ h₂, quot.sound $ h h₁ h₂)
end quotient
lemma quot.eq {α : Type*} {r : α → α → Prop} {x y : α} :
quot.mk r x = quot.mk r y ↔ eqv_gen r x y :=
⟨quot.exact r, quot.eqv_gen_sound⟩
@[simp] theorem quotient.eq [r : setoid α] {x y : α} : ⟦x⟧ = ⟦y⟧ ↔ x ≈ y :=
⟨quotient.exact, quotient.sound⟩
theorem forall_quotient_iff {α : Type*} [r : setoid α] {p : quotient r → Prop} :
(∀a:quotient r, p a) ↔ (∀a:α, p ⟦a⟧) :=
⟨assume h x, h _, assume h a, a.induction_on h⟩
@[simp] lemma quotient.lift_beta [s : setoid α] (f : α → β) (h : ∀ (a b : α), a ≈ b → f a = f b)
(x : α) :
quotient.lift f h (quotient.mk x) = f x := rfl
@[simp] lemma quotient.lift_on_beta [s : setoid α] (f : α → β) (h : ∀ (a b : α), a ≈ b → f a = f b)
(x : α) :
quotient.lift_on (quotient.mk x) f h = f x := rfl
@[simp] theorem quotient.lift_on_beta₂ {α : Sort*} {β : Sort*} [setoid α] (f : α → α → β)
(h : ∀ (a₁ a₂ b₁ b₂ : α), a₁ ≈ b₁ → a₂ ≈ b₂ → f a₁ a₂ = f b₁ b₂) (x y : α) :
quotient.lift_on₂ (quotient.mk x) (quotient.mk y) f h = f x y := rfl
/-- `quot.mk r` is a surjective function. -/
lemma surjective_quot_mk (r : α → α → Prop) : function.surjective (quot.mk r) :=
quot.exists_rep
/-- `quotient.mk` is a surjective function. -/
lemma surjective_quotient_mk (α : Sort*) [s : setoid α] :
function.surjective (quotient.mk : α → quotient s) :=
quot.exists_rep
/-- Choose an element of the equivalence class using the axiom of choice.
Sound but noncomputable. -/
noncomputable def quot.out {r : α → α → Prop} (q : quot r) : α :=
classical.some (quot.exists_rep q)
/-- Unwrap the VM representation of a quotient to obtain an element of the equivalence class.
Computable but unsound. -/
meta def quot.unquot {r : α → α → Prop} : quot r → α := unchecked_cast
@[simp] theorem quot.out_eq {r : α → α → Prop} (q : quot r) : quot.mk r q.out = q :=
classical.some_spec (quot.exists_rep q)
/-- Choose an element of the equivalence class using the axiom of choice.
Sound but noncomputable. -/
noncomputable def quotient.out [s : setoid α] : quotient s → α := quot.out
@[simp] theorem quotient.out_eq [s : setoid α] (q : quotient s) : ⟦q.out⟧ = q := q.out_eq
theorem quotient.mk_out [s : setoid α] (a : α) : ⟦a⟧.out ≈ a :=
quotient.exact (quotient.out_eq _)
instance pi_setoid {ι : Sort*} {α : ι → Sort*} [∀ i, setoid (α i)] : setoid (Π i, α i) :=
{ r := λ a b, ∀ i, a i ≈ b i,
iseqv := ⟨
λ a i, setoid.refl _,
λ a b h i, setoid.symm (h _),
λ a b c h₁ h₂ i, setoid.trans (h₁ _) (h₂ _)⟩ }
/-- Given a function `f : Π i, quotient (S i)`, returns the class of functions `Π i, α i` sending
each `i` to an element of the class `f i`. -/
noncomputable def quotient.choice {ι : Type*} {α : ι → Type*} [S : Π i, setoid (α i)]
(f : Π i, quotient (S i)) : @quotient (Π i, α i) (by apply_instance) :=
⟦λ i, (f i).out⟧
theorem quotient.choice_eq {ι : Type*} {α : ι → Type*} [Π i, setoid (α i)]
(f : Π i, α i) : quotient.choice (λ i, ⟦f i⟧) = ⟦f⟧ :=
quotient.sound $ λ i, quotient.mk_out _
lemma nonempty_quotient_iff (s : setoid α) : nonempty (quotient s) ↔ nonempty α :=
⟨assume ⟨a⟩, quotient.induction_on a nonempty.intro, assume ⟨a⟩, ⟨⟦a⟧⟩⟩
/-- `trunc α` is the quotient of `α` by the always-true relation. This
is related to the propositional truncation in HoTT, and is similar
in effect to `nonempty α`, but unlike `nonempty α`, `trunc α` is data,
so the VM representation is the same as `α`, and so this can be used to
maintain computability. -/
def {u} trunc (α : Sort u) : Sort u := @quot α (λ _ _, true)
theorem true_equivalence : @equivalence α (λ _ _, true) :=
⟨λ _, trivial, λ _ _ _, trivial, λ _ _ _ _ _, trivial⟩
namespace trunc
/-- Constructor for `trunc α` -/
def mk (a : α) : trunc α := quot.mk _ a
instance [inhabited α] : inhabited (trunc α) := ⟨mk (default _)⟩
/-- Any constant function lifts to a function out of the truncation -/
def lift (f : α → β) (c : ∀ a b : α, f a = f b) : trunc α → β :=
quot.lift f (λ a b _, c a b)
theorem ind {β : trunc α → Prop} : (∀ a : α, β (mk a)) → ∀ q : trunc α, β q := quot.ind
protected theorem lift_beta (f : α → β) (c) (a : α) : lift f c (mk a) = f a := rfl
/-- Lift a constant function on `q : trunc α`. -/
@[reducible, elab_as_eliminator]
protected def lift_on (q : trunc α) (f : α → β)
(c : ∀ a b : α, f a = f b) : β := lift f c q
@[elab_as_eliminator]
protected theorem induction_on {β : trunc α → Prop} (q : trunc α)
(h : ∀ a, β (mk a)) : β q := ind h q
theorem exists_rep (q : trunc α) : ∃ a : α, mk a = q := quot.exists_rep q
attribute [elab_as_eliminator]
protected theorem induction_on₂ {C : trunc α → trunc β → Prop} (q₁ : trunc α) (q₂ : trunc β)
(h : ∀ a b, C (mk a) (mk b)) : C q₁ q₂ :=
trunc.induction_on q₁ $ λ a₁, trunc.induction_on q₂ (h a₁)
protected theorem eq (a b : trunc α) : a = b :=
trunc.induction_on₂ a b (λ x y, quot.sound trivial)
instance : subsingleton (trunc α) := ⟨trunc.eq⟩
/-- The `bind` operator for the `trunc` monad. -/
def bind (q : trunc α) (f : α → trunc β) : trunc β :=
trunc.lift_on q f (λ a b, trunc.eq _ _)
/-- A function `f : α → β` defines a function `map f : trunc α → trunc β`. -/
def map (f : α → β) (q : trunc α) : trunc β := bind q (trunc.mk ∘ f)
instance : monad trunc :=
{ pure := @trunc.mk,
bind := @trunc.bind }
instance : is_lawful_monad trunc :=
{ id_map := λ α q, trunc.eq _ _,
pure_bind := λ α β q f, rfl,
bind_assoc := λ α β γ x f g, trunc.eq _ _ }
variable {C : trunc α → Sort*}
/-- Recursion/induction principle for `trunc`. -/
@[reducible, elab_as_eliminator]
protected def rec
(f : Π a, C (mk a)) (h : ∀ (a b : α), (eq.rec (f a) (trunc.eq (mk a) (mk b)) : C (mk b)) = f b)
(q : trunc α) : C q :=
quot.rec f (λ a b _, h a b) q
/-- A version of `trunc.rec` taking `q : trunc α` as the first argument. -/
@[reducible, elab_as_eliminator]
protected def rec_on (q : trunc α) (f : Π a, C (mk a))
(h : ∀ (a b : α), (eq.rec (f a) (trunc.eq (mk a) (mk b)) : C (mk b)) = f b) : C q :=
trunc.rec f h q
/-- A version of `trunc.rec_on` assuming the codomain is a `subsingleton`. -/
@[reducible, elab_as_eliminator]
protected def rec_on_subsingleton
[∀ a, subsingleton (C (mk a))] (q : trunc α) (f : Π a, C (mk a)) : C q :=
trunc.rec f (λ a b, subsingleton.elim _ (f b)) q
/-- Noncomputably extract a representative of `trunc α` (using the axiom of choice). -/
noncomputable def out : trunc α → α := quot.out
@[simp] theorem out_eq (q : trunc α) : mk q.out = q := trunc.eq _ _
end trunc
theorem nonempty_of_trunc (q : trunc α) : nonempty α :=
let ⟨a, _⟩ := q.exists_rep in ⟨a⟩
namespace quotient
variables {γ : Sort*} {φ : Sort*}
{s₁ : setoid α} {s₂ : setoid β} {s₃ : setoid γ}
/- Versions of quotient definitions and lemmas ending in `'` use unification instead
of typeclass inference for inferring the `setoid` argument. This is useful when there are
several different quotient relations on a type, for example quotient groups, rings and modules -/
/-- A version of `quotient.mk` taking `{s : setoid α}` as an implicit argument instead of an
instance argument. -/
protected def mk' (a : α) : quotient s₁ := quot.mk s₁.1 a
/-- `quotient.mk'` is a surjective function. -/
lemma surjective_quotient_mk' : function.surjective (quotient.mk' : α → quotient s₁) :=
quot.exists_rep
/-- A version of `quotient.lift_on` taking `{s : setoid α}` as an implicit argument instead of an
instance argument. -/
@[elab_as_eliminator, reducible]
protected def lift_on' (q : quotient s₁) (f : α → φ)
(h : ∀ a b, @setoid.r α s₁ a b → f a = f b) : φ := quotient.lift_on q f h
/-- A version of `quotient.lift_on₂` taking `{s₁ : setoid α} {s₂ : setoid β}` as implicit arguments
instead of instance arguments. -/
@[elab_as_eliminator, reducible]
protected def lift_on₂' (q₁ : quotient s₁) (q₂ : quotient s₂) (f : α → β → γ)
(h : ∀ a₁ a₂ b₁ b₂, @setoid.r α s₁ a₁ b₁ → @setoid.r β s₂ a₂ b₂ → f a₁ a₂ = f b₁ b₂) : γ :=
quotient.lift_on₂ q₁ q₂ f h
/-- A version of `quotient.ind` taking `{s : setoid α}` as an implicit argument instead of an
instance argument. -/
@[elab_as_eliminator]
protected lemma ind' {p : quotient s₁ → Prop}
(h : ∀ a, p (quotient.mk' a)) (q : quotient s₁) : p q :=
quotient.ind h q
/-- A version of `quotient.ind₂` taking `{s₁ : setoid α} {s₂ : setoid β}` as implicit arguments
instead of instance arguments. -/
@[elab_as_eliminator]
protected lemma ind₂' {p : quotient s₁ → quotient s₂ → Prop}
(h : ∀ a₁ a₂, p (quotient.mk' a₁) (quotient.mk' a₂))
(q₁ : quotient s₁) (q₂ : quotient s₂) : p q₁ q₂ :=
quotient.ind₂ h q₁ q₂
/-- A version of `quotient.induction_on` taking `{s : setoid α}` as an implicit argument instead
of an instance argument. -/
@[elab_as_eliminator]
protected lemma induction_on' {p : quotient s₁ → Prop} (q : quotient s₁)
(h : ∀ a, p (quotient.mk' a)) : p q := quotient.induction_on q h
/-- A version of `quotient.induction_on₂` taking `{s₁ : setoid α} {s₂ : setoid β}` as implicit
arguments instead of instance arguments. -/
@[elab_as_eliminator]
protected lemma induction_on₂' {p : quotient s₁ → quotient s₂ → Prop} (q₁ : quotient s₁)
(q₂ : quotient s₂) (h : ∀ a₁ a₂, p (quotient.mk' a₁) (quotient.mk' a₂)) : p q₁ q₂ :=
quotient.induction_on₂ q₁ q₂ h
/-- A version of `quotient.induction_on₃` taking `{s₁ : setoid α} {s₂ : setoid β} {s₃ : setoid γ}`
as implicit arguments instead of instance arguments. -/
@[elab_as_eliminator]
protected lemma induction_on₃' {p : quotient s₁ → quotient s₂ → quotient s₃ → Prop}
(q₁ : quotient s₁) (q₂ : quotient s₂) (q₃ : quotient s₃)
(h : ∀ a₁ a₂ a₃, p (quotient.mk' a₁) (quotient.mk' a₂) (quotient.mk' a₃)) : p q₁ q₂ q₃ :=
quotient.induction_on₃ q₁ q₂ q₃ h
/-- Recursion on a `quotient` argument `a`, result type depends on `⟦a⟧`. -/
protected def hrec_on' {φ : quotient s₁ → Sort*} (qa : quotient s₁) (f : Π a, φ (quotient.mk' a))
(c : ∀ a₁ a₂, a₁ ≈ a₂ → f a₁ == f a₂) : φ qa :=
quot.hrec_on qa f c
@[simp] lemma hrec_on'_mk' {φ : quotient s₁ → Sort*} (f : Π a, φ (quotient.mk' a))
(c : ∀ a₁ a₂, a₁ ≈ a₂ → f a₁ == f a₂) (x : α) :
(quotient.mk' x).hrec_on' f c = f x :=
rfl
/-- Recursion on two `quotient` arguments `a` and `b`, result type depends on `⟦a⟧` and `⟦b⟧`. -/
protected def hrec_on₂' {φ : quotient s₁ → quotient s₂ → Sort*} (qa : quotient s₁)
(qb : quotient s₂) (f : ∀ a b, φ (quotient.mk' a) (quotient.mk' b))
(c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ == f a₂ b₂) : φ qa qb :=
quotient.hrec_on₂ qa qb f c
@[simp] lemma hrec_on₂'_mk' {φ : quotient s₁ → quotient s₂ → Sort*}
(f : ∀ a b, φ (quotient.mk' a) (quotient.mk' b))
(c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ == f a₂ b₂) (x : α) (qb : quotient s₂) :
(quotient.mk' x).hrec_on₂' qb f c = qb.hrec_on' (f x) (λ b₁ b₂, c _ _ _ _ (setoid.refl _)) :=
rfl
/-- Map a function `f : α → β` that sends equivalent elements to equivalent elements
to a function `quotient sa → quotient sb`. Useful to define unary operations on quotients. -/
protected def map' (f : α → β) (h : ((≈) ⇒ (≈)) f f) :
quotient s₁ → quotient s₂ :=
quot.map f h
@[simp] lemma map'_mk' (f : α → β) (h) (x : α) :
(quotient.mk' x : quotient s₁).map' f h = (quotient.mk' (f x) : quotient s₂) :=
rfl
/-- A version of `quotient.map₂` using curly braces and unification. -/
protected def map₂' (f : α → β → γ) (h : ((≈) ⇒ (≈) ⇒ (≈)) f f) :
quotient s₁ → quotient s₂ → quotient s₃ :=
quotient.map₂ f h
@[simp] lemma map₂'_mk' (f : α → β → γ) (h) (x : α) :
(quotient.mk' x : quotient s₁).map₂' f h =
(quotient.map' (f x) (h (setoid.refl x)) : quotient s₂ → quotient s₃) :=
rfl
lemma exact' {a b : α} :
(quotient.mk' a : quotient s₁) = quotient.mk' b → @setoid.r _ s₁ a b :=
quotient.exact
lemma sound' {a b : α} : @setoid.r _ s₁ a b → @quotient.mk' α s₁ a = quotient.mk' b :=
quotient.sound
@[simp]
protected lemma eq' {a b : α} : @quotient.mk' α s₁ a = quotient.mk' b ↔ @setoid.r _ s₁ a b :=
quotient.eq
/-- A version of `quotient.out` taking `{s₁ : setoid α}` as an implicit argument instead of an
instance argument. -/
noncomputable def out' (a : quotient s₁) : α := quotient.out a
@[simp] theorem out_eq' (q : quotient s₁) : quotient.mk' q.out' = q := q.out_eq
theorem mk_out' (a : α) : @setoid.r α s₁ (quotient.mk' a : quotient s₁).out' a :=
quotient.exact (quotient.out_eq _)
end quotient
|
d804ac5df27ff14f0aa2794f8889220e96e04f50 | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /02_Dependent_Type_Theory.org.39.lean | d90dae6f873464b5c342c755a06dd48dd73b2fdf | [] | 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 | 227 | lean | /- page 30 -/
import standard
definition id {A : Type} (x : A) := x
check id -- ?A → ?A
variables A B : Type
variables (a : A) (b : B)
check id -- ?A A B a b → ?A A B a b
check id a -- A
check id b -- B
|
4df06254130f10e7ac07998565e2f59b594f01f7 | ce6917c5bacabee346655160b74a307b4a5ab620 | /src/ch3/ex0311.lean | 71f2aacc2d7c8d68ca6d45dc872b3f8c44c42fb0 | [] | no_license | Ailrun/Theorem_Proving_in_Lean | ae6a23f3c54d62d401314d6a771e8ff8b4132db2 | 2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68 | refs/heads/master | 1,609,838,270,467 | 1,586,846,743,000 | 1,586,846,743,000 | 240,967,761 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 200 | lean | variables p q : Prop
example (h : p ∨ q) : q ∨ p :=
or.elim h
(assume hp : p,
show q ∨ p, from or.intro_right q hp)
(assume hq : q,
show q ∨ p, from or.intro_left p hq)
|
a1a177fc4c56afe7c057c359aba51bac99700863 | 69d4931b605e11ca61881fc4f66db50a0a875e39 | /src/number_theory/pell.lean | 8978a09bf54300da34b66efabc13394d59b433b7 | [
"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 | 39,426 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import data.nat.modeq
import data.zsqrtd.basic
/-!
# Pell's equation and Matiyasevic's theorem
This file solves Pell's equation, i.e. integer solutions to `x ^ 2 - d * y ^ 2 = 1` in the special
case that `d = a ^ 2 - 1`. This is then applied to prove Matiyasevic's theorem that the power
function is Diophantine, which is the last key ingredient in the solution to Hilbert's tenth
problem. For the definition of Diophantine function, see `dioph.lean`.
## Main definition
* `pell` is a function assigning to a natural number `n` the `n`-th solution to Pell's equation
constructed recursively from the intial solution `(0,1)`.
## Main statements
* `eq_pell` shows that every solution to Pell's equation is recursively obtained using `pell`
* `matiyasevic` shows that a certain system of Diophantine equations has a solution if and only if
the first variable is the `x`-component in a solution to Pell's equation - the key step towards
Hilbert's tenth problem in Davis' version of Matiyasevic's theorem.
* `eq_pow_of_pell` shows that the power function is Diophantine.
## Implementation notes
The proof of Matiyasevic's theorem doesn't follow Matiyasevic's original account of using Fibonacci
numbers but instead Davis' variant of using solutions to Pell's equation.
## References
* [M. Carneiro, _A Lean formalization of Matiyasiv's theorem_][carneiro2018matiysevic]
* [M. Davis, _Hilbert's tenth problem is unsolvable_][MR317916]
## Tags
Pell's equation, Matiyasevic's theorem, Hilbert's tenth problem
## TODO
* Please the unused arguments linter.
* Provide solutions to Pell's equation for the case of arbitrary `d` (not just `d = a ^ 2 - 1` like
in the current version) and furthermore also for `x ^ 2 - d * y ^ 2 = -1`.
* Connect solutions to the continued fraction expansion of `√d`.
-/
namespace pell
open nat
section
parameters {a : ℕ} (a1 : 1 < a)
include a1
private def d := a*a - 1
@[simp] theorem d_pos : 0 < d :=
nat.sub_pos_of_lt (mul_lt_mul a1 (le_of_lt a1) dec_trivial dec_trivial : 1*1<a*a)
/-- The Pell sequences, i.e. the sequence of integer solutions to `x ^ 2 - d * y ^ 2 = 1`, where
`d = a ^ 2 - 1`, defined together in mutual recursion. -/
-- TODO(lint): Fix double namespace issue
@[nolint dup_namespace] def pell : ℕ → ℕ × ℕ :=
λn, nat.rec_on n (1, 0) (λn xy, (xy.1*a + d*xy.2, xy.1 + xy.2*a))
/-- The Pell `x` sequence. -/
def xn (n : ℕ) : ℕ := (pell n).1
/-- The Pell `y` sequence. -/
def yn (n : ℕ) : ℕ := (pell n).2
@[simp] theorem pell_val (n : ℕ) : pell n = (xn n, yn n) :=
show pell n = ((pell n).1, (pell n).2), from match pell n with (a, b) := rfl end
@[simp] theorem xn_zero : xn 0 = 1 := rfl
@[simp] theorem yn_zero : yn 0 = 0 := rfl
@[simp] theorem xn_succ (n : ℕ) : xn (n+1) = xn n * a + d * yn n := rfl
@[simp] theorem yn_succ (n : ℕ) : yn (n+1) = xn n + yn n * a := rfl
@[simp] theorem xn_one : xn 1 = a := by simp
@[simp] theorem yn_one : yn 1 = 1 := by simp
/-- The Pell `x` sequence, considered as an integer sequence.-/
def xz (n : ℕ) : ℤ := xn n
/-- The Pell `y` sequence, considered as an integer sequence.-/
def yz (n : ℕ) : ℤ := yn n
/-- The element `a` such that `d = a ^ 2 - 1`, considered as an integer.-/
def az : ℤ := a
theorem asq_pos : 0 < a*a :=
le_trans (le_of_lt a1) (by have := @nat.mul_le_mul_left 1 a a (le_of_lt a1); rwa mul_one at this)
theorem dz_val : ↑d = az*az - 1 :=
have 1 ≤ a*a, from asq_pos,
show ↑(a*a - 1) = _, by rw int.coe_nat_sub this; refl
@[simp] theorem xz_succ (n : ℕ) : xz (n+1) = xz n * az + ↑d * yz n := rfl
@[simp] theorem yz_succ (n : ℕ) : yz (n+1) = xz n + yz n * az := rfl
/-- The Pell sequence can also be viewed as an element of `ℤ√d` -/
def pell_zd (n : ℕ) : ℤ√d := ⟨xn n, yn n⟩
@[simp] theorem pell_zd_re (n : ℕ) : (pell_zd n).re = xn n := rfl
@[simp] theorem pell_zd_im (n : ℕ) : (pell_zd n).im = yn n := rfl
/-- The property of being a solution to the Pell equation, expressed
as a property of elements of `ℤ√d`. -/
def is_pell : ℤ√d → Prop | ⟨x, y⟩ := x*x - d*y*y = 1
theorem is_pell_nat {x y : ℕ} : is_pell ⟨x, y⟩ ↔ x*x - d*y*y = 1 :=
⟨λh, int.coe_nat_inj
(by rw int.coe_nat_sub (int.le_of_coe_nat_le_coe_nat $ int.le.intro_sub h); exact h),
λh, show ((x*x : ℕ) - (d*y*y:ℕ) : ℤ) = 1,
by rw [← int.coe_nat_sub $ le_of_lt $ nat.lt_of_sub_eq_succ h, h]; refl⟩
theorem is_pell_norm : Π {b : ℤ√d}, is_pell b ↔ b * b.conj = 1
| ⟨x, y⟩ := by simp [zsqrtd.ext, is_pell, mul_comm]; ring_nf
theorem is_pell_mul {b c : ℤ√d} (hb : is_pell b) (hc : is_pell c) : is_pell (b * c) :=
is_pell_norm.2 (by simp [mul_comm, mul_left_comm,
zsqrtd.conj_mul, pell.is_pell_norm.1 hb, pell.is_pell_norm.1 hc])
theorem is_pell_conj : ∀ {b : ℤ√d}, is_pell b ↔ is_pell b.conj | ⟨x, y⟩ :=
by simp [is_pell, zsqrtd.conj]
@[simp] theorem pell_zd_succ (n : ℕ) : pell_zd (n+1) = pell_zd n * ⟨a, 1⟩ :=
by simp [zsqrtd.ext]
theorem is_pell_one : is_pell ⟨a, 1⟩ :=
show az*az-d*1*1=1, by simp [dz_val]; ring
theorem is_pell_pell_zd : ∀ (n : ℕ), is_pell (pell_zd n)
| 0 := rfl
| (n+1) := let o := is_pell_one in by simp; exact pell.is_pell_mul (is_pell_pell_zd n) o
@[simp] theorem pell_eqz (n : ℕ) : xz n * xz n - d * yz n * yz n = 1 := is_pell_pell_zd n
@[simp] theorem pell_eq (n : ℕ) : xn n * xn n - d * yn n * yn n = 1 :=
let pn := pell_eqz n in
have h : (↑(xn n * xn n) : ℤ) - ↑(d * yn n * yn n) = 1,
by repeat {rw int.coe_nat_mul}; exact pn,
have hl : d * yn n * yn n ≤ xn n * xn n, from
int.le_of_coe_nat_le_coe_nat $ int.le.intro $ add_eq_of_eq_sub' $ eq.symm h,
int.coe_nat_inj (by rw int.coe_nat_sub hl; exact h)
instance dnsq : zsqrtd.nonsquare d := ⟨λn h,
have n*n + 1 = a*a, by rw ← h; exact nat.succ_pred_eq_of_pos (asq_pos a1),
have na : n < a, from nat.mul_self_lt_mul_self_iff.2 (by rw ← this; exact nat.lt_succ_self _),
have (n+1)*(n+1) ≤ n*n + 1, by rw this; exact nat.mul_self_le_mul_self na,
have n+n ≤ 0, from @nat.le_of_add_le_add_right (n*n + 1) _ _ (by ring_nf at this ⊢; assumption),
ne_of_gt d_pos $ by rwa nat.eq_zero_of_le_zero ((nat.le_add_left _ _).trans this) at h⟩
theorem xn_ge_a_pow : ∀ (n : ℕ), a^n ≤ xn n
| 0 := le_refl 1
| (n+1) := by simp [pow_succ']; exact le_trans
(nat.mul_le_mul_right _ (xn_ge_a_pow n)) (nat.le_add_right _ _)
theorem n_lt_a_pow : ∀ (n : ℕ), n < a^n
| 0 := nat.le_refl 1
| (n+1) := begin have IH := n_lt_a_pow n,
have : a^n + a^n ≤ a^n * a,
{ rw ← mul_two, exact nat.mul_le_mul_left _ a1 },
simp [pow_succ'], refine lt_of_lt_of_le _ this,
exact add_lt_add_of_lt_of_le IH (lt_of_le_of_lt (nat.zero_le _) IH)
end
theorem n_lt_xn (n) : n < xn n :=
lt_of_lt_of_le (n_lt_a_pow n) (xn_ge_a_pow n)
theorem x_pos (n) : 0 < xn n :=
lt_of_le_of_lt (nat.zero_le n) (n_lt_xn n)
lemma eq_pell_lem : ∀n (b:ℤ√d), 1 ≤ b → is_pell b → b ≤ pell_zd n → ∃n, b = pell_zd n
| 0 b := λh1 hp hl, ⟨0, @zsqrtd.le_antisymm _ dnsq _ _ hl h1⟩
| (n+1) b := λh1 hp h,
have a1p : (0:ℤ√d) ≤ ⟨a, 1⟩, from trivial,
have am1p : (0:ℤ√d) ≤ ⟨a, -1⟩, from show (_:nat) ≤ _, by simp; exact nat.pred_le _,
have a1m : (⟨a, 1⟩ * ⟨a, -1⟩ : ℤ√d) = 1, from is_pell_norm.1 is_pell_one,
if ha : (⟨↑a, 1⟩ : ℤ√d) ≤ b then
let ⟨m, e⟩ := eq_pell_lem n (b * ⟨a, -1⟩)
(by rw ← a1m; exact mul_le_mul_of_nonneg_right ha am1p)
(is_pell_mul hp (is_pell_conj.1 is_pell_one))
(by have t := mul_le_mul_of_nonneg_right h am1p;
rwa [pell_zd_succ, mul_assoc, a1m, mul_one] at t) in
⟨m+1, by rw [show b = b * ⟨a, -1⟩ * ⟨a, 1⟩, by rw [mul_assoc, eq.trans (mul_comm _ _) a1m];
simp, pell_zd_succ, e]⟩
else
suffices ¬1 < b, from ⟨0, show b = 1, from (or.resolve_left (lt_or_eq_of_le h1) this).symm⟩,
λ h1l, by cases b with x y; exact
have bm : (_*⟨_,_⟩ :ℤ√(d a1)) = 1, from pell.is_pell_norm.1 hp,
have y0l : (0:ℤ√(d a1)) < ⟨x - x, y - -y⟩,
from sub_lt_sub h1l $ λ(hn : (1:ℤ√(d a1)) ≤ ⟨x, -y⟩),
by have t := mul_le_mul_of_nonneg_left hn (le_trans zero_le_one h1);
rw [bm, mul_one] at t; exact h1l t,
have yl2 : (⟨_, _⟩ : ℤ√_) < ⟨_, _⟩, from
show (⟨x, y⟩ - ⟨x, -y⟩ : ℤ√(d a1)) < ⟨a, 1⟩ - ⟨a, -1⟩, from
sub_lt_sub (by exact ha) $ λ(hn : (⟨x, -y⟩ : ℤ√(d a1)) ≤ ⟨a, -1⟩),
by have t := mul_le_mul_of_nonneg_right
(mul_le_mul_of_nonneg_left hn (le_trans zero_le_one h1)) a1p;
rw [bm, one_mul, mul_assoc, eq.trans (mul_comm _ _) a1m, mul_one] at t; exact ha t,
by simp at y0l; simp at yl2; exact
match y, y0l, (yl2 : (⟨_, _⟩ : ℤ√_) < ⟨_, _⟩) with
| 0, y0l, yl2 := y0l (le_refl 0)
| (y+1 : ℕ), y0l, yl2 := yl2 (zsqrtd.le_of_le_le (le_refl 0)
(let t := int.coe_nat_le_coe_nat_of_le (nat.succ_pos y) in add_le_add t t))
| -[1+y], y0l, yl2 := y0l trivial
end
theorem eq_pell_zd (b : ℤ√d) (b1 : 1 ≤ b) (hp : is_pell b) : ∃n, b = pell_zd n :=
let ⟨n, h⟩ := @zsqrtd.le_arch d b in
eq_pell_lem n b b1 hp $ zsqrtd.le_trans h $ by rw zsqrtd.coe_nat_val; exact
zsqrtd.le_of_le_le
(int.coe_nat_le_coe_nat_of_le $ le_of_lt $ n_lt_xn _ _) (int.coe_zero_le _)
/-- Every solution to Pell's equation is recursively obtained from the initial solution `(1,0)`
using the recursion `pell`-/
theorem eq_pell {x y : ℕ} (hp : x*x - d*y*y = 1) : ∃n, x = xn n ∧ y = yn n :=
have (1:ℤ√d) ≤ ⟨x, y⟩, from match x, hp with
| 0, (hp : 0 - _ = 1) := by rw nat.zero_sub at hp; contradiction
| (x+1), hp := zsqrtd.le_of_le_le (int.coe_nat_le_coe_nat_of_le $ nat.succ_pos x)
(int.coe_zero_le _)
end,
let ⟨m, e⟩ := eq_pell_zd ⟨x, y⟩ this (is_pell_nat.2 hp) in
⟨m, match x, y, e with ._, ._, rfl := ⟨rfl, rfl⟩ end⟩
theorem pell_zd_add (m) : ∀ n, pell_zd (m + n) = pell_zd m * pell_zd n
| 0 := (mul_one _).symm
| (n+1) := by rw[← add_assoc, pell_zd_succ, pell_zd_succ, pell_zd_add n, ← mul_assoc]
theorem xn_add (m n) : xn (m + n) = xn m * xn n + d * yn m * yn n :=
by injection (pell_zd_add _ m n) with h _;
repeat {rw ← int.coe_nat_add at h <|> rw ← int.coe_nat_mul at h};
exact int.coe_nat_inj h
theorem yn_add (m n) : yn (m + n) = xn m * yn n + yn m * xn n :=
by injection (pell_zd_add _ m n) with _ h;
repeat {rw ← int.coe_nat_add at h <|> rw ← int.coe_nat_mul at h};
exact int.coe_nat_inj h
theorem pell_zd_sub {m n} (h : n ≤ m) : pell_zd (m - n) = pell_zd m * (pell_zd n).conj :=
let t := pell_zd_add n (m - n) in
by rw [nat.add_sub_of_le h] at t;
rw [t, mul_comm (pell_zd _ n) _, mul_assoc, (is_pell_norm _).1 (is_pell_pell_zd _ _), mul_one]
theorem xz_sub {m n} (h : n ≤ m) : xz (m - n) = xz m * xz n - d * yz m * yz n :=
by injection (pell_zd_sub _ h) with h _; repeat {rw ← neg_mul_eq_mul_neg at h}; exact h
theorem yz_sub {m n} (h : n ≤ m) : yz (m - n) = xz n * yz m - xz m * yz n :=
by injection (pell_zd_sub a1 h) with _ h; repeat {rw ← neg_mul_eq_mul_neg at h};
rw [add_comm, mul_comm] at h; exact h
theorem xy_coprime (n) : (xn n).coprime (yn n) :=
nat.coprime_of_dvd' $ λk kp kx ky,
let p := pell_eq n in by rw ← p; exact
nat.dvd_sub (le_of_lt $ nat.lt_of_sub_eq_succ p)
(dvd_mul_of_dvd_right kx _) (dvd_mul_of_dvd_right ky _)
theorem y_increasing {m} : Π {n}, m < n → yn m < yn n
| 0 h := absurd h $ nat.not_lt_zero _
| (n+1) h :=
have yn m ≤ yn n, from or.elim (lt_or_eq_of_le $ nat.le_of_succ_le_succ h)
(λhl, le_of_lt $ y_increasing hl) (λe, by rw e),
by simp; refine lt_of_le_of_lt _ (nat.lt_add_of_pos_left $ x_pos a1 n);
rw ← mul_one (yn a1 m);
exact mul_le_mul this (le_of_lt a1) (nat.zero_le _) (nat.zero_le _)
theorem x_increasing {m} : Π {n}, m < n → xn m < xn n
| 0 h := absurd h $ nat.not_lt_zero _
| (n+1) h :=
have xn m ≤ xn n, from or.elim (lt_or_eq_of_le $ nat.le_of_succ_le_succ h)
(λhl, le_of_lt $ x_increasing hl) (λe, by rw e),
by simp; refine lt_of_lt_of_le (lt_of_le_of_lt this _) (nat.le_add_right _ _);
have t := nat.mul_lt_mul_of_pos_left a1 (x_pos a1 n); rwa mul_one at t
theorem yn_ge_n : Π n, n ≤ yn n
| 0 := nat.zero_le _
| (n+1) := show n < yn (n+1), from lt_of_le_of_lt (yn_ge_n n) (y_increasing $ nat.lt_succ_self n)
theorem y_mul_dvd (n) : ∀k, yn n ∣ yn (n * k)
| 0 := dvd_zero _
| (k+1) := by rw [nat.mul_succ, yn_add]; exact
dvd_add (dvd_mul_left _ _) (dvd_mul_of_dvd_left (y_mul_dvd k) _)
theorem y_dvd_iff (m n) : yn m ∣ yn n ↔ m ∣ n :=
⟨λh, nat.dvd_of_mod_eq_zero $ (nat.eq_zero_or_pos _).resolve_right $ λhp,
have co : nat.coprime (yn m) (xn (m * (n / m))), from nat.coprime.symm $
(xy_coprime _).coprime_dvd_right (y_mul_dvd m (n / m)),
have m0 : 0 < m, from m.eq_zero_or_pos.resolve_left $
λe, by rw [e, nat.mod_zero] at hp; rw [e] at h; exact
ne_of_lt (y_increasing a1 hp) (eq_zero_of_zero_dvd h).symm,
by rw [← nat.mod_add_div n m, yn_add] at h; exact
not_le_of_gt (y_increasing _ $ nat.mod_lt n m0)
(nat.le_of_dvd (y_increasing _ hp) $ co.dvd_of_dvd_mul_right $
(nat.dvd_add_iff_right $ dvd_mul_of_dvd_right (y_mul_dvd _ _ _) _).2 h),
λ⟨k, e⟩, by rw e; apply y_mul_dvd⟩
theorem xy_modeq_yn (n) :
∀k, xn (n * k) ≡ (xn n)^k [MOD (yn n)^2]
∧ yn (n * k) ≡ k * (xn n)^(k-1) * yn n [MOD (yn n)^3]
| 0 := by constructor; simp
| (k+1) :=
let ⟨hx, hy⟩ := xy_modeq_yn k in
have L : xn (n * k) * xn n + d * yn (n * k) * yn n ≡ xn n^k * xn n + 0 [MOD yn n^2], from
modeq.modeq_add (modeq.modeq_mul_right _ hx) $ modeq.modeq_zero_iff.2 $
by rw pow_succ'; exact
mul_dvd_mul_right (dvd_mul_of_dvd_right (modeq.modeq_zero_iff.1 $
(hy.modeq_of_dvd_of_modeq $ by simp [pow_succ']).trans $ modeq.modeq_zero_iff.2 $
by simp [-mul_comm, -mul_assoc]) _) _,
have R : xn (n * k) * yn n + yn (n * k) * xn n ≡
xn n^k * yn n + k * xn n^k * yn n [MOD yn n^3], from
modeq.modeq_add (by rw pow_succ'; exact modeq.modeq_mul_right' _ hx) $
have k * xn n^(k - 1) * yn n * xn n = k * xn n^k * yn n,
by clear _let_match; cases k with k; simp [pow_succ', mul_comm, mul_left_comm],
by rw ← this; exact modeq.modeq_mul_right _ hy,
by rw [nat.add_sub_cancel, nat.mul_succ, xn_add, yn_add, pow_succ' (xn _ n),
nat.succ_mul, add_comm (k * xn _ n^k) (xn _ n^k), right_distrib];
exact ⟨L, R⟩
theorem ysq_dvd_yy (n) : yn n * yn n ∣ yn (n * yn n) :=
modeq.modeq_zero_iff.1 $
((xy_modeq_yn n (yn n)).right.modeq_of_dvd_of_modeq $ by simp [pow_succ]).trans
(modeq.modeq_zero_iff.2 $ by simp [mul_dvd_mul_left, mul_assoc])
theorem dvd_of_ysq_dvd {n t} (h : yn n * yn n ∣ yn t) : yn n ∣ t :=
have nt : n ∣ t, from (y_dvd_iff n t).1 $ dvd_of_mul_left_dvd h,
n.eq_zero_or_pos.elim (λn0, by rw n0; rw n0 at nt; exact nt) $ λ(n0l : 0 < n),
let ⟨k, ke⟩ := nt in
have yn n ∣ k * (xn n)^(k-1), from
nat.dvd_of_mul_dvd_mul_right (y_increasing n0l) $ modeq.modeq_zero_iff.1 $
by have xm := (xy_modeq_yn a1 n k).right; rw ← ke at xm; exact
(xm.modeq_of_dvd_of_modeq $ by simp [pow_succ]).symm.trans
(modeq.modeq_zero_iff.2 h),
by rw ke; exact dvd_mul_of_dvd_right
(((xy_coprime _ _).pow_left _).symm.dvd_of_dvd_mul_right this) _
theorem pell_zd_succ_succ (n) : pell_zd (n + 2) + pell_zd n = (2 * a : ℕ) * pell_zd (n + 1) :=
have (1:ℤ√d) + ⟨a, 1⟩ * ⟨a, 1⟩ = ⟨a, 1⟩ * (2 * a),
by { rw zsqrtd.coe_nat_val, change (⟨_,_⟩:ℤ√(d a1))=⟨_,_⟩,
rw dz_val, change az a1 with a, rw zsqrtd.ext, dsimp, split; ring },
by simpa [mul_add, mul_comm, mul_left_comm, add_comm] using congr_arg (* pell_zd a1 n) this
theorem xy_succ_succ (n) : xn (n + 2) + xn n = (2 * a) * xn (n + 1) ∧
yn (n + 2) + yn n = (2 * a) * yn (n + 1) := begin
have := pell_zd_succ_succ a1 n, unfold pell_zd at this,
rw [← int.cast_coe_nat, zsqrtd.smul_val] at this,
injection this with h₁ h₂,
split; apply int.coe_nat_inj; [simpa using h₁, simpa using h₂]
end
theorem xn_succ_succ (n) : xn (n + 2) + xn n = (2 * a) * xn (n + 1) := (xy_succ_succ n).1
theorem yn_succ_succ (n) : yn (n + 2) + yn n = (2 * a) * yn (n + 1) := (xy_succ_succ n).2
theorem xz_succ_succ (n) : xz (n + 2) = (2 * a : ℕ) * xz (n + 1) - xz n :=
eq_sub_of_add_eq $ by delta xz; rw [← int.coe_nat_add, ← int.coe_nat_mul, xn_succ_succ]
theorem yz_succ_succ (n) : yz (n + 2) = (2 * a : ℕ) * yz (n + 1) - yz n :=
eq_sub_of_add_eq $ by delta yz; rw [← int.coe_nat_add, ← int.coe_nat_mul, yn_succ_succ]
theorem yn_modeq_a_sub_one : ∀ n, yn n ≡ n [MOD a-1]
| 0 := by simp
| 1 := by simp
| (n+2) := modeq.modeq_add_cancel_right (yn_modeq_a_sub_one n) $
have 2*(n+1) = n+2+n, by ring,
by rw [yn_succ_succ, ← this];
refine modeq.modeq_mul (modeq.modeq_mul_left 2 (_ : a ≡ 1 [MOD a-1]))
(yn_modeq_a_sub_one (n+1));
exact (modeq.modeq_of_dvd $ by rw [int.coe_nat_sub $ le_of_lt a1]; apply dvd_refl).symm
theorem yn_modeq_two : ∀ n, yn n ≡ n [MOD 2]
| 0 := by simp
| 1 := by simp
| (n+2) := modeq.modeq_add_cancel_right (yn_modeq_two n) $
have 2*(n+1) = n+2+n, by ring,
by rw [yn_succ_succ, ← this];
refine modeq.modeq_mul _ (yn_modeq_two (n+1));
exact modeq.trans
(modeq.modeq_zero_iff.2 $ by simp)
(modeq.modeq_zero_iff.2 $ by simp).symm
lemma x_sub_y_dvd_pow_lem (y2 y1 y0 yn1 yn0 xn1 xn0 ay a2 : ℤ) :
(a2 * yn1 - yn0) * ay + y2 - (a2 * xn1 - xn0) =
y2 - a2 * y1 + y0 + a2 * (yn1 * ay + y1 - xn1) - (yn0 * ay + y0 - xn0) := by ring
theorem x_sub_y_dvd_pow (y : ℕ) :
∀ n, (2*a*y - y*y - 1 : ℤ) ∣ yz n * (a - y) + ↑(y^n) - xz n
| 0 := by simp [xz, yz, int.coe_nat_zero, int.coe_nat_one]
| 1 := by simp [xz, yz, int.coe_nat_zero, int.coe_nat_one]
| (n+2) :=
have (2*a*y - y*y - 1 : ℤ) ∣ ↑(y^(n + 2)) - ↑(2 * a) * ↑(y^(n + 1)) + ↑(y^n), from
⟨-↑(y^n), by simp [pow_succ, mul_add, int.coe_nat_mul,
show ((2:ℕ):ℤ) = 2, from rfl, mul_comm, mul_left_comm]; ring ⟩,
by rw [xz_succ_succ, yz_succ_succ, x_sub_y_dvd_pow_lem a1 ↑(y^(n+2)) ↑(y^(n+1)) ↑(y^n)]; exact
dvd_sub (dvd_add this $ dvd_mul_of_dvd_right (x_sub_y_dvd_pow (n+1)) _) (x_sub_y_dvd_pow n)
theorem xn_modeq_x2n_add_lem (n j) : xn n ∣ d * yn n * (yn n * xn j) + xn j :=
have h1 : d * yn n * (yn n * xn j) + xn j = (d * yn n * yn n + 1) * xn j,
by simp [add_mul, mul_assoc],
have h2 : d * yn n * yn n + 1 = xn n * xn n, by apply int.coe_nat_inj;
repeat {rw int.coe_nat_add <|> rw int.coe_nat_mul}; exact
add_eq_of_eq_sub' (eq.symm $ pell_eqz _ _),
by rw h2 at h1; rw [h1, mul_assoc]; exact dvd_mul_right _ _
theorem xn_modeq_x2n_add (n j) : xn (2 * n + j) + xn j ≡ 0 [MOD xn n] :=
by rw [two_mul, add_assoc, xn_add, add_assoc]; exact
show _ ≡ 0+0 [MOD xn a1 n],
from modeq.modeq_add (modeq.modeq_zero_iff.2 $ dvd_mul_right (xn a1 n) (xn a1 (n + j))) $
by rw [yn_add, left_distrib, add_assoc]; exact
show _ ≡ 0+0 [MOD xn a1 n],
from modeq.modeq_add (modeq.modeq_zero_iff.2 $ dvd_mul_of_dvd_right (dvd_mul_right _ _) _) $
modeq.modeq_zero_iff.2 $ xn_modeq_x2n_add_lem _ _ _
lemma xn_modeq_x2n_sub_lem {n j} (h : j ≤ n) : xn (2 * n - j) + xn j ≡ 0 [MOD xn n] :=
have h1 : xz n ∣ ↑d * yz n * yz (n - j) + xz j,
by rw [yz_sub _ h, mul_sub_left_distrib, sub_add_eq_add_sub]; exact
dvd_sub
(by delta xz; delta yz;
repeat {rw ← int.coe_nat_add <|> rw ← int.coe_nat_mul}; rw mul_comm (xn a1 j) (yn a1 n);
exact int.coe_nat_dvd.2 (xn_modeq_x2n_add_lem _ _ _))
(dvd_mul_of_dvd_right (dvd_mul_right _ _) _),
by rw [two_mul, nat.add_sub_assoc h, xn_add, add_assoc]; exact
show _ ≡ 0+0 [MOD xn a1 n], from modeq.modeq_add (modeq.modeq_zero_iff.2 $ dvd_mul_right _ _) $
modeq.modeq_zero_iff.2 $ int.coe_nat_dvd.1 $ by simpa [xz, yz] using h1
theorem xn_modeq_x2n_sub {n j} (h : j ≤ 2 * n) : xn (2 * n - j) + xn j ≡ 0 [MOD xn n] :=
(le_total j n).elim xn_modeq_x2n_sub_lem
(λjn, have 2 * n - j + j ≤ n + j, by rw [nat.sub_add_cancel h, two_mul];
exact nat.add_le_add_left jn _,
let t := xn_modeq_x2n_sub_lem (nat.le_of_add_le_add_right this) in
by rwa [nat.sub_sub_self h, add_comm] at t)
theorem xn_modeq_x4n_add (n j) : xn (4 * n + j) ≡ xn j [MOD xn n] :=
modeq.modeq_add_cancel_right (modeq.refl $ xn (2 * n + j)) $
by refine @modeq.trans _ _ 0 _ _ (by rw add_comm; exact (xn_modeq_x2n_add _ _ _).symm);
rw [show 4*n = 2*n + 2*n, from right_distrib 2 2 n, add_assoc]; apply xn_modeq_x2n_add
theorem xn_modeq_x4n_sub {n j} (h : j ≤ 2 * n) : xn (4 * n - j) ≡ xn j [MOD xn n] :=
have h' : j ≤ 2*n, from le_trans h (by rw nat.succ_mul; apply nat.le_add_left),
modeq.modeq_add_cancel_right (modeq.refl $ xn (2 * n - j)) $
by refine @modeq.trans _ _ 0 _ _ (by rw add_comm; exact (xn_modeq_x2n_sub _ h).symm);
rw [show 4*n = 2*n + 2*n, from right_distrib 2 2 n, nat.add_sub_assoc h'];
apply xn_modeq_x2n_add
theorem eq_of_xn_modeq_lem1 {i n} : Π {j}, i < j → j < n → xn i % xn n < xn j % xn n
| 0 ij _ := absurd ij (nat.not_lt_zero _)
| (j+1) ij jn :=
suffices xn j % xn n < xn (j + 1) % xn n, from
(lt_or_eq_of_le (nat.le_of_succ_le_succ ij)).elim
(λh, lt_trans (eq_of_xn_modeq_lem1 h (le_of_lt jn)) this)
(λh, by rw h; exact this),
by rw [nat.mod_eq_of_lt (x_increasing _ (nat.lt_of_succ_lt jn)),
nat.mod_eq_of_lt (x_increasing _ jn)];
exact x_increasing _ (nat.lt_succ_self _)
theorem eq_of_xn_modeq_lem2 {n} (h : 2 * xn n = xn (n + 1)) : a = 2 ∧ n = 0 :=
by rw [xn_succ, mul_comm] at h; exact
have n = 0, from n.eq_zero_or_pos.resolve_right $ λnp,
ne_of_lt (lt_of_le_of_lt (nat.mul_le_mul_left _ a1)
(nat.lt_add_of_pos_right $ mul_pos (d_pos a1) (y_increasing a1 np))) h,
by cases this; simp at h; exact ⟨h.symm, rfl⟩
theorem eq_of_xn_modeq_lem3 {i n} (npos : 0 < n) :
Π {j}, i < j → j ≤ 2 * n → j ≠ n → ¬(a = 2 ∧ n = 1 ∧ i = 0 ∧ j = 2) → xn i % xn n < xn j % xn n
| 0 ij _ _ _ := absurd ij (nat.not_lt_zero _)
| (j+1) ij j2n jnn ntriv :=
have lem2 : ∀k > n, k ≤ 2*n → (↑(xn k % xn n) : ℤ) = xn n - xn (2 * n - k), from λk kn k2n,
let k2nl := lt_of_add_lt_add_right $ show 2*n-k+k < n+k, by
{rw nat.sub_add_cancel, rw two_mul; exact (add_lt_add_left kn n), exact k2n } in
have xle : xn (2 * n - k) ≤ xn n, from le_of_lt $ x_increasing k2nl,
suffices xn k % xn n = xn n - xn (2 * n - k), by rw [this, int.coe_nat_sub xle],
by {
rw ← nat.mod_eq_of_lt (nat.sub_lt (x_pos a1 n) (x_pos a1 (2 * n - k))),
apply modeq.modeq_add_cancel_right (modeq.refl (xn a1 (2 * n - k))),
rw [nat.sub_add_cancel xle],
have t := xn_modeq_x2n_sub_lem a1 (le_of_lt k2nl),
rw nat.sub_sub_self k2n at t,
exact t.trans (modeq.modeq_zero_iff.2 $ dvd_refl _).symm },
(lt_trichotomy j n).elim
(λ (jn : j < n), eq_of_xn_modeq_lem1 ij (lt_of_le_of_ne jn jnn)) $ λo, o.elim
(λ (jn : j = n), by {
cases jn,
apply int.lt_of_coe_nat_lt_coe_nat,
rw [lem2 (n+1) (nat.lt_succ_self _) j2n,
show 2 * n - (n + 1) = n - 1, by rw[two_mul, ← nat.sub_sub, nat.add_sub_cancel]],
refine lt_sub_left_of_add_lt (int.coe_nat_lt_coe_nat_of_lt _),
cases (lt_or_eq_of_le $ nat.le_of_succ_le_succ ij) with lin ein,
{ rw nat.mod_eq_of_lt (x_increasing _ lin),
have ll : xn a1 (n-1) + xn a1 (n-1) ≤ xn a1 n,
{ rw [← two_mul, mul_comm, show xn a1 n = xn a1 (n-1+1),
by rw [nat.sub_add_cancel npos], xn_succ],
exact le_trans (nat.mul_le_mul_left _ a1) (nat.le_add_right _ _) },
have npm : (n-1).succ = n := nat.succ_pred_eq_of_pos npos,
have il : i ≤ n - 1, { apply nat.le_of_succ_le_succ, rw npm, exact lin },
cases lt_or_eq_of_le il with ill ile,
{ exact lt_of_lt_of_le (nat.add_lt_add_left (x_increasing a1 ill) _) ll },
{ rw ile,
apply lt_of_le_of_ne ll,
rw ← two_mul,
exact λe, ntriv $
let ⟨a2, s1⟩ := @eq_of_xn_modeq_lem2 _ a1 (n-1) (by rwa [nat.sub_add_cancel npos]) in
have n1 : n = 1, from le_antisymm (nat.le_of_sub_eq_zero s1) npos,
by rw [ile, a2, n1]; exact ⟨rfl, rfl, rfl, rfl⟩ } },
{ rw [ein, nat.mod_self, add_zero],
exact x_increasing _ (nat.pred_lt $ ne_of_gt npos) } })
(λ (jn : j > n),
have lem1 : j ≠ n → xn j % xn n < xn (j + 1) % xn n → xn i % xn n < xn (j + 1) % xn n,
from λjn s,
(lt_or_eq_of_le (nat.le_of_succ_le_succ ij)).elim
(λh, lt_trans (eq_of_xn_modeq_lem3 h (le_of_lt j2n) jn $ λ⟨a1, n1, i0, j2⟩,
by rw [n1, j2] at j2n; exact absurd j2n dec_trivial) s)
(λh, by rw h; exact s),
lem1 (ne_of_gt jn) $ int.lt_of_coe_nat_lt_coe_nat $ by {
rw [lem2 j jn (le_of_lt j2n), lem2 (j+1) (nat.le_succ_of_le jn) j2n],
refine sub_lt_sub_left (int.coe_nat_lt_coe_nat_of_lt $ x_increasing _ _) _,
rw [nat.sub_succ],
exact nat.pred_lt (ne_of_gt $ nat.sub_pos_of_lt j2n) })
theorem eq_of_xn_modeq_le {i j n} (npos : 0 < n) (ij : i ≤ j) (j2n : j ≤ 2 * n)
(h : xn i ≡ xn j [MOD xn n]) (ntriv : ¬(a = 2 ∧ n = 1 ∧ i = 0 ∧ j = 2)) : i = j :=
(lt_or_eq_of_le ij).resolve_left $ λij',
if jn : j = n then by {
refine ne_of_gt _ h,
rw [jn, nat.mod_self],
have x0 : 0 < xn a1 0 % xn a1 n := by rw [nat.mod_eq_of_lt (x_increasing a1 npos)];
exact dec_trivial,
cases i with i, exact x0,
rw jn at ij',
exact x0.trans (eq_of_xn_modeq_lem3 _ npos (nat.succ_pos _) (le_trans ij j2n) (ne_of_lt ij') $
λ⟨a1, n1, _, i2⟩, by rw [n1, i2] at ij'; exact absurd ij' dec_trivial)
} else ne_of_lt (eq_of_xn_modeq_lem3 npos ij' j2n jn ntriv) h
theorem eq_of_xn_modeq {i j n} (npos : 0 < n) (i2n : i ≤ 2 * n) (j2n : j ≤ 2 * n)
(h : xn i ≡ xn j [MOD xn n]) (ntriv : a = 2 → n = 1 → (i = 0 → j ≠ 2) ∧ (i = 2 → j ≠ 0)) :
i = j :=
(le_total i j).elim
(λij, eq_of_xn_modeq_le npos ij j2n h $ λ⟨a2, n1, i0, j2⟩, (ntriv a2 n1).left i0 j2)
(λij, (eq_of_xn_modeq_le npos ij i2n h.symm $ λ⟨a2, n1, j0, i2⟩,
(ntriv a2 n1).right i2 j0).symm)
theorem eq_of_xn_modeq' {i j n} (ipos : 0 < i) (hin : i ≤ n) (j4n : j ≤ 4 * n)
(h : xn j ≡ xn i [MOD xn n]) : j = i ∨ j + i = 4 * n :=
have i2n : i ≤ 2*n, by apply le_trans hin; rw two_mul; apply nat.le_add_left,
have npos : 0 < n, from lt_of_lt_of_le ipos hin,
(le_or_gt j (2 * n)).imp
(λj2n : j ≤ 2 * n, eq_of_xn_modeq npos j2n i2n h $
λa2 n1, ⟨λj0 i2, by rw [n1, i2] at hin; exact absurd hin dec_trivial,
λj2 i0, ne_of_gt ipos i0⟩)
(λj2n : 2 * n < j, suffices i = 4*n - j, by rw [this, nat.add_sub_of_le j4n],
have j42n : 4*n - j ≤ 2*n, from @nat.le_of_add_le_add_right j _ _ $
by rw [nat.sub_add_cancel j4n, show 4*n = 2*n + 2*n, from right_distrib 2 2 n];
exact nat.add_le_add_left (le_of_lt j2n) _,
eq_of_xn_modeq npos i2n j42n
(h.symm.trans $ let t := xn_modeq_x4n_sub j42n in by rwa [nat.sub_sub_self j4n] at t)
(λa2 n1, ⟨λi0, absurd i0 (ne_of_gt ipos), λi2, by { rw [n1, i2] at hin,
exact absurd hin dec_trivial }⟩))
theorem modeq_of_xn_modeq {i j n} (ipos : 0 < i) (hin : i ≤ n) (h : xn j ≡ xn i [MOD xn n]) :
j ≡ i [MOD 4 * n] ∨ j + i ≡ 0 [MOD 4 * n] :=
let j' := j % (4 * n) in
have n4 : 0 < 4 * n, from mul_pos dec_trivial (lt_of_lt_of_le ipos hin),
have jl : j' < 4 * n, from nat.mod_lt _ n4,
have jj : j ≡ j' [MOD 4 * n], by delta modeq; rw nat.mod_eq_of_lt jl,
have ∀j q, xn (j + 4 * n * q) ≡ xn j [MOD xn n], begin
intros j q, induction q with q IH, { simp },
rw[nat.mul_succ, ← add_assoc, add_comm],
exact modeq.trans (xn_modeq_x4n_add _ _ _) IH
end,
or.imp
(λ(ji : j' = i), by rwa ← ji)
(λ(ji : j' + i = 4 * n), (modeq.modeq_add jj (modeq.refl _)).trans $
by rw ji; exact modeq.modeq_zero_iff.2 (dvd_refl _))
(eq_of_xn_modeq' ipos hin (le_of_lt jl) $
(modeq.symm (by rw ← nat.mod_add_div j (4*n); exact this j' _)).trans h)
end
theorem xy_modeq_of_modeq {a b c} (a1 : 1 < a) (b1 : 1 < b) (h : a ≡ b [MOD c]) :
∀ n, xn a1 n ≡ xn b1 n [MOD c] ∧ yn a1 n ≡ yn b1 n [MOD c]
| 0 := by constructor; refl
| 1 := by simp; exact ⟨h, modeq.refl 1⟩
| (n+2) := ⟨
modeq.modeq_add_cancel_right (xy_modeq_of_modeq n).left $
by rw [xn_succ_succ a1, xn_succ_succ b1]; exact
modeq.modeq_mul (modeq.modeq_mul_left _ h) (xy_modeq_of_modeq (n+1)).left,
modeq.modeq_add_cancel_right (xy_modeq_of_modeq n).right $
by rw [yn_succ_succ a1, yn_succ_succ b1]; exact
modeq.modeq_mul (modeq.modeq_mul_left _ h) (xy_modeq_of_modeq (n+1)).right⟩
theorem matiyasevic {a k x y} : (∃ a1 : 1 < a, xn a1 k = x ∧ yn a1 k = y) ↔
1 < a ∧ k ≤ y ∧
(x = 1 ∧ y = 0 ∨
∃ (u v s t b : ℕ),
x * x - (a * a - 1) * y * y = 1 ∧
u * u - (a * a - 1) * v * v = 1 ∧
s * s - (b * b - 1) * t * t = 1 ∧
1 < b ∧ b ≡ 1 [MOD 4 * y] ∧ b ≡ a [MOD u] ∧
0 < v ∧ y * y ∣ v ∧
s ≡ x [MOD u] ∧
t ≡ k [MOD 4 * y]) :=
⟨λ⟨a1, hx, hy⟩, by rw [← hx, ← hy];
refine ⟨a1, (nat.eq_zero_or_pos k).elim
(λk0, by rw k0; exact ⟨le_refl _, or.inl ⟨rfl, rfl⟩⟩) (λkpos, _)⟩; exact
let x := xn a1 k, y := yn a1 k,
m := 2 * (k * y),
u := xn a1 m, v := yn a1 m in
have ky : k ≤ y, from yn_ge_n a1 k,
have yv : y * y ∣ v, from dvd_trans (ysq_dvd_yy a1 k) $
(y_dvd_iff _ _ _).2 $ dvd_mul_left _ _,
have uco : nat.coprime u (4 * y), from
have 2 ∣ v, from modeq.modeq_zero_iff.1 $ (yn_modeq_two _ _).trans $
modeq.modeq_zero_iff.2 (dvd_mul_right _ _),
have nat.coprime u 2, from
(xy_coprime a1 m).coprime_dvd_right this,
(this.mul_right this).mul_right $
(xy_coprime _ _).coprime_dvd_right (dvd_of_mul_left_dvd yv),
let ⟨b, ba, bm1⟩ := modeq.chinese_remainder uco a 1 in
have m1 : 1 < m, from
have 0 < k * y, from mul_pos kpos (y_increasing a1 kpos),
nat.mul_le_mul_left 2 this,
have vp : 0 < v, from y_increasing a1 (lt_trans zero_lt_one m1),
have b1 : 1 < b, from
have xn a1 1 < u, from x_increasing a1 m1,
have a < u, by simp at this; exact this,
lt_of_lt_of_le a1 $ by delta modeq at ba;
rw nat.mod_eq_of_lt this at ba; rw ← ba; apply nat.mod_le,
let s := xn b1 k, t := yn b1 k in
have sx : s ≡ x [MOD u], from (xy_modeq_of_modeq b1 a1 ba k).left,
have tk : t ≡ k [MOD 4 * y], from
have 4 * y ∣ b - 1, from int.coe_nat_dvd.1 $
by rw int.coe_nat_sub (le_of_lt b1);
exact modeq.dvd_of_modeq bm1.symm,
modeq.modeq_of_dvd_of_modeq this $ yn_modeq_a_sub_one _ _,
⟨ky, or.inr ⟨u, v, s, t, b,
pell_eq _ _, pell_eq _ _, pell_eq _ _, b1, bm1, ba, vp, yv, sx, tk⟩⟩,
λ⟨a1, ky, o⟩, ⟨a1, match o with
| or.inl ⟨x1, y0⟩ := by rw y0 at ky; rw [nat.eq_zero_of_le_zero ky, x1, y0]; exact ⟨rfl, rfl⟩
| or.inr ⟨u, v, s, t, b, xy, uv, st, b1, rem⟩ :=
match x, y, eq_pell a1 xy, u, v, eq_pell a1 uv, s, t, eq_pell b1 st, rem, ky with
| ._, ._, ⟨i, rfl, rfl⟩, ._, ._, ⟨n, rfl, rfl⟩, ._, ._, ⟨j, rfl, rfl⟩,
⟨(bm1 : b ≡ 1 [MOD 4 * yn a1 i]),
(ba : b ≡ a [MOD xn a1 n]),
(vp : 0 < yn a1 n),
(yv : yn a1 i * yn a1 i ∣ yn a1 n),
(sx : xn b1 j ≡ xn a1 i [MOD xn a1 n]),
(tk : yn b1 j ≡ k [MOD 4 * yn a1 i])⟩,
(ky : k ≤ yn a1 i) :=
(nat.eq_zero_or_pos i).elim
(λi0, by simp [i0] at ky; rw [i0, ky]; exact ⟨rfl, rfl⟩) $ λipos,
suffices i = k, by rw this; exact ⟨rfl, rfl⟩,
by clear _x o rem xy uv st _match _match _fun_match; exact
have iln : i ≤ n, from le_of_not_gt $ λhin,
not_lt_of_ge (nat.le_of_dvd vp (dvd_of_mul_left_dvd yv)) (y_increasing a1 hin),
have yd : 4 * yn a1 i ∣ 4 * n, from mul_dvd_mul_left _ $ dvd_of_ysq_dvd a1 yv,
have jk : j ≡ k [MOD 4 * yn a1 i], from
have 4 * yn a1 i ∣ b - 1, from int.coe_nat_dvd.1 $
by rw int.coe_nat_sub (le_of_lt b1); exact modeq.dvd_of_modeq bm1.symm,
(modeq.modeq_of_dvd_of_modeq this (yn_modeq_a_sub_one b1 _)).symm.trans tk,
have ki : k + i < 4 * yn a1 i, from
lt_of_le_of_lt (add_le_add ky (yn_ge_n a1 i)) $
by rw ← two_mul; exact nat.mul_lt_mul_of_pos_right dec_trivial (y_increasing a1 ipos),
have ji : j ≡ i [MOD 4 * n], from
have xn a1 j ≡ xn a1 i [MOD xn a1 n], from (xy_modeq_of_modeq b1 a1 ba j).left.symm.trans sx,
(modeq_of_xn_modeq a1 ipos iln this).resolve_right $ λ (ji : j + i ≡ 0 [MOD 4 * n]),
not_le_of_gt ki $ nat.le_of_dvd (lt_of_lt_of_le ipos $ nat.le_add_left _ _) $
modeq.modeq_zero_iff.1 $ (modeq.modeq_add jk.symm (modeq.refl i)).trans $
modeq.modeq_of_dvd_of_modeq yd ji,
by have : i % (4 * yn a1 i) = k % (4 * yn a1 i) :=
(modeq.modeq_of_dvd_of_modeq yd ji).symm.trans jk;
rwa [nat.mod_eq_of_lt (lt_of_le_of_lt (nat.le_add_left _ _) ki),
nat.mod_eq_of_lt (lt_of_le_of_lt (nat.le_add_right _ _) ki)] at this
end
end⟩⟩
lemma eq_pow_of_pell_lem {a y k} (a1 : 1 < a) (ypos : 0 < y) : 0 < k → y^k < a →
(↑(y^k) : ℤ) < 2*a*y - y*y - 1 :=
have y < a → a + (y*y + 1) ≤ 2*a*y, begin
intro ya, induction y with y IH, exact absurd ypos (lt_irrefl _),
cases nat.eq_zero_or_pos y with y0 ypos,
{ rw y0, simpa [two_mul], },
{ rw [nat.mul_succ, nat.mul_succ, nat.succ_mul y],
have : y + nat.succ y ≤ 2 * a,
{ change y + y < 2 * a, rw ← two_mul,
exact mul_lt_mul_of_pos_left (nat.lt_of_succ_lt ya) dec_trivial },
have := add_le_add (IH ypos (nat.lt_of_succ_lt ya)) this,
convert this using 1,
ring }
end, λk0 yak,
lt_of_lt_of_le (int.coe_nat_lt_coe_nat_of_lt yak) $
by rw sub_sub; apply le_sub_right_of_add_le;
apply int.coe_nat_le_coe_nat_of_le;
have y1 := nat.pow_le_pow_of_le_right ypos k0; simp at y1;
exact this (lt_of_le_of_lt y1 yak)
theorem eq_pow_of_pell {m n k} : (n^k = m ↔
k = 0 ∧ m = 1 ∨ 0 < k ∧
(n = 0 ∧ m = 0 ∨ 0 < n ∧
∃ (w a t z : ℕ) (a1 : 1 < a),
xn a1 k ≡ yn a1 k * (a - n) + m [MOD t] ∧
2 * a * n = t + (n * n + 1) ∧
m < t ∧ n ≤ w ∧ k ≤ w ∧
a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1)) :=
⟨λe, by rw ← e;
refine (nat.eq_zero_or_pos k).elim
(λk0, by rw k0; exact or.inl ⟨rfl, rfl⟩)
(λkpos, or.inr ⟨kpos, _⟩);
refine (nat.eq_zero_or_pos n).elim
(λn0, by rw [n0, zero_pow kpos]; exact or.inl ⟨rfl, rfl⟩)
(λnpos, or.inr ⟨npos, _⟩); exact
let w := _root_.max n k in
have nw : n ≤ w, from le_max_left _ _,
have kw : k ≤ w, from le_max_right _ _,
have wpos : 0 < w, from lt_of_lt_of_le npos nw,
have w1 : 1 < w + 1, from nat.succ_lt_succ wpos,
let a := xn w1 w in
have a1 : 1 < a, from x_increasing w1 wpos,
let x := xn a1 k, y := yn a1 k in
let ⟨z, ze⟩ := show w ∣ yn w1 w, from modeq.modeq_zero_iff.1 $
modeq.trans (yn_modeq_a_sub_one w1 w) (modeq.modeq_zero_iff.2 $ dvd_refl _) in
have nt : (↑(n^k) : ℤ) < 2 * a * n - n * n - 1, from
eq_pow_of_pell_lem a1 npos kpos $ calc
n^k ≤ n^w : nat.pow_le_pow_of_le_right npos kw
... < (w + 1)^w : nat.pow_lt_pow_of_lt_left (nat.lt_succ_of_le nw) wpos
... ≤ a : xn_ge_a_pow w1 w,
let ⟨t, te⟩ := int.eq_coe_of_zero_le $
le_trans (int.coe_zero_le _) $ le_of_lt nt in
have na : n ≤ a, from le_trans nw $ le_of_lt $ n_lt_xn w1 w,
have tm : x ≡ y * (a - n) + n^k [MOD t], begin
apply modeq.modeq_of_dvd,
rw [int.coe_nat_add, int.coe_nat_mul, int.coe_nat_sub na, ← te],
exact x_sub_y_dvd_pow a1 n k
end,
have ta : 2 * a * n = t + (n * n + 1), from int.coe_nat_inj $
by rw [int.coe_nat_add, ← te, sub_sub];
repeat {rw int.coe_nat_add <|> rw int.coe_nat_mul};
rw [int.coe_nat_one, sub_add_cancel]; refl,
have mt : n^k < t, from int.lt_of_coe_nat_lt_coe_nat $
by rw ← te; exact nt,
have zp : a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1,
by rw ← ze; exact pell_eq w1 w,
⟨w, a, t, z, a1, tm, ta, mt, nw, kw, zp⟩,
λo, match o with
| or.inl ⟨k0, m1⟩ := by rw [k0, m1]; refl
| or.inr ⟨kpos, or.inl ⟨n0, m0⟩⟩ := by rw [n0, m0, zero_pow kpos]
| or.inr ⟨kpos, or.inr ⟨npos, w, a, t, z,
(a1 : 1 < a),
(tm : xn a1 k ≡ yn a1 k * (a - n) + m [MOD t]),
(ta : 2 * a * n = t + (n * n + 1)),
(mt : m < t),
(nw : n ≤ w),
(kw : k ≤ w),
(zp : a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1)⟩⟩ :=
have wpos : 0 < w, from lt_of_lt_of_le npos nw,
have w1 : 1 < w + 1, from nat.succ_lt_succ wpos,
let ⟨j, xj, yj⟩ := eq_pell w1 zp in
by clear _match o _let_match; exact
have jpos : 0 < j, from (nat.eq_zero_or_pos j).resolve_left $ λj0,
have a1 : a = 1, by rw j0 at xj; exact xj,
have 2 * n = t + (n * n + 1), by rw a1 at ta; exact ta,
have n1 : n = 1, from
have n * n < n * 2, by rw [mul_comm n 2, this]; apply nat.le_add_left,
have n ≤ 1, from nat.le_of_lt_succ $ lt_of_mul_lt_mul_left this (nat.zero_le _),
le_antisymm this npos,
by rw n1 at this;
rw ← @nat.add_right_cancel 0 2 t this at mt;
exact nat.not_lt_zero _ mt,
have wj : w ≤ j, from nat.le_of_dvd jpos $ modeq.modeq_zero_iff.1 $
(yn_modeq_a_sub_one w1 j).symm.trans $
modeq.modeq_zero_iff.2 ⟨z, yj.symm⟩,
have nt : (↑(n^k) : ℤ) < 2 * a * n - n * n - 1, from
eq_pow_of_pell_lem a1 npos kpos $ calc
n^k ≤ n^j : nat.pow_le_pow_of_le_right npos (le_trans kw wj)
... < (w + 1)^j : nat.pow_lt_pow_of_lt_left (nat.lt_succ_of_le nw) jpos
... ≤ xn w1 j : xn_ge_a_pow w1 j
... = a : xj.symm,
have na : n ≤ a, by rw xj; exact
le_trans (le_trans nw wj) (le_of_lt $ n_lt_xn _ _),
have te : (t : ℤ) = 2 * ↑a * ↑n - ↑n * ↑n - 1, by
rw sub_sub; apply eq_sub_of_add_eq; apply (int.coe_nat_eq_coe_nat_iff _ _).2;
exact ta.symm,
have xn a1 k ≡ yn a1 k * (a - n) + n^k [MOD t],
by have := x_sub_y_dvd_pow a1 n k;
rw [← te, ← int.coe_nat_sub na] at this; exact modeq.modeq_of_dvd this,
have n^k % t = m % t, from
modeq.modeq_add_cancel_left (modeq.refl _) (this.symm.trans tm),
by rw ← te at nt;
rwa [nat.mod_eq_of_lt (int.lt_of_coe_nat_lt_coe_nat nt), nat.mod_eq_of_lt mt] at this
end⟩
end pell
|
b145f43d7268b88fa6fed121d9e99dec0201b2dc | 0003047346476c031128723dfd16fe273c6bc605 | /src/group_theory/perm/cycles.lean | 666199cdab3890cdb5f10d12c7a8c2fbb56042da | [
"Apache-2.0"
] | permissive | ChandanKSingh/mathlib | d2bf4724ccc670bf24915c12c475748281d3fb73 | d60d1616958787ccb9842dc943534f90ea0bab64 | refs/heads/master | 1,588,238,823,679 | 1,552,867,469,000 | 1,552,867,469,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,480 | 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 group_theory.perm.sign group_theory.order_of_element
namespace equiv.perm
open equiv function finset
variables {α : Type*} {β : Type*} [decidable_eq α]
def same_cycle (f : perm β) (x y : β) := ∃ i : ℤ, (f ^ i) x = y
@[refl] lemma same_cycle.refl (f : perm β) (x : β) : same_cycle f x x := ⟨0, rfl⟩
@[symm] lemma same_cycle.symm (f : perm β) {x y : β} : same_cycle f x y → same_cycle f y x :=
λ ⟨i, hi⟩, ⟨-i, by rw [gpow_neg, ← hi, inv_apply_self]⟩
@[trans] lemma same_cycle.trans (f : perm β) {x y z : β} :
same_cycle f x y → same_cycle f y z → same_cycle f x z :=
λ ⟨i, hi⟩ ⟨j, hj⟩, ⟨j + i, by rw [gpow_add, mul_apply, hi, hj]⟩
lemma apply_eq_self_iff_of_same_cycle {f : perm β} {x y : β} :
same_cycle f x y → (f x = x ↔ f y = y) :=
λ ⟨i, hi⟩, by rw [← hi, ← mul_apply, ← gpow_one_add, add_comm, gpow_add_one, mul_apply,
(f ^ i).bijective.1.eq_iff]
lemma same_cycle_of_is_cycle {f : perm β} (hf : is_cycle f) {x y : β}
(hx : f x ≠ x) (hy : f y ≠ y) : same_cycle f x y :=
exists_gpow_eq_of_is_cycle hf hx hy
instance [fintype α] (f : perm α) : decidable_rel (same_cycle f) :=
λ x y, decidable_of_iff (∃ n ∈ list.range (order_of f), (f ^ n) x = y)
⟨λ ⟨n, _, hn⟩, ⟨n, hn⟩, λ ⟨i, hi⟩, ⟨(i % order_of f).nat_abs, list.mem_range.2
(int.coe_nat_lt.1 $
by rw int.nat_abs_of_nonneg (int.mod_nonneg _
(int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _)));
exact calc _ < _ : int.mod_lt _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))
... = _ : by simp),
by rw [← gpow_coe_nat, int.nat_abs_of_nonneg (int.mod_nonneg _
(int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))), ← gpow_eq_mod_order_of, hi]⟩⟩
lemma same_cycle_apply {f : perm β} {x y : β} : same_cycle f x (f y) ↔ same_cycle f x y :=
⟨λ ⟨i, hi⟩, ⟨-1 + i, by rw [gpow_add, mul_apply, hi, gpow_neg_one, inv_apply_self]⟩,
λ ⟨i, hi⟩, ⟨1 + i, by rw [gpow_add, mul_apply, hi, gpow_one]⟩⟩
lemma same_cycle_cycle {f : perm β} {x : β} (hx : f x ≠ x) : is_cycle f ↔
(∀ {y}, same_cycle f x y ↔ f y ≠ y) :=
⟨λ hf y, ⟨λ ⟨i, hi⟩ hy, hx $
by rw [← gpow_apply_eq_self_of_apply_eq_self hy i, (f ^ i).bijective.1.eq_iff] at hi;
rw [hi, hy],
exists_gpow_eq_of_is_cycle hf hx⟩,
λ h, ⟨x, hx, λ y hy, h.2 hy⟩⟩
lemma same_cycle_inv (f : perm β) {x y : β} : same_cycle f⁻¹ x y ↔ same_cycle f x y :=
⟨λ ⟨i, hi⟩, ⟨-i, by rw [gpow_neg, ← inv_gpow, hi]⟩,
λ ⟨i, hi⟩, ⟨-i, by rw [gpow_neg, ← inv_gpow, inv_inv, hi]⟩ ⟩
lemma same_cycle_inv_apply {f : perm β} {x y : β} : same_cycle f x (f⁻¹ y) ↔ same_cycle f x y :=
by rw [← same_cycle_inv, same_cycle_apply, same_cycle_inv]
def cycle_of [fintype α] (f : perm α) (x : α) : perm α :=
of_subtype (@subtype_perm _ f (same_cycle f x) (λ _, same_cycle_apply.symm))
lemma cycle_of_apply [fintype α] (f : perm α) (x y : α) :
cycle_of f x y = if same_cycle f x y then f y else y := rfl
lemma cycle_of_inv [fintype α] (f : perm α) (x : α) :
(cycle_of f x)⁻¹ = cycle_of f⁻¹ x :=
equiv.ext _ _ $ λ y, begin
rw [inv_eq_iff_eq, cycle_of_apply, cycle_of_apply];
split_ifs; simp [*, same_cycle_inv, same_cycle_inv_apply] at *
end
@[simp] lemma cycle_of_pow_apply_self [fintype α] (f : perm α) (x : α) :
∀ n : ℕ, (cycle_of f x ^ n) x = (f ^ n) x
| 0 := rfl
| (n+1) := by rw [pow_succ, mul_apply, cycle_of_apply,
cycle_of_pow_apply_self, if_pos, pow_succ, mul_apply];
exact ⟨n, rfl⟩
@[simp] lemma cycle_of_gpow_apply_self [fintype α] (f : perm α) (x : α) :
∀ n : ℤ, (cycle_of f x ^ n) x = (f ^ n) x
| (n : ℕ) := cycle_of_pow_apply_self f x n
| -[1+ n] := by rw [gpow_neg_succ, ← inv_pow, cycle_of_inv,
gpow_neg_succ, ← inv_pow, cycle_of_pow_apply_self]
lemma cycle_of_apply_of_same_cycle [fintype α] {f : perm α} {x y : α} (h : same_cycle f x y) :
cycle_of f x y = f y := dif_pos h
lemma cycle_of_apply_of_not_same_cycle [fintype α] {f : perm α} {x y : α} (h : ¬same_cycle f x y) :
cycle_of f x y = y := dif_neg h
@[simp] lemma cycle_of_apply_self [fintype α] (f : perm α) (x : α) :
cycle_of f x x = f x := cycle_of_apply_of_same_cycle (same_cycle.refl _ _)
lemma cycle_of_cycle [fintype α] {f : perm α} (hf : is_cycle f) {x : α} (hx : f x ≠ x) :
cycle_of f x = f :=
equiv.ext _ _ $ λ y,
if h : same_cycle f x y then by rw [cycle_of_apply_of_same_cycle h]
else by rw [cycle_of_apply_of_not_same_cycle h, not_not.1 (mt ((same_cycle_cycle hx).1 hf).2 h)]
lemma cycle_of_one [fintype α] (x : α) : cycle_of 1 x = 1 :=
by rw [cycle_of, subtype_perm_one (same_cycle 1 x), of_subtype_one]
lemma is_cycle_cycle_of [fintype α] (f : perm α) {x : α} (hx : f x ≠ x) : is_cycle (cycle_of f x) :=
have cycle_of f x x ≠ x, by rwa [cycle_of_apply_of_same_cycle (same_cycle.refl _ _)],
(same_cycle_cycle this).2 $ λ y,
⟨λ h, mt (apply_eq_self_iff_of_same_cycle h).2 this,
λ h, if hxy : same_cycle f x y then
let ⟨i, hi⟩ := hxy in
⟨i, by rw [cycle_of_gpow_apply_self, hi]⟩
else by rw [cycle_of_apply_of_not_same_cycle hxy] at h; exact (h rfl).elim⟩
def cycle_factors_aux [fintype α] : Π (l : list α) (f : perm α), (∀ {x}, f x ≠ x → x ∈ l) →
{l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint}
| [] f h := ⟨[], by simp [*, imp_false, list.pairwise.nil] at *; ext; simp *⟩
| (x::l) f h :=
if hx : f x = x then cycle_factors_aux l f (λ y hy, list.mem_of_ne_of_mem (λ h, hy (by rwa h)) (h hy))
else let ⟨m, hm₁, hm₂, hm₃⟩ := cycle_factors_aux l ((cycle_of f x)⁻¹ * f)
(λ y hy, list.mem_of_ne_of_mem
(λ h : y = x, by rw [h, mul_apply, ne.def, inv_eq_iff_eq, cycle_of_apply_self] at hy; exact hy rfl)
(h (λ h : f y = y,
by rw [mul_apply, h, ne.def, inv_eq_iff_eq, cycle_of_apply] at hy; split_ifs at hy; cc))) in
⟨(cycle_of f x) :: m, by rw [list.prod_cons, hm₁]; simp,
λ g hg, ((list.mem_cons_iff _ _ _).1 hg).elim (λ hg, hg.symm ▸ is_cycle_cycle_of _ hx)
(hm₂ g),
list.pairwise_cons.2 ⟨λ g hg y,
or_iff_not_imp_left.2 (λ hfy,
have hxy : same_cycle f x y := not_not.1 (mt cycle_of_apply_of_not_same_cycle hfy),
have hgm : g :: m.erase g ~ m := list.cons_perm_iff_perm_erase.2 ⟨hg, list.perm.refl _⟩,
have ∀ h ∈ m.erase g, disjoint g h,
from (list.pairwise_cons.1 ((list.perm_pairwise (λ a b (h : disjoint a b), h.symm) hgm).2 hm₃)).1,
classical.by_cases id $ λ hgy : g y ≠ y,
(disjoint_prod_right _ this y).resolve_right $
have hsc : same_cycle f⁻¹ x (f y), by rwa [same_cycle_inv, same_cycle_apply],
by rw [disjoint_prod_perm hm₃ hgm.symm, list.prod_cons, ← eq_inv_mul_iff_mul_eq] at hm₁;
rwa [hm₁, mul_apply, mul_apply, cycle_of_inv, cycle_of_apply_of_same_cycle hsc,
inv_apply_self, inv_eq_iff_eq, eq_comm]),
hm₃⟩⟩
def cycle_factors [fintype α] [decidable_linear_order α] (f : perm α) :
{l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint} :=
cycle_factors_aux (univ.sort (≤)) f (λ _ _, (mem_sort _).2 (mem_univ _))
end equiv.perm
|
ad44310af6bac1aaf2ea9667e149c4346fa6a369 | 98beff2e97d91a54bdcee52f922c4e1866a6c9b9 | /src/sheaf.lean | 46b01c3d5894d45ebc40c726b6b9fdc917fd6f00 | [] | no_license | b-mehta/topos | c3fc43fb04ba16bae1965ce5c26c6461172e5bc6 | c9032b11789e36038bc841a1e2b486972421b983 | refs/heads/master | 1,629,609,492,867 | 1,609,907,263,000 | 1,609,907,263,000 | 240,943,034 | 43 | 3 | null | 1,598,210,062,000 | 1,581,877,668,000 | Lean | UTF-8 | Lean | false | false | 49,370 | lean | import category_theory.full_subcategory
import category_theory.limits.creates
import category_theory.reflects_isomorphisms
import category_theory.limits.preserves.shapes.binary_products
import category_theory.limits.preserves.shapes.terminal
import category_theory.adjunction.fully_faithful
import category_theory.closed.cartesian
import category.reflects
import equiv
import construction
import topos
import equalizers
namespace category_theory
open category_theory category_theory.category category_theory.limits
open classifier
noncomputable theory
universes v u u₂
variables {C : Type u} [category.{v} C] [topos C]
def indicators {B : C} (m : B ⟶ Ω C) (n : B ⟶ Ω C) : B ⟶ Ω C :=
classify (classification m ⊓ classification n)
def indicators_natural {B B' : C} (f : B' ⟶ B) (m : B ⟶ Ω C) (n : B ⟶ Ω C) :
f ≫ indicators m n = indicators (f ≫ m) (f ≫ n) :=
begin
dunfold indicators,
rw [classification_natural_symm, classification_natural_symm, ← inf_pullback,
classification.eq_symm_apply, classification_natural_symm, classification.apply_symm_apply],
end
variable (C)
def and_arrow : Ω C ⨯ Ω C ⟶ Ω C := indicators limits.prod.fst limits.prod.snd
variable {C}
@[reassoc]
lemma and_property {B : C} (m₁ m₂ : subq B) :
prod.lift (classify m₁) (classify m₂) ≫ and_arrow C = classify (m₁ ⊓ m₂) :=
by rw [and_arrow, indicators_natural, prod.lift_fst, prod.lift_snd, indicators,
classification.apply_symm_apply, classification.apply_symm_apply]
lemma leq_iff_comp_and {E : C} (m n : subq E) :
m ≤ n ↔ prod.lift (classify m) (classify n) ≫ and_arrow C = classify m :=
by simp only [← inf_eq_left, and_property, ← classification.apply_eq_iff_eq, classification.apply_symm_apply]
lemma factors_iff_comp_and {E A₁ A₂ : C} (m₁ : A₁ ⟶ E) (m₂ : A₂ ⟶ E) [mono m₁] [mono m₂] :
factors_through m₁ m₂ ↔ prod.lift (classifier_of m₁) (classifier_of m₂) ≫ and_arrow C = classifier_of m₁ :=
leq_iff_comp_and ⟦sub.mk' m₁⟧ ⟦sub.mk' m₂⟧
@[reassoc] lemma classify_postcompose {A A' E : C} (n : A ⟶ A') (m : A' ⟶ E) [mono n] [mono m] :
classifier_of n = m ≫ classifier_of (n ≫ m) :=
uniquely _ _ (left_right_hpb_to_both_hpb _ (top_iso_has_pullback_top _ n _ m (id_comp _)) (classifies (n ≫ m)))
lemma classify_self {E : C} : classifier_of (𝟙 E) = default (E ⟶ Ω₀ C) ≫ truth C :=
begin
apply uniquely,
apply left_iso_has_pullback_top (default (E ⟶ Ω₀ C)),
rw id_comp
end
lemma classify_mk {A E : C} (m : A ⟶ E) [mono m] : classify ⟦sub.mk' m⟧ = classifier_of m := rfl
lemma classify_top (E : C) : classify ⊤ = default (E ⟶ Ω₀ C) ≫ truth C :=
classify_self
class topology (j : Ω C ⟶ Ω C) :=
(ax1 : truth C ≫ j = truth C)
(ax2 : j ≫ j = j)
(ax3 : and_arrow C ≫ j = limits.prod.map j j ≫ and_arrow C)
variables (j : Ω C ⟶ Ω C) [topology.{v} j]
namespace closure
variables {E A : C}
def obj (m : A ⟶ E) [mono m] : C := get_subobject_obj (classifier_of m ≫ j)
def arrow (m : A ⟶ E) [mono m] : get_subobject_obj (classifier_of m ≫ j) ⟶ E := get_subobject (classifier_of m ≫ j)
instance is_sub (m : A ⟶ E) [mono m] : mono (closure.arrow j m) := category_theory.get_subobject_mono _
lemma classifier (m : A ⟶ E) [mono m] : classifier_of (arrow j m) = classifier_of m ≫ j :=
uniquely _ _ (has_pullback_top_of_pb)
def operator (m : subq E) : subq E := classification (classify m ≫ j)
def subobj (m : A ⟶ E) [mono m] : subq E := operator j ⟦sub.mk' m⟧
lemma classify_op : ∀ (m : subq E), classify (operator j m) = classify m ≫ j :=
quotient.ind $
begin
intro a,
exact classifier j _,
end
lemma classify (m : A ⟶ E) [mono m] : classify (subobj j m) = classify ⟦sub.mk' m⟧ ≫ j :=
classifier j m
lemma operator_idem (m : subq E) : operator j (operator j m) = operator j m :=
begin
simp only [← classify_eq_iff_eq, classify_op, assoc, topology.ax2],
end
def less_than_closure (m : A ⟶ E) [mono m] : A ⟶ closure.obj j m :=
pullback.lift (classifies m).top m $ by rw [← (classifies m).comm_assoc, topology.ax1]
@[reassoc] lemma is_lt (m : A ⟶ E) [mono m] : less_than_closure j m ≫ closure.arrow j m = m :=
pullback.lift_snd _ _ _
instance (m : A ⟶ E) [mono m] : mono (less_than_closure j m) := mono_of_mono_fac (is_lt j m)
def idem (m : A ⟶ E) [mono m] : obj j (arrow j m) ≅ obj j m :=
begin
have: classifier_of (arrow j (arrow j m)) = classifier_of (arrow j m),
rw [classifier, classifier, assoc, topology.ax2],
exact how_inj_is_classifier _ _ this,
end
def closure_intersection {E : C} {m m' : subq E} : closure.operator j (m ⊓ m') = closure.operator j m ⊓ closure.operator j m' :=
by simp only [← classify_eq_iff_eq, closure.classify_op, ← and_property, ← prod.lift_map, assoc, topology.ax3]
def monotone {B : C} (m : A ⟶ E) (n : B ⟶ E) [mono m] [mono n] (h : factors_through m n) :
factors_through (arrow j m) (arrow j n) :=
begin
rw [factors_iff_comp_and] at h,
rw [factors_iff_comp_and, closure.classifier, closure.classifier, ← prod.lift_map, assoc,
← topology.ax3, reassoc_of h],
end
def mono_sub : ∀ {m n : subq E}, m ≤ n → operator j m ≤ operator j n :=
quotient.ind₂ $
begin
intros a b h,
apply monotone,
cases h,
refine ⟨over.hom_mk h.left (sub.w h)⟩,
end
lemma comm_pullback (m : subq E) (f : A ⟶ E) :
(subq.pullback f).obj (operator j m) = operator j ((subq.pullback f).obj m) :=
by rw [← classify_eq_iff_eq, classify_pullback, classify_op, classify_op, classify_pullback, assoc]
class dense (m : A ⟶ E) extends mono.{v} m : Prop :=
(closure_eq_top : subobj j m = ⊤)
def dense_of_classifier_eq {m : A ⟶ E} [mono m] (hm : classifier_of m ≫ j = default _ ≫ truth C) : dense j m :=
⟨by { rw [← classify_eq_iff_eq, classify_top, ← hm, ← closure.classifier], refl }⟩
instance dense_inclusion (m : A ⟶ E) [mono m] : dense j (less_than_closure j m) :=
begin
apply dense_of_classifier_eq,
rw [classify_postcompose _ (arrow j m)],
slice_lhs 2 2 {congr, rw is_lt},
rw [← closure.classifier, ← (classifies (arrow j m)).comm],
congr,
end
lemma classifier_eq_of_dense (m : A ⟶ E) [d : dense j m] : classifier_of m ≫ j = default _ ≫ truth C :=
by { rw [← classify_top, ← d.closure_eq_top, ← closure.classifier], refl }
class closed (m : A ⟶ E) extends mono.{v} m :=
(closure_eq_self : subobj j m = ⟦sub.mk' m⟧)
def closed_of_classifier_eq {m : A ⟶ E} [mono m] (hm : classifier_of m ≫ j = classifier_of m) : closed j m :=
⟨by rwa [← classify_eq_iff_eq, classify_mk, closure.classify]⟩
lemma classifier_eq_of_closed (m : A ⟶ E) [c : closed j m] : classifier_of m ≫ j = classifier_of m :=
by rw [← classify_mk, ← classify, c.closure_eq_self]
instance is_closed (m : A ⟶ E) [mono m] : closed j (arrow j m) :=
begin
apply closed_of_classifier_eq,
rw [closure.classifier, assoc, topology.ax2],
end
def mono_of_is_pullback {E F A B : C} {m : A ⟶ E} {f : F ⟶ E} {l : B ⟶ F} {t : B ⟶ A} (comm : t ≫ m = l ≫ f)
(lim : is_limit (pullback_cone.mk _ _ comm)) [mono m] : mono l :=
begin
refine ⟨λ Z g h eq, _⟩,
apply lim.hom_ext,
apply (pullback_cone.mk t l comm).equalizer_ext,
rw ← cancel_mono m,
erw [assoc, assoc, comm, reassoc_of eq],
exact eq
end
def dense_of_pullback {E F A B : C} {m : A ⟶ E} {f : F ⟶ E} {l : B ⟶ F} {t : B ⟶ A} (comm : t ≫ m = l ≫ f)
(lim : is_limit (pullback_cone.mk _ _ comm)) [d : closure.dense j m] : closure.dense j l :=
begin
haveI := mono_of_is_pullback comm lim,
have : ⟦sub.mk' l⟧ = (subq.pullback f).obj ⟦sub.mk' m⟧,
apply quotient.sound,
refine equiv_of_both_ways (sub.hom_mk _ (pullback.lift_snd _ _ comm)) (sub.hom_mk (lim.lift _) (lim.fac _ walking_cospan.right)),
refine ⟨_⟩,
rw [subobj, this, ← closure.comm_pullback],
convert subq.pullback_top f,
apply d.closure_eq_top,
end
instance dense_pullback {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) [dense j g] : dense j (pullback.snd : pullback g f ⟶ X) :=
dense_of_pullback j pullback.condition (cone_is_pullback _ _)
instance dense_pullback_fst {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) [dense j g] : dense j (pullback.fst : pullback f g ⟶ X) :=
dense_of_pullback j pullback.condition.symm (pullback_cone.flip_is_limit (cone_is_pullback _ _))
def dense_top_of_pullback {E F A B : C} {m : A ⟶ E} {f : F ⟶ E} {l : B ⟶ F} {t : B ⟶ A} (comm : t ≫ m = l ≫ f)
(lim : is_limit (pullback_cone.mk _ _ comm)) [dense j f] : dense j t :=
dense_of_pullback _ comm.symm (pullback_flip lim)
def dense_of_iso {A₁ A₂ E : C} (m : A₁ ⟶ E) (i : A₁ ≅ A₂) [dense j m] : dense j (i.inv ≫ m) :=
{ closure_eq_top :=
begin
have : ⟦sub.mk' (i.inv ≫ m)⟧ = ⟦sub.mk' m⟧,
apply quotient.sound,
refine equiv_of_both_ways (sub.hom_mk i.inv rfl) (sub.hom_mk i.hom (i.hom_inv_id_assoc _)),
rw [subobj, this],
apply dense.closure_eq_top,
end }
def closure_postcompose {A E₁ E₂ : C} (f : E₁ ⟶ E₂) [mono f] (m : A ⟶ E₁) [mono m] :
classifier_of (closure.arrow j m : _ ⟶ E₁) = f ≫ classifier_of (closure.arrow j (m ≫ f)) :=
by rw [classifier, classifier, ← classify_postcompose_assoc]
def is_iso_of_dense_of_closed {A B : C} (f : A ⟶ B) [d : dense j f] [c : closed j f] : is_iso f :=
begin
have := d.closure_eq_top,
rw c.closure_eq_self at this,
have : nonempty (⊤ ⟶ sub.mk' f),
obtain ⟨⟨_, b, _, _⟩⟩ := quotient.exact this,
refine ⟨b⟩,
obtain ⟨r, hr⟩ := raised_factors this,
refine ⟨r, _, hr⟩,
rw [← cancel_mono f, assoc, hr], simp,
end
end closure
def lifting_square {A A' B B' : C} {f' : B' ⟶ A'} {m : A' ⟶ A} {n : B' ⟶ B} {f : B ⟶ A}
(comm : f' ≫ m = n ≫ f) [d : closure.dense j n] [c : closure.closed j m] : {k // k ≫ m = f} :=
begin
have : ⊤ ≤ (subq.pullback f).obj ⟦sub.mk' m⟧,
rw [← d.closure_eq_top, ← c.closure_eq_self, closure.subobj, closure.subobj,
closure.comm_pullback],
apply closure.mono_sub,
refine ⟨sub.hom_mk _ (pullback.lift_snd _ _ comm)⟩,
obtain ⟨p, hp⟩ : {p : B ⟶ pullback m f // p ≫ pullback.snd = 𝟙 B } := raised_factors this,
refine ⟨p ≫ pullback.fst, _⟩,
rw [assoc, pullback.condition, reassoc_of hp],
end
instance dense_comp {E₁ E₂ E₃ : C} (m₁ : E₁ ⟶ E₂) (m₂ : E₂ ⟶ E₃) [closure.dense j m₁] [d : closure.dense j m₂] : closure.dense j (m₁ ≫ m₂) :=
{ closure_eq_top :=
begin
have : closure.less_than_closure j (m₁ ≫ m₂) ≫ closure.arrow j (m₁ ≫ m₂) = m₁ ≫ m₂ := closure.is_lt j (m₁ ≫ m₂),
obtain ⟨r, hr⟩ := lifting_square j this,
have : r ≫ closure.arrow j (m₁ ≫ m₂) = m₂ ≫ 𝟙 _,
rw [hr, comp_id],
obtain ⟨s, hs⟩ := lifting_square j this,
rw eq_top_iff,
refine ⟨sub.hom_mk s hs⟩,
end }
instance dense_prod_map {W X Y Z : C} (f : W ⟶ X) (g : Y ⟶ Z) [closure.dense j f] [closure.dense j g] :
closure.dense j (limits.prod.map f g) :=
begin
have : closure.dense j (limits.prod.map f (𝟙 Y)) := closure.dense_of_pullback j _ (pullback_prod _ _),
haveI : closure.dense j (limits.prod.map (𝟙 X) g) := closure.dense_of_pullback j _ (pullback_prod' _ _),
have : limits.prod.map f g = limits.prod.map f (𝟙 Y) ≫ limits.prod.map (𝟙 X) g,
apply prod.hom_ext; simp only [limits.prod.map_fst, limits.prod.map_snd, limits.prod.map_snd_assoc, assoc, comp_id, id_comp],
rw this,
apply_instance,
end
@[derive subsingleton]
def sheaf_condition (A : C) : Type (max u v) :=
Π ⦃B B'⦄ (m : B' ⟶ B) f' [closure.dense j m], unique {f : B ⟶ A // m ≫ f = f'}
def sheaf_condition.mk' (A : C) (h : Π ⦃B B'⦄ (m : B' ⟶ B) f' [closure.dense j m], {f : B ⟶ A // m ≫ f = f' ∧ ∀ a, m ≫ a = f' → a = f}) :
sheaf_condition j A :=
begin
introsI B B' m f' d,
refine ⟨⟨⟨(h m f').1, (h m f').2.1⟩⟩, _⟩,
rintro ⟨a, ha⟩,
apply subtype.ext,
apply (h m f').2.2 _ ha,
end
structure sheaf' : Type (max u v) :=
(A : C)
(unique_extend : sheaf_condition j A)
def forget_sheaf : sheaf'.{v} j → C := sheaf'.A
def sheaf := induced_category C (forget_sheaf j)
instance sheaf_category.category : category (sheaf j) := induced_category.category _
def sheaf.forget : sheaf j ⥤ C := induced_functor _
variables {j}
@[simps]
def sheaf.mk (A : C) (h : sheaf_condition j A) : sheaf j :=
{ A := A,
unique_extend := h }
@[reducible]
def sheaf.mk' (A : C) (h : Π ⦃B B'⦄ (m : B' ⟶ B) f' [closure.dense j m], {f : B ⟶ A // m ≫ f = f' ∧ ∀ a, m ≫ a = f' → a = f}) : sheaf j :=
sheaf.mk A (sheaf_condition.mk' j A h)
def sheaf.A (A : sheaf j) : C := (sheaf.forget j).obj A
def sheaf.hom_mk (A B : sheaf j) (f : A.A ⟶ B.A) : A ⟶ B := f
def get_condition (A : sheaf j) : sheaf_condition j A.A := A.2
def unique_extend (A : sheaf j) {B B' : C} (m : B' ⟶ B) [closure.dense j m] (f' : B' ⟶ A.A) : unique {f // m ≫ f = f'} :=
(A.unique_extend m f')
def extend_map' (A : sheaf j) {B B' : C} (m : B' ⟶ B) [closure.dense j m] (f' : B' ⟶ A.A) : {f // m ≫ f = f'} :=
(A.unique_extend m f').1.1
def extend_map (A : sheaf j) {B B' : C} (m : B' ⟶ B) [closure.dense j m] (f' : B' ⟶ A.A) : B ⟶ A.A :=
(extend_map' A m f').1
@[reassoc] lemma extend_map_prop (A : sheaf j) {B B' : C} (m : B' ⟶ B) [closure.dense j m] (f' : B' ⟶ A.A) : m ≫ extend_map A m f' = f' :=
(extend_map' A m f').2
lemma unique_extension (A : sheaf j) {B B' : C} (m : B' ⟶ B) [closure.dense j m] (f' : B' ⟶ A.A)
(f : B ⟶ A.A) (h : m ≫ f = f') :
f = extend_map A m f' :=
congr_arg subtype.val ((A.unique_extend m f').2 ⟨f, h⟩)
def unique_ext (A : sheaf j) {B B' : C} (m : B' ⟶ B) [closure.dense j m] (f' : B' ⟶ A.A)
(f₁ f₂ : B ⟶ A.A) (h₁ : m ≫ f₁ = f') (h₂ : m ≫ f₂ = f') :
f₁ = f₂ :=
(unique_extension A m f' f₁ h₁).trans (unique_extension A m f' f₂ h₂).symm
def cancel_dense (A : sheaf j) {B B' : C} (m : B' ⟶ B) [closure.dense j m]
(f₁ f₂ : B ⟶ A.A) (h : m ≫ f₁ = m ≫ f₂) :
f₁ = f₂ :=
unique_ext A m (m ≫ f₂) f₁ f₂ h rfl
instance sheaf_forget_full : full (sheaf.forget j) := induced_category.full _
instance sheaf_forget_faithful : faithful (sheaf.forget j) := induced_category.faithful _
instance sheaf_forget_reflects_limits : reflects_limits (sheaf.forget j) := by apply_instance
attribute [irreducible] sheaf
namespace construct_limits
variables {C} {J : Type v} [𝒥₁ : small_category J] {K : J ⥤ sheaf j} {c : cone (K ⋙ sheaf.forget j)} (t : is_limit c)
variables {B B' : C} (m : B' ⟶ B) (f' : B' ⟶ c.X)
@[simps]
def alt_cone [closure.dense j m] : cone (K ⋙ sheaf.forget j) :=
{ X := B,
π :=
{ app := λ i, extend_map (K.obj i) m (f' ≫ c.π.app i),
naturality' := λ i₁ i₂ g,
begin
dsimp,
rw [id_comp],
symmetry,
apply unique_extension (K.obj i₂) m (f' ≫ c.π.app i₂),
erw [← assoc, extend_map_prop, assoc, c.w g],
end } }
instance sheaf_forget_creates_limits : creates_limits (sheaf.forget j) :=
{ creates_limits_of_shape := λ J 𝒥₁, by exactI
{ creates_limit := λ K,
{ lifts := λ c t,
{ lifted_cone :=
{ X := sheaf.mk' c.X $
λ B B' m f' d, by exactI
begin
refine ⟨t.lift (alt_cone m f'), _, _⟩,
{ apply t.hom_ext,
intro i,
rw [assoc, t.fac (alt_cone m f')],
exact extend_map_prop (K.obj i) m (f' ≫ c.π.app i) },
{ intros f₂ hf₂,
apply t.uniq (alt_cone m f'),
intro i,
apply unique_extension (K.obj i) m,
rw [← hf₂, assoc] }
end,
π :=
{ app := c.π.app,
naturality' := λ X Y f, c.π.naturality f } },
valid_lift := cones.ext (iso.refl _) (λ i, (id_comp _).symm) } } } }
end construct_limits
variables (j)
def sheaf_has_finite_limits : has_finite_limits.{v} (sheaf j) :=
λ J 𝒥₁ 𝒥₂, by exactI
{ has_limit := λ F, has_limit_of_created F (sheaf.forget j) }
local attribute [instance, priority 10] sheaf_has_finite_limits
-- def iso_limit (J : Type v) [small_category J] [fin_category J] (F : J ⥤ sheaf j) : (sheaf.forget j).obj (limit F) ≅ limit (F ⋙ sheaf.forget j) :=
-- by apply (cones.forget (F ⋙ sheaf.forget j)).map_iso (lifted_limit_maps_to_original (limit.is_limit (F ⋙ sheaf.forget j)))
def dense_prod_map_id (A : C) {B B' : C} (m : B' ⟶ B) [closure.dense.{v} j m] :
closure.dense.{v} j (limits.prod.map (𝟙 A) m) :=
closure.dense_of_pullback j _ (pullback_prod' m A)
local attribute [instance] has_finite_products_of_has_finite_limits
def sheaf_exponential (A : C) (s : sheaf j) : sheaf j :=
sheaf.mk' (A ⟹ s.A) $ λ B B' m f' d,
begin
haveI := d,
haveI := dense_prod_map_id j A m,
refine ⟨cartesian_closed.curry _, _, _⟩,
{ exact extend_map s (limits.prod.map (𝟙 A) m) (cartesian_closed.uncurry f') },
{ rw [← curry_natural_left, extend_map_prop s, curry_uncurry] },
{ rintro a ha,
rw eq_curry_iff,
apply unique_extension s,
rw [← uncurry_natural_left, ha] }
end
instance sheaf_cc : cartesian_closed (sheaf j) :=
{ closed := λ A,
{ is_adj :=
{ right :=
{ obj := λ s, sheaf_exponential j A.A s,
map := λ s₁ s₂ f, (exp A.A).map f,
map_id' := λ s, (exp A.A).map_id _,
map_comp' := λ _ _ _ _ _, (exp A.A).map_comp _ _ },
adj := adjunction.mk_of_hom_equiv
{ hom_equiv := λ X Y,
{ to_fun := λ f, cartesian_closed.curry (inv (prod_comparison (sheaf.forget j) A X) ≫ f),
inv_fun := λ g, by apply (prod_comparison (sheaf.forget j) A X) ≫ cartesian_closed.uncurry g,
left_inv := λ f, by simp,
right_inv := λ g, by simp },
hom_equiv_naturality_left_symm' :=
begin
intros X' X Y f g,
dsimp,
conv_lhs {congr, skip, erw uncurry_natural_left },
apply (prod_comparison_natural_assoc (sheaf.forget j) (𝟙 A) f _).symm,
end,
hom_equiv_naturality_right' :=
begin
intros X Y Y' f g,
dsimp,
conv_rhs {apply_congr (curry_natural_right _ _).symm},
simpa
end } } } }
def subobject_of_closed_sheaf (A : sheaf j) (A' : C) (m : A' ⟶ A.A) [closure.closed j m] : sheaf j :=
sheaf.mk' A' $ λ B B' n f' d, by exactI
begin
obtain ⟨g, comm⟩ := extend_map' A n (f' ≫ m),
refine ⟨(lifting_square j comm.symm).1, _, _⟩,
rwa [← cancel_mono m, assoc, (lifting_square j comm.symm).2],
intros a ha,
rw [← cancel_mono m, (lifting_square j comm.symm).2],
apply unique_ext A n (f' ≫ m) (a ≫ m) g _ comm,
rw reassoc_of ha,
end
def closed_of_subsheaf (E A : sheaf j) (m : A.A ⟶ E.A) [mono m] : closure.closed j m :=
begin
obtain ⟨r, hr⟩ := extend_map' A (closure.less_than_closure j m) (𝟙 _),
have := unique_ext _ _ _ (r ≫ m) _ (by rw [reassoc_of hr]) (closure.is_lt _ _),
refine ⟨quotient.sound (equiv_of_both_ways (sub.hom_mk r this) (sub.hom_mk (closure.less_than_closure j m) (closure.is_lt j m)))⟩,
end
def closed_classifier : C := equalizer j (𝟙 _)
def eq_equiv (B : C) : (B ⟶ closed_classifier j) ≃ {cm : B ⟶ Ω C // cm ≫ j = cm} :=
{ to_fun := λ f, ⟨f ≫ equalizer.ι _ _, by simp [equalizer.condition]⟩,
inv_fun := λ f, equalizer.lift f.1 (by rw [f.2, comp_id]),
left_inv := λ f, equalizer.hom_ext (equalizer.lift_ι _ _),
right_inv := λ ⟨f, hf⟩, subtype.eq (equalizer.lift_ι _ _) }
def action {B B' : C} (m : B' ⟶ B) [d : closure.dense j m] :
{n' : subq B // closure.operator j n' = n'} ≃ {n : subq B' // closure.operator j n = n} :=
{ to_fun :=
begin
intro n,
refine ⟨(subq.pullback m).obj n.1, _⟩,
rw [← closure.comm_pullback, n.2],
end,
inv_fun := λ n, ⟨closure.operator j ((subq.post m).obj n.1), closure.operator_idem j _⟩,
left_inv :=
begin
rintro ⟨n, hn⟩,
dsimp,
congr' 1,
have : _ = (subq.post m).obj ((subq.pullback m).obj _) := subq.inf_eq_post_pull (sub.mk' m) n,
rw ← this,
rw closure.closure_intersection,
rw hn,
change closure.subobj j _ ⊓ n = _,
rw d.closure_eq_top,
exact top_inf_eq,
end,
right_inv :=
begin
rintro ⟨n, hn⟩,
dsimp,
congr' 1,
rwa [closure.comm_pullback, subq.pull_post_self],
end }
def closure_equiv {B : C} : {cB : B ⟶ Ω C // cB ≫ j = cB} ≃ {n : subq B // closure.operator j n = n} :=
begin
apply classification.subtype_congr,
intro a,
rw ← classify_eq_iff_eq,
rw closure.classify_op,
change _ ↔ classification.symm _ ≫ _ = classification.symm _,
rw classification.symm_apply_apply,
end
def closed_equiv {B B' : C} (m : B' ⟶ B) [closure.dense j m] : {cB : B ⟶ Ω C // cB ≫ j = cB} ≃ {cB : B' ⟶ Ω C // cB ≫ j = cB} :=
(closure_equiv j).trans ((action j m).trans (closure_equiv j).symm)
def closed_class_equiv {B B' : C} (m : B' ⟶ B) [closure.dense j m] :
(B ⟶ closed_classifier j) ≃ (B' ⟶ closed_classifier j) :=
(eq_equiv j B).trans ((closed_equiv j m).trans (eq_equiv j B').symm)
lemma closed_class_equiv_forward {B B' : C} (m : B' ⟶ B) [closure.dense j m] (f : B ⟶ closed_classifier j) :
m ≫ f = closed_class_equiv j m f :=
begin
dsimp [closed_class_equiv, eq_equiv, closed_equiv, action, closure_equiv, equiv.subtype_congr],
ext1,
rw equalizer.lift_ι,
-- dsimp [subq.pullback, lower_sub],
change _ = classify ((subq.pullback m).obj ⟦_⟧),
-- change _ = classifier_of _,
rw classify_pullback,
change _ = m ≫ classification.symm (classification _),
rw classification.symm_apply_apply,
rw assoc,
end
def sheaf_classifier : sheaf j :=
sheaf.mk' (closed_classifier j) $ λ B B' m f' d, by exactI
begin
refine ⟨(closed_class_equiv j m).symm f', _, _⟩,
rw [closed_class_equiv_forward, equiv.apply_symm_apply],
intros a ha,
rwa [(closed_class_equiv j m).eq_symm_apply, ← closed_class_equiv_forward],
end
def forget_terminal_sheaf : (⊤_ (sheaf j)).A ≅ ⊤_ C :=
preserves_terminal.iso (sheaf.forget j)
def sheaf_classify {U X : C} (f : U ⟶ X) [closure.closed j f] : X ⟶ closed_classifier j :=
equalizer.lift (classifier_of f) (by rw [comp_id, closure.classifier_eq_of_closed])
def sheaf_truth : (⊤_ (sheaf j)).A ⟶ closed_classifier j :=
(forget_terminal_sheaf j).hom ≫ equalizer.lift (default _ ≫ truth C) (by rw [assoc, comp_id, topology.ax1])
def sheaf_hpb {U X : C} (f : U ⟶ X) [closure.closed j f] :
has_pullback_top f (sheaf_classify j f) (sheaf_truth j) :=
begin
apply right_both_hpb_to_left_hpb (truth C) (equalizer.ι _ _),
rw [sheaf_classify, equalizer.lift_ι],
apply classifies,
refine top_iso_has_pullback_top _ _ _ _ _,
apply (forget_terminal_sheaf j).hom ≫ (default (⊤_ C ⟶ Ω₀ C)),
haveI : is_iso (default (⊤_ C ⟶ Ω₀ C)) := ⟨default _, subsingleton.elim _ _, subsingleton.elim _ _⟩,
apply_instance,
rw [sheaf_truth, assoc, assoc, equalizer.lift_ι],
end
def sheaf_has_subobj_classifier : has_subobject_classifier.{v} (sheaf j) :=
{ Ω := sheaf_classifier j,
Ω₀ := ⊤_ _,
truth :=
begin
apply (forget_terminal_sheaf j).hom ≫ _,
apply equalizer.lift (default (⊤_ C ⟶ Ω₀ C) ≫ truth C) _,
rw [assoc, comp_id, topology.ax1],
end,
truth_mono := ⟨λ Z g h eq, subsingleton.elim _ _⟩,
is_subobj_classifier :=
{ classifier_of := λ U X f hf, by exactI
begin
haveI := preserves_mono_of_preserves_pullback (sheaf.forget j) _ _ f,
haveI := closed_of_subsheaf j X U ((sheaf.forget j).map f),
apply (sheaf.forget j).preimage,
apply sheaf_classify j ((sheaf.forget j).map f),
end,
classifies' := λ U X f hf,
begin
apply fully_faithful_reflects_hpb (sheaf.forget j),
apply sheaf_hpb,
end,
uniquely' := λ U X f hf χ hχ,
begin
apply (sheaf.forget j).map_injective,
rw [functor.image_preimage],
rw ← cancel_mono (equalizer.ι j (𝟙 _)),
rw [sheaf_classify, equalizer.lift_ι],
apply uniquely,
apply left_right_hpb_to_both_hpb _ (preserves_hpb (sheaf.forget j) hχ),
refine top_iso_has_pullback_top _ _ _ _ _,
apply (forget_terminal_sheaf j).hom ≫ (default (⊤_ C ⟶ Ω₀ C)),
haveI : is_iso (default (⊤_ C ⟶ Ω₀ C)) := ⟨default _, subsingleton.elim _ _, subsingleton.elim _ _⟩,
apply_instance,
change _ = (_ ≫ _) ≫ _,
rw [assoc, assoc, equalizer.lift_ι],
end } }
/-- The topos of sheaves! -/
instance : topos.{v} (sheaf j) := { sub := sheaf_has_subobj_classifier j }
section close_equiv
variables {R A : C} (rel : relation.{v} R A)
abbreviation close_relation [mono rel] : relation.{v} (closure.obj j rel) A := closure.arrow j rel
instance close_rel_refl [mono rel] [reflexive rel] : reflexive (close_relation j rel) :=
{ r := reflexive.r rel ≫ closure.less_than_closure j _,
cancel_a := by rw [assoc, closure.is_lt_assoc, reflexive.cancel_a],
cancel_b := by rw [assoc, closure.is_lt_assoc, reflexive.cancel_b] }
def symmetric_of_swap_eq_self [mono rel] (h : classifier_of rel = classifier_of (rel ≫ (limits.prod.braiding _ _).hom)) :
symmetric rel :=
begin
have : (how_inj_is_classifier _ _ h).hom ≫ _ = _ := c_very_inj h,
have eq : prod.lift rel.a rel.b ≫ (limits.prod.braiding A A).hom = prod.lift rel.b rel.a,
apply prod.hom_ext; simp,
refine ⟨(how_inj_is_classifier _ _ h).hom, _, _⟩,
have := (c_very_inj h) =≫ limits.prod.snd,
simp only [prod.lift_fst, assoc, prod.lift_snd, prod.braiding_hom] at this,
exact this,
have := (c_very_inj h) =≫ limits.prod.fst,
simp only [prod.lift_fst, assoc, prod.lift_snd, prod.braiding_hom] at this,
exact this,
end
def swap_eq_self_of_symmetric [mono rel] [symmetric rel] :
classifier_of rel = classifier_of (rel ≫ (limits.prod.braiding _ _).inv) :=
begin
apply class_lift_of_iso ⟨symmetric.s rel, symmetric.s rel, symmetric_idem rel, symmetric_idem rel⟩,
dsimp, rw symmetric_pair_assoc rel,
apply prod.hom_ext; simp,
end
instance close_rel_symm [mono rel] [symmetric rel] : symmetric (close_relation j rel) :=
begin
apply symmetric_of_swap_eq_self,
have := classify_postcompose (closure.arrow j rel) (limits.prod.braiding _ _).hom,
rw ← cancel_epi (limits.prod.braiding A A).hom,
erw ← this,
rw closure.classifier,
have := classify_postcompose rel (limits.prod.braiding _ _).inv,
conv_lhs {rw this},
rw [assoc, (limits.prod.braiding A A).hom_inv_id_assoc],
rw ← swap_eq_self_of_symmetric,
end
end close_equiv
def equality (A : C) : relation A A := relation.of_pair (𝟙 A) (𝟙 A)
instance equality_mono {A : C} : mono (equality A) := category_theory.mono_prod_lift_of_left _ _
def equality_sub (A : C) : subq (A ⨯ A) := subq.mk (equality A)
def j_equal (A : C) : relation (closure.obj j (equality A)) A := close_relation j (equality A)
instance j_equal_mono (A : C) : mono (j_equal j A) := closure.is_sub j _
def j_equal_sub (A : C) : subq (A ⨯ A) := subq.mk (j_equal j A)
lemma j_equal_sub_eq (A : C) : j_equal_sub j A = closure.operator j (equality_sub A) := rfl
section
-- Prove that if x' = x and R(x, y) then R(x', y)
variables {A B R : C} (r : R ⟶ A ⨯ B)
def x'_eq_x (A B) : C := pullback (equality A) (limits.prod.fst : A ⨯ A ⨯ B ⟶ A ⨯ A)
def x'_eq_x_arrow (A B : C) : x'_eq_x A B ⟶ A ⨯ A ⨯ B := pullback.snd
instance x'_eq_x_mono [mono r] : mono (x'_eq_x_arrow A B) := pullback.snd_of_mono
def Rxy : C := pullback r (limits.prod.map limits.prod.snd (𝟙 B) : A ⨯ A ⨯ B ⟶ A ⨯ B)
def Rx'y : C := pullback r (limits.prod.map limits.prod.fst (𝟙 B) : A ⨯ A ⨯ B ⟶ A ⨯ B)
def Rxy_arrow : Rxy r ⟶ A ⨯ A ⨯ B := pullback.snd
instance Rxy_mono [mono r] : mono (Rxy_arrow r) := pullback.snd_of_mono
def Rx'y_arrow : Rx'y r ⟶ A ⨯ A ⨯ B := pullback.snd
instance Rx'y_mono [mono r] : mono (Rx'y_arrow r) := pullback.snd_of_mono
def x'_eq_x_and_Rxy : C := pullback (x'_eq_x_arrow A B) (Rxy_arrow r)
def x'_eq_x_and_Rxy_arrow : x'_eq_x_and_Rxy r ⟶ A ⨯ A ⨯ B := pullback.snd ≫ Rxy_arrow r
instance x'_eq_x_and_Rxy_mono [mono r] : mono (x'_eq_x_and_Rxy_arrow r) := mono_comp _ _
def x'_eq_x_sub (A B : C) : subq (A ⨯ A ⨯ B) := (subq.pullback (limits.prod.fst : A ⨯ A ⨯ B ⟶ A ⨯ A)).obj (equality_sub A)
def R_sub [mono r] : subq (A ⨯ B) := subq.mk r
def Rxy_sub [mono r] : subq (A ⨯ A ⨯ B) := (subq.pullback (limits.prod.map limits.prod.snd (𝟙 B) : A ⨯ A ⨯ B ⟶ A ⨯ B)).obj (R_sub r)
def Rx'y_sub [mono r] : subq (A ⨯ A ⨯ B) := (subq.pullback (limits.prod.map limits.prod.fst (𝟙 B) : A ⨯ A ⨯ B ⟶ A ⨯ B)).obj (R_sub r)
lemma x'_eq_x_prop : x'_eq_x_arrow A B ≫ limits.prod.fst ≫ limits.prod.fst = x'_eq_x_arrow A B ≫ limits.prod.fst ≫ limits.prod.snd :=
begin
have : pullback.fst ≫ (prod.lift (𝟙 A) (𝟙 A)) = x'_eq_x_arrow A B ≫ _ := pullback.condition,
rw [← reassoc_of this, ← reassoc_of this],
simp,
end
lemma factors : factors_through (x'_eq_x_and_Rxy_arrow r) (Rx'y_arrow r) :=
begin
refine ⟨over.hom_mk _ (pullback.lift_snd (pullback.snd ≫ pullback.fst) _ _)⟩,
rw x'_eq_x_and_Rxy_arrow,
apply prod.hom_ext,
{ rw [assoc, assoc, assoc, limits.prod.map_fst, ← pullback.condition, over.mk_hom, assoc,
x'_eq_x_prop, pullback.condition_assoc, limits.prod.map_fst, pullback.condition_assoc],
refl },
{ simpa only [limits.prod.map_snd, pullback.condition, assoc, over.mk_hom] },
end
lemma factors_sub [mono r] : x'_eq_x_sub A B ⊓ Rxy_sub r ≤ Rx'y_sub r :=
begin
rw inf_comm,
exact factors r,
end
lemma closure_factors_sub [c : closure.closed j r] :
(subq.pullback limits.prod.fst).obj (j_equal_sub j A) ⊓ Rxy_sub r ≤ Rx'y_sub r :=
begin
have := closure.mono_sub j (factors_sub r),
rw [closure.closure_intersection, Rxy_sub, Rx'y_sub, x'_eq_x_sub,
← closure.comm_pullback, ← closure.comm_pullback, ← closure.comm_pullback] at this,
have r_closed : closure.operator j (R_sub r) = R_sub r := c.closure_eq_self,
rw r_closed at this,
exact this
end
end
section
open category_theory.limits.prod
variables {A R : C} (r : relation R A)
def transitive_of_pair (t : triples r ⟶ R) (ht : t ≫ r = prod.lift (p r ≫ r.a) (q r ≫ r.b)) : transitive r :=
{ t := t,
w₁ := by simpa using ht =≫ limits.prod.fst,
w₂ := by simpa using ht =≫ limits.prod.snd }
def transitive_of_factors_sub [mono r]
(fac : (subq.pullback fst).obj (subq.mk r) ⊓ (subq.pullback (map snd (𝟙 _))).obj (subq.mk r) ≤ (subq.pullback (map fst (𝟙 _))).obj (subq.mk r)) :
transitive r :=
begin
obtain ⟨t, ht⟩ : {t : pullback pullback.snd pullback.snd ⟶ pullback r _ // t ≫ pullback.snd = pullback.snd ≫ pullback.snd} :=
raised_factors fac,
let big : triples r ⟶ A ⨯ A ⨯ A,
apply prod.lift (prod.lift (p r ≫ r.a) (q r ≫ r.a)) (q r ≫ r.b),
fapply transitive_of_pair,
apply pullback.lift (pullback.lift (q r) big _) (pullback.lift (p r) big _) _ ≫ t ≫ pullback.fst,
{ rw [prod.lift_map, comp_id, prod.lift_snd],
apply prod.hom_ext; simp },
{ rw prod.lift_fst,
apply prod.hom_ext,
{ simp },
{ rw [lift_snd, ← consistent r, assoc], refl } },
{ simp },
{ simp only [assoc],
rw [pullback.condition, reassoc_of ht, pullback.lift_snd_assoc, pullback.lift_snd_assoc, lift_map, comp_id],
apply prod.hom_ext; simp }
end
end
instance eq_reflexive (A : C) : reflexive.{v} (equality A) :=
{ r := 𝟙 A,
cancel_a := by simp [equality],
cancel_b := by simp [equality] }
instance eq_symmetric (A : C) : symmetric.{v} (equality A) :=
{ s := 𝟙 A,
w₁ := by simp [equality],
w₂ := by simp [equality] }
instance j_eq_reflexive (A : C) : reflexive (j_equal j A) :=
category_theory.close_rel_refl j (equality A)
instance j_eq_symmetric (A : C) : symmetric (j_equal j A) :=
category_theory.close_rel_symm j (equality A)
instance j_eq_transitive (A : C) : transitive (j_equal j A) :=
begin
apply transitive_of_factors_sub,
apply closure_factors_sub _ _,
rw j_equal,
apply_instance,
end
def j_eq_kernel_pair (A : C) : is_kernel_pair (named (j_equal j A)) (j_equal j A).a (j_equal j A).b :=
equiv_to_kernel_pair (j_equal j A)
def sub_kernel_pair {X Y Z W : C} (a b : X ⟶ Y) (f₁ : Y ⟶ Z) (f₂ : Z ⟶ W) (comm : a ≫ f₁ = b ≫ f₁)
(big_kernel_pair : is_limit (pullback_cone.mk a b (by rw reassoc_of comm) : pullback_cone (f₁ ≫ f₂) (f₁ ≫ f₂))) :
is_limit (pullback_cone.mk a b comm) :=
is_limit.mk' _
begin
intro s,
let s' : pullback_cone (f₁ ≫ f₂) (f₁ ≫ f₂) := pullback_cone.mk s.fst s.snd (s.condition_assoc _),
refine ⟨big_kernel_pair.lift s', big_kernel_pair.fac _ walking_cospan.left, big_kernel_pair.fac _ walking_cospan.right, λ m m₁ m₂, _⟩,
apply big_kernel_pair.hom_ext,
refine ((pullback_cone.mk a b _) : pullback_cone (f₁ ≫ f₂) _).equalizer_ext _ _,
erw m₁,
symmetry,
apply big_kernel_pair.fac _ walking_cospan.left,
erw m₂,
symmetry,
apply big_kernel_pair.fac _ walking_cospan.right,
end
def Pj (A : C) : sheaf j := sheaf_exponential j A (sheaf_classifier j)
def named_factors (A : C) : {hat : A ⟶ (Pj j A).A // hat ≫ (exp _).map (equalizer.ι _ _) = named (j_equal j A)} :=
begin
refine ⟨cartesian_closed.curry (equalizer.lift ((limits.prod.braiding A A).inv ≫ classifier_of (j_equal j A)) _), _⟩,
{ rw [assoc, comp_id, closure.classifier_eq_of_closed _ _],
rw j_equal,
apply_instance },
{ erw [← curry_natural_right, equalizer.lift_ι, curry_eq_iff, named, uncurry_curry] },
end
-- def regular_epi_is_coequalizer_of_kernel_pair {A B Y : C} (e : A ⟶ B) [he : regular_epi e] (h k : Y ⟶ A)
-- (comm : h ≫ e = k ≫ e) (l : is_limit (pullback_cone.mk _ _ comm)) :
-- is_colimit (cofork.of_π e comm) :=
-- begin
-- let t := l.lift (pullback_cone.mk _ _ he.w),
-- have ht : t ≫ h = he.left := l.fac _ walking_cospan.left,
-- have kt : t ≫ k = he.right := l.fac _ walking_cospan.right,
-- apply cofork.is_colimit.mk _ _ _ _,
-- { intro s,
-- apply (cofork.is_colimit.desc' he.is_colimit s.π _).1,
-- rw [← ht, assoc, s.condition, reassoc_of kt] },
-- { intro s,
-- apply (cofork.is_colimit.desc' he.is_colimit s.π _).2 },
-- { intros s m w,
-- apply he.is_colimit.hom_ext,
-- rintro ⟨⟩,
-- change (he.left ≫ e) ≫ m = (he.left ≫ e) ≫ _,
-- rw [assoc, assoc],
-- congr' 1,
-- erw (cofork.is_colimit.desc' he.is_colimit s.π _).2,
-- apply w walking_parallel_pair.one,
-- erw (cofork.is_colimit.desc' he.is_colimit s.π _).2,
-- apply w walking_parallel_pair.one }
-- end
instance mono_post_of_mono {A X Y : C} (f : X ⟶ Y) [mono f] : mono ((exp A).map f) :=
⟨λ Z g h eq, by rw [← uncurry_injective.eq_iff, ← cancel_mono f, ← uncurry_natural_right, ← uncurry_natural_right, eq]⟩
-- local attribute [instance] limits.has_coequalizers_of_has_finite_colimits
def tag' (n : ℕ) (A B : C) (f : A ⟶ B) := f
set_option pp.implicit false
-- lemma pullback_image_fac {X Y Z : C} (f : Y ⟶ Z) (g : X ⟶ Z) [has_coequalizers.{v} C] :
-- (pullback_image f g).hom ≫ image.ι (pullback.snd : pullback g f ⟶ Y) = (pullback.snd : pullback (image.ι g) f ⟶ Y) :=
-- is_image.lift_fac _ _
-- lemma pullback_image_inv_fac {X Y Z : C} (f : Y ⟶ Z) (g : X ⟶ Z) [has_coequalizers.{v} C] :
-- (pullback_image f g).inv ≫ (pullback.snd : pullback (image.ι g) f ⟶ Y) = image.ι (pullback.snd : pullback g f ⟶ Y) :=
-- image.lift_fac _
def dense_image_pullback_of_dense_image {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) [closure.dense j (image.ι g)] :
closure.dense j (image.ι (pullback.snd : pullback g f ⟶ X)) :=
begin
rw ← pullback_image_inv_fac f g,
apply closure.dense_of_iso _ _ _,
apply closure.dense_pullback,
end
lemma cancel_dense_image {P X : C} (Y : sheaf j) (r : P ⟶ X) (u v : X ⟶ Y.A) [closure.dense j (image.ι r)] :
r ≫ u = r ≫ v → u = v :=
begin
intro eq,
rw [← image.fac r, assoc, assoc, cancel_epi (factor_thru_image r)] at eq,
apply cancel_dense Y _ _ _ eq,
end
def M (A : C) : C := image (named_factors j A).1
def M_sub (A : C) : M j A ⟶ (Pj j A).A := image.ι _
instance M_sub_mono (A : C) : mono (M_sub j A) :=
begin
rw [M_sub],
apply_instance
end
def L' (A : C) : C := closure.obj j (M_sub j A)
-- Sheafification!
def L (A : C) : sheaf j := subobject_of_closed_sheaf j (Pj j A) (L' j A) (closure.arrow j (M_sub j A))
def main_kernel_pair (A : C) :
is_kernel_pair (factor_thru_image (named_factors j A).1) (j_equal j A).a (j_equal j A).b :=
begin
have := j_eq_kernel_pair j A,
rw [← (named_factors j A).2, ← image.fac (named_factors j A).1, assoc] at this,
apply this.cancel_right_of_mono,
end
def main_coequalizer (A : C) : is_colimit (cofork.of_π (factor_thru_image (named_factors j A).val) (main_kernel_pair j A).comm) :=
is_kernel_pair.to_coequalizer _
@[simps]
def equivalate (A : C) (B : sheaf j) : (L j A ⟶ B) ≃ (A ⟶ (sheaf.forget j).obj B) :=
{ to_fun := λ f, factor_thru_image (named_factors j A).1 ≫ closure.less_than_closure j _ ≫ f,
inv_fun := λ f,
begin
have : (j_equal j A).a ≫ f = (j_equal j A).b ≫ f,
refine unique_ext B (closure.less_than_closure j (equality A)) f _ _ _ _;
simp [j_equal, closure.is_lt_assoc, equality, relation.of_pair],
let q : M j A ⟶ B.A := (cofork.is_colimit.desc' (main_coequalizer j A) f this).1,
exact extend_map B (closure.less_than_closure j (M_sub j A)) q,
end,
left_inv := λ f,
begin
symmetry,
apply unique_extension,
apply @epi.left_cancellation _ _ _ _ (factor_thru_image (named_factors j A).val),
symmetry,
apply (cofork.is_colimit.desc' (main_coequalizer j A) _ _).2
end,
right_inv := λ f,
begin
dsimp,
conv_lhs {congr, skip, apply_congr extend_map_prop},
apply (cofork.is_colimit.desc' (main_coequalizer j A) _ _).2
end }
def sheafification : C ⥤ sheaf j :=
begin
apply adjunction.left_adjoint_of_equiv (equivalate j),
intros A B B' g h,
dsimp [equivalate],
rw [assoc, assoc], refl,
end
def sheafification_is_adjoint : sheafification j ⊣ sheaf.forget j :=
adjunction.adjunction_of_equiv_left _ _
def sheafy_unit (A : C) :
(sheafification_is_adjoint j).unit.app A = factor_thru_image (named_factors j A).1 ≫ closure.less_than_closure j _ :=
begin
dsimp [sheafification_is_adjoint, adjunction.adjunction_of_equiv_left, adjunction.mk_of_hom_equiv, equivalate],
erw comp_id,
end
def kernel_pair_unit (A : C) :
is_kernel_pair ((sheafification_is_adjoint j).unit.app A) (j_equal j A).a (j_equal j A).b :=
begin
rw sheafy_unit,
apply is_kernel_pair.comp_of_mono,
apply main_kernel_pair
end
def image_unit (A : C) : image ((sheafification_is_adjoint j).unit.app A) ≅ M j A :=
begin
symmetry,
apply unique_factorise _ _ (factor_thru_image _) (closure.less_than_closure j (M_sub j A)) _,
rw sheafy_unit,
end
instance unit_has_dense_image {A : C} : closure.dense j (image.ι ((sheafification_is_adjoint j).unit.app A)) :=
begin
set η := (sheafification_is_adjoint j).unit,
have : (image_unit j A).hom ≫ closure.less_than_closure j (M_sub j A) = image.ι (η.app A),
apply unique_factorise_inv_comp_mono,
rw ← this,
apply closure.dense_of_iso,
end
@[simps]
def prod_iso {X₁ X₂ Y₁ Y₂ : C} (hX : X₁ ≅ X₂) (hY : Y₁ ≅ Y₂) : X₁ ⨯ Y₁ ≅ X₂ ⨯ Y₂ :=
{ hom := limits.prod.map hX.hom hY.hom,
inv := limits.prod.map hX.inv hY.inv }
instance forget_adj : is_right_adjoint (sheaf.forget j) :=
{ left := sheafification j,
adj := adjunction.adjunction_of_equiv_left _ _ }
instance : reflective (sheaf.forget j) := {}.
def sheafification_preserves_terminal : preserves_limits_of_shape (discrete pempty) (sheafification j) :=
{ preserves_limit := λ K,
begin
haveI := nat_iso.is_iso_app_of_is_iso (sheafification_is_adjoint j).counit,
apply preserves_limit_of_iso_diagram _ (functor.unique_from_empty _).symm,
apply preserves_limit_of_preserves_limit_cone (limit.is_limit (functor.empty C)),
have i : (sheafification j).obj (⊤_ C) ≅ (⊤_ sheaf j),
apply functor.map_iso (sheafification j) (forget_terminal_sheaf j).symm ≪≫ (as_iso ((sheafification_is_adjoint j).counit.app _)),
refine ⟨λ s, default _ ≫ i.inv, λ s, _, λ s m w, _⟩,
rintro ⟨⟩,
rw iso.eq_comp_inv,
apply subsingleton.elim,
end }.
instance : exponential_ideal (sheaf.forget j) :=
exponential_ideal_of (sheaf.forget j)
begin
intros A B,
apply in_subcategory_of_has_iso _ (sheaf_exponential _ A B),
apply iso.refl _,
end
def sheafification_preserves_finite_products (J : Type v) [fintype J] [decidable_eq J] :
preserves_limits_of_shape (discrete J) (sheafification j) :=
begin
apply preserves_finite_products_of_preserves_binary_and_terminal _,
apply preserves_binary_products_of_exponential_ideal (sheaf.forget j),
apply sheafification_preserves_terminal,
apply_instance,
apply_instance
end
namespace preserve_equalizers
def aux (A : C) : closure.dense j (image.ι (limits.prod.map ((sheafification_is_adjoint j).unit.app A) ((sheafification_is_adjoint j).unit.app A))) :=
begin
set η := (sheafification_is_adjoint j).unit,
let i : image (limits.prod.map (η.app A) (η.app A)) ≅ M j A ⨯ M j A := image_prod_map (η.app A) _ ≪≫ prod_iso (image_unit j A) (image_unit j A),
have : image.ι (limits.prod.map (η.app A) (η.app A)) = i.hom ≫ limits.prod.map (closure.less_than_closure j (M_sub j A)) (closure.less_than_closure j (M_sub j A)),
change _ = (_ ≫ _) ≫ _,
dsimp [prod_iso_hom],
rw [assoc],
have : limits.prod.map (image_unit j A).hom (image_unit j A).hom ≫ limits.prod.map (closure.less_than_closure j (M_sub j A)) (closure.less_than_closure j (M_sub j A)) =
limits.prod.map ((image_unit j A).hom ≫ closure.less_than_closure j (M_sub j A)) ((image_unit j A).hom ≫ closure.less_than_closure j (M_sub j A)),
apply prod.hom_ext,
rw [assoc, limits.prod.map_fst, limits.prod.map_fst, limits.prod.map_fst_assoc],
rw [assoc, limits.prod.map_snd, limits.prod.map_snd, limits.prod.map_snd_assoc],
rw this,
have : (image_unit j A).hom ≫ closure.less_than_closure j (M_sub j A) = image.ι (η.app A),
apply unique_factorise_inv_comp_mono,
rw [this, image_prod_map_comp],
rw this,
apply closure.dense_of_iso j _ i.symm,
apply_instance,
end
-- local attribute [instance] has_equalizers_of_has_finite_limits
variables {B c : C} (f g : B ⟶ c)
def k : (sheaf.forget j).obj ((sheafification j).obj (equalizer f g)) ⟶ (sheaf.forget j).obj (equalizer ((sheafification j).map f) ((sheafification j).map g)) :=
(sheaf.forget j).map (equalizing_map (sheafification j) f g)
instance mono_k : mono (k j f g) :=
begin
let A := equalizer f g,
let L := sheafification j,
let E := equalizer (L.map f) (L.map g),
let e : A ⟶ B := equalizer.ι _ _,
let d : E ⟶ L.obj B := equalizer.ι _ _,
let k : L.obj A ⟶ E := k j f g,
have hk : k ≫ d = L.map e := equalizer.lift_ι (L.map e) _,
let η := (sheafification_is_adjoint j).unit,
change @mono C _ _ _ k,
refine ⟨λ X u v eq, _⟩,
let P := pullback (limits.prod.map (η.app A) (η.app A)) (prod.lift u v),
let r : P ⟶ X := pullback.snd,
let pq : P ⟶ A ⨯ A := pullback.fst,
let p : P ⟶ A := pq ≫ limits.prod.fst,
let q : P ⟶ A := pq ≫ limits.prod.snd,
have pb : r ≫ _ = pq ≫ _ := pullback.condition.symm,
have pb₁ : r ≫ u = p ≫ η.app A,
simpa only [prod.lift_fst, limits.prod.map_fst, assoc] using pb =≫ limits.prod.fst,
have pb₂ : r ≫ v = q ≫ η.app A,
simpa only [prod.lift_snd, limits.prod.map_snd, assoc] using pb =≫ limits.prod.snd,
have : p ≫ e ≫ η.app B = q ≫ e ≫ η.app B,
erw [η.naturality e, functor.comp_map],
conv_lhs {rw ← assoc, congr, apply_congr pb₁.symm},
conv_rhs {rw ← assoc, congr, apply_congr pb₂.symm},
conv_lhs {congr, skip, congr, apply_congr hk.symm},
conv_rhs {congr, skip, congr, apply_congr hk.symm},
change (r ≫ u) ≫ k ≫ d = (r ≫ v) ≫ k ≫ d,
simp only [assoc],
congr' 1,
simp only [← assoc],
congr' 1,
exact eq,
have : (p ≫ e) ≫ η.app B = (q ≫ e) ≫ η.app B,
rwa [← assoc, ← assoc] at this,
obtain ⟨t, ht₁, ht₂⟩ := (kernel_pair_unit j B).lift' (p ≫ e) (q ≫ e) this,
let denseB : B ⟶ closure.obj j (equality B) := closure.less_than_closure j _,
let P' := pullback denseB t,
let denseP : P' ⟶ P := pullback.snd,
have dpdq : denseP ≫ p = denseP ≫ q,
rw [← cancel_mono e, assoc, ← ht₁, assoc, ← ht₂, ← pullback.condition_assoc, ← pullback.condition_assoc],
erw [closure.is_lt_assoc, closure.is_lt_assoc, prod.lift_fst, prod.lift_snd, comp_id],
have : p ≫ η.app A = q ≫ η.app A,
apply cancel_dense _ denseP,
rw [← assoc, dpdq, assoc],
apply closure.dense_of_pullback j pullback.condition,
apply cone_is_pullback,
have rurv : r ≫ u = r ≫ v,
apply pb₁.trans (this.trans pb₂.symm),
have : closure.dense j (image.ι (limits.prod.map (η.app A) (η.app A))) := aux j A,
resetI,
haveI : closure.dense j (image.ι r) := dense_image_pullback_of_dense_image j (prod.lift u v) (limits.prod.map (η.app A) (η.app A)),
apply cancel_dense_image j (L.obj A) r u v rurv,
end
instance : closure.closed j (k j f g) :=
closed_of_subsheaf j _ _ _
noncomputable instance : closure.dense j (k j f g) :=
begin
let A := equalizer f g,
let L := sheafification j,
let E := equalizer (L.map f) (L.map g),
let e : A ⟶ B := equalizer.ι _ _,
let d : E ⟶ L.obj B := equalizer.ι _ _,
let k : (L.obj A).A ⟶ E.A := k j f g,
let k' : L.obj A ⟶ E := equalizing_map (sheafification j) f g,
have hk' : k' ≫ d = L.map e := equalizer.lift_ι (L.map e) _,
have hk : k ≫ (sheaf.forget j).map d = (sheaf.forget j).map (L.map e),
change (sheaf.forget j).map _ ≫ (sheaf.forget j).map _ = _,
rw ← (sheaf.forget j).map_comp,
congr' 1,
let η := (sheafification_is_adjoint j).unit,
change closure.dense j k,
let Q := pullback (η.app B) ((sheaf.forget j).map d),
let h : Q ⟶ B := pullback.fst,
let i : Q ⟶ E.A := pullback.snd,
have : d ≫ L.map f = d ≫ L.map g := equalizer.condition (L.map f) (L.map g),
have : (sheaf.forget j).map d ≫ (sheaf.forget j).map (L.map f) = (sheaf.forget j).map d ≫ (sheaf.forget j).map (L.map g),
rw [← (sheaf.forget j).map_comp, ← (sheaf.forget j).map_comp],
congr' 1,
have : h ≫ f ≫ η.app c = h ≫ g ≫ η.app c,
erw [η.naturality, η.naturality],
rw [pullback.condition_assoc, functor.comp_map, this, pullback.condition_assoc],
refl,
have : (h ≫ f) ≫ η.app c = (h ≫ g) ≫ η.app c,
rw [assoc, assoc, this],
obtain ⟨t, ht₁, ht₂⟩ := (kernel_pair_unit j c).lift' (h ≫ f) (h ≫ g) this,
let denseC : c ⟶ closure.obj j (equality c) := closure.less_than_closure j _,
let Q' := pullback denseC t,
let m : Q' ⟶ Q := pullback.snd,
have : (m ≫ h) ≫ f = (m ≫ h) ≫ g,
rw [assoc, assoc, ← ht₁, ← ht₂, ← pullback.condition_assoc, ← pullback.condition_assoc],
erw [closure.is_lt_assoc, closure.is_lt_assoc, prod.lift_fst, prod.lift_snd, comp_id],
obtain ⟨l', hl'⟩ := equalizer.lift' (m ≫ h) this,
obtain ⟨l, hl⟩ := extend_map' (L.obj A) m (l' ≫ η.app A),
haveI : mono ((sheaf.forget j).map d) := preserves_mono_of_preserves_pullback _ _ _ _,
have lk : l ≫ k = i,
suffices : l ≫ k ≫ (sheaf.forget j).map d = i ≫ (sheaf.forget j).map d,
simp only [← assoc] at this,
apply mono.right_cancellation _ _ this,
apply cancel_dense (L.obj B) m,
erw [hk, reassoc_of hl, ← η.naturality, functor.id_map, reassoc_of hl', pullback.condition],
let im_i : image i ⟶ E.A := image.ι i,
have : subq.mk im_i ≤ subq.mk k,
refine ⟨sub.hom_mk _ _⟩,
apply image.lift ⟨_, k, l, lk⟩,
apply image.lift_fac,
haveI : closure.dense j im_i := dense_image_pullback_of_dense_image j ((sheaf.forget j).map d) (η.app B),
have : closure.subobj j im_i ≤ closure.subobj j k := closure.mono_sub j ‹subq.mk im_i ≤ subq.mk k›,
rw closure.dense.closure_eq_top at this,
refine ⟨_⟩,
rwa eq_top_iff,
end
def sheafification_preserves_equalizer {B c : C} (f g : B ⟶ c) :
preserves_limit.{v} (parallel_pair f g) (sheafification j) :=
begin
apply equalizer_of_iso_point,
suffices : is_iso (k j f g),
{ apply is_iso_of_reflects_iso _ (sheaf.forget j),
apply this },
apply closure.is_iso_of_dense_of_closed j,
end
end preserve_equalizers
def sheafification_preserves_equalizers : preserves_limits_of_shape.{v} walking_parallel_pair (sheafification j) :=
{ preserves_limit := λ K,
begin
apply preserves_limit_of_iso_diagram (sheafification j) (diagram_iso_parallel_pair _).symm,
apply preserve_equalizers.sheafification_preserves_equalizer,
end }
end category_theory
|
9bd0c4934a64d94fa854988bb988caedc0a5327f | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/topology/order.lean | 4b15e7b6953bcbf11eefa1864f5616db46853c7d | [
"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 | 33,788 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import topology.tactic
/-!
# Ordering on topologies and (co)induced topologies
Topologies on a fixed type `α` are ordered, by reverse inclusion.
That is, for topologies `t₁` and `t₂` on `α`, we write `t₁ ≤ t₂`
if every set open in `t₂` is also open in `t₁`.
(One also calls `t₁` finer than `t₂`, and `t₂` coarser than `t₁`.)
Any function `f : α → β` induces
`induced f : topological_space β → topological_space α`
and `coinduced f : topological_space α → topological_space β`.
Continuity, the ordering on topologies and (co)induced topologies are
related as follows:
* The identity map (α, t₁) → (α, t₂) is continuous iff t₁ ≤ t₂.
* A map f : (α, t) → (β, u) is continuous
iff t ≤ induced f u (`continuous_iff_le_induced`)
iff coinduced f t ≤ u (`continuous_iff_coinduced_le`).
Topologies on α form a complete lattice, with ⊥ the discrete topology
and ⊤ the indiscrete topology.
For a function f : α → β, (coinduced f, induced f) is a Galois connection
between topologies on α and topologies on β.
## Implementation notes
There is a Galois insertion between topologies on α (with the inclusion ordering)
and all collections of sets in α. The complete lattice structure on topologies
on α is defined as the reverse of the one obtained via this Galois insertion.
## Tags
finer, coarser, induced topology, coinduced topology
-/
open set filter classical
open_locale classical topological_space filter
universes u v w
namespace topological_space
variables {α : Type u}
/-- The open sets of the least topology containing a collection of basic sets. -/
inductive generate_open (g : set (set α)) : set α → Prop
| basic : ∀s∈g, generate_open s
| univ : generate_open univ
| inter : ∀s t, generate_open s → generate_open t → generate_open (s ∩ t)
| sUnion : ∀k, (∀s∈k, generate_open s) → generate_open (⋃₀ k)
/-- The smallest topological space containing the collection `g` of basic sets -/
def generate_from (g : set (set α)) : topological_space α :=
{ is_open := generate_open g,
is_open_univ := generate_open.univ,
is_open_inter := generate_open.inter,
is_open_sUnion := generate_open.sUnion }
lemma nhds_generate_from {g : set (set α)} {a : α} :
@nhds α (generate_from g) a = (⨅s∈{s | a ∈ s ∧ s ∈ g}, 𝓟 s) :=
by rw nhds_def; exact le_antisymm
(infi_le_infi $ assume s, infi_le_infi_const $ assume ⟨as, sg⟩, ⟨as, generate_open.basic _ sg⟩)
(le_infi $ assume s, le_infi $ assume ⟨as, hs⟩,
begin
revert as, clear_, induction hs,
case generate_open.basic : s hs
{ exact assume as, infi_le_of_le s $ infi_le _ ⟨as, hs⟩ },
case generate_open.univ
{ rw [principal_univ],
exact assume _, le_top },
case generate_open.inter : s t hs' ht' hs ht
{ exact assume ⟨has, hat⟩, calc _ ≤ 𝓟 s ⊓ 𝓟 t : le_inf (hs has) (ht hat)
... = _ : inf_principal },
case generate_open.sUnion : k hk' hk
{ exact λ ⟨t, htk, hat⟩, calc _ ≤ 𝓟 t : hk t htk hat
... ≤ _ : le_principal_iff.2 $ subset_sUnion_of_mem htk }
end)
lemma tendsto_nhds_generate_from {β : Type*} {m : α → β} {f : filter α} {g : set (set β)} {b : β}
(h : ∀s∈g, b ∈ s → m ⁻¹' s ∈ f) : tendsto m f (@nhds β (generate_from g) b) :=
by rw [nhds_generate_from]; exact
(tendsto_infi.2 $ assume s, tendsto_infi.2 $ assume ⟨hbs, hsg⟩, tendsto_principal.2 $ h s hsg hbs)
/-- Construct a topology on α given the filter of neighborhoods of each point of α. -/
protected def mk_of_nhds (n : α → filter α) : topological_space α :=
{ is_open := λs, ∀a∈s, s ∈ n a,
is_open_univ := assume x h, univ_mem,
is_open_inter := assume s t hs ht x ⟨hxs, hxt⟩, inter_mem (hs x hxs) (ht x hxt),
is_open_sUnion := assume s hs a ⟨x, hx, hxa⟩,
mem_of_superset (hs x hx _ hxa) (set.subset_sUnion_of_mem hx) }
lemma nhds_mk_of_nhds (n : α → filter α) (a : α)
(h₀ : pure ≤ n) (h₁ : ∀{a s}, s ∈ n a → ∃ t ∈ n a, t ⊆ s ∧ ∀a' ∈ t, s ∈ n a') :
@nhds α (topological_space.mk_of_nhds n) a = n a :=
begin
letI := topological_space.mk_of_nhds n,
refine le_antisymm (assume s hs, _) (assume s hs, _),
{ have h₀ : {b | s ∈ n b} ⊆ s := assume b hb, mem_pure.1 $ h₀ b hb,
have h₁ : {b | s ∈ n b} ∈ 𝓝 a,
{ refine is_open.mem_nhds (assume b (hb : s ∈ n b), _) hs,
rcases h₁ hb with ⟨t, ht, hts, h⟩,
exact mem_of_superset ht h },
exact mem_of_superset h₁ h₀ },
{ rcases (@mem_nhds_iff α (topological_space.mk_of_nhds n) _ _).1 hs with ⟨t, hts, ht, hat⟩,
exact (n a).sets_of_superset (ht _ hat) hts },
end
end topological_space
section lattice
variables {α : Type u} {β : Type v}
/-- The inclusion ordering on topologies on α. We use it to get a complete
lattice instance via the Galois insertion method, but the partial order
that we will eventually impose on `topological_space α` is the reverse one. -/
def tmp_order : partial_order (topological_space α) :=
{ le := λt s, t.is_open ≤ s.is_open,
le_antisymm := assume t s h₁ h₂, topological_space_eq $ le_antisymm h₁ h₂,
le_refl := assume t, le_refl t.is_open,
le_trans := assume a b c h₁ h₂, @le_trans _ _ a.is_open b.is_open c.is_open h₁ h₂ }
local attribute [instance] tmp_order
/- We'll later restate this lemma in terms of the correct order on `topological_space α`. -/
private lemma generate_from_le_iff_subset_is_open {g : set (set α)} {t : topological_space α} :
topological_space.generate_from g ≤ t ↔ g ⊆ {s | t.is_open s} :=
iff.intro
(assume ht s hs, ht _ $ topological_space.generate_open.basic s hs)
(assume hg s hs, hs.rec_on (assume v hv, hg hv)
t.is_open_univ (assume u v _ _, t.is_open_inter u v) (assume k _, t.is_open_sUnion k))
/-- If `s` equals the collection of open sets in the topology it generates,
then `s` defines a topology. -/
protected def mk_of_closure (s : set (set α))
(hs : {u | (topological_space.generate_from s).is_open u} = s) : topological_space α :=
{ is_open := λu, u ∈ s,
is_open_univ := hs ▸ topological_space.generate_open.univ,
is_open_inter := hs ▸ topological_space.generate_open.inter,
is_open_sUnion := hs ▸ topological_space.generate_open.sUnion }
lemma mk_of_closure_sets {s : set (set α)}
{hs : {u | (topological_space.generate_from s).is_open u} = s} :
mk_of_closure s hs = topological_space.generate_from s :=
topological_space_eq hs.symm
/-- The Galois insertion between `set (set α)` and `topological_space α` whose lower part
sends a collection of subsets of α to the topology they generate, and whose upper part
sends a topology to its collection of open subsets. -/
def gi_generate_from (α : Type*) :
galois_insertion topological_space.generate_from (λt:topological_space α, {s | t.is_open s}) :=
{ gc := assume g t, generate_from_le_iff_subset_is_open,
le_l_u := assume ts s hs, topological_space.generate_open.basic s hs,
choice := λg hg, mk_of_closure g
(subset.antisymm hg $ generate_from_le_iff_subset_is_open.1 $ le_refl _),
choice_eq := assume s hs, mk_of_closure_sets }
lemma generate_from_mono {α} {g₁ g₂ : set (set α)} (h : g₁ ⊆ g₂) :
topological_space.generate_from g₁ ≤ topological_space.generate_from g₂ :=
(gi_generate_from _).gc.monotone_l h
lemma generate_from_set_of_is_open (t : topological_space α) :
topological_space.generate_from {s | t.is_open s} = t :=
(gi_generate_from α).l_u_eq t
lemma left_inverse_generate_from :
function.left_inverse topological_space.generate_from
(λ t : topological_space α, {s | t.is_open s}) :=
(gi_generate_from α).left_inverse_l_u
lemma generate_from_surjective :
function.surjective (topological_space.generate_from : set (set α) → topological_space α) :=
(gi_generate_from α).l_surjective
lemma set_of_is_open_injective :
function.injective (λ t : topological_space α, {s | t.is_open s}) :=
(gi_generate_from α).u_injective
/-- The "temporary" order `tmp_order` on `topological_space α`, i.e. the inclusion order, is a
complete lattice. (Note that later `topological_space α` will equipped with the dual order to
`tmp_order`). -/
def tmp_complete_lattice {α : Type u} : complete_lattice (topological_space α) :=
(gi_generate_from α).lift_complete_lattice
/-- The ordering on topologies on the type `α`.
`t ≤ s` if every set open in `s` is also open in `t` (`t` is finer than `s`). -/
instance : partial_order (topological_space α) :=
{ le := λ t s, s.is_open ≤ t.is_open,
le_antisymm := assume t s h₁ h₂, topological_space_eq $ le_antisymm h₂ h₁,
le_refl := assume t, le_refl t.is_open,
le_trans := assume a b c h₁ h₂, le_trans h₂ h₁ }
lemma le_generate_from_iff_subset_is_open {g : set (set α)} {t : topological_space α} :
t ≤ topological_space.generate_from g ↔ g ⊆ {s | t.is_open s} :=
generate_from_le_iff_subset_is_open
/-- Topologies on `α` form a complete lattice, with `⊥` the discrete topology
and `⊤` the indiscrete topology. The infimum of a collection of topologies
is the topology generated by all their open sets, while the supremem is the
topology whose open sets are those sets open in every member of the collection. -/
instance : complete_lattice (topological_space α) :=
@order_dual.complete_lattice _ tmp_complete_lattice
lemma is_open_implies_is_open_iff {a b : topological_space α} :
(∀ s, a.is_open s → b.is_open s) ↔ b ≤ a :=
@galois_insertion.u_le_u_iff _ (order_dual (topological_space α)) _ _ _ _ (gi_generate_from α) a b
/-- A topological space is discrete if every set is open, that is,
its topology equals the discrete topology `⊥`. -/
class discrete_topology (α : Type*) [t : topological_space α] : Prop :=
(eq_bot [] : t = ⊥)
@[priority 100]
instance discrete_topology_bot (α : Type*) : @discrete_topology α ⊥ :=
{ eq_bot := rfl }
@[simp] lemma is_open_discrete [topological_space α] [discrete_topology α] (s : set α) :
is_open s :=
(discrete_topology.eq_bot α).symm ▸ trivial
@[simp] lemma is_closed_discrete [topological_space α] [discrete_topology α] (s : set α) :
is_closed s :=
is_open_compl_iff.1 $ (discrete_topology.eq_bot α).symm ▸ trivial
lemma continuous_of_discrete_topology [topological_space α] [discrete_topology α]
[topological_space β] {f : α → β} : continuous f :=
continuous_def.2 $ λs hs, is_open_discrete _
lemma nhds_bot (α : Type*) : (@nhds α ⊥) = pure :=
begin
refine le_antisymm _ (@pure_le_nhds α ⊥),
assume a s hs,
exact @is_open.mem_nhds α ⊥ a s trivial hs
end
lemma nhds_discrete (α : Type*) [topological_space α] [discrete_topology α] : (@nhds α _) = pure :=
(discrete_topology.eq_bot α).symm ▸ nhds_bot α
lemma le_of_nhds_le_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₁ x ≤ @nhds α t₂ x) :
t₁ ≤ t₂ :=
assume s, show @is_open α t₂ s → @is_open α t₁ s,
by { simp only [is_open_iff_nhds, le_principal_iff], exact assume hs a ha, h _ $ hs _ ha }
lemma eq_of_nhds_eq_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₁ x = @nhds α t₂ x) :
t₁ = t₂ :=
le_antisymm
(le_of_nhds_le_nhds $ assume x, le_of_eq $ h x)
(le_of_nhds_le_nhds $ assume x, le_of_eq $ (h x).symm)
lemma eq_bot_of_singletons_open {t : topological_space α} (h : ∀ x, t.is_open {x}) : t = ⊥ :=
bot_unique $ λ s hs, bUnion_of_singleton s ▸ is_open_bUnion (λ x _, h x)
lemma forall_open_iff_discrete {X : Type*} [topological_space X] :
(∀ s : set X, is_open s) ↔ discrete_topology X :=
⟨λ h, ⟨by { ext U , show is_open U ↔ true, simp [h U] }⟩, λ a, @is_open_discrete _ _ a⟩
lemma singletons_open_iff_discrete {X : Type*} [topological_space X] :
(∀ a : X, is_open ({a} : set X)) ↔ discrete_topology X :=
⟨λ h, ⟨eq_bot_of_singletons_open h⟩, λ a _, @is_open_discrete _ _ a _⟩
end lattice
section galois_connection
variables {α : Type*} {β : Type*} {γ : Type*}
/-- Given `f : α → β` and a topology on `β`, the induced topology on `α` is the collection of
sets that are preimages of some open set in `β`. This is the coarsest topology that
makes `f` continuous. -/
def topological_space.induced {α : Type u} {β : Type v} (f : α → β) (t : topological_space β) :
topological_space α :=
{ is_open := λs, ∃s', t.is_open s' ∧ f ⁻¹' s' = s,
is_open_univ := ⟨univ, t.is_open_univ, preimage_univ⟩,
is_open_inter := by rintro s₁ s₂ ⟨s'₁, hs₁, rfl⟩ ⟨s'₂, hs₂, rfl⟩;
exact ⟨s'₁ ∩ s'₂, t.is_open_inter _ _ hs₁ hs₂, preimage_inter⟩,
is_open_sUnion := assume s h,
begin
simp only [classical.skolem] at h,
cases h with f hf,
apply exists.intro (⋃(x : set α) (h : x ∈ s), f x h),
simp only [sUnion_eq_bUnion, preimage_Union, (λx h, (hf x h).right)], refine ⟨_, rfl⟩,
exact (@is_open_Union β _ t _ $ assume i,
show is_open (⋃h, f i h), from @is_open_Union β _ t _ $ assume h, (hf i h).left)
end }
lemma is_open_induced_iff [t : topological_space β] {s : set α} {f : α → β} :
@is_open α (t.induced f) s ↔ (∃t, is_open t ∧ f ⁻¹' t = s) :=
iff.rfl
lemma is_open_induced_iff' [t : topological_space β] {s : set α} {f : α → β} :
(t.induced f).is_open s ↔ (∃t, is_open t ∧ f ⁻¹' t = s) :=
iff.rfl
lemma is_closed_induced_iff [t : topological_space β] {s : set α} {f : α → β} :
@is_closed α (t.induced f) s ↔ (∃t, is_closed t ∧ f ⁻¹' t = s) :=
begin
simp only [← is_open_compl_iff, is_open_induced_iff],
exact ⟨λ ⟨t, ht, heq⟩, ⟨tᶜ, by rwa compl_compl, by simp [preimage_compl, heq, compl_compl]⟩,
λ ⟨t, ht, heq⟩, ⟨tᶜ, ht, by simp only [preimage_compl, heq.symm]⟩⟩
end
/-- Given `f : α → β` and a topology on `α`, the coinduced topology on `β` is defined
such that `s:set β` is open if the preimage of `s` is open. This is the finest topology that
makes `f` continuous. -/
def topological_space.coinduced {α : Type u} {β : Type v} (f : α → β) (t : topological_space α) :
topological_space β :=
{ is_open := λs, t.is_open (f ⁻¹' s),
is_open_univ := by rw preimage_univ; exact t.is_open_univ,
is_open_inter := assume s₁ s₂ h₁ h₂, by rw preimage_inter; exact t.is_open_inter _ _ h₁ h₂,
is_open_sUnion := assume s h, by rw [preimage_sUnion]; exact (@is_open_Union _ _ t _ $ assume i,
show is_open (⋃ (H : i ∈ s), f ⁻¹' i), from
@is_open_Union _ _ t _ $ assume hi, h i hi) }
lemma is_open_coinduced {t : topological_space α} {s : set β} {f : α → β} :
@is_open β (topological_space.coinduced f t) s ↔ is_open (f ⁻¹' s) :=
iff.rfl
lemma preimage_nhds_coinduced [topological_space α] {π : α → β} {s : set β}
{a : α} (hs : s ∈ @nhds β (topological_space.coinduced π ‹_›) (π a)) : π ⁻¹' s ∈ 𝓝 a :=
begin
letI := topological_space.coinduced π ‹_›,
rcases mem_nhds_iff.mp hs with ⟨V, hVs, V_op, mem_V⟩,
exact mem_nhds_iff.mpr ⟨π ⁻¹' V, set.preimage_mono hVs, V_op, mem_V⟩
end
variables {t t₁ t₂ : topological_space α} {t' : topological_space β} {f : α → β} {g : β → α}
lemma continuous.coinduced_le (h : @continuous α β t t' f) :
t.coinduced f ≤ t' :=
λ s hs, (continuous_def.1 h s hs : _)
lemma coinduced_le_iff_le_induced {f : α → β} {tα : topological_space α}
{tβ : topological_space β} :
tα.coinduced f ≤ tβ ↔ tα ≤ tβ.induced f :=
iff.intro
(assume h s ⟨t, ht, hst⟩, hst ▸ h _ ht)
(assume h s hs, show tα.is_open (f ⁻¹' s), from h _ ⟨s, hs, rfl⟩)
lemma continuous.le_induced (h : @continuous α β t t' f) :
t ≤ t'.induced f :=
coinduced_le_iff_le_induced.1 h.coinduced_le
lemma gc_coinduced_induced (f : α → β) :
galois_connection (topological_space.coinduced f) (topological_space.induced f) :=
assume f g, coinduced_le_iff_le_induced
lemma induced_mono (h : t₁ ≤ t₂) : t₁.induced g ≤ t₂.induced g :=
(gc_coinduced_induced g).monotone_u h
lemma coinduced_mono (h : t₁ ≤ t₂) : t₁.coinduced f ≤ t₂.coinduced f :=
(gc_coinduced_induced f).monotone_l h
@[simp] lemma induced_top : (⊤ : topological_space α).induced g = ⊤ :=
(gc_coinduced_induced g).u_top
@[simp] lemma induced_inf : (t₁ ⊓ t₂).induced g = t₁.induced g ⊓ t₂.induced g :=
(gc_coinduced_induced g).u_inf
@[simp] lemma induced_infi {ι : Sort w} {t : ι → topological_space α} :
(⨅i, t i).induced g = (⨅i, (t i).induced g) :=
(gc_coinduced_induced g).u_infi
@[simp] lemma coinduced_bot : (⊥ : topological_space α).coinduced f = ⊥ :=
(gc_coinduced_induced f).l_bot
@[simp] lemma coinduced_sup : (t₁ ⊔ t₂).coinduced f = t₁.coinduced f ⊔ t₂.coinduced f :=
(gc_coinduced_induced f).l_sup
@[simp] lemma coinduced_supr {ι : Sort w} {t : ι → topological_space α} :
(⨆i, t i).coinduced f = (⨆i, (t i).coinduced f) :=
(gc_coinduced_induced f).l_supr
lemma induced_id [t : topological_space α] : t.induced id = t :=
topological_space_eq $ funext $ assume s, propext $
⟨assume ⟨s', hs, h⟩, h ▸ hs, assume hs, ⟨s, hs, rfl⟩⟩
lemma induced_compose [tγ : topological_space γ]
{f : α → β} {g : β → γ} : (tγ.induced g).induced f = tγ.induced (g ∘ f) :=
topological_space_eq $ funext $ assume s, propext $
⟨assume ⟨s', ⟨s, hs, h₂⟩, h₁⟩, h₁ ▸ h₂ ▸ ⟨s, hs, rfl⟩,
assume ⟨s, hs, h⟩, ⟨preimage g s, ⟨s, hs, rfl⟩, h ▸ rfl⟩⟩
lemma induced_const [t : topological_space α] {x : α} :
t.induced (λ y : β, x) = ⊤ :=
le_antisymm le_top (@continuous_const β α ⊤ t x).le_induced
lemma coinduced_id [t : topological_space α] : t.coinduced id = t :=
topological_space_eq rfl
lemma coinduced_compose [tα : topological_space α]
{f : α → β} {g : β → γ} : (tα.coinduced f).coinduced g = tα.coinduced (g ∘ f) :=
topological_space_eq rfl
end galois_connection
/- constructions using the complete lattice structure -/
section constructions
open topological_space
variables {α : Type u} {β : Type v}
instance inhabited_topological_space {α : Type u} : inhabited (topological_space α) :=
⟨⊤⟩
@[priority 100]
instance subsingleton.unique_topological_space [subsingleton α] :
unique (topological_space α) :=
{ default := ⊥,
uniq := λ t, eq_bot_of_singletons_open $ λ x, subsingleton.set_cases
(@is_open_empty _ t) (@is_open_univ _ t) ({x} : set α) }
@[priority 100]
instance subsingleton.discrete_topology [t : topological_space α] [subsingleton α] :
discrete_topology α :=
⟨unique.eq_default t⟩
instance : topological_space empty := ⊥
instance : discrete_topology empty := ⟨rfl⟩
instance : topological_space pempty := ⊥
instance : discrete_topology pempty := ⟨rfl⟩
instance : topological_space punit := ⊥
instance : discrete_topology punit := ⟨rfl⟩
instance : topological_space bool := ⊥
instance : discrete_topology bool := ⟨rfl⟩
instance : topological_space ℕ := ⊥
instance : discrete_topology ℕ := ⟨rfl⟩
instance : topological_space ℤ := ⊥
instance : discrete_topology ℤ := ⟨rfl⟩
instance sierpinski_space : topological_space Prop :=
generate_from {{true}}
lemma le_generate_from {t : topological_space α} { g : set (set α) } (h : ∀s∈g, is_open s) :
t ≤ generate_from g :=
le_generate_from_iff_subset_is_open.2 h
lemma induced_generate_from_eq {α β} {b : set (set β)} {f : α → β} :
(generate_from b).induced f = topological_space.generate_from (preimage f '' b) :=
le_antisymm
(le_generate_from $ ball_image_iff.2 $ assume s hs, ⟨s, generate_open.basic _ hs, rfl⟩)
(coinduced_le_iff_le_induced.1 $ le_generate_from $ assume s hs,
generate_open.basic _ $ mem_image_of_mem _ hs)
lemma le_induced_generate_from {α β} [t : topological_space α] {b : set (set β)}
{f : α → β} (h : ∀ (a : set β), a ∈ b → is_open (f ⁻¹' a)) : t ≤ induced f (generate_from b) :=
begin
rw induced_generate_from_eq,
apply le_generate_from,
simp only [mem_image, and_imp, forall_apply_eq_imp_iff₂, exists_imp_distrib],
exact h,
end
/-- This construction is left adjoint to the operation sending a topology on `α`
to its neighborhood filter at a fixed point `a : α`. -/
protected def topological_space.nhds_adjoint (a : α) (f : filter α) : topological_space α :=
{ is_open := λs, a ∈ s → s ∈ f,
is_open_univ := assume s, univ_mem,
is_open_inter := assume s t hs ht ⟨has, hat⟩, inter_mem (hs has) (ht hat),
is_open_sUnion := assume k hk ⟨u, hu, hau⟩, mem_of_superset (hk u hu hau)
(subset_sUnion_of_mem hu) }
lemma gc_nhds (a : α) :
galois_connection (topological_space.nhds_adjoint a) (λt, @nhds α t a) :=
assume f t, by { rw le_nhds_iff, exact ⟨λ H s hs has, H _ has hs, λ H s has hs, H _ hs has⟩ }
lemma nhds_mono {t₁ t₂ : topological_space α} {a : α} (h : t₁ ≤ t₂) :
@nhds α t₁ a ≤ @nhds α t₂ a := (gc_nhds a).monotone_u h
lemma nhds_infi {ι : Sort*} {t : ι → topological_space α} {a : α} :
@nhds α (infi t) a = (⨅i, @nhds α (t i) a) := (gc_nhds a).u_infi
lemma nhds_Inf {s : set (topological_space α)} {a : α} :
@nhds α (Inf s) a = (⨅t∈s, @nhds α t a) := (gc_nhds a).u_Inf
lemma nhds_inf {t₁ t₂ : topological_space α} {a : α} :
@nhds α (t₁ ⊓ t₂) a = @nhds α t₁ a ⊓ @nhds α t₂ a := (gc_nhds a).u_inf
lemma nhds_top {a : α} : @nhds α ⊤ a = ⊤ := (gc_nhds a).u_top
local notation `cont` := @continuous _ _
local notation `tspace` := topological_space
open topological_space
variables {γ : Type*} {f : α → β} {ι : Sort*}
lemma continuous_iff_coinduced_le {t₁ : tspace α} {t₂ : tspace β} :
cont t₁ t₂ f ↔ coinduced f t₁ ≤ t₂ :=
continuous_def.trans iff.rfl
lemma continuous_iff_le_induced {t₁ : tspace α} {t₂ : tspace β} :
cont t₁ t₂ f ↔ t₁ ≤ induced f t₂ :=
iff.trans continuous_iff_coinduced_le (gc_coinduced_induced f _ _)
theorem continuous_generated_from {t : tspace α} {b : set (set β)}
(h : ∀s∈b, is_open (f ⁻¹' s)) : cont t (generate_from b) f :=
continuous_iff_coinduced_le.2 $ le_generate_from h
@[continuity]
lemma continuous_induced_dom {t : tspace β} : cont (induced f t) t f :=
by { rw continuous_def, assume s h, exact ⟨_, h, rfl⟩ }
lemma continuous_induced_rng {g : γ → α} {t₂ : tspace β} {t₁ : tspace γ}
(h : cont t₁ t₂ (f ∘ g)) : cont t₁ (induced f t₂) g :=
begin
rw continuous_def,
rintros s ⟨t, ht, s_eq⟩,
simpa [← s_eq] using continuous_def.1 h t ht,
end
lemma continuous_induced_rng' [topological_space α] [topological_space β] [topological_space γ]
{g : γ → α} (f : α → β) (H : ‹topological_space α› = ‹topological_space β›.induced f)
(h : continuous (f ∘ g)) : continuous g :=
H.symm ▸ continuous_induced_rng h
lemma continuous_coinduced_rng {t : tspace α} : cont t (coinduced f t) f :=
by { rw continuous_def, assume s h, exact h }
lemma continuous_coinduced_dom {g : β → γ} {t₁ : tspace α} {t₂ : tspace γ}
(h : cont t₁ t₂ (g ∘ f)) : cont (coinduced f t₁) t₂ g :=
begin
rw continuous_def at h ⊢,
assume s hs,
exact h _ hs
end
lemma continuous_le_dom {t₁ t₂ : tspace α} {t₃ : tspace β}
(h₁ : t₂ ≤ t₁) (h₂ : cont t₁ t₃ f) : cont t₂ t₃ f :=
begin
rw continuous_def at h₂ ⊢,
assume s h,
exact h₁ _ (h₂ s h)
end
lemma continuous_le_rng {t₁ : tspace α} {t₂ t₃ : tspace β}
(h₁ : t₂ ≤ t₃) (h₂ : cont t₁ t₂ f) : cont t₁ t₃ f :=
begin
rw continuous_def at h₂ ⊢,
assume s h,
exact h₂ s (h₁ s h)
end
lemma continuous_sup_dom {t₁ t₂ : tspace α} {t₃ : tspace β}
(h₁ : cont t₁ t₃ f) (h₂ : cont t₂ t₃ f) : cont (t₁ ⊔ t₂) t₃ f :=
begin
rw continuous_def at h₁ h₂ ⊢,
assume s h,
exact ⟨h₁ s h, h₂ s h⟩
end
lemma continuous_sup_rng_left {t₁ : tspace α} {t₃ t₂ : tspace β} :
cont t₁ t₂ f → cont t₁ (t₂ ⊔ t₃) f :=
continuous_le_rng le_sup_left
lemma continuous_sup_rng_right {t₁ : tspace α} {t₃ t₂ : tspace β} :
cont t₁ t₃ f → cont t₁ (t₂ ⊔ t₃) f :=
continuous_le_rng le_sup_right
lemma continuous_Sup_dom {t₁ : set (tspace α)} {t₂ : tspace β}
(h : ∀t∈t₁, cont t t₂ f) : cont (Sup t₁) t₂ f :=
continuous_iff_le_induced.2 $ Sup_le $ assume t ht, continuous_iff_le_induced.1 $ h t ht
lemma continuous_Sup_rng {t₁ : tspace α} {t₂ : set (tspace β)} {t : tspace β}
(h₁ : t ∈ t₂) (hf : cont t₁ t f) : cont t₁ (Sup t₂) f :=
continuous_iff_coinduced_le.2 $ le_Sup_of_le h₁ $ continuous_iff_coinduced_le.1 hf
lemma continuous_supr_dom {t₁ : ι → tspace α} {t₂ : tspace β}
(h : ∀i, cont (t₁ i) t₂ f) : cont (supr t₁) t₂ f :=
continuous_Sup_dom $ assume t ⟨i, (t_eq : t₁ i = t)⟩, t_eq ▸ h i
lemma continuous_supr_rng {t₁ : tspace α} {t₂ : ι → tspace β} {i : ι}
(h : cont t₁ (t₂ i) f) : cont t₁ (supr t₂) f :=
continuous_Sup_rng ⟨i, rfl⟩ h
lemma continuous_inf_rng {t₁ : tspace α} {t₂ t₃ : tspace β}
(h₁ : cont t₁ t₂ f) (h₂ : cont t₁ t₃ f) : cont t₁ (t₂ ⊓ t₃) f :=
continuous_iff_coinduced_le.2 $ le_inf
(continuous_iff_coinduced_le.1 h₁)
(continuous_iff_coinduced_le.1 h₂)
lemma continuous_inf_dom_left {t₁ t₂ : tspace α} {t₃ : tspace β} :
cont t₁ t₃ f → cont (t₁ ⊓ t₂) t₃ f :=
continuous_le_dom inf_le_left
lemma continuous_inf_dom_right {t₁ t₂ : tspace α} {t₃ : tspace β} :
cont t₂ t₃ f → cont (t₁ ⊓ t₂) t₃ f :=
continuous_le_dom inf_le_right
lemma continuous_Inf_dom {t₁ : set (tspace α)} {t₂ : tspace β} {t : tspace α} (h₁ : t ∈ t₁) :
cont t t₂ f → cont (Inf t₁) t₂ f :=
continuous_le_dom $ Inf_le h₁
lemma continuous_Inf_rng {t₁ : tspace α} {t₂ : set (tspace β)}
(h : ∀t∈t₂, cont t₁ t f) : cont t₁ (Inf t₂) f :=
continuous_iff_coinduced_le.2 $ le_Inf $ assume b hb, continuous_iff_coinduced_le.1 $ h b hb
lemma continuous_infi_dom {t₁ : ι → tspace α} {t₂ : tspace β} {i : ι} :
cont (t₁ i) t₂ f → cont (infi t₁) t₂ f :=
continuous_le_dom $ infi_le _ _
lemma continuous_infi_rng {t₁ : tspace α} {t₂ : ι → tspace β}
(h : ∀i, cont t₁ (t₂ i) f) : cont t₁ (infi t₂) f :=
continuous_iff_coinduced_le.2 $ le_infi $ assume i, continuous_iff_coinduced_le.1 $ h i
@[continuity] lemma continuous_bot {t : tspace β} : cont ⊥ t f :=
continuous_iff_le_induced.2 $ bot_le
@[continuity] lemma continuous_top {t : tspace α} : cont t ⊤ f :=
continuous_iff_coinduced_le.2 $ le_top
/- 𝓝 in the induced topology -/
theorem mem_nhds_induced [T : topological_space α] (f : β → α) (a : β) (s : set β) :
s ∈ @nhds β (topological_space.induced f T) a ↔ ∃ u ∈ 𝓝 (f a), f ⁻¹' u ⊆ s :=
begin
simp only [mem_nhds_iff, is_open_induced_iff, exists_prop, set.mem_set_of_eq],
split,
{ rintros ⟨u, usub, ⟨v, openv, ueq⟩, au⟩,
exact ⟨v, ⟨v, set.subset.refl v, openv, by rwa ←ueq at au⟩, by rw ueq; exact usub⟩ },
rintros ⟨u, ⟨v, vsubu, openv, amem⟩, finvsub⟩,
exact ⟨f ⁻¹' v, set.subset.trans (set.preimage_mono vsubu) finvsub, ⟨⟨v, openv, rfl⟩, amem⟩⟩
end
theorem nhds_induced [T : topological_space α] (f : β → α) (a : β) :
@nhds β (topological_space.induced f T) a = comap f (𝓝 (f a)) :=
by { ext s, rw [mem_nhds_induced, mem_comap] }
lemma induced_iff_nhds_eq [tα : topological_space α] [tβ : topological_space β] (f : β → α) :
tβ = tα.induced f ↔ ∀ b, 𝓝 b = comap f (𝓝 $ f b) :=
⟨λ h a, h.symm ▸ nhds_induced f a, λ h, eq_of_nhds_eq_nhds $ λ x, by rw [h, nhds_induced]⟩
theorem map_nhds_induced_of_surjective [T : topological_space α]
{f : β → α} (hf : function.surjective f) (a : β) :
map f (@nhds β (topological_space.induced f T) a) = 𝓝 (f a) :=
by rw [nhds_induced, map_comap_of_surjective hf]
end constructions
section induced
open topological_space
variables {α : Type*} {β : Type*}
variables [t : topological_space β] {f : α → β}
theorem is_open_induced_eq {s : set α} :
@is_open _ (induced f t) s ↔ s ∈ preimage f '' {s | is_open s} :=
iff.rfl
theorem is_open_induced {s : set β} (h : is_open s) : (induced f t).is_open (f ⁻¹' s) :=
⟨s, h, rfl⟩
lemma map_nhds_induced_eq (a : α) : map f (@nhds α (induced f t) a) = 𝓝[range f] (f a) :=
by rw [nhds_induced, filter.map_comap, nhds_within]
lemma map_nhds_induced_of_mem {a : α} (h : range f ∈ 𝓝 (f a)) :
map f (@nhds α (induced f t) a) = 𝓝 (f a) :=
by rw [nhds_induced, filter.map_comap_of_mem h]
lemma closure_induced [t : topological_space β] {f : α → β} {a : α} {s : set α} :
a ∈ @closure α (topological_space.induced f t) s ↔ f a ∈ closure (f '' s) :=
by simp only [mem_closure_iff_frequently, nhds_induced, frequently_comap, mem_image, and_comm]
end induced
section sierpinski
variables {α : Type*} [topological_space α]
@[simp] lemma is_open_singleton_true : is_open ({true} : set Prop) :=
topological_space.generate_open.basic _ (by simp)
lemma continuous_Prop {p : α → Prop} : continuous p ↔ is_open {x | p x} :=
⟨assume h : continuous p,
have is_open (p ⁻¹' {true}),
from is_open_singleton_true.preimage h,
by simp [preimage, eq_true] at this; assumption,
assume h : is_open {x | p x},
continuous_generated_from $ assume s (hs : s ∈ {{true}}),
by simp at hs; simp [hs, preimage, eq_true, h]⟩
lemma is_open_iff_continuous_mem {s : set α} : is_open s ↔ continuous (λ x, x ∈ s) :=
continuous_Prop.symm
end sierpinski
section infi
variables {α : Type u} {ι : Sort v}
lemma generate_from_union (a₁ a₂ : set (set α)) :
topological_space.generate_from (a₁ ∪ a₂) =
topological_space.generate_from a₁ ⊓ topological_space.generate_from a₂ :=
@galois_connection.l_sup _ (order_dual (topological_space α)) a₁ a₂ _ _ _ _
(λ g t, generate_from_le_iff_subset_is_open)
lemma set_of_is_open_sup (t₁ t₂ : topological_space α) :
{s | (t₁ ⊔ t₂).is_open s} = {s | t₁.is_open s} ∩ {s | t₂.is_open s} :=
@galois_connection.u_inf _ (order_dual (topological_space α)) t₁ t₂ _ _ _ _
(λ g t, generate_from_le_iff_subset_is_open)
lemma generate_from_Union {f : ι → set (set α)} :
topological_space.generate_from (⋃ i, f i) = (⨅ i, topological_space.generate_from (f i)) :=
@galois_connection.l_supr _ (order_dual (topological_space α)) _ _ _ _ _
(λ g t, generate_from_le_iff_subset_is_open) f
lemma set_of_is_open_supr {t : ι → topological_space α} :
{s | (⨆ i, t i).is_open s} = ⋂ i, {s | (t i).is_open s} :=
@galois_connection.u_infi _ (order_dual (topological_space α)) _ _ _ _ _
(λ g t, generate_from_le_iff_subset_is_open) t
lemma generate_from_sUnion {S : set (set (set α))} :
topological_space.generate_from (⋃₀ S) = (⨅ s ∈ S, topological_space.generate_from s) :=
@galois_connection.l_Sup _ (order_dual (topological_space α)) _ _ _ _
(λ g t, generate_from_le_iff_subset_is_open) S
lemma set_of_is_open_Sup {T : set (topological_space α)} :
{s | (Sup T).is_open s} = ⋂ t ∈ T, {s | (t : topological_space α).is_open s} :=
@galois_connection.u_Inf _ (order_dual (topological_space α)) _ _ _ _
(λ g t, generate_from_le_iff_subset_is_open) T
lemma generate_from_union_is_open (a b : topological_space α) :
topological_space.generate_from ({s | a.is_open s} ∪ {s | b.is_open s}) = a ⊓ b :=
@galois_insertion.l_sup_u _ (order_dual (topological_space α)) _ _ _ _ (gi_generate_from α) a b
lemma generate_from_Union_is_open (f : ι → topological_space α) :
topological_space.generate_from (⋃ i, {s | (f i).is_open s}) = ⨅ i, (f i) :=
@galois_insertion.l_supr_u _ (order_dual (topological_space α)) _ _ _ _ (gi_generate_from α) _ f
lemma generate_from_inter (a b : topological_space α) :
topological_space.generate_from ({s | a.is_open s} ∩ {s | b.is_open s}) = a ⊔ b :=
@galois_insertion.l_inf_u _ (order_dual (topological_space α)) _ _ _ _
(gi_generate_from α) a b
lemma generate_from_Inter (f : ι → topological_space α) :
topological_space.generate_from (⋂ i, {s | (f i).is_open s}) = ⨆ i, (f i) :=
@galois_insertion.l_infi_u _ (order_dual (topological_space α)) _ _ _ _ (gi_generate_from α) _ f
lemma generate_from_Inter_of_generate_from_eq_self (f : ι → set (set α))
(hf : ∀ i, {s | (topological_space.generate_from (f i)).is_open s} = f i) :
topological_space.generate_from (⋂ i, (f i)) = ⨆ i, topological_space.generate_from (f i) :=
@galois_insertion.l_infi_of_ul_eq_self _ (order_dual (topological_space α)) _ _ _ _
(gi_generate_from α) _ f hf
variables {t : ι → topological_space α}
lemma is_open_supr_iff {s : set α} : @is_open _ (⨆ i, t i) s ↔ ∀ i, @is_open _ (t i) s :=
show s ∈ set_of (supr t).is_open ↔ s ∈ {x : set α | ∀ (i : ι), (t i).is_open x},
by simp [set_of_is_open_supr]
lemma is_closed_infi_iff {s : set α} : @is_closed _ (⨆ i, t i) s ↔ ∀ i, @is_closed _ (t i) s :=
by simp [← is_open_compl_iff, is_open_supr_iff]
end infi
|
4b4c43c72f4b9497836f2acaf9e4625729c0b2fa | 05f637fa14ac28031cb1ea92086a0f4eb23ff2b1 | /tests/lean/simp15.lean | 020401f6e6ffaf0c9612f853fe96d8865773dc8b | [
"Apache-2.0"
] | permissive | codyroux/lean0.1 | 1ce92751d664aacff0529e139083304a7bbc8a71 | 0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef | refs/heads/master | 1,610,830,535,062 | 1,402,150,480,000 | 1,402,150,480,000 | 19,588,851 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 444 | lean | rewrite_set simple
add_rewrite and_truer and_truel and_falser and_falsel or_falsel : simple
(*
add_congr_theorem("simple", "and_congr")
add_congr_theorem("simple", "or_congr")
*)
variables a b c : Nat
(*
local opts = options({"simplifier", "contextual"}, false)
local t = parse_lean([[a = 1 ∧ (¬ b = 0 ∨ c ≠ 0 ∨ b + c > a)]])
local s, pr = simplify(t, "simple", opts)
print(s)
print(pr)
print(get_environment():type_check(pr))
*) |
706eafdac2c72fe28a867da8fc7bb9eb83ab973e | 64874bd1010548c7f5a6e3e8902efa63baaff785 | /tests/lean/hott/noc_list.hlean | d61ccbfc63957bdf06aa6aac7201b67cddd76ecc | [
"Apache-2.0"
] | permissive | tjiaqi/lean | 4634d729795c164664d10d093f3545287c76628f | d0ce4cf62f4246b0600c07e074d86e51f2195e30 | refs/heads/master | 1,622,323,796,480 | 1,422,643,069,000 | 1,422,643,069,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 645 | hlean | inductive list (A : Type) : Type :=
nil {} : list A,
cons : A → list A → list A
namespace list
open lift
theorem nil_ne_cons {A : Type} (a : A) (v : list A) : nil ≠ cons a v :=
λ H, down (no_confusion H)
theorem cons.inj₁ {A : Type} (a₁ a₂ : A) (v₁ v₂ : list A) : cons a₁ v₁ = cons a₂ v₂ → a₁ = a₂ :=
λ H, down (no_confusion H (λ (h₁ : a₁ = a₂) (h₂ : v₁ = v₂), h₁))
theorem cons.inj₂ {A : Type} (a₁ a₂ : A) (v₁ v₂ : list A) : cons a₁ v₁ = cons a₂ v₂ → v₁ = v₂ :=
λ H, down (no_confusion H (λ (h₁ : a₁ = a₂) (h₂ : v₁ = v₂), h₂))
end list
|
31ede306f01a4262b11178e56ab8460ff003153a | 46125763b4dbf50619e8846a1371029346f4c3db | /src/group_theory/monoid_localization.lean | d762ce6b554778f4704b82d0be913674b4830af3 | [
"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 | 14,015 | lean | /-
Copyright (c) 2019 Amelia Livingston. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Amelia Livingston
-/
import group_theory.congruence
import algebra.associated
import algebra.punit_instances
/-!
# Localizations of commutative monoids
Localizing a commutative ring at one of its submonoids does not rely on the ring's addition, so
we can generalize localizations to commutative monoids.
We characterize the localization of a commutative monoid `M` at a submonoid `S` up to
isomorphism; that is, a commutative monoid `N` is the localization of `M` at `S` iff we can find a
monoid homomorphism `f : M →* N` satisfying 3 properties:
1. For all `y ∈ S`, `f y` is a unit;
2. For all `z : N`, there exists `(x, y) : M × S` such that `z * f y = f x`;
3. For all `x, y : M`, `f x = f y` iff there exists `c ∈ S` such that `x * c = y * c`.
We also define the quotient of `M × S` by the unique congruence relation (equivalence relation
preserving a binary operation) `r` such that for any other congruence relation `s` on `M × S`
satisfying '`∀ y ∈ S`, `(1, 1) ∼ (y, y)` under `s`', we have that `(x₁, y₁) ∼ (x₂, y₂)` by `s`
whenever `(x₁, y₁) ∼ (x₂, y₂)` by `r`. We show this relation is equivalent to the standard
localization relation.
This defines the localization as a quotient type, but the majority of subsequent lemmas in the file
are given in terms of localizations up to isomorphism, using maps which satisfy the characteristic
predicate.
## Implementation notes
In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one
structure with an isomorphic one; one way around this is to isolate a predicate characterizing
a structure up to isomorphism, and reason about things that satisfy the predicate.
The infimum form of the localization congruence relation is chosen as 'canonical' here, since it
shortens some proofs.
## Tags
localization, monoid localization, quotient monoid, congruence relation, characteristic predicate,
commutative monoid
-/
namespace add_submonoid
variables {M : Type*} [add_comm_monoid M] (S : add_submonoid M) (N : Type*) [add_comm_monoid N]
/-- The type of add_monoid homomorphisms satisfying the characteristic predicate: if `f : M →+ N`
satisfies this predicate, then `N` is isomorphic to the localization of `M` at `S`. -/
@[nolint has_inhabited_instance] structure localization_map :=
(to_fun : M →+ N)
(map_add_units : ∀ y : S, is_add_unit (to_fun y))
(surj : ∀ z : N, ∃ x : M × S, z + to_fun x.2 = to_fun x.1)
(eq_iff_exists : ∀ x y, to_fun x = to_fun y ↔ ∃ c : S, x + c = y + c)
end add_submonoid
variables {M : Type*} [comm_monoid M] (S : submonoid M) (N : Type*) [comm_monoid N]
{P : Type*} [comm_monoid P]
namespace submonoid
/-- The type of monoid homomorphisms satisfying the characteristic predicate: if `f : M →* N`
satisfies this predicate, then `N` is isomorphic to the localization of `M` at `S`. -/
@[nolint has_inhabited_instance] structure localization_map :=
(to_fun : M →* N)
(map_units : ∀ y : S, is_unit (to_fun y))
(surj : ∀ z : N, ∃ x : M × S, z * to_fun x.2 = to_fun x.1)
(eq_iff_exists : ∀ x y, to_fun x = to_fun y ↔ ∃ c : S, x * c = y * c)
attribute [to_additive add_submonoid.localization_map] submonoid.localization_map
namespace localization
/-- The congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose
quotient is the localization of `M` at `S`, defined as the unique congruence relation on
`M × S` such that for any other congruence relation `s` on `M × S` where for all `y ∈ S`,
`(1, 1) ∼ (y, y)` under `s`, we have that `(x₁, y₁) ∼ (x₂, y₂)` by `r` implies
`(x₁, y₁) ∼ (x₂, y₂)` by `s`. -/
@[to_additive "The congruence relation on `M × S`, `M` an `add_comm_monoid` and `S` an `add_submonoid` of `M`, whose quotient is the localization of `M` at `S`, defined as the unique congruence relation on `M × S` such that for any other congruence relation `s` on `M × S` where for all `y ∈ S`, `(0, 0) ∼ (y, y)` under `s`, we have that `(x₁, y₁) ∼ (x₂, y₂)` by `r` implies `(x₁, y₁) ∼ (x₂, y₂)` by `s`."]
def r (S : submonoid M) : con (M × S) :=
lattice.Inf {c | ∀ y : S, c 1 (y, y)}
/-- An alternate form of the congruence relation on `M × S`, `M` a `comm_monoid` and `S` a
submonoid of `M`, whose quotient is the localization of `M` at `S`. Its equivalence to `r` can
be useful for proofs. -/
@[to_additive "An alternate form of the congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose quotient is the localization of `M` at `S`. Its equivalence to `r` can be useful for proofs."]
def r' : con (M × S) :=
begin
refine { r := λ a b : M × S, ∃ c : S, a.1 * b.2 * c = b.1 * a.2 * c,
iseqv := ⟨λ a, ⟨1, rfl⟩, λ a b ⟨c, hc⟩, ⟨c, hc.symm⟩, _⟩,
.. },
{ rintros a b c ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩,
use b.2 * t₁ * t₂,
simp only [submonoid.coe_mul],
calc a.1 * c.2 * (b.2 * t₁ * t₂) = a.1 * b.2 * t₁ * c.2 * t₂ : by ac_refl
... = b.1 * c.2 * t₂ * a.2 * t₁ : by { rw ht₁, ac_refl }
... = c.1 * a.2 * (b.2 * t₁ * t₂) : by { rw ht₂, ac_refl } },
{ rintros a b c d ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩,
use t₁ * t₂,
calc (a.1 * c.1) * (b.2 * d.2) * (t₁ * t₂) = (a.1 * b.2 * t₁) * (c.1 * d.2 * t₂) :
by ac_refl
... = (b.1 * d.1) * (a.2 * c.2) * (t₁ * t₂) : by { rw [ht₁, ht₂], ac_refl } }
end
/-- The congruence relation used to localize a `comm_monoid` at a submonoid can be expressed
equivalently as an infimum (see `localization.r`) or explicitly
(see `localization.r'`). -/
@[to_additive "The additive congruence relation used to localize an `add_comm_monoid` at a submonoid can be expressed equivalently as an infimum (see `localization.r`) or explicitly (see `localization.r'`)."]
theorem r_eq_r' : r S = r' S :=
le_antisymm (lattice.Inf_le $ λ _, ⟨1, by simp⟩) $
lattice.le_Inf $ λ b H ⟨p, q⟩ y ⟨t, ht⟩,
begin
rw [← mul_one (p, q), ← mul_one y],
refine b.trans (b.mul (b.refl _) (H (y.2 * t))) _,
convert b.symm (b.mul (b.refl y) (H (q * t))); simp only [],
rw [prod.mk_mul_mk, submonoid.coe_mul, ← mul_assoc, ht, mul_left_comm, mul_assoc],
refl
end
variables {S}
@[to_additive]
lemma r_iff_exists {x y : M × S} : r S x y ↔ ∃ c : S, x.1 * y.2 * c = y.1 * x.2 * c :=
by rw r_eq_r' S; refl
end localization
/-- The localization of a `comm_monoid` at one of its submonoids (as a quotient type). -/
@[to_additive "The localization of an `add_comm_monoid` at one of its submonoids (as a quotient type)."]
def localization := (localization.r S).quotient
@[to_additive] instance localization.inhabited :
inhabited (localization S) :=
con.quotient.inhabited
namespace localization_map
variables (S) {N}
/-- Given a map `f : M →* N`, a section function sending `z : N` to some
`(x, y) : M × S` such that `f x * (f y)⁻¹ = z` if there always exists such an element. -/
@[to_additive "Given a map `f : M →+ N`, a section function sending `z : N` to some `(x, y) : M × S` such that `f x - f y = z` if there always exists such an element."]
noncomputable def sec (f : M →* N) :=
@classical.epsilon (N → M × S) ⟨λ z, 1⟩ (λ g, ∀ z, z * f (g z).2 = f (g z).1)
variables {S}
@[simp, to_additive] lemma sec_spec {f : M →* N}
(h : ∀ z : N, ∃ x : M × S, z * f x.2 = f x.1) (z : N) :
z * f (sec S f z).2 = f (sec S f z).1 :=
@classical.epsilon_spec (N → M × S) (λ g, ∀ z, z * f (g z).2 = f (g z).1)
⟨λ y, classical.some $ h y, λ y, classical.some_spec $ h y⟩ z
@[simp, to_additive] lemma sec_spec' {f : M →* N}
(h : ∀ z : N, ∃ x : M × S, z * f x.2 = f x.1) (z : N) :
f (sec S f z).1 = f (sec S f z).2 * z :=
by rw [mul_comm, sec_spec h]
@[simp, to_additive] lemma mul_inv_left {f : M →* N} (h : ∀ y : S, is_unit (f y))
(y : S) (w z) : w * ↑(is_unit.lift_right (f.restrict S) h y)⁻¹ = z ↔ w = f y * z :=
by rw mul_comm; convert units.inv_mul_eq_iff_eq_mul _;
exact (is_unit.coe_lift_right (f.restrict S) h _).symm
@[simp, to_additive] lemma mul_inv_right {f : M →* N} (h : ∀ y : S, is_unit (f y))
(y : S) (w z) : z = w * ↑(is_unit.lift_right (f.restrict S) h y)⁻¹ ↔ z * f y = w :=
by rw [eq_comm, mul_inv_left h, mul_comm, eq_comm]
@[simp, to_additive] lemma mul_inv {f : M →* N} (h : ∀ y : S, is_unit (f y)) {x₁ x₂} {y₁ y₂ : S} :
f x₁ * ↑(is_unit.lift_right (f.restrict S) h y₁)⁻¹ =
f x₂ * ↑(is_unit.lift_right (f.restrict S) h y₂)⁻¹ ↔ f (x₁ * y₂) = f (x₂ * y₁) :=
by rw [mul_inv_right h, mul_assoc, mul_comm _ (f y₂), ←mul_assoc, mul_inv_left h, mul_comm x₂,
f.map_mul, f.map_mul]
@[to_additive] lemma inv_inj {f : M →* N} (hf : ∀ y : S, is_unit (f y)) {y z}
(h : (is_unit.lift_right (f.restrict S) hf y)⁻¹ = (is_unit.lift_right (f.restrict S) hf z)⁻¹) :
f y = f z :=
by rw [←mul_one (f y), eq_comm, ←mul_inv_left hf y (f z) 1, h];
convert units.inv_mul _; exact (is_unit.coe_lift_right (f.restrict S) hf _).symm
@[to_additive] lemma inv_unique {f : M →* N} (h : ∀ y : S, is_unit (f y)) {y : S}
{z} (H : f y * z = 1) : ↑(is_unit.lift_right (f.restrict S) h y)⁻¹ = z :=
by rw [←one_mul ↑(_)⁻¹, mul_inv_left, ←H]
variables (f : localization_map S N)
/-- Given a localization map `f : M →* N`, the surjection sending `(x, y) : M × S` to
`f x * (f y)⁻¹`. -/
@[to_additive "Given a localization map `f : M →+ N`, the surjection sending `(x, y) : M × S` to `f x - f y`."]
noncomputable def mk' (f : localization_map S N) (x : M) (y : S) : N :=
f.1 x * ↑(is_unit.lift_right (f.1.restrict S) f.2 y)⁻¹
@[simp, to_additive] lemma mk'_mul (x₁ x₂ : M) (y₁ y₂ : S) :
f.mk' (x₁ * x₂) (y₁ * y₂) = f.mk' x₁ y₁ * f.mk' x₂ y₂ :=
(mul_inv_left f.2 _ _ _).2 $ show _ = _ * (_ * _ * (_ * _)), by
rw [←mul_assoc, ←mul_assoc, mul_inv_right f.2, mul_assoc, mul_assoc, mul_comm _ (f.1 x₂),
←mul_assoc, ←mul_assoc, mul_inv_right f.2, submonoid.coe_mul, f.1.map_mul, f.1.map_mul];
ac_refl
@[to_additive] lemma mk'_one (x) : f.mk' x (1 : S) = f.1 x :=
by rw [mk', monoid_hom.map_one]; simp
@[simp, to_additive] lemma mk'_sec (z : N) : f.mk' (sec S f.1 z).1 (sec S f.1 z).2 = z :=
show _ * _ = _, by rw [←sec_spec f.3, mul_inv_left, mul_comm]
@[to_additive] lemma mk'_surjective (z : N) : ∃ x (y : S), f.mk' x y = z :=
⟨(sec S f.1 z).1, (sec S f.1 z).2, f.mk'_sec z⟩
@[to_additive] lemma mk'_spec (x) (y : S) :
f.mk' x y * f.1 y = f.1 x :=
show _ * _ * _ = _, by rw [mul_assoc, mul_comm _ (f.1 y), ←mul_assoc, mul_inv_left, mul_comm]
@[to_additive] lemma mk'_spec' (x) (y : S) :
f.1 y * f.mk' x y = f.1 x :=
by rw [mul_comm, mk'_spec]
@[simp, to_additive] theorem eq_mk'_iff_mul_eq {x} {y : S} {z} :
z = f.mk' x y ↔ z * f.1 y = f.1 x :=
⟨λ H, by rw [H, mk'_spec], λ H, by erw [mul_inv_right, H]; refl⟩
@[simp, to_additive] theorem mk'_eq_iff_eq_mul {x} {y : S} {z} :
f.mk' x y = z ↔ f.1 x = z * f.1 y :=
by rw [eq_comm, eq_mk'_iff_mul_eq, eq_comm]
@[to_additive] lemma mk'_eq_iff_eq {x₁ x₂} {y₁ y₂ : S} :
f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ f.1 (x₁ * y₂) = f.1 (x₂ * y₁) :=
⟨λ H, by rw [f.1.map_mul, f.mk'_eq_iff_eq_mul.1 H, mul_assoc,
mul_comm (f.1 _), ←mul_assoc, mk'_spec, f.1.map_mul],
λ H, by rw [mk'_eq_iff_eq_mul, mk', mul_assoc, mul_comm _ (f.1 y₁), ←mul_assoc,
←f.1.map_mul, ←H, f.1.map_mul, mul_inv_right f.2]⟩
@[to_additive] protected lemma eq {a₁ b₁} {a₂ b₂ : S} :
f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ ∃ c : S, a₁ * b₂ * c = b₁ * a₂ * c :=
f.mk'_eq_iff_eq.trans $ f.4 _ _
@[to_additive] protected lemma eq' {a₁ b₁} {a₂ b₂ : S} :
f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ localization.r S (a₁, a₂) (b₁, b₂) :=
by rw [f.eq, localization.r_iff_exists]
@[to_additive] lemma eq_iff_eq (g : localization_map S P) {x y} :
f.1 x = f.1 y ↔ g.1 x = g.1 y :=
(f.4 _ _).trans (g.4 _ _).symm
@[to_additive] lemma mk'_eq_iff_mk'_eq (g : localization_map S P) {x₁ x₂}
{y₁ y₂ : S} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ g.mk' x₁ y₁ = g.mk' x₂ y₂ :=
f.eq'.trans g.eq'.symm
@[to_additive] lemma exists_of_sec_mk' (x) (y : S) :
∃ c : S, x * (sec S f.1 $ f.mk' x y).2 * c = (sec S f.1 $ f.mk' x y).1 * y * c :=
(f.4 _ _).1 $ f.mk'_eq_iff_eq.1 $ (mk'_sec _ _).symm
@[to_additive] lemma exists_of_sec (x) :
∃ c : S, x * (sec S f.1 $ f.1 x).2 * c = (sec S f.1 $ f.1 x).1 * c :=
(f.4 _ _).1 $ by rw f.1.map_mul; exact sec_spec f.3 _
@[to_additive] lemma mk'_eq_of_eq {a₁ b₁ : M} {a₂ b₂ : S} (H : b₁ * a₂ = a₁ * b₂) :
f.mk' a₁ a₂ = f.mk' b₁ b₂ :=
f.mk'_eq_iff_eq.2 $ H ▸ rfl
@[simp, to_additive] lemma mk'_self (y : S) :
f.mk' (y : M) y = 1 :=
show _ * _ = _, by rw [mul_inv_left, mul_one]
@[simp, to_additive] lemma mk'_self' (x) (H : x ∈ S) :
f.mk' x ⟨x, H⟩ = 1 :=
by convert mk'_self _ _; refl
@[simp, to_additive] lemma mul_mk'_eq_mk'_of_mul (x₁ x₂) (y : S) :
f.1 x₁ * f.mk' x₂ y = f.mk' (x₁ * x₂) y :=
by rw [←mk'_one, ←mk'_mul, one_mul]
@[simp, to_additive] lemma mk'_mul_eq_mk'_of_mul (x₁ x₂) (y : S) :
f.mk' x₂ y * f.1 x₁ = f.mk' (x₁ * x₂) y :=
by rw [mul_comm, mul_mk'_eq_mk'_of_mul]
@[simp, to_additive] lemma mul_mk'_one_eq_mk' (x) (y : S) :
f.1 x * f.mk' 1 y = f.mk' x y :=
by rw [mul_mk'_eq_mk'_of_mul, mul_one]
@[simp, to_additive] lemma mk'_mul_cancel_right (x : M) (y : S) :
f.mk' (x * y) y = f.1 x :=
by rw [←mul_mk'_one_eq_mk', f.1.map_mul, mul_assoc, mul_mk'_one_eq_mk', mk'_self, mul_one]
@[simp, to_additive] lemma mk'_mul_cancel_left (x) (y : S) :
f.mk' ((y : M) * x) y = f.1 x :=
by rw [mul_comm, mk'_mul_cancel_right]
end localization_map
end submonoid |
8ab95404d584b9081810b65c92b4fd8f205316b1 | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /library/data/num/default.lean | 4fdc6da6c5d13d9d3b7166f67cc311fd676dafbf | [
"Apache-2.0"
] | permissive | codyroux/lean | 7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3 | 0cca265db19f7296531e339192e9b9bae4a31f8b | refs/heads/master | 1,610,909,964,159 | 1,407,084,399,000 | 1,416,857,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 416 | lean | ----------------------------------------------------------------------------------------------------
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Author: Leonardo de Moura
----------------------------------------------------------------------------------------------------
import data.num.decl data.num.ops data.num.thms
|
a659b3dafd902f9c75a56bd6e0da923795f23e5e | 38ee9024fb5974f555fb578fcf5a5a7b71e669b5 | /Mathlib/Lean/LocalContext.lean | f2352397965a8b877f2f3b0478c4d399ee740644 | [
"Apache-2.0"
] | permissive | denayd/mathlib4 | 750e0dcd106554640a1ac701e51517501a574715 | 7f40a5c514066801ab3c6d431e9f405baa9b9c58 | refs/heads/master | 1,693,743,991,894 | 1,636,618,048,000 | 1,636,618,048,000 | 373,926,241 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 864 | lean | /-
Copyright (c) 2021 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Scott Morrison
-/
import Lean.Meta.Tactic.Apply
namespace Lean.LocalContext
universe u v
variable {m : Type u → Type v} [Monad m] [Alternative m]
variable {β : Type u}
/-- Return the result of `f` on the first local declaration on which `f` succeeds. -/
@[specialize] def firstDeclM (lctx : LocalContext) (f : LocalDecl → m β) : m β :=
do match (← lctx.findDeclM? (optional ∘ f)) with
| none => failure
| some b => b
/-- Return the result of `f` on the last local declaration on which `f` succeeds. -/
@[specialize] def lastDeclM (lctx : LocalContext) (f : LocalDecl → m β) : m β :=
do match (← lctx.findDeclRevM? (optional ∘ f)) with
| none => failure
| some b => b
end Lean.LocalContext
|
df3ac4f6eaadeffa0ca8be18d74b858856ae2b6f | 8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3 | /src/ring_theory/polynomial/dickson.lean | c68f397c179a84e0d2d50270cb1756963aac707e | [
"Apache-2.0"
] | permissive | troyjlee/mathlib | e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5 | 45e7eb8447555247246e3fe91c87066506c14875 | refs/heads/master | 1,689,248,035,046 | 1,629,470,528,000 | 1,629,470,528,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,534 | lean | /-
Copyright (c) 2021 Julian Kuelshammer. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Julian Kuelshammer
-/
import ring_theory.polynomial.chebyshev
import ring_theory.localization
import data.zmod.basic
import algebra.char_p.invertible
/-!
# Dickson polynomials
The (generalised) Dickson polynomials are a family of polynomials indexed by `ℕ × ℕ`,
with coefficients in a commutative ring `R` depending on an element `a∈R`. More precisely, the
they satisfy the recursion `dickson k a (n + 2) = X * (dickson k a n + 1) - a * (dickson k a n)`
with starting values `dickson k a 0 = 3 - k` and `dickson k a 1 = X`. In the literature,
`dickson k a n` is called the `n`-th Dickson polynomial of the `k`-th kind associated to the
parameter `a : R`. They are closely related to the Chebyshev polynomials in the case that `a=1`.
When `a=0` they are just the family of monomials `X ^ n`.
## Main definition
* `polynomial.dickson`: the generalised Dickson polynomials.
## Main statements
* `polynomial.dickson_one_one_mul`, the `(m * n)`-th Dickson polynomial of the first kind for
parameter `1 : R` is the composition of the `m`-th and `n`-th Dickson polynomials of the first
kind for `1 : R`.
* `polynomial.dickson_one_one_char_p`, for a prime number `p`, the `p`-th Dickson polynomial of the
first kind associated to parameter `1 : R` is congruent to `X ^ p` modulo `p`.
## References
* [R. Lidl, G. L. Mullen and G. Turnwald, _Dickson polynomials_][MR1237403]
## TODO
* Redefine `dickson` in terms of `linear_recurrence`.
* Show that `dickson 2 1` is equal to the characteristic polynomial of the adjacency matrix of a
type A Dynkin diagram.
* Prove that the adjacency matrices of simply laced Dynkin diagrams are precisely the adjacency
matrices of simple connected graphs which annihilate `dickson 2 1`.
-/
noncomputable theory
namespace polynomial
variables {R S : Type*} [comm_ring R] [comm_ring S] (k : ℕ) (a : R)
/-- `dickson` is the `n`the (generalised) Dickson polynomial of the `k`-th kind associated to the
element `a ∈ R`. -/
noncomputable def dickson : ℕ → polynomial R
| 0 := 3 - k
| 1 := X
| (n + 2) := X * dickson (n + 1) - (C a) * dickson n
@[simp] lemma dickson_zero : dickson k a 0 = 3 - k := rfl
@[simp] lemma dickson_one : dickson k a 1 = X := rfl
lemma dickson_two : dickson k a 2 = X ^ 2 - C a * (3 - k) :=
by simp only [dickson, sq]
@[simp] lemma dickson_add_two (n : ℕ) :
dickson k a (n + 2) = X * dickson k a (n + 1) - C a * dickson k a n :=
by rw dickson
lemma dickson_of_two_le {n : ℕ} (h : 2 ≤ n) :
dickson k a n = X * dickson k a (n - 1) - C a * dickson k a (n - 2) :=
begin
obtain ⟨n, rfl⟩ := nat.exists_eq_add_of_le h,
rw add_comm,
exact dickson_add_two k a n
end
variables {R S k a}
lemma map_dickson (f : R →+* S) :
∀ (n : ℕ), map f (dickson k a n) = dickson k (f a) n
| 0 := by simp only [dickson_zero, map_sub, map_nat_cast, bit1, bit0, map_add, map_one]
| 1 := by simp only [dickson_one, map_X]
| (n + 2) :=
begin
simp only [dickson_add_two, map_sub, map_mul, map_X, map_C],
rw [map_dickson, map_dickson]
end
variable {R}
@[simp] lemma dickson_two_zero :
∀ (n : ℕ), dickson 2 (0 : R) n = X ^ n
| 0 := by { simp only [dickson_zero, pow_zero], norm_num }
| 1 := by simp only [dickson_one, pow_one]
| (n + 2) :=
begin
simp only [dickson_add_two, C_0, zero_mul, sub_zero],
rw [dickson_two_zero, pow_add X (n + 1) 1, mul_comm, pow_one]
end
section dickson
/-!
### A Lambda structure on `polynomial ℤ`
Mathlib doesn't currently know what a Lambda ring is.
But once it does, we can endow `polynomial ℤ` with a Lambda structure
in terms of the `dickson 1 1` polynomials defined below.
There is exactly one other Lambda structure on `polynomial ℤ` in terms of binomial polynomials.
-/
variables {R}
lemma dickson_one_one_eval_add_inv (x y : R) (h : x * y = 1) :
∀ n, (dickson 1 (1 : R) n).eval (x + y) = x ^ n + y ^ n
| 0 := by { simp only [bit0, eval_one, eval_add, pow_zero, dickson_zero], norm_num }
| 1 := by simp only [eval_X, dickson_one, pow_one]
| (n + 2) :=
begin
simp only [eval_sub, eval_mul, dickson_one_one_eval_add_inv, eval_X, dickson_add_two, C_1,
eval_one],
conv_lhs { simp only [pow_succ, add_mul, mul_add, h, ← mul_assoc, mul_comm y x, one_mul] },
ring_exp
end
variables (R)
lemma dickson_one_one_eq_chebyshev_T [invertible (2 : R)] :
∀ n, dickson 1 (1 : R) n = 2 * (chebyshev.T R n).comp (C (⅟2) * X)
| 0 := by { simp only [chebyshev.T_zero, mul_one, one_comp, dickson_zero], norm_num }
| 1 := by rw [dickson_one, chebyshev.T_one, X_comp, ← mul_assoc, ← C_1, ← C_bit0, ← C_mul,
mul_inv_of_self, C_1, one_mul]
| (n + 2) :=
begin
simp only [dickson_add_two, chebyshev.T_add_two, dickson_one_one_eq_chebyshev_T (n + 1),
dickson_one_one_eq_chebyshev_T n, sub_comp, mul_comp, add_comp, X_comp, bit0_comp, one_comp],
simp only [← C_1, ← C_bit0, ← mul_assoc, ← C_mul, mul_inv_of_self],
rw [C_1, one_mul],
ring
end
lemma chebyshev_T_eq_dickson_one_one [invertible (2 : R)] (n : ℕ) :
chebyshev.T R n = C (⅟2) * (dickson 1 1 n).comp (2 * X) :=
begin
rw dickson_one_one_eq_chebyshev_T,
simp only [comp_assoc, mul_comp, C_comp, X_comp, ← mul_assoc, ← C_1, ← C_bit0, ← C_mul],
rw [inv_of_mul_self, C_1, one_mul, one_mul, comp_X]
end
/-- The `(m * n)`-th Dickson polynomial of the first kind is the composition of the `m`-th and
`n`-th. -/
lemma dickson_one_one_mul (m n : ℕ) :
dickson 1 (1 : R) (m * n) = (dickson 1 1 m).comp (dickson 1 1 n) :=
begin
have h : (1 : R) = int.cast_ring_hom R (1),
simp only [ring_hom.eq_int_cast, int.cast_one],
rw h,
simp only [← map_dickson (int.cast_ring_hom R), ← map_comp],
congr' 1,
apply map_injective (int.cast_ring_hom ℚ) int.cast_injective,
simp only [map_dickson, map_comp, ring_hom.eq_int_cast, int.cast_one,
dickson_one_one_eq_chebyshev_T, chebyshev.T_mul, two_mul, ← add_comp],
simp only [← two_mul, ← comp_assoc],
apply eval₂_congr rfl rfl,
rw [comp_assoc],
apply eval₂_congr rfl _ rfl,
rw [mul_comp, C_comp, X_comp, ← mul_assoc, ← C_1, ← C_bit0, ← C_mul,
inv_of_mul_self, C_1, one_mul]
end
lemma dickson_one_one_comp_comm (m n : ℕ) :
(dickson 1 (1 : R) m).comp (dickson 1 1 n) = (dickson 1 1 n).comp (dickson 1 1 m) :=
by rw [← dickson_one_one_mul, mul_comm, dickson_one_one_mul]
lemma dickson_one_one_zmod_p (p : ℕ) [fact p.prime] :
dickson 1 (1 : zmod p) p = X ^ p :=
begin
-- Recall that `dickson_eval_add_inv` characterises `dickson 1 1 p`
-- as a polynomial that maps `x + x⁻¹` to `x ^ p + (x⁻¹) ^ p`.
-- Since `X ^ p` also satisfies this property in characteristic `p`,
-- we can use a variant on `polynomial.funext` to conclude that these polynomials are equal.
-- For this argument, we need an arbitrary infinite field of characteristic `p`.
obtain ⟨K, _, _, H⟩ : ∃ (K : Type) [field K], by exactI ∃ [char_p K p], infinite K,
{ let K := fraction_ring (polynomial (zmod p)),
let f : zmod p →+* K := (algebra_map _ (fraction_ring _)).comp C,
haveI : char_p K p, { rw ← f.char_p_iff_char_p, apply_instance },
haveI : infinite K :=
infinite.of_injective (algebra_map (polynomial (zmod p)) (fraction_ring (polynomial (zmod p))))
(is_fraction_ring.injective _ _),
refine ⟨K, _, _, _⟩; apply_instance },
resetI,
apply map_injective (zmod.cast_hom (dvd_refl p) K) (ring_hom.injective _),
rw [map_dickson, map_pow, map_X],
apply eq_of_infinite_eval_eq,
-- The two polynomials agree on all `x` of the form `x = y + y⁻¹`.
apply @set.infinite_mono _ {x : K | ∃ y, x = y + y⁻¹ ∧ y ≠ 0},
{ rintro _ ⟨x, rfl, hx⟩,
simp only [eval_X, eval_pow, set.mem_set_of_eq, @add_pow_char K _ p,
dickson_one_one_eval_add_inv _ _ (mul_inv_cancel hx), inv_pow', zmod.cast_hom_apply,
zmod.cast_one'] },
-- Now we need to show that the set of such `x` is infinite.
-- If the set is finite, then we will show that `K` is also finite.
{ intro h,
rw ← set.infinite_univ_iff at H,
apply H,
-- To each `x` of the form `x = y + y⁻¹`
-- we `bind` the set of `y` that solve the equation `x = y + y⁻¹`.
-- For every `x`, that set is finite (since it is governed by a quadratic equation).
-- For the moment, we claim that all these sets together cover `K`.
suffices : (set.univ : set K) =
{x : K | ∃ (y : K), x = y + y⁻¹ ∧ y ≠ 0} >>= (λ x, {y | x = y + y⁻¹ ∨ y = 0}),
{ rw this, clear this,
refine h.bUnion (λ x hx, _),
-- The following quadratic polynomial has as solutions the `y` for which `x = y + y⁻¹`.
let φ : polynomial K := X ^ 2 - C x * X + 1,
have hφ : φ ≠ 0,
{ intro H,
have : φ.eval 0 = 0, by rw [H, eval_zero],
simpa [eval_X, eval_one, eval_pow, eval_sub, sub_zero, eval_add,
eval_mul, mul_zero, sq, zero_add, one_ne_zero] },
classical,
convert (φ.roots ∪ {0}).to_finset.finite_to_set using 1,
ext1 y,
simp only [multiset.mem_to_finset, set.mem_set_of_eq, finset.mem_coe, multiset.mem_union,
mem_roots hφ, is_root, eval_add, eval_sub, eval_pow, eval_mul, eval_X, eval_C, eval_one,
multiset.mem_singleton, multiset.singleton_eq_singleton],
by_cases hy : y = 0,
{ simp only [hy, eq_self_iff_true, or_true] },
apply or_congr _ iff.rfl,
rw [← mul_left_inj' hy, eq_comm, ← sub_eq_zero, add_mul, inv_mul_cancel hy],
apply eq_iff_eq_cancel_right.mpr,
ring },
-- Finally, we prove the claim that our finite union of finite sets covers all of `K`.
{ apply (set.eq_univ_of_forall _).symm,
intro x,
simp only [exists_prop, set.mem_Union, set.bind_def, ne.def, set.mem_set_of_eq],
by_cases hx : x = 0,
{ simp only [hx, and_true, eq_self_iff_true, inv_zero, or_true],
exact ⟨_, 1, rfl, one_ne_zero⟩ },
{ simp only [hx, or_false, exists_eq_right],
exact ⟨_, rfl, hx⟩ } } }
end
lemma dickson_one_one_char_p (p : ℕ) [fact p.prime] [char_p R p] :
dickson 1 (1 : R) p = X ^ p :=
begin
have h : (1 : R) = zmod.cast_hom (dvd_refl p) R (1),
simp only [zmod.cast_hom_apply, zmod.cast_one'],
rw [h, ← map_dickson (zmod.cast_hom (dvd_refl p) R), dickson_one_one_zmod_p, map_pow, map_X]
end
end dickson
end polynomial
|
fc7066f5c2e8536cffc539a65d266779e45d9ccc | d436468d80b739ba7e06843c4d0d2070e43448e5 | /src/ring_theory/algebra.lean | a28caf80a60f149333c64be51a7f044d8c72f461 | [
"Apache-2.0"
] | permissive | roro47/mathlib | 761fdc002aef92f77818f3fef06bf6ec6fc1a28e | 80aa7d52537571a2ca62a3fdf71c9533a09422cf | refs/heads/master | 1,599,656,410,625 | 1,573,649,488,000 | 1,573,649,488,000 | 221,452,951 | 0 | 0 | Apache-2.0 | 1,573,647,693,000 | 1,573,647,692,000 | null | UTF-8 | Lean | false | false | 20,487 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
Algebra over Commutative Ring (under category)
-/
import data.polynomial data.mv_polynomial
import data.complex.basic
import data.matrix.basic
import linear_algebra.tensor_product
import ring_theory.subring
noncomputable theory
universes u v w u₁ v₁
open lattice
open_locale tensor_product
/-- The category of R-algebras where R is a commutative
ring is the under category R ↓ CRing. In the categorical
setting we have a forgetful functor R-Alg ⥤ R-Mod.
However here it extends module in order to preserve
definitional equality in certain cases. -/
class algebra (R : Type u) (A : Type v) [comm_ring R] [ring A] extends has_scalar R A :=
(to_fun : R → A) [hom : is_ring_hom to_fun]
(commutes' : ∀ r x, x * to_fun r = to_fun r * x)
(smul_def' : ∀ r x, r • x = to_fun r * x)
attribute [instance] algebra.hom
def algebra_map {R : Type u} (A : Type v) [comm_ring R] [ring A] [algebra R A] (x : R) : A :=
algebra.to_fun A x
namespace algebra
variables {R : Type u} {S : Type v} {A : Type w}
variables [comm_ring R] [comm_ring S] [ring A] [algebra R A]
/-- The codomain of an algebra. -/
instance : has_scalar R A := infer_instance
include R
instance : is_ring_hom (algebra_map A : R → A) := algebra.hom _ A
variables (A)
@[simp] lemma map_add (r s : R) : algebra_map A (r + s) = algebra_map A r + algebra_map A s :=
is_ring_hom.map_add _
@[simp] lemma map_neg (r : R) : algebra_map A (-r) = -algebra_map A r :=
is_ring_hom.map_neg _
@[simp] lemma map_sub (r s : R) : algebra_map A (r - s) = algebra_map A r - algebra_map A s :=
is_ring_hom.map_sub _
@[simp] lemma map_mul (r s : R) : algebra_map A (r * s) = algebra_map A r * algebra_map A s :=
is_ring_hom.map_mul _
variables (R)
@[simp] lemma map_zero : algebra_map A (0 : R) = 0 :=
is_ring_hom.map_zero _
@[simp] lemma map_one : algebra_map A (1 : R) = 1 :=
is_ring_hom.map_one _
variables {R A}
/-- Creating an algebra from a morphism in CRing. -/
def of_ring_hom (i : R → S) (hom : is_ring_hom i) : algebra R S :=
{ smul := λ c x, i c * x,
to_fun := i,
commutes' := λ _ _, mul_comm _ _,
smul_def' := λ c x, rfl }
theorem smul_def (r : R) (x : A) : r • x = algebra_map A r * x :=
algebra.smul_def' r x
theorem commutes (r : R) (x : A) : x * algebra_map A r = algebra_map A r * x :=
algebra.commutes' r x
theorem left_comm (r : R) (x y : A) : x * (algebra_map A r * y) = algebra_map A r * (x * y) :=
by rw [← mul_assoc, commutes, mul_assoc]
@[simp] lemma mul_smul_comm (s : R) (x y : A) :
x * (s • y) = s • (x * y) :=
by rw [smul_def, smul_def, left_comm]
@[simp] lemma smul_mul_assoc (r : R) (x y : A) :
(r • x) * y = r • (x * y) :=
by rw [smul_def, smul_def, mul_assoc]
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] }
omit R
instance {F : Type u} {K : Type v} [discrete_field F] [ring K] [algebra F K] :
vector_space F K :=
@vector_space.mk F _ _ _ algebra.to_module
/-- R[X] is the generator of the category R-Alg. -/
instance polynomial (R : Type u) [comm_ring R] : algebra R (polynomial R) :=
{ to_fun := polynomial.C,
commutes' := λ _ _, mul_comm _ _,
smul_def' := λ c p, (polynomial.C_mul' c p).symm,
.. polynomial.module }
/-- The algebra of multivariate polynomials. -/
instance mv_polynomial (R : Type u) [comm_ring R]
(ι : Type v) : algebra R (mv_polynomial ι R) :=
{ to_fun := mv_polynomial.C,
commutes' := λ _ _, mul_comm _ _,
smul_def' := λ c p, (mv_polynomial.C_mul' c p).symm,
.. mv_polynomial.module }
/-- Creating an algebra from a subring. This is the dual of ring extension. -/
instance of_subring (S : set R) [is_subring S] : algebra S R :=
of_ring_hom subtype.val ⟨rfl, λ _ _, rfl, λ _ _, rfl⟩
variables (R A)
/-- The multiplication in an algebra is a bilinear map. -/
def lmul : A →ₗ A →ₗ A :=
linear_map.mk₂ R (*)
(λ x y z, add_mul x y z)
(λ c x y, by rw [smul_def, smul_def, mul_assoc _ x y])
(λ x y z, mul_add x y z)
(λ c x y, by rw [smul_def, smul_def, left_comm])
set_option class.instance_max_depth 39
def lmul_left (r : A) : A →ₗ A :=
lmul R A r
def lmul_right (r : A) : A →ₗ A :=
(lmul R A).flip r
variables {R A}
@[simp] lemma lmul_apply (p q : A) : lmul R A p q = p * q := rfl
@[simp] lemma lmul_left_apply (p q : A) : lmul_left R A p q = p * q := rfl
@[simp] lemma lmul_right_apply (p q : A) : lmul_right R A p q = q * p := rfl
end algebra
instance module.endomorphism_algebra (R : Type u) (M : Type v)
[comm_ring R] [add_comm_group M] [module R M] : algebra R (M →ₗ[R] M) :=
{ to_fun := (λ r, r • linear_map.id),
hom := by apply is_ring_hom.mk; intros; ext; simp [mul_smul, add_smul],
commutes' := by intros; ext; simp,
smul_def' := by intros; ext; simp }
set_option class.instance_max_depth 50
instance matrix_algebra (n : Type u) (R : Type v)
[fintype n] [decidable_eq n] [comm_ring R] : algebra R (matrix n n R) :=
{ to_fun := (λ r, r • 1),
hom := { map_one := by simp,
map_mul := by { intros, simp [mul_smul], },
map_add := by { intros, simp [add_smul], } },
commutes' := by { intros, simp },
smul_def' := by { intros, simp } }
set_option old_structure_cmd true
/-- Defining the homomorphism in the category R-Alg. -/
structure alg_hom (R : Type u) (A : Type v) (B : Type w)
[comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B] extends ring_hom A B :=
(commutes' : ∀ r : R, to_fun (algebra_map A r) = algebra_map B r)
infixr ` →ₐ `:25 := alg_hom _
notation A ` →ₐ[`:25 R `] ` B := alg_hom R A B
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁}
variables {rR : comm_ring R} {rA : ring A} {rB : ring B} {rC : ring C} {rD : ring D}
variables {aA : algebra R A} {aB : algebra R B} {aC : algebra R C} {aD : algebra R D}
include R rR rA rB aA aB
instance : has_coe_to_fun (A →ₐ[R] B) := ⟨_, λ f, f.to_fun⟩
instance : has_coe (A →ₐ[R] B) (A →+* B) := ⟨alg_hom.to_ring_hom⟩
variables (φ : A →ₐ[R] B)
instance : is_ring_hom ⇑φ := ring_hom.is_ring_hom φ.to_ring_hom
@[ext]
theorem ext {φ₁ φ₂ : A →ₐ[R] B} (H : ∀ x, φ₁ x = φ₂ x) : φ₁ = φ₂ :=
by cases φ₁; cases φ₂; congr' 1; ext; apply H
theorem commutes (r : R) : φ (algebra_map A r) = algebra_map B r := φ.commutes' r
@[simp] lemma map_add (r s : A) : φ (r + s) = φ r + φ s :=
is_ring_hom.map_add _
@[simp] lemma map_zero : φ 0 = 0 :=
is_ring_hom.map_zero _
@[simp] lemma map_neg (x) : φ (-x) = -φ x :=
is_ring_hom.map_neg _
@[simp] lemma map_sub (x y) : φ (x - y) = φ x - φ y :=
is_ring_hom.map_sub _
@[simp] lemma map_mul (x y) : φ (x * y) = φ x * φ y :=
is_ring_hom.map_mul _
@[simp] lemma map_one : φ 1 = 1 :=
is_ring_hom.map_one _
/-- R-Alg ⥤ R-Mod -/
def to_linear_map : A →ₗ B :=
{ to_fun := φ,
add := φ.map_add,
smul := λ (c : R) x, by rw [algebra.smul_def, φ.map_mul, φ.commutes c, algebra.smul_def] }
@[simp] lemma to_linear_map_apply (p : A) : φ.to_linear_map p = φ p := rfl
theorem to_linear_map_inj {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁.to_linear_map = φ₂.to_linear_map) : φ₁ = φ₂ :=
ext $ λ x, show φ₁.to_linear_map x = φ₂.to_linear_map x, by rw H
variables (R A)
omit rB aB
variables [rR] [rA] [aA]
protected def id : A →ₐ[R] A :=
{ commutes' := λ _, rfl,
..ring_hom.id A }
variables {R A rR rA aA}
@[simp] lemma id_to_linear_map :
(alg_hom.id R A).to_linear_map = @linear_map.id R A _ _ _ := rfl
@[simp] lemma id_apply (p : A) : alg_hom.id R A p = p := rfl
include rB rC aB aC
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 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 comp_apply (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) (p : A) :
φ₁.comp φ₂ p = φ₁ (φ₂ p) := rfl
omit rC aC
@[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
include rC aC rD aD
theorem comp_assoc (φ₁ : C →ₐ[R] D) (φ₂ : B →ₐ[R] C) (φ₃ : A →ₐ[R] B) :
(φ₁.comp φ₂).comp φ₃ = φ₁.comp (φ₂.comp φ₃) :=
ext $ λ x, rfl
end alg_hom
namespace algebra
variables (R : Type u) (S : Type v) (A : Type w)
include R S A
/-- `comap R S A` is a type alias for `A`, and has an R-algebra structure defined on it
when `algebra R S` and `algebra S A`. -/
/- This is done to avoid a type class search with meta-variables `algebra R ?m_1` and
`algebra ?m_1 A -/
/- The `nolint` attribute is added because it has unused arguments `R` and `S`, but these are necessary for synthesizing the
appropriate type classes -/
@[nolint] def comap : Type w := A
def comap.to_comap : A → comap R S A := id
def comap.of_comap : comap R S A → A := id
omit R S A
variables [comm_ring R] [comm_ring S] [ring A] [algebra R S] [algebra S A]
instance comap.ring : ring (comap R S A) := _inst_3
instance comap.comm_ring (R : Type u) (S : Type v) (A : Type w)
[comm_ring R] [comm_ring S] [comm_ring A] [algebra R S] [algebra S A] :
comm_ring (comap R S A) := _inst_8
instance comap.module : module S (comap R S A) := show module S A, by apply_instance
instance comap.has_scalar : has_scalar S (comap R S A) := show has_scalar S A, by apply_instance
set_option class.instance_max_depth 40
/-- R ⟶ S induces S-Alg ⥤ R-Alg -/
instance comap.algebra : algebra R (comap R S A) :=
{ smul := λ r x, (algebra_map S r • x : A),
to_fun := (algebra_map A : S → A) ∘ algebra_map S,
hom := by letI : is_ring_hom (algebra_map A) := _inst_5.hom; apply_instance,
commutes' := λ r x, algebra.commutes _ _,
smul_def' := λ _ _, algebra.smul_def _ _ }
def to_comap : S →ₐ[R] comap R S A :=
{ commutes' := λ r, rfl,
..ring_hom.of (algebra_map A : S → A) }
theorem to_comap_apply (x) : to_comap R S A x = (algebra_map A : S → A) x := rfl
end algebra
namespace alg_hom
variables {R : Type u} {S : Type v} {A : Type w} {B : Type u₁}
variables [comm_ring R] [comm_ring S] [ring A] [ring B]
variables [algebra R S] [algebra S A] [algebra S B] (φ : A →ₐ[S] B)
include R
/-- R ⟶ S induces S-Alg ⥤ R-Alg -/
def comap : algebra.comap R S A →ₐ[R] algebra.comap R S B :=
{ commutes' := λ r, φ.commutes (algebra_map S r)
..φ }
end alg_hom
namespace polynomial
variables (R : Type u) (A : Type v)
variables [comm_ring R] [comm_ring A] [algebra R A]
variables (x : A)
/-- A → Hom[R-Alg](R[X],A) -/
def aeval : polynomial R →ₐ[R] A :=
{ commutes' := λ r, eval₂_C _ _,
..ring_hom.of (eval₂ (algebra_map A) x) }
theorem aeval_def (p : polynomial R) : aeval R A x p = eval₂ (algebra_map A) x p := rfl
instance aeval.is_ring_hom : is_ring_hom (aeval R A x) :=
by apply_instance
theorem eval_unique (φ : polynomial R →ₐ[R] A) (p) :
φ p = eval₂ (algebra_map A) (φ X) p :=
begin
apply polynomial.induction_on p,
{ intro r, rw eval₂_C, exact φ.commutes r },
{ intros f g ih1 ih2,
rw [is_ring_hom.map_add φ, ih1, ih2, eval₂_add] },
{ intros n r ih,
rw [pow_succ', ← mul_assoc, is_ring_hom.map_mul φ, eval₂_mul (algebra_map A : R → A), eval₂_X, ih] }
end
end polynomial
namespace mv_polynomial
variables (R : Type u) (A : Type v)
variables [comm_ring R] [comm_ring A] [algebra R A]
variables (σ : set A)
/-- (ι → A) → Hom[R-Alg](R[ι],A) -/
def aeval : mv_polynomial σ R →ₐ[R] A :=
{ commutes' := λ r, eval₂_C _ _ _
..ring_hom.of (eval₂ (algebra_map A) subtype.val) }
theorem aeval_def (p : mv_polynomial σ R) : aeval R A σ p = eval₂ (algebra_map A) subtype.val p := rfl
instance aeval.is_ring_hom : is_ring_hom (aeval R A σ) :=
by apply_instance
variables (ι : Type w)
theorem eval_unique (φ : mv_polynomial ι R →ₐ[R] A) (p) :
φ p = eval₂ (algebra_map A) (φ ∘ X) p :=
begin
apply mv_polynomial.induction_on p,
{ intro r, rw eval₂_C, exact φ.commutes r },
{ intros f g ih1 ih2,
rw [is_ring_hom.map_add φ, ih1, ih2, eval₂_add] },
{ intros p j ih,
rw [is_ring_hom.map_mul φ, eval₂_mul, eval₂_X, ih] }
end
end mv_polynomial
namespace rat
instance algebra_rat {α} [field α] [char_zero α] : algebra ℚ α :=
algebra.of_ring_hom rat.cast (by apply_instance)
end rat
namespace complex
instance algebra_over_reals : algebra ℝ ℂ :=
algebra.of_ring_hom coe $ by constructor; intros; simp [one_re]
instance : has_scalar ℝ ℂ := { smul := λ r c, ↑r * c}
end complex
structure subalgebra (R : Type u) (A : Type v)
[comm_ring R] [ring A] [algebra R A] : Type v :=
(carrier : set A) [subring : is_subring carrier]
(range_le : set.range (algebra_map A : R → A) ≤ carrier)
attribute [instance] subalgebra.subring
namespace subalgebra
variables {R : Type u} {A : Type v}
variables [comm_ring R] [ring A] [algebra R A]
include R
instance : has_coe (subalgebra R A) (set A) :=
⟨λ S, S.carrier⟩
instance : has_mem A (subalgebra R A) :=
⟨λ x S, x ∈ S.carrier⟩
variables {A}
theorem mem_coe {x : A} {s : subalgebra R A} : x ∈ (s : set A) ↔ x ∈ s :=
iff.rfl
@[ext] theorem ext {S T : subalgebra R A}
(h : ∀ x : A, x ∈ S ↔ x ∈ T) : S = T :=
by cases S; cases T; congr; ext x; exact h x
variables (S : subalgebra R A)
instance : is_subring (S : set A) := S.subring
instance : ring S := @@subtype.ring _ S.is_subring
instance (R : Type u) (A : Type v) {rR : comm_ring R} [comm_ring A]
{aA : algebra R A} (S : subalgebra R A) : comm_ring S := @@subtype.comm_ring _ S.is_subring
instance algebra : algebra R S :=
{ smul := λ (c:R) x, ⟨c • x.1,
by rw algebra.smul_def; exact @@is_submonoid.mul_mem _ S.2.2 (S.3 ⟨c, rfl⟩) x.2⟩,
to_fun := λ r, ⟨algebra_map A r, S.range_le ⟨r, rfl⟩⟩,
hom := ⟨subtype.eq $ algebra.map_one R A, λ x y, subtype.eq $ algebra.map_mul A x y,
λ x y, subtype.eq $ algebra.map_add A x y⟩,
commutes' := λ c x, subtype.eq $ by apply _inst_3.4,
smul_def' := λ c x, subtype.eq $ by apply _inst_3.5 }
instance to_algebra (R : Type u) (A : Type v) [comm_ring R] [comm_ring A]
[algebra R A] (S : subalgebra R A) : algebra S A :=
algebra.of_subring _
def val : S →ₐ[R] A :=
by refine_struct { to_fun := subtype.val }; intros; refl
def to_submodule : submodule R A :=
{ carrier := S.carrier,
zero := (0:S).2,
add := λ x y hx hy, (⟨x, hx⟩ + ⟨y, hy⟩ : S).2,
smul := λ c x hx, (algebra.smul_def c x).symm ▸ (⟨algebra_map A c, S.range_le ⟨c, rfl⟩⟩ * ⟨x, hx⟩:S).2 }
instance coe_to_submodule : has_coe (subalgebra R A) (submodule R A) :=
⟨to_submodule⟩
instance to_submodule.is_subring : is_subring ((S : submodule R A) : set A) := S.2
instance : partial_order (subalgebra R A) :=
{ le := λ S T, S.carrier ≤ T.carrier,
le_refl := λ _, le_refl _,
le_trans := λ _ _ _, le_trans,
le_antisymm := λ S T hst hts, ext $ λ x, ⟨@hst x, @hts x⟩ }
def comap {R : Type u} {S : Type v} {A : Type w}
[comm_ring R] [comm_ring S] [ring A] [algebra R S] [algebra S A]
(iSB : subalgebra S A) : subalgebra R (algebra.comap R S A) :=
{ carrier := (iSB : set A),
subring := iSB.is_subring,
range_le := λ a ⟨r, hr⟩, hr ▸ iSB.range_le ⟨_, rfl⟩ }
set_option class.instance_max_depth 48
def under {R : Type u} {A : Type v} [comm_ring R] [comm_ring A]
{i : algebra R A} (S : subalgebra R A)
(T : subalgebra S A) : subalgebra R A :=
{ carrier := T,
range_le := (λ a ⟨r, hr⟩, hr ▸ T.range_le ⟨⟨algebra_map A r, S.range_le ⟨r, rfl⟩⟩, rfl⟩) }
end subalgebra
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w}
variables [comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B]
variables (φ : A →ₐ[R] B)
protected def range : subalgebra R B :=
{ carrier := set.range φ,
subring :=
{ one_mem := ⟨1, φ.map_one⟩,
mul_mem := λ y₁ y₂ ⟨x₁, hx₁⟩ ⟨x₂, hx₂⟩, ⟨x₁ * x₂, hx₁ ▸ hx₂ ▸ φ.map_mul x₁ x₂⟩ },
range_le := λ y ⟨r, hr⟩, ⟨algebra_map A r, hr ▸ φ.commutes r⟩ }
end alg_hom
namespace algebra
variables {R : Type u} (A : Type v)
variables [comm_ring R] [ring A] [algebra R A]
include R
variables (R)
instance id : algebra R R :=
algebra.of_ring_hom id $ by apply_instance
namespace id
@[simp] lemma map_eq_self (x : R) : algebra_map R x = x := rfl
@[simp] lemma smul_eq_mul (x y : R) : x • y = x * y := rfl
end id
def of_id : R →ₐ A :=
{ commutes' := λ _, rfl, .. ring_hom.of (algebra_map A) }
variables {R}
theorem of_id_apply (r) : of_id R A r = algebra_map A r := rfl
variables (R) {A}
def adjoin (s : set A) : subalgebra R A :=
{ carrier := ring.closure (set.range (algebra_map A : R → A) ∪ s),
range_le := le_trans (set.subset_union_left _ _) ring.subset_closure }
variables {R}
protected lemma gc : galois_connection (adjoin R : set A → subalgebra R A) coe :=
λ s S, ⟨λ H, le_trans (le_trans (set.subset_union_right _ _) ring.subset_closure) H,
λ H, ring.closure_subset $ set.union_subset S.range_le H⟩
protected def gi : galois_insertion (adjoin R : set A → subalgebra R A) coe :=
{ choice := λ s hs, adjoin R s,
gc := algebra.gc,
le_l_u := λ S, (algebra.gc (S : set A) (adjoin R S)).1 $ le_refl _,
choice_eq := λ _ _, rfl }
instance : complete_lattice (subalgebra R A) :=
galois_insertion.lift_complete_lattice algebra.gi
theorem mem_bot {x : A} : x ∈ (⊥ : subalgebra R A) ↔ x ∈ set.range (algebra_map A : R → A) :=
suffices (⊥ : subalgebra R A) = (of_id R A).range, by rw this; refl,
le_antisymm bot_le $ subalgebra.range_le _
theorem mem_top {x : A} : x ∈ (⊤ : subalgebra R A) :=
ring.mem_closure $ or.inr trivial
def to_top : A →ₐ[R] (⊤ : subalgebra R A) :=
by refine_struct { to_fun := λ x, (⟨x, mem_top⟩ : (⊤ : subalgebra R A)) }; intros; refl
end algebra
section int
variables (R : Type*) [comm_ring R]
/-- CRing ⥤ ℤ-Alg -/
def alg_hom_int
{R : Type u} [comm_ring R] [algebra ℤ R]
{S : Type v} [comm_ring S] [algebra ℤ S]
(f : R → S) [is_ring_hom f] : R →ₐ[ℤ] S :=
{ commutes' := λ i, by change (ring_hom.of f).to_fun with f; exact
int.induction_on i (by rw [algebra.map_zero, algebra.map_zero, is_ring_hom.map_zero f])
(λ i ih, by rw [algebra.map_add, algebra.map_add, algebra.map_one, algebra.map_one];
rw [is_ring_hom.map_add f, is_ring_hom.map_one f, ih])
(λ i ih, by rw [algebra.map_sub, algebra.map_sub, algebra.map_one, algebra.map_one];
rw [is_ring_hom.map_sub f, is_ring_hom.map_one f, ih]),
..ring_hom.of f }
/-- CRing ⥤ ℤ-Alg -/
instance algebra_int : algebra ℤ R :=
algebra.of_ring_hom coe $ by constructor; intros; simp
variables {R}
/-- CRing ⥤ ℤ-Alg -/
def subalgebra_of_subring (S : set R) [is_subring S] : subalgebra ℤ R :=
{ carrier := S, range_le := λ x ⟨i, h⟩, h ▸ int.induction_on i
(by rw algebra.map_zero; exact is_add_submonoid.zero_mem _)
(λ i hi, by rw [algebra.map_add, algebra.map_one]; exact is_add_submonoid.add_mem hi (is_submonoid.one_mem _))
(λ i hi, by rw [algebra.map_sub, algebra.map_one]; exact is_add_subgroup.sub_mem _ _ _ hi (is_submonoid.one_mem _)) }
@[simp] lemma mem_subalgebra_of_subring {x : R} {S : set R} [is_subring S] :
x ∈ subalgebra_of_subring S ↔ x ∈ S :=
iff.rfl
section span_int
open submodule
lemma span_int_eq_add_group_closure (s : set R) :
↑(span ℤ s) = add_group.closure s :=
set.subset.antisymm (λ x hx, span_induction hx
(λ _, add_group.mem_closure)
(is_add_submonoid.zero_mem _)
(λ a b ha hb, is_add_submonoid.add_mem ha hb)
(λ n a ha, by { erw [show n • a = gsmul n a, from (gsmul_eq_mul a n).symm],
exact is_add_subgroup.gsmul_mem ha}))
(add_group.closure_subset subset_span)
@[simp] lemma span_int_eq (s : set R) [is_add_subgroup s] :
(↑(span ℤ s) : set R) = s :=
by rw [span_int_eq_add_group_closure, add_group.closure_add_subgroup]
end span_int
end int
|
37446757b2ee6486a4fb47610c46b30f73d94495 | 5d166a16ae129621cb54ca9dde86c275d7d2b483 | /library/init/meta/rec_util.lean | f7e00acfe1e1f693586c874df2edf82f1ca4fc65 | [
"Apache-2.0"
] | permissive | jcarlson23/lean | b00098763291397e0ac76b37a2dd96bc013bd247 | 8de88701247f54d325edd46c0eed57aeacb64baf | refs/heads/master | 1,611,571,813,719 | 1,497,020,963,000 | 1,497,021,515,000 | 93,882,536 | 1 | 0 | null | 1,497,029,896,000 | 1,497,029,896,000 | null | UTF-8 | Lean | false | false | 4,662 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
Helper tactic for showing that a type has decidable equality.
-/
prelude
import init.meta.tactic init.data.option.instances
namespace tactic
open expr
/- Return tt iff e's type is of the form `(I_name ...)` -/
meta def is_type_app_of (e : expr) (I_name : name) : tactic bool :=
do t ← infer_type e,
return $ is_constant_of (get_app_fn t) I_name
/- Auxiliary function for using brec_on "dictionary" -/
private meta def mk_rec_inst_aux : expr → nat → tactic expr
| F 0 := do
P ← mk_app `pprod.fst [F],
mk_app `pprod.fst [P]
| F (n+1) := do
F' ← mk_app `pprod.snd [F],
mk_rec_inst_aux F' n
/- Construct brec_on "recursive value". F_name is the name of the brec_on "dictionary".
Type of the F_name hypothesis should be of the form (below (C ...)) where C is a constructor.
The result is the "recursive value" for the (i+1)-th recursive value of the constructor C. -/
meta def mk_brec_on_rec_value (F_name : name) (i : nat) : tactic expr :=
do F ← get_local F_name,
mk_rec_inst_aux F i
meta def constructor_num_fields (c : name) : tactic nat :=
do env ← get_env,
decl ← env.get c,
ctype ← return decl.type,
arity ← get_pi_arity ctype,
I ← env.inductive_type_of c,
nparams ← return (env.inductive_num_params I),
return $ arity - nparams
private meta def mk_name_list_aux : name → nat → nat → list name → list name × nat
| p i 0 l := (list.reverse l, i)
| p i (j+1) l := mk_name_list_aux p (i+1) j (mk_num_name p i :: l)
private meta def mk_name_list (p : name) (i : nat) (n : nat) : list name × nat :=
mk_name_list_aux p i n []
/- Return a list of names of the form [p.i, ..., p.{i+n}] where n is
the number of fields of the constructor c -/
meta def mk_constructor_arg_names (c : name) (p : name) (i : nat) : tactic (list name × nat) :=
do nfields ← constructor_num_fields c,
return $ mk_name_list p i nfields
private meta def mk_constructors_arg_names_aux : list name → name → nat → list (list name) → tactic (list (list name))
| [] p i r := return (list.reverse r)
| (c::cs) p i r := do
v : list name × nat ← mk_constructor_arg_names c p i,
match v with (l, i') := mk_constructors_arg_names_aux cs p i' (l :: r) end
/- Given an inductive datatype I with k constructors and where constructor i has n_i fields,
return the list [[p.1, ..., p.n_1], [p.{n_1 + 1}, ..., p.{n_1 + n_2}], ..., [..., p.{n_1 + ... + n_k}]] -/
meta def mk_constructors_arg_names (I : name) (p : name) : tactic (list (list name)) :=
do env ← get_env,
cs ← return $ env.constructors_of I,
mk_constructors_arg_names_aux cs p 1 []
private meta def mk_fresh_arg_name_aux : name → nat → name_set → tactic (name × name_set)
| n i s :=
do r ← get_unused_name n (some i),
if s.contains r then
mk_fresh_arg_name_aux n (i+1) s
else
return (r, s.insert r)
private meta def mk_fresh_arg_name (n : name) (s : name_set) : tactic (name × name_set) :=
do r ← get_unused_name n,
if s.contains r then
mk_fresh_arg_name_aux n 1 s
else
return (r, s.insert r)
private meta def mk_constructor_fresh_names_aux : nat → expr → name_set → tactic (list name × name_set)
| nparams ty s := do
ty ← whnf ty,
match ty with
| expr.pi n bi d b :=
if nparams = 0 then do {
(n', s') ← mk_fresh_arg_name n s,
x ← mk_local' n' bi d,
let ty' := b.instantiate_var x,
(ns, s'') ← mk_constructor_fresh_names_aux 0 ty' s',
return (n'::ns, s'')
} else do {
x ← mk_local' n bi d,
let ty' := b.instantiate_var x,
mk_constructor_fresh_names_aux (nparams - 1) ty' s
}
| _ := return ([], s)
end
meta def mk_constructor_fresh_names (nparams : nat) (c : name) (s : name_set) : tactic (list name × name_set) :=
do d ← get_decl c,
let t := d.type,
mk_constructor_fresh_names_aux nparams t s
private meta def mk_constructors_fresh_names_aux : nat → list name → name_set → list (list name) → tactic (list (list name))
| np [] s r := return (list.reverse r)
| np (c::cs) s r := do
(ns, s') ← mk_constructor_fresh_names np c s,
mk_constructors_fresh_names_aux np cs s' (ns::r)
meta def mk_constructors_fresh_names (I : name) : tactic (list (list name)) :=
do env ← get_env,
let cs := env.constructors_of I,
let nparams := env.inductive_num_params I,
mk_constructors_fresh_names_aux nparams cs mk_name_set []
end tactic
|
06442887d9b44e034f525d2b422b7328b448cafc | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/playground/hashable.lean | 185faf32f2e675a6f2346b86c90b0e10f70eba76 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | leanprover/lean4 | 4bdf9790294964627eb9be79f5e8f6157780b4cc | f1f9dc0f2f531af3312398999d8b8303fa5f096b | refs/heads/master | 1,693,360,665,786 | 1,693,350,868,000 | 1,693,350,868,000 | 129,571,436 | 2,827 | 311 | Apache-2.0 | 1,694,716,156,000 | 1,523,760,560,000 | Lean | UTF-8 | Lean | false | false | 2,089 | lean | set_option trace.Elab.Deriving.hashable true
inductive SimpleInd
| A
| B
deriving Hashable
theorem «inductive fields have different base hashes» : ∀ x, hash x =
match x with
| SimpleInd.A => 0
| SimpleInd.B => 1 := λ x => rfl
mutual
inductive Foo : Type → Type
| A : Int → (3 = 3) → String → Foo Int
| B : Bar → Foo String
deriving Hashable
inductive Bar
| C
| D : Foo String → Bar
deriving Hashable
end
#eval hash (Foo.A 3 rfl "bla")
#eval hash (Foo.B $ Bar.D $ Foo.B Bar.C)
inductive ManyConstructors | A | B | C | D | E | F | G | H | I | J | K | L
| M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
deriving Hashable
theorem «Each constructor is hashed as a different number to make mixing better» : ∀ x, hash x =
match x with
| ManyConstructors.A => 0
| ManyConstructors.B => 1
| ManyConstructors.C => 2
| ManyConstructors.D => 3
| ManyConstructors.E => 4
| ManyConstructors.F => 5
| ManyConstructors.G => 6
| ManyConstructors.H => 7
| ManyConstructors.I => 8
| ManyConstructors.J => 9
| ManyConstructors.K => 10
| ManyConstructors.L => 11
| ManyConstructors.M => 12
| ManyConstructors.N => 13
| ManyConstructors.O => 14
| ManyConstructors.P => 15
| ManyConstructors.Q => 16
| ManyConstructors.R => 17
| ManyConstructors.S => 18
| ManyConstructors.T => 19
| ManyConstructors.U => 20
| ManyConstructors.V => 21
| ManyConstructors.W => 22
| ManyConstructors.X => 23
| ManyConstructors.Y => 24
| ManyConstructors.Z => 25 := λ x => rfl
structure Person :=
FirstName : String
LastName : String
Age : Nat
deriving Hashable
structure Company :=
Name : String
CEO : Person
NumberOfEmployees : Nat
deriving Hashable
-- structures hash just fine
#eval hash {
Name := "Microsoft"
CEO := { FirstName := "Satya", LastName := "Nadella", Age := 53 }
NumberOfEmployees := 165000 : Company }
-- 10875484723257753924
-- syntax(name := tst) "tst" : command
-- @[command_elab «tst»] def elab_tst : CommandElab := fun stx => do
-- let declNames := #[`Foo, `Bar]
-- let declNames := #[`Foo]
-- discard $ mkHashableHandler declNames
-- pure ()
|
dd7d9d90e8a0a34b499717e67a2aad6dd5362c8b | 076f5040b63237c6dd928c6401329ed5adcb0e44 | /instructor-notes/2019.11.12.Proofs.lean | d6b521bbd5db916f26953ca7eeaf49beadd52ae4 | [] | no_license | kevinsullivan/uva-cs-dm-f19 | 0f123689cf6cb078f263950b18382a7086bf30be | 09a950752884bd7ade4be33e9e89a2c4b1927167 | refs/heads/master | 1,594,771,841,541 | 1,575,853,850,000 | 1,575,853,850,000 | 205,433,890 | 4 | 9 | null | 1,571,592,121,000 | 1,567,188,539,000 | Lean | UTF-8 | Lean | false | false | 5,346 | lean | /-
There are two key things we do with proofs.
(1) We build them
(2) We use them
In logic, the basic rules for building,
or constructing, proofs go by the name
of introduction rules. The basic rules
for using proofs are called elimination
rules.
Each *form* of proposition has its own
introduction and elimination rules. For
example, to build a proof of P ∧ Q, one
must use the introduction rule for and,
while using a proof of P ∧ Q relies on
the two elimination rules, which serve
to extract from a proof of P ∧ Q a proof
of P or a proof of Q.
In this class, we assume (P Q : Prop),
(T : Type), (x y : T) then summarize,
review, and practice using the rules
to build and use proofs of propositions
of these forms:
- ∀ (p : P), Q [Q usually a predicate]
- x = y
- P → Q
- P ∧ Q
- ∃ (x : P), Q [Q usually a predicate]
A key to understanding how to build
proofs is that it's a top-down and
recursive process. For example, to
build a proof of ∀ (p : P), Q, we
use the introduction rule for ∀. It
tells us to assume that p is some
arbitrary but specific value of type
P, and then to prove Q for that p.
To do this, we recursively apply
our approach to building proofs to
build a proof of Q, but now within
a context in which we can use the
assumption that p is some specific
value of type P. We now repeat the
same process: (1) ask what form is
Q; (2) choose an introduction rule
for that form of proposition. Often
we need smaller proofs to apply the
introduction rules, and for this, we
use proofs we have already built or
assumed, using elimination rules as
necessary.
-/
/-
FORALL INTRODUCTION
Assume that you have an arbitrary but
specific value of the quantified type,
(p : P), then prove Q.
Example: Prove "all balls are green".
Step 0: Formalize proposition (whether
you are foralizing the proof or not).
∀ (b : Ball), Green b
Step 1: We start by assuming that
b0 is an arbitrary but specific
ball, and now all that remains to
be proved is Green b (that this b
in particular) is Green.
Formally: Green b0. Remaining goal.
Example: prove ∀ (n : ℕ), n = n.
Proof: We begin by supposing that
n0 is an arbitrary but specific
natural number. What remains to
be proved in this context is that
n0 = n0. [∀ intro].
To prove that n0 = n0 is trivial
by the axiom that tells us that
the equality relation is reflexive.
Thus we have proved the proposition.
[By reflexive property of equality.]
-/
theorem all_n_eq_n : ∀ (n : ℕ), n = n :=
λ (n0 : ℕ),
eq.refl n0
theorem all_n_eq_n' : ∀ (n : ℕ), n = n :=
begin
assume (n0 : ℕ),
exact eq.refl n0,
end
#check all_n_eq_n
#check all_n_eq_n'
example : ℕ := 5
theorem f :
∀ (n : ℕ), nat.pred (nat.succ n) = n :=
λ (n : ℕ),
(eq.refl n)
/- ∀ Elimination -/
#reduce f 3
/-
Example:
Prove that if every ball is green, then
any specific ball, b0, is green.
(∀ (b : Ball), Green b) → (∀ (b0 : Ball), Green b0)
See below. The elimination rule for forall is based
on the idea that we can *apply* a proof of a forall
to a specific value (an argument) to build proof
for that specific value.
-/
axioms (Ball : Type) (Green : Ball → Prop)
example : (∀ (b : Ball), Green b) → (∀ (b0 : Ball), Green b0) :=
begin
assume f,
assume (b0 : Ball),
exact (f b0),
end
/-
IMPLICATIONS
INTRODUCTION FOR →
Assume that we have a proof of the premise.
Then in that context, construct a proof of
the rest, the conclusion.
Example: Prove
(∀ (b : Ball, Green b)) → ((b0: Ball) → (Green b0))
We begin by assuming that every ball is green,
then in this context, all that remains to be
proved is that if b0 is ball then b0 is Green.
To prove this, we assume that b0 is some specific
ball, and in this context all that remains to be
proved is: b0 is Green. This is proved by the use
of forall eliminatio n. We apply our *assumed*
proof of ∀ (b: Ball), Green b, TO b0, to obtain
a proof that b0 is green.
QED
-/
example : (∀ (b : Ball), Green b) → ∀ (b0: Ball), Green b0 :=
λ (h: (∀ (b : Ball), Green b)),
λ (b0 : Ball),
(h b0)
/-
Assume that we have proof, h, that every ball is green.
In this context what remains to be proved is that any
particular ball is green.
So now we assume that b0 is some arbitrary but specific
ball, and in this extend context of assumptions, all
that remains to be proved is that b0 is green.
But we already have a proof that every ball is green
so all we need to is to apply it to b0, and that
gives us a proof that b0 is green.
QED.
-/
theorem and_commutes : ∀ (P Q : Prop), P ∧ Q → Q ∧ P :=
begin
intros P Q,
assume (h : P ∧ Q),
apply and.intro _ _,
exact (and.elim_right h),
exact (and.elim_left h)
end
theorem and_commutes' : ∀ (P Q : Prop), P ∧ Q → Q ∧ P :=
λ P Q,
λ h,
and.intro
(and.elim_right h)
(and.elim_left h)
namespace hidden
inductive nat : Type
| zero
| succ : nat → nat
def n0 := nat.zero
def n1 := nat.succ nat.zero
def n2 := nat.succ (nat.succ nat.zero)
def n3 := nat.succ (nat.succ (nat.succ nat.zero))
def n4 := nat.succ (nat.succ (nat.succ (nat.succ nat.zero)))
def inc (n : nat) : nat :=
nat.succ n
def dec : nat → nat
| nat.zero := nat.zero
| (nat.succ n') := n'
end hidden |
37412a146d65b9278586ac25732f90eca01f7955 | 2a70b774d16dbdf5a533432ee0ebab6838df0948 | /_target/deps/mathlib/src/analysis/convex/integral.lean | 3a5aa3e7f265b1003cd12e5327cfc087a94fde3f | [
"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 | 7,685 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Yury G. Kudryashov
-/
import analysis.convex.basic
import measure_theory.set_integral
/-!
# Jensen's inequality for integrals
In this file we prove four theorems:
* `convex.smul_integral_mem`: if `μ` is a non-zero finite measure on `α`, `s` is a convex closed set
in `E`, and `f` is an integrable function sending `μ`-a.e. points to `s`, then the average value
of `f` belongs to `s`: `(μ univ).to_real⁻¹ • ∫ x, f x ∂μ ∈ s`. See also `convex.center_mass_mem`
for a finite sum version of this lemma.
* `convex.integral_mem`: if `μ` is a probability measure on `α`, `s` is a convex closed set in `E`,
and `f` is an integrable function sending `μ`-a.e. points to `s`, then the expected value of `f`
belongs to `s`: `∫ x, f x ∂μ ∈ s`. See also `convex.sum_mem` for a finite sum version of this
lemma.
* `convex_on.map_smul_integral_le`: Jensen's inequality: if a function `g : E → ℝ` is convex and
continuous on a convex closed set `s`, `μ` is a finite non-zero measure on `α`, and `f : α → E` is
a function sending `μ`-a.e. points to `s`, then the value of `g` at the average value of `f` is
less than or equal to the average value of `g ∘ f` provided that both `f` and `g ∘ f` are
integrable. See also `convex.map_center_mass_le` for a finite sum version of this lemma.
* `convex_on.map_integral_le`: Jensen's inequality: if a function `g : E → ℝ` is convex and
continuous on a convex closed set `s`, `μ` is a probability measure on `α`, and `f : α → E` is a
function sending `μ`-a.e. points to `s`, then the value of `g` at the expected value of `f` is
less than or equal to the expected value of `g ∘ f` provided that both `f` and `g ∘ f` are
integrable. See also `convex.map_sum_le` for a finite sum version of this lemma.
## Tags
convex, integral, center mass, Jensen's inequality
-/
open measure_theory set filter
open_locale topological_space big_operators
variables {α E : Type*} [measurable_space α] {μ : measure α}
[normed_group E] [normed_space ℝ E] [complete_space E]
[topological_space.second_countable_topology E] [measurable_space E] [borel_space E]
private lemma convex.smul_integral_mem_of_measurable
[finite_measure μ] {s : set E} (hs : convex s) (hsc : is_closed s)
(hμ : μ ≠ 0) {f : α → E} (hfs : ∀ᵐ x ∂μ, f x ∈ s) (hfi : integrable f μ) (hfm : measurable f) :
(μ univ).to_real⁻¹ • ∫ x, f x ∂μ ∈ s :=
begin
rcases eq_empty_or_nonempty s with rfl|⟨y₀, h₀⟩, { refine (hμ _).elim, simpa using hfs },
rw ← hsc.closure_eq at hfs,
have hc : integrable (λ _, y₀) μ := integrable_const _,
set F : ℕ → simple_func α E := simple_func.approx_on f hfm s y₀ h₀,
have : tendsto (λ n, (F n).integral μ) at_top (𝓝 $ ∫ x, f x ∂μ),
{ simp only [simple_func.integral_eq_integral _
(simple_func.integrable_approx_on hfm hfi h₀ hc _)],
exact tendsto_integral_of_l1 _ hfi
(eventually_of_forall $ simple_func.integrable_approx_on hfm hfi h₀ hc)
(simple_func.tendsto_approx_on_l1_edist hfm h₀ hfs (hfi.sub hc).2) },
refine hsc.mem_of_tendsto (tendsto_const_nhds.smul this) (eventually_of_forall $ λ n, _),
have : ∑ y in (F n).range, (μ ((F n) ⁻¹' {y})).to_real = (μ univ).to_real,
by rw [← (F n).sum_range_measure_preimage_singleton, @ennreal.to_real_sum _ _
(λ y, μ ((F n) ⁻¹' {y})) (λ _ _, (measure_lt_top _ _))],
rw [← this, simple_func.integral],
refine hs.center_mass_mem (λ _ _, ennreal.to_real_nonneg) _ _,
{ rw [this, ennreal.to_real_pos_iff, pos_iff_ne_zero, ne.def, measure.measure_univ_eq_zero],
exact ⟨hμ, measure_ne_top _ _⟩ },
{ simp only [simple_func.mem_range],
rintros _ ⟨x, rfl⟩,
exact simple_func.approx_on_mem hfm h₀ n x }
end
/-- If `μ` is a non-zero finite measure on `α`, `s` is a convex closed set in `E`, and `f` is an
integrable function sending `μ`-a.e. points to `s`, then the average value of `f` belongs to `s`:
`(μ univ).to_real⁻¹ • ∫ x, f x ∂μ ∈ s`. See also `convex.center_mass_mem` for a finite sum version
of this lemma. -/
lemma convex.smul_integral_mem
[finite_measure μ] {s : set E} (hs : convex s) (hsc : is_closed s)
(hμ : μ ≠ 0) {f : α → E} (hfs : ∀ᵐ x ∂μ, f x ∈ s) (hfi : integrable f μ) :
(μ univ).to_real⁻¹ • ∫ x, f x ∂μ ∈ s :=
begin
have : ∀ᵐ (x : α) ∂μ, hfi.ae_measurable.mk f x ∈ s,
{ filter_upwards [hfs, hfi.ae_measurable.ae_eq_mk],
assume a ha h,
rwa ← h },
convert convex.smul_integral_mem_of_measurable hs hsc hμ this
(hfi.congr hfi.ae_measurable.ae_eq_mk) (hfi.ae_measurable.measurable_mk) using 2,
apply integral_congr_ae,
exact hfi.ae_measurable.ae_eq_mk
end
/-- If `μ` is a probability measure on `α`, `s` is a convex closed set in `E`, and `f` is an
integrable function sending `μ`-a.e. points to `s`, then the expected value of `f` belongs to `s`:
`∫ x, f x ∂μ ∈ s`. See also `convex.sum_mem` for a finite sum version of this lemma. -/
lemma convex.integral_mem [probability_measure μ] {s : set E} (hs : convex s) (hsc : is_closed s)
{f : α → E} (hf : ∀ᵐ x ∂μ, f x ∈ s) (hfi : integrable f μ) :
∫ x, f x ∂μ ∈ s :=
by simpa [measure_univ] using hs.smul_integral_mem hsc (probability_measure.ne_zero μ) hf hfi
/-- Jensen's inequality: if a function `g : E → ℝ` is convex and continuous on a convex closed set
`s`, `μ` is a finite non-zero measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points
to `s`, then the value of `g` at the average value of `f` is less than or equal to the average value
of `g ∘ f` provided that both `f` and `g ∘ f` are integrable. See also `convex.map_center_mass_le`
for a finite sum version of this lemma. -/
lemma convex_on.map_smul_integral_le [finite_measure μ] {s : set E} {g : E → ℝ} (hg : convex_on s g)
(hgc : continuous_on g s) (hsc : is_closed s) (hμ : μ ≠ 0) {f : α → E} (hfs : ∀ᵐ x ∂μ, f x ∈ s)
(hfi : integrable f μ) (hgi : integrable (g ∘ f) μ) :
g ((μ univ).to_real⁻¹ • ∫ x, f x ∂μ) ≤ (μ univ).to_real⁻¹ • ∫ x, g (f x) ∂μ :=
begin
set t := {p : E × ℝ | p.1 ∈ s ∧ g p.1 ≤ p.2},
have ht_conv : convex t := hg.convex_epigraph,
have ht_closed : is_closed t :=
(hsc.preimage continuous_fst).is_closed_le (hgc.comp continuous_on_fst (subset.refl _))
continuous_on_snd,
have ht_mem : ∀ᵐ x ∂μ, (f x, g (f x)) ∈ t := hfs.mono (λ x hx, ⟨hx, le_rfl⟩),
simpa [integral_pair hfi hgi]
using (ht_conv.smul_integral_mem ht_closed hμ ht_mem (hfi.prod_mk hgi)).2
end
/-- Jensen's inequality: if a function `g : E → ℝ` is convex and continuous on a convex closed set
`s`, `μ` is a probability measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points to
`s`, then the value of `g` at the expected value of `f` is less than or equal to the expected value
of `g ∘ f` provided that both `f` and `g ∘ f` are integrable. See also `convex.map_sum_le` for a
finite sum version of this lemma. -/
lemma convex_on.map_integral_le [probability_measure μ] {s : set E} {g : E → ℝ} (hg : convex_on s g)
(hgc : continuous_on g s) (hsc : is_closed s) {f : α → E} (hfs : ∀ᵐ x ∂μ, f x ∈ s)
(hfi : integrable f μ) (hgi : integrable (g ∘ f) μ) :
g (∫ x, f x ∂μ) ≤ ∫ x, g (f x) ∂μ :=
by simpa [measure_univ]
using hg.map_smul_integral_le hgc hsc (probability_measure.ne_zero μ) hfs hfi hgi
|
8f87e5af2a7cc51b302b6e91e45898b2a72ad810 | 31f556cdeb9239ffc2fad8f905e33987ff4feab9 | /stage0/src/Lean/Compiler/LCNF/Types.lean | c1c37e9097ffc2d4dc813303b5a7ccb3c10c4f81 | [
"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 | 10,870 | lean | /-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.InferType
namespace Lean.Compiler
scoped notation:max "◾" => lcErased
scoped notation:max "⊤" => lcAny
namespace LCNF
structure LCNFTypeExtState where
types : PHashMap Name Expr := {}
instLevelType : Core.InstantiateLevelCache := {}
deriving Inhabited
builtin_initialize lcnfTypeExt : EnvExtension LCNFTypeExtState ←
registerEnvExtension (pure {})
def erasedExpr := mkConst ``lcErased
def anyTypeExpr := mkConst ``lcAny
def _root_.Lean.Expr.isAnyType (e : Expr) :=
e.isAppOf ``lcAny
def _root_.Lean.Expr.isErased (e : Expr) :=
e.isAppOf ``lcErased
def isPropFormerTypeQuick : Expr → Bool
| .forallE _ _ b _ => isPropFormerTypeQuick b
| .sort .zero => true
| _ => false
/--
Return true iff `type` is `Prop` or `As → Prop`.
-/
partial def isPropFormerType (type : Expr) : MetaM Bool := do
match isPropFormerTypeQuick type with
| true => return true
| false => go type #[]
where
go (type : Expr) (xs : Array Expr) : MetaM Bool := do
match type with
| .sort .zero => return true
| .forallE n d b c => Meta.withLocalDecl n c (d.instantiateRev xs) fun x => go b (xs.push x)
| _ =>
let type ← Meta.whnfD (type.instantiateRev xs)
match type with
| .sort .zero => return true
| .forallE .. => go type #[]
| _ => return false
/--
Return true iff `e : Prop` or `e : As → Prop`.
-/
def isPropFormer (e : Expr) : MetaM Bool := do
isPropFormerType (← Meta.inferType e)
/-!
The code generator uses a format based on A-normal form.
This normal form uses many let-expressions and it is very convenient for
applying compiler transformations. However, it creates a few issues
in a dependently typed programming language.
- Many casts are needed.
- It is too expensive to ensure we are not losing typeability when creating join points
and simplifying let-values
- It may not be possible to create a join point because the resulting expression is
not type correct. For example, suppose we are trying to create a join point for
making the following `match` terminal.
```
let x := match a with | true => b | false => c;
k[x]
```
and want to transform this code into
```
let jp := fun x => k[x]
match a with
| true => jp b
| false => jp c
```
where `jp` is a new join point (i.e., a local function that is always fully applied and
tail recursive). In many examples in the Lean code-base, we have to skip this transformation
because it produces a type-incorrect term. Recall that types/propositions in `k[x]` may rely on
the fact that `x` is definitionally equal to `match a with ...` before the creation of
the join point.
Thus, in the first code generator pass, we convert types into a `LCNFType` (Lean Compiler Normal Form Type).
The method `toLCNFType` produces a type with the following properties:
- All constants occurring in the result type are inductive datatypes.
- The arguments of type formers are type formers, `◾`, or `⊤`. We use `◾` to denote erased information,
and `⊤` the any type.
- All type definitions are expanded. If reduction gets stuck, it is replaced with `⊤`.
The goal is to preserve as much information as possible and avoid the problems described above.
Then, we don't have `let x := v; ...` in LCNF code when `x` is a type former.
If the user provides a `let x := v; ...` where x is a type former, we can always expand it when
converting into LCNF.
Thus, given a `let x := v, ...` in occurring in LCNF, we know `x` cannot occur in any type since it is
not a type former.
We try to preserve type information because they unlock new optimizations, and we can type check
the result produced by each code generator step.
Below, we provide some example programs and their erased variants:
-- 1. Source type: `f: (n: Nat) -> (tupleN Nat n)`.
LCNF type: `f: Nat -> Any`.
We convert the return type `(tupleN Nat n) to `Any`, since we cannot reduce
`(tupleN Nat n)` to a term of the form `(InductiveTy ...)`.
-- 2. Source type: `f: (n: Nat) (fin: Fin n) -> (tupleN Nat fin)`.
LCNF type: `f: Nat -> Fin Erased -> Any`.
Since `(Fin n)` has dependency on `n`, we erase the `n` to get the
type `(Fin Erased)`. See that Erased only
occurs at argument position to a type constructor.
- NOTE: we cannot have separate notions of ErasedProof
(which occurs at the value level for erased proofs) and ErasedData
(which occurs at the type level for erased dependencies)
because of universe polymorphism. Thus, we have a single notion of
Erased which unifies the two concepts.
-/
open Meta in
/--
Convert a Lean type into a LCNF type used by the code generator.
-/
partial def toLCNFType (type : Expr) : MetaM Expr := do
if (← isProp type) then
return erasedExpr
let type ← whnfEta type
match type with
| .sort u => return .sort u
| .const .. => visitApp type #[]
| .lam n d b bi =>
withLocalDecl n bi d fun x => do
let d ← toLCNFType d
let b ← toLCNFType (b.instantiate1 x)
if b.isAnyType || b.isErased then
return b
else
return Expr.lam n d (b.abstract #[x]) bi
| .forallE .. => visitForall type #[]
| .app .. => type.withApp visitApp
| .fvar .. => visitApp type #[]
| _ => return anyTypeExpr
where
whnfEta (type : Expr) : MetaM Expr := do
let type ← whnf type
let type' := type.eta
if type' != type then
whnfEta type'
else
return type
visitForall (e : Expr) (xs : Array Expr) : MetaM Expr := do
match e with
| .forallE n d b bi =>
let d := d.instantiateRev xs
withLocalDecl n bi d fun x => do
let d := (← toLCNFType d).abstract xs
return .forallE n d (← visitForall b (xs.push x)) bi
| _ =>
let e ← toLCNFType (e.instantiateRev xs)
return e.abstract xs
visitApp (f : Expr) (args : Array Expr) := do
let fNew ← match f with
| .const declName us =>
let .inductInfo _ ← getConstInfo declName | return anyTypeExpr
pure <| .const declName us
| .fvar .. => pure f
| _ => return anyTypeExpr
let mut result := fNew
for arg in args do
if (← isProp arg) then
result := mkApp result erasedExpr
else if (← isPropFormer arg) then
result := mkApp result erasedExpr
else if (← isTypeFormer arg) then
result := mkApp result (← toLCNFType arg)
else
result := mkApp result erasedExpr
return result
/--
Save the LCNF type for the given declaration.
-/
def saveLCNFType (declName : Name) (type : Expr) : CoreM Unit := do
modifyEnv fun env =>
lcnfTypeExt.modifyState env fun s => { s with types := s.types.insert declName type }
/--
Return the LCNF type for the given declaration.
-/
def getDeclLCNFType (declName : Name) : CoreM Expr := do
match lcnfTypeExt.getState (← getEnv) |>.types.find? declName with
| some type => return type
| none =>
let info ← getConstInfo declName
let type ← Meta.MetaM.run' <| toLCNFType info.type
saveLCNFType declName type
return type
/--
Instantiate the LCNF type for the given declaration with the given universe levels.
-/
def instantiateLCNFTypeLevelParams (declName : Name) (us : List Level) : CoreM Expr := do
if us.isEmpty then
getDeclLCNFType declName
else
if let some (us', r) := lcnfTypeExt.getState (← getEnv) |>.instLevelType.find? declName then
if us == us' then
return r
let type ← getDeclLCNFType declName
let info ← getConstInfo declName
let r := type.instantiateLevelParams info.levelParams us
modifyEnv fun env => lcnfTypeExt.modifyState env fun s => { s with instLevelType := s.instLevelType.insert declName (us, r) }
return r
mutual
partial def joinTypes (a b : Expr) : Expr :=
joinTypes? a b |>.getD anyTypeExpr
partial def joinTypes? (a b : Expr) : Option Expr := do
if a.isAnyType then return a
else if b.isAnyType then return b
else if a == b then return a
else if a.isErased || b.isErased then
return erasedExpr -- See comment at `compatibleTypes`.
else
let a' := a.headBeta
let b' := b.headBeta
if a != a' || b != b' then
joinTypes? a' b'
else
match a, b with
| .mdata _ a, b => joinTypes? a b
| a, .mdata _ b => joinTypes? a b
| .app f a, .app g b =>
(do return .app (← joinTypes? f g) (← joinTypes? a b))
<|>
return anyTypeExpr
| .forallE n d₁ b₁ _, .forallE _ d₂ b₂ _ =>
(do return .forallE n (← joinTypes? d₁ d₂) (joinTypes b₁ b₂) .default)
<|>
return anyTypeExpr
| .lam n d₁ b₁ _, .lam _ d₂ b₂ _ =>
(do return .lam n (← joinTypes? d₁ d₂) (joinTypes b₁ b₂) .default)
<|>
return anyTypeExpr
| _, _ => return anyTypeExpr
end
/--
Return `true` if `type` is a LCNF type former type.
Remark: This is faster than `Lean.Meta.isTypeFormer`, as this
assumes that the input `type` is an LCNF type.
-/
partial def isTypeFormerType (type : Expr) : Bool :=
match type.headBeta with
| .sort .. => true
| .forallE _ _ b _ => isTypeFormerType b
| _ => false
/--
Return `true` if `type` is a predicate.
Examples: `Nat → Prop`, `Prop`, `Int → Bool → Prop`.
-/
partial def isPredicateType (type : Expr) : Bool :=
match type.headBeta with
| .sort .zero => true
| .forallE _ _ b _ => isPredicateType b
| _ => false
/--
Return `true` if `type` is a LCNF type former type or it is an "any" type.
This function is similar to `isTypeFormerType`, but more liberal.
For example, `isTypeFormerType` returns false for `lcAny` and `Nat → lcAny`, but
this function returns true.
-/
partial def maybeTypeFormerType (type : Expr) : Bool :=
match type.headBeta with
| .sort .. => true
| .forallE _ _ b _ => maybeTypeFormerType b
| _ => type.isAnyType
/--
`isClass? type` return `some ClsName` if the LCNF `type` is an instance of the class `ClsName`.
-/
def isClass? (type : Expr) : CoreM (Option Name) := do
let .const declName _ := type.getAppFn | return none
if isClass (← getEnv) declName then
return declName
else
return none
/--
`isArrowClass? type` return `some ClsName` if the LCNF `type` is an instance of the class `ClsName`, or
if it is arrow producing an instance of the class `ClsName`.
-/
partial def isArrowClass? (type : Expr) : CoreM (Option Name) := do
match type.headBeta with
| .forallE _ _ b _ => isArrowClass? b
| _ => isClass? type
partial def getArrowArity (e : Expr) :=
match e.headBeta with
| .forallE _ _ b _ => getArrowArity b + 1
| _ => 0
end Lean.Compiler.LCNF
|
779abc89c54def00f133dff2ef01aa44e2007078 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/algebra/ordered_field.lean | 6d672f58ef58ef79468034f410dda4060ce63da8 | [
"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 | 23,904 | lean | /-
Copyright (c) 2014 Robert Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Lewis, Leonardo de Moura, Mario Carneiro, Floris van Doorn
-/
import algebra.ordered_ring
import algebra.field
/-!
### Linear ordered fields
A linear ordered field is a field equipped with a linear order such that
* addition respects the order: `a ≤ b → c + a ≤ c + b`;
* multiplication of positives is positive: `0 < a → 0 < b → 0 < a * b`;
* `0 < 1`.
### Main Definitions
* `linear_ordered_field`: the class of linear ordered fields.
-/
set_option old_structure_cmd true
variable {α : Type*}
/-- A linear ordered field is a field with a linear order respecting the operations. -/
@[protect_proj] class linear_ordered_field (α : Type*) extends linear_ordered_comm_ring α, field α
section linear_ordered_field
variables [linear_ordered_field α] {a b c d e : α}
/-!
### Lemmas about pos, nonneg, nonpos, neg
-/
@[simp] lemma inv_pos : 0 < a⁻¹ ↔ 0 < a :=
suffices ∀ a : α, 0 < a → 0 < a⁻¹,
from ⟨λ h, inv_inv' a ▸ this _ h, this a⟩,
assume a ha, flip lt_of_mul_lt_mul_left ha.le $ by simp [ne_of_gt ha, zero_lt_one]
@[simp] lemma inv_nonneg : 0 ≤ a⁻¹ ↔ 0 ≤ a :=
by simp only [le_iff_eq_or_lt, inv_pos, zero_eq_inv]
@[simp] lemma inv_lt_zero : a⁻¹ < 0 ↔ a < 0 :=
by simp only [← not_le, inv_nonneg]
@[simp] lemma inv_nonpos : a⁻¹ ≤ 0 ↔ a ≤ 0 :=
by simp only [← not_lt, inv_pos]
lemma one_div_pos : 0 < 1 / a ↔ 0 < a :=
inv_eq_one_div a ▸ inv_pos
lemma one_div_neg : 1 / a < 0 ↔ a < 0 :=
inv_eq_one_div a ▸ inv_lt_zero
lemma one_div_nonneg : 0 ≤ 1 / a ↔ 0 ≤ a :=
inv_eq_one_div a ▸ inv_nonneg
lemma one_div_nonpos : 1 / a ≤ 0 ↔ a ≤ 0 :=
inv_eq_one_div a ▸ inv_nonpos
lemma div_pos_iff : 0 < a / b ↔ 0 < a ∧ 0 < b ∨ a < 0 ∧ b < 0 :=
by simp [division_def, mul_pos_iff]
lemma div_neg_iff : a / b < 0 ↔ 0 < a ∧ b < 0 ∨ a < 0 ∧ 0 < b :=
by simp [division_def, mul_neg_iff]
lemma div_nonneg_iff : 0 ≤ a / b ↔ 0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0 :=
by simp [division_def, mul_nonneg_iff]
lemma div_nonpos_iff : a / b ≤ 0 ↔ 0 ≤ a ∧ b ≤ 0 ∨ a ≤ 0 ∧ 0 ≤ b :=
by simp [division_def, mul_nonpos_iff]
lemma div_pos (ha : 0 < a) (hb : 0 < b) : 0 < a / b :=
mul_pos ha (inv_pos.2 hb)
lemma div_pos_of_neg_of_neg (ha : a < 0) (hb : b < 0) : 0 < a / b :=
mul_pos_of_neg_of_neg ha (inv_lt_zero.2 hb)
lemma div_neg_of_neg_of_pos (ha : a < 0) (hb : 0 < b) : a / b < 0 :=
mul_neg_of_neg_of_pos ha (inv_pos.2 hb)
lemma div_neg_of_pos_of_neg (ha : 0 < a) (hb : b < 0) : a / b < 0 :=
mul_neg_of_pos_of_neg ha (inv_lt_zero.2 hb)
lemma div_nonneg (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a / b :=
mul_nonneg ha (inv_nonneg.2 hb)
lemma div_nonneg_of_nonpos (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a / b :=
mul_nonneg_of_nonpos_of_nonpos ha (inv_nonpos.2 hb)
lemma div_nonpos_of_nonpos_of_nonneg (ha : a ≤ 0) (hb : 0 ≤ b) : a / b ≤ 0 :=
mul_nonpos_of_nonpos_of_nonneg ha (inv_nonneg.2 hb)
lemma div_nonpos_of_nonneg_of_nonpos (ha : 0 ≤ a) (hb : b ≤ 0) : a / b ≤ 0 :=
mul_nonpos_of_nonneg_of_nonpos ha (inv_nonpos.2 hb)
/-!
### Relating one division with another term.
-/
lemma le_div_iff (hc : 0 < c) : a ≤ b / c ↔ a * c ≤ b :=
⟨λ h, div_mul_cancel b (ne_of_lt hc).symm ▸ mul_le_mul_of_nonneg_right h hc.le,
λ h, calc
a = a * c * (1 / c) : mul_mul_div a (ne_of_lt hc).symm
... ≤ b * (1 / c) : mul_le_mul_of_nonneg_right h (one_div_pos.2 hc).le
... = b / c : (div_eq_mul_one_div b c).symm⟩
lemma le_div_iff' (hc : 0 < c) : a ≤ b / c ↔ c * a ≤ b :=
by rw [mul_comm, le_div_iff hc]
lemma div_le_iff (hb : 0 < b) : a / b ≤ c ↔ a ≤ c * b :=
⟨λ h, calc
a = a / b * b : by rw (div_mul_cancel _ (ne_of_lt hb).symm)
... ≤ c * b : mul_le_mul_of_nonneg_right h hb.le,
λ h, calc
a / b = a * (1 / b) : div_eq_mul_one_div a b
... ≤ (c * b) * (1 / b) : mul_le_mul_of_nonneg_right h (one_div_pos.2 hb).le
... = (c * b) / b : (div_eq_mul_one_div (c * b) b).symm
... = c : by refine (div_eq_iff (ne_of_gt hb)).mpr rfl⟩
lemma div_le_iff' (hb : 0 < b) : a / b ≤ c ↔ a ≤ b * c :=
by rw [mul_comm, div_le_iff hb]
lemma lt_div_iff (hc : 0 < c) : a < b / c ↔ a * c < b :=
lt_iff_lt_of_le_iff_le $ div_le_iff hc
lemma lt_div_iff' (hc : 0 < c) : a < b / c ↔ c * a < b :=
by rw [mul_comm, lt_div_iff hc]
lemma div_lt_iff (hc : 0 < c) : b / c < a ↔ b < a * c :=
lt_iff_lt_of_le_iff_le (le_div_iff hc)
lemma div_lt_iff' (hc : 0 < c) : b / c < a ↔ b < c * a :=
by rw [mul_comm, div_lt_iff hc]
lemma inv_mul_le_iff (h : 0 < b) : b⁻¹ * a ≤ c ↔ a ≤ b * c :=
begin
rw [inv_eq_one_div, mul_comm, ← div_eq_mul_one_div],
exact div_le_iff' h,
end
lemma inv_mul_le_iff' (h : 0 < b) : b⁻¹ * a ≤ c ↔ a ≤ c * b :=
by rw [inv_mul_le_iff h, mul_comm]
lemma mul_inv_le_iff (h : 0 < b) : a * b⁻¹ ≤ c ↔ a ≤ b * c :=
by rw [mul_comm, inv_mul_le_iff h]
lemma mul_inv_le_iff' (h : 0 < b) : a * b⁻¹ ≤ c ↔ a ≤ c * b :=
by rw [mul_comm, inv_mul_le_iff' h]
lemma inv_mul_lt_iff (h : 0 < b) : b⁻¹ * a < c ↔ a < b * c :=
begin
rw [inv_eq_one_div, mul_comm, ← div_eq_mul_one_div],
exact div_lt_iff' h,
end
lemma inv_mul_lt_iff' (h : 0 < b) : b⁻¹ * a < c ↔ a < c * b :=
by rw [inv_mul_lt_iff h, mul_comm]
lemma mul_inv_lt_iff (h : 0 < b) : a * b⁻¹ < c ↔ a < b * c :=
by rw [mul_comm, inv_mul_lt_iff h]
lemma mul_inv_lt_iff' (h : 0 < b) : a * b⁻¹ < c ↔ a < c * b :=
by rw [mul_comm, inv_mul_lt_iff' h]
lemma div_le_iff_of_neg (hc : c < 0) : b / c ≤ a ↔ a * c ≤ b :=
⟨λ h, div_mul_cancel b (ne_of_lt hc) ▸ mul_le_mul_of_nonpos_right h hc.le,
λ h, calc
a = a * c * (1 / c) : mul_mul_div a (ne_of_lt hc)
... ≥ b * (1 / c) : mul_le_mul_of_nonpos_right h (one_div_neg.2 hc).le
... = b / c : (div_eq_mul_one_div b c).symm⟩
lemma div_le_iff_of_neg' (hc : c < 0) : b / c ≤ a ↔ c * a ≤ b :=
by rw [mul_comm, div_le_iff_of_neg hc]
lemma le_div_iff_of_neg (hc : c < 0) : a ≤ b / c ↔ b ≤ a * c :=
by rw [← neg_neg c, mul_neg_eq_neg_mul_symm, div_neg, le_neg,
div_le_iff (neg_pos.2 hc), neg_mul_eq_neg_mul_symm]
lemma le_div_iff_of_neg' (hc : c < 0) : a ≤ b / c ↔ b ≤ c * a :=
by rw [mul_comm, le_div_iff_of_neg hc]
lemma div_lt_iff_of_neg (hc : c < 0) : b / c < a ↔ a * c < b :=
lt_iff_lt_of_le_iff_le $ le_div_iff_of_neg hc
lemma div_lt_iff_of_neg' (hc : c < 0) : b / c < a ↔ c * a < b :=
by rw [mul_comm, div_lt_iff_of_neg hc]
lemma lt_div_iff_of_neg (hc : c < 0) : a < b / c ↔ b < a * c :=
lt_iff_lt_of_le_iff_le $ div_le_iff_of_neg hc
lemma lt_div_iff_of_neg' (hc : c < 0) : a < b / c ↔ b < c * a :=
by rw [mul_comm, lt_div_iff_of_neg hc]
/-- One direction of `div_le_iff` where `b` is allowed to be `0` (but `c` must be nonnegative) -/
lemma div_le_iff_of_nonneg_of_le (hb : 0 ≤ b) (hc : 0 ≤ c) (h : a ≤ c * b) : a / b ≤ c :=
by { rcases eq_or_lt_of_le hb with rfl|hb', simp [hc], rwa [div_le_iff hb'] }
/-!
### Bi-implications of inequalities using inversions
-/
lemma inv_le_inv_of_le (ha : 0 < a) (h : a ≤ b) : b⁻¹ ≤ a⁻¹ :=
by rwa [← one_div a, le_div_iff' ha, ← div_eq_mul_inv, div_le_iff (ha.trans_le h), one_mul]
/-- See `inv_le_inv_of_le` for the implication from right-to-left with one fewer assumption. -/
lemma inv_le_inv (ha : 0 < a) (hb : 0 < b) : a⁻¹ ≤ b⁻¹ ↔ b ≤ a :=
by rw [← one_div, div_le_iff ha, ← div_eq_inv_mul, le_div_iff hb, one_mul]
lemma inv_le (ha : 0 < a) (hb : 0 < b) : a⁻¹ ≤ b ↔ b⁻¹ ≤ a :=
by rw [← inv_le_inv hb (inv_pos.2 ha), inv_inv']
lemma le_inv (ha : 0 < a) (hb : 0 < b) : a ≤ b⁻¹ ↔ b ≤ a⁻¹ :=
by rw [← inv_le_inv (inv_pos.2 hb) ha, inv_inv']
lemma inv_lt_inv (ha : 0 < a) (hb : 0 < b) : a⁻¹ < b⁻¹ ↔ b < a :=
lt_iff_lt_of_le_iff_le (inv_le_inv hb ha)
lemma inv_lt (ha : 0 < a) (hb : 0 < b) : a⁻¹ < b ↔ b⁻¹ < a :=
lt_iff_lt_of_le_iff_le (le_inv hb ha)
lemma lt_inv (ha : 0 < a) (hb : 0 < b) : a < b⁻¹ ↔ b < a⁻¹ :=
lt_iff_lt_of_le_iff_le (inv_le hb ha)
lemma inv_le_inv_of_neg (ha : a < 0) (hb : b < 0) : a⁻¹ ≤ b⁻¹ ↔ b ≤ a :=
by rw [← one_div, div_le_iff_of_neg ha, ← div_eq_inv_mul, div_le_iff_of_neg hb, one_mul]
lemma inv_le_of_neg (ha : a < 0) (hb : b < 0) : a⁻¹ ≤ b ↔ b⁻¹ ≤ a :=
by rw [← inv_le_inv_of_neg hb (inv_lt_zero.2 ha), inv_inv']
lemma le_inv_of_neg (ha : a < 0) (hb : b < 0) : a ≤ b⁻¹ ↔ b ≤ a⁻¹ :=
by rw [← inv_le_inv_of_neg (inv_lt_zero.2 hb) ha, inv_inv']
lemma inv_lt_inv_of_neg (ha : a < 0) (hb : b < 0) : a⁻¹ < b⁻¹ ↔ b < a :=
lt_iff_lt_of_le_iff_le (inv_le_inv_of_neg hb ha)
lemma inv_lt_of_neg (ha : a < 0) (hb : b < 0) : a⁻¹ < b ↔ b⁻¹ < a :=
lt_iff_lt_of_le_iff_le (le_inv_of_neg hb ha)
lemma lt_inv_of_neg (ha : a < 0) (hb : b < 0) : a < b⁻¹ ↔ b < a⁻¹ :=
lt_iff_lt_of_le_iff_le (inv_le_of_neg hb ha)
lemma inv_lt_one (ha : 1 < a) : a⁻¹ < 1 :=
by rwa [inv_lt ((@zero_lt_one α _ _).trans ha) zero_lt_one, inv_one]
lemma one_lt_inv (h₁ : 0 < a) (h₂ : a < 1) : 1 < a⁻¹ :=
by rwa [lt_inv (@zero_lt_one α _ _) h₁, inv_one]
lemma inv_le_one (ha : 1 ≤ a) : a⁻¹ ≤ 1 :=
by rwa [inv_le ((@zero_lt_one α _ _).trans_le ha) zero_lt_one, inv_one]
lemma one_le_inv (h₁ : 0 < a) (h₂ : a ≤ 1) : 1 ≤ a⁻¹ :=
by rwa [le_inv (@zero_lt_one α _ _) h₁, inv_one]
lemma inv_lt_one_iff_of_pos (h₀ : 0 < a) : a⁻¹ < 1 ↔ 1 < a :=
⟨λ h₁, inv_inv' a ▸ one_lt_inv (inv_pos.2 h₀) h₁, inv_lt_one⟩
lemma inv_lt_one_iff : a⁻¹ < 1 ↔ a ≤ 0 ∨ 1 < a :=
begin
cases le_or_lt a 0 with ha ha,
{ simp [ha, (inv_nonpos.2 ha).trans_lt zero_lt_one] },
{ simp only [ha.not_le, false_or, inv_lt_one_iff_of_pos ha] }
end
lemma one_lt_inv_iff : 1 < a⁻¹ ↔ 0 < a ∧ a < 1 :=
⟨λ h, ⟨inv_pos.1 (zero_lt_one.trans h), inv_inv' a ▸ inv_lt_one h⟩, and_imp.2 one_lt_inv⟩
lemma inv_le_one_iff : a⁻¹ ≤ 1 ↔ a ≤ 0 ∨ 1 ≤ a :=
begin
rcases em (a = 1) with (rfl|ha),
{ simp [le_rfl] },
{ simp only [ne.le_iff_lt (ne.symm ha), ne.le_iff_lt (mt inv_eq_one'.1 ha), inv_lt_one_iff] }
end
lemma one_le_inv_iff : 1 ≤ a⁻¹ ↔ 0 < a ∧ a ≤ 1 :=
⟨λ h, ⟨inv_pos.1 (zero_lt_one.trans_le h), inv_inv' a ▸ inv_le_one h⟩, and_imp.2 one_le_inv⟩
/-!
### Relating two divisions.
-/
lemma div_le_div_of_le (hc : 0 ≤ c) (h : a ≤ b) : a / c ≤ b / c :=
begin
rw [div_eq_mul_one_div a c, div_eq_mul_one_div b c],
exact mul_le_mul_of_nonneg_right h (one_div_nonneg.2 hc)
end
lemma div_le_div_of_le_left (ha : 0 ≤ a) (hc : 0 < c) (h : c ≤ b) : a / b ≤ a / c :=
begin
rw [div_eq_mul_inv, div_eq_mul_inv],
exact mul_le_mul_of_nonneg_left ((inv_le_inv (hc.trans_le h) hc).mpr h) ha
end
lemma div_le_div_of_le_of_nonneg (hab : a ≤ b) (hc : 0 ≤ c) : a / c ≤ b / c :=
mul_le_mul_of_nonneg_right hab (inv_nonneg.2 hc)
lemma div_le_div_of_nonpos_of_le (hc : c ≤ 0) (h : b ≤ a) : a / c ≤ b / c :=
begin
rw [div_eq_mul_one_div a c, div_eq_mul_one_div b c],
exact mul_le_mul_of_nonpos_right h (one_div_nonpos.2 hc)
end
lemma div_lt_div_of_lt (hc : 0 < c) (h : a < b) : a / c < b / c :=
begin
rw [div_eq_mul_one_div a c, div_eq_mul_one_div b c],
exact mul_lt_mul_of_pos_right h (one_div_pos.2 hc)
end
lemma div_lt_div_of_neg_of_lt (hc : c < 0) (h : b < a) : a / c < b / c :=
begin
rw [div_eq_mul_one_div a c, div_eq_mul_one_div b c],
exact mul_lt_mul_of_neg_right h (one_div_neg.2 hc)
end
lemma div_le_div_right (hc : 0 < c) : a / c ≤ b / c ↔ a ≤ b :=
⟨le_imp_le_of_lt_imp_lt $ div_lt_div_of_lt hc, div_le_div_of_le $ hc.le⟩
lemma div_le_div_right_of_neg (hc : c < 0) : a / c ≤ b / c ↔ b ≤ a :=
⟨le_imp_le_of_lt_imp_lt $ div_lt_div_of_neg_of_lt hc, div_le_div_of_nonpos_of_le $ hc.le⟩
lemma div_lt_div_right (hc : 0 < c) : a / c < b / c ↔ a < b :=
lt_iff_lt_of_le_iff_le $ div_le_div_right hc
lemma div_lt_div_right_of_neg (hc : c < 0) : a / c < b / c ↔ b < a :=
lt_iff_lt_of_le_iff_le $ div_le_div_right_of_neg hc
lemma div_lt_div_left (ha : 0 < a) (hb : 0 < b) (hc : 0 < c) : a / b < a / c ↔ c < b :=
(mul_lt_mul_left ha).trans (inv_lt_inv hb hc)
lemma div_le_div_left (ha : 0 < a) (hb : 0 < b) (hc : 0 < c) : a / b ≤ a / c ↔ c ≤ b :=
le_iff_le_iff_lt_iff_lt.2 (div_lt_div_left ha hc hb)
lemma div_lt_div_iff (b0 : 0 < b) (d0 : 0 < d) :
a / b < c / d ↔ a * d < c * b :=
by rw [lt_div_iff d0, div_mul_eq_mul_div, div_lt_iff b0]
lemma div_le_div_iff (b0 : 0 < b) (d0 : 0 < d) : a / b ≤ c / d ↔ a * d ≤ c * b :=
by rw [le_div_iff d0, div_mul_eq_mul_div, div_le_iff b0]
lemma div_le_div (hc : 0 ≤ c) (hac : a ≤ c) (hd : 0 < d) (hbd : d ≤ b) : a / b ≤ c / d :=
by { rw div_le_div_iff (hd.trans_le hbd) hd, exact mul_le_mul hac hbd hd.le hc }
lemma div_lt_div (hac : a < c) (hbd : d ≤ b) (c0 : 0 ≤ c) (d0 : 0 < d) :
a / b < c / d :=
(div_lt_div_iff (d0.trans_le hbd) d0).2 (mul_lt_mul hac hbd d0 c0)
lemma div_lt_div' (hac : a ≤ c) (hbd : d < b) (c0 : 0 < c) (d0 : 0 < d) :
a / b < c / d :=
(div_lt_div_iff (d0.trans hbd) d0).2 (mul_lt_mul' hac hbd d0.le c0)
lemma div_lt_div_of_lt_left (hb : 0 < b) (h : b < a) (hc : 0 < c) : c / a < c / b :=
(div_lt_div_left hc (hb.trans h) hb).mpr h
/-!
### Relating one division and involving `1`
-/
lemma one_le_div (hb : 0 < b) : 1 ≤ a / b ↔ b ≤ a :=
by rw [le_div_iff hb, one_mul]
lemma div_le_one (hb : 0 < b) : a / b ≤ 1 ↔ a ≤ b :=
by rw [div_le_iff hb, one_mul]
lemma one_lt_div (hb : 0 < b) : 1 < a / b ↔ b < a :=
by rw [lt_div_iff hb, one_mul]
lemma div_lt_one (hb : 0 < b) : a / b < 1 ↔ a < b :=
by rw [div_lt_iff hb, one_mul]
lemma one_le_div_of_neg (hb : b < 0) : 1 ≤ a / b ↔ a ≤ b :=
by rw [le_div_iff_of_neg hb, one_mul]
lemma div_le_one_of_neg (hb : b < 0) : a / b ≤ 1 ↔ b ≤ a :=
by rw [div_le_iff_of_neg hb, one_mul]
lemma one_lt_div_of_neg (hb : b < 0) : 1 < a / b ↔ a < b :=
by rw [lt_div_iff_of_neg hb, one_mul]
lemma div_lt_one_of_neg (hb : b < 0) : a / b < 1 ↔ b < a :=
by rw [div_lt_iff_of_neg hb, one_mul]
lemma one_div_le (ha : 0 < a) (hb : 0 < b) : 1 / a ≤ b ↔ 1 / b ≤ a :=
by simpa using inv_le ha hb
lemma one_div_lt (ha : 0 < a) (hb : 0 < b) : 1 / a < b ↔ 1 / b < a :=
by simpa using inv_lt ha hb
lemma le_one_div (ha : 0 < a) (hb : 0 < b) : a ≤ 1 / b ↔ b ≤ 1 / a :=
by simpa using le_inv ha hb
lemma lt_one_div (ha : 0 < a) (hb : 0 < b) : a < 1 / b ↔ b < 1 / a :=
by simpa using lt_inv ha hb
lemma one_div_le_of_neg (ha : a < 0) (hb : b < 0) : 1 / a ≤ b ↔ 1 / b ≤ a :=
by simpa using inv_le_of_neg ha hb
lemma one_div_lt_of_neg (ha : a < 0) (hb : b < 0) : 1 / a < b ↔ 1 / b < a :=
by simpa using inv_lt_of_neg ha hb
lemma le_one_div_of_neg (ha : a < 0) (hb : b < 0) : a ≤ 1 / b ↔ b ≤ 1 / a :=
by simpa using le_inv_of_neg ha hb
lemma lt_one_div_of_neg (ha : a < 0) (hb : b < 0) : a < 1 / b ↔ b < 1 / a :=
by simpa using lt_inv_of_neg ha hb
lemma one_lt_div_iff : 1 < a / b ↔ 0 < b ∧ b < a ∨ b < 0 ∧ a < b :=
begin
rcases lt_trichotomy b 0 with (hb|rfl|hb),
{ simp [hb, hb.not_lt, one_lt_div_of_neg] },
{ simp [lt_irrefl, zero_le_one] },
{ simp [hb, hb.not_lt, one_lt_div] }
end
lemma one_le_div_iff : 1 ≤ a / b ↔ 0 < b ∧ b ≤ a ∨ b < 0 ∧ a ≤ b :=
begin
rcases lt_trichotomy b 0 with (hb|rfl|hb),
{ simp [hb, hb.not_lt, one_le_div_of_neg] },
{ simp [lt_irrefl, zero_lt_one.not_le, zero_lt_one] },
{ simp [hb, hb.not_lt, one_le_div] }
end
lemma div_lt_one_iff : a / b < 1 ↔ 0 < b ∧ a < b ∨ b = 0 ∨ b < 0 ∧ b < a :=
begin
rcases lt_trichotomy b 0 with (hb|rfl|hb),
{ simp [hb, hb.not_lt, hb.ne, div_lt_one_of_neg] },
{ simp [zero_lt_one], },
{ simp [hb, hb.not_lt, div_lt_one, hb.ne.symm] }
end
lemma div_le_one_iff : a / b ≤ 1 ↔ 0 < b ∧ a ≤ b ∨ b = 0 ∨ b < 0 ∧ b ≤ a :=
begin
rcases lt_trichotomy b 0 with (hb|rfl|hb),
{ simp [hb, hb.not_lt, hb.ne, div_le_one_of_neg] },
{ simp [zero_le_one], },
{ simp [hb, hb.not_lt, div_le_one, hb.ne.symm] }
end
/-!
### Relating two divisions, involving `1`
-/
lemma one_div_le_one_div_of_le (ha : 0 < a) (h : a ≤ b) : 1 / b ≤ 1 / a :=
by simpa using inv_le_inv_of_le ha h
lemma one_div_lt_one_div_of_lt (ha : 0 < a) (h : a < b) : 1 / b < 1 / a :=
by rwa [lt_div_iff' ha, ← div_eq_mul_one_div, div_lt_one (ha.trans h)]
lemma one_div_le_one_div_of_neg_of_le (hb : b < 0) (h : a ≤ b) : 1 / b ≤ 1 / a :=
by rwa [div_le_iff_of_neg' hb, ← div_eq_mul_one_div, div_le_one_of_neg (h.trans_lt hb)]
lemma one_div_lt_one_div_of_neg_of_lt (hb : b < 0) (h : a < b) : 1 / b < 1 / a :=
by rwa [div_lt_iff_of_neg' hb, ← div_eq_mul_one_div, div_lt_one_of_neg (h.trans hb)]
lemma le_of_one_div_le_one_div (ha : 0 < a) (h : 1 / a ≤ 1 / b) : b ≤ a :=
le_imp_le_of_lt_imp_lt (one_div_lt_one_div_of_lt ha) h
lemma lt_of_one_div_lt_one_div (ha : 0 < a) (h : 1 / a < 1 / b) : b < a :=
lt_imp_lt_of_le_imp_le (one_div_le_one_div_of_le ha) h
lemma le_of_neg_of_one_div_le_one_div (hb : b < 0) (h : 1 / a ≤ 1 / b) : b ≤ a :=
le_imp_le_of_lt_imp_lt (one_div_lt_one_div_of_neg_of_lt hb) h
lemma lt_of_neg_of_one_div_lt_one_div (hb : b < 0) (h : 1 / a < 1 / b) : b < a :=
lt_imp_lt_of_le_imp_le (one_div_le_one_div_of_neg_of_le hb) h
/-- For the single implications with fewer assumptions, see `one_div_le_one_div_of_le` and
`le_of_one_div_le_one_div` -/
lemma one_div_le_one_div (ha : 0 < a) (hb : 0 < b) : 1 / a ≤ 1 / b ↔ b ≤ a :=
div_le_div_left zero_lt_one ha hb
/-- For the single implications with fewer assumptions, see `one_div_lt_one_div_of_lt` and
`lt_of_one_div_lt_one_div` -/
lemma one_div_lt_one_div (ha : 0 < a) (hb : 0 < b) : 1 / a < 1 / b ↔ b < a :=
div_lt_div_left zero_lt_one ha hb
/-- For the single implications with fewer assumptions, see `one_div_lt_one_div_of_neg_of_lt` and
`lt_of_one_div_lt_one_div` -/
lemma one_div_le_one_div_of_neg (ha : a < 0) (hb : b < 0) : 1 / a ≤ 1 / b ↔ b ≤ a :=
by simpa [one_div] using inv_le_inv_of_neg ha hb
/-- For the single implications with fewer assumptions, see `one_div_lt_one_div_of_lt` and
`lt_of_one_div_lt_one_div` -/
lemma one_div_lt_one_div_of_neg (ha : a < 0) (hb : b < 0) : 1 / a < 1 / b ↔ b < a :=
lt_iff_lt_of_le_iff_le (one_div_le_one_div_of_neg hb ha)
lemma one_lt_one_div (h1 : 0 < a) (h2 : a < 1) : 1 < 1 / a :=
by rwa [lt_one_div (@zero_lt_one α _ _) h1, one_div_one]
lemma one_le_one_div (h1 : 0 < a) (h2 : a ≤ 1) : 1 ≤ 1 / a :=
by rwa [le_one_div (@zero_lt_one α _ _) h1, one_div_one]
lemma one_div_lt_neg_one (h1 : a < 0) (h2 : -1 < a) : 1 / a < -1 :=
suffices 1 / a < 1 / -1, by rwa one_div_neg_one_eq_neg_one at this,
one_div_lt_one_div_of_neg_of_lt h1 h2
lemma one_div_le_neg_one (h1 : a < 0) (h2 : -1 ≤ a) : 1 / a ≤ -1 :=
suffices 1 / a ≤ 1 / -1, by rwa one_div_neg_one_eq_neg_one at this,
one_div_le_one_div_of_neg_of_le h1 h2
/-!
### Results about halving.
The equalities also hold in fields of characteristic `0`. -/
lemma add_halves (a : α) : a / 2 + a / 2 = a :=
by rw [div_add_div_same, ← two_mul, mul_div_cancel_left a two_ne_zero]
lemma sub_self_div_two (a : α) : a - a / 2 = a / 2 :=
suffices a / 2 + a / 2 - a / 2 = a / 2, by rwa add_halves at this,
by rw [add_sub_cancel]
lemma div_two_sub_self (a : α) : a / 2 - a = - (a / 2) :=
suffices a / 2 - (a / 2 + a / 2) = - (a / 2), by rwa add_halves at this,
by rw [sub_add_eq_sub_sub, sub_self, zero_sub]
lemma add_self_div_two (a : α) : (a + a) / 2 = a :=
by rw [← mul_two, mul_div_cancel a two_ne_zero]
lemma half_pos (h : 0 < a) : 0 < a / 2 := div_pos h zero_lt_two
lemma one_half_pos : (0:α) < 1 / 2 := half_pos zero_lt_one
lemma div_two_lt_of_pos (h : 0 < a) : a / 2 < a :=
by { rw [div_lt_iff (@zero_lt_two α _ _)], exact lt_mul_of_one_lt_right h one_lt_two }
lemma half_lt_self : 0 < a → a / 2 < a := div_two_lt_of_pos
lemma one_half_lt_one : (1 / 2 : α) < 1 := half_lt_self zero_lt_one
lemma add_sub_div_two_lt (h : a < b) : a + (b - a) / 2 < b :=
begin
rwa [← div_sub_div_same, sub_eq_add_neg, add_comm (b/2), ← add_assoc, ← sub_eq_add_neg,
← lt_sub_iff_add_lt, sub_self_div_two, sub_self_div_two, div_lt_div_right (@zero_lt_two α _ _)]
end
/-!
### Miscellaneous lemmas
-/
lemma mul_sub_mul_div_mul_neg_iff (hc : c ≠ 0) (hd : d ≠ 0) :
(a * d - b * c) / (c * d) < 0 ↔ a / c < b / d :=
by rw [mul_comm b c, ← div_sub_div _ _ hc hd, sub_lt_zero]
alias mul_sub_mul_div_mul_neg_iff ↔ div_lt_div_of_mul_sub_mul_div_neg mul_sub_mul_div_mul_neg
lemma mul_sub_mul_div_mul_nonpos_iff (hc : c ≠ 0) (hd : d ≠ 0) :
(a * d - b * c) / (c * d) ≤ 0 ↔ a / c ≤ b / d :=
by rw [mul_comm b c, ← div_sub_div _ _ hc hd, sub_nonpos]
alias mul_sub_mul_div_mul_nonpos_iff ↔
div_le_div_of_mul_sub_mul_div_nonpos mul_sub_mul_div_mul_nonpos
lemma mul_le_mul_of_mul_div_le (h : a * (b / c) ≤ d) (hc : 0 < c) : b * a ≤ d * c :=
begin
rw [← mul_div_assoc] at h,
rwa [mul_comm b, ← div_le_iff hc],
end
lemma div_mul_le_div_mul_of_div_le_div (h : a / b ≤ c / d) (he : 0 ≤ e) :
a / (b * e) ≤ c / (d * e) :=
begin
rw [div_mul_eq_div_mul_one_div, div_mul_eq_div_mul_one_div],
exact mul_le_mul_of_nonneg_right h (one_div_nonneg.2 he)
end
lemma exists_add_lt_and_pos_of_lt (h : b < a) : ∃ c : α, b + c < a ∧ 0 < c :=
⟨(a - b) / 2, add_sub_div_two_lt h, div_pos (sub_pos_of_lt h) zero_lt_two⟩
lemma le_of_forall_sub_le (h : ∀ ε > 0, b - ε ≤ a) : b ≤ a :=
begin
contrapose! h,
simpa only [and_comm ((0 : α) < _), lt_sub_iff_add_lt, gt_iff_lt]
using exists_add_lt_and_pos_of_lt h,
end
lemma monotone.div_const {β : Type*} [preorder β] {f : β → α} (hf : monotone f)
{c : α} (hc : 0 ≤ c) : monotone (λ x, (f x) / c) :=
hf.mul_const (inv_nonneg.2 hc)
lemma strict_mono.div_const {β : Type*} [preorder β] {f : β → α} (hf : strict_mono f)
{c : α} (hc : 0 < c) :
strict_mono (λ x, (f x) / c) :=
hf.mul_const (inv_pos.2 hc)
@[priority 100] -- see Note [lower instance priority]
instance linear_ordered_field.to_densely_ordered : densely_ordered α :=
{ dense := λ a₁ a₂ h, ⟨(a₁ + a₂) / 2,
calc a₁ = (a₁ + a₁) / 2 : (add_self_div_two a₁).symm
... < (a₁ + a₂) / 2 : div_lt_div_of_lt zero_lt_two (add_lt_add_left h _),
calc (a₁ + a₂) / 2 < (a₂ + a₂) / 2 : div_lt_div_of_lt zero_lt_two (add_lt_add_right h _)
... = a₂ : add_self_div_two a₂⟩ }
lemma mul_self_inj_of_nonneg (a0 : 0 ≤ a) (b0 : 0 ≤ b) : a * a = b * b ↔ a = b :=
mul_self_eq_mul_self_iff.trans $ or_iff_left_of_imp $
λ h, by { subst a, have : b = 0 := le_antisymm (neg_nonneg.1 a0) b0, rw [this, neg_zero] }
lemma min_div_div_right {c : α} (hc : 0 ≤ c) (a b : α) : min (a / c) (b / c) = (min a b) / c :=
eq.symm $ monotone.map_min (λ x y, div_le_div_of_le hc)
lemma max_div_div_right {c : α} (hc : 0 ≤ c) (a b : α) : max (a / c) (b / c) = (max a b) / c :=
eq.symm $ monotone.map_max (λ x y, div_le_div_of_le hc)
lemma min_div_div_right_of_nonpos {c : α} (hc : c ≤ 0) (a b : α) :
min (a / c) (b / c) = (max a b) / c :=
eq.symm $ @monotone.map_max α (order_dual α) _ _ _ _ _ (λ x y, div_le_div_of_nonpos_of_le hc)
lemma max_div_div_right_of_nonpos {c : α} (hc : c ≤ 0) (a b : α) :
max (a / c) (b / c) = (min a b) / c :=
eq.symm $ @monotone.map_min α (order_dual α) _ _ _ _ _ (λ x y, div_le_div_of_nonpos_of_le hc)
lemma abs_div (a b : α) : abs (a / b) = abs a / abs b :=
(abs_hom : α →* α).map_div abs_zero a b
lemma abs_one_div (a : α) : abs (1 / a) = 1 / abs a :=
by rw [abs_div, abs_of_nonneg (zero_le_one : 1 ≥ (0 : α))]
lemma abs_inv (a : α) : abs a⁻¹ = (abs a)⁻¹ :=
by rw [inv_eq_one_div, abs_one_div, inv_eq_one_div]
end linear_ordered_field
|
abb0e2acb01915807f853666aa714417d94d0a1d | 8cb37a089cdb4af3af9d8bf1002b417e407a8e9e | /library/init/meta/environment.lean | 800536aa50978410e4c6bab44c07c34c0b1276ff | [
"Apache-2.0"
] | permissive | kbuzzard/lean | ae3c3db4bb462d750dbf7419b28bafb3ec983ef7 | ed1788fd674bb8991acffc8fca585ec746711928 | refs/heads/master | 1,620,983,366,617 | 1,618,937,600,000 | 1,618,937,600,000 | 359,886,396 | 1 | 0 | Apache-2.0 | 1,618,936,987,000 | 1,618,936,987,000 | null | UTF-8 | Lean | false | false | 11,033 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.meta.declaration init.meta.exceptional init.data.option.basic
import init.meta.rb_map
/-- An __environment__ contains all of the declarations and notation that have been defined so far. -/
meta constant environment : Type
namespace environment
/--
Consider a type `ψ` which is an inductive datatype using a single constructor `mk (a : α) (b : β) : ψ`.
Lean will automatically make two projection functions `a : ψ → α`, `b : ψ → β`.
Lean tags these declarations as __projections__.
This helps the simplifier / rewriter not have to expand projectors.
Eg `a (mk x y)` will automatically reduce to `x`.
If you `extend` a structure, all of the projections on the parent will also be created for the child.
Projections are also treated differently in the VM for efficiency.
Note that projections have nothing to do with the dot `mylist.map` syntax.
You can find out if a declaration is a projection using `environment.is_projection` which returns `projection_info`.
Data for a projection declaration:
- `cname` is the name of the constructor associated with the projection.
- `nparams` is the number of constructor parameters. Eg `and.intro` has two type parameters.
- `idx` is the parameter being projected by this projection.
- `is_class` is tt iff this is a typeclass projection.
### Examples:
- `and.right` is a projection with ``{cname := `and.intro, nparams := 2, idx := 1, is_class := ff}``
- `ordered_ring.neg` is a projection with ``{cname := `ordered_ring.mk, nparams := 1, idx := 5, is_class := tt}``.
-/
structure projection_info :=
(cname : name)
(nparams : nat)
(idx : nat)
(is_class : bool)
/-- A marking on the binders of structures and inductives indicating
how this constructor should mark its parameters.
inductive foo
| one {} : foo -> foo -- relaxed_implicit
| two ( ) : foo -> foo -- explicit
| two [] : foo -> foo -- implicit
| three : foo -> foo -- relaxed implicit (default)
-/
inductive implicit_infer_kind | implicit | relaxed_implicit | none
instance implicit_infer_kind.inhabited : inhabited implicit_infer_kind := ⟨implicit_infer_kind.implicit⟩
/-- One introduction rule in an inductive declaration -/
meta structure intro_rule :=
(constr : name)
(type : expr)
(infer : implicit_infer_kind := implicit_infer_kind.implicit)
/-- Create a standard environment using the given trust level -/
meta constant mk_std : nat → environment
/-- Return the trust level of the given environment -/
meta constant trust_lvl : environment → nat
/-- Add a new declaration to the environment -/
meta constant add : environment → declaration → exceptional environment
/-- make declaration `n` protected -/
meta constant mk_protected : environment → name → environment
/-- add declaration `d` and make it protected -/
meta def add_protected (env : environment) (d : declaration) : exceptional environment := do
env ← env.add d,
pure $ env.mk_protected d.to_name
/-- check if `n` is the name of a protected declaration -/
meta constant is_protected : environment → name → bool
/-- Retrieve a declaration from the environment -/
meta constant get : environment → name → exceptional declaration
meta def contains (env : environment) (d : name) : bool :=
match env.get d with
| exceptional.success _ := tt
| exceptional.exception _ := ff
end
meta constant add_defn_eqns (env : environment) (opt : options)
(lp_params : list name) (params : list expr) (sig : expr)
(eqns : list (list (expr ff) × expr)) (is_meta : bool) : exceptional environment
/-- Register the given name as a namespace, making it available to the `open` command -/
meta constant add_namespace : environment → name → environment
/-- Mark a namespace as open -/
meta constant mark_namespace_as_open : environment -> name -> environment
/-- Modify the environment as if `open %%name` had been parsed -/
meta constant execute_open : environment -> name -> environment
/-- Retrieve all registered namespaces -/
meta constant get_namespaces : environment -> list name
/-- Return tt iff the given name is a namespace -/
meta constant is_namespace : environment → name → bool
/-- Add a new inductive datatype to the environment
name, universe parameters, number of parameters, type, constructors (name and type), is_meta -/
meta constant add_inductive (env : environment)
(n : name) (levels : list name) (num_params : nat) (type : expr)
(intros : list (name × expr)) (is_meta : bool) : exceptional environment
/-- Add a new general inductive declaration to the environment.
This has the same effect as a `inductive` in the file, including generating
all the auxiliary definitions, as well as triggering mutual/nested inductive
compilation, by contrast to `environment.add_inductive` which only adds the
core axioms supported by the kernel.
The `inds` argument should be a list of inductives in the mutual family.
The first argument is a pair of the name of the type being constructed
and the type of this inductive family (not including the params).
The second argument is a list of intro rules, specified by a name, an
`implicit_infer_kind` giving the implicitness of the params for this constructor,
and an expression with the type of the constructor (not including the params).
-/
meta constant add_ginductive (env : environment) (opt : options)
(levels : list name) (params : list expr)
(inds : list ((name × expr) × list intro_rule))
(is_meta : bool) : exceptional environment
/-- Return tt iff the given name is an inductive datatype -/
meta constant is_inductive : environment → name → bool
/-- Return tt iff the given name is a constructor -/
meta constant is_constructor : environment → name → bool
/-- Return tt iff the given name is a recursor -/
meta constant is_recursor : environment → name → bool
/-- Return tt iff the given name is a recursive inductive datatype -/
meta constant is_recursive : environment → name → bool
/-- Return the name of the inductive datatype of the given constructor. -/
meta constant inductive_type_of : environment → name → option name
/-- Return the constructors of the inductive datatype with the given name -/
meta constant constructors_of : environment → name → list name
/-- Return the recursor of the given inductive datatype -/
meta constant recursor_of : environment → name → option name
/-- Return the number of parameters of the inductive datatype -/
meta constant inductive_num_params : environment → name → nat
/-- Return the number of indices of the inductive datatype -/
meta constant inductive_num_indices : environment → name → nat
/-- Return tt iff the inductive datatype recursor supports dependent elimination -/
meta constant inductive_dep_elim : environment → name → bool
/-- Functionally equivalent to `is_inductive`.
Technically, this works by checking if the name is in the ginductive environment
extension which is outside the kernel, whereas `is_inductive` works by looking at the kernel extension.
But there are no `is_inductive`s which are not `is_ginductive`.
-/
meta constant is_ginductive : environment → name → bool
/-- See the docstring for `projection_info`. -/
meta constant is_projection : environment → name → option projection_info
/-- Fold over declarations in the environment. -/
meta constant fold {α :Type} : environment → α → (declaration → α → α) → α
/-- `relation_info env n` returns some value if n is marked as a relation in the given environment.
the tuple contains: total number of arguments of the relation, lhs position and rhs position. -/
meta constant relation_info : environment → name → option (nat × nat × nat)
/-- `refl_for env R` returns the name of the reflexivity theorem for the relation R -/
meta constant refl_for : environment → name → option name
/-- `symm_for env R` returns the name of the symmetry theorem for the relation R -/
meta constant symm_for : environment → name → option name
/-- `trans_for env R` returns the name of the transitivity theorem for the relation R -/
meta constant trans_for : environment → name → option name
/-- `decl_olean env d` returns the name of the .olean file where d was defined.
The result is none if d was not defined in an imported file. -/
meta constant decl_olean : environment → name → option string
/-- `decl_pos env d` returns the source location of d if available. -/
meta constant decl_pos : environment → name → option pos
/-- Return the fields of the structure with the given name, or `none` if it is not a structure -/
meta constant structure_fields : environment → name → option (list name)
/-- `get_class_attribute_symbols env attr_name` return symbols
occurring in instances of type classes tagged with the attribute `attr_name`.
Example: [algebra] -/
meta constant get_class_attribute_symbols : environment → name → name_set
/-- The fingerprint of the environment is a hash formed from all of the declarations in the environment. -/
meta constant fingerprint : environment → nat
/-- Gets the equation lemmas for the declaration `n`. -/
meta constant get_eqn_lemmas_for (env : environment) (n : name) : list name
/-- Gets the equation lemmas for the declaration `n`, including lemmas for match statements, etc. -/
meta constant get_ext_eqn_lemmas_for (env : environment) (n : name) : list name
/--
Adds the equation lemma `n`.
It is added for the declaration `t.pi_codomain.get_app_fn.const_name` where `t` is the type of the equation lemma.
-/
meta constant add_eqn_lemma (env : environment) (n : name) : environment
open expr
meta constant unfold_untrusted_macros : environment → expr → expr
meta constant unfold_all_macros : environment → expr → expr
meta def is_constructor_app (env : environment) (e : expr) : bool :=
is_constant (get_app_fn e) && is_constructor env (const_name (get_app_fn e))
meta def is_refl_app (env : environment) (e : expr) : option (name × expr × expr) :=
match (refl_for env (const_name (get_app_fn e))) with
| (some n) :=
if get_app_num_args e ≥ 2
then some (n, app_arg (app_fn e), app_arg e)
else none
| none := none
end
/-- Return true if 'n' has been declared in the current file -/
meta def in_current_file (env : environment) (n : name) : bool :=
(env.decl_olean n).is_none && env.contains n && (n ∉ [``quot, ``quot.mk, ``quot.lift, ``quot.ind])
meta def is_definition (env : environment) (n : name) : bool :=
match env.get n with
| exceptional.success (declaration.defn _ _ _ _ _ _) := tt
| _ := ff
end
end environment
meta instance : has_repr environment :=
⟨λ e, "[environment]"⟩
meta instance : inhabited environment :=
⟨environment.mk_std 0⟩
|
845bfc3c0356793cbcdfc44617f12c377f3324bf | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/topology/algebra/module.lean | ae7923f4395f119adb0b31b919a822b5db857879 | [
"Apache-2.0"
] | permissive | agjftucker/mathlib | d634cd0d5256b6325e3c55bb7fb2403548371707 | 87fe50de17b00af533f72a102d0adefe4a2285e8 | refs/heads/master | 1,625,378,131,941 | 1,599,166,526,000 | 1,599,166,526,000 | 160,748,509 | 0 | 0 | Apache-2.0 | 1,544,141,789,000 | 1,544,141,789,000 | null | UTF-8 | Lean | false | false | 41,765 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jan-David Salchow, Sébastien Gouëzel, Jean Lo, Yury Kudryashov
-/
import topology.algebra.ring
import topology.uniform_space.uniform_embedding
import ring_theory.algebra
import linear_algebra.projection
/-!
# Theory of topological modules and continuous linear maps.
We define classes `topological_semimodule`, `topological_module` and `topological_vector_spaces`,
as extensions of the corresponding algebraic classes where the algebraic operations are continuous.
We also define continuous linear maps, as linear maps between topological modules which are
continuous. The set of continuous linear maps between the topological `R`-modules `M` and `M₂` is
denoted by `M →L[R] M₂`.
Continuous linear equivalences are denoted by `M ≃L[R] M₂`.
## Implementation notes
Topological vector spaces are defined as an `abbreviation` for topological modules,
if the base ring is a field. This has as advantage that topological vector spaces are completely
transparent for type class inference, which means that all instances for topological modules
are immediately picked up for vector spaces as well.
A cosmetic disadvantage is that one can not extend topological vector spaces.
The solution is to extend `topological_module` instead.
-/
open filter
open_locale topological_space big_operators
universes u v w u'
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A topological semimodule, over a semiring which is also a topological space, is a
semimodule in which scalar multiplication is continuous. In applications, R will be a topological
semiring and M a topological additive semigroup, but this is not needed for the definition -/
class topological_semimodule (R : Type u) (M : Type v)
[semiring R] [topological_space R]
[topological_space M] [add_comm_monoid M]
[semimodule R M] : Prop :=
(continuous_smul : continuous (λp : R × M, p.1 • p.2))
end prio
section
variables {R : Type u} {M : Type v}
[semiring R] [topological_space R]
[topological_space M] [add_comm_monoid M]
[semimodule R M] [topological_semimodule R M]
lemma continuous_smul : continuous (λp:R×M, p.1 • p.2) :=
topological_semimodule.continuous_smul
@[continuity]
lemma continuous.smul {α : Type*} [topological_space α] {f : α → R} {g : α → M}
(hf : continuous f) (hg : continuous g) : continuous (λp, f p • g p) :=
continuous_smul.comp (hf.prod_mk hg)
lemma tendsto_smul {c : R} {x : M} : tendsto (λp:R×M, p.fst • p.snd) (𝓝 (c, x)) (𝓝 (c • x)) :=
continuous_smul.tendsto _
lemma filter.tendsto.smul {α : Type*} {l : filter α} {f : α → R} {g : α → M} {c : R} {x : M}
(hf : tendsto f l (𝓝 c)) (hg : tendsto g l (𝓝 x)) : tendsto (λ a, f a • g a) l (𝓝 (c • x)) :=
tendsto_smul.comp (hf.prod_mk_nhds hg)
end
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A topological module, over a ring which is also a topological space, is a module in which
scalar multiplication is continuous. In applications, `R` will be a topological ring and `M` a
topological additive group, but this is not needed for the definition -/
class topological_module (R : Type u) (M : Type v)
[ring R] [topological_space R]
[topological_space M] [add_comm_group M]
[module R M]
extends topological_semimodule R M : Prop
/-- A topological vector space is a topological module over a field. -/
abbreviation topological_vector_space (R : Type u) (M : Type v)
[field R] [topological_space R]
[topological_space M] [add_comm_group M] [module R M] :=
topological_module R M
end prio
section
variables {R : Type*} {M : Type*}
[ring R] [topological_space R]
[topological_space M] [add_comm_group M]
[module R M] [topological_module R M]
/-- Scalar multiplication by a unit is a homeomorphism from a
topological module onto itself. -/
protected def homeomorph.smul_of_unit (a : units R) : M ≃ₜ M :=
{ to_fun := λ x, (a : R) • x,
inv_fun := λ x, ((a⁻¹ : units R) : R) • x,
right_inv := λ x, calc (a : R) • ((a⁻¹ : units R) : R) • x = x :
by rw [smul_smul, units.mul_inv, one_smul],
left_inv := λ x, calc ((a⁻¹ : units R) : R) • (a : R) • x = x :
by rw [smul_smul, units.inv_mul, one_smul],
continuous_to_fun := continuous_const.smul continuous_id,
continuous_inv_fun := continuous_const.smul continuous_id }
lemma is_open_map_smul_of_unit (a : units R) : is_open_map (λ (x : M), (a : R) • x) :=
(homeomorph.smul_of_unit a).is_open_map
lemma is_closed_map_smul_of_unit (a : units R) : is_closed_map (λ (x : M), (a : R) • x) :=
(homeomorph.smul_of_unit a).is_closed_map
/-- If `M` is a topological module over `R` and `0` is a limit of invertible elements of `R`, then
`⊤` is the only submodule of `M` with a nonempty interior.
This is the case, e.g., if `R` is a nondiscrete normed field. -/
lemma submodule.eq_top_of_nonempty_interior' [has_continuous_add M]
[ne_bot (𝓝[{x : R | is_unit x}] 0)]
(s : submodule R M) (hs : (interior (s:set M)).nonempty) :
s = ⊤ :=
begin
rcases hs with ⟨y, hy⟩,
refine (submodule.eq_top_iff'.2 $ λ x, _),
rw [mem_interior_iff_mem_nhds] at hy,
have : tendsto (λ c:R, y + c • x) (𝓝[{x : R | is_unit x}] 0) (𝓝 (y + (0:R) • x)),
from tendsto_const_nhds.add ((tendsto_nhds_within_of_tendsto_nhds tendsto_id).smul
tendsto_const_nhds),
rw [zero_smul, add_zero] at this,
rcases nonempty_of_mem_sets (inter_mem_sets (mem_map.1 (this hy)) self_mem_nhds_within)
with ⟨_, hu, u, rfl⟩,
have hy' : y ∈ ↑s := mem_of_nhds hy,
exact (s.smul_mem_iff' _).1 ((s.add_mem_iff_right hy').1 hu)
end
end
section
variables {R : Type*} {M : Type*} {a : R}
[field R] [topological_space R]
[topological_space M] [add_comm_group M]
[vector_space R M] [topological_vector_space R M]
/-- Scalar multiplication by a non-zero field element is a
homeomorphism from a topological vector space onto itself. -/
protected def homeomorph.smul_of_ne_zero (ha : a ≠ 0) : M ≃ₜ M :=
{.. homeomorph.smul_of_unit (units.mk0 a ha)}
lemma is_open_map_smul_of_ne_zero (ha : a ≠ 0) : is_open_map (λ (x : M), a • x) :=
(homeomorph.smul_of_ne_zero ha).is_open_map
lemma is_closed_map_smul_of_ne_zero (ha : a ≠ 0) : is_closed_map (λ (x : M), a • x) :=
(homeomorph.smul_of_ne_zero ha).is_closed_map
end
/-- Continuous linear maps between modules. We only put the type classes that are necessary for the
definition, although in applications `M` and `M₂` will be topological modules over the topological
ring `R`. -/
structure continuous_linear_map
(R : Type*) [semiring R]
(M : Type*) [topological_space M] [add_comm_monoid M]
(M₂ : Type*) [topological_space M₂] [add_comm_monoid M₂]
[semimodule R M] [semimodule R M₂]
extends linear_map R M M₂ :=
(cont : continuous to_fun . tactic.interactive.continuity')
notation M ` →L[`:25 R `] ` M₂ := continuous_linear_map R M M₂
/-- Continuous linear equivalences between modules. We only put the type classes that are necessary
for the definition, although in applications `M` and `M₂` will be topological modules over the
topological ring `R`. -/
@[nolint has_inhabited_instance]
structure continuous_linear_equiv
(R : Type*) [semiring R]
(M : Type*) [topological_space M] [add_comm_monoid M]
(M₂ : Type*) [topological_space M₂] [add_comm_monoid M₂]
[semimodule R M] [semimodule R M₂]
extends linear_equiv R M M₂ :=
(continuous_to_fun : continuous to_fun . tactic.interactive.continuity')
(continuous_inv_fun : continuous inv_fun . tactic.interactive.continuity')
notation M ` ≃L[`:50 R `] ` M₂ := continuous_linear_equiv R M M₂
namespace continuous_linear_map
section semiring
/- Properties that hold for non-necessarily commutative semirings. -/
variables
{R : Type*} [semiring R]
{M : Type*} [topological_space M] [add_comm_monoid M]
{M₂ : Type*} [topological_space M₂] [add_comm_monoid M₂]
{M₃ : Type*} [topological_space M₃] [add_comm_monoid M₃]
{M₄ : Type*} [topological_space M₄] [add_comm_monoid M₄]
[semimodule R M] [semimodule R M₂] [semimodule R M₃] [semimodule R M₄]
/-- Coerce continuous linear maps to linear maps. -/
instance : has_coe (M →L[R] M₂) (M →ₗ[R] M₂) := ⟨to_linear_map⟩
/-- Coerce continuous linear maps to functions. -/
-- see Note [function coercion]
instance to_fun : has_coe_to_fun $ M →L[R] M₂ := ⟨λ _, M → M₂, λ f, f⟩
@[simp] lemma coe_mk (f : M →ₗ[R] M₂) (h) : (mk f h : M →ₗ[R] M₂) = f := rfl
@[simp] lemma coe_mk' (f : M →ₗ[R] M₂) (h) : (mk f h : M → M₂) = f := rfl
@[continuity]
protected lemma continuous (f : M →L[R] M₂) : continuous f := f.2
theorem coe_injective : function.injective (coe : (M →L[R] M₂) → (M →ₗ[R] M₂)) :=
by { intros f g H, cases f, cases g, congr' 1, exact H }
theorem coe_inj ⦃f g : M →L[R] M₂⦄ (H : (f : M → M₂) = g) : f = g :=
coe_injective $ linear_map.coe_inj H
@[ext] theorem ext {f g : M →L[R] M₂} (h : ∀ x, f x = g x) : f = g :=
coe_inj $ funext h
theorem ext_iff {f g : M →L[R] M₂} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, by rw h, by ext⟩
variables (c : R) (f g : M →L[R] M₂) (h : M₂ →L[R] M₃) (x y z : M)
-- make some straightforward lemmas available to `simp`.
@[simp] lemma map_zero : f (0 : M) = 0 := (to_linear_map _).map_zero
@[simp] lemma map_add : f (x + y) = f x + f y := (to_linear_map _).map_add _ _
@[simp] lemma map_smul : f (c • x) = c • f x := (to_linear_map _).map_smul _ _
@[simp, norm_cast] lemma coe_coe : ((f : M →ₗ[R] M₂) : (M → M₂)) = (f : M → M₂) := rfl
/-- The continuous map that is constantly zero. -/
instance: has_zero (M →L[R] M₂) := ⟨⟨0, continuous_const⟩⟩
instance : inhabited (M →L[R] M₂) := ⟨0⟩
@[simp] lemma zero_apply : (0 : M →L[R] M₂) x = 0 := rfl
@[simp, norm_cast] lemma coe_zero : ((0 : M →L[R] M₂) : M →ₗ[R] M₂) = 0 := rfl
/- no simp attribute on the next line as simp does not always simplify `0 x` to `0`
when `0` is the zero function, while it does for the zero continuous linear map,
and this is the most important property we care about. -/
@[norm_cast] lemma coe_zero' : ((0 : M →L[R] M₂) : M → M₂) = 0 := rfl
section
variables (R M)
/-- the identity map as a continuous linear map. -/
def id : M →L[R] M :=
⟨linear_map.id, continuous_id⟩
end
instance : has_one (M →L[R] M) := ⟨id R M⟩
lemma id_apply : id R M x = x := rfl
@[simp, norm_cast] lemma coe_id : (id R M : M →ₗ[R] M) = linear_map.id := rfl
@[simp, norm_cast] lemma coe_id' : (id R M : M → M) = _root_.id := rfl
@[simp] lemma one_apply : (1 : M →L[R] M) x = x := rfl
section add
variables [has_continuous_add M₂]
instance : has_add (M →L[R] M₂) :=
⟨λ f g, ⟨f + g, f.2.add g.2⟩⟩
@[simp] lemma add_apply : (f + g) x = f x + g x := rfl
@[simp, norm_cast] lemma coe_add : (((f + g) : M →L[R] M₂) : M →ₗ[R] M₂) = (f : M →ₗ[R] M₂) + g := rfl
@[norm_cast] lemma coe_add' : (((f + g) : M →L[R] M₂) : M → M₂) = (f : M → M₂) + g := rfl
instance : add_comm_monoid (M →L[R] M₂) :=
by { refine {zero := 0, add := (+), ..}; intros; ext;
apply_rules [zero_add, add_assoc, add_zero, add_left_neg, add_comm] }
lemma sum_apply {ι : Type*} (t : finset ι) (f : ι → M →L[R] M₂) (b : M) :
(∑ d in t, f d) b = ∑ d in t, f d b :=
begin
haveI : is_add_monoid_hom (λ (g : M →L[R] M₂), g b) :=
{ map_add := λ f g, continuous_linear_map.add_apply f g b, map_zero := by simp },
exact (finset.sum_hom t (λ g : M →L[R] M₂, g b)).symm
end
end add
/-- Composition of bounded linear maps. -/
def comp (g : M₂ →L[R] M₃) (f : M →L[R] M₂) : M →L[R] M₃ :=
⟨linear_map.comp g.to_linear_map f.to_linear_map, g.2.comp f.2⟩
@[simp, norm_cast] lemma coe_comp : ((h.comp f) : (M →ₗ[R] M₃)) = (h : M₂ →ₗ[R] M₃).comp f := rfl
@[simp, norm_cast] lemma coe_comp' : ((h.comp f) : (M → M₃)) = (h : M₂ → M₃) ∘ f := rfl
@[simp] theorem comp_id : f.comp (id R M) = f :=
ext $ λ x, rfl
@[simp] theorem id_comp : (id R M₂).comp f = f :=
ext $ λ x, rfl
@[simp] theorem comp_zero : f.comp (0 : M₃ →L[R] M) = 0 :=
by { ext, simp }
@[simp] theorem zero_comp : (0 : M₂ →L[R] M₃).comp f = 0 :=
by { ext, simp }
@[simp] lemma comp_add [has_continuous_add M₂] [has_continuous_add M₃]
(g : M₂ →L[R] M₃) (f₁ f₂ : M →L[R] M₂) :
g.comp (f₁ + f₂) = g.comp f₁ + g.comp f₂ :=
by { ext, simp }
@[simp] lemma add_comp [has_continuous_add M₃]
(g₁ g₂ : M₂ →L[R] M₃) (f : M →L[R] M₂) :
(g₁ + g₂).comp f = g₁.comp f + g₂.comp f :=
by { ext, simp }
theorem comp_assoc (h : M₃ →L[R] M₄) (g : M₂ →L[R] M₃) (f : M →L[R] M₂) :
(h.comp g).comp f = h.comp (g.comp f) :=
rfl
instance : has_mul (M →L[R] M) := ⟨comp⟩
lemma mul_def (f g : M →L[R] M) : f * g = f.comp g := rfl
@[simp] lemma coe_mul (f g : M →L[R] M) : ⇑(f * g) = f ∘ g := rfl
lemma mul_apply (f g : M →L[R] M) (x : M) : (f * g) x = f (g x) := rfl
/-- The cartesian product of two bounded linear maps, as a bounded linear map. -/
protected def prod (f₁ : M →L[R] M₂) (f₂ : M →L[R] M₃) : M →L[R] (M₂ × M₃) :=
{ cont := f₁.2.prod_mk f₂.2,
..f₁.to_linear_map.prod f₂.to_linear_map }
@[simp, norm_cast] lemma coe_prod (f₁ : M →L[R] M₂) (f₂ : M →L[R] M₃) :
(f₁.prod f₂ : M →ₗ[R] M₂ × M₃) = linear_map.prod f₁ f₂ :=
rfl
@[simp, norm_cast] lemma prod_apply (f₁ : M →L[R] M₂) (f₂ : M →L[R] M₃) (x : M) :
f₁.prod f₂ x = (f₁ x, f₂ x) :=
rfl
/-- Kernel of a continuous linear map. -/
def ker (f : M →L[R] M₂) : submodule R M := (f : M →ₗ[R] M₂).ker
@[norm_cast] lemma ker_coe : (f : M →ₗ[R] M₂).ker = f.ker := rfl
@[simp] lemma mem_ker {f : M →L[R] M₂} {x} : x ∈ f.ker ↔ f x = 0 := linear_map.mem_ker
lemma is_closed_ker [t1_space M₂] : is_closed (f.ker : set M) :=
continuous_iff_is_closed.1 f.cont _ is_closed_singleton
@[simp] lemma apply_ker (x : f.ker) : f x = 0 := mem_ker.1 x.2
lemma is_complete_ker {M' : Type*} [uniform_space M'] [complete_space M'] [add_comm_monoid M']
[semimodule R M'] [t1_space M₂] (f : M' →L[R] M₂) :
is_complete (f.ker : set M') :=
f.is_closed_ker.is_complete
instance complete_space_ker {M' : Type*} [uniform_space M'] [complete_space M'] [add_comm_monoid M']
[semimodule R M'] [t1_space M₂] (f : M' →L[R] M₂) :
complete_space f.ker :=
f.is_closed_ker.complete_space_coe
@[simp] lemma ker_prod (f : M →L[R] M₂) (g : M →L[R] M₃) :
ker (f.prod g) = ker f ⊓ ker g :=
linear_map.ker_prod f g
/-- Range of a continuous linear map. -/
def range (f : M →L[R] M₂) : submodule R M₂ := (f : M →ₗ[R] M₂).range
lemma range_coe : (f.range : set M₂) = set.range f := linear_map.range_coe _
lemma mem_range {f : M →L[R] M₂} {y} : y ∈ f.range ↔ ∃ x, f x = y := linear_map.mem_range
lemma range_prod_le (f : M →L[R] M₂) (g : M →L[R] M₃) :
range (f.prod g) ≤ (range f).prod (range g) :=
(f : M →ₗ[R] M₂).range_prod_le g
/-- Restrict codomain of a continuous linear map. -/
def cod_restrict (f : M →L[R] M₂) (p : submodule R M₂) (h : ∀ x, f x ∈ p) :
M →L[R] p :=
{ cont := continuous_subtype_mk h f.continuous,
to_linear_map := (f : M →ₗ[R] M₂).cod_restrict p h}
@[norm_cast] lemma coe_cod_restrict (f : M →L[R] M₂) (p : submodule R M₂) (h : ∀ x, f x ∈ p) :
(f.cod_restrict p h : M →ₗ[R] p) = (f : M →ₗ[R] M₂).cod_restrict p h :=
rfl
@[simp] lemma coe_cod_restrict_apply (f : M →L[R] M₂) (p : submodule R M₂) (h : ∀ x, f x ∈ p) (x) :
(f.cod_restrict p h x : M₂) = f x :=
rfl
@[simp] lemma ker_cod_restrict (f : M →L[R] M₂) (p : submodule R M₂) (h : ∀ x, f x ∈ p) :
ker (f.cod_restrict p h) = ker f :=
(f : M →ₗ[R] M₂).ker_cod_restrict p h
/-- Embedding of a submodule into the ambient space as a continuous linear map. -/
def subtype_val (p : submodule R M) : p →L[R] M :=
{ cont := continuous_subtype_val,
to_linear_map := p.subtype }
@[simp, norm_cast] lemma coe_subtype_val (p : submodule R M) :
(subtype_val p : p →ₗ[R] M) = p.subtype :=
rfl
@[simp, norm_cast] lemma subtype_val_apply (p : submodule R M) (x : p) :
(subtype_val p : p → M) x = x :=
rfl
variables (R M M₂)
/-- `prod.fst` as a `continuous_linear_map`. -/
def fst : M × M₂ →L[R] M :=
{ cont := continuous_fst, to_linear_map := linear_map.fst R M M₂ }
/-- `prod.snd` as a `continuous_linear_map`. -/
def snd : M × M₂ →L[R] M₂ :=
{ cont := continuous_snd, to_linear_map := linear_map.snd R M M₂ }
variables {R M M₂}
@[simp, norm_cast] lemma coe_fst : (fst R M M₂ : M × M₂ →ₗ[R] M) = linear_map.fst R M M₂ := rfl
@[simp, norm_cast] lemma coe_fst' : (fst R M M₂ : M × M₂ → M) = prod.fst := rfl
@[simp, norm_cast] lemma coe_snd : (snd R M M₂ : M × M₂ →ₗ[R] M₂) = linear_map.snd R M M₂ := rfl
@[simp, norm_cast] lemma coe_snd' : (snd R M M₂ : M × M₂ → M₂) = prod.snd := rfl
@[simp] lemma fst_prod_snd : (fst R M M₂).prod (snd R M M₂) = id R (M × M₂) := ext $ λ ⟨x, y⟩, rfl
/-- `prod.map` of two continuous linear maps. -/
def prod_map (f₁ : M →L[R] M₂) (f₂ : M₃ →L[R] M₄) : (M × M₃) →L[R] (M₂ × M₄) :=
(f₁.comp (fst R M M₃)).prod (f₂.comp (snd R M M₃))
@[simp, norm_cast] lemma coe_prod_map (f₁ : M →L[R] M₂) (f₂ : M₃ →L[R] M₄) :
(f₁.prod_map f₂ : (M × M₃) →ₗ[R] (M₂ × M₄)) = ((f₁ : M →ₗ[R] M₂).prod_map (f₂ : M₃ →ₗ[R] M₄)) :=
rfl
@[simp, norm_cast] lemma coe_prod_map' (f₁ : M →L[R] M₂) (f₂ : M₃ →L[R] M₄) :
⇑(f₁.prod_map f₂) = prod.map f₁ f₂ :=
rfl
/-- The continuous linear map given by `(x, y) ↦ f₁ x + f₂ y`. -/
def coprod [has_continuous_add M₃] (f₁ : M →L[R] M₃) (f₂ : M₂ →L[R] M₃) :
(M × M₂) →L[R] M₃ :=
⟨linear_map.coprod f₁ f₂, (f₁.cont.comp continuous_fst).add (f₂.cont.comp continuous_snd)⟩
@[norm_cast, simp] lemma coe_coprod [has_continuous_add M₃]
(f₁ : M →L[R] M₃) (f₂ : M₂ →L[R] M₃) :
(f₁.coprod f₂ : (M × M₂) →ₗ[R] M₃) = linear_map.coprod f₁ f₂ :=
rfl
@[simp] lemma coprod_apply [has_continuous_add M₃] (f₁ : M →L[R] M₃) (f₂ : M₂ →L[R] M₃) (x) :
f₁.coprod f₂ x = f₁ x.1 + f₂ x.2 := rfl
variables [topological_space R] [topological_semimodule R M₂]
/-- The linear map `λ x, c x • f`. Associates to a scalar-valued linear map and an element of
`M₂` the `M₂`-valued linear map obtained by multiplying the two (a.k.a. tensoring by `M₂`) -/
def smul_right (c : M →L[R] R) (f : M₂) : M →L[R] M₂ :=
{ cont := c.2.smul continuous_const,
..c.to_linear_map.smul_right f }
@[simp]
lemma smul_right_apply {c : M →L[R] R} {f : M₂} {x : M} :
(smul_right c f : M → M₂) x = (c : M → R) x • f :=
rfl
@[simp]
lemma smul_right_one_one (c : R →L[R] M₂) : smul_right 1 ((c : R → M₂) 1) = c :=
by ext; simp [-continuous_linear_map.map_smul, (continuous_linear_map.map_smul _ _ _).symm]
@[simp]
lemma smul_right_one_eq_iff {f f' : M₂} :
smul_right (1 : R →L[R] R) f = smul_right 1 f' ↔ f = f' :=
⟨λ h, have (smul_right (1 : R →L[R] R) f : R → M₂) 1 = (smul_right (1 : R →L[R] R) f' : R → M₂) 1,
by rw h,
by simp at this; assumption,
by cc⟩
lemma smul_right_comp [topological_semimodule R R] {x : M₂} {c : R} :
(smul_right 1 x : R →L[R] M₂).comp (smul_right 1 c : R →L[R] R) = smul_right 1 (c • x) :=
by { ext, simp [mul_smul] }
end semiring
section pi
variables
{R : Type*} [semiring R]
{M : Type*} [topological_space M] [add_comm_monoid M] [semimodule R M]
{M₂ : Type*} [topological_space M₂] [add_comm_monoid M₂] [semimodule R M₂]
{ι : Type*} {φ : ι → Type*} [∀i, topological_space (φ i)] [∀i, add_comm_monoid (φ i)] [∀i, semimodule R (φ i)]
/-- `pi` construction for continuous linear functions. From a family of continuous linear functions
it produces a continuous linear function into a family of topological modules. -/
def pi (f : Πi, M →L[R] φ i) : M →L[R] (Πi, φ i) :=
⟨linear_map.pi (λ i, (f i : M →ₗ[R] φ i)),
continuous_pi (λ i, (f i).continuous)⟩
@[simp] lemma pi_apply (f : Πi, M →L[R] φ i) (c : M) (i : ι) :
pi f c i = f i c := rfl
lemma pi_eq_zero (f : Πi, M →L[R] φ i) : pi f = 0 ↔ (∀i, f i = 0) :=
by simp only [ext_iff, pi_apply, function.funext_iff]; exact ⟨λh a b, h b a, λh a b, h b a⟩
lemma pi_zero : pi (λi, 0 : Πi, M →L[R] φ i) = 0 := by ext; refl
lemma pi_comp (f : Πi, M →L[R] φ i) (g : M₂ →L[R] M) : (pi f).comp g = pi (λi, (f i).comp g) := rfl
/-- The projections from a family of topological modules are continuous linear maps. -/
def proj (i : ι) : (Πi, φ i) →L[R] φ i :=
⟨linear_map.proj i, continuous_apply _⟩
@[simp] lemma proj_apply (i : ι) (b : Πi, φ i) : (proj i : (Πi, φ i) →L[R] φ i) b = b i := rfl
lemma proj_pi (f : Πi, M₂ →L[R] φ i) (i : ι) : (proj i).comp (pi f) = f i :=
ext $ assume c, rfl
lemma infi_ker_proj : (⨅i, ker (proj i) : submodule R (Πi, φ i)) = ⊥ :=
linear_map.infi_ker_proj
variables (R φ)
/-- If `I` and `J` are complementary index sets, the product of the kernels of the `J`th projections of
`φ` is linearly equivalent to the product over `I`. -/
def infi_ker_proj_equiv {I J : set ι} [decidable_pred (λi, i ∈ I)]
(hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) :
(⨅i ∈ J, ker (proj i) : submodule R (Πi, φ i)) ≃L[R] (Πi:I, φ i) :=
⟨ linear_map.infi_ker_proj_equiv R φ hd hu,
continuous_pi (λ i, begin
have := @continuous_subtype_coe _ _ (λ x, x ∈ (⨅i ∈ J, ker (proj i) : submodule R (Πi, φ i))),
have := continuous.comp (by exact continuous_apply i) this,
exact this
end),
continuous_subtype_mk _ (continuous_pi (λ i, begin
dsimp, split_ifs; [apply continuous_apply, exact continuous_const]
end)) ⟩
end pi
section ring
variables
{R : Type*} [ring R]
{M : Type*} [topological_space M] [add_comm_group M]
{M₂ : Type*} [topological_space M₂] [add_comm_group M₂]
{M₃ : Type*} [topological_space M₃] [add_comm_group M₃]
{M₄ : Type*} [topological_space M₄] [add_comm_group M₄]
[semimodule R M] [semimodule R M₂] [semimodule R M₃] [semimodule R M₄]
variables (c : R) (f g : M →L[R] M₂) (h : M₂ →L[R] M₃) (x y z : M)
@[simp] lemma map_neg : f (-x) = - (f x) := (to_linear_map _).map_neg _
@[simp] lemma map_sub : f (x - y) = f x - f y := (to_linear_map _).map_sub _ _
@[simp] lemma sub_apply' (x : M) : ((f : M →ₗ[R] M₂) - g) x = f x - g x := rfl
lemma range_prod_eq {f : M →L[R] M₂} {g : M →L[R] M₃} (h : ker f ⊔ ker g = ⊤) :
range (f.prod g) = (range f).prod (range g) :=
linear_map.range_prod_eq h
section
variables [topological_add_group M₂]
instance : has_neg (M →L[R] M₂) := ⟨λ f, ⟨-f, f.2.neg⟩⟩
@[simp] lemma neg_apply : (-f) x = - (f x) := rfl
@[simp, norm_cast] lemma coe_neg : (((-f) : M →L[R] M₂) : M →ₗ[R] M₂) = -(f : M →ₗ[R] M₂) := rfl
@[norm_cast] lemma coe_neg' : (((-f) : M →L[R] M₂) : M → M₂) = -(f : M → M₂) := rfl
instance : add_comm_group (M →L[R] M₂) :=
by { refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext;
apply_rules [zero_add, add_assoc, add_zero, add_left_neg, add_comm] }
lemma sub_apply (x : M) : (f - g) x = f x - g x := rfl
@[simp, norm_cast] lemma coe_sub : (((f - g) : M →L[R] M₂) : M →ₗ[R] M₂) = (f : M →ₗ[R] M₂) - g := rfl
@[simp, norm_cast] lemma coe_sub' : (((f - g) : M →L[R] M₂) : M → M₂) = (f : M → M₂) - g := rfl
end
instance [topological_add_group M] : ring (M →L[R] M) :=
{ mul := (*),
one := 1,
mul_one := λ _, ext $ λ _, rfl,
one_mul := λ _, ext $ λ _, rfl,
mul_assoc := λ _ _ _, ext $ λ _, rfl,
left_distrib := λ _ _ _, ext $ λ _, map_add _ _ _,
right_distrib := λ _ _ _, ext $ λ _, linear_map.add_apply _ _ _,
..continuous_linear_map.add_comm_group }
lemma smul_right_one_pow [topological_space R]
[topological_add_group R] [topological_semimodule R R] (c : R) (n : ℕ) :
(smul_right 1 c : R →L[R] R)^n = smul_right 1 (c^n) :=
begin
induction n with n ihn,
{ ext, simp },
{ rw [pow_succ, ihn, mul_def, smul_right_comp, smul_eq_mul, pow_succ'] }
end
/-- Given a right inverse `f₂ : M₂ →L[R] M` to `f₁ : M →L[R] M₂`,
`proj_ker_of_right_inverse f₁ f₂ h` is the projection `M →L[R] f₁.ker` along `f₂.range`. -/
def proj_ker_of_right_inverse [topological_add_group M] (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M)
(h : function.right_inverse f₂ f₁) :
M →L[R] f₁.ker :=
(id R M - f₂.comp f₁).cod_restrict f₁.ker $ λ x, by simp [h (f₁ x)]
@[simp] lemma coe_proj_ker_of_right_inverse_apply [topological_add_group M]
(f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) (x : M) :
(f₁.proj_ker_of_right_inverse f₂ h x : M) = x - f₂ (f₁ x) :=
rfl
@[simp] lemma proj_ker_of_right_inverse_apply_idem [topological_add_group M]
(f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) (x : f₁.ker) :
f₁.proj_ker_of_right_inverse f₂ h x = x :=
subtype.ext_iff_val.2 $ by simp
@[simp] lemma proj_ker_of_right_inverse_comp_inv [topological_add_group M]
(f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) (y : M₂) :
f₁.proj_ker_of_right_inverse f₂ h (f₂ y) = 0 :=
subtype.ext_iff_val.2 $ by simp [h y]
end ring
section comm_ring
variables
{R : Type*} [comm_ring R] [topological_space R]
{M : Type*} [topological_space M] [add_comm_group M]
{M₂ : Type*} [topological_space M₂] [add_comm_group M₂]
{M₃ : Type*} [topological_space M₃] [add_comm_group M₃]
[module R M] [module R M₂] [module R M₃] [topological_module R M₃]
instance : has_scalar R (M →L[R] M₃) :=
⟨λ c f, ⟨c • f, continuous_const.smul f.2⟩⟩
variables (c : R) (h : M₂ →L[R] M₃) (f g : M →L[R] M₂) (x y z : M)
@[simp] lemma smul_comp : (c • h).comp f = c • (h.comp f) := rfl
variable [topological_module R M₂]
@[simp] lemma smul_apply : (c • f) x = c • (f x) := rfl
@[simp, norm_cast] lemma coe_apply : (((c • f) : M →L[R] M₂) : M →ₗ[R] M₂) = c • (f : M →ₗ[R] M₂) := rfl
@[norm_cast] lemma coe_apply' : (((c • f) : M →L[R] M₂) : M → M₂) = c • (f : M → M₂) := rfl
@[simp] lemma comp_smul : h.comp (c • f) = c • (h.comp f) := by { ext, simp }
variable [topological_add_group M₂]
instance : module R (M →L[R] M₂) :=
{ smul_zero := λ _, ext $ λ _, smul_zero _,
zero_smul := λ _, ext $ λ _, zero_smul _ _,
one_smul := λ _, ext $ λ _, one_smul _ _,
mul_smul := λ _ _ _, ext $ λ _, mul_smul _ _ _,
add_smul := λ _ _ _, ext $ λ _, add_smul _ _ _,
smul_add := λ _ _ _, ext $ λ _, smul_add _ _ _ }
instance : algebra R (M₂ →L[R] M₂) :=
algebra.of_semimodule' (λ c f, ext $ λ x, rfl) (λ c f, ext $ λ x, f.map_smul c x)
end comm_ring
end continuous_linear_map
namespace continuous_linear_equiv
section add_comm_monoid
variables {R : Type*} [semiring R]
{M : Type*} [topological_space M] [add_comm_monoid M]
{M₂ : Type*} [topological_space M₂] [add_comm_monoid M₂]
{M₃ : Type*} [topological_space M₃] [add_comm_monoid M₃]
{M₄ : Type*} [topological_space M₄] [add_comm_monoid M₄]
[semimodule R M] [semimodule R M₂] [semimodule R M₃] [semimodule R M₄]
/-- A continuous linear equivalence induces a continuous linear map. -/
def to_continuous_linear_map (e : M ≃L[R] M₂) : M →L[R] M₂ :=
{ cont := e.continuous_to_fun,
..e.to_linear_equiv.to_linear_map }
/-- Coerce continuous linear equivs to continuous linear maps. -/
instance : has_coe (M ≃L[R] M₂) (M →L[R] M₂) := ⟨to_continuous_linear_map⟩
/-- Coerce continuous linear equivs to maps. -/
-- see Note [function coercion]
instance : has_coe_to_fun (M ≃L[R] M₂) := ⟨λ _, M → M₂, λ f, f⟩
@[simp] theorem coe_def_rev (e : M ≃L[R] M₂) : e.to_continuous_linear_map = e := rfl
@[simp] theorem coe_apply (e : M ≃L[R] M₂) (b : M) : (e : M →L[R] M₂) b = e b := rfl
@[norm_cast] lemma coe_coe (e : M ≃L[R] M₂) : ((e : M →L[R] M₂) : M → M₂) = e := rfl
@[ext] lemma ext {f g : M ≃L[R] M₂} (h : (f : M → M₂) = g) : f = g :=
begin
cases f; cases g,
simp only,
ext x,
induction h,
refl
end
/-- A continuous linear equivalence induces a homeomorphism. -/
def to_homeomorph (e : M ≃L[R] M₂) : M ≃ₜ M₂ := { ..e }
-- Make some straightforward lemmas available to `simp`.
@[simp] lemma map_zero (e : M ≃L[R] M₂) : e (0 : M) = 0 := (e : M →L[R] M₂).map_zero
@[simp] lemma map_add (e : M ≃L[R] M₂) (x y : M) : e (x + y) = e x + e y :=
(e : M →L[R] M₂).map_add x y
@[simp] lemma map_smul (e : M ≃L[R] M₂) (c : R) (x : M) : e (c • x) = c • (e x) :=
(e : M →L[R] M₂).map_smul c x
@[simp] lemma map_eq_zero_iff (e : M ≃L[R] M₂) {x : M} : e x = 0 ↔ x = 0 :=
e.to_linear_equiv.map_eq_zero_iff
attribute [continuity]
continuous_linear_equiv.continuous_to_fun continuous_linear_equiv.continuous_inv_fun
@[continuity]
protected lemma continuous (e : M ≃L[R] M₂) : continuous (e : M → M₂) :=
e.continuous_to_fun
protected lemma continuous_on (e : M ≃L[R] M₂) {s : set M} : continuous_on (e : M → M₂) s :=
e.continuous.continuous_on
protected lemma continuous_at (e : M ≃L[R] M₂) {x : M} : continuous_at (e : M → M₂) x :=
e.continuous.continuous_at
protected lemma continuous_within_at (e : M ≃L[R] M₂) {s : set M} {x : M} :
continuous_within_at (e : M → M₂) s x :=
e.continuous.continuous_within_at
lemma comp_continuous_on_iff
{α : Type*} [topological_space α] (e : M ≃L[R] M₂) (f : α → M) (s : set α) :
continuous_on (e ∘ f) s ↔ continuous_on f s :=
e.to_homeomorph.comp_continuous_on_iff _ _
lemma comp_continuous_iff
{α : Type*} [topological_space α] (e : M ≃L[R] M₂) (f : α → M) :
continuous (e ∘ f) ↔ continuous f :=
e.to_homeomorph.comp_continuous_iff _
/-- An extensionality lemma for `R ≃L[R] M`. -/
lemma ext₁ [topological_space R] {f g : R ≃L[R] M} (h : f 1 = g 1) : f = g :=
ext $ funext $ λ x, mul_one x ▸ by rw [← smul_eq_mul, map_smul, h, map_smul]
section
variables (R M)
/-- The identity map as a continuous linear equivalence. -/
@[refl] protected def refl : M ≃L[R] M :=
{ continuous_to_fun := continuous_id,
continuous_inv_fun := continuous_id,
.. linear_equiv.refl R M }
end
@[simp, norm_cast] lemma coe_refl :
(continuous_linear_equiv.refl R M : M →L[R] M) = continuous_linear_map.id R M := rfl
@[simp, norm_cast] lemma coe_refl' :
(continuous_linear_equiv.refl R M : M → M) = id := rfl
/-- The inverse of a continuous linear equivalence as a continuous linear equivalence-/
@[symm] protected def symm (e : M ≃L[R] M₂) : M₂ ≃L[R] M :=
{ continuous_to_fun := e.continuous_inv_fun,
continuous_inv_fun := e.continuous_to_fun,
.. e.to_linear_equiv.symm }
@[simp] lemma symm_to_linear_equiv (e : M ≃L[R] M₂) :
e.symm.to_linear_equiv = e.to_linear_equiv.symm :=
by { ext, refl }
/-- The composition of two continuous linear equivalences as a continuous linear equivalence. -/
@[trans] protected def trans (e₁ : M ≃L[R] M₂) (e₂ : M₂ ≃L[R] M₃) : M ≃L[R] M₃ :=
{ continuous_to_fun := e₂.continuous_to_fun.comp e₁.continuous_to_fun,
continuous_inv_fun := e₁.continuous_inv_fun.comp e₂.continuous_inv_fun,
.. e₁.to_linear_equiv.trans e₂.to_linear_equiv }
@[simp] lemma trans_to_linear_equiv (e₁ : M ≃L[R] M₂) (e₂ : M₂ ≃L[R] M₃) :
(e₁.trans e₂).to_linear_equiv = e₁.to_linear_equiv.trans e₂.to_linear_equiv :=
by { ext, refl }
/-- Product of two continuous linear equivalences. The map comes from `equiv.prod_congr`. -/
def prod (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) : (M × M₃) ≃L[R] (M₂ × M₄) :=
{ continuous_to_fun := e.continuous_to_fun.prod_map e'.continuous_to_fun,
continuous_inv_fun := e.continuous_inv_fun.prod_map e'.continuous_inv_fun,
.. e.to_linear_equiv.prod e'.to_linear_equiv }
@[simp, norm_cast] lemma prod_apply (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (x) :
e.prod e' x = (e x.1, e' x.2) := rfl
@[simp, norm_cast] lemma coe_prod (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) :
(e.prod e' : (M × M₃) →L[R] (M₂ × M₄)) = (e : M →L[R] M₂).prod_map (e' : M₃ →L[R] M₄) :=
rfl
theorem bijective (e : M ≃L[R] M₂) : function.bijective e := e.to_linear_equiv.to_equiv.bijective
theorem injective (e : M ≃L[R] M₂) : function.injective e := e.to_linear_equiv.to_equiv.injective
theorem surjective (e : M ≃L[R] M₂) : function.surjective e := e.to_linear_equiv.to_equiv.surjective
@[simp] theorem apply_symm_apply (e : M ≃L[R] M₂) (c : M₂) : e (e.symm c) = c := e.1.6 c
@[simp] theorem symm_apply_apply (e : M ≃L[R] M₂) (b : M) : e.symm (e b) = b := e.1.5 b
@[simp] theorem coe_comp_coe_symm (e : M ≃L[R] M₂) :
(e : M →L[R] M₂).comp (e.symm : M₂ →L[R] M) = continuous_linear_map.id R M₂ :=
continuous_linear_map.ext e.apply_symm_apply
@[simp] theorem coe_symm_comp_coe (e : M ≃L[R] M₂) :
(e.symm : M₂ →L[R] M).comp (e : M →L[R] M₂) = continuous_linear_map.id R M :=
continuous_linear_map.ext e.symm_apply_apply
lemma symm_comp_self (e : M ≃L[R] M₂) :
(e.symm : M₂ → M) ∘ (e : M → M₂) = id :=
by{ ext x, exact symm_apply_apply e x }
lemma self_comp_symm (e : M ≃L[R] M₂) :
(e : M → M₂) ∘ (e.symm : M₂ → M) = id :=
by{ ext x, exact apply_symm_apply e x }
@[simp] lemma symm_comp_self' (e : M ≃L[R] M₂) :
((e.symm : M₂ →L[R] M) : M₂ → M) ∘ ((e : M →L[R] M₂) : M → M₂) = id :=
symm_comp_self e
@[simp] lemma self_comp_symm' (e : M ≃L[R] M₂) :
((e : M →L[R] M₂) : M → M₂) ∘ ((e.symm : M₂ →L[R] M) : M₂ → M) = id :=
self_comp_symm e
@[simp] theorem symm_symm (e : M ≃L[R] M₂) : e.symm.symm = e :=
by { ext x, refl }
theorem symm_symm_apply (e : M ≃L[R] M₂) (x : M) : e.symm.symm x = e x :=
rfl
lemma symm_apply_eq (e : M ≃L[R] M₂) {x y} : e.symm x = y ↔ x = e y :=
e.to_linear_equiv.symm_apply_eq
lemma eq_symm_apply (e : M ≃L[R] M₂) {x y} : y = e.symm x ↔ e y = x :=
e.to_linear_equiv.eq_symm_apply
/-- Create a `continuous_linear_equiv` from two `continuous_linear_map`s that are
inverse of each other. -/
def equiv_of_inverse (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h₁ : function.left_inverse f₂ f₁)
(h₂ : function.right_inverse f₂ f₁) :
M ≃L[R] M₂ :=
{ to_fun := f₁,
continuous_to_fun := f₁.continuous,
inv_fun := f₂,
continuous_inv_fun := f₂.continuous,
left_inv := h₁,
right_inv := h₂,
.. f₁ }
@[simp] lemma equiv_of_inverse_apply (f₁ : M →L[R] M₂) (f₂ h₁ h₂ x) :
equiv_of_inverse f₁ f₂ h₁ h₂ x = f₁ x :=
rfl
@[simp] lemma symm_equiv_of_inverse (f₁ : M →L[R] M₂) (f₂ h₁ h₂) :
(equiv_of_inverse f₁ f₂ h₁ h₂).symm = equiv_of_inverse f₂ f₁ h₂ h₁ :=
rfl
end add_comm_monoid
section add_comm_group
variables {R : Type*} [semiring R]
{M : Type*} [topological_space M] [add_comm_group M]
{M₂ : Type*} [topological_space M₂] [add_comm_group M₂]
{M₃ : Type*} [topological_space M₃] [add_comm_group M₃]
{M₄ : Type*} [topological_space M₄] [add_comm_group M₄]
[semimodule R M] [semimodule R M₂] [semimodule R M₃] [semimodule R M₄]
variables [topological_add_group M₄]
/-- Equivalence given by a block lower diagonal matrix. `e` and `e'` are diagonal square blocks,
and `f` is a rectangular block below the diagonal. -/
def skew_prod (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (f : M →L[R] M₄) :
(M × M₃) ≃L[R] M₂ × M₄ :=
{ continuous_to_fun := (e.continuous_to_fun.comp continuous_fst).prod_mk
((e'.continuous_to_fun.comp continuous_snd).add $ f.continuous.comp continuous_fst),
continuous_inv_fun := (e.continuous_inv_fun.comp continuous_fst).prod_mk
(e'.continuous_inv_fun.comp $ continuous_snd.sub $ f.continuous.comp $
e.continuous_inv_fun.comp continuous_fst),
.. e.to_linear_equiv.skew_prod e'.to_linear_equiv ↑f }
@[simp] lemma skew_prod_apply (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (f : M →L[R] M₄) (x) :
e.skew_prod e' f x = (e x.1, e' x.2 + f x.1) := rfl
@[simp] lemma skew_prod_symm_apply (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (f : M →L[R] M₄) (x) :
(e.skew_prod e' f).symm x = (e.symm x.1, e'.symm (x.2 - f (e.symm x.1))) := rfl
end add_comm_group
section ring
variables {R : Type*} [ring R]
{M : Type*} [topological_space M] [add_comm_group M] [semimodule R M]
{M₂ : Type*} [topological_space M₂] [add_comm_group M₂] [semimodule R M₂]
@[simp] lemma map_sub (e : M ≃L[R] M₂) (x y : M) : e (x - y) = e x - e y :=
(e : M →L[R] M₂).map_sub x y
@[simp] lemma map_neg (e : M ≃L[R] M₂) (x : M) : e (-x) = -e x := (e : M →L[R] M₂).map_neg x
section
variables (R) [topological_space R] [topological_module R R]
/-- Continuous linear equivalences `R ≃L[R] R` are enumerated by `units R`. -/
def units_equiv_aut : units R ≃ (R ≃L[R] R) :=
{ to_fun := λ u, equiv_of_inverse
(continuous_linear_map.smul_right 1 ↑u)
(continuous_linear_map.smul_right 1 ↑u⁻¹)
(λ x, by simp) (λ x, by simp),
inv_fun := λ e, ⟨e 1, e.symm 1,
by rw [← smul_eq_mul, ← map_smul, smul_eq_mul, mul_one, symm_apply_apply],
by rw [← smul_eq_mul, ← map_smul, smul_eq_mul, mul_one, apply_symm_apply]⟩,
left_inv := λ u, units.ext $ by simp,
right_inv := λ e, ext₁ $ by simp }
variable {R}
@[simp] lemma units_equiv_aut_apply (u : units R) (x : R) : units_equiv_aut R u x = x * u := rfl
@[simp] lemma units_equiv_aut_apply_symm (u : units R) (x : R) :
(units_equiv_aut R u).symm x = x * ↑u⁻¹ := rfl
@[simp] lemma units_equiv_aut_symm_apply (e : R ≃L[R] R) :
↑((units_equiv_aut R).symm e) = e 1 :=
rfl
end
variables [topological_add_group M]
open continuous_linear_map (id fst snd subtype_val mem_ker)
/-- A pair of continuous linear maps such that `f₁ ∘ f₂ = id` generates a continuous
linear equivalence `e` between `M` and `M₂ × f₁.ker` such that `(e x).2 = x` for `x ∈ f₁.ker`,
`(e x).1 = f₁ x`, and `(e (f₂ y)).2 = 0`. The map is given by `e x = (f₁ x, x - f₂ (f₁ x))`. -/
def equiv_of_right_inverse (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) :
M ≃L[R] M₂ × f₁.ker :=
equiv_of_inverse (f₁.prod (f₁.proj_ker_of_right_inverse f₂ h)) (f₂.coprod (subtype_val f₁.ker))
(λ x, by simp)
(λ ⟨x, y⟩, by simp [h x])
@[simp] lemma fst_equiv_of_right_inverse (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M)
(h : function.right_inverse f₂ f₁) (x : M) :
(equiv_of_right_inverse f₁ f₂ h x).1 = f₁ x := rfl
@[simp] lemma snd_equiv_of_right_inverse (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M)
(h : function.right_inverse f₂ f₁) (x : M) :
((equiv_of_right_inverse f₁ f₂ h x).2 : M) = x - f₂ (f₁ x) := rfl
@[simp] lemma equiv_of_right_inverse_symm_apply (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M)
(h : function.right_inverse f₂ f₁) (y : M₂ × f₁.ker) :
(equiv_of_right_inverse f₁ f₂ h).symm y = f₂ y.1 + y.2 := rfl
end ring
end continuous_linear_equiv
namespace submodule
variables
{R : Type*} [ring R]
{M : Type*} [topological_space M] [add_comm_group M] [module R M]
{M₂ : Type*} [topological_space M₂] [add_comm_group M₂] [module R M₂]
open continuous_linear_map
/-- A submodule `p` is called *complemented* if there exists a continuous projection `M →ₗ[R] p`. -/
def closed_complemented (p : submodule R M) : Prop := ∃ f : M →L[R] p, ∀ x : p, f x = x
lemma closed_complemented.has_closed_complement {p : submodule R M} [t1_space p]
(h : closed_complemented p) :
∃ (q : submodule R M) (hq : is_closed (q : set M)), is_compl p q :=
exists.elim h $ λ f hf, ⟨f.ker, f.is_closed_ker, linear_map.is_compl_of_proj hf⟩
protected lemma closed_complemented.is_closed [topological_add_group M] [t1_space M]
{p : submodule R M} (h : closed_complemented p) :
is_closed (p : set M) :=
begin
rcases h with ⟨f, hf⟩,
have : ker (id R M - (subtype_val p).comp f) = p := linear_map.ker_id_sub_eq_of_proj hf,
exact this ▸ (is_closed_ker _)
end
@[simp] lemma closed_complemented_bot : closed_complemented (⊥ : submodule R M) :=
⟨0, λ x, by simp only [zero_apply, eq_zero_of_bot_submodule x]⟩
@[simp] lemma closed_complemented_top : closed_complemented (⊤ : submodule R M) :=
⟨(id R M).cod_restrict ⊤ (λ x, trivial), λ x, subtype.ext_iff_val.2 $ by simp⟩
end submodule
lemma continuous_linear_map.closed_complemented_ker_of_right_inverse {R : Type*} [ring R]
{M : Type*} [topological_space M] [add_comm_group M]
{M₂ : Type*} [topological_space M₂] [add_comm_group M₂] [module R M] [module R M₂]
[topological_add_group M] (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M)
(h : function.right_inverse f₂ f₁) :
f₁.ker.closed_complemented :=
⟨f₁.proj_ker_of_right_inverse f₂ h, f₁.proj_ker_of_right_inverse_apply_idem f₂ h⟩
|
5b8b3c435fe7afa38fbd048b8df7af9d59058554 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/bitvec/basic.lean | 74c21d597dc6178abb89dfbb199062d5ee0bb044 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,467 | 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 Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.bitvec.core
import Mathlib.data.fin
import Mathlib.tactic.norm_num
import Mathlib.tactic.monotonicity.default
import Mathlib.PostPort
namespace Mathlib
namespace bitvec
protected instance preorder (n : ℕ) : preorder (bitvec n) :=
preorder.lift bitvec.to_nat
/-- convert `fin` to `bitvec` -/
def of_fin {n : ℕ} (i : fin (bit0 1 ^ n)) : bitvec n :=
bitvec.of_nat n (subtype.val i)
theorem of_fin_val {n : ℕ} (i : fin (bit0 1 ^ n)) : bitvec.to_nat (of_fin i) = subtype.val i := sorry
/-- convert `bitvec` to `fin` -/
def to_fin {n : ℕ} (i : bitvec n) : fin (bit0 1 ^ n) :=
fin.of_nat' (bitvec.to_nat i)
theorem add_lsb_eq_twice_add_one {x : ℕ} {b : Bool} : add_lsb x b = bit0 1 * x + cond b 1 0 := sorry
theorem to_nat_eq_foldr_reverse {n : ℕ} (v : bitvec n) : bitvec.to_nat v = list.foldr (flip add_lsb) 0 (list.reverse (vector.to_list v)) := sorry
theorem to_nat_lt {n : ℕ} (v : bitvec n) : bitvec.to_nat v < bit0 1 ^ n := sorry
theorem add_lsb_div_two {x : ℕ} {b : Bool} : add_lsb x b / bit0 1 = x := sorry
theorem to_bool_add_lsb_mod_two {x : ℕ} {b : Bool} : to_bool (add_lsb x b % bit0 1 = 1) = b := sorry
theorem of_nat_to_nat {n : ℕ} (v : bitvec n) : bitvec.of_nat n (bitvec.to_nat v) = v := sorry
theorem to_fin_val {n : ℕ} (v : bitvec n) : ↑(to_fin v) = bitvec.to_nat v := sorry
theorem to_fin_le_to_fin_of_le {n : ℕ} {v₀ : bitvec n} {v₁ : bitvec n} (h : v₀ ≤ v₁) : to_fin v₀ ≤ to_fin v₁ :=
(fun (this : ↑(to_fin v₀) ≤ ↑(to_fin v₁)) => this)
(eq.mpr (id (Eq._oldrec (Eq.refl (↑(to_fin v₀) ≤ ↑(to_fin v₁))) (to_fin_val v₀)))
(eq.mpr (id (Eq._oldrec (Eq.refl (bitvec.to_nat v₀ ≤ ↑(to_fin v₁))) (to_fin_val v₁))) h))
theorem of_fin_le_of_fin_of_le {n : ℕ} {i : fin (bit0 1 ^ n)} {j : fin (bit0 1 ^ n)} (h : i ≤ j) : of_fin i ≤ of_fin j := sorry
theorem to_fin_of_fin {n : ℕ} (i : fin (bit0 1 ^ n)) : to_fin (of_fin i) = i := sorry
theorem of_fin_to_fin {n : ℕ} (v : bitvec n) : of_fin (to_fin v) = v :=
id
(eq.mpr (id (Eq._oldrec (Eq.refl (bitvec.of_nat n ↑(to_fin v) = v)) (to_fin_val v)))
(eq.mpr (id (Eq._oldrec (Eq.refl (bitvec.of_nat n (bitvec.to_nat v) = v)) (of_nat_to_nat v))) (Eq.refl v)))
|
dbf2b5952e6c884de8ef63659a6623f15789306a | df7bb3acd9623e489e95e85d0bc55590ab0bc393 | /lean/love12_basic_mathematical_structures_demo.lean | d0e52552d1503de0c25af200ccf1ee217717ee48 | [] | no_license | MaschavanderMarel/logical_verification_2020 | a41c210b9237c56cb35f6cd399e3ac2fe42e775d | 7d562ef174cc6578ca6013f74db336480470b708 | refs/heads/master | 1,692,144,223,196 | 1,634,661,675,000 | 1,634,661,675,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 12,475 | lean | import .love05_inductive_predicates_demo
/- # LoVe Demo 12: Basic Mathematical Structures
We introduce definitions and proofs about basic mathematical structures such as
groups, fields, and linear orders. -/
set_option pp.beta true
set_option pp.generalized_field_notation false
namespace LoVe
/- ## Type Classes over a Single Binary Operator
Mathematically, a __group__ is a set `G` with a binary operator `• : G × G → G`
with the following properties, called __group axioms__:
* Associativity: For all `a, b, c ∈ G`, we have `(a • b) • c = a • (b • c)`.
* Identity element: There exists an element `e ∈ G` such that for all `a ∈ G`,
we have `e • a = a`.
* Inverse element: For each `a ∈ G`, there exists an inverse element
`inv(a) ∈ G` such that `inv(a) • a = e`.
Examples of groups are
* `ℤ` with `+`;
* `ℝ` with `+`;
* `ℝ \ {0}` with `*`.
In Lean, a type class for groups can be defined as follows: -/
namespace monolithic_group
@[class] structure group (α : Type) :=
(mul : α → α → α)
(one : α)
(inv : α → α)
(mul_assoc : ∀a b c, mul (mul a b) c = mul a (mul b c))
(one_mul : ∀a, mul one a = a)
(mul_left_inv : ∀a, mul (inv a) a = one)
end monolithic_group
/- In Lean, however, group is part of a larger hierarchy of algebraic
structures:
Type class | Properties | Examples
------------------------ | -----------------------------------------|-------------------
`semigroup` | associativity of `*` | `ℝ`, `ℚ`, `ℤ`, `ℕ`
`monoid` | `semigroup` with unit `1` | `ℝ`, `ℚ`, `ℤ`, `ℕ`
`left_cancel_semigroup` | `semigroup` with `c * a = c * b → a = b` |
`right_cancel_semigroup` | `semigroup` with `a * c = b * c → a = b` |
`group` | `monoid` with inverse `⁻¹` |
Most of these structures have commutative versions: `comm_semigroup`,
`comm_monoid`, `comm_group`.
The __multiplicative__ structures (over `*`, `1`, `⁻¹`) are copied to produce
__additive__ versions (over `+`, `0`, `-`):
Type class | Properties | Examples
---------------------------- | ---------------------------------------------|-------------------
`add_semigroup` | associativity of `+` | `ℝ`, `ℚ`, `ℤ`, `ℕ`
`add_monoid` | `add_semigroup` with unit `0` | `ℝ`, `ℚ`, `ℤ`, `ℕ`
`add_left_cancel_semigroup` | `add_semigroup` with `c + a = c + b → a = b` | `ℝ`, `ℚ`, `ℤ`, `ℕ`
`add_right_cancel_semigroup` | `add_semigroup` with `a + c = b + c → a = b` | `ℝ`, `ℚ`, `ℤ`, `ℕ`
`add_group` | `add_monoid` with inverse `-` | `ℝ`, `ℚ`, `ℤ` -/
#print group
#print add_group
/- Let us define our own type, of integers modulo 2, and register it as an
additive group. -/
inductive ℤ₂ : Type
| zero
| one
def ℤ₂.add : ℤ₂ → ℤ₂ → ℤ₂
| ℤ₂.zero a := a
| a ℤ₂.zero := a
| ℤ₂.one ℤ₂.one := ℤ₂.zero
@[instance] def ℤ₂.add_group : add_group ℤ₂ :=
{ add := ℤ₂.add,
add_assoc :=
by intros a b c; cases' a; cases' b; cases' c; refl,
zero := ℤ₂.zero,
zero_add := by intro a; cases' a; refl,
add_zero := by intro a; cases' a; refl,
neg := λa, a,
add_left_neg := by intro a; cases' a; refl }
#reduce ℤ₂.one + 0 - 0 - ℤ₂.one
lemma ℤ₂.add_right_neg:
∀a : ℤ₂, a + - a = 0 :=
add_right_neg
/- Another example: Lists are an `add_monoid`: -/
@[instance] def list.add_monoid {α : Type} :
add_monoid (list α) :=
{ zero := [],
add := (++),
add_assoc := list.append_assoc,
zero_add := list.nil_append,
add_zero := list.append_nil }
/- ## Type Classes with Two Binary Operators
Mathematically, a __field__ is a set `F` such that
* `F` forms a commutative group under an operator `+`, called addition, with
identity element `0`.
* `F\{0}` forms a commutative group under an operator `*`, called
multiplication.
* Multiplication distributes over addition—i.e.,
`a * (b + c) = a * b + a * c` for all `a, b, c ∈ F`.
In Lean, fields are also part of a larger hierarchy:
Type class | Properties | Examples
-----------------|-----------------------------------------------------|-------------------
`semiring` | `monoid` and `add_comm_monoid` with distributivity | `ℝ`, `ℚ`, `ℤ`, `ℕ`
`comm_semiring` | `semiring` with commutativity of `*` | `ℝ`, `ℚ`, `ℤ`, `ℕ`
`ring` | `monoid` and `add_comm_group` with distributivity | `ℝ`, `ℚ`, `ℤ`
`comm_ring` | `ring` with commutativity of `*` | `ℝ`, `ℚ`, `ℤ`
`division_ring` | `ring` with multiplicative inverse `⁻¹` | `ℝ`, `ℚ`
`field` | `division_ring` with commutativity of `*` | `ℝ`, `ℚ`
`discrete_field` | `field` with decidable equality and `∀n, n / 0 = 0` | `ℝ`, `ℚ` -/
#print field
/- Let us continue with our example: -/
def ℤ₂.mul : ℤ₂ → ℤ₂ → ℤ₂
| ℤ₂.one a := a
| a ℤ₂.one := a
| ℤ₂.zero ℤ₂.zero := ℤ₂.zero
@[instance] def ℤ₂.field : field ℤ₂ :=
{ one := ℤ₂.one,
mul := ℤ₂.mul,
inv := λa, a,
add_comm := by intros a b; cases' a; cases' b; refl,
exists_pair_ne :=
by apply exists.intro ℤ₂.zero; apply exists.intro ℤ₂.one;
finish,
one_mul := by intros a; cases' a; refl,
mul_one := by intros a; cases' a; refl,
mul_inv_cancel := by intros a h; cases' a; finish,
inv_zero := by refl,
mul_assoc :=
by intros a b c; cases' a; cases' b; cases' c; refl,
mul_comm := by intros a b; cases' a; cases' b; refl,
left_distrib :=
by intros a b c; cases' a; cases' b; cases' c; refl,
right_distrib :=
by intros a b c; cases' a; cases' b; cases' c; refl,
..ℤ₂.add_group }
#reduce (1 : ℤ₂) * 0 / (0 - 1)
#reduce (3 : ℤ₂)
lemma ring_example (a b : ℤ₂) :
(a + b) ^ 3 = a ^ 3 + 3 * a ^ 2 * b + 3 * a * b ^ 2 + b ^ 3 :=
by ring
lemma ring_exp_example (a b : ℤ₂) (n : ℕ):
(a + b) ^ (2 + n) =
(a + b) ^ n * (a ^ 2 + 2 * a * b + b ^ 2) :=
by ring_exp
/- `ring` and `ring_exp` prove equalities over commutative rings and semirings
by normalizing expressions. The `ring_exp` variant also normalizes exponents. -/
lemma abel_example (a b : ℤ) :
a + b + 0 - (b + a + a) = - a :=
by abel
/- `abel` proves equalities over additive commutative monoids and groups by
normalizing expressions.
## Coercions
When combining numbers form `ℕ`, `ℤ`, `ℚ`, and `ℝ`, we might want to cast from
one type to another. Lean has a mechanism to automatically introduce coercions,
represented by `coe` (syntactic sugar: `↑`). `coe` can be set up to provide
implicit coercions between arbitrary types.
Many coercions are already in place, including the following:
* `coe : ℕ → α` casts `ℕ` to another semiring `α`;
* `coe : ℤ → α` casts `ℤ` to another ring `α`;
* `coe : ℚ → α` casts `ℚ` to another division ring `α`.
For example, this works, although negation `- n` is not defined on natural
numbers: -/
lemma neg_mul_neg_nat (n : ℕ) (z : ℤ) :
(- z) * (- n) = z * n :=
neg_mul_neg z n
/- Notice how Lean introduced a `↑` coercion: -/
#print neg_mul_neg_nat
/- Another example: -/
lemma neg_nat_mul_neg (n : ℕ) (z : ℤ) :
(- n : ℤ) * (- z) = n * z :=
neg_mul_neg n z
#print neg_nat_mul_neg
/- In proofs involving coercions, the tactic `norm_cast` can be convenient. -/
lemma norm_cast_example_1 (m n : ℕ) (h : (m : ℤ) = (n : ℤ)) :
m = n :=
begin
norm_cast at h,
exact h
end
lemma norm_cast_example_2 (m n : ℕ) :
(m : ℤ) + (n : ℤ) = ((m + n : ℕ) : ℤ) :=
by norm_cast
/- `norm_cast` moves coercions towards the inside of expressions, as a form of
simplification. Like `simp`, it will generally produce a subgoal.
`norm_cast` relies on lemmas such as the following: -/
#check nat.cast_add
#check int.cast_add
#check rat.cast_add
/- ### Lists, Multisets and Finite Sets
For finite collections of elements different structures are available:
* lists: order and duplicates matter;
* multisets: only duplicates matter;
* finsets: neither order nor duplicates matter. -/
lemma list_duplicates_example :
[2, 3, 3, 4] ≠ [2, 3, 4] :=
dec_trivial
lemma list_order_example :
[4, 3, 2] ≠ [2, 3, 4] :=
dec_trivial
lemma multiset_duplicates_example :
({2, 3, 3, 4} : multiset ℕ) ≠ {2, 3, 4} :=
dec_trivial
lemma multiset_order_example :
({2, 3, 4} : multiset ℕ) = {4, 3, 2} :=
dec_trivial
lemma finset_duplicates_example :
({2, 3, 3, 4} : finset ℕ) = {2, 3, 4} :=
dec_trivial
lemma finsetorder_example :
({2, 3, 4} : finset ℕ) = {4, 3, 2} :=
dec_trivial
/- `dec_trivial` is a special lemma that can be used on trivial decidable
goals (e.g., true closed executable expressions). -/
def list.elems : btree ℕ → list ℕ
| btree.empty := []
| (btree.node a l r) := a :: list.elems l ++ list.elems r
def multiset.elems : btree ℕ → multiset ℕ
| btree.empty := ∅
| (btree.node a l r) :=
{a} ∪ (multiset.elems l ∪ multiset.elems r)
def finset.elems : btree ℕ → finset ℕ
| btree.empty := ∅
| (btree.node a l r) := {a} ∪ (finset.elems l ∪ finset.elems r)
#eval list.sum [2, 3, 4] -- result: 9
#eval multiset.sum ({2, 3, 4} : multiset ℕ) -- result: 9
#eval finset.sum ({2, 3, 4} : finset ℕ) (λn, n) -- result: 9
#eval list.prod [2, 3, 4] -- result: 24
#eval multiset.prod ({2, 3, 4} : multiset ℕ) -- result: 24
#eval finset.prod ({2, 3, 4} : finset ℕ) (λn, n) -- result: 24
/- ## Order Type Classes
Many of the structures introduced above can be ordered. For example, the
well-known order on the natural numbers can be defined as follows: -/
inductive nat.le : ℕ → ℕ → Prop
| refl : ∀a : ℕ, nat.le a a
| step : ∀a b : ℕ, nat.le a b → nat.le a (b + 1)
/- This is an example of a linear order. A __linear order__ (or
__total order__) is a binary relation `≤` such that for all `a`, `b`, `c`, the
following properties hold:
* Reflexivity: `a ≤ a`.
* Transitivity: If `a ≤ b` and `b ≤ c`, then `a ≤ c`.
* Antisymmetry: If `a ≤ b` and `b ≤ a`, then `a = b`.
* Totality: `a ≤ b` or `b ≤ a`.
If a relation has the first three properties, it is a __partial order__. An
example is `⊆` on sets, finite sets, or multisets. If a relation has the first
two properties, it is a __preorder__. An example is comparing lists by their
length.
In Lean, there are type classes for these different kinds of orders:
`linear_order`, `partial_order`, and `preorder`. The `preorder` class has the
fields
`le : α → α → Prop`
`le_refl : ∀a : α, le a a`
`le_trans : ∀a b c : α, le a b → le b c → le a c`
The `partial_order` class also has
`le_antisymm : ∀a b : α, le a b → le b a → a = b`
and `linear_order` also has
`le_total : ∀a b : α, le a b ∨ le b a`
We can declare the preorder on lists that compares lists by their length as
follows: -/
@[instance] def list.length.preord {α : Type} :
preorder (list α) :=
{ le := λxs ys, list.length xs ≤ list.length ys,
le_refl := by intro xs; exact nat.le_refl _,
le_trans := by intros xs ys zs; exact nat.le_trans }
/- This instance introduces the infix syntax `≤` and the relations `≥`, `<`,
and `>`: -/
lemma list.length.preord_example {α : Type} (c : α) :
[c] > [] :=
dec_trivial
/- Complete lattices (lecture 10) are formalized as another type class,
`complete_lattice`, which inherits from `partial_order`.
Type classes combining orders and algebraic structures are also available:
`ordered_cancel_comm_monoid`
`ordered_comm_group ordered_semiring`
`linear_ordered_semiring`
`linear_ordered_comm_ring`
`linear_ordered_field`
All these mathematical structures relate `≤` and `<` with `0`, `1`, `+`, and `*`
by monotonicity rules (e.g., `a ≤ b → c ≤ d → a + c ≤ b + d`) and cancellation
rules (e.g., `c + a ≤ c + b → a ≤ b`). -/
end LoVe
|
f238f0b4cd5ca64976e8bf69b01dfc6e16514bd9 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/measure_theory/tactic.lean | 1db9e2a782ebc27351d93096b2426c4f309a9b89 | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,436 | lean | /-
Copyright (c) 2021 Rémy Degenne. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Rémy Degenne
-/
import tactic.auto_cases
import tactic.tidy
import tactic.with_local_reducibility
import tactic.show_term
import measure_theory.measure.measure_space_def
/-!
# Tactics for measure theory
Currently we have one domain-specific tactic for measure theory: `measurability`.
This tactic is to a large extent a copy of the `continuity` tactic by Reid Barton.
-/
/-!
### `measurability` tactic
Automatically solve goals of the form `measurable f`, `ae_measurable f μ` and `measurable_set s`.
Mark lemmas with `@[measurability]` to add them to the set of lemmas
used by `measurability`. Note: `to_additive` doesn't know yet how to
copy the attribute to the additive version.
-/
/-- User attribute used to mark tactics used by `measurability`. -/
@[user_attribute]
meta def measurability : user_attribute :=
{ name := `measurability,
descr := "lemmas usable to prove (ae)-measurability" }
/- Mark some measurability lemmas already defined in `measure_theory.measurable_space_def` and
`measure_theory.measure_space_def` -/
attribute [measurability]
measurable_id
measurable_id'
ae_measurable_id
ae_measurable_id'
measurable_const
ae_measurable_const
ae_measurable.measurable_mk
measurable_set.empty
measurable_set.univ
measurable_set.compl
subsingleton.measurable_set
measurable_set.Union
measurable_set.Inter
measurable_set.Union_Prop
measurable_set.Inter_Prop
measurable_set.union
measurable_set.inter
measurable_set.diff
measurable_set.symm_diff
measurable_set.ite
measurable_set.cond
measurable_set.disjointed
measurable_set.const
measurable_set.insert
measurable_set_eq
set.finite.measurable_set
finset.measurable_set
set.countable.measurable_set
measurable_space.measurable_set_top
namespace tactic
/--
Tactic to apply `measurable.comp` when appropriate.
Applying `measurable.comp` is not always a good idea, so we have some
extra logic here to try to avoid bad cases.
* If the function we're trying to prove measurable is actually
constant, and that constant is a function application `f z`, then
measurable.comp would produce new goals `measurable f`, `measurable
(λ _, z)`, which is silly. We avoid this by failing if we could
apply `measurable_const`.
* measurable.comp will always succeed on `measurable (λ x, f x)` and
produce new goals `measurable (λ x, x)`, `measurable f`. We detect
this by failing if a new goal can be closed by applying
measurable_id.
-/
meta def apply_measurable.comp : tactic unit :=
`[fail_if_success { exact measurable_const };
refine measurable.comp _ _;
fail_if_success { exact measurable_id }]
/--
Tactic to apply `measurable.comp_ae_measurable` when appropriate.
Applying `measurable.comp_ae_measurable` is not always a good idea, so we have some
extra logic here to try to avoid bad cases.
* If the function we're trying to prove measurable is actually
constant, and that constant is a function application `f z`, then
`measurable.comp_ae_measurable` would produce new goals `measurable f`, `ae_measurable
(λ _, z) μ`, which is silly. We avoid this by failing if we could
apply `ae_measurable_const`.
* `measurable.comp_ae_measurable` will always succeed on `ae_measurable (λ x, f x) μ` and
can produce new goals (`measurable (λ x, x)`, `ae_measurable f μ`) or
(`measurable f`, `ae_measurable (λ x, x) μ`). We detect those by failing if a new goal can be
closed by applying `measurable_id` or `ae_measurable_id`.
-/
meta def apply_measurable.comp_ae_measurable : tactic unit :=
`[fail_if_success { exact ae_measurable_const };
refine measurable.comp_ae_measurable _ _;
fail_if_success { exact measurable_id };
fail_if_success { exact ae_measurable_id }]
/--
We don't want the intro1 tactic to apply to a goal of the form `measurable f`, `ae_measurable f μ`
or `measurable_set s`. This tactic tests the target to see if it matches that form.
-/
meta def goal_is_not_measurable : tactic unit :=
do t ← tactic.target,
match t with
| `(measurable %%l) := failed
| `(ae_measurable %%l %%r) := failed
| `(measurable_set %%l) := failed
| _ := skip
end
/-- List of tactics used by `measurability` internally. -/
meta def measurability_tactics (md : transparency := semireducible) : list (tactic string) :=
[
propositional_goal >> apply_assumption
>> pure "apply_assumption",
goal_is_not_measurable >> intro1
>>= λ ns, pure ("intro " ++ ns.to_string),
apply_rules [``(measurability)] 50 { md := md }
>> pure "apply_rules measurability",
apply_measurable.comp >> pure "refine measurable.comp _ _",
apply_measurable.comp_ae_measurable
>> pure "refine measurable.comp_ae_measurable _ _",
`[ refine measurable.ae_measurable _ ]
>> pure "refine measurable.ae_measurable _"
]
namespace interactive
setup_tactic_parser
/--
Solve goals of the form `measurable f`, `ae_measurable f μ` or `measurable_set s`.
`measurability?` reports back the proof term it found.
-/
meta def measurability
(bang : parse $ optional (tk "!")) (trace : parse $ optional (tk "?")) (cfg : tidy.cfg := {}) :
tactic unit :=
let md := if bang.is_some then semireducible else reducible,
measurability_core := tactic.tidy { tactics := measurability_tactics md, ..cfg },
trace_fn := if trace.is_some then show_term else id in
trace_fn measurability_core
/-- Version of `measurability` for use with auto_param. -/
meta def measurability' : tactic unit := measurability none none {}
/--
`measurability` solves goals of the form `measurable f`, `ae_measurable f μ` or `measurable_set s`
by applying lemmas tagged with the `measurability` user attribute.
You can also use `measurability!`, which applies lemmas with `{ md := semireducible }`.
The default behaviour is more conservative, and only unfolds `reducible` definitions
when attempting to match lemmas with the goal.
`measurability?` reports back the proof term it found.
-/
add_tactic_doc
{ name := "measurability / measurability'",
category := doc_category.tactic,
decl_names := [`tactic.interactive.measurability, `tactic.interactive.measurability'],
tags := ["lemma application"]
}
end interactive
end tactic
|
4bb7cbade4c832f327348cf7665f5234cbb71249 | 7b02c598aa57070b4cf4fbfe2416d0479220187f | /homotopy/EM.hlean | 33acbbe66e4ca2ecd6b0799c56debb6a255d53b4 | [
"Apache-2.0"
] | permissive | jdchristensen/Spectral | 50d4f0ddaea1484d215ef74be951da6549de221d | 6ded2b94d7ae07c4098d96a68f80a9cd3d433eb8 | refs/heads/master | 1,611,555,010,649 | 1,496,724,191,000 | 1,496,724,191,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 27,814 | hlean | -- Authors: Floris van Doorn
import homotopy.EM algebra.category.functor.equivalence types.pointed2 ..pointed_pi ..pointed
open eq equiv is_equiv algebra group nat pointed EM.ops is_trunc trunc susp function is_conn
namespace EM
definition EMadd1_functor_succ [unfold_full] {G H : AbGroup} (φ : G →g H) (n : ℕ) :
EMadd1_functor φ (succ n) ~* ptrunc_functor (n+2) (psusp_functor (EMadd1_functor φ n)) :=
by reflexivity
definition EM1_functor_gid (G : Group) : EM1_functor (gid G) ~* !pid :=
begin
fapply phomotopy.mk,
{ intro x, induction x,
{ reflexivity },
{ apply eq_pathover_id_right, apply hdeg_square, apply elim_pth, },
{ apply @is_prop.elim, apply is_trunc_pathover }},
{ reflexivity },
end
definition EMadd1_functor_gid (G : AbGroup) (n : ℕ) : EMadd1_functor (gid G) n ~* !pid :=
begin
induction n with n p,
{ apply EM1_functor_gid },
{ refine !EMadd1_functor_succ ⬝* _,
refine ptrunc_functor_phomotopy _ (psusp_functor_phomotopy p ⬝* !psusp_functor_pid) ⬝* _,
apply ptrunc_functor_pid }
end
definition EM_functor_gid (G : AbGroup) (n : ℕ) : EM_functor (gid G) n ~* !pid :=
begin
cases n with n,
{ apply pmap_of_homomorphism_gid },
{ apply EMadd1_functor_gid }
end
definition EM1_functor_gcompose {G H K : Group} (ψ : H →g K) (φ : G →g H) :
EM1_functor (ψ ∘g φ) ~* EM1_functor ψ ∘* EM1_functor φ :=
begin
fapply phomotopy.mk,
{ intro x, induction x,
{ reflexivity },
{ apply eq_pathover, apply hdeg_square, esimp,
refine !elim_pth ⬝ _ ⬝ (ap_compose (EM1_functor ψ) _ _)⁻¹,
refine _ ⬝ ap02 _ !elim_pth⁻¹, exact !elim_pth⁻¹ },
{ apply @is_prop.elim, apply is_trunc_pathover }},
{ reflexivity },
end
definition EMadd1_functor_gcompose {G H K : AbGroup} (ψ : H →g K) (φ : G →g H) (n : ℕ) :
EMadd1_functor (ψ ∘g φ) n ~* EMadd1_functor ψ n ∘* EMadd1_functor φ n :=
begin
induction n with n p,
{ apply EM1_functor_gcompose },
{ refine !EMadd1_functor_succ ⬝* _,
refine ptrunc_functor_phomotopy _ (psusp_functor_phomotopy p ⬝* !psusp_functor_pcompose) ⬝* _,
apply ptrunc_functor_pcompose }
end
definition EM_functor_gcompose {G H K : AbGroup} (ψ : H →g K) (φ : G →g H) (n : ℕ) :
EM_functor (ψ ∘g φ) n ~* EM_functor ψ n ∘* EM_functor φ n :=
begin
cases n with n,
{ apply pmap_of_homomorphism_gcompose },
{ apply EMadd1_functor_gcompose }
end
definition EM1_functor_phomotopy {G H : Group} {φ ψ : G →g H} (p : φ ~ ψ) :
EM1_functor φ ~* EM1_functor ψ :=
begin
fapply phomotopy.mk,
{ intro x, induction x,
{ reflexivity },
{ apply eq_pathover, apply hdeg_square, esimp,
refine !elim_pth ⬝ _ ⬝ !elim_pth⁻¹, exact ap pth (p g) },
{ apply @is_prop.elim, apply is_trunc_pathover }},
{ reflexivity },
end
definition EMadd1_functor_phomotopy {G H : AbGroup} {φ ψ : G →g H} (p : φ ~ ψ) (n : ℕ) :
EMadd1_functor φ n ~* EMadd1_functor ψ n :=
begin
induction n with n q,
{ exact EM1_functor_phomotopy p },
{ exact ptrunc_functor_phomotopy _ (psusp_functor_phomotopy q) }
end
definition EM_functor_phomotopy {G H : AbGroup} {φ ψ : G →g H} (p : φ ~ ψ) (n : ℕ) :
EM_functor φ n ~* EM_functor ψ n :=
begin
cases n with n,
{ exact pmap_of_homomorphism_phomotopy p },
{ exact EMadd1_functor_phomotopy p n }
end
definition EM_equiv_EM [constructor] {G H : AbGroup} (φ : G ≃g H) (n : ℕ) : K G n ≃* K H n :=
begin
fapply pequiv.MK,
{ exact EM_functor φ n },
{ exact EM_functor φ⁻¹ᵍ n },
{ intro x, refine (EM_functor_gcompose φ⁻¹ᵍ φ n)⁻¹* x ⬝ _,
refine _ ⬝ EM_functor_gid G n x,
refine EM_functor_phomotopy _ n x,
rexact left_inv φ },
{ intro x, refine (EM_functor_gcompose φ φ⁻¹ᵍ n)⁻¹* x ⬝ _,
refine _ ⬝ EM_functor_gid H n x,
refine EM_functor_phomotopy _ n x,
rexact right_inv φ }
end
definition is_equiv_EM_functor [constructor] {G H : AbGroup} (φ : G →g H) [H2 : is_equiv φ]
(n : ℕ) : is_equiv (EM_functor φ n) :=
to_is_equiv (EM_equiv_EM (isomorphism.mk φ H2) n)
definition fundamental_group_EM1' (G : Group) : G ≃g π₁ (EM1 G) :=
(fundamental_group_EM1 G)⁻¹ᵍ
definition ghomotopy_group_EMadd1' (G : AbGroup) (n : ℕ) : G ≃g πg[n+1] (EMadd1 G n) :=
begin
change G ≃g π₁ (Ω[n] (EMadd1 G n)),
refine _ ⬝g homotopy_group_isomorphism_of_pequiv 0 (loopn_EMadd1_pequiv_EM1 G n),
apply fundamental_group_EM1'
end
definition homotopy_group_functor_EM1_functor {G H : Group} (φ : G →g H) :
π→g[1] (EM1_functor φ) ∘ fundamental_group_EM1' G ~ fundamental_group_EM1' H ∘ φ :=
begin
intro g, apply ap tr, exact !idp_con ⬝ !elim_pth,
end
section
definition ghomotopy_group_EMadd1'_0 (G : AbGroup) :
ghomotopy_group_EMadd1' G 0 ~ fundamental_group_EM1' G :=
begin
refine _ ⬝hty id_compose _,
unfold [ghomotopy_group_EMadd1'],
apply hwhisker_right (fundamental_group_EM1' G),
refine _ ⬝hty trunc_functor_id _ _,
exact trunc_functor_homotopy _ ap1_pid,
end
definition loopn_EMadd1_pequiv_EM1_succ (G : AbGroup) (n : ℕ) :
loopn_EMadd1_pequiv_EM1 G (succ n) ~* (loopn_succ_in (EMadd1 G (succ n)) n)⁻¹ᵉ* ∘*
Ω→[n] (loop_EMadd1 G n) ∘* loopn_EMadd1_pequiv_EM1 G n :=
by reflexivity
-- definition is_trunc_EMadd1' [instance] (G : AbGroup) (n : ℕ) : is_trunc (succ n) (EMadd1 G n) :=
-- is_trunc_EMadd1 G n
definition loop_EMadd1_succ (G : AbGroup) (n : ℕ) :
loop_EMadd1 G (n+1) ~* (loop_ptrunc_pequiv (n+1+1) (psusp (EMadd1 G (n+1))))⁻¹ᵉ* ∘*
freudenthal_pequiv (EMadd1 G (n+1)) (add_mul_le_mul_add n 1 1) ∘*
(ptrunc_pequiv (n+1+1) (EMadd1 G (n+1)))⁻¹ᵉ* :=
by reflexivity
definition ap1_EMadd1_natural {G H : AbGroup} (φ : G →g H) (n : ℕ) :
Ω→ (EMadd1_functor φ (succ n)) ∘* loop_EMadd1 G n ~* loop_EMadd1 H n ∘* EMadd1_functor φ n :=
begin
refine pwhisker_right _ (ap1_phomotopy !EMadd1_functor_succ) ⬝* _,
induction n with n IH,
{ refine pwhisker_left _ !hopf.to_pmap_delooping_pinv ⬝* _ ⬝*
pwhisker_right _ !hopf.to_pmap_delooping_pinv⁻¹*,
refine !loop_psusp_unit_natural⁻¹* ⬝h* _,
apply ap1_psquare,
apply ptr_natural },
{ refine pwhisker_left _ !loop_EMadd1_succ ⬝* _ ⬝* pwhisker_right _ !loop_EMadd1_succ⁻¹*,
refine _ ⬝h* !ap1_ptrunc_functor,
refine (@(ptrunc_pequiv_natural (n+1+1) _) _ _)⁻¹ʰ* ⬝h* _,
refine pwhisker_left _ !to_pmap_freudenthal_pequiv ⬝* _ ⬝*
pwhisker_right _ !to_pmap_freudenthal_pequiv⁻¹*,
apply ptrunc_functor_psquare,
exact !loop_psusp_unit_natural⁻¹* }
end
definition apn_EMadd1_pequiv_EM1_natural {G H : AbGroup} (φ : G →g H) (n : ℕ) :
Ω→[n] (EMadd1_functor φ n) ∘* loopn_EMadd1_pequiv_EM1 G n ~*
loopn_EMadd1_pequiv_EM1 H n ∘* EM1_functor φ :=
begin
induction n with n IH,
{ reflexivity },
{ refine pwhisker_left _ !loopn_EMadd1_pequiv_EM1_succ ⬝* _,
refine _ ⬝* pwhisker_right _ !loopn_EMadd1_pequiv_EM1_succ⁻¹*,
refine _ ⬝h* !loopn_succ_in_inv_natural,
exact IH ⬝h* (apn_psquare n !ap1_EMadd1_natural) }
end
definition homotopy_group_functor_EMadd1_functor {G H : AbGroup} (φ : G →g H) (n : ℕ) :
π→g[n+1] (EMadd1_functor φ n) ∘ ghomotopy_group_EMadd1' G n ~
ghomotopy_group_EMadd1' H n ∘ φ :=
begin
refine hwhisker_left _ (to_fun_isomorphism_trans _ _) ⬝hty _ ⬝hty
hwhisker_right _ (to_fun_isomorphism_trans _ _)⁻¹ʰᵗʸ,
refine _ ⬝htyh (homotopy_group_homomorphism_psquare 1 (apn_EMadd1_pequiv_EM1_natural φ n)),
apply homotopy_group_functor_EM1_functor
end
definition homotopy_group_functor_EMadd1_functor' {G H : AbGroup} (φ : G →g H) (n : ℕ) :
φ ∘ (ghomotopy_group_EMadd1' G n)⁻¹ᵍ ~
(ghomotopy_group_EMadd1' H n)⁻¹ᵍ ∘ π→g[n+1] (EMadd1_functor φ n) :=
(homotopy_group_functor_EMadd1_functor φ n)⁻¹ʰᵗʸʰ
definition EM1_pmap_natural {G H : Group} {X Y : Type*} (f : X →* Y) (eX : G → Ω X)
(eY : H → Ω Y) (rX : Πg h, eX (g * h) = eX g ⬝ eX h) (rY : Πg h, eY (g * h) = eY g ⬝ eY h)
[H1 : is_conn 0 X] [H2 : is_trunc 1 X] [is_conn 0 Y] [is_trunc 1 Y]
(φ : G →g H) (p : hsquare eX eY φ (Ω→ f)) :
psquare (EM1_pmap eX rX) (EM1_pmap eY rY) (EM1_functor φ) f :=
begin
fapply phomotopy.mk,
{ intro x, induction x using EM.set_rec,
{ exact respect_pt f },
{ apply eq_pathover,
refine ap_compose f _ _ ⬝ph _ ⬝hp (ap_compose (EM1_pmap eY rY) _ _)⁻¹,
refine ap02 _ !elim_pth ⬝ph _ ⬝hp ap02 _ !elim_pth⁻¹,
refine _ ⬝hp !elim_pth⁻¹, apply transpose, exact square_of_eq_bot (p g) }},
{ exact !idp_con⁻¹ }
end
definition EM1_pequiv'_natural {G H : Group} {X Y : Type*} (f : X →* Y) (eX : G ≃* Ω X)
(eY : H ≃* Ω Y) (rX : Πg h, eX (g * h) = eX g ⬝ eX h) (rY : Πg h, eY (g * h) = eY g ⬝ eY h)
[H1 : is_conn 0 X] [H2 : is_trunc 1 X] [is_conn 0 Y] [is_trunc 1 Y]
(φ : G →g H) (p : Ω→ f ∘ eX ~ eY ∘ φ) :
f ∘* EM1_pequiv' eX rX ~* EM1_pequiv' eY rY ∘* EM1_functor φ :=
EM1_pmap_natural f eX eY rX rY φ p
definition EM1_pequiv_natural {G H : Group} {X Y : Type*} (f : X →* Y) (eX : G ≃g π₁ X)
(eY : H ≃g π₁ Y) [H1 : is_conn 0 X] [H2 : is_trunc 1 X] [is_conn 0 Y] [is_trunc 1 Y]
(φ : G →g H) (p : π→g[1] f ∘ eX ~ eY ∘ φ) :
f ∘* EM1_pequiv eX ~* EM1_pequiv eY ∘* EM1_functor φ :=
EM1_pequiv'_natural f _ _ _ _ φ
begin
assert p' : ptrunc_functor 0 (Ω→ f) ∘* pequiv_of_isomorphism eX ~*
pequiv_of_isomorphism eY ∘* pmap_of_homomorphism φ, exact phomotopy_of_homotopy p,
exact p' ⬝h* (ptrunc_pequiv_natural 0 (Ω→ f)),
end
definition EM1_pequiv_type_natural {X Y : Type*} (f : X →* Y) [H1 : is_conn 0 X] [H2 : is_trunc 1 X]
[H3 : is_conn 0 Y] [H4 : is_trunc 1 Y] :
f ∘* EM1_pequiv_type X ~* EM1_pequiv_type Y ∘* EM1_functor (π→g[1] f) :=
begin refine EM1_pequiv_natural f _ _ _ _, reflexivity end
definition EM_up_natural {G H : AbGroup} (φ : G →g H) {X Y : Type*} (f : X →* Y) {n : ℕ}
(eX : Ω[succ (succ n)] X ≃* G) (eY : Ω[succ (succ n)] Y ≃* H) (p : φ ∘ eX ~ eY ∘ Ω→[succ (succ n)] f)
: φ ∘ EM_up eX ~ EM_up eY ∘ Ω→[succ n] (Ω→ f) :=
begin
refine _ ⬝htyh p,
exact to_homotopy (phinverse (loopn_succ_in_natural (succ n) f)⁻¹*)
end
definition EMadd1_pmap_natural {G H : AbGroup} {X Y : Type*} (f : X →* Y) (n : ℕ) (eX : Ω[succ n] X ≃* G)
(eY : Ω[succ n] Y ≃* H) (rX : Πp q, eX (p ⬝ q) = eX p * eX q) (rY : Πp q, eY (p ⬝ q) = eY p * eY q)
[H1 : is_conn n X] [H2 : is_trunc (n.+1) X] [H3 : is_conn n Y] [H4 : is_trunc (n.+1) Y]
(φ : G →g H) (p : φ ∘ eX ~ eY ∘ Ω→[succ n] f) :
f ∘* EMadd1_pmap n eX rX ~* EMadd1_pmap n eY rY ∘* EMadd1_functor φ n :=
begin
revert X Y f eX eY rX rY H1 H2 H3 H4 p, induction n with n IH: intros,
{ apply EM1_pmap_natural, exact @hhinverse _ _ _ _ _ _ eX eY p },
{ do 2 rewrite [EMadd1_pmap_succ], refine _ ⬝* pwhisker_left _ !EMadd1_functor_succ⁻¹*,
refine (ptrunc_elim_pcompose ((succ n).+1) _ _)⁻¹* ⬝* _ ⬝*
(ptrunc_elim_ptrunc_functor ((succ n).+1) _ _)⁻¹*,
apply ptrunc_elim_phomotopy,
refine _ ⬝* !psusp_elim_psusp_functor⁻¹*,
refine _ ⬝* psusp_elim_phomotopy (IH _ _ _ _ _ (is_homomorphism_EM_up eX rX) _ (@is_conn_loop _ _ H1)
(@is_trunc_loop _ _ H2) _ _ (EM_up_natural φ f eX eY p)),
apply psusp_elim_natural }
end
definition EMadd1_pequiv'_natural {G H : AbGroup} {X Y : Type*} (f : X →* Y) (n : ℕ) (eX : Ω[succ n] X ≃* G)
(eY : Ω[succ n] Y ≃* H) (rX : Πp q, eX (p ⬝ q) = eX p * eX q) (rY : Πp q, eY (p ⬝ q) = eY p * eY q)
[H1 : is_conn n X] [H2 : is_trunc (n.+1) X] [is_conn n Y] [is_trunc (n.+1) Y]
(φ : G →g H) (p : φ ∘ eX ~ eY ∘ Ω→[succ n] f) :
f ∘* EMadd1_pequiv' n eX rX ~* EMadd1_pequiv' n eY rY ∘* EMadd1_functor φ n :=
begin rexact EMadd1_pmap_natural f n eX eY rX rY φ p end
definition EMadd1_pequiv_natural_local_instance {X : Type*} (n : ℕ) [H : is_trunc (n.+1) X] :
is_set (Ω[succ n] X) :=
@(is_set_loopn (succ n) X) H
local attribute EMadd1_pequiv_natural_local_instance [instance]
definition EMadd1_pequiv_natural {G H : AbGroup} {X Y : Type*} (f : X →* Y) (n : ℕ) (eX : πg[n+1] X ≃g G)
(eY : πg[n+1] Y ≃g H) [H1 : is_conn n X] [H2 : is_trunc (n.+1) X] [H3 : is_conn n Y]
[H4 : is_trunc (n.+1) Y] (φ : G →g H) (p : φ ∘ eX ~ eY ∘ π→g[n+1] f) :
f ∘* EMadd1_pequiv n eX ~* EMadd1_pequiv n eY ∘* EMadd1_functor φ n :=
EMadd1_pequiv'_natural f n
((ptrunc_pequiv 0 (Ω[succ n] X))⁻¹ᵉ* ⬝e* pequiv_of_isomorphism eX)
((ptrunc_pequiv 0 (Ω[succ n] Y))⁻¹ᵉ* ⬝e* pequiv_of_isomorphism eY)
_ _ φ (hhconcat (to_homotopy (phinverse (ptrunc_pequiv_natural 0 (Ω→[succ n] f)))) p)
definition EMadd1_pequiv_succ_natural {G H : AbGroup} {X Y : Type*} (f : X →* Y) (n : ℕ)
(eX : πag[n+2] X ≃g G) (eY : πag[n+2] Y ≃g H) [is_conn (n.+1) X] [is_trunc (n.+2) X]
[is_conn (n.+1) Y] [is_trunc (n.+2) Y] (φ : G →g H) (p : φ ∘ eX ~ eY ∘ π→g[n+2] f) :
f ∘* EMadd1_pequiv_succ n eX ~* EMadd1_pequiv_succ n eY ∘* EMadd1_functor φ (n+1) :=
@(EMadd1_pequiv_natural f (succ n) eX eY) _ _ _ _ φ p
definition EMadd1_pequiv_type_natural {X Y : Type*} (f : X →* Y) (n : ℕ)
[H1 : is_conn (n+1) X] [H2 : is_trunc (n+1+1) X] [H3 : is_conn (n+1) Y] [H4 : is_trunc (n+1+1) Y] :
f ∘* EMadd1_pequiv_type X n ~* EMadd1_pequiv_type Y n ∘* EMadd1_functor (π→g[n+2] f) (succ n) :=
EMadd1_pequiv_succ_natural f n !isomorphism.refl !isomorphism.refl (π→g[n+2] f)
proof λa, idp qed
-- definition EM1_functor_equiv' (X Y : Type*) [H1 : is_conn 0 X] [H2 : is_trunc 1 X]
-- [H3 : is_conn 0 Y] [H4 : is_trunc 1 Y] : (X →* Y) ≃ (π₁ X →g π₁ Y) :=
-- begin
-- fapply equiv.MK,
-- { intro f, exact π→g[1] f },
-- { intro φ, exact EM1_pequiv_type Y ∘* EM1_functor φ ∘* (EM1_pequiv_type X)⁻¹ᵉ* },
-- { intro φ, apply homomorphism_eq,
-- refine homotopy_group_homomorphism_pcompose _ _ _ ⬝hty _,
-- refine hwhisker_left _ (homotopy_group_homomorphism_pcompose _ _ _) ⬝hty _,
-- refine (hassoc _ _ _)⁻¹ʰᵗʸ ⬝hty _, exact sorry },
-- { intro f, apply eq_of_phomotopy, refine !passoc⁻¹* ⬝* _, apply pinv_right_phomotopy_of_phomotopy,
-- exact sorry }
-- end
-- definition EMadd1_functor_equiv' (n : ℕ) (X Y : Type*) [H1 : is_conn (n+1) X] [H2 : is_trunc (n+1+1) X]
-- [H3 : is_conn (n+1) Y] [H4 : is_trunc (n+1+1) Y] : (X →* Y) ≃ (πag[n+2] X →g πag[n+2] Y) :=
-- begin
-- fapply equiv.MK,
-- { intro f, exact π→g[n+2] f },
-- { intro φ, exact EMadd1_pequiv_type Y n ∘* EMadd1_functor φ (n+1) ∘* (EMadd1_pequiv_type X n)⁻¹ᵉ* },
-- { intro φ, apply homomorphism_eq,
-- refine homotopy_group_homomorphism_pcompose _ _ _ ⬝hty _,
-- refine hwhisker_left _ (homotopy_group_homomorphism_pcompose _ _ _) ⬝hty _,
-- intro g, exact sorry },
-- { intro f, apply eq_of_phomotopy, refine !passoc⁻¹* ⬝* _, apply pinv_right_phomotopy_of_phomotopy,
-- exact !EMadd1_pequiv_type_natural⁻¹* }
-- end
-- definition EM_functor_equiv (n : ℕ) (G H : AbGroup) : (G →g H) ≃ (EMadd1 G (n+1) →* EMadd1 H (n+1)) :=
-- begin
-- fapply equiv.MK,
-- { intro φ, exact EMadd1_functor φ (n+1) },
-- { intro f, exact ghomotopy_group_EMadd1 H (n+1) ∘g π→g[n+2] f ∘g (ghomotopy_group_EMadd1 G (n+1))⁻¹ᵍ },
-- { intro f, apply homomorphism_eq, },
-- { }
-- end
-- definition EMadd1_pmap {G : AbGroup} {X : Type*} (n : ℕ)
-- (e : Ω[succ n] X ≃* G)
-- (r : Πp q, e (p ⬝ q) = e p * e q)
-- [H1 : is_conn n X] [H2 : is_trunc (n.+1) X] : EMadd1 G n →* X :=
-- begin
-- revert X e r H1 H2, induction n with n f: intro X e r H1 H2,
-- { exact EM1_pmap e⁻¹ᵉ* (equiv.inv_preserve_binary e concat mul r) },
-- rewrite [EMadd1_succ],
-- exact ptrunc.elim ((succ n).+1)
-- (psusp.elim (f _ (EM_up e) (is_mul_hom_EM_up e r) _ _)),
-- end
-- definition is_set_pmap_ptruncconntype {n : ℕ₋₂} (X Y : (n.+1)-Type*[n]) : is_set (X →* Y) :=
-- begin
-- apply is_trunc_succ_intro,
-- intro f g,
-- apply @(is_trunc_equiv_closed_rev -1 (pmap_eq_equiv f g)),
-- apply is_prop.mk,
-- exact sorry
-- end
end
section category
/- category -/
structure ptruncconntype' (n : ℕ₋₂) : Type :=
(A : Type*)
(H1 : is_conn n A)
(H2 : is_trunc (n+1) A)
attribute ptruncconntype'.A [coercion]
attribute ptruncconntype'.H1 ptruncconntype'.H2 [instance]
definition EM1_pequiv_ptruncconntype' (X : ptruncconntype' 0) : EM1 (πg[1] X) ≃* X :=
@(EM1_pequiv_type X) _ (ptruncconntype'.H2 X)
definition EMadd1_pequiv_ptruncconntype' {n : ℕ} (X : ptruncconntype' (n+1)) :
EMadd1 (πag[n+2] X) (succ n) ≃* X :=
@(EMadd1_pequiv_type X n) _ (ptruncconntype'.H2 X)
open trunc_index
definition is_set_pmap_ptruncconntype {n : ℕ₋₂} (X Y : ptruncconntype' n) : is_set (X →* Y) :=
begin
cases n with n, { exact _ },
cases Y with Y H1 H2, cases Y with Y y₀,
exact is_trunc_pmap_of_is_conn X n -1 (ptrunctype.mk Y _ y₀),
end
open category
definition precategory_ptruncconntype'.{u} [constructor] (n : ℕ₋₂) :
precategory.{u+1 u} (ptruncconntype' n) :=
begin
fapply precategory.mk,
{ exact λX Y, X →* Y },
{ exact is_set_pmap_ptruncconntype },
{ exact λX Y Z g f, g ∘* f },
{ exact λX, pid X },
{ intros, apply eq_of_phomotopy, exact !passoc⁻¹* },
{ intros, apply eq_of_phomotopy, apply pid_pcompose },
{ intros, apply eq_of_phomotopy, apply pcompose_pid }
end
definition cptruncconntype' [constructor] (n : ℕ₋₂) : Precategory :=
precategory.Mk (precategory_ptruncconntype' n)
notation `cType*[`:95 n `]`:0 := cptruncconntype' n
definition tEM1 [constructor] (G : Group) : ptruncconntype' 0 :=
ptruncconntype'.mk (EM1 G) _ !is_trunc_EM1
definition tEM [constructor] (G : AbGroup) (n : ℕ) : ptruncconntype' (n.-1) :=
ptruncconntype'.mk (EM G n) _ !is_trunc_EM
open functor
definition EM1_cfunctor : Grp ⇒ cType*[0] :=
functor.mk
(λG, tEM1 G)
(λG H φ, EM1_functor φ)
begin intro, fapply eq_of_phomotopy, apply EM1_functor_gid end
begin intros, fapply eq_of_phomotopy, apply EM1_functor_gcompose end
definition EM_cfunctor (n : ℕ) : AbGrp ⇒ cType*[n.-1] :=
functor.mk
(λG, tEM G n)
(λG H φ, EM_functor φ n)
begin intro, fapply eq_of_phomotopy, apply EM_functor_gid end
begin intros, fapply eq_of_phomotopy, apply EM_functor_gcompose end
definition homotopy_group_cfunctor : cType*[0] ⇒ Grp :=
functor.mk
(λX, πg[1] X)
(λX Y f, π→g[1] f)
begin intro, apply homomorphism_eq, exact to_homotopy !homotopy_group_functor_pid end
begin intros, apply homomorphism_eq, exact to_homotopy !homotopy_group_functor_compose end
definition ab_homotopy_group_cfunctor (n : ℕ) : cType*[n+2.-1] ⇒ AbGrp :=
functor.mk
(λX, πag[n+2] X)
(λX Y f, π→g[n+2] f)
begin intro, apply homomorphism_eq, exact to_homotopy !homotopy_group_functor_pid end
begin intros, apply homomorphism_eq, exact to_homotopy !homotopy_group_functor_compose end
open nat_trans category
definition is_equivalence_EM1_cfunctor.{u} : is_equivalence EM1_cfunctor.{u} :=
begin
fapply is_equivalence.mk,
{ exact homotopy_group_cfunctor.{u} },
{ fapply natural_iso.mk,
{ fapply nat_trans.mk,
{ intro G, exact (fundamental_group_EM1' G)⁻¹ᵍ },
{ intro G H φ, apply homomorphism_eq, exact hhinverse (homotopy_group_functor_EM1_functor φ) }},
{ intro G, fapply iso.is_iso.mk,
{ exact fundamental_group_EM1' G },
{ apply homomorphism_eq,
exact to_right_inv (equiv_of_isomorphism (fundamental_group_EM1' G)), },
{ apply homomorphism_eq,
exact to_left_inv (equiv_of_isomorphism (fundamental_group_EM1' G)), }}},
{ fapply natural_iso.mk,
{ fapply nat_trans.mk,
{ intro X, exact EM1_pequiv_ptruncconntype' X },
{ intro X Y f, apply eq_of_phomotopy, apply EM1_pequiv_type_natural }},
{ intro X, fapply iso.is_iso.mk,
{ exact (EM1_pequiv_ptruncconntype' X)⁻¹ᵉ* },
{ apply eq_of_phomotopy, apply pleft_inv },
{ apply eq_of_phomotopy, apply pright_inv }}}
end
definition is_equivalence_EM_cfunctor (n : ℕ) : is_equivalence (EM_cfunctor (n+2)) :=
begin
fapply is_equivalence.mk,
{ exact ab_homotopy_group_cfunctor n },
{ fapply natural_iso.mk,
{ fapply nat_trans.mk,
{ intro G, exact (ghomotopy_group_EMadd1' G (n+1))⁻¹ᵍ },
{ intro G H φ, apply homomorphism_eq, exact homotopy_group_functor_EMadd1_functor' φ (n+1) }},
{ intro G, fapply iso.is_iso.mk,
{ exact ghomotopy_group_EMadd1' G (n+1) },
{ apply homomorphism_eq,
exact to_right_inv (equiv_of_isomorphism (ghomotopy_group_EMadd1' G (n+1))), },
{ apply homomorphism_eq,
exact to_left_inv (equiv_of_isomorphism (ghomotopy_group_EMadd1' G (n+1))), }}},
{ fapply natural_iso.mk,
{ fapply nat_trans.mk,
{ intro X, exact EMadd1_pequiv_ptruncconntype' X },
{ intro X Y f, apply eq_of_phomotopy, apply EMadd1_pequiv_type_natural }},
{ intro X, fapply iso.is_iso.mk,
{ exact (EMadd1_pequiv_ptruncconntype' X)⁻¹ᵉ* },
{ apply eq_of_phomotopy, apply pleft_inv },
{ apply eq_of_phomotopy, apply pright_inv }}}
end
definition Grp_equivalence_cptruncconntype'.{u} [constructor] : Grp.{u} ≃c cType*[0] :=
equivalence.mk EM1_cfunctor.{u} is_equivalence_EM1_cfunctor.{u}
definition AbGrp_equivalence_cptruncconntype' [constructor] (n : ℕ) : AbGrp ≃c cType*[n+2.-1] :=
equivalence.mk (EM_cfunctor (n+2)) (is_equivalence_EM_cfunctor n)
end category
/- move -/
-- switch arguments in homotopy_group_trunc_of_le
lemma ghomotopy_group_trunc_of_le (k n : ℕ) (A : Type*) [Hk : is_succ k] (H : k ≤[ℕ] n)
: πg[k] (ptrunc n A) ≃g πg[k] A :=
begin
exact sorry
end
lemma homotopy_group_isomorphism_of_ptrunc_pequiv {A B : Type*}
(n k : ℕ) (H : n+1 ≤[ℕ] k) (f : ptrunc k A ≃* ptrunc k B) : πg[n+1] A ≃g πg[n+1] B :=
(ghomotopy_group_trunc_of_le _ k A H)⁻¹ᵍ ⬝g
homotopy_group_isomorphism_of_pequiv n f ⬝g
ghomotopy_group_trunc_of_le _ k B H
open trunc_index
lemma minus_two_add_plus_two (n : ℕ₋₂) : -2+2+n = n :=
by induction n with n p; reflexivity; exact ap succ p
definition is_trunc_succ_of_is_trunc_loop (n : ℕ₋₂) (A : Type*) (H : is_trunc (n.+1) (Ω A))
(H2 : is_conn 0 A) : is_trunc (n.+2) A :=
begin
apply is_trunc_succ_of_is_trunc_loop, apply minus_one_le_succ,
refine is_conn.elim -1 _ _, exact H
end
lemma is_trunc_of_is_trunc_loopn (m n : ℕ) (A : Type*) (H : is_trunc n (Ω[m] A))
(H2 : is_conn m A) : is_trunc (m + n) A :=
begin
revert A H H2; induction m with m IH: intro A H H2,
{ rewrite [nat.zero_add], exact H },
rewrite [succ_add],
apply is_trunc_succ_of_is_trunc_loop,
{ apply IH,
{ apply is_trunc_equiv_closed _ !loopn_succ_in },
apply is_conn_loop },
exact is_conn_of_le _ (zero_le_of_nat (succ m))
end
lemma is_trunc_of_is_set_loopn (m : ℕ) (A : Type*) (H : is_set (Ω[m] A))
(H2 : is_conn m A) : is_trunc m A :=
is_trunc_of_is_trunc_loopn m 0 A H H2
definition pequiv_EMadd1_of_loopn_pequiv_EM1 {G : AbGroup} {X : Type*} (n : ℕ) (e : Ω[n] X ≃* EM1 G)
[H1 : is_conn n X] : X ≃* EMadd1 G n :=
begin
symmetry, apply EMadd1_pequiv,
refine isomorphism_of_eq (ap (λx, πg[x+1] X) !zero_add⁻¹) ⬝g homotopy_group_add X 0 n ⬝g _ ⬝g
!fundamental_group_EM1,
exact homotopy_group_isomorphism_of_pequiv 0 e,
refine is_trunc_of_is_trunc_loopn n 1 X _ _,
apply is_trunc_equiv_closed_rev 1 e
end
definition EM1_pequiv_EM1 {G H : Group} (φ : G ≃g H) : EM1 G ≃* EM1 H :=
sorry
definition EMadd1_pequiv_EMadd1 (n : ℕ) {G H : AbGroup} (φ : G ≃g H) : EMadd1 G n ≃* EMadd1 H n :=
sorry
/- Eilenberg MacLane spaces are the fibers of the Postnikov system of a type -/
definition postnikov_map [constructor] (A : Type*) (n : ℕ₋₂) : ptrunc (n.+1) A →* ptrunc n A :=
ptrunc.elim (n.+1) !ptr
open fiber EM.ops
-- definition loopn_succ_pfiber_postnikov_map (A : Type*) (k : ℕ) (n : ℕ₋₂) :
-- Ω[k+1] (pfiber (postnikov_map A (n.+1))) ≃* Ω[k] (pfiber (postnikov_map (Ω A) n)) :=
-- begin
-- exact sorry
-- end
-- definition loopn_pfiber_postnikov_map (A : Type*) (n : ℕ) :
-- Ω[n] (pfiber (postnikov_map A n)) ≃* EM1 (πg[n+1] A) :=
-- begin
-- revert A, induction n with n IH: intro A,
-- { apply pfiber_postnikov_map_zero },
-- exact loopn_succ_pfiber_postnikov_map A n n ⬝e* IH (Ω A) ⬝e*
-- EM1_pequiv_EM1 !ghomotopy_group_succ_in⁻¹ᵍ
-- end
-- move
definition pgroup_of_Group (X : Group) : pgroup X :=
pgroup_of_group _ idp
open prod chain_complex succ_str fin
definition isomorphism_of_trivial_LES {A B : Type*} (f : A →* B) (n : ℕ)
(k : fin (nat.succ 2)) (HX1 : is_contr (homotopy_groups f (n+1, k)))
(HX2 : is_contr (homotopy_groups f (n+2, k))) :
Group_LES_of_homotopy_groups f (@S +3ℕ (S (n, k))) ≃g Group_LES_of_homotopy_groups f (S (n, k)) :=
begin
induction k with k Hk,
cases k with k, rotate 1, cases k with k, rotate 1, cases k with k, rotate 1,
exfalso, apply lt_le_antisymm Hk, apply le_add_left,
all_goals exact let k := fin.mk _ Hk in let x : +3ℕ := (n, k) in let S : +3ℕ → +3ℕ := succ_str.S in
let z :=
@is_equiv_of_trivial _
(LES_of_homotopy_groups f) _
(is_exact_LES_of_homotopy_groups f (n+1, k))
(is_exact_LES_of_homotopy_groups f (S (n+1, k)))
HX1 HX2
(pgroup_of_Group (Group_LES_of_homotopy_groups f (S x)))
(pgroup_of_Group (Group_LES_of_homotopy_groups f (S (S x))))
(homomorphism.struct (homomorphism_LES_of_homotopy_groups_fun f (S x))) in
isomorphism.mk (homomorphism_LES_of_homotopy_groups_fun f _) z
end
definition pfiber_postnikov_map_zero (A : Type*) :
pfiber (postnikov_map A 0) ≃* EM1 (πg[1] A) :=
begin
symmetry, apply EM1_pequiv,
{ symmetry, note z := isomorphism_of_trivial_LES (postnikov_map A 0) 1 0
(trivial_homotopy_group_of_is_trunc (ptrunc 0 A) !zero_lt_succ)
(trivial_homotopy_group_of_is_trunc (ptrunc 0 A) !zero_lt_succ), exact sorry
-- rexact isomorphism_of_equiv (equiv_of_isomorphism z) sorry
},
{ apply @is_conn_fun_trunc_elim, apply is_conn_fun_tr },
{ apply is_trunc_pfiber }
end
definition pfiber_postnikov_map_succ (A : Type*) (n : ℕ) :
pfiber (postnikov_map A (n+1)) ≃* EMadd1 (πag[n+2] A) (n+1) :=
begin
apply pequiv_EMadd1_of_loopn_pequiv_EM1,
{ exact sorry },
{ apply is_conn_fun_trunc_elim, apply is_conn_fun_tr }
end
end EM
|
4c54e93b94453d94897a9eeeac2d776e629b631b | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /src/Init/Lean/Elab/Declaration.lean | 52ef36fab775e02fe620ffbc833e636fefbe404c | [
"Apache-2.0"
] | permissive | mhuisi/lean4 | 28d35a4febc2e251c7f05492e13f3b05d6f9b7af | dda44bc47f3e5d024508060dac2bcb59fd12e4c0 | refs/heads/master | 1,621,225,489,283 | 1,585,142,689,000 | 1,585,142,689,000 | 250,590,438 | 0 | 2 | Apache-2.0 | 1,602,443,220,000 | 1,585,327,814,000 | C | UTF-8 | Lean | false | false | 6,356 | 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, Sebastian Ullrich
-/
prelude
import Init.Lean.Util.CollectLevelParams
import Init.Lean.Elab.Definition
namespace Lean
namespace Elab
namespace Command
def expandOptDeclSig (stx : Syntax) : Syntax × Option Syntax :=
-- many Term.bracktedBinder >> Term.optType
let binders := stx.getArg 0;
let optType := stx.getArg 1; -- optional (parser! " : " >> termParser)
if optType.isNone then
(binders, none)
else
let typeSpec := optType.getArg 0;
(binders, some $ typeSpec.getArg 1)
def expandDeclSig (stx : Syntax) : Syntax × Syntax :=
-- many Term.bracktedBinder >> Term.typeSpec
let binders := stx.getArg 0;
let typeSpec := stx.getArg 1;
(binders, typeSpec.getArg 1)
def elabAbbrev (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit :=
-- parser! "abbrev " >> declId >> optDeclSig >> declVal
let (binders, type) := expandOptDeclSig (stx.getArg 2);
let modifiers := modifiers.addAttribute { name := `inline };
let modifiers := modifiers.addAttribute { name := `reducible };
elabDefLike {
ref := stx, kind := DefKind.abbrev, modifiers := modifiers,
declId := stx.getArg 1, binders := binders, type? := type, val := stx.getArg 3
}
def elabDef (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit :=
-- parser! "def " >> declId >> optDeclSig >> declVal
let (binders, type) := expandOptDeclSig (stx.getArg 2);
elabDefLike {
ref := stx, kind := DefKind.def, modifiers := modifiers,
declId := stx.getArg 1, binders := binders, type? := type, val := stx.getArg 3
}
def elabTheorem (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit :=
-- parser! "theorem " >> declId >> declSig >> declVal
let (binders, type) := expandDeclSig (stx.getArg 2);
elabDefLike {
ref := stx, kind := DefKind.theorem, modifiers := modifiers,
declId := stx.getArg 1, binders := binders, type? := some type, val := stx.getArg 3
}
def elabConstant (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit := do
-- parser! "constant " >> declId >> declSig >> optional declValSimple
let (binders, type) := expandDeclSig (stx.getArg 2);
val ← match (stx.getArg 3).getOptional? with
| some val => pure val
| none => do {
val ← `(arbitrary _);
pure $ Syntax.node `Lean.Parser.Command.declValSimple #[ mkAtomFrom stx ":=", val ]
};
elabDefLike {
ref := stx, kind := DefKind.opaque, modifiers := modifiers,
declId := stx.getArg 1, binders := binders, type? := some type, val := val
}
def elabInstance (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit := do
-- parser! "instance " >> optional declId >> declSig >> declVal
let (binders, type) := expandDeclSig (stx.getArg 2);
let modifiers := modifiers.addAttribute { name := `instance };
declId ← match (stx.getArg 1).getOptional? with
| some declId => pure declId
| none => throwError stx "not implemented yet";
elabDefLike {
ref := stx, kind := DefKind.def, modifiers := modifiers,
declId := declId, binders := binders, type? := type, val := stx.getArg 3
}
def elabExample (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit :=
-- parser! "example " >> declSig >> declVal
let (binders, type) := expandDeclSig (stx.getArg 1);
let id := mkIdentFrom stx `_example;
let declId := Syntax.node `Lean.Parser.Command.declId #[id, mkNullNode];
elabDefLike {
ref := stx, kind := DefKind.example, modifiers := modifiers,
declId := declId, binders := binders, type? := some type, val := stx.getArg 2
}
def elabAxiom (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit :=
-- parser! "axiom " >> declId >> declSig
let declId := stx.getArg 1;
let (binders, typeStx) := expandDeclSig (stx.getArg 2);
withDeclId declId $ fun name => do
declName ← mkDeclName modifiers name;
applyAttributes stx declName modifiers.attrs AttributeApplicationTime.beforeElaboration;
explictLevelNames ← getLevelNames;
decl ← runTermElabM declName $ fun vars => Term.elabBinders binders.getArgs $ fun xs => do {
type ← Term.elabType typeStx;
Term.synthesizeSyntheticMVars false;
type ← Term.instantiateMVars typeStx type;
type ← Term.mkForall typeStx xs type;
(type, _) ← Term.mkForallUsedOnly typeStx vars type;
type ← Term.levelMVarToParam type;
let usedParams := (collectLevelParams {} type).params;
let levelParams := sortDeclLevelParams explictLevelNames usedParams;
pure $ Declaration.axiomDecl {
name := declName,
lparams := levelParams,
type := type,
isUnsafe := modifiers.isUnsafe
}
};
addDecl stx decl;
applyAttributes stx declName modifiers.attrs AttributeApplicationTime.afterTypeChecking;
applyAttributes stx declName modifiers.attrs AttributeApplicationTime.afterCompilation
def elabInductive (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit :=
pure () -- TODO
def elabClassInductive (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit :=
pure () -- TODO
def elabStructure (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit :=
pure () -- TODO
@[builtinCommandElab declaration]
def elabDeclaration : CommandElab :=
fun stx => do
modifiers ← elabModifiers (stx.getArg 0);
let decl := stx.getArg 1;
let declKind := decl.getKind;
if declKind == `Lean.Parser.Command.abbrev then
elabAbbrev modifiers decl
else if declKind == `Lean.Parser.Command.def then
elabDef modifiers decl
else if declKind == `Lean.Parser.Command.theorem then
elabTheorem modifiers decl
else if declKind == `Lean.Parser.Command.constant then
elabConstant modifiers decl
else if declKind == `Lean.Parser.Command.instance then
elabInstance modifiers decl
else if declKind == `Lean.Parser.Command.axiom then
elabAxiom modifiers decl
else if declKind == `Lean.Parser.Command.example then
elabExample modifiers decl
else if declKind == `Lean.Parser.Command.inductive then
elabInductive modifiers decl
else if declKind == `Lean.Parser.Command.classInductive then
elabClassInductive modifiers decl
else if declKind == `Lean.Parser.Command.structure then
elabStructure modifiers decl
else
throwError stx "unexpected declaration"
end Command
end Elab
end Lean
|
1b164b8c43b49714dd1560477dd0dbae77180d1d | bae21755a4a03bbe0a5c22e258db8633407711ad | /library/init/category/option.lean | 6ee2bc20876ecef617ad8940c356eacd695a3d9c | [
"Apache-2.0"
] | permissive | nor-code/lean | f437357a8f85db0f06f186fa50fcb1bc75f6b122 | aa306af3d7c47de3c7937c98d3aa919eb8da6f34 | refs/heads/master | 1,662,613,329,886 | 1,586,696,014,000 | 1,586,696,014,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,206 | 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, Sebastian Ullrich
-/
prelude
import init.category.alternative init.category.lift init.category.except
universes u v
structure option_t (m : Type u → Type v) (α : Type u) : Type v :=
(run : m (option α))
namespace option_t
variables {m : Type u → Type v} [monad m] {α β : Type u}
@[inline] protected def bind_cont {α β : Type u} (f : α → option_t m β) : option α → m (option β)
| (some a) := (f a).run
| none := pure none
@[inline] protected def bind (ma : option_t m α) (f : α → option_t m β) : option_t m β :=
⟨ma.run >>= option_t.bind_cont f⟩
@[inline] protected def pure (a : α) : option_t m α :=
⟨pure (some a)⟩
instance : monad (option_t m) :=
{ pure := @option_t.pure _ _, bind := @option_t.bind _ _ }
protected def orelse (ma : option_t m α) (mb : option_t m α) : option_t m α :=
⟨do some a ← ma.run | mb.run,
pure (some a)⟩
@[inline] protected def fail : option_t m α :=
⟨pure none⟩
@[inline] def of_option : option α → option_t m α
| o := ⟨pure o⟩
instance : alternative (option_t m) :=
{ failure := @option_t.fail m _,
orelse := @option_t.orelse m _,
..option_t.monad }
@[inline] protected def lift (ma : m α) : option_t m α :=
⟨some <$> ma⟩
instance : has_monad_lift m (option_t m) :=
⟨@option_t.lift _ _⟩
@[inline] protected def monad_map {m'} [monad m'] {α} (f : ∀ {α}, m α → m' α) : option_t m α → option_t m' α :=
λ x, ⟨f x.run⟩
instance (m') [monad m'] : monad_functor m m' (option_t m) (option_t m') :=
⟨λ α, option_t.monad_map⟩
protected def catch (ma : option_t m α) (handle : unit → option_t m α) : option_t m α :=
⟨do some a ← ma.run | (handle ()).run,
pure a⟩
instance : monad_except unit (option_t m) :=
{ throw := λ _ _, option_t.fail, catch := @option_t.catch _ _ }
instance (m out) [monad_run out m] : monad_run (λ α, out (option α)) (option_t m) :=
⟨λ α, monad_run.run ∘ option_t.run⟩
end option_t
|
a5210d36b64efd31b5af382c8b32431fbcc363ff | da23b545e1653cafd4ab88b3a42b9115a0b1355f | /src/tidy/mk_apps.lean | e0cc2ddc783e9faf32e24c75922480da62837d96 | [] | no_license | minchaowu/lean-tidy | 137f5058896e0e81dae84bf8d02b74101d21677a | 2d4c52d66cf07c59f8746e405ba861b4fa0e3835 | refs/heads/master | 1,585,283,406,120 | 1,535,094,033,000 | 1,535,094,033,000 | 145,945,792 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,259 | lean | import data.list
import data.option
import .lock_tactic_state
open tactic
meta def mk_app_aux : expr → expr → expr → tactic expr
| f (expr.pi n binder_info.default d b) arg := do
infer_type arg >>= unify d,
return $ f arg
| f (expr.pi n binder_info.inst_implicit d b) arg := do
infer_type arg >>= unify d,
return $ f arg -- TODO use typeclass inference?
-- v ← mk_instance d,
-- t ← whnf (b.instantiate_var v),
-- mk_app_aux (f v) t arg
| f (expr.pi n _ d b) arg := do
v ← mk_meta_var d,
t ← whnf (b.instantiate_var v),
mk_app_aux (f v) t arg
| e _ _ := failed
-- TODO check if just the first will suffice
meta def mk_app' (f arg : expr) : tactic expr :=
do r ← to_expr ``(%%f %%arg) <|> (do infer_type f >>= whnf >>= λ t, mk_app_aux f t arg),
instantiate_mvars r
/--
Given an expression `e` and list of expressions `F`, builds all applications of `e` to elements of `F`.
`mk_apps` returns a list of all pairs ``(`(%%e %%f), f)`` which typecheck, for `f` in the list `F`.
-/
meta def mk_apps (e : expr) (F : list expr) : tactic (list (expr × expr)) :=
lock_tactic_state $
do
l ← F.mmap $ λ f, (do r ← try_core (mk_app' e f >>= λ m, return (m, f)), return r.to_list),
return l.join
|
1325118418662a8d327582856c7053ce29108df8 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/monad/coequalizer_auto.lean | be0203395814a0d4d8af6cdb319a4173bea2a0d5 | [] | 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,432 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.limits.shapes.reflexive
import Mathlib.category_theory.limits.preserves.shapes.equalizers
import Mathlib.category_theory.limits.preserves.limits
import Mathlib.category_theory.monad.adjunction
import Mathlib.PostPort
universes v₁ u₁
namespace Mathlib
/-!
# Special coequalizers associated to a monad
Associated to a monad `T : C ⥤ C` we have important coequalizer constructions:
Any algebra is a coequalizer (in the category of algebras) of free algebras. Furthermore, this
coequalizer is reflexive.
In `C`, this cofork diagram is a split coequalizer (in particular, it is still a coequalizer).
This split coequalizer is known as the Beck coequalizer (as it features heavily in Beck's
monadicity theorem).
-/
namespace category_theory
namespace monad
/-!
Show that any algebra is a coequalizer of free algebras.
-/
/-- The top map in the coequalizer diagram we will construct. -/
@[simp] theorem free_coequalizer.top_map_f {C : Type u₁} [category C] {T : C ⥤ C} [monad T]
(X : algebra T) : algebra.hom.f (free_coequalizer.top_map X) = functor.map T (algebra.a X) :=
Eq.refl (functor.map T (algebra.a X))
/-- The bottom map in the coequalizer diagram we will construct. -/
def free_coequalizer.bottom_map {C : Type u₁} [category C] {T : C ⥤ C} [monad T] (X : algebra T) :
functor.obj (free T) (functor.obj T (algebra.A X)) ⟶ functor.obj (free T) (algebra.A X) :=
algebra.hom.mk (nat_trans.app μ_ (algebra.A X))
/-- The cofork map in the coequalizer diagram we will construct. -/
def free_coequalizer.π {C : Type u₁} [category C] {T : C ⥤ C} [monad T] (X : algebra T) :
functor.obj (free T) (algebra.A X) ⟶ X :=
algebra.hom.mk (algebra.a X)
theorem free_coequalizer.condition {C : Type u₁} [category C] {T : C ⥤ C} [monad T]
(X : algebra T) :
free_coequalizer.top_map X ≫ free_coequalizer.π X =
free_coequalizer.bottom_map X ≫ free_coequalizer.π X :=
algebra.hom.ext (free_coequalizer.top_map X ≫ free_coequalizer.π X)
(free_coequalizer.bottom_map X ≫ free_coequalizer.π X) (Eq.symm (algebra.assoc X))
protected instance free_coequalizer.bottom_map.category_theory.is_reflexive_pair {C : Type u₁}
[category C] {T : C ⥤ C} [monad T] (X : algebra T) :
is_reflexive_pair (free_coequalizer.top_map X) (free_coequalizer.bottom_map X) :=
is_reflexive_pair.mk' (functor.map (free T) (nat_trans.app η_ (algebra.A X)))
(algebra.hom.ext
(functor.map (free T) (nat_trans.app η_ (algebra.A X)) ≫ free_coequalizer.top_map X) 𝟙
(id
(eq.mpr
(id
(Eq._oldrec
(Eq.refl
(functor.map T (nat_trans.app η_ (algebra.A X)) ≫ functor.map T (algebra.a X) = 𝟙))
(Eq.symm (functor.map_comp T (nat_trans.app η_ (algebra.A X)) (algebra.a X)))))
(eq.mpr
(id
(Eq._oldrec
(Eq.refl (functor.map T (nat_trans.app η_ (algebra.A X) ≫ algebra.a X) = 𝟙))
(algebra.unit X)))
(eq.mpr
(id (Eq._oldrec (Eq.refl (functor.map T 𝟙 = 𝟙)) (functor.map_id T (algebra.A X))))
(Eq.refl 𝟙))))))
(algebra.hom.ext
(functor.map (free T) (nat_trans.app η_ (algebra.A X)) ≫ free_coequalizer.bottom_map X) 𝟙
(right_unit (functor.obj 𝟭 (algebra.A X))))
/--
Construct the Beck cofork in the category of algebras. This cofork is reflexive as well as a
coequalizer.
-/
@[simp] theorem beck_algebra_cofork_X {C : Type u₁} [category C] {T : C ⥤ C} [monad T]
(X : algebra T) : limits.cocone.X (beck_algebra_cofork X) = X :=
Eq.refl X
/--
The cofork constructed is a colimit. This shows that any algebra is a (reflexive) coequalizer of
free algebras.
-/
def beck_algebra_coequalizer {C : Type u₁} [category C] {T : C ⥤ C} [monad T] (X : algebra T) :
limits.is_colimit (beck_algebra_cofork X) :=
limits.cofork.is_colimit.mk' (beck_algebra_cofork X)
fun (s : limits.cofork (free_coequalizer.top_map X) (free_coequalizer.bottom_map X)) =>
{ val :=
algebra.hom.mk
(nat_trans.app η_ (algebra.A (limits.cocone.X (beck_algebra_cofork X))) ≫
algebra.hom.f (limits.cofork.π s)),
property := sorry }
/-- The Beck cofork is a split coequalizer. -/
def beck_split_coequalizer {C : Type u₁} [category C] {T : C ⥤ C} [monad T] (X : algebra T) :
is_split_coequalizer (functor.map T (algebra.a X)) (nat_trans.app μ_ (algebra.A X))
(algebra.a X) :=
is_split_coequalizer.mk (nat_trans.app η_ (algebra.A X))
(nat_trans.app η_ (functor.obj T (algebra.A X))) sorry (algebra.unit X) sorry sorry
/-- This is the Beck cofork. It is a split coequalizer, in particular a coequalizer. -/
@[simp] theorem beck_cofork_X {C : Type u₁} [category C] {T : C ⥤ C} [monad T] (X : algebra T) :
limits.cocone.X (beck_cofork X) = algebra.A X :=
Eq.refl (algebra.A X)
/-- The Beck cofork is a coequalizer. -/
def beck_coequalizer {C : Type u₁} [category C] {T : C ⥤ C} [monad T] (X : algebra T) :
limits.is_colimit (beck_cofork X) :=
is_split_coequalizer.is_coequalizer (beck_split_coequalizer X)
end Mathlib |
1571b609d0c04325718a789672e59d8bea023d9a | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /test/linarith.lean | 9caa1309e1fd81aa3d92bc59378df4862b3b0410 | [
"Apache-2.0"
] | permissive | agjftucker/mathlib | d634cd0d5256b6325e3c55bb7fb2403548371707 | 87fe50de17b00af533f72a102d0adefe4a2285e8 | refs/heads/master | 1,625,378,131,941 | 1,599,166,526,000 | 1,599,166,526,000 | 160,748,509 | 0 | 0 | Apache-2.0 | 1,544,141,789,000 | 1,544,141,789,000 | null | UTF-8 | Lean | false | false | 10,171 | lean | import tactic.linarith
example {α : Type} (_inst : Π (a : Prop), decidable a)
[linear_ordered_field α]
{a b c : α}
(ha : a < 0)
(hb : ¬b = 0)
(hc' : c = 0)
(h : (1 - a) * (b * b) ≤ 0)
(hc : 0 ≤ 0)
(this : -(a * -b * -b + b * -b + 0) = (1 - a) * (b * b))
(h : (1 - a) * (b * b) ≤ 0) :
0 < 1 - a :=
begin
linarith
end
example (e b c a v0 v1 : ℚ) (h1 : v0 = 5*a) (h2 : v1 = 3*b) (h3 : v0 + v1 + c = 10) :
v0 + 5 + (v1 - 3) + (c - 2) = 10 :=
by linarith
example (u v r s t : ℚ) (h : 0 < u*(t*v + t*r + s)) : 0 < (t*(r + v) + s)*3*u :=
by linarith
example (A B : ℚ) (h : 0 < A * B) : 0 < 8*A*B :=
begin
linarith
end
example (A B : ℚ) (h : 0 < A * B) : 0 < A*8*B :=
begin
linarith
end
example (A B : ℚ) (h : 0 < A * B) : 0 < A*B/8 :=
begin
linarith
end
example (A B : ℚ) (h : 0 < A * B) : 0 < A/8*B :=
begin
linarith
end
example (ε : ℚ) (h1 : ε > 0) : ε / 2 + ε / 3 + ε / 7 < ε :=
by linarith
example (x y z : ℚ) (h1 : 2*x < 3*y) (h2 : -4*x + z/2 < 0)
(h3 : 12*y - z < 0) : false :=
by linarith
example (ε : ℚ) (h1 : ε > 0) : ε / 2 < ε :=
by linarith
example (ε : ℚ) (h1 : ε > 0) : ε / 3 + ε / 3 + ε / 3 = ε :=
by linarith
example (a b c : ℚ) (h2 : b + 2 > 3 + b) : false :=
by linarith {discharger := `[ring SOP]}
example (a b c : ℚ) (h2 : b + 2 > 3 + b) : false :=
by linarith
example (a b c : ℚ) (x y : ℤ) (h1 : x ≤ 3*y) (h2 : b + 2 > 3 + b) : false :=
by linarith {restrict_type := ℚ}
example (g v V c h : ℚ) (h1 : h = 0) (h2 : v = V) (h3 : V > 0) (h4 : g > 0)
(h5 : 0 ≤ c) (h6 : c < 1) :
v ≤ V :=
by linarith
example (x y z : ℚ) (h1 : 2*x + ((-3)*y) < 0) (h2 : (-4)*x + 2*z < 0)
(h3 : 12*y + (-4)* z < 0) (h4 : nat.prime 7) : false :=
by linarith
example (x y z : ℚ) (h1 : 2*1*x + (3)*(y*(-1)) < 0) (h2 : (-2)*x*2 < -(z + z))
(h3 : 12*y + (-4)* z < 0) (h4 : nat.prime 7) : false :=
by linarith
example (x y z : ℤ) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0)
(h3 : 12*y - 4* z < 0) : false :=
by linarith
example (x y z : ℤ) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : x*y < 5)
(h3 : 12*y - 4* z < 0) : false :=
by linarith
example (x y z : ℤ) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : x*y < 5) :
¬ 12*y - 4* z < 0 :=
by linarith
example (w x y z : ℤ) (h1 : 4*x + (-3)*y + 6*w ≤ 0) (h2 : (-1)*x < 0)
(h3 : y < 0) (h4 : w ≥ 0) (h5 : nat.prime x.nat_abs) : false :=
by linarith
example (a b c : ℚ) (h1 : a > 0) (h2 : b > 5) (h3 : c < -10)
(h4 : a + b - c < 3) : false :=
by linarith
example (a b c : ℚ) (h2 : b > 0) (h3 : ¬ b ≥ 0) : false :=
by linarith
example (a b c : ℚ) (h2 : (2 : ℚ) > 3) : a + b - c ≥ 3 :=
by linarith {exfalso := ff}
example (x : ℚ) (hx : x > 0) (h : x.num < 0) : false :=
by linarith [rat.num_pos_iff_pos.mpr hx, h]
example (x : ℚ) (hx : x > 0) (h : x.num < 0) : false :=
by linarith only [rat.num_pos_iff_pos.mpr hx, h]
example (x y z : ℚ) (hx : x ≤ 3*y) (h2 : y ≤ 2*z) (h3 : x ≥ 6*z) : x = 3*y :=
by linarith
example (x y z : ℕ) (hx : x ≤ 3*y) (h2 : y ≤ 2*z) (h3 : x ≥ 6*z) : x = 3*y :=
by linarith
example (x y z : ℚ) (hx : ¬ x > 3*y) (h2 : ¬ y > 2*z) (h3 : x ≥ 6*z) : x = 3*y :=
by linarith
example (h1 : (1 : ℕ) < 1) : false :=
by linarith
example (a b c : ℚ) (h2 : b > 0) (h3 : b < 0) : nat.prime 10 :=
by linarith
example (a b c : ℕ) : a + b ≥ a :=
by linarith
example (a b c : ℕ) : ¬ a + b < a :=
by linarith
example (x y : ℚ) (h : 6 + ((x + 4) * x + (6 + 3 * y) * y) = 3) (h' : (x + 4) * x ≥ 0)
(h'' : (6 + 3 * y) * y ≥ 0) : false :=
by linarith
example (x y : ℚ)
(h : 6 + ((x + 4) * x + (6 + 3 * y) * y) = 3 ∧ (x + 4) * x ≥ 0 ∧ (6 + 3 * y) * y ≥ 0) : false :=
by linarith
example (a b i : ℕ) (h1 : ¬ a < i) (h2 : b < i) (h3 : a ≤ b) : false :=
by linarith
example (n : ℕ) (h1 : n ≤ 3) (h2 : n > 2) : n = 3 := by linarith
example (z : ℕ) (hz : ¬ z ≥ 2) (h2 : ¬ z + 1 ≤ 2) : false :=
by linarith
example (z : ℕ) (hz : ¬ z ≥ 2) : z + 1 ≤ 2 :=
by linarith
example (a b c : ℚ) (h1 : 1 / a < b) (h2 : b < c) : 1 / a < c :=
by linarith
example
(N : ℕ) (n : ℕ) (Hirrelevant : n > N)
(A : ℚ) (l : ℚ) (h : A - l ≤ -(A - l)) (h_1 : ¬A ≤ -A) (h_2 : ¬l ≤ -l)
(h_3 : -(A - l) < 1) : A < l + 1 := by linarith
example (d : ℚ) (q n : ℕ) (h1 : ((q : ℚ) - 1)*n ≥ 0) (h2 : d = 2/3*(((q : ℚ) - 1)*n)) :
d ≤ ((q : ℚ) - 1)*n :=
by linarith
example (d : ℚ) (q n : ℕ) (h1 : ((q : ℚ) - 1)*n ≥ 0) (h2 : d = 2/3*(((q : ℚ) - 1)*n)) :
((q : ℚ) - 1)*n - d = 1/3 * (((q : ℚ) - 1)*n) :=
by linarith
example (a : ℚ) (ha : 0 ≤ a) : 0 * 0 ≤ 2 * a :=
by linarith
example (x : ℚ) : id x ≥ x :=
by success_if_fail {linarith}; linarith!
example (x y z : ℚ) (hx : x < 5) (hx2 : x > 5) (hy : y < 5000000000) (hz : z > 34*y) : false :=
by linarith only [hx, hx2]
example (x y z : ℚ) (hx : x < 5) (hy : y < 5000000000) (hz : z > 34*y) : x ≤ 5 :=
by linarith only [hx]
example (x y : ℚ) (h : x < y) : x ≠ y := by linarith
example (x y : ℚ) (h : x < y) : ¬ x = y := by linarith
example (u v x y A B : ℚ)
(a : 0 < A)
(a_1 : 0 <= 1 - A)
(a_2 : 0 <= B - 1)
(a_3 : 0 <= B - x)
(a_4 : 0 <= B - y)
(a_5 : 0 <= u)
(a_6 : 0 <= v)
(a_7 : 0 < A - u)
(a_8 : 0 < A - v) :
u * y + v * x + u * v < 3 * A * B :=
by nlinarith
example (u v x y A B : ℚ) : (0 < A) → (A ≤ 1) → (1 ≤ B)
→ (x ≤ B) → ( y ≤ B)
→ (0 ≤ u ) → (0 ≤ v )
→ (u < A) → ( v < A)
→ (u * y + v * x + u * v < 3 * A * B) :=
begin
intros,
nlinarith
end
example (u v x y A B : ℚ)
(a : 0 < A)
(a_1 : 0 <= 1 - A)
(a_2 : 0 <= B - 1)
(a_3 : 0 <= B - x)
(a_4 : 0 <= B - y)
(a_5 : 0 <= u)
(a_6 : 0 <= v)
(a_7 : 0 < A - u)
(a_8 : 0 < A - v) :
(0 < A * A)
-> (0 <= A * (1 - A))
-> (0 <= A * (B - 1))
-> (0 <= A * (B - x))
-> (0 <= A * (B - y))
-> (0 <= A * u)
-> (0 <= A * v)
-> (0 < A * (A - u))
-> (0 < A * (A - v))
-> (0 <= (1 - A) * A)
-> (0 <= (1 - A) * (1 - A))
-> (0 <= (1 - A) * (B - 1))
-> (0 <= (1 - A) * (B - x))
-> (0 <= (1 - A) * (B - y))
-> (0 <= (1 - A) * u)
-> (0 <= (1 - A) * v)
-> (0 <= (1 - A) * (A - u))
-> (0 <= (1 - A) * (A - v))
-> (0 <= (B - 1) * A)
-> (0 <= (B - 1) * (1 - A))
-> (0 <= (B - 1) * (B - 1))
-> (0 <= (B - 1) * (B - x))
-> (0 <= (B - 1) * (B - y))
-> (0 <= (B - 1) * u)
-> (0 <= (B - 1) * v)
-> (0 <= (B - 1) * (A - u))
-> (0 <= (B - 1) * (A - v))
-> (0 <= (B - x) * A)
-> (0 <= (B - x) * (1 - A))
-> (0 <= (B - x) * (B - 1))
-> (0 <= (B - x) * (B - x))
-> (0 <= (B - x) * (B - y))
-> (0 <= (B - x) * u)
-> (0 <= (B - x) * v)
-> (0 <= (B - x) * (A - u))
-> (0 <= (B - x) * (A - v))
-> (0 <= (B - y) * A)
-> (0 <= (B - y) * (1 - A))
-> (0 <= (B - y) * (B - 1))
-> (0 <= (B - y) * (B - x))
-> (0 <= (B - y) * (B - y))
-> (0 <= (B - y) * u)
-> (0 <= (B - y) * v)
-> (0 <= (B - y) * (A - u))
-> (0 <= (B - y) * (A - v))
-> (0 <= u * A)
-> (0 <= u * (1 - A))
-> (0 <= u * (B - 1))
-> (0 <= u * (B - x))
-> (0 <= u * (B - y))
-> (0 <= u * u)
-> (0 <= u * v)
-> (0 <= u * (A - u))
-> (0 <= u * (A - v))
-> (0 <= v * A)
-> (0 <= v * (1 - A))
-> (0 <= v * (B - 1))
-> (0 <= v * (B - x))
-> (0 <= v * (B - y))
-> (0 <= v * u)
-> (0 <= v * v)
-> (0 <= v * (A - u))
-> (0 <= v * (A - v))
-> (0 < (A - u) * A)
-> (0 <= (A - u) * (1 - A))
-> (0 <= (A - u) * (B - 1))
-> (0 <= (A - u) * (B - x))
-> (0 <= (A - u) * (B - y))
-> (0 <= (A - u) * u)
-> (0 <= (A - u) * v)
-> (0 < (A - u) * (A - u))
-> (0 < (A - u) * (A - v))
-> (0 < (A - v) * A)
-> (0 <= (A - v) * (1 - A))
-> (0 <= (A - v) * (B - 1))
-> (0 <= (A - v) * (B - x))
-> (0 <= (A - v) * (B - y))
-> (0 <= (A - v) * u)
-> (0 <= (A - v) * v)
-> (0 < (A - v) * (A - u))
-> (0 < (A - v) * (A - v))
->
u * y + v * x + u * v < 3 * A * B :=
begin
intros,
linarith
end
example (A B : ℚ) : (0 < A) → (1 ≤ B) → (0 < A / 8 * B) :=
begin
intros, nlinarith
end
example (x y : ℚ) : 0 ≤ x ^2 + y ^2 :=
by nlinarith
example (x y : ℚ) : 0 ≤ x*x + y*y :=
by nlinarith
example (x y : ℚ) : x = 0 → y = 0 → x*x + y*y = 0 :=
by intros; nlinarith
lemma norm_eq_zero_iff {x y : ℚ} : x * x + y * y = 0 ↔ x = 0 ∧ y = 0 :=
begin
split,
{ intro, split; nlinarith },
{ intro, nlinarith }
end
lemma norm_nonpos_right {x y : ℚ} (h1 : x * x + y * y ≤ 0) : y = 0 :=
by nlinarith
lemma norm_nonpos_left (x y : ℚ) (h1 : x * x + y * y ≤ 0) : x = 0 :=
by nlinarith
variables {E : Type*} [add_group E]
example (f : ℤ → E) (h : 0 = f 0) : 1 ≤ 2 := by nlinarith
example (a : E) (h : a = a) : 1 ≤ 2 := by nlinarith
-- test that the apply bug doesn't affect linarith preprocessing
constant α : Type
variable [fact false] -- we work in an inconsistent context below
def leα : α → α → Prop := λ a b, ∀ c : α, true
noncomputable instance : discrete_linear_ordered_field α :=
by refine_struct { le := leα }; exact false.elim _inst_2
example (a : α) (ha : a < 2) : a ≤ a :=
by linarith
example (p q r s t u v w : ℕ) (h1 : p + u = q + t) (h2 : r + w = s + v) :
p * r + q * s + (t * w + u * v) = p * s + q * r + (t * v + u * w) :=
by nlinarith
-- Tests involving a norm, including that squares in a type where `pow_two_nonneg` does not apply
-- do not cause an exception
variables {R : Type*} [ring R] (abs : R → ℚ)
lemma abs_nonneg' : ∀ r, 0 ≤ abs r := false.elim _inst_2
example (t : R) (a b : ℚ) (h : a ≤ b) : abs (t^2) * a ≤ abs (t^2) * b :=
by nlinarith [abs_nonneg' abs (t^2)]
example (t : R) (a b : ℚ) (h : a ≤ b) : a ≤ abs (t^2) + b :=
by linarith [abs_nonneg' abs (t^2)]
example (t : R) (a b : ℚ) (h : a ≤ b) : abs t * a ≤ abs t * b :=
by nlinarith [abs_nonneg' abs t]
constant T : Type
attribute [instance]
constant T_zero : ordered_ring T
namespace T
lemma zero_lt_one : (0 : T) < 1 := false.elim _inst_2
lemma works {a b : ℕ} (hab : a ≤ b) (h : b < a) : false :=
begin
linarith,
end
end T
example (a b c : ℚ) (h : a ≠ b) (h3 : b ≠ c) (h2 : a ≥ b) : b ≠ c :=
by linarith {split_ne := tt}
example (a b c : ℚ) (h : a ≠ b) (h2 : a ≥ b) (h3 : b ≠ c) : a > b :=
by linarith {split_ne := tt}
|
89f13fa160b6d966086be5fca277621c930b75d0 | 1446f520c1db37e157b631385707cc28a17a595e | /stage0/src/Init/Data/List/Basic.lean | 0a71a5396b57a3859f9504e26102d48eb2545390 | [
"Apache-2.0"
] | permissive | bdbabiak/lean4 | cab06b8a2606d99a168dd279efdd404edb4e825a | 3f4d0d78b2ce3ef541cb643bbe21496bd6b057ac | refs/heads/master | 1,615,045,275,530 | 1,583,793,696,000 | 1,583,793,696,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 11,165 | 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.Core
import Init.Data.Nat.Basic
open Decidable List
universes u v w
instance (α : Type u) : Inhabited (List α) :=
⟨List.nil⟩
variables {α : Type u} {β : Type v} {γ : Type w}
namespace List
protected def hasDecEq [DecidableEq α] : ∀ (a b : List α), Decidable (a = b)
| [], [] => isTrue rfl
| a::as, [] => isFalse (fun h => List.noConfusion h)
| [], b::bs => isFalse (fun h => List.noConfusion h)
| a::as, b::bs =>
match decEq a b with
| isTrue hab =>
match hasDecEq as bs with
| isTrue habs => isTrue (Eq.subst hab (Eq.subst habs rfl))
| isFalse nabs => isFalse (fun h => List.noConfusion h (fun _ habs => absurd habs nabs))
| isFalse nab => isFalse (fun h => List.noConfusion h (fun hab _ => absurd hab nab))
instance [DecidableEq α] : DecidableEq (List α) :=
List.hasDecEq
def reverseAux : List α → List α → List α
| [], r => r
| a::l, r => reverseAux l (a::r)
def reverse : List α → List α :=
fun l => reverseAux l []
protected def append (as bs : List α) : List α :=
reverseAux as.reverse bs
instance : HasAppend (List α) :=
⟨List.append⟩
theorem reverseAuxReverseAuxNil : ∀ (as bs : List α), reverseAux (reverseAux as bs) [] = reverseAux bs as
| [], bs => rfl
| a::as, bs =>
show reverseAux (reverseAux as (a::bs)) [] = reverseAux bs (a::as) from
reverseAuxReverseAuxNil as (a::bs)
theorem nilAppend (as : List α) : [] ++ as = as :=
rfl
theorem appendNil (as : List α) : as ++ [] = as :=
show reverseAux (reverseAux as []) [] = as from
reverseAuxReverseAuxNil as []
theorem reverseAuxReverseAux : ∀ (as bs cs : List α), reverseAux (reverseAux as bs) cs = reverseAux bs (reverseAux (reverseAux as []) cs)
| [], bs, cs => rfl
| a::as, bs, cs =>
Eq.trans
(reverseAuxReverseAux as (a::bs) cs)
(congrArg (fun b => reverseAux bs b) (reverseAuxReverseAux as [a] cs).symm)
theorem consAppend (a : α) (as bs : List α) : (a::as) ++ bs = a::(as ++ bs) :=
reverseAuxReverseAux as [a] bs
theorem appendAssoc : ∀ (as bs cs : List α), (as ++ bs) ++ cs = as ++ (bs ++ cs)
| [], bs, cs => rfl
| a::as, bs, cs =>
show ((a::as) ++ bs) ++ cs = (a::as) ++ (bs ++ cs) from
have h₁ : ((a::as) ++ bs) ++ cs = a::(as++bs) ++ cs from congrArg (fun ds => ds ++ cs) (consAppend a as bs);
have h₂ : a::(as++bs) ++ cs = a::((as++bs) ++ cs) from consAppend a (as++bs) cs;
have h₃ : a::((as++bs) ++ cs) = a::(as ++ (bs ++ cs)) from congrArg (fun as => a::as) (appendAssoc as bs cs);
have h₄ : a::(as ++ (bs ++ cs)) = (a::as ++ (bs ++ cs)) from (consAppend a as (bs++cs)).symm;
Eq.trans (Eq.trans (Eq.trans h₁ h₂) h₃) h₄
instance : HasEmptyc (List α) :=
⟨List.nil⟩
protected def erase {α} [HasBeq α] : List α → α → List α
| [], b => []
| a::as, b => match a == b with
| true => as
| false => a :: erase as b
def eraseIdx : List α → Nat → List α
| [], _ => []
| a::as, 0 => as
| a::as, n+1 => a :: eraseIdx as n
def lengthAux : List α → Nat → Nat
| [], n => n
| a::as, n => lengthAux as (n+1)
def length (as : List α) : Nat :=
lengthAux as 0
def isEmpty : List α → Bool
| [] => true
| _ :: _ => false
def set : List α → Nat → α → List α
| a::as, 0, b => b::as
| a::as, n+1, b => a::(set as n b)
| [], _, _ => []
@[specialize] def map (f : α → β) : List α → List β
| [] => []
| a::as => f a :: map as
@[specialize] def map₂ (f : α → β → γ) : List α → List β → List γ
| [], _ => []
| _, [] => []
| a::as, b::bs => f a b :: map₂ as bs
def join : List (List α) → List α
| [] => []
| a :: as => a ++ join as
@[specialize] def filterMap (f : α → Option β) : List α → List β
| [] => []
| a::as =>
match f a with
| none => filterMap as
| some b => b :: filterMap as
@[specialize] def filterAux (p : α → Bool) : List α → List α → List α
| [], rs => rs.reverse
| a::as, rs => match p a with
| true => filterAux as (a::rs)
| false => filterAux as rs
@[inline] def filter (p : α → Bool) (as : List α) : List α :=
filterAux p as []
@[specialize] def partitionAux (p : α → Bool) : List α → List α × List α → List α × List α
| [], (bs, cs) => (bs.reverse, cs.reverse)
| a::as, (bs, cs) =>
match p a with
| true => partitionAux as (a::bs, cs)
| false => partitionAux as (bs, a::cs)
@[inline] def partition (p : α → Bool) (as : List α) : List α × List α :=
partitionAux p as ([], [])
def dropWhile (p : α → Bool) : List α → List α
| [] => []
| a::l => match p a with
| true => dropWhile l
| false => a::l
def find? (p : α → Bool) : List α → Option α
| [] => none
| a::as => match p a with
| true => some a
| false => find? as
def findSome? (f : α → Option β) : List α → Option β
| [] => none
| a::as => match f a with
| some b => some b
| none => findSome? as
def replace [HasBeq α] : List α → α → α → List α
| [], _, _ => []
| a::as, b, c => match a == b with
| true => c::as
| flase => a :: (replace as b c)
def elem [HasBeq α] (a : α) : List α → Bool
| [] => false
| b::bs => match a == b with
| true => true
| false => elem bs
def notElem [HasBeq α] (a : α) (as : List α) : Bool :=
!(as.elem a)
abbrev contains [HasBeq α] (as : List α) (a : α) : Bool :=
elem a as
def eraseDupsAux {α} [HasBeq α] : List α → List α → List α
| [], bs => bs.reverse
| a::as, bs => match bs.elem a with
| true => eraseDupsAux as bs
| false => eraseDupsAux as (a::bs)
def eraseDups {α} [HasBeq α] (as : List α) : List α :=
eraseDupsAux as []
@[specialize] def spanAux (p : α → Bool) : List α → List α → List α × List α
| [], rs => (rs.reverse, [])
| a::as, rs => match p a with
| true => spanAux as (a::rs)
| false => (rs.reverse, a::as)
@[inline] def span (p : α → Bool) (as : List α) : List α × List α :=
spanAux p as []
def lookup [HasBeq α] : α → List (α × β) → Option β
| _, [] => none
| a, (k,b)::es => match a == k with
| true => some b
| false => lookup a es
def removeAll [HasBeq α] (xs ys : List α) : List α :=
xs.filter (fun x => ys.notElem x)
def drop : Nat → List α → List α
| 0, a => a
| n+1, [] => []
| n+1, a::as => drop n as
def take : Nat → List α → List α
| 0, a => []
| n+1, [] => []
| n+1, a::as => a :: take n as
@[specialize] def foldl (f : α → β → α) : forall (init : α), List β → α
| a, [] => a
| a, b :: l => foldl (f a b) l
@[specialize] def foldr (f : α → β → β) (init : β) : List α → β
| [] => init
| a :: l => f a (foldr l)
@[specialize] def foldr1 (f : α → α → α) : ∀ (xs : List α), xs ≠ [] → α
| [], h => absurd rfl h
| [a], _ => a
| a :: as@(_::_), _ => f a (foldr1 as (fun h => List.noConfusion h))
@[specialize] def foldr1Opt (f : α → α → α) : List α → Option α
| [] => none
| a :: as => some $ foldr1 f (a :: as) (fun h => List.noConfusion h)
@[inline] def any (l : List α) (p : α → Bool) : Bool :=
foldr (fun a r => p a || r) false l
@[inline] def all (l : List α) (p : α → Bool) : Bool :=
foldr (fun a r => p a && r) true l
def or (bs : List Bool) : Bool := bs.any id
def and (bs : List Bool) : Bool := bs.all id
def zipWith (f : α → β → γ) : List α → List β → List γ
| x::xs, y::ys => f x y :: zipWith xs ys
| _, _ => []
def zip : List α → List β → List (Prod α β) :=
zipWith Prod.mk
def unzip : List (α × β) → List α × List β
| [] => ([], [])
| (a, b) :: t => match unzip t with | (al, bl) => (a::al, b::bl)
def replicate (n : Nat) (a : α) : List α :=
n.repeat (fun xs => a :: xs) []
def rangeAux : Nat → List Nat → List Nat
| 0, ns => ns
| n+1, ns => rangeAux n (n::ns)
def range (n : Nat) : List Nat :=
rangeAux n []
def iota : Nat → List Nat
| 0 => []
| m@(n+1) => m :: iota n
def enumFrom : Nat → List α → List (Nat × α)
| n, [] => nil
| n, x :: xs => (n, x) :: enumFrom (n + 1) xs
def enum : List α → List (Nat × α) := enumFrom 0
def init : List α → List α
| [] => []
| [a] => []
| a::l => a::init l
def intersperse (sep : α) : List α → List α
| [] => []
| [x] => [x]
| x::xs => x::sep::intersperse xs
def intercalate (sep : List α) (xs : List (List α)) : List α :=
join (intersperse sep xs)
@[inline] protected def bind {α : Type u} {β : Type v} (a : List α) (b : α → List β) : List β :=
join (map b a)
@[inline] protected def pure {α : Type u} (a : α) : List α :=
[a]
inductive Less [HasLess α] : List α → List α → Prop
| nil (b : α) (bs : List α) : Less [] (b::bs)
| head {a : α} (as : List α) {b : α} (bs : List α) : a < b → Less (a::as) (b::bs)
| tail {a : α} {as : List α} {b : α} {bs : List α} : ¬ a < b → ¬ b < a → Less as bs → Less (a::as) (b::bs)
instance [HasLess α] : HasLess (List α) :=
⟨List.Less⟩
instance hasDecidableLt [HasLess α] [h : DecidableRel HasLess.Less] : ∀ (l₁ l₂ : List α), Decidable (l₁ < l₂)
| [], [] => isFalse (fun h => nomatch h)
| [], b::bs => isTrue (Less.nil _ _)
| a::as, [] => isFalse (fun h => nomatch h)
| a::as, b::bs =>
match h a b with
| isTrue h₁ => isTrue (Less.head _ _ h₁)
| isFalse h₁ =>
match h b a with
| isTrue h₂ => isFalse (fun h => match h with
| Less.head _ _ h₁' => absurd h₁' h₁
| Less.tail _ h₂' _ => absurd h₂ h₂')
| isFalse h₂ =>
match hasDecidableLt as bs with
| isTrue h₃ => isTrue (Less.tail h₁ h₂ h₃)
| isFalse h₃ => isFalse (fun h => match h with
| Less.head _ _ h₁' => absurd h₁' h₁
| Less.tail _ _ h₃' => absurd h₃' h₃)
@[reducible] protected def LessEq [HasLess α] (a b : List α) : Prop :=
¬ b < a
instance [HasLess α] : HasLessEq (List α) :=
⟨List.LessEq⟩
instance hasDecidableLe [HasLess α] [h : DecidableRel (HasLess.Less : α → α → Prop)] : ∀ (l₁ l₂ : List α), Decidable (l₁ ≤ l₂) :=
fun a b => Not.Decidable
/-- `isPrefixOf l₁ l₂` returns `true` Iff `l₁` is a prefix of `l₂`. -/
def isPrefixOf [HasBeq α] : List α → List α → Bool
| [], _ => true
| _, [] => false
| a::as, b::bs => a == b && isPrefixOf as bs
/-- `isSuffixOf l₁ l₂` returns `true` Iff `l₁` is a suffix of `l₂`. -/
def isSuffixOf [HasBeq α] (l₁ l₂ : List α) : Bool :=
isPrefixOf l₁.reverse l₂.reverse
@[specialize] def isEqv : List α → List α → (α → α → Bool) → Bool
| [], [], _ => true
| a::as, b::bs, eqv => eqv a b && isEqv as bs eqv
| _, _, eqv => false
protected def beq [HasBeq α] : List α → List α → Bool
| [], [] => true
| a::as, b::bs => a == b && beq as bs
| _, _ => false
instance [HasBeq α] : HasBeq (List α) := ⟨List.beq⟩
end List
|
590dae9368a9ed4f4986905073f1470bebd0c691 | 75c54c8946bb4203e0aaf196f918424a17b0de99 | /old/zfc.lean | 2d137ded6825958acddd7a2eb72dc6ad06312767 | [
"Apache-2.0"
] | permissive | urkud/flypitch | 261e2a45f1038130178575406df8aea78255ba77 | 2250f5eda14b6ef9fc3e4e1f4a9ac4005634de5c | refs/heads/master | 1,653,266,469,246 | 1,577,819,679,000 | 1,577,819,679,000 | 259,862,235 | 1 | 0 | Apache-2.0 | 1,588,147,244,000 | 1,588,147,244,000 | null | UTF-8 | Lean | false | false | 14,862 | lean | /-
Copyright (c) 2019 The Flypitch Project. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jesse Han, Floris van Doorn, Andrew Tindall
-/
import .fol set_theory.zfc
open fol
namespace zfc
inductive ZFC_rel : ℕ → Type 1
| ϵ : ZFC_rel 2
def L_ZFC : Language.{1} :=
⟨λ_, ulift empty, ZFC_rel⟩
/- Note from Mario: should try using L_ZFC' -/
def ZFC_el : L_ZFC.relations 2 := ZFC_rel.ϵ
local infix ` ∈' `:100 := bounded_formula_of_relation ZFC_el
local notation `lift_cast` := by {repeat{apply nat.succ_le_succ}, apply nat.zero_le}
----------------------------------------------------------------------------
def Class : Type 1 := bounded_formula L_ZFC 1
def small {n} (c : bounded_formula L_ZFC (n+1)) : bounded_formula L_ZFC n :=
∃' ∀' (&0 ∈' &1 ⇔ (c ↑' 1 # 1))
def subclass (c₁ c₂ : Class) : sentence L_ZFC := ∀' (c₁ ⟹ c₂)
def functional {n} (c : bounded_formula L_ZFC (n+2)) : bounded_formula L_ZFC n :=
-- ∀x ∃y ∀z, c z x ↔ z = y
∀' ∃' ∀' (c ↑' 1 # 1 ⇔ &0 ≃ &1)
def subset : bounded_formula L_ZFC 2 := ∀' (&0 ∈' &1 ⟹ &0 ∈' &2)
def is_emptyset : bounded_formula L_ZFC 1 := ∼ ∃' (&0 ∈' &1)
def pair : bounded_formula L_ZFC 3 :=
(&0 ≃ &1 : bounded_formula L_ZFC 3) ⊔ (&0 ≃ &2 : bounded_formula L_ZFC 3)
def singl : bounded_formula L_ZFC 2 := &0 ≃ &1
def binary_union : bounded_formula L_ZFC 3 := &0 ∈' &1 ⊔ &0 ∈' &2
def succ : bounded_formula L_ZFC 2 := (&0 ≃ &1 : bounded_formula L_ZFC 2) ⊔ &0 ∈' &1
def axiom_of_extensionality : sentence L_ZFC := ∀' ∀' (∀' (&0 ∈' &1 ⇔ &0 ∈' &2) ⟹ &0 ≃ &1)
def axiom_of_union : sentence L_ZFC := ∀' (small ∃' (&1 ∈' &0 ⊓ &0 ∈' &2))
-- todo: c can have free variables. Note that c y x is interpreted as y is the image of x
def axiom_of_replacement (c : bounded_formula L_ZFC 2) : sentence L_ZFC :=
-- ∀α small (λy, ∃x, x ∈ α ∧ c y x)
functional c ⟹ ∀' (small ∃' (&0 ∈' &2 ⊓ c.cast1))
def axiom_of_powerset : sentence L_ZFC :=
-- the class of all subsets of x is small
∀' small subset
def axiom_of_infinity : sentence L_ZFC :=
--∃ y ((∃ y', y = ∅ ∧ y' ∈ y) ∧ ∀ z (z ∈ y → ∃w(z ∈ w ∧ w ∈ y)))
∃' ((∃' (bd_and (is_emptyset ↑' 1 # 1) (&0 ∈' &1) : bounded_formula L_ZFC 2)) ⊓
∀'(&0 ∈' &1 ⟹ ∃' (bd_and (&1 ∈' &0) (&0 ∈' &2))))
def axiom_of_infinity' : sentence L_ZFC :=
∃' ∀' ((is_emptyset.cast (lift_cast) ⟹ (&0 ∈' &1) : bounded_formula L_ZFC 2) ⊓
(∀' (&0 ∈' &2 ⟹ (∃' ((&1 ∈' &0).cast (lift_cast) ⊓ (&0 ∈' &3) : bounded_formula L_ZFC 4)))))
def axiom_of_emptyset : sentence L_ZFC := small ⊥
-- todo: c can have free variables
def axiom_of_separation (c : Class) : sentence L_ZFC := ∀' (small $ &0 ∈' &1 ⊓ c.cast1)
-- the class consisting of the unordered pair {x, y}
def axiom_of_pairing : sentence L_ZFC := ∀' ∀' small pair
def ZF : Theory L_ZFC :=
{axiom_of_extensionality, axiom_of_union, axiom_of_powerset, axiom_of_infinity} ∪
(λ(c : bounded_formula L_ZFC 2), axiom_of_replacement c) '' set.univ
local notation h :: t := dvector.cons h t
local notation `[` l:(foldr `, ` (h t, dvector.cons h t) dvector.nil `]`) := l
def ordered_pair : bounded_formula L_ZFC 3 :=
∀'(&0 ∈' &1 ⇔ (&0 ≃ &3 : bounded_formula L_ZFC 4) ⊔ ∀'(&0 ∈' &1 ⇔ pair ↑' 1 # 1 ↑' 1 # 1))
--the class consisting of the ordered pair ⟨x, y⟩
def axiom_of_ordered_pairing : sentence L_ZFC := ∀' ∀' small ordered_pair
-- &0 is an ordered pair of &2 and &1 (z = ⟨x, y⟩)
def ordered_pair' : bounded_formula L_ZFC 3 :=
∀'(&0 ∈' &3 ⇔ (&0 ≃ &2 : bounded_formula L_ZFC 4) ⊔ ∀'(&0 ∈' &1 ⇔ (pair ↑' 1 # 1).cast(lift_cast)))
-- &2 is an ordered pair of &1 and &0 (x = ⟨y,z⟩)
-- TODO: Make defns like this unnecessary (current effort is subst_var_* in fol.lean)
def is_ordered_pair : bounded_formula L_ZFC 1 := ∃' ∃' ordered_pair
--&0 is an ordered pair of some two elements--
def relation : bounded_formula L_ZFC 1 := ∀' ((&0 ∈' &1) ⟹ is_ordered_pair ↑' 1 # 1)
--&0 is a relation (is a set of ordered pairs)
def relation' : bounded_formula L_ZFC 1 :=
∀' (((&0 ∈' &1) : bounded_formula L_ZFC 2) ⟹ (is_ordered_pair ⊚ [0]))
def function : bounded_formula L_ZFC 1 :=
relation ⊓
∀'∀'∀'∀'∀'(&1 ∈' &5 ⊓ ordered_pair ↑' 1 # 3 ↑' 1 # 1 ↑' 1 # 0 ⊓
((&0 ∈' &5 : bounded_formula L_ZFC 6) ⊓ (ordered_pair ↑' 1 # 2 ↑' 1 # 1).cast(lift_cast)) ⟹
(&3 ≃ &2 : bounded_formula L_ZFC 6))
-- X is a function iff X is a relation and the following holds:
-- ∀x ∀y ∀z ∀w ∀t, ((w ∈ X) ∧ (w = ⟨x, y⟩) ∧ (z ∈ X) ∧ (z = ⟨x, z⟩ ))) → y = z
def function' : bounded_formula L_ZFC 1 :=
relation ⊓
∀'∀'∀'∀'∀'(((&1 ∈' &5 : bounded_formula L_ZFC 6) ⊓ (ordered_pair ⊚ [1,3,4]) ⊓
((&0 ∈' &5 : bounded_formula L_ZFC 6) ⊓ (ordered_pair ⊚ [0,2,4]))) ⟹
(&3 ≃ &2 : bounded_formula L_ZFC 6))
def fn_app : bounded_formula L_ZFC 3 :=
∃'(&0 ∈' &3 ⊓
∀'(&0 ∈' &1 ⇔ ((&0 ≃ &3) : bounded_formula L_ZFC 5) ⊔ (pair ↑' 1 # 1).cast(lift_cast)))
-- ⟨&1, &0⟩ ∈ &2
-- &0 = &2(&1)
def fn_domain : bounded_formula L_ZFC 2 :=
∀'(&0 ∈' &2 ⇔ ∃'∃'(ordered_pair ↑' 1 # 1 ↑' 1 # 1 ⊓ &0 ∈' &3))
-- &1 is the domain of &0
def fn_range : bounded_formula L_ZFC 2 :=
∀'(&0 ∈' &2 ⇔ ∃'∃'(∀'(&0 ∈' &1 ⇔ (&0 ≃ &2 : bounded_formula L_ZFC 6) ⊔
(pair ↑' 1 # 1).cast(lift_cast)) ⊓ &0 ∈' &3))
--&1 is the range of &0
def axiom_of_choice : sentence L_ZFC :=
∀'∀'(fn_domain ⟹ ∃'∀'(&0 ∈' &2 ⟹∀'(fn_app ↑' 1 # 2 ↑' 1 # 2 ⟹
(∀'(fn_app ↑' 1 # 1 ↑' 1 # 4 ↑' 1 # 4 ⊓ ∃'(&0 ∈' &2)) ⟹ &0 ∈' &1))))
def ZFC : Theory L_ZFC := ZF ∪ {axiom_of_choice}
def inverse_relation : bounded_formula L_ZFC 2 :=
∀'(&0 ∈' &1 ⇔ ∃'∃'(∃'(∀'(&0 ∈' &1 ⇔ (&0 ≃ &2 : bounded_formula L_ZFC 7) ⊔
(pair ↑' 1 # 1).cast(lift_cast)) ⊓ &0 ∈' &5 : bounded_formula L_ZFC 6)) ⊓
(ordered_pair'.cast(lift_cast)))
-- &0 is the inverse relation of &1
def function_one_one : bounded_formula L_ZFC 1 :=
function ⊓ ∀' (inverse_relation ⟹ function.cast(lift_cast))
def irreflexive_relation : bounded_formula L_ZFC 2 :=
(∀'(&0 ∈' &2 ⟹ (∀'(∀'(&0 ∈' &1) ⇔ ((&0 ≃ &2 : bounded_formula L_ZFC 4) ⊔
∀'(&0 ∈' &1 ⇔ (&0 ≃ &3 : bounded_formula L_ZFC 5))) ⟹ ∼(&0 ∈' &3))))) ⊓
relation.cast(lift_cast)
-- &0 is an irreflexive relation on &1
def transitive_relation : bounded_formula L_ZFC 2 :=
((∀'∀'∀'((((&2 ∈' &4 : bounded_formula L_ZFC 5) ⊓ (&1 ∈' &4)) ⊓ (&0 ∈' &4)) ⊓
∃'((ordered_pair ↑' 1 # 1).cast(lift_cast) ⊓ (&0 ∈' &4 : bounded_formula L_ZFC 6)) ⊓
∃'(ordered_pair.cast(lift_cast) ⊓ &0 ∈' &4 : bounded_formula L_ZFC 6) ⟹
∃'((ordered_pair ↑' 1 # 2).cast(lift_cast) ⊓ &0 ∈' &4 : bounded_formula L_ZFC 6)))) ⊓
relation.cast(lift_cast)
--&0 is a transitive relation on &1
-- X Tr Y iff X is a relation and the following holds:
-- ∀u ∀v ∀w, (u ∈ Y ∧ v ∈ Y ∧ w ∈ Y ∧ ⟨u, v⟩ ∈ X ∧ ⟨v,w⟩ ∈ X) → ⟨u,w⟩ ∈ X
def partial_order_zfc : bounded_formula L_ZFC 2 := irreflexive_relation ⊓ transitive_relation
def connected_relation : bounded_formula L_ZFC 2 :=
relation ↑' 1 # 1 ⊓ ∀'∀'((bd_and (bd_and (&0 ∈' &3) (&1 ∈' &3)) ∼(bd_equal &0 &1)) ⟹
∃'((&0 ∈' &3 : bounded_formula L_ZFC 5) ⊓ ((ordered_pair ↑' 1 # 3 ↑' 1 # 3) ⊔
∀'(&0 ∈' &1 ⇔ (&0 ≃ &2 : bounded_formula L_ZFC 6) ⊔ (pair ↑' 1 # 1).cast(lift_cast)))))
--&0 is a connected relation on &1
-- X Con Y iff Rel(X) and ∀u ∀v (u ∈ Y ∧ v ∈ Y ∧ u ≠ v) → ⟨u,v⟩ ∈ X ∨ ⟨v, u⟩ ∈ X
def total_order : bounded_formula L_ZFC 2 :=
irreflexive_relation ⊓ transitive_relation ⊓ connected_relation
def well_order : bounded_formula L_ZFC 2 :=
irreflexive_relation ⊓ ∀'(subset ↑' 1 # 1 ⊓ ∃'(&0 ∈' &1) ⟹ ∃'(&0 ∈' &1 ⊓
∀'(((&0 ∈' &2) ⊓ ∼(&0 ≃ &1 : bounded_formula L_ZFC 5)) ⟹ ∃'(ordered_pair.cast(lift_cast) ⊓
(&0 ∈' &4 : bounded_formula L_ZFC 6)) ⊓ ∼∃'(∀'(&0 ∈' &1 ⇔ (( &0 ≃ &2 : bounded_formula L_ZFC 7) ⊔
(pair ↑' 1 # 1).cast(lift_cast) )) ⊓ &0 ∈' &4))))
-- &0 well-orders &1
def membership_relation : bounded_formula L_ZFC 1 :=
relation ⊓
∀'(&0 ∈' &1 ⇔ ∃'∃'∀'(&0 ∈' &3 ⇔ ((bd_equal &0 &2) ⊔ pair ↑' 1 # 3 ↑' 1 # 3) ⊓ &2 ∈' &1))
-- &0 is E, the membership relation {⟨x,y⟩ | x ∈ y}
def transitive_zfc : bounded_formula L_ZFC 1 := ∀' ((&0 ∈' &1) ⟹ subset)
--&0 is transitive
def fn_zfc_equiv : bounded_formula L_ZFC 3 :=
((function_one_one.cast(lift_cast)) ⊓ (fn_domain ↑' 1 # 1)) ⊓ (fn_range ↑' 1 # 2)
def zfc_equiv : bounded_formula L_ZFC 2 := ∃' fn_zfc_equiv
--&0 ≃ &1, i.e. they are equinumerous
def is_powerset : bounded_formula L_ZFC 2 := ∀' ((&0 ∈' &2) ⇔ subset ↑' 1 # 2)
--&1 is P(&0)
def is_suc_of : bounded_formula L_ZFC 2 :=
∀' ((&0 ∈' &2) ⇔ ((&0 ∈' &1) ⊔ ( &0 ≃ &1 : bounded_formula L_ZFC 3)))
-- &1 = succ(&0)
def is_ordinal : bounded_formula L_ZFC 1 :=
(∀' ((membership_relation.cast(lift_cast)) ⟹ well_order)) ⊓ transitive_zfc
def is_suc_ordinal : bounded_formula L_ZFC 1 := is_ordinal ⊓ ∃' is_suc_of
--&0 is a successor ordinal
def ordinal_lt : bounded_formula L_ZFC 2 :=
(is_ordinal.cast(lift_cast)) ⊓ (is_ordinal ↑' 1 # 0) ⊓ (&0 ∈' &1)
-- &0 < &1
def ordinal_le : bounded_formula L_ZFC 2 := ordinal_lt ⊔ (bd_equal &0 &1)
-- &0 ≤ &1
def is_first_infinite_ordinal : bounded_formula L_ZFC 1 :=
∀' ((&0 ∈' &1) ⇔ (((is_emptyset ⊔ is_suc_ordinal)↑' 1 # 1) ⊓
∀'((&0 ∈' &1) ⟹ ((is_emptyset ⊔ is_suc_ordinal).cast(lift_cast)))))
--&0 = ω
def is_uncountable_ordinal : bounded_formula L_ZFC 1 :=
∀' ((is_first_infinite_ordinal.cast(lift_cast)) ⟹
∀' (subset.cast(lift_cast) ⟹(∼(zfc_equiv ↑' 1 # 1))))
--&0 ≥ ω₁
def is_first_uncountable_ordinal : bounded_formula L_ZFC 1 :=
is_uncountable_ordinal ⊓ (∀' ((is_uncountable_ordinal ↑' 1 # 1) ⟹ ordinal_le))
--&0 = ω₁
/- Statement of CH -/
def continuum_hypothesis : sentence L_ZFC :=
∀' ∀' ((∃'((is_first_infinite_ordinal ↑' 1 # 1 ↑' 1 # 1) ⊓ (is_powerset ↑' 1 # 2)) ⊓
(is_first_uncountable_ordinal ↑' 1 #0)) ⟹ zfc_equiv)
/- ZFC formulae in terms of Mario's Set type.
Many of these are a lot shorter than the formulae above, largely because things like {x} and {{x},{x,y}} don't have to be existentially instantiated. It might be advantageous to refactor these to moatch the FOL formulae more precisely.
(e.g. ({x} ∈ y) vs (∃ w, w = {x} ∧ w ∈ y) -/
def Set_subset : Set → Set → Prop := Set.subset
def Set_is_powerset : Set → Set → Prop := λ x y, ∀ w, w ∈ x ↔ w ⊆ y
def Set_is_emptyset: Set → Prop := λ x, ∀ y, y ∉ x
def Set_pair : Set → Set → Set → Prop := λ x y z, x = {y,z}
def Set_ordered_pair : Set → Set → Set → Prop := λ x y z, x = {{y},{y,z}}
--TODO : angle bracket notation ⟪x,y⟫ = {{x},{x,y}}
def Set_is_ordered_pair: Set → Prop := λ x, ∃ y z, Set_ordered_pair x y z
def Set_relation : Set → Prop := λ x, ∀w, w ∈ x ↔ Set_is_ordered_pair w
def Set_function : Set → Prop :=
λ x, Set_relation x ∧ ∀ a b c, ((({{a},{a,b}} ∈ x) ∧ ({{a},{a,c}} ∈ x)) → (b = c))
def Set_fn_app : Set → Set → Set → Prop := λ x y z, {{x},{x,y}} ∈ z
def Set_fn_domain : Set → Set → Prop := λ x y, ∀ w, w ∈ x ↔ ∃ a b, w = {{a},{a,b}} ∧ a ∈ y
def Set_fn_range: Set → Set → Prop := λ x y, ∀ w, w ∈ x ↔ ∃ a b, w = {{a},{a,b}} ∧ b ∈ y
def Set_inverse_relation : Set → Set → Prop := λ x y, ∀ a b, {{a},{a,b}} ∈ x ↔ {{b},{b,a}} ∈ y
def Set_function_one_one : Set → Prop :=
λ x, Set_function x ∧ ∀ y, ((Set_inverse_relation x y) → Set_function y)
def Set_irreflexive_relation : Set → Set → Prop :=
λ x y, Set_relation x ∧ ∀ z, z ∈ y → {{z},{z}} ∉ x
def Set_transitive_relation : Set → Set → Prop :=
λ x y, ∀ u v w, ((u ∈ y ∧ v ∈ y ∧ w ∈ w ∧ {{u},{u,v}} ∈ x ∧ {{v},{v,w}} ∈ x) → {{u},{u,w}} ∈ x)
def Set_partial_order : Set → Set → Prop :=
λ x y, Set_irreflexive_relation x y ⊓ Set_transitive_relation x y
def Set_connected_relation: Set → Set → Prop :=
λ x y, Set_relation x ∧ ∀ u v, (u ∈ y ∧ v ∈ y ∧ u ≠ v) → ({{u},{u,v}} ∈ x ∨ {{v},{v,u}} ∈ x)
def Set_total_order : Set → Set → Prop :=
λ X Y, (Set_irreflexive_relation X Y) ∧ (Set_transitive_relation X Y) ∧ (Set_connected_relation X Y)
def Set_well_order : Set → Set → Prop :=
λ x y, Set_irreflexive_relation x y ∧
∀ z, (z ⊆ y ∧ (z ≠ ∅) → ∃w, (w ∈ z ∧ ∀ v, ((v ∈ z ∧ v ≠ w) → {{w},{w,v}} ∈ x ∧ {{v},{v,w}} ∉ x)))
def Set_membership_relation : Set → Prop :=
λ x, Set_relation x ∧ ∀ w ∈ x, ∃ u v, {{u},{u,v}} = w ∧ u ∈ v
def Set_transitive : Set → Prop := λ x, ∀ w, w ∈ x → w ⊆ x
def Set_fn_equiv : Set → Set → Set → Prop :=
λ x y z, Set_function_one_one x ∧ Set_fn_domain x y ∧ Set_fn_range x z
def Set_zfc_equiv : Set → Set → Prop := λ x y, ∃ f, Set_fn_equiv f x y
def Set_is_suc_of : Set → Set → Prop := λ x y, ∀ w, (w ∈ x ↔ (w ∈ y ∨ w = y))
def Set_is_ordinal : Set → Prop :=
λ x, (∀ w, (Set_membership_relation w → Set_well_order w x)) ∧ Set_transitive x
def Set_is_suc_ordinal : Set → Prop := λ x, Set_is_ordinal x ∧ ∃ w, Set_is_suc_of x w
def Set_ordinal_lt : Set → Set → Prop := λ x y, Set_is_ordinal x ∧ Set_is_ordinal y ∧ x ∈ y
def Set_ordinal_le : Set → Set → Prop := λ x y, Set_ordinal_lt x y ∨ x = y
def Set_is_first_infinite_ordinal : Set → Prop :=
λ x, ∀ w, w ∈ x ↔ ((w = ∅ ∨ Set_is_suc_ordinal w) ∧ ∀ z, (z ∈ w → (z = ∅ ∨ Set_is_suc_ordinal z)))
def Set_is_uncountable_ordinal : Set → Prop :=
λ x, ∀ w, (Set_is_first_infinite_ordinal w → ∀ z, (Set.subset z w → ¬ Set_zfc_equiv z x))
def Set_is_first_uncountable_ordinal : Set → Prop :=
λ x, Set_is_uncountable_ordinal x ∧ ∀ w, (Set_is_uncountable_ordinal w → Set_ordinal_le x w)
def Set_continuum_hypothesis : Prop :=
∀ x y, ((∃ w, (Set_is_first_infinite_ordinal w ∧ Set_is_powerset x w)) ∧
Set_is_first_uncountable_ordinal y) → Set_zfc_equiv x y
--- shallow ZFC axioms
def Set_axiom_of_infinity : Prop :=
∃ y : Set.{0}, ((∃ x : Set, (Set_is_emptyset x ∧ x ∈ y))∧ (∀ z, z ∈ y → (∃ w, z ∈ w ∧ w ∈ y) ))
def Set_axiom_of_infinity' : Prop :=
∃ y : Set.{0}, ∀ x : Set, ((Set_is_emptyset x → x ∈ y) ∧ ∀ z, z ∈ y → ∃ w, (z ∈ w) ∧ w ∈ y)
end zfc
|
2ada9ad85bf9e7ab124c0c468c82354975cd0245 | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /library/data/quotient/util.lean | 4122e004f4d0cd2751420dc9b4c906c705c6ae69 | [
"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 | 3,971 | lean | /-
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
-/
import logic ..prod algebra.relation
open prod eq.ops
namespace quotient
/- auxiliary facts about products -/
variables {A B : Type}
/- flip -/
definition flip (a : A × B) : B × A := pair (pr2 a) (pr1 a)
theorem flip_def (a : A × B) : flip a = pair (pr2 a) (pr1 a) := rfl
theorem flip_pair (a : A) (b : B) : flip (pair a b) = pair b a := rfl
theorem flip_pr1 (a : A × B) : pr1 (flip a) = pr2 a := rfl
theorem flip_pr2 (a : A × B) : pr2 (flip a) = pr1 a := rfl
theorem flip_flip : Π a : A × B, flip (flip a) = a
| (pair x y) := rfl
theorem P_flip {P : A → B → Prop} (a : A × B) (H : P (pr1 a) (pr2 a))
: P (pr2 (flip a)) (pr1 (flip a)) :=
(flip_pr1 a)⁻¹ ▸ (flip_pr2 a)⁻¹ ▸ H
theorem flip_inj {a b : A × B} (H : flip a = flip b) : a = b :=
have H2 : flip (flip a) = flip (flip b), from congr_arg flip H,
show a = b, from (flip_flip a) ▸ (flip_flip b) ▸ H2
/- coordinatewise unary maps -/
definition map_pair (f : A → B) (a : A × A) : B × B :=
pair (f (pr1 a)) (f (pr2 a))
theorem map_pair_def (f : A → B) (a : A × A)
: map_pair f a = pair (f (pr1 a)) (f (pr2 a)) :=
rfl
theorem map_pair_pair (f : A → B) (a a' : A)
: map_pair f (pair a a') = pair (f a) (f a') :=
(pr1.mk a a') ▸ (pr2.mk a a') ▸ rfl
theorem map_pair_pr1 (f : A → B) (a : A × A) : pr1 (map_pair f a) = f (pr1 a) :=
by esimp
theorem map_pair_pr2 (f : A → B) (a : A × A) : pr2 (map_pair f a) = f (pr2 a) :=
by esimp
/- coordinatewise binary maps -/
definition map_pair2 {A B C : Type} (f : A → B → C) (a : A × A) (b : B × B) : C × C :=
pair (f (pr1 a) (pr1 b)) (f (pr2 a) (pr2 b))
theorem map_pair2_def {A B C : Type} (f : A → B → C) (a : A × A) (b : B × B) :
map_pair2 f a b = pair (f (pr1 a) (pr1 b)) (f (pr2 a) (pr2 b)) := rfl
theorem map_pair2_pair {A B C : Type} (f : A → B → C) (a a' : A) (b b' : B) :
map_pair2 f (pair a a') (pair b b') = pair (f a b) (f a' b') := by esimp
theorem map_pair2_pr1 {A B C : Type} (f : A → B → C) (a : A × A) (b : B × B) :
pr1 (map_pair2 f a b) = f (pr1 a) (pr1 b) := by esimp
theorem map_pair2_pr2 {A B C : Type} (f : A → B → C) (a : A × A) (b : B × B) :
pr2 (map_pair2 f a b) = f (pr2 a) (pr2 b) := by esimp
theorem map_pair2_flip {A B C : Type} (f : A → B → C) : Π (a : A × A) (b : B × B),
flip (map_pair2 f a b) = map_pair2 f (flip a) (flip b)
| (pair a₁ a₂) (pair b₁ b₂) := rfl
-- add_rewrite flip_pr1 flip_pr2 flip_pair
-- add_rewrite map_pair_pr1 map_pair_pr2 map_pair_pair
-- add_rewrite map_pair2_pr1 map_pair2_pr2 map_pair2_pair
theorem map_pair2_comm {A B : Type} {f : A → A → B} (Hcomm : ∀a b : A, f a b = f b a)
: Π (v w : A × A), map_pair2 f v w = map_pair2 f w v
| (pair v₁ v₂) (pair w₁ w₂) :=
!map_pair2_pair ⬝ by rewrite [Hcomm v₁ w₁, Hcomm v₂ w₂] ⬝ !map_pair2_pair⁻¹
theorem map_pair2_assoc {A : Type} {f : A → A → A}
(Hassoc : ∀a b c : A, f (f a b) c = f a (f b c)) (u v w : A × A) :
map_pair2 f (map_pair2 f u v) w = map_pair2 f u (map_pair2 f v w) :=
show pair (f (f (pr1 u) (pr1 v)) (pr1 w)) (f (f (pr2 u) (pr2 v)) (pr2 w)) =
pair (f (pr1 u) (f (pr1 v) (pr1 w))) (f (pr2 u) (f (pr2 v) (pr2 w))),
by rewrite [Hassoc (pr1 u) (pr1 v) (pr1 w), Hassoc (pr2 u) (pr2 v) (pr2 w)]
theorem map_pair2_id_right {A B : Type} {f : A → B → A} {e : B} (Hid : ∀a : A, f a e = a)
: Π (v : A × A), map_pair2 f v (pair e e) = v
| (pair v₁ v₂) :=
!map_pair2_pair ⬝ by rewrite [Hid v₁, Hid v₂]
theorem map_pair2_id_left {A B : Type} {f : B → A → A} {e : B} (Hid : ∀a : A, f e a = a)
: Π (v : A × A), map_pair2 f (pair e e) v = v
| (pair v₁ v₂) :=
!map_pair2_pair ⬝ by rewrite [Hid v₁, Hid v₂]
end quotient
|
68de5cb9020368c2dd701060143eee0908514b53 | 05b503addd423dd68145d68b8cde5cd595d74365 | /src/data/prod.lean | 7915203e85a0243cd8222723ca95196f4895336c | [
"Apache-2.0"
] | permissive | aestriplex/mathlib | 77513ff2b176d74a3bec114f33b519069788811d | e2fa8b2b1b732d7c25119229e3cdfba8370cb00f | refs/heads/master | 1,621,969,960,692 | 1,586,279,279,000 | 1,586,279,279,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,045 | 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
-/
/-!
# Extra facts about `prod`
This file defines `prod.swap : α × β → β × α` and proves various simple lemmas about `prod`.
-/
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
@[simp] theorem prod.forall {p : α × β → Prop} : (∀ x, p x) ↔ (∀ a b, p (a, b)) :=
⟨assume h a b, h (a, b), assume h ⟨a, b⟩, h a b⟩
@[simp] theorem prod.exists {p : α × β → Prop} : (∃ x, p x) ↔ (∃ a b, p (a, b)) :=
⟨assume ⟨⟨a, b⟩, h⟩, ⟨a, b, h⟩, assume ⟨a, b, h⟩, ⟨⟨a, b⟩, h⟩⟩
namespace prod
attribute [simp] prod.map
@[simp] lemma map_fst (f : α → γ) (g : β → δ) : ∀(p : α × β), (map f g p).1 = f (p.1)
| ⟨a, b⟩ := rfl
@[simp] lemma map_snd (f : α → γ) (g : β → δ) : ∀(p : α × β), (map f g p).2 = g (p.2)
| ⟨a, b⟩ := rfl
@[simp] lemma map_fst' (f : α → γ) (g : β → δ) : (prod.fst ∘ map f g) = f ∘ prod.fst :=
funext $ map_fst f g
@[simp] lemma map_snd' (f : α → γ) (g : β → δ) : (prod.snd ∘ map f g) = g ∘ prod.snd :=
funext $ map_snd f g
@[simp] theorem mk.inj_iff {a₁ a₂ : α} {b₁ b₂ : β} : (a₁, b₁) = (a₂, b₂) ↔ (a₁ = a₂ ∧ b₁ = b₂) :=
⟨prod.mk.inj, by cc⟩
lemma ext_iff {p q : α × β} : p = q ↔ p.1 = q.1 ∧ p.2 = q.2 :=
by rw [← @mk.eta _ _ p, ← @mk.eta _ _ q, mk.inj_iff]
lemma ext {α β} {p q : α × β} (h₁ : p.1 = q.1) (h₂ : p.2 = q.2) : p = q :=
ext_iff.2 ⟨h₁, h₂⟩
lemma map_def {f : α → γ} {g : β → δ} : prod.map f g = λ (p : α × β), (f p.1, g p.2) :=
funext (λ p, ext (map_fst f g p) (map_snd f g p))
lemma id_prod : (λ (p : α × α), (p.1, p.2)) = id :=
funext $ λ ⟨a, b⟩, rfl
lemma fst_surjective [h : nonempty β] : function.surjective (@fst α β) :=
λ x, h.elim $ λ y, ⟨⟨x, y⟩, rfl⟩
lemma snd_surjective [h : nonempty α] : function.surjective (@snd α β) :=
λ y, h.elim $ λ x, ⟨⟨x, y⟩, rfl⟩
lemma fst_injective [subsingleton β] : function.injective (@fst α β) :=
λ x y h, ext h (subsingleton.elim _ _)
lemma snd_injective [subsingleton α] : function.injective (@snd α β) :=
λ x y h, ext (subsingleton.elim _ _) h
/-- Swap the factors of a product. `swap (a, b) = (b, a)` -/
def swap : α × β → β × α := λp, (p.2, p.1)
@[simp] lemma swap_swap : ∀ x : α × β, swap (swap x) = x
| ⟨a, b⟩ := rfl
@[simp] lemma fst_swap {p : α × β} : (swap p).1 = p.2 := rfl
@[simp] lemma snd_swap {p : α × β} : (swap p).2 = p.1 := rfl
@[simp] lemma swap_prod_mk {a : α} {b : β} : swap (a, b) = (b, a) := rfl
@[simp] lemma swap_swap_eq : swap ∘ swap = @id (α × β) :=
funext swap_swap
@[simp] lemma swap_left_inverse : function.left_inverse (@swap α β) swap :=
swap_swap
@[simp] lemma swap_right_inverse : function.right_inverse (@swap α β) swap :=
swap_swap
lemma eq_iff_fst_eq_snd_eq : ∀{p q : α × β}, p = q ↔ (p.1 = q.1 ∧ p.2 = q.2)
| ⟨p₁, p₂⟩ ⟨q₁, q₂⟩ := by simp
theorem lex_def (r : α → α → Prop) (s : β → β → Prop)
{p q : α × β} : prod.lex r s p q ↔ r p.1 q.1 ∨ p.1 = q.1 ∧ s p.2 q.2 :=
⟨λ h, by cases h; simp *,
λ h, match p, q, h with
| (a, b), (c, d), or.inl h := lex.left _ _ _ h
| (a, b), (c, d), or.inr ⟨e, h⟩ :=
by change a = c at e; subst e; exact lex.right _ _ h
end⟩
instance lex.decidable [decidable_eq α] [decidable_eq β]
(r : α → α → Prop) (s : β → β → Prop) [decidable_rel r] [decidable_rel s] :
decidable_rel (prod.lex r s) :=
λ p q, decidable_of_decidable_of_iff (by apply_instance) (lex_def r s).symm
end prod
open function
lemma function.injective.prod {f : α → γ} {g : β → δ} (hf : injective f) (hg : injective g) :
injective (λ p : α × β, (f p.1, g p.2)) :=
assume ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, by { simp [prod.mk.inj_iff],exact λ ⟨eq₁, eq₂⟩, ⟨hf eq₁, hg eq₂⟩ }
|
b47d95acd70676bab1bed9790949b0fbc7dc57c3 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/linear_algebra/eigenspace_auto.lean | 985ca178008014f8929b73cf257bb1e22e02240a | [] | 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 | 12,504 | lean | /-
Copyright (c) 2020 Alexander Bentkamp. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Alexander Bentkamp.
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.field_theory.algebraic_closure
import Mathlib.linear_algebra.finsupp
import Mathlib.linear_algebra.matrix
import Mathlib.PostPort
universes v w u_1 u_2
namespace Mathlib
/-!
# Eigenvectors and eigenvalues
This file defines eigenspaces, eigenvalues, and eigenvalues, as well as their generalized
counterparts. We follow Axler's approach [axler2015] because it allows us to derive many properties
without choosing a basis and without using matrices.
An eigenspace of a linear map `f` for a scalar `μ` is the kernel of the map `(f - μ • id)`. The
nonzero elements of an eigenspace are eigenvectors `x`. They have the property `f x = μ • x`. If
there are eigenvectors for a scalar `μ`, the scalar `μ` is called an eigenvalue.
There is no consensus in the literature whether `0` is an eigenvector. Our definition of
`has_eigenvector` permits only nonzero vectors. For an eigenvector `x` that may also be `0`, we
write `x ∈ f.eigenspace μ`.
A generalized eigenspace of a linear map `f` for a natural number `k` and a scalar `μ` is the kernel
of the map `(f - μ • id) ^ k`. The nonzero elements of a generalized eigenspace are generalized
eigenvectors `x`. If there are generalized eigenvectors for a natural number `k` and a scalar `μ`,
the scalar `μ` is called a generalized eigenvalue.
## References
* [Sheldon Axler, *Linear Algebra Done Right*][axler2015]
* https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors
## Tags
eigenspace, eigenvector, eigenvalue, eigen
-/
namespace module
namespace End
/-- The submodule `eigenspace f μ` for a linear map `f` and a scalar `μ` consists of all vectors `x`
such that `f x = μ • x`. (Def 5.36 of [axler2015])-/
def eigenspace {R : Type v} {M : Type w} [comm_ring R] [add_comm_group M] [module R M] (f : End R M)
(μ : R) : submodule R M :=
linear_map.ker (f - coe_fn (algebra_map R (End R M)) μ)
/-- A nonzero element of an eigenspace is an eigenvector. (Def 5.7 of [axler2015]) -/
def has_eigenvector {R : Type v} {M : Type w} [comm_ring R] [add_comm_group M] [module R M]
(f : End R M) (μ : R) (x : M) :=
x ≠ 0 ∧ x ∈ eigenspace f μ
/-- A scalar `μ` is an eigenvalue for a linear map `f` if there are nonzero vectors `x`
such that `f x = μ • x`. (Def 5.5 of [axler2015]) -/
def has_eigenvalue {R : Type v} {M : Type w} [comm_ring R] [add_comm_group M] [module R M]
(f : End R M) (a : R) :=
eigenspace f a ≠ ⊥
theorem mem_eigenspace_iff {R : Type v} {M : Type w} [comm_ring R] [add_comm_group M] [module R M]
{f : End R M} {μ : R} {x : M} : x ∈ eigenspace f μ ↔ coe_fn f x = μ • x :=
sorry
theorem eigenspace_div {K : Type v} {V : Type w} [field K] [add_comm_group V] [vector_space K V]
(f : End K V) (a : K) (b : K) (hb : b ≠ 0) :
eigenspace f (a / b) = linear_map.ker (b • f - coe_fn (algebra_map K (End K V)) a) :=
sorry
theorem eigenspace_aeval_polynomial_degree_1 {K : Type v} {V : Type w} [field K] [add_comm_group V]
[vector_space K V] (f : End K V) (q : polynomial K) (hq : polynomial.degree q = 1) :
eigenspace f (-polynomial.coeff q 0 / polynomial.leading_coeff q) =
linear_map.ker (coe_fn (polynomial.aeval f) q) :=
sorry
theorem ker_aeval_ring_hom'_unit_polynomial {K : Type v} {V : Type w} [field K] [add_comm_group V]
[vector_space K V] (f : End K V) (c : units (polynomial K)) :
linear_map.ker (coe_fn (polynomial.aeval f) ↑c) = ⊥ :=
sorry
theorem aeval_apply_of_has_eigenvector {K : Type v} {V : Type w} [field K] [add_comm_group V]
[vector_space K V] {f : End K V} {p : polynomial K} {μ : K} {x : V}
(h : has_eigenvector f μ x) :
coe_fn (coe_fn (polynomial.aeval f) p) x = polynomial.eval μ p • x :=
sorry
theorem is_root_of_has_eigenvalue {K : Type v} {V : Type w} [field K] [add_comm_group V]
[vector_space K V] {f : End K V} {μ : K} (h : has_eigenvalue f μ) :
polynomial.is_root (minpoly K f) μ :=
sorry
protected theorem is_integral {K : Type v} {V : Type w} [field K] [add_comm_group V]
[vector_space K V] [finite_dimensional K V] (f : End K V) : is_integral K f :=
is_integral_of_noetherian (finite_dimensional.linear_map K V V) f
theorem has_eigenvalue_of_is_root {K : Type v} {V : Type w} [field K] [add_comm_group V]
[vector_space K V] [finite_dimensional K V] {f : End K V} {μ : K}
(h : polynomial.is_root (minpoly K f) μ) : has_eigenvalue f μ :=
sorry
theorem has_eigenvalue_iff_is_root {K : Type v} {V : Type w} [field K] [add_comm_group V]
[vector_space K V] [finite_dimensional K V] {f : End K V} {μ : K} :
has_eigenvalue f μ ↔ polynomial.is_root (minpoly K f) μ :=
{ mp := is_root_of_has_eigenvalue, mpr := has_eigenvalue_of_is_root }
/-- Every linear operator on a vector space over an algebraically closed field has
an eigenvalue. (Lemma 5.21 of [axler2015]) -/
theorem exists_eigenvalue {K : Type v} {V : Type w} [field K] [add_comm_group V] [vector_space K V]
[is_alg_closed K] [finite_dimensional K V] [nontrivial V] (f : End K V) :
∃ (c : K), has_eigenvalue f c :=
sorry
/-- Eigenvectors corresponding to distinct eigenvalues of a linear operator are linearly
independent. (Lemma 5.10 of [axler2015])
We use the eigenvalues as indexing set to ensure that there is only one eigenvector for each
eigenvalue in the image of `xs`. -/
theorem eigenvectors_linear_independent {K : Type v} {V : Type w} [field K] [add_comm_group V]
[vector_space K V] (f : End K V) (μs : set K) (xs : ↥μs → V)
(h_eigenvec : ∀ (μ : ↥μs), has_eigenvector f (↑μ) (xs μ)) : linear_independent K xs :=
sorry
/-- The generalized eigenspace for a linear map `f`, a scalar `μ`, and an exponent `k ∈ ℕ` is the
kernel of `(f - μ • id) ^ k`. (Def 8.10 of [axler2015])-/
def generalized_eigenspace {R : Type v} {M : Type w} [comm_ring R] [add_comm_group M] [module R M]
(f : End R M) (μ : R) (k : ℕ) : submodule R M :=
linear_map.ker ((f - coe_fn (algebra_map R (End R M)) μ) ^ k)
/-- A nonzero element of a generalized eigenspace is a generalized eigenvector.
(Def 8.9 of [axler2015])-/
def has_generalized_eigenvector {R : Type v} {M : Type w} [comm_ring R] [add_comm_group M]
[module R M] (f : End R M) (μ : R) (k : ℕ) (x : M) :=
x ≠ 0 ∧ x ∈ generalized_eigenspace f μ k
/-- A scalar `μ` is a generalized eigenvalue for a linear map `f` and an exponent `k ∈ ℕ` if there
are generalized eigenvectors for `f`, `k`, and `μ`. -/
def has_generalized_eigenvalue {R : Type v} {M : Type w} [comm_ring R] [add_comm_group M]
[module R M] (f : End R M) (μ : R) (k : ℕ) :=
generalized_eigenspace f μ k ≠ ⊥
/-- The generalized eigenrange for a linear map `f`, a scalar `μ`, and an exponent `k ∈ ℕ` is the
range of `(f - μ • id) ^ k`. -/
def generalized_eigenrange {R : Type v} {M : Type w} [comm_ring R] [add_comm_group M] [module R M]
(f : End R M) (μ : R) (k : ℕ) : submodule R M :=
linear_map.range ((f - coe_fn (algebra_map R (End R M)) μ) ^ k)
/-- The exponent of a generalized eigenvalue is never 0. -/
theorem exp_ne_zero_of_has_generalized_eigenvalue {R : Type v} {M : Type w} [comm_ring R]
[add_comm_group M] [module R M] {f : End R M} {μ : R} {k : ℕ}
(h : has_generalized_eigenvalue f μ k) : k ≠ 0 :=
id
fun (ᾰ : k = 0) =>
Eq._oldrec (fun (h : has_generalized_eigenvalue f μ 0) => h linear_map.ker_id) (Eq.symm ᾰ) h
/-- A generalized eigenspace for some exponent `k` is contained in
the generalized eigenspace for exponents larger than `k`. -/
theorem generalized_eigenspace_mono {K : Type v} {V : Type w} [field K] [add_comm_group V]
[vector_space K V] {f : End K V} {μ : K} {k : ℕ} {m : ℕ} (hm : k ≤ m) :
generalized_eigenspace f μ k ≤ generalized_eigenspace f μ m :=
sorry
/-- A generalized eigenvalue for some exponent `k` is also
a generalized eigenvalue for exponents larger than `k`. -/
theorem has_generalized_eigenvalue_of_has_generalized_eigenvalue_of_le {K : Type v} {V : Type w}
[field K] [add_comm_group V] [vector_space K V] {f : End K V} {μ : K} {k : ℕ} {m : ℕ}
(hm : k ≤ m) (hk : has_generalized_eigenvalue f μ k) : has_generalized_eigenvalue f μ m :=
sorry
/-- The eigenspace is a subspace of the generalized eigenspace. -/
theorem eigenspace_le_generalized_eigenspace {K : Type v} {V : Type w} [field K] [add_comm_group V]
[vector_space K V] {f : End K V} {μ : K} {k : ℕ} (hk : 0 < k) :
eigenspace f μ ≤ generalized_eigenspace f μ k :=
generalized_eigenspace_mono (nat.succ_le_of_lt hk)
/-- All eigenvalues are generalized eigenvalues. -/
theorem has_generalized_eigenvalue_of_has_eigenvalue {K : Type v} {V : Type w} [field K]
[add_comm_group V] [vector_space K V] {f : End K V} {μ : K} {k : ℕ} (hk : 0 < k)
(hμ : has_eigenvalue f μ) : has_generalized_eigenvalue f μ k :=
sorry
/-- Every generalized eigenvector is a generalized eigenvector for exponent `findim K V`.
(Lemma 8.11 of [axler2015]) -/
theorem generalized_eigenspace_le_generalized_eigenspace_findim {K : Type v} {V : Type w} [field K]
[add_comm_group V] [vector_space K V] [finite_dimensional K V] (f : End K V) (μ : K) (k : ℕ) :
generalized_eigenspace f μ k ≤ generalized_eigenspace f μ (finite_dimensional.findim K V) :=
ker_pow_le_ker_pow_findim (f - coe_fn (algebra_map K (End K V)) μ) k
/-- Generalized eigenspaces for exponents at least `findim K V` are equal to each other. -/
theorem generalized_eigenspace_eq_generalized_eigenspace_findim_of_le {K : Type v} {V : Type w}
[field K] [add_comm_group V] [vector_space K V] [finite_dimensional K V] (f : End K V) (μ : K)
{k : ℕ} (hk : finite_dimensional.findim K V ≤ k) :
generalized_eigenspace f μ k = generalized_eigenspace f μ (finite_dimensional.findim K V) :=
ker_pow_eq_ker_pow_findim_of_le hk
/-- If `f` maps a subspace `p` into itself, then the generalized eigenspace of the restriction
of `f` to `p` is the part of the generalized eigenspace of `f` that lies in `p`. -/
theorem generalized_eigenspace_restrict {K : Type v} {V : Type w} [field K] [add_comm_group V]
[vector_space K V] (f : End K V) (p : submodule K V) (k : ℕ) (μ : K)
(hfp : ∀ (x : V), x ∈ p → coe_fn f x ∈ p) :
generalized_eigenspace (linear_map.restrict f hfp) μ k =
submodule.comap (submodule.subtype p) (generalized_eigenspace f μ k) :=
sorry
/-- Generalized eigenrange and generalized eigenspace for exponent `findim K V` are disjoint. -/
theorem generalized_eigenvec_disjoint_range_ker {K : Type v} {V : Type w} [field K]
[add_comm_group V] [vector_space K V] [finite_dimensional K V] (f : End K V) (μ : K) :
disjoint (generalized_eigenrange f μ (finite_dimensional.findim K V))
(generalized_eigenspace f μ (finite_dimensional.findim K V)) :=
sorry
/-- The generalized eigenspace of an eigenvalue has positive dimension for positive exponents. -/
theorem pos_findim_generalized_eigenspace_of_has_eigenvalue {K : Type v} {V : Type w} [field K]
[add_comm_group V] [vector_space K V] [finite_dimensional K V] {f : End K V} {k : ℕ} {μ : K}
(hx : has_eigenvalue f μ) (hk : 0 < k) :
0 < finite_dimensional.findim K ↥(generalized_eigenspace f μ k) :=
sorry
/-- A linear map maps a generalized eigenrange into itself. -/
theorem map_generalized_eigenrange_le {K : Type v} {V : Type w} [field K] [add_comm_group V]
[vector_space K V] {f : End K V} {μ : K} {n : ℕ} :
submodule.map f (generalized_eigenrange f μ n) ≤ generalized_eigenrange f μ n :=
sorry
/-- The generalized eigenvectors span the entire vector space (Lemma 8.21 of [axler2015]). -/
theorem supr_generalized_eigenspace_eq_top {K : Type v} {V : Type w} [field K] [add_comm_group V]
[vector_space K V] [is_alg_closed K] [finite_dimensional K V] (f : End K V) :
(supr fun (μ : K) => supr fun (k : ℕ) => generalized_eigenspace f μ k) = ⊤ :=
sorry
end End
end module
protected theorem linear_map.is_integral {K : Type u_1} {V : Type u_2} [field K] [add_comm_group V]
[vector_space K V] [finite_dimensional K V] (f : linear_map K V V) : is_integral K f :=
module.End.is_integral f
end Mathlib |
5a1c6334a2b06d63f144a982777d3c4d1dfb19df | 94e33a31faa76775069b071adea97e86e218a8ee | /src/data/dfinsupp/basic.lean | 590b1b7ec21d4086ba13cbc55a368835ce6a6ecb | [
"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 | 81,044 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Kenny Lau
-/
import algebra.module.pi
import algebra.module.linear_map
import algebra.big_operators.basic
import data.set.finite
import group_theory.submonoid.membership
import data.finset.preimage
/-!
# Dependent functions with finite support
For a non-dependent version see `data/finsupp.lean`.
-/
universes u u₁ u₂ v v₁ v₂ v₃ w x y l
open_locale big_operators
variables (ι : Type u) {γ : Type w} (β : ι → Type v) {β₁ : ι → Type v₁} {β₂ : ι → Type v₂}
namespace dfinsupp
variable [Π i, has_zero (β i)]
/-- An auxiliary structure used in the definition of of `dfinsupp`,
the type used to make infinite direct sum of modules over a ring. -/
structure pre : Type (max u v) :=
(to_fun : Π i, β i)
(pre_support : multiset ι)
(zero : ∀ i, i ∈ pre_support ∨ to_fun i = 0)
instance inhabited_pre : inhabited (pre ι β) :=
⟨⟨λ i, 0, ∅, λ i, or.inr rfl⟩⟩
instance : setoid (pre ι β) :=
{ r := λ x y, ∀ i, x.to_fun i = y.to_fun i,
iseqv := ⟨λ f i, rfl, λ f g H i, (H i).symm,
λ f g h H1 H2 i, (H1 i).trans (H2 i)⟩ }
end dfinsupp
variable {ι}
/-- A dependent function `Π i, β i` with finite support. -/
@[reducible]
def dfinsupp [Π i, has_zero (β i)] : Type* :=
quotient (dfinsupp.pre.setoid ι β)
variable {β}
notation `Π₀` binders `, ` r:(scoped f, dfinsupp f) := r
infix ` →ₚ `:25 := dfinsupp
namespace dfinsupp
section basic
variables [Π i, has_zero (β i)] [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)]
instance fun_like : fun_like (Π₀ i, β i) ι β :=
⟨λ f, quotient.lift_on f pre.to_fun $ λ _ _, funext,
λ f g H, quotient.induction_on₂ f g (λ _ _ H, quotient.sound H) (congr_fun H)⟩
/-- Helper instance for when there are too many metavariables to apply `fun_like.has_coe_to_fun`
directly. -/
instance : has_coe_to_fun (Π₀ i, β i) (λ _, Π i, β i) := fun_like.has_coe_to_fun
@[ext] lemma ext {f g : Π₀ i, β i} (h : ∀ i, f i = g i) : f = g := fun_like.ext _ _ h
/-- Deprecated. Use `fun_like.ext_iff` instead. -/
lemma ext_iff {f g : Π₀ i, β i} : f = g ↔ ∀ i, f i = g i := fun_like.ext_iff
/-- Deprecated. Use `fun_like.coe_injective` instead. -/
lemma coe_fn_injective : @function.injective (Π₀ i, β i) (Π i, β i) coe_fn := fun_like.coe_injective
instance : has_zero (Π₀ i, β i) := ⟨⟦⟨0, ∅, λ i, or.inr rfl⟩⟧⟩
instance : inhabited (Π₀ i, β i) := ⟨0⟩
@[simp]
lemma coe_pre_mk (f : Π i, β i) (s : multiset ι) (hf) :
⇑(⟦⟨f, s, hf⟩⟧ : Π₀ i, β i) = f := rfl
@[simp] lemma coe_zero : ⇑(0 : Π₀ i, β i) = 0 := rfl
lemma zero_apply (i : ι) : (0 : Π₀ i, β i) i = 0 := rfl
/-- The composition of `f : β₁ → β₂` and `g : Π₀ i, β₁ i` is
`map_range f hf g : Π₀ i, β₂ i`, well defined when `f 0 = 0`.
This preserves the structure on `f`, and exists in various bundled forms for when `f` is itself
bundled:
* `dfinsupp.map_range.add_monoid_hom`
* `dfinsupp.map_range.add_equiv`
* `dfinsupp.map_range.linear_map`
* `dfinsupp.map_range.linear_equiv`
-/
def map_range (f : Π i, β₁ i → β₂ i) (hf : ∀ i, f i 0 = 0) : (Π₀ i, β₁ i) → Π₀ i, β₂ i :=
quotient.map
(λ x, ⟨λ i, f i (x.1 i), x.2, λ i, (x.3 i).imp_right $ λ H, by rw [H, hf]⟩)
(λ x y H i, by simp only [H i])
@[simp] lemma map_range_apply
(f : Π i, β₁ i → β₂ i) (hf : ∀ i, f i 0 = 0) (g : Π₀ i, β₁ i) (i : ι) :
map_range f hf g i = f i (g i) :=
quotient.induction_on g $ λ x, rfl
@[simp] lemma map_range_id (h : ∀ i, id (0 : β₁ i) = 0 := λ i, rfl) (g : Π₀ (i : ι), β₁ i) :
map_range (λ i, (id : β₁ i → β₁ i)) h g = g :=
by { ext, simp only [map_range_apply, id.def] }
lemma map_range_comp (f : Π i, β₁ i → β₂ i) (f₂ : Π i, β i → β₁ i)
(hf : ∀ i, f i 0 = 0) (hf₂ : ∀ i, f₂ i 0 = 0) (h : ∀ i, (f i ∘ f₂ i) 0 = 0)
(g : Π₀ (i : ι), β i) :
map_range (λ i, f i ∘ f₂ i) h g = map_range f hf (map_range f₂ hf₂ g) :=
by { ext, simp only [map_range_apply] }
@[simp] lemma map_range_zero (f : Π i, β₁ i → β₂ i) (hf : ∀ i, f i 0 = 0) :
map_range f hf (0 : Π₀ i, β₁ i) = 0 :=
by { ext, simp only [map_range_apply, coe_zero, pi.zero_apply, hf] }
/-- Let `f i` be a binary operation `β₁ i → β₂ i → β i` such that `f i 0 0 = 0`.
Then `zip_with f hf` is a binary operation `Π₀ i, β₁ i → Π₀ i, β₂ i → Π₀ i, β i`. -/
def zip_with (f : Π i, β₁ i → β₂ i → β i) (hf : ∀ i, f i 0 0 = 0) :
(Π₀ i, β₁ i) → (Π₀ i, β₂ i) → (Π₀ i, β i) :=
begin
refine quotient.map₂
(λ x y, ⟨λ i, f i (x.1 i) (y.1 i), x.2 + y.2, λ i, _⟩) _,
{ cases x.3 i with h1 h1,
{ left, rw multiset.mem_add, left, exact h1 },
cases y.3 i with h2 h2,
{ left, rw multiset.mem_add, right, exact h2 },
right, rw [h1, h2, hf] },
exact λ x₁ x₂ H1 y₁ y₂ H2 i, by simp only [H1 i, H2 i]
end
@[simp] lemma zip_with_apply
(f : Π i, β₁ i → β₂ i → β i) (hf : ∀ i, f i 0 0 = 0) (g₁ : Π₀ i, β₁ i) (g₂ : Π₀ i, β₂ i) (i : ι) :
zip_with f hf g₁ g₂ i = f i (g₁ i) (g₂ i) :=
quotient.induction_on₂ g₁ g₂ $ λ _ _, rfl
end basic
section algebra
instance [Π i, add_zero_class (β i)] : has_add (Π₀ i, β i) :=
⟨zip_with (λ _, (+)) (λ _, add_zero 0)⟩
lemma add_apply [Π i, add_zero_class (β i)] (g₁ g₂ : Π₀ i, β i) (i : ι) :
(g₁ + g₂) i = g₁ i + g₂ i :=
zip_with_apply _ _ g₁ g₂ i
@[simp] lemma coe_add [Π i, add_zero_class (β i)] (g₁ g₂ : Π₀ i, β i) :
⇑(g₁ + g₂) = g₁ + g₂ :=
funext $ add_apply g₁ g₂
instance [Π i, add_zero_class (β i)] : add_zero_class (Π₀ i, β i) :=
fun_like.coe_injective.add_zero_class _ coe_zero coe_add
/-- Note the general `dfinsupp.has_smul` instance doesn't apply as `ℕ` is not distributive
unless `β i`'s addition is commutative. -/
instance has_nat_scalar [Π i, add_monoid (β i)] : has_smul ℕ (Π₀ i, β i) :=
⟨λc v, v.map_range (λ _, (•) c) (λ _, nsmul_zero _)⟩
lemma nsmul_apply [Π i, add_monoid (β i)] (b : ℕ) (v : Π₀ i, β i) (i : ι) :
(b • v) i = b • (v i) :=
map_range_apply _ _ v i
@[simp] lemma coe_nsmul [Π i, add_monoid (β i)] (b : ℕ) (v : Π₀ i, β i) : ⇑(b • v) = b • v :=
funext $ nsmul_apply b v
instance [Π i, add_monoid (β i)] : add_monoid (Π₀ i, β i) :=
fun_like.coe_injective.add_monoid _ coe_zero coe_add (λ _ _, coe_nsmul _ _)
/-- Coercion from a `dfinsupp` to a pi type is an `add_monoid_hom`. -/
def coe_fn_add_monoid_hom [Π i, add_zero_class (β i)] : (Π₀ i, β i) →+ (Π i, β i) :=
{ to_fun := coe_fn, map_zero' := coe_zero, map_add' := coe_add }
/-- Evaluation at a point is an `add_monoid_hom`. This is the finitely-supported version of
`pi.eval_add_monoid_hom`. -/
def eval_add_monoid_hom [Π i, add_zero_class (β i)] (i : ι) : (Π₀ i, β i) →+ β i :=
(pi.eval_add_monoid_hom β i).comp coe_fn_add_monoid_hom
instance [Π i, add_comm_monoid (β i)] : add_comm_monoid (Π₀ i, β i) :=
fun_like.coe_injective.add_comm_monoid _ coe_zero coe_add (λ _ _, coe_nsmul _ _)
@[simp] lemma coe_finset_sum {α} [Π i, add_comm_monoid (β i)] (s : finset α) (g : α → Π₀ i, β i) :
⇑(∑ a in s, g a) = ∑ a in s, g a :=
(coe_fn_add_monoid_hom : _ →+ (Π i, β i)).map_sum g s
@[simp] lemma finset_sum_apply {α} [Π i, add_comm_monoid (β i)] (s : finset α) (g : α → Π₀ i, β i)
(i : ι) :
(∑ a in s, g a) i = ∑ a in s, g a i :=
(eval_add_monoid_hom i : _ →+ β i).map_sum g s
instance [Π i, add_group (β i)] : has_neg (Π₀ i, β i) :=
⟨λ f, f.map_range (λ _, has_neg.neg) (λ _, neg_zero)⟩
lemma neg_apply [Π i, add_group (β i)] (g : Π₀ i, β i) (i : ι) : (- g) i = - g i :=
map_range_apply _ _ g i
@[simp] lemma coe_neg [Π i, add_group (β i)] (g : Π₀ i, β i) : ⇑(- g) = - g :=
funext $ neg_apply g
instance [Π i, add_group (β i)] : has_sub (Π₀ i, β i) :=
⟨zip_with (λ _, has_sub.sub) (λ _, sub_zero 0)⟩
lemma sub_apply [Π i, add_group (β i)] (g₁ g₂ : Π₀ i, β i) (i : ι) :
(g₁ - g₂) i = g₁ i - g₂ i :=
zip_with_apply _ _ g₁ g₂ i
@[simp] lemma coe_sub [Π i, add_group (β i)] (g₁ g₂ : Π₀ i, β i) :
⇑(g₁ - g₂) = g₁ - g₂ :=
funext $ sub_apply g₁ g₂
/-- Note the general `dfinsupp.has_smul` instance doesn't apply as `ℤ` is not distributive
unless `β i`'s addition is commutative. -/
instance has_int_scalar [Π i, add_group (β i)] : has_smul ℤ (Π₀ i, β i) :=
⟨λc v, v.map_range (λ _, (•) c) (λ _, zsmul_zero _)⟩
lemma zsmul_apply [Π i, add_group (β i)] (b : ℤ) (v : Π₀ i, β i) (i : ι) : (b • v) i = b • (v i) :=
map_range_apply _ _ v i
@[simp] lemma coe_zsmul [Π i, add_group (β i)] (b : ℤ) (v : Π₀ i, β i) : ⇑(b • v) = b • v :=
funext $ zsmul_apply b v
instance [Π i, add_group (β i)] : add_group (Π₀ i, β i) :=
fun_like.coe_injective.add_group _
coe_zero coe_add coe_neg coe_sub (λ _ _, coe_nsmul _ _) (λ _ _, coe_zsmul _ _)
instance [Π i, add_comm_group (β i)] : add_comm_group (Π₀ i, β i) :=
fun_like.coe_injective.add_comm_group _
coe_zero coe_add coe_neg coe_sub (λ _ _, coe_nsmul _ _) (λ _ _, coe_zsmul _ _)
/-- Dependent functions with finite support inherit a semiring action from an action on each
coordinate. -/
instance [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] :
has_smul γ (Π₀ i, β i) :=
⟨λc v, v.map_range (λ _, (•) c) (λ _, smul_zero _)⟩
lemma smul_apply [monoid γ] [Π i, add_monoid (β i)]
[Π i, distrib_mul_action γ (β i)] (b : γ) (v : Π₀ i, β i) (i : ι) :
(b • v) i = b • (v i) :=
map_range_apply _ _ v i
@[simp] lemma coe_smul [monoid γ] [Π i, add_monoid (β i)]
[Π i, distrib_mul_action γ (β i)] (b : γ) (v : Π₀ i, β i) :
⇑(b • v) = b • v :=
funext $ smul_apply b v
instance {δ : Type*} [monoid γ] [monoid δ]
[Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] [Π i, distrib_mul_action δ (β i)]
[Π i, smul_comm_class γ δ (β i)] :
smul_comm_class γ δ (Π₀ i, β i) :=
{ smul_comm := λ r s m, ext $ λ i, by simp only [smul_apply, smul_comm r s (m i)] }
instance {δ : Type*} [monoid γ] [monoid δ]
[Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] [Π i, distrib_mul_action δ (β i)]
[has_smul γ δ] [Π i, is_scalar_tower γ δ (β i)] :
is_scalar_tower γ δ (Π₀ i, β i) :=
{ smul_assoc := λ r s m, ext $ λ i, by simp only [smul_apply, smul_assoc r s (m i)] }
instance [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)]
[Π i, distrib_mul_action γᵐᵒᵖ (β i)] [∀ i, is_central_scalar γ (β i)] :
is_central_scalar γ (Π₀ i, β i) :=
{ op_smul_eq_smul := λ r m, ext $ λ i, by simp only [smul_apply, op_smul_eq_smul r (m i)] }
/-- Dependent functions with finite support inherit a `distrib_mul_action` structure from such a
structure on each coordinate. -/
instance [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] :
distrib_mul_action γ (Π₀ i, β i) :=
function.injective.distrib_mul_action coe_fn_add_monoid_hom fun_like.coe_injective coe_smul
/-- Dependent functions with finite support inherit a module structure from such a structure on
each coordinate. -/
instance [semiring γ] [Π i, add_comm_monoid (β i)] [Π i, module γ (β i)] :
module γ (Π₀ i, β i) :=
{ zero_smul := λ c, ext $ λ i, by simp only [smul_apply, zero_smul, zero_apply],
add_smul := λ c x y, ext $ λ i, by simp only [add_apply, smul_apply, add_smul],
..dfinsupp.distrib_mul_action }
end algebra
section filter_and_subtype_domain
/-- `filter p f` is the function which is `f i` if `p i` is true and 0 otherwise. -/
def filter [Π i, has_zero (β i)] (p : ι → Prop) [decidable_pred p] : (Π₀ i, β i) → Π₀ i, β i :=
quotient.map
(λ x, ⟨λ i, if p i then x.1 i else 0, x.2, λ i, (x.3 i).imp_right $ λ H, by rw [H, if_t_t]⟩)
(λ x y H i, by simp only [H i])
@[simp] lemma filter_apply [Π i, has_zero (β i)]
(p : ι → Prop) [decidable_pred p] (i : ι) (f : Π₀ i, β i) :
f.filter p i = if p i then f i else 0 :=
quotient.induction_on f $ λ x, rfl
lemma filter_apply_pos [Π i, has_zero (β i)]
{p : ι → Prop} [decidable_pred p] (f : Π₀ i, β i) {i : ι} (h : p i) :
f.filter p i = f i :=
by simp only [filter_apply, if_pos h]
lemma filter_apply_neg [Π i, has_zero (β i)]
{p : ι → Prop} [decidable_pred p] (f : Π₀ i, β i) {i : ι} (h : ¬ p i) :
f.filter p i = 0 :=
by simp only [filter_apply, if_neg h]
lemma filter_pos_add_filter_neg [Π i, add_zero_class (β i)] (f : Π₀ i, β i)
(p : ι → Prop) [decidable_pred p] :
f.filter p + f.filter (λi, ¬ p i) = f :=
ext $ λ i, by simp only [add_apply, filter_apply]; split_ifs; simp only [add_zero, zero_add]
@[simp] lemma filter_zero [Π i, has_zero (β i)] (p : ι → Prop) [decidable_pred p] :
(0 : Π₀ i, β i).filter p = 0 :=
by { ext, simp }
@[simp] lemma filter_add [Π i, add_zero_class (β i)] (p : ι → Prop) [decidable_pred p]
(f g : Π₀ i, β i) :
(f + g).filter p = f.filter p + g.filter p :=
by { ext, simp [ite_add_zero] }
@[simp] lemma filter_smul [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)]
(p : ι → Prop) [decidable_pred p] (r : γ) (f : Π₀ i, β i) :
(r • f).filter p = r • f.filter p :=
by { ext, simp [smul_ite] }
variables (γ β)
/-- `dfinsupp.filter` as an `add_monoid_hom`. -/
@[simps]
def filter_add_monoid_hom [Π i, add_zero_class (β i)] (p : ι → Prop) [decidable_pred p] :
(Π₀ i, β i) →+ (Π₀ i, β i) :=
{ to_fun := filter p,
map_zero' := filter_zero p,
map_add' := filter_add p }
/-- `dfinsupp.filter` as a `linear_map`. -/
@[simps]
def filter_linear_map [semiring γ] [Π i, add_comm_monoid (β i)] [Π i, module γ (β i)]
(p : ι → Prop) [decidable_pred p] :
(Π₀ i, β i) →ₗ[γ] (Π₀ i, β i) :=
{ to_fun := filter p,
map_add' := filter_add p,
map_smul' := filter_smul p }
variables {γ β}
@[simp] lemma filter_neg [Π i, add_group (β i)] (p : ι → Prop) [decidable_pred p]
(f : Π₀ i, β i) :
(-f).filter p = -f.filter p :=
(filter_add_monoid_hom β p).map_neg f
@[simp] lemma filter_sub [Π i, add_group (β i)] (p : ι → Prop) [decidable_pred p]
(f g : Π₀ i, β i) :
(f - g).filter p = f.filter p - g.filter p :=
(filter_add_monoid_hom β p).map_sub f g
/-- `subtype_domain p f` is the restriction of the finitely supported function
`f` to the subtype `p`. -/
def subtype_domain [Π i, has_zero (β i)] (p : ι → Prop) [decidable_pred p] :
(Π₀ i, β i) → Π₀ i : subtype p, β i :=
quotient.map
(λ x, ⟨λ i, x.1 (i : ι), (x.2.filter p).attach.map $ λ j, ⟨j, (multiset.mem_filter.1 j.2).2⟩,
λ i, (x.3 i).imp_left $ λ H, multiset.mem_map.2
⟨⟨i, multiset.mem_filter.2 ⟨H, i.2⟩⟩, multiset.mem_attach _ _, subtype.eta _ _⟩⟩)
(λ x y H i, H i)
@[simp] lemma subtype_domain_zero [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] :
subtype_domain p (0 : Π₀ i, β i) = 0 :=
rfl
@[simp] lemma subtype_domain_apply [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p]
{i : subtype p} {v : Π₀ i, β i} :
(subtype_domain p v) i = v i :=
quotient.induction_on v $ λ x, rfl
@[simp] lemma subtype_domain_add [Π i, add_zero_class (β i)] {p : ι → Prop} [decidable_pred p]
(v v' : Π₀ i, β i) :
(v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p :=
ext $ λ i, by simp only [add_apply, subtype_domain_apply]
@[simp] lemma subtype_domain_smul [monoid γ] [Π i, add_monoid (β i)]
[Π i, distrib_mul_action γ (β i)] {p : ι → Prop} [decidable_pred p] (r : γ) (f : Π₀ i, β i) :
(r • f).subtype_domain p = r • f.subtype_domain p :=
quotient.induction_on f $ λ x, rfl
variables (γ β)
/-- `subtype_domain` but as an `add_monoid_hom`. -/
@[simps] def subtype_domain_add_monoid_hom [Π i, add_zero_class (β i)]
(p : ι → Prop) [decidable_pred p] : (Π₀ i : ι, β i) →+ Π₀ i : subtype p, β i :=
{ to_fun := subtype_domain p,
map_zero' := subtype_domain_zero,
map_add' := subtype_domain_add }
/-- `dfinsupp.subtype_domain` as a `linear_map`. -/
@[simps]
def subtype_domain_linear_map [semiring γ] [Π i, add_comm_monoid (β i)] [Π i, module γ (β i)]
(p : ι → Prop) [decidable_pred p] :
(Π₀ i, β i) →ₗ[γ] (Π₀ i : subtype p, β i) :=
{ to_fun := subtype_domain p,
map_add' := subtype_domain_add,
map_smul' := subtype_domain_smul }
variables {γ β}
@[simp]
lemma subtype_domain_neg [Π i, add_group (β i)] {p : ι → Prop} [decidable_pred p] {v : Π₀ i, β i} :
(- v).subtype_domain p = - v.subtype_domain p :=
ext $ λ i, by simp only [neg_apply, subtype_domain_apply]
@[simp] lemma subtype_domain_sub [Π i, add_group (β i)] {p : ι → Prop} [decidable_pred p]
{v v' : Π₀ i, β i} :
(v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p :=
ext $ λ i, by simp only [sub_apply, subtype_domain_apply]
end filter_and_subtype_domain
variable [dec : decidable_eq ι]
include dec
section basic
variable [Π i, has_zero (β i)]
omit dec
lemma finite_support (f : Π₀ i, β i) : set.finite {i | f i ≠ 0} :=
begin
classical,
exact quotient.induction_on f (λ x, x.2.to_finset.finite_to_set.subset (λ i H,
multiset.mem_to_finset.2 ((x.3 i).resolve_right H)))
end
include dec
/-- Create an element of `Π₀ i, β i` from a finset `s` and a function `x`
defined on this `finset`. -/
def mk (s : finset ι) (x : Π i : (↑s : set ι), β (i : ι)) : Π₀ i, β i :=
⟦⟨λ i, if H : i ∈ s then x ⟨i, H⟩ else 0, s.1,
λ i, if H : i ∈ s then or.inl H else or.inr $ dif_neg H⟩⟧
variables {s : finset ι} {x : Π i : (↑s : set ι), β i} {i : ι}
@[simp] lemma mk_apply : (mk s x : Π i, β i) i = if H : i ∈ s then x ⟨i, H⟩ else 0 := rfl
lemma mk_of_mem (hi : i ∈ s) : (mk s x : Π i, β i) i = x ⟨i, hi⟩ := dif_pos hi
lemma mk_of_not_mem (hi : i ∉ s) : (mk s x : Π i, β i) i = 0 := dif_neg hi
theorem mk_injective (s : finset ι) : function.injective (@mk ι β _ _ s) :=
begin
intros x y H,
ext i,
have h1 : (mk s x : Π i, β i) i = (mk s y : Π i, β i) i, {rw H},
cases i with i hi,
change i ∈ s at hi,
dsimp only [mk_apply, subtype.coe_mk] at h1,
simpa only [dif_pos hi] using h1
end
omit dec
instance [is_empty ι] : unique (Π₀ i, β i) :=
⟨⟨0⟩, λ a, by { ext, exact is_empty_elim i }⟩
/-- Given `fintype ι`, `equiv_fun_on_fintype` is the `equiv` between `Π₀ i, β i` and `Π i, β i`.
(All dependent functions on a finite type are finitely supported.) -/
@[simps apply] def equiv_fun_on_fintype [fintype ι] : (Π₀ i, β i) ≃ (Π i, β i) :=
{ to_fun := coe_fn,
inv_fun := λ f, ⟦⟨f, finset.univ.1, λ i, or.inl $ finset.mem_univ_val _⟩⟧,
left_inv := λ x, coe_fn_injective rfl,
right_inv := λ x, rfl }
@[simp] lemma equiv_fun_on_fintype_symm_coe [fintype ι] (f : Π₀ i, β i) :
equiv_fun_on_fintype.symm f = f :=
equiv.symm_apply_apply _ _
include dec
/-- The function `single i b : Π₀ i, β i` sends `i` to `b`
and all other points to `0`. -/
def single (i : ι) (b : β i) : Π₀ i, β i :=
mk {i} $ λ j, eq.rec_on (finset.mem_singleton.1 j.prop).symm b
@[simp] lemma single_apply {i i' b} :
(single i b : Π₀ i, β i) i' = (if h : i = i' then eq.rec_on h b else 0) :=
begin
dsimp only [single],
by_cases h : i = i',
{ have h1 : i' ∈ ({i} : finset ι) := finset.mem_singleton.2 h.symm,
simp only [mk_apply, dif_pos h, dif_pos h1], refl },
{ have h1 : i' ∉ ({i} : finset ι) := finset.not_mem_singleton.2 (ne.symm h),
simp only [mk_apply, dif_neg h, dif_neg h1] }
end
lemma single_eq_pi_single {i b} : ⇑(single i b : Π₀ i, β i) = pi.single i b :=
begin
ext i',
simp only [pi.single, function.update],
split_ifs,
{ simp [h] },
{ simp [ne.symm h] }
end
@[simp] lemma single_zero (i) : (single i 0 : Π₀ i, β i) = 0 :=
quotient.sound $ λ j, if H : j ∈ ({i} : finset _)
then by dsimp only; rw [dif_pos H]; cases finset.mem_singleton.1 H; refl
else dif_neg H
@[simp] lemma single_eq_same {i b} : (single i b : Π₀ i, β i) i = b :=
by simp only [single_apply, dif_pos rfl]
lemma single_eq_of_ne {i i' b} (h : i ≠ i') : (single i b : Π₀ i, β i) i' = 0 :=
by simp only [single_apply, dif_neg h]
lemma single_injective {i} : function.injective (single i : β i → Π₀ i, β i) :=
λ x y H, congr_fun (mk_injective _ H) ⟨i, by simp⟩
/-- Like `finsupp.single_eq_single_iff`, but with a `heq` due to dependent types -/
lemma single_eq_single_iff (i j : ι) (xi : β i) (xj : β j) :
dfinsupp.single i xi = dfinsupp.single j xj ↔ i = j ∧ xi == xj ∨ xi = 0 ∧ xj = 0 :=
begin
split,
{ intro h,
by_cases hij : i = j,
{ subst hij,
exact or.inl ⟨rfl, heq_of_eq (dfinsupp.single_injective h)⟩, },
{ have h_coe : ⇑(dfinsupp.single i xi) = dfinsupp.single j xj := congr_arg coe_fn h,
have hci := congr_fun h_coe i,
have hcj := congr_fun h_coe j,
rw dfinsupp.single_eq_same at hci hcj,
rw dfinsupp.single_eq_of_ne (ne.symm hij) at hci,
rw dfinsupp.single_eq_of_ne (hij) at hcj,
exact or.inr ⟨hci, hcj.symm⟩, }, },
{ rintros (⟨rfl, hxi⟩ | ⟨hi, hj⟩),
{ rw eq_of_heq hxi, },
{ rw [hi, hj, dfinsupp.single_zero, dfinsupp.single_zero], }, },
end
@[simp] lemma single_eq_zero {i : ι} {xi : β i} : single i xi = 0 ↔ xi = 0 :=
begin
rw [←single_zero i, single_eq_single_iff],
simp,
end
lemma filter_single (p : ι → Prop) [decidable_pred p] (i : ι) (x : β i) :
(single i x).filter p = if p i then single i x else 0 :=
begin
ext j,
have := apply_ite (λ x : Π₀ i, β i, x j) (p i) (single i x) 0,
dsimp at this,
rw [filter_apply, this],
obtain rfl | hij := decidable.eq_or_ne i j,
{ refl, },
{ rw [single_eq_of_ne hij, if_t_t, if_t_t], },
end
@[simp] lemma filter_single_pos {p : ι → Prop} [decidable_pred p] (i : ι) (x : β i) (h : p i) :
(single i x).filter p = single i x :=
by rw [filter_single, if_pos h]
@[simp] lemma filter_single_neg {p : ι → Prop} [decidable_pred p] (i : ι) (x : β i) (h : ¬p i) :
(single i x).filter p = 0 :=
by rw [filter_single, if_neg h]
/-- Equality of sigma types is sufficient (but not necessary) to show equality of `dfinsupp`s. -/
lemma single_eq_of_sigma_eq
{i j} {xi : β i} {xj : β j} (h : (⟨i, xi⟩ : sigma β) = ⟨j, xj⟩) :
dfinsupp.single i xi = dfinsupp.single j xj :=
by { cases h, refl }
@[simp] lemma equiv_fun_on_fintype_single [fintype ι] (i : ι) (m : β i) :
(@dfinsupp.equiv_fun_on_fintype ι β _ _) (dfinsupp.single i m) = pi.single i m :=
by { ext, simp [dfinsupp.single_eq_pi_single], }
@[simp] lemma equiv_fun_on_fintype_symm_single [fintype ι] (i : ι) (m : β i) :
(@dfinsupp.equiv_fun_on_fintype ι β _ _).symm (pi.single i m) = dfinsupp.single i m :=
by { ext i', simp only [← single_eq_pi_single, equiv_fun_on_fintype_symm_coe] }
/-- Redefine `f i` to be `0`. -/
def erase (i : ι) : (Π₀ i, β i) → Π₀ i, β i :=
quotient.map
(λ x, ⟨λ j, if j = i then 0 else x.1 j, x.2,
λ j, (x.3 j).imp_right $ λ H, by simp only [H, if_t_t]⟩)
(λ x y H j, if h : j = i then by simp only [if_pos h] else by simp only [if_neg h, H j])
@[simp] lemma erase_apply {i j : ι} {f : Π₀ i, β i} :
(f.erase i) j = if j = i then 0 else f j :=
quotient.induction_on f $ λ x, rfl
@[simp] lemma erase_same {i : ι} {f : Π₀ i, β i} : (f.erase i) i = 0 :=
by simp
lemma erase_ne {i i' : ι} {f : Π₀ i, β i} (h : i' ≠ i) : (f.erase i) i' = f i' :=
by simp [h]
lemma erase_eq_sub_single {β : ι → Type*} [Π i, add_group (β i)] (f : Π₀ i, β i) (i : ι) :
f.erase i = f - single i (f i) :=
begin
ext j,
rcases eq_or_ne i j with rfl|h,
{ simp },
{ simp [erase_ne h.symm, single_eq_of_ne h] }
end
@[simp] lemma erase_zero (i : ι) : erase i (0 : Π₀ i, β i) = 0 :=
ext $ λ _, if_t_t _ _
@[simp] lemma filter_ne_eq_erase (f : Π₀ i, β i) (i : ι) : f.filter (≠ i) = f.erase i :=
begin
ext1 j,
simp only [dfinsupp.filter_apply, dfinsupp.erase_apply, ite_not],
end
@[simp] lemma filter_ne_eq_erase' (f : Π₀ i, β i) (i : ι) : f.filter ((≠) i) = f.erase i :=
begin
rw ←filter_ne_eq_erase f i,
congr' with j,
exact ne_comm,
end
lemma erase_single (j : ι) (i : ι) (x : β i) :
(single i x).erase j = if i = j then 0 else single i x :=
by rw [←filter_ne_eq_erase, filter_single, ite_not]
@[simp] lemma erase_single_same (i : ι) (x : β i) : (single i x).erase i = 0 :=
by rw [erase_single, if_pos rfl]
@[simp] lemma erase_single_ne {i j : ι} (x : β i) (h : i ≠ j) : (single i x).erase j = single i x :=
by rw [erase_single, if_neg h]
section update
variables (f : Π₀ i, β i) (i) (b : β i) [decidable (b = 0)]
/-- Replace the value of a `Π₀ i, β i` at a given point `i : ι` by a given value `b : β i`.
If `b = 0`, this amounts to removing `i` from the support.
Otherwise, `i` is added to it.
This is the (dependent) finitely-supported version of `function.update`. -/
def update : Π₀ i, β i :=
quotient.map (λ (x : pre _ _), ⟨function.update x.to_fun i b,
if b = 0 then x.pre_support.erase i else i ::ₘ x.pre_support,
begin
intro j,
rcases eq_or_ne i j with rfl|hi,
{ split_ifs with hb,
{ simp [hb] },
{ simp } },
{ cases x.zero j with hj hj,
{ split_ifs;
simp [multiset.mem_erase_of_ne hi.symm, hj] },
{ simp [function.update_noteq hi.symm, hj] } }
end⟩)
(λ x y h j,
show function.update x.to_fun i b j = function.update y.to_fun i b j,
by rw (funext h : x.to_fun = y.to_fun)) f
variables (j : ι)
@[simp] lemma coe_update : (f.update i b : Π (i : ι), β i) = function.update f i b :=
quotient.induction_on f (λ _, rfl)
@[simp] lemma update_self [decidable (f i = 0)] : f.update i (f i) = f :=
by { ext, simp }
@[simp] lemma update_eq_erase [decidable ((0 : β i) = 0)] : f.update i 0 = f.erase i :=
begin
ext j,
rcases eq_or_ne i j with rfl|hi,
{ simp },
{ simp [hi.symm] }
end
lemma update_eq_single_add_erase {β : ι → Type*} [Π i, add_zero_class (β i)] (f : Π₀ i, β i) (i : ι)
(b : β i) [decidable (b = 0)] :
f.update i b = single i b + f.erase i :=
begin
ext j,
rcases eq_or_ne i j with rfl|h,
{ simp },
{ simp [function.update_noteq h.symm, h, erase_ne, h.symm] }
end
lemma update_eq_erase_add_single {β : ι → Type*} [Π i, add_zero_class (β i)] (f : Π₀ i, β i) (i : ι)
(b : β i) [decidable (b = 0)] :
f.update i b = f.erase i + single i b :=
begin
ext j,
rcases eq_or_ne i j with rfl|h,
{ simp },
{ simp [function.update_noteq h.symm, h, erase_ne, h.symm] }
end
lemma update_eq_sub_add_single {β : ι → Type*} [Π i, add_group (β i)] (f : Π₀ i, β i) (i : ι)
(b : β i) [decidable (b = 0)] :
f.update i b = f - single i (f i) + single i b :=
by rw [update_eq_erase_add_single f i b, erase_eq_sub_single f i]
end update
end basic
section add_monoid
variable [Π i, add_zero_class (β i)]
@[simp] lemma single_add (i : ι) (b₁ b₂ : β i) : single i (b₁ + b₂) = single i b₁ + single i b₂ :=
ext $ assume i',
begin
by_cases h : i = i',
{ subst h, simp only [add_apply, single_eq_same] },
{ simp only [add_apply, single_eq_of_ne h, zero_add] }
end
@[simp] lemma erase_add (i : ι) (f₁ f₂ : Π₀ i, β i) : erase i (f₁ + f₂) = erase i f₁ + erase i f₂ :=
ext $ λ _, by simp [ite_zero_add]
variables (β)
/-- `dfinsupp.single` as an `add_monoid_hom`. -/
@[simps] def single_add_hom (i : ι) : β i →+ Π₀ i, β i :=
{ to_fun := single i, map_zero' := single_zero i, map_add' := single_add i }
/-- `dfinsupp.erase` as an `add_monoid_hom`. -/
@[simps] def erase_add_hom (i : ι) : (Π₀ i, β i) →+ Π₀ i, β i :=
{ to_fun := erase i, map_zero' := erase_zero i, map_add' := erase_add i }
variables {β}
@[simp] lemma single_neg {β : ι → Type v} [Π i, add_group (β i)] (i : ι) (x : β i) :
single i (-x) = -single i x :=
(single_add_hom β i).map_neg x
@[simp] lemma single_sub {β : ι → Type v} [Π i, add_group (β i)] (i : ι) (x y : β i) :
single i (x - y) = single i x - single i y :=
(single_add_hom β i).map_sub x y
@[simp] lemma erase_neg {β : ι → Type v} [Π i, add_group (β i)] (i : ι) (f : Π₀ i, β i) :
(-f).erase i = -f.erase i :=
(erase_add_hom β i).map_neg f
@[simp] lemma erase_sub {β : ι → Type v} [Π i, add_group (β i)] (i : ι) (f g : Π₀ i, β i) :
(f - g).erase i = f.erase i - g.erase i :=
(erase_add_hom β i).map_sub f g
lemma single_add_erase (i : ι) (f : Π₀ i, β i) : single i (f i) + f.erase i = f :=
ext $ λ i',
if h : i = i'
then by subst h; simp only [add_apply, single_apply, erase_apply, dif_pos rfl, if_pos, add_zero]
else by simp only [add_apply, single_apply, erase_apply, dif_neg h, if_neg (ne.symm h), zero_add]
lemma erase_add_single (i : ι) (f : Π₀ i, β i) : f.erase i + single i (f i) = f :=
ext $ λ i',
if h : i = i'
then by subst h; simp only [add_apply, single_apply, erase_apply, dif_pos rfl, if_pos, zero_add]
else by simp only [add_apply, single_apply, erase_apply, dif_neg h, if_neg (ne.symm h), add_zero]
protected theorem induction {p : (Π₀ i, β i) → Prop} (f : Π₀ i, β i)
(h0 : p 0) (ha : ∀i b (f : Π₀ i, β i), f i = 0 → b ≠ 0 → p f → p (single i b + f)) :
p f :=
begin
refine quotient.induction_on f (λ x, _),
cases x with f s H, revert f H,
apply multiset.induction_on s,
{ intros f H, convert h0, ext i, exact (H i).resolve_left id },
intros i s ih f H,
have H2 : p (erase i ⟦{to_fun := f, pre_support := i ::ₘ s, zero := H}⟧),
{ dsimp only [erase, quotient.map_mk],
have H2 : ∀ j, j ∈ s ∨ ite (j = i) 0 (f j) = 0,
{ intro j, cases H j with H2 H2,
{ cases multiset.mem_cons.1 H2 with H3 H3,
{ right, exact if_pos H3 },
{ left, exact H3 } },
right, split_ifs; [refl, exact H2] },
have H3 : (⟦{to_fun := λ (j : ι), ite (j = i) 0 (f j),
pre_support := i ::ₘ s, zero := _}⟧ : Π₀ i, β i)
= ⟦{to_fun := λ (j : ι), ite (j = i) 0 (f j), pre_support := s, zero := H2}⟧ :=
quotient.sound (λ i, rfl),
rw H3, apply ih },
have H3 : single i _ + _ = (⟦{to_fun := f, pre_support := i ::ₘ s, zero := H}⟧ : Π₀ i, β i) :=
single_add_erase _ _,
rw ← H3,
change p (single i (f i) + _),
cases classical.em (f i = 0) with h h,
{ rw [h, single_zero, zero_add], exact H2 },
refine ha _ _ _ _ h H2,
rw erase_same
end
lemma induction₂ {p : (Π₀ i, β i) → Prop} (f : Π₀ i, β i)
(h0 : p 0) (ha : ∀i b (f : Π₀ i, β i), f i = 0 → b ≠ 0 → p f → p (f + single i b)) :
p f :=
dfinsupp.induction f h0 $ λ i b f h1 h2 h3,
have h4 : f + single i b = single i b + f,
{ ext j, by_cases H : i = j,
{ subst H, simp [h1] },
{ simp [H] } },
eq.rec_on h4 $ ha i b f h1 h2 h3
@[simp] lemma add_closure_Union_range_single :
add_submonoid.closure (⋃ i : ι, set.range (single i : β i → (Π₀ i, β i))) = ⊤ :=
top_unique $ λ x hx, (begin
apply dfinsupp.induction x,
exact add_submonoid.zero_mem _,
exact λ a b f ha hb hf, add_submonoid.add_mem _
(add_submonoid.subset_closure $ set.mem_Union.2 ⟨a, set.mem_range_self _⟩) hf
end)
/-- If two additive homomorphisms from `Π₀ i, β i` are equal on each `single a b`, then
they are equal. -/
lemma add_hom_ext {γ : Type w} [add_zero_class γ] ⦃f g : (Π₀ i, β i) →+ γ⦄
(H : ∀ (i : ι) (y : β i), f (single i y) = g (single i y)) :
f = g :=
begin
refine add_monoid_hom.eq_of_eq_on_mdense add_closure_Union_range_single (λ f hf, _),
simp only [set.mem_Union, set.mem_range] at hf,
rcases hf with ⟨x, y, rfl⟩,
apply H
end
/-- If two additive homomorphisms from `Π₀ i, β i` are equal on each `single a b`, then
they are equal.
See note [partially-applied ext lemmas]. -/
@[ext] lemma add_hom_ext' {γ : Type w} [add_zero_class γ] ⦃f g : (Π₀ i, β i) →+ γ⦄
(H : ∀ x, f.comp (single_add_hom β x) = g.comp (single_add_hom β x)) :
f = g :=
add_hom_ext $ λ x, add_monoid_hom.congr_fun (H x)
end add_monoid
@[simp] lemma mk_add [Π i, add_zero_class (β i)] {s : finset ι} {x y : Π i : (↑s : set ι), β i} :
mk s (x + y) = mk s x + mk s y :=
ext $ λ i, by simp only [add_apply, mk_apply]; split_ifs; [refl, rw zero_add]
@[simp] lemma mk_zero [Π i, has_zero (β i)] {s : finset ι} :
mk s (0 : Π i : (↑s : set ι), β i.1) = 0 :=
ext $ λ i, by simp only [mk_apply]; split_ifs; refl
@[simp] lemma mk_neg [Π i, add_group (β i)] {s : finset ι} {x : Π i : (↑s : set ι), β i.1} :
mk s (-x) = -mk s x :=
ext $ λ i, by simp only [neg_apply, mk_apply]; split_ifs; [refl, rw neg_zero]
@[simp] lemma mk_sub [Π i, add_group (β i)] {s : finset ι} {x y : Π i : (↑s : set ι), β i.1} :
mk s (x - y) = mk s x - mk s y :=
ext $ λ i, by simp only [sub_apply, mk_apply]; split_ifs; [refl, rw sub_zero]
/-- If `s` is a subset of `ι` then `mk_add_group_hom s` is the canonical additive
group homomorphism from $\prod_{i\in s}\beta_i$ to $\prod_{\mathtt{i : \iota}}\beta_i.$-/
def mk_add_group_hom [Π i, add_group (β i)] (s : finset ι) :
(Π (i : (s : set ι)), β ↑i) →+ (Π₀ (i : ι), β i) :=
{ to_fun := mk s,
map_zero' := mk_zero,
map_add' := λ _ _, mk_add }
section
variables [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)]
@[simp] lemma mk_smul {s : finset ι} (c : γ) (x : Π i : (↑s : set ι), β (i : ι)) :
mk s (c • x) = c • mk s x :=
ext $ λ i, by simp only [smul_apply, mk_apply]; split_ifs; [refl, rw smul_zero]
@[simp] lemma single_smul {i : ι} (c : γ) (x : β i) :
single i (c • x) = c • single i x :=
ext $ λ i, by simp only [smul_apply, single_apply]; split_ifs; [cases h, rw smul_zero]; refl
end
section support_basic
variables [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)]
/-- Set `{i | f x ≠ 0}` as a `finset`. -/
def support (f : Π₀ i, β i) : finset ι :=
quotient.lift_on f (λ x, x.2.to_finset.filter $ λ i, x.1 i ≠ 0) $
begin
intros x y Hxy,
ext i, split,
{ intro H,
rcases finset.mem_filter.1 H with ⟨h1, h2⟩,
rw Hxy i at h2,
exact finset.mem_filter.2 ⟨multiset.mem_to_finset.2 $ (y.3 i).resolve_right h2, h2⟩ },
{ intro H,
rcases finset.mem_filter.1 H with ⟨h1, h2⟩,
rw ← Hxy i at h2,
exact finset.mem_filter.2 ⟨multiset.mem_to_finset.2 $ (x.3 i).resolve_right h2, h2⟩ },
end
@[simp] theorem support_mk_subset {s : finset ι} {x : Π i : (↑s : set ι), β i.1} :
(mk s x).support ⊆ s :=
λ i H, multiset.mem_to_finset.1 (finset.mem_filter.1 H).1
@[simp] theorem mem_support_to_fun (f : Π₀ i, β i) (i) : i ∈ f.support ↔ f i ≠ 0 :=
begin
refine quotient.induction_on f (λ x, _),
dsimp only [support, quotient.lift_on_mk],
rw [finset.mem_filter, multiset.mem_to_finset],
exact and_iff_right_of_imp (x.3 i).resolve_right
end
theorem eq_mk_support (f : Π₀ i, β i) : f = mk f.support (λ i, f i) :=
begin
change f = mk f.support (λ i, f i.1),
ext i,
by_cases h : f i ≠ 0; [skip, rw [not_not] at h];
simp [h]
end
@[simp] lemma support_zero : (0 : Π₀ i, β i).support = ∅ := rfl
lemma mem_support_iff {f : Π₀ i, β i} {i : ι} : i ∈ f.support ↔ f i ≠ 0 := f.mem_support_to_fun _
lemma not_mem_support_iff {f : Π₀ i, β i} {i : ι} : i ∉ f.support ↔ f i = 0 :=
not_iff_comm.1 mem_support_iff.symm
@[simp] lemma support_eq_empty {f : Π₀ i, β i} : f.support = ∅ ↔ f = 0 :=
⟨λ H, ext $ by simpa [finset.ext_iff] using H, by simp {contextual:=tt}⟩
instance decidable_zero : decidable_pred (eq (0 : Π₀ i, β i)) :=
λ f, decidable_of_iff _ $ support_eq_empty.trans eq_comm
lemma support_subset_iff {s : set ι} {f : Π₀ i, β i} :
↑f.support ⊆ s ↔ (∀i∉s, f i = 0) :=
by simp [set.subset_def];
exact forall_congr (assume i, not_imp_comm)
lemma support_single_ne_zero {i : ι} {b : β i} (hb : b ≠ 0) : (single i b).support = {i} :=
begin
ext j, by_cases h : i = j,
{ subst h, simp [hb] },
simp [ne.symm h, h]
end
lemma support_single_subset {i : ι} {b : β i} : (single i b).support ⊆ {i} :=
support_mk_subset
section map_range_and_zip_with
variables [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)]
lemma map_range_def [Π i (x : β₁ i), decidable (x ≠ 0)]
{f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} :
map_range f hf g = mk g.support (λ i, f i.1 (g i.1)) :=
begin
ext i,
by_cases h : g i ≠ 0; simp at h; simp [h, hf]
end
@[simp] lemma map_range_single {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {i : ι} {b : β₁ i} :
map_range f hf (single i b) = single i (f i b) :=
dfinsupp.ext $ λ i', by by_cases i = i'; [{subst i', simp}, simp [h, hf]]
variables [Π i (x : β₁ i), decidable (x ≠ 0)] [Π i (x : β₂ i), decidable (x ≠ 0)]
lemma support_map_range {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} :
(map_range f hf g).support ⊆ g.support :=
by simp [map_range_def]
lemma zip_with_def {ι : Type u} {β : ι → Type v} {β₁ : ι → Type v₁} {β₂ : ι → Type v₂}
[dec : decidable_eq ι] [Π (i : ι), has_zero (β i)] [Π (i : ι), has_zero (β₁ i)]
[Π (i : ι), has_zero (β₂ i)] [Π (i : ι) (x : β₁ i), decidable (x ≠ 0)]
[Π (i : ι) (x : β₂ i), decidable (x ≠ 0)]
{f : Π i, β₁ i → β₂ i → β i} {hf : ∀ i, f i 0 0 = 0}
{g₁ : Π₀ i, β₁ i} {g₂ : Π₀ i, β₂ i} :
zip_with f hf g₁ g₂ = mk (g₁.support ∪ g₂.support) (λ i, f i.1 (g₁ i.1) (g₂ i.1)) :=
begin
ext i,
by_cases h1 : g₁ i ≠ 0; by_cases h2 : g₂ i ≠ 0;
simp only [not_not, ne.def] at h1 h2; simp [h1, h2, hf]
end
lemma support_zip_with {f : Π i, β₁ i → β₂ i → β i} {hf : ∀ i, f i 0 0 = 0}
{g₁ : Π₀ i, β₁ i} {g₂ : Π₀ i, β₂ i} :
(zip_with f hf g₁ g₂).support ⊆ g₁.support ∪ g₂.support :=
by simp [zip_with_def]
end map_range_and_zip_with
lemma erase_def (i : ι) (f : Π₀ i, β i) :
f.erase i = mk (f.support.erase i) (λ j, f j.1) :=
by { ext j, by_cases h1 : j = i; by_cases h2 : f j ≠ 0; simp at h2; simp [h1, h2] }
@[simp] lemma support_erase (i : ι) (f : Π₀ i, β i) :
(f.erase i).support = f.support.erase i :=
by { ext j, by_cases h1 : j = i, simp [h1], by_cases h2 : f j ≠ 0; simp at h2; simp [h1, h2] }
lemma support_update_ne_zero (f : Π₀ i, β i) (i : ι) {b : β i} [decidable (b = 0)] (h : b ≠ 0) :
support (f.update i b) = insert i f.support :=
begin
ext j,
rcases eq_or_ne i j with rfl|hi,
{ simp [h] },
{ simp [hi.symm] }
end
lemma support_update (f : Π₀ i, β i) (i : ι) (b : β i) [decidable (b = 0)] :
support (f.update i b) = if b = 0 then support (f.erase i) else insert i f.support :=
begin
ext j,
split_ifs with hb,
{ substI hb, simp [update_eq_erase, support_erase] },
{ rw [support_update_ne_zero f _ hb] }
end
section filter_and_subtype_domain
variables {p : ι → Prop} [decidable_pred p]
lemma filter_def (f : Π₀ i, β i) :
f.filter p = mk (f.support.filter p) (λ i, f i.1) :=
by ext i; by_cases h1 : p i; by_cases h2 : f i ≠ 0;
simp at h2; simp [h1, h2]
@[simp] lemma support_filter (f : Π₀ i, β i) :
(f.filter p).support = f.support.filter p :=
by ext i; by_cases h : p i; simp [h]
lemma subtype_domain_def (f : Π₀ i, β i) :
f.subtype_domain p = mk (f.support.subtype p) (λ i, f i) :=
by ext i; by_cases h2 : f i ≠ 0; try {simp at h2}; dsimp; simp [h2]
@[simp] lemma support_subtype_domain {f : Π₀ i, β i} :
(subtype_domain p f).support = f.support.subtype p :=
by { ext i, simp, }
end filter_and_subtype_domain
end support_basic
lemma support_add [Π i, add_zero_class (β i)] [Π i (x : β i), decidable (x ≠ 0)]
{g₁ g₂ : Π₀ i, β i} :
(g₁ + g₂).support ⊆ g₁.support ∪ g₂.support :=
support_zip_with
@[simp] lemma support_neg [Π i, add_group (β i)] [Π i (x : β i), decidable (x ≠ 0)]
{f : Π₀ i, β i} :
support (-f) = support f :=
by ext i; simp
lemma support_smul {γ : Type w} [semiring γ] [Π i, add_comm_monoid (β i)] [Π i, module γ (β i)]
[Π ( i : ι) (x : β i), decidable (x ≠ 0)]
(b : γ) (v : Π₀ i, β i) : (b • v).support ⊆ v.support :=
support_map_range
instance [Π i, has_zero (β i)] [Π i, decidable_eq (β i)] : decidable_eq (Π₀ i, β i) :=
assume f g, decidable_of_iff (f.support = g.support ∧ (∀i∈f.support, f i = g i))
⟨assume ⟨h₁, h₂⟩, ext $ assume i,
if h : i ∈ f.support then h₂ i h else
have hf : f i = 0, by rwa [mem_support_iff, not_not] at h,
have hg : g i = 0, by rwa [h₁, mem_support_iff, not_not] at h,
by rw [hf, hg],
by { rintro rfl, simp }⟩
section equiv
open finset
variables {κ : Type*}
/--Reindexing (and possibly removing) terms of a dfinsupp.-/
noncomputable def comap_domain [Π i, has_zero (β i)] (h : κ → ι) (hh : function.injective h) :
(Π₀ i, β i) → Π₀ k, β (h k) :=
begin
refine quotient.lift (λ f, ⟦_⟧) (λ f f' h, _),
exact { to_fun := λ x, f.to_fun (h x),
pre_support := (f.pre_support.to_finset.preimage h (hh.inj_on _)).val,
zero := λ x, (f.zero (h x)).imp_left $ λ hx, mem_preimage.mpr $ multiset.mem_to_finset.mpr hx },
exact quot.sound (λ x, h _)
end
@[simp] lemma comap_domain_apply [Π i, has_zero (β i)] (h : κ → ι) (hh : function.injective h)
(f : Π₀ i, β i) (k : κ) :
comap_domain h hh f k = f (h k) :=
by { rcases f, refl }
@[simp] lemma comap_domain_zero [Π i, has_zero (β i)] (h : κ → ι) (hh : function.injective h) :
comap_domain h hh (0 : Π₀ i, β i) = 0 :=
by { ext, rw [zero_apply, comap_domain_apply, zero_apply] }
@[simp] lemma comap_domain_add [Π i, add_zero_class (β i)] (h : κ → ι) (hh : function.injective h)
(f g : Π₀ i, β i) :
comap_domain h hh (f + g) = comap_domain h hh f + comap_domain h hh g :=
by { ext, rw [add_apply, comap_domain_apply, comap_domain_apply, comap_domain_apply, add_apply] }
@[simp] lemma comap_domain_smul [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)]
(h : κ → ι) (hh : function.injective h) (r : γ) (f : Π₀ i, β i) :
comap_domain h hh (r • f) = r • comap_domain h hh f :=
by { ext, rw [smul_apply, comap_domain_apply, smul_apply, comap_domain_apply] }
@[simp] lemma comap_domain_single [decidable_eq κ] [Π i, has_zero (β i)]
(h : κ → ι) (hh : function.injective h) (k : κ) (x : β (h k)) :
comap_domain h hh (single (h k) x) = single k x :=
begin
ext,
rw comap_domain_apply,
obtain rfl | hik := decidable.eq_or_ne i k,
{ rw [single_eq_same, single_eq_same] },
{ rw [single_eq_of_ne hik.symm, single_eq_of_ne (hh.ne hik.symm)] },
end
omit dec
/--A computable version of comap_domain when an explicit left inverse is provided.-/
def comap_domain'[Π i, has_zero (β i)] (h : κ → ι) {h' : ι → κ} (hh' : function.left_inverse h' h) :
(Π₀ i, β i) → (Π₀ k, β (h k)) :=
begin
refine quotient.lift (λ f, ⟦_⟧) (λ f f' h, _),
exact { to_fun := λ x, f.to_fun (h x),
pre_support := f.pre_support.map h',
zero := λ x, (f.zero (h x)).imp_left $ λ hx, multiset.mem_map.mpr ⟨_, hx, hh' _⟩ },
exact quot.sound (λ x, h _),
end
@[simp] lemma comap_domain'_apply [Π i, has_zero (β i)] (h : κ → ι) {h' : ι → κ}
(hh' : function.left_inverse h' h) (f : Π₀ i, β i) (k : κ) : comap_domain' h hh' f k = f (h k) :=
by { rcases f, refl }
@[simp] lemma comap_domain'_zero [Π i, has_zero (β i)] (h : κ → ι) {h' : ι → κ}
(hh' : function.left_inverse h' h) :
comap_domain' h hh' (0 : Π₀ i, β i) = 0 :=
by { ext, rw [zero_apply, comap_domain'_apply, zero_apply] }
@[simp] lemma comap_domain'_add [Π i, add_zero_class (β i)] (h : κ → ι) {h' : ι → κ}
(hh' : function.left_inverse h' h) (f g : Π₀ i, β i) :
comap_domain' h hh' (f + g) = comap_domain' h hh' f + comap_domain' h hh' g :=
by { ext, rw [add_apply, comap_domain'_apply, comap_domain'_apply, comap_domain'_apply, add_apply] }
@[simp] lemma comap_domain'_smul [monoid γ] [Π i, add_monoid (β i)]
[Π i, distrib_mul_action γ (β i)] (h : κ → ι) {h' : ι → κ}
(hh' : function.left_inverse h' h) (r : γ) (f : Π₀ i, β i) :
comap_domain' h hh' (r • f) = r • comap_domain' h hh' f :=
by { ext, rw [smul_apply, comap_domain'_apply, smul_apply, comap_domain'_apply] }
@[simp] lemma comap_domain'_single [decidable_eq ι] [decidable_eq κ] [Π i, has_zero (β i)]
(h : κ → ι) {h' : ι → κ} (hh' : function.left_inverse h' h) (k : κ) (x : β (h k)) :
comap_domain' h hh' (single (h k) x) = single k x :=
begin
ext,
rw comap_domain'_apply,
obtain rfl | hik := decidable.eq_or_ne i k,
{ rw [single_eq_same, single_eq_same] },
{ rw [single_eq_of_ne hik.symm, single_eq_of_ne (hh'.injective.ne hik.symm)] },
end
/-- Reindexing terms of a dfinsupp.
This is the dfinsupp version of `equiv.Pi_congr_left'`. -/
@[simps apply]
def equiv_congr_left [Π i, has_zero (β i)] (h : ι ≃ κ) : (Π₀ i, β i) ≃ (Π₀ k, β (h.symm k)) :=
{ to_fun := comap_domain' h.symm h.right_inv,
inv_fun := λ f, map_range (λ i, equiv.cast $ congr_arg β $ h.symm_apply_apply i)
(λ i, (equiv.cast_eq_iff_heq _).mpr $
by { convert heq.rfl, repeat { exact (h.symm_apply_apply i).symm } })
(@comap_domain' _ _ _ _ h _ h.left_inv f),
left_inv := λ f, by { ext i, rw [map_range_apply, comap_domain'_apply, comap_domain'_apply,
equiv.cast_eq_iff_heq, h.symm_apply_apply] },
right_inv := λ f, by { ext k, rw [comap_domain'_apply, map_range_apply, comap_domain'_apply,
equiv.cast_eq_iff_heq, h.apply_symm_apply] } }
section curry
variables {α : ι → Type*} {δ : Π i, α i → Type v}
-- lean can't find these instances
instance has_add₂ [Π i j, add_zero_class (δ i j)] : has_add (Π₀ (i : ι) (j : α i), δ i j) :=
@dfinsupp.has_add ι (λ i, Π₀ j, δ i j) _
instance add_zero_class₂ [Π i j, add_zero_class (δ i j)] :
add_zero_class (Π₀ (i : ι) (j : α i), δ i j) :=
@dfinsupp.add_zero_class ι (λ i, Π₀ j, δ i j) _
instance add_monoid₂ [Π i j, add_monoid (δ i j)] :
add_monoid (Π₀ (i : ι) (j : α i), δ i j) :=
@dfinsupp.add_monoid ι (λ i, Π₀ j, δ i j) _
instance distrib_mul_action₂ [monoid γ] [Π i j, add_monoid (δ i j)]
[Π i j, distrib_mul_action γ (δ i j)] :
distrib_mul_action γ (Π₀ (i : ι) (j : α i), δ i j) :=
@dfinsupp.distrib_mul_action ι _ (λ i, Π₀ j, δ i j) _ _ _
/--The natural map between `Π₀ (i : Σ i, α i), δ i.1 i.2` and `Π₀ i (j : α i), δ i j`. -/
noncomputable def sigma_curry [Π i j, has_zero (δ i j)] (f : Π₀ (i : Σ i, _), δ i.1 i.2) :
Π₀ i j, δ i j :=
by { classical,
exact mk (f.support.image $ λ i, i.1)
(λ i, mk (f.support.preimage (sigma.mk i) $ sigma_mk_injective.inj_on _) $ λ j, f ⟨i, j⟩) }
@[simp] lemma sigma_curry_apply [Π i j, has_zero (δ i j)] (f : Π₀ (i : Σ i, _), δ i.1 i.2)
(i : ι) (j : α i) :
sigma_curry f i j = f ⟨i, j⟩ :=
begin
dunfold sigma_curry, by_cases h : f ⟨i, j⟩ = 0,
{ rw [h, mk_apply], split_ifs, { rw mk_apply, split_ifs, { exact h }, { refl } }, { refl } },
{ rw [mk_of_mem, mk_of_mem], { refl },
{ rw [mem_preimage, mem_support_to_fun], exact h },
{ rw mem_image, refine ⟨⟨i, j⟩, _, rfl⟩, rw mem_support_to_fun, exact h } }
end
@[simp] lemma sigma_curry_zero [Π i j, has_zero (δ i j)] :
sigma_curry (0 : Π₀ (i : Σ i, _), δ i.1 i.2) = 0 :=
by { ext i j, rw sigma_curry_apply, refl }
@[simp] lemma sigma_curry_add [Π i j, add_zero_class (δ i j)] (f g : Π₀ (i : Σ i, α i), δ i.1 i.2) :
@sigma_curry _ _ δ _ (f + g) = (@sigma_curry _ _ δ _ f + @sigma_curry ι α δ _ g) :=
begin
ext i j,
rw [@add_apply _ (λ i, Π₀ j, δ i j) _ (sigma_curry _), add_apply, sigma_curry_apply,
sigma_curry_apply, sigma_curry_apply, add_apply]
end
@[simp] lemma sigma_curry_smul [monoid γ] [Π i j, add_monoid (δ i j)]
[Π i j, distrib_mul_action γ (δ i j)] (r : γ) (f : Π₀ (i : Σ i, α i), δ i.1 i.2) :
@sigma_curry _ _ δ _ (r • f) = r • @sigma_curry _ _ δ _ f :=
begin
ext i j,
rw [@smul_apply _ _ (λ i, Π₀ j, δ i j) _ _ _ _ (sigma_curry _), smul_apply, sigma_curry_apply,
sigma_curry_apply, smul_apply]
end
@[simp] lemma sigma_curry_single [Π i j, has_zero (δ i j)] (ij : Σ i, α i) (x : δ ij.1 ij.2) :
sigma_curry (single ij x) = single ij.1 (single ij.2 x : Π₀ j, δ ij.1 j) :=
begin
obtain ⟨i, j⟩ := ij,
ext i' j',
dsimp only,
rw sigma_curry_apply,
obtain rfl | hi := eq_or_ne i i',
{ rw single_eq_same,
obtain rfl | hj := eq_or_ne j j',
{ rw [single_eq_same, single_eq_same] },
{ rw [single_eq_of_ne, single_eq_of_ne hj],
simpa using hj }, },
{ rw [single_eq_of_ne, single_eq_of_ne hi, zero_apply],
simpa using hi },
end
/--The natural map between `Π₀ i (j : α i), δ i j` and `Π₀ (i : Σ i, α i), δ i.1 i.2`, inverse of
`curry`.-/
noncomputable def sigma_uncurry [Π i j, has_zero (δ i j)] (f : Π₀ i j, δ i j) :
Π₀ (i : Σ i, _), δ i.1 i.2 :=
by { classical,
exact mk (f.support.bUnion $ λ i, (f i).support.image $ sigma.mk i) (λ ⟨⟨i, j⟩, _⟩, f i j) }
@[simp] lemma sigma_uncurry_apply [Π i j, has_zero (δ i j)] (f : Π₀ i j, δ i j) (i : ι) (j : α i) :
sigma_uncurry f ⟨i, j⟩ = f i j :=
begin
dunfold sigma_uncurry, by_cases h : f i j = 0,
{ rw mk_apply, split_ifs, { refl }, { exact h.symm } },
{ apply mk_of_mem, rw mem_bUnion, refine ⟨i, _, _⟩,
{ rw mem_support_to_fun, intro H, rw ext_iff at H, exact h (H j) },
{ apply mem_image_of_mem, rw mem_support_to_fun, exact h } }
end
@[simp] lemma sigma_uncurry_zero [Π i j, has_zero (δ i j)] :
sigma_uncurry (0 : Π₀ i j, δ i j) = 0 :=
by { ext ⟨i, j⟩, rw sigma_uncurry_apply, refl }
@[simp] lemma sigma_uncurry_add [Π i j, add_zero_class (δ i j)] (f g : Π₀ i j, δ i j) :
sigma_uncurry (f + g) = sigma_uncurry f + sigma_uncurry g :=
by { ext ⟨i, j⟩, rw [add_apply, sigma_uncurry_apply,
sigma_uncurry_apply, sigma_uncurry_apply, @add_apply _ (λ i, Π₀ j, δ i j) _, add_apply] }
@[simp] lemma sigma_uncurry_smul [monoid γ] [Π i j, add_monoid (δ i j)]
[Π i j, distrib_mul_action γ (δ i j)] (r : γ) (f : Π₀ i j, δ i j) :
sigma_uncurry (r • f) = r • sigma_uncurry f :=
by { ext ⟨i, j⟩, rw [smul_apply, sigma_uncurry_apply,
sigma_uncurry_apply, @smul_apply _ _ (λ i, Π₀ j, δ i j) _ _ _, smul_apply] }
@[simp] lemma sigma_uncurry_single [Π i j, has_zero (δ i j)] (i) (j : α i) (x : δ i j) :
sigma_uncurry (single i (single j x : Π₀ (j : α i), δ i j)) = single ⟨i, j⟩ x:=
begin
ext ⟨i', j'⟩,
dsimp only,
rw sigma_uncurry_apply,
obtain rfl | hi := eq_or_ne i i',
{ rw single_eq_same,
obtain rfl | hj := eq_or_ne j j',
{ rw [single_eq_same, single_eq_same] },
{ rw [single_eq_of_ne hj, single_eq_of_ne],
simpa using hj }, },
{ rw [single_eq_of_ne hi, single_eq_of_ne, zero_apply],
simpa using hi },
end
/--The natural bijection between `Π₀ (i : Σ i, α i), δ i.1 i.2` and `Π₀ i (j : α i), δ i j`.
This is the dfinsupp version of `equiv.Pi_curry`. -/
noncomputable def sigma_curry_equiv [Π i j, has_zero (δ i j)] :
(Π₀ (i : Σ i, _), δ i.1 i.2) ≃ Π₀ i j, δ i j :=
{ to_fun := sigma_curry,
inv_fun := sigma_uncurry,
left_inv := λ f, by { ext ⟨i, j⟩, rw [sigma_uncurry_apply, sigma_curry_apply] },
right_inv := λ f, by { ext i j, rw [sigma_curry_apply, sigma_uncurry_apply] } }
end curry
variables {α : option ι → Type v}
/-- Adds a term to a dfinsupp, making a dfinsupp indexed by an `option`.
This is the dfinsupp version of `option.rec`. -/
def extend_with [Π i, has_zero (α i)] (a : α none) : (Π₀ i, α (some i)) → Π₀ i, α i :=
begin
refine quotient.lift (λ f, ⟦_⟧) (λ f f' h, _),
exact { to_fun := option.rec a f.to_fun,
pre_support := none ::ₘ (f.pre_support.map some),
zero := λ i, option.rec (or.inl $ multiset.mem_cons_self _ _)
(λ i, (f.zero i).imp_left $ λ h, multiset.mem_cons_of_mem $ multiset.mem_map_of_mem _ h) i },
{ refine quot.sound (option.rec _ $ λ x, _), refl, exact h x },
end
@[simp] lemma extend_with_none [Π i, has_zero (α i)] (f : Π₀ i, α (some i)) (a : α none) :
f.extend_with a none = a :=
by { rcases f, refl }
@[simp] lemma extend_with_some [Π i, has_zero (α i)] (f : Π₀ i, α (some i)) (a : α none) (i : ι) :
f.extend_with a (some i) = f i :=
by { rcases f, refl }
@[simp] lemma extend_with_single_zero [decidable_eq ι] [Π i, has_zero (α i)]
(i : ι) (x : α (some i)) :
(single i x).extend_with 0 = single (some i) x :=
begin
ext (_ | j),
{ rw [extend_with_none, single_eq_of_ne (option.some_ne_none _)] },
{ rw extend_with_some,
obtain rfl | hij := decidable.eq_or_ne i j,
{ rw [single_eq_same, single_eq_same] },
{ rw [single_eq_of_ne hij, single_eq_of_ne ((option.some_injective _).ne hij)] }, },
end
@[simp] lemma extend_with_zero [decidable_eq ι] [Π i, has_zero (α i)] (x : α none) :
(0 : Π₀ i, α (some i)).extend_with x = single none x :=
begin
ext (_ | j),
{ rw [extend_with_none, single_eq_same] },
{ rw [extend_with_some, single_eq_of_ne (option.some_ne_none _).symm, zero_apply] },
end
include dec
/-- Bijection obtained by separating the term of index `none` of a dfinsupp over `option ι`.
This is the dfinsupp version of `equiv.pi_option_equiv_prod`. -/
@[simps] noncomputable def equiv_prod_dfinsupp [Π i, has_zero (α i)] :
(Π₀ i, α i) ≃ α none × Π₀ i, α (some i) :=
{ to_fun := λ f, (f none, comap_domain some (option.some_injective _) f),
inv_fun := λ f, f.2.extend_with f.1,
left_inv := λ f, begin
ext i, cases i with i,
{ rw extend_with_none },
{ rw [extend_with_some, comap_domain_apply] }
end,
right_inv := λ _, begin
ext,
{ exact extend_with_none _ _ },
{ rw [comap_domain_apply, extend_with_some] }
end }
lemma equiv_prod_dfinsupp_add [Π i, add_zero_class (α i)] (f g : Π₀ i, α i) :
equiv_prod_dfinsupp (f + g) = equiv_prod_dfinsupp f + equiv_prod_dfinsupp g :=
prod.ext (add_apply _ _ _) (comap_domain_add _ _ _ _)
lemma equiv_prod_dfinsupp_smul [monoid γ] [Π i, add_monoid (α i)] [Π i, distrib_mul_action γ (α i)]
(r : γ) (f : Π₀ i, α i) :
equiv_prod_dfinsupp (r • f) = r • equiv_prod_dfinsupp f :=
prod.ext (smul_apply _ _ _) (comap_domain_smul _ _ _ _)
end equiv
section prod_and_sum
/-- `prod f g` is the product of `g i (f i)` over the support of `f`. -/
@[to_additive "`sum f g` is the sum of `g i (f i)` over the support of `f`."]
def prod [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ]
(f : Π₀ i, β i) (g : Π i, β i → γ) : γ :=
∏ i in f.support, g i (f i)
@[to_additive]
lemma prod_map_range_index {β₁ : ι → Type v₁} {β₂ : ι → Type v₂}
[Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)]
[Π i (x : β₁ i), decidable (x ≠ 0)] [Π i (x : β₂ i), decidable (x ≠ 0)] [comm_monoid γ]
{f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} {h : Π i, β₂ i → γ}
(h0 : ∀i, h i 0 = 1) :
(map_range f hf g).prod h = g.prod (λi b, h i (f i b)) :=
begin
rw [map_range_def],
refine (finset.prod_subset support_mk_subset _).trans _,
{ intros i h1 h2,
dsimp, simp [h1] at h2, dsimp at h2,
simp [h1, h2, h0] },
{ refine finset.prod_congr rfl _,
intros i h1,
simp [h1] }
end
@[to_additive]
lemma prod_zero_index [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[comm_monoid γ] {h : Π i, β i → γ} : (0 : Π₀ i, β i).prod h = 1 :=
rfl
@[to_additive]
lemma prod_single_index [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ]
{i : ι} {b : β i} {h : Π i, β i → γ} (h_zero : h i 0 = 1) :
(single i b).prod h = h i b :=
begin
by_cases h : b ≠ 0,
{ simp [dfinsupp.prod, support_single_ne_zero h] },
{ rw [not_not] at h, simp [h, prod_zero_index, h_zero], refl }
end
@[to_additive]
lemma prod_neg_index [Π i, add_group (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ]
{g : Π₀ i, β i} {h : Π i, β i → γ} (h0 : ∀i, h i 0 = 1) :
(-g).prod h = g.prod (λi b, h i (- b)) :=
prod_map_range_index h0
omit dec
@[to_additive]
lemma prod_comm {ι₁ ι₂ : Sort*} {β₁ : ι₁ → Type*} {β₂ : ι₂ → Type*}
[decidable_eq ι₁] [decidable_eq ι₂] [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)]
[Π i (x : β₁ i), decidable (x ≠ 0)] [Π i (x : β₂ i), decidable (x ≠ 0)] [comm_monoid γ]
(f₁ : Π₀ i, β₁ i) (f₂ : Π₀ i, β₂ i) (h : Π i, β₁ i → Π i, β₂ i → γ) :
f₁.prod (λ i₁ x₁, f₂.prod $ λ i₂ x₂, h i₁ x₁ i₂ x₂) =
f₂.prod (λ i₂ x₂, f₁.prod $ λ i₁ x₁, h i₁ x₁ i₂ x₂) := finset.prod_comm
@[simp] lemma sum_apply {ι₁ : Type u₁} [decidable_eq ι₁] {β₁ : ι₁ → Type v₁}
[Π i₁, has_zero (β₁ i₁)] [Π i (x : β₁ i), decidable (x ≠ 0)]
[Π i, add_comm_monoid (β i)]
{f : Π₀ i₁, β₁ i₁} {g : Π i₁, β₁ i₁ → Π₀ i, β i} {i₂ : ι} :
(f.sum g) i₂ = f.sum (λi₁ b, g i₁ b i₂) :=
(eval_add_monoid_hom i₂ : (Π₀ i, β i) →+ β i₂).map_sum _ f.support
include dec
lemma support_sum {ι₁ : Type u₁} [decidable_eq ι₁] {β₁ : ι₁ → Type v₁}
[Π i₁, has_zero (β₁ i₁)] [Π i (x : β₁ i), decidable (x ≠ 0)]
[Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
{f : Π₀ i₁, β₁ i₁} {g : Π i₁, β₁ i₁ → Π₀ i, β i} :
(f.sum g).support ⊆ f.support.bUnion (λi, (g i (f i)).support) :=
have ∀i₁ : ι, f.sum (λ (i : ι₁) (b : β₁ i), (g i b) i₁) ≠ 0 →
(∃ (i : ι₁), f i ≠ 0 ∧ ¬ (g i (f i)) i₁ = 0),
from assume i₁ h,
let ⟨i, hi, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in
⟨i, mem_support_iff.1 hi, ne⟩,
by simpa [finset.subset_iff, mem_support_iff, finset.mem_bUnion, sum_apply] using this
@[simp, to_additive] lemma prod_one [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[comm_monoid γ] {f : Π₀ i, β i} :
f.prod (λi b, (1 : γ)) = 1 :=
finset.prod_const_one
@[simp, to_additive] lemma prod_mul [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[comm_monoid γ] {f : Π₀ i, β i} {h₁ h₂ : Π i, β i → γ} :
f.prod (λi b, h₁ i b * h₂ i b) = f.prod h₁ * f.prod h₂ :=
finset.prod_mul_distrib
@[simp, to_additive] lemma prod_inv [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[comm_group γ] {f : Π₀ i, β i} {h : Π i, β i → γ} :
f.prod (λi b, (h i b)⁻¹) = (f.prod h)⁻¹ :=
((inv_monoid_hom : γ →* γ).map_prod _ f.support).symm
@[to_additive] lemma prod_eq_one [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[comm_monoid γ] {f : Π₀ i, β i} {h : Π i, β i → γ} (hyp : ∀ i, h i (f i) = 1) :
f.prod h = 1 := finset.prod_eq_one $ λ i hi, hyp i
lemma smul_sum {α : Type*} [monoid α] [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[add_comm_monoid γ] [distrib_mul_action α γ] {f : Π₀ i, β i} {h : Π i, β i → γ} {c : α} :
c • f.sum h = f.sum (λ a b, c • h a b) := finset.smul_sum
@[to_additive]
lemma prod_add_index [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[comm_monoid γ] {f g : Π₀ i, β i}
{h : Π i, β i → γ} (h_zero : ∀i, h i 0 = 1) (h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ * h i b₂) :
(f + g).prod h = f.prod h * g.prod h :=
have f_eq : ∏ i in f.support ∪ g.support, h i (f i) = f.prod h,
from (finset.prod_subset (finset.subset_union_left _ _) $
by simp [mem_support_iff, h_zero] {contextual := tt}).symm,
have g_eq : ∏ i in f.support ∪ g.support, h i (g i) = g.prod h,
from (finset.prod_subset (finset.subset_union_right _ _) $
by simp [mem_support_iff, h_zero] {contextual := tt}).symm,
calc ∏ i in (f + g).support, h i ((f + g) i) =
∏ i in f.support ∪ g.support, h i ((f + g) i) :
finset.prod_subset support_add $
by simp [mem_support_iff, h_zero] {contextual := tt}
... = (∏ i in f.support ∪ g.support, h i (f i)) *
(∏ i in f.support ∪ g.support, h i (g i)) :
by simp [h_add, finset.prod_mul_distrib]
... = _ : by rw [f_eq, g_eq]
@[to_additive]
lemma _root_.dfinsupp_prod_mem [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[comm_monoid γ] {S : Type*} [set_like S γ] [submonoid_class S γ] (s : S)
(f : Π₀ i, β i) (g : Π i, β i → γ) (h : ∀ c, f c ≠ 0 → g c (f c) ∈ s) : f.prod g ∈ s :=
prod_mem $ λ i hi, h _ $ mem_support_iff.1 hi
@[simp, to_additive] lemma prod_eq_prod_fintype [fintype ι] [Π i, has_zero (β i)]
[Π (i : ι) (x : β i), decidable (x ≠ 0)] [comm_monoid γ] (v : Π₀ i, β i) [f : Π i, β i → γ]
(hf : ∀ i, f i 0 = 1) :
v.prod f = ∏ i, f i (dfinsupp.equiv_fun_on_fintype v i) :=
begin
suffices : ∏ i in v.support, f i (v i) = ∏ i, f i (v i),
{ simp [dfinsupp.prod, this] },
apply finset.prod_subset v.support.subset_univ,
intros i hi' hi,
rw [mem_support_iff, not_not] at hi,
rw [hi, hf],
end
/--
When summing over an `add_monoid_hom`, the decidability assumption is not needed, and the result is
also an `add_monoid_hom`.
-/
def sum_add_hom [Π i, add_zero_class (β i)] [add_comm_monoid γ] (φ : Π i, β i →+ γ) :
(Π₀ i, β i) →+ γ :=
{ to_fun := (λ f,
quotient.lift_on f (λ x, ∑ i in x.2.to_finset, φ i (x.1 i)) $ λ x y H,
begin
have H1 : x.2.to_finset ∩ y.2.to_finset ⊆ x.2.to_finset, from finset.inter_subset_left _ _,
have H2 : x.2.to_finset ∩ y.2.to_finset ⊆ y.2.to_finset, from finset.inter_subset_right _ _,
refine (finset.sum_subset H1 _).symm.trans
((finset.sum_congr rfl _).trans (finset.sum_subset H2 _)),
{ intros i H1 H2, rw finset.mem_inter at H2, rw H i,
simp only [multiset.mem_to_finset] at H1 H2,
rw [(y.3 i).resolve_left (mt (and.intro H1) H2), add_monoid_hom.map_zero] },
{ intros i H1, rw H i },
{ intros i H1 H2, rw finset.mem_inter at H2, rw ← H i,
simp only [multiset.mem_to_finset] at H1 H2,
rw [(x.3 i).resolve_left (mt (λ H3, and.intro H3 H1) H2), add_monoid_hom.map_zero] }
end),
map_add' := assume f g,
begin
refine quotient.induction_on f (λ x, _),
refine quotient.induction_on g (λ y, _),
change ∑ i in _, _ = (∑ i in _, _) + (∑ i in _, _),
simp only, conv { to_lhs, congr, skip, funext, rw add_monoid_hom.map_add },
simp only [finset.sum_add_distrib],
congr' 1,
{ refine (finset.sum_subset _ _).symm,
{ intro i, simp only [multiset.mem_to_finset, multiset.mem_add], exact or.inl },
{ intros i H1 H2, simp only [multiset.mem_to_finset, multiset.mem_add] at H2,
rw [(x.3 i).resolve_left H2, add_monoid_hom.map_zero] } },
{ refine (finset.sum_subset _ _).symm,
{ intro i, simp only [multiset.mem_to_finset, multiset.mem_add], exact or.inr },
{ intros i H1 H2, simp only [multiset.mem_to_finset, multiset.mem_add] at H2,
rw [(y.3 i).resolve_left H2, add_monoid_hom.map_zero] } }
end,
map_zero' := rfl }
@[simp] lemma sum_add_hom_single [Π i, add_zero_class (β i)] [add_comm_monoid γ]
(φ : Π i, β i →+ γ) (i) (x : β i) : sum_add_hom φ (single i x) = φ i x :=
(add_zero _).trans $ congr_arg (φ i) $ show (if H : i ∈ ({i} : finset _) then x else 0) = x,
from dif_pos $ finset.mem_singleton_self i
@[simp] lemma sum_add_hom_comp_single [Π i, add_zero_class (β i)] [add_comm_monoid γ]
(f : Π i, β i →+ γ) (i : ι) :
(sum_add_hom f).comp (single_add_hom β i) = f i :=
add_monoid_hom.ext $ λ x, sum_add_hom_single f i x
/-- While we didn't need decidable instances to define it, we do to reduce it to a sum -/
lemma sum_add_hom_apply [Π i, add_zero_class (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[add_comm_monoid γ] (φ : Π i, β i →+ γ) (f : Π₀ i, β i) :
sum_add_hom φ f = f.sum (λ x, φ x) :=
begin
refine quotient.induction_on f (λ x, _),
change ∑ i in _, _ = (∑ i in finset.filter _ _, _),
rw [finset.sum_filter, finset.sum_congr rfl],
intros i _,
dsimp only,
split_ifs,
refl,
rw [(not_not.mp h), add_monoid_hom.map_zero],
end
lemma _root_.dfinsupp_sum_add_hom_mem [Π i, add_zero_class (β i)] [add_comm_monoid γ] {S : Type*}
[set_like S γ] [add_submonoid_class S γ] (s : S) (f : Π₀ i, β i) (g : Π i, β i →+ γ)
(h : ∀ c, f c ≠ 0 → g c (f c) ∈ s) : dfinsupp.sum_add_hom g f ∈ s :=
begin
classical,
rw dfinsupp.sum_add_hom_apply,
convert dfinsupp_sum_mem _ _ _ _,
{ apply_instance },
exact h
end
/-- The supremum of a family of commutative additive submonoids is equal to the range of
`dfinsupp.sum_add_hom`; that is, every element in the `supr` can be produced from taking a finite
number of non-zero elements of `S i`, coercing them to `γ`, and summing them. -/
lemma _root_.add_submonoid.supr_eq_mrange_dfinsupp_sum_add_hom [add_comm_monoid γ]
(S : ι → add_submonoid γ) : supr S = (dfinsupp.sum_add_hom (λ i, (S i).subtype)).mrange :=
begin
apply le_antisymm,
{ apply supr_le _,
intros i y hy,
exact ⟨dfinsupp.single i ⟨y, hy⟩, dfinsupp.sum_add_hom_single _ _ _⟩, },
{ rintros x ⟨v, rfl⟩,
exact dfinsupp_sum_add_hom_mem _ v _ (λ i _, (le_supr S i : S i ≤ _) (v i).prop) }
end
/-- The bounded supremum of a family of commutative additive submonoids is equal to the range of
`dfinsupp.sum_add_hom` composed with `dfinsupp.filter_add_monoid_hom`; that is, every element in the
bounded `supr` can be produced from taking a finite number of non-zero elements from the `S i` that
satisfy `p i`, coercing them to `γ`, and summing them. -/
lemma _root_.add_submonoid.bsupr_eq_mrange_dfinsupp_sum_add_hom (p : ι → Prop)
[decidable_pred p] [add_comm_monoid γ] (S : ι → add_submonoid γ) :
(⨆ i (h : p i), S i) =
((sum_add_hom (λ i, (S i).subtype)).comp (filter_add_monoid_hom _ p)).mrange :=
begin
apply le_antisymm,
{ refine supr₂_le (λ i hi y hy, ⟨dfinsupp.single i ⟨y, hy⟩, _⟩),
rw [add_monoid_hom.comp_apply, filter_add_monoid_hom_apply, filter_single_pos _ _ hi],
exact sum_add_hom_single _ _ _, },
{ rintros x ⟨v, rfl⟩,
refine dfinsupp_sum_add_hom_mem _ _ _ (λ i hi, _),
refine add_submonoid.mem_supr_of_mem i _,
by_cases hp : p i,
{ simp [hp], },
{ simp [hp] }, }
end
lemma _root_.add_submonoid.mem_supr_iff_exists_dfinsupp [add_comm_monoid γ]
(S : ι → add_submonoid γ) (x : γ) :
x ∈ supr S ↔ ∃ f : Π₀ i, S i, dfinsupp.sum_add_hom (λ i, (S i).subtype) f = x :=
set_like.ext_iff.mp (add_submonoid.supr_eq_mrange_dfinsupp_sum_add_hom S) x
/-- A variant of `add_submonoid.mem_supr_iff_exists_dfinsupp` with the RHS fully unfolded. -/
lemma _root_.add_submonoid.mem_supr_iff_exists_dfinsupp' [add_comm_monoid γ]
(S : ι → add_submonoid γ) [Π i (x : S i), decidable (x ≠ 0)] (x : γ) :
x ∈ supr S ↔ ∃ f : Π₀ i, S i, f.sum (λ i xi, ↑xi) = x :=
begin
rw add_submonoid.mem_supr_iff_exists_dfinsupp,
simp_rw sum_add_hom_apply,
congr',
end
lemma _root_.add_submonoid.mem_bsupr_iff_exists_dfinsupp (p : ι → Prop)
[decidable_pred p] [add_comm_monoid γ] (S : ι → add_submonoid γ) (x : γ) :
x ∈ (⨆ i (h : p i), S i) ↔
∃ f : Π₀ i, S i, dfinsupp.sum_add_hom (λ i, (S i).subtype) (f.filter p) = x :=
set_like.ext_iff.mp (add_submonoid.bsupr_eq_mrange_dfinsupp_sum_add_hom p S) x
omit dec
lemma sum_add_hom_comm {ι₁ ι₂ : Sort*} {β₁ : ι₁ → Type*} {β₂ : ι₂ → Type*} {γ : Type*}
[decidable_eq ι₁] [decidable_eq ι₂] [Π i, add_zero_class (β₁ i)] [Π i, add_zero_class (β₂ i)]
[add_comm_monoid γ]
(f₁ : Π₀ i, β₁ i) (f₂ : Π₀ i, β₂ i) (h : Π i j, β₁ i →+ β₂ j →+ γ) :
sum_add_hom (λ i₂, sum_add_hom (λ i₁, h i₁ i₂) f₁) f₂ =
sum_add_hom (λ i₁, sum_add_hom (λ i₂, (h i₁ i₂).flip) f₂) f₁ :=
begin
refine quotient.induction_on₂ f₁ f₂ (λ x₁ x₂, _),
simp only [sum_add_hom, add_monoid_hom.finset_sum_apply, quotient.lift_on_mk,
add_monoid_hom.coe_mk, add_monoid_hom.flip_apply],
exact finset.sum_comm,
end
include dec
/-- The `dfinsupp` version of `finsupp.lift_add_hom`,-/
@[simps apply symm_apply]
def lift_add_hom [Π i, add_zero_class (β i)] [add_comm_monoid γ] :
(Π i, β i →+ γ) ≃+ ((Π₀ i, β i) →+ γ) :=
{ to_fun := sum_add_hom,
inv_fun := λ F i, F.comp (single_add_hom β i),
left_inv := λ x, by { ext, simp },
right_inv := λ ψ, by { ext, simp },
map_add' := λ F G, by { ext, simp } }
/-- The `dfinsupp` version of `finsupp.lift_add_hom_single_add_hom`,-/
@[simp] lemma lift_add_hom_single_add_hom [Π i, add_comm_monoid (β i)] :
lift_add_hom (single_add_hom β) = add_monoid_hom.id (Π₀ i, β i) :=
lift_add_hom.to_equiv.apply_eq_iff_eq_symm_apply.2 rfl
/-- The `dfinsupp` version of `finsupp.lift_add_hom_apply_single`,-/
lemma lift_add_hom_apply_single [Π i, add_zero_class (β i)] [add_comm_monoid γ]
(f : Π i, β i →+ γ) (i : ι) (x : β i) :
lift_add_hom f (single i x) = f i x :=
by simp
/-- The `dfinsupp` version of `finsupp.lift_add_hom_comp_single`,-/
lemma lift_add_hom_comp_single [Π i, add_zero_class (β i)] [add_comm_monoid γ]
(f : Π i, β i →+ γ) (i : ι) :
(lift_add_hom f).comp (single_add_hom β i) = f i :=
by simp
/-- The `dfinsupp` version of `finsupp.comp_lift_add_hom`,-/
lemma comp_lift_add_hom {δ : Type*} [Π i, add_zero_class (β i)] [add_comm_monoid γ]
[add_comm_monoid δ] (g : γ →+ δ) (f : Π i, β i →+ γ) :
g.comp (lift_add_hom f) = lift_add_hom (λ a, g.comp (f a)) :=
lift_add_hom.symm_apply_eq.1 $ funext $ λ a,
by rw [lift_add_hom_symm_apply, add_monoid_hom.comp_assoc, lift_add_hom_comp_single]
@[simp]
lemma sum_add_hom_zero [Π i, add_zero_class (β i)] [add_comm_monoid γ] :
sum_add_hom (λ i, (0 : β i →+ γ)) = 0 :=
(lift_add_hom : (Π i, β i →+ γ) ≃+ _).map_zero
@[simp]
lemma sum_add_hom_add [Π i, add_zero_class (β i)] [add_comm_monoid γ]
(g : Π i, β i →+ γ) (h : Π i, β i →+ γ) :
sum_add_hom (λ i, g i + h i) = sum_add_hom g + sum_add_hom h :=
lift_add_hom.map_add _ _
@[simp]
lemma sum_add_hom_single_add_hom [Π i, add_comm_monoid (β i)] :
sum_add_hom (single_add_hom β) = add_monoid_hom.id _ :=
lift_add_hom_single_add_hom
lemma comp_sum_add_hom {δ : Type*} [Π i, add_zero_class (β i)] [add_comm_monoid γ]
[add_comm_monoid δ] (g : γ →+ δ) (f : Π i, β i →+ γ) :
g.comp (sum_add_hom f) = sum_add_hom (λ a, g.comp (f a)) :=
comp_lift_add_hom _ _
lemma sum_sub_index [Π i, add_group (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[add_comm_group γ] {f g : Π₀ i, β i}
{h : Π i, β i → γ} (h_sub : ∀i b₁ b₂, h i (b₁ - b₂) = h i b₁ - h i b₂) :
(f - g).sum h = f.sum h - g.sum h :=
begin
have := (lift_add_hom (λ a, add_monoid_hom.of_map_sub (h a) (h_sub a))).map_sub f g,
rw [lift_add_hom_apply, sum_add_hom_apply, sum_add_hom_apply, sum_add_hom_apply] at this,
exact this,
end
@[to_additive]
lemma prod_finset_sum_index {γ : Type w} {α : Type x}
[Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[comm_monoid γ]
{s : finset α} {g : α → Π₀ i, β i}
{h : Π i, β i → γ} (h_zero : ∀i, h i 0 = 1) (h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ * h i b₂) :
∏ i in s, (g i).prod h = (∑ i in s, g i).prod h :=
begin
classical,
exact finset.induction_on s
(by simp [prod_zero_index])
(by simp [prod_add_index, h_zero, h_add] {contextual := tt})
end
@[to_additive]
lemma prod_sum_index {ι₁ : Type u₁} [decidable_eq ι₁] {β₁ : ι₁ → Type v₁}
[Π i₁, has_zero (β₁ i₁)] [Π i (x : β₁ i), decidable (x ≠ 0)]
[Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[comm_monoid γ]
{f : Π₀ i₁, β₁ i₁} {g : Π i₁, β₁ i₁ → Π₀ i, β i}
{h : Π i, β i → γ} (h_zero : ∀i, h i 0 = 1) (h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ * h i b₂) :
(f.sum g).prod h = f.prod (λi b, (g i b).prod h) :=
(prod_finset_sum_index h_zero h_add).symm
@[simp] lemma sum_single [Π i, add_comm_monoid (β i)]
[Π i (x : β i), decidable (x ≠ 0)] {f : Π₀ i, β i} :
f.sum single = f :=
begin
have := add_monoid_hom.congr_fun lift_add_hom_single_add_hom f,
rw [lift_add_hom_apply, sum_add_hom_apply] at this,
exact this,
end
@[to_additive]
lemma prod_subtype_domain_index [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[comm_monoid γ] {v : Π₀ i, β i} {p : ι → Prop} [decidable_pred p]
{h : Π i, β i → γ} (hp : ∀ x ∈ v.support, p x) :
(v.subtype_domain p).prod (λi b, h i b) = v.prod h :=
finset.prod_bij (λp _, p)
(by simp) (by simp)
(assume ⟨a₀, ha₀⟩ ⟨a₁, ha₁⟩, by simp)
(λ i hi, ⟨⟨i, hp i hi⟩, by simpa using hi, rfl⟩)
omit dec
lemma subtype_domain_sum [Π i, add_comm_monoid (β i)]
{s : finset γ} {h : γ → Π₀ i, β i} {p : ι → Prop} [decidable_pred p] :
(∑ c in s, h c).subtype_domain p = ∑ c in s, (h c).subtype_domain p :=
(subtype_domain_add_monoid_hom β p).map_sum _ s
lemma subtype_domain_finsupp_sum {δ : γ → Type x} [decidable_eq γ]
[Π c, has_zero (δ c)] [Π c (x : δ c), decidable (x ≠ 0)]
[Π i, add_comm_monoid (β i)]
{p : ι → Prop} [decidable_pred p]
{s : Π₀ c, δ c} {h : Π c, δ c → Π₀ i, β i} :
(s.sum h).subtype_domain p = s.sum (λc d, (h c d).subtype_domain p) :=
subtype_domain_sum
end prod_and_sum
/-! ### Bundled versions of `dfinsupp.map_range`
The names should match the equivalent bundled `finsupp.map_range` definitions.
-/
section map_range
omit dec
variables [Π i, add_zero_class (β i)] [Π i, add_zero_class (β₁ i)] [Π i, add_zero_class (β₂ i)]
lemma map_range_add (f : Π i, β₁ i → β₂ i) (hf : ∀ i, f i 0 = 0)
(hf' : ∀ i x y, f i (x + y) = f i x + f i y) (g₁ g₂ : Π₀ i, β₁ i):
map_range f hf (g₁ + g₂) = map_range f hf g₁ + map_range f hf g₂ :=
begin
ext,
simp only [map_range_apply f, coe_add, pi.add_apply, hf']
end
/-- `dfinsupp.map_range` as an `add_monoid_hom`. -/
@[simps apply]
def map_range.add_monoid_hom (f : Π i, β₁ i →+ β₂ i) : (Π₀ i, β₁ i) →+ (Π₀ i, β₂ i) :=
{ to_fun := map_range (λ i x, f i x) (λ i, (f i).map_zero),
map_zero' := map_range_zero _ _,
map_add' := map_range_add _ _ (λ i, (f i).map_add) }
@[simp]
lemma map_range.add_monoid_hom_id :
map_range.add_monoid_hom (λ i, add_monoid_hom.id (β₂ i)) = add_monoid_hom.id _ :=
add_monoid_hom.ext map_range_id
lemma map_range.add_monoid_hom_comp (f : Π i, β₁ i →+ β₂ i) (f₂ : Π i, β i →+ β₁ i):
map_range.add_monoid_hom (λ i, (f i).comp (f₂ i)) =
(map_range.add_monoid_hom f).comp (map_range.add_monoid_hom f₂) :=
add_monoid_hom.ext $ map_range_comp (λ i x, f i x) (λ i x, f₂ i x) _ _ _
/-- `dfinsupp.map_range.add_monoid_hom` as an `add_equiv`. -/
@[simps apply]
def map_range.add_equiv (e : Π i, β₁ i ≃+ β₂ i) : (Π₀ i, β₁ i) ≃+ (Π₀ i, β₂ i) :=
{ to_fun := map_range (λ i x, e i x) (λ i, (e i).map_zero),
inv_fun := map_range (λ i x, (e i).symm x) (λ i, (e i).symm.map_zero),
left_inv := λ x, by rw ←map_range_comp; { simp_rw add_equiv.symm_comp_self, simp },
right_inv := λ x, by rw ←map_range_comp; { simp_rw add_equiv.self_comp_symm, simp },
.. map_range.add_monoid_hom (λ i, (e i).to_add_monoid_hom) }
@[simp]
lemma map_range.add_equiv_refl :
(map_range.add_equiv $ λ i, add_equiv.refl (β₁ i)) = add_equiv.refl _ :=
add_equiv.ext map_range_id
lemma map_range.add_equiv_trans (f : Π i, β i ≃+ β₁ i) (f₂ : Π i, β₁ i ≃+ β₂ i):
map_range.add_equiv (λ i, (f i).trans (f₂ i)) =
(map_range.add_equiv f).trans (map_range.add_equiv f₂) :=
add_equiv.ext $ map_range_comp (λ i x, f₂ i x) (λ i x, f i x) _ _ _
@[simp]
lemma map_range.add_equiv_symm (e : Π i, β₁ i ≃+ β₂ i) :
(map_range.add_equiv e).symm = map_range.add_equiv (λ i, (e i).symm) := rfl
end map_range
end dfinsupp
/-! ### Product and sum lemmas for bundled morphisms.
In this section, we provide analogues of `add_monoid_hom.map_sum`, `add_monoid_hom.coe_finset_sum`,
and `add_monoid_hom.finset_sum_apply` for `dfinsupp.sum` and `dfinsupp.sum_add_hom` instead of
`finset.sum`.
We provide these for `add_monoid_hom`, `monoid_hom`, `ring_hom`, `add_equiv`, and `mul_equiv`.
Lemmas for `linear_map` and `linear_equiv` are in another file.
-/
section
variables [decidable_eq ι]
namespace monoid_hom
variables {R S : Type*}
variables [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)]
@[simp, to_additive]
lemma map_dfinsupp_prod [comm_monoid R] [comm_monoid S]
(h : R →* S) (f : Π₀ i, β i) (g : Π i, β i → R) :
h (f.prod g) = f.prod (λ a b, h (g a b)) := h.map_prod _ _
@[to_additive]
lemma coe_dfinsupp_prod [monoid R] [comm_monoid S]
(f : Π₀ i, β i) (g : Π i, β i → R →* S) :
⇑(f.prod g) = f.prod (λ a b, (g a b)) := coe_finset_prod _ _
@[simp, to_additive]
lemma dfinsupp_prod_apply [monoid R] [comm_monoid S]
(f : Π₀ i, β i) (g : Π i, β i → R →* S) (r : R) :
(f.prod g) r = f.prod (λ a b, (g a b) r) := finset_prod_apply _ _ _
end monoid_hom
namespace ring_hom
variables {R S : Type*}
variables [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)]
@[simp]
lemma map_dfinsupp_prod [comm_semiring R] [comm_semiring S]
(h : R →+* S) (f : Π₀ i, β i) (g : Π i, β i → R) :
h (f.prod g) = f.prod (λ a b, h (g a b)) := h.map_prod _ _
@[simp]
lemma map_dfinsupp_sum [non_assoc_semiring R] [non_assoc_semiring S]
(h : R →+* S) (f : Π₀ i, β i) (g : Π i, β i → R) :
h (f.sum g) = f.sum (λ a b, h (g a b)) := h.map_sum _ _
end ring_hom
namespace mul_equiv
variables {R S : Type*}
variables [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)]
@[simp, to_additive]
lemma map_dfinsupp_prod [comm_monoid R] [comm_monoid S]
(h : R ≃* S) (f : Π₀ i, β i) (g : Π i, β i → R) :
h (f.prod g) = f.prod (λ a b, h (g a b)) := h.map_prod _ _
end mul_equiv
/-! The above lemmas, repeated for `dfinsupp.sum_add_hom`. -/
namespace add_monoid_hom
variables {R S : Type*}
open dfinsupp
@[simp]
lemma map_dfinsupp_sum_add_hom [add_comm_monoid R] [add_comm_monoid S] [Π i, add_zero_class (β i)]
(h : R →+ S) (f : Π₀ i, β i) (g : Π i, β i →+ R) :
h (sum_add_hom g f) = sum_add_hom (λ i, h.comp (g i)) f :=
congr_fun (comp_lift_add_hom h g) f
@[simp]
lemma dfinsupp_sum_add_hom_apply [add_zero_class R] [add_comm_monoid S] [Π i, add_zero_class (β i)]
(f : Π₀ i, β i) (g : Π i, β i →+ R →+ S) (r : R) :
(sum_add_hom g f) r = sum_add_hom (λ i, (eval r).comp (g i)) f :=
map_dfinsupp_sum_add_hom (eval r) f g
lemma coe_dfinsupp_sum_add_hom [add_zero_class R] [add_comm_monoid S] [Π i, add_zero_class (β i)]
(f : Π₀ i, β i) (g : Π i, β i →+ R →+ S) :
⇑(sum_add_hom g f) = sum_add_hom (λ i, (coe_fn R S).comp (g i)) f :=
map_dfinsupp_sum_add_hom (coe_fn R S) f g
end add_monoid_hom
namespace ring_hom
variables {R S : Type*}
open dfinsupp
@[simp]
lemma map_dfinsupp_sum_add_hom [non_assoc_semiring R] [non_assoc_semiring S]
[Π i, add_zero_class (β i)] (h : R →+* S) (f : Π₀ i, β i) (g : Π i, β i →+ R) :
h (sum_add_hom g f) = sum_add_hom (λ i, h.to_add_monoid_hom.comp (g i)) f :=
add_monoid_hom.congr_fun (comp_lift_add_hom h.to_add_monoid_hom g) f
end ring_hom
namespace add_equiv
variables {R S : Type*}
open dfinsupp
@[simp]
lemma map_dfinsupp_sum_add_hom [add_comm_monoid R] [add_comm_monoid S] [Π i, add_zero_class (β i)]
(h : R ≃+ S) (f : Π₀ i, β i) (g : Π i, β i →+ R) :
h (sum_add_hom g f) = sum_add_hom (λ i, h.to_add_monoid_hom.comp (g i)) f :=
add_monoid_hom.congr_fun (comp_lift_add_hom h.to_add_monoid_hom g) f
end add_equiv
end
|
86ed94211b6f2902da2e0a56f83e05376154264e | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/algebra/big_operators/order.lean | 9191c85ad3fe08d2babd9eb3361132ba5d710adb | [
"Apache-2.0"
] | permissive | hikari0108/mathlib | b7ea2b7350497ab1a0b87a09d093ecc025a50dfa | a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901 | refs/heads/master | 1,690,483,608,260 | 1,631,541,580,000 | 1,631,541,580,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 21,663 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import algebra.absolute_value
import algebra.big_operators.basic
/-!
# Results about big operators with values in an ordered algebraic structure.
Mostly monotonicity results for the `∏` and `∑` operations.
-/
open_locale big_operators
variables {ι α β M N G k R : Type*}
namespace finset
section
variables [comm_monoid M] [ordered_comm_monoid N]
/-- Let `{x | p x}` be a subsemigroup of a commutative monoid `M`. Let `f : M → N` be a map
submultiplicative on `{x | p x}`, i.e., `p x → p y → f (x * y) ≤ f x * f y`. Let `g i`, `i ∈ s`, be
a nonempty finite family of elements of `M` such that `∀ i ∈ s, p (g i)`. Then
`f (∏ x in s, g x) ≤ ∏ x in s, f (g x)`. -/
@[to_additive le_sum_nonempty_of_subadditive_on_pred]
lemma le_prod_nonempty_of_submultiplicative_on_pred
(f : M → N) (p : M → Prop) (h_mul : ∀ x y, p x → p y → f (x * y) ≤ f x * f y)
(hp_mul : ∀ x y, p x → p y → p (x * y)) (g : ι → M) (s : finset ι) (hs_nonempty : s.nonempty)
(hs : ∀ i ∈ s, p (g i)) :
f (∏ i in s, g i) ≤ ∏ i in s, f (g i) :=
begin
refine le_trans (multiset.le_prod_nonempty_of_submultiplicative_on_pred f p h_mul hp_mul _ _ _) _,
{ simp [hs_nonempty.ne_empty], },
{ exact multiset.forall_mem_map_iff.mpr hs, },
rw multiset.map_map,
refl,
end
/-- Let `{x | p x}` be an additive subsemigroup of an additive commutative monoid `M`. Let `f : M →
N` be a map subadditive on `{x | p x}`, i.e., `p x → p y → f (x + y) ≤ f x + f y`. Let `g i`, `i ∈
s`, be a nonempty finite family of elements of `M` such that `∀ i ∈ s, p (g i)`. Then
`f (∑ i in s, g i) ≤ ∏ i in s, f (g i)`. -/
add_decl_doc le_sum_nonempty_of_subadditive_on_pred
/-- If `f : M → N` is a submultiplicative function, `f (x * y) ≤ f x * f y` and `g i`, `i ∈ s`, is a
nonempty finite family of elements of `M`, then `f (∏ i in s, g i) ≤ ∏ i in s, f (g i)`. -/
@[to_additive le_sum_nonempty_of_subadditive]
lemma le_prod_nonempty_of_submultiplicative
(f : M → N) (h_mul : ∀ x y, f (x * y) ≤ f x * f y) {s : finset ι} (hs : s.nonempty) (g : ι → M) :
f (∏ i in s, g i) ≤ ∏ i in s, f (g i) :=
le_prod_nonempty_of_submultiplicative_on_pred f (λ i, true) (λ x y _ _, h_mul x y)
(λ _ _ _ _, trivial) g s hs (λ _ _, trivial)
/-- If `f : M → N` is a subadditive function, `f (x + y) ≤ f x + f y` and `g i`, `i ∈ s`, is a
nonempty finite family of elements of `M`, then `f (∑ i in s, g i) ≤ ∑ i in s, f (g i)`. -/
add_decl_doc le_sum_nonempty_of_subadditive
/-- Let `{x | p x}` be a subsemigroup of a commutative monoid `M`. Let `f : M → N` be a map
such that `f 1 = 1` and `f` is submultiplicative on `{x | p x}`, i.e.,
`p x → p y → f (x * y) ≤ f x * f y`. Let `g i`, `i ∈ s`, be a finite family of elements of `M` such
that `∀ i ∈ s, p (g i)`. Then `f (∏ i in s, g i) ≤ ∏ i in s, f (g i)`. -/
@[to_additive le_sum_of_subadditive_on_pred]
lemma le_prod_of_submultiplicative_on_pred (f : M → N) (p : M → Prop) (h_one : f 1 = 1)
(h_mul : ∀ x y, p x → p y → f (x * y) ≤ f x * f y)
(hp_mul : ∀ x y, p x → p y → p (x * y)) (g : ι → M) {s : finset ι} (hs : ∀ i ∈ s, p (g i)) :
f (∏ i in s, g i) ≤ ∏ i in s, f (g i) :=
begin
rcases eq_empty_or_nonempty s with rfl|hs_nonempty,
{ simp [h_one] },
{ exact le_prod_nonempty_of_submultiplicative_on_pred f p h_mul hp_mul g s hs_nonempty hs, },
end
/-- Let `{x | p x}` be a subsemigroup of a commutative monoid `M`. Let `f : M → N` be a map
such that `f 1 = 1` and `f` is submultiplicative on `{x | p x}`, i.e.,
`p x → p y → f (x * y) ≤ f x * f y`. Let `g i`, `i ∈ s`, be a finite family of elements of `M` such
that `∀ i ∈ s, p (g i)`. Then `f (∏ x in s, g x) ≤ ∏ x in s, f (g x)`. -/
add_decl_doc le_sum_of_subadditive_on_pred
/-- If `f : M → N` is a submultiplicative function, `f (x * y) ≤ f x * f y`, `f 1 = 1`, and `g i`,
`i ∈ s`, is a finite family of elements of `M`, then `f (∏ i in s, g i) ≤ ∏ i in s, f (g i)`. -/
@[to_additive le_sum_of_subadditive]
lemma le_prod_of_submultiplicative (f : M → N) (h_one : f 1 = 1)
(h_mul : ∀ x y, f (x * y) ≤ f x * f y) (s : finset ι) (g : ι → M) :
f (∏ i in s, g i) ≤ ∏ i in s, f (g i) :=
begin
refine le_trans (multiset.le_prod_of_submultiplicative f h_one h_mul _) _,
rw multiset.map_map,
refl,
end
/-- If `f : M → N` is a submultiplicative function, `f (x * y) ≤ f x * f y`, `f 1 = 1`, and `g i`,
`i ∈ s`, is a finite family of elements of `M`, then `f (∏ i in s, g i) ≤ ∏ i in s, f (g i)`. -/
add_decl_doc le_sum_of_subadditive
variables {f g : ι → N} {s t : finset ι}
/-- In an ordered commutative monoid, if each factor `f i` of one finite product is less than or
equal to the corresponding factor `g i` of another finite product, then
`∏ i in s, f i ≤ ∏ i in s, g i`. -/
@[to_additive sum_le_sum]
lemma prod_le_prod'' (h : ∀ i ∈ s, f i ≤ g i) : ∏ i in s, f i ≤ ∏ i in s, g i :=
begin
classical,
induction s using finset.induction_on with i s hi ihs h,
{ refl },
{ simp only [prod_insert hi],
exact mul_le_mul' (h _ (mem_insert_self _ _)) (ihs $ λ j hj, h j (mem_insert_of_mem hj)) }
end
/-- In an ordered additive commutative monoid, if each summand `f i` of one finite sum is less than
or equal to the corresponding summand `g i` of another finite sum, then
`∑ i in s, f i ≤ ∑ i in s, g i`. -/
add_decl_doc sum_le_sum
@[to_additive sum_nonneg] lemma one_le_prod' (h : ∀i ∈ s, 1 ≤ f i) : 1 ≤ (∏ i in s, f i) :=
le_trans (by rw prod_const_one) (prod_le_prod'' h)
@[to_additive sum_nonpos] lemma prod_le_one' (h : ∀i ∈ s, f i ≤ 1) : (∏ i in s, f i) ≤ 1 :=
(prod_le_prod'' h).trans_eq (by rw prod_const_one)
@[to_additive sum_le_sum_of_subset_of_nonneg]
lemma prod_le_prod_of_subset_of_one_le' (h : s ⊆ t) (hf : ∀ i ∈ t, i ∉ s → 1 ≤ f i) :
∏ i in s, f i ≤ ∏ i in t, f i :=
by classical;
calc (∏ i in s, f i) ≤ (∏ i in t \ s, f i) * (∏ i in s, f i) :
le_mul_of_one_le_left' $ one_le_prod' $ by simpa only [mem_sdiff, and_imp]
... = ∏ i in t \ s ∪ s, f i : (prod_union sdiff_disjoint).symm
... = ∏ i in t, f i : by rw [sdiff_union_of_subset h]
@[to_additive sum_mono_set_of_nonneg]
lemma prod_mono_set_of_one_le' (hf : ∀ x, 1 ≤ f x) : monotone (λ s, ∏ x in s, f x) :=
λ s t hst, prod_le_prod_of_subset_of_one_le' hst $ λ x _ _, hf x
@[to_additive sum_le_univ_sum_of_nonneg]
lemma prod_le_univ_prod_of_one_le' [fintype ι] {s : finset ι} (w : ∀ x, 1 ≤ f x) :
∏ x in s, f x ≤ ∏ x, f x :=
prod_le_prod_of_subset_of_one_le' (subset_univ s) (λ a _ _, w a)
@[to_additive sum_eq_zero_iff_of_nonneg]
lemma prod_eq_one_iff_of_one_le' : (∀ i ∈ s, 1 ≤ f i) → (∏ i in s, f i = 1 ↔ ∀ i ∈ s, f i = 1) :=
begin
classical,
apply finset.induction_on s,
exact λ _, ⟨λ _ _, false.elim, λ _, rfl⟩,
assume a s ha ih H,
have : ∀ i ∈ s, 1 ≤ f i, from λ _, H _ ∘ mem_insert_of_mem,
rw [prod_insert ha, mul_eq_one_iff' (H _ $ mem_insert_self _ _) (one_le_prod' this),
forall_mem_insert, ih this]
end
@[to_additive sum_eq_zero_iff_of_nonneg]
lemma prod_eq_one_iff_of_le_one' : (∀ i ∈ s, f i ≤ 1) → (∏ i in s, f i = 1 ↔ ∀ i ∈ s, f i = 1) :=
@prod_eq_one_iff_of_one_le' _ (order_dual N) _ _ _
@[to_additive single_le_sum]
lemma single_le_prod' (hf : ∀ i ∈ s, 1 ≤ f i) {a} (h : a ∈ s) : f a ≤ (∏ x in s, f x) :=
calc f a = ∏ i in {a}, f i : prod_singleton.symm
... ≤ ∏ i in s, f i :
prod_le_prod_of_subset_of_one_le' (singleton_subset_iff.2 h) $ λ i hi _, hf i hi
variables {ι' : Type*} [decidable_eq ι']
@[to_additive sum_fiberwise_le_sum_of_sum_fiber_nonneg]
lemma prod_fiberwise_le_prod_of_one_le_prod_fiber' {t : finset ι'}
{g : ι → ι'} {f : ι → N} (h : ∀ y ∉ t, (1 : N) ≤ ∏ x in s.filter (λ x, g x = y), f x) :
∏ y in t, ∏ x in s.filter (λ x, g x = y), f x ≤ ∏ x in s, f x :=
calc (∏ y in t, ∏ x in s.filter (λ x, g x = y), f x) ≤
(∏ y in t ∪ s.image g, ∏ x in s.filter (λ x, g x = y), f x) :
prod_le_prod_of_subset_of_one_le' (subset_union_left _ _) $ λ y hyts, h y
... = ∏ x in s, f x :
prod_fiberwise_of_maps_to (λ x hx, mem_union.2 $ or.inr $ mem_image_of_mem _ hx) _
@[to_additive sum_le_sum_fiberwise_of_sum_fiber_nonpos]
lemma prod_le_prod_fiberwise_of_prod_fiber_le_one' {t : finset ι'}
{g : ι → ι'} {f : ι → N} (h : ∀ y ∉ t, (∏ x in s.filter (λ x, g x = y), f x) ≤ 1) :
(∏ x in s, f x) ≤ ∏ y in t, ∏ x in s.filter (λ x, g x = y), f x :=
@prod_fiberwise_le_prod_of_one_le_prod_fiber' _ (order_dual N) _ _ _ _ _ _ _ h
end
lemma abs_sum_le_sum_abs {G : Type*} [linear_ordered_add_comm_group G] (f : ι → G) (s : finset ι) :
abs (∑ i in s, f i) ≤ ∑ i in s, abs (f i) :=
le_sum_of_subadditive _ abs_zero abs_add s f
lemma abs_prod {R : Type*} [linear_ordered_comm_ring R] {f : ι → R} {s : finset ι} :
abs (∏ x in s, f x) = ∏ x in s, abs (f x) :=
(abs_hom.to_monoid_hom : R →* R).map_prod _ _
section pigeonhole
variable [decidable_eq β]
theorem card_le_mul_card_image_of_maps_to {f : α → β} {s : finset α} {t : finset β}
(Hf : ∀ a ∈ s, f a ∈ t) (n : ℕ) (hn : ∀ a ∈ t, (s.filter (λ x, f x = a)).card ≤ n) :
s.card ≤ n * t.card :=
calc s.card = (∑ a in t, (s.filter (λ x, f x = a)).card) : card_eq_sum_card_fiberwise Hf
... ≤ (∑ _ in t, n) : sum_le_sum hn
... = _ : by simp [mul_comm]
theorem card_le_mul_card_image {f : α → β} (s : finset α)
(n : ℕ) (hn : ∀ a ∈ s.image f, (s.filter (λ x, f x = a)).card ≤ n) :
s.card ≤ n * (s.image f).card :=
card_le_mul_card_image_of_maps_to (λ x, mem_image_of_mem _) n hn
theorem mul_card_image_le_card_of_maps_to {f : α → β} {s : finset α} {t : finset β}
(Hf : ∀ a ∈ s, f a ∈ t) (n : ℕ) (hn : ∀ a ∈ t, n ≤ (s.filter (λ x, f x = a)).card) :
n * t.card ≤ s.card :=
calc n * t.card = (∑ _ in t, n) : by simp [mul_comm]
... ≤ (∑ a in t, (s.filter (λ x, f x = a)).card) : sum_le_sum hn
... = s.card : by rw ← card_eq_sum_card_fiberwise Hf
theorem mul_card_image_le_card {f : α → β} (s : finset α)
(n : ℕ) (hn : ∀ a ∈ s.image f, n ≤ (s.filter (λ x, f x = a)).card) :
n * (s.image f).card ≤ s.card :=
mul_card_image_le_card_of_maps_to (λ x, mem_image_of_mem _) n hn
@[to_additive]
lemma prod_le_of_forall_le {α β : Type*} [ordered_comm_monoid β] (s : finset α) (f : α → β)
(n : β) (h : ∀ (x ∈ s), f x ≤ n) :
s.prod f ≤ n ^ s.card :=
begin
refine (multiset.prod_le_of_forall_le (s.val.map f) n _).trans _,
{ simpa using h },
{ simpa }
end
end pigeonhole
section canonically_ordered_monoid
variables [canonically_ordered_monoid M] {f : ι → M} {s t : finset ι}
@[simp, to_additive sum_eq_zero_iff]
lemma prod_eq_one_iff' : ∏ x in s, f x = 1 ↔ ∀ x ∈ s, f x = 1 :=
prod_eq_one_iff_of_one_le' $ λ x hx, one_le (f x)
@[to_additive sum_le_sum_of_subset]
lemma prod_le_prod_of_subset' (h : s ⊆ t) : ∏ x in s, f x ≤ ∏ x in t, f x :=
prod_le_prod_of_subset_of_one_le' h $ assume x h₁ h₂, one_le _
@[to_additive sum_mono_set]
lemma prod_mono_set' (f : ι → M) : monotone (λ s, ∏ x in s, f x) :=
λ s₁ s₂ hs, prod_le_prod_of_subset' hs
@[to_additive sum_le_sum_of_ne_zero]
lemma prod_le_prod_of_ne_one' (h : ∀ x ∈ s, f x ≠ 1 → x ∈ t) :
∏ x in s, f x ≤ ∏ x in t, f x :=
by classical;
calc ∏ x in s, f x = (∏ x in s.filter (λ x, f x = 1), f x) * ∏ x in s.filter (λ x, f x ≠ 1), f x :
by rw [← prod_union, filter_union_filter_neg_eq];
exact disjoint_filter.2 (assume _ _ h n_h, n_h h)
... ≤ (∏ x in t, f x) : mul_le_of_le_one_of_le
(prod_le_one' $ by simp only [mem_filter, and_imp]; exact λ _ _, le_of_eq)
(prod_le_prod_of_subset' $ by simpa only [subset_iff, mem_filter, and_imp])
end canonically_ordered_monoid
section ordered_cancel_comm_monoid
variables [ordered_cancel_comm_monoid M] {f g : ι → M} {s t : finset ι}
@[to_additive sum_lt_sum]
theorem prod_lt_prod' (Hle : ∀ i ∈ s, f i ≤ g i) (Hlt : ∃ i ∈ s, f i < g i) :
∏ i in s, f i < ∏ i in s, g i :=
begin
classical,
rcases Hlt with ⟨i, hi, hlt⟩,
rw [← insert_erase hi, prod_insert (not_mem_erase _ _), prod_insert (not_mem_erase _ _)],
exact mul_lt_mul_of_lt_of_le hlt (prod_le_prod'' $ λ j hj, Hle j $ mem_of_mem_erase hj)
end
@[to_additive sum_lt_sum_of_nonempty]
lemma prod_lt_prod_of_nonempty' (hs : s.nonempty) (Hlt : ∀ i ∈ s, f i < g i) :
∏ i in s, f i < ∏ i in s, g i :=
begin
apply prod_lt_prod',
{ intros i hi, apply le_of_lt (Hlt i hi) },
cases hs with i hi,
exact ⟨i, hi, Hlt i hi⟩,
end
@[to_additive sum_lt_sum_of_subset]
lemma prod_lt_prod_of_subset' (h : s ⊆ t) {i : ι} (ht : i ∈ t) (hs : i ∉ s) (hlt : 1 < f i)
(hle : ∀ j ∈ t, j ∉ s → 1 ≤ f j) :
∏ j in s, f j < ∏ j in t, f j :=
by classical;
calc ∏ j in s, f j < ∏ j in insert i s, f j :
begin
rw prod_insert hs,
exact lt_mul_of_one_lt_left' (∏ j in s, f j) hlt,
end
... ≤ ∏ j in t, f j :
begin
apply prod_le_prod_of_subset_of_one_le',
{ simp [finset.insert_subset, h, ht] },
{ assume x hx h'x,
simp only [mem_insert, not_or_distrib] at h'x,
exact hle x hx h'x.2 }
end
@[to_additive single_lt_sum]
lemma single_lt_prod' {i j : ι} (hij : j ≠ i) (hi : i ∈ s) (hj : j ∈ s) (hlt : 1 < f j)
(hle : ∀ k ∈ s, k ≠ i → 1 ≤ f k) :
f i < ∏ k in s, f k :=
calc f i = ∏ k in {i}, f k : prod_singleton.symm
... < ∏ k in s, f k :
prod_lt_prod_of_subset' (singleton_subset_iff.2 hi) hj (mt mem_singleton.1 hij) hlt $
λ k hks hki, hle k hks (mt mem_singleton.2 hki)
end ordered_cancel_comm_monoid
section linear_ordered_cancel_comm_monoid
variables [linear_ordered_cancel_comm_monoid M] {f g : ι → M} {s t : finset ι}
@[to_additive exists_lt_of_sum_lt]
theorem exists_lt_of_prod_lt' (Hlt : ∏ i in s, f i < ∏ i in s, g i) :
∃ i ∈ s, f i < g i :=
begin
contrapose! Hlt with Hle,
exact prod_le_prod'' Hle
end
@[to_additive exists_le_of_sum_le]
theorem exists_le_of_prod_le' (hs : s.nonempty) (Hle : ∏ i in s, f i ≤ ∏ i in s, g i) :
∃ i ∈ s, f i ≤ g i :=
begin
contrapose! Hle with Hlt,
exact prod_lt_prod_of_nonempty' hs Hlt
end
@[to_additive exists_pos_of_sum_zero_of_exists_nonzero]
lemma exists_one_lt_of_prod_one_of_exists_ne_one' (f : ι → M)
(h₁ : ∏ i in s, f i = 1) (h₂ : ∃ i ∈ s, f i ≠ 1) :
∃ i ∈ s, 1 < f i :=
begin
contrapose! h₁,
obtain ⟨i, m, i_ne⟩ : ∃ i ∈ s, f i ≠ 1 := h₂,
apply ne_of_lt,
calc ∏ j in s, f j < ∏ j in s, 1 : prod_lt_prod' h₁ ⟨i, m, (h₁ i m).lt_of_ne i_ne⟩
... = 1 : prod_const_one
end
end linear_ordered_cancel_comm_monoid
section ordered_comm_semiring
variables [ordered_comm_semiring R] {f g : ι → R} {s t : finset ι}
open_locale classical
/- this is also true for a ordered commutative multiplicative monoid -/
lemma prod_nonneg (h0 : ∀ i ∈ s, 0 ≤ f i) : 0 ≤ ∏ i in s, f i :=
prod_induction f (λ i, 0 ≤ i) (λ _ _ ha hb, mul_nonneg ha hb) zero_le_one h0
/- this is also true for a ordered commutative multiplicative monoid -/
lemma prod_pos [nontrivial R] (h0 : ∀ i ∈ s, 0 < f i) :
0 < ∏ i in s, f i :=
prod_induction f (λ x, 0 < x) (λ _ _ ha hb, mul_pos ha hb) zero_lt_one h0
/-- If all `f i`, `i ∈ s`, are nonnegative and each `f i` is less than or equal to `g i`, then the
product of `f i` is less than or equal to the product of `g i`. See also `finset.prod_le_prod''` for
the case of an ordered commutative multiplicative monoid. -/
lemma prod_le_prod (h0 : ∀ i ∈ s, 0 ≤ f i) (h1 : ∀ i ∈ s, f i ≤ g i) :
∏ i in s, f i ≤ ∏ i in s, g i :=
begin
induction s using finset.induction with a s has ih h,
{ simp },
{ simp only [prod_insert has], apply mul_le_mul,
{ exact h1 a (mem_insert_self a s) },
{ apply ih (λ x H, h0 _ _) (λ x H, h1 _ _); exact (mem_insert_of_mem H) },
{ apply prod_nonneg (λ x H, h0 x (mem_insert_of_mem H)) },
{ apply le_trans (h0 a (mem_insert_self a s)) (h1 a (mem_insert_self a s)) } }
end
/-- If each `f i`, `i ∈ s` belongs to `[0, 1]`, then their product is less than or equal to one.
See also `finset.prod_le_one'` for the case of an ordered commutative multiplicative monoid. -/
lemma prod_le_one (h0 : ∀ i ∈ s, 0 ≤ f i) (h1 : ∀ i ∈ s, f i ≤ 1) :
∏ i in s, f i ≤ 1 :=
begin
convert ← prod_le_prod h0 h1,
exact finset.prod_const_one
end
/-- If `g, h ≤ f` and `g i + h i ≤ f i`, then the product of `f` over `s` is at least the
sum of the products of `g` and `h`. This is the version for `ordered_comm_semiring`. -/
lemma prod_add_prod_le {i : ι} {f g h : ι → R}
(hi : i ∈ s) (h2i : g i + h i ≤ f i) (hgf : ∀ j ∈ s, j ≠ i → g j ≤ f j)
(hhf : ∀ j ∈ s, j ≠ i → h j ≤ f j) (hg : ∀ i ∈ s, 0 ≤ g i) (hh : ∀ i ∈ s, 0 ≤ h i) :
∏ i in s, g i + ∏ i in s, h i ≤ ∏ i in s, f i :=
begin
simp_rw [prod_eq_mul_prod_diff_singleton hi],
refine le_trans _ (mul_le_mul_of_nonneg_right h2i _),
{ rw [right_distrib],
apply add_le_add; apply mul_le_mul_of_nonneg_left; try { apply_assumption; assumption };
apply prod_le_prod; simp * { contextual := tt } },
{ apply prod_nonneg, simp only [and_imp, mem_sdiff, mem_singleton],
intros j h1j h2j, exact le_trans (hg j h1j) (hgf j h1j h2j) }
end
end ordered_comm_semiring
section canonically_ordered_comm_semiring
variables [canonically_ordered_comm_semiring R] {f g h : ι → R} {s : finset ι} {i : ι}
lemma prod_le_prod' (h : ∀ i ∈ s, f i ≤ g i) :
∏ i in s, f i ≤ ∏ i in s, g i :=
begin
classical,
induction s using finset.induction with a s has ih h,
{ simp },
{ rw [finset.prod_insert has, finset.prod_insert has],
apply mul_le_mul',
{ exact h _ (finset.mem_insert_self a s) },
{ exact ih (λ i hi, h _ (finset.mem_insert_of_mem hi)) } }
end
/-- If `g, h ≤ f` and `g i + h i ≤ f i`, then the product of `f` over `s` is at least the
sum of the products of `g` and `h`. This is the version for `canonically_ordered_comm_semiring`.
-/
lemma prod_add_prod_le' (hi : i ∈ s) (h2i : g i + h i ≤ f i)
(hgf : ∀ j ∈ s, j ≠ i → g j ≤ f j) (hhf : ∀ j ∈ s, j ≠ i → h j ≤ f j) :
∏ i in s, g i + ∏ i in s, h i ≤ ∏ i in s, f i :=
begin
classical, simp_rw [prod_eq_mul_prod_diff_singleton hi],
refine le_trans _ (mul_le_mul_right' h2i _),
rw [right_distrib],
apply add_le_add; apply mul_le_mul_left'; apply prod_le_prod';
simp only [and_imp, mem_sdiff, mem_singleton]; intros; apply_assumption; assumption
end
end canonically_ordered_comm_semiring
end finset
namespace fintype
variables [fintype ι]
@[mono, to_additive sum_mono]
lemma prod_mono' [ordered_comm_monoid M] : monotone (λ f : ι → M, ∏ i, f i) :=
λ f g hfg, finset.prod_le_prod'' $ λ x _, hfg x
@[to_additive sum_strict_mono]
lemma prod_strict_mono' [ordered_cancel_comm_monoid M] : strict_mono (λ f : ι → M, ∏ x, f x) :=
λ f g hfg, let ⟨hle, i, hlt⟩ := pi.lt_def.mp hfg in
finset.prod_lt_prod' (λ i _, hle i) ⟨i, finset.mem_univ i, hlt⟩
end fintype
namespace with_top
open finset
/-- A product of finite numbers is still finite -/
lemma prod_lt_top [canonically_ordered_comm_semiring R] [nontrivial R] [decidable_eq R]
{s : finset ι} {f : ι → with_top R} (h : ∀ i ∈ s, f i < ⊤) :
∏ i in s, f i < ⊤ :=
prod_induction f (λ a, a < ⊤) (λ a b, mul_lt_top) (coe_lt_top 1) h
/-- A sum of finite numbers is still finite -/
lemma sum_lt_top [ordered_add_comm_monoid M] {s : finset ι} {f : ι → with_top M} :
(∀ i ∈ s, f i < ⊤) → (∑ i in s, f i) < ⊤ :=
sum_induction f (λ a, a < ⊤) (by { simp_rw add_lt_top, tauto }) zero_lt_top
/-- A sum of numbers is infinite iff one of them is infinite -/
lemma sum_eq_top_iff [ordered_add_comm_monoid M] {s : finset ι} {f : ι → with_top M} :
∑ i in s, f i = ⊤ ↔ ∃ i ∈ s, f i = ⊤ :=
begin
classical,
split,
{ contrapose!,
exact λ h, (sum_lt_top $ λ i hi, lt_top_iff_ne_top.2 (h i hi)).ne },
{ rintro ⟨i, his, hi⟩,
rw [sum_eq_add_sum_diff_singleton his, hi, top_add] }
end
/-- A sum of finite numbers is still finite -/
lemma sum_lt_top_iff [ordered_add_comm_monoid M] {s : finset ι} {f : ι → with_top M} :
∑ i in s, f i < ⊤ ↔ ∀ i ∈ s, f i < ⊤ :=
by simp only [lt_top_iff_ne_top, ne.def, sum_eq_top_iff, not_exists]
end with_top
section absolute_value
variables {S : Type*}
lemma absolute_value.sum_le [semiring R] [ordered_semiring S]
(abv : absolute_value R S) (s : finset ι) (f : ι → R) :
abv (∑ i in s, f i) ≤ ∑ i in s, abv (f i) :=
begin
letI := classical.dec_eq ι,
refine finset.induction_on s _ (λ i s hi ih, _),
{ simp },
{ simp only [finset.sum_insert hi],
exact (abv.add_le _ _).trans (add_le_add (le_refl _) ih) },
end
lemma absolute_value.map_prod [comm_semiring R] [nontrivial R] [linear_ordered_comm_ring S]
(abv : absolute_value R S) (f : ι → R) (s : finset ι) :
abv (∏ i in s, f i) = ∏ i in s, abv (f i) :=
abv.to_monoid_hom.map_prod f s
end absolute_value
|
dcd17235b7c22b60d1f47f9aed178d92959dd2e9 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/number_theory/prime_counting.lean | b531b97019305e1a369413ce794f3111aeea6ee4 | [
"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 | 3,378 | lean | /-
Copyright (c) 2021 Bolton Bailey. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bolton Bailey
-/
import data.nat.prime_fin
import data.nat.totient
import data.finset.locally_finite
import data.nat.count
import data.nat.nth
/-!
# The Prime Counting Function
In this file we define the prime counting function: the function on natural numbers that returns
the number of primes less than or equal to its input.
## Main Results
The main definitions for this file are
- `nat.prime_counting`: The prime counting function π
- `nat.prime_counting'`: π(n - 1)
We then prove that these are monotone in `nat.monotone_prime_counting` and
`nat.monotone_prime_counting'`. The last main theorem `nat.prime_counting'_add_le` is an upper
bound on `π'` which arises by observing that all numbers greater than `k` and not coprime to `k`
are not prime, and so only at most `φ(k)/k` fraction of the numbers from `k` to `n` are prime.
## Notation
We use the standard notation `π` to represent the prime counting function (and `π'` to represent
the reindexed version).
-/
namespace nat
open finset
/--
A variant of the traditional prime counting function which gives the number of primes
*strictly* less than the input. More convenient for avoiding off-by-one errors.
-/
def prime_counting' : ℕ → ℕ := nat.count prime
/-- The prime counting function: Returns the number of primes less than or equal to the input. -/
def prime_counting (n : ℕ) : ℕ := prime_counting' (n + 1)
localized "notation (name := prime_counting) `π` := nat.prime_counting" in nat
localized "notation (name := prime_counting') `π'` := nat.prime_counting'" in nat
lemma monotone_prime_counting' : monotone prime_counting' := count_monotone prime
lemma monotone_prime_counting : monotone prime_counting :=
monotone_prime_counting'.comp (monotone_id.add_const _)
@[simp] lemma prime_counting'_nth_eq (n : ℕ) : π' (nth prime n) = n :=
count_nth_of_infinite _ infinite_set_of_prime _
@[simp] lemma prime_nth_prime (n : ℕ) : prime (nth prime n) :=
nth_mem_of_infinite _ infinite_set_of_prime _
/-- A linear upper bound on the size of the `prime_counting'` function -/
lemma prime_counting'_add_le {a k : ℕ} (h0 : 0 < a) (h1 : a < k) (n : ℕ) :
π' (k + n) ≤ π' k + nat.totient a * (n / a + 1) :=
calc π' (k + n)
≤ ((range k).filter (prime)).card + ((Ico k (k + n)).filter (prime)).card :
begin
rw [prime_counting', count_eq_card_filter_range, range_eq_Ico,
←Ico_union_Ico_eq_Ico (zero_le k) (le_self_add), filter_union],
apply card_union_le,
end
... ≤ π' k + ((Ico k (k + n)).filter (prime)).card :
by rw [prime_counting', count_eq_card_filter_range]
... ≤ π' k + ((Ico k (k + n)).filter (coprime a)).card :
begin
refine add_le_add_left (card_le_of_subset _) k.prime_counting',
simp only [subset_iff, and_imp, mem_filter, mem_Ico],
intros p succ_k_le_p p_lt_n p_prime,
split,
{ exact ⟨succ_k_le_p, p_lt_n⟩, },
{ rw coprime_comm,
exact coprime_of_lt_prime h0 (gt_of_ge_of_gt succ_k_le_p h1) p_prime, },
end
... ≤ π' k + totient a * (n / a + 1) :
begin
rw [add_le_add_iff_left],
exact Ico_filter_coprime_le k n h0,
end
end nat
|
f87649278fb51746e4f6285b9d1e3a4b9f544a60 | 4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d | /src/Lean/Server/Utils.lean | f8fcc7e5bb32b7a7eb0d3429d26dd02b323d8a55 | [
"Apache-2.0"
] | permissive | subfish-zhou/leanprover-zh_CN.github.io | 30b9fba9bd790720bd95764e61ae796697d2f603 | 8b2985d4a3d458ceda9361ac454c28168d920d3f | refs/heads/master | 1,689,709,967,820 | 1,632,503,056,000 | 1,632,503,056,000 | 409,962,097 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,348 | lean | /-
Copyright (c) 2020 Wojciech Nawrocki. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Wojciech Nawrocki, Marc Huisinga
-/
import Lean.Data.Position
import Lean.Data.Lsp
import Lean.Server.InfoUtils
import Init.System.FilePath
namespace IO
def throwServerError (err : String) : IO α :=
throw (userError err)
namespace FS.Stream
/-- Chains two streams by creating a new stream s.t. writing to it
just writes to `a` but reading from it also duplicates the read output
into `b`, c.f. `a | tee b` on Unix.
NB: if `a` is written to but this stream is never read from,
the output will *not* be duplicated. Use this if you only care
about the data that was actually read. -/
def chainRight (a : Stream) (b : Stream) (flushEagerly : Bool := false) : Stream :=
{ a with
flush := a.flush *> b.flush
read := fun sz => do
let bs ← a.read sz
b.write bs
if flushEagerly then b.flush
pure bs
getLine := do
let ln ← a.getLine
b.putStr ln
if flushEagerly then b.flush
pure ln }
/-- Like `tee a | b` on Unix. See `chainOut`. -/
def chainLeft (a : Stream) (b : Stream) (flushEagerly : Bool := false) : Stream :=
{ b with
flush := a.flush *> b.flush
write := fun bs => do
a.write bs
if flushEagerly then a.flush
b.write bs
putStr := fun s => do
a.putStr s
if flushEagerly then a.flush
b.putStr s }
/-- Prefixes all written outputs with `pre`. -/
def withPrefix (a : Stream) (pre : String) : Stream :=
{ a with
write := fun bs => do
a.putStr pre
a.write bs
putStr := fun s =>
a.putStr (pre ++ s) }
end FS.Stream
end IO
namespace Lean.Server
structure DocumentMeta where
uri : Lsp.DocumentUri
version : Nat
text : FileMap
deriving Inhabited
def replaceLspRange (text : FileMap) (r : Lsp.Range) (newText : String) : FileMap :=
let start := text.lspPosToUtf8Pos r.start
let «end» := text.lspPosToUtf8Pos r.«end»
let pre := text.source.extract 0 start
let post := text.source.extract «end» text.source.bsize
(pre ++ newText ++ post).toFileMap
open IO
/-- Duplicates an I/O stream to a log file `fName` in LEAN_SERVER_LOG_DIR
if that envvar is set. -/
def maybeTee (fName : String) (isOut : Bool) (h : FS.Stream) : IO FS.Stream := do
match (← IO.getEnv "LEAN_SERVER_LOG_DIR") with
| none => pure h
| some logDir =>
IO.FS.createDirAll logDir
let hTee ← FS.Handle.mk (System.mkFilePath [logDir, fName]) FS.Mode.write true
let hTee := FS.Stream.ofHandle hTee
pure $ if isOut then
hTee.chainLeft h true
else
h.chainRight hTee true
/-- Transform the given path to a file:// URI. -/
def toFileUri (fname : System.FilePath) : Lsp.DocumentUri :=
let fname := fname.normalize.toString
let fname := if System.Platform.isWindows then
fname.map fun c => if c == '\\' then '/' else c
else
fname
-- TODO(WN): URL-encode special characters
-- Three slashes denote localhost.
"file:///" ++ fname.dropWhile (· == '/')
open Lsp
/-- Returns the document contents with all changes applied, together with the position of the change
which lands earliest in the file. Panics if there are no changes. -/
def foldDocumentChanges (changes : @& Array Lsp.TextDocumentContentChangeEvent) (oldText : FileMap)
: FileMap × String.Pos :=
if changes.isEmpty then panic! "Lean.Server.foldDocumentChanges: empty change array" else
let accumulateChanges : FileMap × String.Pos → TextDocumentContentChangeEvent → FileMap × String.Pos :=
fun ⟨newDocText, minStartOff⟩ change =>
match change with
| TextDocumentContentChangeEvent.rangeChange (range : Range) (newText : String) =>
let startOff := oldText.lspPosToUtf8Pos range.start
let newDocText := replaceLspRange newDocText range newText
let minStartOff := minStartOff.min startOff
⟨newDocText, minStartOff⟩
| TextDocumentContentChangeEvent.fullChange (newText : String) =>
⟨newText.toFileMap, 0⟩
-- NOTE: We assume Lean files are below 16 EiB.
changes.foldl accumulateChanges (oldText, 0xffffffff)
def publishDiagnostics (m : DocumentMeta) (diagnostics : Array Lsp.Diagnostic) (hOut : FS.Stream) : IO Unit :=
hOut.writeLspNotification {
method := "textDocument/publishDiagnostics"
param := {
uri := m.uri
version? := m.version
diagnostics := diagnostics
: PublishDiagnosticsParams
}
}
def publishProgress (m : DocumentMeta) (processing : Array LeanFileProgressProcessingInfo) (hOut : FS.Stream) : IO Unit :=
hOut.writeLspNotification {
method := "$/lean/fileProgress"
param := {
textDocument := { uri := m.uri, version? := m.version }
processing
: LeanFileProgressParams
}
}
def publishProgressAtPos (m : DocumentMeta) (pos : String.Pos) (hOut : FS.Stream) : IO Unit :=
publishProgress m #[{ range := ⟨m.text.utf8PosToLspPos pos, m.text.utf8PosToLspPos m.text.source.bsize⟩ }] hOut
def publishProgressDone (m : DocumentMeta) (hOut : FS.Stream) : IO Unit :=
publishProgress m #[] hOut
end Lean.Server
def String.Range.toLspRange (text : Lean.FileMap) (r : String.Range) : Lean.Lsp.Range :=
⟨text.utf8PosToLspPos r.start, text.utf8PosToLspPos r.stop⟩
|
d716fe0a1f62f48095ea108ccdf3347c566dae61 | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/category_theory/limits/shapes/regular_mono.lean | 7c8f7e3d4e8f56404ee8f4162938bef1402040ff | [
"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 | 12,913 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Bhavik Mehta
-/
import category_theory.limits.shapes.kernels
import category_theory.limits.shapes.strong_epi
import category_theory.limits.shapes.pullbacks
/-!
# Definitions and basic properties of regular and normal monomorphisms and epimorphisms.
A regular monomorphism is a morphism that is the equalizer of some parallel pair.
A normal monomorphism is a morphism that is the kernel of some other morphism.
We give the constructions
* `split_mono → regular_mono`
* `normal_mono → regular_mono`, and
* `regular_mono → mono`
as well as the dual constructions for regular and normal epimorphisms. Additionally, we give the
construction
* `regular_epi ⟶ strong_epi`.
-/
namespace category_theory
open category_theory.limits
universes v₁ u₁
variables {C : Type u₁} [category.{v₁} C]
variables {X Y : C}
/-- A regular monomorphism is a morphism which is the equalizer of some parallel pair. -/
class regular_mono (f : X ⟶ Y) :=
(Z : C)
(left right : Y ⟶ Z)
(w : f ≫ left = f ≫ right)
(is_limit : is_limit (fork.of_ι f w))
attribute [reassoc] regular_mono.w
/-- Every regular monomorphism is a monomorphism. -/
@[priority 100]
instance regular_mono.mono (f : X ⟶ Y) [regular_mono f] : mono f :=
mono_of_is_limit_parallel_pair regular_mono.is_limit
instance equalizer_regular (g h : X ⟶ Y) [has_limit (parallel_pair g h)] :
regular_mono (equalizer.ι g h) :=
{ Z := Y,
left := g,
right := h,
w := equalizer.condition g h,
is_limit := fork.is_limit.mk _ (λ s, limit.lift _ s) (by simp) (λ s m w, by { ext1, simp [←w] }) }
/-- Every split monomorphism is a regular monomorphism. -/
@[priority 100]
instance regular_mono.of_split_mono (f : X ⟶ Y) [split_mono f] : regular_mono f :=
{ Z := Y,
left := 𝟙 Y,
right := retraction f ≫ f,
w := by tidy,
is_limit := split_mono_equalizes f }
/-- If `f` is a regular mono, then any map `k : W ⟶ Y` equalizing `regular_mono.left` and
`regular_mono.right` induces a morphism `l : W ⟶ X` such that `l ≫ f = k`. -/
def regular_mono.lift' {W : C} (f : X ⟶ Y) [regular_mono f] (k : W ⟶ Y)
(h : k ≫ (regular_mono.left : Y ⟶ @regular_mono.Z _ _ _ _ f _) = k ≫ regular_mono.right) :
{l : W ⟶ X // l ≫ f = k} :=
fork.is_limit.lift' regular_mono.is_limit _ h
/--
The second leg of a pullback cone is a regular monomorphism if the right component is too.
See also `pullback.snd_of_mono` for the basic monomorphism version, and
`regular_of_is_pullback_fst_of_regular` for the flipped version.
-/
def regular_of_is_pullback_snd_of_regular {P Q R S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S}
[hr : regular_mono h] (comm : f ≫ h = g ≫ k) (t : is_limit (pullback_cone.mk _ _ comm)) :
regular_mono g :=
{ Z := hr.Z,
left := k ≫ hr.left,
right := k ≫ hr.right,
w := by rw [← reassoc_of comm, ← reassoc_of comm, hr.w],
is_limit :=
begin
apply fork.is_limit.mk' _ _,
intro s,
have l₁ : (fork.ι s ≫ k) ≫ regular_mono.left = (fork.ι s ≫ k) ≫ regular_mono.right,
rw [category.assoc, s.condition, category.assoc],
obtain ⟨l, hl⟩ := fork.is_limit.lift' hr.is_limit _ l₁,
obtain ⟨p, hp₁, hp₂⟩ := pullback_cone.is_limit.lift' t _ _ hl,
refine ⟨p, hp₂, _⟩,
intros m w,
have z : m ≫ g = p ≫ g := w.trans hp₂.symm,
apply t.hom_ext,
apply (pullback_cone.mk f g comm).equalizer_ext,
{ erw [← cancel_mono h, category.assoc, category.assoc, comm, reassoc_of z] },
{ exact z },
end }
/--
The first leg of a pullback cone is a regular monomorphism if the left component is too.
See also `pullback.fst_of_mono` for the basic monomorphism version, and
`regular_of_is_pullback_snd_of_regular` for the flipped version.
-/
def regular_of_is_pullback_fst_of_regular {P Q R S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S}
[hr : regular_mono k] (comm : f ≫ h = g ≫ k) (t : is_limit (pullback_cone.mk _ _ comm)) :
regular_mono f :=
regular_of_is_pullback_snd_of_regular comm.symm (pullback_cone.flip_is_limit t)
/-- A regular monomorphism is an isomorphism if it is an epimorphism. -/
def is_iso_of_regular_mono_of_epi (f : X ⟶ Y) [regular_mono f] [e : epi f] : is_iso f :=
@is_iso_limit_cone_parallel_pair_of_epi _ _ _ _ _ _ _ regular_mono.is_limit e
section
variables [has_zero_morphisms C]
/-- A normal monomorphism is a morphism which is the kernel of some morphism. -/
class normal_mono (f : X ⟶ Y) :=
(Z : C)
(g : Y ⟶ Z)
(w : f ≫ g = 0)
(is_limit : is_limit (kernel_fork.of_ι f w))
/-- Every normal monomorphism is a regular monomorphism. -/
@[priority 100]
instance normal_mono.regular_mono (f : X ⟶ Y) [I : normal_mono f] : regular_mono f :=
{ left := I.g,
right := 0,
w := (by simpa using I.w),
..I }
/-- If `f` is a normal mono, then any map `k : W ⟶ Y` such that `k ≫ normal_mono.g = 0` induces
a morphism `l : W ⟶ X` such that `l ≫ f = k`. -/
def normal_mono.lift' {W : C} (f : X ⟶ Y) [normal_mono f] (k : W ⟶ Y) (h : k ≫ normal_mono.g = 0) :
{l : W ⟶ X // l ≫ f = k} :=
kernel_fork.is_limit.lift' normal_mono.is_limit _ h
/--
The second leg of a pullback cone is a normal monomorphism if the right component is too.
See also `pullback.snd_of_mono` for the basic monomorphism version, and
`normal_of_is_pullback_fst_of_normal` for the flipped version.
-/
def normal_of_is_pullback_snd_of_normal {P Q R S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S}
[hn : normal_mono h] (comm : f ≫ h = g ≫ k) (t : is_limit (pullback_cone.mk _ _ comm)) :
normal_mono g :=
{ Z := hn.Z,
g := k ≫ hn.g,
w := by rw [← reassoc_of comm, hn.w, has_zero_morphisms.comp_zero],
is_limit :=
begin
letI gr := regular_of_is_pullback_snd_of_regular comm t,
have q := (has_zero_morphisms.comp_zero k hn.Z).symm,
convert gr.is_limit,
dunfold kernel_fork.of_ι fork.of_ι,
congr, exact q, exact q, exact q, apply proof_irrel_heq,
end }
/--
The first leg of a pullback cone is a normal monomorphism if the left component is too.
See also `pullback.fst_of_mono` for the basic monomorphism version, and
`normal_of_is_pullback_snd_of_normal` for the flipped version.
-/
def normal_of_is_pullback_fst_of_normal {P Q R S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S}
[hn : normal_mono k] (comm : f ≫ h = g ≫ k) (t : is_limit (pullback_cone.mk _ _ comm)) :
normal_mono f :=
normal_of_is_pullback_snd_of_normal comm.symm (pullback_cone.flip_is_limit t)
end
/-- A regular epimorphism is a morphism which is the coequalizer of some parallel pair. -/
class regular_epi (f : X ⟶ Y) :=
(W : C)
(left right : W ⟶ X)
(w : left ≫ f = right ≫ f)
(is_colimit : is_colimit (cofork.of_π f w))
attribute [reassoc] regular_epi.w
/-- Every regular epimorphism is an epimorphism. -/
@[priority 100]
instance regular_epi.epi (f : X ⟶ Y) [regular_epi f] : epi f :=
epi_of_is_colimit_parallel_pair regular_epi.is_colimit
instance coequalizer_regular (g h : X ⟶ Y) [has_colimit (parallel_pair g h)] :
regular_epi (coequalizer.π g h) :=
{ W := X,
left := g,
right := h,
w := coequalizer.condition g h,
is_colimit := cofork.is_colimit.mk _ (λ s, colimit.desc _ s) (by simp) (λ s m w, by { ext1, simp [←w] }) }
/-- Every split epimorphism is a regular epimorphism. -/
@[priority 100]
instance regular_epi.of_split_epi (f : X ⟶ Y) [split_epi f] : regular_epi f :=
{ W := X,
left := 𝟙 X,
right := f ≫ section_ f,
w := by tidy,
is_colimit := split_epi_coequalizes f }
/-- If `f` is a regular epi, then every morphism `k : X ⟶ W` coequalizing `regular_epi.left` and
`regular_epi.right` induces `l : Y ⟶ W` such that `f ≫ l = k`. -/
def regular_epi.desc' {W : C} (f : X ⟶ Y) [regular_epi f] (k : X ⟶ W)
(h : (regular_epi.left : regular_epi.W f ⟶ X) ≫ k = regular_epi.right ≫ k) :
{l : Y ⟶ W // f ≫ l = k} :=
cofork.is_colimit.desc' (regular_epi.is_colimit) _ h
/--
The second leg of a pushout cocone is a regular epimorphism if the right component is too.
See also `pushout.snd_of_epi` for the basic epimorphism version, and
`regular_of_is_pushout_fst_of_regular` for the flipped version.
-/
def regular_of_is_pushout_snd_of_regular {P Q R S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S}
[gr : regular_epi g] (comm : f ≫ h = g ≫ k) (t : is_colimit (pushout_cocone.mk _ _ comm)) :
regular_epi h :=
{ W := gr.W,
left := gr.left ≫ f,
right := gr.right ≫ f,
w := by rw [category.assoc, category.assoc, comm, reassoc_of gr.w],
is_colimit :=
begin
apply cofork.is_colimit.mk' _ _,
intro s,
have l₁ : gr.left ≫ f ≫ s.π = gr.right ≫ f ≫ s.π,
rw [← category.assoc, ← category.assoc, s.condition],
obtain ⟨l, hl⟩ := cofork.is_colimit.desc' gr.is_colimit (f ≫ cofork.π s) l₁,
obtain ⟨p, hp₁, hp₂⟩ := pushout_cocone.is_colimit.desc' t _ _ hl.symm,
refine ⟨p, hp₁, _⟩,
intros m w,
have z := w.trans hp₁.symm,
apply t.hom_ext,
apply (pushout_cocone.mk _ _ comm).coequalizer_ext,
{ exact z },
{ erw [← cancel_epi g, ← reassoc_of comm, ← reassoc_of comm, z], refl },
end }
/--
The first leg of a pushout cocone is a regular epimorphism if the left component is too.
See also `pushout.fst_of_epi` for the basic epimorphism version, and
`regular_of_is_pushout_snd_of_regular` for the flipped version.
-/
def regular_of_is_pushout_fst_of_regular {P Q R S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S}
[fr : regular_epi f] (comm : f ≫ h = g ≫ k) (t : is_colimit (pushout_cocone.mk _ _ comm)) :
regular_epi k :=
regular_of_is_pushout_snd_of_regular comm.symm (pushout_cocone.flip_is_colimit t)
/-- A regular epimorphism is an isomorphism if it is a monomorphism. -/
def is_iso_of_regular_epi_of_mono (f : X ⟶ Y) [regular_epi f] [m : mono f] : is_iso f :=
@is_iso_limit_cocone_parallel_pair_of_epi _ _ _ _ _ _ _ regular_epi.is_colimit m
@[priority 100]
instance strong_epi_of_regular_epi (f : X ⟶ Y) [regular_epi f] : strong_epi f :=
{ epi := by apply_instance,
has_lift :=
begin
introsI,
have : (regular_epi.left : regular_epi.W f ⟶ X) ≫ u = regular_epi.right ≫ u,
{ apply (cancel_mono z).1,
simp only [category.assoc, h, regular_epi.w_assoc] },
obtain ⟨t, ht⟩ := regular_epi.desc' f u this,
exact ⟨t, ht, (cancel_epi f).1
(by simp only [←category.assoc, ht, ←h, arrow.mk_hom, arrow.hom_mk'_right])⟩,
end }
section
variables [has_zero_morphisms C]
/-- A normal epimorphism is a morphism which is the cokernel of some morphism. -/
class normal_epi (f : X ⟶ Y) :=
(W : C)
(g : W ⟶ X)
(w : g ≫ f = 0)
(is_colimit : is_colimit (cokernel_cofork.of_π f w))
/-- Every normal epimorphism is a regular epimorphism. -/
@[priority 100]
instance normal_epi.regular_epi (f : X ⟶ Y) [I : normal_epi f] : regular_epi f :=
{ left := I.g,
right := 0,
w := (by simpa using I.w),
..I }
/-- If `f` is a normal epi, then every morphism `k : X ⟶ W` satisfying `normal_epi.g ≫ k = 0`
induces `l : Y ⟶ W` such that `f ≫ l = k`. -/
def normal_epi.desc' {W : C} (f : X ⟶ Y) [normal_epi f] (k : X ⟶ W) (h : normal_epi.g ≫ k = 0) :
{l : Y ⟶ W // f ≫ l = k} :=
cokernel_cofork.is_colimit.desc' (normal_epi.is_colimit) _ h
/--
The second leg of a pushout cocone is a normal epimorphism if the right component is too.
See also `pushout.snd_of_epi` for the basic epimorphism version, and
`normal_of_is_pushout_fst_of_normal` for the flipped version.
-/
def normal_of_is_pushout_snd_of_normal {P Q R S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S}
[gn : normal_epi g] (comm : f ≫ h = g ≫ k) (t : is_colimit (pushout_cocone.mk _ _ comm)) :
normal_epi h :=
{ W := gn.W,
g := gn.g ≫ f,
w := by rw [category.assoc, comm, reassoc_of gn.w, has_zero_morphisms.zero_comp],
is_colimit :=
begin
letI hn := regular_of_is_pushout_snd_of_regular comm t,
have q := (has_zero_morphisms.zero_comp gn.W f).symm,
convert hn.is_colimit,
dunfold cokernel_cofork.of_π cofork.of_π,
congr, exact q, exact q, exact q, apply proof_irrel_heq,
end }
/--
The first leg of a pushout cocone is a normal epimorphism if the left component is too.
See also `pushout.fst_of_epi` for the basic epimorphism version, and
`normal_of_is_pushout_snd_of_normal` for the flipped version.
-/
def normal_of_is_pushout_fst_of_normal {P Q R S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S}
[hn : normal_epi f] (comm : f ≫ h = g ≫ k) (t : is_colimit (pushout_cocone.mk _ _ comm)) :
normal_epi k :=
normal_of_is_pushout_snd_of_normal comm.symm (pushout_cocone.flip_is_colimit t)
end
end category_theory
|
1fd9f2fcc5bd8c5efee02501ef766b4c4fbf4719 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/ring_theory/prime.lean | 2ac7f35dbe098ada776d513508a9967b3523129c | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,585 | lean | /-
Copyright (c) 2020 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.algebra.associated
import Mathlib.algebra.big_operators.basic
import Mathlib.PostPort
universes u_1 u_2
namespace Mathlib
/-!
# Prime elements in rings
This file contains lemmas about prime elements of commutative rings.
-/
/-- If `x * y = a * ∏ i in s, p i` where `p i` is always prime, then
`x` and `y` can both be written as a divisor of `a` multiplied by
a product over a subset of `s` -/
theorem mul_eq_mul_prime_prod {R : Type u_1} [comm_cancel_monoid_with_zero R] {α : Type u_2} [DecidableEq α] {x : R} {y : R} {a : R} {s : finset α} {p : α → R} (hp : ∀ (i : α), i ∈ s → prime (p i)) (hx : x * y = a * finset.prod s fun (i : α) => p i) : ∃ (t : finset α),
∃ (u : finset α),
∃ (b : R),
∃ (c : R),
t ∪ u = s ∧
disjoint t u ∧
a = b * c ∧ (x = b * finset.prod t fun (i : α) => p i) ∧ y = c * finset.prod u fun (i : α) => p i := sorry
/-- If ` x * y = a * p ^ n` where `p` is prime, then `x` and `y` can both be written
as the product of a power of `p` and a divisor of `a`. -/
theorem mul_eq_mul_prime_pow {R : Type u_1} [comm_cancel_monoid_with_zero R] {x : R} {y : R} {a : R} {p : R} {n : ℕ} (hp : prime p) (hx : x * y = a * p ^ n) : ∃ (i : ℕ), ∃ (j : ℕ), ∃ (b : R), ∃ (c : R), i + j = n ∧ a = b * c ∧ x = b * p ^ i ∧ y = c * p ^ j := sorry
|
011dcc9d8b6fa01dff0bb7e4ddd5963acd749526 | 076f5040b63237c6dd928c6401329ed5adcb0e44 | /assignments/hw5_higher_order_functions.lean | 7993b913d40874de10065b9e206c09721de0908d | [] | no_license | kevinsullivan/uva-cs-dm-f19 | 0f123689cf6cb078f263950b18382a7086bf30be | 09a950752884bd7ade4be33e9e89a2c4b1927167 | refs/heads/master | 1,594,771,841,541 | 1,575,853,850,000 | 1,575,853,850,000 | 205,433,890 | 4 | 9 | null | 1,571,592,121,000 | 1,567,188,539,000 | Lean | UTF-8 | Lean | false | false | 7,449 | lean | open list
/-
#1. You are to implement a function
that takes a "predicate" function and a
list and that returns a new list: namely
a list that contains all and only those
elements of the first list for which the
given predicate function returns true.
We start by giving you two predicate
functions. Each takes an argument, n,
of type ℕ. The first returns true if
and only if n < 5. The second returns
true if and only if n is even.
-/
/-
Here's a "predicate" function that
takes a natural number as an argument
and returns true if the number is less
than 5 otherwise it returns false.
-/
def lt_5 (n : ℕ) : bool :=
n < 5
-- example cases
#eval lt_5 2
#eval lt_5 6
/-
Here's another predicate function, one
that returns true if a given nat is even,
and false otherwise. Note that we have
defined this function recursively. Zero
is even, one is not, and (2 + n') is if
and only if n' is.
You can think of a predicate function
as a function that answers the question,
does some value (here a nat) have some
property? The properties in these two
cases are (1) the property of being less
than 5, and (2) the property of being
even.
-/
def evenb : ℕ → bool
| 0 := tt
| 1 := ff
| (n' + 2) := evenb n'
#eval evenb 0
#eval evenb 3
#eval evenb 4
#eval evenb 5
/-
We call the function you are going to
implement a "filter" function, because
it takes a list and returns a "filtered"
version of the list. Call you function
"mfilter".
If you filter an empty list you always
get an empty list as a result. If you
filter a non-empty list, l = (cons h t),
the returned list has h at its head if
and only if the predicate function applied
to h returns true, otherwise the returned
value is just the filtered version of t.
A. [15 points]
Your task is to complete an incomplete
version of the definition of the mfilter
function. We use a Lean construct new to
you: the if ... then ... else. It works
as you would expect from your work with
other programming languages. Replace the
underscores to complete the definition.
-/
def mfilter : (ℕ → bool) → list ℕ → list ℕ
| p [] := []
| p (cons h t) :=
if _
then (cons _ _)
else _
/-
Here's the definition of a simple list.
-/
def a_list := [0,1,2,3,4,5,6,7,8,9]
/-
B. [10 points]
Replace the underscores in the first two
eval commands below as follows. Replace
the first one with an expression in which
mfilter is used to compute a new list that
contains the numbers in a_list that are
less than 5. Replace the second one with
an expression in which mfilter is used to
compute a list containing even elements of
a_list. You may use the predicate functions
we defined above.
Replace the third underscore with a similar
expression but where you use a λ expression
to specify a predicate function that takes
a nat, n, and returns tt if n is equal to
three and false otherwise. Hint: n=3 is
an expression that will return the desired
bool.
-/
#eval _
#eval _
#eval _
/-
#2.
Here's a function that takes a function, f,
from ℕ to ℕ, and a value, n, of type ℕ, and
that returns the value that is obtained by
simply applying f to n.
-/
def f_apply : (ℕ → ℕ) → ℕ → ℕ
| f n := (f n)
-- examples of its use
#eval f_apply nat.succ 3
#eval f_apply (λ n : ℕ, n * n) 3
/-
A. [10 points]
Write a function, f_apply_2, that takes a
function, f, from ℕ to ℕ, and a value, n,
of type ℕ, and that returns (read this
carefully) the value obtained by applying
f twice to n: the result of applying f to
the result of applying f to n. For example,
if f is the function that squares its nat
argument, then (f_apply_2 f 3) returns 81,
as f applied to 3 is 9 and f applied to 9
is 81.
-/
-- Your answer here
def ff_apply : (ℕ → ℕ) → ℕ → ℕ
| f n := _
/-
B. [10 points]
Write a function f_apply_k that takes (1)
a function, f, of type ℕ to ℕ, (2) a value,
n, of type ℕ, and (3) a value, k of type ℕ,
and that returns the result of applying f
to n k times.
Note that f_apply applies f to n once and
ff_apply applies f to n twice. Your job is
to write a function that is general in the
sense that you specify by a parameter, k,
how many times to apply f.
Hint 1: Use recursion. Note: The result of
applying any function, f, to n, zero times
is just n.
-/
-- Answer here
/-
Use #eval to evaluate an expression in
which the squaring function, expressed
as a λ expression, is applied to 3 two
times. You should be able to confirm that
you get the same answer given by using
the ff_apply function in the example above.
-/
-- Answer here
/-
C. [Extra Credit]
Write a function f_apply_k_fun that takes (1)
a function, f, of type ℕ to ℕ, (2) a value,
k of type ℕ, and that returns a function that,
when applied to a natural number, n, returns
the result of applying f to n k times.
-/
/-
#3: [15 points]
Write a function, mcompose, that
takes two functions, g and f (in that
order), each of type ℕ → ℕ, and that
returns *a function* that, when applied
to an argument, n, of type ℕ, returns
the result of applying g to the result
of applying f to n.
-/
-- Answer here
/-
#4. Higher-Order Functions
4A. [10 points] Provide an implementatation of
a function, map_pred that takes as its arguments
(1) a predicate function of type ℕ → bool, (2) a
list of natural numbers (of type "list nat"),
and that returns a list in which each ℕ value,
n,in the argument list is replaced by true (tt)
if the predicate returns true for a, otherwise
false (ff).
For example, if the predicate function returns
true if and only if its argument is zero, then
applying map to this function and to the list
[0,1,2,0,1,0] must return [tt,ff,ff,tt,ff,tt].
Test your code by using #eval or #reduce to evaluate
an expression in which map_pred is applied to
such an "is_zero" predicate function and to the
list 0,1,2,0,1,0]. Express the predicate function
as a lambda abstraction within the #eval command.
NOTE: You will have to use list.nil and list.cons
to refer to the nil and cons constructors of the
library-provided list data type, as you already
have definitions for list and cons in the current
namespace.
-/
-- Answer here
/-
4B. [10 points] Implement a function, reduce_or,
that takes as its argument a list of Boolean values
and that reduces the list to a single Boolean value:
tt if there is at least one true value in the list,
otherwise ff. Note: the Lean libraries provide the
function "bor" to compute "b1 or b2", where b1 and
b2 are Booleans. We recommend that you include
tests of your solution.
-/
-- example
#reduce bor tt tt
-- Answer here
/-
4C. [10points] Implement a function, reduce_and,
that takes as its argument a list of Boolean values
and that reduces the list to a single Boolean value:
tt if every value in the list is true, otherwise ff.
-/
-- Note: band implements the Boolean "and" function
#reduce band tt tt
-- Answer here
/-
4D. [10 points] Define a function, all_zero, that
takes a list of nat values and returns true if and
only if they are all zero. Express your answer using
map and reduce functions that you have previously
defined above. Again we recommend that you test your
solution.
-/
-- Answer here (replace the _'s as needed)
def all_zero : list nat → bool
| [] := _
| _ := _
/-
Some tests
-/
#reduce all_zero []
#reduce all_zero [0,0,0,0]
#reduce all_zero [1,0,0,0]
#reduce all_zero [0,1,0,0]
#reduce all_zero [1,0,0,1]
|
01839401ac8d7a1c33614a71667a69d8863a7ffc | 4f065978c49388d188224610d9984673079f7d91 | /proofs_by_induction.lean | 6a74cd7fbb9c3853d8c971c4eca5fba254e079ff | [] | no_license | kckennylau/Lean | b323103f52706304907adcfaee6f5cb8095d4a33 | 907d0a4d2bd8f23785abd6142ad53d308c54fdcb | refs/heads/master | 1,624,623,720,653 | 1,563,901,820,000 | 1,563,901,820,000 | 109,506,702 | 3 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 3,725 | lean | import algebra.big_operators data.fintype
import tactic.ring
-- https://xenaproject.wordpress.com/2018/03/30/proofs-by-induction/
open nat
def odd : ℕ → ℕ := λ i, 2 * i + 1
def square : ℕ → ℕ := λ i, i ^ 2
theorem odd_square_inductive_step (d : ℕ) :
square d + odd d = square (succ d) :=
by dsimp [square, odd, pow]; rw [succ_eq_add_one]; ring
namespace def1
definition my_sum_to_n (summand : ℕ → ℕ) : ℕ → ℕ
| 0 := 0
| (succ n) := my_sum_to_n n + summand n
theorem my_zero_theorem (summand : ℕ → ℕ) :
my_sum_to_n summand 0 = 0 :=
rfl
theorem my_successor_theorem (summand : ℕ → ℕ) (n : ℕ) :
my_sum_to_n summand (succ n) = my_sum_to_n summand n + summand n :=
rfl
theorem my_odd_square_theorem : ∀ (n : ℕ), my_sum_to_n odd n = square n
| 0 := rfl
| (succ n) := by unfold my_sum_to_n; rw [my_odd_square_theorem n]; exact odd_square_inductive_step n
end def1
namespace def2
definition my_sum_to_n (summand : ℕ → ℕ) : ℕ → ℕ :=
λ n, ((list.range n).map summand).sum
theorem my_zero_theorem (summand : ℕ → ℕ) :
my_sum_to_n summand 0 = 0 :=
rfl
theorem my_successor_theorem (summand : ℕ → ℕ) (n : ℕ) :
my_sum_to_n summand (succ n) = my_sum_to_n summand n + summand n :=
by unfold my_sum_to_n; simp [list.range_concat]
theorem my_odd_square_theorem : ∀ (n : ℕ), my_sum_to_n odd n = square n
| 0 := rfl
| (succ n) := by rw [my_successor_theorem, my_odd_square_theorem n]; exact odd_square_inductive_step n
end def2
namespace def3
definition my_sum_to_n (summand : ℕ → ℕ) : ℕ → ℕ :=
λ n, (finset.range n).sum summand
theorem my_zero_theorem (summand : ℕ → ℕ) :
my_sum_to_n summand 0 = 0 :=
rfl
theorem my_successor_theorem (summand : ℕ → ℕ) (n : ℕ) :
my_sum_to_n summand (succ n) = my_sum_to_n summand n + summand n :=
by unfold my_sum_to_n; simp
theorem my_odd_square_theorem : ∀ (n : ℕ), my_sum_to_n odd n = square n
| 0 := rfl
| (succ n) := by rw [my_successor_theorem, my_odd_square_theorem n]; exact odd_square_inductive_step n
end def3
namespace def4
open finset nat
-- Credits to Chris Hughes
theorem chris (n : ℕ) (f : ℕ → ℕ) (g : fin n → ℕ) (h : ∀ i : fin n, f i.1 = g i) :
(range n).sum f = univ.sum g :=
sum_bij
(λ i h, ⟨i, mem_range.1 h⟩)
(λ i h, mem_univ _)
(λ a ha, h ⟨a, _⟩)
(λ _ _ _ _, fin.veq_of_eq)
(λ ⟨b, hb⟩ _, ⟨b, mem_range.2 hb, rfl⟩)
definition my_sum_to_n (summand : ℕ → ℕ) : ℕ → ℕ :=
λ n, (@finset.univ (fin n) _).sum (summand ∘ fin.val)
theorem my_zero_theorem (summand : ℕ → ℕ) :
my_sum_to_n summand 0 = 0 :=
rfl
-- Credits to Mario Carneiro
theorem my_successor_theorem (summand : ℕ → ℕ) (n : ℕ) :
my_sum_to_n summand (succ n) = my_sum_to_n summand n + summand n :=
by unfold my_sum_to_n;
rw [← chris _ _ _ (λ _, rfl), ← chris _ _ _ (λ _, rfl)]; simp
theorem my_odd_square_theorem : ∀ (n : ℕ), my_sum_to_n odd n = square n
| 0 := rfl
| (succ n) := by rw [my_successor_theorem, my_odd_square_theorem n]; exact odd_square_inductive_step n
end def4
namespace equality
theorem def12 : def1.my_sum_to_n = def2.my_sum_to_n :=
funext $ λ summand, funext $ λ n, nat.rec_on n rfl $
λ m ih, by rw [def1.my_successor_theorem, def2.my_successor_theorem, ih]
theorem def23 : def2.my_sum_to_n = def3.my_sum_to_n :=
funext $ λ summand, funext $ λ n, nat.rec_on n rfl $
λ m ih, by rw [def2.my_successor_theorem, def3.my_successor_theorem, ih]
theorem def34 : def3.my_sum_to_n = def4.my_sum_to_n :=
funext $ λ summand, funext $ λ n, nat.rec_on n rfl $
λ m ih, by rw [def3.my_successor_theorem, def4.my_successor_theorem, ih]
end equality
|
202065744bf3fd6ef816d511954b1967f93e7d99 | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/data/int/parity.lean | 0684b9e46463c424befa2f057b6a9728c06fd320 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 2,951 | lean | /-
Copyright (c) 2019 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
The `even` predicate on the integers.
-/
import .modeq data.nat.parity algebra.group_power
namespace int
@[simp] theorem mod_two_ne_one {n : int} : ¬ n % 2 = 1 ↔ n % 2 = 0 :=
by cases mod_two_eq_zero_or_one n with h h; simp [h]
@[simp] theorem mod_two_ne_zero {n : int} : ¬ n % 2 = 0 ↔ n % 2 = 1 :=
by cases mod_two_eq_zero_or_one n with h h; simp [h]
def even (n : int) : Prop := 2 ∣ n
@[simp] theorem even_coe_nat (n : nat) : even n ↔ nat.even n :=
have ∀ m, 2 * to_nat m = to_nat (2 * m),
from λ m, by cases m; refl,
⟨λ ⟨m, hm⟩, ⟨to_nat m, by rw [this, ←to_nat_coe_nat n, hm]⟩,
λ ⟨m, hm⟩, ⟨m, by simp [hm]⟩⟩
theorem even_iff {n : int} : even n ↔ n % 2 = 0 :=
⟨λ ⟨m, hm⟩, by simp [hm], λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by simp [h])⟩⟩
lemma not_even_iff {n : ℤ} : ¬ even n ↔ n % 2 = 1 :=
by rw [even_iff, mod_two_ne_zero]
instance : decidable_pred even :=
λ n, decidable_of_decidable_of_iff (by apply_instance) even_iff.symm
@[simp] theorem even_zero : even (0 : int) := ⟨0, dec_trivial⟩
@[simp] theorem not_even_one : ¬ even (1 : int) :=
by rw even_iff; apply one_ne_zero
@[simp] theorem even_bit0 (n : int) : even (bit0 n) :=
⟨n, by rw [bit0, two_mul]⟩
@[parity_simps] theorem even_add {m n : int} : even (m + n) ↔ (even m ↔ even n) :=
begin
cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂;
simp [even_iff, h₁, h₂],
{ exact @modeq.modeq_add _ _ 0 _ 0 h₁ h₂ },
{ exact @modeq.modeq_add _ _ 0 _ 1 h₁ h₂ },
{ exact @modeq.modeq_add _ _ 1 _ 0 h₁ h₂ },
exact @modeq.modeq_add _ _ 1 _ 1 h₁ h₂
end
@[parity_simps] theorem even_neg {n : ℤ} : even (-n) ↔ even n := by simp [even]
@[simp] theorem not_even_bit1 (n : int) : ¬ even (bit1 n) :=
by simp [bit1] with parity_simps
@[parity_simps] theorem even_sub {m n : int} : even (m - n) ↔ (even m ↔ even n) :=
by simp with parity_simps
@[parity_simps] theorem even_mul {m n : int} : even (m * n) ↔ even m ∨ even n :=
begin
cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂;
simp [even_iff, h₁, h₂],
{ exact @modeq.modeq_mul _ _ 0 _ 0 h₁ h₂ },
{ exact @modeq.modeq_mul _ _ 0 _ 1 h₁ h₂ },
{ exact @modeq.modeq_mul _ _ 1 _ 0 h₁ h₂ },
exact @modeq.modeq_mul _ _ 1 _ 1 h₁ h₂
end
@[parity_simps] theorem even_pow {m : int} {n : nat} : even (m^n) ↔ even m ∧ n ≠ 0 :=
by { induction n with n ih; simp [*, even_mul, pow_succ], tauto }
-- Here are examples of how `parity_simps` can be used with `int`.
example (m n : int) (h : even m) : ¬ even (n + 3) ↔ even (m^2 + m + n) :=
by simp [*, (dec_trivial : ¬ 2 = 0)] with parity_simps
example : ¬ even (25394535 : int) :=
by simp
end int
|
ce3bbbfabc9751a5d454e15cee8c4bb6645286d0 | 367134ba5a65885e863bdc4507601606690974c1 | /src/algebra/category/CommRing/adjunctions.lean | 2328a7b76fb0eb76204d229ac07637bd9154fe94 | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 1,701 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Johannes Hölzl
-/
import algebra.category.CommRing.basic
import data.mv_polynomial
/-!
Multivariable polynomials on a type is the left adjoint of the
forgetful functor from commutative rings to types.
-/
noncomputable theory
universe u
open mv_polynomial
open category_theory
namespace CommRing
open_locale classical
/--
The free functor `Type u ⥤ CommRing` sending a type `X` to the multivariable (commutative)
polynomials with variables `x : X`.
-/
def free : Type u ⥤ CommRing :=
{ obj := λ α, of (mv_polynomial α ℤ),
map := λ X Y f,
-- otherwise this finds `algebra_int`, which is not defeq to the one used by `mv_polynomial`.
let Zalg : algebra ℤ ℤ := algebra.id ℤ in by exactI
(↑(rename f : _ →ₐ[ℤ] _) : (mv_polynomial X ℤ →+* mv_polynomial Y ℤ)),
-- TODO these next two fields can be done by `tidy`, but the calls in `dsimp` and `simp` it
-- generates are too slow.
map_id' := λ X, ring_hom.ext $ rename_id,
map_comp' := λ X Y Z f g, ring_hom.ext $ λ p, (rename_rename f g p).symm }
@[simp] lemma free_obj_coe {α : Type u} :
(free.obj α : Type u) = mv_polynomial α ℤ := rfl
@[simp] lemma free_map_coe {α β : Type u} {f : α → β} :
⇑(free.map f) = rename f := rfl
/--
The free-forgetful adjunction for commutative rings.
-/
def adj : free ⊣ forget CommRing :=
adjunction.mk_of_hom_equiv
{ hom_equiv := λ X R, hom_equiv,
hom_equiv_naturality_left_symm' :=
λ _ _ Y f g, ring_hom.ext $ λ x, eval₂_cast_comp f (int.cast_ring_hom Y) g x }
end CommRing
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.